goldfish.c 12 KB

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