1
2
3
4
5
6
7
8
9
10
11
12
13
14
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/slab.h>
23#include <linux/list.h>
24#include <linux/io.h>
25#include <linux/of_address.h>
26#include <linux/of_device.h>
27#include <linux/of_irq.h>
28#include <linux/of_platform.h>
29
30#define DRIVER_NAME "xilinx_ps2"
31
32
33#define XPS2_SRST_OFFSET 0x00000000
34#define XPS2_STATUS_OFFSET 0x00000004
35#define XPS2_RX_DATA_OFFSET 0x00000008
36#define XPS2_TX_DATA_OFFSET 0x0000000C
37#define XPS2_GIER_OFFSET 0x0000002C
38#define XPS2_IPISR_OFFSET 0x00000030
39#define XPS2_IPIER_OFFSET 0x00000038
40
41
42#define XPS2_SRST_RESET 0x0000000A
43
44
45#define XPS2_STATUS_RX_FULL 0x00000001
46#define XPS2_STATUS_TX_FULL 0x00000002
47
48
49
50
51
52#define XPS2_IPIXR_WDT_TOUT 0x00000001
53#define XPS2_IPIXR_TX_NOACK 0x00000002
54#define XPS2_IPIXR_TX_ACK 0x00000004
55#define XPS2_IPIXR_RX_OVF 0x00000008
56#define XPS2_IPIXR_RX_ERR 0x00000010
57#define XPS2_IPIXR_RX_FULL 0x00000020
58
59
60#define XPS2_IPIXR_TX_ALL (XPS2_IPIXR_TX_NOACK | XPS2_IPIXR_TX_ACK)
61
62
63#define XPS2_IPIXR_RX_ALL (XPS2_IPIXR_RX_OVF | XPS2_IPIXR_RX_ERR | \
64 XPS2_IPIXR_RX_FULL)
65
66
67#define XPS2_IPIXR_ALL (XPS2_IPIXR_TX_ALL | XPS2_IPIXR_RX_ALL | \
68 XPS2_IPIXR_WDT_TOUT)
69
70
71#define XPS2_GIER_GIE_MASK 0x80000000
72
73struct xps2data {
74 int irq;
75 spinlock_t lock;
76 void __iomem *base_address;
77 unsigned int flags;
78 struct serio *serio;
79 struct device *dev;
80};
81
82
83
84
85
86
87
88
89
90
91
92
93
94static int xps2_recv(struct xps2data *drvdata, u8 *byte)
95{
96 u32 sr;
97 int status = -1;
98
99
100 sr = in_be32(drvdata->base_address + XPS2_STATUS_OFFSET);
101 if (sr & XPS2_STATUS_RX_FULL) {
102 *byte = in_be32(drvdata->base_address + XPS2_RX_DATA_OFFSET);
103 status = 0;
104 }
105
106 return status;
107}
108
109
110
111
112static irqreturn_t xps2_interrupt(int irq, void *dev_id)
113{
114 struct xps2data *drvdata = dev_id;
115 u32 intr_sr;
116 u8 c;
117 int status;
118
119
120 intr_sr = in_be32(drvdata->base_address + XPS2_IPISR_OFFSET);
121 out_be32(drvdata->base_address + XPS2_IPISR_OFFSET, intr_sr);
122
123
124 if (intr_sr & XPS2_IPIXR_RX_OVF)
125 dev_warn(drvdata->dev, "receive overrun error\n");
126
127 if (intr_sr & XPS2_IPIXR_RX_ERR)
128 drvdata->flags |= SERIO_PARITY;
129
130 if (intr_sr & (XPS2_IPIXR_TX_NOACK | XPS2_IPIXR_WDT_TOUT))
131 drvdata->flags |= SERIO_TIMEOUT;
132
133 if (intr_sr & XPS2_IPIXR_RX_FULL) {
134 status = xps2_recv(drvdata, &c);
135
136
137 if (status) {
138 dev_err(drvdata->dev,
139 "wrong rcvd byte count (%d)\n", status);
140 } else {
141 serio_interrupt(drvdata->serio, c, drvdata->flags);
142 drvdata->flags = 0;
143 }
144 }
145
146 return IRQ_HANDLED;
147}
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163static int sxps2_write(struct serio *pserio, unsigned char c)
164{
165 struct xps2data *drvdata = pserio->port_data;
166 unsigned long flags;
167 u32 sr;
168 int status = -1;
169
170 spin_lock_irqsave(&drvdata->lock, flags);
171
172
173 sr = in_be32(drvdata->base_address + XPS2_STATUS_OFFSET);
174 if (!(sr & XPS2_STATUS_TX_FULL)) {
175 out_be32(drvdata->base_address + XPS2_TX_DATA_OFFSET, c);
176 status = 0;
177 }
178
179 spin_unlock_irqrestore(&drvdata->lock, flags);
180
181 return status;
182}
183
184
185
186
187
188
189
190static int sxps2_open(struct serio *pserio)
191{
192 struct xps2data *drvdata = pserio->port_data;
193 int error;
194 u8 c;
195
196 error = request_irq(drvdata->irq, &xps2_interrupt, 0,
197 DRIVER_NAME, drvdata);
198 if (error) {
199 dev_err(drvdata->dev,
200 "Couldn't allocate interrupt %d\n", drvdata->irq);
201 return error;
202 }
203
204
205 out_be32(drvdata->base_address + XPS2_GIER_OFFSET, XPS2_GIER_GIE_MASK);
206 out_be32(drvdata->base_address + XPS2_IPIER_OFFSET, XPS2_IPIXR_RX_ALL);
207 (void)xps2_recv(drvdata, &c);
208
209 return 0;
210}
211
212
213
214
215
216
217
218static void sxps2_close(struct serio *pserio)
219{
220 struct xps2data *drvdata = pserio->port_data;
221
222
223 out_be32(drvdata->base_address + XPS2_GIER_OFFSET, 0x00);
224 out_be32(drvdata->base_address + XPS2_IPIER_OFFSET, 0x00);
225 free_irq(drvdata->irq, drvdata);
226}
227
228
229
230
231
232
233
234
235
236
237
238static int xps2_of_probe(struct platform_device *ofdev)
239{
240 struct resource r_mem;
241 struct xps2data *drvdata;
242 struct serio *serio;
243 struct device *dev = &ofdev->dev;
244 resource_size_t remap_size, phys_addr;
245 unsigned int irq;
246 int error;
247
248 dev_info(dev, "Device Tree Probing \'%s\'\n", dev->of_node->name);
249
250
251 error = of_address_to_resource(dev->of_node, 0, &r_mem);
252 if (error) {
253 dev_err(dev, "invalid address\n");
254 return error;
255 }
256
257
258 irq = irq_of_parse_and_map(dev->of_node, 0);
259 if (!irq) {
260 dev_err(dev, "no IRQ found\n");
261 return -ENODEV;
262 }
263
264 drvdata = kzalloc(sizeof(struct xps2data), GFP_KERNEL);
265 serio = kzalloc(sizeof(struct serio), GFP_KERNEL);
266 if (!drvdata || !serio) {
267 error = -ENOMEM;
268 goto failed1;
269 }
270
271 spin_lock_init(&drvdata->lock);
272 drvdata->irq = irq;
273 drvdata->serio = serio;
274 drvdata->dev = dev;
275
276 phys_addr = r_mem.start;
277 remap_size = resource_size(&r_mem);
278 if (!request_mem_region(phys_addr, remap_size, DRIVER_NAME)) {
279 dev_err(dev, "Couldn't lock memory region at 0x%08llX\n",
280 (unsigned long long)phys_addr);
281 error = -EBUSY;
282 goto failed1;
283 }
284
285
286 drvdata->base_address = ioremap(phys_addr, remap_size);
287 if (drvdata->base_address == NULL) {
288 dev_err(dev, "Couldn't ioremap memory at 0x%08llX\n",
289 (unsigned long long)phys_addr);
290 error = -EFAULT;
291 goto failed2;
292 }
293
294
295 out_be32(drvdata->base_address + XPS2_IPIER_OFFSET, 0);
296
297
298
299
300
301 out_be32(drvdata->base_address + XPS2_SRST_OFFSET, XPS2_SRST_RESET);
302
303 dev_info(dev, "Xilinx PS2 at 0x%08llX mapped to 0x%p, irq=%d\n",
304 (unsigned long long)phys_addr, drvdata->base_address,
305 drvdata->irq);
306
307 serio->id.type = SERIO_8042;
308 serio->write = sxps2_write;
309 serio->open = sxps2_open;
310 serio->close = sxps2_close;
311 serio->port_data = drvdata;
312 serio->dev.parent = dev;
313 snprintf(serio->name, sizeof(serio->name),
314 "Xilinx XPS PS/2 at %08llX", (unsigned long long)phys_addr);
315 snprintf(serio->phys, sizeof(serio->phys),
316 "xilinxps2/serio at %08llX", (unsigned long long)phys_addr);
317
318 serio_register_port(serio);
319
320 platform_set_drvdata(ofdev, drvdata);
321 return 0;
322
323failed2:
324 release_mem_region(phys_addr, remap_size);
325failed1:
326 kfree(serio);
327 kfree(drvdata);
328
329 return error;
330}
331
332
333
334
335
336
337
338
339
340static int xps2_of_remove(struct platform_device *of_dev)
341{
342 struct xps2data *drvdata = platform_get_drvdata(of_dev);
343 struct resource r_mem;
344
345 serio_unregister_port(drvdata->serio);
346 iounmap(drvdata->base_address);
347
348
349 if (of_address_to_resource(of_dev->dev.of_node, 0, &r_mem))
350 dev_err(drvdata->dev, "invalid address\n");
351 else
352 release_mem_region(r_mem.start, resource_size(&r_mem));
353
354 kfree(drvdata);
355
356 return 0;
357}
358
359
360static const struct of_device_id xps2_of_match[] = {
361 { .compatible = "xlnx,xps-ps2-1.00.a", },
362 { },
363};
364MODULE_DEVICE_TABLE(of, xps2_of_match);
365
366static struct platform_driver xps2_of_driver = {
367 .driver = {
368 .name = DRIVER_NAME,
369 .of_match_table = xps2_of_match,
370 },
371 .probe = xps2_of_probe,
372 .remove = xps2_of_remove,
373};
374module_platform_driver(xps2_of_driver);
375
376MODULE_AUTHOR("Xilinx, Inc.");
377MODULE_DESCRIPTION("Xilinx XPS PS/2 driver");
378MODULE_LICENSE("GPL");
379
380