if_mbe_pcmcia.c revision 1.8.6.1 1 /* $NetBSD: if_mbe_pcmcia.c,v 1.8.6.1 1999/12/27 18:35:26 wrstuden Exp $ */
2
3 /*-
4 * Copyright (c) 1998 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/param.h>
40 #include <sys/systm.h>
41 #include <sys/device.h>
42 #include <sys/socket.h>
43
44 #include <net/if.h>
45 #include <net/if_ether.h>
46 #include <net/if_media.h>
47
48 #include <machine/intr.h>
49 #include <machine/bus.h>
50
51 #include <dev/ic/mb86960reg.h>
52 #include <dev/ic/mb86960var.h>
53
54 #include <dev/pcmcia/pcmciareg.h>
55 #include <dev/pcmcia/pcmciavar.h>
56 #include <dev/pcmcia/pcmciadevs.h>
57
58 int mbe_pcmcia_match __P((struct device *, struct cfdata *, void *));
59 void mbe_pcmcia_attach __P((struct device *, struct device *, void *));
60 int mbe_pcmcia_detach __P((struct device *, int));
61
62 struct mbe_pcmcia_softc {
63 struct mb86960_softc sc_mb86960; /* real "mb" softc */
64
65 /* PCMCIA-specific goo. */
66 struct pcmcia_io_handle sc_pcioh; /* PCMCIA i/o space info */
67 int sc_io_window; /* our i/o window */
68 void *sc_ih; /* interrupt cookie */
69 struct pcmcia_function *sc_pf; /* our PCMCIA function */
70 };
71
72 struct cfattach mbe_pcmcia_ca = {
73 sizeof(struct mbe_pcmcia_softc), mbe_pcmcia_match, mbe_pcmcia_attach,
74 mbe_pcmcia_detach, mb86960_activate
75 };
76
77 #if NetBSD <= 199712
78 struct cfdriver mbe_cd = { /* XXX shouldn't be here */
79 NULL, "mbe", DV_IFNET
80 };
81 #endif
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 };
89 int mbe_pcmcia_get_enaddr __P((struct pcmcia_tuple *, void *));
90
91 struct mbe_pcmcia_product {
92 u_int32_t mpp_vendor; /* vendor ID */
93 u_int32_t mpp_product; /* product ID */
94 int mpp_expfunc; /* exptected function */
95 u_int32_t mpp_ioalign; /* required alignment */
96 const char *mpp_name; /* product name */
97 } mbe_pcmcia_products[] = {
98 { PCMCIA_VENDOR_TDK, PCMCIA_PRODUCT_TDK_LAK_CD021BX,
99 0, 0,
100 PCMCIA_STR_TDK_LAK_CD021BX },
101
102 { PCMCIA_VENDOR_TDK, PCMCIA_PRODUCT_TDK_LAK_CF010,
103 0, 0,
104 PCMCIA_STR_TDK_LAK_CF010},
105 #if 0 /* XXX 86960-based? */
106 { PCMCIA_VENDOR_TDK, PCMCIA_PRODUCT_TDK_LAK_DFL9610,
107 1, 0,
108 PCMCIA_STR_TDK_LAK_DFL9610 }
109 #endif
110
111 { PCMCIA_VENDOR_CONTEC, PCMCIA_PRODUCT_CONTEC_CNETPC,
112 0, 0,
113 PCMCIA_STR_CONTEC_CNETPC },
114
115 { PCMCIA_VENDOR_FUJITSU, PCMCIA_PRODUCT_FUJITSU_LA501,
116 0, 0x20,
117 PCMCIA_STR_FUJITSU_LA501 },
118
119 { PCMCIA_VENDOR_FUJITSU, PCMCIA_PRODUCT_FUJITSU_LA10S,
120 0, 0,
121 PCMCIA_STR_FUJITSU_LA10S },
122
123 { 0, 0,
124 0, 0,
125 NULL },
126 };
127
128 const struct mbe_pcmcia_product *mbe_pcmcia_lookup
129 __P((const struct pcmcia_attach_args *pa));
130
131 const struct mbe_pcmcia_product *
132 mbe_pcmcia_lookup(pa)
133 const struct pcmcia_attach_args *pa;
134 {
135 const struct mbe_pcmcia_product *mpp;
136
137 for (mpp = mbe_pcmcia_products; mpp->mpp_name != NULL; mpp++) {
138 if (pa->manufacturer == mpp->mpp_vendor &&
139 pa->product == mpp->mpp_product &&
140 pa->pf->number == mpp->mpp_expfunc)
141 return (mpp);
142 }
143 return (NULL);
144 }
145
146 int
147 mbe_pcmcia_match(parent, match, aux)
148 struct device *parent;
149 struct cfdata *match;
150 void *aux;
151 {
152 struct pcmcia_attach_args *pa = aux;
153
154 if (mbe_pcmcia_lookup(pa) != NULL)
155 return (1);
156
157 return (0);
158 }
159
160 void
161 mbe_pcmcia_attach(parent, self, aux)
162 struct device *parent, *self;
163 void *aux;
164 {
165 struct mbe_pcmcia_softc *psc = (struct mbe_pcmcia_softc *)self;
166 struct mb86960_softc *sc = &psc->sc_mb86960;
167 struct pcmcia_attach_args *pa = aux;
168 struct pcmcia_config_entry *cfe;
169 struct mbe_pcmcia_get_enaddr_args pgea;
170 const struct mbe_pcmcia_product *mpp;
171 int rv;
172
173 mpp = mbe_pcmcia_lookup(pa);
174 if (mpp == NULL) {
175 printf("\n");
176 panic("mbe_pcmcia_attach: impossible");
177 }
178
179 psc->sc_pf = pa->pf;
180 cfe = pa->pf->cfe_head.sqh_first;
181
182 /* Enable the card. */
183 pcmcia_function_init(pa->pf, cfe);
184 if (pcmcia_function_enable(pa->pf)) {
185 printf(": function enable failed\n");
186 return;
187 }
188
189 /* Allocate and map i/o space for the card. */
190 if (pcmcia_io_alloc(pa->pf, cfe->iospace[0].start,
191 cfe->iospace[0].length,
192 mpp->mpp_ioalign ? mpp->mpp_ioalign : cfe->iospace[0].length,
193 &psc->sc_pcioh)) {
194 printf(": can't allocate i/o space\n");
195 return;
196 }
197
198 sc->sc_bst = psc->sc_pcioh.iot;
199 sc->sc_bsh = psc->sc_pcioh.ioh;
200
201 sc->sc_enable = mbe_pcmcia_enable;
202 sc->sc_disable = mbe_pcmcia_disable;
203
204 /*
205 * Don't bother checking flags; the back-end sets the chip
206 * into 16-bit mode.
207 */
208 if (pcmcia_io_map(pa->pf, PCMCIA_WIDTH_IO16, 0, cfe->iospace[0].length,
209 &psc->sc_pcioh, &psc->sc_io_window)) {
210 printf(": can't map i/o space\n");
211 return;
212 }
213
214 printf(": %s\n", mpp->mpp_name);
215
216 /* Read station address from CIS. */
217 rv = pcmcia_scan_cis(parent, mbe_pcmcia_get_enaddr, &pgea);
218 if (rv == -1) {
219 printf("%s: Couldn't read CIS to get ethernet address\n",
220 sc->sc_dev.dv_xname);
221 return;
222 } else if (rv == 0) {
223 printf("%s: Couldn't get ethernet address from CIS\n",
224 sc->sc_dev.dv_xname);
225 return;
226 }
227
228 #ifdef DIAGNOSTIC
229 if (rv != 1) {
230 printf("%s: pcmcia_scan_cis returns %d\n", sc->sc_dev.dv_xname,
231 rv);
232 panic("mbe_pcmcia_attach");
233 }
234 printf("%s: Ethernet address from CIS: %s\n",
235 sc->sc_dev.dv_xname, ether_sprintf(pgea.enaddr));
236 #endif
237
238 /* Perform generic initialization. */
239 mb86960_attach(sc, MB86960_TYPE_86965, pgea.enaddr);
240
241 mb86960_config(sc, NULL, 0, 0);
242
243 pcmcia_function_disable(pa->pf);
244 }
245
246 int
247 mbe_pcmcia_detach(self, flags)
248 struct device *self;
249 int flags;
250 {
251 struct mbe_pcmcia_softc *psc = (struct mbe_pcmcia_softc *)self;
252
253 /* Unmap our i/o window. */
254 pcmcia_io_unmap(psc->sc_pf, psc->sc_io_window);
255
256 /* Free our i/o space. */
257 pcmcia_io_free(psc->sc_pf, &psc->sc_pcioh);
258
259 #ifdef notyet
260 /*
261 * Our softc is about to go away, so drop our reference
262 * to the ifnet.
263 */
264 if_delref(psc->sc_mb86960.sc_ec.ec_if);
265 return (0);
266 #else
267 return (EBUSY);
268 #endif
269 }
270
271 int
272 mbe_pcmcia_enable(sc)
273 struct mb86960_softc *sc;
274 {
275 struct mbe_pcmcia_softc *psc = (struct mbe_pcmcia_softc *)sc;
276
277 /* Establish the interrupt handler. */
278 psc->sc_ih = pcmcia_intr_establish(psc->sc_pf, IPL_NET, mb86960_intr,
279 sc);
280 if (psc->sc_ih == NULL) {
281 printf("%s: couldn't establish interrupt handler\n",
282 sc->sc_dev.dv_xname);
283 return (1);
284 }
285
286 return (pcmcia_function_enable(psc->sc_pf));
287 }
288
289 void
290 mbe_pcmcia_disable(sc)
291 struct mb86960_softc *sc;
292 {
293 struct mbe_pcmcia_softc *psc = (struct mbe_pcmcia_softc *)sc;
294
295 pcmcia_function_disable(psc->sc_pf);
296
297 pcmcia_intr_disestablish(psc->sc_pf, psc->sc_ih);
298 }
299
300 int
301 mbe_pcmcia_get_enaddr(tuple, arg)
302 struct pcmcia_tuple *tuple;
303 void *arg;
304 {
305 struct mbe_pcmcia_get_enaddr_args *p = arg;
306 int i;
307
308 if (tuple->code == PCMCIA_CISTPL_FUNCE) {
309 if (tuple->length < 2) /* sub code and ether addr length */
310 return (0);
311
312 if ((pcmcia_tuple_read_1(tuple, 0) !=
313 PCMCIA_TPLFE_TYPE_LAN_NID) ||
314 (pcmcia_tuple_read_1(tuple, 1) != ETHER_ADDR_LEN))
315 return (0);
316
317 for (i = 0; i < ETHER_ADDR_LEN; i++)
318 p->enaddr[i] = pcmcia_tuple_read_1(tuple, i + 2);
319 return (1);
320 }
321 return (0);
322 }
323