goldfish.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  1. /*
  2. * Copyright (C) 2007 Google, Inc.
  3. * Copyright (C) 2012 Intel, Inc.
  4. *
  5. * This software is licensed under the terms of the GNU General Public
  6. * License version 2, as published by the Free Software Foundation, and
  7. * may be copied, distributed, and modified under those terms.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. */
  15. #include <linux/console.h>
  16. #include <linux/interrupt.h>
  17. #include <linux/platform_device.h>
  18. #include <linux/tty.h>
  19. #include <linux/tty_flip.h>
  20. #include <linux/slab.h>
  21. #include <linux/io.h>
  22. #include <linux/module.h>
  23. #include <linux/goldfish.h>
  24. enum {
  25. GOLDFISH_TTY_PUT_CHAR = 0x00,
  26. GOLDFISH_TTY_BYTES_READY = 0x04,
  27. GOLDFISH_TTY_CMD = 0x08,
  28. GOLDFISH_TTY_DATA_PTR = 0x10,
  29. GOLDFISH_TTY_DATA_LEN = 0x14,
  30. GOLDFISH_TTY_DATA_PTR_HIGH = 0x18,
  31. GOLDFISH_TTY_CMD_INT_DISABLE = 0,
  32. GOLDFISH_TTY_CMD_INT_ENABLE = 1,
  33. GOLDFISH_TTY_CMD_WRITE_BUFFER = 2,
  34. GOLDFISH_TTY_CMD_READ_BUFFER = 3,
  35. };
  36. struct goldfish_tty {
  37. struct tty_port port;
  38. spinlock_t lock;
  39. void __iomem *base;
  40. u32 irq;
  41. int opencount;
  42. struct console console;
  43. };
  44. static DEFINE_MUTEX(goldfish_tty_lock);
  45. static struct tty_driver *goldfish_tty_driver;
  46. static u32 goldfish_tty_line_count = 8;
  47. static u32 goldfish_tty_current_line_count;
  48. static struct goldfish_tty *goldfish_ttys;
  49. static void goldfish_tty_do_write(int line, const char *buf, unsigned count)
  50. {
  51. unsigned long irq_flags;
  52. struct goldfish_tty *qtty = &goldfish_ttys[line];
  53. void __iomem *base = qtty->base;
  54. spin_lock_irqsave(&qtty->lock, irq_flags);
  55. gf_write_ptr(buf, base + GOLDFISH_TTY_DATA_PTR,
  56. base + GOLDFISH_TTY_DATA_PTR_HIGH);
  57. writel(count, base + GOLDFISH_TTY_DATA_LEN);
  58. writel(GOLDFISH_TTY_CMD_WRITE_BUFFER, base + GOLDFISH_TTY_CMD);
  59. spin_unlock_irqrestore(&qtty->lock, irq_flags);
  60. }
  61. static irqreturn_t goldfish_tty_interrupt(int irq, void *dev_id)
  62. {
  63. struct goldfish_tty *qtty = dev_id;
  64. void __iomem *base = qtty->base;
  65. unsigned long irq_flags;
  66. unsigned char *buf;
  67. u32 count;
  68. count = readl(base + GOLDFISH_TTY_BYTES_READY);
  69. if (count == 0)
  70. return IRQ_NONE;
  71. count = tty_prepare_flip_string(&qtty->port, &buf, count);
  72. spin_lock_irqsave(&qtty->lock, irq_flags);
  73. gf_write_ptr(buf, base + GOLDFISH_TTY_DATA_PTR,
  74. base + GOLDFISH_TTY_DATA_PTR_HIGH);
  75. writel(count, base + GOLDFISH_TTY_DATA_LEN);
  76. writel(GOLDFISH_TTY_CMD_READ_BUFFER, base + GOLDFISH_TTY_CMD);
  77. spin_unlock_irqrestore(&qtty->lock, irq_flags);
  78. tty_schedule_flip(&qtty->port);
  79. return IRQ_HANDLED;
  80. }
  81. static int goldfish_tty_activate(struct tty_port *port, struct tty_struct *tty)
  82. {
  83. struct goldfish_tty *qtty = container_of(port, struct goldfish_tty,
  84. port);
  85. writel(GOLDFISH_TTY_CMD_INT_ENABLE, qtty->base + GOLDFISH_TTY_CMD);
  86. return 0;
  87. }
  88. static void goldfish_tty_shutdown(struct tty_port *port)
  89. {
  90. struct goldfish_tty *qtty = container_of(port, struct goldfish_tty,
  91. port);
  92. writel(GOLDFISH_TTY_CMD_INT_DISABLE, qtty->base + GOLDFISH_TTY_CMD);
  93. }
  94. static int goldfish_tty_open(struct tty_struct *tty, struct file *filp)
  95. {
  96. struct goldfish_tty *qtty = &goldfish_ttys[tty->index];
  97. return tty_port_open(&qtty->port, tty, filp);
  98. }
  99. static void goldfish_tty_close(struct tty_struct *tty, struct file *filp)
  100. {
  101. tty_port_close(tty->port, tty, filp);
  102. }
  103. static void goldfish_tty_hangup(struct tty_struct *tty)
  104. {
  105. tty_port_hangup(tty->port);
  106. }
  107. static int goldfish_tty_write(struct tty_struct *tty, const unsigned char *buf,
  108. int count)
  109. {
  110. goldfish_tty_do_write(tty->index, buf, count);
  111. return count;
  112. }
  113. static int goldfish_tty_write_room(struct tty_struct *tty)
  114. {
  115. return 0x10000;
  116. }
  117. static int goldfish_tty_chars_in_buffer(struct tty_struct *tty)
  118. {
  119. struct goldfish_tty *qtty = &goldfish_ttys[tty->index];
  120. void __iomem *base = qtty->base;
  121. return readl(base + GOLDFISH_TTY_BYTES_READY);
  122. }
  123. static void goldfish_tty_console_write(struct console *co, const char *b,
  124. unsigned count)
  125. {
  126. goldfish_tty_do_write(co->index, b, count);
  127. }
  128. static struct tty_driver *goldfish_tty_console_device(struct console *c,
  129. int *index)
  130. {
  131. *index = c->index;
  132. return goldfish_tty_driver;
  133. }
  134. static int goldfish_tty_console_setup(struct console *co, char *options)
  135. {
  136. if ((unsigned)co->index >= goldfish_tty_line_count)
  137. return -ENODEV;
  138. if (!goldfish_ttys[co->index].base)
  139. return -ENODEV;
  140. return 0;
  141. }
  142. static const struct tty_port_operations goldfish_port_ops = {
  143. .activate = goldfish_tty_activate,
  144. .shutdown = goldfish_tty_shutdown
  145. };
  146. static const struct tty_operations goldfish_tty_ops = {
  147. .open = goldfish_tty_open,
  148. .close = goldfish_tty_close,
  149. .hangup = goldfish_tty_hangup,
  150. .write = goldfish_tty_write,
  151. .write_room = goldfish_tty_write_room,
  152. .chars_in_buffer = goldfish_tty_chars_in_buffer,
  153. };
  154. static int goldfish_tty_create_driver(void)
  155. {
  156. int ret;
  157. struct tty_driver *tty;
  158. goldfish_ttys = kzalloc(sizeof(*goldfish_ttys) *
  159. goldfish_tty_line_count, GFP_KERNEL);
  160. if (goldfish_ttys == NULL) {
  161. ret = -ENOMEM;
  162. goto err_alloc_goldfish_ttys_failed;
  163. }
  164. tty = alloc_tty_driver(goldfish_tty_line_count);
  165. if (tty == NULL) {
  166. ret = -ENOMEM;
  167. goto err_alloc_tty_driver_failed;
  168. }
  169. tty->driver_name = "goldfish";
  170. tty->name = "ttyGF";
  171. tty->type = TTY_DRIVER_TYPE_SERIAL;
  172. tty->subtype = SERIAL_TYPE_NORMAL;
  173. tty->init_termios = tty_std_termios;
  174. tty->flags = TTY_DRIVER_RESET_TERMIOS | TTY_DRIVER_REAL_RAW |
  175. TTY_DRIVER_DYNAMIC_DEV;
  176. tty_set_operations(tty, &goldfish_tty_ops);
  177. ret = tty_register_driver(tty);
  178. if (ret)
  179. goto err_tty_register_driver_failed;
  180. goldfish_tty_driver = tty;
  181. return 0;
  182. err_tty_register_driver_failed:
  183. put_tty_driver(tty);
  184. err_alloc_tty_driver_failed:
  185. kfree(goldfish_ttys);
  186. goldfish_ttys = NULL;
  187. err_alloc_goldfish_ttys_failed:
  188. return ret;
  189. }
  190. static void goldfish_tty_delete_driver(void)
  191. {
  192. tty_unregister_driver(goldfish_tty_driver);
  193. put_tty_driver(goldfish_tty_driver);
  194. goldfish_tty_driver = NULL;
  195. kfree(goldfish_ttys);
  196. goldfish_ttys = NULL;
  197. }
  198. static int goldfish_tty_probe(struct platform_device *pdev)
  199. {
  200. struct goldfish_tty *qtty;
  201. int ret = -EINVAL;
  202. struct resource *r;
  203. struct device *ttydev;
  204. void __iomem *base;
  205. u32 irq;
  206. unsigned int line;
  207. r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  208. if (r == NULL)
  209. return -EINVAL;
  210. base = ioremap(r->start, 0x1000);
  211. if (base == NULL)
  212. pr_err("goldfish_tty: unable to remap base\n");
  213. r = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
  214. if (r == NULL)
  215. goto err_unmap;
  216. irq = r->start;
  217. mutex_lock(&goldfish_tty_lock);
  218. if (pdev->id == PLATFORM_DEVID_NONE)
  219. line = goldfish_tty_current_line_count;
  220. else
  221. line = pdev->id;
  222. if (line >= goldfish_tty_line_count)
  223. goto err_create_driver_failed;
  224. if (goldfish_tty_current_line_count == 0) {
  225. ret = goldfish_tty_create_driver();
  226. if (ret)
  227. goto err_create_driver_failed;
  228. }
  229. goldfish_tty_current_line_count++;
  230. qtty = &goldfish_ttys[line];
  231. spin_lock_init(&qtty->lock);
  232. tty_port_init(&qtty->port);
  233. qtty->port.ops = &goldfish_port_ops;
  234. qtty->base = base;
  235. qtty->irq = irq;
  236. writel(GOLDFISH_TTY_CMD_INT_DISABLE, base + GOLDFISH_TTY_CMD);
  237. ret = request_irq(irq, goldfish_tty_interrupt, IRQF_SHARED,
  238. "goldfish_tty", qtty);
  239. if (ret)
  240. goto err_request_irq_failed;
  241. ttydev = tty_port_register_device(&qtty->port, goldfish_tty_driver,
  242. line, &pdev->dev);
  243. if (IS_ERR(ttydev)) {
  244. ret = PTR_ERR(ttydev);
  245. goto err_tty_register_device_failed;
  246. }
  247. strcpy(qtty->console.name, "ttyGF");
  248. qtty->console.write = goldfish_tty_console_write;
  249. qtty->console.device = goldfish_tty_console_device;
  250. qtty->console.setup = goldfish_tty_console_setup;
  251. qtty->console.flags = CON_PRINTBUFFER;
  252. qtty->console.index = line;
  253. register_console(&qtty->console);
  254. platform_set_drvdata(pdev, qtty);
  255. mutex_unlock(&goldfish_tty_lock);
  256. return 0;
  257. err_tty_register_device_failed:
  258. free_irq(irq, qtty);
  259. err_request_irq_failed:
  260. goldfish_tty_current_line_count--;
  261. if (goldfish_tty_current_line_count == 0)
  262. goldfish_tty_delete_driver();
  263. err_create_driver_failed:
  264. mutex_unlock(&goldfish_tty_lock);
  265. err_unmap:
  266. iounmap(base);
  267. return ret;
  268. }
  269. static int goldfish_tty_remove(struct platform_device *pdev)
  270. {
  271. struct goldfish_tty *qtty = platform_get_drvdata(pdev);
  272. mutex_lock(&goldfish_tty_lock);
  273. unregister_console(&qtty->console);
  274. tty_unregister_device(goldfish_tty_driver, qtty->console.index);
  275. iounmap(qtty->base);
  276. qtty->base = NULL;
  277. free_irq(qtty->irq, pdev);
  278. goldfish_tty_current_line_count--;
  279. if (goldfish_tty_current_line_count == 0)
  280. goldfish_tty_delete_driver();
  281. mutex_unlock(&goldfish_tty_lock);
  282. return 0;
  283. }
  284. static const struct of_device_id goldfish_tty_of_match[] = {
  285. { .compatible = "google,goldfish-tty", },
  286. {},
  287. };
  288. MODULE_DEVICE_TABLE(of, goldfish_tty_of_match);
  289. static struct platform_driver goldfish_tty_platform_driver = {
  290. .probe = goldfish_tty_probe,
  291. .remove = goldfish_tty_remove,
  292. .driver = {
  293. .name = "goldfish_tty",
  294. .of_match_table = goldfish_tty_of_match,
  295. }
  296. };
  297. module_platform_driver(goldfish_tty_platform_driver);
  298. MODULE_LICENSE("GPL v2");