if_mbe_pcmcia.c revision 1.24 1 /* $NetBSD: if_mbe_pcmcia.c,v 1.24 2001/12/18 11:32:47 ichiro Exp $ */
2
3 /*-
4 * Copyright (c) 1998, 2000 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.24 2001/12/18 11:32:47 ichiro 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 void mbe_pcmcia_attach __P((struct device *, struct device *, void *));
63 int mbe_pcmcia_detach __P((struct device *, int));
64
65 static const struct mbe_pcmcia_product
66 *mbe_pcmcia_lookup __P((struct pcmcia_attach_args *pa));
67
68 struct mbe_pcmcia_softc {
69 struct mb86960_softc sc_mb86960; /* real "mb" softc */
70
71 /* PCMCIA-specific goo. */
72 struct pcmcia_io_handle sc_pcioh; /* PCMCIA i/o space info */
73 int sc_io_window; /* our i/o window */
74 void *sc_ih; /* interrupt cookie */
75 struct pcmcia_function *sc_pf; /* our PCMCIA function */
76 };
77
78 struct cfattach mbe_pcmcia_ca = {
79 sizeof(struct mbe_pcmcia_softc), mbe_pcmcia_match, mbe_pcmcia_attach,
80 mbe_pcmcia_detach, mb86960_activate
81 };
82
83 int mbe_pcmcia_enable __P((struct mb86960_softc *));
84 void mbe_pcmcia_disable __P((struct mb86960_softc *));
85
86 struct mbe_pcmcia_get_enaddr_args {
87 u_int8_t enaddr[ETHER_ADDR_LEN];
88 int maddr;
89 };
90 int mbe_pcmcia_get_enaddr_from_cis __P((struct pcmcia_tuple *, void *));
91 int mbe_pcmcia_get_enaddr_from_mem __P((struct mbe_pcmcia_softc *,
92 struct mbe_pcmcia_get_enaddr_args *));
93
94 static const struct mbe_pcmcia_product {
95 const char *mpp_name; /* product name */
96 u_int32_t mpp_vendor; /* vendor ID */
97 u_int32_t mpp_product; /* product ID */
98 const char *mpp_cisinfo[4]; /* CIS information */
99 u_int32_t mpp_ioalign; /* required alignment */
100 int mpp_enet_maddr;
101 } mbe_pcmcia_products[] = {
102 { PCMCIA_STR_TDK_LAK_CD021BX, PCMCIA_VENDOR_TDK,
103 PCMCIA_PRODUCT_TDK_LAK_CD021BX, PCMCIA_CIS_TDK_LAK_CD021BX,
104 0, -1 },
105
106 { PCMCIA_STR_TDK_LAK_CF010, PCMCIA_VENDOR_TDK,
107 PCMCIA_PRODUCT_TDK_LAK_CF010, PCMCIA_CIS_TDK_LAK_CF010,
108 0, -1 },
109
110 #if 0 /* XXX 86960-based? */
111 { PCMCIA_STR_TDK_LAK_DFL9610, PCMCIA_VENDOR_TDK,
112 PCMCIA_PRODUCT_TDK_LAK_DFL9610, PCMCIA_CIS_TDK_DFL9610,
113 0, -1 },
114 #endif
115
116 { PCMCIA_STR_CONTEC_CNETPC, PCMCIA_VENDOR_CONTEC,
117 PCMCIA_PRODUCT_CONTEC_CNETPC, PCMCIA_CIS_CONTEC_CNETPC,
118 0, -1 },
119
120 { PCMCIA_STR_FUJITSU_LA501, PCMCIA_VENDOR_FUJITSU,
121 PCMCIA_PRODUCT_FUJITSU_LA501, PCMCIA_CIS_FUJITSU_LA501,
122 0x20, -1 },
123
124 { PCMCIA_STR_FUJITSU_LA10S, PCMCIA_VENDOR_FUJITSU,
125 PCMCIA_PRODUCT_FUJITSU_LA10S, PCMCIA_CIS_FUJITSU_LA10S,
126 0, -1 },
127
128 { PCMCIA_STR_FUJITSU_ITCFJ182A, PCMCIA_VENDOR_FUJITSU,
129 PCMCIA_PRODUCT_FUJITSU_ITCFJ182A, PCMCIA_CIS_FUJITSU_ITCFJ182A,
130 0, 0x1cc },
131
132 { PCMCIA_STR_RATOC_REX_R280, PCMCIA_VENDOR_RATOC,
133 PCMCIA_PRODUCT_RATOC_REX_R280, PCMCIA_CIS_RATOC_REX_R280,
134 0, 0x1fc },
135
136 { NULL, 0,
137 0, { NULL, NULL, NULL, NULL },
138 0, 0 }
139 };
140
141 static const struct mbe_pcmcia_product *
142 mbe_pcmcia_lookup(pa)
143 struct pcmcia_attach_args *pa;
144 {
145 const struct mbe_pcmcia_product *mpp;
146
147 for (mpp = mbe_pcmcia_products; mpp->mpp_name != NULL; mpp++) {
148 /* match by CIS information */
149 if (pa->card->cis1_info[0] != NULL &&
150 mpp->mpp_cisinfo[0] != NULL &&
151 strcmp(pa->card->cis1_info[0], mpp->mpp_cisinfo[0]) == 0 &&
152 pa->card->cis1_info[1] != NULL &&
153 mpp->mpp_cisinfo[1] != NULL &&
154 strcmp(pa->card->cis1_info[1], mpp->mpp_cisinfo[1]) == 0)
155 return (mpp);
156
157 /* match by vendor/product id */
158 if (pa->manufacturer != PCMCIA_VENDOR_INVALID &&
159 pa->manufacturer == mpp->mpp_vendor &&
160 pa->product != PCMCIA_PRODUCT_INVALID &&
161 pa->product == mpp->mpp_product)
162 return (mpp);
163 }
164
165 return (NULL);
166 }
167
168 int
169 mbe_pcmcia_match(parent, match, aux)
170 struct device *parent;
171 struct cfdata *match;
172 void *aux;
173 {
174 struct pcmcia_attach_args *pa = aux;
175
176 if (mbe_pcmcia_lookup(pa) != NULL)
177 return (1);
178 return (0);
179 }
180
181 void
182 mbe_pcmcia_attach(parent, self, aux)
183 struct device *parent, *self;
184 void *aux;
185 {
186 struct mbe_pcmcia_softc *psc = (struct mbe_pcmcia_softc *)self;
187 struct mb86960_softc *sc = &psc->sc_mb86960;
188 struct pcmcia_attach_args *pa = aux;
189 struct pcmcia_config_entry *cfe;
190 struct mbe_pcmcia_get_enaddr_args pgea;
191 const struct mbe_pcmcia_product *mpp;
192 int rv;
193
194 mpp = mbe_pcmcia_lookup(pa);
195 if (mpp == NULL) {
196 printf("\n");
197 panic("mbe_pcmcia_attach: impossible");
198 }
199
200 psc->sc_pf = pa->pf;
201 cfe = pa->pf->cfe_head.sqh_first;
202
203 /* Enable the card. */
204 pcmcia_function_init(pa->pf, cfe);
205 if (pcmcia_function_enable(pa->pf)) {
206 printf(": function enable failed\n");
207 goto enable_failed;
208 }
209
210 /* Allocate and map i/o space for the card. */
211 if (pcmcia_io_alloc(pa->pf, cfe->iospace[0].start,
212 cfe->iospace[0].length,
213 mpp->mpp_ioalign ? mpp->mpp_ioalign : cfe->iospace[0].length,
214 &psc->sc_pcioh)) {
215 printf(": can't allocate i/o space\n");
216 goto ioalloc_failed;
217 }
218
219 sc->sc_bst = psc->sc_pcioh.iot;
220 sc->sc_bsh = psc->sc_pcioh.ioh;
221
222 sc->sc_enable = mbe_pcmcia_enable;
223 sc->sc_disable = mbe_pcmcia_disable;
224
225 /*
226 * Don't bother checking flags; the back-end sets the chip
227 * into 16-bit mode.
228 */
229 if (pcmcia_io_map(pa->pf, PCMCIA_WIDTH_IO16, 0, cfe->iospace[0].length,
230 &psc->sc_pcioh, &psc->sc_io_window)) {
231 printf(": can't map i/o space\n");
232 goto iomap_failed;
233 }
234
235 printf(": %s\n", mpp->mpp_name);
236
237 /* Read station address from mem or CIS. */
238 if (mpp->mpp_enet_maddr >= 0) {
239 pgea.maddr = mpp->mpp_enet_maddr;
240 if (mbe_pcmcia_get_enaddr_from_mem(psc, &pgea) != 0) {
241 printf("%s: Couldn't get ethernet address "
242 "from mem\n", sc->sc_dev.dv_xname);
243 goto no_enaddr;
244 }
245 } else {
246 rv = pcmcia_scan_cis(parent,
247 mbe_pcmcia_get_enaddr_from_cis, &pgea);
248 if (rv == -1) {
249 printf("%s: Couldn't read CIS to get ethernet "
250 "address\n", sc->sc_dev.dv_xname);
251 goto no_enaddr;
252 } else if (rv == 0) {
253 printf("%s: Couldn't get ethernet address "
254 "from CIS\n", sc->sc_dev.dv_xname);
255 goto no_enaddr;
256 }
257 #ifdef DIAGNOSTIC
258 if (rv != 1) {
259 printf("%s: pcmcia_scan_cis returns %d\n",
260 sc->sc_dev.dv_xname, rv);
261 panic("mbe_pcmcia_attach");
262 }
263 printf("%s: Ethernet address from CIS: %s\n",
264 sc->sc_dev.dv_xname, ether_sprintf(pgea.enaddr));
265 #endif
266 }
267
268 /* Perform generic initialization. */
269 mb86960_attach(sc, MB86960_TYPE_86965, pgea.enaddr);
270
271 mb86960_config(sc, NULL, 0, 0);
272
273 pcmcia_function_disable(pa->pf);
274 return;
275
276 no_enaddr:
277 /* Unmap our i/o window. */
278 pcmcia_io_unmap(psc->sc_pf, psc->sc_io_window);
279
280 iomap_failed:
281 /* Free our i/o space. */
282 pcmcia_io_free(psc->sc_pf, &psc->sc_pcioh);
283
284 ioalloc_failed:
285 pcmcia_function_disable(pa->pf);
286
287 enable_failed:
288 psc->sc_io_window = -1;
289 }
290
291 int
292 mbe_pcmcia_detach(self, flags)
293 struct device *self;
294 int flags;
295 {
296 struct mbe_pcmcia_softc *psc = (struct mbe_pcmcia_softc *)self;
297 int error;
298
299 if (psc->sc_io_window == -1)
300 /* Nothing to detach. */
301 return (0);
302
303 error = mb86960_detach(&psc->sc_mb86960);
304 if (error != 0)
305 return (error);
306
307 /* Unmap our i/o window. */
308 pcmcia_io_unmap(psc->sc_pf, psc->sc_io_window);
309
310 /* Free our i/o space. */
311 pcmcia_io_free(psc->sc_pf, &psc->sc_pcioh);
312
313 return (0);
314 }
315
316 int
317 mbe_pcmcia_enable(sc)
318 struct mb86960_softc *sc;
319 {
320 struct mbe_pcmcia_softc *psc = (struct mbe_pcmcia_softc *)sc;
321
322 /* Establish the interrupt handler. */
323 psc->sc_ih = pcmcia_intr_establish(psc->sc_pf, IPL_NET, mb86960_intr,
324 sc);
325 if (psc->sc_ih == NULL) {
326 printf("%s: couldn't establish interrupt handler\n",
327 sc->sc_dev.dv_xname);
328 return (1);
329 }
330
331 if (pcmcia_function_enable(psc->sc_pf)) {
332 pcmcia_intr_disestablish(psc->sc_pf, psc->sc_ih);
333 return (1);
334 }
335
336 return (0);
337 }
338
339 void
340 mbe_pcmcia_disable(sc)
341 struct mb86960_softc *sc;
342 {
343 struct mbe_pcmcia_softc *psc = (struct mbe_pcmcia_softc *)sc;
344
345 pcmcia_function_disable(psc->sc_pf);
346 pcmcia_intr_disestablish(psc->sc_pf, psc->sc_ih);
347 }
348
349 int
350 mbe_pcmcia_get_enaddr_from_cis(tuple, arg)
351 struct pcmcia_tuple *tuple;
352 void *arg;
353 {
354 struct mbe_pcmcia_get_enaddr_args *p = arg;
355 int i;
356
357 if (tuple->code == PCMCIA_CISTPL_FUNCE) {
358 if (tuple->length < 2) /* sub code and ether addr length */
359 return (0);
360
361 if ((pcmcia_tuple_read_1(tuple, 0) !=
362 PCMCIA_TPLFE_TYPE_LAN_NID) ||
363 (pcmcia_tuple_read_1(tuple, 1) != ETHER_ADDR_LEN))
364 return (0);
365
366 for (i = 0; i < ETHER_ADDR_LEN; i++)
367 p->enaddr[i] = pcmcia_tuple_read_1(tuple, i + 2);
368 return (1);
369 }
370 return (0);
371 }
372
373 int
374 mbe_pcmcia_get_enaddr_from_mem(psc, ea)
375 struct mbe_pcmcia_softc *psc;
376 struct mbe_pcmcia_get_enaddr_args *ea;
377 {
378 struct mb86960_softc *sc = &psc->sc_mb86960;
379 struct pcmcia_mem_handle pcmh;
380 bus_size_t offset;
381 int i, mwindow, rv = 1;
382
383 if (ea->maddr < 0)
384 goto bad_memaddr;
385
386 if (pcmcia_mem_alloc(psc->sc_pf, ETHER_ADDR_LEN * 2, &pcmh)) {
387 printf("%s: can't alloc mem for enet addr\n",
388 sc->sc_dev.dv_xname);
389 goto memalloc_failed;
390 }
391
392 if (pcmcia_mem_map(psc->sc_pf, PCMCIA_MEM_ATTR, ea->maddr,
393 ETHER_ADDR_LEN * 2, &pcmh, &offset, &mwindow)) {
394 printf("%s: can't map mem for enet addr\n",
395 sc->sc_dev.dv_xname);
396 goto memmap_failed;
397 }
398
399 for (i = 0; i < ETHER_ADDR_LEN; i++)
400 ea->enaddr[i] = bus_space_read_1(pcmh.memt, pcmh.memh,
401 offset + (i * 2));
402
403 rv = 0;
404 pcmcia_mem_unmap(psc->sc_pf, mwindow);
405 memmap_failed:
406 pcmcia_mem_free(psc->sc_pf, &pcmh);
407 memalloc_failed:
408 bad_memaddr:
409
410 return (rv);
411 }
412