xilinx_ps2.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  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/of_address.h>
  16. #include <linux/of_device.h>
  17. #include <linux/of_irq.h>
  18. #include <linux/of_platform.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. unsigned long flags;
  133. u32 sr;
  134. int status = -1;
  135. spin_lock_irqsave(&drvdata->lock, flags);
  136. /* If the PS/2 transmitter is empty send a byte of data */
  137. sr = in_be32(drvdata->base_address + XPS2_STATUS_OFFSET);
  138. if (!(sr & XPS2_STATUS_TX_FULL)) {
  139. out_be32(drvdata->base_address + XPS2_TX_DATA_OFFSET, c);
  140. status = 0;
  141. }
  142. spin_unlock_irqrestore(&drvdata->lock, flags);
  143. return status;
  144. }
  145. /**
  146. * sxps2_open() - called when a port is opened by the higher layer.
  147. * @pserio: pointer to the serio structure of the PS/2 device
  148. *
  149. * This function requests irq and enables interrupts for the PS/2 device.
  150. */
  151. static int sxps2_open(struct serio *pserio)
  152. {
  153. struct xps2data *drvdata = pserio->port_data;
  154. int error;
  155. u8 c;
  156. error = request_irq(drvdata->irq, &xps2_interrupt, 0,
  157. DRIVER_NAME, drvdata);
  158. if (error) {
  159. dev_err(drvdata->dev,
  160. "Couldn't allocate interrupt %d\n", drvdata->irq);
  161. return error;
  162. }
  163. /* start reception by enabling the interrupts */
  164. out_be32(drvdata->base_address + XPS2_GIER_OFFSET, XPS2_GIER_GIE_MASK);
  165. out_be32(drvdata->base_address + XPS2_IPIER_OFFSET, XPS2_IPIXR_RX_ALL);
  166. (void)xps2_recv(drvdata, &c);
  167. return 0; /* success */
  168. }
  169. /**
  170. * sxps2_close() - frees the interrupt.
  171. * @pserio: pointer to the serio structure of the PS/2 device
  172. *
  173. * This function frees the irq and disables interrupts for the PS/2 device.
  174. */
  175. static void sxps2_close(struct serio *pserio)
  176. {
  177. struct xps2data *drvdata = pserio->port_data;
  178. /* Disable the PS2 interrupts */
  179. out_be32(drvdata->base_address + XPS2_GIER_OFFSET, 0x00);
  180. out_be32(drvdata->base_address + XPS2_IPIER_OFFSET, 0x00);
  181. free_irq(drvdata->irq, drvdata);
  182. }
  183. /**
  184. * xps2_of_probe - probe method for the PS/2 device.
  185. * @of_dev: pointer to OF device structure
  186. * @match: pointer to the structure used for matching a device
  187. *
  188. * This function probes the PS/2 device in the device tree.
  189. * It initializes the driver data structure and the hardware.
  190. * It returns 0, if the driver is bound to the PS/2 device, or a negative
  191. * value if there is an error.
  192. */
  193. static int xps2_of_probe(struct platform_device *ofdev)
  194. {
  195. struct resource r_mem; /* IO mem resources */
  196. struct xps2data *drvdata;
  197. struct serio *serio;
  198. struct device *dev = &ofdev->dev;
  199. resource_size_t remap_size, phys_addr;
  200. unsigned int irq;
  201. int error;
  202. dev_info(dev, "Device Tree Probing \'%pOFn\'\n", dev->of_node);
  203. /* Get iospace for the device */
  204. error = of_address_to_resource(dev->of_node, 0, &r_mem);
  205. if (error) {
  206. dev_err(dev, "invalid address\n");
  207. return error;
  208. }
  209. /* Get IRQ for the device */
  210. irq = irq_of_parse_and_map(dev->of_node, 0);
  211. if (!irq) {
  212. dev_err(dev, "no IRQ found\n");
  213. return -ENODEV;
  214. }
  215. drvdata = kzalloc(sizeof(struct xps2data), GFP_KERNEL);
  216. serio = kzalloc(sizeof(struct serio), GFP_KERNEL);
  217. if (!drvdata || !serio) {
  218. error = -ENOMEM;
  219. goto failed1;
  220. }
  221. spin_lock_init(&drvdata->lock);
  222. drvdata->irq = irq;
  223. drvdata->serio = serio;
  224. drvdata->dev = dev;
  225. phys_addr = r_mem.start;
  226. remap_size = resource_size(&r_mem);
  227. if (!request_mem_region(phys_addr, remap_size, DRIVER_NAME)) {
  228. dev_err(dev, "Couldn't lock memory region at 0x%08llX\n",
  229. (unsigned long long)phys_addr);
  230. error = -EBUSY;
  231. goto failed1;
  232. }
  233. /* Fill in configuration data and add them to the list */
  234. drvdata->base_address = ioremap(phys_addr, remap_size);
  235. if (drvdata->base_address == NULL) {
  236. dev_err(dev, "Couldn't ioremap memory at 0x%08llX\n",
  237. (unsigned long long)phys_addr);
  238. error = -EFAULT;
  239. goto failed2;
  240. }
  241. /* Disable all the interrupts, just in case */
  242. out_be32(drvdata->base_address + XPS2_IPIER_OFFSET, 0);
  243. /*
  244. * Reset the PS2 device and abort any current transaction,
  245. * to make sure we have the PS2 in a good state.
  246. */
  247. out_be32(drvdata->base_address + XPS2_SRST_OFFSET, XPS2_SRST_RESET);
  248. dev_info(dev, "Xilinx PS2 at 0x%08llX mapped to 0x%p, irq=%d\n",
  249. (unsigned long long)phys_addr, drvdata->base_address,
  250. drvdata->irq);
  251. serio->id.type = SERIO_8042;
  252. serio->write = sxps2_write;
  253. serio->open = sxps2_open;
  254. serio->close = sxps2_close;
  255. serio->port_data = drvdata;
  256. serio->dev.parent = dev;
  257. snprintf(serio->name, sizeof(serio->name),
  258. "Xilinx XPS PS/2 at %08llX", (unsigned long long)phys_addr);
  259. snprintf(serio->phys, sizeof(serio->phys),
  260. "xilinxps2/serio at %08llX", (unsigned long long)phys_addr);
  261. serio_register_port(serio);
  262. platform_set_drvdata(ofdev, drvdata);
  263. return 0; /* success */
  264. failed2:
  265. release_mem_region(phys_addr, remap_size);
  266. failed1:
  267. kfree(serio);
  268. kfree(drvdata);
  269. return error;
  270. }
  271. /**
  272. * xps2_of_remove - unbinds the driver from the PS/2 device.
  273. * @of_dev: pointer to OF device structure
  274. *
  275. * This function is called if a device is physically removed from the system or
  276. * if the driver module is being unloaded. It frees any resources allocated to
  277. * the device.
  278. */
  279. static int xps2_of_remove(struct platform_device *of_dev)
  280. {
  281. struct xps2data *drvdata = platform_get_drvdata(of_dev);
  282. struct resource r_mem; /* IO mem resources */
  283. serio_unregister_port(drvdata->serio);
  284. iounmap(drvdata->base_address);
  285. /* Get iospace of the device */
  286. if (of_address_to_resource(of_dev->dev.of_node, 0, &r_mem))
  287. dev_err(drvdata->dev, "invalid address\n");
  288. else
  289. release_mem_region(r_mem.start, resource_size(&r_mem));
  290. kfree(drvdata);
  291. return 0;
  292. }
  293. /* Match table for of_platform binding */
  294. static const struct of_device_id xps2_of_match[] = {
  295. { .compatible = "xlnx,xps-ps2-1.00.a", },
  296. { /* end of list */ },
  297. };
  298. MODULE_DEVICE_TABLE(of, xps2_of_match);
  299. static struct platform_driver xps2_of_driver = {
  300. .driver = {
  301. .name = DRIVER_NAME,
  302. .of_match_table = xps2_of_match,
  303. },
  304. .probe = xps2_of_probe,
  305. .remove = xps2_of_remove,
  306. };
  307. module_platform_driver(xps2_of_driver);
  308. MODULE_AUTHOR("Xilinx, Inc.");
  309. MODULE_DESCRIPTION("Xilinx XPS PS/2 driver");
  310. MODULE_LICENSE("GPL");