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