if_mbe_pcmcia.c revision 1.3 1 /* $NetBSD: if_mbe_pcmcia.c,v 1.3 1998/07/19 17:28:16 christos 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
61 struct mbe_pcmcia_softc {
62 struct mb86960_softc sc_mb86960; /* real "mb" softc */
63
64 /* PCMCIA-specific goo. */
65 struct pcmcia_io_handle sc_pcioh; /* PCMCIA i/o space info */
66 int sc_io_window; /* our i/o window */
67 void *sc_ih; /* interrupt cookie */
68 struct pcmcia_function *sc_pf; /* our PCMCIA function */
69 };
70
71 struct cfattach mbe_pcmcia_ca = {
72 sizeof(struct mbe_pcmcia_softc), mbe_pcmcia_match, mbe_pcmcia_attach
73 };
74
75 #if NetBSD <= 199712
76 struct cfdriver mbe_cd = { /* XXX shouldn't be here */
77 NULL, "mbe", DV_IFNET
78 };
79 #endif
80
81 int mbe_pcmcia_enable __P((struct mb86960_softc *));
82 void mbe_pcmcia_disable __P((struct mb86960_softc *));
83
84 struct mbe_pcmcia_get_enaddr_args {
85 int got_enaddr;
86 u_int8_t enaddr[ETHER_ADDR_LEN];
87 };
88 int mbe_pcmcia_get_enaddr __P((struct pcmcia_tuple *, void *));
89
90 int
91 mbe_pcmcia_match(parent, match, aux)
92 struct device *parent;
93 struct cfdata *match;
94 void *aux;
95 {
96 struct pcmcia_attach_args *pa = aux;
97
98 if (pa->manufacturer == PCMCIA_VENDOR_TDK) {
99 switch (pa->product) {
100 case PCMCIA_PRODUCT_TDK_LAK_CD021BX:
101 if (pa->pf->number == 0)
102 return (1);
103 break;
104 #if 0 /* XXX Is this card mb86960 based one? */
105 case PCMCIA_PRODUCT_TDK_DFL9610:
106 if (pa->pf->number == 1)
107 return (1);
108 break;
109 #endif
110 }
111 }
112
113 return (0);
114 }
115
116 void
117 mbe_pcmcia_attach(parent, self, aux)
118 struct device *parent, *self;
119 void *aux;
120 {
121 struct mbe_pcmcia_softc *psc = (struct mbe_pcmcia_softc *)self;
122 struct mb86960_softc *sc = &psc->sc_mb86960;
123 struct pcmcia_attach_args *pa = aux;
124 struct pcmcia_config_entry *cfe;
125 struct mbe_pcmcia_get_enaddr_args pgea;
126 const char *model;
127
128 psc->sc_pf = pa->pf;
129 cfe = pa->pf->cfe_head.sqh_first;
130
131 /* Enable the card. */
132 pcmcia_function_init(pa->pf, cfe);
133 if (pcmcia_function_enable(pa->pf)) {
134 printf(": function enable failed\n");
135 return;
136 }
137
138 /* Allocate and map i/o space for the card. */
139 if (pcmcia_io_alloc(pa->pf, cfe->iospace[0].start,
140 cfe->iospace[0].length, cfe->iospace[0].length, &psc->sc_pcioh)) {
141 printf(": can't allocate i/o space\n");
142 return;
143 }
144
145 sc->sc_bst = psc->sc_pcioh.iot;
146 sc->sc_bsh = psc->sc_pcioh.ioh;
147
148 sc->sc_enable = mbe_pcmcia_enable;
149 sc->sc_disable = mbe_pcmcia_disable;
150
151 if (pcmcia_io_map(pa->pf, (cfe->flags & PCMCIA_CFE_IO16) ?
152 PCMCIA_WIDTH_IO16 : PCMCIA_WIDTH_IO8, 0, cfe->iospace[0].length,
153 &psc->sc_pcioh, &psc->sc_io_window)) {
154 printf(": can't map i/o space\n");
155 return;
156 }
157
158 switch (pa->product) {
159 case PCMCIA_PRODUCT_TDK_LAK_CD021BX:
160 model = PCMCIA_STR_TDK_LAK_CD021BX;
161 break;
162 case PCMCIA_PRODUCT_TDK_DFL9610:
163 model = PCMCIA_STR_TDK_DFL9610;
164 break;
165
166 default:
167 printf("%s: Unknown MB86960 PCMCIA ethernet card (%d)\n",
168 sc->sc_dev.dv_xname, pa->product);
169 panic("unknown card");
170 }
171 printf(": %s\n", model);
172
173 /* Read station address. */
174 pgea.got_enaddr = 0;
175 if (pcmcia_scan_cis(parent, mbe_pcmcia_get_enaddr, &pgea) == -1) {
176 printf("%s: Couldn't read CIS to get ethernet address\n",
177 sc->sc_dev.dv_xname);
178 return;
179 } else if (!pgea.got_enaddr) {
180 printf("%s: Couldn't get ethernet address from CIS\n",
181 sc->sc_dev.dv_xname);
182 return;
183 } else
184 #ifdef DIAGNOSTIC
185 printf("%s: Ethernet address from CIS: %s\n",
186 sc->sc_dev.dv_xname, ether_sprintf(pgea.enaddr))
187 #endif
188 ;
189
190 /* Perform generic initialization. */
191 mb86960_attach(sc, MB86960_TYPE_86965, pgea.enaddr);
192
193 mb86960_config(sc, NULL, 0, 0);
194
195 pcmcia_function_disable(pa->pf);
196 }
197
198 int
199 mbe_pcmcia_enable(sc)
200 struct mb86960_softc *sc;
201 {
202 struct mbe_pcmcia_softc *psc = (struct mbe_pcmcia_softc *)sc;
203
204 /* Establish the interrupt handler. */
205 psc->sc_ih = pcmcia_intr_establish(psc->sc_pf, IPL_NET, mb86960_intr,
206 sc);
207 if (psc->sc_ih == NULL) {
208 printf("%s: couldn't establish interrupt handler\n",
209 sc->sc_dev.dv_xname);
210 return (1);
211 }
212
213 return (pcmcia_function_enable(psc->sc_pf));
214 }
215
216 void
217 mbe_pcmcia_disable(sc)
218 struct mb86960_softc *sc;
219 {
220 struct mbe_pcmcia_softc *psc = (struct mbe_pcmcia_softc *)sc;
221
222 pcmcia_function_disable(psc->sc_pf);
223
224 pcmcia_intr_disestablish(psc->sc_pf, psc->sc_ih);
225 }
226
227 int
228 mbe_pcmcia_get_enaddr(tuple, arg)
229 struct pcmcia_tuple *tuple;
230 void *arg;
231 {
232 struct mbe_pcmcia_get_enaddr_args *p = arg;
233 int i;
234
235 if (tuple->code == PCMCIA_CISTPL_FUNCE) {
236 if (tuple->length < 2) /* sub code and ether addr length */
237 return (0);
238
239 if ((pcmcia_tuple_read_1(tuple, 0) !=
240 PCMCIA_TPLFE_TYPE_LAN_NID) ||
241 (pcmcia_tuple_read_1(tuple, 1) != ETHER_ADDR_LEN))
242 return (0);
243
244 for (i = 0; i < ETHER_ADDR_LEN; i++)
245 p->enaddr[i] = pcmcia_tuple_read_1(tuple, i + 2);
246 p->got_enaddr = 1;
247 return (1);
248 }
249 return (0);
250 }
251