2 * Xilinx XPS PS/2 device driver
4 * (c) 2005 MontaVista Software, Inc.
5 * (c) 2008 Xilinx, Inc.
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.
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.
18 #include <linux/module.h>
19 #include <linux/serio.h>
20 #include <linux/interrupt.h>
21 #include <linux/errno.h>
22 #include <linux/init.h>
23 #include <linux/list.h>
26 #include <linux/of_device.h>
27 #include <linux/of_platform.h>
29 #define DRIVER_NAME "xilinx_ps2"
31 /* Register offsets for the xps2 device */
32 #define XPS2_SRST_OFFSET 0x00000000 /* Software Reset register */
33 #define XPS2_STATUS_OFFSET 0x00000004 /* Status register */
34 #define XPS2_RX_DATA_OFFSET 0x00000008 /* Receive Data register */
35 #define XPS2_TX_DATA_OFFSET 0x0000000C /* Transmit Data register */
36 #define XPS2_GIER_OFFSET 0x0000002C /* Global Interrupt Enable reg */
37 #define XPS2_IPISR_OFFSET 0x00000030 /* Interrupt Status register */
38 #define XPS2_IPIER_OFFSET 0x00000038 /* Interrupt Enable register */
40 /* Reset Register Bit Definitions */
41 #define XPS2_SRST_RESET 0x0000000A /* Software Reset */
43 /* Status Register Bit Positions */
44 #define XPS2_STATUS_RX_FULL 0x00000001 /* Receive Full */
45 #define XPS2_STATUS_TX_FULL 0x00000002 /* Transmit Full */
47 /* Bit definitions for ISR/IER registers. Both the registers have the same bit
48 * definitions and are only defined once. */
49 #define XPS2_IPIXR_WDT_TOUT 0x00000001 /* Watchdog Timeout Interrupt */
50 #define XPS2_IPIXR_TX_NOACK 0x00000002 /* Transmit No ACK Interrupt */
51 #define XPS2_IPIXR_TX_ACK 0x00000004 /* Transmit ACK (Data) Interrupt */
52 #define XPS2_IPIXR_RX_OVF 0x00000008 /* Receive Overflow Interrupt */
53 #define XPS2_IPIXR_RX_ERR 0x00000010 /* Receive Error Interrupt */
54 #define XPS2_IPIXR_RX_FULL 0x00000020 /* Receive Data Interrupt */
56 /* Mask for all the Transmit Interrupts */
57 #define XPS2_IPIXR_TX_ALL (XPS2_IPIXR_TX_NOACK | XPS2_IPIXR_TX_ACK)
59 /* Mask for all the Receive Interrupts */
60 #define XPS2_IPIXR_RX_ALL (XPS2_IPIXR_RX_OVF | XPS2_IPIXR_RX_ERR | \
63 /* Mask for all the Interrupts */
64 #define XPS2_IPIXR_ALL (XPS2_IPIXR_TX_ALL | XPS2_IPIXR_RX_ALL | \
67 /* Global Interrupt Enable mask */
68 #define XPS2_GIER_GIE_MASK 0x80000000
75 u8 rxb; /* Rx buffer */
76 void __iomem *base_address; /* virt. address of control registers */
78 struct serio serio; /* serio */
81 /************************************/
82 /* XPS PS/2 data transmission calls */
83 /************************************/
86 * xps2_recv() will attempt to receive a byte of data from the PS/2 port.
88 static int xps2_recv(struct xps2data *drvdata, u8 *byte)
93 /* If there is data available in the PS/2 receiver, read it */
94 sr = in_be32(drvdata->base_address + XPS2_STATUS_OFFSET);
95 if (sr & XPS2_STATUS_RX_FULL) {
96 *byte = in_be32(drvdata->base_address + XPS2_RX_DATA_OFFSET);
103 /*********************/
104 /* Interrupt handler */
105 /*********************/
106 static irqreturn_t xps2_interrupt(int irq, void *dev_id)
108 struct xps2data *drvdata = dev_id;
113 /* Get the PS/2 interrupts and clear them */
114 intr_sr = in_be32(drvdata->base_address + XPS2_IPISR_OFFSET);
115 out_be32(drvdata->base_address + XPS2_IPISR_OFFSET, intr_sr);
117 /* Check which interrupt is active */
118 if (intr_sr & XPS2_IPIXR_RX_OVF)
119 printk(KERN_WARNING "%s: receive overrun error\n",
120 drvdata->serio.name);
122 if (intr_sr & XPS2_IPIXR_RX_ERR)
123 drvdata->dfl |= SERIO_PARITY;
125 if (intr_sr & (XPS2_IPIXR_TX_NOACK | XPS2_IPIXR_WDT_TOUT))
126 drvdata->dfl |= SERIO_TIMEOUT;
128 if (intr_sr & XPS2_IPIXR_RX_FULL) {
129 status = xps2_recv(drvdata, &drvdata->rxb);
131 /* Error, if a byte is not received */
134 "%s: wrong rcvd byte count (%d)\n",
135 drvdata->serio.name, status);
138 serio_interrupt(&drvdata->serio, c, drvdata->dfl);
143 if (intr_sr & XPS2_IPIXR_TX_ACK)
149 /*******************/
150 /* serio callbacks */
151 /*******************/
154 * sxps2_write() sends a byte out through the PS/2 interface.
156 static int sxps2_write(struct serio *pserio, unsigned char c)
158 struct xps2data *drvdata = pserio->port_data;
163 spin_lock_irqsave(&drvdata->lock, flags);
165 /* If the PS/2 transmitter is empty send a byte of data */
166 sr = in_be32(drvdata->base_address + XPS2_STATUS_OFFSET);
167 if (!(sr & XPS2_STATUS_TX_FULL)) {
168 out_be32(drvdata->base_address + XPS2_TX_DATA_OFFSET, c);
172 spin_unlock_irqrestore(&drvdata->lock, flags);
178 * sxps2_open() is called when a port is open by the higher layer.
180 static int sxps2_open(struct serio *pserio)
182 struct xps2data *drvdata = pserio->port_data;
185 retval = request_irq(drvdata->irq, &xps2_interrupt, 0,
186 DRIVER_NAME, drvdata);
189 "%s: Couldn't allocate interrupt %d\n",
190 drvdata->serio.name, drvdata->irq);
194 /* start reception by enabling the interrupts */
195 out_be32(drvdata->base_address + XPS2_GIER_OFFSET, XPS2_GIER_GIE_MASK);
196 out_be32(drvdata->base_address + XPS2_IPIER_OFFSET, XPS2_IPIXR_RX_ALL);
197 (void)xps2_recv(drvdata, &drvdata->rxb);
199 return 0; /* success */
203 * sxps2_close() frees the interrupt.
205 static void sxps2_close(struct serio *pserio)
207 struct xps2data *drvdata = pserio->port_data;
209 /* Disable the PS2 interrupts */
210 out_be32(drvdata->base_address + XPS2_GIER_OFFSET, 0x00);
211 out_be32(drvdata->base_address + XPS2_IPIER_OFFSET, 0x00);
212 free_irq(drvdata->irq, drvdata);
215 /*********************/
216 /* Device setup code */
217 /*********************/
219 static int xps2_setup(struct device *dev, struct resource *regs_res,
220 struct resource *irq_res)
222 struct xps2data *drvdata;
224 unsigned long remap_size;
230 if (!regs_res || !irq_res) {
231 dev_err(dev, "IO resource(s) not found\n");
235 drvdata = kzalloc(sizeof(struct xps2data), GFP_KERNEL);
237 dev_err(dev, "Couldn't allocate device private record\n");
241 dev_set_drvdata(dev, drvdata);
243 spin_lock_init(&drvdata->lock);
244 drvdata->irq = irq_res->start;
246 remap_size = regs_res->end - regs_res->start + 1;
247 if (!request_mem_region(regs_res->start, remap_size, DRIVER_NAME)) {
248 dev_err(dev, "Couldn't lock memory region at 0x%08X\n",
249 (unsigned int)regs_res->start);
254 /* Fill in configuration data and add them to the list */
255 drvdata->phys_addr = regs_res->start;
256 drvdata->remap_size = remap_size;
257 drvdata->base_address = ioremap(regs_res->start, remap_size);
258 if (drvdata->base_address == NULL) {
259 dev_err(dev, "Couldn't ioremap memory at 0x%08X\n",
260 (unsigned int)regs_res->start);
265 /* Disable all the interrupts, just in case */
266 out_be32(drvdata->base_address + XPS2_IPIER_OFFSET, 0);
268 /* Reset the PS2 device and abort any current transaction, to make sure
269 * we have the PS2 in a good state */
270 out_be32(drvdata->base_address + XPS2_SRST_OFFSET, XPS2_SRST_RESET);
272 dev_info(dev, "Xilinx PS2 at 0x%08X mapped to 0x%p, irq=%d\n",
273 drvdata->phys_addr, drvdata->base_address, drvdata->irq);
275 serio = &drvdata->serio;
276 serio->id.type = SERIO_8042;
277 serio->write = sxps2_write;
278 serio->open = sxps2_open;
279 serio->close = sxps2_close;
280 serio->port_data = drvdata;
281 serio->dev.parent = dev;
282 snprintf(serio->name, sizeof(serio->name),
283 "Xilinx XPS PS/2 at %08X", drvdata->phys_addr);
284 snprintf(serio->phys, sizeof(serio->phys),
285 "xilinxps2/serio at %08X", drvdata->phys_addr);
286 serio_register_port(serio);
288 return 0; /* success */
291 release_mem_region(regs_res->start, remap_size);
294 dev_set_drvdata(dev, NULL);
299 /***************************/
300 /* OF Platform Bus Support */
301 /***************************/
303 static int __devinit xps2_of_probe(struct of_device *ofdev, const struct
304 of_device_id * match)
306 struct resource r_irq; /* Interrupt resources */
307 struct resource r_mem; /* IO mem resources */
310 printk(KERN_INFO "Device Tree Probing \'%s\'\n",
313 /* Get iospace for the device */
314 rc = of_address_to_resource(ofdev->node, 0, &r_mem);
316 dev_err(&ofdev->dev, "invalid address\n");
320 /* Get IRQ for the device */
321 rc = of_irq_to_resource(ofdev->node, 0, &r_irq);
323 dev_err(&ofdev->dev, "no IRQ found\n");
327 return xps2_setup(&ofdev->dev, &r_mem, &r_irq);
330 static int __devexit xps2_of_remove(struct of_device *of_dev)
332 struct device *dev = &of_dev->dev;
333 struct xps2data *drvdata;
338 drvdata = dev_get_drvdata(dev);
340 serio_unregister_port(&drvdata->serio);
341 iounmap(drvdata->base_address);
342 release_mem_region(drvdata->phys_addr, drvdata->remap_size);
345 dev_set_drvdata(dev, NULL);
347 return 0; /* success */
350 /* Match table for of_platform binding */
351 static struct of_device_id xps2_of_match[] __devinitdata = {
352 { .compatible = "xlnx,xps-ps2-1.00.a", },
353 { /* end of list */ },
355 MODULE_DEVICE_TABLE(of, xps2_of_match);
357 static struct of_platform_driver xps2_of_driver = {
359 .match_table = xps2_of_match,
360 .probe = xps2_of_probe,
361 .remove = __devexit_p(xps2_of_remove),
364 static int __init xps2_init(void)
366 return of_register_platform_driver(&xps2_of_driver);
369 static void __exit xps2_cleanup(void)
371 of_unregister_platform_driver(&xps2_of_driver);
374 module_init(xps2_init);
375 module_exit(xps2_cleanup);
377 MODULE_AUTHOR("Xilinx, Inc.");
378 MODULE_DESCRIPTION("Xilinx XPS PS/2 driver");
379 MODULE_LICENSE("GPL");