Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Simon Glass | 246d693 | 2011-02-16 11:14:34 -0800 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (c) 2011 The Chromium OS Authors. |
Simon Glass | 246d693 | 2011-02-16 11:14:34 -0800 | [diff] [blame] | 4 | * |
Marcel Ziswiler | 7bbbb3c | 2015-08-05 17:16:59 +0200 | [diff] [blame] | 5 | * Patched for AX88772B by Antmicro Ltd <www.antmicro.com> |
Simon Glass | 246d693 | 2011-02-16 11:14:34 -0800 | [diff] [blame] | 6 | */ |
| 7 | |
Simon Glass | f58369f | 2015-07-06 16:47:54 -0600 | [diff] [blame] | 8 | #include <dm.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 9 | #include <log.h> |
Simon Glass | 274e0b0 | 2020-05-10 11:39:56 -0600 | [diff] [blame] | 10 | #include <net.h> |
Simon Glass | 246d693 | 2011-02-16 11:14:34 -0800 | [diff] [blame] | 11 | #include <usb.h> |
Simon Glass | f58369f | 2015-07-06 16:47:54 -0600 | [diff] [blame] | 12 | #include <malloc.h> |
Simon Glass | 2dd337a | 2015-09-02 17:24:58 -0600 | [diff] [blame] | 13 | #include <memalign.h> |
Simon Glass | dbd7954 | 2020-05-10 11:40:11 -0600 | [diff] [blame] | 14 | #include <linux/delay.h> |
Simon Glass | 246d693 | 2011-02-16 11:14:34 -0800 | [diff] [blame] | 15 | #include <linux/mii.h> |
| 16 | #include "usb_ether.h" |
Simon Glass | 246d693 | 2011-02-16 11:14:34 -0800 | [diff] [blame] | 17 | |
| 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 Stach | 95c359b | 2012-08-22 11:05:00 +0000 | [diff] [blame] | 24 | #define AX_CMD_READ_EEPROM 0x0b |
Simon Glass | 246d693 | 2011-02-16 11:14:34 -0800 | [diff] [blame] | 25 | #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 Stach | fc9a758 | 2012-08-22 11:04:59 +0000 | [diff] [blame] | 29 | #define AX_CMD_WRITE_NODE_ID 0x14 |
Simon Glass | 246d693 | 2011-02-16 11:14:34 -0800 | [diff] [blame] | 30 | #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 Bedel | 3cf1a96 | 2016-09-10 03:54:09 +0200 | [diff] [blame] | 71 | #define AX_RX_CTL_SO 0x0080 |
| 72 | #define AX_RX_CTL_AB 0x0008 |
Simon Glass | 246d693 | 2011-02-16 11:14:34 -0800 | [diff] [blame] | 73 | |
| 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 Stach | fc9a758 | 2012-08-22 11:04:59 +0000 | [diff] [blame] | 92 | /* asix_flags defines */ |
| 93 | #define FLAG_NONE 0 |
| 94 | #define FLAG_TYPE_AX88172 (1U << 0) |
| 95 | #define FLAG_TYPE_AX88772 (1U << 1) |
Lucas Stach | 06e4693 | 2012-08-22 11:05:01 +0000 | [diff] [blame] | 96 | #define FLAG_TYPE_AX88772B (1U << 2) |
| 97 | #define FLAG_EEPROM_MAC (1U << 3) /* initial mac address in eeprom */ |
Lucas Stach | fc9a758 | 2012-08-22 11:04:59 +0000 | [diff] [blame] | 98 | |
Simon Glass | 246d693 | 2011-02-16 11:14:34 -0800 | [diff] [blame] | 99 | |
Lucas Stach | fc9a758 | 2012-08-22 11:04:59 +0000 | [diff] [blame] | 100 | /* driver private */ |
| 101 | struct asix_private { |
| 102 | int flags; |
Simon Glass | f58369f | 2015-07-06 16:47:54 -0600 | [diff] [blame] | 103 | struct ueth_data ueth; |
Lucas Stach | fc9a758 | 2012-08-22 11:04:59 +0000 | [diff] [blame] | 104 | }; |
| 105 | |
Simon Glass | 246d693 | 2011-02-16 11:14:34 -0800 | [diff] [blame] | 106 | /* |
| 107 | * Asix infrastructure commands |
| 108 | */ |
| 109 | static 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 | |
| 131 | static 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 | |
| 152 | static 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 | |
| 162 | static 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 | |
| 172 | static int asix_mdio_read(struct ueth_data *dev, int phy_id, int loc) |
| 173 | { |
Marek Vasut | 115436e | 2012-06-24 14:17:56 +0000 | [diff] [blame] | 174 | ALLOC_CACHE_ALIGN_BUFFER(__le16, res, 1); |
Simon Glass | 246d693 | 2011-02-16 11:14:34 -0800 | [diff] [blame] | 175 | |
| 176 | asix_set_sw_mii(dev); |
Marek Vasut | 115436e | 2012-06-24 14:17:56 +0000 | [diff] [blame] | 177 | asix_read_cmd(dev, AX_CMD_READ_MII_REG, phy_id, (__u16)loc, 2, res); |
Simon Glass | 246d693 | 2011-02-16 11:14:34 -0800 | [diff] [blame] | 178 | asix_set_hw_mii(dev); |
| 179 | |
| 180 | debug("asix_mdio_read() phy_id=0x%02x, loc=0x%02x, returns=0x%04x\n", |
Marek Vasut | 115436e | 2012-06-24 14:17:56 +0000 | [diff] [blame] | 181 | phy_id, loc, le16_to_cpu(*res)); |
Simon Glass | 246d693 | 2011-02-16 11:14:34 -0800 | [diff] [blame] | 182 | |
Marek Vasut | 115436e | 2012-06-24 14:17:56 +0000 | [diff] [blame] | 183 | return le16_to_cpu(*res); |
Simon Glass | 246d693 | 2011-02-16 11:14:34 -0800 | [diff] [blame] | 184 | } |
| 185 | |
| 186 | static void |
| 187 | asix_mdio_write(struct ueth_data *dev, int phy_id, int loc, int val) |
| 188 | { |
Marek Vasut | 115436e | 2012-06-24 14:17:56 +0000 | [diff] [blame] | 189 | ALLOC_CACHE_ALIGN_BUFFER(__le16, res, 1); |
| 190 | *res = cpu_to_le16(val); |
Simon Glass | 246d693 | 2011-02-16 11:14:34 -0800 | [diff] [blame] | 191 | |
| 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 Vasut | 115436e | 2012-06-24 14:17:56 +0000 | [diff] [blame] | 195 | asix_write_cmd(dev, AX_CMD_WRITE_MII_REG, phy_id, (__u16)loc, 2, res); |
Simon Glass | 246d693 | 2011-02-16 11:14:34 -0800 | [diff] [blame] | 196 | asix_set_hw_mii(dev); |
| 197 | } |
| 198 | |
| 199 | /* |
| 200 | * Asix "high level" commands |
| 201 | */ |
| 202 | static 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 | |
| 215 | static inline int asix_get_phy_addr(struct ueth_data *dev) |
| 216 | { |
Marek Vasut | 115436e | 2012-06-24 14:17:56 +0000 | [diff] [blame] | 217 | ALLOC_CACHE_ALIGN_BUFFER(u8, buf, 2); |
| 218 | |
Simon Glass | 246d693 | 2011-02-16 11:14:34 -0800 | [diff] [blame] | 219 | 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 Vasut | a61b966 | 2011-09-23 21:13:35 +0200 | [diff] [blame] | 227 | debug("asix_get_phy_addr() returning 0x%02x%02x\n", buf[0], buf[1]); |
Simon Glass | 246d693 | 2011-02-16 11:14:34 -0800 | [diff] [blame] | 228 | ret = buf[1]; |
| 229 | |
| 230 | out: |
| 231 | return ret; |
| 232 | } |
| 233 | |
| 234 | static 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 | |
| 248 | static u16 asix_read_rx_ctl(struct ueth_data *dev) |
| 249 | { |
Marek Vasut | 115436e | 2012-06-24 14:17:56 +0000 | [diff] [blame] | 250 | 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 Glass | 246d693 | 2011-02-16 11:14:34 -0800 | [diff] [blame] | 253 | |
| 254 | if (ret < 0) |
| 255 | debug("Error reading RX_CTL register: %02x\n", ret); |
| 256 | else |
Marek Vasut | 115436e | 2012-06-24 14:17:56 +0000 | [diff] [blame] | 257 | ret = le16_to_cpu(*v); |
Simon Glass | 246d693 | 2011-02-16 11:14:34 -0800 | [diff] [blame] | 258 | return ret; |
| 259 | } |
| 260 | |
| 261 | static 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 | |
| 274 | static 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 Glass | f58369f | 2015-07-06 16:47:54 -0600 | [diff] [blame] | 290 | static int asix_write_hwaddr_common(struct ueth_data *dev, uint8_t *enetaddr) |
Lucas Stach | fc9a758 | 2012-08-22 11:04:59 +0000 | [diff] [blame] | 291 | { |
Lucas Stach | fc9a758 | 2012-08-22 11:04:59 +0000 | [diff] [blame] | 292 | int ret; |
| 293 | ALLOC_CACHE_ALIGN_BUFFER(unsigned char, buf, ETH_ALEN); |
| 294 | |
Simon Glass | f58369f | 2015-07-06 16:47:54 -0600 | [diff] [blame] | 295 | memcpy(buf, enetaddr, ETH_ALEN); |
Lucas Stach | fc9a758 | 2012-08-22 11:04:59 +0000 | [diff] [blame] | 296 | |
| 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 Glass | 246d693 | 2011-02-16 11:14:34 -0800 | [diff] [blame] | 304 | /* |
| 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 | */ |
| 313 | static 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 Glass | f58369f | 2015-07-06 16:47:54 -0600 | [diff] [blame] | 330 | static int asix_read_mac_common(struct ueth_data *dev, |
| 331 | struct asix_private *priv, uint8_t *enetaddr) |
Lucas Stach | 95c359b | 2012-08-22 11:05:00 +0000 | [diff] [blame] | 332 | { |
Lucas Stach | 95c359b | 2012-08-22 11:05:00 +0000 | [diff] [blame] | 333 | ALLOC_CACHE_ALIGN_BUFFER(unsigned char, buf, ETH_ALEN); |
Simon Glass | f58369f | 2015-07-06 16:47:54 -0600 | [diff] [blame] | 334 | int i; |
Lucas Stach | 95c359b | 2012-08-22 11:05:00 +0000 | [diff] [blame] | 335 | |
| 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 Glass | f58369f | 2015-07-06 16:47:54 -0600 | [diff] [blame] | 343 | memcpy(enetaddr + i * 2, buf, 2); |
Lucas Stach | 95c359b | 2012-08-22 11:05:00 +0000 | [diff] [blame] | 344 | } |
| 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 Glass | f58369f | 2015-07-06 16:47:54 -0600 | [diff] [blame] | 351 | memcpy(enetaddr, buf, ETH_ALEN); |
Lucas Stach | 95c359b | 2012-08-22 11:05:00 +0000 | [diff] [blame] | 352 | } |
| 353 | |
| 354 | return 0; |
| 355 | } |
| 356 | |
Lucas Stach | a0d5d03 | 2012-08-22 11:04:58 +0000 | [diff] [blame] | 357 | static int asix_basic_reset(struct ueth_data *dev) |
Simon Glass | 246d693 | 2011-02-16 11:14:34 -0800 | [diff] [blame] | 358 | { |
| 359 | int embd_phy; |
Simon Glass | 246d693 | 2011-02-16 11:14:34 -0800 | [diff] [blame] | 360 | u16 rx_ctl; |
Simon Glass | 246d693 | 2011-02-16 11:14:34 -0800 | [diff] [blame] | 361 | |
| 362 | if (asix_write_gpio(dev, |
| 363 | AX_GPIO_RSE | AX_GPIO_GPO_2 | AX_GPIO_GPO2EN, 5) < 0) |
Lucas Stach | a0d5d03 | 2012-08-22 11:04:58 +0000 | [diff] [blame] | 364 | return -1; |
Simon Glass | 246d693 | 2011-02-16 11:14:34 -0800 | [diff] [blame] | 365 | |
| 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 Stach | a0d5d03 | 2012-08-22 11:04:58 +0000 | [diff] [blame] | 371 | return -1; |
Simon Glass | 246d693 | 2011-02-16 11:14:34 -0800 | [diff] [blame] | 372 | } |
| 373 | |
| 374 | if (asix_sw_reset(dev, AX_SWRESET_IPPD | AX_SWRESET_PRL) < 0) |
Lucas Stach | a0d5d03 | 2012-08-22 11:04:58 +0000 | [diff] [blame] | 375 | return -1; |
Simon Glass | 246d693 | 2011-02-16 11:14:34 -0800 | [diff] [blame] | 376 | |
| 377 | if (asix_sw_reset(dev, AX_SWRESET_CLEAR) < 0) |
Lucas Stach | a0d5d03 | 2012-08-22 11:04:58 +0000 | [diff] [blame] | 378 | return -1; |
Simon Glass | 246d693 | 2011-02-16 11:14:34 -0800 | [diff] [blame] | 379 | |
| 380 | if (embd_phy) { |
| 381 | if (asix_sw_reset(dev, AX_SWRESET_IPRL) < 0) |
Lucas Stach | a0d5d03 | 2012-08-22 11:04:58 +0000 | [diff] [blame] | 382 | return -1; |
Simon Glass | 246d693 | 2011-02-16 11:14:34 -0800 | [diff] [blame] | 383 | } else { |
| 384 | if (asix_sw_reset(dev, AX_SWRESET_PRTE) < 0) |
Lucas Stach | a0d5d03 | 2012-08-22 11:04:58 +0000 | [diff] [blame] | 385 | return -1; |
Simon Glass | 246d693 | 2011-02-16 11:14:34 -0800 | [diff] [blame] | 386 | } |
| 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 Stach | a0d5d03 | 2012-08-22 11:04:58 +0000 | [diff] [blame] | 391 | return -1; |
Simon Glass | 246d693 | 2011-02-16 11:14:34 -0800 | [diff] [blame] | 392 | |
| 393 | rx_ctl = asix_read_rx_ctl(dev); |
| 394 | debug("RX_CTL is 0x%04x setting to 0x0000\n", rx_ctl); |
| 395 | |
Simon Glass | 246d693 | 2011-02-16 11:14:34 -0800 | [diff] [blame] | 396 | dev->phy_id = asix_get_phy_addr(dev); |
| 397 | if (dev->phy_id < 0) |
| 398 | debug("Failed to read phy id\n"); |
| 399 | |
Simon Glass | 246d693 | 2011-02-16 11:14:34 -0800 | [diff] [blame] | 400 | 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 Werner | 160298f | 2013-05-11 13:35:02 -0700 | [diff] [blame] | 406 | return -1; |
Simon Glass | 246d693 | 2011-02-16 11:14:34 -0800 | [diff] [blame] | 407 | |
| 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 Werner | 160298f | 2013-05-11 13:35:02 -0700 | [diff] [blame] | 412 | return -1; |
Simon Glass | 246d693 | 2011-02-16 11:14:34 -0800 | [diff] [blame] | 413 | } |
| 414 | |
Julius Werner | 160298f | 2013-05-11 13:35:02 -0700 | [diff] [blame] | 415 | return 0; |
| 416 | } |
| 417 | |
Marcel Ziswiler | 7bbbb3c | 2015-08-05 17:16:59 +0200 | [diff] [blame] | 418 | static int asix_init_common(struct ueth_data *dev, uint8_t *enetaddr) |
Julius Werner | 160298f | 2013-05-11 13:35:02 -0700 | [diff] [blame] | 419 | { |
Julius Werner | 160298f | 2013-05-11 13:35:02 -0700 | [diff] [blame] | 420 | int timeout = 0; |
| 421 | #define TIMEOUT_RESOLUTION 50 /* ms */ |
| 422 | int link_detected; |
| 423 | |
| 424 | debug("** %s()\n", __func__); |
| 425 | |
Alban Bedel | 3cf1a96 | 2016-09-10 03:54:09 +0200 | [diff] [blame] | 426 | if (asix_write_rx_ctl(dev, AX_DEFAULT_RX_CTL) < 0) |
Marcel Ziswiler | 7bbbb3c | 2015-08-05 17:16:59 +0200 | [diff] [blame] | 427 | goto out_err; |
| 428 | |
| 429 | if (asix_write_hwaddr_common(dev, enetaddr) < 0) |
Simon Glass | 246d693 | 2011-02-16 11:14:34 -0800 | [diff] [blame] | 430 | goto out_err; |
| 431 | |
| 432 | do { |
| 433 | link_detected = asix_mdio_read(dev, dev->phy_id, MII_BMSR) & |
| 434 | BMSR_LSTATUS; |
| 435 | if (!link_detected) { |
| 436 | if (timeout == 0) |
| 437 | printf("Waiting for Ethernet connection... "); |
| 438 | udelay(TIMEOUT_RESOLUTION * 1000); |
| 439 | timeout += TIMEOUT_RESOLUTION; |
| 440 | } |
| 441 | } while (!link_detected && timeout < PHY_CONNECT_TIMEOUT); |
| 442 | if (link_detected) { |
| 443 | if (timeout != 0) |
| 444 | printf("done.\n"); |
| 445 | } else { |
| 446 | printf("unable to connect.\n"); |
| 447 | goto out_err; |
| 448 | } |
| 449 | |
Marcel Ziswiler | 7bbbb3c | 2015-08-05 17:16:59 +0200 | [diff] [blame] | 450 | /* |
| 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 Glass | 246d693 | 2011-02-16 11:14:34 -0800 | [diff] [blame] | 456 | return 0; |
| 457 | out_err: |
| 458 | return -1; |
| 459 | } |
| 460 | |
Simon Glass | f58369f | 2015-07-06 16:47:54 -0600 | [diff] [blame] | 461 | static int asix_send_common(struct ueth_data *dev, void *packet, int length) |
Simon Glass | 246d693 | 2011-02-16 11:14:34 -0800 | [diff] [blame] | 462 | { |
Simon Glass | 246d693 | 2011-02-16 11:14:34 -0800 | [diff] [blame] | 463 | int err; |
| 464 | u32 packet_len; |
| 465 | int actual_len; |
Marek Vasut | 115436e | 2012-06-24 14:17:56 +0000 | [diff] [blame] | 466 | ALLOC_CACHE_ALIGN_BUFFER(unsigned char, msg, |
| 467 | PKTSIZE + sizeof(packet_len)); |
Simon Glass | 246d693 | 2011-02-16 11:14:34 -0800 | [diff] [blame] | 468 | |
| 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 Glass | 246d693 | 2011-02-16 11:14:34 -0800 | [diff] [blame] | 476 | |
| 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 Reding | 7306e9c | 2015-03-20 12:41:23 +0100 | [diff] [blame] | 483 | debug("Tx: len = %zu, actual = %u, err = %d\n", |
Simon Glass | 246d693 | 2011-02-16 11:14:34 -0800 | [diff] [blame] | 484 | length + sizeof(packet_len), actual_len, err); |
| 485 | |
| 486 | return err; |
| 487 | } |
Simon Glass | f58369f | 2015-07-06 16:47:54 -0600 | [diff] [blame] | 488 | |
Simon Glass | f58369f | 2015-07-06 16:47:54 -0600 | [diff] [blame] | 489 | static int asix_eth_start(struct udevice *dev) |
| 490 | { |
Simon Glass | fa20e93 | 2020-12-03 16:55:20 -0700 | [diff] [blame] | 491 | struct eth_pdata *pdata = dev_get_plat(dev); |
Simon Glass | f58369f | 2015-07-06 16:47:54 -0600 | [diff] [blame] | 492 | struct asix_private *priv = dev_get_priv(dev); |
| 493 | |
Marcel Ziswiler | 7bbbb3c | 2015-08-05 17:16:59 +0200 | [diff] [blame] | 494 | return asix_init_common(&priv->ueth, pdata->enetaddr); |
Simon Glass | f58369f | 2015-07-06 16:47:54 -0600 | [diff] [blame] | 495 | } |
| 496 | |
| 497 | void asix_eth_stop(struct udevice *dev) |
| 498 | { |
| 499 | debug("** %s()\n", __func__); |
| 500 | } |
| 501 | |
| 502 | int 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 | |
| 509 | int 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 | |
| 555 | err: |
| 556 | usb_ether_advance_rxbuf(ueth, -1); |
| 557 | return -EINVAL; |
| 558 | } |
| 559 | |
| 560 | static 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 | |
| 571 | int asix_write_hwaddr(struct udevice *dev) |
| 572 | { |
Simon Glass | fa20e93 | 2020-12-03 16:55:20 -0700 | [diff] [blame] | 573 | struct eth_pdata *pdata = dev_get_plat(dev); |
Simon Glass | f58369f | 2015-07-06 16:47:54 -0600 | [diff] [blame] | 574 | 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 | |
| 582 | static int asix_eth_probe(struct udevice *dev) |
| 583 | { |
Simon Glass | fa20e93 | 2020-12-03 16:55:20 -0700 | [diff] [blame] | 584 | struct eth_pdata *pdata = dev_get_plat(dev); |
Simon Glass | f58369f | 2015-07-06 16:47:54 -0600 | [diff] [blame] | 585 | 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 | |
| 606 | err: |
| 607 | return usb_ether_deregister(ss); |
| 608 | } |
| 609 | |
| 610 | static 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 | |
| 619 | U_BOOT_DRIVER(asix_eth) = { |
| 620 | .name = "asix_eth", |
| 621 | .id = UCLASS_ETH, |
| 622 | .probe = asix_eth_probe, |
| 623 | .ops = &asix_eth_ops, |
Simon Glass | 8a2b47f | 2020-12-03 16:55:17 -0700 | [diff] [blame] | 624 | .priv_auto = sizeof(struct asix_private), |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 625 | .plat_auto = sizeof(struct eth_pdata), |
Simon Glass | f58369f | 2015-07-06 16:47:54 -0600 | [diff] [blame] | 626 | }; |
| 627 | |
| 628 | static 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 | |
| 656 | U_BOOT_USB_DEVICE(asix_eth, asix_eth_id_table); |