ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / drivers / parport / parport_gsc.c
1 /*
2  *      Low-level parallel-support for PC-style hardware integrated in the 
3  *      LASI-Controller (on GSC-Bus) for HP-PARISC Workstations
4  *
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.
9  *
10  *      (C) 1999-2001 by Helge Deller <deller@gmx.de>
11  *
12  * 
13  * based on parport_pc.c by 
14  *          Grant Guenther <grant@torque.net>
15  *          Phil Blundell <Philip.Blundell@pobox.com>
16  *          Tim Waugh <tim@cyberelk.demon.co.uk>
17  *          Jose Renau <renau@acm.org>
18  *          David Campbell <campbell@torque.net>
19  *          Andrea Arcangeli
20  */
21
22 #undef DEBUG    /* undef for production */
23
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>
35
36 #include <asm/io.h>
37 #include <asm/dma.h>
38 #include <asm/uaccess.h>
39 #include <asm/superio.h>
40
41 #include <linux/parport.h>
42 #include <asm/pdc.h>
43 #include <asm/parisc-device.h>
44 #include <asm/hardware.h>
45 #include <asm/parport_gsc.h>
46
47
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");
52
53
54 /*
55  * Clear TIMEOUT BIT in EPP MODE
56  *
57  * This is also used in SPP detection.
58  */
59 static int clear_epp_timeout(struct parport *pb)
60 {
61         unsigned char r;
62
63         if (!(parport_gsc_read_status(pb) & 0x01))
64                 return 1;
65
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);
72
73         return !(r & 0x01);
74 }
75
76 /*
77  * Access functions.
78  *
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.
82  */
83
84 static irqreturn_t parport_gsc_interrupt(int irq, void *dev_id, struct pt_regs *regs)
85 {
86         parport_generic_irq(irq, (struct parport *) dev_id, regs);
87         return IRQ_HANDLED;
88 }
89
90 void parport_gsc_write_data(struct parport *p, unsigned char d)
91 {
92         parport_writeb (d, DATA (p));
93 }
94
95 unsigned char parport_gsc_read_data(struct parport *p)
96 {
97         unsigned char c = parport_readb (DATA (p));
98         return c;
99 }
100
101 void parport_gsc_write_control(struct parport *p, unsigned char d)
102 {
103         const unsigned char wm = (PARPORT_CONTROL_STROBE |
104                                   PARPORT_CONTROL_AUTOFD |
105                                   PARPORT_CONTROL_INIT |
106                                   PARPORT_CONTROL_SELECT);
107
108         /* Take this out when drivers have adapted to the newer interface. */
109         if (d & 0x20) {
110                 pr_debug("%s (%s): use data_reverse for this!\n",
111                             p->name, p->cad->name);
112                 parport_gsc_data_reverse (p);
113         }
114
115         __parport_gsc_frob_control (p, wm, d & wm);
116 }
117
118 unsigned char parport_gsc_read_control(struct parport *p)
119 {
120         const unsigned char wm = (PARPORT_CONTROL_STROBE |
121                                   PARPORT_CONTROL_AUTOFD |
122                                   PARPORT_CONTROL_INIT |
123                                   PARPORT_CONTROL_SELECT);
124         const struct parport_gsc_private *priv = p->physport->private_data;
125         return priv->ctr & wm; /* Use soft copy */
126 }
127
128 unsigned char parport_gsc_frob_control (struct parport *p, unsigned char mask,
129                                        unsigned char val)
130 {
131         const unsigned char wm = (PARPORT_CONTROL_STROBE |
132                                   PARPORT_CONTROL_AUTOFD |
133                                   PARPORT_CONTROL_INIT |
134                                   PARPORT_CONTROL_SELECT);
135
136         /* Take this out when drivers have adapted to the newer interface. */
137         if (mask & 0x20) {
138                 pr_debug("%s (%s): use data_%s for this!\n",
139                         p->name, p->cad->name,
140                         (val & 0x20) ? "reverse" : "forward");
141                 if (val & 0x20)
142                         parport_gsc_data_reverse (p);
143                 else
144                         parport_gsc_data_forward (p);
145         }
146
147         /* Restrict mask and val to control lines. */
148         mask &= wm;
149         val &= wm;
150
151         return __parport_gsc_frob_control (p, mask, val);
152 }
153
154 unsigned char parport_gsc_read_status(struct parport *p)
155 {
156         return parport_readb (STATUS (p));
157 }
158
159 void parport_gsc_disable_irq(struct parport *p)
160 {
161         __parport_gsc_frob_control (p, 0x10, 0);
162 }
163
164 void parport_gsc_enable_irq(struct parport *p)
165 {
166         __parport_gsc_frob_control (p, 0x10, 0x10);
167 }
168
169 void parport_gsc_data_forward (struct parport *p)
170 {
171         __parport_gsc_frob_control (p, 0x20, 0);
172 }
173
174 void parport_gsc_data_reverse (struct parport *p)
175 {
176         __parport_gsc_frob_control (p, 0x20, 0x20);
177 }
178
179 void parport_gsc_init_state(struct pardevice *dev, struct parport_state *s)
180 {
181         s->u.pc.ctr = 0xc | (dev->irq_func ? 0x10 : 0x0);
182 }
183
184 void parport_gsc_save_state(struct parport *p, struct parport_state *s)
185 {
186         s->u.pc.ctr = parport_readb (CONTROL (p));
187 }
188
189 void parport_gsc_restore_state(struct parport *p, struct parport_state *s)
190 {
191         parport_writeb (s->u.pc.ctr, CONTROL (p));
192 }
193
194 struct parport_operations parport_gsc_ops = 
195 {
196         .write_data     = parport_gsc_write_data,
197         .read_data      = parport_gsc_read_data,
198
199         .write_control  = parport_gsc_write_control,
200         .read_control   = parport_gsc_read_control,
201         .frob_control   = parport_gsc_frob_control,
202
203         .read_status    = parport_gsc_read_status,
204
205         .enable_irq     = parport_gsc_enable_irq,
206         .disable_irq    = parport_gsc_disable_irq,
207
208         .data_forward   = parport_gsc_data_forward,
209         .data_reverse   = parport_gsc_data_reverse,
210
211         .init_state     = parport_gsc_init_state,
212         .save_state     = parport_gsc_save_state,
213         .restore_state  = parport_gsc_restore_state,
214
215         .epp_write_data = parport_ieee1284_epp_write_data,
216         .epp_read_data  = parport_ieee1284_epp_read_data,
217         .epp_write_addr = parport_ieee1284_epp_write_addr,
218         .epp_read_addr  = parport_ieee1284_epp_read_addr,
219
220         .ecp_write_data = parport_ieee1284_ecp_write_data,
221         .ecp_read_data  = parport_ieee1284_ecp_read_data,
222         .ecp_write_addr = parport_ieee1284_ecp_write_addr,
223
224         .compat_write_data      = parport_ieee1284_write_compat,
225         .nibble_read_data       = parport_ieee1284_read_nibble,
226         .byte_read_data         = parport_ieee1284_read_byte,
227
228         .owner          = THIS_MODULE,
229 };
230
231 /* --- Mode detection ------------------------------------- */
232
233 /*
234  * Checks for port existence, all ports support SPP MODE
235  */
236 static int __devinit parport_SPP_supported(struct parport *pb)
237 {
238         unsigned char r, w;
239
240         /*
241          * first clear an eventually pending EPP timeout 
242          * I (sailer@ife.ee.ethz.ch) have an SMSC chipset
243          * that does not even respond to SPP cycles if an EPP
244          * timeout is pending
245          */
246         clear_epp_timeout(pb);
247
248         /* Do a simple read-write test to make sure the port exists. */
249         w = 0xc;
250         parport_writeb (w, CONTROL (pb));
251
252         /* Is there a control register that we can read from?  Some
253          * ports don't allow reads, so read_control just returns a
254          * software copy. Some ports _do_ allow reads, so bypass the
255          * software copy here.  In addition, some bits aren't
256          * writable. */
257         r = parport_readb (CONTROL (pb));
258         if ((r & 0xf) == w) {
259                 w = 0xe;
260                 parport_writeb (w, CONTROL (pb));
261                 r = parport_readb (CONTROL (pb));
262                 parport_writeb (0xc, CONTROL (pb));
263                 if ((r & 0xf) == w)
264                         return PARPORT_MODE_PCSPP;
265         }
266
267         /* Try the data register.  The data lines aren't tri-stated at
268          * this stage, so we expect back what we wrote. */
269         w = 0xaa;
270         parport_gsc_write_data (pb, w);
271         r = parport_gsc_read_data (pb);
272         if (r == w) {
273                 w = 0x55;
274                 parport_gsc_write_data (pb, w);
275                 r = parport_gsc_read_data (pb);
276                 if (r == w)
277                         return PARPORT_MODE_PCSPP;
278         }
279
280         return 0;
281 }
282
283 /* Detect PS/2 support.
284  *
285  * Bit 5 (0x20) sets the PS/2 data direction; setting this high
286  * allows us to read data from the data lines.  In theory we would get back
287  * 0xff but any peripheral attached to the port may drag some or all of the
288  * lines down to zero.  So if we get back anything that isn't the contents
289  * of the data register we deem PS/2 support to be present. 
290  *
291  * Some SPP ports have "half PS/2" ability - you can't turn off the line
292  * drivers, but an external peripheral with sufficiently beefy drivers of
293  * its own can overpower them and assert its own levels onto the bus, from
294  * where they can then be read back as normal.  Ports with this property
295  * and the right type of device attached are likely to fail the SPP test,
296  * (as they will appear to have stuck bits) and so the fact that they might
297  * be misdetected here is rather academic. 
298  */
299
300 static int __devinit parport_PS2_supported(struct parport *pb)
301 {
302         int ok = 0;
303   
304         clear_epp_timeout(pb);
305
306         /* try to tri-state the buffer */
307         parport_gsc_data_reverse (pb);
308         
309         parport_gsc_write_data(pb, 0x55);
310         if (parport_gsc_read_data(pb) != 0x55) ok++;
311
312         parport_gsc_write_data(pb, 0xaa);
313         if (parport_gsc_read_data(pb) != 0xaa) ok++;
314
315         /* cancel input mode */
316         parport_gsc_data_forward (pb);
317
318         if (ok) {
319                 pb->modes |= PARPORT_MODE_TRISTATE;
320         } else {
321                 struct parport_gsc_private *priv = pb->private_data;
322                 priv->ctr_writable &= ~0x20;
323         }
324
325         return ok;
326 }
327
328
329 /* --- Initialisation code -------------------------------- */
330
331 struct parport *__devinit parport_gsc_probe_port (unsigned long base,
332                                                  unsigned long base_hi,
333                                                  int irq, int dma,
334                                                  struct pci_dev *dev)
335 {
336         struct parport_gsc_private *priv;
337         struct parport_operations *ops;
338         struct parport tmp;
339         struct parport *p = &tmp;
340
341         priv = kmalloc (sizeof (struct parport_gsc_private), GFP_KERNEL);
342         if (!priv) {
343                 printk (KERN_DEBUG "parport (0x%lx): no memory!\n", base);
344                 return NULL;
345         }
346         ops = kmalloc (sizeof (struct parport_operations), GFP_KERNEL);
347         if (!ops) {
348                 printk (KERN_DEBUG "parport (0x%lx): no memory for ops!\n",
349                         base);
350                 kfree (priv);
351                 return NULL;
352         }
353         memcpy (ops, &parport_gsc_ops, sizeof (struct parport_operations));
354         priv->ctr = 0xc;
355         priv->ctr_writable = 0xff;
356         priv->dma_buf = 0;
357         priv->dma_handle = 0;
358         priv->dev = dev;
359         p->base = base;
360         p->base_hi = base_hi;
361         p->irq = irq;
362         p->dma = dma;
363         p->modes = PARPORT_MODE_PCSPP | PARPORT_MODE_SAFEININT;
364         p->ops = ops;
365         p->private_data = priv;
366         p->physport = p;
367         if (!parport_SPP_supported (p)) {
368                 /* No port. */
369                 kfree (priv);
370                 return NULL;
371         }
372         parport_PS2_supported (p);
373
374         if (!(p = parport_register_port(base, PARPORT_IRQ_NONE,
375                                         PARPORT_DMA_NONE, ops))) {
376                 kfree (priv);
377                 kfree (ops);
378                 return NULL;
379         }
380
381         p->base_hi = base_hi;
382         p->modes = tmp.modes;
383         p->size = (p->modes & PARPORT_MODE_EPP)?8:3;
384         p->private_data = priv;
385
386         printk(KERN_INFO "%s: PC-style at 0x%lx", p->name, p->base);
387         p->irq = irq;
388         if (p->irq == PARPORT_IRQ_AUTO) {
389                 p->irq = PARPORT_IRQ_NONE;
390         }
391         if (p->irq != PARPORT_IRQ_NONE) {
392                 printk(", irq %d", p->irq);
393
394                 if (p->dma == PARPORT_DMA_AUTO) {
395                         p->dma = PARPORT_DMA_NONE;
396                 }
397         }
398         if (p->dma == PARPORT_DMA_AUTO) /* To use DMA, giving the irq
399                                            is mandatory (see above) */
400                 p->dma = PARPORT_DMA_NONE;
401
402         printk(" [");
403 #define printmode(x) {if(p->modes&PARPORT_MODE_##x){printk("%s%s",f?",":"",#x);f++;}}
404         {
405                 int f = 0;
406                 printmode(PCSPP);
407                 printmode(TRISTATE);
408                 printmode(COMPAT)
409                 printmode(EPP);
410 //              printmode(ECP);
411 //              printmode(DMA);
412         }
413 #undef printmode
414         printk("]\n");
415
416         if (p->irq != PARPORT_IRQ_NONE) {
417                 if (request_irq (p->irq, parport_gsc_interrupt,
418                                  0, p->name, p)) {
419                         printk (KERN_WARNING "%s: irq %d in use, "
420                                 "resorting to polled operation\n",
421                                 p->name, p->irq);
422                         p->irq = PARPORT_IRQ_NONE;
423                         p->dma = PARPORT_DMA_NONE;
424                 }
425         }
426
427         /* Done probing.  Now put the port into a sensible start-up state. */
428
429         parport_gsc_write_data(p, 0);
430         parport_gsc_data_forward (p);
431
432         /* Now that we've told the sharing engine about the port, and
433            found out its characteristics, let the high-level drivers
434            know about it. */
435         parport_announce_port (p);
436
437         return p;
438 }
439
440
441 #define PARPORT_GSC_OFFSET 0x800
442
443 static int __initdata parport_count;
444
445 static int __devinit parport_init_chip(struct parisc_device *dev)
446 {
447         struct parport *p;
448         unsigned long port;
449
450         if (!dev->irq) {
451                 printk("IRQ not found for parallel device at 0x%lx\n", dev->hpa);
452                 return -ENODEV;
453         }
454
455         port = dev->hpa + PARPORT_GSC_OFFSET;
456         
457         /* some older machines with ASP-chip don't support
458          * the enhanced parport modes.
459          */
460         if (boot_cpu_data.cpu_type > pcxt && !pdc_add_valid(port+4)) {
461
462                 /* Initialize bidirectional-mode (0x10) & data-tranfer-mode #1 (0x20) */
463                 printk("%s: initialize bidirectional-mode.\n", __FUNCTION__);
464                 parport_writeb ( (0x10 + 0x20), port + 4);
465
466         } else {
467                 printk("%s: enhanced parport-modes not supported.\n", __FUNCTION__);
468         }
469         
470         p = parport_gsc_probe_port(port, 0, dev->irq,
471                         /* PARPORT_IRQ_NONE */ PARPORT_DMA_NONE, NULL);
472         if (p)
473                 parport_count++;
474         dev->dev.driver_data = p;
475
476         return 0;
477 }
478
479 static int __devexit parport_remove_chip(struct parisc_device *dev)
480 {
481         struct parport *p = dev->dev.driver_data;
482         if (p) {
483                 struct parport_gsc_private *priv = p->private_data;
484                 struct parport_operations *ops = p->ops;
485                 parport_remove_port(p);
486                 if (p->dma != PARPORT_DMA_NONE)
487                         free_dma(p->dma);
488                 if (p->irq != PARPORT_IRQ_NONE)
489                         free_irq(p->irq, p);
490                 if (priv->dma_buf)
491                         pci_free_consistent(priv->dev, PAGE_SIZE,
492                                             priv->dma_buf,
493                                             priv->dma_handle);
494                 kfree (p->private_data);
495                 parport_put_port(p);
496                 kfree (ops); /* hope no-one cached it */
497         }
498         return 0;
499 }
500
501 static struct parisc_device_id parport_tbl[] = {
502         { HPHW_FIO, HVERSION_REV_ANY_ID, HVERSION_ANY_ID, 0x74 },
503         { 0, }
504 };
505
506 MODULE_DEVICE_TABLE(parisc, parport_tbl);
507
508 static struct parisc_driver parport_driver = {
509         .name           = "Parallel",
510         .id_table       = parport_tbl,
511         .probe          = parport_init_chip,
512         .remove         = __devexit_p(parport_remove_chip),
513 };
514
515 int __devinit parport_gsc_init(void)
516 {
517         return register_parisc_driver(&parport_driver);
518 }
519
520 static void __devexit parport_gsc_exit(void)
521 {
522         unregister_parisc_driver(&parport_driver);
523 }
524
525 module_init(parport_gsc_init);
526 module_exit(parport_gsc_exit);