pcmcia.c revision 1.55 1 /* $NetBSD: pcmcia.c,v 1.55 2004/08/10 15:29:56 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.55 2004/08/10 15:29:56 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;
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_chip_socket_enable(sc->pct, sc->pch);
160
161 pcmcia_read_cis(sc);
162
163 pcmcia_chip_socket_disable(sc->pct, sc->pch);
164
165 pcmcia_check_cis_quirks(sc);
166
167 /*
168 * bail now if the card has no functions, or if there was an error in
169 * the cis.
170 */
171
172 if (sc->card.error)
173 return (1);
174 if (SIMPLEQ_EMPTY(&sc->card.pf_head))
175 return (1);
176
177 if (pcmcia_verbose)
178 pcmcia_print_cis(sc);
179
180 attached = 0;
181
182 SIMPLEQ_FOREACH(pf, &sc->card.pf_head, pf_list) {
183 if (SIMPLEQ_EMPTY(&pf->cfe_head))
184 continue;
185
186 #ifdef DIAGNOSTIC
187 if (pf->child != NULL) {
188 printf("%s: %s still attached to function %d!\n",
189 sc->dev.dv_xname, pf->child->dv_xname,
190 pf->number);
191 panic("pcmcia_card_attach");
192 }
193 #endif
194 pf->sc = sc;
195 pf->child = NULL;
196 pf->cfe = NULL;
197 pf->pf_ih = NULL;
198 }
199
200 SIMPLEQ_FOREACH(pf, &sc->card.pf_head, pf_list) {
201 if (SIMPLEQ_EMPTY(&pf->cfe_head))
202 continue;
203
204 paa.manufacturer = sc->card.manufacturer;
205 paa.product = sc->card.product;
206 paa.card = &sc->card;
207 paa.pf = pf;
208
209 if ((pf->child = config_found_sm(&sc->dev, &paa, pcmcia_print,
210 pcmcia_submatch)) != NULL)
211 attached++;
212 }
213
214 return (attached ? 0 : 1);
215 }
216
217 void
218 pcmcia_card_detach(dev, flags)
219 struct device *dev;
220 int flags; /* DETACH_* flags */
221 {
222 struct pcmcia_softc *sc = (struct pcmcia_softc *) dev;
223 struct pcmcia_function *pf;
224 int error;
225
226 /*
227 * We are running on either the PCMCIA socket's event thread
228 * or in user context detaching a device by user request.
229 */
230 SIMPLEQ_FOREACH(pf, &sc->card.pf_head, pf_list) {
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", 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 struct pcmcia_product *
348 pcmcia_product_lookup(pa, tab, ent_size, matchfn)
349 struct pcmcia_attach_args *pa;
350 const struct pcmcia_product *tab;
351 size_t ent_size;
352 pcmcia_product_match_fn matchfn;
353 {
354 const struct pcmcia_product *ent;
355 int matches;
356
357 #ifdef DIAGNOSTIC
358 if (sizeof *ent > ent_size)
359 panic("pcmcia_product_lookup: bogus ent_size %ld",
360 (long) ent_size);
361 #endif
362
363 for (ent = tab;
364 ent->pp_name != NULL;
365 ent = (const struct pcmcia_product *)
366 ((const char *)ent + ent_size)) {
367
368 /* see if it matches vendor/product/function */
369 matches = (pa->manufacturer == ent->pp_vendor) &&
370 (pa->product == ent->pp_product) &&
371 (pa->pf->number == ent->pp_expfunc);
372
373 /* if a separate match function is given, let it override */
374 if (matchfn != NULL)
375 matches = (*matchfn)(pa, ent, matches);
376
377 if (matches)
378 return (ent);
379 }
380 return (NULL);
381 }
382
383 int
384 pcmcia_card_gettype(dev)
385 struct device *dev;
386 {
387 struct pcmcia_softc *sc = (struct pcmcia_softc *)dev;
388 struct pcmcia_function *pf;
389
390 /*
391 * set the iftype to memory if this card has no functions (not yet
392 * probed), or only one function, and that is not initialized yet or
393 * that is memory.
394 */
395 pf = SIMPLEQ_FIRST(&sc->card.pf_head);
396 if (pf == NULL ||
397 (SIMPLEQ_NEXT(pf, pf_list) == NULL &&
398 (pf->cfe == NULL || pf->cfe->iftype == PCMCIA_IFTYPE_MEMORY)))
399 return (PCMCIA_IFTYPE_MEMORY);
400 else
401 return (PCMCIA_IFTYPE_IO);
402 }
403
404 /*
405 * Initialize a PCMCIA function. May be called as long as the function is
406 * disabled.
407 */
408 void
409 pcmcia_function_init(pf, cfe)
410 struct pcmcia_function *pf;
411 struct pcmcia_config_entry *cfe;
412 {
413 if (pf->pf_flags & PFF_ENABLED)
414 panic("pcmcia_function_init: function is enabled");
415
416 /* Remember which configuration entry we are using. */
417 pf->cfe = cfe;
418 }
419
420 void pcmcia_socket_enable(dev)
421 struct device *dev;
422 {
423 struct pcmcia_softc *sc = (void *)dev;
424
425 pcmcia_chip_socket_enable(sc->pct, sc->pch);
426 }
427
428 void pcmcia_socket_disable(dev)
429 struct device *dev;
430 {
431 struct pcmcia_softc *sc = (void *)dev;
432
433 pcmcia_chip_socket_disable(sc->pct, sc->pch);
434 }
435
436 /* Enable a PCMCIA function */
437 int
438 pcmcia_function_enable(pf)
439 struct pcmcia_function *pf;
440 {
441 struct pcmcia_softc *sc = pf->sc;
442 struct pcmcia_function *tmp;
443 int reg;
444 int error;
445
446 if (pf->cfe == NULL)
447 panic("pcmcia_function_enable: function not initialized");
448
449 /*
450 * Increase the reference count on the socket, enabling power, if
451 * necessary.
452 */
453 if (sc->sc_enabled_count++ == 0)
454 pcmcia_chip_socket_enable(sc->pct, sc->pch);
455 DPRINTF(("%s: ++enabled_count = %d\n", sc->dev.dv_xname,
456 sc->sc_enabled_count));
457
458 if (pf->pf_flags & PFF_ENABLED) {
459 /*
460 * Don't do anything if we're already enabled.
461 */
462 return (0);
463 }
464
465 /*
466 * it's possible for different functions' CCRs to be in the same
467 * underlying page. Check for that.
468 */
469
470 SIMPLEQ_FOREACH(tmp, &sc->card.pf_head, pf_list) {
471 if ((tmp->pf_flags & PFF_ENABLED) &&
472 (pf->ccr_base >= (tmp->ccr_base - tmp->pf_ccr_offset)) &&
473 ((pf->ccr_base + PCMCIA_CCR_SIZE) <=
474 (tmp->ccr_base - tmp->pf_ccr_offset +
475 tmp->pf_ccr_realsize))) {
476 pf->pf_ccrt = tmp->pf_ccrt;
477 pf->pf_ccrh = tmp->pf_ccrh;
478 pf->pf_ccr_realsize = tmp->pf_ccr_realsize;
479
480 /*
481 * pf->pf_ccr_offset = (tmp->pf_ccr_offset -
482 * tmp->ccr_base) + pf->ccr_base;
483 */
484 pf->pf_ccr_offset =
485 (tmp->pf_ccr_offset + pf->ccr_base) -
486 tmp->ccr_base;
487 pf->pf_ccr_window = tmp->pf_ccr_window;
488 break;
489 }
490 }
491
492 if (tmp == NULL) {
493 error = pcmcia_mem_alloc(pf, PCMCIA_CCR_SIZE, &pf->pf_pcmh);
494 if (error)
495 goto bad;
496
497 error = pcmcia_mem_map(pf, PCMCIA_MEM_ATTR, pf->ccr_base,
498 PCMCIA_CCR_SIZE, &pf->pf_pcmh, &pf->pf_ccr_offset,
499 &pf->pf_ccr_window);
500 if (error) {
501 pcmcia_mem_free(pf, &pf->pf_pcmh);
502 goto bad;
503 }
504 }
505
506 if (pcmcia_mfc(sc)) {
507 pcmcia_ccr_write(pf, PCMCIA_CCR_IOBASE0,
508 (pf->pf_mfc_iobase[0] >> 0) & 0xff);
509 pcmcia_ccr_write(pf, PCMCIA_CCR_IOBASE1,
510 (pf->pf_mfc_iobase[0] >> 8) & 0xff);
511 pcmcia_ccr_write(pf, PCMCIA_CCR_IOBASE2,
512 (pf->pf_mfc_iobase[1] >> 0) & 0xff);
513 pcmcia_ccr_write(pf, PCMCIA_CCR_IOBASE3,
514 (pf->pf_mfc_iobase[1] >> 8) & 0xff);
515 pcmcia_ccr_write(pf, PCMCIA_CCR_IOSIZE, pf->pf_mfc_iomask);
516 }
517
518 reg = (pf->cfe->number & PCMCIA_CCR_OPTION_CFINDEX);
519 reg |= PCMCIA_CCR_OPTION_LEVIREQ;
520 if (pcmcia_mfc(sc)) {
521 reg |= (PCMCIA_CCR_OPTION_FUNC_ENABLE |
522 PCMCIA_CCR_OPTION_ADDR_DECODE);
523 if (pf->pf_ih)
524 reg |= PCMCIA_CCR_OPTION_IREQ_ENABLE;
525
526 }
527 pcmcia_ccr_write(pf, PCMCIA_CCR_OPTION, reg);
528
529 reg = 0;
530 if ((pf->cfe->flags & PCMCIA_CFE_IO16) == 0)
531 reg |= PCMCIA_CCR_STATUS_IOIS8;
532 if (pf->cfe->flags & PCMCIA_CFE_AUDIO)
533 reg |= PCMCIA_CCR_STATUS_AUDIO;
534 pcmcia_ccr_write(pf, PCMCIA_CCR_STATUS, reg);
535
536 pcmcia_ccr_write(pf, PCMCIA_CCR_SOCKETCOPY, 0);
537
538 #ifdef PCMCIADEBUG
539 if (pcmcia_debug) {
540 SIMPLEQ_FOREACH(tmp, &sc->card.pf_head, pf_list) {
541 printf("%s: function %d CCR at %d offset %lx: "
542 "%x %x %x %x, %x %x %x %x, %x\n",
543 tmp->sc->dev.dv_xname, tmp->number,
544 tmp->pf_ccr_window,
545 (unsigned long) tmp->pf_ccr_offset,
546 pcmcia_ccr_read(tmp, 0),
547 pcmcia_ccr_read(tmp, 1),
548 pcmcia_ccr_read(tmp, 2),
549 pcmcia_ccr_read(tmp, 3),
550
551 pcmcia_ccr_read(tmp, 5),
552 pcmcia_ccr_read(tmp, 6),
553 pcmcia_ccr_read(tmp, 7),
554 pcmcia_ccr_read(tmp, 8),
555
556 pcmcia_ccr_read(tmp, 9));
557 }
558 }
559 #endif
560
561 pf->pf_flags |= PFF_ENABLED;
562
563 #ifdef IT8368E_LEGACY_MODE
564 /* return to I/O mode */
565 it8368_mode(pf, IT8368_IO_MODE, IT8368_WIDTH_16);
566 #endif
567 return (0);
568
569 bad:
570 /*
571 * Decrement the reference count, and power down the socket, if
572 * necessary.
573 */
574 if (--sc->sc_enabled_count == 0)
575 pcmcia_chip_socket_disable(sc->pct, sc->pch);
576 DPRINTF(("%s: --enabled_count = %d\n", sc->dev.dv_xname,
577 sc->sc_enabled_count));
578 printf("%s: couldn't map the CCR\n", pf->child->dv_xname);
579
580 return (error);
581 }
582
583 /* Disable PCMCIA function. */
584 void
585 pcmcia_function_disable(pf)
586 struct pcmcia_function *pf;
587 {
588 struct pcmcia_function *tmp;
589 int reg;
590
591 if (pf->cfe == NULL)
592 panic("pcmcia_function_enable: function not initialized");
593
594 if ((pf->pf_flags & PFF_ENABLED) == 0) {
595 /*
596 * Don't do anything but decrement if we're already disabled.
597 */
598 goto out;
599 }
600
601 if (pcmcia_mfc(pf->sc)) {
602 reg = pcmcia_ccr_read(pf, PCMCIA_CCR_OPTION);
603 reg &= ~(PCMCIA_CCR_OPTION_FUNC_ENABLE|
604 PCMCIA_CCR_OPTION_ADDR_DECODE|
605 PCMCIA_CCR_OPTION_IREQ_ENABLE);
606 pcmcia_ccr_write(pf, PCMCIA_CCR_OPTION, reg);
607 }
608
609 /*
610 * it's possible for different functions' CCRs to be in the same
611 * underlying page. Check for that. Note we mark us as disabled
612 * first to avoid matching ourself.
613 */
614
615 pf->pf_flags &= ~PFF_ENABLED;
616 SIMPLEQ_FOREACH(tmp, &pf->sc->card.pf_head, pf_list) {
617 if ((tmp->pf_flags & PFF_ENABLED) &&
618 (pf->ccr_base >= (tmp->ccr_base - tmp->pf_ccr_offset)) &&
619 ((pf->ccr_base + PCMCIA_CCR_SIZE) <=
620 (tmp->ccr_base - tmp->pf_ccr_offset + tmp->pf_ccr_realsize)))
621 break;
622 }
623
624 /* Not used by anyone else; unmap the CCR. */
625 if (tmp == NULL) {
626 pcmcia_mem_unmap(pf, pf->pf_ccr_window);
627 pcmcia_mem_free(pf, &pf->pf_pcmh);
628 }
629
630 out:
631 /*
632 * Decrement the reference count, and power down the socket, if
633 * necessary.
634 */
635 if (--pf->sc->sc_enabled_count == 0)
636 pcmcia_chip_socket_disable(pf->sc->pct, pf->sc->pch);
637 DPRINTF(("%s: --enabled_count = %d\n", pf->sc->dev.dv_xname,
638 pf->sc->sc_enabled_count));
639 }
640
641 int
642 pcmcia_io_map(pf, width, pcihp, windowp)
643 struct pcmcia_function *pf;
644 int width;
645 struct pcmcia_io_handle *pcihp;
646 int *windowp;
647 {
648 int error;
649
650 if (pf->pf_flags & PFF_ENABLED)
651 printf("pcmcia_io_map: function is enabled!\n");
652
653 error = pcmcia_chip_io_map(pf->sc->pct, pf->sc->pch,
654 width, 0, pcihp->size, pcihp, windowp);
655 if (error)
656 return (error);
657
658 /*
659 * XXX in the multifunction multi-iospace-per-function case, this
660 * needs to cooperate with io_alloc to make sure that the spaces
661 * don't overlap, and that the ccr's are set correctly
662 */
663
664 if (pcmcia_mfc(pf->sc)) {
665 int win;
666 long iomask;
667
668 /* round up to nearest (2^n)-1 */
669 for (iomask = 1; iomask < pcihp->size; iomask <<= 1)
670 ;
671 iomask--;
672
673 win = pf->pf_mfc_windows;
674 KASSERT(win < 2);
675 printf("win %d = addr %lx size %lx iomask %lx\n", win, (long)pcihp->addr, (long)pcihp->size, (long)iomask);
676 pf->pf_mfc_iobase[win] = pcihp->addr;
677 pf->pf_mfc_iomask = iomask;
678 }
679
680 return (0);
681 }
682
683 void
684 pcmcia_io_unmap(pf, window)
685 struct pcmcia_function *pf;
686 int window;
687 {
688
689 if (pf->pf_flags & PFF_ENABLED)
690 printf("pcmcia_io_unmap: function is enabled!\n");
691
692 if (pcmcia_mfc(pf->sc)) {
693 /*
694 * Don't bother trying to unmap windows in the CCR here,
695 * because we generally get called when a card has been
696 * ejected, and the CCR isn't there any more.
697 */
698 --pf->pf_mfc_windows;
699 }
700
701 pcmcia_chip_io_unmap(pf->sc->pct, pf->sc->pch, window);
702 }
703
704 void *
705 pcmcia_intr_establish(pf, ipl, ih_fct, ih_arg)
706 struct pcmcia_function *pf;
707 int ipl;
708 int (*ih_fct) __P((void *));
709 void *ih_arg;
710 {
711
712 if (pf->pf_flags & PFF_ENABLED)
713 printf("pcmcia_intr_establish: function is enabled!\n");
714 if (pf->pf_ih)
715 panic("pcmcia_intr_establish: already done\n");
716
717 pf->pf_ih = pcmcia_chip_intr_establish(pf->sc->pct, pf->sc->pch,
718 pf, ipl, ih_fct, ih_arg);
719 if (!pf->pf_ih)
720 printf("%s: interrupt establish failed\n", pf->child->dv_xname);
721 return (pf->pf_ih);
722 }
723
724 void
725 pcmcia_intr_disestablish(pf, ih)
726 struct pcmcia_function *pf;
727 void *ih;
728 {
729
730 if (pf->pf_flags & PFF_ENABLED)
731 printf("pcmcia_intr_disestablish: function is enabled!\n");
732 if (!pf->pf_ih)
733 panic("pcmcia_intr_distestablish: already done\n");
734
735 pcmcia_chip_intr_disestablish(pf->sc->pct, pf->sc->pch, ih);
736 pf->pf_ih = 0;
737 }
738
739 int
740 pcmcia_config_alloc(pf, cfe)
741 struct pcmcia_function *pf;
742 struct pcmcia_config_entry *cfe;
743 {
744 int error;
745 int n, m;
746
747 for (n = 0; n < cfe->num_iospace; n++) {
748 bus_addr_t start = cfe->iospace[n].start;
749 bus_size_t length = cfe->iospace[n].length;
750 bus_size_t align = cfe->iomask ? (1 << cfe->iomask) :
751 length;
752 bus_size_t skew = start & (align - 1);
753
754 if ((start - skew) == 0 && align < 0x400) {
755 if (skew)
756 printf("Drats! I need a skew!\n");
757 start = 0;
758 }
759
760 DPRINTF(("pcmcia_config_alloc: io %d start=%lx length=%lx align=%lx skew=%lx\n",
761 n, (long)start, (long)length, (long)align, (long)skew));
762
763 error = pcmcia_io_alloc(pf, start, length, align,
764 &cfe->iospace[n].handle);
765 if (error)
766 break;
767 }
768 if (n < cfe->num_iospace) {
769 for (m = 0; m < n; m++)
770 pcmcia_io_free(pf, &cfe->iospace[m].handle);
771 return (error);
772 }
773
774 for (n = 0; n < cfe->num_memspace; n++) {
775 bus_size_t length = cfe->memspace[n].length;
776
777 DPRINTF(("pcmcia_config_alloc: mem %d length %lx\n", n,
778 (long)length));
779
780 error = pcmcia_mem_alloc(pf, length, &cfe->memspace[n].handle);
781 if (error)
782 break;
783 }
784 if (n < cfe->num_memspace) {
785 for (m = 0; m < cfe->num_iospace; m++)
786 pcmcia_io_free(pf, &cfe->iospace[m].handle);
787 for (m = 0; m < n; m++)
788 pcmcia_mem_free(pf, &cfe->memspace[m].handle);
789 return (error);
790 }
791
792 /* This one's good! */
793 return (0);
794 }
795
796 void
797 pcmcia_config_free(pf)
798 struct pcmcia_function *pf;
799 {
800 struct pcmcia_config_entry *cfe = pf->cfe;
801 int m;
802
803 for (m = 0; m < cfe->num_iospace; m++)
804 pcmcia_io_free(pf, &cfe->iospace[m].handle);
805 for (m = 0; m < cfe->num_memspace; m++)
806 pcmcia_mem_free(pf, &cfe->memspace[m].handle);
807 }
808
809 int
810 pcmcia_config_map(pf)
811 struct pcmcia_function *pf;
812 {
813 struct pcmcia_config_entry *cfe = pf->cfe;
814 int error;
815 int n, m;
816
817 for (n = 0; n < cfe->num_iospace; n++) {
818 int width;
819
820 switch (cfe->flags & (PCMCIA_CFE_IO8|PCMCIA_CFE_IO16)) {
821 case PCMCIA_CFE_IO8:
822 width = PCMCIA_WIDTH_IO8;
823 break;
824 case PCMCIA_CFE_IO16:
825 width = PCMCIA_WIDTH_IO16;
826 break;
827 case PCMCIA_CFE_IO8|PCMCIA_CFE_IO16:
828 default:
829 width = PCMCIA_WIDTH_AUTO;
830 break;
831 }
832 error = pcmcia_io_map(pf, width, &cfe->iospace[n].handle,
833 &cfe->iospace[n].window);
834 if (error)
835 break;
836 }
837 if (n < cfe->num_iospace) {
838 for (m = 0; m < n; m++)
839 pcmcia_io_unmap(pf, cfe->iospace[m].window);
840 return (error);
841 }
842
843 for (n = 0; n < cfe->num_memspace; n++) {
844 bus_size_t length = cfe->memspace[n].length;
845 int width;
846
847 DPRINTF(("pcmcia_config_alloc: mem %d length %lx\n", n,
848 (long)length));
849
850 /*XXX*/
851 width = PCMCIA_WIDTH_MEM8|PCMCIA_MEM_COMMON;
852 error = pcmcia_mem_map(pf, width, 0, length,
853 &cfe->memspace[n].handle, &cfe->memspace[n].offset,
854 &cfe->memspace[n].window);
855 if (error)
856 break;
857 }
858 if (n < cfe->num_memspace) {
859 for (m = 0; m < cfe->num_iospace; m++)
860 pcmcia_io_unmap(pf, cfe->iospace[m].window);
861 for (m = 0; m < n; m++)
862 pcmcia_mem_unmap(pf, cfe->memspace[m].window);
863 return (error);
864 }
865
866 /* This one's good! */
867 return (0);
868 }
869
870 void
871 pcmcia_config_unmap(pf)
872 struct pcmcia_function *pf;
873 {
874 struct pcmcia_config_entry *cfe = pf->cfe;
875 int m;
876
877 for (m = 0; m < cfe->num_iospace; m++)
878 pcmcia_io_unmap(pf, cfe->iospace[m].window);
879 for (m = 0; m < cfe->num_memspace; m++)
880 pcmcia_mem_unmap(pf, cfe->memspace[m].window);
881 }
882
883 int
884 pcmcia_function_configure(pf, validator)
885 struct pcmcia_function *pf;
886 int (*validator) __P((struct pcmcia_config_entry *));
887 {
888 struct pcmcia_config_entry *cfe;
889 int error = ENOENT;
890
891 SIMPLEQ_FOREACH(cfe, &pf->cfe_head, cfe_list) {
892 error = validator(cfe);
893 if (error)
894 continue;
895 error = pcmcia_config_alloc(pf, cfe);
896 if (!error)
897 break;
898 }
899 if (!cfe) {
900 DPRINTF(("pcmcia_function_configure: no config entry found, error=%d\n",
901 error));
902 return (error);
903 }
904
905 /* Remember which configuration entry we are using. */
906 pf->cfe = cfe;
907
908 error = pcmcia_config_map(pf);
909 if (error) {
910 DPRINTF(("pcmcia_function_configure: map failed, error=%d\n",
911 error));
912 return (error);
913 }
914
915 return (0);
916 }
917
918 void
919 pcmcia_function_unconfigure(pf)
920 struct pcmcia_function *pf;
921 {
922
923 pcmcia_config_unmap(pf);
924 pcmcia_config_free(pf);
925 }
926