if_mbe_pcmcia.c revision 1.46 1 1.46 cegger /* $NetBSD: if_mbe_pcmcia.c,v 1.46 2009/05/12 14:42:18 cegger Exp $ */
2 1.1 enami
3 1.1 enami /*-
4 1.32 mycroft * Copyright (c) 1998, 2000, 2004 The NetBSD Foundation, Inc.
5 1.1 enami * All rights reserved.
6 1.1 enami *
7 1.1 enami * This code is derived from software contributed to The NetBSD Foundation
8 1.1 enami * by Enami Tsugutomo.
9 1.1 enami *
10 1.1 enami * Redistribution and use in source and binary forms, with or without
11 1.1 enami * modification, are permitted provided that the following conditions
12 1.1 enami * are met:
13 1.1 enami * 1. Redistributions of source code must retain the above copyright
14 1.1 enami * notice, this list of conditions and the following disclaimer.
15 1.1 enami * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 enami * notice, this list of conditions and the following disclaimer in the
17 1.1 enami * documentation and/or other materials provided with the distribution.
18 1.1 enami *
19 1.1 enami * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 1.1 enami * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.1 enami * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.1 enami * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 1.1 enami * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 1.1 enami * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 1.1 enami * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.1 enami * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.1 enami * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.1 enami * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.1 enami * POSSIBILITY OF SUCH DAMAGE.
30 1.1 enami */
31 1.21 lukem
32 1.21 lukem #include <sys/cdefs.h>
33 1.46 cegger __KERNEL_RCSID(0, "$NetBSD: if_mbe_pcmcia.c,v 1.46 2009/05/12 14:42:18 cegger Exp $");
34 1.1 enami
35 1.1 enami #include <sys/param.h>
36 1.1 enami #include <sys/systm.h>
37 1.1 enami #include <sys/device.h>
38 1.1 enami #include <sys/socket.h>
39 1.1 enami
40 1.1 enami #include <net/if.h>
41 1.1 enami #include <net/if_ether.h>
42 1.1 enami #include <net/if_media.h>
43 1.1 enami
44 1.42 ad #include <sys/intr.h>
45 1.42 ad #include <sys/bus.h>
46 1.1 enami
47 1.1 enami #include <dev/ic/mb86960reg.h>
48 1.1 enami #include <dev/ic/mb86960var.h>
49 1.1 enami
50 1.1 enami #include <dev/pcmcia/pcmciareg.h>
51 1.1 enami #include <dev/pcmcia/pcmciavar.h>
52 1.3 christos #include <dev/pcmcia/pcmciadevs.h>
53 1.1 enami
54 1.44 tsutsui int mbe_pcmcia_match(device_t, cfdata_t, void *);
55 1.36 perry int mbe_pcmcia_validate_config(struct pcmcia_config_entry *);
56 1.44 tsutsui void mbe_pcmcia_attach(device_t, device_t, void *);
57 1.44 tsutsui int mbe_pcmcia_detach(device_t, int);
58 1.1 enami
59 1.1 enami struct mbe_pcmcia_softc {
60 1.1 enami struct mb86960_softc sc_mb86960; /* real "mb" softc */
61 1.1 enami
62 1.34 mycroft struct pcmcia_function *sc_pf; /* our PCMCIA function */
63 1.1 enami void *sc_ih; /* interrupt cookie */
64 1.34 mycroft
65 1.34 mycroft int sc_state;
66 1.34 mycroft #define MBE_PCMCIA_ATTACHED 3
67 1.1 enami };
68 1.1 enami
69 1.44 tsutsui CFATTACH_DECL_NEW(mbe_pcmcia, sizeof(struct mbe_pcmcia_softc),
70 1.29 thorpej mbe_pcmcia_match, mbe_pcmcia_attach, mbe_pcmcia_detach, mb86960_activate);
71 1.1 enami
72 1.36 perry int mbe_pcmcia_enable(struct mb86960_softc *);
73 1.36 perry void mbe_pcmcia_disable(struct mb86960_softc *);
74 1.1 enami
75 1.1 enami struct mbe_pcmcia_get_enaddr_args {
76 1.44 tsutsui uint8_t enaddr[ETHER_ADDR_LEN];
77 1.18 is int maddr;
78 1.1 enami };
79 1.36 perry int mbe_pcmcia_get_enaddr_from_cis(struct pcmcia_tuple *, void *);
80 1.36 perry int mbe_pcmcia_get_enaddr_from_mem(struct mbe_pcmcia_softc *,
81 1.36 perry struct mbe_pcmcia_get_enaddr_args *);
82 1.36 perry int mbe_pcmcia_get_enaddr_from_io(struct mbe_pcmcia_softc *,
83 1.36 perry struct mbe_pcmcia_get_enaddr_args *);
84 1.1 enami
85 1.24 ichiro static const struct mbe_pcmcia_product {
86 1.32 mycroft struct pcmcia_product mpp_product;
87 1.24 ichiro int mpp_enet_maddr;
88 1.32 mycroft int mpp_flags;
89 1.25 ichiro #define MBH10302 0x0001 /* FUJITSU MBH10302 */
90 1.7 thorpej } mbe_pcmcia_products[] = {
91 1.32 mycroft { { PCMCIA_VENDOR_TDK, PCMCIA_PRODUCT_TDK_LAK_CD021BX,
92 1.32 mycroft PCMCIA_CIS_TDK_LAK_CD021BX },
93 1.39 christos -1, 0 },
94 1.13 cgd
95 1.32 mycroft { { PCMCIA_VENDOR_TDK, PCMCIA_PRODUCT_TDK_LAK_CF010,
96 1.32 mycroft PCMCIA_CIS_TDK_LAK_CF010 },
97 1.39 christos -1, 0 },
98 1.19 enami
99 1.7 thorpej #if 0 /* XXX 86960-based? */
100 1.32 mycroft { { PCMCIA_VENDOR_TDK, PCMCIA_PRODUCT_TDK_LAK_DFL9610,
101 1.32 mycroft PCMCIA_CIS_TDK_DFL9610 },
102 1.34 mycroft -1, MBH10302 /* XXX */ },
103 1.7 thorpej #endif
104 1.8 tron
105 1.32 mycroft { { PCMCIA_VENDOR_CONTEC, PCMCIA_PRODUCT_CONTEC_CNETPC,
106 1.32 mycroft PCMCIA_CIS_CONTEC_CNETPC },
107 1.39 christos -1, 0 },
108 1.13 cgd
109 1.32 mycroft { { PCMCIA_VENDOR_FUJITSU, PCMCIA_PRODUCT_FUJITSU_LA501,
110 1.32 mycroft PCMCIA_CIS_FUJITSU_LA501 },
111 1.39 christos -1, 0 },
112 1.13 cgd
113 1.32 mycroft { { PCMCIA_VENDOR_FUJITSU, PCMCIA_PRODUCT_FUJITSU_FMV_J181,
114 1.32 mycroft PCMCIA_CIS_FUJITSU_FMV_J181 },
115 1.34 mycroft -1, MBH10302 },
116 1.25 ichiro
117 1.32 mycroft { { PCMCIA_VENDOR_FUJITSU, PCMCIA_PRODUCT_FUJITSU_FMV_J182,
118 1.32 mycroft PCMCIA_CIS_FUJITSU_FMV_J182 },
119 1.39 christos 0xf2c, 0 },
120 1.25 ichiro
121 1.32 mycroft { { PCMCIA_VENDOR_FUJITSU, PCMCIA_PRODUCT_FUJITSU_FMV_J182A,
122 1.32 mycroft PCMCIA_CIS_FUJITSU_FMV_J182A },
123 1.39 christos 0x1cc, 0 },
124 1.23 ichiro
125 1.32 mycroft { { PCMCIA_VENDOR_FUJITSU, PCMCIA_PRODUCT_FUJITSU_ITCFJ182A,
126 1.32 mycroft PCMCIA_CIS_FUJITSU_ITCFJ182A },
127 1.39 christos 0x1cc, 0 },
128 1.18 is
129 1.32 mycroft { { PCMCIA_VENDOR_FUJITSU, PCMCIA_PRODUCT_FUJITSU_LA10S,
130 1.32 mycroft PCMCIA_CIS_FUJITSU_LA10S },
131 1.39 christos -1, 0 },
132 1.25 ichiro
133 1.32 mycroft { { PCMCIA_VENDOR_RATOC, PCMCIA_PRODUCT_RATOC_REX_R280,
134 1.32 mycroft PCMCIA_CIS_RATOC_REX_R280 },
135 1.39 christos 0x1fc, 0 },
136 1.7 thorpej };
137 1.44 tsutsui static const size_t mbe_pcmcia_nproducts = __arraycount(mbe_pcmcia_products);
138 1.24 ichiro
139 1.1 enami int
140 1.44 tsutsui mbe_pcmcia_match(device_t parent, cfdata_t cf, void *aux)
141 1.1 enami {
142 1.1 enami struct pcmcia_attach_args *pa = aux;
143 1.1 enami
144 1.32 mycroft if (pcmcia_product_lookup(pa, mbe_pcmcia_products, mbe_pcmcia_nproducts,
145 1.32 mycroft sizeof(mbe_pcmcia_products[0]), NULL))
146 1.44 tsutsui return 1;
147 1.44 tsutsui return 0;
148 1.1 enami }
149 1.1 enami
150 1.34 mycroft int
151 1.44 tsutsui mbe_pcmcia_validate_config(struct pcmcia_config_entry *cfe)
152 1.34 mycroft {
153 1.44 tsutsui
154 1.34 mycroft if (cfe->iftype != PCMCIA_IFTYPE_IO ||
155 1.34 mycroft cfe->num_iospace < 1)
156 1.44 tsutsui return EINVAL;
157 1.44 tsutsui return 0;
158 1.34 mycroft }
159 1.34 mycroft
160 1.1 enami void
161 1.44 tsutsui mbe_pcmcia_attach(device_t parent, device_t self, void *aux)
162 1.1 enami {
163 1.44 tsutsui struct mbe_pcmcia_softc *psc = device_private(self);
164 1.1 enami struct mb86960_softc *sc = &psc->sc_mb86960;
165 1.1 enami struct pcmcia_attach_args *pa = aux;
166 1.1 enami struct pcmcia_config_entry *cfe;
167 1.1 enami struct mbe_pcmcia_get_enaddr_args pgea;
168 1.7 thorpej const struct mbe_pcmcia_product *mpp;
169 1.34 mycroft int error;
170 1.1 enami
171 1.44 tsutsui sc->sc_dev = self;
172 1.1 enami psc->sc_pf = pa->pf;
173 1.1 enami
174 1.34 mycroft error = pcmcia_function_configure(pa->pf, mbe_pcmcia_validate_config);
175 1.34 mycroft if (error) {
176 1.43 cegger aprint_error_dev(self, "configure failed, error=%d\n",
177 1.34 mycroft error);
178 1.34 mycroft return;
179 1.1 enami }
180 1.1 enami
181 1.34 mycroft cfe = pa->pf->cfe;
182 1.34 mycroft sc->sc_bst = cfe->iospace[0].handle.iot;
183 1.34 mycroft sc->sc_bsh = cfe->iospace[0].handle.ioh;
184 1.34 mycroft
185 1.34 mycroft mpp = pcmcia_product_lookup(pa, mbe_pcmcia_products,
186 1.34 mycroft mbe_pcmcia_nproducts, sizeof(mbe_pcmcia_products[0]), NULL);
187 1.34 mycroft if (!mpp)
188 1.44 tsutsui panic("%s: impossible", __func__);
189 1.1 enami
190 1.25 ichiro /* Read station address from io/mem or CIS. */
191 1.19 enami if (mpp->mpp_enet_maddr >= 0) {
192 1.19 enami pgea.maddr = mpp->mpp_enet_maddr;
193 1.18 is if (mbe_pcmcia_get_enaddr_from_mem(psc, &pgea) != 0) {
194 1.43 cegger aprint_error_dev(self, "couldn't get ethernet address "
195 1.43 cegger "from memory\n");
196 1.35 mycroft goto fail;
197 1.18 is }
198 1.34 mycroft } else if (mpp->mpp_flags & MBH10302) {
199 1.25 ichiro bus_space_write_1(sc->sc_bst, sc->sc_bsh, FE_MBH0 ,
200 1.25 ichiro FE_MBH0_MASK | FE_MBH0_INTR_ENABLE);
201 1.25 ichiro if (mbe_pcmcia_get_enaddr_from_io(psc, &pgea) != 0) {
202 1.44 tsutsui aprint_error_dev(self,
203 1.44 tsutsui "couldn't get ethernet address from i/o\n");
204 1.35 mycroft goto fail;
205 1.25 ichiro }
206 1.18 is } else {
207 1.34 mycroft if (pa->pf->pf_funce_lan_nidlen != ETHER_ADDR_LEN) {
208 1.44 tsutsui aprint_error_dev(self,
209 1.44 tsutsui "couldn't get ethernet address from CIS\n");
210 1.35 mycroft goto fail;
211 1.18 is }
212 1.34 mycroft memcpy(pgea.enaddr, pa->pf->pf_funce_lan_nid, ETHER_ADDR_LEN);
213 1.10 enami }
214 1.1 enami
215 1.1 enami /* Perform generic initialization. */
216 1.34 mycroft if (mpp->mpp_flags & MBH10302)
217 1.30 tsutsui sc->sc_flags |= FE_FLAGS_MB86960;
218 1.30 tsutsui
219 1.35 mycroft sc->sc_enable = mbe_pcmcia_enable;
220 1.35 mycroft sc->sc_disable = mbe_pcmcia_disable;
221 1.35 mycroft
222 1.35 mycroft error = mbe_pcmcia_enable(sc);
223 1.35 mycroft if (error)
224 1.35 mycroft goto fail;
225 1.35 mycroft
226 1.30 tsutsui mb86960_attach(sc, pgea.enaddr);
227 1.1 enami mb86960_config(sc, NULL, 0, 0);
228 1.1 enami
229 1.34 mycroft mbe_pcmcia_disable(sc);
230 1.34 mycroft psc->sc_state = MBE_PCMCIA_ATTACHED;
231 1.14 enami return;
232 1.14 enami
233 1.34 mycroft fail:
234 1.34 mycroft pcmcia_function_unconfigure(pa->pf);
235 1.4 thorpej }
236 1.4 thorpej
237 1.4 thorpej int
238 1.46 cegger mbe_pcmcia_detach(device_t self, int flags)
239 1.4 thorpej {
240 1.44 tsutsui struct mbe_pcmcia_softc *psc = device_private(self);
241 1.12 enami int error;
242 1.12 enami
243 1.34 mycroft if (psc->sc_state != MBE_PCMCIA_ATTACHED)
244 1.44 tsutsui return 0;
245 1.14 enami
246 1.14 enami error = mb86960_detach(&psc->sc_mb86960);
247 1.34 mycroft if (error)
248 1.44 tsutsui return error;
249 1.5 thorpej
250 1.34 mycroft pcmcia_function_unconfigure(psc->sc_pf);
251 1.5 thorpej
252 1.44 tsutsui return 0;
253 1.1 enami }
254 1.1 enami
255 1.1 enami int
256 1.44 tsutsui mbe_pcmcia_enable(struct mb86960_softc *sc)
257 1.1 enami {
258 1.44 tsutsui struct mbe_pcmcia_softc *psc = (struct mbe_pcmcia_softc *)sc;
259 1.34 mycroft int error;
260 1.1 enami
261 1.1 enami /* Establish the interrupt handler. */
262 1.1 enami psc->sc_ih = pcmcia_intr_establish(psc->sc_pf, IPL_NET, mb86960_intr,
263 1.1 enami sc);
264 1.34 mycroft if (!psc->sc_ih)
265 1.44 tsutsui return EIO;
266 1.1 enami
267 1.34 mycroft error = pcmcia_function_enable(psc->sc_pf);
268 1.34 mycroft if (error) {
269 1.17 enami pcmcia_intr_disestablish(psc->sc_pf, psc->sc_ih);
270 1.34 mycroft psc->sc_ih = 0;
271 1.17 enami }
272 1.16 enami
273 1.44 tsutsui return error;
274 1.1 enami }
275 1.1 enami
276 1.1 enami void
277 1.44 tsutsui mbe_pcmcia_disable(struct mb86960_softc *sc)
278 1.1 enami {
279 1.44 tsutsui struct mbe_pcmcia_softc *psc = (struct mbe_pcmcia_softc *)sc;
280 1.1 enami
281 1.1 enami pcmcia_function_disable(psc->sc_pf);
282 1.17 enami pcmcia_intr_disestablish(psc->sc_pf, psc->sc_ih);
283 1.34 mycroft psc->sc_ih = 0;
284 1.25 ichiro }
285 1.25 ichiro
286 1.25 ichiro int
287 1.44 tsutsui mbe_pcmcia_get_enaddr_from_io(struct mbe_pcmcia_softc *psc,
288 1.44 tsutsui struct mbe_pcmcia_get_enaddr_args *ea)
289 1.37 perry {
290 1.34 mycroft struct mb86960_softc *sc = &psc->sc_mb86960;
291 1.25 ichiro int i;
292 1.25 ichiro
293 1.25 ichiro for (i = 0; i < ETHER_ADDR_LEN; i++)
294 1.34 mycroft ea->enaddr[i] = bus_space_read_1(sc->sc_bst, sc->sc_bsh,
295 1.34 mycroft FE_MBH_ENADDR + i);
296 1.44 tsutsui return 0;
297 1.18 is }
298 1.18 is
299 1.18 is int
300 1.44 tsutsui mbe_pcmcia_get_enaddr_from_mem(struct mbe_pcmcia_softc *psc,
301 1.44 tsutsui struct mbe_pcmcia_get_enaddr_args *ea)
302 1.18 is {
303 1.18 is struct mb86960_softc *sc = &psc->sc_mb86960;
304 1.18 is struct pcmcia_mem_handle pcmh;
305 1.22 soren bus_size_t offset;
306 1.20 enami int i, mwindow, rv = 1;
307 1.18 is
308 1.18 is if (ea->maddr < 0)
309 1.20 enami goto bad_memaddr;
310 1.18 is
311 1.18 is if (pcmcia_mem_alloc(psc->sc_pf, ETHER_ADDR_LEN * 2, &pcmh)) {
312 1.44 tsutsui aprint_error_dev(sc->sc_dev, "can't alloc mem for enet addr\n");
313 1.20 enami goto memalloc_failed;
314 1.18 is }
315 1.18 is
316 1.18 is if (pcmcia_mem_map(psc->sc_pf, PCMCIA_MEM_ATTR, ea->maddr,
317 1.19 enami ETHER_ADDR_LEN * 2, &pcmh, &offset, &mwindow)) {
318 1.44 tsutsui aprint_error_dev(sc->sc_dev, "can't map mem for enet addr\n");
319 1.20 enami goto memmap_failed;
320 1.18 is }
321 1.18 is
322 1.18 is for (i = 0; i < ETHER_ADDR_LEN; i++)
323 1.18 is ea->enaddr[i] = bus_space_read_1(pcmh.memt, pcmh.memh,
324 1.19 enami offset + (i * 2));
325 1.18 is
326 1.20 enami rv = 0;
327 1.18 is pcmcia_mem_unmap(psc->sc_pf, mwindow);
328 1.20 enami memmap_failed:
329 1.18 is pcmcia_mem_free(psc->sc_pf, &pcmh);
330 1.20 enami memalloc_failed:
331 1.20 enami bad_memaddr:
332 1.18 is
333 1.44 tsutsui return rv;
334 1.1 enami }
335