if_mbe_pcmcia.c revision 1.17 1 /* $NetBSD: if_mbe_pcmcia.c,v 1.17 2000/02/04 09:30:28 enami 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/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 int mbe_pcmcia_enable __P((struct mb86960_softc *));
78 void mbe_pcmcia_disable __P((struct mb86960_softc *));
79
80 struct mbe_pcmcia_get_enaddr_args {
81 u_int8_t enaddr[ETHER_ADDR_LEN];
82 };
83 int mbe_pcmcia_get_enaddr __P((struct pcmcia_tuple *, void *));
84
85 const struct mbe_pcmcia_product {
86 struct pcmcia_product mpp_product;
87 u_int32_t mpp_ioalign; /* required alignment */
88 } mbe_pcmcia_products[] = {
89 { { PCMCIA_STR_TDK_LAK_CD021BX, PCMCIA_VENDOR_TDK,
90 PCMCIA_PRODUCT_TDK_LAK_CD021BX, 0 },
91 0 },
92
93 { { PCMCIA_STR_TDK_LAK_CF010, PCMCIA_VENDOR_TDK,
94 PCMCIA_PRODUCT_TDK_LAK_CF010, 0 },
95 0 },
96 #if 0 /* XXX 86960-based? */
97 { { PCMCIA_STR_TDK_LAK_DFL9610, PCMCIA_VENDOR_TDK,
98 PCMCIA_PRODUCT_TDK_LAK_DFL9610, 1 },
99 0 },
100 #endif
101
102 { { PCMCIA_STR_CONTEC_CNETPC, PCMCIA_VENDOR_CONTEC,
103 PCMCIA_PRODUCT_CONTEC_CNETPC, 0 },
104 0 },
105
106 { { PCMCIA_STR_FUJITSU_LA501, PCMCIA_VENDOR_FUJITSU,
107 PCMCIA_PRODUCT_FUJITSU_LA501, 0 },
108 0x20 },
109
110 { { PCMCIA_STR_FUJITSU_LA10S, PCMCIA_VENDOR_FUJITSU,
111 PCMCIA_PRODUCT_FUJITSU_LA10S, 0 },
112 0 },
113
114 { { NULL } }
115 };
116
117 int
118 mbe_pcmcia_match(parent, match, aux)
119 struct device *parent;
120 struct cfdata *match;
121 void *aux;
122 {
123 struct pcmcia_attach_args *pa = aux;
124
125 if (pcmcia_product_lookup(pa,
126 (const struct pcmcia_product *)mbe_pcmcia_products,
127 sizeof mbe_pcmcia_products[0], NULL) != NULL)
128 return (1);
129
130 return (0);
131 }
132
133 void
134 mbe_pcmcia_attach(parent, self, aux)
135 struct device *parent, *self;
136 void *aux;
137 {
138 struct mbe_pcmcia_softc *psc = (struct mbe_pcmcia_softc *)self;
139 struct mb86960_softc *sc = &psc->sc_mb86960;
140 struct pcmcia_attach_args *pa = aux;
141 struct pcmcia_config_entry *cfe;
142 struct mbe_pcmcia_get_enaddr_args pgea;
143 const struct mbe_pcmcia_product *mpp;
144 int rv;
145
146 mpp = (const struct mbe_pcmcia_product *)pcmcia_product_lookup(pa,
147 (const struct pcmcia_product *)mbe_pcmcia_products,
148 sizeof mbe_pcmcia_products[0], NULL);
149 if (mpp == NULL) {
150 printf("\n");
151 panic("mbe_pcmcia_attach: impossible");
152 }
153
154 psc->sc_pf = pa->pf;
155 cfe = pa->pf->cfe_head.sqh_first;
156
157 /* Enable the card. */
158 pcmcia_function_init(pa->pf, cfe);
159 if (pcmcia_function_enable(pa->pf)) {
160 printf(": function enable failed\n");
161 goto enable_failed;
162 }
163
164 /* Allocate and map i/o space for the card. */
165 if (pcmcia_io_alloc(pa->pf, cfe->iospace[0].start,
166 cfe->iospace[0].length,
167 mpp->mpp_ioalign ? mpp->mpp_ioalign : cfe->iospace[0].length,
168 &psc->sc_pcioh)) {
169 printf(": can't allocate i/o space\n");
170 goto ioalloc_failed;
171 }
172
173 sc->sc_bst = psc->sc_pcioh.iot;
174 sc->sc_bsh = psc->sc_pcioh.ioh;
175
176 sc->sc_enable = mbe_pcmcia_enable;
177 sc->sc_disable = mbe_pcmcia_disable;
178
179 /*
180 * Don't bother checking flags; the back-end sets the chip
181 * into 16-bit mode.
182 */
183 if (pcmcia_io_map(pa->pf, PCMCIA_WIDTH_IO16, 0, cfe->iospace[0].length,
184 &psc->sc_pcioh, &psc->sc_io_window)) {
185 printf(": can't map i/o space\n");
186 goto iomap_failed;
187 }
188
189 printf(": %s\n", mpp->mpp_product.pp_name);
190
191 /* Read station address from CIS. */
192 rv = pcmcia_scan_cis(parent, mbe_pcmcia_get_enaddr, &pgea);
193 if (rv == -1) {
194 printf("%s: Couldn't read CIS to get ethernet address\n",
195 sc->sc_dev.dv_xname);
196 goto no_enaddr;
197 } else if (rv == 0) {
198 printf("%s: Couldn't get ethernet address from CIS\n",
199 sc->sc_dev.dv_xname);
200 goto no_enaddr;
201 }
202
203 #ifdef DIAGNOSTIC
204 if (rv != 1) {
205 printf("%s: pcmcia_scan_cis returns %d\n", sc->sc_dev.dv_xname,
206 rv);
207 panic("mbe_pcmcia_attach");
208 }
209 printf("%s: Ethernet address from CIS: %s\n",
210 sc->sc_dev.dv_xname, ether_sprintf(pgea.enaddr));
211 #endif
212
213 /* Perform generic initialization. */
214 mb86960_attach(sc, MB86960_TYPE_86965, pgea.enaddr);
215
216 mb86960_config(sc, NULL, 0, 0);
217
218 pcmcia_function_disable(pa->pf);
219 return;
220
221 no_enaddr:
222 /* Unmap our i/o window. */
223 pcmcia_io_unmap(psc->sc_pf, psc->sc_io_window);
224
225 iomap_failed:
226 /* Free our i/o space. */
227 pcmcia_io_free(psc->sc_pf, &psc->sc_pcioh);
228
229 ioalloc_failed:
230 pcmcia_function_disable(pa->pf);
231
232 enable_failed:
233 psc->sc_io_window = -1;
234 }
235
236 int
237 mbe_pcmcia_detach(self, flags)
238 struct device *self;
239 int flags;
240 {
241 struct mbe_pcmcia_softc *psc = (struct mbe_pcmcia_softc *)self;
242 int error;
243
244 if (psc->sc_io_window == -1)
245 /* Nothing to detach since we've failed to attach. */
246 return (0);
247
248 error = mb86960_detach(&psc->sc_mb86960);
249 if (error != 0)
250 return (error);
251
252 /* Unmap our i/o window. */
253 pcmcia_io_unmap(psc->sc_pf, psc->sc_io_window);
254
255 /* Free our i/o space. */
256 pcmcia_io_free(psc->sc_pf, &psc->sc_pcioh);
257
258 return (0);
259 }
260
261 int
262 mbe_pcmcia_enable(sc)
263 struct mb86960_softc *sc;
264 {
265 struct mbe_pcmcia_softc *psc = (struct mbe_pcmcia_softc *)sc;
266
267 /* Establish the interrupt handler. */
268 psc->sc_ih = pcmcia_intr_establish(psc->sc_pf, IPL_NET, mb86960_intr,
269 sc);
270 if (psc->sc_ih == NULL) {
271 printf("%s: couldn't establish interrupt handler\n",
272 sc->sc_dev.dv_xname);
273 return (1);
274 }
275
276 if (pcmcia_function_enable(psc->sc_pf)) {
277 pcmcia_intr_disestablish(psc->sc_pf, psc->sc_ih);
278 return (1);
279 }
280
281 return (0);
282 }
283
284 void
285 mbe_pcmcia_disable(sc)
286 struct mb86960_softc *sc;
287 {
288 struct mbe_pcmcia_softc *psc = (struct mbe_pcmcia_softc *)sc;
289
290 pcmcia_function_disable(psc->sc_pf);
291 pcmcia_intr_disestablish(psc->sc_pf, psc->sc_ih);
292 }
293
294 int
295 mbe_pcmcia_get_enaddr(tuple, arg)
296 struct pcmcia_tuple *tuple;
297 void *arg;
298 {
299 struct mbe_pcmcia_get_enaddr_args *p = arg;
300 int i;
301
302 if (tuple->code == PCMCIA_CISTPL_FUNCE) {
303 if (tuple->length < 2) /* sub code and ether addr length */
304 return (0);
305
306 if ((pcmcia_tuple_read_1(tuple, 0) !=
307 PCMCIA_TPLFE_TYPE_LAN_NID) ||
308 (pcmcia_tuple_read_1(tuple, 1) != ETHER_ADDR_LEN))
309 return (0);
310
311 for (i = 0; i < ETHER_ADDR_LEN; i++)
312 p->enaddr[i] = pcmcia_tuple_read_1(tuple, i + 2);
313 return (1);
314 }
315 return (0);
316 }
317