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