blob: 283c52c16ac66306b2156010666782d9d5d4b015 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Simon Glass246c1192011-06-13 16:13:09 -07002/*
Simon Glass68c41972015-07-07 20:53:42 -06003 * Copyright (c) 2015 Google, Inc
Simon Glass246c1192011-06-13 16:13:09 -07004 * Copyright (c) 2011 The Chromium OS Authors.
5 * Copyright (C) 2009 NVIDIA, Corporation
Simon Glass00be89c2014-09-08 13:44:14 -06006 * Copyright (C) 2007-2008 SMSC (Steve Glendinning)
Simon Glass246c1192011-06-13 16:13:09 -07007 */
8
9#include <common.h>
Simon Glass68c41972015-07-07 20:53:42 -060010#include <dm.h>
Simon Glassce48e502015-07-07 20:53:38 -060011#include <errno.h>
Simon Glass0f2af882020-05-10 11:40:05 -060012#include <log.h>
Simon Glassce48e502015-07-07 20:53:38 -060013#include <malloc.h>
Simon Glass2dd337a2015-09-02 17:24:58 -060014#include <memalign.h>
Simon Glass274e0b02020-05-10 11:39:56 -060015#include <net.h>
Simon Glass246c1192011-06-13 16:13:09 -070016#include <usb.h>
Simon Glassce48e502015-07-07 20:53:38 -060017#include <asm/unaligned.h>
Simon Glassdbd79542020-05-10 11:40:11 -060018#include <linux/delay.h>
Simon Glass246c1192011-06-13 16:13:09 -070019#include <linux/mii.h>
20#include "usb_ether.h"
21
22/* SMSC LAN95xx based USB 2.0 Ethernet Devices */
23
Suriyan Ramasamie0f2f8c2013-10-07 20:30:58 -070024/* LED defines */
25#define LED_GPIO_CFG (0x24)
26#define LED_GPIO_CFG_SPD_LED (0x01000000)
27#define LED_GPIO_CFG_LNK_LED (0x00100000)
28#define LED_GPIO_CFG_FDX_LED (0x00010000)
29
Simon Glass246c1192011-06-13 16:13:09 -070030/* Tx command words */
31#define TX_CMD_A_FIRST_SEG_ 0x00002000
32#define TX_CMD_A_LAST_SEG_ 0x00001000
33
34/* Rx status word */
35#define RX_STS_FL_ 0x3FFF0000 /* Frame Length */
36#define RX_STS_ES_ 0x00008000 /* Error Summary */
37
38/* SCSRs */
39#define ID_REV 0x00
40
41#define INT_STS 0x08
42
43#define TX_CFG 0x10
44#define TX_CFG_ON_ 0x00000004
45
46#define HW_CFG 0x14
47#define HW_CFG_BIR_ 0x00001000
48#define HW_CFG_RXDOFF_ 0x00000600
49#define HW_CFG_MEF_ 0x00000020
50#define HW_CFG_BCE_ 0x00000002
51#define HW_CFG_LRST_ 0x00000008
52
53#define PM_CTRL 0x20
54#define PM_CTL_PHY_RST_ 0x00000010
55
56#define AFC_CFG 0x2C
57
58/*
59 * Hi watermark = 15.5Kb (~10 mtu pkts)
60 * low watermark = 3k (~2 mtu pkts)
61 * backpressure duration = ~ 350us
62 * Apply FC on any frame.
63 */
64#define AFC_CFG_DEFAULT 0x00F830A1
65
66#define E2P_CMD 0x30
67#define E2P_CMD_BUSY_ 0x80000000
68#define E2P_CMD_READ_ 0x00000000
69#define E2P_CMD_TIMEOUT_ 0x00000400
70#define E2P_CMD_LOADED_ 0x00000200
71#define E2P_CMD_ADDR_ 0x000001FF
72
73#define E2P_DATA 0x34
74
75#define BURST_CAP 0x38
76
77#define INT_EP_CTL 0x68
78#define INT_EP_CTL_PHY_INT_ 0x00008000
79
80#define BULK_IN_DLY 0x6C
81
82/* MAC CSRs */
83#define MAC_CR 0x100
84#define MAC_CR_MCPAS_ 0x00080000
85#define MAC_CR_PRMS_ 0x00040000
86#define MAC_CR_HPFILT_ 0x00002000
87#define MAC_CR_TXEN_ 0x00000008
88#define MAC_CR_RXEN_ 0x00000004
89
90#define ADDRH 0x104
91
92#define ADDRL 0x108
93
94#define MII_ADDR 0x114
95#define MII_WRITE_ 0x02
96#define MII_BUSY_ 0x01
97#define MII_READ_ 0x00 /* ~of MII Write bit */
98
99#define MII_DATA 0x118
100
101#define FLOW 0x11C
102
103#define VLAN1 0x120
104
105#define COE_CR 0x130
106#define Tx_COE_EN_ 0x00010000
107#define Rx_COE_EN_ 0x00000001
108
109/* Vendor-specific PHY Definitions */
110#define PHY_INT_SRC 29
111
112#define PHY_INT_MASK 30
113#define PHY_INT_MASK_ANEG_COMP_ ((u16)0x0040)
114#define PHY_INT_MASK_LINK_DOWN_ ((u16)0x0010)
115#define PHY_INT_MASK_DEFAULT_ (PHY_INT_MASK_ANEG_COMP_ | \
116 PHY_INT_MASK_LINK_DOWN_)
117
118/* USB Vendor Requests */
119#define USB_VENDOR_REQUEST_WRITE_REGISTER 0xA0
120#define USB_VENDOR_REQUEST_READ_REGISTER 0xA1
121
122/* Some extra defines */
123#define HS_USB_PKT_SIZE 512
124#define FS_USB_PKT_SIZE 64
Stefan Brünsd3095fe2015-08-30 17:59:45 +0200125/* 5/33 is lower limit for BURST_CAP to work */
126#define DEFAULT_HS_BURST_CAP_SIZE (5 * HS_USB_PKT_SIZE)
127#define DEFAULT_FS_BURST_CAP_SIZE (33 * FS_USB_PKT_SIZE)
Simon Glass246c1192011-06-13 16:13:09 -0700128#define DEFAULT_BULK_IN_DELAY 0x00002000
129#define MAX_SINGLE_PACKET_SIZE 2048
130#define EEPROM_MAC_OFFSET 0x01
131#define SMSC95XX_INTERNAL_PHY_ID 1
132#define ETH_P_8021Q 0x8100 /* 802.1Q VLAN Extended Header */
133
134/* local defines */
135#define SMSC95XX_BASE_NAME "sms"
136#define USB_CTRL_SET_TIMEOUT 5000
137#define USB_CTRL_GET_TIMEOUT 5000
138#define USB_BULK_SEND_TIMEOUT 5000
139#define USB_BULK_RECV_TIMEOUT 5000
140
Stefan Brünsd3095fe2015-08-30 17:59:45 +0200141#define RX_URB_SIZE DEFAULT_HS_BURST_CAP_SIZE
Simon Glass246c1192011-06-13 16:13:09 -0700142#define PHY_CONNECT_TIMEOUT 5000
143
144#define TURBO_MODE
145
Simon Glass68c41972015-07-07 20:53:42 -0600146#ifndef CONFIG_DM_ETH
Simon Glass246c1192011-06-13 16:13:09 -0700147/* local vars */
148static int curr_eth_dev; /* index for name of next device detected */
Simon Glass68c41972015-07-07 20:53:42 -0600149#endif
Simon Glass246c1192011-06-13 16:13:09 -0700150
Lucas Stach36267c42012-08-22 11:04:57 +0000151/* driver private */
152struct smsc95xx_private {
Simon Glass68c41972015-07-07 20:53:42 -0600153#ifdef CONFIG_DM_ETH
154 struct ueth_data ueth;
155#endif
Lucas Stach36267c42012-08-22 11:04:57 +0000156 size_t rx_urb_size; /* maximum USB URB size */
157 u32 mac_cr; /* MAC control register value */
158 int have_hwaddr; /* 1 if we have a hardware MAC address */
159};
Simon Glass246c1192011-06-13 16:13:09 -0700160
161/*
162 * Smsc95xx infrastructure commands
163 */
Simon Glass3faecae2015-07-07 20:53:41 -0600164static int smsc95xx_write_reg(struct usb_device *udev, u32 index, u32 data)
Simon Glass246c1192011-06-13 16:13:09 -0700165{
166 int len;
Ilya Yanok43b56c22012-07-15 04:43:53 +0000167 ALLOC_CACHE_ALIGN_BUFFER(u32, tmpbuf, 1);
Simon Glass246c1192011-06-13 16:13:09 -0700168
169 cpu_to_le32s(&data);
Ilya Yanok43b56c22012-07-15 04:43:53 +0000170 tmpbuf[0] = data;
Simon Glass246c1192011-06-13 16:13:09 -0700171
Simon Glass3faecae2015-07-07 20:53:41 -0600172 len = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
173 USB_VENDOR_REQUEST_WRITE_REGISTER,
174 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
175 0, index, tmpbuf, sizeof(data),
176 USB_CTRL_SET_TIMEOUT);
Simon Glass246c1192011-06-13 16:13:09 -0700177 if (len != sizeof(data)) {
178 debug("smsc95xx_write_reg failed: index=%d, data=%d, len=%d",
179 index, data, len);
Simon Glassebe0e5a2015-07-07 20:53:40 -0600180 return -EIO;
Simon Glass246c1192011-06-13 16:13:09 -0700181 }
182 return 0;
183}
184
Simon Glass3faecae2015-07-07 20:53:41 -0600185static int smsc95xx_read_reg(struct usb_device *udev, u32 index, u32 *data)
Simon Glass246c1192011-06-13 16:13:09 -0700186{
187 int len;
Ilya Yanok43b56c22012-07-15 04:43:53 +0000188 ALLOC_CACHE_ALIGN_BUFFER(u32, tmpbuf, 1);
Simon Glass246c1192011-06-13 16:13:09 -0700189
Simon Glass3faecae2015-07-07 20:53:41 -0600190 len = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0),
191 USB_VENDOR_REQUEST_READ_REGISTER,
192 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
Stephen Warrenb0e5b492016-03-22 22:28:16 -0600193 0, index, tmpbuf, sizeof(*data),
Simon Glass3faecae2015-07-07 20:53:41 -0600194 USB_CTRL_GET_TIMEOUT);
Ilya Yanok43b56c22012-07-15 04:43:53 +0000195 *data = tmpbuf[0];
Stephen Warrenb0e5b492016-03-22 22:28:16 -0600196 if (len != sizeof(*data)) {
Simon Glass246c1192011-06-13 16:13:09 -0700197 debug("smsc95xx_read_reg failed: index=%d, len=%d",
198 index, len);
Simon Glassebe0e5a2015-07-07 20:53:40 -0600199 return -EIO;
Simon Glass246c1192011-06-13 16:13:09 -0700200 }
201
202 le32_to_cpus(data);
203 return 0;
204}
205
206/* Loop until the read is completed with timeout */
Simon Glass3faecae2015-07-07 20:53:41 -0600207static int smsc95xx_phy_wait_not_busy(struct usb_device *udev)
Simon Glass246c1192011-06-13 16:13:09 -0700208{
209 unsigned long start_time = get_timer(0);
210 u32 val;
211
212 do {
Simon Glass3faecae2015-07-07 20:53:41 -0600213 smsc95xx_read_reg(udev, MII_ADDR, &val);
Simon Glass246c1192011-06-13 16:13:09 -0700214 if (!(val & MII_BUSY_))
215 return 0;
Simon Glass3faecae2015-07-07 20:53:41 -0600216 } while (get_timer(start_time) < 1000);
Simon Glass246c1192011-06-13 16:13:09 -0700217
Simon Glassebe0e5a2015-07-07 20:53:40 -0600218 return -ETIMEDOUT;
Simon Glass246c1192011-06-13 16:13:09 -0700219}
220
Simon Glass3faecae2015-07-07 20:53:41 -0600221static int smsc95xx_mdio_read(struct usb_device *udev, int phy_id, int idx)
Simon Glass246c1192011-06-13 16:13:09 -0700222{
223 u32 val, addr;
224
225 /* confirm MII not busy */
Simon Glass3faecae2015-07-07 20:53:41 -0600226 if (smsc95xx_phy_wait_not_busy(udev)) {
Simon Glass246c1192011-06-13 16:13:09 -0700227 debug("MII is busy in smsc95xx_mdio_read\n");
Simon Glassebe0e5a2015-07-07 20:53:40 -0600228 return -ETIMEDOUT;
Simon Glass246c1192011-06-13 16:13:09 -0700229 }
230
231 /* set the address, index & direction (read from PHY) */
232 addr = (phy_id << 11) | (idx << 6) | MII_READ_;
Simon Glass3faecae2015-07-07 20:53:41 -0600233 smsc95xx_write_reg(udev, MII_ADDR, addr);
Simon Glass246c1192011-06-13 16:13:09 -0700234
Simon Glass3faecae2015-07-07 20:53:41 -0600235 if (smsc95xx_phy_wait_not_busy(udev)) {
Simon Glass246c1192011-06-13 16:13:09 -0700236 debug("Timed out reading MII reg %02X\n", idx);
Simon Glassebe0e5a2015-07-07 20:53:40 -0600237 return -ETIMEDOUT;
Simon Glass246c1192011-06-13 16:13:09 -0700238 }
239
Simon Glass3faecae2015-07-07 20:53:41 -0600240 smsc95xx_read_reg(udev, MII_DATA, &val);
Simon Glass246c1192011-06-13 16:13:09 -0700241
242 return (u16)(val & 0xFFFF);
243}
244
Simon Glass3faecae2015-07-07 20:53:41 -0600245static void smsc95xx_mdio_write(struct usb_device *udev, int phy_id, int idx,
Simon Glass246c1192011-06-13 16:13:09 -0700246 int regval)
247{
248 u32 val, addr;
249
250 /* confirm MII not busy */
Simon Glass3faecae2015-07-07 20:53:41 -0600251 if (smsc95xx_phy_wait_not_busy(udev)) {
Simon Glass246c1192011-06-13 16:13:09 -0700252 debug("MII is busy in smsc95xx_mdio_write\n");
253 return;
254 }
255
256 val = regval;
Simon Glass3faecae2015-07-07 20:53:41 -0600257 smsc95xx_write_reg(udev, MII_DATA, val);
Simon Glass246c1192011-06-13 16:13:09 -0700258
259 /* set the address, index & direction (write to PHY) */
260 addr = (phy_id << 11) | (idx << 6) | MII_WRITE_;
Simon Glass3faecae2015-07-07 20:53:41 -0600261 smsc95xx_write_reg(udev, MII_ADDR, addr);
Simon Glass246c1192011-06-13 16:13:09 -0700262
Simon Glass3faecae2015-07-07 20:53:41 -0600263 if (smsc95xx_phy_wait_not_busy(udev))
Simon Glass246c1192011-06-13 16:13:09 -0700264 debug("Timed out writing MII reg %02X\n", idx);
265}
266
Simon Glass3faecae2015-07-07 20:53:41 -0600267static int smsc95xx_eeprom_confirm_not_busy(struct usb_device *udev)
Simon Glass246c1192011-06-13 16:13:09 -0700268{
269 unsigned long start_time = get_timer(0);
270 u32 val;
271
272 do {
Simon Glass3faecae2015-07-07 20:53:41 -0600273 smsc95xx_read_reg(udev, E2P_CMD, &val);
Simon Glass246c1192011-06-13 16:13:09 -0700274 if (!(val & E2P_CMD_BUSY_))
275 return 0;
276 udelay(40);
277 } while (get_timer(start_time) < 1 * 1000 * 1000);
278
279 debug("EEPROM is busy\n");
Simon Glassebe0e5a2015-07-07 20:53:40 -0600280 return -ETIMEDOUT;
Simon Glass246c1192011-06-13 16:13:09 -0700281}
282
Simon Glass3faecae2015-07-07 20:53:41 -0600283static int smsc95xx_wait_eeprom(struct usb_device *udev)
Simon Glass246c1192011-06-13 16:13:09 -0700284{
285 unsigned long start_time = get_timer(0);
286 u32 val;
287
288 do {
Simon Glass3faecae2015-07-07 20:53:41 -0600289 smsc95xx_read_reg(udev, E2P_CMD, &val);
Simon Glass246c1192011-06-13 16:13:09 -0700290 if (!(val & E2P_CMD_BUSY_) || (val & E2P_CMD_TIMEOUT_))
291 break;
292 udelay(40);
293 } while (get_timer(start_time) < 1 * 1000 * 1000);
294
295 if (val & (E2P_CMD_TIMEOUT_ | E2P_CMD_BUSY_)) {
296 debug("EEPROM read operation timeout\n");
Simon Glassebe0e5a2015-07-07 20:53:40 -0600297 return -ETIMEDOUT;
Simon Glass246c1192011-06-13 16:13:09 -0700298 }
299 return 0;
300}
301
Simon Glass3faecae2015-07-07 20:53:41 -0600302static int smsc95xx_read_eeprom(struct usb_device *udev, u32 offset, u32 length,
Simon Glass246c1192011-06-13 16:13:09 -0700303 u8 *data)
304{
305 u32 val;
306 int i, ret;
307
Simon Glass3faecae2015-07-07 20:53:41 -0600308 ret = smsc95xx_eeprom_confirm_not_busy(udev);
Simon Glass246c1192011-06-13 16:13:09 -0700309 if (ret)
310 return ret;
311
312 for (i = 0; i < length; i++) {
313 val = E2P_CMD_BUSY_ | E2P_CMD_READ_ | (offset & E2P_CMD_ADDR_);
Simon Glass3faecae2015-07-07 20:53:41 -0600314 smsc95xx_write_reg(udev, E2P_CMD, val);
Simon Glass246c1192011-06-13 16:13:09 -0700315
Simon Glass3faecae2015-07-07 20:53:41 -0600316 ret = smsc95xx_wait_eeprom(udev);
Simon Glass246c1192011-06-13 16:13:09 -0700317 if (ret < 0)
318 return ret;
319
Simon Glass3faecae2015-07-07 20:53:41 -0600320 smsc95xx_read_reg(udev, E2P_DATA, &val);
Simon Glass246c1192011-06-13 16:13:09 -0700321 data[i] = val & 0xFF;
322 offset++;
323 }
324 return 0;
325}
326
327/*
328 * mii_nway_restart - restart NWay (autonegotiation) for this interface
329 *
330 * Returns 0 on success, negative on error.
331 */
Simon Glass3faecae2015-07-07 20:53:41 -0600332static int mii_nway_restart(struct usb_device *udev, struct ueth_data *dev)
Simon Glass246c1192011-06-13 16:13:09 -0700333{
334 int bmcr;
335 int r = -1;
336
337 /* if autoneg is off, it's an error */
Simon Glass3faecae2015-07-07 20:53:41 -0600338 bmcr = smsc95xx_mdio_read(udev, dev->phy_id, MII_BMCR);
Simon Glass246c1192011-06-13 16:13:09 -0700339
340 if (bmcr & BMCR_ANENABLE) {
341 bmcr |= BMCR_ANRESTART;
Simon Glass3faecae2015-07-07 20:53:41 -0600342 smsc95xx_mdio_write(udev, dev->phy_id, MII_BMCR, bmcr);
Simon Glass246c1192011-06-13 16:13:09 -0700343 r = 0;
344 }
345 return r;
346}
347
Simon Glass3faecae2015-07-07 20:53:41 -0600348static int smsc95xx_phy_initialize(struct usb_device *udev,
349 struct ueth_data *dev)
Simon Glass246c1192011-06-13 16:13:09 -0700350{
Simon Glass3faecae2015-07-07 20:53:41 -0600351 smsc95xx_mdio_write(udev, dev->phy_id, MII_BMCR, BMCR_RESET);
352 smsc95xx_mdio_write(udev, dev->phy_id, MII_ADVERTISE,
353 ADVERTISE_ALL | ADVERTISE_CSMA |
354 ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM);
Simon Glass246c1192011-06-13 16:13:09 -0700355
356 /* read to clear */
Simon Glass3faecae2015-07-07 20:53:41 -0600357 smsc95xx_mdio_read(udev, dev->phy_id, PHY_INT_SRC);
Simon Glass246c1192011-06-13 16:13:09 -0700358
Simon Glass3faecae2015-07-07 20:53:41 -0600359 smsc95xx_mdio_write(udev, dev->phy_id, PHY_INT_MASK,
360 PHY_INT_MASK_DEFAULT_);
361 mii_nway_restart(udev, dev);
Simon Glass246c1192011-06-13 16:13:09 -0700362
363 debug("phy initialised succesfully\n");
364 return 0;
365}
366
Simon Glass3faecae2015-07-07 20:53:41 -0600367static int smsc95xx_init_mac_address(unsigned char *enetaddr,
368 struct usb_device *udev)
Simon Glass246c1192011-06-13 16:13:09 -0700369{
Simon Glass3faecae2015-07-07 20:53:41 -0600370 int ret;
371
Simon Glass246c1192011-06-13 16:13:09 -0700372 /* try reading mac address from EEPROM */
Simon Glass3faecae2015-07-07 20:53:41 -0600373 ret = smsc95xx_read_eeprom(udev, EEPROM_MAC_OFFSET, ETH_ALEN, enetaddr);
374 if (ret)
375 return ret;
376
377 if (is_valid_ethaddr(enetaddr)) {
378 /* eeprom values are valid so use them */
379 debug("MAC address read from EEPROM\n");
380 return 0;
Simon Glass246c1192011-06-13 16:13:09 -0700381 }
382
383 /*
384 * No eeprom, or eeprom values are invalid. Generating a random MAC
385 * address is not safe. Just return an error.
386 */
Simon Glassebe0e5a2015-07-07 20:53:40 -0600387 debug("Invalid MAC address read from EEPROM\n");
388
389 return -ENXIO;
Simon Glass246c1192011-06-13 16:13:09 -0700390}
391
Simon Glass3faecae2015-07-07 20:53:41 -0600392static int smsc95xx_write_hwaddr_common(struct usb_device *udev,
393 struct smsc95xx_private *priv,
394 unsigned char *enetaddr)
Simon Glass246c1192011-06-13 16:13:09 -0700395{
Chris Packhamfd3224b2016-07-13 09:52:36 +1200396 u32 addr_lo = get_unaligned_le32(&enetaddr[0]);
397 u32 addr_hi = get_unaligned_le16(&enetaddr[4]);
Simon Glass246c1192011-06-13 16:13:09 -0700398 int ret;
399
400 /* set hardware address */
401 debug("** %s()\n", __func__);
Simon Glass3faecae2015-07-07 20:53:41 -0600402 ret = smsc95xx_write_reg(udev, ADDRL, addr_lo);
Wolfgang Grandeggerf9af1f82011-11-14 23:19:15 +0000403 if (ret < 0)
Simon Glass246c1192011-06-13 16:13:09 -0700404 return ret;
Simon Glass246c1192011-06-13 16:13:09 -0700405
Simon Glass3faecae2015-07-07 20:53:41 -0600406 ret = smsc95xx_write_reg(udev, ADDRH, addr_hi);
Simon Glass246c1192011-06-13 16:13:09 -0700407 if (ret < 0)
408 return ret;
Wolfgang Grandeggerf9af1f82011-11-14 23:19:15 +0000409
Simon Glass3faecae2015-07-07 20:53:41 -0600410 debug("MAC %pM\n", enetaddr);
Lucas Stach36267c42012-08-22 11:04:57 +0000411 priv->have_hwaddr = 1;
Simon Glass3faecae2015-07-07 20:53:41 -0600412
Simon Glass246c1192011-06-13 16:13:09 -0700413 return 0;
414}
415
416/* Enable or disable Tx & Rx checksum offload engines */
Simon Glass3faecae2015-07-07 20:53:41 -0600417static int smsc95xx_set_csums(struct usb_device *udev, int use_tx_csum,
418 int use_rx_csum)
Simon Glass246c1192011-06-13 16:13:09 -0700419{
420 u32 read_buf;
Simon Glass3faecae2015-07-07 20:53:41 -0600421 int ret = smsc95xx_read_reg(udev, COE_CR, &read_buf);
Simon Glass246c1192011-06-13 16:13:09 -0700422 if (ret < 0)
423 return ret;
424
425 if (use_tx_csum)
426 read_buf |= Tx_COE_EN_;
427 else
428 read_buf &= ~Tx_COE_EN_;
429
430 if (use_rx_csum)
431 read_buf |= Rx_COE_EN_;
432 else
433 read_buf &= ~Rx_COE_EN_;
434
Simon Glass3faecae2015-07-07 20:53:41 -0600435 ret = smsc95xx_write_reg(udev, COE_CR, read_buf);
Simon Glass246c1192011-06-13 16:13:09 -0700436 if (ret < 0)
437 return ret;
438
439 debug("COE_CR = 0x%08x\n", read_buf);
440 return 0;
441}
442
Simon Glass3faecae2015-07-07 20:53:41 -0600443static void smsc95xx_set_multicast(struct smsc95xx_private *priv)
Simon Glass246c1192011-06-13 16:13:09 -0700444{
445 /* No multicast in u-boot */
Lucas Stach36267c42012-08-22 11:04:57 +0000446 priv->mac_cr &= ~(MAC_CR_PRMS_ | MAC_CR_MCPAS_ | MAC_CR_HPFILT_);
Simon Glass246c1192011-06-13 16:13:09 -0700447}
448
449/* starts the TX path */
Simon Glass3faecae2015-07-07 20:53:41 -0600450static void smsc95xx_start_tx_path(struct usb_device *udev,
451 struct smsc95xx_private *priv)
Simon Glass246c1192011-06-13 16:13:09 -0700452{
453 u32 reg_val;
454
455 /* Enable Tx at MAC */
Lucas Stach36267c42012-08-22 11:04:57 +0000456 priv->mac_cr |= MAC_CR_TXEN_;
Simon Glass246c1192011-06-13 16:13:09 -0700457
Simon Glass3faecae2015-07-07 20:53:41 -0600458 smsc95xx_write_reg(udev, MAC_CR, priv->mac_cr);
Simon Glass246c1192011-06-13 16:13:09 -0700459
460 /* Enable Tx at SCSRs */
461 reg_val = TX_CFG_ON_;
Simon Glass3faecae2015-07-07 20:53:41 -0600462 smsc95xx_write_reg(udev, TX_CFG, reg_val);
Simon Glass246c1192011-06-13 16:13:09 -0700463}
464
465/* Starts the Receive path */
Simon Glass3faecae2015-07-07 20:53:41 -0600466static void smsc95xx_start_rx_path(struct usb_device *udev,
467 struct smsc95xx_private *priv)
Simon Glass246c1192011-06-13 16:13:09 -0700468{
Lucas Stach36267c42012-08-22 11:04:57 +0000469 priv->mac_cr |= MAC_CR_RXEN_;
Simon Glass3faecae2015-07-07 20:53:41 -0600470 smsc95xx_write_reg(udev, MAC_CR, priv->mac_cr);
Simon Glass246c1192011-06-13 16:13:09 -0700471}
472
Simon Glass3faecae2015-07-07 20:53:41 -0600473static int smsc95xx_init_common(struct usb_device *udev, struct ueth_data *dev,
474 struct smsc95xx_private *priv,
475 unsigned char *enetaddr)
Simon Glass246c1192011-06-13 16:13:09 -0700476{
477 int ret;
478 u32 write_buf;
479 u32 read_buf;
480 u32 burst_cap;
481 int timeout;
Simon Glass246c1192011-06-13 16:13:09 -0700482#define TIMEOUT_RESOLUTION 50 /* ms */
483 int link_detected;
484
485 debug("** %s()\n", __func__);
486 dev->phy_id = SMSC95XX_INTERNAL_PHY_ID; /* fixed phy id */
487
488 write_buf = HW_CFG_LRST_;
Simon Glass3faecae2015-07-07 20:53:41 -0600489 ret = smsc95xx_write_reg(udev, HW_CFG, write_buf);
Simon Glass246c1192011-06-13 16:13:09 -0700490 if (ret < 0)
491 return ret;
492
493 timeout = 0;
494 do {
Simon Glass3faecae2015-07-07 20:53:41 -0600495 ret = smsc95xx_read_reg(udev, HW_CFG, &read_buf);
Simon Glass246c1192011-06-13 16:13:09 -0700496 if (ret < 0)
497 return ret;
498 udelay(10 * 1000);
499 timeout++;
500 } while ((read_buf & HW_CFG_LRST_) && (timeout < 100));
501
502 if (timeout >= 100) {
503 debug("timeout waiting for completion of Lite Reset\n");
Simon Glassebe0e5a2015-07-07 20:53:40 -0600504 return -ETIMEDOUT;
Simon Glass246c1192011-06-13 16:13:09 -0700505 }
506
507 write_buf = PM_CTL_PHY_RST_;
Simon Glass3faecae2015-07-07 20:53:41 -0600508 ret = smsc95xx_write_reg(udev, PM_CTRL, write_buf);
Simon Glass246c1192011-06-13 16:13:09 -0700509 if (ret < 0)
510 return ret;
511
512 timeout = 0;
513 do {
Simon Glass3faecae2015-07-07 20:53:41 -0600514 ret = smsc95xx_read_reg(udev, PM_CTRL, &read_buf);
Simon Glass246c1192011-06-13 16:13:09 -0700515 if (ret < 0)
516 return ret;
517 udelay(10 * 1000);
518 timeout++;
519 } while ((read_buf & PM_CTL_PHY_RST_) && (timeout < 100));
520 if (timeout >= 100) {
521 debug("timeout waiting for PHY Reset\n");
Simon Glassebe0e5a2015-07-07 20:53:40 -0600522 return -ETIMEDOUT;
Simon Glass246c1192011-06-13 16:13:09 -0700523 }
Stephen Warrenc8bff832016-09-15 12:53:22 -0600524#ifndef CONFIG_DM_ETH
Simon Glass3faecae2015-07-07 20:53:41 -0600525 if (!priv->have_hwaddr && smsc95xx_init_mac_address(enetaddr, udev) ==
526 0)
Lucas Stach36267c42012-08-22 11:04:57 +0000527 priv->have_hwaddr = 1;
Stephen Warrenc8bff832016-09-15 12:53:22 -0600528#endif
Lucas Stach36267c42012-08-22 11:04:57 +0000529 if (!priv->have_hwaddr) {
Simon Glass246c1192011-06-13 16:13:09 -0700530 puts("Error: SMSC95xx: No MAC address set - set usbethaddr\n");
Simon Glassebe0e5a2015-07-07 20:53:40 -0600531 return -EADDRNOTAVAIL;
Simon Glass246c1192011-06-13 16:13:09 -0700532 }
Simon Glass3faecae2015-07-07 20:53:41 -0600533 ret = smsc95xx_write_hwaddr_common(udev, priv, enetaddr);
Simon Glassebe0e5a2015-07-07 20:53:40 -0600534 if (ret < 0)
535 return ret;
Simon Glass246c1192011-06-13 16:13:09 -0700536
Simon Glass246c1192011-06-13 16:13:09 -0700537#ifdef TURBO_MODE
538 if (dev->pusb_dev->speed == USB_SPEED_HIGH) {
539 burst_cap = DEFAULT_HS_BURST_CAP_SIZE / HS_USB_PKT_SIZE;
Lucas Stach36267c42012-08-22 11:04:57 +0000540 priv->rx_urb_size = DEFAULT_HS_BURST_CAP_SIZE;
Simon Glass246c1192011-06-13 16:13:09 -0700541 } else {
542 burst_cap = DEFAULT_FS_BURST_CAP_SIZE / FS_USB_PKT_SIZE;
Lucas Stach36267c42012-08-22 11:04:57 +0000543 priv->rx_urb_size = DEFAULT_FS_BURST_CAP_SIZE;
Simon Glass246c1192011-06-13 16:13:09 -0700544 }
545#else
546 burst_cap = 0;
Lucas Stach36267c42012-08-22 11:04:57 +0000547 priv->rx_urb_size = MAX_SINGLE_PACKET_SIZE;
Simon Glass246c1192011-06-13 16:13:09 -0700548#endif
Lucas Stach36267c42012-08-22 11:04:57 +0000549 debug("rx_urb_size=%ld\n", (ulong)priv->rx_urb_size);
Simon Glass246c1192011-06-13 16:13:09 -0700550
Simon Glass3faecae2015-07-07 20:53:41 -0600551 ret = smsc95xx_write_reg(udev, BURST_CAP, burst_cap);
Simon Glass246c1192011-06-13 16:13:09 -0700552 if (ret < 0)
553 return ret;
554
Simon Glass3faecae2015-07-07 20:53:41 -0600555 ret = smsc95xx_read_reg(udev, BURST_CAP, &read_buf);
Simon Glass246c1192011-06-13 16:13:09 -0700556 if (ret < 0)
557 return ret;
558 debug("Read Value from BURST_CAP after writing: 0x%08x\n", read_buf);
559
560 read_buf = DEFAULT_BULK_IN_DELAY;
Simon Glass3faecae2015-07-07 20:53:41 -0600561 ret = smsc95xx_write_reg(udev, BULK_IN_DLY, read_buf);
Simon Glass246c1192011-06-13 16:13:09 -0700562 if (ret < 0)
563 return ret;
564
Simon Glass3faecae2015-07-07 20:53:41 -0600565 ret = smsc95xx_read_reg(udev, BULK_IN_DLY, &read_buf);
Simon Glass246c1192011-06-13 16:13:09 -0700566 if (ret < 0)
567 return ret;
568 debug("Read Value from BULK_IN_DLY after writing: "
569 "0x%08x\n", read_buf);
570
Simon Glass3faecae2015-07-07 20:53:41 -0600571 ret = smsc95xx_read_reg(udev, HW_CFG, &read_buf);
Simon Glass246c1192011-06-13 16:13:09 -0700572 if (ret < 0)
573 return ret;
574 debug("Read Value from HW_CFG: 0x%08x\n", read_buf);
575
576#ifdef TURBO_MODE
577 read_buf |= (HW_CFG_MEF_ | HW_CFG_BCE_);
578#endif
579 read_buf &= ~HW_CFG_RXDOFF_;
580
581#define NET_IP_ALIGN 0
582 read_buf |= NET_IP_ALIGN << 9;
583
Simon Glass3faecae2015-07-07 20:53:41 -0600584 ret = smsc95xx_write_reg(udev, HW_CFG, read_buf);
Simon Glass246c1192011-06-13 16:13:09 -0700585 if (ret < 0)
586 return ret;
587
Simon Glass3faecae2015-07-07 20:53:41 -0600588 ret = smsc95xx_read_reg(udev, HW_CFG, &read_buf);
Simon Glass246c1192011-06-13 16:13:09 -0700589 if (ret < 0)
590 return ret;
591 debug("Read Value from HW_CFG after writing: 0x%08x\n", read_buf);
592
593 write_buf = 0xFFFFFFFF;
Simon Glass3faecae2015-07-07 20:53:41 -0600594 ret = smsc95xx_write_reg(udev, INT_STS, write_buf);
Simon Glass246c1192011-06-13 16:13:09 -0700595 if (ret < 0)
596 return ret;
597
Simon Glass3faecae2015-07-07 20:53:41 -0600598 ret = smsc95xx_read_reg(udev, ID_REV, &read_buf);
Simon Glass246c1192011-06-13 16:13:09 -0700599 if (ret < 0)
600 return ret;
601 debug("ID_REV = 0x%08x\n", read_buf);
602
Suriyan Ramasamie0f2f8c2013-10-07 20:30:58 -0700603 /* Configure GPIO pins as LED outputs */
604 write_buf = LED_GPIO_CFG_SPD_LED | LED_GPIO_CFG_LNK_LED |
605 LED_GPIO_CFG_FDX_LED;
Simon Glass3faecae2015-07-07 20:53:41 -0600606 ret = smsc95xx_write_reg(udev, LED_GPIO_CFG, write_buf);
Suriyan Ramasamie0f2f8c2013-10-07 20:30:58 -0700607 if (ret < 0)
608 return ret;
609 debug("LED_GPIO_CFG set\n");
610
Simon Glass246c1192011-06-13 16:13:09 -0700611 /* Init Tx */
612 write_buf = 0;
Simon Glass3faecae2015-07-07 20:53:41 -0600613 ret = smsc95xx_write_reg(udev, FLOW, write_buf);
Simon Glass246c1192011-06-13 16:13:09 -0700614 if (ret < 0)
615 return ret;
616
617 read_buf = AFC_CFG_DEFAULT;
Simon Glass3faecae2015-07-07 20:53:41 -0600618 ret = smsc95xx_write_reg(udev, AFC_CFG, read_buf);
Simon Glass246c1192011-06-13 16:13:09 -0700619 if (ret < 0)
620 return ret;
621
Simon Glass3faecae2015-07-07 20:53:41 -0600622 ret = smsc95xx_read_reg(udev, MAC_CR, &priv->mac_cr);
Simon Glass246c1192011-06-13 16:13:09 -0700623 if (ret < 0)
624 return ret;
625
626 /* Init Rx. Set Vlan */
627 write_buf = (u32)ETH_P_8021Q;
Simon Glass3faecae2015-07-07 20:53:41 -0600628 ret = smsc95xx_write_reg(udev, VLAN1, write_buf);
Simon Glass246c1192011-06-13 16:13:09 -0700629 if (ret < 0)
630 return ret;
631
632 /* Disable checksum offload engines */
Simon Glass3faecae2015-07-07 20:53:41 -0600633 ret = smsc95xx_set_csums(udev, 0, 0);
Simon Glass246c1192011-06-13 16:13:09 -0700634 if (ret < 0) {
635 debug("Failed to set csum offload: %d\n", ret);
636 return ret;
637 }
Simon Glass3faecae2015-07-07 20:53:41 -0600638 smsc95xx_set_multicast(priv);
Simon Glass246c1192011-06-13 16:13:09 -0700639
Simon Glass3faecae2015-07-07 20:53:41 -0600640 ret = smsc95xx_phy_initialize(udev, dev);
Simon Glassebe0e5a2015-07-07 20:53:40 -0600641 if (ret < 0)
642 return ret;
Simon Glass3faecae2015-07-07 20:53:41 -0600643 ret = smsc95xx_read_reg(udev, INT_EP_CTL, &read_buf);
Simon Glass246c1192011-06-13 16:13:09 -0700644 if (ret < 0)
645 return ret;
646
647 /* enable PHY interrupts */
648 read_buf |= INT_EP_CTL_PHY_INT_;
649
Simon Glass3faecae2015-07-07 20:53:41 -0600650 ret = smsc95xx_write_reg(udev, INT_EP_CTL, read_buf);
Simon Glass246c1192011-06-13 16:13:09 -0700651 if (ret < 0)
652 return ret;
653
Simon Glass3faecae2015-07-07 20:53:41 -0600654 smsc95xx_start_tx_path(udev, priv);
655 smsc95xx_start_rx_path(udev, priv);
Simon Glass246c1192011-06-13 16:13:09 -0700656
657 timeout = 0;
658 do {
Simon Glass3faecae2015-07-07 20:53:41 -0600659 link_detected = smsc95xx_mdio_read(udev, dev->phy_id, MII_BMSR)
Simon Glass246c1192011-06-13 16:13:09 -0700660 & BMSR_LSTATUS;
661 if (!link_detected) {
662 if (timeout == 0)
663 printf("Waiting for Ethernet connection... ");
664 udelay(TIMEOUT_RESOLUTION * 1000);
665 timeout += TIMEOUT_RESOLUTION;
666 }
667 } while (!link_detected && timeout < PHY_CONNECT_TIMEOUT);
668 if (link_detected) {
669 if (timeout != 0)
670 printf("done.\n");
671 } else {
672 printf("unable to connect.\n");
Simon Glassebe0e5a2015-07-07 20:53:40 -0600673 return -EIO;
Simon Glass246c1192011-06-13 16:13:09 -0700674 }
675 return 0;
676}
677
Simon Glass3faecae2015-07-07 20:53:41 -0600678static int smsc95xx_send_common(struct ueth_data *dev, void *packet, int length)
Simon Glass246c1192011-06-13 16:13:09 -0700679{
Simon Glass246c1192011-06-13 16:13:09 -0700680 int err;
681 int actual_len;
682 u32 tx_cmd_a;
683 u32 tx_cmd_b;
Ilya Yanok43b56c22012-07-15 04:43:53 +0000684 ALLOC_CACHE_ALIGN_BUFFER(unsigned char, msg,
685 PKTSIZE + sizeof(tx_cmd_a) + sizeof(tx_cmd_b));
Simon Glass246c1192011-06-13 16:13:09 -0700686
Prabhakar Kushwaha91d52542015-10-25 13:18:41 +0530687 debug("** %s(), len %d, buf %#x\n", __func__, length,
688 (unsigned int)(ulong)msg);
Simon Glass246c1192011-06-13 16:13:09 -0700689 if (length > PKTSIZE)
Simon Glassebe0e5a2015-07-07 20:53:40 -0600690 return -ENOSPC;
Simon Glass246c1192011-06-13 16:13:09 -0700691
692 tx_cmd_a = (u32)length | TX_CMD_A_FIRST_SEG_ | TX_CMD_A_LAST_SEG_;
693 tx_cmd_b = (u32)length;
694 cpu_to_le32s(&tx_cmd_a);
695 cpu_to_le32s(&tx_cmd_b);
696
697 /* prepend cmd_a and cmd_b */
698 memcpy(msg, &tx_cmd_a, sizeof(tx_cmd_a));
699 memcpy(msg + sizeof(tx_cmd_a), &tx_cmd_b, sizeof(tx_cmd_b));
700 memcpy(msg + sizeof(tx_cmd_a) + sizeof(tx_cmd_b), (void *)packet,
701 length);
702 err = usb_bulk_msg(dev->pusb_dev,
703 usb_sndbulkpipe(dev->pusb_dev, dev->ep_out),
704 (void *)msg,
705 length + sizeof(tx_cmd_a) + sizeof(tx_cmd_b),
706 &actual_len,
707 USB_BULK_SEND_TIMEOUT);
708 debug("Tx: len = %u, actual = %u, err = %d\n",
Prabhakar Kushwaha91d52542015-10-25 13:18:41 +0530709 (unsigned int)(length + sizeof(tx_cmd_a) + sizeof(tx_cmd_b)),
710 (unsigned int)actual_len, err);
Simon Glass3faecae2015-07-07 20:53:41 -0600711
Simon Glass246c1192011-06-13 16:13:09 -0700712 return err;
713}
714
Simon Glass68c41972015-07-07 20:53:42 -0600715#ifndef CONFIG_DM_ETH
Simon Glass3faecae2015-07-07 20:53:41 -0600716/*
717 * Smsc95xx callbacks
718 */
Masahiro Yamadaf7ed78b2020-06-26 15:13:33 +0900719static int smsc95xx_init(struct eth_device *eth, struct bd_info *bd)
Simon Glass3faecae2015-07-07 20:53:41 -0600720{
721 struct ueth_data *dev = (struct ueth_data *)eth->priv;
722 struct usb_device *udev = dev->pusb_dev;
723 struct smsc95xx_private *priv =
724 (struct smsc95xx_private *)dev->dev_priv;
725
726 return smsc95xx_init_common(udev, dev, priv, eth->enetaddr);
727}
728
729static int smsc95xx_send(struct eth_device *eth, void *packet, int length)
730{
731 struct ueth_data *dev = (struct ueth_data *)eth->priv;
732
733 return smsc95xx_send_common(dev, packet, length);
734}
735
Simon Glass246c1192011-06-13 16:13:09 -0700736static int smsc95xx_recv(struct eth_device *eth)
737{
738 struct ueth_data *dev = (struct ueth_data *)eth->priv;
Simon Glass4d89e5b2015-07-07 20:53:39 -0600739 DEFINE_CACHE_ALIGN_BUFFER(unsigned char, recv_buf, RX_URB_SIZE);
Simon Glass246c1192011-06-13 16:13:09 -0700740 unsigned char *buf_ptr;
741 int err;
742 int actual_len;
743 u32 packet_len;
744 int cur_buf_align;
745
746 debug("** %s()\n", __func__);
747 err = usb_bulk_msg(dev->pusb_dev,
Simon Glass3faecae2015-07-07 20:53:41 -0600748 usb_rcvbulkpipe(dev->pusb_dev, dev->ep_in),
749 (void *)recv_buf, RX_URB_SIZE, &actual_len,
750 USB_BULK_RECV_TIMEOUT);
Simon Glass4d89e5b2015-07-07 20:53:39 -0600751 debug("Rx: len = %u, actual = %u, err = %d\n", RX_URB_SIZE,
Simon Glass246c1192011-06-13 16:13:09 -0700752 actual_len, err);
753 if (err != 0) {
754 debug("Rx: failed to receive\n");
Simon Glass3faecae2015-07-07 20:53:41 -0600755 return -err;
Simon Glass246c1192011-06-13 16:13:09 -0700756 }
Simon Glass4d89e5b2015-07-07 20:53:39 -0600757 if (actual_len > RX_URB_SIZE) {
Simon Glass246c1192011-06-13 16:13:09 -0700758 debug("Rx: received too many bytes %d\n", actual_len);
Simon Glassebe0e5a2015-07-07 20:53:40 -0600759 return -ENOSPC;
Simon Glass246c1192011-06-13 16:13:09 -0700760 }
761
762 buf_ptr = recv_buf;
763 while (actual_len > 0) {
764 /*
765 * 1st 4 bytes contain the length of the actual data plus error
766 * info. Extract data length.
767 */
768 if (actual_len < sizeof(packet_len)) {
769 debug("Rx: incomplete packet length\n");
Simon Glassebe0e5a2015-07-07 20:53:40 -0600770 return -EIO;
Simon Glass246c1192011-06-13 16:13:09 -0700771 }
772 memcpy(&packet_len, buf_ptr, sizeof(packet_len));
773 le32_to_cpus(&packet_len);
774 if (packet_len & RX_STS_ES_) {
775 debug("Rx: Error header=%#x", packet_len);
Simon Glassebe0e5a2015-07-07 20:53:40 -0600776 return -EIO;
Simon Glass246c1192011-06-13 16:13:09 -0700777 }
778 packet_len = ((packet_len & RX_STS_FL_) >> 16);
779
780 if (packet_len > actual_len - sizeof(packet_len)) {
781 debug("Rx: too large packet: %d\n", packet_len);
Simon Glassebe0e5a2015-07-07 20:53:40 -0600782 return -EIO;
Simon Glass246c1192011-06-13 16:13:09 -0700783 }
784
785 /* Notify net stack */
Joe Hershberger9f09a362015-04-08 01:41:06 -0500786 net_process_received_packet(buf_ptr + sizeof(packet_len),
787 packet_len - 4);
Simon Glass246c1192011-06-13 16:13:09 -0700788
789 /* Adjust for next iteration */
790 actual_len -= sizeof(packet_len) + packet_len;
791 buf_ptr += sizeof(packet_len) + packet_len;
Prabhakar Kushwaha91d52542015-10-25 13:18:41 +0530792 cur_buf_align = (ulong)buf_ptr - (ulong)recv_buf;
Simon Glass246c1192011-06-13 16:13:09 -0700793
794 if (cur_buf_align & 0x03) {
795 int align = 4 - (cur_buf_align & 0x03);
796
797 actual_len -= align;
798 buf_ptr += align;
799 }
800 }
801 return err;
802}
803
804static void smsc95xx_halt(struct eth_device *eth)
805{
806 debug("** %s()\n", __func__);
807}
808
Simon Glass3faecae2015-07-07 20:53:41 -0600809static int smsc95xx_write_hwaddr(struct eth_device *eth)
810{
811 struct ueth_data *dev = eth->priv;
812 struct usb_device *udev = dev->pusb_dev;
813 struct smsc95xx_private *priv = dev->dev_priv;
814
815 return smsc95xx_write_hwaddr_common(udev, priv, eth->enetaddr);
816}
817
Simon Glass246c1192011-06-13 16:13:09 -0700818/*
819 * SMSC probing functions
820 */
821void smsc95xx_eth_before_probe(void)
822{
823 curr_eth_dev = 0;
824}
825
826struct smsc95xx_dongle {
827 unsigned short vendor;
828 unsigned short product;
829};
830
831static const struct smsc95xx_dongle smsc95xx_dongles[] = {
832 { 0x0424, 0xec00 }, /* LAN9512/LAN9514 Ethernet */
833 { 0x0424, 0x9500 }, /* LAN9500 Ethernet */
Lubomir Popov7ca25b62013-04-01 04:50:55 +0000834 { 0x0424, 0x9730 }, /* LAN9730 Ethernet (HSIC) */
Stefan Roese8aa7b042013-07-03 18:34:54 +0200835 { 0x0424, 0x9900 }, /* SMSC9500 USB Ethernet Device (SAL10) */
Ilya Ledvich180bb6a2014-03-12 10:36:31 +0200836 { 0x0424, 0x9e00 }, /* LAN9500A Ethernet */
Simon Glass246c1192011-06-13 16:13:09 -0700837 { 0x0000, 0x0000 } /* END - Do not remove */
838};
839
840/* Probe to see if a new device is actually an SMSC device */
841int smsc95xx_eth_probe(struct usb_device *dev, unsigned int ifnum,
842 struct ueth_data *ss)
843{
844 struct usb_interface *iface;
845 struct usb_interface_descriptor *iface_desc;
846 int i;
847
848 /* let's examine the device now */
849 iface = &dev->config.if_desc[ifnum];
850 iface_desc = &dev->config.if_desc[ifnum].desc;
851
852 for (i = 0; smsc95xx_dongles[i].vendor != 0; i++) {
853 if (dev->descriptor.idVendor == smsc95xx_dongles[i].vendor &&
854 dev->descriptor.idProduct == smsc95xx_dongles[i].product)
855 /* Found a supported dongle */
856 break;
857 }
858 if (smsc95xx_dongles[i].vendor == 0)
859 return 0;
860
861 /* At this point, we know we've got a live one */
862 debug("\n\nUSB Ethernet device detected\n");
863 memset(ss, '\0', sizeof(struct ueth_data));
864
865 /* Initialize the ueth_data structure with some useful info */
866 ss->ifnum = ifnum;
867 ss->pusb_dev = dev;
868 ss->subclass = iface_desc->bInterfaceSubClass;
869 ss->protocol = iface_desc->bInterfaceProtocol;
870
871 /*
872 * We are expecting a minimum of 3 endpoints - in, out (bulk), and int.
873 * We will ignore any others.
874 */
875 for (i = 0; i < iface_desc->bNumEndpoints; i++) {
876 /* is it an BULK endpoint? */
877 if ((iface->ep_desc[i].bmAttributes &
878 USB_ENDPOINT_XFERTYPE_MASK) == USB_ENDPOINT_XFER_BULK) {
879 if (iface->ep_desc[i].bEndpointAddress & USB_DIR_IN)
880 ss->ep_in =
881 iface->ep_desc[i].bEndpointAddress &
882 USB_ENDPOINT_NUMBER_MASK;
883 else
884 ss->ep_out =
885 iface->ep_desc[i].bEndpointAddress &
886 USB_ENDPOINT_NUMBER_MASK;
887 }
888
889 /* is it an interrupt endpoint? */
890 if ((iface->ep_desc[i].bmAttributes &
891 USB_ENDPOINT_XFERTYPE_MASK) == USB_ENDPOINT_XFER_INT) {
892 ss->ep_int = iface->ep_desc[i].bEndpointAddress &
893 USB_ENDPOINT_NUMBER_MASK;
894 ss->irqinterval = iface->ep_desc[i].bInterval;
895 }
896 }
897 debug("Endpoints In %d Out %d Int %d\n",
898 ss->ep_in, ss->ep_out, ss->ep_int);
899
900 /* Do some basic sanity checks, and bail if we find a problem */
901 if (usb_set_interface(dev, iface_desc->bInterfaceNumber, 0) ||
902 !ss->ep_in || !ss->ep_out || !ss->ep_int) {
903 debug("Problems with device\n");
904 return 0;
905 }
906 dev->privptr = (void *)ss;
Lucas Stach36267c42012-08-22 11:04:57 +0000907
908 /* alloc driver private */
909 ss->dev_priv = calloc(1, sizeof(struct smsc95xx_private));
910 if (!ss->dev_priv)
911 return 0;
912
Simon Glass246c1192011-06-13 16:13:09 -0700913 return 1;
914}
915
916int smsc95xx_eth_get_info(struct usb_device *dev, struct ueth_data *ss,
917 struct eth_device *eth)
918{
919 debug("** %s()\n", __func__);
920 if (!eth) {
921 debug("%s: missing parameter.\n", __func__);
922 return 0;
923 }
924 sprintf(eth->name, "%s%d", SMSC95XX_BASE_NAME, curr_eth_dev++);
925 eth->init = smsc95xx_init;
926 eth->send = smsc95xx_send;
927 eth->recv = smsc95xx_recv;
928 eth->halt = smsc95xx_halt;
929 eth->write_hwaddr = smsc95xx_write_hwaddr;
930 eth->priv = ss;
931 return 1;
932}
Simon Glass68c41972015-07-07 20:53:42 -0600933#endif /* !CONFIG_DM_ETH */
934
935#ifdef CONFIG_DM_ETH
936static int smsc95xx_eth_start(struct udevice *dev)
937{
Simon Glassde44acf2015-09-28 23:32:01 -0600938 struct usb_device *udev = dev_get_parent_priv(dev);
Simon Glass68c41972015-07-07 20:53:42 -0600939 struct smsc95xx_private *priv = dev_get_priv(dev);
Simon Glassfa20e932020-12-03 16:55:20 -0700940 struct eth_pdata *pdata = dev_get_plat(dev);
Simon Glass68c41972015-07-07 20:53:42 -0600941
942 /* Driver-model Ethernet ensures we have this */
943 priv->have_hwaddr = 1;
944
945 return smsc95xx_init_common(udev, &priv->ueth, priv, pdata->enetaddr);
946}
947
948void smsc95xx_eth_stop(struct udevice *dev)
949{
950 debug("** %s()\n", __func__);
951}
952
953int smsc95xx_eth_send(struct udevice *dev, void *packet, int length)
954{
955 struct smsc95xx_private *priv = dev_get_priv(dev);
956
957 return smsc95xx_send_common(&priv->ueth, packet, length);
958}
959
960int smsc95xx_eth_recv(struct udevice *dev, int flags, uchar **packetp)
961{
962 struct smsc95xx_private *priv = dev_get_priv(dev);
963 struct ueth_data *ueth = &priv->ueth;
964 uint8_t *ptr;
965 int ret, len;
966 u32 packet_len;
967
968 len = usb_ether_get_rx_bytes(ueth, &ptr);
969 debug("%s: first try, len=%d\n", __func__, len);
970 if (!len) {
971 if (!(flags & ETH_RECV_CHECK_DEVICE))
972 return -EAGAIN;
973 ret = usb_ether_receive(ueth, RX_URB_SIZE);
974 if (ret == -EAGAIN)
975 return ret;
976
977 len = usb_ether_get_rx_bytes(ueth, &ptr);
978 debug("%s: second try, len=%d\n", __func__, len);
979 }
980
981 /*
982 * 1st 4 bytes contain the length of the actual data plus error info.
983 * Extract data length.
984 */
985 if (len < sizeof(packet_len)) {
986 debug("Rx: incomplete packet length\n");
987 goto err;
988 }
989 memcpy(&packet_len, ptr, sizeof(packet_len));
990 le32_to_cpus(&packet_len);
991 if (packet_len & RX_STS_ES_) {
992 debug("Rx: Error header=%#x", packet_len);
993 goto err;
994 }
995 packet_len = ((packet_len & RX_STS_FL_) >> 16);
996
997 if (packet_len > len - sizeof(packet_len)) {
998 debug("Rx: too large packet: %d\n", packet_len);
999 goto err;
1000 }
1001
1002 *packetp = ptr + sizeof(packet_len);
Simon Glassb8fa8362017-04-05 16:23:28 -06001003 return packet_len - 4;
Simon Glass68c41972015-07-07 20:53:42 -06001004
1005err:
1006 usb_ether_advance_rxbuf(ueth, -1);
1007 return -EINVAL;
1008}
1009
1010static int smsc95xx_free_pkt(struct udevice *dev, uchar *packet, int packet_len)
1011{
1012 struct smsc95xx_private *priv = dev_get_priv(dev);
1013
Simon Glassb8fa8362017-04-05 16:23:28 -06001014 packet_len = ALIGN(packet_len + sizeof(u32), 4);
Simon Glass68c41972015-07-07 20:53:42 -06001015 usb_ether_advance_rxbuf(&priv->ueth, sizeof(u32) + packet_len);
1016
1017 return 0;
1018}
1019
1020int smsc95xx_write_hwaddr(struct udevice *dev)
1021{
Simon Glassde44acf2015-09-28 23:32:01 -06001022 struct usb_device *udev = dev_get_parent_priv(dev);
Simon Glassfa20e932020-12-03 16:55:20 -07001023 struct eth_pdata *pdata = dev_get_plat(dev);
Simon Glass68c41972015-07-07 20:53:42 -06001024 struct smsc95xx_private *priv = dev_get_priv(dev);
1025
1026 return smsc95xx_write_hwaddr_common(udev, priv, pdata->enetaddr);
1027}
1028
Stephen Warrenc8bff832016-09-15 12:53:22 -06001029int smsc95xx_read_rom_hwaddr(struct udevice *dev)
1030{
1031 struct usb_device *udev = dev_get_parent_priv(dev);
Simon Glassfa20e932020-12-03 16:55:20 -07001032 struct eth_pdata *pdata = dev_get_plat(dev);
Stephen Warrenc8bff832016-09-15 12:53:22 -06001033 int ret;
1034
1035 ret = smsc95xx_init_mac_address(pdata->enetaddr, udev);
1036 if (ret)
1037 memset(pdata->enetaddr, 0, 6);
1038
1039 return 0;
1040}
1041
Simon Glass68c41972015-07-07 20:53:42 -06001042static int smsc95xx_eth_probe(struct udevice *dev)
1043{
1044 struct smsc95xx_private *priv = dev_get_priv(dev);
1045 struct ueth_data *ueth = &priv->ueth;
1046
1047 return usb_ether_register(dev, ueth, RX_URB_SIZE);
1048}
1049
1050static const struct eth_ops smsc95xx_eth_ops = {
1051 .start = smsc95xx_eth_start,
1052 .send = smsc95xx_eth_send,
1053 .recv = smsc95xx_eth_recv,
1054 .free_pkt = smsc95xx_free_pkt,
1055 .stop = smsc95xx_eth_stop,
1056 .write_hwaddr = smsc95xx_write_hwaddr,
Stephen Warrenc8bff832016-09-15 12:53:22 -06001057 .read_rom_hwaddr = smsc95xx_read_rom_hwaddr,
Simon Glass68c41972015-07-07 20:53:42 -06001058};
1059
1060U_BOOT_DRIVER(smsc95xx_eth) = {
1061 .name = "smsc95xx_eth",
1062 .id = UCLASS_ETH,
1063 .probe = smsc95xx_eth_probe,
1064 .ops = &smsc95xx_eth_ops,
Simon Glass8a2b47f2020-12-03 16:55:17 -07001065 .priv_auto = sizeof(struct smsc95xx_private),
Simon Glass71fa5b42020-12-03 16:55:18 -07001066 .plat_auto = sizeof(struct eth_pdata),
Simon Glass68c41972015-07-07 20:53:42 -06001067};
1068
1069static const struct usb_device_id smsc95xx_eth_id_table[] = {
1070 { USB_DEVICE(0x05ac, 0x1402) },
1071 { USB_DEVICE(0x0424, 0xec00) }, /* LAN9512/LAN9514 Ethernet */
1072 { USB_DEVICE(0x0424, 0x9500) }, /* LAN9500 Ethernet */
1073 { USB_DEVICE(0x0424, 0x9730) }, /* LAN9730 Ethernet (HSIC) */
1074 { USB_DEVICE(0x0424, 0x9900) }, /* SMSC9500 USB Ethernet (SAL10) */
1075 { USB_DEVICE(0x0424, 0x9e00) }, /* LAN9500A Ethernet */
1076 { } /* Terminating entry */
1077};
1078
1079U_BOOT_USB_DEVICE(smsc95xx_eth, smsc95xx_eth_id_table);
1080#endif