cardbus.c revision 1.19 1 /* $NetBSD: cardbus.c,v 1.19 2000/01/31 08:49:07 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 #include <dev/cardbus/pccardcis.h>
51
52 #include <dev/cardbus/cardbus_exrom.h>
53
54 #include <dev/pci/pcivar.h> /* XXX */
55 #include <dev/pci/pcireg.h> /* XXX */
56
57 #include <dev/pcmcia/pcmciareg.h> /* XXX */
58
59 #if defined CARDBUS_DEBUG
60 #define STATIC
61 #define DPRINTF(a) printf a
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 if (cold) { /* before kernel thread invoked */
449 delay(100*1000);
450 } else { /* thread context */
451 if (tsleep((void *)sc, PCATCH, "cardbus", hz/10) != EWOULDBLOCK) {
452 break;
453 }
454 }
455 }
456 if (i == 5) {
457 return 0;
458 }
459 }
460
461 bhlc = cardbus_conf_read(cc, cf, tag, CARDBUS_BHLC_REG);
462 if (CARDBUS_LATTIMER(bhlc) < 0x10) {
463 bhlc &= ~(CARDBUS_LATTIMER_MASK << CARDBUS_LATTIMER_SHIFT);
464 bhlc |= (0x10 << CARDBUS_LATTIMER_SHIFT);
465 cardbus_conf_write(cc, cf, tag, CARDBUS_BHLC_REG, bhlc);
466 }
467
468 nfunction = CARDBUS_HDRTYPE_MULTIFN(bhlc) ? 8 : 1;
469
470 for(function = 0; function < nfunction; function++) {
471 struct cardbus_attach_args ca;
472
473 tag = cardbus_make_tag(cc, cf, sc->sc_bus, sc->sc_device, function);
474
475 id = cardbus_conf_read(cc, cf, tag, CARDBUS_ID_REG);
476 class = cardbus_conf_read(cc, cf, tag, CARDBUS_CLASS_REG);
477 cis_ptr = cardbus_conf_read(cc, cf, tag, CARDBUS_CIS_REG);
478
479 /* Invalid vendor ID value? */
480 if (CARDBUS_VENDOR(id) == CARDBUS_VENDOR_INVALID) {
481 continue;
482 }
483
484 DPRINTF(("cardbus_attach_card: Vendor 0x%x, Product 0x%x, CIS 0x%x\n",
485 CARDBUS_VENDOR(id), CARDBUS_PRODUCT(id), cis_ptr));
486
487 enable_function(sc, cdstatus, function);
488
489 /* clean up every BAR */
490 cardbus_conf_write(cc, cf, tag, CARDBUS_BASE0_REG, 0);
491 cardbus_conf_write(cc, cf, tag, CARDBUS_BASE1_REG, 0);
492 cardbus_conf_write(cc, cf, tag, CARDBUS_BASE2_REG, 0);
493 cardbus_conf_write(cc, cf, tag, CARDBUS_BASE3_REG, 0);
494 cardbus_conf_write(cc, cf, tag, CARDBUS_BASE4_REG, 0);
495 cardbus_conf_write(cc, cf, tag, CARDBUS_BASE5_REG, 0);
496 cardbus_conf_write(cc, cf, tag, CARDBUS_ROM_REG, 0);
497
498 /*
499 * We need to allocate the ct here, since we might
500 * need it when reading the CIS
501 */
502 if (NULL == (ct = (cardbus_devfunc_t)malloc(sizeof(struct cardbus_devfunc),
503 M_DEVBUF, M_NOWAIT))) {
504 panic("no room for cardbus_tag");
505 }
506
507 ct->ct_cc = sc->sc_cc;
508 ct->ct_cf = sc->sc_cf;
509 ct->ct_bus = sc->sc_bus;
510 ct->ct_dev = sc->sc_device;
511 ct->ct_func = function;
512 ct->ct_sc = sc;
513 ct->ct_next = NULL;
514 *previous_next = ct;
515
516 memset(&ca, 0, sizeof(ca));
517
518 ca.ca_unit = sc->sc_dev.dv_unit;
519 ca.ca_ct = ct;
520
521 ca.ca_iot = sc->sc_iot;
522 ca.ca_memt = sc->sc_memt;
523 ca.ca_dmat = sc->sc_dmat;
524
525 ca.ca_tag = tag;
526 ca.ca_device = sc->sc_device;
527 ca.ca_function = function;
528 ca.ca_id = id;
529 ca.ca_class = class;
530
531 ca.ca_intrline = sc->sc_intrline;
532
533 bzero(tuple, 2048);
534
535 if(cardbus_read_tuples(&ca, cis_ptr, tuple, sizeof(tuple))) {
536 printf("cardbus_attach_card: failed to read CIS\n");
537 } else {
538 #ifdef CARDBUS_DEBUG
539 decode_tuples(tuple, 2048, print_tuple, NULL);
540 #endif
541 decode_tuples(tuple, 2048, parse_tuple, &ca.ca_cis);
542 }
543
544 if (NULL == (csc = config_found_sm((void *)sc, &ca, cardbusprint, cardbussubmatch))) {
545 /* do not match */
546 disable_function(sc, function);
547 free(ct, M_DEVBUF);
548 *previous_next = NULL;
549 } else {
550 /* found */
551 previous_next = &(ct->ct_next);
552 ct->ct_device = csc;
553 ++no_work_funcs;
554 }
555 }
556 /*
557 * XXX power down pseudo function 8 (this will power down the card
558 * if no functions were attached).
559 */
560 disable_function(sc, 8);
561
562 return no_work_funcs;
563 }
564
565
566 static int
567 #ifdef __BROKEN_INDIRECT_CONFIG
568 cardbussubmatch(parent, match, aux)
569 #else
570 cardbussubmatch(parent, cf, aux)
571 #endif
572 struct device *parent;
573 #ifdef __BROKEN_INDIRECT_CONFIG
574 void *match;
575 #else
576 struct cfdata *cf;
577 #endif
578 void *aux;
579 {
580 #ifdef __BROKEN_INDIRECT_CONFIG
581 struct cfdata *cf = match;
582 #endif
583 struct cardbus_attach_args *ca = aux;
584
585 if (cf->cardbuscf_dev != CARDBUS_UNK_DEV &&
586 cf->cardbuscf_dev != ca->ca_unit) {
587 return 0;
588 }
589 if (cf->cardbuscf_function != CARDBUS_UNK_FUNCTION &&
590 cf->cardbuscf_function != ca->ca_function) {
591 return 0;
592 }
593
594 return ((*cf->cf_attach->ca_match)(parent, cf, aux));
595 }
596
597
598
599 static int
600 cardbusprint(aux, pnp)
601 void *aux;
602 const char *pnp;
603 {
604 struct cardbus_attach_args *ca = aux;
605 char devinfo[256];
606 int i;
607 if (pnp) {
608 pci_devinfo(ca->ca_id, ca->ca_class, 1, devinfo);
609 for (i = 0; i < 4; i++) {
610 if (ca->ca_cis.cis1_info[i] == NULL)
611 break;
612 if (i)
613 printf(", ");
614 printf("%s", ca->ca_cis.cis1_info[i]);
615 }
616 if (i)
617 printf(" ");
618 printf("(manufacturer 0x%x, product 0x%x)", ca->ca_cis.manufacturer,
619 ca->ca_cis.product);
620 printf(" %s at %s", devinfo, pnp);
621 }
622 printf(" dev %d function %d", ca->ca_device, ca->ca_function);
623
624 return UNCONF;
625 }
626
627
628
629
630
631
632 /*
633 * void cardbus_detach_card(struct cardbus_softc *sc)
634 *
635 * This function detaches the card on the slot: detach device data
636 * structure and turns off the power.
637 *
638 * This function must not be called under interrupt context.
639 */
640 void
641 cardbus_detach_card(sc)
642 struct cardbus_softc *sc;
643 {
644 struct cardbus_devfunc *ct, *ct_next, **prev_next;
645
646 prev_next = &(sc->sc_funcs->ct_next);
647
648 for (ct = sc->sc_funcs; ct != NULL; ct = ct_next) {
649 struct device *fndev = ct->ct_device;
650 ct_next = ct->ct_next;
651
652 DPRINTF(("%s: detaching %s\n", sc->sc_dev.dv_xname, fndev->dv_xname));
653 /* call device detach function */
654
655 if (0 != config_detach(fndev, 0)) {
656 printf("%s: cannot detaching dev %s, function %d\n",
657 sc->sc_dev.dv_xname, fndev->dv_xname, ct->ct_func);
658 prev_next = &(ct->ct_next);
659 } else {
660 sc->sc_poweron_func &= ~(1 << ct->ct_func);
661 *prev_next = ct->ct_next;
662 free(ct, M_DEVBUF);
663 }
664 }
665
666 sc->sc_poweron_func = 0;
667 sc->sc_cf->cardbus_power(sc->sc_cc, CARDBUS_VCC_0V | CARDBUS_VPP_0V);
668 }
669
670
671
672
673 /*
674 * void *cardbus_intr_establish(cc, cf, irq, level, func, arg)
675 * Interrupt handler of pccard.
676 * args:
677 * cardbus_chipset_tag_t *cc
678 * int irq:
679 */
680 void *
681 cardbus_intr_establish(cc, cf, irq, level, func, arg)
682 cardbus_chipset_tag_t cc;
683 cardbus_function_tag_t cf;
684 cardbus_intr_handle_t irq;
685 int level;
686 int (*func) __P((void *));
687 void *arg;
688 {
689 DPRINTF(("- cardbus_intr_establish: irq %d\n", irq));
690
691 return (*cf->cardbus_intr_establish)(cc, irq, level, func, arg);
692 }
693
694
695
696 /*
697 * void cardbus_intr_disestablish(cc, cf, handler)
698 * Interrupt handler of pccard.
699 * args:
700 * cardbus_chipset_tag_t *cc
701 */
702 void
703 cardbus_intr_disestablish(cc, cf, handler)
704 cardbus_chipset_tag_t cc;
705 cardbus_function_tag_t cf;
706 void *handler;
707 {
708 DPRINTF(("- pccard_intr_disestablish\n"));
709
710 (*cf->cardbus_intr_disestablish)(cc, handler);
711 return;
712 }
713
714
715
716 /* XXX this should be merged with cardbus_function_{enable,disable},
717 but we don't have a ct when these functions are called */
718
719 static void
720 enable_function(sc, cdstatus, function)
721 struct cardbus_softc *sc;
722 int cdstatus;
723 int function;
724 {
725
726 if(sc->sc_poweron_func == 0) {
727 if (cdstatus & CARDBUS_3V_CARD) {
728 sc->sc_cf->cardbus_power(sc->sc_cc, CARDBUS_VCC_3V);
729 }
730 (sc->sc_cf->cardbus_ctrl)(sc->sc_cc, CARDBUS_RESET);
731 }
732 sc->sc_poweron_func |= (1 << function);
733 }
734
735 static void
736 disable_function(sc, function)
737 struct cardbus_softc *sc;
738 int function;
739 {
740
741 sc->sc_poweron_func &= ~(1 << function);
742 if(sc->sc_poweron_func == 0) {
743 /* power-off because no functions are enabled */
744 sc->sc_cf->cardbus_power(sc->sc_cc, CARDBUS_VCC_0V);
745 }
746 }
747
748 /*
749 * int cardbus_function_enable(struct cardbus_softc *sc, int func)
750 *
751 * This function enables a function on a card. When no power is
752 * applied on the card, power will be applied on it.
753 */
754 int
755 cardbus_function_enable(sc, func)
756 struct cardbus_softc *sc;
757 int func;
758 {
759 cardbus_chipset_tag_t cc = sc->sc_cc;
760 cardbus_function_tag_t cf = sc->sc_cf;
761 cardbusreg_t command;
762 cardbustag_t tag;
763
764 DPRINTF(("entering cardbus_function_enable... "));
765
766 /* entering critical area */
767
768 enable_function(sc, CARDBUS_3V_CARD, func); /* XXX: sc_vold should be used */
769
770 /* exiting critical area */
771
772 tag = cardbus_make_tag(cc, cf, sc->sc_bus, sc->sc_device, func);
773
774 command = cardbus_conf_read(cc, cf, tag, CARDBUS_COMMAND_STATUS_REG);
775 command |= (CARDBUS_COMMAND_MEM_ENABLE | CARDBUS_COMMAND_IO_ENABLE | CARDBUS_COMMAND_MASTER_ENABLE); /* XXX: good guess needed */
776
777 cardbus_conf_write(cc, cf, tag, CARDBUS_COMMAND_STATUS_REG, command);
778
779 cardbus_free_tag(cc, cf, tag);
780
781 DPRINTF(("%x\n", sc->sc_poweron_func));
782
783 return 0;
784 }
785
786
787 /*
788 * int cardbus_function_disable(struct cardbus_softc *, int func)
789 *
790 * This function disable a function on a card. When no functions are
791 * enabled, it turns off the power.
792 */
793 int
794 cardbus_function_disable(sc, func)
795 struct cardbus_softc *sc;
796 int func;
797 {
798
799 DPRINTF(("entering cardbus_function_disable... "));
800
801 disable_function(sc, func);
802
803 return 0;
804 }
805
806
807 /*
808 * int cardbus_get_capability(cardbus_chipset_tag_t cc,
809 * cardbus_function_tag_t cf, cardbustag_t tag, int capid, int *offset,
810 * cardbusreg_t *value)
811 *
812 * Find the specified PCI capability.
813 */
814 int
815 cardbus_get_capability(cc, cf, tag, capid, offset, value)
816 cardbus_chipset_tag_t cc;
817 cardbus_function_tag_t cf;
818 cardbustag_t tag;
819 int capid;
820 int *offset;
821 cardbusreg_t *value;
822 {
823 cardbusreg_t reg;
824 unsigned int ofs;
825
826 reg = cardbus_conf_read(cc, cf, tag, PCI_COMMAND_STATUS_REG);
827 if (!(reg & PCI_STATUS_CAPLIST_SUPPORT))
828 return (0);
829
830 ofs = PCI_CAPLIST_PTR(cardbus_conf_read(cc, cf, tag,
831 PCI_CAPLISTPTR_REG));
832 while (ofs != 0) {
833 #ifdef DIAGNOSTIC
834 if ((ofs & 3) || (ofs < 0x40))
835 panic("cardbus_get_capability");
836 #endif
837 reg = cardbus_conf_read(cc, cf, tag, ofs);
838 if (PCI_CAPLIST_CAP(reg) == capid) {
839 if (offset)
840 *offset = ofs;
841 if (value)
842 *value = reg;
843 return (1);
844 }
845 ofs = PCI_CAPLIST_NEXT(reg);
846 }
847
848 return (0);
849 }
850
851
852 /*
853 * below this line, there are some functions for decoding tuples.
854 * They should go out from this file.
855 */
856
857 static u_int8_t *
858 decode_tuple __P((u_int8_t *tuple, tuple_decode_func func, void *data));
859
860 static int
861 decode_tuples(tuple, buflen, func, data)
862 u_int8_t *tuple;
863 int buflen;
864 tuple_decode_func func;
865 void *data;
866 {
867 u_int8_t *tp = tuple;
868
869 if (CISTPL_LINKTARGET != *tuple) {
870 DPRINTF(("WRONG TUPLE: 0x%x\n", *tuple));
871 return 0;
872 }
873
874 while (NULL != (tp = decode_tuple(tp, func, data))) {
875 if (tuple + buflen < tp) {
876 break;
877 }
878 }
879
880 return 1;
881 }
882
883
884 static u_int8_t *
885 decode_tuple(tuple, func, data)
886 u_int8_t *tuple;
887 tuple_decode_func func;
888 void *data;
889 {
890 u_int8_t type;
891 u_int8_t len;
892
893 type = tuple[0];
894 len = tuple[1] + 2;
895
896 (*func)(tuple, len, data);
897
898 if (CISTPL_END == type) {
899 return NULL;
900 }
901
902 return tuple + len;
903 }
904
905
906 #ifdef CARDBUS_DEBUG
907 static char *tuple_name __P((int type));
908
909 static char *
910 tuple_name(type)
911 int type;
912 {
913 static char *tuple_name_s [] = {
914 "TPL_NULL", "TPL_DEVICE", "Reserved", "Reserved", /* 0-3 */
915 "CONFIG_CB", "CFTABLE_ENTRY_CB", "Reserved", "BAR", /* 4-7 */
916 "Reserved", "Reserved", "Reserved", "Reserved", /* 8-B */
917 "Reserved", "Reserved", "Reserved", "Reserved", /* C-F */
918 "CHECKSUM", "LONGLINK_A", "LONGLINK_C", "LINKTARGET", /* 10-13 */
919 "NO_LINK", "VERS_1", "ALTSTR", "DEVICE_A",
920 "JEDEC_C", "JEDEC_A", "CONFIG", "CFTABLE_ENTRY",
921 "DEVICE_OC", "DEVICE_OA", "DEVICE_GEO", "DEVICE_GEO_A",
922 "MANFID", "FUNCID", "FUNCE", "SWIL", /* 20-23 */
923 "Reserved", "Reserved", "Reserved", "Reserved", /* 24-27 */
924 "Reserved", "Reserved", "Reserved", "Reserved", /* 28-2B */
925 "Reserved", "Reserved", "Reserved", "Reserved", /* 2C-2F */
926 "Reserved", "Reserved", "Reserved", "Reserved", /* 30-33 */
927 "Reserved", "Reserved", "Reserved", "Reserved", /* 34-37 */
928 "Reserved", "Reserved", "Reserved", "Reserved", /* 38-3B */
929 "Reserved", "Reserved", "Reserved", "Reserved", /* 3C-3F */
930 "VERS_2", "FORMAT", "GEOMETRY", "BYTEORDER",
931 "DATE", "BATTERY", "ORG"
932 };
933 #define NAME_LEN(x) (sizeof x / sizeof(x[0]))
934
935 if (type > 0 && type < NAME_LEN(tuple_name_s)) {
936 return tuple_name_s[type];
937 } else if (0xff == type) {
938 return "END";
939 } else {
940 return "Reserved";
941 }
942 }
943
944 static void
945 print_tuple(tuple, len, data)
946 u_int8_t *tuple;
947 int len;
948 void *data;
949 {
950 int i;
951
952 printf("tuple: %s len %d\n", tuple_name(tuple[0]), len);
953
954 for (i = 0; i < len; ++i) {
955 if (i % 16 == 0) {
956 printf(" 0x%2x:", i);
957 }
958 printf(" %x",tuple[i]);
959 if (i % 16 == 15) {
960 printf("\n");
961 }
962 }
963 if (i % 16 != 0) {
964 printf("\n");
965 }
966 }
967
968 #endif
969
970