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