xilinx_ps2.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  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_address.h>
  25. #include <linux/of_device.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. };
  66. /************************************/
  67. /* XPS PS/2 data transmission calls */
  68. /************************************/
  69. /**
  70. * xps2_recv() - attempts to receive a byte from the PS/2 port.
  71. * @drvdata: pointer to ps2 device private data structure
  72. * @byte: address where the read data will be copied
  73. *
  74. * If there is any data available in the PS/2 receiver, this functions reads
  75. * the data, otherwise it returns error.
  76. */
  77. static int xps2_recv(struct xps2data *drvdata, u8 *byte)
  78. {
  79. u32 sr;
  80. int status = -1;
  81. /* If there is data available in the PS/2 receiver, read it */
  82. sr = in_be32(drvdata->base_address + XPS2_STATUS_OFFSET);
  83. if (sr & XPS2_STATUS_RX_FULL) {
  84. *byte = in_be32(drvdata->base_address + XPS2_RX_DATA_OFFSET);
  85. status = 0;
  86. }
  87. return status;
  88. }
  89. /*********************/
  90. /* Interrupt handler */
  91. /*********************/
  92. static irqreturn_t xps2_interrupt(int irq, void *dev_id)
  93. {
  94. struct xps2data *drvdata = dev_id;
  95. u32 intr_sr;
  96. u8 c;
  97. int status;
  98. /* Get the PS/2 interrupts and clear them */
  99. intr_sr = in_be32(drvdata->base_address + XPS2_IPISR_OFFSET);
  100. out_be32(drvdata->base_address + XPS2_IPISR_OFFSET, intr_sr);
  101. /* Check which interrupt is active */
  102. if (intr_sr & XPS2_IPIXR_RX_OVF)
  103. dev_warn(drvdata->serio.dev.parent, "receive overrun error\n");
  104. if (intr_sr & XPS2_IPIXR_RX_ERR)
  105. drvdata->flags |= SERIO_PARITY;
  106. if (intr_sr & (XPS2_IPIXR_TX_NOACK | XPS2_IPIXR_WDT_TOUT))
  107. drvdata->flags |= SERIO_TIMEOUT;
  108. if (intr_sr & XPS2_IPIXR_RX_FULL) {
  109. status = xps2_recv(drvdata, &c);
  110. /* Error, if a byte is not received */
  111. if (status) {
  112. dev_err(drvdata->serio.dev.parent,
  113. "wrong rcvd byte count (%d)\n", status);
  114. } else {
  115. serio_interrupt(&drvdata->serio, c, drvdata->flags);
  116. drvdata->flags = 0;
  117. }
  118. }
  119. return IRQ_HANDLED;
  120. }
  121. /*******************/
  122. /* serio callbacks */
  123. /*******************/
  124. /**
  125. * sxps2_write() - sends a byte out through the PS/2 port.
  126. * @pserio: pointer to the serio structure of the PS/2 port
  127. * @c: data that needs to be written to the PS/2 port
  128. *
  129. * This function checks if the PS/2 transmitter is empty and sends a byte.
  130. * Otherwise it returns error. Transmission fails only when nothing is connected
  131. * to the PS/2 port. Thats why, we do not try to resend the data in case of a
  132. * failure.
  133. */
  134. static int sxps2_write(struct serio *pserio, unsigned char c)
  135. {
  136. struct xps2data *drvdata = pserio->port_data;
  137. unsigned long flags;
  138. u32 sr;
  139. int status = -1;
  140. spin_lock_irqsave(&drvdata->lock, flags);
  141. /* If the PS/2 transmitter is empty send a byte of data */
  142. sr = in_be32(drvdata->base_address + XPS2_STATUS_OFFSET);
  143. if (!(sr & XPS2_STATUS_TX_FULL)) {
  144. out_be32(drvdata->base_address + XPS2_TX_DATA_OFFSET, c);
  145. status = 0;
  146. }
  147. spin_unlock_irqrestore(&drvdata->lock, flags);
  148. return status;
  149. }
  150. /**
  151. * sxps2_open() - called when a port is opened by the higher layer.
  152. * @pserio: pointer to the serio structure of the PS/2 device
  153. *
  154. * This function requests irq and enables interrupts for the PS/2 device.
  155. */
  156. static int sxps2_open(struct serio *pserio)
  157. {
  158. struct xps2data *drvdata = pserio->port_data;
  159. int error;
  160. u8 c;
  161. error = request_irq(drvdata->irq, &xps2_interrupt, 0,
  162. DRIVER_NAME, drvdata);
  163. if (error) {
  164. dev_err(drvdata->serio.dev.parent,
  165. "Couldn't allocate interrupt %d\n", drvdata->irq);
  166. return error;
  167. }
  168. /* start reception by enabling the interrupts */
  169. out_be32(drvdata->base_address + XPS2_GIER_OFFSET, XPS2_GIER_GIE_MASK);
  170. out_be32(drvdata->base_address + XPS2_IPIER_OFFSET, XPS2_IPIXR_RX_ALL);
  171. (void)xps2_recv(drvdata, &c);
  172. return 0; /* success */
  173. }
  174. /**
  175. * sxps2_close() - frees the interrupt.
  176. * @pserio: pointer to the serio structure of the PS/2 device
  177. *
  178. * This function frees the irq and disables interrupts for the PS/2 device.
  179. */
  180. static void sxps2_close(struct serio *pserio)
  181. {
  182. struct xps2data *drvdata = pserio->port_data;
  183. /* Disable the PS2 interrupts */
  184. out_be32(drvdata->base_address + XPS2_GIER_OFFSET, 0x00);
  185. out_be32(drvdata->base_address + XPS2_IPIER_OFFSET, 0x00);
  186. free_irq(drvdata->irq, drvdata);
  187. }
  188. /**
  189. * xps2_of_probe - probe method for the PS/2 device.
  190. * @of_dev: pointer to OF device structure
  191. * @match: pointer to the structure used for matching a device
  192. *
  193. * This function probes the PS/2 device in the device tree.
  194. * It initializes the driver data structure and the hardware.
  195. * It returns 0, if the driver is bound to the PS/2 device, or a negative
  196. * value if there is an error.
  197. */
  198. static int __devinit xps2_of_probe(struct platform_device *ofdev)
  199. {
  200. struct resource r_irq; /* Interrupt resources */
  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. int error;
  207. dev_info(dev, "Device Tree Probing \'%s\'\n",
  208. ofdev->dev.of_node->name);
  209. /* Get iospace for the device */
  210. error = of_address_to_resource(ofdev->dev.of_node, 0, &r_mem);
  211. if (error) {
  212. dev_err(dev, "invalid address\n");
  213. return error;
  214. }
  215. /* Get IRQ for the device */
  216. if (!of_irq_to_resource(ofdev->dev.of_node, 0, &r_irq)) {
  217. dev_err(dev, "no IRQ found\n");
  218. return -ENODEV;
  219. }
  220. drvdata = kzalloc(sizeof(struct xps2data), GFP_KERNEL);
  221. if (!drvdata) {
  222. dev_err(dev, "Couldn't allocate device private record\n");
  223. return -ENOMEM;
  224. }
  225. dev_set_drvdata(dev, drvdata);
  226. spin_lock_init(&drvdata->lock);
  227. drvdata->irq = r_irq.start;
  228. phys_addr = r_mem.start;
  229. remap_size = resource_size(&r_mem);
  230. if (!request_mem_region(phys_addr, remap_size, DRIVER_NAME)) {
  231. dev_err(dev, "Couldn't lock memory region at 0x%08llX\n",
  232. (unsigned long long)phys_addr);
  233. error = -EBUSY;
  234. goto failed1;
  235. }
  236. /* Fill in configuration data and add them to the list */
  237. drvdata->base_address = ioremap(phys_addr, remap_size);
  238. if (drvdata->base_address == NULL) {
  239. dev_err(dev, "Couldn't ioremap memory at 0x%08llX\n",
  240. (unsigned long long)phys_addr);
  241. error = -EFAULT;
  242. goto failed2;
  243. }
  244. /* Disable all the interrupts, just in case */
  245. out_be32(drvdata->base_address + XPS2_IPIER_OFFSET, 0);
  246. /* Reset the PS2 device and abort any current transaction, to make sure
  247. * we have the PS2 in a good state */
  248. out_be32(drvdata->base_address + XPS2_SRST_OFFSET, XPS2_SRST_RESET);
  249. dev_info(dev, "Xilinx PS2 at 0x%08llX mapped to 0x%p, irq=%d\n",
  250. (unsigned long long)phys_addr, drvdata->base_address,
  251. drvdata->irq);
  252. serio = &drvdata->serio;
  253. serio->id.type = SERIO_8042;
  254. serio->write = sxps2_write;
  255. serio->open = sxps2_open;
  256. serio->close = sxps2_close;
  257. serio->port_data = drvdata;
  258. serio->dev.parent = dev;
  259. snprintf(serio->name, sizeof(serio->name),
  260. "Xilinx XPS PS/2 at %08llX", (unsigned long long)phys_addr);
  261. snprintf(serio->phys, sizeof(serio->phys),
  262. "xilinxps2/serio at %08llX", (unsigned long long)phys_addr);
  263. serio_register_port(serio);
  264. return 0; /* success */
  265. failed2:
  266. release_mem_region(phys_addr, remap_size);
  267. failed1:
  268. kfree(drvdata);
  269. dev_set_drvdata(dev, NULL);
  270. return error;
  271. }
  272. /**
  273. * xps2_of_remove - unbinds the driver from the PS/2 device.
  274. * @of_dev: pointer to OF device structure
  275. *
  276. * This function is called if a device is physically removed from the system or
  277. * if the driver module is being unloaded. It frees any resources allocated to
  278. * the device.
  279. */
  280. static int __devexit xps2_of_remove(struct platform_device *of_dev)
  281. {
  282. struct device *dev = &of_dev->dev;
  283. struct xps2data *drvdata = dev_get_drvdata(dev);
  284. struct resource r_mem; /* IO mem resources */
  285. serio_unregister_port(&drvdata->serio);
  286. iounmap(drvdata->base_address);
  287. /* Get iospace of the device */
  288. if (of_address_to_resource(of_dev->dev.of_node, 0, &r_mem))
  289. dev_err(dev, "invalid address\n");
  290. else
  291. release_mem_region(r_mem.start, resource_size(&r_mem));
  292. kfree(drvdata);
  293. dev_set_drvdata(dev, NULL);
  294. return 0;
  295. }
  296. /* Match table for of_platform binding */
  297. static const struct of_device_id xps2_of_match[] __devinitconst = {
  298. { .compatible = "xlnx,xps-ps2-1.00.a", },
  299. { /* end of list */ },
  300. };
  301. MODULE_DEVICE_TABLE(of, xps2_of_match);
  302. static struct platform_driver xps2_of_driver = {
  303. .driver = {
  304. .name = DRIVER_NAME,
  305. .owner = THIS_MODULE,
  306. .of_match_table = xps2_of_match,
  307. },
  308. .probe = xps2_of_probe,
  309. .remove = __devexit_p(xps2_of_remove),
  310. };
  311. module_platform_driver(xps2_of_driver);
  312. MODULE_AUTHOR("Xilinx, Inc.");
  313. MODULE_DESCRIPTION("Xilinx XPS PS/2 driver");
  314. MODULE_LICENSE("GPL");