if_mbe_pcmcia.c revision 1.43 1 /* $NetBSD: if_mbe_pcmcia.c,v 1.43 2008/04/05 21:31:23 cegger 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.43 2008/04/05 21:31:23 cegger 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 <sys/intr.h>
52 #include <sys/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(struct device *, struct cfdata *, void *);
62 int mbe_pcmcia_validate_config(struct pcmcia_config_entry *);
63 void mbe_pcmcia_attach(struct device *, struct device *, void *);
64 int mbe_pcmcia_detach(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(struct mb86960_softc *);
80 void mbe_pcmcia_disable(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(struct pcmcia_tuple *, void *);
87 int mbe_pcmcia_get_enaddr_from_mem(struct mbe_pcmcia_softc *,
88 struct mbe_pcmcia_get_enaddr_args *);
89 int mbe_pcmcia_get_enaddr_from_io(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, 0 },
101
102 { { PCMCIA_VENDOR_TDK, PCMCIA_PRODUCT_TDK_LAK_CF010,
103 PCMCIA_CIS_TDK_LAK_CF010 },
104 -1, 0 },
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, 0 },
115
116 { { PCMCIA_VENDOR_FUJITSU, PCMCIA_PRODUCT_FUJITSU_LA501,
117 PCMCIA_CIS_FUJITSU_LA501 },
118 -1, 0 },
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, 0 },
127
128 { { PCMCIA_VENDOR_FUJITSU, PCMCIA_PRODUCT_FUJITSU_FMV_J182A,
129 PCMCIA_CIS_FUJITSU_FMV_J182A },
130 0x1cc, 0 },
131
132 { { PCMCIA_VENDOR_FUJITSU, PCMCIA_PRODUCT_FUJITSU_ITCFJ182A,
133 PCMCIA_CIS_FUJITSU_ITCFJ182A },
134 0x1cc, 0 },
135
136 { { PCMCIA_VENDOR_FUJITSU, PCMCIA_PRODUCT_FUJITSU_LA10S,
137 PCMCIA_CIS_FUJITSU_LA10S },
138 -1, 0 },
139
140 { { PCMCIA_VENDOR_RATOC, PCMCIA_PRODUCT_RATOC_REX_R280,
141 PCMCIA_CIS_RATOC_REX_R280 },
142 0x1fc, 0 },
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(struct device *parent, struct cfdata *match,
149 void *aux)
150 {
151 struct pcmcia_attach_args *pa = aux;
152
153 if (pcmcia_product_lookup(pa, mbe_pcmcia_products, mbe_pcmcia_nproducts,
154 sizeof(mbe_pcmcia_products[0]), NULL))
155 return (1);
156 return (0);
157 }
158
159 int
160 mbe_pcmcia_validate_config(cfe)
161 struct pcmcia_config_entry *cfe;
162 {
163 if (cfe->iftype != PCMCIA_IFTYPE_IO ||
164 cfe->num_iospace < 1)
165 return (EINVAL);
166 return (0);
167 }
168
169 void
170 mbe_pcmcia_attach(struct device *parent, struct device *self,
171 void *aux)
172 {
173 struct mbe_pcmcia_softc *psc = (void *)self;
174 struct mb86960_softc *sc = &psc->sc_mb86960;
175 struct pcmcia_attach_args *pa = aux;
176 struct pcmcia_config_entry *cfe;
177 struct mbe_pcmcia_get_enaddr_args pgea;
178 const struct mbe_pcmcia_product *mpp;
179 int error;
180
181 psc->sc_pf = pa->pf;
182
183 error = pcmcia_function_configure(pa->pf, mbe_pcmcia_validate_config);
184 if (error) {
185 aprint_error_dev(self, "configure failed, error=%d\n",
186 error);
187 return;
188 }
189
190 cfe = pa->pf->cfe;
191 sc->sc_bst = cfe->iospace[0].handle.iot;
192 sc->sc_bsh = cfe->iospace[0].handle.ioh;
193
194 mpp = pcmcia_product_lookup(pa, mbe_pcmcia_products,
195 mbe_pcmcia_nproducts, sizeof(mbe_pcmcia_products[0]), NULL);
196 if (!mpp)
197 panic("mbe_pcmcia_attach: impossible");
198
199 /* Read station address from io/mem or CIS. */
200 if (mpp->mpp_enet_maddr >= 0) {
201 pgea.maddr = mpp->mpp_enet_maddr;
202 if (mbe_pcmcia_get_enaddr_from_mem(psc, &pgea) != 0) {
203 aprint_error_dev(self, "couldn't get ethernet address "
204 "from memory\n");
205 goto fail;
206 }
207 } else if (mpp->mpp_flags & MBH10302) {
208 bus_space_write_1(sc->sc_bst, sc->sc_bsh, FE_MBH0 ,
209 FE_MBH0_MASK | FE_MBH0_INTR_ENABLE);
210 if (mbe_pcmcia_get_enaddr_from_io(psc, &pgea) != 0) {
211 aprint_error_dev(self, "couldn't get ethernet address from i/o\n");
212 goto fail;
213 }
214 } else {
215 if (pa->pf->pf_funce_lan_nidlen != ETHER_ADDR_LEN) {
216 aprint_error_dev(self, "couldn't get ethernet address from CIS\n");
217 goto fail;
218 }
219 memcpy(pgea.enaddr, pa->pf->pf_funce_lan_nid, ETHER_ADDR_LEN);
220 }
221
222 /* Perform generic initialization. */
223 if (mpp->mpp_flags & MBH10302)
224 sc->sc_flags |= FE_FLAGS_MB86960;
225
226 sc->sc_enable = mbe_pcmcia_enable;
227 sc->sc_disable = mbe_pcmcia_disable;
228
229 error = mbe_pcmcia_enable(sc);
230 if (error)
231 goto fail;
232
233 mb86960_attach(sc, pgea.enaddr);
234 mb86960_config(sc, NULL, 0, 0);
235
236 mbe_pcmcia_disable(sc);
237 psc->sc_state = MBE_PCMCIA_ATTACHED;
238 return;
239
240 fail:
241 pcmcia_function_unconfigure(pa->pf);
242 }
243
244 int
245 mbe_pcmcia_detach(struct device *self, int flags)
246 {
247 struct mbe_pcmcia_softc *psc = (void *)self;
248 int error;
249
250 if (psc->sc_state != MBE_PCMCIA_ATTACHED)
251 return (0);
252
253 error = mb86960_detach(&psc->sc_mb86960);
254 if (error)
255 return (error);
256
257 pcmcia_function_unconfigure(psc->sc_pf);
258
259 return (0);
260 }
261
262 int
263 mbe_pcmcia_enable(sc)
264 struct mb86960_softc *sc;
265 {
266 struct mbe_pcmcia_softc *psc = (void *)sc;
267 int error;
268
269 /* Establish the interrupt handler. */
270 psc->sc_ih = pcmcia_intr_establish(psc->sc_pf, IPL_NET, mb86960_intr,
271 sc);
272 if (!psc->sc_ih)
273 return (EIO);
274
275 error = pcmcia_function_enable(psc->sc_pf);
276 if (error) {
277 pcmcia_intr_disestablish(psc->sc_pf, psc->sc_ih);
278 psc->sc_ih = 0;
279 }
280
281 return (error);
282 }
283
284 void
285 mbe_pcmcia_disable(sc)
286 struct mb86960_softc *sc;
287 {
288 struct mbe_pcmcia_softc *psc = (void *)sc;
289
290 pcmcia_function_disable(psc->sc_pf);
291 pcmcia_intr_disestablish(psc->sc_pf, psc->sc_ih);
292 psc->sc_ih = 0;
293 }
294
295 int
296 mbe_pcmcia_get_enaddr_from_io(psc, ea)
297 struct mbe_pcmcia_softc *psc;
298 struct mbe_pcmcia_get_enaddr_args *ea;
299 {
300 struct mb86960_softc *sc = &psc->sc_mb86960;
301 int i;
302
303 for (i = 0; i < ETHER_ADDR_LEN; i++)
304 ea->enaddr[i] = bus_space_read_1(sc->sc_bst, sc->sc_bsh,
305 FE_MBH_ENADDR + i);
306 return (0);
307 }
308
309 int
310 mbe_pcmcia_get_enaddr_from_mem(psc, ea)
311 struct mbe_pcmcia_softc *psc;
312 struct mbe_pcmcia_get_enaddr_args *ea;
313 {
314 struct mb86960_softc *sc = &psc->sc_mb86960;
315 struct pcmcia_mem_handle pcmh;
316 bus_size_t offset;
317 int i, mwindow, rv = 1;
318
319 if (ea->maddr < 0)
320 goto bad_memaddr;
321
322 if (pcmcia_mem_alloc(psc->sc_pf, ETHER_ADDR_LEN * 2, &pcmh)) {
323 aprint_error_dev(&sc->sc_dev, "can't alloc mem for enet addr\n");
324 goto memalloc_failed;
325 }
326
327 if (pcmcia_mem_map(psc->sc_pf, PCMCIA_MEM_ATTR, ea->maddr,
328 ETHER_ADDR_LEN * 2, &pcmh, &offset, &mwindow)) {
329 aprint_error_dev(&sc->sc_dev, "can't map mem for enet addr\n");
330 goto memmap_failed;
331 }
332
333 for (i = 0; i < ETHER_ADDR_LEN; i++)
334 ea->enaddr[i] = bus_space_read_1(pcmh.memt, pcmh.memh,
335 offset + (i * 2));
336
337 rv = 0;
338 pcmcia_mem_unmap(psc->sc_pf, mwindow);
339 memmap_failed:
340 pcmcia_mem_free(psc->sc_pf, &pcmh);
341 memalloc_failed:
342 bad_memaddr:
343
344 return (rv);
345 }
346