2 * Copyright (C) 2007 Google, Inc.
3 * Copyright (C) 2012 Intel, Inc.
4 * Copyright (C) 2017 Imagination Technologies Ltd.
6 * This software is licensed under the terms of the GNU General Public
7 * License version 2, as published by the Free Software Foundation, and
8 * may be copied, distributed, and modified under those terms.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
17 #include <linux/console.h>
18 #include <linux/interrupt.h>
19 #include <linux/platform_device.h>
20 #include <linux/tty.h>
21 #include <linux/tty_flip.h>
22 #include <linux/slab.h>
24 #include <linux/module.h>
25 #include <linux/goldfish.h>
27 #include <linux/dma-mapping.h>
28 #include <linux/serial_core.h>
30 /* Goldfish tty register's offsets */
31 #define GOLDFISH_TTY_REG_BYTES_READY 0x04
32 #define GOLDFISH_TTY_REG_CMD 0x08
33 #define GOLDFISH_TTY_REG_DATA_PTR 0x10
34 #define GOLDFISH_TTY_REG_DATA_LEN 0x14
35 #define GOLDFISH_TTY_REG_DATA_PTR_HIGH 0x18
36 #define GOLDFISH_TTY_REG_VERSION 0x20
38 /* Goldfish tty commands */
39 #define GOLDFISH_TTY_CMD_INT_DISABLE 0
40 #define GOLDFISH_TTY_CMD_INT_ENABLE 1
41 #define GOLDFISH_TTY_CMD_WRITE_BUFFER 2
42 #define GOLDFISH_TTY_CMD_READ_BUFFER 3
50 struct console console;
55 static DEFINE_MUTEX(goldfish_tty_lock);
56 static struct tty_driver *goldfish_tty_driver;
57 static u32 goldfish_tty_line_count = 8;
58 static u32 goldfish_tty_current_line_count;
59 static struct goldfish_tty *goldfish_ttys;
61 static void do_rw_io(struct goldfish_tty *qtty,
62 unsigned long address,
66 unsigned long irq_flags;
67 void __iomem *base = qtty->base;
69 spin_lock_irqsave(&qtty->lock, irq_flags);
70 gf_write_ptr((void *)address, base + GOLDFISH_TTY_REG_DATA_PTR,
71 base + GOLDFISH_TTY_REG_DATA_PTR_HIGH);
72 writel(count, base + GOLDFISH_TTY_REG_DATA_LEN);
75 writel(GOLDFISH_TTY_CMD_WRITE_BUFFER,
76 base + GOLDFISH_TTY_REG_CMD);
78 writel(GOLDFISH_TTY_CMD_READ_BUFFER,
79 base + GOLDFISH_TTY_REG_CMD);
81 spin_unlock_irqrestore(&qtty->lock, irq_flags);
84 static void goldfish_tty_rw(struct goldfish_tty *qtty,
89 dma_addr_t dma_handle;
90 enum dma_data_direction dma_dir;
92 dma_dir = (is_write ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
93 if (qtty->version > 0) {
95 * Goldfish TTY for Ranchu platform uses
96 * physical addresses and DMA for read/write operations
98 unsigned long addr_end = addr + count;
100 while (addr < addr_end) {
101 unsigned long pg_end = (addr & PAGE_MASK) + PAGE_SIZE;
103 pg_end < addr_end ? pg_end : addr_end;
104 unsigned long avail = next - addr;
107 * Map the buffer's virtual address to the DMA address
108 * so the buffer can be accessed by the device.
110 dma_handle = dma_map_single(qtty->dev, (void *)addr,
113 if (dma_mapping_error(qtty->dev, dma_handle)) {
114 dev_err(qtty->dev, "tty: DMA mapping error.\n");
117 do_rw_io(qtty, dma_handle, avail, is_write);
120 * Unmap the previously mapped region after
121 * the completion of the read/write operation.
123 dma_unmap_single(qtty->dev, dma_handle, avail, dma_dir);
129 * Old style Goldfish TTY used on the Goldfish platform
130 * uses virtual addresses.
132 do_rw_io(qtty, addr, count, is_write);
136 static void goldfish_tty_do_write(int line, const char *buf,
139 struct goldfish_tty *qtty = &goldfish_ttys[line];
140 unsigned long address = (unsigned long)(void *)buf;
142 goldfish_tty_rw(qtty, address, count, 1);
145 static irqreturn_t goldfish_tty_interrupt(int irq, void *dev_id)
147 struct goldfish_tty *qtty = dev_id;
148 void __iomem *base = qtty->base;
149 unsigned long address;
153 count = readl(base + GOLDFISH_TTY_REG_BYTES_READY);
157 count = tty_prepare_flip_string(&qtty->port, &buf, count);
159 address = (unsigned long)(void *)buf;
160 goldfish_tty_rw(qtty, address, count, 0);
162 tty_schedule_flip(&qtty->port);
166 static int goldfish_tty_activate(struct tty_port *port, struct tty_struct *tty)
168 struct goldfish_tty *qtty = container_of(port, struct goldfish_tty,
170 writel(GOLDFISH_TTY_CMD_INT_ENABLE, qtty->base + GOLDFISH_TTY_REG_CMD);
174 static void goldfish_tty_shutdown(struct tty_port *port)
176 struct goldfish_tty *qtty = container_of(port, struct goldfish_tty,
178 writel(GOLDFISH_TTY_CMD_INT_DISABLE, qtty->base + GOLDFISH_TTY_REG_CMD);
181 static int goldfish_tty_open(struct tty_struct *tty, struct file *filp)
183 struct goldfish_tty *qtty = &goldfish_ttys[tty->index];
184 return tty_port_open(&qtty->port, tty, filp);
187 static void goldfish_tty_close(struct tty_struct *tty, struct file *filp)
189 tty_port_close(tty->port, tty, filp);
192 static void goldfish_tty_hangup(struct tty_struct *tty)
194 tty_port_hangup(tty->port);
197 static int goldfish_tty_write(struct tty_struct *tty, const unsigned char *buf,
200 goldfish_tty_do_write(tty->index, buf, count);
204 static int goldfish_tty_write_room(struct tty_struct *tty)
209 static int goldfish_tty_chars_in_buffer(struct tty_struct *tty)
211 struct goldfish_tty *qtty = &goldfish_ttys[tty->index];
212 void __iomem *base = qtty->base;
213 return readl(base + GOLDFISH_TTY_REG_BYTES_READY);
216 static void goldfish_tty_console_write(struct console *co, const char *b,
219 goldfish_tty_do_write(co->index, b, count);
222 static struct tty_driver *goldfish_tty_console_device(struct console *c,
226 return goldfish_tty_driver;
229 static int goldfish_tty_console_setup(struct console *co, char *options)
231 if ((unsigned)co->index >= goldfish_tty_line_count)
233 if (!goldfish_ttys[co->index].base)
238 static const struct tty_port_operations goldfish_port_ops = {
239 .activate = goldfish_tty_activate,
240 .shutdown = goldfish_tty_shutdown
243 static const struct tty_operations goldfish_tty_ops = {
244 .open = goldfish_tty_open,
245 .close = goldfish_tty_close,
246 .hangup = goldfish_tty_hangup,
247 .write = goldfish_tty_write,
248 .write_room = goldfish_tty_write_room,
249 .chars_in_buffer = goldfish_tty_chars_in_buffer,
252 static int goldfish_tty_create_driver(void)
255 struct tty_driver *tty;
257 goldfish_ttys = kzalloc(sizeof(*goldfish_ttys) *
258 goldfish_tty_line_count, GFP_KERNEL);
259 if (goldfish_ttys == NULL) {
261 goto err_alloc_goldfish_ttys_failed;
263 tty = alloc_tty_driver(goldfish_tty_line_count);
266 goto err_alloc_tty_driver_failed;
268 tty->driver_name = "goldfish";
270 tty->type = TTY_DRIVER_TYPE_SERIAL;
271 tty->subtype = SERIAL_TYPE_NORMAL;
272 tty->init_termios = tty_std_termios;
273 tty->flags = TTY_DRIVER_RESET_TERMIOS | TTY_DRIVER_REAL_RAW |
274 TTY_DRIVER_DYNAMIC_DEV;
275 tty_set_operations(tty, &goldfish_tty_ops);
276 ret = tty_register_driver(tty);
278 goto err_tty_register_driver_failed;
280 goldfish_tty_driver = tty;
283 err_tty_register_driver_failed:
285 err_alloc_tty_driver_failed:
286 kfree(goldfish_ttys);
287 goldfish_ttys = NULL;
288 err_alloc_goldfish_ttys_failed:
292 static void goldfish_tty_delete_driver(void)
294 tty_unregister_driver(goldfish_tty_driver);
295 put_tty_driver(goldfish_tty_driver);
296 goldfish_tty_driver = NULL;
297 kfree(goldfish_ttys);
298 goldfish_ttys = NULL;
301 static int goldfish_tty_probe(struct platform_device *pdev)
303 struct goldfish_tty *qtty;
306 struct device *ttydev;
311 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
313 pr_err("goldfish_tty: No MEM resource available!\n");
317 base = ioremap(r->start, 0x1000);
319 pr_err("goldfish_tty: Unable to ioremap base!\n");
323 r = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
325 pr_err("goldfish_tty: No IRQ resource available!\n");
331 mutex_lock(&goldfish_tty_lock);
333 if (pdev->id == PLATFORM_DEVID_NONE)
334 line = goldfish_tty_current_line_count;
338 if (line >= goldfish_tty_line_count) {
339 pr_err("goldfish_tty: Reached maximum tty number of %d.\n",
340 goldfish_tty_current_line_count);
345 if (goldfish_tty_current_line_count == 0) {
346 ret = goldfish_tty_create_driver();
350 goldfish_tty_current_line_count++;
352 qtty = &goldfish_ttys[line];
353 spin_lock_init(&qtty->lock);
354 tty_port_init(&qtty->port);
355 qtty->port.ops = &goldfish_port_ops;
358 qtty->dev = &pdev->dev;
361 * Goldfish TTY device used by the Goldfish emulator
362 * should identify itself with 0, forcing the driver
363 * to use virtual addresses. Goldfish TTY device
364 * on Ranchu emulator (qemu2) returns 1 here and
365 * driver will use physical addresses.
367 qtty->version = readl(base + GOLDFISH_TTY_REG_VERSION);
370 * Goldfish TTY device on Ranchu emulator (qemu2)
371 * will use DMA for read/write IO operations.
373 if (qtty->version > 0) {
375 * Initialize dma_mask to 32-bits.
377 if (!pdev->dev.dma_mask)
378 pdev->dev.dma_mask = &pdev->dev.coherent_dma_mask;
379 ret = dma_set_mask(&pdev->dev, DMA_BIT_MASK(32));
381 dev_err(&pdev->dev, "No suitable DMA available.\n");
382 goto err_dec_line_count;
386 writel(GOLDFISH_TTY_CMD_INT_DISABLE, base + GOLDFISH_TTY_REG_CMD);
388 ret = request_irq(irq, goldfish_tty_interrupt, IRQF_SHARED,
389 "goldfish_tty", qtty);
391 pr_err("goldfish_tty: No IRQ available!\n");
392 goto err_dec_line_count;
395 ttydev = tty_port_register_device(&qtty->port, goldfish_tty_driver,
397 if (IS_ERR(ttydev)) {
398 ret = PTR_ERR(ttydev);
399 goto err_tty_register_device_failed;
402 strcpy(qtty->console.name, "ttyGF");
403 qtty->console.write = goldfish_tty_console_write;
404 qtty->console.device = goldfish_tty_console_device;
405 qtty->console.setup = goldfish_tty_console_setup;
406 qtty->console.flags = CON_PRINTBUFFER;
407 qtty->console.index = line;
408 register_console(&qtty->console);
409 platform_set_drvdata(pdev, qtty);
411 mutex_unlock(&goldfish_tty_lock);
414 err_tty_register_device_failed:
417 goldfish_tty_current_line_count--;
418 if (goldfish_tty_current_line_count == 0)
419 goldfish_tty_delete_driver();
421 mutex_unlock(&goldfish_tty_lock);
427 static int goldfish_tty_remove(struct platform_device *pdev)
429 struct goldfish_tty *qtty = platform_get_drvdata(pdev);
431 mutex_lock(&goldfish_tty_lock);
433 unregister_console(&qtty->console);
434 tty_unregister_device(goldfish_tty_driver, qtty->console.index);
437 free_irq(qtty->irq, pdev);
438 goldfish_tty_current_line_count--;
439 if (goldfish_tty_current_line_count == 0)
440 goldfish_tty_delete_driver();
441 mutex_unlock(&goldfish_tty_lock);
445 static void gf_early_console_putchar(struct uart_port *port, int ch)
447 __raw_writel(ch, port->membase);
450 static void gf_early_write(struct console *con, const char *s, unsigned int n)
452 struct earlycon_device *dev = con->data;
454 uart_console_write(&dev->port, s, n, gf_early_console_putchar);
457 static int __init gf_earlycon_setup(struct earlycon_device *device,
460 if (!device->port.membase)
463 device->con->write = gf_early_write;
467 OF_EARLYCON_DECLARE(early_gf_tty, "google,goldfish-tty", gf_earlycon_setup);
469 static const struct of_device_id goldfish_tty_of_match[] = {
470 { .compatible = "google,goldfish-tty", },
474 MODULE_DEVICE_TABLE(of, goldfish_tty_of_match);
476 static struct platform_driver goldfish_tty_platform_driver = {
477 .probe = goldfish_tty_probe,
478 .remove = goldfish_tty_remove,
480 .name = "goldfish_tty",
481 .of_match_table = goldfish_tty_of_match,
485 module_platform_driver(goldfish_tty_platform_driver);
487 MODULE_LICENSE("GPL v2");