Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Rene Griessl | 979beee | 2014-11-07 16:53:48 +0100 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (c) 2014 Rene Griessl <rgriessl@cit-ec.uni-bielefeld.de> |
| 4 | * based on the U-Boot Asix driver as well as information |
| 5 | * from the Linux AX88179_178a driver |
Rene Griessl | 979beee | 2014-11-07 16:53:48 +0100 | [diff] [blame] | 6 | */ |
| 7 | |
Alban Bedel | 5600d9c | 2016-08-09 11:10:03 +0200 | [diff] [blame] | 8 | #include <dm.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 9 | #include <log.h> |
Rene Griessl | 979beee | 2014-11-07 16:53:48 +0100 | [diff] [blame] | 10 | #include <usb.h> |
| 11 | #include <net.h> |
Simon Glass | dbd7954 | 2020-05-10 11:40:11 -0600 | [diff] [blame] | 12 | #include <linux/delay.h> |
Rene Griessl | 979beee | 2014-11-07 16:53:48 +0100 | [diff] [blame] | 13 | #include <linux/mii.h> |
| 14 | #include "usb_ether.h" |
| 15 | #include <malloc.h> |
Simon Glass | 2dd337a | 2015-09-02 17:24:58 -0600 | [diff] [blame] | 16 | #include <memalign.h> |
Rene Griessl | 979beee | 2014-11-07 16:53:48 +0100 | [diff] [blame] | 17 | #include <errno.h> |
| 18 | |
| 19 | /* ASIX AX88179 based USB 3.0 Ethernet Devices */ |
| 20 | #define AX88179_PHY_ID 0x03 |
| 21 | #define AX_EEPROM_LEN 0x100 |
| 22 | #define AX88179_EEPROM_MAGIC 0x17900b95 |
| 23 | #define AX_MCAST_FLTSIZE 8 |
| 24 | #define AX_MAX_MCAST 64 |
| 25 | #define AX_INT_PPLS_LINK (1 << 16) |
| 26 | #define AX_RXHDR_L4_TYPE_MASK 0x1c |
| 27 | #define AX_RXHDR_L4_TYPE_UDP 4 |
| 28 | #define AX_RXHDR_L4_TYPE_TCP 16 |
| 29 | #define AX_RXHDR_L3CSUM_ERR 2 |
| 30 | #define AX_RXHDR_L4CSUM_ERR 1 |
| 31 | #define AX_RXHDR_CRC_ERR (1 << 29) |
| 32 | #define AX_RXHDR_DROP_ERR (1 << 31) |
| 33 | #define AX_ENDPOINT_INT 0x01 |
| 34 | #define AX_ENDPOINT_IN 0x02 |
| 35 | #define AX_ENDPOINT_OUT 0x03 |
| 36 | #define AX_ACCESS_MAC 0x01 |
| 37 | #define AX_ACCESS_PHY 0x02 |
| 38 | #define AX_ACCESS_EEPROM 0x04 |
| 39 | #define AX_ACCESS_EFUS 0x05 |
| 40 | #define AX_PAUSE_WATERLVL_HIGH 0x54 |
| 41 | #define AX_PAUSE_WATERLVL_LOW 0x55 |
| 42 | |
| 43 | #define PHYSICAL_LINK_STATUS 0x02 |
| 44 | #define AX_USB_SS (1 << 2) |
| 45 | #define AX_USB_HS (1 << 1) |
| 46 | |
| 47 | #define GENERAL_STATUS 0x03 |
| 48 | #define AX_SECLD (1 << 2) |
| 49 | |
| 50 | #define AX_SROM_ADDR 0x07 |
| 51 | #define AX_SROM_CMD 0x0a |
| 52 | #define EEP_RD (1 << 2) |
| 53 | #define EEP_BUSY (1 << 4) |
| 54 | |
| 55 | #define AX_SROM_DATA_LOW 0x08 |
| 56 | #define AX_SROM_DATA_HIGH 0x09 |
| 57 | |
| 58 | #define AX_RX_CTL 0x0b |
| 59 | #define AX_RX_CTL_DROPCRCERR (1 << 8) |
| 60 | #define AX_RX_CTL_IPE (1 << 9) |
| 61 | #define AX_RX_CTL_START (1 << 7) |
| 62 | #define AX_RX_CTL_AP (1 << 5) |
| 63 | #define AX_RX_CTL_AM (1 << 4) |
| 64 | #define AX_RX_CTL_AB (1 << 3) |
| 65 | #define AX_RX_CTL_AMALL (1 << 1) |
| 66 | #define AX_RX_CTL_PRO (1 << 0) |
| 67 | #define AX_RX_CTL_STOP 0 |
| 68 | |
| 69 | #define AX_NODE_ID 0x10 |
| 70 | #define AX_MULFLTARY 0x16 |
| 71 | |
| 72 | #define AX_MEDIUM_STATUS_MODE 0x22 |
| 73 | #define AX_MEDIUM_GIGAMODE (1 << 0) |
| 74 | #define AX_MEDIUM_FULL_DUPLEX (1 << 1) |
| 75 | #define AX_MEDIUM_EN_125MHZ (1 << 3) |
| 76 | #define AX_MEDIUM_RXFLOW_CTRLEN (1 << 4) |
| 77 | #define AX_MEDIUM_TXFLOW_CTRLEN (1 << 5) |
| 78 | #define AX_MEDIUM_RECEIVE_EN (1 << 8) |
| 79 | #define AX_MEDIUM_PS (1 << 9) |
| 80 | #define AX_MEDIUM_JUMBO_EN 0x8040 |
| 81 | |
| 82 | #define AX_MONITOR_MOD 0x24 |
| 83 | #define AX_MONITOR_MODE_RWLC (1 << 1) |
| 84 | #define AX_MONITOR_MODE_RWMP (1 << 2) |
| 85 | #define AX_MONITOR_MODE_PMEPOL (1 << 5) |
| 86 | #define AX_MONITOR_MODE_PMETYPE (1 << 6) |
| 87 | |
| 88 | #define AX_GPIO_CTRL 0x25 |
| 89 | #define AX_GPIO_CTRL_GPIO3EN (1 << 7) |
| 90 | #define AX_GPIO_CTRL_GPIO2EN (1 << 6) |
| 91 | #define AX_GPIO_CTRL_GPIO1EN (1 << 5) |
| 92 | |
| 93 | #define AX_PHYPWR_RSTCTL 0x26 |
| 94 | #define AX_PHYPWR_RSTCTL_BZ (1 << 4) |
| 95 | #define AX_PHYPWR_RSTCTL_IPRL (1 << 5) |
| 96 | #define AX_PHYPWR_RSTCTL_AT (1 << 12) |
| 97 | |
| 98 | #define AX_RX_BULKIN_QCTRL 0x2e |
| 99 | #define AX_CLK_SELECT 0x33 |
| 100 | #define AX_CLK_SELECT_BCS (1 << 0) |
| 101 | #define AX_CLK_SELECT_ACS (1 << 1) |
| 102 | #define AX_CLK_SELECT_ULR (1 << 3) |
| 103 | |
| 104 | #define AX_RXCOE_CTL 0x34 |
| 105 | #define AX_RXCOE_IP (1 << 0) |
| 106 | #define AX_RXCOE_TCP (1 << 1) |
| 107 | #define AX_RXCOE_UDP (1 << 2) |
| 108 | #define AX_RXCOE_TCPV6 (1 << 5) |
| 109 | #define AX_RXCOE_UDPV6 (1 << 6) |
| 110 | |
| 111 | #define AX_TXCOE_CTL 0x35 |
| 112 | #define AX_TXCOE_IP (1 << 0) |
| 113 | #define AX_TXCOE_TCP (1 << 1) |
| 114 | #define AX_TXCOE_UDP (1 << 2) |
| 115 | #define AX_TXCOE_TCPV6 (1 << 5) |
| 116 | #define AX_TXCOE_UDPV6 (1 << 6) |
| 117 | |
| 118 | #define AX_LEDCTRL 0x73 |
| 119 | |
| 120 | #define GMII_PHY_PHYSR 0x11 |
| 121 | #define GMII_PHY_PHYSR_SMASK 0xc000 |
| 122 | #define GMII_PHY_PHYSR_GIGA (1 << 15) |
| 123 | #define GMII_PHY_PHYSR_100 (1 << 14) |
| 124 | #define GMII_PHY_PHYSR_FULL (1 << 13) |
| 125 | #define GMII_PHY_PHYSR_LINK (1 << 10) |
| 126 | |
| 127 | #define GMII_LED_ACT 0x1a |
| 128 | #define GMII_LED_ACTIVE_MASK 0xff8f |
| 129 | #define GMII_LED0_ACTIVE (1 << 4) |
| 130 | #define GMII_LED1_ACTIVE (1 << 5) |
| 131 | #define GMII_LED2_ACTIVE (1 << 6) |
| 132 | |
| 133 | #define GMII_LED_LINK 0x1c |
| 134 | #define GMII_LED_LINK_MASK 0xf888 |
| 135 | #define GMII_LED0_LINK_10 (1 << 0) |
| 136 | #define GMII_LED0_LINK_100 (1 << 1) |
| 137 | #define GMII_LED0_LINK_1000 (1 << 2) |
| 138 | #define GMII_LED1_LINK_10 (1 << 4) |
| 139 | #define GMII_LED1_LINK_100 (1 << 5) |
| 140 | #define GMII_LED1_LINK_1000 (1 << 6) |
| 141 | #define GMII_LED2_LINK_10 (1 << 8) |
| 142 | #define GMII_LED2_LINK_100 (1 << 9) |
| 143 | #define GMII_LED2_LINK_1000 (1 << 10) |
| 144 | #define LED0_ACTIVE (1 << 0) |
| 145 | #define LED0_LINK_10 (1 << 1) |
| 146 | #define LED0_LINK_100 (1 << 2) |
| 147 | #define LED0_LINK_1000 (1 << 3) |
| 148 | #define LED0_FD (1 << 4) |
| 149 | #define LED0_USB3_MASK 0x001f |
| 150 | #define LED1_ACTIVE (1 << 5) |
| 151 | #define LED1_LINK_10 (1 << 6) |
| 152 | #define LED1_LINK_100 (1 << 7) |
| 153 | #define LED1_LINK_1000 (1 << 8) |
| 154 | #define LED1_FD (1 << 9) |
| 155 | #define LED1_USB3_MASK 0x03e0 |
| 156 | #define LED2_ACTIVE (1 << 10) |
| 157 | #define LED2_LINK_1000 (1 << 13) |
| 158 | #define LED2_LINK_100 (1 << 12) |
| 159 | #define LED2_LINK_10 (1 << 11) |
| 160 | #define LED2_FD (1 << 14) |
| 161 | #define LED_VALID (1 << 15) |
| 162 | #define LED2_USB3_MASK 0x7c00 |
| 163 | |
| 164 | #define GMII_PHYPAGE 0x1e |
| 165 | #define GMII_PHY_PAGE_SELECT 0x1f |
| 166 | #define GMII_PHY_PGSEL_EXT 0x0007 |
| 167 | #define GMII_PHY_PGSEL_PAGE0 0x0000 |
| 168 | |
| 169 | /* local defines */ |
| 170 | #define ASIX_BASE_NAME "axg" |
| 171 | #define USB_CTRL_SET_TIMEOUT 5000 |
| 172 | #define USB_CTRL_GET_TIMEOUT 5000 |
| 173 | #define USB_BULK_SEND_TIMEOUT 5000 |
| 174 | #define USB_BULK_RECV_TIMEOUT 5000 |
| 175 | |
| 176 | #define AX_RX_URB_SIZE 1024 * 0x12 |
| 177 | #define BLK_FRAME_SIZE 0x200 |
| 178 | #define PHY_CONNECT_TIMEOUT 5000 |
| 179 | |
| 180 | #define TIMEOUT_RESOLUTION 50 /* ms */ |
| 181 | |
| 182 | #define FLAG_NONE 0 |
| 183 | #define FLAG_TYPE_AX88179 (1U << 0) |
| 184 | #define FLAG_TYPE_AX88178a (1U << 1) |
| 185 | #define FLAG_TYPE_DLINK_DUB1312 (1U << 2) |
| 186 | #define FLAG_TYPE_SITECOM (1U << 3) |
| 187 | #define FLAG_TYPE_SAMSUNG (1U << 4) |
| 188 | #define FLAG_TYPE_LENOVO (1U << 5) |
Alban Bedel | 10a6e1c | 2016-08-03 08:14:40 +0200 | [diff] [blame] | 189 | #define FLAG_TYPE_GX3 (1U << 6) |
Rene Griessl | 979beee | 2014-11-07 16:53:48 +0100 | [diff] [blame] | 190 | |
| 191 | /* local vars */ |
| 192 | static const struct { |
| 193 | unsigned char ctrl, timer_l, timer_h, size, ifg; |
| 194 | } AX88179_BULKIN_SIZE[] = { |
| 195 | {7, 0x4f, 0, 0x02, 0xff}, |
| 196 | {7, 0x20, 3, 0x03, 0xff}, |
| 197 | {7, 0xae, 7, 0x04, 0xff}, |
| 198 | {7, 0xcc, 0x4c, 0x04, 8}, |
| 199 | }; |
| 200 | |
Rene Griessl | 979beee | 2014-11-07 16:53:48 +0100 | [diff] [blame] | 201 | /* driver private */ |
| 202 | struct asix_private { |
Alban Bedel | 5600d9c | 2016-08-09 11:10:03 +0200 | [diff] [blame] | 203 | struct ueth_data ueth; |
| 204 | unsigned pkt_cnt; |
| 205 | uint8_t *pkt_data; |
| 206 | uint32_t *pkt_hdr; |
Rene Griessl | 979beee | 2014-11-07 16:53:48 +0100 | [diff] [blame] | 207 | int flags; |
| 208 | int rx_urb_size; |
| 209 | int maxpacketsize; |
| 210 | }; |
| 211 | |
| 212 | /* |
| 213 | * Asix infrastructure commands |
| 214 | */ |
| 215 | static int asix_write_cmd(struct ueth_data *dev, u8 cmd, u16 value, u16 index, |
| 216 | u16 size, void *data) |
| 217 | { |
| 218 | int len; |
| 219 | ALLOC_CACHE_ALIGN_BUFFER(unsigned char, buf, size); |
| 220 | |
| 221 | debug("asix_write_cmd() cmd=0x%02x value=0x%04x index=0x%04x size=%d\n", |
| 222 | cmd, value, index, size); |
| 223 | |
| 224 | memcpy(buf, data, size); |
| 225 | |
| 226 | len = usb_control_msg( |
| 227 | dev->pusb_dev, |
| 228 | usb_sndctrlpipe(dev->pusb_dev, 0), |
| 229 | cmd, |
| 230 | USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE, |
| 231 | value, |
| 232 | index, |
| 233 | buf, |
| 234 | size, |
| 235 | USB_CTRL_SET_TIMEOUT); |
| 236 | |
| 237 | return len == size ? 0 : ECOMM; |
| 238 | } |
| 239 | |
| 240 | static int asix_read_cmd(struct ueth_data *dev, u8 cmd, u16 value, u16 index, |
| 241 | u16 size, void *data) |
| 242 | { |
| 243 | int len; |
| 244 | ALLOC_CACHE_ALIGN_BUFFER(unsigned char, buf, size); |
| 245 | |
| 246 | debug("asix_read_cmd() cmd=0x%02x value=0x%04x index=0x%04x size=%d\n", |
| 247 | cmd, value, index, size); |
| 248 | |
| 249 | len = usb_control_msg( |
| 250 | dev->pusb_dev, |
| 251 | usb_rcvctrlpipe(dev->pusb_dev, 0), |
| 252 | cmd, |
| 253 | USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE, |
| 254 | value, |
| 255 | index, |
| 256 | buf, |
| 257 | size, |
| 258 | USB_CTRL_GET_TIMEOUT); |
| 259 | |
| 260 | memcpy(data, buf, size); |
| 261 | |
| 262 | return len == size ? 0 : ECOMM; |
| 263 | } |
| 264 | |
Alban Bedel | c51acfa | 2016-08-09 11:10:02 +0200 | [diff] [blame] | 265 | static int asix_read_mac(struct ueth_data *dev, uint8_t *enetaddr) |
Rene Griessl | 979beee | 2014-11-07 16:53:48 +0100 | [diff] [blame] | 266 | { |
Alban Bedel | c51acfa | 2016-08-09 11:10:02 +0200 | [diff] [blame] | 267 | int ret; |
Rene Griessl | 979beee | 2014-11-07 16:53:48 +0100 | [diff] [blame] | 268 | |
Alban Bedel | c51acfa | 2016-08-09 11:10:02 +0200 | [diff] [blame] | 269 | ret = asix_read_cmd(dev, AX_ACCESS_MAC, AX_NODE_ID, 6, 6, enetaddr); |
| 270 | if (ret < 0) |
| 271 | debug("Failed to read MAC address: %02x\n", ret); |
Rene Griessl | 979beee | 2014-11-07 16:53:48 +0100 | [diff] [blame] | 272 | |
Alban Bedel | c51acfa | 2016-08-09 11:10:02 +0200 | [diff] [blame] | 273 | return ret; |
Rene Griessl | 979beee | 2014-11-07 16:53:48 +0100 | [diff] [blame] | 274 | } |
| 275 | |
Alban Bedel | c51acfa | 2016-08-09 11:10:02 +0200 | [diff] [blame] | 276 | static int asix_write_mac(struct ueth_data *dev, uint8_t *enetaddr) |
Rene Griessl | eecc3ce | 2015-01-12 17:51:16 +0100 | [diff] [blame] | 277 | { |
Rene Griessl | eecc3ce | 2015-01-12 17:51:16 +0100 | [diff] [blame] | 278 | int ret; |
| 279 | |
| 280 | ret = asix_write_cmd(dev, AX_ACCESS_MAC, AX_NODE_ID, ETH_ALEN, |
Alban Bedel | c51acfa | 2016-08-09 11:10:02 +0200 | [diff] [blame] | 281 | ETH_ALEN, enetaddr); |
Rene Griessl | eecc3ce | 2015-01-12 17:51:16 +0100 | [diff] [blame] | 282 | if (ret < 0) |
| 283 | debug("Failed to set MAC address: %02x\n", ret); |
| 284 | |
| 285 | return ret; |
| 286 | } |
| 287 | |
Alban Bedel | c51acfa | 2016-08-09 11:10:02 +0200 | [diff] [blame] | 288 | static int asix_basic_reset(struct ueth_data *dev, |
| 289 | struct asix_private *dev_priv) |
Rene Griessl | 979beee | 2014-11-07 16:53:48 +0100 | [diff] [blame] | 290 | { |
Rene Griessl | 979beee | 2014-11-07 16:53:48 +0100 | [diff] [blame] | 291 | u8 buf[5]; |
| 292 | u16 *tmp16; |
| 293 | u8 *tmp; |
| 294 | |
| 295 | tmp16 = (u16 *)buf; |
| 296 | tmp = (u8 *)buf; |
| 297 | |
| 298 | /* Power up ethernet PHY */ |
| 299 | *tmp16 = 0; |
| 300 | asix_write_cmd(dev, AX_ACCESS_MAC, AX_PHYPWR_RSTCTL, 2, 2, tmp16); |
| 301 | |
| 302 | *tmp16 = AX_PHYPWR_RSTCTL_IPRL; |
| 303 | asix_write_cmd(dev, AX_ACCESS_MAC, AX_PHYPWR_RSTCTL, 2, 2, tmp16); |
| 304 | mdelay(200); |
| 305 | |
| 306 | *tmp = AX_CLK_SELECT_ACS | AX_CLK_SELECT_BCS; |
| 307 | asix_write_cmd(dev, AX_ACCESS_MAC, AX_CLK_SELECT, 1, 1, tmp); |
| 308 | mdelay(200); |
| 309 | |
| 310 | /* RX bulk configuration */ |
| 311 | memcpy(tmp, &AX88179_BULKIN_SIZE[0], 5); |
| 312 | asix_write_cmd(dev, AX_ACCESS_MAC, AX_RX_BULKIN_QCTRL, 5, 5, tmp); |
| 313 | |
| 314 | dev_priv->rx_urb_size = 128 * 20; |
| 315 | |
| 316 | /* Water Level configuration */ |
| 317 | *tmp = 0x34; |
| 318 | asix_write_cmd(dev, AX_ACCESS_MAC, AX_PAUSE_WATERLVL_LOW, 1, 1, tmp); |
| 319 | |
| 320 | *tmp = 0x52; |
| 321 | asix_write_cmd(dev, AX_ACCESS_MAC, AX_PAUSE_WATERLVL_HIGH, 1, 1, tmp); |
| 322 | |
| 323 | /* Enable checksum offload */ |
| 324 | *tmp = AX_RXCOE_IP | AX_RXCOE_TCP | AX_RXCOE_UDP | |
| 325 | AX_RXCOE_TCPV6 | AX_RXCOE_UDPV6; |
| 326 | asix_write_cmd(dev, AX_ACCESS_MAC, AX_RXCOE_CTL, 1, 1, tmp); |
| 327 | |
| 328 | *tmp = AX_TXCOE_IP | AX_TXCOE_TCP | AX_TXCOE_UDP | |
| 329 | AX_TXCOE_TCPV6 | AX_TXCOE_UDPV6; |
| 330 | asix_write_cmd(dev, AX_ACCESS_MAC, AX_TXCOE_CTL, 1, 1, tmp); |
| 331 | |
| 332 | /* Configure RX control register => start operation */ |
| 333 | *tmp16 = AX_RX_CTL_DROPCRCERR | AX_RX_CTL_IPE | AX_RX_CTL_START | |
| 334 | AX_RX_CTL_AP | AX_RX_CTL_AMALL | AX_RX_CTL_AB; |
| 335 | asix_write_cmd(dev, AX_ACCESS_MAC, AX_RX_CTL, 2, 2, tmp16); |
| 336 | |
| 337 | *tmp = AX_MONITOR_MODE_PMETYPE | AX_MONITOR_MODE_PMEPOL | |
| 338 | AX_MONITOR_MODE_RWMP; |
| 339 | asix_write_cmd(dev, AX_ACCESS_MAC, AX_MONITOR_MOD, 1, 1, tmp); |
| 340 | |
| 341 | /* Configure default medium type => giga */ |
| 342 | *tmp16 = AX_MEDIUM_RECEIVE_EN | AX_MEDIUM_TXFLOW_CTRLEN | |
| 343 | AX_MEDIUM_RXFLOW_CTRLEN | AX_MEDIUM_FULL_DUPLEX | |
| 344 | AX_MEDIUM_GIGAMODE | AX_MEDIUM_JUMBO_EN; |
| 345 | asix_write_cmd(dev, AX_ACCESS_MAC, AX_MEDIUM_STATUS_MODE, 2, 2, tmp16); |
| 346 | |
| 347 | u16 adv = 0; |
| 348 | adv = ADVERTISE_ALL | ADVERTISE_CSMA | ADVERTISE_LPACK | |
| 349 | ADVERTISE_NPAGE | ADVERTISE_PAUSE_ASYM | ADVERTISE_PAUSE_CAP; |
| 350 | asix_write_cmd(dev, AX_ACCESS_PHY, 0x03, MII_ADVERTISE, 2, &adv); |
| 351 | |
| 352 | adv = ADVERTISE_1000FULL; |
| 353 | asix_write_cmd(dev, AX_ACCESS_PHY, 0x03, MII_CTRL1000, 2, &adv); |
| 354 | |
| 355 | return 0; |
| 356 | } |
| 357 | |
| 358 | static int asix_wait_link(struct ueth_data *dev) |
| 359 | { |
| 360 | int timeout = 0; |
| 361 | int link_detected; |
| 362 | u8 buf[2]; |
| 363 | u16 *tmp16; |
| 364 | |
| 365 | tmp16 = (u16 *)buf; |
| 366 | |
| 367 | do { |
| 368 | asix_read_cmd(dev, AX_ACCESS_PHY, AX88179_PHY_ID, |
| 369 | MII_BMSR, 2, buf); |
| 370 | link_detected = *tmp16 & BMSR_LSTATUS; |
| 371 | if (!link_detected) { |
| 372 | if (timeout == 0) |
| 373 | printf("Waiting for Ethernet connection... "); |
| 374 | mdelay(TIMEOUT_RESOLUTION); |
| 375 | timeout += TIMEOUT_RESOLUTION; |
| 376 | } |
| 377 | } while (!link_detected && timeout < PHY_CONNECT_TIMEOUT); |
| 378 | |
| 379 | if (link_detected) { |
| 380 | if (timeout > 0) |
| 381 | printf("done.\n"); |
| 382 | return 0; |
| 383 | } else { |
| 384 | printf("unable to connect.\n"); |
| 385 | return -ENETUNREACH; |
| 386 | } |
| 387 | } |
| 388 | |
Alban Bedel | c51acfa | 2016-08-09 11:10:02 +0200 | [diff] [blame] | 389 | static int asix_init_common(struct ueth_data *dev, |
| 390 | struct asix_private *dev_priv) |
Rene Griessl | 979beee | 2014-11-07 16:53:48 +0100 | [diff] [blame] | 391 | { |
Rene Griessl | 979beee | 2014-11-07 16:53:48 +0100 | [diff] [blame] | 392 | u8 buf[2], tmp[5], link_sts; |
| 393 | u16 *tmp16, mode; |
| 394 | |
| 395 | |
| 396 | tmp16 = (u16 *)buf; |
| 397 | |
| 398 | debug("** %s()\n", __func__); |
| 399 | |
| 400 | /* Configure RX control register => start operation */ |
| 401 | *tmp16 = AX_RX_CTL_DROPCRCERR | AX_RX_CTL_IPE | AX_RX_CTL_START | |
| 402 | AX_RX_CTL_AP | AX_RX_CTL_AMALL | AX_RX_CTL_AB; |
| 403 | if (asix_write_cmd(dev, AX_ACCESS_MAC, AX_RX_CTL, 2, 2, tmp16) != 0) |
| 404 | goto out_err; |
| 405 | |
| 406 | if (asix_wait_link(dev) != 0) { |
| 407 | /*reset device and try again*/ |
| 408 | printf("Reset Ethernet Device\n"); |
Alban Bedel | c51acfa | 2016-08-09 11:10:02 +0200 | [diff] [blame] | 409 | asix_basic_reset(dev, dev_priv); |
Rene Griessl | 979beee | 2014-11-07 16:53:48 +0100 | [diff] [blame] | 410 | if (asix_wait_link(dev) != 0) |
| 411 | goto out_err; |
| 412 | } |
| 413 | |
| 414 | /* Configure link */ |
| 415 | mode = AX_MEDIUM_RECEIVE_EN | AX_MEDIUM_TXFLOW_CTRLEN | |
| 416 | AX_MEDIUM_RXFLOW_CTRLEN; |
| 417 | |
| 418 | asix_read_cmd(dev, AX_ACCESS_MAC, PHYSICAL_LINK_STATUS, |
| 419 | 1, 1, &link_sts); |
| 420 | |
| 421 | asix_read_cmd(dev, AX_ACCESS_PHY, AX88179_PHY_ID, |
| 422 | GMII_PHY_PHYSR, 2, tmp16); |
| 423 | |
| 424 | if (!(*tmp16 & GMII_PHY_PHYSR_LINK)) { |
| 425 | return 0; |
| 426 | } else if (GMII_PHY_PHYSR_GIGA == (*tmp16 & GMII_PHY_PHYSR_SMASK)) { |
| 427 | mode |= AX_MEDIUM_GIGAMODE | AX_MEDIUM_EN_125MHZ | |
| 428 | AX_MEDIUM_JUMBO_EN; |
| 429 | |
| 430 | if (link_sts & AX_USB_SS) |
| 431 | memcpy(tmp, &AX88179_BULKIN_SIZE[0], 5); |
| 432 | else if (link_sts & AX_USB_HS) |
| 433 | memcpy(tmp, &AX88179_BULKIN_SIZE[1], 5); |
| 434 | else |
| 435 | memcpy(tmp, &AX88179_BULKIN_SIZE[3], 5); |
| 436 | } else if (GMII_PHY_PHYSR_100 == (*tmp16 & GMII_PHY_PHYSR_SMASK)) { |
| 437 | mode |= AX_MEDIUM_PS; |
| 438 | |
| 439 | if (link_sts & (AX_USB_SS | AX_USB_HS)) |
| 440 | memcpy(tmp, &AX88179_BULKIN_SIZE[2], 5); |
| 441 | else |
| 442 | memcpy(tmp, &AX88179_BULKIN_SIZE[3], 5); |
| 443 | } else { |
| 444 | memcpy(tmp, &AX88179_BULKIN_SIZE[3], 5); |
| 445 | } |
| 446 | |
| 447 | /* RX bulk configuration */ |
| 448 | asix_write_cmd(dev, AX_ACCESS_MAC, AX_RX_BULKIN_QCTRL, 5, 5, tmp); |
| 449 | |
| 450 | dev_priv->rx_urb_size = (1024 * (tmp[3] + 2)); |
| 451 | if (*tmp16 & GMII_PHY_PHYSR_FULL) |
| 452 | mode |= AX_MEDIUM_FULL_DUPLEX; |
| 453 | asix_write_cmd(dev, AX_ACCESS_MAC, AX_MEDIUM_STATUS_MODE, |
| 454 | 2, 2, &mode); |
| 455 | |
| 456 | return 0; |
| 457 | out_err: |
| 458 | return -1; |
| 459 | } |
| 460 | |
Alban Bedel | c51acfa | 2016-08-09 11:10:02 +0200 | [diff] [blame] | 461 | static int asix_send_common(struct ueth_data *dev, |
| 462 | struct asix_private *dev_priv, |
| 463 | void *packet, int length) |
Rene Griessl | 979beee | 2014-11-07 16:53:48 +0100 | [diff] [blame] | 464 | { |
Rene Griessl | 979beee | 2014-11-07 16:53:48 +0100 | [diff] [blame] | 465 | int err; |
| 466 | u32 packet_len, tx_hdr2; |
| 467 | int actual_len, framesize; |
| 468 | ALLOC_CACHE_ALIGN_BUFFER(unsigned char, msg, |
| 469 | PKTSIZE + (2 * sizeof(packet_len))); |
| 470 | |
| 471 | debug("** %s(), len %d\n", __func__, length); |
| 472 | |
| 473 | packet_len = length; |
| 474 | cpu_to_le32s(&packet_len); |
| 475 | |
| 476 | memcpy(msg, &packet_len, sizeof(packet_len)); |
| 477 | framesize = dev_priv->maxpacketsize; |
| 478 | tx_hdr2 = 0; |
| 479 | if (((length + 8) % framesize) == 0) |
| 480 | tx_hdr2 |= 0x80008000; /* Enable padding */ |
| 481 | |
| 482 | cpu_to_le32s(&tx_hdr2); |
| 483 | |
| 484 | memcpy(msg + sizeof(packet_len), &tx_hdr2, sizeof(tx_hdr2)); |
| 485 | |
| 486 | memcpy(msg + sizeof(packet_len) + sizeof(tx_hdr2), |
| 487 | (void *)packet, length); |
| 488 | |
| 489 | err = usb_bulk_msg(dev->pusb_dev, |
| 490 | usb_sndbulkpipe(dev->pusb_dev, dev->ep_out), |
| 491 | (void *)msg, |
| 492 | length + sizeof(packet_len) + sizeof(tx_hdr2), |
| 493 | &actual_len, |
| 494 | USB_BULK_SEND_TIMEOUT); |
Mateusz Kulikowski | d3b2359 | 2016-03-31 23:12:22 +0200 | [diff] [blame] | 495 | debug("Tx: len = %zu, actual = %u, err = %d\n", |
Rene Griessl | 979beee | 2014-11-07 16:53:48 +0100 | [diff] [blame] | 496 | length + sizeof(packet_len), actual_len, err); |
| 497 | |
| 498 | return err; |
| 499 | } |
| 500 | |
Alban Bedel | 5600d9c | 2016-08-09 11:10:03 +0200 | [diff] [blame] | 501 | static int ax88179_eth_start(struct udevice *dev) |
| 502 | { |
| 503 | struct asix_private *priv = dev_get_priv(dev); |
| 504 | |
| 505 | return asix_init_common(&priv->ueth, priv); |
| 506 | } |
| 507 | |
| 508 | void ax88179_eth_stop(struct udevice *dev) |
| 509 | { |
| 510 | struct asix_private *priv = dev_get_priv(dev); |
| 511 | struct ueth_data *ueth = &priv->ueth; |
| 512 | |
| 513 | debug("** %s()\n", __func__); |
| 514 | |
| 515 | usb_ether_advance_rxbuf(ueth, -1); |
| 516 | priv->pkt_cnt = 0; |
| 517 | priv->pkt_data = NULL; |
| 518 | priv->pkt_hdr = NULL; |
| 519 | } |
| 520 | |
| 521 | int ax88179_eth_send(struct udevice *dev, void *packet, int length) |
| 522 | { |
| 523 | struct asix_private *priv = dev_get_priv(dev); |
| 524 | |
| 525 | return asix_send_common(&priv->ueth, priv, packet, length); |
| 526 | } |
| 527 | |
| 528 | int ax88179_eth_recv(struct udevice *dev, int flags, uchar **packetp) |
| 529 | { |
| 530 | struct asix_private *priv = dev_get_priv(dev); |
| 531 | struct ueth_data *ueth = &priv->ueth; |
| 532 | int ret, len; |
| 533 | u16 pkt_len; |
| 534 | |
| 535 | /* No packet left, get a new one */ |
| 536 | if (priv->pkt_cnt == 0) { |
| 537 | uint8_t *ptr; |
| 538 | u16 pkt_cnt; |
| 539 | u16 hdr_off; |
| 540 | u32 rx_hdr; |
| 541 | |
| 542 | len = usb_ether_get_rx_bytes(ueth, &ptr); |
| 543 | debug("%s: first try, len=%d\n", __func__, len); |
| 544 | if (!len) { |
| 545 | if (!(flags & ETH_RECV_CHECK_DEVICE)) |
| 546 | return -EAGAIN; |
| 547 | |
| 548 | ret = usb_ether_receive(ueth, priv->rx_urb_size); |
| 549 | if (ret < 0) |
| 550 | return ret; |
| 551 | |
| 552 | len = usb_ether_get_rx_bytes(ueth, &ptr); |
| 553 | debug("%s: second try, len=%d\n", __func__, len); |
| 554 | } |
| 555 | |
| 556 | if (len < 4) { |
| 557 | usb_ether_advance_rxbuf(ueth, -1); |
| 558 | return -EMSGSIZE; |
| 559 | } |
| 560 | |
| 561 | rx_hdr = *(u32 *)(ptr + len - 4); |
| 562 | le32_to_cpus(&rx_hdr); |
| 563 | |
| 564 | pkt_cnt = (u16)rx_hdr; |
| 565 | if (pkt_cnt == 0) { |
| 566 | usb_ether_advance_rxbuf(ueth, -1); |
| 567 | return 0; |
| 568 | } |
| 569 | |
| 570 | hdr_off = (u16)(rx_hdr >> 16); |
| 571 | if (hdr_off > len - 4) { |
| 572 | usb_ether_advance_rxbuf(ueth, -1); |
| 573 | return -EIO; |
| 574 | } |
| 575 | |
| 576 | priv->pkt_cnt = pkt_cnt; |
| 577 | priv->pkt_data = ptr; |
| 578 | priv->pkt_hdr = (u32 *)(ptr + hdr_off); |
| 579 | debug("%s: %d packets received, pkt header at %d\n", |
| 580 | __func__, (int)priv->pkt_cnt, (int)hdr_off); |
| 581 | } |
| 582 | |
| 583 | le32_to_cpus(priv->pkt_hdr); |
| 584 | pkt_len = (*priv->pkt_hdr >> 16) & 0x1fff; |
| 585 | |
| 586 | *packetp = priv->pkt_data + 2; |
| 587 | |
| 588 | priv->pkt_data += (pkt_len + 7) & 0xFFF8; |
| 589 | priv->pkt_cnt--; |
| 590 | priv->pkt_hdr++; |
| 591 | |
| 592 | debug("%s: return packet of %d bytes (%d packets left)\n", |
| 593 | __func__, (int)pkt_len, priv->pkt_cnt); |
| 594 | return pkt_len; |
| 595 | } |
| 596 | |
| 597 | static int ax88179_free_pkt(struct udevice *dev, uchar *packet, int packet_len) |
| 598 | { |
| 599 | struct asix_private *priv = dev_get_priv(dev); |
| 600 | struct ueth_data *ueth = &priv->ueth; |
| 601 | |
| 602 | if (priv->pkt_cnt == 0) |
| 603 | usb_ether_advance_rxbuf(ueth, -1); |
| 604 | |
| 605 | return 0; |
| 606 | } |
| 607 | |
| 608 | int ax88179_write_hwaddr(struct udevice *dev) |
| 609 | { |
Simon Glass | fa20e93 | 2020-12-03 16:55:20 -0700 | [diff] [blame] | 610 | struct eth_pdata *pdata = dev_get_plat(dev); |
Alban Bedel | 5600d9c | 2016-08-09 11:10:03 +0200 | [diff] [blame] | 611 | struct asix_private *priv = dev_get_priv(dev); |
| 612 | struct ueth_data *ueth = &priv->ueth; |
| 613 | |
| 614 | return asix_write_mac(ueth, pdata->enetaddr); |
| 615 | } |
| 616 | |
| 617 | static int ax88179_eth_probe(struct udevice *dev) |
| 618 | { |
Simon Glass | fa20e93 | 2020-12-03 16:55:20 -0700 | [diff] [blame] | 619 | struct eth_pdata *pdata = dev_get_plat(dev); |
Alban Bedel | 5600d9c | 2016-08-09 11:10:03 +0200 | [diff] [blame] | 620 | struct asix_private *priv = dev_get_priv(dev); |
| 621 | struct usb_device *usb_dev; |
| 622 | int ret; |
| 623 | |
| 624 | priv->flags = dev->driver_data; |
| 625 | ret = usb_ether_register(dev, &priv->ueth, AX_RX_URB_SIZE); |
| 626 | if (ret) |
| 627 | return ret; |
| 628 | |
| 629 | usb_dev = priv->ueth.pusb_dev; |
| 630 | priv->maxpacketsize = usb_dev->epmaxpacketout[AX_ENDPOINT_OUT]; |
| 631 | |
Caleb Connolly | 9508cee | 2024-06-18 16:57:57 +0200 | [diff] [blame] | 632 | ret = asix_basic_reset(&priv->ueth, priv); |
| 633 | if (ret) { |
| 634 | printf("Failed to reset ethernet device\n"); |
| 635 | return ret; |
| 636 | } |
| 637 | |
Alban Bedel | 5600d9c | 2016-08-09 11:10:03 +0200 | [diff] [blame] | 638 | /* Get the MAC address */ |
| 639 | ret = asix_read_mac(&priv->ueth, pdata->enetaddr); |
| 640 | if (ret) |
| 641 | return ret; |
| 642 | debug("MAC %pM\n", pdata->enetaddr); |
| 643 | |
| 644 | return 0; |
| 645 | } |
| 646 | |
| 647 | static const struct eth_ops ax88179_eth_ops = { |
| 648 | .start = ax88179_eth_start, |
| 649 | .send = ax88179_eth_send, |
| 650 | .recv = ax88179_eth_recv, |
| 651 | .free_pkt = ax88179_free_pkt, |
| 652 | .stop = ax88179_eth_stop, |
| 653 | .write_hwaddr = ax88179_write_hwaddr, |
| 654 | }; |
| 655 | |
| 656 | U_BOOT_DRIVER(ax88179_eth) = { |
| 657 | .name = "ax88179_eth", |
| 658 | .id = UCLASS_ETH, |
| 659 | .probe = ax88179_eth_probe, |
| 660 | .ops = &ax88179_eth_ops, |
Simon Glass | 8a2b47f | 2020-12-03 16:55:17 -0700 | [diff] [blame] | 661 | .priv_auto = sizeof(struct asix_private), |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 662 | .plat_auto = sizeof(struct eth_pdata), |
Alban Bedel | 5600d9c | 2016-08-09 11:10:03 +0200 | [diff] [blame] | 663 | }; |
| 664 | |
| 665 | static const struct usb_device_id ax88179_eth_id_table[] = { |
| 666 | { USB_DEVICE(0x0b95, 0x1790), .driver_info = FLAG_TYPE_AX88179 }, |
| 667 | { USB_DEVICE(0x0b95, 0x178a), .driver_info = FLAG_TYPE_AX88178a }, |
| 668 | { USB_DEVICE(0x2001, 0x4a00), .driver_info = FLAG_TYPE_DLINK_DUB1312 }, |
| 669 | { USB_DEVICE(0x0df6, 0x0072), .driver_info = FLAG_TYPE_SITECOM }, |
| 670 | { USB_DEVICE(0x04e8, 0xa100), .driver_info = FLAG_TYPE_SAMSUNG }, |
| 671 | { USB_DEVICE(0x17ef, 0x304b), .driver_info = FLAG_TYPE_LENOVO }, |
| 672 | { USB_DEVICE(0x04b4, 0x3610), .driver_info = FLAG_TYPE_GX3 }, |
| 673 | { } /* Terminating entry */ |
| 674 | }; |
| 675 | |
| 676 | U_BOOT_USB_DEVICE(ax88179_eth, ax88179_eth_id_table); |