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