if_ai.c revision 1.9
1/*	$NetBSD: if_ai.c,v 1.9 2001/01/22 22:28:46 bjh21 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
70struct 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
82const char *ai_names[] = {
83        "StarLAN 10",
84        "EN100",
85        "StarLAN Fiber",
86};
87
88/* Functions required by the i82586 MI driver */
89static void 	ai_reset __P((struct ie_softc *, int));
90static void 	ai_atten __P((struct ie_softc *));
91
92static void	ai_copyin __P((struct ie_softc *, void *, int, size_t));
93static void	ai_copyout __P((struct ie_softc *, const void *, int, size_t));
94
95static u_int16_t ai_read_16 __P((struct ie_softc *, int));
96static void	ai_write_16 __P((struct ie_softc *, int, u_int16_t));
97static void	ai_write_24 __P((struct ie_softc *, int, int));
98
99/* Local support functions */
100static int 	check_ie_present __P((struct ie_softc*, bus_space_tag_t,
101					bus_space_handle_t, bus_size_t));
102static int	ai_find_mem_size __P((struct ai_softc*, bus_space_tag_t,
103					bus_size_t));
104
105int ai_match __P((struct device *, struct cfdata *, void *));
106void ai_attach __P((struct device *, struct device *, void *));
107
108/*
109 * AT&T StarLan support routines
110 */
111static void
112ai_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
134static void
135ai_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
142static void
143ai_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
171static void
172ai_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
201static u_int16_t
202ai_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
210static void
211ai_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
220static void
221ai_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
230int
231ai_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
306out:
307	bus_space_unmap(iot, ioh, AI_IOSIZE);
308	return rv;
309}
310
311void
312ai_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->ie_bus_barrier = NULL;
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 */
422static int
423ai_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 */
448static int
449check_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
491struct cfattach ai_ca = {
492	sizeof(struct ai_softc), ai_match, ai_attach
493};
494