i82365_isa.c revision 1.3 1 1.3 thorpej /* $NetBSD: i82365_isa.c,v 1.3 1997/10/19 18:56:55 thorpej Exp $ */
2 1.2 thorpej
3 1.2 thorpej #define PCICISADEBUG
4 1.2 thorpej
5 1.2 thorpej /*
6 1.2 thorpej * Copyright (c) 1997 Marc Horowitz. All rights reserved.
7 1.2 thorpej *
8 1.2 thorpej * Redistribution and use in source and binary forms, with or without
9 1.2 thorpej * modification, are permitted provided that the following conditions
10 1.2 thorpej * are met:
11 1.2 thorpej * 1. Redistributions of source code must retain the above copyright
12 1.2 thorpej * notice, this list of conditions and the following disclaimer.
13 1.2 thorpej * 2. Redistributions in binary form must reproduce the above copyright
14 1.2 thorpej * notice, this list of conditions and the following disclaimer in the
15 1.2 thorpej * documentation and/or other materials provided with the distribution.
16 1.2 thorpej * 3. All advertising materials mentioning features or use of this software
17 1.2 thorpej * must display the following acknowledgement:
18 1.2 thorpej * This product includes software developed by Marc Horowitz.
19 1.2 thorpej * 4. The name of the author may not be used to endorse or promote products
20 1.2 thorpej * derived from this software without specific prior written permission.
21 1.2 thorpej *
22 1.2 thorpej * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
23 1.2 thorpej * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24 1.2 thorpej * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25 1.2 thorpej * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
26 1.2 thorpej * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
27 1.2 thorpej * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 1.2 thorpej * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 1.2 thorpej * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 1.2 thorpej * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
31 1.2 thorpej * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 1.2 thorpej */
33 1.2 thorpej
34 1.2 thorpej #include <sys/types.h>
35 1.2 thorpej #include <sys/param.h>
36 1.2 thorpej #include <sys/systm.h>
37 1.2 thorpej #include <sys/device.h>
38 1.2 thorpej #include <sys/extent.h>
39 1.2 thorpej #include <sys/malloc.h>
40 1.2 thorpej
41 1.2 thorpej #include <vm/vm.h>
42 1.2 thorpej
43 1.2 thorpej #include <machine/bus.h>
44 1.2 thorpej #include <machine/intr.h>
45 1.2 thorpej
46 1.2 thorpej #include <dev/isa/isareg.h>
47 1.2 thorpej #include <dev/isa/isavar.h>
48 1.2 thorpej
49 1.2 thorpej #include <dev/pcmcia/pcmciareg.h>
50 1.2 thorpej #include <dev/pcmcia/pcmciavar.h>
51 1.2 thorpej #include <dev/pcmcia/pcmciachip.h>
52 1.2 thorpej
53 1.2 thorpej #include <dev/ic/i82365reg.h>
54 1.2 thorpej #include <dev/ic/i82365var.h>
55 1.2 thorpej
56 1.2 thorpej #ifdef PCICISADEBUG
57 1.2 thorpej int pcicisa_debug = 0 /* XXX */ ;
58 1.2 thorpej #define DPRINTF(arg) if (pcicisa_debug) printf arg;
59 1.2 thorpej #else
60 1.2 thorpej #define DPRINTF(arg)
61 1.2 thorpej #endif
62 1.2 thorpej
63 1.2 thorpej int pcic_isa_probe __P((struct device *, void *, void *));
64 1.2 thorpej void pcic_isa_attach __P((struct device *, struct device *, void *));
65 1.2 thorpej
66 1.2 thorpej void *pcic_isa_chip_intr_establish __P((pcmcia_chipset_handle_t,
67 1.2 thorpej struct pcmcia_function *, int, int (*) (void *), void *));
68 1.2 thorpej void pcic_isa_chip_intr_disestablish __P((pcmcia_chipset_handle_t, void *));
69 1.2 thorpej
70 1.2 thorpej struct cfattach pcic_isa_ca = {
71 1.2 thorpej sizeof(struct pcic_softc), pcic_isa_probe, pcic_isa_attach
72 1.2 thorpej };
73 1.2 thorpej
74 1.2 thorpej static struct pcmcia_chip_functions pcic_isa_functions = {
75 1.2 thorpej pcic_chip_mem_alloc,
76 1.2 thorpej pcic_chip_mem_free,
77 1.2 thorpej pcic_chip_mem_map,
78 1.2 thorpej pcic_chip_mem_unmap,
79 1.2 thorpej
80 1.2 thorpej pcic_chip_io_alloc,
81 1.2 thorpej pcic_chip_io_free,
82 1.2 thorpej pcic_chip_io_map,
83 1.2 thorpej pcic_chip_io_unmap,
84 1.2 thorpej
85 1.2 thorpej pcic_isa_chip_intr_establish,
86 1.2 thorpej pcic_isa_chip_intr_disestablish,
87 1.2 thorpej
88 1.2 thorpej pcic_chip_socket_enable,
89 1.2 thorpej pcic_chip_socket_disable,
90 1.2 thorpej };
91 1.2 thorpej
92 1.2 thorpej int
93 1.2 thorpej pcic_isa_probe(parent, match, aux)
94 1.2 thorpej struct device *parent;
95 1.2 thorpej void *match, *aux;
96 1.2 thorpej {
97 1.2 thorpej struct isa_attach_args *ia = aux;
98 1.2 thorpej bus_space_tag_t iot = ia->ia_iot;
99 1.2 thorpej bus_space_handle_t ioh, memh;
100 1.2 thorpej int val, found;
101 1.3 thorpej
102 1.3 thorpej /* Disallow wildcarded i/o address. */
103 1.3 thorpej if (ia->ia_iobase == ISACF_PORT_DEFAULT)
104 1.3 thorpej return (0);
105 1.2 thorpej
106 1.2 thorpej if (bus_space_map(iot, ia->ia_iobase, PCIC_IOSIZE, 0, &ioh))
107 1.2 thorpej return (0);
108 1.2 thorpej
109 1.2 thorpej if (ia->ia_msize == -1)
110 1.2 thorpej ia->ia_msize = PCIC_MEMSIZE;
111 1.2 thorpej
112 1.2 thorpej if (bus_space_map(ia->ia_memt, ia->ia_maddr, ia->ia_msize, 0, &memh))
113 1.2 thorpej return (0);
114 1.2 thorpej
115 1.2 thorpej found = 0;
116 1.2 thorpej
117 1.2 thorpej /*
118 1.2 thorpej * this could be done with a loop, but it would violate the
119 1.2 thorpej * abstraction
120 1.2 thorpej */
121 1.2 thorpej
122 1.2 thorpej bus_space_write_1(iot, ioh, PCIC_REG_INDEX, C0SA + PCIC_IDENT);
123 1.2 thorpej
124 1.2 thorpej val = bus_space_read_1(iot, ioh, PCIC_REG_DATA);
125 1.2 thorpej
126 1.2 thorpej if (pcic_ident_ok(val))
127 1.2 thorpej found++;
128 1.2 thorpej
129 1.2 thorpej
130 1.2 thorpej bus_space_write_1(iot, ioh, PCIC_REG_INDEX, C0SB + PCIC_IDENT);
131 1.2 thorpej
132 1.2 thorpej val = bus_space_read_1(iot, ioh, PCIC_REG_DATA);
133 1.2 thorpej
134 1.2 thorpej if (pcic_ident_ok(val))
135 1.2 thorpej found++;
136 1.2 thorpej
137 1.2 thorpej
138 1.2 thorpej bus_space_write_1(iot, ioh, PCIC_REG_INDEX, C1SA + PCIC_IDENT);
139 1.2 thorpej
140 1.2 thorpej val = bus_space_read_1(iot, ioh, PCIC_REG_DATA);
141 1.2 thorpej
142 1.2 thorpej if (pcic_ident_ok(val))
143 1.2 thorpej found++;
144 1.2 thorpej
145 1.2 thorpej
146 1.2 thorpej bus_space_write_1(iot, ioh, PCIC_REG_INDEX, C1SB + PCIC_IDENT);
147 1.2 thorpej
148 1.2 thorpej val = bus_space_read_1(iot, ioh, PCIC_REG_DATA);
149 1.2 thorpej
150 1.2 thorpej if (pcic_ident_ok(val))
151 1.2 thorpej found++;
152 1.2 thorpej
153 1.2 thorpej
154 1.2 thorpej bus_space_unmap(iot, ioh, PCIC_IOSIZE);
155 1.2 thorpej bus_space_unmap(ia->ia_memt, memh, ia->ia_msize);
156 1.2 thorpej
157 1.2 thorpej if (!found)
158 1.2 thorpej return (0);
159 1.2 thorpej
160 1.2 thorpej ia->ia_iosize = PCIC_IOSIZE;
161 1.2 thorpej
162 1.2 thorpej return (1);
163 1.2 thorpej }
164 1.2 thorpej
165 1.2 thorpej #ifndef PCIC_ISA_ALLOC_IOBASE
166 1.2 thorpej #define PCIC_ISA_ALLOC_IOBASE 0
167 1.2 thorpej #endif
168 1.2 thorpej
169 1.2 thorpej #ifndef PCIC_ISA_ALLOC_IOSIZE
170 1.2 thorpej #define PCIC_ISA_ALLOC_IOSIZE 0
171 1.2 thorpej #endif
172 1.2 thorpej
173 1.2 thorpej int pcic_isa_alloc_iobase = PCIC_ISA_ALLOC_IOBASE;
174 1.2 thorpej int pcic_isa_alloc_iosize = PCIC_ISA_ALLOC_IOSIZE;
175 1.2 thorpej
176 1.2 thorpej void
177 1.2 thorpej pcic_isa_attach(parent, self, aux)
178 1.2 thorpej struct device *parent, *self;
179 1.2 thorpej void *aux;
180 1.2 thorpej {
181 1.2 thorpej struct pcic_softc *sc = (void *) self;
182 1.2 thorpej struct isa_attach_args *ia = aux;
183 1.2 thorpej isa_chipset_tag_t ic = ia->ia_ic;
184 1.2 thorpej bus_space_tag_t iot = ia->ia_iot;
185 1.2 thorpej bus_space_tag_t memt = ia->ia_memt;
186 1.2 thorpej bus_space_handle_t ioh;
187 1.2 thorpej bus_space_handle_t memh;
188 1.2 thorpej bus_space_handle_t ioh_high;
189 1.2 thorpej int i, iobuswidth, tmp1, tmp2;
190 1.2 thorpej
191 1.2 thorpej /* Map i/o space. */
192 1.2 thorpej if (bus_space_map(iot, ia->ia_iobase, ia->ia_iosize, 0, &ioh))
193 1.2 thorpej panic("pcic_isa_attach: can't map i/o space");
194 1.2 thorpej
195 1.2 thorpej /* Map mem space. */
196 1.2 thorpej if (bus_space_map(memt, ia->ia_maddr, ia->ia_msize, 0, &memh))
197 1.2 thorpej panic("pcic_isa_attach: can't map i/o space");
198 1.2 thorpej
199 1.2 thorpej sc->membase = ia->ia_maddr;
200 1.2 thorpej sc->subregionmask = (1 << (ia->ia_msize / PCIC_MEM_PAGESIZE)) - 1;
201 1.2 thorpej
202 1.2 thorpej sc->intr_est = ic;
203 1.2 thorpej sc->pct = (pcmcia_chipset_tag_t) & pcic_isa_functions;
204 1.2 thorpej
205 1.2 thorpej sc->iot = iot;
206 1.2 thorpej sc->ioh = ioh;
207 1.2 thorpej sc->memt = memt;
208 1.2 thorpej sc->memh = memh;
209 1.2 thorpej
210 1.2 thorpej /*
211 1.2 thorpej * allocate an irq. it will be used by both controllers. I could
212 1.2 thorpej * use two different interrupts, but interrupts are relatively
213 1.2 thorpej * scarce, shareable, and for PCIC controllers, very infrequent.
214 1.2 thorpej */
215 1.2 thorpej
216 1.2 thorpej if ((sc->irq = ia->ia_irq) == IRQUNK) {
217 1.2 thorpej /* XXX CHECK RETURN VALUE */
218 1.2 thorpej (void) isa_intr_alloc(ic, PCIC_CSC_INTR_IRQ_VALIDMASK,
219 1.2 thorpej IST_EDGE, &sc->irq);
220 1.2 thorpej printf(": using irq %d", sc->irq);
221 1.2 thorpej }
222 1.2 thorpej printf("\n");
223 1.2 thorpej
224 1.2 thorpej pcic_attach(sc);
225 1.2 thorpej
226 1.2 thorpej /*
227 1.2 thorpej * figure out how wide the isa bus is. Do this by checking if the
228 1.2 thorpej * pcic controller is mirrored 0x400 above where we expect it to be.
229 1.2 thorpej */
230 1.2 thorpej
231 1.2 thorpej iobuswidth = 12;
232 1.2 thorpej
233 1.2 thorpej /* Map i/o space. */
234 1.2 thorpej if (bus_space_map(iot, ia->ia_iobase + 0x400, ia->ia_iosize, 0,
235 1.2 thorpej &ioh_high))
236 1.2 thorpej panic("pcic_isa_attach: can't map high i/o space");
237 1.2 thorpej
238 1.2 thorpej for (i = 0; i < PCIC_NSLOTS; i++) {
239 1.2 thorpej if (sc->handle[i].flags & PCIC_FLAG_SOCKETP) {
240 1.2 thorpej /*
241 1.2 thorpej * read the ident flags from the normal space and
242 1.2 thorpej * from the mirror, and compare them
243 1.2 thorpej */
244 1.2 thorpej
245 1.2 thorpej bus_space_write_1(iot, ioh, PCIC_REG_INDEX,
246 1.2 thorpej sc->handle[i].sock + PCIC_IDENT);
247 1.2 thorpej tmp1 = bus_space_read_1(iot, ioh, PCIC_REG_DATA);
248 1.2 thorpej
249 1.2 thorpej bus_space_write_1(iot, ioh_high, PCIC_REG_INDEX,
250 1.2 thorpej sc->handle[i].sock + PCIC_IDENT);
251 1.2 thorpej tmp2 = bus_space_read_1(iot, ioh_high, PCIC_REG_DATA);
252 1.2 thorpej
253 1.2 thorpej if (tmp1 == tmp2)
254 1.2 thorpej iobuswidth = 10;
255 1.2 thorpej }
256 1.2 thorpej }
257 1.2 thorpej
258 1.2 thorpej bus_space_free(iot, ioh_high, ia->ia_iosize);
259 1.2 thorpej
260 1.2 thorpej /*
261 1.2 thorpej * XXX mycroft recommends I/O space range 0x400-0xfff . I should put
262 1.2 thorpej * this in a header somewhere
263 1.2 thorpej */
264 1.2 thorpej
265 1.2 thorpej /*
266 1.2 thorpej * XXX some hardware doesn't seem to grok addresses in 0x400 range--
267 1.2 thorpej * apparently missing a bit or more of address lines. (e.g.
268 1.2 thorpej * CIRRUS_PD672X with Linksys EthernetCard ne2000 clone in TI
269 1.2 thorpej * TravelMate 5000--not clear which is at fault)
270 1.2 thorpej *
271 1.2 thorpej * Add a kludge to detect 10 bit wide buses and deal with them,
272 1.2 thorpej * and also a config file option to override the probe.
273 1.2 thorpej */
274 1.2 thorpej
275 1.2 thorpej if (iobuswidth == 10) {
276 1.2 thorpej sc->iobase = 0x300;
277 1.2 thorpej sc->iosize = 0x0ff;
278 1.2 thorpej } else {
279 1.2 thorpej sc->iobase = 0x400;
280 1.2 thorpej sc->iosize = 0xbff;
281 1.2 thorpej }
282 1.2 thorpej
283 1.2 thorpej DPRINTF(("%s: bus_space_alloc range 0x%04lx-0x%04lx (probed)\n",
284 1.2 thorpej sc->dev.dv_xname, (long) sc->iobase,
285 1.2 thorpej (long) sc->iobase + sc->iosize));
286 1.2 thorpej
287 1.2 thorpej if (pcic_isa_alloc_iobase && pcic_isa_alloc_iosize) {
288 1.2 thorpej sc->iobase = pcic_isa_alloc_iobase;
289 1.2 thorpej sc->iosize = pcic_isa_alloc_iosize;
290 1.2 thorpej
291 1.2 thorpej DPRINTF(("%s: bus_space_alloc range 0x%04lx-0x%04lx "
292 1.2 thorpej "(config override)\n", sc->dev.dv_xname, (long) sc->iobase,
293 1.2 thorpej (long) sc->iobase + sc->iosize));
294 1.2 thorpej }
295 1.2 thorpej sc->ih = isa_intr_establish(ic, sc->irq, IST_EDGE, IPL_TTY,
296 1.2 thorpej pcic_intr, sc);
297 1.2 thorpej
298 1.2 thorpej pcic_attach_sockets(sc);
299 1.2 thorpej }
300 1.2 thorpej
301 1.2 thorpej /*
302 1.2 thorpej * allow patching or kernel option file override of available IRQs. Useful if
303 1.2 thorpej * order of probing would screw up other devices, or if PCIC hardware/cards
304 1.2 thorpej * have trouble with certain interrupt lines.
305 1.2 thorpej */
306 1.2 thorpej
307 1.2 thorpej #ifndef PCIC_INTR_ALLOC_MASK
308 1.2 thorpej #define PCIC_INTR_ALLOC_MASK 0xffff
309 1.2 thorpej #endif
310 1.2 thorpej
311 1.2 thorpej int pcic_intr_alloc_mask = PCIC_INTR_ALLOC_MASK;
312 1.2 thorpej
313 1.2 thorpej void *
314 1.2 thorpej pcic_isa_chip_intr_establish(pch, pf, ipl, fct, arg)
315 1.2 thorpej pcmcia_chipset_handle_t pch;
316 1.2 thorpej struct pcmcia_function *pf;
317 1.2 thorpej int ipl;
318 1.2 thorpej int (*fct) __P((void *));
319 1.2 thorpej void *arg;
320 1.2 thorpej {
321 1.2 thorpej struct pcic_handle *h = (struct pcic_handle *) pch;
322 1.2 thorpej int irq, ist;
323 1.2 thorpej void *ih;
324 1.2 thorpej int reg;
325 1.2 thorpej
326 1.2 thorpej if (pf->cfe->flags & PCMCIA_CFE_IRQLEVEL)
327 1.2 thorpej ist = IST_LEVEL;
328 1.2 thorpej else if (pf->cfe->flags & PCMCIA_CFE_IRQPULSE)
329 1.2 thorpej ist = IST_PULSE;
330 1.2 thorpej else
331 1.2 thorpej ist = IST_LEVEL;
332 1.2 thorpej
333 1.2 thorpej if (isa_intr_alloc(h->sc->intr_est,
334 1.2 thorpej PCIC_INTR_IRQ_VALIDMASK & pcic_intr_alloc_mask, ist, &irq))
335 1.2 thorpej return (NULL);
336 1.2 thorpej if ((ih = isa_intr_establish(h->sc->intr_est, irq, ist, ipl,
337 1.2 thorpej fct, arg)) == NULL)
338 1.2 thorpej return (NULL);
339 1.2 thorpej
340 1.2 thorpej reg = pcic_read(h, PCIC_INTR);
341 1.2 thorpej reg &= ~PCIC_INTR_IRQ_MASK;
342 1.2 thorpej reg |= irq;
343 1.2 thorpej pcic_write(h, PCIC_INTR, reg);
344 1.2 thorpej
345 1.2 thorpej h->ih_irq = irq;
346 1.2 thorpej
347 1.2 thorpej printf("%s: card irq %d\n", h->pcmcia->dv_xname, irq);
348 1.2 thorpej
349 1.2 thorpej return (ih);
350 1.2 thorpej }
351 1.2 thorpej
352 1.2 thorpej void
353 1.2 thorpej pcic_isa_chip_intr_disestablish(pch, ih)
354 1.2 thorpej pcmcia_chipset_handle_t pch;
355 1.2 thorpej void *ih;
356 1.2 thorpej {
357 1.2 thorpej struct pcic_handle *h = (struct pcic_handle *) pch;
358 1.2 thorpej int reg;
359 1.2 thorpej
360 1.2 thorpej h->ih_irq = 0;
361 1.2 thorpej
362 1.2 thorpej reg = pcic_read(h, PCIC_INTR);
363 1.2 thorpej reg &= ~(PCIC_INTR_IRQ_MASK | PCIC_INTR_ENABLE);
364 1.2 thorpej pcic_write(h, PCIC_INTR, reg);
365 1.2 thorpej
366 1.2 thorpej isa_intr_disestablish(h->sc->intr_est, ih);
367 1.2 thorpej }
368