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