Home | History | Annotate | Line # | Download | only in dev
if_ath_arbus.c revision 1.13
      1  1.13   martin /* $NetBSD: if_ath_arbus.c,v 1.13 2008/04/28 20:23:28 martin Exp $ */
      2   1.1  gdamore 
      3   1.1  gdamore /*-
      4   1.1  gdamore  * Copyright (c) 2006 Jared D. McNeill <jmcneill (at) invisible.ca>
      5   1.1  gdamore  * All rights reserved.
      6   1.1  gdamore  *
      7   1.1  gdamore  * Redistribution and use in source and binary forms, with or without
      8   1.1  gdamore  * modification, are permitted provided that the following conditions
      9   1.1  gdamore  * are met:
     10   1.1  gdamore  * 1. Redistributions of source code must retain the above copyright
     11   1.1  gdamore  *    notice, this list of conditions and the following disclaimer.
     12   1.1  gdamore  * 2. Redistributions in binary form must reproduce the above copyright
     13   1.1  gdamore  *    notice, this list of conditions and the following disclaimer in the
     14   1.1  gdamore  *    documentation and/or other materials provided with the distribution.
     15   1.1  gdamore  *
     16   1.1  gdamore  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     17   1.1  gdamore  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     18   1.1  gdamore  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     19   1.1  gdamore  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     20   1.1  gdamore  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     21   1.1  gdamore  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     22   1.1  gdamore  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     23   1.1  gdamore  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     24   1.1  gdamore  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     25   1.1  gdamore  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     26   1.1  gdamore  * POSSIBILITY OF SUCH DAMAGE.
     27   1.1  gdamore  */
     28   1.1  gdamore 
     29   1.1  gdamore #include <sys/cdefs.h>
     30  1.13   martin __KERNEL_RCSID(0, "$NetBSD: if_ath_arbus.c,v 1.13 2008/04/28 20:23:28 martin Exp $");
     31   1.1  gdamore 
     32   1.1  gdamore #include <sys/param.h>
     33   1.1  gdamore #include <sys/systm.h>
     34   1.8  gdamore #include <sys/device.h>
     35   1.1  gdamore #include <sys/mbuf.h>
     36   1.1  gdamore #include <sys/malloc.h>
     37   1.1  gdamore #include <sys/kernel.h>
     38   1.1  gdamore #include <sys/errno.h>
     39   1.1  gdamore 
     40   1.1  gdamore #include <machine/bus.h>
     41   1.1  gdamore #include <machine/intr.h>
     42   1.1  gdamore 
     43   1.1  gdamore #include <net/if.h>
     44   1.1  gdamore #include <net/if_media.h>
     45   1.1  gdamore #include <net/if_ether.h>
     46   1.1  gdamore #include <net/if_llc.h>
     47   1.1  gdamore #include <net/if_arp.h>
     48   1.1  gdamore 
     49   1.1  gdamore #include <netinet/in.h>
     50   1.1  gdamore 
     51   1.1  gdamore #include <net80211/ieee80211_netbsd.h>
     52   1.1  gdamore #include <net80211/ieee80211_var.h>
     53   1.1  gdamore 
     54   1.1  gdamore #include <mips/atheros/include/ar531xvar.h>
     55   1.1  gdamore #include <mips/atheros/include/arbusvar.h>
     56   1.1  gdamore 
     57   1.3  gdamore #include <dev/pci/pcidevs.h>
     58   1.1  gdamore #include <dev/ic/ath_netbsd.h>
     59   1.1  gdamore #include <dev/ic/athvar.h>
     60   1.2  gdamore #include <contrib/dev/ath/ah.h>
     61   1.8  gdamore #include <contrib/dev/ath/ah_soc.h>	/* XXX really doesn't belong in hal */
     62   1.1  gdamore 
     63   1.1  gdamore struct ath_arbus_softc {
     64   1.1  gdamore 	struct ath_softc	sc_ath;
     65   1.1  gdamore 	bus_space_tag_t		sc_iot;
     66   1.1  gdamore 	bus_space_handle_t	sc_ioh;
     67   1.1  gdamore 	void			*sc_ih;
     68   1.4  gdamore 	struct ar531x_config	sc_config;
     69   1.1  gdamore };
     70   1.1  gdamore 
     71  1.10   dyoung static int	ath_arbus_match(device_t, struct cfdata *, void *);
     72  1.10   dyoung static void	ath_arbus_attach(device_t, device_t, void *);
     73  1.10   dyoung static int	ath_arbus_detach(device_t, int);
     74   1.1  gdamore 
     75   1.1  gdamore CFATTACH_DECL(ath_arbus, sizeof(struct ath_arbus_softc),
     76   1.1  gdamore     ath_arbus_match, ath_arbus_attach, ath_arbus_detach, NULL);
     77   1.1  gdamore 
     78   1.1  gdamore static int
     79  1.10   dyoung ath_arbus_match(device_t parent, struct cfdata *cf, void *opaque)
     80   1.1  gdamore {
     81   1.1  gdamore 	struct arbus_attach_args *aa;
     82   1.1  gdamore 
     83   1.1  gdamore 	aa = (struct arbus_attach_args *)opaque;
     84   1.1  gdamore 	if (strcmp(aa->aa_name, "ath") == 0)
     85   1.1  gdamore 		return 1;
     86   1.1  gdamore 
     87   1.1  gdamore 	return 0;
     88   1.1  gdamore }
     89   1.1  gdamore 
     90  1.10   dyoung static bool
     91  1.11   dyoung ath_arbus_resume(device_t dv PMF_FN_ARGS)
     92  1.10   dyoung {
     93  1.10   dyoung 	struct ath_arbus_softc *asc = device_private(dv);
     94  1.10   dyoung 	ath_resume(&asc->sc_ath);
     95  1.10   dyoung 
     96  1.10   dyoung 	return true;
     97  1.10   dyoung }
     98  1.10   dyoung 
     99  1.10   dyoung 
    100   1.1  gdamore static void
    101  1.10   dyoung ath_arbus_attach(device_t parent, device_t self, void *opaque)
    102   1.1  gdamore {
    103   1.1  gdamore 	struct ath_arbus_softc *asc;
    104   1.1  gdamore 	struct ath_softc *sc;
    105   1.1  gdamore 	struct arbus_attach_args *aa;
    106   1.2  gdamore 	const char *name;
    107   1.8  gdamore 	prop_number_t prop;
    108   1.1  gdamore 	int rv;
    109   1.2  gdamore 	uint16_t devid;
    110   1.1  gdamore 
    111  1.10   dyoung 	asc = device_private(self);
    112   1.1  gdamore 	sc = &asc->sc_ath;
    113   1.1  gdamore 	aa = (struct arbus_attach_args *)opaque;
    114   1.1  gdamore 
    115   1.8  gdamore 	prop = prop_dictionary_get(device_properties(&sc->sc_dev),
    116   1.8  gdamore 	    "wmac-rev");
    117   1.8  gdamore 	if (prop == NULL) {
    118   1.8  gdamore 		printf(": unable to get wmac-rev property\n");
    119   1.8  gdamore 		return;
    120   1.8  gdamore 	}
    121   1.8  gdamore 	KDASSERT(prop_object_type(prop) == PROP_TYPE_NUMBER);
    122   1.8  gdamore 
    123   1.8  gdamore 	devid = (uint16_t)prop_number_integer_value(prop);
    124   1.3  gdamore 	name = ath_hal_probe(PCI_VENDOR_ATHEROS, devid);
    125   1.1  gdamore 
    126   1.2  gdamore 	printf(": %s\n", name ? name : "Unknown AR531X WLAN");
    127   1.1  gdamore 
    128   1.1  gdamore 	asc->sc_iot = aa->aa_bst;
    129   1.1  gdamore 	rv = bus_space_map(asc->sc_iot, aa->aa_addr, aa->aa_size, 0,
    130   1.1  gdamore 	    &asc->sc_ioh);
    131   1.1  gdamore 	if (rv) {
    132   1.1  gdamore 		aprint_error("%s: unable to map registers\n",
    133   1.1  gdamore 		    sc->sc_dev.dv_xname);
    134   1.1  gdamore 		return;
    135   1.1  gdamore 	}
    136   1.4  gdamore 	/*
    137   1.4  gdamore 	 * Setup HAL configuration state for use by the driver.
    138   1.4  gdamore 	 */
    139   1.4  gdamore 	rv = ar531x_board_config(&asc->sc_config);
    140   1.4  gdamore 	if (rv) {
    141   1.4  gdamore 		aprint_error("%s: unable to locate board configuration\n",
    142   1.4  gdamore 		    sc->sc_dev.dv_xname);
    143   1.4  gdamore 		return;
    144   1.4  gdamore 	}
    145   1.4  gdamore 	asc->sc_config.unit = sc->sc_dev.dv_unit;	/* XXX? */
    146   1.4  gdamore 	asc->sc_config.tag = asc->sc_iot;
    147   1.1  gdamore 
    148   1.4  gdamore 	/* NB: the HAL expects the config state passed as the tag */
    149   1.4  gdamore 	sc->sc_st = (HAL_BUS_TAG) &asc->sc_config;
    150   1.4  gdamore 	sc->sc_sh = (HAL_BUS_HANDLE) asc->sc_ioh;
    151   1.2  gdamore 	sc->sc_dmat = aa->aa_dmat;
    152   1.1  gdamore 
    153   1.7  gdamore 	asc->sc_ih = arbus_intr_establish(aa->aa_cirq, aa->aa_mirq, ath_intr,
    154   1.7  gdamore 	    sc);
    155   1.1  gdamore 	if (asc->sc_ih == NULL) {
    156   1.1  gdamore 		aprint_error("%s: couldn't establish interrupt\n",
    157   1.1  gdamore 		    sc->sc_dev.dv_xname);
    158   1.1  gdamore 		return;
    159   1.1  gdamore 	}
    160   1.1  gdamore 
    161  1.10   dyoung 	ATH_LOCK_INIT(sc);
    162  1.10   dyoung 
    163  1.10   dyoung 	if (!pmf_device_register(self, NULL, ath_arbus_resume))
    164  1.10   dyoung 		aprint_error_dev(self, "couldn't establish power handler\n");
    165  1.10   dyoung 	else
    166  1.10   dyoung 		pmf_class_network_register(self, &sc->sc_if);
    167  1.10   dyoung 
    168   1.2  gdamore 	if (ath_attach(devid, sc) != 0) {
    169   1.2  gdamore 		aprint_error("%s: ath_attach failed\n", sc->sc_dev.dv_xname);
    170   1.2  gdamore 		goto err;
    171   1.2  gdamore 	}
    172   1.2  gdamore 
    173   1.1  gdamore 	return;
    174   1.1  gdamore 
    175   1.1  gdamore err:
    176   1.1  gdamore 	arbus_intr_disestablish(asc->sc_ih);
    177   1.1  gdamore }
    178   1.1  gdamore 
    179   1.1  gdamore static int
    180  1.10   dyoung ath_arbus_detach(device_t self, int flags)
    181   1.1  gdamore {
    182  1.10   dyoung 	struct ath_arbus_softc *asc = device_private(self);
    183   1.1  gdamore 
    184   1.1  gdamore 	ath_detach(&asc->sc_ath);
    185   1.1  gdamore 	arbus_intr_disestablish(asc->sc_ih);
    186   1.1  gdamore 
    187   1.1  gdamore 	return (0);
    188   1.1  gdamore }
    189