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> 16e0f682e0SAlan #include <linux/goldfish.h> 177157d2beSMiodrag Dinic #include <linux/mm.h> 187157d2beSMiodrag Dinic #include <linux/dma-mapping.h> 193840ed95SMiodrag Dinic #include <linux/serial_core.h> 20666b7793SArve Hjønnevåg 212296eee7SAleksandar Markovic /* Goldfish tty register's offsets */ 222296eee7SAleksandar Markovic #define GOLDFISH_TTY_REG_BYTES_READY 0x04 232296eee7SAleksandar Markovic #define GOLDFISH_TTY_REG_CMD 0x08 242296eee7SAleksandar Markovic #define GOLDFISH_TTY_REG_DATA_PTR 0x10 252296eee7SAleksandar Markovic #define GOLDFISH_TTY_REG_DATA_LEN 0x14 262296eee7SAleksandar Markovic #define GOLDFISH_TTY_REG_DATA_PTR_HIGH 0x18 277157d2beSMiodrag Dinic #define GOLDFISH_TTY_REG_VERSION 0x20 28666b7793SArve Hjønnevåg 292296eee7SAleksandar Markovic /* Goldfish tty commands */ 302296eee7SAleksandar Markovic #define GOLDFISH_TTY_CMD_INT_DISABLE 0 312296eee7SAleksandar Markovic #define GOLDFISH_TTY_CMD_INT_ENABLE 1 322296eee7SAleksandar Markovic #define GOLDFISH_TTY_CMD_WRITE_BUFFER 2 332296eee7SAleksandar Markovic #define GOLDFISH_TTY_CMD_READ_BUFFER 3 34666b7793SArve Hjønnevåg 35666b7793SArve Hjønnevåg struct goldfish_tty { 36666b7793SArve Hjønnevåg struct tty_port port; 37666b7793SArve Hjønnevåg spinlock_t lock; 38666b7793SArve Hjønnevåg void __iomem *base; 39666b7793SArve Hjønnevåg u32 irq; 40666b7793SArve Hjønnevåg int opencount; 41666b7793SArve Hjønnevåg struct console console; 427157d2beSMiodrag Dinic u32 version; 437157d2beSMiodrag Dinic struct device *dev; 44666b7793SArve Hjønnevåg }; 45666b7793SArve Hjønnevåg 46666b7793SArve Hjønnevåg static DEFINE_MUTEX(goldfish_tty_lock); 47666b7793SArve Hjønnevåg static struct tty_driver *goldfish_tty_driver; 48666b7793SArve Hjønnevåg static u32 goldfish_tty_line_count = 8; 49666b7793SArve Hjønnevåg static u32 goldfish_tty_current_line_count; 50666b7793SArve Hjønnevåg static struct goldfish_tty *goldfish_ttys; 51666b7793SArve Hjønnevåg 527157d2beSMiodrag Dinic static void do_rw_io(struct goldfish_tty *qtty, 537157d2beSMiodrag Dinic unsigned long address, 547157d2beSMiodrag Dinic unsigned int count, 557157d2beSMiodrag Dinic int is_write) 56666b7793SArve Hjønnevåg { 57666b7793SArve Hjønnevåg unsigned long irq_flags; 58666b7793SArve Hjønnevåg void __iomem *base = qtty->base; 597157d2beSMiodrag Dinic 60666b7793SArve Hjønnevåg spin_lock_irqsave(&qtty->lock, irq_flags); 617157d2beSMiodrag Dinic gf_write_ptr((void *)address, base + GOLDFISH_TTY_REG_DATA_PTR, 622296eee7SAleksandar Markovic base + GOLDFISH_TTY_REG_DATA_PTR_HIGH); 632296eee7SAleksandar Markovic writel(count, base + GOLDFISH_TTY_REG_DATA_LEN); 647157d2beSMiodrag Dinic 657157d2beSMiodrag Dinic if (is_write) 667157d2beSMiodrag Dinic writel(GOLDFISH_TTY_CMD_WRITE_BUFFER, 677157d2beSMiodrag Dinic base + GOLDFISH_TTY_REG_CMD); 687157d2beSMiodrag Dinic else 697157d2beSMiodrag Dinic writel(GOLDFISH_TTY_CMD_READ_BUFFER, 707157d2beSMiodrag Dinic base + GOLDFISH_TTY_REG_CMD); 717157d2beSMiodrag Dinic 72666b7793SArve Hjønnevåg spin_unlock_irqrestore(&qtty->lock, irq_flags); 73666b7793SArve Hjønnevåg } 74666b7793SArve Hjønnevåg 757157d2beSMiodrag Dinic static void goldfish_tty_rw(struct goldfish_tty *qtty, 767157d2beSMiodrag Dinic unsigned long addr, 777157d2beSMiodrag Dinic unsigned int count, 787157d2beSMiodrag Dinic int is_write) 797157d2beSMiodrag Dinic { 807157d2beSMiodrag Dinic dma_addr_t dma_handle; 817157d2beSMiodrag Dinic enum dma_data_direction dma_dir; 827157d2beSMiodrag Dinic 837157d2beSMiodrag Dinic dma_dir = (is_write ? DMA_TO_DEVICE : DMA_FROM_DEVICE); 847157d2beSMiodrag Dinic if (qtty->version > 0) { 857157d2beSMiodrag Dinic /* 867157d2beSMiodrag Dinic * Goldfish TTY for Ranchu platform uses 877157d2beSMiodrag Dinic * physical addresses and DMA for read/write operations 887157d2beSMiodrag Dinic */ 897157d2beSMiodrag Dinic unsigned long addr_end = addr + count; 907157d2beSMiodrag Dinic 917157d2beSMiodrag Dinic while (addr < addr_end) { 927157d2beSMiodrag Dinic unsigned long pg_end = (addr & PAGE_MASK) + PAGE_SIZE; 937157d2beSMiodrag Dinic unsigned long next = 947157d2beSMiodrag Dinic pg_end < addr_end ? pg_end : addr_end; 957157d2beSMiodrag Dinic unsigned long avail = next - addr; 967157d2beSMiodrag Dinic 977157d2beSMiodrag Dinic /* 987157d2beSMiodrag Dinic * Map the buffer's virtual address to the DMA address 997157d2beSMiodrag Dinic * so the buffer can be accessed by the device. 1007157d2beSMiodrag Dinic */ 1017157d2beSMiodrag Dinic dma_handle = dma_map_single(qtty->dev, (void *)addr, 1027157d2beSMiodrag Dinic avail, dma_dir); 1037157d2beSMiodrag Dinic 1047157d2beSMiodrag Dinic if (dma_mapping_error(qtty->dev, dma_handle)) { 1057157d2beSMiodrag Dinic dev_err(qtty->dev, "tty: DMA mapping error.\n"); 1067157d2beSMiodrag Dinic return; 1077157d2beSMiodrag Dinic } 1087157d2beSMiodrag Dinic do_rw_io(qtty, dma_handle, avail, is_write); 1097157d2beSMiodrag Dinic 1107157d2beSMiodrag Dinic /* 1117157d2beSMiodrag Dinic * Unmap the previously mapped region after 1127157d2beSMiodrag Dinic * the completion of the read/write operation. 1137157d2beSMiodrag Dinic */ 1147157d2beSMiodrag Dinic dma_unmap_single(qtty->dev, dma_handle, avail, dma_dir); 1157157d2beSMiodrag Dinic 1167157d2beSMiodrag Dinic addr += avail; 1177157d2beSMiodrag Dinic } 1187157d2beSMiodrag Dinic } else { 1197157d2beSMiodrag Dinic /* 1207157d2beSMiodrag Dinic * Old style Goldfish TTY used on the Goldfish platform 1217157d2beSMiodrag Dinic * uses virtual addresses. 1227157d2beSMiodrag Dinic */ 1237157d2beSMiodrag Dinic do_rw_io(qtty, addr, count, is_write); 1247157d2beSMiodrag Dinic } 1257157d2beSMiodrag Dinic } 1267157d2beSMiodrag Dinic 1277157d2beSMiodrag Dinic static void goldfish_tty_do_write(int line, const char *buf, 1287157d2beSMiodrag Dinic 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 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 1442296eee7SAleksandar Markovic count = readl(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 153ebcf0981SAlan Cox tty_schedule_flip(&qtty->port); 154666b7793SArve Hjønnevåg return IRQ_HANDLED; 155666b7793SArve Hjønnevåg } 156666b7793SArve Hjønnevåg 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); 1612296eee7SAleksandar Markovic writel(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 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); 1692296eee7SAleksandar Markovic writel(GOLDFISH_TTY_CMD_INT_DISABLE, qtty->base + GOLDFISH_TTY_REG_CMD); 170666b7793SArve Hjønnevåg } 171666b7793SArve Hjønnevåg 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 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 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 188d78055dcSAlan static int goldfish_tty_write(struct tty_struct *tty, const unsigned char *buf, 189d78055dcSAlan int 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 195666b7793SArve Hjønnevåg static 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 200666b7793SArve Hjønnevåg static 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; 2042296eee7SAleksandar Markovic return readl(base + GOLDFISH_TTY_REG_BYTES_READY); 205666b7793SArve Hjønnevåg } 206666b7793SArve Hjønnevåg 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 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 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 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 248*6396bb22SKees Cook goldfish_ttys = kcalloc(goldfish_tty_line_count, 249*6396bb22SKees Cook sizeof(*goldfish_ttys), 250*6396bb22SKees 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 } 255666b7793SArve Hjønnevåg tty = alloc_tty_driver(goldfish_tty_line_count); 256666b7793SArve Hjønnevåg if (tty == NULL) { 257666b7793SArve Hjønnevåg ret = -ENOMEM; 258666b7793SArve Hjønnevåg goto err_alloc_tty_driver_failed; 259666b7793SArve Hjønnevåg } 260666b7793SArve Hjønnevåg tty->driver_name = "goldfish"; 261666b7793SArve Hjønnevåg tty->name = "ttyGF"; 262666b7793SArve Hjønnevåg tty->type = TTY_DRIVER_TYPE_SERIAL; 263666b7793SArve Hjønnevåg tty->subtype = SERIAL_TYPE_NORMAL; 264666b7793SArve Hjønnevåg tty->init_termios = tty_std_termios; 265d78055dcSAlan tty->flags = TTY_DRIVER_RESET_TERMIOS | TTY_DRIVER_REAL_RAW | 266d78055dcSAlan TTY_DRIVER_DYNAMIC_DEV; 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: 276666b7793SArve Hjønnevåg put_tty_driver(tty); 277666b7793SArve Hjønnevåg err_alloc_tty_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 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); 287666b7793SArve Hjønnevåg put_tty_driver(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 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; 300666b7793SArve Hjønnevåg u32 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 315666b7793SArve Hjønnevåg r = platform_get_resource(pdev, IORESOURCE_IRQ, 0); 3167157d2beSMiodrag Dinic if (!r) { 3177157d2beSMiodrag Dinic pr_err("goldfish_tty: No IRQ resource available!\n"); 318666b7793SArve Hjønnevåg goto err_unmap; 3197157d2beSMiodrag Dinic } 320666b7793SArve Hjønnevåg 321666b7793SArve Hjønnevåg irq = r->start; 322666b7793SArve Hjønnevåg 323666b7793SArve Hjønnevåg mutex_lock(&goldfish_tty_lock); 324465893e1SGreg Hackmann 325465893e1SGreg Hackmann if (pdev->id == PLATFORM_DEVID_NONE) 326465893e1SGreg Hackmann line = goldfish_tty_current_line_count; 327465893e1SGreg Hackmann else 328465893e1SGreg Hackmann line = pdev->id; 329465893e1SGreg Hackmann 3307157d2beSMiodrag Dinic if (line >= goldfish_tty_line_count) { 3317157d2beSMiodrag Dinic pr_err("goldfish_tty: Reached maximum tty number of %d.\n", 3327157d2beSMiodrag Dinic goldfish_tty_current_line_count); 3337157d2beSMiodrag Dinic ret = -ENOMEM; 3347157d2beSMiodrag Dinic goto err_unlock; 3357157d2beSMiodrag Dinic } 336465893e1SGreg Hackmann 337666b7793SArve Hjønnevåg if (goldfish_tty_current_line_count == 0) { 338666b7793SArve Hjønnevåg ret = goldfish_tty_create_driver(); 339666b7793SArve Hjønnevåg if (ret) 3407157d2beSMiodrag Dinic goto err_unlock; 341666b7793SArve Hjønnevåg } 342666b7793SArve Hjønnevåg goldfish_tty_current_line_count++; 343666b7793SArve Hjønnevåg 344465893e1SGreg Hackmann qtty = &goldfish_ttys[line]; 345666b7793SArve Hjønnevåg spin_lock_init(&qtty->lock); 346666b7793SArve Hjønnevåg tty_port_init(&qtty->port); 347666b7793SArve Hjønnevåg qtty->port.ops = &goldfish_port_ops; 348666b7793SArve Hjønnevåg qtty->base = base; 349666b7793SArve Hjønnevåg qtty->irq = irq; 3507157d2beSMiodrag Dinic qtty->dev = &pdev->dev; 3517157d2beSMiodrag Dinic 3527157d2beSMiodrag Dinic /* 3537157d2beSMiodrag Dinic * Goldfish TTY device used by the Goldfish emulator 3547157d2beSMiodrag Dinic * should identify itself with 0, forcing the driver 3557157d2beSMiodrag Dinic * to use virtual addresses. Goldfish TTY device 3567157d2beSMiodrag Dinic * on Ranchu emulator (qemu2) returns 1 here and 3577157d2beSMiodrag Dinic * driver will use physical addresses. 3587157d2beSMiodrag Dinic */ 3597157d2beSMiodrag Dinic qtty->version = readl(base + GOLDFISH_TTY_REG_VERSION); 3607157d2beSMiodrag Dinic 3617157d2beSMiodrag Dinic /* 3627157d2beSMiodrag Dinic * Goldfish TTY device on Ranchu emulator (qemu2) 3637157d2beSMiodrag Dinic * will use DMA for read/write IO operations. 3647157d2beSMiodrag Dinic */ 3657157d2beSMiodrag Dinic if (qtty->version > 0) { 3667157d2beSMiodrag Dinic /* 3677157d2beSMiodrag Dinic * Initialize dma_mask to 32-bits. 3687157d2beSMiodrag Dinic */ 3697157d2beSMiodrag Dinic if (!pdev->dev.dma_mask) 3707157d2beSMiodrag Dinic pdev->dev.dma_mask = &pdev->dev.coherent_dma_mask; 3717157d2beSMiodrag Dinic ret = dma_set_mask(&pdev->dev, DMA_BIT_MASK(32)); 3727157d2beSMiodrag Dinic if (ret) { 3737157d2beSMiodrag Dinic dev_err(&pdev->dev, "No suitable DMA available.\n"); 3747157d2beSMiodrag Dinic goto err_dec_line_count; 3757157d2beSMiodrag Dinic } 3767157d2beSMiodrag Dinic } 377666b7793SArve Hjønnevåg 3782296eee7SAleksandar Markovic writel(GOLDFISH_TTY_CMD_INT_DISABLE, base + GOLDFISH_TTY_REG_CMD); 379666b7793SArve Hjønnevåg 380d78055dcSAlan ret = request_irq(irq, goldfish_tty_interrupt, IRQF_SHARED, 381465893e1SGreg Hackmann "goldfish_tty", qtty); 3827157d2beSMiodrag Dinic if (ret) { 3837157d2beSMiodrag Dinic pr_err("goldfish_tty: No IRQ available!\n"); 3847157d2beSMiodrag Dinic goto err_dec_line_count; 3857157d2beSMiodrag Dinic } 386666b7793SArve Hjønnevåg 387666b7793SArve Hjønnevåg ttydev = tty_port_register_device(&qtty->port, goldfish_tty_driver, 388465893e1SGreg Hackmann line, &pdev->dev); 389666b7793SArve Hjønnevåg if (IS_ERR(ttydev)) { 390666b7793SArve Hjønnevåg ret = PTR_ERR(ttydev); 391666b7793SArve Hjønnevåg goto err_tty_register_device_failed; 392666b7793SArve Hjønnevåg } 393666b7793SArve Hjønnevåg 394666b7793SArve Hjønnevåg strcpy(qtty->console.name, "ttyGF"); 395666b7793SArve Hjønnevåg qtty->console.write = goldfish_tty_console_write; 396666b7793SArve Hjønnevåg qtty->console.device = goldfish_tty_console_device; 397666b7793SArve Hjønnevåg qtty->console.setup = goldfish_tty_console_setup; 398666b7793SArve Hjønnevåg qtty->console.flags = CON_PRINTBUFFER; 399465893e1SGreg Hackmann qtty->console.index = line; 400666b7793SArve Hjønnevåg register_console(&qtty->console); 401465893e1SGreg Hackmann platform_set_drvdata(pdev, qtty); 402666b7793SArve Hjønnevåg 403666b7793SArve Hjønnevåg mutex_unlock(&goldfish_tty_lock); 404666b7793SArve Hjønnevåg return 0; 405666b7793SArve Hjønnevåg 406666b7793SArve Hjønnevåg err_tty_register_device_failed: 4071a5c2d1dSChristophe JAILLET free_irq(irq, qtty); 4087157d2beSMiodrag Dinic err_dec_line_count: 409666b7793SArve Hjønnevåg goldfish_tty_current_line_count--; 410666b7793SArve Hjønnevåg if (goldfish_tty_current_line_count == 0) 411666b7793SArve Hjønnevåg goldfish_tty_delete_driver(); 4127157d2beSMiodrag Dinic err_unlock: 413666b7793SArve Hjønnevåg mutex_unlock(&goldfish_tty_lock); 414666b7793SArve Hjønnevåg err_unmap: 415666b7793SArve Hjønnevåg iounmap(base); 416666b7793SArve Hjønnevåg return ret; 417666b7793SArve Hjønnevåg } 418666b7793SArve Hjønnevåg 419666b7793SArve Hjønnevåg static int goldfish_tty_remove(struct platform_device *pdev) 420666b7793SArve Hjønnevåg { 421465893e1SGreg Hackmann struct goldfish_tty *qtty = platform_get_drvdata(pdev); 422666b7793SArve Hjønnevåg 423666b7793SArve Hjønnevåg mutex_lock(&goldfish_tty_lock); 424666b7793SArve Hjønnevåg 425666b7793SArve Hjønnevåg unregister_console(&qtty->console); 426465893e1SGreg Hackmann tty_unregister_device(goldfish_tty_driver, qtty->console.index); 427666b7793SArve Hjønnevåg iounmap(qtty->base); 428a4dc9236SFabian Frederick qtty->base = NULL; 429666b7793SArve Hjønnevåg free_irq(qtty->irq, pdev); 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 4383840ed95SMiodrag Dinic static void gf_early_console_putchar(struct uart_port *port, int ch) 4393840ed95SMiodrag Dinic { 4403840ed95SMiodrag Dinic __raw_writel(ch, port->membase); 4413840ed95SMiodrag Dinic } 4423840ed95SMiodrag Dinic 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 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