goldfish.c 12 KB

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