blob: 81082d5899b060876e4ad7c317a2dab780327877 [file] [log] [blame]
Thomas Gleixner2874c5f2019-05-27 08:55:01 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * Low-level parallel-support for PC-style hardware integrated in the
4 * LASI-Controller (on GSC-Bus) for HP-PARISC Workstations
5 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 * (C) 1999-2001 by Helge Deller <deller@gmx.de>
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 *
8 * based on parport_pc.c by
9 * Grant Guenther <grant@torque.net>
10 * Phil Blundell <philb@gnu.org>
11 * Tim Waugh <tim@cyberelk.demon.co.uk>
12 * Jose Renau <renau@acm.org>
Adrian Bunkbdca3f22006-06-26 18:19:23 +020013 * David Campbell
Linus Torvalds1da177e2005-04-16 15:20:36 -070014 * Andrea Arcangeli
15 */
16
17#undef DEBUG /* undef for production */
18
19#include <linux/module.h>
20#include <linux/init.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include <linux/delay.h>
22#include <linux/errno.h>
23#include <linux/interrupt.h>
24#include <linux/ioport.h>
25#include <linux/kernel.h>
26#include <linux/slab.h>
27#include <linux/pci.h>
28#include <linux/sysctl.h>
29
30#include <asm/io.h>
31#include <asm/dma.h>
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080032#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include <asm/superio.h>
34
35#include <linux/parport.h>
36#include <asm/pdc.h>
37#include <asm/parisc-device.h>
38#include <asm/hardware.h>
39#include "parport_gsc.h"
40
41
42MODULE_AUTHOR("Helge Deller <deller@gmx.de>");
43MODULE_DESCRIPTION("HP-PARISC PC-style parallel port driver");
44MODULE_SUPPORTED_DEVICE("integrated PC-style parallel port");
45MODULE_LICENSE("GPL");
46
47
48/*
49 * Clear TIMEOUT BIT in EPP MODE
50 *
51 * This is also used in SPP detection.
52 */
53static int clear_epp_timeout(struct parport *pb)
54{
55 unsigned char r;
56
57 if (!(parport_gsc_read_status(pb) & 0x01))
58 return 1;
59
60 /* To clear timeout some chips require double read */
61 parport_gsc_read_status(pb);
62 r = parport_gsc_read_status(pb);
63 parport_writeb (r | 0x01, STATUS (pb)); /* Some reset by writing 1 */
64 parport_writeb (r & 0xfe, STATUS (pb)); /* Others by writing 0 */
65 r = parport_gsc_read_status(pb);
66
67 return !(r & 0x01);
68}
69
70/*
71 * Access functions.
72 *
73 * Most of these aren't static because they may be used by the
74 * parport_xxx_yyy macros. extern __inline__ versions of several
75 * of these are in parport_gsc.h.
76 */
77
Linus Torvalds1da177e2005-04-16 15:20:36 -070078void parport_gsc_init_state(struct pardevice *dev, struct parport_state *s)
79{
80 s->u.pc.ctr = 0xc | (dev->irq_func ? 0x10 : 0x0);
81}
82
83void parport_gsc_save_state(struct parport *p, struct parport_state *s)
84{
85 s->u.pc.ctr = parport_readb (CONTROL (p));
86}
87
88void parport_gsc_restore_state(struct parport *p, struct parport_state *s)
89{
90 parport_writeb (s->u.pc.ctr, CONTROL (p));
91}
92
93struct parport_operations parport_gsc_ops =
94{
95 .write_data = parport_gsc_write_data,
96 .read_data = parport_gsc_read_data,
97
98 .write_control = parport_gsc_write_control,
99 .read_control = parport_gsc_read_control,
100 .frob_control = parport_gsc_frob_control,
101
102 .read_status = parport_gsc_read_status,
103
104 .enable_irq = parport_gsc_enable_irq,
105 .disable_irq = parport_gsc_disable_irq,
106
107 .data_forward = parport_gsc_data_forward,
108 .data_reverse = parport_gsc_data_reverse,
109
110 .init_state = parport_gsc_init_state,
111 .save_state = parport_gsc_save_state,
112 .restore_state = parport_gsc_restore_state,
113
114 .epp_write_data = parport_ieee1284_epp_write_data,
115 .epp_read_data = parport_ieee1284_epp_read_data,
116 .epp_write_addr = parport_ieee1284_epp_write_addr,
117 .epp_read_addr = parport_ieee1284_epp_read_addr,
118
119 .ecp_write_data = parport_ieee1284_ecp_write_data,
120 .ecp_read_data = parport_ieee1284_ecp_read_data,
121 .ecp_write_addr = parport_ieee1284_ecp_write_addr,
122
123 .compat_write_data = parport_ieee1284_write_compat,
124 .nibble_read_data = parport_ieee1284_read_nibble,
125 .byte_read_data = parport_ieee1284_read_byte,
126
127 .owner = THIS_MODULE,
128};
129
130/* --- Mode detection ------------------------------------- */
131
132/*
133 * Checks for port existence, all ports support SPP MODE
134 */
Greg Kroah-Hartman312faca2012-12-21 13:23:14 -0800135static int parport_SPP_supported(struct parport *pb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136{
137 unsigned char r, w;
138
139 /*
140 * first clear an eventually pending EPP timeout
141 * I (sailer@ife.ee.ethz.ch) have an SMSC chipset
142 * that does not even respond to SPP cycles if an EPP
143 * timeout is pending
144 */
145 clear_epp_timeout(pb);
146
147 /* Do a simple read-write test to make sure the port exists. */
148 w = 0xc;
149 parport_writeb (w, CONTROL (pb));
150
151 /* Is there a control register that we can read from? Some
152 * ports don't allow reads, so read_control just returns a
153 * software copy. Some ports _do_ allow reads, so bypass the
154 * software copy here. In addition, some bits aren't
155 * writable. */
156 r = parport_readb (CONTROL (pb));
157 if ((r & 0xf) == w) {
158 w = 0xe;
159 parport_writeb (w, CONTROL (pb));
160 r = parport_readb (CONTROL (pb));
161 parport_writeb (0xc, CONTROL (pb));
162 if ((r & 0xf) == w)
163 return PARPORT_MODE_PCSPP;
164 }
165
166 /* Try the data register. The data lines aren't tri-stated at
167 * this stage, so we expect back what we wrote. */
168 w = 0xaa;
169 parport_gsc_write_data (pb, w);
170 r = parport_gsc_read_data (pb);
171 if (r == w) {
172 w = 0x55;
173 parport_gsc_write_data (pb, w);
174 r = parport_gsc_read_data (pb);
175 if (r == w)
176 return PARPORT_MODE_PCSPP;
177 }
178
179 return 0;
180}
181
182/* Detect PS/2 support.
183 *
184 * Bit 5 (0x20) sets the PS/2 data direction; setting this high
185 * allows us to read data from the data lines. In theory we would get back
186 * 0xff but any peripheral attached to the port may drag some or all of the
187 * lines down to zero. So if we get back anything that isn't the contents
188 * of the data register we deem PS/2 support to be present.
189 *
190 * Some SPP ports have "half PS/2" ability - you can't turn off the line
191 * drivers, but an external peripheral with sufficiently beefy drivers of
192 * its own can overpower them and assert its own levels onto the bus, from
193 * where they can then be read back as normal. Ports with this property
194 * and the right type of device attached are likely to fail the SPP test,
195 * (as they will appear to have stuck bits) and so the fact that they might
196 * be misdetected here is rather academic.
197 */
198
Greg Kroah-Hartman312faca2012-12-21 13:23:14 -0800199static int parport_PS2_supported(struct parport *pb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200{
201 int ok = 0;
202
203 clear_epp_timeout(pb);
204
205 /* try to tri-state the buffer */
206 parport_gsc_data_reverse (pb);
207
208 parport_gsc_write_data(pb, 0x55);
209 if (parport_gsc_read_data(pb) != 0x55) ok++;
210
211 parport_gsc_write_data(pb, 0xaa);
212 if (parport_gsc_read_data(pb) != 0xaa) ok++;
213
214 /* cancel input mode */
215 parport_gsc_data_forward (pb);
216
217 if (ok) {
218 pb->modes |= PARPORT_MODE_TRISTATE;
219 } else {
220 struct parport_gsc_private *priv = pb->private_data;
221 priv->ctr_writable &= ~0x20;
222 }
223
224 return ok;
225}
226
227
228/* --- Initialisation code -------------------------------- */
229
Greg Kroah-Hartman312faca2012-12-21 13:23:14 -0800230struct parport *parport_gsc_probe_port(unsigned long base,
231 unsigned long base_hi, int irq,
Helge Deller4edb3862013-05-30 21:06:39 +0000232 int dma, struct parisc_device *padev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233{
234 struct parport_gsc_private *priv;
235 struct parport_operations *ops;
236 struct parport tmp;
237 struct parport *p = &tmp;
238
Helge Dellercb6fc182006-01-17 12:40:40 -0700239 priv = kzalloc (sizeof (struct parport_gsc_private), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 if (!priv) {
241 printk (KERN_DEBUG "parport (0x%lx): no memory!\n", base);
242 return NULL;
243 }
Silviu-Mihai Popescu2451a842013-03-11 18:39:22 +0200244 ops = kmemdup(&parport_gsc_ops, sizeof(struct parport_operations),
245 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 if (!ops) {
247 printk (KERN_DEBUG "parport (0x%lx): no memory for ops!\n",
248 base);
249 kfree (priv);
250 return NULL;
251 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 priv->ctr = 0xc;
253 priv->ctr_writable = 0xff;
Helge Dellerc92826e2018-03-04 20:57:30 +0100254 priv->dma_buf = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 priv->dma_handle = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 p->base = base;
257 p->base_hi = base_hi;
258 p->irq = irq;
259 p->dma = dma;
260 p->modes = PARPORT_MODE_PCSPP | PARPORT_MODE_SAFEININT;
261 p->ops = ops;
262 p->private_data = priv;
263 p->physport = p;
264 if (!parport_SPP_supported (p)) {
265 /* No port. */
266 kfree (priv);
Wei Yongjun9986ffd2012-09-03 18:09:53 +0800267 kfree(ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 return NULL;
269 }
270 parport_PS2_supported (p);
271
272 if (!(p = parport_register_port(base, PARPORT_IRQ_NONE,
273 PARPORT_DMA_NONE, ops))) {
274 kfree (priv);
275 kfree (ops);
276 return NULL;
277 }
278
Helge Deller4edb3862013-05-30 21:06:39 +0000279 p->dev = &padev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 p->base_hi = base_hi;
281 p->modes = tmp.modes;
282 p->size = (p->modes & PARPORT_MODE_EPP)?8:3;
283 p->private_data = priv;
284
Joe Perchesdecf26f2020-04-03 14:43:16 +0100285 pr_info("%s: PC-style at 0x%lx", p->name, p->base);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 p->irq = irq;
287 if (p->irq == PARPORT_IRQ_AUTO) {
288 p->irq = PARPORT_IRQ_NONE;
289 }
290 if (p->irq != PARPORT_IRQ_NONE) {
Helge Deller83b5d1e2017-01-03 22:55:50 +0100291 pr_cont(", irq %d", p->irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292
293 if (p->dma == PARPORT_DMA_AUTO) {
294 p->dma = PARPORT_DMA_NONE;
295 }
296 }
297 if (p->dma == PARPORT_DMA_AUTO) /* To use DMA, giving the irq
298 is mandatory (see above) */
299 p->dma = PARPORT_DMA_NONE;
300
Helge Deller83b5d1e2017-01-03 22:55:50 +0100301 pr_cont(" [");
302#define printmode(x) {if(p->modes&PARPORT_MODE_##x){pr_cont("%s%s",f?",":"",#x);f++;}}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 {
304 int f = 0;
305 printmode(PCSPP);
306 printmode(TRISTATE);
307 printmode(COMPAT)
308 printmode(EPP);
309// printmode(ECP);
310// printmode(DMA);
311 }
312#undef printmode
Helge Deller83b5d1e2017-01-03 22:55:50 +0100313 pr_cont("]\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314
315 if (p->irq != PARPORT_IRQ_NONE) {
Jeff Garzik3f2e40d2007-10-19 01:42:14 -0400316 if (request_irq (p->irq, parport_irq_handler,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 0, p->name, p)) {
Joe Perchesdecf26f2020-04-03 14:43:16 +0100318 pr_warn("%s: irq %d in use, resorting to polled operation\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 p->name, p->irq);
320 p->irq = PARPORT_IRQ_NONE;
321 p->dma = PARPORT_DMA_NONE;
322 }
323 }
324
325 /* Done probing. Now put the port into a sensible start-up state. */
326
327 parport_gsc_write_data(p, 0);
328 parport_gsc_data_forward (p);
329
330 /* Now that we've told the sharing engine about the port, and
331 found out its characteristics, let the high-level drivers
332 know about it. */
333 parport_announce_port (p);
334
335 return p;
336}
337
338
339#define PARPORT_GSC_OFFSET 0x800
340
Greg Kroah-Hartman312faca2012-12-21 13:23:14 -0800341static int parport_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342
Helge Dellerf0973442017-08-21 21:58:19 +0200343static int __init parport_init_chip(struct parisc_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344{
345 struct parport *p;
346 unsigned long port;
347
348 if (!dev->irq) {
Joe Perchesdecf26f2020-04-03 14:43:16 +0100349 pr_warn("IRQ not found for parallel device at 0x%llx\n",
Alexander Beregalovb5d598b2009-05-28 14:34:33 -0700350 (unsigned long long)dev->hpa.start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 return -ENODEV;
352 }
353
Matthew Wilcox53f01bb2005-10-21 22:36:40 -0400354 port = dev->hpa.start + PARPORT_GSC_OFFSET;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355
356 /* some older machines with ASP-chip don't support
357 * the enhanced parport modes.
358 */
359 if (boot_cpu_data.cpu_type > pcxt && !pdc_add_valid(port+4)) {
360
361 /* Initialize bidirectional-mode (0x10) & data-tranfer-mode #1 (0x20) */
Harvey Harrison145980a2008-04-30 00:54:57 -0700362 printk("%s: initialize bidirectional-mode.\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 parport_writeb ( (0x10 + 0x20), port + 4);
364
365 } else {
Harvey Harrison145980a2008-04-30 00:54:57 -0700366 printk("%s: enhanced parport-modes not supported.\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 }
368
369 p = parport_gsc_probe_port(port, 0, dev->irq,
Helge Deller4edb3862013-05-30 21:06:39 +0000370 /* PARPORT_IRQ_NONE */ PARPORT_DMA_NONE, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 if (p)
372 parport_count++;
Greg Kroah-Hartman61616112009-05-04 12:40:54 -0700373 dev_set_drvdata(&dev->dev, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374
375 return 0;
376}
377
Helge Dellerf0973442017-08-21 21:58:19 +0200378static int __exit parport_remove_chip(struct parisc_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379{
Greg Kroah-Hartman61616112009-05-04 12:40:54 -0700380 struct parport *p = dev_get_drvdata(&dev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 if (p) {
382 struct parport_gsc_private *priv = p->private_data;
383 struct parport_operations *ops = p->ops;
384 parport_remove_port(p);
385 if (p->dma != PARPORT_DMA_NONE)
386 free_dma(p->dma);
387 if (p->irq != PARPORT_IRQ_NONE)
388 free_irq(p->irq, p);
389 if (priv->dma_buf)
390 pci_free_consistent(priv->dev, PAGE_SIZE,
391 priv->dma_buf,
392 priv->dma_handle);
393 kfree (p->private_data);
394 parport_put_port(p);
395 kfree (ops); /* hope no-one cached it */
396 }
397 return 0;
398}
399
Helge Dellerf0973442017-08-21 21:58:19 +0200400static const struct parisc_device_id parport_tbl[] __initconst = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 { HPHW_FIO, HVERSION_REV_ANY_ID, HVERSION_ANY_ID, 0x74 },
402 { 0, }
403};
404
405MODULE_DEVICE_TABLE(parisc, parport_tbl);
406
Helge Dellerf0973442017-08-21 21:58:19 +0200407static struct parisc_driver parport_driver __refdata = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 .name = "Parallel",
409 .id_table = parport_tbl,
410 .probe = parport_init_chip,
Helge Dellerf0973442017-08-21 21:58:19 +0200411 .remove = __exit_p(parport_remove_chip),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412};
413
Greg Kroah-Hartman312faca2012-12-21 13:23:14 -0800414int parport_gsc_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415{
416 return register_parisc_driver(&parport_driver);
417}
418
Greg Kroah-Hartman312faca2012-12-21 13:23:14 -0800419static void parport_gsc_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420{
421 unregister_parisc_driver(&parport_driver);
422}
423
424module_init(parport_gsc_init);
425module_exit(parport_gsc_exit);