pcmcia.c revision 1.19 1 /* $NetBSD: pcmcia.c,v 1.19 2000/02/08 12:51:30 enami 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 int showhex;
320 char *cp;
321 int cplen;
322 {
323 int i, n;
324
325 for (i = 0; i < 4 && card->cis1_info[i] != NULL && cplen > 0; i++) {
326 n = snprintf(cp, cplen, "%s%s", i ? ", " : "",
327 card->cis1_info[i]);
328 cp += n;
329 cplen -= n;
330 }
331 if (showhex && cplen > 0)
332 snprintf(cp, cplen, "%s(manufacturer 0x%04x, product 0x%04x)",
333 i ? " " : "", card->manufacturer, card->product);
334 }
335
336 const struct pcmcia_product *
337 pcmcia_product_lookup(pa, tab, ent_size, matchfn)
338 struct pcmcia_attach_args *pa;
339 const struct pcmcia_product *tab;
340 size_t ent_size;
341 pcmcia_product_match_fn matchfn;
342 {
343 const struct pcmcia_product *ent;
344 int matches;
345
346 #ifdef DIAGNOSTIC
347 if (sizeof *ent > ent_size)
348 panic("pcmcia_product_lookup: bogus ent_size %ld",
349 (long) ent_size);
350 #endif
351
352 for (ent = tab;
353 ent->pp_name != NULL;
354 ent = (const struct pcmcia_product *)
355 ((const char *)ent + ent_size)) {
356
357 /* see if it matches vendor/product/function */
358 matches = (pa->manufacturer == ent->pp_vendor) &&
359 (pa->product == ent->pp_product) &&
360 (pa->pf->number == ent->pp_expfunc);
361
362 /* if a separate match function is given, let it override */
363 if (matchfn != NULL)
364 matches = (*matchfn)(pa, ent, matches);
365
366 if (matches)
367 return (ent);
368 }
369 return (NULL);
370 }
371
372 int
373 pcmcia_card_gettype(dev)
374 struct device *dev;
375 {
376 struct pcmcia_softc *sc = (struct pcmcia_softc *)dev;
377 struct pcmcia_function *pf;
378
379 /*
380 * set the iftype to memory if this card has no functions (not yet
381 * probed), or only one function, and that is not initialized yet or
382 * that is memory.
383 */
384 pf = SIMPLEQ_FIRST(&sc->card.pf_head);
385 if (pf == NULL ||
386 (SIMPLEQ_NEXT(pf, pf_list) == NULL &&
387 (pf->cfe == NULL || pf->cfe->iftype == PCMCIA_IFTYPE_MEMORY)))
388 return (PCMCIA_IFTYPE_MEMORY);
389 else
390 return (PCMCIA_IFTYPE_IO);
391 }
392
393 /*
394 * Initialize a PCMCIA function. May be called as long as the function is
395 * disabled.
396 */
397 void
398 pcmcia_function_init(pf, cfe)
399 struct pcmcia_function *pf;
400 struct pcmcia_config_entry *cfe;
401 {
402 if (pf->pf_flags & PFF_ENABLED)
403 panic("pcmcia_function_init: function is enabled");
404
405 /* Remember which configuration entry we are using. */
406 pf->cfe = cfe;
407 }
408
409 static inline void pcmcia_socket_enable(pct, pch)
410 pcmcia_chipset_tag_t pct;
411 pcmcia_chipset_handle_t *pch;
412 {
413 pcmcia_chip_socket_enable(pct, pch);
414 }
415
416 static inline void pcmcia_socket_disable(pct, pch)
417 pcmcia_chipset_tag_t pct;
418 pcmcia_chipset_handle_t *pch;
419 {
420 pcmcia_chip_socket_disable(pct, pch);
421 }
422
423 /* Enable a PCMCIA function */
424 int
425 pcmcia_function_enable(pf)
426 struct pcmcia_function *pf;
427 {
428 struct pcmcia_function *tmp;
429 int reg;
430
431 if (pf->cfe == NULL)
432 panic("pcmcia_function_enable: function not initialized");
433
434 /*
435 * Increase the reference count on the socket, enabling power, if
436 * necessary.
437 */
438 if (pf->sc->sc_enabled_count++ == 0)
439 pcmcia_chip_socket_enable(pf->sc->pct, pf->sc->pch);
440 DPRINTF(("%s: ++enabled_count = %d\n", pf->sc->dev.dv_xname,
441 pf->sc->sc_enabled_count));
442
443 if (pf->pf_flags & PFF_ENABLED) {
444 /*
445 * Don't do anything if we're already enabled.
446 */
447 return (0);
448 }
449
450 /*
451 * it's possible for different functions' CCRs to be in the same
452 * underlying page. Check for that.
453 */
454
455 for (tmp = pf->sc->card.pf_head.sqh_first; tmp != NULL;
456 tmp = tmp->pf_list.sqe_next) {
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 for (tmp = pf->sc->card.pf_head.sqh_first; tmp != NULL;
533 tmp = tmp->pf_list.sqe_next) {
534 printf("%s: function %d CCR at %d offset %lx: "
535 "%x %x %x %x, %x %x %x %x, %x\n",
536 tmp->sc->dev.dv_xname, tmp->number,
537 tmp->pf_ccr_window,
538 (unsigned long) tmp->pf_ccr_offset,
539 pcmcia_ccr_read(tmp, 0x00),
540 pcmcia_ccr_read(tmp, 0x02),
541 pcmcia_ccr_read(tmp, 0x04),
542 pcmcia_ccr_read(tmp, 0x06),
543
544 pcmcia_ccr_read(tmp, 0x0A),
545 pcmcia_ccr_read(tmp, 0x0C),
546 pcmcia_ccr_read(tmp, 0x0E),
547 pcmcia_ccr_read(tmp, 0x10),
548
549 pcmcia_ccr_read(tmp, 0x12));
550 }
551 }
552 #endif
553
554 pf->pf_flags |= PFF_ENABLED;
555 return (0);
556
557 bad:
558 /*
559 * Decrement the reference count, and power down the socket, if
560 * necessary.
561 */
562 if (--pf->sc->sc_enabled_count == 0)
563 pcmcia_chip_socket_disable(pf->sc->pct, pf->sc->pch);
564 DPRINTF(("%s: --enabled_count = %d\n", pf->sc->dev.dv_xname,
565 pf->sc->sc_enabled_count));
566
567 return (1);
568 }
569
570 /* Disable PCMCIA function. */
571 void
572 pcmcia_function_disable(pf)
573 struct pcmcia_function *pf;
574 {
575 struct pcmcia_function *tmp;
576
577 if (pf->cfe == NULL)
578 panic("pcmcia_function_enable: function not initialized");
579
580 if ((pf->pf_flags & PFF_ENABLED) == 0) {
581 /*
582 * Don't do anything if we're already disabled.
583 */
584 return;
585 }
586
587 /*
588 * it's possible for different functions' CCRs to be in the same
589 * underlying page. Check for that. Note we mark us as disabled
590 * first to avoid matching ourself.
591 */
592
593 pf->pf_flags &= ~PFF_ENABLED;
594 for (tmp = pf->sc->card.pf_head.sqh_first; tmp != NULL;
595 tmp = tmp->pf_list.sqe_next) {
596 if ((tmp->pf_flags & PFF_ENABLED) &&
597 (pf->ccr_base >= (tmp->ccr_base - tmp->pf_ccr_offset)) &&
598 ((pf->ccr_base + PCMCIA_CCR_SIZE) <=
599 (tmp->ccr_base - tmp->pf_ccr_offset + tmp->pf_ccr_realsize)))
600 break;
601 }
602
603 /* Not used by anyone else; unmap the CCR. */
604 if (tmp == NULL) {
605 pcmcia_mem_unmap(pf, pf->pf_ccr_window);
606 pcmcia_mem_free(pf, &pf->pf_pcmh);
607 }
608
609 /*
610 * Decrement the reference count, and power down the socket, if
611 * necessary.
612 */
613 if (--pf->sc->sc_enabled_count == 0)
614 pcmcia_chip_socket_disable(pf->sc->pct, pf->sc->pch);
615 DPRINTF(("%s: --enabled_count = %d\n", pf->sc->dev.dv_xname,
616 pf->sc->sc_enabled_count));
617 }
618
619 int
620 pcmcia_io_map(pf, width, offset, size, pcihp, windowp)
621 struct pcmcia_function *pf;
622 int width;
623 bus_addr_t offset;
624 bus_size_t size;
625 struct pcmcia_io_handle *pcihp;
626 int *windowp;
627 {
628 int reg;
629
630 if (pcmcia_chip_io_map(pf->sc->pct, pf->sc->pch,
631 width, offset, size, pcihp, windowp))
632 return (1);
633
634 /*
635 * XXX in the multifunction multi-iospace-per-function case, this
636 * needs to cooperate with io_alloc to make sure that the spaces
637 * don't overlap, and that the ccr's are set correctly
638 */
639
640 if (pcmcia_mfc(pf->sc)) {
641 long tmp, iosize;
642
643 if (pf->pf_mfc_iomax == 0) {
644 pf->pf_mfc_iobase = pcihp->addr + offset;
645 pf->pf_mfc_iomax = pf->pf_mfc_iobase + size;
646 } else {
647 /* this makes the assumption that nothing overlaps */
648 if (pf->pf_mfc_iobase > pcihp->addr + offset)
649 pf->pf_mfc_iobase = pcihp->addr + offset;
650 if (pf->pf_mfc_iomax < pcihp->addr + offset + size)
651 pf->pf_mfc_iomax = pcihp->addr + offset + size;
652 }
653
654 tmp = pf->pf_mfc_iomax - pf->pf_mfc_iobase;
655 /* round up to nearest (2^n)-1 */
656 for (iosize = 1; iosize >= tmp; iosize <<= 1)
657 ;
658 iosize--;
659
660 pcmcia_ccr_write(pf, PCMCIA_CCR_IOBASE0,
661 pf->pf_mfc_iobase & 0xff);
662 pcmcia_ccr_write(pf, PCMCIA_CCR_IOBASE1,
663 (pf->pf_mfc_iobase >> 8) & 0xff);
664 pcmcia_ccr_write(pf, PCMCIA_CCR_IOBASE2, 0);
665 pcmcia_ccr_write(pf, PCMCIA_CCR_IOBASE3, 0);
666
667 pcmcia_ccr_write(pf, PCMCIA_CCR_IOSIZE, iosize);
668
669 reg = pcmcia_ccr_read(pf, PCMCIA_CCR_OPTION);
670 reg |= PCMCIA_CCR_OPTION_ADDR_DECODE;
671 pcmcia_ccr_write(pf, PCMCIA_CCR_OPTION, reg);
672 }
673 return (0);
674 }
675
676 void
677 pcmcia_io_unmap(pf, window)
678 struct pcmcia_function *pf;
679 int window;
680 {
681
682 pcmcia_chip_io_unmap(pf->sc->pct, pf->sc->pch, window);
683
684 /* XXX Anything for multi-function cards? */
685 }
686
687 void *
688 pcmcia_intr_establish(pf, ipl, ih_fct, ih_arg)
689 struct pcmcia_function *pf;
690 int ipl;
691 int (*ih_fct) __P((void *));
692 void *ih_arg;
693 {
694 void *ret;
695
696 /* behave differently if this is a multifunction card */
697
698 if (pcmcia_mfc(pf->sc)) {
699 int s, ihcnt, hiipl, reg;
700 struct pcmcia_function *pf2;
701
702 /*
703 * mask all the ipl's which are already used by this card,
704 * and find the highest ipl number (lowest priority)
705 */
706
707 ihcnt = 0;
708 s = 0; /* this is only here to keep the compiler
709 happy */
710 hiipl = 0; /* this is only here to keep the compiler
711 happy */
712
713 for (pf2 = pf->sc->card.pf_head.sqh_first; pf2 != NULL;
714 pf2 = pf2->pf_list.sqe_next) {
715 if (pf2->ih_fct) {
716 DPRINTF(("%s: function %d has ih_fct %p\n",
717 pf->sc->dev.dv_xname, pf2->number,
718 pf2->ih_fct));
719
720 if (ihcnt == 0) {
721 hiipl = pf2->ih_ipl;
722 } else {
723 if (pf2->ih_ipl > hiipl)
724 hiipl = pf2->ih_ipl;
725 }
726
727 ihcnt++;
728 }
729 }
730
731 /*
732 * establish the real interrupt, changing the ipl if
733 * necessary
734 */
735
736 if (ihcnt == 0) {
737 #ifdef DIAGNOSTIC
738 if (pf->sc->ih != NULL)
739 panic("card has intr handler, but no function does");
740 #endif
741 s = splhigh();
742
743 /* set up the handler for the new function */
744
745 pf->ih_fct = ih_fct;
746 pf->ih_arg = ih_arg;
747 pf->ih_ipl = ipl;
748
749 pf->sc->ih = pcmcia_chip_intr_establish(pf->sc->pct,
750 pf->sc->pch, pf, ipl, PCMCIA_CARD_INTR, pf->sc);
751 splx(s);
752 } else if (ipl > hiipl) {
753 #ifdef DIAGNOSTIC
754 if (pf->sc->ih == NULL)
755 panic("functions have ih, but the card does not");
756 #endif
757
758 /* XXX need #ifdef for splserial on x86 */
759 s = splhigh();
760
761 pcmcia_chip_intr_disestablish(pf->sc->pct, pf->sc->pch,
762 pf->sc->ih);
763
764 /* set up the handler for the new function */
765 pf->ih_fct = ih_fct;
766 pf->ih_arg = ih_arg;
767 pf->ih_ipl = ipl;
768
769 pf->sc->ih = pcmcia_chip_intr_establish(pf->sc->pct,
770 pf->sc->pch, pf, ipl, PCMCIA_CARD_INTR, pf->sc);
771
772 splx(s);
773 } else {
774 s = splhigh();
775
776 /* set up the handler for the new function */
777
778 pf->ih_fct = ih_fct;
779 pf->ih_arg = ih_arg;
780 pf->ih_ipl = ipl;
781
782 splx(s);
783 }
784
785 ret = pf->sc->ih;
786
787 if (ret != NULL) {
788 reg = pcmcia_ccr_read(pf, PCMCIA_CCR_OPTION);
789 reg |= PCMCIA_CCR_OPTION_IREQ_ENABLE;
790 pcmcia_ccr_write(pf, PCMCIA_CCR_OPTION, reg);
791
792 reg = pcmcia_ccr_read(pf, PCMCIA_CCR_STATUS);
793 reg |= PCMCIA_CCR_STATUS_INTRACK;
794 pcmcia_ccr_write(pf, PCMCIA_CCR_STATUS, reg);
795 }
796 } else {
797 ret = pcmcia_chip_intr_establish(pf->sc->pct, pf->sc->pch,
798 pf, ipl, ih_fct, ih_arg);
799 }
800
801 return (ret);
802 }
803
804 void
805 pcmcia_intr_disestablish(pf, ih)
806 struct pcmcia_function *pf;
807 void *ih;
808 {
809 /* behave differently if this is a multifunction card */
810
811 if (pcmcia_mfc(pf->sc)) {
812 int s, ihcnt, hiipl;
813 struct pcmcia_function *pf2;
814
815 /*
816 * mask all the ipl's which are already used by this card,
817 * and find the highest ipl number (lowest priority). Skip
818 * the current function.
819 */
820
821 ihcnt = 0;
822 s = 0; /* this is only here to keep the compipler
823 happy */
824 hiipl = 0; /* this is only here to keep the compipler
825 happy */
826
827 for (pf2 = pf->sc->card.pf_head.sqh_first; pf2 != NULL;
828 pf2 = pf2->pf_list.sqe_next) {
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 /* ihcnt is the number of interrupt handlers *not* including
849 the one about to be removed. */
850
851 if (ihcnt == 0) {
852 int reg;
853
854 #ifdef DIAGNOSTIC
855 if (pf->sc->ih == NULL)
856 panic("disestablishing last function, but card has no ih");
857 #endif
858 pcmcia_chip_intr_disestablish(pf->sc->pct, pf->sc->pch,
859 pf->sc->ih);
860
861 reg = pcmcia_ccr_read(pf, PCMCIA_CCR_OPTION);
862 reg &= ~PCMCIA_CCR_OPTION_IREQ_ENABLE;
863 pcmcia_ccr_write(pf, PCMCIA_CCR_OPTION, reg);
864
865 pf->ih_fct = NULL;
866 pf->ih_arg = NULL;
867
868 pf->sc->ih = NULL;
869 } else if (pf->ih_ipl > hiipl) {
870 #ifdef DIAGNOSTIC
871 if (pf->sc->ih == NULL)
872 panic("changing ih ipl, but card has no ih");
873 #endif
874 /* XXX need #ifdef for splserial on x86 */
875 s = splhigh();
876
877 pcmcia_chip_intr_disestablish(pf->sc->pct, pf->sc->pch,
878 pf->sc->ih);
879 pf->sc->ih = pcmcia_chip_intr_establish(pf->sc->pct,
880 pf->sc->pch, pf, hiipl, PCMCIA_CARD_INTR, pf->sc);
881
882 /* null out the handler for this function */
883
884 pf->ih_fct = NULL;
885 pf->ih_arg = NULL;
886
887 splx(s);
888 } else {
889 s = splhigh();
890
891 pf->ih_fct = NULL;
892 pf->ih_arg = NULL;
893
894 splx(s);
895 }
896 } else {
897 pcmcia_chip_intr_disestablish(pf->sc->pct, pf->sc->pch, ih);
898 }
899 }
900
901 int
902 pcmcia_card_intr(arg)
903 void *arg;
904 {
905 struct pcmcia_softc *sc = arg;
906 struct pcmcia_function *pf;
907 int reg, ret, ret2;
908
909 ret = 0;
910
911 for (pf = sc->card.pf_head.sqh_first; pf != NULL;
912 pf = pf->pf_list.sqe_next) {
913 if (pf->ih_fct != NULL &&
914 (pf->ccr_mask & (1 << (PCMCIA_CCR_STATUS / 2)))) {
915 reg = pcmcia_ccr_read(pf, PCMCIA_CCR_STATUS);
916 if (reg & PCMCIA_CCR_STATUS_INTR) {
917 ret2 = (*pf->ih_fct)(pf->ih_arg);
918 if (ret2 != 0 && ret == 0)
919 ret = ret2;
920 reg = pcmcia_ccr_read(pf, PCMCIA_CCR_STATUS);
921 pcmcia_ccr_write(pf, PCMCIA_CCR_STATUS,
922 reg & ~PCMCIA_CCR_STATUS_INTR);
923 }
924 }
925 }
926
927 return (ret);
928 }
929
930 #ifdef PCMCIADEBUG
931 int
932 pcmcia_card_intrdebug(arg)
933 void *arg;
934 {
935 struct pcmcia_softc *sc = arg;
936 struct pcmcia_function *pf;
937 int reg, ret, ret2;
938
939 ret = 0;
940
941 for (pf = sc->card.pf_head.sqh_first; pf != NULL;
942 pf = pf->pf_list.sqe_next) {
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