sa1111ps2.c 8.0 KB

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