cardbus.c revision 1.17 1 /* $NetBSD: cardbus.c,v 1.17 2000/01/13 10:27:31 joda Exp $ */
2
3 /*
4 * Copyright (c) 1997, 1998 and 1999
5 * HAYAKAWA Koichi. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by HAYAKAWA Koichi.
18 * 4. The name of the author may not be used to endorse or promote products
19 * derived from this software without specific prior written permission.
20 *
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
24 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
25 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
26 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
27 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
28 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
30 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32 * POSSIBILITY OF SUCH DAMAGE.
33 */
34
35 #include "opt_cardbus.h"
36
37 #include <sys/types.h>
38 #include <sys/param.h>
39 #include <sys/systm.h>
40 #include <sys/device.h>
41 #include <sys/malloc.h>
42 #include <sys/kernel.h>
43 #include <sys/syslog.h>
44
45 #include <machine/bus.h>
46
47 #include <dev/cardbus/cardbusvar.h>
48 #include <dev/cardbus/cardbusdevs.h>
49 #include <dev/cardbus/pccardcis.h>
50
51 #include <dev/cardbus/cardbus_exrom.h>
52
53 #include <dev/pci/pcivar.h> /* XXX */
54 #include <dev/pci/pcireg.h> /* XXX */
55
56 #include <dev/pcmcia/pcmciareg.h> /* XXX */
57
58 #if defined CARDBUS_DEBUG
59 #define STATIC
60 #define DPRINTF(a) printf a
61 #define DDELAY(x) delay((x)*1000*1000)
62 #else
63 #define STATIC static
64 #define DPRINTF(a)
65 #endif
66
67
68
69 STATIC void cardbusattach __P((struct device *, struct device *, void *));
70 /* STATIC int cardbusprint __P((void *, const char *)); */
71 int cardbus_attach_card __P((struct cardbus_softc *));
72
73 #if !defined __BROKEN_INDIRECT_CONFIG
74 STATIC int cardbusmatch __P((struct device *, struct cfdata *, void *));
75 static int cardbussubmatch __P((struct device *, struct cfdata *, void *));
76 #else
77 STATIC int cardbusmatch __P((struct device *, void *, void *));
78 static int cardbussubmatch __P((struct device *, void *, void *));
79 #endif
80 static int cardbusprint __P((void *, const char *));
81
82 typedef void (*tuple_decode_func)(u_int8_t*, int, void*);
83
84 static int decode_tuples __P((u_int8_t *, int, tuple_decode_func, void*));
85 #ifdef CARDBUS_DEBUG
86 static void print_tuple __P((u_int8_t*, int, void*));
87 #endif
88
89 static int cardbus_read_tuples __P((struct cardbus_attach_args *,
90 cardbusreg_t, u_int8_t *, size_t));
91
92 static void enable_function __P((struct cardbus_softc *, int, int));
93 static void disable_function __P((struct cardbus_softc *, int));
94
95
96 struct cfattach cardbus_ca = {
97 sizeof(struct cardbus_softc), cardbusmatch, cardbusattach
98 };
99
100 #ifndef __NetBSD_Version__
101 struct cfdriver cardbus_cd = {
102 NULL, "cardbus", DV_DULL
103 };
104 #endif
105
106
107 STATIC int
108 #if defined __BROKEN_INDIRECT_CONFIG
109 cardbusmatch(parent, match, aux)
110 struct device *parent;
111 void *match;
112 void *aux;
113 #else
114 cardbusmatch(parent, cf, aux)
115 struct device *parent;
116 struct cfdata *cf;
117 void *aux;
118 #endif
119 {
120 #if defined __BROKEN_INDIRECT_CONFIG
121 struct cfdata *cf = match;
122 #endif
123 struct cbslot_attach_args *cba = aux;
124
125 if (strcmp(cba->cba_busname, cf->cf_driver->cd_name)) {
126 DPRINTF(("cardbusmatch: busname differs %s <=> %s\n",
127 cba->cba_busname, cf->cf_driver->cd_name));
128 return 0;
129 }
130
131 #if 0
132 /* which function? */
133 if (cf->cbslotcf_func != CBSLOT_UNK_FUNC &&
134 cf->cbslotcf_func != cba->cba_function) {
135 DPRINTF(("cardbusmatch: function differs %d <=> %d\n",
136 cf->cbslotcf_func, cba->cba_function));
137 return 0;
138 }
139 #endif
140
141 if (cba->cba_function < 0 || cba->cba_function > 255) {
142 return 0;
143 }
144
145 return 1;
146 }
147
148
149
150 STATIC void
151 cardbusattach(parent, self, aux)
152 struct device *parent;
153 struct device *self;
154 void *aux;
155 {
156 struct cardbus_softc *sc = (void *)self;
157 struct cbslot_attach_args *cba = aux;
158 int cdstatus;
159
160 sc->sc_bus = cba->cba_bus;
161 sc->sc_device = cba->cba_function;
162 sc->sc_intrline = cba->cba_intrline;
163 sc->sc_cacheline = cba->cba_cacheline;
164 sc->sc_lattimer = cba->cba_lattimer;
165
166 printf(": bus %d device %d", sc->sc_bus, sc->sc_device);
167 printf(" cacheline 0x%x, lattimer 0x%x\n", sc->sc_cacheline,sc->sc_lattimer);
168
169 sc->sc_iot = cba->cba_iot; /* CardBus I/O space tag */
170 sc->sc_memt = cba->cba_memt; /* CardBus MEM space tag */
171 sc->sc_dmat = cba->cba_dmat; /* DMA tag */
172 sc->sc_cc = cba->cba_cc;
173 sc->sc_cf = cba->cba_cf;
174
175 #if rbus
176 sc->sc_rbus_iot = cba->cba_rbus_iot;
177 sc->sc_rbus_memt = cba->cba_rbus_memt;
178 #endif
179
180 sc->sc_funcs = NULL;
181
182 cdstatus = 0;
183 }
184
185 static int
186 cardbus_read_tuples(ca, cis_ptr, tuples, len)
187 struct cardbus_attach_args *ca;
188 cardbusreg_t cis_ptr;
189 u_int8_t *tuples;
190 size_t len;
191 {
192 struct cardbus_softc *sc = ca->ca_ct->ct_sc;
193 cardbus_chipset_tag_t cc = ca->ca_ct->ct_cc;
194 cardbus_function_tag_t cf = ca->ca_ct->ct_cf;
195 cardbustag_t tag = ca->ca_tag;
196 cardbusreg_t command;
197 int found = 0;
198
199 int i, j;
200 int cardbus_space = cis_ptr & CARDBUS_CIS_ASIMASK;
201 bus_space_handle_t bar_memh;
202 bus_size_t bar_size;
203 bus_addr_t bar_addr;
204
205 int reg;
206
207 memset(tuples, 0, len);
208
209 cis_ptr = cis_ptr & CARDBUS_CIS_ADDRMASK;
210
211 switch(cardbus_space) {
212 case CARDBUS_CIS_ASI_TUPLE:
213 DPRINTF(("%s: reading CIS data from configuration space\n",
214 sc->sc_dev.dv_xname));
215 for (i = cis_ptr, j = 0; i < 0xff; i += 4) {
216 u_int32_t e = (cf->cardbus_conf_read)(cc, tag, i);
217 tuples[j] = 0xff & e;
218 e >>= 8;
219 tuples[j + 1] = 0xff & e;
220 e >>= 8;
221 tuples[j + 2] = 0xff & e;
222 e >>= 8;
223 tuples[j + 3] = 0xff & e;
224 j += 4;
225 }
226 found++;
227 break;
228
229 case CARDBUS_CIS_ASI_BAR0:
230 case CARDBUS_CIS_ASI_BAR1:
231 case CARDBUS_CIS_ASI_BAR2:
232 case CARDBUS_CIS_ASI_BAR3:
233 case CARDBUS_CIS_ASI_BAR4:
234 case CARDBUS_CIS_ASI_BAR5:
235 case CARDBUS_CIS_ASI_ROM:
236 if(cardbus_space == CARDBUS_CIS_ASI_ROM) {
237 reg = CARDBUS_ROM_REG;
238 DPRINTF(("%s: reading CIS data from ROM\n",
239 sc->sc_dev.dv_xname));
240 } else {
241 reg = CARDBUS_BASE0_REG + (cardbus_space - 1) * 4;
242 DPRINTF(("%s: reading CIS data from BAR%d\n",
243 sc->sc_dev.dv_xname, cardbus_space - 1));
244 }
245
246 /* XXX zero register so mapreg_map doesn't get confused by old
247 contents */
248 cardbus_conf_write(cc, cf, tag, reg, 0);
249 if(Cardbus_mapreg_map(ca->ca_ct, reg,
250 CARDBUS_MAPREG_TYPE_MEM | PCI_MAPREG_MEM_TYPE_32BIT,
251 0,
252 NULL, &bar_memh, &bar_addr, &bar_size)) {
253 printf("%s: failed to map memory\n", sc->sc_dev.dv_xname);
254 return 1;
255 }
256
257
258 if(cardbus_space == CARDBUS_CIS_ASI_ROM) {
259 cardbusreg_t exrom;
260 int save;
261 struct cardbus_rom_image_head rom_image;
262 struct cardbus_rom_image *p;
263
264 save = splhigh();
265 /* enable rom address decoder */
266 exrom = cardbus_conf_read(cc, cf, tag, reg);
267 cardbus_conf_write(cc, cf, tag, reg, exrom | 1);
268
269 command = cardbus_conf_read(cc, cf, tag, CARDBUS_COMMAND_STATUS_REG);
270 cardbus_conf_write(cc, cf, tag, CARDBUS_COMMAND_STATUS_REG,
271 command | CARDBUS_COMMAND_MEM_ENABLE);
272
273 if(cardbus_read_exrom(ca->ca_memt, bar_memh, &rom_image))
274 goto out;
275
276 for(p = SIMPLEQ_FIRST(&rom_image);
277 p;
278 p = SIMPLEQ_NEXT(p, next)) {
279 if(p->rom_image == CARDBUS_CIS_ASI_ROM_IMAGE(cis_ptr)) {
280 bus_space_read_region_1(p->romt, p->romh,
281 CARDBUS_CIS_ADDR(cis_ptr),
282 tuples, 256);
283 found++;
284 }
285 break;
286 }
287 while((p = SIMPLEQ_FIRST(&rom_image)) != NULL) {
288 SIMPLEQ_REMOVE_HEAD(&rom_image, p, next);
289 free(p, M_DEVBUF);
290 }
291 out:
292 exrom = cardbus_conf_read(cc, cf, tag, reg);
293 cardbus_conf_write(cc, cf, tag, reg, exrom & ~1);
294 splx(save);
295 } else {
296 command = cardbus_conf_read(cc, cf, tag, CARDBUS_COMMAND_STATUS_REG);
297 cardbus_conf_write(cc, cf, tag, CARDBUS_COMMAND_STATUS_REG,
298 command | CARDBUS_COMMAND_MEM_ENABLE);
299 /* XXX byte order? */
300 bus_space_read_region_1(ca->ca_memt, bar_memh,
301 cis_ptr, tuples, 256);
302 found++;
303 }
304 command = cardbus_conf_read(cc, cf, tag, CARDBUS_COMMAND_STATUS_REG);
305 cardbus_conf_write(cc, cf, tag, CARDBUS_COMMAND_STATUS_REG,
306 command & ~CARDBUS_COMMAND_MEM_ENABLE);
307 cardbus_conf_write(cc, cf, tag, reg, 0);
308 #if 0
309 /* XXX unmap memory */
310 (*ca->ca_ct->ct_cf->cardbus_space_free)(ca->ca_ct,
311 ca->ca_ct->ct_sc->sc_rbus_memt,
312 bar_memh, bar_size);
313 #endif
314 break;
315
316 #ifdef DIAGNOSTIC
317 default:
318 panic("%s: bad CIS space (%d)", sc->sc_dev.dv_xname, cardbus_space);
319 #endif
320 }
321 return !found;
322 }
323
324 static void
325 parse_tuple(u_int8_t *tuple, int len, void *data)
326 {
327 #ifdef CARDBUS_DEBUG
328 static const char __func__[] = "parse_tuple";
329 #endif
330 struct cardbus_cis_info *cis = data;
331 int bar_index;
332 int i;
333 char *p;
334 switch(tuple[0]) {
335 case PCMCIA_CISTPL_MANFID:
336 if(tuple[1] != 5) {
337 DPRINTF(("%s: wrong length manufacturer id (%d)\n",
338 __func__, tuple[1]));
339 break;
340 }
341 cis->manufacturer = tuple[2] | (tuple[3] << 8);
342 cis->product = tuple[4] | (tuple[5] << 8);
343 break;
344 case PCMCIA_CISTPL_VERS_1:
345 memcpy(cis->cis1_info_buf, tuple + 2, tuple[1]);
346 i = 0;
347 p = cis->cis1_info_buf + 2;
348 while(i < sizeof(cis->cis1_info) / sizeof(cis->cis1_info[0])) {
349 cis->cis1_info[i++] = p;
350 while(*p != '\0' && *p != '\xff')
351 p++;
352 if(*p == '\xff')
353 break;
354 p++;
355 }
356 break;
357 case PCMCIA_CISTPL_BAR:
358 if(tuple[1] != 6) {
359 DPRINTF(("%s: BAR with short length (%d)\n", __func__, tuple[1]));
360 break;
361 }
362 bar_index = tuple[2] & 7;
363 if(bar_index == 0) {
364 DPRINTF(("%s: invalid ASI in BAR tuple\n", __func__));
365 break;
366 }
367 bar_index--;
368 cis->bar[bar_index].flags = tuple[2];
369 cis->bar[bar_index].size = (tuple[4] << 0) |
370 (tuple[5] << 8) |
371 (tuple[6] << 16) |
372 (tuple[7] << 24);
373 break;
374 case PCMCIA_CISTPL_FUNCID:
375 cis->funcid = tuple[2];
376 break;
377
378 case PCMCIA_CISTPL_FUNCE:
379 if(cis->funcid == PCMCIA_FUNCTION_NETWORK && tuple[1] >= 8) {
380 if(tuple[2] == PCMCIA_TPLFE_TYPE_LAN_NID) {
381 if(tuple[3] > sizeof(cis->funce.network.netid)) {
382 DPRINTF(("%s: unknown network id type (len = %d)\n",
383 __func__, tuple[3]));
384 } else {
385 memcpy(cis->funce.network.netid,
386 tuple + 4, tuple[3]);
387 }
388 }
389 }
390 break;
391 }
392 }
393
394 /*
395 * int cardbus_attach_card(struct cardbus_softc *sc)
396 *
397 * This function attaches the card on the slot: turns on power,
398 * reads and analyses tuple, sets consifuration index.
399 *
400 * This function returns the number of recognised device functions.
401 * If no functions are recognised, return 0.
402 */
403 int
404 cardbus_attach_card(sc)
405 struct cardbus_softc *sc;
406 {
407 cardbus_chipset_tag_t cc;
408 cardbus_function_tag_t cf;
409 int cdstatus;
410 cardbustag_t tag;
411 cardbusreg_t id, class, cis_ptr;
412 cardbusreg_t bhlc;
413 u_int8_t tuple[2048];
414 int function, nfunction;
415 struct cardbus_devfunc **previous_next = &(sc->sc_funcs);
416 struct device *csc;
417 int no_work_funcs = 0;
418 cardbus_devfunc_t ct;
419
420 cc = sc->sc_cc;
421 cf = sc->sc_cf;
422
423 DPRINTF(("cardbus_attach_card: cb%d start\n", sc->sc_dev.dv_unit));
424
425 /* inspect initial voltage */
426 if (0 == (cdstatus = (cf->cardbus_ctrl)(cc, CARDBUS_CD))) {
427 DPRINTF(("cardbusattach: no CardBus card on cb%d\n", sc->sc_dev.dv_unit));
428 return 0;
429 }
430
431 enable_function(sc, cdstatus, 8); /* XXX use fake function 8 to
432 keep power on during whole
433 configuration */
434 function = 0;
435
436 tag = cardbus_make_tag(cc, cf, sc->sc_bus, sc->sc_device, function);
437
438 /*
439 * Wait until power comes up. Maxmum 500 ms.
440 */
441 {
442 int i;
443 for (i = 0; i < 5; ++i) {
444 id = cardbus_conf_read(cc, cf, tag, CARDBUS_ID_REG);
445 if (id != 0xffffffff && id != 0) {
446 break;
447 }
448 delay(100*1000); /* or tsleep */
449 }
450 if (i == 5) {
451 return 0;
452 }
453 }
454
455 bhlc = cardbus_conf_read(cc, cf, tag, CARDBUS_BHLC_REG);
456 if (CARDBUS_LATTIMER(bhlc) < 0x10) {
457 bhlc &= ~(CARDBUS_LATTIMER_MASK << CARDBUS_LATTIMER_SHIFT);
458 bhlc |= (0x10 << CARDBUS_LATTIMER_SHIFT);
459 cardbus_conf_write(cc, cf, tag, CARDBUS_BHLC_REG, bhlc);
460 }
461
462 nfunction = CARDBUS_HDRTYPE_MULTIFN(bhlc) ? 8 : 1;
463
464 for(function = 0; function < nfunction; function++) {
465 struct cardbus_attach_args ca;
466
467 tag = cardbus_make_tag(cc, cf, sc->sc_bus, sc->sc_device, function);
468
469 id = cardbus_conf_read(cc, cf, tag, CARDBUS_ID_REG);
470 class = cardbus_conf_read(cc, cf, tag, CARDBUS_CLASS_REG);
471 cis_ptr = cardbus_conf_read(cc, cf, tag, CARDBUS_CIS_REG);
472
473 /* Invalid vendor ID value? */
474 if (CARDBUS_VENDOR(id) == CARDBUS_VENDOR_INVALID) {
475 continue;
476 }
477
478 DPRINTF(("cardbus_attach_card: Vendor 0x%x, Product 0x%x, CIS 0x%x\n",
479 CARDBUS_VENDOR(id), CARDBUS_PRODUCT(id), cis_ptr));
480
481 enable_function(sc, cdstatus, function);
482
483 /* clean up every BAR */
484 cardbus_conf_write(cc, cf, tag, CARDBUS_BASE0_REG, 0);
485 cardbus_conf_write(cc, cf, tag, CARDBUS_BASE1_REG, 0);
486 cardbus_conf_write(cc, cf, tag, CARDBUS_BASE2_REG, 0);
487 cardbus_conf_write(cc, cf, tag, CARDBUS_BASE3_REG, 0);
488 cardbus_conf_write(cc, cf, tag, CARDBUS_BASE4_REG, 0);
489 cardbus_conf_write(cc, cf, tag, CARDBUS_BASE5_REG, 0);
490 cardbus_conf_write(cc, cf, tag, CARDBUS_ROM_REG, 0);
491
492 /*
493 * We need to allocate the ct here, since we might
494 * need it when reading the CIS
495 */
496 if (NULL == (ct = (cardbus_devfunc_t)malloc(sizeof(struct cardbus_devfunc),
497 M_DEVBUF, M_NOWAIT))) {
498 panic("no room for cardbus_tag");
499 }
500
501 ct->ct_cc = sc->sc_cc;
502 ct->ct_cf = sc->sc_cf;
503 ct->ct_bus = sc->sc_bus;
504 ct->ct_dev = sc->sc_device;
505 ct->ct_func = function;
506 ct->ct_sc = sc;
507 ct->ct_next = NULL;
508 *previous_next = ct;
509
510 memset(&ca, 0, sizeof(ca));
511
512 ca.ca_unit = sc->sc_dev.dv_unit;
513 ca.ca_ct = ct;
514
515 ca.ca_iot = sc->sc_iot;
516 ca.ca_memt = sc->sc_memt;
517 ca.ca_dmat = sc->sc_dmat;
518
519 ca.ca_tag = tag;
520 ca.ca_device = sc->sc_device;
521 ca.ca_function = function;
522 ca.ca_id = id;
523 ca.ca_class = class;
524
525 ca.ca_intrline = sc->sc_intrline;
526
527 bzero(tuple, 2048);
528
529 if(cardbus_read_tuples(&ca, cis_ptr, tuple, sizeof(tuple))) {
530 printf("cardbus_attach_card: failed to read CIS\n");
531 } else {
532 #ifdef CARDBUS_DEBUG
533 decode_tuples(tuple, 2048, print_tuple, NULL);
534 #endif
535 decode_tuples(tuple, 2048, parse_tuple, &ca.ca_cis);
536 }
537
538 if (NULL == (csc = config_found_sm((void *)sc, &ca, cardbusprint, cardbussubmatch))) {
539 /* do not match */
540 disable_function(sc, function);
541 free(ct, M_DEVBUF);
542 *previous_next = NULL;
543 } else {
544 /* found */
545 previous_next = &(ct->ct_next);
546 ct->ct_device = csc;
547 ++no_work_funcs;
548 }
549 }
550 /*
551 * XXX power down pseudo function 8 (this will power down the card
552 * if no functions were attached).
553 */
554 disable_function(sc, 8);
555
556 return no_work_funcs;
557 }
558
559
560 static int
561 #ifdef __BROKEN_INDIRECT_CONFIG
562 cardbussubmatch(parent, match, aux)
563 #else
564 cardbussubmatch(parent, cf, aux)
565 #endif
566 struct device *parent;
567 #ifdef __BROKEN_INDIRECT_CONFIG
568 void *match;
569 #else
570 struct cfdata *cf;
571 #endif
572 void *aux;
573 {
574 #ifdef __BROKEN_INDIRECT_CONFIG
575 struct cfdata *cf = match;
576 #endif
577 struct cardbus_attach_args *ca = aux;
578
579 if (cf->cardbuscf_dev != CARDBUS_UNK_DEV &&
580 cf->cardbuscf_dev != ca->ca_unit) {
581 return 0;
582 }
583 if (cf->cardbuscf_function != CARDBUS_UNK_FUNCTION &&
584 cf->cardbuscf_function != ca->ca_function) {
585 return 0;
586 }
587
588 return ((*cf->cf_attach->ca_match)(parent, cf, aux));
589 }
590
591
592
593 static int
594 cardbusprint(aux, pnp)
595 void *aux;
596 const char *pnp;
597 {
598 struct cardbus_attach_args *ca = aux;
599 char devinfo[256];
600 int i;
601 if (pnp) {
602 pci_devinfo(ca->ca_id, ca->ca_class, 1, devinfo);
603 for (i = 0; i < 4; i++) {
604 if (ca->ca_cis.cis1_info[i] == NULL)
605 break;
606 if (i)
607 printf(", ");
608 printf("%s", ca->ca_cis.cis1_info[i]);
609 }
610 if (i)
611 printf(" ");
612 printf("(manufacturer 0x%x, product 0x%x)", ca->ca_cis.manufacturer,
613 ca->ca_cis.product);
614 printf(" %s at %s", devinfo, pnp);
615 }
616 printf(" dev %d function %d", ca->ca_device, ca->ca_function);
617
618 return UNCONF;
619 }
620
621
622
623
624
625
626 /*
627 * void *cardbus_intr_establish(cc, cf, irq, level, func, arg)
628 * Interrupt handler of pccard.
629 * args:
630 * cardbus_chipset_tag_t *cc
631 * int irq:
632 */
633 void *
634 cardbus_intr_establish(cc, cf, irq, level, func, arg)
635 cardbus_chipset_tag_t cc;
636 cardbus_function_tag_t cf;
637 cardbus_intr_handle_t irq;
638 int level;
639 int (*func) __P((void *));
640 void *arg;
641 {
642 DPRINTF(("- cardbus_intr_establish: irq %d\n", irq));
643
644 return (*cf->cardbus_intr_establish)(cc, irq, level, func, arg);
645 }
646
647
648
649 /*
650 * void cardbus_intr_disestablish(cc, cf, handler)
651 * Interrupt handler of pccard.
652 * args:
653 * cardbus_chipset_tag_t *cc
654 */
655 void
656 cardbus_intr_disestablish(cc, cf, handler)
657 cardbus_chipset_tag_t cc;
658 cardbus_function_tag_t cf;
659 void *handler;
660 {
661 DPRINTF(("- pccard_intr_disestablish\n"));
662
663 (*cf->cardbus_intr_disestablish)(cc, handler);
664 return;
665 }
666
667
668
669 /* XXX this should be merged with cardbus_function_{enable,disable},
670 but we don't have a ct when these functions are called */
671
672 static void
673 enable_function(sc, cdstatus, function)
674 struct cardbus_softc *sc;
675 int cdstatus;
676 int function;
677 {
678 if(sc->sc_poweron_func == 0) {
679 if (cdstatus & CARDBUS_3V_CARD) {
680 sc->sc_cf->cardbus_power(sc->sc_cc, CARDBUS_VCC_3V);
681 }
682 (sc->sc_cf->cardbus_ctrl)(sc->sc_cc, CARDBUS_RESET);
683 }
684 sc->sc_poweron_func |= (1 << function);
685 }
686
687 static void
688 disable_function(sc, function)
689 struct cardbus_softc *sc;
690 int function;
691 {
692 sc->sc_poweron_func &= ~(1 << function);
693 if(sc->sc_poweron_func == 0) {
694 /* power-off because no functions are enabled */
695 sc->sc_cf->cardbus_power(sc->sc_cc, CARDBUS_VCC_0V);
696 }
697 }
698
699 /*
700 * int cardbus_function_enable(struct cardbus_softc *sc, int func)
701 *
702 * This function enables a function on a card. When no power is
703 * applied on the card, power will be applied on it.
704 */
705 int
706 cardbus_function_enable(sc, func)
707 struct cardbus_softc *sc;
708 int func;
709 {
710 cardbus_chipset_tag_t cc = sc->sc_cc;
711 cardbus_function_tag_t cf = sc->sc_cf;
712 cardbusreg_t command;
713 cardbustag_t tag;
714
715 DPRINTF(("entering cardbus_function_enable... "));
716
717 /* entering critical area */
718
719 enable_function(sc, CARDBUS_3V_CARD, func); /* XXX: sc_vold should be used */
720
721 /* exiting critical area */
722
723 tag = cardbus_make_tag(cc, cf, sc->sc_bus, sc->sc_device, func);
724
725 command = cardbus_conf_read(cc, cf, tag, CARDBUS_COMMAND_STATUS_REG);
726 command |= (CARDBUS_COMMAND_MEM_ENABLE | CARDBUS_COMMAND_IO_ENABLE | CARDBUS_COMMAND_MASTER_ENABLE); /* XXX: good guess needed */
727
728 cardbus_conf_write(cc, cf, tag, CARDBUS_COMMAND_STATUS_REG, command);
729
730 cardbus_free_tag(cc, cf, tag);
731
732 DPRINTF(("%x\n", sc->sc_poweron_func));
733
734 return 0;
735 }
736
737
738 /*
739 * int cardbus_function_disable(struct cardbus_softc *, int func)
740 *
741 * This function disable a function on a card. When no functions are
742 * enabled, it turns off the power.
743 */
744 int
745 cardbus_function_disable(sc, func)
746 struct cardbus_softc *sc;
747 int func;
748 {
749
750 DPRINTF(("entering cardbus_function_disable... "));
751
752 disable_function(sc, func);
753
754 return 0;
755 }
756
757
758 /*
759 * int cardbus_get_capability(cardbus_chipset_tag_t cc,
760 * cardbus_function_tag_t cf, cardbustag_t tag, int capid, int *offset,
761 * cardbusreg_t *value)
762 *
763 * Find the specified PCI capability.
764 */
765 int
766 cardbus_get_capability(cc, cf, tag, capid, offset, value)
767 cardbus_chipset_tag_t cc;
768 cardbus_function_tag_t cf;
769 cardbustag_t tag;
770 int capid;
771 int *offset;
772 cardbusreg_t *value;
773 {
774 cardbusreg_t reg;
775 unsigned int ofs;
776
777 reg = cardbus_conf_read(cc, cf, tag, PCI_COMMAND_STATUS_REG);
778 if (!(reg & PCI_STATUS_CAPLIST_SUPPORT))
779 return (0);
780
781 ofs = PCI_CAPLIST_PTR(cardbus_conf_read(cc, cf, tag,
782 PCI_CAPLISTPTR_REG));
783 while (ofs != 0) {
784 #ifdef DIAGNOSTIC
785 if ((ofs & 3) || (ofs < 0x40))
786 panic("cardbus_get_capability");
787 #endif
788 reg = cardbus_conf_read(cc, cf, tag, ofs);
789 if (PCI_CAPLIST_CAP(reg) == capid) {
790 if (offset)
791 *offset = ofs;
792 if (value)
793 *value = reg;
794 return (1);
795 }
796 ofs = PCI_CAPLIST_NEXT(reg);
797 }
798
799 return (0);
800 }
801
802
803 /*
804 * below this line, there are some functions for decoding tuples.
805 * They should go out from this file.
806 */
807
808 static u_int8_t *
809 decode_tuple __P((u_int8_t *tuple, tuple_decode_func func, void *data));
810
811 static int
812 decode_tuples(tuple, buflen, func, data)
813 u_int8_t *tuple;
814 int buflen;
815 tuple_decode_func func;
816 void *data;
817 {
818 u_int8_t *tp = tuple;
819
820 if (CISTPL_LINKTARGET != *tuple) {
821 DPRINTF(("WRONG TUPLE: 0x%x\n", *tuple));
822 return 0;
823 }
824
825 while (NULL != (tp = decode_tuple(tp, func, data))) {
826 if (tuple + buflen < tp) {
827 break;
828 }
829 }
830
831 return 1;
832 }
833
834
835 static u_int8_t *
836 decode_tuple(tuple, func, data)
837 u_int8_t *tuple;
838 tuple_decode_func func;
839 void *data;
840 {
841 u_int8_t type;
842 u_int8_t len;
843
844 type = tuple[0];
845 len = tuple[1] + 2;
846
847 (*func)(tuple, len, data);
848
849 if (CISTPL_END == type) {
850 return NULL;
851 }
852
853 return tuple + len;
854 }
855
856
857 #ifdef CARDBUS_DEBUG
858 static char *tuple_name __P((int type));
859
860 static char *
861 tuple_name(type)
862 int type;
863 {
864 static char *tuple_name_s [] = {
865 "TPL_NULL", "TPL_DEVICE", "Reserved", "Reserved", /* 0-3 */
866 "CONFIG_CB", "CFTABLE_ENTRY_CB", "Reserved", "BAR", /* 4-7 */
867 "Reserved", "Reserved", "Reserved", "Reserved", /* 8-B */
868 "Reserved", "Reserved", "Reserved", "Reserved", /* C-F */
869 "CHECKSUM", "LONGLINK_A", "LONGLINK_C", "LINKTARGET", /* 10-13 */
870 "NO_LINK", "VERS_1", "ALTSTR", "DEVICE_A",
871 "JEDEC_C", "JEDEC_A", "CONFIG", "CFTABLE_ENTRY",
872 "DEVICE_OC", "DEVICE_OA", "DEVICE_GEO", "DEVICE_GEO_A",
873 "MANFID", "FUNCID", "FUNCE", "SWIL", /* 20-23 */
874 "Reserved", "Reserved", "Reserved", "Reserved", /* 24-27 */
875 "Reserved", "Reserved", "Reserved", "Reserved", /* 28-2B */
876 "Reserved", "Reserved", "Reserved", "Reserved", /* 2C-2F */
877 "Reserved", "Reserved", "Reserved", "Reserved", /* 30-33 */
878 "Reserved", "Reserved", "Reserved", "Reserved", /* 34-37 */
879 "Reserved", "Reserved", "Reserved", "Reserved", /* 38-3B */
880 "Reserved", "Reserved", "Reserved", "Reserved", /* 3C-3F */
881 "VERS_2", "FORMAT", "GEOMETRY", "BYTEORDER",
882 "DATE", "BATTERY", "ORG"
883 };
884 #define NAME_LEN(x) (sizeof x / sizeof(x[0]))
885
886 if (type > 0 && type < NAME_LEN(tuple_name_s)) {
887 return tuple_name_s[type];
888 } else if (0xff == type) {
889 return "END";
890 } else {
891 return "Reserved";
892 }
893 }
894
895 static void
896 print_tuple(tuple, len, data)
897 u_int8_t *tuple;
898 int len;
899 void *data;
900 {
901 int i;
902
903 printf("tuple: %s len %d\n", tuple_name(tuple[0]), len);
904
905 for (i = 0; i < len; ++i) {
906 if (i % 16 == 0) {
907 printf(" 0x%2x:", i);
908 }
909 printf(" %x",tuple[i]);
910 if (i % 16 == 15) {
911 printf("\n");
912 }
913 }
914 if (i % 16 != 0) {
915 printf("\n");
916 }
917 }
918
919 #endif
920
921