sa1111ps2.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. /*
  2. * linux/drivers/input/serio/sa1111ps2.c
  3. *
  4. * Copyright (C) 2002 Russell King
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License.
  9. */
  10. #include <linux/module.h>
  11. #include <linux/init.h>
  12. #include <linux/input.h>
  13. #include <linux/serio.h>
  14. #include <linux/errno.h>
  15. #include <linux/interrupt.h>
  16. #include <linux/ioport.h>
  17. #include <linux/delay.h>
  18. #include <linux/device.h>
  19. #include <linux/slab.h>
  20. #include <linux/spinlock.h>
  21. #include <asm/io.h>
  22. #include <asm/irq.h>
  23. #include <asm/system.h>
  24. #include <asm/hardware/sa1111.h>
  25. struct ps2if {
  26. struct serio *io;
  27. struct sa1111_dev *dev;
  28. void __iomem *base;
  29. unsigned int open;
  30. spinlock_t lock;
  31. unsigned int head;
  32. unsigned int tail;
  33. unsigned char buf[4];
  34. };
  35. /*
  36. * Read all bytes waiting in the PS2 port. There should be
  37. * at the most one, but we loop for safety. If there was a
  38. * framing error, we have to manually clear the status.
  39. */
  40. static irqreturn_t ps2_rxint(int irq, void *dev_id, struct pt_regs *regs)
  41. {
  42. struct ps2if *ps2if = dev_id;
  43. unsigned int scancode, flag, status;
  44. status = sa1111_readl(ps2if->base + SA1111_PS2STAT);
  45. while (status & PS2STAT_RXF) {
  46. if (status & PS2STAT_STP)
  47. sa1111_writel(PS2STAT_STP, ps2if->base + SA1111_PS2STAT);
  48. flag = (status & PS2STAT_STP ? SERIO_FRAME : 0) |
  49. (status & PS2STAT_RXP ? 0 : SERIO_PARITY);
  50. scancode = sa1111_readl(ps2if->base + SA1111_PS2DATA) & 0xff;
  51. if (hweight8(scancode) & 1)
  52. flag ^= SERIO_PARITY;
  53. serio_interrupt(ps2if->io, scancode, flag, regs);
  54. status = sa1111_readl(ps2if->base + SA1111_PS2STAT);
  55. }
  56. return IRQ_HANDLED;
  57. }
  58. /*
  59. * Completion of ps2 write
  60. */
  61. static irqreturn_t ps2_txint(int irq, void *dev_id, struct pt_regs *regs)
  62. {
  63. struct ps2if *ps2if = dev_id;
  64. unsigned int status;
  65. spin_lock(&ps2if->lock);
  66. status = sa1111_readl(ps2if->base + SA1111_PS2STAT);
  67. if (ps2if->head == ps2if->tail) {
  68. disable_irq(irq);
  69. /* done */
  70. } else if (status & PS2STAT_TXE) {
  71. sa1111_writel(ps2if->buf[ps2if->tail], ps2if->base + SA1111_PS2DATA);
  72. ps2if->tail = (ps2if->tail + 1) & (sizeof(ps2if->buf) - 1);
  73. }
  74. spin_unlock(&ps2if->lock);
  75. return IRQ_HANDLED;
  76. }
  77. /*
  78. * Write a byte to the PS2 port. We have to wait for the
  79. * port to indicate that the transmitter is empty.
  80. */
  81. static int ps2_write(struct serio *io, unsigned char val)
  82. {
  83. struct ps2if *ps2if = io->port_data;
  84. unsigned long flags;
  85. unsigned int head;
  86. spin_lock_irqsave(&ps2if->lock, flags);
  87. /*
  88. * If the TX register is empty, we can go straight out.
  89. */
  90. if (sa1111_readl(ps2if->base + SA1111_PS2STAT) & PS2STAT_TXE) {
  91. sa1111_writel(val, ps2if->base + SA1111_PS2DATA);
  92. } else {
  93. if (ps2if->head == ps2if->tail)
  94. enable_irq(ps2if->dev->irq[1]);
  95. head = (ps2if->head + 1) & (sizeof(ps2if->buf) - 1);
  96. if (head != ps2if->tail) {
  97. ps2if->buf[ps2if->head] = val;
  98. ps2if->head = head;
  99. }
  100. }
  101. spin_unlock_irqrestore(&ps2if->lock, flags);
  102. return 0;
  103. }
  104. static int ps2_open(struct serio *io)
  105. {
  106. struct ps2if *ps2if = io->port_data;
  107. int ret;
  108. sa1111_enable_device(ps2if->dev);
  109. ret = request_irq(ps2if->dev->irq[0], ps2_rxint, 0,
  110. SA1111_DRIVER_NAME(ps2if->dev), ps2if);
  111. if (ret) {
  112. printk(KERN_ERR "sa1111ps2: could not allocate IRQ%d: %d\n",
  113. ps2if->dev->irq[0], ret);
  114. return ret;
  115. }
  116. ret = request_irq(ps2if->dev->irq[1], ps2_txint, 0,
  117. SA1111_DRIVER_NAME(ps2if->dev), ps2if);
  118. if (ret) {
  119. printk(KERN_ERR "sa1111ps2: could not allocate IRQ%d: %d\n",
  120. ps2if->dev->irq[1], ret);
  121. free_irq(ps2if->dev->irq[0], ps2if);
  122. return ret;
  123. }
  124. ps2if->open = 1;
  125. enable_irq_wake(ps2if->dev->irq[0]);
  126. sa1111_writel(PS2CR_ENA, ps2if->base + SA1111_PS2CR);
  127. return 0;
  128. }
  129. static void ps2_close(struct serio *io)
  130. {
  131. struct ps2if *ps2if = io->port_data;
  132. sa1111_writel(0, ps2if->base + SA1111_PS2CR);
  133. disable_irq_wake(ps2if->dev->irq[0]);
  134. ps2if->open = 0;
  135. free_irq(ps2if->dev->irq[1], ps2if);
  136. free_irq(ps2if->dev->irq[0], ps2if);
  137. sa1111_disable_device(ps2if->dev);
  138. }
  139. /*
  140. * Clear the input buffer.
  141. */
  142. static void __init ps2_clear_input(struct ps2if *ps2if)
  143. {
  144. int maxread = 100;
  145. while (maxread--) {
  146. if ((sa1111_readl(ps2if->base + SA1111_PS2DATA) & 0xff) == 0xff)
  147. break;
  148. }
  149. }
  150. static inline unsigned int
  151. ps2_test_one(struct ps2if *ps2if, unsigned int mask)
  152. {
  153. unsigned int val;
  154. sa1111_writel(PS2CR_ENA | mask, ps2if->base + SA1111_PS2CR);
  155. udelay(2);
  156. val = sa1111_readl(ps2if->base + SA1111_PS2STAT);
  157. return val & (PS2STAT_KBC | PS2STAT_KBD);
  158. }
  159. /*
  160. * Test the keyboard interface. We basically check to make sure that
  161. * we can drive each line to the keyboard independently of each other.
  162. */
  163. static int __init ps2_test(struct ps2if *ps2if)
  164. {
  165. unsigned int stat;
  166. int ret = 0;
  167. stat = ps2_test_one(ps2if, PS2CR_FKC);
  168. if (stat != PS2STAT_KBD) {
  169. printk("PS/2 interface test failed[1]: %02x\n", stat);
  170. ret = -ENODEV;
  171. }
  172. stat = ps2_test_one(ps2if, 0);
  173. if (stat != (PS2STAT_KBC | PS2STAT_KBD)) {
  174. printk("PS/2 interface test failed[2]: %02x\n", stat);
  175. ret = -ENODEV;
  176. }
  177. stat = ps2_test_one(ps2if, PS2CR_FKD);
  178. if (stat != PS2STAT_KBC) {
  179. printk("PS/2 interface test failed[3]: %02x\n", stat);
  180. ret = -ENODEV;
  181. }
  182. sa1111_writel(0, ps2if->base + SA1111_PS2CR);
  183. return ret;
  184. }
  185. /*
  186. * Add one device to this driver.
  187. */
  188. static int ps2_probe(struct sa1111_dev *dev)
  189. {
  190. struct ps2if *ps2if;
  191. struct serio *serio;
  192. int ret;
  193. ps2if = kmalloc(sizeof(struct ps2if), GFP_KERNEL);
  194. serio = kmalloc(sizeof(struct serio), GFP_KERNEL);
  195. if (!ps2if || !serio) {
  196. ret = -ENOMEM;
  197. goto free;
  198. }
  199. memset(ps2if, 0, sizeof(struct ps2if));
  200. memset(serio, 0, sizeof(struct serio));
  201. serio->id.type = SERIO_8042;
  202. serio->write = ps2_write;
  203. serio->open = ps2_open;
  204. serio->close = ps2_close;
  205. strlcpy(serio->name, dev->dev.bus_id, sizeof(serio->name));
  206. strlcpy(serio->phys, dev->dev.bus_id, sizeof(serio->phys));
  207. serio->port_data = ps2if;
  208. serio->dev.parent = &dev->dev;
  209. ps2if->io = serio;
  210. ps2if->dev = dev;
  211. sa1111_set_drvdata(dev, ps2if);
  212. spin_lock_init(&ps2if->lock);
  213. /*
  214. * Request the physical region for this PS2 port.
  215. */
  216. if (!request_mem_region(dev->res.start,
  217. dev->res.end - dev->res.start + 1,
  218. SA1111_DRIVER_NAME(dev))) {
  219. ret = -EBUSY;
  220. goto free;
  221. }
  222. /*
  223. * Our parent device has already mapped the region.
  224. */
  225. ps2if->base = dev->mapbase;
  226. sa1111_enable_device(ps2if->dev);
  227. /* Incoming clock is 8MHz */
  228. sa1111_writel(0, ps2if->base + SA1111_PS2CLKDIV);
  229. sa1111_writel(127, ps2if->base + SA1111_PS2PRECNT);
  230. /*
  231. * Flush any pending input.
  232. */
  233. ps2_clear_input(ps2if);
  234. /*
  235. * Test the keyboard interface.
  236. */
  237. ret = ps2_test(ps2if);
  238. if (ret)
  239. goto out;
  240. /*
  241. * Flush any pending input.
  242. */
  243. ps2_clear_input(ps2if);
  244. sa1111_disable_device(ps2if->dev);
  245. serio_register_port(ps2if->io);
  246. return 0;
  247. out:
  248. sa1111_disable_device(ps2if->dev);
  249. release_mem_region(dev->res.start,
  250. dev->res.end - dev->res.start + 1);
  251. free:
  252. sa1111_set_drvdata(dev, NULL);
  253. kfree(ps2if);
  254. kfree(serio);
  255. return ret;
  256. }
  257. /*
  258. * Remove one device from this driver.
  259. */
  260. static int ps2_remove(struct sa1111_dev *dev)
  261. {
  262. struct ps2if *ps2if = sa1111_get_drvdata(dev);
  263. serio_unregister_port(ps2if->io);
  264. release_mem_region(dev->res.start,
  265. dev->res.end - dev->res.start + 1);
  266. sa1111_set_drvdata(dev, NULL);
  267. kfree(ps2if);
  268. return 0;
  269. }
  270. /*
  271. * Our device driver structure
  272. */
  273. static struct sa1111_driver ps2_driver = {
  274. .drv = {
  275. .name = "sa1111-ps2",
  276. },
  277. .devid = SA1111_DEVID_PS2,
  278. .probe = ps2_probe,
  279. .remove = ps2_remove,
  280. };
  281. static int __init ps2_init(void)
  282. {
  283. return sa1111_driver_register(&ps2_driver);
  284. }
  285. static void __exit ps2_exit(void)
  286. {
  287. sa1111_driver_unregister(&ps2_driver);
  288. }
  289. module_init(ps2_init);
  290. module_exit(ps2_exit);
  291. MODULE_AUTHOR("Russell King <rmk@arm.linux.org.uk>");
  292. MODULE_DESCRIPTION("SA1111 PS2 controller driver");
  293. MODULE_LICENSE("GPL");