blob: c829a436a34cc9baf13c599f46bb90e01e9c0e9d [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Gerhard Sittigdf4fb1c2014-03-08 19:46:14 +01002/*
3 * Copyright (c) 2013 Gerhard Sittig <gsi@denx.de>
4 * based on the U-Boot Asix driver as well as information
5 * from the Linux Moschip driver
Gerhard Sittigdf4fb1c2014-03-08 19:46:14 +01006 */
7
8/*
9 * MOSCHIP MCS7830 based (7730/7830/7832) USB 2.0 Ethernet Devices
10 */
11
12#include <common.h>
Simon Glassd4f847e2015-11-29 13:18:11 -070013#include <dm.h>
Gerhard Sittigdf4fb1c2014-03-08 19:46:14 +010014#include <errno.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -060015#include <log.h>
Simon Glass90526e92020-05-10 11:39:56 -060016#include <net.h>
Simon Glassc05ed002020-05-10 11:40:11 -060017#include <linux/delay.h>
Gerhard Sittigdf4fb1c2014-03-08 19:46:14 +010018#include <linux/mii.h>
19#include <malloc.h>
Simon Glasscf92e052015-09-02 17:24:58 -060020#include <memalign.h>
Gerhard Sittigdf4fb1c2014-03-08 19:46:14 +010021#include <usb.h>
22
23#include "usb_ether.h"
24
25#define MCS7830_BASE_NAME "mcs"
26
27#define USBCALL_TIMEOUT 1000
28#define LINKSTATUS_TIMEOUT 5000 /* link status, connect timeout */
29#define LINKSTATUS_TIMEOUT_RES 50 /* link status, resolution in msec */
30
31#define MCS7830_RX_URB_SIZE 2048
32
33/* command opcodes */
34#define MCS7830_WR_BREQ 0x0d
35#define MCS7830_RD_BREQ 0x0e
36
37/* register layout, numerical offset specs for USB API calls */
38struct mcs7830_regs {
39 uint8_t multicast_hashes[8];
40 uint8_t packet_gap[2];
41 uint8_t phy_data[2];
42 uint8_t phy_command[2];
43 uint8_t configuration;
44 uint8_t ether_address[6];
45 uint8_t frame_drop_count;
46 uint8_t pause_threshold;
47};
48#define REG_MULTICAST_HASH offsetof(struct mcs7830_regs, multicast_hashes)
49#define REG_PHY_DATA offsetof(struct mcs7830_regs, phy_data)
50#define REG_PHY_CMD offsetof(struct mcs7830_regs, phy_command)
51#define REG_CONFIG offsetof(struct mcs7830_regs, configuration)
52#define REG_ETHER_ADDR offsetof(struct mcs7830_regs, ether_address)
53#define REG_FRAME_DROP_COUNTER offsetof(struct mcs7830_regs, frame_drop_count)
54#define REG_PAUSE_THRESHOLD offsetof(struct mcs7830_regs, pause_threshold)
55
56/* bit masks and default values for the above registers */
57#define PHY_CMD1_READ 0x40
58#define PHY_CMD1_WRITE 0x20
59#define PHY_CMD1_PHYADDR 0x01
60
61#define PHY_CMD2_PEND 0x80
62#define PHY_CMD2_READY 0x40
63
64#define CONF_CFG 0x80
65#define CONF_SPEED100 0x40
66#define CONF_FDX_ENABLE 0x20
67#define CONF_RXENABLE 0x10
68#define CONF_TXENABLE 0x08
69#define CONF_SLEEPMODE 0x04
70#define CONF_ALLMULTICAST 0x02
71#define CONF_PROMISCUOUS 0x01
72
73#define PAUSE_THRESHOLD_DEFAULT 0
74
75/* bit masks for the status byte which follows received ethernet frames */
76#define STAT_RX_FRAME_CORRECT 0x20
77#define STAT_RX_LARGE_FRAME 0x10
78#define STAT_RX_CRC_ERROR 0x08
79#define STAT_RX_ALIGNMENT_ERROR 0x04
80#define STAT_RX_LENGTH_ERROR 0x02
81#define STAT_RX_SHORT_FRAME 0x01
82
83/*
84 * struct mcs7830_private - private driver data for an individual adapter
85 * @config: shadow for the network adapter's configuration register
86 * @mchash: shadow for the network adapter's multicast hash registers
87 */
88struct mcs7830_private {
Simon Glassd4f847e2015-11-29 13:18:11 -070089#ifdef CONFIG_DM_ETH
90 uint8_t rx_buf[MCS7830_RX_URB_SIZE];
91 struct ueth_data ueth;
92#endif
Gerhard Sittigdf4fb1c2014-03-08 19:46:14 +010093 uint8_t config;
94 uint8_t mchash[8];
95};
96
97/*
98 * mcs7830_read_reg() - read a register of the network adapter
Simon Glassce932c72015-11-29 13:18:10 -070099 * @udev: network device to read from
Gerhard Sittigdf4fb1c2014-03-08 19:46:14 +0100100 * @idx: index of the register to start reading from
101 * @size: number of bytes to read
102 * @data: buffer to read into
103 * Return: zero upon success, negative upon error
104 */
Simon Glassce932c72015-11-29 13:18:10 -0700105static int mcs7830_read_reg(struct usb_device *udev, uint8_t idx,
Gerhard Sittigdf4fb1c2014-03-08 19:46:14 +0100106 uint16_t size, void *data)
107{
108 int len;
109 ALLOC_CACHE_ALIGN_BUFFER(uint8_t, buf, size);
110
111 debug("%s() idx=0x%04X sz=%d\n", __func__, idx, size);
112
Simon Glassce932c72015-11-29 13:18:10 -0700113 len = usb_control_msg(udev,
114 usb_rcvctrlpipe(udev, 0),
Gerhard Sittigdf4fb1c2014-03-08 19:46:14 +0100115 MCS7830_RD_BREQ,
116 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
117 0, idx, buf, size,
118 USBCALL_TIMEOUT);
119 if (len != size) {
120 debug("%s() len=%d != sz=%d\n", __func__, len, size);
121 return -EIO;
122 }
123 memcpy(data, buf, size);
124 return 0;
125}
126
127/*
128 * mcs7830_write_reg() - write a register of the network adapter
Simon Glassce932c72015-11-29 13:18:10 -0700129 * @udev: network device to write to
Gerhard Sittigdf4fb1c2014-03-08 19:46:14 +0100130 * @idx: index of the register to start writing to
131 * @size: number of bytes to write
132 * @data: buffer holding the data to write
133 * Return: zero upon success, negative upon error
134 */
Simon Glassce932c72015-11-29 13:18:10 -0700135static int mcs7830_write_reg(struct usb_device *udev, uint8_t idx,
Gerhard Sittigdf4fb1c2014-03-08 19:46:14 +0100136 uint16_t size, void *data)
137{
138 int len;
139 ALLOC_CACHE_ALIGN_BUFFER(uint8_t, buf, size);
140
141 debug("%s() idx=0x%04X sz=%d\n", __func__, idx, size);
142
143 memcpy(buf, data, size);
Simon Glassce932c72015-11-29 13:18:10 -0700144 len = usb_control_msg(udev,
145 usb_sndctrlpipe(udev, 0),
Gerhard Sittigdf4fb1c2014-03-08 19:46:14 +0100146 MCS7830_WR_BREQ,
147 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
148 0, idx, buf, size,
149 USBCALL_TIMEOUT);
150 if (len != size) {
151 debug("%s() len=%d != sz=%d\n", __func__, len, size);
152 return -EIO;
153 }
154 return 0;
155}
156
157/*
158 * mcs7830_phy_emit_wait() - emit PHY read/write access, wait for its execution
Simon Glassce932c72015-11-29 13:18:10 -0700159 * @udev: network device to talk to
Gerhard Sittigdf4fb1c2014-03-08 19:46:14 +0100160 * @rwflag: PHY_CMD1_READ or PHY_CMD1_WRITE opcode
161 * @index: number of the PHY register to read or write
162 * Return: zero upon success, negative upon error
163 */
Simon Glassce932c72015-11-29 13:18:10 -0700164static int mcs7830_phy_emit_wait(struct usb_device *udev,
Gerhard Sittigdf4fb1c2014-03-08 19:46:14 +0100165 uint8_t rwflag, uint8_t index)
166{
167 int rc;
168 int retry;
169 uint8_t cmd[2];
170
171 /* send the PHY read/write request */
172 cmd[0] = rwflag | PHY_CMD1_PHYADDR;
173 cmd[1] = PHY_CMD2_PEND | (index & 0x1f);
Simon Glassce932c72015-11-29 13:18:10 -0700174 rc = mcs7830_write_reg(udev, REG_PHY_CMD, sizeof(cmd), cmd);
Gerhard Sittigdf4fb1c2014-03-08 19:46:14 +0100175 if (rc < 0)
176 return rc;
177
178 /* wait for the response to become available (usually < 1ms) */
179 retry = 10;
180 do {
Simon Glassce932c72015-11-29 13:18:10 -0700181 rc = mcs7830_read_reg(udev, REG_PHY_CMD, sizeof(cmd), cmd);
Gerhard Sittigdf4fb1c2014-03-08 19:46:14 +0100182 if (rc < 0)
183 return rc;
184 if (cmd[1] & PHY_CMD2_READY)
185 return 0;
186 if (!retry--)
187 return -ETIMEDOUT;
188 mdelay(1);
189 } while (1);
190 /* UNREACH */
191}
192
193/*
194 * mcs7830_read_phy() - read a PHY register of the network adapter
Simon Glassce932c72015-11-29 13:18:10 -0700195 * @udev: network device to read from
Gerhard Sittigdf4fb1c2014-03-08 19:46:14 +0100196 * @index: index of the PHY register to read from
197 * Return: non-negative 16bit register content, negative upon error
198 */
Simon Glassce932c72015-11-29 13:18:10 -0700199static int mcs7830_read_phy(struct usb_device *udev, uint8_t index)
Gerhard Sittigdf4fb1c2014-03-08 19:46:14 +0100200{
201 int rc;
202 uint16_t val;
203
204 /* issue the PHY read request and wait for its execution */
Simon Glassce932c72015-11-29 13:18:10 -0700205 rc = mcs7830_phy_emit_wait(udev, PHY_CMD1_READ, index);
Gerhard Sittigdf4fb1c2014-03-08 19:46:14 +0100206 if (rc < 0)
207 return rc;
208
209 /* fetch the PHY data which was read */
Simon Glassce932c72015-11-29 13:18:10 -0700210 rc = mcs7830_read_reg(udev, REG_PHY_DATA, sizeof(val), &val);
Gerhard Sittigdf4fb1c2014-03-08 19:46:14 +0100211 if (rc < 0)
212 return rc;
213 rc = le16_to_cpu(val);
Simon Glassce932c72015-11-29 13:18:10 -0700214 debug("%s(%d) => 0x%04X\n", __func__, index, rc);
Gerhard Sittigdf4fb1c2014-03-08 19:46:14 +0100215 return rc;
216}
217
218/*
219 * mcs7830_write_phy() - write a PHY register of the network adapter
Simon Glassce932c72015-11-29 13:18:10 -0700220 * @udev: network device to write to
Gerhard Sittigdf4fb1c2014-03-08 19:46:14 +0100221 * @index: index of the PHY register to write to
222 * @val: value to write to the PHY register
223 * Return: zero upon success, negative upon error
224 */
Simon Glassce932c72015-11-29 13:18:10 -0700225static int mcs7830_write_phy(struct usb_device *udev, uint8_t index,
226 uint16_t val)
Gerhard Sittigdf4fb1c2014-03-08 19:46:14 +0100227{
228 int rc;
229
Simon Glassce932c72015-11-29 13:18:10 -0700230 debug("%s(%d, 0x%04X)\n", __func__, index, val);
Gerhard Sittigdf4fb1c2014-03-08 19:46:14 +0100231
232 /* setup the PHY data which is to get written */
233 val = cpu_to_le16(val);
Simon Glassce932c72015-11-29 13:18:10 -0700234 rc = mcs7830_write_reg(udev, REG_PHY_DATA, sizeof(val), &val);
Gerhard Sittigdf4fb1c2014-03-08 19:46:14 +0100235 if (rc < 0)
236 return rc;
237
238 /* issue the PHY write request and wait for its execution */
Simon Glassce932c72015-11-29 13:18:10 -0700239 rc = mcs7830_phy_emit_wait(udev, PHY_CMD1_WRITE, index);
Gerhard Sittigdf4fb1c2014-03-08 19:46:14 +0100240 if (rc < 0)
241 return rc;
242
243 return 0;
244}
245
246/*
247 * mcs7830_write_config() - write to the network adapter's config register
Simon Glassce932c72015-11-29 13:18:10 -0700248 * @udev: network device to write to
249 * @priv: private data
Gerhard Sittigdf4fb1c2014-03-08 19:46:14 +0100250 * Return: zero upon success, negative upon error
251 *
252 * the data which gets written is taken from the shadow config register
253 * within the device driver's private data
254 */
Simon Glassce932c72015-11-29 13:18:10 -0700255static int mcs7830_write_config(struct usb_device *udev,
256 struct mcs7830_private *priv)
Gerhard Sittigdf4fb1c2014-03-08 19:46:14 +0100257{
Gerhard Sittigdf4fb1c2014-03-08 19:46:14 +0100258 int rc;
259
260 debug("%s()\n", __func__);
Gerhard Sittigdf4fb1c2014-03-08 19:46:14 +0100261
Simon Glassce932c72015-11-29 13:18:10 -0700262 rc = mcs7830_write_reg(udev, REG_CONFIG,
Gerhard Sittigdf4fb1c2014-03-08 19:46:14 +0100263 sizeof(priv->config), &priv->config);
264 if (rc < 0) {
265 debug("writing config to adapter failed\n");
266 return rc;
267 }
268
269 return 0;
270}
271
272/*
273 * mcs7830_write_mchash() - write the network adapter's multicast filter
Simon Glassce932c72015-11-29 13:18:10 -0700274 * @udev: network device to write to
275 * @priv: private data
Gerhard Sittigdf4fb1c2014-03-08 19:46:14 +0100276 * Return: zero upon success, negative upon error
277 *
278 * the data which gets written is taken from the shadow multicast hashes
279 * within the device driver's private data
280 */
Simon Glassce932c72015-11-29 13:18:10 -0700281static int mcs7830_write_mchash(struct usb_device *udev,
282 struct mcs7830_private *priv)
Gerhard Sittigdf4fb1c2014-03-08 19:46:14 +0100283{
Gerhard Sittigdf4fb1c2014-03-08 19:46:14 +0100284 int rc;
285
286 debug("%s()\n", __func__);
Gerhard Sittigdf4fb1c2014-03-08 19:46:14 +0100287
Simon Glassce932c72015-11-29 13:18:10 -0700288 rc = mcs7830_write_reg(udev, REG_MULTICAST_HASH,
Gerhard Sittigdf4fb1c2014-03-08 19:46:14 +0100289 sizeof(priv->mchash), &priv->mchash);
290 if (rc < 0) {
291 debug("writing multicast hash to adapter failed\n");
292 return rc;
293 }
294
295 return 0;
296}
297
298/*
299 * mcs7830_set_autoneg() - setup and trigger ethernet link autonegotiation
Simon Glassce932c72015-11-29 13:18:10 -0700300 * @udev: network device to run link negotiation on
Gerhard Sittigdf4fb1c2014-03-08 19:46:14 +0100301 * Return: zero upon success, negative upon error
302 *
303 * the routine advertises available media and starts autonegotiation
304 */
Simon Glassce932c72015-11-29 13:18:10 -0700305static int mcs7830_set_autoneg(struct usb_device *udev)
Gerhard Sittigdf4fb1c2014-03-08 19:46:14 +0100306{
307 int adv, flg;
308 int rc;
309
310 debug("%s()\n", __func__);
311
312 /*
313 * algorithm taken from the Linux driver, which took it from
314 * "the original mcs7830 version 1.4 driver":
315 *
316 * enable all media, reset BMCR, enable auto neg, restart
317 * auto neg while keeping the enable auto neg flag set
318 */
319
320 adv = ADVERTISE_PAUSE_CAP | ADVERTISE_ALL | ADVERTISE_CSMA;
Simon Glassce932c72015-11-29 13:18:10 -0700321 rc = mcs7830_write_phy(udev, MII_ADVERTISE, adv);
Gerhard Sittigdf4fb1c2014-03-08 19:46:14 +0100322
323 flg = 0;
324 if (!rc)
Simon Glassce932c72015-11-29 13:18:10 -0700325 rc = mcs7830_write_phy(udev, MII_BMCR, flg);
Gerhard Sittigdf4fb1c2014-03-08 19:46:14 +0100326
327 flg |= BMCR_ANENABLE;
328 if (!rc)
Simon Glassce932c72015-11-29 13:18:10 -0700329 rc = mcs7830_write_phy(udev, MII_BMCR, flg);
Gerhard Sittigdf4fb1c2014-03-08 19:46:14 +0100330
331 flg |= BMCR_ANRESTART;
332 if (!rc)
Simon Glassce932c72015-11-29 13:18:10 -0700333 rc = mcs7830_write_phy(udev, MII_BMCR, flg);
Gerhard Sittigdf4fb1c2014-03-08 19:46:14 +0100334
335 return rc;
336}
337
338/*
339 * mcs7830_get_rev() - identify a network adapter's chip revision
Simon Glassce932c72015-11-29 13:18:10 -0700340 * @udev: network device to identify
Gerhard Sittigdf4fb1c2014-03-08 19:46:14 +0100341 * Return: non-negative number, reflecting the revision number
342 *
343 * currently, only "rev C and higher" and "below rev C" are needed, so
344 * the return value is #1 for "below rev C", and #2 for "rev C and above"
345 */
Simon Glassce932c72015-11-29 13:18:10 -0700346static int mcs7830_get_rev(struct usb_device *udev)
Gerhard Sittigdf4fb1c2014-03-08 19:46:14 +0100347{
348 uint8_t buf[2];
349 int rc;
350 int rev;
351
352 /* register 22 is readable in rev C and higher */
Simon Glassce932c72015-11-29 13:18:10 -0700353 rc = mcs7830_read_reg(udev, REG_FRAME_DROP_COUNTER, sizeof(buf), buf);
Gerhard Sittigdf4fb1c2014-03-08 19:46:14 +0100354 if (rc < 0)
355 rev = 1;
356 else
357 rev = 2;
358 debug("%s() rc=%d, rev=%d\n", __func__, rc, rev);
359 return rev;
360}
361
362/*
363 * mcs7830_apply_fixup() - identify an adapter and potentially apply fixups
Simon Glassce932c72015-11-29 13:18:10 -0700364 * @udev: network device to identify and apply fixups to
Gerhard Sittigdf4fb1c2014-03-08 19:46:14 +0100365 * Return: zero upon success (no errors emitted from here)
366 *
367 * this routine identifies the network adapter's chip revision, and applies
368 * fixups for known issues
369 */
Simon Glassce932c72015-11-29 13:18:10 -0700370static int mcs7830_apply_fixup(struct usb_device *udev)
Gerhard Sittigdf4fb1c2014-03-08 19:46:14 +0100371{
372 int rev;
373 int i;
374 uint8_t thr;
375
Simon Glassce932c72015-11-29 13:18:10 -0700376 rev = mcs7830_get_rev(udev);
Gerhard Sittigdf4fb1c2014-03-08 19:46:14 +0100377 debug("%s() rev=%d\n", __func__, rev);
378
379 /*
380 * rev C requires setting the pause threshold (the Linux driver
381 * is inconsistent, the implementation does it for "rev C
382 * exactly", the introductory comment says "rev C and above")
383 */
384 if (rev == 2) {
Simon Glassce932c72015-11-29 13:18:10 -0700385 debug("%s: applying rev C fixup\n", __func__);
Gerhard Sittigdf4fb1c2014-03-08 19:46:14 +0100386 thr = PAUSE_THRESHOLD_DEFAULT;
387 for (i = 0; i < 2; i++) {
Simon Glassce932c72015-11-29 13:18:10 -0700388 (void)mcs7830_write_reg(udev, REG_PAUSE_THRESHOLD,
Gerhard Sittigdf4fb1c2014-03-08 19:46:14 +0100389 sizeof(thr), &thr);
390 mdelay(1);
391 }
392 }
393
394 return 0;
395}
396
397/*
398 * mcs7830_basic_reset() - bring the network adapter into a known first state
399 * @eth: network device to act upon
400 * Return: zero upon success, negative upon error
401 *
402 * this routine initializes the network adapter such that subsequent invocations
403 * of the interface callbacks can exchange ethernet frames; link negotiation is
404 * triggered from here already and continues in background
405 */
Simon Glassce932c72015-11-29 13:18:10 -0700406static int mcs7830_basic_reset(struct usb_device *udev,
407 struct mcs7830_private *priv)
Gerhard Sittigdf4fb1c2014-03-08 19:46:14 +0100408{
Gerhard Sittigdf4fb1c2014-03-08 19:46:14 +0100409 int rc;
410
411 debug("%s()\n", __func__);
Gerhard Sittigdf4fb1c2014-03-08 19:46:14 +0100412
413 /*
414 * comment from the respective Linux driver, which
415 * unconditionally sets the ALLMULTICAST flag as well:
416 * should not be needed, but does not work otherwise
417 */
418 priv->config = CONF_TXENABLE;
419 priv->config |= CONF_ALLMULTICAST;
420
Simon Glassce932c72015-11-29 13:18:10 -0700421 rc = mcs7830_set_autoneg(udev);
Gerhard Sittigdf4fb1c2014-03-08 19:46:14 +0100422 if (rc < 0) {
Masahiro Yamada9b643e32017-09-16 14:10:41 +0900423 pr_err("setting autoneg failed\n");
Gerhard Sittigdf4fb1c2014-03-08 19:46:14 +0100424 return rc;
425 }
426
Simon Glassce932c72015-11-29 13:18:10 -0700427 rc = mcs7830_write_mchash(udev, priv);
Gerhard Sittigdf4fb1c2014-03-08 19:46:14 +0100428 if (rc < 0) {
Masahiro Yamada9b643e32017-09-16 14:10:41 +0900429 pr_err("failed to set multicast hash\n");
Gerhard Sittigdf4fb1c2014-03-08 19:46:14 +0100430 return rc;
431 }
432
Simon Glassce932c72015-11-29 13:18:10 -0700433 rc = mcs7830_write_config(udev, priv);
Gerhard Sittigdf4fb1c2014-03-08 19:46:14 +0100434 if (rc < 0) {
Masahiro Yamada9b643e32017-09-16 14:10:41 +0900435 pr_err("failed to set configuration\n");
Gerhard Sittigdf4fb1c2014-03-08 19:46:14 +0100436 return rc;
437 }
438
Simon Glassce932c72015-11-29 13:18:10 -0700439 rc = mcs7830_apply_fixup(udev);
Gerhard Sittigdf4fb1c2014-03-08 19:46:14 +0100440 if (rc < 0) {
Masahiro Yamada9b643e32017-09-16 14:10:41 +0900441 pr_err("fixup application failed\n");
Gerhard Sittigdf4fb1c2014-03-08 19:46:14 +0100442 return rc;
443 }
444
445 return 0;
446}
447
448/*
449 * mcs7830_read_mac() - read an ethernet adapter's MAC address
Simon Glassce932c72015-11-29 13:18:10 -0700450 * @udev: network device to read from
451 * @enetaddr: place to put ethernet MAC address
Gerhard Sittigdf4fb1c2014-03-08 19:46:14 +0100452 * Return: zero upon success, negative upon error
453 *
454 * this routine fetches the MAC address stored within the ethernet adapter,
455 * and stores it in the ethernet interface's data structure
456 */
Simon Glassce932c72015-11-29 13:18:10 -0700457static int mcs7830_read_mac(struct usb_device *udev, unsigned char enetaddr[])
Gerhard Sittigdf4fb1c2014-03-08 19:46:14 +0100458{
Gerhard Sittigdf4fb1c2014-03-08 19:46:14 +0100459 int rc;
460 uint8_t buf[ETH_ALEN];
461
462 debug("%s()\n", __func__);
Gerhard Sittigdf4fb1c2014-03-08 19:46:14 +0100463
Simon Glassce932c72015-11-29 13:18:10 -0700464 rc = mcs7830_read_reg(udev, REG_ETHER_ADDR, ETH_ALEN, buf);
Gerhard Sittigdf4fb1c2014-03-08 19:46:14 +0100465 if (rc < 0) {
466 debug("reading MAC from adapter failed\n");
467 return rc;
468 }
469
Simon Glassce932c72015-11-29 13:18:10 -0700470 memcpy(enetaddr, buf, ETH_ALEN);
Gerhard Sittigdf4fb1c2014-03-08 19:46:14 +0100471 return 0;
472}
473
Simon Glassce932c72015-11-29 13:18:10 -0700474static int mcs7830_write_mac_common(struct usb_device *udev,
475 unsigned char enetaddr[])
Gerhard Sittigdf4fb1c2014-03-08 19:46:14 +0100476{
Gerhard Sittigdf4fb1c2014-03-08 19:46:14 +0100477 int rc;
478
479 debug("%s()\n", __func__);
Gerhard Sittigdf4fb1c2014-03-08 19:46:14 +0100480
Simon Glassce932c72015-11-29 13:18:10 -0700481 rc = mcs7830_write_reg(udev, REG_ETHER_ADDR, ETH_ALEN, enetaddr);
Gerhard Sittigdf4fb1c2014-03-08 19:46:14 +0100482 if (rc < 0) {
483 debug("writing MAC to adapter failed\n");
484 return rc;
485 }
486 return 0;
487}
488
Simon Glassce932c72015-11-29 13:18:10 -0700489static int mcs7830_init_common(struct usb_device *udev)
Gerhard Sittigdf4fb1c2014-03-08 19:46:14 +0100490{
Gerhard Sittigdf4fb1c2014-03-08 19:46:14 +0100491 int timeout;
492 int have_link;
493
494 debug("%s()\n", __func__);
Gerhard Sittigdf4fb1c2014-03-08 19:46:14 +0100495
496 timeout = 0;
497 do {
Simon Glassce932c72015-11-29 13:18:10 -0700498 have_link = mcs7830_read_phy(udev, MII_BMSR) & BMSR_LSTATUS;
Gerhard Sittigdf4fb1c2014-03-08 19:46:14 +0100499 if (have_link)
500 break;
501 udelay(LINKSTATUS_TIMEOUT_RES * 1000);
502 timeout += LINKSTATUS_TIMEOUT_RES;
503 } while (timeout < LINKSTATUS_TIMEOUT);
504 if (!have_link) {
505 debug("ethernet link is down\n");
506 return -ETIMEDOUT;
507 }
508 return 0;
509}
510
Simon Glassce932c72015-11-29 13:18:10 -0700511static int mcs7830_send_common(struct ueth_data *ueth, void *packet,
512 int length)
Gerhard Sittigdf4fb1c2014-03-08 19:46:14 +0100513{
Simon Glassce932c72015-11-29 13:18:10 -0700514 struct usb_device *udev = ueth->pusb_dev;
Gerhard Sittigdf4fb1c2014-03-08 19:46:14 +0100515 int rc;
516 int gotlen;
517 /* there is a status byte after the ethernet frame */
518 ALLOC_CACHE_ALIGN_BUFFER(uint8_t, buf, PKTSIZE + sizeof(uint8_t));
519
Gerhard Sittigdf4fb1c2014-03-08 19:46:14 +0100520 memcpy(buf, packet, length);
Simon Glassce932c72015-11-29 13:18:10 -0700521 rc = usb_bulk_msg(udev,
522 usb_sndbulkpipe(udev, ueth->ep_out),
Gerhard Sittigdf4fb1c2014-03-08 19:46:14 +0100523 &buf[0], length, &gotlen,
524 USBCALL_TIMEOUT);
525 debug("%s() TX want len %d, got len %d, rc %d\n",
526 __func__, length, gotlen, rc);
527 return rc;
528}
529
Simon Glassce932c72015-11-29 13:18:10 -0700530static int mcs7830_recv_common(struct ueth_data *ueth, uint8_t *buf)
Gerhard Sittigdf4fb1c2014-03-08 19:46:14 +0100531{
Gerhard Sittigdf4fb1c2014-03-08 19:46:14 +0100532 int rc, wantlen, gotlen;
533 uint8_t sts;
534
535 debug("%s()\n", __func__);
Gerhard Sittigdf4fb1c2014-03-08 19:46:14 +0100536
537 /* fetch input data from the adapter */
538 wantlen = MCS7830_RX_URB_SIZE;
Simon Glassce932c72015-11-29 13:18:10 -0700539 rc = usb_bulk_msg(ueth->pusb_dev,
540 usb_rcvbulkpipe(ueth->pusb_dev, ueth->ep_in),
Gerhard Sittigdf4fb1c2014-03-08 19:46:14 +0100541 &buf[0], wantlen, &gotlen,
542 USBCALL_TIMEOUT);
543 debug("%s() RX want len %d, got len %d, rc %d\n",
544 __func__, wantlen, gotlen, rc);
545 if (rc != 0) {
Masahiro Yamada9b643e32017-09-16 14:10:41 +0900546 pr_err("RX: failed to receive\n");
Gerhard Sittigdf4fb1c2014-03-08 19:46:14 +0100547 return rc;
548 }
549 if (gotlen > wantlen) {
Masahiro Yamada9b643e32017-09-16 14:10:41 +0900550 pr_err("RX: got too many bytes (%d)\n", gotlen);
Gerhard Sittigdf4fb1c2014-03-08 19:46:14 +0100551 return -EIO;
552 }
553
554 /*
555 * the bulk message that we received from USB contains exactly
556 * one ethernet frame and a trailing status byte
557 */
558 if (gotlen < sizeof(sts))
559 return -EIO;
560 gotlen -= sizeof(sts);
561 sts = buf[gotlen];
562
563 if (sts == STAT_RX_FRAME_CORRECT) {
564 debug("%s() got a frame, len=%d\n", __func__, gotlen);
Simon Glassce932c72015-11-29 13:18:10 -0700565 return gotlen;
Gerhard Sittigdf4fb1c2014-03-08 19:46:14 +0100566 }
567
568 debug("RX: frame error (sts 0x%02X, %s %s %s %s %s)\n",
569 sts,
570 (sts & STAT_RX_LARGE_FRAME) ? "large" : "-",
571 (sts & STAT_RX_LENGTH_ERROR) ? "length" : "-",
572 (sts & STAT_RX_SHORT_FRAME) ? "short" : "-",
573 (sts & STAT_RX_CRC_ERROR) ? "crc" : "-",
574 (sts & STAT_RX_ALIGNMENT_ERROR) ? "align" : "-");
575 return -EIO;
576}
577
Simon Glassd4f847e2015-11-29 13:18:11 -0700578#ifndef CONFIG_DM_ETH
Gerhard Sittigdf4fb1c2014-03-08 19:46:14 +0100579/*
Simon Glassce932c72015-11-29 13:18:10 -0700580 * mcs7830_init() - network interface's init callback
581 * @udev: network device to initialize
582 * @bd: board information
583 * Return: zero upon success, negative upon error
584 *
585 * after initial setup during probe() and get_info(), this init() callback
586 * ensures that the link is up and subsequent send() and recv() calls can
587 * exchange ethernet frames
588 */
Masahiro Yamadab75d8dc2020-06-26 15:13:33 +0900589static int mcs7830_init(struct eth_device *eth, struct bd_info *bd)
Simon Glassce932c72015-11-29 13:18:10 -0700590{
591 struct ueth_data *dev = eth->priv;
592
593 return mcs7830_init_common(dev->pusb_dev);
594}
595
596/*
597 * mcs7830_send() - network interface's send callback
598 * @eth: network device to send the frame from
599 * @packet: ethernet frame content
600 * @length: ethernet frame length
601 * Return: zero upon success, negative upon error
602 *
603 * this routine send an ethernet frame out of the network interface
604 */
605static int mcs7830_send(struct eth_device *eth, void *packet, int length)
606{
607 struct ueth_data *dev = eth->priv;
608
609 return mcs7830_send_common(dev, packet, length);
610}
611
612/*
613 * mcs7830_recv() - network interface's recv callback
614 * @eth: network device to receive frames from
615 * Return: zero upon success, negative upon error
616 *
617 * this routine checks for available ethernet frames that the network
618 * interface might have received, and notifies the network stack
619 */
620static int mcs7830_recv(struct eth_device *eth)
621{
622 ALLOC_CACHE_ALIGN_BUFFER(uint8_t, buf, MCS7830_RX_URB_SIZE);
623 struct ueth_data *ueth = eth->priv;
624 int len;
625
626 len = mcs7830_recv_common(ueth, buf);
Uri Mashiach3ce30262017-05-21 17:00:52 +0300627 if (len >= 0) {
Simon Glassce932c72015-11-29 13:18:10 -0700628 net_process_received_packet(buf, len);
Uri Mashiach3ce30262017-05-21 17:00:52 +0300629 return 0;
630 }
Simon Glassce932c72015-11-29 13:18:10 -0700631
Uri Mashiach3ce30262017-05-21 17:00:52 +0300632 return len;
Simon Glassce932c72015-11-29 13:18:10 -0700633}
634
635/*
Gerhard Sittigdf4fb1c2014-03-08 19:46:14 +0100636 * mcs7830_halt() - network interface's halt callback
637 * @eth: network device to cease operation of
638 * Return: none
639 *
640 * this routine is supposed to undo the effect of previous initialization and
641 * ethernet frames exchange; in this implementation it's a NOP
642 */
643static void mcs7830_halt(struct eth_device *eth)
644{
645 debug("%s()\n", __func__);
646}
647
648/*
Simon Glassce932c72015-11-29 13:18:10 -0700649 * mcs7830_write_mac() - write an ethernet adapter's MAC address
650 * @eth: network device to write to
651 * Return: zero upon success, negative upon error
652 *
653 * this routine takes the MAC address from the ethernet interface's data
654 * structure, and writes it into the ethernet adapter such that subsequent
655 * exchange of ethernet frames uses this address
656 */
657static int mcs7830_write_mac(struct eth_device *eth)
658{
659 struct ueth_data *ueth = eth->priv;
660
661 return mcs7830_write_mac_common(ueth->pusb_dev, eth->enetaddr);
662}
663
664/*
Gerhard Sittigdf4fb1c2014-03-08 19:46:14 +0100665 * mcs7830_iface_idx - index of detected network interfaces
666 *
667 * this counter keeps track of identified supported interfaces,
668 * to assign unique names as more interfaces are found
669 */
670static int mcs7830_iface_idx;
671
672/*
673 * mcs7830_eth_before_probe() - network driver's before_probe callback
674 * Return: none
675 *
676 * this routine initializes driver's internal data in preparation of
677 * subsequent probe callbacks
678 */
679void mcs7830_eth_before_probe(void)
680{
681 mcs7830_iface_idx = 0;
682}
683
684/*
685 * struct mcs7830_dongle - description of a supported Moschip ethernet dongle
686 * @vendor: 16bit USB vendor identification
687 * @product: 16bit USB product identification
688 *
689 * this structure describes a supported USB ethernet dongle by means of the
690 * vendor and product codes found during USB enumeration; no flags are held
691 * here since all supported dongles have identical behaviour, and required
692 * fixups get determined at runtime, such that no manual configuration is
693 * needed
694 */
695struct mcs7830_dongle {
696 uint16_t vendor;
697 uint16_t product;
698};
699
700/*
701 * mcs7830_dongles - the list of supported Moschip based USB ethernet dongles
702 */
Jeroen Hofstee51afc2c2014-06-17 21:14:17 +0200703static const struct mcs7830_dongle mcs7830_dongles[] = {
Gerhard Sittigdf4fb1c2014-03-08 19:46:14 +0100704 { 0x9710, 0x7832, }, /* Moschip 7832 */
705 { 0x9710, 0x7830, }, /* Moschip 7830 */
706 { 0x9710, 0x7730, }, /* Moschip 7730 */
707 { 0x0df6, 0x0021, }, /* Sitecom LN 30 */
708};
709
710/*
711 * mcs7830_eth_probe() - network driver's probe callback
712 * @dev: detected USB device to check
713 * @ifnum: detected USB interface to check
714 * @ss: USB ethernet data structure to fill in upon match
715 * Return: #1 upon match, #0 upon mismatch or error
716 *
717 * this routine checks whether the found USB device is supported by
718 * this ethernet driver, and upon match fills in the USB ethernet
719 * data structure which later is passed to the get_info callback
720 */
721int mcs7830_eth_probe(struct usb_device *dev, unsigned int ifnum,
722 struct ueth_data *ss)
723{
724 struct usb_interface *iface;
725 struct usb_interface_descriptor *iface_desc;
726 int i;
727 struct mcs7830_private *priv;
728 int ep_in_found, ep_out_found, ep_intr_found;
729
730 debug("%s()\n", __func__);
731
732 /* iterate the list of supported dongles */
733 iface = &dev->config.if_desc[ifnum];
734 iface_desc = &iface->desc;
735 for (i = 0; i < ARRAY_SIZE(mcs7830_dongles); i++) {
736 if (dev->descriptor.idVendor == mcs7830_dongles[i].vendor &&
737 dev->descriptor.idProduct == mcs7830_dongles[i].product)
738 break;
739 }
740 if (i == ARRAY_SIZE(mcs7830_dongles))
741 return 0;
742 debug("detected USB ethernet device: %04X:%04X\n",
743 dev->descriptor.idVendor, dev->descriptor.idProduct);
744
745 /* fill in driver private data */
746 priv = calloc(1, sizeof(*priv));
747 if (!priv)
748 return 0;
749
750 /* fill in the ueth_data structure, attach private data */
751 memset(ss, 0, sizeof(*ss));
752 ss->ifnum = ifnum;
753 ss->pusb_dev = dev;
754 ss->subclass = iface_desc->bInterfaceSubClass;
755 ss->protocol = iface_desc->bInterfaceProtocol;
756 ss->dev_priv = priv;
757
758 /*
759 * a minimum of three endpoints is expected: in (bulk),
760 * out (bulk), and interrupt; ignore all others
761 */
762 ep_in_found = ep_out_found = ep_intr_found = 0;
763 for (i = 0; i < iface_desc->bNumEndpoints; i++) {
764 uint8_t eptype, epaddr;
765 bool is_input;
766
767 eptype = iface->ep_desc[i].bmAttributes;
768 eptype &= USB_ENDPOINT_XFERTYPE_MASK;
769
770 epaddr = iface->ep_desc[i].bEndpointAddress;
771 is_input = epaddr & USB_DIR_IN;
772 epaddr &= USB_ENDPOINT_NUMBER_MASK;
773
774 if (eptype == USB_ENDPOINT_XFER_BULK) {
775 if (is_input && !ep_in_found) {
776 ss->ep_in = epaddr;
777 ep_in_found++;
778 }
779 if (!is_input && !ep_out_found) {
780 ss->ep_out = epaddr;
781 ep_out_found++;
782 }
783 }
784
785 if (eptype == USB_ENDPOINT_XFER_INT) {
786 if (is_input && !ep_intr_found) {
787 ss->ep_int = epaddr;
788 ss->irqinterval = iface->ep_desc[i].bInterval;
789 ep_intr_found++;
790 }
791 }
792 }
793 debug("endpoints: in %d, out %d, intr %d\n",
794 ss->ep_in, ss->ep_out, ss->ep_int);
795
796 /* apply basic sanity checks */
797 if (usb_set_interface(dev, iface_desc->bInterfaceNumber, 0) ||
798 !ss->ep_in || !ss->ep_out || !ss->ep_int) {
799 debug("device probe incomplete\n");
800 return 0;
801 }
802
803 dev->privptr = ss;
804 return 1;
805}
806
807/*
808 * mcs7830_eth_get_info() - network driver's get_info callback
809 * @dev: detected USB device
810 * @ss: USB ethernet data structure filled in at probe()
811 * @eth: ethernet interface data structure to fill in
812 * Return: #1 upon success, #0 upon error
813 *
814 * this routine registers the mandatory init(), send(), recv(), and
815 * halt() callbacks with the ethernet interface, can register the
816 * optional write_hwaddr() callback with the ethernet interface,
817 * and initiates configuration of the interface such that subsequent
818 * calls to those callbacks results in network communication
819 */
820int mcs7830_eth_get_info(struct usb_device *dev, struct ueth_data *ss,
821 struct eth_device *eth)
822{
823 debug("%s()\n", __func__);
824 if (!eth) {
825 debug("%s: missing parameter.\n", __func__);
826 return 0;
827 }
828
829 snprintf(eth->name, sizeof(eth->name), "%s%d",
830 MCS7830_BASE_NAME, mcs7830_iface_idx++);
831 eth->init = mcs7830_init;
832 eth->send = mcs7830_send;
833 eth->recv = mcs7830_recv;
834 eth->halt = mcs7830_halt;
835 eth->write_hwaddr = mcs7830_write_mac;
836 eth->priv = ss;
837
Simon Glassce932c72015-11-29 13:18:10 -0700838 if (mcs7830_basic_reset(ss->pusb_dev, ss->dev_priv))
Gerhard Sittigdf4fb1c2014-03-08 19:46:14 +0100839 return 0;
840
Simon Glassce932c72015-11-29 13:18:10 -0700841 if (mcs7830_read_mac(ss->pusb_dev, eth->enetaddr))
Gerhard Sittigdf4fb1c2014-03-08 19:46:14 +0100842 return 0;
843 debug("MAC %pM\n", eth->enetaddr);
844
845 return 1;
846}
Simon Glassd4f847e2015-11-29 13:18:11 -0700847#endif
848
849
850#ifdef CONFIG_DM_ETH
851static int mcs7830_eth_start(struct udevice *dev)
852{
853 struct usb_device *udev = dev_get_parent_priv(dev);
854
855 return mcs7830_init_common(udev);
856}
857
858void mcs7830_eth_stop(struct udevice *dev)
859{
860 debug("** %s()\n", __func__);
861}
862
863int mcs7830_eth_send(struct udevice *dev, void *packet, int length)
864{
865 struct mcs7830_private *priv = dev_get_priv(dev);
866 struct ueth_data *ueth = &priv->ueth;
867
868 return mcs7830_send_common(ueth, packet, length);
869}
870
871int mcs7830_eth_recv(struct udevice *dev, int flags, uchar **packetp)
872{
873 struct mcs7830_private *priv = dev_get_priv(dev);
874 struct ueth_data *ueth = &priv->ueth;
875 int len;
876
877 len = mcs7830_recv_common(ueth, priv->rx_buf);
878 *packetp = priv->rx_buf;
879
880 return len;
881}
882
883static int mcs7830_free_pkt(struct udevice *dev, uchar *packet, int packet_len)
884{
885 struct mcs7830_private *priv = dev_get_priv(dev);
886
887 packet_len = ALIGN(packet_len, 4);
888 usb_ether_advance_rxbuf(&priv->ueth, sizeof(u32) + packet_len);
889
890 return 0;
891}
892
893int mcs7830_write_hwaddr(struct udevice *dev)
894{
895 struct usb_device *udev = dev_get_parent_priv(dev);
896 struct eth_pdata *pdata = dev_get_platdata(dev);
897
898 return mcs7830_write_mac_common(udev, pdata->enetaddr);
899}
900
901static int mcs7830_eth_probe(struct udevice *dev)
902{
903 struct usb_device *udev = dev_get_parent_priv(dev);
904 struct mcs7830_private *priv = dev_get_priv(dev);
905 struct eth_pdata *pdata = dev_get_platdata(dev);
906 struct ueth_data *ueth = &priv->ueth;
907
908 if (mcs7830_basic_reset(udev, priv))
909 return 0;
910
911 if (mcs7830_read_mac(udev, pdata->enetaddr))
912 return 0;
913
914 return usb_ether_register(dev, ueth, MCS7830_RX_URB_SIZE);
915}
916
917static const struct eth_ops mcs7830_eth_ops = {
918 .start = mcs7830_eth_start,
919 .send = mcs7830_eth_send,
920 .recv = mcs7830_eth_recv,
921 .free_pkt = mcs7830_free_pkt,
922 .stop = mcs7830_eth_stop,
923 .write_hwaddr = mcs7830_write_hwaddr,
924};
925
926U_BOOT_DRIVER(mcs7830_eth) = {
927 .name = "mcs7830_eth",
928 .id = UCLASS_ETH,
929 .probe = mcs7830_eth_probe,
930 .ops = &mcs7830_eth_ops,
931 .priv_auto_alloc_size = sizeof(struct mcs7830_private),
932 .platdata_auto_alloc_size = sizeof(struct eth_pdata),
933 .flags = DM_FLAG_ALLOC_PRIV_DMA,
934};
935
936static const struct usb_device_id mcs7830_eth_id_table[] = {
937 { USB_DEVICE(0x9710, 0x7832) }, /* Moschip 7832 */
938 { USB_DEVICE(0x9710, 0x7830), }, /* Moschip 7830 */
939 { USB_DEVICE(0x9710, 0x7730), }, /* Moschip 7730 */
940 { USB_DEVICE(0x0df6, 0x0021), }, /* Sitecom LN 30 */
941 { } /* Terminating entry */
942};
943
944U_BOOT_USB_DEVICE(mcs7830_eth, mcs7830_eth_id_table);
945#endif