if_ral_pci.c revision 1.2 1 1.2 drochner /* $NetBSD: if_ral_pci.c,v 1.2 2005/07/04 17:50:10 drochner Exp $ */
2 1.1 drochner /* $OpenBSD: if_ral_pci.c,v 1.4 2005/02/22 10:41:30 damien Exp $ */
3 1.1 drochner
4 1.1 drochner /*-
5 1.1 drochner * Copyright (c) 2005
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.1 drochner * PCI front-end for the Ralink RT2500 driver.
23 1.1 drochner */
24 1.1 drochner
25 1.1 drochner #include <sys/cdefs.h>
26 1.2 drochner __KERNEL_RCSID(0, "$NetBSD: if_ral_pci.c,v 1.2 2005/07/04 17:50:10 drochner Exp $");
27 1.1 drochner
28 1.1 drochner #include "bpfilter.h"
29 1.1 drochner
30 1.1 drochner #include <sys/param.h>
31 1.1 drochner #include <sys/sockio.h>
32 1.1 drochner #include <sys/mbuf.h>
33 1.1 drochner #include <sys/kernel.h>
34 1.1 drochner #include <sys/socket.h>
35 1.1 drochner #include <sys/systm.h>
36 1.1 drochner #include <sys/malloc.h>
37 1.1 drochner #include <sys/device.h>
38 1.1 drochner
39 1.1 drochner #include <machine/bus.h>
40 1.1 drochner #include <machine/intr.h>
41 1.1 drochner
42 1.1 drochner #include <net/if.h>
43 1.1 drochner #include <net/if_dl.h>
44 1.1 drochner #include <net/if_ether.h>
45 1.1 drochner #include <net/if_media.h>
46 1.1 drochner
47 1.1 drochner #include <netinet/in.h>
48 1.1 drochner
49 1.1 drochner #include <net80211/ieee80211_var.h>
50 1.1 drochner #include <net80211/ieee80211_rssadapt.h>
51 1.1 drochner #include <net80211/ieee80211_radiotap.h>
52 1.1 drochner
53 1.1 drochner #include <dev/ic/ralvar.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.1 drochner struct ral_pci_softc {
60 1.1 drochner struct ral_softc sc_sc;
61 1.1 drochner
62 1.1 drochner /* PCI specific goo */
63 1.1 drochner pci_chipset_tag_t sc_pc;
64 1.1 drochner void *sc_ih;
65 1.1 drochner bus_size_t sc_mapsize;
66 1.1 drochner };
67 1.1 drochner
68 1.1 drochner /* Base Address Register */
69 1.1 drochner #define RAL_PCI_BAR0 0x10
70 1.1 drochner
71 1.1 drochner int ral_pci_match(struct device *, struct cfdata *, void *);
72 1.1 drochner void ral_pci_attach(struct device *, struct device *, void *);
73 1.1 drochner int ral_pci_detach(struct device *, int);
74 1.1 drochner
75 1.1 drochner CFATTACH_DECL(ral_pci, sizeof (struct ral_pci_softc),
76 1.1 drochner ral_pci_match, ral_pci_attach, ral_pci_detach, NULL);
77 1.1 drochner
78 1.1 drochner int
79 1.1 drochner ral_pci_match(struct device *parent, struct cfdata *match, void *aux)
80 1.1 drochner {
81 1.1 drochner struct pci_attach_args *pa = aux;
82 1.1 drochner
83 1.1 drochner if (PCI_VENDOR(pa->pa_id) == PCI_VENDOR_RALINK &&
84 1.1 drochner PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_RALINK_RT2560)
85 1.1 drochner return 1;
86 1.1 drochner
87 1.1 drochner return 0;
88 1.1 drochner }
89 1.1 drochner
90 1.1 drochner void
91 1.1 drochner ral_pci_attach(struct device *parent, struct device *self, void *aux)
92 1.1 drochner {
93 1.1 drochner struct ral_pci_softc *psc = (struct ral_pci_softc *)self;
94 1.1 drochner struct ral_softc *sc = &psc->sc_sc;
95 1.1 drochner struct pci_attach_args *pa = aux;
96 1.1 drochner const char *intrstr;
97 1.1 drochner bus_addr_t base;
98 1.1 drochner pci_intr_handle_t ih;
99 1.1 drochner pcireg_t reg;
100 1.1 drochner int error;
101 1.1 drochner
102 1.1 drochner sc->sc_dmat = pa->pa_dmat;
103 1.1 drochner psc->sc_pc = pa->pa_pc;
104 1.1 drochner
105 1.1 drochner /* enable the appropriate bits in the PCI CSR */
106 1.1 drochner reg = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG);
107 1.1 drochner reg |= PCI_COMMAND_MASTER_ENABLE | PCI_COMMAND_MEM_ENABLE;
108 1.1 drochner pci_conf_write(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG, reg);
109 1.1 drochner
110 1.1 drochner /* map control/status registers */
111 1.1 drochner error = pci_mapreg_map(pa, RAL_PCI_BAR0, PCI_MAPREG_TYPE_MEM |
112 1.1 drochner PCI_MAPREG_MEM_TYPE_32BIT, 0, &sc->sc_st, &sc->sc_sh, &base,
113 1.1 drochner &psc->sc_mapsize);
114 1.1 drochner if (error != 0) {
115 1.1 drochner printf(": could not map memory space\n");
116 1.1 drochner return;
117 1.1 drochner }
118 1.1 drochner
119 1.1 drochner if (pci_intr_map(pa, &ih) != 0) {
120 1.1 drochner printf(": could not map interrupt\n");
121 1.1 drochner return;
122 1.1 drochner }
123 1.1 drochner
124 1.1 drochner intrstr = pci_intr_string(psc->sc_pc, ih);
125 1.1 drochner psc->sc_ih = pci_intr_establish(psc->sc_pc, ih, IPL_NET, ral_intr, sc);
126 1.1 drochner if (psc->sc_ih == NULL) {
127 1.1 drochner printf(": could not establish interrupt");
128 1.1 drochner if (intrstr != NULL)
129 1.1 drochner printf(" at %s", intrstr);
130 1.1 drochner printf("\n");
131 1.1 drochner return;
132 1.1 drochner }
133 1.2 drochner printf(": %s\n", intrstr);
134 1.1 drochner
135 1.1 drochner ral_attach(sc);
136 1.1 drochner }
137 1.1 drochner
138 1.1 drochner int
139 1.1 drochner ral_pci_detach(struct device *self, int flags)
140 1.1 drochner {
141 1.1 drochner struct ral_pci_softc *psc = (struct ral_pci_softc *)self;
142 1.1 drochner struct ral_softc *sc = &psc->sc_sc;
143 1.1 drochner
144 1.1 drochner ral_detach(sc);
145 1.1 drochner pci_intr_disestablish(psc->sc_pc, psc->sc_ih);
146 1.1 drochner
147 1.1 drochner return 0;
148 1.1 drochner }
149