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