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