sa1111ps2.c 8.1 KB

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