xref: /linux/drivers/tty/goldfish.c (revision 69050f8d6d075dc01af7a5f2f550a8067510366f)
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/mod_devicetable.h>
17 #include <linux/goldfish.h>
18 #include <linux/mm.h>
19 #include <linux/dma-mapping.h>
20 #include <linux/serial_core.h>
21 
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
28 #define	GOLDFISH_TTY_REG_VERSION	0x20
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
35 
36 struct 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;
43 	u32 version;
44 	struct device *dev;
45 };
46 
47 static DEFINE_MUTEX(goldfish_tty_lock);
48 static struct tty_driver *goldfish_tty_driver;
49 static u32 goldfish_tty_line_count = 8;
50 static u32 goldfish_tty_current_line_count;
51 static struct goldfish_tty *goldfish_ttys;
52 
53 static void do_rw_io(struct goldfish_tty *qtty, unsigned long address,
54 		     size_t count, bool is_write)
55 {
56 	unsigned long irq_flags;
57 	void __iomem *base = qtty->base;
58 
59 	spin_lock_irqsave(&qtty->lock, irq_flags);
60 	gf_write_ptr((void *)address, base + GOLDFISH_TTY_REG_DATA_PTR,
61 		     base + GOLDFISH_TTY_REG_DATA_PTR_HIGH);
62 	gf_iowrite32(count, base + GOLDFISH_TTY_REG_DATA_LEN);
63 
64 	if (is_write)
65 		gf_iowrite32(GOLDFISH_TTY_CMD_WRITE_BUFFER,
66 		       base + GOLDFISH_TTY_REG_CMD);
67 	else
68 		gf_iowrite32(GOLDFISH_TTY_CMD_READ_BUFFER,
69 		       base + GOLDFISH_TTY_REG_CMD);
70 
71 	spin_unlock_irqrestore(&qtty->lock, irq_flags);
72 }
73 
74 static void goldfish_tty_rw(struct goldfish_tty *qtty, unsigned long addr,
75 			    size_t count, bool is_write)
76 {
77 	dma_addr_t dma_handle;
78 	enum dma_data_direction dma_dir;
79 
80 	dma_dir = (is_write ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
81 	if (qtty->version > 0) {
82 		/*
83 		 * Goldfish TTY for Ranchu platform uses
84 		 * physical addresses and DMA for read/write operations
85 		 */
86 		unsigned long addr_end = addr + count;
87 
88 		while (addr < addr_end) {
89 			unsigned long pg_end = (addr & PAGE_MASK) + PAGE_SIZE;
90 			unsigned long next =
91 					pg_end < addr_end ? pg_end : addr_end;
92 			unsigned long avail = next - addr;
93 
94 			/*
95 			 * Map the buffer's virtual address to the DMA address
96 			 * so the buffer can be accessed by the device.
97 			 */
98 			dma_handle = dma_map_single(qtty->dev, (void *)addr,
99 						    avail, dma_dir);
100 
101 			if (dma_mapping_error(qtty->dev, dma_handle)) {
102 				dev_err(qtty->dev, "tty: DMA mapping error.\n");
103 				return;
104 			}
105 			do_rw_io(qtty, dma_handle, avail, is_write);
106 
107 			/*
108 			 * Unmap the previously mapped region after
109 			 * the completion of the read/write operation.
110 			 */
111 			dma_unmap_single(qtty->dev, dma_handle, avail, dma_dir);
112 
113 			addr += avail;
114 		}
115 	} else {
116 		/*
117 		 * Old style Goldfish TTY used on the Goldfish platform
118 		 * uses virtual addresses.
119 		 */
120 		do_rw_io(qtty, addr, count, is_write);
121 	}
122 }
123 
124 static void goldfish_tty_do_write(int line, const u8 *buf, size_t count)
125 {
126 	struct goldfish_tty *qtty = &goldfish_ttys[line];
127 
128 	goldfish_tty_rw(qtty, (unsigned long)buf, count, true);
129 }
130 
131 static irqreturn_t goldfish_tty_interrupt(int irq, void *dev_id)
132 {
133 	struct goldfish_tty *qtty = dev_id;
134 	void __iomem *base = qtty->base;
135 	u8 *buf;
136 	u32 count;
137 
138 	count = gf_ioread32(base + GOLDFISH_TTY_REG_BYTES_READY);
139 	if (count == 0)
140 		return IRQ_NONE;
141 
142 	count = tty_prepare_flip_string(&qtty->port, &buf, count);
143 
144 	goldfish_tty_rw(qtty, (unsigned long)buf, count, false);
145 
146 	tty_flip_buffer_push(&qtty->port);
147 	return IRQ_HANDLED;
148 }
149 
150 static int goldfish_tty_activate(struct tty_port *port, struct tty_struct *tty)
151 {
152 	struct goldfish_tty *qtty = container_of(port, struct goldfish_tty,
153 									port);
154 	gf_iowrite32(GOLDFISH_TTY_CMD_INT_ENABLE, qtty->base + GOLDFISH_TTY_REG_CMD);
155 	return 0;
156 }
157 
158 static void goldfish_tty_shutdown(struct tty_port *port)
159 {
160 	struct goldfish_tty *qtty = container_of(port, struct goldfish_tty,
161 									port);
162 	gf_iowrite32(GOLDFISH_TTY_CMD_INT_DISABLE, qtty->base + GOLDFISH_TTY_REG_CMD);
163 }
164 
165 static int goldfish_tty_open(struct tty_struct *tty, struct file *filp)
166 {
167 	struct goldfish_tty *qtty = &goldfish_ttys[tty->index];
168 	return tty_port_open(&qtty->port, tty, filp);
169 }
170 
171 static void goldfish_tty_close(struct tty_struct *tty, struct file *filp)
172 {
173 	tty_port_close(tty->port, tty, filp);
174 }
175 
176 static void goldfish_tty_hangup(struct tty_struct *tty)
177 {
178 	tty_port_hangup(tty->port);
179 }
180 
181 static ssize_t goldfish_tty_write(struct tty_struct *tty, const u8 *buf,
182 				  size_t count)
183 {
184 	goldfish_tty_do_write(tty->index, buf, count);
185 	return count;
186 }
187 
188 static unsigned int goldfish_tty_write_room(struct tty_struct *tty)
189 {
190 	return 0x10000;
191 }
192 
193 static unsigned int goldfish_tty_chars_in_buffer(struct tty_struct *tty)
194 {
195 	struct goldfish_tty *qtty = &goldfish_ttys[tty->index];
196 	void __iomem *base = qtty->base;
197 	return gf_ioread32(base + GOLDFISH_TTY_REG_BYTES_READY);
198 }
199 
200 static void goldfish_tty_console_write(struct console *co, const char *b,
201 								unsigned count)
202 {
203 	goldfish_tty_do_write(co->index, b, count);
204 }
205 
206 static struct tty_driver *goldfish_tty_console_device(struct console *c,
207 								int *index)
208 {
209 	*index = c->index;
210 	return goldfish_tty_driver;
211 }
212 
213 static int goldfish_tty_console_setup(struct console *co, char *options)
214 {
215 	if ((unsigned)co->index >= goldfish_tty_line_count)
216 		return -ENODEV;
217 	if (!goldfish_ttys[co->index].base)
218 		return -ENODEV;
219 	return 0;
220 }
221 
222 static const struct tty_port_operations goldfish_port_ops = {
223 	.activate = goldfish_tty_activate,
224 	.shutdown = goldfish_tty_shutdown
225 };
226 
227 static const struct tty_operations goldfish_tty_ops = {
228 	.open = goldfish_tty_open,
229 	.close = goldfish_tty_close,
230 	.hangup = goldfish_tty_hangup,
231 	.write = goldfish_tty_write,
232 	.write_room = goldfish_tty_write_room,
233 	.chars_in_buffer = goldfish_tty_chars_in_buffer,
234 };
235 
236 static int goldfish_tty_create_driver(void)
237 {
238 	int ret;
239 	struct tty_driver *tty;
240 
241 	goldfish_ttys = kzalloc_objs(*goldfish_ttys, goldfish_tty_line_count,
242 				     GFP_KERNEL);
243 	if (goldfish_ttys == NULL) {
244 		ret = -ENOMEM;
245 		goto err_alloc_goldfish_ttys_failed;
246 	}
247 	tty = tty_alloc_driver(goldfish_tty_line_count,
248 			TTY_DRIVER_RESET_TERMIOS | TTY_DRIVER_REAL_RAW |
249 			TTY_DRIVER_DYNAMIC_DEV);
250 	if (IS_ERR(tty)) {
251 		ret = PTR_ERR(tty);
252 		goto err_tty_alloc_driver_failed;
253 	}
254 	tty->driver_name = "goldfish";
255 	tty->name = "ttyGF";
256 	tty->type = TTY_DRIVER_TYPE_SERIAL;
257 	tty->subtype = SERIAL_TYPE_NORMAL;
258 	tty->init_termios = tty_std_termios;
259 	tty_set_operations(tty, &goldfish_tty_ops);
260 	ret = tty_register_driver(tty);
261 	if (ret)
262 		goto err_tty_register_driver_failed;
263 
264 	goldfish_tty_driver = tty;
265 	return 0;
266 
267 err_tty_register_driver_failed:
268 	tty_driver_kref_put(tty);
269 err_tty_alloc_driver_failed:
270 	kfree(goldfish_ttys);
271 	goldfish_ttys = NULL;
272 err_alloc_goldfish_ttys_failed:
273 	return ret;
274 }
275 
276 static void goldfish_tty_delete_driver(void)
277 {
278 	tty_unregister_driver(goldfish_tty_driver);
279 	tty_driver_kref_put(goldfish_tty_driver);
280 	goldfish_tty_driver = NULL;
281 	kfree(goldfish_ttys);
282 	goldfish_ttys = NULL;
283 }
284 
285 static int goldfish_tty_probe(struct platform_device *pdev)
286 {
287 	struct goldfish_tty *qtty;
288 	int ret = -ENODEV;
289 	struct resource *r;
290 	struct device *ttydev;
291 	void __iomem *base;
292 	int irq;
293 	unsigned int line;
294 
295 	r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
296 	if (!r) {
297 		pr_err("goldfish_tty: No MEM resource available!\n");
298 		return -ENOMEM;
299 	}
300 
301 	base = ioremap(r->start, 0x1000);
302 	if (!base) {
303 		pr_err("goldfish_tty: Unable to ioremap base!\n");
304 		return -ENOMEM;
305 	}
306 
307 	irq = platform_get_irq(pdev, 0);
308 	if (irq < 0) {
309 		ret = irq;
310 		goto err_unmap;
311 	}
312 
313 	mutex_lock(&goldfish_tty_lock);
314 
315 	if (pdev->id == PLATFORM_DEVID_NONE)
316 		line = goldfish_tty_current_line_count;
317 	else
318 		line = pdev->id;
319 
320 	if (line >= goldfish_tty_line_count) {
321 		pr_err("goldfish_tty: Reached maximum tty number of %d.\n",
322 		       goldfish_tty_current_line_count);
323 		ret = -ENOMEM;
324 		goto err_unlock;
325 	}
326 
327 	if (goldfish_tty_current_line_count == 0) {
328 		ret = goldfish_tty_create_driver();
329 		if (ret)
330 			goto err_unlock;
331 	}
332 	goldfish_tty_current_line_count++;
333 
334 	qtty = &goldfish_ttys[line];
335 	spin_lock_init(&qtty->lock);
336 	tty_port_init(&qtty->port);
337 	qtty->port.ops = &goldfish_port_ops;
338 	qtty->base = base;
339 	qtty->irq = irq;
340 	qtty->dev = &pdev->dev;
341 
342 	/*
343 	 * Goldfish TTY device used by the Goldfish emulator
344 	 * should identify itself with 0, forcing the driver
345 	 * to use virtual addresses. Goldfish TTY device
346 	 * on Ranchu emulator (qemu2) returns 1 here and
347 	 * driver will use physical addresses.
348 	 */
349 	qtty->version = gf_ioread32(base + GOLDFISH_TTY_REG_VERSION);
350 
351 	/*
352 	 * Goldfish TTY device on Ranchu emulator (qemu2)
353 	 * will use DMA for read/write IO operations.
354 	 */
355 	if (qtty->version > 0) {
356 		/*
357 		 * Initialize dma_mask to 32-bits.
358 		 */
359 		if (!pdev->dev.dma_mask)
360 			pdev->dev.dma_mask = &pdev->dev.coherent_dma_mask;
361 		ret = dma_set_mask(&pdev->dev, DMA_BIT_MASK(32));
362 		if (ret) {
363 			dev_err(&pdev->dev, "No suitable DMA available.\n");
364 			goto err_dec_line_count;
365 		}
366 	}
367 
368 	gf_iowrite32(GOLDFISH_TTY_CMD_INT_DISABLE, base + GOLDFISH_TTY_REG_CMD);
369 
370 	ret = request_irq(irq, goldfish_tty_interrupt, IRQF_SHARED,
371 			  "goldfish_tty", qtty);
372 	if (ret) {
373 		pr_err("goldfish_tty: No IRQ available!\n");
374 		goto err_dec_line_count;
375 	}
376 
377 	ttydev = tty_port_register_device(&qtty->port, goldfish_tty_driver,
378 					  line, &pdev->dev);
379 	if (IS_ERR(ttydev)) {
380 		ret = PTR_ERR(ttydev);
381 		goto err_tty_register_device_failed;
382 	}
383 
384 	strcpy(qtty->console.name, "ttyGF");
385 	qtty->console.write = goldfish_tty_console_write;
386 	qtty->console.device = goldfish_tty_console_device;
387 	qtty->console.setup = goldfish_tty_console_setup;
388 	qtty->console.flags = CON_PRINTBUFFER;
389 	qtty->console.index = line;
390 	register_console(&qtty->console);
391 	platform_set_drvdata(pdev, qtty);
392 
393 	mutex_unlock(&goldfish_tty_lock);
394 	return 0;
395 
396 err_tty_register_device_failed:
397 	free_irq(irq, qtty);
398 err_dec_line_count:
399 	tty_port_destroy(&qtty->port);
400 	goldfish_tty_current_line_count--;
401 	if (goldfish_tty_current_line_count == 0)
402 		goldfish_tty_delete_driver();
403 err_unlock:
404 	mutex_unlock(&goldfish_tty_lock);
405 err_unmap:
406 	iounmap(base);
407 	return ret;
408 }
409 
410 static void goldfish_tty_remove(struct platform_device *pdev)
411 {
412 	struct goldfish_tty *qtty = platform_get_drvdata(pdev);
413 
414 	mutex_lock(&goldfish_tty_lock);
415 
416 	unregister_console(&qtty->console);
417 	tty_unregister_device(goldfish_tty_driver, qtty->console.index);
418 	iounmap(qtty->base);
419 	qtty->base = NULL;
420 	free_irq(qtty->irq, qtty);
421 	tty_port_destroy(&qtty->port);
422 	goldfish_tty_current_line_count--;
423 	if (goldfish_tty_current_line_count == 0)
424 		goldfish_tty_delete_driver();
425 	mutex_unlock(&goldfish_tty_lock);
426 }
427 
428 #ifdef CONFIG_GOLDFISH_TTY_EARLY_CONSOLE
429 static void gf_early_console_putchar(struct uart_port *port, unsigned char ch)
430 {
431 	gf_iowrite32(ch, port->membase);
432 }
433 
434 static void gf_early_write(struct console *con, const char *s, unsigned int n)
435 {
436 	struct earlycon_device *dev = con->data;
437 
438 	uart_console_write(&dev->port, s, n, gf_early_console_putchar);
439 }
440 
441 static int __init gf_earlycon_setup(struct earlycon_device *device,
442 				    const char *opt)
443 {
444 	if (!device->port.membase)
445 		return -ENODEV;
446 
447 	device->con->write = gf_early_write;
448 	return 0;
449 }
450 
451 OF_EARLYCON_DECLARE(early_gf_tty, "google,goldfish-tty", gf_earlycon_setup);
452 #endif
453 
454 static const struct of_device_id goldfish_tty_of_match[] = {
455 	{ .compatible = "google,goldfish-tty", },
456 	{},
457 };
458 
459 MODULE_DEVICE_TABLE(of, goldfish_tty_of_match);
460 
461 static struct platform_driver goldfish_tty_platform_driver = {
462 	.probe = goldfish_tty_probe,
463 	.remove = goldfish_tty_remove,
464 	.driver = {
465 		.name = "goldfish_tty",
466 		.of_match_table = goldfish_tty_of_match,
467 	}
468 };
469 
470 module_platform_driver(goldfish_tty_platform_driver);
471 
472 MODULE_DESCRIPTION("Goldfish TTY Driver");
473 MODULE_LICENSE("GPL v2");
474