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