pcmcia.c revision 1.59 1 /* $NetBSD: pcmcia.c,v 1.59 2004/08/10 23:34:06 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.59 2004/08/10 23:34:06 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 = 0;
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_socket_enable(dev);
160
161 pcmcia_read_cis(sc);
162 pcmcia_check_cis_quirks(sc);
163
164 pcmcia_socket_disable(dev);
165
166 /*
167 * bail now if the card has no functions, or if there was an error in
168 * the cis.
169 */
170
171 if (sc->card.error)
172 goto done;
173 if (SIMPLEQ_EMPTY(&sc->card.pf_head))
174 goto done;
175
176 if (pcmcia_verbose)
177 pcmcia_print_cis(sc);
178
179 SIMPLEQ_FOREACH(pf, &sc->card.pf_head, pf_list) {
180 if (SIMPLEQ_EMPTY(&pf->cfe_head))
181 continue;
182
183 #ifdef DIAGNOSTIC
184 if (pf->child != NULL) {
185 printf("%s: %s still attached to function %d!\n",
186 sc->dev.dv_xname, pf->child->dv_xname,
187 pf->number);
188 panic("pcmcia_card_attach");
189 }
190 #endif
191 pf->sc = sc;
192 pf->child = NULL;
193 pf->cfe = NULL;
194 pf->pf_ih = NULL;
195 }
196
197 SIMPLEQ_FOREACH(pf, &sc->card.pf_head, pf_list) {
198 if (SIMPLEQ_EMPTY(&pf->cfe_head))
199 continue;
200
201 paa.manufacturer = sc->card.manufacturer;
202 paa.product = sc->card.product;
203 paa.card = &sc->card;
204 paa.pf = pf;
205
206 if ((pf->child = config_found_sm(&sc->dev, &paa, pcmcia_print,
207 pcmcia_submatch)) != NULL)
208 attached++;
209 }
210
211 done:
212 return (attached ? 0 : 1);
213 }
214
215 void
216 pcmcia_card_detach(dev, flags)
217 struct device *dev;
218 int flags; /* DETACH_* flags */
219 {
220 struct pcmcia_softc *sc = (struct pcmcia_softc *) dev;
221 struct pcmcia_function *pf;
222 int error;
223
224 /*
225 * We are running on either the PCMCIA socket's event thread
226 * or in user context detaching a device by user request.
227 */
228 SIMPLEQ_FOREACH(pf, &sc->card.pf_head, pf_list) {
229 if (SIMPLEQ_EMPTY(&pf->cfe_head))
230 continue;
231 if (pf->child == NULL)
232 continue;
233 DPRINTF(("%s: detaching %s (function %d)\n",
234 sc->dev.dv_xname, pf->child->dv_xname, pf->number));
235 if ((error = config_detach(pf->child, flags)) != 0) {
236 printf("%s: error %d detaching %s (function %d)\n",
237 sc->dev.dv_xname, error, pf->child->dv_xname,
238 pf->number);
239 } else
240 pf->child = NULL;
241 }
242
243 if (sc->sc_enabled_count != 0) {
244 #ifdef DIAGNOSTIC
245 printf("pcmcia_card_detach: enabled_count should be 0 here??\n");
246 #endif
247 pcmcia_chip_socket_disable(sc->pct, sc->pch);
248 sc->sc_enabled_count = 0;
249 }
250 }
251
252 void
253 pcmcia_card_deactivate(dev)
254 struct device *dev;
255 {
256 struct pcmcia_softc *sc = (struct pcmcia_softc *) dev;
257 struct pcmcia_function *pf;
258
259 /*
260 * We're in the chip's card removal interrupt handler.
261 * Deactivate the child driver. The PCMCIA socket's
262 * event thread will run later to finish the detach.
263 */
264 SIMPLEQ_FOREACH(pf, &sc->card.pf_head, pf_list) {
265 if (SIMPLEQ_EMPTY(&pf->cfe_head))
266 continue;
267 if (pf->child == NULL)
268 continue;
269 DPRINTF(("%s: deactivating %s (function %d)\n",
270 sc->dev.dv_xname, pf->child->dv_xname, pf->number));
271 config_deactivate(pf->child);
272 }
273 }
274
275 int
276 pcmcia_submatch(parent, cf, aux)
277 struct device *parent;
278 struct cfdata *cf;
279 void *aux;
280 {
281 struct pcmcia_attach_args *paa = aux;
282
283 if (cf->cf_loc[PCMCIACF_FUNCTION] != PCMCIACF_FUNCTION_DEFAULT &&
284 cf->cf_loc[PCMCIACF_FUNCTION] != paa->pf->number)
285 return (0);
286
287 return (config_match(parent, cf, aux));
288 }
289
290 int
291 pcmcia_print(arg, pnp)
292 void *arg;
293 const char *pnp;
294 {
295 struct pcmcia_attach_args *pa = arg;
296 struct pcmcia_softc *sc = pa->pf->sc;
297 struct pcmcia_card *card = &sc->card;
298 char devinfo[256];
299
300 if (pnp)
301 aprint_normal("%s", pnp);
302
303 pcmcia_devinfo(card, !!pnp, devinfo, sizeof(devinfo));
304
305 aprint_normal(" function %d: %s\n", pa->pf->number, devinfo);
306
307 return (UNCONF);
308 }
309
310 void
311 pcmcia_devinfo(card, showhex, cp, cplen)
312 struct pcmcia_card *card;
313 int showhex;
314 char *cp;
315 size_t cplen;
316 {
317 int i, n;
318
319 if (cplen > 1) {
320 *cp++ = '<';
321 *cp = '\0';
322 cplen--;
323 }
324
325 for (i = 0; i < 4 && card->cis1_info[i] != NULL && cplen > 1; i++) {
326 n = snprintf(cp, cplen, "%s%s", i ? ", " : "",
327 card->cis1_info[i]);
328 cp += n;
329 if (cplen < n)
330 return;
331 cplen -= n;
332 }
333
334 if (cplen > 1) {
335 *cp++ = '>';
336 *cp = '\0';
337 cplen--;
338 }
339
340 if (showhex && cplen > 1)
341 snprintf(cp, cplen, " (manufacturer 0x%04x, product 0x%04x)",
342 card->manufacturer, card->product);
343 }
344
345 const void *
346 pcmcia_product_lookup(pa, tab, nent, ent_size, matchfn)
347 struct pcmcia_attach_args *pa;
348 const void *tab;
349 size_t nent;
350 size_t ent_size;
351 pcmcia_product_match_fn matchfn;
352 {
353 const struct pcmcia_product *pp;
354 int n;
355 int matches;
356
357 #ifdef DIAGNOSTIC
358 if (sizeof *pp > ent_size)
359 panic("pcmcia_product_lookup: bogus ent_size %ld",
360 (long) ent_size);
361 #endif
362
363 for (pp = tab, n = nent; n; pp = (const struct pcmcia_product *)
364 ((const char *)pp + ent_size), n--) {
365 /* see if it matches vendor/product */
366 matches = 0;
367 if ((pp->pp_vendor != PCMCIA_VENDOR_INVALID &&
368 pp->pp_vendor == pa->manufacturer) &&
369 (pp->pp_product != PCMCIA_PRODUCT_INVALID &&
370 pp->pp_product == pa->product))
371 matches = 1;
372 if ((pp->pp_cisinfo[0] && pa->card->cis1_info[0] &&
373 !strcmp(pp->pp_cisinfo[0], pa->card->cis1_info[0])) &&
374 (pp->pp_cisinfo[1] && pa->card->cis1_info[1] &&
375 !strcmp(pp->pp_cisinfo[1], pa->card->cis1_info[1])) &&
376 (!pp->pp_cisinfo[2] || (pa->card->cis1_info[2] &&
377 !strcmp(pp->pp_cisinfo[2], pa->card->cis1_info[2]))) &&
378 (!pp->pp_cisinfo[3] || (pa->card->cis1_info[3] &&
379 !strcmp(pp->pp_cisinfo[3], pa->card->cis1_info[3]))))
380 matches = 1;
381
382 /* if a separate match function is given, let it override */
383 if (matchfn)
384 matches = (*matchfn)(pa, pp, matches);
385
386 if (matches)
387 return (pp);
388 }
389 return (0);
390 }
391
392 int
393 pcmcia_card_gettype(dev)
394 struct device *dev;
395 {
396 struct pcmcia_softc *sc = (struct pcmcia_softc *)dev;
397 struct pcmcia_function *pf;
398
399 /*
400 * set the iftype to memory if this card has no functions (not yet
401 * probed), or only one function, and that is not initialized yet or
402 * that is memory.
403 */
404 pf = SIMPLEQ_FIRST(&sc->card.pf_head);
405 if (pf == NULL ||
406 (SIMPLEQ_NEXT(pf, pf_list) == NULL &&
407 (pf->cfe == NULL || pf->cfe->iftype == PCMCIA_IFTYPE_MEMORY)))
408 return (PCMCIA_IFTYPE_MEMORY);
409 else
410 return (PCMCIA_IFTYPE_IO);
411 }
412
413 /*
414 * Initialize a PCMCIA function. May be called as long as the function is
415 * disabled.
416 */
417 void
418 pcmcia_function_init(pf, cfe)
419 struct pcmcia_function *pf;
420 struct pcmcia_config_entry *cfe;
421 {
422 if (pf->pf_flags & PFF_ENABLED)
423 panic("pcmcia_function_init: function is enabled");
424
425 /* Remember which configuration entry we are using. */
426 pf->cfe = cfe;
427 }
428
429 void
430 pcmcia_socket_enable(dev)
431 struct device *dev;
432 {
433 struct pcmcia_softc *sc = (void *)dev;
434
435 if (sc->sc_enabled_count++ == 0)
436 pcmcia_chip_socket_enable(sc->pct, sc->pch);
437 DPRINTF(("%s: ++enabled_count = %d\n", sc->dev.dv_xname,
438 sc->sc_enabled_count));
439 }
440
441 void
442 pcmcia_socket_disable(dev)
443 struct device *dev;
444 {
445 struct pcmcia_softc *sc = (void *)dev;
446
447 if (--sc->sc_enabled_count == 0)
448 pcmcia_chip_socket_disable(sc->pct, sc->pch);
449 DPRINTF(("%s: --enabled_count = %d\n", sc->dev.dv_xname,
450 sc->sc_enabled_count));
451 }
452
453 /* Enable a PCMCIA function */
454 int
455 pcmcia_function_enable(pf)
456 struct pcmcia_function *pf;
457 {
458 struct pcmcia_softc *sc = pf->sc;
459 struct pcmcia_function *tmp;
460 int reg;
461 int error;
462
463 if (pf->cfe == NULL)
464 panic("pcmcia_function_enable: function not initialized");
465
466 /*
467 * Increase the reference count on the socket, enabling power, if
468 * necessary.
469 */
470 pcmcia_socket_enable(&sc->dev);
471
472 if (pf->pf_flags & PFF_ENABLED) {
473 /*
474 * Don't do anything if we're already enabled.
475 */
476 return (0);
477 }
478
479 /*
480 * it's possible for different functions' CCRs to be in the same
481 * underlying page. Check for that.
482 */
483
484 SIMPLEQ_FOREACH(tmp, &sc->card.pf_head, pf_list) {
485 if ((tmp->pf_flags & PFF_ENABLED) &&
486 (pf->ccr_base >= (tmp->ccr_base - tmp->pf_ccr_offset)) &&
487 ((pf->ccr_base + PCMCIA_CCR_SIZE) <=
488 (tmp->ccr_base - tmp->pf_ccr_offset +
489 tmp->pf_ccr_realsize))) {
490 pf->pf_ccrt = tmp->pf_ccrt;
491 pf->pf_ccrh = tmp->pf_ccrh;
492 pf->pf_ccr_realsize = tmp->pf_ccr_realsize;
493
494 /*
495 * pf->pf_ccr_offset = (tmp->pf_ccr_offset -
496 * tmp->ccr_base) + pf->ccr_base;
497 */
498 pf->pf_ccr_offset =
499 (tmp->pf_ccr_offset + pf->ccr_base) -
500 tmp->ccr_base;
501 pf->pf_ccr_window = tmp->pf_ccr_window;
502 break;
503 }
504 }
505
506 if (tmp == NULL) {
507 error = pcmcia_mem_alloc(pf, PCMCIA_CCR_SIZE, &pf->pf_pcmh);
508 if (error)
509 goto bad;
510
511 error = pcmcia_mem_map(pf, PCMCIA_MEM_ATTR, pf->ccr_base,
512 PCMCIA_CCR_SIZE, &pf->pf_pcmh, &pf->pf_ccr_offset,
513 &pf->pf_ccr_window);
514 if (error) {
515 pcmcia_mem_free(pf, &pf->pf_pcmh);
516 goto bad;
517 }
518 }
519
520 if (pcmcia_mfc(sc)) {
521 pcmcia_ccr_write(pf, PCMCIA_CCR_IOBASE0,
522 (pf->pf_mfc_iobase[0] >> 0) & 0xff);
523 pcmcia_ccr_write(pf, PCMCIA_CCR_IOBASE1,
524 (pf->pf_mfc_iobase[0] >> 8) & 0xff);
525 pcmcia_ccr_write(pf, PCMCIA_CCR_IOBASE2,
526 (pf->pf_mfc_iobase[1] >> 0) & 0xff);
527 pcmcia_ccr_write(pf, PCMCIA_CCR_IOBASE3,
528 (pf->pf_mfc_iobase[1] >> 8) & 0xff);
529 pcmcia_ccr_write(pf, PCMCIA_CCR_IOSIZE, pf->pf_mfc_iomask);
530 }
531
532 reg = (pf->cfe->number & PCMCIA_CCR_OPTION_CFINDEX);
533 reg |= PCMCIA_CCR_OPTION_LEVIREQ;
534 if (pcmcia_mfc(sc)) {
535 reg |= (PCMCIA_CCR_OPTION_FUNC_ENABLE |
536 PCMCIA_CCR_OPTION_ADDR_DECODE);
537 if (pf->pf_ih)
538 reg |= PCMCIA_CCR_OPTION_IREQ_ENABLE;
539
540 }
541 pcmcia_ccr_write(pf, PCMCIA_CCR_OPTION, reg);
542
543 reg = 0;
544 if ((pf->cfe->flags & PCMCIA_CFE_IO16) == 0)
545 reg |= PCMCIA_CCR_STATUS_IOIS8;
546 if (pf->cfe->flags & PCMCIA_CFE_AUDIO)
547 reg |= PCMCIA_CCR_STATUS_AUDIO;
548 pcmcia_ccr_write(pf, PCMCIA_CCR_STATUS, reg);
549
550 pcmcia_ccr_write(pf, PCMCIA_CCR_SOCKETCOPY, 0);
551
552 #ifdef PCMCIADEBUG
553 if (pcmcia_debug) {
554 SIMPLEQ_FOREACH(tmp, &sc->card.pf_head, pf_list) {
555 printf("%s: function %d CCR at %d offset %lx: "
556 "%x %x %x %x, %x %x %x %x, %x\n",
557 tmp->sc->dev.dv_xname, tmp->number,
558 tmp->pf_ccr_window,
559 (unsigned long) tmp->pf_ccr_offset,
560 pcmcia_ccr_read(tmp, 0),
561 pcmcia_ccr_read(tmp, 1),
562 pcmcia_ccr_read(tmp, 2),
563 pcmcia_ccr_read(tmp, 3),
564
565 pcmcia_ccr_read(tmp, 5),
566 pcmcia_ccr_read(tmp, 6),
567 pcmcia_ccr_read(tmp, 7),
568 pcmcia_ccr_read(tmp, 8),
569
570 pcmcia_ccr_read(tmp, 9));
571 }
572 }
573 #endif
574
575 pf->pf_flags |= PFF_ENABLED;
576
577 #ifdef IT8368E_LEGACY_MODE
578 /* return to I/O mode */
579 it8368_mode(pf, IT8368_IO_MODE, IT8368_WIDTH_16);
580 #endif
581 return (0);
582
583 bad:
584 /*
585 * Decrement the reference count, and power down the socket, if
586 * necessary.
587 */
588 printf("%s: couldn't map the CCR\n", pf->child->dv_xname);
589 pcmcia_socket_disable(&sc->dev);
590
591 return (error);
592 }
593
594 /* Disable PCMCIA function. */
595 void
596 pcmcia_function_disable(pf)
597 struct pcmcia_function *pf;
598 {
599 struct pcmcia_softc *sc = pf->sc;
600 struct pcmcia_function *tmp;
601 int reg;
602
603 if (pf->cfe == NULL)
604 panic("pcmcia_function_enable: function not initialized");
605
606 if ((pf->pf_flags & PFF_ENABLED) == 0) {
607 /*
608 * Don't do anything but decrement if we're already disabled.
609 */
610 goto out;
611 }
612
613 if (pcmcia_mfc(sc)) {
614 reg = pcmcia_ccr_read(pf, PCMCIA_CCR_OPTION);
615 reg &= ~(PCMCIA_CCR_OPTION_FUNC_ENABLE|
616 PCMCIA_CCR_OPTION_ADDR_DECODE|
617 PCMCIA_CCR_OPTION_IREQ_ENABLE);
618 pcmcia_ccr_write(pf, PCMCIA_CCR_OPTION, reg);
619 }
620
621 /*
622 * it's possible for different functions' CCRs to be in the same
623 * underlying page. Check for that. Note we mark us as disabled
624 * first to avoid matching ourself.
625 */
626
627 pf->pf_flags &= ~PFF_ENABLED;
628 SIMPLEQ_FOREACH(tmp, &sc->card.pf_head, pf_list) {
629 if ((tmp->pf_flags & PFF_ENABLED) &&
630 (pf->ccr_base >= (tmp->ccr_base - tmp->pf_ccr_offset)) &&
631 ((pf->ccr_base + PCMCIA_CCR_SIZE) <=
632 (tmp->ccr_base - tmp->pf_ccr_offset + tmp->pf_ccr_realsize)))
633 break;
634 }
635
636 /* Not used by anyone else; unmap the CCR. */
637 if (tmp == NULL) {
638 pcmcia_mem_unmap(pf, pf->pf_ccr_window);
639 pcmcia_mem_free(pf, &pf->pf_pcmh);
640 }
641
642 out:
643 /*
644 * Decrement the reference count, and power down the socket, if
645 * necessary.
646 */
647 pcmcia_socket_disable(&sc->dev);
648 }
649
650 int
651 pcmcia_io_map(pf, width, pcihp, windowp)
652 struct pcmcia_function *pf;
653 int width;
654 struct pcmcia_io_handle *pcihp;
655 int *windowp;
656 {
657 int error;
658
659 if (pf->pf_flags & PFF_ENABLED)
660 printf("pcmcia_io_map: function is enabled!\n");
661
662 error = pcmcia_chip_io_map(pf->sc->pct, pf->sc->pch,
663 width, 0, pcihp->size, pcihp, windowp);
664 if (error)
665 return (error);
666
667 /*
668 * XXX in the multifunction multi-iospace-per-function case, this
669 * needs to cooperate with io_alloc to make sure that the spaces
670 * don't overlap, and that the ccr's are set correctly
671 */
672
673 if (pcmcia_mfc(pf->sc)) {
674 int win;
675 long iomask;
676
677 /* round up to nearest (2^n)-1 */
678 for (iomask = 1; iomask < pcihp->size; iomask <<= 1)
679 ;
680 iomask--;
681
682 win = pf->pf_mfc_windows;
683 KASSERT(win < 2);
684 printf("win %d = addr %lx size %lx iomask %lx\n", win, (long)pcihp->addr, (long)pcihp->size, (long)iomask);
685 pf->pf_mfc_iobase[win] = pcihp->addr;
686 pf->pf_mfc_iomask = iomask;
687 }
688
689 return (0);
690 }
691
692 void
693 pcmcia_io_unmap(pf, window)
694 struct pcmcia_function *pf;
695 int window;
696 {
697
698 if (pf->pf_flags & PFF_ENABLED)
699 printf("pcmcia_io_unmap: function is enabled!\n");
700
701 if (pcmcia_mfc(pf->sc)) {
702 /*
703 * Don't bother trying to unmap windows in the CCR here,
704 * because we generally get called when a card has been
705 * ejected, and the CCR isn't there any more.
706 */
707 --pf->pf_mfc_windows;
708 }
709
710 pcmcia_chip_io_unmap(pf->sc->pct, pf->sc->pch, window);
711 }
712
713 void *
714 pcmcia_intr_establish(pf, ipl, ih_fct, ih_arg)
715 struct pcmcia_function *pf;
716 int ipl;
717 int (*ih_fct) __P((void *));
718 void *ih_arg;
719 {
720
721 if (pf->pf_flags & PFF_ENABLED)
722 printf("pcmcia_intr_establish: function is enabled!\n");
723 if (pf->pf_ih)
724 panic("pcmcia_intr_establish: already done\n");
725
726 pf->pf_ih = pcmcia_chip_intr_establish(pf->sc->pct, pf->sc->pch,
727 pf, ipl, ih_fct, ih_arg);
728 if (!pf->pf_ih)
729 printf("%s: interrupt establish failed\n", pf->child->dv_xname);
730 return (pf->pf_ih);
731 }
732
733 void
734 pcmcia_intr_disestablish(pf, ih)
735 struct pcmcia_function *pf;
736 void *ih;
737 {
738
739 if (pf->pf_flags & PFF_ENABLED)
740 printf("pcmcia_intr_disestablish: function is enabled!\n");
741 if (!pf->pf_ih)
742 panic("pcmcia_intr_distestablish: already done\n");
743
744 pcmcia_chip_intr_disestablish(pf->sc->pct, pf->sc->pch, ih);
745 pf->pf_ih = 0;
746 }
747
748 int
749 pcmcia_config_alloc(pf, cfe)
750 struct pcmcia_function *pf;
751 struct pcmcia_config_entry *cfe;
752 {
753 int error;
754 int n, m;
755
756 for (n = 0; n < cfe->num_iospace; n++) {
757 bus_addr_t start = cfe->iospace[n].start;
758 bus_size_t length = cfe->iospace[n].length;
759 bus_size_t align = cfe->iomask ? (1 << cfe->iomask) :
760 length;
761 bus_size_t skew = start & (align - 1);
762
763 if ((start - skew) == 0 && align < 0x400) {
764 if (skew)
765 printf("Drats! I need a skew!\n");
766 start = 0;
767 }
768
769 DPRINTF(("pcmcia_config_alloc: io %d start=%lx length=%lx align=%lx skew=%lx\n",
770 n, (long)start, (long)length, (long)align, (long)skew));
771
772 error = pcmcia_io_alloc(pf, start, length, align,
773 &cfe->iospace[n].handle);
774 if (error)
775 break;
776 }
777 if (n < cfe->num_iospace) {
778 for (m = 0; m < n; m++)
779 pcmcia_io_free(pf, &cfe->iospace[m].handle);
780 return (error);
781 }
782
783 for (n = 0; n < cfe->num_memspace; n++) {
784 bus_size_t length = cfe->memspace[n].length;
785
786 DPRINTF(("pcmcia_config_alloc: mem %d length %lx\n", n,
787 (long)length));
788
789 error = pcmcia_mem_alloc(pf, length, &cfe->memspace[n].handle);
790 if (error)
791 break;
792 }
793 if (n < cfe->num_memspace) {
794 for (m = 0; m < cfe->num_iospace; m++)
795 pcmcia_io_free(pf, &cfe->iospace[m].handle);
796 for (m = 0; m < n; m++)
797 pcmcia_mem_free(pf, &cfe->memspace[m].handle);
798 return (error);
799 }
800
801 /* This one's good! */
802 return (0);
803 }
804
805 void
806 pcmcia_config_free(pf)
807 struct pcmcia_function *pf;
808 {
809 struct pcmcia_config_entry *cfe = pf->cfe;
810 int m;
811
812 for (m = 0; m < cfe->num_iospace; m++)
813 pcmcia_io_free(pf, &cfe->iospace[m].handle);
814 for (m = 0; m < cfe->num_memspace; m++)
815 pcmcia_mem_free(pf, &cfe->memspace[m].handle);
816 }
817
818 int
819 pcmcia_config_map(pf)
820 struct pcmcia_function *pf;
821 {
822 struct pcmcia_config_entry *cfe = pf->cfe;
823 int error;
824 int n, m;
825
826 for (n = 0; n < cfe->num_iospace; n++) {
827 int width;
828
829 switch (cfe->flags & (PCMCIA_CFE_IO8|PCMCIA_CFE_IO16)) {
830 case PCMCIA_CFE_IO8:
831 width = PCMCIA_WIDTH_IO8;
832 break;
833 case PCMCIA_CFE_IO16:
834 width = PCMCIA_WIDTH_IO16;
835 break;
836 case PCMCIA_CFE_IO8|PCMCIA_CFE_IO16:
837 default:
838 width = PCMCIA_WIDTH_AUTO;
839 break;
840 }
841 error = pcmcia_io_map(pf, width, &cfe->iospace[n].handle,
842 &cfe->iospace[n].window);
843 if (error)
844 break;
845 }
846 if (n < cfe->num_iospace) {
847 for (m = 0; m < n; m++)
848 pcmcia_io_unmap(pf, cfe->iospace[m].window);
849 return (error);
850 }
851
852 for (n = 0; n < cfe->num_memspace; n++) {
853 bus_size_t length = cfe->memspace[n].length;
854 int width;
855
856 DPRINTF(("pcmcia_config_alloc: mem %d length %lx\n", n,
857 (long)length));
858
859 /*XXX*/
860 width = PCMCIA_WIDTH_MEM8|PCMCIA_MEM_COMMON;
861 error = pcmcia_mem_map(pf, width, 0, length,
862 &cfe->memspace[n].handle, &cfe->memspace[n].offset,
863 &cfe->memspace[n].window);
864 if (error)
865 break;
866 }
867 if (n < cfe->num_memspace) {
868 for (m = 0; m < cfe->num_iospace; m++)
869 pcmcia_io_unmap(pf, cfe->iospace[m].window);
870 for (m = 0; m < n; m++)
871 pcmcia_mem_unmap(pf, cfe->memspace[m].window);
872 return (error);
873 }
874
875 /* This one's good! */
876 return (0);
877 }
878
879 void
880 pcmcia_config_unmap(pf)
881 struct pcmcia_function *pf;
882 {
883 struct pcmcia_config_entry *cfe = pf->cfe;
884 int m;
885
886 for (m = 0; m < cfe->num_iospace; m++)
887 pcmcia_io_unmap(pf, cfe->iospace[m].window);
888 for (m = 0; m < cfe->num_memspace; m++)
889 pcmcia_mem_unmap(pf, cfe->memspace[m].window);
890 }
891
892 int
893 pcmcia_function_configure(pf, validator)
894 struct pcmcia_function *pf;
895 int (*validator) __P((struct pcmcia_config_entry *));
896 {
897 struct pcmcia_config_entry *cfe;
898 int error = ENOENT;
899
900 SIMPLEQ_FOREACH(cfe, &pf->cfe_head, cfe_list) {
901 error = validator(cfe);
902 if (error)
903 continue;
904 error = pcmcia_config_alloc(pf, cfe);
905 if (!error)
906 break;
907 }
908 if (!cfe) {
909 DPRINTF(("pcmcia_function_configure: no config entry found, error=%d\n",
910 error));
911 return (error);
912 }
913
914 /* Remember which configuration entry we are using. */
915 pf->cfe = cfe;
916
917 error = pcmcia_config_map(pf);
918 if (error) {
919 DPRINTF(("pcmcia_function_configure: map failed, error=%d\n",
920 error));
921 return (error);
922 }
923
924 return (0);
925 }
926
927 void
928 pcmcia_function_unconfigure(pf)
929 struct pcmcia_function *pf;
930 {
931
932 pcmcia_config_unmap(pf);
933 pcmcia_config_free(pf);
934 }
935