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