goldfish.c 12 KB

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