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