sa1111ps2.c 8.2 KB

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