asix_devices.c 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587
  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 <[email protected]>
  5. * Copyright (C) 2005 Phil Chang <[email protected]>
  6. * Copyright (C) 2006 James Painter <[email protected]>
  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. static int asix_suspend(struct usb_interface *intf, pm_message_t message)
  510. {
  511. struct usbnet *dev = usb_get_intfdata(intf);
  512. struct asix_common_private *priv = dev->driver_priv;
  513. if (priv && priv->suspend)
  514. priv->suspend(dev);
  515. return usbnet_suspend(intf, message);
  516. }
  517. static void ax88772_resume(struct usbnet *dev)
  518. {
  519. struct asix_common_private *priv = dev->driver_priv;
  520. int i;
  521. for (i = 0; i < 3; i++)
  522. if (!priv->reset(dev, 1))
  523. break;
  524. if (netif_running(dev->net)) {
  525. rtnl_lock();
  526. phylink_resume(priv->phylink);
  527. rtnl_unlock();
  528. }
  529. }
  530. static int asix_resume(struct usb_interface *intf)
  531. {
  532. struct usbnet *dev = usb_get_intfdata(intf);
  533. struct asix_common_private *priv = dev->driver_priv;
  534. if (priv && priv->resume)
  535. priv->resume(dev);
  536. return usbnet_resume(intf);
  537. }
  538. static int ax88772_init_mdio(struct usbnet *dev)
  539. {
  540. struct asix_common_private *priv = dev->driver_priv;
  541. int ret;
  542. priv->mdio = mdiobus_alloc();
  543. if (!priv->mdio)
  544. return -ENOMEM;
  545. priv->mdio->priv = dev;
  546. priv->mdio->read = &asix_mdio_bus_read;
  547. priv->mdio->write = &asix_mdio_bus_write;
  548. priv->mdio->name = "Asix MDIO Bus";
  549. /* mii bus name is usb-<usb bus number>-<usb device number> */
  550. snprintf(priv->mdio->id, MII_BUS_ID_SIZE, "usb-%03d:%03d",
  551. dev->udev->bus->busnum, dev->udev->devnum);
  552. ret = mdiobus_register(priv->mdio);
  553. if (ret) {
  554. netdev_err(dev->net, "Could not register MDIO bus (err %d)\n", ret);
  555. mdiobus_free(priv->mdio);
  556. priv->mdio = NULL;
  557. }
  558. return ret;
  559. }
  560. static void ax88772_mdio_unregister(struct asix_common_private *priv)
  561. {
  562. mdiobus_unregister(priv->mdio);
  563. mdiobus_free(priv->mdio);
  564. }
  565. static int ax88772_init_phy(struct usbnet *dev)
  566. {
  567. struct asix_common_private *priv = dev->driver_priv;
  568. int ret;
  569. priv->phydev = mdiobus_get_phy(priv->mdio, priv->phy_addr);
  570. if (!priv->phydev) {
  571. netdev_err(dev->net, "Could not find PHY\n");
  572. return -ENODEV;
  573. }
  574. ret = phylink_connect_phy(priv->phylink, priv->phydev);
  575. if (ret) {
  576. netdev_err(dev->net, "Could not connect PHY\n");
  577. return ret;
  578. }
  579. phy_suspend(priv->phydev);
  580. priv->phydev->mac_managed_pm = 1;
  581. phy_attached_info(priv->phydev);
  582. if (priv->embd_phy)
  583. return 0;
  584. /* In case main PHY is not the embedded PHY and MAC is RMII clock
  585. * provider, we need to suspend embedded PHY by keeping PLL enabled
  586. * (AX_SWRESET_IPPD == 0).
  587. */
  588. priv->phydev_int = mdiobus_get_phy(priv->mdio, AX_EMBD_PHY_ADDR);
  589. if (!priv->phydev_int) {
  590. rtnl_lock();
  591. phylink_disconnect_phy(priv->phylink);
  592. rtnl_unlock();
  593. netdev_err(dev->net, "Could not find internal PHY\n");
  594. return -ENODEV;
  595. }
  596. priv->phydev_int->mac_managed_pm = 1;
  597. phy_suspend(priv->phydev_int);
  598. return 0;
  599. }
  600. static void ax88772_mac_config(struct phylink_config *config, unsigned int mode,
  601. const struct phylink_link_state *state)
  602. {
  603. /* Nothing to do */
  604. }
  605. static void ax88772_mac_link_down(struct phylink_config *config,
  606. unsigned int mode, phy_interface_t interface)
  607. {
  608. struct usbnet *dev = netdev_priv(to_net_dev(config->dev));
  609. asix_write_medium_mode(dev, 0, 0);
  610. usbnet_link_change(dev, false, false);
  611. }
  612. static void ax88772_mac_link_up(struct phylink_config *config,
  613. struct phy_device *phy,
  614. unsigned int mode, phy_interface_t interface,
  615. int speed, int duplex,
  616. bool tx_pause, bool rx_pause)
  617. {
  618. struct usbnet *dev = netdev_priv(to_net_dev(config->dev));
  619. u16 m = AX_MEDIUM_AC | AX_MEDIUM_RE;
  620. m |= duplex ? AX_MEDIUM_FD : 0;
  621. switch (speed) {
  622. case SPEED_100:
  623. m |= AX_MEDIUM_PS;
  624. break;
  625. case SPEED_10:
  626. break;
  627. default:
  628. return;
  629. }
  630. if (tx_pause)
  631. m |= AX_MEDIUM_TFC;
  632. if (rx_pause)
  633. m |= AX_MEDIUM_RFC;
  634. asix_write_medium_mode(dev, m, 0);
  635. usbnet_link_change(dev, true, false);
  636. }
  637. static const struct phylink_mac_ops ax88772_phylink_mac_ops = {
  638. .validate = phylink_generic_validate,
  639. .mac_config = ax88772_mac_config,
  640. .mac_link_down = ax88772_mac_link_down,
  641. .mac_link_up = ax88772_mac_link_up,
  642. };
  643. static int ax88772_phylink_setup(struct usbnet *dev)
  644. {
  645. struct asix_common_private *priv = dev->driver_priv;
  646. phy_interface_t phy_if_mode;
  647. struct phylink *phylink;
  648. priv->phylink_config.dev = &dev->net->dev;
  649. priv->phylink_config.type = PHYLINK_NETDEV;
  650. priv->phylink_config.mac_capabilities = MAC_SYM_PAUSE | MAC_ASYM_PAUSE |
  651. MAC_10 | MAC_100;
  652. __set_bit(PHY_INTERFACE_MODE_INTERNAL,
  653. priv->phylink_config.supported_interfaces);
  654. __set_bit(PHY_INTERFACE_MODE_RMII,
  655. priv->phylink_config.supported_interfaces);
  656. if (priv->embd_phy)
  657. phy_if_mode = PHY_INTERFACE_MODE_INTERNAL;
  658. else
  659. phy_if_mode = PHY_INTERFACE_MODE_RMII;
  660. phylink = phylink_create(&priv->phylink_config, dev->net->dev.fwnode,
  661. phy_if_mode, &ax88772_phylink_mac_ops);
  662. if (IS_ERR(phylink))
  663. return PTR_ERR(phylink);
  664. priv->phylink = phylink;
  665. return 0;
  666. }
  667. static int ax88772_bind(struct usbnet *dev, struct usb_interface *intf)
  668. {
  669. struct asix_common_private *priv;
  670. u8 buf[ETH_ALEN] = {0};
  671. int ret, i;
  672. priv = devm_kzalloc(&dev->udev->dev, sizeof(*priv), GFP_KERNEL);
  673. if (!priv)
  674. return -ENOMEM;
  675. dev->driver_priv = priv;
  676. usbnet_get_endpoints(dev, intf);
  677. /* Maybe the boot loader passed the MAC address via device tree */
  678. if (!eth_platform_get_mac_address(&dev->udev->dev, buf)) {
  679. netif_dbg(dev, ifup, dev->net,
  680. "MAC address read from device tree");
  681. } else {
  682. /* Try getting the MAC address from EEPROM */
  683. if (dev->driver_info->data & FLAG_EEPROM_MAC) {
  684. for (i = 0; i < (ETH_ALEN >> 1); i++) {
  685. ret = asix_read_cmd(dev, AX_CMD_READ_EEPROM,
  686. 0x04 + i, 0, 2, buf + i * 2,
  687. 0);
  688. if (ret < 0)
  689. break;
  690. }
  691. } else {
  692. ret = asix_read_cmd(dev, AX_CMD_READ_NODE_ID,
  693. 0, 0, ETH_ALEN, buf, 0);
  694. }
  695. if (ret < 0) {
  696. netdev_dbg(dev->net, "Failed to read MAC address: %d\n",
  697. ret);
  698. return ret;
  699. }
  700. }
  701. asix_set_netdev_dev_addr(dev, buf);
  702. dev->net->netdev_ops = &ax88772_netdev_ops;
  703. dev->net->ethtool_ops = &ax88772_ethtool_ops;
  704. dev->net->needed_headroom = 4; /* cf asix_tx_fixup() */
  705. dev->net->needed_tailroom = 4; /* cf asix_tx_fixup() */
  706. ret = asix_read_phy_addr(dev, true);
  707. if (ret < 0)
  708. return ret;
  709. priv->phy_addr = ret;
  710. priv->embd_phy = ((priv->phy_addr & 0x1f) == AX_EMBD_PHY_ADDR);
  711. ret = asix_read_cmd(dev, AX_CMD_STATMNGSTS_REG, 0, 0, 1,
  712. &priv->chipcode, 0);
  713. if (ret < 0) {
  714. netdev_dbg(dev->net, "Failed to read STATMNGSTS_REG: %d\n", ret);
  715. return ret;
  716. }
  717. priv->chipcode &= AX_CHIPCODE_MASK;
  718. priv->resume = ax88772_resume;
  719. priv->suspend = ax88772_suspend;
  720. if (priv->chipcode == AX_AX88772_CHIPCODE)
  721. priv->reset = ax88772_hw_reset;
  722. else
  723. priv->reset = ax88772a_hw_reset;
  724. ret = priv->reset(dev, 0);
  725. if (ret < 0) {
  726. netdev_dbg(dev->net, "Failed to reset AX88772: %d\n", ret);
  727. return ret;
  728. }
  729. /* Asix framing packs multiple eth frames into a 2K usb bulk transfer */
  730. if (dev->driver_info->flags & FLAG_FRAMING_AX) {
  731. /* hard_mtu is still the default - the device does not support
  732. jumbo eth frames */
  733. dev->rx_urb_size = 2048;
  734. }
  735. priv->presvd_phy_bmcr = 0;
  736. priv->presvd_phy_advertise = 0;
  737. ret = ax88772_init_mdio(dev);
  738. if (ret)
  739. goto mdio_err;
  740. ret = ax88772_phylink_setup(dev);
  741. if (ret)
  742. goto phylink_err;
  743. ret = ax88772_init_phy(dev);
  744. if (ret)
  745. goto initphy_err;
  746. return 0;
  747. initphy_err:
  748. phylink_destroy(priv->phylink);
  749. phylink_err:
  750. ax88772_mdio_unregister(priv);
  751. mdio_err:
  752. return ret;
  753. }
  754. static int ax88772_stop(struct usbnet *dev)
  755. {
  756. struct asix_common_private *priv = dev->driver_priv;
  757. phylink_stop(priv->phylink);
  758. return 0;
  759. }
  760. static void ax88772_unbind(struct usbnet *dev, struct usb_interface *intf)
  761. {
  762. struct asix_common_private *priv = dev->driver_priv;
  763. rtnl_lock();
  764. phylink_disconnect_phy(priv->phylink);
  765. rtnl_unlock();
  766. phylink_destroy(priv->phylink);
  767. ax88772_mdio_unregister(priv);
  768. asix_rx_fixup_common_free(dev->driver_priv);
  769. }
  770. static void ax88178_unbind(struct usbnet *dev, struct usb_interface *intf)
  771. {
  772. asix_rx_fixup_common_free(dev->driver_priv);
  773. kfree(dev->driver_priv);
  774. }
  775. static const struct ethtool_ops ax88178_ethtool_ops = {
  776. .get_drvinfo = asix_get_drvinfo,
  777. .get_link = asix_get_link,
  778. .get_msglevel = usbnet_get_msglevel,
  779. .set_msglevel = usbnet_set_msglevel,
  780. .get_wol = asix_get_wol,
  781. .set_wol = asix_set_wol,
  782. .get_eeprom_len = asix_get_eeprom_len,
  783. .get_eeprom = asix_get_eeprom,
  784. .set_eeprom = asix_set_eeprom,
  785. .nway_reset = usbnet_nway_reset,
  786. .get_link_ksettings = usbnet_get_link_ksettings_mii,
  787. .set_link_ksettings = usbnet_set_link_ksettings_mii,
  788. };
  789. static int marvell_phy_init(struct usbnet *dev)
  790. {
  791. struct asix_data *data = (struct asix_data *)&dev->data;
  792. u16 reg;
  793. netdev_dbg(dev->net, "marvell_phy_init()\n");
  794. reg = asix_mdio_read(dev->net, dev->mii.phy_id, MII_MARVELL_STATUS);
  795. netdev_dbg(dev->net, "MII_MARVELL_STATUS = 0x%04x\n", reg);
  796. asix_mdio_write(dev->net, dev->mii.phy_id, MII_MARVELL_CTRL,
  797. MARVELL_CTRL_RXDELAY | MARVELL_CTRL_TXDELAY);
  798. if (data->ledmode) {
  799. reg = asix_mdio_read(dev->net, dev->mii.phy_id,
  800. MII_MARVELL_LED_CTRL);
  801. netdev_dbg(dev->net, "MII_MARVELL_LED_CTRL (1) = 0x%04x\n", reg);
  802. reg &= 0xf8ff;
  803. reg |= (1 + 0x0100);
  804. asix_mdio_write(dev->net, dev->mii.phy_id,
  805. MII_MARVELL_LED_CTRL, reg);
  806. reg = asix_mdio_read(dev->net, dev->mii.phy_id,
  807. MII_MARVELL_LED_CTRL);
  808. netdev_dbg(dev->net, "MII_MARVELL_LED_CTRL (2) = 0x%04x\n", reg);
  809. }
  810. return 0;
  811. }
  812. static int rtl8211cl_phy_init(struct usbnet *dev)
  813. {
  814. struct asix_data *data = (struct asix_data *)&dev->data;
  815. netdev_dbg(dev->net, "rtl8211cl_phy_init()\n");
  816. asix_mdio_write (dev->net, dev->mii.phy_id, 0x1f, 0x0005);
  817. asix_mdio_write (dev->net, dev->mii.phy_id, 0x0c, 0);
  818. asix_mdio_write (dev->net, dev->mii.phy_id, 0x01,
  819. asix_mdio_read (dev->net, dev->mii.phy_id, 0x01) | 0x0080);
  820. asix_mdio_write (dev->net, dev->mii.phy_id, 0x1f, 0);
  821. if (data->ledmode == 12) {
  822. asix_mdio_write (dev->net, dev->mii.phy_id, 0x1f, 0x0002);
  823. asix_mdio_write (dev->net, dev->mii.phy_id, 0x1a, 0x00cb);
  824. asix_mdio_write (dev->net, dev->mii.phy_id, 0x1f, 0);
  825. }
  826. return 0;
  827. }
  828. static int marvell_led_status(struct usbnet *dev, u16 speed)
  829. {
  830. u16 reg = asix_mdio_read(dev->net, dev->mii.phy_id, MARVELL_LED_MANUAL);
  831. netdev_dbg(dev->net, "marvell_led_status() read 0x%04x\n", reg);
  832. /* Clear out the center LED bits - 0x03F0 */
  833. reg &= 0xfc0f;
  834. switch (speed) {
  835. case SPEED_1000:
  836. reg |= 0x03e0;
  837. break;
  838. case SPEED_100:
  839. reg |= 0x03b0;
  840. break;
  841. default:
  842. reg |= 0x02f0;
  843. }
  844. netdev_dbg(dev->net, "marvell_led_status() writing 0x%04x\n", reg);
  845. asix_mdio_write(dev->net, dev->mii.phy_id, MARVELL_LED_MANUAL, reg);
  846. return 0;
  847. }
  848. static int ax88178_reset(struct usbnet *dev)
  849. {
  850. struct asix_data *data = (struct asix_data *)&dev->data;
  851. int ret;
  852. __le16 eeprom;
  853. u8 status;
  854. int gpio0 = 0;
  855. u32 phyid;
  856. ret = asix_read_cmd(dev, AX_CMD_READ_GPIOS, 0, 0, 1, &status, 0);
  857. if (ret < 0) {
  858. netdev_dbg(dev->net, "Failed to read GPIOS: %d\n", ret);
  859. return ret;
  860. }
  861. netdev_dbg(dev->net, "GPIO Status: 0x%04x\n", status);
  862. asix_write_cmd(dev, AX_CMD_WRITE_ENABLE, 0, 0, 0, NULL, 0);
  863. ret = asix_read_cmd(dev, AX_CMD_READ_EEPROM, 0x0017, 0, 2, &eeprom, 0);
  864. if (ret < 0) {
  865. netdev_dbg(dev->net, "Failed to read EEPROM: %d\n", ret);
  866. return ret;
  867. }
  868. asix_write_cmd(dev, AX_CMD_WRITE_DISABLE, 0, 0, 0, NULL, 0);
  869. netdev_dbg(dev->net, "EEPROM index 0x17 is 0x%04x\n", eeprom);
  870. if (eeprom == cpu_to_le16(0xffff)) {
  871. data->phymode = PHY_MODE_MARVELL;
  872. data->ledmode = 0;
  873. gpio0 = 1;
  874. } else {
  875. data->phymode = le16_to_cpu(eeprom) & 0x7F;
  876. data->ledmode = le16_to_cpu(eeprom) >> 8;
  877. gpio0 = (le16_to_cpu(eeprom) & 0x80) ? 0 : 1;
  878. }
  879. netdev_dbg(dev->net, "GPIO0: %d, PhyMode: %d\n", gpio0, data->phymode);
  880. /* Power up external GigaPHY through AX88178 GPIO pin */
  881. asix_write_gpio(dev, AX_GPIO_RSE | AX_GPIO_GPO_1 |
  882. AX_GPIO_GPO1EN, 40, 0);
  883. if ((le16_to_cpu(eeprom) >> 8) != 1) {
  884. asix_write_gpio(dev, 0x003c, 30, 0);
  885. asix_write_gpio(dev, 0x001c, 300, 0);
  886. asix_write_gpio(dev, 0x003c, 30, 0);
  887. } else {
  888. netdev_dbg(dev->net, "gpio phymode == 1 path\n");
  889. asix_write_gpio(dev, AX_GPIO_GPO1EN, 30, 0);
  890. asix_write_gpio(dev, AX_GPIO_GPO1EN | AX_GPIO_GPO_1, 30, 0);
  891. }
  892. /* Read PHYID register *AFTER* powering up PHY */
  893. phyid = asix_get_phyid(dev);
  894. netdev_dbg(dev->net, "PHYID=0x%08x\n", phyid);
  895. /* Set AX88178 to enable MII/GMII/RGMII interface for external PHY */
  896. asix_write_cmd(dev, AX_CMD_SW_PHY_SELECT, 0, 0, 0, NULL, 0);
  897. asix_sw_reset(dev, 0, 0);
  898. msleep(150);
  899. asix_sw_reset(dev, AX_SWRESET_PRL | AX_SWRESET_IPPD, 0);
  900. msleep(150);
  901. asix_write_rx_ctl(dev, 0, 0);
  902. if (data->phymode == PHY_MODE_MARVELL) {
  903. marvell_phy_init(dev);
  904. msleep(60);
  905. } else if (data->phymode == PHY_MODE_RTL8211CL)
  906. rtl8211cl_phy_init(dev);
  907. asix_phy_reset(dev, BMCR_RESET | BMCR_ANENABLE);
  908. asix_mdio_write(dev->net, dev->mii.phy_id, MII_ADVERTISE,
  909. ADVERTISE_ALL | ADVERTISE_CSMA | ADVERTISE_PAUSE_CAP);
  910. asix_mdio_write(dev->net, dev->mii.phy_id, MII_CTRL1000,
  911. ADVERTISE_1000FULL);
  912. asix_write_medium_mode(dev, AX88178_MEDIUM_DEFAULT, 0);
  913. mii_nway_restart(&dev->mii);
  914. /* Rewrite MAC address */
  915. memcpy(data->mac_addr, dev->net->dev_addr, ETH_ALEN);
  916. ret = asix_write_cmd(dev, AX_CMD_WRITE_NODE_ID, 0, 0, ETH_ALEN,
  917. data->mac_addr, 0);
  918. if (ret < 0)
  919. return ret;
  920. ret = asix_write_rx_ctl(dev, AX_DEFAULT_RX_CTL, 0);
  921. if (ret < 0)
  922. return ret;
  923. return 0;
  924. }
  925. static int ax88178_link_reset(struct usbnet *dev)
  926. {
  927. u16 mode;
  928. struct ethtool_cmd ecmd = { .cmd = ETHTOOL_GSET };
  929. struct asix_data *data = (struct asix_data *)&dev->data;
  930. u32 speed;
  931. netdev_dbg(dev->net, "ax88178_link_reset()\n");
  932. mii_check_media(&dev->mii, 1, 1);
  933. mii_ethtool_gset(&dev->mii, &ecmd);
  934. mode = AX88178_MEDIUM_DEFAULT;
  935. speed = ethtool_cmd_speed(&ecmd);
  936. if (speed == SPEED_1000)
  937. mode |= AX_MEDIUM_GM;
  938. else if (speed == SPEED_100)
  939. mode |= AX_MEDIUM_PS;
  940. else
  941. mode &= ~(AX_MEDIUM_PS | AX_MEDIUM_GM);
  942. mode |= AX_MEDIUM_ENCK;
  943. if (ecmd.duplex == DUPLEX_FULL)
  944. mode |= AX_MEDIUM_FD;
  945. else
  946. mode &= ~AX_MEDIUM_FD;
  947. netdev_dbg(dev->net, "ax88178_link_reset() speed: %u duplex: %d setting mode to 0x%04x\n",
  948. speed, ecmd.duplex, mode);
  949. asix_write_medium_mode(dev, mode, 0);
  950. if (data->phymode == PHY_MODE_MARVELL && data->ledmode)
  951. marvell_led_status(dev, speed);
  952. return 0;
  953. }
  954. static void ax88178_set_mfb(struct usbnet *dev)
  955. {
  956. u16 mfb = AX_RX_CTL_MFB_16384;
  957. u16 rxctl;
  958. u16 medium;
  959. int old_rx_urb_size = dev->rx_urb_size;
  960. if (dev->hard_mtu < 2048) {
  961. dev->rx_urb_size = 2048;
  962. mfb = AX_RX_CTL_MFB_2048;
  963. } else if (dev->hard_mtu < 4096) {
  964. dev->rx_urb_size = 4096;
  965. mfb = AX_RX_CTL_MFB_4096;
  966. } else if (dev->hard_mtu < 8192) {
  967. dev->rx_urb_size = 8192;
  968. mfb = AX_RX_CTL_MFB_8192;
  969. } else if (dev->hard_mtu < 16384) {
  970. dev->rx_urb_size = 16384;
  971. mfb = AX_RX_CTL_MFB_16384;
  972. }
  973. rxctl = asix_read_rx_ctl(dev, 0);
  974. asix_write_rx_ctl(dev, (rxctl & ~AX_RX_CTL_MFB_16384) | mfb, 0);
  975. medium = asix_read_medium_status(dev, 0);
  976. if (dev->net->mtu > 1500)
  977. medium |= AX_MEDIUM_JFE;
  978. else
  979. medium &= ~AX_MEDIUM_JFE;
  980. asix_write_medium_mode(dev, medium, 0);
  981. if (dev->rx_urb_size > old_rx_urb_size)
  982. usbnet_unlink_rx_urbs(dev);
  983. }
  984. static int ax88178_change_mtu(struct net_device *net, int new_mtu)
  985. {
  986. struct usbnet *dev = netdev_priv(net);
  987. int ll_mtu = new_mtu + net->hard_header_len + 4;
  988. netdev_dbg(dev->net, "ax88178_change_mtu() new_mtu=%d\n", new_mtu);
  989. if ((ll_mtu % dev->maxpacket) == 0)
  990. return -EDOM;
  991. net->mtu = new_mtu;
  992. dev->hard_mtu = net->mtu + net->hard_header_len;
  993. ax88178_set_mfb(dev);
  994. /* max qlen depend on hard_mtu and rx_urb_size */
  995. usbnet_update_max_qlen(dev);
  996. return 0;
  997. }
  998. static const struct net_device_ops ax88178_netdev_ops = {
  999. .ndo_open = usbnet_open,
  1000. .ndo_stop = usbnet_stop,
  1001. .ndo_start_xmit = usbnet_start_xmit,
  1002. .ndo_tx_timeout = usbnet_tx_timeout,
  1003. .ndo_get_stats64 = dev_get_tstats64,
  1004. .ndo_set_mac_address = asix_set_mac_address,
  1005. .ndo_validate_addr = eth_validate_addr,
  1006. .ndo_set_rx_mode = asix_set_multicast,
  1007. .ndo_eth_ioctl = asix_ioctl,
  1008. .ndo_change_mtu = ax88178_change_mtu,
  1009. };
  1010. static int ax88178_bind(struct usbnet *dev, struct usb_interface *intf)
  1011. {
  1012. int ret;
  1013. u8 buf[ETH_ALEN] = {0};
  1014. usbnet_get_endpoints(dev,intf);
  1015. /* Get the MAC address */
  1016. ret = asix_read_cmd(dev, AX_CMD_READ_NODE_ID, 0, 0, ETH_ALEN, buf, 0);
  1017. if (ret < 0) {
  1018. netdev_dbg(dev->net, "Failed to read MAC address: %d\n", ret);
  1019. return ret;
  1020. }
  1021. asix_set_netdev_dev_addr(dev, buf);
  1022. /* Initialize MII structure */
  1023. dev->mii.dev = dev->net;
  1024. dev->mii.mdio_read = asix_mdio_read;
  1025. dev->mii.mdio_write = asix_mdio_write;
  1026. dev->mii.phy_id_mask = 0x1f;
  1027. dev->mii.reg_num_mask = 0xff;
  1028. dev->mii.supports_gmii = 1;
  1029. dev->mii.phy_id = asix_read_phy_addr(dev, true);
  1030. if (dev->mii.phy_id < 0)
  1031. return dev->mii.phy_id;
  1032. dev->net->netdev_ops = &ax88178_netdev_ops;
  1033. dev->net->ethtool_ops = &ax88178_ethtool_ops;
  1034. dev->net->max_mtu = 16384 - (dev->net->hard_header_len + 4);
  1035. /* Blink LEDS so users know driver saw dongle */
  1036. asix_sw_reset(dev, 0, 0);
  1037. msleep(150);
  1038. asix_sw_reset(dev, AX_SWRESET_PRL | AX_SWRESET_IPPD, 0);
  1039. msleep(150);
  1040. /* Asix framing packs multiple eth frames into a 2K usb bulk transfer */
  1041. if (dev->driver_info->flags & FLAG_FRAMING_AX) {
  1042. /* hard_mtu is still the default - the device does not support
  1043. jumbo eth frames */
  1044. dev->rx_urb_size = 2048;
  1045. }
  1046. dev->driver_priv = kzalloc(sizeof(struct asix_common_private), GFP_KERNEL);
  1047. if (!dev->driver_priv)
  1048. return -ENOMEM;
  1049. return 0;
  1050. }
  1051. static const struct driver_info ax8817x_info = {
  1052. .description = "ASIX AX8817x USB 2.0 Ethernet",
  1053. .bind = ax88172_bind,
  1054. .status = asix_status,
  1055. .link_reset = ax88172_link_reset,
  1056. .reset = ax88172_link_reset,
  1057. .flags = FLAG_ETHER | FLAG_LINK_INTR,
  1058. .data = 0x00130103,
  1059. };
  1060. static const struct driver_info dlink_dub_e100_info = {
  1061. .description = "DLink DUB-E100 USB Ethernet",
  1062. .bind = ax88172_bind,
  1063. .status = asix_status,
  1064. .link_reset = ax88172_link_reset,
  1065. .reset = ax88172_link_reset,
  1066. .flags = FLAG_ETHER | FLAG_LINK_INTR,
  1067. .data = 0x009f9d9f,
  1068. };
  1069. static const struct driver_info netgear_fa120_info = {
  1070. .description = "Netgear FA-120 USB Ethernet",
  1071. .bind = ax88172_bind,
  1072. .status = asix_status,
  1073. .link_reset = ax88172_link_reset,
  1074. .reset = ax88172_link_reset,
  1075. .flags = FLAG_ETHER | FLAG_LINK_INTR,
  1076. .data = 0x00130103,
  1077. };
  1078. static const struct driver_info hawking_uf200_info = {
  1079. .description = "Hawking UF200 USB Ethernet",
  1080. .bind = ax88172_bind,
  1081. .status = asix_status,
  1082. .link_reset = ax88172_link_reset,
  1083. .reset = ax88172_link_reset,
  1084. .flags = FLAG_ETHER | FLAG_LINK_INTR,
  1085. .data = 0x001f1d1f,
  1086. };
  1087. static const struct driver_info ax88772_info = {
  1088. .description = "ASIX AX88772 USB 2.0 Ethernet",
  1089. .bind = ax88772_bind,
  1090. .unbind = ax88772_unbind,
  1091. .status = asix_status,
  1092. .reset = ax88772_reset,
  1093. .stop = ax88772_stop,
  1094. .flags = FLAG_ETHER | FLAG_FRAMING_AX | FLAG_LINK_INTR | FLAG_MULTI_PACKET,
  1095. .rx_fixup = asix_rx_fixup_common,
  1096. .tx_fixup = asix_tx_fixup,
  1097. };
  1098. static const struct driver_info ax88772b_info = {
  1099. .description = "ASIX AX88772B USB 2.0 Ethernet",
  1100. .bind = ax88772_bind,
  1101. .unbind = ax88772_unbind,
  1102. .status = asix_status,
  1103. .reset = ax88772_reset,
  1104. .stop = ax88772_stop,
  1105. .flags = FLAG_ETHER | FLAG_FRAMING_AX | FLAG_LINK_INTR |
  1106. FLAG_MULTI_PACKET,
  1107. .rx_fixup = asix_rx_fixup_common,
  1108. .tx_fixup = asix_tx_fixup,
  1109. .data = FLAG_EEPROM_MAC,
  1110. };
  1111. static const struct driver_info ax88178_info = {
  1112. .description = "ASIX AX88178 USB 2.0 Ethernet",
  1113. .bind = ax88178_bind,
  1114. .unbind = ax88178_unbind,
  1115. .status = asix_status,
  1116. .link_reset = ax88178_link_reset,
  1117. .reset = ax88178_reset,
  1118. .flags = FLAG_ETHER | FLAG_FRAMING_AX | FLAG_LINK_INTR |
  1119. FLAG_MULTI_PACKET,
  1120. .rx_fixup = asix_rx_fixup_common,
  1121. .tx_fixup = asix_tx_fixup,
  1122. };
  1123. /*
  1124. * USBLINK 20F9 "USB 2.0 LAN" USB ethernet adapter, typically found in
  1125. * no-name packaging.
  1126. * USB device strings are:
  1127. * 1: Manufacturer: USBLINK
  1128. * 2: Product: HG20F9 USB2.0
  1129. * 3: Serial: 000003
  1130. * Appears to be compatible with Asix 88772B.
  1131. */
  1132. static const struct driver_info hg20f9_info = {
  1133. .description = "HG20F9 USB 2.0 Ethernet",
  1134. .bind = ax88772_bind,
  1135. .unbind = ax88772_unbind,
  1136. .status = asix_status,
  1137. .reset = ax88772_reset,
  1138. .flags = FLAG_ETHER | FLAG_FRAMING_AX | FLAG_LINK_INTR |
  1139. FLAG_MULTI_PACKET,
  1140. .rx_fixup = asix_rx_fixup_common,
  1141. .tx_fixup = asix_tx_fixup,
  1142. .data = FLAG_EEPROM_MAC,
  1143. };
  1144. static const struct usb_device_id products [] = {
  1145. {
  1146. // Linksys USB200M
  1147. USB_DEVICE (0x077b, 0x2226),
  1148. .driver_info = (unsigned long) &ax8817x_info,
  1149. }, {
  1150. // Netgear FA120
  1151. USB_DEVICE (0x0846, 0x1040),
  1152. .driver_info = (unsigned long) &netgear_fa120_info,
  1153. }, {
  1154. // DLink DUB-E100
  1155. USB_DEVICE (0x2001, 0x1a00),
  1156. .driver_info = (unsigned long) &dlink_dub_e100_info,
  1157. }, {
  1158. // Intellinet, ST Lab USB Ethernet
  1159. USB_DEVICE (0x0b95, 0x1720),
  1160. .driver_info = (unsigned long) &ax8817x_info,
  1161. }, {
  1162. // Hawking UF200, TrendNet TU2-ET100
  1163. USB_DEVICE (0x07b8, 0x420a),
  1164. .driver_info = (unsigned long) &hawking_uf200_info,
  1165. }, {
  1166. // Billionton Systems, USB2AR
  1167. USB_DEVICE (0x08dd, 0x90ff),
  1168. .driver_info = (unsigned long) &ax8817x_info,
  1169. }, {
  1170. // Billionton Systems, GUSB2AM-1G-B
  1171. USB_DEVICE(0x08dd, 0x0114),
  1172. .driver_info = (unsigned long) &ax88178_info,
  1173. }, {
  1174. // ATEN UC210T
  1175. USB_DEVICE (0x0557, 0x2009),
  1176. .driver_info = (unsigned long) &ax8817x_info,
  1177. }, {
  1178. // Buffalo LUA-U2-KTX
  1179. USB_DEVICE (0x0411, 0x003d),
  1180. .driver_info = (unsigned long) &ax8817x_info,
  1181. }, {
  1182. // Buffalo LUA-U2-GT 10/100/1000
  1183. USB_DEVICE (0x0411, 0x006e),
  1184. .driver_info = (unsigned long) &ax88178_info,
  1185. }, {
  1186. // Sitecom LN-029 "USB 2.0 10/100 Ethernet adapter"
  1187. USB_DEVICE (0x6189, 0x182d),
  1188. .driver_info = (unsigned long) &ax8817x_info,
  1189. }, {
  1190. // Sitecom LN-031 "USB 2.0 10/100/1000 Ethernet adapter"
  1191. USB_DEVICE (0x0df6, 0x0056),
  1192. .driver_info = (unsigned long) &ax88178_info,
  1193. }, {
  1194. // Sitecom LN-028 "USB 2.0 10/100/1000 Ethernet adapter"
  1195. USB_DEVICE (0x0df6, 0x061c),
  1196. .driver_info = (unsigned long) &ax88178_info,
  1197. }, {
  1198. // corega FEther USB2-TX
  1199. USB_DEVICE (0x07aa, 0x0017),
  1200. .driver_info = (unsigned long) &ax8817x_info,
  1201. }, {
  1202. // Surecom EP-1427X-2
  1203. USB_DEVICE (0x1189, 0x0893),
  1204. .driver_info = (unsigned long) &ax8817x_info,
  1205. }, {
  1206. // goodway corp usb gwusb2e
  1207. USB_DEVICE (0x1631, 0x6200),
  1208. .driver_info = (unsigned long) &ax8817x_info,
  1209. }, {
  1210. // JVC MP-PRX1 Port Replicator
  1211. USB_DEVICE (0x04f1, 0x3008),
  1212. .driver_info = (unsigned long) &ax8817x_info,
  1213. }, {
  1214. // Lenovo U2L100P 10/100
  1215. USB_DEVICE (0x17ef, 0x7203),
  1216. .driver_info = (unsigned long)&ax88772b_info,
  1217. }, {
  1218. // ASIX AX88772B 10/100
  1219. USB_DEVICE (0x0b95, 0x772b),
  1220. .driver_info = (unsigned long) &ax88772b_info,
  1221. }, {
  1222. // ASIX AX88772 10/100
  1223. USB_DEVICE (0x0b95, 0x7720),
  1224. .driver_info = (unsigned long) &ax88772_info,
  1225. }, {
  1226. // ASIX AX88178 10/100/1000
  1227. USB_DEVICE (0x0b95, 0x1780),
  1228. .driver_info = (unsigned long) &ax88178_info,
  1229. }, {
  1230. // Logitec LAN-GTJ/U2A
  1231. USB_DEVICE (0x0789, 0x0160),
  1232. .driver_info = (unsigned long) &ax88178_info,
  1233. }, {
  1234. // Linksys USB200M Rev 2
  1235. USB_DEVICE (0x13b1, 0x0018),
  1236. .driver_info = (unsigned long) &ax88772_info,
  1237. }, {
  1238. // 0Q0 cable ethernet
  1239. USB_DEVICE (0x1557, 0x7720),
  1240. .driver_info = (unsigned long) &ax88772_info,
  1241. }, {
  1242. // DLink DUB-E100 H/W Ver B1
  1243. USB_DEVICE (0x07d1, 0x3c05),
  1244. .driver_info = (unsigned long) &ax88772_info,
  1245. }, {
  1246. // DLink DUB-E100 H/W Ver B1 Alternate
  1247. USB_DEVICE (0x2001, 0x3c05),
  1248. .driver_info = (unsigned long) &ax88772_info,
  1249. }, {
  1250. // DLink DUB-E100 H/W Ver C1
  1251. USB_DEVICE (0x2001, 0x1a02),
  1252. .driver_info = (unsigned long) &ax88772_info,
  1253. }, {
  1254. // Linksys USB1000
  1255. USB_DEVICE (0x1737, 0x0039),
  1256. .driver_info = (unsigned long) &ax88178_info,
  1257. }, {
  1258. // IO-DATA ETG-US2
  1259. USB_DEVICE (0x04bb, 0x0930),
  1260. .driver_info = (unsigned long) &ax88178_info,
  1261. }, {
  1262. // Belkin F5D5055
  1263. USB_DEVICE(0x050d, 0x5055),
  1264. .driver_info = (unsigned long) &ax88178_info,
  1265. }, {
  1266. // Apple USB Ethernet Adapter
  1267. USB_DEVICE(0x05ac, 0x1402),
  1268. .driver_info = (unsigned long) &ax88772_info,
  1269. }, {
  1270. // Cables-to-Go USB Ethernet Adapter
  1271. USB_DEVICE(0x0b95, 0x772a),
  1272. .driver_info = (unsigned long) &ax88772_info,
  1273. }, {
  1274. // ABOCOM for pci
  1275. USB_DEVICE(0x14ea, 0xab11),
  1276. .driver_info = (unsigned long) &ax88178_info,
  1277. }, {
  1278. // ASIX 88772a
  1279. USB_DEVICE(0x0db0, 0xa877),
  1280. .driver_info = (unsigned long) &ax88772_info,
  1281. }, {
  1282. // Asus USB Ethernet Adapter
  1283. USB_DEVICE (0x0b95, 0x7e2b),
  1284. .driver_info = (unsigned long)&ax88772b_info,
  1285. }, {
  1286. /* ASIX 88172a demo board */
  1287. USB_DEVICE(0x0b95, 0x172a),
  1288. .driver_info = (unsigned long) &ax88172a_info,
  1289. }, {
  1290. /*
  1291. * USBLINK HG20F9 "USB 2.0 LAN"
  1292. * Appears to have gazumped Linksys's manufacturer ID but
  1293. * doesn't (yet) conflict with any known Linksys product.
  1294. */
  1295. USB_DEVICE(0x066b, 0x20f9),
  1296. .driver_info = (unsigned long) &hg20f9_info,
  1297. },
  1298. { }, // END
  1299. };
  1300. MODULE_DEVICE_TABLE(usb, products);
  1301. static struct usb_driver asix_driver = {
  1302. .name = DRIVER_NAME,
  1303. .id_table = products,
  1304. .probe = usbnet_probe,
  1305. .suspend = asix_suspend,
  1306. .resume = asix_resume,
  1307. .reset_resume = asix_resume,
  1308. .disconnect = usbnet_disconnect,
  1309. .supports_autosuspend = 1,
  1310. .disable_hub_initiated_lpm = 1,
  1311. };
  1312. module_usb_driver(asix_driver);
  1313. MODULE_AUTHOR("David Hollis");
  1314. MODULE_VERSION(DRIVER_VERSION);
  1315. MODULE_DESCRIPTION("ASIX AX8817X based USB 2.0 Ethernet Devices");
  1316. MODULE_LICENSE("GPL");