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