introduce an 'mcs7830' driver for Moschip based USB ethernet adapters, which was implemented based on the U-Boot Asix driver with additional information gathered from the Moschip Linux driver
Signed-off-by: Gerhard Sittig <g...@denx.de> --- the driver was tested with a Logilink and a Delock product each, both of which contain an MCS7830 chip rev C, by several times transferring a file of 50MB size via TFTP (on a board with a total of 64MB RAM) --- drivers/usb/eth/Makefile | 1 + drivers/usb/eth/mcs7830.c | 677 +++++++++++++++++++++++++++++++++++++++++++ drivers/usb/eth/usb_ether.c | 7 + include/usb_ether.h | 8 + 4 files changed, 693 insertions(+) create mode 100644 drivers/usb/eth/mcs7830.c diff --git a/drivers/usb/eth/Makefile b/drivers/usb/eth/Makefile index 03f54749f720..94551c4c0c9a 100644 --- a/drivers/usb/eth/Makefile +++ b/drivers/usb/eth/Makefile @@ -8,4 +8,5 @@ obj-$(CONFIG_USB_HOST_ETHER) += usb_ether.o ifdef CONFIG_USB_ETHER_ASIX obj-y += asix.o endif +obj-$(CONFIG_USB_ETHER_MCS7830) += mcs7830.o obj-$(CONFIG_USB_ETHER_SMSC95XX) += smsc95xx.o diff --git a/drivers/usb/eth/mcs7830.c b/drivers/usb/eth/mcs7830.c new file mode 100644 index 000000000000..60cacc752578 --- /dev/null +++ b/drivers/usb/eth/mcs7830.c @@ -0,0 +1,677 @@ +/* + * Copyright (c) 2013 Gerhard Sittig <g...@denx.de> + * based on the U-Boot Asix driver as well as information + * from the Linux Moschip driver + * + * SPDX-License-Identifier: GPL-2.0+ + */ + +/* + * MOSCHIP MCS7830 based (7730/7830/7832) USB 2.0 Ethernet Devices + */ + +#include <common.h> +#include <errno.h> +#include <linux/mii.h> +#include <malloc.h> +#include <usb.h> + +#include "usb_ether.h" + +/* global declarations {{{ */ + +#define MCS7830_BASE_NAME "mcs" + +#define USB_CTRL_SET_TIMEOUT 5000 +#define USB_CTRL_GET_TIMEOUT 5000 +#define USB_BULK_SEND_TIMEOUT 5000 +#define USB_BULK_RECV_TIMEOUT 5000 +#define PHY_CONNECT_TIMEOUT 5000 /* link status, connect timeout */ +#define PHY_CONNECT_TIMEOUT_RES 50 /* link status, resolution in msec */ + +#define MCS7830_RX_URB_SIZE 2048 + +#ifndef BIT +#define BIT(n) (1 << (n)) +#endif + +/* command opcodes */ +enum { + MCS7830_WR_BREQ = 0x0d, + MCS7830_RD_BREQ = 0x0e, +}; + +/* register offsets, field bitmasks and default values */ +enum { + REG_MULTICAST_HASH = 0x00, + REG_PHY_DATA = 0x0a, + REG_PHY_CMD1 = 0x0c, + PHY_CMD1_READ = BIT(6), + PHY_CMD1_WRITE = BIT(5), + PHY_CMD1_PHYADDR = BIT(0), + REG_PHY_CMD2 = 0x0d, + PHY_CMD2_PEND_FLAG_BIT = BIT(7), + PHY_CMD2_READY_FLAG_BIT = BIT(6), + REG_CONFIG = 0x0e, + CONFIG_CFG = BIT(7), + CONFIG_SPEED100 = BIT(6), + CONFIG_FDX_ENABLE = BIT(5), + CONFIG_RXENABLE = BIT(4), + CONFIG_TXENABLE = BIT(3), + CONFIG_SLEEPMODE = BIT(2), + CONFIG_ALLMULTICAST = BIT(1), + CONFIG_PROMISCUOUS = BIT(0), + REG_ETHER_ADDR = 0x0f, + REG_FRAME_DROP_COUNTER = 0x15, + REG_PAUSE_THRESHOLD = 0x16, + PAUSE_THRESHOLD_DEFAULT = 0, +}; + +/* trailing status byte after RX frame */ +enum { + STAT_RX_FRAME_CORRECT = BIT(5), + STAT_RX_LARGE_FRAME = BIT(4), + STAT_RX_CRC_ERROR = BIT(3), + STAT_RX_ALIGNMENT_ERROR = BIT(2), + STAT_RX_LENGTH_ERROR = BIT(1), + STAT_RX_SHORT_FRAME = BIT(0), +}; + +struct mcs7830_private { + uint8_t config; + uint8_t mchash[8]; +}; + +/* }}} global declarations */ +/* private helper routines {{{ */ + +static int mcs7830_read_reg(struct ueth_data *dev, uint8_t idx, + uint16_t size, void *data) +{ + int len; + ALLOC_CACHE_ALIGN_BUFFER(uint8_t, buf, size); + + debug("%s() idx=0x%04X sz=%d\n", __func__, idx, size); + + len = usb_control_msg(dev->pusb_dev, + usb_rcvctrlpipe(dev->pusb_dev, 0), + MCS7830_RD_BREQ, + USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE, + 0, idx, buf, size, + USB_CTRL_GET_TIMEOUT); + if (len == size) { + memcpy(data, buf, size); + return 0; + } + debug("%s() len=%d != sz=%d\n", __func__, len, size); + return -1; +} + +static int mcs7830_write_reg(struct ueth_data *dev, uint8_t idx, + uint16_t size, void *data) +{ + int len; + ALLOC_CACHE_ALIGN_BUFFER(uint8_t, buf, size); + + debug("%s() idx=0x%04X sz=%d\n", __func__, idx, size); + + memcpy(buf, data, size); + len = usb_control_msg(dev->pusb_dev, + usb_sndctrlpipe(dev->pusb_dev, 0), + MCS7830_WR_BREQ, + USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE, + 0, idx, buf, size, + USB_CTRL_SET_TIMEOUT); + if (len != size) + debug("%s() len=%d != sz=%d\n", __func__, len, size); + return (len == size) ? 0 : -1; +} + +static int mcs7830_read_phy(struct ueth_data *dev, uint8_t index) +{ + int rc; + int retry; + uint8_t cmd[2]; + uint16_t val; + + /* send the PHY read request */ + cmd[0] = PHY_CMD1_READ | PHY_CMD1_PHYADDR; + cmd[1] = PHY_CMD2_PEND_FLAG_BIT | (index & 0x1f); + rc = mcs7830_write_reg(dev, REG_PHY_CMD1, sizeof(cmd), cmd); + if (rc < 0) + return rc; + + /* wait for the response to become available (usually < 1ms) */ + retry = 10; + do { + rc = mcs7830_read_reg(dev, REG_PHY_CMD1, sizeof(cmd), cmd); + if (rc < 0) + return rc; + if (cmd[1] & PHY_CMD2_READY_FLAG_BIT) + break; + if (!retry) + return -EIO; + mdelay(1); + } while (--retry); + + /* fetch the response data */ + rc = mcs7830_read_reg(dev, REG_PHY_DATA, sizeof(val), &val); + if (rc < 0) + return rc; + rc = le16_to_cpu(val); + debug("%s(%s, %d) => 0x%04X\n", __func__, dev->eth_dev.name, index, rc); + return rc; +} + +static int mcs7830_write_phy(struct ueth_data *dev, uint8_t index, uint16_t val) +{ + int rc; + int retry; + uint8_t cmd[2]; + + debug("%s(%s, %d, 0x%04X)\n", __func__, dev->eth_dev.name, index, val); + + /* send the PHY data to get written */ + val = cpu_to_le16(val); + rc = mcs7830_write_reg(dev, REG_PHY_DATA, sizeof(val), &val); + if (rc < 0) + return rc; + + /* send the PHY write request */ + cmd[0] = PHY_CMD1_WRITE | PHY_CMD1_PHYADDR; + cmd[1] = PHY_CMD2_PEND_FLAG_BIT | (index & 0x1f); + rc = mcs7830_write_reg(dev, REG_PHY_CMD1, sizeof(cmd), cmd); + if (rc < 0) + return rc; + + /* wait for the command to get accepted */ + retry = 10; + do { + rc = mcs7830_read_reg(dev, REG_PHY_CMD1, sizeof(cmd), cmd); + if (rc < 0) + return rc; + if (cmd[1] & PHY_CMD2_READY_FLAG_BIT) + break; + if (!retry) + return -EIO; + mdelay(1); + } while (--retry); + + return 0; +} + +static int mcs7830_read_config(struct eth_device *eth) +{ + struct ueth_data *dev; + int rc; + uint8_t cfg; + + debug("%s()\n", __func__); + dev = eth->priv; + + rc = mcs7830_read_reg(dev, REG_CONFIG, sizeof(cfg), &cfg); + if (rc < 0) { + debug("reading config from adapter failed\n"); + return -1; + } + + debug("%s() reg=0x%02X [ %s %s %s %s %s %s %s %s ]\n", + __func__, cfg, + (cfg & CONFIG_CFG) ? "cfg" : "-", + (cfg & CONFIG_SPEED100) ? "100" : "-", + (cfg & CONFIG_FDX_ENABLE) ? "fdx" : "-", + (cfg & CONFIG_RXENABLE) ? "rx" : "-", + (cfg & CONFIG_TXENABLE) ? "tx" : "-", + (cfg & CONFIG_SLEEPMODE) ? "sleep" : "-", + (cfg & CONFIG_ALLMULTICAST) ? "multi" : "-", + (cfg & CONFIG_PROMISCUOUS) ? "promisc" : "-"); + return cfg; +} + +static int mcs7830_write_config(struct eth_device *eth) +{ + struct ueth_data *dev; + struct mcs7830_private *priv; + int rc; + + debug("%s()\n", __func__); + dev = eth->priv; + priv = dev->dev_priv; + + rc = mcs7830_write_reg(dev, REG_CONFIG, + sizeof(priv->config), &priv->config); + if (rc < 0) { + debug("writing config to adapter failed\n"); + return -1; + } + + return 0; +} + +static int mcs7830_write_mchash(struct eth_device *eth) +{ + struct ueth_data *dev; + struct mcs7830_private *priv; + int rc; + + debug("%s()\n", __func__); + dev = eth->priv; + priv = dev->dev_priv; + + rc = mcs7830_write_reg(dev, REG_MULTICAST_HASH, + sizeof(priv->mchash), &priv->mchash); + if (rc < 0) { + debug("writing multicast hash to adapter failed\n"); + return -1; + } + + return 0; +} + +static int mcs7830_set_autoneg(struct ueth_data *dev) +{ + int adv, flg; + int rc; + + debug("%s()\n", __func__); + + /* + * algorithm taken from the Linux driver, which took it from + * "the original mcs7830 version 1.4 driver": + * + * enable all media, reset BMCR, enable auto neg, restart + * auto neg while keeping the enable auto neg flag set + */ + + adv = ADVERTISE_PAUSE_CAP | ADVERTISE_ALL | ADVERTISE_CSMA; + rc = mcs7830_write_phy(dev, MII_ADVERTISE, adv); + + flg = 0; + if (!rc) + rc = mcs7830_write_phy(dev, MII_BMCR, flg); + + flg |= BMCR_ANENABLE; + if (!rc) + rc = mcs7830_write_phy(dev, MII_BMCR, flg); + + flg |= BMCR_ANRESTART; + if (!rc) + rc = mcs7830_write_phy(dev, MII_BMCR, flg); + + return rc; +} + +static int mcs7830_get_rev(struct ueth_data *dev) +{ + uint8_t buf[2]; + int rc; + int rev; + + /* register 22 is readable in rev C and higher */ + rc = mcs7830_read_reg(dev, REG_FRAME_DROP_COUNTER, sizeof(buf), buf); + if (rc < 0) + rev = 1; + else + rev = 2; + debug("%s() rc=%d, rev=%d\n", __func__, rc, rev); + return rev; +} + +static int mcs7830_apply_fixup(struct ueth_data *dev) +{ + int rev; + int i; + uint8_t thr; + + rev = mcs7830_get_rev(dev); + debug("%s() rev=%d\n", __func__, rev); + + /* + * rev C requires setting the pause threshold (the Linux driver + * is inconsistent, the implementation does it for "rev C + * exactly", the introductory comment says "rev C and above") + */ + if (rev == 2) { + debug("%s: applying rev C fixup\n", dev->eth_dev.name); + thr = PAUSE_THRESHOLD_DEFAULT; + for (i = 0; i < 2; i++) { + (void)mcs7830_write_reg(dev, REG_PAUSE_THRESHOLD, + sizeof(thr), &thr); + mdelay(1); + } + } + + return 0; +} + +static int mcs7830_basic_reset(struct ueth_data *dev) +{ + struct mcs7830_private *priv; + int rc; + + debug("%s()\n", __func__); + priv = dev->dev_priv; + + /* + * comment from the respective Linux driver, which + * unconditionally sets the ALLMULTICAST flag as well: + * should not be needed, but does not work otherwise + */ + priv->config = CONFIG_TXENABLE; + priv->config |= CONFIG_ALLMULTICAST; + + rc = mcs7830_set_autoneg(dev); + if (rc < 0) { + error("setting autoneg failed\n"); + return rc; + } + + rc = mcs7830_write_mchash(dev); + if (rc < 0) { + error("failed to set multicast hash\n"); + return rc; + } + + rc = mcs7830_write_config(dev); + if (rc < 0) { + error("failed to set configuration\n"); + return rc; + } + + rc = mcs7830_apply_fixup(dev); + if (rc < 0) { + error("fixup application failed\n"); + return rc; + } + + return 0; +} + +static int mcs7830_read_mac(struct eth_device *eth) +{ + struct ueth_data *dev; + int ret; + uint8_t buf[ETH_ALEN]; + + debug("%s()\n", __func__); + dev = eth->priv; + + ret = mcs7830_read_reg(dev, REG_ETHER_ADDR, ETH_ALEN, buf); + if (ret < 0) { + debug("reading MAC from adapter failed\n"); + return -1; + } + + memcpy(ð->enetaddr[0], buf, ETH_ALEN); + return 0; +} + +static int mcs7830_write_mac(struct eth_device *eth) +{ + struct ueth_data *dev; + int ret; + + debug("%s()\n", __func__); + dev = eth->priv; + + if (sizeof(eth->enetaddr) != ETH_ALEN) + return -1; + ret = mcs7830_write_reg(dev, REG_ETHER_ADDR, ETH_ALEN, eth->enetaddr); + if (ret < 0) { + debug("writing MAC to adapter failed\n"); + return -1; + } + return 0; +} + +/* }}} private helper routines */ +/* private callbacks init/send/recv/halt {{{ */ + +static int mcs7830_init(struct eth_device *eth, bd_t *bd) +{ + struct ueth_data *dev; + int timeout; + int have_link; + + debug("%s()\n", __func__); + dev = eth->priv; + + timeout = 0; + do { + have_link = mcs7830_read_phy(dev, MII_BMSR) & BMSR_LSTATUS; + if (!have_link) { + if (!timeout) + puts("Waiting for ethernet connection ... "); + udelay(PHY_CONNECT_TIMEOUT_RES * 1000); + timeout += PHY_CONNECT_TIMEOUT_RES; + } + } while (!have_link && timeout < PHY_CONNECT_TIMEOUT); + if (have_link) { + if (timeout) + puts("done.\n"); + return 0; + } else { + puts("unable to connect.\n"); + return -1; + } +} + +static int mcs7830_send(struct eth_device *eth, void *packet, int length) +{ + struct ueth_data *dev; + int rc; + int gotlen; + /* there is a status byte after the ethernet frame */ + ALLOC_CACHE_ALIGN_BUFFER(uint8_t, buf, PKTSIZE + sizeof(uint8_t)); + + dev = eth->priv; + + memcpy(buf, packet, length); + rc = usb_bulk_msg(dev->pusb_dev, + usb_sndbulkpipe(dev->pusb_dev, dev->ep_out), + &buf[0], length, &gotlen, + USB_BULK_SEND_TIMEOUT); + debug("%s() TX want len %d, got len %d, rc %d\n", + __func__, length, gotlen, rc); + return rc; +} + +static int mcs7830_recv(struct eth_device *eth) +{ + struct ueth_data *dev; + ALLOC_CACHE_ALIGN_BUFFER(uint8_t, buf, MCS7830_RX_URB_SIZE); + int rc, wantlen, gotlen; + uint8_t sts; + + debug("%s()\n", __func__); + dev = eth->priv; + + /* fetch input data from the adapter */ + wantlen = MCS7830_RX_URB_SIZE; + rc = usb_bulk_msg(dev->pusb_dev, + usb_rcvbulkpipe(dev->pusb_dev, dev->ep_in), + &buf[0], wantlen, &gotlen, + USB_BULK_RECV_TIMEOUT); + debug("%s() RX want len %d, got len %d, rc %d\n", + __func__, wantlen, gotlen, rc); + if (rc != 0) { + error("RX: failed to receive\n"); + return -1; + } + if (gotlen > wantlen) { + error("RX: got too many bytes (%d)\n", gotlen); + return -1; + } + + /* + * the bulk message that we received from USB contains exactly + * one ethernet frame and a trailing status byte + */ + if (gotlen < sizeof(sts)) + return -1; + gotlen -= sizeof(sts); + sts = buf[gotlen]; + + if (sts == STAT_RX_FRAME_CORRECT) { + debug("%s() got a frame, len=%d\n", __func__, gotlen); + NetReceive(buf, gotlen); + return 0; + } + + debug("RX: frame error (sts 0x%02X, %s %s %s %s %s)\n", + sts, + (sts & STAT_RX_LARGE_FRAME) ? "large" : "-", + (sts & STAT_RX_LENGTH_ERROR) ? "length" : "-", + (sts & STAT_RX_SHORT_FRAME) ? "short" : "-", + (sts & STAT_RX_CRC_ERROR) ? "crc" : "-", + (sts & STAT_RX_ALIGNMENT_ERROR) ? "align" : "-"); + return -1; +} + +static void mcs7830_halt(struct eth_device *eth) +{ + debug("%s()\n", __func__); +} + +/* }}} init/send/recv/halt */ +/* public routines probe/getinfo {{{ */ + +static int curr_eth_dev; /* index for name of next device detected */ + +void mcs7830_eth_before_probe(void) +{ + curr_eth_dev = 0; +} + +static const struct mcs7830_dongle { + uint16_t vendor; + uint16_t product; +} const mcs7830_dongles[] = { + { 0x9710, 0x7832, }, /* Moschip 7832 */ + { 0x9710, 0x7830, }, /* Moschip 7830 */ + { 0x9710, 0x7730, }, /* Moschip 7730 */ + { 0x0df6, 0x0021, }, /* Sitecom LN 30 */ + { /* sentinel */ }, +}; + +int mcs7830_eth_probe(struct usb_device *dev, unsigned int ifnum, + struct ueth_data *ss) +{ + struct usb_interface *iface; + struct usb_interface_descriptor *iface_desc; + int i; + struct mcs7830_private *priv; + int ep_in_found, ep_out_found, ep_intr_found; + + debug("%s()\n", __func__); + + /* iterate the list of supported dongles */ + iface = &dev->config.if_desc[ifnum]; + iface_desc = &iface->desc; + for (i = 0; mcs7830_dongles[i].vendor; i++) { + if (dev->descriptor.idVendor == mcs7830_dongles[i].vendor && + dev->descriptor.idProduct == mcs7830_dongles[i].product) + break; + } + if (!mcs7830_dongles[i].vendor) + return 0; + debug("detected USB ethernet device: %04X:%04X\n", + dev->descriptor.idVendor, dev->descriptor.idProduct); + + /* fill in driver private data */ + priv = calloc(1, sizeof(*priv)); + if (!priv) + return 0; + + /* fill in the ueth_data structure, attach private data */ + memset(ss, 0, sizeof(*ss)); + ss->ifnum = ifnum; + ss->pusb_dev = dev; + ss->subclass = iface_desc->bInterfaceSubClass; + ss->protocol = iface_desc->bInterfaceProtocol; + ss->dev_priv = priv; + + /* + * a minimum of three endpoints is expected: in (bulk), + * out (bulk), and interrupt; ignore all others + */ + ep_in_found = ep_out_found = ep_intr_found = 0; + for (i = 0; i < iface_desc->bNumEndpoints; i++) { + uint8_t eptype, epaddr; + bool is_input; + + eptype = iface->ep_desc[i].bmAttributes; + eptype &= USB_ENDPOINT_XFERTYPE_MASK; + + epaddr = iface->ep_desc[i].bEndpointAddress; + is_input = epaddr & USB_DIR_IN; + epaddr &= USB_ENDPOINT_NUMBER_MASK; + + if (eptype == USB_ENDPOINT_XFER_BULK) { + if (is_input && !ep_in_found) { + ss->ep_in = epaddr; + ep_in_found++; + } + if (!is_input && !ep_out_found) { + ss->ep_out = epaddr; + ep_out_found++; + } + } + + if (eptype == USB_ENDPOINT_XFER_INT) { + if (is_input && !ep_intr_found) { + ss->ep_int = epaddr; + ss->irqinterval = iface->ep_desc[i].bInterval; + ep_intr_found++; + } + } + } + debug("endpoints: in %d, out %d, intr %d\n", + ss->ep_in, ss->ep_out, ss->ep_int); + + /* apply basic sanity checks */ + if (usb_set_interface(dev, iface_desc->bInterfaceNumber, 0) || + !ss->ep_in || !ss->ep_out || !ss->ep_int) { + debug("device probe incomplete\n"); + return 0; + } + + dev->privptr = ss; + return 1; +} + +int mcs7830_eth_get_info(struct usb_device *dev, struct ueth_data *ss, + struct eth_device *eth) +{ + debug("%s()\n", __func__); + if (!eth) { + debug("%s: missing parameter.\n", __func__); + return 0; + } + + snprintf(eth->name, sizeof(eth->name), "%s%d", + MCS7830_BASE_NAME, curr_eth_dev++); + eth->init = mcs7830_init; + eth->send = mcs7830_send; + eth->recv = mcs7830_recv; + eth->halt = mcs7830_halt; + eth->write_hwaddr = mcs7830_write_mac; + eth->priv = ss; + + if (mcs7830_basic_reset(ss)) + return 0; + + (void)mcs7830_read_config; +#ifdef DEBUG + (void)mcs7830_read_config(eth); +#endif + + if (mcs7830_read_mac(eth)) + return 0; + debug("MAC %pM\n", eth->enetaddr); + + return 1; +} + +/* }}} public routines */ +/* vim:set foldmethod=marker: */ diff --git a/drivers/usb/eth/usb_ether.c b/drivers/usb/eth/usb_ether.c index 2c4126be3601..1dda54c2f116 100644 --- a/drivers/usb/eth/usb_ether.c +++ b/drivers/usb/eth/usb_ether.c @@ -30,6 +30,13 @@ static const struct usb_eth_prob_dev prob_dev[] = { .get_info = asix_eth_get_info, }, #endif +#ifdef CONFIG_USB_ETHER_MCS7830 + { + .before_probe = mcs7830_eth_before_probe, + .probe = mcs7830_eth_probe, + .get_info = mcs7830_eth_get_info, + }, +#endif #ifdef CONFIG_USB_ETHER_SMSC95XX { .before_probe = smsc95xx_eth_before_probe, diff --git a/include/usb_ether.h b/include/usb_ether.h index 678c9dff2524..ddc11fb69560 100644 --- a/include/usb_ether.h +++ b/include/usb_ether.h @@ -51,6 +51,14 @@ int asix_eth_get_info(struct usb_device *dev, struct ueth_data *ss, struct eth_device *eth); #endif +#ifdef CONFIG_USB_ETHER_MCS7830 +void mcs7830_eth_before_probe(void); +int mcs7830_eth_probe(struct usb_device *dev, unsigned int ifnum, + struct ueth_data *ss); +int mcs7830_eth_get_info(struct usb_device *dev, struct ueth_data *ss, + struct eth_device *eth); +#endif + #ifdef CONFIG_USB_ETHER_SMSC95XX void smsc95xx_eth_before_probe(void); int smsc95xx_eth_probe(struct usb_device *dev, unsigned int ifnum, -- 1.7.10.4 _______________________________________________ U-Boot mailing list U-Boot@lists.denx.de http://lists.denx.de/mailman/listinfo/u-boot