#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: if_ath_cardbus.c,v 1.46 2022/09/25 17:33:19 thorpej Exp $");
#include "opt_inet.h"
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/mbuf.h>
#include <sys/kernel.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <sys/errno.h>
#include <sys/device.h>
#include <machine/endian.h>
#include <net/if.h>
#include <net/if_dl.h>
#include <net/if_media.h>
#include <net/if_ether.h>
#include <net80211/ieee80211_netbsd.h>
#include <net80211/ieee80211_var.h>
#ifdef INET
#include <netinet/in.h>
#include <netinet/if_inarp.h>
#endif
#include <sys/bus.h>
#include <sys/intr.h>
#include <dev/mii/miivar.h>
#include <dev/mii/mii_bitbang.h>
#include <dev/ic/ath_netbsd.h>
#include <dev/ic/athvar.h>
#include <external/isc/atheros_hal/dist/ah.h>
#include <dev/pci/pcivar.h>
#include <dev/pci/pcireg.h>
#include <dev/pci/pcidevs.h>
#include <dev/cardbus/cardbusvar.h>
#include <dev/pci/pcidevs.h>
#define ATH_PCI_MMBA PCI_BAR(0)
struct ath_cardbus_softc {
struct ath_softc sc_ath;
void *sc_ih;
cardbus_devfunc_t sc_ct;
pcitag_t sc_tag;
bus_size_t sc_mapsize;
pcireg_t sc_bar_val;
bus_space_tag_t sc_iot;
bus_space_handle_t sc_ioh;
};
int ath_cardbus_match(device_t, cfdata_t, void *);
void ath_cardbus_attach(device_t, device_t, void *);
int ath_cardbus_detach(device_t, int);
CFATTACH_DECL_NEW(ath_cardbus, sizeof(struct ath_cardbus_softc),
ath_cardbus_match, ath_cardbus_attach, ath_cardbus_detach, NULL);
void ath_cardbus_setup(struct ath_cardbus_softc *);
static bool
ath_cardbus_suspend(device_t self, const pmf_qual_t *qual)
{
struct ath_cardbus_softc *csc = device_private(self);
ath_suspend(&csc->sc_ath);
if (csc->sc_ih != NULL) {
Cardbus_intr_disestablish(csc->sc_ct, csc->sc_ih);
csc->sc_ih = NULL;
}
return true;
}
static bool
ath_cardbus_resume(device_t self, const pmf_qual_t *qual)
{
struct ath_cardbus_softc *csc = device_private(self);
csc->sc_ih = Cardbus_intr_establish(csc->sc_ct,
IPL_NET, ath_intr, &csc->sc_ath);
if (csc->sc_ih == NULL) {
aprint_error_dev(self,
"unable to establish interrupt\n");
return false;
}
return ath_resume(&csc->sc_ath);
}
int
ath_cardbus_match(device_t parent, cfdata_t match, void *aux)
{
struct cardbus_attach_args *ca = aux;
const char *devname;
devname = ath_hal_probe(PCI_VENDOR(ca->ca_id), PCI_PRODUCT(ca->ca_id));
if (devname)
return 1;
return 0;
}
void
ath_cardbus_attach(device_t parent, device_t self, void *aux)
{
struct ath_cardbus_softc *csc = device_private(self);
struct ath_softc *sc = &csc->sc_ath;
struct cardbus_attach_args *ca = aux;
cardbus_devfunc_t ct = ca->ca_ct;
bus_addr_t adr;
sc->sc_dev = self;
sc->sc_dmat = ca->ca_dmat;
csc->sc_ct = ct;
csc->sc_tag = ca->ca_tag;
aprint_normal("\n");
if (Cardbus_mapreg_map(ct, ATH_PCI_MMBA, PCI_MAPREG_TYPE_MEM, 0,
&csc->sc_iot, &csc->sc_ioh, &adr, &csc->sc_mapsize) == 0) {
csc->sc_bar_val = adr | PCI_MAPREG_TYPE_MEM;
} else {
aprint_error_dev(self, "unable to map device registers\n");
return;
}
sc->sc_st = HALTAG(csc->sc_iot);
sc->sc_sh = HALHANDLE(csc->sc_ioh);
ath_cardbus_setup(csc);
if (ath_attach(PCI_PRODUCT(ca->ca_id), sc) != 0)
return;
if (pmf_device_register(self,
ath_cardbus_suspend, ath_cardbus_resume)) {
pmf_class_network_register(self, &sc->sc_if);
pmf_device_suspend(self, &sc->sc_qual);
} else
aprint_error_dev(self, "couldn't establish power handler\n");
}
int
ath_cardbus_detach(device_t self, int flags)
{
struct ath_cardbus_softc *csc = device_private(self);
struct ath_softc *sc = &csc->sc_ath;
struct cardbus_devfunc *ct = csc->sc_ct;
int rv;
#if defined(DIAGNOSTIC)
if (ct == NULL)
panic("%s: data structure lacks", device_xname(sc->sc_dev));
#endif
rv = ath_detach(sc);
if (rv)
return (rv);
pmf_device_deregister(self);
if (csc->sc_ih != NULL) {
Cardbus_intr_disestablish(ct, csc->sc_ih);
csc->sc_ih = NULL;
}
Cardbus_mapreg_unmap(ct, ATH_PCI_MMBA, csc->sc_iot, csc->sc_ioh,
csc->sc_mapsize);
return (0);
}
void
ath_cardbus_setup(struct ath_cardbus_softc *csc)
{
cardbus_devfunc_t ct = csc->sc_ct;
int rc;
pcireg_t reg;
if ((rc = cardbus_set_powerstate(ct, csc->sc_tag, PCI_PWR_D0)) != 0)
aprint_debug("%s: cardbus_set_powerstate %d\n", __func__, rc);
Cardbus_conf_write(ct, csc->sc_tag, ATH_PCI_MMBA, csc->sc_bar_val);
reg = Cardbus_conf_read(ct, csc->sc_tag,
PCI_COMMAND_STATUS_REG);
reg |= PCI_COMMAND_MASTER_ENABLE | PCI_COMMAND_MEM_ENABLE;
Cardbus_conf_write(ct, csc->sc_tag, PCI_COMMAND_STATUS_REG, reg);
}