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