xref: /netbsd-src/sys/dev/isa/if_ec.c (revision 3b01aba77a7a698587faaae455bbfe740923c1f5)
1 /*	$NetBSD: if_ec.c,v 1.14 2001/07/08 17:55:50 thorpej 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  * Device driver for the 3Com Etherlink II (3c503).
55  */
56 
57 #include <sys/param.h>
58 #include <sys/systm.h>
59 #include <sys/device.h>
60 #include <sys/socket.h>
61 #include <sys/mbuf.h>
62 #include <sys/syslog.h>
63 
64 #include <net/if.h>
65 #include <net/if_dl.h>
66 #include <net/if_types.h>
67 #include <net/if_media.h>
68 
69 #include <net/if_ether.h>
70 
71 #include <machine/bus.h>
72 #include <machine/intr.h>
73 
74 #include <dev/isa/isareg.h>
75 #include <dev/isa/isavar.h>
76 
77 #include <dev/ic/dp8390reg.h>
78 #include <dev/ic/dp8390var.h>
79 
80 #include <dev/isa/if_ecreg.h>
81 
82 struct ec_softc {
83 	struct dp8390_softc sc_dp8390;
84 
85 	bus_space_tag_t sc_asict;	/* space tag for ASIC */
86 	bus_space_handle_t sc_asich;	/* space handle for ASIC */
87 
88 	int sc_16bitp;			/* are we 16 bit? */
89 
90 	void *sc_ih;			/* interrupt handle */
91 };
92 
93 int	ec_probe __P((struct device *, struct cfdata *, void *));
94 void	ec_attach __P((struct device *, struct device *, void *));
95 
96 struct cfattach ec_ca = {
97 	sizeof(struct ec_softc), ec_probe, ec_attach
98 };
99 
100 int	ec_set_media __P((struct ec_softc *, int));
101 
102 void	ec_media_init __P((struct dp8390_softc *));
103 
104 int	ec_mediachange __P((struct dp8390_softc *));
105 void	ec_mediastatus __P((struct dp8390_softc *, struct ifmediareq *));
106 
107 void	ec_init_card __P((struct dp8390_softc *));
108 int	ec_write_mbuf __P((struct dp8390_softc *, struct mbuf *, int));
109 int	ec_ring_copy __P((struct dp8390_softc *, int, caddr_t, u_short));
110 void	ec_read_hdr __P((struct dp8390_softc *, int, struct dp8390_ring *));
111 int	ec_fake_test_mem __P((struct dp8390_softc *));
112 int	ec_test_mem __P((struct dp8390_softc *));
113 
114 __inline void ec_readmem __P((struct ec_softc *, int, u_int8_t *, int));
115 
116 static const int ec_iobase[] = {
117 	0x2e0, 0x2a0, 0x280, 0x250, 0x350, 0x330, 0x310, 0x300,
118 };
119 #define	NEC_IOBASE	(sizeof(ec_iobase) / sizeof(ec_iobase[0]))
120 
121 static const int ec_membase[] = {
122 	MADDRUNK, MADDRUNK, MADDRUNK, MADDRUNK, 0xc8000, 0xcc000,
123 	0xd8000, 0xdc000,
124 };
125 #define	NEC_MEMBASE	(sizeof(ec_membase) / sizeof(ec_membase[0]))
126 
127 int
128 ec_probe(parent, match, aux)
129 	struct device *parent;
130 	struct cfdata *match;
131 	void *aux;
132 {
133 	struct isa_attach_args *ia = aux;
134 	bus_space_tag_t nict, asict, memt;
135 	bus_space_handle_t nich, asich, memh;
136 	bus_size_t memsize;
137 	int nich_valid, asich_valid, memh_valid;
138 	int i, rv = 0;
139 	u_int8_t x;
140 
141 	nict = asict = ia->ia_iot;
142 	memt = ia->ia_memt;
143 
144 	nich_valid = asich_valid = memh_valid = 0;
145 
146 	/*
147 	 * Hmm, a 16-bit card has 16k of memory, but only an 8k window
148 	 * to it.
149 	 */
150 	memsize = 8192;
151 
152 	/* Disallow wildcarded i/o addresses. */
153 	if (ia->ia_iobase == ISACF_PORT_DEFAULT)
154 		return (0);
155 
156 	/* Disallow wildcarded mem address. */
157 	if (ia->ia_maddr == ISACF_IOMEM_DEFAULT)
158 		return (0);
159 
160 	/* Validate the i/o base. */
161 	for (i = 0; i < NEC_IOBASE; i++)
162 		if (ia->ia_iobase == ec_iobase[i])
163 			break;
164 	if (i == NEC_IOBASE)
165 		return (0);
166 
167 	/* Validate the mem base. */
168 	for (i = 0; i < NEC_MEMBASE; i++) {
169 		if (ec_membase[i] == MADDRUNK)
170 			continue;
171 		if (ia->ia_maddr == ec_membase[i])
172 			break;
173 	}
174 	if (i == NEC_MEMBASE)
175 		return (0);
176 
177 	/* Attempt to map the NIC space. */
178 	if (bus_space_map(nict, ia->ia_iobase + ELINK2_NIC_OFFSET,
179 	    ELINK2_NIC_PORTS, 0, &nich))
180 		goto out;
181 	nich_valid = 1;
182 
183 	/* Attempt to map the ASIC space. */
184 	if (bus_space_map(asict, ia->ia_iobase + ELINK2_ASIC_OFFSET,
185 	    ELINK2_ASIC_PORTS, 0, &asich))
186 		goto out;
187 	asich_valid = 1;
188 
189 	/* Attempt to map the memory space. */
190 	if (bus_space_map(memt, ia->ia_maddr, memsize, 0, &memh))
191 		goto out;
192 	memh_valid = 1;
193 
194 	/*
195 	 * Verify that the kernel configured I/O address matches the
196 	 * board configured I/O address.
197 	 *
198 	 * This is really only useful to see if something that looks like
199 	 * the board is there; after all, we're already talking to it at
200 	 * this point.
201 	 */
202 	x = bus_space_read_1(asict, asich, ELINK2_BCFR);
203 	if (x == 0 || (x & (x - 1)) != 0)
204 		goto out;
205 	i = ffs(x) - 1;
206 	if (ia->ia_iobase != ec_iobase[i])
207 		goto out;
208 
209 	/*
210 	 * ...and for the memory address.  Note we do not support
211 	 * cards configured with shared memory disabled.
212 	 */
213 	x = bus_space_read_1(asict, asich, ELINK2_PCFR);
214 	if (x == 0 || (x & (x - 1)) != 0)
215 		goto out;
216 	i = ffs(x) - 1;
217 	if (ia->ia_maddr != ec_membase[i])
218 		goto out;
219 
220 	/* So, we say we've found it! */
221 	ia->ia_iosize = ELINK2_NIC_PORTS;
222 	ia->ia_msize = memsize;
223 	rv = 1;
224 
225  out:
226 	if (nich_valid)
227 		bus_space_unmap(nict, nich, ELINK2_NIC_PORTS);
228 	if (asich_valid)
229 		bus_space_unmap(asict, asich, ELINK2_ASIC_PORTS);
230 	if (memh_valid)
231 		bus_space_unmap(memt, memh, memsize);
232 	return (rv);
233 }
234 
235 void
236 ec_attach(parent, self, aux)
237 	struct device *parent, *self;
238 	void *aux;
239 {
240 	struct ec_softc *esc = (struct ec_softc *)self;
241 	struct dp8390_softc *sc = &esc->sc_dp8390;
242 	struct isa_attach_args *ia = aux;
243 	bus_space_tag_t nict, asict, memt;
244 	bus_space_handle_t nich, asich, memh;
245 	bus_size_t memsize;
246 	u_int8_t tmp;
247 	int i;
248 
249 	printf("\n");
250 
251 	nict = asict = ia->ia_iot;
252 	memt = ia->ia_memt;
253 
254 	/*
255 	 * Hmm, a 16-bit card has 16k of memory, but only an 8k window
256 	 * to it.
257 	 */
258 	memsize = 8192;
259 
260 	/* Map the NIC space. */
261 	if (bus_space_map(nict, ia->ia_iobase + ELINK2_NIC_OFFSET,
262 	    ELINK2_NIC_PORTS, 0, &nich)) {
263 		printf("%s: can't map nic i/o space\n",
264 		    sc->sc_dev.dv_xname);
265 		return;
266 	}
267 
268 	/* Map the ASIC space. */
269 	if (bus_space_map(asict, ia->ia_iobase + ELINK2_ASIC_OFFSET,
270 	    ELINK2_ASIC_PORTS, 0, &asich)) {
271 		printf("%s: can't map asic i/o space\n",
272 		    sc->sc_dev.dv_xname);
273 		return;
274 	}
275 
276 	/* Map the memory space. */
277 	if (bus_space_map(memt, ia->ia_maddr, memsize, 0, &memh)) {
278 		printf("%s: can't map shared memory\n",
279 		    sc->sc_dev.dv_xname);
280 		return;
281 	}
282 
283 	esc->sc_asict = asict;
284 	esc->sc_asich = asich;
285 
286 	sc->sc_regt = nict;
287 	sc->sc_regh = nich;
288 
289 	sc->sc_buft = memt;
290 	sc->sc_bufh = memh;
291 
292 	/* Interface is always enabled. */
293 	sc->sc_enabled = 1;
294 
295 	/* Registers are linear. */
296 	for (i = 0; i < 16; i++)
297 		sc->sc_reg_map[i] = i;
298 
299 	/* Now we can use the NIC_{GET,PUT}() macros. */
300 
301 	/*
302 	 * Reset NIC and ASIC.  Enable on-board transeiver throughout
303 	 * reset sequence since it will lock up if the cable isn't
304 	 * connected if we don't.
305 	 */
306 	bus_space_write_1(asict, asich, ELINK2_CR,
307 	    ELINK2_CR_RST | ELINK2_CR_XSEL);
308 
309 	/* Wait for a while, then un-reset it. */
310 	delay(50);
311 
312 	/*
313 	 * The 3Com ASIC defaults to rather strange settings for the CR
314 	 * after a reset.  It's important to set it again after the
315 	 * following write (this is done when we map the PROM below).
316 	 */
317 	bus_space_write_1(asict, asich, ELINK2_CR, ELINK2_CR_XSEL);
318 
319 	/* Wait a bit for the NIC to recover from the reset. */
320 	delay(5000);
321 
322 	/*
323 	 * Get the station address from on-board ROM.
324 	 *
325 	 * First, map Ethernet address PROM over the top of where the NIC
326 	 * registers normally appear.
327 	 */
328 	bus_space_write_1(asict, asich, ELINK2_CR,
329 	    ELINK2_CR_XSEL | ELINK2_CR_EALO);
330 
331 	for (i = 0; i < ETHER_ADDR_LEN; i++)
332 		sc->sc_enaddr[i] = NIC_GET(nict, nich, i);
333 
334 	/*
335 	 * Unmap PROM - select NIC registers.  The proper setting of the
336 	 * transceiver is set in later in ec_init_card() via dp8390_init().
337 	 */
338 	bus_space_write_1(asict, asich, ELINK2_CR, ELINK2_CR_XSEL);
339 
340 	/* Determine if this is an 8-bit or 16-bit board. */
341 
342 	/* Select page 0 registers. */
343 	NIC_PUT(nict, nich, ED_P0_CR, ED_CR_RD2 | ED_CR_PAGE_0 | ED_CR_STP);
344 
345 	/*
346 	 * Attempt to clear WTS.  If it doesn't clear, then this is a
347 	 * 16-bit board.
348 	 */
349 	NIC_PUT(nict, nich, ED_P0_DCR, 0);
350 
351 	/* Select page 2 registers. */
352 	NIC_PUT(nict, nich, ED_P0_CR, ED_CR_RD2 | ED_CR_PAGE_2 | ED_CR_STP);
353 
354 	/* The 3c503 forces the WTS bit to a one if this is a 16-bit board. */
355 	if (NIC_GET(nict, nich, ED_P2_DCR) & ED_DCR_WTS)
356 		esc->sc_16bitp = 1;
357 	else
358 		esc->sc_16bitp = 0;
359 
360 	printf("%s: 3Com 3c503 Ethernet (%s-bit)\n",
361 	    sc->sc_dev.dv_xname, esc->sc_16bitp ? "16" : "8");
362 
363 	/* Select page 0 registers. */
364 	NIC_PUT(nict, nich, ED_P2_CR, ED_CR_RD2 | ED_CR_PAGE_0 | ED_CR_STP);
365 
366 	sc->cr_proto = ED_CR_RD2;
367 
368 	/*
369 	 * DCR gets:
370 	 *
371 	 *	FIFO threshold to 8, No auto-init Remote DMA,
372 	 *	byte order=80x86.
373 	 *
374 	 * 16-bit cards also get word-wide DMA transfers.
375 	 */
376 	sc->dcr_reg = ED_DCR_FT1 | ED_DCR_LS |
377 	    (esc->sc_16bitp ? ED_DCR_WTS : 0);
378 
379 	sc->test_mem = ec_fake_test_mem;
380 	sc->ring_copy = ec_ring_copy;
381 	sc->write_mbuf = ec_write_mbuf;
382 	sc->read_hdr = ec_read_hdr;
383 	sc->init_card = ec_init_card;
384 
385 	sc->sc_media_init = ec_media_init;
386 
387 	sc->sc_mediachange = ec_mediachange;
388 	sc->sc_mediastatus = ec_mediastatus;
389 
390 	sc->mem_start = 0;
391 	sc->mem_size = memsize;
392 
393 	/* Do generic parts of attach. */
394 	if (dp8390_config(sc)) {
395 		printf("%s: configuration failed\n", sc->sc_dev.dv_xname);
396 		return;
397 	}
398 
399 	/*
400 	 * We need to override the way dp8390_config() set up our
401 	 * shared memory.
402 	 *
403 	 * We have an entire 8k window to put the transmit buffers on the
404 	 * 16-bit boards.  But since the 16bit 3c503's shared memory is only
405 	 * fast enough to overlap the loading of one full-size packet, trying
406 	 * to load more than 2 buffers can actually leave the transmitter idle
407 	 * during the load.  So 2 seems the best value.  (Although a mix of
408 	 * variable-sized packets might change this assumption.  Nonetheless,
409 	 * we optimize for linear transfers of same-size packets.)
410 	 */
411 	if (esc->sc_16bitp) {
412 		if (sc->sc_dev.dv_cfdata->cf_flags & DP8390_NO_MULTI_BUFFERING)
413 			sc->txb_cnt = 1;
414 		else
415 			sc->txb_cnt = 2;
416 
417 		sc->tx_page_start = ELINK2_TX_PAGE_OFFSET_16BIT;
418 		sc->rec_page_start = ELINK2_RX_PAGE_OFFSET_16BIT;
419 		sc->rec_page_stop = (memsize >> ED_PAGE_SHIFT) +
420 		    sc->rec_page_start;
421 		sc->mem_ring = sc->mem_start;
422 	} else {
423 		sc->txb_cnt = 1;
424 		sc->tx_page_start = ELINK2_TX_PAGE_OFFSET_8BIT;
425 		sc->rec_page_start = sc->tx_page_start + ED_TXBUF_SIZE;
426 		sc->rec_page_stop = (memsize >> ED_PAGE_SHIFT) +
427 		    sc->tx_page_start;
428 		sc->mem_ring = sc->mem_start +
429 		    (ED_TXBUF_SIZE << ED_PAGE_SHIFT);
430 	}
431 
432 	/*
433 	 * Initialize CA page start/stop registers.  Probably only needed
434 	 * if doing DMA, but what the Hell.
435 	 */
436 	bus_space_write_1(asict, asich, ELINK2_PSTR, sc->rec_page_start);
437 	bus_space_write_1(asict, asich, ELINK2_PSPR, sc->rec_page_stop);
438 
439 	/*
440 	 * Program the IRQ.
441 	 */
442 	switch (ia->ia_irq) {
443 	case 9:	tmp = ELINK2_IDCFR_IRQ2; break;
444 	case 3:	tmp = ELINK2_IDCFR_IRQ3; break;
445 	case 4:	tmp = ELINK2_IDCFR_IRQ4; break;
446 	case 5:	tmp = ELINK2_IDCFR_IRQ5; break;
447 		break;
448 
449 	case IRQUNK:
450 		printf("%s: wildcarded IRQ is not allowed\n",
451 		    sc->sc_dev.dv_xname);
452 		return;
453 
454 	default:
455 		printf("%s: invalid IRQ %d, must be 3, 4, 5, or 9\n",
456 		    sc->sc_dev.dv_xname, ia->ia_irq);
457 		return;
458 	}
459 
460 	bus_space_write_1(asict, asich, ELINK2_IDCFR, tmp);
461 
462 	/*
463 	 * Initialize the GA configuration register.  Set bank and enable
464 	 * shared memory.
465 	 */
466 	bus_space_write_1(asict, asich, ELINK2_GACFR,
467 	    ELINK2_GACFR_RSEL | ELINK2_GACFR_MBS0);
468 
469 	/*
470 	 * Intialize "Vector Pointer" registers.  These gawd-awful things
471 	 * are compared to 20 bits of the address on the ISA, and if they
472 	 * match, the shared memory is disabled.  We se them to 0xffff0...
473 	 * allegedly the reset vector.
474 	 */
475 	bus_space_write_1(asict, asich, ELINK2_VPTR2, 0xff);
476 	bus_space_write_1(asict, asich, ELINK2_VPTR1, 0xff);
477 	bus_space_write_1(asict, asich, ELINK2_VPTR0, 0x00);
478 
479 	/*
480 	 * Now run the real memory test.
481 	 */
482 	if (ec_test_mem(sc)) {
483 		printf("%s: memory test failed\n", sc->sc_dev.dv_xname);
484 		return;
485 	}
486 
487 	/* Establish interrupt handler. */
488 	esc->sc_ih = isa_intr_establish(ia->ia_ic, ia->ia_irq, IST_EDGE,
489 	    IPL_NET, dp8390_intr, sc);
490 	if (esc->sc_ih == NULL)
491 		printf("%s: can't establish interrupt\n", sc->sc_dev.dv_xname);
492 }
493 
494 int
495 ec_fake_test_mem(sc)
496 	struct dp8390_softc *sc;
497 {
498 
499 	/*
500 	 * We have to do this after we initialize the GA, but we
501 	 * have to do that after calling dp8390_config(), which
502 	 * wants to test memory.  Put this noop here, and then
503 	 * actually test memory later.
504 	 */
505 	return (0);
506 }
507 
508 int
509 ec_test_mem(sc)
510 	struct dp8390_softc *sc;
511 {
512 	struct ec_softc *esc = (struct ec_softc *)sc;
513 	bus_space_tag_t memt = sc->sc_buft;
514 	bus_space_handle_t memh = sc->sc_bufh;
515 	bus_size_t memsize = sc->mem_size;
516 	int i;
517 
518 	if (esc->sc_16bitp)
519 		bus_space_set_region_2(memt, memh, 0, 0, memsize >> 1);
520 	else
521 		bus_space_set_region_1(memt, memh, 0, 0, memsize);
522 
523 	if (esc->sc_16bitp) {
524 		for (i = 0; i < memsize; i += 2) {
525 			if (bus_space_read_2(memt, memh, i) != 0)
526 				goto fail;
527 		}
528 	} else {
529 		for (i = 0; i < memsize; i++) {
530 			if (bus_space_read_1(memt, memh, i) != 0)
531 				goto fail;
532 		}
533 	}
534 
535 	return (0);
536 
537  fail:
538 	printf("%s: failed to clear shared memory at offset 0x%x\n",
539 	    sc->sc_dev.dv_xname, i);
540 	return (1);
541 }
542 
543 /*
544  * Given a NIC memory source address and a host memory destination address,
545  * copy 'len' from NIC to host using shared memory.  The 'len' is rounded
546  * up to a word - ok as long as mbufs are word-sized.
547  */
548 __inline void
549 ec_readmem(esc, from, to, len)
550 	struct ec_softc *esc;
551 	int from;
552 	u_int8_t *to;
553 	int len;
554 {
555 	bus_space_tag_t memt = esc->sc_dp8390.sc_buft;
556 	bus_space_handle_t memh = esc->sc_dp8390.sc_bufh;
557 
558 	if (len & 1)
559 		++len;
560 
561 	if (esc->sc_16bitp)
562 		bus_space_read_region_2(memt, memh, from, (u_int16_t *)to,
563 		    len >> 1);
564 	else
565 		bus_space_read_region_1(memt, memh, from, to, len);
566 }
567 
568 int
569 ec_write_mbuf(sc, m, buf)
570 	struct dp8390_softc *sc;
571 	struct mbuf *m;
572 	int buf;
573 {
574 	struct ec_softc *esc = (struct ec_softc *)sc;
575 	bus_space_tag_t asict = esc->sc_asict;
576 	bus_space_handle_t asich = esc->sc_asich;
577 	bus_space_tag_t memt = esc->sc_dp8390.sc_buft;
578 	bus_space_handle_t memh = esc->sc_dp8390.sc_bufh;
579 	u_int8_t *data, savebyte[2];
580 	int savelen, len, leftover;
581 #ifdef DIAGNOSTIC
582 	u_int8_t *lim;
583 #endif
584 
585 	savelen = m->m_pkthdr.len;
586 
587 	/*
588 	 * 8-bit boards are simple: we're already in the correct
589 	 * page, and no alignment tricks are necessary.
590 	 */
591 	if (esc->sc_16bitp == 0) {
592 		for (; m != NULL; buf += m->m_len, m = m->m_next)
593 			bus_space_write_region_1(memt, memh, buf,
594 			    mtod(m, u_int8_t *), m->m_len);
595 		return (savelen);
596 	}
597 
598 	/*
599 	 * If it's a 16-bit board, we have transmit buffers
600 	 * in a different page; switch to it.
601 	 */
602 	if (esc->sc_16bitp)
603 		bus_space_write_1(asict, asich, ELINK2_GACFR,
604 		    ELINK2_GACFR_RSEL);
605 
606 	/* Start out with no leftover data. */
607 	leftover = 0;
608 	savebyte[0] = savebyte[1] = 0;
609 
610 	for (; m != NULL; m = m->m_next) {
611 		len = m->m_len;
612 		if (len == 0)
613 			continue;
614 		data = mtod(m, u_int8_t *);
615 #ifdef DIAGNOSTIC
616 		lim = data + len;
617 #endif
618 		while (len > 0) {
619 			if (leftover) {
620 				/*
621 				 * Data left over (from mbuf or realignment).
622 				 * Buffer the next byte, and write it and
623 				 * the leftover data out.
624 				 */
625 				savebyte[1] = *data++;
626 				len--;
627 				bus_space_write_2(memt, memh, buf,
628 				    *(u_int16_t *)savebyte);
629 				buf += 2;
630 				leftover = 0;
631 			} else if (BUS_SPACE_ALIGNED_POINTER(data, u_int16_t)
632 				   == 0) {
633 				/*
634 				 * Unaligned data; buffer the next byte.
635 				 */
636 				savebyte[0] = *data++;
637 				len--;
638 				leftover = 1;
639 			} else {
640 				/*
641 				 * Aligned data; output contiguous words as
642 				 * much as we can, then buffer the remaining
643 				 * byte, if any.
644 				 */
645 				leftover = len & 1;
646 				len &= ~1;
647 				bus_space_write_region_2(memt, memh, buf,
648 				    (u_int16_t *)data, len >> 1);
649 				data += len;
650 				buf += len;
651 				if (leftover)
652 					savebyte[0] = *data++;
653 				len = 0;
654 			}
655 		}
656 		if (len < 0)
657 			panic("ec_write_mbuf: negative len");
658 #ifdef DIAGNOSTIC
659 		if (data != lim)
660 			panic("ec_write_mbuf: data != lim");
661 #endif
662 	}
663 	if (leftover) {
664 		savebyte[1] = 0;
665 		bus_space_write_2(memt, memh, buf, *(u_int16_t *)savebyte);
666 	}
667 
668 	/*
669 	 * Switch back to receive page.
670 	 */
671 	if (esc->sc_16bitp)
672 		bus_space_write_1(asict, asich, ELINK2_GACFR,
673 		    ELINK2_GACFR_RSEL | ELINK2_GACFR_MBS0);
674 
675 	return (savelen);
676 }
677 
678 int
679 ec_ring_copy(sc, src, dst, amount)
680 	struct dp8390_softc *sc;
681 	int src;
682 	caddr_t dst;
683 	u_short amount;
684 {
685 	struct ec_softc *esc = (struct ec_softc *)sc;
686 	u_short tmp_amount;
687 
688 	/* Does copy wrap to lower addr in ring buffer? */
689 	if (src + amount > sc->mem_end) {
690 		tmp_amount = sc->mem_end - src;
691 
692 		/* Copy amount up to end of NIC memory. */
693 		ec_readmem(esc, src, dst, tmp_amount);
694 
695 		amount -= tmp_amount;
696 		src = sc->mem_ring;
697 		dst += tmp_amount;
698 	}
699 
700 	ec_readmem(esc, src, dst, amount);
701 
702 	return (src + amount);
703 }
704 
705 void
706 ec_read_hdr(sc, packet_ptr, packet_hdrp)
707 	struct dp8390_softc *sc;
708 	int packet_ptr;
709 	struct dp8390_ring *packet_hdrp;
710 {
711 	struct ec_softc *esc = (struct ec_softc *)sc;
712 
713 	ec_readmem(esc, packet_ptr, (u_int8_t *)packet_hdrp,
714 	    sizeof(struct dp8390_ring));
715 #if BYTE_ORDER == BIG_ENDIAN
716 	packet_hdrp->count = bswap16(packet_hdrp->count);
717 #endif
718 }
719 
720 void
721 ec_media_init(struct dp8390_softc *sc)
722 {
723 
724 	ifmedia_init(&sc->sc_media, 0, dp8390_mediachange, dp8390_mediastatus);
725 	ifmedia_add(&sc->sc_media, IFM_ETHER|IFM_10_2, 0, NULL);
726 	ifmedia_add(&sc->sc_media, IFM_ETHER|IFM_10_5, 0, NULL);
727 	ifmedia_set(&sc->sc_media, IFM_ETHER|IFM_10_2);
728 }
729 
730 int
731 ec_mediachange(sc)
732 	struct dp8390_softc *sc;
733 {
734 	struct ec_softc *esc = (struct ec_softc *)sc;
735 	struct ifmedia *ifm = &sc->sc_media;
736 
737 	return (ec_set_media(esc, ifm->ifm_media));
738 }
739 
740 void
741 ec_mediastatus(sc, ifmr)
742 	struct dp8390_softc *sc;
743 	struct ifmediareq *ifmr;
744 {
745 	struct ifmedia *ifm = &sc->sc_media;
746 
747 	/*
748 	 * The currently selected media is always the active media.
749 	 */
750 	ifmr->ifm_active = ifm->ifm_cur->ifm_media;
751 }
752 
753 void
754 ec_init_card(sc)
755 	struct dp8390_softc *sc;
756 {
757 	struct ec_softc *esc = (struct ec_softc *)sc;
758 	struct ifmedia *ifm = &sc->sc_media;
759 
760 	(void) ec_set_media(esc, ifm->ifm_cur->ifm_media);
761 }
762 
763 int
764 ec_set_media(esc, media)
765 	struct ec_softc *esc;
766 	int media;
767 {
768 	u_int8_t new;
769 
770 	if (IFM_TYPE(media) != IFM_ETHER)
771 		return (EINVAL);
772 
773 	switch (IFM_SUBTYPE(media)) {
774 	case IFM_10_2:
775 		new = ELINK2_CR_XSEL;
776 		break;
777 
778 	case IFM_10_5:
779 		new = 0;
780 		break;
781 
782 	default:
783 		return (EINVAL);
784 	}
785 
786 	bus_space_write_1(esc->sc_asict, esc->sc_asich, ELINK2_CR, new);
787 	return (0);
788 }
789