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