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