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