xref: /netbsd-src/sys/dev/isa/if_ai.c (revision 404fbe5fb94ca1e054339640cabb2801ce52dd30)
1 /*	$NetBSD: if_ai.c,v 1.28 2008/04/28 20:23:52 martin Exp $	*/
2 
3 /*-
4  * Copyright (c) 1998 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Rafal K. Boni.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 #include <sys/cdefs.h>
33 __KERNEL_RCSID(0, "$NetBSD: if_ai.c,v 1.28 2008/04/28 20:23:52 martin Exp $");
34 
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/mbuf.h>
38 #include <sys/errno.h>
39 #include <sys/device.h>
40 #include <sys/protosw.h>
41 #include <sys/socket.h>
42 
43 #include <net/if.h>
44 #include <net/if_dl.h>
45 #include <net/if_types.h>
46 #include <net/if_media.h>
47 #include <net/if_ether.h>
48 
49 #include <sys/cpu.h>
50 #include <sys/bus.h>
51 #include <sys/intr.h>
52 
53 #include <dev/isa/isareg.h>
54 #include <dev/isa/isavar.h>
55 
56 #include <dev/ic/i82586reg.h>
57 #include <dev/ic/i82586var.h>
58 #include <dev/isa/if_aireg.h>
59 
60 #ifdef AI_DEBUG
61 #define DPRINTF(x)	printf x
62 #else
63 #define DPRINTF(x)
64 #endif
65 
66 struct ai_softc {
67 	struct ie_softc sc_ie;
68 
69 	bus_space_tag_t sc_regt;	/* space tag for registers */
70 	bus_space_handle_t sc_regh;	/* space handle for registers */
71 
72 	u_int8_t	card_rev;
73 	u_int8_t	card_type;
74 
75 	void		*sc_ih;		/* interrupt handle */
76 };
77 
78 const char *ai_names[] = {
79         "StarLAN 10",
80         "EN100",
81         "StarLAN Fiber",
82 };
83 
84 /* Functions required by the i82586 MI driver */
85 static void 	ai_reset(struct ie_softc *, int);
86 static void 	ai_atten(struct ie_softc *, int);
87 
88 static void	ai_copyin(struct ie_softc *, void *, int, size_t);
89 static void	ai_copyout(struct ie_softc *, const void *, int, size_t);
90 
91 static u_int16_t ai_read_16(struct ie_softc *, int);
92 static void	ai_write_16(struct ie_softc *, int, u_int16_t);
93 static void	ai_write_24(struct ie_softc *, int, int);
94 
95 /* Local support functions */
96 static int 	check_ie_present(struct ie_softc*, bus_space_tag_t,
97 					bus_space_handle_t, bus_size_t);
98 static int	ai_find_mem_size(struct ai_softc*, bus_space_tag_t,
99 					bus_size_t);
100 
101 int ai_match(struct device *, struct cfdata *, void *);
102 void ai_attach(struct device *, struct device *, void *);
103 
104 /*
105  * AT&T StarLan support routines
106  */
107 static void
108 ai_reset(sc, why)
109 	struct ie_softc *sc;
110 	int why;
111 {
112 	struct ai_softc* asc = (struct ai_softc *) sc;
113 
114 	switch (why) {
115 	case CHIP_PROBE:
116 		/* reset to chip to see if it responds */
117 		bus_space_write_1(asc->sc_regt, asc->sc_regh, AI_RESET, 0);
118 		DELAY(100);
119 		break;
120 
121 	case CARD_RESET:
122 		/*
123 		 * this takes around 10sec, and we can get
124 		 * by quite well w/out it...
125 		 */
126 		break;
127 	}
128 }
129 
130 static void
131 ai_atten(struct ie_softc *sc, int why)
132 {
133     struct ai_softc* asc = (struct ai_softc *) sc;
134     bus_space_write_1(asc->sc_regt, asc->sc_regh, AI_ATTN, 0);
135 }
136 
137 static void
138 ai_copyin (sc, dst, offset, size)
139         struct ie_softc *sc;
140         void *dst;
141         int offset;
142         size_t size;
143 {
144 	int dribble;
145 	u_int8_t* bptr = dst;
146 
147 	bus_space_barrier(sc->bt, sc->bh, offset, size,
148 			  BUS_SPACE_BARRIER_READ);
149 
150 	if (offset % 2) {
151 		*bptr = bus_space_read_1(sc->bt, sc->bh, offset);
152 		offset++; bptr++; size--;
153 	}
154 
155 	dribble = size % 2;
156 	bus_space_read_region_2(sc->bt, sc->bh, offset, (u_int16_t *) bptr,
157 				size >> 1);
158 
159 	if (dribble) {
160 		bptr += size - 1;
161 		offset += size - 1;
162 		*bptr = bus_space_read_1(sc->bt, sc->bh, offset);
163 	}
164 }
165 
166 static void
167 ai_copyout (sc, src, offset, size)
168         struct ie_softc *sc;
169         const void *src;
170         int offset;
171         size_t size;
172 {
173 	int dribble;
174 	int osize = size;
175 	int ooffset = offset;
176 	const u_int8_t* bptr = src;
177 
178 	if (offset % 2) {
179 		bus_space_write_1(sc->bt, sc->bh, offset, *bptr);
180 		offset++; bptr++; size--;
181 	}
182 
183 	dribble = size % 2;
184 	bus_space_write_region_2(sc->bt, sc->bh, offset,
185 	    (const u_int16_t *)bptr, size >> 1);
186 	if (dribble) {
187 		bptr += size - 1;
188 		offset += size - 1;
189 		bus_space_write_1(sc->bt, sc->bh, offset, *bptr);
190 	}
191 
192 	bus_space_barrier(sc->bt, sc->bh, ooffset, osize,
193 			  BUS_SPACE_BARRIER_WRITE);
194 }
195 
196 static u_int16_t
197 ai_read_16 (sc, offset)
198         struct ie_softc *sc;
199         int offset;
200 {
201 	bus_space_barrier(sc->bt, sc->bh, offset, 2, BUS_SPACE_BARRIER_READ);
202         return bus_space_read_2(sc->bt, sc->bh, offset);
203 }
204 
205 static void
206 ai_write_16 (sc, offset, value)
207         struct ie_softc *sc;
208         int offset;
209         u_int16_t value;
210 {
211         bus_space_write_2(sc->bt, sc->bh, offset, value);
212 	bus_space_barrier(sc->bt, sc->bh, offset, 2, BUS_SPACE_BARRIER_WRITE);
213 }
214 
215 static void
216 ai_write_24 (sc, offset, addr)
217         struct ie_softc *sc;
218         int offset, addr;
219 {
220         bus_space_write_4(sc->bt, sc->bh, offset, addr +
221                                 (u_long) sc->sc_maddr - (u_long) sc->sc_iobase);
222 	bus_space_barrier(sc->bt, sc->bh, offset, 4, BUS_SPACE_BARRIER_WRITE);
223 }
224 
225 int
226 ai_match(struct device *parent, struct cfdata *cf, void *aux)
227 {
228 	int rv = 0;
229 	u_int8_t val, type;
230 	bus_size_t memsize;
231 	bus_space_tag_t iot;
232 	bus_space_handle_t ioh;
233 	struct isa_attach_args * const ia = aux;
234 	struct ai_softc asc;
235 
236 	if (ia->ia_nio < 1)
237 		return (0);
238 	if (ia->ia_niomem < 1)
239 		return (0);
240 	if (ia->ia_nirq < 1)
241 		return (0);
242 
243 	if (ISA_DIRECT_CONFIG(ia))
244 		return (0);
245 
246 	/* Punt if wildcarded port, IRQ or memory address */
247 	if (ia->ia_io[0].ir_addr == ISA_UNKNOWN_PORT ||
248 	    ia->ia_iomem[0].ir_addr == ISA_UNKNOWN_IOMEM ||
249 	    ia->ia_irq[0].ir_irq == ISA_UNKNOWN_IRQ) {
250 		DPRINTF((
251 		 "ai_match: wildcarded IRQ, IOAddr, or memAddr, skipping\n"));
252 		return (0);
253 	}
254 
255 	iot = ia->ia_iot;
256 
257 	/*
258 	 * This probe is horribly bad, but I have no info on this card other
259 	 * than the former driver, and it was just as bad!
260 	 */
261 	if (bus_space_map(iot, ia->ia_io[0].ir_addr,
262 			  AI_IOSIZE, 0, &ioh) != 0) {
263 
264 		DPRINTF(("ai_match: cannot map %d IO ports @ 0x%x\n",
265 			 AI_IOSIZE, ia->ia_iobase));
266 		return (0);
267 	}
268 
269 	val = bus_space_read_1(iot, ioh, AI_REVISION);
270 
271 	type = SL_BOARD(val);
272 	if (type != SL10_BOARD && type != EN100_BOARD &&
273 	    type != SLFIBER_BOARD) {
274 		DPRINTF(("ai_match: unknown board code 0x%02x @ 0x%x\n",
275 			 type, ia->ia_iobase));
276 		goto out;
277 	}
278 
279 	/*
280 	 * Fill in just about enough of our local `ai_softc' for
281 	 * ai_find_mem_size() to do its job.
282 	 */
283 	memset(&asc, 0, sizeof asc);
284 	asc.sc_regt = iot;
285 	asc.sc_regh = ioh;
286 
287 	if ((memsize = ai_find_mem_size(&asc, ia->ia_memt,
288 	     ia->ia_iomem[0].ir_addr)) == 0) {
289 		DPRINTF(("ai_match: cannot size memory of board @ 0x%x\n",
290 			 ia->ia_io[0].ir_addr));
291 		goto out;
292 	}
293 
294 	if (ia->ia_iomem[0].ir_size != 0 &&
295 	    ia->ia_iomem[0].ir_size != memsize) {
296 		DPRINTF((
297 		   "ai_match: memsize of board @ 0x%x doesn't match config\n",
298 		   ia->ia_iobase));
299 		goto out;
300 	}
301 
302 	rv = 1;
303 
304 	ia->ia_nio = 1;
305 	ia->ia_io[0].ir_size = AI_IOSIZE;
306 
307 	ia->ia_niomem = 1;
308 	ia->ia_iomem[0].ir_size = memsize;
309 
310 	ia->ia_nirq = 1;
311 
312 	ia->ia_ndrq = 0;
313 
314 	DPRINTF(("ai_match: found board @ 0x%x\n", ia->ia_iobase));
315 
316 out:
317 	bus_space_unmap(iot, ioh, AI_IOSIZE);
318 	return rv;
319 }
320 
321 void
322 ai_attach(struct device *parent, struct device *self, void *aux)
323 {
324 	struct ai_softc *asc = (void *)self;
325 	struct ie_softc *sc = &asc->sc_ie;
326 	struct isa_attach_args *ia = aux;
327 
328 	u_int8_t val = 0;
329 	bus_space_handle_t ioh, memh;
330 	u_int8_t ethaddr[ETHER_ADDR_LEN];
331 	char name[80];
332 
333 	if (bus_space_map(ia->ia_iot, ia->ia_io[0].ir_addr,
334 			  ia->ia_io[0].ir_size, 0, &ioh) != 0) {
335 		DPRINTF(("\n%s: can't map i/o space 0x%x-0x%x\n",
336 			 device_xname(&sc->sc_dev),
337 		         ia->ia_io[0].ir_addr, ia->ia_io[0].ir_addr +
338 		         ia->ia_io[0].ir_size - 1));
339 		return;
340 	}
341 
342 	if (bus_space_map(ia->ia_memt, ia->ia_iomem[0].ir_addr,
343 			  ia->ia_iomem[0].ir_size, 0, &memh) != 0) {
344 		DPRINTF(("\n%s: can't map iomem space 0x%x-0x%x\n",
345 			 device_xname(&sc->sc_dev),
346 			 ia->ia_iomem[0].ir_addr, ia->ia_iomem[0].ir_addr +
347 			 ia->ia_iomem[0].ir_size - 1));
348 		bus_space_unmap(ia->ia_iot, ioh, ia->ia_io[0].ir_size);
349 		return;
350 	}
351 
352 	asc->sc_regt = ia->ia_iot;
353 	asc->sc_regh = ioh;
354 
355 	sc->hwinit = NULL;
356 	sc->intrhook = NULL;
357 	sc->hwreset = ai_reset;
358 	sc->chan_attn = ai_atten;
359 
360 	sc->ie_bus_barrier = NULL;
361 
362 	sc->memcopyin = ai_copyin;
363 	sc->memcopyout = ai_copyout;
364 	sc->ie_bus_read16 = ai_read_16;
365 	sc->ie_bus_write16 = ai_write_16;
366 	sc->ie_bus_write24 = ai_write_24;
367 
368 	sc->do_xmitnopchain = 0;
369 
370 	sc->sc_mediachange = NULL;
371 	sc->sc_mediastatus = NULL;
372 
373 	sc->bt = ia->ia_memt;
374 	sc->bh = memh;
375 
376 	/* Map i/o space. */
377 	sc->sc_msize = ia->ia_iomem[0].ir_size;
378 	sc->sc_maddr = (void *)memh;
379 	sc->sc_iobase = (char *)sc->sc_maddr + sc->sc_msize - (1 << 24);
380 
381 	/* set up pointers to important on-card control structures */
382 	sc->iscp = 0;
383 	sc->scb = IE_ISCP_SZ;
384 	sc->scp = sc->sc_msize + IE_SCP_ADDR - (1 << 24);
385 
386 	sc->buf_area = sc->scb + IE_SCB_SZ;
387 	sc->buf_area_sz = sc->sc_msize - IE_ISCP_SZ - IE_SCB_SZ - IE_SCP_SZ;
388 
389 	/* zero card memory */
390 	bus_space_set_region_1(sc->bt, sc->bh, 0, 0, sc->sc_msize);
391 
392 	/* set card to 16-bit bus mode */
393 	bus_space_write_1(sc->bt, sc->bh, IE_SCP_BUS_USE((u_long)sc->scp),
394 			  IE_SYSBUS_16BIT);
395 
396 	/* set up pointers to key structures */
397 	ai_write_24(sc, IE_SCP_ISCP((u_long)sc->scp), (u_long) sc->iscp);
398 	ai_write_16(sc, IE_ISCP_SCB((u_long)sc->iscp), (u_long) sc->scb);
399 	ai_write_24(sc, IE_ISCP_BASE((u_long)sc->iscp), (u_long) sc->iscp);
400 
401 	/* flush setup of pointers, check if chip answers */
402 	bus_space_barrier(sc->bt, sc->bh, 0, sc->sc_msize,
403 			  BUS_SPACE_BARRIER_WRITE);
404 	if (!i82586_proberam(sc)) {
405 		DPRINTF(("\n%s: can't talk to i82586!\n",
406 			device_xname(&sc->sc_dev)));
407 		bus_space_unmap(ia->ia_iot, ioh, ia->ia_io[0].ir_size);
408 		bus_space_unmap(ia->ia_memt, memh, ia->ia_iomem[0].ir_size);
409 		return;
410 	}
411 
412 	val = bus_space_read_1(asc->sc_regt, asc->sc_regh, AI_REVISION);
413 	asc->card_rev = SL_REV(val);
414 	asc->card_type = SL_BOARD(val) - 1;
415 	snprintf(name, sizeof(name), "%s, rev. %d",
416 	    ai_names[asc->card_type], asc->card_rev);
417 
418 	i82586_attach(sc, name, ethaddr, NULL, 0, 0);
419 
420 	asc->sc_ih = isa_intr_establish(ia->ia_ic, ia->ia_irq[0].ir_irq,
421 	    IST_EDGE, IPL_NET, i82586_intr, sc);
422 	if (asc->sc_ih == NULL) {
423 		DPRINTF(("\n%s: can't establish interrupt\n",
424 			device_xname(&sc->sc_dev)));
425 	}
426 }
427 
428 /*
429  * Divine the memory size of this board.
430  * Better hope there's nothing important hiding just below the card...
431  */
432 static int
433 ai_find_mem_size(asc, memt, maddr)
434 	struct ai_softc* asc;
435 	bus_space_tag_t memt;
436 	bus_size_t maddr;
437 {
438 	int size;
439 	bus_space_handle_t memh;
440 	struct ie_softc* sc = &asc->sc_ie;
441 
442 	for (size = 65536; size >= 16384; size -= 16384) {
443 		if (bus_space_map(memt, maddr, size, 0, &memh) == 0) {
444 			size = check_ie_present(sc, memt, maddr, size);
445 			bus_space_unmap(memt, memh, size);
446 
447 			if (size != 0)
448 				return size;
449 		}
450 	}
451 
452 	return (0);
453 }
454 
455 /*
456  * Check to see if there's an 82586 out there.
457  */
458 static int
459 check_ie_present(sc, memt, memh, size)
460 	struct ie_softc* sc;
461 	bus_space_tag_t memt;
462 	bus_space_handle_t memh;
463 	bus_size_t size;
464 {
465 	sc->hwreset = ai_reset;
466 	sc->chan_attn = ai_atten;
467 	sc->ie_bus_read16 = ai_read_16;
468 	sc->ie_bus_write16 = ai_write_16;
469 
470 	sc->bt = memt;
471 	sc->bh = memh;
472 	sc->sc_iobase = (char *)memh + size - (1 << 24);
473 
474 	sc->scp = size + IE_SCP_ADDR - (1 << 24);
475 	bus_space_set_region_1(memt, memh, (u_long) sc->scp, 0, IE_SCP_SZ);
476 
477 	sc->iscp = 0;
478 	bus_space_set_region_1(memt, memh, (u_long) sc->iscp, 0, IE_ISCP_SZ);
479 
480 	sc->scb = IE_ISCP_SZ;
481 	bus_space_set_region_1(memt, memh, sc->scb, 0, IE_SCB_SZ);
482 
483 	/* set card to 16-bit bus mode */
484 	bus_space_write_1(sc->bt, sc->bh, IE_SCP_BUS_USE((u_long)sc->scp),
485 			  IE_SYSBUS_16BIT);
486 
487 	/* set up pointers to key structures */
488 	ai_write_24(sc, IE_SCP_ISCP((u_long)sc->scp), (u_long) sc->iscp);
489 	ai_write_16(sc, IE_ISCP_SCB((u_long)sc->iscp), (u_long) sc->scb);
490 	ai_write_24(sc, IE_ISCP_BASE((u_long)sc->iscp), (u_long) sc->iscp);
491 
492 	/* flush setup of pointers, check if chip answers */
493 	bus_space_barrier(sc->bt, sc->bh, 0, sc->sc_msize,
494 			  BUS_SPACE_BARRIER_WRITE);
495 
496 	if (!i82586_proberam(sc))
497 		return (0);
498 
499 	return (size);
500 }
501 
502 CFATTACH_DECL(ai, sizeof(struct ai_softc),
503     ai_match, ai_attach, NULL, NULL);
504