ne2000.c revision 1.38 1 /* $NetBSD: ne2000.c,v 1.38 2003/01/15 22:20:06 bouyer 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.38 2003/01/15 22:20:06 bouyer 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, padlen;
489 int maxwait = 100; /* about 120us */
490
491 savelen = m->m_pkthdr.len;
492 if (savelen < ETHER_MIN_LEN - ETHER_CRC_LEN) {
493 padlen = ETHER_MIN_LEN - ETHER_CRC_LEN - savelen;
494 savelen = ETHER_MIN_LEN - ETHER_CRC_LEN;
495 } else
496 padlen = 0;
497
498
499 /* Select page 0 registers. */
500 NIC_BARRIER(nict, nich);
501 bus_space_write_1(nict, nich, ED_P0_CR,
502 ED_CR_RD2 | ED_CR_PAGE_0 | ED_CR_STA);
503 NIC_BARRIER(nict, nich);
504
505 /* Reset remote DMA complete flag. */
506 bus_space_write_1(nict, nich, ED_P0_ISR, ED_ISR_RDC);
507 NIC_BARRIER(nict, nich);
508
509 /* Set up DMA byte count. */
510 bus_space_write_1(nict, nich, ED_P0_RBCR0, savelen);
511 bus_space_write_1(nict, nich, ED_P0_RBCR1, savelen >> 8);
512
513 /* Set up destination address in NIC mem. */
514 bus_space_write_1(nict, nich, ED_P0_RSAR0, buf);
515 bus_space_write_1(nict, nich, ED_P0_RSAR1, buf >> 8);
516
517 /* Set remote DMA write. */
518 NIC_BARRIER(nict, nich);
519 bus_space_write_1(nict, nich,
520 ED_P0_CR, ED_CR_RD1 | ED_CR_PAGE_0 | ED_CR_STA);
521 NIC_BARRIER(nict, nich);
522
523 /*
524 * Transfer the mbuf chain to the NIC memory. NE2000 cards
525 * require that data be transferred as words, and only words,
526 * so that case requires some extra code to patch over odd-length
527 * mbufs.
528 */
529 if (nsc->sc_type == NE2000_TYPE_NE1000) {
530 /* NE1000s are easy. */
531 for (; m != 0; m = m->m_next) {
532 if (m->m_len) {
533 bus_space_write_multi_1(asict, asich,
534 NE2000_ASIC_DATA, mtod(m, u_int8_t *),
535 m->m_len);
536 }
537 }
538 if (padlen) {
539 for(; padlen > 0; padlen--)
540 bus_space_write_1(asict, asich,
541 NE2000_ASIC_DATA, 0);
542 }
543 } else {
544 /* NE2000s are a bit trickier. */
545 u_int8_t *data, savebyte[2];
546 int l, leftover;
547 #ifdef DIAGNOSTIC
548 u_int8_t *lim;
549 #endif
550 /* Start out with no leftover data. */
551 leftover = 0;
552 savebyte[0] = savebyte[1] = 0;
553
554 for (; m != 0; m = m->m_next) {
555 l = m->m_len;
556 if (l == 0)
557 continue;
558 data = mtod(m, u_int8_t *);
559 #ifdef DIAGNOSTIC
560 lim = data + l;
561 #endif
562 while (l > 0) {
563 if (leftover) {
564 /*
565 * Data left over (from mbuf or
566 * realignment). Buffer the next
567 * byte, and write it and the
568 * leftover data out.
569 */
570 savebyte[1] = *data++;
571 l--;
572 bus_space_write_stream_2(asict, asich,
573 NE2000_ASIC_DATA,
574 *(u_int16_t *)savebyte);
575 leftover = 0;
576 } else if (BUS_SPACE_ALIGNED_POINTER(data,
577 u_int16_t) == 0) {
578 /*
579 * Unaligned data; buffer the next
580 * byte.
581 */
582 savebyte[0] = *data++;
583 l--;
584 leftover = 1;
585 } else {
586 /*
587 * Aligned data; output contiguous
588 * words as much as we can, then
589 * buffer the remaining byte, if any.
590 */
591 leftover = l & 1;
592 l &= ~1;
593 bus_space_write_multi_stream_2(asict,
594 asich, NE2000_ASIC_DATA,
595 (u_int16_t *)data, l >> 1);
596 data += l;
597 if (leftover)
598 savebyte[0] = *data++;
599 l = 0;
600 }
601 }
602 if (l < 0)
603 panic("ne2000_write_mbuf: negative len");
604 #ifdef DIAGNOSTIC
605 if (data != lim)
606 panic("ne2000_write_mbuf: data != lim");
607 #endif
608 }
609 if (leftover) {
610 savebyte[1] = 0;
611 bus_space_write_stream_2(asict, asich, NE2000_ASIC_DATA,
612 *(u_int16_t *)savebyte);
613 }
614 if (padlen) {
615 for(; padlen > 0; padlen -= 2)
616 bus_space_write_stream_2(asict, asich,
617 NE2000_ASIC_DATA, 0);
618 }
619 }
620 NIC_BARRIER(nict, nich);
621
622 /*
623 * Wait for remote DMA to complete. This is necessary because on the
624 * transmit side, data is handled internally by the NIC in bursts, and
625 * we can't start another remote DMA until this one completes. Not
626 * waiting causes really bad things to happen - like the NIC wedging
627 * the bus.
628 */
629 while (((bus_space_read_1(nict, nich, ED_P0_ISR) & ED_ISR_RDC) !=
630 ED_ISR_RDC) && --maxwait) {
631 bus_space_read_1(nict, nich, ED_P0_CRDA1);
632 bus_space_read_1(nict, nich, ED_P0_CRDA0);
633 NIC_BARRIER(nict, nich);
634 DELAY(1);
635 }
636
637 if (maxwait == 0) {
638 log(LOG_WARNING,
639 "%s: remote transmit DMA failed to complete\n",
640 sc->sc_dev.dv_xname);
641 dp8390_reset(sc);
642 }
643
644 return (savelen);
645 }
646
647 /*
648 * Given a source and destination address, copy 'amout' of a packet from
649 * the ring buffer into a linear destination buffer. Takes into account
650 * ring-wrap.
651 */
652 int
653 ne2000_ring_copy(sc, src, dst, amount)
654 struct dp8390_softc *sc;
655 int src;
656 caddr_t dst;
657 u_short amount;
658 {
659 struct ne2000_softc *nsc = (struct ne2000_softc *)sc;
660 bus_space_tag_t nict = sc->sc_regt;
661 bus_space_handle_t nich = sc->sc_regh;
662 bus_space_tag_t asict = nsc->sc_asict;
663 bus_space_handle_t asich = nsc->sc_asich;
664 u_short tmp_amount;
665 int useword = NE2000_USE_WORD(nsc);
666
667 /* Does copy wrap to lower addr in ring buffer? */
668 if (src + amount > sc->mem_end) {
669 tmp_amount = sc->mem_end - src;
670
671 /* Copy amount up to end of NIC memory. */
672 ne2000_readmem(nict, nich, asict, asich, src,
673 (u_int8_t *)dst, tmp_amount, useword);
674
675 amount -= tmp_amount;
676 src = sc->mem_ring;
677 dst += tmp_amount;
678 }
679
680 ne2000_readmem(nict, nich, asict, asich, src, (u_int8_t *)dst,
681 amount, useword);
682
683 return (src + amount);
684 }
685
686 void
687 ne2000_read_hdr(sc, buf, hdr)
688 struct dp8390_softc *sc;
689 int buf;
690 struct dp8390_ring *hdr;
691 {
692 struct ne2000_softc *nsc = (struct ne2000_softc *)sc;
693
694 ne2000_readmem(sc->sc_regt, sc->sc_regh, nsc->sc_asict, nsc->sc_asich,
695 buf, (u_int8_t *)hdr, sizeof(struct dp8390_ring),
696 NE2000_USE_WORD(nsc));
697 #if BYTE_ORDER == BIG_ENDIAN
698 hdr->count = bswap16(hdr->count);
699 #endif
700 }
701
702 int
703 ne2000_test_mem(sc)
704 struct dp8390_softc *sc;
705 {
706
707 /* Noop. */
708 return (0);
709 }
710
711 /*
712 * Given a NIC memory source address and a host memory destination address,
713 * copy 'amount' from NIC to host using programmed i/o. The 'amount' is
714 * rounded up to a word - ok as long as mbufs are word sized.
715 */
716 void
717 ne2000_readmem(nict, nich, asict, asich, src, dst, amount, useword)
718 bus_space_tag_t nict;
719 bus_space_handle_t nich;
720 bus_space_tag_t asict;
721 bus_space_handle_t asich;
722 int src;
723 u_int8_t *dst;
724 size_t amount;
725 int useword;
726 {
727
728 /* Select page 0 registers. */
729 NIC_BARRIER(nict, nich);
730 bus_space_write_1(nict, nich, ED_P0_CR,
731 ED_CR_RD2 | ED_CR_PAGE_0 | ED_CR_STA);
732 NIC_BARRIER(nict, nich);
733
734 /* Round up to a word. */
735 if (amount & 1)
736 ++amount;
737
738 /* Set up DMA byte count. */
739 bus_space_write_1(nict, nich, ED_P0_RBCR0, amount);
740 bus_space_write_1(nict, nich, ED_P0_RBCR1, amount >> 8);
741
742 /* Set up source address in NIC mem. */
743 bus_space_write_1(nict, nich, ED_P0_RSAR0, src);
744 bus_space_write_1(nict, nich, ED_P0_RSAR1, src >> 8);
745
746 NIC_BARRIER(nict, nich);
747 bus_space_write_1(nict, nich, ED_P0_CR,
748 ED_CR_RD0 | ED_CR_PAGE_0 | ED_CR_STA);
749
750 bus_space_barrier(nict, nich, 0, NE2000_NPORTS,
751 BUS_SPACE_BARRIER_READ | BUS_SPACE_BARRIER_WRITE);
752 if (useword)
753 bus_space_read_multi_stream_2(asict, asich, NE2000_ASIC_DATA,
754 (u_int16_t *)dst, amount >> 1);
755 else
756 bus_space_read_multi_1(asict, asich, NE2000_ASIC_DATA,
757 dst, amount);
758 }
759
760 /*
761 * Stripped down routine for writing a linear buffer to NIC memory. Only
762 * used in the probe routine to test the memory. 'len' must be even.
763 */
764 void
765 ne2000_writemem(nict, nich, asict, asich, src, dst, len, useword, quiet)
766 bus_space_tag_t nict;
767 bus_space_handle_t nich;
768 bus_space_tag_t asict;
769 bus_space_handle_t asich;
770 u_int8_t *src;
771 int dst;
772 size_t len;
773 int useword;
774 int quiet;
775 {
776 int maxwait = 100; /* about 120us */
777
778 /* Select page 0 registers. */
779 NIC_BARRIER(nict, nich);
780 bus_space_write_1(nict, nich, ED_P0_CR,
781 ED_CR_RD2 | ED_CR_PAGE_0 | ED_CR_STA);
782 NIC_BARRIER(nict, nich);
783
784 /* Reset remote DMA complete flag. */
785 bus_space_write_1(nict, nich, ED_P0_ISR, ED_ISR_RDC);
786 NIC_BARRIER(nict, nich);
787
788 /* Set up DMA byte count. */
789 bus_space_write_1(nict, nich, ED_P0_RBCR0, len);
790 bus_space_write_1(nict, nich, ED_P0_RBCR1, len >> 8);
791
792 /* Set up destination address in NIC mem. */
793 bus_space_write_1(nict, nich, ED_P0_RSAR0, dst);
794 bus_space_write_1(nict, nich, ED_P0_RSAR1, dst >> 8);
795
796 /* Set remote DMA write. */
797 NIC_BARRIER(nict, nich);
798 bus_space_write_1(nict, nich, ED_P0_CR,
799 ED_CR_RD1 | ED_CR_PAGE_0 | ED_CR_STA);
800
801 bus_space_barrier(nict, nich, 0, NE2000_NPORTS,
802 BUS_SPACE_BARRIER_READ | BUS_SPACE_BARRIER_WRITE);
803 if (useword)
804 bus_space_write_multi_stream_2(asict, asich, NE2000_ASIC_DATA,
805 (u_int16_t *)src, len >> 1);
806 else
807 bus_space_write_multi_1(asict, asich, NE2000_ASIC_DATA,
808 src, len);
809
810 /*
811 * Wait for remote DMA to complete. This is necessary because on the
812 * transmit side, data is handled internally by the NIC in bursts, and
813 * we can't start another remote DMA until this one completes. Not
814 * waiting causes really bad things to happen - like the NIC wedging
815 * the bus.
816 */
817 bus_space_barrier(nict, nich, 0, NE2000_NPORTS,
818 BUS_SPACE_BARRIER_READ | BUS_SPACE_BARRIER_WRITE);
819 while (((bus_space_read_1(nict, nich, ED_P0_ISR) & ED_ISR_RDC) !=
820 ED_ISR_RDC) && --maxwait)
821 DELAY(1);
822
823 if (!quiet && maxwait == 0)
824 printf("ne2000_writemem: failed to complete\n");
825 }
826
827 int
828 ne2000_detach(sc, flags)
829 struct ne2000_softc *sc;
830 int flags;
831 {
832
833 return (dp8390_detach(&sc->sc_dp8390, flags));
834 }
835
836 #ifdef IPKDB_NE
837 /*
838 * This code is essentially the same as ne2000_attach above.
839 */
840 int
841 ne2000_ipkdb_attach(kip)
842 struct ipkdb_if *kip;
843 {
844 struct ne2000_softc *np = kip->port;
845 struct dp8390_softc *dp = &np->sc_dp8390;
846 bus_space_tag_t nict = dp->sc_regt;
847 bus_space_handle_t nich = dp->sc_regh;
848 int i, useword;
849
850 #ifdef GWETHER
851 /* Not supported (yet?) */
852 return -1;
853 #endif
854
855 if (np->sc_type == 0)
856 np->sc_type = ne2000_detect(nict, nich,
857 np->sc_asict, np->sc_asich);
858 if (np->sc_type == 0)
859 return -1;
860
861 useword = NE2000_USE_WORD(np);
862
863 dp->cr_proto = ED_CR_RD2;
864 dp->dcr_reg = ED_DCR_FT1 | ED_DCR_LS | (useword ? ED_DCR_WTS : 0);
865 dp->rcr_proto = 0;
866
867 dp->test_mem = ne2000_test_mem;
868 dp->ring_copy = ne2000_ring_copy;
869 dp->write_mbuf = ne2000_write_mbuf;
870 dp->read_hdr = ne2000_read_hdr;
871
872 for (i = 0; i < 16; i++)
873 dp->sc_reg_map[i] = i;
874
875 switch (np->sc_type) {
876 case NE2000_TYPE_NE1000:
877 dp->mem_start = dp->mem_size = 8192;
878 kip->name = "ne1000";
879 break;
880 case NE2000_TYPE_NE2000:
881 dp->mem_start = dp->mem_size = 8192 * 2;
882 kip->name = "ne2000";
883 break;
884 case NE2000_TYPE_DL10019:
885 case NE2000_TYPE_DL10022:
886 dp->mem_start = dp->mem_size = 8192 * 3;
887 kip->name = (np->sc_type == NE2000_TYPE_DL10019) ?
888 "dl10022" : "dl10019";
889 break;
890 case NE2000_TYPE_AX88190:
891 dp->rcr_proto = ED_RCR_INTT;
892 dp->sc_flags |= DP8390_DO_AX88190_WORKAROUND;
893 dp->mem_start = dp->mem_size = 8192 * 2;
894 kip->name = "ax88190";
895 break;
896 }
897
898 if (dp8390_ipkdb_attach(kip))
899 return -1;
900
901 dp->mem_ring = dp->mem_start
902 + ((dp->txb_cnt * ED_TXBUF_SIZE) << ED_PAGE_SHIFT);
903
904 if (!(kip->flags & IPKDB_MYHW)) {
905 char romdata[16];
906
907 /* Read the station address. */
908 if (np->sc_type == NE2000_TYPE_AX88190) {
909 /* Select page 0 registers. */
910 NIC_BARRIER(nict, nich);
911 bus_space_write_1(nict, nich, ED_P0_CR,
912 ED_CR_RD2 | ED_CR_PAGE_0 | ED_CR_STA);
913 NIC_BARRIER(nict, nich);
914 /* Select word transfer */
915 bus_space_write_1(nict, nich, ED_P0_DCR, ED_DCR_WTS);
916 ne2000_readmem(nict, nich, np->sc_asict, np->sc_asich,
917 AX88190_NODEID_OFFSET, kip->myenetaddr,
918 ETHER_ADDR_LEN, useword);
919 } else {
920 ne2000_readmem(nict, nich, np->sc_asict, np->sc_asich,
921 0, romdata, sizeof romdata, useword);
922 useword = useword ? 2 : 1;
923 for (i = 0; i < ETHER_ADDR_LEN; i++)
924 kip->myenetaddr[i] = romdata[i * useword];
925 }
926 kip->flags |= IPKDB_MYHW;
927
928 }
929 dp8390_stop(dp);
930
931 return 0;
932 }
933 #endif
934