blob: c5a01ec922be55cbb5ac6ca21fd7bf6f2a15f844 [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
Simon Glass246d6932011-02-16 11:14:34 -080099
Lucas Stachfc9a7582012-08-22 11:04:59 +0000100/* driver private */
101struct asix_private {
102 int flags;
Simon Glassf58369f2015-07-06 16:47:54 -0600103 struct ueth_data ueth;
Lucas Stachfc9a7582012-08-22 11:04:59 +0000104};
105
Simon Glass246d6932011-02-16 11:14:34 -0800106/*
107 * Asix infrastructure commands
108 */
109static int asix_write_cmd(struct ueth_data *dev, u8 cmd, u16 value, u16 index,
110 u16 size, void *data)
111{
112 int len;
113
114 debug("asix_write_cmd() cmd=0x%02x value=0x%04x index=0x%04x "
115 "size=%d\n", cmd, value, index, size);
116
117 len = usb_control_msg(
118 dev->pusb_dev,
119 usb_sndctrlpipe(dev->pusb_dev, 0),
120 cmd,
121 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
122 value,
123 index,
124 data,
125 size,
126 USB_CTRL_SET_TIMEOUT);
127
128 return len == size ? 0 : -1;
129}
130
131static int asix_read_cmd(struct ueth_data *dev, u8 cmd, u16 value, u16 index,
132 u16 size, void *data)
133{
134 int len;
135
136 debug("asix_read_cmd() cmd=0x%02x value=0x%04x index=0x%04x size=%d\n",
137 cmd, value, index, size);
138
139 len = usb_control_msg(
140 dev->pusb_dev,
141 usb_rcvctrlpipe(dev->pusb_dev, 0),
142 cmd,
143 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
144 value,
145 index,
146 data,
147 size,
148 USB_CTRL_GET_TIMEOUT);
149 return len == size ? 0 : -1;
150}
151
152static inline int asix_set_sw_mii(struct ueth_data *dev)
153{
154 int ret;
155
156 ret = asix_write_cmd(dev, AX_CMD_SET_SW_MII, 0x0000, 0, 0, NULL);
157 if (ret < 0)
158 debug("Failed to enable software MII access\n");
159 return ret;
160}
161
162static inline int asix_set_hw_mii(struct ueth_data *dev)
163{
164 int ret;
165
166 ret = asix_write_cmd(dev, AX_CMD_SET_HW_MII, 0x0000, 0, 0, NULL);
167 if (ret < 0)
168 debug("Failed to enable hardware MII access\n");
169 return ret;
170}
171
172static int asix_mdio_read(struct ueth_data *dev, int phy_id, int loc)
173{
Marek Vasut115436e2012-06-24 14:17:56 +0000174 ALLOC_CACHE_ALIGN_BUFFER(__le16, res, 1);
Simon Glass246d6932011-02-16 11:14:34 -0800175
176 asix_set_sw_mii(dev);
Marek Vasut115436e2012-06-24 14:17:56 +0000177 asix_read_cmd(dev, AX_CMD_READ_MII_REG, phy_id, (__u16)loc, 2, res);
Simon Glass246d6932011-02-16 11:14:34 -0800178 asix_set_hw_mii(dev);
179
180 debug("asix_mdio_read() phy_id=0x%02x, loc=0x%02x, returns=0x%04x\n",
Marek Vasut115436e2012-06-24 14:17:56 +0000181 phy_id, loc, le16_to_cpu(*res));
Simon Glass246d6932011-02-16 11:14:34 -0800182
Marek Vasut115436e2012-06-24 14:17:56 +0000183 return le16_to_cpu(*res);
Simon Glass246d6932011-02-16 11:14:34 -0800184}
185
186static void
187asix_mdio_write(struct ueth_data *dev, int phy_id, int loc, int val)
188{
Marek Vasut115436e2012-06-24 14:17:56 +0000189 ALLOC_CACHE_ALIGN_BUFFER(__le16, res, 1);
190 *res = cpu_to_le16(val);
Simon Glass246d6932011-02-16 11:14:34 -0800191
192 debug("asix_mdio_write() phy_id=0x%02x, loc=0x%02x, val=0x%04x\n",
193 phy_id, loc, val);
194 asix_set_sw_mii(dev);
Marek Vasut115436e2012-06-24 14:17:56 +0000195 asix_write_cmd(dev, AX_CMD_WRITE_MII_REG, phy_id, (__u16)loc, 2, res);
Simon Glass246d6932011-02-16 11:14:34 -0800196 asix_set_hw_mii(dev);
197}
198
199/*
200 * Asix "high level" commands
201 */
202static int asix_sw_reset(struct ueth_data *dev, u8 flags)
203{
204 int ret;
205
206 ret = asix_write_cmd(dev, AX_CMD_SW_RESET, flags, 0, 0, NULL);
207 if (ret < 0)
208 debug("Failed to send software reset: %02x\n", ret);
209 else
210 udelay(150 * 1000);
211
212 return ret;
213}
214
215static inline int asix_get_phy_addr(struct ueth_data *dev)
216{
Marek Vasut115436e2012-06-24 14:17:56 +0000217 ALLOC_CACHE_ALIGN_BUFFER(u8, buf, 2);
218
Simon Glass246d6932011-02-16 11:14:34 -0800219 int ret = asix_read_cmd(dev, AX_CMD_READ_PHY_ID, 0, 0, 2, buf);
220
221 debug("asix_get_phy_addr()\n");
222
223 if (ret < 0) {
224 debug("Error reading PHYID register: %02x\n", ret);
225 goto out;
226 }
Marek Vasuta61b9662011-09-23 21:13:35 +0200227 debug("asix_get_phy_addr() returning 0x%02x%02x\n", buf[0], buf[1]);
Simon Glass246d6932011-02-16 11:14:34 -0800228 ret = buf[1];
229
230out:
231 return ret;
232}
233
234static int asix_write_medium_mode(struct ueth_data *dev, u16 mode)
235{
236 int ret;
237
238 debug("asix_write_medium_mode() - mode = 0x%04x\n", mode);
239 ret = asix_write_cmd(dev, AX_CMD_WRITE_MEDIUM_MODE, mode,
240 0, 0, NULL);
241 if (ret < 0) {
242 debug("Failed to write Medium Mode mode to 0x%04x: %02x\n",
243 mode, ret);
244 }
245 return ret;
246}
247
248static u16 asix_read_rx_ctl(struct ueth_data *dev)
249{
Marek Vasut115436e2012-06-24 14:17:56 +0000250 ALLOC_CACHE_ALIGN_BUFFER(__le16, v, 1);
251
252 int ret = asix_read_cmd(dev, AX_CMD_READ_RX_CTL, 0, 0, 2, v);
Simon Glass246d6932011-02-16 11:14:34 -0800253
254 if (ret < 0)
255 debug("Error reading RX_CTL register: %02x\n", ret);
256 else
Marek Vasut115436e2012-06-24 14:17:56 +0000257 ret = le16_to_cpu(*v);
Simon Glass246d6932011-02-16 11:14:34 -0800258 return ret;
259}
260
261static int asix_write_rx_ctl(struct ueth_data *dev, u16 mode)
262{
263 int ret;
264
265 debug("asix_write_rx_ctl() - mode = 0x%04x\n", mode);
266 ret = asix_write_cmd(dev, AX_CMD_WRITE_RX_CTL, mode, 0, 0, NULL);
267 if (ret < 0) {
268 debug("Failed to write RX_CTL mode to 0x%04x: %02x\n",
269 mode, ret);
270 }
271 return ret;
272}
273
274static int asix_write_gpio(struct ueth_data *dev, u16 value, int sleep)
275{
276 int ret;
277
278 debug("asix_write_gpio() - value = 0x%04x\n", value);
279 ret = asix_write_cmd(dev, AX_CMD_WRITE_GPIOS, value, 0, 0, NULL);
280 if (ret < 0) {
281 debug("Failed to write GPIO value 0x%04x: %02x\n",
282 value, ret);
283 }
284 if (sleep)
285 udelay(sleep * 1000);
286
287 return ret;
288}
289
Simon Glassf58369f2015-07-06 16:47:54 -0600290static int asix_write_hwaddr_common(struct ueth_data *dev, uint8_t *enetaddr)
Lucas Stachfc9a7582012-08-22 11:04:59 +0000291{
Lucas Stachfc9a7582012-08-22 11:04:59 +0000292 int ret;
293 ALLOC_CACHE_ALIGN_BUFFER(unsigned char, buf, ETH_ALEN);
294
Simon Glassf58369f2015-07-06 16:47:54 -0600295 memcpy(buf, enetaddr, ETH_ALEN);
Lucas Stachfc9a7582012-08-22 11:04:59 +0000296
297 ret = asix_write_cmd(dev, AX_CMD_WRITE_NODE_ID, 0, 0, ETH_ALEN, buf);
298 if (ret < 0)
299 debug("Failed to set MAC address: %02x\n", ret);
300
301 return ret;
302}
303
Simon Glass246d6932011-02-16 11:14:34 -0800304/*
305 * mii commands
306 */
307
308/*
309 * mii_nway_restart - restart NWay (autonegotiation) for this interface
310 *
311 * Returns 0 on success, negative on error.
312 */
313static int mii_nway_restart(struct ueth_data *dev)
314{
315 int bmcr;
316 int r = -1;
317
318 /* if autoneg is off, it's an error */
319 bmcr = asix_mdio_read(dev, dev->phy_id, MII_BMCR);
320
321 if (bmcr & BMCR_ANENABLE) {
322 bmcr |= BMCR_ANRESTART;
323 asix_mdio_write(dev, dev->phy_id, MII_BMCR, bmcr);
324 r = 0;
325 }
326
327 return r;
328}
329
Simon Glassf58369f2015-07-06 16:47:54 -0600330static int asix_read_mac_common(struct ueth_data *dev,
331 struct asix_private *priv, uint8_t *enetaddr)
Lucas Stach95c359b2012-08-22 11:05:00 +0000332{
Lucas Stach95c359b2012-08-22 11:05:00 +0000333 ALLOC_CACHE_ALIGN_BUFFER(unsigned char, buf, ETH_ALEN);
Simon Glassf58369f2015-07-06 16:47:54 -0600334 int i;
Lucas Stach95c359b2012-08-22 11:05:00 +0000335
336 if (priv->flags & FLAG_EEPROM_MAC) {
337 for (i = 0; i < (ETH_ALEN >> 1); i++) {
338 if (asix_read_cmd(dev, AX_CMD_READ_EEPROM,
339 0x04 + i, 0, 2, buf) < 0) {
340 debug("Failed to read SROM address 04h.\n");
341 return -1;
342 }
Simon Glassf58369f2015-07-06 16:47:54 -0600343 memcpy(enetaddr + i * 2, buf, 2);
Lucas Stach95c359b2012-08-22 11:05:00 +0000344 }
345 } else {
346 if (asix_read_cmd(dev, AX_CMD_READ_NODE_ID, 0, 0, ETH_ALEN, buf)
347 < 0) {
348 debug("Failed to read MAC address.\n");
349 return -1;
350 }
Simon Glassf58369f2015-07-06 16:47:54 -0600351 memcpy(enetaddr, buf, ETH_ALEN);
Lucas Stach95c359b2012-08-22 11:05:00 +0000352 }
353
354 return 0;
355}
356
Lucas Stacha0d5d032012-08-22 11:04:58 +0000357static int asix_basic_reset(struct ueth_data *dev)
Simon Glass246d6932011-02-16 11:14:34 -0800358{
359 int embd_phy;
Simon Glass246d6932011-02-16 11:14:34 -0800360 u16 rx_ctl;
Simon Glass246d6932011-02-16 11:14:34 -0800361
362 if (asix_write_gpio(dev,
363 AX_GPIO_RSE | AX_GPIO_GPO_2 | AX_GPIO_GPO2EN, 5) < 0)
Lucas Stacha0d5d032012-08-22 11:04:58 +0000364 return -1;
Simon Glass246d6932011-02-16 11:14:34 -0800365
366 /* 0x10 is the phy id of the embedded 10/100 ethernet phy */
367 embd_phy = ((asix_get_phy_addr(dev) & 0x1f) == 0x10 ? 1 : 0);
368 if (asix_write_cmd(dev, AX_CMD_SW_PHY_SELECT,
369 embd_phy, 0, 0, NULL) < 0) {
370 debug("Select PHY #1 failed\n");
Lucas Stacha0d5d032012-08-22 11:04:58 +0000371 return -1;
Simon Glass246d6932011-02-16 11:14:34 -0800372 }
373
374 if (asix_sw_reset(dev, AX_SWRESET_IPPD | AX_SWRESET_PRL) < 0)
Lucas Stacha0d5d032012-08-22 11:04:58 +0000375 return -1;
Simon Glass246d6932011-02-16 11:14:34 -0800376
377 if (asix_sw_reset(dev, AX_SWRESET_CLEAR) < 0)
Lucas Stacha0d5d032012-08-22 11:04:58 +0000378 return -1;
Simon Glass246d6932011-02-16 11:14:34 -0800379
380 if (embd_phy) {
381 if (asix_sw_reset(dev, AX_SWRESET_IPRL) < 0)
Lucas Stacha0d5d032012-08-22 11:04:58 +0000382 return -1;
Simon Glass246d6932011-02-16 11:14:34 -0800383 } else {
384 if (asix_sw_reset(dev, AX_SWRESET_PRTE) < 0)
Lucas Stacha0d5d032012-08-22 11:04:58 +0000385 return -1;
Simon Glass246d6932011-02-16 11:14:34 -0800386 }
387
388 rx_ctl = asix_read_rx_ctl(dev);
389 debug("RX_CTL is 0x%04x after software reset\n", rx_ctl);
390 if (asix_write_rx_ctl(dev, 0x0000) < 0)
Lucas Stacha0d5d032012-08-22 11:04:58 +0000391 return -1;
Simon Glass246d6932011-02-16 11:14:34 -0800392
393 rx_ctl = asix_read_rx_ctl(dev);
394 debug("RX_CTL is 0x%04x setting to 0x0000\n", rx_ctl);
395
Simon Glass246d6932011-02-16 11:14:34 -0800396 dev->phy_id = asix_get_phy_addr(dev);
397 if (dev->phy_id < 0)
398 debug("Failed to read phy id\n");
399
Simon Glass246d6932011-02-16 11:14:34 -0800400 asix_mdio_write(dev, dev->phy_id, MII_BMCR, BMCR_RESET);
401 asix_mdio_write(dev, dev->phy_id, MII_ADVERTISE,
402 ADVERTISE_ALL | ADVERTISE_CSMA);
403 mii_nway_restart(dev);
404
405 if (asix_write_medium_mode(dev, AX88772_MEDIUM_DEFAULT) < 0)
Julius Werner160298f2013-05-11 13:35:02 -0700406 return -1;
Simon Glass246d6932011-02-16 11:14:34 -0800407
408 if (asix_write_cmd(dev, AX_CMD_WRITE_IPG0,
409 AX88772_IPG0_DEFAULT | AX88772_IPG1_DEFAULT,
410 AX88772_IPG2_DEFAULT, 0, NULL) < 0) {
411 debug("Write IPG,IPG1,IPG2 failed\n");
Julius Werner160298f2013-05-11 13:35:02 -0700412 return -1;
Simon Glass246d6932011-02-16 11:14:34 -0800413 }
414
Julius Werner160298f2013-05-11 13:35:02 -0700415 return 0;
416}
417
Marcel Ziswiler7bbbb3c2015-08-05 17:16:59 +0200418static int asix_init_common(struct ueth_data *dev, uint8_t *enetaddr)
Julius Werner160298f2013-05-11 13:35:02 -0700419{
Julius Werner160298f2013-05-11 13:35:02 -0700420 int timeout = 0;
421#define TIMEOUT_RESOLUTION 50 /* ms */
422 int link_detected;
423
424 debug("** %s()\n", __func__);
425
Alban Bedel3cf1a962016-09-10 03:54:09 +0200426 if (asix_write_rx_ctl(dev, AX_DEFAULT_RX_CTL) < 0)
Marcel Ziswiler7bbbb3c2015-08-05 17:16:59 +0200427 goto out_err;
428
429 if (asix_write_hwaddr_common(dev, enetaddr) < 0)
Simon Glass246d6932011-02-16 11:14:34 -0800430 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
Marcel Ziswiler7bbbb3c2015-08-05 17:16:59 +0200450 /*
451 * Wait some more to avoid timeout on first transfer
452 * (e.g. EHCI timed out on TD - token=0x8008d80)
453 */
454 mdelay(25);
455
Simon Glass246d6932011-02-16 11:14:34 -0800456 return 0;
457out_err:
458 return -1;
459}
460
Simon Glassf58369f2015-07-06 16:47:54 -0600461static int asix_send_common(struct ueth_data *dev, void *packet, int length)
Simon Glass246d6932011-02-16 11:14:34 -0800462{
Simon Glass246d6932011-02-16 11:14:34 -0800463 int err;
464 u32 packet_len;
465 int actual_len;
Marek Vasut115436e2012-06-24 14:17:56 +0000466 ALLOC_CACHE_ALIGN_BUFFER(unsigned char, msg,
467 PKTSIZE + sizeof(packet_len));
Simon Glass246d6932011-02-16 11:14:34 -0800468
469 debug("** %s(), len %d\n", __func__, length);
470
471 packet_len = (((length) ^ 0x0000ffff) << 16) + (length);
472 cpu_to_le32s(&packet_len);
473
474 memcpy(msg, &packet_len, sizeof(packet_len));
475 memcpy(msg + sizeof(packet_len), (void *)packet, length);
Simon Glass246d6932011-02-16 11:14:34 -0800476
477 err = usb_bulk_msg(dev->pusb_dev,
478 usb_sndbulkpipe(dev->pusb_dev, dev->ep_out),
479 (void *)msg,
480 length + sizeof(packet_len),
481 &actual_len,
482 USB_BULK_SEND_TIMEOUT);
Thierry Reding7306e9c2015-03-20 12:41:23 +0100483 debug("Tx: len = %zu, actual = %u, err = %d\n",
Simon Glass246d6932011-02-16 11:14:34 -0800484 length + sizeof(packet_len), actual_len, err);
485
486 return err;
487}
Simon Glassf58369f2015-07-06 16:47:54 -0600488
Simon Glassf58369f2015-07-06 16:47:54 -0600489static int asix_eth_start(struct udevice *dev)
490{
Simon Glassfa20e932020-12-03 16:55:20 -0700491 struct eth_pdata *pdata = dev_get_plat(dev);
Simon Glassf58369f2015-07-06 16:47:54 -0600492 struct asix_private *priv = dev_get_priv(dev);
493
Marcel Ziswiler7bbbb3c2015-08-05 17:16:59 +0200494 return asix_init_common(&priv->ueth, pdata->enetaddr);
Simon Glassf58369f2015-07-06 16:47:54 -0600495}
496
497void asix_eth_stop(struct udevice *dev)
498{
499 debug("** %s()\n", __func__);
500}
501
502int asix_eth_send(struct udevice *dev, void *packet, int length)
503{
504 struct asix_private *priv = dev_get_priv(dev);
505
506 return asix_send_common(&priv->ueth, packet, length);
507}
508
509int asix_eth_recv(struct udevice *dev, int flags, uchar **packetp)
510{
511 struct asix_private *priv = dev_get_priv(dev);
512 struct ueth_data *ueth = &priv->ueth;
513 uint8_t *ptr;
514 int ret, len;
515 u32 packet_len;
516
517 len = usb_ether_get_rx_bytes(ueth, &ptr);
518 debug("%s: first try, len=%d\n", __func__, len);
519 if (!len) {
520 if (!(flags & ETH_RECV_CHECK_DEVICE))
521 return -EAGAIN;
522 ret = usb_ether_receive(ueth, AX_RX_URB_SIZE);
523 if (ret == -EAGAIN)
524 return ret;
525
526 len = usb_ether_get_rx_bytes(ueth, &ptr);
527 debug("%s: second try, len=%d\n", __func__, len);
528 }
529
530 /*
531 * 1st 4 bytes contain the length of the actual data as two
532 * complementary 16-bit words. Extract the length of the data.
533 */
534 if (len < sizeof(packet_len)) {
535 debug("Rx: incomplete packet length\n");
536 goto err;
537 }
538 memcpy(&packet_len, ptr, sizeof(packet_len));
539 le32_to_cpus(&packet_len);
540 if (((~packet_len >> 16) & 0x7ff) != (packet_len & 0x7ff)) {
541 debug("Rx: malformed packet length: %#x (%#x:%#x)\n",
542 packet_len, (~packet_len >> 16) & 0x7ff,
543 packet_len & 0x7ff);
544 goto err;
545 }
546 packet_len = packet_len & 0x7ff;
547 if (packet_len > len - sizeof(packet_len)) {
548 debug("Rx: too large packet: %d\n", packet_len);
549 goto err;
550 }
551
552 *packetp = ptr + sizeof(packet_len);
553 return packet_len;
554
555err:
556 usb_ether_advance_rxbuf(ueth, -1);
557 return -EINVAL;
558}
559
560static int asix_free_pkt(struct udevice *dev, uchar *packet, int packet_len)
561{
562 struct asix_private *priv = dev_get_priv(dev);
563
564 if (packet_len & 1)
565 packet_len++;
566 usb_ether_advance_rxbuf(&priv->ueth, sizeof(u32) + packet_len);
567
568 return 0;
569}
570
571int asix_write_hwaddr(struct udevice *dev)
572{
Simon Glassfa20e932020-12-03 16:55:20 -0700573 struct eth_pdata *pdata = dev_get_plat(dev);
Simon Glassf58369f2015-07-06 16:47:54 -0600574 struct asix_private *priv = dev_get_priv(dev);
575
576 if (priv->flags & FLAG_TYPE_AX88172)
577 return -ENOSYS;
578
579 return asix_write_hwaddr_common(&priv->ueth, pdata->enetaddr);
580}
581
582static int asix_eth_probe(struct udevice *dev)
583{
Simon Glassfa20e932020-12-03 16:55:20 -0700584 struct eth_pdata *pdata = dev_get_plat(dev);
Simon Glassf58369f2015-07-06 16:47:54 -0600585 struct asix_private *priv = dev_get_priv(dev);
586 struct ueth_data *ss = &priv->ueth;
587 int ret;
588
589 priv->flags = dev->driver_data;
590 ret = usb_ether_register(dev, ss, AX_RX_URB_SIZE);
591 if (ret)
592 return ret;
593
594 ret = asix_basic_reset(ss);
595 if (ret)
596 goto err;
597
598 /* Get the MAC address */
599 ret = asix_read_mac_common(ss, priv, pdata->enetaddr);
600 if (ret)
601 goto err;
602 debug("MAC %pM\n", pdata->enetaddr);
603
604 return 0;
605
606err:
607 return usb_ether_deregister(ss);
608}
609
610static const struct eth_ops asix_eth_ops = {
611 .start = asix_eth_start,
612 .send = asix_eth_send,
613 .recv = asix_eth_recv,
614 .free_pkt = asix_free_pkt,
615 .stop = asix_eth_stop,
616 .write_hwaddr = asix_write_hwaddr,
617};
618
619U_BOOT_DRIVER(asix_eth) = {
620 .name = "asix_eth",
621 .id = UCLASS_ETH,
622 .probe = asix_eth_probe,
623 .ops = &asix_eth_ops,
Simon Glass8a2b47f2020-12-03 16:55:17 -0700624 .priv_auto = sizeof(struct asix_private),
Simon Glass71fa5b42020-12-03 16:55:18 -0700625 .plat_auto = sizeof(struct eth_pdata),
Simon Glassf58369f2015-07-06 16:47:54 -0600626};
627
628static const struct usb_device_id asix_eth_id_table[] = {
629 /* Apple USB Ethernet Adapter */
630 { USB_DEVICE(0x05ac, 0x1402), .driver_info = FLAG_TYPE_AX88772 },
631 /* D-Link DUB-E100 H/W Ver B1 */
632 { USB_DEVICE(0x07d1, 0x3c05), .driver_info = FLAG_TYPE_AX88772 },
633 /* D-Link DUB-E100 H/W Ver C1 */
634 { USB_DEVICE(0x2001, 0x1a02), .driver_info = FLAG_TYPE_AX88772 },
635 /* Cables-to-Go USB Ethernet Adapter */
636 { USB_DEVICE(0x0b95, 0x772a), .driver_info = FLAG_TYPE_AX88772 },
637 /* Trendnet TU2-ET100 V3.0R */
638 { USB_DEVICE(0x0b95, 0x7720), .driver_info = FLAG_TYPE_AX88772 },
639 /* SMC */
640 { USB_DEVICE(0x0b95, 0x1720), .driver_info = FLAG_TYPE_AX88172 },
641 /* MSI - ASIX 88772a */
642 { USB_DEVICE(0x0db0, 0xa877), .driver_info = FLAG_TYPE_AX88772 },
643 /* Linksys 200M v2.1 */
644 { USB_DEVICE(0x13b1, 0x0018), .driver_info = FLAG_TYPE_AX88172 },
645 /* 0Q0 cable ethernet */
646 { USB_DEVICE(0x1557, 0x7720), .driver_info = FLAG_TYPE_AX88772 },
647 /* DLink DUB-E100 H/W Ver B1 Alternate */
648 { USB_DEVICE(0x2001, 0x3c05), .driver_info = FLAG_TYPE_AX88772 },
649 /* ASIX 88772B */
650 { USB_DEVICE(0x0b95, 0x772b),
651 .driver_info = FLAG_TYPE_AX88772B | FLAG_EEPROM_MAC },
652 { USB_DEVICE(0x0b95, 0x7e2b), .driver_info = FLAG_TYPE_AX88772B },
653 { } /* Terminating entry */
654};
655
656U_BOOT_USB_DEVICE(asix_eth, asix_eth_id_table);