i82365_isa.c revision 1.1.2.4 1 /* $NetBSD: i82365_isa.c,v 1.1.2.4 1997/10/16 22:46:23 thorpej 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 #ifdef PCICISADEBUG
57 int pcicisa_debug = 0 /* XXX */ ;
58 #define DPRINTF(arg) if (pcicisa_debug) printf arg;
59 #else
60 #define DPRINTF(arg)
61 #endif
62
63 int pcic_isa_probe __P((struct device *, void *, void *));
64 void pcic_isa_attach __P((struct device *, struct device *, void *));
65
66 void *pcic_isa_chip_intr_establish __P((pcmcia_chipset_handle_t,
67 struct pcmcia_function *, int, int (*) (void *), void *));
68 void pcic_isa_chip_intr_disestablish __P((pcmcia_chipset_handle_t, void *));
69
70 struct cfattach pcic_isa_ca = {
71 sizeof(struct pcic_softc), pcic_isa_probe, pcic_isa_attach
72 };
73
74 static struct pcmcia_chip_functions pcic_isa_functions = {
75 pcic_chip_mem_alloc,
76 pcic_chip_mem_free,
77 pcic_chip_mem_map,
78 pcic_chip_mem_unmap,
79
80 pcic_chip_io_alloc,
81 pcic_chip_io_free,
82 pcic_chip_io_map,
83 pcic_chip_io_unmap,
84
85 pcic_isa_chip_intr_establish,
86 pcic_isa_chip_intr_disestablish,
87
88 pcic_chip_socket_enable,
89 pcic_chip_socket_disable,
90 };
91
92 int
93 pcic_isa_probe(parent, match, aux)
94 struct device *parent;
95 void *match, *aux;
96 {
97 struct isa_attach_args *ia = aux;
98 bus_space_tag_t iot = ia->ia_iot;
99 bus_space_handle_t ioh, memh;
100 int val, found;
101
102 if (bus_space_map(iot, ia->ia_iobase, PCIC_IOSIZE, 0, &ioh))
103 return (0);
104
105 if (ia->ia_msize == -1)
106 ia->ia_msize = PCIC_MEMSIZE;
107
108 if (bus_space_map(ia->ia_memt, ia->ia_maddr, ia->ia_msize, 0, &memh))
109 return (0);
110
111 found = 0;
112
113 /*
114 * this could be done with a loop, but it would violate the
115 * abstraction
116 */
117
118 bus_space_write_1(iot, ioh, PCIC_REG_INDEX, C0SA + PCIC_IDENT);
119
120 val = bus_space_read_1(iot, ioh, PCIC_REG_DATA);
121
122 if (pcic_ident_ok(val))
123 found++;
124
125
126 bus_space_write_1(iot, ioh, PCIC_REG_INDEX, C0SB + PCIC_IDENT);
127
128 val = bus_space_read_1(iot, ioh, PCIC_REG_DATA);
129
130 if (pcic_ident_ok(val))
131 found++;
132
133
134 bus_space_write_1(iot, ioh, PCIC_REG_INDEX, C1SA + PCIC_IDENT);
135
136 val = bus_space_read_1(iot, ioh, PCIC_REG_DATA);
137
138 if (pcic_ident_ok(val))
139 found++;
140
141
142 bus_space_write_1(iot, ioh, PCIC_REG_INDEX, C1SB + PCIC_IDENT);
143
144 val = bus_space_read_1(iot, ioh, PCIC_REG_DATA);
145
146 if (pcic_ident_ok(val))
147 found++;
148
149
150 bus_space_unmap(iot, ioh, PCIC_IOSIZE);
151 bus_space_unmap(ia->ia_memt, memh, ia->ia_msize);
152
153 if (!found)
154 return (0);
155
156 ia->ia_iosize = PCIC_IOSIZE;
157
158 return (1);
159 }
160
161 #ifndef PCIC_ISA_ALLOC_IOBASE
162 #define PCIC_ISA_ALLOC_IOBASE 0
163 #endif
164
165 #ifndef PCIC_ISA_ALLOC_IOSIZE
166 #define PCIC_ISA_ALLOC_IOSIZE 0
167 #endif
168
169 int pcic_isa_alloc_iobase = PCIC_ISA_ALLOC_IOBASE;
170 int pcic_isa_alloc_iosize = PCIC_ISA_ALLOC_IOSIZE;
171
172 void
173 pcic_isa_attach(parent, self, aux)
174 struct device *parent, *self;
175 void *aux;
176 {
177 struct pcic_softc *sc = (void *) self;
178 struct isa_attach_args *ia = aux;
179 isa_chipset_tag_t ic = ia->ia_ic;
180 bus_space_tag_t iot = ia->ia_iot;
181 bus_space_tag_t memt = ia->ia_memt;
182 bus_space_handle_t ioh;
183 bus_space_handle_t memh;
184 bus_space_handle_t ioh_high;
185 int i, iobuswidth, tmp1, tmp2;
186
187 /* Map i/o space. */
188 if (bus_space_map(iot, ia->ia_iobase, ia->ia_iosize, 0, &ioh))
189 panic("pcic_isa_attach: can't map i/o space");
190
191 /* Map mem space. */
192 if (bus_space_map(memt, ia->ia_maddr, ia->ia_msize, 0, &memh))
193 panic("pcic_isa_attach: can't map i/o space");
194
195 sc->membase = ia->ia_maddr;
196 sc->subregionmask = (1 << (ia->ia_msize / PCIC_MEM_PAGESIZE)) - 1;
197
198 sc->intr_est = ic;
199 sc->pct = (pcmcia_chipset_tag_t) & pcic_isa_functions;
200
201 sc->iot = iot;
202 sc->ioh = ioh;
203 sc->memt = memt;
204 sc->memh = memh;
205
206 /*
207 * allocate an irq. it will be used by both controllers. I could
208 * use two different interrupts, but interrupts are relatively
209 * scarce, shareable, and for PCIC controllers, very infrequent.
210 */
211
212 if ((sc->irq = ia->ia_irq) == IRQUNK) {
213 /* XXX CHECK RETURN VALUE */
214 (void) isa_intr_alloc(ic, PCIC_CSC_INTR_IRQ_VALIDMASK,
215 IST_EDGE, &sc->irq);
216 printf(": using irq %d", sc->irq);
217 }
218 printf("\n");
219
220 pcic_attach(sc);
221
222 /*
223 * figure out how wide the isa bus is. Do this by checking if the
224 * pcic controller is mirrored 0x400 above where we expect it to be.
225 */
226
227 iobuswidth = 12;
228
229 /* Map i/o space. */
230 if (bus_space_map(iot, ia->ia_iobase + 0x400, ia->ia_iosize, 0,
231 &ioh_high))
232 panic("pcic_isa_attach: can't map high i/o space");
233
234 for (i = 0; i < PCIC_NSLOTS; i++) {
235 if (sc->handle[i].flags & PCIC_FLAG_SOCKETP) {
236 /*
237 * read the ident flags from the normal space and
238 * from the mirror, and compare them
239 */
240
241 bus_space_write_1(iot, ioh, PCIC_REG_INDEX,
242 sc->handle[i].sock + PCIC_IDENT);
243 tmp1 = bus_space_read_1(iot, ioh, PCIC_REG_DATA);
244
245 bus_space_write_1(iot, ioh_high, PCIC_REG_INDEX,
246 sc->handle[i].sock + PCIC_IDENT);
247 tmp2 = bus_space_read_1(iot, ioh_high, PCIC_REG_DATA);
248
249 if (tmp1 == tmp2)
250 iobuswidth = 10;
251 }
252 }
253
254 bus_space_free(iot, ioh_high, ia->ia_iosize);
255
256 /*
257 * XXX mycroft recommends I/O space range 0x400-0xfff . I should put
258 * this in a header somewhere
259 */
260
261 /*
262 * XXX some hardware doesn't seem to grok addresses in 0x400 range--
263 * apparently missing a bit or more of address lines. (e.g.
264 * CIRRUS_PD672X with Linksys EthernetCard ne2000 clone in TI
265 * TravelMate 5000--not clear which is at fault)
266 *
267 * Add a kludge to detect 10 bit wide buses and deal with them,
268 * and also a config file option to override the probe.
269 */
270
271 if (iobuswidth == 10) {
272 sc->iobase = 0x300;
273 sc->iosize = 0x0ff;
274 } else {
275 sc->iobase = 0x400;
276 sc->iosize = 0xbff;
277 }
278
279 DPRINTF(("%s: bus_space_alloc range 0x%04lx-0x%04lx (probed)\n",
280 sc->dev.dv_xname, (long) sc->iobase,
281 (long) sc->iobase + sc->iosize));
282
283 if (pcic_isa_alloc_iobase && pcic_isa_alloc_iosize) {
284 sc->iobase = pcic_isa_alloc_iobase;
285 sc->iosize = pcic_isa_alloc_iosize;
286
287 DPRINTF(("%s: bus_space_alloc range 0x%04lx-0x%04lx "
288 "(config override)\n", sc->dev.dv_xname, (long) sc->iobase,
289 (long) sc->iobase + sc->iosize));
290 }
291 sc->ih = isa_intr_establish(ic, sc->irq, IST_EDGE, IPL_TTY,
292 pcic_intr, sc);
293
294 pcic_attach_sockets(sc);
295 }
296
297 /*
298 * allow patching or kernel option file override of available IRQs. Useful if
299 * order of probing would screw up other devices, or if PCIC hardware/cards
300 * have trouble with certain interrupt lines.
301 */
302
303 #ifndef PCIC_INTR_ALLOC_MASK
304 #define PCIC_INTR_ALLOC_MASK 0xffff
305 #endif
306
307 int pcic_intr_alloc_mask = PCIC_INTR_ALLOC_MASK;
308
309 void *
310 pcic_isa_chip_intr_establish(pch, pf, ipl, fct, arg)
311 pcmcia_chipset_handle_t pch;
312 struct pcmcia_function *pf;
313 int ipl;
314 int (*fct) __P((void *));
315 void *arg;
316 {
317 struct pcic_handle *h = (struct pcic_handle *) pch;
318 int irq, ist;
319 void *ih;
320 int reg;
321
322 if (pf->cfe->flags & PCMCIA_CFE_IRQLEVEL)
323 ist = IST_LEVEL;
324 else if (pf->cfe->flags & PCMCIA_CFE_IRQPULSE)
325 ist = IST_PULSE;
326 else
327 ist = IST_LEVEL;
328
329 if (isa_intr_alloc(h->sc->intr_est,
330 PCIC_INTR_IRQ_VALIDMASK & pcic_intr_alloc_mask, ist, &irq))
331 return (NULL);
332 if ((ih = isa_intr_establish(h->sc->intr_est, irq, ist, ipl,
333 fct, arg)) == NULL)
334 return (NULL);
335
336 reg = pcic_read(h, PCIC_INTR);
337 reg &= ~PCIC_INTR_IRQ_MASK;
338 reg |= irq;
339 pcic_write(h, PCIC_INTR, reg);
340
341 h->ih_irq = irq;
342
343 printf("%s: card irq %d\n", h->pcmcia->dv_xname, irq);
344
345 return (ih);
346 }
347
348 void
349 pcic_isa_chip_intr_disestablish(pch, ih)
350 pcmcia_chipset_handle_t pch;
351 void *ih;
352 {
353 struct pcic_handle *h = (struct pcic_handle *) pch;
354 int reg;
355
356 h->ih_irq = 0;
357
358 reg = pcic_read(h, PCIC_INTR);
359 reg &= ~(PCIC_INTR_IRQ_MASK | PCIC_INTR_ENABLE);
360 pcic_write(h, PCIC_INTR, reg);
361
362 isa_intr_disestablish(h->sc->intr_est, ih);
363 }
364