if_malo_pci.c revision 1.4 1 /* $NetBSD: if_malo_pci.c,v 1.4 2013/03/30 03:21:06 christos 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 (at) 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.4 2013/03/30 03:21:06 christos 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/malloc.h>
34 #include <sys/device.h>
35 #include <sys/bus.h>
36
37 #include <machine/intr.h>
38
39 #include <net/if.h>
40 #include <net/if_dl.h>
41 #include <net/if_media.h>
42 #include <net/if_ether.h>
43
44 #include <netinet/in.h>
45
46 #include <net80211/ieee80211_var.h>
47 #include <net80211/ieee80211_radiotap.h>
48
49 #include <dev/ic/malovar.h>
50
51 #include <dev/pci/pcireg.h>
52 #include <dev/pci/pcivar.h>
53 #include <dev/pci/pcidevs.h>
54
55 /* Base Address Register */
56 #define MALO_PCI_BAR1 0x10
57 #define MALO_PCI_BAR2 0x14
58
59 static int malo_pci_match(device_t parent, cfdata_t match, void *aux);
60 static void malo_pci_attach(device_t, device_t, void *);
61 static int malo_pci_detach(device_t, int);
62 static bool malo_pci_suspend(device_t, const pmf_qual_t *);
63 static bool malo_pci_resume(device_t, const pmf_qual_t *);
64
65 struct malo_pci_softc {
66 struct malo_softc sc_malo;
67
68 pci_chipset_tag_t sc_pc;
69 void *sc_ih;
70
71 bus_size_t sc_mapsize1;
72 bus_size_t sc_mapsize2;
73 };
74
75 CFATTACH_DECL_NEW(malo_pci, sizeof(struct malo_pci_softc),
76 malo_pci_match, malo_pci_attach, malo_pci_detach, NULL);
77
78 static int
79 malo_pci_match(device_t parent, cfdata_t match, void *aux)
80 {
81 struct pci_attach_args *pa = aux;
82
83 if (PCI_VENDOR(pa->pa_id) != PCI_VENDOR_MARVELL)
84 return (0);
85
86 switch (PCI_PRODUCT(pa->pa_id)) {
87 case PCI_PRODUCT_MARVELL_88W8310:
88 case PCI_PRODUCT_MARVELL_88W8335_1:
89 case PCI_PRODUCT_MARVELL_88W8335_2:
90 return (1);
91 }
92
93 return (0);
94 }
95
96 static void
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
107 sc->sc_dev = self;
108 sc->sc_dmat = pa->pa_dmat;
109 psc->sc_pc = pa->pa_pc;
110
111 aprint_normal("\n");
112 aprint_normal_dev(self,"Marvell Libertas Wireless\n");
113
114 /* map control / status registers */
115 memtype1 = pci_mapreg_type(pa->pa_pc, pa->pa_tag, MALO_PCI_BAR1);
116 switch (memtype1) {
117 case PCI_MAPREG_TYPE_MEM | PCI_MAPREG_MEM_TYPE_32BIT:
118 case PCI_MAPREG_TYPE_MEM | PCI_MAPREG_MEM_TYPE_64BIT:
119 break;
120 default:
121 aprint_error_dev(self, "invalid base address register\n");
122 return;
123 }
124
125 error = pci_mapreg_map(pa, MALO_PCI_BAR1,
126 memtype1, 0, &sc->sc_mem1_bt, &sc->sc_mem1_bh,
127 NULL, &psc->sc_mapsize1);
128 if (error != 0) {
129 aprint_error_dev(self, "can't map 1st mem space\n");
130 return;
131 }
132
133 /* map control / status registers */
134 memtype2 = pci_mapreg_type(pa->pa_pc, pa->pa_tag, MALO_PCI_BAR1);
135 switch (memtype2) {
136 case PCI_MAPREG_TYPE_MEM | PCI_MAPREG_MEM_TYPE_32BIT:
137 case PCI_MAPREG_TYPE_MEM | PCI_MAPREG_MEM_TYPE_64BIT:
138 break;
139 default:
140 aprint_error_dev(self, "invalid base address register\n");
141 return;
142 }
143
144 error = pci_mapreg_map(pa, MALO_PCI_BAR2,
145 memtype2, 0, &sc->sc_mem2_bt, &sc->sc_mem2_bh,
146 NULL, &psc->sc_mapsize2);
147 if (error != 0) {
148 aprint_error_dev(self, "can't map 2nd mem space\n");
149 return;
150 }
151
152 /* map interrupt */
153 if (pci_intr_map(pa, &ih) != 0) {
154 aprint_error_dev(self, "can't map interrupt\n");
155 return;
156 }
157
158 /* establish interrupt */
159 intrstr = pci_intr_string(psc->sc_pc, ih);
160 psc->sc_ih = pci_intr_establish(psc->sc_pc, ih, IPL_NET, malo_intr, sc);
161 if (psc->sc_ih == NULL) {
162 aprint_error_dev(self, "could not establish interrupt");
163 if (intrstr != NULL)
164 aprint_error(" at %s", intrstr);
165 aprint_error("\n");
166 return;
167 }
168 aprint_normal_dev(self, "interrupting at %s\n", intrstr);
169
170 malo_attach(sc);
171
172 if (pmf_device_register(self, malo_pci_suspend, malo_pci_resume))
173 pmf_class_network_register(self, &sc->sc_if);
174 else
175 aprint_error_dev(self, "couldn't establish power handler\n");
176 }
177
178 int
179 malo_pci_detach(device_t self, int flags)
180 {
181 struct malo_pci_softc *psc = device_private(self);
182 struct malo_softc *sc = &psc->sc_malo;
183
184 malo_detach(sc);
185 pci_intr_disestablish(psc->sc_pc, psc->sc_ih);
186
187 return (0);
188 }
189
190 static bool
191 malo_pci_suspend(device_t self, const pmf_qual_t *qual)
192 {
193 struct malo_pci_softc *psc = device_private(self);
194 struct malo_softc *sc = &psc->sc_malo;
195 struct ifnet *ifp = &sc->sc_if;
196
197 malo_stop(ifp, 1);
198
199 return true;
200 }
201
202 static bool
203 malo_pci_resume(device_t self, const pmf_qual_t *qual)
204 {
205 struct malo_pci_softc *psc = device_private(self);
206 struct malo_softc *sc = &psc->sc_malo;
207 struct ifnet *ifp = &sc->sc_if;
208
209 if (ifp->if_flags & IFF_UP)
210 malo_init(ifp);
211
212 return true;
213 }
214