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