blob: b3e688911fd955106c5ad71d58c3ea87ef95ac9a [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/drivers/input/serio/sa1111ps2.c
3 *
4 * Copyright (C) 2002 Russell King
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License.
9 */
10#include <linux/module.h>
11#include <linux/init.h>
12#include <linux/input.h>
13#include <linux/serio.h>
14#include <linux/errno.h>
15#include <linux/interrupt.h>
16#include <linux/ioport.h>
17#include <linux/delay.h>
18#include <linux/device.h>
19#include <linux/slab.h>
20#include <linux/spinlock.h>
21
22#include <asm/io.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023
24#include <asm/hardware/sa1111.h>
25
Russell King4f8d9ca2012-01-16 11:29:43 +000026#define PS2CR 0x0000
27#define PS2STAT 0x0004
28#define PS2DATA 0x0008
29#define PS2CLKDIV 0x000c
30#define PS2PRECNT 0x0010
31
32#define PS2CR_ENA 0x08
33#define PS2CR_FKD 0x02
34#define PS2CR_FKC 0x01
35
36#define PS2STAT_STP 0x0100
37#define PS2STAT_TXE 0x0080
38#define PS2STAT_TXB 0x0040
39#define PS2STAT_RXF 0x0020
40#define PS2STAT_RXB 0x0010
41#define PS2STAT_ENA 0x0008
42#define PS2STAT_RXP 0x0004
43#define PS2STAT_KBD 0x0002
44#define PS2STAT_KBC 0x0001
45
Linus Torvalds1da177e2005-04-16 15:20:36 -070046struct ps2if {
47 struct serio *io;
48 struct sa1111_dev *dev;
49 void __iomem *base;
50 unsigned int open;
51 spinlock_t lock;
52 unsigned int head;
53 unsigned int tail;
54 unsigned char buf[4];
55};
56
57/*
58 * Read all bytes waiting in the PS2 port. There should be
59 * at the most one, but we loop for safety. If there was a
60 * framing error, we have to manually clear the status.
61 */
David Howells7d12e782006-10-05 14:55:46 +010062static irqreturn_t ps2_rxint(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -070063{
64 struct ps2if *ps2if = dev_id;
65 unsigned int scancode, flag, status;
66
Russell King4f8d9ca2012-01-16 11:29:43 +000067 status = sa1111_readl(ps2if->base + PS2STAT);
Linus Torvalds1da177e2005-04-16 15:20:36 -070068 while (status & PS2STAT_RXF) {
69 if (status & PS2STAT_STP)
Russell King4f8d9ca2012-01-16 11:29:43 +000070 sa1111_writel(PS2STAT_STP, ps2if->base + PS2STAT);
Linus Torvalds1da177e2005-04-16 15:20:36 -070071
72 flag = (status & PS2STAT_STP ? SERIO_FRAME : 0) |
73 (status & PS2STAT_RXP ? 0 : SERIO_PARITY);
74
Russell King4f8d9ca2012-01-16 11:29:43 +000075 scancode = sa1111_readl(ps2if->base + PS2DATA) & 0xff;
Linus Torvalds1da177e2005-04-16 15:20:36 -070076
77 if (hweight8(scancode) & 1)
78 flag ^= SERIO_PARITY;
79
David Howells7d12e782006-10-05 14:55:46 +010080 serio_interrupt(ps2if->io, scancode, flag);
Linus Torvalds1da177e2005-04-16 15:20:36 -070081
Russell King4f8d9ca2012-01-16 11:29:43 +000082 status = sa1111_readl(ps2if->base + PS2STAT);
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 }
84
85 return IRQ_HANDLED;
86}
87
88/*
89 * Completion of ps2 write
90 */
David Howells7d12e782006-10-05 14:55:46 +010091static irqreturn_t ps2_txint(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -070092{
93 struct ps2if *ps2if = dev_id;
94 unsigned int status;
95
96 spin_lock(&ps2if->lock);
Russell King4f8d9ca2012-01-16 11:29:43 +000097 status = sa1111_readl(ps2if->base + PS2STAT);
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 if (ps2if->head == ps2if->tail) {
Ben Nizettee4bd3e52009-04-17 20:35:57 -070099 disable_irq_nosync(irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 /* done */
101 } else if (status & PS2STAT_TXE) {
Russell King4f8d9ca2012-01-16 11:29:43 +0000102 sa1111_writel(ps2if->buf[ps2if->tail], ps2if->base + PS2DATA);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 ps2if->tail = (ps2if->tail + 1) & (sizeof(ps2if->buf) - 1);
104 }
105 spin_unlock(&ps2if->lock);
106
107 return IRQ_HANDLED;
108}
109
110/*
111 * Write a byte to the PS2 port. We have to wait for the
112 * port to indicate that the transmitter is empty.
113 */
114static int ps2_write(struct serio *io, unsigned char val)
115{
116 struct ps2if *ps2if = io->port_data;
117 unsigned long flags;
118 unsigned int head;
119
120 spin_lock_irqsave(&ps2if->lock, flags);
121
122 /*
123 * If the TX register is empty, we can go straight out.
124 */
Russell King4f8d9ca2012-01-16 11:29:43 +0000125 if (sa1111_readl(ps2if->base + PS2STAT) & PS2STAT_TXE) {
126 sa1111_writel(val, ps2if->base + PS2DATA);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 } else {
128 if (ps2if->head == ps2if->tail)
129 enable_irq(ps2if->dev->irq[1]);
130 head = (ps2if->head + 1) & (sizeof(ps2if->buf) - 1);
131 if (head != ps2if->tail) {
132 ps2if->buf[ps2if->head] = val;
133 ps2if->head = head;
134 }
135 }
136
137 spin_unlock_irqrestore(&ps2if->lock, flags);
138 return 0;
139}
140
141static int ps2_open(struct serio *io)
142{
143 struct ps2if *ps2if = io->port_data;
144 int ret;
145
Russell Kingae99ddb2012-01-26 13:25:47 +0000146 ret = sa1111_enable_device(ps2if->dev);
147 if (ret)
148 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149
150 ret = request_irq(ps2if->dev->irq[0], ps2_rxint, 0,
151 SA1111_DRIVER_NAME(ps2if->dev), ps2if);
152 if (ret) {
153 printk(KERN_ERR "sa1111ps2: could not allocate IRQ%d: %d\n",
154 ps2if->dev->irq[0], ret);
Russell Kingae99ddb2012-01-26 13:25:47 +0000155 sa1111_disable_device(ps2if->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 return ret;
157 }
158
159 ret = request_irq(ps2if->dev->irq[1], ps2_txint, 0,
160 SA1111_DRIVER_NAME(ps2if->dev), ps2if);
161 if (ret) {
162 printk(KERN_ERR "sa1111ps2: could not allocate IRQ%d: %d\n",
163 ps2if->dev->irq[1], ret);
164 free_irq(ps2if->dev->irq[0], ps2if);
Russell Kingae99ddb2012-01-26 13:25:47 +0000165 sa1111_disable_device(ps2if->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 return ret;
167 }
168
169 ps2if->open = 1;
170
171 enable_irq_wake(ps2if->dev->irq[0]);
172
Russell King4f8d9ca2012-01-16 11:29:43 +0000173 sa1111_writel(PS2CR_ENA, ps2if->base + PS2CR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 return 0;
175}
176
177static void ps2_close(struct serio *io)
178{
179 struct ps2if *ps2if = io->port_data;
180
Russell King4f8d9ca2012-01-16 11:29:43 +0000181 sa1111_writel(0, ps2if->base + PS2CR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182
183 disable_irq_wake(ps2if->dev->irq[0]);
184
185 ps2if->open = 0;
186
187 free_irq(ps2if->dev->irq[1], ps2if);
188 free_irq(ps2if->dev->irq[0], ps2if);
189
190 sa1111_disable_device(ps2if->dev);
191}
192
193/*
194 * Clear the input buffer.
195 */
Bill Pemberton5298cc42012-11-23 21:38:25 -0800196static void ps2_clear_input(struct ps2if *ps2if)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197{
198 int maxread = 100;
199
200 while (maxread--) {
Russell King4f8d9ca2012-01-16 11:29:43 +0000201 if ((sa1111_readl(ps2if->base + PS2DATA) & 0xff) == 0xff)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 break;
203 }
204}
205
Bill Pemberton5298cc42012-11-23 21:38:25 -0800206static unsigned int ps2_test_one(struct ps2if *ps2if,
Dmitry Torokhov010c33c2009-12-11 21:57:04 -0800207 unsigned int mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208{
209 unsigned int val;
210
Russell King4f8d9ca2012-01-16 11:29:43 +0000211 sa1111_writel(PS2CR_ENA | mask, ps2if->base + PS2CR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212
213 udelay(2);
214
Russell King4f8d9ca2012-01-16 11:29:43 +0000215 val = sa1111_readl(ps2if->base + PS2STAT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 return val & (PS2STAT_KBC | PS2STAT_KBD);
217}
218
219/*
220 * Test the keyboard interface. We basically check to make sure that
221 * we can drive each line to the keyboard independently of each other.
222 */
Bill Pemberton5298cc42012-11-23 21:38:25 -0800223static int ps2_test(struct ps2if *ps2if)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224{
225 unsigned int stat;
226 int ret = 0;
227
228 stat = ps2_test_one(ps2if, PS2CR_FKC);
229 if (stat != PS2STAT_KBD) {
230 printk("PS/2 interface test failed[1]: %02x\n", stat);
231 ret = -ENODEV;
232 }
233
234 stat = ps2_test_one(ps2if, 0);
235 if (stat != (PS2STAT_KBC | PS2STAT_KBD)) {
236 printk("PS/2 interface test failed[2]: %02x\n", stat);
237 ret = -ENODEV;
238 }
239
240 stat = ps2_test_one(ps2if, PS2CR_FKD);
241 if (stat != PS2STAT_KBC) {
242 printk("PS/2 interface test failed[3]: %02x\n", stat);
243 ret = -ENODEV;
244 }
245
Russell King4f8d9ca2012-01-16 11:29:43 +0000246 sa1111_writel(0, ps2if->base + PS2CR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247
248 return ret;
249}
250
251/*
252 * Add one device to this driver.
253 */
Bill Pemberton5298cc42012-11-23 21:38:25 -0800254static int ps2_probe(struct sa1111_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255{
256 struct ps2if *ps2if;
257 struct serio *serio;
258 int ret;
259
Yoann Padioleaudd00cc42007-07-19 01:49:03 -0700260 ps2if = kzalloc(sizeof(struct ps2if), GFP_KERNEL);
261 serio = kzalloc(sizeof(struct serio), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 if (!ps2if || !serio) {
263 ret = -ENOMEM;
264 goto free;
265 }
266
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267
268 serio->id.type = SERIO_8042;
269 serio->write = ps2_write;
270 serio->open = ps2_open;
271 serio->close = ps2_close;
Kay Sievers4e8718a2009-01-29 22:56:08 -0800272 strlcpy(serio->name, dev_name(&dev->dev), sizeof(serio->name));
273 strlcpy(serio->phys, dev_name(&dev->dev), sizeof(serio->phys));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 serio->port_data = ps2if;
275 serio->dev.parent = &dev->dev;
276 ps2if->io = serio;
277 ps2if->dev = dev;
278 sa1111_set_drvdata(dev, ps2if);
279
280 spin_lock_init(&ps2if->lock);
281
282 /*
283 * Request the physical region for this PS2 port.
284 */
285 if (!request_mem_region(dev->res.start,
286 dev->res.end - dev->res.start + 1,
287 SA1111_DRIVER_NAME(dev))) {
288 ret = -EBUSY;
289 goto free;
290 }
291
292 /*
293 * Our parent device has already mapped the region.
294 */
295 ps2if->base = dev->mapbase;
296
297 sa1111_enable_device(ps2if->dev);
298
299 /* Incoming clock is 8MHz */
Russell King4f8d9ca2012-01-16 11:29:43 +0000300 sa1111_writel(0, ps2if->base + PS2CLKDIV);
301 sa1111_writel(127, ps2if->base + PS2PRECNT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302
303 /*
304 * Flush any pending input.
305 */
306 ps2_clear_input(ps2if);
307
308 /*
309 * Test the keyboard interface.
310 */
311 ret = ps2_test(ps2if);
312 if (ret)
313 goto out;
314
315 /*
316 * Flush any pending input.
317 */
318 ps2_clear_input(ps2if);
319
320 sa1111_disable_device(ps2if->dev);
321 serio_register_port(ps2if->io);
322 return 0;
323
324 out:
325 sa1111_disable_device(ps2if->dev);
Joe Perches28f65c112011-06-09 09:13:32 -0700326 release_mem_region(dev->res.start, resource_size(&dev->res));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 free:
328 sa1111_set_drvdata(dev, NULL);
329 kfree(ps2if);
330 kfree(serio);
331 return ret;
332}
333
334/*
335 * Remove one device from this driver.
336 */
Bill Pembertone2619cf2012-11-23 21:50:47 -0800337static int ps2_remove(struct sa1111_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338{
339 struct ps2if *ps2if = sa1111_get_drvdata(dev);
340
341 serio_unregister_port(ps2if->io);
Joe Perches28f65c112011-06-09 09:13:32 -0700342 release_mem_region(dev->res.start, resource_size(&dev->res));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 sa1111_set_drvdata(dev, NULL);
344
345 kfree(ps2if);
346
347 return 0;
348}
349
350/*
351 * Our device driver structure
352 */
353static struct sa1111_driver ps2_driver = {
354 .drv = {
355 .name = "sa1111-ps2",
Russell King1ebcd762012-01-26 11:19:48 +0000356 .owner = THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 },
358 .devid = SA1111_DEVID_PS2,
359 .probe = ps2_probe,
Bill Pemberton1cb0aa82012-11-23 21:27:39 -0800360 .remove = ps2_remove,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361};
362
363static int __init ps2_init(void)
364{
365 return sa1111_driver_register(&ps2_driver);
366}
367
368static void __exit ps2_exit(void)
369{
370 sa1111_driver_unregister(&ps2_driver);
371}
372
373module_init(ps2_init);
374module_exit(ps2_exit);
375
376MODULE_AUTHOR("Russell King <rmk@arm.linux.org.uk>");
377MODULE_DESCRIPTION("SA1111 PS2 controller driver");
378MODULE_LICENSE("GPL");