if_ate.c revision 1.30 1 /* $NetBSD: if_ate.c,v 1.30 2002/01/07 21:47:06 thorpej 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.30 2002/01/07 21:47:06 thorpej 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 #include <dev/ic/ate_subr.h>
53
54 #include <dev/isa/isavar.h>
55 #include <dev/isa/if_fereg.h> /* XXX */
56
57 int ate_match __P((struct device *, struct cfdata *, void *));
58 void ate_attach __P((struct device *, struct device *, void *));
59
60 struct ate_softc {
61 struct mb86960_softc sc_mb86960; /* real "mb86960" softc */
62
63 /* ISA-specific goo. */
64 void *sc_ih; /* interrupt cookie */
65 };
66
67 struct cfattach ate_isa_ca = {
68 sizeof(struct ate_softc), ate_match, ate_attach
69 };
70
71 #if NetBSD <= 199712
72 struct cfdriver ate_isa_cd = {
73 NULL, "ate", DV_IFNET
74 };
75 #endif
76
77 struct fe_simple_probe_struct {
78 u_char port; /* Offset from the base I/O address. */
79 u_char mask; /* Bits to be checked. */
80 u_char bits; /* Values to be compared against. */
81 };
82
83 static __inline__ int fe_simple_probe __P((bus_space_tag_t,
84 bus_space_handle_t, struct fe_simple_probe_struct const *));
85 static int ate_find __P((bus_space_tag_t, bus_space_handle_t, int *,
86 int *));
87 static int ate_detect __P((bus_space_tag_t, bus_space_handle_t,
88 u_int8_t enaddr[ETHER_ADDR_LEN]));
89
90 static int const ate_iomap[8] = {
91 0x260, 0x280, 0x2A0, 0x240, 0x340, 0x320, 0x380, 0x300
92 };
93 #define NATE_IOMAP (sizeof (ate_iomap) / sizeof (ate_iomap[0]))
94 #define ATE_NPORTS 0x20
95
96 /*
97 * Hardware probe routines.
98 */
99
100 /*
101 * Determine if the device is present.
102 */
103 int
104 ate_match(parent, match, aux)
105 struct device *parent;
106 struct cfdata *match;
107 void *aux;
108 {
109 struct isa_attach_args *ia = aux;
110 bus_space_tag_t iot = ia->ia_iot;
111 bus_space_handle_t ioh;
112 int i, iobase, irq, rv = 0;
113 u_int8_t myea[ETHER_ADDR_LEN];
114
115 if (ia->ia_nio < 1)
116 return (0);
117 if (ia->ia_nirq < 1)
118 return (0);
119
120 if (ISA_DIRECT_CONFIG(ia))
121 return (0);
122
123 /* Disallow wildcarded values. */
124 if (ia->ia_io[0].ir_addr == ISACF_PORT_DEFAULT)
125 return (0);
126
127 /*
128 * See if the sepcified address is valid for MB86965A JLI mode.
129 */
130 for (i = 0; i < NATE_IOMAP; i++)
131 if (ate_iomap[i] == ia->ia_io[0].ir_addr)
132 break;
133 if (i == NATE_IOMAP) {
134 #ifdef ATE_DEBUG
135 printf("ate_match: unknown iobase 0x%x\n", ia->ia_iobase);
136 #endif
137 return (0);
138 }
139
140 /* Map i/o space. */
141 if (bus_space_map(iot, ia->ia_io[0].ir_addr, ATE_NPORTS, 0, &ioh)) {
142 #ifdef ATE_DEBUG
143 printf("ate_match: couldn't map iospace 0x%x\n",
144 ia->ia_io[0].ir_addr);
145 #endif
146 return (0);
147 }
148
149 if (ate_find(iot, ioh, &iobase, &irq) == 0) {
150 #ifdef ATE_DEBUG
151 printf("ate_match: ate_find failed\n");
152 #endif
153 goto out;
154 }
155
156 if (iobase != ia->ia_io[0].ir_addr) {
157 #ifdef ATE_DEBUG
158 printf("ate_match: unexpected iobase in board: 0x%x\n",
159 ia->ia_iobase);
160 #endif
161 goto out;
162 }
163
164 if (ate_detect(iot, ioh, myea) == 0) { /* XXX necessary? */
165 #ifdef ATE_DEBUG
166 printf("ate_match: ate_detect failed\n");
167 #endif
168 goto out;
169 }
170
171 if (ia->ia_irq[0].ir_irq != ISACF_IRQ_DEFAULT) {
172 if (ia->ia_irq[0].ir_irq != irq) {
173 printf("ate_match: irq mismatch; "
174 "kernel configured %d != board configured %d\n",
175 ia->ia_irq[0].ir_irq, irq);
176 goto out;
177 }
178 } else
179 ia->ia_irq[0].ir_irq = irq;
180
181 ia->ia_nio = 1;
182 ia->ia_io[0].ir_size = ATE_NPORTS;
183
184 ia->ia_nirq = 1;
185
186 ia->ia_niomem = 0;
187 ia->ia_ndrq = 0;
188
189 rv = 1;
190
191 out:
192 bus_space_unmap(iot, ioh, ATE_NPORTS);
193 return (rv);
194 }
195
196 /*
197 * Check for specific bits in specific registers have specific values.
198 */
199 static __inline__ int
200 fe_simple_probe (iot, ioh, sp)
201 bus_space_tag_t iot;
202 bus_space_handle_t ioh;
203 struct fe_simple_probe_struct const *sp;
204 {
205 u_int8_t val;
206 struct fe_simple_probe_struct const *p;
207
208 for (p = sp; p->mask != 0; p++) {
209 val = bus_space_read_1(iot, ioh, p->port);
210 if ((val & p->mask) != p->bits) {
211 #ifdef ATE_DEBUG
212 printf("fe_simple_probe: %x & %x != %x\n",
213 val, p->mask, p->bits);
214 #endif
215 return (0);
216 }
217 }
218
219 return (1);
220 }
221
222 /*
223 * Hardware (vendor) specific probe routines.
224 */
225
226 /*
227 * Probe and initialization for Allied-Telesis AT1700/RE2000 series.
228 */
229 static int
230 ate_find(iot, ioh, iobase, irq)
231 bus_space_tag_t iot;
232 bus_space_handle_t ioh;
233 int *iobase, *irq;
234 {
235 u_char eeprom[FE_EEPROM_SIZE];
236 int n;
237
238 static int const irqmap[4][4] = {
239 { 3, 4, 5, 9 },
240 { 10, 11, 12, 15 },
241 { 3, 11, 5, 15 },
242 { 10, 11, 14, 15 },
243 };
244 static struct fe_simple_probe_struct const probe_table[] = {
245 { FE_DLCR2, 0x70, 0x00 },
246 { FE_DLCR4, 0x08, 0x00 },
247 { FE_DLCR5, 0x80, 0x00 },
248 #if 0
249 { FE_BMPR16, 0x1B, 0x00 },
250 { FE_BMPR17, 0x7F, 0x00 },
251 #endif
252 { 0 }
253 };
254
255 #if ATE_DEBUG >= 4
256 log(LOG_INFO, "ate_find: probe (0x%x) for ATI\n", iobase);
257 #if 0
258 fe_dump(LOG_INFO, sc);
259 #endif
260 #endif
261
262 /*
263 * We should test if MB86965A is on the base address now.
264 * Unfortunately, it is very hard to probe it reliably, since
265 * we have no way to reset the chip under software control.
266 * On cold boot, we could check the "signature" bit patterns
267 * described in the Fujitsu document. On warm boot, however,
268 * we can predict almost nothing about register values.
269 */
270 if (!fe_simple_probe(iot, ioh, probe_table))
271 return (0);
272
273 /* Check if our I/O address matches config info on 86965. */
274 n = (bus_space_read_1(iot, ioh, FE_BMPR19) & FE_B19_ADDR)
275 >> FE_B19_ADDR_SHIFT;
276 *iobase = ate_iomap[n];
277
278 /*
279 * We are now almost sure we have an AT1700 at the given
280 * address. So, read EEPROM through 86965. We have to write
281 * into LSI registers to read from EEPROM. I want to avoid it
282 * at this stage, but I cannot test the presence of the chip
283 * any further without reading EEPROM. FIXME.
284 */
285 ate_read_eeprom(iot, ioh, eeprom);
286
287 /* Make sure that config info in EEPROM and 86965 agree. */
288 if (eeprom[FE_EEPROM_CONF] != bus_space_read_1(iot, ioh, FE_BMPR19)) {
289 #ifdef DIAGNOSTIC
290 printf("ate_find: "
291 "incorrect configration in eeprom and chip\n");
292 #endif
293 return (0);
294 }
295
296 /*
297 * Try to determine IRQ settings.
298 * Different models use different ranges of IRQs.
299 */
300 n = (bus_space_read_1(iot, ioh, FE_BMPR19) & FE_B19_IRQ)
301 >> FE_B19_IRQ_SHIFT;
302 switch (eeprom[FE_ATI_EEP_REVISION] & 0xf0) {
303 case 0x30:
304 *irq = irqmap[3][n];
305 break;
306 case 0x10:
307 case 0x50:
308 *irq = irqmap[2][n];
309 break;
310 case 0x40:
311 case 0x60:
312 if (eeprom[FE_ATI_EEP_MAGIC] & 0x04) {
313 *irq = irqmap[1][n];
314 break;
315 }
316 default:
317 *irq = irqmap[0][n];
318 break;
319 }
320
321 return (1);
322 }
323
324 /*
325 * Determine type and ethernet address.
326 */
327 static int
328 ate_detect(iot, ioh, enaddr)
329 bus_space_tag_t iot;
330 bus_space_handle_t ioh;
331 u_int8_t enaddr[ETHER_ADDR_LEN];
332 {
333 u_char eeprom[FE_EEPROM_SIZE];
334 int type;
335
336 /* Get our station address from EEPROM. */
337 ate_read_eeprom(iot, ioh, eeprom);
338 memcpy(enaddr, eeprom + FE_ATI_EEP_ADDR, ETHER_ADDR_LEN);
339
340 /* Make sure we got a valid station address. */
341 if ((enaddr[0] & 0x03) != 0x00 ||
342 (enaddr[0] == 0x00 && enaddr[1] == 0x00 && enaddr[2] == 0x00)) {
343 #ifdef ATE_DEBUG
344 printf("fmv_detect: invalid ethernet address\n");
345 #endif
346 return (0);
347 }
348
349 /*
350 * Determine the card type.
351 */
352 switch (eeprom[FE_ATI_EEP_MODEL]) {
353 case FE_ATI_MODEL_AT1700T:
354 type = FE_TYPE_AT1700T;
355 break;
356 case FE_ATI_MODEL_AT1700BT:
357 type = FE_TYPE_AT1700BT;
358 break;
359 case FE_ATI_MODEL_AT1700FT:
360 type = FE_TYPE_AT1700FT;
361 break;
362 case FE_ATI_MODEL_AT1700AT:
363 type = FE_TYPE_AT1700AT;
364 break;
365 default:
366 type = FE_TYPE_RE2000;
367 break;
368 }
369
370 return (type);
371 }
372
373 void
374 ate_attach(parent, self, aux)
375 struct device *parent, *self;
376 void *aux;
377 {
378 struct ate_softc *isc = (struct ate_softc *)self;
379 struct mb86960_softc *sc = &isc->sc_mb86960;
380 struct isa_attach_args *ia = aux;
381 bus_space_tag_t iot = ia->ia_iot;
382 bus_space_handle_t ioh;
383 u_int8_t myea[ETHER_ADDR_LEN];
384 const char *typestr;
385 int type;
386
387 printf("\n");
388
389 /* Map i/o space. */
390 if (bus_space_map(iot, ia->ia_io[0].ir_addr, ATE_NPORTS, 0, &ioh)) {
391 printf("%s: can't map i/o space\n", sc->sc_dev.dv_xname);
392 return;
393 }
394
395 sc->sc_bst = iot;
396 sc->sc_bsh = ioh;
397
398 /* Determine the card type and get ethernet address. */
399 type = ate_detect(iot, ioh, myea);
400 switch (type) {
401 case FE_TYPE_AT1700T:
402 typestr = "AT-1700T";
403 break;
404 case FE_TYPE_AT1700BT:
405 typestr = "AT-1700BT";
406 break;
407 case FE_TYPE_AT1700FT:
408 typestr = "AT-1700FT";
409 break;
410 case FE_TYPE_AT1700AT:
411 typestr = "AT-1700AT";
412 break;
413 case FE_TYPE_RE2000:
414 typestr = "unknown (RE-2000?)";
415 break;
416
417 default:
418 /* Unknown card type: maybe a new model, but... */
419 printf("%s: where did the card go?!\n", sc->sc_dev.dv_xname);
420 panic("unknown card");
421 }
422
423 printf("%s: %s Ethernet\n", sc->sc_dev.dv_xname, typestr);
424
425 /* This interface is always enabled. */
426 sc->sc_flags |= FE_FLAGS_ENABLED;
427
428 /*
429 * Do generic MB86960 attach.
430 */
431 mb86960_attach(sc, MB86960_TYPE_86965, myea);
432
433 mb86960_config(sc, NULL, 0, 0);
434
435 /* Establish the interrupt handler. */
436 isc->sc_ih = isa_intr_establish(ia->ia_ic, ia->ia_irq[0].ir_irq,
437 IST_EDGE, IPL_NET, mb86960_intr, sc);
438 if (isc->sc_ih == NULL)
439 printf("%s: couldn't establish interrupt handler\n",
440 sc->sc_dev.dv_xname);
441 }
442