goldfish.c 12 KB

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