xref: /linux/drivers/parport/parport_gsc.c (revision 2b8232ce512105e28453f301d1510de8363bccd1)
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
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/delay.h>
27 #include <linux/errno.h>
28 #include <linux/interrupt.h>
29 #include <linux/ioport.h>
30 #include <linux/kernel.h>
31 #include <linux/slab.h>
32 #include <linux/pci.h>
33 #include <linux/sysctl.h>
34 
35 #include <asm/io.h>
36 #include <asm/dma.h>
37 #include <asm/uaccess.h>
38 #include <asm/superio.h>
39 
40 #include <linux/parport.h>
41 #include <asm/pdc.h>
42 #include <asm/parisc-device.h>
43 #include <asm/hardware.h>
44 #include "parport_gsc.h"
45 
46 
47 MODULE_AUTHOR("Helge Deller <deller@gmx.de>");
48 MODULE_DESCRIPTION("HP-PARISC PC-style parallel port driver");
49 MODULE_SUPPORTED_DEVICE("integrated PC-style parallel port");
50 MODULE_LICENSE("GPL");
51 
52 
53 /*
54  * Clear TIMEOUT BIT in EPP MODE
55  *
56  * This is also used in SPP detection.
57  */
58 static int clear_epp_timeout(struct parport *pb)
59 {
60 	unsigned char r;
61 
62 	if (!(parport_gsc_read_status(pb) & 0x01))
63 		return 1;
64 
65 	/* To clear timeout some chips require double read */
66 	parport_gsc_read_status(pb);
67 	r = parport_gsc_read_status(pb);
68 	parport_writeb (r | 0x01, STATUS (pb)); /* Some reset by writing 1 */
69 	parport_writeb (r & 0xfe, STATUS (pb)); /* Others by writing 0 */
70 	r = parport_gsc_read_status(pb);
71 
72 	return !(r & 0x01);
73 }
74 
75 /*
76  * Access functions.
77  *
78  * Most of these aren't static because they may be used by the
79  * parport_xxx_yyy macros.  extern __inline__ versions of several
80  * of these are in parport_gsc.h.
81  */
82 
83 static irqreturn_t parport_gsc_interrupt(int irq, void *dev_id)
84 {
85 	parport_generic_irq(irq, (struct parport *) dev_id);
86 	return IRQ_HANDLED;
87 }
88 
89 void parport_gsc_init_state(struct pardevice *dev, struct parport_state *s)
90 {
91 	s->u.pc.ctr = 0xc | (dev->irq_func ? 0x10 : 0x0);
92 }
93 
94 void parport_gsc_save_state(struct parport *p, struct parport_state *s)
95 {
96 	s->u.pc.ctr = parport_readb (CONTROL (p));
97 }
98 
99 void parport_gsc_restore_state(struct parport *p, struct parport_state *s)
100 {
101 	parport_writeb (s->u.pc.ctr, CONTROL (p));
102 }
103 
104 struct parport_operations parport_gsc_ops =
105 {
106 	.write_data	= parport_gsc_write_data,
107 	.read_data	= parport_gsc_read_data,
108 
109 	.write_control	= parport_gsc_write_control,
110 	.read_control	= parport_gsc_read_control,
111 	.frob_control	= parport_gsc_frob_control,
112 
113 	.read_status	= parport_gsc_read_status,
114 
115 	.enable_irq	= parport_gsc_enable_irq,
116 	.disable_irq	= parport_gsc_disable_irq,
117 
118 	.data_forward	= parport_gsc_data_forward,
119 	.data_reverse	= parport_gsc_data_reverse,
120 
121 	.init_state	= parport_gsc_init_state,
122 	.save_state	= parport_gsc_save_state,
123 	.restore_state	= parport_gsc_restore_state,
124 
125 	.epp_write_data	= parport_ieee1284_epp_write_data,
126 	.epp_read_data	= parport_ieee1284_epp_read_data,
127 	.epp_write_addr	= parport_ieee1284_epp_write_addr,
128 	.epp_read_addr	= parport_ieee1284_epp_read_addr,
129 
130 	.ecp_write_data	= parport_ieee1284_ecp_write_data,
131 	.ecp_read_data	= parport_ieee1284_ecp_read_data,
132 	.ecp_write_addr	= parport_ieee1284_ecp_write_addr,
133 
134 	.compat_write_data 	= parport_ieee1284_write_compat,
135 	.nibble_read_data	= parport_ieee1284_read_nibble,
136 	.byte_read_data		= parport_ieee1284_read_byte,
137 
138 	.owner		= THIS_MODULE,
139 };
140 
141 /* --- Mode detection ------------------------------------- */
142 
143 /*
144  * Checks for port existence, all ports support SPP MODE
145  */
146 static int __devinit parport_SPP_supported(struct parport *pb)
147 {
148 	unsigned char r, w;
149 
150 	/*
151 	 * first clear an eventually pending EPP timeout
152 	 * I (sailer@ife.ee.ethz.ch) have an SMSC chipset
153 	 * that does not even respond to SPP cycles if an EPP
154 	 * timeout is pending
155 	 */
156 	clear_epp_timeout(pb);
157 
158 	/* Do a simple read-write test to make sure the port exists. */
159 	w = 0xc;
160 	parport_writeb (w, CONTROL (pb));
161 
162 	/* Is there a control register that we can read from?  Some
163 	 * ports don't allow reads, so read_control just returns a
164 	 * software copy. Some ports _do_ allow reads, so bypass the
165 	 * software copy here.  In addition, some bits aren't
166 	 * writable. */
167 	r = parport_readb (CONTROL (pb));
168 	if ((r & 0xf) == w) {
169 		w = 0xe;
170 		parport_writeb (w, CONTROL (pb));
171 		r = parport_readb (CONTROL (pb));
172 		parport_writeb (0xc, CONTROL (pb));
173 		if ((r & 0xf) == w)
174 			return PARPORT_MODE_PCSPP;
175 	}
176 
177 	/* Try the data register.  The data lines aren't tri-stated at
178 	 * this stage, so we expect back what we wrote. */
179 	w = 0xaa;
180 	parport_gsc_write_data (pb, w);
181 	r = parport_gsc_read_data (pb);
182 	if (r == w) {
183 		w = 0x55;
184 		parport_gsc_write_data (pb, w);
185 		r = parport_gsc_read_data (pb);
186 		if (r == w)
187 			return PARPORT_MODE_PCSPP;
188 	}
189 
190 	return 0;
191 }
192 
193 /* Detect PS/2 support.
194  *
195  * Bit 5 (0x20) sets the PS/2 data direction; setting this high
196  * allows us to read data from the data lines.  In theory we would get back
197  * 0xff but any peripheral attached to the port may drag some or all of the
198  * lines down to zero.  So if we get back anything that isn't the contents
199  * of the data register we deem PS/2 support to be present.
200  *
201  * Some SPP ports have "half PS/2" ability - you can't turn off the line
202  * drivers, but an external peripheral with sufficiently beefy drivers of
203  * its own can overpower them and assert its own levels onto the bus, from
204  * where they can then be read back as normal.  Ports with this property
205  * and the right type of device attached are likely to fail the SPP test,
206  * (as they will appear to have stuck bits) and so the fact that they might
207  * be misdetected here is rather academic.
208  */
209 
210 static int __devinit parport_PS2_supported(struct parport *pb)
211 {
212 	int ok = 0;
213 
214 	clear_epp_timeout(pb);
215 
216 	/* try to tri-state the buffer */
217 	parport_gsc_data_reverse (pb);
218 
219 	parport_gsc_write_data(pb, 0x55);
220 	if (parport_gsc_read_data(pb) != 0x55) ok++;
221 
222 	parport_gsc_write_data(pb, 0xaa);
223 	if (parport_gsc_read_data(pb) != 0xaa) ok++;
224 
225 	/* cancel input mode */
226 	parport_gsc_data_forward (pb);
227 
228 	if (ok) {
229 		pb->modes |= PARPORT_MODE_TRISTATE;
230 	} else {
231 		struct parport_gsc_private *priv = pb->private_data;
232 		priv->ctr_writable &= ~0x20;
233 	}
234 
235 	return ok;
236 }
237 
238 
239 /* --- Initialisation code -------------------------------- */
240 
241 struct parport *__devinit parport_gsc_probe_port (unsigned long base,
242 						 unsigned long base_hi,
243 						 int irq, int dma,
244 						 struct pci_dev *dev)
245 {
246 	struct parport_gsc_private *priv;
247 	struct parport_operations *ops;
248 	struct parport tmp;
249 	struct parport *p = &tmp;
250 
251 	priv = kzalloc (sizeof (struct parport_gsc_private), GFP_KERNEL);
252 	if (!priv) {
253 		printk (KERN_DEBUG "parport (0x%lx): no memory!\n", base);
254 		return NULL;
255 	}
256 	ops = kmalloc (sizeof (struct parport_operations), GFP_KERNEL);
257 	if (!ops) {
258 		printk (KERN_DEBUG "parport (0x%lx): no memory for ops!\n",
259 			base);
260 		kfree (priv);
261 		return NULL;
262 	}
263 	memcpy (ops, &parport_gsc_ops, sizeof (struct parport_operations));
264 	priv->ctr = 0xc;
265 	priv->ctr_writable = 0xff;
266 	priv->dma_buf = 0;
267 	priv->dma_handle = 0;
268 	priv->dev = dev;
269 	p->base = base;
270 	p->base_hi = base_hi;
271 	p->irq = irq;
272 	p->dma = dma;
273 	p->modes = PARPORT_MODE_PCSPP | PARPORT_MODE_SAFEININT;
274 	p->ops = ops;
275 	p->private_data = priv;
276 	p->physport = p;
277 	if (!parport_SPP_supported (p)) {
278 		/* No port. */
279 		kfree (priv);
280 		return NULL;
281 	}
282 	parport_PS2_supported (p);
283 
284 	if (!(p = parport_register_port(base, PARPORT_IRQ_NONE,
285 					PARPORT_DMA_NONE, ops))) {
286 		kfree (priv);
287 		kfree (ops);
288 		return NULL;
289 	}
290 
291 	p->base_hi = base_hi;
292 	p->modes = tmp.modes;
293 	p->size = (p->modes & PARPORT_MODE_EPP)?8:3;
294 	p->private_data = priv;
295 
296 	printk(KERN_INFO "%s: PC-style at 0x%lx", p->name, p->base);
297 	p->irq = irq;
298 	if (p->irq == PARPORT_IRQ_AUTO) {
299 		p->irq = PARPORT_IRQ_NONE;
300 	}
301 	if (p->irq != PARPORT_IRQ_NONE) {
302 		printk(", irq %d", p->irq);
303 
304 		if (p->dma == PARPORT_DMA_AUTO) {
305 			p->dma = PARPORT_DMA_NONE;
306 		}
307 	}
308 	if (p->dma == PARPORT_DMA_AUTO) /* To use DMA, giving the irq
309                                            is mandatory (see above) */
310 		p->dma = PARPORT_DMA_NONE;
311 
312 	printk(" [");
313 #define printmode(x) {if(p->modes&PARPORT_MODE_##x){printk("%s%s",f?",":"",#x);f++;}}
314 	{
315 		int f = 0;
316 		printmode(PCSPP);
317 		printmode(TRISTATE);
318 		printmode(COMPAT)
319 		printmode(EPP);
320 //		printmode(ECP);
321 //		printmode(DMA);
322 	}
323 #undef printmode
324 	printk("]\n");
325 
326 	if (p->irq != PARPORT_IRQ_NONE) {
327 		if (request_irq (p->irq, parport_gsc_interrupt,
328 				 0, p->name, p)) {
329 			printk (KERN_WARNING "%s: irq %d in use, "
330 				"resorting to polled operation\n",
331 				p->name, p->irq);
332 			p->irq = PARPORT_IRQ_NONE;
333 			p->dma = PARPORT_DMA_NONE;
334 		}
335 	}
336 
337 	/* Done probing.  Now put the port into a sensible start-up state. */
338 
339 	parport_gsc_write_data(p, 0);
340 	parport_gsc_data_forward (p);
341 
342 	/* Now that we've told the sharing engine about the port, and
343 	   found out its characteristics, let the high-level drivers
344 	   know about it. */
345 	parport_announce_port (p);
346 
347 	return p;
348 }
349 
350 
351 #define PARPORT_GSC_OFFSET 0x800
352 
353 static int __devinitdata parport_count;
354 
355 static int __devinit parport_init_chip(struct parisc_device *dev)
356 {
357 	struct parport *p;
358 	unsigned long port;
359 
360 	if (!dev->irq) {
361 		printk(KERN_WARNING "IRQ not found for parallel device at 0x%lx\n",
362 			dev->hpa.start);
363 		return -ENODEV;
364 	}
365 
366 	port = dev->hpa.start + PARPORT_GSC_OFFSET;
367 
368 	/* some older machines with ASP-chip don't support
369 	 * the enhanced parport modes.
370 	 */
371 	if (boot_cpu_data.cpu_type > pcxt && !pdc_add_valid(port+4)) {
372 
373 		/* Initialize bidirectional-mode (0x10) & data-tranfer-mode #1 (0x20) */
374 		printk("%s: initialize bidirectional-mode.\n", __FUNCTION__);
375 		parport_writeb ( (0x10 + 0x20), port + 4);
376 
377 	} else {
378 		printk("%s: enhanced parport-modes not supported.\n", __FUNCTION__);
379 	}
380 
381 	p = parport_gsc_probe_port(port, 0, dev->irq,
382 			/* PARPORT_IRQ_NONE */ PARPORT_DMA_NONE, NULL);
383 	if (p)
384 		parport_count++;
385 	dev->dev.driver_data = p;
386 
387 	return 0;
388 }
389 
390 static int __devexit parport_remove_chip(struct parisc_device *dev)
391 {
392 	struct parport *p = dev->dev.driver_data;
393 	if (p) {
394 		struct parport_gsc_private *priv = p->private_data;
395 		struct parport_operations *ops = p->ops;
396 		parport_remove_port(p);
397 		if (p->dma != PARPORT_DMA_NONE)
398 			free_dma(p->dma);
399 		if (p->irq != PARPORT_IRQ_NONE)
400 			free_irq(p->irq, p);
401 		if (priv->dma_buf)
402 			pci_free_consistent(priv->dev, PAGE_SIZE,
403 					    priv->dma_buf,
404 					    priv->dma_handle);
405 		kfree (p->private_data);
406 		parport_put_port(p);
407 		kfree (ops); /* hope no-one cached it */
408 	}
409 	return 0;
410 }
411 
412 static struct parisc_device_id parport_tbl[] = {
413 	{ HPHW_FIO, HVERSION_REV_ANY_ID, HVERSION_ANY_ID, 0x74 },
414 	{ 0, }
415 };
416 
417 MODULE_DEVICE_TABLE(parisc, parport_tbl);
418 
419 static struct parisc_driver parport_driver = {
420 	.name		= "Parallel",
421 	.id_table	= parport_tbl,
422 	.probe		= parport_init_chip,
423 	.remove		= __devexit_p(parport_remove_chip),
424 };
425 
426 int __devinit parport_gsc_init(void)
427 {
428 	return register_parisc_driver(&parport_driver);
429 }
430 
431 static void __devexit parport_gsc_exit(void)
432 {
433 	unregister_parisc_driver(&parport_driver);
434 }
435 
436 module_init(parport_gsc_init);
437 module_exit(parport_gsc_exit);
438