asix_devices.c 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * ASIX AX8817X based USB 2.0 Ethernet Devices
  4. * Copyright (C) 2003-2006 David Hollis <dhollis@davehollis.com>
  5. * Copyright (C) 2005 Phil Chang <pchang23@sbcglobal.net>
  6. * Copyright (C) 2006 James Painter <jamie.painter@iname.com>
  7. * Copyright (c) 2002-2003 TiVo Inc.
  8. */
  9. #include "asix.h"
  10. #define PHY_MODE_MARVELL 0x0000
  11. #define MII_MARVELL_LED_CTRL 0x0018
  12. #define MII_MARVELL_STATUS 0x001b
  13. #define MII_MARVELL_CTRL 0x0014
  14. #define MARVELL_LED_MANUAL 0x0019
  15. #define MARVELL_STATUS_HWCFG 0x0004
  16. #define MARVELL_CTRL_TXDELAY 0x0002
  17. #define MARVELL_CTRL_RXDELAY 0x0080
  18. #define PHY_MODE_RTL8211CL 0x000C
  19. #define AX88772A_PHY14H 0x14
  20. #define AX88772A_PHY14H_DEFAULT 0x442C
  21. #define AX88772A_PHY15H 0x15
  22. #define AX88772A_PHY15H_DEFAULT 0x03C8
  23. #define AX88772A_PHY16H 0x16
  24. #define AX88772A_PHY16H_DEFAULT 0x4044
  25. struct ax88172_int_data {
  26. __le16 res1;
  27. u8 link;
  28. __le16 res2;
  29. u8 status;
  30. __le16 res3;
  31. } __packed;
  32. static void asix_status(struct usbnet *dev, struct urb *urb)
  33. {
  34. struct ax88172_int_data *event;
  35. int link;
  36. if (urb->actual_length < 8)
  37. return;
  38. event = urb->transfer_buffer;
  39. link = event->link & 0x01;
  40. if (netif_carrier_ok(dev->net) != link) {
  41. usbnet_link_change(dev, link, 1);
  42. netdev_dbg(dev->net, "Link Status is: %d\n", link);
  43. }
  44. }
  45. static void asix_set_netdev_dev_addr(struct usbnet *dev, u8 *addr)
  46. {
  47. if (is_valid_ether_addr(addr)) {
  48. eth_hw_addr_set(dev->net, addr);
  49. } else {
  50. netdev_info(dev->net, "invalid hw address, using random\n");
  51. eth_hw_addr_random(dev->net);
  52. }
  53. }
  54. /* Get the PHY Identifier from the PHYSID1 & PHYSID2 MII registers */
  55. static u32 asix_get_phyid(struct usbnet *dev)
  56. {
  57. int phy_reg;
  58. u32 phy_id;
  59. int i;
  60. /* Poll for the rare case the FW or phy isn't ready yet. */
  61. for (i = 0; i < 100; i++) {
  62. phy_reg = asix_mdio_read(dev->net, dev->mii.phy_id, MII_PHYSID1);
  63. if (phy_reg < 0)
  64. return 0;
  65. if (phy_reg != 0 && phy_reg != 0xFFFF)
  66. break;
  67. mdelay(1);
  68. }
  69. if (phy_reg <= 0 || phy_reg == 0xFFFF)
  70. return 0;
  71. phy_id = (phy_reg & 0xffff) << 16;
  72. phy_reg = asix_mdio_read(dev->net, dev->mii.phy_id, MII_PHYSID2);
  73. if (phy_reg < 0)
  74. return 0;
  75. phy_id |= (phy_reg & 0xffff);
  76. return phy_id;
  77. }
  78. static u32 asix_get_link(struct net_device *net)
  79. {
  80. struct usbnet *dev = netdev_priv(net);
  81. return mii_link_ok(&dev->mii);
  82. }
  83. static int asix_ioctl (struct net_device *net, struct ifreq *rq, int cmd)
  84. {
  85. struct usbnet *dev = netdev_priv(net);
  86. return generic_mii_ioctl(&dev->mii, if_mii(rq), cmd, NULL);
  87. }
  88. /* We need to override some ethtool_ops so we require our
  89. own structure so we don't interfere with other usbnet
  90. devices that may be connected at the same time. */
  91. static const struct ethtool_ops ax88172_ethtool_ops = {
  92. .get_drvinfo = asix_get_drvinfo,
  93. .get_link = asix_get_link,
  94. .get_msglevel = usbnet_get_msglevel,
  95. .set_msglevel = usbnet_set_msglevel,
  96. .get_wol = asix_get_wol,
  97. .set_wol = asix_set_wol,
  98. .get_eeprom_len = asix_get_eeprom_len,
  99. .get_eeprom = asix_get_eeprom,
  100. .set_eeprom = asix_set_eeprom,
  101. .nway_reset = usbnet_nway_reset,
  102. .get_link_ksettings = usbnet_get_link_ksettings_mii,
  103. .set_link_ksettings = usbnet_set_link_ksettings_mii,
  104. };
  105. static void ax88172_set_multicast(struct net_device *net)
  106. {
  107. struct usbnet *dev = netdev_priv(net);
  108. struct asix_data *data = (struct asix_data *)&dev->data;
  109. u8 rx_ctl = 0x8c;
  110. if (net->flags & IFF_PROMISC) {
  111. rx_ctl |= 0x01;
  112. } else if (net->flags & IFF_ALLMULTI ||
  113. netdev_mc_count(net) > AX_MAX_MCAST) {
  114. rx_ctl |= 0x02;
  115. } else if (netdev_mc_empty(net)) {
  116. /* just broadcast and directed */
  117. } else {
  118. /* We use the 20 byte dev->data
  119. * for our 8 byte filter buffer
  120. * to avoid allocating memory that
  121. * is tricky to free later */
  122. struct netdev_hw_addr *ha;
  123. u32 crc_bits;
  124. memset(data->multi_filter, 0, AX_MCAST_FILTER_SIZE);
  125. /* Build the multicast hash filter. */
  126. netdev_for_each_mc_addr(ha, net) {
  127. crc_bits = ether_crc(ETH_ALEN, ha->addr) >> 26;
  128. data->multi_filter[crc_bits >> 3] |=
  129. 1 << (crc_bits & 7);
  130. }
  131. asix_write_cmd_async(dev, AX_CMD_WRITE_MULTI_FILTER, 0, 0,
  132. AX_MCAST_FILTER_SIZE, data->multi_filter);
  133. rx_ctl |= 0x10;
  134. }
  135. asix_write_cmd_async(dev, AX_CMD_WRITE_RX_CTL, rx_ctl, 0, 0, NULL);
  136. }
  137. static int ax88172_link_reset(struct usbnet *dev)
  138. {
  139. u8 mode;
  140. struct ethtool_cmd ecmd = { .cmd = ETHTOOL_GSET };
  141. mii_check_media(&dev->mii, 1, 1);
  142. mii_ethtool_gset(&dev->mii, &ecmd);
  143. mode = AX88172_MEDIUM_DEFAULT;
  144. if (ecmd.duplex != DUPLEX_FULL)
  145. mode |= ~AX88172_MEDIUM_FD;
  146. netdev_dbg(dev->net, "ax88172_link_reset() speed: %u duplex: %d setting mode to 0x%04x\n",
  147. ethtool_cmd_speed(&ecmd), ecmd.duplex, mode);
  148. asix_write_medium_mode(dev, mode, 0);
  149. return 0;
  150. }
  151. static const struct net_device_ops ax88172_netdev_ops = {
  152. .ndo_open = usbnet_open,
  153. .ndo_stop = usbnet_stop,
  154. .ndo_start_xmit = usbnet_start_xmit,
  155. .ndo_tx_timeout = usbnet_tx_timeout,
  156. .ndo_change_mtu = usbnet_change_mtu,
  157. .ndo_get_stats64 = dev_get_tstats64,
  158. .ndo_set_mac_address = eth_mac_addr,
  159. .ndo_validate_addr = eth_validate_addr,
  160. .ndo_eth_ioctl = asix_ioctl,
  161. .ndo_set_rx_mode = ax88172_set_multicast,
  162. };
  163. static void asix_phy_reset(struct usbnet *dev, unsigned int reset_bits)
  164. {
  165. unsigned int timeout = 5000;
  166. asix_mdio_write(dev->net, dev->mii.phy_id, MII_BMCR, reset_bits);
  167. /* give phy_id a chance to process reset */
  168. udelay(500);
  169. /* See IEEE 802.3 "22.2.4.1.1 Reset": 500ms max */
  170. while (timeout--) {
  171. if (asix_mdio_read(dev->net, dev->mii.phy_id, MII_BMCR)
  172. & BMCR_RESET)
  173. udelay(100);
  174. else
  175. return;
  176. }
  177. netdev_err(dev->net, "BMCR_RESET timeout on phy_id %d\n",
  178. dev->mii.phy_id);
  179. }
  180. static int ax88172_bind(struct usbnet *dev, struct usb_interface *intf)
  181. {
  182. int ret = 0;
  183. u8 buf[ETH_ALEN] = {0};
  184. int i;
  185. unsigned long gpio_bits = dev->driver_info->data;
  186. usbnet_get_endpoints(dev,intf);
  187. /* Toggle the GPIOs in a manufacturer/model specific way */
  188. for (i = 2; i >= 0; i--) {
  189. ret = asix_write_cmd(dev, AX_CMD_WRITE_GPIOS,
  190. (gpio_bits >> (i * 8)) & 0xff, 0, 0, NULL, 0);
  191. if (ret < 0)
  192. goto out;
  193. msleep(5);
  194. }
  195. ret = asix_write_rx_ctl(dev, 0x80, 0);
  196. if (ret < 0)
  197. goto out;
  198. /* Get the MAC address */
  199. ret = asix_read_cmd(dev, AX88172_CMD_READ_NODE_ID,
  200. 0, 0, ETH_ALEN, buf, 0);
  201. if (ret < 0) {
  202. netdev_dbg(dev->net, "read AX_CMD_READ_NODE_ID failed: %d\n",
  203. ret);
  204. goto out;
  205. }
  206. asix_set_netdev_dev_addr(dev, buf);
  207. /* Initialize MII structure */
  208. dev->mii.dev = dev->net;
  209. dev->mii.mdio_read = asix_mdio_read;
  210. dev->mii.mdio_write = asix_mdio_write;
  211. dev->mii.phy_id_mask = 0x3f;
  212. dev->mii.reg_num_mask = 0x1f;
  213. dev->mii.phy_id = asix_read_phy_addr(dev, true);
  214. if (dev->mii.phy_id < 0)
  215. return dev->mii.phy_id;
  216. dev->net->netdev_ops = &ax88172_netdev_ops;
  217. dev->net->ethtool_ops = &ax88172_ethtool_ops;
  218. dev->net->needed_headroom = 4; /* cf asix_tx_fixup() */
  219. dev->net->needed_tailroom = 4; /* cf asix_tx_fixup() */
  220. asix_phy_reset(dev, BMCR_RESET);
  221. asix_mdio_write(dev->net, dev->mii.phy_id, MII_ADVERTISE,
  222. ADVERTISE_ALL | ADVERTISE_CSMA | ADVERTISE_PAUSE_CAP);
  223. mii_nway_restart(&dev->mii);
  224. return 0;
  225. out:
  226. return ret;
  227. }
  228. static void ax88772_ethtool_get_strings(struct net_device *netdev, u32 sset,
  229. u8 *data)
  230. {
  231. switch (sset) {
  232. case ETH_SS_TEST:
  233. net_selftest_get_strings(data);
  234. break;
  235. }
  236. }
  237. static int ax88772_ethtool_get_sset_count(struct net_device *ndev, int sset)
  238. {
  239. switch (sset) {
  240. case ETH_SS_TEST:
  241. return net_selftest_get_count();
  242. default:
  243. return -EOPNOTSUPP;
  244. }
  245. }
  246. static void ax88772_ethtool_get_pauseparam(struct net_device *ndev,
  247. struct ethtool_pauseparam *pause)
  248. {
  249. struct usbnet *dev = netdev_priv(ndev);
  250. struct asix_common_private *priv = dev->driver_priv;
  251. phylink_ethtool_get_pauseparam(priv->phylink, pause);
  252. }
  253. static int ax88772_ethtool_set_pauseparam(struct net_device *ndev,
  254. struct ethtool_pauseparam *pause)
  255. {
  256. struct usbnet *dev = netdev_priv(ndev);
  257. struct asix_common_private *priv = dev->driver_priv;
  258. return phylink_ethtool_set_pauseparam(priv->phylink, pause);
  259. }
  260. static const struct ethtool_ops ax88772_ethtool_ops = {
  261. .get_drvinfo = asix_get_drvinfo,
  262. .get_link = usbnet_get_link,
  263. .get_msglevel = usbnet_get_msglevel,
  264. .set_msglevel = usbnet_set_msglevel,
  265. .get_wol = asix_get_wol,
  266. .set_wol = asix_set_wol,
  267. .get_eeprom_len = asix_get_eeprom_len,
  268. .get_eeprom = asix_get_eeprom,
  269. .set_eeprom = asix_set_eeprom,
  270. .nway_reset = phy_ethtool_nway_reset,
  271. .get_link_ksettings = phy_ethtool_get_link_ksettings,
  272. .set_link_ksettings = phy_ethtool_set_link_ksettings,
  273. .self_test = net_selftest,
  274. .get_strings = ax88772_ethtool_get_strings,
  275. .get_sset_count = ax88772_ethtool_get_sset_count,
  276. .get_pauseparam = ax88772_ethtool_get_pauseparam,
  277. .set_pauseparam = ax88772_ethtool_set_pauseparam,
  278. };
  279. static int ax88772_reset(struct usbnet *dev)
  280. {
  281. struct asix_data *data = (struct asix_data *)&dev->data;
  282. struct asix_common_private *priv = dev->driver_priv;
  283. int ret;
  284. /* Rewrite MAC address */
  285. ether_addr_copy(data->mac_addr, dev->net->dev_addr);
  286. ret = asix_write_cmd(dev, AX_CMD_WRITE_NODE_ID, 0, 0,
  287. ETH_ALEN, data->mac_addr, 0);
  288. if (ret < 0)
  289. goto out;
  290. /* Set RX_CTL to default values with 2k buffer, and enable cactus */
  291. ret = asix_write_rx_ctl(dev, AX_DEFAULT_RX_CTL, 0);
  292. if (ret < 0)
  293. goto out;
  294. ret = asix_write_medium_mode(dev, AX88772_MEDIUM_DEFAULT, 0);
  295. if (ret < 0)
  296. goto out;
  297. phylink_start(priv->phylink);
  298. return 0;
  299. out:
  300. return ret;
  301. }
  302. static int ax88772_hw_reset(struct usbnet *dev, int in_pm)
  303. {
  304. struct asix_data *data = (struct asix_data *)&dev->data;
  305. struct asix_common_private *priv = dev->driver_priv;
  306. u16 rx_ctl;
  307. int ret;
  308. ret = asix_write_gpio(dev, AX_GPIO_RSE | AX_GPIO_GPO_2 |
  309. AX_GPIO_GPO2EN, 5, in_pm);
  310. if (ret < 0)
  311. goto out;
  312. ret = asix_write_cmd(dev, AX_CMD_SW_PHY_SELECT, priv->embd_phy,
  313. 0, 0, NULL, in_pm);
  314. if (ret < 0) {
  315. netdev_dbg(dev->net, "Select PHY #1 failed: %d\n", ret);
  316. goto out;
  317. }
  318. if (priv->embd_phy) {
  319. ret = asix_sw_reset(dev, AX_SWRESET_IPPD, in_pm);
  320. if (ret < 0)
  321. goto out;
  322. usleep_range(10000, 11000);
  323. ret = asix_sw_reset(dev, AX_SWRESET_CLEAR, in_pm);
  324. if (ret < 0)
  325. goto out;
  326. msleep(60);
  327. ret = asix_sw_reset(dev, AX_SWRESET_IPRL | AX_SWRESET_PRL,
  328. in_pm);
  329. if (ret < 0)
  330. goto out;
  331. } else {
  332. ret = asix_sw_reset(dev, AX_SWRESET_IPPD | AX_SWRESET_PRL,
  333. in_pm);
  334. if (ret < 0)
  335. goto out;
  336. }
  337. msleep(150);
  338. if (in_pm && (!asix_mdio_read_nopm(dev->net, dev->mii.phy_id,
  339. MII_PHYSID1))){
  340. ret = -EIO;
  341. goto out;
  342. }
  343. ret = asix_write_rx_ctl(dev, AX_DEFAULT_RX_CTL, in_pm);
  344. if (ret < 0)
  345. goto out;
  346. ret = asix_write_medium_mode(dev, AX88772_MEDIUM_DEFAULT, in_pm);
  347. if (ret < 0)
  348. goto out;
  349. ret = asix_write_cmd(dev, AX_CMD_WRITE_IPG0,
  350. AX88772_IPG0_DEFAULT | AX88772_IPG1_DEFAULT,
  351. AX88772_IPG2_DEFAULT, 0, NULL, in_pm);
  352. if (ret < 0) {
  353. netdev_dbg(dev->net, "Write IPG,IPG1,IPG2 failed: %d\n", ret);
  354. goto out;
  355. }
  356. /* Rewrite MAC address */
  357. ether_addr_copy(data->mac_addr, dev->net->dev_addr);
  358. ret = asix_write_cmd(dev, AX_CMD_WRITE_NODE_ID, 0, 0,
  359. ETH_ALEN, data->mac_addr, in_pm);
  360. if (ret < 0)
  361. goto out;
  362. /* Set RX_CTL to default values with 2k buffer, and enable cactus */
  363. ret = asix_write_rx_ctl(dev, AX_DEFAULT_RX_CTL, in_pm);
  364. if (ret < 0)
  365. goto out;
  366. rx_ctl = asix_read_rx_ctl(dev, in_pm);
  367. netdev_dbg(dev->net, "RX_CTL is 0x%04x after all initializations\n",
  368. rx_ctl);
  369. rx_ctl = asix_read_medium_status(dev, in_pm);
  370. netdev_dbg(dev->net,
  371. "Medium Status is 0x%04x after all initializations\n",
  372. rx_ctl);
  373. return 0;
  374. out:
  375. return ret;
  376. }
  377. static int ax88772a_hw_reset(struct usbnet *dev, int in_pm)
  378. {
  379. struct asix_data *data = (struct asix_data *)&dev->data;
  380. struct asix_common_private *priv = dev->driver_priv;
  381. u16 rx_ctl, phy14h, phy15h, phy16h;
  382. int ret;
  383. ret = asix_write_gpio(dev, AX_GPIO_RSE, 5, in_pm);
  384. if (ret < 0)
  385. goto out;
  386. ret = asix_write_cmd(dev, AX_CMD_SW_PHY_SELECT, priv->embd_phy |
  387. AX_PHYSEL_SSEN, 0, 0, NULL, in_pm);
  388. if (ret < 0) {
  389. netdev_dbg(dev->net, "Select PHY #1 failed: %d\n", ret);
  390. goto out;
  391. }
  392. usleep_range(10000, 11000);
  393. ret = asix_sw_reset(dev, AX_SWRESET_IPPD | AX_SWRESET_IPRL, in_pm);
  394. if (ret < 0)
  395. goto out;
  396. usleep_range(10000, 11000);
  397. ret = asix_sw_reset(dev, AX_SWRESET_IPRL, in_pm);
  398. if (ret < 0)
  399. goto out;
  400. msleep(160);
  401. ret = asix_sw_reset(dev, AX_SWRESET_CLEAR, in_pm);
  402. if (ret < 0)
  403. goto out;
  404. ret = asix_sw_reset(dev, AX_SWRESET_IPRL, in_pm);
  405. if (ret < 0)
  406. goto out;
  407. msleep(200);
  408. if (in_pm && (!asix_mdio_read_nopm(dev->net, dev->mii.phy_id,
  409. MII_PHYSID1))) {
  410. ret = -1;
  411. goto out;
  412. }
  413. if (priv->chipcode == AX_AX88772B_CHIPCODE) {
  414. ret = asix_write_cmd(dev, AX_QCTCTRL, 0x8000, 0x8001,
  415. 0, NULL, in_pm);
  416. if (ret < 0) {
  417. netdev_dbg(dev->net, "Write BQ setting failed: %d\n",
  418. ret);
  419. goto out;
  420. }
  421. } else if (priv->chipcode == AX_AX88772A_CHIPCODE) {
  422. /* Check if the PHY registers have default settings */
  423. phy14h = asix_mdio_read_nopm(dev->net, dev->mii.phy_id,
  424. AX88772A_PHY14H);
  425. phy15h = asix_mdio_read_nopm(dev->net, dev->mii.phy_id,
  426. AX88772A_PHY15H);
  427. phy16h = asix_mdio_read_nopm(dev->net, dev->mii.phy_id,
  428. AX88772A_PHY16H);
  429. netdev_dbg(dev->net,
  430. "772a_hw_reset: MR20=0x%x MR21=0x%x MR22=0x%x\n",
  431. phy14h, phy15h, phy16h);
  432. /* Restore PHY registers default setting if not */
  433. if (phy14h != AX88772A_PHY14H_DEFAULT)
  434. asix_mdio_write_nopm(dev->net, dev->mii.phy_id,
  435. AX88772A_PHY14H,
  436. AX88772A_PHY14H_DEFAULT);
  437. if (phy15h != AX88772A_PHY15H_DEFAULT)
  438. asix_mdio_write_nopm(dev->net, dev->mii.phy_id,
  439. AX88772A_PHY15H,
  440. AX88772A_PHY15H_DEFAULT);
  441. if (phy16h != AX88772A_PHY16H_DEFAULT)
  442. asix_mdio_write_nopm(dev->net, dev->mii.phy_id,
  443. AX88772A_PHY16H,
  444. AX88772A_PHY16H_DEFAULT);
  445. }
  446. ret = asix_write_cmd(dev, AX_CMD_WRITE_IPG0,
  447. AX88772_IPG0_DEFAULT | AX88772_IPG1_DEFAULT,
  448. AX88772_IPG2_DEFAULT, 0, NULL, in_pm);
  449. if (ret < 0) {
  450. netdev_dbg(dev->net, "Write IPG,IPG1,IPG2 failed: %d\n", ret);
  451. goto out;
  452. }
  453. /* Rewrite MAC address */
  454. memcpy(data->mac_addr, dev->net->dev_addr, ETH_ALEN);
  455. ret = asix_write_cmd(dev, AX_CMD_WRITE_NODE_ID, 0, 0, ETH_ALEN,
  456. data->mac_addr, in_pm);
  457. if (ret < 0)
  458. goto out;
  459. /* Set RX_CTL to default values with 2k buffer, and enable cactus */
  460. ret = asix_write_rx_ctl(dev, AX_DEFAULT_RX_CTL, in_pm);
  461. if (ret < 0)
  462. goto out;
  463. ret = asix_write_medium_mode(dev, AX88772_MEDIUM_DEFAULT, in_pm);
  464. if (ret < 0)
  465. return ret;
  466. /* Set RX_CTL to default values with 2k buffer, and enable cactus */
  467. ret = asix_write_rx_ctl(dev, AX_DEFAULT_RX_CTL, in_pm);
  468. if (ret < 0)
  469. goto out;
  470. rx_ctl = asix_read_rx_ctl(dev, in_pm);
  471. netdev_dbg(dev->net, "RX_CTL is 0x%04x after all initializations\n",
  472. rx_ctl);
  473. rx_ctl = asix_read_medium_status(dev, in_pm);
  474. netdev_dbg(dev->net,
  475. "Medium Status is 0x%04x after all initializations\n",
  476. rx_ctl);
  477. return 0;
  478. out:
  479. return ret;
  480. }
  481. static const struct net_device_ops ax88772_netdev_ops = {
  482. .ndo_open = usbnet_open,
  483. .ndo_stop = usbnet_stop,
  484. .ndo_start_xmit = usbnet_start_xmit,
  485. .ndo_tx_timeout = usbnet_tx_timeout,
  486. .ndo_change_mtu = usbnet_change_mtu,
  487. .ndo_get_stats64 = dev_get_tstats64,
  488. .ndo_set_mac_address = asix_set_mac_address,
  489. .ndo_validate_addr = eth_validate_addr,
  490. .ndo_eth_ioctl = phy_do_ioctl_running,
  491. .ndo_set_rx_mode = asix_set_multicast,
  492. };
  493. static void ax88772_suspend(struct usbnet *dev)
  494. {
  495. struct asix_common_private *priv = dev->driver_priv;
  496. u16 medium;
  497. if (netif_running(dev->net)) {
  498. rtnl_lock();
  499. phylink_suspend(priv->phylink, false);
  500. rtnl_unlock();
  501. }
  502. /* Stop MAC operation */
  503. medium = asix_read_medium_status(dev, 1);
  504. medium &= ~AX_MEDIUM_RE;
  505. asix_write_medium_mode(dev, medium, 1);
  506. netdev_dbg(dev->net, "ax88772_suspend: medium=0x%04x\n",
  507. asix_read_medium_status(dev, 1));
  508. }
  509. /* Notes on PM callbacks and locking context:
  510. *
  511. * - asix_suspend()/asix_resume() are invoked for both runtime PM and
  512. * system-wide suspend/resume. For struct usb_driver the ->resume()
  513. * callback does not receive pm_message_t, so the resume type cannot
  514. * be distinguished here.
  515. *
  516. * - The MAC driver must hold RTNL when calling phylink interfaces such as
  517. * phylink_suspend()/resume(). Those calls will also perform MDIO I/O.
  518. *
  519. * - Taking RTNL and doing MDIO from a runtime-PM resume callback (while
  520. * the USB PM lock is held) is fragile. Since autosuspend brings no
  521. * measurable power saving here, we block it by holding a PM usage
  522. * reference in ax88772_bind().
  523. */
  524. static int asix_suspend(struct usb_interface *intf, pm_message_t message)
  525. {
  526. struct usbnet *dev = usb_get_intfdata(intf);
  527. struct asix_common_private *priv = dev->driver_priv;
  528. if (priv && priv->suspend)
  529. priv->suspend(dev);
  530. return usbnet_suspend(intf, message);
  531. }
  532. static void ax88772_resume(struct usbnet *dev)
  533. {
  534. struct asix_common_private *priv = dev->driver_priv;
  535. int i;
  536. for (i = 0; i < 3; i++)
  537. if (!priv->reset(dev, 1))
  538. break;
  539. if (netif_running(dev->net)) {
  540. rtnl_lock();
  541. phylink_resume(priv->phylink);
  542. rtnl_unlock();
  543. }
  544. }
  545. static int asix_resume(struct usb_interface *intf)
  546. {
  547. struct usbnet *dev = usb_get_intfdata(intf);
  548. struct asix_common_private *priv = dev->driver_priv;
  549. if (priv && priv->resume)
  550. priv->resume(dev);
  551. return usbnet_resume(intf);
  552. }
  553. static int ax88772_init_mdio(struct usbnet *dev)
  554. {
  555. struct asix_common_private *priv = dev->driver_priv;
  556. int ret;
  557. priv->mdio = mdiobus_alloc();
  558. if (!priv->mdio)
  559. return -ENOMEM;
  560. priv->mdio->priv = dev;
  561. priv->mdio->read = &asix_mdio_bus_read;
  562. priv->mdio->write = &asix_mdio_bus_write;
  563. priv->mdio->name = "Asix MDIO Bus";
  564. priv->mdio->phy_mask = ~(BIT(priv->phy_addr & 0x1f) | BIT(AX_EMBD_PHY_ADDR));
  565. /* mii bus name is usb-<usb bus number>-<usb device number> */
  566. snprintf(priv->mdio->id, MII_BUS_ID_SIZE, "usb-%03d:%03d",
  567. dev->udev->bus->busnum, dev->udev->devnum);
  568. ret = mdiobus_register(priv->mdio);
  569. if (ret) {
  570. netdev_err(dev->net, "Could not register MDIO bus (err %d)\n", ret);
  571. mdiobus_free(priv->mdio);
  572. priv->mdio = NULL;
  573. }
  574. return ret;
  575. }
  576. static void ax88772_mdio_unregister(struct asix_common_private *priv)
  577. {
  578. mdiobus_unregister(priv->mdio);
  579. mdiobus_free(priv->mdio);
  580. }
  581. static int ax88772_init_phy(struct usbnet *dev)
  582. {
  583. struct asix_common_private *priv = dev->driver_priv;
  584. int ret;
  585. priv->phydev = mdiobus_get_phy(priv->mdio, priv->phy_addr);
  586. if (!priv->phydev) {
  587. netdev_err(dev->net, "Could not find PHY\n");
  588. return -ENODEV;
  589. }
  590. ret = phylink_connect_phy(priv->phylink, priv->phydev);
  591. if (ret) {
  592. netdev_err(dev->net, "Could not connect PHY\n");
  593. return ret;
  594. }
  595. phy_suspend(priv->phydev);
  596. priv->phydev->mac_managed_pm = true;
  597. phy_attached_info(priv->phydev);
  598. if (priv->embd_phy)
  599. return 0;
  600. /* In case main PHY is not the embedded PHY and MAC is RMII clock
  601. * provider, we need to suspend embedded PHY by keeping PLL enabled
  602. * (AX_SWRESET_IPPD == 0).
  603. */
  604. priv->phydev_int = mdiobus_get_phy(priv->mdio, AX_EMBD_PHY_ADDR);
  605. if (!priv->phydev_int) {
  606. rtnl_lock();
  607. phylink_disconnect_phy(priv->phylink);
  608. rtnl_unlock();
  609. netdev_err(dev->net, "Could not find internal PHY\n");
  610. return -ENODEV;
  611. }
  612. priv->phydev_int->mac_managed_pm = true;
  613. phy_suspend(priv->phydev_int);
  614. return 0;
  615. }
  616. static void ax88772_mac_config(struct phylink_config *config, unsigned int mode,
  617. const struct phylink_link_state *state)
  618. {
  619. /* Nothing to do */
  620. }
  621. static void ax88772_mac_link_down(struct phylink_config *config,
  622. unsigned int mode, phy_interface_t interface)
  623. {
  624. struct usbnet *dev = netdev_priv(to_net_dev(config->dev));
  625. asix_write_medium_mode(dev, 0, 0);
  626. }
  627. static void ax88772_mac_link_up(struct phylink_config *config,
  628. struct phy_device *phy,
  629. unsigned int mode, phy_interface_t interface,
  630. int speed, int duplex,
  631. bool tx_pause, bool rx_pause)
  632. {
  633. struct usbnet *dev = netdev_priv(to_net_dev(config->dev));
  634. u16 m = AX_MEDIUM_AC | AX_MEDIUM_RE;
  635. m |= duplex ? AX_MEDIUM_FD : 0;
  636. switch (speed) {
  637. case SPEED_100:
  638. m |= AX_MEDIUM_PS;
  639. break;
  640. case SPEED_10:
  641. break;
  642. default:
  643. return;
  644. }
  645. if (tx_pause)
  646. m |= AX_MEDIUM_TFC;
  647. if (rx_pause)
  648. m |= AX_MEDIUM_RFC;
  649. asix_write_medium_mode(dev, m, 0);
  650. }
  651. static const struct phylink_mac_ops ax88772_phylink_mac_ops = {
  652. .mac_config = ax88772_mac_config,
  653. .mac_link_down = ax88772_mac_link_down,
  654. .mac_link_up = ax88772_mac_link_up,
  655. };
  656. static int ax88772_phylink_setup(struct usbnet *dev)
  657. {
  658. struct asix_common_private *priv = dev->driver_priv;
  659. phy_interface_t phy_if_mode;
  660. struct phylink *phylink;
  661. priv->phylink_config.dev = &dev->net->dev;
  662. priv->phylink_config.type = PHYLINK_NETDEV;
  663. priv->phylink_config.mac_capabilities = MAC_SYM_PAUSE | MAC_ASYM_PAUSE |
  664. MAC_10 | MAC_100;
  665. __set_bit(PHY_INTERFACE_MODE_INTERNAL,
  666. priv->phylink_config.supported_interfaces);
  667. __set_bit(PHY_INTERFACE_MODE_RMII,
  668. priv->phylink_config.supported_interfaces);
  669. if (priv->embd_phy)
  670. phy_if_mode = PHY_INTERFACE_MODE_INTERNAL;
  671. else
  672. phy_if_mode = PHY_INTERFACE_MODE_RMII;
  673. phylink = phylink_create(&priv->phylink_config, dev->net->dev.fwnode,
  674. phy_if_mode, &ax88772_phylink_mac_ops);
  675. if (IS_ERR(phylink))
  676. return PTR_ERR(phylink);
  677. priv->phylink = phylink;
  678. return 0;
  679. }
  680. static int ax88772_bind(struct usbnet *dev, struct usb_interface *intf)
  681. {
  682. struct asix_common_private *priv;
  683. u8 buf[ETH_ALEN] = {0};
  684. int ret, i;
  685. priv = devm_kzalloc(&dev->udev->dev, sizeof(*priv), GFP_KERNEL);
  686. if (!priv)
  687. return -ENOMEM;
  688. dev->driver_priv = priv;
  689. usbnet_get_endpoints(dev, intf);
  690. /* Maybe the boot loader passed the MAC address via device tree */
  691. if (!eth_platform_get_mac_address(&dev->udev->dev, buf)) {
  692. netif_dbg(dev, ifup, dev->net,
  693. "MAC address read from device tree");
  694. } else {
  695. /* Try getting the MAC address from EEPROM */
  696. if (dev->driver_info->data & FLAG_EEPROM_MAC) {
  697. for (i = 0; i < (ETH_ALEN >> 1); i++) {
  698. ret = asix_read_cmd(dev, AX_CMD_READ_EEPROM,
  699. 0x04 + i, 0, 2, buf + i * 2,
  700. 0);
  701. if (ret < 0)
  702. break;
  703. }
  704. } else {
  705. ret = asix_read_cmd(dev, AX_CMD_READ_NODE_ID,
  706. 0, 0, ETH_ALEN, buf, 0);
  707. }
  708. if (ret < 0) {
  709. netdev_dbg(dev->net, "Failed to read MAC address: %d\n",
  710. ret);
  711. return ret;
  712. }
  713. }
  714. asix_set_netdev_dev_addr(dev, buf);
  715. dev->net->netdev_ops = &ax88772_netdev_ops;
  716. dev->net->ethtool_ops = &ax88772_ethtool_ops;
  717. dev->net->needed_headroom = 4; /* cf asix_tx_fixup() */
  718. dev->net->needed_tailroom = 4; /* cf asix_tx_fixup() */
  719. ret = asix_read_phy_addr(dev, true);
  720. if (ret < 0)
  721. return ret;
  722. priv->phy_addr = ret;
  723. priv->embd_phy = ((priv->phy_addr & 0x1f) == AX_EMBD_PHY_ADDR);
  724. ret = asix_read_cmd(dev, AX_CMD_STATMNGSTS_REG, 0, 0, 1,
  725. &priv->chipcode, 0);
  726. if (ret < 0) {
  727. netdev_dbg(dev->net, "Failed to read STATMNGSTS_REG: %d\n", ret);
  728. return ret;
  729. }
  730. priv->chipcode &= AX_CHIPCODE_MASK;
  731. priv->resume = ax88772_resume;
  732. priv->suspend = ax88772_suspend;
  733. if (priv->chipcode == AX_AX88772_CHIPCODE)
  734. priv->reset = ax88772_hw_reset;
  735. else
  736. priv->reset = ax88772a_hw_reset;
  737. ret = priv->reset(dev, 0);
  738. if (ret < 0) {
  739. netdev_dbg(dev->net, "Failed to reset AX88772: %d\n", ret);
  740. return ret;
  741. }
  742. /* Asix framing packs multiple eth frames into a 2K usb bulk transfer */
  743. if (dev->driver_info->flags & FLAG_FRAMING_AX) {
  744. /* hard_mtu is still the default - the device does not support
  745. jumbo eth frames */
  746. dev->rx_urb_size = 2048;
  747. }
  748. priv->presvd_phy_bmcr = 0;
  749. priv->presvd_phy_advertise = 0;
  750. ret = ax88772_init_mdio(dev);
  751. if (ret)
  752. goto mdio_err;
  753. ret = ax88772_phylink_setup(dev);
  754. if (ret)
  755. goto phylink_err;
  756. ret = ax88772_init_phy(dev);
  757. if (ret)
  758. goto initphy_err;
  759. /* Keep this interface runtime-PM active by taking a usage ref.
  760. * Prevents runtime suspend while bound and avoids resume paths
  761. * that could deadlock (autoresume under RTNL while USB PM lock
  762. * is held, phylink/MDIO wants RTNL).
  763. */
  764. pm_runtime_get_noresume(&intf->dev);
  765. return 0;
  766. initphy_err:
  767. phylink_destroy(priv->phylink);
  768. phylink_err:
  769. ax88772_mdio_unregister(priv);
  770. mdio_err:
  771. return ret;
  772. }
  773. static int ax88772_stop(struct usbnet *dev)
  774. {
  775. struct asix_common_private *priv = dev->driver_priv;
  776. phylink_stop(priv->phylink);
  777. return 0;
  778. }
  779. static void ax88772_unbind(struct usbnet *dev, struct usb_interface *intf)
  780. {
  781. struct asix_common_private *priv = dev->driver_priv;
  782. rtnl_lock();
  783. phylink_disconnect_phy(priv->phylink);
  784. rtnl_unlock();
  785. phylink_destroy(priv->phylink);
  786. ax88772_mdio_unregister(priv);
  787. asix_rx_fixup_common_free(dev->driver_priv);
  788. /* Drop the PM usage ref taken in bind() */
  789. pm_runtime_put(&intf->dev);
  790. }
  791. static void ax88178_unbind(struct usbnet *dev, struct usb_interface *intf)
  792. {
  793. asix_rx_fixup_common_free(dev->driver_priv);
  794. kfree(dev->driver_priv);
  795. }
  796. static const struct ethtool_ops ax88178_ethtool_ops = {
  797. .get_drvinfo = asix_get_drvinfo,
  798. .get_link = asix_get_link,
  799. .get_msglevel = usbnet_get_msglevel,
  800. .set_msglevel = usbnet_set_msglevel,
  801. .get_wol = asix_get_wol,
  802. .set_wol = asix_set_wol,
  803. .get_eeprom_len = asix_get_eeprom_len,
  804. .get_eeprom = asix_get_eeprom,
  805. .set_eeprom = asix_set_eeprom,
  806. .nway_reset = usbnet_nway_reset,
  807. .get_link_ksettings = usbnet_get_link_ksettings_mii,
  808. .set_link_ksettings = usbnet_set_link_ksettings_mii,
  809. };
  810. static int marvell_phy_init(struct usbnet *dev)
  811. {
  812. struct asix_data *data = (struct asix_data *)&dev->data;
  813. u16 reg;
  814. netdev_dbg(dev->net, "marvell_phy_init()\n");
  815. reg = asix_mdio_read(dev->net, dev->mii.phy_id, MII_MARVELL_STATUS);
  816. netdev_dbg(dev->net, "MII_MARVELL_STATUS = 0x%04x\n", reg);
  817. asix_mdio_write(dev->net, dev->mii.phy_id, MII_MARVELL_CTRL,
  818. MARVELL_CTRL_RXDELAY | MARVELL_CTRL_TXDELAY);
  819. if (data->ledmode) {
  820. reg = asix_mdio_read(dev->net, dev->mii.phy_id,
  821. MII_MARVELL_LED_CTRL);
  822. netdev_dbg(dev->net, "MII_MARVELL_LED_CTRL (1) = 0x%04x\n", reg);
  823. reg &= 0xf8ff;
  824. reg |= (1 + 0x0100);
  825. asix_mdio_write(dev->net, dev->mii.phy_id,
  826. MII_MARVELL_LED_CTRL, reg);
  827. reg = asix_mdio_read(dev->net, dev->mii.phy_id,
  828. MII_MARVELL_LED_CTRL);
  829. netdev_dbg(dev->net, "MII_MARVELL_LED_CTRL (2) = 0x%04x\n", reg);
  830. }
  831. return 0;
  832. }
  833. static int rtl8211cl_phy_init(struct usbnet *dev)
  834. {
  835. struct asix_data *data = (struct asix_data *)&dev->data;
  836. netdev_dbg(dev->net, "rtl8211cl_phy_init()\n");
  837. asix_mdio_write (dev->net, dev->mii.phy_id, 0x1f, 0x0005);
  838. asix_mdio_write (dev->net, dev->mii.phy_id, 0x0c, 0);
  839. asix_mdio_write (dev->net, dev->mii.phy_id, 0x01,
  840. asix_mdio_read (dev->net, dev->mii.phy_id, 0x01) | 0x0080);
  841. asix_mdio_write (dev->net, dev->mii.phy_id, 0x1f, 0);
  842. if (data->ledmode == 12) {
  843. asix_mdio_write (dev->net, dev->mii.phy_id, 0x1f, 0x0002);
  844. asix_mdio_write (dev->net, dev->mii.phy_id, 0x1a, 0x00cb);
  845. asix_mdio_write (dev->net, dev->mii.phy_id, 0x1f, 0);
  846. }
  847. return 0;
  848. }
  849. static int marvell_led_status(struct usbnet *dev, u16 speed)
  850. {
  851. u16 reg = asix_mdio_read(dev->net, dev->mii.phy_id, MARVELL_LED_MANUAL);
  852. netdev_dbg(dev->net, "marvell_led_status() read 0x%04x\n", reg);
  853. /* Clear out the center LED bits - 0x03F0 */
  854. reg &= 0xfc0f;
  855. switch (speed) {
  856. case SPEED_1000:
  857. reg |= 0x03e0;
  858. break;
  859. case SPEED_100:
  860. reg |= 0x03b0;
  861. break;
  862. default:
  863. reg |= 0x02f0;
  864. }
  865. netdev_dbg(dev->net, "marvell_led_status() writing 0x%04x\n", reg);
  866. asix_mdio_write(dev->net, dev->mii.phy_id, MARVELL_LED_MANUAL, reg);
  867. return 0;
  868. }
  869. static int ax88178_reset(struct usbnet *dev)
  870. {
  871. struct asix_data *data = (struct asix_data *)&dev->data;
  872. int ret;
  873. __le16 eeprom;
  874. u8 status;
  875. int gpio0 = 0;
  876. u32 phyid;
  877. ret = asix_read_cmd(dev, AX_CMD_READ_GPIOS, 0, 0, 1, &status, 0);
  878. if (ret < 0) {
  879. netdev_dbg(dev->net, "Failed to read GPIOS: %d\n", ret);
  880. return ret;
  881. }
  882. netdev_dbg(dev->net, "GPIO Status: 0x%04x\n", status);
  883. asix_write_cmd(dev, AX_CMD_WRITE_ENABLE, 0, 0, 0, NULL, 0);
  884. ret = asix_read_cmd(dev, AX_CMD_READ_EEPROM, 0x0017, 0, 2, &eeprom, 0);
  885. if (ret < 0) {
  886. netdev_dbg(dev->net, "Failed to read EEPROM: %d\n", ret);
  887. return ret;
  888. }
  889. asix_write_cmd(dev, AX_CMD_WRITE_DISABLE, 0, 0, 0, NULL, 0);
  890. netdev_dbg(dev->net, "EEPROM index 0x17 is 0x%04x\n", eeprom);
  891. if (eeprom == cpu_to_le16(0xffff)) {
  892. data->phymode = PHY_MODE_MARVELL;
  893. data->ledmode = 0;
  894. gpio0 = 1;
  895. } else {
  896. data->phymode = le16_to_cpu(eeprom) & 0x7F;
  897. data->ledmode = le16_to_cpu(eeprom) >> 8;
  898. gpio0 = (le16_to_cpu(eeprom) & 0x80) ? 0 : 1;
  899. }
  900. netdev_dbg(dev->net, "GPIO0: %d, PhyMode: %d\n", gpio0, data->phymode);
  901. /* Power up external GigaPHY through AX88178 GPIO pin */
  902. asix_write_gpio(dev, AX_GPIO_RSE | AX_GPIO_GPO_1 |
  903. AX_GPIO_GPO1EN, 40, 0);
  904. if ((le16_to_cpu(eeprom) >> 8) != 1) {
  905. asix_write_gpio(dev, 0x003c, 30, 0);
  906. asix_write_gpio(dev, 0x001c, 300, 0);
  907. asix_write_gpio(dev, 0x003c, 30, 0);
  908. } else {
  909. netdev_dbg(dev->net, "gpio phymode == 1 path\n");
  910. asix_write_gpio(dev, AX_GPIO_GPO1EN, 30, 0);
  911. asix_write_gpio(dev, AX_GPIO_GPO1EN | AX_GPIO_GPO_1, 30, 0);
  912. }
  913. /* Read PHYID register *AFTER* powering up PHY */
  914. phyid = asix_get_phyid(dev);
  915. netdev_dbg(dev->net, "PHYID=0x%08x\n", phyid);
  916. /* Set AX88178 to enable MII/GMII/RGMII interface for external PHY */
  917. asix_write_cmd(dev, AX_CMD_SW_PHY_SELECT, 0, 0, 0, NULL, 0);
  918. asix_sw_reset(dev, 0, 0);
  919. msleep(150);
  920. asix_sw_reset(dev, AX_SWRESET_PRL | AX_SWRESET_IPPD, 0);
  921. msleep(150);
  922. asix_write_rx_ctl(dev, 0, 0);
  923. if (data->phymode == PHY_MODE_MARVELL) {
  924. marvell_phy_init(dev);
  925. msleep(60);
  926. } else if (data->phymode == PHY_MODE_RTL8211CL)
  927. rtl8211cl_phy_init(dev);
  928. asix_phy_reset(dev, BMCR_RESET | BMCR_ANENABLE);
  929. asix_mdio_write(dev->net, dev->mii.phy_id, MII_ADVERTISE,
  930. ADVERTISE_ALL | ADVERTISE_CSMA | ADVERTISE_PAUSE_CAP);
  931. asix_mdio_write(dev->net, dev->mii.phy_id, MII_CTRL1000,
  932. ADVERTISE_1000FULL);
  933. asix_write_medium_mode(dev, AX88178_MEDIUM_DEFAULT, 0);
  934. mii_nway_restart(&dev->mii);
  935. /* Rewrite MAC address */
  936. memcpy(data->mac_addr, dev->net->dev_addr, ETH_ALEN);
  937. ret = asix_write_cmd(dev, AX_CMD_WRITE_NODE_ID, 0, 0, ETH_ALEN,
  938. data->mac_addr, 0);
  939. if (ret < 0)
  940. return ret;
  941. ret = asix_write_rx_ctl(dev, AX_DEFAULT_RX_CTL, 0);
  942. if (ret < 0)
  943. return ret;
  944. return 0;
  945. }
  946. static int ax88178_link_reset(struct usbnet *dev)
  947. {
  948. u16 mode;
  949. struct ethtool_cmd ecmd = { .cmd = ETHTOOL_GSET };
  950. struct asix_data *data = (struct asix_data *)&dev->data;
  951. u32 speed;
  952. netdev_dbg(dev->net, "ax88178_link_reset()\n");
  953. mii_check_media(&dev->mii, 1, 1);
  954. mii_ethtool_gset(&dev->mii, &ecmd);
  955. mode = AX88178_MEDIUM_DEFAULT;
  956. speed = ethtool_cmd_speed(&ecmd);
  957. if (speed == SPEED_1000)
  958. mode |= AX_MEDIUM_GM;
  959. else if (speed == SPEED_100)
  960. mode |= AX_MEDIUM_PS;
  961. else
  962. mode &= ~(AX_MEDIUM_PS | AX_MEDIUM_GM);
  963. mode |= AX_MEDIUM_ENCK;
  964. if (ecmd.duplex == DUPLEX_FULL)
  965. mode |= AX_MEDIUM_FD;
  966. else
  967. mode &= ~AX_MEDIUM_FD;
  968. netdev_dbg(dev->net, "ax88178_link_reset() speed: %u duplex: %d setting mode to 0x%04x\n",
  969. speed, ecmd.duplex, mode);
  970. asix_write_medium_mode(dev, mode, 0);
  971. if (data->phymode == PHY_MODE_MARVELL && data->ledmode)
  972. marvell_led_status(dev, speed);
  973. return 0;
  974. }
  975. static void ax88178_set_mfb(struct usbnet *dev)
  976. {
  977. u16 mfb = AX_RX_CTL_MFB_16384;
  978. u16 rxctl;
  979. u16 medium;
  980. int old_rx_urb_size = dev->rx_urb_size;
  981. if (dev->hard_mtu < 2048) {
  982. dev->rx_urb_size = 2048;
  983. mfb = AX_RX_CTL_MFB_2048;
  984. } else if (dev->hard_mtu < 4096) {
  985. dev->rx_urb_size = 4096;
  986. mfb = AX_RX_CTL_MFB_4096;
  987. } else if (dev->hard_mtu < 8192) {
  988. dev->rx_urb_size = 8192;
  989. mfb = AX_RX_CTL_MFB_8192;
  990. } else if (dev->hard_mtu < 16384) {
  991. dev->rx_urb_size = 16384;
  992. mfb = AX_RX_CTL_MFB_16384;
  993. }
  994. rxctl = asix_read_rx_ctl(dev, 0);
  995. asix_write_rx_ctl(dev, (rxctl & ~AX_RX_CTL_MFB_16384) | mfb, 0);
  996. medium = asix_read_medium_status(dev, 0);
  997. if (dev->net->mtu > 1500)
  998. medium |= AX_MEDIUM_JFE;
  999. else
  1000. medium &= ~AX_MEDIUM_JFE;
  1001. asix_write_medium_mode(dev, medium, 0);
  1002. if (dev->rx_urb_size > old_rx_urb_size)
  1003. usbnet_unlink_rx_urbs(dev);
  1004. }
  1005. static int ax88178_change_mtu(struct net_device *net, int new_mtu)
  1006. {
  1007. struct usbnet *dev = netdev_priv(net);
  1008. int ll_mtu = new_mtu + net->hard_header_len + 4;
  1009. netdev_dbg(dev->net, "ax88178_change_mtu() new_mtu=%d\n", new_mtu);
  1010. if ((ll_mtu % dev->maxpacket) == 0)
  1011. return -EDOM;
  1012. WRITE_ONCE(net->mtu, new_mtu);
  1013. dev->hard_mtu = net->mtu + net->hard_header_len;
  1014. ax88178_set_mfb(dev);
  1015. /* max qlen depend on hard_mtu and rx_urb_size */
  1016. usbnet_update_max_qlen(dev);
  1017. return 0;
  1018. }
  1019. static const struct net_device_ops ax88178_netdev_ops = {
  1020. .ndo_open = usbnet_open,
  1021. .ndo_stop = usbnet_stop,
  1022. .ndo_start_xmit = usbnet_start_xmit,
  1023. .ndo_tx_timeout = usbnet_tx_timeout,
  1024. .ndo_get_stats64 = dev_get_tstats64,
  1025. .ndo_set_mac_address = asix_set_mac_address,
  1026. .ndo_validate_addr = eth_validate_addr,
  1027. .ndo_set_rx_mode = asix_set_multicast,
  1028. .ndo_eth_ioctl = asix_ioctl,
  1029. .ndo_change_mtu = ax88178_change_mtu,
  1030. };
  1031. static int ax88178_bind(struct usbnet *dev, struct usb_interface *intf)
  1032. {
  1033. int ret;
  1034. u8 buf[ETH_ALEN] = {0};
  1035. usbnet_get_endpoints(dev,intf);
  1036. /* Get the MAC address */
  1037. ret = asix_read_cmd(dev, AX_CMD_READ_NODE_ID, 0, 0, ETH_ALEN, buf, 0);
  1038. if (ret < 0) {
  1039. netdev_dbg(dev->net, "Failed to read MAC address: %d\n", ret);
  1040. return ret;
  1041. }
  1042. asix_set_netdev_dev_addr(dev, buf);
  1043. /* Initialize MII structure */
  1044. dev->mii.dev = dev->net;
  1045. dev->mii.mdio_read = asix_mdio_read;
  1046. dev->mii.mdio_write = asix_mdio_write;
  1047. dev->mii.phy_id_mask = 0x1f;
  1048. dev->mii.reg_num_mask = 0xff;
  1049. dev->mii.supports_gmii = 1;
  1050. dev->mii.phy_id = asix_read_phy_addr(dev, true);
  1051. if (dev->mii.phy_id < 0)
  1052. return dev->mii.phy_id;
  1053. dev->net->netdev_ops = &ax88178_netdev_ops;
  1054. dev->net->ethtool_ops = &ax88178_ethtool_ops;
  1055. dev->net->max_mtu = 16384 - (dev->net->hard_header_len + 4);
  1056. /* Blink LEDS so users know driver saw dongle */
  1057. asix_sw_reset(dev, 0, 0);
  1058. msleep(150);
  1059. asix_sw_reset(dev, AX_SWRESET_PRL | AX_SWRESET_IPPD, 0);
  1060. msleep(150);
  1061. /* Asix framing packs multiple eth frames into a 2K usb bulk transfer */
  1062. if (dev->driver_info->flags & FLAG_FRAMING_AX) {
  1063. /* hard_mtu is still the default - the device does not support
  1064. jumbo eth frames */
  1065. dev->rx_urb_size = 2048;
  1066. }
  1067. dev->driver_priv = kzalloc(sizeof(struct asix_common_private), GFP_KERNEL);
  1068. if (!dev->driver_priv)
  1069. return -ENOMEM;
  1070. return 0;
  1071. }
  1072. static const struct driver_info ax8817x_info = {
  1073. .description = "ASIX AX8817x USB 2.0 Ethernet",
  1074. .bind = ax88172_bind,
  1075. .status = asix_status,
  1076. .link_reset = ax88172_link_reset,
  1077. .reset = ax88172_link_reset,
  1078. .flags = FLAG_ETHER | FLAG_LINK_INTR,
  1079. .data = 0x00130103,
  1080. };
  1081. static const struct driver_info dlink_dub_e100_info = {
  1082. .description = "DLink DUB-E100 USB Ethernet",
  1083. .bind = ax88172_bind,
  1084. .status = asix_status,
  1085. .link_reset = ax88172_link_reset,
  1086. .reset = ax88172_link_reset,
  1087. .flags = FLAG_ETHER | FLAG_LINK_INTR,
  1088. .data = 0x009f9d9f,
  1089. };
  1090. static const struct driver_info netgear_fa120_info = {
  1091. .description = "Netgear FA-120 USB Ethernet",
  1092. .bind = ax88172_bind,
  1093. .status = asix_status,
  1094. .link_reset = ax88172_link_reset,
  1095. .reset = ax88172_link_reset,
  1096. .flags = FLAG_ETHER | FLAG_LINK_INTR,
  1097. .data = 0x00130103,
  1098. };
  1099. static const struct driver_info hawking_uf200_info = {
  1100. .description = "Hawking UF200 USB Ethernet",
  1101. .bind = ax88172_bind,
  1102. .status = asix_status,
  1103. .link_reset = ax88172_link_reset,
  1104. .reset = ax88172_link_reset,
  1105. .flags = FLAG_ETHER | FLAG_LINK_INTR,
  1106. .data = 0x001f1d1f,
  1107. };
  1108. static const struct driver_info ax88772_info = {
  1109. .description = "ASIX AX88772 USB 2.0 Ethernet",
  1110. .bind = ax88772_bind,
  1111. .unbind = ax88772_unbind,
  1112. .reset = ax88772_reset,
  1113. .stop = ax88772_stop,
  1114. .flags = FLAG_ETHER | FLAG_FRAMING_AX | FLAG_MULTI_PACKET,
  1115. .rx_fixup = asix_rx_fixup_common,
  1116. .tx_fixup = asix_tx_fixup,
  1117. };
  1118. static const struct driver_info ax88772b_info = {
  1119. .description = "ASIX AX88772B USB 2.0 Ethernet",
  1120. .bind = ax88772_bind,
  1121. .unbind = ax88772_unbind,
  1122. .reset = ax88772_reset,
  1123. .stop = ax88772_stop,
  1124. .flags = FLAG_ETHER | FLAG_FRAMING_AX | FLAG_MULTI_PACKET,
  1125. .rx_fixup = asix_rx_fixup_common,
  1126. .tx_fixup = asix_tx_fixup,
  1127. .data = FLAG_EEPROM_MAC,
  1128. };
  1129. static const struct driver_info lxausb_t1l_info = {
  1130. .description = "Linux Automation GmbH USB 10Base-T1L",
  1131. .bind = ax88772_bind,
  1132. .unbind = ax88772_unbind,
  1133. .reset = ax88772_reset,
  1134. .stop = ax88772_stop,
  1135. .flags = FLAG_ETHER | FLAG_FRAMING_AX | FLAG_MULTI_PACKET,
  1136. .rx_fixup = asix_rx_fixup_common,
  1137. .tx_fixup = asix_tx_fixup,
  1138. .data = FLAG_EEPROM_MAC,
  1139. };
  1140. static const struct driver_info ax88178_info = {
  1141. .description = "ASIX AX88178 USB 2.0 Ethernet",
  1142. .bind = ax88178_bind,
  1143. .unbind = ax88178_unbind,
  1144. .status = asix_status,
  1145. .link_reset = ax88178_link_reset,
  1146. .reset = ax88178_reset,
  1147. .flags = FLAG_ETHER | FLAG_FRAMING_AX | FLAG_LINK_INTR |
  1148. FLAG_MULTI_PACKET,
  1149. .rx_fixup = asix_rx_fixup_common,
  1150. .tx_fixup = asix_tx_fixup,
  1151. };
  1152. /*
  1153. * USBLINK 20F9 "USB 2.0 LAN" USB ethernet adapter, typically found in
  1154. * no-name packaging.
  1155. * USB device strings are:
  1156. * 1: Manufacturer: USBLINK
  1157. * 2: Product: HG20F9 USB2.0
  1158. * 3: Serial: 000003
  1159. * Appears to be compatible with Asix 88772B.
  1160. */
  1161. static const struct driver_info hg20f9_info = {
  1162. .description = "HG20F9 USB 2.0 Ethernet",
  1163. .bind = ax88772_bind,
  1164. .unbind = ax88772_unbind,
  1165. .reset = ax88772_reset,
  1166. .flags = FLAG_ETHER | FLAG_FRAMING_AX | FLAG_MULTI_PACKET,
  1167. .rx_fixup = asix_rx_fixup_common,
  1168. .tx_fixup = asix_tx_fixup,
  1169. .data = FLAG_EEPROM_MAC,
  1170. };
  1171. static const struct driver_info lyconsys_fibergecko100_info = {
  1172. .description = "LyconSys FiberGecko 100 USB 2.0 to SFP Adapter",
  1173. .bind = ax88178_bind,
  1174. .status = asix_status,
  1175. .link_reset = ax88178_link_reset,
  1176. .reset = ax88178_link_reset,
  1177. .flags = FLAG_ETHER | FLAG_FRAMING_AX | FLAG_LINK_INTR |
  1178. FLAG_MULTI_PACKET,
  1179. .rx_fixup = asix_rx_fixup_common,
  1180. .tx_fixup = asix_tx_fixup,
  1181. .data = 0x20061201,
  1182. };
  1183. static const struct usb_device_id products [] = {
  1184. {
  1185. // Linksys USB200M
  1186. USB_DEVICE (0x077b, 0x2226),
  1187. .driver_info = (unsigned long) &ax8817x_info,
  1188. }, {
  1189. // Netgear FA120
  1190. USB_DEVICE (0x0846, 0x1040),
  1191. .driver_info = (unsigned long) &netgear_fa120_info,
  1192. }, {
  1193. // DLink DUB-E100
  1194. USB_DEVICE (0x2001, 0x1a00),
  1195. .driver_info = (unsigned long) &dlink_dub_e100_info,
  1196. }, {
  1197. // Intellinet, ST Lab USB Ethernet
  1198. USB_DEVICE (0x0b95, 0x1720),
  1199. .driver_info = (unsigned long) &ax8817x_info,
  1200. }, {
  1201. // Hawking UF200, TrendNet TU2-ET100
  1202. USB_DEVICE (0x07b8, 0x420a),
  1203. .driver_info = (unsigned long) &hawking_uf200_info,
  1204. }, {
  1205. // Billionton Systems, USB2AR
  1206. USB_DEVICE (0x08dd, 0x90ff),
  1207. .driver_info = (unsigned long) &ax8817x_info,
  1208. }, {
  1209. // Billionton Systems, GUSB2AM-1G-B
  1210. USB_DEVICE(0x08dd, 0x0114),
  1211. .driver_info = (unsigned long) &ax88178_info,
  1212. }, {
  1213. // ATEN UC210T
  1214. USB_DEVICE (0x0557, 0x2009),
  1215. .driver_info = (unsigned long) &ax8817x_info,
  1216. }, {
  1217. // Buffalo LUA-U2-KTX
  1218. USB_DEVICE (0x0411, 0x003d),
  1219. .driver_info = (unsigned long) &ax8817x_info,
  1220. }, {
  1221. // Buffalo LUA-U2-GT 10/100/1000
  1222. USB_DEVICE (0x0411, 0x006e),
  1223. .driver_info = (unsigned long) &ax88178_info,
  1224. }, {
  1225. // Sitecom LN-029 "USB 2.0 10/100 Ethernet adapter"
  1226. USB_DEVICE (0x6189, 0x182d),
  1227. .driver_info = (unsigned long) &ax8817x_info,
  1228. }, {
  1229. // Sitecom LN-031 "USB 2.0 10/100/1000 Ethernet adapter"
  1230. USB_DEVICE (0x0df6, 0x0056),
  1231. .driver_info = (unsigned long) &ax88178_info,
  1232. }, {
  1233. // Sitecom LN-028 "USB 2.0 10/100/1000 Ethernet adapter"
  1234. USB_DEVICE (0x0df6, 0x061c),
  1235. .driver_info = (unsigned long) &ax88178_info,
  1236. }, {
  1237. // corega FEther USB2-TX
  1238. USB_DEVICE (0x07aa, 0x0017),
  1239. .driver_info = (unsigned long) &ax8817x_info,
  1240. }, {
  1241. // Surecom EP-1427X-2
  1242. USB_DEVICE (0x1189, 0x0893),
  1243. .driver_info = (unsigned long) &ax8817x_info,
  1244. }, {
  1245. // goodway corp usb gwusb2e
  1246. USB_DEVICE (0x1631, 0x6200),
  1247. .driver_info = (unsigned long) &ax8817x_info,
  1248. }, {
  1249. // JVC MP-PRX1 Port Replicator
  1250. USB_DEVICE (0x04f1, 0x3008),
  1251. .driver_info = (unsigned long) &ax8817x_info,
  1252. }, {
  1253. // Lenovo U2L100P 10/100
  1254. USB_DEVICE (0x17ef, 0x7203),
  1255. .driver_info = (unsigned long)&ax88772b_info,
  1256. }, {
  1257. // ASIX AX88772B 10/100
  1258. USB_DEVICE (0x0b95, 0x772b),
  1259. .driver_info = (unsigned long) &ax88772b_info,
  1260. }, {
  1261. // ASIX AX88772 10/100
  1262. USB_DEVICE (0x0b95, 0x7720),
  1263. .driver_info = (unsigned long) &ax88772_info,
  1264. }, {
  1265. // ASIX AX88178 10/100/1000
  1266. USB_DEVICE (0x0b95, 0x1780),
  1267. .driver_info = (unsigned long) &ax88178_info,
  1268. }, {
  1269. // Logitec LAN-GTJ/U2A
  1270. USB_DEVICE (0x0789, 0x0160),
  1271. .driver_info = (unsigned long) &ax88178_info,
  1272. }, {
  1273. // Linksys USB200M Rev 2
  1274. USB_DEVICE (0x13b1, 0x0018),
  1275. .driver_info = (unsigned long) &ax88772_info,
  1276. }, {
  1277. // 0Q0 cable ethernet
  1278. USB_DEVICE (0x1557, 0x7720),
  1279. .driver_info = (unsigned long) &ax88772_info,
  1280. }, {
  1281. // DLink DUB-E100 H/W Ver B1
  1282. USB_DEVICE (0x07d1, 0x3c05),
  1283. .driver_info = (unsigned long) &ax88772_info,
  1284. }, {
  1285. // DLink DUB-E100 H/W Ver B1 Alternate
  1286. USB_DEVICE (0x2001, 0x3c05),
  1287. .driver_info = (unsigned long) &ax88772_info,
  1288. }, {
  1289. // DLink DUB-E100 H/W Ver C1
  1290. USB_DEVICE (0x2001, 0x1a02),
  1291. .driver_info = (unsigned long) &ax88772_info,
  1292. }, {
  1293. // Linksys USB1000
  1294. USB_DEVICE (0x1737, 0x0039),
  1295. .driver_info = (unsigned long) &ax88178_info,
  1296. }, {
  1297. // IO-DATA ETG-US2
  1298. USB_DEVICE (0x04bb, 0x0930),
  1299. .driver_info = (unsigned long) &ax88178_info,
  1300. }, {
  1301. // Belkin F5D5055
  1302. USB_DEVICE(0x050d, 0x5055),
  1303. .driver_info = (unsigned long) &ax88178_info,
  1304. }, {
  1305. // Apple USB Ethernet Adapter
  1306. USB_DEVICE(0x05ac, 0x1402),
  1307. .driver_info = (unsigned long) &ax88772_info,
  1308. }, {
  1309. // Cables-to-Go USB Ethernet Adapter
  1310. USB_DEVICE(0x0b95, 0x772a),
  1311. .driver_info = (unsigned long) &ax88772_info,
  1312. }, {
  1313. // ABOCOM for pci
  1314. USB_DEVICE(0x14ea, 0xab11),
  1315. .driver_info = (unsigned long) &ax88178_info,
  1316. }, {
  1317. // ASIX 88772a
  1318. USB_DEVICE(0x0db0, 0xa877),
  1319. .driver_info = (unsigned long) &ax88772_info,
  1320. }, {
  1321. // Asus USB Ethernet Adapter
  1322. USB_DEVICE (0x0b95, 0x7e2b),
  1323. .driver_info = (unsigned long)&ax88772b_info,
  1324. }, {
  1325. /* ASIX 88172a demo board */
  1326. USB_DEVICE(0x0b95, 0x172a),
  1327. .driver_info = (unsigned long) &ax88172a_info,
  1328. }, {
  1329. /*
  1330. * USBLINK HG20F9 "USB 2.0 LAN"
  1331. * Appears to have gazumped Linksys's manufacturer ID but
  1332. * doesn't (yet) conflict with any known Linksys product.
  1333. */
  1334. USB_DEVICE(0x066b, 0x20f9),
  1335. .driver_info = (unsigned long) &hg20f9_info,
  1336. }, {
  1337. // Linux Automation GmbH USB 10Base-T1L
  1338. USB_DEVICE(0x33f7, 0x0004),
  1339. .driver_info = (unsigned long) &lxausb_t1l_info,
  1340. }, {
  1341. /* LyconSys FiberGecko 100 */
  1342. USB_DEVICE(0x1d2a, 0x0801),
  1343. .driver_info = (unsigned long) &lyconsys_fibergecko100_info,
  1344. },
  1345. { }, // END
  1346. };
  1347. MODULE_DEVICE_TABLE(usb, products);
  1348. static struct usb_driver asix_driver = {
  1349. .name = DRIVER_NAME,
  1350. .id_table = products,
  1351. .probe = usbnet_probe,
  1352. .suspend = asix_suspend,
  1353. .resume = asix_resume,
  1354. .reset_resume = asix_resume,
  1355. .disconnect = usbnet_disconnect,
  1356. /* usbnet enables autosuspend by default (supports_autosuspend=1).
  1357. * We keep runtime-PM active for AX88772* by taking a PM usage
  1358. * reference in ax88772_bind() (pm_runtime_get_noresume()) and
  1359. * dropping it in unbind(), which effectively blocks autosuspend.
  1360. */
  1361. .supports_autosuspend = 1,
  1362. .disable_hub_initiated_lpm = 1,
  1363. };
  1364. module_usb_driver(asix_driver);
  1365. MODULE_AUTHOR("David Hollis");
  1366. MODULE_VERSION(DRIVER_VERSION);
  1367. MODULE_DESCRIPTION("ASIX AX8817X based USB 2.0 Ethernet Devices");
  1368. MODULE_LICENSE("GPL");