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