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/system.h>
  23. #include <asm/hardware/sa1111.h>
  24. struct ps2if {
  25. struct serio *io;
  26. struct sa1111_dev *dev;
  27. void __iomem *base;
  28. unsigned int open;
  29. spinlock_t lock;
  30. unsigned int head;
  31. unsigned int tail;
  32. unsigned char buf[4];
  33. };
  34. /*
  35. * Read all bytes waiting in the PS2 port. There should be
  36. * at the most one, but we loop for safety. If there was a
  37. * framing error, we have to manually clear the status.
  38. */
  39. static irqreturn_t ps2_rxint(int irq, void *dev_id)
  40. {
  41. struct ps2if *ps2if = dev_id;
  42. unsigned int scancode, flag, status;
  43. status = sa1111_readl(ps2if->base + SA1111_PS2STAT);
  44. while (status & PS2STAT_RXF) {
  45. if (status & PS2STAT_STP)
  46. sa1111_writel(PS2STAT_STP, ps2if->base + SA1111_PS2STAT);
  47. flag = (status & PS2STAT_STP ? SERIO_FRAME : 0) |
  48. (status & PS2STAT_RXP ? 0 : SERIO_PARITY);
  49. scancode = sa1111_readl(ps2if->base + SA1111_PS2DATA) & 0xff;
  50. if (hweight8(scancode) & 1)
  51. flag ^= SERIO_PARITY;
  52. serio_interrupt(ps2if->io, scancode, flag);
  53. status = sa1111_readl(ps2if->base + SA1111_PS2STAT);
  54. }
  55. return IRQ_HANDLED;
  56. }
  57. /*
  58. * Completion of ps2 write
  59. */
  60. static irqreturn_t ps2_txint(int irq, void *dev_id)
  61. {
  62. struct ps2if *ps2if = dev_id;
  63. unsigned int status;
  64. spin_lock(&ps2if->lock);
  65. status = sa1111_readl(ps2if->base + SA1111_PS2STAT);
  66. if (ps2if->head == ps2if->tail) {
  67. disable_irq_nosync(irq);
  68. /* done */
  69. } else if (status & PS2STAT_TXE) {
  70. sa1111_writel(ps2if->buf[ps2if->tail], ps2if->base + SA1111_PS2DATA);
  71. ps2if->tail = (ps2if->tail + 1) & (sizeof(ps2if->buf) - 1);
  72. }
  73. spin_unlock(&ps2if->lock);
  74. return IRQ_HANDLED;
  75. }
  76. /*
  77. * Write a byte to the PS2 port. We have to wait for the
  78. * port to indicate that the transmitter is empty.
  79. */
  80. static int ps2_write(struct serio *io, unsigned char val)
  81. {
  82. struct ps2if *ps2if = io->port_data;
  83. unsigned long flags;
  84. unsigned int head;
  85. spin_lock_irqsave(&ps2if->lock, flags);
  86. /*
  87. * If the TX register is empty, we can go straight out.
  88. */
  89. if (sa1111_readl(ps2if->base + SA1111_PS2STAT) & PS2STAT_TXE) {
  90. sa1111_writel(val, ps2if->base + SA1111_PS2DATA);
  91. } else {
  92. if (ps2if->head == ps2if->tail)
  93. enable_irq(ps2if->dev->irq[1]);
  94. head = (ps2if->head + 1) & (sizeof(ps2if->buf) - 1);
  95. if (head != ps2if->tail) {
  96. ps2if->buf[ps2if->head] = val;
  97. ps2if->head = head;
  98. }
  99. }
  100. spin_unlock_irqrestore(&ps2if->lock, flags);
  101. return 0;
  102. }
  103. static int ps2_open(struct serio *io)
  104. {
  105. struct ps2if *ps2if = io->port_data;
  106. int ret;
  107. ret = sa1111_enable_device(ps2if->dev);
  108. if (ret)
  109. return ret;
  110. ret = request_irq(ps2if->dev->irq[0], ps2_rxint, 0,
  111. SA1111_DRIVER_NAME(ps2if->dev), ps2if);
  112. if (ret) {
  113. printk(KERN_ERR "sa1111ps2: could not allocate IRQ%d: %d\n",
  114. ps2if->dev->irq[0], ret);
  115. sa1111_disable_device(ps2if->dev);
  116. return ret;
  117. }
  118. ret = request_irq(ps2if->dev->irq[1], ps2_txint, 0,
  119. SA1111_DRIVER_NAME(ps2if->dev), ps2if);
  120. if (ret) {
  121. printk(KERN_ERR "sa1111ps2: could not allocate IRQ%d: %d\n",
  122. ps2if->dev->irq[1], ret);
  123. free_irq(ps2if->dev->irq[0], ps2if);
  124. sa1111_disable_device(ps2if->dev);
  125. return ret;
  126. }
  127. ps2if->open = 1;
  128. enable_irq_wake(ps2if->dev->irq[0]);
  129. sa1111_writel(PS2CR_ENA, ps2if->base + SA1111_PS2CR);
  130. return 0;
  131. }
  132. static void ps2_close(struct serio *io)
  133. {
  134. struct ps2if *ps2if = io->port_data;
  135. sa1111_writel(0, ps2if->base + SA1111_PS2CR);
  136. disable_irq_wake(ps2if->dev->irq[0]);
  137. ps2if->open = 0;
  138. free_irq(ps2if->dev->irq[1], ps2if);
  139. free_irq(ps2if->dev->irq[0], ps2if);
  140. sa1111_disable_device(ps2if->dev);
  141. }
  142. /*
  143. * Clear the input buffer.
  144. */
  145. static void __devinit ps2_clear_input(struct ps2if *ps2if)
  146. {
  147. int maxread = 100;
  148. while (maxread--) {
  149. if ((sa1111_readl(ps2if->base + SA1111_PS2DATA) & 0xff) == 0xff)
  150. break;
  151. }
  152. }
  153. static unsigned int __devinit ps2_test_one(struct ps2if *ps2if,
  154. unsigned int mask)
  155. {
  156. unsigned int val;
  157. sa1111_writel(PS2CR_ENA | mask, ps2if->base + SA1111_PS2CR);
  158. udelay(2);
  159. val = sa1111_readl(ps2if->base + SA1111_PS2STAT);
  160. return val & (PS2STAT_KBC | PS2STAT_KBD);
  161. }
  162. /*
  163. * Test the keyboard interface. We basically check to make sure that
  164. * we can drive each line to the keyboard independently of each other.
  165. */
  166. static int __devinit ps2_test(struct ps2if *ps2if)
  167. {
  168. unsigned int stat;
  169. int ret = 0;
  170. stat = ps2_test_one(ps2if, PS2CR_FKC);
  171. if (stat != PS2STAT_KBD) {
  172. printk("PS/2 interface test failed[1]: %02x\n", stat);
  173. ret = -ENODEV;
  174. }
  175. stat = ps2_test_one(ps2if, 0);
  176. if (stat != (PS2STAT_KBC | PS2STAT_KBD)) {
  177. printk("PS/2 interface test failed[2]: %02x\n", stat);
  178. ret = -ENODEV;
  179. }
  180. stat = ps2_test_one(ps2if, PS2CR_FKD);
  181. if (stat != PS2STAT_KBC) {
  182. printk("PS/2 interface test failed[3]: %02x\n", stat);
  183. ret = -ENODEV;
  184. }
  185. sa1111_writel(0, ps2if->base + SA1111_PS2CR);
  186. return ret;
  187. }
  188. /*
  189. * Add one device to this driver.
  190. */
  191. static int __devinit ps2_probe(struct sa1111_dev *dev)
  192. {
  193. struct ps2if *ps2if;
  194. struct serio *serio;
  195. int ret;
  196. ps2if = kzalloc(sizeof(struct ps2if), GFP_KERNEL);
  197. serio = kzalloc(sizeof(struct serio), GFP_KERNEL);
  198. if (!ps2if || !serio) {
  199. ret = -ENOMEM;
  200. goto free;
  201. }
  202. serio->id.type = SERIO_8042;
  203. serio->write = ps2_write;
  204. serio->open = ps2_open;
  205. serio->close = ps2_close;
  206. strlcpy(serio->name, dev_name(&dev->dev), sizeof(serio->name));
  207. strlcpy(serio->phys, dev_name(&dev->dev), sizeof(serio->phys));
  208. serio->port_data = ps2if;
  209. serio->dev.parent = &dev->dev;
  210. ps2if->io = serio;
  211. ps2if->dev = dev;
  212. sa1111_set_drvdata(dev, ps2if);
  213. spin_lock_init(&ps2if->lock);
  214. /*
  215. * Request the physical region for this PS2 port.
  216. */
  217. if (!request_mem_region(dev->res.start,
  218. dev->res.end - dev->res.start + 1,
  219. SA1111_DRIVER_NAME(dev))) {
  220. ret = -EBUSY;
  221. goto free;
  222. }
  223. /*
  224. * Our parent device has already mapped the region.
  225. */
  226. ps2if->base = dev->mapbase;
  227. sa1111_enable_device(ps2if->dev);
  228. /* Incoming clock is 8MHz */
  229. sa1111_writel(0, ps2if->base + SA1111_PS2CLKDIV);
  230. sa1111_writel(127, ps2if->base + SA1111_PS2PRECNT);
  231. /*
  232. * Flush any pending input.
  233. */
  234. ps2_clear_input(ps2if);
  235. /*
  236. * Test the keyboard interface.
  237. */
  238. ret = ps2_test(ps2if);
  239. if (ret)
  240. goto out;
  241. /*
  242. * Flush any pending input.
  243. */
  244. ps2_clear_input(ps2if);
  245. sa1111_disable_device(ps2if->dev);
  246. serio_register_port(ps2if->io);
  247. return 0;
  248. out:
  249. sa1111_disable_device(ps2if->dev);
  250. release_mem_region(dev->res.start, resource_size(&dev->res));
  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 __devexit 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, resource_size(&dev->res));
  265. sa1111_set_drvdata(dev, NULL);
  266. kfree(ps2if);
  267. return 0;
  268. }
  269. /*
  270. * Our device driver structure
  271. */
  272. static struct sa1111_driver ps2_driver = {
  273. .drv = {
  274. .name = "sa1111-ps2",
  275. .owner = THIS_MODULE,
  276. },
  277. .devid = SA1111_DEVID_PS2,
  278. .probe = ps2_probe,
  279. .remove = __devexit_p(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");