[TG3]: Set minimal hw interrupt mitigation.
[linux-2.6/verdex.git] / drivers / input / serio / gscps2.c
blob897e4c12b6427186ceb7d7645a75e681ad99666c
1 /*
2 * drivers/input/serio/gscps2.c
4 * Copyright (c) 2004 Helge Deller <deller@gmx.de>
5 * Copyright (c) 2002 Laurent Canet <canetl@esiee.fr>
6 * Copyright (c) 2002 Thibaut Varene <varenet@parisc-linux.org>
8 * Pieces of code based on linux-2.4's hp_mouse.c & hp_keyb.c
9 * Copyright (c) 1999 Alex deVries <alex@onefishtwo.ca>
10 * Copyright (c) 1999-2000 Philipp Rumpf <prumpf@tux.org>
11 * Copyright (c) 2000 Xavier Debacker <debackex@esiee.fr>
12 * Copyright (c) 2000-2001 Thomas Marteau <marteaut@esiee.fr>
14 * HP GSC PS/2 port driver, found in PA/RISC Workstations
16 * This file is subject to the terms and conditions of the GNU General Public
17 * License. See the file "COPYING" in the main directory of this archive
18 * for more details.
20 * TODO:
21 * - Dino testing (did HP ever shipped a machine on which this port
22 * was usable/enabled ?)
25 #include <linux/config.h>
26 #include <linux/init.h>
27 #include <linux/module.h>
28 #include <linux/serio.h>
29 #include <linux/input.h>
30 #include <linux/interrupt.h>
31 #include <linux/spinlock.h>
32 #include <linux/delay.h>
33 #include <linux/ioport.h>
34 #include <linux/pci_ids.h>
36 #include <asm/irq.h>
37 #include <asm/io.h>
38 #include <asm/parisc-device.h>
40 MODULE_AUTHOR("Laurent Canet <canetl@esiee.fr>, Thibaut Varene <varenet@parisc-linux.org>, Helge Deller <deller@gmx.de>");
41 MODULE_DESCRIPTION("HP GSC PS2 port driver");
42 MODULE_LICENSE("GPL");
43 MODULE_DEVICE_TABLE(parisc, gscps2_device_tbl);
45 #define PFX "gscps2.c: "
48 * Driver constants
51 /* various constants */
52 #define ENABLE 1
53 #define DISABLE 0
55 #define GSC_DINO_OFFSET 0x0800 /* offset for DINO controller versus LASI one */
57 /* PS/2 IO port offsets */
58 #define GSC_ID 0x00 /* device ID offset (see: GSC_ID_XXX) */
59 #define GSC_RESET 0x00 /* reset port offset */
60 #define GSC_RCVDATA 0x04 /* receive port offset */
61 #define GSC_XMTDATA 0x04 /* transmit port offset */
62 #define GSC_CONTROL 0x08 /* see: Control register bits */
63 #define GSC_STATUS 0x0C /* see: Status register bits */
65 /* Control register bits */
66 #define GSC_CTRL_ENBL 0x01 /* enable interface */
67 #define GSC_CTRL_LPBXR 0x02 /* loopback operation */
68 #define GSC_CTRL_DIAG 0x20 /* directly control clock/data line */
69 #define GSC_CTRL_DATDIR 0x40 /* data line direct control */
70 #define GSC_CTRL_CLKDIR 0x80 /* clock line direct control */
72 /* Status register bits */
73 #define GSC_STAT_RBNE 0x01 /* Receive Buffer Not Empty */
74 #define GSC_STAT_TBNE 0x02 /* Transmit Buffer Not Empty */
75 #define GSC_STAT_TERR 0x04 /* Timeout Error */
76 #define GSC_STAT_PERR 0x08 /* Parity Error */
77 #define GSC_STAT_CMPINTR 0x10 /* Composite Interrupt = irq on any port */
78 #define GSC_STAT_DATSHD 0x40 /* Data Line Shadow */
79 #define GSC_STAT_CLKSHD 0x80 /* Clock Line Shadow */
81 /* IDs returned by GSC_ID port register */
82 #define GSC_ID_KEYBOARD 0 /* device ID values */
83 #define GSC_ID_MOUSE 1
86 static irqreturn_t gscps2_interrupt(int irq, void *dev, struct pt_regs *regs);
88 #define BUFFER_SIZE 0x0f
90 /* GSC PS/2 port device struct */
91 struct gscps2port {
92 struct list_head node;
93 struct parisc_device *padev;
94 struct serio *port;
95 spinlock_t lock;
96 char *addr;
97 u8 act, append; /* position in buffer[] */
98 struct {
99 u8 data;
100 u8 str;
101 } buffer[BUFFER_SIZE+1];
102 int id;
106 * Various HW level routines
109 #define gscps2_readb_input(x) readb((x)+GSC_RCVDATA)
110 #define gscps2_readb_control(x) readb((x)+GSC_CONTROL)
111 #define gscps2_readb_status(x) readb((x)+GSC_STATUS)
112 #define gscps2_writeb_control(x, y) writeb((x), (y)+GSC_CONTROL)
116 * wait_TBE() - wait for Transmit Buffer Empty
119 static int wait_TBE(char *addr)
121 int timeout = 25000; /* device is expected to react within 250 msec */
122 while (gscps2_readb_status(addr) & GSC_STAT_TBNE) {
123 if (!--timeout)
124 return 0; /* This should not happen */
125 udelay(10);
127 return 1;
132 * gscps2_flush() - flush the receive buffer
135 static void gscps2_flush(struct gscps2port *ps2port)
137 while (gscps2_readb_status(ps2port->addr) & GSC_STAT_RBNE)
138 gscps2_readb_input(ps2port->addr);
139 ps2port->act = ps2port->append = 0;
143 * gscps2_writeb_output() - write a byte to the port
145 * returns 1 on sucess, 0 on error
148 static inline int gscps2_writeb_output(struct gscps2port *ps2port, u8 data)
150 unsigned long flags;
151 char *addr = ps2port->addr;
153 if (!wait_TBE(addr)) {
154 printk(KERN_DEBUG PFX "timeout - could not write byte %#x\n", data);
155 return 0;
158 while (gscps2_readb_status(ps2port->addr) & GSC_STAT_RBNE)
159 /* wait */;
161 spin_lock_irqsave(&ps2port->lock, flags);
162 writeb(data, addr+GSC_XMTDATA);
163 spin_unlock_irqrestore(&ps2port->lock, flags);
165 /* this is ugly, but due to timing of the port it seems to be necessary. */
166 mdelay(6);
168 /* make sure any received data is returned as fast as possible */
169 /* this is important e.g. when we set the LEDs on the keyboard */
170 gscps2_interrupt(0, NULL, NULL);
172 return 1;
177 * gscps2_enable() - enables or disables the port
180 static void gscps2_enable(struct gscps2port *ps2port, int enable)
182 unsigned long flags;
183 u8 data;
185 /* now enable/disable the port */
186 spin_lock_irqsave(&ps2port->lock, flags);
187 gscps2_flush(ps2port);
188 data = gscps2_readb_control(ps2port->addr);
189 if (enable)
190 data |= GSC_CTRL_ENBL;
191 else
192 data &= ~GSC_CTRL_ENBL;
193 gscps2_writeb_control(data, ps2port->addr);
194 spin_unlock_irqrestore(&ps2port->lock, flags);
195 wait_TBE(ps2port->addr);
196 gscps2_flush(ps2port);
200 * gscps2_reset() - resets the PS/2 port
203 static void gscps2_reset(struct gscps2port *ps2port)
205 char *addr = ps2port->addr;
206 unsigned long flags;
208 /* reset the interface */
209 spin_lock_irqsave(&ps2port->lock, flags);
210 gscps2_flush(ps2port);
211 writeb(0xff, addr+GSC_RESET);
212 gscps2_flush(ps2port);
213 spin_unlock_irqrestore(&ps2port->lock, flags);
215 /* enable it */
216 gscps2_enable(ps2port, ENABLE);
219 static LIST_HEAD(ps2port_list);
222 * gscps2_interrupt() - Interruption service routine
224 * This function reads received PS/2 bytes and processes them on
225 * all interfaces.
226 * The problematic part here is, that the keyboard and mouse PS/2 port
227 * share the same interrupt and it's not possible to send data if any
228 * one of them holds input data. To solve this problem we try to receive
229 * the data as fast as possible and handle the reporting to the upper layer
230 * later.
233 static irqreturn_t gscps2_interrupt(int irq, void *dev, struct pt_regs *regs)
235 struct gscps2port *ps2port;
237 list_for_each_entry(ps2port, &ps2port_list, node) {
239 unsigned long flags;
240 spin_lock_irqsave(&ps2port->lock, flags);
242 while ( (ps2port->buffer[ps2port->append].str =
243 gscps2_readb_status(ps2port->addr)) & GSC_STAT_RBNE ) {
244 ps2port->buffer[ps2port->append].data =
245 gscps2_readb_input(ps2port->addr);
246 ps2port->append = ((ps2port->append+1) & BUFFER_SIZE);
249 spin_unlock_irqrestore(&ps2port->lock, flags);
251 } /* list_for_each_entry */
253 /* all data was read from the ports - now report the data to upper layer */
255 list_for_each_entry(ps2port, &ps2port_list, node) {
257 while (ps2port->act != ps2port->append) {
259 unsigned int rxflags;
260 u8 data, status;
262 /* Did new data arrived while we read existing data ?
263 If yes, exit now and let the new irq handler start over again */
264 if (gscps2_readb_status(ps2port->addr) & GSC_STAT_CMPINTR)
265 return IRQ_HANDLED;
267 status = ps2port->buffer[ps2port->act].str;
268 data = ps2port->buffer[ps2port->act].data;
270 ps2port->act = ((ps2port->act+1) & BUFFER_SIZE);
271 rxflags = ((status & GSC_STAT_TERR) ? SERIO_TIMEOUT : 0 ) |
272 ((status & GSC_STAT_PERR) ? SERIO_PARITY : 0 );
274 serio_interrupt(ps2port->port, data, rxflags, regs);
276 } /* while() */
278 } /* list_for_each_entry */
280 return IRQ_HANDLED;
285 * gscps2_write() - send a byte out through the aux interface.
288 static int gscps2_write(struct serio *port, unsigned char data)
290 struct gscps2port *ps2port = port->port_data;
292 if (!gscps2_writeb_output(ps2port, data)) {
293 printk(KERN_DEBUG PFX "sending byte %#x failed.\n", data);
294 return -1;
296 return 0;
300 * gscps2_open() is called when a port is opened by the higher layer.
301 * It resets and enables the port.
304 static int gscps2_open(struct serio *port)
306 struct gscps2port *ps2port = port->port_data;
308 gscps2_reset(ps2port);
310 gscps2_interrupt(0, NULL, NULL);
312 return 0;
316 * gscps2_close() disables the port
319 static void gscps2_close(struct serio *port)
321 struct gscps2port *ps2port = port->port_data;
322 gscps2_enable(ps2port, DISABLE);
326 * gscps2_probe() - Probes PS2 devices
327 * @return: success/error report
330 static int __init gscps2_probe(struct parisc_device *dev)
332 struct gscps2port *ps2port;
333 struct serio *serio;
334 unsigned long hpa = dev->hpa;
335 int ret;
337 if (!dev->irq)
338 return -ENODEV;
340 /* Offset for DINO PS/2. Works with LASI even */
341 if (dev->id.sversion == 0x96)
342 hpa += GSC_DINO_OFFSET;
344 ps2port = kmalloc(sizeof(struct gscps2port), GFP_KERNEL);
345 serio = kmalloc(sizeof(struct serio), GFP_KERNEL);
346 if (!ps2port || !serio) {
347 ret = -ENOMEM;
348 goto fail_nomem;
351 dev_set_drvdata(&dev->dev, ps2port);
353 memset(ps2port, 0, sizeof(struct gscps2port));
354 memset(serio, 0, sizeof(struct serio));
355 ps2port->port = serio;
356 ps2port->padev = dev;
357 ps2port->addr = ioremap(hpa, GSC_STATUS + 4);
358 spin_lock_init(&ps2port->lock);
360 gscps2_reset(ps2port);
361 ps2port->id = readb(ps2port->addr + GSC_ID) & 0x0f;
363 snprintf(serio->name, sizeof(serio->name), "GSC PS/2 %s",
364 (ps2port->id == GSC_ID_KEYBOARD) ? "keyboard" : "mouse");
365 strlcpy(serio->phys, dev->dev.bus_id, sizeof(serio->phys));
366 serio->id.type = SERIO_8042;
367 serio->write = gscps2_write;
368 serio->open = gscps2_open;
369 serio->close = gscps2_close;
370 serio->port_data = ps2port;
371 serio->dev.parent = &dev->dev;
373 list_add_tail(&ps2port->node, &ps2port_list);
375 ret = -EBUSY;
376 if (request_irq(dev->irq, gscps2_interrupt, SA_SHIRQ, ps2port->port->name, ps2port))
377 goto fail_miserably;
379 if (ps2port->id != GSC_ID_KEYBOARD && ps2port->id != GSC_ID_MOUSE) {
380 printk(KERN_WARNING PFX "Unsupported PS/2 port at 0x%08lx (id=%d) ignored\n",
381 hpa, ps2port->id);
382 ret = -ENODEV;
383 goto fail;
386 #if 0
387 if (!request_mem_region(hpa, GSC_STATUS + 4, ps2port->port.name))
388 goto fail;
389 #endif
391 printk(KERN_INFO "serio: %s port at 0x%p irq %d @ %s\n",
392 ps2port->port->name,
393 ps2port->addr,
394 ps2port->padev->irq,
395 ps2port->port->phys);
397 serio_register_port(ps2port->port);
399 return 0;
401 fail:
402 free_irq(dev->irq, ps2port);
404 fail_miserably:
405 list_del(&ps2port->node);
406 iounmap(ps2port->addr);
407 release_mem_region(dev->hpa, GSC_STATUS + 4);
409 fail_nomem:
410 kfree(ps2port);
411 kfree(serio);
412 return ret;
416 * gscps2_remove() - Removes PS2 devices
417 * @return: success/error report
420 static int __devexit gscps2_remove(struct parisc_device *dev)
422 struct gscps2port *ps2port = dev_get_drvdata(&dev->dev);
424 serio_unregister_port(ps2port->port);
425 free_irq(dev->irq, ps2port);
426 gscps2_flush(ps2port);
427 list_del(&ps2port->node);
428 iounmap(ps2port->addr);
429 #if 0
430 release_mem_region(dev->hpa, GSC_STATUS + 4);
431 #endif
432 dev_set_drvdata(&dev->dev, NULL);
433 kfree(ps2port);
434 return 0;
438 static struct parisc_device_id gscps2_device_tbl[] = {
439 { HPHW_FIO, HVERSION_REV_ANY_ID, HVERSION_ANY_ID, 0x00084 }, /* LASI PS/2 */
440 #ifdef DINO_TESTED
441 { HPHW_FIO, HVERSION_REV_ANY_ID, HVERSION_ANY_ID, 0x00096 }, /* DINO PS/2 */
442 #endif
443 { 0, } /* 0 terminated list */
446 static struct parisc_driver parisc_ps2_driver = {
447 .name = "GSC PS2",
448 .id_table = gscps2_device_tbl,
449 .probe = gscps2_probe,
450 .remove = gscps2_remove,
453 static int __init gscps2_init(void)
455 register_parisc_driver(&parisc_ps2_driver);
456 return 0;
459 static void __exit gscps2_exit(void)
461 unregister_parisc_driver(&parisc_ps2_driver);
465 module_init(gscps2_init);
466 module_exit(gscps2_exit);