Greg Kroah-Hartman | e3b3d0f | 2017-11-06 18:11:51 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Arve Hjønnevåg | 666b779 | 2013-01-21 23:38:47 +0000 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (C) 2007 Google, Inc. |
| 4 | * Copyright (C) 2012 Intel, Inc. |
Miodrag Dinic | 3840ed9 | 2017-08-29 15:53:20 +0200 | [diff] [blame] | 5 | * Copyright (C) 2017 Imagination Technologies Ltd. |
Arve Hjønnevåg | 666b779 | 2013-01-21 23:38:47 +0000 | [diff] [blame] | 6 | */ |
| 7 | |
| 8 | #include <linux/console.h> |
Arve Hjønnevåg | 666b779 | 2013-01-21 23:38:47 +0000 | [diff] [blame] | 9 | #include <linux/interrupt.h> |
| 10 | #include <linux/platform_device.h> |
| 11 | #include <linux/tty.h> |
| 12 | #include <linux/tty_flip.h> |
| 13 | #include <linux/slab.h> |
| 14 | #include <linux/io.h> |
| 15 | #include <linux/module.h> |
Randy Dunlap | ac31672 | 2018-06-19 22:47:28 -0700 | [diff] [blame] | 16 | #include <linux/mod_devicetable.h> |
Alan | e0f682e | 2014-05-12 16:57:05 +0100 | [diff] [blame] | 17 | #include <linux/goldfish.h> |
Miodrag Dinic | 7157d2b | 2017-08-29 15:53:19 +0200 | [diff] [blame] | 18 | #include <linux/mm.h> |
| 19 | #include <linux/dma-mapping.h> |
Miodrag Dinic | 3840ed9 | 2017-08-29 15:53:20 +0200 | [diff] [blame] | 20 | #include <linux/serial_core.h> |
Arve Hjønnevåg | 666b779 | 2013-01-21 23:38:47 +0000 | [diff] [blame] | 21 | |
Aleksandar Markovic | 2296eee | 2017-08-29 15:53:18 +0200 | [diff] [blame] | 22 | /* Goldfish tty register's offsets */ |
| 23 | #define GOLDFISH_TTY_REG_BYTES_READY 0x04 |
| 24 | #define GOLDFISH_TTY_REG_CMD 0x08 |
| 25 | #define GOLDFISH_TTY_REG_DATA_PTR 0x10 |
| 26 | #define GOLDFISH_TTY_REG_DATA_LEN 0x14 |
| 27 | #define GOLDFISH_TTY_REG_DATA_PTR_HIGH 0x18 |
Miodrag Dinic | 7157d2b | 2017-08-29 15:53:19 +0200 | [diff] [blame] | 28 | #define GOLDFISH_TTY_REG_VERSION 0x20 |
Arve Hjønnevåg | 666b779 | 2013-01-21 23:38:47 +0000 | [diff] [blame] | 29 | |
Aleksandar Markovic | 2296eee | 2017-08-29 15:53:18 +0200 | [diff] [blame] | 30 | /* Goldfish tty commands */ |
| 31 | #define GOLDFISH_TTY_CMD_INT_DISABLE 0 |
| 32 | #define GOLDFISH_TTY_CMD_INT_ENABLE 1 |
| 33 | #define GOLDFISH_TTY_CMD_WRITE_BUFFER 2 |
| 34 | #define GOLDFISH_TTY_CMD_READ_BUFFER 3 |
Arve Hjønnevåg | 666b779 | 2013-01-21 23:38:47 +0000 | [diff] [blame] | 35 | |
| 36 | struct goldfish_tty { |
| 37 | struct tty_port port; |
| 38 | spinlock_t lock; |
| 39 | void __iomem *base; |
| 40 | u32 irq; |
| 41 | int opencount; |
| 42 | struct console console; |
Miodrag Dinic | 7157d2b | 2017-08-29 15:53:19 +0200 | [diff] [blame] | 43 | u32 version; |
| 44 | struct device *dev; |
Arve Hjønnevåg | 666b779 | 2013-01-21 23:38:47 +0000 | [diff] [blame] | 45 | }; |
| 46 | |
| 47 | static DEFINE_MUTEX(goldfish_tty_lock); |
| 48 | static struct tty_driver *goldfish_tty_driver; |
| 49 | static u32 goldfish_tty_line_count = 8; |
| 50 | static u32 goldfish_tty_current_line_count; |
| 51 | static struct goldfish_tty *goldfish_ttys; |
| 52 | |
Miodrag Dinic | 7157d2b | 2017-08-29 15:53:19 +0200 | [diff] [blame] | 53 | static void do_rw_io(struct goldfish_tty *qtty, |
| 54 | unsigned long address, |
| 55 | unsigned int count, |
| 56 | int is_write) |
Arve Hjønnevåg | 666b779 | 2013-01-21 23:38:47 +0000 | [diff] [blame] | 57 | { |
| 58 | unsigned long irq_flags; |
Arve Hjønnevåg | 666b779 | 2013-01-21 23:38:47 +0000 | [diff] [blame] | 59 | void __iomem *base = qtty->base; |
Miodrag Dinic | 7157d2b | 2017-08-29 15:53:19 +0200 | [diff] [blame] | 60 | |
Arve Hjønnevåg | 666b779 | 2013-01-21 23:38:47 +0000 | [diff] [blame] | 61 | spin_lock_irqsave(&qtty->lock, irq_flags); |
Miodrag Dinic | 7157d2b | 2017-08-29 15:53:19 +0200 | [diff] [blame] | 62 | gf_write_ptr((void *)address, base + GOLDFISH_TTY_REG_DATA_PTR, |
Aleksandar Markovic | 2296eee | 2017-08-29 15:53:18 +0200 | [diff] [blame] | 63 | base + GOLDFISH_TTY_REG_DATA_PTR_HIGH); |
| 64 | writel(count, base + GOLDFISH_TTY_REG_DATA_LEN); |
Miodrag Dinic | 7157d2b | 2017-08-29 15:53:19 +0200 | [diff] [blame] | 65 | |
| 66 | if (is_write) |
| 67 | writel(GOLDFISH_TTY_CMD_WRITE_BUFFER, |
| 68 | base + GOLDFISH_TTY_REG_CMD); |
| 69 | else |
| 70 | writel(GOLDFISH_TTY_CMD_READ_BUFFER, |
| 71 | base + GOLDFISH_TTY_REG_CMD); |
| 72 | |
Arve Hjønnevåg | 666b779 | 2013-01-21 23:38:47 +0000 | [diff] [blame] | 73 | spin_unlock_irqrestore(&qtty->lock, irq_flags); |
| 74 | } |
| 75 | |
Miodrag Dinic | 7157d2b | 2017-08-29 15:53:19 +0200 | [diff] [blame] | 76 | static void goldfish_tty_rw(struct goldfish_tty *qtty, |
| 77 | unsigned long addr, |
| 78 | unsigned int count, |
| 79 | int is_write) |
| 80 | { |
| 81 | dma_addr_t dma_handle; |
| 82 | enum dma_data_direction dma_dir; |
| 83 | |
| 84 | dma_dir = (is_write ? DMA_TO_DEVICE : DMA_FROM_DEVICE); |
| 85 | if (qtty->version > 0) { |
| 86 | /* |
| 87 | * Goldfish TTY for Ranchu platform uses |
| 88 | * physical addresses and DMA for read/write operations |
| 89 | */ |
| 90 | unsigned long addr_end = addr + count; |
| 91 | |
| 92 | while (addr < addr_end) { |
| 93 | unsigned long pg_end = (addr & PAGE_MASK) + PAGE_SIZE; |
| 94 | unsigned long next = |
| 95 | pg_end < addr_end ? pg_end : addr_end; |
| 96 | unsigned long avail = next - addr; |
| 97 | |
| 98 | /* |
| 99 | * Map the buffer's virtual address to the DMA address |
| 100 | * so the buffer can be accessed by the device. |
| 101 | */ |
| 102 | dma_handle = dma_map_single(qtty->dev, (void *)addr, |
| 103 | avail, dma_dir); |
| 104 | |
| 105 | if (dma_mapping_error(qtty->dev, dma_handle)) { |
| 106 | dev_err(qtty->dev, "tty: DMA mapping error.\n"); |
| 107 | return; |
| 108 | } |
| 109 | do_rw_io(qtty, dma_handle, avail, is_write); |
| 110 | |
| 111 | /* |
| 112 | * Unmap the previously mapped region after |
| 113 | * the completion of the read/write operation. |
| 114 | */ |
| 115 | dma_unmap_single(qtty->dev, dma_handle, avail, dma_dir); |
| 116 | |
| 117 | addr += avail; |
| 118 | } |
| 119 | } else { |
| 120 | /* |
| 121 | * Old style Goldfish TTY used on the Goldfish platform |
| 122 | * uses virtual addresses. |
| 123 | */ |
| 124 | do_rw_io(qtty, addr, count, is_write); |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | static void goldfish_tty_do_write(int line, const char *buf, |
| 129 | unsigned int count) |
| 130 | { |
| 131 | struct goldfish_tty *qtty = &goldfish_ttys[line]; |
| 132 | unsigned long address = (unsigned long)(void *)buf; |
| 133 | |
| 134 | goldfish_tty_rw(qtty, address, count, 1); |
| 135 | } |
| 136 | |
Arve Hjønnevåg | 666b779 | 2013-01-21 23:38:47 +0000 | [diff] [blame] | 137 | static irqreturn_t goldfish_tty_interrupt(int irq, void *dev_id) |
| 138 | { |
Greg Hackmann | 465893e | 2016-02-26 19:01:05 +0000 | [diff] [blame] | 139 | struct goldfish_tty *qtty = dev_id; |
Arve Hjønnevåg | 666b779 | 2013-01-21 23:38:47 +0000 | [diff] [blame] | 140 | void __iomem *base = qtty->base; |
Miodrag Dinic | 7157d2b | 2017-08-29 15:53:19 +0200 | [diff] [blame] | 141 | unsigned long address; |
Arve Hjønnevåg | 666b779 | 2013-01-21 23:38:47 +0000 | [diff] [blame] | 142 | unsigned char *buf; |
| 143 | u32 count; |
Arve Hjønnevåg | 666b779 | 2013-01-21 23:38:47 +0000 | [diff] [blame] | 144 | |
Aleksandar Markovic | 2296eee | 2017-08-29 15:53:18 +0200 | [diff] [blame] | 145 | count = readl(base + GOLDFISH_TTY_REG_BYTES_READY); |
Alan | d78055d | 2014-05-12 16:57:14 +0100 | [diff] [blame] | 146 | if (count == 0) |
Arve Hjønnevåg | 666b779 | 2013-01-21 23:38:47 +0000 | [diff] [blame] | 147 | return IRQ_NONE; |
| 148 | |
Alan Cox | ebcf098 | 2013-01-25 15:05:30 +0000 | [diff] [blame] | 149 | count = tty_prepare_flip_string(&qtty->port, &buf, count); |
Miodrag Dinic | 7157d2b | 2017-08-29 15:53:19 +0200 | [diff] [blame] | 150 | |
| 151 | address = (unsigned long)(void *)buf; |
| 152 | goldfish_tty_rw(qtty, address, count, 0); |
| 153 | |
Alan Cox | ebcf098 | 2013-01-25 15:05:30 +0000 | [diff] [blame] | 154 | tty_schedule_flip(&qtty->port); |
Arve Hjønnevåg | 666b779 | 2013-01-21 23:38:47 +0000 | [diff] [blame] | 155 | return IRQ_HANDLED; |
| 156 | } |
| 157 | |
| 158 | static int goldfish_tty_activate(struct tty_port *port, struct tty_struct *tty) |
| 159 | { |
Alan | d78055d | 2014-05-12 16:57:14 +0100 | [diff] [blame] | 160 | struct goldfish_tty *qtty = container_of(port, struct goldfish_tty, |
| 161 | port); |
Aleksandar Markovic | 2296eee | 2017-08-29 15:53:18 +0200 | [diff] [blame] | 162 | writel(GOLDFISH_TTY_CMD_INT_ENABLE, qtty->base + GOLDFISH_TTY_REG_CMD); |
Arve Hjønnevåg | 666b779 | 2013-01-21 23:38:47 +0000 | [diff] [blame] | 163 | return 0; |
| 164 | } |
| 165 | |
| 166 | static void goldfish_tty_shutdown(struct tty_port *port) |
| 167 | { |
Alan | d78055d | 2014-05-12 16:57:14 +0100 | [diff] [blame] | 168 | struct goldfish_tty *qtty = container_of(port, struct goldfish_tty, |
| 169 | port); |
Aleksandar Markovic | 2296eee | 2017-08-29 15:53:18 +0200 | [diff] [blame] | 170 | writel(GOLDFISH_TTY_CMD_INT_DISABLE, qtty->base + GOLDFISH_TTY_REG_CMD); |
Arve Hjønnevåg | 666b779 | 2013-01-21 23:38:47 +0000 | [diff] [blame] | 171 | } |
| 172 | |
Alan | d78055d | 2014-05-12 16:57:14 +0100 | [diff] [blame] | 173 | static int goldfish_tty_open(struct tty_struct *tty, struct file *filp) |
Arve Hjønnevåg | 666b779 | 2013-01-21 23:38:47 +0000 | [diff] [blame] | 174 | { |
| 175 | struct goldfish_tty *qtty = &goldfish_ttys[tty->index]; |
| 176 | return tty_port_open(&qtty->port, tty, filp); |
| 177 | } |
| 178 | |
Alan | d78055d | 2014-05-12 16:57:14 +0100 | [diff] [blame] | 179 | static void goldfish_tty_close(struct tty_struct *tty, struct file *filp) |
Arve Hjønnevåg | 666b779 | 2013-01-21 23:38:47 +0000 | [diff] [blame] | 180 | { |
| 181 | tty_port_close(tty->port, tty, filp); |
| 182 | } |
| 183 | |
| 184 | static void goldfish_tty_hangup(struct tty_struct *tty) |
| 185 | { |
| 186 | tty_port_hangup(tty->port); |
| 187 | } |
| 188 | |
Alan | d78055d | 2014-05-12 16:57:14 +0100 | [diff] [blame] | 189 | static int goldfish_tty_write(struct tty_struct *tty, const unsigned char *buf, |
| 190 | int count) |
Arve Hjønnevåg | 666b779 | 2013-01-21 23:38:47 +0000 | [diff] [blame] | 191 | { |
| 192 | goldfish_tty_do_write(tty->index, buf, count); |
| 193 | return count; |
| 194 | } |
| 195 | |
| 196 | static int goldfish_tty_write_room(struct tty_struct *tty) |
| 197 | { |
| 198 | return 0x10000; |
| 199 | } |
| 200 | |
| 201 | static int goldfish_tty_chars_in_buffer(struct tty_struct *tty) |
| 202 | { |
| 203 | struct goldfish_tty *qtty = &goldfish_ttys[tty->index]; |
| 204 | void __iomem *base = qtty->base; |
Aleksandar Markovic | 2296eee | 2017-08-29 15:53:18 +0200 | [diff] [blame] | 205 | return readl(base + GOLDFISH_TTY_REG_BYTES_READY); |
Arve Hjønnevåg | 666b779 | 2013-01-21 23:38:47 +0000 | [diff] [blame] | 206 | } |
| 207 | |
Alan | d78055d | 2014-05-12 16:57:14 +0100 | [diff] [blame] | 208 | static void goldfish_tty_console_write(struct console *co, const char *b, |
| 209 | unsigned count) |
Arve Hjønnevåg | 666b779 | 2013-01-21 23:38:47 +0000 | [diff] [blame] | 210 | { |
| 211 | goldfish_tty_do_write(co->index, b, count); |
| 212 | } |
| 213 | |
Alan | d78055d | 2014-05-12 16:57:14 +0100 | [diff] [blame] | 214 | static struct tty_driver *goldfish_tty_console_device(struct console *c, |
| 215 | int *index) |
Arve Hjønnevåg | 666b779 | 2013-01-21 23:38:47 +0000 | [diff] [blame] | 216 | { |
| 217 | *index = c->index; |
| 218 | return goldfish_tty_driver; |
| 219 | } |
| 220 | |
| 221 | static int goldfish_tty_console_setup(struct console *co, char *options) |
| 222 | { |
Dan Carpenter | fda2b41 | 2014-10-29 11:43:25 +0300 | [diff] [blame] | 223 | if ((unsigned)co->index >= goldfish_tty_line_count) |
Arve Hjønnevåg | 666b779 | 2013-01-21 23:38:47 +0000 | [diff] [blame] | 224 | return -ENODEV; |
Fabian Frederick | a4dc923 | 2014-09-28 20:10:17 +0200 | [diff] [blame] | 225 | if (!goldfish_ttys[co->index].base) |
Arve Hjønnevåg | 666b779 | 2013-01-21 23:38:47 +0000 | [diff] [blame] | 226 | return -ENODEV; |
| 227 | return 0; |
| 228 | } |
| 229 | |
Aya Mahfouz | 04b757d | 2015-12-15 01:53:36 +0200 | [diff] [blame] | 230 | static const struct tty_port_operations goldfish_port_ops = { |
Arve Hjønnevåg | 666b779 | 2013-01-21 23:38:47 +0000 | [diff] [blame] | 231 | .activate = goldfish_tty_activate, |
| 232 | .shutdown = goldfish_tty_shutdown |
| 233 | }; |
| 234 | |
Alan | d78055d | 2014-05-12 16:57:14 +0100 | [diff] [blame] | 235 | static const struct tty_operations goldfish_tty_ops = { |
Arve Hjønnevåg | 666b779 | 2013-01-21 23:38:47 +0000 | [diff] [blame] | 236 | .open = goldfish_tty_open, |
| 237 | .close = goldfish_tty_close, |
| 238 | .hangup = goldfish_tty_hangup, |
| 239 | .write = goldfish_tty_write, |
| 240 | .write_room = goldfish_tty_write_room, |
| 241 | .chars_in_buffer = goldfish_tty_chars_in_buffer, |
| 242 | }; |
| 243 | |
| 244 | static int goldfish_tty_create_driver(void) |
| 245 | { |
| 246 | int ret; |
| 247 | struct tty_driver *tty; |
| 248 | |
Kees Cook | 6396bb2 | 2018-06-12 14:03:40 -0700 | [diff] [blame] | 249 | goldfish_ttys = kcalloc(goldfish_tty_line_count, |
| 250 | sizeof(*goldfish_ttys), |
| 251 | GFP_KERNEL); |
Alan | d78055d | 2014-05-12 16:57:14 +0100 | [diff] [blame] | 252 | if (goldfish_ttys == NULL) { |
Arve Hjønnevåg | 666b779 | 2013-01-21 23:38:47 +0000 | [diff] [blame] | 253 | ret = -ENOMEM; |
| 254 | goto err_alloc_goldfish_ttys_failed; |
| 255 | } |
| 256 | tty = alloc_tty_driver(goldfish_tty_line_count); |
Alan | d78055d | 2014-05-12 16:57:14 +0100 | [diff] [blame] | 257 | if (tty == NULL) { |
Arve Hjønnevåg | 666b779 | 2013-01-21 23:38:47 +0000 | [diff] [blame] | 258 | ret = -ENOMEM; |
| 259 | goto err_alloc_tty_driver_failed; |
| 260 | } |
| 261 | tty->driver_name = "goldfish"; |
| 262 | tty->name = "ttyGF"; |
| 263 | tty->type = TTY_DRIVER_TYPE_SERIAL; |
| 264 | tty->subtype = SERIAL_TYPE_NORMAL; |
| 265 | tty->init_termios = tty_std_termios; |
Alan | d78055d | 2014-05-12 16:57:14 +0100 | [diff] [blame] | 266 | tty->flags = TTY_DRIVER_RESET_TERMIOS | TTY_DRIVER_REAL_RAW | |
| 267 | TTY_DRIVER_DYNAMIC_DEV; |
Arve Hjønnevåg | 666b779 | 2013-01-21 23:38:47 +0000 | [diff] [blame] | 268 | tty_set_operations(tty, &goldfish_tty_ops); |
| 269 | ret = tty_register_driver(tty); |
Alan | d78055d | 2014-05-12 16:57:14 +0100 | [diff] [blame] | 270 | if (ret) |
Arve Hjønnevåg | 666b779 | 2013-01-21 23:38:47 +0000 | [diff] [blame] | 271 | goto err_tty_register_driver_failed; |
| 272 | |
| 273 | goldfish_tty_driver = tty; |
| 274 | return 0; |
| 275 | |
| 276 | err_tty_register_driver_failed: |
| 277 | put_tty_driver(tty); |
| 278 | err_alloc_tty_driver_failed: |
| 279 | kfree(goldfish_ttys); |
| 280 | goldfish_ttys = NULL; |
| 281 | err_alloc_goldfish_ttys_failed: |
| 282 | return ret; |
| 283 | } |
| 284 | |
| 285 | static void goldfish_tty_delete_driver(void) |
| 286 | { |
| 287 | tty_unregister_driver(goldfish_tty_driver); |
| 288 | put_tty_driver(goldfish_tty_driver); |
| 289 | goldfish_tty_driver = NULL; |
| 290 | kfree(goldfish_ttys); |
| 291 | goldfish_ttys = NULL; |
| 292 | } |
| 293 | |
| 294 | static int goldfish_tty_probe(struct platform_device *pdev) |
| 295 | { |
| 296 | struct goldfish_tty *qtty; |
Miodrag Dinic | 7157d2b | 2017-08-29 15:53:19 +0200 | [diff] [blame] | 297 | int ret = -ENODEV; |
Arve Hjønnevåg | 666b779 | 2013-01-21 23:38:47 +0000 | [diff] [blame] | 298 | struct resource *r; |
| 299 | struct device *ttydev; |
| 300 | void __iomem *base; |
| 301 | u32 irq; |
Greg Hackmann | 465893e | 2016-02-26 19:01:05 +0000 | [diff] [blame] | 302 | unsigned int line; |
Arve Hjønnevåg | 666b779 | 2013-01-21 23:38:47 +0000 | [diff] [blame] | 303 | |
| 304 | r = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
Miodrag Dinic | 7157d2b | 2017-08-29 15:53:19 +0200 | [diff] [blame] | 305 | if (!r) { |
| 306 | pr_err("goldfish_tty: No MEM resource available!\n"); |
| 307 | return -ENOMEM; |
| 308 | } |
Arve Hjønnevåg | 666b779 | 2013-01-21 23:38:47 +0000 | [diff] [blame] | 309 | |
| 310 | base = ioremap(r->start, 0x1000); |
Miodrag Dinic | 7157d2b | 2017-08-29 15:53:19 +0200 | [diff] [blame] | 311 | if (!base) { |
| 312 | pr_err("goldfish_tty: Unable to ioremap base!\n"); |
| 313 | return -ENOMEM; |
| 314 | } |
Arve Hjønnevåg | 666b779 | 2013-01-21 23:38:47 +0000 | [diff] [blame] | 315 | |
| 316 | r = platform_get_resource(pdev, IORESOURCE_IRQ, 0); |
Miodrag Dinic | 7157d2b | 2017-08-29 15:53:19 +0200 | [diff] [blame] | 317 | if (!r) { |
| 318 | pr_err("goldfish_tty: No IRQ resource available!\n"); |
Arve Hjønnevåg | 666b779 | 2013-01-21 23:38:47 +0000 | [diff] [blame] | 319 | goto err_unmap; |
Miodrag Dinic | 7157d2b | 2017-08-29 15:53:19 +0200 | [diff] [blame] | 320 | } |
Arve Hjønnevåg | 666b779 | 2013-01-21 23:38:47 +0000 | [diff] [blame] | 321 | |
| 322 | irq = r->start; |
| 323 | |
Arve Hjønnevåg | 666b779 | 2013-01-21 23:38:47 +0000 | [diff] [blame] | 324 | mutex_lock(&goldfish_tty_lock); |
Greg Hackmann | 465893e | 2016-02-26 19:01:05 +0000 | [diff] [blame] | 325 | |
| 326 | if (pdev->id == PLATFORM_DEVID_NONE) |
| 327 | line = goldfish_tty_current_line_count; |
| 328 | else |
| 329 | line = pdev->id; |
| 330 | |
Miodrag Dinic | 7157d2b | 2017-08-29 15:53:19 +0200 | [diff] [blame] | 331 | if (line >= goldfish_tty_line_count) { |
| 332 | pr_err("goldfish_tty: Reached maximum tty number of %d.\n", |
| 333 | goldfish_tty_current_line_count); |
| 334 | ret = -ENOMEM; |
| 335 | goto err_unlock; |
| 336 | } |
Greg Hackmann | 465893e | 2016-02-26 19:01:05 +0000 | [diff] [blame] | 337 | |
Alan | d78055d | 2014-05-12 16:57:14 +0100 | [diff] [blame] | 338 | if (goldfish_tty_current_line_count == 0) { |
Arve Hjønnevåg | 666b779 | 2013-01-21 23:38:47 +0000 | [diff] [blame] | 339 | ret = goldfish_tty_create_driver(); |
Alan | d78055d | 2014-05-12 16:57:14 +0100 | [diff] [blame] | 340 | if (ret) |
Miodrag Dinic | 7157d2b | 2017-08-29 15:53:19 +0200 | [diff] [blame] | 341 | goto err_unlock; |
Arve Hjønnevåg | 666b779 | 2013-01-21 23:38:47 +0000 | [diff] [blame] | 342 | } |
| 343 | goldfish_tty_current_line_count++; |
| 344 | |
Greg Hackmann | 465893e | 2016-02-26 19:01:05 +0000 | [diff] [blame] | 345 | qtty = &goldfish_ttys[line]; |
Arve Hjønnevåg | 666b779 | 2013-01-21 23:38:47 +0000 | [diff] [blame] | 346 | spin_lock_init(&qtty->lock); |
| 347 | tty_port_init(&qtty->port); |
| 348 | qtty->port.ops = &goldfish_port_ops; |
| 349 | qtty->base = base; |
| 350 | qtty->irq = irq; |
Miodrag Dinic | 7157d2b | 2017-08-29 15:53:19 +0200 | [diff] [blame] | 351 | qtty->dev = &pdev->dev; |
| 352 | |
| 353 | /* |
| 354 | * Goldfish TTY device used by the Goldfish emulator |
| 355 | * should identify itself with 0, forcing the driver |
| 356 | * to use virtual addresses. Goldfish TTY device |
| 357 | * on Ranchu emulator (qemu2) returns 1 here and |
| 358 | * driver will use physical addresses. |
| 359 | */ |
| 360 | qtty->version = readl(base + GOLDFISH_TTY_REG_VERSION); |
| 361 | |
| 362 | /* |
| 363 | * Goldfish TTY device on Ranchu emulator (qemu2) |
| 364 | * will use DMA for read/write IO operations. |
| 365 | */ |
| 366 | if (qtty->version > 0) { |
| 367 | /* |
| 368 | * Initialize dma_mask to 32-bits. |
| 369 | */ |
| 370 | if (!pdev->dev.dma_mask) |
| 371 | pdev->dev.dma_mask = &pdev->dev.coherent_dma_mask; |
| 372 | ret = dma_set_mask(&pdev->dev, DMA_BIT_MASK(32)); |
| 373 | if (ret) { |
| 374 | dev_err(&pdev->dev, "No suitable DMA available.\n"); |
| 375 | goto err_dec_line_count; |
| 376 | } |
| 377 | } |
Arve Hjønnevåg | 666b779 | 2013-01-21 23:38:47 +0000 | [diff] [blame] | 378 | |
Aleksandar Markovic | 2296eee | 2017-08-29 15:53:18 +0200 | [diff] [blame] | 379 | writel(GOLDFISH_TTY_CMD_INT_DISABLE, base + GOLDFISH_TTY_REG_CMD); |
Arve Hjønnevåg | 666b779 | 2013-01-21 23:38:47 +0000 | [diff] [blame] | 380 | |
Alan | d78055d | 2014-05-12 16:57:14 +0100 | [diff] [blame] | 381 | ret = request_irq(irq, goldfish_tty_interrupt, IRQF_SHARED, |
Miodrag Dinic | 7157d2b | 2017-08-29 15:53:19 +0200 | [diff] [blame] | 382 | "goldfish_tty", qtty); |
| 383 | if (ret) { |
| 384 | pr_err("goldfish_tty: No IRQ available!\n"); |
| 385 | goto err_dec_line_count; |
| 386 | } |
Arve Hjønnevåg | 666b779 | 2013-01-21 23:38:47 +0000 | [diff] [blame] | 387 | |
| 388 | ttydev = tty_port_register_device(&qtty->port, goldfish_tty_driver, |
Miodrag Dinic | 7157d2b | 2017-08-29 15:53:19 +0200 | [diff] [blame] | 389 | line, &pdev->dev); |
Alan | d78055d | 2014-05-12 16:57:14 +0100 | [diff] [blame] | 390 | if (IS_ERR(ttydev)) { |
Arve Hjønnevåg | 666b779 | 2013-01-21 23:38:47 +0000 | [diff] [blame] | 391 | ret = PTR_ERR(ttydev); |
| 392 | goto err_tty_register_device_failed; |
| 393 | } |
| 394 | |
| 395 | strcpy(qtty->console.name, "ttyGF"); |
| 396 | qtty->console.write = goldfish_tty_console_write; |
| 397 | qtty->console.device = goldfish_tty_console_device; |
| 398 | qtty->console.setup = goldfish_tty_console_setup; |
| 399 | qtty->console.flags = CON_PRINTBUFFER; |
Greg Hackmann | 465893e | 2016-02-26 19:01:05 +0000 | [diff] [blame] | 400 | qtty->console.index = line; |
Arve Hjønnevåg | 666b779 | 2013-01-21 23:38:47 +0000 | [diff] [blame] | 401 | register_console(&qtty->console); |
Greg Hackmann | 465893e | 2016-02-26 19:01:05 +0000 | [diff] [blame] | 402 | platform_set_drvdata(pdev, qtty); |
Arve Hjønnevåg | 666b779 | 2013-01-21 23:38:47 +0000 | [diff] [blame] | 403 | |
| 404 | mutex_unlock(&goldfish_tty_lock); |
| 405 | return 0; |
| 406 | |
Arve Hjønnevåg | 666b779 | 2013-01-21 23:38:47 +0000 | [diff] [blame] | 407 | err_tty_register_device_failed: |
Christophe JAILLET | 1a5c2d1 | 2017-01-09 01:26:37 +0100 | [diff] [blame] | 408 | free_irq(irq, qtty); |
Miodrag Dinic | 7157d2b | 2017-08-29 15:53:19 +0200 | [diff] [blame] | 409 | err_dec_line_count: |
Arve Hjønnevåg | 666b779 | 2013-01-21 23:38:47 +0000 | [diff] [blame] | 410 | goldfish_tty_current_line_count--; |
Alan | d78055d | 2014-05-12 16:57:14 +0100 | [diff] [blame] | 411 | if (goldfish_tty_current_line_count == 0) |
Arve Hjønnevåg | 666b779 | 2013-01-21 23:38:47 +0000 | [diff] [blame] | 412 | goldfish_tty_delete_driver(); |
Miodrag Dinic | 7157d2b | 2017-08-29 15:53:19 +0200 | [diff] [blame] | 413 | err_unlock: |
Arve Hjønnevåg | 666b779 | 2013-01-21 23:38:47 +0000 | [diff] [blame] | 414 | mutex_unlock(&goldfish_tty_lock); |
| 415 | err_unmap: |
| 416 | iounmap(base); |
| 417 | return ret; |
| 418 | } |
| 419 | |
| 420 | static int goldfish_tty_remove(struct platform_device *pdev) |
| 421 | { |
Greg Hackmann | 465893e | 2016-02-26 19:01:05 +0000 | [diff] [blame] | 422 | struct goldfish_tty *qtty = platform_get_drvdata(pdev); |
Arve Hjønnevåg | 666b779 | 2013-01-21 23:38:47 +0000 | [diff] [blame] | 423 | |
| 424 | mutex_lock(&goldfish_tty_lock); |
| 425 | |
Arve Hjønnevåg | 666b779 | 2013-01-21 23:38:47 +0000 | [diff] [blame] | 426 | unregister_console(&qtty->console); |
Greg Hackmann | 465893e | 2016-02-26 19:01:05 +0000 | [diff] [blame] | 427 | tty_unregister_device(goldfish_tty_driver, qtty->console.index); |
Arve Hjønnevåg | 666b779 | 2013-01-21 23:38:47 +0000 | [diff] [blame] | 428 | iounmap(qtty->base); |
Fabian Frederick | a4dc923 | 2014-09-28 20:10:17 +0200 | [diff] [blame] | 429 | qtty->base = NULL; |
Arve Hjønnevåg | 666b779 | 2013-01-21 23:38:47 +0000 | [diff] [blame] | 430 | free_irq(qtty->irq, pdev); |
| 431 | goldfish_tty_current_line_count--; |
Alan | d78055d | 2014-05-12 16:57:14 +0100 | [diff] [blame] | 432 | if (goldfish_tty_current_line_count == 0) |
Arve Hjønnevåg | 666b779 | 2013-01-21 23:38:47 +0000 | [diff] [blame] | 433 | goldfish_tty_delete_driver(); |
| 434 | mutex_unlock(&goldfish_tty_lock); |
| 435 | return 0; |
| 436 | } |
| 437 | |
Sebastian Andrzej Siewior | 6a28fd2 | 2017-11-30 09:16:31 +0100 | [diff] [blame] | 438 | #ifdef CONFIG_GOLDFISH_TTY_EARLY_CONSOLE |
Miodrag Dinic | 3840ed9 | 2017-08-29 15:53:20 +0200 | [diff] [blame] | 439 | static void gf_early_console_putchar(struct uart_port *port, int ch) |
| 440 | { |
| 441 | __raw_writel(ch, port->membase); |
| 442 | } |
| 443 | |
| 444 | static void gf_early_write(struct console *con, const char *s, unsigned int n) |
| 445 | { |
| 446 | struct earlycon_device *dev = con->data; |
| 447 | |
| 448 | uart_console_write(&dev->port, s, n, gf_early_console_putchar); |
| 449 | } |
| 450 | |
| 451 | static int __init gf_earlycon_setup(struct earlycon_device *device, |
| 452 | const char *opt) |
| 453 | { |
| 454 | if (!device->port.membase) |
| 455 | return -ENODEV; |
| 456 | |
| 457 | device->con->write = gf_early_write; |
| 458 | return 0; |
| 459 | } |
| 460 | |
| 461 | OF_EARLYCON_DECLARE(early_gf_tty, "google,goldfish-tty", gf_earlycon_setup); |
Sebastian Andrzej Siewior | 6a28fd2 | 2017-11-30 09:16:31 +0100 | [diff] [blame] | 462 | #endif |
Miodrag Dinic | 3840ed9 | 2017-08-29 15:53:20 +0200 | [diff] [blame] | 463 | |
Miodrag Dinic | 9b883ee | 2016-02-26 19:00:44 +0000 | [diff] [blame] | 464 | static const struct of_device_id goldfish_tty_of_match[] = { |
| 465 | { .compatible = "google,goldfish-tty", }, |
| 466 | {}, |
| 467 | }; |
| 468 | |
| 469 | MODULE_DEVICE_TABLE(of, goldfish_tty_of_match); |
| 470 | |
Arve Hjønnevåg | 666b779 | 2013-01-21 23:38:47 +0000 | [diff] [blame] | 471 | static struct platform_driver goldfish_tty_platform_driver = { |
| 472 | .probe = goldfish_tty_probe, |
| 473 | .remove = goldfish_tty_remove, |
| 474 | .driver = { |
Miodrag Dinic | 9b883ee | 2016-02-26 19:00:44 +0000 | [diff] [blame] | 475 | .name = "goldfish_tty", |
| 476 | .of_match_table = goldfish_tty_of_match, |
Arve Hjønnevåg | 666b779 | 2013-01-21 23:38:47 +0000 | [diff] [blame] | 477 | } |
| 478 | }; |
| 479 | |
| 480 | module_platform_driver(goldfish_tty_platform_driver); |
| 481 | |
| 482 | MODULE_LICENSE("GPL v2"); |