Simon Glass | 9b70e00 | 2011-02-16 11:14:34 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2011 The Chromium OS Authors. |
| 3 | * See file CREDITS for list of people who contributed to this |
| 4 | * project. |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU General Public License as |
| 8 | * published by the Free Software Foundation; either version 2 of |
| 9 | * the License, or (at your option) any later version. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License |
| 17 | * along with this program; if not, write to the Free Software |
| 18 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, |
| 19 | * MA 02111-1307 USA |
| 20 | */ |
| 21 | |
| 22 | #include <common.h> |
| 23 | #include <usb.h> |
| 24 | #include <linux/mii.h> |
| 25 | #include "usb_ether.h" |
Lucas Stach | 58f8fab | 2012-08-22 11:04:59 +0000 | [diff] [blame] | 26 | #include <malloc.h> |
Simon Glass | 9b70e00 | 2011-02-16 11:14:34 -0800 | [diff] [blame] | 27 | |
| 28 | |
| 29 | /* ASIX AX8817X based USB 2.0 Ethernet Devices */ |
| 30 | |
| 31 | #define AX_CMD_SET_SW_MII 0x06 |
| 32 | #define AX_CMD_READ_MII_REG 0x07 |
| 33 | #define AX_CMD_WRITE_MII_REG 0x08 |
| 34 | #define AX_CMD_SET_HW_MII 0x0a |
Lucas Stach | 02c8d8c | 2012-08-22 11:05:00 +0000 | [diff] [blame] | 35 | #define AX_CMD_READ_EEPROM 0x0b |
Simon Glass | 9b70e00 | 2011-02-16 11:14:34 -0800 | [diff] [blame] | 36 | #define AX_CMD_READ_RX_CTL 0x0f |
| 37 | #define AX_CMD_WRITE_RX_CTL 0x10 |
| 38 | #define AX_CMD_WRITE_IPG0 0x12 |
| 39 | #define AX_CMD_READ_NODE_ID 0x13 |
Lucas Stach | 58f8fab | 2012-08-22 11:04:59 +0000 | [diff] [blame] | 40 | #define AX_CMD_WRITE_NODE_ID 0x14 |
Simon Glass | 9b70e00 | 2011-02-16 11:14:34 -0800 | [diff] [blame] | 41 | #define AX_CMD_READ_PHY_ID 0x19 |
| 42 | #define AX_CMD_WRITE_MEDIUM_MODE 0x1b |
| 43 | #define AX_CMD_WRITE_GPIOS 0x1f |
| 44 | #define AX_CMD_SW_RESET 0x20 |
| 45 | #define AX_CMD_SW_PHY_SELECT 0x22 |
| 46 | |
| 47 | #define AX_SWRESET_CLEAR 0x00 |
| 48 | #define AX_SWRESET_PRTE 0x04 |
| 49 | #define AX_SWRESET_PRL 0x08 |
| 50 | #define AX_SWRESET_IPRL 0x20 |
| 51 | #define AX_SWRESET_IPPD 0x40 |
| 52 | |
| 53 | #define AX88772_IPG0_DEFAULT 0x15 |
| 54 | #define AX88772_IPG1_DEFAULT 0x0c |
| 55 | #define AX88772_IPG2_DEFAULT 0x12 |
| 56 | |
| 57 | /* AX88772 & AX88178 Medium Mode Register */ |
| 58 | #define AX_MEDIUM_PF 0x0080 |
| 59 | #define AX_MEDIUM_JFE 0x0040 |
| 60 | #define AX_MEDIUM_TFC 0x0020 |
| 61 | #define AX_MEDIUM_RFC 0x0010 |
| 62 | #define AX_MEDIUM_ENCK 0x0008 |
| 63 | #define AX_MEDIUM_AC 0x0004 |
| 64 | #define AX_MEDIUM_FD 0x0002 |
| 65 | #define AX_MEDIUM_GM 0x0001 |
| 66 | #define AX_MEDIUM_SM 0x1000 |
| 67 | #define AX_MEDIUM_SBP 0x0800 |
| 68 | #define AX_MEDIUM_PS 0x0200 |
| 69 | #define AX_MEDIUM_RE 0x0100 |
| 70 | |
| 71 | #define AX88178_MEDIUM_DEFAULT \ |
| 72 | (AX_MEDIUM_PS | AX_MEDIUM_FD | AX_MEDIUM_AC | \ |
| 73 | AX_MEDIUM_RFC | AX_MEDIUM_TFC | AX_MEDIUM_JFE | \ |
| 74 | AX_MEDIUM_RE) |
| 75 | |
| 76 | #define AX88772_MEDIUM_DEFAULT \ |
| 77 | (AX_MEDIUM_FD | AX_MEDIUM_RFC | \ |
| 78 | AX_MEDIUM_TFC | AX_MEDIUM_PS | \ |
| 79 | AX_MEDIUM_AC | AX_MEDIUM_RE) |
| 80 | |
| 81 | /* AX88772 & AX88178 RX_CTL values */ |
| 82 | #define AX_RX_CTL_SO 0x0080 |
| 83 | #define AX_RX_CTL_AB 0x0008 |
| 84 | |
| 85 | #define AX_DEFAULT_RX_CTL \ |
| 86 | (AX_RX_CTL_SO | AX_RX_CTL_AB) |
| 87 | |
| 88 | /* GPIO 2 toggles */ |
| 89 | #define AX_GPIO_GPO2EN 0x10 /* GPIO2 Output enable */ |
| 90 | #define AX_GPIO_GPO_2 0x20 /* GPIO2 Output value */ |
| 91 | #define AX_GPIO_RSE 0x80 /* Reload serial EEPROM */ |
| 92 | |
| 93 | /* local defines */ |
| 94 | #define ASIX_BASE_NAME "asx" |
| 95 | #define USB_CTRL_SET_TIMEOUT 5000 |
| 96 | #define USB_CTRL_GET_TIMEOUT 5000 |
| 97 | #define USB_BULK_SEND_TIMEOUT 5000 |
| 98 | #define USB_BULK_RECV_TIMEOUT 5000 |
| 99 | |
| 100 | #define AX_RX_URB_SIZE 2048 |
| 101 | #define PHY_CONNECT_TIMEOUT 5000 |
| 102 | |
Lucas Stach | 58f8fab | 2012-08-22 11:04:59 +0000 | [diff] [blame] | 103 | /* asix_flags defines */ |
| 104 | #define FLAG_NONE 0 |
| 105 | #define FLAG_TYPE_AX88172 (1U << 0) |
| 106 | #define FLAG_TYPE_AX88772 (1U << 1) |
Lucas Stach | 1dff9d0 | 2012-08-22 11:05:01 +0000 | [diff] [blame] | 107 | #define FLAG_TYPE_AX88772B (1U << 2) |
| 108 | #define FLAG_EEPROM_MAC (1U << 3) /* initial mac address in eeprom */ |
Lucas Stach | 58f8fab | 2012-08-22 11:04:59 +0000 | [diff] [blame] | 109 | |
Simon Glass | 9b70e00 | 2011-02-16 11:14:34 -0800 | [diff] [blame] | 110 | /* local vars */ |
| 111 | static int curr_eth_dev; /* index for name of next device detected */ |
| 112 | |
Lucas Stach | 58f8fab | 2012-08-22 11:04:59 +0000 | [diff] [blame] | 113 | /* driver private */ |
| 114 | struct asix_private { |
| 115 | int flags; |
| 116 | }; |
| 117 | |
Simon Glass | 9b70e00 | 2011-02-16 11:14:34 -0800 | [diff] [blame] | 118 | /* |
| 119 | * Asix infrastructure commands |
| 120 | */ |
| 121 | static int asix_write_cmd(struct ueth_data *dev, u8 cmd, u16 value, u16 index, |
| 122 | u16 size, void *data) |
| 123 | { |
| 124 | int len; |
| 125 | |
| 126 | debug("asix_write_cmd() cmd=0x%02x value=0x%04x index=0x%04x " |
| 127 | "size=%d\n", cmd, value, index, size); |
| 128 | |
| 129 | len = usb_control_msg( |
| 130 | dev->pusb_dev, |
| 131 | usb_sndctrlpipe(dev->pusb_dev, 0), |
| 132 | cmd, |
| 133 | USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE, |
| 134 | value, |
| 135 | index, |
| 136 | data, |
| 137 | size, |
| 138 | USB_CTRL_SET_TIMEOUT); |
| 139 | |
| 140 | return len == size ? 0 : -1; |
| 141 | } |
| 142 | |
| 143 | static int asix_read_cmd(struct ueth_data *dev, u8 cmd, u16 value, u16 index, |
| 144 | u16 size, void *data) |
| 145 | { |
| 146 | int len; |
| 147 | |
| 148 | debug("asix_read_cmd() cmd=0x%02x value=0x%04x index=0x%04x size=%d\n", |
| 149 | cmd, value, index, size); |
| 150 | |
| 151 | len = usb_control_msg( |
| 152 | dev->pusb_dev, |
| 153 | usb_rcvctrlpipe(dev->pusb_dev, 0), |
| 154 | cmd, |
| 155 | USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE, |
| 156 | value, |
| 157 | index, |
| 158 | data, |
| 159 | size, |
| 160 | USB_CTRL_GET_TIMEOUT); |
| 161 | return len == size ? 0 : -1; |
| 162 | } |
| 163 | |
| 164 | static inline int asix_set_sw_mii(struct ueth_data *dev) |
| 165 | { |
| 166 | int ret; |
| 167 | |
| 168 | ret = asix_write_cmd(dev, AX_CMD_SET_SW_MII, 0x0000, 0, 0, NULL); |
| 169 | if (ret < 0) |
| 170 | debug("Failed to enable software MII access\n"); |
| 171 | return ret; |
| 172 | } |
| 173 | |
| 174 | static inline int asix_set_hw_mii(struct ueth_data *dev) |
| 175 | { |
| 176 | int ret; |
| 177 | |
| 178 | ret = asix_write_cmd(dev, AX_CMD_SET_HW_MII, 0x0000, 0, 0, NULL); |
| 179 | if (ret < 0) |
| 180 | debug("Failed to enable hardware MII access\n"); |
| 181 | return ret; |
| 182 | } |
| 183 | |
| 184 | static int asix_mdio_read(struct ueth_data *dev, int phy_id, int loc) |
| 185 | { |
Marek Vasut | c59ab09 | 2012-06-24 14:17:56 +0000 | [diff] [blame] | 186 | ALLOC_CACHE_ALIGN_BUFFER(__le16, res, 1); |
Simon Glass | 9b70e00 | 2011-02-16 11:14:34 -0800 | [diff] [blame] | 187 | |
| 188 | asix_set_sw_mii(dev); |
Marek Vasut | c59ab09 | 2012-06-24 14:17:56 +0000 | [diff] [blame] | 189 | asix_read_cmd(dev, AX_CMD_READ_MII_REG, phy_id, (__u16)loc, 2, res); |
Simon Glass | 9b70e00 | 2011-02-16 11:14:34 -0800 | [diff] [blame] | 190 | asix_set_hw_mii(dev); |
| 191 | |
| 192 | debug("asix_mdio_read() phy_id=0x%02x, loc=0x%02x, returns=0x%04x\n", |
Marek Vasut | c59ab09 | 2012-06-24 14:17:56 +0000 | [diff] [blame] | 193 | phy_id, loc, le16_to_cpu(*res)); |
Simon Glass | 9b70e00 | 2011-02-16 11:14:34 -0800 | [diff] [blame] | 194 | |
Marek Vasut | c59ab09 | 2012-06-24 14:17:56 +0000 | [diff] [blame] | 195 | return le16_to_cpu(*res); |
Simon Glass | 9b70e00 | 2011-02-16 11:14:34 -0800 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | static void |
| 199 | asix_mdio_write(struct ueth_data *dev, int phy_id, int loc, int val) |
| 200 | { |
Marek Vasut | c59ab09 | 2012-06-24 14:17:56 +0000 | [diff] [blame] | 201 | ALLOC_CACHE_ALIGN_BUFFER(__le16, res, 1); |
| 202 | *res = cpu_to_le16(val); |
Simon Glass | 9b70e00 | 2011-02-16 11:14:34 -0800 | [diff] [blame] | 203 | |
| 204 | debug("asix_mdio_write() phy_id=0x%02x, loc=0x%02x, val=0x%04x\n", |
| 205 | phy_id, loc, val); |
| 206 | asix_set_sw_mii(dev); |
Marek Vasut | c59ab09 | 2012-06-24 14:17:56 +0000 | [diff] [blame] | 207 | asix_write_cmd(dev, AX_CMD_WRITE_MII_REG, phy_id, (__u16)loc, 2, res); |
Simon Glass | 9b70e00 | 2011-02-16 11:14:34 -0800 | [diff] [blame] | 208 | asix_set_hw_mii(dev); |
| 209 | } |
| 210 | |
| 211 | /* |
| 212 | * Asix "high level" commands |
| 213 | */ |
| 214 | static int asix_sw_reset(struct ueth_data *dev, u8 flags) |
| 215 | { |
| 216 | int ret; |
| 217 | |
| 218 | ret = asix_write_cmd(dev, AX_CMD_SW_RESET, flags, 0, 0, NULL); |
| 219 | if (ret < 0) |
| 220 | debug("Failed to send software reset: %02x\n", ret); |
| 221 | else |
| 222 | udelay(150 * 1000); |
| 223 | |
| 224 | return ret; |
| 225 | } |
| 226 | |
| 227 | static inline int asix_get_phy_addr(struct ueth_data *dev) |
| 228 | { |
Marek Vasut | c59ab09 | 2012-06-24 14:17:56 +0000 | [diff] [blame] | 229 | ALLOC_CACHE_ALIGN_BUFFER(u8, buf, 2); |
| 230 | |
Simon Glass | 9b70e00 | 2011-02-16 11:14:34 -0800 | [diff] [blame] | 231 | int ret = asix_read_cmd(dev, AX_CMD_READ_PHY_ID, 0, 0, 2, buf); |
| 232 | |
| 233 | debug("asix_get_phy_addr()\n"); |
| 234 | |
| 235 | if (ret < 0) { |
| 236 | debug("Error reading PHYID register: %02x\n", ret); |
| 237 | goto out; |
| 238 | } |
Marek Vasut | 0ed1eb6 | 2011-09-23 21:13:35 +0200 | [diff] [blame] | 239 | debug("asix_get_phy_addr() returning 0x%02x%02x\n", buf[0], buf[1]); |
Simon Glass | 9b70e00 | 2011-02-16 11:14:34 -0800 | [diff] [blame] | 240 | ret = buf[1]; |
| 241 | |
| 242 | out: |
| 243 | return ret; |
| 244 | } |
| 245 | |
| 246 | static int asix_write_medium_mode(struct ueth_data *dev, u16 mode) |
| 247 | { |
| 248 | int ret; |
| 249 | |
| 250 | debug("asix_write_medium_mode() - mode = 0x%04x\n", mode); |
| 251 | ret = asix_write_cmd(dev, AX_CMD_WRITE_MEDIUM_MODE, mode, |
| 252 | 0, 0, NULL); |
| 253 | if (ret < 0) { |
| 254 | debug("Failed to write Medium Mode mode to 0x%04x: %02x\n", |
| 255 | mode, ret); |
| 256 | } |
| 257 | return ret; |
| 258 | } |
| 259 | |
| 260 | static u16 asix_read_rx_ctl(struct ueth_data *dev) |
| 261 | { |
Marek Vasut | c59ab09 | 2012-06-24 14:17:56 +0000 | [diff] [blame] | 262 | ALLOC_CACHE_ALIGN_BUFFER(__le16, v, 1); |
| 263 | |
| 264 | int ret = asix_read_cmd(dev, AX_CMD_READ_RX_CTL, 0, 0, 2, v); |
Simon Glass | 9b70e00 | 2011-02-16 11:14:34 -0800 | [diff] [blame] | 265 | |
| 266 | if (ret < 0) |
| 267 | debug("Error reading RX_CTL register: %02x\n", ret); |
| 268 | else |
Marek Vasut | c59ab09 | 2012-06-24 14:17:56 +0000 | [diff] [blame] | 269 | ret = le16_to_cpu(*v); |
Simon Glass | 9b70e00 | 2011-02-16 11:14:34 -0800 | [diff] [blame] | 270 | return ret; |
| 271 | } |
| 272 | |
| 273 | static int asix_write_rx_ctl(struct ueth_data *dev, u16 mode) |
| 274 | { |
| 275 | int ret; |
| 276 | |
| 277 | debug("asix_write_rx_ctl() - mode = 0x%04x\n", mode); |
| 278 | ret = asix_write_cmd(dev, AX_CMD_WRITE_RX_CTL, mode, 0, 0, NULL); |
| 279 | if (ret < 0) { |
| 280 | debug("Failed to write RX_CTL mode to 0x%04x: %02x\n", |
| 281 | mode, ret); |
| 282 | } |
| 283 | return ret; |
| 284 | } |
| 285 | |
| 286 | static int asix_write_gpio(struct ueth_data *dev, u16 value, int sleep) |
| 287 | { |
| 288 | int ret; |
| 289 | |
| 290 | debug("asix_write_gpio() - value = 0x%04x\n", value); |
| 291 | ret = asix_write_cmd(dev, AX_CMD_WRITE_GPIOS, value, 0, 0, NULL); |
| 292 | if (ret < 0) { |
| 293 | debug("Failed to write GPIO value 0x%04x: %02x\n", |
| 294 | value, ret); |
| 295 | } |
| 296 | if (sleep) |
| 297 | udelay(sleep * 1000); |
| 298 | |
| 299 | return ret; |
| 300 | } |
| 301 | |
Lucas Stach | 58f8fab | 2012-08-22 11:04:59 +0000 | [diff] [blame] | 302 | static int asix_write_hwaddr(struct eth_device *eth) |
| 303 | { |
| 304 | struct ueth_data *dev = (struct ueth_data *)eth->priv; |
| 305 | int ret; |
| 306 | ALLOC_CACHE_ALIGN_BUFFER(unsigned char, buf, ETH_ALEN); |
| 307 | |
| 308 | memcpy(buf, eth->enetaddr, ETH_ALEN); |
| 309 | |
| 310 | ret = asix_write_cmd(dev, AX_CMD_WRITE_NODE_ID, 0, 0, ETH_ALEN, buf); |
| 311 | if (ret < 0) |
| 312 | debug("Failed to set MAC address: %02x\n", ret); |
| 313 | |
| 314 | return ret; |
| 315 | } |
| 316 | |
Simon Glass | 9b70e00 | 2011-02-16 11:14:34 -0800 | [diff] [blame] | 317 | /* |
| 318 | * mii commands |
| 319 | */ |
| 320 | |
| 321 | /* |
| 322 | * mii_nway_restart - restart NWay (autonegotiation) for this interface |
| 323 | * |
| 324 | * Returns 0 on success, negative on error. |
| 325 | */ |
| 326 | static int mii_nway_restart(struct ueth_data *dev) |
| 327 | { |
| 328 | int bmcr; |
| 329 | int r = -1; |
| 330 | |
| 331 | /* if autoneg is off, it's an error */ |
| 332 | bmcr = asix_mdio_read(dev, dev->phy_id, MII_BMCR); |
| 333 | |
| 334 | if (bmcr & BMCR_ANENABLE) { |
| 335 | bmcr |= BMCR_ANRESTART; |
| 336 | asix_mdio_write(dev, dev->phy_id, MII_BMCR, bmcr); |
| 337 | r = 0; |
| 338 | } |
| 339 | |
| 340 | return r; |
| 341 | } |
| 342 | |
Lucas Stach | 02c8d8c | 2012-08-22 11:05:00 +0000 | [diff] [blame] | 343 | static int asix_read_mac(struct eth_device *eth) |
| 344 | { |
| 345 | struct ueth_data *dev = (struct ueth_data *)eth->priv; |
| 346 | struct asix_private *priv = (struct asix_private *)dev->dev_priv; |
| 347 | int i; |
| 348 | ALLOC_CACHE_ALIGN_BUFFER(unsigned char, buf, ETH_ALEN); |
| 349 | |
| 350 | if (priv->flags & FLAG_EEPROM_MAC) { |
| 351 | for (i = 0; i < (ETH_ALEN >> 1); i++) { |
| 352 | if (asix_read_cmd(dev, AX_CMD_READ_EEPROM, |
| 353 | 0x04 + i, 0, 2, buf) < 0) { |
| 354 | debug("Failed to read SROM address 04h.\n"); |
| 355 | return -1; |
| 356 | } |
| 357 | memcpy((eth->enetaddr + i * 2), buf, 2); |
| 358 | } |
| 359 | } else { |
| 360 | if (asix_read_cmd(dev, AX_CMD_READ_NODE_ID, 0, 0, ETH_ALEN, buf) |
| 361 | < 0) { |
| 362 | debug("Failed to read MAC address.\n"); |
| 363 | return -1; |
| 364 | } |
| 365 | memcpy(eth->enetaddr, buf, ETH_ALEN); |
| 366 | } |
| 367 | |
| 368 | return 0; |
| 369 | } |
| 370 | |
Lucas Stach | 5fae53d | 2012-08-22 11:04:58 +0000 | [diff] [blame] | 371 | static int asix_basic_reset(struct ueth_data *dev) |
Simon Glass | 9b70e00 | 2011-02-16 11:14:34 -0800 | [diff] [blame] | 372 | { |
| 373 | int embd_phy; |
Simon Glass | 9b70e00 | 2011-02-16 11:14:34 -0800 | [diff] [blame] | 374 | u16 rx_ctl; |
Simon Glass | 9b70e00 | 2011-02-16 11:14:34 -0800 | [diff] [blame] | 375 | |
| 376 | if (asix_write_gpio(dev, |
| 377 | AX_GPIO_RSE | AX_GPIO_GPO_2 | AX_GPIO_GPO2EN, 5) < 0) |
Lucas Stach | 5fae53d | 2012-08-22 11:04:58 +0000 | [diff] [blame] | 378 | return -1; |
Simon Glass | 9b70e00 | 2011-02-16 11:14:34 -0800 | [diff] [blame] | 379 | |
| 380 | /* 0x10 is the phy id of the embedded 10/100 ethernet phy */ |
| 381 | embd_phy = ((asix_get_phy_addr(dev) & 0x1f) == 0x10 ? 1 : 0); |
| 382 | if (asix_write_cmd(dev, AX_CMD_SW_PHY_SELECT, |
| 383 | embd_phy, 0, 0, NULL) < 0) { |
| 384 | debug("Select PHY #1 failed\n"); |
Lucas Stach | 5fae53d | 2012-08-22 11:04:58 +0000 | [diff] [blame] | 385 | return -1; |
Simon Glass | 9b70e00 | 2011-02-16 11:14:34 -0800 | [diff] [blame] | 386 | } |
| 387 | |
| 388 | if (asix_sw_reset(dev, AX_SWRESET_IPPD | AX_SWRESET_PRL) < 0) |
Lucas Stach | 5fae53d | 2012-08-22 11:04:58 +0000 | [diff] [blame] | 389 | return -1; |
Simon Glass | 9b70e00 | 2011-02-16 11:14:34 -0800 | [diff] [blame] | 390 | |
| 391 | if (asix_sw_reset(dev, AX_SWRESET_CLEAR) < 0) |
Lucas Stach | 5fae53d | 2012-08-22 11:04:58 +0000 | [diff] [blame] | 392 | return -1; |
Simon Glass | 9b70e00 | 2011-02-16 11:14:34 -0800 | [diff] [blame] | 393 | |
| 394 | if (embd_phy) { |
| 395 | if (asix_sw_reset(dev, AX_SWRESET_IPRL) < 0) |
Lucas Stach | 5fae53d | 2012-08-22 11:04:58 +0000 | [diff] [blame] | 396 | return -1; |
Simon Glass | 9b70e00 | 2011-02-16 11:14:34 -0800 | [diff] [blame] | 397 | } else { |
| 398 | if (asix_sw_reset(dev, AX_SWRESET_PRTE) < 0) |
Lucas Stach | 5fae53d | 2012-08-22 11:04:58 +0000 | [diff] [blame] | 399 | return -1; |
Simon Glass | 9b70e00 | 2011-02-16 11:14:34 -0800 | [diff] [blame] | 400 | } |
| 401 | |
| 402 | rx_ctl = asix_read_rx_ctl(dev); |
| 403 | debug("RX_CTL is 0x%04x after software reset\n", rx_ctl); |
| 404 | if (asix_write_rx_ctl(dev, 0x0000) < 0) |
Lucas Stach | 5fae53d | 2012-08-22 11:04:58 +0000 | [diff] [blame] | 405 | return -1; |
Simon Glass | 9b70e00 | 2011-02-16 11:14:34 -0800 | [diff] [blame] | 406 | |
| 407 | rx_ctl = asix_read_rx_ctl(dev); |
| 408 | debug("RX_CTL is 0x%04x setting to 0x0000\n", rx_ctl); |
| 409 | |
Lucas Stach | 5fae53d | 2012-08-22 11:04:58 +0000 | [diff] [blame] | 410 | return 0; |
| 411 | } |
| 412 | |
| 413 | /* |
| 414 | * Asix callbacks |
| 415 | */ |
| 416 | static int asix_init(struct eth_device *eth, bd_t *bd) |
| 417 | { |
| 418 | struct ueth_data *dev = (struct ueth_data *)eth->priv; |
| 419 | int timeout = 0; |
| 420 | #define TIMEOUT_RESOLUTION 50 /* ms */ |
| 421 | int link_detected; |
| 422 | |
| 423 | debug("** %s()\n", __func__); |
| 424 | |
Simon Glass | 9b70e00 | 2011-02-16 11:14:34 -0800 | [diff] [blame] | 425 | dev->phy_id = asix_get_phy_addr(dev); |
| 426 | if (dev->phy_id < 0) |
| 427 | debug("Failed to read phy id\n"); |
| 428 | |
| 429 | if (asix_sw_reset(dev, AX_SWRESET_PRL) < 0) |
| 430 | goto out_err; |
| 431 | |
| 432 | if (asix_sw_reset(dev, AX_SWRESET_IPRL | AX_SWRESET_PRL) < 0) |
| 433 | goto out_err; |
| 434 | |
| 435 | asix_mdio_write(dev, dev->phy_id, MII_BMCR, BMCR_RESET); |
| 436 | asix_mdio_write(dev, dev->phy_id, MII_ADVERTISE, |
| 437 | ADVERTISE_ALL | ADVERTISE_CSMA); |
| 438 | mii_nway_restart(dev); |
| 439 | |
| 440 | if (asix_write_medium_mode(dev, AX88772_MEDIUM_DEFAULT) < 0) |
| 441 | goto out_err; |
| 442 | |
| 443 | if (asix_write_cmd(dev, AX_CMD_WRITE_IPG0, |
| 444 | AX88772_IPG0_DEFAULT | AX88772_IPG1_DEFAULT, |
| 445 | AX88772_IPG2_DEFAULT, 0, NULL) < 0) { |
| 446 | debug("Write IPG,IPG1,IPG2 failed\n"); |
| 447 | goto out_err; |
| 448 | } |
| 449 | |
| 450 | if (asix_write_rx_ctl(dev, AX_DEFAULT_RX_CTL) < 0) |
| 451 | goto out_err; |
| 452 | |
| 453 | do { |
| 454 | link_detected = asix_mdio_read(dev, dev->phy_id, MII_BMSR) & |
| 455 | BMSR_LSTATUS; |
| 456 | if (!link_detected) { |
| 457 | if (timeout == 0) |
| 458 | printf("Waiting for Ethernet connection... "); |
| 459 | udelay(TIMEOUT_RESOLUTION * 1000); |
| 460 | timeout += TIMEOUT_RESOLUTION; |
| 461 | } |
| 462 | } while (!link_detected && timeout < PHY_CONNECT_TIMEOUT); |
| 463 | if (link_detected) { |
| 464 | if (timeout != 0) |
| 465 | printf("done.\n"); |
| 466 | } else { |
| 467 | printf("unable to connect.\n"); |
| 468 | goto out_err; |
| 469 | } |
| 470 | |
| 471 | return 0; |
| 472 | out_err: |
| 473 | return -1; |
| 474 | } |
| 475 | |
Anatolij Gustschin | e14826b | 2012-05-20 12:22:59 +0000 | [diff] [blame] | 476 | static int asix_send(struct eth_device *eth, void *packet, int length) |
Simon Glass | 9b70e00 | 2011-02-16 11:14:34 -0800 | [diff] [blame] | 477 | { |
| 478 | struct ueth_data *dev = (struct ueth_data *)eth->priv; |
| 479 | int err; |
| 480 | u32 packet_len; |
| 481 | int actual_len; |
Marek Vasut | c59ab09 | 2012-06-24 14:17:56 +0000 | [diff] [blame] | 482 | ALLOC_CACHE_ALIGN_BUFFER(unsigned char, msg, |
| 483 | PKTSIZE + sizeof(packet_len)); |
Simon Glass | 9b70e00 | 2011-02-16 11:14:34 -0800 | [diff] [blame] | 484 | |
| 485 | debug("** %s(), len %d\n", __func__, length); |
| 486 | |
| 487 | packet_len = (((length) ^ 0x0000ffff) << 16) + (length); |
| 488 | cpu_to_le32s(&packet_len); |
| 489 | |
| 490 | memcpy(msg, &packet_len, sizeof(packet_len)); |
| 491 | memcpy(msg + sizeof(packet_len), (void *)packet, length); |
| 492 | if (length & 1) |
| 493 | length++; |
| 494 | |
| 495 | err = usb_bulk_msg(dev->pusb_dev, |
| 496 | usb_sndbulkpipe(dev->pusb_dev, dev->ep_out), |
| 497 | (void *)msg, |
| 498 | length + sizeof(packet_len), |
| 499 | &actual_len, |
| 500 | USB_BULK_SEND_TIMEOUT); |
| 501 | debug("Tx: len = %u, actual = %u, err = %d\n", |
| 502 | length + sizeof(packet_len), actual_len, err); |
| 503 | |
| 504 | return err; |
| 505 | } |
| 506 | |
| 507 | static int asix_recv(struct eth_device *eth) |
| 508 | { |
| 509 | struct ueth_data *dev = (struct ueth_data *)eth->priv; |
Marek Vasut | c59ab09 | 2012-06-24 14:17:56 +0000 | [diff] [blame] | 510 | ALLOC_CACHE_ALIGN_BUFFER(unsigned char, recv_buf, AX_RX_URB_SIZE); |
Simon Glass | 9b70e00 | 2011-02-16 11:14:34 -0800 | [diff] [blame] | 511 | unsigned char *buf_ptr; |
| 512 | int err; |
| 513 | int actual_len; |
| 514 | u32 packet_len; |
| 515 | |
| 516 | debug("** %s()\n", __func__); |
| 517 | |
| 518 | err = usb_bulk_msg(dev->pusb_dev, |
| 519 | usb_rcvbulkpipe(dev->pusb_dev, dev->ep_in), |
| 520 | (void *)recv_buf, |
| 521 | AX_RX_URB_SIZE, |
| 522 | &actual_len, |
| 523 | USB_BULK_RECV_TIMEOUT); |
| 524 | debug("Rx: len = %u, actual = %u, err = %d\n", AX_RX_URB_SIZE, |
| 525 | actual_len, err); |
| 526 | if (err != 0) { |
| 527 | debug("Rx: failed to receive\n"); |
| 528 | return -1; |
| 529 | } |
| 530 | if (actual_len > AX_RX_URB_SIZE) { |
| 531 | debug("Rx: received too many bytes %d\n", actual_len); |
| 532 | return -1; |
| 533 | } |
| 534 | |
| 535 | buf_ptr = recv_buf; |
| 536 | while (actual_len > 0) { |
| 537 | /* |
| 538 | * 1st 4 bytes contain the length of the actual data as two |
| 539 | * complementary 16-bit words. Extract the length of the data. |
| 540 | */ |
| 541 | if (actual_len < sizeof(packet_len)) { |
| 542 | debug("Rx: incomplete packet length\n"); |
| 543 | return -1; |
| 544 | } |
| 545 | memcpy(&packet_len, buf_ptr, sizeof(packet_len)); |
| 546 | le32_to_cpus(&packet_len); |
Lucas Stach | 1dff9d0 | 2012-08-22 11:05:01 +0000 | [diff] [blame] | 547 | if (((~packet_len >> 16) & 0x7ff) != (packet_len & 0x7ff)) { |
Simon Glass | 9b70e00 | 2011-02-16 11:14:34 -0800 | [diff] [blame] | 548 | debug("Rx: malformed packet length: %#x (%#x:%#x)\n", |
Lucas Stach | 1dff9d0 | 2012-08-22 11:05:01 +0000 | [diff] [blame] | 549 | packet_len, (~packet_len >> 16) & 0x7ff, |
| 550 | packet_len & 0x7ff); |
Simon Glass | 9b70e00 | 2011-02-16 11:14:34 -0800 | [diff] [blame] | 551 | return -1; |
| 552 | } |
Lucas Stach | 1dff9d0 | 2012-08-22 11:05:01 +0000 | [diff] [blame] | 553 | packet_len = packet_len & 0x7ff; |
Simon Glass | 9b70e00 | 2011-02-16 11:14:34 -0800 | [diff] [blame] | 554 | if (packet_len > actual_len - sizeof(packet_len)) { |
| 555 | debug("Rx: too large packet: %d\n", packet_len); |
| 556 | return -1; |
| 557 | } |
| 558 | |
| 559 | /* Notify net stack */ |
| 560 | NetReceive(buf_ptr + sizeof(packet_len), packet_len); |
| 561 | |
| 562 | /* Adjust for next iteration. Packets are padded to 16-bits */ |
| 563 | if (packet_len & 1) |
| 564 | packet_len++; |
| 565 | actual_len -= sizeof(packet_len) + packet_len; |
| 566 | buf_ptr += sizeof(packet_len) + packet_len; |
| 567 | } |
| 568 | |
| 569 | return err; |
| 570 | } |
| 571 | |
| 572 | static void asix_halt(struct eth_device *eth) |
| 573 | { |
| 574 | debug("** %s()\n", __func__); |
| 575 | } |
| 576 | |
| 577 | /* |
| 578 | * Asix probing functions |
| 579 | */ |
| 580 | void asix_eth_before_probe(void) |
| 581 | { |
| 582 | curr_eth_dev = 0; |
| 583 | } |
| 584 | |
| 585 | struct asix_dongle { |
| 586 | unsigned short vendor; |
| 587 | unsigned short product; |
Lucas Stach | 58f8fab | 2012-08-22 11:04:59 +0000 | [diff] [blame] | 588 | int flags; |
Simon Glass | 9b70e00 | 2011-02-16 11:14:34 -0800 | [diff] [blame] | 589 | }; |
| 590 | |
Lucas Stach | 58f8fab | 2012-08-22 11:04:59 +0000 | [diff] [blame] | 591 | static const struct asix_dongle const asix_dongles[] = { |
| 592 | { 0x05ac, 0x1402, FLAG_TYPE_AX88772 }, /* Apple USB Ethernet Adapter */ |
| 593 | { 0x07d1, 0x3c05, FLAG_TYPE_AX88772 }, /* D-Link DUB-E100 H/W Ver B1 */ |
| 594 | /* Cables-to-Go USB Ethernet Adapter */ |
| 595 | { 0x0b95, 0x772a, FLAG_TYPE_AX88772 }, |
| 596 | { 0x0b95, 0x7720, FLAG_TYPE_AX88772 }, /* Trendnet TU2-ET100 V3.0R */ |
| 597 | { 0x0b95, 0x1720, FLAG_TYPE_AX88172 }, /* SMC */ |
| 598 | { 0x0db0, 0xa877, FLAG_TYPE_AX88772 }, /* MSI - ASIX 88772a */ |
| 599 | { 0x13b1, 0x0018, FLAG_TYPE_AX88172 }, /* Linksys 200M v2.1 */ |
| 600 | { 0x1557, 0x7720, FLAG_TYPE_AX88772 }, /* 0Q0 cable ethernet */ |
| 601 | /* DLink DUB-E100 H/W Ver B1 Alternate */ |
| 602 | { 0x2001, 0x3c05, FLAG_TYPE_AX88772 }, |
Lucas Stach | 1dff9d0 | 2012-08-22 11:05:01 +0000 | [diff] [blame] | 603 | /* ASIX 88772B */ |
| 604 | { 0x0b95, 0x772b, FLAG_TYPE_AX88772B | FLAG_EEPROM_MAC }, |
Lucas Stach | 58f8fab | 2012-08-22 11:04:59 +0000 | [diff] [blame] | 605 | { 0x0000, 0x0000, FLAG_NONE } /* END - Do not remove */ |
Simon Glass | 9b70e00 | 2011-02-16 11:14:34 -0800 | [diff] [blame] | 606 | }; |
| 607 | |
| 608 | /* Probe to see if a new device is actually an asix device */ |
| 609 | int asix_eth_probe(struct usb_device *dev, unsigned int ifnum, |
| 610 | struct ueth_data *ss) |
| 611 | { |
| 612 | struct usb_interface *iface; |
| 613 | struct usb_interface_descriptor *iface_desc; |
Lucas Stach | 1dff9d0 | 2012-08-22 11:05:01 +0000 | [diff] [blame] | 614 | int ep_in_found = 0, ep_out_found = 0; |
Simon Glass | 9b70e00 | 2011-02-16 11:14:34 -0800 | [diff] [blame] | 615 | int i; |
| 616 | |
| 617 | /* let's examine the device now */ |
| 618 | iface = &dev->config.if_desc[ifnum]; |
| 619 | iface_desc = &dev->config.if_desc[ifnum].desc; |
| 620 | |
| 621 | for (i = 0; asix_dongles[i].vendor != 0; i++) { |
| 622 | if (dev->descriptor.idVendor == asix_dongles[i].vendor && |
| 623 | dev->descriptor.idProduct == asix_dongles[i].product) |
| 624 | /* Found a supported dongle */ |
| 625 | break; |
| 626 | } |
| 627 | |
| 628 | if (asix_dongles[i].vendor == 0) |
| 629 | return 0; |
| 630 | |
| 631 | memset(ss, 0, sizeof(struct ueth_data)); |
| 632 | |
| 633 | /* At this point, we know we've got a live one */ |
| 634 | debug("\n\nUSB Ethernet device detected: %#04x:%#04x\n", |
| 635 | dev->descriptor.idVendor, dev->descriptor.idProduct); |
| 636 | |
| 637 | /* Initialize the ueth_data structure with some useful info */ |
| 638 | ss->ifnum = ifnum; |
| 639 | ss->pusb_dev = dev; |
| 640 | ss->subclass = iface_desc->bInterfaceSubClass; |
| 641 | ss->protocol = iface_desc->bInterfaceProtocol; |
| 642 | |
Lucas Stach | 58f8fab | 2012-08-22 11:04:59 +0000 | [diff] [blame] | 643 | /* alloc driver private */ |
| 644 | ss->dev_priv = calloc(1, sizeof(struct asix_private)); |
| 645 | if (!ss->dev_priv) |
| 646 | return 0; |
| 647 | |
| 648 | ((struct asix_private *)ss->dev_priv)->flags = asix_dongles[i].flags; |
| 649 | |
Simon Glass | 9b70e00 | 2011-02-16 11:14:34 -0800 | [diff] [blame] | 650 | /* |
| 651 | * We are expecting a minimum of 3 endpoints - in, out (bulk), and |
| 652 | * int. We will ignore any others. |
| 653 | */ |
| 654 | for (i = 0; i < iface_desc->bNumEndpoints; i++) { |
| 655 | /* is it an BULK endpoint? */ |
| 656 | if ((iface->ep_desc[i].bmAttributes & |
| 657 | USB_ENDPOINT_XFERTYPE_MASK) == USB_ENDPOINT_XFER_BULK) { |
Lucas Stach | 1dff9d0 | 2012-08-22 11:05:01 +0000 | [diff] [blame] | 658 | u8 ep_addr = iface->ep_desc[i].bEndpointAddress; |
| 659 | if (ep_addr & USB_DIR_IN) { |
| 660 | if (!ep_in_found) { |
| 661 | ss->ep_in = ep_addr & |
| 662 | USB_ENDPOINT_NUMBER_MASK; |
| 663 | ep_in_found = 1; |
| 664 | } |
| 665 | } else { |
| 666 | if (!ep_out_found) { |
| 667 | ss->ep_out = ep_addr & |
| 668 | USB_ENDPOINT_NUMBER_MASK; |
| 669 | ep_out_found = 1; |
| 670 | } |
| 671 | } |
Simon Glass | 9b70e00 | 2011-02-16 11:14:34 -0800 | [diff] [blame] | 672 | } |
| 673 | |
| 674 | /* is it an interrupt endpoint? */ |
| 675 | if ((iface->ep_desc[i].bmAttributes & |
| 676 | USB_ENDPOINT_XFERTYPE_MASK) == USB_ENDPOINT_XFER_INT) { |
| 677 | ss->ep_int = iface->ep_desc[i].bEndpointAddress & |
| 678 | USB_ENDPOINT_NUMBER_MASK; |
| 679 | ss->irqinterval = iface->ep_desc[i].bInterval; |
| 680 | } |
| 681 | } |
| 682 | debug("Endpoints In %d Out %d Int %d\n", |
| 683 | ss->ep_in, ss->ep_out, ss->ep_int); |
| 684 | |
| 685 | /* Do some basic sanity checks, and bail if we find a problem */ |
| 686 | if (usb_set_interface(dev, iface_desc->bInterfaceNumber, 0) || |
| 687 | !ss->ep_in || !ss->ep_out || !ss->ep_int) { |
| 688 | debug("Problems with device\n"); |
| 689 | return 0; |
| 690 | } |
| 691 | dev->privptr = (void *)ss; |
| 692 | return 1; |
| 693 | } |
| 694 | |
| 695 | int asix_eth_get_info(struct usb_device *dev, struct ueth_data *ss, |
| 696 | struct eth_device *eth) |
| 697 | { |
Lucas Stach | 58f8fab | 2012-08-22 11:04:59 +0000 | [diff] [blame] | 698 | struct asix_private *priv = (struct asix_private *)ss->dev_priv; |
| 699 | |
Simon Glass | 9b70e00 | 2011-02-16 11:14:34 -0800 | [diff] [blame] | 700 | if (!eth) { |
| 701 | debug("%s: missing parameter.\n", __func__); |
| 702 | return 0; |
| 703 | } |
| 704 | sprintf(eth->name, "%s%d", ASIX_BASE_NAME, curr_eth_dev++); |
| 705 | eth->init = asix_init; |
| 706 | eth->send = asix_send; |
| 707 | eth->recv = asix_recv; |
| 708 | eth->halt = asix_halt; |
Lucas Stach | 58f8fab | 2012-08-22 11:04:59 +0000 | [diff] [blame] | 709 | if (!(priv->flags & FLAG_TYPE_AX88172)) |
| 710 | eth->write_hwaddr = asix_write_hwaddr; |
Simon Glass | 9b70e00 | 2011-02-16 11:14:34 -0800 | [diff] [blame] | 711 | eth->priv = ss; |
| 712 | |
Lucas Stach | 5fae53d | 2012-08-22 11:04:58 +0000 | [diff] [blame] | 713 | if (asix_basic_reset(ss)) |
| 714 | return 0; |
| 715 | |
Lucas Stach | 02c8d8c | 2012-08-22 11:05:00 +0000 | [diff] [blame] | 716 | /* Get the MAC address */ |
| 717 | if (asix_read_mac(eth)) |
| 718 | return 0; |
| 719 | debug("MAC %pM\n", eth->enetaddr); |
| 720 | |
Simon Glass | 9b70e00 | 2011-02-16 11:14:34 -0800 | [diff] [blame] | 721 | return 1; |
| 722 | } |