ax88172a.c 9.3 KB

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