Loading...
1// SPDX-License-Identifier: GPL-2.0
2/*
3 * Copyright (C) 2007 Google, Inc.
4 * Copyright (C) 2012 Intel, Inc.
5 * Copyright (C) 2017 Imagination Technologies Ltd.
6 */
7
8#include <linux/console.h>
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>
16#include <linux/goldfish.h>
17#include <linux/mm.h>
18#include <linux/dma-mapping.h>
19#include <linux/serial_core.h>
20
21/* Goldfish tty register's offsets */
22#define GOLDFISH_TTY_REG_BYTES_READY 0x04
23#define GOLDFISH_TTY_REG_CMD 0x08
24#define GOLDFISH_TTY_REG_DATA_PTR 0x10
25#define GOLDFISH_TTY_REG_DATA_LEN 0x14
26#define GOLDFISH_TTY_REG_DATA_PTR_HIGH 0x18
27#define GOLDFISH_TTY_REG_VERSION 0x20
28
29/* Goldfish tty commands */
30#define GOLDFISH_TTY_CMD_INT_DISABLE 0
31#define GOLDFISH_TTY_CMD_INT_ENABLE 1
32#define GOLDFISH_TTY_CMD_WRITE_BUFFER 2
33#define GOLDFISH_TTY_CMD_READ_BUFFER 3
34
35struct goldfish_tty {
36 struct tty_port port;
37 spinlock_t lock;
38 void __iomem *base;
39 u32 irq;
40 int opencount;
41 struct console console;
42 u32 version;
43 struct device *dev;
44};
45
46static DEFINE_MUTEX(goldfish_tty_lock);
47static struct tty_driver *goldfish_tty_driver;
48static u32 goldfish_tty_line_count = 8;
49static u32 goldfish_tty_current_line_count;
50static struct goldfish_tty *goldfish_ttys;
51
52static void do_rw_io(struct goldfish_tty *qtty,
53 unsigned long address,
54 unsigned int count,
55 int is_write)
56{
57 unsigned long irq_flags;
58 void __iomem *base = qtty->base;
59
60 spin_lock_irqsave(&qtty->lock, irq_flags);
61 gf_write_ptr((void *)address, base + GOLDFISH_TTY_REG_DATA_PTR,
62 base + GOLDFISH_TTY_REG_DATA_PTR_HIGH);
63 writel(count, base + GOLDFISH_TTY_REG_DATA_LEN);
64
65 if (is_write)
66 writel(GOLDFISH_TTY_CMD_WRITE_BUFFER,
67 base + GOLDFISH_TTY_REG_CMD);
68 else
69 writel(GOLDFISH_TTY_CMD_READ_BUFFER,
70 base + GOLDFISH_TTY_REG_CMD);
71
72 spin_unlock_irqrestore(&qtty->lock, irq_flags);
73}
74
75static void goldfish_tty_rw(struct goldfish_tty *qtty,
76 unsigned long addr,
77 unsigned int count,
78 int is_write)
79{
80 dma_addr_t dma_handle;
81 enum dma_data_direction dma_dir;
82
83 dma_dir = (is_write ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
84 if (qtty->version > 0) {
85 /*
86 * Goldfish TTY for Ranchu platform uses
87 * physical addresses and DMA for read/write operations
88 */
89 unsigned long addr_end = addr + count;
90
91 while (addr < addr_end) {
92 unsigned long pg_end = (addr & PAGE_MASK) + PAGE_SIZE;
93 unsigned long next =
94 pg_end < addr_end ? pg_end : addr_end;
95 unsigned long avail = next - addr;
96
97 /*
98 * Map the buffer's virtual address to the DMA address
99 * so the buffer can be accessed by the device.
100 */
101 dma_handle = dma_map_single(qtty->dev, (void *)addr,
102 avail, dma_dir);
103
104 if (dma_mapping_error(qtty->dev, dma_handle)) {
105 dev_err(qtty->dev, "tty: DMA mapping error.\n");
106 return;
107 }
108 do_rw_io(qtty, dma_handle, avail, is_write);
109
110 /*
111 * Unmap the previously mapped region after
112 * the completion of the read/write operation.
113 */
114 dma_unmap_single(qtty->dev, dma_handle, avail, dma_dir);
115
116 addr += avail;
117 }
118 } else {
119 /*
120 * Old style Goldfish TTY used on the Goldfish platform
121 * uses virtual addresses.
122 */
123 do_rw_io(qtty, addr, count, is_write);
124 }
125}
126
127static void goldfish_tty_do_write(int line, const char *buf,
128 unsigned int count)
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
136static irqreturn_t goldfish_tty_interrupt(int irq, void *dev_id)
137{
138 struct goldfish_tty *qtty = dev_id;
139 void __iomem *base = qtty->base;
140 unsigned long address;
141 unsigned char *buf;
142 u32 count;
143
144 count = readl(base + GOLDFISH_TTY_REG_BYTES_READY);
145 if (count == 0)
146 return IRQ_NONE;
147
148 count = tty_prepare_flip_string(&qtty->port, &buf, count);
149
150 address = (unsigned long)(void *)buf;
151 goldfish_tty_rw(qtty, address, count, 0);
152
153 tty_schedule_flip(&qtty->port);
154 return IRQ_HANDLED;
155}
156
157static int goldfish_tty_activate(struct tty_port *port, struct tty_struct *tty)
158{
159 struct goldfish_tty *qtty = container_of(port, struct goldfish_tty,
160 port);
161 writel(GOLDFISH_TTY_CMD_INT_ENABLE, qtty->base + GOLDFISH_TTY_REG_CMD);
162 return 0;
163}
164
165static void goldfish_tty_shutdown(struct tty_port *port)
166{
167 struct goldfish_tty *qtty = container_of(port, struct goldfish_tty,
168 port);
169 writel(GOLDFISH_TTY_CMD_INT_DISABLE, qtty->base + GOLDFISH_TTY_REG_CMD);
170}
171
172static int goldfish_tty_open(struct tty_struct *tty, struct file *filp)
173{
174 struct goldfish_tty *qtty = &goldfish_ttys[tty->index];
175 return tty_port_open(&qtty->port, tty, filp);
176}
177
178static void goldfish_tty_close(struct tty_struct *tty, struct file *filp)
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
188static int goldfish_tty_write(struct tty_struct *tty, const unsigned char *buf,
189 int count)
190{
191 goldfish_tty_do_write(tty->index, buf, count);
192 return count;
193}
194
195static int goldfish_tty_write_room(struct tty_struct *tty)
196{
197 return 0x10000;
198}
199
200static int goldfish_tty_chars_in_buffer(struct tty_struct *tty)
201{
202 struct goldfish_tty *qtty = &goldfish_ttys[tty->index];
203 void __iomem *base = qtty->base;
204 return readl(base + GOLDFISH_TTY_REG_BYTES_READY);
205}
206
207static void goldfish_tty_console_write(struct console *co, const char *b,
208 unsigned count)
209{
210 goldfish_tty_do_write(co->index, b, count);
211}
212
213static struct tty_driver *goldfish_tty_console_device(struct console *c,
214 int *index)
215{
216 *index = c->index;
217 return goldfish_tty_driver;
218}
219
220static int goldfish_tty_console_setup(struct console *co, char *options)
221{
222 if ((unsigned)co->index >= goldfish_tty_line_count)
223 return -ENODEV;
224 if (!goldfish_ttys[co->index].base)
225 return -ENODEV;
226 return 0;
227}
228
229static const struct tty_port_operations goldfish_port_ops = {
230 .activate = goldfish_tty_activate,
231 .shutdown = goldfish_tty_shutdown
232};
233
234static const struct tty_operations goldfish_tty_ops = {
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
248 goldfish_ttys = kzalloc(sizeof(*goldfish_ttys) *
249 goldfish_tty_line_count, GFP_KERNEL);
250 if (goldfish_ttys == NULL) {
251 ret = -ENOMEM;
252 goto err_alloc_goldfish_ttys_failed;
253 }
254 tty = alloc_tty_driver(goldfish_tty_line_count);
255 if (tty == NULL) {
256 ret = -ENOMEM;
257 goto err_alloc_tty_driver_failed;
258 }
259 tty->driver_name = "goldfish";
260 tty->name = "ttyGF";
261 tty->type = TTY_DRIVER_TYPE_SERIAL;
262 tty->subtype = SERIAL_TYPE_NORMAL;
263 tty->init_termios = tty_std_termios;
264 tty->flags = TTY_DRIVER_RESET_TERMIOS | TTY_DRIVER_REAL_RAW |
265 TTY_DRIVER_DYNAMIC_DEV;
266 tty_set_operations(tty, &goldfish_tty_ops);
267 ret = tty_register_driver(tty);
268 if (ret)
269 goto err_tty_register_driver_failed;
270
271 goldfish_tty_driver = tty;
272 return 0;
273
274err_tty_register_driver_failed:
275 put_tty_driver(tty);
276err_alloc_tty_driver_failed:
277 kfree(goldfish_ttys);
278 goldfish_ttys = NULL;
279err_alloc_goldfish_ttys_failed:
280 return ret;
281}
282
283static void goldfish_tty_delete_driver(void)
284{
285 tty_unregister_driver(goldfish_tty_driver);
286 put_tty_driver(goldfish_tty_driver);
287 goldfish_tty_driver = NULL;
288 kfree(goldfish_ttys);
289 goldfish_ttys = NULL;
290}
291
292static int goldfish_tty_probe(struct platform_device *pdev)
293{
294 struct goldfish_tty *qtty;
295 int ret = -ENODEV;
296 struct resource *r;
297 struct device *ttydev;
298 void __iomem *base;
299 u32 irq;
300 unsigned int line;
301
302 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
303 if (!r) {
304 pr_err("goldfish_tty: No MEM resource available!\n");
305 return -ENOMEM;
306 }
307
308 base = ioremap(r->start, 0x1000);
309 if (!base) {
310 pr_err("goldfish_tty: Unable to ioremap base!\n");
311 return -ENOMEM;
312 }
313
314 r = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
315 if (!r) {
316 pr_err("goldfish_tty: No IRQ resource available!\n");
317 goto err_unmap;
318 }
319
320 irq = r->start;
321
322 mutex_lock(&goldfish_tty_lock);
323
324 if (pdev->id == PLATFORM_DEVID_NONE)
325 line = goldfish_tty_current_line_count;
326 else
327 line = pdev->id;
328
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 }
335
336 if (goldfish_tty_current_line_count == 0) {
337 ret = goldfish_tty_create_driver();
338 if (ret)
339 goto err_unlock;
340 }
341 goldfish_tty_current_line_count++;
342
343 qtty = &goldfish_ttys[line];
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;
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 */
358 qtty->version = readl(base + GOLDFISH_TTY_REG_VERSION);
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 }
376
377 writel(GOLDFISH_TTY_CMD_INT_DISABLE, base + GOLDFISH_TTY_REG_CMD);
378
379 ret = request_irq(irq, goldfish_tty_interrupt, IRQF_SHARED,
380 "goldfish_tty", qtty);
381 if (ret) {
382 pr_err("goldfish_tty: No IRQ available!\n");
383 goto err_dec_line_count;
384 }
385
386 ttydev = tty_port_register_device(&qtty->port, goldfish_tty_driver,
387 line, &pdev->dev);
388 if (IS_ERR(ttydev)) {
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;
398 qtty->console.index = line;
399 register_console(&qtty->console);
400 platform_set_drvdata(pdev, qtty);
401
402 mutex_unlock(&goldfish_tty_lock);
403 return 0;
404
405err_tty_register_device_failed:
406 free_irq(irq, qtty);
407err_dec_line_count:
408 goldfish_tty_current_line_count--;
409 if (goldfish_tty_current_line_count == 0)
410 goldfish_tty_delete_driver();
411err_unlock:
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{
420 struct goldfish_tty *qtty = platform_get_drvdata(pdev);
421
422 mutex_lock(&goldfish_tty_lock);
423
424 unregister_console(&qtty->console);
425 tty_unregister_device(goldfish_tty_driver, qtty->console.index);
426 iounmap(qtty->base);
427 qtty->base = NULL;
428 free_irq(qtty->irq, pdev);
429 goldfish_tty_current_line_count--;
430 if (goldfish_tty_current_line_count == 0)
431 goldfish_tty_delete_driver();
432 mutex_unlock(&goldfish_tty_lock);
433 return 0;
434}
435
436#ifdef CONFIG_GOLDFISH_TTY_EARLY_CONSOLE
437static void gf_early_console_putchar(struct uart_port *port, int ch)
438{
439 __raw_writel(ch, port->membase);
440}
441
442static void gf_early_write(struct console *con, const char *s, unsigned int n)
443{
444 struct earlycon_device *dev = con->data;
445
446 uart_console_write(&dev->port, s, n, gf_early_console_putchar);
447}
448
449static int __init gf_earlycon_setup(struct earlycon_device *device,
450 const char *opt)
451{
452 if (!device->port.membase)
453 return -ENODEV;
454
455 device->con->write = gf_early_write;
456 return 0;
457}
458
459OF_EARLYCON_DECLARE(early_gf_tty, "google,goldfish-tty", gf_earlycon_setup);
460#endif
461
462static const struct of_device_id goldfish_tty_of_match[] = {
463 { .compatible = "google,goldfish-tty", },
464 {},
465};
466
467MODULE_DEVICE_TABLE(of, goldfish_tty_of_match);
468
469static struct platform_driver goldfish_tty_platform_driver = {
470 .probe = goldfish_tty_probe,
471 .remove = goldfish_tty_remove,
472 .driver = {
473 .name = "goldfish_tty",
474 .of_match_table = goldfish_tty_of_match,
475 }
476};
477
478module_platform_driver(goldfish_tty_platform_driver);
479
480MODULE_LICENSE("GPL v2");
1/*
2 * Copyright (C) 2007 Google, Inc.
3 * Copyright (C) 2012 Intel, Inc.
4 *
5 * 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 * may be copied, distributed, and modified under those terms.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 */
15
16#include <linux/console.h>
17#include <linux/interrupt.h>
18#include <linux/platform_device.h>
19#include <linux/tty.h>
20#include <linux/tty_flip.h>
21#include <linux/slab.h>
22#include <linux/io.h>
23#include <linux/module.h>
24#include <linux/goldfish.h>
25
26enum {
27 GOLDFISH_TTY_PUT_CHAR = 0x00,
28 GOLDFISH_TTY_BYTES_READY = 0x04,
29 GOLDFISH_TTY_CMD = 0x08,
30
31 GOLDFISH_TTY_DATA_PTR = 0x10,
32 GOLDFISH_TTY_DATA_LEN = 0x14,
33 GOLDFISH_TTY_DATA_PTR_HIGH = 0x18,
34
35 GOLDFISH_TTY_CMD_INT_DISABLE = 0,
36 GOLDFISH_TTY_CMD_INT_ENABLE = 1,
37 GOLDFISH_TTY_CMD_WRITE_BUFFER = 2,
38 GOLDFISH_TTY_CMD_READ_BUFFER = 3,
39};
40
41struct goldfish_tty {
42 struct tty_port port;
43 spinlock_t lock;
44 void __iomem *base;
45 u32 irq;
46 int opencount;
47 struct console console;
48};
49
50static DEFINE_MUTEX(goldfish_tty_lock);
51static struct tty_driver *goldfish_tty_driver;
52static u32 goldfish_tty_line_count = 8;
53static u32 goldfish_tty_current_line_count;
54static struct goldfish_tty *goldfish_ttys;
55
56static void goldfish_tty_do_write(int line, const char *buf, unsigned count)
57{
58 unsigned long irq_flags;
59 struct goldfish_tty *qtty = &goldfish_ttys[line];
60 void __iomem *base = qtty->base;
61 spin_lock_irqsave(&qtty->lock, irq_flags);
62 gf_write_ptr(buf, base + GOLDFISH_TTY_DATA_PTR,
63 base + GOLDFISH_TTY_DATA_PTR_HIGH);
64 writel(count, base + GOLDFISH_TTY_DATA_LEN);
65 writel(GOLDFISH_TTY_CMD_WRITE_BUFFER, base + GOLDFISH_TTY_CMD);
66 spin_unlock_irqrestore(&qtty->lock, irq_flags);
67}
68
69static irqreturn_t goldfish_tty_interrupt(int irq, void *dev_id)
70{
71 struct goldfish_tty *qtty = dev_id;
72 void __iomem *base = qtty->base;
73 unsigned long irq_flags;
74 unsigned char *buf;
75 u32 count;
76
77 count = readl(base + GOLDFISH_TTY_BYTES_READY);
78 if (count == 0)
79 return IRQ_NONE;
80
81 count = tty_prepare_flip_string(&qtty->port, &buf, count);
82 spin_lock_irqsave(&qtty->lock, irq_flags);
83 gf_write_ptr(buf, base + GOLDFISH_TTY_DATA_PTR,
84 base + GOLDFISH_TTY_DATA_PTR_HIGH);
85 writel(count, base + GOLDFISH_TTY_DATA_LEN);
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);
89 return IRQ_HANDLED;
90}
91
92static int goldfish_tty_activate(struct tty_port *port, struct tty_struct *tty)
93{
94 struct goldfish_tty *qtty = container_of(port, struct goldfish_tty,
95 port);
96 writel(GOLDFISH_TTY_CMD_INT_ENABLE, qtty->base + GOLDFISH_TTY_CMD);
97 return 0;
98}
99
100static void goldfish_tty_shutdown(struct tty_port *port)
101{
102 struct goldfish_tty *qtty = container_of(port, struct goldfish_tty,
103 port);
104 writel(GOLDFISH_TTY_CMD_INT_DISABLE, qtty->base + GOLDFISH_TTY_CMD);
105}
106
107static int goldfish_tty_open(struct tty_struct *tty, struct file *filp)
108{
109 struct goldfish_tty *qtty = &goldfish_ttys[tty->index];
110 return tty_port_open(&qtty->port, tty, filp);
111}
112
113static void goldfish_tty_close(struct tty_struct *tty, struct file *filp)
114{
115 tty_port_close(tty->port, tty, filp);
116}
117
118static void goldfish_tty_hangup(struct tty_struct *tty)
119{
120 tty_port_hangup(tty->port);
121}
122
123static int goldfish_tty_write(struct tty_struct *tty, const unsigned char *buf,
124 int count)
125{
126 goldfish_tty_do_write(tty->index, buf, count);
127 return count;
128}
129
130static int goldfish_tty_write_room(struct tty_struct *tty)
131{
132 return 0x10000;
133}
134
135static int goldfish_tty_chars_in_buffer(struct tty_struct *tty)
136{
137 struct goldfish_tty *qtty = &goldfish_ttys[tty->index];
138 void __iomem *base = qtty->base;
139 return readl(base + GOLDFISH_TTY_BYTES_READY);
140}
141
142static void goldfish_tty_console_write(struct console *co, const char *b,
143 unsigned count)
144{
145 goldfish_tty_do_write(co->index, b, count);
146}
147
148static struct tty_driver *goldfish_tty_console_device(struct console *c,
149 int *index)
150{
151 *index = c->index;
152 return goldfish_tty_driver;
153}
154
155static int goldfish_tty_console_setup(struct console *co, char *options)
156{
157 if ((unsigned)co->index >= goldfish_tty_line_count)
158 return -ENODEV;
159 if (!goldfish_ttys[co->index].base)
160 return -ENODEV;
161 return 0;
162}
163
164static const struct tty_port_operations goldfish_port_ops = {
165 .activate = goldfish_tty_activate,
166 .shutdown = goldfish_tty_shutdown
167};
168
169static const struct tty_operations goldfish_tty_ops = {
170 .open = goldfish_tty_open,
171 .close = goldfish_tty_close,
172 .hangup = goldfish_tty_hangup,
173 .write = goldfish_tty_write,
174 .write_room = goldfish_tty_write_room,
175 .chars_in_buffer = goldfish_tty_chars_in_buffer,
176};
177
178static int goldfish_tty_create_driver(void)
179{
180 int ret;
181 struct tty_driver *tty;
182
183 goldfish_ttys = kzalloc(sizeof(*goldfish_ttys) *
184 goldfish_tty_line_count, GFP_KERNEL);
185 if (goldfish_ttys == NULL) {
186 ret = -ENOMEM;
187 goto err_alloc_goldfish_ttys_failed;
188 }
189 tty = alloc_tty_driver(goldfish_tty_line_count);
190 if (tty == NULL) {
191 ret = -ENOMEM;
192 goto err_alloc_tty_driver_failed;
193 }
194 tty->driver_name = "goldfish";
195 tty->name = "ttyGF";
196 tty->type = TTY_DRIVER_TYPE_SERIAL;
197 tty->subtype = SERIAL_TYPE_NORMAL;
198 tty->init_termios = tty_std_termios;
199 tty->flags = TTY_DRIVER_RESET_TERMIOS | TTY_DRIVER_REAL_RAW |
200 TTY_DRIVER_DYNAMIC_DEV;
201 tty_set_operations(tty, &goldfish_tty_ops);
202 ret = tty_register_driver(tty);
203 if (ret)
204 goto err_tty_register_driver_failed;
205
206 goldfish_tty_driver = tty;
207 return 0;
208
209err_tty_register_driver_failed:
210 put_tty_driver(tty);
211err_alloc_tty_driver_failed:
212 kfree(goldfish_ttys);
213 goldfish_ttys = NULL;
214err_alloc_goldfish_ttys_failed:
215 return ret;
216}
217
218static void goldfish_tty_delete_driver(void)
219{
220 tty_unregister_driver(goldfish_tty_driver);
221 put_tty_driver(goldfish_tty_driver);
222 goldfish_tty_driver = NULL;
223 kfree(goldfish_ttys);
224 goldfish_ttys = NULL;
225}
226
227static int goldfish_tty_probe(struct platform_device *pdev)
228{
229 struct goldfish_tty *qtty;
230 int ret = -EINVAL;
231 struct resource *r;
232 struct device *ttydev;
233 void __iomem *base;
234 u32 irq;
235 unsigned int line;
236
237 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
238 if (r == NULL)
239 return -EINVAL;
240
241 base = ioremap(r->start, 0x1000);
242 if (base == NULL)
243 pr_err("goldfish_tty: unable to remap base\n");
244
245 r = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
246 if (r == NULL)
247 goto err_unmap;
248
249 irq = r->start;
250
251 mutex_lock(&goldfish_tty_lock);
252
253 if (pdev->id == PLATFORM_DEVID_NONE)
254 line = goldfish_tty_current_line_count;
255 else
256 line = pdev->id;
257
258 if (line >= goldfish_tty_line_count)
259 goto err_create_driver_failed;
260
261 if (goldfish_tty_current_line_count == 0) {
262 ret = goldfish_tty_create_driver();
263 if (ret)
264 goto err_create_driver_failed;
265 }
266 goldfish_tty_current_line_count++;
267
268 qtty = &goldfish_ttys[line];
269 spin_lock_init(&qtty->lock);
270 tty_port_init(&qtty->port);
271 qtty->port.ops = &goldfish_port_ops;
272 qtty->base = base;
273 qtty->irq = irq;
274
275 writel(GOLDFISH_TTY_CMD_INT_DISABLE, base + GOLDFISH_TTY_CMD);
276
277 ret = request_irq(irq, goldfish_tty_interrupt, IRQF_SHARED,
278 "goldfish_tty", qtty);
279 if (ret)
280 goto err_request_irq_failed;
281
282
283 ttydev = tty_port_register_device(&qtty->port, goldfish_tty_driver,
284 line, &pdev->dev);
285 if (IS_ERR(ttydev)) {
286 ret = PTR_ERR(ttydev);
287 goto err_tty_register_device_failed;
288 }
289
290 strcpy(qtty->console.name, "ttyGF");
291 qtty->console.write = goldfish_tty_console_write;
292 qtty->console.device = goldfish_tty_console_device;
293 qtty->console.setup = goldfish_tty_console_setup;
294 qtty->console.flags = CON_PRINTBUFFER;
295 qtty->console.index = line;
296 register_console(&qtty->console);
297 platform_set_drvdata(pdev, qtty);
298
299 mutex_unlock(&goldfish_tty_lock);
300 return 0;
301
302err_tty_register_device_failed:
303 free_irq(irq, pdev);
304err_request_irq_failed:
305 goldfish_tty_current_line_count--;
306 if (goldfish_tty_current_line_count == 0)
307 goldfish_tty_delete_driver();
308err_create_driver_failed:
309 mutex_unlock(&goldfish_tty_lock);
310err_unmap:
311 iounmap(base);
312 return ret;
313}
314
315static int goldfish_tty_remove(struct platform_device *pdev)
316{
317 struct goldfish_tty *qtty = platform_get_drvdata(pdev);
318
319 mutex_lock(&goldfish_tty_lock);
320
321 unregister_console(&qtty->console);
322 tty_unregister_device(goldfish_tty_driver, qtty->console.index);
323 iounmap(qtty->base);
324 qtty->base = NULL;
325 free_irq(qtty->irq, pdev);
326 goldfish_tty_current_line_count--;
327 if (goldfish_tty_current_line_count == 0)
328 goldfish_tty_delete_driver();
329 mutex_unlock(&goldfish_tty_lock);
330 return 0;
331}
332
333static const struct of_device_id goldfish_tty_of_match[] = {
334 { .compatible = "google,goldfish-tty", },
335 {},
336};
337
338MODULE_DEVICE_TABLE(of, goldfish_tty_of_match);
339
340static struct platform_driver goldfish_tty_platform_driver = {
341 .probe = goldfish_tty_probe,
342 .remove = goldfish_tty_remove,
343 .driver = {
344 .name = "goldfish_tty",
345 .of_match_table = goldfish_tty_of_match,
346 }
347};
348
349module_platform_driver(goldfish_tty_platform_driver);
350
351MODULE_LICENSE("GPL v2");