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