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