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