GNU Linux-libre 4.14.290-gnu1
[releases.git] / drivers / tty / goldfish.c
1 /*
2  * Copyright (C) 2007 Google, Inc.
3  * Copyright (C) 2012 Intel, Inc.
4  * Copyright (C) 2017 Imagination Technologies Ltd.
5  *
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.
9  *
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.
14  *
15  */
16
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>
23 #include <linux/io.h>
24 #include <linux/module.h>
25 #include <linux/goldfish.h>
26 #include <linux/mm.h>
27 #include <linux/dma-mapping.h>
28 #include <linux/serial_core.h>
29
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
37
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
43
44 struct goldfish_tty {
45         struct tty_port port;
46         spinlock_t lock;
47         void __iomem *base;
48         u32 irq;
49         int opencount;
50         struct console console;
51         u32 version;
52         struct device *dev;
53 };
54
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;
60
61 static void do_rw_io(struct goldfish_tty *qtty,
62                      unsigned long address,
63                      unsigned int count,
64                      int is_write)
65 {
66         unsigned long irq_flags;
67         void __iomem *base = qtty->base;
68
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);
73
74         if (is_write)
75                 writel(GOLDFISH_TTY_CMD_WRITE_BUFFER,
76                        base + GOLDFISH_TTY_REG_CMD);
77         else
78                 writel(GOLDFISH_TTY_CMD_READ_BUFFER,
79                        base + GOLDFISH_TTY_REG_CMD);
80
81         spin_unlock_irqrestore(&qtty->lock, irq_flags);
82 }
83
84 static void goldfish_tty_rw(struct goldfish_tty *qtty,
85                             unsigned long addr,
86                             unsigned int count,
87                             int is_write)
88 {
89         dma_addr_t dma_handle;
90         enum dma_data_direction dma_dir;
91
92         dma_dir = (is_write ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
93         if (qtty->version > 0) {
94                 /*
95                  * Goldfish TTY for Ranchu platform uses
96                  * physical addresses and DMA for read/write operations
97                  */
98                 unsigned long addr_end = addr + count;
99
100                 while (addr < addr_end) {
101                         unsigned long pg_end = (addr & PAGE_MASK) + PAGE_SIZE;
102                         unsigned long next =
103                                         pg_end < addr_end ? pg_end : addr_end;
104                         unsigned long avail = next - addr;
105
106                         /*
107                          * Map the buffer's virtual address to the DMA address
108                          * so the buffer can be accessed by the device.
109                          */
110                         dma_handle = dma_map_single(qtty->dev, (void *)addr,
111                                                     avail, dma_dir);
112
113                         if (dma_mapping_error(qtty->dev, dma_handle)) {
114                                 dev_err(qtty->dev, "tty: DMA mapping error.\n");
115                                 return;
116                         }
117                         do_rw_io(qtty, dma_handle, avail, is_write);
118
119                         /*
120                          * Unmap the previously mapped region after
121                          * the completion of the read/write operation.
122                          */
123                         dma_unmap_single(qtty->dev, dma_handle, avail, dma_dir);
124
125                         addr += avail;
126                 }
127         } else {
128                 /*
129                  * Old style Goldfish TTY used on the Goldfish platform
130                  * uses virtual addresses.
131                  */
132                 do_rw_io(qtty, addr, count, is_write);
133         }
134 }
135
136 static void goldfish_tty_do_write(int line, const char *buf,
137                                   unsigned int count)
138 {
139         struct goldfish_tty *qtty = &goldfish_ttys[line];
140         unsigned long address = (unsigned long)(void *)buf;
141
142         goldfish_tty_rw(qtty, address, count, 1);
143 }
144
145 static irqreturn_t goldfish_tty_interrupt(int irq, void *dev_id)
146 {
147         struct goldfish_tty *qtty = dev_id;
148         void __iomem *base = qtty->base;
149         unsigned long address;
150         unsigned char *buf;
151         u32 count;
152
153         count = readl(base + GOLDFISH_TTY_REG_BYTES_READY);
154         if (count == 0)
155                 return IRQ_NONE;
156
157         count = tty_prepare_flip_string(&qtty->port, &buf, count);
158
159         address = (unsigned long)(void *)buf;
160         goldfish_tty_rw(qtty, address, count, 0);
161
162         tty_flip_buffer_push(&qtty->port);
163         return IRQ_HANDLED;
164 }
165
166 static int goldfish_tty_activate(struct tty_port *port, struct tty_struct *tty)
167 {
168         struct goldfish_tty *qtty = container_of(port, struct goldfish_tty,
169                                                                         port);
170         writel(GOLDFISH_TTY_CMD_INT_ENABLE, qtty->base + GOLDFISH_TTY_REG_CMD);
171         return 0;
172 }
173
174 static void goldfish_tty_shutdown(struct tty_port *port)
175 {
176         struct goldfish_tty *qtty = container_of(port, struct goldfish_tty,
177                                                                         port);
178         writel(GOLDFISH_TTY_CMD_INT_DISABLE, qtty->base + GOLDFISH_TTY_REG_CMD);
179 }
180
181 static int goldfish_tty_open(struct tty_struct *tty, struct file *filp)
182 {
183         struct goldfish_tty *qtty = &goldfish_ttys[tty->index];
184         return tty_port_open(&qtty->port, tty, filp);
185 }
186
187 static void goldfish_tty_close(struct tty_struct *tty, struct file *filp)
188 {
189         tty_port_close(tty->port, tty, filp);
190 }
191
192 static void goldfish_tty_hangup(struct tty_struct *tty)
193 {
194         tty_port_hangup(tty->port);
195 }
196
197 static int goldfish_tty_write(struct tty_struct *tty, const unsigned char *buf,
198                                                                 int count)
199 {
200         goldfish_tty_do_write(tty->index, buf, count);
201         return count;
202 }
203
204 static int goldfish_tty_write_room(struct tty_struct *tty)
205 {
206         return 0x10000;
207 }
208
209 static int goldfish_tty_chars_in_buffer(struct tty_struct *tty)
210 {
211         struct goldfish_tty *qtty = &goldfish_ttys[tty->index];
212         void __iomem *base = qtty->base;
213         return readl(base + GOLDFISH_TTY_REG_BYTES_READY);
214 }
215
216 static void goldfish_tty_console_write(struct console *co, const char *b,
217                                                                 unsigned count)
218 {
219         goldfish_tty_do_write(co->index, b, count);
220 }
221
222 static struct tty_driver *goldfish_tty_console_device(struct console *c,
223                                                                 int *index)
224 {
225         *index = c->index;
226         return goldfish_tty_driver;
227 }
228
229 static int goldfish_tty_console_setup(struct console *co, char *options)
230 {
231         if ((unsigned)co->index >= goldfish_tty_line_count)
232                 return -ENODEV;
233         if (!goldfish_ttys[co->index].base)
234                 return -ENODEV;
235         return 0;
236 }
237
238 static const struct tty_port_operations goldfish_port_ops = {
239         .activate = goldfish_tty_activate,
240         .shutdown = goldfish_tty_shutdown
241 };
242
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,
250 };
251
252 static int goldfish_tty_create_driver(void)
253 {
254         int ret;
255         struct tty_driver *tty;
256
257         goldfish_ttys = kzalloc(sizeof(*goldfish_ttys) *
258                                 goldfish_tty_line_count, GFP_KERNEL);
259         if (goldfish_ttys == NULL) {
260                 ret = -ENOMEM;
261                 goto err_alloc_goldfish_ttys_failed;
262         }
263         tty = alloc_tty_driver(goldfish_tty_line_count);
264         if (tty == NULL) {
265                 ret = -ENOMEM;
266                 goto err_alloc_tty_driver_failed;
267         }
268         tty->driver_name = "goldfish";
269         tty->name = "ttyGF";
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);
277         if (ret)
278                 goto err_tty_register_driver_failed;
279
280         goldfish_tty_driver = tty;
281         return 0;
282
283 err_tty_register_driver_failed:
284         put_tty_driver(tty);
285 err_alloc_tty_driver_failed:
286         kfree(goldfish_ttys);
287         goldfish_ttys = NULL;
288 err_alloc_goldfish_ttys_failed:
289         return ret;
290 }
291
292 static void goldfish_tty_delete_driver(void)
293 {
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;
299 }
300
301 static int goldfish_tty_probe(struct platform_device *pdev)
302 {
303         struct goldfish_tty *qtty;
304         int ret = -ENODEV;
305         struct resource *r;
306         struct device *ttydev;
307         void __iomem *base;
308         u32 irq;
309         unsigned int line;
310
311         r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
312         if (!r) {
313                 pr_err("goldfish_tty: No MEM resource available!\n");
314                 return -ENOMEM;
315         }
316
317         base = ioremap(r->start, 0x1000);
318         if (!base) {
319                 pr_err("goldfish_tty: Unable to ioremap base!\n");
320                 return -ENOMEM;
321         }
322
323         r = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
324         if (!r) {
325                 pr_err("goldfish_tty: No IRQ resource available!\n");
326                 goto err_unmap;
327         }
328
329         irq = r->start;
330
331         mutex_lock(&goldfish_tty_lock);
332
333         if (pdev->id == PLATFORM_DEVID_NONE)
334                 line = goldfish_tty_current_line_count;
335         else
336                 line = pdev->id;
337
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);
341                 ret = -ENOMEM;
342                 goto err_unlock;
343         }
344
345         if (goldfish_tty_current_line_count == 0) {
346                 ret = goldfish_tty_create_driver();
347                 if (ret)
348                         goto err_unlock;
349         }
350         goldfish_tty_current_line_count++;
351
352         qtty = &goldfish_ttys[line];
353         spin_lock_init(&qtty->lock);
354         tty_port_init(&qtty->port);
355         qtty->port.ops = &goldfish_port_ops;
356         qtty->base = base;
357         qtty->irq = irq;
358         qtty->dev = &pdev->dev;
359
360         /*
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.
366          */
367         qtty->version = readl(base + GOLDFISH_TTY_REG_VERSION);
368
369         /*
370          * Goldfish TTY device on Ranchu emulator (qemu2)
371          * will use DMA for read/write IO operations.
372          */
373         if (qtty->version > 0) {
374                 /*
375                  * Initialize dma_mask to 32-bits.
376                  */
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));
380                 if (ret) {
381                         dev_err(&pdev->dev, "No suitable DMA available.\n");
382                         goto err_dec_line_count;
383                 }
384         }
385
386         writel(GOLDFISH_TTY_CMD_INT_DISABLE, base + GOLDFISH_TTY_REG_CMD);
387
388         ret = request_irq(irq, goldfish_tty_interrupt, IRQF_SHARED,
389                           "goldfish_tty", qtty);
390         if (ret) {
391                 pr_err("goldfish_tty: No IRQ available!\n");
392                 goto err_dec_line_count;
393         }
394
395         ttydev = tty_port_register_device(&qtty->port, goldfish_tty_driver,
396                                           line, &pdev->dev);
397         if (IS_ERR(ttydev)) {
398                 ret = PTR_ERR(ttydev);
399                 goto err_tty_register_device_failed;
400         }
401
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);
410
411         mutex_unlock(&goldfish_tty_lock);
412         return 0;
413
414 err_tty_register_device_failed:
415         free_irq(irq, qtty);
416 err_dec_line_count:
417         tty_port_destroy(&qtty->port);
418         goldfish_tty_current_line_count--;
419         if (goldfish_tty_current_line_count == 0)
420                 goldfish_tty_delete_driver();
421 err_unlock:
422         mutex_unlock(&goldfish_tty_lock);
423 err_unmap:
424         iounmap(base);
425         return ret;
426 }
427
428 static int goldfish_tty_remove(struct platform_device *pdev)
429 {
430         struct goldfish_tty *qtty = platform_get_drvdata(pdev);
431
432         mutex_lock(&goldfish_tty_lock);
433
434         unregister_console(&qtty->console);
435         tty_unregister_device(goldfish_tty_driver, qtty->console.index);
436         iounmap(qtty->base);
437         qtty->base = NULL;
438         free_irq(qtty->irq, qtty);
439         tty_port_destroy(&qtty->port);
440         goldfish_tty_current_line_count--;
441         if (goldfish_tty_current_line_count == 0)
442                 goldfish_tty_delete_driver();
443         mutex_unlock(&goldfish_tty_lock);
444         return 0;
445 }
446
447 #ifdef CONFIG_GOLDFISH_TTY_EARLY_CONSOLE
448 static void gf_early_console_putchar(struct uart_port *port, int ch)
449 {
450         __raw_writel(ch, port->membase);
451 }
452
453 static void gf_early_write(struct console *con, const char *s, unsigned int n)
454 {
455         struct earlycon_device *dev = con->data;
456
457         uart_console_write(&dev->port, s, n, gf_early_console_putchar);
458 }
459
460 static int __init gf_earlycon_setup(struct earlycon_device *device,
461                                     const char *opt)
462 {
463         if (!device->port.membase)
464                 return -ENODEV;
465
466         device->con->write = gf_early_write;
467         return 0;
468 }
469
470 OF_EARLYCON_DECLARE(early_gf_tty, "google,goldfish-tty", gf_earlycon_setup);
471 #endif
472
473 static const struct of_device_id goldfish_tty_of_match[] = {
474         { .compatible = "google,goldfish-tty", },
475         {},
476 };
477
478 MODULE_DEVICE_TABLE(of, goldfish_tty_of_match);
479
480 static struct platform_driver goldfish_tty_platform_driver = {
481         .probe = goldfish_tty_probe,
482         .remove = goldfish_tty_remove,
483         .driver = {
484                 .name = "goldfish_tty",
485                 .of_match_table = goldfish_tty_of_match,
486         }
487 };
488
489 module_platform_driver(goldfish_tty_platform_driver);
490
491 MODULE_LICENSE("GPL v2");