cardbus.c revision 1.25 1 /* $NetBSD: cardbus.c,v 1.25 2000/04/17 09:16:38 joda 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 switch(cis->funcid) {
364 case PCMCIA_FUNCTION_SERIAL:
365 if(tuple[1] >= 2 &&
366 tuple[2] == 0 /* XXX PCMCIA_TPLFE_TYPE_SERIAL_??? */) {
367 cis->funce.serial.uart_type = tuple[3] & 0x1f;
368 cis->funce.serial.uart_present = 1;
369 }
370 break;
371 case PCMCIA_FUNCTION_NETWORK:
372 if(tuple[1] >= 8 && tuple[2] == PCMCIA_TPLFE_TYPE_LAN_NID) {
373 if(tuple[3] > sizeof(cis->funce.network.netid)) {
374 DPRINTF(("%s: unknown network id type (len = %d)\n",
375 __func__, tuple[3]));
376 } else {
377 cis->funce.network.netid_present = 1;
378 memcpy(cis->funce.network.netid,
379 tuple + 4, tuple[3]);
380 }
381 }
382 break;
383 }
384 break;
385 }
386 }
387
388 /*
389 * int cardbus_attach_card(struct cardbus_softc *sc)
390 *
391 * This function attaches the card on the slot: turns on power,
392 * reads and analyses tuple, sets consifuration index.
393 *
394 * This function returns the number of recognised device functions.
395 * If no functions are recognised, return 0.
396 */
397 int
398 cardbus_attach_card(sc)
399 struct cardbus_softc *sc;
400 {
401 cardbus_chipset_tag_t cc;
402 cardbus_function_tag_t cf;
403 int cdstatus;
404 cardbustag_t tag;
405 cardbusreg_t id, class, cis_ptr;
406 cardbusreg_t bhlc;
407 u_int8_t tuple[2048];
408 int function, nfunction;
409 struct cardbus_devfunc **previous_next = &(sc->sc_funcs);
410 struct device *csc;
411 int no_work_funcs = 0;
412 cardbus_devfunc_t ct;
413
414 cc = sc->sc_cc;
415 cf = sc->sc_cf;
416
417 DPRINTF(("cardbus_attach_card: cb%d start\n", sc->sc_dev.dv_unit));
418
419 /* inspect initial voltage */
420 if (0 == (cdstatus = (cf->cardbus_ctrl)(cc, CARDBUS_CD))) {
421 DPRINTF(("cardbusattach: no CardBus card on cb%d\n", sc->sc_dev.dv_unit));
422 return 0;
423 }
424
425 enable_function(sc, cdstatus, 8); /* XXX use fake function 8 to
426 keep power on during whole
427 configuration */
428 function = 0;
429
430 tag = cardbus_make_tag(cc, cf, sc->sc_bus, sc->sc_device, function);
431
432 /*
433 * Wait until power comes up. Maxmum 500 ms.
434 */
435 {
436 int i;
437 for (i = 0; i < 5; ++i) {
438 id = cardbus_conf_read(cc, cf, tag, CARDBUS_ID_REG);
439 if (id != 0xffffffff && id != 0) {
440 break;
441 }
442 if (cold) { /* before kernel thread invoked */
443 delay(100*1000);
444 } else { /* thread context */
445 if (tsleep((void *)sc, PCATCH, "cardbus", hz/10) != EWOULDBLOCK) {
446 break;
447 }
448 }
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_detach_card(struct cardbus_softc *sc)
628 *
629 * This function detaches the card on the slot: detach device data
630 * structure and turns off the power.
631 *
632 * This function must not be called under interrupt context.
633 */
634 void
635 cardbus_detach_card(sc)
636 struct cardbus_softc *sc;
637 {
638 struct cardbus_devfunc *ct, *ct_next, **prev_next;
639
640 prev_next = &(sc->sc_funcs->ct_next);
641
642 for (ct = sc->sc_funcs; ct != NULL; ct = ct_next) {
643 struct device *fndev = ct->ct_device;
644 ct_next = ct->ct_next;
645
646 DPRINTF(("%s: detaching %s\n", sc->sc_dev.dv_xname, fndev->dv_xname));
647 /* call device detach function */
648
649 if (0 != config_detach(fndev, 0)) {
650 printf("%s: cannot detaching dev %s, function %d\n",
651 sc->sc_dev.dv_xname, fndev->dv_xname, ct->ct_func);
652 prev_next = &(ct->ct_next);
653 } else {
654 sc->sc_poweron_func &= ~(1 << ct->ct_func);
655 *prev_next = ct->ct_next;
656 free(ct, M_DEVBUF);
657 }
658 }
659
660 sc->sc_poweron_func = 0;
661 sc->sc_cf->cardbus_power(sc->sc_cc, CARDBUS_VCC_0V | CARDBUS_VPP_0V);
662 }
663
664
665
666
667 /*
668 * void *cardbus_intr_establish(cc, cf, irq, level, func, arg)
669 * Interrupt handler of pccard.
670 * args:
671 * cardbus_chipset_tag_t *cc
672 * int irq:
673 */
674 void *
675 cardbus_intr_establish(cc, cf, irq, level, func, arg)
676 cardbus_chipset_tag_t cc;
677 cardbus_function_tag_t cf;
678 cardbus_intr_handle_t irq;
679 int level;
680 int (*func) __P((void *));
681 void *arg;
682 {
683 DPRINTF(("- cardbus_intr_establish: irq %d\n", irq));
684
685 return (*cf->cardbus_intr_establish)(cc, irq, level, func, arg);
686 }
687
688
689
690 /*
691 * void cardbus_intr_disestablish(cc, cf, handler)
692 * Interrupt handler of pccard.
693 * args:
694 * cardbus_chipset_tag_t *cc
695 */
696 void
697 cardbus_intr_disestablish(cc, cf, handler)
698 cardbus_chipset_tag_t cc;
699 cardbus_function_tag_t cf;
700 void *handler;
701 {
702 DPRINTF(("- pccard_intr_disestablish\n"));
703
704 (*cf->cardbus_intr_disestablish)(cc, handler);
705 return;
706 }
707
708
709
710 /* XXX this should be merged with cardbus_function_{enable,disable},
711 but we don't have a ct when these functions are called */
712
713 static void
714 enable_function(sc, cdstatus, function)
715 struct cardbus_softc *sc;
716 int cdstatus;
717 int function;
718 {
719
720 if (sc->sc_poweron_func == 0) {
721 /* switch to 3V and/or wait for power to stabilize */
722 if (cdstatus & CARDBUS_3V_CARD) {
723 sc->sc_cf->cardbus_power(sc->sc_cc, CARDBUS_VCC_3V);
724 } else {
725 /* No cards other than 3.3V cards. */
726 return;
727 }
728 (sc->sc_cf->cardbus_ctrl)(sc->sc_cc, CARDBUS_RESET);
729 }
730 sc->sc_poweron_func |= (1 << function);
731 }
732
733 static void
734 disable_function(sc, function)
735 struct cardbus_softc *sc;
736 int function;
737 {
738
739 sc->sc_poweron_func &= ~(1 << function);
740 if (sc->sc_poweron_func == 0) {
741 /* power-off because no functions are enabled */
742 sc->sc_cf->cardbus_power(sc->sc_cc, CARDBUS_VCC_0V);
743 }
744 }
745
746 /*
747 * int cardbus_function_enable(struct cardbus_softc *sc, int func)
748 *
749 * This function enables a function on a card. When no power is
750 * applied on the card, power will be applied on it.
751 */
752 int
753 cardbus_function_enable(sc, func)
754 struct cardbus_softc *sc;
755 int func;
756 {
757 cardbus_chipset_tag_t cc = sc->sc_cc;
758 cardbus_function_tag_t cf = sc->sc_cf;
759 cardbusreg_t command;
760 cardbustag_t tag;
761
762 DPRINTF(("entering cardbus_function_enable... "));
763
764 /* entering critical area */
765
766 enable_function(sc, CARDBUS_3V_CARD, func); /* XXX: sc_vold should be used */
767
768 /* exiting critical area */
769
770 tag = cardbus_make_tag(cc, cf, sc->sc_bus, sc->sc_device, func);
771
772 command = cardbus_conf_read(cc, cf, tag, CARDBUS_COMMAND_STATUS_REG);
773 command |= (CARDBUS_COMMAND_MEM_ENABLE | CARDBUS_COMMAND_IO_ENABLE | CARDBUS_COMMAND_MASTER_ENABLE); /* XXX: good guess needed */
774
775 cardbus_conf_write(cc, cf, tag, CARDBUS_COMMAND_STATUS_REG, command);
776
777 cardbus_free_tag(cc, cf, tag);
778
779 DPRINTF(("%x\n", sc->sc_poweron_func));
780
781 return 0;
782 }
783
784
785 /*
786 * int cardbus_function_disable(struct cardbus_softc *, int func)
787 *
788 * This function disable a function on a card. When no functions are
789 * enabled, it turns off the power.
790 */
791 int
792 cardbus_function_disable(sc, func)
793 struct cardbus_softc *sc;
794 int func;
795 {
796
797 DPRINTF(("entering cardbus_function_disable... "));
798
799 disable_function(sc, func);
800
801 return 0;
802 }
803
804
805 /*
806 * int cardbus_get_capability(cardbus_chipset_tag_t cc,
807 * cardbus_function_tag_t cf, cardbustag_t tag, int capid, int *offset,
808 * cardbusreg_t *value)
809 *
810 * Find the specified PCI capability.
811 */
812 int
813 cardbus_get_capability(cc, cf, tag, capid, offset, value)
814 cardbus_chipset_tag_t cc;
815 cardbus_function_tag_t cf;
816 cardbustag_t tag;
817 int capid;
818 int *offset;
819 cardbusreg_t *value;
820 {
821 cardbusreg_t reg;
822 unsigned int ofs;
823
824 reg = cardbus_conf_read(cc, cf, tag, PCI_COMMAND_STATUS_REG);
825 if (!(reg & PCI_STATUS_CAPLIST_SUPPORT))
826 return (0);
827
828 ofs = PCI_CAPLIST_PTR(cardbus_conf_read(cc, cf, tag,
829 PCI_CAPLISTPTR_REG));
830 while (ofs != 0) {
831 #ifdef DIAGNOSTIC
832 if ((ofs & 3) || (ofs < 0x40))
833 panic("cardbus_get_capability");
834 #endif
835 reg = cardbus_conf_read(cc, cf, tag, ofs);
836 if (PCI_CAPLIST_CAP(reg) == capid) {
837 if (offset)
838 *offset = ofs;
839 if (value)
840 *value = reg;
841 return (1);
842 }
843 ofs = PCI_CAPLIST_NEXT(reg);
844 }
845
846 return (0);
847 }
848
849
850 /*
851 * below this line, there are some functions for decoding tuples.
852 * They should go out from this file.
853 */
854
855 static u_int8_t *
856 decode_tuple __P((u_int8_t *tuple, tuple_decode_func func, void *data));
857
858 static int
859 decode_tuples(tuple, buflen, func, data)
860 u_int8_t *tuple;
861 int buflen;
862 tuple_decode_func func;
863 void *data;
864 {
865 u_int8_t *tp = tuple;
866
867 if (PCMCIA_CISTPL_LINKTARGET != *tuple) {
868 DPRINTF(("WRONG TUPLE: 0x%x\n", *tuple));
869 return 0;
870 }
871
872 while (NULL != (tp = decode_tuple(tp, func, data))) {
873 if (tuple + buflen < tp) {
874 break;
875 }
876 }
877
878 return 1;
879 }
880
881
882 static u_int8_t *
883 decode_tuple(tuple, func, data)
884 u_int8_t *tuple;
885 tuple_decode_func func;
886 void *data;
887 {
888 u_int8_t type;
889 u_int8_t len;
890
891 type = tuple[0];
892 len = tuple[1] + 2;
893
894 (*func)(tuple, len, data);
895
896 if (PCMCIA_CISTPL_END == type) {
897 return NULL;
898 }
899
900 return tuple + len;
901 }
902
903
904 #ifdef CARDBUS_DEBUG
905 static char *tuple_name __P((int type));
906
907 static char *
908 tuple_name(type)
909 int type;
910 {
911 static char *tuple_name_s [] = {
912 "TPL_NULL", "TPL_DEVICE", "Reserved", "Reserved", /* 0-3 */
913 "CONFIG_CB", "CFTABLE_ENTRY_CB", "Reserved", "BAR", /* 4-7 */
914 "Reserved", "Reserved", "Reserved", "Reserved", /* 8-B */
915 "Reserved", "Reserved", "Reserved", "Reserved", /* C-F */
916 "CHECKSUM", "LONGLINK_A", "LONGLINK_C", "LINKTARGET", /* 10-13 */
917 "NO_LINK", "VERS_1", "ALTSTR", "DEVICE_A",
918 "JEDEC_C", "JEDEC_A", "CONFIG", "CFTABLE_ENTRY",
919 "DEVICE_OC", "DEVICE_OA", "DEVICE_GEO", "DEVICE_GEO_A",
920 "MANFID", "FUNCID", "FUNCE", "SWIL", /* 20-23 */
921 "Reserved", "Reserved", "Reserved", "Reserved", /* 24-27 */
922 "Reserved", "Reserved", "Reserved", "Reserved", /* 28-2B */
923 "Reserved", "Reserved", "Reserved", "Reserved", /* 2C-2F */
924 "Reserved", "Reserved", "Reserved", "Reserved", /* 30-33 */
925 "Reserved", "Reserved", "Reserved", "Reserved", /* 34-37 */
926 "Reserved", "Reserved", "Reserved", "Reserved", /* 38-3B */
927 "Reserved", "Reserved", "Reserved", "Reserved", /* 3C-3F */
928 "VERS_2", "FORMAT", "GEOMETRY", "BYTEORDER",
929 "DATE", "BATTERY", "ORG"
930 };
931 #define NAME_LEN(x) (sizeof x / sizeof(x[0]))
932
933 if (type > 0 && type < NAME_LEN(tuple_name_s)) {
934 return tuple_name_s[type];
935 } else if (0xff == type) {
936 return "END";
937 } else {
938 return "Reserved";
939 }
940 }
941
942 static void
943 print_tuple(tuple, len, data)
944 u_int8_t *tuple;
945 int len;
946 void *data;
947 {
948 int i;
949
950 printf("tuple: %s len %d\n", tuple_name(tuple[0]), len);
951
952 for (i = 0; i < len; ++i) {
953 if (i % 16 == 0) {
954 printf(" 0x%2x:", i);
955 }
956 printf(" %x",tuple[i]);
957 if (i % 16 == 15) {
958 printf("\n");
959 }
960 }
961 if (i % 16 != 0) {
962 printf("\n");
963 }
964 }
965
966 #endif
967