tty: serial: fsl_lpuart: clear LPUART Status Register in lpuart32_shutdown()
[linux-2.6-block.git] / drivers / tty / goldfish.c
CommitLineData
e3b3d0f5 1// SPDX-License-Identifier: GPL-2.0
666b7793
AH
2/*
3 * Copyright (C) 2007 Google, Inc.
4 * Copyright (C) 2012 Intel, Inc.
3840ed95 5 * Copyright (C) 2017 Imagination Technologies Ltd.
666b7793
AH
6 */
7
8#include <linux/console.h>
666b7793
AH
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>
ac316725 16#include <linux/mod_devicetable.h>
e0f682e0 17#include <linux/goldfish.h>
7157d2be
MD
18#include <linux/mm.h>
19#include <linux/dma-mapping.h>
3840ed95 20#include <linux/serial_core.h>
666b7793 21
2296eee7
AM
22/* Goldfish tty register's offsets */
23#define GOLDFISH_TTY_REG_BYTES_READY 0x04
24#define GOLDFISH_TTY_REG_CMD 0x08
25#define GOLDFISH_TTY_REG_DATA_PTR 0x10
26#define GOLDFISH_TTY_REG_DATA_LEN 0x14
27#define GOLDFISH_TTY_REG_DATA_PTR_HIGH 0x18
7157d2be 28#define GOLDFISH_TTY_REG_VERSION 0x20
2296eee7
AM
29
30/* Goldfish tty commands */
31#define GOLDFISH_TTY_CMD_INT_DISABLE 0
32#define GOLDFISH_TTY_CMD_INT_ENABLE 1
33#define GOLDFISH_TTY_CMD_WRITE_BUFFER 2
34#define GOLDFISH_TTY_CMD_READ_BUFFER 3
666b7793
AH
35
36struct goldfish_tty {
37 struct tty_port port;
38 spinlock_t lock;
39 void __iomem *base;
40 u32 irq;
41 int opencount;
42 struct console console;
7157d2be
MD
43 u32 version;
44 struct device *dev;
666b7793
AH
45};
46
47static DEFINE_MUTEX(goldfish_tty_lock);
48static struct tty_driver *goldfish_tty_driver;
49static u32 goldfish_tty_line_count = 8;
50static u32 goldfish_tty_current_line_count;
51static struct goldfish_tty *goldfish_ttys;
52
7157d2be
MD
53static void do_rw_io(struct goldfish_tty *qtty,
54 unsigned long address,
55 unsigned int count,
56 int is_write)
666b7793
AH
57{
58 unsigned long irq_flags;
666b7793 59 void __iomem *base = qtty->base;
7157d2be 60
666b7793 61 spin_lock_irqsave(&qtty->lock, irq_flags);
7157d2be 62 gf_write_ptr((void *)address, base + GOLDFISH_TTY_REG_DATA_PTR,
2296eee7 63 base + GOLDFISH_TTY_REG_DATA_PTR_HIGH);
2e2ac4a3 64 gf_iowrite32(count, base + GOLDFISH_TTY_REG_DATA_LEN);
7157d2be
MD
65
66 if (is_write)
2e2ac4a3 67 gf_iowrite32(GOLDFISH_TTY_CMD_WRITE_BUFFER,
7157d2be
MD
68 base + GOLDFISH_TTY_REG_CMD);
69 else
2e2ac4a3 70 gf_iowrite32(GOLDFISH_TTY_CMD_READ_BUFFER,
7157d2be
MD
71 base + GOLDFISH_TTY_REG_CMD);
72
666b7793
AH
73 spin_unlock_irqrestore(&qtty->lock, irq_flags);
74}
75
7157d2be
MD
76static void goldfish_tty_rw(struct goldfish_tty *qtty,
77 unsigned long addr,
78 unsigned int count,
79 int is_write)
80{
81 dma_addr_t dma_handle;
82 enum dma_data_direction dma_dir;
83
84 dma_dir = (is_write ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
85 if (qtty->version > 0) {
86 /*
87 * Goldfish TTY for Ranchu platform uses
88 * physical addresses and DMA for read/write operations
89 */
90 unsigned long addr_end = addr + count;
91
92 while (addr < addr_end) {
93 unsigned long pg_end = (addr & PAGE_MASK) + PAGE_SIZE;
94 unsigned long next =
95 pg_end < addr_end ? pg_end : addr_end;
96 unsigned long avail = next - addr;
97
98 /*
99 * Map the buffer's virtual address to the DMA address
100 * so the buffer can be accessed by the device.
101 */
102 dma_handle = dma_map_single(qtty->dev, (void *)addr,
103 avail, dma_dir);
104
105 if (dma_mapping_error(qtty->dev, dma_handle)) {
106 dev_err(qtty->dev, "tty: DMA mapping error.\n");
107 return;
108 }
109 do_rw_io(qtty, dma_handle, avail, is_write);
110
111 /*
112 * Unmap the previously mapped region after
113 * the completion of the read/write operation.
114 */
115 dma_unmap_single(qtty->dev, dma_handle, avail, dma_dir);
116
117 addr += avail;
118 }
119 } else {
120 /*
121 * Old style Goldfish TTY used on the Goldfish platform
122 * uses virtual addresses.
123 */
124 do_rw_io(qtty, addr, count, is_write);
125 }
126}
127
128static void goldfish_tty_do_write(int line, const char *buf,
129 unsigned int count)
130{
131 struct goldfish_tty *qtty = &goldfish_ttys[line];
132 unsigned long address = (unsigned long)(void *)buf;
133
134 goldfish_tty_rw(qtty, address, count, 1);
135}
136
666b7793
AH
137static irqreturn_t goldfish_tty_interrupt(int irq, void *dev_id)
138{
465893e1 139 struct goldfish_tty *qtty = dev_id;
666b7793 140 void __iomem *base = qtty->base;
7157d2be 141 unsigned long address;
666b7793
AH
142 unsigned char *buf;
143 u32 count;
666b7793 144
2e2ac4a3 145 count = gf_ioread32(base + GOLDFISH_TTY_REG_BYTES_READY);
d78055dc 146 if (count == 0)
666b7793
AH
147 return IRQ_NONE;
148
ebcf0981 149 count = tty_prepare_flip_string(&qtty->port, &buf, count);
7157d2be
MD
150
151 address = (unsigned long)(void *)buf;
152 goldfish_tty_rw(qtty, address, count, 0);
153
5f6a8515 154 tty_flip_buffer_push(&qtty->port);
666b7793
AH
155 return IRQ_HANDLED;
156}
157
158static int goldfish_tty_activate(struct tty_port *port, struct tty_struct *tty)
159{
d78055dc
A
160 struct goldfish_tty *qtty = container_of(port, struct goldfish_tty,
161 port);
2e2ac4a3 162 gf_iowrite32(GOLDFISH_TTY_CMD_INT_ENABLE, qtty->base + GOLDFISH_TTY_REG_CMD);
666b7793
AH
163 return 0;
164}
165
166static void goldfish_tty_shutdown(struct tty_port *port)
167{
d78055dc
A
168 struct goldfish_tty *qtty = container_of(port, struct goldfish_tty,
169 port);
2e2ac4a3 170 gf_iowrite32(GOLDFISH_TTY_CMD_INT_DISABLE, qtty->base + GOLDFISH_TTY_REG_CMD);
666b7793
AH
171}
172
d78055dc 173static int goldfish_tty_open(struct tty_struct *tty, struct file *filp)
666b7793
AH
174{
175 struct goldfish_tty *qtty = &goldfish_ttys[tty->index];
176 return tty_port_open(&qtty->port, tty, filp);
177}
178
d78055dc 179static void goldfish_tty_close(struct tty_struct *tty, struct file *filp)
666b7793
AH
180{
181 tty_port_close(tty->port, tty, filp);
182}
183
184static void goldfish_tty_hangup(struct tty_struct *tty)
185{
186 tty_port_hangup(tty->port);
187}
188
d78055dc
A
189static int goldfish_tty_write(struct tty_struct *tty, const unsigned char *buf,
190 int count)
666b7793
AH
191{
192 goldfish_tty_do_write(tty->index, buf, count);
193 return count;
194}
195
03b3b1a2 196static unsigned int goldfish_tty_write_room(struct tty_struct *tty)
666b7793
AH
197{
198 return 0x10000;
199}
200
fff4ef17 201static unsigned int goldfish_tty_chars_in_buffer(struct tty_struct *tty)
666b7793
AH
202{
203 struct goldfish_tty *qtty = &goldfish_ttys[tty->index];
204 void __iomem *base = qtty->base;
2e2ac4a3 205 return gf_ioread32(base + GOLDFISH_TTY_REG_BYTES_READY);
666b7793
AH
206}
207
d78055dc
A
208static void goldfish_tty_console_write(struct console *co, const char *b,
209 unsigned count)
666b7793
AH
210{
211 goldfish_tty_do_write(co->index, b, count);
212}
213
d78055dc
A
214static struct tty_driver *goldfish_tty_console_device(struct console *c,
215 int *index)
666b7793
AH
216{
217 *index = c->index;
218 return goldfish_tty_driver;
219}
220
221static int goldfish_tty_console_setup(struct console *co, char *options)
222{
fda2b418 223 if ((unsigned)co->index >= goldfish_tty_line_count)
666b7793 224 return -ENODEV;
a4dc9236 225 if (!goldfish_ttys[co->index].base)
666b7793
AH
226 return -ENODEV;
227 return 0;
228}
229
04b757df 230static const struct tty_port_operations goldfish_port_ops = {
666b7793
AH
231 .activate = goldfish_tty_activate,
232 .shutdown = goldfish_tty_shutdown
233};
234
d78055dc 235static const struct tty_operations goldfish_tty_ops = {
666b7793
AH
236 .open = goldfish_tty_open,
237 .close = goldfish_tty_close,
238 .hangup = goldfish_tty_hangup,
239 .write = goldfish_tty_write,
240 .write_room = goldfish_tty_write_room,
241 .chars_in_buffer = goldfish_tty_chars_in_buffer,
242};
243
244static int goldfish_tty_create_driver(void)
245{
246 int ret;
247 struct tty_driver *tty;
248
6396bb22
KC
249 goldfish_ttys = kcalloc(goldfish_tty_line_count,
250 sizeof(*goldfish_ttys),
251 GFP_KERNEL);
d78055dc 252 if (goldfish_ttys == NULL) {
666b7793
AH
253 ret = -ENOMEM;
254 goto err_alloc_goldfish_ttys_failed;
255 }
39b7b42b
JS
256 tty = tty_alloc_driver(goldfish_tty_line_count,
257 TTY_DRIVER_RESET_TERMIOS | TTY_DRIVER_REAL_RAW |
258 TTY_DRIVER_DYNAMIC_DEV);
259 if (IS_ERR(tty)) {
260 ret = PTR_ERR(tty);
261 goto err_tty_alloc_driver_failed;
666b7793
AH
262 }
263 tty->driver_name = "goldfish";
264 tty->name = "ttyGF";
265 tty->type = TTY_DRIVER_TYPE_SERIAL;
266 tty->subtype = SERIAL_TYPE_NORMAL;
267 tty->init_termios = tty_std_termios;
666b7793
AH
268 tty_set_operations(tty, &goldfish_tty_ops);
269 ret = tty_register_driver(tty);
d78055dc 270 if (ret)
666b7793
AH
271 goto err_tty_register_driver_failed;
272
273 goldfish_tty_driver = tty;
274 return 0;
275
276err_tty_register_driver_failed:
9f90a4dd 277 tty_driver_kref_put(tty);
39b7b42b 278err_tty_alloc_driver_failed:
666b7793
AH
279 kfree(goldfish_ttys);
280 goldfish_ttys = NULL;
281err_alloc_goldfish_ttys_failed:
282 return ret;
283}
284
285static void goldfish_tty_delete_driver(void)
286{
287 tty_unregister_driver(goldfish_tty_driver);
9f90a4dd 288 tty_driver_kref_put(goldfish_tty_driver);
666b7793
AH
289 goldfish_tty_driver = NULL;
290 kfree(goldfish_ttys);
291 goldfish_ttys = NULL;
292}
293
294static int goldfish_tty_probe(struct platform_device *pdev)
295{
296 struct goldfish_tty *qtty;
7157d2be 297 int ret = -ENODEV;
666b7793
AH
298 struct resource *r;
299 struct device *ttydev;
300 void __iomem *base;
5acb78dc 301 int irq;
465893e1 302 unsigned int line;
666b7793
AH
303
304 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
7157d2be
MD
305 if (!r) {
306 pr_err("goldfish_tty: No MEM resource available!\n");
307 return -ENOMEM;
308 }
666b7793
AH
309
310 base = ioremap(r->start, 0x1000);
7157d2be
MD
311 if (!base) {
312 pr_err("goldfish_tty: Unable to ioremap base!\n");
313 return -ENOMEM;
314 }
666b7793 315
5acb78dc
LP
316 irq = platform_get_irq(pdev, 0);
317 if (irq < 0) {
318 ret = irq;
666b7793 319 goto err_unmap;
7157d2be 320 }
666b7793 321
666b7793 322 mutex_lock(&goldfish_tty_lock);
465893e1
GH
323
324 if (pdev->id == PLATFORM_DEVID_NONE)
325 line = goldfish_tty_current_line_count;
326 else
327 line = pdev->id;
328
7157d2be
MD
329 if (line >= goldfish_tty_line_count) {
330 pr_err("goldfish_tty: Reached maximum tty number of %d.\n",
331 goldfish_tty_current_line_count);
332 ret = -ENOMEM;
333 goto err_unlock;
334 }
465893e1 335
d78055dc 336 if (goldfish_tty_current_line_count == 0) {
666b7793 337 ret = goldfish_tty_create_driver();
d78055dc 338 if (ret)
7157d2be 339 goto err_unlock;
666b7793
AH
340 }
341 goldfish_tty_current_line_count++;
342
465893e1 343 qtty = &goldfish_ttys[line];
666b7793
AH
344 spin_lock_init(&qtty->lock);
345 tty_port_init(&qtty->port);
346 qtty->port.ops = &goldfish_port_ops;
347 qtty->base = base;
348 qtty->irq = irq;
7157d2be
MD
349 qtty->dev = &pdev->dev;
350
351 /*
352 * Goldfish TTY device used by the Goldfish emulator
353 * should identify itself with 0, forcing the driver
354 * to use virtual addresses. Goldfish TTY device
355 * on Ranchu emulator (qemu2) returns 1 here and
356 * driver will use physical addresses.
357 */
2e2ac4a3 358 qtty->version = gf_ioread32(base + GOLDFISH_TTY_REG_VERSION);
7157d2be
MD
359
360 /*
361 * Goldfish TTY device on Ranchu emulator (qemu2)
362 * will use DMA for read/write IO operations.
363 */
364 if (qtty->version > 0) {
365 /*
366 * Initialize dma_mask to 32-bits.
367 */
368 if (!pdev->dev.dma_mask)
369 pdev->dev.dma_mask = &pdev->dev.coherent_dma_mask;
370 ret = dma_set_mask(&pdev->dev, DMA_BIT_MASK(32));
371 if (ret) {
372 dev_err(&pdev->dev, "No suitable DMA available.\n");
373 goto err_dec_line_count;
374 }
375 }
666b7793 376
2e2ac4a3 377 gf_iowrite32(GOLDFISH_TTY_CMD_INT_DISABLE, base + GOLDFISH_TTY_REG_CMD);
666b7793 378
d78055dc 379 ret = request_irq(irq, goldfish_tty_interrupt, IRQF_SHARED,
7157d2be
MD
380 "goldfish_tty", qtty);
381 if (ret) {
382 pr_err("goldfish_tty: No IRQ available!\n");
383 goto err_dec_line_count;
384 }
666b7793
AH
385
386 ttydev = tty_port_register_device(&qtty->port, goldfish_tty_driver,
7157d2be 387 line, &pdev->dev);
d78055dc 388 if (IS_ERR(ttydev)) {
666b7793
AH
389 ret = PTR_ERR(ttydev);
390 goto err_tty_register_device_failed;
391 }
392
393 strcpy(qtty->console.name, "ttyGF");
394 qtty->console.write = goldfish_tty_console_write;
395 qtty->console.device = goldfish_tty_console_device;
396 qtty->console.setup = goldfish_tty_console_setup;
397 qtty->console.flags = CON_PRINTBUFFER;
465893e1 398 qtty->console.index = line;
666b7793 399 register_console(&qtty->console);
465893e1 400 platform_set_drvdata(pdev, qtty);
666b7793
AH
401
402 mutex_unlock(&goldfish_tty_lock);
403 return 0;
404
666b7793 405err_tty_register_device_failed:
1a5c2d1d 406 free_irq(irq, qtty);
7157d2be 407err_dec_line_count:
507b0506 408 tty_port_destroy(&qtty->port);
666b7793 409 goldfish_tty_current_line_count--;
d78055dc 410 if (goldfish_tty_current_line_count == 0)
666b7793 411 goldfish_tty_delete_driver();
7157d2be 412err_unlock:
666b7793
AH
413 mutex_unlock(&goldfish_tty_lock);
414err_unmap:
415 iounmap(base);
416 return ret;
417}
418
419static int goldfish_tty_remove(struct platform_device *pdev)
420{
465893e1 421 struct goldfish_tty *qtty = platform_get_drvdata(pdev);
666b7793
AH
422
423 mutex_lock(&goldfish_tty_lock);
424
666b7793 425 unregister_console(&qtty->console);
465893e1 426 tty_unregister_device(goldfish_tty_driver, qtty->console.index);
666b7793 427 iounmap(qtty->base);
a4dc9236 428 qtty->base = NULL;
499e13aa 429 free_irq(qtty->irq, qtty);
507b0506 430 tty_port_destroy(&qtty->port);
666b7793 431 goldfish_tty_current_line_count--;
d78055dc 432 if (goldfish_tty_current_line_count == 0)
666b7793
AH
433 goldfish_tty_delete_driver();
434 mutex_unlock(&goldfish_tty_lock);
435 return 0;
436}
437
6a28fd2b 438#ifdef CONFIG_GOLDFISH_TTY_EARLY_CONSOLE
3f8bab17 439static void gf_early_console_putchar(struct uart_port *port, unsigned char ch)
3840ed95 440{
2e2ac4a3 441 gf_iowrite32(ch, port->membase);
3840ed95
MD
442}
443
444static void gf_early_write(struct console *con, const char *s, unsigned int n)
445{
446 struct earlycon_device *dev = con->data;
447
448 uart_console_write(&dev->port, s, n, gf_early_console_putchar);
449}
450
451static int __init gf_earlycon_setup(struct earlycon_device *device,
452 const char *opt)
453{
454 if (!device->port.membase)
455 return -ENODEV;
456
457 device->con->write = gf_early_write;
458 return 0;
459}
460
461OF_EARLYCON_DECLARE(early_gf_tty, "google,goldfish-tty", gf_earlycon_setup);
6a28fd2b 462#endif
3840ed95 463
9b883eea
MD
464static const struct of_device_id goldfish_tty_of_match[] = {
465 { .compatible = "google,goldfish-tty", },
466 {},
467};
468
469MODULE_DEVICE_TABLE(of, goldfish_tty_of_match);
470
666b7793
AH
471static struct platform_driver goldfish_tty_platform_driver = {
472 .probe = goldfish_tty_probe,
473 .remove = goldfish_tty_remove,
474 .driver = {
9b883eea
MD
475 .name = "goldfish_tty",
476 .of_match_table = goldfish_tty_of_match,
666b7793
AH
477 }
478};
479
480module_platform_driver(goldfish_tty_platform_driver);
481
482MODULE_LICENSE("GPL v2");