xref: /OK3568_Linux_fs/u-boot/drivers/usb/eth/mcs7830.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun /*
2*4882a593Smuzhiyun  * Copyright (c) 2013 Gerhard Sittig <gsi@denx.de>
3*4882a593Smuzhiyun  * based on the U-Boot Asix driver as well as information
4*4882a593Smuzhiyun  * from the Linux Moschip driver
5*4882a593Smuzhiyun  *
6*4882a593Smuzhiyun  * SPDX-License-Identifier:	GPL-2.0+
7*4882a593Smuzhiyun  */
8*4882a593Smuzhiyun 
9*4882a593Smuzhiyun /*
10*4882a593Smuzhiyun  * MOSCHIP MCS7830 based (7730/7830/7832) USB 2.0 Ethernet Devices
11*4882a593Smuzhiyun  */
12*4882a593Smuzhiyun 
13*4882a593Smuzhiyun #include <common.h>
14*4882a593Smuzhiyun #include <dm.h>
15*4882a593Smuzhiyun #include <errno.h>
16*4882a593Smuzhiyun #include <linux/mii.h>
17*4882a593Smuzhiyun #include <malloc.h>
18*4882a593Smuzhiyun #include <memalign.h>
19*4882a593Smuzhiyun #include <usb.h>
20*4882a593Smuzhiyun 
21*4882a593Smuzhiyun #include "usb_ether.h"
22*4882a593Smuzhiyun 
23*4882a593Smuzhiyun #define MCS7830_BASE_NAME	"mcs"
24*4882a593Smuzhiyun 
25*4882a593Smuzhiyun #define USBCALL_TIMEOUT		1000
26*4882a593Smuzhiyun #define LINKSTATUS_TIMEOUT	5000	/* link status, connect timeout */
27*4882a593Smuzhiyun #define LINKSTATUS_TIMEOUT_RES	50	/* link status, resolution in msec */
28*4882a593Smuzhiyun 
29*4882a593Smuzhiyun #define MCS7830_RX_URB_SIZE	2048
30*4882a593Smuzhiyun 
31*4882a593Smuzhiyun /* command opcodes */
32*4882a593Smuzhiyun #define MCS7830_WR_BREQ		0x0d
33*4882a593Smuzhiyun #define MCS7830_RD_BREQ		0x0e
34*4882a593Smuzhiyun 
35*4882a593Smuzhiyun /* register layout, numerical offset specs for USB API calls */
36*4882a593Smuzhiyun struct mcs7830_regs {
37*4882a593Smuzhiyun 	uint8_t multicast_hashes[8];
38*4882a593Smuzhiyun 	uint8_t packet_gap[2];
39*4882a593Smuzhiyun 	uint8_t phy_data[2];
40*4882a593Smuzhiyun 	uint8_t phy_command[2];
41*4882a593Smuzhiyun 	uint8_t configuration;
42*4882a593Smuzhiyun 	uint8_t ether_address[6];
43*4882a593Smuzhiyun 	uint8_t frame_drop_count;
44*4882a593Smuzhiyun 	uint8_t pause_threshold;
45*4882a593Smuzhiyun };
46*4882a593Smuzhiyun #define REG_MULTICAST_HASH	offsetof(struct mcs7830_regs, multicast_hashes)
47*4882a593Smuzhiyun #define REG_PHY_DATA		offsetof(struct mcs7830_regs, phy_data)
48*4882a593Smuzhiyun #define REG_PHY_CMD		offsetof(struct mcs7830_regs, phy_command)
49*4882a593Smuzhiyun #define REG_CONFIG		offsetof(struct mcs7830_regs, configuration)
50*4882a593Smuzhiyun #define REG_ETHER_ADDR		offsetof(struct mcs7830_regs, ether_address)
51*4882a593Smuzhiyun #define REG_FRAME_DROP_COUNTER	offsetof(struct mcs7830_regs, frame_drop_count)
52*4882a593Smuzhiyun #define REG_PAUSE_THRESHOLD	offsetof(struct mcs7830_regs, pause_threshold)
53*4882a593Smuzhiyun 
54*4882a593Smuzhiyun /* bit masks and default values for the above registers */
55*4882a593Smuzhiyun #define PHY_CMD1_READ		0x40
56*4882a593Smuzhiyun #define PHY_CMD1_WRITE		0x20
57*4882a593Smuzhiyun #define PHY_CMD1_PHYADDR	0x01
58*4882a593Smuzhiyun 
59*4882a593Smuzhiyun #define PHY_CMD2_PEND		0x80
60*4882a593Smuzhiyun #define PHY_CMD2_READY		0x40
61*4882a593Smuzhiyun 
62*4882a593Smuzhiyun #define CONF_CFG		0x80
63*4882a593Smuzhiyun #define CONF_SPEED100		0x40
64*4882a593Smuzhiyun #define CONF_FDX_ENABLE		0x20
65*4882a593Smuzhiyun #define CONF_RXENABLE		0x10
66*4882a593Smuzhiyun #define CONF_TXENABLE		0x08
67*4882a593Smuzhiyun #define CONF_SLEEPMODE		0x04
68*4882a593Smuzhiyun #define CONF_ALLMULTICAST	0x02
69*4882a593Smuzhiyun #define CONF_PROMISCUOUS	0x01
70*4882a593Smuzhiyun 
71*4882a593Smuzhiyun #define PAUSE_THRESHOLD_DEFAULT	0
72*4882a593Smuzhiyun 
73*4882a593Smuzhiyun /* bit masks for the status byte which follows received ethernet frames */
74*4882a593Smuzhiyun #define STAT_RX_FRAME_CORRECT	0x20
75*4882a593Smuzhiyun #define STAT_RX_LARGE_FRAME	0x10
76*4882a593Smuzhiyun #define STAT_RX_CRC_ERROR	0x08
77*4882a593Smuzhiyun #define STAT_RX_ALIGNMENT_ERROR	0x04
78*4882a593Smuzhiyun #define STAT_RX_LENGTH_ERROR	0x02
79*4882a593Smuzhiyun #define STAT_RX_SHORT_FRAME	0x01
80*4882a593Smuzhiyun 
81*4882a593Smuzhiyun /*
82*4882a593Smuzhiyun  * struct mcs7830_private - private driver data for an individual adapter
83*4882a593Smuzhiyun  * @config:	shadow for the network adapter's configuration register
84*4882a593Smuzhiyun  * @mchash:	shadow for the network adapter's multicast hash registers
85*4882a593Smuzhiyun  */
86*4882a593Smuzhiyun struct mcs7830_private {
87*4882a593Smuzhiyun #ifdef CONFIG_DM_ETH
88*4882a593Smuzhiyun 	uint8_t rx_buf[MCS7830_RX_URB_SIZE];
89*4882a593Smuzhiyun 	struct ueth_data ueth;
90*4882a593Smuzhiyun #endif
91*4882a593Smuzhiyun 	uint8_t config;
92*4882a593Smuzhiyun 	uint8_t mchash[8];
93*4882a593Smuzhiyun };
94*4882a593Smuzhiyun 
95*4882a593Smuzhiyun /*
96*4882a593Smuzhiyun  * mcs7830_read_reg() - read a register of the network adapter
97*4882a593Smuzhiyun  * @udev:	network device to read from
98*4882a593Smuzhiyun  * @idx:	index of the register to start reading from
99*4882a593Smuzhiyun  * @size:	number of bytes to read
100*4882a593Smuzhiyun  * @data:	buffer to read into
101*4882a593Smuzhiyun  * Return: zero upon success, negative upon error
102*4882a593Smuzhiyun  */
mcs7830_read_reg(struct usb_device * udev,uint8_t idx,uint16_t size,void * data)103*4882a593Smuzhiyun static int mcs7830_read_reg(struct usb_device *udev, uint8_t idx,
104*4882a593Smuzhiyun 			    uint16_t size, void *data)
105*4882a593Smuzhiyun {
106*4882a593Smuzhiyun 	int len;
107*4882a593Smuzhiyun 	ALLOC_CACHE_ALIGN_BUFFER(uint8_t, buf, size);
108*4882a593Smuzhiyun 
109*4882a593Smuzhiyun 	debug("%s() idx=0x%04X sz=%d\n", __func__, idx, size);
110*4882a593Smuzhiyun 
111*4882a593Smuzhiyun 	len = usb_control_msg(udev,
112*4882a593Smuzhiyun 			      usb_rcvctrlpipe(udev, 0),
113*4882a593Smuzhiyun 			      MCS7830_RD_BREQ,
114*4882a593Smuzhiyun 			      USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
115*4882a593Smuzhiyun 			      0, idx, buf, size,
116*4882a593Smuzhiyun 			      USBCALL_TIMEOUT);
117*4882a593Smuzhiyun 	if (len != size) {
118*4882a593Smuzhiyun 		debug("%s() len=%d != sz=%d\n", __func__, len, size);
119*4882a593Smuzhiyun 		return -EIO;
120*4882a593Smuzhiyun 	}
121*4882a593Smuzhiyun 	memcpy(data, buf, size);
122*4882a593Smuzhiyun 	return 0;
123*4882a593Smuzhiyun }
124*4882a593Smuzhiyun 
125*4882a593Smuzhiyun /*
126*4882a593Smuzhiyun  * mcs7830_write_reg() - write a register of the network adapter
127*4882a593Smuzhiyun  * @udev:	network device to write to
128*4882a593Smuzhiyun  * @idx:	index of the register to start writing to
129*4882a593Smuzhiyun  * @size:	number of bytes to write
130*4882a593Smuzhiyun  * @data:	buffer holding the data to write
131*4882a593Smuzhiyun  * Return: zero upon success, negative upon error
132*4882a593Smuzhiyun  */
mcs7830_write_reg(struct usb_device * udev,uint8_t idx,uint16_t size,void * data)133*4882a593Smuzhiyun static int mcs7830_write_reg(struct usb_device *udev, uint8_t idx,
134*4882a593Smuzhiyun 			     uint16_t size, void *data)
135*4882a593Smuzhiyun {
136*4882a593Smuzhiyun 	int len;
137*4882a593Smuzhiyun 	ALLOC_CACHE_ALIGN_BUFFER(uint8_t, buf, size);
138*4882a593Smuzhiyun 
139*4882a593Smuzhiyun 	debug("%s() idx=0x%04X sz=%d\n", __func__, idx, size);
140*4882a593Smuzhiyun 
141*4882a593Smuzhiyun 	memcpy(buf, data, size);
142*4882a593Smuzhiyun 	len = usb_control_msg(udev,
143*4882a593Smuzhiyun 			      usb_sndctrlpipe(udev, 0),
144*4882a593Smuzhiyun 			      MCS7830_WR_BREQ,
145*4882a593Smuzhiyun 			      USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
146*4882a593Smuzhiyun 			      0, idx, buf, size,
147*4882a593Smuzhiyun 			      USBCALL_TIMEOUT);
148*4882a593Smuzhiyun 	if (len != size) {
149*4882a593Smuzhiyun 		debug("%s() len=%d != sz=%d\n", __func__, len, size);
150*4882a593Smuzhiyun 		return -EIO;
151*4882a593Smuzhiyun 	}
152*4882a593Smuzhiyun 	return 0;
153*4882a593Smuzhiyun }
154*4882a593Smuzhiyun 
155*4882a593Smuzhiyun /*
156*4882a593Smuzhiyun  * mcs7830_phy_emit_wait() - emit PHY read/write access, wait for its execution
157*4882a593Smuzhiyun  * @udev:	network device to talk to
158*4882a593Smuzhiyun  * @rwflag:	PHY_CMD1_READ or PHY_CMD1_WRITE opcode
159*4882a593Smuzhiyun  * @index:	number of the PHY register to read or write
160*4882a593Smuzhiyun  * Return: zero upon success, negative upon error
161*4882a593Smuzhiyun  */
mcs7830_phy_emit_wait(struct usb_device * udev,uint8_t rwflag,uint8_t index)162*4882a593Smuzhiyun static int mcs7830_phy_emit_wait(struct usb_device *udev,
163*4882a593Smuzhiyun 				 uint8_t rwflag, uint8_t index)
164*4882a593Smuzhiyun {
165*4882a593Smuzhiyun 	int rc;
166*4882a593Smuzhiyun 	int retry;
167*4882a593Smuzhiyun 	uint8_t cmd[2];
168*4882a593Smuzhiyun 
169*4882a593Smuzhiyun 	/* send the PHY read/write request */
170*4882a593Smuzhiyun 	cmd[0] = rwflag | PHY_CMD1_PHYADDR;
171*4882a593Smuzhiyun 	cmd[1] = PHY_CMD2_PEND | (index & 0x1f);
172*4882a593Smuzhiyun 	rc = mcs7830_write_reg(udev, REG_PHY_CMD, sizeof(cmd), cmd);
173*4882a593Smuzhiyun 	if (rc < 0)
174*4882a593Smuzhiyun 		return rc;
175*4882a593Smuzhiyun 
176*4882a593Smuzhiyun 	/* wait for the response to become available (usually < 1ms) */
177*4882a593Smuzhiyun 	retry = 10;
178*4882a593Smuzhiyun 	do {
179*4882a593Smuzhiyun 		rc = mcs7830_read_reg(udev, REG_PHY_CMD, sizeof(cmd), cmd);
180*4882a593Smuzhiyun 		if (rc < 0)
181*4882a593Smuzhiyun 			return rc;
182*4882a593Smuzhiyun 		if (cmd[1] & PHY_CMD2_READY)
183*4882a593Smuzhiyun 			return 0;
184*4882a593Smuzhiyun 		if (!retry--)
185*4882a593Smuzhiyun 			return -ETIMEDOUT;
186*4882a593Smuzhiyun 		mdelay(1);
187*4882a593Smuzhiyun 	} while (1);
188*4882a593Smuzhiyun 	/* UNREACH */
189*4882a593Smuzhiyun }
190*4882a593Smuzhiyun 
191*4882a593Smuzhiyun /*
192*4882a593Smuzhiyun  * mcs7830_read_phy() - read a PHY register of the network adapter
193*4882a593Smuzhiyun  * @udev:	network device to read from
194*4882a593Smuzhiyun  * @index:	index of the PHY register to read from
195*4882a593Smuzhiyun  * Return: non-negative 16bit register content, negative upon error
196*4882a593Smuzhiyun  */
mcs7830_read_phy(struct usb_device * udev,uint8_t index)197*4882a593Smuzhiyun static int mcs7830_read_phy(struct usb_device *udev, uint8_t index)
198*4882a593Smuzhiyun {
199*4882a593Smuzhiyun 	int rc;
200*4882a593Smuzhiyun 	uint16_t val;
201*4882a593Smuzhiyun 
202*4882a593Smuzhiyun 	/* issue the PHY read request and wait for its execution */
203*4882a593Smuzhiyun 	rc = mcs7830_phy_emit_wait(udev, PHY_CMD1_READ, index);
204*4882a593Smuzhiyun 	if (rc < 0)
205*4882a593Smuzhiyun 		return rc;
206*4882a593Smuzhiyun 
207*4882a593Smuzhiyun 	/* fetch the PHY data which was read */
208*4882a593Smuzhiyun 	rc = mcs7830_read_reg(udev, REG_PHY_DATA, sizeof(val), &val);
209*4882a593Smuzhiyun 	if (rc < 0)
210*4882a593Smuzhiyun 		return rc;
211*4882a593Smuzhiyun 	rc = le16_to_cpu(val);
212*4882a593Smuzhiyun 	debug("%s(%d) => 0x%04X\n", __func__, index, rc);
213*4882a593Smuzhiyun 	return rc;
214*4882a593Smuzhiyun }
215*4882a593Smuzhiyun 
216*4882a593Smuzhiyun /*
217*4882a593Smuzhiyun  * mcs7830_write_phy() - write a PHY register of the network adapter
218*4882a593Smuzhiyun  * @udev:	network device to write to
219*4882a593Smuzhiyun  * @index:	index of the PHY register to write to
220*4882a593Smuzhiyun  * @val:	value to write to the PHY register
221*4882a593Smuzhiyun  * Return: zero upon success, negative upon error
222*4882a593Smuzhiyun  */
mcs7830_write_phy(struct usb_device * udev,uint8_t index,uint16_t val)223*4882a593Smuzhiyun static int mcs7830_write_phy(struct usb_device *udev, uint8_t index,
224*4882a593Smuzhiyun 			     uint16_t val)
225*4882a593Smuzhiyun {
226*4882a593Smuzhiyun 	int rc;
227*4882a593Smuzhiyun 
228*4882a593Smuzhiyun 	debug("%s(%d, 0x%04X)\n", __func__, index, val);
229*4882a593Smuzhiyun 
230*4882a593Smuzhiyun 	/* setup the PHY data which is to get written */
231*4882a593Smuzhiyun 	val = cpu_to_le16(val);
232*4882a593Smuzhiyun 	rc = mcs7830_write_reg(udev, REG_PHY_DATA, sizeof(val), &val);
233*4882a593Smuzhiyun 	if (rc < 0)
234*4882a593Smuzhiyun 		return rc;
235*4882a593Smuzhiyun 
236*4882a593Smuzhiyun 	/* issue the PHY write request and wait for its execution */
237*4882a593Smuzhiyun 	rc = mcs7830_phy_emit_wait(udev, PHY_CMD1_WRITE, index);
238*4882a593Smuzhiyun 	if (rc < 0)
239*4882a593Smuzhiyun 		return rc;
240*4882a593Smuzhiyun 
241*4882a593Smuzhiyun 	return 0;
242*4882a593Smuzhiyun }
243*4882a593Smuzhiyun 
244*4882a593Smuzhiyun /*
245*4882a593Smuzhiyun  * mcs7830_write_config() - write to the network adapter's config register
246*4882a593Smuzhiyun  * @udev:	network device to write to
247*4882a593Smuzhiyun  * @priv:	private data
248*4882a593Smuzhiyun  * Return: zero upon success, negative upon error
249*4882a593Smuzhiyun  *
250*4882a593Smuzhiyun  * the data which gets written is taken from the shadow config register
251*4882a593Smuzhiyun  * within the device driver's private data
252*4882a593Smuzhiyun  */
mcs7830_write_config(struct usb_device * udev,struct mcs7830_private * priv)253*4882a593Smuzhiyun static int mcs7830_write_config(struct usb_device *udev,
254*4882a593Smuzhiyun 				struct mcs7830_private *priv)
255*4882a593Smuzhiyun {
256*4882a593Smuzhiyun 	int rc;
257*4882a593Smuzhiyun 
258*4882a593Smuzhiyun 	debug("%s()\n", __func__);
259*4882a593Smuzhiyun 
260*4882a593Smuzhiyun 	rc = mcs7830_write_reg(udev, REG_CONFIG,
261*4882a593Smuzhiyun 			       sizeof(priv->config), &priv->config);
262*4882a593Smuzhiyun 	if (rc < 0) {
263*4882a593Smuzhiyun 		debug("writing config to adapter failed\n");
264*4882a593Smuzhiyun 		return rc;
265*4882a593Smuzhiyun 	}
266*4882a593Smuzhiyun 
267*4882a593Smuzhiyun 	return 0;
268*4882a593Smuzhiyun }
269*4882a593Smuzhiyun 
270*4882a593Smuzhiyun /*
271*4882a593Smuzhiyun  * mcs7830_write_mchash() - write the network adapter's multicast filter
272*4882a593Smuzhiyun  * @udev:	network device to write to
273*4882a593Smuzhiyun  * @priv:	private data
274*4882a593Smuzhiyun  * Return: zero upon success, negative upon error
275*4882a593Smuzhiyun  *
276*4882a593Smuzhiyun  * the data which gets written is taken from the shadow multicast hashes
277*4882a593Smuzhiyun  * within the device driver's private data
278*4882a593Smuzhiyun  */
mcs7830_write_mchash(struct usb_device * udev,struct mcs7830_private * priv)279*4882a593Smuzhiyun static int mcs7830_write_mchash(struct usb_device *udev,
280*4882a593Smuzhiyun 				struct mcs7830_private *priv)
281*4882a593Smuzhiyun {
282*4882a593Smuzhiyun 	int rc;
283*4882a593Smuzhiyun 
284*4882a593Smuzhiyun 	debug("%s()\n", __func__);
285*4882a593Smuzhiyun 
286*4882a593Smuzhiyun 	rc = mcs7830_write_reg(udev, REG_MULTICAST_HASH,
287*4882a593Smuzhiyun 			       sizeof(priv->mchash), &priv->mchash);
288*4882a593Smuzhiyun 	if (rc < 0) {
289*4882a593Smuzhiyun 		debug("writing multicast hash to adapter failed\n");
290*4882a593Smuzhiyun 		return rc;
291*4882a593Smuzhiyun 	}
292*4882a593Smuzhiyun 
293*4882a593Smuzhiyun 	return 0;
294*4882a593Smuzhiyun }
295*4882a593Smuzhiyun 
296*4882a593Smuzhiyun /*
297*4882a593Smuzhiyun  * mcs7830_set_autoneg() - setup and trigger ethernet link autonegotiation
298*4882a593Smuzhiyun  * @udev:	network device to run link negotiation on
299*4882a593Smuzhiyun  * Return: zero upon success, negative upon error
300*4882a593Smuzhiyun  *
301*4882a593Smuzhiyun  * the routine advertises available media and starts autonegotiation
302*4882a593Smuzhiyun  */
mcs7830_set_autoneg(struct usb_device * udev)303*4882a593Smuzhiyun static int mcs7830_set_autoneg(struct usb_device *udev)
304*4882a593Smuzhiyun {
305*4882a593Smuzhiyun 	int adv, flg;
306*4882a593Smuzhiyun 	int rc;
307*4882a593Smuzhiyun 
308*4882a593Smuzhiyun 	debug("%s()\n", __func__);
309*4882a593Smuzhiyun 
310*4882a593Smuzhiyun 	/*
311*4882a593Smuzhiyun 	 * algorithm taken from the Linux driver, which took it from
312*4882a593Smuzhiyun 	 * "the original mcs7830 version 1.4 driver":
313*4882a593Smuzhiyun 	 *
314*4882a593Smuzhiyun 	 * enable all media, reset BMCR, enable auto neg, restart
315*4882a593Smuzhiyun 	 * auto neg while keeping the enable auto neg flag set
316*4882a593Smuzhiyun 	 */
317*4882a593Smuzhiyun 
318*4882a593Smuzhiyun 	adv = ADVERTISE_PAUSE_CAP | ADVERTISE_ALL | ADVERTISE_CSMA;
319*4882a593Smuzhiyun 	rc = mcs7830_write_phy(udev, MII_ADVERTISE, adv);
320*4882a593Smuzhiyun 
321*4882a593Smuzhiyun 	flg = 0;
322*4882a593Smuzhiyun 	if (!rc)
323*4882a593Smuzhiyun 		rc = mcs7830_write_phy(udev, MII_BMCR, flg);
324*4882a593Smuzhiyun 
325*4882a593Smuzhiyun 	flg |= BMCR_ANENABLE;
326*4882a593Smuzhiyun 	if (!rc)
327*4882a593Smuzhiyun 		rc = mcs7830_write_phy(udev, MII_BMCR, flg);
328*4882a593Smuzhiyun 
329*4882a593Smuzhiyun 	flg |= BMCR_ANRESTART;
330*4882a593Smuzhiyun 	if (!rc)
331*4882a593Smuzhiyun 		rc = mcs7830_write_phy(udev, MII_BMCR, flg);
332*4882a593Smuzhiyun 
333*4882a593Smuzhiyun 	return rc;
334*4882a593Smuzhiyun }
335*4882a593Smuzhiyun 
336*4882a593Smuzhiyun /*
337*4882a593Smuzhiyun  * mcs7830_get_rev() - identify a network adapter's chip revision
338*4882a593Smuzhiyun  * @udev:	network device to identify
339*4882a593Smuzhiyun  * Return: non-negative number, reflecting the revision number
340*4882a593Smuzhiyun  *
341*4882a593Smuzhiyun  * currently, only "rev C and higher" and "below rev C" are needed, so
342*4882a593Smuzhiyun  * the return value is #1 for "below rev C", and #2 for "rev C and above"
343*4882a593Smuzhiyun  */
mcs7830_get_rev(struct usb_device * udev)344*4882a593Smuzhiyun static int mcs7830_get_rev(struct usb_device *udev)
345*4882a593Smuzhiyun {
346*4882a593Smuzhiyun 	uint8_t buf[2];
347*4882a593Smuzhiyun 	int rc;
348*4882a593Smuzhiyun 	int rev;
349*4882a593Smuzhiyun 
350*4882a593Smuzhiyun 	/* register 22 is readable in rev C and higher */
351*4882a593Smuzhiyun 	rc = mcs7830_read_reg(udev, REG_FRAME_DROP_COUNTER, sizeof(buf), buf);
352*4882a593Smuzhiyun 	if (rc < 0)
353*4882a593Smuzhiyun 		rev = 1;
354*4882a593Smuzhiyun 	else
355*4882a593Smuzhiyun 		rev = 2;
356*4882a593Smuzhiyun 	debug("%s() rc=%d, rev=%d\n", __func__, rc, rev);
357*4882a593Smuzhiyun 	return rev;
358*4882a593Smuzhiyun }
359*4882a593Smuzhiyun 
360*4882a593Smuzhiyun /*
361*4882a593Smuzhiyun  * mcs7830_apply_fixup() - identify an adapter and potentially apply fixups
362*4882a593Smuzhiyun  * @udev:	network device to identify and apply fixups to
363*4882a593Smuzhiyun  * Return: zero upon success (no errors emitted from here)
364*4882a593Smuzhiyun  *
365*4882a593Smuzhiyun  * this routine identifies the network adapter's chip revision, and applies
366*4882a593Smuzhiyun  * fixups for known issues
367*4882a593Smuzhiyun  */
mcs7830_apply_fixup(struct usb_device * udev)368*4882a593Smuzhiyun static int mcs7830_apply_fixup(struct usb_device *udev)
369*4882a593Smuzhiyun {
370*4882a593Smuzhiyun 	int rev;
371*4882a593Smuzhiyun 	int i;
372*4882a593Smuzhiyun 	uint8_t thr;
373*4882a593Smuzhiyun 
374*4882a593Smuzhiyun 	rev = mcs7830_get_rev(udev);
375*4882a593Smuzhiyun 	debug("%s() rev=%d\n", __func__, rev);
376*4882a593Smuzhiyun 
377*4882a593Smuzhiyun 	/*
378*4882a593Smuzhiyun 	 * rev C requires setting the pause threshold (the Linux driver
379*4882a593Smuzhiyun 	 * is inconsistent, the implementation does it for "rev C
380*4882a593Smuzhiyun 	 * exactly", the introductory comment says "rev C and above")
381*4882a593Smuzhiyun 	 */
382*4882a593Smuzhiyun 	if (rev == 2) {
383*4882a593Smuzhiyun 		debug("%s: applying rev C fixup\n", __func__);
384*4882a593Smuzhiyun 		thr = PAUSE_THRESHOLD_DEFAULT;
385*4882a593Smuzhiyun 		for (i = 0; i < 2; i++) {
386*4882a593Smuzhiyun 			(void)mcs7830_write_reg(udev, REG_PAUSE_THRESHOLD,
387*4882a593Smuzhiyun 						sizeof(thr), &thr);
388*4882a593Smuzhiyun 			mdelay(1);
389*4882a593Smuzhiyun 		}
390*4882a593Smuzhiyun 	}
391*4882a593Smuzhiyun 
392*4882a593Smuzhiyun 	return 0;
393*4882a593Smuzhiyun }
394*4882a593Smuzhiyun 
395*4882a593Smuzhiyun /*
396*4882a593Smuzhiyun  * mcs7830_basic_reset() - bring the network adapter into a known first state
397*4882a593Smuzhiyun  * @eth:	network device to act upon
398*4882a593Smuzhiyun  * Return: zero upon success, negative upon error
399*4882a593Smuzhiyun  *
400*4882a593Smuzhiyun  * this routine initializes the network adapter such that subsequent invocations
401*4882a593Smuzhiyun  * of the interface callbacks can exchange ethernet frames; link negotiation is
402*4882a593Smuzhiyun  * triggered from here already and continues in background
403*4882a593Smuzhiyun  */
mcs7830_basic_reset(struct usb_device * udev,struct mcs7830_private * priv)404*4882a593Smuzhiyun static int mcs7830_basic_reset(struct usb_device *udev,
405*4882a593Smuzhiyun 			       struct mcs7830_private *priv)
406*4882a593Smuzhiyun {
407*4882a593Smuzhiyun 	int rc;
408*4882a593Smuzhiyun 
409*4882a593Smuzhiyun 	debug("%s()\n", __func__);
410*4882a593Smuzhiyun 
411*4882a593Smuzhiyun 	/*
412*4882a593Smuzhiyun 	 * comment from the respective Linux driver, which
413*4882a593Smuzhiyun 	 * unconditionally sets the ALLMULTICAST flag as well:
414*4882a593Smuzhiyun 	 * should not be needed, but does not work otherwise
415*4882a593Smuzhiyun 	 */
416*4882a593Smuzhiyun 	priv->config = CONF_TXENABLE;
417*4882a593Smuzhiyun 	priv->config |= CONF_ALLMULTICAST;
418*4882a593Smuzhiyun 
419*4882a593Smuzhiyun 	rc = mcs7830_set_autoneg(udev);
420*4882a593Smuzhiyun 	if (rc < 0) {
421*4882a593Smuzhiyun 		pr_err("setting autoneg failed\n");
422*4882a593Smuzhiyun 		return rc;
423*4882a593Smuzhiyun 	}
424*4882a593Smuzhiyun 
425*4882a593Smuzhiyun 	rc = mcs7830_write_mchash(udev, priv);
426*4882a593Smuzhiyun 	if (rc < 0) {
427*4882a593Smuzhiyun 		pr_err("failed to set multicast hash\n");
428*4882a593Smuzhiyun 		return rc;
429*4882a593Smuzhiyun 	}
430*4882a593Smuzhiyun 
431*4882a593Smuzhiyun 	rc = mcs7830_write_config(udev, priv);
432*4882a593Smuzhiyun 	if (rc < 0) {
433*4882a593Smuzhiyun 		pr_err("failed to set configuration\n");
434*4882a593Smuzhiyun 		return rc;
435*4882a593Smuzhiyun 	}
436*4882a593Smuzhiyun 
437*4882a593Smuzhiyun 	rc = mcs7830_apply_fixup(udev);
438*4882a593Smuzhiyun 	if (rc < 0) {
439*4882a593Smuzhiyun 		pr_err("fixup application failed\n");
440*4882a593Smuzhiyun 		return rc;
441*4882a593Smuzhiyun 	}
442*4882a593Smuzhiyun 
443*4882a593Smuzhiyun 	return 0;
444*4882a593Smuzhiyun }
445*4882a593Smuzhiyun 
446*4882a593Smuzhiyun /*
447*4882a593Smuzhiyun  * mcs7830_read_mac() - read an ethernet adapter's MAC address
448*4882a593Smuzhiyun  * @udev:	network device to read from
449*4882a593Smuzhiyun  * @enetaddr:	place to put ethernet MAC address
450*4882a593Smuzhiyun  * Return: zero upon success, negative upon error
451*4882a593Smuzhiyun  *
452*4882a593Smuzhiyun  * this routine fetches the MAC address stored within the ethernet adapter,
453*4882a593Smuzhiyun  * and stores it in the ethernet interface's data structure
454*4882a593Smuzhiyun  */
mcs7830_read_mac(struct usb_device * udev,unsigned char enetaddr[])455*4882a593Smuzhiyun static int mcs7830_read_mac(struct usb_device *udev, unsigned char enetaddr[])
456*4882a593Smuzhiyun {
457*4882a593Smuzhiyun 	int rc;
458*4882a593Smuzhiyun 	uint8_t buf[ETH_ALEN];
459*4882a593Smuzhiyun 
460*4882a593Smuzhiyun 	debug("%s()\n", __func__);
461*4882a593Smuzhiyun 
462*4882a593Smuzhiyun 	rc = mcs7830_read_reg(udev, REG_ETHER_ADDR, ETH_ALEN, buf);
463*4882a593Smuzhiyun 	if (rc < 0) {
464*4882a593Smuzhiyun 		debug("reading MAC from adapter failed\n");
465*4882a593Smuzhiyun 		return rc;
466*4882a593Smuzhiyun 	}
467*4882a593Smuzhiyun 
468*4882a593Smuzhiyun 	memcpy(enetaddr, buf, ETH_ALEN);
469*4882a593Smuzhiyun 	return 0;
470*4882a593Smuzhiyun }
471*4882a593Smuzhiyun 
mcs7830_write_mac_common(struct usb_device * udev,unsigned char enetaddr[])472*4882a593Smuzhiyun static int mcs7830_write_mac_common(struct usb_device *udev,
473*4882a593Smuzhiyun 				    unsigned char enetaddr[])
474*4882a593Smuzhiyun {
475*4882a593Smuzhiyun 	int rc;
476*4882a593Smuzhiyun 
477*4882a593Smuzhiyun 	debug("%s()\n", __func__);
478*4882a593Smuzhiyun 
479*4882a593Smuzhiyun 	rc = mcs7830_write_reg(udev, REG_ETHER_ADDR, ETH_ALEN, enetaddr);
480*4882a593Smuzhiyun 	if (rc < 0) {
481*4882a593Smuzhiyun 		debug("writing MAC to adapter failed\n");
482*4882a593Smuzhiyun 		return rc;
483*4882a593Smuzhiyun 	}
484*4882a593Smuzhiyun 	return 0;
485*4882a593Smuzhiyun }
486*4882a593Smuzhiyun 
mcs7830_init_common(struct usb_device * udev)487*4882a593Smuzhiyun static int mcs7830_init_common(struct usb_device *udev)
488*4882a593Smuzhiyun {
489*4882a593Smuzhiyun 	int timeout;
490*4882a593Smuzhiyun 	int have_link;
491*4882a593Smuzhiyun 
492*4882a593Smuzhiyun 	debug("%s()\n", __func__);
493*4882a593Smuzhiyun 
494*4882a593Smuzhiyun 	timeout = 0;
495*4882a593Smuzhiyun 	do {
496*4882a593Smuzhiyun 		have_link = mcs7830_read_phy(udev, MII_BMSR) & BMSR_LSTATUS;
497*4882a593Smuzhiyun 		if (have_link)
498*4882a593Smuzhiyun 			break;
499*4882a593Smuzhiyun 		udelay(LINKSTATUS_TIMEOUT_RES * 1000);
500*4882a593Smuzhiyun 		timeout += LINKSTATUS_TIMEOUT_RES;
501*4882a593Smuzhiyun 	} while (timeout < LINKSTATUS_TIMEOUT);
502*4882a593Smuzhiyun 	if (!have_link) {
503*4882a593Smuzhiyun 		debug("ethernet link is down\n");
504*4882a593Smuzhiyun 		return -ETIMEDOUT;
505*4882a593Smuzhiyun 	}
506*4882a593Smuzhiyun 	return 0;
507*4882a593Smuzhiyun }
508*4882a593Smuzhiyun 
mcs7830_send_common(struct ueth_data * ueth,void * packet,int length)509*4882a593Smuzhiyun static int mcs7830_send_common(struct ueth_data *ueth, void *packet,
510*4882a593Smuzhiyun 			       int length)
511*4882a593Smuzhiyun {
512*4882a593Smuzhiyun 	struct usb_device *udev = ueth->pusb_dev;
513*4882a593Smuzhiyun 	int rc;
514*4882a593Smuzhiyun 	int gotlen;
515*4882a593Smuzhiyun 	/* there is a status byte after the ethernet frame */
516*4882a593Smuzhiyun 	ALLOC_CACHE_ALIGN_BUFFER(uint8_t, buf, PKTSIZE + sizeof(uint8_t));
517*4882a593Smuzhiyun 
518*4882a593Smuzhiyun 	memcpy(buf, packet, length);
519*4882a593Smuzhiyun 	rc = usb_bulk_msg(udev,
520*4882a593Smuzhiyun 			  usb_sndbulkpipe(udev, ueth->ep_out),
521*4882a593Smuzhiyun 			  &buf[0], length, &gotlen,
522*4882a593Smuzhiyun 			  USBCALL_TIMEOUT);
523*4882a593Smuzhiyun 	debug("%s() TX want len %d, got len %d, rc %d\n",
524*4882a593Smuzhiyun 	      __func__, length, gotlen, rc);
525*4882a593Smuzhiyun 	return rc;
526*4882a593Smuzhiyun }
527*4882a593Smuzhiyun 
mcs7830_recv_common(struct ueth_data * ueth,uint8_t * buf)528*4882a593Smuzhiyun static int mcs7830_recv_common(struct ueth_data *ueth, uint8_t *buf)
529*4882a593Smuzhiyun {
530*4882a593Smuzhiyun 	int rc, wantlen, gotlen;
531*4882a593Smuzhiyun 	uint8_t sts;
532*4882a593Smuzhiyun 
533*4882a593Smuzhiyun 	debug("%s()\n", __func__);
534*4882a593Smuzhiyun 
535*4882a593Smuzhiyun 	/* fetch input data from the adapter */
536*4882a593Smuzhiyun 	wantlen = MCS7830_RX_URB_SIZE;
537*4882a593Smuzhiyun 	rc = usb_bulk_msg(ueth->pusb_dev,
538*4882a593Smuzhiyun 			  usb_rcvbulkpipe(ueth->pusb_dev, ueth->ep_in),
539*4882a593Smuzhiyun 			  &buf[0], wantlen, &gotlen,
540*4882a593Smuzhiyun 			  USBCALL_TIMEOUT);
541*4882a593Smuzhiyun 	debug("%s() RX want len %d, got len %d, rc %d\n",
542*4882a593Smuzhiyun 	      __func__, wantlen, gotlen, rc);
543*4882a593Smuzhiyun 	if (rc != 0) {
544*4882a593Smuzhiyun 		pr_err("RX: failed to receive\n");
545*4882a593Smuzhiyun 		return rc;
546*4882a593Smuzhiyun 	}
547*4882a593Smuzhiyun 	if (gotlen > wantlen) {
548*4882a593Smuzhiyun 		pr_err("RX: got too many bytes (%d)\n", gotlen);
549*4882a593Smuzhiyun 		return -EIO;
550*4882a593Smuzhiyun 	}
551*4882a593Smuzhiyun 
552*4882a593Smuzhiyun 	/*
553*4882a593Smuzhiyun 	 * the bulk message that we received from USB contains exactly
554*4882a593Smuzhiyun 	 * one ethernet frame and a trailing status byte
555*4882a593Smuzhiyun 	 */
556*4882a593Smuzhiyun 	if (gotlen < sizeof(sts))
557*4882a593Smuzhiyun 		return -EIO;
558*4882a593Smuzhiyun 	gotlen -= sizeof(sts);
559*4882a593Smuzhiyun 	sts = buf[gotlen];
560*4882a593Smuzhiyun 
561*4882a593Smuzhiyun 	if (sts == STAT_RX_FRAME_CORRECT) {
562*4882a593Smuzhiyun 		debug("%s() got a frame, len=%d\n", __func__, gotlen);
563*4882a593Smuzhiyun 		return gotlen;
564*4882a593Smuzhiyun 	}
565*4882a593Smuzhiyun 
566*4882a593Smuzhiyun 	debug("RX: frame error (sts 0x%02X, %s %s %s %s %s)\n",
567*4882a593Smuzhiyun 	      sts,
568*4882a593Smuzhiyun 	      (sts & STAT_RX_LARGE_FRAME) ? "large" : "-",
569*4882a593Smuzhiyun 	      (sts & STAT_RX_LENGTH_ERROR) ?  "length" : "-",
570*4882a593Smuzhiyun 	      (sts & STAT_RX_SHORT_FRAME) ? "short" : "-",
571*4882a593Smuzhiyun 	      (sts & STAT_RX_CRC_ERROR) ? "crc" : "-",
572*4882a593Smuzhiyun 	      (sts & STAT_RX_ALIGNMENT_ERROR) ?  "align" : "-");
573*4882a593Smuzhiyun 	return -EIO;
574*4882a593Smuzhiyun }
575*4882a593Smuzhiyun 
576*4882a593Smuzhiyun #ifndef CONFIG_DM_ETH
577*4882a593Smuzhiyun /*
578*4882a593Smuzhiyun  * mcs7830_init() - network interface's init callback
579*4882a593Smuzhiyun  * @udev:	network device to initialize
580*4882a593Smuzhiyun  * @bd:		board information
581*4882a593Smuzhiyun  * Return: zero upon success, negative upon error
582*4882a593Smuzhiyun  *
583*4882a593Smuzhiyun  * after initial setup during probe() and get_info(), this init() callback
584*4882a593Smuzhiyun  * ensures that the link is up and subsequent send() and recv() calls can
585*4882a593Smuzhiyun  * exchange ethernet frames
586*4882a593Smuzhiyun  */
mcs7830_init(struct eth_device * eth,bd_t * bd)587*4882a593Smuzhiyun static int mcs7830_init(struct eth_device *eth, bd_t *bd)
588*4882a593Smuzhiyun {
589*4882a593Smuzhiyun 	struct ueth_data *dev = eth->priv;
590*4882a593Smuzhiyun 
591*4882a593Smuzhiyun 	return mcs7830_init_common(dev->pusb_dev);
592*4882a593Smuzhiyun }
593*4882a593Smuzhiyun 
594*4882a593Smuzhiyun /*
595*4882a593Smuzhiyun  * mcs7830_send() - network interface's send callback
596*4882a593Smuzhiyun  * @eth:	network device to send the frame from
597*4882a593Smuzhiyun  * @packet:	ethernet frame content
598*4882a593Smuzhiyun  * @length:	ethernet frame length
599*4882a593Smuzhiyun  * Return: zero upon success, negative upon error
600*4882a593Smuzhiyun  *
601*4882a593Smuzhiyun  * this routine send an ethernet frame out of the network interface
602*4882a593Smuzhiyun  */
mcs7830_send(struct eth_device * eth,void * packet,int length)603*4882a593Smuzhiyun static int mcs7830_send(struct eth_device *eth, void *packet, int length)
604*4882a593Smuzhiyun {
605*4882a593Smuzhiyun 	struct ueth_data *dev = eth->priv;
606*4882a593Smuzhiyun 
607*4882a593Smuzhiyun 	return mcs7830_send_common(dev, packet, length);
608*4882a593Smuzhiyun }
609*4882a593Smuzhiyun 
610*4882a593Smuzhiyun /*
611*4882a593Smuzhiyun  * mcs7830_recv() - network interface's recv callback
612*4882a593Smuzhiyun  * @eth:	network device to receive frames from
613*4882a593Smuzhiyun  * Return: zero upon success, negative upon error
614*4882a593Smuzhiyun  *
615*4882a593Smuzhiyun  * this routine checks for available ethernet frames that the network
616*4882a593Smuzhiyun  * interface might have received, and notifies the network stack
617*4882a593Smuzhiyun  */
mcs7830_recv(struct eth_device * eth)618*4882a593Smuzhiyun static int mcs7830_recv(struct eth_device *eth)
619*4882a593Smuzhiyun {
620*4882a593Smuzhiyun 	ALLOC_CACHE_ALIGN_BUFFER(uint8_t, buf, MCS7830_RX_URB_SIZE);
621*4882a593Smuzhiyun 	struct ueth_data *ueth = eth->priv;
622*4882a593Smuzhiyun 	int len;
623*4882a593Smuzhiyun 
624*4882a593Smuzhiyun 	len = mcs7830_recv_common(ueth, buf);
625*4882a593Smuzhiyun 	if (len >= 0) {
626*4882a593Smuzhiyun 		net_process_received_packet(buf, len);
627*4882a593Smuzhiyun 		return 0;
628*4882a593Smuzhiyun 	}
629*4882a593Smuzhiyun 
630*4882a593Smuzhiyun 	return len;
631*4882a593Smuzhiyun }
632*4882a593Smuzhiyun 
633*4882a593Smuzhiyun /*
634*4882a593Smuzhiyun  * mcs7830_halt() - network interface's halt callback
635*4882a593Smuzhiyun  * @eth:	network device to cease operation of
636*4882a593Smuzhiyun  * Return: none
637*4882a593Smuzhiyun  *
638*4882a593Smuzhiyun  * this routine is supposed to undo the effect of previous initialization and
639*4882a593Smuzhiyun  * ethernet frames exchange; in this implementation it's a NOP
640*4882a593Smuzhiyun  */
mcs7830_halt(struct eth_device * eth)641*4882a593Smuzhiyun static void mcs7830_halt(struct eth_device *eth)
642*4882a593Smuzhiyun {
643*4882a593Smuzhiyun 	debug("%s()\n", __func__);
644*4882a593Smuzhiyun }
645*4882a593Smuzhiyun 
646*4882a593Smuzhiyun /*
647*4882a593Smuzhiyun  * mcs7830_write_mac() - write an ethernet adapter's MAC address
648*4882a593Smuzhiyun  * @eth:	network device to write to
649*4882a593Smuzhiyun  * Return: zero upon success, negative upon error
650*4882a593Smuzhiyun  *
651*4882a593Smuzhiyun  * this routine takes the MAC address from the ethernet interface's data
652*4882a593Smuzhiyun  * structure, and writes it into the ethernet adapter such that subsequent
653*4882a593Smuzhiyun  * exchange of ethernet frames uses this address
654*4882a593Smuzhiyun  */
mcs7830_write_mac(struct eth_device * eth)655*4882a593Smuzhiyun static int mcs7830_write_mac(struct eth_device *eth)
656*4882a593Smuzhiyun {
657*4882a593Smuzhiyun 	struct ueth_data *ueth = eth->priv;
658*4882a593Smuzhiyun 
659*4882a593Smuzhiyun 	return mcs7830_write_mac_common(ueth->pusb_dev, eth->enetaddr);
660*4882a593Smuzhiyun }
661*4882a593Smuzhiyun 
662*4882a593Smuzhiyun /*
663*4882a593Smuzhiyun  * mcs7830_iface_idx - index of detected network interfaces
664*4882a593Smuzhiyun  *
665*4882a593Smuzhiyun  * this counter keeps track of identified supported interfaces,
666*4882a593Smuzhiyun  * to assign unique names as more interfaces are found
667*4882a593Smuzhiyun  */
668*4882a593Smuzhiyun static int mcs7830_iface_idx;
669*4882a593Smuzhiyun 
670*4882a593Smuzhiyun /*
671*4882a593Smuzhiyun  * mcs7830_eth_before_probe() - network driver's before_probe callback
672*4882a593Smuzhiyun  * Return: none
673*4882a593Smuzhiyun  *
674*4882a593Smuzhiyun  * this routine initializes driver's internal data in preparation of
675*4882a593Smuzhiyun  * subsequent probe callbacks
676*4882a593Smuzhiyun  */
mcs7830_eth_before_probe(void)677*4882a593Smuzhiyun void mcs7830_eth_before_probe(void)
678*4882a593Smuzhiyun {
679*4882a593Smuzhiyun 	mcs7830_iface_idx = 0;
680*4882a593Smuzhiyun }
681*4882a593Smuzhiyun 
682*4882a593Smuzhiyun /*
683*4882a593Smuzhiyun  * struct mcs7830_dongle - description of a supported Moschip ethernet dongle
684*4882a593Smuzhiyun  * @vendor:	16bit USB vendor identification
685*4882a593Smuzhiyun  * @product:	16bit USB product identification
686*4882a593Smuzhiyun  *
687*4882a593Smuzhiyun  * this structure describes a supported USB ethernet dongle by means of the
688*4882a593Smuzhiyun  * vendor and product codes found during USB enumeration; no flags are held
689*4882a593Smuzhiyun  * here since all supported dongles have identical behaviour, and required
690*4882a593Smuzhiyun  * fixups get determined at runtime, such that no manual configuration is
691*4882a593Smuzhiyun  * needed
692*4882a593Smuzhiyun  */
693*4882a593Smuzhiyun struct mcs7830_dongle {
694*4882a593Smuzhiyun 	uint16_t vendor;
695*4882a593Smuzhiyun 	uint16_t product;
696*4882a593Smuzhiyun };
697*4882a593Smuzhiyun 
698*4882a593Smuzhiyun /*
699*4882a593Smuzhiyun  * mcs7830_dongles - the list of supported Moschip based USB ethernet dongles
700*4882a593Smuzhiyun  */
701*4882a593Smuzhiyun static const struct mcs7830_dongle mcs7830_dongles[] = {
702*4882a593Smuzhiyun 	{ 0x9710, 0x7832, },	/* Moschip 7832 */
703*4882a593Smuzhiyun 	{ 0x9710, 0x7830, },	/* Moschip 7830 */
704*4882a593Smuzhiyun 	{ 0x9710, 0x7730, },	/* Moschip 7730 */
705*4882a593Smuzhiyun 	{ 0x0df6, 0x0021, },	/* Sitecom LN 30 */
706*4882a593Smuzhiyun };
707*4882a593Smuzhiyun 
708*4882a593Smuzhiyun /*
709*4882a593Smuzhiyun  * mcs7830_eth_probe() - network driver's probe callback
710*4882a593Smuzhiyun  * @dev:	detected USB device to check
711*4882a593Smuzhiyun  * @ifnum:	detected USB interface to check
712*4882a593Smuzhiyun  * @ss:		USB ethernet data structure to fill in upon match
713*4882a593Smuzhiyun  * Return: #1 upon match, #0 upon mismatch or error
714*4882a593Smuzhiyun  *
715*4882a593Smuzhiyun  * this routine checks whether the found USB device is supported by
716*4882a593Smuzhiyun  * this ethernet driver, and upon match fills in the USB ethernet
717*4882a593Smuzhiyun  * data structure which later is passed to the get_info callback
718*4882a593Smuzhiyun  */
mcs7830_eth_probe(struct usb_device * dev,unsigned int ifnum,struct ueth_data * ss)719*4882a593Smuzhiyun int mcs7830_eth_probe(struct usb_device *dev, unsigned int ifnum,
720*4882a593Smuzhiyun 		      struct ueth_data *ss)
721*4882a593Smuzhiyun {
722*4882a593Smuzhiyun 	struct usb_interface *iface;
723*4882a593Smuzhiyun 	struct usb_interface_descriptor *iface_desc;
724*4882a593Smuzhiyun 	int i;
725*4882a593Smuzhiyun 	struct mcs7830_private *priv;
726*4882a593Smuzhiyun 	int ep_in_found, ep_out_found, ep_intr_found;
727*4882a593Smuzhiyun 
728*4882a593Smuzhiyun 	debug("%s()\n", __func__);
729*4882a593Smuzhiyun 
730*4882a593Smuzhiyun 	/* iterate the list of supported dongles */
731*4882a593Smuzhiyun 	iface = &dev->config.if_desc[ifnum];
732*4882a593Smuzhiyun 	iface_desc = &iface->desc;
733*4882a593Smuzhiyun 	for (i = 0; i < ARRAY_SIZE(mcs7830_dongles); i++) {
734*4882a593Smuzhiyun 		if (dev->descriptor.idVendor == mcs7830_dongles[i].vendor &&
735*4882a593Smuzhiyun 		    dev->descriptor.idProduct == mcs7830_dongles[i].product)
736*4882a593Smuzhiyun 			break;
737*4882a593Smuzhiyun 	}
738*4882a593Smuzhiyun 	if (i == ARRAY_SIZE(mcs7830_dongles))
739*4882a593Smuzhiyun 		return 0;
740*4882a593Smuzhiyun 	debug("detected USB ethernet device: %04X:%04X\n",
741*4882a593Smuzhiyun 	      dev->descriptor.idVendor, dev->descriptor.idProduct);
742*4882a593Smuzhiyun 
743*4882a593Smuzhiyun 	/* fill in driver private data */
744*4882a593Smuzhiyun 	priv = calloc(1, sizeof(*priv));
745*4882a593Smuzhiyun 	if (!priv)
746*4882a593Smuzhiyun 		return 0;
747*4882a593Smuzhiyun 
748*4882a593Smuzhiyun 	/* fill in the ueth_data structure, attach private data */
749*4882a593Smuzhiyun 	memset(ss, 0, sizeof(*ss));
750*4882a593Smuzhiyun 	ss->ifnum = ifnum;
751*4882a593Smuzhiyun 	ss->pusb_dev = dev;
752*4882a593Smuzhiyun 	ss->subclass = iface_desc->bInterfaceSubClass;
753*4882a593Smuzhiyun 	ss->protocol = iface_desc->bInterfaceProtocol;
754*4882a593Smuzhiyun 	ss->dev_priv = priv;
755*4882a593Smuzhiyun 
756*4882a593Smuzhiyun 	/*
757*4882a593Smuzhiyun 	 * a minimum of three endpoints is expected: in (bulk),
758*4882a593Smuzhiyun 	 * out (bulk), and interrupt; ignore all others
759*4882a593Smuzhiyun 	 */
760*4882a593Smuzhiyun 	ep_in_found = ep_out_found = ep_intr_found = 0;
761*4882a593Smuzhiyun 	for (i = 0; i < iface_desc->bNumEndpoints; i++) {
762*4882a593Smuzhiyun 		uint8_t eptype, epaddr;
763*4882a593Smuzhiyun 		bool is_input;
764*4882a593Smuzhiyun 
765*4882a593Smuzhiyun 		eptype = iface->ep_desc[i].bmAttributes;
766*4882a593Smuzhiyun 		eptype &= USB_ENDPOINT_XFERTYPE_MASK;
767*4882a593Smuzhiyun 
768*4882a593Smuzhiyun 		epaddr = iface->ep_desc[i].bEndpointAddress;
769*4882a593Smuzhiyun 		is_input = epaddr & USB_DIR_IN;
770*4882a593Smuzhiyun 		epaddr &= USB_ENDPOINT_NUMBER_MASK;
771*4882a593Smuzhiyun 
772*4882a593Smuzhiyun 		if (eptype == USB_ENDPOINT_XFER_BULK) {
773*4882a593Smuzhiyun 			if (is_input && !ep_in_found) {
774*4882a593Smuzhiyun 				ss->ep_in = epaddr;
775*4882a593Smuzhiyun 				ep_in_found++;
776*4882a593Smuzhiyun 			}
777*4882a593Smuzhiyun 			if (!is_input && !ep_out_found) {
778*4882a593Smuzhiyun 				ss->ep_out = epaddr;
779*4882a593Smuzhiyun 				ep_out_found++;
780*4882a593Smuzhiyun 			}
781*4882a593Smuzhiyun 		}
782*4882a593Smuzhiyun 
783*4882a593Smuzhiyun 		if (eptype == USB_ENDPOINT_XFER_INT) {
784*4882a593Smuzhiyun 			if (is_input && !ep_intr_found) {
785*4882a593Smuzhiyun 				ss->ep_int = epaddr;
786*4882a593Smuzhiyun 				ss->irqinterval = iface->ep_desc[i].bInterval;
787*4882a593Smuzhiyun 				ep_intr_found++;
788*4882a593Smuzhiyun 			}
789*4882a593Smuzhiyun 		}
790*4882a593Smuzhiyun 	}
791*4882a593Smuzhiyun 	debug("endpoints: in %d, out %d, intr %d\n",
792*4882a593Smuzhiyun 	      ss->ep_in, ss->ep_out, ss->ep_int);
793*4882a593Smuzhiyun 
794*4882a593Smuzhiyun 	/* apply basic sanity checks */
795*4882a593Smuzhiyun 	if (usb_set_interface(dev, iface_desc->bInterfaceNumber, 0) ||
796*4882a593Smuzhiyun 	    !ss->ep_in || !ss->ep_out || !ss->ep_int) {
797*4882a593Smuzhiyun 		debug("device probe incomplete\n");
798*4882a593Smuzhiyun 		return 0;
799*4882a593Smuzhiyun 	}
800*4882a593Smuzhiyun 
801*4882a593Smuzhiyun 	dev->privptr = ss;
802*4882a593Smuzhiyun 	return 1;
803*4882a593Smuzhiyun }
804*4882a593Smuzhiyun 
805*4882a593Smuzhiyun /*
806*4882a593Smuzhiyun  * mcs7830_eth_get_info() - network driver's get_info callback
807*4882a593Smuzhiyun  * @dev:	detected USB device
808*4882a593Smuzhiyun  * @ss:		USB ethernet data structure filled in at probe()
809*4882a593Smuzhiyun  * @eth:	ethernet interface data structure to fill in
810*4882a593Smuzhiyun  * Return: #1 upon success, #0 upon error
811*4882a593Smuzhiyun  *
812*4882a593Smuzhiyun  * this routine registers the mandatory init(), send(), recv(), and
813*4882a593Smuzhiyun  * halt() callbacks with the ethernet interface, can register the
814*4882a593Smuzhiyun  * optional write_hwaddr() callback with the ethernet interface,
815*4882a593Smuzhiyun  * and initiates configuration of the interface such that subsequent
816*4882a593Smuzhiyun  * calls to those callbacks results in network communication
817*4882a593Smuzhiyun  */
mcs7830_eth_get_info(struct usb_device * dev,struct ueth_data * ss,struct eth_device * eth)818*4882a593Smuzhiyun int mcs7830_eth_get_info(struct usb_device *dev, struct ueth_data *ss,
819*4882a593Smuzhiyun 			 struct eth_device *eth)
820*4882a593Smuzhiyun {
821*4882a593Smuzhiyun 	debug("%s()\n", __func__);
822*4882a593Smuzhiyun 	if (!eth) {
823*4882a593Smuzhiyun 		debug("%s: missing parameter.\n", __func__);
824*4882a593Smuzhiyun 		return 0;
825*4882a593Smuzhiyun 	}
826*4882a593Smuzhiyun 
827*4882a593Smuzhiyun 	snprintf(eth->name, sizeof(eth->name), "%s%d",
828*4882a593Smuzhiyun 		 MCS7830_BASE_NAME, mcs7830_iface_idx++);
829*4882a593Smuzhiyun 	eth->init = mcs7830_init;
830*4882a593Smuzhiyun 	eth->send = mcs7830_send;
831*4882a593Smuzhiyun 	eth->recv = mcs7830_recv;
832*4882a593Smuzhiyun 	eth->halt = mcs7830_halt;
833*4882a593Smuzhiyun 	eth->write_hwaddr = mcs7830_write_mac;
834*4882a593Smuzhiyun 	eth->priv = ss;
835*4882a593Smuzhiyun 
836*4882a593Smuzhiyun 	if (mcs7830_basic_reset(ss->pusb_dev, ss->dev_priv))
837*4882a593Smuzhiyun 		return 0;
838*4882a593Smuzhiyun 
839*4882a593Smuzhiyun 	if (mcs7830_read_mac(ss->pusb_dev, eth->enetaddr))
840*4882a593Smuzhiyun 		return 0;
841*4882a593Smuzhiyun 	debug("MAC %pM\n", eth->enetaddr);
842*4882a593Smuzhiyun 
843*4882a593Smuzhiyun 	return 1;
844*4882a593Smuzhiyun }
845*4882a593Smuzhiyun #endif
846*4882a593Smuzhiyun 
847*4882a593Smuzhiyun 
848*4882a593Smuzhiyun #ifdef CONFIG_DM_ETH
mcs7830_eth_start(struct udevice * dev)849*4882a593Smuzhiyun static int mcs7830_eth_start(struct udevice *dev)
850*4882a593Smuzhiyun {
851*4882a593Smuzhiyun 	struct usb_device *udev = dev_get_parent_priv(dev);
852*4882a593Smuzhiyun 
853*4882a593Smuzhiyun 	return mcs7830_init_common(udev);
854*4882a593Smuzhiyun }
855*4882a593Smuzhiyun 
mcs7830_eth_stop(struct udevice * dev)856*4882a593Smuzhiyun void mcs7830_eth_stop(struct udevice *dev)
857*4882a593Smuzhiyun {
858*4882a593Smuzhiyun 	debug("** %s()\n", __func__);
859*4882a593Smuzhiyun }
860*4882a593Smuzhiyun 
mcs7830_eth_send(struct udevice * dev,void * packet,int length)861*4882a593Smuzhiyun int mcs7830_eth_send(struct udevice *dev, void *packet, int length)
862*4882a593Smuzhiyun {
863*4882a593Smuzhiyun 	struct mcs7830_private *priv = dev_get_priv(dev);
864*4882a593Smuzhiyun 	struct ueth_data *ueth = &priv->ueth;
865*4882a593Smuzhiyun 
866*4882a593Smuzhiyun 	return mcs7830_send_common(ueth, packet, length);
867*4882a593Smuzhiyun }
868*4882a593Smuzhiyun 
mcs7830_eth_recv(struct udevice * dev,int flags,uchar ** packetp)869*4882a593Smuzhiyun int mcs7830_eth_recv(struct udevice *dev, int flags, uchar **packetp)
870*4882a593Smuzhiyun {
871*4882a593Smuzhiyun 	struct mcs7830_private *priv = dev_get_priv(dev);
872*4882a593Smuzhiyun 	struct ueth_data *ueth = &priv->ueth;
873*4882a593Smuzhiyun 	int len;
874*4882a593Smuzhiyun 
875*4882a593Smuzhiyun 	len = mcs7830_recv_common(ueth, priv->rx_buf);
876*4882a593Smuzhiyun 	*packetp = priv->rx_buf;
877*4882a593Smuzhiyun 
878*4882a593Smuzhiyun 	return len;
879*4882a593Smuzhiyun }
880*4882a593Smuzhiyun 
mcs7830_free_pkt(struct udevice * dev,uchar * packet,int packet_len)881*4882a593Smuzhiyun static int mcs7830_free_pkt(struct udevice *dev, uchar *packet, int packet_len)
882*4882a593Smuzhiyun {
883*4882a593Smuzhiyun 	struct mcs7830_private *priv = dev_get_priv(dev);
884*4882a593Smuzhiyun 
885*4882a593Smuzhiyun 	packet_len = ALIGN(packet_len, 4);
886*4882a593Smuzhiyun 	usb_ether_advance_rxbuf(&priv->ueth, sizeof(u32) + packet_len);
887*4882a593Smuzhiyun 
888*4882a593Smuzhiyun 	return 0;
889*4882a593Smuzhiyun }
890*4882a593Smuzhiyun 
mcs7830_write_hwaddr(struct udevice * dev)891*4882a593Smuzhiyun int mcs7830_write_hwaddr(struct udevice *dev)
892*4882a593Smuzhiyun {
893*4882a593Smuzhiyun 	struct usb_device *udev = dev_get_parent_priv(dev);
894*4882a593Smuzhiyun 	struct eth_pdata *pdata = dev_get_platdata(dev);
895*4882a593Smuzhiyun 
896*4882a593Smuzhiyun 	return mcs7830_write_mac_common(udev, pdata->enetaddr);
897*4882a593Smuzhiyun }
898*4882a593Smuzhiyun 
mcs7830_eth_probe(struct udevice * dev)899*4882a593Smuzhiyun static int mcs7830_eth_probe(struct udevice *dev)
900*4882a593Smuzhiyun {
901*4882a593Smuzhiyun 	struct usb_device *udev = dev_get_parent_priv(dev);
902*4882a593Smuzhiyun 	struct mcs7830_private *priv = dev_get_priv(dev);
903*4882a593Smuzhiyun 	struct eth_pdata *pdata = dev_get_platdata(dev);
904*4882a593Smuzhiyun 	struct ueth_data *ueth = &priv->ueth;
905*4882a593Smuzhiyun 
906*4882a593Smuzhiyun 	if (mcs7830_basic_reset(udev, priv))
907*4882a593Smuzhiyun 		return 0;
908*4882a593Smuzhiyun 
909*4882a593Smuzhiyun 	if (mcs7830_read_mac(udev, pdata->enetaddr))
910*4882a593Smuzhiyun 		return 0;
911*4882a593Smuzhiyun 
912*4882a593Smuzhiyun 	return usb_ether_register(dev, ueth, MCS7830_RX_URB_SIZE);
913*4882a593Smuzhiyun }
914*4882a593Smuzhiyun 
915*4882a593Smuzhiyun static const struct eth_ops mcs7830_eth_ops = {
916*4882a593Smuzhiyun 	.start	= mcs7830_eth_start,
917*4882a593Smuzhiyun 	.send	= mcs7830_eth_send,
918*4882a593Smuzhiyun 	.recv	= mcs7830_eth_recv,
919*4882a593Smuzhiyun 	.free_pkt = mcs7830_free_pkt,
920*4882a593Smuzhiyun 	.stop	= mcs7830_eth_stop,
921*4882a593Smuzhiyun 	.write_hwaddr = mcs7830_write_hwaddr,
922*4882a593Smuzhiyun };
923*4882a593Smuzhiyun 
924*4882a593Smuzhiyun U_BOOT_DRIVER(mcs7830_eth) = {
925*4882a593Smuzhiyun 	.name	= "mcs7830_eth",
926*4882a593Smuzhiyun 	.id	= UCLASS_ETH,
927*4882a593Smuzhiyun 	.probe = mcs7830_eth_probe,
928*4882a593Smuzhiyun 	.ops	= &mcs7830_eth_ops,
929*4882a593Smuzhiyun 	.priv_auto_alloc_size = sizeof(struct mcs7830_private),
930*4882a593Smuzhiyun 	.platdata_auto_alloc_size = sizeof(struct eth_pdata),
931*4882a593Smuzhiyun 	.flags	= DM_FLAG_ALLOC_PRIV_DMA,
932*4882a593Smuzhiyun };
933*4882a593Smuzhiyun 
934*4882a593Smuzhiyun static const struct usb_device_id mcs7830_eth_id_table[] = {
935*4882a593Smuzhiyun 	{ USB_DEVICE(0x9710, 0x7832) },		/* Moschip 7832 */
936*4882a593Smuzhiyun 	{ USB_DEVICE(0x9710, 0x7830), },	/* Moschip 7830 */
937*4882a593Smuzhiyun 	{ USB_DEVICE(0x9710, 0x7730), },	/* Moschip 7730 */
938*4882a593Smuzhiyun 	{ USB_DEVICE(0x0df6, 0x0021), },	/* Sitecom LN 30 */
939*4882a593Smuzhiyun 	{ }		/* Terminating entry */
940*4882a593Smuzhiyun };
941*4882a593Smuzhiyun 
942*4882a593Smuzhiyun U_BOOT_USB_DEVICE(mcs7830_eth, mcs7830_eth_id_table);
943*4882a593Smuzhiyun #endif
944