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