blob: 40a6e9291dea4f46c2f024d705ac65ecee0dc9b3 [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
Simon Glassf58369f2015-07-06 16:47:54 -06008#include <dm.h>
Simon Glass0f2af882020-05-10 11:40:05 -06009#include <log.h>
Simon Glass274e0b02020-05-10 11:39:56 -060010#include <net.h>
Simon Glass246d6932011-02-16 11:14:34 -080011#include <usb.h>
Simon Glassf58369f2015-07-06 16:47:54 -060012#include <malloc.h>
Simon Glass2dd337a2015-09-02 17:24:58 -060013#include <memalign.h>
Simon Glassdbd79542020-05-10 11:40:11 -060014#include <linux/delay.h>
Simon Glass246d6932011-02-16 11:14:34 -080015#include <linux/mii.h>
16#include "usb_ether.h"
Simon Glass246d6932011-02-16 11:14:34 -080017
18/* ASIX AX8817X based USB 2.0 Ethernet Devices */
19
20#define AX_CMD_SET_SW_MII 0x06
21#define AX_CMD_READ_MII_REG 0x07
22#define AX_CMD_WRITE_MII_REG 0x08
23#define AX_CMD_SET_HW_MII 0x0a
Lucas Stach95c359b2012-08-22 11:05:00 +000024#define AX_CMD_READ_EEPROM 0x0b
Simon Glass246d6932011-02-16 11:14:34 -080025#define AX_CMD_READ_RX_CTL 0x0f
26#define AX_CMD_WRITE_RX_CTL 0x10
27#define AX_CMD_WRITE_IPG0 0x12
28#define AX_CMD_READ_NODE_ID 0x13
Lucas Stachfc9a7582012-08-22 11:04:59 +000029#define AX_CMD_WRITE_NODE_ID 0x14
Simon Glass246d6932011-02-16 11:14:34 -080030#define AX_CMD_READ_PHY_ID 0x19
31#define AX_CMD_WRITE_MEDIUM_MODE 0x1b
32#define AX_CMD_WRITE_GPIOS 0x1f
33#define AX_CMD_SW_RESET 0x20
34#define AX_CMD_SW_PHY_SELECT 0x22
35
36#define AX_SWRESET_CLEAR 0x00
37#define AX_SWRESET_PRTE 0x04
38#define AX_SWRESET_PRL 0x08
39#define AX_SWRESET_IPRL 0x20
40#define AX_SWRESET_IPPD 0x40
41
42#define AX88772_IPG0_DEFAULT 0x15
43#define AX88772_IPG1_DEFAULT 0x0c
44#define AX88772_IPG2_DEFAULT 0x12
45
46/* AX88772 & AX88178 Medium Mode Register */
47#define AX_MEDIUM_PF 0x0080
48#define AX_MEDIUM_JFE 0x0040
49#define AX_MEDIUM_TFC 0x0020
50#define AX_MEDIUM_RFC 0x0010
51#define AX_MEDIUM_ENCK 0x0008
52#define AX_MEDIUM_AC 0x0004
53#define AX_MEDIUM_FD 0x0002
54#define AX_MEDIUM_GM 0x0001
55#define AX_MEDIUM_SM 0x1000
56#define AX_MEDIUM_SBP 0x0800
57#define AX_MEDIUM_PS 0x0200
58#define AX_MEDIUM_RE 0x0100
59
60#define AX88178_MEDIUM_DEFAULT \
61 (AX_MEDIUM_PS | AX_MEDIUM_FD | AX_MEDIUM_AC | \
62 AX_MEDIUM_RFC | AX_MEDIUM_TFC | AX_MEDIUM_JFE | \
63 AX_MEDIUM_RE)
64
65#define AX88772_MEDIUM_DEFAULT \
66 (AX_MEDIUM_FD | AX_MEDIUM_RFC | \
67 AX_MEDIUM_TFC | AX_MEDIUM_PS | \
68 AX_MEDIUM_AC | AX_MEDIUM_RE)
69
70/* AX88772 & AX88178 RX_CTL values */
Alban Bedel3cf1a962016-09-10 03:54:09 +020071#define AX_RX_CTL_SO 0x0080
72#define AX_RX_CTL_AB 0x0008
Simon Glass246d6932011-02-16 11:14:34 -080073
74#define AX_DEFAULT_RX_CTL \
75 (AX_RX_CTL_SO | AX_RX_CTL_AB)
76
77/* GPIO 2 toggles */
78#define AX_GPIO_GPO2EN 0x10 /* GPIO2 Output enable */
79#define AX_GPIO_GPO_2 0x20 /* GPIO2 Output value */
80#define AX_GPIO_RSE 0x80 /* Reload serial EEPROM */
81
82/* local defines */
83#define ASIX_BASE_NAME "asx"
84#define USB_CTRL_SET_TIMEOUT 5000
85#define USB_CTRL_GET_TIMEOUT 5000
86#define USB_BULK_SEND_TIMEOUT 5000
87#define USB_BULK_RECV_TIMEOUT 5000
88
89#define AX_RX_URB_SIZE 2048
90#define PHY_CONNECT_TIMEOUT 5000
91
Lucas Stachfc9a7582012-08-22 11:04:59 +000092/* asix_flags defines */
93#define FLAG_NONE 0
94#define FLAG_TYPE_AX88172 (1U << 0)
95#define FLAG_TYPE_AX88772 (1U << 1)
Lucas Stach06e46932012-08-22 11:05:01 +000096#define FLAG_TYPE_AX88772B (1U << 2)
97#define FLAG_EEPROM_MAC (1U << 3) /* initial mac address in eeprom */
Lucas Stachfc9a7582012-08-22 11:04:59 +000098
Lucas Stachfc9a7582012-08-22 11:04:59 +000099/* driver private */
100struct asix_private {
101 int flags;
Simon Glassf58369f2015-07-06 16:47:54 -0600102 struct ueth_data ueth;
Lucas Stachfc9a7582012-08-22 11:04:59 +0000103};
104
Simon Glass246d6932011-02-16 11:14:34 -0800105/*
106 * Asix infrastructure commands
107 */
108static int asix_write_cmd(struct ueth_data *dev, u8 cmd, u16 value, u16 index,
109 u16 size, void *data)
110{
111 int len;
112
113 debug("asix_write_cmd() cmd=0x%02x value=0x%04x index=0x%04x "
114 "size=%d\n", cmd, value, index, size);
115
116 len = usb_control_msg(
117 dev->pusb_dev,
118 usb_sndctrlpipe(dev->pusb_dev, 0),
119 cmd,
120 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
121 value,
122 index,
123 data,
124 size,
125 USB_CTRL_SET_TIMEOUT);
126
127 return len == size ? 0 : -1;
128}
129
130static int asix_read_cmd(struct ueth_data *dev, u8 cmd, u16 value, u16 index,
131 u16 size, void *data)
132{
133 int len;
134
135 debug("asix_read_cmd() cmd=0x%02x value=0x%04x index=0x%04x size=%d\n",
136 cmd, value, index, size);
137
138 len = usb_control_msg(
139 dev->pusb_dev,
140 usb_rcvctrlpipe(dev->pusb_dev, 0),
141 cmd,
142 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
143 value,
144 index,
145 data,
146 size,
147 USB_CTRL_GET_TIMEOUT);
148 return len == size ? 0 : -1;
149}
150
151static inline int asix_set_sw_mii(struct ueth_data *dev)
152{
153 int ret;
154
155 ret = asix_write_cmd(dev, AX_CMD_SET_SW_MII, 0x0000, 0, 0, NULL);
156 if (ret < 0)
157 debug("Failed to enable software MII access\n");
158 return ret;
159}
160
161static inline int asix_set_hw_mii(struct ueth_data *dev)
162{
163 int ret;
164
165 ret = asix_write_cmd(dev, AX_CMD_SET_HW_MII, 0x0000, 0, 0, NULL);
166 if (ret < 0)
167 debug("Failed to enable hardware MII access\n");
168 return ret;
169}
170
171static int asix_mdio_read(struct ueth_data *dev, int phy_id, int loc)
172{
Marek Vasut115436e2012-06-24 14:17:56 +0000173 ALLOC_CACHE_ALIGN_BUFFER(__le16, res, 1);
Simon Glass246d6932011-02-16 11:14:34 -0800174
175 asix_set_sw_mii(dev);
Marek Vasut115436e2012-06-24 14:17:56 +0000176 asix_read_cmd(dev, AX_CMD_READ_MII_REG, phy_id, (__u16)loc, 2, res);
Simon Glass246d6932011-02-16 11:14:34 -0800177 asix_set_hw_mii(dev);
178
179 debug("asix_mdio_read() phy_id=0x%02x, loc=0x%02x, returns=0x%04x\n",
Marek Vasut115436e2012-06-24 14:17:56 +0000180 phy_id, loc, le16_to_cpu(*res));
Simon Glass246d6932011-02-16 11:14:34 -0800181
Marek Vasut115436e2012-06-24 14:17:56 +0000182 return le16_to_cpu(*res);
Simon Glass246d6932011-02-16 11:14:34 -0800183}
184
185static void
186asix_mdio_write(struct ueth_data *dev, int phy_id, int loc, int val)
187{
Marek Vasut115436e2012-06-24 14:17:56 +0000188 ALLOC_CACHE_ALIGN_BUFFER(__le16, res, 1);
189 *res = cpu_to_le16(val);
Simon Glass246d6932011-02-16 11:14:34 -0800190
191 debug("asix_mdio_write() phy_id=0x%02x, loc=0x%02x, val=0x%04x\n",
192 phy_id, loc, val);
193 asix_set_sw_mii(dev);
Marek Vasut115436e2012-06-24 14:17:56 +0000194 asix_write_cmd(dev, AX_CMD_WRITE_MII_REG, phy_id, (__u16)loc, 2, res);
Simon Glass246d6932011-02-16 11:14:34 -0800195 asix_set_hw_mii(dev);
196}
197
198/*
199 * Asix "high level" commands
200 */
201static int asix_sw_reset(struct ueth_data *dev, u8 flags)
202{
203 int ret;
204
205 ret = asix_write_cmd(dev, AX_CMD_SW_RESET, flags, 0, 0, NULL);
206 if (ret < 0)
207 debug("Failed to send software reset: %02x\n", ret);
208 else
209 udelay(150 * 1000);
210
211 return ret;
212}
213
214static inline int asix_get_phy_addr(struct ueth_data *dev)
215{
Marek Vasut115436e2012-06-24 14:17:56 +0000216 ALLOC_CACHE_ALIGN_BUFFER(u8, buf, 2);
217
Simon Glass246d6932011-02-16 11:14:34 -0800218 int ret = asix_read_cmd(dev, AX_CMD_READ_PHY_ID, 0, 0, 2, buf);
219
220 debug("asix_get_phy_addr()\n");
221
222 if (ret < 0) {
223 debug("Error reading PHYID register: %02x\n", ret);
224 goto out;
225 }
Marek Vasuta61b9662011-09-23 21:13:35 +0200226 debug("asix_get_phy_addr() returning 0x%02x%02x\n", buf[0], buf[1]);
Simon Glass246d6932011-02-16 11:14:34 -0800227 ret = buf[1];
228
229out:
230 return ret;
231}
232
233static int asix_write_medium_mode(struct ueth_data *dev, u16 mode)
234{
235 int ret;
236
237 debug("asix_write_medium_mode() - mode = 0x%04x\n", mode);
238 ret = asix_write_cmd(dev, AX_CMD_WRITE_MEDIUM_MODE, mode,
239 0, 0, NULL);
240 if (ret < 0) {
241 debug("Failed to write Medium Mode mode to 0x%04x: %02x\n",
242 mode, ret);
243 }
244 return ret;
245}
246
247static u16 asix_read_rx_ctl(struct ueth_data *dev)
248{
Marek Vasut115436e2012-06-24 14:17:56 +0000249 ALLOC_CACHE_ALIGN_BUFFER(__le16, v, 1);
250
251 int ret = asix_read_cmd(dev, AX_CMD_READ_RX_CTL, 0, 0, 2, v);
Simon Glass246d6932011-02-16 11:14:34 -0800252
253 if (ret < 0)
254 debug("Error reading RX_CTL register: %02x\n", ret);
255 else
Marek Vasut115436e2012-06-24 14:17:56 +0000256 ret = le16_to_cpu(*v);
Simon Glass246d6932011-02-16 11:14:34 -0800257 return ret;
258}
259
260static int asix_write_rx_ctl(struct ueth_data *dev, u16 mode)
261{
262 int ret;
263
264 debug("asix_write_rx_ctl() - mode = 0x%04x\n", mode);
265 ret = asix_write_cmd(dev, AX_CMD_WRITE_RX_CTL, mode, 0, 0, NULL);
266 if (ret < 0) {
267 debug("Failed to write RX_CTL mode to 0x%04x: %02x\n",
268 mode, ret);
269 }
270 return ret;
271}
272
273static int asix_write_gpio(struct ueth_data *dev, u16 value, int sleep)
274{
275 int ret;
276
277 debug("asix_write_gpio() - value = 0x%04x\n", value);
278 ret = asix_write_cmd(dev, AX_CMD_WRITE_GPIOS, value, 0, 0, NULL);
279 if (ret < 0) {
280 debug("Failed to write GPIO value 0x%04x: %02x\n",
281 value, ret);
282 }
283 if (sleep)
284 udelay(sleep * 1000);
285
286 return ret;
287}
288
Simon Glassf58369f2015-07-06 16:47:54 -0600289static int asix_write_hwaddr_common(struct ueth_data *dev, uint8_t *enetaddr)
Lucas Stachfc9a7582012-08-22 11:04:59 +0000290{
Lucas Stachfc9a7582012-08-22 11:04:59 +0000291 int ret;
292 ALLOC_CACHE_ALIGN_BUFFER(unsigned char, buf, ETH_ALEN);
293
Simon Glassf58369f2015-07-06 16:47:54 -0600294 memcpy(buf, enetaddr, ETH_ALEN);
Lucas Stachfc9a7582012-08-22 11:04:59 +0000295
296 ret = asix_write_cmd(dev, AX_CMD_WRITE_NODE_ID, 0, 0, ETH_ALEN, buf);
297 if (ret < 0)
298 debug("Failed to set MAC address: %02x\n", ret);
299
300 return ret;
301}
302
Simon Glass246d6932011-02-16 11:14:34 -0800303/*
304 * mii commands
305 */
306
307/*
308 * mii_nway_restart - restart NWay (autonegotiation) for this interface
309 *
310 * Returns 0 on success, negative on error.
311 */
312static int mii_nway_restart(struct ueth_data *dev)
313{
314 int bmcr;
315 int r = -1;
316
317 /* if autoneg is off, it's an error */
318 bmcr = asix_mdio_read(dev, dev->phy_id, MII_BMCR);
319
320 if (bmcr & BMCR_ANENABLE) {
321 bmcr |= BMCR_ANRESTART;
322 asix_mdio_write(dev, dev->phy_id, MII_BMCR, bmcr);
323 r = 0;
324 }
325
326 return r;
327}
328
Simon Glassf58369f2015-07-06 16:47:54 -0600329static int asix_read_mac_common(struct ueth_data *dev,
330 struct asix_private *priv, uint8_t *enetaddr)
Lucas Stach95c359b2012-08-22 11:05:00 +0000331{
Lucas Stach95c359b2012-08-22 11:05:00 +0000332 ALLOC_CACHE_ALIGN_BUFFER(unsigned char, buf, ETH_ALEN);
Simon Glassf58369f2015-07-06 16:47:54 -0600333 int i;
Lucas Stach95c359b2012-08-22 11:05:00 +0000334
335 if (priv->flags & FLAG_EEPROM_MAC) {
336 for (i = 0; i < (ETH_ALEN >> 1); i++) {
337 if (asix_read_cmd(dev, AX_CMD_READ_EEPROM,
338 0x04 + i, 0, 2, buf) < 0) {
339 debug("Failed to read SROM address 04h.\n");
340 return -1;
341 }
Simon Glassf58369f2015-07-06 16:47:54 -0600342 memcpy(enetaddr + i * 2, buf, 2);
Lucas Stach95c359b2012-08-22 11:05:00 +0000343 }
344 } else {
345 if (asix_read_cmd(dev, AX_CMD_READ_NODE_ID, 0, 0, ETH_ALEN, buf)
346 < 0) {
347 debug("Failed to read MAC address.\n");
348 return -1;
349 }
Simon Glassf58369f2015-07-06 16:47:54 -0600350 memcpy(enetaddr, buf, ETH_ALEN);
Lucas Stach95c359b2012-08-22 11:05:00 +0000351 }
352
353 return 0;
354}
355
Lucas Stacha0d5d032012-08-22 11:04:58 +0000356static int asix_basic_reset(struct ueth_data *dev)
Simon Glass246d6932011-02-16 11:14:34 -0800357{
358 int embd_phy;
Simon Glass246d6932011-02-16 11:14:34 -0800359 u16 rx_ctl;
Simon Glass246d6932011-02-16 11:14:34 -0800360
361 if (asix_write_gpio(dev,
362 AX_GPIO_RSE | AX_GPIO_GPO_2 | AX_GPIO_GPO2EN, 5) < 0)
Lucas Stacha0d5d032012-08-22 11:04:58 +0000363 return -1;
Simon Glass246d6932011-02-16 11:14:34 -0800364
365 /* 0x10 is the phy id of the embedded 10/100 ethernet phy */
366 embd_phy = ((asix_get_phy_addr(dev) & 0x1f) == 0x10 ? 1 : 0);
367 if (asix_write_cmd(dev, AX_CMD_SW_PHY_SELECT,
368 embd_phy, 0, 0, NULL) < 0) {
369 debug("Select PHY #1 failed\n");
Lucas Stacha0d5d032012-08-22 11:04:58 +0000370 return -1;
Simon Glass246d6932011-02-16 11:14:34 -0800371 }
372
373 if (asix_sw_reset(dev, AX_SWRESET_IPPD | AX_SWRESET_PRL) < 0)
Lucas Stacha0d5d032012-08-22 11:04:58 +0000374 return -1;
Simon Glass246d6932011-02-16 11:14:34 -0800375
376 if (asix_sw_reset(dev, AX_SWRESET_CLEAR) < 0)
Lucas Stacha0d5d032012-08-22 11:04:58 +0000377 return -1;
Simon Glass246d6932011-02-16 11:14:34 -0800378
379 if (embd_phy) {
380 if (asix_sw_reset(dev, AX_SWRESET_IPRL) < 0)
Lucas Stacha0d5d032012-08-22 11:04:58 +0000381 return -1;
Simon Glass246d6932011-02-16 11:14:34 -0800382 } else {
383 if (asix_sw_reset(dev, AX_SWRESET_PRTE) < 0)
Lucas Stacha0d5d032012-08-22 11:04:58 +0000384 return -1;
Simon Glass246d6932011-02-16 11:14:34 -0800385 }
386
387 rx_ctl = asix_read_rx_ctl(dev);
388 debug("RX_CTL is 0x%04x after software reset\n", rx_ctl);
389 if (asix_write_rx_ctl(dev, 0x0000) < 0)
Lucas Stacha0d5d032012-08-22 11:04:58 +0000390 return -1;
Simon Glass246d6932011-02-16 11:14:34 -0800391
392 rx_ctl = asix_read_rx_ctl(dev);
393 debug("RX_CTL is 0x%04x setting to 0x0000\n", rx_ctl);
394
Simon Glass246d6932011-02-16 11:14:34 -0800395 dev->phy_id = asix_get_phy_addr(dev);
396 if (dev->phy_id < 0)
397 debug("Failed to read phy id\n");
398
Simon Glass246d6932011-02-16 11:14:34 -0800399 asix_mdio_write(dev, dev->phy_id, MII_BMCR, BMCR_RESET);
400 asix_mdio_write(dev, dev->phy_id, MII_ADVERTISE,
401 ADVERTISE_ALL | ADVERTISE_CSMA);
402 mii_nway_restart(dev);
403
404 if (asix_write_medium_mode(dev, AX88772_MEDIUM_DEFAULT) < 0)
Julius Werner160298f2013-05-11 13:35:02 -0700405 return -1;
Simon Glass246d6932011-02-16 11:14:34 -0800406
407 if (asix_write_cmd(dev, AX_CMD_WRITE_IPG0,
408 AX88772_IPG0_DEFAULT | AX88772_IPG1_DEFAULT,
409 AX88772_IPG2_DEFAULT, 0, NULL) < 0) {
410 debug("Write IPG,IPG1,IPG2 failed\n");
Julius Werner160298f2013-05-11 13:35:02 -0700411 return -1;
Simon Glass246d6932011-02-16 11:14:34 -0800412 }
413
Julius Werner160298f2013-05-11 13:35:02 -0700414 return 0;
415}
416
Marcel Ziswiler7bbbb3c2015-08-05 17:16:59 +0200417static int asix_init_common(struct ueth_data *dev, uint8_t *enetaddr)
Julius Werner160298f2013-05-11 13:35:02 -0700418{
Julius Werner160298f2013-05-11 13:35:02 -0700419 int timeout = 0;
420#define TIMEOUT_RESOLUTION 50 /* ms */
421 int link_detected;
422
423 debug("** %s()\n", __func__);
424
Alban Bedel3cf1a962016-09-10 03:54:09 +0200425 if (asix_write_rx_ctl(dev, AX_DEFAULT_RX_CTL) < 0)
Marcel Ziswiler7bbbb3c2015-08-05 17:16:59 +0200426 goto out_err;
427
428 if (asix_write_hwaddr_common(dev, enetaddr) < 0)
Simon Glass246d6932011-02-16 11:14:34 -0800429 goto out_err;
430
431 do {
432 link_detected = asix_mdio_read(dev, dev->phy_id, MII_BMSR) &
433 BMSR_LSTATUS;
434 if (!link_detected) {
435 if (timeout == 0)
436 printf("Waiting for Ethernet connection... ");
437 udelay(TIMEOUT_RESOLUTION * 1000);
438 timeout += TIMEOUT_RESOLUTION;
439 }
440 } while (!link_detected && timeout < PHY_CONNECT_TIMEOUT);
441 if (link_detected) {
442 if (timeout != 0)
443 printf("done.\n");
444 } else {
445 printf("unable to connect.\n");
446 goto out_err;
447 }
448
Marcel Ziswiler7bbbb3c2015-08-05 17:16:59 +0200449 /*
450 * Wait some more to avoid timeout on first transfer
451 * (e.g. EHCI timed out on TD - token=0x8008d80)
452 */
453 mdelay(25);
454
Simon Glass246d6932011-02-16 11:14:34 -0800455 return 0;
456out_err:
457 return -1;
458}
459
Simon Glassf58369f2015-07-06 16:47:54 -0600460static int asix_send_common(struct ueth_data *dev, void *packet, int length)
Simon Glass246d6932011-02-16 11:14:34 -0800461{
Simon Glass246d6932011-02-16 11:14:34 -0800462 int err;
463 u32 packet_len;
464 int actual_len;
Marek Vasut115436e2012-06-24 14:17:56 +0000465 ALLOC_CACHE_ALIGN_BUFFER(unsigned char, msg,
466 PKTSIZE + sizeof(packet_len));
Simon Glass246d6932011-02-16 11:14:34 -0800467
468 debug("** %s(), len %d\n", __func__, length);
469
470 packet_len = (((length) ^ 0x0000ffff) << 16) + (length);
471 cpu_to_le32s(&packet_len);
472
473 memcpy(msg, &packet_len, sizeof(packet_len));
474 memcpy(msg + sizeof(packet_len), (void *)packet, length);
Simon Glass246d6932011-02-16 11:14:34 -0800475
476 err = usb_bulk_msg(dev->pusb_dev,
477 usb_sndbulkpipe(dev->pusb_dev, dev->ep_out),
478 (void *)msg,
479 length + sizeof(packet_len),
480 &actual_len,
481 USB_BULK_SEND_TIMEOUT);
Thierry Reding7306e9c2015-03-20 12:41:23 +0100482 debug("Tx: len = %zu, actual = %u, err = %d\n",
Simon Glass246d6932011-02-16 11:14:34 -0800483 length + sizeof(packet_len), actual_len, err);
484
485 return err;
486}
Simon Glassf58369f2015-07-06 16:47:54 -0600487
Simon Glassf58369f2015-07-06 16:47:54 -0600488static int asix_eth_start(struct udevice *dev)
489{
Simon Glassfa20e932020-12-03 16:55:20 -0700490 struct eth_pdata *pdata = dev_get_plat(dev);
Simon Glassf58369f2015-07-06 16:47:54 -0600491 struct asix_private *priv = dev_get_priv(dev);
492
Marcel Ziswiler7bbbb3c2015-08-05 17:16:59 +0200493 return asix_init_common(&priv->ueth, pdata->enetaddr);
Simon Glassf58369f2015-07-06 16:47:54 -0600494}
495
496void asix_eth_stop(struct udevice *dev)
497{
498 debug("** %s()\n", __func__);
499}
500
501int asix_eth_send(struct udevice *dev, void *packet, int length)
502{
503 struct asix_private *priv = dev_get_priv(dev);
504
505 return asix_send_common(&priv->ueth, packet, length);
506}
507
508int asix_eth_recv(struct udevice *dev, int flags, uchar **packetp)
509{
510 struct asix_private *priv = dev_get_priv(dev);
511 struct ueth_data *ueth = &priv->ueth;
512 uint8_t *ptr;
513 int ret, len;
514 u32 packet_len;
515
516 len = usb_ether_get_rx_bytes(ueth, &ptr);
517 debug("%s: first try, len=%d\n", __func__, len);
518 if (!len) {
519 if (!(flags & ETH_RECV_CHECK_DEVICE))
520 return -EAGAIN;
521 ret = usb_ether_receive(ueth, AX_RX_URB_SIZE);
522 if (ret == -EAGAIN)
523 return ret;
524
525 len = usb_ether_get_rx_bytes(ueth, &ptr);
526 debug("%s: second try, len=%d\n", __func__, len);
527 }
528
529 /*
530 * 1st 4 bytes contain the length of the actual data as two
531 * complementary 16-bit words. Extract the length of the data.
532 */
533 if (len < sizeof(packet_len)) {
534 debug("Rx: incomplete packet length\n");
535 goto err;
536 }
537 memcpy(&packet_len, ptr, sizeof(packet_len));
538 le32_to_cpus(&packet_len);
539 if (((~packet_len >> 16) & 0x7ff) != (packet_len & 0x7ff)) {
540 debug("Rx: malformed packet length: %#x (%#x:%#x)\n",
541 packet_len, (~packet_len >> 16) & 0x7ff,
542 packet_len & 0x7ff);
543 goto err;
544 }
545 packet_len = packet_len & 0x7ff;
546 if (packet_len > len - sizeof(packet_len)) {
547 debug("Rx: too large packet: %d\n", packet_len);
548 goto err;
549 }
550
551 *packetp = ptr + sizeof(packet_len);
552 return packet_len;
553
554err:
555 usb_ether_advance_rxbuf(ueth, -1);
556 return -EINVAL;
557}
558
559static int asix_free_pkt(struct udevice *dev, uchar *packet, int packet_len)
560{
561 struct asix_private *priv = dev_get_priv(dev);
562
563 if (packet_len & 1)
564 packet_len++;
565 usb_ether_advance_rxbuf(&priv->ueth, sizeof(u32) + packet_len);
566
567 return 0;
568}
569
570int asix_write_hwaddr(struct udevice *dev)
571{
Simon Glassfa20e932020-12-03 16:55:20 -0700572 struct eth_pdata *pdata = dev_get_plat(dev);
Simon Glassf58369f2015-07-06 16:47:54 -0600573 struct asix_private *priv = dev_get_priv(dev);
574
575 if (priv->flags & FLAG_TYPE_AX88172)
576 return -ENOSYS;
577
578 return asix_write_hwaddr_common(&priv->ueth, pdata->enetaddr);
579}
580
581static int asix_eth_probe(struct udevice *dev)
582{
Simon Glassfa20e932020-12-03 16:55:20 -0700583 struct eth_pdata *pdata = dev_get_plat(dev);
Simon Glassf58369f2015-07-06 16:47:54 -0600584 struct asix_private *priv = dev_get_priv(dev);
585 struct ueth_data *ss = &priv->ueth;
586 int ret;
587
588 priv->flags = dev->driver_data;
589 ret = usb_ether_register(dev, ss, AX_RX_URB_SIZE);
590 if (ret)
591 return ret;
592
593 ret = asix_basic_reset(ss);
594 if (ret)
595 goto err;
596
597 /* Get the MAC address */
598 ret = asix_read_mac_common(ss, priv, pdata->enetaddr);
599 if (ret)
600 goto err;
601 debug("MAC %pM\n", pdata->enetaddr);
602
603 return 0;
604
605err:
606 return usb_ether_deregister(ss);
607}
608
609static const struct eth_ops asix_eth_ops = {
610 .start = asix_eth_start,
611 .send = asix_eth_send,
612 .recv = asix_eth_recv,
613 .free_pkt = asix_free_pkt,
614 .stop = asix_eth_stop,
615 .write_hwaddr = asix_write_hwaddr,
616};
617
618U_BOOT_DRIVER(asix_eth) = {
619 .name = "asix_eth",
620 .id = UCLASS_ETH,
621 .probe = asix_eth_probe,
622 .ops = &asix_eth_ops,
Simon Glass8a2b47f2020-12-03 16:55:17 -0700623 .priv_auto = sizeof(struct asix_private),
Simon Glass71fa5b42020-12-03 16:55:18 -0700624 .plat_auto = sizeof(struct eth_pdata),
Simon Glassf58369f2015-07-06 16:47:54 -0600625};
626
627static const struct usb_device_id asix_eth_id_table[] = {
628 /* Apple USB Ethernet Adapter */
629 { USB_DEVICE(0x05ac, 0x1402), .driver_info = FLAG_TYPE_AX88772 },
630 /* D-Link DUB-E100 H/W Ver B1 */
631 { USB_DEVICE(0x07d1, 0x3c05), .driver_info = FLAG_TYPE_AX88772 },
632 /* D-Link DUB-E100 H/W Ver C1 */
633 { USB_DEVICE(0x2001, 0x1a02), .driver_info = FLAG_TYPE_AX88772 },
634 /* Cables-to-Go USB Ethernet Adapter */
635 { USB_DEVICE(0x0b95, 0x772a), .driver_info = FLAG_TYPE_AX88772 },
636 /* Trendnet TU2-ET100 V3.0R */
637 { USB_DEVICE(0x0b95, 0x7720), .driver_info = FLAG_TYPE_AX88772 },
638 /* SMC */
639 { USB_DEVICE(0x0b95, 0x1720), .driver_info = FLAG_TYPE_AX88172 },
640 /* MSI - ASIX 88772a */
641 { USB_DEVICE(0x0db0, 0xa877), .driver_info = FLAG_TYPE_AX88772 },
642 /* Linksys 200M v2.1 */
643 { USB_DEVICE(0x13b1, 0x0018), .driver_info = FLAG_TYPE_AX88172 },
644 /* 0Q0 cable ethernet */
645 { USB_DEVICE(0x1557, 0x7720), .driver_info = FLAG_TYPE_AX88772 },
646 /* DLink DUB-E100 H/W Ver B1 Alternate */
647 { USB_DEVICE(0x2001, 0x3c05), .driver_info = FLAG_TYPE_AX88772 },
648 /* ASIX 88772B */
649 { USB_DEVICE(0x0b95, 0x772b),
650 .driver_info = FLAG_TYPE_AX88772B | FLAG_EEPROM_MAC },
651 { USB_DEVICE(0x0b95, 0x7e2b), .driver_info = FLAG_TYPE_AX88772B },
652 { } /* Terminating entry */
653};
654
655U_BOOT_USB_DEVICE(asix_eth, asix_eth_id_table);