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