blob: 5ebabe3fc84521e2ad18149f3f3092a8be686231 [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#include <asm/system.h>
24
25#include <asm/hardware/sa1111.h>
26
Russell King4f8d9ca2012-01-16 11:29:43 +000027#define PS2CR 0x0000
28#define PS2STAT 0x0004
29#define PS2DATA 0x0008
30#define PS2CLKDIV 0x000c
31#define PS2PRECNT 0x0010
32
33#define PS2CR_ENA 0x08
34#define PS2CR_FKD 0x02
35#define PS2CR_FKC 0x01
36
37#define PS2STAT_STP 0x0100
38#define PS2STAT_TXE 0x0080
39#define PS2STAT_TXB 0x0040
40#define PS2STAT_RXF 0x0020
41#define PS2STAT_RXB 0x0010
42#define PS2STAT_ENA 0x0008
43#define PS2STAT_RXP 0x0004
44#define PS2STAT_KBD 0x0002
45#define PS2STAT_KBC 0x0001
46
Linus Torvalds1da177e2005-04-16 15:20:36 -070047struct ps2if {
48 struct serio *io;
49 struct sa1111_dev *dev;
50 void __iomem *base;
51 unsigned int open;
52 spinlock_t lock;
53 unsigned int head;
54 unsigned int tail;
55 unsigned char buf[4];
56};
57
58/*
59 * Read all bytes waiting in the PS2 port. There should be
60 * at the most one, but we loop for safety. If there was a
61 * framing error, we have to manually clear the status.
62 */
David Howells7d12e782006-10-05 14:55:46 +010063static irqreturn_t ps2_rxint(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -070064{
65 struct ps2if *ps2if = dev_id;
66 unsigned int scancode, flag, status;
67
Russell King4f8d9ca2012-01-16 11:29:43 +000068 status = sa1111_readl(ps2if->base + PS2STAT);
Linus Torvalds1da177e2005-04-16 15:20:36 -070069 while (status & PS2STAT_RXF) {
70 if (status & PS2STAT_STP)
Russell King4f8d9ca2012-01-16 11:29:43 +000071 sa1111_writel(PS2STAT_STP, ps2if->base + PS2STAT);
Linus Torvalds1da177e2005-04-16 15:20:36 -070072
73 flag = (status & PS2STAT_STP ? SERIO_FRAME : 0) |
74 (status & PS2STAT_RXP ? 0 : SERIO_PARITY);
75
Russell King4f8d9ca2012-01-16 11:29:43 +000076 scancode = sa1111_readl(ps2if->base + PS2DATA) & 0xff;
Linus Torvalds1da177e2005-04-16 15:20:36 -070077
78 if (hweight8(scancode) & 1)
79 flag ^= SERIO_PARITY;
80
David Howells7d12e782006-10-05 14:55:46 +010081 serio_interrupt(ps2if->io, scancode, flag);
Linus Torvalds1da177e2005-04-16 15:20:36 -070082
Russell King4f8d9ca2012-01-16 11:29:43 +000083 status = sa1111_readl(ps2if->base + PS2STAT);
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 }
85
86 return IRQ_HANDLED;
87}
88
89/*
90 * Completion of ps2 write
91 */
David Howells7d12e782006-10-05 14:55:46 +010092static irqreturn_t ps2_txint(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -070093{
94 struct ps2if *ps2if = dev_id;
95 unsigned int status;
96
97 spin_lock(&ps2if->lock);
Russell King4f8d9ca2012-01-16 11:29:43 +000098 status = sa1111_readl(ps2if->base + PS2STAT);
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 if (ps2if->head == ps2if->tail) {
Ben Nizettee4bd3e52009-04-17 20:35:57 -0700100 disable_irq_nosync(irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 /* done */
102 } else if (status & PS2STAT_TXE) {
Russell King4f8d9ca2012-01-16 11:29:43 +0000103 sa1111_writel(ps2if->buf[ps2if->tail], ps2if->base + PS2DATA);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 ps2if->tail = (ps2if->tail + 1) & (sizeof(ps2if->buf) - 1);
105 }
106 spin_unlock(&ps2if->lock);
107
108 return IRQ_HANDLED;
109}
110
111/*
112 * Write a byte to the PS2 port. We have to wait for the
113 * port to indicate that the transmitter is empty.
114 */
115static int ps2_write(struct serio *io, unsigned char val)
116{
117 struct ps2if *ps2if = io->port_data;
118 unsigned long flags;
119 unsigned int head;
120
121 spin_lock_irqsave(&ps2if->lock, flags);
122
123 /*
124 * If the TX register is empty, we can go straight out.
125 */
Russell King4f8d9ca2012-01-16 11:29:43 +0000126 if (sa1111_readl(ps2if->base + PS2STAT) & PS2STAT_TXE) {
127 sa1111_writel(val, ps2if->base + PS2DATA);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 } else {
129 if (ps2if->head == ps2if->tail)
130 enable_irq(ps2if->dev->irq[1]);
131 head = (ps2if->head + 1) & (sizeof(ps2if->buf) - 1);
132 if (head != ps2if->tail) {
133 ps2if->buf[ps2if->head] = val;
134 ps2if->head = head;
135 }
136 }
137
138 spin_unlock_irqrestore(&ps2if->lock, flags);
139 return 0;
140}
141
142static int ps2_open(struct serio *io)
143{
144 struct ps2if *ps2if = io->port_data;
145 int ret;
146
Russell Kingae99ddb2012-01-26 13:25:47 +0000147 ret = sa1111_enable_device(ps2if->dev);
148 if (ret)
149 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150
151 ret = request_irq(ps2if->dev->irq[0], ps2_rxint, 0,
152 SA1111_DRIVER_NAME(ps2if->dev), ps2if);
153 if (ret) {
154 printk(KERN_ERR "sa1111ps2: could not allocate IRQ%d: %d\n",
155 ps2if->dev->irq[0], ret);
Russell Kingae99ddb2012-01-26 13:25:47 +0000156 sa1111_disable_device(ps2if->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 return ret;
158 }
159
160 ret = request_irq(ps2if->dev->irq[1], ps2_txint, 0,
161 SA1111_DRIVER_NAME(ps2if->dev), ps2if);
162 if (ret) {
163 printk(KERN_ERR "sa1111ps2: could not allocate IRQ%d: %d\n",
164 ps2if->dev->irq[1], ret);
165 free_irq(ps2if->dev->irq[0], ps2if);
Russell Kingae99ddb2012-01-26 13:25:47 +0000166 sa1111_disable_device(ps2if->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 return ret;
168 }
169
170 ps2if->open = 1;
171
172 enable_irq_wake(ps2if->dev->irq[0]);
173
Russell King4f8d9ca2012-01-16 11:29:43 +0000174 sa1111_writel(PS2CR_ENA, ps2if->base + PS2CR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 return 0;
176}
177
178static void ps2_close(struct serio *io)
179{
180 struct ps2if *ps2if = io->port_data;
181
Russell King4f8d9ca2012-01-16 11:29:43 +0000182 sa1111_writel(0, ps2if->base + PS2CR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183
184 disable_irq_wake(ps2if->dev->irq[0]);
185
186 ps2if->open = 0;
187
188 free_irq(ps2if->dev->irq[1], ps2if);
189 free_irq(ps2if->dev->irq[0], ps2if);
190
191 sa1111_disable_device(ps2if->dev);
192}
193
194/*
195 * Clear the input buffer.
196 */
Russell Kingcdea4602007-05-30 17:48:45 +0100197static void __devinit ps2_clear_input(struct ps2if *ps2if)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198{
199 int maxread = 100;
200
201 while (maxread--) {
Russell King4f8d9ca2012-01-16 11:29:43 +0000202 if ((sa1111_readl(ps2if->base + PS2DATA) & 0xff) == 0xff)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 break;
204 }
205}
206
Dmitry Torokhov010c33c2009-12-11 21:57:04 -0800207static unsigned int __devinit ps2_test_one(struct ps2if *ps2if,
208 unsigned int mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209{
210 unsigned int val;
211
Russell King4f8d9ca2012-01-16 11:29:43 +0000212 sa1111_writel(PS2CR_ENA | mask, ps2if->base + PS2CR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213
214 udelay(2);
215
Russell King4f8d9ca2012-01-16 11:29:43 +0000216 val = sa1111_readl(ps2if->base + PS2STAT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 return val & (PS2STAT_KBC | PS2STAT_KBD);
218}
219
220/*
221 * Test the keyboard interface. We basically check to make sure that
222 * we can drive each line to the keyboard independently of each other.
223 */
Dmitry Torokhov010c33c2009-12-11 21:57:04 -0800224static int __devinit ps2_test(struct ps2if *ps2if)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225{
226 unsigned int stat;
227 int ret = 0;
228
229 stat = ps2_test_one(ps2if, PS2CR_FKC);
230 if (stat != PS2STAT_KBD) {
231 printk("PS/2 interface test failed[1]: %02x\n", stat);
232 ret = -ENODEV;
233 }
234
235 stat = ps2_test_one(ps2if, 0);
236 if (stat != (PS2STAT_KBC | PS2STAT_KBD)) {
237 printk("PS/2 interface test failed[2]: %02x\n", stat);
238 ret = -ENODEV;
239 }
240
241 stat = ps2_test_one(ps2if, PS2CR_FKD);
242 if (stat != PS2STAT_KBC) {
243 printk("PS/2 interface test failed[3]: %02x\n", stat);
244 ret = -ENODEV;
245 }
246
Russell King4f8d9ca2012-01-16 11:29:43 +0000247 sa1111_writel(0, ps2if->base + PS2CR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248
249 return ret;
250}
251
252/*
253 * Add one device to this driver.
254 */
Russell Kingcdea4602007-05-30 17:48:45 +0100255static int __devinit ps2_probe(struct sa1111_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256{
257 struct ps2if *ps2if;
258 struct serio *serio;
259 int ret;
260
Yoann Padioleaudd00cc42007-07-19 01:49:03 -0700261 ps2if = kzalloc(sizeof(struct ps2if), GFP_KERNEL);
262 serio = kzalloc(sizeof(struct serio), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 if (!ps2if || !serio) {
264 ret = -ENOMEM;
265 goto free;
266 }
267
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268
269 serio->id.type = SERIO_8042;
270 serio->write = ps2_write;
271 serio->open = ps2_open;
272 serio->close = ps2_close;
Kay Sievers4e8718a2009-01-29 22:56:08 -0800273 strlcpy(serio->name, dev_name(&dev->dev), sizeof(serio->name));
274 strlcpy(serio->phys, dev_name(&dev->dev), sizeof(serio->phys));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 serio->port_data = ps2if;
276 serio->dev.parent = &dev->dev;
277 ps2if->io = serio;
278 ps2if->dev = dev;
279 sa1111_set_drvdata(dev, ps2if);
280
281 spin_lock_init(&ps2if->lock);
282
283 /*
284 * Request the physical region for this PS2 port.
285 */
286 if (!request_mem_region(dev->res.start,
287 dev->res.end - dev->res.start + 1,
288 SA1111_DRIVER_NAME(dev))) {
289 ret = -EBUSY;
290 goto free;
291 }
292
293 /*
294 * Our parent device has already mapped the region.
295 */
296 ps2if->base = dev->mapbase;
297
298 sa1111_enable_device(ps2if->dev);
299
300 /* Incoming clock is 8MHz */
Russell King4f8d9ca2012-01-16 11:29:43 +0000301 sa1111_writel(0, ps2if->base + PS2CLKDIV);
302 sa1111_writel(127, ps2if->base + PS2PRECNT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303
304 /*
305 * Flush any pending input.
306 */
307 ps2_clear_input(ps2if);
308
309 /*
310 * Test the keyboard interface.
311 */
312 ret = ps2_test(ps2if);
313 if (ret)
314 goto out;
315
316 /*
317 * Flush any pending input.
318 */
319 ps2_clear_input(ps2if);
320
321 sa1111_disable_device(ps2if->dev);
322 serio_register_port(ps2if->io);
323 return 0;
324
325 out:
326 sa1111_disable_device(ps2if->dev);
Joe Perches28f65c12011-06-09 09:13:32 -0700327 release_mem_region(dev->res.start, resource_size(&dev->res));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 free:
329 sa1111_set_drvdata(dev, NULL);
330 kfree(ps2if);
331 kfree(serio);
332 return ret;
333}
334
335/*
336 * Remove one device from this driver.
337 */
Dmitry Torokhov010c33c2009-12-11 21:57:04 -0800338static int __devexit ps2_remove(struct sa1111_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339{
340 struct ps2if *ps2if = sa1111_get_drvdata(dev);
341
342 serio_unregister_port(ps2if->io);
Joe Perches28f65c12011-06-09 09:13:32 -0700343 release_mem_region(dev->res.start, resource_size(&dev->res));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 sa1111_set_drvdata(dev, NULL);
345
346 kfree(ps2if);
347
348 return 0;
349}
350
351/*
352 * Our device driver structure
353 */
354static struct sa1111_driver ps2_driver = {
355 .drv = {
356 .name = "sa1111-ps2",
Russell King1ebcd762012-01-26 11:19:48 +0000357 .owner = THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 },
359 .devid = SA1111_DEVID_PS2,
360 .probe = ps2_probe,
Dmitry Torokhov010c33c2009-12-11 21:57:04 -0800361 .remove = __devexit_p(ps2_remove),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362};
363
364static int __init ps2_init(void)
365{
366 return sa1111_driver_register(&ps2_driver);
367}
368
369static void __exit ps2_exit(void)
370{
371 sa1111_driver_unregister(&ps2_driver);
372}
373
374module_init(ps2_init);
375module_exit(ps2_exit);
376
377MODULE_AUTHOR("Russell King <rmk@arm.linux.org.uk>");
378MODULE_DESCRIPTION("SA1111 PS2 controller driver");
379MODULE_LICENSE("GPL");