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 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); 64da31de35SLaurent Vivier __raw_writel(count, base + GOLDFISH_TTY_REG_DATA_LEN); 657157d2beSMiodrag Dinic 667157d2beSMiodrag Dinic if (is_write) 67da31de35SLaurent Vivier __raw_writel(GOLDFISH_TTY_CMD_WRITE_BUFFER, 687157d2beSMiodrag Dinic base + GOLDFISH_TTY_REG_CMD); 697157d2beSMiodrag Dinic else 70da31de35SLaurent Vivier __raw_writel(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 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 1287157d2beSMiodrag Dinic static void goldfish_tty_do_write(int line, const char *buf, 1297157d2beSMiodrag Dinic unsigned int count) 1307157d2beSMiodrag Dinic { 1317157d2beSMiodrag Dinic struct goldfish_tty *qtty = &goldfish_ttys[line]; 1327157d2beSMiodrag Dinic unsigned long address = (unsigned long)(void *)buf; 1337157d2beSMiodrag Dinic 1347157d2beSMiodrag Dinic goldfish_tty_rw(qtty, address, count, 1); 1357157d2beSMiodrag Dinic } 1367157d2beSMiodrag Dinic 137666b7793SArve Hjønnevåg static irqreturn_t goldfish_tty_interrupt(int irq, void *dev_id) 138666b7793SArve Hjønnevåg { 139465893e1SGreg Hackmann struct goldfish_tty *qtty = dev_id; 140666b7793SArve Hjønnevåg void __iomem *base = qtty->base; 1417157d2beSMiodrag Dinic unsigned long address; 142666b7793SArve Hjønnevåg unsigned char *buf; 143666b7793SArve Hjønnevåg u32 count; 144666b7793SArve Hjønnevåg 145da31de35SLaurent Vivier count = __raw_readl(base + GOLDFISH_TTY_REG_BYTES_READY); 146666b7793SArve Hjønnevåg if (count == 0) 147666b7793SArve Hjønnevåg return IRQ_NONE; 148666b7793SArve Hjønnevåg 149ebcf0981SAlan Cox count = tty_prepare_flip_string(&qtty->port, &buf, count); 1507157d2beSMiodrag Dinic 1517157d2beSMiodrag Dinic address = (unsigned long)(void *)buf; 1527157d2beSMiodrag Dinic goldfish_tty_rw(qtty, address, count, 0); 1537157d2beSMiodrag Dinic 154*5f6a8515SJiri Slaby tty_flip_buffer_push(&qtty->port); 155666b7793SArve Hjønnevåg return IRQ_HANDLED; 156666b7793SArve Hjønnevåg } 157666b7793SArve Hjønnevåg 158666b7793SArve Hjønnevåg static int goldfish_tty_activate(struct tty_port *port, struct tty_struct *tty) 159666b7793SArve Hjønnevåg { 160d78055dcSAlan struct goldfish_tty *qtty = container_of(port, struct goldfish_tty, 161d78055dcSAlan port); 162da31de35SLaurent Vivier __raw_writel(GOLDFISH_TTY_CMD_INT_ENABLE, qtty->base + GOLDFISH_TTY_REG_CMD); 163666b7793SArve Hjønnevåg return 0; 164666b7793SArve Hjønnevåg } 165666b7793SArve Hjønnevåg 166666b7793SArve Hjønnevåg static void goldfish_tty_shutdown(struct tty_port *port) 167666b7793SArve Hjønnevåg { 168d78055dcSAlan struct goldfish_tty *qtty = container_of(port, struct goldfish_tty, 169d78055dcSAlan port); 170da31de35SLaurent Vivier __raw_writel(GOLDFISH_TTY_CMD_INT_DISABLE, qtty->base + GOLDFISH_TTY_REG_CMD); 171666b7793SArve Hjønnevåg } 172666b7793SArve Hjønnevåg 173666b7793SArve Hjønnevåg static int goldfish_tty_open(struct tty_struct *tty, struct file *filp) 174666b7793SArve Hjønnevåg { 175666b7793SArve Hjønnevåg struct goldfish_tty *qtty = &goldfish_ttys[tty->index]; 176666b7793SArve Hjønnevåg return tty_port_open(&qtty->port, tty, filp); 177666b7793SArve Hjønnevåg } 178666b7793SArve Hjønnevåg 179666b7793SArve Hjønnevåg static void goldfish_tty_close(struct tty_struct *tty, struct file *filp) 180666b7793SArve Hjønnevåg { 181666b7793SArve Hjønnevåg tty_port_close(tty->port, tty, filp); 182666b7793SArve Hjønnevåg } 183666b7793SArve Hjønnevåg 184666b7793SArve Hjønnevåg static void goldfish_tty_hangup(struct tty_struct *tty) 185666b7793SArve Hjønnevåg { 186666b7793SArve Hjønnevåg tty_port_hangup(tty->port); 187666b7793SArve Hjønnevåg } 188666b7793SArve Hjønnevåg 189d78055dcSAlan static int goldfish_tty_write(struct tty_struct *tty, const unsigned char *buf, 190d78055dcSAlan int count) 191666b7793SArve Hjønnevåg { 192666b7793SArve Hjønnevåg goldfish_tty_do_write(tty->index, buf, count); 193666b7793SArve Hjønnevåg return count; 194666b7793SArve Hjønnevåg } 195666b7793SArve Hjønnevåg 19603b3b1a2SJiri Slaby static unsigned int goldfish_tty_write_room(struct tty_struct *tty) 197666b7793SArve Hjønnevåg { 198666b7793SArve Hjønnevåg return 0x10000; 199666b7793SArve Hjønnevåg } 200666b7793SArve Hjønnevåg 201fff4ef17SJiri Slaby static unsigned int goldfish_tty_chars_in_buffer(struct tty_struct *tty) 202666b7793SArve Hjønnevåg { 203666b7793SArve Hjønnevåg struct goldfish_tty *qtty = &goldfish_ttys[tty->index]; 204666b7793SArve Hjønnevåg void __iomem *base = qtty->base; 205da31de35SLaurent Vivier return __raw_readl(base + GOLDFISH_TTY_REG_BYTES_READY); 206666b7793SArve Hjønnevåg } 207666b7793SArve Hjønnevåg 208d78055dcSAlan static void goldfish_tty_console_write(struct console *co, const char *b, 209d78055dcSAlan unsigned count) 210666b7793SArve Hjønnevåg { 211666b7793SArve Hjønnevåg goldfish_tty_do_write(co->index, b, count); 212666b7793SArve Hjønnevåg } 213666b7793SArve Hjønnevåg 214d78055dcSAlan static struct tty_driver *goldfish_tty_console_device(struct console *c, 215d78055dcSAlan int *index) 216666b7793SArve Hjønnevåg { 217666b7793SArve Hjønnevåg *index = c->index; 218666b7793SArve Hjønnevåg return goldfish_tty_driver; 219666b7793SArve Hjønnevåg } 220666b7793SArve Hjønnevåg 221666b7793SArve Hjønnevåg static int goldfish_tty_console_setup(struct console *co, char *options) 222666b7793SArve Hjønnevåg { 223fda2b418SDan Carpenter if ((unsigned)co->index >= goldfish_tty_line_count) 224666b7793SArve Hjønnevåg return -ENODEV; 225a4dc9236SFabian Frederick if (!goldfish_ttys[co->index].base) 226666b7793SArve Hjønnevåg return -ENODEV; 227666b7793SArve Hjønnevåg return 0; 228666b7793SArve Hjønnevåg } 229666b7793SArve Hjønnevåg 23004b757dfSAya Mahfouz static const struct tty_port_operations goldfish_port_ops = { 231666b7793SArve Hjønnevåg .activate = goldfish_tty_activate, 232666b7793SArve Hjønnevåg .shutdown = goldfish_tty_shutdown 233666b7793SArve Hjønnevåg }; 234666b7793SArve Hjønnevåg 235d78055dcSAlan static const struct tty_operations goldfish_tty_ops = { 236666b7793SArve Hjønnevåg .open = goldfish_tty_open, 237666b7793SArve Hjønnevåg .close = goldfish_tty_close, 238666b7793SArve Hjønnevåg .hangup = goldfish_tty_hangup, 239666b7793SArve Hjønnevåg .write = goldfish_tty_write, 240666b7793SArve Hjønnevåg .write_room = goldfish_tty_write_room, 241666b7793SArve Hjønnevåg .chars_in_buffer = goldfish_tty_chars_in_buffer, 242666b7793SArve Hjønnevåg }; 243666b7793SArve Hjønnevåg 244666b7793SArve Hjønnevåg static int goldfish_tty_create_driver(void) 245666b7793SArve Hjønnevåg { 246666b7793SArve Hjønnevåg int ret; 247666b7793SArve Hjønnevåg struct tty_driver *tty; 248666b7793SArve Hjønnevåg 2496396bb22SKees Cook goldfish_ttys = kcalloc(goldfish_tty_line_count, 2506396bb22SKees Cook sizeof(*goldfish_ttys), 2516396bb22SKees Cook GFP_KERNEL); 252666b7793SArve Hjønnevåg if (goldfish_ttys == NULL) { 253666b7793SArve Hjønnevåg ret = -ENOMEM; 254666b7793SArve Hjønnevåg goto err_alloc_goldfish_ttys_failed; 255666b7793SArve Hjønnevåg } 25639b7b42bSJiri Slaby tty = tty_alloc_driver(goldfish_tty_line_count, 25739b7b42bSJiri Slaby TTY_DRIVER_RESET_TERMIOS | TTY_DRIVER_REAL_RAW | 25839b7b42bSJiri Slaby TTY_DRIVER_DYNAMIC_DEV); 25939b7b42bSJiri Slaby if (IS_ERR(tty)) { 26039b7b42bSJiri Slaby ret = PTR_ERR(tty); 26139b7b42bSJiri Slaby goto err_tty_alloc_driver_failed; 262666b7793SArve Hjønnevåg } 263666b7793SArve Hjønnevåg tty->driver_name = "goldfish"; 264666b7793SArve Hjønnevåg tty->name = "ttyGF"; 265666b7793SArve Hjønnevåg tty->type = TTY_DRIVER_TYPE_SERIAL; 266666b7793SArve Hjønnevåg tty->subtype = SERIAL_TYPE_NORMAL; 267666b7793SArve Hjønnevåg tty->init_termios = tty_std_termios; 268666b7793SArve Hjønnevåg tty_set_operations(tty, &goldfish_tty_ops); 269666b7793SArve Hjønnevåg ret = tty_register_driver(tty); 270666b7793SArve Hjønnevåg if (ret) 271666b7793SArve Hjønnevåg goto err_tty_register_driver_failed; 272666b7793SArve Hjønnevåg 273666b7793SArve Hjønnevåg goldfish_tty_driver = tty; 274666b7793SArve Hjønnevåg return 0; 275666b7793SArve Hjønnevåg 276666b7793SArve Hjønnevåg err_tty_register_driver_failed: 2779f90a4ddSJiri Slaby tty_driver_kref_put(tty); 27839b7b42bSJiri Slaby err_tty_alloc_driver_failed: 279666b7793SArve Hjønnevåg kfree(goldfish_ttys); 280666b7793SArve Hjønnevåg goldfish_ttys = NULL; 281666b7793SArve Hjønnevåg err_alloc_goldfish_ttys_failed: 282666b7793SArve Hjønnevåg return ret; 283666b7793SArve Hjønnevåg } 284666b7793SArve Hjønnevåg 285666b7793SArve Hjønnevåg static void goldfish_tty_delete_driver(void) 286666b7793SArve Hjønnevåg { 287666b7793SArve Hjønnevåg tty_unregister_driver(goldfish_tty_driver); 2889f90a4ddSJiri Slaby tty_driver_kref_put(goldfish_tty_driver); 289666b7793SArve Hjønnevåg goldfish_tty_driver = NULL; 290666b7793SArve Hjønnevåg kfree(goldfish_ttys); 291666b7793SArve Hjønnevåg goldfish_ttys = NULL; 292666b7793SArve Hjønnevåg } 293666b7793SArve Hjønnevåg 294666b7793SArve Hjønnevåg static int goldfish_tty_probe(struct platform_device *pdev) 295666b7793SArve Hjønnevåg { 296666b7793SArve Hjønnevåg struct goldfish_tty *qtty; 2977157d2beSMiodrag Dinic int ret = -ENODEV; 298666b7793SArve Hjønnevåg struct resource *r; 299666b7793SArve Hjønnevåg struct device *ttydev; 300666b7793SArve Hjønnevåg void __iomem *base; 301666b7793SArve Hjønnevåg u32 irq; 302465893e1SGreg Hackmann unsigned int line; 303666b7793SArve Hjønnevåg 304666b7793SArve Hjønnevåg r = platform_get_resource(pdev, IORESOURCE_MEM, 0); 3057157d2beSMiodrag Dinic if (!r) { 3067157d2beSMiodrag Dinic pr_err("goldfish_tty: No MEM resource available!\n"); 3077157d2beSMiodrag Dinic return -ENOMEM; 3087157d2beSMiodrag Dinic } 309666b7793SArve Hjønnevåg 310666b7793SArve Hjønnevåg base = ioremap(r->start, 0x1000); 3117157d2beSMiodrag Dinic if (!base) { 3127157d2beSMiodrag Dinic pr_err("goldfish_tty: Unable to ioremap base!\n"); 3137157d2beSMiodrag Dinic return -ENOMEM; 3147157d2beSMiodrag Dinic } 315666b7793SArve Hjønnevåg 316666b7793SArve Hjønnevåg r = platform_get_resource(pdev, IORESOURCE_IRQ, 0); 3177157d2beSMiodrag Dinic if (!r) { 3187157d2beSMiodrag Dinic pr_err("goldfish_tty: No IRQ resource available!\n"); 319666b7793SArve Hjønnevåg goto err_unmap; 3207157d2beSMiodrag Dinic } 321666b7793SArve Hjønnevåg 322666b7793SArve Hjønnevåg irq = r->start; 323666b7793SArve Hjønnevåg 324666b7793SArve Hjønnevåg mutex_lock(&goldfish_tty_lock); 325465893e1SGreg Hackmann 326465893e1SGreg Hackmann if (pdev->id == PLATFORM_DEVID_NONE) 327465893e1SGreg Hackmann line = goldfish_tty_current_line_count; 328465893e1SGreg Hackmann else 329465893e1SGreg Hackmann line = pdev->id; 330465893e1SGreg Hackmann 3317157d2beSMiodrag Dinic if (line >= goldfish_tty_line_count) { 3327157d2beSMiodrag Dinic pr_err("goldfish_tty: Reached maximum tty number of %d.\n", 3337157d2beSMiodrag Dinic goldfish_tty_current_line_count); 3347157d2beSMiodrag Dinic ret = -ENOMEM; 3357157d2beSMiodrag Dinic goto err_unlock; 3367157d2beSMiodrag Dinic } 337465893e1SGreg Hackmann 338666b7793SArve Hjønnevåg if (goldfish_tty_current_line_count == 0) { 339666b7793SArve Hjønnevåg ret = goldfish_tty_create_driver(); 340666b7793SArve Hjønnevåg if (ret) 3417157d2beSMiodrag Dinic goto err_unlock; 342666b7793SArve Hjønnevåg } 343666b7793SArve Hjønnevåg goldfish_tty_current_line_count++; 344666b7793SArve Hjønnevåg 345465893e1SGreg Hackmann qtty = &goldfish_ttys[line]; 346666b7793SArve Hjønnevåg spin_lock_init(&qtty->lock); 347666b7793SArve Hjønnevåg tty_port_init(&qtty->port); 348666b7793SArve Hjønnevåg qtty->port.ops = &goldfish_port_ops; 349666b7793SArve Hjønnevåg qtty->base = base; 350666b7793SArve Hjønnevåg qtty->irq = irq; 3517157d2beSMiodrag Dinic qtty->dev = &pdev->dev; 3527157d2beSMiodrag Dinic 3537157d2beSMiodrag Dinic /* 3547157d2beSMiodrag Dinic * Goldfish TTY device used by the Goldfish emulator 3557157d2beSMiodrag Dinic * should identify itself with 0, forcing the driver 3567157d2beSMiodrag Dinic * to use virtual addresses. Goldfish TTY device 3577157d2beSMiodrag Dinic * on Ranchu emulator (qemu2) returns 1 here and 3587157d2beSMiodrag Dinic * driver will use physical addresses. 3597157d2beSMiodrag Dinic */ 360da31de35SLaurent Vivier qtty->version = __raw_readl(base + GOLDFISH_TTY_REG_VERSION); 3617157d2beSMiodrag Dinic 3627157d2beSMiodrag Dinic /* 3637157d2beSMiodrag Dinic * Goldfish TTY device on Ranchu emulator (qemu2) 3647157d2beSMiodrag Dinic * will use DMA for read/write IO operations. 3657157d2beSMiodrag Dinic */ 3667157d2beSMiodrag Dinic if (qtty->version > 0) { 3677157d2beSMiodrag Dinic /* 3687157d2beSMiodrag Dinic * Initialize dma_mask to 32-bits. 3697157d2beSMiodrag Dinic */ 3707157d2beSMiodrag Dinic if (!pdev->dev.dma_mask) 3717157d2beSMiodrag Dinic pdev->dev.dma_mask = &pdev->dev.coherent_dma_mask; 3727157d2beSMiodrag Dinic ret = dma_set_mask(&pdev->dev, DMA_BIT_MASK(32)); 3737157d2beSMiodrag Dinic if (ret) { 3747157d2beSMiodrag Dinic dev_err(&pdev->dev, "No suitable DMA available.\n"); 3757157d2beSMiodrag Dinic goto err_dec_line_count; 3767157d2beSMiodrag Dinic } 3777157d2beSMiodrag Dinic } 378666b7793SArve Hjønnevåg 379da31de35SLaurent Vivier __raw_writel(GOLDFISH_TTY_CMD_INT_DISABLE, base + GOLDFISH_TTY_REG_CMD); 380666b7793SArve Hjønnevåg 381d78055dcSAlan ret = request_irq(irq, goldfish_tty_interrupt, IRQF_SHARED, 382465893e1SGreg Hackmann "goldfish_tty", qtty); 3837157d2beSMiodrag Dinic if (ret) { 3847157d2beSMiodrag Dinic pr_err("goldfish_tty: No IRQ available!\n"); 3857157d2beSMiodrag Dinic goto err_dec_line_count; 3867157d2beSMiodrag Dinic } 387666b7793SArve Hjønnevåg 388666b7793SArve Hjønnevåg ttydev = tty_port_register_device(&qtty->port, goldfish_tty_driver, 389465893e1SGreg Hackmann line, &pdev->dev); 390666b7793SArve Hjønnevåg if (IS_ERR(ttydev)) { 391666b7793SArve Hjønnevåg ret = PTR_ERR(ttydev); 392666b7793SArve Hjønnevåg goto err_tty_register_device_failed; 393666b7793SArve Hjønnevåg } 394666b7793SArve Hjønnevåg 395666b7793SArve Hjønnevåg strcpy(qtty->console.name, "ttyGF"); 396666b7793SArve Hjønnevåg qtty->console.write = goldfish_tty_console_write; 397666b7793SArve Hjønnevåg qtty->console.device = goldfish_tty_console_device; 398666b7793SArve Hjønnevåg qtty->console.setup = goldfish_tty_console_setup; 399666b7793SArve Hjønnevåg qtty->console.flags = CON_PRINTBUFFER; 400465893e1SGreg Hackmann qtty->console.index = line; 401666b7793SArve Hjønnevåg register_console(&qtty->console); 402465893e1SGreg Hackmann platform_set_drvdata(pdev, qtty); 403666b7793SArve Hjønnevåg 404666b7793SArve Hjønnevåg mutex_unlock(&goldfish_tty_lock); 405666b7793SArve Hjønnevåg return 0; 406666b7793SArve Hjønnevåg 407666b7793SArve Hjønnevåg err_tty_register_device_failed: 4081a5c2d1dSChristophe JAILLET free_irq(irq, qtty); 4097157d2beSMiodrag Dinic err_dec_line_count: 410666b7793SArve Hjønnevåg goldfish_tty_current_line_count--; 411666b7793SArve Hjønnevåg if (goldfish_tty_current_line_count == 0) 412666b7793SArve Hjønnevåg goldfish_tty_delete_driver(); 4137157d2beSMiodrag Dinic err_unlock: 414666b7793SArve Hjønnevåg mutex_unlock(&goldfish_tty_lock); 415666b7793SArve Hjønnevåg err_unmap: 416666b7793SArve Hjønnevåg iounmap(base); 417666b7793SArve Hjønnevåg return ret; 418666b7793SArve Hjønnevåg } 419666b7793SArve Hjønnevåg 420666b7793SArve Hjønnevåg static int goldfish_tty_remove(struct platform_device *pdev) 421666b7793SArve Hjønnevåg { 422465893e1SGreg Hackmann struct goldfish_tty *qtty = platform_get_drvdata(pdev); 423666b7793SArve Hjønnevåg 424666b7793SArve Hjønnevåg mutex_lock(&goldfish_tty_lock); 425666b7793SArve Hjønnevåg 426666b7793SArve Hjønnevåg unregister_console(&qtty->console); 427465893e1SGreg Hackmann tty_unregister_device(goldfish_tty_driver, qtty->console.index); 428666b7793SArve Hjønnevåg iounmap(qtty->base); 429a4dc9236SFabian Frederick qtty->base = NULL; 430666b7793SArve Hjønnevåg free_irq(qtty->irq, pdev); 431666b7793SArve Hjønnevåg goldfish_tty_current_line_count--; 432666b7793SArve Hjønnevåg if (goldfish_tty_current_line_count == 0) 433666b7793SArve Hjønnevåg goldfish_tty_delete_driver(); 434666b7793SArve Hjønnevåg mutex_unlock(&goldfish_tty_lock); 435666b7793SArve Hjønnevåg return 0; 436666b7793SArve Hjønnevåg } 437666b7793SArve Hjønnevåg 4386a28fd2bSSebastian Andrzej Siewior #ifdef CONFIG_GOLDFISH_TTY_EARLY_CONSOLE 4393840ed95SMiodrag Dinic static void gf_early_console_putchar(struct uart_port *port, int ch) 4403840ed95SMiodrag Dinic { 4413840ed95SMiodrag Dinic __raw_writel(ch, port->membase); 4423840ed95SMiodrag Dinic } 4433840ed95SMiodrag Dinic 4443840ed95SMiodrag Dinic static void gf_early_write(struct console *con, const char *s, unsigned int n) 4453840ed95SMiodrag Dinic { 4463840ed95SMiodrag Dinic struct earlycon_device *dev = con->data; 4473840ed95SMiodrag Dinic 4483840ed95SMiodrag Dinic uart_console_write(&dev->port, s, n, gf_early_console_putchar); 4493840ed95SMiodrag Dinic } 4503840ed95SMiodrag Dinic 4513840ed95SMiodrag Dinic static int __init gf_earlycon_setup(struct earlycon_device *device, 4523840ed95SMiodrag Dinic const char *opt) 4533840ed95SMiodrag Dinic { 4543840ed95SMiodrag Dinic if (!device->port.membase) 4553840ed95SMiodrag Dinic return -ENODEV; 4563840ed95SMiodrag Dinic 4573840ed95SMiodrag Dinic device->con->write = gf_early_write; 4583840ed95SMiodrag Dinic return 0; 4593840ed95SMiodrag Dinic } 4603840ed95SMiodrag Dinic 4613840ed95SMiodrag Dinic OF_EARLYCON_DECLARE(early_gf_tty, "google,goldfish-tty", gf_earlycon_setup); 4626a28fd2bSSebastian Andrzej Siewior #endif 4633840ed95SMiodrag Dinic 4649b883eeaSMiodrag Dinic static const struct of_device_id goldfish_tty_of_match[] = { 4659b883eeaSMiodrag Dinic { .compatible = "google,goldfish-tty", }, 4669b883eeaSMiodrag Dinic {}, 4679b883eeaSMiodrag Dinic }; 4689b883eeaSMiodrag Dinic 4699b883eeaSMiodrag Dinic MODULE_DEVICE_TABLE(of, goldfish_tty_of_match); 4709b883eeaSMiodrag Dinic 471666b7793SArve Hjønnevåg static struct platform_driver goldfish_tty_platform_driver = { 472666b7793SArve Hjønnevåg .probe = goldfish_tty_probe, 473666b7793SArve Hjønnevåg .remove = goldfish_tty_remove, 474666b7793SArve Hjønnevåg .driver = { 4759b883eeaSMiodrag Dinic .name = "goldfish_tty", 4769b883eeaSMiodrag Dinic .of_match_table = goldfish_tty_of_match, 477666b7793SArve Hjønnevåg } 478666b7793SArve Hjønnevåg }; 479666b7793SArve Hjønnevåg 480666b7793SArve Hjønnevåg module_platform_driver(goldfish_tty_platform_driver); 481666b7793SArve Hjønnevåg 482666b7793SArve Hjønnevåg MODULE_LICENSE("GPL v2"); 483