ax88172a.c 10 KB

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