1 /* $OpenBSD: if_malo_cardbus.c,v 1.14 2024/05/24 06:26:47 jsg Exp $ */
2
3 /*
4 * Copyright (c) 2006 Claudio Jeker <claudio@openbsd.org>
5 *
6 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
9 *
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 */
18
19 #include "bpfilter.h"
20
21 #include <sys/param.h>
22 #include <sys/systm.h>
23 #include <sys/timeout.h>
24
25 #include <net/if.h>
26 #include <net/if_media.h>
27
28 #include <netinet/in.h>
29 #include <netinet/if_ether.h>
30
31 #include <net80211/ieee80211_var.h>
32 #include <net80211/ieee80211_radiotap.h>
33
34 #include <dev/pci/pcireg.h>
35 #include <dev/pci/pcivar.h>
36 #include <dev/pci/pcidevs.h>
37
38 #include <dev/cardbus/cardbusvar.h>
39
40 #include <dev/ic/malo.h>
41
42 struct malo_cardbus_softc {
43 struct malo_softc sc_malo;
44
45 /* cardbus specific goo */
46 cardbus_devfunc_t sc_ct;
47 pcitag_t sc_tag;
48 void *sc_ih;
49
50 bus_size_t sc_mapsize1;
51 bus_size_t sc_mapsize2;
52 pcireg_t sc_bar1_val;
53 pcireg_t sc_bar2_val;
54 int sc_intrline;
55 pci_chipset_tag_t sc_pc;
56 };
57
58 int malo_cardbus_match(struct device *parent, void *match, void *aux);
59 void malo_cardbus_attach(struct device *parent, struct device *self,
60 void *aux);
61 int malo_cardbus_detach(struct device *self, int flags);
62 void malo_cardbus_setup(struct malo_cardbus_softc *csc);
63 int malo_cardbus_enable(struct malo_softc *sc);
64 void malo_cardbus_disable(struct malo_softc *sc);
65
66 const struct cfattach malo_cardbus_ca = {
67 sizeof (struct malo_cardbus_softc), malo_cardbus_match,
68 malo_cardbus_attach, malo_cardbus_detach
69 };
70
71 static const struct pci_matchid malo_cardbus_devices[] = {
72 { PCI_VENDOR_MARVELL, PCI_PRODUCT_MARVELL_88W8310 },
73 { PCI_VENDOR_MARVELL, PCI_PRODUCT_MARVELL_88W8335_1 },
74 { PCI_VENDOR_MARVELL, PCI_PRODUCT_MARVELL_88W8335_2 }
75 };
76
77 int
malo_cardbus_match(struct device * parent,void * match,void * aux)78 malo_cardbus_match(struct device *parent, void *match, void *aux)
79 {
80 return (cardbus_matchbyid(aux, malo_cardbus_devices,
81 sizeof (malo_cardbus_devices) / sizeof (malo_cardbus_devices[0])));
82 }
83
84 void
malo_cardbus_attach(struct device * parent,struct device * self,void * aux)85 malo_cardbus_attach(struct device *parent, struct device *self, void *aux)
86 {
87 struct malo_cardbus_softc *csc = (struct malo_cardbus_softc *)self;
88 struct cardbus_attach_args *ca = aux;
89 struct malo_softc *sc = &csc->sc_malo;
90 cardbus_devfunc_t ct = ca->ca_ct;
91 bus_addr_t base;
92 int error;
93
94 sc->sc_dmat = ca->ca_dmat;
95 csc->sc_ct = ct;
96 csc->sc_tag = ca->ca_tag;
97 csc->sc_intrline = ca->ca_intrline;
98 csc->sc_pc = ca->ca_pc;
99
100 /* power management hooks */
101 sc->sc_enable = malo_cardbus_enable;
102 sc->sc_disable = malo_cardbus_disable;
103 #if 0
104 sc->sc_power = malo_cardbus_power;
105 #endif
106
107 /* map control/status registers */
108 error = Cardbus_mapreg_map(ct, CARDBUS_BASE0_REG,
109 PCI_MAPREG_TYPE_MEM, 0, &sc->sc_mem1_bt,
110 &sc->sc_mem1_bh, &base, &csc->sc_mapsize1);
111 if (error != 0) {
112 printf(": can't map mem1 space\n");
113 return;
114 }
115 csc->sc_bar1_val = base | PCI_MAPREG_TYPE_MEM;
116
117 /* map control/status registers */
118 error = Cardbus_mapreg_map(ct, CARDBUS_BASE1_REG,
119 PCI_MAPREG_TYPE_MEM, 0, &sc->sc_mem2_bt,
120 &sc->sc_mem2_bh, &base, &csc->sc_mapsize2);
121 if (error != 0) {
122 printf(": can't map mem2 space\n");
123 Cardbus_mapreg_unmap(ct, CARDBUS_BASE0_REG, sc->sc_mem1_bt,
124 sc->sc_mem1_bh, csc->sc_mapsize1);
125 return;
126 }
127 csc->sc_bar2_val = base | PCI_MAPREG_TYPE_MEM;
128
129 /* set up the PCI configuration registers */
130 malo_cardbus_setup(csc);
131
132 printf(": irq %d", csc->sc_intrline);
133
134 error = malo_attach(sc);
135 if (error != 0)
136 malo_cardbus_detach(&sc->sc_dev, 0);
137
138 Cardbus_function_disable(ct);
139 }
140
141 int
malo_cardbus_detach(struct device * self,int flags)142 malo_cardbus_detach(struct device *self, int flags)
143 {
144 struct malo_cardbus_softc *csc = (struct malo_cardbus_softc *)self;
145 struct malo_softc *sc = &csc->sc_malo;
146 cardbus_devfunc_t ct = csc->sc_ct;
147 cardbus_chipset_tag_t cc = ct->ct_cc;
148 cardbus_function_tag_t cf = ct->ct_cf;
149 int error;
150
151 error = malo_detach(sc);
152 if (error != 0)
153 return (error);
154
155 /* unhook the interrupt handler */
156 if (csc->sc_ih != NULL) {
157 cardbus_intr_disestablish(cc, cf, csc->sc_ih);
158 csc->sc_ih = NULL;
159 }
160
161 /* release bus space and close window */
162 Cardbus_mapreg_unmap(ct, CARDBUS_BASE0_REG, sc->sc_mem1_bt,
163 sc->sc_mem1_bh, csc->sc_mapsize1);
164 Cardbus_mapreg_unmap(ct, CARDBUS_BASE1_REG, sc->sc_mem2_bt,
165 sc->sc_mem2_bh, csc->sc_mapsize2);
166
167 return (0);
168 }
169
170 void
malo_cardbus_setup(struct malo_cardbus_softc * csc)171 malo_cardbus_setup(struct malo_cardbus_softc *csc)
172 {
173 cardbus_devfunc_t ct = csc->sc_ct;
174 cardbus_chipset_tag_t cc = ct->ct_cc;
175 pci_chipset_tag_t pc = csc->sc_pc;
176 cardbus_function_tag_t cf = ct->ct_cf;
177 pcireg_t reg;
178
179 /* program the BAR */
180 pci_conf_write(pc, csc->sc_tag, CARDBUS_BASE0_REG,
181 csc->sc_bar1_val);
182 pci_conf_write(pc, csc->sc_tag, CARDBUS_BASE1_REG,
183 csc->sc_bar2_val);
184
185 /* make sure the right access type is on the cardbus bridge */
186 (*cf->cardbus_ctrl)(cc, CARDBUS_MEM_ENABLE);
187 (*cf->cardbus_ctrl)(cc, CARDBUS_BM_ENABLE);
188
189 /* enable the appropriate bits in the PCI CSR */
190 reg = pci_conf_read(pc, csc->sc_tag,
191 PCI_COMMAND_STATUS_REG);
192 reg |= PCI_COMMAND_MASTER_ENABLE | PCI_COMMAND_MEM_ENABLE;
193 pci_conf_write(pc, csc->sc_tag, PCI_COMMAND_STATUS_REG,
194 reg);
195 }
196
197 int
malo_cardbus_enable(struct malo_softc * sc)198 malo_cardbus_enable(struct malo_softc *sc)
199 {
200 struct malo_cardbus_softc *csc = (struct malo_cardbus_softc *)sc;
201 cardbus_devfunc_t ct = csc->sc_ct;
202 cardbus_chipset_tag_t cc = ct->ct_cc;
203 cardbus_function_tag_t cf = ct->ct_cf;
204
205 /* power on the socket */
206 Cardbus_function_enable(ct);
207
208 /* setup the PCI configuration registers */
209 malo_cardbus_setup(csc);
210
211 /* map and establish the interrupt handler */
212 csc->sc_ih = cardbus_intr_establish(cc, cf, csc->sc_intrline, IPL_NET,
213 malo_intr, sc, sc->sc_dev.dv_xname);
214 if (csc->sc_ih == NULL) {
215 printf("%s: could not establish interrupt at %d\n",
216 sc->sc_dev.dv_xname, csc->sc_intrline);
217 Cardbus_function_disable(ct);
218 return (1);
219 }
220
221 return (0);
222 }
223
224 void
malo_cardbus_disable(struct malo_softc * sc)225 malo_cardbus_disable(struct malo_softc *sc)
226 {
227 struct malo_cardbus_softc *csc = (struct malo_cardbus_softc *)sc;
228 cardbus_devfunc_t ct = csc->sc_ct;
229 cardbus_chipset_tag_t cc = ct->ct_cc;
230 cardbus_function_tag_t cf = ct->ct_cf;
231
232 /* unhook the interrupt handler */
233 cardbus_intr_disestablish(cc, cf, csc->sc_ih);
234 csc->sc_ih = NULL;
235
236 /* power down the socket */
237 Cardbus_function_disable(ct);
238 }
239