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 | |
Lucas Stach | fc9a758 | 2012-08-22 11:04:59 +0000 | [diff] [blame] | 99 | /* driver private */ |
| 100 | struct asix_private { |
| 101 | int flags; |
Simon Glass | f58369f | 2015-07-06 16:47:54 -0600 | [diff] [blame] | 102 | struct ueth_data ueth; |
Lucas Stach | fc9a758 | 2012-08-22 11:04:59 +0000 | [diff] [blame] | 103 | }; |
| 104 | |
Simon Glass | 246d693 | 2011-02-16 11:14:34 -0800 | [diff] [blame] | 105 | /* |
| 106 | * Asix infrastructure commands |
| 107 | */ |
| 108 | static 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 | |
| 130 | static 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 | |
| 151 | static 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 | |
| 161 | static 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 | |
| 171 | static int asix_mdio_read(struct ueth_data *dev, int phy_id, int loc) |
| 172 | { |
Marek Vasut | 115436e | 2012-06-24 14:17:56 +0000 | [diff] [blame] | 173 | ALLOC_CACHE_ALIGN_BUFFER(__le16, res, 1); |
Simon Glass | 246d693 | 2011-02-16 11:14:34 -0800 | [diff] [blame] | 174 | |
| 175 | asix_set_sw_mii(dev); |
Marek Vasut | 115436e | 2012-06-24 14:17:56 +0000 | [diff] [blame] | 176 | 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] | 177 | asix_set_hw_mii(dev); |
| 178 | |
| 179 | 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] | 180 | phy_id, loc, le16_to_cpu(*res)); |
Simon Glass | 246d693 | 2011-02-16 11:14:34 -0800 | [diff] [blame] | 181 | |
Marek Vasut | 115436e | 2012-06-24 14:17:56 +0000 | [diff] [blame] | 182 | return le16_to_cpu(*res); |
Simon Glass | 246d693 | 2011-02-16 11:14:34 -0800 | [diff] [blame] | 183 | } |
| 184 | |
| 185 | static void |
| 186 | asix_mdio_write(struct ueth_data *dev, int phy_id, int loc, int val) |
| 187 | { |
Marek Vasut | 115436e | 2012-06-24 14:17:56 +0000 | [diff] [blame] | 188 | ALLOC_CACHE_ALIGN_BUFFER(__le16, res, 1); |
| 189 | *res = cpu_to_le16(val); |
Simon Glass | 246d693 | 2011-02-16 11:14:34 -0800 | [diff] [blame] | 190 | |
| 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 Vasut | 115436e | 2012-06-24 14:17:56 +0000 | [diff] [blame] | 194 | 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] | 195 | asix_set_hw_mii(dev); |
| 196 | } |
| 197 | |
| 198 | /* |
| 199 | * Asix "high level" commands |
| 200 | */ |
| 201 | static 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 | |
| 214 | static inline int asix_get_phy_addr(struct ueth_data *dev) |
| 215 | { |
Marek Vasut | 115436e | 2012-06-24 14:17:56 +0000 | [diff] [blame] | 216 | ALLOC_CACHE_ALIGN_BUFFER(u8, buf, 2); |
| 217 | |
Simon Glass | 246d693 | 2011-02-16 11:14:34 -0800 | [diff] [blame] | 218 | 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 Vasut | a61b966 | 2011-09-23 21:13:35 +0200 | [diff] [blame] | 226 | 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] | 227 | ret = buf[1]; |
| 228 | |
| 229 | out: |
| 230 | return ret; |
| 231 | } |
| 232 | |
| 233 | static 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 | |
| 247 | static u16 asix_read_rx_ctl(struct ueth_data *dev) |
| 248 | { |
Marek Vasut | 115436e | 2012-06-24 14:17:56 +0000 | [diff] [blame] | 249 | 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 Glass | 246d693 | 2011-02-16 11:14:34 -0800 | [diff] [blame] | 252 | |
| 253 | if (ret < 0) |
| 254 | debug("Error reading RX_CTL register: %02x\n", ret); |
| 255 | else |
Marek Vasut | 115436e | 2012-06-24 14:17:56 +0000 | [diff] [blame] | 256 | ret = le16_to_cpu(*v); |
Simon Glass | 246d693 | 2011-02-16 11:14:34 -0800 | [diff] [blame] | 257 | return ret; |
| 258 | } |
| 259 | |
| 260 | static 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 | |
| 273 | static 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 Glass | f58369f | 2015-07-06 16:47:54 -0600 | [diff] [blame] | 289 | 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] | 290 | { |
Lucas Stach | fc9a758 | 2012-08-22 11:04:59 +0000 | [diff] [blame] | 291 | int ret; |
| 292 | ALLOC_CACHE_ALIGN_BUFFER(unsigned char, buf, ETH_ALEN); |
| 293 | |
Simon Glass | f58369f | 2015-07-06 16:47:54 -0600 | [diff] [blame] | 294 | memcpy(buf, enetaddr, ETH_ALEN); |
Lucas Stach | fc9a758 | 2012-08-22 11:04:59 +0000 | [diff] [blame] | 295 | |
| 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 Glass | 246d693 | 2011-02-16 11:14:34 -0800 | [diff] [blame] | 303 | /* |
| 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 | */ |
| 312 | static 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 Glass | f58369f | 2015-07-06 16:47:54 -0600 | [diff] [blame] | 329 | static int asix_read_mac_common(struct ueth_data *dev, |
| 330 | struct asix_private *priv, uint8_t *enetaddr) |
Lucas Stach | 95c359b | 2012-08-22 11:05:00 +0000 | [diff] [blame] | 331 | { |
Lucas Stach | 95c359b | 2012-08-22 11:05:00 +0000 | [diff] [blame] | 332 | ALLOC_CACHE_ALIGN_BUFFER(unsigned char, buf, ETH_ALEN); |
Simon Glass | f58369f | 2015-07-06 16:47:54 -0600 | [diff] [blame] | 333 | int i; |
Lucas Stach | 95c359b | 2012-08-22 11:05:00 +0000 | [diff] [blame] | 334 | |
| 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 Glass | f58369f | 2015-07-06 16:47:54 -0600 | [diff] [blame] | 342 | memcpy(enetaddr + i * 2, buf, 2); |
Lucas Stach | 95c359b | 2012-08-22 11:05:00 +0000 | [diff] [blame] | 343 | } |
| 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 Glass | f58369f | 2015-07-06 16:47:54 -0600 | [diff] [blame] | 350 | memcpy(enetaddr, buf, ETH_ALEN); |
Lucas Stach | 95c359b | 2012-08-22 11:05:00 +0000 | [diff] [blame] | 351 | } |
| 352 | |
| 353 | return 0; |
| 354 | } |
| 355 | |
Lucas Stach | a0d5d03 | 2012-08-22 11:04:58 +0000 | [diff] [blame] | 356 | static int asix_basic_reset(struct ueth_data *dev) |
Simon Glass | 246d693 | 2011-02-16 11:14:34 -0800 | [diff] [blame] | 357 | { |
| 358 | int embd_phy; |
Simon Glass | 246d693 | 2011-02-16 11:14:34 -0800 | [diff] [blame] | 359 | u16 rx_ctl; |
Simon Glass | 246d693 | 2011-02-16 11:14:34 -0800 | [diff] [blame] | 360 | |
| 361 | if (asix_write_gpio(dev, |
| 362 | AX_GPIO_RSE | AX_GPIO_GPO_2 | AX_GPIO_GPO2EN, 5) < 0) |
Lucas Stach | a0d5d03 | 2012-08-22 11:04:58 +0000 | [diff] [blame] | 363 | return -1; |
Simon Glass | 246d693 | 2011-02-16 11:14:34 -0800 | [diff] [blame] | 364 | |
| 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 Stach | a0d5d03 | 2012-08-22 11:04:58 +0000 | [diff] [blame] | 370 | return -1; |
Simon Glass | 246d693 | 2011-02-16 11:14:34 -0800 | [diff] [blame] | 371 | } |
| 372 | |
| 373 | if (asix_sw_reset(dev, AX_SWRESET_IPPD | AX_SWRESET_PRL) < 0) |
Lucas Stach | a0d5d03 | 2012-08-22 11:04:58 +0000 | [diff] [blame] | 374 | return -1; |
Simon Glass | 246d693 | 2011-02-16 11:14:34 -0800 | [diff] [blame] | 375 | |
| 376 | if (asix_sw_reset(dev, AX_SWRESET_CLEAR) < 0) |
Lucas Stach | a0d5d03 | 2012-08-22 11:04:58 +0000 | [diff] [blame] | 377 | return -1; |
Simon Glass | 246d693 | 2011-02-16 11:14:34 -0800 | [diff] [blame] | 378 | |
| 379 | if (embd_phy) { |
| 380 | if (asix_sw_reset(dev, AX_SWRESET_IPRL) < 0) |
Lucas Stach | a0d5d03 | 2012-08-22 11:04:58 +0000 | [diff] [blame] | 381 | return -1; |
Simon Glass | 246d693 | 2011-02-16 11:14:34 -0800 | [diff] [blame] | 382 | } else { |
| 383 | if (asix_sw_reset(dev, AX_SWRESET_PRTE) < 0) |
Lucas Stach | a0d5d03 | 2012-08-22 11:04:58 +0000 | [diff] [blame] | 384 | return -1; |
Simon Glass | 246d693 | 2011-02-16 11:14:34 -0800 | [diff] [blame] | 385 | } |
| 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 Stach | a0d5d03 | 2012-08-22 11:04:58 +0000 | [diff] [blame] | 390 | return -1; |
Simon Glass | 246d693 | 2011-02-16 11:14:34 -0800 | [diff] [blame] | 391 | |
| 392 | rx_ctl = asix_read_rx_ctl(dev); |
| 393 | debug("RX_CTL is 0x%04x setting to 0x0000\n", rx_ctl); |
| 394 | |
Simon Glass | 246d693 | 2011-02-16 11:14:34 -0800 | [diff] [blame] | 395 | dev->phy_id = asix_get_phy_addr(dev); |
| 396 | if (dev->phy_id < 0) |
| 397 | debug("Failed to read phy id\n"); |
| 398 | |
Simon Glass | 246d693 | 2011-02-16 11:14:34 -0800 | [diff] [blame] | 399 | 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 Werner | 160298f | 2013-05-11 13:35:02 -0700 | [diff] [blame] | 405 | return -1; |
Simon Glass | 246d693 | 2011-02-16 11:14:34 -0800 | [diff] [blame] | 406 | |
| 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 Werner | 160298f | 2013-05-11 13:35:02 -0700 | [diff] [blame] | 411 | return -1; |
Simon Glass | 246d693 | 2011-02-16 11:14:34 -0800 | [diff] [blame] | 412 | } |
| 413 | |
Julius Werner | 160298f | 2013-05-11 13:35:02 -0700 | [diff] [blame] | 414 | return 0; |
| 415 | } |
| 416 | |
Marcel Ziswiler | 7bbbb3c | 2015-08-05 17:16:59 +0200 | [diff] [blame] | 417 | static int asix_init_common(struct ueth_data *dev, uint8_t *enetaddr) |
Julius Werner | 160298f | 2013-05-11 13:35:02 -0700 | [diff] [blame] | 418 | { |
Julius Werner | 160298f | 2013-05-11 13:35:02 -0700 | [diff] [blame] | 419 | int timeout = 0; |
| 420 | #define TIMEOUT_RESOLUTION 50 /* ms */ |
| 421 | int link_detected; |
| 422 | |
| 423 | debug("** %s()\n", __func__); |
| 424 | |
Alban Bedel | 3cf1a96 | 2016-09-10 03:54:09 +0200 | [diff] [blame] | 425 | if (asix_write_rx_ctl(dev, AX_DEFAULT_RX_CTL) < 0) |
Marcel Ziswiler | 7bbbb3c | 2015-08-05 17:16:59 +0200 | [diff] [blame] | 426 | goto out_err; |
| 427 | |
| 428 | if (asix_write_hwaddr_common(dev, enetaddr) < 0) |
Simon Glass | 246d693 | 2011-02-16 11:14:34 -0800 | [diff] [blame] | 429 | 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 Ziswiler | 7bbbb3c | 2015-08-05 17:16:59 +0200 | [diff] [blame] | 449 | /* |
| 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 Glass | 246d693 | 2011-02-16 11:14:34 -0800 | [diff] [blame] | 455 | return 0; |
| 456 | out_err: |
| 457 | return -1; |
| 458 | } |
| 459 | |
Simon Glass | f58369f | 2015-07-06 16:47:54 -0600 | [diff] [blame] | 460 | 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] | 461 | { |
Simon Glass | 246d693 | 2011-02-16 11:14:34 -0800 | [diff] [blame] | 462 | int err; |
| 463 | u32 packet_len; |
| 464 | int actual_len; |
Marek Vasut | 115436e | 2012-06-24 14:17:56 +0000 | [diff] [blame] | 465 | ALLOC_CACHE_ALIGN_BUFFER(unsigned char, msg, |
| 466 | PKTSIZE + sizeof(packet_len)); |
Simon Glass | 246d693 | 2011-02-16 11:14:34 -0800 | [diff] [blame] | 467 | |
| 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 Glass | 246d693 | 2011-02-16 11:14:34 -0800 | [diff] [blame] | 475 | |
| 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 Reding | 7306e9c | 2015-03-20 12:41:23 +0100 | [diff] [blame] | 482 | debug("Tx: len = %zu, actual = %u, err = %d\n", |
Simon Glass | 246d693 | 2011-02-16 11:14:34 -0800 | [diff] [blame] | 483 | length + sizeof(packet_len), actual_len, err); |
| 484 | |
| 485 | return err; |
| 486 | } |
Simon Glass | f58369f | 2015-07-06 16:47:54 -0600 | [diff] [blame] | 487 | |
Simon Glass | f58369f | 2015-07-06 16:47:54 -0600 | [diff] [blame] | 488 | static int asix_eth_start(struct udevice *dev) |
| 489 | { |
Simon Glass | fa20e93 | 2020-12-03 16:55:20 -0700 | [diff] [blame] | 490 | struct eth_pdata *pdata = dev_get_plat(dev); |
Simon Glass | f58369f | 2015-07-06 16:47:54 -0600 | [diff] [blame] | 491 | struct asix_private *priv = dev_get_priv(dev); |
| 492 | |
Marcel Ziswiler | 7bbbb3c | 2015-08-05 17:16:59 +0200 | [diff] [blame] | 493 | return asix_init_common(&priv->ueth, pdata->enetaddr); |
Simon Glass | f58369f | 2015-07-06 16:47:54 -0600 | [diff] [blame] | 494 | } |
| 495 | |
| 496 | void asix_eth_stop(struct udevice *dev) |
| 497 | { |
| 498 | debug("** %s()\n", __func__); |
| 499 | } |
| 500 | |
| 501 | int 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 | |
| 508 | int 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 | |
| 554 | err: |
| 555 | usb_ether_advance_rxbuf(ueth, -1); |
| 556 | return -EINVAL; |
| 557 | } |
| 558 | |
| 559 | static 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 | |
| 570 | int asix_write_hwaddr(struct udevice *dev) |
| 571 | { |
Simon Glass | fa20e93 | 2020-12-03 16:55:20 -0700 | [diff] [blame] | 572 | struct eth_pdata *pdata = dev_get_plat(dev); |
Simon Glass | f58369f | 2015-07-06 16:47:54 -0600 | [diff] [blame] | 573 | 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 | |
| 581 | static int asix_eth_probe(struct udevice *dev) |
| 582 | { |
Simon Glass | fa20e93 | 2020-12-03 16:55:20 -0700 | [diff] [blame] | 583 | struct eth_pdata *pdata = dev_get_plat(dev); |
Simon Glass | f58369f | 2015-07-06 16:47:54 -0600 | [diff] [blame] | 584 | 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 | |
| 605 | err: |
| 606 | return usb_ether_deregister(ss); |
| 607 | } |
| 608 | |
| 609 | static 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 | |
| 618 | U_BOOT_DRIVER(asix_eth) = { |
| 619 | .name = "asix_eth", |
| 620 | .id = UCLASS_ETH, |
| 621 | .probe = asix_eth_probe, |
| 622 | .ops = &asix_eth_ops, |
Simon Glass | 8a2b47f | 2020-12-03 16:55:17 -0700 | [diff] [blame] | 623 | .priv_auto = sizeof(struct asix_private), |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 624 | .plat_auto = sizeof(struct eth_pdata), |
Simon Glass | f58369f | 2015-07-06 16:47:54 -0600 | [diff] [blame] | 625 | }; |
| 626 | |
| 627 | static 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 | |
| 655 | U_BOOT_USB_DEVICE(asix_eth, asix_eth_id_table); |