blob: c62a8c112eb85d77cd0e0b043905fd6dc9f21c63 [file] [log] [blame]
Simon Glass291391b2011-06-13 16:13:09 -07001/*
2 * Copyright (c) 2011 The Chromium OS Authors.
3 * Copyright (C) 2009 NVIDIA, Corporation
4 * See file CREDITS for list of people who contributed to this
5 * project.
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License as
9 * published by the Free Software Foundation; either version 2 of
10 * the License, or (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
20 * MA 02111-1307 USA
21 */
22
Wolfgang Grandegger50d89f52011-11-14 23:19:14 +000023#include <asm/unaligned.h>
Simon Glass291391b2011-06-13 16:13:09 -070024#include <common.h>
25#include <usb.h>
26#include <linux/mii.h>
27#include "usb_ether.h"
28
29/* SMSC LAN95xx based USB 2.0 Ethernet Devices */
30
31/* Tx command words */
32#define TX_CMD_A_FIRST_SEG_ 0x00002000
33#define TX_CMD_A_LAST_SEG_ 0x00001000
34
35/* Rx status word */
36#define RX_STS_FL_ 0x3FFF0000 /* Frame Length */
37#define RX_STS_ES_ 0x00008000 /* Error Summary */
38
39/* SCSRs */
40#define ID_REV 0x00
41
42#define INT_STS 0x08
43
44#define TX_CFG 0x10
45#define TX_CFG_ON_ 0x00000004
46
47#define HW_CFG 0x14
48#define HW_CFG_BIR_ 0x00001000
49#define HW_CFG_RXDOFF_ 0x00000600
50#define HW_CFG_MEF_ 0x00000020
51#define HW_CFG_BCE_ 0x00000002
52#define HW_CFG_LRST_ 0x00000008
53
54#define PM_CTRL 0x20
55#define PM_CTL_PHY_RST_ 0x00000010
56
57#define AFC_CFG 0x2C
58
59/*
60 * Hi watermark = 15.5Kb (~10 mtu pkts)
61 * low watermark = 3k (~2 mtu pkts)
62 * backpressure duration = ~ 350us
63 * Apply FC on any frame.
64 */
65#define AFC_CFG_DEFAULT 0x00F830A1
66
67#define E2P_CMD 0x30
68#define E2P_CMD_BUSY_ 0x80000000
69#define E2P_CMD_READ_ 0x00000000
70#define E2P_CMD_TIMEOUT_ 0x00000400
71#define E2P_CMD_LOADED_ 0x00000200
72#define E2P_CMD_ADDR_ 0x000001FF
73
74#define E2P_DATA 0x34
75
76#define BURST_CAP 0x38
77
78#define INT_EP_CTL 0x68
79#define INT_EP_CTL_PHY_INT_ 0x00008000
80
81#define BULK_IN_DLY 0x6C
82
83/* MAC CSRs */
84#define MAC_CR 0x100
85#define MAC_CR_MCPAS_ 0x00080000
86#define MAC_CR_PRMS_ 0x00040000
87#define MAC_CR_HPFILT_ 0x00002000
88#define MAC_CR_TXEN_ 0x00000008
89#define MAC_CR_RXEN_ 0x00000004
90
91#define ADDRH 0x104
92
93#define ADDRL 0x108
94
95#define MII_ADDR 0x114
96#define MII_WRITE_ 0x02
97#define MII_BUSY_ 0x01
98#define MII_READ_ 0x00 /* ~of MII Write bit */
99
100#define MII_DATA 0x118
101
102#define FLOW 0x11C
103
104#define VLAN1 0x120
105
106#define COE_CR 0x130
107#define Tx_COE_EN_ 0x00010000
108#define Rx_COE_EN_ 0x00000001
109
110/* Vendor-specific PHY Definitions */
111#define PHY_INT_SRC 29
112
113#define PHY_INT_MASK 30
114#define PHY_INT_MASK_ANEG_COMP_ ((u16)0x0040)
115#define PHY_INT_MASK_LINK_DOWN_ ((u16)0x0010)
116#define PHY_INT_MASK_DEFAULT_ (PHY_INT_MASK_ANEG_COMP_ | \
117 PHY_INT_MASK_LINK_DOWN_)
118
119/* USB Vendor Requests */
120#define USB_VENDOR_REQUEST_WRITE_REGISTER 0xA0
121#define USB_VENDOR_REQUEST_READ_REGISTER 0xA1
122
123/* Some extra defines */
124#define HS_USB_PKT_SIZE 512
125#define FS_USB_PKT_SIZE 64
126#define DEFAULT_HS_BURST_CAP_SIZE (16 * 1024 + 5 * HS_USB_PKT_SIZE)
127#define DEFAULT_FS_BURST_CAP_SIZE (6 * 1024 + 33 * FS_USB_PKT_SIZE)
128#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
141#define AX_RX_URB_SIZE 2048
142#define PHY_CONNECT_TIMEOUT 5000
143
144#define TURBO_MODE
145
146/* local vars */
147static int curr_eth_dev; /* index for name of next device detected */
148
149
150/*
151 * Smsc95xx infrastructure commands
152 */
153static int smsc95xx_write_reg(struct ueth_data *dev, u32 index, u32 data)
154{
155 int len;
Ilya Yanoke3b31c82012-07-15 04:43:53 +0000156 ALLOC_CACHE_ALIGN_BUFFER(u32, tmpbuf, 1);
Simon Glass291391b2011-06-13 16:13:09 -0700157
158 cpu_to_le32s(&data);
Ilya Yanoke3b31c82012-07-15 04:43:53 +0000159 tmpbuf[0] = data;
Simon Glass291391b2011-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 Yanoke3b31c82012-07-15 04:43:53 +0000164 00, index, tmpbuf, sizeof(data), USB_CTRL_SET_TIMEOUT);
Simon Glass291391b2011-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);
168 return -1;
169 }
170 return 0;
171}
172
173static int smsc95xx_read_reg(struct ueth_data *dev, u32 index, u32 *data)
174{
175 int len;
Ilya Yanoke3b31c82012-07-15 04:43:53 +0000176 ALLOC_CACHE_ALIGN_BUFFER(u32, tmpbuf, 1);
Simon Glass291391b2011-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 Yanoke3b31c82012-07-15 04:43:53 +0000181 00, index, tmpbuf, sizeof(data), USB_CTRL_GET_TIMEOUT);
182 *data = tmpbuf[0];
Simon Glass291391b2011-06-13 16:13:09 -0700183 if (len != sizeof(data)) {
184 debug("smsc95xx_read_reg failed: index=%d, len=%d",
185 index, len);
186 return -1;
187 }
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
205 return -1;
206}
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");
215 return -1;
216 }
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);
224 return -1;
225 }
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);
261 if (!(val & E2P_CMD_LOADED_)) {
262 debug("No EEPROM present\n");
263 return -1;
264 }
265 if (!(val & E2P_CMD_BUSY_))
266 return 0;
267 udelay(40);
268 } while (get_timer(start_time) < 1 * 1000 * 1000);
269
270 debug("EEPROM is busy\n");
271 return -1;
272}
273
274static int smsc95xx_wait_eeprom(struct ueth_data *dev)
275{
276 unsigned long start_time = get_timer(0);
277 u32 val;
278
279 do {
280 smsc95xx_read_reg(dev, E2P_CMD, &val);
281 if (!(val & E2P_CMD_BUSY_) || (val & E2P_CMD_TIMEOUT_))
282 break;
283 udelay(40);
284 } while (get_timer(start_time) < 1 * 1000 * 1000);
285
286 if (val & (E2P_CMD_TIMEOUT_ | E2P_CMD_BUSY_)) {
287 debug("EEPROM read operation timeout\n");
288 return -1;
289 }
290 return 0;
291}
292
293static int smsc95xx_read_eeprom(struct ueth_data *dev, u32 offset, u32 length,
294 u8 *data)
295{
296 u32 val;
297 int i, ret;
298
299 ret = smsc95xx_eeprom_confirm_not_busy(dev);
300 if (ret)
301 return ret;
302
303 for (i = 0; i < length; i++) {
304 val = E2P_CMD_BUSY_ | E2P_CMD_READ_ | (offset & E2P_CMD_ADDR_);
305 smsc95xx_write_reg(dev, E2P_CMD, val);
306
307 ret = smsc95xx_wait_eeprom(dev);
308 if (ret < 0)
309 return ret;
310
311 smsc95xx_read_reg(dev, E2P_DATA, &val);
312 data[i] = val & 0xFF;
313 offset++;
314 }
315 return 0;
316}
317
318/*
319 * mii_nway_restart - restart NWay (autonegotiation) for this interface
320 *
321 * Returns 0 on success, negative on error.
322 */
323static int mii_nway_restart(struct ueth_data *dev)
324{
325 int bmcr;
326 int r = -1;
327
328 /* if autoneg is off, it's an error */
329 bmcr = smsc95xx_mdio_read(dev, dev->phy_id, MII_BMCR);
330
331 if (bmcr & BMCR_ANENABLE) {
332 bmcr |= BMCR_ANRESTART;
333 smsc95xx_mdio_write(dev, dev->phy_id, MII_BMCR, bmcr);
334 r = 0;
335 }
336 return r;
337}
338
339static int smsc95xx_phy_initialize(struct ueth_data *dev)
340{
341 smsc95xx_mdio_write(dev, dev->phy_id, MII_BMCR, BMCR_RESET);
342 smsc95xx_mdio_write(dev, dev->phy_id, MII_ADVERTISE,
343 ADVERTISE_ALL | ADVERTISE_CSMA | ADVERTISE_PAUSE_CAP |
344 ADVERTISE_PAUSE_ASYM);
345
346 /* read to clear */
347 smsc95xx_mdio_read(dev, dev->phy_id, PHY_INT_SRC);
348
349 smsc95xx_mdio_write(dev, dev->phy_id, PHY_INT_MASK,
350 PHY_INT_MASK_DEFAULT_);
351 mii_nway_restart(dev);
352
353 debug("phy initialised succesfully\n");
354 return 0;
355}
356
357static int smsc95xx_init_mac_address(struct eth_device *eth,
358 struct ueth_data *dev)
359{
360 /* try reading mac address from EEPROM */
361 if (smsc95xx_read_eeprom(dev, EEPROM_MAC_OFFSET, ETH_ALEN,
362 eth->enetaddr) == 0) {
363 if (is_valid_ether_addr(eth->enetaddr)) {
364 /* eeprom values are valid so use them */
365 debug("MAC address read from EEPROM\n");
366 return 0;
367 }
368 }
369
370 /*
371 * No eeprom, or eeprom values are invalid. Generating a random MAC
372 * address is not safe. Just return an error.
373 */
374 return -1;
375}
376
377static int smsc95xx_write_hwaddr(struct eth_device *eth)
378{
379 struct ueth_data *dev = (struct ueth_data *)eth->priv;
Wolfgang Grandegger50d89f52011-11-14 23:19:14 +0000380 u32 addr_lo = __get_unaligned_le32(&eth->enetaddr[0]);
381 u32 addr_hi = __get_unaligned_le16(&eth->enetaddr[4]);
Simon Glass291391b2011-06-13 16:13:09 -0700382 int ret;
383
384 /* set hardware address */
385 debug("** %s()\n", __func__);
Simon Glass291391b2011-06-13 16:13:09 -0700386 ret = smsc95xx_write_reg(dev, ADDRL, addr_lo);
Wolfgang Grandegger0d9679e2011-11-14 23:19:15 +0000387 if (ret < 0)
Simon Glass291391b2011-06-13 16:13:09 -0700388 return ret;
Simon Glass291391b2011-06-13 16:13:09 -0700389
390 ret = smsc95xx_write_reg(dev, ADDRH, addr_hi);
391 if (ret < 0)
392 return ret;
Wolfgang Grandegger0d9679e2011-11-14 23:19:15 +0000393
394 debug("MAC %pM\n", eth->enetaddr);
Simon Glass291391b2011-06-13 16:13:09 -0700395 dev->have_hwaddr = 1;
396 return 0;
397}
398
399/* Enable or disable Tx & Rx checksum offload engines */
400static int smsc95xx_set_csums(struct ueth_data *dev,
401 int use_tx_csum, int use_rx_csum)
402{
403 u32 read_buf;
404 int ret = smsc95xx_read_reg(dev, COE_CR, &read_buf);
405 if (ret < 0)
406 return ret;
407
408 if (use_tx_csum)
409 read_buf |= Tx_COE_EN_;
410 else
411 read_buf &= ~Tx_COE_EN_;
412
413 if (use_rx_csum)
414 read_buf |= Rx_COE_EN_;
415 else
416 read_buf &= ~Rx_COE_EN_;
417
418 ret = smsc95xx_write_reg(dev, COE_CR, read_buf);
419 if (ret < 0)
420 return ret;
421
422 debug("COE_CR = 0x%08x\n", read_buf);
423 return 0;
424}
425
426static void smsc95xx_set_multicast(struct ueth_data *dev)
427{
428 /* No multicast in u-boot */
429 dev->mac_cr &= ~(MAC_CR_PRMS_ | MAC_CR_MCPAS_ | MAC_CR_HPFILT_);
430}
431
432/* starts the TX path */
433static void smsc95xx_start_tx_path(struct ueth_data *dev)
434{
435 u32 reg_val;
436
437 /* Enable Tx at MAC */
438 dev->mac_cr |= MAC_CR_TXEN_;
439
440 smsc95xx_write_reg(dev, MAC_CR, dev->mac_cr);
441
442 /* Enable Tx at SCSRs */
443 reg_val = TX_CFG_ON_;
444 smsc95xx_write_reg(dev, TX_CFG, reg_val);
445}
446
447/* Starts the Receive path */
448static void smsc95xx_start_rx_path(struct ueth_data *dev)
449{
450 dev->mac_cr |= MAC_CR_RXEN_;
451 smsc95xx_write_reg(dev, MAC_CR, dev->mac_cr);
452}
453
454/*
455 * Smsc95xx callbacks
456 */
457static int smsc95xx_init(struct eth_device *eth, bd_t *bd)
458{
459 int ret;
460 u32 write_buf;
461 u32 read_buf;
462 u32 burst_cap;
463 int timeout;
464 struct ueth_data *dev = (struct ueth_data *)eth->priv;
465#define TIMEOUT_RESOLUTION 50 /* ms */
466 int link_detected;
467
468 debug("** %s()\n", __func__);
469 dev->phy_id = SMSC95XX_INTERNAL_PHY_ID; /* fixed phy id */
470
471 write_buf = HW_CFG_LRST_;
472 ret = smsc95xx_write_reg(dev, HW_CFG, write_buf);
473 if (ret < 0)
474 return ret;
475
476 timeout = 0;
477 do {
478 ret = smsc95xx_read_reg(dev, HW_CFG, &read_buf);
479 if (ret < 0)
480 return ret;
481 udelay(10 * 1000);
482 timeout++;
483 } while ((read_buf & HW_CFG_LRST_) && (timeout < 100));
484
485 if (timeout >= 100) {
486 debug("timeout waiting for completion of Lite Reset\n");
487 return -1;
488 }
489
490 write_buf = PM_CTL_PHY_RST_;
491 ret = smsc95xx_write_reg(dev, PM_CTRL, write_buf);
492 if (ret < 0)
493 return ret;
494
495 timeout = 0;
496 do {
497 ret = smsc95xx_read_reg(dev, PM_CTRL, &read_buf);
498 if (ret < 0)
499 return ret;
500 udelay(10 * 1000);
501 timeout++;
502 } while ((read_buf & PM_CTL_PHY_RST_) && (timeout < 100));
503 if (timeout >= 100) {
504 debug("timeout waiting for PHY Reset\n");
505 return -1;
506 }
507 if (!dev->have_hwaddr && smsc95xx_init_mac_address(eth, dev) == 0)
508 dev->have_hwaddr = 1;
509 if (!dev->have_hwaddr) {
510 puts("Error: SMSC95xx: No MAC address set - set usbethaddr\n");
511 return -1;
512 }
513 if (smsc95xx_write_hwaddr(eth) < 0)
514 return -1;
515
516 ret = smsc95xx_read_reg(dev, HW_CFG, &read_buf);
517 if (ret < 0)
518 return ret;
519 debug("Read Value from HW_CFG : 0x%08x\n", read_buf);
520
521 read_buf |= HW_CFG_BIR_;
522 ret = smsc95xx_write_reg(dev, HW_CFG, read_buf);
523 if (ret < 0)
524 return ret;
525
526 ret = smsc95xx_read_reg(dev, HW_CFG, &read_buf);
527 if (ret < 0)
528 return ret;
529 debug("Read Value from HW_CFG after writing "
530 "HW_CFG_BIR_: 0x%08x\n", read_buf);
531
532#ifdef TURBO_MODE
533 if (dev->pusb_dev->speed == USB_SPEED_HIGH) {
534 burst_cap = DEFAULT_HS_BURST_CAP_SIZE / HS_USB_PKT_SIZE;
535 dev->rx_urb_size = DEFAULT_HS_BURST_CAP_SIZE;
536 } else {
537 burst_cap = DEFAULT_FS_BURST_CAP_SIZE / FS_USB_PKT_SIZE;
538 dev->rx_urb_size = DEFAULT_FS_BURST_CAP_SIZE;
539 }
540#else
541 burst_cap = 0;
542 dev->rx_urb_size = MAX_SINGLE_PACKET_SIZE;
543#endif
544 debug("rx_urb_size=%ld\n", (ulong)dev->rx_urb_size);
545
546 ret = smsc95xx_write_reg(dev, BURST_CAP, burst_cap);
547 if (ret < 0)
548 return ret;
549
550 ret = smsc95xx_read_reg(dev, BURST_CAP, &read_buf);
551 if (ret < 0)
552 return ret;
553 debug("Read Value from BURST_CAP after writing: 0x%08x\n", read_buf);
554
555 read_buf = DEFAULT_BULK_IN_DELAY;
556 ret = smsc95xx_write_reg(dev, BULK_IN_DLY, read_buf);
557 if (ret < 0)
558 return ret;
559
560 ret = smsc95xx_read_reg(dev, BULK_IN_DLY, &read_buf);
561 if (ret < 0)
562 return ret;
563 debug("Read Value from BULK_IN_DLY after writing: "
564 "0x%08x\n", read_buf);
565
566 ret = smsc95xx_read_reg(dev, HW_CFG, &read_buf);
567 if (ret < 0)
568 return ret;
569 debug("Read Value from HW_CFG: 0x%08x\n", read_buf);
570
571#ifdef TURBO_MODE
572 read_buf |= (HW_CFG_MEF_ | HW_CFG_BCE_);
573#endif
574 read_buf &= ~HW_CFG_RXDOFF_;
575
576#define NET_IP_ALIGN 0
577 read_buf |= NET_IP_ALIGN << 9;
578
579 ret = smsc95xx_write_reg(dev, HW_CFG, read_buf);
580 if (ret < 0)
581 return ret;
582
583 ret = smsc95xx_read_reg(dev, HW_CFG, &read_buf);
584 if (ret < 0)
585 return ret;
586 debug("Read Value from HW_CFG after writing: 0x%08x\n", read_buf);
587
588 write_buf = 0xFFFFFFFF;
589 ret = smsc95xx_write_reg(dev, INT_STS, write_buf);
590 if (ret < 0)
591 return ret;
592
593 ret = smsc95xx_read_reg(dev, ID_REV, &read_buf);
594 if (ret < 0)
595 return ret;
596 debug("ID_REV = 0x%08x\n", read_buf);
597
598 /* Init Tx */
599 write_buf = 0;
600 ret = smsc95xx_write_reg(dev, FLOW, write_buf);
601 if (ret < 0)
602 return ret;
603
604 read_buf = AFC_CFG_DEFAULT;
605 ret = smsc95xx_write_reg(dev, AFC_CFG, read_buf);
606 if (ret < 0)
607 return ret;
608
609 ret = smsc95xx_read_reg(dev, MAC_CR, &dev->mac_cr);
610 if (ret < 0)
611 return ret;
612
613 /* Init Rx. Set Vlan */
614 write_buf = (u32)ETH_P_8021Q;
615 ret = smsc95xx_write_reg(dev, VLAN1, write_buf);
616 if (ret < 0)
617 return ret;
618
619 /* Disable checksum offload engines */
620 ret = smsc95xx_set_csums(dev, 0, 0);
621 if (ret < 0) {
622 debug("Failed to set csum offload: %d\n", ret);
623 return ret;
624 }
625 smsc95xx_set_multicast(dev);
626
627 if (smsc95xx_phy_initialize(dev) < 0)
628 return -1;
629 ret = smsc95xx_read_reg(dev, INT_EP_CTL, &read_buf);
630 if (ret < 0)
631 return ret;
632
633 /* enable PHY interrupts */
634 read_buf |= INT_EP_CTL_PHY_INT_;
635
636 ret = smsc95xx_write_reg(dev, INT_EP_CTL, read_buf);
637 if (ret < 0)
638 return ret;
639
640 smsc95xx_start_tx_path(dev);
641 smsc95xx_start_rx_path(dev);
642
643 timeout = 0;
644 do {
645 link_detected = smsc95xx_mdio_read(dev, dev->phy_id, MII_BMSR)
646 & BMSR_LSTATUS;
647 if (!link_detected) {
648 if (timeout == 0)
649 printf("Waiting for Ethernet connection... ");
650 udelay(TIMEOUT_RESOLUTION * 1000);
651 timeout += TIMEOUT_RESOLUTION;
652 }
653 } while (!link_detected && timeout < PHY_CONNECT_TIMEOUT);
654 if (link_detected) {
655 if (timeout != 0)
656 printf("done.\n");
657 } else {
658 printf("unable to connect.\n");
659 return -1;
660 }
661 return 0;
662}
663
Anatolij Gustschin92ec2102012-05-20 12:22:56 +0000664static int smsc95xx_send(struct eth_device *eth, void* packet, int length)
Simon Glass291391b2011-06-13 16:13:09 -0700665{
666 struct ueth_data *dev = (struct ueth_data *)eth->priv;
667 int err;
668 int actual_len;
669 u32 tx_cmd_a;
670 u32 tx_cmd_b;
Ilya Yanoke3b31c82012-07-15 04:43:53 +0000671 ALLOC_CACHE_ALIGN_BUFFER(unsigned char, msg,
672 PKTSIZE + sizeof(tx_cmd_a) + sizeof(tx_cmd_b));
Simon Glass291391b2011-06-13 16:13:09 -0700673
674 debug("** %s(), len %d, buf %#x\n", __func__, length, (int)msg);
675 if (length > PKTSIZE)
676 return -1;
677
678 tx_cmd_a = (u32)length | TX_CMD_A_FIRST_SEG_ | TX_CMD_A_LAST_SEG_;
679 tx_cmd_b = (u32)length;
680 cpu_to_le32s(&tx_cmd_a);
681 cpu_to_le32s(&tx_cmd_b);
682
683 /* prepend cmd_a and cmd_b */
684 memcpy(msg, &tx_cmd_a, sizeof(tx_cmd_a));
685 memcpy(msg + sizeof(tx_cmd_a), &tx_cmd_b, sizeof(tx_cmd_b));
686 memcpy(msg + sizeof(tx_cmd_a) + sizeof(tx_cmd_b), (void *)packet,
687 length);
688 err = usb_bulk_msg(dev->pusb_dev,
689 usb_sndbulkpipe(dev->pusb_dev, dev->ep_out),
690 (void *)msg,
691 length + sizeof(tx_cmd_a) + sizeof(tx_cmd_b),
692 &actual_len,
693 USB_BULK_SEND_TIMEOUT);
694 debug("Tx: len = %u, actual = %u, err = %d\n",
695 length + sizeof(tx_cmd_a) + sizeof(tx_cmd_b),
696 actual_len, err);
697 return err;
698}
699
700static int smsc95xx_recv(struct eth_device *eth)
701{
702 struct ueth_data *dev = (struct ueth_data *)eth->priv;
Ilya Yanoke3b31c82012-07-15 04:43:53 +0000703 DEFINE_CACHE_ALIGN_BUFFER(unsigned char, recv_buf, AX_RX_URB_SIZE);
Simon Glass291391b2011-06-13 16:13:09 -0700704 unsigned char *buf_ptr;
705 int err;
706 int actual_len;
707 u32 packet_len;
708 int cur_buf_align;
709
710 debug("** %s()\n", __func__);
711 err = usb_bulk_msg(dev->pusb_dev,
712 usb_rcvbulkpipe(dev->pusb_dev, dev->ep_in),
713 (void *)recv_buf,
714 AX_RX_URB_SIZE,
715 &actual_len,
716 USB_BULK_RECV_TIMEOUT);
717 debug("Rx: len = %u, actual = %u, err = %d\n", AX_RX_URB_SIZE,
718 actual_len, err);
719 if (err != 0) {
720 debug("Rx: failed to receive\n");
721 return -1;
722 }
723 if (actual_len > AX_RX_URB_SIZE) {
724 debug("Rx: received too many bytes %d\n", actual_len);
725 return -1;
726 }
727
728 buf_ptr = recv_buf;
729 while (actual_len > 0) {
730 /*
731 * 1st 4 bytes contain the length of the actual data plus error
732 * info. Extract data length.
733 */
734 if (actual_len < sizeof(packet_len)) {
735 debug("Rx: incomplete packet length\n");
736 return -1;
737 }
738 memcpy(&packet_len, buf_ptr, sizeof(packet_len));
739 le32_to_cpus(&packet_len);
740 if (packet_len & RX_STS_ES_) {
741 debug("Rx: Error header=%#x", packet_len);
742 return -1;
743 }
744 packet_len = ((packet_len & RX_STS_FL_) >> 16);
745
746 if (packet_len > actual_len - sizeof(packet_len)) {
747 debug("Rx: too large packet: %d\n", packet_len);
748 return -1;
749 }
750
751 /* Notify net stack */
752 NetReceive(buf_ptr + sizeof(packet_len), packet_len - 4);
753
754 /* Adjust for next iteration */
755 actual_len -= sizeof(packet_len) + packet_len;
756 buf_ptr += sizeof(packet_len) + packet_len;
757 cur_buf_align = (int)buf_ptr - (int)recv_buf;
758
759 if (cur_buf_align & 0x03) {
760 int align = 4 - (cur_buf_align & 0x03);
761
762 actual_len -= align;
763 buf_ptr += align;
764 }
765 }
766 return err;
767}
768
769static void smsc95xx_halt(struct eth_device *eth)
770{
771 debug("** %s()\n", __func__);
772}
773
774/*
775 * SMSC probing functions
776 */
777void smsc95xx_eth_before_probe(void)
778{
779 curr_eth_dev = 0;
780}
781
782struct smsc95xx_dongle {
783 unsigned short vendor;
784 unsigned short product;
785};
786
787static const struct smsc95xx_dongle smsc95xx_dongles[] = {
788 { 0x0424, 0xec00 }, /* LAN9512/LAN9514 Ethernet */
789 { 0x0424, 0x9500 }, /* LAN9500 Ethernet */
790 { 0x0000, 0x0000 } /* END - Do not remove */
791};
792
793/* Probe to see if a new device is actually an SMSC device */
794int smsc95xx_eth_probe(struct usb_device *dev, unsigned int ifnum,
795 struct ueth_data *ss)
796{
797 struct usb_interface *iface;
798 struct usb_interface_descriptor *iface_desc;
799 int i;
800
801 /* let's examine the device now */
802 iface = &dev->config.if_desc[ifnum];
803 iface_desc = &dev->config.if_desc[ifnum].desc;
804
805 for (i = 0; smsc95xx_dongles[i].vendor != 0; i++) {
806 if (dev->descriptor.idVendor == smsc95xx_dongles[i].vendor &&
807 dev->descriptor.idProduct == smsc95xx_dongles[i].product)
808 /* Found a supported dongle */
809 break;
810 }
811 if (smsc95xx_dongles[i].vendor == 0)
812 return 0;
813
814 /* At this point, we know we've got a live one */
815 debug("\n\nUSB Ethernet device detected\n");
816 memset(ss, '\0', sizeof(struct ueth_data));
817
818 /* Initialize the ueth_data structure with some useful info */
819 ss->ifnum = ifnum;
820 ss->pusb_dev = dev;
821 ss->subclass = iface_desc->bInterfaceSubClass;
822 ss->protocol = iface_desc->bInterfaceProtocol;
823
824 /*
825 * We are expecting a minimum of 3 endpoints - in, out (bulk), and int.
826 * We will ignore any others.
827 */
828 for (i = 0; i < iface_desc->bNumEndpoints; i++) {
829 /* is it an BULK endpoint? */
830 if ((iface->ep_desc[i].bmAttributes &
831 USB_ENDPOINT_XFERTYPE_MASK) == USB_ENDPOINT_XFER_BULK) {
832 if (iface->ep_desc[i].bEndpointAddress & USB_DIR_IN)
833 ss->ep_in =
834 iface->ep_desc[i].bEndpointAddress &
835 USB_ENDPOINT_NUMBER_MASK;
836 else
837 ss->ep_out =
838 iface->ep_desc[i].bEndpointAddress &
839 USB_ENDPOINT_NUMBER_MASK;
840 }
841
842 /* is it an interrupt endpoint? */
843 if ((iface->ep_desc[i].bmAttributes &
844 USB_ENDPOINT_XFERTYPE_MASK) == USB_ENDPOINT_XFER_INT) {
845 ss->ep_int = iface->ep_desc[i].bEndpointAddress &
846 USB_ENDPOINT_NUMBER_MASK;
847 ss->irqinterval = iface->ep_desc[i].bInterval;
848 }
849 }
850 debug("Endpoints In %d Out %d Int %d\n",
851 ss->ep_in, ss->ep_out, ss->ep_int);
852
853 /* Do some basic sanity checks, and bail if we find a problem */
854 if (usb_set_interface(dev, iface_desc->bInterfaceNumber, 0) ||
855 !ss->ep_in || !ss->ep_out || !ss->ep_int) {
856 debug("Problems with device\n");
857 return 0;
858 }
859 dev->privptr = (void *)ss;
860 return 1;
861}
862
863int smsc95xx_eth_get_info(struct usb_device *dev, struct ueth_data *ss,
864 struct eth_device *eth)
865{
866 debug("** %s()\n", __func__);
867 if (!eth) {
868 debug("%s: missing parameter.\n", __func__);
869 return 0;
870 }
871 sprintf(eth->name, "%s%d", SMSC95XX_BASE_NAME, curr_eth_dev++);
872 eth->init = smsc95xx_init;
873 eth->send = smsc95xx_send;
874 eth->recv = smsc95xx_recv;
875 eth->halt = smsc95xx_halt;
876 eth->write_hwaddr = smsc95xx_write_hwaddr;
877 eth->priv = ss;
878 return 1;
879}