diff options
Diffstat (limited to 'drivers/tty/goldfish.c')
-rw-r--r-- | drivers/tty/goldfish.c | 234 |
1 files changed, 185 insertions, 49 deletions
diff --git a/drivers/tty/goldfish.c b/drivers/tty/goldfish.c index 996bd473dd03..381e981dee06 100644 --- a/drivers/tty/goldfish.c +++ b/drivers/tty/goldfish.c | |||
@@ -1,6 +1,7 @@ | |||
1 | /* | 1 | /* |
2 | * Copyright (C) 2007 Google, Inc. | 2 | * Copyright (C) 2007 Google, Inc. |
3 | * Copyright (C) 2012 Intel, Inc. | 3 | * Copyright (C) 2012 Intel, Inc. |
4 | * Copyright (C) 2017 Imagination Technologies Ltd. | ||
4 | * | 5 | * |
5 | * This software is licensed under the terms of the GNU General Public | 6 | * This software is licensed under the terms of the GNU General Public |
6 | * License version 2, as published by the Free Software Foundation, and | 7 | * License version 2, as published by the Free Software Foundation, and |
@@ -22,21 +23,23 @@ | |||
22 | #include <linux/io.h> | 23 | #include <linux/io.h> |
23 | #include <linux/module.h> | 24 | #include <linux/module.h> |
24 | #include <linux/goldfish.h> | 25 | #include <linux/goldfish.h> |
25 | 26 | #include <linux/mm.h> | |
26 | enum { | 27 | #include <linux/dma-mapping.h> |
27 | GOLDFISH_TTY_PUT_CHAR = 0x00, | 28 | #include <linux/serial_core.h> |
28 | GOLDFISH_TTY_BYTES_READY = 0x04, | 29 | |
29 | GOLDFISH_TTY_CMD = 0x08, | 30 | /* Goldfish tty register's offsets */ |
30 | 31 | #define GOLDFISH_TTY_REG_BYTES_READY 0x04 | |
31 | GOLDFISH_TTY_DATA_PTR = 0x10, | 32 | #define GOLDFISH_TTY_REG_CMD 0x08 |
32 | GOLDFISH_TTY_DATA_LEN = 0x14, | 33 | #define GOLDFISH_TTY_REG_DATA_PTR 0x10 |
33 | GOLDFISH_TTY_DATA_PTR_HIGH = 0x18, | 34 | #define GOLDFISH_TTY_REG_DATA_LEN 0x14 |
34 | 35 | #define GOLDFISH_TTY_REG_DATA_PTR_HIGH 0x18 | |
35 | GOLDFISH_TTY_CMD_INT_DISABLE = 0, | 36 | #define GOLDFISH_TTY_REG_VERSION 0x20 |
36 | GOLDFISH_TTY_CMD_INT_ENABLE = 1, | 37 | |
37 | GOLDFISH_TTY_CMD_WRITE_BUFFER = 2, | 38 | /* Goldfish tty commands */ |
38 | GOLDFISH_TTY_CMD_READ_BUFFER = 3, | 39 | #define GOLDFISH_TTY_CMD_INT_DISABLE 0 |
39 | }; | 40 | #define GOLDFISH_TTY_CMD_INT_ENABLE 1 |
41 | #define GOLDFISH_TTY_CMD_WRITE_BUFFER 2 | ||
42 | #define GOLDFISH_TTY_CMD_READ_BUFFER 3 | ||
40 | 43 | ||
41 | struct goldfish_tty { | 44 | struct goldfish_tty { |
42 | struct tty_port port; | 45 | struct tty_port port; |
@@ -45,6 +48,8 @@ struct goldfish_tty { | |||
45 | u32 irq; | 48 | u32 irq; |
46 | int opencount; | 49 | int opencount; |
47 | struct console console; | 50 | struct console console; |
51 | u32 version; | ||
52 | struct device *dev; | ||
48 | }; | 53 | }; |
49 | 54 | ||
50 | static DEFINE_MUTEX(goldfish_tty_lock); | 55 | static DEFINE_MUTEX(goldfish_tty_lock); |
@@ -53,38 +58,107 @@ static u32 goldfish_tty_line_count = 8; | |||
53 | static u32 goldfish_tty_current_line_count; | 58 | static u32 goldfish_tty_current_line_count; |
54 | static struct goldfish_tty *goldfish_ttys; | 59 | static struct goldfish_tty *goldfish_ttys; |
55 | 60 | ||
56 | static void goldfish_tty_do_write(int line, const char *buf, unsigned count) | 61 | static void do_rw_io(struct goldfish_tty *qtty, |
62 | unsigned long address, | ||
63 | unsigned int count, | ||
64 | int is_write) | ||
57 | { | 65 | { |
58 | unsigned long irq_flags; | 66 | unsigned long irq_flags; |
59 | struct goldfish_tty *qtty = &goldfish_ttys[line]; | ||
60 | void __iomem *base = qtty->base; | 67 | void __iomem *base = qtty->base; |
68 | |||
61 | spin_lock_irqsave(&qtty->lock, irq_flags); | 69 | spin_lock_irqsave(&qtty->lock, irq_flags); |
62 | gf_write_ptr(buf, base + GOLDFISH_TTY_DATA_PTR, | 70 | gf_write_ptr((void *)address, base + GOLDFISH_TTY_REG_DATA_PTR, |
63 | base + GOLDFISH_TTY_DATA_PTR_HIGH); | 71 | base + GOLDFISH_TTY_REG_DATA_PTR_HIGH); |
64 | writel(count, base + GOLDFISH_TTY_DATA_LEN); | 72 | writel(count, base + GOLDFISH_TTY_REG_DATA_LEN); |
65 | writel(GOLDFISH_TTY_CMD_WRITE_BUFFER, base + GOLDFISH_TTY_CMD); | 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 | |||
66 | spin_unlock_irqrestore(&qtty->lock, irq_flags); | 81 | spin_unlock_irqrestore(&qtty->lock, irq_flags); |
67 | } | 82 | } |
68 | 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 | |||
69 | static irqreturn_t goldfish_tty_interrupt(int irq, void *dev_id) | 145 | static irqreturn_t goldfish_tty_interrupt(int irq, void *dev_id) |
70 | { | 146 | { |
71 | struct goldfish_tty *qtty = dev_id; | 147 | struct goldfish_tty *qtty = dev_id; |
72 | void __iomem *base = qtty->base; | 148 | void __iomem *base = qtty->base; |
73 | unsigned long irq_flags; | 149 | unsigned long address; |
74 | unsigned char *buf; | 150 | unsigned char *buf; |
75 | u32 count; | 151 | u32 count; |
76 | 152 | ||
77 | count = readl(base + GOLDFISH_TTY_BYTES_READY); | 153 | count = readl(base + GOLDFISH_TTY_REG_BYTES_READY); |
78 | if (count == 0) | 154 | if (count == 0) |
79 | return IRQ_NONE; | 155 | return IRQ_NONE; |
80 | 156 | ||
81 | count = tty_prepare_flip_string(&qtty->port, &buf, count); | 157 | count = tty_prepare_flip_string(&qtty->port, &buf, count); |
82 | spin_lock_irqsave(&qtty->lock, irq_flags); | 158 | |
83 | gf_write_ptr(buf, base + GOLDFISH_TTY_DATA_PTR, | 159 | address = (unsigned long)(void *)buf; |
84 | base + GOLDFISH_TTY_DATA_PTR_HIGH); | 160 | goldfish_tty_rw(qtty, address, count, 0); |
85 | writel(count, base + GOLDFISH_TTY_DATA_LEN); | 161 | |
86 | writel(GOLDFISH_TTY_CMD_READ_BUFFER, base + GOLDFISH_TTY_CMD); | ||
87 | spin_unlock_irqrestore(&qtty->lock, irq_flags); | ||
88 | tty_schedule_flip(&qtty->port); | 162 | tty_schedule_flip(&qtty->port); |
89 | return IRQ_HANDLED; | 163 | return IRQ_HANDLED; |
90 | } | 164 | } |
@@ -93,7 +167,7 @@ static int goldfish_tty_activate(struct tty_port *port, struct tty_struct *tty) | |||
93 | { | 167 | { |
94 | struct goldfish_tty *qtty = container_of(port, struct goldfish_tty, | 168 | struct goldfish_tty *qtty = container_of(port, struct goldfish_tty, |
95 | port); | 169 | port); |
96 | writel(GOLDFISH_TTY_CMD_INT_ENABLE, qtty->base + GOLDFISH_TTY_CMD); | 170 | writel(GOLDFISH_TTY_CMD_INT_ENABLE, qtty->base + GOLDFISH_TTY_REG_CMD); |
97 | return 0; | 171 | return 0; |
98 | } | 172 | } |
99 | 173 | ||
@@ -101,7 +175,7 @@ static void goldfish_tty_shutdown(struct tty_port *port) | |||
101 | { | 175 | { |
102 | struct goldfish_tty *qtty = container_of(port, struct goldfish_tty, | 176 | struct goldfish_tty *qtty = container_of(port, struct goldfish_tty, |
103 | port); | 177 | port); |
104 | writel(GOLDFISH_TTY_CMD_INT_DISABLE, qtty->base + GOLDFISH_TTY_CMD); | 178 | writel(GOLDFISH_TTY_CMD_INT_DISABLE, qtty->base + GOLDFISH_TTY_REG_CMD); |
105 | } | 179 | } |
106 | 180 | ||
107 | static int goldfish_tty_open(struct tty_struct *tty, struct file *filp) | 181 | static int goldfish_tty_open(struct tty_struct *tty, struct file *filp) |
@@ -136,7 +210,7 @@ static int goldfish_tty_chars_in_buffer(struct tty_struct *tty) | |||
136 | { | 210 | { |
137 | struct goldfish_tty *qtty = &goldfish_ttys[tty->index]; | 211 | struct goldfish_tty *qtty = &goldfish_ttys[tty->index]; |
138 | void __iomem *base = qtty->base; | 212 | void __iomem *base = qtty->base; |
139 | return readl(base + GOLDFISH_TTY_BYTES_READY); | 213 | return readl(base + GOLDFISH_TTY_REG_BYTES_READY); |
140 | } | 214 | } |
141 | 215 | ||
142 | static void goldfish_tty_console_write(struct console *co, const char *b, | 216 | static void goldfish_tty_console_write(struct console *co, const char *b, |
@@ -227,7 +301,7 @@ static void goldfish_tty_delete_driver(void) | |||
227 | static int goldfish_tty_probe(struct platform_device *pdev) | 301 | static int goldfish_tty_probe(struct platform_device *pdev) |
228 | { | 302 | { |
229 | struct goldfish_tty *qtty; | 303 | struct goldfish_tty *qtty; |
230 | int ret = -EINVAL; | 304 | int ret = -ENODEV; |
231 | struct resource *r; | 305 | struct resource *r; |
232 | struct device *ttydev; | 306 | struct device *ttydev; |
233 | void __iomem *base; | 307 | void __iomem *base; |
@@ -235,16 +309,22 @@ static int goldfish_tty_probe(struct platform_device *pdev) | |||
235 | unsigned int line; | 309 | unsigned int line; |
236 | 310 | ||
237 | r = platform_get_resource(pdev, IORESOURCE_MEM, 0); | 311 | r = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
238 | if (r == NULL) | 312 | if (!r) { |
239 | return -EINVAL; | 313 | pr_err("goldfish_tty: No MEM resource available!\n"); |
314 | return -ENOMEM; | ||
315 | } | ||
240 | 316 | ||
241 | base = ioremap(r->start, 0x1000); | 317 | base = ioremap(r->start, 0x1000); |
242 | if (base == NULL) | 318 | if (!base) { |
243 | pr_err("goldfish_tty: unable to remap base\n"); | 319 | pr_err("goldfish_tty: Unable to ioremap base!\n"); |
320 | return -ENOMEM; | ||
321 | } | ||
244 | 322 | ||
245 | r = platform_get_resource(pdev, IORESOURCE_IRQ, 0); | 323 | r = platform_get_resource(pdev, IORESOURCE_IRQ, 0); |
246 | if (r == NULL) | 324 | if (!r) { |
325 | pr_err("goldfish_tty: No IRQ resource available!\n"); | ||
247 | goto err_unmap; | 326 | goto err_unmap; |
327 | } | ||
248 | 328 | ||
249 | irq = r->start; | 329 | irq = r->start; |
250 | 330 | ||
@@ -255,13 +335,17 @@ static int goldfish_tty_probe(struct platform_device *pdev) | |||
255 | else | 335 | else |
256 | line = pdev->id; | 336 | line = pdev->id; |
257 | 337 | ||
258 | if (line >= goldfish_tty_line_count) | 338 | if (line >= goldfish_tty_line_count) { |
259 | goto err_create_driver_failed; | 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 | } | ||
260 | 344 | ||
261 | if (goldfish_tty_current_line_count == 0) { | 345 | if (goldfish_tty_current_line_count == 0) { |
262 | ret = goldfish_tty_create_driver(); | 346 | ret = goldfish_tty_create_driver(); |
263 | if (ret) | 347 | if (ret) |
264 | goto err_create_driver_failed; | 348 | goto err_unlock; |
265 | } | 349 | } |
266 | goldfish_tty_current_line_count++; | 350 | goldfish_tty_current_line_count++; |
267 | 351 | ||
@@ -271,17 +355,45 @@ static int goldfish_tty_probe(struct platform_device *pdev) | |||
271 | qtty->port.ops = &goldfish_port_ops; | 355 | qtty->port.ops = &goldfish_port_ops; |
272 | qtty->base = base; | 356 | qtty->base = base; |
273 | qtty->irq = irq; | 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 | } | ||
274 | 385 | ||
275 | writel(GOLDFISH_TTY_CMD_INT_DISABLE, base + GOLDFISH_TTY_CMD); | 386 | writel(GOLDFISH_TTY_CMD_INT_DISABLE, base + GOLDFISH_TTY_REG_CMD); |
276 | 387 | ||
277 | ret = request_irq(irq, goldfish_tty_interrupt, IRQF_SHARED, | 388 | ret = request_irq(irq, goldfish_tty_interrupt, IRQF_SHARED, |
278 | "goldfish_tty", qtty); | 389 | "goldfish_tty", qtty); |
279 | if (ret) | 390 | if (ret) { |
280 | goto err_request_irq_failed; | 391 | pr_err("goldfish_tty: No IRQ available!\n"); |
281 | 392 | goto err_dec_line_count; | |
393 | } | ||
282 | 394 | ||
283 | ttydev = tty_port_register_device(&qtty->port, goldfish_tty_driver, | 395 | ttydev = tty_port_register_device(&qtty->port, goldfish_tty_driver, |
284 | line, &pdev->dev); | 396 | line, &pdev->dev); |
285 | if (IS_ERR(ttydev)) { | 397 | if (IS_ERR(ttydev)) { |
286 | ret = PTR_ERR(ttydev); | 398 | ret = PTR_ERR(ttydev); |
287 | goto err_tty_register_device_failed; | 399 | goto err_tty_register_device_failed; |
@@ -301,11 +413,11 @@ static int goldfish_tty_probe(struct platform_device *pdev) | |||
301 | 413 | ||
302 | err_tty_register_device_failed: | 414 | err_tty_register_device_failed: |
303 | free_irq(irq, qtty); | 415 | free_irq(irq, qtty); |
304 | err_request_irq_failed: | 416 | err_dec_line_count: |
305 | goldfish_tty_current_line_count--; | 417 | goldfish_tty_current_line_count--; |
306 | if (goldfish_tty_current_line_count == 0) | 418 | if (goldfish_tty_current_line_count == 0) |
307 | goldfish_tty_delete_driver(); | 419 | goldfish_tty_delete_driver(); |
308 | err_create_driver_failed: | 420 | err_unlock: |
309 | mutex_unlock(&goldfish_tty_lock); | 421 | mutex_unlock(&goldfish_tty_lock); |
310 | err_unmap: | 422 | err_unmap: |
311 | iounmap(base); | 423 | iounmap(base); |
@@ -330,6 +442,30 @@ static int goldfish_tty_remove(struct platform_device *pdev) | |||
330 | return 0; | 442 | return 0; |
331 | } | 443 | } |
332 | 444 | ||
445 | static void gf_early_console_putchar(struct uart_port *port, int ch) | ||
446 | { | ||
447 | __raw_writel(ch, port->membase); | ||
448 | } | ||
449 | |||
450 | static 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 | |||
457 | static 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 | |||
467 | OF_EARLYCON_DECLARE(early_gf_tty, "google,goldfish-tty", gf_earlycon_setup); | ||
468 | |||
333 | static const struct of_device_id goldfish_tty_of_match[] = { | 469 | static const struct of_device_id goldfish_tty_of_match[] = { |
334 | { .compatible = "google,goldfish-tty", }, | 470 | { .compatible = "google,goldfish-tty", }, |
335 | {}, | 471 | {}, |