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