pcmcia.c revision 1.6 1 /* $NetBSD: pcmcia.c,v 1.6 1998/03/07 17:58:17 christos Exp $ */
2
3 #define PCMCIADEBUG
4
5 /*
6 * Copyright (c) 1997 Marc Horowitz. All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by Marc Horowitz.
19 * 4. The name of the author may not be used to endorse or promote products
20 * derived from this software without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
23 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
27 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
31 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 */
33
34 #include "opt_pcmciaverbose.h"
35
36 #include <sys/types.h>
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/device.h>
40
41 /* XXX only needed for intr debugging */
42 #include <vm/vm.h>
43
44 #include <dev/pcmcia/pcmciareg.h>
45 #include <dev/pcmcia/pcmciachip.h>
46 #include <dev/pcmcia/pcmciavar.h>
47
48 #include "locators.h"
49
50 #ifdef PCMCIADEBUG
51 int pcmcia_debug = 0;
52 #define DPRINTF(arg) if (pcmcia_debug) printf arg
53 #else
54 #define DPRINTF(arg)
55 #endif
56
57 #ifdef PCMCIAVERBOSE
58 int pcmcia_verbose = 1;
59 #else
60 int pcmcia_verbose = 0;
61 #endif
62
63 #ifdef __BROKEN_INDIRECT_CONFIG
64 int pcmcia_match __P((struct device *, void *, void *));
65 int pcmcia_submatch __P((struct device *, void *, void *));
66 #else
67 int pcmcia_match __P((struct device *, struct cfdata *, void *));
68 int pcmcia_submatch __P((struct device *, struct cfdata *, void *));
69 #endif
70 void pcmcia_attach __P((struct device *, struct device *, void *));
71 int pcmcia_print __P((void *, const char *));
72
73 static inline void pcmcia_socket_enable __P((pcmcia_chipset_tag_t,
74 pcmcia_chipset_handle_t *));
75 static inline void pcmcia_socket_disable __P((pcmcia_chipset_tag_t,
76 pcmcia_chipset_handle_t *));
77
78 int pcmcia_card_intr __P((void *));
79
80 struct cfattach pcmcia_ca = {
81 sizeof(struct pcmcia_softc), pcmcia_match, pcmcia_attach
82 };
83
84 int
85 pcmcia_ccr_read(pf, ccr)
86 struct pcmcia_function *pf;
87 int ccr;
88 {
89
90 return (bus_space_read_1(pf->pf_ccrt, pf->pf_ccrh,
91 pf->pf_ccr_offset + ccr));
92 }
93
94 void
95 pcmcia_ccr_write(pf, ccr, val)
96 struct pcmcia_function *pf;
97 int ccr;
98 int val;
99 {
100
101 if ((pf->ccr_mask) & (1 << (ccr / 2))) {
102 bus_space_write_1(pf->pf_ccrt, pf->pf_ccrh,
103 pf->pf_ccr_offset + ccr, val);
104 }
105 }
106
107 int
108 pcmcia_match(parent, match, aux)
109 struct device *parent;
110 #ifdef __BROKEN_INDIRECT_CONFIG
111 void *match;
112 #else
113 struct cfdata *match;
114 #endif
115 void *aux;
116 {
117 /* if the autoconfiguration got this far, there's a socket here */
118 return (1);
119 }
120
121 void
122 pcmcia_attach(parent, self, aux)
123 struct device *parent, *self;
124 void *aux;
125 {
126 struct pcmciabus_attach_args *paa = aux;
127 struct pcmcia_softc *sc = (struct pcmcia_softc *) self;
128
129 printf("\n");
130
131 sc->pct = paa->pct;
132 sc->pch = paa->pch;
133 sc->iobase = paa->iobase;
134 sc->iosize = paa->iosize;
135
136 sc->ih = NULL;
137 }
138
139 int
140 pcmcia_card_attach(dev)
141 struct device *dev;
142 {
143 struct pcmcia_softc *sc = (struct pcmcia_softc *) dev;
144 struct pcmcia_function *pf;
145 struct pcmcia_attach_args paa;
146 int attached;
147
148 /*
149 * this is here so that when socket_enable calls gettype, trt happens
150 */
151 sc->card.pf_head.sqh_first = NULL;
152
153 pcmcia_chip_socket_enable(sc->pct, sc->pch);
154
155 pcmcia_read_cis(sc);
156
157 pcmcia_chip_socket_disable(sc->pct, sc->pch);
158
159 /*
160 * bail now if the card has no functions, or if there was an error in
161 * the cis.
162 */
163
164 if (sc->card.error)
165 return (1);
166 if (sc->card.pf_head.sqh_first == NULL)
167 return (1);
168
169 if (pcmcia_verbose)
170 pcmcia_print_cis(sc);
171
172 attached = 0;
173
174 for (pf = sc->card.pf_head.sqh_first; pf != NULL;
175 pf = pf->pf_list.sqe_next) {
176 if (pf->cfe_head.sqh_first == NULL)
177 continue;
178
179 pf->sc = sc;
180 pf->cfe = NULL;
181 pf->ih_fct = NULL;
182 pf->ih_arg = NULL;
183 }
184
185 for (pf = sc->card.pf_head.sqh_first; pf != NULL;
186 pf = pf->pf_list.sqe_next) {
187 if (pf->cfe_head.sqh_first == NULL)
188 continue;
189
190 paa.manufacturer = sc->card.manufacturer;
191 paa.product = sc->card.product;
192 paa.card = &sc->card;
193 paa.pf = pf;
194
195 if (config_found_sm(&sc->dev, &paa, pcmcia_print,
196 pcmcia_submatch)) {
197 attached++;
198
199 DPRINTF(("%s: function %d CCR at %d "
200 "offset %lx: %x %x %x %x, %x %x %x %x, %x\n",
201 sc->dev.dv_xname, pf->number,
202 pf->pf_ccr_window, pf->pf_ccr_offset,
203 pcmcia_ccr_read(pf, 0x00),
204 pcmcia_ccr_read(pf, 0x02), pcmcia_ccr_read(pf, 0x04),
205 pcmcia_ccr_read(pf, 0x06), pcmcia_ccr_read(pf, 0x0A),
206 pcmcia_ccr_read(pf, 0x0C), pcmcia_ccr_read(pf, 0x0E),
207 pcmcia_ccr_read(pf, 0x10), pcmcia_ccr_read(pf, 0x12)));
208 }
209 }
210
211 return (attached ? 0 : 1);
212 }
213
214 void
215 pcmcia_card_detach(dev)
216 struct device *dev;
217 {
218 /* struct pcmcia_softc *sc = (struct pcmcia_softc *) dev; */
219 /* don't do anything yet */
220 }
221
222 int
223 #ifdef __BROKEN_INDIRECT_CONFIG
224 pcmcia_submatch(parent, match, aux)
225 struct device *parent;
226 void *match, *aux;
227 {
228 struct cfdata *cf = match;
229 #else
230 pcmcia_submatch(parent, cf, aux)
231 struct device *parent;
232 struct cfdata *cf;
233 void *aux;
234 {
235 #endif
236 struct pcmcia_attach_args *paa = aux;
237
238 if (cf->cf_loc[PCMCIACF_FUNCTION] != PCMCIACF_FUNCTION_DEFAULT &&
239 cf->cf_loc[PCMCIACF_FUNCTION] != paa->pf->number)
240 return (0);
241
242 return ((*cf->cf_attach->ca_match)(parent, cf, aux));
243 }
244
245 int
246 pcmcia_print(arg, pnp)
247 void *arg;
248 const char *pnp;
249 {
250 struct pcmcia_attach_args *pa = arg;
251 struct pcmcia_softc *sc = pa->pf->sc;
252 struct pcmcia_card *card = &sc->card;
253 int i;
254
255 if (pnp) {
256 for (i = 0; i < 4; i++) {
257 if (card->cis1_info[i] == NULL)
258 break;
259 if (i)
260 printf(", ");
261 printf("%s", card->cis1_info[i]);
262 }
263 if (i)
264 printf(" ");
265 printf("(manufacturer 0x%x, product 0x%x)", card->manufacturer,
266 card->product);
267 }
268 printf(" function %d", pa->pf->number);
269
270 return (UNCONF);
271 }
272
273 int
274 pcmcia_card_gettype(dev)
275 struct device *dev;
276 {
277 struct pcmcia_softc *sc = (struct pcmcia_softc *) dev;
278
279 /*
280 * set the iftype to memory if this card has no functions (not yet
281 * probed), or only one function, and that is memory.
282 */
283 if (sc->card.pf_head.sqh_first == NULL ||
284 (sc->card.pf_head.sqh_first != NULL &&
285 sc->card.pf_head.sqh_first->pf_list.sqe_next == NULL &&
286 (sc->card.pf_head.sqh_first->cfe_head.sqh_first->iftype ==
287 PCMCIA_IFTYPE_MEMORY)))
288 return (PCMCIA_IFTYPE_MEMORY);
289 else
290 return (PCMCIA_IFTYPE_IO);
291 }
292
293 /*
294 * Initialize a PCMCIA function. May be called as long as the function is
295 * disabled.
296 */
297 void
298 pcmcia_function_init(pf, cfe)
299 struct pcmcia_function *pf;
300 struct pcmcia_config_entry *cfe;
301 {
302 if (pf->pf_flags & PFF_ENABLED)
303 panic("pcmcia_function_init: function is enabled");
304
305 /* Remember which configuration entry we are using. */
306 pf->cfe = cfe;
307 }
308
309 static inline void pcmcia_socket_enable(pct, pch)
310 pcmcia_chipset_tag_t pct;
311 pcmcia_chipset_handle_t *pch;
312 {
313 pcmcia_chip_socket_enable(pct, pch);
314 }
315
316 static inline void pcmcia_socket_disable(pct, pch)
317 pcmcia_chipset_tag_t pct;
318 pcmcia_chipset_handle_t *pch;
319 {
320 pcmcia_chip_socket_disable(pct, pch);
321 }
322
323 /* Enable a PCMCIA function */
324 int
325 pcmcia_function_enable(pf)
326 struct pcmcia_function *pf;
327 {
328 struct pcmcia_function *tmp;
329 int reg;
330
331 if (pf->cfe == NULL)
332 panic("pcmcia_function_enable: function not initialized");
333
334 /*
335 * Increase the reference count on the socket, enabling power, if
336 * necessary.
337 */
338 if (pf->sc->sc_enabled_count++ == 0)
339 pcmcia_chip_socket_enable(pf->sc->pct, pf->sc->pch);
340 DPRINTF(("%s: ++enabled_count = %d\n", pf->sc->dev.dv_xname,
341 pf->sc->sc_enabled_count));
342
343 if (pf->pf_flags & PFF_ENABLED) {
344 /*
345 * Don't do anything if we're already enabled.
346 */
347 return (0);
348 }
349
350 /*
351 * it's possible for different functions' CCRs to be in the same
352 * underlying page. Check for that.
353 */
354
355 for (tmp = pf->sc->card.pf_head.sqh_first; tmp != NULL;
356 tmp = tmp->pf_list.sqe_next) {
357 if ((tmp->pf_flags & PFF_ENABLED) &&
358 (pf->ccr_base >= (tmp->ccr_base - tmp->pf_ccr_offset)) &&
359 ((pf->ccr_base + PCMCIA_CCR_SIZE) <=
360 (tmp->ccr_base - tmp->pf_ccr_offset +
361 tmp->pf_ccr_realsize))) {
362 pf->pf_ccrt = tmp->pf_ccrt;
363 pf->pf_ccrh = tmp->pf_ccrh;
364 pf->pf_ccr_realsize = tmp->pf_ccr_realsize;
365
366 /*
367 * pf->pf_ccr_offset = (tmp->pf_ccr_offset -
368 * tmp->ccr_base) + pf->ccr_base;
369 */
370 pf->pf_ccr_offset =
371 (tmp->pf_ccr_offset + pf->ccr_base) -
372 tmp->ccr_base;
373 pf->pf_ccr_window = tmp->pf_ccr_window;
374 break;
375 }
376 }
377
378 if (tmp == NULL) {
379 if (pcmcia_mem_alloc(pf, PCMCIA_CCR_SIZE, &pf->pf_pcmh))
380 goto bad;
381
382 if (pcmcia_mem_map(pf, PCMCIA_MEM_ATTR, pf->ccr_base,
383 PCMCIA_CCR_SIZE, &pf->pf_pcmh, &pf->pf_ccr_offset,
384 &pf->pf_ccr_window)) {
385 pcmcia_mem_free(pf, &pf->pf_pcmh);
386 goto bad;
387 }
388 }
389
390 reg = (pf->cfe->number & PCMCIA_CCR_OPTION_CFINDEX);
391 reg |= PCMCIA_CCR_OPTION_LEVIREQ;
392 if (pcmcia_mfc(pf->sc)) {
393 reg |= (PCMCIA_CCR_OPTION_FUNC_ENABLE |
394 PCMCIA_CCR_OPTION_ADDR_DECODE);
395 if (pf->ih_fct)
396 reg |= PCMCIA_CCR_OPTION_IREQ_ENABLE;
397
398 }
399 pcmcia_ccr_write(pf, PCMCIA_CCR_OPTION, reg);
400
401 reg = 0;
402
403 if ((pf->cfe->flags & PCMCIA_CFE_IO16) == 0)
404 reg |= PCMCIA_CCR_STATUS_IOIS8;
405 if (pf->cfe->flags & PCMCIA_CFE_AUDIO)
406 reg |= PCMCIA_CCR_STATUS_AUDIO;
407 pcmcia_ccr_write(pf, PCMCIA_CCR_STATUS, reg);
408
409 pcmcia_ccr_write(pf, PCMCIA_CCR_SOCKETCOPY, 0);
410
411 if (pcmcia_mfc(pf->sc)) {
412 long tmp, iosize;
413
414 tmp = pf->pf_mfc_iomax - pf->pf_mfc_iobase;
415 /* round up to nearest (2^n)-1 */
416 for (iosize = 1; iosize < tmp; iosize <<= 1)
417 ;
418 iosize--;
419
420 pcmcia_ccr_write(pf, PCMCIA_CCR_IOBASE0,
421 pf->pf_mfc_iobase & 0xff);
422 pcmcia_ccr_write(pf, PCMCIA_CCR_IOBASE1,
423 (pf->pf_mfc_iobase >> 8) & 0xff);
424 pcmcia_ccr_write(pf, PCMCIA_CCR_IOBASE2, 0);
425 pcmcia_ccr_write(pf, PCMCIA_CCR_IOBASE3, 0);
426
427 pcmcia_ccr_write(pf, PCMCIA_CCR_IOSIZE, iosize);
428 }
429
430 DPRINTF(("%s: function %d CCR at %d offset %lx: "
431 "%x %x %x %x, %x %x %x %x, %x\n",
432 pf->sc->dev.dv_xname, pf->number,
433 pf->pf_ccr_window, pf->pf_ccr_offset,
434 pcmcia_ccr_read(pf, 0x00), pcmcia_ccr_read(pf, 0x02),
435 pcmcia_ccr_read(pf, 0x04), pcmcia_ccr_read(pf, 0x06),
436
437 pcmcia_ccr_read(pf, 0x0A), pcmcia_ccr_read(pf, 0x0C),
438 pcmcia_ccr_read(pf, 0x0E), pcmcia_ccr_read(pf, 0x10),
439
440 pcmcia_ccr_read(pf, 0x12)));
441
442 pf->pf_flags |= PFF_ENABLED;
443 return (0);
444
445 bad:
446 /*
447 * Decrement the reference count, and power down the socket, if
448 * necessary.
449 */
450 if (--pf->sc->sc_enabled_count == 0)
451 pcmcia_chip_socket_disable(pf->sc->pct, pf->sc->pch);
452 DPRINTF(("%s: --enabled_count = %d\n", pf->sc->dev.dv_xname,
453 pf->sc->sc_enabled_count));
454
455 return (1);
456 }
457
458 /* Disable PCMCIA function. */
459 void
460 pcmcia_function_disable(pf)
461 struct pcmcia_function *pf;
462 {
463 struct pcmcia_function *tmp;
464
465 if (pf->cfe == NULL)
466 panic("pcmcia_function_enable: function not initialized");
467
468 if ((pf->pf_flags & PFF_ENABLED) == 0) {
469 /*
470 * Don't do anything if we're already disabled.
471 */
472 return;
473 }
474
475 /*
476 * it's possible for different functions' CCRs to be in the same
477 * underlying page. Check for that. Note we mark us as disabled
478 * first to avoid matching ourself.
479 */
480
481 pf->pf_flags &= ~PFF_ENABLED;
482 for (tmp = pf->sc->card.pf_head.sqh_first; tmp != NULL;
483 tmp = tmp->pf_list.sqe_next) {
484 if ((tmp->pf_flags & PFF_ENABLED) &&
485 (pf->ccr_base >= (tmp->ccr_base - tmp->pf_ccr_offset)) &&
486 ((pf->ccr_base + PCMCIA_CCR_SIZE) <=
487 (tmp->ccr_base - tmp->pf_ccr_offset + tmp->pf_ccr_realsize)))
488 break;
489 }
490
491 /* Not used by anyone else; unmap the CCR. */
492 if (tmp == NULL) {
493 pcmcia_mem_unmap(pf, pf->pf_ccr_window);
494 pcmcia_mem_free(pf, &pf->pf_pcmh);
495 }
496
497 /*
498 * Decrement the reference count, and power down the socket, if
499 * necessary.
500 */
501 if (--pf->sc->sc_enabled_count == 0)
502 pcmcia_chip_socket_disable(pf->sc->pct, pf->sc->pch);
503 DPRINTF(("%s: --enabled_count = %d\n", pf->sc->dev.dv_xname,
504 pf->sc->sc_enabled_count));
505 }
506
507 int
508 pcmcia_io_map(pf, width, offset, size, pcihp, windowp)
509 struct pcmcia_function *pf;
510 int width;
511 bus_addr_t offset;
512 bus_size_t size;
513 struct pcmcia_io_handle *pcihp;
514 int *windowp;
515 {
516 int reg;
517
518 if (pcmcia_chip_io_map(pf->sc->pct, pf->sc->pch,
519 width, offset, size, pcihp, windowp))
520 return (1);
521
522 /*
523 * XXX in the multifunction multi-iospace-per-function case, this
524 * needs to cooperate with io_alloc to make sure that the spaces
525 * don't overlap, and that the ccr's are set correctly
526 */
527
528 if (pcmcia_mfc(pf->sc)) {
529 long tmp, iosize;
530
531 if (pf->pf_mfc_iomax == 0) {
532 pf->pf_mfc_iobase = pcihp->addr + offset;
533 pf->pf_mfc_iomax = pf->pf_mfc_iobase + size;
534 } else {
535 /* this makes the assumption that nothing overlaps */
536 if (pf->pf_mfc_iobase > pcihp->addr + offset)
537 pf->pf_mfc_iobase = pcihp->addr + offset;
538 if (pf->pf_mfc_iomax < pcihp->addr + offset + size)
539 pf->pf_mfc_iomax = pcihp->addr + offset + size;
540 }
541
542 tmp = pf->pf_mfc_iomax - pf->pf_mfc_iobase;
543 /* round up to nearest (2^n)-1 */
544 for (iosize = 1; iosize >= tmp; iosize <<= 1)
545 ;
546 iosize--;
547
548 pcmcia_ccr_write(pf, PCMCIA_CCR_IOBASE0,
549 pf->pf_mfc_iobase & 0xff);
550 pcmcia_ccr_write(pf, PCMCIA_CCR_IOBASE1,
551 (pf->pf_mfc_iobase >> 8) & 0xff);
552 pcmcia_ccr_write(pf, PCMCIA_CCR_IOBASE2, 0);
553 pcmcia_ccr_write(pf, PCMCIA_CCR_IOBASE3, 0);
554
555 pcmcia_ccr_write(pf, PCMCIA_CCR_IOSIZE, iosize);
556
557 reg = pcmcia_ccr_read(pf, PCMCIA_CCR_OPTION);
558 reg |= PCMCIA_CCR_OPTION_ADDR_DECODE;
559 pcmcia_ccr_write(pf, PCMCIA_CCR_OPTION, reg);
560 }
561 return (0);
562 }
563
564 void *
565 pcmcia_intr_establish(pf, ipl, ih_fct, ih_arg)
566 struct pcmcia_function *pf;
567 int ipl;
568 int (*ih_fct) __P((void *));
569 void *ih_arg;
570 {
571 void *ret;
572
573 /* behave differently if this is a multifunction card */
574
575 if (pcmcia_mfc(pf->sc)) {
576 int s, ihcnt, hiipl, reg;
577 struct pcmcia_function *pf2;
578
579 /*
580 * mask all the ipl's which are already used by this card,
581 * and find the highest ipl number (lowest priority)
582 */
583
584 ihcnt = 0;
585 s = 0; /* this is only here to keep the compiler
586 happy */
587 hiipl = 0; /* this is only here to keep the compiler
588 happy */
589
590 for (pf2 = pf->sc->card.pf_head.sqh_first; pf2 != NULL;
591 pf2 = pf2->pf_list.sqe_next) {
592 if (pf2->ih_fct) {
593 DPRINTF(("%s: function %d has ih_fct %p\n",
594 pf->sc->dev.dv_xname, pf2->number,
595 pf2->ih_fct));
596
597 if (ihcnt == 0) {
598 hiipl = pf2->ih_ipl;
599 } else {
600 if (pf2->ih_ipl > hiipl)
601 hiipl = pf2->ih_ipl;
602 }
603
604 ihcnt++;
605 }
606 }
607
608 /*
609 * establish the real interrupt, changing the ipl if
610 * necessary
611 */
612
613 if (ihcnt == 0) {
614 #ifdef DIAGNOSTIC
615 if (pf->sc->ih != NULL)
616 panic("card has intr handler, but no function does");
617 #endif
618 s = splhigh();
619
620 /* set up the handler for the new function */
621
622 pf->ih_fct = ih_fct;
623 pf->ih_arg = ih_arg;
624 pf->ih_ipl = ipl;
625
626 pf->sc->ih = pcmcia_chip_intr_establish(pf->sc->pct,
627 pf->sc->pch, pf, ipl, pcmcia_card_intr, pf->sc);
628 splx(s);
629 } else if (ipl > hiipl) {
630 #ifdef DIAGNOSTIC
631 if (pf->sc->ih == NULL)
632 panic("functions have ih, but the card does not");
633 #endif
634
635 /* XXX need #ifdef for splserial on x86 */
636 s = splhigh();
637
638 pcmcia_chip_intr_disestablish(pf->sc->pct, pf->sc->pch,
639 pf->sc->ih);
640
641 /* set up the handler for the new function */
642 pf->ih_fct = ih_fct;
643 pf->ih_arg = ih_arg;
644 pf->ih_ipl = ipl;
645
646 pf->sc->ih = pcmcia_chip_intr_establish(pf->sc->pct,
647 pf->sc->pch, pf, ipl, pcmcia_card_intr, pf->sc);
648
649 splx(s);
650 } else {
651 s = splhigh();
652
653 /* set up the handler for the new function */
654
655 pf->ih_fct = ih_fct;
656 pf->ih_arg = ih_arg;
657 pf->ih_ipl = ipl;
658
659 splx(s);
660 }
661
662 ret = pf->sc->ih;
663
664 if (ret != NULL) {
665 reg = pcmcia_ccr_read(pf, PCMCIA_CCR_OPTION);
666 reg |= PCMCIA_CCR_OPTION_IREQ_ENABLE;
667 pcmcia_ccr_write(pf, PCMCIA_CCR_OPTION, reg);
668
669 reg = pcmcia_ccr_read(pf, PCMCIA_CCR_STATUS);
670 reg |= PCMCIA_CCR_STATUS_INTRACK;
671 pcmcia_ccr_write(pf, PCMCIA_CCR_STATUS, reg);
672 }
673 } else {
674 ret = pcmcia_chip_intr_establish(pf->sc->pct, pf->sc->pch,
675 pf, ipl, ih_fct, ih_arg);
676 }
677
678 return (ret);
679 }
680
681 void
682 pcmcia_intr_disestablish(pf, ih)
683 struct pcmcia_function *pf;
684 void *ih;
685 {
686 /* behave differently if this is a multifunction card */
687
688 if (pcmcia_mfc(pf->sc)) {
689 int s, ihcnt, hiipl;
690 struct pcmcia_function *pf2;
691
692 /*
693 * mask all the ipl's which are already used by this card,
694 * and find the highest ipl number (lowest priority). Skip
695 * the current function.
696 */
697
698 ihcnt = 0;
699 s = 0; /* this is only here to keep the compipler
700 happy */
701 hiipl = 0; /* this is only here to keep the compipler
702 happy */
703
704 for (pf2 = pf->sc->card.pf_head.sqh_first; pf2 != NULL;
705 pf2 = pf2->pf_list.sqe_next) {
706 if (pf2 == pf)
707 continue;
708
709 if (pf2->ih_fct) {
710 if (ihcnt == 0) {
711 hiipl = pf2->ih_ipl;
712 } else {
713 if (pf2->ih_ipl > hiipl)
714 hiipl = pf2->ih_ipl;
715 }
716 ihcnt++;
717 }
718 }
719
720 /*
721 * if the ih being removed is lower priority than the lowest
722 * priority remaining interrupt, up the priority.
723 */
724
725 /* ihcnt is the number of interrupt handlers *not* including
726 the one about to be removed. */
727
728 if (ihcnt == 0) {
729 int reg;
730
731 #ifdef DIAGNOSTIC
732 if (pf->sc->ih == NULL)
733 panic("disestablishing last function, but card has no ih");
734 #endif
735 pcmcia_chip_intr_disestablish(pf->sc->pct, pf->sc->pch,
736 pf->sc->ih);
737
738 reg = pcmcia_ccr_read(pf, PCMCIA_CCR_OPTION);
739 reg &= ~PCMCIA_CCR_OPTION_IREQ_ENABLE;
740 pcmcia_ccr_write(pf, PCMCIA_CCR_OPTION, reg);
741
742 pf->ih_fct = NULL;
743 pf->ih_arg = NULL;
744
745 pf->sc->ih = NULL;
746 } else if (pf->ih_ipl > hiipl) {
747 #ifdef DIAGNOSTIC
748 if (pf->sc->ih == NULL)
749 panic("changing ih ipl, but card has no ih");
750 #endif
751 /* XXX need #ifdef for splserial on x86 */
752 s = splhigh();
753
754 pcmcia_chip_intr_disestablish(pf->sc->pct, pf->sc->pch,
755 pf->sc->ih);
756 pf->sc->ih = pcmcia_chip_intr_establish(pf->sc->pct,
757 pf->sc->pch, pf, hiipl, pcmcia_card_intr, pf->sc);
758
759 /* null out the handler for this function */
760
761 pf->ih_fct = NULL;
762 pf->ih_arg = NULL;
763
764 splx(s);
765 } else {
766 s = splhigh();
767
768 pf->ih_fct = NULL;
769 pf->ih_arg = NULL;
770
771 splx(s);
772 }
773 } else {
774 pcmcia_chip_intr_disestablish(pf->sc->pct, pf->sc->pch, ih);
775 }
776 }
777
778 int
779 pcmcia_card_intr(arg)
780 void *arg;
781 {
782 struct pcmcia_softc *sc = arg;
783 struct pcmcia_function *pf;
784 int reg, ret, ret2;
785
786 ret = 0;
787
788 for (pf = sc->card.pf_head.sqh_first; pf != NULL;
789 pf = pf->pf_list.sqe_next) {
790 #if 0
791 printf("%s: intr flags=%x fct=%d physaddr=%lx cor=%02x csr=%02x pin=%02x",
792 sc->dev.dv_xname, pf->pf_flags, pf->number,
793 pmap_extract(pmap_kernel(),
794 (vm_offset_t) pf->pf_ccrh) + pf->pf_ccr_offset,
795 pcmcia_ccr_read(pf, PCMCIA_CCR_OPTION),
796 pcmcia_ccr_read(pf, PCMCIA_CCR_STATUS),
797 pcmcia_ccr_read(pf, PCMCIA_CCR_PIN));
798 #endif
799 if (pf->ih_fct != NULL &&
800 (pf->ccr_mask & (1 << (PCMCIA_CCR_STATUS / 2)))) {
801 reg = pcmcia_ccr_read(pf, PCMCIA_CCR_STATUS);
802 if (reg & PCMCIA_CCR_STATUS_INTR) {
803 ret2 = (*pf->ih_fct)(pf->ih_arg);
804 if (ret2 != 0 && ret == 0)
805 ret = ret2;
806 reg = pcmcia_ccr_read(pf, PCMCIA_CCR_STATUS);
807 #if 0
808 printf("; csr %02x->%02x",
809 reg, reg & ~PCMCIA_CCR_STATUS_INTR);
810 #endif
811 pcmcia_ccr_write(pf, PCMCIA_CCR_STATUS,
812 reg & ~PCMCIA_CCR_STATUS_INTR);
813 }
814 }
815 #if 0
816 printf("\n");
817 #endif
818 }
819
820 return (ret);
821 }
822