goldfish.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480
  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 = kzalloc(sizeof(*goldfish_ttys) *
  211. goldfish_tty_line_count, GFP_KERNEL);
  212. if (goldfish_ttys == NULL) {
  213. ret = -ENOMEM;
  214. goto err_alloc_goldfish_ttys_failed;
  215. }
  216. tty = alloc_tty_driver(goldfish_tty_line_count);
  217. if (tty == NULL) {
  218. ret = -ENOMEM;
  219. goto err_alloc_tty_driver_failed;
  220. }
  221. tty->driver_name = "goldfish";
  222. tty->name = "ttyGF";
  223. tty->type = TTY_DRIVER_TYPE_SERIAL;
  224. tty->subtype = SERIAL_TYPE_NORMAL;
  225. tty->init_termios = tty_std_termios;
  226. tty->flags = TTY_DRIVER_RESET_TERMIOS | TTY_DRIVER_REAL_RAW |
  227. TTY_DRIVER_DYNAMIC_DEV;
  228. tty_set_operations(tty, &goldfish_tty_ops);
  229. ret = tty_register_driver(tty);
  230. if (ret)
  231. goto err_tty_register_driver_failed;
  232. goldfish_tty_driver = tty;
  233. return 0;
  234. err_tty_register_driver_failed:
  235. put_tty_driver(tty);
  236. err_alloc_tty_driver_failed:
  237. kfree(goldfish_ttys);
  238. goldfish_ttys = NULL;
  239. err_alloc_goldfish_ttys_failed:
  240. return ret;
  241. }
  242. static void goldfish_tty_delete_driver(void)
  243. {
  244. tty_unregister_driver(goldfish_tty_driver);
  245. put_tty_driver(goldfish_tty_driver);
  246. goldfish_tty_driver = NULL;
  247. kfree(goldfish_ttys);
  248. goldfish_ttys = NULL;
  249. }
  250. static int goldfish_tty_probe(struct platform_device *pdev)
  251. {
  252. struct goldfish_tty *qtty;
  253. int ret = -ENODEV;
  254. struct resource *r;
  255. struct device *ttydev;
  256. void __iomem *base;
  257. u32 irq;
  258. unsigned int line;
  259. r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  260. if (!r) {
  261. pr_err("goldfish_tty: No MEM resource available!\n");
  262. return -ENOMEM;
  263. }
  264. base = ioremap(r->start, 0x1000);
  265. if (!base) {
  266. pr_err("goldfish_tty: Unable to ioremap base!\n");
  267. return -ENOMEM;
  268. }
  269. r = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
  270. if (!r) {
  271. pr_err("goldfish_tty: No IRQ resource available!\n");
  272. goto err_unmap;
  273. }
  274. irq = r->start;
  275. mutex_lock(&goldfish_tty_lock);
  276. if (pdev->id == PLATFORM_DEVID_NONE)
  277. line = goldfish_tty_current_line_count;
  278. else
  279. line = pdev->id;
  280. if (line >= goldfish_tty_line_count) {
  281. pr_err("goldfish_tty: Reached maximum tty number of %d.\n",
  282. goldfish_tty_current_line_count);
  283. ret = -ENOMEM;
  284. goto err_unlock;
  285. }
  286. if (goldfish_tty_current_line_count == 0) {
  287. ret = goldfish_tty_create_driver();
  288. if (ret)
  289. goto err_unlock;
  290. }
  291. goldfish_tty_current_line_count++;
  292. qtty = &goldfish_ttys[line];
  293. spin_lock_init(&qtty->lock);
  294. tty_port_init(&qtty->port);
  295. qtty->port.ops = &goldfish_port_ops;
  296. qtty->base = base;
  297. qtty->irq = irq;
  298. qtty->dev = &pdev->dev;
  299. /*
  300. * Goldfish TTY device used by the Goldfish emulator
  301. * should identify itself with 0, forcing the driver
  302. * to use virtual addresses. Goldfish TTY device
  303. * on Ranchu emulator (qemu2) returns 1 here and
  304. * driver will use physical addresses.
  305. */
  306. qtty->version = readl(base + GOLDFISH_TTY_REG_VERSION);
  307. /*
  308. * Goldfish TTY device on Ranchu emulator (qemu2)
  309. * will use DMA for read/write IO operations.
  310. */
  311. if (qtty->version > 0) {
  312. /*
  313. * Initialize dma_mask to 32-bits.
  314. */
  315. if (!pdev->dev.dma_mask)
  316. pdev->dev.dma_mask = &pdev->dev.coherent_dma_mask;
  317. ret = dma_set_mask(&pdev->dev, DMA_BIT_MASK(32));
  318. if (ret) {
  319. dev_err(&pdev->dev, "No suitable DMA available.\n");
  320. goto err_dec_line_count;
  321. }
  322. }
  323. writel(GOLDFISH_TTY_CMD_INT_DISABLE, base + GOLDFISH_TTY_REG_CMD);
  324. ret = request_irq(irq, goldfish_tty_interrupt, IRQF_SHARED,
  325. "goldfish_tty", qtty);
  326. if (ret) {
  327. pr_err("goldfish_tty: No IRQ available!\n");
  328. goto err_dec_line_count;
  329. }
  330. ttydev = tty_port_register_device(&qtty->port, goldfish_tty_driver,
  331. line, &pdev->dev);
  332. if (IS_ERR(ttydev)) {
  333. ret = PTR_ERR(ttydev);
  334. goto err_tty_register_device_failed;
  335. }
  336. strcpy(qtty->console.name, "ttyGF");
  337. qtty->console.write = goldfish_tty_console_write;
  338. qtty->console.device = goldfish_tty_console_device;
  339. qtty->console.setup = goldfish_tty_console_setup;
  340. qtty->console.flags = CON_PRINTBUFFER;
  341. qtty->console.index = line;
  342. register_console(&qtty->console);
  343. platform_set_drvdata(pdev, qtty);
  344. mutex_unlock(&goldfish_tty_lock);
  345. return 0;
  346. err_tty_register_device_failed:
  347. free_irq(irq, qtty);
  348. err_dec_line_count:
  349. goldfish_tty_current_line_count--;
  350. if (goldfish_tty_current_line_count == 0)
  351. goldfish_tty_delete_driver();
  352. err_unlock:
  353. mutex_unlock(&goldfish_tty_lock);
  354. err_unmap:
  355. iounmap(base);
  356. return ret;
  357. }
  358. static int goldfish_tty_remove(struct platform_device *pdev)
  359. {
  360. struct goldfish_tty *qtty = platform_get_drvdata(pdev);
  361. mutex_lock(&goldfish_tty_lock);
  362. unregister_console(&qtty->console);
  363. tty_unregister_device(goldfish_tty_driver, qtty->console.index);
  364. iounmap(qtty->base);
  365. qtty->base = NULL;
  366. free_irq(qtty->irq, pdev);
  367. goldfish_tty_current_line_count--;
  368. if (goldfish_tty_current_line_count == 0)
  369. goldfish_tty_delete_driver();
  370. mutex_unlock(&goldfish_tty_lock);
  371. return 0;
  372. }
  373. #ifdef CONFIG_GOLDFISH_TTY_EARLY_CONSOLE
  374. static void gf_early_console_putchar(struct uart_port *port, int ch)
  375. {
  376. __raw_writel(ch, port->membase);
  377. }
  378. static void gf_early_write(struct console *con, const char *s, unsigned int n)
  379. {
  380. struct earlycon_device *dev = con->data;
  381. uart_console_write(&dev->port, s, n, gf_early_console_putchar);
  382. }
  383. static int __init gf_earlycon_setup(struct earlycon_device *device,
  384. const char *opt)
  385. {
  386. if (!device->port.membase)
  387. return -ENODEV;
  388. device->con->write = gf_early_write;
  389. return 0;
  390. }
  391. OF_EARLYCON_DECLARE(early_gf_tty, "google,goldfish-tty", gf_earlycon_setup);
  392. #endif
  393. static const struct of_device_id goldfish_tty_of_match[] = {
  394. { .compatible = "google,goldfish-tty", },
  395. {},
  396. };
  397. MODULE_DEVICE_TABLE(of, goldfish_tty_of_match);
  398. static struct platform_driver goldfish_tty_platform_driver = {
  399. .probe = goldfish_tty_probe,
  400. .remove = goldfish_tty_remove,
  401. .driver = {
  402. .name = "goldfish_tty",
  403. .of_match_table = goldfish_tty_of_match,
  404. }
  405. };
  406. module_platform_driver(goldfish_tty_platform_driver);
  407. MODULE_LICENSE("GPL v2");