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