if_ate.c revision 1.45 1 /* $NetBSD: if_ate.c,v 1.45 2006/08/30 15:46:58 christos Exp $ */
2
3 /*
4 * All Rights Reserved, Copyright (C) Fujitsu Limited 1995
5 *
6 * This software may be used, modified, copied, distributed, and sold, in
7 * both source and binary form provided that the above copyright, these
8 * terms and the following disclaimer are retained. The name of the author
9 * and/or the contributor may not be used to endorse or promote products
10 * derived from this software without specific prior written permission.
11 *
12 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND THE CONTRIBUTOR ``AS IS'' AND
13 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
14 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
15 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR THE CONTRIBUTOR BE LIABLE
16 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
17 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
18 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION.
19 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
20 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
21 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
22 * SUCH DAMAGE.
23 */
24
25 /*
26 * Portions copyright (C) 1993, David Greenman. This software may be used,
27 * modified, copied, distributed, and sold, in both source and binary form
28 * provided that the above copyright and these terms are retained. Under no
29 * circumstances is the author responsible for the proper functioning of this
30 * software, nor does the author assume any responsibility for damages
31 * incurred with its use.
32 */
33
34 #include <sys/cdefs.h>
35 __KERNEL_RCSID(0, "$NetBSD: if_ate.c,v 1.45 2006/08/30 15:46:58 christos Exp $");
36
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/device.h>
40 #include <sys/socket.h>
41 #include <sys/syslog.h>
42
43 #include <net/if.h>
44 #include <net/if_ether.h>
45 #include <net/if_media.h>
46
47 #include <machine/bus.h>
48 #include <machine/intr.h>
49
50 #include <dev/ic/mb86960reg.h>
51 #include <dev/ic/mb86960var.h>
52
53 #include <dev/isa/isavar.h>
54
55 int ate_match(struct device *, struct cfdata *, void *);
56 void ate_attach(struct device *, struct device *, void *);
57
58 struct ate_softc {
59 struct mb86960_softc sc_mb86960; /* real "mb86960" softc */
60
61 /* ISA-specific goo. */
62 void *sc_ih; /* interrupt cookie */
63 };
64
65 CFATTACH_DECL(ate_isa, sizeof(struct ate_softc),
66 ate_match, ate_attach, NULL, NULL);
67
68 struct fe_simple_probe_struct {
69 uint8_t port; /* Offset from the base I/O address. */
70 uint8_t mask; /* Bits to be checked. */
71 uint8_t bits; /* Values to be compared against. */
72 };
73
74 static inline int fe_simple_probe(bus_space_tag_t, bus_space_handle_t,
75 struct fe_simple_probe_struct const *);
76 static int ate_find(bus_space_tag_t, bus_space_handle_t, int *, int *);
77 static int ate_detect(bus_space_tag_t, bus_space_handle_t,
78 uint8_t enaddr[ETHER_ADDR_LEN]);
79
80 static int const ate_iomap[8] = {
81 0x260, 0x280, 0x2A0, 0x240, 0x340, 0x320, 0x380, 0x300
82 };
83 #define NATE_IOMAP (sizeof (ate_iomap) / sizeof (ate_iomap[0]))
84 #define ATE_NPORTS 0x20
85
86 #ifdef ATE_DEBUG
87 #define DPRINTF printf
88 #else
89 #define DPRINTF while (/*CONSTCOND*/0) printf
90 #endif
91
92 /*
93 * Hardware probe routines.
94 */
95
96 /*
97 * Determine if the device is present.
98 */
99 int
100 ate_match(struct device *parent, struct cfdata *match, void *aux)
101 {
102 struct isa_attach_args *ia = aux;
103 bus_space_tag_t iot = ia->ia_iot;
104 bus_space_handle_t ioh;
105 int i, iobase, irq, rv = 0;
106 uint8_t myea[ETHER_ADDR_LEN];
107
108 if (ia->ia_nio < 1)
109 return 0;
110 if (ia->ia_nirq < 1)
111 return 0;
112
113 if (ISA_DIRECT_CONFIG(ia))
114 return 0;
115
116 /* Disallow wildcarded values. */
117 if (ia->ia_io[0].ir_addr == ISA_UNKNOWN_PORT)
118 return 0;
119
120 /*
121 * See if the sepcified address is valid for MB86965A JLI mode.
122 */
123 for (i = 0; i < NATE_IOMAP; i++)
124 if (ate_iomap[i] == ia->ia_io[0].ir_addr)
125 break;
126 if (i == NATE_IOMAP) {
127 DPRINTF("ate_match: unknown iobase 0x%x\n",
128 ia->ia_io[0].ir_addr);
129 return 0;
130 }
131
132 /* Map i/o space. */
133 if (bus_space_map(iot, ia->ia_io[0].ir_addr, ATE_NPORTS, 0, &ioh)) {
134 DPRINTF("ate_match: couldn't map iospace 0x%x\n",
135 ia->ia_io[0].ir_addr);
136 return 0;
137 }
138
139 if (ate_find(iot, ioh, &iobase, &irq) == 0) {
140 DPRINTF("ate_match: ate_find failed\n");
141 goto out;
142 }
143
144 if (iobase != ia->ia_io[0].ir_addr) {
145 DPRINTF("ate_match: unexpected iobase in board: 0x%x\n",
146 iobase);
147 goto out;
148 }
149
150 if (ate_detect(iot, ioh, myea) == 0) { /* XXX necessary? */
151 DPRINTF("ate_match: ate_detect failed\n");
152 goto out;
153 }
154
155 if (ia->ia_irq[0].ir_irq != ISA_UNKNOWN_IRQ) {
156 if (ia->ia_irq[0].ir_irq != irq) {
157 printf("ate_match: irq mismatch; "
158 "kernel configured %d != board configured %d\n",
159 ia->ia_irq[0].ir_irq, irq);
160 goto out;
161 }
162 } else
163 ia->ia_irq[0].ir_irq = irq;
164
165 ia->ia_nio = 1;
166 ia->ia_io[0].ir_size = ATE_NPORTS;
167
168 ia->ia_nirq = 1;
169
170 ia->ia_niomem = 0;
171 ia->ia_ndrq = 0;
172
173 rv = 1;
174
175 out:
176 bus_space_unmap(iot, ioh, ATE_NPORTS);
177 return rv;
178 }
179
180 /*
181 * Check for specific bits in specific registers have specific values.
182 */
183 static inline int
184 fe_simple_probe(bus_space_tag_t iot, bus_space_handle_t ioh,
185 struct fe_simple_probe_struct const *sp)
186 {
187 uint8_t val;
188 struct fe_simple_probe_struct const *p;
189
190 for (p = sp; p->mask != 0; p++) {
191 val = bus_space_read_1(iot, ioh, p->port);
192 if ((val & p->mask) != p->bits) {
193 DPRINTF("fe_simple_probe: %x & %x != %x\n",
194 val, p->mask, p->bits);
195 return 0;
196 }
197 }
198
199 return 1;
200 }
201
202 /*
203 * Hardware (vendor) specific probe routines.
204 */
205
206 /*
207 * Probe and initialization for Allied-Telesis AT1700/RE2000 series.
208 */
209 static int
210 ate_find(bus_space_tag_t iot, bus_space_handle_t ioh, int *iobase, int *irq)
211 {
212 uint8_t eeprom[FE_EEPROM_SIZE];
213 int n;
214
215 static int const irqmap[4][4] = {
216 { 3, 4, 5, 9 },
217 { 10, 11, 12, 15 },
218 { 3, 11, 5, 15 },
219 { 10, 11, 14, 15 },
220 };
221 static struct fe_simple_probe_struct const probe_table[] = {
222 { FE_DLCR2, 0x70, 0x00 },
223 { FE_DLCR4, 0x08, 0x00 },
224 { FE_DLCR5, 0x80, 0x00 },
225 #if 0
226 { FE_BMPR16, 0x1B, 0x00 },
227 { FE_BMPR17, 0x7F, 0x00 },
228 #endif
229 { 0, 0x00, 0x00 },
230 };
231
232 #if ATE_DEBUG >= 4
233 log(LOG_INFO, "ate_find: probe (0x%x) for ATE\n", iobase);
234 #if 0
235 fe_dump(LOG_INFO, sc);
236 #endif
237 #endif
238
239 /*
240 * We should test if MB86965A is on the base address now.
241 * Unfortunately, it is very hard to probe it reliably, since
242 * we have no way to reset the chip under software control.
243 * On cold boot, we could check the "signature" bit patterns
244 * described in the Fujitsu document. On warm boot, however,
245 * we can predict almost nothing about register values.
246 */
247 if (!fe_simple_probe(iot, ioh, probe_table))
248 return 0;
249
250 /* Check if our I/O address matches config info on 86965. */
251 n = (bus_space_read_1(iot, ioh, FE_BMPR19) & FE_B19_ADDR)
252 >> FE_B19_ADDR_SHIFT;
253 *iobase = ate_iomap[n];
254
255 /*
256 * We are now almost sure we have an AT1700 at the given
257 * address. So, read EEPROM through 86965. We have to write
258 * into LSI registers to read from EEPROM. I want to avoid it
259 * at this stage, but I cannot test the presence of the chip
260 * any further without reading EEPROM. FIXME.
261 */
262 mb86965_read_eeprom(iot, ioh, eeprom);
263
264 /* Make sure that config info in EEPROM and 86965 agree. */
265 if (eeprom[FE_EEPROM_CONF] != bus_space_read_1(iot, ioh, FE_BMPR19)) {
266 #ifdef DIAGNOSTIC
267 printf("ate_find: "
268 "incorrect configuration in eeprom and chip\n");
269 #endif
270 return 0;
271 }
272
273 /*
274 * Try to determine IRQ settings.
275 * Different models use different ranges of IRQs.
276 */
277 n = (bus_space_read_1(iot, ioh, FE_BMPR19) & FE_B19_IRQ)
278 >> FE_B19_IRQ_SHIFT;
279 switch (eeprom[FE_ATI_EEP_REVISION] & 0xf0) {
280 case 0x30:
281 *irq = irqmap[3][n];
282 break;
283 case 0x10:
284 case 0x50:
285 *irq = irqmap[2][n];
286 break;
287 case 0x40:
288 case 0x60:
289 if (eeprom[FE_ATI_EEP_MAGIC] & 0x04) {
290 *irq = irqmap[1][n];
291 break;
292 }
293 default:
294 *irq = irqmap[0][n];
295 break;
296 }
297
298 return 1;
299 }
300
301 /*
302 * Determine type and ethernet address.
303 */
304 static int
305 ate_detect(bus_space_tag_t iot, bus_space_handle_t ioh,
306 uint8_t enaddr[ETHER_ADDR_LEN])
307 {
308 uint8_t eeprom[FE_EEPROM_SIZE];
309 int type;
310
311 /* Get our station address from EEPROM. */
312 mb86965_read_eeprom(iot, ioh, eeprom);
313 memcpy(enaddr, eeprom + FE_ATI_EEP_ADDR, ETHER_ADDR_LEN);
314
315 /* Make sure we got a valid station address. */
316 if ((enaddr[0] & 0x03) != 0x00 ||
317 (enaddr[0] == 0x00 && enaddr[1] == 0x00 && enaddr[2] == 0x00)) {
318 DPRINTF("ate_detect: invalid ethernet address\n");
319 return 0;
320 }
321
322 /*
323 * Determine the card type.
324 */
325 switch (eeprom[FE_ATI_EEP_MODEL]) {
326 case FE_ATI_MODEL_AT1700T:
327 type = FE_TYPE_AT1700T;
328 break;
329 case FE_ATI_MODEL_AT1700BT:
330 type = FE_TYPE_AT1700BT;
331 break;
332 case FE_ATI_MODEL_AT1700FT:
333 type = FE_TYPE_AT1700FT;
334 break;
335 case FE_ATI_MODEL_AT1700AT:
336 type = FE_TYPE_AT1700AT;
337 break;
338 default:
339 type = FE_TYPE_AT_UNKNOWN;
340 break;
341 }
342
343 return type;
344 }
345
346 void
347 ate_attach(struct device *parent, struct device *self, void *aux)
348 {
349 struct ate_softc *isc = (struct ate_softc *)self;
350 struct mb86960_softc *sc = &isc->sc_mb86960;
351 struct isa_attach_args *ia = aux;
352 bus_space_tag_t iot = ia->ia_iot;
353 bus_space_handle_t ioh;
354 uint8_t myea[ETHER_ADDR_LEN];
355 const char *typestr;
356 int type;
357
358 /* Map i/o space. */
359 if (bus_space_map(iot, ia->ia_io[0].ir_addr, ATE_NPORTS, 0, &ioh)) {
360 printf(": can't map i/o space\n");
361 return;
362 }
363
364 sc->sc_bst = iot;
365 sc->sc_bsh = ioh;
366
367 /* Determine the card type and get ethernet address. */
368 type = ate_detect(iot, ioh, myea);
369 switch (type) {
370 case FE_TYPE_AT1700T:
371 typestr = "AT-1700T/RE2001";
372 break;
373 case FE_TYPE_AT1700BT:
374 typestr = "AT-1700BT/RE2003";
375 break;
376 case FE_TYPE_AT1700FT:
377 typestr = "AT-1700FT/RE2009";
378 break;
379 case FE_TYPE_AT1700AT:
380 typestr = "AT-1700AT/RE2005";
381 break;
382 case FE_TYPE_AT_UNKNOWN:
383 typestr = "unknown AT-1700/RE2000";
384 break;
385
386 default:
387 /* Unknown card type: maybe a new model, but... */
388 printf(": where did the card go?!\n");
389 panic("unknown card");
390 }
391
392 printf(": %s Ethernet\n", typestr);
393
394 /* This interface is always enabled. */
395 sc->sc_stat |= FE_STAT_ENABLED;
396
397 /*
398 * Do generic MB86960 attach.
399 */
400 mb86960_attach(sc, myea);
401
402 mb86960_config(sc, NULL, 0, 0);
403
404 /* Establish the interrupt handler. */
405 isc->sc_ih = isa_intr_establish(ia->ia_ic, ia->ia_irq[0].ir_irq,
406 IST_EDGE, IPL_NET, mb86960_intr, sc);
407 if (isc->sc_ih == NULL)
408 printf("%s: couldn't establish interrupt handler\n",
409 sc->sc_dev.dv_xname);
410 }
411