ax88172a.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  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, write to the Free Software
  25. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  26. */
  27. #include "asix.h"
  28. #include <linux/phy.h>
  29. struct ax88172a_private {
  30. struct mii_bus *mdio;
  31. struct phy_device *phydev;
  32. char phy_name[20];
  33. u16 phy_addr;
  34. u16 oldmode;
  35. int use_embdphy;
  36. struct asix_rx_fixup_info rx_fixup_info;
  37. };
  38. /* MDIO read and write wrappers for phylib */
  39. static int asix_mdio_bus_read(struct mii_bus *bus, int phy_id, int regnum)
  40. {
  41. return asix_mdio_read(((struct usbnet *)bus->priv)->net, phy_id,
  42. regnum);
  43. }
  44. static int asix_mdio_bus_write(struct mii_bus *bus, int phy_id, int regnum,
  45. u16 val)
  46. {
  47. asix_mdio_write(((struct usbnet *)bus->priv)->net, phy_id, regnum, val);
  48. return 0;
  49. }
  50. static int ax88172a_ioctl(struct net_device *net, struct ifreq *rq, int cmd)
  51. {
  52. if (!netif_running(net))
  53. return -EINVAL;
  54. if (!net->phydev)
  55. return -ENODEV;
  56. return phy_mii_ioctl(net->phydev, rq, cmd);
  57. }
  58. /* set MAC link settings according to information from phylib */
  59. static void ax88172a_adjust_link(struct net_device *netdev)
  60. {
  61. struct phy_device *phydev = netdev->phydev;
  62. struct usbnet *dev = netdev_priv(netdev);
  63. struct ax88172a_private *priv = dev->driver_priv;
  64. u16 mode = 0;
  65. if (phydev->link) {
  66. mode = AX88772_MEDIUM_DEFAULT;
  67. if (phydev->duplex == DUPLEX_HALF)
  68. mode &= ~AX_MEDIUM_FD;
  69. if (phydev->speed != SPEED_100)
  70. mode &= ~AX_MEDIUM_PS;
  71. }
  72. if (mode != priv->oldmode) {
  73. asix_write_medium_mode(dev, mode);
  74. priv->oldmode = mode;
  75. netdev_dbg(netdev, "speed %u duplex %d, setting mode to 0x%04x\n",
  76. phydev->speed, phydev->duplex, mode);
  77. phy_print_status(phydev);
  78. }
  79. }
  80. static void ax88172a_status(struct usbnet *dev, struct urb *urb)
  81. {
  82. /* link changes are detected by polling the phy */
  83. }
  84. /* use phylib infrastructure */
  85. static int ax88172a_init_mdio(struct usbnet *dev)
  86. {
  87. struct ax88172a_private *priv = dev->driver_priv;
  88. int ret, i;
  89. priv->mdio = mdiobus_alloc();
  90. if (!priv->mdio) {
  91. netdev_err(dev->net, "Could not allocate MDIO bus\n");
  92. return -ENOMEM;
  93. }
  94. priv->mdio->priv = (void *)dev;
  95. priv->mdio->read = &asix_mdio_bus_read;
  96. priv->mdio->write = &asix_mdio_bus_write;
  97. priv->mdio->name = "Asix MDIO Bus";
  98. /* mii bus name is usb-<usb bus number>-<usb device number> */
  99. snprintf(priv->mdio->id, MII_BUS_ID_SIZE, "usb-%03d:%03d",
  100. dev->udev->bus->busnum, dev->udev->devnum);
  101. priv->mdio->irq = kzalloc(sizeof(int) * PHY_MAX_ADDR, GFP_KERNEL);
  102. if (!priv->mdio->irq) {
  103. netdev_err(dev->net, "Could not allocate mdio->irq\n");
  104. ret = -ENOMEM;
  105. goto mfree;
  106. }
  107. for (i = 0; i < PHY_MAX_ADDR; i++)
  108. priv->mdio->irq[i] = PHY_POLL;
  109. ret = mdiobus_register(priv->mdio);
  110. if (ret) {
  111. netdev_err(dev->net, "Could not register MDIO bus\n");
  112. goto ifree;
  113. }
  114. netdev_info(dev->net, "registered mdio bus %s\n", priv->mdio->id);
  115. return 0;
  116. ifree:
  117. kfree(priv->mdio->irq);
  118. mfree:
  119. mdiobus_free(priv->mdio);
  120. return ret;
  121. }
  122. static void ax88172a_remove_mdio(struct usbnet *dev)
  123. {
  124. struct ax88172a_private *priv = dev->driver_priv;
  125. netdev_info(dev->net, "deregistering mdio bus %s\n", priv->mdio->id);
  126. mdiobus_unregister(priv->mdio);
  127. kfree(priv->mdio->irq);
  128. mdiobus_free(priv->mdio);
  129. }
  130. static const struct net_device_ops ax88172a_netdev_ops = {
  131. .ndo_open = usbnet_open,
  132. .ndo_stop = usbnet_stop,
  133. .ndo_start_xmit = usbnet_start_xmit,
  134. .ndo_tx_timeout = usbnet_tx_timeout,
  135. .ndo_change_mtu = usbnet_change_mtu,
  136. .ndo_set_mac_address = asix_set_mac_address,
  137. .ndo_validate_addr = eth_validate_addr,
  138. .ndo_do_ioctl = ax88172a_ioctl,
  139. .ndo_set_rx_mode = asix_set_multicast,
  140. };
  141. int ax88172a_get_settings(struct net_device *net, struct ethtool_cmd *cmd)
  142. {
  143. if (!net->phydev)
  144. return -ENODEV;
  145. return phy_ethtool_gset(net->phydev, cmd);
  146. }
  147. int ax88172a_set_settings(struct net_device *net, struct ethtool_cmd *cmd)
  148. {
  149. if (!net->phydev)
  150. return -ENODEV;
  151. return phy_ethtool_sset(net->phydev, cmd);
  152. }
  153. 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. netdev_err(dev->net, "Could not allocate memory for private data\n");
  200. return -ENOMEM;
  201. }
  202. dev->driver_priv = priv;
  203. /* Get the MAC address */
  204. ret = asix_read_cmd(dev, AX_CMD_READ_NODE_ID, 0, 0, ETH_ALEN, buf);
  205. if (ret < 0) {
  206. netdev_err(dev->net, "Failed to read MAC address: %d\n", ret);
  207. goto free;
  208. }
  209. memcpy(dev->net->dev_addr, buf, ETH_ALEN);
  210. dev->net->netdev_ops = &ax88172a_netdev_ops;
  211. dev->net->ethtool_ops = &ax88172a_ethtool_ops;
  212. /* are we using the internal or the external phy? */
  213. ret = asix_read_cmd(dev, AX_CMD_SW_PHY_STATUS, 0, 0, 1, buf);
  214. if (ret < 0) {
  215. netdev_err(dev->net, "Failed to read software interface selection register: %d\n",
  216. ret);
  217. goto free;
  218. }
  219. netdev_dbg(dev->net, "AX_CMD_SW_PHY_STATUS = 0x%02x\n", buf[0]);
  220. switch (buf[0] & AX_PHY_SELECT_MASK) {
  221. case AX_PHY_SELECT_INTERNAL:
  222. netdev_dbg(dev->net, "use internal phy\n");
  223. priv->use_embdphy = 1;
  224. break;
  225. case AX_PHY_SELECT_EXTERNAL:
  226. netdev_dbg(dev->net, "use external phy\n");
  227. priv->use_embdphy = 0;
  228. break;
  229. default:
  230. netdev_err(dev->net, "Interface mode not supported by driver\n");
  231. ret = -ENOTSUPP;
  232. goto free;
  233. }
  234. priv->phy_addr = asix_read_phy_addr(dev, priv->use_embdphy);
  235. ax88172a_reset_phy(dev, priv->use_embdphy);
  236. /* Asix framing packs multiple eth frames into a 2K usb bulk transfer */
  237. if (dev->driver_info->flags & FLAG_FRAMING_AX) {
  238. /* hard_mtu is still the default - the device does not support
  239. jumbo eth frames */
  240. dev->rx_urb_size = 2048;
  241. }
  242. /* init MDIO bus */
  243. ret = ax88172a_init_mdio(dev);
  244. if (ret)
  245. goto free;
  246. return 0;
  247. free:
  248. kfree(priv);
  249. return ret;
  250. }
  251. static int ax88172a_stop(struct usbnet *dev)
  252. {
  253. struct ax88172a_private *priv = dev->driver_priv;
  254. netdev_dbg(dev->net, "Stopping interface\n");
  255. if (priv->phydev) {
  256. netdev_info(dev->net, "Disconnecting from phy %s\n",
  257. priv->phy_name);
  258. phy_stop(priv->phydev);
  259. phy_disconnect(priv->phydev);
  260. }
  261. return 0;
  262. }
  263. static void ax88172a_unbind(struct usbnet *dev, struct usb_interface *intf)
  264. {
  265. struct ax88172a_private *priv = dev->driver_priv;
  266. ax88172a_remove_mdio(dev);
  267. kfree(priv);
  268. }
  269. static int ax88172a_reset(struct usbnet *dev)
  270. {
  271. struct asix_data *data = (struct asix_data *)&dev->data;
  272. struct ax88172a_private *priv = dev->driver_priv;
  273. int ret;
  274. u16 rx_ctl;
  275. ax88172a_reset_phy(dev, priv->use_embdphy);
  276. msleep(150);
  277. rx_ctl = asix_read_rx_ctl(dev);
  278. netdev_dbg(dev->net, "RX_CTL is 0x%04x after software reset\n", rx_ctl);
  279. ret = asix_write_rx_ctl(dev, 0x0000);
  280. if (ret < 0)
  281. goto out;
  282. rx_ctl = asix_read_rx_ctl(dev);
  283. netdev_dbg(dev->net, "RX_CTL is 0x%04x setting to 0x0000\n", rx_ctl);
  284. msleep(150);
  285. ret = asix_write_cmd(dev, AX_CMD_WRITE_IPG0,
  286. AX88772_IPG0_DEFAULT | AX88772_IPG1_DEFAULT,
  287. AX88772_IPG2_DEFAULT, 0, NULL);
  288. if (ret < 0) {
  289. netdev_err(dev->net, "Write IPG,IPG1,IPG2 failed: %d\n", ret);
  290. goto out;
  291. }
  292. /* Rewrite MAC address */
  293. memcpy(data->mac_addr, dev->net->dev_addr, ETH_ALEN);
  294. ret = asix_write_cmd(dev, AX_CMD_WRITE_NODE_ID, 0, 0, ETH_ALEN,
  295. data->mac_addr);
  296. if (ret < 0)
  297. goto out;
  298. /* Set RX_CTL to default values with 2k buffer, and enable cactus */
  299. ret = asix_write_rx_ctl(dev, AX_DEFAULT_RX_CTL);
  300. if (ret < 0)
  301. goto out;
  302. rx_ctl = asix_read_rx_ctl(dev);
  303. netdev_dbg(dev->net, "RX_CTL is 0x%04x after all initializations\n",
  304. rx_ctl);
  305. rx_ctl = asix_read_medium_status(dev);
  306. netdev_dbg(dev->net, "Medium Status is 0x%04x after all initializations\n",
  307. rx_ctl);
  308. /* Connect to PHY */
  309. snprintf(priv->phy_name, 20, PHY_ID_FMT,
  310. priv->mdio->id, priv->phy_addr);
  311. priv->phydev = phy_connect(dev->net, priv->phy_name,
  312. &ax88172a_adjust_link,
  313. PHY_INTERFACE_MODE_MII);
  314. if (IS_ERR(priv->phydev)) {
  315. netdev_err(dev->net, "Could not connect to PHY device %s\n",
  316. priv->phy_name);
  317. ret = PTR_ERR(priv->phydev);
  318. goto out;
  319. }
  320. netdev_info(dev->net, "Connected to phy %s\n", priv->phy_name);
  321. /* During power-up, the AX88172A set the power down (BMCR_PDOWN)
  322. * bit of the PHY. Bring the PHY up again.
  323. */
  324. genphy_resume(priv->phydev);
  325. phy_start(priv->phydev);
  326. return 0;
  327. out:
  328. return ret;
  329. }
  330. static int ax88172a_rx_fixup(struct usbnet *dev, struct sk_buff *skb)
  331. {
  332. struct ax88172a_private *dp = dev->driver_priv;
  333. struct asix_rx_fixup_info *rx = &dp->rx_fixup_info;
  334. return asix_rx_fixup_internal(dev, skb, rx);
  335. }
  336. const struct driver_info ax88172a_info = {
  337. .description = "ASIX AX88172A USB 2.0 Ethernet",
  338. .bind = ax88172a_bind,
  339. .reset = ax88172a_reset,
  340. .stop = ax88172a_stop,
  341. .unbind = ax88172a_unbind,
  342. .status = ax88172a_status,
  343. .flags = FLAG_ETHER | FLAG_FRAMING_AX | FLAG_LINK_INTR |
  344. FLAG_MULTI_PACKET,
  345. .rx_fixup = ax88172a_rx_fixup,
  346. .tx_fixup = asix_tx_fixup,
  347. };