blob: 81ea0a380285651351c6e6ac7624b7d351253bec [file] [log] [blame]
Simon Glass246c1192011-06-13 16:13:09 -07001/*
2 * Copyright (c) 2011 The Chromium OS Authors.
3 * Copyright (C) 2009 NVIDIA, Corporation
Simon Glass00be89c2014-09-08 13:44:14 -06004 * Copyright (C) 2007-2008 SMSC (Steve Glendinning)
Simon Glass246c1192011-06-13 16:13:09 -07005 *
Wolfgang Denkd79de1d2013-07-08 09:37:19 +02006 * SPDX-License-Identifier: GPL-2.0+
Simon Glass246c1192011-06-13 16:13:09 -07007 */
8
9#include <common.h>
Simon Glassce48e502015-07-07 20:53:38 -060010#include <errno.h>
11#include <malloc.h>
Simon Glass246c1192011-06-13 16:13:09 -070012#include <usb.h>
Simon Glassce48e502015-07-07 20:53:38 -060013#include <asm/unaligned.h>
Simon Glass246c1192011-06-13 16:13:09 -070014#include <linux/mii.h>
15#include "usb_ether.h"
16
17/* SMSC LAN95xx based USB 2.0 Ethernet Devices */
18
Suriyan Ramasamie0f2f8c2013-10-07 20:30:58 -070019/* LED defines */
20#define LED_GPIO_CFG (0x24)
21#define LED_GPIO_CFG_SPD_LED (0x01000000)
22#define LED_GPIO_CFG_LNK_LED (0x00100000)
23#define LED_GPIO_CFG_FDX_LED (0x00010000)
24
Simon Glass246c1192011-06-13 16:13:09 -070025/* Tx command words */
26#define TX_CMD_A_FIRST_SEG_ 0x00002000
27#define TX_CMD_A_LAST_SEG_ 0x00001000
28
29/* Rx status word */
30#define RX_STS_FL_ 0x3FFF0000 /* Frame Length */
31#define RX_STS_ES_ 0x00008000 /* Error Summary */
32
33/* SCSRs */
34#define ID_REV 0x00
35
36#define INT_STS 0x08
37
38#define TX_CFG 0x10
39#define TX_CFG_ON_ 0x00000004
40
41#define HW_CFG 0x14
42#define HW_CFG_BIR_ 0x00001000
43#define HW_CFG_RXDOFF_ 0x00000600
44#define HW_CFG_MEF_ 0x00000020
45#define HW_CFG_BCE_ 0x00000002
46#define HW_CFG_LRST_ 0x00000008
47
48#define PM_CTRL 0x20
49#define PM_CTL_PHY_RST_ 0x00000010
50
51#define AFC_CFG 0x2C
52
53/*
54 * Hi watermark = 15.5Kb (~10 mtu pkts)
55 * low watermark = 3k (~2 mtu pkts)
56 * backpressure duration = ~ 350us
57 * Apply FC on any frame.
58 */
59#define AFC_CFG_DEFAULT 0x00F830A1
60
61#define E2P_CMD 0x30
62#define E2P_CMD_BUSY_ 0x80000000
63#define E2P_CMD_READ_ 0x00000000
64#define E2P_CMD_TIMEOUT_ 0x00000400
65#define E2P_CMD_LOADED_ 0x00000200
66#define E2P_CMD_ADDR_ 0x000001FF
67
68#define E2P_DATA 0x34
69
70#define BURST_CAP 0x38
71
72#define INT_EP_CTL 0x68
73#define INT_EP_CTL_PHY_INT_ 0x00008000
74
75#define BULK_IN_DLY 0x6C
76
77/* MAC CSRs */
78#define MAC_CR 0x100
79#define MAC_CR_MCPAS_ 0x00080000
80#define MAC_CR_PRMS_ 0x00040000
81#define MAC_CR_HPFILT_ 0x00002000
82#define MAC_CR_TXEN_ 0x00000008
83#define MAC_CR_RXEN_ 0x00000004
84
85#define ADDRH 0x104
86
87#define ADDRL 0x108
88
89#define MII_ADDR 0x114
90#define MII_WRITE_ 0x02
91#define MII_BUSY_ 0x01
92#define MII_READ_ 0x00 /* ~of MII Write bit */
93
94#define MII_DATA 0x118
95
96#define FLOW 0x11C
97
98#define VLAN1 0x120
99
100#define COE_CR 0x130
101#define Tx_COE_EN_ 0x00010000
102#define Rx_COE_EN_ 0x00000001
103
104/* Vendor-specific PHY Definitions */
105#define PHY_INT_SRC 29
106
107#define PHY_INT_MASK 30
108#define PHY_INT_MASK_ANEG_COMP_ ((u16)0x0040)
109#define PHY_INT_MASK_LINK_DOWN_ ((u16)0x0010)
110#define PHY_INT_MASK_DEFAULT_ (PHY_INT_MASK_ANEG_COMP_ | \
111 PHY_INT_MASK_LINK_DOWN_)
112
113/* USB Vendor Requests */
114#define USB_VENDOR_REQUEST_WRITE_REGISTER 0xA0
115#define USB_VENDOR_REQUEST_READ_REGISTER 0xA1
116
117/* Some extra defines */
118#define HS_USB_PKT_SIZE 512
119#define FS_USB_PKT_SIZE 64
120#define DEFAULT_HS_BURST_CAP_SIZE (16 * 1024 + 5 * HS_USB_PKT_SIZE)
121#define DEFAULT_FS_BURST_CAP_SIZE (6 * 1024 + 33 * FS_USB_PKT_SIZE)
122#define DEFAULT_BULK_IN_DELAY 0x00002000
123#define MAX_SINGLE_PACKET_SIZE 2048
124#define EEPROM_MAC_OFFSET 0x01
125#define SMSC95XX_INTERNAL_PHY_ID 1
126#define ETH_P_8021Q 0x8100 /* 802.1Q VLAN Extended Header */
127
128/* local defines */
129#define SMSC95XX_BASE_NAME "sms"
130#define USB_CTRL_SET_TIMEOUT 5000
131#define USB_CTRL_GET_TIMEOUT 5000
132#define USB_BULK_SEND_TIMEOUT 5000
133#define USB_BULK_RECV_TIMEOUT 5000
134
Simon Glass4d89e5b2015-07-07 20:53:39 -0600135#define RX_URB_SIZE 2048
Simon Glass246c1192011-06-13 16:13:09 -0700136#define PHY_CONNECT_TIMEOUT 5000
137
138#define TURBO_MODE
139
140/* local vars */
141static int curr_eth_dev; /* index for name of next device detected */
142
Lucas Stach36267c42012-08-22 11:04:57 +0000143/* driver private */
144struct smsc95xx_private {
145 size_t rx_urb_size; /* maximum USB URB size */
146 u32 mac_cr; /* MAC control register value */
147 int have_hwaddr; /* 1 if we have a hardware MAC address */
148};
Simon Glass246c1192011-06-13 16:13:09 -0700149
150/*
151 * Smsc95xx infrastructure commands
152 */
153static int smsc95xx_write_reg(struct ueth_data *dev, u32 index, u32 data)
154{
155 int len;
Ilya Yanok43b56c22012-07-15 04:43:53 +0000156 ALLOC_CACHE_ALIGN_BUFFER(u32, tmpbuf, 1);
Simon Glass246c1192011-06-13 16:13:09 -0700157
158 cpu_to_le32s(&data);
Ilya Yanok43b56c22012-07-15 04:43:53 +0000159 tmpbuf[0] = data;
Simon Glass246c1192011-06-13 16:13:09 -0700160
161 len = usb_control_msg(dev->pusb_dev, usb_sndctrlpipe(dev->pusb_dev, 0),
162 USB_VENDOR_REQUEST_WRITE_REGISTER,
163 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
Ilya Yanok43b56c22012-07-15 04:43:53 +0000164 00, index, tmpbuf, sizeof(data), USB_CTRL_SET_TIMEOUT);
Simon Glass246c1192011-06-13 16:13:09 -0700165 if (len != sizeof(data)) {
166 debug("smsc95xx_write_reg failed: index=%d, data=%d, len=%d",
167 index, data, len);
Simon Glassebe0e5a2015-07-07 20:53:40 -0600168 return -EIO;
Simon Glass246c1192011-06-13 16:13:09 -0700169 }
170 return 0;
171}
172
173static int smsc95xx_read_reg(struct ueth_data *dev, u32 index, u32 *data)
174{
175 int len;
Ilya Yanok43b56c22012-07-15 04:43:53 +0000176 ALLOC_CACHE_ALIGN_BUFFER(u32, tmpbuf, 1);
Simon Glass246c1192011-06-13 16:13:09 -0700177
178 len = usb_control_msg(dev->pusb_dev, usb_rcvctrlpipe(dev->pusb_dev, 0),
179 USB_VENDOR_REQUEST_READ_REGISTER,
180 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
Ilya Yanok43b56c22012-07-15 04:43:53 +0000181 00, index, tmpbuf, sizeof(data), USB_CTRL_GET_TIMEOUT);
182 *data = tmpbuf[0];
Simon Glass246c1192011-06-13 16:13:09 -0700183 if (len != sizeof(data)) {
184 debug("smsc95xx_read_reg failed: index=%d, len=%d",
185 index, len);
Simon Glassebe0e5a2015-07-07 20:53:40 -0600186 return -EIO;
Simon Glass246c1192011-06-13 16:13:09 -0700187 }
188
189 le32_to_cpus(data);
190 return 0;
191}
192
193/* Loop until the read is completed with timeout */
194static int smsc95xx_phy_wait_not_busy(struct ueth_data *dev)
195{
196 unsigned long start_time = get_timer(0);
197 u32 val;
198
199 do {
200 smsc95xx_read_reg(dev, MII_ADDR, &val);
201 if (!(val & MII_BUSY_))
202 return 0;
203 } while (get_timer(start_time) < 1 * 1000 * 1000);
204
Simon Glassebe0e5a2015-07-07 20:53:40 -0600205 return -ETIMEDOUT;
Simon Glass246c1192011-06-13 16:13:09 -0700206}
207
208static int smsc95xx_mdio_read(struct ueth_data *dev, int phy_id, int idx)
209{
210 u32 val, addr;
211
212 /* confirm MII not busy */
213 if (smsc95xx_phy_wait_not_busy(dev)) {
214 debug("MII is busy in smsc95xx_mdio_read\n");
Simon Glassebe0e5a2015-07-07 20:53:40 -0600215 return -ETIMEDOUT;
Simon Glass246c1192011-06-13 16:13:09 -0700216 }
217
218 /* set the address, index & direction (read from PHY) */
219 addr = (phy_id << 11) | (idx << 6) | MII_READ_;
220 smsc95xx_write_reg(dev, MII_ADDR, addr);
221
222 if (smsc95xx_phy_wait_not_busy(dev)) {
223 debug("Timed out reading MII reg %02X\n", idx);
Simon Glassebe0e5a2015-07-07 20:53:40 -0600224 return -ETIMEDOUT;
Simon Glass246c1192011-06-13 16:13:09 -0700225 }
226
227 smsc95xx_read_reg(dev, MII_DATA, &val);
228
229 return (u16)(val & 0xFFFF);
230}
231
232static void smsc95xx_mdio_write(struct ueth_data *dev, int phy_id, int idx,
233 int regval)
234{
235 u32 val, addr;
236
237 /* confirm MII not busy */
238 if (smsc95xx_phy_wait_not_busy(dev)) {
239 debug("MII is busy in smsc95xx_mdio_write\n");
240 return;
241 }
242
243 val = regval;
244 smsc95xx_write_reg(dev, MII_DATA, val);
245
246 /* set the address, index & direction (write to PHY) */
247 addr = (phy_id << 11) | (idx << 6) | MII_WRITE_;
248 smsc95xx_write_reg(dev, MII_ADDR, addr);
249
250 if (smsc95xx_phy_wait_not_busy(dev))
251 debug("Timed out writing MII reg %02X\n", idx);
252}
253
254static int smsc95xx_eeprom_confirm_not_busy(struct ueth_data *dev)
255{
256 unsigned long start_time = get_timer(0);
257 u32 val;
258
259 do {
260 smsc95xx_read_reg(dev, E2P_CMD, &val);
Simon Glass246c1192011-06-13 16:13:09 -0700261 if (!(val & E2P_CMD_BUSY_))
262 return 0;
263 udelay(40);
264 } while (get_timer(start_time) < 1 * 1000 * 1000);
265
266 debug("EEPROM is busy\n");
Simon Glassebe0e5a2015-07-07 20:53:40 -0600267 return -ETIMEDOUT;
Simon Glass246c1192011-06-13 16:13:09 -0700268}
269
270static int smsc95xx_wait_eeprom(struct ueth_data *dev)
271{
272 unsigned long start_time = get_timer(0);
273 u32 val;
274
275 do {
276 smsc95xx_read_reg(dev, E2P_CMD, &val);
277 if (!(val & E2P_CMD_BUSY_) || (val & E2P_CMD_TIMEOUT_))
278 break;
279 udelay(40);
280 } while (get_timer(start_time) < 1 * 1000 * 1000);
281
282 if (val & (E2P_CMD_TIMEOUT_ | E2P_CMD_BUSY_)) {
283 debug("EEPROM read operation timeout\n");
Simon Glassebe0e5a2015-07-07 20:53:40 -0600284 return -ETIMEDOUT;
Simon Glass246c1192011-06-13 16:13:09 -0700285 }
286 return 0;
287}
288
289static int smsc95xx_read_eeprom(struct ueth_data *dev, u32 offset, u32 length,
290 u8 *data)
291{
292 u32 val;
293 int i, ret;
294
295 ret = smsc95xx_eeprom_confirm_not_busy(dev);
296 if (ret)
297 return ret;
298
299 for (i = 0; i < length; i++) {
300 val = E2P_CMD_BUSY_ | E2P_CMD_READ_ | (offset & E2P_CMD_ADDR_);
301 smsc95xx_write_reg(dev, E2P_CMD, val);
302
303 ret = smsc95xx_wait_eeprom(dev);
304 if (ret < 0)
305 return ret;
306
307 smsc95xx_read_reg(dev, E2P_DATA, &val);
308 data[i] = val & 0xFF;
309 offset++;
310 }
311 return 0;
312}
313
314/*
315 * mii_nway_restart - restart NWay (autonegotiation) for this interface
316 *
317 * Returns 0 on success, negative on error.
318 */
319static int mii_nway_restart(struct ueth_data *dev)
320{
321 int bmcr;
322 int r = -1;
323
324 /* if autoneg is off, it's an error */
325 bmcr = smsc95xx_mdio_read(dev, dev->phy_id, MII_BMCR);
326
327 if (bmcr & BMCR_ANENABLE) {
328 bmcr |= BMCR_ANRESTART;
329 smsc95xx_mdio_write(dev, dev->phy_id, MII_BMCR, bmcr);
330 r = 0;
331 }
332 return r;
333}
334
335static int smsc95xx_phy_initialize(struct ueth_data *dev)
336{
337 smsc95xx_mdio_write(dev, dev->phy_id, MII_BMCR, BMCR_RESET);
338 smsc95xx_mdio_write(dev, dev->phy_id, MII_ADVERTISE,
339 ADVERTISE_ALL | ADVERTISE_CSMA | ADVERTISE_PAUSE_CAP |
340 ADVERTISE_PAUSE_ASYM);
341
342 /* read to clear */
343 smsc95xx_mdio_read(dev, dev->phy_id, PHY_INT_SRC);
344
345 smsc95xx_mdio_write(dev, dev->phy_id, PHY_INT_MASK,
346 PHY_INT_MASK_DEFAULT_);
347 mii_nway_restart(dev);
348
349 debug("phy initialised succesfully\n");
350 return 0;
351}
352
353static int smsc95xx_init_mac_address(struct eth_device *eth,
354 struct ueth_data *dev)
355{
356 /* try reading mac address from EEPROM */
357 if (smsc95xx_read_eeprom(dev, EEPROM_MAC_OFFSET, ETH_ALEN,
358 eth->enetaddr) == 0) {
Joe Hershberger8ecdbed2015-04-08 01:41:04 -0500359 if (is_valid_ethaddr(eth->enetaddr)) {
Simon Glass246c1192011-06-13 16:13:09 -0700360 /* eeprom values are valid so use them */
361 debug("MAC address read from EEPROM\n");
362 return 0;
363 }
364 }
365
366 /*
367 * No eeprom, or eeprom values are invalid. Generating a random MAC
368 * address is not safe. Just return an error.
369 */
Simon Glassebe0e5a2015-07-07 20:53:40 -0600370 debug("Invalid MAC address read from EEPROM\n");
371
372 return -ENXIO;
Simon Glass246c1192011-06-13 16:13:09 -0700373}
374
375static int smsc95xx_write_hwaddr(struct eth_device *eth)
376{
377 struct ueth_data *dev = (struct ueth_data *)eth->priv;
Lucas Stach36267c42012-08-22 11:04:57 +0000378 struct smsc95xx_private *priv = dev->dev_priv;
Wolfgang Grandegger0151b682011-11-14 23:19:14 +0000379 u32 addr_lo = __get_unaligned_le32(&eth->enetaddr[0]);
380 u32 addr_hi = __get_unaligned_le16(&eth->enetaddr[4]);
Simon Glass246c1192011-06-13 16:13:09 -0700381 int ret;
382
383 /* set hardware address */
384 debug("** %s()\n", __func__);
Simon Glass246c1192011-06-13 16:13:09 -0700385 ret = smsc95xx_write_reg(dev, ADDRL, addr_lo);
Wolfgang Grandeggerf9af1f82011-11-14 23:19:15 +0000386 if (ret < 0)
Simon Glass246c1192011-06-13 16:13:09 -0700387 return ret;
Simon Glass246c1192011-06-13 16:13:09 -0700388
389 ret = smsc95xx_write_reg(dev, ADDRH, addr_hi);
390 if (ret < 0)
391 return ret;
Wolfgang Grandeggerf9af1f82011-11-14 23:19:15 +0000392
393 debug("MAC %pM\n", eth->enetaddr);
Lucas Stach36267c42012-08-22 11:04:57 +0000394 priv->have_hwaddr = 1;
Simon Glass246c1192011-06-13 16:13:09 -0700395 return 0;
396}
397
398/* Enable or disable Tx & Rx checksum offload engines */
399static int smsc95xx_set_csums(struct ueth_data *dev,
400 int use_tx_csum, int use_rx_csum)
401{
402 u32 read_buf;
403 int ret = smsc95xx_read_reg(dev, COE_CR, &read_buf);
404 if (ret < 0)
405 return ret;
406
407 if (use_tx_csum)
408 read_buf |= Tx_COE_EN_;
409 else
410 read_buf &= ~Tx_COE_EN_;
411
412 if (use_rx_csum)
413 read_buf |= Rx_COE_EN_;
414 else
415 read_buf &= ~Rx_COE_EN_;
416
417 ret = smsc95xx_write_reg(dev, COE_CR, read_buf);
418 if (ret < 0)
419 return ret;
420
421 debug("COE_CR = 0x%08x\n", read_buf);
422 return 0;
423}
424
425static void smsc95xx_set_multicast(struct ueth_data *dev)
426{
Lucas Stach36267c42012-08-22 11:04:57 +0000427 struct smsc95xx_private *priv = dev->dev_priv;
428
Simon Glass246c1192011-06-13 16:13:09 -0700429 /* No multicast in u-boot */
Lucas Stach36267c42012-08-22 11:04:57 +0000430 priv->mac_cr &= ~(MAC_CR_PRMS_ | MAC_CR_MCPAS_ | MAC_CR_HPFILT_);
Simon Glass246c1192011-06-13 16:13:09 -0700431}
432
433/* starts the TX path */
434static void smsc95xx_start_tx_path(struct ueth_data *dev)
435{
Lucas Stach36267c42012-08-22 11:04:57 +0000436 struct smsc95xx_private *priv = dev->dev_priv;
Simon Glass246c1192011-06-13 16:13:09 -0700437 u32 reg_val;
438
439 /* Enable Tx at MAC */
Lucas Stach36267c42012-08-22 11:04:57 +0000440 priv->mac_cr |= MAC_CR_TXEN_;
Simon Glass246c1192011-06-13 16:13:09 -0700441
Lucas Stach36267c42012-08-22 11:04:57 +0000442 smsc95xx_write_reg(dev, MAC_CR, priv->mac_cr);
Simon Glass246c1192011-06-13 16:13:09 -0700443
444 /* Enable Tx at SCSRs */
445 reg_val = TX_CFG_ON_;
446 smsc95xx_write_reg(dev, TX_CFG, reg_val);
447}
448
449/* Starts the Receive path */
450static void smsc95xx_start_rx_path(struct ueth_data *dev)
451{
Lucas Stach36267c42012-08-22 11:04:57 +0000452 struct smsc95xx_private *priv = dev->dev_priv;
453
454 priv->mac_cr |= MAC_CR_RXEN_;
455 smsc95xx_write_reg(dev, MAC_CR, priv->mac_cr);
Simon Glass246c1192011-06-13 16:13:09 -0700456}
457
458/*
459 * Smsc95xx callbacks
460 */
461static int smsc95xx_init(struct eth_device *eth, bd_t *bd)
462{
463 int ret;
464 u32 write_buf;
465 u32 read_buf;
466 u32 burst_cap;
467 int timeout;
468 struct ueth_data *dev = (struct ueth_data *)eth->priv;
Lucas Stach36267c42012-08-22 11:04:57 +0000469 struct smsc95xx_private *priv =
470 (struct smsc95xx_private *)dev->dev_priv;
Simon Glass246c1192011-06-13 16:13:09 -0700471#define TIMEOUT_RESOLUTION 50 /* ms */
472 int link_detected;
473
474 debug("** %s()\n", __func__);
475 dev->phy_id = SMSC95XX_INTERNAL_PHY_ID; /* fixed phy id */
476
477 write_buf = HW_CFG_LRST_;
478 ret = smsc95xx_write_reg(dev, HW_CFG, write_buf);
479 if (ret < 0)
480 return ret;
481
482 timeout = 0;
483 do {
484 ret = smsc95xx_read_reg(dev, HW_CFG, &read_buf);
485 if (ret < 0)
486 return ret;
487 udelay(10 * 1000);
488 timeout++;
489 } while ((read_buf & HW_CFG_LRST_) && (timeout < 100));
490
491 if (timeout >= 100) {
492 debug("timeout waiting for completion of Lite Reset\n");
Simon Glassebe0e5a2015-07-07 20:53:40 -0600493 return -ETIMEDOUT;
Simon Glass246c1192011-06-13 16:13:09 -0700494 }
495
496 write_buf = PM_CTL_PHY_RST_;
497 ret = smsc95xx_write_reg(dev, PM_CTRL, write_buf);
498 if (ret < 0)
499 return ret;
500
501 timeout = 0;
502 do {
503 ret = smsc95xx_read_reg(dev, PM_CTRL, &read_buf);
504 if (ret < 0)
505 return ret;
506 udelay(10 * 1000);
507 timeout++;
508 } while ((read_buf & PM_CTL_PHY_RST_) && (timeout < 100));
509 if (timeout >= 100) {
510 debug("timeout waiting for PHY Reset\n");
Simon Glassebe0e5a2015-07-07 20:53:40 -0600511 return -ETIMEDOUT;
Simon Glass246c1192011-06-13 16:13:09 -0700512 }
Lucas Stach36267c42012-08-22 11:04:57 +0000513 if (!priv->have_hwaddr && smsc95xx_init_mac_address(eth, dev) == 0)
514 priv->have_hwaddr = 1;
515 if (!priv->have_hwaddr) {
Simon Glass246c1192011-06-13 16:13:09 -0700516 puts("Error: SMSC95xx: No MAC address set - set usbethaddr\n");
Simon Glassebe0e5a2015-07-07 20:53:40 -0600517 return -EADDRNOTAVAIL;
Simon Glass246c1192011-06-13 16:13:09 -0700518 }
Simon Glassebe0e5a2015-07-07 20:53:40 -0600519 ret = smsc95xx_write_hwaddr(eth);
520 if (ret < 0)
521 return ret;
Simon Glass246c1192011-06-13 16:13:09 -0700522
523 ret = smsc95xx_read_reg(dev, HW_CFG, &read_buf);
524 if (ret < 0)
525 return ret;
526 debug("Read Value from HW_CFG : 0x%08x\n", read_buf);
527
528 read_buf |= HW_CFG_BIR_;
529 ret = smsc95xx_write_reg(dev, HW_CFG, read_buf);
530 if (ret < 0)
531 return ret;
532
533 ret = smsc95xx_read_reg(dev, HW_CFG, &read_buf);
534 if (ret < 0)
535 return ret;
536 debug("Read Value from HW_CFG after writing "
537 "HW_CFG_BIR_: 0x%08x\n", read_buf);
538
539#ifdef TURBO_MODE
540 if (dev->pusb_dev->speed == USB_SPEED_HIGH) {
541 burst_cap = DEFAULT_HS_BURST_CAP_SIZE / HS_USB_PKT_SIZE;
Lucas Stach36267c42012-08-22 11:04:57 +0000542 priv->rx_urb_size = DEFAULT_HS_BURST_CAP_SIZE;
Simon Glass246c1192011-06-13 16:13:09 -0700543 } else {
544 burst_cap = DEFAULT_FS_BURST_CAP_SIZE / FS_USB_PKT_SIZE;
Lucas Stach36267c42012-08-22 11:04:57 +0000545 priv->rx_urb_size = DEFAULT_FS_BURST_CAP_SIZE;
Simon Glass246c1192011-06-13 16:13:09 -0700546 }
547#else
548 burst_cap = 0;
Lucas Stach36267c42012-08-22 11:04:57 +0000549 priv->rx_urb_size = MAX_SINGLE_PACKET_SIZE;
Simon Glass246c1192011-06-13 16:13:09 -0700550#endif
Lucas Stach36267c42012-08-22 11:04:57 +0000551 debug("rx_urb_size=%ld\n", (ulong)priv->rx_urb_size);
Simon Glass246c1192011-06-13 16:13:09 -0700552
553 ret = smsc95xx_write_reg(dev, BURST_CAP, burst_cap);
554 if (ret < 0)
555 return ret;
556
557 ret = smsc95xx_read_reg(dev, BURST_CAP, &read_buf);
558 if (ret < 0)
559 return ret;
560 debug("Read Value from BURST_CAP after writing: 0x%08x\n", read_buf);
561
562 read_buf = DEFAULT_BULK_IN_DELAY;
563 ret = smsc95xx_write_reg(dev, BULK_IN_DLY, read_buf);
564 if (ret < 0)
565 return ret;
566
567 ret = smsc95xx_read_reg(dev, BULK_IN_DLY, &read_buf);
568 if (ret < 0)
569 return ret;
570 debug("Read Value from BULK_IN_DLY after writing: "
571 "0x%08x\n", read_buf);
572
573 ret = smsc95xx_read_reg(dev, HW_CFG, &read_buf);
574 if (ret < 0)
575 return ret;
576 debug("Read Value from HW_CFG: 0x%08x\n", read_buf);
577
578#ifdef TURBO_MODE
579 read_buf |= (HW_CFG_MEF_ | HW_CFG_BCE_);
580#endif
581 read_buf &= ~HW_CFG_RXDOFF_;
582
583#define NET_IP_ALIGN 0
584 read_buf |= NET_IP_ALIGN << 9;
585
586 ret = smsc95xx_write_reg(dev, HW_CFG, read_buf);
587 if (ret < 0)
588 return ret;
589
590 ret = smsc95xx_read_reg(dev, HW_CFG, &read_buf);
591 if (ret < 0)
592 return ret;
593 debug("Read Value from HW_CFG after writing: 0x%08x\n", read_buf);
594
595 write_buf = 0xFFFFFFFF;
596 ret = smsc95xx_write_reg(dev, INT_STS, write_buf);
597 if (ret < 0)
598 return ret;
599
600 ret = smsc95xx_read_reg(dev, ID_REV, &read_buf);
601 if (ret < 0)
602 return ret;
603 debug("ID_REV = 0x%08x\n", read_buf);
604
Suriyan Ramasamie0f2f8c2013-10-07 20:30:58 -0700605 /* Configure GPIO pins as LED outputs */
606 write_buf = LED_GPIO_CFG_SPD_LED | LED_GPIO_CFG_LNK_LED |
607 LED_GPIO_CFG_FDX_LED;
608 ret = smsc95xx_write_reg(dev, LED_GPIO_CFG, write_buf);
609 if (ret < 0)
610 return ret;
611 debug("LED_GPIO_CFG set\n");
612
Simon Glass246c1192011-06-13 16:13:09 -0700613 /* Init Tx */
614 write_buf = 0;
615 ret = smsc95xx_write_reg(dev, FLOW, write_buf);
616 if (ret < 0)
617 return ret;
618
619 read_buf = AFC_CFG_DEFAULT;
620 ret = smsc95xx_write_reg(dev, AFC_CFG, read_buf);
621 if (ret < 0)
622 return ret;
623
Lucas Stach36267c42012-08-22 11:04:57 +0000624 ret = smsc95xx_read_reg(dev, MAC_CR, &priv->mac_cr);
Simon Glass246c1192011-06-13 16:13:09 -0700625 if (ret < 0)
626 return ret;
627
628 /* Init Rx. Set Vlan */
629 write_buf = (u32)ETH_P_8021Q;
630 ret = smsc95xx_write_reg(dev, VLAN1, write_buf);
631 if (ret < 0)
632 return ret;
633
634 /* Disable checksum offload engines */
635 ret = smsc95xx_set_csums(dev, 0, 0);
636 if (ret < 0) {
637 debug("Failed to set csum offload: %d\n", ret);
638 return ret;
639 }
640 smsc95xx_set_multicast(dev);
641
Simon Glassebe0e5a2015-07-07 20:53:40 -0600642 ret = smsc95xx_phy_initialize(dev);
643 if (ret < 0)
644 return ret;
Simon Glass246c1192011-06-13 16:13:09 -0700645 ret = smsc95xx_read_reg(dev, INT_EP_CTL, &read_buf);
646 if (ret < 0)
647 return ret;
648
649 /* enable PHY interrupts */
650 read_buf |= INT_EP_CTL_PHY_INT_;
651
652 ret = smsc95xx_write_reg(dev, INT_EP_CTL, read_buf);
653 if (ret < 0)
654 return ret;
655
656 smsc95xx_start_tx_path(dev);
657 smsc95xx_start_rx_path(dev);
658
659 timeout = 0;
660 do {
661 link_detected = smsc95xx_mdio_read(dev, dev->phy_id, MII_BMSR)
662 & BMSR_LSTATUS;
663 if (!link_detected) {
664 if (timeout == 0)
665 printf("Waiting for Ethernet connection... ");
666 udelay(TIMEOUT_RESOLUTION * 1000);
667 timeout += TIMEOUT_RESOLUTION;
668 }
669 } while (!link_detected && timeout < PHY_CONNECT_TIMEOUT);
670 if (link_detected) {
671 if (timeout != 0)
672 printf("done.\n");
673 } else {
674 printf("unable to connect.\n");
Simon Glassebe0e5a2015-07-07 20:53:40 -0600675 return -EIO;
Simon Glass246c1192011-06-13 16:13:09 -0700676 }
677 return 0;
678}
679
Anatolij Gustschindf93c8e2012-05-20 12:22:56 +0000680static int smsc95xx_send(struct eth_device *eth, void* packet, int length)
Simon Glass246c1192011-06-13 16:13:09 -0700681{
682 struct ueth_data *dev = (struct ueth_data *)eth->priv;
683 int err;
684 int actual_len;
685 u32 tx_cmd_a;
686 u32 tx_cmd_b;
Ilya Yanok43b56c22012-07-15 04:43:53 +0000687 ALLOC_CACHE_ALIGN_BUFFER(unsigned char, msg,
688 PKTSIZE + sizeof(tx_cmd_a) + sizeof(tx_cmd_b));
Simon Glass246c1192011-06-13 16:13:09 -0700689
690 debug("** %s(), len %d, buf %#x\n", __func__, length, (int)msg);
691 if (length > PKTSIZE)
Simon Glassebe0e5a2015-07-07 20:53:40 -0600692 return -ENOSPC;
Simon Glass246c1192011-06-13 16:13:09 -0700693
694 tx_cmd_a = (u32)length | TX_CMD_A_FIRST_SEG_ | TX_CMD_A_LAST_SEG_;
695 tx_cmd_b = (u32)length;
696 cpu_to_le32s(&tx_cmd_a);
697 cpu_to_le32s(&tx_cmd_b);
698
699 /* prepend cmd_a and cmd_b */
700 memcpy(msg, &tx_cmd_a, sizeof(tx_cmd_a));
701 memcpy(msg + sizeof(tx_cmd_a), &tx_cmd_b, sizeof(tx_cmd_b));
702 memcpy(msg + sizeof(tx_cmd_a) + sizeof(tx_cmd_b), (void *)packet,
703 length);
704 err = usb_bulk_msg(dev->pusb_dev,
705 usb_sndbulkpipe(dev->pusb_dev, dev->ep_out),
706 (void *)msg,
707 length + sizeof(tx_cmd_a) + sizeof(tx_cmd_b),
708 &actual_len,
709 USB_BULK_SEND_TIMEOUT);
710 debug("Tx: len = %u, actual = %u, err = %d\n",
711 length + sizeof(tx_cmd_a) + sizeof(tx_cmd_b),
712 actual_len, err);
713 return err;
714}
715
716static int smsc95xx_recv(struct eth_device *eth)
717{
718 struct ueth_data *dev = (struct ueth_data *)eth->priv;
Simon Glass4d89e5b2015-07-07 20:53:39 -0600719 DEFINE_CACHE_ALIGN_BUFFER(unsigned char, recv_buf, RX_URB_SIZE);
Simon Glass246c1192011-06-13 16:13:09 -0700720 unsigned char *buf_ptr;
721 int err;
722 int actual_len;
723 u32 packet_len;
724 int cur_buf_align;
725
726 debug("** %s()\n", __func__);
727 err = usb_bulk_msg(dev->pusb_dev,
728 usb_rcvbulkpipe(dev->pusb_dev, dev->ep_in),
729 (void *)recv_buf,
Simon Glass4d89e5b2015-07-07 20:53:39 -0600730 RX_URB_SIZE,
Simon Glass246c1192011-06-13 16:13:09 -0700731 &actual_len,
732 USB_BULK_RECV_TIMEOUT);
Simon Glass4d89e5b2015-07-07 20:53:39 -0600733 debug("Rx: len = %u, actual = %u, err = %d\n", RX_URB_SIZE,
Simon Glass246c1192011-06-13 16:13:09 -0700734 actual_len, err);
735 if (err != 0) {
736 debug("Rx: failed to receive\n");
Simon Glassebe0e5a2015-07-07 20:53:40 -0600737 return err;
Simon Glass246c1192011-06-13 16:13:09 -0700738 }
Simon Glass4d89e5b2015-07-07 20:53:39 -0600739 if (actual_len > RX_URB_SIZE) {
Simon Glass246c1192011-06-13 16:13:09 -0700740 debug("Rx: received too many bytes %d\n", actual_len);
Simon Glassebe0e5a2015-07-07 20:53:40 -0600741 return -ENOSPC;
Simon Glass246c1192011-06-13 16:13:09 -0700742 }
743
744 buf_ptr = recv_buf;
745 while (actual_len > 0) {
746 /*
747 * 1st 4 bytes contain the length of the actual data plus error
748 * info. Extract data length.
749 */
750 if (actual_len < sizeof(packet_len)) {
751 debug("Rx: incomplete packet length\n");
Simon Glassebe0e5a2015-07-07 20:53:40 -0600752 return -EIO;
Simon Glass246c1192011-06-13 16:13:09 -0700753 }
754 memcpy(&packet_len, buf_ptr, sizeof(packet_len));
755 le32_to_cpus(&packet_len);
756 if (packet_len & RX_STS_ES_) {
757 debug("Rx: Error header=%#x", packet_len);
Simon Glassebe0e5a2015-07-07 20:53:40 -0600758 return -EIO;
Simon Glass246c1192011-06-13 16:13:09 -0700759 }
760 packet_len = ((packet_len & RX_STS_FL_) >> 16);
761
762 if (packet_len > actual_len - sizeof(packet_len)) {
763 debug("Rx: too large packet: %d\n", packet_len);
Simon Glassebe0e5a2015-07-07 20:53:40 -0600764 return -EIO;
Simon Glass246c1192011-06-13 16:13:09 -0700765 }
766
767 /* Notify net stack */
Joe Hershberger9f09a362015-04-08 01:41:06 -0500768 net_process_received_packet(buf_ptr + sizeof(packet_len),
769 packet_len - 4);
Simon Glass246c1192011-06-13 16:13:09 -0700770
771 /* Adjust for next iteration */
772 actual_len -= sizeof(packet_len) + packet_len;
773 buf_ptr += sizeof(packet_len) + packet_len;
774 cur_buf_align = (int)buf_ptr - (int)recv_buf;
775
776 if (cur_buf_align & 0x03) {
777 int align = 4 - (cur_buf_align & 0x03);
778
779 actual_len -= align;
780 buf_ptr += align;
781 }
782 }
783 return err;
784}
785
786static void smsc95xx_halt(struct eth_device *eth)
787{
788 debug("** %s()\n", __func__);
789}
790
791/*
792 * SMSC probing functions
793 */
794void smsc95xx_eth_before_probe(void)
795{
796 curr_eth_dev = 0;
797}
798
799struct smsc95xx_dongle {
800 unsigned short vendor;
801 unsigned short product;
802};
803
804static const struct smsc95xx_dongle smsc95xx_dongles[] = {
805 { 0x0424, 0xec00 }, /* LAN9512/LAN9514 Ethernet */
806 { 0x0424, 0x9500 }, /* LAN9500 Ethernet */
Lubomir Popov7ca25b62013-04-01 04:50:55 +0000807 { 0x0424, 0x9730 }, /* LAN9730 Ethernet (HSIC) */
Stefan Roese8aa7b042013-07-03 18:34:54 +0200808 { 0x0424, 0x9900 }, /* SMSC9500 USB Ethernet Device (SAL10) */
Ilya Ledvich180bb6a2014-03-12 10:36:31 +0200809 { 0x0424, 0x9e00 }, /* LAN9500A Ethernet */
Simon Glass246c1192011-06-13 16:13:09 -0700810 { 0x0000, 0x0000 } /* END - Do not remove */
811};
812
813/* Probe to see if a new device is actually an SMSC device */
814int smsc95xx_eth_probe(struct usb_device *dev, unsigned int ifnum,
815 struct ueth_data *ss)
816{
817 struct usb_interface *iface;
818 struct usb_interface_descriptor *iface_desc;
819 int i;
820
821 /* let's examine the device now */
822 iface = &dev->config.if_desc[ifnum];
823 iface_desc = &dev->config.if_desc[ifnum].desc;
824
825 for (i = 0; smsc95xx_dongles[i].vendor != 0; i++) {
826 if (dev->descriptor.idVendor == smsc95xx_dongles[i].vendor &&
827 dev->descriptor.idProduct == smsc95xx_dongles[i].product)
828 /* Found a supported dongle */
829 break;
830 }
831 if (smsc95xx_dongles[i].vendor == 0)
832 return 0;
833
834 /* At this point, we know we've got a live one */
835 debug("\n\nUSB Ethernet device detected\n");
836 memset(ss, '\0', sizeof(struct ueth_data));
837
838 /* Initialize the ueth_data structure with some useful info */
839 ss->ifnum = ifnum;
840 ss->pusb_dev = dev;
841 ss->subclass = iface_desc->bInterfaceSubClass;
842 ss->protocol = iface_desc->bInterfaceProtocol;
843
844 /*
845 * We are expecting a minimum of 3 endpoints - in, out (bulk), and int.
846 * We will ignore any others.
847 */
848 for (i = 0; i < iface_desc->bNumEndpoints; i++) {
849 /* is it an BULK endpoint? */
850 if ((iface->ep_desc[i].bmAttributes &
851 USB_ENDPOINT_XFERTYPE_MASK) == USB_ENDPOINT_XFER_BULK) {
852 if (iface->ep_desc[i].bEndpointAddress & USB_DIR_IN)
853 ss->ep_in =
854 iface->ep_desc[i].bEndpointAddress &
855 USB_ENDPOINT_NUMBER_MASK;
856 else
857 ss->ep_out =
858 iface->ep_desc[i].bEndpointAddress &
859 USB_ENDPOINT_NUMBER_MASK;
860 }
861
862 /* is it an interrupt endpoint? */
863 if ((iface->ep_desc[i].bmAttributes &
864 USB_ENDPOINT_XFERTYPE_MASK) == USB_ENDPOINT_XFER_INT) {
865 ss->ep_int = iface->ep_desc[i].bEndpointAddress &
866 USB_ENDPOINT_NUMBER_MASK;
867 ss->irqinterval = iface->ep_desc[i].bInterval;
868 }
869 }
870 debug("Endpoints In %d Out %d Int %d\n",
871 ss->ep_in, ss->ep_out, ss->ep_int);
872
873 /* Do some basic sanity checks, and bail if we find a problem */
874 if (usb_set_interface(dev, iface_desc->bInterfaceNumber, 0) ||
875 !ss->ep_in || !ss->ep_out || !ss->ep_int) {
876 debug("Problems with device\n");
877 return 0;
878 }
879 dev->privptr = (void *)ss;
Lucas Stach36267c42012-08-22 11:04:57 +0000880
881 /* alloc driver private */
882 ss->dev_priv = calloc(1, sizeof(struct smsc95xx_private));
883 if (!ss->dev_priv)
884 return 0;
885
Simon Glass246c1192011-06-13 16:13:09 -0700886 return 1;
887}
888
889int smsc95xx_eth_get_info(struct usb_device *dev, struct ueth_data *ss,
890 struct eth_device *eth)
891{
892 debug("** %s()\n", __func__);
893 if (!eth) {
894 debug("%s: missing parameter.\n", __func__);
895 return 0;
896 }
897 sprintf(eth->name, "%s%d", SMSC95XX_BASE_NAME, curr_eth_dev++);
898 eth->init = smsc95xx_init;
899 eth->send = smsc95xx_send;
900 eth->recv = smsc95xx_recv;
901 eth->halt = smsc95xx_halt;
902 eth->write_hwaddr = smsc95xx_write_hwaddr;
903 eth->priv = ss;
904 return 1;
905}