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