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