Linux 4.18.10
[linux/fpc-iii.git] / drivers / tty / goldfish.c
blob37caba7c3affda7c32a76513f5a31518ca3572a8
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Copyright (C) 2007 Google, Inc.
4 * Copyright (C) 2012 Intel, Inc.
5 * Copyright (C) 2017 Imagination Technologies Ltd.
6 */
8 #include <linux/console.h>
9 #include <linux/interrupt.h>
10 #include <linux/platform_device.h>
11 #include <linux/tty.h>
12 #include <linux/tty_flip.h>
13 #include <linux/slab.h>
14 #include <linux/io.h>
15 #include <linux/module.h>
16 #include <linux/goldfish.h>
17 #include <linux/mm.h>
18 #include <linux/dma-mapping.h>
19 #include <linux/serial_core.h>
21 /* Goldfish tty register's offsets */
22 #define GOLDFISH_TTY_REG_BYTES_READY 0x04
23 #define GOLDFISH_TTY_REG_CMD 0x08
24 #define GOLDFISH_TTY_REG_DATA_PTR 0x10
25 #define GOLDFISH_TTY_REG_DATA_LEN 0x14
26 #define GOLDFISH_TTY_REG_DATA_PTR_HIGH 0x18
27 #define GOLDFISH_TTY_REG_VERSION 0x20
29 /* Goldfish tty commands */
30 #define GOLDFISH_TTY_CMD_INT_DISABLE 0
31 #define GOLDFISH_TTY_CMD_INT_ENABLE 1
32 #define GOLDFISH_TTY_CMD_WRITE_BUFFER 2
33 #define GOLDFISH_TTY_CMD_READ_BUFFER 3
35 struct goldfish_tty {
36 struct tty_port port;
37 spinlock_t lock;
38 void __iomem *base;
39 u32 irq;
40 int opencount;
41 struct console console;
42 u32 version;
43 struct device *dev;
46 static DEFINE_MUTEX(goldfish_tty_lock);
47 static struct tty_driver *goldfish_tty_driver;
48 static u32 goldfish_tty_line_count = 8;
49 static u32 goldfish_tty_current_line_count;
50 static struct goldfish_tty *goldfish_ttys;
52 static void do_rw_io(struct goldfish_tty *qtty,
53 unsigned long address,
54 unsigned int count,
55 int is_write)
57 unsigned long irq_flags;
58 void __iomem *base = qtty->base;
60 spin_lock_irqsave(&qtty->lock, irq_flags);
61 gf_write_ptr((void *)address, base + GOLDFISH_TTY_REG_DATA_PTR,
62 base + GOLDFISH_TTY_REG_DATA_PTR_HIGH);
63 writel(count, base + GOLDFISH_TTY_REG_DATA_LEN);
65 if (is_write)
66 writel(GOLDFISH_TTY_CMD_WRITE_BUFFER,
67 base + GOLDFISH_TTY_REG_CMD);
68 else
69 writel(GOLDFISH_TTY_CMD_READ_BUFFER,
70 base + GOLDFISH_TTY_REG_CMD);
72 spin_unlock_irqrestore(&qtty->lock, irq_flags);
75 static void goldfish_tty_rw(struct goldfish_tty *qtty,
76 unsigned long addr,
77 unsigned int count,
78 int is_write)
80 dma_addr_t dma_handle;
81 enum dma_data_direction dma_dir;
83 dma_dir = (is_write ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
84 if (qtty->version > 0) {
86 * Goldfish TTY for Ranchu platform uses
87 * physical addresses and DMA for read/write operations
89 unsigned long addr_end = addr + count;
91 while (addr < addr_end) {
92 unsigned long pg_end = (addr & PAGE_MASK) + PAGE_SIZE;
93 unsigned long next =
94 pg_end < addr_end ? pg_end : addr_end;
95 unsigned long avail = next - addr;
98 * Map the buffer's virtual address to the DMA address
99 * so the buffer can be accessed by the device.
101 dma_handle = dma_map_single(qtty->dev, (void *)addr,
102 avail, dma_dir);
104 if (dma_mapping_error(qtty->dev, dma_handle)) {
105 dev_err(qtty->dev, "tty: DMA mapping error.\n");
106 return;
108 do_rw_io(qtty, dma_handle, avail, is_write);
111 * Unmap the previously mapped region after
112 * the completion of the read/write operation.
114 dma_unmap_single(qtty->dev, dma_handle, avail, dma_dir);
116 addr += avail;
118 } else {
120 * Old style Goldfish TTY used on the Goldfish platform
121 * uses virtual addresses.
123 do_rw_io(qtty, addr, count, is_write);
127 static void goldfish_tty_do_write(int line, const char *buf,
128 unsigned int count)
130 struct goldfish_tty *qtty = &goldfish_ttys[line];
131 unsigned long address = (unsigned long)(void *)buf;
133 goldfish_tty_rw(qtty, address, count, 1);
136 static irqreturn_t goldfish_tty_interrupt(int irq, void *dev_id)
138 struct goldfish_tty *qtty = dev_id;
139 void __iomem *base = qtty->base;
140 unsigned long address;
141 unsigned char *buf;
142 u32 count;
144 count = readl(base + GOLDFISH_TTY_REG_BYTES_READY);
145 if (count == 0)
146 return IRQ_NONE;
148 count = tty_prepare_flip_string(&qtty->port, &buf, count);
150 address = (unsigned long)(void *)buf;
151 goldfish_tty_rw(qtty, address, count, 0);
153 tty_schedule_flip(&qtty->port);
154 return IRQ_HANDLED;
157 static int goldfish_tty_activate(struct tty_port *port, struct tty_struct *tty)
159 struct goldfish_tty *qtty = container_of(port, struct goldfish_tty,
160 port);
161 writel(GOLDFISH_TTY_CMD_INT_ENABLE, qtty->base + GOLDFISH_TTY_REG_CMD);
162 return 0;
165 static void goldfish_tty_shutdown(struct tty_port *port)
167 struct goldfish_tty *qtty = container_of(port, struct goldfish_tty,
168 port);
169 writel(GOLDFISH_TTY_CMD_INT_DISABLE, qtty->base + GOLDFISH_TTY_REG_CMD);
172 static int goldfish_tty_open(struct tty_struct *tty, struct file *filp)
174 struct goldfish_tty *qtty = &goldfish_ttys[tty->index];
175 return tty_port_open(&qtty->port, tty, filp);
178 static void goldfish_tty_close(struct tty_struct *tty, struct file *filp)
180 tty_port_close(tty->port, tty, filp);
183 static void goldfish_tty_hangup(struct tty_struct *tty)
185 tty_port_hangup(tty->port);
188 static int goldfish_tty_write(struct tty_struct *tty, const unsigned char *buf,
189 int count)
191 goldfish_tty_do_write(tty->index, buf, count);
192 return count;
195 static int goldfish_tty_write_room(struct tty_struct *tty)
197 return 0x10000;
200 static int goldfish_tty_chars_in_buffer(struct tty_struct *tty)
202 struct goldfish_tty *qtty = &goldfish_ttys[tty->index];
203 void __iomem *base = qtty->base;
204 return readl(base + GOLDFISH_TTY_REG_BYTES_READY);
207 static void goldfish_tty_console_write(struct console *co, const char *b,
208 unsigned count)
210 goldfish_tty_do_write(co->index, b, count);
213 static struct tty_driver *goldfish_tty_console_device(struct console *c,
214 int *index)
216 *index = c->index;
217 return goldfish_tty_driver;
220 static int goldfish_tty_console_setup(struct console *co, char *options)
222 if ((unsigned)co->index >= goldfish_tty_line_count)
223 return -ENODEV;
224 if (!goldfish_ttys[co->index].base)
225 return -ENODEV;
226 return 0;
229 static const struct tty_port_operations goldfish_port_ops = {
230 .activate = goldfish_tty_activate,
231 .shutdown = goldfish_tty_shutdown
234 static const struct tty_operations goldfish_tty_ops = {
235 .open = goldfish_tty_open,
236 .close = goldfish_tty_close,
237 .hangup = goldfish_tty_hangup,
238 .write = goldfish_tty_write,
239 .write_room = goldfish_tty_write_room,
240 .chars_in_buffer = goldfish_tty_chars_in_buffer,
243 static int goldfish_tty_create_driver(void)
245 int ret;
246 struct tty_driver *tty;
248 goldfish_ttys = kcalloc(goldfish_tty_line_count,
249 sizeof(*goldfish_ttys),
250 GFP_KERNEL);
251 if (goldfish_ttys == NULL) {
252 ret = -ENOMEM;
253 goto err_alloc_goldfish_ttys_failed;
255 tty = alloc_tty_driver(goldfish_tty_line_count);
256 if (tty == NULL) {
257 ret = -ENOMEM;
258 goto err_alloc_tty_driver_failed;
260 tty->driver_name = "goldfish";
261 tty->name = "ttyGF";
262 tty->type = TTY_DRIVER_TYPE_SERIAL;
263 tty->subtype = SERIAL_TYPE_NORMAL;
264 tty->init_termios = tty_std_termios;
265 tty->flags = TTY_DRIVER_RESET_TERMIOS | TTY_DRIVER_REAL_RAW |
266 TTY_DRIVER_DYNAMIC_DEV;
267 tty_set_operations(tty, &goldfish_tty_ops);
268 ret = tty_register_driver(tty);
269 if (ret)
270 goto err_tty_register_driver_failed;
272 goldfish_tty_driver = tty;
273 return 0;
275 err_tty_register_driver_failed:
276 put_tty_driver(tty);
277 err_alloc_tty_driver_failed:
278 kfree(goldfish_ttys);
279 goldfish_ttys = NULL;
280 err_alloc_goldfish_ttys_failed:
281 return ret;
284 static void goldfish_tty_delete_driver(void)
286 tty_unregister_driver(goldfish_tty_driver);
287 put_tty_driver(goldfish_tty_driver);
288 goldfish_tty_driver = NULL;
289 kfree(goldfish_ttys);
290 goldfish_ttys = NULL;
293 static int goldfish_tty_probe(struct platform_device *pdev)
295 struct goldfish_tty *qtty;
296 int ret = -ENODEV;
297 struct resource *r;
298 struct device *ttydev;
299 void __iomem *base;
300 u32 irq;
301 unsigned int line;
303 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
304 if (!r) {
305 pr_err("goldfish_tty: No MEM resource available!\n");
306 return -ENOMEM;
309 base = ioremap(r->start, 0x1000);
310 if (!base) {
311 pr_err("goldfish_tty: Unable to ioremap base!\n");
312 return -ENOMEM;
315 r = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
316 if (!r) {
317 pr_err("goldfish_tty: No IRQ resource available!\n");
318 goto err_unmap;
321 irq = r->start;
323 mutex_lock(&goldfish_tty_lock);
325 if (pdev->id == PLATFORM_DEVID_NONE)
326 line = goldfish_tty_current_line_count;
327 else
328 line = pdev->id;
330 if (line >= goldfish_tty_line_count) {
331 pr_err("goldfish_tty: Reached maximum tty number of %d.\n",
332 goldfish_tty_current_line_count);
333 ret = -ENOMEM;
334 goto err_unlock;
337 if (goldfish_tty_current_line_count == 0) {
338 ret = goldfish_tty_create_driver();
339 if (ret)
340 goto err_unlock;
342 goldfish_tty_current_line_count++;
344 qtty = &goldfish_ttys[line];
345 spin_lock_init(&qtty->lock);
346 tty_port_init(&qtty->port);
347 qtty->port.ops = &goldfish_port_ops;
348 qtty->base = base;
349 qtty->irq = irq;
350 qtty->dev = &pdev->dev;
353 * Goldfish TTY device used by the Goldfish emulator
354 * should identify itself with 0, forcing the driver
355 * to use virtual addresses. Goldfish TTY device
356 * on Ranchu emulator (qemu2) returns 1 here and
357 * driver will use physical addresses.
359 qtty->version = readl(base + GOLDFISH_TTY_REG_VERSION);
362 * Goldfish TTY device on Ranchu emulator (qemu2)
363 * will use DMA for read/write IO operations.
365 if (qtty->version > 0) {
367 * Initialize dma_mask to 32-bits.
369 if (!pdev->dev.dma_mask)
370 pdev->dev.dma_mask = &pdev->dev.coherent_dma_mask;
371 ret = dma_set_mask(&pdev->dev, DMA_BIT_MASK(32));
372 if (ret) {
373 dev_err(&pdev->dev, "No suitable DMA available.\n");
374 goto err_dec_line_count;
378 writel(GOLDFISH_TTY_CMD_INT_DISABLE, base + GOLDFISH_TTY_REG_CMD);
380 ret = request_irq(irq, goldfish_tty_interrupt, IRQF_SHARED,
381 "goldfish_tty", qtty);
382 if (ret) {
383 pr_err("goldfish_tty: No IRQ available!\n");
384 goto err_dec_line_count;
387 ttydev = tty_port_register_device(&qtty->port, goldfish_tty_driver,
388 line, &pdev->dev);
389 if (IS_ERR(ttydev)) {
390 ret = PTR_ERR(ttydev);
391 goto err_tty_register_device_failed;
394 strcpy(qtty->console.name, "ttyGF");
395 qtty->console.write = goldfish_tty_console_write;
396 qtty->console.device = goldfish_tty_console_device;
397 qtty->console.setup = goldfish_tty_console_setup;
398 qtty->console.flags = CON_PRINTBUFFER;
399 qtty->console.index = line;
400 register_console(&qtty->console);
401 platform_set_drvdata(pdev, qtty);
403 mutex_unlock(&goldfish_tty_lock);
404 return 0;
406 err_tty_register_device_failed:
407 free_irq(irq, qtty);
408 err_dec_line_count:
409 goldfish_tty_current_line_count--;
410 if (goldfish_tty_current_line_count == 0)
411 goldfish_tty_delete_driver();
412 err_unlock:
413 mutex_unlock(&goldfish_tty_lock);
414 err_unmap:
415 iounmap(base);
416 return ret;
419 static int goldfish_tty_remove(struct platform_device *pdev)
421 struct goldfish_tty *qtty = platform_get_drvdata(pdev);
423 mutex_lock(&goldfish_tty_lock);
425 unregister_console(&qtty->console);
426 tty_unregister_device(goldfish_tty_driver, qtty->console.index);
427 iounmap(qtty->base);
428 qtty->base = NULL;
429 free_irq(qtty->irq, pdev);
430 goldfish_tty_current_line_count--;
431 if (goldfish_tty_current_line_count == 0)
432 goldfish_tty_delete_driver();
433 mutex_unlock(&goldfish_tty_lock);
434 return 0;
437 #ifdef CONFIG_GOLDFISH_TTY_EARLY_CONSOLE
438 static void gf_early_console_putchar(struct uart_port *port, int ch)
440 __raw_writel(ch, port->membase);
443 static void gf_early_write(struct console *con, const char *s, unsigned int n)
445 struct earlycon_device *dev = con->data;
447 uart_console_write(&dev->port, s, n, gf_early_console_putchar);
450 static int __init gf_earlycon_setup(struct earlycon_device *device,
451 const char *opt)
453 if (!device->port.membase)
454 return -ENODEV;
456 device->con->write = gf_early_write;
457 return 0;
460 OF_EARLYCON_DECLARE(early_gf_tty, "google,goldfish-tty", gf_earlycon_setup);
461 #endif
463 static const struct of_device_id goldfish_tty_of_match[] = {
464 { .compatible = "google,goldfish-tty", },
468 MODULE_DEVICE_TABLE(of, goldfish_tty_of_match);
470 static struct platform_driver goldfish_tty_platform_driver = {
471 .probe = goldfish_tty_probe,
472 .remove = goldfish_tty_remove,
473 .driver = {
474 .name = "goldfish_tty",
475 .of_match_table = goldfish_tty_of_match,
479 module_platform_driver(goldfish_tty_platform_driver);
481 MODULE_LICENSE("GPL v2");