xilinx_ps2.c 11 KB

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