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