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