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