xref: /linux/drivers/tty/goldfish.c (revision 7157d2be23da9f8860c69e2b79184a4e02701dad)
1666b7793SArve Hjønnevåg /*
2666b7793SArve Hjønnevåg  * Copyright (C) 2007 Google, Inc.
3666b7793SArve Hjønnevåg  * Copyright (C) 2012 Intel, Inc.
4666b7793SArve Hjønnevåg  *
5666b7793SArve Hjønnevåg  * This software is licensed under the terms of the GNU General Public
6666b7793SArve Hjønnevåg  * License version 2, as published by the Free Software Foundation, and
7666b7793SArve Hjønnevåg  * may be copied, distributed, and modified under those terms.
8666b7793SArve Hjønnevåg  *
9666b7793SArve Hjønnevåg  * This program is distributed in the hope that it will be useful,
10666b7793SArve Hjønnevåg  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11666b7793SArve Hjønnevåg  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12666b7793SArve Hjønnevåg  * GNU General Public License for more details.
13666b7793SArve Hjønnevåg  *
14666b7793SArve Hjønnevåg  */
15666b7793SArve Hjønnevåg 
16666b7793SArve Hjønnevåg #include <linux/console.h>
17666b7793SArve Hjønnevåg #include <linux/interrupt.h>
18666b7793SArve Hjønnevåg #include <linux/platform_device.h>
19666b7793SArve Hjønnevåg #include <linux/tty.h>
20666b7793SArve Hjønnevåg #include <linux/tty_flip.h>
21666b7793SArve Hjønnevåg #include <linux/slab.h>
22666b7793SArve Hjønnevåg #include <linux/io.h>
23666b7793SArve Hjønnevåg #include <linux/module.h>
24e0f682e0SAlan #include <linux/goldfish.h>
25*7157d2beSMiodrag Dinic #include <linux/mm.h>
26*7157d2beSMiodrag Dinic #include <linux/dma-mapping.h>
27666b7793SArve Hjønnevåg 
282296eee7SAleksandar Markovic /* Goldfish tty register's offsets */
292296eee7SAleksandar Markovic #define	GOLDFISH_TTY_REG_BYTES_READY	0x04
302296eee7SAleksandar Markovic #define	GOLDFISH_TTY_REG_CMD		0x08
312296eee7SAleksandar Markovic #define	GOLDFISH_TTY_REG_DATA_PTR	0x10
322296eee7SAleksandar Markovic #define	GOLDFISH_TTY_REG_DATA_LEN	0x14
332296eee7SAleksandar Markovic #define	GOLDFISH_TTY_REG_DATA_PTR_HIGH	0x18
34*7157d2beSMiodrag Dinic #define	GOLDFISH_TTY_REG_VERSION	0x20
35666b7793SArve Hjønnevåg 
362296eee7SAleksandar Markovic /* Goldfish tty commands */
372296eee7SAleksandar Markovic #define	GOLDFISH_TTY_CMD_INT_DISABLE	0
382296eee7SAleksandar Markovic #define	GOLDFISH_TTY_CMD_INT_ENABLE	1
392296eee7SAleksandar Markovic #define	GOLDFISH_TTY_CMD_WRITE_BUFFER	2
402296eee7SAleksandar Markovic #define	GOLDFISH_TTY_CMD_READ_BUFFER	3
41666b7793SArve Hjønnevåg 
42666b7793SArve Hjønnevåg struct goldfish_tty {
43666b7793SArve Hjønnevåg 	struct tty_port port;
44666b7793SArve Hjønnevåg 	spinlock_t lock;
45666b7793SArve Hjønnevåg 	void __iomem *base;
46666b7793SArve Hjønnevåg 	u32 irq;
47666b7793SArve Hjønnevåg 	int opencount;
48666b7793SArve Hjønnevåg 	struct console console;
49*7157d2beSMiodrag Dinic 	u32 version;
50*7157d2beSMiodrag Dinic 	struct device *dev;
51666b7793SArve Hjønnevåg };
52666b7793SArve Hjønnevåg 
53666b7793SArve Hjønnevåg static DEFINE_MUTEX(goldfish_tty_lock);
54666b7793SArve Hjønnevåg static struct tty_driver *goldfish_tty_driver;
55666b7793SArve Hjønnevåg static u32 goldfish_tty_line_count = 8;
56666b7793SArve Hjønnevåg static u32 goldfish_tty_current_line_count;
57666b7793SArve Hjønnevåg static struct goldfish_tty *goldfish_ttys;
58666b7793SArve Hjønnevåg 
59*7157d2beSMiodrag Dinic static void do_rw_io(struct goldfish_tty *qtty,
60*7157d2beSMiodrag Dinic 		     unsigned long address,
61*7157d2beSMiodrag Dinic 		     unsigned int count,
62*7157d2beSMiodrag Dinic 		     int is_write)
63666b7793SArve Hjønnevåg {
64666b7793SArve Hjønnevåg 	unsigned long irq_flags;
65666b7793SArve Hjønnevåg 	void __iomem *base = qtty->base;
66*7157d2beSMiodrag Dinic 
67666b7793SArve Hjønnevåg 	spin_lock_irqsave(&qtty->lock, irq_flags);
68*7157d2beSMiodrag Dinic 	gf_write_ptr((void *)address, base + GOLDFISH_TTY_REG_DATA_PTR,
692296eee7SAleksandar Markovic 		     base + GOLDFISH_TTY_REG_DATA_PTR_HIGH);
702296eee7SAleksandar Markovic 	writel(count, base + GOLDFISH_TTY_REG_DATA_LEN);
71*7157d2beSMiodrag Dinic 
72*7157d2beSMiodrag Dinic 	if (is_write)
73*7157d2beSMiodrag Dinic 		writel(GOLDFISH_TTY_CMD_WRITE_BUFFER,
74*7157d2beSMiodrag Dinic 		       base + GOLDFISH_TTY_REG_CMD);
75*7157d2beSMiodrag Dinic 	else
76*7157d2beSMiodrag Dinic 		writel(GOLDFISH_TTY_CMD_READ_BUFFER,
77*7157d2beSMiodrag Dinic 		       base + GOLDFISH_TTY_REG_CMD);
78*7157d2beSMiodrag Dinic 
79666b7793SArve Hjønnevåg 	spin_unlock_irqrestore(&qtty->lock, irq_flags);
80666b7793SArve Hjønnevåg }
81666b7793SArve Hjønnevåg 
82*7157d2beSMiodrag Dinic static void goldfish_tty_rw(struct goldfish_tty *qtty,
83*7157d2beSMiodrag Dinic 			    unsigned long addr,
84*7157d2beSMiodrag Dinic 			    unsigned int count,
85*7157d2beSMiodrag Dinic 			    int is_write)
86*7157d2beSMiodrag Dinic {
87*7157d2beSMiodrag Dinic 	dma_addr_t dma_handle;
88*7157d2beSMiodrag Dinic 	enum dma_data_direction dma_dir;
89*7157d2beSMiodrag Dinic 
90*7157d2beSMiodrag Dinic 	dma_dir = (is_write ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
91*7157d2beSMiodrag Dinic 	if (qtty->version > 0) {
92*7157d2beSMiodrag Dinic 		/*
93*7157d2beSMiodrag Dinic 		 * Goldfish TTY for Ranchu platform uses
94*7157d2beSMiodrag Dinic 		 * physical addresses and DMA for read/write operations
95*7157d2beSMiodrag Dinic 		 */
96*7157d2beSMiodrag Dinic 		unsigned long addr_end = addr + count;
97*7157d2beSMiodrag Dinic 
98*7157d2beSMiodrag Dinic 		while (addr < addr_end) {
99*7157d2beSMiodrag Dinic 			unsigned long pg_end = (addr & PAGE_MASK) + PAGE_SIZE;
100*7157d2beSMiodrag Dinic 			unsigned long next =
101*7157d2beSMiodrag Dinic 					pg_end < addr_end ? pg_end : addr_end;
102*7157d2beSMiodrag Dinic 			unsigned long avail = next - addr;
103*7157d2beSMiodrag Dinic 
104*7157d2beSMiodrag Dinic 			/*
105*7157d2beSMiodrag Dinic 			 * Map the buffer's virtual address to the DMA address
106*7157d2beSMiodrag Dinic 			 * so the buffer can be accessed by the device.
107*7157d2beSMiodrag Dinic 			 */
108*7157d2beSMiodrag Dinic 			dma_handle = dma_map_single(qtty->dev, (void *)addr,
109*7157d2beSMiodrag Dinic 						    avail, dma_dir);
110*7157d2beSMiodrag Dinic 
111*7157d2beSMiodrag Dinic 			if (dma_mapping_error(qtty->dev, dma_handle)) {
112*7157d2beSMiodrag Dinic 				dev_err(qtty->dev, "tty: DMA mapping error.\n");
113*7157d2beSMiodrag Dinic 				return;
114*7157d2beSMiodrag Dinic 			}
115*7157d2beSMiodrag Dinic 			do_rw_io(qtty, dma_handle, avail, is_write);
116*7157d2beSMiodrag Dinic 
117*7157d2beSMiodrag Dinic 			/*
118*7157d2beSMiodrag Dinic 			 * Unmap the previously mapped region after
119*7157d2beSMiodrag Dinic 			 * the completion of the read/write operation.
120*7157d2beSMiodrag Dinic 			 */
121*7157d2beSMiodrag Dinic 			dma_unmap_single(qtty->dev, dma_handle, avail, dma_dir);
122*7157d2beSMiodrag Dinic 
123*7157d2beSMiodrag Dinic 			addr += avail;
124*7157d2beSMiodrag Dinic 		}
125*7157d2beSMiodrag Dinic 	} else {
126*7157d2beSMiodrag Dinic 		/*
127*7157d2beSMiodrag Dinic 		 * Old style Goldfish TTY used on the Goldfish platform
128*7157d2beSMiodrag Dinic 		 * uses virtual addresses.
129*7157d2beSMiodrag Dinic 		 */
130*7157d2beSMiodrag Dinic 		do_rw_io(qtty, addr, count, is_write);
131*7157d2beSMiodrag Dinic 	}
132*7157d2beSMiodrag Dinic }
133*7157d2beSMiodrag Dinic 
134*7157d2beSMiodrag Dinic static void goldfish_tty_do_write(int line, const char *buf,
135*7157d2beSMiodrag Dinic 				  unsigned int count)
136*7157d2beSMiodrag Dinic {
137*7157d2beSMiodrag Dinic 	struct goldfish_tty *qtty = &goldfish_ttys[line];
138*7157d2beSMiodrag Dinic 	unsigned long address = (unsigned long)(void *)buf;
139*7157d2beSMiodrag Dinic 
140*7157d2beSMiodrag Dinic 	goldfish_tty_rw(qtty, address, count, 1);
141*7157d2beSMiodrag Dinic }
142*7157d2beSMiodrag Dinic 
143666b7793SArve Hjønnevåg static irqreturn_t goldfish_tty_interrupt(int irq, void *dev_id)
144666b7793SArve Hjønnevåg {
145465893e1SGreg Hackmann 	struct goldfish_tty *qtty = dev_id;
146666b7793SArve Hjønnevåg 	void __iomem *base = qtty->base;
147*7157d2beSMiodrag Dinic 	unsigned long address;
148666b7793SArve Hjønnevåg 	unsigned char *buf;
149666b7793SArve Hjønnevåg 	u32 count;
150666b7793SArve Hjønnevåg 
1512296eee7SAleksandar Markovic 	count = readl(base + GOLDFISH_TTY_REG_BYTES_READY);
152666b7793SArve Hjønnevåg 	if (count == 0)
153666b7793SArve Hjønnevåg 		return IRQ_NONE;
154666b7793SArve Hjønnevåg 
155ebcf0981SAlan Cox 	count = tty_prepare_flip_string(&qtty->port, &buf, count);
156*7157d2beSMiodrag Dinic 
157*7157d2beSMiodrag Dinic 	address = (unsigned long)(void *)buf;
158*7157d2beSMiodrag Dinic 	goldfish_tty_rw(qtty, address, count, 0);
159*7157d2beSMiodrag Dinic 
160ebcf0981SAlan Cox 	tty_schedule_flip(&qtty->port);
161666b7793SArve Hjønnevåg 	return IRQ_HANDLED;
162666b7793SArve Hjønnevåg }
163666b7793SArve Hjønnevåg 
164666b7793SArve Hjønnevåg static int goldfish_tty_activate(struct tty_port *port, struct tty_struct *tty)
165666b7793SArve Hjønnevåg {
166d78055dcSAlan 	struct goldfish_tty *qtty = container_of(port, struct goldfish_tty,
167d78055dcSAlan 									port);
1682296eee7SAleksandar Markovic 	writel(GOLDFISH_TTY_CMD_INT_ENABLE, qtty->base + GOLDFISH_TTY_REG_CMD);
169666b7793SArve Hjønnevåg 	return 0;
170666b7793SArve Hjønnevåg }
171666b7793SArve Hjønnevåg 
172666b7793SArve Hjønnevåg static void goldfish_tty_shutdown(struct tty_port *port)
173666b7793SArve Hjønnevåg {
174d78055dcSAlan 	struct goldfish_tty *qtty = container_of(port, struct goldfish_tty,
175d78055dcSAlan 									port);
1762296eee7SAleksandar Markovic 	writel(GOLDFISH_TTY_CMD_INT_DISABLE, qtty->base + GOLDFISH_TTY_REG_CMD);
177666b7793SArve Hjønnevåg }
178666b7793SArve Hjønnevåg 
179666b7793SArve Hjønnevåg static int goldfish_tty_open(struct tty_struct *tty, struct file *filp)
180666b7793SArve Hjønnevåg {
181666b7793SArve Hjønnevåg 	struct goldfish_tty *qtty = &goldfish_ttys[tty->index];
182666b7793SArve Hjønnevåg 	return tty_port_open(&qtty->port, tty, filp);
183666b7793SArve Hjønnevåg }
184666b7793SArve Hjønnevåg 
185666b7793SArve Hjønnevåg static void goldfish_tty_close(struct tty_struct *tty, struct file *filp)
186666b7793SArve Hjønnevåg {
187666b7793SArve Hjønnevåg 	tty_port_close(tty->port, tty, filp);
188666b7793SArve Hjønnevåg }
189666b7793SArve Hjønnevåg 
190666b7793SArve Hjønnevåg static void goldfish_tty_hangup(struct tty_struct *tty)
191666b7793SArve Hjønnevåg {
192666b7793SArve Hjønnevåg 	tty_port_hangup(tty->port);
193666b7793SArve Hjønnevåg }
194666b7793SArve Hjønnevåg 
195d78055dcSAlan static int goldfish_tty_write(struct tty_struct *tty, const unsigned char *buf,
196d78055dcSAlan 								int count)
197666b7793SArve Hjønnevåg {
198666b7793SArve Hjønnevåg 	goldfish_tty_do_write(tty->index, buf, count);
199666b7793SArve Hjønnevåg 	return count;
200666b7793SArve Hjønnevåg }
201666b7793SArve Hjønnevåg 
202666b7793SArve Hjønnevåg static int goldfish_tty_write_room(struct tty_struct *tty)
203666b7793SArve Hjønnevåg {
204666b7793SArve Hjønnevåg 	return 0x10000;
205666b7793SArve Hjønnevåg }
206666b7793SArve Hjønnevåg 
207666b7793SArve Hjønnevåg static int goldfish_tty_chars_in_buffer(struct tty_struct *tty)
208666b7793SArve Hjønnevåg {
209666b7793SArve Hjønnevåg 	struct goldfish_tty *qtty = &goldfish_ttys[tty->index];
210666b7793SArve Hjønnevåg 	void __iomem *base = qtty->base;
2112296eee7SAleksandar Markovic 	return readl(base + GOLDFISH_TTY_REG_BYTES_READY);
212666b7793SArve Hjønnevåg }
213666b7793SArve Hjønnevåg 
214d78055dcSAlan static void goldfish_tty_console_write(struct console *co, const char *b,
215d78055dcSAlan 								unsigned count)
216666b7793SArve Hjønnevåg {
217666b7793SArve Hjønnevåg 	goldfish_tty_do_write(co->index, b, count);
218666b7793SArve Hjønnevåg }
219666b7793SArve Hjønnevåg 
220d78055dcSAlan static struct tty_driver *goldfish_tty_console_device(struct console *c,
221d78055dcSAlan 								int *index)
222666b7793SArve Hjønnevåg {
223666b7793SArve Hjønnevåg 	*index = c->index;
224666b7793SArve Hjønnevåg 	return goldfish_tty_driver;
225666b7793SArve Hjønnevåg }
226666b7793SArve Hjønnevåg 
227666b7793SArve Hjønnevåg static int goldfish_tty_console_setup(struct console *co, char *options)
228666b7793SArve Hjønnevåg {
229fda2b418SDan Carpenter 	if ((unsigned)co->index >= goldfish_tty_line_count)
230666b7793SArve Hjønnevåg 		return -ENODEV;
231a4dc9236SFabian Frederick 	if (!goldfish_ttys[co->index].base)
232666b7793SArve Hjønnevåg 		return -ENODEV;
233666b7793SArve Hjønnevåg 	return 0;
234666b7793SArve Hjønnevåg }
235666b7793SArve Hjønnevåg 
23604b757dfSAya Mahfouz static const struct tty_port_operations goldfish_port_ops = {
237666b7793SArve Hjønnevåg 	.activate = goldfish_tty_activate,
238666b7793SArve Hjønnevåg 	.shutdown = goldfish_tty_shutdown
239666b7793SArve Hjønnevåg };
240666b7793SArve Hjønnevåg 
241d78055dcSAlan static const struct tty_operations goldfish_tty_ops = {
242666b7793SArve Hjønnevåg 	.open = goldfish_tty_open,
243666b7793SArve Hjønnevåg 	.close = goldfish_tty_close,
244666b7793SArve Hjønnevåg 	.hangup = goldfish_tty_hangup,
245666b7793SArve Hjønnevåg 	.write = goldfish_tty_write,
246666b7793SArve Hjønnevåg 	.write_room = goldfish_tty_write_room,
247666b7793SArve Hjønnevåg 	.chars_in_buffer = goldfish_tty_chars_in_buffer,
248666b7793SArve Hjønnevåg };
249666b7793SArve Hjønnevåg 
250666b7793SArve Hjønnevåg static int goldfish_tty_create_driver(void)
251666b7793SArve Hjønnevåg {
252666b7793SArve Hjønnevåg 	int ret;
253666b7793SArve Hjønnevåg 	struct tty_driver *tty;
254666b7793SArve Hjønnevåg 
255d78055dcSAlan 	goldfish_ttys = kzalloc(sizeof(*goldfish_ttys) *
256d78055dcSAlan 				goldfish_tty_line_count, GFP_KERNEL);
257666b7793SArve Hjønnevåg 	if (goldfish_ttys == NULL) {
258666b7793SArve Hjønnevåg 		ret = -ENOMEM;
259666b7793SArve Hjønnevåg 		goto err_alloc_goldfish_ttys_failed;
260666b7793SArve Hjønnevåg 	}
261666b7793SArve Hjønnevåg 	tty = alloc_tty_driver(goldfish_tty_line_count);
262666b7793SArve Hjønnevåg 	if (tty == NULL) {
263666b7793SArve Hjønnevåg 		ret = -ENOMEM;
264666b7793SArve Hjønnevåg 		goto err_alloc_tty_driver_failed;
265666b7793SArve Hjønnevåg 	}
266666b7793SArve Hjønnevåg 	tty->driver_name = "goldfish";
267666b7793SArve Hjønnevåg 	tty->name = "ttyGF";
268666b7793SArve Hjønnevåg 	tty->type = TTY_DRIVER_TYPE_SERIAL;
269666b7793SArve Hjønnevåg 	tty->subtype = SERIAL_TYPE_NORMAL;
270666b7793SArve Hjønnevåg 	tty->init_termios = tty_std_termios;
271d78055dcSAlan 	tty->flags = TTY_DRIVER_RESET_TERMIOS | TTY_DRIVER_REAL_RAW |
272d78055dcSAlan 						TTY_DRIVER_DYNAMIC_DEV;
273666b7793SArve Hjønnevåg 	tty_set_operations(tty, &goldfish_tty_ops);
274666b7793SArve Hjønnevåg 	ret = tty_register_driver(tty);
275666b7793SArve Hjønnevåg 	if (ret)
276666b7793SArve Hjønnevåg 		goto err_tty_register_driver_failed;
277666b7793SArve Hjønnevåg 
278666b7793SArve Hjønnevåg 	goldfish_tty_driver = tty;
279666b7793SArve Hjønnevåg 	return 0;
280666b7793SArve Hjønnevåg 
281666b7793SArve Hjønnevåg err_tty_register_driver_failed:
282666b7793SArve Hjønnevåg 	put_tty_driver(tty);
283666b7793SArve Hjønnevåg err_alloc_tty_driver_failed:
284666b7793SArve Hjønnevåg 	kfree(goldfish_ttys);
285666b7793SArve Hjønnevåg 	goldfish_ttys = NULL;
286666b7793SArve Hjønnevåg err_alloc_goldfish_ttys_failed:
287666b7793SArve Hjønnevåg 	return ret;
288666b7793SArve Hjønnevåg }
289666b7793SArve Hjønnevåg 
290666b7793SArve Hjønnevåg static void goldfish_tty_delete_driver(void)
291666b7793SArve Hjønnevåg {
292666b7793SArve Hjønnevåg 	tty_unregister_driver(goldfish_tty_driver);
293666b7793SArve Hjønnevåg 	put_tty_driver(goldfish_tty_driver);
294666b7793SArve Hjønnevåg 	goldfish_tty_driver = NULL;
295666b7793SArve Hjønnevåg 	kfree(goldfish_ttys);
296666b7793SArve Hjønnevåg 	goldfish_ttys = NULL;
297666b7793SArve Hjønnevåg }
298666b7793SArve Hjønnevåg 
299666b7793SArve Hjønnevåg static int goldfish_tty_probe(struct platform_device *pdev)
300666b7793SArve Hjønnevåg {
301666b7793SArve Hjønnevåg 	struct goldfish_tty *qtty;
302*7157d2beSMiodrag Dinic 	int ret = -ENODEV;
303666b7793SArve Hjønnevåg 	struct resource *r;
304666b7793SArve Hjønnevåg 	struct device *ttydev;
305666b7793SArve Hjønnevåg 	void __iomem *base;
306666b7793SArve Hjønnevåg 	u32 irq;
307465893e1SGreg Hackmann 	unsigned int line;
308666b7793SArve Hjønnevåg 
309666b7793SArve Hjønnevåg 	r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
310*7157d2beSMiodrag Dinic 	if (!r) {
311*7157d2beSMiodrag Dinic 		pr_err("goldfish_tty: No MEM resource available!\n");
312*7157d2beSMiodrag Dinic 		return -ENOMEM;
313*7157d2beSMiodrag Dinic 	}
314666b7793SArve Hjønnevåg 
315666b7793SArve Hjønnevåg 	base = ioremap(r->start, 0x1000);
316*7157d2beSMiodrag Dinic 	if (!base) {
317*7157d2beSMiodrag Dinic 		pr_err("goldfish_tty: Unable to ioremap base!\n");
318*7157d2beSMiodrag Dinic 		return -ENOMEM;
319*7157d2beSMiodrag Dinic 	}
320666b7793SArve Hjønnevåg 
321666b7793SArve Hjønnevåg 	r = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
322*7157d2beSMiodrag Dinic 	if (!r) {
323*7157d2beSMiodrag Dinic 		pr_err("goldfish_tty: No IRQ resource available!\n");
324666b7793SArve Hjønnevåg 		goto err_unmap;
325*7157d2beSMiodrag Dinic 	}
326666b7793SArve Hjønnevåg 
327666b7793SArve Hjønnevåg 	irq = r->start;
328666b7793SArve Hjønnevåg 
329666b7793SArve Hjønnevåg 	mutex_lock(&goldfish_tty_lock);
330465893e1SGreg Hackmann 
331465893e1SGreg Hackmann 	if (pdev->id == PLATFORM_DEVID_NONE)
332465893e1SGreg Hackmann 		line = goldfish_tty_current_line_count;
333465893e1SGreg Hackmann 	else
334465893e1SGreg Hackmann 		line = pdev->id;
335465893e1SGreg Hackmann 
336*7157d2beSMiodrag Dinic 	if (line >= goldfish_tty_line_count) {
337*7157d2beSMiodrag Dinic 		pr_err("goldfish_tty: Reached maximum tty number of %d.\n",
338*7157d2beSMiodrag Dinic 		       goldfish_tty_current_line_count);
339*7157d2beSMiodrag Dinic 		ret = -ENOMEM;
340*7157d2beSMiodrag Dinic 		goto err_unlock;
341*7157d2beSMiodrag Dinic 	}
342465893e1SGreg Hackmann 
343666b7793SArve Hjønnevåg 	if (goldfish_tty_current_line_count == 0) {
344666b7793SArve Hjønnevåg 		ret = goldfish_tty_create_driver();
345666b7793SArve Hjønnevåg 		if (ret)
346*7157d2beSMiodrag Dinic 			goto err_unlock;
347666b7793SArve Hjønnevåg 	}
348666b7793SArve Hjønnevåg 	goldfish_tty_current_line_count++;
349666b7793SArve Hjønnevåg 
350465893e1SGreg Hackmann 	qtty = &goldfish_ttys[line];
351666b7793SArve Hjønnevåg 	spin_lock_init(&qtty->lock);
352666b7793SArve Hjønnevåg 	tty_port_init(&qtty->port);
353666b7793SArve Hjønnevåg 	qtty->port.ops = &goldfish_port_ops;
354666b7793SArve Hjønnevåg 	qtty->base = base;
355666b7793SArve Hjønnevåg 	qtty->irq = irq;
356*7157d2beSMiodrag Dinic 	qtty->dev = &pdev->dev;
357*7157d2beSMiodrag Dinic 
358*7157d2beSMiodrag Dinic 	/*
359*7157d2beSMiodrag Dinic 	 * Goldfish TTY device used by the Goldfish emulator
360*7157d2beSMiodrag Dinic 	 * should identify itself with 0, forcing the driver
361*7157d2beSMiodrag Dinic 	 * to use virtual addresses. Goldfish TTY device
362*7157d2beSMiodrag Dinic 	 * on Ranchu emulator (qemu2) returns 1 here and
363*7157d2beSMiodrag Dinic 	 * driver will use physical addresses.
364*7157d2beSMiodrag Dinic 	 */
365*7157d2beSMiodrag Dinic 	qtty->version = readl(base + GOLDFISH_TTY_REG_VERSION);
366*7157d2beSMiodrag Dinic 
367*7157d2beSMiodrag Dinic 	/*
368*7157d2beSMiodrag Dinic 	 * Goldfish TTY device on Ranchu emulator (qemu2)
369*7157d2beSMiodrag Dinic 	 * will use DMA for read/write IO operations.
370*7157d2beSMiodrag Dinic 	 */
371*7157d2beSMiodrag Dinic 	if (qtty->version > 0) {
372*7157d2beSMiodrag Dinic 		/*
373*7157d2beSMiodrag Dinic 		 * Initialize dma_mask to 32-bits.
374*7157d2beSMiodrag Dinic 		 */
375*7157d2beSMiodrag Dinic 		if (!pdev->dev.dma_mask)
376*7157d2beSMiodrag Dinic 			pdev->dev.dma_mask = &pdev->dev.coherent_dma_mask;
377*7157d2beSMiodrag Dinic 		ret = dma_set_mask(&pdev->dev, DMA_BIT_MASK(32));
378*7157d2beSMiodrag Dinic 		if (ret) {
379*7157d2beSMiodrag Dinic 			dev_err(&pdev->dev, "No suitable DMA available.\n");
380*7157d2beSMiodrag Dinic 			goto err_dec_line_count;
381*7157d2beSMiodrag Dinic 		}
382*7157d2beSMiodrag Dinic 	}
383666b7793SArve Hjønnevåg 
3842296eee7SAleksandar Markovic 	writel(GOLDFISH_TTY_CMD_INT_DISABLE, base + GOLDFISH_TTY_REG_CMD);
385666b7793SArve Hjønnevåg 
386d78055dcSAlan 	ret = request_irq(irq, goldfish_tty_interrupt, IRQF_SHARED,
387465893e1SGreg Hackmann 			  "goldfish_tty", qtty);
388*7157d2beSMiodrag Dinic 	if (ret) {
389*7157d2beSMiodrag Dinic 		pr_err("goldfish_tty: No IRQ available!\n");
390*7157d2beSMiodrag Dinic 		goto err_dec_line_count;
391*7157d2beSMiodrag Dinic 	}
392666b7793SArve Hjønnevåg 
393666b7793SArve Hjønnevåg 	ttydev = tty_port_register_device(&qtty->port, goldfish_tty_driver,
394465893e1SGreg Hackmann 					  line, &pdev->dev);
395666b7793SArve Hjønnevåg 	if (IS_ERR(ttydev)) {
396666b7793SArve Hjønnevåg 		ret = PTR_ERR(ttydev);
397666b7793SArve Hjønnevåg 		goto err_tty_register_device_failed;
398666b7793SArve Hjønnevåg 	}
399666b7793SArve Hjønnevåg 
400666b7793SArve Hjønnevåg 	strcpy(qtty->console.name, "ttyGF");
401666b7793SArve Hjønnevåg 	qtty->console.write = goldfish_tty_console_write;
402666b7793SArve Hjønnevåg 	qtty->console.device = goldfish_tty_console_device;
403666b7793SArve Hjønnevåg 	qtty->console.setup = goldfish_tty_console_setup;
404666b7793SArve Hjønnevåg 	qtty->console.flags = CON_PRINTBUFFER;
405465893e1SGreg Hackmann 	qtty->console.index = line;
406666b7793SArve Hjønnevåg 	register_console(&qtty->console);
407465893e1SGreg Hackmann 	platform_set_drvdata(pdev, qtty);
408666b7793SArve Hjønnevåg 
409666b7793SArve Hjønnevåg 	mutex_unlock(&goldfish_tty_lock);
410666b7793SArve Hjønnevåg 	return 0;
411666b7793SArve Hjønnevåg 
412666b7793SArve Hjønnevåg err_tty_register_device_failed:
4131a5c2d1dSChristophe JAILLET 	free_irq(irq, qtty);
414*7157d2beSMiodrag Dinic err_dec_line_count:
415666b7793SArve Hjønnevåg 	goldfish_tty_current_line_count--;
416666b7793SArve Hjønnevåg 	if (goldfish_tty_current_line_count == 0)
417666b7793SArve Hjønnevåg 		goldfish_tty_delete_driver();
418*7157d2beSMiodrag Dinic err_unlock:
419666b7793SArve Hjønnevåg 	mutex_unlock(&goldfish_tty_lock);
420666b7793SArve Hjønnevåg err_unmap:
421666b7793SArve Hjønnevåg 	iounmap(base);
422666b7793SArve Hjønnevåg 	return ret;
423666b7793SArve Hjønnevåg }
424666b7793SArve Hjønnevåg 
425666b7793SArve Hjønnevåg static int goldfish_tty_remove(struct platform_device *pdev)
426666b7793SArve Hjønnevåg {
427465893e1SGreg Hackmann 	struct goldfish_tty *qtty = platform_get_drvdata(pdev);
428666b7793SArve Hjønnevåg 
429666b7793SArve Hjønnevåg 	mutex_lock(&goldfish_tty_lock);
430666b7793SArve Hjønnevåg 
431666b7793SArve Hjønnevåg 	unregister_console(&qtty->console);
432465893e1SGreg Hackmann 	tty_unregister_device(goldfish_tty_driver, qtty->console.index);
433666b7793SArve Hjønnevåg 	iounmap(qtty->base);
434a4dc9236SFabian Frederick 	qtty->base = NULL;
435666b7793SArve Hjønnevåg 	free_irq(qtty->irq, pdev);
436666b7793SArve Hjønnevåg 	goldfish_tty_current_line_count--;
437666b7793SArve Hjønnevåg 	if (goldfish_tty_current_line_count == 0)
438666b7793SArve Hjønnevåg 		goldfish_tty_delete_driver();
439666b7793SArve Hjønnevåg 	mutex_unlock(&goldfish_tty_lock);
440666b7793SArve Hjønnevåg 	return 0;
441666b7793SArve Hjønnevåg }
442666b7793SArve Hjønnevåg 
4439b883eeaSMiodrag Dinic static const struct of_device_id goldfish_tty_of_match[] = {
4449b883eeaSMiodrag Dinic 	{ .compatible = "google,goldfish-tty", },
4459b883eeaSMiodrag Dinic 	{},
4469b883eeaSMiodrag Dinic };
4479b883eeaSMiodrag Dinic 
4489b883eeaSMiodrag Dinic MODULE_DEVICE_TABLE(of, goldfish_tty_of_match);
4499b883eeaSMiodrag Dinic 
450666b7793SArve Hjønnevåg static struct platform_driver goldfish_tty_platform_driver = {
451666b7793SArve Hjønnevåg 	.probe = goldfish_tty_probe,
452666b7793SArve Hjønnevåg 	.remove = goldfish_tty_remove,
453666b7793SArve Hjønnevåg 	.driver = {
4549b883eeaSMiodrag Dinic 		.name = "goldfish_tty",
4559b883eeaSMiodrag Dinic 		.of_match_table = goldfish_tty_of_match,
456666b7793SArve Hjønnevåg 	}
457666b7793SArve Hjønnevåg };
458666b7793SArve Hjønnevåg 
459666b7793SArve Hjønnevåg module_platform_driver(goldfish_tty_platform_driver);
460666b7793SArve Hjønnevåg 
461666b7793SArve Hjønnevåg MODULE_LICENSE("GPL v2");
462