/*
*
* Copyright (C) 2007 Mark Kettenis.
* Copyright (C) 2001 Eduardo Horvath.
* All rights reserved.
*
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
*/
/*
* Driver for Sun Cassini ethernet controllers.
*
* There are basically two variants of this chip: Cassini and
* Cassini+. We can distinguish between the two by revision: 0x10 and
* up are Cassini+. The most important difference is that Cassini+
* has a second RX descriptor ring. Cassini+ will not work without
* configuring that second ring. However, since we don't use it we
* don't actually fill the descriptors, and only hand off the first
* four to the chip.
*/
int cas_eint(struct cas_softc *, u_int);
int cas_rint(struct cas_softc *);
int cas_tint(struct cas_softc *, uint32_t);
int cas_pint(struct cas_softc *);
int cas_intr(void *);
#ifdef CAS_DEBUG
#define DPRINTF(sc, x) if ((sc)->sc_ethercom.ec_if.if_flags & IFF_DEBUG) \
printf x
#else
#define DPRINTF(sc, x) /* nothing */
#endif
if (pci_intr_map(pa, &sc->sc_handle) != 0) {
aprint_error_dev(sc->sc_dev, "unable to map interrupt\n");
bus_space_unmap(sc->sc_memt, sc->sc_memh, sc->sc_size);
return;
}
sc->sc_pc = pa->pa_pc;
if (!cas_estintr(sc, CAS_INTR_PCI)) {
bus_space_unmap(sc->sc_memt, sc->sc_memh, sc->sc_size);
aprint_error_dev(sc->sc_dev, "unable to establish interrupt\n");
return;
}
sc->sc_att_stage = CAS_ATT_BACKEND_1;
/*
* call the main configure
*/
cas_config(sc, enaddr);
if (pmf_device_register1(sc->sc_dev,
cas_suspend, cas_resume, cas_shutdown))
pmf_class_network_register(sc->sc_dev, &sc->sc_ethercom.ec_if);
else
aprint_error_dev(sc->sc_dev,
"could not establish power handlers\n");
/*
* cas_config:
*
* Attach a Cassini interface to the system.
*/
void
cas_config(struct cas_softc *sc, const uint8_t *enaddr)
{
struct ifnet *ifp = &sc->sc_ethercom.ec_if;
struct mii_data *mii = &sc->sc_mii;
struct mii_softc *child;
uint32_t reg;
int i, error;
/* Make sure the chip is stopped. */
ifp->if_softc = sc;
cas_reset(sc);
/*
* Allocate the control data structures, and create and load the
* DMA map for it.
*/
if ((error = bus_dmamem_alloc(sc->sc_dmatag,
sizeof(struct cas_control_data), CAS_PAGE_SIZE, 0, &sc->sc_cdseg,
1, &sc->sc_cdnseg, 0)) != 0) {
aprint_error_dev(sc->sc_dev,
"unable to allocate control data, error = %d\n",
error);
cas_partial_detach(sc, CAS_ATT_0);
}
/* XXX should map this in with correct endianness */
if ((error = bus_dmamem_map(sc->sc_dmatag, &sc->sc_cdseg,
sc->sc_cdnseg, sizeof(struct cas_control_data),
(void **)&sc->sc_control_data, BUS_DMA_COHERENT)) != 0) {
aprint_error_dev(sc->sc_dev,
"unable to map control data, error = %d\n", error);
cas_partial_detach(sc, CAS_ATT_1);
}
if ((error = bus_dmamap_create(sc->sc_dmatag,
sizeof(struct cas_control_data), 1,
sizeof(struct cas_control_data), 0, 0, &sc->sc_cddmamap)) != 0) {
aprint_error_dev(sc->sc_dev,
"unable to create control data DMA map, error = %d\n",
error);
cas_partial_detach(sc, CAS_ATT_2);
}
if ((error = bus_dmamap_load(sc->sc_dmatag, sc->sc_cddmamap,
sc->sc_control_data, sizeof(struct cas_control_data), NULL,
0)) != 0) {
aprint_error_dev(sc->sc_dev,
"unable to load control data DMA map, error = %d\n",
error);
cas_partial_detach(sc, CAS_ATT_3);
}
child = LIST_FIRST(&mii->mii_phys);
if (child == NULL) {
/* No PHY attached */
ifmedia_add(&sc->sc_media, IFM_ETHER | IFM_MANUAL, 0, NULL);
ifmedia_set(&sc->sc_media, IFM_ETHER | IFM_MANUAL);
} else {
/*
* Walk along the list of attached MII devices and
* establish an `MII instance' to `phy number'
* mapping. We'll use this mapping in media change
* requests to determine which phy to use to program
* the MIF configuration register.
*/
for (; child != NULL; child = LIST_NEXT(child, mii_list)) {
/*
* Note: we support just two PHYs: the built-in
* internal device and an external on the MII
* connector.
*/
if (child->mii_phy > 1 || child->mii_inst > 1) {
aprint_error_dev(sc->sc_dev,
"cannot accommodate MII device %s"
" at phy %d, instance %d\n",
device_xname(child->mii_dev),
child->mii_phy, child->mii_inst);
continue;
}
sc->sc_phys[child->mii_inst] = child->mii_phy;
}
/*
* XXX - we can really do the following ONLY if the
* phy indeed has the auto negotiation capability!!
*/
ifmedia_set(&sc->sc_media, IFM_ETHER | IFM_AUTO);
}
int
cas_detach(device_t self, int flags)
{
int i;
struct cas_softc *sc = device_private(self);
bus_space_tag_t t = sc->sc_memt;
bus_space_handle_t h = sc->sc_memh;
struct ifnet *ifp = &sc->sc_ethercom.ec_if;
/*
* Free any resources we've allocated during the failed attach
* attempt. Do this in reverse order and fall through.
*/
switch (sc->sc_att_stage) {
case CAS_ATT_FINISHED:
bus_space_write_4(t, h, CAS_INTMASK, ~(uint32_t)0);
pmf_device_deregister(self);
cas_stop(&sc->sc_ethercom.ec_if, 1);
evcnt_detach(&sc->sc_ev_intr);
/*FALLTHROUGH*/
case CAS_ATT_MII:
case CAS_ATT_7:
case CAS_ATT_6:
for (i = 0; i < CAS_NTXDESC; i++) {
if (sc->sc_txd[i].sd_map != NULL)
bus_dmamap_destroy(sc->sc_dmatag,
sc->sc_txd[i].sd_map);
}
/*FALLTHROUGH*/
case CAS_ATT_5:
for (i = 0; i < CAS_NRXDESC; i++) {
if (sc->sc_rxsoft[i].rxs_dmamap != NULL)
bus_dmamap_unload(sc->sc_dmatag,
sc->sc_rxsoft[i].rxs_dmamap);
if (sc->sc_rxsoft[i].rxs_dmamap != NULL)
bus_dmamap_destroy(sc->sc_dmatag,
sc->sc_rxsoft[i].rxs_dmamap);
if (sc->sc_rxsoft[i].rxs_kva != NULL)
bus_dmamem_unmap(sc->sc_dmatag,
sc->sc_rxsoft[i].rxs_kva, CAS_PAGE_SIZE);
/* XXX need to check that bus_dmamem_alloc suceeded
if (sc->sc_rxsoft[i].rxs_dmaseg != NULL)
*/
bus_dmamem_free(sc->sc_dmatag,
&(sc->sc_rxsoft[i].rxs_dmaseg), 1);
}
bus_dmamap_unload(sc->sc_dmatag, sc->sc_cddmamap);
/*FALLTHROUGH*/
case CAS_ATT_4:
case CAS_ATT_3:
bus_dmamap_destroy(sc->sc_dmatag, sc->sc_cddmamap);
/*FALLTHROUGH*/
case CAS_ATT_2:
bus_dmamem_unmap(sc->sc_dmatag, sc->sc_control_data,
sizeof(struct cas_control_data));
/*FALLTHROUGH*/
case CAS_ATT_1:
bus_dmamem_free(sc->sc_dmatag, &sc->sc_cdseg, sc->sc_cdnseg);
/*FALLTHROUGH*/
case CAS_ATT_0:
sc->sc_att_stage = CAS_ATT_0;
/*FALLTHROUGH*/
case CAS_ATT_BACKEND_2:
case CAS_ATT_BACKEND_1:
if (sc->sc_ih != NULL) {
pci_intr_disestablish(sc->sc_pc, sc->sc_ih);
sc->sc_ih = NULL;
}
bus_space_unmap(sc->sc_memt, sc->sc_memh, sc->sc_size);
/*FALLTHROUGH*/
case CAS_ATT_BACKEND_0:
break;
}
return 0;
}
/*
* Mark the interface down and cancel the watchdog timer.
*/
ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
ifp->if_timer = 0;
mii_down(&sc->sc_mii);
cas_reset_rx(sc);
cas_reset_tx(sc);
/*
* Release any queued transmit buffers.
*/
for (i = 0; i < CAS_NTXDESC; i++) {
sd = &sc->sc_txd[i];
if (sd->sd_mbuf != NULL) {
bus_dmamap_sync(sc->sc_dmatag, sd->sd_map, 0,
sd->sd_map->dm_mapsize, BUS_DMASYNC_POSTWRITE);
bus_dmamap_unload(sc->sc_dmatag, sd->sd_map);
m_freem(sd->sd_mbuf);
sd->sd_mbuf = NULL;
}
}
sc->sc_tx_cnt = sc->sc_tx_prod = sc->sc_tx_cons = 0;
if (disable)
cas_rxdrain(sc);
}
/*
* Reset the receiver
*/
int
cas_reset_rx(struct cas_softc *sc)
{
bus_space_tag_t t = sc->sc_memt;
bus_space_handle_t h = sc->sc_memh;
/*
* Resetting while DMA is in progress can cause a bus hang, so we
* disable DMA first.
*/
cas_disable_rx(sc);
bus_space_write_4(t, h, CAS_RX_CONFIG, 0);
/* Wait till it finishes */
if (!cas_bitwait(sc, h, CAS_RX_CONFIG, 1, 0))
aprint_error_dev(sc->sc_dev, "cannot disable rx dma\n");
/* Wait 5ms extra. */
delay(5000);
/* Finally, reset the ERX */
bus_space_write_4(t, h, CAS_RESET, CAS_RESET_RX);
/* Wait till it finishes */
if (!cas_bitwait(sc, h, CAS_RESET, CAS_RESET_RX, 0)) {
aprint_error_dev(sc->sc_dev, "cannot reset receiver\n");
return (1);
}
return (0);
}
/*
* Reset the transmitter
*/
int
cas_reset_tx(struct cas_softc *sc)
{
bus_space_tag_t t = sc->sc_memt;
bus_space_handle_t h = sc->sc_memh;
/*
* Resetting while DMA is in progress can cause a bus hang, so we
* disable DMA first.
*/
cas_disable_tx(sc);
bus_space_write_4(t, h, CAS_TX_CONFIG, 0);
/* Wait till it finishes */
if (!cas_bitwait(sc, h, CAS_TX_CONFIG, 1, 0))
aprint_error_dev(sc->sc_dev, "cannot disable tx dma\n");
/* Wait 5ms extra. */
delay(5000);
/* Finally, reset the ETX */
bus_space_write_4(t, h, CAS_RESET, CAS_RESET_TX);
/* Wait till it finishes */
if (!cas_bitwait(sc, h, CAS_RESET, CAS_RESET_TX, 0)) {
aprint_error_dev(sc->sc_dev, "cannot reset transmitter\n");
return (1);
}
return (0);
}
/*
* Disable receiver.
*/
int
cas_disable_rx(struct cas_softc *sc)
{
bus_space_tag_t t = sc->sc_memt;
bus_space_handle_t h = sc->sc_memh;
uint32_t cfg;
/* Flip the enable bit */
cfg = bus_space_read_4(t, h, CAS_MAC_RX_CONFIG);
cfg &= ~CAS_MAC_RX_ENABLE;
bus_space_write_4(t, h, CAS_MAC_RX_CONFIG, cfg);
/* Wait for it to finish */
return (cas_bitwait(sc, h, CAS_MAC_RX_CONFIG, CAS_MAC_RX_ENABLE, 0));
}
/*
* Disable transmitter.
*/
int
cas_disable_tx(struct cas_softc *sc)
{
bus_space_tag_t t = sc->sc_memt;
bus_space_handle_t h = sc->sc_memh;
uint32_t cfg;
/* Flip the enable bit */
cfg = bus_space_read_4(t, h, CAS_MAC_TX_CONFIG);
cfg &= ~CAS_MAC_TX_ENABLE;
bus_space_write_4(t, h, CAS_MAC_TX_CONFIG, cfg);
/* Wait for it to finish */
return (cas_bitwait(sc, h, CAS_MAC_TX_CONFIG, CAS_MAC_TX_ENABLE, 0));
}
/*
* Initialize interface.
*/
int
cas_meminit(struct cas_softc *sc)
{
int i;
/*
* Initialize the transmit descriptor ring.
*/
for (i = 0; i < CAS_NTXDESC; i++) {
sc->sc_txdescs[i].cd_flags = 0;
sc->sc_txdescs[i].cd_addr = 0;
}
CAS_CDTXSYNC(sc, 0, CAS_NTXDESC,
BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
/*
* Initialize the receive descriptor and receive job
* descriptor rings.
*/
for (i = 0; i < CAS_NRXDESC; i++)
CAS_INIT_RXDESC(sc, i, i);
sc->sc_rxdptr = 0;
sc->sc_rxptr = 0;
/*
* Initialize the receive completion ring.
*/
for (i = 0; i < CAS_NRXCOMP; i++) {
sc->sc_rxcomps[i].cc_word[0] = 0;
sc->sc_rxcomps[i].cc_word[1] = 0;
sc->sc_rxcomps[i].cc_word[2] = 0;
sc->sc_rxcomps[i].cc_word[3] = CAS_DMA_WRITE(CAS_RC3_OWN);
CAS_CDRXCSYNC(sc, i,
BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
}
return (0);
}
int
cas_ringsize(int sz)
{
switch (sz) {
case 32:
return CAS_RING_SZ_32;
case 64:
return CAS_RING_SZ_64;
case 128:
return CAS_RING_SZ_128;
case 256:
return CAS_RING_SZ_256;
case 512:
return CAS_RING_SZ_512;
case 1024:
return CAS_RING_SZ_1024;
case 2048:
return CAS_RING_SZ_2048;
case 4096:
return CAS_RING_SZ_4096;
case 8192:
return CAS_RING_SZ_8192;
default:
aprint_error("cas: invalid Receive Descriptor ring size %d\n",
sz);
return CAS_RING_SZ_32;
}
}
int
cas_cringsize(int sz)
{
int i;
for (i = 0; i < 9; i++)
if (sz == (128 << i))
return i;
aprint_error("cas: invalid completion ring size %d\n", sz);
return 128;
}
/*
* Initialization of interface; set up initialization block
* and transmit/receive descriptor rings.
*/
int
cas_init(struct ifnet *ifp)
{
struct cas_softc *sc = (struct cas_softc *)ifp->if_softc;
bus_space_tag_t t = sc->sc_memt;
bus_space_handle_t h = sc->sc_memh;
int s;
u_int max_frame_size;
uint32_t v;
s = splnet();
DPRINTF(sc, ("%s: cas_init: calling stop\n", device_xname(sc->sc_dev)));
/*
* Initialization sequence. The numbered steps below correspond
* to the sequence outlined in section 6.3.5.1 in the Ethernet
* Channel Engine manual (part of the PCIO manual).
* See also the STP2002-STQ document from Sun Microsystems.
*/
/* Encode Receive Descriptor ring size */
v = cas_ringsize(CAS_NRXDESC) << CAS_RX_CONFIG_RXDRNG_SZ_SHIFT;
if (CAS_PLUS(sc))
v |= cas_ringsize(32) << CAS_RX_CONFIG_RXDRNG2_SZ_SHIFT;
/* Encode Receive Completion ring size */
v |= cas_cringsize(CAS_NRXCOMP) << CAS_RX_CONFIG_RXCRNG_SZ_SHIFT;
/*
* The following value is for an OFF Threshold of about 3/4 full
* and an ON Threshold of 1/4 full.
*/
bus_space_write_4(t, h, CAS_RX_PAUSE_THRESH,
(3 * sc->sc_rxfifosize / 256) |
((sc->sc_rxfifosize / 256) << 12));
bus_space_write_4(t, h, CAS_RX_BLANKING, (6 << 12) | 6);
/* step 11. Configure Media */
mii_ifmedia_change(&sc->sc_mii);
/* step 15. Give the receiver a swift kick */
bus_space_write_4(t, h, CAS_RX_KICK, CAS_NRXDESC-4);
if (CAS_PLUS(sc))
bus_space_write_4(t, h, CAS_RX_KICK2, 4);
/* Start the one second timer. */
callout_schedule(&sc->sc_tick_ch, hz);
/* Secondary MAC addresses set to 0:0:0:0:0:0 */
for (r = CAS_MAC_ADDR3; r < CAS_MAC_ADDR42; r += 4)
bus_space_write_4(t, h, r, 0);
/* MAC control addr set to 0:1:c2:0:1:80 */
bus_space_write_4(t, h, CAS_MAC_ADDR42, 0x0001);
bus_space_write_4(t, h, CAS_MAC_ADDR43, 0xc200);
bus_space_write_4(t, h, CAS_MAC_ADDR44, 0x0180);
/* MAC filter addr set to 0:0:0:0:0:0 */
bus_space_write_4(t, h, CAS_MAC_ADDR_FILTER0, 0);
bus_space_write_4(t, h, CAS_MAC_ADDR_FILTER1, 0);
bus_space_write_4(t, h, CAS_MAC_ADDR_FILTER2, 0);
cp = rxs->rxs_kva + off * 256 + ETHER_ALIGN;
m = m_devget(cp, len, 0, ifp);
if (word[0] & CAS_RC0_RELEASE_HDR)
cas_add_rxbuf(sc, idx);
if (m != NULL) {
/*
* Pass this up to any BPF listeners, but only
* pass it up the stack if its for us.
*/
m->m_pkthdr.csum_flags = 0;
if_percpuq_enqueue(ifp->if_percpuq, m);
} else
if_statinc(ifp, if_ierrors);
}
len = CAS_RC0_DATA_LEN(word[0]);
if (len > 0) {
off = CAS_RC0_DATA_OFF(word[0]);
idx = CAS_RC0_DATA_IDX(word[0]);
rxs = &sc->sc_rxsoft[idx];
DPRINTF(sc, ("data at idx %d, off %d, len %d\n",
idx, off, len));
/* XXX We should not be copying the packet here. */
cp = rxs->rxs_kva + off + ETHER_ALIGN;
m = m_devget(cp, len, 0, ifp);
if (word[0] & CAS_RC0_RELEASE_DATA)
cas_add_rxbuf(sc, idx);
if (m != NULL) {
/*
* Pass this up to any BPF listeners, but only
* pass it up the stack if its for us.
*/
m->m_pkthdr.csum_flags = 0;
if_percpuq_enqueue(ifp->if_percpuq, m);
} else
if_statinc(ifp, if_ierrors);
}
if (word[0] & CAS_RC0_SPLIT)
aprint_error_dev(sc->sc_dev, "split packet\n");
/*
* cas_add_rxbuf:
*
* Add a receive buffer to the indicated descriptor.
*/
int
cas_add_rxbuf(struct cas_softc *sc, int idx)
{
bus_space_tag_t t = sc->sc_memt;
bus_space_handle_t h = sc->sc_memh;
CAS_INIT_RXDESC(sc, sc->sc_rxdptr, idx);
if ((sc->sc_rxdptr % 4) == 0)
bus_space_write_4(t, h, CAS_RX_KICK, sc->sc_rxdptr);
if (++sc->sc_rxdptr == CAS_NRXDESC)
sc->sc_rxdptr = 0;
return (0);
}
int
cas_eint(struct cas_softc *sc, u_int status)
{
char bits[128];
if ((status & CAS_INTR_MIF) != 0) {
DPRINTF(sc, ("%s: link status changed\n",
device_xname(sc->sc_dev)));
return (1);
}
int
cas_pint(struct cas_softc *sc)
{
bus_space_tag_t t = sc->sc_memt;
bus_space_handle_t seb = sc->sc_memh;
uint32_t status;
status = bus_space_read_4(t, seb, CAS_MII_INTERRUP_STATUS);
status |= bus_space_read_4(t, seb, CAS_MII_INTERRUP_STATUS);
#ifdef CAS_DEBUG
if (status)
printf("%s: link status changed\n", device_xname(sc->sc_dev));
#endif
return (1);
}
int
cas_intr(void *v)
{
struct cas_softc *sc = (struct cas_softc *)v;
struct ifnet *ifp = &sc->sc_ethercom.ec_if;
bus_space_tag_t t = sc->sc_memt;
bus_space_handle_t seb = sc->sc_memh;
uint32_t status;
int r = 0;
#ifdef CAS_DEBUG
char bits[128];
#endif
if ((status & CAS_INTR_PCS) != 0)
r |= cas_pint(sc);
if ((status & (CAS_INTR_TX_TAG_ERR | CAS_INTR_RX_TAG_ERR |
CAS_INTR_RX_COMP_FULL | CAS_INTR_BERR)) != 0)
r |= cas_eint(sc, status);
if ((status & (CAS_INTR_TX_EMPTY | CAS_INTR_TX_INTME)) != 0)
r |= cas_tint(sc, status);
if ((status & (CAS_INTR_RX_DONE | CAS_INTR_RX_NOBUF)) != 0)
r |= cas_rint(sc);
/* We should eventually do more than just print out error stats. */
if (status & CAS_INTR_TX_MAC) {
int txstat = bus_space_read_4(t, seb, CAS_MAC_TX_STATUS);
#ifdef CAS_DEBUG
if (txstat & ~CAS_MAC_TX_XMIT_DONE)
printf("%s: MAC tx fault, status %x\n",
device_xname(sc->sc_dev), txstat);
#endif
if (txstat & (CAS_MAC_TX_UNDERRUN | CAS_MAC_TX_PKT_TOO_LONG))
cas_init(ifp);
}
if (status & CAS_INTR_RX_MAC) {
int rxstat = bus_space_read_4(t, seb, CAS_MAC_RX_STATUS);
#ifdef CAS_DEBUG
if (rxstat & ~CAS_MAC_RX_DONE)
printf("%s: MAC rx fault, status %x\n",
device_xname(sc->sc_dev), rxstat);
#endif
/*
* On some chip revisions CAS_MAC_RX_OVERFLOW happen often
* due to a silicon bug so handle them silently.
*/
if (rxstat & CAS_MAC_RX_OVERFLOW) {
if_statinc(ifp, if_ierrors);
cas_init(ifp);
}
#ifdef CAS_DEBUG
else if (rxstat & ~(CAS_MAC_RX_DONE | CAS_MAC_RX_FRAME_CNT))
printf("%s: MAC rx fault, status %x\n",
device_xname(sc->sc_dev), rxstat);
#endif
}
rnd_add_uint32(&sc->rnd_source, status);
return (r);
}
/*
* MII interface
*
* The Cassini MII interface supports at least three different operating modes:
*
* Bitbang mode is implemented using data, clock and output enable registers.
*
* Frame mode is implemented by loading a complete frame into the frame
* register and polling the valid bit for completion.
*
* Polling mode uses the frame register but completion is indicated by
* an interrupt.
*
*/
int
cas_mii_readreg(device_t self, int phy, int reg, uint16_t *val)
{
struct cas_softc *sc = device_private(self);
bus_space_tag_t t = sc->sc_memt;
bus_space_handle_t mif = sc->sc_memh;
int n;
uint32_t v;
void
cas_mii_statchg(struct ifnet *ifp)
{
struct cas_softc *sc = ifp->if_softc;
#ifdef CAS_DEBUG
int instance = IFM_INST(sc->sc_media.ifm_cur->ifm_media);
#endif
bus_space_tag_t t = sc->sc_memt;
bus_space_handle_t mac = sc->sc_memh;
uint32_t v;
#ifdef CAS_DEBUG
if (sc->sc_debug)
printf("cas_mii_statchg: status change: phy = %d\n",
sc->sc_phys[instance]);
#endif
/* Set tx full duplex options */
bus_space_write_4(t, mac, CAS_MAC_TX_CONFIG, 0);
delay(10000); /* reg must be cleared and delay before changing. */
v = CAS_MAC_TX_ENA_IPG0 | CAS_MAC_TX_NGU | CAS_MAC_TX_NGU_LIMIT |
CAS_MAC_TX_ENABLE;
if ((IFM_OPTIONS(sc->sc_mii.mii_media_active) & IFM_FDX) != 0) {
v |= CAS_MAC_TX_IGN_CARRIER | CAS_MAC_TX_IGN_COLLIS;
}
bus_space_write_4(t, mac, CAS_MAC_TX_CONFIG, v);
/* XIF Configuration */
v = CAS_MAC_XIF_TX_MII_ENA;
v |= CAS_MAC_XIF_LINK_LED;
/* MII needs echo disable if half duplex. */
if ((IFM_OPTIONS(sc->sc_mii.mii_media_active) & IFM_FDX) != 0)
/* turn on full duplex LED */
v |= CAS_MAC_XIF_FDPLX_LED;
else
/* half duplex -- disable echo */
v |= CAS_MAC_XIF_ECHO_DISABL;
switch (IFM_SUBTYPE(sc->sc_mii.mii_media_active)) {
case IFM_1000_T: /* Gigabit using GMII interface */
case IFM_1000_SX:
v |= CAS_MAC_XIF_GMII_MODE;
break;
default:
v &= ~CAS_MAC_XIF_GMII_MODE;
}
bus_space_write_4(t, mac, CAS_MAC_XIF_CONFIG, v);
}
int
cas_pcs_readreg(device_t self, int phy, int reg, uint16_t *val)
{
struct cas_softc *sc = device_private(self);
bus_space_tag_t t = sc->sc_memt;
bus_space_handle_t pcs = sc->sc_memh;
if ((ifp->if_flags & IFF_PROMISC) != 0)
goto update;
/*
* Set up multicast address filter by passing all multicast
* addresses through a crc generator, and then using the
* high order 8 bits as an index into the 256 bit logical
* address filter. The high order 4 bits selects the word,
* while the other 4 bits select the bit within the word
* (where bit 0 is the MSB).
*/
/* Clear hash table */
for (i = 0; i < 16; i++)
hash[i] = 0;
ETHER_LOCK(ec);
ETHER_FIRST_MULTI(step, ec, enm);
while (enm != NULL) {
if (memcmp(enm->enm_addrlo, enm->enm_addrhi, ETHER_ADDR_LEN)) {
/* XXX Use ETHER_F_ALLMULTI in future. */
ifp->if_flags |= IFF_ALLMULTI;
ETHER_UNLOCK(ec);
goto update;
}
/* Just want the 8 most significant bits. */
crc >>= 24;
/* Set the corresponding bit in the filter. */
hash[crc >> 4] |= 1 << (15 - (crc & 15));
ETHER_NEXT_MULTI(step, enm);
}
ETHER_UNLOCK(ec);
rxcfg |= CAS_MAC_RX_HASH_FILTER;
/* Now load the hash table into the chip (if we are using it) */
for (i = 0; i < 16; i++) {
bus_space_write_4(t, h,
CAS_MAC_HASH0 + i * (CAS_MAC_HASH1 - CAS_MAC_HASH0),
hash[i]);
}
update:
if ((ifp->if_flags & (IFF_PROMISC | IFF_ALLMULTI)) != 0) {
if (ifp->if_flags & IFF_PROMISC) {
rxcfg |= CAS_MAC_RX_PROMISCUOUS;
/* XXX Use ETHER_F_ALLMULTI in future. */
ifp->if_flags |= IFF_ALLMULTI;
} else
rxcfg |= CAS_MAC_RX_PROMISC_GRP;
}
bus_space_write_4(t, h, CAS_MAC_RX_CONFIG, rxcfg);
}