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 * @udev: 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 usb_device *udev, 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(udev,
107 usb_rcvctrlpipe(udev, 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 * @udev: 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 usb_device *udev, 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(udev,
138 usb_sndctrlpipe(udev, 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 * @udev: 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 usb_device *udev,
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(udev, REG_PHY_CMD, sizeof(cmd), cmd);
171 /* wait for the response to become available (usually < 1ms) */
174 rc = mcs7830_read_reg(udev, 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 * @udev: 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 usb_device *udev, uint8_t index)
197 /* issue the PHY read request and wait for its execution */
198 rc = mcs7830_phy_emit_wait(udev, PHY_CMD1_READ, index);
202 /* fetch the PHY data which was read */
203 rc = mcs7830_read_reg(udev, REG_PHY_DATA, sizeof(val), &val);
206 rc = le16_to_cpu(val);
207 debug("%s(%d) => 0x%04X\n", __func__, index, rc);
212 * mcs7830_write_phy() - write a PHY register of the network adapter
213 * @udev: 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 usb_device *udev, uint8_t index,
223 debug("%s(%d, 0x%04X)\n", __func__, index, val);
225 /* setup the PHY data which is to get written */
226 val = cpu_to_le16(val);
227 rc = mcs7830_write_reg(udev, REG_PHY_DATA, sizeof(val), &val);
231 /* issue the PHY write request and wait for its execution */
232 rc = mcs7830_phy_emit_wait(udev, PHY_CMD1_WRITE, index);
240 * mcs7830_write_config() - write to the network adapter's config register
241 * @udev: network device to write to
242 * @priv: private data
243 * Return: zero upon success, negative upon error
245 * the data which gets written is taken from the shadow config register
246 * within the device driver's private data
248 static int mcs7830_write_config(struct usb_device *udev,
249 struct mcs7830_private *priv)
253 debug("%s()\n", __func__);
255 rc = mcs7830_write_reg(udev, REG_CONFIG,
256 sizeof(priv->config), &priv->config);
258 debug("writing config to adapter failed\n");
266 * mcs7830_write_mchash() - write the network adapter's multicast filter
267 * @udev: network device to write to
268 * @priv: private data
269 * Return: zero upon success, negative upon error
271 * the data which gets written is taken from the shadow multicast hashes
272 * within the device driver's private data
274 static int mcs7830_write_mchash(struct usb_device *udev,
275 struct mcs7830_private *priv)
279 debug("%s()\n", __func__);
281 rc = mcs7830_write_reg(udev, REG_MULTICAST_HASH,
282 sizeof(priv->mchash), &priv->mchash);
284 debug("writing multicast hash to adapter failed\n");
292 * mcs7830_set_autoneg() - setup and trigger ethernet link autonegotiation
293 * @udev: network device to run link negotiation on
294 * Return: zero upon success, negative upon error
296 * the routine advertises available media and starts autonegotiation
298 static int mcs7830_set_autoneg(struct usb_device *udev)
303 debug("%s()\n", __func__);
306 * algorithm taken from the Linux driver, which took it from
307 * "the original mcs7830 version 1.4 driver":
309 * enable all media, reset BMCR, enable auto neg, restart
310 * auto neg while keeping the enable auto neg flag set
313 adv = ADVERTISE_PAUSE_CAP | ADVERTISE_ALL | ADVERTISE_CSMA;
314 rc = mcs7830_write_phy(udev, MII_ADVERTISE, adv);
318 rc = mcs7830_write_phy(udev, MII_BMCR, flg);
320 flg |= BMCR_ANENABLE;
322 rc = mcs7830_write_phy(udev, MII_BMCR, flg);
324 flg |= BMCR_ANRESTART;
326 rc = mcs7830_write_phy(udev, MII_BMCR, flg);
332 * mcs7830_get_rev() - identify a network adapter's chip revision
333 * @udev: network device to identify
334 * Return: non-negative number, reflecting the revision number
336 * currently, only "rev C and higher" and "below rev C" are needed, so
337 * the return value is #1 for "below rev C", and #2 for "rev C and above"
339 static int mcs7830_get_rev(struct usb_device *udev)
345 /* register 22 is readable in rev C and higher */
346 rc = mcs7830_read_reg(udev, REG_FRAME_DROP_COUNTER, sizeof(buf), buf);
351 debug("%s() rc=%d, rev=%d\n", __func__, rc, rev);
356 * mcs7830_apply_fixup() - identify an adapter and potentially apply fixups
357 * @udev: network device to identify and apply fixups to
358 * Return: zero upon success (no errors emitted from here)
360 * this routine identifies the network adapter's chip revision, and applies
361 * fixups for known issues
363 static int mcs7830_apply_fixup(struct usb_device *udev)
369 rev = mcs7830_get_rev(udev);
370 debug("%s() rev=%d\n", __func__, rev);
373 * rev C requires setting the pause threshold (the Linux driver
374 * is inconsistent, the implementation does it for "rev C
375 * exactly", the introductory comment says "rev C and above")
378 debug("%s: applying rev C fixup\n", __func__);
379 thr = PAUSE_THRESHOLD_DEFAULT;
380 for (i = 0; i < 2; i++) {
381 (void)mcs7830_write_reg(udev, REG_PAUSE_THRESHOLD,
391 * mcs7830_basic_reset() - bring the network adapter into a known first state
392 * @eth: network device to act upon
393 * Return: zero upon success, negative upon error
395 * this routine initializes the network adapter such that subsequent invocations
396 * of the interface callbacks can exchange ethernet frames; link negotiation is
397 * triggered from here already and continues in background
399 static int mcs7830_basic_reset(struct usb_device *udev,
400 struct mcs7830_private *priv)
404 debug("%s()\n", __func__);
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(udev);
416 error("setting autoneg failed\n");
420 rc = mcs7830_write_mchash(udev, priv);
422 error("failed to set multicast hash\n");
426 rc = mcs7830_write_config(udev, priv);
428 error("failed to set configuration\n");
432 rc = mcs7830_apply_fixup(udev);
434 error("fixup application failed\n");
442 * mcs7830_read_mac() - read an ethernet adapter's MAC address
443 * @udev: network device to read from
444 * @enetaddr: place to put ethernet MAC address
445 * Return: zero upon success, negative upon error
447 * this routine fetches the MAC address stored within the ethernet adapter,
448 * and stores it in the ethernet interface's data structure
450 static int mcs7830_read_mac(struct usb_device *udev, unsigned char enetaddr[])
453 uint8_t buf[ETH_ALEN];
455 debug("%s()\n", __func__);
457 rc = mcs7830_read_reg(udev, REG_ETHER_ADDR, ETH_ALEN, buf);
459 debug("reading MAC from adapter failed\n");
463 memcpy(enetaddr, buf, ETH_ALEN);
467 static int mcs7830_write_mac_common(struct usb_device *udev,
468 unsigned char enetaddr[])
472 debug("%s()\n", __func__);
474 rc = mcs7830_write_reg(udev, REG_ETHER_ADDR, ETH_ALEN, enetaddr);
476 debug("writing MAC to adapter failed\n");
482 static int mcs7830_init_common(struct usb_device *udev)
487 debug("%s()\n", __func__);
491 have_link = mcs7830_read_phy(udev, MII_BMSR) & BMSR_LSTATUS;
494 udelay(LINKSTATUS_TIMEOUT_RES * 1000);
495 timeout += LINKSTATUS_TIMEOUT_RES;
496 } while (timeout < LINKSTATUS_TIMEOUT);
498 debug("ethernet link is down\n");
504 static int mcs7830_send_common(struct ueth_data *ueth, void *packet,
507 struct usb_device *udev = ueth->pusb_dev;
510 /* there is a status byte after the ethernet frame */
511 ALLOC_CACHE_ALIGN_BUFFER(uint8_t, buf, PKTSIZE + sizeof(uint8_t));
513 memcpy(buf, packet, length);
514 rc = usb_bulk_msg(udev,
515 usb_sndbulkpipe(udev, ueth->ep_out),
516 &buf[0], length, &gotlen,
518 debug("%s() TX want len %d, got len %d, rc %d\n",
519 __func__, length, gotlen, rc);
523 static int mcs7830_recv_common(struct ueth_data *ueth, uint8_t *buf)
525 int rc, wantlen, gotlen;
528 debug("%s()\n", __func__);
530 /* fetch input data from the adapter */
531 wantlen = MCS7830_RX_URB_SIZE;
532 rc = usb_bulk_msg(ueth->pusb_dev,
533 usb_rcvbulkpipe(ueth->pusb_dev, ueth->ep_in),
534 &buf[0], wantlen, &gotlen,
536 debug("%s() RX want len %d, got len %d, rc %d\n",
537 __func__, wantlen, gotlen, rc);
539 error("RX: failed to receive\n");
542 if (gotlen > wantlen) {
543 error("RX: got too many bytes (%d)\n", gotlen);
548 * the bulk message that we received from USB contains exactly
549 * one ethernet frame and a trailing status byte
551 if (gotlen < sizeof(sts))
553 gotlen -= sizeof(sts);
556 if (sts == STAT_RX_FRAME_CORRECT) {
557 debug("%s() got a frame, len=%d\n", __func__, gotlen);
561 debug("RX: frame error (sts 0x%02X, %s %s %s %s %s)\n",
563 (sts & STAT_RX_LARGE_FRAME) ? "large" : "-",
564 (sts & STAT_RX_LENGTH_ERROR) ? "length" : "-",
565 (sts & STAT_RX_SHORT_FRAME) ? "short" : "-",
566 (sts & STAT_RX_CRC_ERROR) ? "crc" : "-",
567 (sts & STAT_RX_ALIGNMENT_ERROR) ? "align" : "-");
572 * mcs7830_init() - network interface's init callback
573 * @udev: network device to initialize
574 * @bd: board information
575 * Return: zero upon success, negative upon error
577 * after initial setup during probe() and get_info(), this init() callback
578 * ensures that the link is up and subsequent send() and recv() calls can
579 * exchange ethernet frames
581 static int mcs7830_init(struct eth_device *eth, bd_t *bd)
583 struct ueth_data *dev = eth->priv;
585 return mcs7830_init_common(dev->pusb_dev);
589 * mcs7830_send() - network interface's send callback
590 * @eth: network device to send the frame from
591 * @packet: ethernet frame content
592 * @length: ethernet frame length
593 * Return: zero upon success, negative upon error
595 * this routine send an ethernet frame out of the network interface
597 static int mcs7830_send(struct eth_device *eth, void *packet, int length)
599 struct ueth_data *dev = eth->priv;
601 return mcs7830_send_common(dev, packet, length);
605 * mcs7830_recv() - network interface's recv callback
606 * @eth: network device to receive frames from
607 * Return: zero upon success, negative upon error
609 * this routine checks for available ethernet frames that the network
610 * interface might have received, and notifies the network stack
612 static int mcs7830_recv(struct eth_device *eth)
614 ALLOC_CACHE_ALIGN_BUFFER(uint8_t, buf, MCS7830_RX_URB_SIZE);
615 struct ueth_data *ueth = eth->priv;
618 len = mcs7830_recv_common(ueth, buf);
620 net_process_received_packet(buf, len);
626 * mcs7830_halt() - network interface's halt callback
627 * @eth: network device to cease operation of
630 * this routine is supposed to undo the effect of previous initialization and
631 * ethernet frames exchange; in this implementation it's a NOP
633 static void mcs7830_halt(struct eth_device *eth)
635 debug("%s()\n", __func__);
639 * mcs7830_write_mac() - write an ethernet adapter's MAC address
640 * @eth: network device to write to
641 * Return: zero upon success, negative upon error
643 * this routine takes the MAC address from the ethernet interface's data
644 * structure, and writes it into the ethernet adapter such that subsequent
645 * exchange of ethernet frames uses this address
647 static int mcs7830_write_mac(struct eth_device *eth)
649 struct ueth_data *ueth = eth->priv;
651 return mcs7830_write_mac_common(ueth->pusb_dev, eth->enetaddr);
655 * mcs7830_iface_idx - index of detected network interfaces
657 * this counter keeps track of identified supported interfaces,
658 * to assign unique names as more interfaces are found
660 static int mcs7830_iface_idx;
663 * mcs7830_eth_before_probe() - network driver's before_probe callback
666 * this routine initializes driver's internal data in preparation of
667 * subsequent probe callbacks
669 void mcs7830_eth_before_probe(void)
671 mcs7830_iface_idx = 0;
675 * struct mcs7830_dongle - description of a supported Moschip ethernet dongle
676 * @vendor: 16bit USB vendor identification
677 * @product: 16bit USB product identification
679 * this structure describes a supported USB ethernet dongle by means of the
680 * vendor and product codes found during USB enumeration; no flags are held
681 * here since all supported dongles have identical behaviour, and required
682 * fixups get determined at runtime, such that no manual configuration is
685 struct mcs7830_dongle {
691 * mcs7830_dongles - the list of supported Moschip based USB ethernet dongles
693 static const struct mcs7830_dongle mcs7830_dongles[] = {
694 { 0x9710, 0x7832, }, /* Moschip 7832 */
695 { 0x9710, 0x7830, }, /* Moschip 7830 */
696 { 0x9710, 0x7730, }, /* Moschip 7730 */
697 { 0x0df6, 0x0021, }, /* Sitecom LN 30 */
701 * mcs7830_eth_probe() - network driver's probe callback
702 * @dev: detected USB device to check
703 * @ifnum: detected USB interface to check
704 * @ss: USB ethernet data structure to fill in upon match
705 * Return: #1 upon match, #0 upon mismatch or error
707 * this routine checks whether the found USB device is supported by
708 * this ethernet driver, and upon match fills in the USB ethernet
709 * data structure which later is passed to the get_info callback
711 int mcs7830_eth_probe(struct usb_device *dev, unsigned int ifnum,
712 struct ueth_data *ss)
714 struct usb_interface *iface;
715 struct usb_interface_descriptor *iface_desc;
717 struct mcs7830_private *priv;
718 int ep_in_found, ep_out_found, ep_intr_found;
720 debug("%s()\n", __func__);
722 /* iterate the list of supported dongles */
723 iface = &dev->config.if_desc[ifnum];
724 iface_desc = &iface->desc;
725 for (i = 0; i < ARRAY_SIZE(mcs7830_dongles); i++) {
726 if (dev->descriptor.idVendor == mcs7830_dongles[i].vendor &&
727 dev->descriptor.idProduct == mcs7830_dongles[i].product)
730 if (i == ARRAY_SIZE(mcs7830_dongles))
732 debug("detected USB ethernet device: %04X:%04X\n",
733 dev->descriptor.idVendor, dev->descriptor.idProduct);
735 /* fill in driver private data */
736 priv = calloc(1, sizeof(*priv));
740 /* fill in the ueth_data structure, attach private data */
741 memset(ss, 0, sizeof(*ss));
744 ss->subclass = iface_desc->bInterfaceSubClass;
745 ss->protocol = iface_desc->bInterfaceProtocol;
749 * a minimum of three endpoints is expected: in (bulk),
750 * out (bulk), and interrupt; ignore all others
752 ep_in_found = ep_out_found = ep_intr_found = 0;
753 for (i = 0; i < iface_desc->bNumEndpoints; i++) {
754 uint8_t eptype, epaddr;
757 eptype = iface->ep_desc[i].bmAttributes;
758 eptype &= USB_ENDPOINT_XFERTYPE_MASK;
760 epaddr = iface->ep_desc[i].bEndpointAddress;
761 is_input = epaddr & USB_DIR_IN;
762 epaddr &= USB_ENDPOINT_NUMBER_MASK;
764 if (eptype == USB_ENDPOINT_XFER_BULK) {
765 if (is_input && !ep_in_found) {
769 if (!is_input && !ep_out_found) {
775 if (eptype == USB_ENDPOINT_XFER_INT) {
776 if (is_input && !ep_intr_found) {
778 ss->irqinterval = iface->ep_desc[i].bInterval;
783 debug("endpoints: in %d, out %d, intr %d\n",
784 ss->ep_in, ss->ep_out, ss->ep_int);
786 /* apply basic sanity checks */
787 if (usb_set_interface(dev, iface_desc->bInterfaceNumber, 0) ||
788 !ss->ep_in || !ss->ep_out || !ss->ep_int) {
789 debug("device probe incomplete\n");
798 * mcs7830_eth_get_info() - network driver's get_info callback
799 * @dev: detected USB device
800 * @ss: USB ethernet data structure filled in at probe()
801 * @eth: ethernet interface data structure to fill in
802 * Return: #1 upon success, #0 upon error
804 * this routine registers the mandatory init(), send(), recv(), and
805 * halt() callbacks with the ethernet interface, can register the
806 * optional write_hwaddr() callback with the ethernet interface,
807 * and initiates configuration of the interface such that subsequent
808 * calls to those callbacks results in network communication
810 int mcs7830_eth_get_info(struct usb_device *dev, struct ueth_data *ss,
811 struct eth_device *eth)
813 debug("%s()\n", __func__);
815 debug("%s: missing parameter.\n", __func__);
819 snprintf(eth->name, sizeof(eth->name), "%s%d",
820 MCS7830_BASE_NAME, mcs7830_iface_idx++);
821 eth->init = mcs7830_init;
822 eth->send = mcs7830_send;
823 eth->recv = mcs7830_recv;
824 eth->halt = mcs7830_halt;
825 eth->write_hwaddr = mcs7830_write_mac;
828 if (mcs7830_basic_reset(ss->pusb_dev, ss->dev_priv))
831 if (mcs7830_read_mac(ss->pusb_dev, eth->enetaddr))
833 debug("MAC %pM\n", eth->enetaddr);