blob: 1e983bec7d86c524243d55e57ff409e2d831571d [file] [log] [blame]
John Linn11918282008-07-07 16:17:48 -04001/*
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
17
18#include <linux/module.h>
19#include <linux/serio.h>
20#include <linux/interrupt.h>
21#include <linux/errno.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090022#include <linux/slab.h>
John Linn11918282008-07-07 16:17:48 -040023#include <linux/init.h>
24#include <linux/list.h>
25#include <linux/io.h>
Michal Simek5f9288a2011-07-13 15:46:21 +020026#include <linux/of_address.h>
John Linn11918282008-07-07 16:17:48 -040027#include <linux/of_device.h>
28#include <linux/of_platform.h>
29
30#define DRIVER_NAME "xilinx_ps2"
31
32/* Register offsets for the xps2 device */
33#define XPS2_SRST_OFFSET 0x00000000 /* Software Reset register */
34#define XPS2_STATUS_OFFSET 0x00000004 /* Status register */
35#define XPS2_RX_DATA_OFFSET 0x00000008 /* Receive Data register */
36#define XPS2_TX_DATA_OFFSET 0x0000000C /* Transmit Data register */
37#define XPS2_GIER_OFFSET 0x0000002C /* Global Interrupt Enable reg */
38#define XPS2_IPISR_OFFSET 0x00000030 /* Interrupt Status register */
39#define XPS2_IPIER_OFFSET 0x00000038 /* Interrupt Enable register */
40
41/* Reset Register Bit Definitions */
42#define XPS2_SRST_RESET 0x0000000A /* Software Reset */
43
44/* Status Register Bit Positions */
45#define XPS2_STATUS_RX_FULL 0x00000001 /* Receive Full */
46#define XPS2_STATUS_TX_FULL 0x00000002 /* Transmit Full */
47
48/* Bit definitions for ISR/IER registers. Both the registers have the same bit
49 * definitions and are only defined once. */
50#define XPS2_IPIXR_WDT_TOUT 0x00000001 /* Watchdog Timeout Interrupt */
51#define XPS2_IPIXR_TX_NOACK 0x00000002 /* Transmit No ACK Interrupt */
52#define XPS2_IPIXR_TX_ACK 0x00000004 /* Transmit ACK (Data) Interrupt */
53#define XPS2_IPIXR_RX_OVF 0x00000008 /* Receive Overflow Interrupt */
54#define XPS2_IPIXR_RX_ERR 0x00000010 /* Receive Error Interrupt */
55#define XPS2_IPIXR_RX_FULL 0x00000020 /* Receive Data Interrupt */
56
57/* Mask for all the Transmit Interrupts */
58#define XPS2_IPIXR_TX_ALL (XPS2_IPIXR_TX_NOACK | XPS2_IPIXR_TX_ACK)
59
60/* Mask for all the Receive Interrupts */
61#define XPS2_IPIXR_RX_ALL (XPS2_IPIXR_RX_OVF | XPS2_IPIXR_RX_ERR | \
John Linnb4a3ac92008-10-27 22:17:22 -040062 XPS2_IPIXR_RX_FULL)
John Linn11918282008-07-07 16:17:48 -040063
64/* Mask for all the Interrupts */
65#define XPS2_IPIXR_ALL (XPS2_IPIXR_TX_ALL | XPS2_IPIXR_RX_ALL | \
John Linnb4a3ac92008-10-27 22:17:22 -040066 XPS2_IPIXR_WDT_TOUT)
John Linn11918282008-07-07 16:17:48 -040067
68/* Global Interrupt Enable mask */
69#define XPS2_GIER_GIE_MASK 0x80000000
70
71struct xps2data {
72 int irq;
John Linn11918282008-07-07 16:17:48 -040073 spinlock_t lock;
John Linn11918282008-07-07 16:17:48 -040074 void __iomem *base_address; /* virt. address of control registers */
John Linnb4a3ac92008-10-27 22:17:22 -040075 unsigned int flags;
Dmitry Torokhov271002c2012-03-25 17:23:19 -070076 struct serio *serio; /* serio */
77 struct device *dev;
John Linn11918282008-07-07 16:17:48 -040078};
79
80/************************************/
81/* XPS PS/2 data transmission calls */
82/************************************/
83
John Linnb4a3ac92008-10-27 22:17:22 -040084/**
85 * xps2_recv() - attempts to receive a byte from the PS/2 port.
86 * @drvdata: pointer to ps2 device private data structure
87 * @byte: address where the read data will be copied
88 *
89 * If there is any data available in the PS/2 receiver, this functions reads
90 * the data, otherwise it returns error.
John Linn11918282008-07-07 16:17:48 -040091 */
92static int xps2_recv(struct xps2data *drvdata, u8 *byte)
93{
94 u32 sr;
95 int status = -1;
96
97 /* If there is data available in the PS/2 receiver, read it */
98 sr = in_be32(drvdata->base_address + XPS2_STATUS_OFFSET);
99 if (sr & XPS2_STATUS_RX_FULL) {
100 *byte = in_be32(drvdata->base_address + XPS2_RX_DATA_OFFSET);
101 status = 0;
102 }
103
104 return status;
105}
106
107/*********************/
108/* Interrupt handler */
109/*********************/
110static irqreturn_t xps2_interrupt(int irq, void *dev_id)
111{
112 struct xps2data *drvdata = dev_id;
113 u32 intr_sr;
114 u8 c;
115 int status;
116
117 /* Get the PS/2 interrupts and clear them */
118 intr_sr = in_be32(drvdata->base_address + XPS2_IPISR_OFFSET);
119 out_be32(drvdata->base_address + XPS2_IPISR_OFFSET, intr_sr);
120
121 /* Check which interrupt is active */
122 if (intr_sr & XPS2_IPIXR_RX_OVF)
Dmitry Torokhov271002c2012-03-25 17:23:19 -0700123 dev_warn(drvdata->dev, "receive overrun error\n");
John Linn11918282008-07-07 16:17:48 -0400124
125 if (intr_sr & XPS2_IPIXR_RX_ERR)
John Linnb4a3ac92008-10-27 22:17:22 -0400126 drvdata->flags |= SERIO_PARITY;
John Linn11918282008-07-07 16:17:48 -0400127
128 if (intr_sr & (XPS2_IPIXR_TX_NOACK | XPS2_IPIXR_WDT_TOUT))
John Linnb4a3ac92008-10-27 22:17:22 -0400129 drvdata->flags |= SERIO_TIMEOUT;
John Linn11918282008-07-07 16:17:48 -0400130
131 if (intr_sr & XPS2_IPIXR_RX_FULL) {
John Linnb4a3ac92008-10-27 22:17:22 -0400132 status = xps2_recv(drvdata, &c);
John Linn11918282008-07-07 16:17:48 -0400133
134 /* Error, if a byte is not received */
135 if (status) {
Dmitry Torokhov271002c2012-03-25 17:23:19 -0700136 dev_err(drvdata->dev,
John Linnb4a3ac92008-10-27 22:17:22 -0400137 "wrong rcvd byte count (%d)\n", status);
John Linn11918282008-07-07 16:17:48 -0400138 } else {
Dmitry Torokhov271002c2012-03-25 17:23:19 -0700139 serio_interrupt(drvdata->serio, c, drvdata->flags);
John Linnb4a3ac92008-10-27 22:17:22 -0400140 drvdata->flags = 0;
John Linn11918282008-07-07 16:17:48 -0400141 }
142 }
143
John Linn11918282008-07-07 16:17:48 -0400144 return IRQ_HANDLED;
145}
146
147/*******************/
148/* serio callbacks */
149/*******************/
150
John Linnb4a3ac92008-10-27 22:17:22 -0400151/**
152 * sxps2_write() - sends a byte out through the PS/2 port.
153 * @pserio: pointer to the serio structure of the PS/2 port
154 * @c: data that needs to be written to the PS/2 port
155 *
156 * This function checks if the PS/2 transmitter is empty and sends a byte.
157 * Otherwise it returns error. Transmission fails only when nothing is connected
158 * to the PS/2 port. Thats why, we do not try to resend the data in case of a
159 * failure.
John Linn11918282008-07-07 16:17:48 -0400160 */
161static int sxps2_write(struct serio *pserio, unsigned char c)
162{
163 struct xps2data *drvdata = pserio->port_data;
164 unsigned long flags;
165 u32 sr;
166 int status = -1;
167
168 spin_lock_irqsave(&drvdata->lock, flags);
169
170 /* If the PS/2 transmitter is empty send a byte of data */
171 sr = in_be32(drvdata->base_address + XPS2_STATUS_OFFSET);
172 if (!(sr & XPS2_STATUS_TX_FULL)) {
173 out_be32(drvdata->base_address + XPS2_TX_DATA_OFFSET, c);
174 status = 0;
175 }
176
177 spin_unlock_irqrestore(&drvdata->lock, flags);
178
179 return status;
180}
181
John Linnb4a3ac92008-10-27 22:17:22 -0400182/**
183 * sxps2_open() - called when a port is opened by the higher layer.
184 * @pserio: pointer to the serio structure of the PS/2 device
185 *
186 * This function requests irq and enables interrupts for the PS/2 device.
John Linn11918282008-07-07 16:17:48 -0400187 */
188static int sxps2_open(struct serio *pserio)
189{
190 struct xps2data *drvdata = pserio->port_data;
John Linnb4a3ac92008-10-27 22:17:22 -0400191 int error;
192 u8 c;
John Linn11918282008-07-07 16:17:48 -0400193
John Linnb4a3ac92008-10-27 22:17:22 -0400194 error = request_irq(drvdata->irq, &xps2_interrupt, 0,
John Linn11918282008-07-07 16:17:48 -0400195 DRIVER_NAME, drvdata);
John Linnb4a3ac92008-10-27 22:17:22 -0400196 if (error) {
Dmitry Torokhov271002c2012-03-25 17:23:19 -0700197 dev_err(drvdata->dev,
John Linnb4a3ac92008-10-27 22:17:22 -0400198 "Couldn't allocate interrupt %d\n", drvdata->irq);
199 return error;
John Linn11918282008-07-07 16:17:48 -0400200 }
201
202 /* start reception by enabling the interrupts */
203 out_be32(drvdata->base_address + XPS2_GIER_OFFSET, XPS2_GIER_GIE_MASK);
204 out_be32(drvdata->base_address + XPS2_IPIER_OFFSET, XPS2_IPIXR_RX_ALL);
John Linnb4a3ac92008-10-27 22:17:22 -0400205 (void)xps2_recv(drvdata, &c);
John Linn11918282008-07-07 16:17:48 -0400206
207 return 0; /* success */
208}
209
John Linnb4a3ac92008-10-27 22:17:22 -0400210/**
211 * sxps2_close() - frees the interrupt.
212 * @pserio: pointer to the serio structure of the PS/2 device
213 *
214 * This function frees the irq and disables interrupts for the PS/2 device.
John Linn11918282008-07-07 16:17:48 -0400215 */
216static void sxps2_close(struct serio *pserio)
217{
218 struct xps2data *drvdata = pserio->port_data;
219
220 /* Disable the PS2 interrupts */
221 out_be32(drvdata->base_address + XPS2_GIER_OFFSET, 0x00);
222 out_be32(drvdata->base_address + XPS2_IPIER_OFFSET, 0x00);
223 free_irq(drvdata->irq, drvdata);
224}
225
John Linnb4a3ac92008-10-27 22:17:22 -0400226/**
227 * xps2_of_probe - probe method for the PS/2 device.
228 * @of_dev: pointer to OF device structure
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300229 * @match: pointer to the structure used for matching a device
John Linnb4a3ac92008-10-27 22:17:22 -0400230 *
231 * This function probes the PS/2 device in the device tree.
232 * It initializes the driver data structure and the hardware.
233 * It returns 0, if the driver is bound to the PS/2 device, or a negative
234 * value if there is an error.
235 */
Grant Likely1c48a5c2011-02-17 02:43:24 -0700236static int __devinit xps2_of_probe(struct platform_device *ofdev)
John Linn11918282008-07-07 16:17:48 -0400237{
John Linnb4a3ac92008-10-27 22:17:22 -0400238 struct resource r_irq; /* Interrupt resources */
239 struct resource r_mem; /* IO mem resources */
John Linn11918282008-07-07 16:17:48 -0400240 struct xps2data *drvdata;
241 struct serio *serio;
John Linnb4a3ac92008-10-27 22:17:22 -0400242 struct device *dev = &ofdev->dev;
243 resource_size_t remap_size, phys_addr;
244 int error;
John Linn11918282008-07-07 16:17:48 -0400245
John Linnb4a3ac92008-10-27 22:17:22 -0400246 dev_info(dev, "Device Tree Probing \'%s\'\n",
Grant Likely61c7a082010-04-13 16:12:29 -0700247 ofdev->dev.of_node->name);
John Linn11918282008-07-07 16:17:48 -0400248
John Linnb4a3ac92008-10-27 22:17:22 -0400249 /* Get iospace for the device */
Grant Likely61c7a082010-04-13 16:12:29 -0700250 error = of_address_to_resource(ofdev->dev.of_node, 0, &r_mem);
John Linnb4a3ac92008-10-27 22:17:22 -0400251 if (error) {
252 dev_err(dev, "invalid address\n");
253 return error;
254 }
255
256 /* Get IRQ for the device */
Michal Simekbe3bad62011-12-21 13:28:23 +0100257 if (!of_irq_to_resource(ofdev->dev.of_node, 0, &r_irq)) {
John Linnb4a3ac92008-10-27 22:17:22 -0400258 dev_err(dev, "no IRQ found\n");
259 return -ENODEV;
John Linn11918282008-07-07 16:17:48 -0400260 }
261
262 drvdata = kzalloc(sizeof(struct xps2data), GFP_KERNEL);
Dmitry Torokhov271002c2012-03-25 17:23:19 -0700263 serio = kzalloc(sizeof(struct serio), GFP_KERNEL);
264 if (!drvdata || !serio) {
265 error = -ENOMEM;
266 goto failed1;
John Linn11918282008-07-07 16:17:48 -0400267 }
268
John Linn11918282008-07-07 16:17:48 -0400269 spin_lock_init(&drvdata->lock);
John Linnb4a3ac92008-10-27 22:17:22 -0400270 drvdata->irq = r_irq.start;
Dmitry Torokhov271002c2012-03-25 17:23:19 -0700271 drvdata->serio = serio;
272 drvdata->dev = dev;
John Linn11918282008-07-07 16:17:48 -0400273
John Linnb4a3ac92008-10-27 22:17:22 -0400274 phys_addr = r_mem.start;
Tobias Klauserce841b92010-01-21 23:52:37 -0800275 remap_size = resource_size(&r_mem);
John Linnb4a3ac92008-10-27 22:17:22 -0400276 if (!request_mem_region(phys_addr, remap_size, DRIVER_NAME)) {
277 dev_err(dev, "Couldn't lock memory region at 0x%08llX\n",
278 (unsigned long long)phys_addr);
279 error = -EBUSY;
John Linn11918282008-07-07 16:17:48 -0400280 goto failed1;
281 }
282
283 /* Fill in configuration data and add them to the list */
John Linnb4a3ac92008-10-27 22:17:22 -0400284 drvdata->base_address = ioremap(phys_addr, remap_size);
John Linn11918282008-07-07 16:17:48 -0400285 if (drvdata->base_address == NULL) {
John Linnb4a3ac92008-10-27 22:17:22 -0400286 dev_err(dev, "Couldn't ioremap memory at 0x%08llX\n",
287 (unsigned long long)phys_addr);
288 error = -EFAULT;
John Linn11918282008-07-07 16:17:48 -0400289 goto failed2;
290 }
291
292 /* Disable all the interrupts, just in case */
293 out_be32(drvdata->base_address + XPS2_IPIER_OFFSET, 0);
294
295 /* Reset the PS2 device and abort any current transaction, to make sure
296 * we have the PS2 in a good state */
297 out_be32(drvdata->base_address + XPS2_SRST_OFFSET, XPS2_SRST_RESET);
298
John Linnb4a3ac92008-10-27 22:17:22 -0400299 dev_info(dev, "Xilinx PS2 at 0x%08llX mapped to 0x%p, irq=%d\n",
300 (unsigned long long)phys_addr, drvdata->base_address,
301 drvdata->irq);
John Linn11918282008-07-07 16:17:48 -0400302
John Linn11918282008-07-07 16:17:48 -0400303 serio->id.type = SERIO_8042;
304 serio->write = sxps2_write;
305 serio->open = sxps2_open;
306 serio->close = sxps2_close;
307 serio->port_data = drvdata;
308 serio->dev.parent = dev;
309 snprintf(serio->name, sizeof(serio->name),
John Linnb4a3ac92008-10-27 22:17:22 -0400310 "Xilinx XPS PS/2 at %08llX", (unsigned long long)phys_addr);
John Linn11918282008-07-07 16:17:48 -0400311 snprintf(serio->phys, sizeof(serio->phys),
John Linnb4a3ac92008-10-27 22:17:22 -0400312 "xilinxps2/serio at %08llX", (unsigned long long)phys_addr);
313
John Linn11918282008-07-07 16:17:48 -0400314 serio_register_port(serio);
315
Dmitry Torokhov271002c2012-03-25 17:23:19 -0700316 platform_set_drvdata(ofdev, drvdata);
John Linn11918282008-07-07 16:17:48 -0400317 return 0; /* success */
318
319failed2:
John Linnb4a3ac92008-10-27 22:17:22 -0400320 release_mem_region(phys_addr, remap_size);
John Linn11918282008-07-07 16:17:48 -0400321failed1:
Dmitry Torokhov271002c2012-03-25 17:23:19 -0700322 kfree(serio);
John Linn11918282008-07-07 16:17:48 -0400323 kfree(drvdata);
John Linn11918282008-07-07 16:17:48 -0400324
John Linnb4a3ac92008-10-27 22:17:22 -0400325 return error;
John Linn11918282008-07-07 16:17:48 -0400326}
327
John Linnb4a3ac92008-10-27 22:17:22 -0400328/**
329 * xps2_of_remove - unbinds the driver from the PS/2 device.
330 * @of_dev: pointer to OF device structure
331 *
332 * This function is called if a device is physically removed from the system or
333 * if the driver module is being unloaded. It frees any resources allocated to
334 * the device.
335 */
Grant Likely2dc11582010-08-06 09:25:50 -0600336static int __devexit xps2_of_remove(struct platform_device *of_dev)
John Linn11918282008-07-07 16:17:48 -0400337{
Dmitry Torokhov271002c2012-03-25 17:23:19 -0700338 struct xps2data *drvdata = platform_get_drvdata(of_dev);
John Linnb4a3ac92008-10-27 22:17:22 -0400339 struct resource r_mem; /* IO mem resources */
John Linn11918282008-07-07 16:17:48 -0400340
Dmitry Torokhov271002c2012-03-25 17:23:19 -0700341 serio_unregister_port(drvdata->serio);
John Linn11918282008-07-07 16:17:48 -0400342 iounmap(drvdata->base_address);
John Linnb4a3ac92008-10-27 22:17:22 -0400343
344 /* Get iospace of the device */
Grant Likely61c7a082010-04-13 16:12:29 -0700345 if (of_address_to_resource(of_dev->dev.of_node, 0, &r_mem))
Dmitry Torokhov271002c2012-03-25 17:23:19 -0700346 dev_err(drvdata->dev, "invalid address\n");
John Linnb4a3ac92008-10-27 22:17:22 -0400347 else
Tobias Klauserce841b92010-01-21 23:52:37 -0800348 release_mem_region(r_mem.start, resource_size(&r_mem));
John Linnb4a3ac92008-10-27 22:17:22 -0400349
John Linn11918282008-07-07 16:17:48 -0400350 kfree(drvdata);
351
Dmitry Torokhov271002c2012-03-25 17:23:19 -0700352 platform_set_drvdata(of_dev, NULL);
John Linn11918282008-07-07 16:17:48 -0400353
John Linnb4a3ac92008-10-27 22:17:22 -0400354 return 0;
John Linn11918282008-07-07 16:17:48 -0400355}
356
357/* Match table for of_platform binding */
Márton Némethef9a16f2010-01-09 23:23:02 -0800358static const struct of_device_id xps2_of_match[] __devinitconst = {
John Linn11918282008-07-07 16:17:48 -0400359 { .compatible = "xlnx,xps-ps2-1.00.a", },
360 { /* end of list */ },
361};
362MODULE_DEVICE_TABLE(of, xps2_of_match);
363
Grant Likely1c48a5c2011-02-17 02:43:24 -0700364static struct platform_driver xps2_of_driver = {
Grant Likely40182942010-04-13 16:13:02 -0700365 .driver = {
366 .name = DRIVER_NAME,
367 .owner = THIS_MODULE,
368 .of_match_table = xps2_of_match,
369 },
John Linn11918282008-07-07 16:17:48 -0400370 .probe = xps2_of_probe,
371 .remove = __devexit_p(xps2_of_remove),
372};
JJ Ding24d24692011-11-29 11:08:43 -0800373module_platform_driver(xps2_of_driver);
John Linn11918282008-07-07 16:17:48 -0400374
375MODULE_AUTHOR("Xilinx, Inc.");
376MODULE_DESCRIPTION("Xilinx XPS PS/2 driver");
377MODULE_LICENSE("GPL");
378