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