1 /* $NetBSD: if_ath_cardbus.c,v 1.36 2009/10/21 14:15:52 rmind Exp $ */
4 * Ichiro FUKUHARA <ichiro@ichiro.org>.
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 ICHIRO FUKUHARA ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL ICHIRO FUKUHARA OR THE VOICES IN HIS HEAD BE LIABLE FOR
20 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * CardBus bus front-end for the AR5001 Wireless LAN 802.11a/b/g CardBus.
32 #include <sys/cdefs.h>
33 __KERNEL_RCSID(0, "$NetBSD: if_ath_cardbus.c,v 1.36 2009/10/21 14:15:52 rmind Exp $");
38 #include <sys/param.h>
39 #include <sys/systm.h>
41 #include <sys/malloc.h>
42 #include <sys/kernel.h>
43 #include <sys/socket.h>
44 #include <sys/ioctl.h>
45 #include <sys/errno.h>
46 #include <sys/device.h>
48 #include <machine/endian.h>
51 #include <net/if_dl.h>
52 #include <net/if_media.h>
53 #include <net/if_ether.h>
55 #include <net80211/ieee80211_netbsd.h>
56 #include <net80211/ieee80211_var.h>
63 #include <netinet/in.h>
64 #include <netinet/if_inarp.h>
71 #include <dev/mii/miivar.h>
72 #include <dev/mii/mii_bitbang.h>
74 #include <dev/ic/ath_netbsd.h>
75 #include <dev/ic/athvar.h>
77 #include <external/isc/atheros_hal/dist/ah.h>
79 #include <dev/pci/pcivar.h>
80 #include <dev/pci/pcireg.h>
81 #include <dev/pci/pcidevs.h>
83 #include <dev/cardbus/cardbusvar.h>
84 #include <dev/pci/pcidevs.h>
87 * PCI configuration space registers
89 #define ATH_PCI_MMBA 0x10 /* memory mapped base */
91 struct ath_cardbus_softc
{
92 struct ath_softc sc_ath
;
94 /* CardBus-specific goo. */
95 void *sc_ih
; /* interrupt handle */
96 cardbus_devfunc_t sc_ct
; /* our CardBus devfuncs */
97 cardbustag_t sc_tag
; /* our CardBus tag */
98 bus_size_t sc_mapsize
; /* the size of mapped bus space region */
100 pcireg_t sc_bar_val
; /* value of the BAR */
102 cardbus_intr_line_t sc_intrline
; /* interrupt line */
103 bus_space_tag_t sc_iot
;
104 bus_space_handle_t sc_ioh
;
107 int ath_cardbus_match(device_t
, cfdata_t
, void *);
108 void ath_cardbus_attach(device_t
, device_t
, void *);
109 int ath_cardbus_detach(device_t
, int);
111 CFATTACH_DECL_NEW(ath_cardbus
, sizeof(struct ath_cardbus_softc
),
112 ath_cardbus_match
, ath_cardbus_attach
, ath_cardbus_detach
, NULL
);
114 void ath_cardbus_setup(struct ath_cardbus_softc
*);
117 ath_cardbus_suspend(device_t self
, pmf_qual_t qual
)
119 struct ath_cardbus_softc
*csc
= device_private(self
);
121 ath_suspend(&csc
->sc_ath
);
122 if (csc
->sc_ih
!= NULL
) {
123 cardbus_intr_disestablish(csc
->sc_ct
->ct_cc
, csc
->sc_ct
->ct_cf
,
131 ath_cardbus_resume(device_t self
, pmf_qual_t qual
)
133 struct ath_cardbus_softc
*csc
= device_private(self
);
135 csc
->sc_ih
= cardbus_intr_establish(csc
->sc_ct
->ct_cc
,
136 csc
->sc_ct
->ct_cf
, csc
->sc_intrline
, IPL_NET
, ath_intr
,
139 if (csc
->sc_ih
== NULL
) {
140 aprint_error_dev(self
,
141 "unable to establish interrupt\n");
145 return ath_resume(&csc
->sc_ath
);
149 ath_cardbus_match(device_t parent
, cfdata_t match
, void *aux
)
151 struct cardbus_attach_args
*ca
= aux
;
154 devname
= ath_hal_probe(PCI_VENDOR(ca
->ca_id
), PCI_PRODUCT(ca
->ca_id
));
163 ath_cardbus_attach(device_t parent
, device_t self
, void *aux
)
165 struct ath_cardbus_softc
*csc
= device_private(self
);
166 struct ath_softc
*sc
= &csc
->sc_ath
;
167 struct cardbus_attach_args
*ca
= aux
;
168 cardbus_devfunc_t ct
= ca
->ca_ct
;
172 sc
->sc_dmat
= ca
->ca_dmat
;
174 csc
->sc_tag
= ca
->ca_tag
;
181 if (Cardbus_mapreg_map(ct
, ATH_PCI_MMBA
, PCI_MAPREG_TYPE_MEM
, 0,
182 &csc
->sc_iot
, &csc
->sc_ioh
, &adr
, &csc
->sc_mapsize
) == 0) {
185 (*ct
->ct_cf
->cardbus_mem_open
)(cc
, 0, adr
, adr
+csc
->sc_mapsize
);
187 csc
->sc_bar_val
= adr
| PCI_MAPREG_TYPE_MEM
;
189 aprint_error_dev(self
, "unable to map device registers\n");
193 sc
->sc_st
= HALTAG(csc
->sc_iot
);
194 sc
->sc_sh
= HALHANDLE(csc
->sc_ioh
);
197 * Set up the PCI configuration registers.
199 ath_cardbus_setup(csc
);
201 /* Remember which interrupt line. */
202 csc
->sc_intrline
= ca
->ca_intrline
;
207 * Finish off the attach.
209 if (ath_attach(PCI_PRODUCT(ca
->ca_id
), sc
) != 0)
212 if (pmf_device_register(self
,
213 ath_cardbus_suspend
, ath_cardbus_resume
)) {
214 pmf_class_network_register(self
, &sc
->sc_if
);
215 pmf_device_suspend(self
, &sc
->sc_qual
);
217 aprint_error_dev(self
, "couldn't establish power handler\n");
221 ath_cardbus_detach(device_t self
, int flags
)
223 struct ath_cardbus_softc
*csc
= device_private(self
);
224 struct ath_softc
*sc
= &csc
->sc_ath
;
225 struct cardbus_devfunc
*ct
= csc
->sc_ct
;
228 #if defined(DIAGNOSTIC)
230 panic("%s: data structure lacks", device_xname(sc
->sc_dev
));
237 pmf_device_deregister(self
);
240 * Unhook the interrupt handler.
242 if (csc
->sc_ih
!= NULL
) {
243 cardbus_intr_disestablish(ct
->ct_cc
, ct
->ct_cf
, csc
->sc_ih
);
248 * Release bus space and close window.
250 Cardbus_mapreg_unmap(ct
, ATH_PCI_MMBA
, csc
->sc_iot
, csc
->sc_ioh
,
253 ATH_LOCK_DESTROY(sc
);
259 ath_cardbus_setup(struct ath_cardbus_softc
*csc
)
261 cardbus_devfunc_t ct
= csc
->sc_ct
;
262 cardbus_chipset_tag_t cc
= ct
->ct_cc
;
263 cardbus_function_tag_t cf
= ct
->ct_cf
;
267 if ((rc
= cardbus_set_powerstate(ct
, csc
->sc_tag
, PCI_PWR_D0
)) != 0)
268 aprint_debug("%s: cardbus_set_powerstate %d\n", __func__
, rc
);
270 /* Program the BAR. */
271 cardbus_conf_write(cc
, cf
, csc
->sc_tag
, ATH_PCI_MMBA
, csc
->sc_bar_val
);
273 /* Enable the appropriate bits in the PCI CSR. */
274 reg
= cardbus_conf_read(cc
, cf
, csc
->sc_tag
,
275 PCI_COMMAND_STATUS_REG
);
276 reg
|= PCI_COMMAND_MASTER_ENABLE
| PCI_COMMAND_MEM_ENABLE
;
277 cardbus_conf_write(cc
, cf
, csc
->sc_tag
, PCI_COMMAND_STATUS_REG
, reg
);