if_malo_pci.c revision 1.3.4.2 1 1.3.4.2 yamt /* $NetBSD: if_malo_pci.c,v 1.3.4.2 2012/10/30 17:21:30 yamt Exp $ */
2 1.3.4.2 yamt /* $OpenBSD: if_malo_pci.c,v 1.6 2010/08/28 23:19:29 deraadt Exp $ */
3 1.3.4.2 yamt
4 1.3.4.2 yamt /*
5 1.3.4.2 yamt * Copyright (c) 2006 Marcus Glocker <mglocker (at) openbsd.org>
6 1.3.4.2 yamt *
7 1.3.4.2 yamt * Permission to use, copy, modify, and distribute this software for any
8 1.3.4.2 yamt * purpose with or without fee is hereby granted, provided that the above
9 1.3.4.2 yamt * copyright notice and this permission notice appear in all copies.
10 1.3.4.2 yamt *
11 1.3.4.2 yamt * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12 1.3.4.2 yamt * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 1.3.4.2 yamt * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14 1.3.4.2 yamt * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 1.3.4.2 yamt * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 1.3.4.2 yamt * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17 1.3.4.2 yamt * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 1.3.4.2 yamt */
19 1.3.4.2 yamt
20 1.3.4.2 yamt /*
21 1.3.4.2 yamt * PCI front-end for the Marvell Libertas
22 1.3.4.2 yamt */
23 1.3.4.2 yamt
24 1.3.4.2 yamt #include <sys/cdefs.h>
25 1.3.4.2 yamt __KERNEL_RCSID(0, "$NetBSD: if_malo_pci.c,v 1.3.4.2 2012/10/30 17:21:30 yamt Exp $");
26 1.3.4.2 yamt
27 1.3.4.2 yamt #include <sys/param.h>
28 1.3.4.2 yamt #include <sys/sockio.h>
29 1.3.4.2 yamt #include <sys/mbuf.h>
30 1.3.4.2 yamt #include <sys/kernel.h>
31 1.3.4.2 yamt #include <sys/socket.h>
32 1.3.4.2 yamt #include <sys/systm.h>
33 1.3.4.2 yamt #include <sys/malloc.h>
34 1.3.4.2 yamt #include <sys/device.h>
35 1.3.4.2 yamt #include <sys/bus.h>
36 1.3.4.2 yamt
37 1.3.4.2 yamt #include <machine/intr.h>
38 1.3.4.2 yamt
39 1.3.4.2 yamt #include <net/if.h>
40 1.3.4.2 yamt #include <net/if_dl.h>
41 1.3.4.2 yamt #include <net/if_media.h>
42 1.3.4.2 yamt #include <net/if_ether.h>
43 1.3.4.2 yamt
44 1.3.4.2 yamt #include <netinet/in.h>
45 1.3.4.2 yamt
46 1.3.4.2 yamt #include <net80211/ieee80211_var.h>
47 1.3.4.2 yamt #include <net80211/ieee80211_radiotap.h>
48 1.3.4.2 yamt
49 1.3.4.2 yamt #include <dev/ic/malovar.h>
50 1.3.4.2 yamt
51 1.3.4.2 yamt #include <dev/pci/pcireg.h>
52 1.3.4.2 yamt #include <dev/pci/pcivar.h>
53 1.3.4.2 yamt #include <dev/pci/pcidevs.h>
54 1.3.4.2 yamt
55 1.3.4.2 yamt /* Base Address Register */
56 1.3.4.2 yamt #define MALO_PCI_BAR1 0x10
57 1.3.4.2 yamt #define MALO_PCI_BAR2 0x14
58 1.3.4.2 yamt
59 1.3.4.2 yamt static int malo_pci_match(device_t parent, cfdata_t match, void *aux);
60 1.3.4.2 yamt static void malo_pci_attach(device_t, device_t, void *);
61 1.3.4.2 yamt static int malo_pci_detach(device_t, int);
62 1.3.4.2 yamt static bool malo_pci_suspend(device_t, const pmf_qual_t *);
63 1.3.4.2 yamt static bool malo_pci_resume(device_t, const pmf_qual_t *);
64 1.3.4.2 yamt
65 1.3.4.2 yamt struct malo_pci_softc {
66 1.3.4.2 yamt struct malo_softc sc_malo;
67 1.3.4.2 yamt
68 1.3.4.2 yamt pci_chipset_tag_t sc_pc;
69 1.3.4.2 yamt void *sc_ih;
70 1.3.4.2 yamt
71 1.3.4.2 yamt bus_size_t sc_mapsize1;
72 1.3.4.2 yamt bus_size_t sc_mapsize2;
73 1.3.4.2 yamt };
74 1.3.4.2 yamt
75 1.3.4.2 yamt CFATTACH_DECL_NEW(malo_pci, sizeof(struct malo_pci_softc),
76 1.3.4.2 yamt malo_pci_match, malo_pci_attach, malo_pci_detach, NULL);
77 1.3.4.2 yamt
78 1.3.4.2 yamt static int
79 1.3.4.2 yamt malo_pci_match(device_t parent, cfdata_t match, void *aux)
80 1.3.4.2 yamt {
81 1.3.4.2 yamt struct pci_attach_args *pa = aux;
82 1.3.4.2 yamt
83 1.3.4.2 yamt if (PCI_VENDOR(pa->pa_id) != PCI_VENDOR_MARVELL)
84 1.3.4.2 yamt return (0);
85 1.3.4.2 yamt
86 1.3.4.2 yamt switch (PCI_PRODUCT(pa->pa_id)) {
87 1.3.4.2 yamt case PCI_PRODUCT_MARVELL_88W8310:
88 1.3.4.2 yamt case PCI_PRODUCT_MARVELL_88W8335_1:
89 1.3.4.2 yamt case PCI_PRODUCT_MARVELL_88W8335_2:
90 1.3.4.2 yamt return (1);
91 1.3.4.2 yamt }
92 1.3.4.2 yamt
93 1.3.4.2 yamt return (0);
94 1.3.4.2 yamt }
95 1.3.4.2 yamt
96 1.3.4.2 yamt static void
97 1.3.4.2 yamt malo_pci_attach(device_t parent, device_t self, void *aux)
98 1.3.4.2 yamt {
99 1.3.4.2 yamt struct malo_pci_softc *psc = device_private(self);
100 1.3.4.2 yamt struct pci_attach_args *pa = aux;
101 1.3.4.2 yamt struct malo_softc *sc = &psc->sc_malo;
102 1.3.4.2 yamt const char *intrstr = NULL;
103 1.3.4.2 yamt pci_intr_handle_t ih;
104 1.3.4.2 yamt pcireg_t memtype1, memtype2;
105 1.3.4.2 yamt int error;
106 1.3.4.2 yamt
107 1.3.4.2 yamt sc->sc_dev = self;
108 1.3.4.2 yamt sc->sc_dmat = pa->pa_dmat;
109 1.3.4.2 yamt psc->sc_pc = pa->pa_pc;
110 1.3.4.2 yamt
111 1.3.4.2 yamt aprint_normal("\n");
112 1.3.4.2 yamt aprint_normal_dev(self,"Marvell Libertas Wireless\n");
113 1.3.4.2 yamt
114 1.3.4.2 yamt /* map control / status registers */
115 1.3.4.2 yamt memtype1 = pci_mapreg_type(pa->pa_pc, pa->pa_tag, MALO_PCI_BAR1);
116 1.3.4.2 yamt switch (memtype1) {
117 1.3.4.2 yamt case PCI_MAPREG_TYPE_MEM | PCI_MAPREG_MEM_TYPE_32BIT:
118 1.3.4.2 yamt case PCI_MAPREG_TYPE_MEM | PCI_MAPREG_MEM_TYPE_64BIT:
119 1.3.4.2 yamt break;
120 1.3.4.2 yamt default:
121 1.3.4.2 yamt aprint_error_dev(self, "invalid base address register\n");
122 1.3.4.2 yamt return;
123 1.3.4.2 yamt }
124 1.3.4.2 yamt
125 1.3.4.2 yamt error = pci_mapreg_map(pa, MALO_PCI_BAR1,
126 1.3.4.2 yamt memtype1, 0, &sc->sc_mem1_bt, &sc->sc_mem1_bh,
127 1.3.4.2 yamt NULL, &psc->sc_mapsize1);
128 1.3.4.2 yamt if (error != 0) {
129 1.3.4.2 yamt aprint_error_dev(self, "can't map 1st mem space\n");
130 1.3.4.2 yamt return;
131 1.3.4.2 yamt }
132 1.3.4.2 yamt
133 1.3.4.2 yamt /* map control / status registers */
134 1.3.4.2 yamt memtype2 = pci_mapreg_type(pa->pa_pc, pa->pa_tag, MALO_PCI_BAR1);
135 1.3.4.2 yamt switch (memtype2) {
136 1.3.4.2 yamt case PCI_MAPREG_TYPE_MEM | PCI_MAPREG_MEM_TYPE_32BIT:
137 1.3.4.2 yamt case PCI_MAPREG_TYPE_MEM | PCI_MAPREG_MEM_TYPE_64BIT:
138 1.3.4.2 yamt break;
139 1.3.4.2 yamt default:
140 1.3.4.2 yamt aprint_error_dev(self, "invalid base address register\n");
141 1.3.4.2 yamt return;
142 1.3.4.2 yamt }
143 1.3.4.2 yamt
144 1.3.4.2 yamt error = pci_mapreg_map(pa, MALO_PCI_BAR2,
145 1.3.4.2 yamt memtype2, 0, &sc->sc_mem2_bt, &sc->sc_mem2_bh,
146 1.3.4.2 yamt NULL, &psc->sc_mapsize2);
147 1.3.4.2 yamt if (error != 0) {
148 1.3.4.2 yamt aprint_error_dev(self, "can't map 2nd mem space\n");
149 1.3.4.2 yamt return;
150 1.3.4.2 yamt }
151 1.3.4.2 yamt
152 1.3.4.2 yamt /* map interrupt */
153 1.3.4.2 yamt if (pci_intr_map(pa, &ih) != 0) {
154 1.3.4.2 yamt aprint_error_dev(self, "can't map interrupt\n");
155 1.3.4.2 yamt return;
156 1.3.4.2 yamt }
157 1.3.4.2 yamt
158 1.3.4.2 yamt /* establish interrupt */
159 1.3.4.2 yamt intrstr = pci_intr_string(psc->sc_pc, ih);
160 1.3.4.2 yamt psc->sc_ih = pci_intr_establish(psc->sc_pc, ih, IPL_NET, malo_intr, sc);
161 1.3.4.2 yamt if (psc->sc_ih == NULL) {
162 1.3.4.2 yamt aprint_error_dev(self, "could not establish interrupt");
163 1.3.4.2 yamt if (intrstr != NULL)
164 1.3.4.2 yamt aprint_error(" at %s", intrstr);
165 1.3.4.2 yamt aprint_error("\n");
166 1.3.4.2 yamt return;
167 1.3.4.2 yamt }
168 1.3.4.2 yamt aprint_normal_dev(self, "interrupting at %s\n", intrstr);
169 1.3.4.2 yamt
170 1.3.4.2 yamt malo_attach(sc);
171 1.3.4.2 yamt
172 1.3.4.2 yamt if (pmf_device_register(self, malo_pci_suspend, malo_pci_resume))
173 1.3.4.2 yamt pmf_class_network_register(self, &sc->sc_if);
174 1.3.4.2 yamt else
175 1.3.4.2 yamt aprint_error_dev(self, "couldn't establish power handler\n");
176 1.3.4.2 yamt }
177 1.3.4.2 yamt
178 1.3.4.2 yamt int
179 1.3.4.2 yamt malo_pci_detach(device_t self, int flags)
180 1.3.4.2 yamt {
181 1.3.4.2 yamt struct malo_pci_softc *psc = device_private(self);
182 1.3.4.2 yamt struct malo_softc *sc = &psc->sc_malo;
183 1.3.4.2 yamt
184 1.3.4.2 yamt malo_detach(sc);
185 1.3.4.2 yamt pci_intr_disestablish(psc->sc_pc, psc->sc_ih);
186 1.3.4.2 yamt
187 1.3.4.2 yamt return (0);
188 1.3.4.2 yamt }
189 1.3.4.2 yamt
190 1.3.4.2 yamt static bool
191 1.3.4.2 yamt malo_pci_suspend(device_t self, const pmf_qual_t *qual)
192 1.3.4.2 yamt {
193 1.3.4.2 yamt struct malo_pci_softc *psc = device_private(self);
194 1.3.4.2 yamt struct malo_softc *sc = &psc->sc_malo;
195 1.3.4.2 yamt struct ifnet *ifp = &sc->sc_if;
196 1.3.4.2 yamt
197 1.3.4.2 yamt malo_stop(ifp, 1);
198 1.3.4.2 yamt
199 1.3.4.2 yamt return true;
200 1.3.4.2 yamt }
201 1.3.4.2 yamt
202 1.3.4.2 yamt static bool
203 1.3.4.2 yamt malo_pci_resume(device_t self, const pmf_qual_t *qual)
204 1.3.4.2 yamt {
205 1.3.4.2 yamt struct malo_pci_softc *psc = device_private(self);
206 1.3.4.2 yamt struct malo_softc *sc = &psc->sc_malo;
207 1.3.4.2 yamt struct ifnet *ifp = &sc->sc_if;
208 1.3.4.2 yamt
209 1.3.4.2 yamt if (ifp->if_flags & IFF_UP)
210 1.3.4.2 yamt malo_init(ifp);
211 1.3.4.2 yamt
212 1.3.4.2 yamt return true;
213 1.3.4.2 yamt }
214