goldfish.c 8.5 KB

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