blob: b8fe65237d5708eb9eedf2141eb1e7c5c035bfba [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Gerhard Sittig2385df62014-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 Sittig2385df62014-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 Glassfd954e52015-11-29 13:18:11 -070013#include <dm.h>
Gerhard Sittig2385df62014-03-08 19:46:14 +010014#include <errno.h>
Simon Glass0f2af882020-05-10 11:40:05 -060015#include <log.h>
Simon Glass274e0b02020-05-10 11:39:56 -060016#include <net.h>
Gerhard Sittig2385df62014-03-08 19:46:14 +010017#include <linux/mii.h>
18#include <malloc.h>
Simon Glass2dd337a2015-09-02 17:24:58 -060019#include <memalign.h>
Gerhard Sittig2385df62014-03-08 19:46:14 +010020#include <usb.h>
21
22#include "usb_ether.h"
23
24#define MCS7830_BASE_NAME "mcs"
25
26#define USBCALL_TIMEOUT 1000
27#define LINKSTATUS_TIMEOUT 5000 /* link status, connect timeout */
28#define LINKSTATUS_TIMEOUT_RES 50 /* link status, resolution in msec */
29
30#define MCS7830_RX_URB_SIZE 2048
31
32/* command opcodes */
33#define MCS7830_WR_BREQ 0x0d
34#define MCS7830_RD_BREQ 0x0e
35
36/* register layout, numerical offset specs for USB API calls */
37struct mcs7830_regs {
38 uint8_t multicast_hashes[8];
39 uint8_t packet_gap[2];
40 uint8_t phy_data[2];
41 uint8_t phy_command[2];
42 uint8_t configuration;
43 uint8_t ether_address[6];
44 uint8_t frame_drop_count;
45 uint8_t pause_threshold;
46};
47#define REG_MULTICAST_HASH offsetof(struct mcs7830_regs, multicast_hashes)
48#define REG_PHY_DATA offsetof(struct mcs7830_regs, phy_data)
49#define REG_PHY_CMD offsetof(struct mcs7830_regs, phy_command)
50#define REG_CONFIG offsetof(struct mcs7830_regs, configuration)
51#define REG_ETHER_ADDR offsetof(struct mcs7830_regs, ether_address)
52#define REG_FRAME_DROP_COUNTER offsetof(struct mcs7830_regs, frame_drop_count)
53#define REG_PAUSE_THRESHOLD offsetof(struct mcs7830_regs, pause_threshold)
54
55/* bit masks and default values for the above registers */
56#define PHY_CMD1_READ 0x40
57#define PHY_CMD1_WRITE 0x20
58#define PHY_CMD1_PHYADDR 0x01
59
60#define PHY_CMD2_PEND 0x80
61#define PHY_CMD2_READY 0x40
62
63#define CONF_CFG 0x80
64#define CONF_SPEED100 0x40
65#define CONF_FDX_ENABLE 0x20
66#define CONF_RXENABLE 0x10
67#define CONF_TXENABLE 0x08
68#define CONF_SLEEPMODE 0x04
69#define CONF_ALLMULTICAST 0x02
70#define CONF_PROMISCUOUS 0x01
71
72#define PAUSE_THRESHOLD_DEFAULT 0
73
74/* bit masks for the status byte which follows received ethernet frames */
75#define STAT_RX_FRAME_CORRECT 0x20
76#define STAT_RX_LARGE_FRAME 0x10
77#define STAT_RX_CRC_ERROR 0x08
78#define STAT_RX_ALIGNMENT_ERROR 0x04
79#define STAT_RX_LENGTH_ERROR 0x02
80#define STAT_RX_SHORT_FRAME 0x01
81
82/*
83 * struct mcs7830_private - private driver data for an individual adapter
84 * @config: shadow for the network adapter's configuration register
85 * @mchash: shadow for the network adapter's multicast hash registers
86 */
87struct mcs7830_private {
Simon Glassfd954e52015-11-29 13:18:11 -070088#ifdef CONFIG_DM_ETH
89 uint8_t rx_buf[MCS7830_RX_URB_SIZE];
90 struct ueth_data ueth;
91#endif
Gerhard Sittig2385df62014-03-08 19:46:14 +010092 uint8_t config;
93 uint8_t mchash[8];
94};
95
96/*
97 * mcs7830_read_reg() - read a register of the network adapter
Simon Glassb82f62d2015-11-29 13:18:10 -070098 * @udev: network device to read from
Gerhard Sittig2385df62014-03-08 19:46:14 +010099 * @idx: index of the register to start reading from
100 * @size: number of bytes to read
101 * @data: buffer to read into
102 * Return: zero upon success, negative upon error
103 */
Simon Glassb82f62d2015-11-29 13:18:10 -0700104static int mcs7830_read_reg(struct usb_device *udev, uint8_t idx,
Gerhard Sittig2385df62014-03-08 19:46:14 +0100105 uint16_t size, void *data)
106{
107 int len;
108 ALLOC_CACHE_ALIGN_BUFFER(uint8_t, buf, size);
109
110 debug("%s() idx=0x%04X sz=%d\n", __func__, idx, size);
111
Simon Glassb82f62d2015-11-29 13:18:10 -0700112 len = usb_control_msg(udev,
113 usb_rcvctrlpipe(udev, 0),
Gerhard Sittig2385df62014-03-08 19:46:14 +0100114 MCS7830_RD_BREQ,
115 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
116 0, idx, buf, size,
117 USBCALL_TIMEOUT);
118 if (len != size) {
119 debug("%s() len=%d != sz=%d\n", __func__, len, size);
120 return -EIO;
121 }
122 memcpy(data, buf, size);
123 return 0;
124}
125
126/*
127 * mcs7830_write_reg() - write a register of the network adapter
Simon Glassb82f62d2015-11-29 13:18:10 -0700128 * @udev: network device to write to
Gerhard Sittig2385df62014-03-08 19:46:14 +0100129 * @idx: index of the register to start writing to
130 * @size: number of bytes to write
131 * @data: buffer holding the data to write
132 * Return: zero upon success, negative upon error
133 */
Simon Glassb82f62d2015-11-29 13:18:10 -0700134static int mcs7830_write_reg(struct usb_device *udev, uint8_t idx,
Gerhard Sittig2385df62014-03-08 19:46:14 +0100135 uint16_t size, void *data)
136{
137 int len;
138 ALLOC_CACHE_ALIGN_BUFFER(uint8_t, buf, size);
139
140 debug("%s() idx=0x%04X sz=%d\n", __func__, idx, size);
141
142 memcpy(buf, data, size);
Simon Glassb82f62d2015-11-29 13:18:10 -0700143 len = usb_control_msg(udev,
144 usb_sndctrlpipe(udev, 0),
Gerhard Sittig2385df62014-03-08 19:46:14 +0100145 MCS7830_WR_BREQ,
146 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
147 0, idx, buf, size,
148 USBCALL_TIMEOUT);
149 if (len != size) {
150 debug("%s() len=%d != sz=%d\n", __func__, len, size);
151 return -EIO;
152 }
153 return 0;
154}
155
156/*
157 * mcs7830_phy_emit_wait() - emit PHY read/write access, wait for its execution
Simon Glassb82f62d2015-11-29 13:18:10 -0700158 * @udev: network device to talk to
Gerhard Sittig2385df62014-03-08 19:46:14 +0100159 * @rwflag: PHY_CMD1_READ or PHY_CMD1_WRITE opcode
160 * @index: number of the PHY register to read or write
161 * Return: zero upon success, negative upon error
162 */
Simon Glassb82f62d2015-11-29 13:18:10 -0700163static int mcs7830_phy_emit_wait(struct usb_device *udev,
Gerhard Sittig2385df62014-03-08 19:46:14 +0100164 uint8_t rwflag, uint8_t index)
165{
166 int rc;
167 int retry;
168 uint8_t cmd[2];
169
170 /* send the PHY read/write request */
171 cmd[0] = rwflag | PHY_CMD1_PHYADDR;
172 cmd[1] = PHY_CMD2_PEND | (index & 0x1f);
Simon Glassb82f62d2015-11-29 13:18:10 -0700173 rc = mcs7830_write_reg(udev, REG_PHY_CMD, sizeof(cmd), cmd);
Gerhard Sittig2385df62014-03-08 19:46:14 +0100174 if (rc < 0)
175 return rc;
176
177 /* wait for the response to become available (usually < 1ms) */
178 retry = 10;
179 do {
Simon Glassb82f62d2015-11-29 13:18:10 -0700180 rc = mcs7830_read_reg(udev, REG_PHY_CMD, sizeof(cmd), cmd);
Gerhard Sittig2385df62014-03-08 19:46:14 +0100181 if (rc < 0)
182 return rc;
183 if (cmd[1] & PHY_CMD2_READY)
184 return 0;
185 if (!retry--)
186 return -ETIMEDOUT;
187 mdelay(1);
188 } while (1);
189 /* UNREACH */
190}
191
192/*
193 * mcs7830_read_phy() - read a PHY register of the network adapter
Simon Glassb82f62d2015-11-29 13:18:10 -0700194 * @udev: network device to read from
Gerhard Sittig2385df62014-03-08 19:46:14 +0100195 * @index: index of the PHY register to read from
196 * Return: non-negative 16bit register content, negative upon error
197 */
Simon Glassb82f62d2015-11-29 13:18:10 -0700198static int mcs7830_read_phy(struct usb_device *udev, uint8_t index)
Gerhard Sittig2385df62014-03-08 19:46:14 +0100199{
200 int rc;
201 uint16_t val;
202
203 /* issue the PHY read request and wait for its execution */
Simon Glassb82f62d2015-11-29 13:18:10 -0700204 rc = mcs7830_phy_emit_wait(udev, PHY_CMD1_READ, index);
Gerhard Sittig2385df62014-03-08 19:46:14 +0100205 if (rc < 0)
206 return rc;
207
208 /* fetch the PHY data which was read */
Simon Glassb82f62d2015-11-29 13:18:10 -0700209 rc = mcs7830_read_reg(udev, REG_PHY_DATA, sizeof(val), &val);
Gerhard Sittig2385df62014-03-08 19:46:14 +0100210 if (rc < 0)
211 return rc;
212 rc = le16_to_cpu(val);
Simon Glassb82f62d2015-11-29 13:18:10 -0700213 debug("%s(%d) => 0x%04X\n", __func__, index, rc);
Gerhard Sittig2385df62014-03-08 19:46:14 +0100214 return rc;
215}
216
217/*
218 * mcs7830_write_phy() - write a PHY register of the network adapter
Simon Glassb82f62d2015-11-29 13:18:10 -0700219 * @udev: network device to write to
Gerhard Sittig2385df62014-03-08 19:46:14 +0100220 * @index: index of the PHY register to write to
221 * @val: value to write to the PHY register
222 * Return: zero upon success, negative upon error
223 */
Simon Glassb82f62d2015-11-29 13:18:10 -0700224static int mcs7830_write_phy(struct usb_device *udev, uint8_t index,
225 uint16_t val)
Gerhard Sittig2385df62014-03-08 19:46:14 +0100226{
227 int rc;
228
Simon Glassb82f62d2015-11-29 13:18:10 -0700229 debug("%s(%d, 0x%04X)\n", __func__, index, val);
Gerhard Sittig2385df62014-03-08 19:46:14 +0100230
231 /* setup the PHY data which is to get written */
232 val = cpu_to_le16(val);
Simon Glassb82f62d2015-11-29 13:18:10 -0700233 rc = mcs7830_write_reg(udev, REG_PHY_DATA, sizeof(val), &val);
Gerhard Sittig2385df62014-03-08 19:46:14 +0100234 if (rc < 0)
235 return rc;
236
237 /* issue the PHY write request and wait for its execution */
Simon Glassb82f62d2015-11-29 13:18:10 -0700238 rc = mcs7830_phy_emit_wait(udev, PHY_CMD1_WRITE, index);
Gerhard Sittig2385df62014-03-08 19:46:14 +0100239 if (rc < 0)
240 return rc;
241
242 return 0;
243}
244
245/*
246 * mcs7830_write_config() - write to the network adapter's config register
Simon Glassb82f62d2015-11-29 13:18:10 -0700247 * @udev: network device to write to
248 * @priv: private data
Gerhard Sittig2385df62014-03-08 19:46:14 +0100249 * Return: zero upon success, negative upon error
250 *
251 * the data which gets written is taken from the shadow config register
252 * within the device driver's private data
253 */
Simon Glassb82f62d2015-11-29 13:18:10 -0700254static int mcs7830_write_config(struct usb_device *udev,
255 struct mcs7830_private *priv)
Gerhard Sittig2385df62014-03-08 19:46:14 +0100256{
Gerhard Sittig2385df62014-03-08 19:46:14 +0100257 int rc;
258
259 debug("%s()\n", __func__);
Gerhard Sittig2385df62014-03-08 19:46:14 +0100260
Simon Glassb82f62d2015-11-29 13:18:10 -0700261 rc = mcs7830_write_reg(udev, REG_CONFIG,
Gerhard Sittig2385df62014-03-08 19:46:14 +0100262 sizeof(priv->config), &priv->config);
263 if (rc < 0) {
264 debug("writing config to adapter failed\n");
265 return rc;
266 }
267
268 return 0;
269}
270
271/*
272 * mcs7830_write_mchash() - write the network adapter's multicast filter
Simon Glassb82f62d2015-11-29 13:18:10 -0700273 * @udev: network device to write to
274 * @priv: private data
Gerhard Sittig2385df62014-03-08 19:46:14 +0100275 * Return: zero upon success, negative upon error
276 *
277 * the data which gets written is taken from the shadow multicast hashes
278 * within the device driver's private data
279 */
Simon Glassb82f62d2015-11-29 13:18:10 -0700280static int mcs7830_write_mchash(struct usb_device *udev,
281 struct mcs7830_private *priv)
Gerhard Sittig2385df62014-03-08 19:46:14 +0100282{
Gerhard Sittig2385df62014-03-08 19:46:14 +0100283 int rc;
284
285 debug("%s()\n", __func__);
Gerhard Sittig2385df62014-03-08 19:46:14 +0100286
Simon Glassb82f62d2015-11-29 13:18:10 -0700287 rc = mcs7830_write_reg(udev, REG_MULTICAST_HASH,
Gerhard Sittig2385df62014-03-08 19:46:14 +0100288 sizeof(priv->mchash), &priv->mchash);
289 if (rc < 0) {
290 debug("writing multicast hash to adapter failed\n");
291 return rc;
292 }
293
294 return 0;
295}
296
297/*
298 * mcs7830_set_autoneg() - setup and trigger ethernet link autonegotiation
Simon Glassb82f62d2015-11-29 13:18:10 -0700299 * @udev: network device to run link negotiation on
Gerhard Sittig2385df62014-03-08 19:46:14 +0100300 * Return: zero upon success, negative upon error
301 *
302 * the routine advertises available media and starts autonegotiation
303 */
Simon Glassb82f62d2015-11-29 13:18:10 -0700304static int mcs7830_set_autoneg(struct usb_device *udev)
Gerhard Sittig2385df62014-03-08 19:46:14 +0100305{
306 int adv, flg;
307 int rc;
308
309 debug("%s()\n", __func__);
310
311 /*
312 * algorithm taken from the Linux driver, which took it from
313 * "the original mcs7830 version 1.4 driver":
314 *
315 * enable all media, reset BMCR, enable auto neg, restart
316 * auto neg while keeping the enable auto neg flag set
317 */
318
319 adv = ADVERTISE_PAUSE_CAP | ADVERTISE_ALL | ADVERTISE_CSMA;
Simon Glassb82f62d2015-11-29 13:18:10 -0700320 rc = mcs7830_write_phy(udev, MII_ADVERTISE, adv);
Gerhard Sittig2385df62014-03-08 19:46:14 +0100321
322 flg = 0;
323 if (!rc)
Simon Glassb82f62d2015-11-29 13:18:10 -0700324 rc = mcs7830_write_phy(udev, MII_BMCR, flg);
Gerhard Sittig2385df62014-03-08 19:46:14 +0100325
326 flg |= BMCR_ANENABLE;
327 if (!rc)
Simon Glassb82f62d2015-11-29 13:18:10 -0700328 rc = mcs7830_write_phy(udev, MII_BMCR, flg);
Gerhard Sittig2385df62014-03-08 19:46:14 +0100329
330 flg |= BMCR_ANRESTART;
331 if (!rc)
Simon Glassb82f62d2015-11-29 13:18:10 -0700332 rc = mcs7830_write_phy(udev, MII_BMCR, flg);
Gerhard Sittig2385df62014-03-08 19:46:14 +0100333
334 return rc;
335}
336
337/*
338 * mcs7830_get_rev() - identify a network adapter's chip revision
Simon Glassb82f62d2015-11-29 13:18:10 -0700339 * @udev: network device to identify
Gerhard Sittig2385df62014-03-08 19:46:14 +0100340 * Return: non-negative number, reflecting the revision number
341 *
342 * currently, only "rev C and higher" and "below rev C" are needed, so
343 * the return value is #1 for "below rev C", and #2 for "rev C and above"
344 */
Simon Glassb82f62d2015-11-29 13:18:10 -0700345static int mcs7830_get_rev(struct usb_device *udev)
Gerhard Sittig2385df62014-03-08 19:46:14 +0100346{
347 uint8_t buf[2];
348 int rc;
349 int rev;
350
351 /* register 22 is readable in rev C and higher */
Simon Glassb82f62d2015-11-29 13:18:10 -0700352 rc = mcs7830_read_reg(udev, REG_FRAME_DROP_COUNTER, sizeof(buf), buf);
Gerhard Sittig2385df62014-03-08 19:46:14 +0100353 if (rc < 0)
354 rev = 1;
355 else
356 rev = 2;
357 debug("%s() rc=%d, rev=%d\n", __func__, rc, rev);
358 return rev;
359}
360
361/*
362 * mcs7830_apply_fixup() - identify an adapter and potentially apply fixups
Simon Glassb82f62d2015-11-29 13:18:10 -0700363 * @udev: network device to identify and apply fixups to
Gerhard Sittig2385df62014-03-08 19:46:14 +0100364 * Return: zero upon success (no errors emitted from here)
365 *
366 * this routine identifies the network adapter's chip revision, and applies
367 * fixups for known issues
368 */
Simon Glassb82f62d2015-11-29 13:18:10 -0700369static int mcs7830_apply_fixup(struct usb_device *udev)
Gerhard Sittig2385df62014-03-08 19:46:14 +0100370{
371 int rev;
372 int i;
373 uint8_t thr;
374
Simon Glassb82f62d2015-11-29 13:18:10 -0700375 rev = mcs7830_get_rev(udev);
Gerhard Sittig2385df62014-03-08 19:46:14 +0100376 debug("%s() rev=%d\n", __func__, rev);
377
378 /*
379 * rev C requires setting the pause threshold (the Linux driver
380 * is inconsistent, the implementation does it for "rev C
381 * exactly", the introductory comment says "rev C and above")
382 */
383 if (rev == 2) {
Simon Glassb82f62d2015-11-29 13:18:10 -0700384 debug("%s: applying rev C fixup\n", __func__);
Gerhard Sittig2385df62014-03-08 19:46:14 +0100385 thr = PAUSE_THRESHOLD_DEFAULT;
386 for (i = 0; i < 2; i++) {
Simon Glassb82f62d2015-11-29 13:18:10 -0700387 (void)mcs7830_write_reg(udev, REG_PAUSE_THRESHOLD,
Gerhard Sittig2385df62014-03-08 19:46:14 +0100388 sizeof(thr), &thr);
389 mdelay(1);
390 }
391 }
392
393 return 0;
394}
395
396/*
397 * mcs7830_basic_reset() - bring the network adapter into a known first state
398 * @eth: network device to act upon
399 * Return: zero upon success, negative upon error
400 *
401 * this routine initializes the network adapter such that subsequent invocations
402 * of the interface callbacks can exchange ethernet frames; link negotiation is
403 * triggered from here already and continues in background
404 */
Simon Glassb82f62d2015-11-29 13:18:10 -0700405static int mcs7830_basic_reset(struct usb_device *udev,
406 struct mcs7830_private *priv)
Gerhard Sittig2385df62014-03-08 19:46:14 +0100407{
Gerhard Sittig2385df62014-03-08 19:46:14 +0100408 int rc;
409
410 debug("%s()\n", __func__);
Gerhard Sittig2385df62014-03-08 19:46:14 +0100411
412 /*
413 * comment from the respective Linux driver, which
414 * unconditionally sets the ALLMULTICAST flag as well:
415 * should not be needed, but does not work otherwise
416 */
417 priv->config = CONF_TXENABLE;
418 priv->config |= CONF_ALLMULTICAST;
419
Simon Glassb82f62d2015-11-29 13:18:10 -0700420 rc = mcs7830_set_autoneg(udev);
Gerhard Sittig2385df62014-03-08 19:46:14 +0100421 if (rc < 0) {
Masahiro Yamada81e10422017-09-16 14:10:41 +0900422 pr_err("setting autoneg failed\n");
Gerhard Sittig2385df62014-03-08 19:46:14 +0100423 return rc;
424 }
425
Simon Glassb82f62d2015-11-29 13:18:10 -0700426 rc = mcs7830_write_mchash(udev, priv);
Gerhard Sittig2385df62014-03-08 19:46:14 +0100427 if (rc < 0) {
Masahiro Yamada81e10422017-09-16 14:10:41 +0900428 pr_err("failed to set multicast hash\n");
Gerhard Sittig2385df62014-03-08 19:46:14 +0100429 return rc;
430 }
431
Simon Glassb82f62d2015-11-29 13:18:10 -0700432 rc = mcs7830_write_config(udev, priv);
Gerhard Sittig2385df62014-03-08 19:46:14 +0100433 if (rc < 0) {
Masahiro Yamada81e10422017-09-16 14:10:41 +0900434 pr_err("failed to set configuration\n");
Gerhard Sittig2385df62014-03-08 19:46:14 +0100435 return rc;
436 }
437
Simon Glassb82f62d2015-11-29 13:18:10 -0700438 rc = mcs7830_apply_fixup(udev);
Gerhard Sittig2385df62014-03-08 19:46:14 +0100439 if (rc < 0) {
Masahiro Yamada81e10422017-09-16 14:10:41 +0900440 pr_err("fixup application failed\n");
Gerhard Sittig2385df62014-03-08 19:46:14 +0100441 return rc;
442 }
443
444 return 0;
445}
446
447/*
448 * mcs7830_read_mac() - read an ethernet adapter's MAC address
Simon Glassb82f62d2015-11-29 13:18:10 -0700449 * @udev: network device to read from
450 * @enetaddr: place to put ethernet MAC address
Gerhard Sittig2385df62014-03-08 19:46:14 +0100451 * Return: zero upon success, negative upon error
452 *
453 * this routine fetches the MAC address stored within the ethernet adapter,
454 * and stores it in the ethernet interface's data structure
455 */
Simon Glassb82f62d2015-11-29 13:18:10 -0700456static int mcs7830_read_mac(struct usb_device *udev, unsigned char enetaddr[])
Gerhard Sittig2385df62014-03-08 19:46:14 +0100457{
Gerhard Sittig2385df62014-03-08 19:46:14 +0100458 int rc;
459 uint8_t buf[ETH_ALEN];
460
461 debug("%s()\n", __func__);
Gerhard Sittig2385df62014-03-08 19:46:14 +0100462
Simon Glassb82f62d2015-11-29 13:18:10 -0700463 rc = mcs7830_read_reg(udev, REG_ETHER_ADDR, ETH_ALEN, buf);
Gerhard Sittig2385df62014-03-08 19:46:14 +0100464 if (rc < 0) {
465 debug("reading MAC from adapter failed\n");
466 return rc;
467 }
468
Simon Glassb82f62d2015-11-29 13:18:10 -0700469 memcpy(enetaddr, buf, ETH_ALEN);
Gerhard Sittig2385df62014-03-08 19:46:14 +0100470 return 0;
471}
472
Simon Glassb82f62d2015-11-29 13:18:10 -0700473static int mcs7830_write_mac_common(struct usb_device *udev,
474 unsigned char enetaddr[])
Gerhard Sittig2385df62014-03-08 19:46:14 +0100475{
Gerhard Sittig2385df62014-03-08 19:46:14 +0100476 int rc;
477
478 debug("%s()\n", __func__);
Gerhard Sittig2385df62014-03-08 19:46:14 +0100479
Simon Glassb82f62d2015-11-29 13:18:10 -0700480 rc = mcs7830_write_reg(udev, REG_ETHER_ADDR, ETH_ALEN, enetaddr);
Gerhard Sittig2385df62014-03-08 19:46:14 +0100481 if (rc < 0) {
482 debug("writing MAC to adapter failed\n");
483 return rc;
484 }
485 return 0;
486}
487
Simon Glassb82f62d2015-11-29 13:18:10 -0700488static int mcs7830_init_common(struct usb_device *udev)
Gerhard Sittig2385df62014-03-08 19:46:14 +0100489{
Gerhard Sittig2385df62014-03-08 19:46:14 +0100490 int timeout;
491 int have_link;
492
493 debug("%s()\n", __func__);
Gerhard Sittig2385df62014-03-08 19:46:14 +0100494
495 timeout = 0;
496 do {
Simon Glassb82f62d2015-11-29 13:18:10 -0700497 have_link = mcs7830_read_phy(udev, MII_BMSR) & BMSR_LSTATUS;
Gerhard Sittig2385df62014-03-08 19:46:14 +0100498 if (have_link)
499 break;
500 udelay(LINKSTATUS_TIMEOUT_RES * 1000);
501 timeout += LINKSTATUS_TIMEOUT_RES;
502 } while (timeout < LINKSTATUS_TIMEOUT);
503 if (!have_link) {
504 debug("ethernet link is down\n");
505 return -ETIMEDOUT;
506 }
507 return 0;
508}
509
Simon Glassb82f62d2015-11-29 13:18:10 -0700510static int mcs7830_send_common(struct ueth_data *ueth, void *packet,
511 int length)
Gerhard Sittig2385df62014-03-08 19:46:14 +0100512{
Simon Glassb82f62d2015-11-29 13:18:10 -0700513 struct usb_device *udev = ueth->pusb_dev;
Gerhard Sittig2385df62014-03-08 19:46:14 +0100514 int rc;
515 int gotlen;
516 /* there is a status byte after the ethernet frame */
517 ALLOC_CACHE_ALIGN_BUFFER(uint8_t, buf, PKTSIZE + sizeof(uint8_t));
518
Gerhard Sittig2385df62014-03-08 19:46:14 +0100519 memcpy(buf, packet, length);
Simon Glassb82f62d2015-11-29 13:18:10 -0700520 rc = usb_bulk_msg(udev,
521 usb_sndbulkpipe(udev, ueth->ep_out),
Gerhard Sittig2385df62014-03-08 19:46:14 +0100522 &buf[0], length, &gotlen,
523 USBCALL_TIMEOUT);
524 debug("%s() TX want len %d, got len %d, rc %d\n",
525 __func__, length, gotlen, rc);
526 return rc;
527}
528
Simon Glassb82f62d2015-11-29 13:18:10 -0700529static int mcs7830_recv_common(struct ueth_data *ueth, uint8_t *buf)
Gerhard Sittig2385df62014-03-08 19:46:14 +0100530{
Gerhard Sittig2385df62014-03-08 19:46:14 +0100531 int rc, wantlen, gotlen;
532 uint8_t sts;
533
534 debug("%s()\n", __func__);
Gerhard Sittig2385df62014-03-08 19:46:14 +0100535
536 /* fetch input data from the adapter */
537 wantlen = MCS7830_RX_URB_SIZE;
Simon Glassb82f62d2015-11-29 13:18:10 -0700538 rc = usb_bulk_msg(ueth->pusb_dev,
539 usb_rcvbulkpipe(ueth->pusb_dev, ueth->ep_in),
Gerhard Sittig2385df62014-03-08 19:46:14 +0100540 &buf[0], wantlen, &gotlen,
541 USBCALL_TIMEOUT);
542 debug("%s() RX want len %d, got len %d, rc %d\n",
543 __func__, wantlen, gotlen, rc);
544 if (rc != 0) {
Masahiro Yamada81e10422017-09-16 14:10:41 +0900545 pr_err("RX: failed to receive\n");
Gerhard Sittig2385df62014-03-08 19:46:14 +0100546 return rc;
547 }
548 if (gotlen > wantlen) {
Masahiro Yamada81e10422017-09-16 14:10:41 +0900549 pr_err("RX: got too many bytes (%d)\n", gotlen);
Gerhard Sittig2385df62014-03-08 19:46:14 +0100550 return -EIO;
551 }
552
553 /*
554 * the bulk message that we received from USB contains exactly
555 * one ethernet frame and a trailing status byte
556 */
557 if (gotlen < sizeof(sts))
558 return -EIO;
559 gotlen -= sizeof(sts);
560 sts = buf[gotlen];
561
562 if (sts == STAT_RX_FRAME_CORRECT) {
563 debug("%s() got a frame, len=%d\n", __func__, gotlen);
Simon Glassb82f62d2015-11-29 13:18:10 -0700564 return gotlen;
Gerhard Sittig2385df62014-03-08 19:46:14 +0100565 }
566
567 debug("RX: frame error (sts 0x%02X, %s %s %s %s %s)\n",
568 sts,
569 (sts & STAT_RX_LARGE_FRAME) ? "large" : "-",
570 (sts & STAT_RX_LENGTH_ERROR) ? "length" : "-",
571 (sts & STAT_RX_SHORT_FRAME) ? "short" : "-",
572 (sts & STAT_RX_CRC_ERROR) ? "crc" : "-",
573 (sts & STAT_RX_ALIGNMENT_ERROR) ? "align" : "-");
574 return -EIO;
575}
576
Simon Glassfd954e52015-11-29 13:18:11 -0700577#ifndef CONFIG_DM_ETH
Gerhard Sittig2385df62014-03-08 19:46:14 +0100578/*
Simon Glassb82f62d2015-11-29 13:18:10 -0700579 * mcs7830_init() - network interface's init callback
580 * @udev: network device to initialize
581 * @bd: board information
582 * Return: zero upon success, negative upon error
583 *
584 * after initial setup during probe() and get_info(), this init() callback
585 * ensures that the link is up and subsequent send() and recv() calls can
586 * exchange ethernet frames
587 */
588static int mcs7830_init(struct eth_device *eth, bd_t *bd)
589{
590 struct ueth_data *dev = eth->priv;
591
592 return mcs7830_init_common(dev->pusb_dev);
593}
594
595/*
596 * mcs7830_send() - network interface's send callback
597 * @eth: network device to send the frame from
598 * @packet: ethernet frame content
599 * @length: ethernet frame length
600 * Return: zero upon success, negative upon error
601 *
602 * this routine send an ethernet frame out of the network interface
603 */
604static int mcs7830_send(struct eth_device *eth, void *packet, int length)
605{
606 struct ueth_data *dev = eth->priv;
607
608 return mcs7830_send_common(dev, packet, length);
609}
610
611/*
612 * mcs7830_recv() - network interface's recv callback
613 * @eth: network device to receive frames from
614 * Return: zero upon success, negative upon error
615 *
616 * this routine checks for available ethernet frames that the network
617 * interface might have received, and notifies the network stack
618 */
619static int mcs7830_recv(struct eth_device *eth)
620{
621 ALLOC_CACHE_ALIGN_BUFFER(uint8_t, buf, MCS7830_RX_URB_SIZE);
622 struct ueth_data *ueth = eth->priv;
623 int len;
624
625 len = mcs7830_recv_common(ueth, buf);
Uri Mashiach258a3202017-05-21 17:00:52 +0300626 if (len >= 0) {
Simon Glassb82f62d2015-11-29 13:18:10 -0700627 net_process_received_packet(buf, len);
Uri Mashiach258a3202017-05-21 17:00:52 +0300628 return 0;
629 }
Simon Glassb82f62d2015-11-29 13:18:10 -0700630
Uri Mashiach258a3202017-05-21 17:00:52 +0300631 return len;
Simon Glassb82f62d2015-11-29 13:18:10 -0700632}
633
634/*
Gerhard Sittig2385df62014-03-08 19:46:14 +0100635 * mcs7830_halt() - network interface's halt callback
636 * @eth: network device to cease operation of
637 * Return: none
638 *
639 * this routine is supposed to undo the effect of previous initialization and
640 * ethernet frames exchange; in this implementation it's a NOP
641 */
642static void mcs7830_halt(struct eth_device *eth)
643{
644 debug("%s()\n", __func__);
645}
646
647/*
Simon Glassb82f62d2015-11-29 13:18:10 -0700648 * mcs7830_write_mac() - write an ethernet adapter's MAC address
649 * @eth: network device to write to
650 * Return: zero upon success, negative upon error
651 *
652 * this routine takes the MAC address from the ethernet interface's data
653 * structure, and writes it into the ethernet adapter such that subsequent
654 * exchange of ethernet frames uses this address
655 */
656static int mcs7830_write_mac(struct eth_device *eth)
657{
658 struct ueth_data *ueth = eth->priv;
659
660 return mcs7830_write_mac_common(ueth->pusb_dev, eth->enetaddr);
661}
662
663/*
Gerhard Sittig2385df62014-03-08 19:46:14 +0100664 * mcs7830_iface_idx - index of detected network interfaces
665 *
666 * this counter keeps track of identified supported interfaces,
667 * to assign unique names as more interfaces are found
668 */
669static int mcs7830_iface_idx;
670
671/*
672 * mcs7830_eth_before_probe() - network driver's before_probe callback
673 * Return: none
674 *
675 * this routine initializes driver's internal data in preparation of
676 * subsequent probe callbacks
677 */
678void mcs7830_eth_before_probe(void)
679{
680 mcs7830_iface_idx = 0;
681}
682
683/*
684 * struct mcs7830_dongle - description of a supported Moschip ethernet dongle
685 * @vendor: 16bit USB vendor identification
686 * @product: 16bit USB product identification
687 *
688 * this structure describes a supported USB ethernet dongle by means of the
689 * vendor and product codes found during USB enumeration; no flags are held
690 * here since all supported dongles have identical behaviour, and required
691 * fixups get determined at runtime, such that no manual configuration is
692 * needed
693 */
694struct mcs7830_dongle {
695 uint16_t vendor;
696 uint16_t product;
697};
698
699/*
700 * mcs7830_dongles - the list of supported Moschip based USB ethernet dongles
701 */
Jeroen Hofstee5528f4c2014-06-17 21:14:17 +0200702static const struct mcs7830_dongle mcs7830_dongles[] = {
Gerhard Sittig2385df62014-03-08 19:46:14 +0100703 { 0x9710, 0x7832, }, /* Moschip 7832 */
704 { 0x9710, 0x7830, }, /* Moschip 7830 */
705 { 0x9710, 0x7730, }, /* Moschip 7730 */
706 { 0x0df6, 0x0021, }, /* Sitecom LN 30 */
707};
708
709/*
710 * mcs7830_eth_probe() - network driver's probe callback
711 * @dev: detected USB device to check
712 * @ifnum: detected USB interface to check
713 * @ss: USB ethernet data structure to fill in upon match
714 * Return: #1 upon match, #0 upon mismatch or error
715 *
716 * this routine checks whether the found USB device is supported by
717 * this ethernet driver, and upon match fills in the USB ethernet
718 * data structure which later is passed to the get_info callback
719 */
720int mcs7830_eth_probe(struct usb_device *dev, unsigned int ifnum,
721 struct ueth_data *ss)
722{
723 struct usb_interface *iface;
724 struct usb_interface_descriptor *iface_desc;
725 int i;
726 struct mcs7830_private *priv;
727 int ep_in_found, ep_out_found, ep_intr_found;
728
729 debug("%s()\n", __func__);
730
731 /* iterate the list of supported dongles */
732 iface = &dev->config.if_desc[ifnum];
733 iface_desc = &iface->desc;
734 for (i = 0; i < ARRAY_SIZE(mcs7830_dongles); i++) {
735 if (dev->descriptor.idVendor == mcs7830_dongles[i].vendor &&
736 dev->descriptor.idProduct == mcs7830_dongles[i].product)
737 break;
738 }
739 if (i == ARRAY_SIZE(mcs7830_dongles))
740 return 0;
741 debug("detected USB ethernet device: %04X:%04X\n",
742 dev->descriptor.idVendor, dev->descriptor.idProduct);
743
744 /* fill in driver private data */
745 priv = calloc(1, sizeof(*priv));
746 if (!priv)
747 return 0;
748
749 /* fill in the ueth_data structure, attach private data */
750 memset(ss, 0, sizeof(*ss));
751 ss->ifnum = ifnum;
752 ss->pusb_dev = dev;
753 ss->subclass = iface_desc->bInterfaceSubClass;
754 ss->protocol = iface_desc->bInterfaceProtocol;
755 ss->dev_priv = priv;
756
757 /*
758 * a minimum of three endpoints is expected: in (bulk),
759 * out (bulk), and interrupt; ignore all others
760 */
761 ep_in_found = ep_out_found = ep_intr_found = 0;
762 for (i = 0; i < iface_desc->bNumEndpoints; i++) {
763 uint8_t eptype, epaddr;
764 bool is_input;
765
766 eptype = iface->ep_desc[i].bmAttributes;
767 eptype &= USB_ENDPOINT_XFERTYPE_MASK;
768
769 epaddr = iface->ep_desc[i].bEndpointAddress;
770 is_input = epaddr & USB_DIR_IN;
771 epaddr &= USB_ENDPOINT_NUMBER_MASK;
772
773 if (eptype == USB_ENDPOINT_XFER_BULK) {
774 if (is_input && !ep_in_found) {
775 ss->ep_in = epaddr;
776 ep_in_found++;
777 }
778 if (!is_input && !ep_out_found) {
779 ss->ep_out = epaddr;
780 ep_out_found++;
781 }
782 }
783
784 if (eptype == USB_ENDPOINT_XFER_INT) {
785 if (is_input && !ep_intr_found) {
786 ss->ep_int = epaddr;
787 ss->irqinterval = iface->ep_desc[i].bInterval;
788 ep_intr_found++;
789 }
790 }
791 }
792 debug("endpoints: in %d, out %d, intr %d\n",
793 ss->ep_in, ss->ep_out, ss->ep_int);
794
795 /* apply basic sanity checks */
796 if (usb_set_interface(dev, iface_desc->bInterfaceNumber, 0) ||
797 !ss->ep_in || !ss->ep_out || !ss->ep_int) {
798 debug("device probe incomplete\n");
799 return 0;
800 }
801
802 dev->privptr = ss;
803 return 1;
804}
805
806/*
807 * mcs7830_eth_get_info() - network driver's get_info callback
808 * @dev: detected USB device
809 * @ss: USB ethernet data structure filled in at probe()
810 * @eth: ethernet interface data structure to fill in
811 * Return: #1 upon success, #0 upon error
812 *
813 * this routine registers the mandatory init(), send(), recv(), and
814 * halt() callbacks with the ethernet interface, can register the
815 * optional write_hwaddr() callback with the ethernet interface,
816 * and initiates configuration of the interface such that subsequent
817 * calls to those callbacks results in network communication
818 */
819int mcs7830_eth_get_info(struct usb_device *dev, struct ueth_data *ss,
820 struct eth_device *eth)
821{
822 debug("%s()\n", __func__);
823 if (!eth) {
824 debug("%s: missing parameter.\n", __func__);
825 return 0;
826 }
827
828 snprintf(eth->name, sizeof(eth->name), "%s%d",
829 MCS7830_BASE_NAME, mcs7830_iface_idx++);
830 eth->init = mcs7830_init;
831 eth->send = mcs7830_send;
832 eth->recv = mcs7830_recv;
833 eth->halt = mcs7830_halt;
834 eth->write_hwaddr = mcs7830_write_mac;
835 eth->priv = ss;
836
Simon Glassb82f62d2015-11-29 13:18:10 -0700837 if (mcs7830_basic_reset(ss->pusb_dev, ss->dev_priv))
Gerhard Sittig2385df62014-03-08 19:46:14 +0100838 return 0;
839
Simon Glassb82f62d2015-11-29 13:18:10 -0700840 if (mcs7830_read_mac(ss->pusb_dev, eth->enetaddr))
Gerhard Sittig2385df62014-03-08 19:46:14 +0100841 return 0;
842 debug("MAC %pM\n", eth->enetaddr);
843
844 return 1;
845}
Simon Glassfd954e52015-11-29 13:18:11 -0700846#endif
847
848
849#ifdef CONFIG_DM_ETH
850static int mcs7830_eth_start(struct udevice *dev)
851{
852 struct usb_device *udev = dev_get_parent_priv(dev);
853
854 return mcs7830_init_common(udev);
855}
856
857void mcs7830_eth_stop(struct udevice *dev)
858{
859 debug("** %s()\n", __func__);
860}
861
862int mcs7830_eth_send(struct udevice *dev, void *packet, int length)
863{
864 struct mcs7830_private *priv = dev_get_priv(dev);
865 struct ueth_data *ueth = &priv->ueth;
866
867 return mcs7830_send_common(ueth, packet, length);
868}
869
870int mcs7830_eth_recv(struct udevice *dev, int flags, uchar **packetp)
871{
872 struct mcs7830_private *priv = dev_get_priv(dev);
873 struct ueth_data *ueth = &priv->ueth;
874 int len;
875
876 len = mcs7830_recv_common(ueth, priv->rx_buf);
877 *packetp = priv->rx_buf;
878
879 return len;
880}
881
882static int mcs7830_free_pkt(struct udevice *dev, uchar *packet, int packet_len)
883{
884 struct mcs7830_private *priv = dev_get_priv(dev);
885
886 packet_len = ALIGN(packet_len, 4);
887 usb_ether_advance_rxbuf(&priv->ueth, sizeof(u32) + packet_len);
888
889 return 0;
890}
891
892int mcs7830_write_hwaddr(struct udevice *dev)
893{
894 struct usb_device *udev = dev_get_parent_priv(dev);
895 struct eth_pdata *pdata = dev_get_platdata(dev);
896
897 return mcs7830_write_mac_common(udev, pdata->enetaddr);
898}
899
900static int mcs7830_eth_probe(struct udevice *dev)
901{
902 struct usb_device *udev = dev_get_parent_priv(dev);
903 struct mcs7830_private *priv = dev_get_priv(dev);
904 struct eth_pdata *pdata = dev_get_platdata(dev);
905 struct ueth_data *ueth = &priv->ueth;
906
907 if (mcs7830_basic_reset(udev, priv))
908 return 0;
909
910 if (mcs7830_read_mac(udev, pdata->enetaddr))
911 return 0;
912
913 return usb_ether_register(dev, ueth, MCS7830_RX_URB_SIZE);
914}
915
916static const struct eth_ops mcs7830_eth_ops = {
917 .start = mcs7830_eth_start,
918 .send = mcs7830_eth_send,
919 .recv = mcs7830_eth_recv,
920 .free_pkt = mcs7830_free_pkt,
921 .stop = mcs7830_eth_stop,
922 .write_hwaddr = mcs7830_write_hwaddr,
923};
924
925U_BOOT_DRIVER(mcs7830_eth) = {
926 .name = "mcs7830_eth",
927 .id = UCLASS_ETH,
928 .probe = mcs7830_eth_probe,
929 .ops = &mcs7830_eth_ops,
930 .priv_auto_alloc_size = sizeof(struct mcs7830_private),
931 .platdata_auto_alloc_size = sizeof(struct eth_pdata),
932 .flags = DM_FLAG_ALLOC_PRIV_DMA,
933};
934
935static const struct usb_device_id mcs7830_eth_id_table[] = {
936 { USB_DEVICE(0x9710, 0x7832) }, /* Moschip 7832 */
937 { USB_DEVICE(0x9710, 0x7830), }, /* Moschip 7830 */
938 { USB_DEVICE(0x9710, 0x7730), }, /* Moschip 7730 */
939 { USB_DEVICE(0x0df6, 0x0021), }, /* Sitecom LN 30 */
940 { } /* Terminating entry */
941};
942
943U_BOOT_USB_DEVICE(mcs7830_eth, mcs7830_eth_id_table);
944#endif