i82365_isa.c revision 1.8 1 /* $NetBSD: i82365_isa.c,v 1.8 1997/11/30 15:31:24 drochner Exp $ */
2
3 #define PCICISADEBUG
4
5 /*
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/types.h>
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/device.h>
38 #include <sys/extent.h>
39 #include <sys/malloc.h>
40
41 #include <vm/vm.h>
42
43 #include <machine/bus.h>
44 #include <machine/intr.h>
45
46 #include <dev/isa/isareg.h>
47 #include <dev/isa/isavar.h>
48
49 #include <dev/pcmcia/pcmciareg.h>
50 #include <dev/pcmcia/pcmciavar.h>
51 #include <dev/pcmcia/pcmciachip.h>
52
53 #include <dev/ic/i82365reg.h>
54 #include <dev/ic/i82365var.h>
55
56 /*****************************************************************************
57 * Configurable parameters.
58 *****************************************************************************/
59
60 #include "opt_pcic_isa_alloc_iobase.h"
61 #include "opt_pcic_isa_alloc_iosize.h"
62 #include "opt_pcic_isa_intr_alloc_mask.h"
63
64 /*
65 * Default I/O allocation range. If both are set to non-zero, these
66 * values will be used instead. Otherwise, the code attempts to probe
67 * the bus width. Systems with 10 address bits should use 0x300 and 0xff.
68 * Systems with 12 address bits (most) should use 0x400 and 0xbff.
69 */
70
71 #ifndef PCIC_ISA_ALLOC_IOBASE
72 #define PCIC_ISA_ALLOC_IOBASE 0
73 #endif
74
75 #ifndef PCIC_ISA_ALLOC_IOSIZE
76 #define PCIC_ISA_ALLOC_IOSIZE 0
77 #endif
78
79 int pcic_isa_alloc_iobase = PCIC_ISA_ALLOC_IOBASE;
80 int pcic_isa_alloc_iosize = PCIC_ISA_ALLOC_IOSIZE;
81
82 /*
83 * Default IRQ allocation bitmask. This defines the range of allowable
84 * IRQs for PCMCIA slots. Useful if order of probing would screw up other
85 * devices, or if PCIC hardware/cards have trouble with certain interrupt
86 * lines.
87 *
88 * We disable IRQ 10 by default, since some common laptops (namely, the
89 * NEC Versa series) reserve IRQ 10 for the docking station SCSI interface.
90 */
91
92 #ifndef PCIC_ISA_INTR_ALLOC_MASK
93 #define PCIC_ISA_INTR_ALLOC_MASK 0xfbff
94 #endif
95
96 int pcic_isa_intr_alloc_mask = PCIC_ISA_INTR_ALLOC_MASK;
97
98 /*****************************************************************************
99 * End of configurable parameters.
100 *****************************************************************************/
101
102 #ifdef PCICISADEBUG
103 int pcicisa_debug = 0 /* XXX */ ;
104 #define DPRINTF(arg) if (pcicisa_debug) printf arg;
105 #else
106 #define DPRINTF(arg)
107 #endif
108
109 #ifdef __BROKEN_INDIRECT_CONFIG
110 int pcic_isa_probe __P((struct device *, void *, void *));
111 #else
112 int pcic_isa_probe __P((struct device *, struct cfdata *, void *));
113 #endif
114 void pcic_isa_attach __P((struct device *, struct device *, void *));
115
116 void *pcic_isa_chip_intr_establish __P((pcmcia_chipset_handle_t,
117 struct pcmcia_function *, int, int (*) (void *), void *));
118 void pcic_isa_chip_intr_disestablish __P((pcmcia_chipset_handle_t, void *));
119
120 struct cfattach pcic_isa_ca = {
121 sizeof(struct pcic_softc), pcic_isa_probe, pcic_isa_attach
122 };
123
124 static struct pcmcia_chip_functions pcic_isa_functions = {
125 pcic_chip_mem_alloc,
126 pcic_chip_mem_free,
127 pcic_chip_mem_map,
128 pcic_chip_mem_unmap,
129
130 pcic_chip_io_alloc,
131 pcic_chip_io_free,
132 pcic_chip_io_map,
133 pcic_chip_io_unmap,
134
135 pcic_isa_chip_intr_establish,
136 pcic_isa_chip_intr_disestablish,
137
138 pcic_chip_socket_enable,
139 pcic_chip_socket_disable,
140 };
141
142 int
143 pcic_isa_probe(parent, match, aux)
144 struct device *parent;
145 #ifdef __BROKEN_INDIRECT_CONFIG
146 void *match;
147 #else
148 struct cfdata *match;
149 #endif
150 void *aux;
151 {
152 struct isa_attach_args *ia = aux;
153 bus_space_tag_t iot = ia->ia_iot;
154 bus_space_handle_t ioh, memh;
155 int val, found;
156
157 /* Disallow wildcarded i/o address. */
158 if (ia->ia_iobase == ISACF_PORT_DEFAULT)
159 return (0);
160
161 if (bus_space_map(iot, ia->ia_iobase, PCIC_IOSIZE, 0, &ioh))
162 return (0);
163
164 if (ia->ia_msize == -1)
165 ia->ia_msize = PCIC_MEMSIZE;
166
167 if (bus_space_map(ia->ia_memt, ia->ia_maddr, ia->ia_msize, 0, &memh))
168 return (0);
169
170 found = 0;
171
172 /*
173 * this could be done with a loop, but it would violate the
174 * abstraction
175 */
176
177 bus_space_write_1(iot, ioh, PCIC_REG_INDEX, C0SA + PCIC_IDENT);
178
179 val = bus_space_read_1(iot, ioh, PCIC_REG_DATA);
180
181 if (pcic_ident_ok(val))
182 found++;
183
184
185 bus_space_write_1(iot, ioh, PCIC_REG_INDEX, C0SB + PCIC_IDENT);
186
187 val = bus_space_read_1(iot, ioh, PCIC_REG_DATA);
188
189 if (pcic_ident_ok(val))
190 found++;
191
192
193 bus_space_write_1(iot, ioh, PCIC_REG_INDEX, C1SA + PCIC_IDENT);
194
195 val = bus_space_read_1(iot, ioh, PCIC_REG_DATA);
196
197 if (pcic_ident_ok(val))
198 found++;
199
200
201 bus_space_write_1(iot, ioh, PCIC_REG_INDEX, C1SB + PCIC_IDENT);
202
203 val = bus_space_read_1(iot, ioh, PCIC_REG_DATA);
204
205 if (pcic_ident_ok(val))
206 found++;
207
208
209 bus_space_unmap(iot, ioh, PCIC_IOSIZE);
210 bus_space_unmap(ia->ia_memt, memh, ia->ia_msize);
211
212 if (!found)
213 return (0);
214
215 ia->ia_iosize = PCIC_IOSIZE;
216
217 return (1);
218 }
219
220 void
221 pcic_isa_attach(parent, self, aux)
222 struct device *parent, *self;
223 void *aux;
224 {
225 struct pcic_softc *sc = (void *) self;
226 struct isa_attach_args *ia = aux;
227 isa_chipset_tag_t ic = ia->ia_ic;
228 bus_space_tag_t iot = ia->ia_iot;
229 bus_space_tag_t memt = ia->ia_memt;
230 bus_space_handle_t ioh;
231 bus_space_handle_t memh;
232 bus_space_handle_t ioh_high;
233 int i, iobuswidth, tmp1, tmp2;
234
235 /* Map i/o space. */
236 if (bus_space_map(iot, ia->ia_iobase, ia->ia_iosize, 0, &ioh)) {
237 printf(": can't map i/o space\n");
238 return;
239 }
240
241 /* Map mem space. */
242 if (bus_space_map(memt, ia->ia_maddr, ia->ia_msize, 0, &memh)) {
243 printf(": can't map mem space\n");
244 return;
245 }
246
247 sc->membase = ia->ia_maddr;
248 sc->subregionmask = (1 << (ia->ia_msize / PCIC_MEM_PAGESIZE)) - 1;
249
250 sc->intr_est = ic;
251 sc->pct = (pcmcia_chipset_tag_t) & pcic_isa_functions;
252
253 sc->iot = iot;
254 sc->ioh = ioh;
255 sc->memt = memt;
256 sc->memh = memh;
257
258 /*
259 * allocate an irq. it will be used by both controllers. I could
260 * use two different interrupts, but interrupts are relatively
261 * scarce, shareable, and for PCIC controllers, very infrequent.
262 */
263
264 if ((sc->irq = ia->ia_irq) == IRQUNK) {
265 if (isa_intr_alloc(ic,
266 PCIC_CSC_INTR_IRQ_VALIDMASK & pcic_isa_intr_alloc_mask,
267 IST_EDGE, &sc->irq)) {
268 printf("\n%s: can't allocate interrupt\n",
269 sc->dev.dv_xname);
270 return;
271 }
272 printf(": using irq %d", sc->irq);
273 }
274 printf("\n");
275
276 pcic_attach(sc);
277
278 /*
279 * figure out how wide the isa bus is. Do this by checking if the
280 * pcic controller is mirrored 0x400 above where we expect it to be.
281 */
282
283 iobuswidth = 12;
284
285 /* Map i/o space. */
286 if (bus_space_map(iot, ia->ia_iobase + 0x400, ia->ia_iosize, 0,
287 &ioh_high)) {
288 printf("%s: can't map high i/o space\n", sc->dev.dv_xname);
289 return;
290 }
291
292 for (i = 0; i < PCIC_NSLOTS; i++) {
293 if (sc->handle[i].flags & PCIC_FLAG_SOCKETP) {
294 /*
295 * read the ident flags from the normal space and
296 * from the mirror, and compare them
297 */
298
299 bus_space_write_1(iot, ioh, PCIC_REG_INDEX,
300 sc->handle[i].sock + PCIC_IDENT);
301 tmp1 = bus_space_read_1(iot, ioh, PCIC_REG_DATA);
302
303 bus_space_write_1(iot, ioh_high, PCIC_REG_INDEX,
304 sc->handle[i].sock + PCIC_IDENT);
305 tmp2 = bus_space_read_1(iot, ioh_high, PCIC_REG_DATA);
306
307 if (tmp1 == tmp2)
308 iobuswidth = 10;
309 }
310 }
311
312 bus_space_free(iot, ioh_high, ia->ia_iosize);
313
314 /*
315 * XXX mycroft recommends I/O space range 0x400-0xfff . I should put
316 * this in a header somewhere
317 */
318
319 /*
320 * XXX some hardware doesn't seem to grok addresses in 0x400 range--
321 * apparently missing a bit or more of address lines. (e.g.
322 * CIRRUS_PD672X with Linksys EthernetCard ne2000 clone in TI
323 * TravelMate 5000--not clear which is at fault)
324 *
325 * Add a kludge to detect 10 bit wide buses and deal with them,
326 * and also a config file option to override the probe.
327 */
328
329 if (iobuswidth == 10) {
330 sc->iobase = 0x300;
331 sc->iosize = 0x0ff;
332 } else {
333 #if 0
334 /*
335 * This is what we'd like to use, but...
336 */
337 sc->iobase = 0x400;
338 sc->iosize = 0xbff;
339 #else
340 /*
341 * ...the above bus width probe doesn't always work.
342 * So, experimentation has shown the following range
343 * to not lose on systems that 0x300-0x3ff loses on
344 * (e.g. the NEC Versa 6030X).
345 */
346 sc->iobase = 0x330;
347 sc->iosize = 0x0cf;
348 #endif
349 }
350
351 DPRINTF(("%s: bus_space_alloc range 0x%04lx-0x%04lx (probed)\n",
352 sc->dev.dv_xname, (long) sc->iobase,
353 (long) sc->iobase + sc->iosize));
354
355 if (pcic_isa_alloc_iobase && pcic_isa_alloc_iosize) {
356 sc->iobase = pcic_isa_alloc_iobase;
357 sc->iosize = pcic_isa_alloc_iosize;
358
359 DPRINTF(("%s: bus_space_alloc range 0x%04lx-0x%04lx "
360 "(config override)\n", sc->dev.dv_xname, (long) sc->iobase,
361 (long) sc->iobase + sc->iosize));
362 }
363 sc->ih = isa_intr_establish(ic, sc->irq, IST_EDGE, IPL_TTY,
364 pcic_intr, sc);
365 if (sc->ih == NULL) {
366 printf("%s: can't establish interrupt\n", sc->dev.dv_xname);
367 return;
368 }
369
370 pcic_attach_sockets(sc);
371 }
372
373 void *
374 pcic_isa_chip_intr_establish(pch, pf, ipl, fct, arg)
375 pcmcia_chipset_handle_t pch;
376 struct pcmcia_function *pf;
377 int ipl;
378 int (*fct) __P((void *));
379 void *arg;
380 {
381 struct pcic_handle *h = (struct pcic_handle *) pch;
382 int irq, ist;
383 void *ih;
384 int reg;
385
386 if (pf->cfe->flags & PCMCIA_CFE_IRQLEVEL)
387 ist = IST_LEVEL;
388 else if (pf->cfe->flags & PCMCIA_CFE_IRQPULSE)
389 ist = IST_PULSE;
390 else
391 ist = IST_LEVEL;
392
393 if (isa_intr_alloc(h->sc->intr_est,
394 PCIC_INTR_IRQ_VALIDMASK & pcic_isa_intr_alloc_mask, ist, &irq))
395 return (NULL);
396 if ((ih = isa_intr_establish(h->sc->intr_est, irq, ist, ipl,
397 fct, arg)) == NULL)
398 return (NULL);
399
400 reg = pcic_read(h, PCIC_INTR);
401 reg &= ~PCIC_INTR_IRQ_MASK;
402 reg |= irq;
403 pcic_write(h, PCIC_INTR, reg);
404
405 h->ih_irq = irq;
406
407 printf("%s: card irq %d\n", h->pcmcia->dv_xname, irq);
408
409 return (ih);
410 }
411
412 void
413 pcic_isa_chip_intr_disestablish(pch, ih)
414 pcmcia_chipset_handle_t pch;
415 void *ih;
416 {
417 struct pcic_handle *h = (struct pcic_handle *) pch;
418 int reg;
419
420 h->ih_irq = 0;
421
422 reg = pcic_read(h, PCIC_INTR);
423 reg &= ~(PCIC_INTR_IRQ_MASK | PCIC_INTR_ENABLE);
424 pcic_write(h, PCIC_INTR, reg);
425
426 isa_intr_disestablish(h->sc->intr_est, ih);
427 }
428