if_mbe_pcmcia.c revision 1.18 1 1.18 is /* $NetBSD: if_mbe_pcmcia.c,v 1.18 2000/05/11 19:24:35 is Exp $ */
2 1.1 enami
3 1.1 enami /*-
4 1.13 cgd * Copyright (c) 1998, 2000 The NetBSD Foundation, Inc.
5 1.1 enami * All rights reserved.
6 1.1 enami *
7 1.1 enami * This code is derived from software contributed to The NetBSD Foundation
8 1.1 enami * by Enami Tsugutomo.
9 1.1 enami *
10 1.1 enami * Redistribution and use in source and binary forms, with or without
11 1.1 enami * modification, are permitted provided that the following conditions
12 1.1 enami * are met:
13 1.1 enami * 1. Redistributions of source code must retain the above copyright
14 1.1 enami * notice, this list of conditions and the following disclaimer.
15 1.1 enami * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 enami * notice, this list of conditions and the following disclaimer in the
17 1.1 enami * documentation and/or other materials provided with the distribution.
18 1.1 enami * 3. All advertising materials mentioning features or use of this software
19 1.1 enami * must display the following acknowledgement:
20 1.1 enami * This product includes software developed by the NetBSD
21 1.1 enami * Foundation, Inc. and its contributors.
22 1.1 enami * 4. Neither the name of The NetBSD Foundation nor the names of its
23 1.1 enami * contributors may be used to endorse or promote products derived
24 1.1 enami * from this software without specific prior written permission.
25 1.1 enami *
26 1.1 enami * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 1.1 enami * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 1.1 enami * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 1.1 enami * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 1.1 enami * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 1.1 enami * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 1.1 enami * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 1.1 enami * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 1.1 enami * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 1.1 enami * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 1.1 enami * POSSIBILITY OF SUCH DAMAGE.
37 1.1 enami */
38 1.1 enami
39 1.1 enami #include <sys/param.h>
40 1.1 enami #include <sys/systm.h>
41 1.1 enami #include <sys/device.h>
42 1.1 enami #include <sys/socket.h>
43 1.1 enami
44 1.1 enami #include <net/if.h>
45 1.1 enami #include <net/if_ether.h>
46 1.1 enami #include <net/if_media.h>
47 1.1 enami
48 1.1 enami #include <machine/intr.h>
49 1.1 enami #include <machine/bus.h>
50 1.1 enami
51 1.1 enami #include <dev/ic/mb86960reg.h>
52 1.1 enami #include <dev/ic/mb86960var.h>
53 1.1 enami
54 1.1 enami #include <dev/pcmcia/pcmciareg.h>
55 1.1 enami #include <dev/pcmcia/pcmciavar.h>
56 1.3 christos #include <dev/pcmcia/pcmciadevs.h>
57 1.1 enami
58 1.1 enami int mbe_pcmcia_match __P((struct device *, struct cfdata *, void *));
59 1.1 enami void mbe_pcmcia_attach __P((struct device *, struct device *, void *));
60 1.4 thorpej int mbe_pcmcia_detach __P((struct device *, int));
61 1.1 enami
62 1.1 enami struct mbe_pcmcia_softc {
63 1.1 enami struct mb86960_softc sc_mb86960; /* real "mb" softc */
64 1.1 enami
65 1.1 enami /* PCMCIA-specific goo. */
66 1.1 enami struct pcmcia_io_handle sc_pcioh; /* PCMCIA i/o space info */
67 1.1 enami int sc_io_window; /* our i/o window */
68 1.1 enami void *sc_ih; /* interrupt cookie */
69 1.1 enami struct pcmcia_function *sc_pf; /* our PCMCIA function */
70 1.1 enami };
71 1.1 enami
72 1.1 enami struct cfattach mbe_pcmcia_ca = {
73 1.4 thorpej sizeof(struct mbe_pcmcia_softc), mbe_pcmcia_match, mbe_pcmcia_attach,
74 1.6 thorpej mbe_pcmcia_detach, mb86960_activate
75 1.1 enami };
76 1.1 enami
77 1.1 enami int mbe_pcmcia_enable __P((struct mb86960_softc *));
78 1.1 enami void mbe_pcmcia_disable __P((struct mb86960_softc *));
79 1.1 enami
80 1.1 enami struct mbe_pcmcia_get_enaddr_args {
81 1.1 enami u_int8_t enaddr[ETHER_ADDR_LEN];
82 1.18 is int maddr;
83 1.1 enami };
84 1.18 is int mbe_pcmcia_get_enaddr_from_cis __P((struct pcmcia_tuple *, void *));
85 1.18 is int mbe_pcmcia_get_enaddr_from_mem __P((struct mbe_pcmcia_softc *,
86 1.18 is struct mbe_pcmcia_get_enaddr_args *));
87 1.1 enami
88 1.13 cgd const struct mbe_pcmcia_product {
89 1.13 cgd struct pcmcia_product mpp_product;
90 1.7 thorpej u_int32_t mpp_ioalign; /* required alignment */
91 1.18 is int enet_maddr;
92 1.7 thorpej } mbe_pcmcia_products[] = {
93 1.13 cgd { { PCMCIA_STR_TDK_LAK_CD021BX, PCMCIA_VENDOR_TDK,
94 1.13 cgd PCMCIA_PRODUCT_TDK_LAK_CD021BX, 0 },
95 1.18 is 0, -1},
96 1.13 cgd
97 1.13 cgd { { PCMCIA_STR_TDK_LAK_CF010, PCMCIA_VENDOR_TDK,
98 1.13 cgd PCMCIA_PRODUCT_TDK_LAK_CF010, 0 },
99 1.18 is 0, -1},
100 1.7 thorpej #if 0 /* XXX 86960-based? */
101 1.13 cgd { { PCMCIA_STR_TDK_LAK_DFL9610, PCMCIA_VENDOR_TDK,
102 1.13 cgd PCMCIA_PRODUCT_TDK_LAK_DFL9610, 1 },
103 1.18 is 0, -1 },
104 1.7 thorpej #endif
105 1.8 tron
106 1.13 cgd { { PCMCIA_STR_CONTEC_CNETPC, PCMCIA_VENDOR_CONTEC,
107 1.13 cgd PCMCIA_PRODUCT_CONTEC_CNETPC, 0 },
108 1.18 is 0, -1 },
109 1.13 cgd
110 1.13 cgd { { PCMCIA_STR_FUJITSU_LA501, PCMCIA_VENDOR_FUJITSU,
111 1.13 cgd PCMCIA_PRODUCT_FUJITSU_LA501, 0 },
112 1.18 is 0x20, -1 },
113 1.13 cgd
114 1.13 cgd { { PCMCIA_STR_FUJITSU_LA10S, PCMCIA_VENDOR_FUJITSU,
115 1.13 cgd PCMCIA_PRODUCT_FUJITSU_LA10S, 0 },
116 1.18 is 0, -1 },
117 1.18 is
118 1.18 is { { PCMCIA_STR_RATOC_REX_R280, PCMCIA_VENDOR_RATOC,
119 1.18 is PCMCIA_PRODUCT_RATOC_REX_R280, 0 },
120 1.18 is 0, 0x1fc },
121 1.13 cgd
122 1.13 cgd { { NULL } }
123 1.7 thorpej };
124 1.7 thorpej
125 1.1 enami int
126 1.1 enami mbe_pcmcia_match(parent, match, aux)
127 1.1 enami struct device *parent;
128 1.1 enami struct cfdata *match;
129 1.1 enami void *aux;
130 1.1 enami {
131 1.1 enami struct pcmcia_attach_args *pa = aux;
132 1.1 enami
133 1.13 cgd if (pcmcia_product_lookup(pa,
134 1.13 cgd (const struct pcmcia_product *)mbe_pcmcia_products,
135 1.13 cgd sizeof mbe_pcmcia_products[0], NULL) != NULL)
136 1.7 thorpej return (1);
137 1.1 enami
138 1.1 enami return (0);
139 1.1 enami }
140 1.1 enami
141 1.1 enami void
142 1.1 enami mbe_pcmcia_attach(parent, self, aux)
143 1.1 enami struct device *parent, *self;
144 1.1 enami void *aux;
145 1.1 enami {
146 1.1 enami struct mbe_pcmcia_softc *psc = (struct mbe_pcmcia_softc *)self;
147 1.1 enami struct mb86960_softc *sc = &psc->sc_mb86960;
148 1.1 enami struct pcmcia_attach_args *pa = aux;
149 1.1 enami struct pcmcia_config_entry *cfe;
150 1.1 enami struct mbe_pcmcia_get_enaddr_args pgea;
151 1.7 thorpej const struct mbe_pcmcia_product *mpp;
152 1.10 enami int rv;
153 1.7 thorpej
154 1.13 cgd mpp = (const struct mbe_pcmcia_product *)pcmcia_product_lookup(pa,
155 1.13 cgd (const struct pcmcia_product *)mbe_pcmcia_products,
156 1.13 cgd sizeof mbe_pcmcia_products[0], NULL);
157 1.7 thorpej if (mpp == NULL) {
158 1.7 thorpej printf("\n");
159 1.7 thorpej panic("mbe_pcmcia_attach: impossible");
160 1.7 thorpej }
161 1.1 enami
162 1.1 enami psc->sc_pf = pa->pf;
163 1.1 enami cfe = pa->pf->cfe_head.sqh_first;
164 1.1 enami
165 1.1 enami /* Enable the card. */
166 1.1 enami pcmcia_function_init(pa->pf, cfe);
167 1.1 enami if (pcmcia_function_enable(pa->pf)) {
168 1.1 enami printf(": function enable failed\n");
169 1.17 enami goto enable_failed;
170 1.1 enami }
171 1.1 enami
172 1.1 enami /* Allocate and map i/o space for the card. */
173 1.1 enami if (pcmcia_io_alloc(pa->pf, cfe->iospace[0].start,
174 1.7 thorpej cfe->iospace[0].length,
175 1.7 thorpej mpp->mpp_ioalign ? mpp->mpp_ioalign : cfe->iospace[0].length,
176 1.7 thorpej &psc->sc_pcioh)) {
177 1.1 enami printf(": can't allocate i/o space\n");
178 1.14 enami goto ioalloc_failed;
179 1.1 enami }
180 1.1 enami
181 1.1 enami sc->sc_bst = psc->sc_pcioh.iot;
182 1.1 enami sc->sc_bsh = psc->sc_pcioh.ioh;
183 1.1 enami
184 1.1 enami sc->sc_enable = mbe_pcmcia_enable;
185 1.1 enami sc->sc_disable = mbe_pcmcia_disable;
186 1.1 enami
187 1.7 thorpej /*
188 1.7 thorpej * Don't bother checking flags; the back-end sets the chip
189 1.7 thorpej * into 16-bit mode.
190 1.7 thorpej */
191 1.7 thorpej if (pcmcia_io_map(pa->pf, PCMCIA_WIDTH_IO16, 0, cfe->iospace[0].length,
192 1.1 enami &psc->sc_pcioh, &psc->sc_io_window)) {
193 1.1 enami printf(": can't map i/o space\n");
194 1.14 enami goto iomap_failed;
195 1.1 enami }
196 1.1 enami
197 1.13 cgd printf(": %s\n", mpp->mpp_product.pp_name);
198 1.1 enami
199 1.18 is /* Read station address from mem or CIS. */
200 1.18 is if (mpp->enet_maddr >= 0) {
201 1.18 is pgea.maddr = mpp->enet_maddr;
202 1.18 is if (mbe_pcmcia_get_enaddr_from_mem(psc, &pgea) != 0) {
203 1.18 is printf("%s: Couldn't get ethernet address "
204 1.18 is "from mem\n", sc->sc_dev.dv_xname);
205 1.18 is goto no_enaddr;
206 1.18 is }
207 1.18 is } else {
208 1.18 is rv = pcmcia_scan_cis(parent,
209 1.18 is mbe_pcmcia_get_enaddr_from_cis, &pgea);
210 1.18 is if (rv == -1) {
211 1.18 is printf("%s: Couldn't read CIS to get ethernet "
212 1.18 is "address\n", sc->sc_dev.dv_xname);
213 1.18 is goto no_enaddr;
214 1.18 is } else if (rv == 0) {
215 1.18 is printf("%s: Couldn't get ethernet address "
216 1.18 is "from CIS\n", sc->sc_dev.dv_xname);
217 1.18 is goto no_enaddr;
218 1.18 is }
219 1.1 enami #ifdef DIAGNOSTIC
220 1.18 is if (rv != 1) {
221 1.18 is printf("%s: pcmcia_scan_cis returns %d\n",
222 1.18 is sc->sc_dev.dv_xname, rv);
223 1.18 is panic("mbe_pcmcia_attach");
224 1.18 is }
225 1.18 is printf("%s: Ethernet address from CIS: %s\n",
226 1.18 is sc->sc_dev.dv_xname, ether_sprintf(pgea.enaddr));
227 1.18 is #endif
228 1.10 enami }
229 1.1 enami
230 1.1 enami /* Perform generic initialization. */
231 1.1 enami mb86960_attach(sc, MB86960_TYPE_86965, pgea.enaddr);
232 1.1 enami
233 1.1 enami mb86960_config(sc, NULL, 0, 0);
234 1.1 enami
235 1.1 enami pcmcia_function_disable(pa->pf);
236 1.14 enami return;
237 1.14 enami
238 1.14 enami no_enaddr:
239 1.14 enami /* Unmap our i/o window. */
240 1.14 enami pcmcia_io_unmap(psc->sc_pf, psc->sc_io_window);
241 1.14 enami
242 1.14 enami iomap_failed:
243 1.14 enami /* Free our i/o space. */
244 1.14 enami pcmcia_io_free(psc->sc_pf, &psc->sc_pcioh);
245 1.14 enami
246 1.14 enami ioalloc_failed:
247 1.14 enami pcmcia_function_disable(pa->pf);
248 1.17 enami
249 1.17 enami enable_failed:
250 1.17 enami psc->sc_io_window = -1;
251 1.4 thorpej }
252 1.4 thorpej
253 1.4 thorpej int
254 1.4 thorpej mbe_pcmcia_detach(self, flags)
255 1.4 thorpej struct device *self;
256 1.4 thorpej int flags;
257 1.4 thorpej {
258 1.5 thorpej struct mbe_pcmcia_softc *psc = (struct mbe_pcmcia_softc *)self;
259 1.12 enami int error;
260 1.12 enami
261 1.14 enami if (psc->sc_io_window == -1)
262 1.14 enami /* Nothing to detach since we've failed to attach. */
263 1.14 enami return (0);
264 1.14 enami
265 1.14 enami error = mb86960_detach(&psc->sc_mb86960);
266 1.12 enami if (error != 0)
267 1.12 enami return (error);
268 1.5 thorpej
269 1.5 thorpej /* Unmap our i/o window. */
270 1.5 thorpej pcmcia_io_unmap(psc->sc_pf, psc->sc_io_window);
271 1.5 thorpej
272 1.5 thorpej /* Free our i/o space. */
273 1.5 thorpej pcmcia_io_free(psc->sc_pf, &psc->sc_pcioh);
274 1.5 thorpej
275 1.4 thorpej return (0);
276 1.1 enami }
277 1.1 enami
278 1.1 enami int
279 1.1 enami mbe_pcmcia_enable(sc)
280 1.1 enami struct mb86960_softc *sc;
281 1.1 enami {
282 1.1 enami struct mbe_pcmcia_softc *psc = (struct mbe_pcmcia_softc *)sc;
283 1.1 enami
284 1.1 enami /* Establish the interrupt handler. */
285 1.1 enami psc->sc_ih = pcmcia_intr_establish(psc->sc_pf, IPL_NET, mb86960_intr,
286 1.1 enami sc);
287 1.1 enami if (psc->sc_ih == NULL) {
288 1.1 enami printf("%s: couldn't establish interrupt handler\n",
289 1.1 enami sc->sc_dev.dv_xname);
290 1.1 enami return (1);
291 1.1 enami }
292 1.1 enami
293 1.17 enami if (pcmcia_function_enable(psc->sc_pf)) {
294 1.17 enami pcmcia_intr_disestablish(psc->sc_pf, psc->sc_ih);
295 1.17 enami return (1);
296 1.17 enami }
297 1.16 enami
298 1.17 enami return (0);
299 1.1 enami }
300 1.1 enami
301 1.1 enami void
302 1.1 enami mbe_pcmcia_disable(sc)
303 1.1 enami struct mb86960_softc *sc;
304 1.1 enami {
305 1.1 enami struct mbe_pcmcia_softc *psc = (struct mbe_pcmcia_softc *)sc;
306 1.1 enami
307 1.1 enami pcmcia_function_disable(psc->sc_pf);
308 1.17 enami pcmcia_intr_disestablish(psc->sc_pf, psc->sc_ih);
309 1.1 enami }
310 1.1 enami
311 1.1 enami int
312 1.18 is mbe_pcmcia_get_enaddr_from_cis(tuple, arg)
313 1.1 enami struct pcmcia_tuple *tuple;
314 1.1 enami void *arg;
315 1.1 enami {
316 1.1 enami struct mbe_pcmcia_get_enaddr_args *p = arg;
317 1.1 enami int i;
318 1.1 enami
319 1.1 enami if (tuple->code == PCMCIA_CISTPL_FUNCE) {
320 1.1 enami if (tuple->length < 2) /* sub code and ether addr length */
321 1.1 enami return (0);
322 1.1 enami
323 1.1 enami if ((pcmcia_tuple_read_1(tuple, 0) !=
324 1.1 enami PCMCIA_TPLFE_TYPE_LAN_NID) ||
325 1.1 enami (pcmcia_tuple_read_1(tuple, 1) != ETHER_ADDR_LEN))
326 1.1 enami return (0);
327 1.1 enami
328 1.1 enami for (i = 0; i < ETHER_ADDR_LEN; i++)
329 1.1 enami p->enaddr[i] = pcmcia_tuple_read_1(tuple, i + 2);
330 1.1 enami return (1);
331 1.1 enami }
332 1.18 is return (0);
333 1.18 is }
334 1.18 is
335 1.18 is int
336 1.18 is mbe_pcmcia_get_enaddr_from_mem(psc, ea)
337 1.18 is struct mbe_pcmcia_softc *psc;
338 1.18 is struct mbe_pcmcia_get_enaddr_args *ea;
339 1.18 is {
340 1.18 is struct mb86960_softc *sc = &psc->sc_mb86960;
341 1.18 is struct pcmcia_mem_handle pcmh;
342 1.18 is bus_addr_t offset;
343 1.18 is int i, mwindow;
344 1.18 is
345 1.18 is if (ea->maddr < 0)
346 1.18 is return (1);
347 1.18 is
348 1.18 is if (pcmcia_mem_alloc(psc->sc_pf, ETHER_ADDR_LEN * 2, &pcmh)) {
349 1.18 is printf("%s: can't alloc mem for enet addr\n",
350 1.18 is sc->sc_dev.dv_xname);
351 1.18 is return (1);
352 1.18 is }
353 1.18 is
354 1.18 is if (pcmcia_mem_map(psc->sc_pf, PCMCIA_MEM_ATTR, ea->maddr,
355 1.18 is ETHER_ADDR_LEN * 2, &pcmh, &offset, &mwindow)) {
356 1.18 is printf("%s: can't map mem for enet addr\n",
357 1.18 is sc->sc_dev.dv_xname);
358 1.18 is return (1);
359 1.18 is }
360 1.18 is
361 1.18 is for (i = 0; i < ETHER_ADDR_LEN; i++)
362 1.18 is ea->enaddr[i] = bus_space_read_1(pcmh.memt, pcmh.memh,
363 1.18 is offset + (i * 2));
364 1.18 is
365 1.18 is pcmcia_mem_unmap(psc->sc_pf, mwindow);
366 1.18 is pcmcia_mem_free(psc->sc_pf, &pcmh);
367 1.18 is
368 1.1 enami return (0);
369 1.1 enami }
370