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