xilinx_ps2.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Xilinx XPS PS/2 device driver
  4. *
  5. * (c) 2005 MontaVista Software, Inc.
  6. * (c) 2008 Xilinx, Inc.
  7. */
  8. #include <linux/module.h>
  9. #include <linux/serio.h>
  10. #include <linux/interrupt.h>
  11. #include <linux/errno.h>
  12. #include <linux/slab.h>
  13. #include <linux/list.h>
  14. #include <linux/io.h>
  15. #include <linux/mod_devicetable.h>
  16. #include <linux/of_address.h>
  17. #include <linux/of_irq.h>
  18. #include <linux/platform_device.h>
  19. #define DRIVER_NAME "xilinx_ps2"
  20. /* Register offsets for the xps2 device */
  21. #define XPS2_SRST_OFFSET 0x00000000 /* Software Reset register */
  22. #define XPS2_STATUS_OFFSET 0x00000004 /* Status register */
  23. #define XPS2_RX_DATA_OFFSET 0x00000008 /* Receive Data register */
  24. #define XPS2_TX_DATA_OFFSET 0x0000000C /* Transmit Data register */
  25. #define XPS2_GIER_OFFSET 0x0000002C /* Global Interrupt Enable reg */
  26. #define XPS2_IPISR_OFFSET 0x00000030 /* Interrupt Status register */
  27. #define XPS2_IPIER_OFFSET 0x00000038 /* Interrupt Enable register */
  28. /* Reset Register Bit Definitions */
  29. #define XPS2_SRST_RESET 0x0000000A /* Software Reset */
  30. /* Status Register Bit Positions */
  31. #define XPS2_STATUS_RX_FULL 0x00000001 /* Receive Full */
  32. #define XPS2_STATUS_TX_FULL 0x00000002 /* Transmit Full */
  33. /*
  34. * Bit definitions for ISR/IER registers. Both the registers have the same bit
  35. * definitions and are only defined once.
  36. */
  37. #define XPS2_IPIXR_WDT_TOUT 0x00000001 /* Watchdog Timeout Interrupt */
  38. #define XPS2_IPIXR_TX_NOACK 0x00000002 /* Transmit No ACK Interrupt */
  39. #define XPS2_IPIXR_TX_ACK 0x00000004 /* Transmit ACK (Data) Interrupt */
  40. #define XPS2_IPIXR_RX_OVF 0x00000008 /* Receive Overflow Interrupt */
  41. #define XPS2_IPIXR_RX_ERR 0x00000010 /* Receive Error Interrupt */
  42. #define XPS2_IPIXR_RX_FULL 0x00000020 /* Receive Data Interrupt */
  43. /* Mask for all the Transmit Interrupts */
  44. #define XPS2_IPIXR_TX_ALL (XPS2_IPIXR_TX_NOACK | XPS2_IPIXR_TX_ACK)
  45. /* Mask for all the Receive Interrupts */
  46. #define XPS2_IPIXR_RX_ALL (XPS2_IPIXR_RX_OVF | XPS2_IPIXR_RX_ERR | \
  47. XPS2_IPIXR_RX_FULL)
  48. /* Mask for all the Interrupts */
  49. #define XPS2_IPIXR_ALL (XPS2_IPIXR_TX_ALL | XPS2_IPIXR_RX_ALL | \
  50. XPS2_IPIXR_WDT_TOUT)
  51. /* Global Interrupt Enable mask */
  52. #define XPS2_GIER_GIE_MASK 0x80000000
  53. struct xps2data {
  54. int irq;
  55. spinlock_t lock;
  56. void __iomem *base_address; /* virt. address of control registers */
  57. unsigned int flags;
  58. struct serio *serio; /* serio */
  59. struct device *dev;
  60. };
  61. /************************************/
  62. /* XPS PS/2 data transmission calls */
  63. /************************************/
  64. /**
  65. * xps2_recv() - attempts to receive a byte from the PS/2 port.
  66. * @drvdata: pointer to ps2 device private data structure
  67. * @byte: address where the read data will be copied
  68. *
  69. * If there is any data available in the PS/2 receiver, this functions reads
  70. * the data, otherwise it returns error.
  71. */
  72. static int xps2_recv(struct xps2data *drvdata, u8 *byte)
  73. {
  74. u32 sr;
  75. int status = -1;
  76. /* If there is data available in the PS/2 receiver, read it */
  77. sr = in_be32(drvdata->base_address + XPS2_STATUS_OFFSET);
  78. if (sr & XPS2_STATUS_RX_FULL) {
  79. *byte = in_be32(drvdata->base_address + XPS2_RX_DATA_OFFSET);
  80. status = 0;
  81. }
  82. return status;
  83. }
  84. /*********************/
  85. /* Interrupt handler */
  86. /*********************/
  87. static irqreturn_t xps2_interrupt(int irq, void *dev_id)
  88. {
  89. struct xps2data *drvdata = dev_id;
  90. u32 intr_sr;
  91. u8 c;
  92. int status;
  93. /* Get the PS/2 interrupts and clear them */
  94. intr_sr = in_be32(drvdata->base_address + XPS2_IPISR_OFFSET);
  95. out_be32(drvdata->base_address + XPS2_IPISR_OFFSET, intr_sr);
  96. /* Check which interrupt is active */
  97. if (intr_sr & XPS2_IPIXR_RX_OVF)
  98. dev_warn(drvdata->dev, "receive overrun error\n");
  99. if (intr_sr & XPS2_IPIXR_RX_ERR)
  100. drvdata->flags |= SERIO_PARITY;
  101. if (intr_sr & (XPS2_IPIXR_TX_NOACK | XPS2_IPIXR_WDT_TOUT))
  102. drvdata->flags |= SERIO_TIMEOUT;
  103. if (intr_sr & XPS2_IPIXR_RX_FULL) {
  104. status = xps2_recv(drvdata, &c);
  105. /* Error, if a byte is not received */
  106. if (status) {
  107. dev_err(drvdata->dev,
  108. "wrong rcvd byte count (%d)\n", status);
  109. } else {
  110. serio_interrupt(drvdata->serio, c, drvdata->flags);
  111. drvdata->flags = 0;
  112. }
  113. }
  114. return IRQ_HANDLED;
  115. }
  116. /*******************/
  117. /* serio callbacks */
  118. /*******************/
  119. /**
  120. * sxps2_write() - sends a byte out through the PS/2 port.
  121. * @pserio: pointer to the serio structure of the PS/2 port
  122. * @c: data that needs to be written to the PS/2 port
  123. *
  124. * This function checks if the PS/2 transmitter is empty and sends a byte.
  125. * Otherwise it returns error. Transmission fails only when nothing is connected
  126. * to the PS/2 port. Thats why, we do not try to resend the data in case of a
  127. * failure.
  128. */
  129. static int sxps2_write(struct serio *pserio, unsigned char c)
  130. {
  131. struct xps2data *drvdata = pserio->port_data;
  132. u32 sr;
  133. guard(spinlock_irqsave)(&drvdata->lock);
  134. /* If the PS/2 transmitter is empty send a byte of data */
  135. sr = in_be32(drvdata->base_address + XPS2_STATUS_OFFSET);
  136. if (sr & XPS2_STATUS_TX_FULL)
  137. return -EAGAIN;
  138. out_be32(drvdata->base_address + XPS2_TX_DATA_OFFSET, c);
  139. return 0;
  140. }
  141. /**
  142. * sxps2_open() - called when a port is opened by the higher layer.
  143. * @pserio: pointer to the serio structure of the PS/2 device
  144. *
  145. * This function requests irq and enables interrupts for the PS/2 device.
  146. */
  147. static int sxps2_open(struct serio *pserio)
  148. {
  149. struct xps2data *drvdata = pserio->port_data;
  150. int error;
  151. u8 c;
  152. error = request_irq(drvdata->irq, &xps2_interrupt, 0,
  153. DRIVER_NAME, drvdata);
  154. if (error) {
  155. dev_err(drvdata->dev,
  156. "Couldn't allocate interrupt %d\n", drvdata->irq);
  157. return error;
  158. }
  159. /* start reception by enabling the interrupts */
  160. out_be32(drvdata->base_address + XPS2_GIER_OFFSET, XPS2_GIER_GIE_MASK);
  161. out_be32(drvdata->base_address + XPS2_IPIER_OFFSET, XPS2_IPIXR_RX_ALL);
  162. (void)xps2_recv(drvdata, &c);
  163. return 0; /* success */
  164. }
  165. /**
  166. * sxps2_close() - frees the interrupt.
  167. * @pserio: pointer to the serio structure of the PS/2 device
  168. *
  169. * This function frees the irq and disables interrupts for the PS/2 device.
  170. */
  171. static void sxps2_close(struct serio *pserio)
  172. {
  173. struct xps2data *drvdata = pserio->port_data;
  174. /* Disable the PS2 interrupts */
  175. out_be32(drvdata->base_address + XPS2_GIER_OFFSET, 0x00);
  176. out_be32(drvdata->base_address + XPS2_IPIER_OFFSET, 0x00);
  177. free_irq(drvdata->irq, drvdata);
  178. }
  179. /**
  180. * xps2_of_probe - probe method for the PS/2 device.
  181. * @ofdev: pointer to OF device structure
  182. *
  183. * This function probes the PS/2 device in the device tree.
  184. * It initializes the driver data structure and the hardware.
  185. * It returns 0, if the driver is bound to the PS/2 device, or a negative
  186. * value if there is an error.
  187. */
  188. static int xps2_of_probe(struct platform_device *ofdev)
  189. {
  190. struct resource r_mem; /* IO mem resources */
  191. struct xps2data *drvdata;
  192. struct serio *serio;
  193. struct device *dev = &ofdev->dev;
  194. resource_size_t remap_size, phys_addr;
  195. unsigned int irq;
  196. int error;
  197. dev_info(dev, "Device Tree Probing \'%pOFn\'\n", dev->of_node);
  198. /* Get iospace for the device */
  199. error = of_address_to_resource(dev->of_node, 0, &r_mem);
  200. if (error) {
  201. dev_err(dev, "invalid address\n");
  202. return error;
  203. }
  204. /* Get IRQ for the device */
  205. irq = irq_of_parse_and_map(dev->of_node, 0);
  206. if (!irq) {
  207. dev_err(dev, "no IRQ found\n");
  208. return -ENODEV;
  209. }
  210. drvdata = kzalloc_obj(*drvdata);
  211. serio = kzalloc_obj(*serio);
  212. if (!drvdata || !serio) {
  213. error = -ENOMEM;
  214. goto failed1;
  215. }
  216. spin_lock_init(&drvdata->lock);
  217. drvdata->irq = irq;
  218. drvdata->serio = serio;
  219. drvdata->dev = dev;
  220. phys_addr = r_mem.start;
  221. remap_size = resource_size(&r_mem);
  222. if (!request_mem_region(phys_addr, remap_size, DRIVER_NAME)) {
  223. dev_err(dev, "Couldn't lock memory region at 0x%08llX\n",
  224. (unsigned long long)phys_addr);
  225. error = -EBUSY;
  226. goto failed1;
  227. }
  228. /* Fill in configuration data and add them to the list */
  229. drvdata->base_address = ioremap(phys_addr, remap_size);
  230. if (drvdata->base_address == NULL) {
  231. dev_err(dev, "Couldn't ioremap memory at 0x%08llX\n",
  232. (unsigned long long)phys_addr);
  233. error = -EFAULT;
  234. goto failed2;
  235. }
  236. /* Disable all the interrupts, just in case */
  237. out_be32(drvdata->base_address + XPS2_IPIER_OFFSET, 0);
  238. /*
  239. * Reset the PS2 device and abort any current transaction,
  240. * to make sure we have the PS2 in a good state.
  241. */
  242. out_be32(drvdata->base_address + XPS2_SRST_OFFSET, XPS2_SRST_RESET);
  243. dev_info(dev, "Xilinx PS2 at 0x%08llX mapped to 0x%p, irq=%d\n",
  244. (unsigned long long)phys_addr, drvdata->base_address,
  245. drvdata->irq);
  246. serio->id.type = SERIO_8042;
  247. serio->write = sxps2_write;
  248. serio->open = sxps2_open;
  249. serio->close = sxps2_close;
  250. serio->port_data = drvdata;
  251. serio->dev.parent = dev;
  252. snprintf(serio->name, sizeof(serio->name),
  253. "Xilinx XPS PS/2 at %08llX", (unsigned long long)phys_addr);
  254. snprintf(serio->phys, sizeof(serio->phys),
  255. "xilinxps2/serio at %08llX", (unsigned long long)phys_addr);
  256. serio_register_port(serio);
  257. platform_set_drvdata(ofdev, drvdata);
  258. return 0; /* success */
  259. failed2:
  260. release_mem_region(phys_addr, remap_size);
  261. failed1:
  262. kfree(serio);
  263. kfree(drvdata);
  264. return error;
  265. }
  266. /**
  267. * xps2_of_remove - unbinds the driver from the PS/2 device.
  268. * @of_dev: pointer to OF device structure
  269. *
  270. * This function is called if a device is physically removed from the system or
  271. * if the driver module is being unloaded. It frees any resources allocated to
  272. * the device.
  273. */
  274. static void xps2_of_remove(struct platform_device *of_dev)
  275. {
  276. struct xps2data *drvdata = platform_get_drvdata(of_dev);
  277. struct resource r_mem; /* IO mem resources */
  278. serio_unregister_port(drvdata->serio);
  279. iounmap(drvdata->base_address);
  280. /* Get iospace of the device */
  281. if (of_address_to_resource(of_dev->dev.of_node, 0, &r_mem))
  282. dev_err(drvdata->dev, "invalid address\n");
  283. else
  284. release_mem_region(r_mem.start, resource_size(&r_mem));
  285. kfree(drvdata);
  286. }
  287. /* Match table for of_platform binding */
  288. static const struct of_device_id xps2_of_match[] = {
  289. { .compatible = "xlnx,xps-ps2-1.00.a", },
  290. { /* end of list */ },
  291. };
  292. MODULE_DEVICE_TABLE(of, xps2_of_match);
  293. static struct platform_driver xps2_of_driver = {
  294. .driver = {
  295. .name = DRIVER_NAME,
  296. .of_match_table = xps2_of_match,
  297. },
  298. .probe = xps2_of_probe,
  299. .remove = xps2_of_remove,
  300. };
  301. module_platform_driver(xps2_of_driver);
  302. MODULE_AUTHOR("Xilinx, Inc.");
  303. MODULE_DESCRIPTION("Xilinx XPS PS/2 driver");
  304. MODULE_LICENSE("GPL");