blob: 3a8ec0bf2dd785a52499a63930f28f838e1d5297 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Simon Glass246d6932011-02-16 11:14:34 -08002/*
3 * Copyright (c) 2011 The Chromium OS Authors.
Simon Glass246d6932011-02-16 11:14:34 -08004 *
Marcel Ziswiler7bbbb3c2015-08-05 17:16:59 +02005 * Patched for AX88772B by Antmicro Ltd <www.antmicro.com>
Simon Glass246d6932011-02-16 11:14:34 -08006 */
7
8#include <common.h>
Simon Glassf58369f2015-07-06 16:47:54 -06009#include <dm.h>
Simon Glass246d6932011-02-16 11:14:34 -080010#include <usb.h>
Simon Glassf58369f2015-07-06 16:47:54 -060011#include <malloc.h>
Simon Glass2dd337a2015-09-02 17:24:58 -060012#include <memalign.h>
Simon Glass246d6932011-02-16 11:14:34 -080013#include <linux/mii.h>
14#include "usb_ether.h"
Simon Glass246d6932011-02-16 11:14:34 -080015
16/* ASIX AX8817X based USB 2.0 Ethernet Devices */
17
18#define AX_CMD_SET_SW_MII 0x06
19#define AX_CMD_READ_MII_REG 0x07
20#define AX_CMD_WRITE_MII_REG 0x08
21#define AX_CMD_SET_HW_MII 0x0a
Lucas Stach95c359b2012-08-22 11:05:00 +000022#define AX_CMD_READ_EEPROM 0x0b
Simon Glass246d6932011-02-16 11:14:34 -080023#define AX_CMD_READ_RX_CTL 0x0f
24#define AX_CMD_WRITE_RX_CTL 0x10
25#define AX_CMD_WRITE_IPG0 0x12
26#define AX_CMD_READ_NODE_ID 0x13
Lucas Stachfc9a7582012-08-22 11:04:59 +000027#define AX_CMD_WRITE_NODE_ID 0x14
Simon Glass246d6932011-02-16 11:14:34 -080028#define AX_CMD_READ_PHY_ID 0x19
29#define AX_CMD_WRITE_MEDIUM_MODE 0x1b
30#define AX_CMD_WRITE_GPIOS 0x1f
31#define AX_CMD_SW_RESET 0x20
32#define AX_CMD_SW_PHY_SELECT 0x22
33
34#define AX_SWRESET_CLEAR 0x00
35#define AX_SWRESET_PRTE 0x04
36#define AX_SWRESET_PRL 0x08
37#define AX_SWRESET_IPRL 0x20
38#define AX_SWRESET_IPPD 0x40
39
40#define AX88772_IPG0_DEFAULT 0x15
41#define AX88772_IPG1_DEFAULT 0x0c
42#define AX88772_IPG2_DEFAULT 0x12
43
44/* AX88772 & AX88178 Medium Mode Register */
45#define AX_MEDIUM_PF 0x0080
46#define AX_MEDIUM_JFE 0x0040
47#define AX_MEDIUM_TFC 0x0020
48#define AX_MEDIUM_RFC 0x0010
49#define AX_MEDIUM_ENCK 0x0008
50#define AX_MEDIUM_AC 0x0004
51#define AX_MEDIUM_FD 0x0002
52#define AX_MEDIUM_GM 0x0001
53#define AX_MEDIUM_SM 0x1000
54#define AX_MEDIUM_SBP 0x0800
55#define AX_MEDIUM_PS 0x0200
56#define AX_MEDIUM_RE 0x0100
57
58#define AX88178_MEDIUM_DEFAULT \
59 (AX_MEDIUM_PS | AX_MEDIUM_FD | AX_MEDIUM_AC | \
60 AX_MEDIUM_RFC | AX_MEDIUM_TFC | AX_MEDIUM_JFE | \
61 AX_MEDIUM_RE)
62
63#define AX88772_MEDIUM_DEFAULT \
64 (AX_MEDIUM_FD | AX_MEDIUM_RFC | \
65 AX_MEDIUM_TFC | AX_MEDIUM_PS | \
66 AX_MEDIUM_AC | AX_MEDIUM_RE)
67
68/* AX88772 & AX88178 RX_CTL values */
Alban Bedel3cf1a962016-09-10 03:54:09 +020069#define AX_RX_CTL_SO 0x0080
70#define AX_RX_CTL_AB 0x0008
Simon Glass246d6932011-02-16 11:14:34 -080071
72#define AX_DEFAULT_RX_CTL \
73 (AX_RX_CTL_SO | AX_RX_CTL_AB)
74
75/* GPIO 2 toggles */
76#define AX_GPIO_GPO2EN 0x10 /* GPIO2 Output enable */
77#define AX_GPIO_GPO_2 0x20 /* GPIO2 Output value */
78#define AX_GPIO_RSE 0x80 /* Reload serial EEPROM */
79
80/* local defines */
81#define ASIX_BASE_NAME "asx"
82#define USB_CTRL_SET_TIMEOUT 5000
83#define USB_CTRL_GET_TIMEOUT 5000
84#define USB_BULK_SEND_TIMEOUT 5000
85#define USB_BULK_RECV_TIMEOUT 5000
86
87#define AX_RX_URB_SIZE 2048
88#define PHY_CONNECT_TIMEOUT 5000
89
Lucas Stachfc9a7582012-08-22 11:04:59 +000090/* asix_flags defines */
91#define FLAG_NONE 0
92#define FLAG_TYPE_AX88172 (1U << 0)
93#define FLAG_TYPE_AX88772 (1U << 1)
Lucas Stach06e46932012-08-22 11:05:01 +000094#define FLAG_TYPE_AX88772B (1U << 2)
95#define FLAG_EEPROM_MAC (1U << 3) /* initial mac address in eeprom */
Lucas Stachfc9a7582012-08-22 11:04:59 +000096
Simon Glass246d6932011-02-16 11:14:34 -080097
Lucas Stachfc9a7582012-08-22 11:04:59 +000098/* driver private */
99struct asix_private {
100 int flags;
Simon Glassf58369f2015-07-06 16:47:54 -0600101#ifdef CONFIG_DM_ETH
102 struct ueth_data ueth;
103#endif
Lucas Stachfc9a7582012-08-22 11:04:59 +0000104};
105
Simon Glassf58369f2015-07-06 16:47:54 -0600106#ifndef CONFIG_DM_ETH
107/* local vars */
108static int curr_eth_dev; /* index for name of next device detected */
109#endif
110
Simon Glass246d6932011-02-16 11:14:34 -0800111/*
112 * Asix infrastructure commands
113 */
114static int asix_write_cmd(struct ueth_data *dev, u8 cmd, u16 value, u16 index,
115 u16 size, void *data)
116{
117 int len;
118
119 debug("asix_write_cmd() cmd=0x%02x value=0x%04x index=0x%04x "
120 "size=%d\n", cmd, value, index, size);
121
122 len = usb_control_msg(
123 dev->pusb_dev,
124 usb_sndctrlpipe(dev->pusb_dev, 0),
125 cmd,
126 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
127 value,
128 index,
129 data,
130 size,
131 USB_CTRL_SET_TIMEOUT);
132
133 return len == size ? 0 : -1;
134}
135
136static int asix_read_cmd(struct ueth_data *dev, u8 cmd, u16 value, u16 index,
137 u16 size, void *data)
138{
139 int len;
140
141 debug("asix_read_cmd() cmd=0x%02x value=0x%04x index=0x%04x size=%d\n",
142 cmd, value, index, size);
143
144 len = usb_control_msg(
145 dev->pusb_dev,
146 usb_rcvctrlpipe(dev->pusb_dev, 0),
147 cmd,
148 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
149 value,
150 index,
151 data,
152 size,
153 USB_CTRL_GET_TIMEOUT);
154 return len == size ? 0 : -1;
155}
156
157static inline int asix_set_sw_mii(struct ueth_data *dev)
158{
159 int ret;
160
161 ret = asix_write_cmd(dev, AX_CMD_SET_SW_MII, 0x0000, 0, 0, NULL);
162 if (ret < 0)
163 debug("Failed to enable software MII access\n");
164 return ret;
165}
166
167static inline int asix_set_hw_mii(struct ueth_data *dev)
168{
169 int ret;
170
171 ret = asix_write_cmd(dev, AX_CMD_SET_HW_MII, 0x0000, 0, 0, NULL);
172 if (ret < 0)
173 debug("Failed to enable hardware MII access\n");
174 return ret;
175}
176
177static int asix_mdio_read(struct ueth_data *dev, int phy_id, int loc)
178{
Marek Vasut115436e2012-06-24 14:17:56 +0000179 ALLOC_CACHE_ALIGN_BUFFER(__le16, res, 1);
Simon Glass246d6932011-02-16 11:14:34 -0800180
181 asix_set_sw_mii(dev);
Marek Vasut115436e2012-06-24 14:17:56 +0000182 asix_read_cmd(dev, AX_CMD_READ_MII_REG, phy_id, (__u16)loc, 2, res);
Simon Glass246d6932011-02-16 11:14:34 -0800183 asix_set_hw_mii(dev);
184
185 debug("asix_mdio_read() phy_id=0x%02x, loc=0x%02x, returns=0x%04x\n",
Marek Vasut115436e2012-06-24 14:17:56 +0000186 phy_id, loc, le16_to_cpu(*res));
Simon Glass246d6932011-02-16 11:14:34 -0800187
Marek Vasut115436e2012-06-24 14:17:56 +0000188 return le16_to_cpu(*res);
Simon Glass246d6932011-02-16 11:14:34 -0800189}
190
191static void
192asix_mdio_write(struct ueth_data *dev, int phy_id, int loc, int val)
193{
Marek Vasut115436e2012-06-24 14:17:56 +0000194 ALLOC_CACHE_ALIGN_BUFFER(__le16, res, 1);
195 *res = cpu_to_le16(val);
Simon Glass246d6932011-02-16 11:14:34 -0800196
197 debug("asix_mdio_write() phy_id=0x%02x, loc=0x%02x, val=0x%04x\n",
198 phy_id, loc, val);
199 asix_set_sw_mii(dev);
Marek Vasut115436e2012-06-24 14:17:56 +0000200 asix_write_cmd(dev, AX_CMD_WRITE_MII_REG, phy_id, (__u16)loc, 2, res);
Simon Glass246d6932011-02-16 11:14:34 -0800201 asix_set_hw_mii(dev);
202}
203
204/*
205 * Asix "high level" commands
206 */
207static int asix_sw_reset(struct ueth_data *dev, u8 flags)
208{
209 int ret;
210
211 ret = asix_write_cmd(dev, AX_CMD_SW_RESET, flags, 0, 0, NULL);
212 if (ret < 0)
213 debug("Failed to send software reset: %02x\n", ret);
214 else
215 udelay(150 * 1000);
216
217 return ret;
218}
219
220static inline int asix_get_phy_addr(struct ueth_data *dev)
221{
Marek Vasut115436e2012-06-24 14:17:56 +0000222 ALLOC_CACHE_ALIGN_BUFFER(u8, buf, 2);
223
Simon Glass246d6932011-02-16 11:14:34 -0800224 int ret = asix_read_cmd(dev, AX_CMD_READ_PHY_ID, 0, 0, 2, buf);
225
226 debug("asix_get_phy_addr()\n");
227
228 if (ret < 0) {
229 debug("Error reading PHYID register: %02x\n", ret);
230 goto out;
231 }
Marek Vasuta61b9662011-09-23 21:13:35 +0200232 debug("asix_get_phy_addr() returning 0x%02x%02x\n", buf[0], buf[1]);
Simon Glass246d6932011-02-16 11:14:34 -0800233 ret = buf[1];
234
235out:
236 return ret;
237}
238
239static int asix_write_medium_mode(struct ueth_data *dev, u16 mode)
240{
241 int ret;
242
243 debug("asix_write_medium_mode() - mode = 0x%04x\n", mode);
244 ret = asix_write_cmd(dev, AX_CMD_WRITE_MEDIUM_MODE, mode,
245 0, 0, NULL);
246 if (ret < 0) {
247 debug("Failed to write Medium Mode mode to 0x%04x: %02x\n",
248 mode, ret);
249 }
250 return ret;
251}
252
253static u16 asix_read_rx_ctl(struct ueth_data *dev)
254{
Marek Vasut115436e2012-06-24 14:17:56 +0000255 ALLOC_CACHE_ALIGN_BUFFER(__le16, v, 1);
256
257 int ret = asix_read_cmd(dev, AX_CMD_READ_RX_CTL, 0, 0, 2, v);
Simon Glass246d6932011-02-16 11:14:34 -0800258
259 if (ret < 0)
260 debug("Error reading RX_CTL register: %02x\n", ret);
261 else
Marek Vasut115436e2012-06-24 14:17:56 +0000262 ret = le16_to_cpu(*v);
Simon Glass246d6932011-02-16 11:14:34 -0800263 return ret;
264}
265
266static int asix_write_rx_ctl(struct ueth_data *dev, u16 mode)
267{
268 int ret;
269
270 debug("asix_write_rx_ctl() - mode = 0x%04x\n", mode);
271 ret = asix_write_cmd(dev, AX_CMD_WRITE_RX_CTL, mode, 0, 0, NULL);
272 if (ret < 0) {
273 debug("Failed to write RX_CTL mode to 0x%04x: %02x\n",
274 mode, ret);
275 }
276 return ret;
277}
278
279static int asix_write_gpio(struct ueth_data *dev, u16 value, int sleep)
280{
281 int ret;
282
283 debug("asix_write_gpio() - value = 0x%04x\n", value);
284 ret = asix_write_cmd(dev, AX_CMD_WRITE_GPIOS, value, 0, 0, NULL);
285 if (ret < 0) {
286 debug("Failed to write GPIO value 0x%04x: %02x\n",
287 value, ret);
288 }
289 if (sleep)
290 udelay(sleep * 1000);
291
292 return ret;
293}
294
Simon Glassf58369f2015-07-06 16:47:54 -0600295static int asix_write_hwaddr_common(struct ueth_data *dev, uint8_t *enetaddr)
Lucas Stachfc9a7582012-08-22 11:04:59 +0000296{
Lucas Stachfc9a7582012-08-22 11:04:59 +0000297 int ret;
298 ALLOC_CACHE_ALIGN_BUFFER(unsigned char, buf, ETH_ALEN);
299
Simon Glassf58369f2015-07-06 16:47:54 -0600300 memcpy(buf, enetaddr, ETH_ALEN);
Lucas Stachfc9a7582012-08-22 11:04:59 +0000301
302 ret = asix_write_cmd(dev, AX_CMD_WRITE_NODE_ID, 0, 0, ETH_ALEN, buf);
303 if (ret < 0)
304 debug("Failed to set MAC address: %02x\n", ret);
305
306 return ret;
307}
308
Simon Glass246d6932011-02-16 11:14:34 -0800309/*
310 * mii commands
311 */
312
313/*
314 * mii_nway_restart - restart NWay (autonegotiation) for this interface
315 *
316 * Returns 0 on success, negative on error.
317 */
318static int mii_nway_restart(struct ueth_data *dev)
319{
320 int bmcr;
321 int r = -1;
322
323 /* if autoneg is off, it's an error */
324 bmcr = asix_mdio_read(dev, dev->phy_id, MII_BMCR);
325
326 if (bmcr & BMCR_ANENABLE) {
327 bmcr |= BMCR_ANRESTART;
328 asix_mdio_write(dev, dev->phy_id, MII_BMCR, bmcr);
329 r = 0;
330 }
331
332 return r;
333}
334
Simon Glassf58369f2015-07-06 16:47:54 -0600335static int asix_read_mac_common(struct ueth_data *dev,
336 struct asix_private *priv, uint8_t *enetaddr)
Lucas Stach95c359b2012-08-22 11:05:00 +0000337{
Lucas Stach95c359b2012-08-22 11:05:00 +0000338 ALLOC_CACHE_ALIGN_BUFFER(unsigned char, buf, ETH_ALEN);
Simon Glassf58369f2015-07-06 16:47:54 -0600339 int i;
Lucas Stach95c359b2012-08-22 11:05:00 +0000340
341 if (priv->flags & FLAG_EEPROM_MAC) {
342 for (i = 0; i < (ETH_ALEN >> 1); i++) {
343 if (asix_read_cmd(dev, AX_CMD_READ_EEPROM,
344 0x04 + i, 0, 2, buf) < 0) {
345 debug("Failed to read SROM address 04h.\n");
346 return -1;
347 }
Simon Glassf58369f2015-07-06 16:47:54 -0600348 memcpy(enetaddr + i * 2, buf, 2);
Lucas Stach95c359b2012-08-22 11:05:00 +0000349 }
350 } else {
351 if (asix_read_cmd(dev, AX_CMD_READ_NODE_ID, 0, 0, ETH_ALEN, buf)
352 < 0) {
353 debug("Failed to read MAC address.\n");
354 return -1;
355 }
Simon Glassf58369f2015-07-06 16:47:54 -0600356 memcpy(enetaddr, buf, ETH_ALEN);
Lucas Stach95c359b2012-08-22 11:05:00 +0000357 }
358
359 return 0;
360}
361
Lucas Stacha0d5d032012-08-22 11:04:58 +0000362static int asix_basic_reset(struct ueth_data *dev)
Simon Glass246d6932011-02-16 11:14:34 -0800363{
364 int embd_phy;
Simon Glass246d6932011-02-16 11:14:34 -0800365 u16 rx_ctl;
Simon Glass246d6932011-02-16 11:14:34 -0800366
367 if (asix_write_gpio(dev,
368 AX_GPIO_RSE | AX_GPIO_GPO_2 | AX_GPIO_GPO2EN, 5) < 0)
Lucas Stacha0d5d032012-08-22 11:04:58 +0000369 return -1;
Simon Glass246d6932011-02-16 11:14:34 -0800370
371 /* 0x10 is the phy id of the embedded 10/100 ethernet phy */
372 embd_phy = ((asix_get_phy_addr(dev) & 0x1f) == 0x10 ? 1 : 0);
373 if (asix_write_cmd(dev, AX_CMD_SW_PHY_SELECT,
374 embd_phy, 0, 0, NULL) < 0) {
375 debug("Select PHY #1 failed\n");
Lucas Stacha0d5d032012-08-22 11:04:58 +0000376 return -1;
Simon Glass246d6932011-02-16 11:14:34 -0800377 }
378
379 if (asix_sw_reset(dev, AX_SWRESET_IPPD | AX_SWRESET_PRL) < 0)
Lucas Stacha0d5d032012-08-22 11:04:58 +0000380 return -1;
Simon Glass246d6932011-02-16 11:14:34 -0800381
382 if (asix_sw_reset(dev, AX_SWRESET_CLEAR) < 0)
Lucas Stacha0d5d032012-08-22 11:04:58 +0000383 return -1;
Simon Glass246d6932011-02-16 11:14:34 -0800384
385 if (embd_phy) {
386 if (asix_sw_reset(dev, AX_SWRESET_IPRL) < 0)
Lucas Stacha0d5d032012-08-22 11:04:58 +0000387 return -1;
Simon Glass246d6932011-02-16 11:14:34 -0800388 } else {
389 if (asix_sw_reset(dev, AX_SWRESET_PRTE) < 0)
Lucas Stacha0d5d032012-08-22 11:04:58 +0000390 return -1;
Simon Glass246d6932011-02-16 11:14:34 -0800391 }
392
393 rx_ctl = asix_read_rx_ctl(dev);
394 debug("RX_CTL is 0x%04x after software reset\n", rx_ctl);
395 if (asix_write_rx_ctl(dev, 0x0000) < 0)
Lucas Stacha0d5d032012-08-22 11:04:58 +0000396 return -1;
Simon Glass246d6932011-02-16 11:14:34 -0800397
398 rx_ctl = asix_read_rx_ctl(dev);
399 debug("RX_CTL is 0x%04x setting to 0x0000\n", rx_ctl);
400
Simon Glass246d6932011-02-16 11:14:34 -0800401 dev->phy_id = asix_get_phy_addr(dev);
402 if (dev->phy_id < 0)
403 debug("Failed to read phy id\n");
404
Simon Glass246d6932011-02-16 11:14:34 -0800405 asix_mdio_write(dev, dev->phy_id, MII_BMCR, BMCR_RESET);
406 asix_mdio_write(dev, dev->phy_id, MII_ADVERTISE,
407 ADVERTISE_ALL | ADVERTISE_CSMA);
408 mii_nway_restart(dev);
409
410 if (asix_write_medium_mode(dev, AX88772_MEDIUM_DEFAULT) < 0)
Julius Werner160298f2013-05-11 13:35:02 -0700411 return -1;
Simon Glass246d6932011-02-16 11:14:34 -0800412
413 if (asix_write_cmd(dev, AX_CMD_WRITE_IPG0,
414 AX88772_IPG0_DEFAULT | AX88772_IPG1_DEFAULT,
415 AX88772_IPG2_DEFAULT, 0, NULL) < 0) {
416 debug("Write IPG,IPG1,IPG2 failed\n");
Julius Werner160298f2013-05-11 13:35:02 -0700417 return -1;
Simon Glass246d6932011-02-16 11:14:34 -0800418 }
419
Julius Werner160298f2013-05-11 13:35:02 -0700420 return 0;
421}
422
Marcel Ziswiler7bbbb3c2015-08-05 17:16:59 +0200423static int asix_init_common(struct ueth_data *dev, uint8_t *enetaddr)
Julius Werner160298f2013-05-11 13:35:02 -0700424{
Julius Werner160298f2013-05-11 13:35:02 -0700425 int timeout = 0;
426#define TIMEOUT_RESOLUTION 50 /* ms */
427 int link_detected;
428
429 debug("** %s()\n", __func__);
430
Alban Bedel3cf1a962016-09-10 03:54:09 +0200431 if (asix_write_rx_ctl(dev, AX_DEFAULT_RX_CTL) < 0)
Marcel Ziswiler7bbbb3c2015-08-05 17:16:59 +0200432 goto out_err;
433
434 if (asix_write_hwaddr_common(dev, enetaddr) < 0)
Simon Glass246d6932011-02-16 11:14:34 -0800435 goto out_err;
436
437 do {
438 link_detected = asix_mdio_read(dev, dev->phy_id, MII_BMSR) &
439 BMSR_LSTATUS;
440 if (!link_detected) {
441 if (timeout == 0)
442 printf("Waiting for Ethernet connection... ");
443 udelay(TIMEOUT_RESOLUTION * 1000);
444 timeout += TIMEOUT_RESOLUTION;
445 }
446 } while (!link_detected && timeout < PHY_CONNECT_TIMEOUT);
447 if (link_detected) {
448 if (timeout != 0)
449 printf("done.\n");
450 } else {
451 printf("unable to connect.\n");
452 goto out_err;
453 }
454
Marcel Ziswiler7bbbb3c2015-08-05 17:16:59 +0200455 /*
456 * Wait some more to avoid timeout on first transfer
457 * (e.g. EHCI timed out on TD - token=0x8008d80)
458 */
459 mdelay(25);
460
Simon Glass246d6932011-02-16 11:14:34 -0800461 return 0;
462out_err:
463 return -1;
464}
465
Simon Glassf58369f2015-07-06 16:47:54 -0600466static int asix_send_common(struct ueth_data *dev, void *packet, int length)
Simon Glass246d6932011-02-16 11:14:34 -0800467{
Simon Glass246d6932011-02-16 11:14:34 -0800468 int err;
469 u32 packet_len;
470 int actual_len;
Marek Vasut115436e2012-06-24 14:17:56 +0000471 ALLOC_CACHE_ALIGN_BUFFER(unsigned char, msg,
472 PKTSIZE + sizeof(packet_len));
Simon Glass246d6932011-02-16 11:14:34 -0800473
474 debug("** %s(), len %d\n", __func__, length);
475
476 packet_len = (((length) ^ 0x0000ffff) << 16) + (length);
477 cpu_to_le32s(&packet_len);
478
479 memcpy(msg, &packet_len, sizeof(packet_len));
480 memcpy(msg + sizeof(packet_len), (void *)packet, length);
Simon Glass246d6932011-02-16 11:14:34 -0800481
482 err = usb_bulk_msg(dev->pusb_dev,
483 usb_sndbulkpipe(dev->pusb_dev, dev->ep_out),
484 (void *)msg,
485 length + sizeof(packet_len),
486 &actual_len,
487 USB_BULK_SEND_TIMEOUT);
Thierry Reding7306e9c2015-03-20 12:41:23 +0100488 debug("Tx: len = %zu, actual = %u, err = %d\n",
Simon Glass246d6932011-02-16 11:14:34 -0800489 length + sizeof(packet_len), actual_len, err);
490
491 return err;
492}
493
Simon Glassf58369f2015-07-06 16:47:54 -0600494#ifndef CONFIG_DM_ETH
495/*
496 * Asix callbacks
497 */
498static int asix_init(struct eth_device *eth, bd_t *bd)
499{
500 struct ueth_data *dev = (struct ueth_data *)eth->priv;
501
Marcel Ziswiler7bbbb3c2015-08-05 17:16:59 +0200502 return asix_init_common(dev, eth->enetaddr);
Simon Glassf58369f2015-07-06 16:47:54 -0600503}
504
505static int asix_send(struct eth_device *eth, void *packet, int length)
506{
507 struct ueth_data *dev = (struct ueth_data *)eth->priv;
508
509 return asix_send_common(dev, packet, length);
510}
511
Simon Glass246d6932011-02-16 11:14:34 -0800512static int asix_recv(struct eth_device *eth)
513{
514 struct ueth_data *dev = (struct ueth_data *)eth->priv;
Marek Vasut115436e2012-06-24 14:17:56 +0000515 ALLOC_CACHE_ALIGN_BUFFER(unsigned char, recv_buf, AX_RX_URB_SIZE);
Simon Glass246d6932011-02-16 11:14:34 -0800516 unsigned char *buf_ptr;
517 int err;
518 int actual_len;
519 u32 packet_len;
520
521 debug("** %s()\n", __func__);
522
523 err = usb_bulk_msg(dev->pusb_dev,
524 usb_rcvbulkpipe(dev->pusb_dev, dev->ep_in),
525 (void *)recv_buf,
526 AX_RX_URB_SIZE,
527 &actual_len,
528 USB_BULK_RECV_TIMEOUT);
529 debug("Rx: len = %u, actual = %u, err = %d\n", AX_RX_URB_SIZE,
530 actual_len, err);
531 if (err != 0) {
532 debug("Rx: failed to receive\n");
533 return -1;
534 }
535 if (actual_len > AX_RX_URB_SIZE) {
536 debug("Rx: received too many bytes %d\n", actual_len);
537 return -1;
538 }
539
540 buf_ptr = recv_buf;
541 while (actual_len > 0) {
542 /*
543 * 1st 4 bytes contain the length of the actual data as two
544 * complementary 16-bit words. Extract the length of the data.
545 */
546 if (actual_len < sizeof(packet_len)) {
547 debug("Rx: incomplete packet length\n");
548 return -1;
549 }
550 memcpy(&packet_len, buf_ptr, sizeof(packet_len));
551 le32_to_cpus(&packet_len);
Lucas Stach06e46932012-08-22 11:05:01 +0000552 if (((~packet_len >> 16) & 0x7ff) != (packet_len & 0x7ff)) {
Simon Glass246d6932011-02-16 11:14:34 -0800553 debug("Rx: malformed packet length: %#x (%#x:%#x)\n",
Lucas Stach06e46932012-08-22 11:05:01 +0000554 packet_len, (~packet_len >> 16) & 0x7ff,
555 packet_len & 0x7ff);
Simon Glass246d6932011-02-16 11:14:34 -0800556 return -1;
557 }
Lucas Stach06e46932012-08-22 11:05:01 +0000558 packet_len = packet_len & 0x7ff;
Simon Glass246d6932011-02-16 11:14:34 -0800559 if (packet_len > actual_len - sizeof(packet_len)) {
560 debug("Rx: too large packet: %d\n", packet_len);
561 return -1;
562 }
563
564 /* Notify net stack */
Joe Hershberger9f09a362015-04-08 01:41:06 -0500565 net_process_received_packet(buf_ptr + sizeof(packet_len),
566 packet_len);
Simon Glass246d6932011-02-16 11:14:34 -0800567
568 /* Adjust for next iteration. Packets are padded to 16-bits */
569 if (packet_len & 1)
570 packet_len++;
571 actual_len -= sizeof(packet_len) + packet_len;
572 buf_ptr += sizeof(packet_len) + packet_len;
573 }
574
575 return err;
576}
577
578static void asix_halt(struct eth_device *eth)
579{
580 debug("** %s()\n", __func__);
581}
582
Simon Glassf58369f2015-07-06 16:47:54 -0600583static int asix_write_hwaddr(struct eth_device *eth)
584{
585 struct ueth_data *dev = (struct ueth_data *)eth->priv;
586
587 return asix_write_hwaddr_common(dev, eth->enetaddr);
588}
589
Simon Glass246d6932011-02-16 11:14:34 -0800590/*
591 * Asix probing functions
592 */
593void asix_eth_before_probe(void)
594{
595 curr_eth_dev = 0;
596}
597
598struct asix_dongle {
599 unsigned short vendor;
600 unsigned short product;
Lucas Stachfc9a7582012-08-22 11:04:59 +0000601 int flags;
Simon Glass246d6932011-02-16 11:14:34 -0800602};
603
Jeroen Hofstee5528f4c2014-06-17 21:14:17 +0200604static const struct asix_dongle asix_dongles[] = {
Lucas Stachfc9a7582012-08-22 11:04:59 +0000605 { 0x05ac, 0x1402, FLAG_TYPE_AX88772 }, /* Apple USB Ethernet Adapter */
606 { 0x07d1, 0x3c05, FLAG_TYPE_AX88772 }, /* D-Link DUB-E100 H/W Ver B1 */
Pierre Aubert75caa352013-06-27 09:01:54 +0200607 { 0x2001, 0x1a02, FLAG_TYPE_AX88772 }, /* D-Link DUB-E100 H/W Ver C1 */
Lucas Stachfc9a7582012-08-22 11:04:59 +0000608 /* Cables-to-Go USB Ethernet Adapter */
609 { 0x0b95, 0x772a, FLAG_TYPE_AX88772 },
610 { 0x0b95, 0x7720, FLAG_TYPE_AX88772 }, /* Trendnet TU2-ET100 V3.0R */
611 { 0x0b95, 0x1720, FLAG_TYPE_AX88172 }, /* SMC */
612 { 0x0db0, 0xa877, FLAG_TYPE_AX88772 }, /* MSI - ASIX 88772a */
613 { 0x13b1, 0x0018, FLAG_TYPE_AX88172 }, /* Linksys 200M v2.1 */
614 { 0x1557, 0x7720, FLAG_TYPE_AX88772 }, /* 0Q0 cable ethernet */
615 /* DLink DUB-E100 H/W Ver B1 Alternate */
616 { 0x2001, 0x3c05, FLAG_TYPE_AX88772 },
Lucas Stach06e46932012-08-22 11:05:01 +0000617 /* ASIX 88772B */
618 { 0x0b95, 0x772b, FLAG_TYPE_AX88772B | FLAG_EEPROM_MAC },
Alexandre Courbotceeb40c2014-10-09 12:43:30 +0900619 { 0x0b95, 0x7e2b, FLAG_TYPE_AX88772B },
Lucas Stachfc9a7582012-08-22 11:04:59 +0000620 { 0x0000, 0x0000, FLAG_NONE } /* END - Do not remove */
Simon Glass246d6932011-02-16 11:14:34 -0800621};
622
623/* Probe to see if a new device is actually an asix device */
624int asix_eth_probe(struct usb_device *dev, unsigned int ifnum,
625 struct ueth_data *ss)
626{
627 struct usb_interface *iface;
628 struct usb_interface_descriptor *iface_desc;
Lucas Stach06e46932012-08-22 11:05:01 +0000629 int ep_in_found = 0, ep_out_found = 0;
Simon Glass246d6932011-02-16 11:14:34 -0800630 int i;
631
632 /* let's examine the device now */
633 iface = &dev->config.if_desc[ifnum];
634 iface_desc = &dev->config.if_desc[ifnum].desc;
635
636 for (i = 0; asix_dongles[i].vendor != 0; i++) {
637 if (dev->descriptor.idVendor == asix_dongles[i].vendor &&
638 dev->descriptor.idProduct == asix_dongles[i].product)
639 /* Found a supported dongle */
640 break;
641 }
642
643 if (asix_dongles[i].vendor == 0)
644 return 0;
645
646 memset(ss, 0, sizeof(struct ueth_data));
647
648 /* At this point, we know we've got a live one */
649 debug("\n\nUSB Ethernet device detected: %#04x:%#04x\n",
650 dev->descriptor.idVendor, dev->descriptor.idProduct);
651
652 /* Initialize the ueth_data structure with some useful info */
653 ss->ifnum = ifnum;
654 ss->pusb_dev = dev;
655 ss->subclass = iface_desc->bInterfaceSubClass;
656 ss->protocol = iface_desc->bInterfaceProtocol;
657
Lucas Stachfc9a7582012-08-22 11:04:59 +0000658 /* alloc driver private */
659 ss->dev_priv = calloc(1, sizeof(struct asix_private));
660 if (!ss->dev_priv)
661 return 0;
662
663 ((struct asix_private *)ss->dev_priv)->flags = asix_dongles[i].flags;
664
Simon Glass246d6932011-02-16 11:14:34 -0800665 /*
666 * We are expecting a minimum of 3 endpoints - in, out (bulk), and
667 * int. We will ignore any others.
668 */
669 for (i = 0; i < iface_desc->bNumEndpoints; i++) {
670 /* is it an BULK endpoint? */
671 if ((iface->ep_desc[i].bmAttributes &
672 USB_ENDPOINT_XFERTYPE_MASK) == USB_ENDPOINT_XFER_BULK) {
Lucas Stach06e46932012-08-22 11:05:01 +0000673 u8 ep_addr = iface->ep_desc[i].bEndpointAddress;
674 if (ep_addr & USB_DIR_IN) {
675 if (!ep_in_found) {
676 ss->ep_in = ep_addr &
677 USB_ENDPOINT_NUMBER_MASK;
678 ep_in_found = 1;
679 }
680 } else {
681 if (!ep_out_found) {
682 ss->ep_out = ep_addr &
683 USB_ENDPOINT_NUMBER_MASK;
684 ep_out_found = 1;
685 }
686 }
Simon Glass246d6932011-02-16 11:14:34 -0800687 }
688
689 /* is it an interrupt endpoint? */
690 if ((iface->ep_desc[i].bmAttributes &
691 USB_ENDPOINT_XFERTYPE_MASK) == USB_ENDPOINT_XFER_INT) {
692 ss->ep_int = iface->ep_desc[i].bEndpointAddress &
693 USB_ENDPOINT_NUMBER_MASK;
694 ss->irqinterval = iface->ep_desc[i].bInterval;
695 }
696 }
697 debug("Endpoints In %d Out %d Int %d\n",
698 ss->ep_in, ss->ep_out, ss->ep_int);
699
700 /* Do some basic sanity checks, and bail if we find a problem */
701 if (usb_set_interface(dev, iface_desc->bInterfaceNumber, 0) ||
702 !ss->ep_in || !ss->ep_out || !ss->ep_int) {
703 debug("Problems with device\n");
704 return 0;
705 }
706 dev->privptr = (void *)ss;
707 return 1;
708}
709
710int asix_eth_get_info(struct usb_device *dev, struct ueth_data *ss,
711 struct eth_device *eth)
712{
Lucas Stachfc9a7582012-08-22 11:04:59 +0000713 struct asix_private *priv = (struct asix_private *)ss->dev_priv;
714
Simon Glass246d6932011-02-16 11:14:34 -0800715 if (!eth) {
716 debug("%s: missing parameter.\n", __func__);
717 return 0;
718 }
719 sprintf(eth->name, "%s%d", ASIX_BASE_NAME, curr_eth_dev++);
720 eth->init = asix_init;
721 eth->send = asix_send;
722 eth->recv = asix_recv;
723 eth->halt = asix_halt;
Lucas Stachfc9a7582012-08-22 11:04:59 +0000724 if (!(priv->flags & FLAG_TYPE_AX88172))
725 eth->write_hwaddr = asix_write_hwaddr;
Simon Glass246d6932011-02-16 11:14:34 -0800726 eth->priv = ss;
727
Lucas Stacha0d5d032012-08-22 11:04:58 +0000728 if (asix_basic_reset(ss))
729 return 0;
Lucas Stach95c359b2012-08-22 11:05:00 +0000730
731 /* Get the MAC address */
Simon Glassf58369f2015-07-06 16:47:54 -0600732 if (asix_read_mac_common(ss, priv, eth->enetaddr))
Lucas Stach95c359b2012-08-22 11:05:00 +0000733 return 0;
734 debug("MAC %pM\n", eth->enetaddr);
Lucas Stacha0d5d032012-08-22 11:04:58 +0000735
Simon Glass246d6932011-02-16 11:14:34 -0800736 return 1;
737}
Simon Glassf58369f2015-07-06 16:47:54 -0600738#endif
739
740#ifdef CONFIG_DM_ETH
741static int asix_eth_start(struct udevice *dev)
742{
Marcel Ziswiler7bbbb3c2015-08-05 17:16:59 +0200743 struct eth_pdata *pdata = dev_get_platdata(dev);
Simon Glassf58369f2015-07-06 16:47:54 -0600744 struct asix_private *priv = dev_get_priv(dev);
745
Marcel Ziswiler7bbbb3c2015-08-05 17:16:59 +0200746 return asix_init_common(&priv->ueth, pdata->enetaddr);
Simon Glassf58369f2015-07-06 16:47:54 -0600747}
748
749void asix_eth_stop(struct udevice *dev)
750{
751 debug("** %s()\n", __func__);
752}
753
754int asix_eth_send(struct udevice *dev, void *packet, int length)
755{
756 struct asix_private *priv = dev_get_priv(dev);
757
758 return asix_send_common(&priv->ueth, packet, length);
759}
760
761int asix_eth_recv(struct udevice *dev, int flags, uchar **packetp)
762{
763 struct asix_private *priv = dev_get_priv(dev);
764 struct ueth_data *ueth = &priv->ueth;
765 uint8_t *ptr;
766 int ret, len;
767 u32 packet_len;
768
769 len = usb_ether_get_rx_bytes(ueth, &ptr);
770 debug("%s: first try, len=%d\n", __func__, len);
771 if (!len) {
772 if (!(flags & ETH_RECV_CHECK_DEVICE))
773 return -EAGAIN;
774 ret = usb_ether_receive(ueth, AX_RX_URB_SIZE);
775 if (ret == -EAGAIN)
776 return ret;
777
778 len = usb_ether_get_rx_bytes(ueth, &ptr);
779 debug("%s: second try, len=%d\n", __func__, len);
780 }
781
782 /*
783 * 1st 4 bytes contain the length of the actual data as two
784 * complementary 16-bit words. Extract the length of the data.
785 */
786 if (len < sizeof(packet_len)) {
787 debug("Rx: incomplete packet length\n");
788 goto err;
789 }
790 memcpy(&packet_len, ptr, sizeof(packet_len));
791 le32_to_cpus(&packet_len);
792 if (((~packet_len >> 16) & 0x7ff) != (packet_len & 0x7ff)) {
793 debug("Rx: malformed packet length: %#x (%#x:%#x)\n",
794 packet_len, (~packet_len >> 16) & 0x7ff,
795 packet_len & 0x7ff);
796 goto err;
797 }
798 packet_len = packet_len & 0x7ff;
799 if (packet_len > len - sizeof(packet_len)) {
800 debug("Rx: too large packet: %d\n", packet_len);
801 goto err;
802 }
803
804 *packetp = ptr + sizeof(packet_len);
805 return packet_len;
806
807err:
808 usb_ether_advance_rxbuf(ueth, -1);
809 return -EINVAL;
810}
811
812static int asix_free_pkt(struct udevice *dev, uchar *packet, int packet_len)
813{
814 struct asix_private *priv = dev_get_priv(dev);
815
816 if (packet_len & 1)
817 packet_len++;
818 usb_ether_advance_rxbuf(&priv->ueth, sizeof(u32) + packet_len);
819
820 return 0;
821}
822
823int asix_write_hwaddr(struct udevice *dev)
824{
825 struct eth_pdata *pdata = dev_get_platdata(dev);
826 struct asix_private *priv = dev_get_priv(dev);
827
828 if (priv->flags & FLAG_TYPE_AX88172)
829 return -ENOSYS;
830
831 return asix_write_hwaddr_common(&priv->ueth, pdata->enetaddr);
832}
833
834static int asix_eth_probe(struct udevice *dev)
835{
836 struct eth_pdata *pdata = dev_get_platdata(dev);
837 struct asix_private *priv = dev_get_priv(dev);
838 struct ueth_data *ss = &priv->ueth;
839 int ret;
840
841 priv->flags = dev->driver_data;
842 ret = usb_ether_register(dev, ss, AX_RX_URB_SIZE);
843 if (ret)
844 return ret;
845
846 ret = asix_basic_reset(ss);
847 if (ret)
848 goto err;
849
850 /* Get the MAC address */
851 ret = asix_read_mac_common(ss, priv, pdata->enetaddr);
852 if (ret)
853 goto err;
854 debug("MAC %pM\n", pdata->enetaddr);
855
856 return 0;
857
858err:
859 return usb_ether_deregister(ss);
860}
861
862static const struct eth_ops asix_eth_ops = {
863 .start = asix_eth_start,
864 .send = asix_eth_send,
865 .recv = asix_eth_recv,
866 .free_pkt = asix_free_pkt,
867 .stop = asix_eth_stop,
868 .write_hwaddr = asix_write_hwaddr,
869};
870
871U_BOOT_DRIVER(asix_eth) = {
872 .name = "asix_eth",
873 .id = UCLASS_ETH,
874 .probe = asix_eth_probe,
875 .ops = &asix_eth_ops,
876 .priv_auto_alloc_size = sizeof(struct asix_private),
877 .platdata_auto_alloc_size = sizeof(struct eth_pdata),
878};
879
880static const struct usb_device_id asix_eth_id_table[] = {
881 /* Apple USB Ethernet Adapter */
882 { USB_DEVICE(0x05ac, 0x1402), .driver_info = FLAG_TYPE_AX88772 },
883 /* D-Link DUB-E100 H/W Ver B1 */
884 { USB_DEVICE(0x07d1, 0x3c05), .driver_info = FLAG_TYPE_AX88772 },
885 /* D-Link DUB-E100 H/W Ver C1 */
886 { USB_DEVICE(0x2001, 0x1a02), .driver_info = FLAG_TYPE_AX88772 },
887 /* Cables-to-Go USB Ethernet Adapter */
888 { USB_DEVICE(0x0b95, 0x772a), .driver_info = FLAG_TYPE_AX88772 },
889 /* Trendnet TU2-ET100 V3.0R */
890 { USB_DEVICE(0x0b95, 0x7720), .driver_info = FLAG_TYPE_AX88772 },
891 /* SMC */
892 { USB_DEVICE(0x0b95, 0x1720), .driver_info = FLAG_TYPE_AX88172 },
893 /* MSI - ASIX 88772a */
894 { USB_DEVICE(0x0db0, 0xa877), .driver_info = FLAG_TYPE_AX88772 },
895 /* Linksys 200M v2.1 */
896 { USB_DEVICE(0x13b1, 0x0018), .driver_info = FLAG_TYPE_AX88172 },
897 /* 0Q0 cable ethernet */
898 { USB_DEVICE(0x1557, 0x7720), .driver_info = FLAG_TYPE_AX88772 },
899 /* DLink DUB-E100 H/W Ver B1 Alternate */
900 { USB_DEVICE(0x2001, 0x3c05), .driver_info = FLAG_TYPE_AX88772 },
901 /* ASIX 88772B */
902 { USB_DEVICE(0x0b95, 0x772b),
903 .driver_info = FLAG_TYPE_AX88772B | FLAG_EEPROM_MAC },
904 { USB_DEVICE(0x0b95, 0x7e2b), .driver_info = FLAG_TYPE_AX88772B },
905 { } /* Terminating entry */
906};
907
908U_BOOT_USB_DEVICE(asix_eth, asix_eth_id_table);
909#endif