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>
19 #include "usb_ether.h"
21 #define MCS7830_BASE_NAME "mcs"
23 #define USBCALL_TIMEOUT 1000
24 #define LINKSTATUS_TIMEOUT 5000 /* link status, connect timeout */
25 #define LINKSTATUS_TIMEOUT_RES 50 /* link status, resolution in msec */
27 #define MCS7830_RX_URB_SIZE 2048
30 #define MCS7830_WR_BREQ 0x0d
31 #define MCS7830_RD_BREQ 0x0e
33 /* register layout, numerical offset specs for USB API calls */
35 uint8_t multicast_hashes
[8];
36 uint8_t packet_gap
[2];
38 uint8_t phy_command
[2];
39 uint8_t configuration
;
40 uint8_t ether_address
[6];
41 uint8_t frame_drop_count
;
42 uint8_t pause_threshold
;
44 #define REG_MULTICAST_HASH offsetof(struct mcs7830_regs, multicast_hashes)
45 #define REG_PHY_DATA offsetof(struct mcs7830_regs, phy_data)
46 #define REG_PHY_CMD offsetof(struct mcs7830_regs, phy_command)
47 #define REG_CONFIG offsetof(struct mcs7830_regs, configuration)
48 #define REG_ETHER_ADDR offsetof(struct mcs7830_regs, ether_address)
49 #define REG_FRAME_DROP_COUNTER offsetof(struct mcs7830_regs, frame_drop_count)
50 #define REG_PAUSE_THRESHOLD offsetof(struct mcs7830_regs, pause_threshold)
52 /* bit masks and default values for the above registers */
53 #define PHY_CMD1_READ 0x40
54 #define PHY_CMD1_WRITE 0x20
55 #define PHY_CMD1_PHYADDR 0x01
57 #define PHY_CMD2_PEND 0x80
58 #define PHY_CMD2_READY 0x40
61 #define CONF_SPEED100 0x40
62 #define CONF_FDX_ENABLE 0x20
63 #define CONF_RXENABLE 0x10
64 #define CONF_TXENABLE 0x08
65 #define CONF_SLEEPMODE 0x04
66 #define CONF_ALLMULTICAST 0x02
67 #define CONF_PROMISCUOUS 0x01
69 #define PAUSE_THRESHOLD_DEFAULT 0
71 /* bit masks for the status byte which follows received ethernet frames */
72 #define STAT_RX_FRAME_CORRECT 0x20
73 #define STAT_RX_LARGE_FRAME 0x10
74 #define STAT_RX_CRC_ERROR 0x08
75 #define STAT_RX_ALIGNMENT_ERROR 0x04
76 #define STAT_RX_LENGTH_ERROR 0x02
77 #define STAT_RX_SHORT_FRAME 0x01
80 * struct mcs7830_private - private driver data for an individual adapter
81 * @config: shadow for the network adapter's configuration register
82 * @mchash: shadow for the network adapter's multicast hash registers
84 struct mcs7830_private
{
90 * mcs7830_read_reg() - read a register of the network adapter
91 * @dev: network device to read from
92 * @idx: index of the register to start reading from
93 * @size: number of bytes to read
94 * @data: buffer to read into
95 * Return: zero upon success, negative upon error
97 static int mcs7830_read_reg(struct ueth_data
*dev
, uint8_t idx
,
98 uint16_t size
, void *data
)
101 ALLOC_CACHE_ALIGN_BUFFER(uint8_t, buf
, size
);
103 debug("%s() idx=0x%04X sz=%d\n", __func__
, idx
, size
);
105 len
= usb_control_msg(dev
->pusb_dev
,
106 usb_rcvctrlpipe(dev
->pusb_dev
, 0),
108 USB_DIR_IN
| USB_TYPE_VENDOR
| USB_RECIP_DEVICE
,
112 debug("%s() len=%d != sz=%d\n", __func__
, len
, size
);
115 memcpy(data
, buf
, size
);
120 * mcs7830_write_reg() - write a register of the network adapter
121 * @dev: network device to write to
122 * @idx: index of the register to start writing to
123 * @size: number of bytes to write
124 * @data: buffer holding the data to write
125 * Return: zero upon success, negative upon error
127 static int mcs7830_write_reg(struct ueth_data
*dev
, uint8_t idx
,
128 uint16_t size
, void *data
)
131 ALLOC_CACHE_ALIGN_BUFFER(uint8_t, buf
, size
);
133 debug("%s() idx=0x%04X sz=%d\n", __func__
, idx
, size
);
135 memcpy(buf
, data
, size
);
136 len
= usb_control_msg(dev
->pusb_dev
,
137 usb_sndctrlpipe(dev
->pusb_dev
, 0),
139 USB_DIR_OUT
| USB_TYPE_VENDOR
| USB_RECIP_DEVICE
,
143 debug("%s() len=%d != sz=%d\n", __func__
, len
, size
);
150 * mcs7830_phy_emit_wait() - emit PHY read/write access, wait for its execution
151 * @dev: network device to talk to
152 * @rwflag: PHY_CMD1_READ or PHY_CMD1_WRITE opcode
153 * @index: number of the PHY register to read or write
154 * Return: zero upon success, negative upon error
156 static int mcs7830_phy_emit_wait(struct ueth_data
*dev
,
157 uint8_t rwflag
, uint8_t index
)
163 /* send the PHY read/write request */
164 cmd
[0] = rwflag
| PHY_CMD1_PHYADDR
;
165 cmd
[1] = PHY_CMD2_PEND
| (index
& 0x1f);
166 rc
= mcs7830_write_reg(dev
, REG_PHY_CMD
, sizeof(cmd
), cmd
);
170 /* wait for the response to become available (usually < 1ms) */
173 rc
= mcs7830_read_reg(dev
, REG_PHY_CMD
, sizeof(cmd
), cmd
);
176 if (cmd
[1] & PHY_CMD2_READY
)
186 * mcs7830_read_phy() - read a PHY register of the network adapter
187 * @dev: network device to read from
188 * @index: index of the PHY register to read from
189 * Return: non-negative 16bit register content, negative upon error
191 static int mcs7830_read_phy(struct ueth_data
*dev
, uint8_t index
)
196 /* issue the PHY read request and wait for its execution */
197 rc
= mcs7830_phy_emit_wait(dev
, PHY_CMD1_READ
, index
);
201 /* fetch the PHY data which was read */
202 rc
= mcs7830_read_reg(dev
, REG_PHY_DATA
, sizeof(val
), &val
);
205 rc
= le16_to_cpu(val
);
206 debug("%s(%s, %d) => 0x%04X\n", __func__
, dev
->eth_dev
.name
, index
, rc
);
211 * mcs7830_write_phy() - write a PHY register of the network adapter
212 * @dev: network device to write to
213 * @index: index of the PHY register to write to
214 * @val: value to write to the PHY register
215 * Return: zero upon success, negative upon error
217 static int mcs7830_write_phy(struct ueth_data
*dev
, uint8_t index
, uint16_t val
)
221 debug("%s(%s, %d, 0x%04X)\n", __func__
, dev
->eth_dev
.name
, index
, val
);
223 /* setup the PHY data which is to get written */
224 val
= cpu_to_le16(val
);
225 rc
= mcs7830_write_reg(dev
, REG_PHY_DATA
, sizeof(val
), &val
);
229 /* issue the PHY write request and wait for its execution */
230 rc
= mcs7830_phy_emit_wait(dev
, PHY_CMD1_WRITE
, index
);
238 * mcs7830_write_config() - write to the network adapter's config register
239 * @eth: network device to write to
240 * Return: zero upon success, negative upon error
242 * the data which gets written is taken from the shadow config register
243 * within the device driver's private data
245 static int mcs7830_write_config(struct ueth_data
*dev
)
247 struct mcs7830_private
*priv
;
250 debug("%s()\n", __func__
);
251 priv
= dev
->dev_priv
;
253 rc
= mcs7830_write_reg(dev
, REG_CONFIG
,
254 sizeof(priv
->config
), &priv
->config
);
256 debug("writing config to adapter failed\n");
264 * mcs7830_write_mchash() - write the network adapter's multicast filter
265 * @eth: network device to write to
266 * Return: zero upon success, negative upon error
268 * the data which gets written is taken from the shadow multicast hashes
269 * within the device driver's private data
271 static int mcs7830_write_mchash(struct ueth_data
*dev
)
273 struct mcs7830_private
*priv
;
276 debug("%s()\n", __func__
);
277 priv
= dev
->dev_priv
;
279 rc
= mcs7830_write_reg(dev
, REG_MULTICAST_HASH
,
280 sizeof(priv
->mchash
), &priv
->mchash
);
282 debug("writing multicast hash to adapter failed\n");
290 * mcs7830_set_autoneg() - setup and trigger ethernet link autonegotiation
291 * @eth: network device to run link negotiation on
292 * Return: zero upon success, negative upon error
294 * the routine advertises available media and starts autonegotiation
296 static int mcs7830_set_autoneg(struct ueth_data
*dev
)
301 debug("%s()\n", __func__
);
304 * algorithm taken from the Linux driver, which took it from
305 * "the original mcs7830 version 1.4 driver":
307 * enable all media, reset BMCR, enable auto neg, restart
308 * auto neg while keeping the enable auto neg flag set
311 adv
= ADVERTISE_PAUSE_CAP
| ADVERTISE_ALL
| ADVERTISE_CSMA
;
312 rc
= mcs7830_write_phy(dev
, MII_ADVERTISE
, adv
);
316 rc
= mcs7830_write_phy(dev
, MII_BMCR
, flg
);
318 flg
|= BMCR_ANENABLE
;
320 rc
= mcs7830_write_phy(dev
, MII_BMCR
, flg
);
322 flg
|= BMCR_ANRESTART
;
324 rc
= mcs7830_write_phy(dev
, MII_BMCR
, flg
);
330 * mcs7830_get_rev() - identify a network adapter's chip revision
331 * @eth: network device to identify
332 * Return: non-negative number, reflecting the revision number
334 * currently, only "rev C and higher" and "below rev C" are needed, so
335 * the return value is #1 for "below rev C", and #2 for "rev C and above"
337 static int mcs7830_get_rev(struct ueth_data
*dev
)
343 /* register 22 is readable in rev C and higher */
344 rc
= mcs7830_read_reg(dev
, REG_FRAME_DROP_COUNTER
, sizeof(buf
), buf
);
349 debug("%s() rc=%d, rev=%d\n", __func__
, rc
, rev
);
354 * mcs7830_apply_fixup() - identify an adapter and potentially apply fixups
355 * @eth: network device to identify and apply fixups to
356 * Return: zero upon success (no errors emitted from here)
358 * this routine identifies the network adapter's chip revision, and applies
359 * fixups for known issues
361 static int mcs7830_apply_fixup(struct ueth_data
*dev
)
367 rev
= mcs7830_get_rev(dev
);
368 debug("%s() rev=%d\n", __func__
, rev
);
371 * rev C requires setting the pause threshold (the Linux driver
372 * is inconsistent, the implementation does it for "rev C
373 * exactly", the introductory comment says "rev C and above")
376 debug("%s: applying rev C fixup\n", dev
->eth_dev
.name
);
377 thr
= PAUSE_THRESHOLD_DEFAULT
;
378 for (i
= 0; i
< 2; i
++) {
379 (void)mcs7830_write_reg(dev
, REG_PAUSE_THRESHOLD
,
389 * mcs7830_basic_reset() - bring the network adapter into a known first state
390 * @eth: network device to act upon
391 * Return: zero upon success, negative upon error
393 * this routine initializes the network adapter such that subsequent invocations
394 * of the interface callbacks can exchange ethernet frames; link negotiation is
395 * triggered from here already and continues in background
397 static int mcs7830_basic_reset(struct ueth_data
*dev
)
399 struct mcs7830_private
*priv
;
402 debug("%s()\n", __func__
);
403 priv
= dev
->dev_priv
;
406 * comment from the respective Linux driver, which
407 * unconditionally sets the ALLMULTICAST flag as well:
408 * should not be needed, but does not work otherwise
410 priv
->config
= CONF_TXENABLE
;
411 priv
->config
|= CONF_ALLMULTICAST
;
413 rc
= mcs7830_set_autoneg(dev
);
415 error("setting autoneg failed\n");
419 rc
= mcs7830_write_mchash(dev
);
421 error("failed to set multicast hash\n");
425 rc
= mcs7830_write_config(dev
);
427 error("failed to set configuration\n");
431 rc
= mcs7830_apply_fixup(dev
);
433 error("fixup application failed\n");
441 * mcs7830_read_mac() - read an ethernet adapter's MAC address
442 * @eth: network device to read from
443 * Return: zero upon success, negative upon error
445 * this routine fetches the MAC address stored within the ethernet adapter,
446 * and stores it in the ethernet interface's data structure
448 static int mcs7830_read_mac(struct eth_device
*eth
)
450 struct ueth_data
*dev
;
452 uint8_t buf
[ETH_ALEN
];
454 debug("%s()\n", __func__
);
457 rc
= mcs7830_read_reg(dev
, REG_ETHER_ADDR
, ETH_ALEN
, buf
);
459 debug("reading MAC from adapter failed\n");
463 memcpy(ð
->enetaddr
[0], buf
, ETH_ALEN
);
468 * mcs7830_write_mac() - write an ethernet adapter's MAC address
469 * @eth: network device to write to
470 * Return: zero upon success, negative upon error
472 * this routine takes the MAC address from the ethernet interface's data
473 * structure, and writes it into the ethernet adapter such that subsequent
474 * exchange of ethernet frames uses this address
476 static int mcs7830_write_mac(struct eth_device
*eth
)
478 struct ueth_data
*dev
;
481 debug("%s()\n", __func__
);
484 if (sizeof(eth
->enetaddr
) != ETH_ALEN
)
486 rc
= mcs7830_write_reg(dev
, REG_ETHER_ADDR
, ETH_ALEN
, eth
->enetaddr
);
488 debug("writing MAC to adapter failed\n");
495 * mcs7830_init() - network interface's init callback
496 * @eth: network device to initialize
497 * @bd: board information
498 * Return: zero upon success, negative upon error
500 * after initial setup during probe() and get_info(), this init() callback
501 * ensures that the link is up and subsequent send() and recv() calls can
502 * exchange ethernet frames
504 static int mcs7830_init(struct eth_device
*eth
, bd_t
*bd
)
506 struct ueth_data
*dev
;
510 debug("%s()\n", __func__
);
515 have_link
= mcs7830_read_phy(dev
, MII_BMSR
) & BMSR_LSTATUS
;
518 udelay(LINKSTATUS_TIMEOUT_RES
* 1000);
519 timeout
+= LINKSTATUS_TIMEOUT_RES
;
520 } while (timeout
< LINKSTATUS_TIMEOUT
);
522 debug("ethernet link is down\n");
529 * mcs7830_send() - network interface's send callback
530 * @eth: network device to send the frame from
531 * @packet: ethernet frame content
532 * @length: ethernet frame length
533 * Return: zero upon success, negative upon error
535 * this routine send an ethernet frame out of the network interface
537 static int mcs7830_send(struct eth_device
*eth
, void *packet
, int length
)
539 struct ueth_data
*dev
;
542 /* there is a status byte after the ethernet frame */
543 ALLOC_CACHE_ALIGN_BUFFER(uint8_t, buf
, PKTSIZE
+ sizeof(uint8_t));
547 memcpy(buf
, packet
, length
);
548 rc
= usb_bulk_msg(dev
->pusb_dev
,
549 usb_sndbulkpipe(dev
->pusb_dev
, dev
->ep_out
),
550 &buf
[0], length
, &gotlen
,
552 debug("%s() TX want len %d, got len %d, rc %d\n",
553 __func__
, length
, gotlen
, rc
);
558 * mcs7830_recv() - network interface's recv callback
559 * @eth: network device to receive frames from
560 * Return: zero upon success, negative upon error
562 * this routine checks for available ethernet frames that the network
563 * interface might have received, and notifies the network stack
565 static int mcs7830_recv(struct eth_device
*eth
)
567 struct ueth_data
*dev
;
568 ALLOC_CACHE_ALIGN_BUFFER(uint8_t, buf
, MCS7830_RX_URB_SIZE
);
569 int rc
, wantlen
, gotlen
;
572 debug("%s()\n", __func__
);
575 /* fetch input data from the adapter */
576 wantlen
= MCS7830_RX_URB_SIZE
;
577 rc
= usb_bulk_msg(dev
->pusb_dev
,
578 usb_rcvbulkpipe(dev
->pusb_dev
, dev
->ep_in
),
579 &buf
[0], wantlen
, &gotlen
,
581 debug("%s() RX want len %d, got len %d, rc %d\n",
582 __func__
, wantlen
, gotlen
, rc
);
584 error("RX: failed to receive\n");
587 if (gotlen
> wantlen
) {
588 error("RX: got too many bytes (%d)\n", gotlen
);
593 * the bulk message that we received from USB contains exactly
594 * one ethernet frame and a trailing status byte
596 if (gotlen
< sizeof(sts
))
598 gotlen
-= sizeof(sts
);
601 if (sts
== STAT_RX_FRAME_CORRECT
) {
602 debug("%s() got a frame, len=%d\n", __func__
, gotlen
);
603 net_process_received_packet(buf
, gotlen
);
607 debug("RX: frame error (sts 0x%02X, %s %s %s %s %s)\n",
609 (sts
& STAT_RX_LARGE_FRAME
) ? "large" : "-",
610 (sts
& STAT_RX_LENGTH_ERROR
) ? "length" : "-",
611 (sts
& STAT_RX_SHORT_FRAME
) ? "short" : "-",
612 (sts
& STAT_RX_CRC_ERROR
) ? "crc" : "-",
613 (sts
& STAT_RX_ALIGNMENT_ERROR
) ? "align" : "-");
618 * mcs7830_halt() - network interface's halt callback
619 * @eth: network device to cease operation of
622 * this routine is supposed to undo the effect of previous initialization and
623 * ethernet frames exchange; in this implementation it's a NOP
625 static void mcs7830_halt(struct eth_device
*eth
)
627 debug("%s()\n", __func__
);
631 * mcs7830_iface_idx - index of detected network interfaces
633 * this counter keeps track of identified supported interfaces,
634 * to assign unique names as more interfaces are found
636 static int mcs7830_iface_idx
;
639 * mcs7830_eth_before_probe() - network driver's before_probe callback
642 * this routine initializes driver's internal data in preparation of
643 * subsequent probe callbacks
645 void mcs7830_eth_before_probe(void)
647 mcs7830_iface_idx
= 0;
651 * struct mcs7830_dongle - description of a supported Moschip ethernet dongle
652 * @vendor: 16bit USB vendor identification
653 * @product: 16bit USB product identification
655 * this structure describes a supported USB ethernet dongle by means of the
656 * vendor and product codes found during USB enumeration; no flags are held
657 * here since all supported dongles have identical behaviour, and required
658 * fixups get determined at runtime, such that no manual configuration is
661 struct mcs7830_dongle
{
667 * mcs7830_dongles - the list of supported Moschip based USB ethernet dongles
669 static const struct mcs7830_dongle mcs7830_dongles
[] = {
670 { 0x9710, 0x7832, }, /* Moschip 7832 */
671 { 0x9710, 0x7830, }, /* Moschip 7830 */
672 { 0x9710, 0x7730, }, /* Moschip 7730 */
673 { 0x0df6, 0x0021, }, /* Sitecom LN 30 */
677 * mcs7830_eth_probe() - network driver's probe callback
678 * @dev: detected USB device to check
679 * @ifnum: detected USB interface to check
680 * @ss: USB ethernet data structure to fill in upon match
681 * Return: #1 upon match, #0 upon mismatch or error
683 * this routine checks whether the found USB device is supported by
684 * this ethernet driver, and upon match fills in the USB ethernet
685 * data structure which later is passed to the get_info callback
687 int mcs7830_eth_probe(struct usb_device
*dev
, unsigned int ifnum
,
688 struct ueth_data
*ss
)
690 struct usb_interface
*iface
;
691 struct usb_interface_descriptor
*iface_desc
;
693 struct mcs7830_private
*priv
;
694 int ep_in_found
, ep_out_found
, ep_intr_found
;
696 debug("%s()\n", __func__
);
698 /* iterate the list of supported dongles */
699 iface
= &dev
->config
.if_desc
[ifnum
];
700 iface_desc
= &iface
->desc
;
701 for (i
= 0; i
< ARRAY_SIZE(mcs7830_dongles
); i
++) {
702 if (dev
->descriptor
.idVendor
== mcs7830_dongles
[i
].vendor
&&
703 dev
->descriptor
.idProduct
== mcs7830_dongles
[i
].product
)
706 if (i
== ARRAY_SIZE(mcs7830_dongles
))
708 debug("detected USB ethernet device: %04X:%04X\n",
709 dev
->descriptor
.idVendor
, dev
->descriptor
.idProduct
);
711 /* fill in driver private data */
712 priv
= calloc(1, sizeof(*priv
));
716 /* fill in the ueth_data structure, attach private data */
717 memset(ss
, 0, sizeof(*ss
));
720 ss
->subclass
= iface_desc
->bInterfaceSubClass
;
721 ss
->protocol
= iface_desc
->bInterfaceProtocol
;
725 * a minimum of three endpoints is expected: in (bulk),
726 * out (bulk), and interrupt; ignore all others
728 ep_in_found
= ep_out_found
= ep_intr_found
= 0;
729 for (i
= 0; i
< iface_desc
->bNumEndpoints
; i
++) {
730 uint8_t eptype
, epaddr
;
733 eptype
= iface
->ep_desc
[i
].bmAttributes
;
734 eptype
&= USB_ENDPOINT_XFERTYPE_MASK
;
736 epaddr
= iface
->ep_desc
[i
].bEndpointAddress
;
737 is_input
= epaddr
& USB_DIR_IN
;
738 epaddr
&= USB_ENDPOINT_NUMBER_MASK
;
740 if (eptype
== USB_ENDPOINT_XFER_BULK
) {
741 if (is_input
&& !ep_in_found
) {
745 if (!is_input
&& !ep_out_found
) {
751 if (eptype
== USB_ENDPOINT_XFER_INT
) {
752 if (is_input
&& !ep_intr_found
) {
754 ss
->irqinterval
= iface
->ep_desc
[i
].bInterval
;
759 debug("endpoints: in %d, out %d, intr %d\n",
760 ss
->ep_in
, ss
->ep_out
, ss
->ep_int
);
762 /* apply basic sanity checks */
763 if (usb_set_interface(dev
, iface_desc
->bInterfaceNumber
, 0) ||
764 !ss
->ep_in
|| !ss
->ep_out
|| !ss
->ep_int
) {
765 debug("device probe incomplete\n");
774 * mcs7830_eth_get_info() - network driver's get_info callback
775 * @dev: detected USB device
776 * @ss: USB ethernet data structure filled in at probe()
777 * @eth: ethernet interface data structure to fill in
778 * Return: #1 upon success, #0 upon error
780 * this routine registers the mandatory init(), send(), recv(), and
781 * halt() callbacks with the ethernet interface, can register the
782 * optional write_hwaddr() callback with the ethernet interface,
783 * and initiates configuration of the interface such that subsequent
784 * calls to those callbacks results in network communication
786 int mcs7830_eth_get_info(struct usb_device
*dev
, struct ueth_data
*ss
,
787 struct eth_device
*eth
)
789 debug("%s()\n", __func__
);
791 debug("%s: missing parameter.\n", __func__
);
795 snprintf(eth
->name
, sizeof(eth
->name
), "%s%d",
796 MCS7830_BASE_NAME
, mcs7830_iface_idx
++);
797 eth
->init
= mcs7830_init
;
798 eth
->send
= mcs7830_send
;
799 eth
->recv
= mcs7830_recv
;
800 eth
->halt
= mcs7830_halt
;
801 eth
->write_hwaddr
= mcs7830_write_mac
;
804 if (mcs7830_basic_reset(ss
))
807 if (mcs7830_read_mac(eth
))
809 debug("MAC %pM\n", eth
->enetaddr
);