xref: /linux/drivers/tty/goldfish.c (revision f0a17485cc2c6984381bac5c10e3564675dcbea5)
1e3b3d0f5SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
2666b7793SArve Hjønnevåg /*
3666b7793SArve Hjønnevåg  * Copyright (C) 2007 Google, Inc.
4666b7793SArve Hjønnevåg  * Copyright (C) 2012 Intel, Inc.
53840ed95SMiodrag Dinic  * Copyright (C) 2017 Imagination Technologies Ltd.
6666b7793SArve Hjønnevåg  */
7666b7793SArve Hjønnevåg 
8666b7793SArve Hjønnevåg #include <linux/console.h>
9666b7793SArve Hjønnevåg #include <linux/interrupt.h>
10666b7793SArve Hjønnevåg #include <linux/platform_device.h>
11666b7793SArve Hjønnevåg #include <linux/tty.h>
12666b7793SArve Hjønnevåg #include <linux/tty_flip.h>
13666b7793SArve Hjønnevåg #include <linux/slab.h>
14666b7793SArve Hjønnevåg #include <linux/io.h>
15666b7793SArve Hjønnevåg #include <linux/module.h>
16ac316725SRandy Dunlap #include <linux/mod_devicetable.h>
17e0f682e0SAlan #include <linux/goldfish.h>
187157d2beSMiodrag Dinic #include <linux/mm.h>
197157d2beSMiodrag Dinic #include <linux/dma-mapping.h>
203840ed95SMiodrag Dinic #include <linux/serial_core.h>
21666b7793SArve Hjønnevåg 
222296eee7SAleksandar Markovic /* Goldfish tty register's offsets */
232296eee7SAleksandar Markovic #define	GOLDFISH_TTY_REG_BYTES_READY	0x04
242296eee7SAleksandar Markovic #define	GOLDFISH_TTY_REG_CMD		0x08
252296eee7SAleksandar Markovic #define	GOLDFISH_TTY_REG_DATA_PTR	0x10
262296eee7SAleksandar Markovic #define	GOLDFISH_TTY_REG_DATA_LEN	0x14
272296eee7SAleksandar Markovic #define	GOLDFISH_TTY_REG_DATA_PTR_HIGH	0x18
287157d2beSMiodrag Dinic #define	GOLDFISH_TTY_REG_VERSION	0x20
29666b7793SArve Hjønnevåg 
302296eee7SAleksandar Markovic /* Goldfish tty commands */
312296eee7SAleksandar Markovic #define	GOLDFISH_TTY_CMD_INT_DISABLE	0
322296eee7SAleksandar Markovic #define	GOLDFISH_TTY_CMD_INT_ENABLE	1
332296eee7SAleksandar Markovic #define	GOLDFISH_TTY_CMD_WRITE_BUFFER	2
342296eee7SAleksandar Markovic #define	GOLDFISH_TTY_CMD_READ_BUFFER	3
35666b7793SArve Hjønnevåg 
36666b7793SArve Hjønnevåg struct goldfish_tty {
37666b7793SArve Hjønnevåg 	struct tty_port port;
38666b7793SArve Hjønnevåg 	spinlock_t lock;
39666b7793SArve Hjønnevåg 	void __iomem *base;
40666b7793SArve Hjønnevåg 	u32 irq;
41666b7793SArve Hjønnevåg 	int opencount;
42666b7793SArve Hjønnevåg 	struct console console;
437157d2beSMiodrag Dinic 	u32 version;
447157d2beSMiodrag Dinic 	struct device *dev;
45666b7793SArve Hjønnevåg };
46666b7793SArve Hjønnevåg 
47666b7793SArve Hjønnevåg static DEFINE_MUTEX(goldfish_tty_lock);
48666b7793SArve Hjønnevåg static struct tty_driver *goldfish_tty_driver;
49666b7793SArve Hjønnevåg static u32 goldfish_tty_line_count = 8;
50666b7793SArve Hjønnevåg static u32 goldfish_tty_current_line_count;
51666b7793SArve Hjønnevåg static struct goldfish_tty *goldfish_ttys;
52666b7793SArve Hjønnevåg 
53f3fb7367SJiri Slaby (SUSE) static void do_rw_io(struct goldfish_tty *qtty, unsigned long address,
54f3fb7367SJiri Slaby (SUSE) 		     size_t count, bool is_write)
55666b7793SArve Hjønnevåg {
56666b7793SArve Hjønnevåg 	unsigned long irq_flags;
57666b7793SArve Hjønnevåg 	void __iomem *base = qtty->base;
587157d2beSMiodrag Dinic 
59666b7793SArve Hjønnevåg 	spin_lock_irqsave(&qtty->lock, irq_flags);
607157d2beSMiodrag Dinic 	gf_write_ptr((void *)address, base + GOLDFISH_TTY_REG_DATA_PTR,
612296eee7SAleksandar Markovic 		     base + GOLDFISH_TTY_REG_DATA_PTR_HIGH);
622e2ac4a3SLaurent Vivier 	gf_iowrite32(count, base + GOLDFISH_TTY_REG_DATA_LEN);
637157d2beSMiodrag Dinic 
647157d2beSMiodrag Dinic 	if (is_write)
652e2ac4a3SLaurent Vivier 		gf_iowrite32(GOLDFISH_TTY_CMD_WRITE_BUFFER,
667157d2beSMiodrag Dinic 		       base + GOLDFISH_TTY_REG_CMD);
677157d2beSMiodrag Dinic 	else
682e2ac4a3SLaurent Vivier 		gf_iowrite32(GOLDFISH_TTY_CMD_READ_BUFFER,
697157d2beSMiodrag Dinic 		       base + GOLDFISH_TTY_REG_CMD);
707157d2beSMiodrag Dinic 
71666b7793SArve Hjønnevåg 	spin_unlock_irqrestore(&qtty->lock, irq_flags);
72666b7793SArve Hjønnevåg }
73666b7793SArve Hjønnevåg 
74f3fb7367SJiri Slaby (SUSE) static void goldfish_tty_rw(struct goldfish_tty *qtty, unsigned long addr,
75f3fb7367SJiri Slaby (SUSE) 			    size_t count, bool is_write)
767157d2beSMiodrag Dinic {
777157d2beSMiodrag Dinic 	dma_addr_t dma_handle;
787157d2beSMiodrag Dinic 	enum dma_data_direction dma_dir;
797157d2beSMiodrag Dinic 
807157d2beSMiodrag Dinic 	dma_dir = (is_write ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
817157d2beSMiodrag Dinic 	if (qtty->version > 0) {
827157d2beSMiodrag Dinic 		/*
837157d2beSMiodrag Dinic 		 * Goldfish TTY for Ranchu platform uses
847157d2beSMiodrag Dinic 		 * physical addresses and DMA for read/write operations
857157d2beSMiodrag Dinic 		 */
867157d2beSMiodrag Dinic 		unsigned long addr_end = addr + count;
877157d2beSMiodrag Dinic 
887157d2beSMiodrag Dinic 		while (addr < addr_end) {
897157d2beSMiodrag Dinic 			unsigned long pg_end = (addr & PAGE_MASK) + PAGE_SIZE;
907157d2beSMiodrag Dinic 			unsigned long next =
917157d2beSMiodrag Dinic 					pg_end < addr_end ? pg_end : addr_end;
927157d2beSMiodrag Dinic 			unsigned long avail = next - addr;
937157d2beSMiodrag Dinic 
947157d2beSMiodrag Dinic 			/*
957157d2beSMiodrag Dinic 			 * Map the buffer's virtual address to the DMA address
967157d2beSMiodrag Dinic 			 * so the buffer can be accessed by the device.
977157d2beSMiodrag Dinic 			 */
987157d2beSMiodrag Dinic 			dma_handle = dma_map_single(qtty->dev, (void *)addr,
997157d2beSMiodrag Dinic 						    avail, dma_dir);
1007157d2beSMiodrag Dinic 
1017157d2beSMiodrag Dinic 			if (dma_mapping_error(qtty->dev, dma_handle)) {
1027157d2beSMiodrag Dinic 				dev_err(qtty->dev, "tty: DMA mapping error.\n");
1037157d2beSMiodrag Dinic 				return;
1047157d2beSMiodrag Dinic 			}
1057157d2beSMiodrag Dinic 			do_rw_io(qtty, dma_handle, avail, is_write);
1067157d2beSMiodrag Dinic 
1077157d2beSMiodrag Dinic 			/*
1087157d2beSMiodrag Dinic 			 * Unmap the previously mapped region after
1097157d2beSMiodrag Dinic 			 * the completion of the read/write operation.
1107157d2beSMiodrag Dinic 			 */
1117157d2beSMiodrag Dinic 			dma_unmap_single(qtty->dev, dma_handle, avail, dma_dir);
1127157d2beSMiodrag Dinic 
1137157d2beSMiodrag Dinic 			addr += avail;
1147157d2beSMiodrag Dinic 		}
1157157d2beSMiodrag Dinic 	} else {
1167157d2beSMiodrag Dinic 		/*
1177157d2beSMiodrag Dinic 		 * Old style Goldfish TTY used on the Goldfish platform
1187157d2beSMiodrag Dinic 		 * uses virtual addresses.
1197157d2beSMiodrag Dinic 		 */
1207157d2beSMiodrag Dinic 		do_rw_io(qtty, addr, count, is_write);
1217157d2beSMiodrag Dinic 	}
1227157d2beSMiodrag Dinic }
1237157d2beSMiodrag Dinic 
124f3fb7367SJiri Slaby (SUSE) static void goldfish_tty_do_write(int line, const u8 *buf, size_t count)
1257157d2beSMiodrag Dinic {
1267157d2beSMiodrag Dinic 	struct goldfish_tty *qtty = &goldfish_ttys[line];
1277157d2beSMiodrag Dinic 
128e01b5712SJiri Slaby (SUSE) 	goldfish_tty_rw(qtty, (unsigned long)buf, count, true);
1297157d2beSMiodrag Dinic }
1307157d2beSMiodrag Dinic 
131666b7793SArve Hjønnevåg static irqreturn_t goldfish_tty_interrupt(int irq, void *dev_id)
132666b7793SArve Hjønnevåg {
133465893e1SGreg Hackmann 	struct goldfish_tty *qtty = dev_id;
134666b7793SArve Hjønnevåg 	void __iomem *base = qtty->base;
135f3fb7367SJiri Slaby (SUSE) 	u8 *buf;
136666b7793SArve Hjønnevåg 	u32 count;
137666b7793SArve Hjønnevåg 
1382e2ac4a3SLaurent Vivier 	count = gf_ioread32(base + GOLDFISH_TTY_REG_BYTES_READY);
139666b7793SArve Hjønnevåg 	if (count == 0)
140666b7793SArve Hjønnevåg 		return IRQ_NONE;
141666b7793SArve Hjønnevåg 
142ebcf0981SAlan Cox 	count = tty_prepare_flip_string(&qtty->port, &buf, count);
1437157d2beSMiodrag Dinic 
144e01b5712SJiri Slaby (SUSE) 	goldfish_tty_rw(qtty, (unsigned long)buf, count, false);
1457157d2beSMiodrag Dinic 
1465f6a8515SJiri Slaby 	tty_flip_buffer_push(&qtty->port);
147666b7793SArve Hjønnevåg 	return IRQ_HANDLED;
148666b7793SArve Hjønnevåg }
149666b7793SArve Hjønnevåg 
150666b7793SArve Hjønnevåg static int goldfish_tty_activate(struct tty_port *port, struct tty_struct *tty)
151666b7793SArve Hjønnevåg {
152d78055dcSAlan 	struct goldfish_tty *qtty = container_of(port, struct goldfish_tty,
153d78055dcSAlan 									port);
1542e2ac4a3SLaurent Vivier 	gf_iowrite32(GOLDFISH_TTY_CMD_INT_ENABLE, qtty->base + GOLDFISH_TTY_REG_CMD);
155666b7793SArve Hjønnevåg 	return 0;
156666b7793SArve Hjønnevåg }
157666b7793SArve Hjønnevåg 
158666b7793SArve Hjønnevåg static void goldfish_tty_shutdown(struct tty_port *port)
159666b7793SArve Hjønnevåg {
160d78055dcSAlan 	struct goldfish_tty *qtty = container_of(port, struct goldfish_tty,
161d78055dcSAlan 									port);
1622e2ac4a3SLaurent Vivier 	gf_iowrite32(GOLDFISH_TTY_CMD_INT_DISABLE, qtty->base + GOLDFISH_TTY_REG_CMD);
163666b7793SArve Hjønnevåg }
164666b7793SArve Hjønnevåg 
165666b7793SArve Hjønnevåg static int goldfish_tty_open(struct tty_struct *tty, struct file *filp)
166666b7793SArve Hjønnevåg {
167666b7793SArve Hjønnevåg 	struct goldfish_tty *qtty = &goldfish_ttys[tty->index];
168666b7793SArve Hjønnevåg 	return tty_port_open(&qtty->port, tty, filp);
169666b7793SArve Hjønnevåg }
170666b7793SArve Hjønnevåg 
171666b7793SArve Hjønnevåg static void goldfish_tty_close(struct tty_struct *tty, struct file *filp)
172666b7793SArve Hjønnevåg {
173666b7793SArve Hjønnevåg 	tty_port_close(tty->port, tty, filp);
174666b7793SArve Hjønnevåg }
175666b7793SArve Hjønnevåg 
176666b7793SArve Hjønnevåg static void goldfish_tty_hangup(struct tty_struct *tty)
177666b7793SArve Hjønnevåg {
178666b7793SArve Hjønnevåg 	tty_port_hangup(tty->port);
179666b7793SArve Hjønnevåg }
180666b7793SArve Hjønnevåg 
18195713967SJiri Slaby (SUSE) static ssize_t goldfish_tty_write(struct tty_struct *tty, const u8 *buf,
18295713967SJiri Slaby (SUSE) 				  size_t count)
183666b7793SArve Hjønnevåg {
184666b7793SArve Hjønnevåg 	goldfish_tty_do_write(tty->index, buf, count);
185666b7793SArve Hjønnevåg 	return count;
186666b7793SArve Hjønnevåg }
187666b7793SArve Hjønnevåg 
18803b3b1a2SJiri Slaby static unsigned int goldfish_tty_write_room(struct tty_struct *tty)
189666b7793SArve Hjønnevåg {
190666b7793SArve Hjønnevåg 	return 0x10000;
191666b7793SArve Hjønnevåg }
192666b7793SArve Hjønnevåg 
193fff4ef17SJiri Slaby static unsigned int goldfish_tty_chars_in_buffer(struct tty_struct *tty)
194666b7793SArve Hjønnevåg {
195666b7793SArve Hjønnevåg 	struct goldfish_tty *qtty = &goldfish_ttys[tty->index];
196666b7793SArve Hjønnevåg 	void __iomem *base = qtty->base;
1972e2ac4a3SLaurent Vivier 	return gf_ioread32(base + GOLDFISH_TTY_REG_BYTES_READY);
198666b7793SArve Hjønnevåg }
199666b7793SArve Hjønnevåg 
200d78055dcSAlan static void goldfish_tty_console_write(struct console *co, const char *b,
201d78055dcSAlan 								unsigned count)
202666b7793SArve Hjønnevåg {
203666b7793SArve Hjønnevåg 	goldfish_tty_do_write(co->index, b, count);
204666b7793SArve Hjønnevåg }
205666b7793SArve Hjønnevåg 
206d78055dcSAlan static struct tty_driver *goldfish_tty_console_device(struct console *c,
207d78055dcSAlan 								int *index)
208666b7793SArve Hjønnevåg {
209666b7793SArve Hjønnevåg 	*index = c->index;
210666b7793SArve Hjønnevåg 	return goldfish_tty_driver;
211666b7793SArve Hjønnevåg }
212666b7793SArve Hjønnevåg 
213666b7793SArve Hjønnevåg static int goldfish_tty_console_setup(struct console *co, char *options)
214666b7793SArve Hjønnevåg {
215fda2b418SDan Carpenter 	if ((unsigned)co->index >= goldfish_tty_line_count)
216666b7793SArve Hjønnevåg 		return -ENODEV;
217a4dc9236SFabian Frederick 	if (!goldfish_ttys[co->index].base)
218666b7793SArve Hjønnevåg 		return -ENODEV;
219666b7793SArve Hjønnevåg 	return 0;
220666b7793SArve Hjønnevåg }
221666b7793SArve Hjønnevåg 
22204b757dfSAya Mahfouz static const struct tty_port_operations goldfish_port_ops = {
223666b7793SArve Hjønnevåg 	.activate = goldfish_tty_activate,
224666b7793SArve Hjønnevåg 	.shutdown = goldfish_tty_shutdown
225666b7793SArve Hjønnevåg };
226666b7793SArve Hjønnevåg 
227d78055dcSAlan static const struct tty_operations goldfish_tty_ops = {
228666b7793SArve Hjønnevåg 	.open = goldfish_tty_open,
229666b7793SArve Hjønnevåg 	.close = goldfish_tty_close,
230666b7793SArve Hjønnevåg 	.hangup = goldfish_tty_hangup,
231666b7793SArve Hjønnevåg 	.write = goldfish_tty_write,
232666b7793SArve Hjønnevåg 	.write_room = goldfish_tty_write_room,
233666b7793SArve Hjønnevåg 	.chars_in_buffer = goldfish_tty_chars_in_buffer,
234666b7793SArve Hjønnevåg };
235666b7793SArve Hjønnevåg 
236666b7793SArve Hjønnevåg static int goldfish_tty_create_driver(void)
237666b7793SArve Hjønnevåg {
238666b7793SArve Hjønnevåg 	int ret;
239666b7793SArve Hjønnevåg 	struct tty_driver *tty;
240666b7793SArve Hjønnevåg 
2416396bb22SKees Cook 	goldfish_ttys = kcalloc(goldfish_tty_line_count,
2426396bb22SKees Cook 				sizeof(*goldfish_ttys),
2436396bb22SKees Cook 				GFP_KERNEL);
244666b7793SArve Hjønnevåg 	if (goldfish_ttys == NULL) {
245666b7793SArve Hjønnevåg 		ret = -ENOMEM;
246666b7793SArve Hjønnevåg 		goto err_alloc_goldfish_ttys_failed;
247666b7793SArve Hjønnevåg 	}
24839b7b42bSJiri Slaby 	tty = tty_alloc_driver(goldfish_tty_line_count,
24939b7b42bSJiri Slaby 			TTY_DRIVER_RESET_TERMIOS | TTY_DRIVER_REAL_RAW |
25039b7b42bSJiri Slaby 			TTY_DRIVER_DYNAMIC_DEV);
25139b7b42bSJiri Slaby 	if (IS_ERR(tty)) {
25239b7b42bSJiri Slaby 		ret = PTR_ERR(tty);
25339b7b42bSJiri Slaby 		goto err_tty_alloc_driver_failed;
254666b7793SArve Hjønnevåg 	}
255666b7793SArve Hjønnevåg 	tty->driver_name = "goldfish";
256666b7793SArve Hjønnevåg 	tty->name = "ttyGF";
257666b7793SArve Hjønnevåg 	tty->type = TTY_DRIVER_TYPE_SERIAL;
258666b7793SArve Hjønnevåg 	tty->subtype = SERIAL_TYPE_NORMAL;
259666b7793SArve Hjønnevåg 	tty->init_termios = tty_std_termios;
260666b7793SArve Hjønnevåg 	tty_set_operations(tty, &goldfish_tty_ops);
261666b7793SArve Hjønnevåg 	ret = tty_register_driver(tty);
262666b7793SArve Hjønnevåg 	if (ret)
263666b7793SArve Hjønnevåg 		goto err_tty_register_driver_failed;
264666b7793SArve Hjønnevåg 
265666b7793SArve Hjønnevåg 	goldfish_tty_driver = tty;
266666b7793SArve Hjønnevåg 	return 0;
267666b7793SArve Hjønnevåg 
268666b7793SArve Hjønnevåg err_tty_register_driver_failed:
2699f90a4ddSJiri Slaby 	tty_driver_kref_put(tty);
27039b7b42bSJiri Slaby err_tty_alloc_driver_failed:
271666b7793SArve Hjønnevåg 	kfree(goldfish_ttys);
272666b7793SArve Hjønnevåg 	goldfish_ttys = NULL;
273666b7793SArve Hjønnevåg err_alloc_goldfish_ttys_failed:
274666b7793SArve Hjønnevåg 	return ret;
275666b7793SArve Hjønnevåg }
276666b7793SArve Hjønnevåg 
277666b7793SArve Hjønnevåg static void goldfish_tty_delete_driver(void)
278666b7793SArve Hjønnevåg {
279666b7793SArve Hjønnevåg 	tty_unregister_driver(goldfish_tty_driver);
2809f90a4ddSJiri Slaby 	tty_driver_kref_put(goldfish_tty_driver);
281666b7793SArve Hjønnevåg 	goldfish_tty_driver = NULL;
282666b7793SArve Hjønnevåg 	kfree(goldfish_ttys);
283666b7793SArve Hjønnevåg 	goldfish_ttys = NULL;
284666b7793SArve Hjønnevåg }
285666b7793SArve Hjønnevåg 
286666b7793SArve Hjønnevåg static int goldfish_tty_probe(struct platform_device *pdev)
287666b7793SArve Hjønnevåg {
288666b7793SArve Hjønnevåg 	struct goldfish_tty *qtty;
2897157d2beSMiodrag Dinic 	int ret = -ENODEV;
290666b7793SArve Hjønnevåg 	struct resource *r;
291666b7793SArve Hjønnevåg 	struct device *ttydev;
292666b7793SArve Hjønnevåg 	void __iomem *base;
2935acb78dcSLad Prabhakar 	int irq;
294465893e1SGreg Hackmann 	unsigned int line;
295666b7793SArve Hjønnevåg 
296666b7793SArve Hjønnevåg 	r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2977157d2beSMiodrag Dinic 	if (!r) {
2987157d2beSMiodrag Dinic 		pr_err("goldfish_tty: No MEM resource available!\n");
2997157d2beSMiodrag Dinic 		return -ENOMEM;
3007157d2beSMiodrag Dinic 	}
301666b7793SArve Hjønnevåg 
302666b7793SArve Hjønnevåg 	base = ioremap(r->start, 0x1000);
3037157d2beSMiodrag Dinic 	if (!base) {
3047157d2beSMiodrag Dinic 		pr_err("goldfish_tty: Unable to ioremap base!\n");
3057157d2beSMiodrag Dinic 		return -ENOMEM;
3067157d2beSMiodrag Dinic 	}
307666b7793SArve Hjønnevåg 
3085acb78dcSLad Prabhakar 	irq = platform_get_irq(pdev, 0);
3095acb78dcSLad Prabhakar 	if (irq < 0) {
3105acb78dcSLad Prabhakar 		ret = irq;
311666b7793SArve Hjønnevåg 		goto err_unmap;
3127157d2beSMiodrag Dinic 	}
313666b7793SArve Hjønnevåg 
314666b7793SArve Hjønnevåg 	mutex_lock(&goldfish_tty_lock);
315465893e1SGreg Hackmann 
316465893e1SGreg Hackmann 	if (pdev->id == PLATFORM_DEVID_NONE)
317465893e1SGreg Hackmann 		line = goldfish_tty_current_line_count;
318465893e1SGreg Hackmann 	else
319465893e1SGreg Hackmann 		line = pdev->id;
320465893e1SGreg Hackmann 
3217157d2beSMiodrag Dinic 	if (line >= goldfish_tty_line_count) {
3227157d2beSMiodrag Dinic 		pr_err("goldfish_tty: Reached maximum tty number of %d.\n",
3237157d2beSMiodrag Dinic 		       goldfish_tty_current_line_count);
3247157d2beSMiodrag Dinic 		ret = -ENOMEM;
3257157d2beSMiodrag Dinic 		goto err_unlock;
3267157d2beSMiodrag Dinic 	}
327465893e1SGreg Hackmann 
328666b7793SArve Hjønnevåg 	if (goldfish_tty_current_line_count == 0) {
329666b7793SArve Hjønnevåg 		ret = goldfish_tty_create_driver();
330666b7793SArve Hjønnevåg 		if (ret)
3317157d2beSMiodrag Dinic 			goto err_unlock;
332666b7793SArve Hjønnevåg 	}
333666b7793SArve Hjønnevåg 	goldfish_tty_current_line_count++;
334666b7793SArve Hjønnevåg 
335465893e1SGreg Hackmann 	qtty = &goldfish_ttys[line];
336666b7793SArve Hjønnevåg 	spin_lock_init(&qtty->lock);
337666b7793SArve Hjønnevåg 	tty_port_init(&qtty->port);
338666b7793SArve Hjønnevåg 	qtty->port.ops = &goldfish_port_ops;
339666b7793SArve Hjønnevåg 	qtty->base = base;
340666b7793SArve Hjønnevåg 	qtty->irq = irq;
3417157d2beSMiodrag Dinic 	qtty->dev = &pdev->dev;
3427157d2beSMiodrag Dinic 
3437157d2beSMiodrag Dinic 	/*
3447157d2beSMiodrag Dinic 	 * Goldfish TTY device used by the Goldfish emulator
3457157d2beSMiodrag Dinic 	 * should identify itself with 0, forcing the driver
3467157d2beSMiodrag Dinic 	 * to use virtual addresses. Goldfish TTY device
3477157d2beSMiodrag Dinic 	 * on Ranchu emulator (qemu2) returns 1 here and
3487157d2beSMiodrag Dinic 	 * driver will use physical addresses.
3497157d2beSMiodrag Dinic 	 */
3502e2ac4a3SLaurent Vivier 	qtty->version = gf_ioread32(base + GOLDFISH_TTY_REG_VERSION);
3517157d2beSMiodrag Dinic 
3527157d2beSMiodrag Dinic 	/*
3537157d2beSMiodrag Dinic 	 * Goldfish TTY device on Ranchu emulator (qemu2)
3547157d2beSMiodrag Dinic 	 * will use DMA for read/write IO operations.
3557157d2beSMiodrag Dinic 	 */
3567157d2beSMiodrag Dinic 	if (qtty->version > 0) {
3577157d2beSMiodrag Dinic 		/*
3587157d2beSMiodrag Dinic 		 * Initialize dma_mask to 32-bits.
3597157d2beSMiodrag Dinic 		 */
3607157d2beSMiodrag Dinic 		if (!pdev->dev.dma_mask)
3617157d2beSMiodrag Dinic 			pdev->dev.dma_mask = &pdev->dev.coherent_dma_mask;
3627157d2beSMiodrag Dinic 		ret = dma_set_mask(&pdev->dev, DMA_BIT_MASK(32));
3637157d2beSMiodrag Dinic 		if (ret) {
3647157d2beSMiodrag Dinic 			dev_err(&pdev->dev, "No suitable DMA available.\n");
3657157d2beSMiodrag Dinic 			goto err_dec_line_count;
3667157d2beSMiodrag Dinic 		}
3677157d2beSMiodrag Dinic 	}
368666b7793SArve Hjønnevåg 
3692e2ac4a3SLaurent Vivier 	gf_iowrite32(GOLDFISH_TTY_CMD_INT_DISABLE, base + GOLDFISH_TTY_REG_CMD);
370666b7793SArve Hjønnevåg 
371d78055dcSAlan 	ret = request_irq(irq, goldfish_tty_interrupt, IRQF_SHARED,
372465893e1SGreg Hackmann 			  "goldfish_tty", qtty);
3737157d2beSMiodrag Dinic 	if (ret) {
3747157d2beSMiodrag Dinic 		pr_err("goldfish_tty: No IRQ available!\n");
3757157d2beSMiodrag Dinic 		goto err_dec_line_count;
3767157d2beSMiodrag Dinic 	}
377666b7793SArve Hjønnevåg 
378666b7793SArve Hjønnevåg 	ttydev = tty_port_register_device(&qtty->port, goldfish_tty_driver,
379465893e1SGreg Hackmann 					  line, &pdev->dev);
380666b7793SArve Hjønnevåg 	if (IS_ERR(ttydev)) {
381666b7793SArve Hjønnevåg 		ret = PTR_ERR(ttydev);
382666b7793SArve Hjønnevåg 		goto err_tty_register_device_failed;
383666b7793SArve Hjønnevåg 	}
384666b7793SArve Hjønnevåg 
385666b7793SArve Hjønnevåg 	strcpy(qtty->console.name, "ttyGF");
386666b7793SArve Hjønnevåg 	qtty->console.write = goldfish_tty_console_write;
387666b7793SArve Hjønnevåg 	qtty->console.device = goldfish_tty_console_device;
388666b7793SArve Hjønnevåg 	qtty->console.setup = goldfish_tty_console_setup;
389666b7793SArve Hjønnevåg 	qtty->console.flags = CON_PRINTBUFFER;
390465893e1SGreg Hackmann 	qtty->console.index = line;
391666b7793SArve Hjønnevåg 	register_console(&qtty->console);
392465893e1SGreg Hackmann 	platform_set_drvdata(pdev, qtty);
393666b7793SArve Hjønnevåg 
394666b7793SArve Hjønnevåg 	mutex_unlock(&goldfish_tty_lock);
395666b7793SArve Hjønnevåg 	return 0;
396666b7793SArve Hjønnevåg 
397666b7793SArve Hjønnevåg err_tty_register_device_failed:
3981a5c2d1dSChristophe JAILLET 	free_irq(irq, qtty);
3997157d2beSMiodrag Dinic err_dec_line_count:
400507b0506SWang Weiyang 	tty_port_destroy(&qtty->port);
401666b7793SArve Hjønnevåg 	goldfish_tty_current_line_count--;
402666b7793SArve Hjønnevåg 	if (goldfish_tty_current_line_count == 0)
403666b7793SArve Hjønnevåg 		goldfish_tty_delete_driver();
4047157d2beSMiodrag Dinic err_unlock:
405666b7793SArve Hjønnevåg 	mutex_unlock(&goldfish_tty_lock);
406666b7793SArve Hjønnevåg err_unmap:
407666b7793SArve Hjønnevåg 	iounmap(base);
408666b7793SArve Hjønnevåg 	return ret;
409666b7793SArve Hjønnevåg }
410666b7793SArve Hjønnevåg 
411138ab8afSUwe Kleine-König static void goldfish_tty_remove(struct platform_device *pdev)
412666b7793SArve Hjønnevåg {
413465893e1SGreg Hackmann 	struct goldfish_tty *qtty = platform_get_drvdata(pdev);
414666b7793SArve Hjønnevåg 
415666b7793SArve Hjønnevåg 	mutex_lock(&goldfish_tty_lock);
416666b7793SArve Hjønnevåg 
417666b7793SArve Hjønnevåg 	unregister_console(&qtty->console);
418465893e1SGreg Hackmann 	tty_unregister_device(goldfish_tty_driver, qtty->console.index);
419666b7793SArve Hjønnevåg 	iounmap(qtty->base);
420a4dc9236SFabian Frederick 	qtty->base = NULL;
421499e13aaSVincent Whitchurch 	free_irq(qtty->irq, qtty);
422507b0506SWang Weiyang 	tty_port_destroy(&qtty->port);
423666b7793SArve Hjønnevåg 	goldfish_tty_current_line_count--;
424666b7793SArve Hjønnevåg 	if (goldfish_tty_current_line_count == 0)
425666b7793SArve Hjønnevåg 		goldfish_tty_delete_driver();
426666b7793SArve Hjønnevåg 	mutex_unlock(&goldfish_tty_lock);
427666b7793SArve Hjønnevåg }
428666b7793SArve Hjønnevåg 
4296a28fd2bSSebastian Andrzej Siewior #ifdef CONFIG_GOLDFISH_TTY_EARLY_CONSOLE
4303f8bab17SJiri Slaby static void gf_early_console_putchar(struct uart_port *port, unsigned char ch)
4313840ed95SMiodrag Dinic {
4322e2ac4a3SLaurent Vivier 	gf_iowrite32(ch, port->membase);
4333840ed95SMiodrag Dinic }
4343840ed95SMiodrag Dinic 
4353840ed95SMiodrag Dinic static void gf_early_write(struct console *con, const char *s, unsigned int n)
4363840ed95SMiodrag Dinic {
4373840ed95SMiodrag Dinic 	struct earlycon_device *dev = con->data;
4383840ed95SMiodrag Dinic 
4393840ed95SMiodrag Dinic 	uart_console_write(&dev->port, s, n, gf_early_console_putchar);
4403840ed95SMiodrag Dinic }
4413840ed95SMiodrag Dinic 
4423840ed95SMiodrag Dinic static int __init gf_earlycon_setup(struct earlycon_device *device,
4433840ed95SMiodrag Dinic 				    const char *opt)
4443840ed95SMiodrag Dinic {
4453840ed95SMiodrag Dinic 	if (!device->port.membase)
4463840ed95SMiodrag Dinic 		return -ENODEV;
4473840ed95SMiodrag Dinic 
4483840ed95SMiodrag Dinic 	device->con->write = gf_early_write;
4493840ed95SMiodrag Dinic 	return 0;
4503840ed95SMiodrag Dinic }
4513840ed95SMiodrag Dinic 
4523840ed95SMiodrag Dinic OF_EARLYCON_DECLARE(early_gf_tty, "google,goldfish-tty", gf_earlycon_setup);
4536a28fd2bSSebastian Andrzej Siewior #endif
4543840ed95SMiodrag Dinic 
4559b883eeaSMiodrag Dinic static const struct of_device_id goldfish_tty_of_match[] = {
4569b883eeaSMiodrag Dinic 	{ .compatible = "google,goldfish-tty", },
4579b883eeaSMiodrag Dinic 	{},
4589b883eeaSMiodrag Dinic };
4599b883eeaSMiodrag Dinic 
4609b883eeaSMiodrag Dinic MODULE_DEVICE_TABLE(of, goldfish_tty_of_match);
4619b883eeaSMiodrag Dinic 
462666b7793SArve Hjønnevåg static struct platform_driver goldfish_tty_platform_driver = {
463666b7793SArve Hjønnevåg 	.probe = goldfish_tty_probe,
464138ab8afSUwe Kleine-König 	.remove_new = goldfish_tty_remove,
465666b7793SArve Hjønnevåg 	.driver = {
4669b883eeaSMiodrag Dinic 		.name = "goldfish_tty",
4679b883eeaSMiodrag Dinic 		.of_match_table = goldfish_tty_of_match,
468666b7793SArve Hjønnevåg 	}
469666b7793SArve Hjønnevåg };
470666b7793SArve Hjønnevåg 
471666b7793SArve Hjønnevåg module_platform_driver(goldfish_tty_platform_driver);
472666b7793SArve Hjønnevåg 
473*f0a17485SJeff Johnson MODULE_DESCRIPTION("Goldfish TTY Driver");
474666b7793SArve Hjønnevåg MODULE_LICENSE("GPL v2");
475