goldfish.c 8.5 KB

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