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