sa1111ps2.c 8.1 KB

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