pcmcia.c revision 1.53 1 /* $NetBSD: pcmcia.c,v 1.53 2004/08/10 02:50:52 mycroft Exp $ */
2
3 /*
4 * Copyright (c) 2004 Charles M. Hannum. All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. All advertising materials mentioning features or use of this software
15 * must display the following acknowledgement:
16 * This product includes software developed by Charles M. Hannum.
17 * 4. The name of the author may not be used to endorse or promote products
18 * derived from this software without specific prior written permission.
19 */
20
21 /*
22 * Copyright (c) 1997 Marc Horowitz. All rights reserved.
23 *
24 * Redistribution and use in source and binary forms, with or without
25 * modification, are permitted provided that the following conditions
26 * are met:
27 * 1. Redistributions of source code must retain the above copyright
28 * notice, this list of conditions and the following disclaimer.
29 * 2. Redistributions in binary form must reproduce the above copyright
30 * notice, this list of conditions and the following disclaimer in the
31 * documentation and/or other materials provided with the distribution.
32 * 3. All advertising materials mentioning features or use of this software
33 * must display the following acknowledgement:
34 * This product includes software developed by Marc Horowitz.
35 * 4. The name of the author may not be used to endorse or promote products
36 * derived from this software without specific prior written permission.
37 *
38 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
39 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
40 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
41 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
42 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
43 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
44 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
45 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
46 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
47 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
48 */
49
50 #include <sys/cdefs.h>
51 __KERNEL_RCSID(0, "$NetBSD: pcmcia.c,v 1.53 2004/08/10 02:50:52 mycroft Exp $");
52
53 #include "opt_pcmciaverbose.h"
54
55 #include <sys/param.h>
56 #include <sys/systm.h>
57 #include <sys/device.h>
58
59 #include <dev/pcmcia/pcmciareg.h>
60 #include <dev/pcmcia/pcmciachip.h>
61 #include <dev/pcmcia/pcmciavar.h>
62 #ifdef IT8368E_LEGACY_MODE /* XXX -uch */
63 #include <arch/hpcmips/dev/it8368var.h>
64 #endif
65
66 #include "locators.h"
67
68 #ifdef PCMCIADEBUG
69 int pcmcia_debug = 0;
70 #define DPRINTF(arg) if (pcmcia_debug) printf arg
71 #else
72 #define DPRINTF(arg)
73 #endif
74
75 #ifdef PCMCIAVERBOSE
76 int pcmcia_verbose = 1;
77 #else
78 int pcmcia_verbose = 0;
79 #endif
80
81 int pcmcia_match __P((struct device *, struct cfdata *, void *));
82 int pcmcia_submatch __P((struct device *, struct cfdata *, void *));
83 void pcmcia_attach __P((struct device *, struct device *, void *));
84 int pcmcia_print __P((void *, const char *));
85
86 CFATTACH_DECL(pcmcia, sizeof(struct pcmcia_softc),
87 pcmcia_match, pcmcia_attach, NULL, NULL);
88
89 int
90 pcmcia_ccr_read(pf, ccr)
91 struct pcmcia_function *pf;
92 int ccr;
93 {
94
95 return (bus_space_read_1(pf->pf_ccrt, pf->pf_ccrh,
96 pf->pf_ccr_offset + ccr * 2));
97 }
98
99 void
100 pcmcia_ccr_write(pf, ccr, val)
101 struct pcmcia_function *pf;
102 int ccr;
103 int val;
104 {
105
106 if (pf->ccr_mask & (1 << ccr)) {
107 bus_space_write_1(pf->pf_ccrt, pf->pf_ccrh,
108 pf->pf_ccr_offset + ccr * 2, val);
109 }
110 }
111
112 int
113 pcmcia_match(parent, match, aux)
114 struct device *parent;
115 struct cfdata *match;
116 void *aux;
117 {
118 struct pcmciabus_attach_args *paa = aux;
119
120 if (strcmp(paa->paa_busname, match->cf_name)) {
121 return 0;
122 }
123 /* if the autoconfiguration got this far, there's a socket here */
124 return (1);
125 }
126
127 void
128 pcmcia_attach(parent, self, aux)
129 struct device *parent, *self;
130 void *aux;
131 {
132 struct pcmciabus_attach_args *paa = aux;
133 struct pcmcia_softc *sc = (struct pcmcia_softc *) self;
134
135 printf("\n");
136
137 sc->pct = paa->pct;
138 sc->pch = paa->pch;
139 sc->iobase = paa->iobase;
140 sc->iosize = paa->iosize;
141
142 sc->ih = NULL;
143 }
144
145 int
146 pcmcia_card_attach(dev)
147 struct device *dev;
148 {
149 struct pcmcia_softc *sc = (struct pcmcia_softc *) dev;
150 struct pcmcia_function *pf;
151 struct pcmcia_attach_args paa;
152 int attached;
153
154 /*
155 * this is here so that when socket_enable calls gettype, trt happens
156 */
157 SIMPLEQ_FIRST(&sc->card.pf_head) = NULL;
158
159 pcmcia_chip_socket_enable(sc->pct, sc->pch);
160
161 pcmcia_read_cis(sc);
162
163 pcmcia_chip_socket_disable(sc->pct, sc->pch);
164
165 pcmcia_check_cis_quirks(sc);
166
167 /*
168 * bail now if the card has no functions, or if there was an error in
169 * the cis.
170 */
171
172 if (sc->card.error)
173 return (1);
174 if (SIMPLEQ_EMPTY(&sc->card.pf_head))
175 return (1);
176
177 if (pcmcia_verbose)
178 pcmcia_print_cis(sc);
179
180 attached = 0;
181
182 SIMPLEQ_FOREACH(pf, &sc->card.pf_head, pf_list) {
183 if (SIMPLEQ_EMPTY(&pf->cfe_head))
184 continue;
185
186 #ifdef DIAGNOSTIC
187 if (pf->child != NULL) {
188 printf("%s: %s still attached to function %d!\n",
189 sc->dev.dv_xname, pf->child->dv_xname,
190 pf->number);
191 panic("pcmcia_card_attach");
192 }
193 #endif
194 pf->sc = sc;
195 pf->child = NULL;
196 pf->cfe = NULL;
197 pf->pf_ih = NULL;
198 }
199
200 SIMPLEQ_FOREACH(pf, &sc->card.pf_head, pf_list) {
201 if (SIMPLEQ_EMPTY(&pf->cfe_head))
202 continue;
203
204 paa.manufacturer = sc->card.manufacturer;
205 paa.product = sc->card.product;
206 paa.card = &sc->card;
207 paa.pf = pf;
208
209 if ((pf->child = config_found_sm(&sc->dev, &paa, pcmcia_print,
210 pcmcia_submatch)) != NULL)
211 attached++;
212 }
213
214 return (attached ? 0 : 1);
215 }
216
217 void
218 pcmcia_card_detach(dev, flags)
219 struct device *dev;
220 int flags; /* DETACH_* flags */
221 {
222 struct pcmcia_softc *sc = (struct pcmcia_softc *) dev;
223 struct pcmcia_function *pf;
224 int error;
225
226 /*
227 * We are running on either the PCMCIA socket's event thread
228 * or in user context detaching a device by user request.
229 */
230 SIMPLEQ_FOREACH(pf, &sc->card.pf_head, pf_list) {
231 if (SIMPLEQ_EMPTY(&pf->cfe_head))
232 continue;
233 if (pf->child == NULL)
234 continue;
235 DPRINTF(("%s: detaching %s (function %d)\n",
236 sc->dev.dv_xname, pf->child->dv_xname, pf->number));
237 if ((error = config_detach(pf->child, flags)) != 0) {
238 printf("%s: error %d detaching %s (function %d)\n",
239 sc->dev.dv_xname, error, pf->child->dv_xname,
240 pf->number);
241 } else
242 pf->child = NULL;
243 }
244
245 if (sc->sc_enabled_count != 0) {
246 #ifdef DIAGNOSTIC
247 printf("pcmcia_card_detach: enabled_count should be 0 here??\n");
248 #endif
249 pcmcia_chip_socket_disable(sc->pct, sc->pch);
250 sc->sc_enabled_count = 0;
251 }
252 }
253
254 void
255 pcmcia_card_deactivate(dev)
256 struct device *dev;
257 {
258 struct pcmcia_softc *sc = (struct pcmcia_softc *) dev;
259 struct pcmcia_function *pf;
260
261 /*
262 * We're in the chip's card removal interrupt handler.
263 * Deactivate the child driver. The PCMCIA socket's
264 * event thread will run later to finish the detach.
265 */
266 SIMPLEQ_FOREACH(pf, &sc->card.pf_head, pf_list) {
267 if (SIMPLEQ_EMPTY(&pf->cfe_head))
268 continue;
269 if (pf->child == NULL)
270 continue;
271 DPRINTF(("%s: deactivating %s (function %d)\n",
272 sc->dev.dv_xname, pf->child->dv_xname, pf->number));
273 config_deactivate(pf->child);
274 }
275 }
276
277 int
278 pcmcia_submatch(parent, cf, aux)
279 struct device *parent;
280 struct cfdata *cf;
281 void *aux;
282 {
283 struct pcmcia_attach_args *paa = aux;
284
285 if (cf->cf_loc[PCMCIACF_FUNCTION] != PCMCIACF_FUNCTION_DEFAULT &&
286 cf->cf_loc[PCMCIACF_FUNCTION] != paa->pf->number)
287 return (0);
288
289 return (config_match(parent, cf, aux));
290 }
291
292 int
293 pcmcia_print(arg, pnp)
294 void *arg;
295 const char *pnp;
296 {
297 struct pcmcia_attach_args *pa = arg;
298 struct pcmcia_softc *sc = pa->pf->sc;
299 struct pcmcia_card *card = &sc->card;
300 char devinfo[256];
301
302 if (pnp)
303 aprint_normal("%s", pnp);
304
305 pcmcia_devinfo(card, !!pnp, devinfo, sizeof(devinfo));
306
307 aprint_normal(" function %d: %s", pa->pf->number, devinfo);
308
309 return (UNCONF);
310 }
311
312 void
313 pcmcia_devinfo(card, showhex, cp, cplen)
314 struct pcmcia_card *card;
315 int showhex;
316 char *cp;
317 size_t cplen;
318 {
319 int i, n;
320
321 if (cplen > 1) {
322 *cp++ = '<';
323 *cp = '\0';
324 cplen--;
325 }
326
327 for (i = 0; i < 4 && card->cis1_info[i] != NULL && cplen > 1; i++) {
328 n = snprintf(cp, cplen, "%s%s", i ? ", " : "",
329 card->cis1_info[i]);
330 cp += n;
331 if (cplen < n)
332 return;
333 cplen -= n;
334 }
335
336 if (cplen > 1) {
337 *cp++ = '>';
338 *cp = '\0';
339 cplen--;
340 }
341
342 if (showhex && cplen > 1)
343 snprintf(cp, cplen, " (manufacturer 0x%04x, product 0x%04x)",
344 card->manufacturer, card->product);
345 }
346
347 const struct pcmcia_product *
348 pcmcia_product_lookup(pa, tab, ent_size, matchfn)
349 struct pcmcia_attach_args *pa;
350 const struct pcmcia_product *tab;
351 size_t ent_size;
352 pcmcia_product_match_fn matchfn;
353 {
354 const struct pcmcia_product *ent;
355 int matches;
356
357 #ifdef DIAGNOSTIC
358 if (sizeof *ent > ent_size)
359 panic("pcmcia_product_lookup: bogus ent_size %ld",
360 (long) ent_size);
361 #endif
362
363 for (ent = tab;
364 ent->pp_name != NULL;
365 ent = (const struct pcmcia_product *)
366 ((const char *)ent + ent_size)) {
367
368 /* see if it matches vendor/product/function */
369 matches = (pa->manufacturer == ent->pp_vendor) &&
370 (pa->product == ent->pp_product) &&
371 (pa->pf->number == ent->pp_expfunc);
372
373 /* if a separate match function is given, let it override */
374 if (matchfn != NULL)
375 matches = (*matchfn)(pa, ent, matches);
376
377 if (matches)
378 return (ent);
379 }
380 return (NULL);
381 }
382
383 int
384 pcmcia_card_gettype(dev)
385 struct device *dev;
386 {
387 struct pcmcia_softc *sc = (struct pcmcia_softc *)dev;
388 struct pcmcia_function *pf;
389
390 /*
391 * set the iftype to memory if this card has no functions (not yet
392 * probed), or only one function, and that is not initialized yet or
393 * that is memory.
394 */
395 pf = SIMPLEQ_FIRST(&sc->card.pf_head);
396 if (pf == NULL ||
397 (SIMPLEQ_NEXT(pf, pf_list) == NULL &&
398 (pf->cfe == NULL || pf->cfe->iftype == PCMCIA_IFTYPE_MEMORY)))
399 return (PCMCIA_IFTYPE_MEMORY);
400 else
401 return (PCMCIA_IFTYPE_IO);
402 }
403
404 /*
405 * Initialize a PCMCIA function. May be called as long as the function is
406 * disabled.
407 */
408 void
409 pcmcia_function_init(pf, cfe)
410 struct pcmcia_function *pf;
411 struct pcmcia_config_entry *cfe;
412 {
413 if (pf->pf_flags & PFF_ENABLED)
414 panic("pcmcia_function_init: function is enabled");
415
416 /* Remember which configuration entry we are using. */
417 pf->cfe = cfe;
418 }
419
420 void pcmcia_socket_enable(dev)
421 struct device *dev;
422 {
423 struct pcmcia_softc *sc = (void *)dev;
424
425 pcmcia_chip_socket_enable(sc->pct, sc->pch);
426 }
427
428 void pcmcia_socket_disable(dev)
429 struct device *dev;
430 {
431 struct pcmcia_softc *sc = (void *)dev;
432
433 pcmcia_chip_socket_disable(sc->pct, sc->pch);
434 }
435
436 /* Enable a PCMCIA function */
437 int
438 pcmcia_function_enable(pf)
439 struct pcmcia_function *pf;
440 {
441 struct pcmcia_softc *sc = pf->sc;
442 struct pcmcia_function *tmp;
443 int reg;
444
445 if (pf->cfe == NULL)
446 panic("pcmcia_function_enable: function not initialized");
447
448 /*
449 * Increase the reference count on the socket, enabling power, if
450 * necessary.
451 */
452 if (sc->sc_enabled_count++ == 0)
453 pcmcia_chip_socket_enable(sc->pct, sc->pch);
454 DPRINTF(("%s: ++enabled_count = %d\n", sc->dev.dv_xname,
455 sc->sc_enabled_count));
456
457 if (pf->pf_flags & PFF_ENABLED) {
458 /*
459 * Don't do anything if we're already enabled.
460 */
461 return (0);
462 }
463
464 /*
465 * it's possible for different functions' CCRs to be in the same
466 * underlying page. Check for that.
467 */
468
469 SIMPLEQ_FOREACH(tmp, &sc->card.pf_head, pf_list) {
470 if ((tmp->pf_flags & PFF_ENABLED) &&
471 (pf->ccr_base >= (tmp->ccr_base - tmp->pf_ccr_offset)) &&
472 ((pf->ccr_base + PCMCIA_CCR_SIZE) <=
473 (tmp->ccr_base - tmp->pf_ccr_offset +
474 tmp->pf_ccr_realsize))) {
475 pf->pf_ccrt = tmp->pf_ccrt;
476 pf->pf_ccrh = tmp->pf_ccrh;
477 pf->pf_ccr_realsize = tmp->pf_ccr_realsize;
478
479 /*
480 * pf->pf_ccr_offset = (tmp->pf_ccr_offset -
481 * tmp->ccr_base) + pf->ccr_base;
482 */
483 pf->pf_ccr_offset =
484 (tmp->pf_ccr_offset + pf->ccr_base) -
485 tmp->ccr_base;
486 pf->pf_ccr_window = tmp->pf_ccr_window;
487 break;
488 }
489 }
490
491 if (tmp == NULL) {
492 if (pcmcia_mem_alloc(pf, PCMCIA_CCR_SIZE, &pf->pf_pcmh))
493 goto bad;
494
495 if (pcmcia_mem_map(pf, PCMCIA_MEM_ATTR, pf->ccr_base,
496 PCMCIA_CCR_SIZE, &pf->pf_pcmh, &pf->pf_ccr_offset,
497 &pf->pf_ccr_window)) {
498 pcmcia_mem_free(pf, &pf->pf_pcmh);
499 goto bad;
500 }
501 }
502
503 if (pcmcia_mfc(sc)) {
504 pcmcia_ccr_write(pf, PCMCIA_CCR_IOBASE0,
505 (pf->pf_mfc_iobase[0] >> 0) & 0xff);
506 pcmcia_ccr_write(pf, PCMCIA_CCR_IOBASE1,
507 (pf->pf_mfc_iobase[0] >> 8) & 0xff);
508 pcmcia_ccr_write(pf, PCMCIA_CCR_IOBASE2,
509 (pf->pf_mfc_iobase[1] >> 0) & 0xff);
510 pcmcia_ccr_write(pf, PCMCIA_CCR_IOBASE3,
511 (pf->pf_mfc_iobase[1] >> 8) & 0xff);
512 pcmcia_ccr_write(pf, PCMCIA_CCR_IOSIZE, pf->pf_mfc_iomask);
513 }
514
515 reg = (pf->cfe->number & PCMCIA_CCR_OPTION_CFINDEX);
516 reg |= PCMCIA_CCR_OPTION_LEVIREQ;
517 if (pcmcia_mfc(sc)) {
518 reg |= (PCMCIA_CCR_OPTION_FUNC_ENABLE |
519 PCMCIA_CCR_OPTION_ADDR_DECODE);
520 if (pf->pf_ih)
521 reg |= PCMCIA_CCR_OPTION_IREQ_ENABLE;
522
523 }
524 pcmcia_ccr_write(pf, PCMCIA_CCR_OPTION, reg);
525
526 reg = 0;
527 if ((pf->cfe->flags & PCMCIA_CFE_IO16) == 0)
528 reg |= PCMCIA_CCR_STATUS_IOIS8;
529 if (pf->cfe->flags & PCMCIA_CFE_AUDIO)
530 reg |= PCMCIA_CCR_STATUS_AUDIO;
531 pcmcia_ccr_write(pf, PCMCIA_CCR_STATUS, reg);
532
533 pcmcia_ccr_write(pf, PCMCIA_CCR_SOCKETCOPY, 0);
534
535 #ifdef PCMCIADEBUG
536 if (pcmcia_debug) {
537 SIMPLEQ_FOREACH(tmp, &sc->card.pf_head, pf_list) {
538 printf("%s: function %d CCR at %d offset %lx: "
539 "%x %x %x %x, %x %x %x %x, %x\n",
540 tmp->sc->dev.dv_xname, tmp->number,
541 tmp->pf_ccr_window,
542 (unsigned long) tmp->pf_ccr_offset,
543 pcmcia_ccr_read(tmp, 0),
544 pcmcia_ccr_read(tmp, 1),
545 pcmcia_ccr_read(tmp, 2),
546 pcmcia_ccr_read(tmp, 3),
547
548 pcmcia_ccr_read(tmp, 5),
549 pcmcia_ccr_read(tmp, 6),
550 pcmcia_ccr_read(tmp, 7),
551 pcmcia_ccr_read(tmp, 8),
552
553 pcmcia_ccr_read(tmp, 9));
554 }
555 }
556 #endif
557
558 pf->pf_flags |= PFF_ENABLED;
559
560 #ifdef IT8368E_LEGACY_MODE
561 /* return to I/O mode */
562 it8368_mode(pf, IT8368_IO_MODE, IT8368_WIDTH_16);
563 #endif
564 return (0);
565
566 bad:
567 /*
568 * Decrement the reference count, and power down the socket, if
569 * necessary.
570 */
571 if (--sc->sc_enabled_count == 0)
572 pcmcia_chip_socket_disable(sc->pct, sc->pch);
573 DPRINTF(("%s: --enabled_count = %d\n", sc->dev.dv_xname,
574 sc->sc_enabled_count));
575
576 return (1);
577 }
578
579 /* Disable PCMCIA function. */
580 void
581 pcmcia_function_disable(pf)
582 struct pcmcia_function *pf;
583 {
584 struct pcmcia_function *tmp;
585 int reg;
586
587 if (pf->cfe == NULL)
588 panic("pcmcia_function_enable: function not initialized");
589
590 if ((pf->pf_flags & PFF_ENABLED) == 0) {
591 /*
592 * Don't do anything but decrement if we're already disabled.
593 */
594 goto out;
595 }
596
597 if (pcmcia_mfc(pf->sc)) {
598 reg = pcmcia_ccr_read(pf, PCMCIA_CCR_OPTION);
599 reg &= ~(PCMCIA_CCR_OPTION_FUNC_ENABLE|
600 PCMCIA_CCR_OPTION_ADDR_DECODE|
601 PCMCIA_CCR_OPTION_IREQ_ENABLE);
602 pcmcia_ccr_write(pf, PCMCIA_CCR_OPTION, reg);
603 }
604
605 /*
606 * it's possible for different functions' CCRs to be in the same
607 * underlying page. Check for that. Note we mark us as disabled
608 * first to avoid matching ourself.
609 */
610
611 pf->pf_flags &= ~PFF_ENABLED;
612 SIMPLEQ_FOREACH(tmp, &pf->sc->card.pf_head, pf_list) {
613 if ((tmp->pf_flags & PFF_ENABLED) &&
614 (pf->ccr_base >= (tmp->ccr_base - tmp->pf_ccr_offset)) &&
615 ((pf->ccr_base + PCMCIA_CCR_SIZE) <=
616 (tmp->ccr_base - tmp->pf_ccr_offset + tmp->pf_ccr_realsize)))
617 break;
618 }
619
620 /* Not used by anyone else; unmap the CCR. */
621 if (tmp == NULL) {
622 pcmcia_mem_unmap(pf, pf->pf_ccr_window);
623 pcmcia_mem_free(pf, &pf->pf_pcmh);
624 }
625
626 out:
627 /*
628 * Decrement the reference count, and power down the socket, if
629 * necessary.
630 */
631 if (--pf->sc->sc_enabled_count == 0)
632 pcmcia_chip_socket_disable(pf->sc->pct, pf->sc->pch);
633 DPRINTF(("%s: --enabled_count = %d\n", pf->sc->dev.dv_xname,
634 pf->sc->sc_enabled_count));
635 }
636
637 int
638 pcmcia_io_map(pf, width, pcihp, windowp)
639 struct pcmcia_function *pf;
640 int width;
641 struct pcmcia_io_handle *pcihp;
642 int *windowp;
643 {
644 int error;
645
646 if (pf->pf_flags & PFF_ENABLED)
647 printf("pcmcia_io_map: function is enabled!\n");
648
649 error = pcmcia_chip_io_map(pf->sc->pct, pf->sc->pch,
650 width, 0, pcihp->size, pcihp, windowp);
651 if (error)
652 return (error);
653
654 /*
655 * XXX in the multifunction multi-iospace-per-function case, this
656 * needs to cooperate with io_alloc to make sure that the spaces
657 * don't overlap, and that the ccr's are set correctly
658 */
659
660 if (pcmcia_mfc(pf->sc)) {
661 int win;
662 long iomask;
663
664 /* round up to nearest (2^n)-1 */
665 for (iomask = 1; iomask < pcihp->size; iomask <<= 1)
666 ;
667 iomask--;
668
669 win = pf->pf_mfc_windows;
670 KASSERT(win < 2);
671 printf("win %d = addr %lx size %lx iomask %lx\n", win, (long)pcihp->addr, (long)pcihp->size, (long)iomask);
672 pf->pf_mfc_iobase[win] = pcihp->addr;
673 pf->pf_mfc_iomask = iomask;
674 }
675
676 return (0);
677 }
678
679 void
680 pcmcia_io_unmap(pf, window)
681 struct pcmcia_function *pf;
682 int window;
683 {
684
685 if (pf->pf_flags & PFF_ENABLED)
686 printf("pcmcia_io_unmap: function is enabled!\n");
687
688 if (pcmcia_mfc(pf->sc)) {
689 /*
690 * Don't bother trying to unmap windows in the CCR here,
691 * because we generally get called when a card has been
692 * ejected, and the CCR isn't there any more.
693 */
694 --pf->pf_mfc_windows;
695 }
696
697 pcmcia_chip_io_unmap(pf->sc->pct, pf->sc->pch, window);
698 }
699
700 void *
701 pcmcia_intr_establish(pf, ipl, ih_fct, ih_arg)
702 struct pcmcia_function *pf;
703 int ipl;
704 int (*ih_fct) __P((void *));
705 void *ih_arg;
706 {
707
708 if (pf->pf_flags & PFF_ENABLED)
709 printf("pcmcia_intr_establish: function is enabled!\n");
710 if (pf->pf_ih)
711 panic("pcmcia_intr_establish: already done\n");
712
713 pf->pf_ih = pcmcia_chip_intr_establish(pf->sc->pct, pf->sc->pch,
714 pf, ipl, ih_fct, ih_arg);
715 return (pf->pf_ih);
716 }
717
718 void
719 pcmcia_intr_disestablish(pf, ih)
720 struct pcmcia_function *pf;
721 void *ih;
722 {
723
724 if (pf->pf_flags & PFF_ENABLED)
725 printf("pcmcia_intr_disestablish: function is enabled!\n");
726 if (!pf->pf_ih)
727 panic("pcmcia_intr_distestablish: already done\n");
728
729 pcmcia_chip_intr_disestablish(pf->sc->pct, pf->sc->pch, ih);
730 pf->pf_ih = 0;
731 }
732
733 int
734 pcmcia_config_alloc(pf, cfe)
735 struct pcmcia_function *pf;
736 struct pcmcia_config_entry *cfe;
737 {
738 int error;
739 int n, m;
740
741 for (n = 0; n < cfe->num_iospace; n++) {
742 bus_addr_t start = cfe->iospace[n].start;
743 bus_size_t length = cfe->iospace[n].length;
744 bus_size_t align = cfe->iomask ? (1 << cfe->iomask) :
745 length;
746 bus_size_t skew = start & (align - 1);
747
748 if (skew)
749 printf("Drats! I need a skew!\n");
750 if ((start & ~(align - 1)) == 0 && align < 0x400)
751 start = 0;
752
753 DPRINTF(("pcmcia_config_alloc: io %d start=%lx length=%lx align=%lx skew=%lx\n",
754 n, (long)start, (long)length, (long)align, (long)skew));
755
756 error = pcmcia_io_alloc(pf, start, length, align,
757 &cfe->iospace[n].handle);
758 if (error)
759 break;
760 }
761 if (n < cfe->num_iospace) {
762 for (m = 0; m < n; m++)
763 pcmcia_io_free(pf, &cfe->iospace[m].handle);
764 return (error);
765 }
766
767 for (n = 0; n < cfe->num_memspace; n++) {
768 bus_size_t length = cfe->memspace[n].length;
769
770 DPRINTF(("pcmcia_config_alloc: mem %d length %lx\n", n,
771 (long)length));
772
773 error = pcmcia_mem_alloc(pf, length, &cfe->memspace[n].handle);
774 if (error)
775 break;
776 }
777 if (n < cfe->num_memspace) {
778 for (m = 0; m < cfe->num_iospace; m++)
779 pcmcia_io_free(pf, &cfe->iospace[m].handle);
780 for (m = 0; m < n; m++)
781 pcmcia_mem_free(pf, &cfe->memspace[m].handle);
782 return (error);
783 }
784
785 /* This one's good! */
786 return (0);
787 }
788
789 void
790 pcmcia_config_free(pf)
791 struct pcmcia_function *pf;
792 {
793 struct pcmcia_config_entry *cfe = pf->cfe;
794 int m;
795
796 for (m = 0; m < cfe->num_iospace; m++)
797 pcmcia_io_free(pf, &cfe->iospace[m].handle);
798 for (m = 0; m < cfe->num_memspace; m++)
799 pcmcia_mem_free(pf, &cfe->memspace[m].handle);
800 }
801
802 int
803 pcmcia_config_map(pf)
804 struct pcmcia_function *pf;
805 {
806 struct pcmcia_config_entry *cfe = pf->cfe;
807 int error;
808 int n, m;
809
810 for (n = 0; n < cfe->num_iospace; n++) {
811 int width;
812
813 switch (cfe->flags & (PCMCIA_CFE_IO8|PCMCIA_CFE_IO16)) {
814 case PCMCIA_CFE_IO8:
815 width = PCMCIA_WIDTH_IO8;
816 break;
817 case PCMCIA_CFE_IO16:
818 width = PCMCIA_WIDTH_IO16;
819 break;
820 case PCMCIA_CFE_IO8|PCMCIA_CFE_IO16:
821 default:
822 width = PCMCIA_WIDTH_AUTO;
823 break;
824 }
825 error = pcmcia_io_map(pf, width, &cfe->iospace[n].handle,
826 &cfe->iospace[n].window);
827 if (error)
828 break;
829 }
830 if (n < cfe->num_iospace) {
831 for (m = 0; m < n; m++)
832 pcmcia_io_unmap(pf, cfe->iospace[m].window);
833 return (error);
834 }
835
836 for (n = 0; n < cfe->num_memspace; n++) {
837 bus_size_t length = cfe->memspace[n].length;
838 int width;
839
840 DPRINTF(("pcmcia_config_alloc: mem %d length %lx\n", n,
841 (long)length));
842
843 /*XXX*/
844 width = PCMCIA_WIDTH_MEM8|PCMCIA_MEM_COMMON;
845 error = pcmcia_mem_map(pf, width, 0, length,
846 &cfe->memspace[n].handle, &cfe->memspace[n].offset,
847 &cfe->memspace[n].window);
848 if (error)
849 break;
850 }
851 if (n < cfe->num_memspace) {
852 for (m = 0; m < cfe->num_iospace; m++)
853 pcmcia_io_unmap(pf, cfe->iospace[m].window);
854 for (m = 0; m < n; m++)
855 pcmcia_mem_unmap(pf, cfe->memspace[m].window);
856 return (error);
857 }
858
859 /* This one's good! */
860 return (0);
861 }
862
863 void
864 pcmcia_config_unmap(pf)
865 struct pcmcia_function *pf;
866 {
867 struct pcmcia_config_entry *cfe = pf->cfe;
868 int m;
869
870 for (m = 0; m < cfe->num_iospace; m++)
871 pcmcia_io_unmap(pf, cfe->iospace[m].window);
872 for (m = 0; m < cfe->num_memspace; m++)
873 pcmcia_mem_unmap(pf, cfe->memspace[m].window);
874 }
875
876 int
877 pcmcia_function_configure(pf, validator)
878 struct pcmcia_function *pf;
879 int (*validator) __P((struct pcmcia_config_entry *));
880 {
881 struct pcmcia_config_entry *cfe;
882 int error = ENOENT;
883
884 SIMPLEQ_FOREACH(cfe, &pf->cfe_head, cfe_list) {
885 error = validator(cfe);
886 if (error)
887 continue;
888 error = pcmcia_config_alloc(pf, cfe);
889 if (!error)
890 break;
891 }
892 if (!cfe) {
893 DPRINTF(("pcmcia_function_configure: no config entry found, error=%d\n",
894 error));
895 return (error);
896 }
897
898 /* Remember which configuration entry we are using. */
899 pf->cfe = cfe;
900
901 error = pcmcia_config_map(pf);
902 if (error) {
903 DPRINTF(("pcmcia_function_configure: map failed, error=%d\n",
904 error));
905 return (error);
906 }
907
908 return (0);
909 }
910
911 void
912 pcmcia_function_unconfigure(pf)
913 struct pcmcia_function *pf;
914 {
915
916 pcmcia_config_unmap(pf);
917 pcmcia_config_free(pf);
918 }
919