if_ae.c revision 1.74 1 /* $NetBSD: if_ae.c,v 1.74 2003/01/22 21:47:47 bouyer Exp $ */
2
3 /*
4 * Device driver for National Semiconductor DS8390/WD83C690 based ethernet
5 * adapters.
6 *
7 * Copyright (c) 1994, 1995 Charles M. Hannum. All rights reserved.
8 *
9 * Copyright (C) 1993, David Greenman. This software may be used, modified,
10 * copied, distributed, and sold, in both source and binary form provided that
11 * the above copyright and these terms are retained. Under no circumstances is
12 * the author responsible for the proper functioning of this software, nor does
13 * the author assume any responsibility for damages incurred with its use.
14 */
15
16 #include "bpfilter.h"
17
18 #include <sys/param.h>
19 #include <sys/systm.h>
20 #include <sys/device.h>
21 #include <sys/mbuf.h>
22 #include <sys/socket.h>
23
24 #include <net/if.h>
25 #include <net/if_media.h>
26 #include <net/if_ether.h>
27
28 #include <machine/bus.h>
29
30 #include <dev/ic/dp8390reg.h>
31 #include <dev/ic/dp8390var.h>
32 #include <mac68k/dev/if_aevar.h>
33
34 int
35 ae_size_card_memory(bst, bsh, ofs)
36 bus_space_tag_t bst;
37 bus_space_handle_t bsh;
38 int ofs;
39 {
40 int i1, i2, i3, i4, i8;
41
42 /*
43 * banks; also assume it will generally mirror in upper banks
44 * if not installed.
45 */
46 i1 = (8192 * 0);
47 i2 = (8192 * 1);
48 i3 = (8192 * 2);
49 i4 = (8192 * 3);
50
51 i8 = (8192 * 4);
52
53 bus_space_write_2(bst, bsh, ofs + i8, 0x8888);
54 bus_space_write_2(bst, bsh, ofs + i4, 0x4444);
55 bus_space_write_2(bst, bsh, ofs + i3, 0x3333);
56 bus_space_write_2(bst, bsh, ofs + i2, 0x2222);
57 bus_space_write_2(bst, bsh, ofs + i1, 0x1111);
58
59 /*
60 * 1) If the memory range is decoded completely, it does not
61 * matter what we write first: High tags written into
62 * the void are lost.
63 * 2) If the memory range is not decoded completely (banks are
64 * mirrored), high tags are overwritten by lower ones.
65 * 3) Lazy implementation of pathological cases - none found yet.
66 */
67
68 if (bus_space_read_2(bst, bsh, ofs + i1) == 0x1111 &&
69 bus_space_read_2(bst, bsh, ofs + i2) == 0x2222 &&
70 bus_space_read_2(bst, bsh, ofs + i3) == 0x3333 &&
71 bus_space_read_2(bst, bsh, ofs + i4) == 0x4444 &&
72 bus_space_read_2(bst, bsh, ofs + i8) == 0x8888)
73 return 8192 * 8;
74
75 if (bus_space_read_2(bst, bsh, ofs + i1) == 0x1111 &&
76 bus_space_read_2(bst, bsh, ofs + i2) == 0x2222 &&
77 bus_space_read_2(bst, bsh, ofs + i3) == 0x3333 &&
78 bus_space_read_2(bst, bsh, ofs + i4) == 0x4444)
79 return 8192 * 4;
80
81 if ((bus_space_read_2(bst, bsh, ofs + i1) == 0x1111 &&
82 bus_space_read_2(bst, bsh, ofs + i2) == 0x2222) ||
83 (bus_space_read_2(bst, bsh, ofs + i1) == 0x3333 &&
84 bus_space_read_2(bst, bsh, ofs + i2) == 0x4444))
85 return 8192 * 2;
86
87 if (bus_space_read_2(bst, bsh, ofs + i1) == 0x1111 ||
88 bus_space_read_2(bst, bsh, ofs + i1) == 0x4444)
89 return 8192;
90
91 return 0;
92 }
93
94 /*
95 * Zero memory and verify that it is clear. The only difference between
96 * this and the default test_mem function is that the DP8390-based NuBus
97 * cards * apparently require word-wide writes and byte-wide reads, an
98 * `interesting' combination.
99 */
100 int
101 ae_test_mem(sc)
102 struct dp8390_softc *sc;
103 {
104 bus_space_tag_t buft = sc->sc_buft;
105 bus_space_handle_t bufh = sc->sc_bufh;
106 int i;
107
108 bus_space_set_region_2(buft, bufh, sc->mem_start, 0,
109 sc->mem_size / 2);
110
111 for (i = 0; i < sc->mem_size; ++i) {
112 if (bus_space_read_1(sc->sc_buft, sc->sc_bufh, i)) {
113 printf(": failed to clear NIC buffer at offset %x - "
114 "check configuration\n", (sc->mem_start + i));
115 return 1;
116 }
117 }
118
119 return 0;
120 }
121
122 /*
123 * Copy packet from mbuf to the board memory Currently uses an extra
124 * buffer/extra memory copy, unless the whole packet fits in one mbuf.
125 *
126 * As in the test_mem function, we use word-wide writes.
127 */
128 int
129 ae_write_mbuf(sc, m, buf)
130 struct dp8390_softc *sc;
131 struct mbuf *m;
132 int buf;
133 {
134 u_char *data, savebyte[2];
135 int len, wantbyte;
136 u_short totlen = 0;
137
138 wantbyte = 0;
139
140 for (; m ; m = m->m_next) {
141 data = mtod(m, u_char *);
142 len = m->m_len;
143 totlen += len;
144 if (len > 0) {
145 /* Finish the last word. */
146 if (wantbyte) {
147 savebyte[1] = *data;
148 bus_space_write_region_2(sc->sc_buft,
149 sc->sc_bufh, buf, (u_int16_t *)savebyte, 1);
150 buf += 2;
151 data++;
152 len--;
153 wantbyte = 0;
154 }
155 /* Output contiguous words. */
156 if (len > 1) {
157 bus_space_write_region_2(
158 sc->sc_buft, sc->sc_bufh,
159 buf, (u_int16_t *)data, len >> 1);
160 buf += len & ~1;
161 data += len & ~1;
162 len &= 1;
163 }
164 /* Save last byte, if necessary. */
165 if (len == 1) {
166 savebyte[0] = *data;
167 wantbyte = 1;
168 }
169 }
170 }
171
172 if (wantbyte) {
173 savebyte[1] = 0;
174 bus_space_write_region_2(sc->sc_buft, sc->sc_bufh,
175 buf, (u_int16_t *)savebyte, 1);
176 buf += 2;
177 }
178 if (totlen < ETHER_MIN_LEN - ETHER_CRC_LEN) {
179 bus_space_set_region_2(sc->sc_buft, sc->sc_bufh, buf, 0,
180 (ETHER_MIN_LEN - ETHER_CRC_LEN - totlen) >> 1);
181 totlen = ETHER_MIN_LEN - ETHER_CRC_LEN;
182 }
183 return (totlen);
184 }
185