blob: 5ed19a9857adf72f55e7cf72171a053fe5a52201 [file] [log] [blame]
Greg Kroah-Hartmane3b3d0f2017-11-06 18:11:51 +01001// SPDX-License-Identifier: GPL-2.0
Arve Hjønnevåg666b7792013-01-21 23:38:47 +00002/*
3 * Copyright (C) 2007 Google, Inc.
4 * Copyright (C) 2012 Intel, Inc.
Miodrag Dinic3840ed92017-08-29 15:53:20 +02005 * Copyright (C) 2017 Imagination Technologies Ltd.
Arve Hjønnevåg666b7792013-01-21 23:38:47 +00006 */
7
8#include <linux/console.h>
Arve Hjønnevåg666b7792013-01-21 23:38:47 +00009#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>
Randy Dunlapac316722018-06-19 22:47:28 -070016#include <linux/mod_devicetable.h>
Alane0f682e02014-05-12 16:57:05 +010017#include <linux/goldfish.h>
Miodrag Dinic7157d2b2017-08-29 15:53:19 +020018#include <linux/mm.h>
19#include <linux/dma-mapping.h>
Miodrag Dinic3840ed92017-08-29 15:53:20 +020020#include <linux/serial_core.h>
Arve Hjønnevåg666b7792013-01-21 23:38:47 +000021
Aleksandar Markovic2296eee2017-08-29 15:53:18 +020022/* 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
Miodrag Dinic7157d2b2017-08-29 15:53:19 +020028#define GOLDFISH_TTY_REG_VERSION 0x20
Arve Hjønnevåg666b7792013-01-21 23:38:47 +000029
Aleksandar Markovic2296eee2017-08-29 15:53:18 +020030/* 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
Arve Hjønnevåg666b7792013-01-21 23:38:47 +000035
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;
Miodrag Dinic7157d2b2017-08-29 15:53:19 +020043 u32 version;
44 struct device *dev;
Arve Hjønnevåg666b7792013-01-21 23:38:47 +000045};
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
Miodrag Dinic7157d2b2017-08-29 15:53:19 +020053static void do_rw_io(struct goldfish_tty *qtty,
54 unsigned long address,
55 unsigned int count,
56 int is_write)
Arve Hjønnevåg666b7792013-01-21 23:38:47 +000057{
58 unsigned long irq_flags;
Arve Hjønnevåg666b7792013-01-21 23:38:47 +000059 void __iomem *base = qtty->base;
Miodrag Dinic7157d2b2017-08-29 15:53:19 +020060
Arve Hjønnevåg666b7792013-01-21 23:38:47 +000061 spin_lock_irqsave(&qtty->lock, irq_flags);
Miodrag Dinic7157d2b2017-08-29 15:53:19 +020062 gf_write_ptr((void *)address, base + GOLDFISH_TTY_REG_DATA_PTR,
Aleksandar Markovic2296eee2017-08-29 15:53:18 +020063 base + GOLDFISH_TTY_REG_DATA_PTR_HIGH);
Laurent Vivierda31de32020-10-10 02:47:49 +020064 __raw_writel(count, base + GOLDFISH_TTY_REG_DATA_LEN);
Miodrag Dinic7157d2b2017-08-29 15:53:19 +020065
66 if (is_write)
Laurent Vivierda31de32020-10-10 02:47:49 +020067 __raw_writel(GOLDFISH_TTY_CMD_WRITE_BUFFER,
Miodrag Dinic7157d2b2017-08-29 15:53:19 +020068 base + GOLDFISH_TTY_REG_CMD);
69 else
Laurent Vivierda31de32020-10-10 02:47:49 +020070 __raw_writel(GOLDFISH_TTY_CMD_READ_BUFFER,
Miodrag Dinic7157d2b2017-08-29 15:53:19 +020071 base + GOLDFISH_TTY_REG_CMD);
72
Arve Hjønnevåg666b7792013-01-21 23:38:47 +000073 spin_unlock_irqrestore(&qtty->lock, irq_flags);
74}
75
Miodrag Dinic7157d2b2017-08-29 15:53:19 +020076static 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
Arve Hjønnevåg666b7792013-01-21 23:38:47 +0000137static irqreturn_t goldfish_tty_interrupt(int irq, void *dev_id)
138{
Greg Hackmann465893e2016-02-26 19:01:05 +0000139 struct goldfish_tty *qtty = dev_id;
Arve Hjønnevåg666b7792013-01-21 23:38:47 +0000140 void __iomem *base = qtty->base;
Miodrag Dinic7157d2b2017-08-29 15:53:19 +0200141 unsigned long address;
Arve Hjønnevåg666b7792013-01-21 23:38:47 +0000142 unsigned char *buf;
143 u32 count;
Arve Hjønnevåg666b7792013-01-21 23:38:47 +0000144
Laurent Vivierda31de32020-10-10 02:47:49 +0200145 count = __raw_readl(base + GOLDFISH_TTY_REG_BYTES_READY);
Aland78055d2014-05-12 16:57:14 +0100146 if (count == 0)
Arve Hjønnevåg666b7792013-01-21 23:38:47 +0000147 return IRQ_NONE;
148
Alan Coxebcf0982013-01-25 15:05:30 +0000149 count = tty_prepare_flip_string(&qtty->port, &buf, count);
Miodrag Dinic7157d2b2017-08-29 15:53:19 +0200150
151 address = (unsigned long)(void *)buf;
152 goldfish_tty_rw(qtty, address, count, 0);
153
Jiri Slaby5f6a8512021-11-22 12:16:46 +0100154 tty_flip_buffer_push(&qtty->port);
Arve Hjønnevåg666b7792013-01-21 23:38:47 +0000155 return IRQ_HANDLED;
156}
157
158static int goldfish_tty_activate(struct tty_port *port, struct tty_struct *tty)
159{
Aland78055d2014-05-12 16:57:14 +0100160 struct goldfish_tty *qtty = container_of(port, struct goldfish_tty,
161 port);
Laurent Vivierda31de32020-10-10 02:47:49 +0200162 __raw_writel(GOLDFISH_TTY_CMD_INT_ENABLE, qtty->base + GOLDFISH_TTY_REG_CMD);
Arve Hjønnevåg666b7792013-01-21 23:38:47 +0000163 return 0;
164}
165
166static void goldfish_tty_shutdown(struct tty_port *port)
167{
Aland78055d2014-05-12 16:57:14 +0100168 struct goldfish_tty *qtty = container_of(port, struct goldfish_tty,
169 port);
Laurent Vivierda31de32020-10-10 02:47:49 +0200170 __raw_writel(GOLDFISH_TTY_CMD_INT_DISABLE, qtty->base + GOLDFISH_TTY_REG_CMD);
Arve Hjønnevåg666b7792013-01-21 23:38:47 +0000171}
172
Aland78055d2014-05-12 16:57:14 +0100173static int goldfish_tty_open(struct tty_struct *tty, struct file *filp)
Arve Hjønnevåg666b7792013-01-21 23:38:47 +0000174{
175 struct goldfish_tty *qtty = &goldfish_ttys[tty->index];
176 return tty_port_open(&qtty->port, tty, filp);
177}
178
Aland78055d2014-05-12 16:57:14 +0100179static void goldfish_tty_close(struct tty_struct *tty, struct file *filp)
Arve Hjønnevåg666b7792013-01-21 23:38:47 +0000180{
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
Aland78055d2014-05-12 16:57:14 +0100189static int goldfish_tty_write(struct tty_struct *tty, const unsigned char *buf,
190 int count)
Arve Hjønnevåg666b7792013-01-21 23:38:47 +0000191{
192 goldfish_tty_do_write(tty->index, buf, count);
193 return count;
194}
195
Jiri Slaby03b3b1a2021-05-05 11:19:15 +0200196static unsigned int goldfish_tty_write_room(struct tty_struct *tty)
Arve Hjønnevåg666b7792013-01-21 23:38:47 +0000197{
198 return 0x10000;
199}
200
Jiri Slabyfff4ef12021-05-05 11:19:19 +0200201static unsigned int goldfish_tty_chars_in_buffer(struct tty_struct *tty)
Arve Hjønnevåg666b7792013-01-21 23:38:47 +0000202{
203 struct goldfish_tty *qtty = &goldfish_ttys[tty->index];
204 void __iomem *base = qtty->base;
Laurent Vivierda31de32020-10-10 02:47:49 +0200205 return __raw_readl(base + GOLDFISH_TTY_REG_BYTES_READY);
Arve Hjønnevåg666b7792013-01-21 23:38:47 +0000206}
207
Aland78055d2014-05-12 16:57:14 +0100208static void goldfish_tty_console_write(struct console *co, const char *b,
209 unsigned count)
Arve Hjønnevåg666b7792013-01-21 23:38:47 +0000210{
211 goldfish_tty_do_write(co->index, b, count);
212}
213
Aland78055d2014-05-12 16:57:14 +0100214static struct tty_driver *goldfish_tty_console_device(struct console *c,
215 int *index)
Arve Hjønnevåg666b7792013-01-21 23:38:47 +0000216{
217 *index = c->index;
218 return goldfish_tty_driver;
219}
220
221static int goldfish_tty_console_setup(struct console *co, char *options)
222{
Dan Carpenterfda2b412014-10-29 11:43:25 +0300223 if ((unsigned)co->index >= goldfish_tty_line_count)
Arve Hjønnevåg666b7792013-01-21 23:38:47 +0000224 return -ENODEV;
Fabian Fredericka4dc9232014-09-28 20:10:17 +0200225 if (!goldfish_ttys[co->index].base)
Arve Hjønnevåg666b7792013-01-21 23:38:47 +0000226 return -ENODEV;
227 return 0;
228}
229
Aya Mahfouz04b757d2015-12-15 01:53:36 +0200230static const struct tty_port_operations goldfish_port_ops = {
Arve Hjønnevåg666b7792013-01-21 23:38:47 +0000231 .activate = goldfish_tty_activate,
232 .shutdown = goldfish_tty_shutdown
233};
234
Aland78055d2014-05-12 16:57:14 +0100235static const struct tty_operations goldfish_tty_ops = {
Arve Hjønnevåg666b7792013-01-21 23:38:47 +0000236 .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
Kees Cook6396bb22018-06-12 14:03:40 -0700249 goldfish_ttys = kcalloc(goldfish_tty_line_count,
250 sizeof(*goldfish_ttys),
251 GFP_KERNEL);
Aland78055d2014-05-12 16:57:14 +0100252 if (goldfish_ttys == NULL) {
Arve Hjønnevåg666b7792013-01-21 23:38:47 +0000253 ret = -ENOMEM;
254 goto err_alloc_goldfish_ttys_failed;
255 }
Jiri Slaby39b7b422021-07-23 09:43:13 +0200256 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;
Arve Hjønnevåg666b7792013-01-21 23:38:47 +0000262 }
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;
Arve Hjønnevåg666b7792013-01-21 23:38:47 +0000268 tty_set_operations(tty, &goldfish_tty_ops);
269 ret = tty_register_driver(tty);
Aland78055d2014-05-12 16:57:14 +0100270 if (ret)
Arve Hjønnevåg666b7792013-01-21 23:38:47 +0000271 goto err_tty_register_driver_failed;
272
273 goldfish_tty_driver = tty;
274 return 0;
275
276err_tty_register_driver_failed:
Jiri Slaby9f90a4d2021-07-23 09:43:16 +0200277 tty_driver_kref_put(tty);
Jiri Slaby39b7b422021-07-23 09:43:13 +0200278err_tty_alloc_driver_failed:
Arve Hjønnevåg666b7792013-01-21 23:38:47 +0000279 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);
Jiri Slaby9f90a4d2021-07-23 09:43:16 +0200288 tty_driver_kref_put(goldfish_tty_driver);
Arve Hjønnevåg666b7792013-01-21 23:38:47 +0000289 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;
Miodrag Dinic7157d2b2017-08-29 15:53:19 +0200297 int ret = -ENODEV;
Arve Hjønnevåg666b7792013-01-21 23:38:47 +0000298 struct resource *r;
299 struct device *ttydev;
300 void __iomem *base;
Lad Prabhakar5acb78d2021-12-24 15:37:53 +0000301 int irq;
Greg Hackmann465893e2016-02-26 19:01:05 +0000302 unsigned int line;
Arve Hjønnevåg666b7792013-01-21 23:38:47 +0000303
304 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Miodrag Dinic7157d2b2017-08-29 15:53:19 +0200305 if (!r) {
306 pr_err("goldfish_tty: No MEM resource available!\n");
307 return -ENOMEM;
308 }
Arve Hjønnevåg666b7792013-01-21 23:38:47 +0000309
310 base = ioremap(r->start, 0x1000);
Miodrag Dinic7157d2b2017-08-29 15:53:19 +0200311 if (!base) {
312 pr_err("goldfish_tty: Unable to ioremap base!\n");
313 return -ENOMEM;
314 }
Arve Hjønnevåg666b7792013-01-21 23:38:47 +0000315
Lad Prabhakar5acb78d2021-12-24 15:37:53 +0000316 irq = platform_get_irq(pdev, 0);
317 if (irq < 0) {
318 ret = irq;
Arve Hjønnevåg666b7792013-01-21 23:38:47 +0000319 goto err_unmap;
Miodrag Dinic7157d2b2017-08-29 15:53:19 +0200320 }
Arve Hjønnevåg666b7792013-01-21 23:38:47 +0000321
Arve Hjønnevåg666b7792013-01-21 23:38:47 +0000322 mutex_lock(&goldfish_tty_lock);
Greg Hackmann465893e2016-02-26 19:01:05 +0000323
324 if (pdev->id == PLATFORM_DEVID_NONE)
325 line = goldfish_tty_current_line_count;
326 else
327 line = pdev->id;
328
Miodrag Dinic7157d2b2017-08-29 15:53:19 +0200329 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 }
Greg Hackmann465893e2016-02-26 19:01:05 +0000335
Aland78055d2014-05-12 16:57:14 +0100336 if (goldfish_tty_current_line_count == 0) {
Arve Hjønnevåg666b7792013-01-21 23:38:47 +0000337 ret = goldfish_tty_create_driver();
Aland78055d2014-05-12 16:57:14 +0100338 if (ret)
Miodrag Dinic7157d2b2017-08-29 15:53:19 +0200339 goto err_unlock;
Arve Hjønnevåg666b7792013-01-21 23:38:47 +0000340 }
341 goldfish_tty_current_line_count++;
342
Greg Hackmann465893e2016-02-26 19:01:05 +0000343 qtty = &goldfish_ttys[line];
Arve Hjønnevåg666b7792013-01-21 23:38:47 +0000344 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;
Miodrag Dinic7157d2b2017-08-29 15:53:19 +0200349 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 */
Laurent Vivierda31de32020-10-10 02:47:49 +0200358 qtty->version = __raw_readl(base + GOLDFISH_TTY_REG_VERSION);
Miodrag Dinic7157d2b2017-08-29 15:53:19 +0200359
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 }
Arve Hjønnevåg666b7792013-01-21 23:38:47 +0000376
Laurent Vivierda31de32020-10-10 02:47:49 +0200377 __raw_writel(GOLDFISH_TTY_CMD_INT_DISABLE, base + GOLDFISH_TTY_REG_CMD);
Arve Hjønnevåg666b7792013-01-21 23:38:47 +0000378
Aland78055d2014-05-12 16:57:14 +0100379 ret = request_irq(irq, goldfish_tty_interrupt, IRQF_SHARED,
Miodrag Dinic7157d2b2017-08-29 15:53:19 +0200380 "goldfish_tty", qtty);
381 if (ret) {
382 pr_err("goldfish_tty: No IRQ available!\n");
383 goto err_dec_line_count;
384 }
Arve Hjønnevåg666b7792013-01-21 23:38:47 +0000385
386 ttydev = tty_port_register_device(&qtty->port, goldfish_tty_driver,
Miodrag Dinic7157d2b2017-08-29 15:53:19 +0200387 line, &pdev->dev);
Aland78055d2014-05-12 16:57:14 +0100388 if (IS_ERR(ttydev)) {
Arve Hjønnevåg666b7792013-01-21 23:38:47 +0000389 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;
Greg Hackmann465893e2016-02-26 19:01:05 +0000398 qtty->console.index = line;
Arve Hjønnevåg666b7792013-01-21 23:38:47 +0000399 register_console(&qtty->console);
Greg Hackmann465893e2016-02-26 19:01:05 +0000400 platform_set_drvdata(pdev, qtty);
Arve Hjønnevåg666b7792013-01-21 23:38:47 +0000401
402 mutex_unlock(&goldfish_tty_lock);
403 return 0;
404
Arve Hjønnevåg666b7792013-01-21 23:38:47 +0000405err_tty_register_device_failed:
Christophe JAILLET1a5c2d12017-01-09 01:26:37 +0100406 free_irq(irq, qtty);
Miodrag Dinic7157d2b2017-08-29 15:53:19 +0200407err_dec_line_count:
Arve Hjønnevåg666b7792013-01-21 23:38:47 +0000408 goldfish_tty_current_line_count--;
Aland78055d2014-05-12 16:57:14 +0100409 if (goldfish_tty_current_line_count == 0)
Arve Hjønnevåg666b7792013-01-21 23:38:47 +0000410 goldfish_tty_delete_driver();
Miodrag Dinic7157d2b2017-08-29 15:53:19 +0200411err_unlock:
Arve Hjønnevåg666b7792013-01-21 23:38:47 +0000412 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{
Greg Hackmann465893e2016-02-26 19:01:05 +0000420 struct goldfish_tty *qtty = platform_get_drvdata(pdev);
Arve Hjønnevåg666b7792013-01-21 23:38:47 +0000421
422 mutex_lock(&goldfish_tty_lock);
423
Arve Hjønnevåg666b7792013-01-21 23:38:47 +0000424 unregister_console(&qtty->console);
Greg Hackmann465893e2016-02-26 19:01:05 +0000425 tty_unregister_device(goldfish_tty_driver, qtty->console.index);
Arve Hjønnevåg666b7792013-01-21 23:38:47 +0000426 iounmap(qtty->base);
Fabian Fredericka4dc9232014-09-28 20:10:17 +0200427 qtty->base = NULL;
Arve Hjønnevåg666b7792013-01-21 23:38:47 +0000428 free_irq(qtty->irq, pdev);
429 goldfish_tty_current_line_count--;
Aland78055d2014-05-12 16:57:14 +0100430 if (goldfish_tty_current_line_count == 0)
Arve Hjønnevåg666b7792013-01-21 23:38:47 +0000431 goldfish_tty_delete_driver();
432 mutex_unlock(&goldfish_tty_lock);
433 return 0;
434}
435
Sebastian Andrzej Siewior6a28fd22017-11-30 09:16:31 +0100436#ifdef CONFIG_GOLDFISH_TTY_EARLY_CONSOLE
Miodrag Dinic3840ed92017-08-29 15:53:20 +0200437static 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);
Sebastian Andrzej Siewior6a28fd22017-11-30 09:16:31 +0100460#endif
Miodrag Dinic3840ed92017-08-29 15:53:20 +0200461
Miodrag Dinic9b883ee2016-02-26 19:00:44 +0000462static 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
Arve Hjønnevåg666b7792013-01-21 23:38:47 +0000469static struct platform_driver goldfish_tty_platform_driver = {
470 .probe = goldfish_tty_probe,
471 .remove = goldfish_tty_remove,
472 .driver = {
Miodrag Dinic9b883ee2016-02-26 19:00:44 +0000473 .name = "goldfish_tty",
474 .of_match_table = goldfish_tty_of_match,
Arve Hjønnevåg666b7792013-01-21 23:38:47 +0000475 }
476};
477
478module_platform_driver(goldfish_tty_platform_driver);
479
480MODULE_LICENSE("GPL v2");