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> 24666b7793SArve Hjønnevåg 25666b7793SArve Hjønnevåg enum { 26666b7793SArve Hjønnevåg GOLDFISH_TTY_PUT_CHAR = 0x00, 27666b7793SArve Hjønnevåg GOLDFISH_TTY_BYTES_READY = 0x04, 28666b7793SArve Hjønnevåg GOLDFISH_TTY_CMD = 0x08, 29666b7793SArve Hjønnevåg 30666b7793SArve Hjønnevåg GOLDFISH_TTY_DATA_PTR = 0x10, 31666b7793SArve Hjønnevåg GOLDFISH_TTY_DATA_LEN = 0x14, 32*b8658bc8SJun Tian #ifdef CONFIG_64BIT 33*b8658bc8SJun Tian GOLDFISH_TTY_DATA_PTR_HIGH = 0x18, 34*b8658bc8SJun Tian #endif 35666b7793SArve Hjønnevåg 36666b7793SArve Hjønnevåg GOLDFISH_TTY_CMD_INT_DISABLE = 0, 37666b7793SArve Hjønnevåg GOLDFISH_TTY_CMD_INT_ENABLE = 1, 38666b7793SArve Hjønnevåg GOLDFISH_TTY_CMD_WRITE_BUFFER = 2, 39666b7793SArve Hjønnevåg GOLDFISH_TTY_CMD_READ_BUFFER = 3, 40666b7793SArve Hjønnevåg }; 41666b7793SArve Hjønnevåg 42666b7793SArve Hjønnevåg struct goldfish_tty { 43666b7793SArve Hjønnevåg struct tty_port port; 44666b7793SArve Hjønnevåg spinlock_t lock; 45666b7793SArve Hjønnevåg void __iomem *base; 46666b7793SArve Hjønnevåg u32 irq; 47666b7793SArve Hjønnevåg int opencount; 48666b7793SArve Hjønnevåg struct console console; 49666b7793SArve Hjønnevåg }; 50666b7793SArve Hjønnevåg 51666b7793SArve Hjønnevåg static DEFINE_MUTEX(goldfish_tty_lock); 52666b7793SArve Hjønnevåg static struct tty_driver *goldfish_tty_driver; 53666b7793SArve Hjønnevåg static u32 goldfish_tty_line_count = 8; 54666b7793SArve Hjønnevåg static u32 goldfish_tty_current_line_count; 55666b7793SArve Hjønnevåg static struct goldfish_tty *goldfish_ttys; 56666b7793SArve Hjønnevåg 57666b7793SArve Hjønnevåg static void goldfish_tty_do_write(int line, const char *buf, unsigned count) 58666b7793SArve Hjønnevåg { 59666b7793SArve Hjønnevåg unsigned long irq_flags; 60666b7793SArve Hjønnevåg struct goldfish_tty *qtty = &goldfish_ttys[line]; 61666b7793SArve Hjønnevåg void __iomem *base = qtty->base; 62666b7793SArve Hjønnevåg spin_lock_irqsave(&qtty->lock, irq_flags); 63666b7793SArve Hjønnevåg writel((u32)buf, base + GOLDFISH_TTY_DATA_PTR); 64*b8658bc8SJun Tian #ifdef CONFIG_64BIT 65*b8658bc8SJun Tian writel((u32)((u64)buf >> 32), base + GOLDFISH_TTY_DATA_PTR_HIGH); 66*b8658bc8SJun Tian #endif 67666b7793SArve Hjønnevåg writel(count, base + GOLDFISH_TTY_DATA_LEN); 68666b7793SArve Hjønnevåg writel(GOLDFISH_TTY_CMD_WRITE_BUFFER, base + GOLDFISH_TTY_CMD); 69666b7793SArve Hjønnevåg spin_unlock_irqrestore(&qtty->lock, irq_flags); 70666b7793SArve Hjønnevåg } 71666b7793SArve Hjønnevåg 72666b7793SArve Hjønnevåg static irqreturn_t goldfish_tty_interrupt(int irq, void *dev_id) 73666b7793SArve Hjønnevåg { 74666b7793SArve Hjønnevåg struct platform_device *pdev = dev_id; 75666b7793SArve Hjønnevåg struct goldfish_tty *qtty = &goldfish_ttys[pdev->id]; 76666b7793SArve Hjønnevåg void __iomem *base = qtty->base; 77666b7793SArve Hjønnevåg unsigned long irq_flags; 78666b7793SArve Hjønnevåg unsigned char *buf; 79666b7793SArve Hjønnevåg u32 count; 80666b7793SArve Hjønnevåg 81666b7793SArve Hjønnevåg count = readl(base + GOLDFISH_TTY_BYTES_READY); 82666b7793SArve Hjønnevåg if(count == 0) 83666b7793SArve Hjønnevåg return IRQ_NONE; 84666b7793SArve Hjønnevåg 85ebcf0981SAlan Cox count = tty_prepare_flip_string(&qtty->port, &buf, count); 86666b7793SArve Hjønnevåg spin_lock_irqsave(&qtty->lock, irq_flags); 87666b7793SArve Hjønnevåg writel((u32)buf, base + GOLDFISH_TTY_DATA_PTR); 88*b8658bc8SJun Tian #ifdef CONFIG_64BIT 89*b8658bc8SJun Tian writel((u32)((u64)buf >> 32), base + GOLDFISH_TTY_DATA_PTR_HIGH); 90*b8658bc8SJun Tian #endif 91666b7793SArve Hjønnevåg writel(count, base + GOLDFISH_TTY_DATA_LEN); 92666b7793SArve Hjønnevåg writel(GOLDFISH_TTY_CMD_READ_BUFFER, base + GOLDFISH_TTY_CMD); 93666b7793SArve Hjønnevåg spin_unlock_irqrestore(&qtty->lock, irq_flags); 94ebcf0981SAlan Cox tty_schedule_flip(&qtty->port); 95666b7793SArve Hjønnevåg return IRQ_HANDLED; 96666b7793SArve Hjønnevåg } 97666b7793SArve Hjønnevåg 98666b7793SArve Hjønnevåg static int goldfish_tty_activate(struct tty_port *port, struct tty_struct *tty) 99666b7793SArve Hjønnevåg { 100666b7793SArve Hjønnevåg struct goldfish_tty *qtty = container_of(port, struct goldfish_tty, port); 101666b7793SArve Hjønnevåg writel(GOLDFISH_TTY_CMD_INT_ENABLE, qtty->base + GOLDFISH_TTY_CMD); 102666b7793SArve Hjønnevåg return 0; 103666b7793SArve Hjønnevåg } 104666b7793SArve Hjønnevåg 105666b7793SArve Hjønnevåg static void goldfish_tty_shutdown(struct tty_port *port) 106666b7793SArve Hjønnevåg { 107666b7793SArve Hjønnevåg struct goldfish_tty *qtty = container_of(port, struct goldfish_tty, port); 108666b7793SArve Hjønnevåg writel(GOLDFISH_TTY_CMD_INT_DISABLE, qtty->base + GOLDFISH_TTY_CMD); 109666b7793SArve Hjønnevåg } 110666b7793SArve Hjønnevåg 111666b7793SArve Hjønnevåg static int goldfish_tty_open(struct tty_struct * tty, struct file * filp) 112666b7793SArve Hjønnevåg { 113666b7793SArve Hjønnevåg struct goldfish_tty *qtty = &goldfish_ttys[tty->index]; 114666b7793SArve Hjønnevåg return tty_port_open(&qtty->port, tty, filp); 115666b7793SArve Hjønnevåg } 116666b7793SArve Hjønnevåg 117666b7793SArve Hjønnevåg static void goldfish_tty_close(struct tty_struct * tty, struct file * filp) 118666b7793SArve Hjønnevåg { 119666b7793SArve Hjønnevåg tty_port_close(tty->port, tty, filp); 120666b7793SArve Hjønnevåg } 121666b7793SArve Hjønnevåg 122666b7793SArve Hjønnevåg static void goldfish_tty_hangup(struct tty_struct *tty) 123666b7793SArve Hjønnevåg { 124666b7793SArve Hjønnevåg tty_port_hangup(tty->port); 125666b7793SArve Hjønnevåg } 126666b7793SArve Hjønnevåg 127666b7793SArve Hjønnevåg static int goldfish_tty_write(struct tty_struct * tty, const unsigned char *buf, int count) 128666b7793SArve Hjønnevåg { 129666b7793SArve Hjønnevåg goldfish_tty_do_write(tty->index, buf, count); 130666b7793SArve Hjønnevåg return count; 131666b7793SArve Hjønnevåg } 132666b7793SArve Hjønnevåg 133666b7793SArve Hjønnevåg static int goldfish_tty_write_room(struct tty_struct *tty) 134666b7793SArve Hjønnevåg { 135666b7793SArve Hjønnevåg return 0x10000; 136666b7793SArve Hjønnevåg } 137666b7793SArve Hjønnevåg 138666b7793SArve Hjønnevåg static int goldfish_tty_chars_in_buffer(struct tty_struct *tty) 139666b7793SArve Hjønnevåg { 140666b7793SArve Hjønnevåg struct goldfish_tty *qtty = &goldfish_ttys[tty->index]; 141666b7793SArve Hjønnevåg void __iomem *base = qtty->base; 142666b7793SArve Hjønnevåg return readl(base + GOLDFISH_TTY_BYTES_READY); 143666b7793SArve Hjønnevåg } 144666b7793SArve Hjønnevåg 145666b7793SArve Hjønnevåg static void goldfish_tty_console_write(struct console *co, const char *b, unsigned count) 146666b7793SArve Hjønnevåg { 147666b7793SArve Hjønnevåg goldfish_tty_do_write(co->index, b, count); 148666b7793SArve Hjønnevåg } 149666b7793SArve Hjønnevåg 150666b7793SArve Hjønnevåg static struct tty_driver *goldfish_tty_console_device(struct console *c, int *index) 151666b7793SArve Hjønnevåg { 152666b7793SArve Hjønnevåg *index = c->index; 153666b7793SArve Hjønnevåg return goldfish_tty_driver; 154666b7793SArve Hjønnevåg } 155666b7793SArve Hjønnevåg 156666b7793SArve Hjønnevåg static int goldfish_tty_console_setup(struct console *co, char *options) 157666b7793SArve Hjønnevåg { 158666b7793SArve Hjønnevåg if((unsigned)co->index > goldfish_tty_line_count) 159666b7793SArve Hjønnevåg return -ENODEV; 160666b7793SArve Hjønnevåg if(goldfish_ttys[co->index].base == 0) 161666b7793SArve Hjønnevåg return -ENODEV; 162666b7793SArve Hjønnevåg return 0; 163666b7793SArve Hjønnevåg } 164666b7793SArve Hjønnevåg 165666b7793SArve Hjønnevåg static struct tty_port_operations goldfish_port_ops = { 166666b7793SArve Hjønnevåg .activate = goldfish_tty_activate, 167666b7793SArve Hjønnevåg .shutdown = goldfish_tty_shutdown 168666b7793SArve Hjønnevåg }; 169666b7793SArve Hjønnevåg 170666b7793SArve Hjønnevåg static struct tty_operations goldfish_tty_ops = { 171666b7793SArve Hjønnevåg .open = goldfish_tty_open, 172666b7793SArve Hjønnevåg .close = goldfish_tty_close, 173666b7793SArve Hjønnevåg .hangup = goldfish_tty_hangup, 174666b7793SArve Hjønnevåg .write = goldfish_tty_write, 175666b7793SArve Hjønnevåg .write_room = goldfish_tty_write_room, 176666b7793SArve Hjønnevåg .chars_in_buffer = goldfish_tty_chars_in_buffer, 177666b7793SArve Hjønnevåg }; 178666b7793SArve Hjønnevåg 179666b7793SArve Hjønnevåg static int goldfish_tty_create_driver(void) 180666b7793SArve Hjønnevåg { 181666b7793SArve Hjønnevåg int ret; 182666b7793SArve Hjønnevåg struct tty_driver *tty; 183666b7793SArve Hjønnevåg 184666b7793SArve Hjønnevåg goldfish_ttys = kzalloc(sizeof(*goldfish_ttys) * 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; 199666b7793SArve Hjønnevåg tty->flags = TTY_DRIVER_RESET_TERMIOS | TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV; 200666b7793SArve Hjønnevåg tty_set_operations(tty, &goldfish_tty_ops); 201666b7793SArve Hjønnevåg ret = tty_register_driver(tty); 202666b7793SArve Hjønnevåg if(ret) 203666b7793SArve Hjønnevåg goto err_tty_register_driver_failed; 204666b7793SArve Hjønnevåg 205666b7793SArve Hjønnevåg goldfish_tty_driver = tty; 206666b7793SArve Hjønnevåg return 0; 207666b7793SArve Hjønnevåg 208666b7793SArve Hjønnevåg err_tty_register_driver_failed: 209666b7793SArve Hjønnevåg put_tty_driver(tty); 210666b7793SArve Hjønnevåg err_alloc_tty_driver_failed: 211666b7793SArve Hjønnevåg kfree(goldfish_ttys); 212666b7793SArve Hjønnevåg goldfish_ttys = NULL; 213666b7793SArve Hjønnevåg err_alloc_goldfish_ttys_failed: 214666b7793SArve Hjønnevåg return ret; 215666b7793SArve Hjønnevåg } 216666b7793SArve Hjønnevåg 217666b7793SArve Hjønnevåg static void goldfish_tty_delete_driver(void) 218666b7793SArve Hjønnevåg { 219666b7793SArve Hjønnevåg tty_unregister_driver(goldfish_tty_driver); 220666b7793SArve Hjønnevåg put_tty_driver(goldfish_tty_driver); 221666b7793SArve Hjønnevåg goldfish_tty_driver = NULL; 222666b7793SArve Hjønnevåg kfree(goldfish_ttys); 223666b7793SArve Hjønnevåg goldfish_ttys = NULL; 224666b7793SArve Hjønnevåg } 225666b7793SArve Hjønnevåg 226666b7793SArve Hjønnevåg static int goldfish_tty_probe(struct platform_device *pdev) 227666b7793SArve Hjønnevåg { 228666b7793SArve Hjønnevåg struct goldfish_tty *qtty; 229666b7793SArve Hjønnevåg int ret = -EINVAL; 230666b7793SArve Hjønnevåg int i; 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; 235666b7793SArve Hjønnevåg 236666b7793SArve Hjønnevåg r = platform_get_resource(pdev, IORESOURCE_MEM, 0); 237666b7793SArve Hjønnevåg if(r == NULL) 238666b7793SArve Hjønnevåg return -EINVAL; 239666b7793SArve Hjønnevåg 240666b7793SArve Hjønnevåg base = ioremap(r->start, 0x1000); 241666b7793SArve Hjønnevåg if (base == NULL) 242666b7793SArve Hjønnevåg pr_err("goldfish_tty: unable to remap base\n"); 243666b7793SArve Hjønnevåg 244666b7793SArve Hjønnevåg r = platform_get_resource(pdev, IORESOURCE_IRQ, 0); 245666b7793SArve Hjønnevåg if(r == NULL) 246666b7793SArve Hjønnevåg goto err_unmap; 247666b7793SArve Hjønnevåg 248666b7793SArve Hjønnevåg irq = r->start; 249666b7793SArve Hjønnevåg 250666b7793SArve Hjønnevåg if(pdev->id >= goldfish_tty_line_count) 251666b7793SArve Hjønnevåg goto err_unmap; 252666b7793SArve Hjønnevåg 253666b7793SArve Hjønnevåg mutex_lock(&goldfish_tty_lock); 254666b7793SArve Hjønnevåg if(goldfish_tty_current_line_count == 0) { 255666b7793SArve Hjønnevåg ret = goldfish_tty_create_driver(); 256666b7793SArve Hjønnevåg if(ret) 257666b7793SArve Hjønnevåg goto err_create_driver_failed; 258666b7793SArve Hjønnevåg } 259666b7793SArve Hjønnevåg goldfish_tty_current_line_count++; 260666b7793SArve Hjønnevåg 261666b7793SArve Hjønnevåg qtty = &goldfish_ttys[pdev->id]; 262666b7793SArve Hjønnevåg spin_lock_init(&qtty->lock); 263666b7793SArve Hjønnevåg tty_port_init(&qtty->port); 264666b7793SArve Hjønnevåg qtty->port.ops = &goldfish_port_ops; 265666b7793SArve Hjønnevåg qtty->base = base; 266666b7793SArve Hjønnevåg qtty->irq = irq; 267666b7793SArve Hjønnevåg 268666b7793SArve Hjønnevåg writel(GOLDFISH_TTY_CMD_INT_DISABLE, base + GOLDFISH_TTY_CMD); 269666b7793SArve Hjønnevåg 270666b7793SArve Hjønnevåg ret = request_irq(irq, goldfish_tty_interrupt, IRQF_SHARED, "goldfish_tty", pdev); 271666b7793SArve Hjønnevåg if(ret) 272666b7793SArve Hjønnevåg goto err_request_irq_failed; 273666b7793SArve Hjønnevåg 274666b7793SArve Hjønnevåg 275666b7793SArve Hjønnevåg ttydev = tty_port_register_device(&qtty->port, goldfish_tty_driver, 276666b7793SArve Hjønnevåg pdev->id, &pdev->dev); 277666b7793SArve Hjønnevåg if(IS_ERR(ttydev)) { 278666b7793SArve Hjønnevåg ret = PTR_ERR(ttydev); 279666b7793SArve Hjønnevåg goto err_tty_register_device_failed; 280666b7793SArve Hjønnevåg } 281666b7793SArve Hjønnevåg 282666b7793SArve Hjønnevåg strcpy(qtty->console.name, "ttyGF"); 283666b7793SArve Hjønnevåg qtty->console.write = goldfish_tty_console_write; 284666b7793SArve Hjønnevåg qtty->console.device = goldfish_tty_console_device; 285666b7793SArve Hjønnevåg qtty->console.setup = goldfish_tty_console_setup; 286666b7793SArve Hjønnevåg qtty->console.flags = CON_PRINTBUFFER; 287666b7793SArve Hjønnevåg qtty->console.index = pdev->id; 288666b7793SArve Hjønnevåg register_console(&qtty->console); 289666b7793SArve Hjønnevåg 290666b7793SArve Hjønnevåg mutex_unlock(&goldfish_tty_lock); 291666b7793SArve Hjønnevåg return 0; 292666b7793SArve Hjønnevåg 293666b7793SArve Hjønnevåg tty_unregister_device(goldfish_tty_driver, i); 294666b7793SArve Hjønnevåg err_tty_register_device_failed: 295666b7793SArve Hjønnevåg free_irq(irq, pdev); 296666b7793SArve Hjønnevåg err_request_irq_failed: 297666b7793SArve Hjønnevåg goldfish_tty_current_line_count--; 298666b7793SArve Hjønnevåg if(goldfish_tty_current_line_count == 0) 299666b7793SArve Hjønnevåg goldfish_tty_delete_driver(); 300666b7793SArve Hjønnevåg err_create_driver_failed: 301666b7793SArve Hjønnevåg mutex_unlock(&goldfish_tty_lock); 302666b7793SArve Hjønnevåg err_unmap: 303666b7793SArve Hjønnevåg iounmap(base); 304666b7793SArve Hjønnevåg return ret; 305666b7793SArve Hjønnevåg } 306666b7793SArve Hjønnevåg 307666b7793SArve Hjønnevåg static int goldfish_tty_remove(struct platform_device *pdev) 308666b7793SArve Hjønnevåg { 309666b7793SArve Hjønnevåg struct goldfish_tty *qtty; 310666b7793SArve Hjønnevåg 311666b7793SArve Hjønnevåg mutex_lock(&goldfish_tty_lock); 312666b7793SArve Hjønnevåg 313666b7793SArve Hjønnevåg qtty = &goldfish_ttys[pdev->id]; 314666b7793SArve Hjønnevåg unregister_console(&qtty->console); 315666b7793SArve Hjønnevåg tty_unregister_device(goldfish_tty_driver, pdev->id); 316666b7793SArve Hjønnevåg iounmap(qtty->base); 317666b7793SArve Hjønnevåg qtty->base = 0; 318666b7793SArve Hjønnevåg free_irq(qtty->irq, pdev); 319666b7793SArve Hjønnevåg goldfish_tty_current_line_count--; 320666b7793SArve Hjønnevåg if(goldfish_tty_current_line_count == 0) 321666b7793SArve Hjønnevåg goldfish_tty_delete_driver(); 322666b7793SArve Hjønnevåg mutex_unlock(&goldfish_tty_lock); 323666b7793SArve Hjønnevåg return 0; 324666b7793SArve Hjønnevåg } 325666b7793SArve Hjønnevåg 326666b7793SArve Hjønnevåg static struct platform_driver goldfish_tty_platform_driver = { 327666b7793SArve Hjønnevåg .probe = goldfish_tty_probe, 328666b7793SArve Hjønnevåg .remove = goldfish_tty_remove, 329666b7793SArve Hjønnevåg .driver = { 330666b7793SArve Hjønnevåg .name = "goldfish_tty" 331666b7793SArve Hjønnevåg } 332666b7793SArve Hjønnevåg }; 333666b7793SArve Hjønnevåg 334666b7793SArve Hjønnevåg module_platform_driver(goldfish_tty_platform_driver); 335666b7793SArve Hjønnevåg 336666b7793SArve Hjønnevåg MODULE_LICENSE("GPL v2"); 337