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