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