be.c revision 1.10 1 /* $NetBSD: be.c,v 1.10 1999/12/20 22:23:39 pk Exp $ */
2
3 /*-
4 * Copyright (c) 1999 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Paul Kranenburg.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the NetBSD
21 * Foundation, Inc. and its contributors.
22 * 4. Neither the name of The NetBSD Foundation nor the names of its
23 * contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 * POSSIBILITY OF SUCH DAMAGE.
37 */
38
39 /*
40 * Copyright (c) 1998 Theo de Raadt and Jason L. Wright.
41 * All rights reserved.
42 *
43 * Redistribution and use in source and binary forms, with or without
44 * modification, are permitted provided that the following conditions
45 * are met:
46 * 1. Redistributions of source code must retain the above copyright
47 * notice, this list of conditions and the following disclaimer.
48 * 2. Redistributions in binary form must reproduce the above copyright
49 * notice, this list of conditions and the following disclaimer in the
50 * documentation and/or other materials provided with the distribution.
51 * 3. The name of the authors may not be used to endorse or promote products
52 * derived from this software without specific prior written permission.
53 *
54 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
55 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
56 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
57 * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT,
58 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
59 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
60 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
61 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
62 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
63 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
64 */
65
66 #include "opt_ddb.h"
67 #include "opt_inet.h"
68 #include "opt_ccitt.h"
69 #include "opt_llc.h"
70 #include "opt_ns.h"
71 #include "bpfilter.h"
72 #include "rnd.h"
73
74 #include <sys/param.h>
75 #include <sys/systm.h>
76 #include <sys/kernel.h>
77 #include <sys/errno.h>
78 #include <sys/ioctl.h>
79 #include <sys/mbuf.h>
80 #include <sys/socket.h>
81 #include <sys/syslog.h>
82 #include <sys/device.h>
83 #include <sys/malloc.h>
84 #if NRND > 0
85 #include <sys/rnd.h>
86 #endif
87
88 #include <net/if.h>
89 #include <net/if_dl.h>
90 #include <net/if_types.h>
91 #include <net/netisr.h>
92 #include <net/if_media.h>
93 #include <net/if_ether.h>
94
95 #ifdef INET
96 #include <netinet/in.h>
97 #include <netinet/if_inarp.h>
98 #include <netinet/in_systm.h>
99 #include <netinet/in_var.h>
100 #include <netinet/ip.h>
101 #endif
102
103 #ifdef NS
104 #include <netns/ns.h>
105 #include <netns/ns_if.h>
106 #endif
107
108 #if NBPFILTER > 0
109 #include <net/bpf.h>
110 #include <net/bpfdesc.h>
111 #endif
112
113 #include <machine/autoconf.h>
114 #include <machine/cpu.h>
115
116 #include <dev/sbus/sbusvar.h>
117
118 #include <dev/mii/mii.h>
119 #include <dev/mii/miivar.h>
120
121 #include <dev/sbus/qecreg.h>
122 #include <dev/sbus/qecvar.h>
123 #include <dev/sbus/bereg.h>
124
125 struct be_softc {
126 struct device sc_dev;
127 struct sbusdev sc_sd; /* sbus device */
128 bus_space_tag_t sc_bustag; /* bus & dma tags */
129 bus_dma_tag_t sc_dmatag;
130 struct ethercom sc_ethercom;
131 /*struct ifmedia sc_ifmedia; -* interface media */
132 struct mii_data sc_mii; /* MII media control */
133 #define sc_media sc_mii.mii_media/* shorthand */
134
135 struct qec_softc *sc_qec; /* QEC parent */
136
137 bus_space_handle_t sc_qr; /* QEC registers */
138 bus_space_handle_t sc_br; /* BE registers */
139 bus_space_handle_t sc_cr; /* channel registers */
140 bus_space_handle_t sc_tr; /* transceiver registers */
141
142 u_int sc_rev;
143
144 int sc_channel; /* channel number */
145 int sc_burst;
146 int sc_conf;
147 #define BE_CONF_MII 1
148
149 struct qec_ring sc_rb; /* Packet Ring Buffer */
150
151 /* MAC address */
152 u_int8_t sc_enaddr[6];
153 };
154
155 int bematch __P((struct device *, struct cfdata *, void *));
156 void beattach __P((struct device *, struct device *, void *));
157
158 void beinit __P((struct be_softc *));
159 void bestart __P((struct ifnet *));
160 void bestop __P((struct be_softc *));
161 void bewatchdog __P((struct ifnet *));
162 int beioctl __P((struct ifnet *, u_long, caddr_t));
163 void bereset __P((struct be_softc *));
164
165 int beintr __P((void *));
166 int berint __P((struct be_softc *));
167 int betint __P((struct be_softc *));
168 int beqint __P((struct be_softc *, u_int32_t));
169 int beeint __P((struct be_softc *, u_int32_t));
170
171 static void be_read __P((struct be_softc *, int, int));
172 static int be_put __P((struct be_softc *, int, struct mbuf *));
173 static struct mbuf *be_get __P((struct be_softc *, int, int));
174
175 void be_tcvr_init __P((struct be_softc *));
176
177 /* ifmedia callbacks */
178 void be_ifmedia_sts __P((struct ifnet *, struct ifmediareq *));
179 int be_ifmedia_upd __P((struct ifnet *));
180
181 void be_mcreset __P((struct be_softc *));
182
183 /* MII methods & callbacks */
184 static int be_mii_readreg __P((struct device *, int, int));
185 static void be_mii_writereg __P((struct device *, int, int, int));
186 static void be_mii_statchg __P((struct device *));
187
188 /* MII helpers */
189 static void be_mii_sync __P((struct be_softc *));
190 static void be_mii_sendbits __P((struct be_softc *, int, u_int32_t, int));
191 static int be_mii_reset __P((struct be_softc *, int));
192 static int be_tcvr_read_bit __P((struct be_softc *, int));
193 static void be_tcvr_write_bit __P((struct be_softc *, int, int));
194
195 void be_tick __P((void *));
196 void be_internal_phy_auto __P((struct be_softc *));
197
198
199 struct cfattach be_ca = {
200 sizeof(struct be_softc), bematch, beattach
201 };
202
203 int
204 bematch(parent, cf, aux)
205 struct device *parent;
206 struct cfdata *cf;
207 void *aux;
208 {
209 struct sbus_attach_args *sa = aux;
210
211 return (strcmp(cf->cf_driver->cd_name, sa->sa_name) == 0);
212 }
213
214 void
215 beattach(parent, self, aux)
216 struct device *parent, *self;
217 void *aux;
218 {
219 struct sbus_attach_args *sa = aux;
220 struct qec_softc *qec = (struct qec_softc *)parent;
221 struct be_softc *sc = (struct be_softc *)self;
222 struct ifnet *ifp = &sc->sc_ethercom.ec_if;
223 struct mii_data *mii = &sc->sc_mii;
224 int node = sa->sa_node;
225 bus_dma_segment_t seg;
226 bus_size_t size;
227 int rseg, error;
228 extern void myetheraddr __P((u_char *));
229
230 if (sa->sa_nreg < 3) {
231 printf("%s: only %d register sets\n",
232 self->dv_xname, sa->sa_nreg);
233 return;
234 }
235
236 if (bus_space_map2(sa->sa_bustag,
237 (bus_type_t)sa->sa_reg[0].sbr_slot,
238 (bus_addr_t)sa->sa_reg[0].sbr_offset,
239 (bus_size_t)sa->sa_reg[0].sbr_size,
240 BUS_SPACE_MAP_LINEAR, 0, &sc->sc_cr) != 0) {
241 printf("beattach: cannot map registers\n");
242 return;
243 }
244
245 if (bus_space_map2(sa->sa_bustag,
246 (bus_type_t)sa->sa_reg[1].sbr_slot,
247 (bus_addr_t)sa->sa_reg[1].sbr_offset,
248 (bus_size_t)sa->sa_reg[1].sbr_size,
249 BUS_SPACE_MAP_LINEAR, 0, &sc->sc_br) != 0) {
250 printf("beattach: cannot map registers\n");
251 return;
252 }
253
254 if (bus_space_map2(sa->sa_bustag,
255 (bus_type_t)sa->sa_reg[2].sbr_slot,
256 (bus_addr_t)sa->sa_reg[2].sbr_offset,
257 (bus_size_t)sa->sa_reg[2].sbr_size,
258 BUS_SPACE_MAP_LINEAR, 0, &sc->sc_tr) != 0) {
259 printf("beattach: cannot map registers\n");
260 return;
261 }
262
263 sc->sc_qec = qec;
264 sc->sc_qr = qec->sc_regs;
265
266 sc->sc_rev = getpropint(node, "board-version", -1);
267 printf(" rev %x", sc->sc_rev);
268
269 bestop(sc);
270
271 sc->sc_channel = getpropint(node, "channel#", -1);
272 if (sc->sc_channel == -1)
273 sc->sc_channel = 0;
274
275 sc->sc_burst = getpropint(node, "burst-sizes", -1);
276 if (sc->sc_burst == -1)
277 sc->sc_burst = qec->sc_burst;
278
279 /* Clamp at parent's burst sizes */
280 sc->sc_burst &= qec->sc_burst;
281
282 /* Establish interrupt handler */
283 if (sa->sa_nintr)
284 (void)bus_intr_establish(sa->sa_bustag, sa->sa_pri,
285 0, beintr, sc);
286
287 myetheraddr(sc->sc_enaddr);
288 printf(" address %s\n", ether_sprintf(sc->sc_enaddr));
289
290 /*
291 * Allocate descriptor ring and buffers.
292 */
293
294 /* for now, allocate as many bufs as there are ring descriptors */
295 sc->sc_rb.rb_ntbuf = QEC_XD_RING_MAXSIZE;
296 sc->sc_rb.rb_nrbuf = QEC_XD_RING_MAXSIZE;
297
298 size = QEC_XD_RING_MAXSIZE * sizeof(struct qec_xd) +
299 QEC_XD_RING_MAXSIZE * sizeof(struct qec_xd) +
300 sc->sc_rb.rb_ntbuf * BE_PKT_BUF_SZ +
301 sc->sc_rb.rb_nrbuf * BE_PKT_BUF_SZ;
302 if ((error = bus_dmamem_alloc(sa->sa_dmatag, size,
303 NBPG, 0,
304 &seg, 1, &rseg, BUS_DMA_NOWAIT)) != 0) {
305 printf("%s: DMA buffer alloc error %d\n",
306 self->dv_xname, error);
307 return;
308 }
309 sc->sc_rb.rb_dmabase = seg.ds_addr;
310
311 if ((error = bus_dmamem_map(sa->sa_dmatag, &seg, rseg, size,
312 &sc->sc_rb.rb_membase,
313 BUS_DMA_NOWAIT|BUS_DMA_COHERENT)) != 0) {
314 printf("%s: DMA buffer map error %d\n",
315 self->dv_xname, error);
316 bus_dmamem_free(sa->sa_dmatag, &seg, rseg);
317 return;
318 }
319
320 /*
321 * Initialize transceiver and determine which PHY connection to use.
322 */
323 be_tcvr_init(sc);
324
325 /*
326 * Initialize our media structures and MII info.
327 */
328 mii->mii_ifp = ifp;
329 mii->mii_readreg = be_mii_readreg;
330 mii->mii_writereg = be_mii_writereg;
331 mii->mii_statchg = be_mii_statchg;
332
333 ifmedia_init(&mii->mii_media, 0, be_ifmedia_upd, be_ifmedia_sts);
334
335 if ((sc->sc_conf & BE_CONF_MII) != 0) {
336
337 mii_phy_probe(&sc->sc_dev, mii, 0xffffffff, BE_PHY_EXTERNAL,
338 MII_OFFSET_ANY);
339
340 if (LIST_FIRST(&sc->sc_mii.mii_phys) == NULL) {
341 /* No PHY attached */
342 ifmedia_add(&sc->sc_media, IFM_ETHER|IFM_NONE, 0, NULL);
343 ifmedia_set(&sc->sc_media, IFM_ETHER|IFM_NONE);
344 } else {
345 /*
346 * XXX - we can really do the following ONLY if the
347 * phy indeed has the auto negotiation capability!!
348 */
349 ifmedia_set(&sc->sc_media, IFM_ETHER|IFM_AUTO);
350 }
351 } else {
352 /*
353 * The be internal phy looks vaguely like MII hardware,
354 * but not enough to be able to use the MII device
355 * layer. Hence, we have to take care of media selection
356 * ourselves.
357 */
358
359 /* Use `ifm_data' to store BMCR bits */
360 ifmedia_add(&sc->sc_media,
361 IFM_MAKEWORD(IFM_ETHER,IFM_10_T,0,0),
362 0, NULL);
363 ifmedia_add(&sc->sc_media,
364 IFM_MAKEWORD(IFM_ETHER,IFM_10_T,IFM_FDX,0),
365 BMCR_FDX, NULL);
366 ifmedia_add(&sc->sc_media,
367 IFM_MAKEWORD(IFM_ETHER,IFM_100_TX,0,0),
368 BMCR_S100, NULL);
369 ifmedia_add(&sc->sc_media,
370 IFM_MAKEWORD(IFM_ETHER,IFM_100_TX,IFM_FDX,0),
371 BMCR_S100|BMCR_FDX, NULL);
372 ifmedia_add(&sc->sc_media,
373 IFM_MAKEWORD(IFM_ETHER,IFM_AUTO,0,0),
374 0, NULL);
375 ifmedia_set(&sc->sc_media, IFM_ETHER|IFM_AUTO);
376 }
377
378 bcopy(sc->sc_dev.dv_xname, ifp->if_xname, IFNAMSIZ);
379 ifp->if_softc = sc;
380 ifp->if_start = bestart;
381 ifp->if_ioctl = beioctl;
382 ifp->if_watchdog = bewatchdog;
383 ifp->if_flags =
384 IFF_BROADCAST | IFF_SIMPLEX | IFF_NOTRAILERS | IFF_MULTICAST;
385
386 /* Attach the interface. */
387 if_attach(ifp);
388 ether_ifattach(ifp, sc->sc_enaddr);
389
390 #if NBPFILTER > 0
391 bpfattach(&ifp->if_bpf, ifp, DLT_EN10MB,
392 sizeof(struct ether_header));
393 #endif
394 }
395
396
397 /*
398 * Routine to copy from mbuf chain to transmit buffer in
399 * network buffer memory.
400 */
401 static __inline__ int
402 be_put(sc, idx, m)
403 struct be_softc *sc;
404 int idx;
405 struct mbuf *m;
406 {
407 struct mbuf *n;
408 int len, tlen = 0, boff = 0;
409 caddr_t bp;
410
411 bp = sc->sc_rb.rb_txbuf + (idx % sc->sc_rb.rb_ntbuf) * BE_PKT_BUF_SZ;
412
413 for (; m; m = n) {
414 len = m->m_len;
415 if (len == 0) {
416 MFREE(m, n);
417 continue;
418 }
419 bcopy(mtod(m, caddr_t), bp+boff, len);
420 boff += len;
421 tlen += len;
422 MFREE(m, n);
423 }
424 return (tlen);
425 }
426
427 /*
428 * Pull data off an interface.
429 * Len is the length of data, with local net header stripped.
430 * We copy the data into mbufs. When full cluster sized units are present,
431 * we copy into clusters.
432 */
433 static __inline__ struct mbuf *
434 be_get(sc, idx, totlen)
435 struct be_softc *sc;
436 int idx, totlen;
437 {
438 struct ifnet *ifp = &sc->sc_ethercom.ec_if;
439 struct mbuf *m;
440 struct mbuf *top, **mp;
441 int len, pad, boff = 0;
442 caddr_t bp;
443
444 bp = sc->sc_rb.rb_rxbuf + (idx % sc->sc_rb.rb_nrbuf) * BE_PKT_BUF_SZ;
445
446 MGETHDR(m, M_DONTWAIT, MT_DATA);
447 if (m == NULL)
448 return (NULL);
449 m->m_pkthdr.rcvif = ifp;
450 m->m_pkthdr.len = totlen;
451
452 pad = ALIGN(sizeof(struct ether_header)) - sizeof(struct ether_header);
453 m->m_data += pad;
454 len = MHLEN - pad;
455 top = NULL;
456 mp = ⊤
457
458 while (totlen > 0) {
459 if (top) {
460 MGET(m, M_DONTWAIT, MT_DATA);
461 if (m == NULL) {
462 m_freem(top);
463 return (NULL);
464 }
465 len = MLEN;
466 }
467 if (top && totlen >= MINCLSIZE) {
468 MCLGET(m, M_DONTWAIT);
469 if (m->m_flags & M_EXT)
470 len = MCLBYTES;
471 }
472 m->m_len = len = min(totlen, len);
473 bcopy(bp + boff, mtod(m, caddr_t), len);
474 boff += len;
475 totlen -= len;
476 *mp = m;
477 mp = &m->m_next;
478 }
479
480 return (top);
481 }
482
483 /*
484 * Pass a packet to the higher levels.
485 */
486 static __inline__ void
487 be_read(sc, idx, len)
488 struct be_softc *sc;
489 int idx, len;
490 {
491 struct ifnet *ifp = &sc->sc_ethercom.ec_if;
492 struct mbuf *m;
493
494 if (len <= sizeof(struct ether_header) ||
495 len > ETHERMTU + sizeof(struct ether_header)) {
496
497 printf("%s: invalid packet size %d; dropping\n",
498 ifp->if_xname, len);
499
500 ifp->if_ierrors++;
501 return;
502 }
503
504 /*
505 * Pull packet off interface.
506 */
507 m = be_get(sc, idx, len);
508 if (m == NULL) {
509 ifp->if_ierrors++;
510 return;
511 }
512 ifp->if_ipackets++;
513
514 #if NBPFILTER > 0
515 /*
516 * Check if there's a BPF listener on this interface.
517 * If so, hand off the raw packet to BPF.
518 */
519 if (ifp->if_bpf)
520 bpf_mtap(ifp->if_bpf, m);
521 #endif
522 /* Pass the packet up. */
523 (*ifp->if_input)(ifp, m);
524 }
525
526 /*
527 * Start output on interface.
528 * We make two assumptions here:
529 * 1) that the current priority is set to splnet _before_ this code
530 * is called *and* is returned to the appropriate priority after
531 * return
532 * 2) that the IFF_OACTIVE flag is checked before this code is called
533 * (i.e. that the output part of the interface is idle)
534 */
535 void
536 bestart(ifp)
537 struct ifnet *ifp;
538 {
539 struct be_softc *sc = (struct be_softc *)ifp->if_softc;
540 struct qec_xd *txd = sc->sc_rb.rb_txd;
541 struct mbuf *m;
542 unsigned int bix, len;
543 unsigned int ntbuf = sc->sc_rb.rb_ntbuf;
544
545 if ((ifp->if_flags & (IFF_RUNNING | IFF_OACTIVE)) != IFF_RUNNING)
546 return;
547
548 bix = sc->sc_rb.rb_tdhead;
549
550 for (;;) {
551 IF_DEQUEUE(&ifp->if_snd, m);
552 if (m == 0)
553 break;
554
555 #if NBPFILTER > 0
556 /*
557 * If BPF is listening on this interface, let it see the
558 * packet before we commit it to the wire.
559 */
560 if (ifp->if_bpf)
561 bpf_mtap(ifp->if_bpf, m);
562 #endif
563
564 /*
565 * Copy the mbuf chain into the transmit buffer.
566 */
567 len = be_put(sc, bix, m);
568
569 /*
570 * Initialize transmit registers and start transmission
571 */
572 txd[bix].xd_flags = QEC_XD_OWN | QEC_XD_SOP | QEC_XD_EOP |
573 (len & QEC_XD_LENGTH);
574 bus_space_write_4(sc->sc_bustag, sc->sc_cr, BE_CRI_CTRL,
575 BE_CR_CTRL_TWAKEUP);
576
577 if (++bix == QEC_XD_RING_MAXSIZE)
578 bix = 0;
579
580 if (++sc->sc_rb.rb_td_nbusy == ntbuf) {
581 ifp->if_flags |= IFF_OACTIVE;
582 break;
583 }
584 }
585
586 sc->sc_rb.rb_tdhead = bix;
587 }
588
589 void
590 bestop(sc)
591 struct be_softc *sc;
592 {
593 int n;
594 bus_space_tag_t t = sc->sc_bustag;
595 bus_space_handle_t br = sc->sc_br;
596
597 untimeout(be_tick, sc);
598
599 if (sc->sc_conf & BE_CONF_MII) {
600 /* Down the MII. */
601 mii_down(&sc->sc_mii);
602 }
603
604 /* Stop the transmitter */
605 bus_space_write_4(t, br, BE_BRI_TXCFG, 0);
606 for (n = 32; n > 0; n--) {
607 if (bus_space_read_4(t, br, BE_BRI_TXCFG) == 0)
608 break;
609 DELAY(20);
610 }
611
612 /* Stop the receiver */
613 bus_space_write_4(t, br, BE_BRI_RXCFG, 0);
614 for (n = 32; n > 0; n--) {
615 if (bus_space_read_4(t, br, BE_BRI_RXCFG) == 0)
616 break;
617 DELAY(20);
618 }
619 }
620
621 /*
622 * Reset interface.
623 */
624 void
625 bereset(sc)
626 struct be_softc *sc;
627 {
628 int s;
629
630 s = splnet();
631 bestop(sc);
632 beinit(sc);
633 splx(s);
634 }
635
636 void
637 bewatchdog(ifp)
638 struct ifnet *ifp;
639 {
640 struct be_softc *sc = ifp->if_softc;
641
642 log(LOG_ERR, "%s: device timeout\n", sc->sc_dev.dv_xname);
643 ++sc->sc_ethercom.ec_if.if_oerrors;
644
645 bereset(sc);
646 }
647
648 int
649 beintr(v)
650 void *v;
651 {
652 struct be_softc *sc = (struct be_softc *)v;
653 bus_space_tag_t t = sc->sc_bustag;
654 u_int32_t whyq, whyb, whyc;
655 int r = 0;
656
657 /* Read QEC status, channel status and BE status */
658 whyq = bus_space_read_4(t, sc->sc_qr, QEC_QRI_STAT);
659 whyc = bus_space_read_4(t, sc->sc_cr, BE_CRI_STAT);
660 whyb = bus_space_read_4(t, sc->sc_br, BE_BRI_STAT);
661
662 if (whyq & QEC_STAT_BM)
663 r |= beeint(sc, whyb);
664
665 if (whyq & QEC_STAT_ER)
666 r |= beqint(sc, whyc);
667
668 if (whyq & QEC_STAT_TX && whyc & BE_CR_STAT_TXIRQ)
669 r |= betint(sc);
670
671 if (whyq & QEC_STAT_RX && whyc & BE_CR_STAT_RXIRQ)
672 r |= berint(sc);
673
674 return (r);
675 }
676
677 /*
678 * QEC Interrupt.
679 */
680 int
681 beqint(sc, why)
682 struct be_softc *sc;
683 u_int32_t why;
684 {
685 int r = 0, rst = 0;
686
687 if (why & BE_CR_STAT_TXIRQ)
688 r |= 1;
689 if (why & BE_CR_STAT_RXIRQ)
690 r |= 1;
691
692 if (why & BE_CR_STAT_BERROR) {
693 r |= 1;
694 rst = 1;
695 printf("%s: bigmac error\n", sc->sc_dev.dv_xname);
696 }
697
698 if (why & BE_CR_STAT_TXDERR) {
699 r |= 1;
700 rst = 1;
701 printf("%s: bogus tx descriptor\n", sc->sc_dev.dv_xname);
702 }
703
704 if (why & (BE_CR_STAT_TXLERR | BE_CR_STAT_TXPERR | BE_CR_STAT_TXSERR)) {
705 r |= 1;
706 rst = 1;
707 printf("%s: tx dma error ( ", sc->sc_dev.dv_xname);
708 if (why & BE_CR_STAT_TXLERR)
709 printf("Late ");
710 if (why & BE_CR_STAT_TXPERR)
711 printf("Parity ");
712 if (why & BE_CR_STAT_TXSERR)
713 printf("Generic ");
714 printf(")\n");
715 }
716
717 if (why & BE_CR_STAT_RXDROP) {
718 r |= 1;
719 rst = 1;
720 printf("%s: out of rx descriptors\n", sc->sc_dev.dv_xname);
721 }
722
723 if (why & BE_CR_STAT_RXSMALL) {
724 r |= 1;
725 rst = 1;
726 printf("%s: rx descriptor too small\n", sc->sc_dev.dv_xname);
727 }
728
729 if (why & (BE_CR_STAT_RXLERR | BE_CR_STAT_RXPERR | BE_CR_STAT_RXSERR)) {
730 r |= 1;
731 rst = 1;
732 printf("%s: rx dma error ( ", sc->sc_dev.dv_xname);
733 if (why & BE_CR_STAT_RXLERR)
734 printf("Late ");
735 if (why & BE_CR_STAT_RXPERR)
736 printf("Parity ");
737 if (why & BE_CR_STAT_RXSERR)
738 printf("Generic ");
739 printf(")\n");
740 }
741
742 if (!r) {
743 rst = 1;
744 printf("%s: unexpected error interrupt %08x\n",
745 sc->sc_dev.dv_xname, why);
746 }
747
748 if (rst) {
749 printf("%s: resetting\n", sc->sc_dev.dv_xname);
750 bereset(sc);
751 }
752
753 return (r);
754 }
755
756 /*
757 * Error interrupt.
758 */
759 int
760 beeint(sc, why)
761 struct be_softc *sc;
762 u_int32_t why;
763 {
764 int r = 0, rst = 0;
765
766 if (why & BE_BR_STAT_RFIFOVF) {
767 r |= 1;
768 rst = 1;
769 printf("%s: receive fifo overrun\n", sc->sc_dev.dv_xname);
770 }
771 if (why & BE_BR_STAT_TFIFO_UND) {
772 r |= 1;
773 rst = 1;
774 printf("%s: transmit fifo underrun\n", sc->sc_dev.dv_xname);
775 }
776 if (why & BE_BR_STAT_MAXPKTERR) {
777 r |= 1;
778 rst = 1;
779 printf("%s: max packet size error\n", sc->sc_dev.dv_xname);
780 }
781
782 if (!r) {
783 rst = 1;
784 printf("%s: unexpected error interrupt %08x\n",
785 sc->sc_dev.dv_xname, why);
786 }
787
788 if (rst) {
789 printf("%s: resetting\n", sc->sc_dev.dv_xname);
790 bereset(sc);
791 }
792
793 return (r);
794 }
795
796 /*
797 * Transmit interrupt.
798 */
799 int
800 betint(sc)
801 struct be_softc *sc;
802 {
803 struct ifnet *ifp = &sc->sc_ethercom.ec_if;
804 bus_space_tag_t t = sc->sc_bustag;
805 bus_space_handle_t br = sc->sc_br;
806 unsigned int bix, txflags;
807
808 /*
809 * Unload collision counters
810 */
811 ifp->if_collisions +=
812 bus_space_read_4(t, br, BE_BRI_NCCNT) +
813 bus_space_read_4(t, br, BE_BRI_FCCNT) +
814 bus_space_read_4(t, br, BE_BRI_EXCNT) +
815 bus_space_read_4(t, br, BE_BRI_LTCNT);
816
817 /*
818 * the clear the hardware counters
819 */
820 bus_space_write_4(t, br, BE_BRI_NCCNT, 0);
821 bus_space_write_4(t, br, BE_BRI_FCCNT, 0);
822 bus_space_write_4(t, br, BE_BRI_EXCNT, 0);
823 bus_space_write_4(t, br, BE_BRI_LTCNT, 0);
824
825 bix = sc->sc_rb.rb_tdtail;
826
827 for (;;) {
828 if (sc->sc_rb.rb_td_nbusy <= 0)
829 break;
830
831 txflags = sc->sc_rb.rb_txd[bix].xd_flags;
832
833 if (txflags & QEC_XD_OWN)
834 break;
835
836 ifp->if_flags &= ~IFF_OACTIVE;
837 ifp->if_opackets++;
838
839 if (++bix == QEC_XD_RING_MAXSIZE)
840 bix = 0;
841
842 --sc->sc_rb.rb_td_nbusy;
843 }
844
845 sc->sc_rb.rb_tdtail = bix;
846
847 bestart(ifp);
848
849 if (sc->sc_rb.rb_td_nbusy == 0)
850 ifp->if_timer = 0;
851
852 return (1);
853 }
854
855 /*
856 * Receive interrupt.
857 */
858 int
859 berint(sc)
860 struct be_softc *sc;
861 {
862 struct qec_xd *xd = sc->sc_rb.rb_rxd;
863 unsigned int bix, len;
864 unsigned int nrbuf = sc->sc_rb.rb_nrbuf;
865
866 bix = sc->sc_rb.rb_rdtail;
867
868 /*
869 * Process all buffers with valid data.
870 */
871 for (;;) {
872 len = xd[bix].xd_flags;
873 if (len & QEC_XD_OWN)
874 break;
875
876 len &= QEC_XD_LENGTH;
877 be_read(sc, bix, len);
878
879 /* ... */
880 xd[(bix+nrbuf) % QEC_XD_RING_MAXSIZE].xd_flags =
881 QEC_XD_OWN | (BE_PKT_BUF_SZ & QEC_XD_LENGTH);
882
883 if (++bix == QEC_XD_RING_MAXSIZE)
884 bix = 0;
885 }
886
887 sc->sc_rb.rb_rdtail = bix;
888
889 return (1);
890 }
891
892 int
893 beioctl(ifp, cmd, data)
894 struct ifnet *ifp;
895 u_long cmd;
896 caddr_t data;
897 {
898 struct be_softc *sc = ifp->if_softc;
899 struct ifaddr *ifa = (struct ifaddr *)data;
900 struct ifreq *ifr = (struct ifreq *)data;
901 int s, error = 0;
902
903 s = splnet();
904
905 switch (cmd) {
906 case SIOCSIFADDR:
907 ifp->if_flags |= IFF_UP;
908 switch (ifa->ifa_addr->sa_family) {
909 #ifdef INET
910 case AF_INET:
911 beinit(sc);
912 arp_ifinit(ifp, ifa);
913 break;
914 #endif /* INET */
915 #ifdef NS
916 case AF_NS:
917 {
918 struct ns_addr *ina = &IA_SNS(ifa)->sns_addr;
919
920 if (ns_nullhost(*ina))
921 ina->x_host =
922 *(union ns_host *)LLADDR(ifp->if_sadl);
923 else
924 bcopy(ina->x_host.c_host, LLADDR(ifp->if_sadl),
925 sizeof(sc->sc_enaddr));
926 /* Set new address. */
927 beinit(sc);
928 break;
929 }
930 #endif /* NS */
931 default:
932 beinit(sc);
933 break;
934 }
935 break;
936
937 case SIOCSIFFLAGS:
938 if ((ifp->if_flags & IFF_UP) == 0 &&
939 (ifp->if_flags & IFF_RUNNING) != 0) {
940 /*
941 * If interface is marked down and it is running, then
942 * stop it.
943 */
944 bestop(sc);
945 ifp->if_flags &= ~IFF_RUNNING;
946 } else if ((ifp->if_flags & IFF_UP) != 0 &&
947 (ifp->if_flags & IFF_RUNNING) == 0) {
948 /*
949 * If interface is marked up and it is stopped, then
950 * start it.
951 */
952 beinit(sc);
953 } else {
954 /*
955 * Reset the interface to pick up changes in any other
956 * flags that affect hardware registers.
957 */
958 bestop(sc);
959 beinit(sc);
960 }
961 #ifdef BEDEBUG
962 if (ifp->if_flags & IFF_DEBUG)
963 sc->sc_debug = 1;
964 else
965 sc->sc_debug = 0;
966 #endif
967 break;
968
969 case SIOCADDMULTI:
970 case SIOCDELMULTI:
971 error = (cmd == SIOCADDMULTI) ?
972 ether_addmulti(ifr, &sc->sc_ethercom):
973 ether_delmulti(ifr, &sc->sc_ethercom);
974
975 if (error == ENETRESET) {
976 /*
977 * Multicast list has changed; set the hardware filter
978 * accordingly.
979 */
980 be_mcreset(sc);
981 error = 0;
982 }
983 break;
984 case SIOCGIFMEDIA:
985 case SIOCSIFMEDIA:
986 error = ifmedia_ioctl(ifp, ifr, &sc->sc_media, cmd);
987 break;
988 default:
989 error = EINVAL;
990 break;
991 }
992 splx(s);
993 return (error);
994 }
995
996
997 void
998 beinit(sc)
999 struct be_softc *sc;
1000 {
1001 struct ifnet *ifp = &sc->sc_ethercom.ec_if;
1002 bus_space_tag_t t = sc->sc_bustag;
1003 bus_space_handle_t br = sc->sc_br;
1004 bus_space_handle_t cr = sc->sc_cr;
1005 struct qec_softc *qec = sc->sc_qec;
1006 u_int32_t qecaddr;
1007 u_int8_t *ea;
1008 int s;
1009
1010 s = splimp();
1011
1012 qec_meminit(&sc->sc_rb, BE_PKT_BUF_SZ);
1013 be_tcvr_init(sc);
1014
1015 be_ifmedia_upd(ifp);
1016
1017 bestop(sc);
1018
1019 ea = sc->sc_enaddr;
1020 bus_space_write_4(t, br, BE_BRI_MACADDR0, (ea[0] << 8) | ea[1]);
1021 bus_space_write_4(t, br, BE_BRI_MACADDR1, (ea[2] << 8) | ea[3]);
1022 bus_space_write_4(t, br, BE_BRI_MACADDR2, (ea[4] << 8) | ea[5]);
1023
1024 bus_space_write_4(t, br, BE_BRI_HASHTAB0, 0);
1025 bus_space_write_4(t, br, BE_BRI_HASHTAB1, 0);
1026 bus_space_write_4(t, br, BE_BRI_HASHTAB2, 0);
1027 bus_space_write_4(t, br, BE_BRI_HASHTAB3, 0);
1028
1029 be_mcreset(sc);
1030
1031 bus_space_write_4(t, br, BE_BRI_RANDSEED, 0xbd);
1032
1033 bus_space_write_4(t, br, BE_BRI_XIFCFG,
1034 BE_BR_XCFG_ODENABLE | BE_BR_XCFG_RESV);
1035
1036 bus_space_write_4(t, br, BE_BRI_JSIZE, 4);
1037
1038 /*
1039 * Turn off counter expiration interrupts as well as
1040 * 'gotframe' and 'sentframe'
1041 */
1042 bus_space_write_4(t, br, BE_BRI_IMASK,
1043 BE_BR_IMASK_GOTFRAME |
1044 BE_BR_IMASK_RCNTEXP |
1045 BE_BR_IMASK_ACNTEXP |
1046 BE_BR_IMASK_CCNTEXP |
1047 BE_BR_IMASK_LCNTEXP |
1048 BE_BR_IMASK_CVCNTEXP |
1049 BE_BR_IMASK_SENTFRAME |
1050 BE_BR_IMASK_NCNTEXP |
1051 BE_BR_IMASK_ECNTEXP |
1052 BE_BR_IMASK_LCCNTEXP |
1053 BE_BR_IMASK_FCNTEXP |
1054 BE_BR_IMASK_DTIMEXP);
1055
1056 /* Channel registers: */
1057 bus_space_write_4(t, cr, BE_CRI_RXDS, (u_int32_t)sc->sc_rb.rb_rxddma);
1058 bus_space_write_4(t, cr, BE_CRI_TXDS, (u_int32_t)sc->sc_rb.rb_txddma);
1059
1060 qecaddr = sc->sc_channel * qec->sc_msize;
1061 bus_space_write_4(t, cr, BE_CRI_RXWBUF, qecaddr);
1062 bus_space_write_4(t, cr, BE_CRI_RXRBUF, qecaddr);
1063 bus_space_write_4(t, cr, BE_CRI_TXWBUF, qecaddr + qec->sc_rsize);
1064 bus_space_write_4(t, cr, BE_CRI_TXRBUF, qecaddr + qec->sc_rsize);
1065
1066 bus_space_write_4(t, cr, BE_CRI_RIMASK, 0);
1067 bus_space_write_4(t, cr, BE_CRI_TIMASK, 0);
1068 bus_space_write_4(t, cr, BE_CRI_QMASK, 0);
1069 bus_space_write_4(t, cr, BE_CRI_BMASK, 0);
1070 bus_space_write_4(t, cr, BE_CRI_CCNT, 0);
1071
1072 /* Enable transmitter */
1073 bus_space_write_4(t, br, BE_BRI_TXCFG,
1074 BE_BR_TXCFG_FIFO | BE_BR_TXCFG_ENABLE);
1075
1076 /* Enable receiver */
1077 bus_space_write_4(t, br, BE_BRI_RXCFG,
1078 BE_BR_RXCFG_HENABLE | BE_BR_RXCFG_FIFO |
1079 BE_BR_RXCFG_ENABLE);
1080
1081 ifp->if_flags |= IFF_RUNNING;
1082 ifp->if_flags &= ~IFF_OACTIVE;
1083
1084 timeout(be_tick, sc, hz);
1085 splx(s);
1086 }
1087
1088 void
1089 be_mcreset(sc)
1090 struct be_softc *sc;
1091 {
1092 struct ethercom *ec = &sc->sc_ethercom;
1093 struct ifnet *ifp = &sc->sc_ethercom.ec_if;
1094 bus_space_tag_t t = sc->sc_bustag;
1095 bus_space_handle_t br = sc->sc_br;
1096 u_int32_t crc;
1097 u_int16_t hash[4];
1098 u_int8_t octet;
1099 u_int32_t v;
1100 int i, j;
1101 struct ether_multi *enm;
1102 struct ether_multistep step;
1103
1104 if (ifp->if_flags & IFF_PROMISC) {
1105 v = bus_space_read_4(t, br, BE_BRI_RXCFG);
1106 v |= BE_BR_RXCFG_PMISC;
1107 bus_space_write_4(t, br, BE_BRI_RXCFG, v);
1108 return;
1109 }
1110
1111 v = bus_space_read_4(t, br, BE_BRI_RXCFG);
1112 v &= ~BE_BR_RXCFG_PMISC;
1113 bus_space_write_4(t, br, BE_BRI_RXCFG, v);
1114
1115 if (ifp->if_flags & IFF_ALLMULTI) {
1116 bus_space_write_4(t, br, BE_BRI_HASHTAB0, 0xffff);
1117 bus_space_write_4(t, br, BE_BRI_HASHTAB1, 0xffff);
1118 bus_space_write_4(t, br, BE_BRI_HASHTAB2, 0xffff);
1119 bus_space_write_4(t, br, BE_BRI_HASHTAB3, 0xffff);
1120 return;
1121 }
1122
1123 hash[3] = hash[2] = hash[1] = hash[0] = 0;
1124
1125 ETHER_FIRST_MULTI(step, ec, enm);
1126 while (enm != NULL) {
1127 if (bcmp(enm->enm_addrlo, enm->enm_addrhi, ETHER_ADDR_LEN)) {
1128 /*
1129 * We must listen to a range of multicast
1130 * addresses. For now, just accept all
1131 * multicasts, rather than trying to set only
1132 * those filter bits needed to match the range.
1133 * (At this time, the only use of address
1134 * ranges is for IP multicast routing, for
1135 * which the range is big enough to require
1136 * all bits set.)
1137 */
1138 bus_space_write_4(t, br, BE_BRI_HASHTAB0, 0xffff);
1139 bus_space_write_4(t, br, BE_BRI_HASHTAB1, 0xffff);
1140 bus_space_write_4(t, br, BE_BRI_HASHTAB2, 0xffff);
1141 bus_space_write_4(t, br, BE_BRI_HASHTAB3, 0xffff);
1142 ifp->if_flags |= IFF_ALLMULTI;
1143 return;
1144 }
1145
1146 crc = 0xffffffff;
1147
1148 for (i = 0; i < ETHER_ADDR_LEN; i++) {
1149 octet = enm->enm_addrlo[i];
1150
1151 for (j = 0; j < 8; j++) {
1152 if ((crc & 1) ^ (octet & 1)) {
1153 crc >>= 1;
1154 crc ^= MC_POLY_LE;
1155 }
1156 else
1157 crc >>= 1;
1158 octet >>= 1;
1159 }
1160 }
1161
1162 crc >>= 26;
1163 hash[crc >> 4] |= 1 << (crc & 0xf);
1164 ETHER_NEXT_MULTI(step, enm);
1165 }
1166
1167 bus_space_write_4(t, br, BE_BRI_HASHTAB0, hash[0]);
1168 bus_space_write_4(t, br, BE_BRI_HASHTAB1, hash[1]);
1169 bus_space_write_4(t, br, BE_BRI_HASHTAB2, hash[2]);
1170 bus_space_write_4(t, br, BE_BRI_HASHTAB3, hash[3]);
1171 ifp->if_flags &= ~IFF_ALLMULTI;
1172 }
1173
1174 /*
1175 * Set the tcvr to an idle state
1176 */
1177 void
1178 be_mii_sync(sc)
1179 struct be_softc *sc;
1180 {
1181 bus_space_tag_t t = sc->sc_bustag;
1182 bus_space_handle_t tr = sc->sc_tr;
1183 int n = 32;
1184
1185 while (n--) {
1186 bus_space_write_4(t, tr, BE_TRI_MGMTPAL,
1187 MGMT_PAL_INT_MDIO | MGMT_PAL_EXT_MDIO |
1188 MGMT_PAL_OENAB);
1189 (void)bus_space_read_4(t, tr, BE_TRI_MGMTPAL);
1190 bus_space_write_4(t, tr, BE_TRI_MGMTPAL,
1191 MGMT_PAL_INT_MDIO | MGMT_PAL_EXT_MDIO |
1192 MGMT_PAL_OENAB | MGMT_PAL_DCLOCK);
1193 (void)bus_space_read_4(t, tr, BE_TRI_MGMTPAL);
1194 }
1195 }
1196
1197 /*
1198 * Initialize the transceiver and figure out whether we're using the
1199 * external or internal one.
1200 */
1201 void
1202 be_tcvr_init(sc)
1203 struct be_softc *sc;
1204 {
1205 bus_space_tag_t t = sc->sc_bustag;
1206 bus_space_handle_t tr = sc->sc_tr;
1207 u_int32_t v;
1208
1209 be_mii_sync(sc);
1210
1211 if (sc->sc_rev != 1) {
1212 printf("%s: rev %d PAL not supported.\n",
1213 sc->sc_dev.dv_xname,
1214 sc->sc_rev);
1215 return;
1216 }
1217
1218 bus_space_write_4(t, tr, BE_TRI_MGMTPAL,
1219 MGMT_PAL_INT_MDIO | MGMT_PAL_EXT_MDIO |
1220 MGMT_PAL_DCLOCK);
1221 (void)bus_space_read_4(t, tr, BE_TRI_MGMTPAL);
1222
1223 bus_space_write_4(t, tr, BE_TRI_MGMTPAL,
1224 MGMT_PAL_INT_MDIO | MGMT_PAL_EXT_MDIO);
1225 (void)bus_space_read_4(t, tr, BE_TRI_MGMTPAL);
1226 DELAY(200);
1227
1228 v = bus_space_read_4(t, tr, BE_TRI_MGMTPAL);
1229 #ifdef BEDEBUG
1230 if (sc->sc_debug != 0) {
1231 char bits[64];
1232 printf("be_tcvr_init: MGMTPAL=%s\n",
1233 bitmask_snprintf(v, MGMT_PAL_BITS, bits, sizeof(bits)));
1234 }
1235 #endif
1236
1237 if ((v & MGMT_PAL_EXT_MDIO) != 0) {
1238 sc->sc_conf |= BE_CONF_MII;
1239 bus_space_write_4(t, tr, BE_TRI_TCVRPAL,
1240 ~(TCVR_PAL_EXTLBACK | TCVR_PAL_MSENSE |
1241 TCVR_PAL_LTENABLE));
1242 (void)bus_space_read_4(t, tr, BE_TRI_TCVRPAL);
1243 } else if ((v & MGMT_PAL_INT_MDIO) != 0) {
1244 bus_space_write_4(t, tr, BE_TRI_TCVRPAL,
1245 ~(TCVR_PAL_EXTLBACK | TCVR_PAL_MSENSE |
1246 TCVR_PAL_LTENABLE | TCVR_PAL_SERIAL));
1247 (void)bus_space_read_4(t, tr, BE_TRI_TCVRPAL);
1248 } else {
1249 printf("%s: no internal or external transceiver found.\n",
1250 sc->sc_dev.dv_xname);
1251 }
1252 }
1253
1254
1255 static int
1256 be_tcvr_read_bit(sc, phy)
1257 struct be_softc *sc;
1258 int phy;
1259 {
1260 bus_space_tag_t t = sc->sc_bustag;
1261 bus_space_handle_t tr = sc->sc_tr;
1262 int ret;
1263
1264 if (phy == BE_PHY_INTERNAL) {
1265 bus_space_write_4(t, tr, BE_TRI_MGMTPAL, MGMT_PAL_EXT_MDIO);
1266 (void)bus_space_read_4(t, tr, BE_TRI_MGMTPAL);
1267 bus_space_write_4(t, tr, BE_TRI_MGMTPAL,
1268 MGMT_PAL_EXT_MDIO | MGMT_PAL_DCLOCK);
1269 (void)bus_space_read_4(t, tr, BE_TRI_MGMTPAL);
1270 ret = (bus_space_read_4(t, tr, BE_TRI_MGMTPAL) &
1271 MGMT_PAL_INT_MDIO) >> MGMT_PAL_INT_MDIO_SHIFT;
1272 } else {
1273 bus_space_write_4(t, tr, BE_TRI_MGMTPAL, MGMT_PAL_INT_MDIO);
1274 (void)bus_space_read_4(t, tr, BE_TRI_MGMTPAL);
1275 ret = (bus_space_read_4(t, tr, BE_TRI_MGMTPAL) &
1276 MGMT_PAL_EXT_MDIO) >> MGMT_PAL_EXT_MDIO_SHIFT;
1277 bus_space_write_4(t, tr, BE_TRI_MGMTPAL,
1278 MGMT_PAL_INT_MDIO | MGMT_PAL_DCLOCK);
1279 (void)bus_space_read_4(t, tr, BE_TRI_MGMTPAL);
1280 }
1281
1282 return (ret);
1283 }
1284
1285 static void
1286 be_tcvr_write_bit(sc, phy, bit)
1287 struct be_softc *sc;
1288 int phy;
1289 int bit;
1290 {
1291 bus_space_tag_t t = sc->sc_bustag;
1292 bus_space_handle_t tr = sc->sc_tr;
1293 u_int32_t v;
1294
1295 if (phy == BE_PHY_INTERNAL) {
1296 v = ((bit & 1) << MGMT_PAL_INT_MDIO_SHIFT) |
1297 MGMT_PAL_OENAB | MGMT_PAL_EXT_MDIO;
1298 bus_space_write_4(t, tr, BE_TRI_MGMTPAL, v);
1299 (void)bus_space_read_4(t, tr, BE_TRI_MGMTPAL);
1300
1301 bus_space_write_4(t, tr, BE_TRI_MGMTPAL, bit | MGMT_PAL_DCLOCK);
1302 (void)bus_space_read_4(t, tr, BE_TRI_MGMTPAL);
1303 } else {
1304 v = ((bit & 1) << MGMT_PAL_EXT_MDIO_SHIFT)
1305 | MGMT_PAL_OENAB | MGMT_PAL_INT_MDIO;
1306 bus_space_write_4(t, tr, BE_TRI_MGMTPAL, v);
1307 (void)bus_space_read_4(t, tr, BE_TRI_MGMTPAL);
1308 bus_space_write_4(t, tr, BE_TRI_MGMTPAL, v | MGMT_PAL_DCLOCK);
1309 (void)bus_space_read_4(t, tr, BE_TRI_MGMTPAL);
1310 }
1311 }
1312
1313 static void
1314 be_mii_sendbits(sc, phy, data, nbits)
1315 struct be_softc *sc;
1316 int phy;
1317 u_int32_t data;
1318 int nbits;
1319 {
1320 int i;
1321
1322 for (i = 1 << (nbits - 1); i != 0; i >>= 1) {
1323 be_tcvr_write_bit(sc, phy, (data & i) != 0);
1324 }
1325 }
1326
1327 static int
1328 be_mii_readreg(self, phy, reg)
1329 struct device *self;
1330 int phy, reg;
1331 {
1332 struct be_softc *sc = (struct be_softc *)self;
1333 int val = 0, i;
1334
1335 /*
1336 * Read the PHY register by manually driving the MII control lines.
1337 */
1338 be_mii_sync(sc);
1339 be_mii_sendbits(sc, phy, MII_COMMAND_START, 2);
1340 be_mii_sendbits(sc, phy, MII_COMMAND_READ, 2);
1341 be_mii_sendbits(sc, phy, phy, 5);
1342 be_mii_sendbits(sc, phy, reg, 5);
1343
1344 (void) be_tcvr_read_bit(sc, phy);
1345 (void) be_tcvr_read_bit(sc, phy);
1346
1347 for (i = 15; i >= 0; i--)
1348 val |= (be_tcvr_read_bit(sc, phy) << i);
1349
1350 (void) be_tcvr_read_bit(sc, phy);
1351 (void) be_tcvr_read_bit(sc, phy);
1352 (void) be_tcvr_read_bit(sc, phy);
1353
1354 return (val);
1355 }
1356
1357 void
1358 be_mii_writereg(self, phy, reg, val)
1359 struct device *self;
1360 int phy, reg, val;
1361 {
1362 struct be_softc *sc = (struct be_softc *)self;
1363 int i;
1364
1365 /*
1366 * Write the PHY register by manually driving the MII control lines.
1367 */
1368 be_mii_sync(sc);
1369 be_mii_sendbits(sc, phy, MII_COMMAND_START, 2);
1370 be_mii_sendbits(sc, phy, MII_COMMAND_WRITE, 2);
1371 be_mii_sendbits(sc, phy, phy, 5);
1372 be_mii_sendbits(sc, phy, reg, 5);
1373
1374 be_tcvr_write_bit(sc, phy, 1);
1375 be_tcvr_write_bit(sc, phy, 0);
1376
1377 for (i = 15; i >= 0; i--)
1378 be_tcvr_write_bit(sc, phy, (val >> i) & 1);
1379 }
1380
1381 int
1382 be_mii_reset(sc, phy)
1383 struct be_softc *sc;
1384 int phy;
1385 {
1386 int n;
1387
1388 be_mii_writereg((struct device *)sc, phy, MII_BMCR,
1389 BMCR_LOOP | BMCR_PDOWN | BMCR_ISO);
1390 be_mii_writereg((struct device *)sc, phy, MII_BMCR, BMCR_RESET);
1391
1392 for (n = 16; n >= 0; n--) {
1393 int bmcr = be_mii_readreg((struct device *)sc, phy, MII_BMCR);
1394 if ((bmcr & BMCR_RESET) == 0)
1395 break;
1396 DELAY(20);
1397 }
1398 if (n == 0) {
1399 printf("%s: bmcr reset failed\n", sc->sc_dev.dv_xname);
1400 return (EIO);
1401 }
1402 return (0);
1403 }
1404
1405 void
1406 be_mii_statchg(self)
1407 struct device *self;
1408 {
1409 struct be_softc *sc = (struct be_softc *)self;
1410 bus_space_tag_t t = sc->sc_bustag;
1411 bus_space_handle_t br = sc->sc_br;
1412 u_int32_t v;
1413
1414 printf("%s: media_active=%x\n",
1415 self->dv_xname, sc->sc_mii.mii_media_active);
1416
1417 /* Update duplex mode in TX configuration */
1418 v = bus_space_read_4(t, br, BE_BRI_TXCFG);
1419 if ((IFM_OPTIONS(sc->sc_mii.mii_media_active) & IFM_FDX) != 0)
1420 v |= BE_BR_TXCFG_FULLDPLX;
1421 else
1422 v &= ~BE_BR_TXCFG_FULLDPLX;
1423 bus_space_write_4(t, br, BE_BRI_TXCFG, v);
1424 }
1425
1426 void
1427 be_tick(arg)
1428 void *arg;
1429 {
1430 struct be_softc *sc = arg;
1431 int s = splnet();
1432
1433 if ((sc->sc_conf & BE_CONF_MII) != 0)
1434 mii_tick(&sc->sc_mii);
1435 else
1436 be_internal_phy_auto(sc);
1437
1438 splx(s);
1439 timeout(be_tick, sc, hz);
1440 }
1441
1442 void
1443 be_internal_phy_auto(sc)
1444 struct be_softc *sc;
1445 {
1446 struct ifnet *ifp = &sc->sc_ethercom.ec_if;
1447 int bmcr, bmsr;
1448 int bmcr_s100_bit;
1449
1450 /*
1451 * Check link status; if we don't have a link, try another
1452 * speed. We can't detect duplex mode, so half-duplex is
1453 * what we have to settle for.
1454 */
1455
1456 /* Only used for automatic media selection */
1457 if (IFM_SUBTYPE(sc->sc_media.ifm_cur->ifm_media) != IFM_AUTO)
1458 return;
1459
1460 /* Don't bother if interface isn't up */
1461 if ((ifp->if_flags & IFF_UP) == 0)
1462 return;
1463
1464 /* Read twice in case the register is latched */
1465 bmsr = be_mii_readreg((struct device *)sc, BE_PHY_INTERNAL, MII_BMSR)|
1466 be_mii_readreg((struct device *)sc, BE_PHY_INTERNAL, MII_BMSR);
1467
1468 if ((bmsr & BMSR_LINK) != 0) {
1469 /* We have a carrier */
1470 return;
1471 }
1472
1473 /* Note current fast speed bit */
1474 bmcr = be_mii_readreg((struct device *)sc, BE_PHY_INTERNAL, MII_BMCR);
1475 bmcr_s100_bit = bmcr & BMCR_S100;
1476
1477 if (be_mii_reset(sc, BE_PHY_INTERNAL) != 0)
1478 return;
1479
1480 bmcr = be_mii_readreg((struct device *)sc, BE_PHY_INTERNAL, MII_BMCR);
1481 /* Just flip the fast speed bit */
1482 bmcr ^= bmcr_s100_bit;
1483 be_mii_writereg((struct device *)sc, BE_PHY_INTERNAL, MII_BMCR, bmcr);
1484 }
1485
1486 /*
1487 * Get current media settings.
1488 */
1489 void
1490 be_ifmedia_sts(ifp, ifmr)
1491 struct ifnet *ifp;
1492 struct ifmediareq *ifmr;
1493 {
1494 struct be_softc *sc = ifp->if_softc;
1495 int media_active, media_status;
1496 int bmcr, bmsr;
1497
1498 if ((sc->sc_conf & BE_CONF_MII) != 0) {
1499 mii_pollstat(&sc->sc_mii);
1500 ifmr->ifm_status = sc->sc_mii.mii_media_status;
1501 ifmr->ifm_active = sc->sc_mii.mii_media_active;
1502 return;
1503 }
1504
1505 media_status = IFM_AVALID;
1506 media_active = 0;
1507
1508 /*
1509 * Internal transceiver; do the work here.
1510 */
1511 bmcr = be_mii_readreg((struct device *)sc, BE_PHY_INTERNAL, MII_BMCR);
1512
1513 switch (bmcr & (BMCR_S100 | BMCR_FDX)) {
1514 case (BMCR_S100 | BMCR_FDX):
1515 media_active = IFM_ETHER | IFM_100_TX | IFM_FDX;
1516 break;
1517 case BMCR_S100:
1518 media_active = IFM_ETHER | IFM_100_TX | IFM_HDX;
1519 break;
1520 case BMCR_FDX:
1521 media_active = IFM_ETHER | IFM_10_T | IFM_FDX;
1522 break;
1523 case 0:
1524 media_active = IFM_ETHER | IFM_10_T | IFM_HDX;
1525 break;
1526 }
1527
1528 /* Read twice in case the register is latched */
1529 bmsr = be_mii_readreg((struct device *)sc, BE_PHY_INTERNAL, MII_BMSR)|
1530 be_mii_readreg((struct device *)sc, BE_PHY_INTERNAL, MII_BMSR);
1531 if (bmsr & BMSR_LINK)
1532 ifmr->ifm_status |= IFM_ACTIVE;
1533
1534 ifmr->ifm_status = media_status;
1535 ifmr->ifm_active = media_active;
1536 }
1537
1538 /*
1539 * Set media options.
1540 */
1541 int
1542 be_ifmedia_upd(ifp)
1543 struct ifnet *ifp;
1544 {
1545 struct be_softc *sc = ifp->if_softc;
1546 struct ifmedia *ifm = &sc->sc_media;
1547 int newmedia = ifm->ifm_media;
1548 int n, error, phy, bmcr;
1549 char *speed, *mode;
1550 bus_space_tag_t t;
1551 bus_space_handle_t br;
1552 u_int32_t v;
1553
1554 if (IFM_TYPE(newmedia) != IFM_ETHER)
1555 return (EINVAL);
1556
1557 if ((sc->sc_conf & BE_CONF_MII) != 0)
1558 return (mii_mediachg(&sc->sc_mii));
1559
1560 /*
1561 * The rest of this routine is devoted to the
1562 * not-quite-a-phy internal transceiver case.
1563 */
1564 t = sc->sc_bustag;
1565 br = sc->sc_br;
1566 phy = BE_PHY_INTERNAL;
1567
1568 /* Why must we reset the device? */
1569 if ((error = be_mii_reset(sc, phy)) != 0)
1570 return (error);
1571
1572 bmcr = be_mii_readreg((struct device *)sc, phy, MII_BMCR);
1573
1574 if (IFM_SUBTYPE(newmedia) == IFM_100_TX) {
1575 bmcr |= BMCR_S100;
1576 speed = "100baseTX";
1577 } else if (IFM_SUBTYPE(newmedia) == IFM_10_T) {
1578 bmcr &= ~BMCR_S100;
1579 speed = "10baseT";
1580 } else {
1581 speed = "auto sense";
1582 }
1583
1584 printf("%s: selecting %s", sc->sc_dev.dv_xname, speed);
1585
1586 v = bus_space_read_4(t, br, BE_BRI_TXCFG);
1587 if ((IFM_OPTIONS(newmedia) & IFM_FDX) != 0) {
1588 bmcr |= BMCR_FDX;
1589 v |= BE_BR_TXCFG_FULLDPLX;
1590 mode = "full";
1591 } else {
1592 bmcr &= ~BMCR_FDX;
1593 v &= ~BE_BR_TXCFG_FULLDPLX;
1594 mode = "half";
1595 }
1596 bus_space_write_4(t, br, BE_BRI_TXCFG, v);
1597 printf(" %s-duplex\n", mode);
1598
1599 /* Select the new mode and take out of isolation */
1600 be_mii_writereg((struct device *)sc, phy, MII_BMCR, bmcr & ~BMCR_ISO);
1601
1602 for (n = 32; n >= 0; n--) {
1603 bmcr = be_mii_readreg((struct device *)sc, phy, MII_BMCR);
1604 if ((bmcr & BMCR_ISO) == 0)
1605 break;
1606 DELAY(20);
1607 }
1608 if (n == 0) {
1609 printf("%s: bmcr unisolate failed\n", sc->sc_dev.dv_xname);
1610 return (EIO);
1611 }
1612
1613 return (0);
1614 }
1615