goldfish.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482
  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. gf_iowrite32(count, base + GOLDFISH_TTY_REG_DATA_LEN);
  58. if (is_write)
  59. gf_iowrite32(GOLDFISH_TTY_CMD_WRITE_BUFFER,
  60. base + GOLDFISH_TTY_REG_CMD);
  61. else
  62. gf_iowrite32(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 = gf_ioread32(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_flip_buffer_push(&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. gf_iowrite32(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. gf_iowrite32(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 unsigned int goldfish_tty_write_room(struct tty_struct *tty)
  167. {
  168. return 0x10000;
  169. }
  170. static unsigned 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 gf_ioread32(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 = tty_alloc_driver(goldfish_tty_line_count,
  219. TTY_DRIVER_RESET_TERMIOS | TTY_DRIVER_REAL_RAW |
  220. TTY_DRIVER_DYNAMIC_DEV);
  221. if (IS_ERR(tty)) {
  222. ret = PTR_ERR(tty);
  223. goto err_tty_alloc_driver_failed;
  224. }
  225. tty->driver_name = "goldfish";
  226. tty->name = "ttyGF";
  227. tty->type = TTY_DRIVER_TYPE_SERIAL;
  228. tty->subtype = SERIAL_TYPE_NORMAL;
  229. tty->init_termios = tty_std_termios;
  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. tty_driver_kref_put(tty);
  238. err_tty_alloc_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. tty_driver_kref_put(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. int 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. irq = platform_get_irq(pdev, 0);
  272. if (irq < 0) {
  273. ret = irq;
  274. goto err_unmap;
  275. }
  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 = gf_ioread32(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. gf_iowrite32(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. tty_port_destroy(&qtty->port);
  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, qtty);
  369. tty_port_destroy(&qtty->port);
  370. goldfish_tty_current_line_count--;
  371. if (goldfish_tty_current_line_count == 0)
  372. goldfish_tty_delete_driver();
  373. mutex_unlock(&goldfish_tty_lock);
  374. return 0;
  375. }
  376. #ifdef CONFIG_GOLDFISH_TTY_EARLY_CONSOLE
  377. static void gf_early_console_putchar(struct uart_port *port, unsigned char ch)
  378. {
  379. gf_iowrite32(ch, port->membase);
  380. }
  381. static void gf_early_write(struct console *con, const char *s, unsigned int n)
  382. {
  383. struct earlycon_device *dev = con->data;
  384. uart_console_write(&dev->port, s, n, gf_early_console_putchar);
  385. }
  386. static int __init gf_earlycon_setup(struct earlycon_device *device,
  387. const char *opt)
  388. {
  389. if (!device->port.membase)
  390. return -ENODEV;
  391. device->con->write = gf_early_write;
  392. return 0;
  393. }
  394. OF_EARLYCON_DECLARE(early_gf_tty, "google,goldfish-tty", gf_earlycon_setup);
  395. #endif
  396. static const struct of_device_id goldfish_tty_of_match[] = {
  397. { .compatible = "google,goldfish-tty", },
  398. {},
  399. };
  400. MODULE_DEVICE_TABLE(of, goldfish_tty_of_match);
  401. static struct platform_driver goldfish_tty_platform_driver = {
  402. .probe = goldfish_tty_probe,
  403. .remove = goldfish_tty_remove,
  404. .driver = {
  405. .name = "goldfish_tty",
  406. .of_match_table = goldfish_tty_of_match,
  407. }
  408. };
  409. module_platform_driver(goldfish_tty_platform_driver);
  410. MODULE_LICENSE("GPL v2");