1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * ASIX AX88172A based USB 2.0 Ethernet Devices 4 * Copyright (C) 2012 OMICRON electronics GmbH 5 * 6 * Supports external PHYs via phylib. Based on the driver for the 7 * AX88772. Original copyrights follow: 8 * 9 * Copyright (C) 2003-2006 David Hollis <dhollis@davehollis.com> 10 * Copyright (C) 2005 Phil Chang <pchang23@sbcglobal.net> 11 * Copyright (C) 2006 James Painter <jamie.painter@iname.com> 12 * Copyright (c) 2002-2003 TiVo Inc. 13 */ 14 15 #include "asix.h" 16 #include <linux/phy.h> 17 18 struct ax88172a_private { 19 struct mii_bus *mdio; 20 struct phy_device *phydev; 21 char phy_name[PHY_ID_SIZE]; 22 u8 phy_addr; 23 u16 oldmode; 24 int use_embdphy; 25 struct asix_rx_fixup_info rx_fixup_info; 26 }; 27 28 /* set MAC link settings according to information from phylib */ 29 static void ax88172a_adjust_link(struct net_device *netdev) 30 { 31 struct phy_device *phydev = netdev->phydev; 32 struct usbnet *dev = netdev_priv(netdev); 33 struct ax88172a_private *priv = dev->driver_priv; 34 u16 mode = 0; 35 36 if (phydev->link) { 37 mode = AX88772_MEDIUM_DEFAULT; 38 39 if (phydev->duplex == DUPLEX_HALF) 40 mode &= ~AX_MEDIUM_FD; 41 42 if (phydev->speed != SPEED_100) 43 mode &= ~AX_MEDIUM_PS; 44 } 45 46 if (mode != priv->oldmode) { 47 asix_write_medium_mode(dev, mode, 0); 48 priv->oldmode = mode; 49 netdev_dbg(netdev, "speed %u duplex %d, setting mode to 0x%04x\n", 50 phydev->speed, phydev->duplex, mode); 51 phy_print_status(phydev); 52 } 53 } 54 55 static void ax88172a_status(struct usbnet *dev, struct urb *urb) 56 { 57 /* link changes are detected by polling the phy */ 58 } 59 60 /* use phylib infrastructure */ 61 static int ax88172a_init_mdio(struct usbnet *dev) 62 { 63 struct ax88172a_private *priv = dev->driver_priv; 64 int ret; 65 66 priv->mdio = mdiobus_alloc(); 67 if (!priv->mdio) { 68 netdev_err(dev->net, "Could not allocate MDIO bus\n"); 69 return -ENOMEM; 70 } 71 72 priv->mdio->priv = (void *)dev; 73 priv->mdio->read = &asix_mdio_bus_read; 74 priv->mdio->write = &asix_mdio_bus_write; 75 priv->mdio->name = "Asix MDIO Bus"; 76 /* mii bus name is usb-<usb bus number>-<usb device number> */ 77 snprintf(priv->mdio->id, MII_BUS_ID_SIZE, "usb-%03d:%03d", 78 dev->udev->bus->busnum, dev->udev->devnum); 79 80 ret = mdiobus_register(priv->mdio); 81 if (ret) { 82 netdev_err(dev->net, "Could not register MDIO bus\n"); 83 goto mfree; 84 } 85 86 netdev_info(dev->net, "registered mdio bus %s\n", priv->mdio->id); 87 return 0; 88 89 mfree: 90 mdiobus_free(priv->mdio); 91 return ret; 92 } 93 94 static void ax88172a_remove_mdio(struct usbnet *dev) 95 { 96 struct ax88172a_private *priv = dev->driver_priv; 97 98 netdev_info(dev->net, "deregistering mdio bus %s\n", priv->mdio->id); 99 mdiobus_unregister(priv->mdio); 100 mdiobus_free(priv->mdio); 101 } 102 103 static const struct net_device_ops ax88172a_netdev_ops = { 104 .ndo_open = usbnet_open, 105 .ndo_stop = usbnet_stop, 106 .ndo_start_xmit = usbnet_start_xmit, 107 .ndo_tx_timeout = usbnet_tx_timeout, 108 .ndo_change_mtu = usbnet_change_mtu, 109 .ndo_get_stats64 = dev_get_tstats64, 110 .ndo_set_mac_address = asix_set_mac_address, 111 .ndo_validate_addr = eth_validate_addr, 112 .ndo_eth_ioctl = phy_do_ioctl_running, 113 .ndo_set_rx_mode = asix_set_multicast, 114 }; 115 116 static const struct ethtool_ops ax88172a_ethtool_ops = { 117 .get_drvinfo = usbnet_get_drvinfo, 118 .get_link = usbnet_get_link, 119 .get_msglevel = usbnet_get_msglevel, 120 .set_msglevel = usbnet_set_msglevel, 121 .get_wol = asix_get_wol, 122 .set_wol = asix_set_wol, 123 .get_eeprom_len = asix_get_eeprom_len, 124 .get_eeprom = asix_get_eeprom, 125 .set_eeprom = asix_set_eeprom, 126 .nway_reset = phy_ethtool_nway_reset, 127 .get_link_ksettings = phy_ethtool_get_link_ksettings, 128 .set_link_ksettings = phy_ethtool_set_link_ksettings, 129 }; 130 131 static int ax88172a_reset_phy(struct usbnet *dev, int embd_phy) 132 { 133 int ret; 134 135 ret = asix_sw_reset(dev, AX_SWRESET_IPPD, 0); 136 if (ret < 0) 137 goto err; 138 139 msleep(150); 140 ret = asix_sw_reset(dev, AX_SWRESET_CLEAR, 0); 141 if (ret < 0) 142 goto err; 143 144 msleep(150); 145 146 ret = asix_sw_reset(dev, embd_phy ? AX_SWRESET_IPRL : AX_SWRESET_IPPD, 147 0); 148 if (ret < 0) 149 goto err; 150 151 return 0; 152 153 err: 154 return ret; 155 } 156 157 158 static int ax88172a_bind(struct usbnet *dev, struct usb_interface *intf) 159 { 160 int ret; 161 u8 buf[ETH_ALEN]; 162 struct ax88172a_private *priv; 163 164 ret = usbnet_get_endpoints(dev, intf); 165 if (ret) 166 return ret; 167 168 priv = kzalloc_obj(*priv); 169 if (!priv) 170 return -ENOMEM; 171 172 dev->driver_priv = priv; 173 174 /* Get the MAC address */ 175 ret = asix_read_cmd(dev, AX_CMD_READ_NODE_ID, 0, 0, ETH_ALEN, buf, 0); 176 if (ret < 0) { 177 netdev_err(dev->net, "Failed to read MAC address: %d\n", ret); 178 goto free; 179 } 180 eth_hw_addr_set(dev->net, buf); 181 182 dev->net->netdev_ops = &ax88172a_netdev_ops; 183 dev->net->ethtool_ops = &ax88172a_ethtool_ops; 184 185 /* are we using the internal or the external phy? */ 186 ret = asix_read_cmd(dev, AX_CMD_SW_PHY_STATUS, 0, 0, 1, buf, 0); 187 if (ret < 0) { 188 netdev_err(dev->net, "Failed to read software interface selection register: %d\n", 189 ret); 190 goto free; 191 } 192 193 netdev_dbg(dev->net, "AX_CMD_SW_PHY_STATUS = 0x%02x\n", buf[0]); 194 switch (buf[0] & AX_PHY_SELECT_MASK) { 195 case AX_PHY_SELECT_INTERNAL: 196 netdev_dbg(dev->net, "use internal phy\n"); 197 priv->use_embdphy = 1; 198 break; 199 case AX_PHY_SELECT_EXTERNAL: 200 netdev_dbg(dev->net, "use external phy\n"); 201 priv->use_embdphy = 0; 202 break; 203 default: 204 netdev_err(dev->net, "Interface mode not supported by driver\n"); 205 ret = -ENOTSUPP; 206 goto free; 207 } 208 209 ret = asix_read_phy_addr(dev, priv->use_embdphy); 210 if (ret < 0) 211 goto free; 212 213 priv->phy_addr = ret; 214 215 ax88172a_reset_phy(dev, priv->use_embdphy); 216 217 /* Asix framing packs multiple eth frames into a 2K usb bulk transfer */ 218 if (dev->driver_info->flags & FLAG_FRAMING_AX) { 219 /* hard_mtu is still the default - the device does not support 220 jumbo eth frames */ 221 dev->rx_urb_size = 2048; 222 } 223 224 /* init MDIO bus */ 225 ret = ax88172a_init_mdio(dev); 226 if (ret) 227 goto free; 228 229 return 0; 230 231 free: 232 kfree(priv); 233 return ret; 234 } 235 236 static int ax88172a_stop(struct usbnet *dev) 237 { 238 struct ax88172a_private *priv = dev->driver_priv; 239 240 netdev_dbg(dev->net, "Stopping interface\n"); 241 242 if (priv->phydev) { 243 netdev_info(dev->net, "Disconnecting from phy %s\n", 244 priv->phy_name); 245 phy_stop(priv->phydev); 246 phy_disconnect(priv->phydev); 247 } 248 249 return 0; 250 } 251 252 static void ax88172a_unbind(struct usbnet *dev, struct usb_interface *intf) 253 { 254 struct ax88172a_private *priv = dev->driver_priv; 255 256 ax88172a_remove_mdio(dev); 257 kfree(priv); 258 } 259 260 static int ax88172a_reset(struct usbnet *dev) 261 { 262 struct asix_data *data = (struct asix_data *)&dev->data; 263 struct ax88172a_private *priv = dev->driver_priv; 264 int ret; 265 u16 rx_ctl; 266 267 ax88172a_reset_phy(dev, priv->use_embdphy); 268 269 msleep(150); 270 rx_ctl = asix_read_rx_ctl(dev, 0); 271 netdev_dbg(dev->net, "RX_CTL is 0x%04x after software reset\n", rx_ctl); 272 ret = asix_write_rx_ctl(dev, 0x0000, 0); 273 if (ret < 0) 274 goto out; 275 276 rx_ctl = asix_read_rx_ctl(dev, 0); 277 netdev_dbg(dev->net, "RX_CTL is 0x%04x setting to 0x0000\n", rx_ctl); 278 279 msleep(150); 280 281 ret = asix_write_cmd(dev, AX_CMD_WRITE_IPG0, 282 AX88772_IPG0_DEFAULT | AX88772_IPG1_DEFAULT, 283 AX88772_IPG2_DEFAULT, 0, NULL, 0); 284 if (ret < 0) { 285 netdev_err(dev->net, "Write IPG,IPG1,IPG2 failed: %d\n", ret); 286 goto out; 287 } 288 289 /* Rewrite MAC address */ 290 memcpy(data->mac_addr, dev->net->dev_addr, ETH_ALEN); 291 ret = asix_write_cmd(dev, AX_CMD_WRITE_NODE_ID, 0, 0, ETH_ALEN, 292 data->mac_addr, 0); 293 if (ret < 0) 294 goto out; 295 296 /* Set RX_CTL to default values with 2k buffer, and enable cactus */ 297 ret = asix_write_rx_ctl(dev, AX_DEFAULT_RX_CTL, 0); 298 if (ret < 0) 299 goto out; 300 301 rx_ctl = asix_read_rx_ctl(dev, 0); 302 netdev_dbg(dev->net, "RX_CTL is 0x%04x after all initializations\n", 303 rx_ctl); 304 305 rx_ctl = asix_read_medium_status(dev, 0); 306 netdev_dbg(dev->net, "Medium Status is 0x%04x after all initializations\n", 307 rx_ctl); 308 309 /* Connect to PHY */ 310 snprintf(priv->phy_name, sizeof(priv->phy_name), PHY_ID_FMT, 311 priv->mdio->id, priv->phy_addr); 312 313 priv->phydev = phy_connect(dev->net, priv->phy_name, 314 &ax88172a_adjust_link, 315 PHY_INTERFACE_MODE_MII); 316 if (IS_ERR(priv->phydev)) { 317 netdev_err(dev->net, "Could not connect to PHY device %s\n", 318 priv->phy_name); 319 ret = PTR_ERR(priv->phydev); 320 goto out; 321 } 322 323 netdev_info(dev->net, "Connected to phy %s\n", priv->phy_name); 324 325 /* During power-up, the AX88172A set the power down (BMCR_PDOWN) 326 * bit of the PHY. Bring the PHY up again. 327 */ 328 genphy_resume(priv->phydev); 329 phy_start(priv->phydev); 330 331 return 0; 332 333 out: 334 return ret; 335 336 } 337 338 static int ax88172a_rx_fixup(struct usbnet *dev, struct sk_buff *skb) 339 { 340 struct ax88172a_private *dp = dev->driver_priv; 341 struct asix_rx_fixup_info *rx = &dp->rx_fixup_info; 342 343 return asix_rx_fixup_internal(dev, skb, rx); 344 } 345 346 const struct driver_info ax88172a_info = { 347 .description = "ASIX AX88172A USB 2.0 Ethernet", 348 .bind = ax88172a_bind, 349 .reset = ax88172a_reset, 350 .stop = ax88172a_stop, 351 .unbind = ax88172a_unbind, 352 .status = ax88172a_status, 353 .flags = FLAG_ETHER | FLAG_FRAMING_AX | FLAG_LINK_INTR | 354 FLAG_MULTI_PACKET, 355 .rx_fixup = ax88172a_rx_fixup, 356 .tx_fixup = asix_tx_fixup, 357 }; 358