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