if_ral_pci.c revision 1.4.6.1       1 /*	$NetBSD: if_ral_pci.c,v 1.4.6.1 2006/11/18 21:34:30 ad Exp $	*/
      2 /*	$OpenBSD: if_ral_pci.c,v 1.6 2006/01/09 20:03:43 damien Exp $  */
      3 
      4 /*-
      5  * Copyright (c) 2005, 2006
      6  *	Damien Bergamini <damien.bergamini (at) free.fr>
      7  *
      8  * Permission to use, copy, modify, and distribute this software for any
      9  * purpose with or without fee is hereby granted, provided that the above
     10  * copyright notice and this permission notice appear in all copies.
     11  *
     12  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
     13  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
     14  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
     15  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
     16  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
     17  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
     18  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
     19  */
     20 
     21 /*
     22  * PCI front-end for the Ralink RT2560/RT2561/RT2561S/RT2661 driver.
     23  */
     24 #include <sys/cdefs.h>
     25 __KERNEL_RCSID(0, "$NetBSD: if_ral_pci.c,v 1.4.6.1 2006/11/18 21:34:30 ad Exp $");
     26 
     27 #include "bpfilter.h"
     28 
     29 #include <sys/param.h>
     30 #include <sys/sockio.h>
     31 #include <sys/mbuf.h>
     32 #include <sys/kernel.h>
     33 #include <sys/socket.h>
     34 #include <sys/systm.h>
     35 #include <sys/malloc.h>
     36 #include <sys/device.h>
     37 
     38 #include <machine/bus.h>
     39 #include <machine/intr.h>
     40 
     41 #include <net/if.h>
     42 #include <net/if_dl.h>
     43 #include <net/if_media.h>
     44 #include <net/if_ether.h>
     45 
     46 #include <netinet/in.h>
     47 
     48 #include <net80211/ieee80211_var.h>
     49 #include <net80211/ieee80211_rssadapt.h>
     50 #include <net80211/ieee80211_radiotap.h>
     51 
     52 #include <dev/ic/rt2560var.h>
     53 #include <dev/ic/rt2661var.h>
     54 
     55 #include <dev/pci/pcireg.h>
     56 #include <dev/pci/pcivar.h>
     57 #include <dev/pci/pcidevs.h>
     58 
     59 static struct ral_opns {
     60 	int	(*attach)(void *, int);
     61 	int	(*detach)(void *);
     62 	int	(*intr)(void *);
     63 
     64 }  ral_rt2560_opns = {
     65 	rt2560_attach,
     66 	rt2560_detach,
     67 	rt2560_intr
     68 
     69 }, ral_rt2661_opns = {
     70 	rt2661_attach,
     71 	rt2661_detach,
     72 	rt2661_intr
     73 };
     74 
     75 struct ral_pci_softc {
     76 	union {
     77 		struct rt2560_softc	sc_rt2560;
     78 		struct rt2661_softc	sc_rt2661;
     79 	} u;
     80 #define sc_sc	u.sc_rt2560
     81 
     82 	/* PCI specific goo */
     83 	struct ral_opns		*sc_opns;
     84 	pci_chipset_tag_t	sc_pc;
     85 	void			*sc_ih;
     86 	bus_size_t		sc_mapsize;
     87 };
     88 
     89 /* Base Address Register */
     90 #define RAL_PCI_BAR0	0x10
     91 
     92 int	ral_pci_match(struct device *, struct cfdata *, void *);
     93 void	ral_pci_attach(struct device *, struct device *, void *);
     94 int	ral_pci_detach(struct device *, int);
     95 
     96 CFATTACH_DECL(ral_pci, sizeof (struct ral_pci_softc),
     97 	ral_pci_match, ral_pci_attach, ral_pci_detach, NULL);
     98 
     99 int
    100 ral_pci_match(struct device *parent, struct cfdata *cfdata,
    101     void *aux)
    102 {
    103 	struct pci_attach_args *pa = aux;
    104 
    105 	if (PCI_VENDOR(pa->pa_id) == PCI_VENDOR_RALINK) {
    106 		switch (PCI_PRODUCT(pa->pa_id)) {
    107 			case PCI_PRODUCT_RALINK_RT2560:
    108 			case PCI_PRODUCT_RALINK_RT2561:
    109 			case PCI_PRODUCT_RALINK_RT2561S:
    110 			case PCI_PRODUCT_RALINK_RT2661:
    111 				return 1;
    112 			default:
    113 				return 0;
    114 		}
    115 	}
    116 
    117 	return 0;
    118 }
    119 
    120 void
    121 ral_pci_attach(struct device *parent, struct device *self, void *aux)
    122 {
    123 	struct ral_pci_softc *psc = (struct ral_pci_softc *)self;
    124 	struct rt2560_softc *sc = &psc->sc_sc;
    125 	struct pci_attach_args *pa = aux;
    126 	const char *intrstr;
    127 	char devinfo[256];
    128 	bus_addr_t base;
    129 	pci_intr_handle_t ih;
    130 	pcireg_t reg;
    131 	int error, revision;
    132 
    133 	pci_devinfo(pa->pa_id, pa->pa_class, 0, devinfo, sizeof(devinfo));
    134 	revision = PCI_REVISION(pa->pa_class);
    135 	aprint_normal(": %s (rev. 0x%02x)\n", devinfo, revision);
    136 
    137 	psc->sc_opns = (PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_RALINK_RT2560) ?
    138 	    &ral_rt2560_opns : &ral_rt2661_opns;
    139 
    140 	sc->sc_dmat = pa->pa_dmat;
    141 	psc->sc_pc = pa->pa_pc;
    142 
    143 	/* enable the appropriate bits in the PCI CSR */
    144 	reg = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG);
    145 	reg |= PCI_COMMAND_MASTER_ENABLE | PCI_COMMAND_MEM_ENABLE;
    146 	pci_conf_write(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG, reg);
    147 
    148 	/* map control/status registers */
    149 	error = pci_mapreg_map(pa, RAL_PCI_BAR0, PCI_MAPREG_TYPE_MEM |
    150 	    PCI_MAPREG_MEM_TYPE_32BIT, 0, &sc->sc_st, &sc->sc_sh, &base,
    151 	    &psc->sc_mapsize);
    152 
    153 	if (error != 0) {
    154 		aprint_error(": could not map memory space\n");
    155 		return;
    156 	}
    157 
    158 	if (pci_intr_map(pa, &ih) != 0) {
    159 		aprint_error(": could not map interrupt\n");
    160 		return;
    161 	}
    162 
    163 	intrstr = pci_intr_string(psc->sc_pc, ih);
    164 	psc->sc_ih = pci_intr_establish(psc->sc_pc, ih, IPL_NET,
    165 	    psc->sc_opns->intr, sc);
    166 
    167 	if (psc->sc_ih == NULL) {
    168 		aprint_error(": could not establish interrupt");
    169 		if (intrstr != NULL)
    170 			printf(" at %s", intrstr);
    171 		aprint_error("\n");
    172 		return;
    173 	}
    174 	aprint_normal("%s: interrupting at %s\n", sc->sc_dev.dv_xname, intrstr);
    175 
    176 	(*psc->sc_opns->attach)(sc, PCI_PRODUCT(pa->pa_id));
    177 }
    178 
    179 int
    180 ral_pci_detach(struct device *self, int flags)
    181 {
    182 	struct ral_pci_softc *psc = (struct ral_pci_softc *)self;
    183 	struct rt2560_softc *sc = &psc->sc_sc;
    184 
    185 	(*psc->sc_opns->detach)(sc);
    186 	pci_intr_disestablish(psc->sc_pc, psc->sc_ih);
    187 
    188 	return 0;
    189 }
    190 
    191