Expand PMF_FN_* macros.
[netbsd-mini2440.git] / sys / dev / isa / if_ai.c
blobebcce4eed1b44feef1fb7ff4a8e9ea5aba9c9bd8
1 /* $NetBSD: if_ai.c,v 1.31 2009/05/12 08:44:19 cegger Exp $ */
3 /*-
4 * Copyright (c) 1998 The NetBSD Foundation, Inc.
5 * All rights reserved.
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Rafal K. Boni.
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.
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.
32 #include <sys/cdefs.h>
33 __KERNEL_RCSID(0, "$NetBSD: if_ai.c,v 1.31 2009/05/12 08:44:19 cegger Exp $");
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>
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>
49 #include <sys/cpu.h>
50 #include <sys/bus.h>
51 #include <sys/intr.h>
53 #include <dev/isa/isareg.h>
54 #include <dev/isa/isavar.h>
56 #include <dev/ic/i82586reg.h>
57 #include <dev/ic/i82586var.h>
58 #include <dev/isa/if_aireg.h>
60 #ifdef AI_DEBUG
61 #define DPRINTF(x) printf x
62 #else
63 #define DPRINTF(x)
64 #endif
66 struct ai_softc {
67 struct ie_softc sc_ie;
69 bus_space_tag_t sc_regt; /* space tag for registers */
70 bus_space_handle_t sc_regh; /* space handle for registers */
72 u_int8_t card_rev;
73 u_int8_t card_type;
75 void *sc_ih; /* interrupt handle */
78 const char *ai_names[] = {
79 "StarLAN 10",
80 "EN100",
81 "StarLAN Fiber",
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);
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);
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);
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);
101 int ai_match(device_t, cfdata_t, void *);
102 void ai_attach(device_t, device_t, void *);
105 * AT&T StarLan support routines
107 static void
108 ai_reset(struct ie_softc *sc, int why)
110 struct ai_softc* asc = (struct ai_softc *) sc;
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;
119 case CARD_RESET:
121 * this takes around 10sec, and we can get
122 * by quite well w/out it...
124 break;
128 static void
129 ai_atten(struct ie_softc *sc, int why)
131 struct ai_softc* asc = (struct ai_softc *) sc;
132 bus_space_write_1(asc->sc_regt, asc->sc_regh, AI_ATTN, 0);
135 static void
136 ai_copyin (struct ie_softc *sc, void *dst, int offset, size_t size)
138 int dribble;
139 u_int8_t* bptr = dst;
141 bus_space_barrier(sc->bt, sc->bh, offset, size,
142 BUS_SPACE_BARRIER_READ);
144 if (offset % 2) {
145 *bptr = bus_space_read_1(sc->bt, sc->bh, offset);
146 offset++; bptr++; size--;
149 dribble = size % 2;
150 bus_space_read_region_2(sc->bt, sc->bh, offset, (u_int16_t *) bptr,
151 size >> 1);
153 if (dribble) {
154 bptr += size - 1;
155 offset += size - 1;
156 *bptr = bus_space_read_1(sc->bt, sc->bh, offset);
160 static void
161 ai_copyout (struct ie_softc *sc, const void *src, int offset, size_t size)
163 int dribble;
164 int osize = size;
165 int ooffset = offset;
166 const u_int8_t* bptr = src;
168 if (offset % 2) {
169 bus_space_write_1(sc->bt, sc->bh, offset, *bptr);
170 offset++; bptr++; size--;
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);
182 bus_space_barrier(sc->bt, sc->bh, ooffset, osize,
183 BUS_SPACE_BARRIER_WRITE);
186 static u_int16_t
187 ai_read_16 (struct ie_softc *sc, int offset)
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);
193 static void
194 ai_write_16 (struct ie_softc *sc, int offset, u_int16_t value)
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);
200 static void
201 ai_write_24 (struct ie_softc *sc, int offset, int addr)
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);
209 ai_match(device_t parent, cfdata_t cf, void *aux)
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;
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);
226 if (ISA_DIRECT_CONFIG(ia))
227 return (0);
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);
238 iot = ia->ia_iot;
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!
244 if (bus_space_map(iot, ia->ia_io[0].ir_addr,
245 AI_IOSIZE, 0, &ioh) != 0) {
247 DPRINTF(("ai_match: cannot map %d IO ports @ 0x%x\n",
248 AI_IOSIZE, ia->ia_iobase));
249 return (0);
252 val = bus_space_read_1(iot, ioh, AI_REVISION);
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;
263 * Fill in just about enough of our local `ai_softc' for
264 * ai_find_mem_size() to do its job.
266 memset(&asc, 0, sizeof asc);
267 asc.sc_regt = iot;
268 asc.sc_regh = ioh;
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;
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;
285 rv = 1;
287 ia->ia_nio = 1;
288 ia->ia_io[0].ir_size = AI_IOSIZE;
290 ia->ia_niomem = 1;
291 ia->ia_iomem[0].ir_size = memsize;
293 ia->ia_nirq = 1;
295 ia->ia_ndrq = 0;
297 DPRINTF(("ai_match: found board @ 0x%x\n", ia->ia_iobase));
299 out:
300 bus_space_unmap(iot, ioh, AI_IOSIZE);
301 return rv;
304 void
305 ai_attach(device_t parent, device_t self, void *aux)
307 struct ai_softc *asc = (void *)self;
308 struct ie_softc *sc = &asc->sc_ie;
309 struct isa_attach_args *ia = aux;
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];
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;
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;
335 asc->sc_regt = ia->ia_iot;
336 asc->sc_regh = ioh;
338 sc->hwinit = NULL;
339 sc->intrhook = NULL;
340 sc->hwreset = ai_reset;
341 sc->chan_attn = ai_atten;
343 sc->ie_bus_barrier = NULL;
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;
351 sc->do_xmitnopchain = 0;
353 sc->sc_mediachange = NULL;
354 sc->sc_mediastatus = NULL;
356 sc->bt = ia->ia_memt;
357 sc->bh = memh;
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);
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);
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;
372 /* zero card memory */
373 bus_space_set_region_1(sc->bt, sc->bh, 0, 0, sc->sc_msize);
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);
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);
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;
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);
401 i82586_attach(sc, name, ethaddr, NULL, 0, 0);
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)));
412 * Divine the memory size of this board.
413 * Better hope there's nothing important hiding just below the card...
415 static int
416 ai_find_mem_size(struct ai_softc* asc, bus_space_tag_t memt, bus_size_t maddr)
418 int size;
419 bus_space_handle_t memh;
420 struct ie_softc* sc = &asc->sc_ie;
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);
427 if (size != 0)
428 return size;
432 return (0);
436 * Check to see if there's an 82586 out there.
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)
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;
446 sc->bt = memt;
447 sc->bh = memh;
448 sc->sc_iobase = (char *)memh + size - (1 << 24);
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);
453 sc->iscp = 0;
454 bus_space_set_region_1(memt, memh, (u_long) sc->iscp, 0, IE_ISCP_SZ);
456 sc->scb = IE_ISCP_SZ;
457 bus_space_set_region_1(memt, memh, sc->scb, 0, IE_SCB_SZ);
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);
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);
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);
472 if (!i82586_proberam(sc))
473 return (0);
475 return (size);
478 CFATTACH_DECL(ai, sizeof(struct ai_softc),
479 ai_match, ai_attach, NULL, NULL);