1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * Copyright (C) 2007 Google, Inc. 4 * Copyright (C) 2012 Intel, Inc. 5 * Copyright (C) 2017 Imagination Technologies Ltd. 6 */ 7 8 #include <linux/console.h> 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> 16 #include <linux/mod_devicetable.h> 17 #include <linux/goldfish.h> 18 #include <linux/mm.h> 19 #include <linux/dma-mapping.h> 20 #include <linux/serial_core.h> 21 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 28 #define GOLDFISH_TTY_REG_VERSION 0x20 29 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 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; 43 u32 version; 44 struct device *dev; 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 53 static void do_rw_io(struct goldfish_tty *qtty, 54 unsigned long address, 55 unsigned int count, 56 bool is_write) 57 { 58 unsigned long irq_flags; 59 void __iomem *base = qtty->base; 60 61 spin_lock_irqsave(&qtty->lock, irq_flags); 62 gf_write_ptr((void *)address, base + GOLDFISH_TTY_REG_DATA_PTR, 63 base + GOLDFISH_TTY_REG_DATA_PTR_HIGH); 64 gf_iowrite32(count, base + GOLDFISH_TTY_REG_DATA_LEN); 65 66 if (is_write) 67 gf_iowrite32(GOLDFISH_TTY_CMD_WRITE_BUFFER, 68 base + GOLDFISH_TTY_REG_CMD); 69 else 70 gf_iowrite32(GOLDFISH_TTY_CMD_READ_BUFFER, 71 base + GOLDFISH_TTY_REG_CMD); 72 73 spin_unlock_irqrestore(&qtty->lock, irq_flags); 74 } 75 76 static void goldfish_tty_rw(struct goldfish_tty *qtty, 77 unsigned long addr, 78 unsigned int count, 79 bool 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 u8 *buf, unsigned int count) 129 { 130 struct goldfish_tty *qtty = &goldfish_ttys[line]; 131 132 goldfish_tty_rw(qtty, (unsigned long)buf, count, true); 133 } 134 135 static irqreturn_t goldfish_tty_interrupt(int irq, void *dev_id) 136 { 137 struct goldfish_tty *qtty = dev_id; 138 void __iomem *base = qtty->base; 139 unsigned char *buf; 140 u32 count; 141 142 count = gf_ioread32(base + GOLDFISH_TTY_REG_BYTES_READY); 143 if (count == 0) 144 return IRQ_NONE; 145 146 count = tty_prepare_flip_string(&qtty->port, &buf, count); 147 148 goldfish_tty_rw(qtty, (unsigned long)buf, count, false); 149 150 tty_flip_buffer_push(&qtty->port); 151 return IRQ_HANDLED; 152 } 153 154 static int goldfish_tty_activate(struct tty_port *port, struct tty_struct *tty) 155 { 156 struct goldfish_tty *qtty = container_of(port, struct goldfish_tty, 157 port); 158 gf_iowrite32(GOLDFISH_TTY_CMD_INT_ENABLE, qtty->base + GOLDFISH_TTY_REG_CMD); 159 return 0; 160 } 161 162 static void goldfish_tty_shutdown(struct tty_port *port) 163 { 164 struct goldfish_tty *qtty = container_of(port, struct goldfish_tty, 165 port); 166 gf_iowrite32(GOLDFISH_TTY_CMD_INT_DISABLE, qtty->base + GOLDFISH_TTY_REG_CMD); 167 } 168 169 static int goldfish_tty_open(struct tty_struct *tty, struct file *filp) 170 { 171 struct goldfish_tty *qtty = &goldfish_ttys[tty->index]; 172 return tty_port_open(&qtty->port, tty, filp); 173 } 174 175 static void goldfish_tty_close(struct tty_struct *tty, struct file *filp) 176 { 177 tty_port_close(tty->port, tty, filp); 178 } 179 180 static void goldfish_tty_hangup(struct tty_struct *tty) 181 { 182 tty_port_hangup(tty->port); 183 } 184 185 static ssize_t goldfish_tty_write(struct tty_struct *tty, const u8 *buf, 186 size_t count) 187 { 188 goldfish_tty_do_write(tty->index, buf, count); 189 return count; 190 } 191 192 static unsigned int goldfish_tty_write_room(struct tty_struct *tty) 193 { 194 return 0x10000; 195 } 196 197 static unsigned int goldfish_tty_chars_in_buffer(struct tty_struct *tty) 198 { 199 struct goldfish_tty *qtty = &goldfish_ttys[tty->index]; 200 void __iomem *base = qtty->base; 201 return gf_ioread32(base + GOLDFISH_TTY_REG_BYTES_READY); 202 } 203 204 static void goldfish_tty_console_write(struct console *co, const char *b, 205 unsigned count) 206 { 207 goldfish_tty_do_write(co->index, b, count); 208 } 209 210 static struct tty_driver *goldfish_tty_console_device(struct console *c, 211 int *index) 212 { 213 *index = c->index; 214 return goldfish_tty_driver; 215 } 216 217 static int goldfish_tty_console_setup(struct console *co, char *options) 218 { 219 if ((unsigned)co->index >= goldfish_tty_line_count) 220 return -ENODEV; 221 if (!goldfish_ttys[co->index].base) 222 return -ENODEV; 223 return 0; 224 } 225 226 static const struct tty_port_operations goldfish_port_ops = { 227 .activate = goldfish_tty_activate, 228 .shutdown = goldfish_tty_shutdown 229 }; 230 231 static const struct tty_operations goldfish_tty_ops = { 232 .open = goldfish_tty_open, 233 .close = goldfish_tty_close, 234 .hangup = goldfish_tty_hangup, 235 .write = goldfish_tty_write, 236 .write_room = goldfish_tty_write_room, 237 .chars_in_buffer = goldfish_tty_chars_in_buffer, 238 }; 239 240 static int goldfish_tty_create_driver(void) 241 { 242 int ret; 243 struct tty_driver *tty; 244 245 goldfish_ttys = kcalloc(goldfish_tty_line_count, 246 sizeof(*goldfish_ttys), 247 GFP_KERNEL); 248 if (goldfish_ttys == NULL) { 249 ret = -ENOMEM; 250 goto err_alloc_goldfish_ttys_failed; 251 } 252 tty = tty_alloc_driver(goldfish_tty_line_count, 253 TTY_DRIVER_RESET_TERMIOS | TTY_DRIVER_REAL_RAW | 254 TTY_DRIVER_DYNAMIC_DEV); 255 if (IS_ERR(tty)) { 256 ret = PTR_ERR(tty); 257 goto err_tty_alloc_driver_failed; 258 } 259 tty->driver_name = "goldfish"; 260 tty->name = "ttyGF"; 261 tty->type = TTY_DRIVER_TYPE_SERIAL; 262 tty->subtype = SERIAL_TYPE_NORMAL; 263 tty->init_termios = tty_std_termios; 264 tty_set_operations(tty, &goldfish_tty_ops); 265 ret = tty_register_driver(tty); 266 if (ret) 267 goto err_tty_register_driver_failed; 268 269 goldfish_tty_driver = tty; 270 return 0; 271 272 err_tty_register_driver_failed: 273 tty_driver_kref_put(tty); 274 err_tty_alloc_driver_failed: 275 kfree(goldfish_ttys); 276 goldfish_ttys = NULL; 277 err_alloc_goldfish_ttys_failed: 278 return ret; 279 } 280 281 static void goldfish_tty_delete_driver(void) 282 { 283 tty_unregister_driver(goldfish_tty_driver); 284 tty_driver_kref_put(goldfish_tty_driver); 285 goldfish_tty_driver = NULL; 286 kfree(goldfish_ttys); 287 goldfish_ttys = NULL; 288 } 289 290 static int goldfish_tty_probe(struct platform_device *pdev) 291 { 292 struct goldfish_tty *qtty; 293 int ret = -ENODEV; 294 struct resource *r; 295 struct device *ttydev; 296 void __iomem *base; 297 int irq; 298 unsigned int line; 299 300 r = platform_get_resource(pdev, IORESOURCE_MEM, 0); 301 if (!r) { 302 pr_err("goldfish_tty: No MEM resource available!\n"); 303 return -ENOMEM; 304 } 305 306 base = ioremap(r->start, 0x1000); 307 if (!base) { 308 pr_err("goldfish_tty: Unable to ioremap base!\n"); 309 return -ENOMEM; 310 } 311 312 irq = platform_get_irq(pdev, 0); 313 if (irq < 0) { 314 ret = irq; 315 goto err_unmap; 316 } 317 318 mutex_lock(&goldfish_tty_lock); 319 320 if (pdev->id == PLATFORM_DEVID_NONE) 321 line = goldfish_tty_current_line_count; 322 else 323 line = pdev->id; 324 325 if (line >= goldfish_tty_line_count) { 326 pr_err("goldfish_tty: Reached maximum tty number of %d.\n", 327 goldfish_tty_current_line_count); 328 ret = -ENOMEM; 329 goto err_unlock; 330 } 331 332 if (goldfish_tty_current_line_count == 0) { 333 ret = goldfish_tty_create_driver(); 334 if (ret) 335 goto err_unlock; 336 } 337 goldfish_tty_current_line_count++; 338 339 qtty = &goldfish_ttys[line]; 340 spin_lock_init(&qtty->lock); 341 tty_port_init(&qtty->port); 342 qtty->port.ops = &goldfish_port_ops; 343 qtty->base = base; 344 qtty->irq = irq; 345 qtty->dev = &pdev->dev; 346 347 /* 348 * Goldfish TTY device used by the Goldfish emulator 349 * should identify itself with 0, forcing the driver 350 * to use virtual addresses. Goldfish TTY device 351 * on Ranchu emulator (qemu2) returns 1 here and 352 * driver will use physical addresses. 353 */ 354 qtty->version = gf_ioread32(base + GOLDFISH_TTY_REG_VERSION); 355 356 /* 357 * Goldfish TTY device on Ranchu emulator (qemu2) 358 * will use DMA for read/write IO operations. 359 */ 360 if (qtty->version > 0) { 361 /* 362 * Initialize dma_mask to 32-bits. 363 */ 364 if (!pdev->dev.dma_mask) 365 pdev->dev.dma_mask = &pdev->dev.coherent_dma_mask; 366 ret = dma_set_mask(&pdev->dev, DMA_BIT_MASK(32)); 367 if (ret) { 368 dev_err(&pdev->dev, "No suitable DMA available.\n"); 369 goto err_dec_line_count; 370 } 371 } 372 373 gf_iowrite32(GOLDFISH_TTY_CMD_INT_DISABLE, base + GOLDFISH_TTY_REG_CMD); 374 375 ret = request_irq(irq, goldfish_tty_interrupt, IRQF_SHARED, 376 "goldfish_tty", qtty); 377 if (ret) { 378 pr_err("goldfish_tty: No IRQ available!\n"); 379 goto err_dec_line_count; 380 } 381 382 ttydev = tty_port_register_device(&qtty->port, goldfish_tty_driver, 383 line, &pdev->dev); 384 if (IS_ERR(ttydev)) { 385 ret = PTR_ERR(ttydev); 386 goto err_tty_register_device_failed; 387 } 388 389 strcpy(qtty->console.name, "ttyGF"); 390 qtty->console.write = goldfish_tty_console_write; 391 qtty->console.device = goldfish_tty_console_device; 392 qtty->console.setup = goldfish_tty_console_setup; 393 qtty->console.flags = CON_PRINTBUFFER; 394 qtty->console.index = line; 395 register_console(&qtty->console); 396 platform_set_drvdata(pdev, qtty); 397 398 mutex_unlock(&goldfish_tty_lock); 399 return 0; 400 401 err_tty_register_device_failed: 402 free_irq(irq, qtty); 403 err_dec_line_count: 404 tty_port_destroy(&qtty->port); 405 goldfish_tty_current_line_count--; 406 if (goldfish_tty_current_line_count == 0) 407 goldfish_tty_delete_driver(); 408 err_unlock: 409 mutex_unlock(&goldfish_tty_lock); 410 err_unmap: 411 iounmap(base); 412 return ret; 413 } 414 415 static int goldfish_tty_remove(struct platform_device *pdev) 416 { 417 struct goldfish_tty *qtty = platform_get_drvdata(pdev); 418 419 mutex_lock(&goldfish_tty_lock); 420 421 unregister_console(&qtty->console); 422 tty_unregister_device(goldfish_tty_driver, qtty->console.index); 423 iounmap(qtty->base); 424 qtty->base = NULL; 425 free_irq(qtty->irq, qtty); 426 tty_port_destroy(&qtty->port); 427 goldfish_tty_current_line_count--; 428 if (goldfish_tty_current_line_count == 0) 429 goldfish_tty_delete_driver(); 430 mutex_unlock(&goldfish_tty_lock); 431 return 0; 432 } 433 434 #ifdef CONFIG_GOLDFISH_TTY_EARLY_CONSOLE 435 static void gf_early_console_putchar(struct uart_port *port, unsigned char ch) 436 { 437 gf_iowrite32(ch, port->membase); 438 } 439 440 static void gf_early_write(struct console *con, const char *s, unsigned int n) 441 { 442 struct earlycon_device *dev = con->data; 443 444 uart_console_write(&dev->port, s, n, gf_early_console_putchar); 445 } 446 447 static int __init gf_earlycon_setup(struct earlycon_device *device, 448 const char *opt) 449 { 450 if (!device->port.membase) 451 return -ENODEV; 452 453 device->con->write = gf_early_write; 454 return 0; 455 } 456 457 OF_EARLYCON_DECLARE(early_gf_tty, "google,goldfish-tty", gf_earlycon_setup); 458 #endif 459 460 static const struct of_device_id goldfish_tty_of_match[] = { 461 { .compatible = "google,goldfish-tty", }, 462 {}, 463 }; 464 465 MODULE_DEVICE_TABLE(of, goldfish_tty_of_match); 466 467 static struct platform_driver goldfish_tty_platform_driver = { 468 .probe = goldfish_tty_probe, 469 .remove = goldfish_tty_remove, 470 .driver = { 471 .name = "goldfish_tty", 472 .of_match_table = goldfish_tty_of_match, 473 } 474 }; 475 476 module_platform_driver(goldfish_tty_platform_driver); 477 478 MODULE_LICENSE("GPL v2"); 479