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