pcmcia.c revision 1.37 1 /* $NetBSD: pcmcia.c,v 1.37 2004/07/07 06:08:25 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.37 2004/07/07 06:08:25 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 pcmcia_devinfo(card, 1, devinfo, sizeof devinfo);
294 aprint_normal("%s at %s, ", devinfo, pnp);
295 }
296 aprint_normal(" function %d", pa->pf->number);
297
298 return (UNCONF);
299 }
300
301 void
302 pcmcia_devinfo(card, showhex, cp, cplen)
303 struct pcmcia_card *card;
304 int showhex;
305 char *cp;
306 int cplen;
307 {
308 int i, n;
309
310 if (cplen > 1) {
311 *cp++ = '<';
312 cplen--;
313 }
314
315 for (i = 0; i < 4 && card->cis1_info[i] != NULL && cplen > 1; i++) {
316 n = snprintf(cp, cplen, "%s%s", (i && i != 3) ? ", " : "",
317 card->cis1_info[i]);
318 cp += n;
319 cplen -= n;
320 }
321
322 if (cplen > 1) {
323 *cp++ = '>';
324 cplen--;
325 }
326
327 if (showhex && cplen > 1)
328 snprintf(cp, cplen, " (manufacturer 0x%04x, product 0x%04x)",
329 card->manufacturer, card->product);
330 }
331
332 const struct pcmcia_product *
333 pcmcia_product_lookup(pa, tab, ent_size, matchfn)
334 struct pcmcia_attach_args *pa;
335 const struct pcmcia_product *tab;
336 size_t ent_size;
337 pcmcia_product_match_fn matchfn;
338 {
339 const struct pcmcia_product *ent;
340 int matches;
341
342 #ifdef DIAGNOSTIC
343 if (sizeof *ent > ent_size)
344 panic("pcmcia_product_lookup: bogus ent_size %ld",
345 (long) ent_size);
346 #endif
347
348 for (ent = tab;
349 ent->pp_name != NULL;
350 ent = (const struct pcmcia_product *)
351 ((const char *)ent + ent_size)) {
352
353 /* see if it matches vendor/product/function */
354 matches = (pa->manufacturer == ent->pp_vendor) &&
355 (pa->product == ent->pp_product) &&
356 (pa->pf->number == ent->pp_expfunc);
357
358 /* if a separate match function is given, let it override */
359 if (matchfn != NULL)
360 matches = (*matchfn)(pa, ent, matches);
361
362 if (matches)
363 return (ent);
364 }
365 return (NULL);
366 }
367
368 int
369 pcmcia_card_gettype(dev)
370 struct device *dev;
371 {
372 struct pcmcia_softc *sc = (struct pcmcia_softc *)dev;
373 struct pcmcia_function *pf;
374
375 /*
376 * set the iftype to memory if this card has no functions (not yet
377 * probed), or only one function, and that is not initialized yet or
378 * that is memory.
379 */
380 pf = SIMPLEQ_FIRST(&sc->card.pf_head);
381 if (pf == NULL ||
382 (SIMPLEQ_NEXT(pf, pf_list) == NULL &&
383 (pf->cfe == NULL || pf->cfe->iftype == PCMCIA_IFTYPE_MEMORY)))
384 return (PCMCIA_IFTYPE_MEMORY);
385 else
386 return (PCMCIA_IFTYPE_IO);
387 }
388
389 /*
390 * Initialize a PCMCIA function. May be called as long as the function is
391 * disabled.
392 */
393 void
394 pcmcia_function_init(pf, cfe)
395 struct pcmcia_function *pf;
396 struct pcmcia_config_entry *cfe;
397 {
398 if (pf->pf_flags & PFF_ENABLED)
399 panic("pcmcia_function_init: function is enabled");
400
401 /* Remember which configuration entry we are using. */
402 pf->cfe = cfe;
403 }
404
405 static inline void pcmcia_socket_enable(pct, pch)
406 pcmcia_chipset_tag_t pct;
407 pcmcia_chipset_handle_t *pch;
408 {
409 pcmcia_chip_socket_enable(pct, pch);
410 }
411
412 static inline void pcmcia_socket_disable(pct, pch)
413 pcmcia_chipset_tag_t pct;
414 pcmcia_chipset_handle_t *pch;
415 {
416 pcmcia_chip_socket_disable(pct, pch);
417 }
418
419 /* Enable a PCMCIA function */
420 int
421 pcmcia_function_enable(pf)
422 struct pcmcia_function *pf;
423 {
424 struct pcmcia_function *tmp;
425 int reg;
426
427 if (pf->cfe == NULL)
428 panic("pcmcia_function_enable: function not initialized");
429
430 /*
431 * Increase the reference count on the socket, enabling power, if
432 * necessary.
433 */
434 if (pf->sc->sc_enabled_count++ == 0)
435 pcmcia_chip_socket_enable(pf->sc->pct, pf->sc->pch);
436 DPRINTF(("%s: ++enabled_count = %d\n", pf->sc->dev.dv_xname,
437 pf->sc->sc_enabled_count));
438
439 if (pf->pf_flags & PFF_ENABLED) {
440 /*
441 * Don't do anything if we're already enabled.
442 */
443 return (0);
444 }
445
446 /*
447 * it's possible for different functions' CCRs to be in the same
448 * underlying page. Check for that.
449 */
450
451 SIMPLEQ_FOREACH(tmp, &pf->sc->card.pf_head, pf_list) {
452 if ((tmp->pf_flags & PFF_ENABLED) &&
453 (pf->ccr_base >= (tmp->ccr_base - tmp->pf_ccr_offset)) &&
454 ((pf->ccr_base + PCMCIA_CCR_SIZE) <=
455 (tmp->ccr_base - tmp->pf_ccr_offset +
456 tmp->pf_ccr_realsize))) {
457 pf->pf_ccrt = tmp->pf_ccrt;
458 pf->pf_ccrh = tmp->pf_ccrh;
459 pf->pf_ccr_realsize = tmp->pf_ccr_realsize;
460
461 /*
462 * pf->pf_ccr_offset = (tmp->pf_ccr_offset -
463 * tmp->ccr_base) + pf->ccr_base;
464 */
465 pf->pf_ccr_offset =
466 (tmp->pf_ccr_offset + pf->ccr_base) -
467 tmp->ccr_base;
468 pf->pf_ccr_window = tmp->pf_ccr_window;
469 break;
470 }
471 }
472
473 if (tmp == NULL) {
474 if (pcmcia_mem_alloc(pf, PCMCIA_CCR_SIZE, &pf->pf_pcmh))
475 goto bad;
476
477 if (pcmcia_mem_map(pf, PCMCIA_MEM_ATTR, pf->ccr_base,
478 PCMCIA_CCR_SIZE, &pf->pf_pcmh, &pf->pf_ccr_offset,
479 &pf->pf_ccr_window)) {
480 pcmcia_mem_free(pf, &pf->pf_pcmh);
481 goto bad;
482 }
483 }
484
485 reg = (pf->cfe->number & PCMCIA_CCR_OPTION_CFINDEX);
486 reg |= PCMCIA_CCR_OPTION_LEVIREQ;
487 if (pcmcia_mfc(pf->sc)) {
488 reg |= (PCMCIA_CCR_OPTION_FUNC_ENABLE |
489 PCMCIA_CCR_OPTION_ADDR_DECODE);
490 if (pf->ih_fct)
491 reg |= PCMCIA_CCR_OPTION_IREQ_ENABLE;
492
493 }
494 pcmcia_ccr_write(pf, PCMCIA_CCR_OPTION, reg);
495
496 reg = 0;
497
498 if ((pf->cfe->flags & PCMCIA_CFE_IO16) == 0)
499 reg |= PCMCIA_CCR_STATUS_IOIS8;
500 if (pf->cfe->flags & PCMCIA_CFE_AUDIO)
501 reg |= PCMCIA_CCR_STATUS_AUDIO;
502 pcmcia_ccr_write(pf, PCMCIA_CCR_STATUS, reg);
503
504 pcmcia_ccr_write(pf, PCMCIA_CCR_SOCKETCOPY, 0);
505
506 if (pcmcia_mfc(pf->sc)) {
507 long tmp, iosize;
508
509 tmp = pf->pf_mfc_iomax - pf->pf_mfc_iobase;
510 /* round up to nearest (2^n)-1 */
511 for (iosize = 1; iosize < tmp; iosize <<= 1)
512 ;
513 iosize--;
514
515 pcmcia_ccr_write(pf, PCMCIA_CCR_IOBASE0,
516 pf->pf_mfc_iobase & 0xff);
517 pcmcia_ccr_write(pf, PCMCIA_CCR_IOBASE1,
518 (pf->pf_mfc_iobase >> 8) & 0xff);
519 pcmcia_ccr_write(pf, PCMCIA_CCR_IOBASE2, 0);
520 pcmcia_ccr_write(pf, PCMCIA_CCR_IOBASE3, 0);
521
522 pcmcia_ccr_write(pf, PCMCIA_CCR_IOSIZE, iosize);
523 }
524
525 #ifdef PCMCIADEBUG
526 if (pcmcia_debug) {
527 SIMPLEQ_FOREACH(tmp, &pf->sc->card.pf_head, pf_list) {
528 printf("%s: function %d CCR at %d offset %lx: "
529 "%x %x %x %x, %x %x %x %x, %x\n",
530 tmp->sc->dev.dv_xname, tmp->number,
531 tmp->pf_ccr_window,
532 (unsigned long) tmp->pf_ccr_offset,
533 pcmcia_ccr_read(tmp, 0x00),
534 pcmcia_ccr_read(tmp, 0x02),
535 pcmcia_ccr_read(tmp, 0x04),
536 pcmcia_ccr_read(tmp, 0x06),
537
538 pcmcia_ccr_read(tmp, 0x0A),
539 pcmcia_ccr_read(tmp, 0x0C),
540 pcmcia_ccr_read(tmp, 0x0E),
541 pcmcia_ccr_read(tmp, 0x10),
542
543 pcmcia_ccr_read(tmp, 0x12));
544 }
545 }
546 #endif
547
548 pf->pf_flags |= PFF_ENABLED;
549
550 #ifdef IT8368E_LEGACY_MODE
551 /* return to I/O mode */
552 it8368_mode(pf, IT8368_IO_MODE, IT8368_WIDTH_16);
553 #endif
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 but decrement if we're already disabled.
582 */
583 goto out;
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 SIMPLEQ_FOREACH(tmp, &pf->sc->card.pf_head, pf_list) {
594 if ((tmp->pf_flags & PFF_ENABLED) &&
595 (pf->ccr_base >= (tmp->ccr_base - tmp->pf_ccr_offset)) &&
596 ((pf->ccr_base + PCMCIA_CCR_SIZE) <=
597 (tmp->ccr_base - tmp->pf_ccr_offset + tmp->pf_ccr_realsize)))
598 break;
599 }
600
601 /* Not used by anyone else; unmap the CCR. */
602 if (tmp == NULL) {
603 pcmcia_mem_unmap(pf, pf->pf_ccr_window);
604 pcmcia_mem_free(pf, &pf->pf_pcmh);
605 }
606
607 out:
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 SIMPLEQ_FOREACH(pf2, &pf->sc->card.pf_head, pf_list) {
713 if (pf2->ih_fct) {
714 DPRINTF(("%s: function %d has ih_fct %p\n",
715 pf->sc->dev.dv_xname, pf2->number,
716 pf2->ih_fct));
717
718 if (ihcnt == 0) {
719 hiipl = pf2->ih_ipl;
720 } else {
721 if (pf2->ih_ipl > hiipl)
722 hiipl = pf2->ih_ipl;
723 }
724
725 ihcnt++;
726 }
727 }
728
729 /*
730 * establish the real interrupt, changing the ipl if
731 * necessary
732 */
733
734 if (ihcnt == 0) {
735 #ifdef DIAGNOSTIC
736 if (pf->sc->ih != NULL)
737 panic("card has intr handler, but no function does");
738 #endif
739 s = splhigh();
740
741 /* set up the handler for the new function */
742
743 pf->ih_fct = ih_fct;
744 pf->ih_arg = ih_arg;
745 pf->ih_ipl = ipl;
746
747 pf->sc->ih = pcmcia_chip_intr_establish(pf->sc->pct,
748 pf->sc->pch, pf, ipl, PCMCIA_CARD_INTR, pf->sc);
749 splx(s);
750 } else if (ipl > hiipl) {
751 #ifdef DIAGNOSTIC
752 if (pf->sc->ih == NULL)
753 panic("functions have ih, but the card does not");
754 #endif
755
756 /* XXX need #ifdef for splserial on x86 */
757 s = splhigh();
758
759 pcmcia_chip_intr_disestablish(pf->sc->pct, pf->sc->pch,
760 pf->sc->ih);
761
762 /* set up the handler for the new function */
763 pf->ih_fct = ih_fct;
764 pf->ih_arg = ih_arg;
765 pf->ih_ipl = ipl;
766
767 pf->sc->ih = pcmcia_chip_intr_establish(pf->sc->pct,
768 pf->sc->pch, pf, ipl, PCMCIA_CARD_INTR, pf->sc);
769
770 splx(s);
771 } else {
772 s = splhigh();
773
774 /* set up the handler for the new function */
775
776 pf->ih_fct = ih_fct;
777 pf->ih_arg = ih_arg;
778 pf->ih_ipl = ipl;
779
780 splx(s);
781 }
782
783 ret = pf->sc->ih;
784
785 if (ret != NULL) {
786 reg = pcmcia_ccr_read(pf, PCMCIA_CCR_OPTION);
787 reg |= PCMCIA_CCR_OPTION_IREQ_ENABLE;
788 pcmcia_ccr_write(pf, PCMCIA_CCR_OPTION, reg);
789
790 reg = pcmcia_ccr_read(pf, PCMCIA_CCR_STATUS);
791 reg |= PCMCIA_CCR_STATUS_INTRACK;
792 pcmcia_ccr_write(pf, PCMCIA_CCR_STATUS, reg);
793 }
794 } else {
795 ret = pcmcia_chip_intr_establish(pf->sc->pct, pf->sc->pch,
796 pf, ipl, ih_fct, ih_arg);
797 }
798
799 return (ret);
800 }
801
802 void
803 pcmcia_intr_disestablish(pf, ih)
804 struct pcmcia_function *pf;
805 void *ih;
806 {
807 /* behave differently if this is a multifunction card */
808
809 if (pcmcia_mfc(pf->sc)) {
810 int s, ihcnt, hiipl;
811 struct pcmcia_function *pf2;
812
813 /*
814 * mask all the ipl's which are already used by this card,
815 * and find the highest ipl number (lowest priority). Skip
816 * the current function.
817 */
818
819 ihcnt = 0;
820 s = 0; /* avoid compiler warning */
821 hiipl = 0; /* avoid compiler warning */
822
823 SIMPLEQ_FOREACH(pf2, &pf->sc->card.pf_head, pf_list) {
824 if (pf2 == pf)
825 continue;
826
827 if (pf2->ih_fct) {
828 if (ihcnt == 0) {
829 hiipl = pf2->ih_ipl;
830 } else {
831 if (pf2->ih_ipl > hiipl)
832 hiipl = pf2->ih_ipl;
833 }
834 ihcnt++;
835 }
836 }
837
838 /*
839 * If the ih being removed is lower priority than the lowest
840 * priority remaining interrupt, up the priority.
841 */
842
843 /*
844 * ihcnt is the number of interrupt handlers *not* including
845 * the one about to be removed.
846 */
847
848 if (ihcnt == 0) {
849 int reg;
850
851 #ifdef DIAGNOSTIC
852 if (pf->sc->ih == NULL)
853 panic("disestablishing last function, but card has no ih");
854 #endif
855 pcmcia_chip_intr_disestablish(pf->sc->pct, pf->sc->pch,
856 pf->sc->ih);
857
858 reg = pcmcia_ccr_read(pf, PCMCIA_CCR_OPTION);
859 reg &= ~PCMCIA_CCR_OPTION_IREQ_ENABLE;
860 pcmcia_ccr_write(pf, PCMCIA_CCR_OPTION, reg);
861
862 pf->ih_fct = NULL;
863 pf->ih_arg = NULL;
864
865 pf->sc->ih = NULL;
866 } else if (pf->ih_ipl > hiipl) {
867 #ifdef DIAGNOSTIC
868 if (pf->sc->ih == NULL)
869 panic("changing ih ipl, but card has no ih");
870 #endif
871 /* XXX need #ifdef for splserial on x86 */
872 s = splhigh();
873
874 pcmcia_chip_intr_disestablish(pf->sc->pct, pf->sc->pch,
875 pf->sc->ih);
876 pf->sc->ih = pcmcia_chip_intr_establish(pf->sc->pct,
877 pf->sc->pch, pf, hiipl, PCMCIA_CARD_INTR, pf->sc);
878
879 /* null out the handler for this function */
880
881 pf->ih_fct = NULL;
882 pf->ih_arg = NULL;
883
884 splx(s);
885 } else {
886 s = splhigh();
887
888 pf->ih_fct = NULL;
889 pf->ih_arg = NULL;
890
891 splx(s);
892 }
893 } else {
894 pcmcia_chip_intr_disestablish(pf->sc->pct, pf->sc->pch, ih);
895 }
896 }
897
898 int
899 pcmcia_card_intr(arg)
900 void *arg;
901 {
902 struct pcmcia_softc *sc = arg;
903 struct pcmcia_function *pf;
904 int reg, ret, ret2;
905
906 ret = 0;
907
908 SIMPLEQ_FOREACH(pf, &sc->card.pf_head, pf_list) {
909 if (pf->ih_fct != NULL &&
910 (pf->ccr_mask & (1 << (PCMCIA_CCR_STATUS / 2)))) {
911 reg = pcmcia_ccr_read(pf, PCMCIA_CCR_STATUS);
912 if (reg & PCMCIA_CCR_STATUS_INTR) {
913 ret2 = (*pf->ih_fct)(pf->ih_arg);
914 if (ret2 != 0 && ret == 0)
915 ret = ret2;
916 reg = pcmcia_ccr_read(pf, PCMCIA_CCR_STATUS);
917 pcmcia_ccr_write(pf, PCMCIA_CCR_STATUS,
918 reg & ~PCMCIA_CCR_STATUS_INTR);
919 }
920 }
921 }
922
923 return (ret);
924 }
925
926 #ifdef PCMCIADEBUG
927 int
928 pcmcia_card_intrdebug(arg)
929 void *arg;
930 {
931 struct pcmcia_softc *sc = arg;
932 struct pcmcia_function *pf;
933 int reg, ret, ret2;
934
935 ret = 0;
936
937 SIMPLEQ_FOREACH(pf, &sc->card.pf_head, pf_list) {
938 printf("%s: intr flags=%x fct=%d cor=%02x csr=%02x pin=%02x",
939 sc->dev.dv_xname, pf->pf_flags, pf->number,
940 pcmcia_ccr_read(pf, PCMCIA_CCR_OPTION),
941 pcmcia_ccr_read(pf, PCMCIA_CCR_STATUS),
942 pcmcia_ccr_read(pf, PCMCIA_CCR_PIN));
943 if (pf->ih_fct != NULL &&
944 (pf->ccr_mask & (1 << (PCMCIA_CCR_STATUS / 2)))) {
945 reg = pcmcia_ccr_read(pf, PCMCIA_CCR_STATUS);
946 if (reg & PCMCIA_CCR_STATUS_INTR) {
947 ret2 = (*pf->ih_fct)(pf->ih_arg);
948 if (ret2 != 0 && ret == 0)
949 ret = ret2;
950 reg = pcmcia_ccr_read(pf, PCMCIA_CCR_STATUS);
951 printf("; csr %02x->%02x",
952 reg, reg & ~PCMCIA_CCR_STATUS_INTR);
953 pcmcia_ccr_write(pf, PCMCIA_CCR_STATUS,
954 reg & ~PCMCIA_CCR_STATUS_INTR);
955 }
956 }
957 printf("\n");
958 }
959
960 return (ret);
961 }
962 #endif
963