blob: ebb22f88c8426bdd3cebff9e2f822d0b7412ade7 [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>
22#include <linux/init.h>
23#include <linux/list.h>
24#include <linux/io.h>
25
26#include <linux/of_device.h>
27#include <linux/of_platform.h>
28
29#define DRIVER_NAME "xilinx_ps2"
30
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 */
39
40/* Reset Register Bit Definitions */
41#define XPS2_SRST_RESET 0x0000000A /* Software Reset */
42
43/* Status Register Bit Positions */
44#define XPS2_STATUS_RX_FULL 0x00000001 /* Receive Full */
45#define XPS2_STATUS_TX_FULL 0x00000002 /* Transmit Full */
46
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 */
55
56/* Mask for all the Transmit Interrupts */
57#define XPS2_IPIXR_TX_ALL (XPS2_IPIXR_TX_NOACK | XPS2_IPIXR_TX_ACK)
58
59/* Mask for all the Receive Interrupts */
60#define XPS2_IPIXR_RX_ALL (XPS2_IPIXR_RX_OVF | XPS2_IPIXR_RX_ERR | \
John Linnb4a3ac92008-10-27 22:17:22 -040061 XPS2_IPIXR_RX_FULL)
John Linn11918282008-07-07 16:17:48 -040062
63/* Mask for all the Interrupts */
64#define XPS2_IPIXR_ALL (XPS2_IPIXR_TX_ALL | XPS2_IPIXR_RX_ALL | \
John Linnb4a3ac92008-10-27 22:17:22 -040065 XPS2_IPIXR_WDT_TOUT)
John Linn11918282008-07-07 16:17:48 -040066
67/* Global Interrupt Enable mask */
68#define XPS2_GIER_GIE_MASK 0x80000000
69
70struct xps2data {
71 int irq;
John Linn11918282008-07-07 16:17:48 -040072 spinlock_t lock;
John Linn11918282008-07-07 16:17:48 -040073 void __iomem *base_address; /* virt. address of control registers */
John Linnb4a3ac92008-10-27 22:17:22 -040074 unsigned int flags;
John Linn11918282008-07-07 16:17:48 -040075 struct serio serio; /* serio */
76};
77
78/************************************/
79/* XPS PS/2 data transmission calls */
80/************************************/
81
John Linnb4a3ac92008-10-27 22:17:22 -040082/**
83 * xps2_recv() - attempts to receive a byte from the PS/2 port.
84 * @drvdata: pointer to ps2 device private data structure
85 * @byte: address where the read data will be copied
86 *
87 * If there is any data available in the PS/2 receiver, this functions reads
88 * the data, otherwise it returns error.
John Linn11918282008-07-07 16:17:48 -040089 */
90static int xps2_recv(struct xps2data *drvdata, u8 *byte)
91{
92 u32 sr;
93 int status = -1;
94
95 /* If there is data available in the PS/2 receiver, read it */
96 sr = in_be32(drvdata->base_address + XPS2_STATUS_OFFSET);
97 if (sr & XPS2_STATUS_RX_FULL) {
98 *byte = in_be32(drvdata->base_address + XPS2_RX_DATA_OFFSET);
99 status = 0;
100 }
101
102 return status;
103}
104
105/*********************/
106/* Interrupt handler */
107/*********************/
108static irqreturn_t xps2_interrupt(int irq, void *dev_id)
109{
110 struct xps2data *drvdata = dev_id;
111 u32 intr_sr;
112 u8 c;
113 int status;
114
115 /* Get the PS/2 interrupts and clear them */
116 intr_sr = in_be32(drvdata->base_address + XPS2_IPISR_OFFSET);
117 out_be32(drvdata->base_address + XPS2_IPISR_OFFSET, intr_sr);
118
119 /* Check which interrupt is active */
120 if (intr_sr & XPS2_IPIXR_RX_OVF)
John Linnb4a3ac92008-10-27 22:17:22 -0400121 dev_warn(drvdata->serio.dev.parent, "receive overrun error\n");
John Linn11918282008-07-07 16:17:48 -0400122
123 if (intr_sr & XPS2_IPIXR_RX_ERR)
John Linnb4a3ac92008-10-27 22:17:22 -0400124 drvdata->flags |= SERIO_PARITY;
John Linn11918282008-07-07 16:17:48 -0400125
126 if (intr_sr & (XPS2_IPIXR_TX_NOACK | XPS2_IPIXR_WDT_TOUT))
John Linnb4a3ac92008-10-27 22:17:22 -0400127 drvdata->flags |= SERIO_TIMEOUT;
John Linn11918282008-07-07 16:17:48 -0400128
129 if (intr_sr & XPS2_IPIXR_RX_FULL) {
John Linnb4a3ac92008-10-27 22:17:22 -0400130 status = xps2_recv(drvdata, &c);
John Linn11918282008-07-07 16:17:48 -0400131
132 /* Error, if a byte is not received */
133 if (status) {
John Linnb4a3ac92008-10-27 22:17:22 -0400134 dev_err(drvdata->serio.dev.parent,
135 "wrong rcvd byte count (%d)\n", status);
John Linn11918282008-07-07 16:17:48 -0400136 } else {
John Linnb4a3ac92008-10-27 22:17:22 -0400137 serio_interrupt(&drvdata->serio, c, drvdata->flags);
138 drvdata->flags = 0;
John Linn11918282008-07-07 16:17:48 -0400139 }
140 }
141
John Linn11918282008-07-07 16:17:48 -0400142 return IRQ_HANDLED;
143}
144
145/*******************/
146/* serio callbacks */
147/*******************/
148
John Linnb4a3ac92008-10-27 22:17:22 -0400149/**
150 * sxps2_write() - sends a byte out through the PS/2 port.
151 * @pserio: pointer to the serio structure of the PS/2 port
152 * @c: data that needs to be written to the PS/2 port
153 *
154 * This function checks if the PS/2 transmitter is empty and sends a byte.
155 * Otherwise it returns error. Transmission fails only when nothing is connected
156 * to the PS/2 port. Thats why, we do not try to resend the data in case of a
157 * failure.
John Linn11918282008-07-07 16:17:48 -0400158 */
159static int sxps2_write(struct serio *pserio, unsigned char c)
160{
161 struct xps2data *drvdata = pserio->port_data;
162 unsigned long flags;
163 u32 sr;
164 int status = -1;
165
166 spin_lock_irqsave(&drvdata->lock, flags);
167
168 /* If the PS/2 transmitter is empty send a byte of data */
169 sr = in_be32(drvdata->base_address + XPS2_STATUS_OFFSET);
170 if (!(sr & XPS2_STATUS_TX_FULL)) {
171 out_be32(drvdata->base_address + XPS2_TX_DATA_OFFSET, c);
172 status = 0;
173 }
174
175 spin_unlock_irqrestore(&drvdata->lock, flags);
176
177 return status;
178}
179
John Linnb4a3ac92008-10-27 22:17:22 -0400180/**
181 * sxps2_open() - called when a port is opened by the higher layer.
182 * @pserio: pointer to the serio structure of the PS/2 device
183 *
184 * This function requests irq and enables interrupts for the PS/2 device.
John Linn11918282008-07-07 16:17:48 -0400185 */
186static int sxps2_open(struct serio *pserio)
187{
188 struct xps2data *drvdata = pserio->port_data;
John Linnb4a3ac92008-10-27 22:17:22 -0400189 int error;
190 u8 c;
John Linn11918282008-07-07 16:17:48 -0400191
John Linnb4a3ac92008-10-27 22:17:22 -0400192 error = request_irq(drvdata->irq, &xps2_interrupt, 0,
John Linn11918282008-07-07 16:17:48 -0400193 DRIVER_NAME, drvdata);
John Linnb4a3ac92008-10-27 22:17:22 -0400194 if (error) {
195 dev_err(drvdata->serio.dev.parent,
196 "Couldn't allocate interrupt %d\n", drvdata->irq);
197 return error;
John Linn11918282008-07-07 16:17:48 -0400198 }
199
200 /* start reception by enabling the interrupts */
201 out_be32(drvdata->base_address + XPS2_GIER_OFFSET, XPS2_GIER_GIE_MASK);
202 out_be32(drvdata->base_address + XPS2_IPIER_OFFSET, XPS2_IPIXR_RX_ALL);
John Linnb4a3ac92008-10-27 22:17:22 -0400203 (void)xps2_recv(drvdata, &c);
John Linn11918282008-07-07 16:17:48 -0400204
205 return 0; /* success */
206}
207
John Linnb4a3ac92008-10-27 22:17:22 -0400208/**
209 * sxps2_close() - frees the interrupt.
210 * @pserio: pointer to the serio structure of the PS/2 device
211 *
212 * This function frees the irq and disables interrupts for the PS/2 device.
John Linn11918282008-07-07 16:17:48 -0400213 */
214static void sxps2_close(struct serio *pserio)
215{
216 struct xps2data *drvdata = pserio->port_data;
217
218 /* Disable the PS2 interrupts */
219 out_be32(drvdata->base_address + XPS2_GIER_OFFSET, 0x00);
220 out_be32(drvdata->base_address + XPS2_IPIER_OFFSET, 0x00);
221 free_irq(drvdata->irq, drvdata);
222}
223
John Linnb4a3ac92008-10-27 22:17:22 -0400224/**
225 * xps2_of_probe - probe method for the PS/2 device.
226 * @of_dev: pointer to OF device structure
227 * @match: pointer to the stucture used for matching a device
228 *
229 * This function probes the PS/2 device in the device tree.
230 * It initializes the driver data structure and the hardware.
231 * It returns 0, if the driver is bound to the PS/2 device, or a negative
232 * value if there is an error.
233 */
234static int __devinit xps2_of_probe(struct of_device *ofdev,
235 const struct of_device_id *match)
John Linn11918282008-07-07 16:17:48 -0400236{
John Linnb4a3ac92008-10-27 22:17:22 -0400237 struct resource r_irq; /* Interrupt resources */
238 struct resource r_mem; /* IO mem resources */
John Linn11918282008-07-07 16:17:48 -0400239 struct xps2data *drvdata;
240 struct serio *serio;
John Linnb4a3ac92008-10-27 22:17:22 -0400241 struct device *dev = &ofdev->dev;
242 resource_size_t remap_size, phys_addr;
243 int error;
John Linn11918282008-07-07 16:17:48 -0400244
John Linnb4a3ac92008-10-27 22:17:22 -0400245 dev_info(dev, "Device Tree Probing \'%s\'\n",
246 ofdev->node->name);
John Linn11918282008-07-07 16:17:48 -0400247
John Linnb4a3ac92008-10-27 22:17:22 -0400248 /* Get iospace for the device */
249 error = of_address_to_resource(ofdev->node, 0, &r_mem);
250 if (error) {
251 dev_err(dev, "invalid address\n");
252 return error;
253 }
254
255 /* Get IRQ for the device */
256 if (of_irq_to_resource(ofdev->node, 0, &r_irq) == NO_IRQ) {
257 dev_err(dev, "no IRQ found\n");
258 return -ENODEV;
John Linn11918282008-07-07 16:17:48 -0400259 }
260
261 drvdata = kzalloc(sizeof(struct xps2data), GFP_KERNEL);
262 if (!drvdata) {
263 dev_err(dev, "Couldn't allocate device private record\n");
264 return -ENOMEM;
265 }
266
267 dev_set_drvdata(dev, drvdata);
268
269 spin_lock_init(&drvdata->lock);
John Linnb4a3ac92008-10-27 22:17:22 -0400270 drvdata->irq = r_irq.start;
John Linn11918282008-07-07 16:17:48 -0400271
John Linnb4a3ac92008-10-27 22:17:22 -0400272 phys_addr = r_mem.start;
273 remap_size = r_mem.end - r_mem.start + 1;
274 if (!request_mem_region(phys_addr, remap_size, DRIVER_NAME)) {
275 dev_err(dev, "Couldn't lock memory region at 0x%08llX\n",
276 (unsigned long long)phys_addr);
277 error = -EBUSY;
John Linn11918282008-07-07 16:17:48 -0400278 goto failed1;
279 }
280
281 /* Fill in configuration data and add them to the list */
John Linnb4a3ac92008-10-27 22:17:22 -0400282 drvdata->base_address = ioremap(phys_addr, remap_size);
John Linn11918282008-07-07 16:17:48 -0400283 if (drvdata->base_address == NULL) {
John Linnb4a3ac92008-10-27 22:17:22 -0400284 dev_err(dev, "Couldn't ioremap memory at 0x%08llX\n",
285 (unsigned long long)phys_addr);
286 error = -EFAULT;
John Linn11918282008-07-07 16:17:48 -0400287 goto failed2;
288 }
289
290 /* Disable all the interrupts, just in case */
291 out_be32(drvdata->base_address + XPS2_IPIER_OFFSET, 0);
292
293 /* Reset the PS2 device and abort any current transaction, to make sure
294 * we have the PS2 in a good state */
295 out_be32(drvdata->base_address + XPS2_SRST_OFFSET, XPS2_SRST_RESET);
296
John Linnb4a3ac92008-10-27 22:17:22 -0400297 dev_info(dev, "Xilinx PS2 at 0x%08llX mapped to 0x%p, irq=%d\n",
298 (unsigned long long)phys_addr, drvdata->base_address,
299 drvdata->irq);
John Linn11918282008-07-07 16:17:48 -0400300
301 serio = &drvdata->serio;
302 serio->id.type = SERIO_8042;
303 serio->write = sxps2_write;
304 serio->open = sxps2_open;
305 serio->close = sxps2_close;
306 serio->port_data = drvdata;
307 serio->dev.parent = dev;
308 snprintf(serio->name, sizeof(serio->name),
John Linnb4a3ac92008-10-27 22:17:22 -0400309 "Xilinx XPS PS/2 at %08llX", (unsigned long long)phys_addr);
John Linn11918282008-07-07 16:17:48 -0400310 snprintf(serio->phys, sizeof(serio->phys),
John Linnb4a3ac92008-10-27 22:17:22 -0400311 "xilinxps2/serio at %08llX", (unsigned long long)phys_addr);
312
John Linn11918282008-07-07 16:17:48 -0400313 serio_register_port(serio);
314
315 return 0; /* success */
316
317failed2:
John Linnb4a3ac92008-10-27 22:17:22 -0400318 release_mem_region(phys_addr, remap_size);
John Linn11918282008-07-07 16:17:48 -0400319failed1:
320 kfree(drvdata);
321 dev_set_drvdata(dev, NULL);
322
John Linnb4a3ac92008-10-27 22:17:22 -0400323 return error;
John Linn11918282008-07-07 16:17:48 -0400324}
325
John Linnb4a3ac92008-10-27 22:17:22 -0400326/**
327 * xps2_of_remove - unbinds the driver from the PS/2 device.
328 * @of_dev: pointer to OF device structure
329 *
330 * This function is called if a device is physically removed from the system or
331 * if the driver module is being unloaded. It frees any resources allocated to
332 * the device.
333 */
John Linn11918282008-07-07 16:17:48 -0400334static int __devexit xps2_of_remove(struct of_device *of_dev)
335{
336 struct device *dev = &of_dev->dev;
John Linnb4a3ac92008-10-27 22:17:22 -0400337 struct xps2data *drvdata = dev_get_drvdata(dev);
338 struct resource r_mem; /* IO mem resources */
John Linn11918282008-07-07 16:17:48 -0400339
340 serio_unregister_port(&drvdata->serio);
341 iounmap(drvdata->base_address);
John Linnb4a3ac92008-10-27 22:17:22 -0400342
343 /* Get iospace of the device */
344 if (of_address_to_resource(of_dev->node, 0, &r_mem))
345 dev_err(dev, "invalid address\n");
346 else
347 release_mem_region(r_mem.start, r_mem.end - r_mem.start + 1);
348
John Linn11918282008-07-07 16:17:48 -0400349 kfree(drvdata);
350
351 dev_set_drvdata(dev, NULL);
352
John Linnb4a3ac92008-10-27 22:17:22 -0400353 return 0;
John Linn11918282008-07-07 16:17:48 -0400354}
355
356/* Match table for of_platform binding */
357static struct of_device_id xps2_of_match[] __devinitdata = {
358 { .compatible = "xlnx,xps-ps2-1.00.a", },
359 { /* end of list */ },
360};
361MODULE_DEVICE_TABLE(of, xps2_of_match);
362
363static struct of_platform_driver xps2_of_driver = {
364 .name = DRIVER_NAME,
365 .match_table = xps2_of_match,
366 .probe = xps2_of_probe,
367 .remove = __devexit_p(xps2_of_remove),
368};
369
370static int __init xps2_init(void)
371{
372 return of_register_platform_driver(&xps2_of_driver);
373}
374
375static void __exit xps2_cleanup(void)
376{
377 of_unregister_platform_driver(&xps2_of_driver);
378}
379
380module_init(xps2_init);
381module_exit(xps2_cleanup);
382
383MODULE_AUTHOR("Xilinx, Inc.");
384MODULE_DESCRIPTION("Xilinx XPS PS/2 driver");
385MODULE_LICENSE("GPL");
386