ax88172a.c 8.8 KB

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