sa1111ps2.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  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. sa1111_enable_device(ps2if->dev);
  108. ret = request_irq(ps2if->dev->irq[0], ps2_rxint, 0,
  109. SA1111_DRIVER_NAME(ps2if->dev), ps2if);
  110. if (ret) {
  111. printk(KERN_ERR "sa1111ps2: could not allocate IRQ%d: %d\n",
  112. ps2if->dev->irq[0], ret);
  113. return ret;
  114. }
  115. ret = request_irq(ps2if->dev->irq[1], ps2_txint, 0,
  116. SA1111_DRIVER_NAME(ps2if->dev), ps2if);
  117. if (ret) {
  118. printk(KERN_ERR "sa1111ps2: could not allocate IRQ%d: %d\n",
  119. ps2if->dev->irq[1], ret);
  120. free_irq(ps2if->dev->irq[0], ps2if);
  121. return ret;
  122. }
  123. ps2if->open = 1;
  124. enable_irq_wake(ps2if->dev->irq[0]);
  125. sa1111_writel(PS2CR_ENA, ps2if->base + SA1111_PS2CR);
  126. return 0;
  127. }
  128. static void ps2_close(struct serio *io)
  129. {
  130. struct ps2if *ps2if = io->port_data;
  131. sa1111_writel(0, ps2if->base + SA1111_PS2CR);
  132. disable_irq_wake(ps2if->dev->irq[0]);
  133. ps2if->open = 0;
  134. free_irq(ps2if->dev->irq[1], ps2if);
  135. free_irq(ps2if->dev->irq[0], ps2if);
  136. sa1111_disable_device(ps2if->dev);
  137. }
  138. /*
  139. * Clear the input buffer.
  140. */
  141. static void __devinit ps2_clear_input(struct ps2if *ps2if)
  142. {
  143. int maxread = 100;
  144. while (maxread--) {
  145. if ((sa1111_readl(ps2if->base + SA1111_PS2DATA) & 0xff) == 0xff)
  146. break;
  147. }
  148. }
  149. static unsigned int __devinit ps2_test_one(struct ps2if *ps2if,
  150. unsigned int mask)
  151. {
  152. unsigned int val;
  153. sa1111_writel(PS2CR_ENA | mask, ps2if->base + SA1111_PS2CR);
  154. udelay(2);
  155. val = sa1111_readl(ps2if->base + SA1111_PS2STAT);
  156. return val & (PS2STAT_KBC | PS2STAT_KBD);
  157. }
  158. /*
  159. * Test the keyboard interface. We basically check to make sure that
  160. * we can drive each line to the keyboard independently of each other.
  161. */
  162. static int __devinit ps2_test(struct ps2if *ps2if)
  163. {
  164. unsigned int stat;
  165. int ret = 0;
  166. stat = ps2_test_one(ps2if, PS2CR_FKC);
  167. if (stat != PS2STAT_KBD) {
  168. printk("PS/2 interface test failed[1]: %02x\n", stat);
  169. ret = -ENODEV;
  170. }
  171. stat = ps2_test_one(ps2if, 0);
  172. if (stat != (PS2STAT_KBC | PS2STAT_KBD)) {
  173. printk("PS/2 interface test failed[2]: %02x\n", stat);
  174. ret = -ENODEV;
  175. }
  176. stat = ps2_test_one(ps2if, PS2CR_FKD);
  177. if (stat != PS2STAT_KBC) {
  178. printk("PS/2 interface test failed[3]: %02x\n", stat);
  179. ret = -ENODEV;
  180. }
  181. sa1111_writel(0, ps2if->base + SA1111_PS2CR);
  182. return ret;
  183. }
  184. /*
  185. * Add one device to this driver.
  186. */
  187. static int __devinit ps2_probe(struct sa1111_dev *dev)
  188. {
  189. struct ps2if *ps2if;
  190. struct serio *serio;
  191. int ret;
  192. ps2if = kzalloc(sizeof(struct ps2if), GFP_KERNEL);
  193. serio = kzalloc(sizeof(struct serio), GFP_KERNEL);
  194. if (!ps2if || !serio) {
  195. ret = -ENOMEM;
  196. goto free;
  197. }
  198. serio->id.type = SERIO_8042;
  199. serio->write = ps2_write;
  200. serio->open = ps2_open;
  201. serio->close = ps2_close;
  202. strlcpy(serio->name, dev_name(&dev->dev), sizeof(serio->name));
  203. strlcpy(serio->phys, dev_name(&dev->dev), sizeof(serio->phys));
  204. serio->port_data = ps2if;
  205. serio->dev.parent = &dev->dev;
  206. ps2if->io = serio;
  207. ps2if->dev = dev;
  208. sa1111_set_drvdata(dev, ps2if);
  209. spin_lock_init(&ps2if->lock);
  210. /*
  211. * Request the physical region for this PS2 port.
  212. */
  213. if (!request_mem_region(dev->res.start,
  214. dev->res.end - dev->res.start + 1,
  215. SA1111_DRIVER_NAME(dev))) {
  216. ret = -EBUSY;
  217. goto free;
  218. }
  219. /*
  220. * Our parent device has already mapped the region.
  221. */
  222. ps2if->base = dev->mapbase;
  223. sa1111_enable_device(ps2if->dev);
  224. /* Incoming clock is 8MHz */
  225. sa1111_writel(0, ps2if->base + SA1111_PS2CLKDIV);
  226. sa1111_writel(127, ps2if->base + SA1111_PS2PRECNT);
  227. /*
  228. * Flush any pending input.
  229. */
  230. ps2_clear_input(ps2if);
  231. /*
  232. * Test the keyboard interface.
  233. */
  234. ret = ps2_test(ps2if);
  235. if (ret)
  236. goto out;
  237. /*
  238. * Flush any pending input.
  239. */
  240. ps2_clear_input(ps2if);
  241. sa1111_disable_device(ps2if->dev);
  242. serio_register_port(ps2if->io);
  243. return 0;
  244. out:
  245. sa1111_disable_device(ps2if->dev);
  246. release_mem_region(dev->res.start,
  247. dev->res.end - dev->res.start + 1);
  248. free:
  249. sa1111_set_drvdata(dev, NULL);
  250. kfree(ps2if);
  251. kfree(serio);
  252. return ret;
  253. }
  254. /*
  255. * Remove one device from this driver.
  256. */
  257. static int __devexit ps2_remove(struct sa1111_dev *dev)
  258. {
  259. struct ps2if *ps2if = sa1111_get_drvdata(dev);
  260. serio_unregister_port(ps2if->io);
  261. release_mem_region(dev->res.start,
  262. dev->res.end - dev->res.start + 1);
  263. sa1111_set_drvdata(dev, NULL);
  264. kfree(ps2if);
  265. return 0;
  266. }
  267. /*
  268. * Our device driver structure
  269. */
  270. static struct sa1111_driver ps2_driver = {
  271. .drv = {
  272. .name = "sa1111-ps2",
  273. },
  274. .devid = SA1111_DEVID_PS2,
  275. .probe = ps2_probe,
  276. .remove = __devexit_p(ps2_remove),
  277. };
  278. static int __init ps2_init(void)
  279. {
  280. return sa1111_driver_register(&ps2_driver);
  281. }
  282. static void __exit ps2_exit(void)
  283. {
  284. sa1111_driver_unregister(&ps2_driver);
  285. }
  286. module_init(ps2_init);
  287. module_exit(ps2_exit);
  288. MODULE_AUTHOR("Russell King <rmk@arm.linux.org.uk>");
  289. MODULE_DESCRIPTION("SA1111 PS2 controller driver");
  290. MODULE_LICENSE("GPL");