if_ep_pcmcia.c revision 1.1.2.11 1 /* $NetBSD: if_ep_pcmcia.c,v 1.1.2.11 1997/10/16 17:30:34 thorpej Exp $ */
2
3 #include "bpfilter.h"
4
5 #include <sys/param.h>
6 #include <sys/systm.h>
7 #include <sys/mbuf.h>
8 #include <sys/socket.h>
9 #include <sys/ioctl.h>
10 #include <sys/errno.h>
11 #include <sys/syslog.h>
12 #include <sys/select.h>
13 #include <sys/device.h>
14
15 #include <net/if.h>
16 #include <net/if_dl.h>
17 #include <net/if_ether.h>
18 #include <net/if_media.h>
19
20 #ifdef INET
21 #include <netinet/in.h>
22 #include <netinet/in_systm.h>
23 #include <netinet/in_var.h>
24 #include <netinet/ip.h>
25 #include <netinet/if_inarp.h>
26 #endif
27
28 #ifdef NS
29 #include <netns/ns.h>
30 #include <netns/ns_if.h>
31 #endif
32
33 #if NBPFILTER > 0
34 #include <net/bpf.h>
35 #include <net/bpfdesc.h>
36 #endif
37
38 #include <machine/cpu.h>
39 #include <machine/bus.h>
40 #include <machine/intr.h>
41
42 #include <dev/ic/elink3var.h>
43 #include <dev/ic/elink3reg.h>
44
45 #include <dev/pcmcia/pcmciareg.h>
46 #include <dev/pcmcia/pcmciavar.h>
47
48 #define PCMCIA_MANUFACTURER_3COM 0x101
49 #define PCMCIA_PRODUCT_3COM_3C562 0x562
50 #define PCMCIA_PRODUCT_3COM_3C589 0x589
51
52 #ifdef __BROKEN_INDIRECT_CONFIG
53 int ep_pcmcia_match __P((struct device *, void *, void *));
54 #else
55 int ep_pcmcia_match __P((struct device *, struct cfdata *, void *));
56 #endif
57 void ep_pcmcia_attach __P((struct device *, struct device *, void *));
58
59 int ep_pcmcia_get_enaddr __P((struct pcmcia_tuple *, void *));
60 int ep_pcmcia_enable __P((struct ep_softc *));
61 void ep_pcmcia_disable __P((struct ep_softc *));
62
63 struct ep_pcmcia_softc {
64 struct ep_softc sc_ep; /* real "ep" softc */
65
66 /* PCMCIA-specific goo */
67 struct pcmcia_io_handle sc_pcioh; /* PCMCIA i/o space info */
68 int sc_io_window; /* our i/o window */
69 struct pcmcia_function *sc_pf; /* our PCMCIA function */
70 };
71
72 struct cfattach ep_pcmcia_ca = {
73 sizeof(struct ep_pcmcia_softc), ep_pcmcia_match, ep_pcmcia_attach
74 };
75
76 int
77 ep_pcmcia_match(parent, match, aux)
78 struct device *parent;
79 #ifdef __BROKEN_INDIRECT_CONFIG
80 void *match;
81 #else
82 struct cfdata *cf;
83 #endif
84 void *aux;
85 {
86 struct pcmcia_attach_args *pa = aux;
87
88 if ((pa->manufacturer == PCMCIA_MANUFACTURER_3COM) &&
89 (pa->product == PCMCIA_PRODUCT_3COM_3C562) &&
90 (pa->pf->number == 0))
91 return (1);
92
93 if ((pa->manufacturer == PCMCIA_MANUFACTURER_3COM) &&
94 (pa->product == PCMCIA_PRODUCT_3COM_3C589) &&
95 (pa->pf->number == 0))
96 return (1);
97
98 return (0);
99 }
100
101 int
102 ep_pcmcia_enable(sc)
103 struct ep_softc *sc;
104 {
105 struct ep_pcmcia_softc *psc = (struct ep_pcmcia_softc *) sc;
106
107 /* establish the interrupt. */
108 sc->sc_ih = pcmcia_intr_establish(psc->sc_pf, IPL_NET, epintr, sc);
109 if (sc->sc_ih == NULL) {
110 printf("%s: couldn't establish interrupt\n",
111 sc->sc_dev.dv_xname);
112 return (1);
113 }
114
115 return (pcmcia_function_enable(psc->sc_pf));
116 }
117
118 void
119 ep_pcmcia_disable(sc)
120 struct ep_softc *sc;
121 {
122 struct ep_pcmcia_softc *psc = (struct ep_pcmcia_softc *) sc;
123
124 pcmcia_function_disable(psc->sc_pf);
125
126 pcmcia_intr_disestablish(psc->sc_pf, sc->sc_ih);
127 }
128
129 void
130 ep_pcmcia_attach(parent, self, aux)
131 struct device *parent, *self;
132 void *aux;
133 {
134 struct ep_pcmcia_softc *psc = (void *) self;
135 struct ep_softc *sc = &psc->sc_ep;
136 struct pcmcia_attach_args *pa = aux;
137 struct pcmcia_config_entry *cfe;
138 int i;
139 u_int8_t myla[ETHER_ADDR_LEN];
140 u_int8_t *enaddr;
141 char *model;
142
143 psc->sc_pf = pa->pf;
144 cfe = pa->pf->cfe_head.sqh_first;
145
146 /* Enable the card. */
147 pcmcia_function_init(pa->pf, cfe);
148 if (pcmcia_function_enable(pa->pf))
149 printf(": function enable failed\n");
150
151 sc->enabled = 1;
152 /* turn off the bit which disables the modem */
153 if (pa->product == PCMCIA_PRODUCT_3COM_3C562) {
154 int reg;
155
156 reg = pcmcia_ccr_read(pa->pf, PCMCIA_CCR_OPTION);
157 reg &= ~0x08;
158 pcmcia_ccr_write(pa->pf, PCMCIA_CCR_OPTION, reg);
159 }
160 if (cfe->num_memspace != 0)
161 printf(": unexpected number of memory spaces %d should be 0\n",
162 cfe->num_memspace);
163
164 if (cfe->num_iospace != 1)
165 printf(": unexpected number of I/O spaces %d should be 1\n",
166 cfe->num_iospace);
167
168 /*
169 * XXX there's a comment in the linux driver about the io address
170 * having to be between 0x00 and 0x70 mod 0x100. weird.
171 */
172
173 /* XXX Will have to fix this!! */
174
175 if (pa->product == PCMCIA_PRODUCT_3COM_3C562) {
176 for (i = 0x300; i < 0x1000;
177 i += ((i % 0x100) == 0x70) ? 0x90 : 0x10) {
178 if (pcmcia_io_alloc(pa->pf, i, cfe->iospace[0].length,
179 0, &psc->sc_pcioh) == 0)
180 break;
181 }
182 if (i == 0x1000) {
183 printf(": can't allocate i/o space\n");
184 return;
185 }
186 } else {
187 if (pcmcia_io_alloc(pa->pf, 0, cfe->iospace[0].length,
188 cfe->iospace[0].length, &psc->sc_pcioh))
189 printf(": can't allocate i/o space\n");
190 }
191
192 sc->sc_iot = psc->sc_pcioh.iot;
193 sc->sc_ioh = psc->sc_pcioh.ioh;
194
195 if (pcmcia_io_map(pa->pf, ((cfe->flags & PCMCIA_CFE_IO16) ?
196 PCMCIA_WIDTH_IO16 : PCMCIA_WIDTH_IO8), 0, cfe->iospace[0].length,
197 &psc->sc_pcioh, &psc->sc_io_window)) {
198 printf(": can't map i/o space\n");
199 return;
200 }
201 if (pa->product == PCMCIA_PRODUCT_3COM_3C562) {
202 if (pcmcia_scan_cis(parent, ep_pcmcia_get_enaddr, myla)) {
203 printf(": can't read ethernet address from CIS\n");
204 return;
205 }
206 enaddr = myla;
207 } else {
208 enaddr = NULL;
209 }
210
211 sc->bustype = EP_BUS_PCMCIA;
212
213 switch (pa->product) {
214 case PCMCIA_PRODUCT_3COM_3C589:
215 model = "3Com 3C589 Ethernet";
216 break;
217 case PCMCIA_PRODUCT_3COM_3C562:
218 model = "3Com 3C562 Ethernet";
219 break;
220 default:
221 model = "3Com Ethernet, model unknown";
222 break;
223 }
224
225 printf(": %s\n", model);
226
227 sc->enable = ep_pcmcia_enable;
228 sc->disable = ep_pcmcia_disable;
229
230 epconfig(sc, EP_CHIPSET_3C509, enaddr);
231
232 sc->enabled = 0;
233
234 pcmcia_function_disable(pa->pf);
235 }
236
237 int
238 ep_pcmcia_get_enaddr(tuple, arg)
239 struct pcmcia_tuple *tuple;
240 void *arg;
241 {
242 u_int8_t *myla = arg;
243 int i;
244
245 /* this is 3c562 magic */
246
247 if (tuple->code == 0x88) {
248 if (tuple->length < ETHER_ADDR_LEN)
249 return (0);
250
251 for (i = 0; i < ETHER_ADDR_LEN; i += 2) {
252 myla[i] = pcmcia_tuple_read_1(tuple, i + 1);
253 myla[i + 1] = pcmcia_tuple_read_1(tuple, i);
254 }
255
256 return (1);
257 }
258 return (0);
259 }
260