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