blob: 75c45ee0ae8b19675778019a3a1ec46fb9d3eb31 [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>
15#include <linux/mii.h>
16#include <malloc.h>
Simon Glasscf92e052015-09-02 17:24:58 -060017#include <memalign.h>
Gerhard Sittigdf4fb1c2014-03-08 19:46:14 +010018#include <usb.h>
19
20#include "usb_ether.h"
21
22#define MCS7830_BASE_NAME "mcs"
23
24#define USBCALL_TIMEOUT 1000
25#define LINKSTATUS_TIMEOUT 5000 /* link status, connect timeout */
26#define LINKSTATUS_TIMEOUT_RES 50 /* link status, resolution in msec */
27
28#define MCS7830_RX_URB_SIZE 2048
29
30/* command opcodes */
31#define MCS7830_WR_BREQ 0x0d
32#define MCS7830_RD_BREQ 0x0e
33
34/* register layout, numerical offset specs for USB API calls */
35struct mcs7830_regs {
36 uint8_t multicast_hashes[8];
37 uint8_t packet_gap[2];
38 uint8_t phy_data[2];
39 uint8_t phy_command[2];
40 uint8_t configuration;
41 uint8_t ether_address[6];
42 uint8_t frame_drop_count;
43 uint8_t pause_threshold;
44};
45#define REG_MULTICAST_HASH offsetof(struct mcs7830_regs, multicast_hashes)
46#define REG_PHY_DATA offsetof(struct mcs7830_regs, phy_data)
47#define REG_PHY_CMD offsetof(struct mcs7830_regs, phy_command)
48#define REG_CONFIG offsetof(struct mcs7830_regs, configuration)
49#define REG_ETHER_ADDR offsetof(struct mcs7830_regs, ether_address)
50#define REG_FRAME_DROP_COUNTER offsetof(struct mcs7830_regs, frame_drop_count)
51#define REG_PAUSE_THRESHOLD offsetof(struct mcs7830_regs, pause_threshold)
52
53/* bit masks and default values for the above registers */
54#define PHY_CMD1_READ 0x40
55#define PHY_CMD1_WRITE 0x20
56#define PHY_CMD1_PHYADDR 0x01
57
58#define PHY_CMD2_PEND 0x80
59#define PHY_CMD2_READY 0x40
60
61#define CONF_CFG 0x80
62#define CONF_SPEED100 0x40
63#define CONF_FDX_ENABLE 0x20
64#define CONF_RXENABLE 0x10
65#define CONF_TXENABLE 0x08
66#define CONF_SLEEPMODE 0x04
67#define CONF_ALLMULTICAST 0x02
68#define CONF_PROMISCUOUS 0x01
69
70#define PAUSE_THRESHOLD_DEFAULT 0
71
72/* bit masks for the status byte which follows received ethernet frames */
73#define STAT_RX_FRAME_CORRECT 0x20
74#define STAT_RX_LARGE_FRAME 0x10
75#define STAT_RX_CRC_ERROR 0x08
76#define STAT_RX_ALIGNMENT_ERROR 0x04
77#define STAT_RX_LENGTH_ERROR 0x02
78#define STAT_RX_SHORT_FRAME 0x01
79
80/*
81 * struct mcs7830_private - private driver data for an individual adapter
82 * @config: shadow for the network adapter's configuration register
83 * @mchash: shadow for the network adapter's multicast hash registers
84 */
85struct mcs7830_private {
Simon Glassd4f847e2015-11-29 13:18:11 -070086#ifdef CONFIG_DM_ETH
87 uint8_t rx_buf[MCS7830_RX_URB_SIZE];
88 struct ueth_data ueth;
89#endif
Gerhard Sittigdf4fb1c2014-03-08 19:46:14 +010090 uint8_t config;
91 uint8_t mchash[8];
92};
93
94/*
95 * mcs7830_read_reg() - read a register of the network adapter
Simon Glassce932c72015-11-29 13:18:10 -070096 * @udev: network device to read from
Gerhard Sittigdf4fb1c2014-03-08 19:46:14 +010097 * @idx: index of the register to start reading from
98 * @size: number of bytes to read
99 * @data: buffer to read into
100 * Return: zero upon success, negative upon error
101 */
Simon Glassce932c72015-11-29 13:18:10 -0700102static int mcs7830_read_reg(struct usb_device *udev, uint8_t idx,
Gerhard Sittigdf4fb1c2014-03-08 19:46:14 +0100103 uint16_t size, void *data)
104{
105 int len;
106 ALLOC_CACHE_ALIGN_BUFFER(uint8_t, buf, size);
107
108 debug("%s() idx=0x%04X sz=%d\n", __func__, idx, size);
109
Simon Glassce932c72015-11-29 13:18:10 -0700110 len = usb_control_msg(udev,
111 usb_rcvctrlpipe(udev, 0),
Gerhard Sittigdf4fb1c2014-03-08 19:46:14 +0100112 MCS7830_RD_BREQ,
113 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
114 0, idx, buf, size,
115 USBCALL_TIMEOUT);
116 if (len != size) {
117 debug("%s() len=%d != sz=%d\n", __func__, len, size);
118 return -EIO;
119 }
120 memcpy(data, buf, size);
121 return 0;
122}
123
124/*
125 * mcs7830_write_reg() - write a register of the network adapter
Simon Glassce932c72015-11-29 13:18:10 -0700126 * @udev: network device to write to
Gerhard Sittigdf4fb1c2014-03-08 19:46:14 +0100127 * @idx: index of the register to start writing to
128 * @size: number of bytes to write
129 * @data: buffer holding the data to write
130 * Return: zero upon success, negative upon error
131 */
Simon Glassce932c72015-11-29 13:18:10 -0700132static int mcs7830_write_reg(struct usb_device *udev, uint8_t idx,
Gerhard Sittigdf4fb1c2014-03-08 19:46:14 +0100133 uint16_t size, void *data)
134{
135 int len;
136 ALLOC_CACHE_ALIGN_BUFFER(uint8_t, buf, size);
137
138 debug("%s() idx=0x%04X sz=%d\n", __func__, idx, size);
139
140 memcpy(buf, data, size);
Simon Glassce932c72015-11-29 13:18:10 -0700141 len = usb_control_msg(udev,
142 usb_sndctrlpipe(udev, 0),
Gerhard Sittigdf4fb1c2014-03-08 19:46:14 +0100143 MCS7830_WR_BREQ,
144 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
145 0, idx, buf, size,
146 USBCALL_TIMEOUT);
147 if (len != size) {
148 debug("%s() len=%d != sz=%d\n", __func__, len, size);
149 return -EIO;
150 }
151 return 0;
152}
153
154/*
155 * mcs7830_phy_emit_wait() - emit PHY read/write access, wait for its execution
Simon Glassce932c72015-11-29 13:18:10 -0700156 * @udev: network device to talk to
Gerhard Sittigdf4fb1c2014-03-08 19:46:14 +0100157 * @rwflag: PHY_CMD1_READ or PHY_CMD1_WRITE opcode
158 * @index: number of the PHY register to read or write
159 * Return: zero upon success, negative upon error
160 */
Simon Glassce932c72015-11-29 13:18:10 -0700161static int mcs7830_phy_emit_wait(struct usb_device *udev,
Gerhard Sittigdf4fb1c2014-03-08 19:46:14 +0100162 uint8_t rwflag, uint8_t index)
163{
164 int rc;
165 int retry;
166 uint8_t cmd[2];
167
168 /* send the PHY read/write request */
169 cmd[0] = rwflag | PHY_CMD1_PHYADDR;
170 cmd[1] = PHY_CMD2_PEND | (index & 0x1f);
Simon Glassce932c72015-11-29 13:18:10 -0700171 rc = mcs7830_write_reg(udev, REG_PHY_CMD, sizeof(cmd), cmd);
Gerhard Sittigdf4fb1c2014-03-08 19:46:14 +0100172 if (rc < 0)
173 return rc;
174
175 /* wait for the response to become available (usually < 1ms) */
176 retry = 10;
177 do {
Simon Glassce932c72015-11-29 13:18:10 -0700178 rc = mcs7830_read_reg(udev, REG_PHY_CMD, sizeof(cmd), cmd);
Gerhard Sittigdf4fb1c2014-03-08 19:46:14 +0100179 if (rc < 0)
180 return rc;
181 if (cmd[1] & PHY_CMD2_READY)
182 return 0;
183 if (!retry--)
184 return -ETIMEDOUT;
185 mdelay(1);
186 } while (1);
187 /* UNREACH */
188}
189
190/*
191 * mcs7830_read_phy() - read a PHY register of the network adapter
Simon Glassce932c72015-11-29 13:18:10 -0700192 * @udev: network device to read from
Gerhard Sittigdf4fb1c2014-03-08 19:46:14 +0100193 * @index: index of the PHY register to read from
194 * Return: non-negative 16bit register content, negative upon error
195 */
Simon Glassce932c72015-11-29 13:18:10 -0700196static int mcs7830_read_phy(struct usb_device *udev, uint8_t index)
Gerhard Sittigdf4fb1c2014-03-08 19:46:14 +0100197{
198 int rc;
199 uint16_t val;
200
201 /* issue the PHY read request and wait for its execution */
Simon Glassce932c72015-11-29 13:18:10 -0700202 rc = mcs7830_phy_emit_wait(udev, PHY_CMD1_READ, index);
Gerhard Sittigdf4fb1c2014-03-08 19:46:14 +0100203 if (rc < 0)
204 return rc;
205
206 /* fetch the PHY data which was read */
Simon Glassce932c72015-11-29 13:18:10 -0700207 rc = mcs7830_read_reg(udev, REG_PHY_DATA, sizeof(val), &val);
Gerhard Sittigdf4fb1c2014-03-08 19:46:14 +0100208 if (rc < 0)
209 return rc;
210 rc = le16_to_cpu(val);
Simon Glassce932c72015-11-29 13:18:10 -0700211 debug("%s(%d) => 0x%04X\n", __func__, index, rc);
Gerhard Sittigdf4fb1c2014-03-08 19:46:14 +0100212 return rc;
213}
214
215/*
216 * mcs7830_write_phy() - write a PHY register of the network adapter
Simon Glassce932c72015-11-29 13:18:10 -0700217 * @udev: network device to write to
Gerhard Sittigdf4fb1c2014-03-08 19:46:14 +0100218 * @index: index of the PHY register to write to
219 * @val: value to write to the PHY register
220 * Return: zero upon success, negative upon error
221 */
Simon Glassce932c72015-11-29 13:18:10 -0700222static int mcs7830_write_phy(struct usb_device *udev, uint8_t index,
223 uint16_t val)
Gerhard Sittigdf4fb1c2014-03-08 19:46:14 +0100224{
225 int rc;
226
Simon Glassce932c72015-11-29 13:18:10 -0700227 debug("%s(%d, 0x%04X)\n", __func__, index, val);
Gerhard Sittigdf4fb1c2014-03-08 19:46:14 +0100228
229 /* setup the PHY data which is to get written */
230 val = cpu_to_le16(val);
Simon Glassce932c72015-11-29 13:18:10 -0700231 rc = mcs7830_write_reg(udev, REG_PHY_DATA, sizeof(val), &val);
Gerhard Sittigdf4fb1c2014-03-08 19:46:14 +0100232 if (rc < 0)
233 return rc;
234
235 /* issue the PHY write request and wait for its execution */
Simon Glassce932c72015-11-29 13:18:10 -0700236 rc = mcs7830_phy_emit_wait(udev, PHY_CMD1_WRITE, index);
Gerhard Sittigdf4fb1c2014-03-08 19:46:14 +0100237 if (rc < 0)
238 return rc;
239
240 return 0;
241}
242
243/*
244 * mcs7830_write_config() - write to the network adapter's config register
Simon Glassce932c72015-11-29 13:18:10 -0700245 * @udev: network device to write to
246 * @priv: private data
Gerhard Sittigdf4fb1c2014-03-08 19:46:14 +0100247 * Return: zero upon success, negative upon error
248 *
249 * the data which gets written is taken from the shadow config register
250 * within the device driver's private data
251 */
Simon Glassce932c72015-11-29 13:18:10 -0700252static int mcs7830_write_config(struct usb_device *udev,
253 struct mcs7830_private *priv)
Gerhard Sittigdf4fb1c2014-03-08 19:46:14 +0100254{
Gerhard Sittigdf4fb1c2014-03-08 19:46:14 +0100255 int rc;
256
257 debug("%s()\n", __func__);
Gerhard Sittigdf4fb1c2014-03-08 19:46:14 +0100258
Simon Glassce932c72015-11-29 13:18:10 -0700259 rc = mcs7830_write_reg(udev, REG_CONFIG,
Gerhard Sittigdf4fb1c2014-03-08 19:46:14 +0100260 sizeof(priv->config), &priv->config);
261 if (rc < 0) {
262 debug("writing config to adapter failed\n");
263 return rc;
264 }
265
266 return 0;
267}
268
269/*
270 * mcs7830_write_mchash() - write the network adapter's multicast filter
Simon Glassce932c72015-11-29 13:18:10 -0700271 * @udev: network device to write to
272 * @priv: private data
Gerhard Sittigdf4fb1c2014-03-08 19:46:14 +0100273 * Return: zero upon success, negative upon error
274 *
275 * the data which gets written is taken from the shadow multicast hashes
276 * within the device driver's private data
277 */
Simon Glassce932c72015-11-29 13:18:10 -0700278static int mcs7830_write_mchash(struct usb_device *udev,
279 struct mcs7830_private *priv)
Gerhard Sittigdf4fb1c2014-03-08 19:46:14 +0100280{
Gerhard Sittigdf4fb1c2014-03-08 19:46:14 +0100281 int rc;
282
283 debug("%s()\n", __func__);
Gerhard Sittigdf4fb1c2014-03-08 19:46:14 +0100284
Simon Glassce932c72015-11-29 13:18:10 -0700285 rc = mcs7830_write_reg(udev, REG_MULTICAST_HASH,
Gerhard Sittigdf4fb1c2014-03-08 19:46:14 +0100286 sizeof(priv->mchash), &priv->mchash);
287 if (rc < 0) {
288 debug("writing multicast hash to adapter failed\n");
289 return rc;
290 }
291
292 return 0;
293}
294
295/*
296 * mcs7830_set_autoneg() - setup and trigger ethernet link autonegotiation
Simon Glassce932c72015-11-29 13:18:10 -0700297 * @udev: network device to run link negotiation on
Gerhard Sittigdf4fb1c2014-03-08 19:46:14 +0100298 * Return: zero upon success, negative upon error
299 *
300 * the routine advertises available media and starts autonegotiation
301 */
Simon Glassce932c72015-11-29 13:18:10 -0700302static int mcs7830_set_autoneg(struct usb_device *udev)
Gerhard Sittigdf4fb1c2014-03-08 19:46:14 +0100303{
304 int adv, flg;
305 int rc;
306
307 debug("%s()\n", __func__);
308
309 /*
310 * algorithm taken from the Linux driver, which took it from
311 * "the original mcs7830 version 1.4 driver":
312 *
313 * enable all media, reset BMCR, enable auto neg, restart
314 * auto neg while keeping the enable auto neg flag set
315 */
316
317 adv = ADVERTISE_PAUSE_CAP | ADVERTISE_ALL | ADVERTISE_CSMA;
Simon Glassce932c72015-11-29 13:18:10 -0700318 rc = mcs7830_write_phy(udev, MII_ADVERTISE, adv);
Gerhard Sittigdf4fb1c2014-03-08 19:46:14 +0100319
320 flg = 0;
321 if (!rc)
Simon Glassce932c72015-11-29 13:18:10 -0700322 rc = mcs7830_write_phy(udev, MII_BMCR, flg);
Gerhard Sittigdf4fb1c2014-03-08 19:46:14 +0100323
324 flg |= BMCR_ANENABLE;
325 if (!rc)
Simon Glassce932c72015-11-29 13:18:10 -0700326 rc = mcs7830_write_phy(udev, MII_BMCR, flg);
Gerhard Sittigdf4fb1c2014-03-08 19:46:14 +0100327
328 flg |= BMCR_ANRESTART;
329 if (!rc)
Simon Glassce932c72015-11-29 13:18:10 -0700330 rc = mcs7830_write_phy(udev, MII_BMCR, flg);
Gerhard Sittigdf4fb1c2014-03-08 19:46:14 +0100331
332 return rc;
333}
334
335/*
336 * mcs7830_get_rev() - identify a network adapter's chip revision
Simon Glassce932c72015-11-29 13:18:10 -0700337 * @udev: network device to identify
Gerhard Sittigdf4fb1c2014-03-08 19:46:14 +0100338 * Return: non-negative number, reflecting the revision number
339 *
340 * currently, only "rev C and higher" and "below rev C" are needed, so
341 * the return value is #1 for "below rev C", and #2 for "rev C and above"
342 */
Simon Glassce932c72015-11-29 13:18:10 -0700343static int mcs7830_get_rev(struct usb_device *udev)
Gerhard Sittigdf4fb1c2014-03-08 19:46:14 +0100344{
345 uint8_t buf[2];
346 int rc;
347 int rev;
348
349 /* register 22 is readable in rev C and higher */
Simon Glassce932c72015-11-29 13:18:10 -0700350 rc = mcs7830_read_reg(udev, REG_FRAME_DROP_COUNTER, sizeof(buf), buf);
Gerhard Sittigdf4fb1c2014-03-08 19:46:14 +0100351 if (rc < 0)
352 rev = 1;
353 else
354 rev = 2;
355 debug("%s() rc=%d, rev=%d\n", __func__, rc, rev);
356 return rev;
357}
358
359/*
360 * mcs7830_apply_fixup() - identify an adapter and potentially apply fixups
Simon Glassce932c72015-11-29 13:18:10 -0700361 * @udev: network device to identify and apply fixups to
Gerhard Sittigdf4fb1c2014-03-08 19:46:14 +0100362 * Return: zero upon success (no errors emitted from here)
363 *
364 * this routine identifies the network adapter's chip revision, and applies
365 * fixups for known issues
366 */
Simon Glassce932c72015-11-29 13:18:10 -0700367static int mcs7830_apply_fixup(struct usb_device *udev)
Gerhard Sittigdf4fb1c2014-03-08 19:46:14 +0100368{
369 int rev;
370 int i;
371 uint8_t thr;
372
Simon Glassce932c72015-11-29 13:18:10 -0700373 rev = mcs7830_get_rev(udev);
Gerhard Sittigdf4fb1c2014-03-08 19:46:14 +0100374 debug("%s() rev=%d\n", __func__, rev);
375
376 /*
377 * rev C requires setting the pause threshold (the Linux driver
378 * is inconsistent, the implementation does it for "rev C
379 * exactly", the introductory comment says "rev C and above")
380 */
381 if (rev == 2) {
Simon Glassce932c72015-11-29 13:18:10 -0700382 debug("%s: applying rev C fixup\n", __func__);
Gerhard Sittigdf4fb1c2014-03-08 19:46:14 +0100383 thr = PAUSE_THRESHOLD_DEFAULT;
384 for (i = 0; i < 2; i++) {
Simon Glassce932c72015-11-29 13:18:10 -0700385 (void)mcs7830_write_reg(udev, REG_PAUSE_THRESHOLD,
Gerhard Sittigdf4fb1c2014-03-08 19:46:14 +0100386 sizeof(thr), &thr);
387 mdelay(1);
388 }
389 }
390
391 return 0;
392}
393
394/*
395 * mcs7830_basic_reset() - bring the network adapter into a known first state
396 * @eth: network device to act upon
397 * Return: zero upon success, negative upon error
398 *
399 * this routine initializes the network adapter such that subsequent invocations
400 * of the interface callbacks can exchange ethernet frames; link negotiation is
401 * triggered from here already and continues in background
402 */
Simon Glassce932c72015-11-29 13:18:10 -0700403static int mcs7830_basic_reset(struct usb_device *udev,
404 struct mcs7830_private *priv)
Gerhard Sittigdf4fb1c2014-03-08 19:46:14 +0100405{
Gerhard Sittigdf4fb1c2014-03-08 19:46:14 +0100406 int rc;
407
408 debug("%s()\n", __func__);
Gerhard Sittigdf4fb1c2014-03-08 19:46:14 +0100409
410 /*
411 * comment from the respective Linux driver, which
412 * unconditionally sets the ALLMULTICAST flag as well:
413 * should not be needed, but does not work otherwise
414 */
415 priv->config = CONF_TXENABLE;
416 priv->config |= CONF_ALLMULTICAST;
417
Simon Glassce932c72015-11-29 13:18:10 -0700418 rc = mcs7830_set_autoneg(udev);
Gerhard Sittigdf4fb1c2014-03-08 19:46:14 +0100419 if (rc < 0) {
Masahiro Yamada9b643e32017-09-16 14:10:41 +0900420 pr_err("setting autoneg failed\n");
Gerhard Sittigdf4fb1c2014-03-08 19:46:14 +0100421 return rc;
422 }
423
Simon Glassce932c72015-11-29 13:18:10 -0700424 rc = mcs7830_write_mchash(udev, priv);
Gerhard Sittigdf4fb1c2014-03-08 19:46:14 +0100425 if (rc < 0) {
Masahiro Yamada9b643e32017-09-16 14:10:41 +0900426 pr_err("failed to set multicast hash\n");
Gerhard Sittigdf4fb1c2014-03-08 19:46:14 +0100427 return rc;
428 }
429
Simon Glassce932c72015-11-29 13:18:10 -0700430 rc = mcs7830_write_config(udev, priv);
Gerhard Sittigdf4fb1c2014-03-08 19:46:14 +0100431 if (rc < 0) {
Masahiro Yamada9b643e32017-09-16 14:10:41 +0900432 pr_err("failed to set configuration\n");
Gerhard Sittigdf4fb1c2014-03-08 19:46:14 +0100433 return rc;
434 }
435
Simon Glassce932c72015-11-29 13:18:10 -0700436 rc = mcs7830_apply_fixup(udev);
Gerhard Sittigdf4fb1c2014-03-08 19:46:14 +0100437 if (rc < 0) {
Masahiro Yamada9b643e32017-09-16 14:10:41 +0900438 pr_err("fixup application failed\n");
Gerhard Sittigdf4fb1c2014-03-08 19:46:14 +0100439 return rc;
440 }
441
442 return 0;
443}
444
445/*
446 * mcs7830_read_mac() - read an ethernet adapter's MAC address
Simon Glassce932c72015-11-29 13:18:10 -0700447 * @udev: network device to read from
448 * @enetaddr: place to put ethernet MAC address
Gerhard Sittigdf4fb1c2014-03-08 19:46:14 +0100449 * Return: zero upon success, negative upon error
450 *
451 * this routine fetches the MAC address stored within the ethernet adapter,
452 * and stores it in the ethernet interface's data structure
453 */
Simon Glassce932c72015-11-29 13:18:10 -0700454static int mcs7830_read_mac(struct usb_device *udev, unsigned char enetaddr[])
Gerhard Sittigdf4fb1c2014-03-08 19:46:14 +0100455{
Gerhard Sittigdf4fb1c2014-03-08 19:46:14 +0100456 int rc;
457 uint8_t buf[ETH_ALEN];
458
459 debug("%s()\n", __func__);
Gerhard Sittigdf4fb1c2014-03-08 19:46:14 +0100460
Simon Glassce932c72015-11-29 13:18:10 -0700461 rc = mcs7830_read_reg(udev, REG_ETHER_ADDR, ETH_ALEN, buf);
Gerhard Sittigdf4fb1c2014-03-08 19:46:14 +0100462 if (rc < 0) {
463 debug("reading MAC from adapter failed\n");
464 return rc;
465 }
466
Simon Glassce932c72015-11-29 13:18:10 -0700467 memcpy(enetaddr, buf, ETH_ALEN);
Gerhard Sittigdf4fb1c2014-03-08 19:46:14 +0100468 return 0;
469}
470
Simon Glassce932c72015-11-29 13:18:10 -0700471static int mcs7830_write_mac_common(struct usb_device *udev,
472 unsigned char enetaddr[])
Gerhard Sittigdf4fb1c2014-03-08 19:46:14 +0100473{
Gerhard Sittigdf4fb1c2014-03-08 19:46:14 +0100474 int rc;
475
476 debug("%s()\n", __func__);
Gerhard Sittigdf4fb1c2014-03-08 19:46:14 +0100477
Simon Glassce932c72015-11-29 13:18:10 -0700478 rc = mcs7830_write_reg(udev, REG_ETHER_ADDR, ETH_ALEN, enetaddr);
Gerhard Sittigdf4fb1c2014-03-08 19:46:14 +0100479 if (rc < 0) {
480 debug("writing MAC to adapter failed\n");
481 return rc;
482 }
483 return 0;
484}
485
Simon Glassce932c72015-11-29 13:18:10 -0700486static int mcs7830_init_common(struct usb_device *udev)
Gerhard Sittigdf4fb1c2014-03-08 19:46:14 +0100487{
Gerhard Sittigdf4fb1c2014-03-08 19:46:14 +0100488 int timeout;
489 int have_link;
490
491 debug("%s()\n", __func__);
Gerhard Sittigdf4fb1c2014-03-08 19:46:14 +0100492
493 timeout = 0;
494 do {
Simon Glassce932c72015-11-29 13:18:10 -0700495 have_link = mcs7830_read_phy(udev, MII_BMSR) & BMSR_LSTATUS;
Gerhard Sittigdf4fb1c2014-03-08 19:46:14 +0100496 if (have_link)
497 break;
498 udelay(LINKSTATUS_TIMEOUT_RES * 1000);
499 timeout += LINKSTATUS_TIMEOUT_RES;
500 } while (timeout < LINKSTATUS_TIMEOUT);
501 if (!have_link) {
502 debug("ethernet link is down\n");
503 return -ETIMEDOUT;
504 }
505 return 0;
506}
507
Simon Glassce932c72015-11-29 13:18:10 -0700508static int mcs7830_send_common(struct ueth_data *ueth, void *packet,
509 int length)
Gerhard Sittigdf4fb1c2014-03-08 19:46:14 +0100510{
Simon Glassce932c72015-11-29 13:18:10 -0700511 struct usb_device *udev = ueth->pusb_dev;
Gerhard Sittigdf4fb1c2014-03-08 19:46:14 +0100512 int rc;
513 int gotlen;
514 /* there is a status byte after the ethernet frame */
515 ALLOC_CACHE_ALIGN_BUFFER(uint8_t, buf, PKTSIZE + sizeof(uint8_t));
516
Gerhard Sittigdf4fb1c2014-03-08 19:46:14 +0100517 memcpy(buf, packet, length);
Simon Glassce932c72015-11-29 13:18:10 -0700518 rc = usb_bulk_msg(udev,
519 usb_sndbulkpipe(udev, ueth->ep_out),
Gerhard Sittigdf4fb1c2014-03-08 19:46:14 +0100520 &buf[0], length, &gotlen,
521 USBCALL_TIMEOUT);
522 debug("%s() TX want len %d, got len %d, rc %d\n",
523 __func__, length, gotlen, rc);
524 return rc;
525}
526
Simon Glassce932c72015-11-29 13:18:10 -0700527static int mcs7830_recv_common(struct ueth_data *ueth, uint8_t *buf)
Gerhard Sittigdf4fb1c2014-03-08 19:46:14 +0100528{
Gerhard Sittigdf4fb1c2014-03-08 19:46:14 +0100529 int rc, wantlen, gotlen;
530 uint8_t sts;
531
532 debug("%s()\n", __func__);
Gerhard Sittigdf4fb1c2014-03-08 19:46:14 +0100533
534 /* fetch input data from the adapter */
535 wantlen = MCS7830_RX_URB_SIZE;
Simon Glassce932c72015-11-29 13:18:10 -0700536 rc = usb_bulk_msg(ueth->pusb_dev,
537 usb_rcvbulkpipe(ueth->pusb_dev, ueth->ep_in),
Gerhard Sittigdf4fb1c2014-03-08 19:46:14 +0100538 &buf[0], wantlen, &gotlen,
539 USBCALL_TIMEOUT);
540 debug("%s() RX want len %d, got len %d, rc %d\n",
541 __func__, wantlen, gotlen, rc);
542 if (rc != 0) {
Masahiro Yamada9b643e32017-09-16 14:10:41 +0900543 pr_err("RX: failed to receive\n");
Gerhard Sittigdf4fb1c2014-03-08 19:46:14 +0100544 return rc;
545 }
546 if (gotlen > wantlen) {
Masahiro Yamada9b643e32017-09-16 14:10:41 +0900547 pr_err("RX: got too many bytes (%d)\n", gotlen);
Gerhard Sittigdf4fb1c2014-03-08 19:46:14 +0100548 return -EIO;
549 }
550
551 /*
552 * the bulk message that we received from USB contains exactly
553 * one ethernet frame and a trailing status byte
554 */
555 if (gotlen < sizeof(sts))
556 return -EIO;
557 gotlen -= sizeof(sts);
558 sts = buf[gotlen];
559
560 if (sts == STAT_RX_FRAME_CORRECT) {
561 debug("%s() got a frame, len=%d\n", __func__, gotlen);
Simon Glassce932c72015-11-29 13:18:10 -0700562 return gotlen;
Gerhard Sittigdf4fb1c2014-03-08 19:46:14 +0100563 }
564
565 debug("RX: frame error (sts 0x%02X, %s %s %s %s %s)\n",
566 sts,
567 (sts & STAT_RX_LARGE_FRAME) ? "large" : "-",
568 (sts & STAT_RX_LENGTH_ERROR) ? "length" : "-",
569 (sts & STAT_RX_SHORT_FRAME) ? "short" : "-",
570 (sts & STAT_RX_CRC_ERROR) ? "crc" : "-",
571 (sts & STAT_RX_ALIGNMENT_ERROR) ? "align" : "-");
572 return -EIO;
573}
574
Simon Glassd4f847e2015-11-29 13:18:11 -0700575#ifndef CONFIG_DM_ETH
Gerhard Sittigdf4fb1c2014-03-08 19:46:14 +0100576/*
Simon Glassce932c72015-11-29 13:18:10 -0700577 * mcs7830_init() - network interface's init callback
578 * @udev: network device to initialize
579 * @bd: board information
580 * Return: zero upon success, negative upon error
581 *
582 * after initial setup during probe() and get_info(), this init() callback
583 * ensures that the link is up and subsequent send() and recv() calls can
584 * exchange ethernet frames
585 */
586static int mcs7830_init(struct eth_device *eth, bd_t *bd)
587{
588 struct ueth_data *dev = eth->priv;
589
590 return mcs7830_init_common(dev->pusb_dev);
591}
592
593/*
594 * mcs7830_send() - network interface's send callback
595 * @eth: network device to send the frame from
596 * @packet: ethernet frame content
597 * @length: ethernet frame length
598 * Return: zero upon success, negative upon error
599 *
600 * this routine send an ethernet frame out of the network interface
601 */
602static int mcs7830_send(struct eth_device *eth, void *packet, int length)
603{
604 struct ueth_data *dev = eth->priv;
605
606 return mcs7830_send_common(dev, packet, length);
607}
608
609/*
610 * mcs7830_recv() - network interface's recv callback
611 * @eth: network device to receive frames from
612 * Return: zero upon success, negative upon error
613 *
614 * this routine checks for available ethernet frames that the network
615 * interface might have received, and notifies the network stack
616 */
617static int mcs7830_recv(struct eth_device *eth)
618{
619 ALLOC_CACHE_ALIGN_BUFFER(uint8_t, buf, MCS7830_RX_URB_SIZE);
620 struct ueth_data *ueth = eth->priv;
621 int len;
622
623 len = mcs7830_recv_common(ueth, buf);
Uri Mashiach3ce30262017-05-21 17:00:52 +0300624 if (len >= 0) {
Simon Glassce932c72015-11-29 13:18:10 -0700625 net_process_received_packet(buf, len);
Uri Mashiach3ce30262017-05-21 17:00:52 +0300626 return 0;
627 }
Simon Glassce932c72015-11-29 13:18:10 -0700628
Uri Mashiach3ce30262017-05-21 17:00:52 +0300629 return len;
Simon Glassce932c72015-11-29 13:18:10 -0700630}
631
632/*
Gerhard Sittigdf4fb1c2014-03-08 19:46:14 +0100633 * mcs7830_halt() - network interface's halt callback
634 * @eth: network device to cease operation of
635 * Return: none
636 *
637 * this routine is supposed to undo the effect of previous initialization and
638 * ethernet frames exchange; in this implementation it's a NOP
639 */
640static void mcs7830_halt(struct eth_device *eth)
641{
642 debug("%s()\n", __func__);
643}
644
645/*
Simon Glassce932c72015-11-29 13:18:10 -0700646 * mcs7830_write_mac() - write an ethernet adapter's MAC address
647 * @eth: network device to write to
648 * Return: zero upon success, negative upon error
649 *
650 * this routine takes the MAC address from the ethernet interface's data
651 * structure, and writes it into the ethernet adapter such that subsequent
652 * exchange of ethernet frames uses this address
653 */
654static int mcs7830_write_mac(struct eth_device *eth)
655{
656 struct ueth_data *ueth = eth->priv;
657
658 return mcs7830_write_mac_common(ueth->pusb_dev, eth->enetaddr);
659}
660
661/*
Gerhard Sittigdf4fb1c2014-03-08 19:46:14 +0100662 * mcs7830_iface_idx - index of detected network interfaces
663 *
664 * this counter keeps track of identified supported interfaces,
665 * to assign unique names as more interfaces are found
666 */
667static int mcs7830_iface_idx;
668
669/*
670 * mcs7830_eth_before_probe() - network driver's before_probe callback
671 * Return: none
672 *
673 * this routine initializes driver's internal data in preparation of
674 * subsequent probe callbacks
675 */
676void mcs7830_eth_before_probe(void)
677{
678 mcs7830_iface_idx = 0;
679}
680
681/*
682 * struct mcs7830_dongle - description of a supported Moschip ethernet dongle
683 * @vendor: 16bit USB vendor identification
684 * @product: 16bit USB product identification
685 *
686 * this structure describes a supported USB ethernet dongle by means of the
687 * vendor and product codes found during USB enumeration; no flags are held
688 * here since all supported dongles have identical behaviour, and required
689 * fixups get determined at runtime, such that no manual configuration is
690 * needed
691 */
692struct mcs7830_dongle {
693 uint16_t vendor;
694 uint16_t product;
695};
696
697/*
698 * mcs7830_dongles - the list of supported Moschip based USB ethernet dongles
699 */
Jeroen Hofstee51afc2c2014-06-17 21:14:17 +0200700static const struct mcs7830_dongle mcs7830_dongles[] = {
Gerhard Sittigdf4fb1c2014-03-08 19:46:14 +0100701 { 0x9710, 0x7832, }, /* Moschip 7832 */
702 { 0x9710, 0x7830, }, /* Moschip 7830 */
703 { 0x9710, 0x7730, }, /* Moschip 7730 */
704 { 0x0df6, 0x0021, }, /* Sitecom LN 30 */
705};
706
707/*
708 * mcs7830_eth_probe() - network driver's probe callback
709 * @dev: detected USB device to check
710 * @ifnum: detected USB interface to check
711 * @ss: USB ethernet data structure to fill in upon match
712 * Return: #1 upon match, #0 upon mismatch or error
713 *
714 * this routine checks whether the found USB device is supported by
715 * this ethernet driver, and upon match fills in the USB ethernet
716 * data structure which later is passed to the get_info callback
717 */
718int mcs7830_eth_probe(struct usb_device *dev, unsigned int ifnum,
719 struct ueth_data *ss)
720{
721 struct usb_interface *iface;
722 struct usb_interface_descriptor *iface_desc;
723 int i;
724 struct mcs7830_private *priv;
725 int ep_in_found, ep_out_found, ep_intr_found;
726
727 debug("%s()\n", __func__);
728
729 /* iterate the list of supported dongles */
730 iface = &dev->config.if_desc[ifnum];
731 iface_desc = &iface->desc;
732 for (i = 0; i < ARRAY_SIZE(mcs7830_dongles); i++) {
733 if (dev->descriptor.idVendor == mcs7830_dongles[i].vendor &&
734 dev->descriptor.idProduct == mcs7830_dongles[i].product)
735 break;
736 }
737 if (i == ARRAY_SIZE(mcs7830_dongles))
738 return 0;
739 debug("detected USB ethernet device: %04X:%04X\n",
740 dev->descriptor.idVendor, dev->descriptor.idProduct);
741
742 /* fill in driver private data */
743 priv = calloc(1, sizeof(*priv));
744 if (!priv)
745 return 0;
746
747 /* fill in the ueth_data structure, attach private data */
748 memset(ss, 0, sizeof(*ss));
749 ss->ifnum = ifnum;
750 ss->pusb_dev = dev;
751 ss->subclass = iface_desc->bInterfaceSubClass;
752 ss->protocol = iface_desc->bInterfaceProtocol;
753 ss->dev_priv = priv;
754
755 /*
756 * a minimum of three endpoints is expected: in (bulk),
757 * out (bulk), and interrupt; ignore all others
758 */
759 ep_in_found = ep_out_found = ep_intr_found = 0;
760 for (i = 0; i < iface_desc->bNumEndpoints; i++) {
761 uint8_t eptype, epaddr;
762 bool is_input;
763
764 eptype = iface->ep_desc[i].bmAttributes;
765 eptype &= USB_ENDPOINT_XFERTYPE_MASK;
766
767 epaddr = iface->ep_desc[i].bEndpointAddress;
768 is_input = epaddr & USB_DIR_IN;
769 epaddr &= USB_ENDPOINT_NUMBER_MASK;
770
771 if (eptype == USB_ENDPOINT_XFER_BULK) {
772 if (is_input && !ep_in_found) {
773 ss->ep_in = epaddr;
774 ep_in_found++;
775 }
776 if (!is_input && !ep_out_found) {
777 ss->ep_out = epaddr;
778 ep_out_found++;
779 }
780 }
781
782 if (eptype == USB_ENDPOINT_XFER_INT) {
783 if (is_input && !ep_intr_found) {
784 ss->ep_int = epaddr;
785 ss->irqinterval = iface->ep_desc[i].bInterval;
786 ep_intr_found++;
787 }
788 }
789 }
790 debug("endpoints: in %d, out %d, intr %d\n",
791 ss->ep_in, ss->ep_out, ss->ep_int);
792
793 /* apply basic sanity checks */
794 if (usb_set_interface(dev, iface_desc->bInterfaceNumber, 0) ||
795 !ss->ep_in || !ss->ep_out || !ss->ep_int) {
796 debug("device probe incomplete\n");
797 return 0;
798 }
799
800 dev->privptr = ss;
801 return 1;
802}
803
804/*
805 * mcs7830_eth_get_info() - network driver's get_info callback
806 * @dev: detected USB device
807 * @ss: USB ethernet data structure filled in at probe()
808 * @eth: ethernet interface data structure to fill in
809 * Return: #1 upon success, #0 upon error
810 *
811 * this routine registers the mandatory init(), send(), recv(), and
812 * halt() callbacks with the ethernet interface, can register the
813 * optional write_hwaddr() callback with the ethernet interface,
814 * and initiates configuration of the interface such that subsequent
815 * calls to those callbacks results in network communication
816 */
817int mcs7830_eth_get_info(struct usb_device *dev, struct ueth_data *ss,
818 struct eth_device *eth)
819{
820 debug("%s()\n", __func__);
821 if (!eth) {
822 debug("%s: missing parameter.\n", __func__);
823 return 0;
824 }
825
826 snprintf(eth->name, sizeof(eth->name), "%s%d",
827 MCS7830_BASE_NAME, mcs7830_iface_idx++);
828 eth->init = mcs7830_init;
829 eth->send = mcs7830_send;
830 eth->recv = mcs7830_recv;
831 eth->halt = mcs7830_halt;
832 eth->write_hwaddr = mcs7830_write_mac;
833 eth->priv = ss;
834
Simon Glassce932c72015-11-29 13:18:10 -0700835 if (mcs7830_basic_reset(ss->pusb_dev, ss->dev_priv))
Gerhard Sittigdf4fb1c2014-03-08 19:46:14 +0100836 return 0;
837
Simon Glassce932c72015-11-29 13:18:10 -0700838 if (mcs7830_read_mac(ss->pusb_dev, eth->enetaddr))
Gerhard Sittigdf4fb1c2014-03-08 19:46:14 +0100839 return 0;
840 debug("MAC %pM\n", eth->enetaddr);
841
842 return 1;
843}
Simon Glassd4f847e2015-11-29 13:18:11 -0700844#endif
845
846
847#ifdef CONFIG_DM_ETH
848static int mcs7830_eth_start(struct udevice *dev)
849{
850 struct usb_device *udev = dev_get_parent_priv(dev);
851
852 return mcs7830_init_common(udev);
853}
854
855void mcs7830_eth_stop(struct udevice *dev)
856{
857 debug("** %s()\n", __func__);
858}
859
860int mcs7830_eth_send(struct udevice *dev, void *packet, int length)
861{
862 struct mcs7830_private *priv = dev_get_priv(dev);
863 struct ueth_data *ueth = &priv->ueth;
864
865 return mcs7830_send_common(ueth, packet, length);
866}
867
868int mcs7830_eth_recv(struct udevice *dev, int flags, uchar **packetp)
869{
870 struct mcs7830_private *priv = dev_get_priv(dev);
871 struct ueth_data *ueth = &priv->ueth;
872 int len;
873
874 len = mcs7830_recv_common(ueth, priv->rx_buf);
875 *packetp = priv->rx_buf;
876
877 return len;
878}
879
880static int mcs7830_free_pkt(struct udevice *dev, uchar *packet, int packet_len)
881{
882 struct mcs7830_private *priv = dev_get_priv(dev);
883
884 packet_len = ALIGN(packet_len, 4);
885 usb_ether_advance_rxbuf(&priv->ueth, sizeof(u32) + packet_len);
886
887 return 0;
888}
889
890int mcs7830_write_hwaddr(struct udevice *dev)
891{
892 struct usb_device *udev = dev_get_parent_priv(dev);
893 struct eth_pdata *pdata = dev_get_platdata(dev);
894
895 return mcs7830_write_mac_common(udev, pdata->enetaddr);
896}
897
898static int mcs7830_eth_probe(struct udevice *dev)
899{
900 struct usb_device *udev = dev_get_parent_priv(dev);
901 struct mcs7830_private *priv = dev_get_priv(dev);
902 struct eth_pdata *pdata = dev_get_platdata(dev);
903 struct ueth_data *ueth = &priv->ueth;
904
905 if (mcs7830_basic_reset(udev, priv))
906 return 0;
907
908 if (mcs7830_read_mac(udev, pdata->enetaddr))
909 return 0;
910
911 return usb_ether_register(dev, ueth, MCS7830_RX_URB_SIZE);
912}
913
914static const struct eth_ops mcs7830_eth_ops = {
915 .start = mcs7830_eth_start,
916 .send = mcs7830_eth_send,
917 .recv = mcs7830_eth_recv,
918 .free_pkt = mcs7830_free_pkt,
919 .stop = mcs7830_eth_stop,
920 .write_hwaddr = mcs7830_write_hwaddr,
921};
922
923U_BOOT_DRIVER(mcs7830_eth) = {
924 .name = "mcs7830_eth",
925 .id = UCLASS_ETH,
926 .probe = mcs7830_eth_probe,
927 .ops = &mcs7830_eth_ops,
928 .priv_auto_alloc_size = sizeof(struct mcs7830_private),
929 .platdata_auto_alloc_size = sizeof(struct eth_pdata),
930 .flags = DM_FLAG_ALLOC_PRIV_DMA,
931};
932
933static const struct usb_device_id mcs7830_eth_id_table[] = {
934 { USB_DEVICE(0x9710, 0x7832) }, /* Moschip 7832 */
935 { USB_DEVICE(0x9710, 0x7830), }, /* Moschip 7830 */
936 { USB_DEVICE(0x9710, 0x7730), }, /* Moschip 7730 */
937 { USB_DEVICE(0x0df6, 0x0021), }, /* Sitecom LN 30 */
938 { } /* Terminating entry */
939};
940
941U_BOOT_USB_DEVICE(mcs7830_eth, mcs7830_eth_id_table);
942#endif