blob: 72ec41ea896b1059af802b1172cb6f1796425fe2 [file] [log] [blame]
Simon Glass9b70e002011-02-16 11:14:34 -08001/*
2 * Copyright (c) 2011 The Chromium OS Authors.
Simon Glass9b70e002011-02-16 11:14:34 -08003 *
Wolfgang Denk1a459662013-07-08 09:37:19 +02004 * SPDX-License-Identifier: GPL-2.0+
Simon Glass9b70e002011-02-16 11:14:34 -08005 */
6
7#include <common.h>
Simon Glassfbc4b8a2015-07-06 16:47:54 -06008#include <dm.h>
Simon Glass9b70e002011-02-16 11:14:34 -08009#include <usb.h>
Simon Glassfbc4b8a2015-07-06 16:47:54 -060010#include <malloc.h>
Simon Glass9b70e002011-02-16 11:14:34 -080011#include <linux/mii.h>
12#include "usb_ether.h"
Simon Glass9b70e002011-02-16 11:14:34 -080013
14/* ASIX AX8817X based USB 2.0 Ethernet Devices */
15
16#define AX_CMD_SET_SW_MII 0x06
17#define AX_CMD_READ_MII_REG 0x07
18#define AX_CMD_WRITE_MII_REG 0x08
19#define AX_CMD_SET_HW_MII 0x0a
Lucas Stach02c8d8c2012-08-22 11:05:00 +000020#define AX_CMD_READ_EEPROM 0x0b
Simon Glass9b70e002011-02-16 11:14:34 -080021#define AX_CMD_READ_RX_CTL 0x0f
22#define AX_CMD_WRITE_RX_CTL 0x10
23#define AX_CMD_WRITE_IPG0 0x12
24#define AX_CMD_READ_NODE_ID 0x13
Lucas Stach58f8fab2012-08-22 11:04:59 +000025#define AX_CMD_WRITE_NODE_ID 0x14
Simon Glass9b70e002011-02-16 11:14:34 -080026#define AX_CMD_READ_PHY_ID 0x19
27#define AX_CMD_WRITE_MEDIUM_MODE 0x1b
28#define AX_CMD_WRITE_GPIOS 0x1f
29#define AX_CMD_SW_RESET 0x20
30#define AX_CMD_SW_PHY_SELECT 0x22
31
32#define AX_SWRESET_CLEAR 0x00
33#define AX_SWRESET_PRTE 0x04
34#define AX_SWRESET_PRL 0x08
35#define AX_SWRESET_IPRL 0x20
36#define AX_SWRESET_IPPD 0x40
37
38#define AX88772_IPG0_DEFAULT 0x15
39#define AX88772_IPG1_DEFAULT 0x0c
40#define AX88772_IPG2_DEFAULT 0x12
41
42/* AX88772 & AX88178 Medium Mode Register */
43#define AX_MEDIUM_PF 0x0080
44#define AX_MEDIUM_JFE 0x0040
45#define AX_MEDIUM_TFC 0x0020
46#define AX_MEDIUM_RFC 0x0010
47#define AX_MEDIUM_ENCK 0x0008
48#define AX_MEDIUM_AC 0x0004
49#define AX_MEDIUM_FD 0x0002
50#define AX_MEDIUM_GM 0x0001
51#define AX_MEDIUM_SM 0x1000
52#define AX_MEDIUM_SBP 0x0800
53#define AX_MEDIUM_PS 0x0200
54#define AX_MEDIUM_RE 0x0100
55
56#define AX88178_MEDIUM_DEFAULT \
57 (AX_MEDIUM_PS | AX_MEDIUM_FD | AX_MEDIUM_AC | \
58 AX_MEDIUM_RFC | AX_MEDIUM_TFC | AX_MEDIUM_JFE | \
59 AX_MEDIUM_RE)
60
61#define AX88772_MEDIUM_DEFAULT \
62 (AX_MEDIUM_FD | AX_MEDIUM_RFC | \
63 AX_MEDIUM_TFC | AX_MEDIUM_PS | \
64 AX_MEDIUM_AC | AX_MEDIUM_RE)
65
66/* AX88772 & AX88178 RX_CTL values */
67#define AX_RX_CTL_SO 0x0080
68#define AX_RX_CTL_AB 0x0008
69
70#define AX_DEFAULT_RX_CTL \
71 (AX_RX_CTL_SO | AX_RX_CTL_AB)
72
73/* GPIO 2 toggles */
74#define AX_GPIO_GPO2EN 0x10 /* GPIO2 Output enable */
75#define AX_GPIO_GPO_2 0x20 /* GPIO2 Output value */
76#define AX_GPIO_RSE 0x80 /* Reload serial EEPROM */
77
78/* local defines */
79#define ASIX_BASE_NAME "asx"
80#define USB_CTRL_SET_TIMEOUT 5000
81#define USB_CTRL_GET_TIMEOUT 5000
82#define USB_BULK_SEND_TIMEOUT 5000
83#define USB_BULK_RECV_TIMEOUT 5000
84
85#define AX_RX_URB_SIZE 2048
86#define PHY_CONNECT_TIMEOUT 5000
87
Lucas Stach58f8fab2012-08-22 11:04:59 +000088/* asix_flags defines */
89#define FLAG_NONE 0
90#define FLAG_TYPE_AX88172 (1U << 0)
91#define FLAG_TYPE_AX88772 (1U << 1)
Lucas Stach1dff9d02012-08-22 11:05:01 +000092#define FLAG_TYPE_AX88772B (1U << 2)
93#define FLAG_EEPROM_MAC (1U << 3) /* initial mac address in eeprom */
Lucas Stach58f8fab2012-08-22 11:04:59 +000094
Simon Glass9b70e002011-02-16 11:14:34 -080095
Lucas Stach58f8fab2012-08-22 11:04:59 +000096/* driver private */
97struct asix_private {
98 int flags;
Simon Glassfbc4b8a2015-07-06 16:47:54 -060099#ifdef CONFIG_DM_ETH
100 struct ueth_data ueth;
101#endif
Lucas Stach58f8fab2012-08-22 11:04:59 +0000102};
103
Simon Glassfbc4b8a2015-07-06 16:47:54 -0600104#ifndef CONFIG_DM_ETH
105/* local vars */
106static int curr_eth_dev; /* index for name of next device detected */
107#endif
108
Simon Glass9b70e002011-02-16 11:14:34 -0800109/*
110 * Asix infrastructure commands
111 */
112static int asix_write_cmd(struct ueth_data *dev, u8 cmd, u16 value, u16 index,
113 u16 size, void *data)
114{
115 int len;
116
117 debug("asix_write_cmd() cmd=0x%02x value=0x%04x index=0x%04x "
118 "size=%d\n", cmd, value, index, size);
119
120 len = usb_control_msg(
121 dev->pusb_dev,
122 usb_sndctrlpipe(dev->pusb_dev, 0),
123 cmd,
124 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
125 value,
126 index,
127 data,
128 size,
129 USB_CTRL_SET_TIMEOUT);
130
131 return len == size ? 0 : -1;
132}
133
134static int asix_read_cmd(struct ueth_data *dev, u8 cmd, u16 value, u16 index,
135 u16 size, void *data)
136{
137 int len;
138
139 debug("asix_read_cmd() cmd=0x%02x value=0x%04x index=0x%04x size=%d\n",
140 cmd, value, index, size);
141
142 len = usb_control_msg(
143 dev->pusb_dev,
144 usb_rcvctrlpipe(dev->pusb_dev, 0),
145 cmd,
146 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
147 value,
148 index,
149 data,
150 size,
151 USB_CTRL_GET_TIMEOUT);
152 return len == size ? 0 : -1;
153}
154
155static inline int asix_set_sw_mii(struct ueth_data *dev)
156{
157 int ret;
158
159 ret = asix_write_cmd(dev, AX_CMD_SET_SW_MII, 0x0000, 0, 0, NULL);
160 if (ret < 0)
161 debug("Failed to enable software MII access\n");
162 return ret;
163}
164
165static inline int asix_set_hw_mii(struct ueth_data *dev)
166{
167 int ret;
168
169 ret = asix_write_cmd(dev, AX_CMD_SET_HW_MII, 0x0000, 0, 0, NULL);
170 if (ret < 0)
171 debug("Failed to enable hardware MII access\n");
172 return ret;
173}
174
175static int asix_mdio_read(struct ueth_data *dev, int phy_id, int loc)
176{
Marek Vasutc59ab092012-06-24 14:17:56 +0000177 ALLOC_CACHE_ALIGN_BUFFER(__le16, res, 1);
Simon Glass9b70e002011-02-16 11:14:34 -0800178
179 asix_set_sw_mii(dev);
Marek Vasutc59ab092012-06-24 14:17:56 +0000180 asix_read_cmd(dev, AX_CMD_READ_MII_REG, phy_id, (__u16)loc, 2, res);
Simon Glass9b70e002011-02-16 11:14:34 -0800181 asix_set_hw_mii(dev);
182
183 debug("asix_mdio_read() phy_id=0x%02x, loc=0x%02x, returns=0x%04x\n",
Marek Vasutc59ab092012-06-24 14:17:56 +0000184 phy_id, loc, le16_to_cpu(*res));
Simon Glass9b70e002011-02-16 11:14:34 -0800185
Marek Vasutc59ab092012-06-24 14:17:56 +0000186 return le16_to_cpu(*res);
Simon Glass9b70e002011-02-16 11:14:34 -0800187}
188
189static void
190asix_mdio_write(struct ueth_data *dev, int phy_id, int loc, int val)
191{
Marek Vasutc59ab092012-06-24 14:17:56 +0000192 ALLOC_CACHE_ALIGN_BUFFER(__le16, res, 1);
193 *res = cpu_to_le16(val);
Simon Glass9b70e002011-02-16 11:14:34 -0800194
195 debug("asix_mdio_write() phy_id=0x%02x, loc=0x%02x, val=0x%04x\n",
196 phy_id, loc, val);
197 asix_set_sw_mii(dev);
Marek Vasutc59ab092012-06-24 14:17:56 +0000198 asix_write_cmd(dev, AX_CMD_WRITE_MII_REG, phy_id, (__u16)loc, 2, res);
Simon Glass9b70e002011-02-16 11:14:34 -0800199 asix_set_hw_mii(dev);
200}
201
202/*
203 * Asix "high level" commands
204 */
205static int asix_sw_reset(struct ueth_data *dev, u8 flags)
206{
207 int ret;
208
209 ret = asix_write_cmd(dev, AX_CMD_SW_RESET, flags, 0, 0, NULL);
210 if (ret < 0)
211 debug("Failed to send software reset: %02x\n", ret);
212 else
213 udelay(150 * 1000);
214
215 return ret;
216}
217
218static inline int asix_get_phy_addr(struct ueth_data *dev)
219{
Marek Vasutc59ab092012-06-24 14:17:56 +0000220 ALLOC_CACHE_ALIGN_BUFFER(u8, buf, 2);
221
Simon Glass9b70e002011-02-16 11:14:34 -0800222 int ret = asix_read_cmd(dev, AX_CMD_READ_PHY_ID, 0, 0, 2, buf);
223
224 debug("asix_get_phy_addr()\n");
225
226 if (ret < 0) {
227 debug("Error reading PHYID register: %02x\n", ret);
228 goto out;
229 }
Marek Vasut0ed1eb62011-09-23 21:13:35 +0200230 debug("asix_get_phy_addr() returning 0x%02x%02x\n", buf[0], buf[1]);
Simon Glass9b70e002011-02-16 11:14:34 -0800231 ret = buf[1];
232
233out:
234 return ret;
235}
236
237static int asix_write_medium_mode(struct ueth_data *dev, u16 mode)
238{
239 int ret;
240
241 debug("asix_write_medium_mode() - mode = 0x%04x\n", mode);
242 ret = asix_write_cmd(dev, AX_CMD_WRITE_MEDIUM_MODE, mode,
243 0, 0, NULL);
244 if (ret < 0) {
245 debug("Failed to write Medium Mode mode to 0x%04x: %02x\n",
246 mode, ret);
247 }
248 return ret;
249}
250
251static u16 asix_read_rx_ctl(struct ueth_data *dev)
252{
Marek Vasutc59ab092012-06-24 14:17:56 +0000253 ALLOC_CACHE_ALIGN_BUFFER(__le16, v, 1);
254
255 int ret = asix_read_cmd(dev, AX_CMD_READ_RX_CTL, 0, 0, 2, v);
Simon Glass9b70e002011-02-16 11:14:34 -0800256
257 if (ret < 0)
258 debug("Error reading RX_CTL register: %02x\n", ret);
259 else
Marek Vasutc59ab092012-06-24 14:17:56 +0000260 ret = le16_to_cpu(*v);
Simon Glass9b70e002011-02-16 11:14:34 -0800261 return ret;
262}
263
264static int asix_write_rx_ctl(struct ueth_data *dev, u16 mode)
265{
266 int ret;
267
268 debug("asix_write_rx_ctl() - mode = 0x%04x\n", mode);
269 ret = asix_write_cmd(dev, AX_CMD_WRITE_RX_CTL, mode, 0, 0, NULL);
270 if (ret < 0) {
271 debug("Failed to write RX_CTL mode to 0x%04x: %02x\n",
272 mode, ret);
273 }
274 return ret;
275}
276
277static int asix_write_gpio(struct ueth_data *dev, u16 value, int sleep)
278{
279 int ret;
280
281 debug("asix_write_gpio() - value = 0x%04x\n", value);
282 ret = asix_write_cmd(dev, AX_CMD_WRITE_GPIOS, value, 0, 0, NULL);
283 if (ret < 0) {
284 debug("Failed to write GPIO value 0x%04x: %02x\n",
285 value, ret);
286 }
287 if (sleep)
288 udelay(sleep * 1000);
289
290 return ret;
291}
292
Simon Glassfbc4b8a2015-07-06 16:47:54 -0600293static int asix_write_hwaddr_common(struct ueth_data *dev, uint8_t *enetaddr)
Lucas Stach58f8fab2012-08-22 11:04:59 +0000294{
Lucas Stach58f8fab2012-08-22 11:04:59 +0000295 int ret;
296 ALLOC_CACHE_ALIGN_BUFFER(unsigned char, buf, ETH_ALEN);
297
Simon Glassfbc4b8a2015-07-06 16:47:54 -0600298 memcpy(buf, enetaddr, ETH_ALEN);
Lucas Stach58f8fab2012-08-22 11:04:59 +0000299
300 ret = asix_write_cmd(dev, AX_CMD_WRITE_NODE_ID, 0, 0, ETH_ALEN, buf);
301 if (ret < 0)
302 debug("Failed to set MAC address: %02x\n", ret);
303
304 return ret;
305}
306
Simon Glass9b70e002011-02-16 11:14:34 -0800307/*
308 * mii commands
309 */
310
311/*
312 * mii_nway_restart - restart NWay (autonegotiation) for this interface
313 *
314 * Returns 0 on success, negative on error.
315 */
316static int mii_nway_restart(struct ueth_data *dev)
317{
318 int bmcr;
319 int r = -1;
320
321 /* if autoneg is off, it's an error */
322 bmcr = asix_mdio_read(dev, dev->phy_id, MII_BMCR);
323
324 if (bmcr & BMCR_ANENABLE) {
325 bmcr |= BMCR_ANRESTART;
326 asix_mdio_write(dev, dev->phy_id, MII_BMCR, bmcr);
327 r = 0;
328 }
329
330 return r;
331}
332
Simon Glassfbc4b8a2015-07-06 16:47:54 -0600333static int asix_read_mac_common(struct ueth_data *dev,
334 struct asix_private *priv, uint8_t *enetaddr)
Lucas Stach02c8d8c2012-08-22 11:05:00 +0000335{
Lucas Stach02c8d8c2012-08-22 11:05:00 +0000336 ALLOC_CACHE_ALIGN_BUFFER(unsigned char, buf, ETH_ALEN);
Simon Glassfbc4b8a2015-07-06 16:47:54 -0600337 int i;
Lucas Stach02c8d8c2012-08-22 11:05:00 +0000338
339 if (priv->flags & FLAG_EEPROM_MAC) {
340 for (i = 0; i < (ETH_ALEN >> 1); i++) {
341 if (asix_read_cmd(dev, AX_CMD_READ_EEPROM,
342 0x04 + i, 0, 2, buf) < 0) {
343 debug("Failed to read SROM address 04h.\n");
344 return -1;
345 }
Simon Glassfbc4b8a2015-07-06 16:47:54 -0600346 memcpy(enetaddr + i * 2, buf, 2);
Lucas Stach02c8d8c2012-08-22 11:05:00 +0000347 }
348 } else {
349 if (asix_read_cmd(dev, AX_CMD_READ_NODE_ID, 0, 0, ETH_ALEN, buf)
350 < 0) {
351 debug("Failed to read MAC address.\n");
352 return -1;
353 }
Simon Glassfbc4b8a2015-07-06 16:47:54 -0600354 memcpy(enetaddr, buf, ETH_ALEN);
Lucas Stach02c8d8c2012-08-22 11:05:00 +0000355 }
356
357 return 0;
358}
359
Lucas Stach5fae53d2012-08-22 11:04:58 +0000360static int asix_basic_reset(struct ueth_data *dev)
Simon Glass9b70e002011-02-16 11:14:34 -0800361{
362 int embd_phy;
Simon Glass9b70e002011-02-16 11:14:34 -0800363 u16 rx_ctl;
Simon Glass9b70e002011-02-16 11:14:34 -0800364
365 if (asix_write_gpio(dev,
366 AX_GPIO_RSE | AX_GPIO_GPO_2 | AX_GPIO_GPO2EN, 5) < 0)
Lucas Stach5fae53d2012-08-22 11:04:58 +0000367 return -1;
Simon Glass9b70e002011-02-16 11:14:34 -0800368
369 /* 0x10 is the phy id of the embedded 10/100 ethernet phy */
370 embd_phy = ((asix_get_phy_addr(dev) & 0x1f) == 0x10 ? 1 : 0);
371 if (asix_write_cmd(dev, AX_CMD_SW_PHY_SELECT,
372 embd_phy, 0, 0, NULL) < 0) {
373 debug("Select PHY #1 failed\n");
Lucas Stach5fae53d2012-08-22 11:04:58 +0000374 return -1;
Simon Glass9b70e002011-02-16 11:14:34 -0800375 }
376
377 if (asix_sw_reset(dev, AX_SWRESET_IPPD | AX_SWRESET_PRL) < 0)
Lucas Stach5fae53d2012-08-22 11:04:58 +0000378 return -1;
Simon Glass9b70e002011-02-16 11:14:34 -0800379
380 if (asix_sw_reset(dev, AX_SWRESET_CLEAR) < 0)
Lucas Stach5fae53d2012-08-22 11:04:58 +0000381 return -1;
Simon Glass9b70e002011-02-16 11:14:34 -0800382
383 if (embd_phy) {
384 if (asix_sw_reset(dev, AX_SWRESET_IPRL) < 0)
Lucas Stach5fae53d2012-08-22 11:04:58 +0000385 return -1;
Simon Glass9b70e002011-02-16 11:14:34 -0800386 } else {
387 if (asix_sw_reset(dev, AX_SWRESET_PRTE) < 0)
Lucas Stach5fae53d2012-08-22 11:04:58 +0000388 return -1;
Simon Glass9b70e002011-02-16 11:14:34 -0800389 }
390
391 rx_ctl = asix_read_rx_ctl(dev);
392 debug("RX_CTL is 0x%04x after software reset\n", rx_ctl);
393 if (asix_write_rx_ctl(dev, 0x0000) < 0)
Lucas Stach5fae53d2012-08-22 11:04:58 +0000394 return -1;
Simon Glass9b70e002011-02-16 11:14:34 -0800395
396 rx_ctl = asix_read_rx_ctl(dev);
397 debug("RX_CTL is 0x%04x setting to 0x0000\n", rx_ctl);
398
Julius Werner4edcf0a2013-05-11 13:35:02 -0700399 dev->phy_id = asix_get_phy_addr(dev);
400 if (dev->phy_id < 0)
401 debug("Failed to read phy id\n");
402
403 asix_mdio_write(dev, dev->phy_id, MII_BMCR, BMCR_RESET);
404 asix_mdio_write(dev, dev->phy_id, MII_ADVERTISE,
405 ADVERTISE_ALL | ADVERTISE_CSMA);
406 mii_nway_restart(dev);
407
408 if (asix_write_medium_mode(dev, AX88772_MEDIUM_DEFAULT) < 0)
409 return -1;
410
411 if (asix_write_cmd(dev, AX_CMD_WRITE_IPG0,
412 AX88772_IPG0_DEFAULT | AX88772_IPG1_DEFAULT,
413 AX88772_IPG2_DEFAULT, 0, NULL) < 0) {
414 debug("Write IPG,IPG1,IPG2 failed\n");
415 return -1;
416 }
417
Lucas Stach5fae53d2012-08-22 11:04:58 +0000418 return 0;
419}
420
Simon Glassfbc4b8a2015-07-06 16:47:54 -0600421static int asix_init_common(struct ueth_data *dev)
Lucas Stach5fae53d2012-08-22 11:04:58 +0000422{
Lucas Stach5fae53d2012-08-22 11:04:58 +0000423 int timeout = 0;
424#define TIMEOUT_RESOLUTION 50 /* ms */
425 int link_detected;
426
427 debug("** %s()\n", __func__);
428
Simon Glass9b70e002011-02-16 11:14:34 -0800429 if (asix_write_rx_ctl(dev, AX_DEFAULT_RX_CTL) < 0)
430 goto out_err;
431
432 do {
433 link_detected = asix_mdio_read(dev, dev->phy_id, MII_BMSR) &
434 BMSR_LSTATUS;
435 if (!link_detected) {
436 if (timeout == 0)
437 printf("Waiting for Ethernet connection... ");
438 udelay(TIMEOUT_RESOLUTION * 1000);
439 timeout += TIMEOUT_RESOLUTION;
440 }
441 } while (!link_detected && timeout < PHY_CONNECT_TIMEOUT);
442 if (link_detected) {
443 if (timeout != 0)
444 printf("done.\n");
445 } else {
446 printf("unable to connect.\n");
447 goto out_err;
448 }
449
450 return 0;
451out_err:
452 return -1;
453}
454
Simon Glassfbc4b8a2015-07-06 16:47:54 -0600455static int asix_send_common(struct ueth_data *dev, void *packet, int length)
Simon Glass9b70e002011-02-16 11:14:34 -0800456{
Simon Glass9b70e002011-02-16 11:14:34 -0800457 int err;
458 u32 packet_len;
459 int actual_len;
Marek Vasutc59ab092012-06-24 14:17:56 +0000460 ALLOC_CACHE_ALIGN_BUFFER(unsigned char, msg,
461 PKTSIZE + sizeof(packet_len));
Simon Glass9b70e002011-02-16 11:14:34 -0800462
463 debug("** %s(), len %d\n", __func__, length);
464
465 packet_len = (((length) ^ 0x0000ffff) << 16) + (length);
466 cpu_to_le32s(&packet_len);
467
468 memcpy(msg, &packet_len, sizeof(packet_len));
469 memcpy(msg + sizeof(packet_len), (void *)packet, length);
Simon Glass9b70e002011-02-16 11:14:34 -0800470
471 err = usb_bulk_msg(dev->pusb_dev,
472 usb_sndbulkpipe(dev->pusb_dev, dev->ep_out),
473 (void *)msg,
474 length + sizeof(packet_len),
475 &actual_len,
476 USB_BULK_SEND_TIMEOUT);
Thierry Reding4c5998b2015-03-20 12:41:23 +0100477 debug("Tx: len = %zu, actual = %u, err = %d\n",
Simon Glass9b70e002011-02-16 11:14:34 -0800478 length + sizeof(packet_len), actual_len, err);
479
480 return err;
481}
482
Simon Glassfbc4b8a2015-07-06 16:47:54 -0600483#ifndef CONFIG_DM_ETH
484/*
485 * Asix callbacks
486 */
487static int asix_init(struct eth_device *eth, bd_t *bd)
488{
489 struct ueth_data *dev = (struct ueth_data *)eth->priv;
490
491 return asix_init_common(dev);
492}
493
494static int asix_send(struct eth_device *eth, void *packet, int length)
495{
496 struct ueth_data *dev = (struct ueth_data *)eth->priv;
497
498 return asix_send_common(dev, packet, length);
499}
500
Simon Glass9b70e002011-02-16 11:14:34 -0800501static int asix_recv(struct eth_device *eth)
502{
503 struct ueth_data *dev = (struct ueth_data *)eth->priv;
Marek Vasutc59ab092012-06-24 14:17:56 +0000504 ALLOC_CACHE_ALIGN_BUFFER(unsigned char, recv_buf, AX_RX_URB_SIZE);
Simon Glass9b70e002011-02-16 11:14:34 -0800505 unsigned char *buf_ptr;
506 int err;
507 int actual_len;
508 u32 packet_len;
509
510 debug("** %s()\n", __func__);
511
512 err = usb_bulk_msg(dev->pusb_dev,
513 usb_rcvbulkpipe(dev->pusb_dev, dev->ep_in),
514 (void *)recv_buf,
515 AX_RX_URB_SIZE,
516 &actual_len,
517 USB_BULK_RECV_TIMEOUT);
518 debug("Rx: len = %u, actual = %u, err = %d\n", AX_RX_URB_SIZE,
519 actual_len, err);
520 if (err != 0) {
521 debug("Rx: failed to receive\n");
522 return -1;
523 }
524 if (actual_len > AX_RX_URB_SIZE) {
525 debug("Rx: received too many bytes %d\n", actual_len);
526 return -1;
527 }
528
529 buf_ptr = recv_buf;
530 while (actual_len > 0) {
531 /*
532 * 1st 4 bytes contain the length of the actual data as two
533 * complementary 16-bit words. Extract the length of the data.
534 */
535 if (actual_len < sizeof(packet_len)) {
536 debug("Rx: incomplete packet length\n");
537 return -1;
538 }
539 memcpy(&packet_len, buf_ptr, sizeof(packet_len));
540 le32_to_cpus(&packet_len);
Lucas Stach1dff9d02012-08-22 11:05:01 +0000541 if (((~packet_len >> 16) & 0x7ff) != (packet_len & 0x7ff)) {
Simon Glass9b70e002011-02-16 11:14:34 -0800542 debug("Rx: malformed packet length: %#x (%#x:%#x)\n",
Lucas Stach1dff9d02012-08-22 11:05:01 +0000543 packet_len, (~packet_len >> 16) & 0x7ff,
544 packet_len & 0x7ff);
Simon Glass9b70e002011-02-16 11:14:34 -0800545 return -1;
546 }
Lucas Stach1dff9d02012-08-22 11:05:01 +0000547 packet_len = packet_len & 0x7ff;
Simon Glass9b70e002011-02-16 11:14:34 -0800548 if (packet_len > actual_len - sizeof(packet_len)) {
549 debug("Rx: too large packet: %d\n", packet_len);
550 return -1;
551 }
552
553 /* Notify net stack */
Joe Hershberger1fd92db2015-04-08 01:41:06 -0500554 net_process_received_packet(buf_ptr + sizeof(packet_len),
555 packet_len);
Simon Glass9b70e002011-02-16 11:14:34 -0800556
557 /* Adjust for next iteration. Packets are padded to 16-bits */
558 if (packet_len & 1)
559 packet_len++;
560 actual_len -= sizeof(packet_len) + packet_len;
561 buf_ptr += sizeof(packet_len) + packet_len;
562 }
563
564 return err;
565}
566
567static void asix_halt(struct eth_device *eth)
568{
569 debug("** %s()\n", __func__);
570}
571
Simon Glassfbc4b8a2015-07-06 16:47:54 -0600572static int asix_write_hwaddr(struct eth_device *eth)
573{
574 struct ueth_data *dev = (struct ueth_data *)eth->priv;
575
576 return asix_write_hwaddr_common(dev, eth->enetaddr);
577}
578
Simon Glass9b70e002011-02-16 11:14:34 -0800579/*
580 * Asix probing functions
581 */
582void asix_eth_before_probe(void)
583{
584 curr_eth_dev = 0;
585}
586
587struct asix_dongle {
588 unsigned short vendor;
589 unsigned short product;
Lucas Stach58f8fab2012-08-22 11:04:59 +0000590 int flags;
Simon Glass9b70e002011-02-16 11:14:34 -0800591};
592
Jeroen Hofstee51afc2c2014-06-17 21:14:17 +0200593static const struct asix_dongle asix_dongles[] = {
Lucas Stach58f8fab2012-08-22 11:04:59 +0000594 { 0x05ac, 0x1402, FLAG_TYPE_AX88772 }, /* Apple USB Ethernet Adapter */
595 { 0x07d1, 0x3c05, FLAG_TYPE_AX88772 }, /* D-Link DUB-E100 H/W Ver B1 */
Pierre Aubertbefd3872013-06-27 09:01:54 +0200596 { 0x2001, 0x1a02, FLAG_TYPE_AX88772 }, /* D-Link DUB-E100 H/W Ver C1 */
Lucas Stach58f8fab2012-08-22 11:04:59 +0000597 /* Cables-to-Go USB Ethernet Adapter */
598 { 0x0b95, 0x772a, FLAG_TYPE_AX88772 },
599 { 0x0b95, 0x7720, FLAG_TYPE_AX88772 }, /* Trendnet TU2-ET100 V3.0R */
600 { 0x0b95, 0x1720, FLAG_TYPE_AX88172 }, /* SMC */
601 { 0x0db0, 0xa877, FLAG_TYPE_AX88772 }, /* MSI - ASIX 88772a */
602 { 0x13b1, 0x0018, FLAG_TYPE_AX88172 }, /* Linksys 200M v2.1 */
603 { 0x1557, 0x7720, FLAG_TYPE_AX88772 }, /* 0Q0 cable ethernet */
604 /* DLink DUB-E100 H/W Ver B1 Alternate */
605 { 0x2001, 0x3c05, FLAG_TYPE_AX88772 },
Lucas Stach1dff9d02012-08-22 11:05:01 +0000606 /* ASIX 88772B */
607 { 0x0b95, 0x772b, FLAG_TYPE_AX88772B | FLAG_EEPROM_MAC },
Alexandre Courbota7f24722014-10-09 12:43:30 +0900608 { 0x0b95, 0x7e2b, FLAG_TYPE_AX88772B },
Lucas Stach58f8fab2012-08-22 11:04:59 +0000609 { 0x0000, 0x0000, FLAG_NONE } /* END - Do not remove */
Simon Glass9b70e002011-02-16 11:14:34 -0800610};
611
612/* Probe to see if a new device is actually an asix device */
613int asix_eth_probe(struct usb_device *dev, unsigned int ifnum,
614 struct ueth_data *ss)
615{
616 struct usb_interface *iface;
617 struct usb_interface_descriptor *iface_desc;
Lucas Stach1dff9d02012-08-22 11:05:01 +0000618 int ep_in_found = 0, ep_out_found = 0;
Simon Glass9b70e002011-02-16 11:14:34 -0800619 int i;
620
621 /* let's examine the device now */
622 iface = &dev->config.if_desc[ifnum];
623 iface_desc = &dev->config.if_desc[ifnum].desc;
624
625 for (i = 0; asix_dongles[i].vendor != 0; i++) {
626 if (dev->descriptor.idVendor == asix_dongles[i].vendor &&
627 dev->descriptor.idProduct == asix_dongles[i].product)
628 /* Found a supported dongle */
629 break;
630 }
631
632 if (asix_dongles[i].vendor == 0)
633 return 0;
634
635 memset(ss, 0, sizeof(struct ueth_data));
636
637 /* At this point, we know we've got a live one */
638 debug("\n\nUSB Ethernet device detected: %#04x:%#04x\n",
639 dev->descriptor.idVendor, dev->descriptor.idProduct);
640
641 /* Initialize the ueth_data structure with some useful info */
642 ss->ifnum = ifnum;
643 ss->pusb_dev = dev;
644 ss->subclass = iface_desc->bInterfaceSubClass;
645 ss->protocol = iface_desc->bInterfaceProtocol;
646
Lucas Stach58f8fab2012-08-22 11:04:59 +0000647 /* alloc driver private */
648 ss->dev_priv = calloc(1, sizeof(struct asix_private));
649 if (!ss->dev_priv)
650 return 0;
651
652 ((struct asix_private *)ss->dev_priv)->flags = asix_dongles[i].flags;
653
Simon Glass9b70e002011-02-16 11:14:34 -0800654 /*
655 * We are expecting a minimum of 3 endpoints - in, out (bulk), and
656 * int. We will ignore any others.
657 */
658 for (i = 0; i < iface_desc->bNumEndpoints; i++) {
659 /* is it an BULK endpoint? */
660 if ((iface->ep_desc[i].bmAttributes &
661 USB_ENDPOINT_XFERTYPE_MASK) == USB_ENDPOINT_XFER_BULK) {
Lucas Stach1dff9d02012-08-22 11:05:01 +0000662 u8 ep_addr = iface->ep_desc[i].bEndpointAddress;
663 if (ep_addr & USB_DIR_IN) {
664 if (!ep_in_found) {
665 ss->ep_in = ep_addr &
666 USB_ENDPOINT_NUMBER_MASK;
667 ep_in_found = 1;
668 }
669 } else {
670 if (!ep_out_found) {
671 ss->ep_out = ep_addr &
672 USB_ENDPOINT_NUMBER_MASK;
673 ep_out_found = 1;
674 }
675 }
Simon Glass9b70e002011-02-16 11:14:34 -0800676 }
677
678 /* is it an interrupt endpoint? */
679 if ((iface->ep_desc[i].bmAttributes &
680 USB_ENDPOINT_XFERTYPE_MASK) == USB_ENDPOINT_XFER_INT) {
681 ss->ep_int = iface->ep_desc[i].bEndpointAddress &
682 USB_ENDPOINT_NUMBER_MASK;
683 ss->irqinterval = iface->ep_desc[i].bInterval;
684 }
685 }
686 debug("Endpoints In %d Out %d Int %d\n",
687 ss->ep_in, ss->ep_out, ss->ep_int);
688
689 /* Do some basic sanity checks, and bail if we find a problem */
690 if (usb_set_interface(dev, iface_desc->bInterfaceNumber, 0) ||
691 !ss->ep_in || !ss->ep_out || !ss->ep_int) {
692 debug("Problems with device\n");
693 return 0;
694 }
695 dev->privptr = (void *)ss;
696 return 1;
697}
698
699int asix_eth_get_info(struct usb_device *dev, struct ueth_data *ss,
700 struct eth_device *eth)
701{
Lucas Stach58f8fab2012-08-22 11:04:59 +0000702 struct asix_private *priv = (struct asix_private *)ss->dev_priv;
703
Simon Glass9b70e002011-02-16 11:14:34 -0800704 if (!eth) {
705 debug("%s: missing parameter.\n", __func__);
706 return 0;
707 }
708 sprintf(eth->name, "%s%d", ASIX_BASE_NAME, curr_eth_dev++);
709 eth->init = asix_init;
710 eth->send = asix_send;
711 eth->recv = asix_recv;
712 eth->halt = asix_halt;
Lucas Stach58f8fab2012-08-22 11:04:59 +0000713 if (!(priv->flags & FLAG_TYPE_AX88172))
714 eth->write_hwaddr = asix_write_hwaddr;
Simon Glass9b70e002011-02-16 11:14:34 -0800715 eth->priv = ss;
716
Lucas Stach5fae53d2012-08-22 11:04:58 +0000717 if (asix_basic_reset(ss))
718 return 0;
719
Lucas Stach02c8d8c2012-08-22 11:05:00 +0000720 /* Get the MAC address */
Simon Glassfbc4b8a2015-07-06 16:47:54 -0600721 if (asix_read_mac_common(ss, priv, eth->enetaddr))
Lucas Stach02c8d8c2012-08-22 11:05:00 +0000722 return 0;
723 debug("MAC %pM\n", eth->enetaddr);
724
Simon Glass9b70e002011-02-16 11:14:34 -0800725 return 1;
726}
Simon Glassfbc4b8a2015-07-06 16:47:54 -0600727#endif
728
729#ifdef CONFIG_DM_ETH
730static int asix_eth_start(struct udevice *dev)
731{
732 struct asix_private *priv = dev_get_priv(dev);
733
734 return asix_init_common(&priv->ueth);
735}
736
737void asix_eth_stop(struct udevice *dev)
738{
739 debug("** %s()\n", __func__);
740}
741
742int asix_eth_send(struct udevice *dev, void *packet, int length)
743{
744 struct asix_private *priv = dev_get_priv(dev);
745
746 return asix_send_common(&priv->ueth, packet, length);
747}
748
749int asix_eth_recv(struct udevice *dev, int flags, uchar **packetp)
750{
751 struct asix_private *priv = dev_get_priv(dev);
752 struct ueth_data *ueth = &priv->ueth;
753 uint8_t *ptr;
754 int ret, len;
755 u32 packet_len;
756
757 len = usb_ether_get_rx_bytes(ueth, &ptr);
758 debug("%s: first try, len=%d\n", __func__, len);
759 if (!len) {
760 if (!(flags & ETH_RECV_CHECK_DEVICE))
761 return -EAGAIN;
762 ret = usb_ether_receive(ueth, AX_RX_URB_SIZE);
763 if (ret == -EAGAIN)
764 return ret;
765
766 len = usb_ether_get_rx_bytes(ueth, &ptr);
767 debug("%s: second try, len=%d\n", __func__, len);
768 }
769
770 /*
771 * 1st 4 bytes contain the length of the actual data as two
772 * complementary 16-bit words. Extract the length of the data.
773 */
774 if (len < sizeof(packet_len)) {
775 debug("Rx: incomplete packet length\n");
776 goto err;
777 }
778 memcpy(&packet_len, ptr, sizeof(packet_len));
779 le32_to_cpus(&packet_len);
780 if (((~packet_len >> 16) & 0x7ff) != (packet_len & 0x7ff)) {
781 debug("Rx: malformed packet length: %#x (%#x:%#x)\n",
782 packet_len, (~packet_len >> 16) & 0x7ff,
783 packet_len & 0x7ff);
784 goto err;
785 }
786 packet_len = packet_len & 0x7ff;
787 if (packet_len > len - sizeof(packet_len)) {
788 debug("Rx: too large packet: %d\n", packet_len);
789 goto err;
790 }
791
792 *packetp = ptr + sizeof(packet_len);
793 return packet_len;
794
795err:
796 usb_ether_advance_rxbuf(ueth, -1);
797 return -EINVAL;
798}
799
800static int asix_free_pkt(struct udevice *dev, uchar *packet, int packet_len)
801{
802 struct asix_private *priv = dev_get_priv(dev);
803
804 if (packet_len & 1)
805 packet_len++;
806 usb_ether_advance_rxbuf(&priv->ueth, sizeof(u32) + packet_len);
807
808 return 0;
809}
810
811int asix_write_hwaddr(struct udevice *dev)
812{
813 struct eth_pdata *pdata = dev_get_platdata(dev);
814 struct asix_private *priv = dev_get_priv(dev);
815
816 if (priv->flags & FLAG_TYPE_AX88172)
817 return -ENOSYS;
818
819 return asix_write_hwaddr_common(&priv->ueth, pdata->enetaddr);
820}
821
822static int asix_eth_probe(struct udevice *dev)
823{
824 struct eth_pdata *pdata = dev_get_platdata(dev);
825 struct asix_private *priv = dev_get_priv(dev);
826 struct ueth_data *ss = &priv->ueth;
827 int ret;
828
829 priv->flags = dev->driver_data;
830 ret = usb_ether_register(dev, ss, AX_RX_URB_SIZE);
831 if (ret)
832 return ret;
833
834 ret = asix_basic_reset(ss);
835 if (ret)
836 goto err;
837
838 /* Get the MAC address */
839 ret = asix_read_mac_common(ss, priv, pdata->enetaddr);
840 if (ret)
841 goto err;
842 debug("MAC %pM\n", pdata->enetaddr);
843
844 return 0;
845
846err:
847 return usb_ether_deregister(ss);
848}
849
850static const struct eth_ops asix_eth_ops = {
851 .start = asix_eth_start,
852 .send = asix_eth_send,
853 .recv = asix_eth_recv,
854 .free_pkt = asix_free_pkt,
855 .stop = asix_eth_stop,
856 .write_hwaddr = asix_write_hwaddr,
857};
858
859U_BOOT_DRIVER(asix_eth) = {
860 .name = "asix_eth",
861 .id = UCLASS_ETH,
862 .probe = asix_eth_probe,
863 .ops = &asix_eth_ops,
864 .priv_auto_alloc_size = sizeof(struct asix_private),
865 .platdata_auto_alloc_size = sizeof(struct eth_pdata),
866};
867
868static const struct usb_device_id asix_eth_id_table[] = {
869 /* Apple USB Ethernet Adapter */
870 { USB_DEVICE(0x05ac, 0x1402), .driver_info = FLAG_TYPE_AX88772 },
871 /* D-Link DUB-E100 H/W Ver B1 */
872 { USB_DEVICE(0x07d1, 0x3c05), .driver_info = FLAG_TYPE_AX88772 },
873 /* D-Link DUB-E100 H/W Ver C1 */
874 { USB_DEVICE(0x2001, 0x1a02), .driver_info = FLAG_TYPE_AX88772 },
875 /* Cables-to-Go USB Ethernet Adapter */
876 { USB_DEVICE(0x0b95, 0x772a), .driver_info = FLAG_TYPE_AX88772 },
877 /* Trendnet TU2-ET100 V3.0R */
878 { USB_DEVICE(0x0b95, 0x7720), .driver_info = FLAG_TYPE_AX88772 },
879 /* SMC */
880 { USB_DEVICE(0x0b95, 0x1720), .driver_info = FLAG_TYPE_AX88172 },
881 /* MSI - ASIX 88772a */
882 { USB_DEVICE(0x0db0, 0xa877), .driver_info = FLAG_TYPE_AX88772 },
883 /* Linksys 200M v2.1 */
884 { USB_DEVICE(0x13b1, 0x0018), .driver_info = FLAG_TYPE_AX88172 },
885 /* 0Q0 cable ethernet */
886 { USB_DEVICE(0x1557, 0x7720), .driver_info = FLAG_TYPE_AX88772 },
887 /* DLink DUB-E100 H/W Ver B1 Alternate */
888 { USB_DEVICE(0x2001, 0x3c05), .driver_info = FLAG_TYPE_AX88772 },
889 /* ASIX 88772B */
890 { USB_DEVICE(0x0b95, 0x772b),
891 .driver_info = FLAG_TYPE_AX88772B | FLAG_EEPROM_MAC },
892 { USB_DEVICE(0x0b95, 0x7e2b), .driver_info = FLAG_TYPE_AX88772B },
893 { } /* Terminating entry */
894};
895
896U_BOOT_USB_DEVICE(asix_eth, asix_eth_id_table);
897#endif