1 /* $NetBSD: cardbus.c,v 1.98 2009/12/15 22:17:12 snj Exp $ */
4 * Copyright (c) 1997, 1998, 1999 and 2000
5 * HAYAKAWA Koichi. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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.
29 #include <sys/cdefs.h>
30 __KERNEL_RCSID(0, "$NetBSD: cardbus.c,v 1.98 2009/12/15 22:17:12 snj Exp $");
32 #include "opt_cardbus.h"
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>
41 #include <sys/reboot.h> /* for AB_* needed by bootverbose */
45 #include <dev/cardbus/cardbusvar.h>
46 #include <dev/pci/pcidevs.h>
48 #include <dev/cardbus/cardbus_exrom.h>
50 #include <dev/pci/pcivar.h> /* XXX */
51 #include <dev/pci/pcireg.h> /* XXX */
53 #include <dev/pcmcia/pcmciareg.h>
57 #if defined CARDBUS_DEBUG
59 #define DPRINTF(a) printf a
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 *);
73 typedef void (*tuple_decode_func
)(u_int8_t
*, int, void*);
75 static int decode_tuples(u_int8_t
*, int, tuple_decode_func
, void*);
77 static void print_tuple(u_int8_t
*, int, void*);
80 static int cardbus_read_tuples(struct cardbus_attach_args
*,
81 cardbusreg_t
, u_int8_t
*, size_t);
83 static void enable_function(struct cardbus_softc
*, int, int);
84 static void disable_function(struct cardbus_softc
*, int);
86 static bool cardbus_child_register(device_t
);
88 CFATTACH_DECL3_NEW(cardbus
, sizeof(struct cardbus_softc
),
89 cardbusmatch
, cardbusattach
, cardbusdetach
, NULL
,
90 cardbus_rescan
, cardbus_childdetached
, DVF_DETACH_SHUTDOWN
);
92 #ifndef __NetBSD_Version__
93 struct cfdriver cardbus_cd
= {
94 NULL
, "cardbus", DV_DULL
100 cardbusmatch(device_t parent
, cfdata_t cf
, void *aux
)
107 cardbusattach(device_t parent
, device_t self
, void *aux
)
109 struct cardbus_softc
*sc
= device_private(self
);
110 struct cbslot_attach_args
*cba
= aux
;
114 sc
->sc_bus
= cba
->cba_bus
;
115 sc
->sc_intrline
= cba
->cba_intrline
;
116 sc
->sc_cacheline
= cba
->cba_cacheline
;
117 sc
->sc_max_lattimer
= MIN(0xf8, cba
->cba_max_lattimer
);
120 aprint_normal(": bus %d", sc
->sc_bus
);
122 aprint_normal(" cacheline 0x%x, lattimer 0x%x",
123 sc
->sc_cacheline
, sc
->sc_max_lattimer
);
126 sc
->sc_iot
= cba
->cba_iot
; /* CardBus I/O space tag */
127 sc
->sc_memt
= cba
->cba_memt
; /* CardBus MEM space tag */
128 sc
->sc_dmat
= cba
->cba_dmat
; /* DMA tag */
129 sc
->sc_cc
= cba
->cba_cc
;
130 sc
->sc_cf
= cba
->cba_cf
;
133 sc
->sc_rbus_iot
= cba
->cba_rbus_iot
;
134 sc
->sc_rbus_memt
= cba
->cba_rbus_memt
;
137 if (!pmf_device_register(self
, NULL
, NULL
))
138 aprint_error_dev(self
, "couldn't establish power handler\n");
142 cardbusdetach(device_t self
, int flags
)
146 if ((rc
= config_detach_children(self
, flags
)) != 0)
149 pmf_device_deregister(self
);
154 cardbus_read_tuples(struct cardbus_attach_args
*ca
, cardbusreg_t cis_ptr
,
155 u_int8_t
*tuples
, size_t len
)
157 struct cardbus_softc
*sc
= ca
->ca_ct
->ct_sc
;
158 cardbus_chipset_tag_t cc
= ca
->ca_ct
->ct_cc
;
159 cardbus_function_tag_t cf
= ca
->ca_ct
->ct_cf
;
160 cardbustag_t tag
= ca
->ca_tag
;
161 cardbusreg_t command
;
162 bus_space_tag_t bar_tag
;
163 bus_space_handle_t bar_memh
;
168 int cardbus_space
= cis_ptr
& CARDBUS_CIS_ASIMASK
;
171 memset(tuples
, 0, len
);
173 cis_ptr
= cis_ptr
& CARDBUS_CIS_ADDRMASK
;
175 switch (cardbus_space
) {
176 case CARDBUS_CIS_ASI_TUPLE
:
177 DPRINTF(("%s: reading CIS data from configuration space\n",
178 device_xname(sc
->sc_dev
)));
179 for (i
= cis_ptr
, j
= 0; i
< 0xff; i
+= 4) {
180 u_int32_t e
= (*cf
->cardbus_conf_read
)(cc
, tag
, i
);
181 tuples
[j
] = 0xff & e
;
183 tuples
[j
+ 1] = 0xff & e
;
185 tuples
[j
+ 2] = 0xff & e
;
187 tuples
[j
+ 3] = 0xff & e
;
193 case CARDBUS_CIS_ASI_BAR0
:
194 case CARDBUS_CIS_ASI_BAR1
:
195 case CARDBUS_CIS_ASI_BAR2
:
196 case CARDBUS_CIS_ASI_BAR3
:
197 case CARDBUS_CIS_ASI_BAR4
:
198 case CARDBUS_CIS_ASI_BAR5
:
199 case CARDBUS_CIS_ASI_ROM
:
200 if (cardbus_space
== CARDBUS_CIS_ASI_ROM
) {
201 reg
= CARDBUS_ROM_REG
;
202 DPRINTF(("%s: reading CIS data from ROM\n",
203 device_xname(sc
->sc_dev
)));
205 reg
= CARDBUS_CIS_ASI_BAR(cardbus_space
);
206 DPRINTF(("%s: reading CIS data from BAR%d\n",
207 device_xname(sc
->sc_dev
), cardbus_space
- 1));
211 * XXX zero register so mapreg_map doesn't get confused by old
214 cardbus_conf_write(cc
, cf
, tag
, reg
, 0);
215 if (Cardbus_mapreg_map(ca
->ca_ct
, reg
,
216 CARDBUS_MAPREG_TYPE_MEM
| PCI_MAPREG_MEM_TYPE_32BIT
,
217 0, &bar_tag
, &bar_memh
, &bar_addr
, &bar_size
)) {
218 aprint_error_dev(sc
->sc_dev
, "failed to map memory\n");
221 aprint_debug_dev(sc
->sc_dev
, "mapped %ju bytes at 0x%jx\n",
222 (uintmax_t)bar_size
, (uintmax_t)bar_addr
);
224 if (cardbus_space
== CARDBUS_CIS_ASI_ROM
) {
227 struct cardbus_rom_image_head rom_image
;
228 struct cardbus_rom_image
*p
;
231 /* enable rom address decoder */
232 exrom
= cardbus_conf_read(cc
, cf
, tag
, reg
);
233 cardbus_conf_write(cc
, cf
, tag
, reg
, exrom
| 1);
235 command
= cardbus_conf_read(cc
, cf
, tag
,
236 CARDBUS_COMMAND_STATUS_REG
);
237 cardbus_conf_write(cc
, cf
, tag
,
238 CARDBUS_COMMAND_STATUS_REG
,
239 command
| CARDBUS_COMMAND_MEM_ENABLE
);
241 if (cardbus_read_exrom(bar_tag
, bar_memh
, &rom_image
))
244 SIMPLEQ_FOREACH(p
, &rom_image
, next
) {
246 CARDBUS_CIS_ASI_ROM_IMAGE(cis_ptr
)) {
247 bus_space_read_region_1(p
->romt
,
248 p
->romh
, CARDBUS_CIS_ADDR(cis_ptr
),
249 tuples
, MIN(p
->image_size
, len
));
254 while ((p
= SIMPLEQ_FIRST(&rom_image
)) != NULL
) {
255 SIMPLEQ_REMOVE_HEAD(&rom_image
, next
);
259 exrom
= cardbus_conf_read(cc
, cf
, tag
, reg
);
260 cardbus_conf_write(cc
, cf
, tag
, reg
, exrom
& ~1);
263 command
= cardbus_conf_read(cc
, cf
, tag
,
264 CARDBUS_COMMAND_STATUS_REG
);
265 cardbus_conf_write(cc
, cf
, tag
,
266 CARDBUS_COMMAND_STATUS_REG
,
267 command
| CARDBUS_COMMAND_MEM_ENABLE
);
268 /* XXX byte order? */
269 bus_space_read_region_1(bar_tag
, bar_memh
,
271 MIN(bar_size
- MIN(bar_size
, cis_ptr
), len
));
274 command
= cardbus_conf_read(cc
, cf
, tag
,
275 CARDBUS_COMMAND_STATUS_REG
);
276 cardbus_conf_write(cc
, cf
, tag
, CARDBUS_COMMAND_STATUS_REG
,
277 command
& ~CARDBUS_COMMAND_MEM_ENABLE
);
278 cardbus_conf_write(cc
, cf
, tag
, reg
, 0);
280 Cardbus_mapreg_unmap(ca
->ca_ct
, reg
, bar_tag
, bar_memh
,
286 panic("%s: bad CIS space (%d)", device_xname(sc
->sc_dev
),
294 parse_tuple(u_int8_t
*tuple
, int len
, void *data
)
296 struct cardbus_cis_info
*cis
= data
;
301 case PCMCIA_CISTPL_MANFID
:
303 DPRINTF(("%s: wrong length manufacturer id (%d)\n",
304 __func__
, tuple
[1]));
307 cis
->manufacturer
= tuple
[2] | (tuple
[3] << 8);
308 cis
->product
= tuple
[4] | (tuple
[5] << 8);
311 case PCMCIA_CISTPL_VERS_1
:
312 memcpy(cis
->cis1_info_buf
, tuple
+ 2, tuple
[1]);
314 p
= cis
->cis1_info_buf
+ 2;
316 sizeof(cis
->cis1_info
) / sizeof(cis
->cis1_info
[0])) {
317 if (p
>= cis
->cis1_info_buf
+ tuple
[1] || *p
== '\xff')
319 cis
->cis1_info
[i
++] = p
;
320 while (*p
!= '\0' && *p
!= '\xff')
327 case PCMCIA_CISTPL_BAR
:
329 DPRINTF(("%s: BAR with short length (%d)\n",
330 __func__
, tuple
[1]));
333 bar_index
= tuple
[2] & 7;
334 if (bar_index
== 0) {
335 DPRINTF(("%s: invalid ASI in BAR tuple\n", __func__
));
339 cis
->bar
[bar_index
].flags
= tuple
[2];
340 cis
->bar
[bar_index
].size
=
347 case PCMCIA_CISTPL_FUNCID
:
348 cis
->funcid
= tuple
[2];
351 case PCMCIA_CISTPL_FUNCE
:
352 switch (cis
->funcid
) {
353 case PCMCIA_FUNCTION_SERIAL
:
355 /* XXX PCMCIA_TPLFE_TYPE_SERIAL_??? */
357 cis
->funce
.serial
.uart_type
= tuple
[3] & 0x1f;
358 cis
->funce
.serial
.uart_present
= 1;
362 case PCMCIA_FUNCTION_NETWORK
:
364 tuple
[2] == PCMCIA_TPLFE_TYPE_LAN_NID
) {
366 sizeof(cis
->funce
.network
.netid
)) {
367 DPRINTF(("%s: unknown network id type "
369 __func__
, tuple
[3]));
371 cis
->funce
.network
.netid_present
= 1;
372 memcpy(cis
->funce
.network
.netid
,
373 tuple
+ 4, tuple
[3]);
383 * int cardbus_attach_card(struct cardbus_softc *sc)
385 * This function attaches the card on the slot: turns on power,
386 * reads and analyses tuple, sets configuration index.
388 * This function returns the number of recognised device functions.
389 * If no functions are recognised, return 0.
392 cardbus_attach_card(struct cardbus_softc
*sc
)
394 cardbus_chipset_tag_t cc
;
395 cardbus_function_tag_t cf
;
397 static int wildcard
[CARDBUSCF_NLOCS
] = {
398 CARDBUSCF_FUNCTION_DEFAULT
404 DPRINTF(("cardbus_attach_card: cb%d start\n",
405 device_unit(sc
->sc_dev
)));
407 /* inspect initial voltage */
408 if ((cdstatus
= (*cf
->cardbus_ctrl
)(cc
, CARDBUS_CD
)) == 0) {
409 DPRINTF(("%s: no CardBus card on cb%d\n", __func__
,
410 device_unit(sc
->sc_dev
)));
414 device_pmf_driver_set_child_register(sc
->sc_dev
, cardbus_child_register
);
415 cardbus_rescan(sc
->sc_dev
, "cardbus", wildcard
);
416 return (1); /* XXX */
420 cardbus_rescan(device_t self
, const char *ifattr
,
423 struct cardbus_softc
*sc
= device_private(self
);
424 cardbus_chipset_tag_t cc
;
425 cardbus_function_tag_t cf
;
427 cardbusreg_t id
, class, cis_ptr
;
428 cardbusreg_t bhlc
, icr
, lattimer
;
430 int function
, nfunction
;
432 cardbus_devfunc_t ct
;
437 /* inspect initial voltage */
438 if ((cdstatus
= (*cf
->cardbus_ctrl
)(cc
, CARDBUS_CD
)) == 0) {
439 DPRINTF(("%s: no CardBus card on cb%d\n", __func__
,
440 device_unit(sc
->sc_dev
)));
445 * XXX use fake function 8 to keep power on during whole
448 enable_function(sc
, cdstatus
, 8);
451 tag
= cardbus_make_tag(cc
, cf
, sc
->sc_bus
, function
);
454 * Wait until power comes up. Maxmum 500 ms.
456 * XXX What is this for? The bridge driver ought to have waited
462 for (i
= 0; i
< 5; ++i
) {
463 id
= cardbus_conf_read(cc
, cf
, tag
, CARDBUS_ID_REG
);
464 if (id
!= 0xffffffff && id
!= 0) {
467 if (cold
) { /* before kernel thread invoked */
469 } else { /* thread context */
470 if (tsleep((void *)sc
, PCATCH
, "cardbus",
471 hz
/ 10) != EWOULDBLOCK
) {
476 aprint_debug_dev(self
, "id reg valid in %d iterations\n", i
);
482 bhlc
= cardbus_conf_read(cc
, cf
, tag
, CARDBUS_BHLC_REG
);
483 DPRINTF(("%s bhlc 0x%08x -> ", device_xname(sc
->sc_dev
), bhlc
));
484 nfunction
= CARDBUS_HDRTYPE_MULTIFN(bhlc
) ? 8 : 1;
486 for (function
= 0; function
< nfunction
; function
++) {
487 struct cardbus_attach_args ca
;
488 int locs
[CARDBUSCF_NLOCS
];
490 if (locators
[CARDBUSCF_FUNCTION
] !=
491 CARDBUSCF_FUNCTION_DEFAULT
&&
492 locators
[CARDBUSCF_FUNCTION
] != function
)
495 if (sc
->sc_funcs
[function
])
498 tag
= cardbus_make_tag(cc
, cf
, sc
->sc_bus
, function
);
500 id
= cardbus_conf_read(cc
, cf
, tag
, CARDBUS_ID_REG
);
501 class = cardbus_conf_read(cc
, cf
, tag
, CARDBUS_CLASS_REG
);
502 cis_ptr
= cardbus_conf_read(cc
, cf
, tag
, CARDBUS_CIS_REG
);
504 /* Invalid vendor ID value? */
505 if (CARDBUS_VENDOR(id
) == PCI_VENDOR_INVALID
) {
509 DPRINTF(("cardbus_attach_card: "
510 "Vendor 0x%x, Product 0x%x, CIS 0x%x\n",
511 CARDBUS_VENDOR(id
), CARDBUS_PRODUCT(id
), cis_ptr
));
513 enable_function(sc
, cdstatus
, function
);
515 /* clean up every BAR */
516 cardbus_conf_write(cc
, cf
, tag
, CARDBUS_BASE0_REG
, 0);
517 cardbus_conf_write(cc
, cf
, tag
, CARDBUS_BASE1_REG
, 0);
518 cardbus_conf_write(cc
, cf
, tag
, CARDBUS_BASE2_REG
, 0);
519 cardbus_conf_write(cc
, cf
, tag
, CARDBUS_BASE3_REG
, 0);
520 cardbus_conf_write(cc
, cf
, tag
, CARDBUS_BASE4_REG
, 0);
521 cardbus_conf_write(cc
, cf
, tag
, CARDBUS_BASE5_REG
, 0);
522 cardbus_conf_write(cc
, cf
, tag
, CARDBUS_ROM_REG
, 0);
524 /* set initial latency and cacheline size */
525 bhlc
= cardbus_conf_read(cc
, cf
, tag
, CARDBUS_BHLC_REG
);
526 icr
= cardbus_conf_read(cc
, cf
, tag
, CARDBUS_INTERRUPT_REG
);
527 DPRINTF(("%s func%d icr 0x%08x bhlc 0x%08x -> ",
528 device_xname(sc
->sc_dev
), function
, icr
, bhlc
));
529 bhlc
&= ~(CARDBUS_CACHELINE_MASK
<< CARDBUS_CACHELINE_SHIFT
);
530 bhlc
|= (sc
->sc_cacheline
& CARDBUS_CACHELINE_MASK
) <<
531 CARDBUS_CACHELINE_SHIFT
;
533 * Set the initial value of the Latency Timer.
535 * While a PCI device owns the bus, its Latency
536 * Timer counts down bus cycles from its initial
537 * value to 0. Minimum Grant tells for how long
538 * the device wants to own the bus once it gets
539 * access, in units of 250ns.
541 * On a 33 MHz bus, there are 8 cycles per 250ns.
542 * So I multiply the Minimum Grant by 8 to find
543 * out the initial value of the Latency Timer.
545 * Avoid setting a Latency Timer less than 0x10,
546 * since the old code did not do that.
549 MIN(sc
->sc_max_lattimer
, MAX(0x10, 8 * PCI_MIN_GNT(icr
)));
550 if (PCI_LATTIMER(bhlc
) < lattimer
) {
551 bhlc
&= ~(PCI_LATTIMER_MASK
<< PCI_LATTIMER_SHIFT
);
552 bhlc
|= (lattimer
<< PCI_LATTIMER_SHIFT
);
555 cardbus_conf_write(cc
, cf
, tag
, CARDBUS_BHLC_REG
, bhlc
);
556 bhlc
= cardbus_conf_read(cc
, cf
, tag
, CARDBUS_BHLC_REG
);
557 DPRINTF(("0x%08x\n", bhlc
));
560 * We need to allocate the ct here, since we might
561 * need it when reading the CIS
563 if ((ct
= malloc(sizeof(struct cardbus_devfunc
),
564 M_DEVBUF
, M_NOWAIT
)) == NULL
) {
565 panic("no room for cardbus_tag");
569 ct
->ct_cc
= sc
->sc_cc
;
570 ct
->ct_cf
= sc
->sc_cf
;
571 ct
->ct_bus
= sc
->sc_bus
;
572 ct
->ct_func
= function
;
574 sc
->sc_funcs
[function
] = ct
;
576 memset(&ca
, 0, sizeof(ca
));
580 ca
.ca_iot
= sc
->sc_iot
;
581 ca
.ca_memt
= sc
->sc_memt
;
582 ca
.ca_dmat
= sc
->sc_dmat
;
585 ca
.ca_rbus_iot
= sc
->sc_rbus_iot
;
586 ca
.ca_rbus_memt
= sc
->sc_rbus_memt
;
590 ca
.ca_bus
= sc
->sc_bus
;
591 ca
.ca_function
= function
;
595 ca
.ca_intrline
= sc
->sc_intrline
;
598 #define TUPLESIZE 2048
599 u_int8_t
*tuple
= malloc(TUPLESIZE
, M_DEVBUF
, M_WAITOK
);
600 if (cardbus_read_tuples(&ca
, cis_ptr
,
602 printf("cardbus_attach_card: "
603 "failed to read CIS\n");
606 decode_tuples(tuple
, TUPLESIZE
,
609 decode_tuples(tuple
, TUPLESIZE
,
610 parse_tuple
, &ca
.ca_cis
);
612 free(tuple
, M_DEVBUF
);
615 locs
[CARDBUSCF_FUNCTION
] = function
;
617 if ((csc
= config_found_sm_loc(sc
->sc_dev
, "cardbus", locs
,
618 &ca
, cardbusprint
, config_stdsubmatch
)) == NULL
) {
620 disable_function(sc
, function
);
621 sc
->sc_funcs
[function
] = NULL
;
629 * XXX power down pseudo function 8 (this will power down the card
630 * if no functions were attached).
632 disable_function(sc
, 8);
638 cardbusprint(void *aux
, const char *pnp
)
640 struct cardbus_attach_args
*ca
= aux
;
645 pci_devinfo(ca
->ca_id
, ca
->ca_class
, 1, devinfo
,
647 for (i
= 0; i
< 4; i
++) {
648 if (ca
->ca_cis
.cis1_info
[i
] == NULL
)
652 aprint_normal("%s", ca
->ca_cis
.cis1_info
[i
]);
654 aprint_verbose("%s(manufacturer 0x%x, product 0x%x)",
656 ca
->ca_cis
.manufacturer
, ca
->ca_cis
.product
);
657 aprint_normal(" %s at %s", devinfo
, pnp
);
659 aprint_normal(" function %d", ca
->ca_function
);
665 * void cardbus_detach_card(struct cardbus_softc *sc)
667 * This function detaches the card on the slot: detach device data
668 * structure and turns off the power.
670 * This function must not be called under interrupt context.
673 cardbus_detach_card(struct cardbus_softc
*sc
)
676 struct cardbus_devfunc
*ct
;
678 for (f
= 0; f
< 8; f
++) {
679 ct
= sc
->sc_funcs
[f
];
683 DPRINTF(("%s: detaching %s\n", device_xname(sc
->sc_dev
),
684 device_xname(ct
->ct_device
)));
685 /* call device detach function */
687 if (config_detach(ct
->ct_device
, 0) != 0) {
688 aprint_error_dev(sc
->sc_dev
,
689 "cannot detach dev %s, function %d\n",
690 device_xname(ct
->ct_device
), ct
->ct_func
);
694 sc
->sc_poweron_func
= 0;
695 (*sc
->sc_cf
->cardbus_power
)(sc
->sc_cc
,
696 CARDBUS_VCC_0V
| CARDBUS_VPP_0V
);
700 cardbus_childdetached(device_t self
, device_t child
)
702 struct cardbus_softc
*sc
= device_private(self
);
703 struct cardbus_devfunc
*ct
;
705 ct
= sc
->sc_funcs
[device_locator(child
, CARDBUSCF_FUNCTION
)];
706 KASSERT(ct
->ct_device
== child
);
708 sc
->sc_poweron_func
&= ~(1 << ct
->ct_func
);
709 sc
->sc_funcs
[ct
->ct_func
] = NULL
;
714 * void *cardbus_intr_establish(cc, cf, irq, level, func, arg)
715 * Interrupt handler of pccard.
717 * cardbus_chipset_tag_t *cc
721 cardbus_intr_establish(cardbus_chipset_tag_t cc
, cardbus_function_tag_t cf
,
722 cardbus_intr_line_t irq
, int level
, int (*func
)(void *), void *arg
)
725 DPRINTF(("- cardbus_intr_establish: irq %d\n", irq
));
726 return ((*cf
->cardbus_intr_establish
)(cc
, irq
, level
, func
, arg
));
730 * void cardbus_intr_disestablish(cc, cf, handler)
731 * Interrupt handler of pccard.
733 * cardbus_chipset_tag_t *cc
736 cardbus_intr_disestablish(cardbus_chipset_tag_t cc
, cardbus_function_tag_t cf
,
740 DPRINTF(("- pccard_intr_disestablish\n"));
741 (*cf
->cardbus_intr_disestablish
)(cc
, handler
);
745 * XXX this should be merged with cardbus_function_{enable,disable},
746 * but we don't have a ct when these functions are called.
749 enable_function(struct cardbus_softc
*sc
, int cdstatus
, int function
)
752 if (sc
->sc_poweron_func
== 0) {
753 /* switch to 3V and/or wait for power to stabilize */
754 if (cdstatus
& CARDBUS_3V_CARD
) {
756 * sc_poweron_func must be substituted before
757 * entering sleep, in order to avoid turn on
760 sc
->sc_poweron_func
|= (1 << function
);
761 (*sc
->sc_cf
->cardbus_power
)(sc
->sc_cc
, CARDBUS_VCC_3V
);
763 /* No cards other than 3.3V cards. */
766 (*sc
->sc_cf
->cardbus_ctrl
)(sc
->sc_cc
, CARDBUS_RESET
);
768 sc
->sc_poweron_func
|= (1 << function
);
772 disable_function(struct cardbus_softc
*sc
, int function
)
775 cardbus_devfunc_t ct
;
779 sc
->sc_poweron_func
&= ~(1 << function
);
780 if (sc
->sc_poweron_func
!= 0)
782 for (i
= 0; i
< __arraycount(sc
->sc_funcs
); i
++) {
783 if ((ct
= sc
->sc_funcs
[i
]) == NULL
)
786 if (prop_dictionary_get_bool(device_properties(dv
),
787 "pmf-powerdown", &powerdown
) && !powerdown
)
790 /* power-off because no functions are enabled */
791 (*sc
->sc_cf
->cardbus_power
)(sc
->sc_cc
, CARDBUS_VCC_0V
);
795 * int cardbus_function_enable(struct cardbus_softc *sc, int func)
797 * This function enables a function on a card. When no power is
798 * applied on the card, power will be applied on it.
801 cardbus_function_enable(struct cardbus_softc
*sc
, int func
)
803 cardbus_chipset_tag_t cc
= sc
->sc_cc
;
804 cardbus_function_tag_t cf
= sc
->sc_cf
;
805 cardbus_devfunc_t ct
;
806 cardbusreg_t command
;
809 DPRINTF(("entering cardbus_function_enable... "));
811 /* entering critical area */
813 /* XXX: sc_vold should be used */
814 enable_function(sc
, CARDBUS_3V_CARD
, func
);
816 /* exiting critical area */
818 tag
= cardbus_make_tag(cc
, cf
, sc
->sc_bus
, func
);
820 command
= cardbus_conf_read(cc
, cf
, tag
, CARDBUS_COMMAND_STATUS_REG
);
821 command
|= (CARDBUS_COMMAND_MEM_ENABLE
| CARDBUS_COMMAND_IO_ENABLE
|
822 CARDBUS_COMMAND_MASTER_ENABLE
); /* XXX: good guess needed */
824 cardbus_conf_write(cc
, cf
, tag
, CARDBUS_COMMAND_STATUS_REG
, command
);
826 if ((ct
= sc
->sc_funcs
[func
]) != NULL
)
827 Cardbus_conf_write(ct
, tag
, CARDBUS_BHLC_REG
, ct
->ct_bhlc
);
829 cardbus_free_tag(cc
, cf
, tag
);
831 DPRINTF(("%x\n", sc
->sc_poweron_func
));
837 * int cardbus_function_disable(struct cardbus_softc *, int func)
839 * This function disable a function on a card. When no functions are
840 * enabled, it turns off the power.
843 cardbus_function_disable(struct cardbus_softc
*sc
, int func
)
846 DPRINTF(("entering cardbus_function_disable... "));
848 disable_function(sc
, func
);
854 * int cardbus_get_capability(cardbus_chipset_tag_t cc,
855 * cardbus_function_tag_t cf, cardbustag_t tag, int capid, int *offset,
856 * cardbusreg_t *value)
858 * Find the specified PCI capability.
861 cardbus_get_capability(cardbus_chipset_tag_t cc
, cardbus_function_tag_t cf
,
862 cardbustag_t tag
, int capid
, int *offset
, cardbusreg_t
*value
)
867 reg
= cardbus_conf_read(cc
, cf
, tag
, PCI_COMMAND_STATUS_REG
);
868 if (!(reg
& PCI_STATUS_CAPLIST_SUPPORT
))
871 ofs
= PCI_CAPLIST_PTR(cardbus_conf_read(cc
, cf
, tag
,
872 PCI_CAPLISTPTR_REG
));
875 if ((ofs
& 3) || (ofs
< 0x40))
876 panic("cardbus_get_capability");
878 reg
= cardbus_conf_read(cc
, cf
, tag
, ofs
);
879 if (PCI_CAPLIST_CAP(reg
) == capid
) {
886 ofs
= PCI_CAPLIST_NEXT(reg
);
893 * below this line, there are some functions for decoding tuples.
894 * They should go out from this file.
898 decode_tuple(u_int8_t
*, u_int8_t
*, tuple_decode_func
, void *);
901 decode_tuples(u_int8_t
*tuple
, int buflen
, tuple_decode_func func
, void *data
)
903 u_int8_t
*tp
= tuple
;
905 if (PCMCIA_CISTPL_LINKTARGET
!= *tuple
) {
906 DPRINTF(("WRONG TUPLE: 0x%x\n", *tuple
));
910 while ((tp
= decode_tuple(tp
, tuple
+ buflen
, func
, data
)) != NULL
)
917 decode_tuple(u_int8_t
*tuple
, u_int8_t
*end
,
918 tuple_decode_func func
, void *data
)
925 case PCMCIA_CISTPL_NULL
:
926 case PCMCIA_CISTPL_END
:
936 if (tuple
+ len
> end
)
939 (*func
)(tuple
, len
, data
);
941 if (type
== PCMCIA_CISTPL_END
|| tuple
+ len
== end
)
944 return (tuple
+ len
);
948 * XXX: this is another reason why this code should be shared with PCI.
951 cardbus_get_powerstate_int(cardbus_devfunc_t ct
, cardbustag_t tag
,
952 cardbusreg_t
*state
, int offset
)
954 cardbusreg_t value
, now
;
955 cardbus_chipset_tag_t cc
= ct
->ct_cc
;
956 cardbus_function_tag_t cf
= ct
->ct_cf
;
958 value
= cardbus_conf_read(cc
, cf
, tag
, offset
+ PCI_PMCSR
);
959 now
= value
& PCI_PMCSR_STATE_MASK
;
961 case PCI_PMCSR_STATE_D0
:
962 case PCI_PMCSR_STATE_D1
:
963 case PCI_PMCSR_STATE_D2
:
964 case PCI_PMCSR_STATE_D3
:
973 cardbus_get_powerstate(cardbus_devfunc_t ct
, cardbustag_t tag
,
976 cardbus_chipset_tag_t cc
= ct
->ct_cc
;
977 cardbus_function_tag_t cf
= ct
->ct_cf
;
981 if (!cardbus_get_capability(cc
, cf
, tag
, PCI_CAP_PWRMGMT
, &offset
, &value
))
984 return cardbus_get_powerstate_int(ct
, tag
, state
, offset
);
988 cardbus_set_powerstate_int(cardbus_devfunc_t ct
, cardbustag_t tag
,
989 cardbusreg_t state
, int offset
, cardbusreg_t cap_reg
)
991 cardbus_chipset_tag_t cc
= ct
->ct_cc
;
992 cardbus_function_tag_t cf
= ct
->ct_cf
;
994 cardbusreg_t value
, cap
, now
;
996 KASSERT((offset
& 0x3) == 0);
998 cap
= cap_reg
>> PCI_PMCR_SHIFT
;
999 value
= cardbus_conf_read(cc
, cf
, tag
, offset
+ PCI_PMCSR
);
1000 now
= value
& PCI_PMCSR_STATE_MASK
;
1001 value
&= ~PCI_PMCSR_STATE_MASK
;
1006 case PCI_PMCSR_STATE_D0
:
1008 case PCI_PMCSR_STATE_D1
:
1009 if (now
== PCI_PMCSR_STATE_D2
|| now
== PCI_PMCSR_STATE_D3
) {
1010 printf("invalid transition from %d to D1\n", (int)now
);
1013 if (!(cap
& PCI_PMCR_D1SUPP
)) {
1014 printf("D1 not supported\n");
1018 case PCI_PMCSR_STATE_D2
:
1019 if (now
== PCI_PMCSR_STATE_D3
) {
1020 printf("invalid transition from %d to D2\n", (int)now
);
1023 if (!(cap
& PCI_PMCR_D2SUPP
)) {
1024 printf("D2 not supported\n");
1028 case PCI_PMCSR_STATE_D3
:
1034 cardbus_conf_write(cc
, cf
, tag
, offset
+ PCI_PMCSR
, value
);
1035 if (state
== PCI_PMCSR_STATE_D3
|| now
== PCI_PMCSR_STATE_D3
)
1037 else if (state
== PCI_PMCSR_STATE_D2
|| now
== PCI_PMCSR_STATE_D2
)
1044 cardbus_set_powerstate(cardbus_devfunc_t ct
, cardbustag_t tag
, cardbusreg_t state
)
1046 cardbus_chipset_tag_t cc
= ct
->ct_cc
;
1047 cardbus_function_tag_t cf
= ct
->ct_cf
;
1051 if (!cardbus_get_capability(cc
, cf
, tag
, PCI_CAP_PWRMGMT
, &offset
,
1055 return cardbus_set_powerstate_int(ct
, tag
, state
, offset
, value
);
1058 #ifdef CARDBUS_DEBUG
1059 static const char *tuple_name(int);
1060 static const char *tuple_names
[] = {
1061 "TPL_NULL", "TPL_DEVICE", "Reserved", "Reserved", /* 0-3 */
1062 "CONFIG_CB", "CFTABLE_ENTRY_CB", "Reserved", "BAR", /* 4-7 */
1063 "Reserved", "Reserved", "Reserved", "Reserved", /* 8-B */
1064 "Reserved", "Reserved", "Reserved", "Reserved", /* C-F */
1065 "CHECKSUM", "LONGLINK_A", "LONGLINK_C", "LINKTARGET", /* 10-13 */
1066 "NO_LINK", "VERS_1", "ALTSTR", "DEVICE_A",
1067 "JEDEC_C", "JEDEC_A", "CONFIG", "CFTABLE_ENTRY",
1068 "DEVICE_OC", "DEVICE_OA", "DEVICE_GEO", "DEVICE_GEO_A",
1069 "MANFID", "FUNCID", "FUNCE", "SWIL", /* 20-23 */
1070 "Reserved", "Reserved", "Reserved", "Reserved", /* 24-27 */
1071 "Reserved", "Reserved", "Reserved", "Reserved", /* 28-2B */
1072 "Reserved", "Reserved", "Reserved", "Reserved", /* 2C-2F */
1073 "Reserved", "Reserved", "Reserved", "Reserved", /* 30-33 */
1074 "Reserved", "Reserved", "Reserved", "Reserved", /* 34-37 */
1075 "Reserved", "Reserved", "Reserved", "Reserved", /* 38-3B */
1076 "Reserved", "Reserved", "Reserved", "Reserved", /* 3C-3F */
1077 "VERS_2", "FORMAT", "GEOMETRY", "BYTEORDER",
1078 "DATE", "BATTERY", "ORG"
1080 #define NAME_LEN(x) (sizeof x / sizeof(x[0]))
1083 tuple_name(int type
)
1086 if (0 <= type
&& type
< NAME_LEN(tuple_names
)) {
1087 return (tuple_names
[type
]);
1088 } else if (type
== 0xff) {
1091 return ("Reserved");
1096 print_tuple(u_int8_t
*tuple
, int len
, void *data
)
1100 printf("tuple: %s len %d\n", tuple_name(tuple
[0]), len
);
1102 for (i
= 0; i
< len
; ++i
) {
1104 printf(" 0x%2x:", i
);
1106 printf(" %x", tuple
[i
]);
1118 cardbus_conf_capture(cardbus_chipset_tag_t cc
, cardbus_function_tag_t cf
,
1119 cardbustag_t tag
, struct cardbus_conf_state
*pcs
)
1123 for (off
= 0; off
< 16; off
++)
1124 pcs
->reg
[off
] = cardbus_conf_read(cc
, cf
, tag
, (off
* 4));
1128 cardbus_conf_restore(cardbus_chipset_tag_t cc
, cardbus_function_tag_t cf
,
1129 cardbustag_t tag
, struct cardbus_conf_state
*pcs
)
1134 for (off
= 15; off
>= 0; off
--) {
1135 val
= cardbus_conf_read(cc
, cf
, tag
, (off
* 4));
1136 if (val
!= pcs
->reg
[off
])
1137 cardbus_conf_write(cc
, cf
,tag
, (off
* 4), pcs
->reg
[off
]);
1142 cardbus_disable_retry(cardbus_chipset_tag_t cc
, cardbus_function_tag_t cf
,
1145 /* See comment on sys/dev/pci/pci.c:pci_disable_retry() for
1146 * the reason I comment-out this code.
1151 retry
= cardbus_conf_read(cc
, cf
, tag
, PCI_RETRY_TIMEOUT_REG
);
1152 retry
&= ~PCI_RETRY_TIMEOUT_REG_MASK
;
1153 cardbus_conf_write(cc
, cf
, tag
, PCI_RETRY_TIMEOUT_REG
, retry
);
1157 struct cardbus_child_power
{
1158 struct cardbus_conf_state p_cardbusconf
;
1159 cardbus_devfunc_t p_ct
;
1161 cardbus_chipset_tag_t p_cc
;
1162 cardbus_function_tag_t p_cf
;
1163 cardbusreg_t p_pm_cap
;
1169 cardbus_child_suspend(device_t dv
, pmf_qual_t qual
)
1171 struct cardbus_child_power
*priv
= device_pmf_bus_private(dv
);
1173 cardbus_conf_capture(priv
->p_cc
, priv
->p_cf
, priv
->p_tag
,
1174 &priv
->p_cardbusconf
);
1176 if (priv
->p_has_pm
&&
1177 cardbus_set_powerstate_int(priv
->p_ct
, priv
->p_tag
,
1178 PCI_PMCSR_STATE_D3
, priv
->p_pm_offset
, priv
->p_pm_cap
)) {
1179 aprint_error_dev(dv
, "unsupported state, continuing.\n");
1183 Cardbus_function_disable(priv
->p_ct
);
1189 cardbus_child_resume(device_t dv
, pmf_qual_t qual
)
1191 struct cardbus_child_power
*priv
= device_pmf_bus_private(dv
);
1193 Cardbus_function_enable(priv
->p_ct
);
1195 if (priv
->p_has_pm
&&
1196 cardbus_set_powerstate_int(priv
->p_ct
, priv
->p_tag
,
1197 PCI_PMCSR_STATE_D0
, priv
->p_pm_offset
, priv
->p_pm_cap
)) {
1198 aprint_error_dev(dv
, "unsupported state, continuing.\n");
1202 cardbus_conf_restore(priv
->p_cc
, priv
->p_cf
, priv
->p_tag
,
1203 &priv
->p_cardbusconf
);
1209 cardbus_child_deregister(device_t dv
)
1211 struct cardbus_child_power
*priv
= device_pmf_bus_private(dv
);
1213 free(priv
, M_DEVBUF
);
1217 cardbus_child_register(device_t child
)
1219 device_t self
= device_parent(child
);
1220 struct cardbus_softc
*sc
= device_private(self
);
1221 struct cardbus_devfunc
*ct
;
1222 struct cardbus_child_power
*priv
;
1226 ct
= sc
->sc_funcs
[device_locator(child
, CARDBUSCF_FUNCTION
)];
1228 priv
= malloc(sizeof(*priv
), M_DEVBUF
, M_WAITOK
);
1231 priv
->p_cc
= ct
->ct_cc
;
1232 priv
->p_cf
= ct
->ct_cf
;
1233 priv
->p_tag
= cardbus_make_tag(priv
->p_cc
, priv
->p_cf
, ct
->ct_bus
,
1236 if (cardbus_get_capability(priv
->p_cc
, priv
->p_cf
, priv
->p_tag
,
1237 PCI_CAP_PWRMGMT
, &off
, ®
)) {
1238 priv
->p_has_pm
= true;
1239 priv
->p_pm_offset
= off
;
1240 priv
->p_pm_cap
= reg
;
1242 priv
->p_has_pm
= false;
1243 priv
->p_pm_offset
= -1;
1246 device_pmf_bus_register(child
, priv
, cardbus_child_suspend
,
1247 cardbus_child_resume
, 0, cardbus_child_deregister
);