i82365_isasubr.c revision 1.29 1 /* $NetBSD: i82365_isasubr.c,v 1.29 2001/11/13 08:01:15 lukem Exp $ */
2
3 /*
4 * Copyright (c) 2000 Christian E. Hopps. All rights reserved.
5 * Copyright (c) 1998 Bill Sommerfeld. All rights reserved.
6 * Copyright (c) 1997 Marc Horowitz. All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by Marc Horowitz.
19 * 4. The name of the author may not be used to endorse or promote products
20 * derived from this software without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
23 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
27 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
31 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 */
33
34 #include <sys/cdefs.h>
35 __KERNEL_RCSID(0, "$NetBSD: i82365_isasubr.c,v 1.29 2001/11/13 08:01:15 lukem Exp $");
36
37 #define PCICISADEBUG
38
39 #include <sys/types.h>
40 #include <sys/param.h>
41 #include <sys/systm.h>
42 #include <sys/device.h>
43 #include <sys/extent.h>
44 #include <sys/malloc.h>
45
46 #include <machine/bus.h>
47 #include <machine/intr.h>
48
49 #include <dev/isa/isareg.h>
50 #include <dev/isa/isavar.h>
51
52 #include <dev/pcmcia/pcmciareg.h>
53 #include <dev/pcmcia/pcmciavar.h>
54 #include <dev/pcmcia/pcmciachip.h>
55
56 #include <dev/ic/i82365reg.h>
57 #include <dev/ic/i82365var.h>
58 #include <dev/isa/i82365_isavar.h>
59
60 /*****************************************************************************
61 * Configurable parameters.
62 *****************************************************************************/
63
64 #include "opt_pcic_isa_alloc_iobase.h"
65 #include "opt_pcic_isa_alloc_iosize.h"
66 #include "opt_pcic_isa_intr_alloc_mask.h"
67
68 /*
69 * Default I/O allocation range. If both are set to non-zero, these
70 * values will be used instead. Otherwise, the code attempts to probe
71 * the bus width. Systems with 10 address bits should use 0x300 and 0xff.
72 * Systems with 12 address bits (most) should use 0x400 and 0xbff.
73 */
74
75 #ifndef PCIC_ISA_ALLOC_IOBASE
76 #define PCIC_ISA_ALLOC_IOBASE 0
77 #endif
78
79 #ifndef PCIC_ISA_ALLOC_IOSIZE
80 #define PCIC_ISA_ALLOC_IOSIZE 0
81 #endif
82
83 int pcic_isa_alloc_iobase = PCIC_ISA_ALLOC_IOBASE;
84 int pcic_isa_alloc_iosize = PCIC_ISA_ALLOC_IOSIZE;
85
86
87 /*
88 * Default IRQ allocation bitmask. This defines the range of allowable
89 * IRQs for PCMCIA slots. Useful if order of probing would screw up other
90 * devices, or if PCIC hardware/cards have trouble with certain interrupt
91 * lines.
92 */
93
94 #ifndef PCIC_ISA_INTR_ALLOC_MASK
95 #define PCIC_ISA_INTR_ALLOC_MASK 0xffff
96 #endif
97
98 int pcic_isa_intr_alloc_mask = PCIC_ISA_INTR_ALLOC_MASK;
99
100 #ifndef PCIC_IRQ_PROBE
101 #ifdef hpcmips
102 /*
103 * The irq probing doesn't work with current vrisab implementation.
104 * The irq is just an key to find matching GPIO port to use and is fixed.
105 */
106 #define PCIC_IRQ_PROBE 0
107 #else
108 #define PCIC_IRQ_PROBE 1
109 #endif
110 #endif
111
112 int pcic_irq_probe = PCIC_IRQ_PROBE;
113
114 /*****************************************************************************
115 * End of configurable parameters.
116 *****************************************************************************/
117
118 #ifdef PCICISADEBUG
119 int pcicsubr_debug = 0;
120 #define DPRINTF(arg) do { if (pcicsubr_debug) printf arg ; } while (0)
121 #else
122 #define DPRINTF(arg)
123 #endif
124
125 /*
126 * count the interrupt if we have a status set
127 * just use socket 0
128 */
129
130 void pcic_isa_probe_interrupts __P((struct pcic_softc *, struct pcic_handle *));
131 static int pcic_isa_count_intr __P((void *));
132
133 static int
134 pcic_isa_count_intr(arg)
135 void *arg;
136 {
137 struct pcic_softc *sc;
138 struct pcic_isa_softc *isc;
139 struct pcic_handle *h;
140 int cscreg;
141
142 h = arg;
143 sc = (struct pcic_softc *)h->ph_parent;
144 isc = (struct pcic_isa_softc *)h->ph_parent;
145
146 cscreg = pcic_read(h, PCIC_CSC);
147 if (cscreg & PCIC_CSC_CD) {
148 if ((++sc->intr_detect % 20) == 0)
149 printf(".");
150 else
151 DPRINTF(("."));
152 return (1);
153 }
154
155 /*
156 * make sure we don't get stuck in a loop due to
157 * unhandled level interupts
158 */
159 if (++sc->intr_false > 40) {
160 isa_intr_disestablish(isc->sc_ic, sc->ih);
161 sc->ih = 0;
162
163 pcic_write(h, PCIC_CSC_INTR, 0);
164 delay(10);
165 }
166
167 #ifdef PCICISADEBUG
168 if (cscreg)
169 DPRINTF(("o"));
170 else
171 DPRINTF(("X"));
172 #endif
173 return (cscreg ? 1 : 0);
174 }
175
176 /*
177 * use soft interrupt card detect to find out which irqs are available
178 * for this controller
179 */
180 void
181 pcic_isa_probe_interrupts(sc, h)
182 struct pcic_softc *sc;
183 struct pcic_handle *h;
184 {
185 struct pcic_isa_softc *isc = (void *) sc;
186 isa_chipset_tag_t ic;
187 int i, j, mask, irq;
188 int cd, cscintr, intr, csc;
189
190 ic = isc->sc_ic;
191
192 printf("%s: controller %d detecting irqs with mask 0x%04x:",
193 sc->dev.dv_xname, h->chip, sc->intr_mask[h->chip]);
194 DPRINTF(("\n"));
195
196 /* clear any current interrupt */
197 pcic_read(h, PCIC_CSC);
198
199 /* first disable the status irq, card detect is enabled later */
200 pcic_write(h, PCIC_CSC_INTR, 0);
201
202 /* steer the interrupt to isa and disable ring and interrupt */
203 intr = pcic_read(h, PCIC_INTR);
204 DPRINTF(("pcic: old intr 0x%x\n", intr));
205 intr &= ~(PCIC_INTR_RI_ENABLE | PCIC_INTR_ENABLE | PCIC_INTR_IRQ_MASK);
206 pcic_write(h, PCIC_INTR, intr);
207
208
209 /* clear any current interrupt */
210 pcic_read(h, PCIC_CSC);
211
212 cd = pcic_read(h, PCIC_CARD_DETECT);
213 cd |= PCIC_CARD_DETECT_SW_INTR;
214
215 mask = 0;
216 for (i = 0; i < 16; i++) {
217 /* honor configured limitations */
218 if ((sc->intr_mask[h->chip] & (1 << i)) == 0)
219 continue;
220
221 DPRINTF(("probing irq %d: ", i));
222
223 /* ask for a pulse interrupt so we don't share */
224 if (isa_intr_alloc(ic, (1 << i), IST_PULSE, &irq)) {
225 DPRINTF(("currently allocated\n"));
226 continue;
227 }
228
229 cscintr = PCIC_CSC_INTR_CD_ENABLE;
230 cscintr |= (irq << PCIC_CSC_INTR_IRQ_SHIFT);
231 pcic_write(h, PCIC_CSC_INTR, cscintr);
232 delay(10);
233
234 /* Clear any pending interrupt. */
235 (void) pcic_read(h, PCIC_CSC);
236
237 if ((sc->ih = isa_intr_establish(ic, irq, IST_EDGE, IPL_TTY,
238 pcic_isa_count_intr, h)) == NULL)
239 panic("cant get interrupt");
240
241 /* interrupt 40 times */
242 sc->intr_detect = 0;
243 for (j = 0; j < 40 && sc->ih; j++) {
244 sc->intr_false = 0;
245 pcic_write(h, PCIC_CARD_DETECT, cd);
246 delay(100);
247 csc = pcic_read(h, PCIC_CSC);
248 DPRINTF(("%s", csc ? "-" : ""));
249 }
250 DPRINTF((" total %d\n", sc->intr_detect));
251 /* allow for misses */
252 if (sc->intr_detect > 37 && sc->intr_detect <= 40) {
253 printf("%d", i);
254 DPRINTF((" succeded\n"));
255 mask |= (1 << i);
256 }
257
258 if (sc->ih) {
259 isa_intr_disestablish(ic, sc->ih);
260 sc->ih = 0;
261
262 pcic_write(h, PCIC_CSC_INTR, 0);
263 delay(10);
264 }
265 }
266 sc->intr_mask[h->chip] = mask;
267
268 printf("%s\n", sc->intr_mask[h->chip] ? "" : " none");
269 }
270
271 /*
272 * called with interrupts enabled, light up the irqs to find out
273 * which irq lines are actually hooked up to our pcic
274 */
275 void
276 pcic_isa_config_interrupts(self)
277 struct device *self;
278 {
279 struct pcic_softc *sc;
280 struct pcic_isa_softc *isc;
281 struct pcic_handle *h;
282 isa_chipset_tag_t ic;
283 int s, i, chipmask, chipuniq;
284
285 sc = (struct pcic_softc *) self;
286 isc = (struct pcic_isa_softc *) self;
287 ic = isc->sc_ic;
288
289 /* probe each controller */
290 chipmask = 0xffff;
291 for (i = 0; i < PCIC_NSLOTS; i += 2) {
292 if (sc->handle[i].flags & PCIC_FLAG_SOCKETP)
293 h = &sc->handle[i];
294 else if (sc->handle[i + 1].flags & PCIC_FLAG_SOCKETP)
295 h = &sc->handle[i + 1];
296 else
297 continue;
298
299 sc->intr_mask[h->chip] =
300 PCIC_INTR_IRQ_VALIDMASK & pcic_isa_intr_alloc_mask;
301
302 /* the cirrus chips lack support for the soft interrupt */
303 if (pcic_irq_probe != 0 &&
304 h->vendor != PCIC_VENDOR_CIRRUS_PD6710 &&
305 h->vendor != PCIC_VENDOR_CIRRUS_PD672X)
306 pcic_isa_probe_interrupts(sc, h);
307
308 chipmask &= sc->intr_mask[h->chip];
309 }
310 /* now see if there is at least one irq per chip not shared by all */
311 chipuniq = 1;
312 for (i = 0; i < PCIC_NSLOTS; i += 2) {
313 if ((sc->handle[i].flags & PCIC_FLAG_SOCKETP) == 0 &&
314 (sc->handle[i + 1].flags & PCIC_FLAG_SOCKETP) == 0)
315 continue;
316 if ((sc->intr_mask[i / 2] & ~chipmask) == 0) {
317 chipuniq = 0;
318 break;
319 }
320 }
321 /*
322 * the rest of the following code used to run at config time with
323 * no interrupts and gets unhappy if this is violated so...
324 */
325 s = splhigh();
326
327 /*
328 * allocate our irq. it will be used by both controllers. I could
329 * use two different interrupts, but interrupts are relatively
330 * scarce, shareable, and for PCIC controllers, very infrequent.
331 */
332 if ((self->dv_cfdata->cf_flags & 1) == 0) {
333 if (sc->irq != ISACF_IRQ_DEFAULT) {
334 if ((chipmask & (1 << sc->irq)) == 0)
335 printf("%s: warning: configured irq %d not "
336 "detected as available\n",
337 sc->dev.dv_xname, sc->irq);
338 } else if (chipmask == 0 ||
339 isa_intr_alloc(ic, chipmask, IST_EDGE, &sc->irq)) {
340 printf("%s: no available irq; ", sc->dev.dv_xname);
341 sc->irq = ISACF_IRQ_DEFAULT;
342 } else if ((chipmask & ~(1 << sc->irq)) == 0 && chipuniq == 0) {
343 printf("%s: can't share irq with cards; ",
344 sc->dev.dv_xname);
345 sc->irq = ISACF_IRQ_DEFAULT;
346 }
347 } else {
348 printf("%s: ", sc->dev.dv_xname);
349 sc->irq = ISACF_IRQ_DEFAULT;
350 }
351
352 if (sc->irq != ISACF_IRQ_DEFAULT) {
353 sc->ih = isa_intr_establish(ic, sc->irq, IST_EDGE, IPL_TTY,
354 pcic_intr, sc);
355 if (sc->ih == NULL) {
356 printf("%s: can't establish interrupt",
357 sc->dev.dv_xname);
358 sc->irq = ISACF_IRQ_DEFAULT;
359 }
360 }
361 if (sc->irq == ISACF_IRQ_DEFAULT)
362 printf("polling for socket events\n");
363 else
364 printf("%s: using irq %d for socket events\n", sc->dev.dv_xname,
365 sc->irq);
366
367 pcic_attach_sockets_finish(sc);
368
369 splx(s);
370 }
371
372 /*
373 * XXX This routine does not deal with the aliasing issue that its
374 * trying to.
375 *
376 * Any isa device may be decoding only 10 bits of address including
377 * the pcic. This routine only detects if the pcic is doing 10 bits.
378 *
379 * What should be done is detect the pcic's idea of the bus width,
380 * and then within those limits allocate a sparse map, where the
381 * each sub region is offset by 0x400.
382 */
383 void pcic_isa_bus_width_probe (sc, iot, ioh, base, length)
384 struct pcic_softc *sc;
385 bus_space_tag_t iot;
386 bus_space_handle_t ioh;
387 bus_addr_t base;
388 u_int32_t length;
389 {
390 bus_space_handle_t ioh_high;
391 int i, iobuswidth, tmp1, tmp2;
392
393 /*
394 * figure out how wide the isa bus is. Do this by checking if the
395 * pcic controller is mirrored 0x400 above where we expect it to be.
396 */
397
398 iobuswidth = 12;
399
400 /* Map i/o space. */
401 if (bus_space_map(iot, base + 0x400, length, 0, &ioh_high)) {
402 printf("%s: can't map high i/o space\n", sc->dev.dv_xname);
403 return;
404 }
405
406 for (i = 0; i < PCIC_NSLOTS; i++) {
407 if (sc->handle[i].flags & PCIC_FLAG_SOCKETP) {
408 /*
409 * read the ident flags from the normal space and
410 * from the mirror, and compare them
411 */
412
413 bus_space_write_1(iot, ioh, PCIC_REG_INDEX,
414 sc->handle[i].sock + PCIC_IDENT);
415 tmp1 = bus_space_read_1(iot, ioh, PCIC_REG_DATA);
416
417 bus_space_write_1(iot, ioh_high, PCIC_REG_INDEX,
418 sc->handle[i].sock + PCIC_IDENT);
419 tmp2 = bus_space_read_1(iot, ioh_high, PCIC_REG_DATA);
420
421 if (tmp1 == tmp2)
422 iobuswidth = 10;
423 }
424 }
425
426 bus_space_free(iot, ioh_high, length);
427
428 /*
429 * XXX some hardware doesn't seem to grok addresses in 0x400 range--
430 * apparently missing a bit or more of address lines. (e.g.
431 * CIRRUS_PD672X with Linksys EthernetCard ne2000 clone in TI
432 * TravelMate 5000--not clear which is at fault)
433 *
434 * Add a kludge to detect 10 bit wide buses and deal with them,
435 * and also a config file option to override the probe.
436 */
437
438 if (iobuswidth == 10) {
439 sc->iobase = 0x300;
440 sc->iosize = 0x0ff;
441 } else {
442 sc->iobase = 0x400;
443 sc->iosize = 0xbff;
444 }
445
446 DPRINTF(("%s: bus_space_alloc range 0x%04lx-0x%04lx (probed)\n",
447 sc->dev.dv_xname, (long) sc->iobase,
448
449 (long) sc->iobase + sc->iosize));
450
451 if (pcic_isa_alloc_iobase && pcic_isa_alloc_iosize) {
452 sc->iobase = pcic_isa_alloc_iobase;
453 sc->iosize = pcic_isa_alloc_iosize;
454
455 DPRINTF(("%s: bus_space_alloc range 0x%04lx-0x%04lx "
456 "(config override)\n", sc->dev.dv_xname, (long) sc->iobase,
457 (long) sc->iobase + sc->iosize));
458 }
459 }
460
461 void *
462 pcic_isa_chip_intr_establish(pch, pf, ipl, fct, arg)
463 pcmcia_chipset_handle_t pch;
464 struct pcmcia_function *pf;
465 int ipl;
466 int (*fct) __P((void *));
467 void *arg;
468 {
469 struct pcic_handle *h = (struct pcic_handle *) pch;
470 struct pcic_softc *sc = (struct pcic_softc *)(h->ph_parent);
471 struct pcic_isa_softc *isc = (struct pcic_isa_softc *)(h->ph_parent);
472 isa_chipset_tag_t ic = isc->sc_ic;
473 int irq, ist;
474 void *ih;
475 int reg;
476
477 /*
478 * PLEASE NOTE:
479 * The IRQLEVEL bit has no bearing on what happens on the host side of
480 * the PCMCIA controller. ISA interrupts are defined to be edge-
481 * triggered, and as this attachment is for ISA devices, the interrupt
482 * *must* be configured for edge-trigger. If you think you should
483 * change this to use IST_LEVEL, you are *wrong*. You should figure
484 * out what your real problem is and leave this code alone rather than
485 * breaking everyone else's systems. - mycroft
486 */
487 if (pf->cfe->flags & PCMCIA_CFE_IRQLEVEL)
488 ist = IST_EDGE; /* SEE COMMENT ABOVE */
489 else if (pf->cfe->flags & PCMCIA_CFE_IRQPULSE)
490 ist = IST_PULSE; /* SEE COMMENT ABOVE */
491 else
492 ist = IST_EDGE; /* SEE COMMENT ABOVE */
493
494 if (isa_intr_alloc(ic, sc->intr_mask[h->chip], ist, &irq))
495 return (NULL);
496
497 h->ih_irq = irq;
498 if (h->flags & PCIC_FLAG_ENABLED) {
499 reg = pcic_read(h, PCIC_INTR);
500 reg &= ~(PCIC_INTR_IRQ_MASK | PCIC_INTR_ENABLE);
501 pcic_write(h, PCIC_INTR, reg | irq);
502 }
503
504 if ((ih = isa_intr_establish(ic, irq, ist, ipl, fct, arg)) == NULL)
505 return (NULL);
506
507 printf("%s: card irq %d\n", h->pcmcia->dv_xname, irq);
508
509 return (ih);
510 }
511
512 void
513 pcic_isa_chip_intr_disestablish(pch, ih)
514 pcmcia_chipset_handle_t pch;
515 void *ih;
516 {
517 struct pcic_handle *h = (struct pcic_handle *) pch;
518 struct pcic_isa_softc *isc = (struct pcic_isa_softc *)(h->ph_parent);
519 isa_chipset_tag_t ic = isc->sc_ic;
520 int reg;
521
522 isa_intr_disestablish(ic, ih);
523
524 h->ih_irq = 0;
525 if (h->flags & PCIC_FLAG_ENABLED) {
526 reg = pcic_read(h, PCIC_INTR);
527 reg &= ~(PCIC_INTR_IRQ_MASK | PCIC_INTR_ENABLE);
528 pcic_write(h, PCIC_INTR, reg);
529 }
530 }
531