blob: 8a43e7c27d6fe263f1635206f30570ad92852ff8 [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 *
Marcel Ziswiler14727122015-08-05 17:16:59 +02004 * Patched for AX88772B by Antmicro Ltd <www.antmicro.com>
5 *
Wolfgang Denk1a459662013-07-08 09:37:19 +02006 * SPDX-License-Identifier: GPL-2.0+
Simon Glass9b70e002011-02-16 11:14:34 -08007 */
8
9#include <common.h>
Simon Glassfbc4b8a2015-07-06 16:47:54 -060010#include <dm.h>
Simon Glass9b70e002011-02-16 11:14:34 -080011#include <usb.h>
Simon Glassfbc4b8a2015-07-06 16:47:54 -060012#include <malloc.h>
Simon Glass9b70e002011-02-16 11:14:34 -080013#include <linux/mii.h>
14#include "usb_ether.h"
Simon Glass9b70e002011-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 Stach02c8d8c2012-08-22 11:05:00 +000022#define AX_CMD_READ_EEPROM 0x0b
Simon Glass9b70e002011-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 Stach58f8fab2012-08-22 11:04:59 +000027#define AX_CMD_WRITE_NODE_ID 0x14
Simon Glass9b70e002011-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 */
Marcel Ziswiler14727122015-08-05 17:16:59 +020069#define AX_RX_CTL_RH2M 0x0200 /* 32-bit aligned RX IP header */
70#define AX_RX_CTL_RH1M 0x0100 /* Enable RX header format type 1 */
71#define AX_RX_CTL_SO 0x0080
72#define AX_RX_CTL_AB 0x0008
73#define AX_RX_HEADER_DEFAULT (AX_RX_CTL_RH1M | AX_RX_CTL_RH2M)
Simon Glass9b70e002011-02-16 11:14:34 -080074
75#define AX_DEFAULT_RX_CTL \
76 (AX_RX_CTL_SO | AX_RX_CTL_AB)
77
78/* GPIO 2 toggles */
79#define AX_GPIO_GPO2EN 0x10 /* GPIO2 Output enable */
80#define AX_GPIO_GPO_2 0x20 /* GPIO2 Output value */
81#define AX_GPIO_RSE 0x80 /* Reload serial EEPROM */
82
83/* local defines */
84#define ASIX_BASE_NAME "asx"
85#define USB_CTRL_SET_TIMEOUT 5000
86#define USB_CTRL_GET_TIMEOUT 5000
87#define USB_BULK_SEND_TIMEOUT 5000
88#define USB_BULK_RECV_TIMEOUT 5000
89
90#define AX_RX_URB_SIZE 2048
91#define PHY_CONNECT_TIMEOUT 5000
92
Lucas Stach58f8fab2012-08-22 11:04:59 +000093/* asix_flags defines */
94#define FLAG_NONE 0
95#define FLAG_TYPE_AX88172 (1U << 0)
96#define FLAG_TYPE_AX88772 (1U << 1)
Lucas Stach1dff9d02012-08-22 11:05:01 +000097#define FLAG_TYPE_AX88772B (1U << 2)
98#define FLAG_EEPROM_MAC (1U << 3) /* initial mac address in eeprom */
Lucas Stach58f8fab2012-08-22 11:04:59 +000099
Marcel Ziswiler14727122015-08-05 17:16:59 +0200100#define ASIX_USB_VENDOR_ID 0x0b95
101#define AX88772B_USB_PRODUCT_ID 0x772b
Simon Glass9b70e002011-02-16 11:14:34 -0800102
Lucas Stach58f8fab2012-08-22 11:04:59 +0000103/* driver private */
104struct asix_private {
105 int flags;
Simon Glassfbc4b8a2015-07-06 16:47:54 -0600106#ifdef CONFIG_DM_ETH
107 struct ueth_data ueth;
108#endif
Lucas Stach58f8fab2012-08-22 11:04:59 +0000109};
110
Simon Glassfbc4b8a2015-07-06 16:47:54 -0600111#ifndef CONFIG_DM_ETH
112/* local vars */
113static int curr_eth_dev; /* index for name of next device detected */
114#endif
115
Simon Glass9b70e002011-02-16 11:14:34 -0800116/*
117 * Asix infrastructure commands
118 */
119static int asix_write_cmd(struct ueth_data *dev, u8 cmd, u16 value, u16 index,
120 u16 size, void *data)
121{
122 int len;
123
124 debug("asix_write_cmd() cmd=0x%02x value=0x%04x index=0x%04x "
125 "size=%d\n", cmd, value, index, size);
126
127 len = usb_control_msg(
128 dev->pusb_dev,
129 usb_sndctrlpipe(dev->pusb_dev, 0),
130 cmd,
131 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
132 value,
133 index,
134 data,
135 size,
136 USB_CTRL_SET_TIMEOUT);
137
138 return len == size ? 0 : -1;
139}
140
141static int asix_read_cmd(struct ueth_data *dev, u8 cmd, u16 value, u16 index,
142 u16 size, void *data)
143{
144 int len;
145
146 debug("asix_read_cmd() cmd=0x%02x value=0x%04x index=0x%04x size=%d\n",
147 cmd, value, index, size);
148
149 len = usb_control_msg(
150 dev->pusb_dev,
151 usb_rcvctrlpipe(dev->pusb_dev, 0),
152 cmd,
153 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
154 value,
155 index,
156 data,
157 size,
158 USB_CTRL_GET_TIMEOUT);
159 return len == size ? 0 : -1;
160}
161
162static inline int asix_set_sw_mii(struct ueth_data *dev)
163{
164 int ret;
165
166 ret = asix_write_cmd(dev, AX_CMD_SET_SW_MII, 0x0000, 0, 0, NULL);
167 if (ret < 0)
168 debug("Failed to enable software MII access\n");
169 return ret;
170}
171
172static inline int asix_set_hw_mii(struct ueth_data *dev)
173{
174 int ret;
175
176 ret = asix_write_cmd(dev, AX_CMD_SET_HW_MII, 0x0000, 0, 0, NULL);
177 if (ret < 0)
178 debug("Failed to enable hardware MII access\n");
179 return ret;
180}
181
182static int asix_mdio_read(struct ueth_data *dev, int phy_id, int loc)
183{
Marek Vasutc59ab092012-06-24 14:17:56 +0000184 ALLOC_CACHE_ALIGN_BUFFER(__le16, res, 1);
Simon Glass9b70e002011-02-16 11:14:34 -0800185
186 asix_set_sw_mii(dev);
Marek Vasutc59ab092012-06-24 14:17:56 +0000187 asix_read_cmd(dev, AX_CMD_READ_MII_REG, phy_id, (__u16)loc, 2, res);
Simon Glass9b70e002011-02-16 11:14:34 -0800188 asix_set_hw_mii(dev);
189
190 debug("asix_mdio_read() phy_id=0x%02x, loc=0x%02x, returns=0x%04x\n",
Marek Vasutc59ab092012-06-24 14:17:56 +0000191 phy_id, loc, le16_to_cpu(*res));
Simon Glass9b70e002011-02-16 11:14:34 -0800192
Marek Vasutc59ab092012-06-24 14:17:56 +0000193 return le16_to_cpu(*res);
Simon Glass9b70e002011-02-16 11:14:34 -0800194}
195
196static void
197asix_mdio_write(struct ueth_data *dev, int phy_id, int loc, int val)
198{
Marek Vasutc59ab092012-06-24 14:17:56 +0000199 ALLOC_CACHE_ALIGN_BUFFER(__le16, res, 1);
200 *res = cpu_to_le16(val);
Simon Glass9b70e002011-02-16 11:14:34 -0800201
202 debug("asix_mdio_write() phy_id=0x%02x, loc=0x%02x, val=0x%04x\n",
203 phy_id, loc, val);
204 asix_set_sw_mii(dev);
Marek Vasutc59ab092012-06-24 14:17:56 +0000205 asix_write_cmd(dev, AX_CMD_WRITE_MII_REG, phy_id, (__u16)loc, 2, res);
Simon Glass9b70e002011-02-16 11:14:34 -0800206 asix_set_hw_mii(dev);
207}
208
209/*
210 * Asix "high level" commands
211 */
212static int asix_sw_reset(struct ueth_data *dev, u8 flags)
213{
214 int ret;
215
216 ret = asix_write_cmd(dev, AX_CMD_SW_RESET, flags, 0, 0, NULL);
217 if (ret < 0)
218 debug("Failed to send software reset: %02x\n", ret);
219 else
220 udelay(150 * 1000);
221
222 return ret;
223}
224
225static inline int asix_get_phy_addr(struct ueth_data *dev)
226{
Marek Vasutc59ab092012-06-24 14:17:56 +0000227 ALLOC_CACHE_ALIGN_BUFFER(u8, buf, 2);
228
Simon Glass9b70e002011-02-16 11:14:34 -0800229 int ret = asix_read_cmd(dev, AX_CMD_READ_PHY_ID, 0, 0, 2, buf);
230
231 debug("asix_get_phy_addr()\n");
232
233 if (ret < 0) {
234 debug("Error reading PHYID register: %02x\n", ret);
235 goto out;
236 }
Marek Vasut0ed1eb62011-09-23 21:13:35 +0200237 debug("asix_get_phy_addr() returning 0x%02x%02x\n", buf[0], buf[1]);
Simon Glass9b70e002011-02-16 11:14:34 -0800238 ret = buf[1];
239
240out:
241 return ret;
242}
243
244static int asix_write_medium_mode(struct ueth_data *dev, u16 mode)
245{
246 int ret;
247
248 debug("asix_write_medium_mode() - mode = 0x%04x\n", mode);
249 ret = asix_write_cmd(dev, AX_CMD_WRITE_MEDIUM_MODE, mode,
250 0, 0, NULL);
251 if (ret < 0) {
252 debug("Failed to write Medium Mode mode to 0x%04x: %02x\n",
253 mode, ret);
254 }
255 return ret;
256}
257
258static u16 asix_read_rx_ctl(struct ueth_data *dev)
259{
Marek Vasutc59ab092012-06-24 14:17:56 +0000260 ALLOC_CACHE_ALIGN_BUFFER(__le16, v, 1);
261
262 int ret = asix_read_cmd(dev, AX_CMD_READ_RX_CTL, 0, 0, 2, v);
Simon Glass9b70e002011-02-16 11:14:34 -0800263
264 if (ret < 0)
265 debug("Error reading RX_CTL register: %02x\n", ret);
266 else
Marek Vasutc59ab092012-06-24 14:17:56 +0000267 ret = le16_to_cpu(*v);
Simon Glass9b70e002011-02-16 11:14:34 -0800268 return ret;
269}
270
271static int asix_write_rx_ctl(struct ueth_data *dev, u16 mode)
272{
273 int ret;
274
275 debug("asix_write_rx_ctl() - mode = 0x%04x\n", mode);
276 ret = asix_write_cmd(dev, AX_CMD_WRITE_RX_CTL, mode, 0, 0, NULL);
277 if (ret < 0) {
278 debug("Failed to write RX_CTL mode to 0x%04x: %02x\n",
279 mode, ret);
280 }
281 return ret;
282}
283
284static int asix_write_gpio(struct ueth_data *dev, u16 value, int sleep)
285{
286 int ret;
287
288 debug("asix_write_gpio() - value = 0x%04x\n", value);
289 ret = asix_write_cmd(dev, AX_CMD_WRITE_GPIOS, value, 0, 0, NULL);
290 if (ret < 0) {
291 debug("Failed to write GPIO value 0x%04x: %02x\n",
292 value, ret);
293 }
294 if (sleep)
295 udelay(sleep * 1000);
296
297 return ret;
298}
299
Simon Glassfbc4b8a2015-07-06 16:47:54 -0600300static int asix_write_hwaddr_common(struct ueth_data *dev, uint8_t *enetaddr)
Lucas Stach58f8fab2012-08-22 11:04:59 +0000301{
Lucas Stach58f8fab2012-08-22 11:04:59 +0000302 int ret;
303 ALLOC_CACHE_ALIGN_BUFFER(unsigned char, buf, ETH_ALEN);
304
Simon Glassfbc4b8a2015-07-06 16:47:54 -0600305 memcpy(buf, enetaddr, ETH_ALEN);
Lucas Stach58f8fab2012-08-22 11:04:59 +0000306
307 ret = asix_write_cmd(dev, AX_CMD_WRITE_NODE_ID, 0, 0, ETH_ALEN, buf);
308 if (ret < 0)
309 debug("Failed to set MAC address: %02x\n", ret);
310
311 return ret;
312}
313
Simon Glass9b70e002011-02-16 11:14:34 -0800314/*
315 * mii commands
316 */
317
318/*
319 * mii_nway_restart - restart NWay (autonegotiation) for this interface
320 *
321 * Returns 0 on success, negative on error.
322 */
323static int mii_nway_restart(struct ueth_data *dev)
324{
325 int bmcr;
326 int r = -1;
327
328 /* if autoneg is off, it's an error */
329 bmcr = asix_mdio_read(dev, dev->phy_id, MII_BMCR);
330
331 if (bmcr & BMCR_ANENABLE) {
332 bmcr |= BMCR_ANRESTART;
333 asix_mdio_write(dev, dev->phy_id, MII_BMCR, bmcr);
334 r = 0;
335 }
336
337 return r;
338}
339
Simon Glassfbc4b8a2015-07-06 16:47:54 -0600340static int asix_read_mac_common(struct ueth_data *dev,
341 struct asix_private *priv, uint8_t *enetaddr)
Lucas Stach02c8d8c2012-08-22 11:05:00 +0000342{
Lucas Stach02c8d8c2012-08-22 11:05:00 +0000343 ALLOC_CACHE_ALIGN_BUFFER(unsigned char, buf, ETH_ALEN);
Simon Glassfbc4b8a2015-07-06 16:47:54 -0600344 int i;
Lucas Stach02c8d8c2012-08-22 11:05:00 +0000345
346 if (priv->flags & FLAG_EEPROM_MAC) {
347 for (i = 0; i < (ETH_ALEN >> 1); i++) {
348 if (asix_read_cmd(dev, AX_CMD_READ_EEPROM,
349 0x04 + i, 0, 2, buf) < 0) {
350 debug("Failed to read SROM address 04h.\n");
351 return -1;
352 }
Simon Glassfbc4b8a2015-07-06 16:47:54 -0600353 memcpy(enetaddr + i * 2, buf, 2);
Lucas Stach02c8d8c2012-08-22 11:05:00 +0000354 }
355 } else {
356 if (asix_read_cmd(dev, AX_CMD_READ_NODE_ID, 0, 0, ETH_ALEN, buf)
357 < 0) {
358 debug("Failed to read MAC address.\n");
359 return -1;
360 }
Simon Glassfbc4b8a2015-07-06 16:47:54 -0600361 memcpy(enetaddr, buf, ETH_ALEN);
Lucas Stach02c8d8c2012-08-22 11:05:00 +0000362 }
363
364 return 0;
365}
366
Lucas Stach5fae53d2012-08-22 11:04:58 +0000367static int asix_basic_reset(struct ueth_data *dev)
Simon Glass9b70e002011-02-16 11:14:34 -0800368{
369 int embd_phy;
Simon Glass9b70e002011-02-16 11:14:34 -0800370 u16 rx_ctl;
Simon Glass9b70e002011-02-16 11:14:34 -0800371
372 if (asix_write_gpio(dev,
373 AX_GPIO_RSE | AX_GPIO_GPO_2 | AX_GPIO_GPO2EN, 5) < 0)
Lucas Stach5fae53d2012-08-22 11:04:58 +0000374 return -1;
Simon Glass9b70e002011-02-16 11:14:34 -0800375
376 /* 0x10 is the phy id of the embedded 10/100 ethernet phy */
377 embd_phy = ((asix_get_phy_addr(dev) & 0x1f) == 0x10 ? 1 : 0);
378 if (asix_write_cmd(dev, AX_CMD_SW_PHY_SELECT,
379 embd_phy, 0, 0, NULL) < 0) {
380 debug("Select PHY #1 failed\n");
Lucas Stach5fae53d2012-08-22 11:04:58 +0000381 return -1;
Simon Glass9b70e002011-02-16 11:14:34 -0800382 }
383
384 if (asix_sw_reset(dev, AX_SWRESET_IPPD | AX_SWRESET_PRL) < 0)
Lucas Stach5fae53d2012-08-22 11:04:58 +0000385 return -1;
Simon Glass9b70e002011-02-16 11:14:34 -0800386
387 if (asix_sw_reset(dev, AX_SWRESET_CLEAR) < 0)
Lucas Stach5fae53d2012-08-22 11:04:58 +0000388 return -1;
Simon Glass9b70e002011-02-16 11:14:34 -0800389
390 if (embd_phy) {
391 if (asix_sw_reset(dev, AX_SWRESET_IPRL) < 0)
Lucas Stach5fae53d2012-08-22 11:04:58 +0000392 return -1;
Simon Glass9b70e002011-02-16 11:14:34 -0800393 } else {
394 if (asix_sw_reset(dev, AX_SWRESET_PRTE) < 0)
Lucas Stach5fae53d2012-08-22 11:04:58 +0000395 return -1;
Simon Glass9b70e002011-02-16 11:14:34 -0800396 }
397
398 rx_ctl = asix_read_rx_ctl(dev);
399 debug("RX_CTL is 0x%04x after software reset\n", rx_ctl);
400 if (asix_write_rx_ctl(dev, 0x0000) < 0)
Lucas Stach5fae53d2012-08-22 11:04:58 +0000401 return -1;
Simon Glass9b70e002011-02-16 11:14:34 -0800402
403 rx_ctl = asix_read_rx_ctl(dev);
404 debug("RX_CTL is 0x%04x setting to 0x0000\n", rx_ctl);
405
Julius Werner4edcf0a2013-05-11 13:35:02 -0700406 dev->phy_id = asix_get_phy_addr(dev);
407 if (dev->phy_id < 0)
408 debug("Failed to read phy id\n");
409
410 asix_mdio_write(dev, dev->phy_id, MII_BMCR, BMCR_RESET);
411 asix_mdio_write(dev, dev->phy_id, MII_ADVERTISE,
412 ADVERTISE_ALL | ADVERTISE_CSMA);
413 mii_nway_restart(dev);
414
415 if (asix_write_medium_mode(dev, AX88772_MEDIUM_DEFAULT) < 0)
416 return -1;
417
418 if (asix_write_cmd(dev, AX_CMD_WRITE_IPG0,
419 AX88772_IPG0_DEFAULT | AX88772_IPG1_DEFAULT,
420 AX88772_IPG2_DEFAULT, 0, NULL) < 0) {
421 debug("Write IPG,IPG1,IPG2 failed\n");
422 return -1;
423 }
424
Lucas Stach5fae53d2012-08-22 11:04:58 +0000425 return 0;
426}
427
Marcel Ziswiler14727122015-08-05 17:16:59 +0200428static int asix_init_common(struct ueth_data *dev, uint8_t *enetaddr)
Lucas Stach5fae53d2012-08-22 11:04:58 +0000429{
Lucas Stach5fae53d2012-08-22 11:04:58 +0000430 int timeout = 0;
431#define TIMEOUT_RESOLUTION 50 /* ms */
432 int link_detected;
Marcel Ziswiler14727122015-08-05 17:16:59 +0200433 u32 ctl = AX_DEFAULT_RX_CTL;
Lucas Stach5fae53d2012-08-22 11:04:58 +0000434
435 debug("** %s()\n", __func__);
436
Marcel Ziswiler14727122015-08-05 17:16:59 +0200437 if ((dev->pusb_dev->descriptor.idVendor == ASIX_USB_VENDOR_ID) &&
438 (dev->pusb_dev->descriptor.idProduct == AX88772B_USB_PRODUCT_ID))
439 ctl |= AX_RX_HEADER_DEFAULT;
440
441 if (asix_write_rx_ctl(dev, ctl) < 0)
442 goto out_err;
443
444 if (asix_write_hwaddr_common(dev, enetaddr) < 0)
Simon Glass9b70e002011-02-16 11:14:34 -0800445 goto out_err;
446
447 do {
448 link_detected = asix_mdio_read(dev, dev->phy_id, MII_BMSR) &
449 BMSR_LSTATUS;
450 if (!link_detected) {
451 if (timeout == 0)
452 printf("Waiting for Ethernet connection... ");
453 udelay(TIMEOUT_RESOLUTION * 1000);
454 timeout += TIMEOUT_RESOLUTION;
455 }
456 } while (!link_detected && timeout < PHY_CONNECT_TIMEOUT);
457 if (link_detected) {
458 if (timeout != 0)
459 printf("done.\n");
460 } else {
461 printf("unable to connect.\n");
462 goto out_err;
463 }
464
Marcel Ziswiler14727122015-08-05 17:16:59 +0200465 /*
466 * Wait some more to avoid timeout on first transfer
467 * (e.g. EHCI timed out on TD - token=0x8008d80)
468 */
469 mdelay(25);
470
Simon Glass9b70e002011-02-16 11:14:34 -0800471 return 0;
472out_err:
473 return -1;
474}
475
Simon Glassfbc4b8a2015-07-06 16:47:54 -0600476static int asix_send_common(struct ueth_data *dev, void *packet, int length)
Simon Glass9b70e002011-02-16 11:14:34 -0800477{
Simon Glass9b70e002011-02-16 11:14:34 -0800478 int err;
479 u32 packet_len;
480 int actual_len;
Marek Vasutc59ab092012-06-24 14:17:56 +0000481 ALLOC_CACHE_ALIGN_BUFFER(unsigned char, msg,
482 PKTSIZE + sizeof(packet_len));
Simon Glass9b70e002011-02-16 11:14:34 -0800483
484 debug("** %s(), len %d\n", __func__, length);
485
486 packet_len = (((length) ^ 0x0000ffff) << 16) + (length);
487 cpu_to_le32s(&packet_len);
488
489 memcpy(msg, &packet_len, sizeof(packet_len));
490 memcpy(msg + sizeof(packet_len), (void *)packet, length);
Simon Glass9b70e002011-02-16 11:14:34 -0800491
492 err = usb_bulk_msg(dev->pusb_dev,
493 usb_sndbulkpipe(dev->pusb_dev, dev->ep_out),
494 (void *)msg,
495 length + sizeof(packet_len),
496 &actual_len,
497 USB_BULK_SEND_TIMEOUT);
Thierry Reding4c5998b2015-03-20 12:41:23 +0100498 debug("Tx: len = %zu, actual = %u, err = %d\n",
Simon Glass9b70e002011-02-16 11:14:34 -0800499 length + sizeof(packet_len), actual_len, err);
500
501 return err;
502}
503
Simon Glassfbc4b8a2015-07-06 16:47:54 -0600504#ifndef CONFIG_DM_ETH
505/*
506 * Asix callbacks
507 */
508static int asix_init(struct eth_device *eth, bd_t *bd)
509{
510 struct ueth_data *dev = (struct ueth_data *)eth->priv;
511
Marcel Ziswiler14727122015-08-05 17:16:59 +0200512 return asix_init_common(dev, eth->enetaddr);
Simon Glassfbc4b8a2015-07-06 16:47:54 -0600513}
514
515static int asix_send(struct eth_device *eth, void *packet, int length)
516{
517 struct ueth_data *dev = (struct ueth_data *)eth->priv;
518
519 return asix_send_common(dev, packet, length);
520}
521
Simon Glass9b70e002011-02-16 11:14:34 -0800522static int asix_recv(struct eth_device *eth)
523{
524 struct ueth_data *dev = (struct ueth_data *)eth->priv;
Marek Vasutc59ab092012-06-24 14:17:56 +0000525 ALLOC_CACHE_ALIGN_BUFFER(unsigned char, recv_buf, AX_RX_URB_SIZE);
Simon Glass9b70e002011-02-16 11:14:34 -0800526 unsigned char *buf_ptr;
527 int err;
528 int actual_len;
529 u32 packet_len;
530
531 debug("** %s()\n", __func__);
532
533 err = usb_bulk_msg(dev->pusb_dev,
534 usb_rcvbulkpipe(dev->pusb_dev, dev->ep_in),
535 (void *)recv_buf,
536 AX_RX_URB_SIZE,
537 &actual_len,
538 USB_BULK_RECV_TIMEOUT);
539 debug("Rx: len = %u, actual = %u, err = %d\n", AX_RX_URB_SIZE,
540 actual_len, err);
541 if (err != 0) {
542 debug("Rx: failed to receive\n");
543 return -1;
544 }
545 if (actual_len > AX_RX_URB_SIZE) {
546 debug("Rx: received too many bytes %d\n", actual_len);
547 return -1;
548 }
549
550 buf_ptr = recv_buf;
551 while (actual_len > 0) {
552 /*
553 * 1st 4 bytes contain the length of the actual data as two
554 * complementary 16-bit words. Extract the length of the data.
555 */
556 if (actual_len < sizeof(packet_len)) {
557 debug("Rx: incomplete packet length\n");
558 return -1;
559 }
560 memcpy(&packet_len, buf_ptr, sizeof(packet_len));
561 le32_to_cpus(&packet_len);
Lucas Stach1dff9d02012-08-22 11:05:01 +0000562 if (((~packet_len >> 16) & 0x7ff) != (packet_len & 0x7ff)) {
Simon Glass9b70e002011-02-16 11:14:34 -0800563 debug("Rx: malformed packet length: %#x (%#x:%#x)\n",
Lucas Stach1dff9d02012-08-22 11:05:01 +0000564 packet_len, (~packet_len >> 16) & 0x7ff,
565 packet_len & 0x7ff);
Simon Glass9b70e002011-02-16 11:14:34 -0800566 return -1;
567 }
Lucas Stach1dff9d02012-08-22 11:05:01 +0000568 packet_len = packet_len & 0x7ff;
Simon Glass9b70e002011-02-16 11:14:34 -0800569 if (packet_len > actual_len - sizeof(packet_len)) {
570 debug("Rx: too large packet: %d\n", packet_len);
571 return -1;
572 }
573
Marcel Ziswiler14727122015-08-05 17:16:59 +0200574 if ((dev->pusb_dev->descriptor.idVendor ==
575 ASIX_USB_VENDOR_ID) &&
576 (dev->pusb_dev->descriptor.idProduct ==
577 AX88772B_USB_PRODUCT_ID))
578 buf_ptr += 2;
579
Simon Glass9b70e002011-02-16 11:14:34 -0800580 /* Notify net stack */
Joe Hershberger1fd92db2015-04-08 01:41:06 -0500581 net_process_received_packet(buf_ptr + sizeof(packet_len),
582 packet_len);
Simon Glass9b70e002011-02-16 11:14:34 -0800583
584 /* Adjust for next iteration. Packets are padded to 16-bits */
585 if (packet_len & 1)
586 packet_len++;
587 actual_len -= sizeof(packet_len) + packet_len;
588 buf_ptr += sizeof(packet_len) + packet_len;
589 }
590
591 return err;
592}
593
594static void asix_halt(struct eth_device *eth)
595{
596 debug("** %s()\n", __func__);
597}
598
Simon Glassfbc4b8a2015-07-06 16:47:54 -0600599static int asix_write_hwaddr(struct eth_device *eth)
600{
601 struct ueth_data *dev = (struct ueth_data *)eth->priv;
602
603 return asix_write_hwaddr_common(dev, eth->enetaddr);
604}
605
Simon Glass9b70e002011-02-16 11:14:34 -0800606/*
607 * Asix probing functions
608 */
609void asix_eth_before_probe(void)
610{
611 curr_eth_dev = 0;
612}
613
614struct asix_dongle {
615 unsigned short vendor;
616 unsigned short product;
Lucas Stach58f8fab2012-08-22 11:04:59 +0000617 int flags;
Simon Glass9b70e002011-02-16 11:14:34 -0800618};
619
Jeroen Hofstee51afc2c2014-06-17 21:14:17 +0200620static const struct asix_dongle asix_dongles[] = {
Lucas Stach58f8fab2012-08-22 11:04:59 +0000621 { 0x05ac, 0x1402, FLAG_TYPE_AX88772 }, /* Apple USB Ethernet Adapter */
622 { 0x07d1, 0x3c05, FLAG_TYPE_AX88772 }, /* D-Link DUB-E100 H/W Ver B1 */
Pierre Aubertbefd3872013-06-27 09:01:54 +0200623 { 0x2001, 0x1a02, FLAG_TYPE_AX88772 }, /* D-Link DUB-E100 H/W Ver C1 */
Lucas Stach58f8fab2012-08-22 11:04:59 +0000624 /* Cables-to-Go USB Ethernet Adapter */
625 { 0x0b95, 0x772a, FLAG_TYPE_AX88772 },
626 { 0x0b95, 0x7720, FLAG_TYPE_AX88772 }, /* Trendnet TU2-ET100 V3.0R */
627 { 0x0b95, 0x1720, FLAG_TYPE_AX88172 }, /* SMC */
628 { 0x0db0, 0xa877, FLAG_TYPE_AX88772 }, /* MSI - ASIX 88772a */
629 { 0x13b1, 0x0018, FLAG_TYPE_AX88172 }, /* Linksys 200M v2.1 */
630 { 0x1557, 0x7720, FLAG_TYPE_AX88772 }, /* 0Q0 cable ethernet */
631 /* DLink DUB-E100 H/W Ver B1 Alternate */
632 { 0x2001, 0x3c05, FLAG_TYPE_AX88772 },
Lucas Stach1dff9d02012-08-22 11:05:01 +0000633 /* ASIX 88772B */
634 { 0x0b95, 0x772b, FLAG_TYPE_AX88772B | FLAG_EEPROM_MAC },
Alexandre Courbota7f24722014-10-09 12:43:30 +0900635 { 0x0b95, 0x7e2b, FLAG_TYPE_AX88772B },
Lucas Stach58f8fab2012-08-22 11:04:59 +0000636 { 0x0000, 0x0000, FLAG_NONE } /* END - Do not remove */
Simon Glass9b70e002011-02-16 11:14:34 -0800637};
638
639/* Probe to see if a new device is actually an asix device */
640int asix_eth_probe(struct usb_device *dev, unsigned int ifnum,
641 struct ueth_data *ss)
642{
643 struct usb_interface *iface;
644 struct usb_interface_descriptor *iface_desc;
Lucas Stach1dff9d02012-08-22 11:05:01 +0000645 int ep_in_found = 0, ep_out_found = 0;
Simon Glass9b70e002011-02-16 11:14:34 -0800646 int i;
647
648 /* let's examine the device now */
649 iface = &dev->config.if_desc[ifnum];
650 iface_desc = &dev->config.if_desc[ifnum].desc;
651
652 for (i = 0; asix_dongles[i].vendor != 0; i++) {
653 if (dev->descriptor.idVendor == asix_dongles[i].vendor &&
654 dev->descriptor.idProduct == asix_dongles[i].product)
655 /* Found a supported dongle */
656 break;
657 }
658
659 if (asix_dongles[i].vendor == 0)
660 return 0;
661
662 memset(ss, 0, sizeof(struct ueth_data));
663
664 /* At this point, we know we've got a live one */
665 debug("\n\nUSB Ethernet device detected: %#04x:%#04x\n",
666 dev->descriptor.idVendor, dev->descriptor.idProduct);
667
668 /* Initialize the ueth_data structure with some useful info */
669 ss->ifnum = ifnum;
670 ss->pusb_dev = dev;
671 ss->subclass = iface_desc->bInterfaceSubClass;
672 ss->protocol = iface_desc->bInterfaceProtocol;
673
Lucas Stach58f8fab2012-08-22 11:04:59 +0000674 /* alloc driver private */
675 ss->dev_priv = calloc(1, sizeof(struct asix_private));
676 if (!ss->dev_priv)
677 return 0;
678
679 ((struct asix_private *)ss->dev_priv)->flags = asix_dongles[i].flags;
680
Simon Glass9b70e002011-02-16 11:14:34 -0800681 /*
682 * We are expecting a minimum of 3 endpoints - in, out (bulk), and
683 * int. We will ignore any others.
684 */
685 for (i = 0; i < iface_desc->bNumEndpoints; i++) {
686 /* is it an BULK endpoint? */
687 if ((iface->ep_desc[i].bmAttributes &
688 USB_ENDPOINT_XFERTYPE_MASK) == USB_ENDPOINT_XFER_BULK) {
Lucas Stach1dff9d02012-08-22 11:05:01 +0000689 u8 ep_addr = iface->ep_desc[i].bEndpointAddress;
690 if (ep_addr & USB_DIR_IN) {
691 if (!ep_in_found) {
692 ss->ep_in = ep_addr &
693 USB_ENDPOINT_NUMBER_MASK;
694 ep_in_found = 1;
695 }
696 } else {
697 if (!ep_out_found) {
698 ss->ep_out = ep_addr &
699 USB_ENDPOINT_NUMBER_MASK;
700 ep_out_found = 1;
701 }
702 }
Simon Glass9b70e002011-02-16 11:14:34 -0800703 }
704
705 /* is it an interrupt endpoint? */
706 if ((iface->ep_desc[i].bmAttributes &
707 USB_ENDPOINT_XFERTYPE_MASK) == USB_ENDPOINT_XFER_INT) {
708 ss->ep_int = iface->ep_desc[i].bEndpointAddress &
709 USB_ENDPOINT_NUMBER_MASK;
710 ss->irqinterval = iface->ep_desc[i].bInterval;
711 }
712 }
713 debug("Endpoints In %d Out %d Int %d\n",
714 ss->ep_in, ss->ep_out, ss->ep_int);
715
716 /* Do some basic sanity checks, and bail if we find a problem */
717 if (usb_set_interface(dev, iface_desc->bInterfaceNumber, 0) ||
718 !ss->ep_in || !ss->ep_out || !ss->ep_int) {
719 debug("Problems with device\n");
720 return 0;
721 }
722 dev->privptr = (void *)ss;
723 return 1;
724}
725
726int asix_eth_get_info(struct usb_device *dev, struct ueth_data *ss,
727 struct eth_device *eth)
728{
Lucas Stach58f8fab2012-08-22 11:04:59 +0000729 struct asix_private *priv = (struct asix_private *)ss->dev_priv;
730
Simon Glass9b70e002011-02-16 11:14:34 -0800731 if (!eth) {
732 debug("%s: missing parameter.\n", __func__);
733 return 0;
734 }
735 sprintf(eth->name, "%s%d", ASIX_BASE_NAME, curr_eth_dev++);
736 eth->init = asix_init;
737 eth->send = asix_send;
738 eth->recv = asix_recv;
739 eth->halt = asix_halt;
Lucas Stach58f8fab2012-08-22 11:04:59 +0000740 if (!(priv->flags & FLAG_TYPE_AX88172))
741 eth->write_hwaddr = asix_write_hwaddr;
Simon Glass9b70e002011-02-16 11:14:34 -0800742 eth->priv = ss;
743
Lucas Stach5fae53d2012-08-22 11:04:58 +0000744 if (asix_basic_reset(ss))
745 return 0;
746
Lucas Stach02c8d8c2012-08-22 11:05:00 +0000747 /* Get the MAC address */
Simon Glassfbc4b8a2015-07-06 16:47:54 -0600748 if (asix_read_mac_common(ss, priv, eth->enetaddr))
Lucas Stach02c8d8c2012-08-22 11:05:00 +0000749 return 0;
750 debug("MAC %pM\n", eth->enetaddr);
751
Simon Glass9b70e002011-02-16 11:14:34 -0800752 return 1;
753}
Simon Glassfbc4b8a2015-07-06 16:47:54 -0600754#endif
755
756#ifdef CONFIG_DM_ETH
757static int asix_eth_start(struct udevice *dev)
758{
Marcel Ziswiler14727122015-08-05 17:16:59 +0200759 struct eth_pdata *pdata = dev_get_platdata(dev);
Simon Glassfbc4b8a2015-07-06 16:47:54 -0600760 struct asix_private *priv = dev_get_priv(dev);
761
Marcel Ziswiler14727122015-08-05 17:16:59 +0200762 return asix_init_common(&priv->ueth, pdata->enetaddr);
Simon Glassfbc4b8a2015-07-06 16:47:54 -0600763}
764
765void asix_eth_stop(struct udevice *dev)
766{
767 debug("** %s()\n", __func__);
768}
769
770int asix_eth_send(struct udevice *dev, void *packet, int length)
771{
772 struct asix_private *priv = dev_get_priv(dev);
773
774 return asix_send_common(&priv->ueth, packet, length);
775}
776
777int asix_eth_recv(struct udevice *dev, int flags, uchar **packetp)
778{
779 struct asix_private *priv = dev_get_priv(dev);
780 struct ueth_data *ueth = &priv->ueth;
781 uint8_t *ptr;
782 int ret, len;
783 u32 packet_len;
784
785 len = usb_ether_get_rx_bytes(ueth, &ptr);
786 debug("%s: first try, len=%d\n", __func__, len);
787 if (!len) {
788 if (!(flags & ETH_RECV_CHECK_DEVICE))
789 return -EAGAIN;
790 ret = usb_ether_receive(ueth, AX_RX_URB_SIZE);
791 if (ret == -EAGAIN)
792 return ret;
793
794 len = usb_ether_get_rx_bytes(ueth, &ptr);
795 debug("%s: second try, len=%d\n", __func__, len);
796 }
797
798 /*
799 * 1st 4 bytes contain the length of the actual data as two
800 * complementary 16-bit words. Extract the length of the data.
801 */
802 if (len < sizeof(packet_len)) {
803 debug("Rx: incomplete packet length\n");
804 goto err;
805 }
806 memcpy(&packet_len, ptr, sizeof(packet_len));
807 le32_to_cpus(&packet_len);
808 if (((~packet_len >> 16) & 0x7ff) != (packet_len & 0x7ff)) {
809 debug("Rx: malformed packet length: %#x (%#x:%#x)\n",
810 packet_len, (~packet_len >> 16) & 0x7ff,
811 packet_len & 0x7ff);
812 goto err;
813 }
814 packet_len = packet_len & 0x7ff;
815 if (packet_len > len - sizeof(packet_len)) {
816 debug("Rx: too large packet: %d\n", packet_len);
817 goto err;
818 }
819
820 *packetp = ptr + sizeof(packet_len);
821 return packet_len;
822
823err:
824 usb_ether_advance_rxbuf(ueth, -1);
825 return -EINVAL;
826}
827
828static int asix_free_pkt(struct udevice *dev, uchar *packet, int packet_len)
829{
830 struct asix_private *priv = dev_get_priv(dev);
831
832 if (packet_len & 1)
833 packet_len++;
834 usb_ether_advance_rxbuf(&priv->ueth, sizeof(u32) + packet_len);
835
836 return 0;
837}
838
839int asix_write_hwaddr(struct udevice *dev)
840{
841 struct eth_pdata *pdata = dev_get_platdata(dev);
842 struct asix_private *priv = dev_get_priv(dev);
843
844 if (priv->flags & FLAG_TYPE_AX88172)
845 return -ENOSYS;
846
847 return asix_write_hwaddr_common(&priv->ueth, pdata->enetaddr);
848}
849
850static int asix_eth_probe(struct udevice *dev)
851{
852 struct eth_pdata *pdata = dev_get_platdata(dev);
853 struct asix_private *priv = dev_get_priv(dev);
854 struct ueth_data *ss = &priv->ueth;
855 int ret;
856
857 priv->flags = dev->driver_data;
858 ret = usb_ether_register(dev, ss, AX_RX_URB_SIZE);
859 if (ret)
860 return ret;
861
862 ret = asix_basic_reset(ss);
863 if (ret)
864 goto err;
865
866 /* Get the MAC address */
867 ret = asix_read_mac_common(ss, priv, pdata->enetaddr);
868 if (ret)
869 goto err;
870 debug("MAC %pM\n", pdata->enetaddr);
871
872 return 0;
873
874err:
875 return usb_ether_deregister(ss);
876}
877
878static const struct eth_ops asix_eth_ops = {
879 .start = asix_eth_start,
880 .send = asix_eth_send,
881 .recv = asix_eth_recv,
882 .free_pkt = asix_free_pkt,
883 .stop = asix_eth_stop,
884 .write_hwaddr = asix_write_hwaddr,
885};
886
887U_BOOT_DRIVER(asix_eth) = {
888 .name = "asix_eth",
889 .id = UCLASS_ETH,
890 .probe = asix_eth_probe,
891 .ops = &asix_eth_ops,
892 .priv_auto_alloc_size = sizeof(struct asix_private),
893 .platdata_auto_alloc_size = sizeof(struct eth_pdata),
894};
895
896static const struct usb_device_id asix_eth_id_table[] = {
897 /* Apple USB Ethernet Adapter */
898 { USB_DEVICE(0x05ac, 0x1402), .driver_info = FLAG_TYPE_AX88772 },
899 /* D-Link DUB-E100 H/W Ver B1 */
900 { USB_DEVICE(0x07d1, 0x3c05), .driver_info = FLAG_TYPE_AX88772 },
901 /* D-Link DUB-E100 H/W Ver C1 */
902 { USB_DEVICE(0x2001, 0x1a02), .driver_info = FLAG_TYPE_AX88772 },
903 /* Cables-to-Go USB Ethernet Adapter */
904 { USB_DEVICE(0x0b95, 0x772a), .driver_info = FLAG_TYPE_AX88772 },
905 /* Trendnet TU2-ET100 V3.0R */
906 { USB_DEVICE(0x0b95, 0x7720), .driver_info = FLAG_TYPE_AX88772 },
907 /* SMC */
908 { USB_DEVICE(0x0b95, 0x1720), .driver_info = FLAG_TYPE_AX88172 },
909 /* MSI - ASIX 88772a */
910 { USB_DEVICE(0x0db0, 0xa877), .driver_info = FLAG_TYPE_AX88772 },
911 /* Linksys 200M v2.1 */
912 { USB_DEVICE(0x13b1, 0x0018), .driver_info = FLAG_TYPE_AX88172 },
913 /* 0Q0 cable ethernet */
914 { USB_DEVICE(0x1557, 0x7720), .driver_info = FLAG_TYPE_AX88772 },
915 /* DLink DUB-E100 H/W Ver B1 Alternate */
916 { USB_DEVICE(0x2001, 0x3c05), .driver_info = FLAG_TYPE_AX88772 },
917 /* ASIX 88772B */
918 { USB_DEVICE(0x0b95, 0x772b),
919 .driver_info = FLAG_TYPE_AX88772B | FLAG_EEPROM_MAC },
920 { USB_DEVICE(0x0b95, 0x7e2b), .driver_info = FLAG_TYPE_AX88772B },
921 { } /* Terminating entry */
922};
923
924U_BOOT_USB_DEVICE(asix_eth, asix_eth_id_table);
925#endif