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