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
66struct 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
78const char *ai_names[] = {
79        "StarLAN 10",
80        "EN100",
81        "StarLAN Fiber",
82};
83
84/* Functions required by the i82586 MI driver */
85static void 	ai_reset(struct ie_softc *, int);
86static void 	ai_atten(struct ie_softc *, int);
87
88static void	ai_copyin(struct ie_softc *, void *, int, size_t);
89static void	ai_copyout(struct ie_softc *, const void *, int, size_t);
90
91static u_int16_t ai_read_16(struct ie_softc *, int);
92static void	ai_write_16(struct ie_softc *, int, u_int16_t);
93static void	ai_write_24(struct ie_softc *, int, int);
94
95/* Local support functions */
96static int 	check_ie_present(struct ie_softc*, bus_space_tag_t,
97					bus_space_handle_t, bus_size_t);
98static int	ai_find_mem_size(struct ai_softc*, bus_space_tag_t,
99					bus_size_t);
100
101int ai_match(device_t, cfdata_t, void *);
102void ai_attach(device_t, device_t, void *);
103
104/*
105 * AT&T StarLan support routines
106 */
107static void
108ai_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
128static void
129ai_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
135static void
136ai_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
160static void
161ai_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
186static u_int16_t
187ai_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
193static void
194ai_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
200static void
201ai_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
208int
209ai_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
299out:
300	bus_space_unmap(iot, ioh, AI_IOSIZE);
301	return rv;
302}
303
304void
305ai_attach(device_t parent, device_t self, void *aux)
306{
307	struct ai_softc *asc = device_private(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	sc->sc_dev = self;
317
318	if (bus_space_map(ia->ia_iot, ia->ia_io[0].ir_addr,
319			  ia->ia_io[0].ir_size, 0, &ioh) != 0) {
320		DPRINTF(("\n%s: can't map i/o space 0x%x-0x%x\n",
321			 device_xname(self),
322		         ia->ia_io[0].ir_addr, ia->ia_io[0].ir_addr +
323		         ia->ia_io[0].ir_size - 1));
324		return;
325	}
326
327	if (bus_space_map(ia->ia_memt, ia->ia_iomem[0].ir_addr,
328			  ia->ia_iomem[0].ir_size, 0, &memh) != 0) {
329		DPRINTF(("\n%s: can't map iomem space 0x%x-0x%x\n",
330			 device_xname(self),
331			 ia->ia_iomem[0].ir_addr, ia->ia_iomem[0].ir_addr +
332			 ia->ia_iomem[0].ir_size - 1));
333		bus_space_unmap(ia->ia_iot, ioh, ia->ia_io[0].ir_size);
334		return;
335	}
336
337	asc->sc_regt = ia->ia_iot;
338	asc->sc_regh = ioh;
339
340	sc->hwinit = NULL;
341	sc->intrhook = NULL;
342	sc->hwreset = ai_reset;
343	sc->chan_attn = ai_atten;
344
345	sc->ie_bus_barrier = NULL;
346
347	sc->memcopyin = ai_copyin;
348	sc->memcopyout = ai_copyout;
349	sc->ie_bus_read16 = ai_read_16;
350	sc->ie_bus_write16 = ai_write_16;
351	sc->ie_bus_write24 = ai_write_24;
352
353	sc->do_xmitnopchain = 0;
354
355	sc->sc_mediachange = NULL;
356	sc->sc_mediastatus = NULL;
357
358	sc->bt = ia->ia_memt;
359	sc->bh = memh;
360
361	/* Map i/o space. */
362	sc->sc_msize = ia->ia_iomem[0].ir_size;
363	sc->sc_maddr = (void *)memh;
364	sc->sc_iobase = (char *)sc->sc_maddr + sc->sc_msize - (1 << 24);
365
366	/* set up pointers to important on-card control structures */
367	sc->iscp = 0;
368	sc->scb = IE_ISCP_SZ;
369	sc->scp = sc->sc_msize + IE_SCP_ADDR - (1 << 24);
370
371	sc->buf_area = sc->scb + IE_SCB_SZ;
372	sc->buf_area_sz = sc->sc_msize - IE_ISCP_SZ - IE_SCB_SZ - IE_SCP_SZ;
373
374	/* zero card memory */
375	bus_space_set_region_1(sc->bt, sc->bh, 0, 0, sc->sc_msize);
376
377	/* set card to 16-bit bus mode */
378	bus_space_write_1(sc->bt, sc->bh, IE_SCP_BUS_USE((u_long)sc->scp),
379			  IE_SYSBUS_16BIT);
380
381	/* set up pointers to key structures */
382	ai_write_24(sc, IE_SCP_ISCP((u_long)sc->scp), (u_long) sc->iscp);
383	ai_write_16(sc, IE_ISCP_SCB((u_long)sc->iscp), (u_long) sc->scb);
384	ai_write_24(sc, IE_ISCP_BASE((u_long)sc->iscp), (u_long) sc->iscp);
385
386	/* flush setup of pointers, check if chip answers */
387	bus_space_barrier(sc->bt, sc->bh, 0, sc->sc_msize,
388			  BUS_SPACE_BARRIER_WRITE);
389	if (!i82586_proberam(sc)) {
390		DPRINTF(("\n%s: can't talk to i82586!\n",
391			device_xname(self)));
392		bus_space_unmap(ia->ia_iot, ioh, ia->ia_io[0].ir_size);
393		bus_space_unmap(ia->ia_memt, memh, ia->ia_iomem[0].ir_size);
394		return;
395	}
396
397	val = bus_space_read_1(asc->sc_regt, asc->sc_regh, AI_REVISION);
398	asc->card_rev = SL_REV(val);
399	asc->card_type = SL_BOARD(val) - 1;
400	snprintf(name, sizeof(name), "%s, rev. %d",
401	    ai_names[asc->card_type], asc->card_rev);
402
403	i82586_attach(sc, name, ethaddr, NULL, 0, 0);
404
405	asc->sc_ih = isa_intr_establish(ia->ia_ic, ia->ia_irq[0].ir_irq,
406	    IST_EDGE, IPL_NET, i82586_intr, sc);
407	if (asc->sc_ih == NULL) {
408		DPRINTF(("\n%s: can't establish interrupt\n",
409			device_xname(self)));
410	}
411}
412
413/*
414 * Divine the memory size of this board.
415 * Better hope there's nothing important hiding just below the card...
416 */
417static int
418ai_find_mem_size(struct ai_softc* asc, bus_space_tag_t memt, bus_size_t maddr)
419{
420	int size;
421	bus_space_handle_t memh;
422	struct ie_softc* sc = &asc->sc_ie;
423
424	for (size = 65536; size >= 16384; size -= 16384) {
425		if (bus_space_map(memt, maddr, size, 0, &memh) == 0) {
426			size = check_ie_present(sc, memt, maddr, size);
427			bus_space_unmap(memt, memh, size);
428
429			if (size != 0)
430				return size;
431		}
432	}
433
434	return (0);
435}
436
437/*
438 * Check to see if there's an 82586 out there.
439 */
440static int
441check_ie_present(struct ie_softc* sc, bus_space_tag_t memt, bus_space_handle_t memh, bus_size_t size)
442{
443	sc->hwreset = ai_reset;
444	sc->chan_attn = ai_atten;
445	sc->ie_bus_read16 = ai_read_16;
446	sc->ie_bus_write16 = ai_write_16;
447
448	sc->bt = memt;
449	sc->bh = memh;
450	sc->sc_iobase = (char *)memh + size - (1 << 24);
451
452	sc->scp = size + IE_SCP_ADDR - (1 << 24);
453	bus_space_set_region_1(memt, memh, (u_long) sc->scp, 0, IE_SCP_SZ);
454
455	sc->iscp = 0;
456	bus_space_set_region_1(memt, memh, (u_long) sc->iscp, 0, IE_ISCP_SZ);
457
458	sc->scb = IE_ISCP_SZ;
459	bus_space_set_region_1(memt, memh, sc->scb, 0, IE_SCB_SZ);
460
461	/* set card to 16-bit bus mode */
462	bus_space_write_1(sc->bt, sc->bh, IE_SCP_BUS_USE((u_long)sc->scp),
463			  IE_SYSBUS_16BIT);
464
465	/* set up pointers to key structures */
466	ai_write_24(sc, IE_SCP_ISCP((u_long)sc->scp), (u_long) sc->iscp);
467	ai_write_16(sc, IE_ISCP_SCB((u_long)sc->iscp), (u_long) sc->scb);
468	ai_write_24(sc, IE_ISCP_BASE((u_long)sc->iscp), (u_long) sc->iscp);
469
470	/* flush setup of pointers, check if chip answers */
471	bus_space_barrier(sc->bt, sc->bh, 0, sc->sc_msize,
472			  BUS_SPACE_BARRIER_WRITE);
473
474	if (!i82586_proberam(sc))
475		return (0);
476
477	return (size);
478}
479
480CFATTACH_DECL_NEW(ai, sizeof(struct ai_softc),
481    ai_match, ai_attach, NULL, NULL);
482