goldfish.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  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 platform_device *pdev = dev_id;
  64. struct goldfish_tty *qtty = &goldfish_ttys[pdev->id];
  65. void __iomem *base = qtty->base;
  66. unsigned long irq_flags;
  67. unsigned char *buf;
  68. u32 count;
  69. count = readl(base + GOLDFISH_TTY_BYTES_READY);
  70. if (count == 0)
  71. return IRQ_NONE;
  72. count = tty_prepare_flip_string(&qtty->port, &buf, count);
  73. spin_lock_irqsave(&qtty->lock, irq_flags);
  74. gf_write_ptr(buf, base + GOLDFISH_TTY_DATA_PTR,
  75. base + GOLDFISH_TTY_DATA_PTR_HIGH);
  76. writel(count, base + GOLDFISH_TTY_DATA_LEN);
  77. writel(GOLDFISH_TTY_CMD_READ_BUFFER, base + GOLDFISH_TTY_CMD);
  78. spin_unlock_irqrestore(&qtty->lock, irq_flags);
  79. tty_schedule_flip(&qtty->port);
  80. return IRQ_HANDLED;
  81. }
  82. static int goldfish_tty_activate(struct tty_port *port, struct tty_struct *tty)
  83. {
  84. struct goldfish_tty *qtty = container_of(port, struct goldfish_tty,
  85. port);
  86. writel(GOLDFISH_TTY_CMD_INT_ENABLE, qtty->base + GOLDFISH_TTY_CMD);
  87. return 0;
  88. }
  89. static void goldfish_tty_shutdown(struct tty_port *port)
  90. {
  91. struct goldfish_tty *qtty = container_of(port, struct goldfish_tty,
  92. port);
  93. writel(GOLDFISH_TTY_CMD_INT_DISABLE, qtty->base + GOLDFISH_TTY_CMD);
  94. }
  95. static int goldfish_tty_open(struct tty_struct *tty, struct file *filp)
  96. {
  97. struct goldfish_tty *qtty = &goldfish_ttys[tty->index];
  98. return tty_port_open(&qtty->port, tty, filp);
  99. }
  100. static void goldfish_tty_close(struct tty_struct *tty, struct file *filp)
  101. {
  102. tty_port_close(tty->port, tty, filp);
  103. }
  104. static void goldfish_tty_hangup(struct tty_struct *tty)
  105. {
  106. tty_port_hangup(tty->port);
  107. }
  108. static int goldfish_tty_write(struct tty_struct *tty, const unsigned char *buf,
  109. int count)
  110. {
  111. goldfish_tty_do_write(tty->index, buf, count);
  112. return count;
  113. }
  114. static int goldfish_tty_write_room(struct tty_struct *tty)
  115. {
  116. return 0x10000;
  117. }
  118. static int goldfish_tty_chars_in_buffer(struct tty_struct *tty)
  119. {
  120. struct goldfish_tty *qtty = &goldfish_ttys[tty->index];
  121. void __iomem *base = qtty->base;
  122. return readl(base + GOLDFISH_TTY_BYTES_READY);
  123. }
  124. static void goldfish_tty_console_write(struct console *co, const char *b,
  125. unsigned count)
  126. {
  127. goldfish_tty_do_write(co->index, b, count);
  128. }
  129. static struct tty_driver *goldfish_tty_console_device(struct console *c,
  130. int *index)
  131. {
  132. *index = c->index;
  133. return goldfish_tty_driver;
  134. }
  135. static int goldfish_tty_console_setup(struct console *co, char *options)
  136. {
  137. if ((unsigned)co->index >= goldfish_tty_line_count)
  138. return -ENODEV;
  139. if (!goldfish_ttys[co->index].base)
  140. return -ENODEV;
  141. return 0;
  142. }
  143. static struct tty_port_operations goldfish_port_ops = {
  144. .activate = goldfish_tty_activate,
  145. .shutdown = goldfish_tty_shutdown
  146. };
  147. static const struct tty_operations goldfish_tty_ops = {
  148. .open = goldfish_tty_open,
  149. .close = goldfish_tty_close,
  150. .hangup = goldfish_tty_hangup,
  151. .write = goldfish_tty_write,
  152. .write_room = goldfish_tty_write_room,
  153. .chars_in_buffer = goldfish_tty_chars_in_buffer,
  154. };
  155. static int goldfish_tty_create_driver(void)
  156. {
  157. int ret;
  158. struct tty_driver *tty;
  159. goldfish_ttys = kzalloc(sizeof(*goldfish_ttys) *
  160. goldfish_tty_line_count, GFP_KERNEL);
  161. if (goldfish_ttys == NULL) {
  162. ret = -ENOMEM;
  163. goto err_alloc_goldfish_ttys_failed;
  164. }
  165. tty = alloc_tty_driver(goldfish_tty_line_count);
  166. if (tty == NULL) {
  167. ret = -ENOMEM;
  168. goto err_alloc_tty_driver_failed;
  169. }
  170. tty->driver_name = "goldfish";
  171. tty->name = "ttyGF";
  172. tty->type = TTY_DRIVER_TYPE_SERIAL;
  173. tty->subtype = SERIAL_TYPE_NORMAL;
  174. tty->init_termios = tty_std_termios;
  175. tty->flags = TTY_DRIVER_RESET_TERMIOS | TTY_DRIVER_REAL_RAW |
  176. TTY_DRIVER_DYNAMIC_DEV;
  177. tty_set_operations(tty, &goldfish_tty_ops);
  178. ret = tty_register_driver(tty);
  179. if (ret)
  180. goto err_tty_register_driver_failed;
  181. goldfish_tty_driver = tty;
  182. return 0;
  183. err_tty_register_driver_failed:
  184. put_tty_driver(tty);
  185. err_alloc_tty_driver_failed:
  186. kfree(goldfish_ttys);
  187. goldfish_ttys = NULL;
  188. err_alloc_goldfish_ttys_failed:
  189. return ret;
  190. }
  191. static void goldfish_tty_delete_driver(void)
  192. {
  193. tty_unregister_driver(goldfish_tty_driver);
  194. put_tty_driver(goldfish_tty_driver);
  195. goldfish_tty_driver = NULL;
  196. kfree(goldfish_ttys);
  197. goldfish_ttys = NULL;
  198. }
  199. static int goldfish_tty_probe(struct platform_device *pdev)
  200. {
  201. struct goldfish_tty *qtty;
  202. int ret = -EINVAL;
  203. struct resource *r;
  204. struct device *ttydev;
  205. void __iomem *base;
  206. u32 irq;
  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. if (pdev->id >= goldfish_tty_line_count)
  218. goto err_unmap;
  219. mutex_lock(&goldfish_tty_lock);
  220. if (goldfish_tty_current_line_count == 0) {
  221. ret = goldfish_tty_create_driver();
  222. if (ret)
  223. goto err_create_driver_failed;
  224. }
  225. goldfish_tty_current_line_count++;
  226. qtty = &goldfish_ttys[pdev->id];
  227. spin_lock_init(&qtty->lock);
  228. tty_port_init(&qtty->port);
  229. qtty->port.ops = &goldfish_port_ops;
  230. qtty->base = base;
  231. qtty->irq = irq;
  232. writel(GOLDFISH_TTY_CMD_INT_DISABLE, base + GOLDFISH_TTY_CMD);
  233. ret = request_irq(irq, goldfish_tty_interrupt, IRQF_SHARED,
  234. "goldfish_tty", pdev);
  235. if (ret)
  236. goto err_request_irq_failed;
  237. ttydev = tty_port_register_device(&qtty->port, goldfish_tty_driver,
  238. pdev->id, &pdev->dev);
  239. if (IS_ERR(ttydev)) {
  240. ret = PTR_ERR(ttydev);
  241. goto err_tty_register_device_failed;
  242. }
  243. strcpy(qtty->console.name, "ttyGF");
  244. qtty->console.write = goldfish_tty_console_write;
  245. qtty->console.device = goldfish_tty_console_device;
  246. qtty->console.setup = goldfish_tty_console_setup;
  247. qtty->console.flags = CON_PRINTBUFFER;
  248. qtty->console.index = pdev->id;
  249. register_console(&qtty->console);
  250. mutex_unlock(&goldfish_tty_lock);
  251. return 0;
  252. err_tty_register_device_failed:
  253. free_irq(irq, pdev);
  254. err_request_irq_failed:
  255. goldfish_tty_current_line_count--;
  256. if (goldfish_tty_current_line_count == 0)
  257. goldfish_tty_delete_driver();
  258. err_create_driver_failed:
  259. mutex_unlock(&goldfish_tty_lock);
  260. err_unmap:
  261. iounmap(base);
  262. return ret;
  263. }
  264. static int goldfish_tty_remove(struct platform_device *pdev)
  265. {
  266. struct goldfish_tty *qtty;
  267. mutex_lock(&goldfish_tty_lock);
  268. qtty = &goldfish_ttys[pdev->id];
  269. unregister_console(&qtty->console);
  270. tty_unregister_device(goldfish_tty_driver, pdev->id);
  271. iounmap(qtty->base);
  272. qtty->base = NULL;
  273. free_irq(qtty->irq, pdev);
  274. goldfish_tty_current_line_count--;
  275. if (goldfish_tty_current_line_count == 0)
  276. goldfish_tty_delete_driver();
  277. mutex_unlock(&goldfish_tty_lock);
  278. return 0;
  279. }
  280. static struct platform_driver goldfish_tty_platform_driver = {
  281. .probe = goldfish_tty_probe,
  282. .remove = goldfish_tty_remove,
  283. .driver = {
  284. .name = "goldfish_tty"
  285. }
  286. };
  287. module_platform_driver(goldfish_tty_platform_driver);
  288. MODULE_LICENSE("GPL v2");