]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - drivers/tty/goldfish.c
Merge tag 'mfd-next-4.14' of git://git.kernel.org/pub/scm/linux/kernel/git/lee/mfd
[mirror_ubuntu-bionic-kernel.git] / drivers / tty / goldfish.c
CommitLineData
666b7793
AH
1/*
2 * Copyright (C) 2007 Google, Inc.
3 * Copyright (C) 2012 Intel, Inc.
3840ed95 4 * Copyright (C) 2017 Imagination Technologies Ltd.
666b7793
AH
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>
666b7793
AH
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>
e0f682e0 25#include <linux/goldfish.h>
7157d2be
MD
26#include <linux/mm.h>
27#include <linux/dma-mapping.h>
3840ed95 28#include <linux/serial_core.h>
666b7793 29
2296eee7
AM
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
7157d2be 36#define GOLDFISH_TTY_REG_VERSION 0x20
2296eee7
AM
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
666b7793
AH
43
44struct 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;
7157d2be
MD
51 u32 version;
52 struct device *dev;
666b7793
AH
53};
54
55static DEFINE_MUTEX(goldfish_tty_lock);
56static struct tty_driver *goldfish_tty_driver;
57static u32 goldfish_tty_line_count = 8;
58static u32 goldfish_tty_current_line_count;
59static struct goldfish_tty *goldfish_ttys;
60
7157d2be
MD
61static void do_rw_io(struct goldfish_tty *qtty,
62 unsigned long address,
63 unsigned int count,
64 int is_write)
666b7793
AH
65{
66 unsigned long irq_flags;
666b7793 67 void __iomem *base = qtty->base;
7157d2be 68
666b7793 69 spin_lock_irqsave(&qtty->lock, irq_flags);
7157d2be 70 gf_write_ptr((void *)address, base + GOLDFISH_TTY_REG_DATA_PTR,
2296eee7
AM
71 base + GOLDFISH_TTY_REG_DATA_PTR_HIGH);
72 writel(count, base + GOLDFISH_TTY_REG_DATA_LEN);
7157d2be
MD
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
666b7793
AH
81 spin_unlock_irqrestore(&qtty->lock, irq_flags);
82}
83
7157d2be
MD
84static 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
136static 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
666b7793
AH
145static irqreturn_t goldfish_tty_interrupt(int irq, void *dev_id)
146{
465893e1 147 struct goldfish_tty *qtty = dev_id;
666b7793 148 void __iomem *base = qtty->base;
7157d2be 149 unsigned long address;
666b7793
AH
150 unsigned char *buf;
151 u32 count;
666b7793 152
2296eee7 153 count = readl(base + GOLDFISH_TTY_REG_BYTES_READY);
d78055dc 154 if (count == 0)
666b7793
AH
155 return IRQ_NONE;
156
ebcf0981 157 count = tty_prepare_flip_string(&qtty->port, &buf, count);
7157d2be
MD
158
159 address = (unsigned long)(void *)buf;
160 goldfish_tty_rw(qtty, address, count, 0);
161
ebcf0981 162 tty_schedule_flip(&qtty->port);
666b7793
AH
163 return IRQ_HANDLED;
164}
165
166static int goldfish_tty_activate(struct tty_port *port, struct tty_struct *tty)
167{
d78055dc
A
168 struct goldfish_tty *qtty = container_of(port, struct goldfish_tty,
169 port);
2296eee7 170 writel(GOLDFISH_TTY_CMD_INT_ENABLE, qtty->base + GOLDFISH_TTY_REG_CMD);
666b7793
AH
171 return 0;
172}
173
174static void goldfish_tty_shutdown(struct tty_port *port)
175{
d78055dc
A
176 struct goldfish_tty *qtty = container_of(port, struct goldfish_tty,
177 port);
2296eee7 178 writel(GOLDFISH_TTY_CMD_INT_DISABLE, qtty->base + GOLDFISH_TTY_REG_CMD);
666b7793
AH
179}
180
d78055dc 181static int goldfish_tty_open(struct tty_struct *tty, struct file *filp)
666b7793
AH
182{
183 struct goldfish_tty *qtty = &goldfish_ttys[tty->index];
184 return tty_port_open(&qtty->port, tty, filp);
185}
186
d78055dc 187static void goldfish_tty_close(struct tty_struct *tty, struct file *filp)
666b7793
AH
188{
189 tty_port_close(tty->port, tty, filp);
190}
191
192static void goldfish_tty_hangup(struct tty_struct *tty)
193{
194 tty_port_hangup(tty->port);
195}
196
d78055dc
A
197static int goldfish_tty_write(struct tty_struct *tty, const unsigned char *buf,
198 int count)
666b7793
AH
199{
200 goldfish_tty_do_write(tty->index, buf, count);
201 return count;
202}
203
204static int goldfish_tty_write_room(struct tty_struct *tty)
205{
206 return 0x10000;
207}
208
209static 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;
2296eee7 213 return readl(base + GOLDFISH_TTY_REG_BYTES_READY);
666b7793
AH
214}
215
d78055dc
A
216static void goldfish_tty_console_write(struct console *co, const char *b,
217 unsigned count)
666b7793
AH
218{
219 goldfish_tty_do_write(co->index, b, count);
220}
221
d78055dc
A
222static struct tty_driver *goldfish_tty_console_device(struct console *c,
223 int *index)
666b7793
AH
224{
225 *index = c->index;
226 return goldfish_tty_driver;
227}
228
229static int goldfish_tty_console_setup(struct console *co, char *options)
230{
fda2b418 231 if ((unsigned)co->index >= goldfish_tty_line_count)
666b7793 232 return -ENODEV;
a4dc9236 233 if (!goldfish_ttys[co->index].base)
666b7793
AH
234 return -ENODEV;
235 return 0;
236}
237
04b757df 238static const struct tty_port_operations goldfish_port_ops = {
666b7793
AH
239 .activate = goldfish_tty_activate,
240 .shutdown = goldfish_tty_shutdown
241};
242
d78055dc 243static const struct tty_operations goldfish_tty_ops = {
666b7793
AH
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
252static int goldfish_tty_create_driver(void)
253{
254 int ret;
255 struct tty_driver *tty;
256
d78055dc
A
257 goldfish_ttys = kzalloc(sizeof(*goldfish_ttys) *
258 goldfish_tty_line_count, GFP_KERNEL);
259 if (goldfish_ttys == NULL) {
666b7793
AH
260 ret = -ENOMEM;
261 goto err_alloc_goldfish_ttys_failed;
262 }
263 tty = alloc_tty_driver(goldfish_tty_line_count);
d78055dc 264 if (tty == NULL) {
666b7793
AH
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;
d78055dc
A
273 tty->flags = TTY_DRIVER_RESET_TERMIOS | TTY_DRIVER_REAL_RAW |
274 TTY_DRIVER_DYNAMIC_DEV;
666b7793
AH
275 tty_set_operations(tty, &goldfish_tty_ops);
276 ret = tty_register_driver(tty);
d78055dc 277 if (ret)
666b7793
AH
278 goto err_tty_register_driver_failed;
279
280 goldfish_tty_driver = tty;
281 return 0;
282
283err_tty_register_driver_failed:
284 put_tty_driver(tty);
285err_alloc_tty_driver_failed:
286 kfree(goldfish_ttys);
287 goldfish_ttys = NULL;
288err_alloc_goldfish_ttys_failed:
289 return ret;
290}
291
292static 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
301static int goldfish_tty_probe(struct platform_device *pdev)
302{
303 struct goldfish_tty *qtty;
7157d2be 304 int ret = -ENODEV;
666b7793
AH
305 struct resource *r;
306 struct device *ttydev;
307 void __iomem *base;
308 u32 irq;
465893e1 309 unsigned int line;
666b7793
AH
310
311 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
7157d2be
MD
312 if (!r) {
313 pr_err("goldfish_tty: No MEM resource available!\n");
314 return -ENOMEM;
315 }
666b7793
AH
316
317 base = ioremap(r->start, 0x1000);
7157d2be
MD
318 if (!base) {
319 pr_err("goldfish_tty: Unable to ioremap base!\n");
320 return -ENOMEM;
321 }
666b7793
AH
322
323 r = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
7157d2be
MD
324 if (!r) {
325 pr_err("goldfish_tty: No IRQ resource available!\n");
666b7793 326 goto err_unmap;
7157d2be 327 }
666b7793
AH
328
329 irq = r->start;
330
666b7793 331 mutex_lock(&goldfish_tty_lock);
465893e1
GH
332
333 if (pdev->id == PLATFORM_DEVID_NONE)
334 line = goldfish_tty_current_line_count;
335 else
336 line = pdev->id;
337
7157d2be
MD
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 }
465893e1 344
d78055dc 345 if (goldfish_tty_current_line_count == 0) {
666b7793 346 ret = goldfish_tty_create_driver();
d78055dc 347 if (ret)
7157d2be 348 goto err_unlock;
666b7793
AH
349 }
350 goldfish_tty_current_line_count++;
351
465893e1 352 qtty = &goldfish_ttys[line];
666b7793
AH
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;
7157d2be
MD
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 }
666b7793 385
2296eee7 386 writel(GOLDFISH_TTY_CMD_INT_DISABLE, base + GOLDFISH_TTY_REG_CMD);
666b7793 387
d78055dc 388 ret = request_irq(irq, goldfish_tty_interrupt, IRQF_SHARED,
7157d2be
MD
389 "goldfish_tty", qtty);
390 if (ret) {
391 pr_err("goldfish_tty: No IRQ available!\n");
392 goto err_dec_line_count;
393 }
666b7793
AH
394
395 ttydev = tty_port_register_device(&qtty->port, goldfish_tty_driver,
7157d2be 396 line, &pdev->dev);
d78055dc 397 if (IS_ERR(ttydev)) {
666b7793
AH
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;
465893e1 407 qtty->console.index = line;
666b7793 408 register_console(&qtty->console);
465893e1 409 platform_set_drvdata(pdev, qtty);
666b7793
AH
410
411 mutex_unlock(&goldfish_tty_lock);
412 return 0;
413
666b7793 414err_tty_register_device_failed:
1a5c2d1d 415 free_irq(irq, qtty);
7157d2be 416err_dec_line_count:
666b7793 417 goldfish_tty_current_line_count--;
d78055dc 418 if (goldfish_tty_current_line_count == 0)
666b7793 419 goldfish_tty_delete_driver();
7157d2be 420err_unlock:
666b7793
AH
421 mutex_unlock(&goldfish_tty_lock);
422err_unmap:
423 iounmap(base);
424 return ret;
425}
426
427static int goldfish_tty_remove(struct platform_device *pdev)
428{
465893e1 429 struct goldfish_tty *qtty = platform_get_drvdata(pdev);
666b7793
AH
430
431 mutex_lock(&goldfish_tty_lock);
432
666b7793 433 unregister_console(&qtty->console);
465893e1 434 tty_unregister_device(goldfish_tty_driver, qtty->console.index);
666b7793 435 iounmap(qtty->base);
a4dc9236 436 qtty->base = NULL;
666b7793
AH
437 free_irq(qtty->irq, pdev);
438 goldfish_tty_current_line_count--;
d78055dc 439 if (goldfish_tty_current_line_count == 0)
666b7793
AH
440 goldfish_tty_delete_driver();
441 mutex_unlock(&goldfish_tty_lock);
442 return 0;
443}
444
3840ed95
MD
445static void gf_early_console_putchar(struct uart_port *port, int ch)
446{
447 __raw_writel(ch, port->membase);
448}
449
450static void gf_early_write(struct console *con, const char *s, unsigned int n)
451{
452 struct earlycon_device *dev = con->data;
453
454 uart_console_write(&dev->port, s, n, gf_early_console_putchar);
455}
456
457static int __init gf_earlycon_setup(struct earlycon_device *device,
458 const char *opt)
459{
460 if (!device->port.membase)
461 return -ENODEV;
462
463 device->con->write = gf_early_write;
464 return 0;
465}
466
467OF_EARLYCON_DECLARE(early_gf_tty, "google,goldfish-tty", gf_earlycon_setup);
468
9b883eea
MD
469static const struct of_device_id goldfish_tty_of_match[] = {
470 { .compatible = "google,goldfish-tty", },
471 {},
472};
473
474MODULE_DEVICE_TABLE(of, goldfish_tty_of_match);
475
666b7793
AH
476static struct platform_driver goldfish_tty_platform_driver = {
477 .probe = goldfish_tty_probe,
478 .remove = goldfish_tty_remove,
479 .driver = {
9b883eea
MD
480 .name = "goldfish_tty",
481 .of_match_table = goldfish_tty_of_match,
666b7793
AH
482 }
483};
484
485module_platform_driver(goldfish_tty_platform_driver);
486
487MODULE_LICENSE("GPL v2");