Arve Hjønnevåg | 666b779 | 2013-01-21 23:38:47 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2007 Google, Inc. |
| 3 | * Copyright (C) 2012 Intel, Inc. |
| 4 | * |
| 5 | * This software is licensed under the terms of the GNU General Public |
| 6 | * License version 2, as published by the Free Software Foundation, and |
| 7 | * may be copied, distributed, and modified under those terms. |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | */ |
| 15 | |
| 16 | #include <linux/console.h> |
Arve Hjønnevåg | 666b779 | 2013-01-21 23:38:47 +0000 | [diff] [blame] | 17 | #include <linux/interrupt.h> |
| 18 | #include <linux/platform_device.h> |
| 19 | #include <linux/tty.h> |
| 20 | #include <linux/tty_flip.h> |
| 21 | #include <linux/slab.h> |
| 22 | #include <linux/io.h> |
| 23 | #include <linux/module.h> |
Alan | e0f682e | 2014-05-12 16:57:05 +0100 | [diff] [blame^] | 24 | #include <linux/goldfish.h> |
Arve Hjønnevåg | 666b779 | 2013-01-21 23:38:47 +0000 | [diff] [blame] | 25 | |
| 26 | enum { |
| 27 | GOLDFISH_TTY_PUT_CHAR = 0x00, |
| 28 | GOLDFISH_TTY_BYTES_READY = 0x04, |
| 29 | GOLDFISH_TTY_CMD = 0x08, |
| 30 | |
| 31 | GOLDFISH_TTY_DATA_PTR = 0x10, |
| 32 | GOLDFISH_TTY_DATA_LEN = 0x14, |
Jun Tian | b8658bc | 2014-05-12 16:55:26 +0100 | [diff] [blame] | 33 | GOLDFISH_TTY_DATA_PTR_HIGH = 0x18, |
Arve Hjønnevåg | 666b779 | 2013-01-21 23:38:47 +0000 | [diff] [blame] | 34 | |
| 35 | GOLDFISH_TTY_CMD_INT_DISABLE = 0, |
| 36 | GOLDFISH_TTY_CMD_INT_ENABLE = 1, |
| 37 | GOLDFISH_TTY_CMD_WRITE_BUFFER = 2, |
| 38 | GOLDFISH_TTY_CMD_READ_BUFFER = 3, |
| 39 | }; |
| 40 | |
| 41 | struct goldfish_tty { |
| 42 | struct tty_port port; |
| 43 | spinlock_t lock; |
| 44 | void __iomem *base; |
| 45 | u32 irq; |
| 46 | int opencount; |
| 47 | struct console console; |
| 48 | }; |
| 49 | |
| 50 | static DEFINE_MUTEX(goldfish_tty_lock); |
| 51 | static struct tty_driver *goldfish_tty_driver; |
| 52 | static u32 goldfish_tty_line_count = 8; |
| 53 | static u32 goldfish_tty_current_line_count; |
| 54 | static struct goldfish_tty *goldfish_ttys; |
| 55 | |
| 56 | static void goldfish_tty_do_write(int line, const char *buf, unsigned count) |
| 57 | { |
| 58 | unsigned long irq_flags; |
| 59 | struct goldfish_tty *qtty = &goldfish_ttys[line]; |
| 60 | void __iomem *base = qtty->base; |
| 61 | spin_lock_irqsave(&qtty->lock, irq_flags); |
Alan | e0f682e | 2014-05-12 16:57:05 +0100 | [diff] [blame^] | 62 | gf_write64((u64)buf, base + GOLDFISH_TTY_DATA_PTR, |
| 63 | base + GOLDFISH_TTY_DATA_PTR_HIGH); |
Arve Hjønnevåg | 666b779 | 2013-01-21 23:38:47 +0000 | [diff] [blame] | 64 | writel(count, base + GOLDFISH_TTY_DATA_LEN); |
| 65 | writel(GOLDFISH_TTY_CMD_WRITE_BUFFER, base + GOLDFISH_TTY_CMD); |
| 66 | spin_unlock_irqrestore(&qtty->lock, irq_flags); |
| 67 | } |
| 68 | |
| 69 | static irqreturn_t goldfish_tty_interrupt(int irq, void *dev_id) |
| 70 | { |
| 71 | struct platform_device *pdev = dev_id; |
| 72 | struct goldfish_tty *qtty = &goldfish_ttys[pdev->id]; |
| 73 | void __iomem *base = qtty->base; |
| 74 | unsigned long irq_flags; |
| 75 | unsigned char *buf; |
| 76 | u32 count; |
Arve Hjønnevåg | 666b779 | 2013-01-21 23:38:47 +0000 | [diff] [blame] | 77 | |
| 78 | count = readl(base + GOLDFISH_TTY_BYTES_READY); |
| 79 | if(count == 0) |
| 80 | return IRQ_NONE; |
| 81 | |
Alan Cox | ebcf098 | 2013-01-25 15:05:30 +0000 | [diff] [blame] | 82 | count = tty_prepare_flip_string(&qtty->port, &buf, count); |
| 83 | spin_lock_irqsave(&qtty->lock, irq_flags); |
Alan | e0f682e | 2014-05-12 16:57:05 +0100 | [diff] [blame^] | 84 | gf_write64((u64)buf, base + GOLDFISH_TTY_DATA_PTR, |
| 85 | base + GOLDFISH_TTY_DATA_PTR_HIGH); |
Alan Cox | ebcf098 | 2013-01-25 15:05:30 +0000 | [diff] [blame] | 86 | writel(count, base + GOLDFISH_TTY_DATA_LEN); |
| 87 | writel(GOLDFISH_TTY_CMD_READ_BUFFER, base + GOLDFISH_TTY_CMD); |
| 88 | spin_unlock_irqrestore(&qtty->lock, irq_flags); |
| 89 | tty_schedule_flip(&qtty->port); |
Arve Hjønnevåg | 666b779 | 2013-01-21 23:38:47 +0000 | [diff] [blame] | 90 | return IRQ_HANDLED; |
| 91 | } |
| 92 | |
| 93 | static int goldfish_tty_activate(struct tty_port *port, struct tty_struct *tty) |
| 94 | { |
| 95 | struct goldfish_tty *qtty = container_of(port, struct goldfish_tty, port); |
| 96 | writel(GOLDFISH_TTY_CMD_INT_ENABLE, qtty->base + GOLDFISH_TTY_CMD); |
| 97 | return 0; |
| 98 | } |
| 99 | |
| 100 | static void goldfish_tty_shutdown(struct tty_port *port) |
| 101 | { |
| 102 | struct goldfish_tty *qtty = container_of(port, struct goldfish_tty, port); |
| 103 | writel(GOLDFISH_TTY_CMD_INT_DISABLE, qtty->base + GOLDFISH_TTY_CMD); |
| 104 | } |
| 105 | |
| 106 | static int goldfish_tty_open(struct tty_struct * tty, struct file * filp) |
| 107 | { |
| 108 | struct goldfish_tty *qtty = &goldfish_ttys[tty->index]; |
| 109 | return tty_port_open(&qtty->port, tty, filp); |
| 110 | } |
| 111 | |
| 112 | static void goldfish_tty_close(struct tty_struct * tty, struct file * filp) |
| 113 | { |
| 114 | tty_port_close(tty->port, tty, filp); |
| 115 | } |
| 116 | |
| 117 | static void goldfish_tty_hangup(struct tty_struct *tty) |
| 118 | { |
| 119 | tty_port_hangup(tty->port); |
| 120 | } |
| 121 | |
| 122 | static int goldfish_tty_write(struct tty_struct * tty, const unsigned char *buf, int count) |
| 123 | { |
| 124 | goldfish_tty_do_write(tty->index, buf, count); |
| 125 | return count; |
| 126 | } |
| 127 | |
| 128 | static int goldfish_tty_write_room(struct tty_struct *tty) |
| 129 | { |
| 130 | return 0x10000; |
| 131 | } |
| 132 | |
| 133 | static int goldfish_tty_chars_in_buffer(struct tty_struct *tty) |
| 134 | { |
| 135 | struct goldfish_tty *qtty = &goldfish_ttys[tty->index]; |
| 136 | void __iomem *base = qtty->base; |
| 137 | return readl(base + GOLDFISH_TTY_BYTES_READY); |
| 138 | } |
| 139 | |
| 140 | static void goldfish_tty_console_write(struct console *co, const char *b, unsigned count) |
| 141 | { |
| 142 | goldfish_tty_do_write(co->index, b, count); |
| 143 | } |
| 144 | |
| 145 | static struct tty_driver *goldfish_tty_console_device(struct console *c, int *index) |
| 146 | { |
| 147 | *index = c->index; |
| 148 | return goldfish_tty_driver; |
| 149 | } |
| 150 | |
| 151 | static int goldfish_tty_console_setup(struct console *co, char *options) |
| 152 | { |
| 153 | if((unsigned)co->index > goldfish_tty_line_count) |
| 154 | return -ENODEV; |
| 155 | if(goldfish_ttys[co->index].base == 0) |
| 156 | return -ENODEV; |
| 157 | return 0; |
| 158 | } |
| 159 | |
| 160 | static struct tty_port_operations goldfish_port_ops = { |
| 161 | .activate = goldfish_tty_activate, |
| 162 | .shutdown = goldfish_tty_shutdown |
| 163 | }; |
| 164 | |
| 165 | static struct tty_operations goldfish_tty_ops = { |
| 166 | .open = goldfish_tty_open, |
| 167 | .close = goldfish_tty_close, |
| 168 | .hangup = goldfish_tty_hangup, |
| 169 | .write = goldfish_tty_write, |
| 170 | .write_room = goldfish_tty_write_room, |
| 171 | .chars_in_buffer = goldfish_tty_chars_in_buffer, |
| 172 | }; |
| 173 | |
| 174 | static int goldfish_tty_create_driver(void) |
| 175 | { |
| 176 | int ret; |
| 177 | struct tty_driver *tty; |
| 178 | |
| 179 | goldfish_ttys = kzalloc(sizeof(*goldfish_ttys) * goldfish_tty_line_count, GFP_KERNEL); |
| 180 | if(goldfish_ttys == NULL) { |
| 181 | ret = -ENOMEM; |
| 182 | goto err_alloc_goldfish_ttys_failed; |
| 183 | } |
| 184 | tty = alloc_tty_driver(goldfish_tty_line_count); |
| 185 | if(tty == NULL) { |
| 186 | ret = -ENOMEM; |
| 187 | goto err_alloc_tty_driver_failed; |
| 188 | } |
| 189 | tty->driver_name = "goldfish"; |
| 190 | tty->name = "ttyGF"; |
| 191 | tty->type = TTY_DRIVER_TYPE_SERIAL; |
| 192 | tty->subtype = SERIAL_TYPE_NORMAL; |
| 193 | tty->init_termios = tty_std_termios; |
| 194 | tty->flags = TTY_DRIVER_RESET_TERMIOS | TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV; |
| 195 | tty_set_operations(tty, &goldfish_tty_ops); |
| 196 | ret = tty_register_driver(tty); |
| 197 | if(ret) |
| 198 | goto err_tty_register_driver_failed; |
| 199 | |
| 200 | goldfish_tty_driver = tty; |
| 201 | return 0; |
| 202 | |
| 203 | err_tty_register_driver_failed: |
| 204 | put_tty_driver(tty); |
| 205 | err_alloc_tty_driver_failed: |
| 206 | kfree(goldfish_ttys); |
| 207 | goldfish_ttys = NULL; |
| 208 | err_alloc_goldfish_ttys_failed: |
| 209 | return ret; |
| 210 | } |
| 211 | |
| 212 | static void goldfish_tty_delete_driver(void) |
| 213 | { |
| 214 | tty_unregister_driver(goldfish_tty_driver); |
| 215 | put_tty_driver(goldfish_tty_driver); |
| 216 | goldfish_tty_driver = NULL; |
| 217 | kfree(goldfish_ttys); |
| 218 | goldfish_ttys = NULL; |
| 219 | } |
| 220 | |
| 221 | static int goldfish_tty_probe(struct platform_device *pdev) |
| 222 | { |
| 223 | struct goldfish_tty *qtty; |
| 224 | int ret = -EINVAL; |
| 225 | int i; |
| 226 | struct resource *r; |
| 227 | struct device *ttydev; |
| 228 | void __iomem *base; |
| 229 | u32 irq; |
| 230 | |
| 231 | r = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 232 | if(r == NULL) |
| 233 | return -EINVAL; |
| 234 | |
| 235 | base = ioremap(r->start, 0x1000); |
| 236 | if (base == NULL) |
| 237 | pr_err("goldfish_tty: unable to remap base\n"); |
| 238 | |
| 239 | r = platform_get_resource(pdev, IORESOURCE_IRQ, 0); |
| 240 | if(r == NULL) |
| 241 | goto err_unmap; |
| 242 | |
| 243 | irq = r->start; |
| 244 | |
| 245 | if(pdev->id >= goldfish_tty_line_count) |
| 246 | goto err_unmap; |
| 247 | |
| 248 | mutex_lock(&goldfish_tty_lock); |
| 249 | if(goldfish_tty_current_line_count == 0) { |
| 250 | ret = goldfish_tty_create_driver(); |
| 251 | if(ret) |
| 252 | goto err_create_driver_failed; |
| 253 | } |
| 254 | goldfish_tty_current_line_count++; |
| 255 | |
| 256 | qtty = &goldfish_ttys[pdev->id]; |
| 257 | spin_lock_init(&qtty->lock); |
| 258 | tty_port_init(&qtty->port); |
| 259 | qtty->port.ops = &goldfish_port_ops; |
| 260 | qtty->base = base; |
| 261 | qtty->irq = irq; |
| 262 | |
| 263 | writel(GOLDFISH_TTY_CMD_INT_DISABLE, base + GOLDFISH_TTY_CMD); |
| 264 | |
| 265 | ret = request_irq(irq, goldfish_tty_interrupt, IRQF_SHARED, "goldfish_tty", pdev); |
| 266 | if(ret) |
| 267 | goto err_request_irq_failed; |
| 268 | |
| 269 | |
| 270 | ttydev = tty_port_register_device(&qtty->port, goldfish_tty_driver, |
| 271 | pdev->id, &pdev->dev); |
| 272 | if(IS_ERR(ttydev)) { |
| 273 | ret = PTR_ERR(ttydev); |
| 274 | goto err_tty_register_device_failed; |
| 275 | } |
| 276 | |
| 277 | strcpy(qtty->console.name, "ttyGF"); |
| 278 | qtty->console.write = goldfish_tty_console_write; |
| 279 | qtty->console.device = goldfish_tty_console_device; |
| 280 | qtty->console.setup = goldfish_tty_console_setup; |
| 281 | qtty->console.flags = CON_PRINTBUFFER; |
| 282 | qtty->console.index = pdev->id; |
| 283 | register_console(&qtty->console); |
| 284 | |
| 285 | mutex_unlock(&goldfish_tty_lock); |
| 286 | return 0; |
| 287 | |
| 288 | tty_unregister_device(goldfish_tty_driver, i); |
| 289 | err_tty_register_device_failed: |
| 290 | free_irq(irq, pdev); |
| 291 | err_request_irq_failed: |
| 292 | goldfish_tty_current_line_count--; |
| 293 | if(goldfish_tty_current_line_count == 0) |
| 294 | goldfish_tty_delete_driver(); |
| 295 | err_create_driver_failed: |
| 296 | mutex_unlock(&goldfish_tty_lock); |
| 297 | err_unmap: |
| 298 | iounmap(base); |
| 299 | return ret; |
| 300 | } |
| 301 | |
| 302 | static int goldfish_tty_remove(struct platform_device *pdev) |
| 303 | { |
| 304 | struct goldfish_tty *qtty; |
| 305 | |
| 306 | mutex_lock(&goldfish_tty_lock); |
| 307 | |
| 308 | qtty = &goldfish_ttys[pdev->id]; |
| 309 | unregister_console(&qtty->console); |
| 310 | tty_unregister_device(goldfish_tty_driver, pdev->id); |
| 311 | iounmap(qtty->base); |
| 312 | qtty->base = 0; |
| 313 | free_irq(qtty->irq, pdev); |
| 314 | goldfish_tty_current_line_count--; |
| 315 | if(goldfish_tty_current_line_count == 0) |
| 316 | goldfish_tty_delete_driver(); |
| 317 | mutex_unlock(&goldfish_tty_lock); |
| 318 | return 0; |
| 319 | } |
| 320 | |
| 321 | static struct platform_driver goldfish_tty_platform_driver = { |
| 322 | .probe = goldfish_tty_probe, |
| 323 | .remove = goldfish_tty_remove, |
| 324 | .driver = { |
| 325 | .name = "goldfish_tty" |
| 326 | } |
| 327 | }; |
| 328 | |
| 329 | module_platform_driver(goldfish_tty_platform_driver); |
| 330 | |
| 331 | MODULE_LICENSE("GPL v2"); |