ne2000.c revision 1.39 1 /* $NetBSD: ne2000.c,v 1.39 2003/10/22 00:12:36 christos 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.39 2003/10/22 00:12:36 christos 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 nsc->sc_type == NE2000_TYPE_AX88790) {
143 dsc->rcr_proto = ED_RCR_INTT;
144 dsc->sc_flags |= DP8390_DO_AX88190_WORKAROUND;
145 } else
146 dsc->rcr_proto = 0;
147
148 /*
149 * DCR gets:
150 *
151 * FIFO threshold to 8, No auto-init Remote DMA,
152 * byte order=80x86.
153 *
154 * NE1000 gets byte-wide DMA, NE2000 gets word-wide DMA.
155 */
156 dsc->dcr_reg = ED_DCR_FT1 | ED_DCR_LS | (useword ? ED_DCR_WTS : 0);
157
158 dsc->test_mem = ne2000_test_mem;
159 dsc->ring_copy = ne2000_ring_copy;
160 dsc->write_mbuf = ne2000_write_mbuf;
161 dsc->read_hdr = ne2000_read_hdr;
162
163 /* Registers are linear. */
164 for (i = 0; i < 16; i++)
165 dsc->sc_reg_map[i] = i;
166
167 /*
168 * 8k of memory for NE1000, 16k for NE2000 and 24k for the
169 * card uses DL10019.
170 */
171 switch (nsc->sc_type) {
172 case NE2000_TYPE_NE1000:
173 memsize = 8192;
174 break;
175 case NE2000_TYPE_NE2000:
176 case NE2000_TYPE_AX88190: /* XXX really? */
177 case NE2000_TYPE_AX88790:
178 memsize = 8192 * 2;
179 break;
180 case NE2000_TYPE_DL10019:
181 case NE2000_TYPE_DL10022:
182 memsize = 8192 * 3;
183 break;
184 }
185
186 /*
187 * NIC memory doens't start at zero on an NE board.
188 * The start address is tied to the bus width.
189 * (It happens to be computed the same way as mem size.)
190 */
191 dsc->mem_start = memsize;
192
193 #ifdef GWETHER
194 {
195 int x, mstart = 0;
196 int8_t pbuf0[ED_PAGE_SIZE], pbuf[ED_PAGE_SIZE],
197 tbuf[ED_PAGE_SIZE];
198
199 for (i = 0; i < ED_PAGE_SIZE; i++)
200 pbuf0[i] = 0;
201
202 /* Search for the start of RAM. */
203 for (x = 1; x < 256; x++) {
204 ne2000_writemem(nict, nich, asict, asich, pbuf0,
205 x << ED_PAGE_SHIFT, ED_PAGE_SIZE, useword, 0);
206 ne2000_readmem(nict, nich, asict, asich,
207 x << ED_PAGE_SHIFT, tbuf, ED_PAGE_SIZE, useword);
208 if (memcmp(pbuf0, tbuf, ED_PAGE_SIZE) == 0) {
209 for (i = 0; i < ED_PAGE_SIZE; i++)
210 pbuf[i] = 255 - x;
211 ne2000_writemem(nict, nich, asict, asich,
212 pbuf, x << ED_PAGE_SHIFT, ED_PAGE_SIZE,
213 useword, 0);
214 ne2000_readmem(nict, nich, asict, asich,
215 x << ED_PAGE_SHIFT, tbuf, ED_PAGE_SIZE,
216 useword);
217 if (memcmp(pbuf, tbuf, ED_PAGE_SIZE) == 0) {
218 mstart = x << ED_PAGE_SHIFT;
219 memsize = ED_PAGE_SIZE;
220 break;
221 }
222 }
223 }
224
225 if (mstart == 0) {
226 printf("%s: cannot find start of RAM\n",
227 dsc->sc_dev.dv_xname);
228 return (1);
229 }
230
231 /* Search for the end of RAM. */
232 for (++x; x < 256; x++) {
233 ne2000_writemem(nict, nich, asict, asich, pbuf0,
234 x << ED_PAGE_SHIFT, ED_PAGE_SIZE, useword, 0);
235 ne2000_readmem(nict, nich, asict, asich,
236 x << ED_PAGE_SHIFT, tbuf, ED_PAGE_SIZE, useword);
237 if (memcmp(pbuf0, tbuf, ED_PAGE_SIZE) == 0) {
238 for (i = 0; i < ED_PAGE_SIZE; i++)
239 pbuf[i] = 255 - x;
240 ne2000_writemem(nict, nich, asict, asich,
241 pbuf, x << ED_PAGE_SHIFT, ED_PAGE_SIZE,
242 useword, 0);
243 ne2000_readmem(nict, nich, asict, asich,
244 x << ED_PAGE_SHIFT, tbuf, ED_PAGE_SIZE,
245 useword);
246 if (memcmp(pbuf, tbuf, ED_PAGE_SIZE) == 0)
247 memsize += ED_PAGE_SIZE;
248 else
249 break;
250 } else
251 break;
252 }
253
254 printf("%s: RAM start 0x%x, size %d\n",
255 dsc->sc_dev.dv_xname, mstart, memsize);
256
257 dsc->mem_start = mstart;
258 }
259 #endif /* GWETHER */
260
261 dsc->mem_size = memsize;
262
263 if (myea == NULL) {
264 /* Read the station address. */
265 if (nsc->sc_type == NE2000_TYPE_AX88190 ||
266 nsc->sc_type == NE2000_TYPE_AX88790) {
267 /* Select page 0 registers. */
268 NIC_BARRIER(nict, nich);
269 bus_space_write_1(nict, nich, ED_P0_CR,
270 ED_CR_RD2 | ED_CR_PAGE_0 | ED_CR_STA);
271 NIC_BARRIER(nict, nich);
272 /* Select word transfer. */
273 bus_space_write_1(nict, nich, ED_P0_DCR, ED_DCR_WTS);
274 NIC_BARRIER(nict, nich);
275 ne2000_readmem(nict, nich, asict, asich,
276 AX88190_NODEID_OFFSET, dsc->sc_enaddr,
277 ETHER_ADDR_LEN, useword);
278 } else {
279 ne2000_readmem(nict, nich, asict, asich, 0, romdata,
280 sizeof(romdata), useword);
281 for (i = 0; i < ETHER_ADDR_LEN; i++)
282 dsc->sc_enaddr[i] =
283 romdata[i * (useword ? 2 : 1)];
284 }
285 } else
286 memcpy(dsc->sc_enaddr, myea, sizeof(dsc->sc_enaddr));
287
288 /* Clear any pending interrupts that might have occurred above. */
289 NIC_BARRIER(nict, nich);
290 bus_space_write_1(nict, nich, ED_P0_ISR, 0xff);
291 NIC_BARRIER(nict, nich);
292
293 if (dsc->sc_media_init == NULL)
294 dsc->sc_media_init = dp8390_media_init;
295
296 if (dp8390_config(dsc)) {
297 printf("%s: setup failed\n", dsc->sc_dev.dv_xname);
298 return (1);
299 }
300
301 /*
302 * We need to compute mem_ring a bit differently; override the
303 * value set up in dp8390_config().
304 */
305 dsc->mem_ring =
306 dsc->mem_start + ((dsc->txb_cnt * ED_TXBUF_SIZE) << ED_PAGE_SHIFT);
307
308 return (0);
309 }
310
311 /*
312 * Detect an NE-2000 or compatible. Returns a model code.
313 */
314 int
315 ne2000_detect(nict, nich, asict, asich)
316 bus_space_tag_t nict;
317 bus_space_handle_t nich;
318 bus_space_tag_t asict;
319 bus_space_handle_t asich;
320 {
321 static u_int8_t test_pattern[32] = "THIS is A memory TEST pattern";
322 u_int8_t test_buffer[32], tmp;
323 int i, rv = 0;
324
325 /* Reset the board. */
326 #ifdef GWETHER
327 bus_space_write_1(asict, asich, NE2000_ASIC_RESET, 0);
328 bus_space_barrier(nict, nich, 0, NE2000_NPORTS,
329 BUS_SPACE_BARRIER_READ | BUS_SPACE_BARRIER_WRITE);
330 delay(200);
331 #endif /* GWETHER */
332 tmp = bus_space_read_1(asict, asich, NE2000_ASIC_RESET);
333 bus_space_barrier(nict, nich, 0, NE2000_NPORTS,
334 BUS_SPACE_BARRIER_READ | BUS_SPACE_BARRIER_WRITE);
335 delay(10000);
336
337 /*
338 * I don't know if this is necessary; probably cruft leftover from
339 * Clarkson packet driver code. Doesn't do a thing on the boards I've
340 * tested. -DG [note that a outb(0x84, 0) seems to work here, and is
341 * non-invasive...but some boards don't seem to reset and I don't have
342 * complete documentation on what the 'right' thing to do is...so we do
343 * the invasive thing for now. Yuck.]
344 */
345 bus_space_write_1(asict, asich, NE2000_ASIC_RESET, tmp);
346 bus_space_barrier(nict, nich, 0, NE2000_NPORTS,
347 BUS_SPACE_BARRIER_READ | BUS_SPACE_BARRIER_WRITE);
348 delay(5000);
349
350 /*
351 * This is needed because some NE clones apparently don't reset the
352 * NIC properly (or the NIC chip doesn't reset fully on power-up).
353 * XXX - this makes the probe invasive! Done against my better
354 * judgement. -DLG
355 */
356 bus_space_write_1(nict, nich, ED_P0_CR,
357 ED_CR_RD2 | ED_CR_PAGE_0 | ED_CR_STP);
358 NIC_BARRIER(nict, nich);
359
360 delay(5000);
361
362 /*
363 * Generic probe routine for testing for the existance of a DS8390.
364 * Must be performed after the NIC has just been reset. This
365 * works by looking at certain register values that are guaranteed
366 * to be initialized a certain way after power-up or reset.
367 *
368 * Specifically:
369 *
370 * Register reset bits set bits
371 * -------- ---------- --------
372 * CR TXP, STA RD2, STP
373 * ISR RST
374 * IMR <all>
375 * DCR LAS
376 * TCR LB1, LB0
377 *
378 * We only look at CR and ISR, however, since looking at the others
379 * would require changing register pages, which would be intrusive
380 * if this isn't an 8390.
381 */
382
383 tmp = bus_space_read_1(nict, nich, ED_P0_CR);
384 if ((tmp & (ED_CR_RD2 | ED_CR_TXP | ED_CR_STA | ED_CR_STP)) !=
385 (ED_CR_RD2 | ED_CR_STP))
386 goto out;
387
388 tmp = bus_space_read_1(nict, nich, ED_P0_ISR);
389 if ((tmp & ED_ISR_RST) != ED_ISR_RST)
390 goto out;
391
392 bus_space_write_1(nict, nich,
393 ED_P0_CR, ED_CR_RD2 | ED_CR_PAGE_0 | ED_CR_STA);
394 NIC_BARRIER(nict, nich);
395
396 for (i = 0; i < 100; i++) {
397 if ((bus_space_read_1(nict, nich, ED_P0_ISR) & ED_ISR_RST) ==
398 ED_ISR_RST) {
399 /* Ack the reset bit. */
400 bus_space_write_1(nict, nich, ED_P0_ISR, ED_ISR_RST);
401 NIC_BARRIER(nict, nich);
402 break;
403 }
404 delay(100);
405 }
406
407 #if 0
408 /* XXX */
409 if (i == 100)
410 goto out;
411 #endif
412
413 /*
414 * Test the ability to read and write to the NIC memory. This has
415 * the side effect of determining if this is an NE1000 or an NE2000.
416 */
417
418 /*
419 * This prevents packets from being stored in the NIC memory when
420 * the readmem routine turns on the start bit in the CR.
421 */
422 bus_space_write_1(nict, nich, ED_P0_RCR, ED_RCR_MON);
423 NIC_BARRIER(nict, nich);
424
425 /* Temporarily initialize DCR for byte operations. */
426 bus_space_write_1(nict, nich, ED_P0_DCR, ED_DCR_FT1 | ED_DCR_LS);
427
428 bus_space_write_1(nict, nich, ED_P0_PSTART, 8192 >> ED_PAGE_SHIFT);
429 bus_space_write_1(nict, nich, ED_P0_PSTOP, 16384 >> ED_PAGE_SHIFT);
430
431 /*
432 * Write a test pattern in byte mode. If this fails, then there
433 * probably isn't any memory at 8k - which likely means that the
434 * board is an NE2000.
435 */
436 ne2000_writemem(nict, nich, asict, asich, test_pattern, 8192,
437 sizeof(test_pattern), 0, 1);
438 ne2000_readmem(nict, nich, asict, asich, 8192, test_buffer,
439 sizeof(test_buffer), 0);
440
441 if (memcmp(test_pattern, test_buffer, sizeof(test_pattern))) {
442 /* not an NE1000 - try NE2000 */
443 bus_space_write_1(nict, nich, ED_P0_DCR,
444 ED_DCR_WTS | ED_DCR_FT1 | ED_DCR_LS);
445 bus_space_write_1(nict, nich, ED_P0_PSTART,
446 16384 >> ED_PAGE_SHIFT);
447 bus_space_write_1(nict, nich, ED_P0_PSTOP,
448 32768 >> ED_PAGE_SHIFT);
449
450 /*
451 * Write the test pattern in word mode. If this also fails,
452 * then we don't know what this board is.
453 */
454 ne2000_writemem(nict, nich, asict, asich, test_pattern, 16384,
455 sizeof(test_pattern), 1, 0);
456 ne2000_readmem(nict, nich, asict, asich, 16384, test_buffer,
457 sizeof(test_buffer), 1);
458
459 if (memcmp(test_pattern, test_buffer, sizeof(test_pattern)))
460 goto out; /* not an NE2000 either */
461
462 rv = NE2000_TYPE_NE2000;
463 } else {
464 /* We're an NE1000. */
465 rv = NE2000_TYPE_NE1000;
466 }
467
468 /* Clear any pending interrupts that might have occurred above. */
469 NIC_BARRIER(nict, nich);
470 bus_space_write_1(nict, nich, ED_P0_ISR, 0xff);
471
472 out:
473 return (rv);
474 }
475
476 /*
477 * Write an mbuf chain to the destination NIC memory address using programmed
478 * I/O.
479 */
480 int
481 ne2000_write_mbuf(sc, m, buf)
482 struct dp8390_softc *sc;
483 struct mbuf *m;
484 int buf;
485 {
486 struct ne2000_softc *nsc = (struct ne2000_softc *)sc;
487 bus_space_tag_t nict = sc->sc_regt;
488 bus_space_handle_t nich = sc->sc_regh;
489 bus_space_tag_t asict = nsc->sc_asict;
490 bus_space_handle_t asich = nsc->sc_asich;
491 int savelen, padlen;
492 int maxwait = 100; /* about 120us */
493
494 savelen = m->m_pkthdr.len;
495 if (savelen < ETHER_MIN_LEN - ETHER_CRC_LEN) {
496 padlen = ETHER_MIN_LEN - ETHER_CRC_LEN - savelen;
497 savelen = ETHER_MIN_LEN - ETHER_CRC_LEN;
498 } else
499 padlen = 0;
500
501
502 /* Select page 0 registers. */
503 NIC_BARRIER(nict, nich);
504 bus_space_write_1(nict, nich, ED_P0_CR,
505 ED_CR_RD2 | ED_CR_PAGE_0 | ED_CR_STA);
506 NIC_BARRIER(nict, nich);
507
508 /* Reset remote DMA complete flag. */
509 bus_space_write_1(nict, nich, ED_P0_ISR, ED_ISR_RDC);
510 NIC_BARRIER(nict, nich);
511
512 /* Set up DMA byte count. */
513 bus_space_write_1(nict, nich, ED_P0_RBCR0, savelen);
514 bus_space_write_1(nict, nich, ED_P0_RBCR1, savelen >> 8);
515
516 /* Set up destination address in NIC mem. */
517 bus_space_write_1(nict, nich, ED_P0_RSAR0, buf);
518 bus_space_write_1(nict, nich, ED_P0_RSAR1, buf >> 8);
519
520 /* Set remote DMA write. */
521 NIC_BARRIER(nict, nich);
522 bus_space_write_1(nict, nich,
523 ED_P0_CR, ED_CR_RD1 | ED_CR_PAGE_0 | ED_CR_STA);
524 NIC_BARRIER(nict, nich);
525
526 /*
527 * Transfer the mbuf chain to the NIC memory. NE2000 cards
528 * require that data be transferred as words, and only words,
529 * so that case requires some extra code to patch over odd-length
530 * mbufs.
531 */
532 if (nsc->sc_type == NE2000_TYPE_NE1000) {
533 /* NE1000s are easy. */
534 for (; m != 0; m = m->m_next) {
535 if (m->m_len) {
536 bus_space_write_multi_1(asict, asich,
537 NE2000_ASIC_DATA, mtod(m, u_int8_t *),
538 m->m_len);
539 }
540 }
541 if (padlen) {
542 for(; padlen > 0; padlen--)
543 bus_space_write_1(asict, asich,
544 NE2000_ASIC_DATA, 0);
545 }
546 } else {
547 /* NE2000s are a bit trickier. */
548 u_int8_t *data, savebyte[2];
549 int l, leftover;
550 #ifdef DIAGNOSTIC
551 u_int8_t *lim;
552 #endif
553 /* Start out with no leftover data. */
554 leftover = 0;
555 savebyte[0] = savebyte[1] = 0;
556
557 for (; m != 0; m = m->m_next) {
558 l = m->m_len;
559 if (l == 0)
560 continue;
561 data = mtod(m, u_int8_t *);
562 #ifdef DIAGNOSTIC
563 lim = data + l;
564 #endif
565 while (l > 0) {
566 if (leftover) {
567 /*
568 * Data left over (from mbuf or
569 * realignment). Buffer the next
570 * byte, and write it and the
571 * leftover data out.
572 */
573 savebyte[1] = *data++;
574 l--;
575 bus_space_write_stream_2(asict, asich,
576 NE2000_ASIC_DATA,
577 *(u_int16_t *)savebyte);
578 leftover = 0;
579 } else if (BUS_SPACE_ALIGNED_POINTER(data,
580 u_int16_t) == 0) {
581 /*
582 * Unaligned data; buffer the next
583 * byte.
584 */
585 savebyte[0] = *data++;
586 l--;
587 leftover = 1;
588 } else {
589 /*
590 * Aligned data; output contiguous
591 * words as much as we can, then
592 * buffer the remaining byte, if any.
593 */
594 leftover = l & 1;
595 l &= ~1;
596 bus_space_write_multi_stream_2(asict,
597 asich, NE2000_ASIC_DATA,
598 (u_int16_t *)data, l >> 1);
599 data += l;
600 if (leftover)
601 savebyte[0] = *data++;
602 l = 0;
603 }
604 }
605 if (l < 0)
606 panic("ne2000_write_mbuf: negative len");
607 #ifdef DIAGNOSTIC
608 if (data != lim)
609 panic("ne2000_write_mbuf: data != lim");
610 #endif
611 }
612 if (leftover) {
613 savebyte[1] = 0;
614 bus_space_write_stream_2(asict, asich, NE2000_ASIC_DATA,
615 *(u_int16_t *)savebyte);
616 }
617 if (padlen) {
618 for(; padlen > 0; padlen -= 2)
619 bus_space_write_stream_2(asict, asich,
620 NE2000_ASIC_DATA, 0);
621 }
622 }
623 NIC_BARRIER(nict, nich);
624
625 /*
626 * Wait for remote DMA to complete. This is necessary because on the
627 * transmit side, data is handled internally by the NIC in bursts, and
628 * we can't start another remote DMA until this one completes. Not
629 * waiting causes really bad things to happen - like the NIC wedging
630 * the bus.
631 */
632 while (((bus_space_read_1(nict, nich, ED_P0_ISR) & ED_ISR_RDC) !=
633 ED_ISR_RDC) && --maxwait) {
634 bus_space_read_1(nict, nich, ED_P0_CRDA1);
635 bus_space_read_1(nict, nich, ED_P0_CRDA0);
636 NIC_BARRIER(nict, nich);
637 DELAY(1);
638 }
639
640 if (maxwait == 0) {
641 log(LOG_WARNING,
642 "%s: remote transmit DMA failed to complete\n",
643 sc->sc_dev.dv_xname);
644 dp8390_reset(sc);
645 }
646
647 return (savelen);
648 }
649
650 /*
651 * Given a source and destination address, copy 'amout' of a packet from
652 * the ring buffer into a linear destination buffer. Takes into account
653 * ring-wrap.
654 */
655 int
656 ne2000_ring_copy(sc, src, dst, amount)
657 struct dp8390_softc *sc;
658 int src;
659 caddr_t dst;
660 u_short amount;
661 {
662 struct ne2000_softc *nsc = (struct ne2000_softc *)sc;
663 bus_space_tag_t nict = sc->sc_regt;
664 bus_space_handle_t nich = sc->sc_regh;
665 bus_space_tag_t asict = nsc->sc_asict;
666 bus_space_handle_t asich = nsc->sc_asich;
667 u_short tmp_amount;
668 int useword = NE2000_USE_WORD(nsc);
669
670 /* Does copy wrap to lower addr in ring buffer? */
671 if (src + amount > sc->mem_end) {
672 tmp_amount = sc->mem_end - src;
673
674 /* Copy amount up to end of NIC memory. */
675 ne2000_readmem(nict, nich, asict, asich, src,
676 (u_int8_t *)dst, tmp_amount, useword);
677
678 amount -= tmp_amount;
679 src = sc->mem_ring;
680 dst += tmp_amount;
681 }
682
683 ne2000_readmem(nict, nich, asict, asich, src, (u_int8_t *)dst,
684 amount, useword);
685
686 return (src + amount);
687 }
688
689 void
690 ne2000_read_hdr(sc, buf, hdr)
691 struct dp8390_softc *sc;
692 int buf;
693 struct dp8390_ring *hdr;
694 {
695 struct ne2000_softc *nsc = (struct ne2000_softc *)sc;
696
697 ne2000_readmem(sc->sc_regt, sc->sc_regh, nsc->sc_asict, nsc->sc_asich,
698 buf, (u_int8_t *)hdr, sizeof(struct dp8390_ring),
699 NE2000_USE_WORD(nsc));
700 #if BYTE_ORDER == BIG_ENDIAN
701 hdr->count = bswap16(hdr->count);
702 #endif
703 }
704
705 int
706 ne2000_test_mem(sc)
707 struct dp8390_softc *sc;
708 {
709
710 /* Noop. */
711 return (0);
712 }
713
714 /*
715 * Given a NIC memory source address and a host memory destination address,
716 * copy 'amount' from NIC to host using programmed i/o. The 'amount' is
717 * rounded up to a word - ok as long as mbufs are word sized.
718 */
719 void
720 ne2000_readmem(nict, nich, asict, asich, src, dst, amount, useword)
721 bus_space_tag_t nict;
722 bus_space_handle_t nich;
723 bus_space_tag_t asict;
724 bus_space_handle_t asich;
725 int src;
726 u_int8_t *dst;
727 size_t amount;
728 int useword;
729 {
730
731 /* Select page 0 registers. */
732 NIC_BARRIER(nict, nich);
733 bus_space_write_1(nict, nich, ED_P0_CR,
734 ED_CR_RD2 | ED_CR_PAGE_0 | ED_CR_STA);
735 NIC_BARRIER(nict, nich);
736
737 /* Round up to a word. */
738 if (amount & 1)
739 ++amount;
740
741 /* Set up DMA byte count. */
742 bus_space_write_1(nict, nich, ED_P0_RBCR0, amount);
743 bus_space_write_1(nict, nich, ED_P0_RBCR1, amount >> 8);
744
745 /* Set up source address in NIC mem. */
746 bus_space_write_1(nict, nich, ED_P0_RSAR0, src);
747 bus_space_write_1(nict, nich, ED_P0_RSAR1, src >> 8);
748
749 NIC_BARRIER(nict, nich);
750 bus_space_write_1(nict, nich, ED_P0_CR,
751 ED_CR_RD0 | ED_CR_PAGE_0 | ED_CR_STA);
752
753 bus_space_barrier(nict, nich, 0, NE2000_NPORTS,
754 BUS_SPACE_BARRIER_READ | BUS_SPACE_BARRIER_WRITE);
755 if (useword)
756 bus_space_read_multi_stream_2(asict, asich, NE2000_ASIC_DATA,
757 (u_int16_t *)dst, amount >> 1);
758 else
759 bus_space_read_multi_1(asict, asich, NE2000_ASIC_DATA,
760 dst, amount);
761 }
762
763 /*
764 * Stripped down routine for writing a linear buffer to NIC memory. Only
765 * used in the probe routine to test the memory. 'len' must be even.
766 */
767 void
768 ne2000_writemem(nict, nich, asict, asich, src, dst, len, useword, quiet)
769 bus_space_tag_t nict;
770 bus_space_handle_t nich;
771 bus_space_tag_t asict;
772 bus_space_handle_t asich;
773 u_int8_t *src;
774 int dst;
775 size_t len;
776 int useword;
777 int quiet;
778 {
779 int maxwait = 100; /* about 120us */
780
781 /* Select page 0 registers. */
782 NIC_BARRIER(nict, nich);
783 bus_space_write_1(nict, nich, ED_P0_CR,
784 ED_CR_RD2 | ED_CR_PAGE_0 | ED_CR_STA);
785 NIC_BARRIER(nict, nich);
786
787 /* Reset remote DMA complete flag. */
788 bus_space_write_1(nict, nich, ED_P0_ISR, ED_ISR_RDC);
789 NIC_BARRIER(nict, nich);
790
791 /* Set up DMA byte count. */
792 bus_space_write_1(nict, nich, ED_P0_RBCR0, len);
793 bus_space_write_1(nict, nich, ED_P0_RBCR1, len >> 8);
794
795 /* Set up destination address in NIC mem. */
796 bus_space_write_1(nict, nich, ED_P0_RSAR0, dst);
797 bus_space_write_1(nict, nich, ED_P0_RSAR1, dst >> 8);
798
799 /* Set remote DMA write. */
800 NIC_BARRIER(nict, nich);
801 bus_space_write_1(nict, nich, ED_P0_CR,
802 ED_CR_RD1 | ED_CR_PAGE_0 | ED_CR_STA);
803
804 bus_space_barrier(nict, nich, 0, NE2000_NPORTS,
805 BUS_SPACE_BARRIER_READ | BUS_SPACE_BARRIER_WRITE);
806 if (useword)
807 bus_space_write_multi_stream_2(asict, asich, NE2000_ASIC_DATA,
808 (u_int16_t *)src, len >> 1);
809 else
810 bus_space_write_multi_1(asict, asich, NE2000_ASIC_DATA,
811 src, len);
812
813 /*
814 * Wait for remote DMA to complete. This is necessary because on the
815 * transmit side, data is handled internally by the NIC in bursts, and
816 * we can't start another remote DMA until this one completes. Not
817 * waiting causes really bad things to happen - like the NIC wedging
818 * the bus.
819 */
820 bus_space_barrier(nict, nich, 0, NE2000_NPORTS,
821 BUS_SPACE_BARRIER_READ | BUS_SPACE_BARRIER_WRITE);
822 while (((bus_space_read_1(nict, nich, ED_P0_ISR) & ED_ISR_RDC) !=
823 ED_ISR_RDC) && --maxwait)
824 DELAY(1);
825
826 if (!quiet && maxwait == 0)
827 printf("ne2000_writemem: failed to complete\n");
828 }
829
830 int
831 ne2000_detach(sc, flags)
832 struct ne2000_softc *sc;
833 int flags;
834 {
835
836 return (dp8390_detach(&sc->sc_dp8390, flags));
837 }
838
839 #ifdef IPKDB_NE
840 /*
841 * This code is essentially the same as ne2000_attach above.
842 */
843 int
844 ne2000_ipkdb_attach(kip)
845 struct ipkdb_if *kip;
846 {
847 struct ne2000_softc *np = kip->port;
848 struct dp8390_softc *dp = &np->sc_dp8390;
849 bus_space_tag_t nict = dp->sc_regt;
850 bus_space_handle_t nich = dp->sc_regh;
851 int i, useword;
852
853 #ifdef GWETHER
854 /* Not supported (yet?) */
855 return -1;
856 #endif
857
858 if (np->sc_type == 0)
859 np->sc_type = ne2000_detect(nict, nich,
860 np->sc_asict, np->sc_asich);
861 if (np->sc_type == 0)
862 return -1;
863
864 useword = NE2000_USE_WORD(np);
865
866 dp->cr_proto = ED_CR_RD2;
867 dp->dcr_reg = ED_DCR_FT1 | ED_DCR_LS | (useword ? ED_DCR_WTS : 0);
868 dp->rcr_proto = 0;
869
870 dp->test_mem = ne2000_test_mem;
871 dp->ring_copy = ne2000_ring_copy;
872 dp->write_mbuf = ne2000_write_mbuf;
873 dp->read_hdr = ne2000_read_hdr;
874
875 for (i = 0; i < 16; i++)
876 dp->sc_reg_map[i] = i;
877
878 switch (np->sc_type) {
879 case NE2000_TYPE_NE1000:
880 dp->mem_start = dp->mem_size = 8192;
881 kip->name = "ne1000";
882 break;
883 case NE2000_TYPE_NE2000:
884 dp->mem_start = dp->mem_size = 8192 * 2;
885 kip->name = "ne2000";
886 break;
887 case NE2000_TYPE_DL10019:
888 case NE2000_TYPE_DL10022:
889 dp->mem_start = dp->mem_size = 8192 * 3;
890 kip->name = (np->sc_type == NE2000_TYPE_DL10019) ?
891 "dl10022" : "dl10019";
892 break;
893 case NE2000_TYPE_AX88190:
894 case NE2000_TYPE_AX88790:
895 dp->rcr_proto = ED_RCR_INTT;
896 dp->sc_flags |= DP8390_DO_AX88190_WORKAROUND;
897 dp->mem_start = dp->mem_size = 8192 * 2;
898 kip->name = "ax88190";
899 break;
900 }
901
902 if (dp8390_ipkdb_attach(kip))
903 return -1;
904
905 dp->mem_ring = dp->mem_start
906 + ((dp->txb_cnt * ED_TXBUF_SIZE) << ED_PAGE_SHIFT);
907
908 if (!(kip->flags & IPKDB_MYHW)) {
909 char romdata[16];
910
911 /* Read the station address. */
912 if (np->sc_type == NE2000_TYPE_AX88190 ||
913 np->sc_type == NE2000_TYPE_AX88790) {
914 /* Select page 0 registers. */
915 NIC_BARRIER(nict, nich);
916 bus_space_write_1(nict, nich, ED_P0_CR,
917 ED_CR_RD2 | ED_CR_PAGE_0 | ED_CR_STA);
918 NIC_BARRIER(nict, nich);
919 /* Select word transfer */
920 bus_space_write_1(nict, nich, ED_P0_DCR, ED_DCR_WTS);
921 ne2000_readmem(nict, nich, np->sc_asict, np->sc_asich,
922 AX88190_NODEID_OFFSET, kip->myenetaddr,
923 ETHER_ADDR_LEN, useword);
924 } else {
925 ne2000_readmem(nict, nich, np->sc_asict, np->sc_asich,
926 0, romdata, sizeof romdata, useword);
927 useword = useword ? 2 : 1;
928 for (i = 0; i < ETHER_ADDR_LEN; i++)
929 kip->myenetaddr[i] = romdata[i * useword];
930 }
931 kip->flags |= IPKDB_MYHW;
932
933 }
934 dp8390_stop(dp);
935
936 return 0;
937 }
938 #endif
939