pcmcia.c revision 1.74 1 /* $NetBSD: pcmcia.c,v 1.74 2005/08/15 18:58:24 christos 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.74 2005/08/15 18:58:24 christos 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 pcmcia_socket_settype(&sc->dev, pf->cfe->iftype);
520
521 if (pf->pf_flags & PFF_ENABLED) {
522 /*
523 * Don't do anything if we're already enabled.
524 */
525 return (0);
526 }
527
528 /*
529 * it's possible for different functions' CCRs to be in the same
530 * underlying page. Check for that.
531 */
532
533 SIMPLEQ_FOREACH(tmp, &sc->card.pf_head, pf_list) {
534 if ((tmp->pf_flags & PFF_ENABLED) &&
535 (pf->ccr_base >= (tmp->ccr_base - tmp->pf_ccr_offset)) &&
536 ((pf->ccr_base + PCMCIA_CCR_SIZE) <=
537 (tmp->ccr_base - tmp->pf_ccr_offset +
538 tmp->pf_ccr_realsize))) {
539 pf->pf_ccrt = tmp->pf_ccrt;
540 pf->pf_ccrh = tmp->pf_ccrh;
541 pf->pf_ccr_realsize = tmp->pf_ccr_realsize;
542
543 /*
544 * pf->pf_ccr_offset = (tmp->pf_ccr_offset -
545 * tmp->ccr_base) + pf->ccr_base;
546 */
547 pf->pf_ccr_offset =
548 (tmp->pf_ccr_offset + pf->ccr_base) -
549 tmp->ccr_base;
550 pf->pf_ccr_window = tmp->pf_ccr_window;
551 break;
552 }
553 }
554
555 if (tmp == NULL) {
556 error = pcmcia_mem_alloc(pf, PCMCIA_CCR_SIZE, &pf->pf_pcmh);
557 if (error)
558 goto bad;
559
560 error = pcmcia_mem_map(pf, PCMCIA_MEM_ATTR, pf->ccr_base,
561 PCMCIA_CCR_SIZE, &pf->pf_pcmh, &pf->pf_ccr_offset,
562 &pf->pf_ccr_window);
563 if (error) {
564 pcmcia_mem_free(pf, &pf->pf_pcmh);
565 goto bad;
566 }
567 }
568
569 if (pcmcia_mfc(sc) || 1) {
570 pcmcia_ccr_write(pf, PCMCIA_CCR_IOBASE0,
571 (pf->pf_mfc_iobase >> 0) & 0xff);
572 pcmcia_ccr_write(pf, PCMCIA_CCR_IOBASE1,
573 (pf->pf_mfc_iobase >> 8) & 0xff);
574 pcmcia_ccr_write(pf, PCMCIA_CCR_IOBASE2,
575 (pf->pf_mfc_iobase >> 16) & 0xff);
576 pcmcia_ccr_write(pf, PCMCIA_CCR_IOBASE3,
577 (pf->pf_mfc_iobase >> 24) & 0xff);
578 pcmcia_ccr_write(pf, PCMCIA_CCR_IOLIMIT,
579 pf->pf_mfc_iomax - pf->pf_mfc_iobase);
580 }
581
582 reg = 0;
583 if (pf->cfe->flags & PCMCIA_CFE_AUDIO)
584 reg |= PCMCIA_CCR_STATUS_AUDIO;
585 pcmcia_ccr_write(pf, PCMCIA_CCR_STATUS, reg);
586
587 pcmcia_ccr_write(pf, PCMCIA_CCR_SOCKETCOPY, 0);
588
589 reg = (pf->cfe->number & PCMCIA_CCR_OPTION_CFINDEX);
590 reg |= PCMCIA_CCR_OPTION_LEVIREQ;
591 if (pcmcia_mfc(sc)) {
592 reg |= (PCMCIA_CCR_OPTION_FUNC_ENABLE |
593 PCMCIA_CCR_OPTION_ADDR_DECODE);
594 if (pf->pf_ih)
595 reg |= PCMCIA_CCR_OPTION_IREQ_ENABLE;
596
597 }
598 pcmcia_ccr_write(pf, PCMCIA_CCR_OPTION, reg);
599
600 #ifdef PCMCIADEBUG
601 if (pcmcia_debug) {
602 SIMPLEQ_FOREACH(tmp, &sc->card.pf_head, pf_list) {
603 printf("%s: function %d CCR at %d offset %lx: "
604 "%x %x %x %x, %x %x %x %x, %x\n",
605 tmp->sc->dev.dv_xname, tmp->number,
606 tmp->pf_ccr_window,
607 (unsigned long) tmp->pf_ccr_offset,
608 pcmcia_ccr_read(tmp, 0),
609 pcmcia_ccr_read(tmp, 1),
610 pcmcia_ccr_read(tmp, 2),
611 pcmcia_ccr_read(tmp, 3),
612
613 pcmcia_ccr_read(tmp, 5),
614 pcmcia_ccr_read(tmp, 6),
615 pcmcia_ccr_read(tmp, 7),
616 pcmcia_ccr_read(tmp, 8),
617
618 pcmcia_ccr_read(tmp, 9));
619 }
620 }
621 #endif
622
623 #ifdef IT8368E_LEGACY_MODE
624 /* return to I/O mode */
625 it8368_mode(pf, IT8368_IO_MODE, IT8368_WIDTH_16);
626 #endif
627
628 pf->pf_flags |= PFF_ENABLED;
629 return (0);
630
631 bad:
632 /*
633 * Decrement the reference count, and power down the socket, if
634 * necessary.
635 */
636 printf("%s: couldn't map the CCR\n", pf->child->dv_xname);
637 pcmcia_socket_disable(&sc->dev);
638
639 return (error);
640 }
641
642 /* Disable PCMCIA function. */
643 void
644 pcmcia_function_disable(pf)
645 struct pcmcia_function *pf;
646 {
647 struct pcmcia_softc *sc = pf->sc;
648 struct pcmcia_function *tmp;
649 int reg;
650
651 if (pf->cfe == NULL)
652 panic("pcmcia_function_enable: function not initialized");
653
654 if ((pf->pf_flags & PFF_ENABLED) == 0) {
655 /*
656 * Don't do anything but decrement if we're already disabled.
657 */
658 goto out;
659 }
660
661 if (pcmcia_mfc(sc) &&
662 (pf->pf_flags & PFF_DETACHED) == 0) {
663 reg = pcmcia_ccr_read(pf, PCMCIA_CCR_OPTION);
664 reg &= ~(PCMCIA_CCR_OPTION_FUNC_ENABLE|
665 PCMCIA_CCR_OPTION_ADDR_DECODE|
666 PCMCIA_CCR_OPTION_IREQ_ENABLE);
667 pcmcia_ccr_write(pf, PCMCIA_CCR_OPTION, reg);
668 }
669
670 /*
671 * it's possible for different functions' CCRs to be in the same
672 * underlying page. Check for that. Note we mark us as disabled
673 * first to avoid matching ourself.
674 */
675
676 pf->pf_flags &= ~PFF_ENABLED;
677 SIMPLEQ_FOREACH(tmp, &sc->card.pf_head, pf_list) {
678 if ((tmp->pf_flags & PFF_ENABLED) &&
679 (pf->ccr_base >= (tmp->ccr_base - tmp->pf_ccr_offset)) &&
680 ((pf->ccr_base + PCMCIA_CCR_SIZE) <=
681 (tmp->ccr_base - tmp->pf_ccr_offset + tmp->pf_ccr_realsize)))
682 break;
683 }
684
685 /* Not used by anyone else; unmap the CCR. */
686 if (tmp == NULL) {
687 pcmcia_mem_unmap(pf, pf->pf_ccr_window);
688 pcmcia_mem_free(pf, &pf->pf_pcmh);
689 }
690
691 out:
692 /*
693 * Decrement the reference count, and power down the socket, if
694 * necessary.
695 */
696 pcmcia_socket_disable(&sc->dev);
697 }
698
699 int
700 pcmcia_io_map(pf, width, pcihp, windowp)
701 struct pcmcia_function *pf;
702 int width;
703 struct pcmcia_io_handle *pcihp;
704 int *windowp;
705 {
706 struct pcmcia_softc *sc = pf->sc;
707 int error;
708
709 if (pf->pf_flags & PFF_ENABLED)
710 printf("pcmcia_io_map: function is enabled!\n");
711
712 error = pcmcia_chip_io_map(sc->pct, sc->pch,
713 width, 0, pcihp->size, pcihp, windowp);
714 if (error)
715 return (error);
716
717 /*
718 * XXX in the multifunction multi-iospace-per-function case, this
719 * needs to cooperate with io_alloc to make sure that the spaces
720 * don't overlap, and that the ccr's are set correctly
721 */
722
723 if (pcmcia_mfc(sc) || 1) {
724 bus_addr_t iobase = pcihp->addr;
725 bus_addr_t iomax = pcihp->addr + pcihp->size - 1;
726
727 DPRINTF(("window iobase %lx iomax %lx\n", (long)iobase,
728 (long)iomax));
729 if (pf->pf_mfc_iobase == 0) {
730 pf->pf_mfc_iobase = iobase;
731 pf->pf_mfc_iomax = iomax;
732 } else {
733 if (iobase < pf->pf_mfc_iobase)
734 pf->pf_mfc_iobase = iobase;
735 if (iomax > pf->pf_mfc_iomax)
736 pf->pf_mfc_iomax = iomax;
737 }
738 DPRINTF(("function iobase %lx iomax %lx\n",
739 (long)pf->pf_mfc_iobase, (long)pf->pf_mfc_iomax));
740 }
741
742 return (0);
743 }
744
745 void
746 pcmcia_io_unmap(pf, window)
747 struct pcmcia_function *pf;
748 int window;
749 {
750 struct pcmcia_softc *sc = pf->sc;
751
752 if (pf->pf_flags & PFF_ENABLED)
753 printf("pcmcia_io_unmap: function is enabled!\n");
754
755 pcmcia_chip_io_unmap(sc->pct, sc->pch, window);
756 }
757
758 void *
759 pcmcia_intr_establish(pf, ipl, ih_fct, ih_arg)
760 struct pcmcia_function *pf;
761 int ipl;
762 int (*ih_fct)(void *);
763 void *ih_arg;
764 {
765
766 if (pf->pf_flags & PFF_ENABLED)
767 printf("pcmcia_intr_establish: function is enabled!\n");
768 if (pf->pf_ih)
769 panic("pcmcia_intr_establish: already done\n");
770
771 pf->pf_ih = pcmcia_chip_intr_establish(pf->sc->pct, pf->sc->pch,
772 pf, ipl, ih_fct, ih_arg);
773 if (!pf->pf_ih)
774 printf("%s: interrupt establish failed\n", pf->child->dv_xname);
775 return (pf->pf_ih);
776 }
777
778 void
779 pcmcia_intr_disestablish(pf, ih)
780 struct pcmcia_function *pf;
781 void *ih;
782 {
783
784 if (pf->pf_flags & PFF_ENABLED)
785 printf("pcmcia_intr_disestablish: function is enabled!\n");
786 if (!pf->pf_ih)
787 panic("pcmcia_intr_distestablish: already done\n");
788
789 pcmcia_chip_intr_disestablish(pf->sc->pct, pf->sc->pch, ih);
790 pf->pf_ih = 0;
791 }
792
793 int
794 pcmcia_config_alloc(pf, cfe)
795 struct pcmcia_function *pf;
796 struct pcmcia_config_entry *cfe;
797 {
798 int error = 0;
799 int n, m;
800
801 for (n = 0; n < cfe->num_iospace; n++) {
802 bus_addr_t start = cfe->iospace[n].start;
803 bus_size_t length = cfe->iospace[n].length;
804 bus_size_t align = cfe->iomask ? (1 << cfe->iomask) :
805 length;
806 bus_size_t skew = start & (align - 1);
807
808 if ((start - skew) == 0 && align < 0x400) {
809 if (skew)
810 printf("Drats! I need a skew!\n");
811 start = 0;
812 }
813
814 DPRINTF(("pcmcia_config_alloc: io %d start=%lx length=%lx align=%lx skew=%lx\n",
815 n, (long)start, (long)length, (long)align, (long)skew));
816
817 error = pcmcia_io_alloc(pf, start, length, align,
818 &cfe->iospace[n].handle);
819 if (error)
820 break;
821 }
822 if (n < cfe->num_iospace) {
823 for (m = 0; m < n; m++)
824 pcmcia_io_free(pf, &cfe->iospace[m].handle);
825 return (error);
826 }
827
828 for (n = 0; n < cfe->num_memspace; n++) {
829 bus_size_t length = cfe->memspace[n].length;
830
831 DPRINTF(("pcmcia_config_alloc: mem %d length %lx\n", n,
832 (long)length));
833
834 error = pcmcia_mem_alloc(pf, length, &cfe->memspace[n].handle);
835 if (error)
836 break;
837 }
838 if (n < cfe->num_memspace) {
839 for (m = 0; m < cfe->num_iospace; m++)
840 pcmcia_io_free(pf, &cfe->iospace[m].handle);
841 for (m = 0; m < n; m++)
842 pcmcia_mem_free(pf, &cfe->memspace[m].handle);
843 return (error);
844 }
845
846 /* This one's good! */
847 return (error);
848 }
849
850 void
851 pcmcia_config_free(pf)
852 struct pcmcia_function *pf;
853 {
854 struct pcmcia_config_entry *cfe = pf->cfe;
855 int m;
856
857 for (m = 0; m < cfe->num_iospace; m++)
858 pcmcia_io_free(pf, &cfe->iospace[m].handle);
859 for (m = 0; m < cfe->num_memspace; m++)
860 pcmcia_mem_free(pf, &cfe->memspace[m].handle);
861 }
862
863 int
864 pcmcia_config_map(pf)
865 struct pcmcia_function *pf;
866 {
867 struct pcmcia_config_entry *cfe = pf->cfe;
868 int error = 0;
869 int n, m;
870
871 for (n = 0; n < cfe->num_iospace; n++) {
872 int width;
873
874 if (cfe->flags & PCMCIA_CFE_IO16)
875 width = PCMCIA_WIDTH_AUTO;
876 else
877 width = PCMCIA_WIDTH_IO8;
878 error = pcmcia_io_map(pf, width, &cfe->iospace[n].handle,
879 &cfe->iospace[n].window);
880 if (error)
881 break;
882 }
883 if (n < cfe->num_iospace) {
884 for (m = 0; m < n; m++)
885 pcmcia_io_unmap(pf, cfe->iospace[m].window);
886 return (error);
887 }
888
889 for (n = 0; n < cfe->num_memspace; n++) {
890 bus_size_t length = cfe->memspace[n].length;
891 int width;
892
893 DPRINTF(("pcmcia_config_alloc: mem %d length %lx\n", n,
894 (long)length));
895
896 /*XXX*/
897 width = PCMCIA_WIDTH_MEM8|PCMCIA_MEM_COMMON;
898 error = pcmcia_mem_map(pf, width, 0, length,
899 &cfe->memspace[n].handle, &cfe->memspace[n].offset,
900 &cfe->memspace[n].window);
901 if (error)
902 break;
903 }
904 if (n < cfe->num_memspace) {
905 for (m = 0; m < cfe->num_iospace; m++)
906 pcmcia_io_unmap(pf, cfe->iospace[m].window);
907 for (m = 0; m < n; m++)
908 pcmcia_mem_unmap(pf, cfe->memspace[m].window);
909 return (error);
910 }
911
912 /* This one's good! */
913 return (error);
914 }
915
916 void
917 pcmcia_config_unmap(pf)
918 struct pcmcia_function *pf;
919 {
920 struct pcmcia_config_entry *cfe = pf->cfe;
921 int m;
922
923 for (m = 0; m < cfe->num_iospace; m++)
924 pcmcia_io_unmap(pf, cfe->iospace[m].window);
925 for (m = 0; m < cfe->num_memspace; m++)
926 pcmcia_mem_unmap(pf, cfe->memspace[m].window);
927 }
928
929 int
930 pcmcia_function_configure(pf, validator)
931 struct pcmcia_function *pf;
932 int (*validator)(struct pcmcia_config_entry *);
933 {
934 struct pcmcia_config_entry *cfe;
935 int error = ENOENT;
936
937 SIMPLEQ_FOREACH(cfe, &pf->cfe_head, cfe_list) {
938 error = validator(cfe);
939 if (error)
940 continue;
941 error = pcmcia_config_alloc(pf, cfe);
942 if (!error)
943 break;
944 }
945 if (!cfe) {
946 DPRINTF(("pcmcia_function_configure: no config entry found, error=%d\n",
947 error));
948 return (error);
949 }
950
951 /* Remember which configuration entry we are using. */
952 pf->cfe = cfe;
953
954 error = pcmcia_config_map(pf);
955 if (error) {
956 DPRINTF(("pcmcia_function_configure: map failed, error=%d\n",
957 error));
958 return (error);
959 }
960
961 return (0);
962 }
963
964 void
965 pcmcia_function_unconfigure(pf)
966 struct pcmcia_function *pf;
967 {
968
969 pcmcia_config_unmap(pf);
970 pcmcia_config_free(pf);
971 }
972