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
do_rw_io(struct goldfish_tty * qtty,unsigned long address,unsigned int count,int is_write)537157d2beSMiodrag Dinic static void do_rw_io(struct goldfish_tty *qtty,
547157d2beSMiodrag Dinic unsigned long address,
557157d2beSMiodrag Dinic unsigned int count,
567157d2beSMiodrag Dinic int is_write)
57666b7793SArve Hjønnevåg {
58666b7793SArve Hjønnevåg unsigned long irq_flags;
59666b7793SArve Hjønnevåg void __iomem *base = qtty->base;
607157d2beSMiodrag Dinic
61666b7793SArve Hjønnevåg spin_lock_irqsave(&qtty->lock, irq_flags);
627157d2beSMiodrag Dinic gf_write_ptr((void *)address, base + GOLDFISH_TTY_REG_DATA_PTR,
632296eee7SAleksandar Markovic base + GOLDFISH_TTY_REG_DATA_PTR_HIGH);
642e2ac4a3SLaurent Vivier gf_iowrite32(count, base + GOLDFISH_TTY_REG_DATA_LEN);
657157d2beSMiodrag Dinic
667157d2beSMiodrag Dinic if (is_write)
672e2ac4a3SLaurent Vivier gf_iowrite32(GOLDFISH_TTY_CMD_WRITE_BUFFER,
687157d2beSMiodrag Dinic base + GOLDFISH_TTY_REG_CMD);
697157d2beSMiodrag Dinic else
702e2ac4a3SLaurent Vivier gf_iowrite32(GOLDFISH_TTY_CMD_READ_BUFFER,
717157d2beSMiodrag Dinic base + GOLDFISH_TTY_REG_CMD);
727157d2beSMiodrag Dinic
73666b7793SArve Hjønnevåg spin_unlock_irqrestore(&qtty->lock, irq_flags);
74666b7793SArve Hjønnevåg }
75666b7793SArve Hjønnevåg
goldfish_tty_rw(struct goldfish_tty * qtty,unsigned long addr,unsigned int count,int is_write)767157d2beSMiodrag Dinic static void goldfish_tty_rw(struct goldfish_tty *qtty,
777157d2beSMiodrag Dinic unsigned long addr,
787157d2beSMiodrag Dinic unsigned int count,
797157d2beSMiodrag Dinic int is_write)
807157d2beSMiodrag Dinic {
817157d2beSMiodrag Dinic dma_addr_t dma_handle;
827157d2beSMiodrag Dinic enum dma_data_direction dma_dir;
837157d2beSMiodrag Dinic
847157d2beSMiodrag Dinic dma_dir = (is_write ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
857157d2beSMiodrag Dinic if (qtty->version > 0) {
867157d2beSMiodrag Dinic /*
877157d2beSMiodrag Dinic * Goldfish TTY for Ranchu platform uses
887157d2beSMiodrag Dinic * physical addresses and DMA for read/write operations
897157d2beSMiodrag Dinic */
907157d2beSMiodrag Dinic unsigned long addr_end = addr + count;
917157d2beSMiodrag Dinic
927157d2beSMiodrag Dinic while (addr < addr_end) {
937157d2beSMiodrag Dinic unsigned long pg_end = (addr & PAGE_MASK) + PAGE_SIZE;
947157d2beSMiodrag Dinic unsigned long next =
957157d2beSMiodrag Dinic pg_end < addr_end ? pg_end : addr_end;
967157d2beSMiodrag Dinic unsigned long avail = next - addr;
977157d2beSMiodrag Dinic
987157d2beSMiodrag Dinic /*
997157d2beSMiodrag Dinic * Map the buffer's virtual address to the DMA address
1007157d2beSMiodrag Dinic * so the buffer can be accessed by the device.
1017157d2beSMiodrag Dinic */
1027157d2beSMiodrag Dinic dma_handle = dma_map_single(qtty->dev, (void *)addr,
1037157d2beSMiodrag Dinic avail, dma_dir);
1047157d2beSMiodrag Dinic
1057157d2beSMiodrag Dinic if (dma_mapping_error(qtty->dev, dma_handle)) {
1067157d2beSMiodrag Dinic dev_err(qtty->dev, "tty: DMA mapping error.\n");
1077157d2beSMiodrag Dinic return;
1087157d2beSMiodrag Dinic }
1097157d2beSMiodrag Dinic do_rw_io(qtty, dma_handle, avail, is_write);
1107157d2beSMiodrag Dinic
1117157d2beSMiodrag Dinic /*
1127157d2beSMiodrag Dinic * Unmap the previously mapped region after
1137157d2beSMiodrag Dinic * the completion of the read/write operation.
1147157d2beSMiodrag Dinic */
1157157d2beSMiodrag Dinic dma_unmap_single(qtty->dev, dma_handle, avail, dma_dir);
1167157d2beSMiodrag Dinic
1177157d2beSMiodrag Dinic addr += avail;
1187157d2beSMiodrag Dinic }
1197157d2beSMiodrag Dinic } else {
1207157d2beSMiodrag Dinic /*
1217157d2beSMiodrag Dinic * Old style Goldfish TTY used on the Goldfish platform
1227157d2beSMiodrag Dinic * uses virtual addresses.
1237157d2beSMiodrag Dinic */
1247157d2beSMiodrag Dinic do_rw_io(qtty, addr, count, is_write);
1257157d2beSMiodrag Dinic }
1267157d2beSMiodrag Dinic }
1277157d2beSMiodrag Dinic
goldfish_tty_do_write(int line,const u8 * buf,unsigned int count)12869851e4aSJiri Slaby (SUSE) static void goldfish_tty_do_write(int line, const u8 *buf, unsigned int count)
1297157d2beSMiodrag Dinic {
1307157d2beSMiodrag Dinic struct goldfish_tty *qtty = &goldfish_ttys[line];
1317157d2beSMiodrag Dinic unsigned long address = (unsigned long)(void *)buf;
1327157d2beSMiodrag Dinic
1337157d2beSMiodrag Dinic goldfish_tty_rw(qtty, address, count, 1);
1347157d2beSMiodrag Dinic }
1357157d2beSMiodrag Dinic
goldfish_tty_interrupt(int irq,void * dev_id)136666b7793SArve Hjønnevåg static irqreturn_t goldfish_tty_interrupt(int irq, void *dev_id)
137666b7793SArve Hjønnevåg {
138465893e1SGreg Hackmann struct goldfish_tty *qtty = dev_id;
139666b7793SArve Hjønnevåg void __iomem *base = qtty->base;
1407157d2beSMiodrag Dinic unsigned long address;
141666b7793SArve Hjønnevåg unsigned char *buf;
142666b7793SArve Hjønnevåg u32 count;
143666b7793SArve Hjønnevåg
1442e2ac4a3SLaurent Vivier count = gf_ioread32(base + GOLDFISH_TTY_REG_BYTES_READY);
145666b7793SArve Hjønnevåg if (count == 0)
146666b7793SArve Hjønnevåg return IRQ_NONE;
147666b7793SArve Hjønnevåg
148ebcf0981SAlan Cox count = tty_prepare_flip_string(&qtty->port, &buf, count);
1497157d2beSMiodrag Dinic
1507157d2beSMiodrag Dinic address = (unsigned long)(void *)buf;
1517157d2beSMiodrag Dinic goldfish_tty_rw(qtty, address, count, 0);
1527157d2beSMiodrag Dinic
1535f6a8515SJiri Slaby tty_flip_buffer_push(&qtty->port);
154666b7793SArve Hjønnevåg return IRQ_HANDLED;
155666b7793SArve Hjønnevåg }
156666b7793SArve Hjønnevåg
goldfish_tty_activate(struct tty_port * port,struct tty_struct * tty)157666b7793SArve Hjønnevåg static int goldfish_tty_activate(struct tty_port *port, struct tty_struct *tty)
158666b7793SArve Hjønnevåg {
159d78055dcSAlan struct goldfish_tty *qtty = container_of(port, struct goldfish_tty,
160d78055dcSAlan port);
1612e2ac4a3SLaurent Vivier gf_iowrite32(GOLDFISH_TTY_CMD_INT_ENABLE, qtty->base + GOLDFISH_TTY_REG_CMD);
162666b7793SArve Hjønnevåg return 0;
163666b7793SArve Hjønnevåg }
164666b7793SArve Hjønnevåg
goldfish_tty_shutdown(struct tty_port * port)165666b7793SArve Hjønnevåg static void goldfish_tty_shutdown(struct tty_port *port)
166666b7793SArve Hjønnevåg {
167d78055dcSAlan struct goldfish_tty *qtty = container_of(port, struct goldfish_tty,
168d78055dcSAlan port);
1692e2ac4a3SLaurent Vivier gf_iowrite32(GOLDFISH_TTY_CMD_INT_DISABLE, qtty->base + GOLDFISH_TTY_REG_CMD);
170666b7793SArve Hjønnevåg }
171666b7793SArve Hjønnevåg
goldfish_tty_open(struct tty_struct * tty,struct file * filp)172666b7793SArve Hjønnevåg static int goldfish_tty_open(struct tty_struct *tty, struct file *filp)
173666b7793SArve Hjønnevåg {
174666b7793SArve Hjønnevåg struct goldfish_tty *qtty = &goldfish_ttys[tty->index];
175666b7793SArve Hjønnevåg return tty_port_open(&qtty->port, tty, filp);
176666b7793SArve Hjønnevåg }
177666b7793SArve Hjønnevåg
goldfish_tty_close(struct tty_struct * tty,struct file * filp)178666b7793SArve Hjønnevåg static void goldfish_tty_close(struct tty_struct *tty, struct file *filp)
179666b7793SArve Hjønnevåg {
180666b7793SArve Hjønnevåg tty_port_close(tty->port, tty, filp);
181666b7793SArve Hjønnevåg }
182666b7793SArve Hjønnevåg
goldfish_tty_hangup(struct tty_struct * tty)183666b7793SArve Hjønnevåg static void goldfish_tty_hangup(struct tty_struct *tty)
184666b7793SArve Hjønnevåg {
185666b7793SArve Hjønnevåg tty_port_hangup(tty->port);
186666b7793SArve Hjønnevåg }
187666b7793SArve Hjønnevåg
goldfish_tty_write(struct tty_struct * tty,const u8 * buf,size_t count)188*95713967SJiri Slaby (SUSE) static ssize_t goldfish_tty_write(struct tty_struct *tty, const u8 *buf,
189*95713967SJiri Slaby (SUSE) size_t count)
190666b7793SArve Hjønnevåg {
191666b7793SArve Hjønnevåg goldfish_tty_do_write(tty->index, buf, count);
192666b7793SArve Hjønnevåg return count;
193666b7793SArve Hjønnevåg }
194666b7793SArve Hjønnevåg
goldfish_tty_write_room(struct tty_struct * tty)19503b3b1a2SJiri Slaby static unsigned int goldfish_tty_write_room(struct tty_struct *tty)
196666b7793SArve Hjønnevåg {
197666b7793SArve Hjønnevåg return 0x10000;
198666b7793SArve Hjønnevåg }
199666b7793SArve Hjønnevåg
goldfish_tty_chars_in_buffer(struct tty_struct * tty)200fff4ef17SJiri Slaby static unsigned int goldfish_tty_chars_in_buffer(struct tty_struct *tty)
201666b7793SArve Hjønnevåg {
202666b7793SArve Hjønnevåg struct goldfish_tty *qtty = &goldfish_ttys[tty->index];
203666b7793SArve Hjønnevåg void __iomem *base = qtty->base;
2042e2ac4a3SLaurent Vivier return gf_ioread32(base + GOLDFISH_TTY_REG_BYTES_READY);
205666b7793SArve Hjønnevåg }
206666b7793SArve Hjønnevåg
goldfish_tty_console_write(struct console * co,const char * b,unsigned count)207d78055dcSAlan static void goldfish_tty_console_write(struct console *co, const char *b,
208d78055dcSAlan unsigned count)
209666b7793SArve Hjønnevåg {
210666b7793SArve Hjønnevåg goldfish_tty_do_write(co->index, b, count);
211666b7793SArve Hjønnevåg }
212666b7793SArve Hjønnevåg
goldfish_tty_console_device(struct console * c,int * index)213d78055dcSAlan static struct tty_driver *goldfish_tty_console_device(struct console *c,
214d78055dcSAlan int *index)
215666b7793SArve Hjønnevåg {
216666b7793SArve Hjønnevåg *index = c->index;
217666b7793SArve Hjønnevåg return goldfish_tty_driver;
218666b7793SArve Hjønnevåg }
219666b7793SArve Hjønnevåg
goldfish_tty_console_setup(struct console * co,char * options)220666b7793SArve Hjønnevåg static int goldfish_tty_console_setup(struct console *co, char *options)
221666b7793SArve Hjønnevåg {
222fda2b418SDan Carpenter if ((unsigned)co->index >= goldfish_tty_line_count)
223666b7793SArve Hjønnevåg return -ENODEV;
224a4dc9236SFabian Frederick if (!goldfish_ttys[co->index].base)
225666b7793SArve Hjønnevåg return -ENODEV;
226666b7793SArve Hjønnevåg return 0;
227666b7793SArve Hjønnevåg }
228666b7793SArve Hjønnevåg
22904b757dfSAya Mahfouz static const struct tty_port_operations goldfish_port_ops = {
230666b7793SArve Hjønnevåg .activate = goldfish_tty_activate,
231666b7793SArve Hjønnevåg .shutdown = goldfish_tty_shutdown
232666b7793SArve Hjønnevåg };
233666b7793SArve Hjønnevåg
234d78055dcSAlan static const struct tty_operations goldfish_tty_ops = {
235666b7793SArve Hjønnevåg .open = goldfish_tty_open,
236666b7793SArve Hjønnevåg .close = goldfish_tty_close,
237666b7793SArve Hjønnevåg .hangup = goldfish_tty_hangup,
238666b7793SArve Hjønnevåg .write = goldfish_tty_write,
239666b7793SArve Hjønnevåg .write_room = goldfish_tty_write_room,
240666b7793SArve Hjønnevåg .chars_in_buffer = goldfish_tty_chars_in_buffer,
241666b7793SArve Hjønnevåg };
242666b7793SArve Hjønnevåg
goldfish_tty_create_driver(void)243666b7793SArve Hjønnevåg static int goldfish_tty_create_driver(void)
244666b7793SArve Hjønnevåg {
245666b7793SArve Hjønnevåg int ret;
246666b7793SArve Hjønnevåg struct tty_driver *tty;
247666b7793SArve Hjønnevåg
2486396bb22SKees Cook goldfish_ttys = kcalloc(goldfish_tty_line_count,
2496396bb22SKees Cook sizeof(*goldfish_ttys),
2506396bb22SKees Cook GFP_KERNEL);
251666b7793SArve Hjønnevåg if (goldfish_ttys == NULL) {
252666b7793SArve Hjønnevåg ret = -ENOMEM;
253666b7793SArve Hjønnevåg goto err_alloc_goldfish_ttys_failed;
254666b7793SArve Hjønnevåg }
25539b7b42bSJiri Slaby tty = tty_alloc_driver(goldfish_tty_line_count,
25639b7b42bSJiri Slaby TTY_DRIVER_RESET_TERMIOS | TTY_DRIVER_REAL_RAW |
25739b7b42bSJiri Slaby TTY_DRIVER_DYNAMIC_DEV);
25839b7b42bSJiri Slaby if (IS_ERR(tty)) {
25939b7b42bSJiri Slaby ret = PTR_ERR(tty);
26039b7b42bSJiri Slaby goto err_tty_alloc_driver_failed;
261666b7793SArve Hjønnevåg }
262666b7793SArve Hjønnevåg tty->driver_name = "goldfish";
263666b7793SArve Hjønnevåg tty->name = "ttyGF";
264666b7793SArve Hjønnevåg tty->type = TTY_DRIVER_TYPE_SERIAL;
265666b7793SArve Hjønnevåg tty->subtype = SERIAL_TYPE_NORMAL;
266666b7793SArve Hjønnevåg tty->init_termios = tty_std_termios;
267666b7793SArve Hjønnevåg tty_set_operations(tty, &goldfish_tty_ops);
268666b7793SArve Hjønnevåg ret = tty_register_driver(tty);
269666b7793SArve Hjønnevåg if (ret)
270666b7793SArve Hjønnevåg goto err_tty_register_driver_failed;
271666b7793SArve Hjønnevåg
272666b7793SArve Hjønnevåg goldfish_tty_driver = tty;
273666b7793SArve Hjønnevåg return 0;
274666b7793SArve Hjønnevåg
275666b7793SArve Hjønnevåg err_tty_register_driver_failed:
2769f90a4ddSJiri Slaby tty_driver_kref_put(tty);
27739b7b42bSJiri Slaby err_tty_alloc_driver_failed:
278666b7793SArve Hjønnevåg kfree(goldfish_ttys);
279666b7793SArve Hjønnevåg goldfish_ttys = NULL;
280666b7793SArve Hjønnevåg err_alloc_goldfish_ttys_failed:
281666b7793SArve Hjønnevåg return ret;
282666b7793SArve Hjønnevåg }
283666b7793SArve Hjønnevåg
goldfish_tty_delete_driver(void)284666b7793SArve Hjønnevåg static void goldfish_tty_delete_driver(void)
285666b7793SArve Hjønnevåg {
286666b7793SArve Hjønnevåg tty_unregister_driver(goldfish_tty_driver);
2879f90a4ddSJiri Slaby tty_driver_kref_put(goldfish_tty_driver);
288666b7793SArve Hjønnevåg goldfish_tty_driver = NULL;
289666b7793SArve Hjønnevåg kfree(goldfish_ttys);
290666b7793SArve Hjønnevåg goldfish_ttys = NULL;
291666b7793SArve Hjønnevåg }
292666b7793SArve Hjønnevåg
goldfish_tty_probe(struct platform_device * pdev)293666b7793SArve Hjønnevåg static int goldfish_tty_probe(struct platform_device *pdev)
294666b7793SArve Hjønnevåg {
295666b7793SArve Hjønnevåg struct goldfish_tty *qtty;
2967157d2beSMiodrag Dinic int ret = -ENODEV;
297666b7793SArve Hjønnevåg struct resource *r;
298666b7793SArve Hjønnevåg struct device *ttydev;
299666b7793SArve Hjønnevåg void __iomem *base;
3005acb78dcSLad Prabhakar int irq;
301465893e1SGreg Hackmann unsigned int line;
302666b7793SArve Hjønnevåg
303666b7793SArve Hjønnevåg r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
3047157d2beSMiodrag Dinic if (!r) {
3057157d2beSMiodrag Dinic pr_err("goldfish_tty: No MEM resource available!\n");
3067157d2beSMiodrag Dinic return -ENOMEM;
3077157d2beSMiodrag Dinic }
308666b7793SArve Hjønnevåg
309666b7793SArve Hjønnevåg base = ioremap(r->start, 0x1000);
3107157d2beSMiodrag Dinic if (!base) {
3117157d2beSMiodrag Dinic pr_err("goldfish_tty: Unable to ioremap base!\n");
3127157d2beSMiodrag Dinic return -ENOMEM;
3137157d2beSMiodrag Dinic }
314666b7793SArve Hjønnevåg
3155acb78dcSLad Prabhakar irq = platform_get_irq(pdev, 0);
3165acb78dcSLad Prabhakar if (irq < 0) {
3175acb78dcSLad Prabhakar ret = irq;
318666b7793SArve Hjønnevåg goto err_unmap;
3197157d2beSMiodrag Dinic }
320666b7793SArve Hjønnevåg
321666b7793SArve Hjønnevåg mutex_lock(&goldfish_tty_lock);
322465893e1SGreg Hackmann
323465893e1SGreg Hackmann if (pdev->id == PLATFORM_DEVID_NONE)
324465893e1SGreg Hackmann line = goldfish_tty_current_line_count;
325465893e1SGreg Hackmann else
326465893e1SGreg Hackmann line = pdev->id;
327465893e1SGreg Hackmann
3287157d2beSMiodrag Dinic if (line >= goldfish_tty_line_count) {
3297157d2beSMiodrag Dinic pr_err("goldfish_tty: Reached maximum tty number of %d.\n",
3307157d2beSMiodrag Dinic goldfish_tty_current_line_count);
3317157d2beSMiodrag Dinic ret = -ENOMEM;
3327157d2beSMiodrag Dinic goto err_unlock;
3337157d2beSMiodrag Dinic }
334465893e1SGreg Hackmann
335666b7793SArve Hjønnevåg if (goldfish_tty_current_line_count == 0) {
336666b7793SArve Hjønnevåg ret = goldfish_tty_create_driver();
337666b7793SArve Hjønnevåg if (ret)
3387157d2beSMiodrag Dinic goto err_unlock;
339666b7793SArve Hjønnevåg }
340666b7793SArve Hjønnevåg goldfish_tty_current_line_count++;
341666b7793SArve Hjønnevåg
342465893e1SGreg Hackmann qtty = &goldfish_ttys[line];
343666b7793SArve Hjønnevåg spin_lock_init(&qtty->lock);
344666b7793SArve Hjønnevåg tty_port_init(&qtty->port);
345666b7793SArve Hjønnevåg qtty->port.ops = &goldfish_port_ops;
346666b7793SArve Hjønnevåg qtty->base = base;
347666b7793SArve Hjønnevåg qtty->irq = irq;
3487157d2beSMiodrag Dinic qtty->dev = &pdev->dev;
3497157d2beSMiodrag Dinic
3507157d2beSMiodrag Dinic /*
3517157d2beSMiodrag Dinic * Goldfish TTY device used by the Goldfish emulator
3527157d2beSMiodrag Dinic * should identify itself with 0, forcing the driver
3537157d2beSMiodrag Dinic * to use virtual addresses. Goldfish TTY device
3547157d2beSMiodrag Dinic * on Ranchu emulator (qemu2) returns 1 here and
3557157d2beSMiodrag Dinic * driver will use physical addresses.
3567157d2beSMiodrag Dinic */
3572e2ac4a3SLaurent Vivier qtty->version = gf_ioread32(base + GOLDFISH_TTY_REG_VERSION);
3587157d2beSMiodrag Dinic
3597157d2beSMiodrag Dinic /*
3607157d2beSMiodrag Dinic * Goldfish TTY device on Ranchu emulator (qemu2)
3617157d2beSMiodrag Dinic * will use DMA for read/write IO operations.
3627157d2beSMiodrag Dinic */
3637157d2beSMiodrag Dinic if (qtty->version > 0) {
3647157d2beSMiodrag Dinic /*
3657157d2beSMiodrag Dinic * Initialize dma_mask to 32-bits.
3667157d2beSMiodrag Dinic */
3677157d2beSMiodrag Dinic if (!pdev->dev.dma_mask)
3687157d2beSMiodrag Dinic pdev->dev.dma_mask = &pdev->dev.coherent_dma_mask;
3697157d2beSMiodrag Dinic ret = dma_set_mask(&pdev->dev, DMA_BIT_MASK(32));
3707157d2beSMiodrag Dinic if (ret) {
3717157d2beSMiodrag Dinic dev_err(&pdev->dev, "No suitable DMA available.\n");
3727157d2beSMiodrag Dinic goto err_dec_line_count;
3737157d2beSMiodrag Dinic }
3747157d2beSMiodrag Dinic }
375666b7793SArve Hjønnevåg
3762e2ac4a3SLaurent Vivier gf_iowrite32(GOLDFISH_TTY_CMD_INT_DISABLE, base + GOLDFISH_TTY_REG_CMD);
377666b7793SArve Hjønnevåg
378d78055dcSAlan ret = request_irq(irq, goldfish_tty_interrupt, IRQF_SHARED,
379465893e1SGreg Hackmann "goldfish_tty", qtty);
3807157d2beSMiodrag Dinic if (ret) {
3817157d2beSMiodrag Dinic pr_err("goldfish_tty: No IRQ available!\n");
3827157d2beSMiodrag Dinic goto err_dec_line_count;
3837157d2beSMiodrag Dinic }
384666b7793SArve Hjønnevåg
385666b7793SArve Hjønnevåg ttydev = tty_port_register_device(&qtty->port, goldfish_tty_driver,
386465893e1SGreg Hackmann line, &pdev->dev);
387666b7793SArve Hjønnevåg if (IS_ERR(ttydev)) {
388666b7793SArve Hjønnevåg ret = PTR_ERR(ttydev);
389666b7793SArve Hjønnevåg goto err_tty_register_device_failed;
390666b7793SArve Hjønnevåg }
391666b7793SArve Hjønnevåg
392666b7793SArve Hjønnevåg strcpy(qtty->console.name, "ttyGF");
393666b7793SArve Hjønnevåg qtty->console.write = goldfish_tty_console_write;
394666b7793SArve Hjønnevåg qtty->console.device = goldfish_tty_console_device;
395666b7793SArve Hjønnevåg qtty->console.setup = goldfish_tty_console_setup;
396666b7793SArve Hjønnevåg qtty->console.flags = CON_PRINTBUFFER;
397465893e1SGreg Hackmann qtty->console.index = line;
398666b7793SArve Hjønnevåg register_console(&qtty->console);
399465893e1SGreg Hackmann platform_set_drvdata(pdev, qtty);
400666b7793SArve Hjønnevåg
401666b7793SArve Hjønnevåg mutex_unlock(&goldfish_tty_lock);
402666b7793SArve Hjønnevåg return 0;
403666b7793SArve Hjønnevåg
404666b7793SArve Hjønnevåg err_tty_register_device_failed:
4051a5c2d1dSChristophe JAILLET free_irq(irq, qtty);
4067157d2beSMiodrag Dinic err_dec_line_count:
407507b0506SWang Weiyang tty_port_destroy(&qtty->port);
408666b7793SArve Hjønnevåg goldfish_tty_current_line_count--;
409666b7793SArve Hjønnevåg if (goldfish_tty_current_line_count == 0)
410666b7793SArve Hjønnevåg goldfish_tty_delete_driver();
4117157d2beSMiodrag Dinic err_unlock:
412666b7793SArve Hjønnevåg mutex_unlock(&goldfish_tty_lock);
413666b7793SArve Hjønnevåg err_unmap:
414666b7793SArve Hjønnevåg iounmap(base);
415666b7793SArve Hjønnevåg return ret;
416666b7793SArve Hjønnevåg }
417666b7793SArve Hjønnevåg
goldfish_tty_remove(struct platform_device * pdev)418666b7793SArve Hjønnevåg static int goldfish_tty_remove(struct platform_device *pdev)
419666b7793SArve Hjønnevåg {
420465893e1SGreg Hackmann struct goldfish_tty *qtty = platform_get_drvdata(pdev);
421666b7793SArve Hjønnevåg
422666b7793SArve Hjønnevåg mutex_lock(&goldfish_tty_lock);
423666b7793SArve Hjønnevåg
424666b7793SArve Hjønnevåg unregister_console(&qtty->console);
425465893e1SGreg Hackmann tty_unregister_device(goldfish_tty_driver, qtty->console.index);
426666b7793SArve Hjønnevåg iounmap(qtty->base);
427a4dc9236SFabian Frederick qtty->base = NULL;
428499e13aaSVincent Whitchurch free_irq(qtty->irq, qtty);
429507b0506SWang Weiyang tty_port_destroy(&qtty->port);
430666b7793SArve Hjønnevåg goldfish_tty_current_line_count--;
431666b7793SArve Hjønnevåg if (goldfish_tty_current_line_count == 0)
432666b7793SArve Hjønnevåg goldfish_tty_delete_driver();
433666b7793SArve Hjønnevåg mutex_unlock(&goldfish_tty_lock);
434666b7793SArve Hjønnevåg return 0;
435666b7793SArve Hjønnevåg }
436666b7793SArve Hjønnevåg
4376a28fd2bSSebastian Andrzej Siewior #ifdef CONFIG_GOLDFISH_TTY_EARLY_CONSOLE
gf_early_console_putchar(struct uart_port * port,unsigned char ch)4383f8bab17SJiri Slaby static void gf_early_console_putchar(struct uart_port *port, unsigned char ch)
4393840ed95SMiodrag Dinic {
4402e2ac4a3SLaurent Vivier gf_iowrite32(ch, port->membase);
4413840ed95SMiodrag Dinic }
4423840ed95SMiodrag Dinic
gf_early_write(struct console * con,const char * s,unsigned int n)4433840ed95SMiodrag Dinic static void gf_early_write(struct console *con, const char *s, unsigned int n)
4443840ed95SMiodrag Dinic {
4453840ed95SMiodrag Dinic struct earlycon_device *dev = con->data;
4463840ed95SMiodrag Dinic
4473840ed95SMiodrag Dinic uart_console_write(&dev->port, s, n, gf_early_console_putchar);
4483840ed95SMiodrag Dinic }
4493840ed95SMiodrag Dinic
gf_earlycon_setup(struct earlycon_device * device,const char * opt)4503840ed95SMiodrag Dinic static int __init gf_earlycon_setup(struct earlycon_device *device,
4513840ed95SMiodrag Dinic const char *opt)
4523840ed95SMiodrag Dinic {
4533840ed95SMiodrag Dinic if (!device->port.membase)
4543840ed95SMiodrag Dinic return -ENODEV;
4553840ed95SMiodrag Dinic
4563840ed95SMiodrag Dinic device->con->write = gf_early_write;
4573840ed95SMiodrag Dinic return 0;
4583840ed95SMiodrag Dinic }
4593840ed95SMiodrag Dinic
4603840ed95SMiodrag Dinic OF_EARLYCON_DECLARE(early_gf_tty, "google,goldfish-tty", gf_earlycon_setup);
4616a28fd2bSSebastian Andrzej Siewior #endif
4623840ed95SMiodrag Dinic
4639b883eeaSMiodrag Dinic static const struct of_device_id goldfish_tty_of_match[] = {
4649b883eeaSMiodrag Dinic { .compatible = "google,goldfish-tty", },
4659b883eeaSMiodrag Dinic {},
4669b883eeaSMiodrag Dinic };
4679b883eeaSMiodrag Dinic
4689b883eeaSMiodrag Dinic MODULE_DEVICE_TABLE(of, goldfish_tty_of_match);
4699b883eeaSMiodrag Dinic
470666b7793SArve Hjønnevåg static struct platform_driver goldfish_tty_platform_driver = {
471666b7793SArve Hjønnevåg .probe = goldfish_tty_probe,
472666b7793SArve Hjønnevåg .remove = goldfish_tty_remove,
473666b7793SArve Hjønnevåg .driver = {
4749b883eeaSMiodrag Dinic .name = "goldfish_tty",
4759b883eeaSMiodrag Dinic .of_match_table = goldfish_tty_of_match,
476666b7793SArve Hjønnevåg }
477666b7793SArve Hjønnevåg };
478666b7793SArve Hjønnevåg
479666b7793SArve Hjønnevåg module_platform_driver(goldfish_tty_platform_driver);
480666b7793SArve Hjønnevåg
481666b7793SArve Hjønnevåg MODULE_LICENSE("GPL v2");
482