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