i82365_isasubr.c revision 1.10 1 /* $NetBSD: i82365_isasubr.c,v 1.10 2000/02/04 08:42:47 chopps 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_NO_IRQ_PROBE
104 #ifdef __hpcmips__
105 #define PCIC_NO_IRQ_PROBE 0
106 #else
107 #define PCIC_NO_IRQ_PROBE 1
108 #endif
109 #endif
110
111 int pcic_no_irq_probe = PCIC_NO_IRQ_PROBE;
112
113 /*****************************************************************************
114 * End of configurable parameters.
115 *****************************************************************************/
116
117 #ifdef PCICISADEBUG
118 int pcicsubr_debug = 0;
119 #define DPRINTF(arg) do { if (pcicsubr_debug) printf arg ; } while (0)
120 #else
121 #define DPRINTF(arg)
122 #endif
123
124 /*
125 * count the interrupt if we have a status set
126 * just use socket 0
127 */
128
129 void pcic_isa_probe_interrupts __P((struct pcic_softc *, struct pcic_handle *));
130 static int pcic_isa_count_intr __P((void *));
131
132 static int
133 pcic_isa_count_intr(arg)
134 void *arg;
135 {
136 struct pcic_softc *sc;
137 struct pcic_handle *h;
138 int cscreg;
139
140 h = arg;
141 sc = (struct pcic_softc *)h->ph_parent;
142
143 cscreg = pcic_read(h, PCIC_CSC);
144 if (cscreg & PCIC_CSC_CD) {
145 if ((++sc->intr_detect % 20) == 0)
146 printf(".");
147 else
148 DPRINTF(("."));
149 return (1);
150 }
151
152 #ifdef PCICISADEBUG
153 if (cscreg)
154 DPRINTF(("o"));
155 else
156 DPRINTF(("X"));
157 #endif
158 return (cscreg ? 1 : 0);
159 }
160
161 /*
162 * use soft interrupt card detect to find out which irqs are available
163 * for this controller
164 */
165 void
166 pcic_isa_probe_interrupts(sc, h)
167 struct pcic_softc *sc;
168 struct pcic_handle *h;
169 {
170 isa_chipset_tag_t ic;
171 int i, j, mask, irq;
172 int cd, cscintr, intr, csc;
173 void *ih;
174
175 ic = sc->intr_est;
176
177 printf("%s: controller %d detecting irqs with mask 0x%04x:",
178 sc->dev.dv_xname, h->chip, sc->intr_mask[h->chip]);
179 DPRINTF(("\n"));
180
181 /* clear any current interrupt */
182 pcic_read(h, PCIC_CSC);
183
184 /* first disable the status irq, then enable card detect */
185 pcic_write(h, PCIC_CSC_INTR, 0);
186 cscintr = PCIC_CSC_INTR_CD_ENABLE;
187 pcic_write(h, PCIC_CSC_INTR, cscintr);
188
189 /* steer the interrupt to isa and disable ring and interrupt */
190 intr = pcic_read(h, PCIC_INTR);
191 intr &= ~(PCIC_INTR_RI_ENABLE | PCIC_INTR_ENABLE | PCIC_INTR_IRQ_MASK);
192 pcic_write(h, PCIC_INTR, intr);
193
194 /* clear any current interrupt */
195 pcic_read(h, PCIC_CSC);
196
197 cd = pcic_read(h, PCIC_CARD_DETECT);
198 cd |= PCIC_CARD_DETECT_SW_INTR;
199 mask = 0;
200 for (i = 0; i < 16; i++) {
201 /* honor configured limitations */
202 if ((sc->intr_mask[h->chip] & (1 << i)) == 0)
203 continue;
204
205 DPRINTF(("probing irq %d: ", i));
206
207 /* ask for a pulse interrupt so we don't share */
208 if (isa_intr_alloc(ic, (1 << i), IST_PULSE, &irq)) {
209 DPRINTF(("currently allocated\n"));
210 continue;
211 }
212
213 if ((ih = isa_intr_establish(ic, irq, IST_LEVEL, IPL_TTY,
214 pcic_isa_count_intr, h)) == NULL)
215 panic("cant get interrupt");
216
217 cscintr &= ~PCIC_CSC_INTR_IRQ_MASK;
218 cscintr |= (irq << PCIC_CSC_INTR_IRQ_SHIFT);
219 pcic_write(h, PCIC_CSC_INTR, cscintr);
220
221 /* interrupt 40 times */
222 sc->intr_detect = 0;
223 for (j = 0; j < 40; j++) {
224 pcic_write(h, PCIC_CARD_DETECT, cd);
225 delay(100);
226 csc = pcic_read(h, PCIC_CSC);
227 DPRINTF(("%s", csc ? "-" : ""));
228 }
229 DPRINTF((" total %d\n", sc->intr_detect));
230 /* allow for misses */
231 if (sc->intr_detect > 37 && sc->intr_detect <= 40) {
232 printf("%d", i);
233 DPRINTF((" succeded\n"));
234 mask |= (1 << i);
235 }
236 isa_intr_disestablish(ic, ih);
237 }
238 sc->intr_mask[h->chip] = mask;
239 printf("%s\n", sc->intr_mask ? "" : " none");
240
241 /* disable all status interrupts */
242 pcic_write(h, PCIC_CSC_INTR, 0);
243
244 /* clear any current interrupt */
245 pcic_read(h, PCIC_CSC);
246 }
247
248 /*
249 * called with interrupts enabled, light up the irqs to find out
250 * which irq lines are actually hooked up to our pcic
251 */
252 void
253 pcic_isa_config_interrupts(self)
254 struct device *self;
255 {
256 struct pcic_softc *sc;
257 struct pcic_handle *h;
258 isa_chipset_tag_t ic;
259 int s, i, chipmask, chipuniq;
260
261 sc = (struct pcic_softc *)self;
262 ic = sc->intr_est;
263
264 /* probe each controller */
265 chipmask = 0xffff;
266 for (i = 0; i < PCIC_NSLOTS; i += 2) {
267 if (sc->handle[i].flags & PCIC_FLAG_SOCKETP)
268 h = &sc->handle[i];
269 else if (sc->handle[i + 1].flags & PCIC_FLAG_SOCKETP)
270 h = &sc->handle[i + 1];
271 else
272 continue;
273
274 sc->intr_mask[h->chip] =
275 PCIC_INTR_IRQ_VALIDMASK & pcic_isa_intr_alloc_mask;
276
277 /* the cirrus chips lack support for the soft interrupt */
278 if (pcic_no_irq_probe != 0 &&
279 h->vendor != PCIC_VENDOR_CIRRUS_PD6710 &&
280 h->vendor != PCIC_VENDOR_CIRRUS_PD672X)
281 pcic_isa_probe_interrupts(sc, h);
282
283 chipmask &= sc->intr_mask[h->chip];
284 }
285 /* now see if there is at least one irq per chip not shared by all */
286 chipuniq = 1;
287 for (i = 0; i < PCIC_NSLOTS; i += 2) {
288 if ((sc->handle[i].flags & PCIC_FLAG_SOCKETP) == 0 &&
289 (sc->handle[i + 1].flags & PCIC_FLAG_SOCKETP) == 0)
290 continue;
291 if ((sc->intr_mask[i / 2] & ~chipmask) == 0) {
292 chipuniq = 0;
293 break;
294 }
295 }
296 /*
297 * the rest of the following code used to run at config time with
298 * no interrupts and gets unhappy if this is violated so...
299 */
300 s = splhigh();
301
302 /*
303 * allocate our irq. it will be used by both controllers. I could
304 * use two different interrupts, but interrupts are relatively
305 * scarce, shareable, and for PCIC controllers, very infrequent.
306 */
307 if (sc->irq != IRQUNK) {
308 if ((chipmask & (1 << sc->irq)) == 0)
309 printf("%s: warning: configured irq %d not detected as"
310 " available\n", sc->dev.dv_xname, sc->irq);
311 } else if (chipmask == 0 ||
312 isa_intr_alloc(ic, chipmask, IST_EDGE, &sc->irq)) {
313 printf("%s: no available irq", sc->dev.dv_xname);
314 sc->irq = IRQUNK;
315 } else if ((chipmask & ~(1 << sc->irq)) == 0 && chipuniq == 0) {
316 printf("%s: can\'t share irq with cards", sc->dev.dv_xname);
317 sc->irq = IRQUNK;
318 }
319 if (sc->irq != IRQUNK) {
320 printf("%s: using irq %d\n", sc->dev.dv_xname, sc->irq);
321 sc->ih = isa_intr_establish(ic, sc->irq, IST_EDGE, IPL_TTY,
322 pcic_intr, sc);
323 if (sc->ih == NULL) {
324 printf("%s: can't establish interrupt",
325 sc->dev.dv_xname);
326 sc->irq = IRQUNK;
327 }
328 }
329 if (sc->irq == IRQUNK)
330 printf(", will poll for card insertion and removal\n");
331
332 pcic_attach_sockets_finish(sc);
333
334 splx(s);
335 }
336
337 /*
338 * XXX This routine does not deal with the aliasing issue that its
339 * trying to.
340 *
341 * Any isa device may be decoding only 10 bits of address including
342 * the pcic. This routine only detects if the pcic is doing 10 bits.
343 *
344 * What should be done is detect the pcic's idea of the bus width,
345 * and then within those limits allocate a sparse map, where the
346 * each sub region is offset by 0x400.
347 */
348 void pcic_isa_bus_width_probe (sc, iot, ioh, base, length)
349 struct pcic_softc *sc;
350 bus_space_tag_t iot;
351 bus_space_handle_t ioh;
352 bus_addr_t base;
353 u_int32_t length;
354 {
355 bus_space_handle_t ioh_high;
356 int i, iobuswidth, tmp1, tmp2;
357
358 /*
359 * figure out how wide the isa bus is. Do this by checking if the
360 * pcic controller is mirrored 0x400 above where we expect it to be.
361 */
362
363 iobuswidth = 12;
364
365 /* Map i/o space. */
366 if (bus_space_map(iot, base + 0x400, length, 0, &ioh_high)) {
367 printf("%s: can't map high i/o space\n", sc->dev.dv_xname);
368 return;
369 }
370
371 for (i = 0; i < PCIC_NSLOTS; i++) {
372 if (sc->handle[i].flags & PCIC_FLAG_SOCKETP) {
373 /*
374 * read the ident flags from the normal space and
375 * from the mirror, and compare them
376 */
377
378 bus_space_write_1(iot, ioh, PCIC_REG_INDEX,
379 sc->handle[i].sock + PCIC_IDENT);
380 tmp1 = bus_space_read_1(iot, ioh, PCIC_REG_DATA);
381
382 bus_space_write_1(iot, ioh_high, PCIC_REG_INDEX,
383 sc->handle[i].sock + PCIC_IDENT);
384 tmp2 = bus_space_read_1(iot, ioh_high, PCIC_REG_DATA);
385
386 if (tmp1 == tmp2)
387 iobuswidth = 10;
388 }
389 }
390
391 bus_space_free(iot, ioh_high, length);
392
393 /*
394 * XXX mycroft recommends I/O space range 0x400-0xfff . I should put
395 * this in a header somewhere
396 */
397
398 /*
399 * XXX some hardware doesn't seem to grok addresses in 0x400 range--
400 * apparently missing a bit or more of address lines. (e.g.
401 * CIRRUS_PD672X with Linksys EthernetCard ne2000 clone in TI
402 * TravelMate 5000--not clear which is at fault)
403 *
404 * Add a kludge to detect 10 bit wide buses and deal with them,
405 * and also a config file option to override the probe.
406 */
407
408 if (iobuswidth == 10) {
409 sc->iobase = 0x300;
410 sc->iosize = 0x0ff;
411 } else {
412 #if 0
413 /*
414 * This is what we'd like to use, but...
415 */
416 sc->iobase = 0x400;
417 sc->iosize = 0xbff;
418 #else
419 /*
420 * ...the above bus width probe doesn't always work.
421 * So, experimentation has shown the following range
422 * to not lose on systems that 0x300-0x3ff loses on
423 * (e.g. the NEC Versa 6030X).
424 */
425 sc->iobase = 0x330;
426 sc->iosize = 0x0cf;
427 #endif
428 }
429
430 DPRINTF(("%s: bus_space_alloc range 0x%04lx-0x%04lx (probed)\n",
431 sc->dev.dv_xname, (long) sc->iobase,
432
433 (long) sc->iobase + sc->iosize));
434
435 if (pcic_isa_alloc_iobase && pcic_isa_alloc_iosize) {
436 sc->iobase = pcic_isa_alloc_iobase;
437 sc->iosize = pcic_isa_alloc_iosize;
438
439 DPRINTF(("%s: bus_space_alloc range 0x%04lx-0x%04lx "
440 "(config override)\n", sc->dev.dv_xname, (long) sc->iobase,
441 (long) sc->iobase + sc->iosize));
442 }
443 }
444
445 void *
446 pcic_isa_chip_intr_establish(pch, pf, ipl, fct, arg)
447 pcmcia_chipset_handle_t pch;
448 struct pcmcia_function *pf;
449 int ipl;
450 int (*fct) __P((void *));
451 void *arg;
452 {
453 struct pcic_handle *h = (struct pcic_handle *) pch;
454 struct pcic_softc *sc = (struct pcic_softc *)(h->ph_parent);
455 isa_chipset_tag_t ic = sc->intr_est;
456 int irq, ist;
457 void *ih;
458 int reg;
459
460 if (pf->cfe->flags & PCMCIA_CFE_IRQLEVEL)
461 ist = IST_LEVEL;
462 else if (pf->cfe->flags & PCMCIA_CFE_IRQPULSE)
463 ist = IST_PULSE;
464 else
465 ist = IST_EDGE;
466
467 if (isa_intr_alloc(ic, sc->intr_mask[h->chip], ist, &irq))
468 return (NULL);
469 if ((ih = isa_intr_establish(ic, irq, ist, ipl, fct, arg)) == NULL)
470 return (NULL);
471
472 h->ih_irq = irq;
473 if (h->flags & PCIC_FLAG_ENABLED) {
474 reg = pcic_read(h, PCIC_INTR);
475 reg &= ~(PCIC_INTR_IRQ_MASK | PCIC_INTR_ENABLE);
476 reg |= irq;
477 pcic_write(h, PCIC_INTR, reg);
478 }
479
480 printf("%s: card irq %d\n", h->pcmcia->dv_xname, irq);
481
482 return (ih);
483 }
484
485 void
486 pcic_isa_chip_intr_disestablish(pch, ih)
487 pcmcia_chipset_handle_t pch;
488 void *ih;
489 {
490 struct pcic_handle *h = (struct pcic_handle *) pch;
491 struct pcic_softc *sc = (struct pcic_softc *)(h->ph_parent);
492 isa_chipset_tag_t ic = sc->intr_est;
493 int reg;
494
495 h->ih_irq = 0;
496 if (h->flags & PCIC_FLAG_ENABLED) {
497 reg = pcic_read(h, PCIC_INTR);
498 reg &= ~(PCIC_INTR_IRQ_MASK | PCIC_INTR_ENABLE);
499 pcic_write(h, PCIC_INTR, reg);
500 }
501 isa_intr_disestablish(ic, ih);
502 }
503