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