if_ep_pcmcia.c revision 1.1.2.9 1 /* $NetBSD: if_ep_pcmcia.c,v 1.1.2.9 1997/10/14 04:11:45 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 = (struct pcmcia_attach_args *) 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 return (pcmcia_function_enable(psc->sc_pf));
108 }
109
110 void
111 ep_pcmcia_disable(sc)
112 struct ep_softc *sc;
113 {
114 struct ep_pcmcia_softc *psc = (struct ep_pcmcia_softc *) sc;
115
116 pcmcia_function_disable(psc->sc_pf);
117 }
118
119 void
120 ep_pcmcia_attach(parent, self, aux)
121 struct device *parent, *self;
122 void *aux;
123 {
124 struct ep_pcmcia_softc *psc = (void *) self;
125 struct ep_softc *sc = &psc->sc_ep;
126 struct pcmcia_attach_args *pa = aux;
127 struct pcmcia_config_entry *cfe;
128 int i;
129 u_int8_t myla[ETHER_ADDR_LEN];
130 u_int8_t *enaddr;
131 char *model;
132
133 psc->sc_pf = pa->pf;
134 cfe = pa->pf->cfe_head.sqh_first;
135
136 /* Enable the card. */
137 pcmcia_function_init(pa->pf, cfe);
138 if (pcmcia_function_enable(pa->pf))
139 printf(": function enable failed\n");
140
141 sc->enabled = 1;
142 /* turn off the bit which disables the modem */
143 if (pa->product == PCMCIA_PRODUCT_3COM_3C562) {
144 int reg;
145
146 reg = pcmcia_ccr_read(pa->pf, PCMCIA_CCR_OPTION);
147 reg &= ~0x08;
148 pcmcia_ccr_write(pa->pf, PCMCIA_CCR_OPTION, reg);
149 }
150 if (cfe->num_memspace != 0)
151 printf(": unexpected number of memory spaces %d should be 0\n",
152 cfe->num_memspace);
153
154 if (cfe->num_iospace != 1)
155 printf(": unexpected number of I/O spaces %d should be 1\n",
156 cfe->num_iospace);
157
158 /*
159 * XXX there's a comment in the linux driver about the io address
160 * having to be between 0x00 and 0x70 mod 0x100. weird.
161 */
162
163 /* XXX Will have to fix this!! */
164
165 if (pa->product == PCMCIA_PRODUCT_3COM_3C562) {
166 for (i = 0x300; i < 0x1000;
167 i += ((i % 0x100) == 0x70) ? 0x90 : 0x10) {
168 if (pcmcia_io_alloc(pa->pf, i, cfe->iospace[0].length,
169 0, &psc->sc_pcioh) == 0)
170 break;
171 }
172 if (i == 0x1000) {
173 printf(": can't allocate i/o space\n");
174 return;
175 }
176 } else {
177 if (pcmcia_io_alloc(pa->pf, 0, cfe->iospace[0].length,
178 cfe->iospace[0].length, &psc->sc_pcioh))
179 printf(": can't allocate i/o space\n");
180 }
181
182 sc->sc_iot = psc->sc_pcioh.iot;
183 sc->sc_ioh = psc->sc_pcioh.ioh;
184
185 if (pcmcia_io_map(pa->pf, ((cfe->flags & PCMCIA_CFE_IO16) ?
186 PCMCIA_WIDTH_IO16 : PCMCIA_WIDTH_IO8), 0, cfe->iospace[0].length,
187 &psc->sc_pcioh, &psc->sc_io_window)) {
188 printf(": can't map i/o space\n");
189 return;
190 }
191 if (pa->product == PCMCIA_PRODUCT_3COM_3C562) {
192 if (pcmcia_scan_cis(parent, ep_pcmcia_get_enaddr, myla)) {
193 printf(": can't read ethernet address from CIS\n");
194 return;
195 }
196 enaddr = myla;
197 } else {
198 enaddr = NULL;
199 }
200
201 sc->bustype = EP_BUS_PCMCIA;
202
203 switch (pa->product) {
204 case PCMCIA_PRODUCT_3COM_3C589:
205 model = "3Com 3C589 Ethernet";
206 break;
207 case PCMCIA_PRODUCT_3COM_3C562:
208 model = "3Com 3C562 Ethernet";
209 break;
210 default:
211 model = "3Com Ethernet, model unknown";
212 break;
213 }
214
215 printf(": %s\n", model);
216
217 sc->enable = ep_pcmcia_enable;
218 sc->disable = ep_pcmcia_disable;
219
220 epconfig(sc, EP_CHIPSET_3C509, enaddr);
221
222 /* establish the interrupt. */
223 sc->sc_ih = pcmcia_intr_establish(pa->pf, IPL_NET, epintr, sc);
224 if (sc->sc_ih == NULL) {
225 printf("%s: couldn't establish interrupt\n",
226 sc->sc_dev.dv_xname);
227 return;
228 }
229 sc->enabled = 0;
230
231 pcmcia_function_disable(pa->pf);
232 }
233
234 int
235 ep_pcmcia_get_enaddr(tuple, arg)
236 struct pcmcia_tuple *tuple;
237 void *arg;
238 {
239 u_int8_t *myla = (u_int8_t *) arg;
240 int i;
241
242 /* this is 3c562 magic */
243
244 if (tuple->code == 0x88) {
245 if (tuple->length < ETHER_ADDR_LEN)
246 return (0);
247
248 for (i = 0; i < ETHER_ADDR_LEN; i += 2) {
249 myla[i] = pcmcia_tuple_read_1(tuple, i + 1);
250 myla[i + 1] = pcmcia_tuple_read_1(tuple, i);
251 }
252
253 return (1);
254 }
255 return (0);
256 }
257