Linux 2.6.20.7
[linux/fpc-iii.git] / drivers / parport / parport_gsc.c
bloba7c5ead9a3d38ab146811f4077a5499c5e0f8e74
1 /*
2 * Low-level parallel-support for PC-style hardware integrated in the
3 * LASI-Controller (on GSC-Bus) for HP-PARISC Workstations
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * (C) 1999-2001 by Helge Deller <deller@gmx.de>
13 * based on parport_pc.c by
14 * Grant Guenther <grant@torque.net>
15 * Phil Blundell <philb@gnu.org>
16 * Tim Waugh <tim@cyberelk.demon.co.uk>
17 * Jose Renau <renau@acm.org>
18 * David Campbell
19 * Andrea Arcangeli
22 #undef DEBUG /* undef for production */
24 #include <linux/module.h>
25 #include <linux/init.h>
26 #include <linux/sched.h>
27 #include <linux/delay.h>
28 #include <linux/errno.h>
29 #include <linux/interrupt.h>
30 #include <linux/ioport.h>
31 #include <linux/kernel.h>
32 #include <linux/slab.h>
33 #include <linux/pci.h>
34 #include <linux/sysctl.h>
36 #include <asm/io.h>
37 #include <asm/dma.h>
38 #include <asm/uaccess.h>
39 #include <asm/superio.h>
41 #include <linux/parport.h>
42 #include <asm/pdc.h>
43 #include <asm/parisc-device.h>
44 #include <asm/hardware.h>
45 #include "parport_gsc.h"
48 MODULE_AUTHOR("Helge Deller <deller@gmx.de>");
49 MODULE_DESCRIPTION("HP-PARISC PC-style parallel port driver");
50 MODULE_SUPPORTED_DEVICE("integrated PC-style parallel port");
51 MODULE_LICENSE("GPL");
55 * Clear TIMEOUT BIT in EPP MODE
57 * This is also used in SPP detection.
59 static int clear_epp_timeout(struct parport *pb)
61 unsigned char r;
63 if (!(parport_gsc_read_status(pb) & 0x01))
64 return 1;
66 /* To clear timeout some chips require double read */
67 parport_gsc_read_status(pb);
68 r = parport_gsc_read_status(pb);
69 parport_writeb (r | 0x01, STATUS (pb)); /* Some reset by writing 1 */
70 parport_writeb (r & 0xfe, STATUS (pb)); /* Others by writing 0 */
71 r = parport_gsc_read_status(pb);
73 return !(r & 0x01);
77 * Access functions.
79 * Most of these aren't static because they may be used by the
80 * parport_xxx_yyy macros. extern __inline__ versions of several
81 * of these are in parport_gsc.h.
84 static irqreturn_t parport_gsc_interrupt(int irq, void *dev_id)
86 parport_generic_irq(irq, (struct parport *) dev_id);
87 return IRQ_HANDLED;
90 void parport_gsc_init_state(struct pardevice *dev, struct parport_state *s)
92 s->u.pc.ctr = 0xc | (dev->irq_func ? 0x10 : 0x0);
95 void parport_gsc_save_state(struct parport *p, struct parport_state *s)
97 s->u.pc.ctr = parport_readb (CONTROL (p));
100 void parport_gsc_restore_state(struct parport *p, struct parport_state *s)
102 parport_writeb (s->u.pc.ctr, CONTROL (p));
105 struct parport_operations parport_gsc_ops =
107 .write_data = parport_gsc_write_data,
108 .read_data = parport_gsc_read_data,
110 .write_control = parport_gsc_write_control,
111 .read_control = parport_gsc_read_control,
112 .frob_control = parport_gsc_frob_control,
114 .read_status = parport_gsc_read_status,
116 .enable_irq = parport_gsc_enable_irq,
117 .disable_irq = parport_gsc_disable_irq,
119 .data_forward = parport_gsc_data_forward,
120 .data_reverse = parport_gsc_data_reverse,
122 .init_state = parport_gsc_init_state,
123 .save_state = parport_gsc_save_state,
124 .restore_state = parport_gsc_restore_state,
126 .epp_write_data = parport_ieee1284_epp_write_data,
127 .epp_read_data = parport_ieee1284_epp_read_data,
128 .epp_write_addr = parport_ieee1284_epp_write_addr,
129 .epp_read_addr = parport_ieee1284_epp_read_addr,
131 .ecp_write_data = parport_ieee1284_ecp_write_data,
132 .ecp_read_data = parport_ieee1284_ecp_read_data,
133 .ecp_write_addr = parport_ieee1284_ecp_write_addr,
135 .compat_write_data = parport_ieee1284_write_compat,
136 .nibble_read_data = parport_ieee1284_read_nibble,
137 .byte_read_data = parport_ieee1284_read_byte,
139 .owner = THIS_MODULE,
142 /* --- Mode detection ------------------------------------- */
145 * Checks for port existence, all ports support SPP MODE
147 static int __devinit parport_SPP_supported(struct parport *pb)
149 unsigned char r, w;
152 * first clear an eventually pending EPP timeout
153 * I (sailer@ife.ee.ethz.ch) have an SMSC chipset
154 * that does not even respond to SPP cycles if an EPP
155 * timeout is pending
157 clear_epp_timeout(pb);
159 /* Do a simple read-write test to make sure the port exists. */
160 w = 0xc;
161 parport_writeb (w, CONTROL (pb));
163 /* Is there a control register that we can read from? Some
164 * ports don't allow reads, so read_control just returns a
165 * software copy. Some ports _do_ allow reads, so bypass the
166 * software copy here. In addition, some bits aren't
167 * writable. */
168 r = parport_readb (CONTROL (pb));
169 if ((r & 0xf) == w) {
170 w = 0xe;
171 parport_writeb (w, CONTROL (pb));
172 r = parport_readb (CONTROL (pb));
173 parport_writeb (0xc, CONTROL (pb));
174 if ((r & 0xf) == w)
175 return PARPORT_MODE_PCSPP;
178 /* Try the data register. The data lines aren't tri-stated at
179 * this stage, so we expect back what we wrote. */
180 w = 0xaa;
181 parport_gsc_write_data (pb, w);
182 r = parport_gsc_read_data (pb);
183 if (r == w) {
184 w = 0x55;
185 parport_gsc_write_data (pb, w);
186 r = parport_gsc_read_data (pb);
187 if (r == w)
188 return PARPORT_MODE_PCSPP;
191 return 0;
194 /* Detect PS/2 support.
196 * Bit 5 (0x20) sets the PS/2 data direction; setting this high
197 * allows us to read data from the data lines. In theory we would get back
198 * 0xff but any peripheral attached to the port may drag some or all of the
199 * lines down to zero. So if we get back anything that isn't the contents
200 * of the data register we deem PS/2 support to be present.
202 * Some SPP ports have "half PS/2" ability - you can't turn off the line
203 * drivers, but an external peripheral with sufficiently beefy drivers of
204 * its own can overpower them and assert its own levels onto the bus, from
205 * where they can then be read back as normal. Ports with this property
206 * and the right type of device attached are likely to fail the SPP test,
207 * (as they will appear to have stuck bits) and so the fact that they might
208 * be misdetected here is rather academic.
211 static int __devinit parport_PS2_supported(struct parport *pb)
213 int ok = 0;
215 clear_epp_timeout(pb);
217 /* try to tri-state the buffer */
218 parport_gsc_data_reverse (pb);
220 parport_gsc_write_data(pb, 0x55);
221 if (parport_gsc_read_data(pb) != 0x55) ok++;
223 parport_gsc_write_data(pb, 0xaa);
224 if (parport_gsc_read_data(pb) != 0xaa) ok++;
226 /* cancel input mode */
227 parport_gsc_data_forward (pb);
229 if (ok) {
230 pb->modes |= PARPORT_MODE_TRISTATE;
231 } else {
232 struct parport_gsc_private *priv = pb->private_data;
233 priv->ctr_writable &= ~0x20;
236 return ok;
240 /* --- Initialisation code -------------------------------- */
242 struct parport *__devinit parport_gsc_probe_port (unsigned long base,
243 unsigned long base_hi,
244 int irq, int dma,
245 struct pci_dev *dev)
247 struct parport_gsc_private *priv;
248 struct parport_operations *ops;
249 struct parport tmp;
250 struct parport *p = &tmp;
252 priv = kzalloc (sizeof (struct parport_gsc_private), GFP_KERNEL);
253 if (!priv) {
254 printk (KERN_DEBUG "parport (0x%lx): no memory!\n", base);
255 return NULL;
257 ops = kmalloc (sizeof (struct parport_operations), GFP_KERNEL);
258 if (!ops) {
259 printk (KERN_DEBUG "parport (0x%lx): no memory for ops!\n",
260 base);
261 kfree (priv);
262 return NULL;
264 memcpy (ops, &parport_gsc_ops, sizeof (struct parport_operations));
265 priv->ctr = 0xc;
266 priv->ctr_writable = 0xff;
267 priv->dma_buf = 0;
268 priv->dma_handle = 0;
269 priv->dev = dev;
270 p->base = base;
271 p->base_hi = base_hi;
272 p->irq = irq;
273 p->dma = dma;
274 p->modes = PARPORT_MODE_PCSPP | PARPORT_MODE_SAFEININT;
275 p->ops = ops;
276 p->private_data = priv;
277 p->physport = p;
278 if (!parport_SPP_supported (p)) {
279 /* No port. */
280 kfree (priv);
281 return NULL;
283 parport_PS2_supported (p);
285 if (!(p = parport_register_port(base, PARPORT_IRQ_NONE,
286 PARPORT_DMA_NONE, ops))) {
287 kfree (priv);
288 kfree (ops);
289 return NULL;
292 p->base_hi = base_hi;
293 p->modes = tmp.modes;
294 p->size = (p->modes & PARPORT_MODE_EPP)?8:3;
295 p->private_data = priv;
297 printk(KERN_INFO "%s: PC-style at 0x%lx", p->name, p->base);
298 p->irq = irq;
299 if (p->irq == PARPORT_IRQ_AUTO) {
300 p->irq = PARPORT_IRQ_NONE;
302 if (p->irq != PARPORT_IRQ_NONE) {
303 printk(", irq %d", p->irq);
305 if (p->dma == PARPORT_DMA_AUTO) {
306 p->dma = PARPORT_DMA_NONE;
309 if (p->dma == PARPORT_DMA_AUTO) /* To use DMA, giving the irq
310 is mandatory (see above) */
311 p->dma = PARPORT_DMA_NONE;
313 printk(" [");
314 #define printmode(x) {if(p->modes&PARPORT_MODE_##x){printk("%s%s",f?",":"",#x);f++;}}
316 int f = 0;
317 printmode(PCSPP);
318 printmode(TRISTATE);
319 printmode(COMPAT)
320 printmode(EPP);
321 // printmode(ECP);
322 // printmode(DMA);
324 #undef printmode
325 printk("]\n");
327 if (p->irq != PARPORT_IRQ_NONE) {
328 if (request_irq (p->irq, parport_gsc_interrupt,
329 0, p->name, p)) {
330 printk (KERN_WARNING "%s: irq %d in use, "
331 "resorting to polled operation\n",
332 p->name, p->irq);
333 p->irq = PARPORT_IRQ_NONE;
334 p->dma = PARPORT_DMA_NONE;
338 /* Done probing. Now put the port into a sensible start-up state. */
340 parport_gsc_write_data(p, 0);
341 parport_gsc_data_forward (p);
343 /* Now that we've told the sharing engine about the port, and
344 found out its characteristics, let the high-level drivers
345 know about it. */
346 parport_announce_port (p);
348 return p;
352 #define PARPORT_GSC_OFFSET 0x800
354 static int __initdata parport_count;
356 static int __devinit parport_init_chip(struct parisc_device *dev)
358 struct parport *p;
359 unsigned long port;
361 if (!dev->irq) {
362 printk(KERN_WARNING "IRQ not found for parallel device at 0x%lx\n",
363 dev->hpa.start);
364 return -ENODEV;
367 port = dev->hpa.start + PARPORT_GSC_OFFSET;
369 /* some older machines with ASP-chip don't support
370 * the enhanced parport modes.
372 if (boot_cpu_data.cpu_type > pcxt && !pdc_add_valid(port+4)) {
374 /* Initialize bidirectional-mode (0x10) & data-tranfer-mode #1 (0x20) */
375 printk("%s: initialize bidirectional-mode.\n", __FUNCTION__);
376 parport_writeb ( (0x10 + 0x20), port + 4);
378 } else {
379 printk("%s: enhanced parport-modes not supported.\n", __FUNCTION__);
382 p = parport_gsc_probe_port(port, 0, dev->irq,
383 /* PARPORT_IRQ_NONE */ PARPORT_DMA_NONE, NULL);
384 if (p)
385 parport_count++;
386 dev->dev.driver_data = p;
388 return 0;
391 static int __devexit parport_remove_chip(struct parisc_device *dev)
393 struct parport *p = dev->dev.driver_data;
394 if (p) {
395 struct parport_gsc_private *priv = p->private_data;
396 struct parport_operations *ops = p->ops;
397 parport_remove_port(p);
398 if (p->dma != PARPORT_DMA_NONE)
399 free_dma(p->dma);
400 if (p->irq != PARPORT_IRQ_NONE)
401 free_irq(p->irq, p);
402 if (priv->dma_buf)
403 pci_free_consistent(priv->dev, PAGE_SIZE,
404 priv->dma_buf,
405 priv->dma_handle);
406 kfree (p->private_data);
407 parport_put_port(p);
408 kfree (ops); /* hope no-one cached it */
410 return 0;
413 static struct parisc_device_id parport_tbl[] = {
414 { HPHW_FIO, HVERSION_REV_ANY_ID, HVERSION_ANY_ID, 0x74 },
415 { 0, }
418 MODULE_DEVICE_TABLE(parisc, parport_tbl);
420 static struct parisc_driver parport_driver = {
421 .name = "Parallel",
422 .id_table = parport_tbl,
423 .probe = parport_init_chip,
424 .remove = __devexit_p(parport_remove_chip),
427 int __devinit parport_gsc_init(void)
429 return register_parisc_driver(&parport_driver);
432 static void __devexit parport_gsc_exit(void)
434 unregister_parisc_driver(&parport_driver);
437 module_init(parport_gsc_init);
438 module_exit(parport_gsc_exit);