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