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