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