2 * Copyright (c) 2013 Gerhard Sittig <gsi@denx.de>
3 * based on the U-Boot Asix driver as well as information
4 * from the Linux Moschip driver
6 * SPDX-License-Identifier: GPL-2.0+
10 * MOSCHIP MCS7830 based (7730/7830/7832) USB 2.0 Ethernet Devices
15 #include <linux/mii.h>
20 #include "usb_ether.h"
22 #define MCS7830_BASE_NAME "mcs"
24 #define USBCALL_TIMEOUT 1000
25 #define LINKSTATUS_TIMEOUT 5000 /* link status, connect timeout */
26 #define LINKSTATUS_TIMEOUT_RES 50 /* link status, resolution in msec */
28 #define MCS7830_RX_URB_SIZE 2048
31 #define MCS7830_WR_BREQ 0x0d
32 #define MCS7830_RD_BREQ 0x0e
34 /* register layout, numerical offset specs for USB API calls */
36 uint8_t multicast_hashes[8];
37 uint8_t packet_gap[2];
39 uint8_t phy_command[2];
40 uint8_t configuration;
41 uint8_t ether_address[6];
42 uint8_t frame_drop_count;
43 uint8_t pause_threshold;
45 #define REG_MULTICAST_HASH offsetof(struct mcs7830_regs, multicast_hashes)
46 #define REG_PHY_DATA offsetof(struct mcs7830_regs, phy_data)
47 #define REG_PHY_CMD offsetof(struct mcs7830_regs, phy_command)
48 #define REG_CONFIG offsetof(struct mcs7830_regs, configuration)
49 #define REG_ETHER_ADDR offsetof(struct mcs7830_regs, ether_address)
50 #define REG_FRAME_DROP_COUNTER offsetof(struct mcs7830_regs, frame_drop_count)
51 #define REG_PAUSE_THRESHOLD offsetof(struct mcs7830_regs, pause_threshold)
53 /* bit masks and default values for the above registers */
54 #define PHY_CMD1_READ 0x40
55 #define PHY_CMD1_WRITE 0x20
56 #define PHY_CMD1_PHYADDR 0x01
58 #define PHY_CMD2_PEND 0x80
59 #define PHY_CMD2_READY 0x40
62 #define CONF_SPEED100 0x40
63 #define CONF_FDX_ENABLE 0x20
64 #define CONF_RXENABLE 0x10
65 #define CONF_TXENABLE 0x08
66 #define CONF_SLEEPMODE 0x04
67 #define CONF_ALLMULTICAST 0x02
68 #define CONF_PROMISCUOUS 0x01
70 #define PAUSE_THRESHOLD_DEFAULT 0
72 /* bit masks for the status byte which follows received ethernet frames */
73 #define STAT_RX_FRAME_CORRECT 0x20
74 #define STAT_RX_LARGE_FRAME 0x10
75 #define STAT_RX_CRC_ERROR 0x08
76 #define STAT_RX_ALIGNMENT_ERROR 0x04
77 #define STAT_RX_LENGTH_ERROR 0x02
78 #define STAT_RX_SHORT_FRAME 0x01
81 * struct mcs7830_private - private driver data for an individual adapter
82 * @config: shadow for the network adapter's configuration register
83 * @mchash: shadow for the network adapter's multicast hash registers
85 struct mcs7830_private {
91 * mcs7830_read_reg() - read a register of the network adapter
92 * @dev: network device to read from
93 * @idx: index of the register to start reading from
94 * @size: number of bytes to read
95 * @data: buffer to read into
96 * Return: zero upon success, negative upon error
98 static int mcs7830_read_reg(struct ueth_data *dev, uint8_t idx,
99 uint16_t size, void *data)
102 ALLOC_CACHE_ALIGN_BUFFER(uint8_t, buf, size);
104 debug("%s() idx=0x%04X sz=%d\n", __func__, idx, size);
106 len = usb_control_msg(dev->pusb_dev,
107 usb_rcvctrlpipe(dev->pusb_dev, 0),
109 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
113 debug("%s() len=%d != sz=%d\n", __func__, len, size);
116 memcpy(data, buf, size);
121 * mcs7830_write_reg() - write a register of the network adapter
122 * @dev: network device to write to
123 * @idx: index of the register to start writing to
124 * @size: number of bytes to write
125 * @data: buffer holding the data to write
126 * Return: zero upon success, negative upon error
128 static int mcs7830_write_reg(struct ueth_data *dev, uint8_t idx,
129 uint16_t size, void *data)
132 ALLOC_CACHE_ALIGN_BUFFER(uint8_t, buf, size);
134 debug("%s() idx=0x%04X sz=%d\n", __func__, idx, size);
136 memcpy(buf, data, size);
137 len = usb_control_msg(dev->pusb_dev,
138 usb_sndctrlpipe(dev->pusb_dev, 0),
140 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
144 debug("%s() len=%d != sz=%d\n", __func__, len, size);
151 * mcs7830_phy_emit_wait() - emit PHY read/write access, wait for its execution
152 * @dev: network device to talk to
153 * @rwflag: PHY_CMD1_READ or PHY_CMD1_WRITE opcode
154 * @index: number of the PHY register to read or write
155 * Return: zero upon success, negative upon error
157 static int mcs7830_phy_emit_wait(struct ueth_data *dev,
158 uint8_t rwflag, uint8_t index)
164 /* send the PHY read/write request */
165 cmd[0] = rwflag | PHY_CMD1_PHYADDR;
166 cmd[1] = PHY_CMD2_PEND | (index & 0x1f);
167 rc = mcs7830_write_reg(dev, REG_PHY_CMD, sizeof(cmd), cmd);
171 /* wait for the response to become available (usually < 1ms) */
174 rc = mcs7830_read_reg(dev, REG_PHY_CMD, sizeof(cmd), cmd);
177 if (cmd[1] & PHY_CMD2_READY)
187 * mcs7830_read_phy() - read a PHY register of the network adapter
188 * @dev: network device to read from
189 * @index: index of the PHY register to read from
190 * Return: non-negative 16bit register content, negative upon error
192 static int mcs7830_read_phy(struct ueth_data *dev, uint8_t index)
197 /* issue the PHY read request and wait for its execution */
198 rc = mcs7830_phy_emit_wait(dev, PHY_CMD1_READ, index);
202 /* fetch the PHY data which was read */
203 rc = mcs7830_read_reg(dev, REG_PHY_DATA, sizeof(val), &val);
206 rc = le16_to_cpu(val);
207 debug("%s(%s, %d) => 0x%04X\n", __func__, dev->eth_dev.name, index, rc);
212 * mcs7830_write_phy() - write a PHY register of the network adapter
213 * @dev: network device to write to
214 * @index: index of the PHY register to write to
215 * @val: value to write to the PHY register
216 * Return: zero upon success, negative upon error
218 static int mcs7830_write_phy(struct ueth_data *dev, uint8_t index, uint16_t val)
222 debug("%s(%s, %d, 0x%04X)\n", __func__, dev->eth_dev.name, index, val);
224 /* setup the PHY data which is to get written */
225 val = cpu_to_le16(val);
226 rc = mcs7830_write_reg(dev, REG_PHY_DATA, sizeof(val), &val);
230 /* issue the PHY write request and wait for its execution */
231 rc = mcs7830_phy_emit_wait(dev, PHY_CMD1_WRITE, index);
239 * mcs7830_write_config() - write to the network adapter's config register
240 * @eth: network device to write to
241 * Return: zero upon success, negative upon error
243 * the data which gets written is taken from the shadow config register
244 * within the device driver's private data
246 static int mcs7830_write_config(struct ueth_data *dev)
248 struct mcs7830_private *priv;
251 debug("%s()\n", __func__);
252 priv = dev->dev_priv;
254 rc = mcs7830_write_reg(dev, REG_CONFIG,
255 sizeof(priv->config), &priv->config);
257 debug("writing config to adapter failed\n");
265 * mcs7830_write_mchash() - write the network adapter's multicast filter
266 * @eth: network device to write to
267 * Return: zero upon success, negative upon error
269 * the data which gets written is taken from the shadow multicast hashes
270 * within the device driver's private data
272 static int mcs7830_write_mchash(struct ueth_data *dev)
274 struct mcs7830_private *priv;
277 debug("%s()\n", __func__);
278 priv = dev->dev_priv;
280 rc = mcs7830_write_reg(dev, REG_MULTICAST_HASH,
281 sizeof(priv->mchash), &priv->mchash);
283 debug("writing multicast hash to adapter failed\n");
291 * mcs7830_set_autoneg() - setup and trigger ethernet link autonegotiation
292 * @eth: network device to run link negotiation on
293 * Return: zero upon success, negative upon error
295 * the routine advertises available media and starts autonegotiation
297 static int mcs7830_set_autoneg(struct ueth_data *dev)
302 debug("%s()\n", __func__);
305 * algorithm taken from the Linux driver, which took it from
306 * "the original mcs7830 version 1.4 driver":
308 * enable all media, reset BMCR, enable auto neg, restart
309 * auto neg while keeping the enable auto neg flag set
312 adv = ADVERTISE_PAUSE_CAP | ADVERTISE_ALL | ADVERTISE_CSMA;
313 rc = mcs7830_write_phy(dev, MII_ADVERTISE, adv);
317 rc = mcs7830_write_phy(dev, MII_BMCR, flg);
319 flg |= BMCR_ANENABLE;
321 rc = mcs7830_write_phy(dev, MII_BMCR, flg);
323 flg |= BMCR_ANRESTART;
325 rc = mcs7830_write_phy(dev, MII_BMCR, flg);
331 * mcs7830_get_rev() - identify a network adapter's chip revision
332 * @eth: network device to identify
333 * Return: non-negative number, reflecting the revision number
335 * currently, only "rev C and higher" and "below rev C" are needed, so
336 * the return value is #1 for "below rev C", and #2 for "rev C and above"
338 static int mcs7830_get_rev(struct ueth_data *dev)
344 /* register 22 is readable in rev C and higher */
345 rc = mcs7830_read_reg(dev, REG_FRAME_DROP_COUNTER, sizeof(buf), buf);
350 debug("%s() rc=%d, rev=%d\n", __func__, rc, rev);
355 * mcs7830_apply_fixup() - identify an adapter and potentially apply fixups
356 * @eth: network device to identify and apply fixups to
357 * Return: zero upon success (no errors emitted from here)
359 * this routine identifies the network adapter's chip revision, and applies
360 * fixups for known issues
362 static int mcs7830_apply_fixup(struct ueth_data *dev)
368 rev = mcs7830_get_rev(dev);
369 debug("%s() rev=%d\n", __func__, rev);
372 * rev C requires setting the pause threshold (the Linux driver
373 * is inconsistent, the implementation does it for "rev C
374 * exactly", the introductory comment says "rev C and above")
377 debug("%s: applying rev C fixup\n", dev->eth_dev.name);
378 thr = PAUSE_THRESHOLD_DEFAULT;
379 for (i = 0; i < 2; i++) {
380 (void)mcs7830_write_reg(dev, REG_PAUSE_THRESHOLD,
390 * mcs7830_basic_reset() - bring the network adapter into a known first state
391 * @eth: network device to act upon
392 * Return: zero upon success, negative upon error
394 * this routine initializes the network adapter such that subsequent invocations
395 * of the interface callbacks can exchange ethernet frames; link negotiation is
396 * triggered from here already and continues in background
398 static int mcs7830_basic_reset(struct ueth_data *dev)
400 struct mcs7830_private *priv;
403 debug("%s()\n", __func__);
404 priv = dev->dev_priv;
407 * comment from the respective Linux driver, which
408 * unconditionally sets the ALLMULTICAST flag as well:
409 * should not be needed, but does not work otherwise
411 priv->config = CONF_TXENABLE;
412 priv->config |= CONF_ALLMULTICAST;
414 rc = mcs7830_set_autoneg(dev);
416 error("setting autoneg failed\n");
420 rc = mcs7830_write_mchash(dev);
422 error("failed to set multicast hash\n");
426 rc = mcs7830_write_config(dev);
428 error("failed to set configuration\n");
432 rc = mcs7830_apply_fixup(dev);
434 error("fixup application failed\n");
442 * mcs7830_read_mac() - read an ethernet adapter's MAC address
443 * @eth: network device to read from
444 * Return: zero upon success, negative upon error
446 * this routine fetches the MAC address stored within the ethernet adapter,
447 * and stores it in the ethernet interface's data structure
449 static int mcs7830_read_mac(struct eth_device *eth)
451 struct ueth_data *dev;
453 uint8_t buf[ETH_ALEN];
455 debug("%s()\n", __func__);
458 rc = mcs7830_read_reg(dev, REG_ETHER_ADDR, ETH_ALEN, buf);
460 debug("reading MAC from adapter failed\n");
464 memcpy(ð->enetaddr[0], buf, ETH_ALEN);
469 * mcs7830_write_mac() - write an ethernet adapter's MAC address
470 * @eth: network device to write to
471 * Return: zero upon success, negative upon error
473 * this routine takes the MAC address from the ethernet interface's data
474 * structure, and writes it into the ethernet adapter such that subsequent
475 * exchange of ethernet frames uses this address
477 static int mcs7830_write_mac(struct eth_device *eth)
479 struct ueth_data *dev;
482 debug("%s()\n", __func__);
485 if (sizeof(eth->enetaddr) != ETH_ALEN)
487 rc = mcs7830_write_reg(dev, REG_ETHER_ADDR, ETH_ALEN, eth->enetaddr);
489 debug("writing MAC to adapter failed\n");
496 * mcs7830_init() - network interface's init callback
497 * @eth: network device to initialize
498 * @bd: board information
499 * Return: zero upon success, negative upon error
501 * after initial setup during probe() and get_info(), this init() callback
502 * ensures that the link is up and subsequent send() and recv() calls can
503 * exchange ethernet frames
505 static int mcs7830_init(struct eth_device *eth, bd_t *bd)
507 struct ueth_data *dev;
511 debug("%s()\n", __func__);
516 have_link = mcs7830_read_phy(dev, MII_BMSR) & BMSR_LSTATUS;
519 udelay(LINKSTATUS_TIMEOUT_RES * 1000);
520 timeout += LINKSTATUS_TIMEOUT_RES;
521 } while (timeout < LINKSTATUS_TIMEOUT);
523 debug("ethernet link is down\n");
530 * mcs7830_send() - network interface's send callback
531 * @eth: network device to send the frame from
532 * @packet: ethernet frame content
533 * @length: ethernet frame length
534 * Return: zero upon success, negative upon error
536 * this routine send an ethernet frame out of the network interface
538 static int mcs7830_send(struct eth_device *eth, void *packet, int length)
540 struct ueth_data *dev;
543 /* there is a status byte after the ethernet frame */
544 ALLOC_CACHE_ALIGN_BUFFER(uint8_t, buf, PKTSIZE + sizeof(uint8_t));
548 memcpy(buf, packet, length);
549 rc = usb_bulk_msg(dev->pusb_dev,
550 usb_sndbulkpipe(dev->pusb_dev, dev->ep_out),
551 &buf[0], length, &gotlen,
553 debug("%s() TX want len %d, got len %d, rc %d\n",
554 __func__, length, gotlen, rc);
559 * mcs7830_recv() - network interface's recv callback
560 * @eth: network device to receive frames from
561 * Return: zero upon success, negative upon error
563 * this routine checks for available ethernet frames that the network
564 * interface might have received, and notifies the network stack
566 static int mcs7830_recv(struct eth_device *eth)
568 struct ueth_data *dev;
569 ALLOC_CACHE_ALIGN_BUFFER(uint8_t, buf, MCS7830_RX_URB_SIZE);
570 int rc, wantlen, gotlen;
573 debug("%s()\n", __func__);
576 /* fetch input data from the adapter */
577 wantlen = MCS7830_RX_URB_SIZE;
578 rc = usb_bulk_msg(dev->pusb_dev,
579 usb_rcvbulkpipe(dev->pusb_dev, dev->ep_in),
580 &buf[0], wantlen, &gotlen,
582 debug("%s() RX want len %d, got len %d, rc %d\n",
583 __func__, wantlen, gotlen, rc);
585 error("RX: failed to receive\n");
588 if (gotlen > wantlen) {
589 error("RX: got too many bytes (%d)\n", gotlen);
594 * the bulk message that we received from USB contains exactly
595 * one ethernet frame and a trailing status byte
597 if (gotlen < sizeof(sts))
599 gotlen -= sizeof(sts);
602 if (sts == STAT_RX_FRAME_CORRECT) {
603 debug("%s() got a frame, len=%d\n", __func__, gotlen);
604 net_process_received_packet(buf, gotlen);
608 debug("RX: frame error (sts 0x%02X, %s %s %s %s %s)\n",
610 (sts & STAT_RX_LARGE_FRAME) ? "large" : "-",
611 (sts & STAT_RX_LENGTH_ERROR) ? "length" : "-",
612 (sts & STAT_RX_SHORT_FRAME) ? "short" : "-",
613 (sts & STAT_RX_CRC_ERROR) ? "crc" : "-",
614 (sts & STAT_RX_ALIGNMENT_ERROR) ? "align" : "-");
619 * mcs7830_halt() - network interface's halt callback
620 * @eth: network device to cease operation of
623 * this routine is supposed to undo the effect of previous initialization and
624 * ethernet frames exchange; in this implementation it's a NOP
626 static void mcs7830_halt(struct eth_device *eth)
628 debug("%s()\n", __func__);
632 * mcs7830_iface_idx - index of detected network interfaces
634 * this counter keeps track of identified supported interfaces,
635 * to assign unique names as more interfaces are found
637 static int mcs7830_iface_idx;
640 * mcs7830_eth_before_probe() - network driver's before_probe callback
643 * this routine initializes driver's internal data in preparation of
644 * subsequent probe callbacks
646 void mcs7830_eth_before_probe(void)
648 mcs7830_iface_idx = 0;
652 * struct mcs7830_dongle - description of a supported Moschip ethernet dongle
653 * @vendor: 16bit USB vendor identification
654 * @product: 16bit USB product identification
656 * this structure describes a supported USB ethernet dongle by means of the
657 * vendor and product codes found during USB enumeration; no flags are held
658 * here since all supported dongles have identical behaviour, and required
659 * fixups get determined at runtime, such that no manual configuration is
662 struct mcs7830_dongle {
668 * mcs7830_dongles - the list of supported Moschip based USB ethernet dongles
670 static const struct mcs7830_dongle mcs7830_dongles[] = {
671 { 0x9710, 0x7832, }, /* Moschip 7832 */
672 { 0x9710, 0x7830, }, /* Moschip 7830 */
673 { 0x9710, 0x7730, }, /* Moschip 7730 */
674 { 0x0df6, 0x0021, }, /* Sitecom LN 30 */
678 * mcs7830_eth_probe() - network driver's probe callback
679 * @dev: detected USB device to check
680 * @ifnum: detected USB interface to check
681 * @ss: USB ethernet data structure to fill in upon match
682 * Return: #1 upon match, #0 upon mismatch or error
684 * this routine checks whether the found USB device is supported by
685 * this ethernet driver, and upon match fills in the USB ethernet
686 * data structure which later is passed to the get_info callback
688 int mcs7830_eth_probe(struct usb_device *dev, unsigned int ifnum,
689 struct ueth_data *ss)
691 struct usb_interface *iface;
692 struct usb_interface_descriptor *iface_desc;
694 struct mcs7830_private *priv;
695 int ep_in_found, ep_out_found, ep_intr_found;
697 debug("%s()\n", __func__);
699 /* iterate the list of supported dongles */
700 iface = &dev->config.if_desc[ifnum];
701 iface_desc = &iface->desc;
702 for (i = 0; i < ARRAY_SIZE(mcs7830_dongles); i++) {
703 if (dev->descriptor.idVendor == mcs7830_dongles[i].vendor &&
704 dev->descriptor.idProduct == mcs7830_dongles[i].product)
707 if (i == ARRAY_SIZE(mcs7830_dongles))
709 debug("detected USB ethernet device: %04X:%04X\n",
710 dev->descriptor.idVendor, dev->descriptor.idProduct);
712 /* fill in driver private data */
713 priv = calloc(1, sizeof(*priv));
717 /* fill in the ueth_data structure, attach private data */
718 memset(ss, 0, sizeof(*ss));
721 ss->subclass = iface_desc->bInterfaceSubClass;
722 ss->protocol = iface_desc->bInterfaceProtocol;
726 * a minimum of three endpoints is expected: in (bulk),
727 * out (bulk), and interrupt; ignore all others
729 ep_in_found = ep_out_found = ep_intr_found = 0;
730 for (i = 0; i < iface_desc->bNumEndpoints; i++) {
731 uint8_t eptype, epaddr;
734 eptype = iface->ep_desc[i].bmAttributes;
735 eptype &= USB_ENDPOINT_XFERTYPE_MASK;
737 epaddr = iface->ep_desc[i].bEndpointAddress;
738 is_input = epaddr & USB_DIR_IN;
739 epaddr &= USB_ENDPOINT_NUMBER_MASK;
741 if (eptype == USB_ENDPOINT_XFER_BULK) {
742 if (is_input && !ep_in_found) {
746 if (!is_input && !ep_out_found) {
752 if (eptype == USB_ENDPOINT_XFER_INT) {
753 if (is_input && !ep_intr_found) {
755 ss->irqinterval = iface->ep_desc[i].bInterval;
760 debug("endpoints: in %d, out %d, intr %d\n",
761 ss->ep_in, ss->ep_out, ss->ep_int);
763 /* apply basic sanity checks */
764 if (usb_set_interface(dev, iface_desc->bInterfaceNumber, 0) ||
765 !ss->ep_in || !ss->ep_out || !ss->ep_int) {
766 debug("device probe incomplete\n");
775 * mcs7830_eth_get_info() - network driver's get_info callback
776 * @dev: detected USB device
777 * @ss: USB ethernet data structure filled in at probe()
778 * @eth: ethernet interface data structure to fill in
779 * Return: #1 upon success, #0 upon error
781 * this routine registers the mandatory init(), send(), recv(), and
782 * halt() callbacks with the ethernet interface, can register the
783 * optional write_hwaddr() callback with the ethernet interface,
784 * and initiates configuration of the interface such that subsequent
785 * calls to those callbacks results in network communication
787 int mcs7830_eth_get_info(struct usb_device *dev, struct ueth_data *ss,
788 struct eth_device *eth)
790 debug("%s()\n", __func__);
792 debug("%s: missing parameter.\n", __func__);
796 snprintf(eth->name, sizeof(eth->name), "%s%d",
797 MCS7830_BASE_NAME, mcs7830_iface_idx++);
798 eth->init = mcs7830_init;
799 eth->send = mcs7830_send;
800 eth->recv = mcs7830_recv;
801 eth->halt = mcs7830_halt;
802 eth->write_hwaddr = mcs7830_write_mac;
805 if (mcs7830_basic_reset(ss))
808 if (mcs7830_read_mac(eth))
810 debug("MAC %pM\n", eth->enetaddr);