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 * 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 #include <linux/module.h>
23 #include <linux/init.h>
24 #include <linux/sched.h>
25 #include <linux/delay.h>
26 #include <linux/errno.h>
27 #include <linux/interrupt.h>
28 #include <linux/ioport.h>
29 #include <linux/kernel.h>
30 #include <linux/malloc.h>
31 #include <linux/pci.h>
32 #include <linux/sysctl.h>
33
34 #include <asm/io.h>
35 #include <asm/dma.h>
36 #include <asm/uaccess.h>
37
38 #include <linux/parport.h>
39 #include <asm/gsc.h>
40 #include <asm/pdc.h>
41 #include <asm/hardware.h>
42 #include <asm/parport_gsc.h>
43
44
45 #undef DEBUG /* undef for production */
46
47 #ifdef DEBUG
48 #define DPRINTK printk
49 #else
50 #define DPRINTK(stuff...)
51 #endif
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 void parport_gsc_interrupt(int irq, void *dev_id, struct pt_regs *regs)
85 {
86 DPRINTK(__FILE__ ": got IRQ\n");
87 parport_generic_irq(irq, (struct parport *) dev_id, regs);
88 }
89
90 void parport_gsc_write_data(struct parport *p, unsigned char d)
91 {
92 DPRINTK(__FILE__ ": write (0x%02x) %c \n", d, d);
93 parport_writeb (d, DATA (p));
94 }
95
96 unsigned char parport_gsc_read_data(struct parport *p)
97 {
98 #ifdef DEBUG
99 unsigned char c = parport_readb (DATA (p));
100 DPRINTK(__FILE__ ": read (0x%02x) %c\n", c,c);
101 return c;
102 #else
103 return parport_readb (DATA (p));
104 #endif
105 }
106
107 void parport_gsc_write_control(struct parport *p, unsigned char d)
108 {
109 const unsigned char wm = (PARPORT_CONTROL_STROBE |
110 PARPORT_CONTROL_AUTOFD |
111 PARPORT_CONTROL_INIT |
112 PARPORT_CONTROL_SELECT);
113
114 /* Take this out when drivers have adapted to the newer interface. */
115 if (d & 0x20) {
116 printk (KERN_DEBUG "%s (%s): use data_reverse for this!\n",
117 p->name, p->cad->name);
118 parport_gsc_data_reverse (p);
119 }
120
121 __parport_gsc_frob_control (p, wm, d & wm);
122 }
123
124 unsigned char parport_gsc_read_control(struct parport *p)
125 {
126 const unsigned char wm = (PARPORT_CONTROL_STROBE |
127 PARPORT_CONTROL_AUTOFD |
128 PARPORT_CONTROL_INIT |
129 PARPORT_CONTROL_SELECT);
130 const struct parport_gsc_private *priv = p->physport->private_data;
131 return priv->ctr & wm; /* Use soft copy */
132 }
133
134 unsigned char parport_gsc_frob_control (struct parport *p, unsigned char mask,
135 unsigned char val)
136 {
137 const unsigned char wm = (PARPORT_CONTROL_STROBE |
138 PARPORT_CONTROL_AUTOFD |
139 PARPORT_CONTROL_INIT |
140 PARPORT_CONTROL_SELECT);
141
142 /* Take this out when drivers have adapted to the newer interface. */
143 if (mask & 0x20) {
144 printk (KERN_DEBUG "%s (%s): use data_%s for this!\n",
145 p->name, p->cad->name,
146 (val & 0x20) ? "reverse" : "forward");
147 if (val & 0x20)
148 parport_gsc_data_reverse (p);
149 else
150 parport_gsc_data_forward (p);
151 }
152
153 /* Restrict mask and val to control lines. */
154 mask &= wm;
155 val &= wm;
156
157 return __parport_gsc_frob_control (p, mask, val);
158 }
159
160 unsigned char parport_gsc_read_status(struct parport *p)
161 {
162 return parport_readb (STATUS (p));
163 }
164
165 void parport_gsc_disable_irq(struct parport *p)
166 {
167 __parport_gsc_frob_control (p, 0x10, 0);
168 }
169
170 void parport_gsc_enable_irq(struct parport *p)
171 {
172 __parport_gsc_frob_control (p, 0x10, 0x10);
173 }
174
175 void parport_gsc_data_forward (struct parport *p)
176 {
177 __parport_gsc_frob_control (p, 0x20, 0);
178 }
179
180 void parport_gsc_data_reverse (struct parport *p)
181 {
182 __parport_gsc_frob_control (p, 0x20, 0x20);
183 }
184
185 void parport_gsc_init_state(struct pardevice *dev, struct parport_state *s)
186 {
187 s->u.pc.ctr = 0xc | (dev->irq_func ? 0x10 : 0x0);
188 }
189
190 void parport_gsc_save_state(struct parport *p, struct parport_state *s)
191 {
192 s->u.pc.ctr = parport_readb (CONTROL (p));
193 }
194
195 void parport_gsc_restore_state(struct parport *p, struct parport_state *s)
196 {
197 parport_writeb (s->u.pc.ctr, CONTROL (p));
198 }
199
200 void parport_gsc_inc_use_count(void)
201 {
202 #ifdef MODULE
203 MOD_INC_USE_COUNT;
204 #endif
205 }
206
207 void parport_gsc_dec_use_count(void)
208 {
209 #ifdef MODULE
210 MOD_DEC_USE_COUNT;
211 #endif
212 }
213
214
215 struct parport_operations parport_gsc_ops =
216 {
217 write_data: parport_gsc_write_data,
218 read_data: parport_gsc_read_data,
219
220 write_control: parport_gsc_write_control,
221 read_control: parport_gsc_read_control,
222 frob_control: parport_gsc_frob_control,
223
224 read_status: parport_gsc_read_status,
225
226 enable_irq: parport_gsc_enable_irq,
227 disable_irq: parport_gsc_disable_irq,
228
229 data_forward: parport_gsc_data_forward,
230 data_reverse: parport_gsc_data_reverse,
231
232 init_state: parport_gsc_init_state,
233 save_state: parport_gsc_save_state,
234 restore_state: parport_gsc_restore_state,
235
236 inc_use_count: parport_gsc_inc_use_count,
237 dec_use_count: parport_gsc_dec_use_count,
238
239 epp_write_data: parport_ieee1284_epp_write_data,
240 epp_read_data: parport_ieee1284_epp_read_data,
241 epp_write_addr: parport_ieee1284_epp_write_addr,
242 epp_read_addr: parport_ieee1284_epp_read_addr,
243
244 ecp_write_data: parport_ieee1284_ecp_write_data,
245 ecp_read_data: parport_ieee1284_ecp_read_data,
246 ecp_write_addr: parport_ieee1284_ecp_write_addr,
247
248 compat_write_data: parport_ieee1284_write_compat,
249 nibble_read_data: parport_ieee1284_read_nibble,
250 byte_read_data: parport_ieee1284_read_byte,
251 };
252
253 /* --- Mode detection ------------------------------------- */
254
255 /*
256 * Checks for port existence, all ports support SPP MODE
257 */
258 static int __devinit parport_SPP_supported(struct parport *pb)
259 {
260 unsigned char r, w;
261
262 /*
263 * first clear an eventually pending EPP timeout
264 * I (sailer@ife.ee.ethz.ch) have an SMSC chipset
265 * that does not even respond to SPP cycles if an EPP
266 * timeout is pending
267 */
268 clear_epp_timeout(pb);
269
270 /* Do a simple read-write test to make sure the port exists. */
271 w = 0xc;
272 parport_writeb (w, CONTROL (pb));
273
274 /* Is there a control register that we can read from? Some
275 * ports don't allow reads, so read_control just returns a
276 * software copy. Some ports _do_ allow reads, so bypass the
277 * software copy here. In addition, some bits aren't
278 * writable. */
279 r = parport_readb (CONTROL (pb));
280 if ((r & 0xf) == w) {
281 w = 0xe;
282 parport_writeb (w, CONTROL (pb));
283 r = parport_readb (CONTROL (pb));
284 parport_writeb (0xc, CONTROL (pb));
285 if ((r & 0xf) == w)
286 return PARPORT_MODE_PCSPP;
287 }
288
289 /* Try the data register. The data lines aren't tri-stated at
290 * this stage, so we expect back what we wrote. */
291 w = 0xaa;
292 parport_gsc_write_data (pb, w);
293 r = parport_gsc_read_data (pb);
294 if (r == w) {
295 w = 0x55;
296 parport_gsc_write_data (pb, w);
297 r = parport_gsc_read_data (pb);
298 if (r == w)
299 return PARPORT_MODE_PCSPP;
300 }
301
302 return 0;
303 }
304
305 /* Detect PS/2 support.
306 *
307 * Bit 5 (0x20) sets the PS/2 data direction; setting this high
308 * allows us to read data from the data lines. In theory we would get back
309 * 0xff but any peripheral attached to the port may drag some or all of the
310 * lines down to zero. So if we get back anything that isn't the contents
311 * of the data register we deem PS/2 support to be present.
312 *
313 * Some SPP ports have "half PS/2" ability - you can't turn off the line
314 * drivers, but an external peripheral with sufficiently beefy drivers of
315 * its own can overpower them and assert its own levels onto the bus, from
316 * where they can then be read back as normal. Ports with this property
317 * and the right type of device attached are likely to fail the SPP test,
318 * (as they will appear to have stuck bits) and so the fact that they might
319 * be misdetected here is rather academic.
320 */
321
322 static int __devinit parport_PS2_supported(struct parport *pb)
323 {
324 int ok = 0;
325
326 clear_epp_timeout(pb);
327
328 /* try to tri-state the buffer */
329 parport_gsc_data_reverse (pb);
330
331 parport_gsc_write_data(pb, 0x55);
332 if (parport_gsc_read_data(pb) != 0x55) ok++;
333
334 parport_gsc_write_data(pb, 0xaa);
335 if (parport_gsc_read_data(pb) != 0xaa) ok++;
336
337 /* cancel input mode */
338 parport_gsc_data_forward (pb);
339
340 if (ok) {
341 pb->modes |= PARPORT_MODE_TRISTATE;
342 } else {
343 struct parport_gsc_private *priv = pb->private_data;
344 priv->ctr_writable &= ~0x20;
345 }
346
347 return ok;
348 }
349
350
351 /* --- Initialisation code -------------------------------- */
352
353 struct parport *__devinit parport_gsc_probe_port (unsigned long base,
354 unsigned long base_hi,
355 int irq, int dma,
356 struct pci_dev *dev)
357 {
358 struct parport_gsc_private *priv;
359 struct parport_operations *ops;
360 struct parport tmp;
361 struct parport *p = &tmp;
362
363 if (check_region(base, 3))
364 return NULL;
365
366 priv = kmalloc (sizeof (struct parport_gsc_private), GFP_KERNEL);
367 if (!priv) {
368 printk (KERN_DEBUG "parport (0x%lx): no memory!\n", base);
369 return NULL;
370 }
371 ops = kmalloc (sizeof (struct parport_operations), GFP_KERNEL);
372 if (!ops) {
373 printk (KERN_DEBUG "parport (0x%lx): no memory for ops!\n",
374 base);
375 kfree (priv);
376 return NULL;
377 }
378 memcpy (ops, &parport_gsc_ops, sizeof (struct parport_operations));
379 priv->ctr = 0xc;
380 priv->ctr_writable = 0xff;
381 priv->dma_buf = 0;
382 priv->dma_handle = 0;
383 priv->dev = dev;
384 p->base = base;
385 p->base_hi = base_hi;
386 p->irq = irq;
387 p->dma = dma;
388 p->modes = PARPORT_MODE_PCSPP | PARPORT_MODE_SAFEININT;
389 p->ops = ops;
390 p->private_data = priv;
391 p->physport = p;
392 if (!parport_SPP_supported (p)) {
393 /* No port. */
394 kfree (priv);
395 return NULL;
396 }
397 parport_PS2_supported (p);
398
399 if (!(p = parport_register_port(base, PARPORT_IRQ_NONE,
400 PARPORT_DMA_NONE, ops))) {
401 kfree (priv);
402 kfree (ops);
403 return NULL;
404 }
405
406 p->base_hi = base_hi;
407 p->modes = tmp.modes;
408 p->size = (p->modes & PARPORT_MODE_EPP)?8:3;
409 p->private_data = priv;
410
411 printk(KERN_INFO "%s: PC-style at 0x%lx", p->name, p->base);
412 p->irq = irq;
413 if (p->irq == PARPORT_IRQ_AUTO) {
414 p->irq = PARPORT_IRQ_NONE;
415 }
416 if (p->irq != PARPORT_IRQ_NONE) {
417 printk(", irq %d", p->irq);
418
419 if (p->dma == PARPORT_DMA_AUTO) {
420 p->dma = PARPORT_DMA_NONE;
421 }
422 }
423 if (p->dma == PARPORT_DMA_AUTO) /* To use DMA, giving the irq
424 is mandatory (see above) */
425 p->dma = PARPORT_DMA_NONE;
426
427 printk(" [");
428 #define printmode(x) {if(p->modes&PARPORT_MODE_##x){printk("%s%s",f?",":"",#x);f++;}}
429 {
430 int f = 0;
431 printmode(PCSPP);
432 printmode(TRISTATE);
433 printmode(COMPAT)
434 printmode(EPP);
435 // printmode(ECP);
436 // printmode(DMA);
437 }
438 #undef printmode
439 printk("]\n");
440 parport_proc_register(p);
441
442 request_region (p->base, 3, p->name);
443 if (p->size > 3)
444 request_region (p->base + 3, p->size - 3, p->name);
445 if (p->modes & PARPORT_MODE_ECP)
446 request_region (p->base_hi, 3, p->name);
447
448 if (p->irq != PARPORT_IRQ_NONE) {
449 if (request_irq (p->irq, parport_gsc_interrupt,
450 0, p->name, p)) {
451 printk (KERN_WARNING "%s: irq %d in use, "
452 "resorting to polled operation\n",
453 p->name, p->irq);
454 p->irq = PARPORT_IRQ_NONE;
455 p->dma = PARPORT_DMA_NONE;
456 }
457 }
458
459 /* Done probing. Now put the port into a sensible start-up state. */
460
461 parport_gsc_write_data(p, 0);
462 parport_gsc_data_forward (p);
463
464 /* Now that we've told the sharing engine about the port, and
465 found out its characteristics, let the high-level drivers
466 know about it. */
467 parport_announce_port (p);
468
469 return p;
470 }
471
472
473 #define PARPORT_GSC_OFFSET 0x800
474
475 static int __initdata parport_count;
476
477 static int __init
478 parport_init_chip(struct hp_device *d, struct pa_iodc_driver *dri)
479 {
480 unsigned long port;
481 int irq;
482
483 irq = busdevice_alloc_irq(d);
484
485 if (!irq) {
486 printk("IRQ not found for parallel device at 0x%p\n", d->hpa);
487 return -ENODEV;
488 }
489
490 port = ((unsigned long) d->hpa) + PARPORT_GSC_OFFSET;
491
492 /*
493 some older machines with ASP-chip don't support the enhanced parport modes
494 */
495 if (!pdc_add_valid( (void *)(port+4))) {
496 /* Initialize bidirectional-mode (0x10) & data-tranfer-mode #1 (0x20) */
497 printk("%s: initialize bidirectional-mode.\n", __FUNCTION__);
498 parport_writeb ( (0x10 + 0x20), port + 4);
499 } else {
500 printk("%s: enhanced parport-modes not supported.\n", __FUNCTION__);
501 }
502
503 if (parport_gsc_probe_port(port, 0,
504 irq, /* PARPORT_IRQ_NONE */
505 PARPORT_DMA_NONE, NULL))
506 parport_count++;
507
508 return 0;
509 }
510
511 static struct pa_iodc_driver parport_drivers_for[] __initdata = {
512 {HPHW_FIO, 0x0, 0x0, 0x74, 0x0, 0, /* 715/64 */
513 DRIVER_CHECK_SVERSION + DRIVER_CHECK_HWTYPE,
514 "parallel device", "HP 7xx - Series", (void *) parport_init_chip},
515 { 0 }
516 };
517
518 int __init
519 parport_gsc_init ( void )
520 {
521 parport_count = 0;
522
523 register_driver(parport_drivers_for);
524
525 return parport_count;
526 }
527
528 /* Exported symbols. */
529 EXPORT_NO_SYMBOLS;
530
531
532 #ifdef MODULE
533
534 MODULE_AUTHOR("Helge Deller <deller@gmx.de>");
535 MODULE_DESCRIPTION("HP-PARISC PC-style parallel port driver");
536 MODULE_SUPPORTED_DEVICE("integrated PC-style parallel port");
537
538 int init_module(void)
539 {
540 return !parport_gsc_init ();
541 }
542
543 void cleanup_module(void)
544 {
545 struct parport *p = parport_enumerate(), *tmp;
546 while (p) {
547 tmp = p->next;
548 if (p->modes & PARPORT_MODE_PCSPP) {
549 struct parport_gsc_private *priv = p->private_data;
550 struct parport_operations *ops = p->ops;
551 if (p->dma != PARPORT_DMA_NONE)
552 free_dma(p->dma);
553 if (p->irq != PARPORT_IRQ_NONE)
554 free_irq(p->irq, p);
555 release_region(p->base, 3);
556 if (p->size > 3)
557 release_region(p->base + 3, p->size - 3);
558 if (p->modes & PARPORT_MODE_ECP)
559 release_region(p->base_hi, 3);
560 parport_proc_unregister(p);
561 if (priv->dma_buf)
562 pci_free_consistent(priv->dev, PAGE_SIZE,
563 priv->dma_buf,
564 priv->dma_handle);
565 kfree (p->private_data);
566 parport_unregister_port(p);
567 kfree (ops); /* hope no-one cached it */
568 }
569 p = tmp;
570 }
571 }
572 #endif
573
This page was automatically generated by the
LXR engine.
Visit the LXR main site for more
information.