mx98905.c revision 1.1 1 1.1 bjh21 /* $NetBSD: mx98905.c,v 1.1 2001/12/15 17:47:35 bjh21 Exp $ */
2 1.1 bjh21
3 1.1 bjh21 /*-
4 1.1 bjh21 * Copyright (c) 1997, 1998 The NetBSD Foundation, Inc.
5 1.1 bjh21 * All rights reserved.
6 1.1 bjh21 *
7 1.1 bjh21 * This code is derived from software contributed to The NetBSD Foundation
8 1.1 bjh21 * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
9 1.1 bjh21 * NASA Ames Research Center.
10 1.1 bjh21 *
11 1.1 bjh21 * Redistribution and use in source and binary forms, with or without
12 1.1 bjh21 * modification, are permitted provided that the following conditions
13 1.1 bjh21 * are met:
14 1.1 bjh21 * 1. Redistributions of source code must retain the above copyright
15 1.1 bjh21 * notice, this list of conditions and the following disclaimer.
16 1.1 bjh21 * 2. Redistributions in binary form must reproduce the above copyright
17 1.1 bjh21 * notice, this list of conditions and the following disclaimer in the
18 1.1 bjh21 * documentation and/or other materials provided with the distribution.
19 1.1 bjh21 * 3. All advertising materials mentioning features or use of this software
20 1.1 bjh21 * must display the following acknowledgement:
21 1.1 bjh21 * This product includes software developed by the NetBSD
22 1.1 bjh21 * Foundation, Inc. and its contributors.
23 1.1 bjh21 * 4. Neither the name of The NetBSD Foundation nor the names of its
24 1.1 bjh21 * contributors may be used to endorse or promote products derived
25 1.1 bjh21 * from this software without specific prior written permission.
26 1.1 bjh21 *
27 1.1 bjh21 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
28 1.1 bjh21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
29 1.1 bjh21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30 1.1 bjh21 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
31 1.1 bjh21 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32 1.1 bjh21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33 1.1 bjh21 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34 1.1 bjh21 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35 1.1 bjh21 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36 1.1 bjh21 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37 1.1 bjh21 * POSSIBILITY OF SUCH DAMAGE.
38 1.1 bjh21 */
39 1.1 bjh21
40 1.1 bjh21 /*
41 1.1 bjh21 * Device driver for National Semiconductor DS8390/WD83C690 based ethernet
42 1.1 bjh21 * adapters.
43 1.1 bjh21 *
44 1.1 bjh21 * Copyright (c) 1994, 1995 Charles M. Hannum. All rights reserved.
45 1.1 bjh21 *
46 1.1 bjh21 * Copyright (C) 1993, David Greenman. This software may be used, modified,
47 1.1 bjh21 * copied, distributed, and sold, in both source and binary form provided that
48 1.1 bjh21 * the above copyright and these terms are retained. Under no circumstances is
49 1.1 bjh21 * the author responsible for the proper functioning of this software, nor does
50 1.1 bjh21 * the author assume any responsibility for damages incurred with its use.
51 1.1 bjh21 */
52 1.1 bjh21
53 1.1 bjh21 /*
54 1.1 bjh21 * Special routines for the Macronix MX 98905. For use with the "ne" driver.
55 1.1 bjh21 */
56 1.1 bjh21
57 1.1 bjh21 /*
58 1.1 bjh21 * <URL:http://mail-index.netbsd.org/port-arm32/1996/06/23/0005.html>:
59 1.1 bjh21 * There are 2 types of etherh card. One uses the macronics chipset MX98905
60 1.1 bjh21 * and that chipset has a bug in it, in that it the MSB remote dma
61 1.1 bjh21 * register does not work. There is a workaround for this which
62 1.1 bjh21 * should be around soon. In fact, I think only the buffer ram test
63 1.1 bjh21 * ever transfers more than 256 bytes across the dma channel, so diabling
64 1.1 bjh21 * it will make the mx stuff work.
65 1.1 bjh21 */
66 1.1 bjh21
67 1.1 bjh21 #include <sys/param.h>
68 1.1 bjh21
69 1.1 bjh21 __KERNEL_RCSID(0, "$NetBSD: mx98905.c,v 1.1 2001/12/15 17:47:35 bjh21 Exp $");
70 1.1 bjh21
71 1.1 bjh21 #include <sys/device.h>
72 1.1 bjh21 #include <sys/mbuf.h>
73 1.1 bjh21 #include <sys/socket.h>
74 1.1 bjh21 #include <sys/syslog.h>
75 1.1 bjh21 #include <sys/systm.h>
76 1.1 bjh21
77 1.1 bjh21 #include <net/if.h>
78 1.1 bjh21 #include <net/if_ether.h>
79 1.1 bjh21 #include <net/if_media.h>
80 1.1 bjh21
81 1.1 bjh21 #include <machine/bus.h>
82 1.1 bjh21
83 1.1 bjh21 #include <dev/ic/dp8390reg.h>
84 1.1 bjh21 #include <dev/ic/dp8390var.h>
85 1.1 bjh21 #include <dev/ic/ne2000reg.h>
86 1.1 bjh21 #include <dev/ic/ne2000var.h>
87 1.1 bjh21 #include <dev/ic/mx98905var.h>
88 1.1 bjh21
89 1.1 bjh21 #ifndef __BUS_SPACE_HAS_STREAM_METHODS
90 1.1 bjh21 #define bus_space_write_stream_2 bus_space_write_2
91 1.1 bjh21 #define bus_space_write_multi_stream_2 bus_space_write_multi_2
92 1.1 bjh21 #define bus_space_read_multi_stream_2 bus_space_read_multi_2
93 1.1 bjh21 #endif /* __BUS_SPACE_HAS_STREAM_METHODS */
94 1.1 bjh21
95 1.1 bjh21 void
96 1.1 bjh21 mx98905_attach(struct dp8390_softc *sc)
97 1.1 bjh21 {
98 1.1 bjh21
99 1.1 bjh21 sc->ring_copy = mx98905_ring_copy;
100 1.1 bjh21 sc->write_mbuf = mx98905_write_mbuf;
101 1.1 bjh21 sc->read_hdr = mx98905_read_hdr;
102 1.1 bjh21 }
103 1.1 bjh21
104 1.1 bjh21 static __inline void
105 1.1 bjh21 mx98905_write_setup(sc, len, buf)
106 1.1 bjh21 struct dp8390_softc *sc;
107 1.1 bjh21 int len, buf;
108 1.1 bjh21 {
109 1.1 bjh21 bus_space_tag_t nict = sc->sc_regt;
110 1.1 bjh21 bus_space_handle_t nich = sc->sc_regh;
111 1.1 bjh21
112 1.1 bjh21 /* Select page 0 registers. */
113 1.1 bjh21 NIC_BARRIER(nict, nich);
114 1.1 bjh21 bus_space_write_1(nict, nich, ED_P0_CR,
115 1.1 bjh21 ED_CR_RD2 | ED_CR_PAGE_0 | ED_CR_STA);
116 1.1 bjh21 NIC_BARRIER(nict, nich);
117 1.1 bjh21
118 1.1 bjh21 /* Reset remote DMA complete flag. */
119 1.1 bjh21 bus_space_write_1(nict, nich, ED_P0_ISR, ED_ISR_RDC);
120 1.1 bjh21 NIC_BARRIER(nict, nich);
121 1.1 bjh21
122 1.1 bjh21 /* Set up DMA byte count. */
123 1.1 bjh21 bus_space_write_1(nict, nich, ED_P0_RBCR0, len);
124 1.1 bjh21 bus_space_write_1(nict, nich, ED_P0_RBCR1, len >> 8);
125 1.1 bjh21
126 1.1 bjh21 /* Set up destination address in NIC mem. */
127 1.1 bjh21 bus_space_write_1(nict, nich, ED_P0_RSAR0, buf);
128 1.1 bjh21 bus_space_write_1(nict, nich, ED_P0_RSAR1, buf >> 8);
129 1.1 bjh21
130 1.1 bjh21 /* Set remote DMA write. */
131 1.1 bjh21 NIC_BARRIER(nict, nich);
132 1.1 bjh21 bus_space_write_1(nict, nich,
133 1.1 bjh21 ED_P0_CR, ED_CR_RD1 | ED_CR_PAGE_0 | ED_CR_STA);
134 1.1 bjh21 NIC_BARRIER(nict, nich);
135 1.1 bjh21 }
136 1.1 bjh21
137 1.1 bjh21
138 1.1 bjh21 static __inline void
139 1.1 bjh21 mx98905_write_wait(sc)
140 1.1 bjh21 struct dp8390_softc *sc;
141 1.1 bjh21 {
142 1.1 bjh21 int maxwait = 100; /* about 120us */
143 1.1 bjh21 bus_space_tag_t nict = sc->sc_regt;
144 1.1 bjh21 bus_space_handle_t nich = sc->sc_regh;
145 1.1 bjh21
146 1.1 bjh21 /*
147 1.1 bjh21 * Wait for remote DMA to complete. This is necessary because on the
148 1.1 bjh21 * transmit side, data is handled internally by the NIC in bursts, and
149 1.1 bjh21 * we can't start another remote DMA until this one completes. Not
150 1.1 bjh21 * waiting causes really bad things to happen - like the NIC wedging
151 1.1 bjh21 * the bus.
152 1.1 bjh21 */
153 1.1 bjh21 while (((bus_space_read_1(nict, nich, ED_P0_ISR) & ED_ISR_RDC) !=
154 1.1 bjh21 ED_ISR_RDC) && --maxwait) {
155 1.1 bjh21 bus_space_read_1(nict, nich, ED_P0_CRDA1);
156 1.1 bjh21 bus_space_read_1(nict, nich, ED_P0_CRDA0);
157 1.1 bjh21 NIC_BARRIER(nict, nich);
158 1.1 bjh21 DELAY(1);
159 1.1 bjh21 }
160 1.1 bjh21
161 1.1 bjh21 if (maxwait == 0) {
162 1.1 bjh21 log(LOG_WARNING,
163 1.1 bjh21 "%s: remote transmit DMA failed to complete\n",
164 1.1 bjh21 sc->sc_dev.dv_xname);
165 1.1 bjh21 dp8390_reset(sc);
166 1.1 bjh21 }
167 1.1 bjh21 }
168 1.1 bjh21
169 1.1 bjh21 /*
170 1.1 bjh21 * Write an mbuf chain to the destination NIC memory address using programmed
171 1.1 bjh21 * I/O.
172 1.1 bjh21 */
173 1.1 bjh21 int
174 1.1 bjh21 mx98905_write_mbuf(sc, m, buf)
175 1.1 bjh21 struct dp8390_softc *sc;
176 1.1 bjh21 struct mbuf *m;
177 1.1 bjh21 int buf;
178 1.1 bjh21 {
179 1.1 bjh21 struct ne2000_softc *nsc = (struct ne2000_softc *)sc;
180 1.1 bjh21 bus_space_tag_t nict = sc->sc_regt;
181 1.1 bjh21 bus_space_handle_t nich = sc->sc_regh;
182 1.1 bjh21 bus_space_tag_t asict = nsc->sc_asict;
183 1.1 bjh21 bus_space_handle_t asich = nsc->sc_asich;
184 1.1 bjh21 int savelen, dmalen, resid, len;
185 1.1 bjh21 u_int8_t *data, savebyte[2];
186 1.1 bjh21 int l, leftover;
187 1.1 bjh21 #ifdef DIAGNOSTIC
188 1.1 bjh21 u_int8_t *lim;
189 1.1 bjh21 #endif
190 1.1 bjh21
191 1.1 bjh21 resid = savelen = m->m_pkthdr.len;
192 1.1 bjh21
193 1.1 bjh21 dmalen = min(resid, 254);
194 1.1 bjh21
195 1.1 bjh21 mx98905_write_setup(sc, dmalen, buf);
196 1.1 bjh21
197 1.1 bjh21 buf += dmalen;
198 1.1 bjh21 resid -= dmalen;
199 1.1 bjh21
200 1.1 bjh21 /*
201 1.1 bjh21 * Transfer the mbuf chain to the NIC memory. NE2000 cards
202 1.1 bjh21 * require that data be transferred as words, and only words,
203 1.1 bjh21 * so that case requires some extra code to patch over odd-length
204 1.1 bjh21 * mbufs.
205 1.1 bjh21 */
206 1.1 bjh21 /* NE2000s are a bit trickier. */
207 1.1 bjh21 /* Start out with no leftover data. */
208 1.1 bjh21 leftover = 0;
209 1.1 bjh21 savebyte[0] = savebyte[1] = 0;
210 1.1 bjh21
211 1.1 bjh21 for (; m != 0; m = m->m_next) {
212 1.1 bjh21 l = m->m_len;
213 1.1 bjh21 if (l == 0)
214 1.1 bjh21 continue;
215 1.1 bjh21 data = mtod(m, u_int8_t *);
216 1.1 bjh21 #ifdef DIAGNOSTIC
217 1.1 bjh21 lim = data + l;
218 1.1 bjh21 #endif
219 1.1 bjh21 while (l > 0) {
220 1.1 bjh21 if (leftover) {
221 1.1 bjh21 /*
222 1.1 bjh21 * Data left over (from mbuf or
223 1.1 bjh21 * realignment). Buffer the next
224 1.1 bjh21 * byte, and write it and the leftover
225 1.1 bjh21 * data out.
226 1.1 bjh21 */
227 1.1 bjh21 savebyte[1] = *data++;
228 1.1 bjh21 l--;
229 1.1 bjh21 bus_space_write_stream_2(asict, asich,
230 1.1 bjh21 NE2000_ASIC_DATA, *(u_int16_t *)savebyte);
231 1.1 bjh21 dmalen -= 2;
232 1.1 bjh21 leftover = 0;
233 1.1 bjh21 } else if (BUS_SPACE_ALIGNED_POINTER(data,
234 1.1 bjh21 u_int16_t) == 0) {
235 1.1 bjh21 /* Unaligned data; buffer the next byte. */
236 1.1 bjh21 savebyte[0] = *data++;
237 1.1 bjh21 l--;
238 1.1 bjh21 leftover = 1;
239 1.1 bjh21 } else {
240 1.1 bjh21 /*
241 1.1 bjh21 * Aligned data; output contiguous
242 1.1 bjh21 * words as much as we can, then
243 1.1 bjh21 * buffer the remaining byte, if any.
244 1.1 bjh21 */
245 1.1 bjh21 len = min(l, dmalen);
246 1.1 bjh21 leftover = len & 1;
247 1.1 bjh21 len &= ~1;
248 1.1 bjh21 bus_space_write_multi_stream_2(asict,
249 1.1 bjh21 asich, NE2000_ASIC_DATA,
250 1.1 bjh21 (u_int16_t *)data, len >> 1);
251 1.1 bjh21 dmalen -= len;
252 1.1 bjh21 data += len;
253 1.1 bjh21 if (leftover)
254 1.1 bjh21 savebyte[0] = *data++;
255 1.1 bjh21 l -= len + leftover;
256 1.1 bjh21 }
257 1.1 bjh21 if (dmalen == 0 && resid > 0) {
258 1.1 bjh21 mx98905_write_wait(sc);
259 1.1 bjh21 dmalen = min(resid, 254);
260 1.1 bjh21
261 1.1 bjh21 mx98905_write_setup(sc, dmalen, buf);
262 1.1 bjh21
263 1.1 bjh21 buf += dmalen;
264 1.1 bjh21 resid -= dmalen;
265 1.1 bjh21 }
266 1.1 bjh21 }
267 1.1 bjh21 if (l < 0)
268 1.1 bjh21 panic("mx98905_write_mbuf: negative len");
269 1.1 bjh21 #ifdef DIAGNOSTIC
270 1.1 bjh21 if (data != lim)
271 1.1 bjh21 panic("mx98905_write_mbuf: data != lim");
272 1.1 bjh21 #endif
273 1.1 bjh21 }
274 1.1 bjh21 if (leftover) {
275 1.1 bjh21 savebyte[1] = 0;
276 1.1 bjh21 bus_space_write_stream_2(asict, asich, NE2000_ASIC_DATA,
277 1.1 bjh21 *(u_int16_t *)savebyte);
278 1.1 bjh21 }
279 1.1 bjh21 NIC_BARRIER(nict, nich);
280 1.1 bjh21
281 1.1 bjh21 mx98905_write_wait(sc);
282 1.1 bjh21
283 1.1 bjh21 return (savelen);
284 1.1 bjh21 }
285 1.1 bjh21
286 1.1 bjh21 /*
287 1.1 bjh21 * Given a source and destination address, copy 'amout' of a packet from
288 1.1 bjh21 * the ring buffer into a linear destination buffer. Takes into account
289 1.1 bjh21 * ring-wrap.
290 1.1 bjh21 */
291 1.1 bjh21 int
292 1.1 bjh21 mx98905_ring_copy(sc, src, dst, amount)
293 1.1 bjh21 struct dp8390_softc *sc;
294 1.1 bjh21 int src;
295 1.1 bjh21 caddr_t dst;
296 1.1 bjh21 u_short amount;
297 1.1 bjh21 {
298 1.1 bjh21 struct ne2000_softc *nsc = (struct ne2000_softc *)sc;
299 1.1 bjh21 bus_space_tag_t nict = sc->sc_regt;
300 1.1 bjh21 bus_space_handle_t nich = sc->sc_regh;
301 1.1 bjh21 bus_space_tag_t asict = nsc->sc_asict;
302 1.1 bjh21 bus_space_handle_t asich = nsc->sc_asich;
303 1.1 bjh21 u_short tmp_amount;
304 1.1 bjh21 int useword = NE2000_USE_WORD(nsc);
305 1.1 bjh21
306 1.1 bjh21 /* Does copy wrap to lower addr in ring buffer? */
307 1.1 bjh21 if (src + amount > sc->mem_end) {
308 1.1 bjh21 tmp_amount = sc->mem_end - src;
309 1.1 bjh21
310 1.1 bjh21 /* Copy amount up to end of NIC memory. */
311 1.1 bjh21 mx98905_readmem(nict, nich, asict, asich, src,
312 1.1 bjh21 (u_int8_t *)dst, tmp_amount, useword);
313 1.1 bjh21
314 1.1 bjh21 amount -= tmp_amount;
315 1.1 bjh21 src = sc->mem_ring;
316 1.1 bjh21 dst += tmp_amount;
317 1.1 bjh21 }
318 1.1 bjh21
319 1.1 bjh21 mx98905_readmem(nict, nich, asict, asich, src, (u_int8_t *)dst,
320 1.1 bjh21 amount, useword);
321 1.1 bjh21
322 1.1 bjh21 return (src + amount);
323 1.1 bjh21 }
324 1.1 bjh21
325 1.1 bjh21 void
326 1.1 bjh21 mx98905_read_hdr(sc, buf, hdr)
327 1.1 bjh21 struct dp8390_softc *sc;
328 1.1 bjh21 int buf;
329 1.1 bjh21 struct dp8390_ring *hdr;
330 1.1 bjh21 {
331 1.1 bjh21 struct ne2000_softc *nsc = (struct ne2000_softc *)sc;
332 1.1 bjh21
333 1.1 bjh21 mx98905_readmem(sc->sc_regt, sc->sc_regh, nsc->sc_asict, nsc->sc_asich,
334 1.1 bjh21 buf, (u_int8_t *)hdr, sizeof(struct dp8390_ring),
335 1.1 bjh21 NE2000_USE_WORD(nsc));
336 1.1 bjh21 #if BYTE_ORDER == BIG_ENDIAN
337 1.1 bjh21 hdr->count = bswap16(hdr->count);
338 1.1 bjh21 #endif
339 1.1 bjh21 }
340 1.1 bjh21
341 1.1 bjh21 static __inline void
342 1.1 bjh21 mx98905_read_setup(bus_space_tag_t nict, bus_space_handle_t nich,
343 1.1 bjh21 int len, int buf)
344 1.1 bjh21 {
345 1.1 bjh21
346 1.1 bjh21 /* Select page 0 registers. */
347 1.1 bjh21 NIC_BARRIER(nict, nich);
348 1.1 bjh21 bus_space_write_1(nict, nich, ED_P0_CR,
349 1.1 bjh21 ED_CR_RD2 | ED_CR_PAGE_0 | ED_CR_STA);
350 1.1 bjh21 NIC_BARRIER(nict, nich);
351 1.1 bjh21
352 1.1 bjh21 /* Set up DMA byte count. */
353 1.1 bjh21 bus_space_write_1(nict, nich, ED_P0_RBCR0, len);
354 1.1 bjh21 bus_space_write_1(nict, nich, ED_P0_RBCR1, len >> 8);
355 1.1 bjh21
356 1.1 bjh21 /* Set up source address in NIC mem. */
357 1.1 bjh21 bus_space_write_1(nict, nich, ED_P0_RSAR0, buf);
358 1.1 bjh21 bus_space_write_1(nict, nich, ED_P0_RSAR1, buf >> 8);
359 1.1 bjh21
360 1.1 bjh21 NIC_BARRIER(nict, nich);
361 1.1 bjh21 bus_space_write_1(nict, nich, ED_P0_CR,
362 1.1 bjh21 ED_CR_RD0 | ED_CR_PAGE_0 | ED_CR_STA);
363 1.1 bjh21
364 1.1 bjh21 bus_space_barrier(nict, nich, 0, NE2000_NPORTS,
365 1.1 bjh21 BUS_SPACE_BARRIER_READ | BUS_SPACE_BARRIER_WRITE);
366 1.1 bjh21 }
367 1.1 bjh21
368 1.1 bjh21 /*
369 1.1 bjh21 * Given a NIC memory source address and a host memory destination address,
370 1.1 bjh21 * copy 'amount' from NIC to host using programmed i/o. The 'amount' is
371 1.1 bjh21 * rounded up to a word - ok as long as mbufs are word sized.
372 1.1 bjh21 */
373 1.1 bjh21 void
374 1.1 bjh21 mx98905_readmem(nict, nich, asict, asich, src, dst, amount, useword)
375 1.1 bjh21 bus_space_tag_t nict;
376 1.1 bjh21 bus_space_handle_t nich;
377 1.1 bjh21 bus_space_tag_t asict;
378 1.1 bjh21 bus_space_handle_t asich;
379 1.1 bjh21 int src;
380 1.1 bjh21 u_int8_t *dst;
381 1.1 bjh21 size_t amount;
382 1.1 bjh21 int useword;
383 1.1 bjh21 {
384 1.1 bjh21 int len, resid;
385 1.1 bjh21
386 1.1 bjh21 resid = amount;
387 1.1 bjh21 /* Round up to a word. */
388 1.1 bjh21 if (resid & 1)
389 1.1 bjh21 ++resid;
390 1.1 bjh21
391 1.1 bjh21 while (resid > 0) {
392 1.1 bjh21 len = min(resid, 254);
393 1.1 bjh21 mx98905_read_setup(nict, nich, len, src);
394 1.1 bjh21 if (useword)
395 1.1 bjh21 bus_space_read_multi_stream_2(asict, asich,
396 1.1 bjh21 NE2000_ASIC_DATA, (u_int16_t *)dst, len >> 1);
397 1.1 bjh21 else
398 1.1 bjh21 bus_space_read_multi_1(asict, asich, NE2000_ASIC_DATA,
399 1.1 bjh21 dst, len);
400 1.1 bjh21 resid -= len;
401 1.1 bjh21 src += len;
402 1.1 bjh21 dst += len;
403 1.1 bjh21 }
404 1.1 bjh21 }
405