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> 25666b7793SArve Hjønnevåg 26666b7793SArve Hjønnevåg enum { 27666b7793SArve Hjønnevåg GOLDFISH_TTY_PUT_CHAR = 0x00, 28666b7793SArve Hjønnevåg GOLDFISH_TTY_BYTES_READY = 0x04, 29666b7793SArve Hjønnevåg GOLDFISH_TTY_CMD = 0x08, 30666b7793SArve Hjønnevåg 31666b7793SArve Hjønnevåg GOLDFISH_TTY_DATA_PTR = 0x10, 32666b7793SArve Hjønnevåg GOLDFISH_TTY_DATA_LEN = 0x14, 33b8658bc8SJun Tian GOLDFISH_TTY_DATA_PTR_HIGH = 0x18, 34666b7793SArve Hjønnevåg 35666b7793SArve Hjønnevåg GOLDFISH_TTY_CMD_INT_DISABLE = 0, 36666b7793SArve Hjønnevåg GOLDFISH_TTY_CMD_INT_ENABLE = 1, 37666b7793SArve Hjønnevåg GOLDFISH_TTY_CMD_WRITE_BUFFER = 2, 38666b7793SArve Hjønnevåg GOLDFISH_TTY_CMD_READ_BUFFER = 3, 39666b7793SArve Hjønnevåg }; 40666b7793SArve Hjønnevåg 41666b7793SArve Hjønnevåg struct goldfish_tty { 42666b7793SArve Hjønnevåg struct tty_port port; 43666b7793SArve Hjønnevåg spinlock_t lock; 44666b7793SArve Hjønnevåg void __iomem *base; 45666b7793SArve Hjønnevåg u32 irq; 46666b7793SArve Hjønnevåg int opencount; 47666b7793SArve Hjønnevåg struct console console; 48666b7793SArve Hjønnevåg }; 49666b7793SArve Hjønnevåg 50666b7793SArve Hjønnevåg static DEFINE_MUTEX(goldfish_tty_lock); 51666b7793SArve Hjønnevåg static struct tty_driver *goldfish_tty_driver; 52666b7793SArve Hjønnevåg static u32 goldfish_tty_line_count = 8; 53666b7793SArve Hjønnevåg static u32 goldfish_tty_current_line_count; 54666b7793SArve Hjønnevåg static struct goldfish_tty *goldfish_ttys; 55666b7793SArve Hjønnevåg 56666b7793SArve Hjønnevåg static void goldfish_tty_do_write(int line, const char *buf, unsigned count) 57666b7793SArve Hjønnevåg { 58666b7793SArve Hjønnevåg unsigned long irq_flags; 59666b7793SArve Hjønnevåg struct goldfish_tty *qtty = &goldfish_ttys[line]; 60666b7793SArve Hjønnevåg void __iomem *base = qtty->base; 61666b7793SArve Hjønnevåg spin_lock_irqsave(&qtty->lock, irq_flags); 6207d783fdSPeter Senna Tschudin gf_write_ptr(buf, base + GOLDFISH_TTY_DATA_PTR, 63e0f682e0SAlan base + GOLDFISH_TTY_DATA_PTR_HIGH); 64666b7793SArve Hjønnevåg writel(count, base + GOLDFISH_TTY_DATA_LEN); 65666b7793SArve Hjønnevåg writel(GOLDFISH_TTY_CMD_WRITE_BUFFER, base + GOLDFISH_TTY_CMD); 66666b7793SArve Hjønnevåg spin_unlock_irqrestore(&qtty->lock, irq_flags); 67666b7793SArve Hjønnevåg } 68666b7793SArve Hjønnevåg 69666b7793SArve Hjønnevåg static irqreturn_t goldfish_tty_interrupt(int irq, void *dev_id) 70666b7793SArve Hjønnevåg { 71465893e1SGreg Hackmann struct goldfish_tty *qtty = dev_id; 72666b7793SArve Hjønnevåg void __iomem *base = qtty->base; 73666b7793SArve Hjønnevåg unsigned long irq_flags; 74666b7793SArve Hjønnevåg unsigned char *buf; 75666b7793SArve Hjønnevåg u32 count; 76666b7793SArve Hjønnevåg 77666b7793SArve Hjønnevåg count = readl(base + GOLDFISH_TTY_BYTES_READY); 78666b7793SArve Hjønnevåg if (count == 0) 79666b7793SArve Hjønnevåg return IRQ_NONE; 80666b7793SArve Hjønnevåg 81ebcf0981SAlan Cox count = tty_prepare_flip_string(&qtty->port, &buf, count); 82666b7793SArve Hjønnevåg spin_lock_irqsave(&qtty->lock, irq_flags); 8307d783fdSPeter Senna Tschudin gf_write_ptr(buf, base + GOLDFISH_TTY_DATA_PTR, 84e0f682e0SAlan base + GOLDFISH_TTY_DATA_PTR_HIGH); 85666b7793SArve Hjønnevåg writel(count, base + GOLDFISH_TTY_DATA_LEN); 86666b7793SArve Hjønnevåg writel(GOLDFISH_TTY_CMD_READ_BUFFER, base + GOLDFISH_TTY_CMD); 87666b7793SArve Hjønnevåg spin_unlock_irqrestore(&qtty->lock, irq_flags); 88ebcf0981SAlan Cox tty_schedule_flip(&qtty->port); 89666b7793SArve Hjønnevåg return IRQ_HANDLED; 90666b7793SArve Hjønnevåg } 91666b7793SArve Hjønnevåg 92666b7793SArve Hjønnevåg static int goldfish_tty_activate(struct tty_port *port, struct tty_struct *tty) 93666b7793SArve Hjønnevåg { 94d78055dcSAlan struct goldfish_tty *qtty = container_of(port, struct goldfish_tty, 95d78055dcSAlan port); 96666b7793SArve Hjønnevåg writel(GOLDFISH_TTY_CMD_INT_ENABLE, qtty->base + GOLDFISH_TTY_CMD); 97666b7793SArve Hjønnevåg return 0; 98666b7793SArve Hjønnevåg } 99666b7793SArve Hjønnevåg 100666b7793SArve Hjønnevåg static void goldfish_tty_shutdown(struct tty_port *port) 101666b7793SArve Hjønnevåg { 102d78055dcSAlan struct goldfish_tty *qtty = container_of(port, struct goldfish_tty, 103d78055dcSAlan port); 104666b7793SArve Hjønnevåg writel(GOLDFISH_TTY_CMD_INT_DISABLE, qtty->base + GOLDFISH_TTY_CMD); 105666b7793SArve Hjønnevåg } 106666b7793SArve Hjønnevåg 107666b7793SArve Hjønnevåg static int goldfish_tty_open(struct tty_struct *tty, struct file *filp) 108666b7793SArve Hjønnevåg { 109666b7793SArve Hjønnevåg struct goldfish_tty *qtty = &goldfish_ttys[tty->index]; 110666b7793SArve Hjønnevåg return tty_port_open(&qtty->port, tty, filp); 111666b7793SArve Hjønnevåg } 112666b7793SArve Hjønnevåg 113666b7793SArve Hjønnevåg static void goldfish_tty_close(struct tty_struct *tty, struct file *filp) 114666b7793SArve Hjønnevåg { 115666b7793SArve Hjønnevåg tty_port_close(tty->port, tty, filp); 116666b7793SArve Hjønnevåg } 117666b7793SArve Hjønnevåg 118666b7793SArve Hjønnevåg static void goldfish_tty_hangup(struct tty_struct *tty) 119666b7793SArve Hjønnevåg { 120666b7793SArve Hjønnevåg tty_port_hangup(tty->port); 121666b7793SArve Hjønnevåg } 122666b7793SArve Hjønnevåg 123d78055dcSAlan static int goldfish_tty_write(struct tty_struct *tty, const unsigned char *buf, 124d78055dcSAlan int count) 125666b7793SArve Hjønnevåg { 126666b7793SArve Hjønnevåg goldfish_tty_do_write(tty->index, buf, count); 127666b7793SArve Hjønnevåg return count; 128666b7793SArve Hjønnevåg } 129666b7793SArve Hjønnevåg 130666b7793SArve Hjønnevåg static int goldfish_tty_write_room(struct tty_struct *tty) 131666b7793SArve Hjønnevåg { 132666b7793SArve Hjønnevåg return 0x10000; 133666b7793SArve Hjønnevåg } 134666b7793SArve Hjønnevåg 135666b7793SArve Hjønnevåg static int goldfish_tty_chars_in_buffer(struct tty_struct *tty) 136666b7793SArve Hjønnevåg { 137666b7793SArve Hjønnevåg struct goldfish_tty *qtty = &goldfish_ttys[tty->index]; 138666b7793SArve Hjønnevåg void __iomem *base = qtty->base; 139666b7793SArve Hjønnevåg return readl(base + GOLDFISH_TTY_BYTES_READY); 140666b7793SArve Hjønnevåg } 141666b7793SArve Hjønnevåg 142d78055dcSAlan static void goldfish_tty_console_write(struct console *co, const char *b, 143d78055dcSAlan unsigned count) 144666b7793SArve Hjønnevåg { 145666b7793SArve Hjønnevåg goldfish_tty_do_write(co->index, b, count); 146666b7793SArve Hjønnevåg } 147666b7793SArve Hjønnevåg 148d78055dcSAlan static struct tty_driver *goldfish_tty_console_device(struct console *c, 149d78055dcSAlan int *index) 150666b7793SArve Hjønnevåg { 151666b7793SArve Hjønnevåg *index = c->index; 152666b7793SArve Hjønnevåg return goldfish_tty_driver; 153666b7793SArve Hjønnevåg } 154666b7793SArve Hjønnevåg 155666b7793SArve Hjønnevåg static int goldfish_tty_console_setup(struct console *co, char *options) 156666b7793SArve Hjønnevåg { 157fda2b418SDan Carpenter if ((unsigned)co->index >= goldfish_tty_line_count) 158666b7793SArve Hjønnevåg return -ENODEV; 159a4dc9236SFabian Frederick if (!goldfish_ttys[co->index].base) 160666b7793SArve Hjønnevåg return -ENODEV; 161666b7793SArve Hjønnevåg return 0; 162666b7793SArve Hjønnevåg } 163666b7793SArve Hjønnevåg 16404b757dfSAya Mahfouz static const struct tty_port_operations goldfish_port_ops = { 165666b7793SArve Hjønnevåg .activate = goldfish_tty_activate, 166666b7793SArve Hjønnevåg .shutdown = goldfish_tty_shutdown 167666b7793SArve Hjønnevåg }; 168666b7793SArve Hjønnevåg 169d78055dcSAlan static const struct tty_operations goldfish_tty_ops = { 170666b7793SArve Hjønnevåg .open = goldfish_tty_open, 171666b7793SArve Hjønnevåg .close = goldfish_tty_close, 172666b7793SArve Hjønnevåg .hangup = goldfish_tty_hangup, 173666b7793SArve Hjønnevåg .write = goldfish_tty_write, 174666b7793SArve Hjønnevåg .write_room = goldfish_tty_write_room, 175666b7793SArve Hjønnevåg .chars_in_buffer = goldfish_tty_chars_in_buffer, 176666b7793SArve Hjønnevåg }; 177666b7793SArve Hjønnevåg 178666b7793SArve Hjønnevåg static int goldfish_tty_create_driver(void) 179666b7793SArve Hjønnevåg { 180666b7793SArve Hjønnevåg int ret; 181666b7793SArve Hjønnevåg struct tty_driver *tty; 182666b7793SArve Hjønnevåg 183d78055dcSAlan goldfish_ttys = kzalloc(sizeof(*goldfish_ttys) * 184d78055dcSAlan goldfish_tty_line_count, GFP_KERNEL); 185666b7793SArve Hjønnevåg if (goldfish_ttys == NULL) { 186666b7793SArve Hjønnevåg ret = -ENOMEM; 187666b7793SArve Hjønnevåg goto err_alloc_goldfish_ttys_failed; 188666b7793SArve Hjønnevåg } 189666b7793SArve Hjønnevåg tty = alloc_tty_driver(goldfish_tty_line_count); 190666b7793SArve Hjønnevåg if (tty == NULL) { 191666b7793SArve Hjønnevåg ret = -ENOMEM; 192666b7793SArve Hjønnevåg goto err_alloc_tty_driver_failed; 193666b7793SArve Hjønnevåg } 194666b7793SArve Hjønnevåg tty->driver_name = "goldfish"; 195666b7793SArve Hjønnevåg tty->name = "ttyGF"; 196666b7793SArve Hjønnevåg tty->type = TTY_DRIVER_TYPE_SERIAL; 197666b7793SArve Hjønnevåg tty->subtype = SERIAL_TYPE_NORMAL; 198666b7793SArve Hjønnevåg tty->init_termios = tty_std_termios; 199d78055dcSAlan tty->flags = TTY_DRIVER_RESET_TERMIOS | TTY_DRIVER_REAL_RAW | 200d78055dcSAlan TTY_DRIVER_DYNAMIC_DEV; 201666b7793SArve Hjønnevåg tty_set_operations(tty, &goldfish_tty_ops); 202666b7793SArve Hjønnevåg ret = tty_register_driver(tty); 203666b7793SArve Hjønnevåg if (ret) 204666b7793SArve Hjønnevåg goto err_tty_register_driver_failed; 205666b7793SArve Hjønnevåg 206666b7793SArve Hjønnevåg goldfish_tty_driver = tty; 207666b7793SArve Hjønnevåg return 0; 208666b7793SArve Hjønnevåg 209666b7793SArve Hjønnevåg err_tty_register_driver_failed: 210666b7793SArve Hjønnevåg put_tty_driver(tty); 211666b7793SArve Hjønnevåg err_alloc_tty_driver_failed: 212666b7793SArve Hjønnevåg kfree(goldfish_ttys); 213666b7793SArve Hjønnevåg goldfish_ttys = NULL; 214666b7793SArve Hjønnevåg err_alloc_goldfish_ttys_failed: 215666b7793SArve Hjønnevåg return ret; 216666b7793SArve Hjønnevåg } 217666b7793SArve Hjønnevåg 218666b7793SArve Hjønnevåg static void goldfish_tty_delete_driver(void) 219666b7793SArve Hjønnevåg { 220666b7793SArve Hjønnevåg tty_unregister_driver(goldfish_tty_driver); 221666b7793SArve Hjønnevåg put_tty_driver(goldfish_tty_driver); 222666b7793SArve Hjønnevåg goldfish_tty_driver = NULL; 223666b7793SArve Hjønnevåg kfree(goldfish_ttys); 224666b7793SArve Hjønnevåg goldfish_ttys = NULL; 225666b7793SArve Hjønnevåg } 226666b7793SArve Hjønnevåg 227666b7793SArve Hjønnevåg static int goldfish_tty_probe(struct platform_device *pdev) 228666b7793SArve Hjønnevåg { 229666b7793SArve Hjønnevåg struct goldfish_tty *qtty; 230666b7793SArve Hjønnevåg int ret = -EINVAL; 231666b7793SArve Hjønnevåg struct resource *r; 232666b7793SArve Hjønnevåg struct device *ttydev; 233666b7793SArve Hjønnevåg void __iomem *base; 234666b7793SArve Hjønnevåg u32 irq; 235465893e1SGreg Hackmann unsigned int line; 236666b7793SArve Hjønnevåg 237666b7793SArve Hjønnevåg r = platform_get_resource(pdev, IORESOURCE_MEM, 0); 238666b7793SArve Hjønnevåg if (r == NULL) 239666b7793SArve Hjønnevåg return -EINVAL; 240666b7793SArve Hjønnevåg 241666b7793SArve Hjønnevåg base = ioremap(r->start, 0x1000); 242666b7793SArve Hjønnevåg if (base == NULL) 243666b7793SArve Hjønnevåg pr_err("goldfish_tty: unable to remap base\n"); 244666b7793SArve Hjønnevåg 245666b7793SArve Hjønnevåg r = platform_get_resource(pdev, IORESOURCE_IRQ, 0); 246666b7793SArve Hjønnevåg if (r == NULL) 247666b7793SArve Hjønnevåg goto err_unmap; 248666b7793SArve Hjønnevåg 249666b7793SArve Hjønnevåg irq = r->start; 250666b7793SArve Hjønnevåg 251666b7793SArve Hjønnevåg mutex_lock(&goldfish_tty_lock); 252465893e1SGreg Hackmann 253465893e1SGreg Hackmann if (pdev->id == PLATFORM_DEVID_NONE) 254465893e1SGreg Hackmann line = goldfish_tty_current_line_count; 255465893e1SGreg Hackmann else 256465893e1SGreg Hackmann line = pdev->id; 257465893e1SGreg Hackmann 258465893e1SGreg Hackmann if (line >= goldfish_tty_line_count) 259465893e1SGreg Hackmann goto err_create_driver_failed; 260465893e1SGreg Hackmann 261666b7793SArve Hjønnevåg if (goldfish_tty_current_line_count == 0) { 262666b7793SArve Hjønnevåg ret = goldfish_tty_create_driver(); 263666b7793SArve Hjønnevåg if (ret) 264666b7793SArve Hjønnevåg goto err_create_driver_failed; 265666b7793SArve Hjønnevåg } 266666b7793SArve Hjønnevåg goldfish_tty_current_line_count++; 267666b7793SArve Hjønnevåg 268465893e1SGreg Hackmann qtty = &goldfish_ttys[line]; 269666b7793SArve Hjønnevåg spin_lock_init(&qtty->lock); 270666b7793SArve Hjønnevåg tty_port_init(&qtty->port); 271666b7793SArve Hjønnevåg qtty->port.ops = &goldfish_port_ops; 272666b7793SArve Hjønnevåg qtty->base = base; 273666b7793SArve Hjønnevåg qtty->irq = irq; 274666b7793SArve Hjønnevåg 275666b7793SArve Hjønnevåg writel(GOLDFISH_TTY_CMD_INT_DISABLE, base + GOLDFISH_TTY_CMD); 276666b7793SArve Hjønnevåg 277d78055dcSAlan ret = request_irq(irq, goldfish_tty_interrupt, IRQF_SHARED, 278465893e1SGreg Hackmann "goldfish_tty", qtty); 279666b7793SArve Hjønnevåg if (ret) 280666b7793SArve Hjønnevåg goto err_request_irq_failed; 281666b7793SArve Hjønnevåg 282666b7793SArve Hjønnevåg 283666b7793SArve Hjønnevåg ttydev = tty_port_register_device(&qtty->port, goldfish_tty_driver, 284465893e1SGreg Hackmann line, &pdev->dev); 285666b7793SArve Hjønnevåg if (IS_ERR(ttydev)) { 286666b7793SArve Hjønnevåg ret = PTR_ERR(ttydev); 287666b7793SArve Hjønnevåg goto err_tty_register_device_failed; 288666b7793SArve Hjønnevåg } 289666b7793SArve Hjønnevåg 290666b7793SArve Hjønnevåg strcpy(qtty->console.name, "ttyGF"); 291666b7793SArve Hjønnevåg qtty->console.write = goldfish_tty_console_write; 292666b7793SArve Hjønnevåg qtty->console.device = goldfish_tty_console_device; 293666b7793SArve Hjønnevåg qtty->console.setup = goldfish_tty_console_setup; 294666b7793SArve Hjønnevåg qtty->console.flags = CON_PRINTBUFFER; 295465893e1SGreg Hackmann qtty->console.index = line; 296666b7793SArve Hjønnevåg register_console(&qtty->console); 297465893e1SGreg Hackmann platform_set_drvdata(pdev, qtty); 298666b7793SArve Hjønnevåg 299666b7793SArve Hjønnevåg mutex_unlock(&goldfish_tty_lock); 300666b7793SArve Hjønnevåg return 0; 301666b7793SArve Hjønnevåg 302666b7793SArve Hjønnevåg err_tty_register_device_failed: 303666b7793SArve Hjønnevåg free_irq(irq, pdev); 304666b7793SArve Hjønnevåg err_request_irq_failed: 305666b7793SArve Hjønnevåg goldfish_tty_current_line_count--; 306666b7793SArve Hjønnevåg if (goldfish_tty_current_line_count == 0) 307666b7793SArve Hjønnevåg goldfish_tty_delete_driver(); 308666b7793SArve Hjønnevåg err_create_driver_failed: 309666b7793SArve Hjønnevåg mutex_unlock(&goldfish_tty_lock); 310666b7793SArve Hjønnevåg err_unmap: 311666b7793SArve Hjønnevåg iounmap(base); 312666b7793SArve Hjønnevåg return ret; 313666b7793SArve Hjønnevåg } 314666b7793SArve Hjønnevåg 315666b7793SArve Hjønnevåg static int goldfish_tty_remove(struct platform_device *pdev) 316666b7793SArve Hjønnevåg { 317465893e1SGreg Hackmann struct goldfish_tty *qtty = platform_get_drvdata(pdev); 318666b7793SArve Hjønnevåg 319666b7793SArve Hjønnevåg mutex_lock(&goldfish_tty_lock); 320666b7793SArve Hjønnevåg 321666b7793SArve Hjønnevåg unregister_console(&qtty->console); 322465893e1SGreg Hackmann tty_unregister_device(goldfish_tty_driver, qtty->console.index); 323666b7793SArve Hjønnevåg iounmap(qtty->base); 324a4dc9236SFabian Frederick qtty->base = NULL; 325666b7793SArve Hjønnevåg free_irq(qtty->irq, pdev); 326666b7793SArve Hjønnevåg goldfish_tty_current_line_count--; 327666b7793SArve Hjønnevåg if (goldfish_tty_current_line_count == 0) 328666b7793SArve Hjønnevåg goldfish_tty_delete_driver(); 329666b7793SArve Hjønnevåg mutex_unlock(&goldfish_tty_lock); 330666b7793SArve Hjønnevåg return 0; 331666b7793SArve Hjønnevåg } 332666b7793SArve Hjønnevåg 3339b883eeaSMiodrag Dinic static const struct of_device_id goldfish_tty_of_match[] = { 3349b883eeaSMiodrag Dinic { .compatible = "google,goldfish-tty", }, 3359b883eeaSMiodrag Dinic {}, 3369b883eeaSMiodrag Dinic }; 3379b883eeaSMiodrag Dinic 3389b883eeaSMiodrag Dinic MODULE_DEVICE_TABLE(of, goldfish_tty_of_match); 3399b883eeaSMiodrag Dinic 340666b7793SArve Hjønnevåg static struct platform_driver goldfish_tty_platform_driver = { 341666b7793SArve Hjønnevåg .probe = goldfish_tty_probe, 342666b7793SArve Hjønnevåg .remove = goldfish_tty_remove, 343666b7793SArve Hjønnevåg .driver = { 3449b883eeaSMiodrag Dinic .name = "goldfish_tty", 3459b883eeaSMiodrag Dinic .of_match_table = goldfish_tty_of_match, 346666b7793SArve Hjønnevåg } 347666b7793SArve Hjønnevåg }; 348666b7793SArve Hjønnevåg 349666b7793SArve Hjønnevåg module_platform_driver(goldfish_tty_platform_driver); 350666b7793SArve Hjønnevåg 351666b7793SArve Hjønnevåg MODULE_LICENSE("GPL v2"); 352