blob: b90eb4fb2b5be66a216d843b5ce61b33f7904c69 [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>
Rob Herring5af50732013-09-17 14:28:33 -050028#include <linux/of_irq.h>
John Linn11918282008-07-07 16:17:48 -040029#include <linux/of_platform.h>
30
31#define DRIVER_NAME "xilinx_ps2"
32
33/* Register offsets for the xps2 device */
34#define XPS2_SRST_OFFSET 0x00000000 /* Software Reset register */
35#define XPS2_STATUS_OFFSET 0x00000004 /* Status register */
36#define XPS2_RX_DATA_OFFSET 0x00000008 /* Receive Data register */
37#define XPS2_TX_DATA_OFFSET 0x0000000C /* Transmit Data register */
38#define XPS2_GIER_OFFSET 0x0000002C /* Global Interrupt Enable reg */
39#define XPS2_IPISR_OFFSET 0x00000030 /* Interrupt Status register */
40#define XPS2_IPIER_OFFSET 0x00000038 /* Interrupt Enable register */
41
42/* Reset Register Bit Definitions */
43#define XPS2_SRST_RESET 0x0000000A /* Software Reset */
44
45/* Status Register Bit Positions */
46#define XPS2_STATUS_RX_FULL 0x00000001 /* Receive Full */
47#define XPS2_STATUS_TX_FULL 0x00000002 /* Transmit Full */
48
49/* Bit definitions for ISR/IER registers. Both the registers have the same bit
50 * definitions and are only defined once. */
51#define XPS2_IPIXR_WDT_TOUT 0x00000001 /* Watchdog Timeout Interrupt */
52#define XPS2_IPIXR_TX_NOACK 0x00000002 /* Transmit No ACK Interrupt */
53#define XPS2_IPIXR_TX_ACK 0x00000004 /* Transmit ACK (Data) Interrupt */
54#define XPS2_IPIXR_RX_OVF 0x00000008 /* Receive Overflow Interrupt */
55#define XPS2_IPIXR_RX_ERR 0x00000010 /* Receive Error Interrupt */
56#define XPS2_IPIXR_RX_FULL 0x00000020 /* Receive Data Interrupt */
57
58/* Mask for all the Transmit Interrupts */
59#define XPS2_IPIXR_TX_ALL (XPS2_IPIXR_TX_NOACK | XPS2_IPIXR_TX_ACK)
60
61/* Mask for all the Receive Interrupts */
62#define XPS2_IPIXR_RX_ALL (XPS2_IPIXR_RX_OVF | XPS2_IPIXR_RX_ERR | \
John Linnb4a3ac92008-10-27 22:17:22 -040063 XPS2_IPIXR_RX_FULL)
John Linn11918282008-07-07 16:17:48 -040064
65/* Mask for all the Interrupts */
66#define XPS2_IPIXR_ALL (XPS2_IPIXR_TX_ALL | XPS2_IPIXR_RX_ALL | \
John Linnb4a3ac92008-10-27 22:17:22 -040067 XPS2_IPIXR_WDT_TOUT)
John Linn11918282008-07-07 16:17:48 -040068
69/* Global Interrupt Enable mask */
70#define XPS2_GIER_GIE_MASK 0x80000000
71
72struct xps2data {
73 int irq;
John Linn11918282008-07-07 16:17:48 -040074 spinlock_t lock;
John Linn11918282008-07-07 16:17:48 -040075 void __iomem *base_address; /* virt. address of control registers */
John Linnb4a3ac92008-10-27 22:17:22 -040076 unsigned int flags;
Dmitry Torokhov271002c2012-03-25 17:23:19 -070077 struct serio *serio; /* serio */
78 struct device *dev;
John Linn11918282008-07-07 16:17:48 -040079};
80
81/************************************/
82/* XPS PS/2 data transmission calls */
83/************************************/
84
John Linnb4a3ac92008-10-27 22:17:22 -040085/**
86 * xps2_recv() - attempts to receive a byte from the PS/2 port.
87 * @drvdata: pointer to ps2 device private data structure
88 * @byte: address where the read data will be copied
89 *
90 * If there is any data available in the PS/2 receiver, this functions reads
91 * the data, otherwise it returns error.
John Linn11918282008-07-07 16:17:48 -040092 */
93static int xps2_recv(struct xps2data *drvdata, u8 *byte)
94{
95 u32 sr;
96 int status = -1;
97
98 /* If there is data available in the PS/2 receiver, read it */
99 sr = in_be32(drvdata->base_address + XPS2_STATUS_OFFSET);
100 if (sr & XPS2_STATUS_RX_FULL) {
101 *byte = in_be32(drvdata->base_address + XPS2_RX_DATA_OFFSET);
102 status = 0;
103 }
104
105 return status;
106}
107
108/*********************/
109/* Interrupt handler */
110/*********************/
111static irqreturn_t xps2_interrupt(int irq, void *dev_id)
112{
113 struct xps2data *drvdata = dev_id;
114 u32 intr_sr;
115 u8 c;
116 int status;
117
118 /* Get the PS/2 interrupts and clear them */
119 intr_sr = in_be32(drvdata->base_address + XPS2_IPISR_OFFSET);
120 out_be32(drvdata->base_address + XPS2_IPISR_OFFSET, intr_sr);
121
122 /* Check which interrupt is active */
123 if (intr_sr & XPS2_IPIXR_RX_OVF)
Dmitry Torokhov271002c2012-03-25 17:23:19 -0700124 dev_warn(drvdata->dev, "receive overrun error\n");
John Linn11918282008-07-07 16:17:48 -0400125
126 if (intr_sr & XPS2_IPIXR_RX_ERR)
John Linnb4a3ac92008-10-27 22:17:22 -0400127 drvdata->flags |= SERIO_PARITY;
John Linn11918282008-07-07 16:17:48 -0400128
129 if (intr_sr & (XPS2_IPIXR_TX_NOACK | XPS2_IPIXR_WDT_TOUT))
John Linnb4a3ac92008-10-27 22:17:22 -0400130 drvdata->flags |= SERIO_TIMEOUT;
John Linn11918282008-07-07 16:17:48 -0400131
132 if (intr_sr & XPS2_IPIXR_RX_FULL) {
John Linnb4a3ac92008-10-27 22:17:22 -0400133 status = xps2_recv(drvdata, &c);
John Linn11918282008-07-07 16:17:48 -0400134
135 /* Error, if a byte is not received */
136 if (status) {
Dmitry Torokhov271002c2012-03-25 17:23:19 -0700137 dev_err(drvdata->dev,
John Linnb4a3ac92008-10-27 22:17:22 -0400138 "wrong rcvd byte count (%d)\n", status);
John Linn11918282008-07-07 16:17:48 -0400139 } else {
Dmitry Torokhov271002c2012-03-25 17:23:19 -0700140 serio_interrupt(drvdata->serio, c, drvdata->flags);
John Linnb4a3ac92008-10-27 22:17:22 -0400141 drvdata->flags = 0;
John Linn11918282008-07-07 16:17:48 -0400142 }
143 }
144
John Linn11918282008-07-07 16:17:48 -0400145 return IRQ_HANDLED;
146}
147
148/*******************/
149/* serio callbacks */
150/*******************/
151
John Linnb4a3ac92008-10-27 22:17:22 -0400152/**
153 * sxps2_write() - sends a byte out through the PS/2 port.
154 * @pserio: pointer to the serio structure of the PS/2 port
155 * @c: data that needs to be written to the PS/2 port
156 *
157 * This function checks if the PS/2 transmitter is empty and sends a byte.
158 * Otherwise it returns error. Transmission fails only when nothing is connected
159 * to the PS/2 port. Thats why, we do not try to resend the data in case of a
160 * failure.
John Linn11918282008-07-07 16:17:48 -0400161 */
162static int sxps2_write(struct serio *pserio, unsigned char c)
163{
164 struct xps2data *drvdata = pserio->port_data;
165 unsigned long flags;
166 u32 sr;
167 int status = -1;
168
169 spin_lock_irqsave(&drvdata->lock, flags);
170
171 /* If the PS/2 transmitter is empty send a byte of data */
172 sr = in_be32(drvdata->base_address + XPS2_STATUS_OFFSET);
173 if (!(sr & XPS2_STATUS_TX_FULL)) {
174 out_be32(drvdata->base_address + XPS2_TX_DATA_OFFSET, c);
175 status = 0;
176 }
177
178 spin_unlock_irqrestore(&drvdata->lock, flags);
179
180 return status;
181}
182
John Linnb4a3ac92008-10-27 22:17:22 -0400183/**
184 * sxps2_open() - called when a port is opened by the higher layer.
185 * @pserio: pointer to the serio structure of the PS/2 device
186 *
187 * This function requests irq and enables interrupts for the PS/2 device.
John Linn11918282008-07-07 16:17:48 -0400188 */
189static int sxps2_open(struct serio *pserio)
190{
191 struct xps2data *drvdata = pserio->port_data;
John Linnb4a3ac92008-10-27 22:17:22 -0400192 int error;
193 u8 c;
John Linn11918282008-07-07 16:17:48 -0400194
John Linnb4a3ac92008-10-27 22:17:22 -0400195 error = request_irq(drvdata->irq, &xps2_interrupt, 0,
John Linn11918282008-07-07 16:17:48 -0400196 DRIVER_NAME, drvdata);
John Linnb4a3ac92008-10-27 22:17:22 -0400197 if (error) {
Dmitry Torokhov271002c2012-03-25 17:23:19 -0700198 dev_err(drvdata->dev,
John Linnb4a3ac92008-10-27 22:17:22 -0400199 "Couldn't allocate interrupt %d\n", drvdata->irq);
200 return error;
John Linn11918282008-07-07 16:17:48 -0400201 }
202
203 /* start reception by enabling the interrupts */
204 out_be32(drvdata->base_address + XPS2_GIER_OFFSET, XPS2_GIER_GIE_MASK);
205 out_be32(drvdata->base_address + XPS2_IPIER_OFFSET, XPS2_IPIXR_RX_ALL);
John Linnb4a3ac92008-10-27 22:17:22 -0400206 (void)xps2_recv(drvdata, &c);
John Linn11918282008-07-07 16:17:48 -0400207
208 return 0; /* success */
209}
210
John Linnb4a3ac92008-10-27 22:17:22 -0400211/**
212 * sxps2_close() - frees the interrupt.
213 * @pserio: pointer to the serio structure of the PS/2 device
214 *
215 * This function frees the irq and disables interrupts for the PS/2 device.
John Linn11918282008-07-07 16:17:48 -0400216 */
217static void sxps2_close(struct serio *pserio)
218{
219 struct xps2data *drvdata = pserio->port_data;
220
221 /* Disable the PS2 interrupts */
222 out_be32(drvdata->base_address + XPS2_GIER_OFFSET, 0x00);
223 out_be32(drvdata->base_address + XPS2_IPIER_OFFSET, 0x00);
224 free_irq(drvdata->irq, drvdata);
225}
226
John Linnb4a3ac92008-10-27 22:17:22 -0400227/**
228 * xps2_of_probe - probe method for the PS/2 device.
229 * @of_dev: pointer to OF device structure
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300230 * @match: pointer to the structure used for matching a device
John Linnb4a3ac92008-10-27 22:17:22 -0400231 *
232 * This function probes the PS/2 device in the device tree.
233 * It initializes the driver data structure and the hardware.
234 * It returns 0, if the driver is bound to the PS/2 device, or a negative
235 * value if there is an error.
236 */
Bill Pemberton5298cc42012-11-23 21:38:25 -0800237static int xps2_of_probe(struct platform_device *ofdev)
John Linn11918282008-07-07 16:17:48 -0400238{
John Linnb4a3ac92008-10-27 22:17:22 -0400239 struct resource r_irq; /* Interrupt resources */
240 struct resource r_mem; /* IO mem resources */
John Linn11918282008-07-07 16:17:48 -0400241 struct xps2data *drvdata;
242 struct serio *serio;
John Linnb4a3ac92008-10-27 22:17:22 -0400243 struct device *dev = &ofdev->dev;
244 resource_size_t remap_size, phys_addr;
245 int error;
John Linn11918282008-07-07 16:17:48 -0400246
John Linnb4a3ac92008-10-27 22:17:22 -0400247 dev_info(dev, "Device Tree Probing \'%s\'\n",
Grant Likely61c7a082010-04-13 16:12:29 -0700248 ofdev->dev.of_node->name);
John Linn11918282008-07-07 16:17:48 -0400249
John Linnb4a3ac92008-10-27 22:17:22 -0400250 /* Get iospace for the device */
Grant Likely61c7a082010-04-13 16:12:29 -0700251 error = of_address_to_resource(ofdev->dev.of_node, 0, &r_mem);
John Linnb4a3ac92008-10-27 22:17:22 -0400252 if (error) {
253 dev_err(dev, "invalid address\n");
254 return error;
255 }
256
257 /* Get IRQ for the device */
Michal Simekbe3bad62011-12-21 13:28:23 +0100258 if (!of_irq_to_resource(ofdev->dev.of_node, 0, &r_irq)) {
John Linnb4a3ac92008-10-27 22:17:22 -0400259 dev_err(dev, "no IRQ found\n");
260 return -ENODEV;
John Linn11918282008-07-07 16:17:48 -0400261 }
262
263 drvdata = kzalloc(sizeof(struct xps2data), GFP_KERNEL);
Dmitry Torokhov271002c2012-03-25 17:23:19 -0700264 serio = kzalloc(sizeof(struct serio), GFP_KERNEL);
265 if (!drvdata || !serio) {
266 error = -ENOMEM;
267 goto failed1;
John Linn11918282008-07-07 16:17:48 -0400268 }
269
John Linn11918282008-07-07 16:17:48 -0400270 spin_lock_init(&drvdata->lock);
John Linnb4a3ac92008-10-27 22:17:22 -0400271 drvdata->irq = r_irq.start;
Dmitry Torokhov271002c2012-03-25 17:23:19 -0700272 drvdata->serio = serio;
273 drvdata->dev = dev;
John Linn11918282008-07-07 16:17:48 -0400274
John Linnb4a3ac92008-10-27 22:17:22 -0400275 phys_addr = r_mem.start;
Tobias Klauserce841b92010-01-21 23:52:37 -0800276 remap_size = resource_size(&r_mem);
John Linnb4a3ac92008-10-27 22:17:22 -0400277 if (!request_mem_region(phys_addr, remap_size, DRIVER_NAME)) {
278 dev_err(dev, "Couldn't lock memory region at 0x%08llX\n",
279 (unsigned long long)phys_addr);
280 error = -EBUSY;
John Linn11918282008-07-07 16:17:48 -0400281 goto failed1;
282 }
283
284 /* Fill in configuration data and add them to the list */
John Linnb4a3ac92008-10-27 22:17:22 -0400285 drvdata->base_address = ioremap(phys_addr, remap_size);
John Linn11918282008-07-07 16:17:48 -0400286 if (drvdata->base_address == NULL) {
John Linnb4a3ac92008-10-27 22:17:22 -0400287 dev_err(dev, "Couldn't ioremap memory at 0x%08llX\n",
288 (unsigned long long)phys_addr);
289 error = -EFAULT;
John Linn11918282008-07-07 16:17:48 -0400290 goto failed2;
291 }
292
293 /* Disable all the interrupts, just in case */
294 out_be32(drvdata->base_address + XPS2_IPIER_OFFSET, 0);
295
296 /* Reset the PS2 device and abort any current transaction, to make sure
297 * we have the PS2 in a good state */
298 out_be32(drvdata->base_address + XPS2_SRST_OFFSET, XPS2_SRST_RESET);
299
John Linnb4a3ac92008-10-27 22:17:22 -0400300 dev_info(dev, "Xilinx PS2 at 0x%08llX mapped to 0x%p, irq=%d\n",
301 (unsigned long long)phys_addr, drvdata->base_address,
302 drvdata->irq);
John Linn11918282008-07-07 16:17:48 -0400303
John Linn11918282008-07-07 16:17:48 -0400304 serio->id.type = SERIO_8042;
305 serio->write = sxps2_write;
306 serio->open = sxps2_open;
307 serio->close = sxps2_close;
308 serio->port_data = drvdata;
309 serio->dev.parent = dev;
310 snprintf(serio->name, sizeof(serio->name),
John Linnb4a3ac92008-10-27 22:17:22 -0400311 "Xilinx XPS PS/2 at %08llX", (unsigned long long)phys_addr);
John Linn11918282008-07-07 16:17:48 -0400312 snprintf(serio->phys, sizeof(serio->phys),
John Linnb4a3ac92008-10-27 22:17:22 -0400313 "xilinxps2/serio at %08llX", (unsigned long long)phys_addr);
314
John Linn11918282008-07-07 16:17:48 -0400315 serio_register_port(serio);
316
Dmitry Torokhov271002c2012-03-25 17:23:19 -0700317 platform_set_drvdata(ofdev, drvdata);
John Linn11918282008-07-07 16:17:48 -0400318 return 0; /* success */
319
320failed2:
John Linnb4a3ac92008-10-27 22:17:22 -0400321 release_mem_region(phys_addr, remap_size);
John Linn11918282008-07-07 16:17:48 -0400322failed1:
Dmitry Torokhov271002c2012-03-25 17:23:19 -0700323 kfree(serio);
John Linn11918282008-07-07 16:17:48 -0400324 kfree(drvdata);
John Linn11918282008-07-07 16:17:48 -0400325
John Linnb4a3ac92008-10-27 22:17:22 -0400326 return error;
John Linn11918282008-07-07 16:17:48 -0400327}
328
John Linnb4a3ac92008-10-27 22:17:22 -0400329/**
330 * xps2_of_remove - unbinds the driver from the PS/2 device.
331 * @of_dev: pointer to OF device structure
332 *
333 * This function is called if a device is physically removed from the system or
334 * if the driver module is being unloaded. It frees any resources allocated to
335 * the device.
336 */
Bill Pembertone2619cf2012-11-23 21:50:47 -0800337static int xps2_of_remove(struct platform_device *of_dev)
John Linn11918282008-07-07 16:17:48 -0400338{
Dmitry Torokhov271002c2012-03-25 17:23:19 -0700339 struct xps2data *drvdata = platform_get_drvdata(of_dev);
John Linnb4a3ac92008-10-27 22:17:22 -0400340 struct resource r_mem; /* IO mem resources */
John Linn11918282008-07-07 16:17:48 -0400341
Dmitry Torokhov271002c2012-03-25 17:23:19 -0700342 serio_unregister_port(drvdata->serio);
John Linn11918282008-07-07 16:17:48 -0400343 iounmap(drvdata->base_address);
John Linnb4a3ac92008-10-27 22:17:22 -0400344
345 /* Get iospace of the device */
Grant Likely61c7a082010-04-13 16:12:29 -0700346 if (of_address_to_resource(of_dev->dev.of_node, 0, &r_mem))
Dmitry Torokhov271002c2012-03-25 17:23:19 -0700347 dev_err(drvdata->dev, "invalid address\n");
John Linnb4a3ac92008-10-27 22:17:22 -0400348 else
Tobias Klauserce841b92010-01-21 23:52:37 -0800349 release_mem_region(r_mem.start, resource_size(&r_mem));
John Linnb4a3ac92008-10-27 22:17:22 -0400350
John Linn11918282008-07-07 16:17:48 -0400351 kfree(drvdata);
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 */
Bill Pemberton78f50c22012-11-23 21:31:00 -0800357static const struct of_device_id xps2_of_match[] = {
John Linn11918282008-07-07 16:17:48 -0400358 { .compatible = "xlnx,xps-ps2-1.00.a", },
359 { /* end of list */ },
360};
361MODULE_DEVICE_TABLE(of, xps2_of_match);
362
Grant Likely1c48a5c2011-02-17 02:43:24 -0700363static struct platform_driver xps2_of_driver = {
Grant Likely40182942010-04-13 16:13:02 -0700364 .driver = {
365 .name = DRIVER_NAME,
366 .owner = THIS_MODULE,
367 .of_match_table = xps2_of_match,
368 },
John Linn11918282008-07-07 16:17:48 -0400369 .probe = xps2_of_probe,
Bill Pemberton1cb0aa82012-11-23 21:27:39 -0800370 .remove = xps2_of_remove,
John Linn11918282008-07-07 16:17:48 -0400371};
JJ Ding24d24692011-11-29 11:08:43 -0800372module_platform_driver(xps2_of_driver);
John Linn11918282008-07-07 16:17:48 -0400373
374MODULE_AUTHOR("Xilinx, Inc.");
375MODULE_DESCRIPTION("Xilinx XPS PS/2 driver");
376MODULE_LICENSE("GPL");
377