i82365.c revision 1.6 1 1.6 enami /* $NetBSD: i82365.c,v 1.6 1997/10/19 14:10:11 enami Exp $ */
2 1.2 thorpej
3 1.2 thorpej #define PCICDEBUG
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/pcmcia/pcmciareg.h>
47 1.2 thorpej #include <dev/pcmcia/pcmciavar.h>
48 1.2 thorpej
49 1.2 thorpej #include <dev/ic/i82365reg.h>
50 1.2 thorpej #include <dev/ic/i82365var.h>
51 1.2 thorpej
52 1.5 enami #include "locators.h"
53 1.5 enami
54 1.2 thorpej #ifdef PCICDEBUG
55 1.2 thorpej int pcic_debug = 0;
56 1.2 thorpej #define DPRINTF(arg) if (pcic_debug) printf arg;
57 1.2 thorpej #else
58 1.2 thorpej #define DPRINTF(arg)
59 1.2 thorpej #endif
60 1.2 thorpej
61 1.2 thorpej #define PCIC_VENDOR_UNKNOWN 0
62 1.2 thorpej #define PCIC_VENDOR_I82365SLR0 1
63 1.2 thorpej #define PCIC_VENDOR_I82365SLR1 2
64 1.2 thorpej #define PCIC_VENDOR_CIRRUS_PD6710 3
65 1.2 thorpej #define PCIC_VENDOR_CIRRUS_PD672X 4
66 1.2 thorpej
67 1.2 thorpej /*
68 1.2 thorpej * Individual drivers will allocate their own memory and io regions. Memory
69 1.2 thorpej * regions must be a multiple of 4k, aligned on a 4k boundary.
70 1.2 thorpej */
71 1.2 thorpej
72 1.2 thorpej #define PCIC_MEM_ALIGN PCIC_MEM_PAGESIZE
73 1.2 thorpej
74 1.2 thorpej void pcic_attach_socket __P((struct pcic_handle *));
75 1.2 thorpej void pcic_init_socket __P((struct pcic_handle *));
76 1.2 thorpej
77 1.2 thorpej #ifdef __BROKEN_INDIRECT_CONFIG
78 1.2 thorpej int pcic_submatch __P((struct device *, void *, void *));
79 1.2 thorpej #else
80 1.2 thorpej int pcic_submatch __P((struct device *, struct cfdata *, void *));
81 1.2 thorpej #endif
82 1.2 thorpej int pcic_print __P((void *arg, const char *pnp));
83 1.2 thorpej int pcic_intr_socket __P((struct pcic_handle *));
84 1.2 thorpej
85 1.2 thorpej void pcic_attach_card __P((struct pcic_handle *));
86 1.2 thorpej void pcic_detach_card __P((struct pcic_handle *));
87 1.2 thorpej
88 1.2 thorpej void pcic_chip_do_mem_map __P((struct pcic_handle *, int));
89 1.2 thorpej void pcic_chip_do_io_map __P((struct pcic_handle *, int));
90 1.2 thorpej
91 1.2 thorpej struct cfdriver pcic_cd = {
92 1.2 thorpej NULL, "pcic", DV_DULL
93 1.2 thorpej };
94 1.2 thorpej
95 1.2 thorpej int
96 1.2 thorpej pcic_ident_ok(ident)
97 1.2 thorpej int ident;
98 1.2 thorpej {
99 1.2 thorpej /* this is very empirical and heuristic */
100 1.2 thorpej
101 1.2 thorpej if ((ident == 0) || (ident == 0xff) || (ident & PCIC_IDENT_ZERO))
102 1.2 thorpej return (0);
103 1.2 thorpej
104 1.2 thorpej if ((ident & PCIC_IDENT_IFTYPE_MASK) != PCIC_IDENT_IFTYPE_MEM_AND_IO) {
105 1.2 thorpej #ifdef DIAGNOSTIC
106 1.2 thorpej printf("pcic: does not support memory and I/O cards, "
107 1.2 thorpej "ignored (ident=%0x)\n", ident);
108 1.2 thorpej #endif
109 1.2 thorpej return (0);
110 1.2 thorpej }
111 1.2 thorpej return (1);
112 1.2 thorpej }
113 1.2 thorpej
114 1.2 thorpej int
115 1.2 thorpej pcic_vendor(h)
116 1.2 thorpej struct pcic_handle *h;
117 1.2 thorpej {
118 1.2 thorpej int reg;
119 1.2 thorpej
120 1.2 thorpej /*
121 1.2 thorpej * the chip_id of the cirrus toggles between 11 and 00 after a write.
122 1.2 thorpej * weird.
123 1.2 thorpej */
124 1.2 thorpej
125 1.2 thorpej pcic_write(h, PCIC_CIRRUS_CHIP_INFO, 0);
126 1.2 thorpej reg = pcic_read(h, -1);
127 1.2 thorpej
128 1.2 thorpej if ((reg & PCIC_CIRRUS_CHIP_INFO_CHIP_ID) ==
129 1.2 thorpej PCIC_CIRRUS_CHIP_INFO_CHIP_ID) {
130 1.2 thorpej reg = pcic_read(h, -1);
131 1.2 thorpej if ((reg & PCIC_CIRRUS_CHIP_INFO_CHIP_ID) == 0) {
132 1.2 thorpej if (reg & PCIC_CIRRUS_CHIP_INFO_SLOTS)
133 1.2 thorpej return (PCIC_VENDOR_CIRRUS_PD672X);
134 1.2 thorpej else
135 1.2 thorpej return (PCIC_VENDOR_CIRRUS_PD6710);
136 1.2 thorpej }
137 1.2 thorpej }
138 1.2 thorpej /* XXX how do I identify the GD6729? */
139 1.2 thorpej
140 1.2 thorpej reg = pcic_read(h, PCIC_IDENT);
141 1.2 thorpej
142 1.2 thorpej if ((reg & PCIC_IDENT_REV_MASK) == PCIC_IDENT_REV_I82365SLR0)
143 1.2 thorpej return (PCIC_VENDOR_I82365SLR0);
144 1.2 thorpej else
145 1.2 thorpej return (PCIC_VENDOR_I82365SLR1);
146 1.2 thorpej
147 1.2 thorpej return (PCIC_VENDOR_UNKNOWN);
148 1.2 thorpej }
149 1.2 thorpej
150 1.2 thorpej char *
151 1.2 thorpej pcic_vendor_to_string(vendor)
152 1.2 thorpej int vendor;
153 1.2 thorpej {
154 1.2 thorpej switch (vendor) {
155 1.2 thorpej case PCIC_VENDOR_I82365SLR0:
156 1.2 thorpej return ("Intel 82365SL Revision 0");
157 1.2 thorpej case PCIC_VENDOR_I82365SLR1:
158 1.2 thorpej return ("Intel 82365SL Revision 1");
159 1.2 thorpej case PCIC_VENDOR_CIRRUS_PD6710:
160 1.2 thorpej return ("Cirrus PD6710");
161 1.2 thorpej case PCIC_VENDOR_CIRRUS_PD672X:
162 1.2 thorpej return ("Cirrus PD672X");
163 1.2 thorpej }
164 1.2 thorpej
165 1.2 thorpej return ("Unknown controller");
166 1.2 thorpej }
167 1.2 thorpej
168 1.2 thorpej void
169 1.2 thorpej pcic_attach(sc)
170 1.2 thorpej struct pcic_softc *sc;
171 1.2 thorpej {
172 1.2 thorpej int vendor, count, i, reg;
173 1.2 thorpej
174 1.2 thorpej /* now check for each controller/socket */
175 1.2 thorpej
176 1.2 thorpej /*
177 1.2 thorpej * this could be done with a loop, but it would violate the
178 1.2 thorpej * abstraction
179 1.2 thorpej */
180 1.2 thorpej
181 1.2 thorpej count = 0;
182 1.2 thorpej
183 1.2 thorpej DPRINTF(("pcic ident regs:"));
184 1.2 thorpej
185 1.2 thorpej sc->handle[0].sc = sc;
186 1.2 thorpej sc->handle[0].sock = C0SA;
187 1.2 thorpej if (pcic_ident_ok(reg = pcic_read(&sc->handle[0], PCIC_IDENT))) {
188 1.2 thorpej sc->handle[0].flags = PCIC_FLAG_SOCKETP;
189 1.2 thorpej count++;
190 1.2 thorpej } else {
191 1.2 thorpej sc->handle[0].flags = 0;
192 1.2 thorpej }
193 1.2 thorpej
194 1.2 thorpej DPRINTF((" 0x%02x", reg));
195 1.2 thorpej
196 1.2 thorpej sc->handle[1].sc = sc;
197 1.2 thorpej sc->handle[1].sock = C0SB;
198 1.2 thorpej if (pcic_ident_ok(reg = pcic_read(&sc->handle[1], PCIC_IDENT))) {
199 1.2 thorpej sc->handle[1].flags = PCIC_FLAG_SOCKETP;
200 1.2 thorpej count++;
201 1.2 thorpej } else {
202 1.2 thorpej sc->handle[1].flags = 0;
203 1.2 thorpej }
204 1.2 thorpej
205 1.2 thorpej DPRINTF((" 0x%02x", reg));
206 1.2 thorpej
207 1.2 thorpej sc->handle[2].sc = sc;
208 1.2 thorpej sc->handle[2].sock = C1SA;
209 1.2 thorpej if (pcic_ident_ok(reg = pcic_read(&sc->handle[2], PCIC_IDENT))) {
210 1.2 thorpej sc->handle[2].flags = PCIC_FLAG_SOCKETP;
211 1.2 thorpej count++;
212 1.2 thorpej } else {
213 1.2 thorpej sc->handle[2].flags = 0;
214 1.2 thorpej }
215 1.2 thorpej
216 1.2 thorpej DPRINTF((" 0x%02x", reg));
217 1.2 thorpej
218 1.2 thorpej sc->handle[3].sc = sc;
219 1.2 thorpej sc->handle[3].sock = C1SB;
220 1.2 thorpej if (pcic_ident_ok(reg = pcic_read(&sc->handle[3], PCIC_IDENT))) {
221 1.2 thorpej sc->handle[3].flags = PCIC_FLAG_SOCKETP;
222 1.2 thorpej count++;
223 1.2 thorpej } else {
224 1.2 thorpej sc->handle[3].flags = 0;
225 1.2 thorpej }
226 1.2 thorpej
227 1.2 thorpej DPRINTF((" 0x%02x\n", reg));
228 1.2 thorpej
229 1.2 thorpej if (count == 0)
230 1.2 thorpej panic("pcic_attach: attach found no sockets");
231 1.2 thorpej
232 1.2 thorpej /* establish the interrupt */
233 1.2 thorpej
234 1.2 thorpej /* XXX block interrupts? */
235 1.2 thorpej
236 1.2 thorpej for (i = 0; i < PCIC_NSLOTS; i++) {
237 1.2 thorpej #if 0
238 1.2 thorpej /*
239 1.2 thorpej * this should work, but w/o it, setting tty flags hangs at
240 1.2 thorpej * boot time.
241 1.2 thorpej */
242 1.2 thorpej if (sc->handle[i].flags & PCIC_FLAG_SOCKETP)
243 1.2 thorpej #endif
244 1.2 thorpej {
245 1.2 thorpej pcic_write(&sc->handle[i], PCIC_CSC_INTR, 0);
246 1.2 thorpej pcic_read(&sc->handle[i], PCIC_CSC);
247 1.2 thorpej }
248 1.2 thorpej }
249 1.2 thorpej
250 1.2 thorpej if ((sc->handle[0].flags & PCIC_FLAG_SOCKETP) ||
251 1.2 thorpej (sc->handle[1].flags & PCIC_FLAG_SOCKETP)) {
252 1.2 thorpej vendor = pcic_vendor(&sc->handle[0]);
253 1.2 thorpej
254 1.2 thorpej printf("%s: controller 0 (%s) has ", sc->dev.dv_xname,
255 1.2 thorpej pcic_vendor_to_string(vendor));
256 1.2 thorpej
257 1.2 thorpej if ((sc->handle[0].flags & PCIC_FLAG_SOCKETP) &&
258 1.2 thorpej (sc->handle[1].flags & PCIC_FLAG_SOCKETP))
259 1.2 thorpej printf("sockets A and B\n");
260 1.2 thorpej else if (sc->handle[0].flags & PCIC_FLAG_SOCKETP)
261 1.2 thorpej printf("socket A only\n");
262 1.2 thorpej else
263 1.2 thorpej printf("socket B only\n");
264 1.2 thorpej
265 1.2 thorpej if (sc->handle[0].flags & PCIC_FLAG_SOCKETP)
266 1.2 thorpej sc->handle[0].vendor = vendor;
267 1.2 thorpej if (sc->handle[1].flags & PCIC_FLAG_SOCKETP)
268 1.2 thorpej sc->handle[1].vendor = vendor;
269 1.2 thorpej }
270 1.2 thorpej if ((sc->handle[2].flags & PCIC_FLAG_SOCKETP) ||
271 1.2 thorpej (sc->handle[3].flags & PCIC_FLAG_SOCKETP)) {
272 1.2 thorpej vendor = pcic_vendor(&sc->handle[2]);
273 1.2 thorpej
274 1.2 thorpej printf("%s: controller 1 (%s) has ", sc->dev.dv_xname,
275 1.2 thorpej pcic_vendor_to_string(vendor));
276 1.2 thorpej
277 1.2 thorpej if ((sc->handle[2].flags & PCIC_FLAG_SOCKETP) &&
278 1.2 thorpej (sc->handle[3].flags & PCIC_FLAG_SOCKETP))
279 1.2 thorpej printf("sockets A and B\n");
280 1.2 thorpej else if (sc->handle[2].flags & PCIC_FLAG_SOCKETP)
281 1.2 thorpej printf("socket A only\n");
282 1.2 thorpej else
283 1.2 thorpej printf("socket B only\n");
284 1.2 thorpej
285 1.2 thorpej if (sc->handle[2].flags & PCIC_FLAG_SOCKETP)
286 1.2 thorpej sc->handle[2].vendor = vendor;
287 1.2 thorpej if (sc->handle[3].flags & PCIC_FLAG_SOCKETP)
288 1.2 thorpej sc->handle[3].vendor = vendor;
289 1.2 thorpej }
290 1.2 thorpej }
291 1.2 thorpej
292 1.2 thorpej void
293 1.2 thorpej pcic_attach_sockets(sc)
294 1.2 thorpej struct pcic_softc *sc;
295 1.2 thorpej {
296 1.2 thorpej int i;
297 1.2 thorpej
298 1.2 thorpej for (i = 0; i < PCIC_NSLOTS; i++)
299 1.2 thorpej if (sc->handle[i].flags & PCIC_FLAG_SOCKETP)
300 1.2 thorpej pcic_attach_socket(&sc->handle[i]);
301 1.2 thorpej }
302 1.2 thorpej
303 1.2 thorpej void
304 1.2 thorpej pcic_attach_socket(h)
305 1.2 thorpej struct pcic_handle *h;
306 1.2 thorpej {
307 1.2 thorpej struct pcmciabus_attach_args paa;
308 1.2 thorpej
309 1.2 thorpej /* initialize the rest of the handle */
310 1.2 thorpej
311 1.2 thorpej h->memalloc = 0;
312 1.2 thorpej h->ioalloc = 0;
313 1.2 thorpej h->ih_irq = 0;
314 1.2 thorpej
315 1.2 thorpej /* now, config one pcmcia device per socket */
316 1.2 thorpej
317 1.2 thorpej paa.pct = (pcmcia_chipset_tag_t) h->sc->pct;
318 1.2 thorpej paa.pch = (pcmcia_chipset_handle_t) h;
319 1.2 thorpej paa.iobase = h->sc->iobase;
320 1.2 thorpej paa.iosize = h->sc->iosize;
321 1.2 thorpej
322 1.2 thorpej h->pcmcia = config_found_sm(&h->sc->dev, &paa, pcic_print,
323 1.2 thorpej pcic_submatch);
324 1.2 thorpej
325 1.2 thorpej /* if there's actually a pcmcia device attached, initialize the slot */
326 1.2 thorpej
327 1.2 thorpej if (h->pcmcia)
328 1.2 thorpej pcic_init_socket(h);
329 1.2 thorpej }
330 1.2 thorpej
331 1.2 thorpej void
332 1.2 thorpej pcic_init_socket(h)
333 1.2 thorpej struct pcic_handle *h;
334 1.2 thorpej {
335 1.2 thorpej int reg;
336 1.2 thorpej
337 1.2 thorpej /* set up the card to interrupt on card detect */
338 1.2 thorpej
339 1.2 thorpej pcic_write(h, PCIC_CSC_INTR, (h->sc->irq << PCIC_CSC_INTR_IRQ_SHIFT) |
340 1.2 thorpej PCIC_CSC_INTR_CD_ENABLE);
341 1.2 thorpej pcic_write(h, PCIC_INTR, 0);
342 1.2 thorpej pcic_read(h, PCIC_CSC);
343 1.2 thorpej
344 1.2 thorpej /* unsleep the cirrus controller */
345 1.2 thorpej
346 1.2 thorpej if ((h->vendor == PCIC_VENDOR_CIRRUS_PD6710) ||
347 1.2 thorpej (h->vendor == PCIC_VENDOR_CIRRUS_PD672X)) {
348 1.2 thorpej reg = pcic_read(h, PCIC_CIRRUS_MISC_CTL_2);
349 1.2 thorpej if (reg & PCIC_CIRRUS_MISC_CTL_2_SUSPEND) {
350 1.2 thorpej DPRINTF(("%s: socket %02x was suspended\n",
351 1.2 thorpej h->sc->dev.dv_xname, h->sock));
352 1.2 thorpej reg &= ~PCIC_CIRRUS_MISC_CTL_2_SUSPEND;
353 1.2 thorpej pcic_write(h, PCIC_CIRRUS_MISC_CTL_2, reg);
354 1.2 thorpej }
355 1.2 thorpej }
356 1.2 thorpej /* if there's a card there, then attach it. */
357 1.2 thorpej
358 1.2 thorpej reg = pcic_read(h, PCIC_IF_STATUS);
359 1.2 thorpej
360 1.2 thorpej if ((reg & PCIC_IF_STATUS_CARDDETECT_MASK) ==
361 1.2 thorpej PCIC_IF_STATUS_CARDDETECT_PRESENT)
362 1.2 thorpej pcic_attach_card(h);
363 1.2 thorpej }
364 1.2 thorpej
365 1.2 thorpej int
366 1.2 thorpej #ifdef __BROKEN_INDIRECT_CONFIG
367 1.2 thorpej pcic_submatch(parent, match, aux)
368 1.2 thorpej #else
369 1.2 thorpej pcic_submatch(parent, cf, aux)
370 1.2 thorpej #endif
371 1.2 thorpej struct device *parent;
372 1.2 thorpej #ifdef __BROKEN_INDIRECT_CONFIG
373 1.2 thorpej void *match;
374 1.2 thorpej #else
375 1.2 thorpej struct cfdata *cf;
376 1.2 thorpej #endif
377 1.2 thorpej void *aux;
378 1.2 thorpej {
379 1.2 thorpej #ifdef __BROKEN_INDIRECT_CONFIG
380 1.2 thorpej struct cfdata *cf = match;
381 1.2 thorpej #endif
382 1.2 thorpej
383 1.3 enami struct pcmciabus_attach_args *paa = aux;
384 1.2 thorpej struct pcic_handle *h = (struct pcic_handle *) paa->pch;
385 1.2 thorpej
386 1.2 thorpej switch (h->sock) {
387 1.2 thorpej case C0SA:
388 1.5 enami if (cf->cf_loc[PCICCF_CONTROLLER] !=
389 1.5 enami PCICCF_CONTROLLER_DEFAULT &&
390 1.5 enami cf->cf_loc[PCICCF_CONTROLLER] != 0)
391 1.2 thorpej return 0;
392 1.5 enami if (cf->cf_loc[PCICCF_SOCKET] != PCICCF_SOCKET_DEFAULT &&
393 1.5 enami cf->cf_loc[PCICCF_SOCKET] != 0)
394 1.2 thorpej return 0;
395 1.2 thorpej
396 1.2 thorpej break;
397 1.2 thorpej case C0SB:
398 1.5 enami if (cf->cf_loc[PCICCF_CONTROLLER] !=
399 1.5 enami PCICCF_CONTROLLER_DEFAULT &&
400 1.5 enami cf->cf_loc[PCICCF_CONTROLLER] != 0)
401 1.2 thorpej return 0;
402 1.5 enami if (cf->cf_loc[PCICCF_SOCKET] != PCICCF_SOCKET_DEFAULT &&
403 1.5 enami cf->cf_loc[PCICCF_SOCKET] != 1)
404 1.2 thorpej return 0;
405 1.2 thorpej
406 1.2 thorpej break;
407 1.2 thorpej case C1SA:
408 1.5 enami if (cf->cf_loc[PCICCF_CONTROLLER] !=
409 1.5 enami PCICCF_CONTROLLER_DEFAULT &&
410 1.5 enami cf->cf_loc[PCICCF_CONTROLLER] != 1)
411 1.2 thorpej return 0;
412 1.5 enami if (cf->cf_loc[PCICCF_SOCKET] != PCICCF_SOCKET_DEFAULT &&
413 1.5 enami cf->cf_loc[PCICCF_SOCKET] != 0)
414 1.2 thorpej return 0;
415 1.2 thorpej
416 1.2 thorpej break;
417 1.2 thorpej case C1SB:
418 1.5 enami if (cf->cf_loc[PCICCF_CONTROLLER] !=
419 1.5 enami PCICCF_CONTROLLER_DEFAULT &&
420 1.5 enami cf->cf_loc[PCICCF_CONTROLLER] != 1)
421 1.2 thorpej return 0;
422 1.5 enami if (cf->cf_loc[PCICCF_SOCKET] != PCICCF_SOCKET_DEFAULT &&
423 1.5 enami cf->cf_loc[PCICCF_SOCKET] != 1)
424 1.2 thorpej return 0;
425 1.2 thorpej
426 1.2 thorpej break;
427 1.2 thorpej default:
428 1.2 thorpej panic("unknown pcic socket");
429 1.2 thorpej }
430 1.2 thorpej
431 1.2 thorpej return ((*cf->cf_attach->ca_match)(parent, cf, aux));
432 1.2 thorpej }
433 1.2 thorpej
434 1.2 thorpej int
435 1.2 thorpej pcic_print(arg, pnp)
436 1.2 thorpej void *arg;
437 1.2 thorpej const char *pnp;
438 1.2 thorpej {
439 1.3 enami struct pcmciabus_attach_args *paa = arg;
440 1.2 thorpej struct pcic_handle *h = (struct pcic_handle *) paa->pch;
441 1.2 thorpej
442 1.2 thorpej /* Only "pcmcia"s can attach to "pcic"s... easy. */
443 1.2 thorpej if (pnp)
444 1.2 thorpej printf("pcmcia at %s", pnp);
445 1.2 thorpej
446 1.2 thorpej switch (h->sock) {
447 1.2 thorpej case C0SA:
448 1.2 thorpej printf(" controller 0 socket 0");
449 1.2 thorpej break;
450 1.2 thorpej case C0SB:
451 1.2 thorpej printf(" controller 0 socket 1");
452 1.2 thorpej break;
453 1.2 thorpej case C1SA:
454 1.2 thorpej printf(" controller 1 socket 0");
455 1.2 thorpej break;
456 1.2 thorpej case C1SB:
457 1.2 thorpej printf(" controller 1 socket 1");
458 1.2 thorpej break;
459 1.2 thorpej default:
460 1.2 thorpej panic("unknown pcic socket");
461 1.2 thorpej }
462 1.2 thorpej
463 1.2 thorpej return (UNCONF);
464 1.2 thorpej }
465 1.2 thorpej
466 1.2 thorpej int
467 1.2 thorpej pcic_intr(arg)
468 1.2 thorpej void *arg;
469 1.2 thorpej {
470 1.3 enami struct pcic_softc *sc = arg;
471 1.2 thorpej int i, ret = 0;
472 1.2 thorpej
473 1.2 thorpej DPRINTF(("%s: intr\n", sc->dev.dv_xname));
474 1.2 thorpej
475 1.2 thorpej for (i = 0; i < PCIC_NSLOTS; i++)
476 1.2 thorpej if (sc->handle[i].flags & PCIC_FLAG_SOCKETP)
477 1.2 thorpej ret += pcic_intr_socket(&sc->handle[i]);
478 1.2 thorpej
479 1.2 thorpej return (ret ? 1 : 0);
480 1.2 thorpej }
481 1.2 thorpej
482 1.2 thorpej int
483 1.2 thorpej pcic_intr_socket(h)
484 1.2 thorpej struct pcic_handle *h;
485 1.2 thorpej {
486 1.2 thorpej int cscreg;
487 1.2 thorpej
488 1.2 thorpej cscreg = pcic_read(h, PCIC_CSC);
489 1.2 thorpej
490 1.2 thorpej cscreg &= (PCIC_CSC_GPI |
491 1.2 thorpej PCIC_CSC_CD |
492 1.2 thorpej PCIC_CSC_READY |
493 1.2 thorpej PCIC_CSC_BATTWARN |
494 1.2 thorpej PCIC_CSC_BATTDEAD);
495 1.2 thorpej
496 1.2 thorpej if (cscreg & PCIC_CSC_GPI) {
497 1.2 thorpej DPRINTF(("%s: %02x GPI\n", h->sc->dev.dv_xname, h->sock));
498 1.2 thorpej }
499 1.2 thorpej if (cscreg & PCIC_CSC_CD) {
500 1.2 thorpej int statreg;
501 1.2 thorpej
502 1.2 thorpej statreg = pcic_read(h, PCIC_IF_STATUS);
503 1.2 thorpej
504 1.2 thorpej DPRINTF(("%s: %02x CD %x\n", h->sc->dev.dv_xname, h->sock,
505 1.2 thorpej statreg));
506 1.2 thorpej
507 1.2 thorpej /*
508 1.2 thorpej * XXX This should probably schedule something to happen
509 1.2 thorpej * after the interrupt handler completes
510 1.2 thorpej */
511 1.2 thorpej
512 1.2 thorpej if ((statreg & PCIC_IF_STATUS_CARDDETECT_MASK) ==
513 1.2 thorpej PCIC_IF_STATUS_CARDDETECT_PRESENT) {
514 1.2 thorpej if (!(h->flags & PCIC_FLAG_CARDP))
515 1.2 thorpej pcic_attach_card(h);
516 1.2 thorpej } else {
517 1.2 thorpej if (h->flags & PCIC_FLAG_CARDP)
518 1.2 thorpej pcic_detach_card(h);
519 1.2 thorpej }
520 1.2 thorpej }
521 1.2 thorpej if (cscreg & PCIC_CSC_READY) {
522 1.2 thorpej DPRINTF(("%s: %02x READY\n", h->sc->dev.dv_xname, h->sock));
523 1.2 thorpej /* shouldn't happen */
524 1.2 thorpej }
525 1.2 thorpej if (cscreg & PCIC_CSC_BATTWARN) {
526 1.2 thorpej DPRINTF(("%s: %02x BATTWARN\n", h->sc->dev.dv_xname, h->sock));
527 1.2 thorpej }
528 1.2 thorpej if (cscreg & PCIC_CSC_BATTDEAD) {
529 1.2 thorpej DPRINTF(("%s: %02x BATTDEAD\n", h->sc->dev.dv_xname, h->sock));
530 1.2 thorpej }
531 1.2 thorpej return (cscreg ? 1 : 0);
532 1.2 thorpej }
533 1.2 thorpej
534 1.2 thorpej void
535 1.2 thorpej pcic_attach_card(h)
536 1.2 thorpej struct pcic_handle *h;
537 1.2 thorpej {
538 1.2 thorpej if (h->flags & PCIC_FLAG_CARDP)
539 1.2 thorpej panic("pcic_attach_card: already attached");
540 1.2 thorpej
541 1.2 thorpej /* call the MI attach function */
542 1.2 thorpej
543 1.2 thorpej pcmcia_card_attach(h->pcmcia);
544 1.2 thorpej
545 1.2 thorpej h->flags |= PCIC_FLAG_CARDP;
546 1.2 thorpej }
547 1.2 thorpej
548 1.2 thorpej void
549 1.2 thorpej pcic_detach_card(h)
550 1.2 thorpej struct pcic_handle *h;
551 1.2 thorpej {
552 1.2 thorpej if (!(h->flags & PCIC_FLAG_CARDP))
553 1.2 thorpej panic("pcic_attach_card: already detached");
554 1.2 thorpej
555 1.2 thorpej h->flags &= ~PCIC_FLAG_CARDP;
556 1.2 thorpej
557 1.2 thorpej /* call the MI attach function */
558 1.2 thorpej
559 1.2 thorpej pcmcia_card_detach(h->pcmcia);
560 1.2 thorpej
561 1.2 thorpej /* disable card detect resume and configuration reset */
562 1.2 thorpej
563 1.2 thorpej /* power down the socket */
564 1.2 thorpej
565 1.2 thorpej pcic_write(h, PCIC_PWRCTL, 0);
566 1.2 thorpej
567 1.2 thorpej /* reset the card */
568 1.2 thorpej
569 1.2 thorpej pcic_write(h, PCIC_INTR, 0);
570 1.2 thorpej }
571 1.2 thorpej
572 1.2 thorpej int
573 1.2 thorpej pcic_chip_mem_alloc(pch, size, pcmhp)
574 1.2 thorpej pcmcia_chipset_handle_t pch;
575 1.2 thorpej bus_size_t size;
576 1.2 thorpej struct pcmcia_mem_handle *pcmhp;
577 1.2 thorpej {
578 1.2 thorpej struct pcic_handle *h = (struct pcic_handle *) pch;
579 1.2 thorpej bus_space_handle_t memh;
580 1.2 thorpej bus_addr_t addr;
581 1.2 thorpej bus_size_t sizepg;
582 1.2 thorpej int i, mask, mhandle;
583 1.2 thorpej
584 1.2 thorpej /* out of sc->memh, allocate as many pages as necessary */
585 1.2 thorpej
586 1.2 thorpej /* convert size to PCIC pages */
587 1.2 thorpej sizepg = (size + (PCIC_MEM_ALIGN - 1)) / PCIC_MEM_ALIGN;
588 1.2 thorpej
589 1.2 thorpej mask = (1 << sizepg) - 1;
590 1.2 thorpej
591 1.2 thorpej addr = 0; /* XXX gcc -Wuninitialized */
592 1.2 thorpej mhandle = 0; /* XXX gcc -Wuninitialized */
593 1.2 thorpej
594 1.2 thorpej for (i = 0; i < (PCIC_MEM_PAGES + 1 - sizepg); i++) {
595 1.2 thorpej if ((h->sc->subregionmask & (mask << i)) == (mask << i)) {
596 1.2 thorpej if (bus_space_subregion(h->sc->memt, h->sc->memh,
597 1.2 thorpej i * PCIC_MEM_PAGESIZE,
598 1.2 thorpej sizepg * PCIC_MEM_PAGESIZE, &memh))
599 1.2 thorpej return (1);
600 1.2 thorpej mhandle = mask << i;
601 1.2 thorpej addr = h->sc->membase + (i * PCIC_MEM_PAGESIZE);
602 1.2 thorpej h->sc->subregionmask &= ~(mhandle);
603 1.2 thorpej break;
604 1.2 thorpej }
605 1.2 thorpej }
606 1.2 thorpej
607 1.2 thorpej if (i == (PCIC_MEM_PAGES + 1 - size))
608 1.2 thorpej return (1);
609 1.2 thorpej
610 1.2 thorpej DPRINTF(("pcic_chip_mem_alloc bus addr 0x%lx+0x%lx\n", (u_long) addr,
611 1.2 thorpej (u_long) size));
612 1.2 thorpej
613 1.2 thorpej pcmhp->memt = h->sc->memt;
614 1.2 thorpej pcmhp->memh = memh;
615 1.2 thorpej pcmhp->addr = addr;
616 1.2 thorpej pcmhp->size = size;
617 1.2 thorpej pcmhp->mhandle = mhandle;
618 1.2 thorpej pcmhp->realsize = sizepg * PCIC_MEM_PAGESIZE;
619 1.2 thorpej
620 1.2 thorpej return (0);
621 1.2 thorpej }
622 1.2 thorpej
623 1.2 thorpej void
624 1.2 thorpej pcic_chip_mem_free(pch, pcmhp)
625 1.2 thorpej pcmcia_chipset_handle_t pch;
626 1.2 thorpej struct pcmcia_mem_handle *pcmhp;
627 1.2 thorpej {
628 1.2 thorpej struct pcic_handle *h = (struct pcic_handle *) pch;
629 1.2 thorpej
630 1.2 thorpej h->sc->subregionmask |= pcmhp->mhandle;
631 1.2 thorpej }
632 1.2 thorpej
633 1.2 thorpej static struct mem_map_index_st {
634 1.2 thorpej int sysmem_start_lsb;
635 1.2 thorpej int sysmem_start_msb;
636 1.2 thorpej int sysmem_stop_lsb;
637 1.2 thorpej int sysmem_stop_msb;
638 1.2 thorpej int cardmem_lsb;
639 1.2 thorpej int cardmem_msb;
640 1.2 thorpej int memenable;
641 1.2 thorpej } mem_map_index[] = {
642 1.2 thorpej {
643 1.2 thorpej PCIC_SYSMEM_ADDR0_START_LSB,
644 1.2 thorpej PCIC_SYSMEM_ADDR0_START_MSB,
645 1.2 thorpej PCIC_SYSMEM_ADDR0_STOP_LSB,
646 1.2 thorpej PCIC_SYSMEM_ADDR0_STOP_MSB,
647 1.2 thorpej PCIC_CARDMEM_ADDR0_LSB,
648 1.2 thorpej PCIC_CARDMEM_ADDR0_MSB,
649 1.2 thorpej PCIC_ADDRWIN_ENABLE_MEM0,
650 1.2 thorpej },
651 1.2 thorpej {
652 1.2 thorpej PCIC_SYSMEM_ADDR1_START_LSB,
653 1.2 thorpej PCIC_SYSMEM_ADDR1_START_MSB,
654 1.2 thorpej PCIC_SYSMEM_ADDR1_STOP_LSB,
655 1.2 thorpej PCIC_SYSMEM_ADDR1_STOP_MSB,
656 1.2 thorpej PCIC_CARDMEM_ADDR1_LSB,
657 1.2 thorpej PCIC_CARDMEM_ADDR1_MSB,
658 1.2 thorpej PCIC_ADDRWIN_ENABLE_MEM1,
659 1.2 thorpej },
660 1.2 thorpej {
661 1.2 thorpej PCIC_SYSMEM_ADDR2_START_LSB,
662 1.2 thorpej PCIC_SYSMEM_ADDR2_START_MSB,
663 1.2 thorpej PCIC_SYSMEM_ADDR2_STOP_LSB,
664 1.2 thorpej PCIC_SYSMEM_ADDR2_STOP_MSB,
665 1.2 thorpej PCIC_CARDMEM_ADDR2_LSB,
666 1.2 thorpej PCIC_CARDMEM_ADDR2_MSB,
667 1.2 thorpej PCIC_ADDRWIN_ENABLE_MEM2,
668 1.2 thorpej },
669 1.2 thorpej {
670 1.2 thorpej PCIC_SYSMEM_ADDR3_START_LSB,
671 1.2 thorpej PCIC_SYSMEM_ADDR3_START_MSB,
672 1.2 thorpej PCIC_SYSMEM_ADDR3_STOP_LSB,
673 1.2 thorpej PCIC_SYSMEM_ADDR3_STOP_MSB,
674 1.2 thorpej PCIC_CARDMEM_ADDR3_LSB,
675 1.2 thorpej PCIC_CARDMEM_ADDR3_MSB,
676 1.2 thorpej PCIC_ADDRWIN_ENABLE_MEM3,
677 1.2 thorpej },
678 1.2 thorpej {
679 1.2 thorpej PCIC_SYSMEM_ADDR4_START_LSB,
680 1.2 thorpej PCIC_SYSMEM_ADDR4_START_MSB,
681 1.2 thorpej PCIC_SYSMEM_ADDR4_STOP_LSB,
682 1.2 thorpej PCIC_SYSMEM_ADDR4_STOP_MSB,
683 1.2 thorpej PCIC_CARDMEM_ADDR4_LSB,
684 1.2 thorpej PCIC_CARDMEM_ADDR4_MSB,
685 1.2 thorpej PCIC_ADDRWIN_ENABLE_MEM4,
686 1.2 thorpej },
687 1.2 thorpej };
688 1.2 thorpej
689 1.2 thorpej void
690 1.2 thorpej pcic_chip_do_mem_map(h, win)
691 1.2 thorpej struct pcic_handle *h;
692 1.2 thorpej int win;
693 1.2 thorpej {
694 1.2 thorpej int reg;
695 1.2 thorpej
696 1.2 thorpej pcic_write(h, mem_map_index[win].sysmem_start_lsb,
697 1.2 thorpej (h->mem[win].addr >> PCIC_SYSMEM_ADDRX_SHIFT) & 0xff);
698 1.2 thorpej pcic_write(h, mem_map_index[win].sysmem_start_msb,
699 1.2 thorpej ((h->mem[win].addr >> (PCIC_SYSMEM_ADDRX_SHIFT + 8)) &
700 1.2 thorpej PCIC_SYSMEM_ADDRX_START_MSB_ADDR_MASK));
701 1.2 thorpej
702 1.2 thorpej #if 0
703 1.2 thorpej /* XXX do I want 16 bit all the time? */
704 1.2 thorpej PCIC_SYSMEM_ADDRX_START_MSB_DATASIZE_16BIT;
705 1.2 thorpej #endif
706 1.2 thorpej
707 1.2 thorpej pcic_write(h, mem_map_index[win].sysmem_stop_lsb,
708 1.2 thorpej ((h->mem[win].addr + h->mem[win].size) >>
709 1.2 thorpej PCIC_SYSMEM_ADDRX_SHIFT) & 0xff);
710 1.2 thorpej pcic_write(h, mem_map_index[win].sysmem_stop_msb,
711 1.2 thorpej (((h->mem[win].addr + h->mem[win].size) >>
712 1.2 thorpej (PCIC_SYSMEM_ADDRX_SHIFT + 8)) &
713 1.2 thorpej PCIC_SYSMEM_ADDRX_STOP_MSB_ADDR_MASK) |
714 1.2 thorpej PCIC_SYSMEM_ADDRX_STOP_MSB_WAIT2);
715 1.2 thorpej
716 1.2 thorpej pcic_write(h, mem_map_index[win].cardmem_lsb,
717 1.2 thorpej (h->mem[win].offset >> PCIC_CARDMEM_ADDRX_SHIFT) & 0xff);
718 1.2 thorpej pcic_write(h, mem_map_index[win].cardmem_msb,
719 1.2 thorpej ((h->mem[win].offset >> (PCIC_CARDMEM_ADDRX_SHIFT + 8)) &
720 1.2 thorpej PCIC_CARDMEM_ADDRX_MSB_ADDR_MASK) |
721 1.2 thorpej ((h->mem[win].kind == PCMCIA_MEM_ATTR) ?
722 1.2 thorpej PCIC_CARDMEM_ADDRX_MSB_REGACTIVE_ATTR : 0));
723 1.2 thorpej
724 1.2 thorpej reg = pcic_read(h, PCIC_ADDRWIN_ENABLE);
725 1.2 thorpej reg |= (mem_map_index[win].memenable | PCIC_ADDRWIN_ENABLE_MEMCS16);
726 1.2 thorpej pcic_write(h, PCIC_ADDRWIN_ENABLE, reg);
727 1.2 thorpej
728 1.2 thorpej #ifdef PCICDEBUG
729 1.2 thorpej {
730 1.2 thorpej int r1, r2, r3, r4, r5, r6;
731 1.2 thorpej
732 1.2 thorpej r1 = pcic_read(h, mem_map_index[win].sysmem_start_msb);
733 1.2 thorpej r2 = pcic_read(h, mem_map_index[win].sysmem_start_lsb);
734 1.2 thorpej r3 = pcic_read(h, mem_map_index[win].sysmem_stop_msb);
735 1.2 thorpej r4 = pcic_read(h, mem_map_index[win].sysmem_stop_lsb);
736 1.2 thorpej r5 = pcic_read(h, mem_map_index[win].cardmem_msb);
737 1.2 thorpej r6 = pcic_read(h, mem_map_index[win].cardmem_lsb);
738 1.2 thorpej
739 1.2 thorpej DPRINTF(("pcic_chip_do_mem_map window %d: %02x%02x %02x%02x "
740 1.2 thorpej "%02x%02x\n", win, r1, r2, r3, r4, r5, r6));
741 1.2 thorpej }
742 1.2 thorpej #endif
743 1.2 thorpej }
744 1.2 thorpej
745 1.2 thorpej int
746 1.2 thorpej pcic_chip_mem_map(pch, kind, card_addr, size, pcmhp, offsetp, windowp)
747 1.2 thorpej pcmcia_chipset_handle_t pch;
748 1.2 thorpej int kind;
749 1.2 thorpej bus_addr_t card_addr;
750 1.2 thorpej bus_size_t size;
751 1.2 thorpej struct pcmcia_mem_handle *pcmhp;
752 1.2 thorpej bus_addr_t *offsetp;
753 1.2 thorpej int *windowp;
754 1.2 thorpej {
755 1.2 thorpej struct pcic_handle *h = (struct pcic_handle *) pch;
756 1.2 thorpej bus_addr_t busaddr;
757 1.2 thorpej long card_offset;
758 1.2 thorpej int i, win;
759 1.2 thorpej
760 1.2 thorpej win = -1;
761 1.2 thorpej for (i = 0; i < (sizeof(mem_map_index) / sizeof(mem_map_index[0]));
762 1.2 thorpej i++) {
763 1.2 thorpej if ((h->memalloc & (1 << i)) == 0) {
764 1.2 thorpej win = i;
765 1.2 thorpej h->memalloc |= (1 << i);
766 1.2 thorpej break;
767 1.2 thorpej }
768 1.2 thorpej }
769 1.2 thorpej
770 1.2 thorpej if (win == -1)
771 1.2 thorpej return (1);
772 1.2 thorpej
773 1.2 thorpej *windowp = win;
774 1.2 thorpej
775 1.2 thorpej /* XXX this is pretty gross */
776 1.2 thorpej
777 1.2 thorpej if (h->sc->memt != pcmhp->memt)
778 1.2 thorpej panic("pcic_chip_mem_map memt is bogus");
779 1.2 thorpej
780 1.2 thorpej busaddr = pcmhp->addr;
781 1.2 thorpej
782 1.2 thorpej /*
783 1.2 thorpej * compute the address offset to the pcmcia address space for the
784 1.2 thorpej * pcic. this is intentionally signed. The masks and shifts below
785 1.2 thorpej * will cause TRT to happen in the pcic registers. Deal with making
786 1.2 thorpej * sure the address is aligned, and return the alignment offset.
787 1.2 thorpej */
788 1.2 thorpej
789 1.2 thorpej *offsetp = card_addr % PCIC_MEM_ALIGN;
790 1.2 thorpej card_addr -= *offsetp;
791 1.2 thorpej
792 1.2 thorpej DPRINTF(("pcic_chip_mem_map window %d bus %lx+%lx+%lx at card addr "
793 1.2 thorpej "%lx\n", win, (u_long) busaddr, (u_long) * offsetp, (u_long) size,
794 1.2 thorpej (u_long) card_addr));
795 1.2 thorpej
796 1.2 thorpej /*
797 1.2 thorpej * include the offset in the size, and decrement size by one, since
798 1.2 thorpej * the hw wants start/stop
799 1.2 thorpej */
800 1.2 thorpej size += *offsetp - 1;
801 1.2 thorpej
802 1.2 thorpej card_offset = (((long) card_addr) - ((long) busaddr));
803 1.2 thorpej
804 1.2 thorpej h->mem[win].addr = busaddr;
805 1.2 thorpej h->mem[win].size = size;
806 1.2 thorpej h->mem[win].offset = card_offset;
807 1.2 thorpej h->mem[win].kind = kind;
808 1.2 thorpej
809 1.2 thorpej pcic_chip_do_mem_map(h, win);
810 1.2 thorpej
811 1.2 thorpej return (0);
812 1.2 thorpej }
813 1.2 thorpej
814 1.2 thorpej void
815 1.2 thorpej pcic_chip_mem_unmap(pch, window)
816 1.2 thorpej pcmcia_chipset_handle_t pch;
817 1.2 thorpej int window;
818 1.2 thorpej {
819 1.2 thorpej struct pcic_handle *h = (struct pcic_handle *) pch;
820 1.2 thorpej int reg;
821 1.2 thorpej
822 1.2 thorpej if (window >= (sizeof(mem_map_index) / sizeof(mem_map_index[0])))
823 1.2 thorpej panic("pcic_chip_mem_unmap: window out of range");
824 1.2 thorpej
825 1.2 thorpej reg = pcic_read(h, PCIC_ADDRWIN_ENABLE);
826 1.2 thorpej reg &= ~mem_map_index[window].memenable;
827 1.2 thorpej pcic_write(h, PCIC_ADDRWIN_ENABLE, reg);
828 1.2 thorpej
829 1.2 thorpej h->memalloc &= ~(1 << window);
830 1.2 thorpej }
831 1.2 thorpej
832 1.2 thorpej int
833 1.2 thorpej pcic_chip_io_alloc(pch, start, size, align, pcihp)
834 1.2 thorpej pcmcia_chipset_handle_t pch;
835 1.2 thorpej bus_addr_t start;
836 1.2 thorpej bus_size_t size;
837 1.2 thorpej bus_size_t align;
838 1.2 thorpej struct pcmcia_io_handle *pcihp;
839 1.2 thorpej {
840 1.2 thorpej struct pcic_handle *h = (struct pcic_handle *) pch;
841 1.2 thorpej bus_space_tag_t iot;
842 1.2 thorpej bus_space_handle_t ioh;
843 1.2 thorpej bus_addr_t ioaddr;
844 1.2 thorpej int flags = 0;
845 1.2 thorpej
846 1.2 thorpej /*
847 1.2 thorpej * Allocate some arbitrary I/O space.
848 1.2 thorpej */
849 1.2 thorpej
850 1.2 thorpej iot = h->sc->iot;
851 1.2 thorpej
852 1.2 thorpej if (start) {
853 1.2 thorpej ioaddr = start;
854 1.2 thorpej if (bus_space_map(iot, start, size, 0, &ioh))
855 1.2 thorpej return (1);
856 1.2 thorpej DPRINTF(("pcic_chip_io_alloc map port %lx+%lx\n",
857 1.2 thorpej (u_long) ioaddr, (u_long) size));
858 1.2 thorpej } else {
859 1.2 thorpej flags |= PCMCIA_IO_ALLOCATED;
860 1.2 thorpej if (bus_space_alloc(iot, h->sc->iobase,
861 1.2 thorpej h->sc->iobase + h->sc->iosize, size, align, 0, 0,
862 1.2 thorpej &ioaddr, &ioh))
863 1.2 thorpej return (1);
864 1.2 thorpej DPRINTF(("pcic_chip_io_alloc alloc port %lx+%lx\n",
865 1.2 thorpej (u_long) ioaddr, (u_long) size));
866 1.2 thorpej }
867 1.2 thorpej
868 1.2 thorpej pcihp->iot = iot;
869 1.2 thorpej pcihp->ioh = ioh;
870 1.2 thorpej pcihp->addr = ioaddr;
871 1.2 thorpej pcihp->size = size;
872 1.2 thorpej pcihp->flags = flags;
873 1.2 thorpej
874 1.2 thorpej return (0);
875 1.2 thorpej }
876 1.2 thorpej
877 1.2 thorpej void
878 1.2 thorpej pcic_chip_io_free(pch, pcihp)
879 1.2 thorpej pcmcia_chipset_handle_t pch;
880 1.2 thorpej struct pcmcia_io_handle *pcihp;
881 1.2 thorpej {
882 1.2 thorpej bus_space_tag_t iot = pcihp->iot;
883 1.2 thorpej bus_space_handle_t ioh = pcihp->ioh;
884 1.2 thorpej bus_size_t size = pcihp->size;
885 1.2 thorpej
886 1.2 thorpej if (pcihp->flags & PCMCIA_IO_ALLOCATED)
887 1.2 thorpej bus_space_free(iot, ioh, size);
888 1.2 thorpej else
889 1.2 thorpej bus_space_unmap(iot, ioh, size);
890 1.2 thorpej }
891 1.2 thorpej
892 1.2 thorpej
893 1.2 thorpej static struct io_map_index_st {
894 1.2 thorpej int start_lsb;
895 1.2 thorpej int start_msb;
896 1.2 thorpej int stop_lsb;
897 1.2 thorpej int stop_msb;
898 1.2 thorpej int ioenable;
899 1.2 thorpej int ioctlmask;
900 1.2 thorpej int ioctlbits[3]; /* indexed by PCMCIA_WIDTH_* */
901 1.2 thorpej } io_map_index[] = {
902 1.2 thorpej {
903 1.2 thorpej PCIC_IOADDR0_START_LSB,
904 1.2 thorpej PCIC_IOADDR0_START_MSB,
905 1.2 thorpej PCIC_IOADDR0_STOP_LSB,
906 1.2 thorpej PCIC_IOADDR0_STOP_MSB,
907 1.2 thorpej PCIC_ADDRWIN_ENABLE_IO0,
908 1.2 thorpej PCIC_IOCTL_IO0_WAITSTATE | PCIC_IOCTL_IO0_ZEROWAIT |
909 1.2 thorpej PCIC_IOCTL_IO0_IOCS16SRC_MASK | PCIC_IOCTL_IO0_DATASIZE_MASK,
910 1.2 thorpej {
911 1.2 thorpej PCIC_IOCTL_IO0_IOCS16SRC_CARD,
912 1.6 enami PCIC_IOCTL_IO0_IOCS16SRC_DATASIZE |
913 1.6 enami PCIC_IOCTL_IO0_DATASIZE_8BIT,
914 1.6 enami PCIC_IOCTL_IO0_IOCS16SRC_DATASIZE |
915 1.6 enami PCIC_IOCTL_IO0_DATASIZE_16BIT,
916 1.2 thorpej },
917 1.2 thorpej },
918 1.2 thorpej {
919 1.2 thorpej PCIC_IOADDR1_START_LSB,
920 1.2 thorpej PCIC_IOADDR1_START_MSB,
921 1.2 thorpej PCIC_IOADDR1_STOP_LSB,
922 1.2 thorpej PCIC_IOADDR1_STOP_MSB,
923 1.2 thorpej PCIC_ADDRWIN_ENABLE_IO1,
924 1.2 thorpej PCIC_IOCTL_IO1_WAITSTATE | PCIC_IOCTL_IO1_ZEROWAIT |
925 1.2 thorpej PCIC_IOCTL_IO1_IOCS16SRC_MASK | PCIC_IOCTL_IO1_DATASIZE_MASK,
926 1.2 thorpej {
927 1.2 thorpej PCIC_IOCTL_IO1_IOCS16SRC_CARD,
928 1.2 thorpej PCIC_IOCTL_IO1_IOCS16SRC_DATASIZE |
929 1.2 thorpej PCIC_IOCTL_IO1_DATASIZE_8BIT,
930 1.2 thorpej PCIC_IOCTL_IO1_IOCS16SRC_DATASIZE |
931 1.2 thorpej PCIC_IOCTL_IO1_DATASIZE_16BIT,
932 1.2 thorpej },
933 1.2 thorpej },
934 1.2 thorpej };
935 1.2 thorpej
936 1.2 thorpej void
937 1.2 thorpej pcic_chip_do_io_map(h, win)
938 1.2 thorpej struct pcic_handle *h;
939 1.2 thorpej int win;
940 1.2 thorpej {
941 1.2 thorpej int reg;
942 1.2 thorpej
943 1.2 thorpej DPRINTF(("pcic_chip_do_io_map win %d addr %lx size %lx width %d\n",
944 1.2 thorpej win, (long) h->io[win].addr, (long) h->io[win].size,
945 1.2 thorpej h->io[win].width * 8));
946 1.2 thorpej
947 1.2 thorpej pcic_write(h, io_map_index[win].start_lsb, h->io[win].addr & 0xff);
948 1.2 thorpej pcic_write(h, io_map_index[win].start_msb,
949 1.2 thorpej (h->io[win].addr >> 8) & 0xff);
950 1.2 thorpej
951 1.2 thorpej pcic_write(h, io_map_index[win].stop_lsb,
952 1.2 thorpej (h->io[win].addr + h->io[win].size - 1) & 0xff);
953 1.2 thorpej pcic_write(h, io_map_index[win].stop_msb,
954 1.2 thorpej ((h->io[win].addr + h->io[win].size - 1) >> 8) & 0xff);
955 1.2 thorpej
956 1.2 thorpej reg = pcic_read(h, PCIC_IOCTL);
957 1.2 thorpej reg &= ~io_map_index[win].ioctlmask;
958 1.2 thorpej reg |= io_map_index[win].ioctlbits[h->io[win].width];
959 1.2 thorpej pcic_write(h, PCIC_IOCTL, reg);
960 1.2 thorpej
961 1.2 thorpej reg = pcic_read(h, PCIC_ADDRWIN_ENABLE);
962 1.2 thorpej reg |= io_map_index[win].ioenable;
963 1.2 thorpej pcic_write(h, PCIC_ADDRWIN_ENABLE, reg);
964 1.2 thorpej }
965 1.2 thorpej
966 1.2 thorpej int
967 1.2 thorpej pcic_chip_io_map(pch, width, offset, size, pcihp, windowp)
968 1.2 thorpej pcmcia_chipset_handle_t pch;
969 1.2 thorpej int width;
970 1.2 thorpej bus_addr_t offset;
971 1.2 thorpej bus_size_t size;
972 1.2 thorpej struct pcmcia_io_handle *pcihp;
973 1.2 thorpej int *windowp;
974 1.2 thorpej {
975 1.2 thorpej struct pcic_handle *h = (struct pcic_handle *) pch;
976 1.2 thorpej bus_addr_t ioaddr = pcihp->addr + offset;
977 1.4 enami int i, win;
978 1.4 enami #ifdef PCICDEBUG
979 1.2 thorpej static char *width_names[] = { "auto", "io8", "io16" };
980 1.4 enami #endif
981 1.2 thorpej
982 1.2 thorpej /* XXX Sanity check offset/size. */
983 1.2 thorpej
984 1.2 thorpej win = -1;
985 1.2 thorpej for (i = 0; i < (sizeof(io_map_index) / sizeof(io_map_index[0])); i++) {
986 1.2 thorpej if ((h->ioalloc & (1 << i)) == 0) {
987 1.2 thorpej win = i;
988 1.2 thorpej h->ioalloc |= (1 << i);
989 1.2 thorpej break;
990 1.2 thorpej }
991 1.2 thorpej }
992 1.2 thorpej
993 1.2 thorpej if (win == -1)
994 1.2 thorpej return (1);
995 1.2 thorpej
996 1.2 thorpej *windowp = win;
997 1.2 thorpej
998 1.2 thorpej /* XXX this is pretty gross */
999 1.2 thorpej
1000 1.2 thorpej if (h->sc->iot != pcihp->iot)
1001 1.2 thorpej panic("pcic_chip_io_map iot is bogus");
1002 1.2 thorpej
1003 1.2 thorpej DPRINTF(("pcic_chip_io_map window %d %s port %lx+%lx\n",
1004 1.2 thorpej win, width_names[width], (u_long) ioaddr, (u_long) size));
1005 1.2 thorpej
1006 1.2 thorpej /* XXX wtf is this doing here? */
1007 1.2 thorpej
1008 1.2 thorpej printf(" port 0x%lx", (u_long) ioaddr);
1009 1.2 thorpej if (size > 1)
1010 1.2 thorpej printf("-0x%lx", (u_long) ioaddr + (u_long) size - 1);
1011 1.2 thorpej
1012 1.2 thorpej h->io[win].addr = ioaddr;
1013 1.2 thorpej h->io[win].size = size;
1014 1.2 thorpej h->io[win].width = width;
1015 1.2 thorpej
1016 1.2 thorpej pcic_chip_do_io_map(h, win);
1017 1.2 thorpej
1018 1.2 thorpej return (0);
1019 1.2 thorpej }
1020 1.2 thorpej
1021 1.2 thorpej void
1022 1.2 thorpej pcic_chip_io_unmap(pch, window)
1023 1.2 thorpej pcmcia_chipset_handle_t pch;
1024 1.2 thorpej int window;
1025 1.2 thorpej {
1026 1.2 thorpej struct pcic_handle *h = (struct pcic_handle *) pch;
1027 1.2 thorpej int reg;
1028 1.2 thorpej
1029 1.2 thorpej if (window >= (sizeof(io_map_index) / sizeof(io_map_index[0])))
1030 1.2 thorpej panic("pcic_chip_io_unmap: window out of range");
1031 1.2 thorpej
1032 1.2 thorpej reg = pcic_read(h, PCIC_ADDRWIN_ENABLE);
1033 1.2 thorpej reg &= ~io_map_index[window].ioenable;
1034 1.2 thorpej pcic_write(h, PCIC_ADDRWIN_ENABLE, reg);
1035 1.2 thorpej
1036 1.2 thorpej h->ioalloc &= ~(1 << window);
1037 1.2 thorpej }
1038 1.2 thorpej
1039 1.2 thorpej void
1040 1.2 thorpej pcic_chip_socket_enable(pch)
1041 1.2 thorpej pcmcia_chipset_handle_t pch;
1042 1.2 thorpej {
1043 1.2 thorpej struct pcic_handle *h = (struct pcic_handle *) pch;
1044 1.2 thorpej int cardtype, reg, win;
1045 1.2 thorpej
1046 1.2 thorpej /* this bit is mostly stolen from pcic_attach_card */
1047 1.2 thorpej
1048 1.2 thorpej /* power down the socket to reset it, clear the card reset pin */
1049 1.2 thorpej
1050 1.2 thorpej pcic_write(h, PCIC_PWRCTL, 0);
1051 1.2 thorpej
1052 1.2 thorpej /* power up the socket */
1053 1.2 thorpej
1054 1.2 thorpej pcic_write(h, PCIC_PWRCTL, PCIC_PWRCTL_PWR_ENABLE);
1055 1.2 thorpej delay(10000);
1056 1.2 thorpej pcic_write(h, PCIC_PWRCTL, PCIC_PWRCTL_PWR_ENABLE | PCIC_PWRCTL_OE);
1057 1.2 thorpej
1058 1.2 thorpej /* clear the reset flag */
1059 1.2 thorpej
1060 1.2 thorpej pcic_write(h, PCIC_INTR, PCIC_INTR_RESET);
1061 1.2 thorpej
1062 1.2 thorpej /* wait 20ms as per pc card standard (r2.01) section 4.3.6 */
1063 1.2 thorpej
1064 1.2 thorpej delay(20000);
1065 1.2 thorpej
1066 1.2 thorpej /* wait for the chip to finish initializing */
1067 1.2 thorpej
1068 1.2 thorpej pcic_wait_ready(h);
1069 1.2 thorpej
1070 1.2 thorpej /* zero out the address windows */
1071 1.2 thorpej
1072 1.2 thorpej pcic_write(h, PCIC_ADDRWIN_ENABLE, 0);
1073 1.2 thorpej
1074 1.2 thorpej /* set the card type */
1075 1.2 thorpej
1076 1.2 thorpej cardtype = pcmcia_card_gettype(h->pcmcia);
1077 1.2 thorpej
1078 1.2 thorpej reg = pcic_read(h, PCIC_INTR);
1079 1.2 thorpej reg &= ~PCIC_INTR_CARDTYPE_MASK;
1080 1.2 thorpej reg |= ((cardtype == PCMCIA_IFTYPE_IO) ?
1081 1.2 thorpej PCIC_INTR_CARDTYPE_IO :
1082 1.2 thorpej PCIC_INTR_CARDTYPE_MEM);
1083 1.2 thorpej reg |= h->ih_irq;
1084 1.2 thorpej pcic_write(h, PCIC_INTR, reg);
1085 1.2 thorpej
1086 1.2 thorpej DPRINTF(("%s: pcic_chip_socket_enable %02x cardtype %s %02x\n",
1087 1.2 thorpej h->sc->dev.dv_xname, h->sock,
1088 1.2 thorpej ((cardtype == PCMCIA_IFTYPE_IO) ? "io" : "mem"), reg));
1089 1.2 thorpej
1090 1.2 thorpej /* reinstall all the memory and io mappings */
1091 1.2 thorpej
1092 1.2 thorpej for (win = 0; win < PCIC_MEM_WINS; win++)
1093 1.2 thorpej if (h->memalloc & (1 << win))
1094 1.2 thorpej pcic_chip_do_mem_map(h, win);
1095 1.2 thorpej
1096 1.2 thorpej for (win = 0; win < PCIC_IO_WINS; win++)
1097 1.2 thorpej if (h->ioalloc & (1 << win))
1098 1.2 thorpej pcic_chip_do_io_map(h, win);
1099 1.2 thorpej }
1100 1.2 thorpej
1101 1.2 thorpej void
1102 1.2 thorpej pcic_chip_socket_disable(pch)
1103 1.2 thorpej pcmcia_chipset_handle_t pch;
1104 1.2 thorpej {
1105 1.2 thorpej struct pcic_handle *h = (struct pcic_handle *) pch;
1106 1.2 thorpej
1107 1.2 thorpej DPRINTF(("pcic_chip_socket_disable\n"));
1108 1.2 thorpej
1109 1.2 thorpej /* power down the socket */
1110 1.2 thorpej
1111 1.2 thorpej pcic_write(h, PCIC_PWRCTL, 0);
1112 1.2 thorpej }
1113