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/init.h> 18666b7793SArve Hjønnevåg #include <linux/interrupt.h> 19666b7793SArve Hjønnevåg #include <linux/platform_device.h> 20666b7793SArve Hjønnevåg #include <linux/tty.h> 21666b7793SArve Hjønnevåg #include <linux/tty_flip.h> 22666b7793SArve Hjønnevåg #include <linux/slab.h> 23666b7793SArve Hjønnevåg #include <linux/io.h> 24666b7793SArve Hjønnevåg #include <linux/module.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, 33666b7793SArve Hjønnevåg 34666b7793SArve Hjønnevåg GOLDFISH_TTY_CMD_INT_DISABLE = 0, 35666b7793SArve Hjønnevåg GOLDFISH_TTY_CMD_INT_ENABLE = 1, 36666b7793SArve Hjønnevåg GOLDFISH_TTY_CMD_WRITE_BUFFER = 2, 37666b7793SArve Hjønnevåg GOLDFISH_TTY_CMD_READ_BUFFER = 3, 38666b7793SArve Hjønnevåg }; 39666b7793SArve Hjønnevåg 40666b7793SArve Hjønnevåg struct goldfish_tty { 41666b7793SArve Hjønnevåg struct tty_port port; 42666b7793SArve Hjønnevåg spinlock_t lock; 43666b7793SArve Hjønnevåg void __iomem *base; 44666b7793SArve Hjønnevåg u32 irq; 45666b7793SArve Hjønnevåg int opencount; 46666b7793SArve Hjønnevåg struct console console; 47666b7793SArve Hjønnevåg }; 48666b7793SArve Hjønnevåg 49666b7793SArve Hjønnevåg static DEFINE_MUTEX(goldfish_tty_lock); 50666b7793SArve Hjønnevåg static struct tty_driver *goldfish_tty_driver; 51666b7793SArve Hjønnevåg static u32 goldfish_tty_line_count = 8; 52666b7793SArve Hjønnevåg static u32 goldfish_tty_current_line_count; 53666b7793SArve Hjønnevåg static struct goldfish_tty *goldfish_ttys; 54666b7793SArve Hjønnevåg 55666b7793SArve Hjønnevåg static void goldfish_tty_do_write(int line, const char *buf, unsigned count) 56666b7793SArve Hjønnevåg { 57666b7793SArve Hjønnevåg unsigned long irq_flags; 58666b7793SArve Hjønnevåg struct goldfish_tty *qtty = &goldfish_ttys[line]; 59666b7793SArve Hjønnevåg void __iomem *base = qtty->base; 60666b7793SArve Hjønnevåg spin_lock_irqsave(&qtty->lock, irq_flags); 61666b7793SArve Hjønnevåg writel((u32)buf, base + GOLDFISH_TTY_DATA_PTR); 62666b7793SArve Hjønnevåg writel(count, base + GOLDFISH_TTY_DATA_LEN); 63666b7793SArve Hjønnevåg writel(GOLDFISH_TTY_CMD_WRITE_BUFFER, base + GOLDFISH_TTY_CMD); 64666b7793SArve Hjønnevåg spin_unlock_irqrestore(&qtty->lock, irq_flags); 65666b7793SArve Hjønnevåg } 66666b7793SArve Hjønnevåg 67666b7793SArve Hjønnevåg static irqreturn_t goldfish_tty_interrupt(int irq, void *dev_id) 68666b7793SArve Hjønnevåg { 69666b7793SArve Hjønnevåg struct platform_device *pdev = dev_id; 70666b7793SArve Hjønnevåg struct goldfish_tty *qtty = &goldfish_ttys[pdev->id]; 71666b7793SArve Hjønnevåg void __iomem *base = qtty->base; 72666b7793SArve Hjønnevåg unsigned long irq_flags; 73666b7793SArve Hjønnevåg unsigned char *buf; 74666b7793SArve Hjønnevåg u32 count; 75666b7793SArve Hjønnevåg struct tty_struct *tty; 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 81666b7793SArve Hjønnevåg tty = tty_port_tty_get(&qtty->port); 82666b7793SArve Hjønnevåg if (tty) { 83666b7793SArve Hjønnevåg count = tty_prepare_flip_string(tty, &buf, count); 84666b7793SArve Hjønnevåg spin_lock_irqsave(&qtty->lock, irq_flags); 85666b7793SArve Hjønnevåg writel((u32)buf, base + GOLDFISH_TTY_DATA_PTR); 86666b7793SArve Hjønnevåg writel(count, base + GOLDFISH_TTY_DATA_LEN); 87666b7793SArve Hjønnevåg writel(GOLDFISH_TTY_CMD_READ_BUFFER, base + GOLDFISH_TTY_CMD); 88666b7793SArve Hjønnevåg spin_unlock_irqrestore(&qtty->lock, irq_flags); 89666b7793SArve Hjønnevåg tty_schedule_flip(tty); 90666b7793SArve Hjønnevåg tty_kref_put(tty); 91666b7793SArve Hjønnevåg } 92666b7793SArve Hjønnevåg return IRQ_HANDLED; 93666b7793SArve Hjønnevåg } 94666b7793SArve Hjønnevåg 95666b7793SArve Hjønnevåg static int goldfish_tty_activate(struct tty_port *port, struct tty_struct *tty) 96666b7793SArve Hjønnevåg { 97666b7793SArve Hjønnevåg struct goldfish_tty *qtty = container_of(port, struct goldfish_tty, port); 98666b7793SArve Hjønnevåg writel(GOLDFISH_TTY_CMD_INT_ENABLE, qtty->base + GOLDFISH_TTY_CMD); 99666b7793SArve Hjønnevåg return 0; 100666b7793SArve Hjønnevåg } 101666b7793SArve Hjønnevåg 102666b7793SArve Hjønnevåg static void goldfish_tty_shutdown(struct tty_port *port) 103666b7793SArve Hjønnevåg { 104666b7793SArve Hjønnevåg struct goldfish_tty *qtty = container_of(port, struct goldfish_tty, port); 105666b7793SArve Hjønnevåg writel(GOLDFISH_TTY_CMD_INT_DISABLE, qtty->base + GOLDFISH_TTY_CMD); 106666b7793SArve Hjønnevåg } 107666b7793SArve Hjønnevåg 108666b7793SArve Hjønnevåg static int goldfish_tty_open(struct tty_struct * tty, struct file * filp) 109666b7793SArve Hjønnevåg { 110666b7793SArve Hjønnevåg struct goldfish_tty *qtty = &goldfish_ttys[tty->index]; 111666b7793SArve Hjønnevåg return tty_port_open(&qtty->port, tty, filp); 112666b7793SArve Hjønnevåg } 113666b7793SArve Hjønnevåg 114666b7793SArve Hjønnevåg static void goldfish_tty_close(struct tty_struct * tty, struct file * filp) 115666b7793SArve Hjønnevåg { 116666b7793SArve Hjønnevåg tty_port_close(tty->port, tty, filp); 117666b7793SArve Hjønnevåg } 118666b7793SArve Hjønnevåg 119666b7793SArve Hjønnevåg static void goldfish_tty_hangup(struct tty_struct *tty) 120666b7793SArve Hjønnevåg { 121666b7793SArve Hjønnevåg tty_port_hangup(tty->port); 122666b7793SArve Hjønnevåg } 123666b7793SArve Hjønnevåg 124666b7793SArve Hjønnevåg static int goldfish_tty_write(struct tty_struct * tty, const unsigned char *buf, 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 142666b7793SArve Hjønnevåg static void goldfish_tty_console_write(struct console *co, const char *b, unsigned count) 143666b7793SArve Hjønnevåg { 144666b7793SArve Hjønnevåg goldfish_tty_do_write(co->index, b, count); 145666b7793SArve Hjønnevåg } 146666b7793SArve Hjønnevåg 147666b7793SArve Hjønnevåg static struct tty_driver *goldfish_tty_console_device(struct console *c, int *index) 148666b7793SArve Hjønnevåg { 149666b7793SArve Hjønnevåg *index = c->index; 150666b7793SArve Hjønnevåg return goldfish_tty_driver; 151666b7793SArve Hjønnevåg } 152666b7793SArve Hjønnevåg 153666b7793SArve Hjønnevåg static int goldfish_tty_console_setup(struct console *co, char *options) 154666b7793SArve Hjønnevåg { 155666b7793SArve Hjønnevåg if((unsigned)co->index > goldfish_tty_line_count) 156666b7793SArve Hjønnevåg return -ENODEV; 157666b7793SArve Hjønnevåg if(goldfish_ttys[co->index].base == 0) 158666b7793SArve Hjønnevåg return -ENODEV; 159666b7793SArve Hjønnevåg return 0; 160666b7793SArve Hjønnevåg } 161666b7793SArve Hjønnevåg 162666b7793SArve Hjønnevåg static struct tty_port_operations goldfish_port_ops = { 163666b7793SArve Hjønnevåg .activate = goldfish_tty_activate, 164666b7793SArve Hjønnevåg .shutdown = goldfish_tty_shutdown 165666b7793SArve Hjønnevåg }; 166666b7793SArve Hjønnevåg 167666b7793SArve Hjønnevåg static struct tty_operations goldfish_tty_ops = { 168666b7793SArve Hjønnevåg .open = goldfish_tty_open, 169666b7793SArve Hjønnevåg .close = goldfish_tty_close, 170666b7793SArve Hjønnevåg .hangup = goldfish_tty_hangup, 171666b7793SArve Hjønnevåg .write = goldfish_tty_write, 172666b7793SArve Hjønnevåg .write_room = goldfish_tty_write_room, 173666b7793SArve Hjønnevåg .chars_in_buffer = goldfish_tty_chars_in_buffer, 174666b7793SArve Hjønnevåg }; 175666b7793SArve Hjønnevåg 176666b7793SArve Hjønnevåg static int goldfish_tty_create_driver(void) 177666b7793SArve Hjønnevåg { 178666b7793SArve Hjønnevåg int ret; 179666b7793SArve Hjønnevåg struct tty_driver *tty; 180666b7793SArve Hjønnevåg 181666b7793SArve Hjønnevåg goldfish_ttys = kzalloc(sizeof(*goldfish_ttys) * goldfish_tty_line_count, GFP_KERNEL); 182666b7793SArve Hjønnevåg if(goldfish_ttys == NULL) { 183666b7793SArve Hjønnevåg ret = -ENOMEM; 184666b7793SArve Hjønnevåg goto err_alloc_goldfish_ttys_failed; 185666b7793SArve Hjønnevåg } 186666b7793SArve Hjønnevåg tty = alloc_tty_driver(goldfish_tty_line_count); 187666b7793SArve Hjønnevåg if(tty == NULL) { 188666b7793SArve Hjønnevåg ret = -ENOMEM; 189666b7793SArve Hjønnevåg goto err_alloc_tty_driver_failed; 190666b7793SArve Hjønnevåg } 191666b7793SArve Hjønnevåg tty->driver_name = "goldfish"; 192666b7793SArve Hjønnevåg tty->name = "ttyGF"; 193666b7793SArve Hjønnevåg tty->type = TTY_DRIVER_TYPE_SERIAL; 194666b7793SArve Hjønnevåg tty->subtype = SERIAL_TYPE_NORMAL; 195666b7793SArve Hjønnevåg tty->init_termios = tty_std_termios; 196666b7793SArve Hjønnevåg tty->flags = TTY_DRIVER_RESET_TERMIOS | TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV; 197666b7793SArve Hjønnevåg tty_set_operations(tty, &goldfish_tty_ops); 198666b7793SArve Hjønnevåg ret = tty_register_driver(tty); 199666b7793SArve Hjønnevåg if(ret) 200666b7793SArve Hjønnevåg goto err_tty_register_driver_failed; 201666b7793SArve Hjønnevåg 202666b7793SArve Hjønnevåg goldfish_tty_driver = tty; 203666b7793SArve Hjønnevåg return 0; 204666b7793SArve Hjønnevåg 205666b7793SArve Hjønnevåg err_tty_register_driver_failed: 206666b7793SArve Hjønnevåg put_tty_driver(tty); 207666b7793SArve Hjønnevåg err_alloc_tty_driver_failed: 208666b7793SArve Hjønnevåg kfree(goldfish_ttys); 209666b7793SArve Hjønnevåg goldfish_ttys = NULL; 210666b7793SArve Hjønnevåg err_alloc_goldfish_ttys_failed: 211666b7793SArve Hjønnevåg return ret; 212666b7793SArve Hjønnevåg } 213666b7793SArve Hjønnevåg 214666b7793SArve Hjønnevåg static void goldfish_tty_delete_driver(void) 215666b7793SArve Hjønnevåg { 216666b7793SArve Hjønnevåg tty_unregister_driver(goldfish_tty_driver); 217666b7793SArve Hjønnevåg put_tty_driver(goldfish_tty_driver); 218666b7793SArve Hjønnevåg goldfish_tty_driver = NULL; 219666b7793SArve Hjønnevåg kfree(goldfish_ttys); 220666b7793SArve Hjønnevåg goldfish_ttys = NULL; 221666b7793SArve Hjønnevåg } 222666b7793SArve Hjønnevåg 223666b7793SArve Hjønnevåg static int goldfish_tty_probe(struct platform_device *pdev) 224666b7793SArve Hjønnevåg { 225666b7793SArve Hjønnevåg struct goldfish_tty *qtty; 226666b7793SArve Hjønnevåg int ret = -EINVAL; 227666b7793SArve Hjønnevåg int i; 228666b7793SArve Hjønnevåg struct resource *r; 229666b7793SArve Hjønnevåg struct device *ttydev; 230666b7793SArve Hjønnevåg void __iomem *base; 231666b7793SArve Hjønnevåg u32 irq; 232666b7793SArve Hjønnevåg 233666b7793SArve Hjønnevåg r = platform_get_resource(pdev, IORESOURCE_MEM, 0); 234666b7793SArve Hjønnevåg if(r == NULL) 235666b7793SArve Hjønnevåg return -EINVAL; 236666b7793SArve Hjønnevåg 237666b7793SArve Hjønnevåg base = ioremap(r->start, 0x1000); 238666b7793SArve Hjønnevåg if (base == NULL) 239666b7793SArve Hjønnevåg pr_err("goldfish_tty: unable to remap base\n"); 240666b7793SArve Hjønnevåg 241666b7793SArve Hjønnevåg r = platform_get_resource(pdev, IORESOURCE_IRQ, 0); 242666b7793SArve Hjønnevåg if(r == NULL) 243666b7793SArve Hjønnevåg goto err_unmap; 244666b7793SArve Hjønnevåg 245666b7793SArve Hjønnevåg irq = r->start; 246666b7793SArve Hjønnevåg 247666b7793SArve Hjønnevåg if(pdev->id >= goldfish_tty_line_count) 248666b7793SArve Hjønnevåg goto err_unmap; 249666b7793SArve Hjønnevåg 250666b7793SArve Hjønnevåg mutex_lock(&goldfish_tty_lock); 251666b7793SArve Hjønnevåg if(goldfish_tty_current_line_count == 0) { 252666b7793SArve Hjønnevåg ret = goldfish_tty_create_driver(); 253666b7793SArve Hjønnevåg if(ret) 254666b7793SArve Hjønnevåg goto err_create_driver_failed; 255666b7793SArve Hjønnevåg } 256666b7793SArve Hjønnevåg goldfish_tty_current_line_count++; 257666b7793SArve Hjønnevåg 258666b7793SArve Hjønnevåg qtty = &goldfish_ttys[pdev->id]; 259666b7793SArve Hjønnevåg spin_lock_init(&qtty->lock); 260666b7793SArve Hjønnevåg tty_port_init(&qtty->port); 261666b7793SArve Hjønnevåg qtty->port.ops = &goldfish_port_ops; 262666b7793SArve Hjønnevåg qtty->base = base; 263666b7793SArve Hjønnevåg qtty->irq = irq; 264666b7793SArve Hjønnevåg 265666b7793SArve Hjønnevåg writel(GOLDFISH_TTY_CMD_INT_DISABLE, base + GOLDFISH_TTY_CMD); 266666b7793SArve Hjønnevåg 267666b7793SArve Hjønnevåg ret = request_irq(irq, goldfish_tty_interrupt, IRQF_SHARED, "goldfish_tty", pdev); 268666b7793SArve Hjønnevåg if(ret) 269666b7793SArve Hjønnevåg goto err_request_irq_failed; 270666b7793SArve Hjønnevåg 271666b7793SArve Hjønnevåg 272666b7793SArve Hjønnevåg ttydev = tty_port_register_device(&qtty->port, goldfish_tty_driver, 273666b7793SArve Hjønnevåg pdev->id, &pdev->dev); 274666b7793SArve Hjønnevåg if(IS_ERR(ttydev)) { 275666b7793SArve Hjønnevåg ret = PTR_ERR(ttydev); 276666b7793SArve Hjønnevåg goto err_tty_register_device_failed; 277666b7793SArve Hjønnevåg } 278666b7793SArve Hjønnevåg 279666b7793SArve Hjønnevåg strcpy(qtty->console.name, "ttyGF"); 280666b7793SArve Hjønnevåg qtty->console.write = goldfish_tty_console_write; 281666b7793SArve Hjønnevåg qtty->console.device = goldfish_tty_console_device; 282666b7793SArve Hjønnevåg qtty->console.setup = goldfish_tty_console_setup; 283666b7793SArve Hjønnevåg qtty->console.flags = CON_PRINTBUFFER; 284666b7793SArve Hjønnevåg qtty->console.index = pdev->id; 285666b7793SArve Hjønnevåg register_console(&qtty->console); 286666b7793SArve Hjønnevåg 287666b7793SArve Hjønnevåg mutex_unlock(&goldfish_tty_lock); 288666b7793SArve Hjønnevåg return 0; 289666b7793SArve Hjønnevåg 290666b7793SArve Hjønnevåg tty_unregister_device(goldfish_tty_driver, i); 291666b7793SArve Hjønnevåg err_tty_register_device_failed: 292666b7793SArve Hjønnevåg free_irq(irq, pdev); 293666b7793SArve Hjønnevåg err_request_irq_failed: 294666b7793SArve Hjønnevåg goldfish_tty_current_line_count--; 295666b7793SArve Hjønnevåg if(goldfish_tty_current_line_count == 0) 296666b7793SArve Hjønnevåg goldfish_tty_delete_driver(); 297666b7793SArve Hjønnevåg err_create_driver_failed: 298666b7793SArve Hjønnevåg mutex_unlock(&goldfish_tty_lock); 299666b7793SArve Hjønnevåg err_unmap: 300666b7793SArve Hjønnevåg iounmap(base); 301666b7793SArve Hjønnevåg return ret; 302666b7793SArve Hjønnevåg } 303666b7793SArve Hjønnevåg 304666b7793SArve Hjønnevåg static int goldfish_tty_remove(struct platform_device *pdev) 305666b7793SArve Hjønnevåg { 306666b7793SArve Hjønnevåg struct goldfish_tty *qtty; 307666b7793SArve Hjønnevåg 308666b7793SArve Hjønnevåg mutex_lock(&goldfish_tty_lock); 309666b7793SArve Hjønnevåg 310666b7793SArve Hjønnevåg qtty = &goldfish_ttys[pdev->id]; 311666b7793SArve Hjønnevåg unregister_console(&qtty->console); 312666b7793SArve Hjønnevåg tty_unregister_device(goldfish_tty_driver, pdev->id); 313666b7793SArve Hjønnevåg iounmap(qtty->base); 314666b7793SArve Hjønnevåg qtty->base = 0; 315666b7793SArve Hjønnevåg free_irq(qtty->irq, pdev); 316666b7793SArve Hjønnevåg goldfish_tty_current_line_count--; 317666b7793SArve Hjønnevåg if(goldfish_tty_current_line_count == 0) 318666b7793SArve Hjønnevåg goldfish_tty_delete_driver(); 319666b7793SArve Hjønnevåg mutex_unlock(&goldfish_tty_lock); 320666b7793SArve Hjønnevåg return 0; 321666b7793SArve Hjønnevåg } 322666b7793SArve Hjønnevåg 323666b7793SArve Hjønnevåg static struct platform_driver goldfish_tty_platform_driver = { 324666b7793SArve Hjønnevåg .probe = goldfish_tty_probe, 325666b7793SArve Hjønnevåg .remove = goldfish_tty_remove, 326666b7793SArve Hjønnevåg .driver = { 327666b7793SArve Hjønnevåg .name = "goldfish_tty" 328666b7793SArve Hjønnevåg } 329666b7793SArve Hjønnevåg }; 330666b7793SArve Hjønnevåg 331666b7793SArve Hjønnevåg module_platform_driver(goldfish_tty_platform_driver); 332666b7793SArve Hjønnevåg 333666b7793SArve Hjønnevåg MODULE_LICENSE("GPL v2"); 334