cardbus.c revision 1.16 1 /* $NetBSD: cardbus.c,v 1.16 1999/12/11 00:29:11 thorpej 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 ca.ca_unit = sc->sc_dev.dv_unit;
511 ca.ca_ct = ct;
512
513 ca.ca_iot = sc->sc_iot;
514 ca.ca_memt = sc->sc_memt;
515 ca.ca_dmat = sc->sc_dmat;
516
517 ca.ca_tag = tag;
518 ca.ca_device = sc->sc_device;
519 ca.ca_function = function;
520 ca.ca_id = id;
521 ca.ca_class = class;
522
523 ca.ca_intrline = sc->sc_intrline;
524
525 bzero(tuple, 2048);
526
527 if(cardbus_read_tuples(&ca, cis_ptr, tuple, sizeof(tuple))) {
528 printf("cardbus_attach_card: failed to read CIS\n");
529 } else {
530 #ifdef CARDBUS_DEBUG
531 decode_tuples(tuple, 2048, print_tuple, NULL);
532 #endif
533 decode_tuples(tuple, 2048, parse_tuple, &ca.ca_cis);
534 }
535
536 if (NULL == (csc = config_found_sm((void *)sc, &ca, cardbusprint, cardbussubmatch))) {
537 /* do not match */
538 disable_function(sc, function);
539 free(ct, M_DEVBUF);
540 *previous_next = NULL;
541 } else {
542 /* found */
543 previous_next = &(ct->ct_next);
544 ct->ct_device = csc;
545 ++no_work_funcs;
546 }
547 }
548 /*
549 * XXX power down pseudo function 8 (this will power down the card
550 * if no functions were attached).
551 */
552 disable_function(sc, 8);
553
554 return no_work_funcs;
555 }
556
557
558 static int
559 #ifdef __BROKEN_INDIRECT_CONFIG
560 cardbussubmatch(parent, match, aux)
561 #else
562 cardbussubmatch(parent, cf, aux)
563 #endif
564 struct device *parent;
565 #ifdef __BROKEN_INDIRECT_CONFIG
566 void *match;
567 #else
568 struct cfdata *cf;
569 #endif
570 void *aux;
571 {
572 #ifdef __BROKEN_INDIRECT_CONFIG
573 struct cfdata *cf = match;
574 #endif
575 struct cardbus_attach_args *ca = aux;
576
577 if (cf->cardbuscf_dev != CARDBUS_UNK_DEV &&
578 cf->cardbuscf_dev != ca->ca_unit) {
579 return 0;
580 }
581 if (cf->cardbuscf_function != CARDBUS_UNK_FUNCTION &&
582 cf->cardbuscf_function != ca->ca_function) {
583 return 0;
584 }
585
586 return ((*cf->cf_attach->ca_match)(parent, cf, aux));
587 }
588
589
590
591 static int
592 cardbusprint(aux, pnp)
593 void *aux;
594 const char *pnp;
595 {
596 struct cardbus_attach_args *ca = aux;
597 char devinfo[256];
598 int i;
599 if (pnp) {
600 pci_devinfo(ca->ca_id, ca->ca_class, 1, devinfo);
601 for (i = 0; i < 4; i++) {
602 if (ca->ca_cis.cis1_info[i] == NULL)
603 break;
604 if (i)
605 printf(", ");
606 printf("%s", ca->ca_cis.cis1_info[i]);
607 }
608 if (i)
609 printf(" ");
610 printf("(manufacturer 0x%x, product 0x%x)", ca->ca_cis.manufacturer,
611 ca->ca_cis.product);
612 printf(" %s at %s", devinfo, pnp);
613 }
614 printf(" dev %d function %d", ca->ca_device, ca->ca_function);
615
616 return UNCONF;
617 }
618
619
620
621
622
623
624 /*
625 * void *cardbus_intr_establish(cc, cf, irq, level, func, arg)
626 * Interrupt handler of pccard.
627 * args:
628 * cardbus_chipset_tag_t *cc
629 * int irq:
630 */
631 void *
632 cardbus_intr_establish(cc, cf, irq, level, func, arg)
633 cardbus_chipset_tag_t cc;
634 cardbus_function_tag_t cf;
635 cardbus_intr_handle_t irq;
636 int level;
637 int (*func) __P((void *));
638 void *arg;
639 {
640 DPRINTF(("- cardbus_intr_establish: irq %d\n", irq));
641
642 return (*cf->cardbus_intr_establish)(cc, irq, level, func, arg);
643 }
644
645
646
647 /*
648 * void cardbus_intr_disestablish(cc, cf, handler)
649 * Interrupt handler of pccard.
650 * args:
651 * cardbus_chipset_tag_t *cc
652 */
653 void
654 cardbus_intr_disestablish(cc, cf, handler)
655 cardbus_chipset_tag_t cc;
656 cardbus_function_tag_t cf;
657 void *handler;
658 {
659 DPRINTF(("- pccard_intr_disestablish\n"));
660
661 (*cf->cardbus_intr_disestablish)(cc, handler);
662 return;
663 }
664
665
666
667 /* XXX this should be merged with cardbus_function_{enable,disable},
668 but we don't have a ct when these functions are called */
669
670 static void
671 enable_function(sc, cdstatus, function)
672 struct cardbus_softc *sc;
673 int cdstatus;
674 int function;
675 {
676 if(sc->sc_poweron_func == 0) {
677 if (cdstatus & CARDBUS_3V_CARD) {
678 sc->sc_cf->cardbus_power(sc->sc_cc, CARDBUS_VCC_3V);
679 }
680 (sc->sc_cf->cardbus_ctrl)(sc->sc_cc, CARDBUS_RESET);
681 }
682 sc->sc_poweron_func |= (1 << function);
683 }
684
685 static void
686 disable_function(sc, function)
687 struct cardbus_softc *sc;
688 int function;
689 {
690 sc->sc_poweron_func &= ~(1 << function);
691 if(sc->sc_poweron_func == 0) {
692 /* power-off because no functions are enabled */
693 sc->sc_cf->cardbus_power(sc->sc_cc, CARDBUS_VCC_0V);
694 }
695 }
696
697 /*
698 * int cardbus_function_enable(struct cardbus_softc *sc, int func)
699 *
700 * This function enables a function on a card. When no power is
701 * applied on the card, power will be applied on it.
702 */
703 int
704 cardbus_function_enable(sc, func)
705 struct cardbus_softc *sc;
706 int func;
707 {
708 cardbus_chipset_tag_t cc = sc->sc_cc;
709 cardbus_function_tag_t cf = sc->sc_cf;
710 cardbusreg_t command;
711 cardbustag_t tag;
712
713 DPRINTF(("entering cardbus_function_enable... "));
714
715 /* entering critical area */
716
717 enable_function(sc, CARDBUS_3V_CARD, func); /* XXX: sc_vold should be used */
718
719 /* exiting critical area */
720
721 tag = cardbus_make_tag(cc, cf, sc->sc_bus, sc->sc_device, func);
722
723 command = cardbus_conf_read(cc, cf, tag, CARDBUS_COMMAND_STATUS_REG);
724 command |= (CARDBUS_COMMAND_MEM_ENABLE | CARDBUS_COMMAND_IO_ENABLE | CARDBUS_COMMAND_MASTER_ENABLE); /* XXX: good guess needed */
725
726 cardbus_conf_write(cc, cf, tag, CARDBUS_COMMAND_STATUS_REG, command);
727
728 cardbus_free_tag(cc, cf, tag);
729
730 DPRINTF(("%x\n", sc->sc_poweron_func));
731
732 return 0;
733 }
734
735
736 /*
737 * int cardbus_function_disable(struct cardbus_softc *, int func)
738 *
739 * This function disable a function on a card. When no functions are
740 * enabled, it turns off the power.
741 */
742 int
743 cardbus_function_disable(sc, func)
744 struct cardbus_softc *sc;
745 int func;
746 {
747
748 DPRINTF(("entering cardbus_function_disable... "));
749
750 disable_function(sc, func);
751
752 return 0;
753 }
754
755
756 /*
757 * int cardbus_get_capability(cardbus_chipset_tag_t cc,
758 * cardbus_function_tag_t cf, cardbustag_t tag, int capid, int *offset,
759 * cardbusreg_t *value)
760 *
761 * Find the specified PCI capability.
762 */
763 int
764 cardbus_get_capability(cc, cf, tag, capid, offset, value)
765 cardbus_chipset_tag_t cc;
766 cardbus_function_tag_t cf;
767 cardbustag_t tag;
768 int capid;
769 int *offset;
770 cardbusreg_t *value;
771 {
772 cardbusreg_t reg;
773 unsigned int ofs;
774
775 reg = cardbus_conf_read(cc, cf, tag, PCI_COMMAND_STATUS_REG);
776 if (!(reg & PCI_STATUS_CAPLIST_SUPPORT))
777 return (0);
778
779 ofs = PCI_CAPLIST_PTR(cardbus_conf_read(cc, cf, tag,
780 PCI_CAPLISTPTR_REG));
781 while (ofs != 0) {
782 #ifdef DIAGNOSTIC
783 if ((ofs & 3) || (ofs < 0x40))
784 panic("cardbus_get_capability");
785 #endif
786 reg = cardbus_conf_read(cc, cf, tag, ofs);
787 if (PCI_CAPLIST_CAP(reg) == capid) {
788 if (offset)
789 *offset = ofs;
790 if (value)
791 *value = reg;
792 return (1);
793 }
794 ofs = PCI_CAPLIST_NEXT(reg);
795 }
796
797 return (0);
798 }
799
800
801 /*
802 * below this line, there are some functions for decoding tuples.
803 * They should go out from this file.
804 */
805
806 static u_int8_t *
807 decode_tuple __P((u_int8_t *tuple, tuple_decode_func func, void *data));
808
809 static int
810 decode_tuples(tuple, buflen, func, data)
811 u_int8_t *tuple;
812 int buflen;
813 tuple_decode_func func;
814 void *data;
815 {
816 u_int8_t *tp = tuple;
817
818 if (CISTPL_LINKTARGET != *tuple) {
819 DPRINTF(("WRONG TUPLE: 0x%x\n", *tuple));
820 return 0;
821 }
822
823 while (NULL != (tp = decode_tuple(tp, func, data))) {
824 if (tuple + buflen < tp) {
825 break;
826 }
827 }
828
829 return 1;
830 }
831
832
833 static u_int8_t *
834 decode_tuple(tuple, func, data)
835 u_int8_t *tuple;
836 tuple_decode_func func;
837 void *data;
838 {
839 u_int8_t type;
840 u_int8_t len;
841
842 type = tuple[0];
843 len = tuple[1] + 2;
844
845 (*func)(tuple, len, data);
846
847 if (CISTPL_END == type) {
848 return NULL;
849 }
850
851 return tuple + len;
852 }
853
854
855 #ifdef CARDBUS_DEBUG
856 static char *tuple_name __P((int type));
857
858 static char *
859 tuple_name(type)
860 int type;
861 {
862 static char *tuple_name_s [] = {
863 "TPL_NULL", "TPL_DEVICE", "Reserved", "Reserved", /* 0-3 */
864 "CONFIG_CB", "CFTABLE_ENTRY_CB", "Reserved", "BAR", /* 4-7 */
865 "Reserved", "Reserved", "Reserved", "Reserved", /* 8-B */
866 "Reserved", "Reserved", "Reserved", "Reserved", /* C-F */
867 "CHECKSUM", "LONGLINK_A", "LONGLINK_C", "LINKTARGET", /* 10-13 */
868 "NO_LINK", "VERS_1", "ALTSTR", "DEVICE_A",
869 "JEDEC_C", "JEDEC_A", "CONFIG", "CFTABLE_ENTRY",
870 "DEVICE_OC", "DEVICE_OA", "DEVICE_GEO", "DEVICE_GEO_A",
871 "MANFID", "FUNCID", "FUNCE", "SWIL", /* 20-23 */
872 "Reserved", "Reserved", "Reserved", "Reserved", /* 24-27 */
873 "Reserved", "Reserved", "Reserved", "Reserved", /* 28-2B */
874 "Reserved", "Reserved", "Reserved", "Reserved", /* 2C-2F */
875 "Reserved", "Reserved", "Reserved", "Reserved", /* 30-33 */
876 "Reserved", "Reserved", "Reserved", "Reserved", /* 34-37 */
877 "Reserved", "Reserved", "Reserved", "Reserved", /* 38-3B */
878 "Reserved", "Reserved", "Reserved", "Reserved", /* 3C-3F */
879 "VERS_2", "FORMAT", "GEOMETRY", "BYTEORDER",
880 "DATE", "BATTERY", "ORG"
881 };
882 #define NAME_LEN(x) (sizeof x / sizeof(x[0]))
883
884 if (type > 0 && type < NAME_LEN(tuple_name_s)) {
885 return tuple_name_s[type];
886 } else if (0xff == type) {
887 return "END";
888 } else {
889 return "Reserved";
890 }
891 }
892
893 static void
894 print_tuple(tuple, len, data)
895 u_int8_t *tuple;
896 int len;
897 void *data;
898 {
899 int i;
900
901 printf("tuple: %s len %d\n", tuple_name(tuple[0]), len);
902
903 for (i = 0; i < len; ++i) {
904 if (i % 16 == 0) {
905 printf(" 0x%2x:", i);
906 }
907 printf(" %x",tuple[i]);
908 if (i % 16 == 15) {
909 printf("\n");
910 }
911 }
912 if (i % 16 != 0) {
913 printf("\n");
914 }
915 }
916
917 #endif
918
919