asix_devices.c 42 KB

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