Merge tag 'cxl-for-6.7' of git://git.kernel.org/pub/scm/linux/kernel/git/cxl/cxl
[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
69851e4a 128static void goldfish_tty_do_write(int line, const u8 *buf, unsigned int count)
7157d2be
MD
129{
130 struct goldfish_tty *qtty = &goldfish_ttys[line];
131 unsigned long address = (unsigned long)(void *)buf;
132
133 goldfish_tty_rw(qtty, address, count, 1);
134}
135
666b7793
AH
136static irqreturn_t goldfish_tty_interrupt(int irq, void *dev_id)
137{
465893e1 138 struct goldfish_tty *qtty = dev_id;
666b7793 139 void __iomem *base = qtty->base;
7157d2be 140 unsigned long address;
666b7793
AH
141 unsigned char *buf;
142 u32 count;
666b7793 143
2e2ac4a3 144 count = gf_ioread32(base + GOLDFISH_TTY_REG_BYTES_READY);
d78055dc 145 if (count == 0)
666b7793
AH
146 return IRQ_NONE;
147
ebcf0981 148 count = tty_prepare_flip_string(&qtty->port, &buf, count);
7157d2be
MD
149
150 address = (unsigned long)(void *)buf;
151 goldfish_tty_rw(qtty, address, count, 0);
152
5f6a8515 153 tty_flip_buffer_push(&qtty->port);
666b7793
AH
154 return IRQ_HANDLED;
155}
156
157static int goldfish_tty_activate(struct tty_port *port, struct tty_struct *tty)
158{
d78055dc
A
159 struct goldfish_tty *qtty = container_of(port, struct goldfish_tty,
160 port);
2e2ac4a3 161 gf_iowrite32(GOLDFISH_TTY_CMD_INT_ENABLE, qtty->base + GOLDFISH_TTY_REG_CMD);
666b7793
AH
162 return 0;
163}
164
165static void goldfish_tty_shutdown(struct tty_port *port)
166{
d78055dc
A
167 struct goldfish_tty *qtty = container_of(port, struct goldfish_tty,
168 port);
2e2ac4a3 169 gf_iowrite32(GOLDFISH_TTY_CMD_INT_DISABLE, qtty->base + GOLDFISH_TTY_REG_CMD);
666b7793
AH
170}
171
d78055dc 172static int goldfish_tty_open(struct tty_struct *tty, struct file *filp)
666b7793
AH
173{
174 struct goldfish_tty *qtty = &goldfish_ttys[tty->index];
175 return tty_port_open(&qtty->port, tty, filp);
176}
177
d78055dc 178static void goldfish_tty_close(struct tty_struct *tty, struct file *filp)
666b7793
AH
179{
180 tty_port_close(tty->port, tty, filp);
181}
182
183static void goldfish_tty_hangup(struct tty_struct *tty)
184{
185 tty_port_hangup(tty->port);
186}
187
95713967
JSS
188static ssize_t goldfish_tty_write(struct tty_struct *tty, const u8 *buf,
189 size_t count)
666b7793
AH
190{
191 goldfish_tty_do_write(tty->index, buf, count);
192 return count;
193}
194
03b3b1a2 195static unsigned int goldfish_tty_write_room(struct tty_struct *tty)
666b7793
AH
196{
197 return 0x10000;
198}
199
fff4ef17 200static unsigned int goldfish_tty_chars_in_buffer(struct tty_struct *tty)
666b7793
AH
201{
202 struct goldfish_tty *qtty = &goldfish_ttys[tty->index];
203 void __iomem *base = qtty->base;
2e2ac4a3 204 return gf_ioread32(base + GOLDFISH_TTY_REG_BYTES_READY);
666b7793
AH
205}
206
d78055dc
A
207static void goldfish_tty_console_write(struct console *co, const char *b,
208 unsigned count)
666b7793
AH
209{
210 goldfish_tty_do_write(co->index, b, count);
211}
212
d78055dc
A
213static struct tty_driver *goldfish_tty_console_device(struct console *c,
214 int *index)
666b7793
AH
215{
216 *index = c->index;
217 return goldfish_tty_driver;
218}
219
220static int goldfish_tty_console_setup(struct console *co, char *options)
221{
fda2b418 222 if ((unsigned)co->index >= goldfish_tty_line_count)
666b7793 223 return -ENODEV;
a4dc9236 224 if (!goldfish_ttys[co->index].base)
666b7793
AH
225 return -ENODEV;
226 return 0;
227}
228
04b757df 229static const struct tty_port_operations goldfish_port_ops = {
666b7793
AH
230 .activate = goldfish_tty_activate,
231 .shutdown = goldfish_tty_shutdown
232};
233
d78055dc 234static const struct tty_operations goldfish_tty_ops = {
666b7793
AH
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,
241};
242
243static int goldfish_tty_create_driver(void)
244{
245 int ret;
246 struct tty_driver *tty;
247
6396bb22
KC
248 goldfish_ttys = kcalloc(goldfish_tty_line_count,
249 sizeof(*goldfish_ttys),
250 GFP_KERNEL);
d78055dc 251 if (goldfish_ttys == NULL) {
666b7793
AH
252 ret = -ENOMEM;
253 goto err_alloc_goldfish_ttys_failed;
254 }
39b7b42b
JS
255 tty = tty_alloc_driver(goldfish_tty_line_count,
256 TTY_DRIVER_RESET_TERMIOS | TTY_DRIVER_REAL_RAW |
257 TTY_DRIVER_DYNAMIC_DEV);
258 if (IS_ERR(tty)) {
259 ret = PTR_ERR(tty);
260 goto err_tty_alloc_driver_failed;
666b7793
AH
261 }
262 tty->driver_name = "goldfish";
263 tty->name = "ttyGF";
264 tty->type = TTY_DRIVER_TYPE_SERIAL;
265 tty->subtype = SERIAL_TYPE_NORMAL;
266 tty->init_termios = tty_std_termios;
666b7793
AH
267 tty_set_operations(tty, &goldfish_tty_ops);
268 ret = tty_register_driver(tty);
d78055dc 269 if (ret)
666b7793
AH
270 goto err_tty_register_driver_failed;
271
272 goldfish_tty_driver = tty;
273 return 0;
274
275err_tty_register_driver_failed:
9f90a4dd 276 tty_driver_kref_put(tty);
39b7b42b 277err_tty_alloc_driver_failed:
666b7793
AH
278 kfree(goldfish_ttys);
279 goldfish_ttys = NULL;
280err_alloc_goldfish_ttys_failed:
281 return ret;
282}
283
284static void goldfish_tty_delete_driver(void)
285{
286 tty_unregister_driver(goldfish_tty_driver);
9f90a4dd 287 tty_driver_kref_put(goldfish_tty_driver);
666b7793
AH
288 goldfish_tty_driver = NULL;
289 kfree(goldfish_ttys);
290 goldfish_ttys = NULL;
291}
292
293static int goldfish_tty_probe(struct platform_device *pdev)
294{
295 struct goldfish_tty *qtty;
7157d2be 296 int ret = -ENODEV;
666b7793
AH
297 struct resource *r;
298 struct device *ttydev;
299 void __iomem *base;
5acb78dc 300 int irq;
465893e1 301 unsigned int line;
666b7793
AH
302
303 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
7157d2be
MD
304 if (!r) {
305 pr_err("goldfish_tty: No MEM resource available!\n");
306 return -ENOMEM;
307 }
666b7793
AH
308
309 base = ioremap(r->start, 0x1000);
7157d2be
MD
310 if (!base) {
311 pr_err("goldfish_tty: Unable to ioremap base!\n");
312 return -ENOMEM;
313 }
666b7793 314
5acb78dc
LP
315 irq = platform_get_irq(pdev, 0);
316 if (irq < 0) {
317 ret = irq;
666b7793 318 goto err_unmap;
7157d2be 319 }
666b7793 320
666b7793 321 mutex_lock(&goldfish_tty_lock);
465893e1
GH
322
323 if (pdev->id == PLATFORM_DEVID_NONE)
324 line = goldfish_tty_current_line_count;
325 else
326 line = pdev->id;
327
7157d2be
MD
328 if (line >= goldfish_tty_line_count) {
329 pr_err("goldfish_tty: Reached maximum tty number of %d.\n",
330 goldfish_tty_current_line_count);
331 ret = -ENOMEM;
332 goto err_unlock;
333 }
465893e1 334
d78055dc 335 if (goldfish_tty_current_line_count == 0) {
666b7793 336 ret = goldfish_tty_create_driver();
d78055dc 337 if (ret)
7157d2be 338 goto err_unlock;
666b7793
AH
339 }
340 goldfish_tty_current_line_count++;
341
465893e1 342 qtty = &goldfish_ttys[line];
666b7793
AH
343 spin_lock_init(&qtty->lock);
344 tty_port_init(&qtty->port);
345 qtty->port.ops = &goldfish_port_ops;
346 qtty->base = base;
347 qtty->irq = irq;
7157d2be
MD
348 qtty->dev = &pdev->dev;
349
350 /*
351 * Goldfish TTY device used by the Goldfish emulator
352 * should identify itself with 0, forcing the driver
353 * to use virtual addresses. Goldfish TTY device
354 * on Ranchu emulator (qemu2) returns 1 here and
355 * driver will use physical addresses.
356 */
2e2ac4a3 357 qtty->version = gf_ioread32(base + GOLDFISH_TTY_REG_VERSION);
7157d2be
MD
358
359 /*
360 * Goldfish TTY device on Ranchu emulator (qemu2)
361 * will use DMA for read/write IO operations.
362 */
363 if (qtty->version > 0) {
364 /*
365 * Initialize dma_mask to 32-bits.
366 */
367 if (!pdev->dev.dma_mask)
368 pdev->dev.dma_mask = &pdev->dev.coherent_dma_mask;
369 ret = dma_set_mask(&pdev->dev, DMA_BIT_MASK(32));
370 if (ret) {
371 dev_err(&pdev->dev, "No suitable DMA available.\n");
372 goto err_dec_line_count;
373 }
374 }
666b7793 375
2e2ac4a3 376 gf_iowrite32(GOLDFISH_TTY_CMD_INT_DISABLE, base + GOLDFISH_TTY_REG_CMD);
666b7793 377
d78055dc 378 ret = request_irq(irq, goldfish_tty_interrupt, IRQF_SHARED,
7157d2be
MD
379 "goldfish_tty", qtty);
380 if (ret) {
381 pr_err("goldfish_tty: No IRQ available!\n");
382 goto err_dec_line_count;
383 }
666b7793
AH
384
385 ttydev = tty_port_register_device(&qtty->port, goldfish_tty_driver,
7157d2be 386 line, &pdev->dev);
d78055dc 387 if (IS_ERR(ttydev)) {
666b7793
AH
388 ret = PTR_ERR(ttydev);
389 goto err_tty_register_device_failed;
390 }
391
392 strcpy(qtty->console.name, "ttyGF");
393 qtty->console.write = goldfish_tty_console_write;
394 qtty->console.device = goldfish_tty_console_device;
395 qtty->console.setup = goldfish_tty_console_setup;
396 qtty->console.flags = CON_PRINTBUFFER;
465893e1 397 qtty->console.index = line;
666b7793 398 register_console(&qtty->console);
465893e1 399 platform_set_drvdata(pdev, qtty);
666b7793
AH
400
401 mutex_unlock(&goldfish_tty_lock);
402 return 0;
403
666b7793 404err_tty_register_device_failed:
1a5c2d1d 405 free_irq(irq, qtty);
7157d2be 406err_dec_line_count:
507b0506 407 tty_port_destroy(&qtty->port);
666b7793 408 goldfish_tty_current_line_count--;
d78055dc 409 if (goldfish_tty_current_line_count == 0)
666b7793 410 goldfish_tty_delete_driver();
7157d2be 411err_unlock:
666b7793
AH
412 mutex_unlock(&goldfish_tty_lock);
413err_unmap:
414 iounmap(base);
415 return ret;
416}
417
418static int goldfish_tty_remove(struct platform_device *pdev)
419{
465893e1 420 struct goldfish_tty *qtty = platform_get_drvdata(pdev);
666b7793
AH
421
422 mutex_lock(&goldfish_tty_lock);
423
666b7793 424 unregister_console(&qtty->console);
465893e1 425 tty_unregister_device(goldfish_tty_driver, qtty->console.index);
666b7793 426 iounmap(qtty->base);
a4dc9236 427 qtty->base = NULL;
499e13aa 428 free_irq(qtty->irq, qtty);
507b0506 429 tty_port_destroy(&qtty->port);
666b7793 430 goldfish_tty_current_line_count--;
d78055dc 431 if (goldfish_tty_current_line_count == 0)
666b7793
AH
432 goldfish_tty_delete_driver();
433 mutex_unlock(&goldfish_tty_lock);
434 return 0;
435}
436
6a28fd2b 437#ifdef CONFIG_GOLDFISH_TTY_EARLY_CONSOLE
3f8bab17 438static void gf_early_console_putchar(struct uart_port *port, unsigned char ch)
3840ed95 439{
2e2ac4a3 440 gf_iowrite32(ch, port->membase);
3840ed95
MD
441}
442
443static void gf_early_write(struct console *con, const char *s, unsigned int n)
444{
445 struct earlycon_device *dev = con->data;
446
447 uart_console_write(&dev->port, s, n, gf_early_console_putchar);
448}
449
450static int __init gf_earlycon_setup(struct earlycon_device *device,
451 const char *opt)
452{
453 if (!device->port.membase)
454 return -ENODEV;
455
456 device->con->write = gf_early_write;
457 return 0;
458}
459
460OF_EARLYCON_DECLARE(early_gf_tty, "google,goldfish-tty", gf_earlycon_setup);
6a28fd2b 461#endif
3840ed95 462
9b883eea
MD
463static const struct of_device_id goldfish_tty_of_match[] = {
464 { .compatible = "google,goldfish-tty", },
465 {},
466};
467
468MODULE_DEVICE_TABLE(of, goldfish_tty_of_match);
469
666b7793
AH
470static struct platform_driver goldfish_tty_platform_driver = {
471 .probe = goldfish_tty_probe,
472 .remove = goldfish_tty_remove,
473 .driver = {
9b883eea
MD
474 .name = "goldfish_tty",
475 .of_match_table = goldfish_tty_of_match,
666b7793
AH
476 }
477};
478
479module_platform_driver(goldfish_tty_platform_driver);
480
481MODULE_LICENSE("GPL v2");