1 /* $NetBSD: be.c,v 1.74 2009/09/18 14:40:49 tsutsui Exp $ */
4 * Copyright (c) 1999 The NetBSD Foundation, Inc.
7 * This code is derived from software contributed to The NetBSD Foundation
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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.
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.
33 * Copyright (c) 1998 Theo de Raadt and Jason L. Wright.
34 * All rights reserved.
36 * Redistribution and use in source and binary forms, with or without
37 * modification, are permitted provided that the following conditions
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.
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.
59 #include <sys/cdefs.h>
60 __KERNEL_RCSID(0, "$NetBSD: be.c,v 1.74 2009/09/18 14:40:49 tsutsui Exp $");
67 #include <sys/param.h>
68 #include <sys/systm.h>
69 #include <sys/callout.h>
70 #include <sys/kernel.h>
71 #include <sys/errno.h>
72 #include <sys/ioctl.h>
74 #include <sys/socket.h>
75 #include <sys/syslog.h>
76 #include <sys/device.h>
77 #include <sys/malloc.h>
83 #include <net/if_dl.h>
84 #include <net/if_types.h>
85 #include <net/netisr.h>
86 #include <net/if_media.h>
87 #include <net/if_ether.h>
90 #include <netinet/in.h>
91 #include <netinet/if_inarp.h>
92 #include <netinet/in_systm.h>
93 #include <netinet/in_var.h>
94 #include <netinet/ip.h>
100 #include <net/bpfdesc.h>
104 #include <sys/intr.h>
105 #include <machine/autoconf.h>
107 #include <dev/sbus/sbusvar.h>
109 #include <dev/mii/mii.h>
110 #include <dev/mii/miivar.h>
112 #include <dev/sbus/qecreg.h>
113 #include <dev/sbus/qecvar.h>
114 #include <dev/sbus/bereg.h>
118 bus_space_tag_t sc_bustag
; /* bus & DMA tags */
119 bus_dma_tag_t sc_dmatag
;
120 bus_dmamap_t sc_dmamap
;
121 struct ethercom sc_ethercom
;
122 /*struct ifmedia sc_ifmedia; -* interface media */
123 struct mii_data sc_mii
; /* MII media control */
124 #define sc_media sc_mii.mii_media/* shorthand */
125 int sc_phys
[2]; /* MII instance -> phy */
127 struct callout sc_tick_ch
;
130 * Some `mii_softc' items we need to emulate MII operation
131 * for our internal transceiver.
133 int sc_mii_inst
; /* instance of internal phy */
134 int sc_mii_active
; /* currently active medium */
135 int sc_mii_ticks
; /* tick counter */
136 int sc_mii_flags
; /* phy status flags */
137 #define MIIF_HAVELINK 0x04000000
138 int sc_intphy_curspeed
; /* Established link speed */
140 struct qec_softc
*sc_qec
; /* QEC parent */
142 bus_space_handle_t sc_qr
; /* QEC registers */
143 bus_space_handle_t sc_br
; /* BE registers */
144 bus_space_handle_t sc_cr
; /* channel registers */
145 bus_space_handle_t sc_tr
; /* transceiver registers */
149 int sc_channel
; /* channel number */
152 struct qec_ring sc_rb
; /* Packet Ring Buffer */
155 uint8_t sc_enaddr
[ETHER_ADDR_LEN
];
161 static int bematch(device_t
, cfdata_t
, void *);
162 static void beattach(device_t
, device_t
, void *);
164 static int beinit(struct ifnet
*);
165 static void bestart(struct ifnet
*);
166 static void bestop(struct ifnet
*, int);
167 static void bewatchdog(struct ifnet
*);
168 static int beioctl(struct ifnet
*, u_long
, void *);
169 static void bereset(struct be_softc
*);
170 static void behwreset(struct be_softc
*);
172 static int beintr(void *);
173 static int berint(struct be_softc
*);
174 static int betint(struct be_softc
*);
175 static int beqint(struct be_softc
*, uint32_t);
176 static int beeint(struct be_softc
*, uint32_t);
178 static void be_read(struct be_softc
*, int, int);
179 static int be_put(struct be_softc
*, int, struct mbuf
*);
180 static struct mbuf
*be_get(struct be_softc
*, int, int);
182 static void be_pal_gate(struct be_softc
*, int);
184 /* ifmedia callbacks */
185 static void be_ifmedia_sts(struct ifnet
*, struct ifmediareq
*);
186 static int be_ifmedia_upd(struct ifnet
*);
188 static void be_mcreset(struct be_softc
*);
190 /* MII methods & callbacks */
191 static int be_mii_readreg(device_t
, int, int);
192 static void be_mii_writereg(device_t
, int, int, int);
193 static void be_mii_statchg(device_t
);
196 static void be_mii_sync(struct be_softc
*);
197 static void be_mii_sendbits(struct be_softc
*, int, uint32_t, int);
198 static int be_mii_reset(struct be_softc
*, int);
199 static int be_tcvr_read_bit(struct be_softc
*, int);
200 static void be_tcvr_write_bit(struct be_softc
*, int, int);
202 static void be_tick(void *);
204 static void be_intphy_auto(struct be_softc
*);
206 static void be_intphy_status(struct be_softc
*);
207 static int be_intphy_service(struct be_softc
*, struct mii_data
*, int);
210 CFATTACH_DECL_NEW(be
, sizeof(struct be_softc
),
211 bematch
, beattach
, NULL
, NULL
);
214 bematch(device_t parent
, cfdata_t cf
, void *aux
)
216 struct sbus_attach_args
*sa
= aux
;
218 return strcmp(cf
->cf_name
, sa
->sa_name
) == 0;
222 beattach(device_t parent
, device_t self
, void *aux
)
224 struct sbus_attach_args
*sa
= aux
;
225 struct qec_softc
*qec
= device_private(parent
);
226 struct be_softc
*sc
= device_private(self
);
227 struct ifnet
*ifp
= &sc
->sc_ethercom
.ec_if
;
228 struct mii_data
*mii
= &sc
->sc_mii
;
229 struct mii_softc
*child
;
230 int node
= sa
->sa_node
;
231 bus_dma_tag_t dmatag
= sa
->sa_dmatag
;
232 bus_dma_segment_t seg
;
240 if (sa
->sa_nreg
< 3) {
241 printf(": only %d register sets\n", sa
->sa_nreg
);
245 if (bus_space_map(sa
->sa_bustag
,
246 (bus_addr_t
)BUS_ADDR(sa
->sa_reg
[0].oa_space
, sa
->sa_reg
[0].oa_base
),
247 (bus_size_t
)sa
->sa_reg
[0].oa_size
,
248 0, &sc
->sc_cr
) != 0) {
249 printf(": cannot map registers\n");
253 if (bus_space_map(sa
->sa_bustag
,
254 (bus_addr_t
)BUS_ADDR(sa
->sa_reg
[1].oa_space
, sa
->sa_reg
[1].oa_base
),
255 (bus_size_t
)sa
->sa_reg
[1].oa_size
,
256 0, &sc
->sc_br
) != 0) {
257 printf(": cannot map registers\n");
261 if (bus_space_map(sa
->sa_bustag
,
262 (bus_addr_t
)BUS_ADDR(sa
->sa_reg
[2].oa_space
, sa
->sa_reg
[2].oa_base
),
263 (bus_size_t
)sa
->sa_reg
[2].oa_size
,
264 0, &sc
->sc_tr
) != 0) {
265 printf(": cannot map registers\n");
269 sc
->sc_bustag
= sa
->sa_bustag
;
271 sc
->sc_qr
= qec
->sc_regs
;
273 sc
->sc_rev
= prom_getpropint(node
, "board-version", -1);
274 printf(": rev %x,", sc
->sc_rev
);
276 callout_init(&sc
->sc_tick_ch
, 0);
278 sc
->sc_channel
= prom_getpropint(node
, "channel#", -1);
279 if (sc
->sc_channel
== -1)
282 sc
->sc_burst
= prom_getpropint(node
, "burst-sizes", -1);
283 if (sc
->sc_burst
== -1)
284 sc
->sc_burst
= qec
->sc_burst
;
286 /* Clamp at parent's burst sizes */
287 sc
->sc_burst
&= qec
->sc_burst
;
289 /* Establish interrupt handler */
291 (void)bus_intr_establish(sa
->sa_bustag
, sa
->sa_pri
, IPL_NET
,
294 prom_getether(node
, sc
->sc_enaddr
);
295 printf(" address %s\n", ether_sprintf(sc
->sc_enaddr
));
298 * Allocate descriptor ring and buffers.
301 /* for now, allocate as many bufs as there are ring descriptors */
302 sc
->sc_rb
.rb_ntbuf
= QEC_XD_RING_MAXSIZE
;
303 sc
->sc_rb
.rb_nrbuf
= QEC_XD_RING_MAXSIZE
;
306 QEC_XD_RING_MAXSIZE
* sizeof(struct qec_xd
) +
307 QEC_XD_RING_MAXSIZE
* sizeof(struct qec_xd
) +
308 sc
->sc_rb
.rb_ntbuf
* BE_PKT_BUF_SZ
+
309 sc
->sc_rb
.rb_nrbuf
* BE_PKT_BUF_SZ
;
311 /* Get a DMA handle */
312 if ((error
= bus_dmamap_create(dmatag
, size
, 1, size
, 0,
313 BUS_DMA_NOWAIT
, &sc
->sc_dmamap
)) != 0) {
314 aprint_error_dev(self
, "DMA map create error %d\n", error
);
318 /* Allocate DMA buffer */
319 if ((error
= bus_dmamem_alloc(sa
->sa_dmatag
, size
, 0, 0,
320 &seg
, 1, &rseg
, BUS_DMA_NOWAIT
)) != 0) {
321 aprint_error_dev(self
, "DMA buffer alloc error %d\n", error
);
325 /* Map DMA memory in CPU addressable space */
326 if ((error
= bus_dmamem_map(sa
->sa_dmatag
, &seg
, rseg
, size
,
327 &sc
->sc_rb
.rb_membase
, BUS_DMA_NOWAIT
|BUS_DMA_COHERENT
)) != 0) {
328 aprint_error_dev(self
, "DMA buffer map error %d\n", error
);
329 bus_dmamem_free(sa
->sa_dmatag
, &seg
, rseg
);
333 /* Load the buffer */
334 if ((error
= bus_dmamap_load(dmatag
, sc
->sc_dmamap
,
335 sc
->sc_rb
.rb_membase
, size
, NULL
, BUS_DMA_NOWAIT
)) != 0) {
336 aprint_error_dev(self
, "DMA buffer map load error %d\n", error
);
337 bus_dmamem_unmap(dmatag
, sc
->sc_rb
.rb_membase
, size
);
338 bus_dmamem_free(dmatag
, &seg
, rseg
);
341 sc
->sc_rb
.rb_dmabase
= sc
->sc_dmamap
->dm_segs
[0].ds_addr
;
344 * Initialize our media structures and MII info.
347 mii
->mii_readreg
= be_mii_readreg
;
348 mii
->mii_writereg
= be_mii_writereg
;
349 mii
->mii_statchg
= be_mii_statchg
;
351 ifmedia_init(&mii
->mii_media
, 0, be_ifmedia_upd
, be_ifmedia_sts
);
354 * Initialize transceiver and determine which PHY connection to use.
357 v
= bus_space_read_4(sc
->sc_bustag
, sc
->sc_tr
, BE_TRI_MGMTPAL
);
361 if ((v
& MGMT_PAL_EXT_MDIO
) != 0) {
363 mii_attach(self
, mii
, 0xffffffff, BE_PHY_EXTERNAL
,
366 child
= LIST_FIRST(&mii
->mii_phys
);
368 /* No PHY attached */
369 ifmedia_add(&sc
->sc_media
,
370 IFM_MAKEWORD(IFM_ETHER
, IFM_NONE
, 0, instance
),
372 ifmedia_set(&sc
->sc_media
,
373 IFM_MAKEWORD(IFM_ETHER
, IFM_NONE
, 0, instance
));
376 * Note: we support just one PHY on the external
380 if (LIST_NEXT(child
, mii_list
) != NULL
) {
381 aprint_error_dev(self
,
382 "spurious MII device %s attached\n",
383 device_xname(child
->mii_dev
));
386 if (child
->mii_phy
!= BE_PHY_EXTERNAL
||
387 child
->mii_inst
> 0) {
388 aprint_error_dev(self
,
389 "cannot accommodate MII device %s"
390 " at phy %d, instance %d\n",
391 device_xname(child
->mii_dev
),
392 child
->mii_phy
, child
->mii_inst
);
394 sc
->sc_phys
[instance
] = child
->mii_phy
;
398 * XXX - we can really do the following ONLY if the
399 * phy indeed has the auto negotiation capability!!
401 ifmedia_set(&sc
->sc_media
,
402 IFM_MAKEWORD(IFM_ETHER
, IFM_AUTO
, 0, instance
));
404 /* Mark our current media setting */
405 be_pal_gate(sc
, BE_PHY_EXTERNAL
);
411 if ((v
& MGMT_PAL_INT_MDIO
) != 0) {
413 * The be internal phy looks vaguely like MII hardware,
414 * but not enough to be able to use the MII device
415 * layer. Hence, we have to take care of media selection
419 sc
->sc_mii_inst
= instance
;
420 sc
->sc_phys
[instance
] = BE_PHY_INTERNAL
;
422 /* Use `ifm_data' to store BMCR bits */
423 ifmedia_add(&sc
->sc_media
,
424 IFM_MAKEWORD(IFM_ETHER
, IFM_10_T
, 0, instance
),
426 ifmedia_add(&sc
->sc_media
,
427 IFM_MAKEWORD(IFM_ETHER
, IFM_100_TX
, 0, instance
),
429 ifmedia_add(&sc
->sc_media
,
430 IFM_MAKEWORD(IFM_ETHER
, IFM_AUTO
, 0, instance
),
433 printf("on-board transceiver at %s: 10baseT, 100baseTX, auto\n",
436 be_mii_reset(sc
, BE_PHY_INTERNAL
);
437 /* Only set default medium here if there's no external PHY */
439 be_pal_gate(sc
, BE_PHY_INTERNAL
);
440 ifmedia_set(&sc
->sc_media
,
441 IFM_MAKEWORD(IFM_ETHER
, IFM_AUTO
, 0, instance
));
443 be_mii_writereg(self
,
444 BE_PHY_INTERNAL
, MII_BMCR
, BMCR_ISO
);
447 memcpy(ifp
->if_xname
, device_xname(self
), IFNAMSIZ
);
449 ifp
->if_start
= bestart
;
450 ifp
->if_ioctl
= beioctl
;
451 ifp
->if_watchdog
= bewatchdog
;
452 ifp
->if_init
= beinit
;
453 ifp
->if_stop
= bestop
;
455 IFF_BROADCAST
| IFF_SIMPLEX
| IFF_NOTRAILERS
| IFF_MULTICAST
;
456 IFQ_SET_READY(&ifp
->if_snd
);
458 /* claim 802.1q capability */
459 sc
->sc_ethercom
.ec_capabilities
|= ETHERCAP_VLAN_MTU
;
461 /* Attach the interface. */
463 ether_ifattach(ifp
, sc
->sc_enaddr
);
468 * Routine to copy from mbuf chain to transmit buffer in
469 * network buffer memory.
472 be_put(struct be_softc
*sc
, int idx
, struct mbuf
*m
)
475 int len
, tlen
= 0, boff
= 0;
478 bp
= sc
->sc_rb
.rb_txbuf
+ (idx
% sc
->sc_rb
.rb_ntbuf
) * BE_PKT_BUF_SZ
;
486 memcpy(bp
+ boff
, mtod(m
, void *), len
);
495 * Pull data off an interface.
496 * Len is the length of data, with local net header stripped.
497 * We copy the data into mbufs. When full cluster sized units are present,
498 * we copy into clusters.
500 static inline struct mbuf
*
501 be_get(struct be_softc
*sc
, int idx
, int totlen
)
503 struct ifnet
*ifp
= &sc
->sc_ethercom
.ec_if
;
505 struct mbuf
*top
, **mp
;
506 int len
, pad
, boff
= 0;
509 bp
= sc
->sc_rb
.rb_rxbuf
+ (idx
% sc
->sc_rb
.rb_nrbuf
) * BE_PKT_BUF_SZ
;
511 MGETHDR(m
, M_DONTWAIT
, MT_DATA
);
514 m
->m_pkthdr
.rcvif
= ifp
;
515 m
->m_pkthdr
.len
= totlen
;
517 pad
= ALIGN(sizeof(struct ether_header
)) - sizeof(struct ether_header
);
525 MGET(m
, M_DONTWAIT
, MT_DATA
);
532 if (top
&& totlen
>= MINCLSIZE
) {
533 MCLGET(m
, M_DONTWAIT
);
534 if (m
->m_flags
& M_EXT
)
537 m
->m_len
= len
= min(totlen
, len
);
538 memcpy(mtod(m
, void *), bp
+ boff
, len
);
549 * Pass a packet to the higher levels.
552 be_read(struct be_softc
*sc
, int idx
, int len
)
554 struct ifnet
*ifp
= &sc
->sc_ethercom
.ec_if
;
557 if (len
<= sizeof(struct ether_header
) ||
558 len
> ETHER_MAX_LEN
+ ETHER_VLAN_ENCAP_LEN
) {
561 printf("%s: invalid packet size %d; dropping\n",
569 * Pull packet off interface.
571 m
= be_get(sc
, idx
, len
);
580 * Check if there's a BPF listener on this interface.
581 * If so, hand off the raw packet to BPF.
584 bpf_mtap(ifp
->if_bpf
, m
);
586 /* Pass the packet up. */
587 (*ifp
->if_input
)(ifp
, m
);
591 * Start output on interface.
592 * We make two assumptions here:
593 * 1) that the current priority is set to splnet _before_ this code
594 * is called *and* is returned to the appropriate priority after
596 * 2) that the IFF_OACTIVE flag is checked before this code is called
597 * (i.e. that the output part of the interface is idle)
600 bestart(struct ifnet
*ifp
)
602 struct be_softc
*sc
= ifp
->if_softc
;
603 struct qec_xd
*txd
= sc
->sc_rb
.rb_txd
;
605 unsigned int bix
, len
;
606 unsigned int ntbuf
= sc
->sc_rb
.rb_ntbuf
;
608 if ((ifp
->if_flags
& (IFF_RUNNING
| IFF_OACTIVE
)) != IFF_RUNNING
)
611 bix
= sc
->sc_rb
.rb_tdhead
;
614 IFQ_DEQUEUE(&ifp
->if_snd
, m
);
620 * If BPF is listening on this interface, let it see the
621 * packet before we commit it to the wire.
624 bpf_mtap(ifp
->if_bpf
, m
);
628 * Copy the mbuf chain into the transmit buffer.
630 len
= be_put(sc
, bix
, m
);
633 * Initialize transmit registers and start transmission
635 txd
[bix
].xd_flags
= QEC_XD_OWN
| QEC_XD_SOP
| QEC_XD_EOP
|
636 (len
& QEC_XD_LENGTH
);
637 bus_space_write_4(sc
->sc_bustag
, sc
->sc_cr
,
638 BE_CRI_CTRL
, BE_CR_CTRL_TWAKEUP
);
640 if (++bix
== QEC_XD_RING_MAXSIZE
)
643 if (++sc
->sc_rb
.rb_td_nbusy
== ntbuf
) {
644 ifp
->if_flags
|= IFF_OACTIVE
;
649 sc
->sc_rb
.rb_tdhead
= bix
;
653 bestop(struct ifnet
*ifp
, int disable
)
655 struct be_softc
*sc
= ifp
->if_softc
;
657 callout_stop(&sc
->sc_tick_ch
);
660 mii_down(&sc
->sc_mii
);
661 (void)be_intphy_service(sc
, &sc
->sc_mii
, MII_DOWN
);
667 behwreset(struct be_softc
*sc
)
670 bus_space_tag_t t
= sc
->sc_bustag
;
671 bus_space_handle_t br
= sc
->sc_br
;
673 /* Stop the transmitter */
674 bus_space_write_4(t
, br
, BE_BRI_TXCFG
, 0);
675 for (n
= 32; n
> 0; n
--) {
676 if (bus_space_read_4(t
, br
, BE_BRI_TXCFG
) == 0)
681 /* Stop the receiver */
682 bus_space_write_4(t
, br
, BE_BRI_RXCFG
, 0);
683 for (n
= 32; n
> 0; n
--) {
684 if (bus_space_read_4(t
, br
, BE_BRI_RXCFG
) == 0)
694 bereset(struct be_softc
*sc
)
696 struct ifnet
*ifp
= &sc
->sc_ethercom
.ec_if
;
701 if ((sc
->sc_ethercom
.ec_if
.if_flags
& IFF_UP
) != 0)
707 bewatchdog(struct ifnet
*ifp
)
709 struct be_softc
*sc
= ifp
->if_softc
;
711 log(LOG_ERR
, "%s: device timeout\n", device_xname(sc
->sc_dev
));
712 ++sc
->sc_ethercom
.ec_if
.if_oerrors
;
720 struct be_softc
*sc
= arg
;
721 bus_space_tag_t t
= sc
->sc_bustag
;
722 uint32_t whyq
, whyb
, whyc
;
725 /* Read QEC status, channel status and BE status */
726 whyq
= bus_space_read_4(t
, sc
->sc_qr
, QEC_QRI_STAT
);
727 whyc
= bus_space_read_4(t
, sc
->sc_cr
, BE_CRI_STAT
);
728 whyb
= bus_space_read_4(t
, sc
->sc_br
, BE_BRI_STAT
);
730 if (whyq
& QEC_STAT_BM
)
731 r
|= beeint(sc
, whyb
);
733 if (whyq
& QEC_STAT_ER
)
734 r
|= beqint(sc
, whyc
);
736 if (whyq
& QEC_STAT_TX
&& whyc
& BE_CR_STAT_TXIRQ
)
739 if (whyq
& QEC_STAT_RX
&& whyc
& BE_CR_STAT_RXIRQ
)
749 beqint(struct be_softc
*sc
, uint32_t why
)
751 device_t self
= sc
->sc_dev
;
754 if (why
& BE_CR_STAT_TXIRQ
)
756 if (why
& BE_CR_STAT_RXIRQ
)
759 if (why
& BE_CR_STAT_BERROR
) {
762 aprint_error_dev(self
, "bigmac error\n");
765 if (why
& BE_CR_STAT_TXDERR
) {
768 aprint_error_dev(self
, "bogus tx descriptor\n");
771 if (why
& (BE_CR_STAT_TXLERR
| BE_CR_STAT_TXPERR
| BE_CR_STAT_TXSERR
)) {
774 aprint_error_dev(self
, "tx DMA error ( ");
775 if (why
& BE_CR_STAT_TXLERR
)
777 if (why
& BE_CR_STAT_TXPERR
)
779 if (why
& BE_CR_STAT_TXSERR
)
784 if (why
& BE_CR_STAT_RXDROP
) {
787 aprint_error_dev(self
, "out of rx descriptors\n");
790 if (why
& BE_CR_STAT_RXSMALL
) {
793 aprint_error_dev(self
, "rx descriptor too small\n");
796 if (why
& (BE_CR_STAT_RXLERR
| BE_CR_STAT_RXPERR
| BE_CR_STAT_RXSERR
)) {
799 aprint_error_dev(self
, "rx DMA error ( ");
800 if (why
& BE_CR_STAT_RXLERR
)
802 if (why
& BE_CR_STAT_RXPERR
)
804 if (why
& BE_CR_STAT_RXSERR
)
811 aprint_error_dev(self
, "unexpected error interrupt %08x\n",
816 printf("%s: resetting\n", device_xname(self
));
827 beeint(struct be_softc
*sc
, uint32_t why
)
829 device_t self
= sc
->sc_dev
;
832 if (why
& BE_BR_STAT_RFIFOVF
) {
835 aprint_error_dev(self
, "receive fifo overrun\n");
837 if (why
& BE_BR_STAT_TFIFO_UND
) {
840 aprint_error_dev(self
, "transmit fifo underrun\n");
842 if (why
& BE_BR_STAT_MAXPKTERR
) {
845 aprint_error_dev(self
, "max packet size error\n");
850 aprint_error_dev(self
, "unexpected error interrupt %08x\n",
855 printf("%s: resetting\n", device_xname(self
));
863 * Transmit interrupt.
866 betint(struct be_softc
*sc
)
868 struct ifnet
*ifp
= &sc
->sc_ethercom
.ec_if
;
869 bus_space_tag_t t
= sc
->sc_bustag
;
870 bus_space_handle_t br
= sc
->sc_br
;
871 unsigned int bix
, txflags
;
874 * Unload collision counters
876 ifp
->if_collisions
+=
877 bus_space_read_4(t
, br
, BE_BRI_NCCNT
) +
878 bus_space_read_4(t
, br
, BE_BRI_FCCNT
) +
879 bus_space_read_4(t
, br
, BE_BRI_EXCNT
) +
880 bus_space_read_4(t
, br
, BE_BRI_LTCNT
);
883 * the clear the hardware counters
885 bus_space_write_4(t
, br
, BE_BRI_NCCNT
, 0);
886 bus_space_write_4(t
, br
, BE_BRI_FCCNT
, 0);
887 bus_space_write_4(t
, br
, BE_BRI_EXCNT
, 0);
888 bus_space_write_4(t
, br
, BE_BRI_LTCNT
, 0);
890 bix
= sc
->sc_rb
.rb_tdtail
;
893 if (sc
->sc_rb
.rb_td_nbusy
<= 0)
896 txflags
= sc
->sc_rb
.rb_txd
[bix
].xd_flags
;
898 if (txflags
& QEC_XD_OWN
)
901 ifp
->if_flags
&= ~IFF_OACTIVE
;
904 if (++bix
== QEC_XD_RING_MAXSIZE
)
907 --sc
->sc_rb
.rb_td_nbusy
;
910 sc
->sc_rb
.rb_tdtail
= bix
;
914 if (sc
->sc_rb
.rb_td_nbusy
== 0)
924 berint(struct be_softc
*sc
)
926 struct qec_xd
*xd
= sc
->sc_rb
.rb_rxd
;
927 unsigned int bix
, len
;
928 unsigned int nrbuf
= sc
->sc_rb
.rb_nrbuf
;
930 bix
= sc
->sc_rb
.rb_rdtail
;
933 * Process all buffers with valid data.
936 len
= xd
[bix
].xd_flags
;
937 if (len
& QEC_XD_OWN
)
940 len
&= QEC_XD_LENGTH
;
941 be_read(sc
, bix
, len
);
944 xd
[(bix
+nrbuf
) % QEC_XD_RING_MAXSIZE
].xd_flags
=
945 QEC_XD_OWN
| (BE_PKT_BUF_SZ
& QEC_XD_LENGTH
);
947 if (++bix
== QEC_XD_RING_MAXSIZE
)
951 sc
->sc_rb
.rb_rdtail
= bix
;
957 beioctl(struct ifnet
*ifp
, u_long cmd
, void *data
)
959 struct be_softc
*sc
= ifp
->if_softc
;
960 struct ifaddr
*ifa
= data
;
961 struct ifreq
*ifr
= data
;
968 ifp
->if_flags
|= IFF_UP
;
970 switch (ifa
->ifa_addr
->sa_family
) {
973 arp_ifinit(ifp
, ifa
);
982 if ((error
= ifioctl_common(ifp
, cmd
, data
)) != 0)
984 /* XXX re-use ether_ioctl() */
985 switch (ifp
->if_flags
& (IFF_UP
|IFF_RUNNING
)) {
988 * If interface is marked down and it is running, then
992 ifp
->if_flags
&= ~IFF_RUNNING
;
996 * If interface is marked up and it is stopped, then
1003 * Reset the interface to pick up changes in any other
1004 * flags that affect hardware registers.
1011 if (ifp
->if_flags
& IFF_DEBUG
)
1020 error
= ifmedia_ioctl(ifp
, ifr
, &sc
->sc_media
, cmd
);
1023 if ((error
= ether_ioctl(ifp
, cmd
, data
)) == ENETRESET
) {
1025 * Multicast list has changed; set the hardware filter
1028 if (ifp
->if_flags
& IFF_RUNNING
)
1029 error
= beinit(ifp
);
1041 beinit(struct ifnet
*ifp
)
1043 struct be_softc
*sc
= ifp
->if_softc
;
1044 bus_space_tag_t t
= sc
->sc_bustag
;
1045 bus_space_handle_t br
= sc
->sc_br
;
1046 bus_space_handle_t cr
= sc
->sc_cr
;
1047 struct qec_softc
*qec
= sc
->sc_qec
;
1055 qec_meminit(&sc
->sc_rb
, BE_PKT_BUF_SZ
);
1060 bus_space_write_4(t
, br
, BE_BRI_MACADDR0
, (ea
[0] << 8) | ea
[1]);
1061 bus_space_write_4(t
, br
, BE_BRI_MACADDR1
, (ea
[2] << 8) | ea
[3]);
1062 bus_space_write_4(t
, br
, BE_BRI_MACADDR2
, (ea
[4] << 8) | ea
[5]);
1064 /* Clear hash table */
1065 bus_space_write_4(t
, br
, BE_BRI_HASHTAB0
, 0);
1066 bus_space_write_4(t
, br
, BE_BRI_HASHTAB1
, 0);
1067 bus_space_write_4(t
, br
, BE_BRI_HASHTAB2
, 0);
1068 bus_space_write_4(t
, br
, BE_BRI_HASHTAB3
, 0);
1070 /* Re-initialize RX configuration */
1071 v
= BE_BR_RXCFG_FIFO
;
1072 bus_space_write_4(t
, br
, BE_BRI_RXCFG
, v
);
1076 bus_space_write_4(t
, br
, BE_BRI_RANDSEED
, 0xbd);
1078 bus_space_write_4(t
, br
,
1079 BE_BRI_XIFCFG
, BE_BR_XCFG_ODENABLE
| BE_BR_XCFG_RESV
);
1081 bus_space_write_4(t
, br
, BE_BRI_JSIZE
, 4);
1084 * Turn off counter expiration interrupts as well as
1085 * 'gotframe' and 'sentframe'
1087 bus_space_write_4(t
, br
, BE_BRI_IMASK
,
1088 BE_BR_IMASK_GOTFRAME
|
1089 BE_BR_IMASK_RCNTEXP
|
1090 BE_BR_IMASK_ACNTEXP
|
1091 BE_BR_IMASK_CCNTEXP
|
1092 BE_BR_IMASK_LCNTEXP
|
1093 BE_BR_IMASK_CVCNTEXP
|
1094 BE_BR_IMASK_SENTFRAME
|
1095 BE_BR_IMASK_NCNTEXP
|
1096 BE_BR_IMASK_ECNTEXP
|
1097 BE_BR_IMASK_LCCNTEXP
|
1098 BE_BR_IMASK_FCNTEXP
|
1099 BE_BR_IMASK_DTIMEXP
);
1101 /* Channel registers: */
1102 bus_space_write_4(t
, cr
, BE_CRI_RXDS
, (uint32_t)sc
->sc_rb
.rb_rxddma
);
1103 bus_space_write_4(t
, cr
, BE_CRI_TXDS
, (uint32_t)sc
->sc_rb
.rb_txddma
);
1105 qecaddr
= sc
->sc_channel
* qec
->sc_msize
;
1106 bus_space_write_4(t
, cr
, BE_CRI_RXWBUF
, qecaddr
);
1107 bus_space_write_4(t
, cr
, BE_CRI_RXRBUF
, qecaddr
);
1108 bus_space_write_4(t
, cr
, BE_CRI_TXWBUF
, qecaddr
+ qec
->sc_rsize
);
1109 bus_space_write_4(t
, cr
, BE_CRI_TXRBUF
, qecaddr
+ qec
->sc_rsize
);
1111 bus_space_write_4(t
, cr
, BE_CRI_RIMASK
, 0);
1112 bus_space_write_4(t
, cr
, BE_CRI_TIMASK
, 0);
1113 bus_space_write_4(t
, cr
, BE_CRI_QMASK
, 0);
1114 bus_space_write_4(t
, cr
, BE_CRI_BMASK
, 0);
1115 bus_space_write_4(t
, cr
, BE_CRI_CCNT
, 0);
1117 /* Set max packet length */
1119 if (sc
->sc_ethercom
.ec_capenable
& ETHERCAP_VLAN_MTU
)
1120 v
+= ETHER_VLAN_ENCAP_LEN
;
1121 bus_space_write_4(t
, br
, BE_BRI_RXMAX
, v
);
1122 bus_space_write_4(t
, br
, BE_BRI_TXMAX
, v
);
1124 /* Enable transmitter */
1125 bus_space_write_4(t
, br
,
1126 BE_BRI_TXCFG
, BE_BR_TXCFG_FIFO
| BE_BR_TXCFG_ENABLE
);
1128 /* Enable receiver */
1129 v
= bus_space_read_4(t
, br
, BE_BRI_RXCFG
);
1130 v
|= BE_BR_RXCFG_FIFO
| BE_BR_RXCFG_ENABLE
;
1131 bus_space_write_4(t
, br
, BE_BRI_RXCFG
, v
);
1133 if ((rc
= be_ifmedia_upd(ifp
)) != 0)
1136 ifp
->if_flags
|= IFF_RUNNING
;
1137 ifp
->if_flags
&= ~IFF_OACTIVE
;
1139 callout_reset(&sc
->sc_tick_ch
, hz
, be_tick
, sc
);
1148 be_mcreset(struct be_softc
*sc
)
1150 struct ethercom
*ec
= &sc
->sc_ethercom
;
1151 struct ifnet
*ifp
= &sc
->sc_ethercom
.ec_if
;
1152 bus_space_tag_t t
= sc
->sc_bustag
;
1153 bus_space_handle_t br
= sc
->sc_br
;
1157 struct ether_multi
*enm
;
1158 struct ether_multistep step
;
1160 if (ifp
->if_flags
& IFF_PROMISC
) {
1161 v
= bus_space_read_4(t
, br
, BE_BRI_RXCFG
);
1162 v
|= BE_BR_RXCFG_PMISC
;
1163 bus_space_write_4(t
, br
, BE_BRI_RXCFG
, v
);
1167 if (ifp
->if_flags
& IFF_ALLMULTI
) {
1168 hash
[3] = hash
[2] = hash
[1] = hash
[0] = 0xffff;
1172 hash
[3] = hash
[2] = hash
[1] = hash
[0] = 0;
1174 ETHER_FIRST_MULTI(step
, ec
, enm
);
1175 while (enm
!= NULL
) {
1176 if (memcmp(enm
->enm_addrlo
, enm
->enm_addrhi
, ETHER_ADDR_LEN
)) {
1178 * We must listen to a range of multicast
1179 * addresses. For now, just accept all
1180 * multicasts, rather than trying to set only
1181 * those filter bits needed to match the range.
1182 * (At this time, the only use of address
1183 * ranges is for IP multicast routing, for
1184 * which the range is big enough to require
1187 hash
[3] = hash
[2] = hash
[1] = hash
[0] = 0xffff;
1188 ifp
->if_flags
|= IFF_ALLMULTI
;
1192 crc
= ether_crc32_le(enm
->enm_addrlo
, ETHER_ADDR_LEN
);
1193 /* Just want the 6 most significant bits. */
1196 hash
[crc
>> 4] |= 1 << (crc
& 0xf);
1197 ETHER_NEXT_MULTI(step
, enm
);
1200 ifp
->if_flags
&= ~IFF_ALLMULTI
;
1203 /* Enable the hash filter */
1204 bus_space_write_4(t
, br
, BE_BRI_HASHTAB0
, hash
[0]);
1205 bus_space_write_4(t
, br
, BE_BRI_HASHTAB1
, hash
[1]);
1206 bus_space_write_4(t
, br
, BE_BRI_HASHTAB2
, hash
[2]);
1207 bus_space_write_4(t
, br
, BE_BRI_HASHTAB3
, hash
[3]);
1209 v
= bus_space_read_4(t
, br
, BE_BRI_RXCFG
);
1210 v
&= ~BE_BR_RXCFG_PMISC
;
1211 v
|= BE_BR_RXCFG_HENABLE
;
1212 bus_space_write_4(t
, br
, BE_BRI_RXCFG
, v
);
1216 * Set the tcvr to an idle state
1219 be_mii_sync(struct be_softc
*sc
)
1221 bus_space_tag_t t
= sc
->sc_bustag
;
1222 bus_space_handle_t tr
= sc
->sc_tr
;
1226 bus_space_write_4(t
, tr
, BE_TRI_MGMTPAL
,
1227 MGMT_PAL_INT_MDIO
| MGMT_PAL_EXT_MDIO
| MGMT_PAL_OENAB
);
1228 (void)bus_space_read_4(t
, tr
, BE_TRI_MGMTPAL
);
1229 bus_space_write_4(t
, tr
, BE_TRI_MGMTPAL
,
1230 MGMT_PAL_INT_MDIO
| MGMT_PAL_EXT_MDIO
|
1231 MGMT_PAL_OENAB
| MGMT_PAL_DCLOCK
);
1232 (void)bus_space_read_4(t
, tr
, BE_TRI_MGMTPAL
);
1237 be_pal_gate(struct be_softc
*sc
, int phy
)
1239 bus_space_tag_t t
= sc
->sc_bustag
;
1240 bus_space_handle_t tr
= sc
->sc_tr
;
1245 v
= ~(TCVR_PAL_EXTLBACK
| TCVR_PAL_MSENSE
| TCVR_PAL_LTENABLE
);
1246 if (phy
== BE_PHY_INTERNAL
)
1247 v
&= ~TCVR_PAL_SERIAL
;
1249 bus_space_write_4(t
, tr
, BE_TRI_TCVRPAL
, v
);
1250 (void)bus_space_read_4(t
, tr
, BE_TRI_TCVRPAL
);
1254 be_tcvr_read_bit(struct be_softc
*sc
, int phy
)
1256 bus_space_tag_t t
= sc
->sc_bustag
;
1257 bus_space_handle_t tr
= sc
->sc_tr
;
1260 if (phy
== BE_PHY_INTERNAL
) {
1261 bus_space_write_4(t
, tr
, BE_TRI_MGMTPAL
, MGMT_PAL_EXT_MDIO
);
1262 (void)bus_space_read_4(t
, tr
, BE_TRI_MGMTPAL
);
1263 bus_space_write_4(t
, tr
,
1264 BE_TRI_MGMTPAL
, MGMT_PAL_EXT_MDIO
| MGMT_PAL_DCLOCK
);
1265 (void)bus_space_read_4(t
, tr
, BE_TRI_MGMTPAL
);
1266 ret
= (bus_space_read_4(t
, tr
, BE_TRI_MGMTPAL
) &
1267 MGMT_PAL_INT_MDIO
) >> MGMT_PAL_INT_MDIO_SHIFT
;
1269 bus_space_write_4(t
, tr
, BE_TRI_MGMTPAL
, MGMT_PAL_INT_MDIO
);
1270 (void)bus_space_read_4(t
, tr
, BE_TRI_MGMTPAL
);
1271 ret
= (bus_space_read_4(t
, tr
, BE_TRI_MGMTPAL
) &
1272 MGMT_PAL_EXT_MDIO
) >> MGMT_PAL_EXT_MDIO_SHIFT
;
1273 bus_space_write_4(t
, tr
,
1274 BE_TRI_MGMTPAL
, MGMT_PAL_INT_MDIO
| MGMT_PAL_DCLOCK
);
1275 (void)bus_space_read_4(t
, tr
, BE_TRI_MGMTPAL
);
1282 be_tcvr_write_bit(struct be_softc
*sc
, int phy
, int bit
)
1284 bus_space_tag_t t
= sc
->sc_bustag
;
1285 bus_space_handle_t tr
= sc
->sc_tr
;
1288 if (phy
== BE_PHY_INTERNAL
) {
1289 v
= ((bit
& 1) << MGMT_PAL_INT_MDIO_SHIFT
) |
1290 MGMT_PAL_OENAB
| MGMT_PAL_EXT_MDIO
;
1292 v
= ((bit
& 1) << MGMT_PAL_EXT_MDIO_SHIFT
) |
1293 MGMT_PAL_OENAB
| MGMT_PAL_INT_MDIO
;
1295 bus_space_write_4(t
, tr
, BE_TRI_MGMTPAL
, v
);
1296 (void)bus_space_read_4(t
, tr
, BE_TRI_MGMTPAL
);
1297 bus_space_write_4(t
, tr
, BE_TRI_MGMTPAL
, v
| MGMT_PAL_DCLOCK
);
1298 (void)bus_space_read_4(t
, tr
, BE_TRI_MGMTPAL
);
1302 be_mii_sendbits(struct be_softc
*sc
, int phy
, uint32_t data
, int nbits
)
1306 for (i
= 1 << (nbits
- 1); i
!= 0; i
>>= 1) {
1307 be_tcvr_write_bit(sc
, phy
, (data
& i
) != 0);
1312 be_mii_readreg(device_t self
, int phy
, int reg
)
1314 struct be_softc
*sc
= device_private(self
);
1318 * Read the PHY register by manually driving the MII control lines.
1321 be_mii_sendbits(sc
, phy
, MII_COMMAND_START
, 2);
1322 be_mii_sendbits(sc
, phy
, MII_COMMAND_READ
, 2);
1323 be_mii_sendbits(sc
, phy
, phy
, 5);
1324 be_mii_sendbits(sc
, phy
, reg
, 5);
1326 (void)be_tcvr_read_bit(sc
, phy
);
1327 (void)be_tcvr_read_bit(sc
, phy
);
1329 for (i
= 15; i
>= 0; i
--)
1330 val
|= (be_tcvr_read_bit(sc
, phy
) << i
);
1332 (void)be_tcvr_read_bit(sc
, phy
);
1333 (void)be_tcvr_read_bit(sc
, phy
);
1334 (void)be_tcvr_read_bit(sc
, phy
);
1340 be_mii_writereg(device_t self
, int phy
, int reg
, int val
)
1342 struct be_softc
*sc
= device_private(self
);
1346 * Write the PHY register by manually driving the MII control lines.
1349 be_mii_sendbits(sc
, phy
, MII_COMMAND_START
, 2);
1350 be_mii_sendbits(sc
, phy
, MII_COMMAND_WRITE
, 2);
1351 be_mii_sendbits(sc
, phy
, phy
, 5);
1352 be_mii_sendbits(sc
, phy
, reg
, 5);
1354 be_tcvr_write_bit(sc
, phy
, 1);
1355 be_tcvr_write_bit(sc
, phy
, 0);
1357 for (i
= 15; i
>= 0; i
--)
1358 be_tcvr_write_bit(sc
, phy
, (val
>> i
) & 1);
1362 be_mii_reset(struct be_softc
*sc
, int phy
)
1364 device_t self
= sc
->sc_dev
;
1367 be_mii_writereg(self
, phy
, MII_BMCR
, BMCR_LOOP
| BMCR_PDOWN
| BMCR_ISO
);
1368 be_mii_writereg(self
, phy
, MII_BMCR
, BMCR_RESET
);
1370 for (n
= 16; n
>= 0; n
--) {
1371 int bmcr
= be_mii_readreg(self
, phy
, MII_BMCR
);
1372 if ((bmcr
& BMCR_RESET
) == 0)
1377 aprint_error_dev(self
, "bmcr reset failed\n");
1387 struct be_softc
*sc
= arg
;
1390 mii_tick(&sc
->sc_mii
);
1391 (void)be_intphy_service(sc
, &sc
->sc_mii
, MII_TICK
);
1394 callout_reset(&sc
->sc_tick_ch
, hz
, be_tick
, sc
);
1398 be_mii_statchg(device_t self
)
1400 struct be_softc
*sc
= device_private(self
);
1401 bus_space_tag_t t
= sc
->sc_bustag
;
1402 bus_space_handle_t br
= sc
->sc_br
;
1406 instance
= IFM_INST(sc
->sc_mii
.mii_media
.ifm_cur
->ifm_media
);
1409 panic("be_mii_statchg: instance %d out of range", instance
);
1412 /* Update duplex mode in TX configuration */
1413 v
= bus_space_read_4(t
, br
, BE_BRI_TXCFG
);
1414 if ((IFM_OPTIONS(sc
->sc_mii
.mii_media_active
) & IFM_FDX
) != 0)
1415 v
|= BE_BR_TXCFG_FULLDPLX
;
1417 v
&= ~BE_BR_TXCFG_FULLDPLX
;
1418 bus_space_write_4(t
, br
, BE_BRI_TXCFG
, v
);
1420 /* Change to appropriate gate in transceiver PAL */
1421 be_pal_gate(sc
, sc
->sc_phys
[instance
]);
1425 * Get current media settings.
1428 be_ifmedia_sts(struct ifnet
*ifp
, struct ifmediareq
*ifmr
)
1430 struct be_softc
*sc
= ifp
->if_softc
;
1432 mii_pollstat(&sc
->sc_mii
);
1433 (void)be_intphy_service(sc
, &sc
->sc_mii
, MII_POLLSTAT
);
1435 ifmr
->ifm_status
= sc
->sc_mii
.mii_media_status
;
1436 ifmr
->ifm_active
= sc
->sc_mii
.mii_media_active
;
1440 * Set media options.
1443 be_ifmedia_upd(struct ifnet
*ifp
)
1445 struct be_softc
*sc
= ifp
->if_softc
;
1448 if ((error
= mii_mediachg(&sc
->sc_mii
)) == ENXIO
)
1450 else if (error
!= 0)
1453 return be_intphy_service(sc
, &sc
->sc_mii
, MII_MEDIACHG
);
1457 * Service routine for our pseudo-MII internal transceiver.
1460 be_intphy_service(struct be_softc
*sc
, struct mii_data
*mii
, int cmd
)
1462 struct ifmedia_entry
*ife
= mii
->mii_media
.ifm_cur
;
1463 device_t self
= sc
->sc_dev
;
1470 * If we're not polling our PHY instance, just return.
1472 if (IFM_INST(ife
->ifm_media
) != sc
->sc_mii_inst
)
1480 * If the media indicates a different PHY instance,
1481 * isolate ourselves.
1483 if (IFM_INST(ife
->ifm_media
) != sc
->sc_mii_inst
) {
1484 bmcr
= be_mii_readreg(self
, BE_PHY_INTERNAL
, MII_BMCR
);
1485 be_mii_writereg(self
,
1486 BE_PHY_INTERNAL
, MII_BMCR
, bmcr
| BMCR_ISO
);
1487 sc
->sc_mii_flags
&= ~MIIF_HAVELINK
;
1488 sc
->sc_intphy_curspeed
= 0;
1493 if ((error
= be_mii_reset(sc
, BE_PHY_INTERNAL
)) != 0)
1496 bmcr
= be_mii_readreg(self
, BE_PHY_INTERNAL
, MII_BMCR
);
1499 * Select the new mode and take out of isolation
1501 if (IFM_SUBTYPE(ife
->ifm_media
) == IFM_100_TX
)
1503 else if (IFM_SUBTYPE(ife
->ifm_media
) == IFM_10_T
)
1505 else if (IFM_SUBTYPE(ife
->ifm_media
) == IFM_AUTO
) {
1506 if ((sc
->sc_mii_flags
& MIIF_HAVELINK
) != 0) {
1508 bmcr
|= sc
->sc_intphy_curspeed
;
1510 /* Keep isolated until link is up */
1512 sc
->sc_mii_flags
|= MIIF_DOINGAUTO
;
1516 if ((IFM_OPTIONS(ife
->ifm_media
) & IFM_FDX
) != 0)
1521 be_mii_writereg(self
, BE_PHY_INTERNAL
, MII_BMCR
, bmcr
);
1526 * If we're not currently selected, just return.
1528 if (IFM_INST(ife
->ifm_media
) != sc
->sc_mii_inst
)
1531 /* Only used for automatic media selection */
1532 if (IFM_SUBTYPE(ife
->ifm_media
) != IFM_AUTO
)
1535 /* Is the interface even up? */
1536 if ((mii
->mii_ifp
->if_flags
& IFF_UP
) == 0)
1540 * Check link status; if we don't have a link, try another
1541 * speed. We can't detect duplex mode, so half-duplex is
1542 * what we have to settle for.
1545 /* Read twice in case the register is latched */
1547 be_mii_readreg(self
, BE_PHY_INTERNAL
, MII_BMSR
) |
1548 be_mii_readreg(self
, BE_PHY_INTERNAL
, MII_BMSR
);
1550 if ((bmsr
& BMSR_LINK
) != 0) {
1551 /* We have a carrier */
1552 bmcr
= be_mii_readreg(self
, BE_PHY_INTERNAL
, MII_BMCR
);
1554 if ((sc
->sc_mii_flags
& MIIF_DOINGAUTO
) != 0) {
1555 bmcr
= be_mii_readreg(self
,
1556 BE_PHY_INTERNAL
, MII_BMCR
);
1558 sc
->sc_mii_flags
|= MIIF_HAVELINK
;
1559 sc
->sc_intphy_curspeed
= (bmcr
& BMCR_S100
);
1560 sc
->sc_mii_flags
&= ~MIIF_DOINGAUTO
;
1563 be_mii_writereg(self
,
1564 BE_PHY_INTERNAL
, MII_BMCR
, bmcr
);
1566 printf("%s: link up at %s Mbps\n",
1568 (bmcr
& BMCR_S100
) ? "100" : "10");
1573 if ((sc
->sc_mii_flags
& MIIF_DOINGAUTO
) == 0) {
1574 sc
->sc_mii_flags
|= MIIF_DOINGAUTO
;
1575 sc
->sc_mii_flags
&= ~MIIF_HAVELINK
;
1576 sc
->sc_intphy_curspeed
= 0;
1577 printf("%s: link down\n", device_xname(self
));
1580 /* Only retry autonegotiation every 5 seconds. */
1581 if (++sc
->sc_mii_ticks
< 5)
1584 sc
->sc_mii_ticks
= 0;
1585 bmcr
= be_mii_readreg(self
, BE_PHY_INTERNAL
, MII_BMCR
);
1586 /* Just flip the fast speed bit */
1588 be_mii_writereg(self
, BE_PHY_INTERNAL
, MII_BMCR
, bmcr
);
1593 /* Isolate this phy */
1594 bmcr
= be_mii_readreg(self
, BE_PHY_INTERNAL
, MII_BMCR
);
1595 be_mii_writereg(self
,
1596 BE_PHY_INTERNAL
, MII_BMCR
, bmcr
| BMCR_ISO
);
1600 /* Update the media status. */
1601 be_intphy_status(sc
);
1603 /* Callback if something changed. */
1604 if (sc
->sc_mii_active
!= mii
->mii_media_active
|| cmd
== MII_MEDIACHG
) {
1605 (*mii
->mii_statchg
)(self
);
1606 sc
->sc_mii_active
= mii
->mii_media_active
;
1612 * Determine status of internal transceiver
1615 be_intphy_status(struct be_softc
*sc
)
1617 struct mii_data
*mii
= &sc
->sc_mii
;
1618 device_t self
= sc
->sc_dev
;
1619 int media_active
, media_status
;
1622 media_status
= IFM_AVALID
;
1626 * Internal transceiver; do the work here.
1628 bmcr
= be_mii_readreg(self
, BE_PHY_INTERNAL
, MII_BMCR
);
1630 switch (bmcr
& (BMCR_S100
| BMCR_FDX
)) {
1631 case (BMCR_S100
| BMCR_FDX
):
1632 media_active
= IFM_ETHER
| IFM_100_TX
| IFM_FDX
;
1635 media_active
= IFM_ETHER
| IFM_100_TX
| IFM_HDX
;
1638 media_active
= IFM_ETHER
| IFM_10_T
| IFM_FDX
;
1641 media_active
= IFM_ETHER
| IFM_10_T
| IFM_HDX
;
1645 /* Read twice in case the register is latched */
1647 be_mii_readreg(self
, BE_PHY_INTERNAL
, MII_BMSR
) |
1648 be_mii_readreg(self
, BE_PHY_INTERNAL
, MII_BMSR
);
1649 if (bmsr
& BMSR_LINK
)
1650 media_status
|= IFM_ACTIVE
;
1652 mii
->mii_media_status
= media_status
;
1653 mii
->mii_media_active
= media_active
;