ne2000.c revision 1.37 1 /* $NetBSD: ne2000.c,v 1.37 2001/11/13 13:14:42 lukem Exp $ */
2
3 /*-
4 * Copyright (c) 1997, 1998 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
9 * NASA Ames Research Center.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. All advertising materials mentioning features or use of this software
20 * must display the following acknowledgement:
21 * This product includes software developed by the NetBSD
22 * Foundation, Inc. and its contributors.
23 * 4. Neither the name of The NetBSD Foundation nor the names of its
24 * contributors may be used to endorse or promote products derived
25 * from this software without specific prior written permission.
26 *
27 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
28 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
29 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
31 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37 * POSSIBILITY OF SUCH DAMAGE.
38 */
39
40 /*
41 * Device driver for National Semiconductor DS8390/WD83C690 based ethernet
42 * adapters.
43 *
44 * Copyright (c) 1994, 1995 Charles M. Hannum. All rights reserved.
45 *
46 * Copyright (C) 1993, David Greenman. This software may be used, modified,
47 * copied, distributed, and sold, in both source and binary form provided that
48 * the above copyright and these terms are retained. Under no circumstances is
49 * the author responsible for the proper functioning of this software, nor does
50 * the author assume any responsibility for damages incurred with its use.
51 */
52
53 /*
54 * Common code shared by all NE2000-compatible Ethernet interfaces.
55 */
56
57 #include <sys/cdefs.h>
58 __KERNEL_RCSID(0, "$NetBSD: ne2000.c,v 1.37 2001/11/13 13:14:42 lukem Exp $");
59
60 #include "opt_ipkdb.h"
61
62 #include <sys/param.h>
63 #include <sys/systm.h>
64 #include <sys/device.h>
65 #include <sys/socket.h>
66 #include <sys/mbuf.h>
67 #include <sys/syslog.h>
68
69 #include <net/if.h>
70 #include <net/if_dl.h>
71 #include <net/if_types.h>
72 #include <net/if_media.h>
73
74 #include <net/if_ether.h>
75
76 #include <machine/bswap.h>
77 #include <machine/bus.h>
78
79 #ifndef __BUS_SPACE_HAS_STREAM_METHODS
80 #define bus_space_write_stream_2 bus_space_write_2
81 #define bus_space_write_multi_stream_2 bus_space_write_multi_2
82 #define bus_space_read_multi_stream_2 bus_space_read_multi_2
83 #endif /* __BUS_SPACE_HAS_STREAM_METHODS */
84
85 #ifdef IPKDB_NE
86 #include <ipkdb/ipkdb.h>
87 #endif
88
89 #include <dev/ic/dp8390reg.h>
90 #include <dev/ic/dp8390var.h>
91
92 #include <dev/ic/ne2000reg.h>
93 #include <dev/ic/ne2000var.h>
94
95 #include <dev/ic/ax88190reg.h>
96
97 #if BYTE_ORDER == BIG_ENDIAN
98 #include <machine/bswap.h>
99 #endif
100
101 int ne2000_write_mbuf __P((struct dp8390_softc *, struct mbuf *, int));
102 int ne2000_ring_copy __P((struct dp8390_softc *, int, caddr_t, u_short));
103 void ne2000_read_hdr __P((struct dp8390_softc *, int, struct dp8390_ring *));
104 int ne2000_test_mem __P((struct dp8390_softc *));
105
106 void ne2000_writemem __P((bus_space_tag_t, bus_space_handle_t,
107 bus_space_tag_t, bus_space_handle_t, u_int8_t *, int, size_t,
108 int, int));
109 void ne2000_readmem __P((bus_space_tag_t, bus_space_handle_t,
110 bus_space_tag_t, bus_space_handle_t, int, u_int8_t *, size_t, int));
111
112 int
113 ne2000_attach(nsc, myea)
114 struct ne2000_softc *nsc;
115 u_int8_t *myea;
116 {
117 struct dp8390_softc *dsc = &nsc->sc_dp8390;
118 bus_space_tag_t nict = dsc->sc_regt;
119 bus_space_handle_t nich = dsc->sc_regh;
120 bus_space_tag_t asict = nsc->sc_asict;
121 bus_space_handle_t asich = nsc->sc_asich;
122 u_int8_t romdata[16];
123 int memsize, i, useword;
124
125 /*
126 * Detect it again unless caller specified it; this gives us
127 * the memory size.
128 */
129 if (nsc->sc_type == 0) {
130 nsc->sc_type = ne2000_detect(nict, nich, asict, asich);
131 if (nsc->sc_type == 0) {
132 printf("%s: where did the card go?\n",
133 dsc->sc_dev.dv_xname);
134 return (1);
135 }
136 }
137
138 useword = NE2000_USE_WORD(nsc);
139
140 dsc->cr_proto = ED_CR_RD2;
141 if (nsc->sc_type == NE2000_TYPE_AX88190) {
142 dsc->rcr_proto = ED_RCR_INTT;
143 dsc->sc_flags |= DP8390_DO_AX88190_WORKAROUND;
144 } else
145 dsc->rcr_proto = 0;
146
147 /*
148 * DCR gets:
149 *
150 * FIFO threshold to 8, No auto-init Remote DMA,
151 * byte order=80x86.
152 *
153 * NE1000 gets byte-wide DMA, NE2000 gets word-wide DMA.
154 */
155 dsc->dcr_reg = ED_DCR_FT1 | ED_DCR_LS | (useword ? ED_DCR_WTS : 0);
156
157 dsc->test_mem = ne2000_test_mem;
158 dsc->ring_copy = ne2000_ring_copy;
159 dsc->write_mbuf = ne2000_write_mbuf;
160 dsc->read_hdr = ne2000_read_hdr;
161
162 /* Registers are linear. */
163 for (i = 0; i < 16; i++)
164 dsc->sc_reg_map[i] = i;
165
166 /*
167 * 8k of memory for NE1000, 16k for NE2000 and 24k for the
168 * card uses DL10019.
169 */
170 switch (nsc->sc_type) {
171 case NE2000_TYPE_NE1000:
172 memsize = 8192;
173 break;
174 case NE2000_TYPE_NE2000:
175 case NE2000_TYPE_AX88190: /* XXX really? */
176 memsize = 8192 * 2;
177 break;
178 case NE2000_TYPE_DL10019:
179 case NE2000_TYPE_DL10022:
180 memsize = 8192 * 3;
181 break;
182 }
183
184 /*
185 * NIC memory doens't start at zero on an NE board.
186 * The start address is tied to the bus width.
187 * (It happens to be computed the same way as mem size.)
188 */
189 dsc->mem_start = memsize;
190
191 #ifdef GWETHER
192 {
193 int x, mstart = 0;
194 int8_t pbuf0[ED_PAGE_SIZE], pbuf[ED_PAGE_SIZE],
195 tbuf[ED_PAGE_SIZE];
196
197 for (i = 0; i < ED_PAGE_SIZE; i++)
198 pbuf0[i] = 0;
199
200 /* Search for the start of RAM. */
201 for (x = 1; x < 256; x++) {
202 ne2000_writemem(nict, nich, asict, asich, pbuf0,
203 x << ED_PAGE_SHIFT, ED_PAGE_SIZE, useword, 0);
204 ne2000_readmem(nict, nich, asict, asich,
205 x << ED_PAGE_SHIFT, tbuf, ED_PAGE_SIZE, useword);
206 if (memcmp(pbuf0, tbuf, ED_PAGE_SIZE) == 0) {
207 for (i = 0; i < ED_PAGE_SIZE; i++)
208 pbuf[i] = 255 - x;
209 ne2000_writemem(nict, nich, asict, asich,
210 pbuf, x << ED_PAGE_SHIFT, ED_PAGE_SIZE,
211 useword, 0);
212 ne2000_readmem(nict, nich, asict, asich,
213 x << ED_PAGE_SHIFT, tbuf, ED_PAGE_SIZE,
214 useword);
215 if (memcmp(pbuf, tbuf, ED_PAGE_SIZE) == 0) {
216 mstart = x << ED_PAGE_SHIFT;
217 memsize = ED_PAGE_SIZE;
218 break;
219 }
220 }
221 }
222
223 if (mstart == 0) {
224 printf("%s: cannot find start of RAM\n",
225 dsc->sc_dev.dv_xname);
226 return (1);
227 }
228
229 /* Search for the end of RAM. */
230 for (++x; x < 256; x++) {
231 ne2000_writemem(nict, nich, asict, asich, pbuf0,
232 x << ED_PAGE_SHIFT, ED_PAGE_SIZE, useword, 0);
233 ne2000_readmem(nict, nich, asict, asich,
234 x << ED_PAGE_SHIFT, tbuf, ED_PAGE_SIZE, useword);
235 if (memcmp(pbuf0, tbuf, ED_PAGE_SIZE) == 0) {
236 for (i = 0; i < ED_PAGE_SIZE; i++)
237 pbuf[i] = 255 - x;
238 ne2000_writemem(nict, nich, asict, asich,
239 pbuf, x << ED_PAGE_SHIFT, ED_PAGE_SIZE,
240 useword, 0);
241 ne2000_readmem(nict, nich, asict, asich,
242 x << ED_PAGE_SHIFT, tbuf, ED_PAGE_SIZE,
243 useword);
244 if (memcmp(pbuf, tbuf, ED_PAGE_SIZE) == 0)
245 memsize += ED_PAGE_SIZE;
246 else
247 break;
248 } else
249 break;
250 }
251
252 printf("%s: RAM start 0x%x, size %d\n",
253 dsc->sc_dev.dv_xname, mstart, memsize);
254
255 dsc->mem_start = mstart;
256 }
257 #endif /* GWETHER */
258
259 dsc->mem_size = memsize;
260
261 if (myea == NULL) {
262 /* Read the station address. */
263 if (nsc->sc_type == NE2000_TYPE_AX88190) {
264 /* Select page 0 registers. */
265 NIC_BARRIER(nict, nich);
266 bus_space_write_1(nict, nich, ED_P0_CR,
267 ED_CR_RD2 | ED_CR_PAGE_0 | ED_CR_STA);
268 NIC_BARRIER(nict, nich);
269 /* Select word transfer. */
270 bus_space_write_1(nict, nich, ED_P0_DCR, ED_DCR_WTS);
271 NIC_BARRIER(nict, nich);
272 ne2000_readmem(nict, nich, asict, asich,
273 AX88190_NODEID_OFFSET, dsc->sc_enaddr,
274 ETHER_ADDR_LEN, useword);
275 } else {
276 ne2000_readmem(nict, nich, asict, asich, 0, romdata,
277 sizeof(romdata), useword);
278 for (i = 0; i < ETHER_ADDR_LEN; i++)
279 dsc->sc_enaddr[i] =
280 romdata[i * (useword ? 2 : 1)];
281 }
282 } else
283 memcpy(dsc->sc_enaddr, myea, sizeof(dsc->sc_enaddr));
284
285 /* Clear any pending interrupts that might have occurred above. */
286 NIC_BARRIER(nict, nich);
287 bus_space_write_1(nict, nich, ED_P0_ISR, 0xff);
288 NIC_BARRIER(nict, nich);
289
290 if (dsc->sc_media_init == NULL)
291 dsc->sc_media_init = dp8390_media_init;
292
293 if (dp8390_config(dsc)) {
294 printf("%s: setup failed\n", dsc->sc_dev.dv_xname);
295 return (1);
296 }
297
298 /*
299 * We need to compute mem_ring a bit differently; override the
300 * value set up in dp8390_config().
301 */
302 dsc->mem_ring =
303 dsc->mem_start + ((dsc->txb_cnt * ED_TXBUF_SIZE) << ED_PAGE_SHIFT);
304
305 return (0);
306 }
307
308 /*
309 * Detect an NE-2000 or compatible. Returns a model code.
310 */
311 int
312 ne2000_detect(nict, nich, asict, asich)
313 bus_space_tag_t nict;
314 bus_space_handle_t nich;
315 bus_space_tag_t asict;
316 bus_space_handle_t asich;
317 {
318 static u_int8_t test_pattern[32] = "THIS is A memory TEST pattern";
319 u_int8_t test_buffer[32], tmp;
320 int i, rv = 0;
321
322 /* Reset the board. */
323 #ifdef GWETHER
324 bus_space_write_1(asict, asich, NE2000_ASIC_RESET, 0);
325 bus_space_barrier(nict, nich, 0, NE2000_NPORTS,
326 BUS_SPACE_BARRIER_READ | BUS_SPACE_BARRIER_WRITE);
327 delay(200);
328 #endif /* GWETHER */
329 tmp = bus_space_read_1(asict, asich, NE2000_ASIC_RESET);
330 bus_space_barrier(nict, nich, 0, NE2000_NPORTS,
331 BUS_SPACE_BARRIER_READ | BUS_SPACE_BARRIER_WRITE);
332 delay(10000);
333
334 /*
335 * I don't know if this is necessary; probably cruft leftover from
336 * Clarkson packet driver code. Doesn't do a thing on the boards I've
337 * tested. -DG [note that a outb(0x84, 0) seems to work here, and is
338 * non-invasive...but some boards don't seem to reset and I don't have
339 * complete documentation on what the 'right' thing to do is...so we do
340 * the invasive thing for now. Yuck.]
341 */
342 bus_space_write_1(asict, asich, NE2000_ASIC_RESET, tmp);
343 bus_space_barrier(nict, nich, 0, NE2000_NPORTS,
344 BUS_SPACE_BARRIER_READ | BUS_SPACE_BARRIER_WRITE);
345 delay(5000);
346
347 /*
348 * This is needed because some NE clones apparently don't reset the
349 * NIC properly (or the NIC chip doesn't reset fully on power-up).
350 * XXX - this makes the probe invasive! Done against my better
351 * judgement. -DLG
352 */
353 bus_space_write_1(nict, nich, ED_P0_CR,
354 ED_CR_RD2 | ED_CR_PAGE_0 | ED_CR_STP);
355 NIC_BARRIER(nict, nich);
356
357 delay(5000);
358
359 /*
360 * Generic probe routine for testing for the existance of a DS8390.
361 * Must be performed after the NIC has just been reset. This
362 * works by looking at certain register values that are guaranteed
363 * to be initialized a certain way after power-up or reset.
364 *
365 * Specifically:
366 *
367 * Register reset bits set bits
368 * -------- ---------- --------
369 * CR TXP, STA RD2, STP
370 * ISR RST
371 * IMR <all>
372 * DCR LAS
373 * TCR LB1, LB0
374 *
375 * We only look at CR and ISR, however, since looking at the others
376 * would require changing register pages, which would be intrusive
377 * if this isn't an 8390.
378 */
379
380 tmp = bus_space_read_1(nict, nich, ED_P0_CR);
381 if ((tmp & (ED_CR_RD2 | ED_CR_TXP | ED_CR_STA | ED_CR_STP)) !=
382 (ED_CR_RD2 | ED_CR_STP))
383 goto out;
384
385 tmp = bus_space_read_1(nict, nich, ED_P0_ISR);
386 if ((tmp & ED_ISR_RST) != ED_ISR_RST)
387 goto out;
388
389 bus_space_write_1(nict, nich,
390 ED_P0_CR, ED_CR_RD2 | ED_CR_PAGE_0 | ED_CR_STA);
391 NIC_BARRIER(nict, nich);
392
393 for (i = 0; i < 100; i++) {
394 if ((bus_space_read_1(nict, nich, ED_P0_ISR) & ED_ISR_RST) ==
395 ED_ISR_RST) {
396 /* Ack the reset bit. */
397 bus_space_write_1(nict, nich, ED_P0_ISR, ED_ISR_RST);
398 NIC_BARRIER(nict, nich);
399 break;
400 }
401 delay(100);
402 }
403
404 #if 0
405 /* XXX */
406 if (i == 100)
407 goto out;
408 #endif
409
410 /*
411 * Test the ability to read and write to the NIC memory. This has
412 * the side effect of determining if this is an NE1000 or an NE2000.
413 */
414
415 /*
416 * This prevents packets from being stored in the NIC memory when
417 * the readmem routine turns on the start bit in the CR.
418 */
419 bus_space_write_1(nict, nich, ED_P0_RCR, ED_RCR_MON);
420 NIC_BARRIER(nict, nich);
421
422 /* Temporarily initialize DCR for byte operations. */
423 bus_space_write_1(nict, nich, ED_P0_DCR, ED_DCR_FT1 | ED_DCR_LS);
424
425 bus_space_write_1(nict, nich, ED_P0_PSTART, 8192 >> ED_PAGE_SHIFT);
426 bus_space_write_1(nict, nich, ED_P0_PSTOP, 16384 >> ED_PAGE_SHIFT);
427
428 /*
429 * Write a test pattern in byte mode. If this fails, then there
430 * probably isn't any memory at 8k - which likely means that the
431 * board is an NE2000.
432 */
433 ne2000_writemem(nict, nich, asict, asich, test_pattern, 8192,
434 sizeof(test_pattern), 0, 1);
435 ne2000_readmem(nict, nich, asict, asich, 8192, test_buffer,
436 sizeof(test_buffer), 0);
437
438 if (memcmp(test_pattern, test_buffer, sizeof(test_pattern))) {
439 /* not an NE1000 - try NE2000 */
440 bus_space_write_1(nict, nich, ED_P0_DCR,
441 ED_DCR_WTS | ED_DCR_FT1 | ED_DCR_LS);
442 bus_space_write_1(nict, nich, ED_P0_PSTART,
443 16384 >> ED_PAGE_SHIFT);
444 bus_space_write_1(nict, nich, ED_P0_PSTOP,
445 32768 >> ED_PAGE_SHIFT);
446
447 /*
448 * Write the test pattern in word mode. If this also fails,
449 * then we don't know what this board is.
450 */
451 ne2000_writemem(nict, nich, asict, asich, test_pattern, 16384,
452 sizeof(test_pattern), 1, 0);
453 ne2000_readmem(nict, nich, asict, asich, 16384, test_buffer,
454 sizeof(test_buffer), 1);
455
456 if (memcmp(test_pattern, test_buffer, sizeof(test_pattern)))
457 goto out; /* not an NE2000 either */
458
459 rv = NE2000_TYPE_NE2000;
460 } else {
461 /* We're an NE1000. */
462 rv = NE2000_TYPE_NE1000;
463 }
464
465 /* Clear any pending interrupts that might have occurred above. */
466 NIC_BARRIER(nict, nich);
467 bus_space_write_1(nict, nich, ED_P0_ISR, 0xff);
468
469 out:
470 return (rv);
471 }
472
473 /*
474 * Write an mbuf chain to the destination NIC memory address using programmed
475 * I/O.
476 */
477 int
478 ne2000_write_mbuf(sc, m, buf)
479 struct dp8390_softc *sc;
480 struct mbuf *m;
481 int buf;
482 {
483 struct ne2000_softc *nsc = (struct ne2000_softc *)sc;
484 bus_space_tag_t nict = sc->sc_regt;
485 bus_space_handle_t nich = sc->sc_regh;
486 bus_space_tag_t asict = nsc->sc_asict;
487 bus_space_handle_t asich = nsc->sc_asich;
488 int savelen;
489 int maxwait = 100; /* about 120us */
490
491 savelen = m->m_pkthdr.len;
492
493 /* Select page 0 registers. */
494 NIC_BARRIER(nict, nich);
495 bus_space_write_1(nict, nich, ED_P0_CR,
496 ED_CR_RD2 | ED_CR_PAGE_0 | ED_CR_STA);
497 NIC_BARRIER(nict, nich);
498
499 /* Reset remote DMA complete flag. */
500 bus_space_write_1(nict, nich, ED_P0_ISR, ED_ISR_RDC);
501 NIC_BARRIER(nict, nich);
502
503 /* Set up DMA byte count. */
504 bus_space_write_1(nict, nich, ED_P0_RBCR0, savelen);
505 bus_space_write_1(nict, nich, ED_P0_RBCR1, savelen >> 8);
506
507 /* Set up destination address in NIC mem. */
508 bus_space_write_1(nict, nich, ED_P0_RSAR0, buf);
509 bus_space_write_1(nict, nich, ED_P0_RSAR1, buf >> 8);
510
511 /* Set remote DMA write. */
512 NIC_BARRIER(nict, nich);
513 bus_space_write_1(nict, nich,
514 ED_P0_CR, ED_CR_RD1 | ED_CR_PAGE_0 | ED_CR_STA);
515 NIC_BARRIER(nict, nich);
516
517 /*
518 * Transfer the mbuf chain to the NIC memory. NE2000 cards
519 * require that data be transferred as words, and only words,
520 * so that case requires some extra code to patch over odd-length
521 * mbufs.
522 */
523 if (nsc->sc_type == NE2000_TYPE_NE1000) {
524 /* NE1000s are easy. */
525 for (; m != 0; m = m->m_next) {
526 if (m->m_len) {
527 bus_space_write_multi_1(asict, asich,
528 NE2000_ASIC_DATA, mtod(m, u_int8_t *),
529 m->m_len);
530 }
531 }
532 } else {
533 /* NE2000s are a bit trickier. */
534 u_int8_t *data, savebyte[2];
535 int l, leftover;
536 #ifdef DIAGNOSTIC
537 u_int8_t *lim;
538 #endif
539 /* Start out with no leftover data. */
540 leftover = 0;
541 savebyte[0] = savebyte[1] = 0;
542
543 for (; m != 0; m = m->m_next) {
544 l = m->m_len;
545 if (l == 0)
546 continue;
547 data = mtod(m, u_int8_t *);
548 #ifdef DIAGNOSTIC
549 lim = data + l;
550 #endif
551 while (l > 0) {
552 if (leftover) {
553 /*
554 * Data left over (from mbuf or
555 * realignment). Buffer the next
556 * byte, and write it and the
557 * leftover data out.
558 */
559 savebyte[1] = *data++;
560 l--;
561 bus_space_write_stream_2(asict, asich,
562 NE2000_ASIC_DATA,
563 *(u_int16_t *)savebyte);
564 leftover = 0;
565 } else if (BUS_SPACE_ALIGNED_POINTER(data,
566 u_int16_t) == 0) {
567 /*
568 * Unaligned data; buffer the next
569 * byte.
570 */
571 savebyte[0] = *data++;
572 l--;
573 leftover = 1;
574 } else {
575 /*
576 * Aligned data; output contiguous
577 * words as much as we can, then
578 * buffer the remaining byte, if any.
579 */
580 leftover = l & 1;
581 l &= ~1;
582 bus_space_write_multi_stream_2(asict,
583 asich, NE2000_ASIC_DATA,
584 (u_int16_t *)data, l >> 1);
585 data += l;
586 if (leftover)
587 savebyte[0] = *data++;
588 l = 0;
589 }
590 }
591 if (l < 0)
592 panic("ne2000_write_mbuf: negative len");
593 #ifdef DIAGNOSTIC
594 if (data != lim)
595 panic("ne2000_write_mbuf: data != lim");
596 #endif
597 }
598 if (leftover) {
599 savebyte[1] = 0;
600 bus_space_write_stream_2(asict, asich, NE2000_ASIC_DATA,
601 *(u_int16_t *)savebyte);
602 }
603 }
604 NIC_BARRIER(nict, nich);
605
606 /*
607 * Wait for remote DMA to complete. This is necessary because on the
608 * transmit side, data is handled internally by the NIC in bursts, and
609 * we can't start another remote DMA until this one completes. Not
610 * waiting causes really bad things to happen - like the NIC wedging
611 * the bus.
612 */
613 while (((bus_space_read_1(nict, nich, ED_P0_ISR) & ED_ISR_RDC) !=
614 ED_ISR_RDC) && --maxwait) {
615 bus_space_read_1(nict, nich, ED_P0_CRDA1);
616 bus_space_read_1(nict, nich, ED_P0_CRDA0);
617 NIC_BARRIER(nict, nich);
618 DELAY(1);
619 }
620
621 if (maxwait == 0) {
622 log(LOG_WARNING,
623 "%s: remote transmit DMA failed to complete\n",
624 sc->sc_dev.dv_xname);
625 dp8390_reset(sc);
626 }
627
628 return (savelen);
629 }
630
631 /*
632 * Given a source and destination address, copy 'amout' of a packet from
633 * the ring buffer into a linear destination buffer. Takes into account
634 * ring-wrap.
635 */
636 int
637 ne2000_ring_copy(sc, src, dst, amount)
638 struct dp8390_softc *sc;
639 int src;
640 caddr_t dst;
641 u_short amount;
642 {
643 struct ne2000_softc *nsc = (struct ne2000_softc *)sc;
644 bus_space_tag_t nict = sc->sc_regt;
645 bus_space_handle_t nich = sc->sc_regh;
646 bus_space_tag_t asict = nsc->sc_asict;
647 bus_space_handle_t asich = nsc->sc_asich;
648 u_short tmp_amount;
649 int useword = NE2000_USE_WORD(nsc);
650
651 /* Does copy wrap to lower addr in ring buffer? */
652 if (src + amount > sc->mem_end) {
653 tmp_amount = sc->mem_end - src;
654
655 /* Copy amount up to end of NIC memory. */
656 ne2000_readmem(nict, nich, asict, asich, src,
657 (u_int8_t *)dst, tmp_amount, useword);
658
659 amount -= tmp_amount;
660 src = sc->mem_ring;
661 dst += tmp_amount;
662 }
663
664 ne2000_readmem(nict, nich, asict, asich, src, (u_int8_t *)dst,
665 amount, useword);
666
667 return (src + amount);
668 }
669
670 void
671 ne2000_read_hdr(sc, buf, hdr)
672 struct dp8390_softc *sc;
673 int buf;
674 struct dp8390_ring *hdr;
675 {
676 struct ne2000_softc *nsc = (struct ne2000_softc *)sc;
677
678 ne2000_readmem(sc->sc_regt, sc->sc_regh, nsc->sc_asict, nsc->sc_asich,
679 buf, (u_int8_t *)hdr, sizeof(struct dp8390_ring),
680 NE2000_USE_WORD(nsc));
681 #if BYTE_ORDER == BIG_ENDIAN
682 hdr->count = bswap16(hdr->count);
683 #endif
684 }
685
686 int
687 ne2000_test_mem(sc)
688 struct dp8390_softc *sc;
689 {
690
691 /* Noop. */
692 return (0);
693 }
694
695 /*
696 * Given a NIC memory source address and a host memory destination address,
697 * copy 'amount' from NIC to host using programmed i/o. The 'amount' is
698 * rounded up to a word - ok as long as mbufs are word sized.
699 */
700 void
701 ne2000_readmem(nict, nich, asict, asich, src, dst, amount, useword)
702 bus_space_tag_t nict;
703 bus_space_handle_t nich;
704 bus_space_tag_t asict;
705 bus_space_handle_t asich;
706 int src;
707 u_int8_t *dst;
708 size_t amount;
709 int useword;
710 {
711
712 /* Select page 0 registers. */
713 NIC_BARRIER(nict, nich);
714 bus_space_write_1(nict, nich, ED_P0_CR,
715 ED_CR_RD2 | ED_CR_PAGE_0 | ED_CR_STA);
716 NIC_BARRIER(nict, nich);
717
718 /* Round up to a word. */
719 if (amount & 1)
720 ++amount;
721
722 /* Set up DMA byte count. */
723 bus_space_write_1(nict, nich, ED_P0_RBCR0, amount);
724 bus_space_write_1(nict, nich, ED_P0_RBCR1, amount >> 8);
725
726 /* Set up source address in NIC mem. */
727 bus_space_write_1(nict, nich, ED_P0_RSAR0, src);
728 bus_space_write_1(nict, nich, ED_P0_RSAR1, src >> 8);
729
730 NIC_BARRIER(nict, nich);
731 bus_space_write_1(nict, nich, ED_P0_CR,
732 ED_CR_RD0 | ED_CR_PAGE_0 | ED_CR_STA);
733
734 bus_space_barrier(nict, nich, 0, NE2000_NPORTS,
735 BUS_SPACE_BARRIER_READ | BUS_SPACE_BARRIER_WRITE);
736 if (useword)
737 bus_space_read_multi_stream_2(asict, asich, NE2000_ASIC_DATA,
738 (u_int16_t *)dst, amount >> 1);
739 else
740 bus_space_read_multi_1(asict, asich, NE2000_ASIC_DATA,
741 dst, amount);
742 }
743
744 /*
745 * Stripped down routine for writing a linear buffer to NIC memory. Only
746 * used in the probe routine to test the memory. 'len' must be even.
747 */
748 void
749 ne2000_writemem(nict, nich, asict, asich, src, dst, len, useword, quiet)
750 bus_space_tag_t nict;
751 bus_space_handle_t nich;
752 bus_space_tag_t asict;
753 bus_space_handle_t asich;
754 u_int8_t *src;
755 int dst;
756 size_t len;
757 int useword;
758 int quiet;
759 {
760 int maxwait = 100; /* about 120us */
761
762 /* Select page 0 registers. */
763 NIC_BARRIER(nict, nich);
764 bus_space_write_1(nict, nich, ED_P0_CR,
765 ED_CR_RD2 | ED_CR_PAGE_0 | ED_CR_STA);
766 NIC_BARRIER(nict, nich);
767
768 /* Reset remote DMA complete flag. */
769 bus_space_write_1(nict, nich, ED_P0_ISR, ED_ISR_RDC);
770 NIC_BARRIER(nict, nich);
771
772 /* Set up DMA byte count. */
773 bus_space_write_1(nict, nich, ED_P0_RBCR0, len);
774 bus_space_write_1(nict, nich, ED_P0_RBCR1, len >> 8);
775
776 /* Set up destination address in NIC mem. */
777 bus_space_write_1(nict, nich, ED_P0_RSAR0, dst);
778 bus_space_write_1(nict, nich, ED_P0_RSAR1, dst >> 8);
779
780 /* Set remote DMA write. */
781 NIC_BARRIER(nict, nich);
782 bus_space_write_1(nict, nich, ED_P0_CR,
783 ED_CR_RD1 | ED_CR_PAGE_0 | ED_CR_STA);
784
785 bus_space_barrier(nict, nich, 0, NE2000_NPORTS,
786 BUS_SPACE_BARRIER_READ | BUS_SPACE_BARRIER_WRITE);
787 if (useword)
788 bus_space_write_multi_stream_2(asict, asich, NE2000_ASIC_DATA,
789 (u_int16_t *)src, len >> 1);
790 else
791 bus_space_write_multi_1(asict, asich, NE2000_ASIC_DATA,
792 src, len);
793
794 /*
795 * Wait for remote DMA to complete. This is necessary because on the
796 * transmit side, data is handled internally by the NIC in bursts, and
797 * we can't start another remote DMA until this one completes. Not
798 * waiting causes really bad things to happen - like the NIC wedging
799 * the bus.
800 */
801 bus_space_barrier(nict, nich, 0, NE2000_NPORTS,
802 BUS_SPACE_BARRIER_READ | BUS_SPACE_BARRIER_WRITE);
803 while (((bus_space_read_1(nict, nich, ED_P0_ISR) & ED_ISR_RDC) !=
804 ED_ISR_RDC) && --maxwait)
805 DELAY(1);
806
807 if (!quiet && maxwait == 0)
808 printf("ne2000_writemem: failed to complete\n");
809 }
810
811 int
812 ne2000_detach(sc, flags)
813 struct ne2000_softc *sc;
814 int flags;
815 {
816
817 return (dp8390_detach(&sc->sc_dp8390, flags));
818 }
819
820 #ifdef IPKDB_NE
821 /*
822 * This code is essentially the same as ne2000_attach above.
823 */
824 int
825 ne2000_ipkdb_attach(kip)
826 struct ipkdb_if *kip;
827 {
828 struct ne2000_softc *np = kip->port;
829 struct dp8390_softc *dp = &np->sc_dp8390;
830 bus_space_tag_t nict = dp->sc_regt;
831 bus_space_handle_t nich = dp->sc_regh;
832 int i, useword;
833
834 #ifdef GWETHER
835 /* Not supported (yet?) */
836 return -1;
837 #endif
838
839 if (np->sc_type == 0)
840 np->sc_type = ne2000_detect(nict, nich,
841 np->sc_asict, np->sc_asich);
842 if (np->sc_type == 0)
843 return -1;
844
845 useword = NE2000_USE_WORD(np);
846
847 dp->cr_proto = ED_CR_RD2;
848 dp->dcr_reg = ED_DCR_FT1 | ED_DCR_LS | (useword ? ED_DCR_WTS : 0);
849 dp->rcr_proto = 0;
850
851 dp->test_mem = ne2000_test_mem;
852 dp->ring_copy = ne2000_ring_copy;
853 dp->write_mbuf = ne2000_write_mbuf;
854 dp->read_hdr = ne2000_read_hdr;
855
856 for (i = 0; i < 16; i++)
857 dp->sc_reg_map[i] = i;
858
859 switch (np->sc_type) {
860 case NE2000_TYPE_NE1000:
861 dp->mem_start = dp->mem_size = 8192;
862 kip->name = "ne1000";
863 break;
864 case NE2000_TYPE_NE2000:
865 dp->mem_start = dp->mem_size = 8192 * 2;
866 kip->name = "ne2000";
867 break;
868 case NE2000_TYPE_DL10019:
869 case NE2000_TYPE_DL10022:
870 dp->mem_start = dp->mem_size = 8192 * 3;
871 kip->name = (np->sc_type == NE2000_TYPE_DL10019) ?
872 "dl10022" : "dl10019";
873 break;
874 case NE2000_TYPE_AX88190:
875 dp->rcr_proto = ED_RCR_INTT;
876 dp->sc_flags |= DP8390_DO_AX88190_WORKAROUND;
877 dp->mem_start = dp->mem_size = 8192 * 2;
878 kip->name = "ax88190";
879 break;
880 }
881
882 if (dp8390_ipkdb_attach(kip))
883 return -1;
884
885 dp->mem_ring = dp->mem_start
886 + ((dp->txb_cnt * ED_TXBUF_SIZE) << ED_PAGE_SHIFT);
887
888 if (!(kip->flags & IPKDB_MYHW)) {
889 char romdata[16];
890
891 /* Read the station address. */
892 if (np->sc_type == NE2000_TYPE_AX88190) {
893 /* Select page 0 registers. */
894 NIC_BARRIER(nict, nich);
895 bus_space_write_1(nict, nich, ED_P0_CR,
896 ED_CR_RD2 | ED_CR_PAGE_0 | ED_CR_STA);
897 NIC_BARRIER(nict, nich);
898 /* Select word transfer */
899 bus_space_write_1(nict, nich, ED_P0_DCR, ED_DCR_WTS);
900 ne2000_readmem(nict, nich, np->sc_asict, np->sc_asich,
901 AX88190_NODEID_OFFSET, kip->myenetaddr,
902 ETHER_ADDR_LEN, useword);
903 } else {
904 ne2000_readmem(nict, nich, np->sc_asict, np->sc_asich,
905 0, romdata, sizeof romdata, useword);
906 useword = useword ? 2 : 1;
907 for (i = 0; i < ETHER_ADDR_LEN; i++)
908 kip->myenetaddr[i] = romdata[i * useword];
909 }
910 kip->flags |= IPKDB_MYHW;
911
912 }
913 dp8390_stop(dp);
914
915 return 0;
916 }
917 #endif
918