ax88172a.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  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 <[email protected]>
  10. * Copyright (C) 2005 Phil Chang <[email protected]>
  11. * Copyright (C) 2006 James Painter <[email protected]>
  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[20];
  20. u16 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 = asix_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. usbnet_get_endpoints(dev, intf);
  137. priv = kzalloc(sizeof(*priv), GFP_KERNEL);
  138. if (!priv)
  139. return -ENOMEM;
  140. dev->driver_priv = priv;
  141. /* Get the MAC address */
  142. ret = asix_read_cmd(dev, AX_CMD_READ_NODE_ID, 0, 0, ETH_ALEN, buf, 0);
  143. if (ret < ETH_ALEN) {
  144. netdev_err(dev->net, "Failed to read MAC address: %d\n", ret);
  145. ret = -EIO;
  146. goto free;
  147. }
  148. eth_hw_addr_set(dev->net, buf);
  149. dev->net->netdev_ops = &ax88172a_netdev_ops;
  150. dev->net->ethtool_ops = &ax88172a_ethtool_ops;
  151. /* are we using the internal or the external phy? */
  152. ret = asix_read_cmd(dev, AX_CMD_SW_PHY_STATUS, 0, 0, 1, buf, 0);
  153. if (ret < 0) {
  154. netdev_err(dev->net, "Failed to read software interface selection register: %d\n",
  155. ret);
  156. goto free;
  157. }
  158. netdev_dbg(dev->net, "AX_CMD_SW_PHY_STATUS = 0x%02x\n", buf[0]);
  159. switch (buf[0] & AX_PHY_SELECT_MASK) {
  160. case AX_PHY_SELECT_INTERNAL:
  161. netdev_dbg(dev->net, "use internal phy\n");
  162. priv->use_embdphy = 1;
  163. break;
  164. case AX_PHY_SELECT_EXTERNAL:
  165. netdev_dbg(dev->net, "use external phy\n");
  166. priv->use_embdphy = 0;
  167. break;
  168. default:
  169. netdev_err(dev->net, "Interface mode not supported by driver\n");
  170. ret = -ENOTSUPP;
  171. goto free;
  172. }
  173. ret = asix_read_phy_addr(dev, priv->use_embdphy);
  174. if (ret < 0)
  175. goto free;
  176. priv->phy_addr = ret;
  177. ax88172a_reset_phy(dev, priv->use_embdphy);
  178. /* Asix framing packs multiple eth frames into a 2K usb bulk transfer */
  179. if (dev->driver_info->flags & FLAG_FRAMING_AX) {
  180. /* hard_mtu is still the default - the device does not support
  181. jumbo eth frames */
  182. dev->rx_urb_size = 2048;
  183. }
  184. /* init MDIO bus */
  185. ret = ax88172a_init_mdio(dev);
  186. if (ret)
  187. goto free;
  188. return 0;
  189. free:
  190. kfree(priv);
  191. return ret;
  192. }
  193. static int ax88172a_stop(struct usbnet *dev)
  194. {
  195. struct ax88172a_private *priv = dev->driver_priv;
  196. netdev_dbg(dev->net, "Stopping interface\n");
  197. if (priv->phydev) {
  198. netdev_info(dev->net, "Disconnecting from phy %s\n",
  199. priv->phy_name);
  200. phy_stop(priv->phydev);
  201. phy_disconnect(priv->phydev);
  202. }
  203. return 0;
  204. }
  205. static void ax88172a_unbind(struct usbnet *dev, struct usb_interface *intf)
  206. {
  207. struct ax88172a_private *priv = dev->driver_priv;
  208. ax88172a_remove_mdio(dev);
  209. kfree(priv);
  210. }
  211. static int ax88172a_reset(struct usbnet *dev)
  212. {
  213. struct asix_data *data = (struct asix_data *)&dev->data;
  214. struct ax88172a_private *priv = dev->driver_priv;
  215. int ret;
  216. u16 rx_ctl;
  217. ax88172a_reset_phy(dev, priv->use_embdphy);
  218. msleep(150);
  219. rx_ctl = asix_read_rx_ctl(dev, 0);
  220. netdev_dbg(dev->net, "RX_CTL is 0x%04x after software reset\n", rx_ctl);
  221. ret = asix_write_rx_ctl(dev, 0x0000, 0);
  222. if (ret < 0)
  223. goto out;
  224. rx_ctl = asix_read_rx_ctl(dev, 0);
  225. netdev_dbg(dev->net, "RX_CTL is 0x%04x setting to 0x0000\n", rx_ctl);
  226. msleep(150);
  227. ret = asix_write_cmd(dev, AX_CMD_WRITE_IPG0,
  228. AX88772_IPG0_DEFAULT | AX88772_IPG1_DEFAULT,
  229. AX88772_IPG2_DEFAULT, 0, NULL, 0);
  230. if (ret < 0) {
  231. netdev_err(dev->net, "Write IPG,IPG1,IPG2 failed: %d\n", ret);
  232. goto out;
  233. }
  234. /* Rewrite MAC address */
  235. memcpy(data->mac_addr, dev->net->dev_addr, ETH_ALEN);
  236. ret = asix_write_cmd(dev, AX_CMD_WRITE_NODE_ID, 0, 0, ETH_ALEN,
  237. data->mac_addr, 0);
  238. if (ret < 0)
  239. goto out;
  240. /* Set RX_CTL to default values with 2k buffer, and enable cactus */
  241. ret = asix_write_rx_ctl(dev, AX_DEFAULT_RX_CTL, 0);
  242. if (ret < 0)
  243. goto out;
  244. rx_ctl = asix_read_rx_ctl(dev, 0);
  245. netdev_dbg(dev->net, "RX_CTL is 0x%04x after all initializations\n",
  246. rx_ctl);
  247. rx_ctl = asix_read_medium_status(dev, 0);
  248. netdev_dbg(dev->net, "Medium Status is 0x%04x after all initializations\n",
  249. rx_ctl);
  250. /* Connect to PHY */
  251. snprintf(priv->phy_name, 20, PHY_ID_FMT,
  252. priv->mdio->id, priv->phy_addr);
  253. priv->phydev = phy_connect(dev->net, priv->phy_name,
  254. &ax88172a_adjust_link,
  255. PHY_INTERFACE_MODE_MII);
  256. if (IS_ERR(priv->phydev)) {
  257. netdev_err(dev->net, "Could not connect to PHY device %s\n",
  258. priv->phy_name);
  259. ret = PTR_ERR(priv->phydev);
  260. goto out;
  261. }
  262. netdev_info(dev->net, "Connected to phy %s\n", priv->phy_name);
  263. /* During power-up, the AX88172A set the power down (BMCR_PDOWN)
  264. * bit of the PHY. Bring the PHY up again.
  265. */
  266. genphy_resume(priv->phydev);
  267. phy_start(priv->phydev);
  268. return 0;
  269. out:
  270. return ret;
  271. }
  272. static int ax88172a_rx_fixup(struct usbnet *dev, struct sk_buff *skb)
  273. {
  274. struct ax88172a_private *dp = dev->driver_priv;
  275. struct asix_rx_fixup_info *rx = &dp->rx_fixup_info;
  276. return asix_rx_fixup_internal(dev, skb, rx);
  277. }
  278. const struct driver_info ax88172a_info = {
  279. .description = "ASIX AX88172A USB 2.0 Ethernet",
  280. .bind = ax88172a_bind,
  281. .reset = ax88172a_reset,
  282. .stop = ax88172a_stop,
  283. .unbind = ax88172a_unbind,
  284. .status = ax88172a_status,
  285. .flags = FLAG_ETHER | FLAG_FRAMING_AX | FLAG_LINK_INTR |
  286. FLAG_MULTI_PACKET,
  287. .rx_fixup = ax88172a_rx_fixup,
  288. .tx_fixup = asix_tx_fixup,
  289. };