xilinx_ps2.c 11 KB

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