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); 62e0f682e0SAlan gf_write64((u64)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 { 71666b7793SArve Hjønnevåg struct platform_device *pdev = dev_id; 72666b7793SArve Hjønnevåg struct goldfish_tty *qtty = &goldfish_ttys[pdev->id]; 73666b7793SArve Hjønnevåg void __iomem *base = qtty->base; 74666b7793SArve Hjønnevåg unsigned long irq_flags; 75666b7793SArve Hjønnevåg unsigned char *buf; 76666b7793SArve Hjønnevåg u32 count; 77666b7793SArve Hjønnevåg 78666b7793SArve Hjønnevåg count = readl(base + GOLDFISH_TTY_BYTES_READY); 79666b7793SArve Hjønnevåg if (count == 0) 80666b7793SArve Hjønnevåg return IRQ_NONE; 81666b7793SArve Hjønnevåg 82ebcf0981SAlan Cox count = tty_prepare_flip_string(&qtty->port, &buf, count); 83666b7793SArve Hjønnevåg spin_lock_irqsave(&qtty->lock, irq_flags); 84e0f682e0SAlan gf_write64((u64)buf, base + GOLDFISH_TTY_DATA_PTR, 85e0f682e0SAlan base + GOLDFISH_TTY_DATA_PTR_HIGH); 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); 89ebcf0981SAlan Cox tty_schedule_flip(&qtty->port); 90666b7793SArve Hjønnevåg return IRQ_HANDLED; 91666b7793SArve Hjønnevåg } 92666b7793SArve Hjønnevåg 93666b7793SArve Hjønnevåg static int goldfish_tty_activate(struct tty_port *port, struct tty_struct *tty) 94666b7793SArve Hjønnevåg { 95d78055dcSAlan struct goldfish_tty *qtty = container_of(port, struct goldfish_tty, 96d78055dcSAlan port); 97666b7793SArve Hjønnevåg writel(GOLDFISH_TTY_CMD_INT_ENABLE, qtty->base + GOLDFISH_TTY_CMD); 98666b7793SArve Hjønnevåg return 0; 99666b7793SArve Hjønnevåg } 100666b7793SArve Hjønnevåg 101666b7793SArve Hjønnevåg static void goldfish_tty_shutdown(struct tty_port *port) 102666b7793SArve Hjønnevåg { 103d78055dcSAlan struct goldfish_tty *qtty = container_of(port, struct goldfish_tty, 104d78055dcSAlan 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 124d78055dcSAlan static int goldfish_tty_write(struct tty_struct *tty, const unsigned char *buf, 125d78055dcSAlan int count) 126666b7793SArve Hjønnevåg { 127666b7793SArve Hjønnevåg goldfish_tty_do_write(tty->index, buf, count); 128666b7793SArve Hjønnevåg return count; 129666b7793SArve Hjønnevåg } 130666b7793SArve Hjønnevåg 131666b7793SArve Hjønnevåg static int goldfish_tty_write_room(struct tty_struct *tty) 132666b7793SArve Hjønnevåg { 133666b7793SArve Hjønnevåg return 0x10000; 134666b7793SArve Hjønnevåg } 135666b7793SArve Hjønnevåg 136666b7793SArve Hjønnevåg static int goldfish_tty_chars_in_buffer(struct tty_struct *tty) 137666b7793SArve Hjønnevåg { 138666b7793SArve Hjønnevåg struct goldfish_tty *qtty = &goldfish_ttys[tty->index]; 139666b7793SArve Hjønnevåg void __iomem *base = qtty->base; 140666b7793SArve Hjønnevåg return readl(base + GOLDFISH_TTY_BYTES_READY); 141666b7793SArve Hjønnevåg } 142666b7793SArve Hjønnevåg 143d78055dcSAlan static void goldfish_tty_console_write(struct console *co, const char *b, 144d78055dcSAlan unsigned count) 145666b7793SArve Hjønnevåg { 146666b7793SArve Hjønnevåg goldfish_tty_do_write(co->index, b, count); 147666b7793SArve Hjønnevåg } 148666b7793SArve Hjønnevåg 149d78055dcSAlan static struct tty_driver *goldfish_tty_console_device(struct console *c, 150d78055dcSAlan 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; 160*a4dc9236SFabian Frederick if (!goldfish_ttys[co->index].base) 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 170d78055dcSAlan static const 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 184d78055dcSAlan goldfish_ttys = kzalloc(sizeof(*goldfish_ttys) * 185d78055dcSAlan goldfish_tty_line_count, GFP_KERNEL); 186666b7793SArve Hjønnevåg if (goldfish_ttys == NULL) { 187666b7793SArve Hjønnevåg ret = -ENOMEM; 188666b7793SArve Hjønnevåg goto err_alloc_goldfish_ttys_failed; 189666b7793SArve Hjønnevåg } 190666b7793SArve Hjønnevåg tty = alloc_tty_driver(goldfish_tty_line_count); 191666b7793SArve Hjønnevåg if (tty == NULL) { 192666b7793SArve Hjønnevåg ret = -ENOMEM; 193666b7793SArve Hjønnevåg goto err_alloc_tty_driver_failed; 194666b7793SArve Hjønnevåg } 195666b7793SArve Hjønnevåg tty->driver_name = "goldfish"; 196666b7793SArve Hjønnevåg tty->name = "ttyGF"; 197666b7793SArve Hjønnevåg tty->type = TTY_DRIVER_TYPE_SERIAL; 198666b7793SArve Hjønnevåg tty->subtype = SERIAL_TYPE_NORMAL; 199666b7793SArve Hjønnevåg tty->init_termios = tty_std_termios; 200d78055dcSAlan tty->flags = TTY_DRIVER_RESET_TERMIOS | TTY_DRIVER_REAL_RAW | 201d78055dcSAlan TTY_DRIVER_DYNAMIC_DEV; 202666b7793SArve Hjønnevåg tty_set_operations(tty, &goldfish_tty_ops); 203666b7793SArve Hjønnevåg ret = tty_register_driver(tty); 204666b7793SArve Hjønnevåg if (ret) 205666b7793SArve Hjønnevåg goto err_tty_register_driver_failed; 206666b7793SArve Hjønnevåg 207666b7793SArve Hjønnevåg goldfish_tty_driver = tty; 208666b7793SArve Hjønnevåg return 0; 209666b7793SArve Hjønnevåg 210666b7793SArve Hjønnevåg err_tty_register_driver_failed: 211666b7793SArve Hjønnevåg put_tty_driver(tty); 212666b7793SArve Hjønnevåg err_alloc_tty_driver_failed: 213666b7793SArve Hjønnevåg kfree(goldfish_ttys); 214666b7793SArve Hjønnevåg goldfish_ttys = NULL; 215666b7793SArve Hjønnevåg err_alloc_goldfish_ttys_failed: 216666b7793SArve Hjønnevåg return ret; 217666b7793SArve Hjønnevåg } 218666b7793SArve Hjønnevåg 219666b7793SArve Hjønnevåg static void goldfish_tty_delete_driver(void) 220666b7793SArve Hjønnevåg { 221666b7793SArve Hjønnevåg tty_unregister_driver(goldfish_tty_driver); 222666b7793SArve Hjønnevåg put_tty_driver(goldfish_tty_driver); 223666b7793SArve Hjønnevåg goldfish_tty_driver = NULL; 224666b7793SArve Hjønnevåg kfree(goldfish_ttys); 225666b7793SArve Hjønnevåg goldfish_ttys = NULL; 226666b7793SArve Hjønnevåg } 227666b7793SArve Hjønnevåg 228666b7793SArve Hjønnevåg static int goldfish_tty_probe(struct platform_device *pdev) 229666b7793SArve Hjønnevåg { 230666b7793SArve Hjønnevåg struct goldfish_tty *qtty; 231666b7793SArve Hjønnevåg int ret = -EINVAL; 232666b7793SArve Hjønnevåg int i; 233666b7793SArve Hjønnevåg struct resource *r; 234666b7793SArve Hjønnevåg struct device *ttydev; 235666b7793SArve Hjønnevåg void __iomem *base; 236666b7793SArve Hjønnevåg u32 irq; 237666b7793SArve Hjønnevåg 238666b7793SArve Hjønnevåg r = platform_get_resource(pdev, IORESOURCE_MEM, 0); 239666b7793SArve Hjønnevåg if (r == NULL) 240666b7793SArve Hjønnevåg return -EINVAL; 241666b7793SArve Hjønnevåg 242666b7793SArve Hjønnevåg base = ioremap(r->start, 0x1000); 243666b7793SArve Hjønnevåg if (base == NULL) 244666b7793SArve Hjønnevåg pr_err("goldfish_tty: unable to remap base\n"); 245666b7793SArve Hjønnevåg 246666b7793SArve Hjønnevåg r = platform_get_resource(pdev, IORESOURCE_IRQ, 0); 247666b7793SArve Hjønnevåg if (r == NULL) 248666b7793SArve Hjønnevåg goto err_unmap; 249666b7793SArve Hjønnevåg 250666b7793SArve Hjønnevåg irq = r->start; 251666b7793SArve Hjønnevåg 252666b7793SArve Hjønnevåg if (pdev->id >= goldfish_tty_line_count) 253666b7793SArve Hjønnevåg goto err_unmap; 254666b7793SArve Hjønnevåg 255666b7793SArve Hjønnevåg mutex_lock(&goldfish_tty_lock); 256666b7793SArve Hjønnevåg if (goldfish_tty_current_line_count == 0) { 257666b7793SArve Hjønnevåg ret = goldfish_tty_create_driver(); 258666b7793SArve Hjønnevåg if (ret) 259666b7793SArve Hjønnevåg goto err_create_driver_failed; 260666b7793SArve Hjønnevåg } 261666b7793SArve Hjønnevåg goldfish_tty_current_line_count++; 262666b7793SArve Hjønnevåg 263666b7793SArve Hjønnevåg qtty = &goldfish_ttys[pdev->id]; 264666b7793SArve Hjønnevåg spin_lock_init(&qtty->lock); 265666b7793SArve Hjønnevåg tty_port_init(&qtty->port); 266666b7793SArve Hjønnevåg qtty->port.ops = &goldfish_port_ops; 267666b7793SArve Hjønnevåg qtty->base = base; 268666b7793SArve Hjønnevåg qtty->irq = irq; 269666b7793SArve Hjønnevåg 270666b7793SArve Hjønnevåg writel(GOLDFISH_TTY_CMD_INT_DISABLE, base + GOLDFISH_TTY_CMD); 271666b7793SArve Hjønnevåg 272d78055dcSAlan ret = request_irq(irq, goldfish_tty_interrupt, IRQF_SHARED, 273d78055dcSAlan "goldfish_tty", pdev); 274666b7793SArve Hjønnevåg if (ret) 275666b7793SArve Hjønnevåg goto err_request_irq_failed; 276666b7793SArve Hjønnevåg 277666b7793SArve Hjønnevåg 278666b7793SArve Hjønnevåg ttydev = tty_port_register_device(&qtty->port, goldfish_tty_driver, 279666b7793SArve Hjønnevåg pdev->id, &pdev->dev); 280666b7793SArve Hjønnevåg if (IS_ERR(ttydev)) { 281666b7793SArve Hjønnevåg ret = PTR_ERR(ttydev); 282666b7793SArve Hjønnevåg goto err_tty_register_device_failed; 283666b7793SArve Hjønnevåg } 284666b7793SArve Hjønnevåg 285666b7793SArve Hjønnevåg strcpy(qtty->console.name, "ttyGF"); 286666b7793SArve Hjønnevåg qtty->console.write = goldfish_tty_console_write; 287666b7793SArve Hjønnevåg qtty->console.device = goldfish_tty_console_device; 288666b7793SArve Hjønnevåg qtty->console.setup = goldfish_tty_console_setup; 289666b7793SArve Hjønnevåg qtty->console.flags = CON_PRINTBUFFER; 290666b7793SArve Hjønnevåg qtty->console.index = pdev->id; 291666b7793SArve Hjønnevåg register_console(&qtty->console); 292666b7793SArve Hjønnevåg 293666b7793SArve Hjønnevåg mutex_unlock(&goldfish_tty_lock); 294666b7793SArve Hjønnevåg return 0; 295666b7793SArve Hjønnevåg 296666b7793SArve Hjønnevåg tty_unregister_device(goldfish_tty_driver, i); 297666b7793SArve Hjønnevåg err_tty_register_device_failed: 298666b7793SArve Hjønnevåg free_irq(irq, pdev); 299666b7793SArve Hjønnevåg err_request_irq_failed: 300666b7793SArve Hjønnevåg goldfish_tty_current_line_count--; 301666b7793SArve Hjønnevåg if (goldfish_tty_current_line_count == 0) 302666b7793SArve Hjønnevåg goldfish_tty_delete_driver(); 303666b7793SArve Hjønnevåg err_create_driver_failed: 304666b7793SArve Hjønnevåg mutex_unlock(&goldfish_tty_lock); 305666b7793SArve Hjønnevåg err_unmap: 306666b7793SArve Hjønnevåg iounmap(base); 307666b7793SArve Hjønnevåg return ret; 308666b7793SArve Hjønnevåg } 309666b7793SArve Hjønnevåg 310666b7793SArve Hjønnevåg static int goldfish_tty_remove(struct platform_device *pdev) 311666b7793SArve Hjønnevåg { 312666b7793SArve Hjønnevåg struct goldfish_tty *qtty; 313666b7793SArve Hjønnevåg 314666b7793SArve Hjønnevåg mutex_lock(&goldfish_tty_lock); 315666b7793SArve Hjønnevåg 316666b7793SArve Hjønnevåg qtty = &goldfish_ttys[pdev->id]; 317666b7793SArve Hjønnevåg unregister_console(&qtty->console); 318666b7793SArve Hjønnevåg tty_unregister_device(goldfish_tty_driver, pdev->id); 319666b7793SArve Hjønnevåg iounmap(qtty->base); 320*a4dc9236SFabian Frederick qtty->base = NULL; 321666b7793SArve Hjønnevåg free_irq(qtty->irq, pdev); 322666b7793SArve Hjønnevåg goldfish_tty_current_line_count--; 323666b7793SArve Hjønnevåg if (goldfish_tty_current_line_count == 0) 324666b7793SArve Hjønnevåg goldfish_tty_delete_driver(); 325666b7793SArve Hjønnevåg mutex_unlock(&goldfish_tty_lock); 326666b7793SArve Hjønnevåg return 0; 327666b7793SArve Hjønnevåg } 328666b7793SArve Hjønnevåg 329666b7793SArve Hjønnevåg static struct platform_driver goldfish_tty_platform_driver = { 330666b7793SArve Hjønnevåg .probe = goldfish_tty_probe, 331666b7793SArve Hjønnevåg .remove = goldfish_tty_remove, 332666b7793SArve Hjønnevåg .driver = { 333666b7793SArve Hjønnevåg .name = "goldfish_tty" 334666b7793SArve Hjønnevåg } 335666b7793SArve Hjønnevåg }; 336666b7793SArve Hjønnevåg 337666b7793SArve Hjønnevåg module_platform_driver(goldfish_tty_platform_driver); 338666b7793SArve Hjønnevåg 339666b7793SArve Hjønnevåg MODULE_LICENSE("GPL v2"); 340