xilinx_ps2.c 11 KB

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