xref: /netbsd-src/sys/dev/isa/if_ate.c (revision 8b0f9554ff8762542c4defc4f70e1eb76fb508fa)
1 /*	$NetBSD: if_ate.c,v 1.48 2007/10/19 12:00:17 ad 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.48 2007/10/19 12:00:17 ad 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 <sys/bus.h>
48 #include <sys/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,
101     void *aux)
102 {
103 	struct isa_attach_args *ia = aux;
104 	bus_space_tag_t iot = ia->ia_iot;
105 	bus_space_handle_t ioh;
106 	int i, iobase, irq, rv = 0;
107 	uint8_t myea[ETHER_ADDR_LEN];
108 
109 	if (ia->ia_nio < 1)
110 		return 0;
111 	if (ia->ia_nirq < 1)
112 		return 0;
113 
114 	if (ISA_DIRECT_CONFIG(ia))
115 		return 0;
116 
117 	/* Disallow wildcarded values. */
118 	if (ia->ia_io[0].ir_addr == ISA_UNKNOWN_PORT)
119 		return 0;
120 
121 	/*
122 	 * See if the sepcified address is valid for MB86965A JLI mode.
123 	 */
124 	for (i = 0; i < NATE_IOMAP; i++)
125 		if (ate_iomap[i] == ia->ia_io[0].ir_addr)
126 			break;
127 	if (i == NATE_IOMAP) {
128 		DPRINTF("ate_match: unknown iobase 0x%x\n",
129 		    ia->ia_io[0].ir_addr);
130 		return 0;
131 	}
132 
133 	/* Map i/o space. */
134 	if (bus_space_map(iot, ia->ia_io[0].ir_addr, ATE_NPORTS, 0, &ioh)) {
135 		DPRINTF("ate_match: couldn't map iospace 0x%x\n",
136 		    ia->ia_io[0].ir_addr);
137 		return 0;
138 	}
139 
140 	if (ate_find(iot, ioh, &iobase, &irq) == 0) {
141 		DPRINTF("ate_match: ate_find failed\n");
142 		goto out;
143 	}
144 
145 	if (iobase != ia->ia_io[0].ir_addr) {
146 		DPRINTF("ate_match: unexpected iobase in board: 0x%x\n",
147 		    iobase);
148 		goto out;
149 	}
150 
151 	if (ate_detect(iot, ioh, myea) == 0) { /* XXX necessary? */
152 		DPRINTF("ate_match: ate_detect failed\n");
153 		goto out;
154 	}
155 
156 	if (ia->ia_irq[0].ir_irq != ISA_UNKNOWN_IRQ) {
157 		if (ia->ia_irq[0].ir_irq != irq) {
158 			printf("ate_match: irq mismatch; "
159 			    "kernel configured %d != board configured %d\n",
160 			    ia->ia_irq[0].ir_irq, irq);
161 			goto out;
162 		}
163 	} else
164 		ia->ia_irq[0].ir_irq = irq;
165 
166 	ia->ia_nio = 1;
167 	ia->ia_io[0].ir_size = ATE_NPORTS;
168 
169 	ia->ia_nirq = 1;
170 
171 	ia->ia_niomem = 0;
172 	ia->ia_ndrq = 0;
173 
174 	rv = 1;
175 
176  out:
177 	bus_space_unmap(iot, ioh, ATE_NPORTS);
178 	return rv;
179 }
180 
181 /*
182  * Check for specific bits in specific registers have specific values.
183  */
184 static inline int
185 fe_simple_probe(bus_space_tag_t iot, bus_space_handle_t ioh,
186     struct fe_simple_probe_struct const *sp)
187 {
188 	uint8_t val;
189 	struct fe_simple_probe_struct const *p;
190 
191 	for (p = sp; p->mask != 0; p++) {
192 		val = bus_space_read_1(iot, ioh, p->port);
193 		if ((val & p->mask) != p->bits) {
194 			DPRINTF("fe_simple_probe: %x & %x != %x\n",
195 			    val, p->mask, p->bits);
196 			return 0;
197 		}
198 	}
199 
200 	return 1;
201 }
202 
203 /*
204  * Hardware (vendor) specific probe routines.
205  */
206 
207 /*
208  * Probe and initialization for Allied-Telesis AT1700/RE2000 series.
209  */
210 static int
211 ate_find(bus_space_tag_t iot, bus_space_handle_t ioh, int *iobase, int *irq)
212 {
213 	uint8_t eeprom[FE_EEPROM_SIZE];
214 	int n;
215 
216 	static int const irqmap[4][4] = {
217 		{  3,  4,  5,  9 },
218 		{ 10, 11, 12, 15 },
219 		{  3, 11,  5, 15 },
220 		{ 10, 11, 14, 15 },
221 	};
222 	static struct fe_simple_probe_struct const probe_table[] = {
223 		{ FE_DLCR2,  0x70, 0x00 },
224 		{ FE_DLCR4,  0x08, 0x00 },
225 		{ FE_DLCR5,  0x80, 0x00 },
226 #if 0
227 		{ FE_BMPR16, 0x1B, 0x00 },
228 		{ FE_BMPR17, 0x7F, 0x00 },
229 #endif
230 		{ 0,	     0x00, 0x00 },
231 	};
232 
233 #if ATE_DEBUG >= 4
234 	log(LOG_INFO, "ate_find: probe (0x%x) for ATE\n", iobase);
235 #if 0
236 	fe_dump(LOG_INFO, sc);
237 #endif
238 #endif
239 
240 	/*
241 	 * We should test if MB86965A is on the base address now.
242 	 * Unfortunately, it is very hard to probe it reliably, since
243 	 * we have no way to reset the chip under software control.
244 	 * On cold boot, we could check the "signature" bit patterns
245 	 * described in the Fujitsu document.  On warm boot, however,
246 	 * we can predict almost nothing about register values.
247 	 */
248 	if (!fe_simple_probe(iot, ioh, probe_table))
249 		return 0;
250 
251 	/* Check if our I/O address matches config info on 86965. */
252 	n = (bus_space_read_1(iot, ioh, FE_BMPR19) & FE_B19_ADDR)
253 	    >> FE_B19_ADDR_SHIFT;
254 	*iobase = ate_iomap[n];
255 
256 	/*
257 	 * We are now almost sure we have an AT1700 at the given
258 	 * address.  So, read EEPROM through 86965.  We have to write
259 	 * into LSI registers to read from EEPROM.  I want to avoid it
260 	 * at this stage, but I cannot test the presence of the chip
261 	 * any further without reading EEPROM.  FIXME.
262 	 */
263 	mb86965_read_eeprom(iot, ioh, eeprom);
264 
265 	/* Make sure that config info in EEPROM and 86965 agree. */
266 	if (eeprom[FE_EEPROM_CONF] != bus_space_read_1(iot, ioh, FE_BMPR19)) {
267 #ifdef DIAGNOSTIC
268 		printf("ate_find: "
269 		    "incorrect configuration in eeprom and chip\n");
270 #endif
271 		return 0;
272 	}
273 
274 	/*
275 	 * Try to determine IRQ settings.
276 	 * Different models use different ranges of IRQs.
277 	 */
278 	n = (bus_space_read_1(iot, ioh, FE_BMPR19) & FE_B19_IRQ)
279 	    >> FE_B19_IRQ_SHIFT;
280 	switch (eeprom[FE_ATI_EEP_REVISION] & 0xf0) {
281 	case 0x30:
282 		*irq = irqmap[3][n];
283 		break;
284 	case 0x10:
285 	case 0x50:
286 		*irq = irqmap[2][n];
287 		break;
288 	case 0x40:
289 	case 0x60:
290 		if (eeprom[FE_ATI_EEP_MAGIC] & 0x04) {
291 			*irq = irqmap[1][n];
292 			break;
293 		}
294 	default:
295 		*irq = irqmap[0][n];
296 		break;
297 	}
298 
299 	return 1;
300 }
301 
302 /*
303  * Determine type and ethernet address.
304  */
305 static int
306 ate_detect(bus_space_tag_t iot, bus_space_handle_t ioh,
307     uint8_t enaddr[ETHER_ADDR_LEN])
308 {
309 	uint8_t eeprom[FE_EEPROM_SIZE];
310 	int type;
311 
312 	/* Get our station address from EEPROM. */
313 	mb86965_read_eeprom(iot, ioh, eeprom);
314 	memcpy(enaddr, eeprom + FE_ATI_EEP_ADDR, ETHER_ADDR_LEN);
315 
316 	/* Make sure we got a valid station address. */
317 	if ((enaddr[0] & 0x03) != 0x00 ||
318 	    (enaddr[0] == 0x00 && enaddr[1] == 0x00 && enaddr[2] == 0x00)) {
319 		DPRINTF("ate_detect: invalid ethernet address\n");
320 		return 0;
321 	}
322 
323 	/*
324 	 * Determine the card type.
325 	 */
326 	switch (eeprom[FE_ATI_EEP_MODEL]) {
327 	case FE_ATI_MODEL_AT1700T:
328 		type = FE_TYPE_AT1700T;
329 		break;
330 	case FE_ATI_MODEL_AT1700BT:
331 		type = FE_TYPE_AT1700BT;
332 		break;
333 	case FE_ATI_MODEL_AT1700FT:
334 		type = FE_TYPE_AT1700FT;
335 		break;
336 	case FE_ATI_MODEL_AT1700AT:
337 		type = FE_TYPE_AT1700AT;
338 		break;
339 	default:
340 		type = FE_TYPE_AT_UNKNOWN;
341 		break;
342 	}
343 
344 	return type;
345 }
346 
347 void
348 ate_attach(struct device *parent, struct device *self, void *aux)
349 {
350 	struct ate_softc *isc = (struct ate_softc *)self;
351 	struct mb86960_softc *sc = &isc->sc_mb86960;
352 	struct isa_attach_args *ia = aux;
353 	bus_space_tag_t iot = ia->ia_iot;
354 	bus_space_handle_t ioh;
355 	uint8_t myea[ETHER_ADDR_LEN];
356 	const char *typestr;
357 	int type;
358 
359 	/* Map i/o space. */
360 	if (bus_space_map(iot, ia->ia_io[0].ir_addr, ATE_NPORTS, 0, &ioh)) {
361 		printf(": can't map i/o space\n");
362 		return;
363 	}
364 
365 	sc->sc_bst = iot;
366 	sc->sc_bsh = ioh;
367 
368 	/* Determine the card type and get ethernet address. */
369 	type = ate_detect(iot, ioh, myea);
370 	switch (type) {
371 	case FE_TYPE_AT1700T:
372 		typestr = "AT-1700T/RE2001";
373 		break;
374 	case FE_TYPE_AT1700BT:
375 		typestr = "AT-1700BT/RE2003";
376 		break;
377 	case FE_TYPE_AT1700FT:
378 		typestr = "AT-1700FT/RE2009";
379 		break;
380 	case FE_TYPE_AT1700AT:
381 		typestr = "AT-1700AT/RE2005";
382 		break;
383 	case FE_TYPE_AT_UNKNOWN:
384 		typestr = "unknown AT-1700/RE2000";
385 		break;
386 
387 	default:
388 	  	/* Unknown card type: maybe a new model, but... */
389 		printf(": where did the card go?!\n");
390 		panic("unknown card");
391 	}
392 
393 	printf(": %s Ethernet\n", typestr);
394 
395 	/* This interface is always enabled. */
396 	sc->sc_stat |= FE_STAT_ENABLED;
397 
398 	/*
399 	 * Do generic MB86960 attach.
400 	 */
401 	mb86960_attach(sc, myea);
402 
403 	mb86960_config(sc, NULL, 0, 0);
404 
405 	/* Establish the interrupt handler. */
406 	isc->sc_ih = isa_intr_establish(ia->ia_ic, ia->ia_irq[0].ir_irq,
407 	    IST_EDGE, IPL_NET, mb86960_intr, sc);
408 	if (isc->sc_ih == NULL)
409 		printf("%s: couldn't establish interrupt handler\n",
410 		    sc->sc_dev.dv_xname);
411 }
412