asix_devices.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047
  1. /*
  2. * ASIX AX8817X based USB 2.0 Ethernet Devices
  3. * Copyright (C) 2003-2006 David Hollis <dhollis@davehollis.com>
  4. * Copyright (C) 2005 Phil Chang <pchang23@sbcglobal.net>
  5. * Copyright (C) 2006 James Painter <jamie.painter@iname.com>
  6. * Copyright (c) 2002-2003 TiVo Inc.
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  21. */
  22. #include "asix.h"
  23. #define PHY_MODE_MARVELL 0x0000
  24. #define MII_MARVELL_LED_CTRL 0x0018
  25. #define MII_MARVELL_STATUS 0x001b
  26. #define MII_MARVELL_CTRL 0x0014
  27. #define MARVELL_LED_MANUAL 0x0019
  28. #define MARVELL_STATUS_HWCFG 0x0004
  29. #define MARVELL_CTRL_TXDELAY 0x0002
  30. #define MARVELL_CTRL_RXDELAY 0x0080
  31. #define PHY_MODE_RTL8211CL 0x000C
  32. struct ax88172_int_data {
  33. __le16 res1;
  34. u8 link;
  35. __le16 res2;
  36. u8 status;
  37. __le16 res3;
  38. } __packed;
  39. static void asix_status(struct usbnet *dev, struct urb *urb)
  40. {
  41. struct ax88172_int_data *event;
  42. int link;
  43. if (urb->actual_length < 8)
  44. return;
  45. event = urb->transfer_buffer;
  46. link = event->link & 0x01;
  47. if (netif_carrier_ok(dev->net) != link) {
  48. if (link) {
  49. netif_carrier_on(dev->net);
  50. usbnet_defer_kevent (dev, EVENT_LINK_RESET );
  51. } else
  52. netif_carrier_off(dev->net);
  53. netdev_dbg(dev->net, "Link Status is: %d\n", link);
  54. }
  55. }
  56. static void asix_set_netdev_dev_addr(struct usbnet *dev, u8 *addr)
  57. {
  58. if (is_valid_ether_addr(addr)) {
  59. memcpy(dev->net->dev_addr, addr, ETH_ALEN);
  60. } else {
  61. netdev_info(dev->net, "invalid hw address, using random\n");
  62. eth_hw_addr_random(dev->net);
  63. }
  64. }
  65. /* Get the PHY Identifier from the PHYSID1 & PHYSID2 MII registers */
  66. static u32 asix_get_phyid(struct usbnet *dev)
  67. {
  68. int phy_reg;
  69. u32 phy_id;
  70. int i;
  71. /* Poll for the rare case the FW or phy isn't ready yet. */
  72. for (i = 0; i < 100; i++) {
  73. phy_reg = asix_mdio_read(dev->net, dev->mii.phy_id, MII_PHYSID1);
  74. if (phy_reg != 0 && phy_reg != 0xFFFF)
  75. break;
  76. mdelay(1);
  77. }
  78. if (phy_reg <= 0 || phy_reg == 0xFFFF)
  79. return 0;
  80. phy_id = (phy_reg & 0xffff) << 16;
  81. phy_reg = asix_mdio_read(dev->net, dev->mii.phy_id, MII_PHYSID2);
  82. if (phy_reg < 0)
  83. return 0;
  84. phy_id |= (phy_reg & 0xffff);
  85. return phy_id;
  86. }
  87. static u32 asix_get_link(struct net_device *net)
  88. {
  89. struct usbnet *dev = netdev_priv(net);
  90. return mii_link_ok(&dev->mii);
  91. }
  92. static int asix_ioctl (struct net_device *net, struct ifreq *rq, int cmd)
  93. {
  94. struct usbnet *dev = netdev_priv(net);
  95. return generic_mii_ioctl(&dev->mii, if_mii(rq), cmd, NULL);
  96. }
  97. /* We need to override some ethtool_ops so we require our
  98. own structure so we don't interfere with other usbnet
  99. devices that may be connected at the same time. */
  100. static const struct ethtool_ops ax88172_ethtool_ops = {
  101. .get_drvinfo = asix_get_drvinfo,
  102. .get_link = asix_get_link,
  103. .get_msglevel = usbnet_get_msglevel,
  104. .set_msglevel = usbnet_set_msglevel,
  105. .get_wol = asix_get_wol,
  106. .set_wol = asix_set_wol,
  107. .get_eeprom_len = asix_get_eeprom_len,
  108. .get_eeprom = asix_get_eeprom,
  109. .set_eeprom = asix_set_eeprom,
  110. .get_settings = usbnet_get_settings,
  111. .set_settings = usbnet_set_settings,
  112. .nway_reset = usbnet_nway_reset,
  113. };
  114. static void ax88172_set_multicast(struct net_device *net)
  115. {
  116. struct usbnet *dev = netdev_priv(net);
  117. struct asix_data *data = (struct asix_data *)&dev->data;
  118. u8 rx_ctl = 0x8c;
  119. if (net->flags & IFF_PROMISC) {
  120. rx_ctl |= 0x01;
  121. } else if (net->flags & IFF_ALLMULTI ||
  122. netdev_mc_count(net) > AX_MAX_MCAST) {
  123. rx_ctl |= 0x02;
  124. } else if (netdev_mc_empty(net)) {
  125. /* just broadcast and directed */
  126. } else {
  127. /* We use the 20 byte dev->data
  128. * for our 8 byte filter buffer
  129. * to avoid allocating memory that
  130. * is tricky to free later */
  131. struct netdev_hw_addr *ha;
  132. u32 crc_bits;
  133. memset(data->multi_filter, 0, AX_MCAST_FILTER_SIZE);
  134. /* Build the multicast hash filter. */
  135. netdev_for_each_mc_addr(ha, net) {
  136. crc_bits = ether_crc(ETH_ALEN, ha->addr) >> 26;
  137. data->multi_filter[crc_bits >> 3] |=
  138. 1 << (crc_bits & 7);
  139. }
  140. asix_write_cmd_async(dev, AX_CMD_WRITE_MULTI_FILTER, 0, 0,
  141. AX_MCAST_FILTER_SIZE, data->multi_filter);
  142. rx_ctl |= 0x10;
  143. }
  144. asix_write_cmd_async(dev, AX_CMD_WRITE_RX_CTL, rx_ctl, 0, 0, NULL);
  145. }
  146. static int ax88172_link_reset(struct usbnet *dev)
  147. {
  148. u8 mode;
  149. struct ethtool_cmd ecmd = { .cmd = ETHTOOL_GSET };
  150. mii_check_media(&dev->mii, 1, 1);
  151. mii_ethtool_gset(&dev->mii, &ecmd);
  152. mode = AX88172_MEDIUM_DEFAULT;
  153. if (ecmd.duplex != DUPLEX_FULL)
  154. mode |= ~AX88172_MEDIUM_FD;
  155. netdev_dbg(dev->net, "ax88172_link_reset() speed: %u duplex: %d setting mode to 0x%04x\n",
  156. ethtool_cmd_speed(&ecmd), ecmd.duplex, mode);
  157. asix_write_medium_mode(dev, mode);
  158. return 0;
  159. }
  160. static const struct net_device_ops ax88172_netdev_ops = {
  161. .ndo_open = usbnet_open,
  162. .ndo_stop = usbnet_stop,
  163. .ndo_start_xmit = usbnet_start_xmit,
  164. .ndo_tx_timeout = usbnet_tx_timeout,
  165. .ndo_change_mtu = usbnet_change_mtu,
  166. .ndo_set_mac_address = eth_mac_addr,
  167. .ndo_validate_addr = eth_validate_addr,
  168. .ndo_do_ioctl = asix_ioctl,
  169. .ndo_set_rx_mode = ax88172_set_multicast,
  170. };
  171. static int ax88172_bind(struct usbnet *dev, struct usb_interface *intf)
  172. {
  173. int ret = 0;
  174. u8 buf[ETH_ALEN];
  175. int i;
  176. unsigned long gpio_bits = dev->driver_info->data;
  177. usbnet_get_endpoints(dev,intf);
  178. /* Toggle the GPIOs in a manufacturer/model specific way */
  179. for (i = 2; i >= 0; i--) {
  180. ret = asix_write_cmd(dev, AX_CMD_WRITE_GPIOS,
  181. (gpio_bits >> (i * 8)) & 0xff, 0, 0, NULL);
  182. if (ret < 0)
  183. goto out;
  184. msleep(5);
  185. }
  186. ret = asix_write_rx_ctl(dev, 0x80);
  187. if (ret < 0)
  188. goto out;
  189. /* Get the MAC address */
  190. ret = asix_read_cmd(dev, AX88172_CMD_READ_NODE_ID, 0, 0, ETH_ALEN, buf);
  191. if (ret < 0) {
  192. netdev_dbg(dev->net, "read AX_CMD_READ_NODE_ID failed: %d\n",
  193. ret);
  194. goto out;
  195. }
  196. asix_set_netdev_dev_addr(dev, buf);
  197. /* Initialize MII structure */
  198. dev->mii.dev = dev->net;
  199. dev->mii.mdio_read = asix_mdio_read;
  200. dev->mii.mdio_write = asix_mdio_write;
  201. dev->mii.phy_id_mask = 0x3f;
  202. dev->mii.reg_num_mask = 0x1f;
  203. dev->mii.phy_id = asix_get_phy_addr(dev);
  204. dev->net->netdev_ops = &ax88172_netdev_ops;
  205. dev->net->ethtool_ops = &ax88172_ethtool_ops;
  206. dev->net->needed_headroom = 4; /* cf asix_tx_fixup() */
  207. dev->net->needed_tailroom = 4; /* cf asix_tx_fixup() */
  208. asix_mdio_write(dev->net, dev->mii.phy_id, MII_BMCR, BMCR_RESET);
  209. asix_mdio_write(dev->net, dev->mii.phy_id, MII_ADVERTISE,
  210. ADVERTISE_ALL | ADVERTISE_CSMA | ADVERTISE_PAUSE_CAP);
  211. mii_nway_restart(&dev->mii);
  212. return 0;
  213. out:
  214. return ret;
  215. }
  216. static const struct ethtool_ops ax88772_ethtool_ops = {
  217. .get_drvinfo = asix_get_drvinfo,
  218. .get_link = asix_get_link,
  219. .get_msglevel = usbnet_get_msglevel,
  220. .set_msglevel = usbnet_set_msglevel,
  221. .get_wol = asix_get_wol,
  222. .set_wol = asix_set_wol,
  223. .get_eeprom_len = asix_get_eeprom_len,
  224. .get_eeprom = asix_get_eeprom,
  225. .set_eeprom = asix_set_eeprom,
  226. .get_settings = usbnet_get_settings,
  227. .set_settings = usbnet_set_settings,
  228. .nway_reset = usbnet_nway_reset,
  229. };
  230. static int ax88772_link_reset(struct usbnet *dev)
  231. {
  232. u16 mode;
  233. struct ethtool_cmd ecmd = { .cmd = ETHTOOL_GSET };
  234. mii_check_media(&dev->mii, 1, 1);
  235. mii_ethtool_gset(&dev->mii, &ecmd);
  236. mode = AX88772_MEDIUM_DEFAULT;
  237. if (ethtool_cmd_speed(&ecmd) != SPEED_100)
  238. mode &= ~AX_MEDIUM_PS;
  239. if (ecmd.duplex != DUPLEX_FULL)
  240. mode &= ~AX_MEDIUM_FD;
  241. netdev_dbg(dev->net, "ax88772_link_reset() speed: %u duplex: %d setting mode to 0x%04x\n",
  242. ethtool_cmd_speed(&ecmd), ecmd.duplex, mode);
  243. asix_write_medium_mode(dev, mode);
  244. return 0;
  245. }
  246. static int ax88772_reset(struct usbnet *dev)
  247. {
  248. struct asix_data *data = (struct asix_data *)&dev->data;
  249. int ret, embd_phy;
  250. u16 rx_ctl;
  251. ret = asix_write_gpio(dev,
  252. AX_GPIO_RSE | AX_GPIO_GPO_2 | AX_GPIO_GPO2EN, 5);
  253. if (ret < 0)
  254. goto out;
  255. embd_phy = ((asix_get_phy_addr(dev) & 0x1f) == 0x10 ? 1 : 0);
  256. ret = asix_write_cmd(dev, AX_CMD_SW_PHY_SELECT, embd_phy, 0, 0, NULL);
  257. if (ret < 0) {
  258. netdev_dbg(dev->net, "Select PHY #1 failed: %d\n", ret);
  259. goto out;
  260. }
  261. ret = asix_sw_reset(dev, AX_SWRESET_IPPD | AX_SWRESET_PRL);
  262. if (ret < 0)
  263. goto out;
  264. msleep(150);
  265. ret = asix_sw_reset(dev, AX_SWRESET_CLEAR);
  266. if (ret < 0)
  267. goto out;
  268. msleep(150);
  269. if (embd_phy) {
  270. ret = asix_sw_reset(dev, AX_SWRESET_IPRL);
  271. if (ret < 0)
  272. goto out;
  273. } else {
  274. ret = asix_sw_reset(dev, AX_SWRESET_PRTE);
  275. if (ret < 0)
  276. goto out;
  277. }
  278. msleep(150);
  279. rx_ctl = asix_read_rx_ctl(dev);
  280. netdev_dbg(dev->net, "RX_CTL is 0x%04x after software reset\n", rx_ctl);
  281. ret = asix_write_rx_ctl(dev, 0x0000);
  282. if (ret < 0)
  283. goto out;
  284. rx_ctl = asix_read_rx_ctl(dev);
  285. netdev_dbg(dev->net, "RX_CTL is 0x%04x setting to 0x0000\n", rx_ctl);
  286. ret = asix_sw_reset(dev, AX_SWRESET_PRL);
  287. if (ret < 0)
  288. goto out;
  289. msleep(150);
  290. ret = asix_sw_reset(dev, AX_SWRESET_IPRL | AX_SWRESET_PRL);
  291. if (ret < 0)
  292. goto out;
  293. msleep(150);
  294. asix_mdio_write(dev->net, dev->mii.phy_id, MII_BMCR, BMCR_RESET);
  295. asix_mdio_write(dev->net, dev->mii.phy_id, MII_ADVERTISE,
  296. ADVERTISE_ALL | ADVERTISE_CSMA);
  297. mii_nway_restart(&dev->mii);
  298. ret = asix_write_medium_mode(dev, AX88772_MEDIUM_DEFAULT);
  299. if (ret < 0)
  300. goto out;
  301. ret = asix_write_cmd(dev, AX_CMD_WRITE_IPG0,
  302. AX88772_IPG0_DEFAULT | AX88772_IPG1_DEFAULT,
  303. AX88772_IPG2_DEFAULT, 0, NULL);
  304. if (ret < 0) {
  305. netdev_dbg(dev->net, "Write IPG,IPG1,IPG2 failed: %d\n", ret);
  306. goto out;
  307. }
  308. /* Rewrite MAC address */
  309. memcpy(data->mac_addr, dev->net->dev_addr, ETH_ALEN);
  310. ret = asix_write_cmd(dev, AX_CMD_WRITE_NODE_ID, 0, 0, ETH_ALEN,
  311. data->mac_addr);
  312. if (ret < 0)
  313. goto out;
  314. /* Set RX_CTL to default values with 2k buffer, and enable cactus */
  315. ret = asix_write_rx_ctl(dev, AX_DEFAULT_RX_CTL);
  316. if (ret < 0)
  317. goto out;
  318. rx_ctl = asix_read_rx_ctl(dev);
  319. netdev_dbg(dev->net, "RX_CTL is 0x%04x after all initializations\n",
  320. rx_ctl);
  321. rx_ctl = asix_read_medium_status(dev);
  322. netdev_dbg(dev->net,
  323. "Medium Status is 0x%04x after all initializations\n",
  324. rx_ctl);
  325. return 0;
  326. out:
  327. return ret;
  328. }
  329. static const struct net_device_ops ax88772_netdev_ops = {
  330. .ndo_open = usbnet_open,
  331. .ndo_stop = usbnet_stop,
  332. .ndo_start_xmit = usbnet_start_xmit,
  333. .ndo_tx_timeout = usbnet_tx_timeout,
  334. .ndo_change_mtu = usbnet_change_mtu,
  335. .ndo_set_mac_address = asix_set_mac_address,
  336. .ndo_validate_addr = eth_validate_addr,
  337. .ndo_do_ioctl = asix_ioctl,
  338. .ndo_set_rx_mode = asix_set_multicast,
  339. };
  340. static int ax88772_bind(struct usbnet *dev, struct usb_interface *intf)
  341. {
  342. int ret, embd_phy;
  343. u8 buf[ETH_ALEN];
  344. u32 phyid;
  345. usbnet_get_endpoints(dev,intf);
  346. /* Get the MAC address */
  347. ret = asix_read_cmd(dev, AX_CMD_READ_NODE_ID, 0, 0, ETH_ALEN, buf);
  348. if (ret < 0) {
  349. netdev_dbg(dev->net, "Failed to read MAC address: %d\n", ret);
  350. return ret;
  351. }
  352. asix_set_netdev_dev_addr(dev, buf);
  353. /* Initialize MII structure */
  354. dev->mii.dev = dev->net;
  355. dev->mii.mdio_read = asix_mdio_read;
  356. dev->mii.mdio_write = asix_mdio_write;
  357. dev->mii.phy_id_mask = 0x1f;
  358. dev->mii.reg_num_mask = 0x1f;
  359. dev->mii.phy_id = asix_get_phy_addr(dev);
  360. dev->net->netdev_ops = &ax88772_netdev_ops;
  361. dev->net->ethtool_ops = &ax88772_ethtool_ops;
  362. dev->net->needed_headroom = 4; /* cf asix_tx_fixup() */
  363. dev->net->needed_tailroom = 4; /* cf asix_tx_fixup() */
  364. embd_phy = ((dev->mii.phy_id & 0x1f) == 0x10 ? 1 : 0);
  365. /* Reset the PHY to normal operation mode */
  366. ret = asix_write_cmd(dev, AX_CMD_SW_PHY_SELECT, embd_phy, 0, 0, NULL);
  367. if (ret < 0) {
  368. netdev_dbg(dev->net, "Select PHY #1 failed: %d\n", ret);
  369. return ret;
  370. }
  371. ret = asix_sw_reset(dev, AX_SWRESET_IPPD | AX_SWRESET_PRL);
  372. if (ret < 0)
  373. return ret;
  374. msleep(150);
  375. ret = asix_sw_reset(dev, AX_SWRESET_CLEAR);
  376. if (ret < 0)
  377. return ret;
  378. msleep(150);
  379. ret = asix_sw_reset(dev, embd_phy ? AX_SWRESET_IPRL : AX_SWRESET_PRTE);
  380. /* Read PHYID register *AFTER* the PHY was reset properly */
  381. phyid = asix_get_phyid(dev);
  382. netdev_dbg(dev->net, "PHYID=0x%08x\n", phyid);
  383. /* Asix framing packs multiple eth frames into a 2K usb bulk transfer */
  384. if (dev->driver_info->flags & FLAG_FRAMING_AX) {
  385. /* hard_mtu is still the default - the device does not support
  386. jumbo eth frames */
  387. dev->rx_urb_size = 2048;
  388. }
  389. return 0;
  390. }
  391. static const struct ethtool_ops ax88178_ethtool_ops = {
  392. .get_drvinfo = asix_get_drvinfo,
  393. .get_link = asix_get_link,
  394. .get_msglevel = usbnet_get_msglevel,
  395. .set_msglevel = usbnet_set_msglevel,
  396. .get_wol = asix_get_wol,
  397. .set_wol = asix_set_wol,
  398. .get_eeprom_len = asix_get_eeprom_len,
  399. .get_eeprom = asix_get_eeprom,
  400. .set_eeprom = asix_set_eeprom,
  401. .get_settings = usbnet_get_settings,
  402. .set_settings = usbnet_set_settings,
  403. .nway_reset = usbnet_nway_reset,
  404. };
  405. static int marvell_phy_init(struct usbnet *dev)
  406. {
  407. struct asix_data *data = (struct asix_data *)&dev->data;
  408. u16 reg;
  409. netdev_dbg(dev->net, "marvell_phy_init()\n");
  410. reg = asix_mdio_read(dev->net, dev->mii.phy_id, MII_MARVELL_STATUS);
  411. netdev_dbg(dev->net, "MII_MARVELL_STATUS = 0x%04x\n", reg);
  412. asix_mdio_write(dev->net, dev->mii.phy_id, MII_MARVELL_CTRL,
  413. MARVELL_CTRL_RXDELAY | MARVELL_CTRL_TXDELAY);
  414. if (data->ledmode) {
  415. reg = asix_mdio_read(dev->net, dev->mii.phy_id,
  416. MII_MARVELL_LED_CTRL);
  417. netdev_dbg(dev->net, "MII_MARVELL_LED_CTRL (1) = 0x%04x\n", reg);
  418. reg &= 0xf8ff;
  419. reg |= (1 + 0x0100);
  420. asix_mdio_write(dev->net, dev->mii.phy_id,
  421. MII_MARVELL_LED_CTRL, reg);
  422. reg = asix_mdio_read(dev->net, dev->mii.phy_id,
  423. MII_MARVELL_LED_CTRL);
  424. netdev_dbg(dev->net, "MII_MARVELL_LED_CTRL (2) = 0x%04x\n", reg);
  425. reg &= 0xfc0f;
  426. }
  427. return 0;
  428. }
  429. static int rtl8211cl_phy_init(struct usbnet *dev)
  430. {
  431. struct asix_data *data = (struct asix_data *)&dev->data;
  432. netdev_dbg(dev->net, "rtl8211cl_phy_init()\n");
  433. asix_mdio_write (dev->net, dev->mii.phy_id, 0x1f, 0x0005);
  434. asix_mdio_write (dev->net, dev->mii.phy_id, 0x0c, 0);
  435. asix_mdio_write (dev->net, dev->mii.phy_id, 0x01,
  436. asix_mdio_read (dev->net, dev->mii.phy_id, 0x01) | 0x0080);
  437. asix_mdio_write (dev->net, dev->mii.phy_id, 0x1f, 0);
  438. if (data->ledmode == 12) {
  439. asix_mdio_write (dev->net, dev->mii.phy_id, 0x1f, 0x0002);
  440. asix_mdio_write (dev->net, dev->mii.phy_id, 0x1a, 0x00cb);
  441. asix_mdio_write (dev->net, dev->mii.phy_id, 0x1f, 0);
  442. }
  443. return 0;
  444. }
  445. static int marvell_led_status(struct usbnet *dev, u16 speed)
  446. {
  447. u16 reg = asix_mdio_read(dev->net, dev->mii.phy_id, MARVELL_LED_MANUAL);
  448. netdev_dbg(dev->net, "marvell_led_status() read 0x%04x\n", reg);
  449. /* Clear out the center LED bits - 0x03F0 */
  450. reg &= 0xfc0f;
  451. switch (speed) {
  452. case SPEED_1000:
  453. reg |= 0x03e0;
  454. break;
  455. case SPEED_100:
  456. reg |= 0x03b0;
  457. break;
  458. default:
  459. reg |= 0x02f0;
  460. }
  461. netdev_dbg(dev->net, "marvell_led_status() writing 0x%04x\n", reg);
  462. asix_mdio_write(dev->net, dev->mii.phy_id, MARVELL_LED_MANUAL, reg);
  463. return 0;
  464. }
  465. static int ax88178_reset(struct usbnet *dev)
  466. {
  467. struct asix_data *data = (struct asix_data *)&dev->data;
  468. int ret;
  469. __le16 eeprom;
  470. u8 status;
  471. int gpio0 = 0;
  472. u32 phyid;
  473. asix_read_cmd(dev, AX_CMD_READ_GPIOS, 0, 0, 1, &status);
  474. netdev_dbg(dev->net, "GPIO Status: 0x%04x\n", status);
  475. asix_write_cmd(dev, AX_CMD_WRITE_ENABLE, 0, 0, 0, NULL);
  476. asix_read_cmd(dev, AX_CMD_READ_EEPROM, 0x0017, 0, 2, &eeprom);
  477. asix_write_cmd(dev, AX_CMD_WRITE_DISABLE, 0, 0, 0, NULL);
  478. netdev_dbg(dev->net, "EEPROM index 0x17 is 0x%04x\n", eeprom);
  479. if (eeprom == cpu_to_le16(0xffff)) {
  480. data->phymode = PHY_MODE_MARVELL;
  481. data->ledmode = 0;
  482. gpio0 = 1;
  483. } else {
  484. data->phymode = le16_to_cpu(eeprom) & 0x7F;
  485. data->ledmode = le16_to_cpu(eeprom) >> 8;
  486. gpio0 = (le16_to_cpu(eeprom) & 0x80) ? 0 : 1;
  487. }
  488. netdev_dbg(dev->net, "GPIO0: %d, PhyMode: %d\n", gpio0, data->phymode);
  489. /* Power up external GigaPHY through AX88178 GPIO pin */
  490. asix_write_gpio(dev, AX_GPIO_RSE | AX_GPIO_GPO_1 | AX_GPIO_GPO1EN, 40);
  491. if ((le16_to_cpu(eeprom) >> 8) != 1) {
  492. asix_write_gpio(dev, 0x003c, 30);
  493. asix_write_gpio(dev, 0x001c, 300);
  494. asix_write_gpio(dev, 0x003c, 30);
  495. } else {
  496. netdev_dbg(dev->net, "gpio phymode == 1 path\n");
  497. asix_write_gpio(dev, AX_GPIO_GPO1EN, 30);
  498. asix_write_gpio(dev, AX_GPIO_GPO1EN | AX_GPIO_GPO_1, 30);
  499. }
  500. /* Read PHYID register *AFTER* powering up PHY */
  501. phyid = asix_get_phyid(dev);
  502. netdev_dbg(dev->net, "PHYID=0x%08x\n", phyid);
  503. /* Set AX88178 to enable MII/GMII/RGMII interface for external PHY */
  504. asix_write_cmd(dev, AX_CMD_SW_PHY_SELECT, 0, 0, 0, NULL);
  505. asix_sw_reset(dev, 0);
  506. msleep(150);
  507. asix_sw_reset(dev, AX_SWRESET_PRL | AX_SWRESET_IPPD);
  508. msleep(150);
  509. asix_write_rx_ctl(dev, 0);
  510. if (data->phymode == PHY_MODE_MARVELL) {
  511. marvell_phy_init(dev);
  512. msleep(60);
  513. } else if (data->phymode == PHY_MODE_RTL8211CL)
  514. rtl8211cl_phy_init(dev);
  515. asix_mdio_write(dev->net, dev->mii.phy_id, MII_BMCR,
  516. BMCR_RESET | BMCR_ANENABLE);
  517. asix_mdio_write(dev->net, dev->mii.phy_id, MII_ADVERTISE,
  518. ADVERTISE_ALL | ADVERTISE_CSMA | ADVERTISE_PAUSE_CAP);
  519. asix_mdio_write(dev->net, dev->mii.phy_id, MII_CTRL1000,
  520. ADVERTISE_1000FULL);
  521. mii_nway_restart(&dev->mii);
  522. ret = asix_write_medium_mode(dev, AX88178_MEDIUM_DEFAULT);
  523. if (ret < 0)
  524. return ret;
  525. /* Rewrite MAC address */
  526. memcpy(data->mac_addr, dev->net->dev_addr, ETH_ALEN);
  527. ret = asix_write_cmd(dev, AX_CMD_WRITE_NODE_ID, 0, 0, ETH_ALEN,
  528. data->mac_addr);
  529. if (ret < 0)
  530. return ret;
  531. ret = asix_write_rx_ctl(dev, AX_DEFAULT_RX_CTL);
  532. if (ret < 0)
  533. return ret;
  534. return 0;
  535. }
  536. static int ax88178_link_reset(struct usbnet *dev)
  537. {
  538. u16 mode;
  539. struct ethtool_cmd ecmd = { .cmd = ETHTOOL_GSET };
  540. struct asix_data *data = (struct asix_data *)&dev->data;
  541. u32 speed;
  542. netdev_dbg(dev->net, "ax88178_link_reset()\n");
  543. mii_check_media(&dev->mii, 1, 1);
  544. mii_ethtool_gset(&dev->mii, &ecmd);
  545. mode = AX88178_MEDIUM_DEFAULT;
  546. speed = ethtool_cmd_speed(&ecmd);
  547. if (speed == SPEED_1000)
  548. mode |= AX_MEDIUM_GM;
  549. else if (speed == SPEED_100)
  550. mode |= AX_MEDIUM_PS;
  551. else
  552. mode &= ~(AX_MEDIUM_PS | AX_MEDIUM_GM);
  553. mode |= AX_MEDIUM_ENCK;
  554. if (ecmd.duplex == DUPLEX_FULL)
  555. mode |= AX_MEDIUM_FD;
  556. else
  557. mode &= ~AX_MEDIUM_FD;
  558. netdev_dbg(dev->net, "ax88178_link_reset() speed: %u duplex: %d setting mode to 0x%04x\n",
  559. speed, ecmd.duplex, mode);
  560. asix_write_medium_mode(dev, mode);
  561. if (data->phymode == PHY_MODE_MARVELL && data->ledmode)
  562. marvell_led_status(dev, speed);
  563. return 0;
  564. }
  565. static void ax88178_set_mfb(struct usbnet *dev)
  566. {
  567. u16 mfb = AX_RX_CTL_MFB_16384;
  568. u16 rxctl;
  569. u16 medium;
  570. int old_rx_urb_size = dev->rx_urb_size;
  571. if (dev->hard_mtu < 2048) {
  572. dev->rx_urb_size = 2048;
  573. mfb = AX_RX_CTL_MFB_2048;
  574. } else if (dev->hard_mtu < 4096) {
  575. dev->rx_urb_size = 4096;
  576. mfb = AX_RX_CTL_MFB_4096;
  577. } else if (dev->hard_mtu < 8192) {
  578. dev->rx_urb_size = 8192;
  579. mfb = AX_RX_CTL_MFB_8192;
  580. } else if (dev->hard_mtu < 16384) {
  581. dev->rx_urb_size = 16384;
  582. mfb = AX_RX_CTL_MFB_16384;
  583. }
  584. rxctl = asix_read_rx_ctl(dev);
  585. asix_write_rx_ctl(dev, (rxctl & ~AX_RX_CTL_MFB_16384) | mfb);
  586. medium = asix_read_medium_status(dev);
  587. if (dev->net->mtu > 1500)
  588. medium |= AX_MEDIUM_JFE;
  589. else
  590. medium &= ~AX_MEDIUM_JFE;
  591. asix_write_medium_mode(dev, medium);
  592. if (dev->rx_urb_size > old_rx_urb_size)
  593. usbnet_unlink_rx_urbs(dev);
  594. }
  595. static int ax88178_change_mtu(struct net_device *net, int new_mtu)
  596. {
  597. struct usbnet *dev = netdev_priv(net);
  598. int ll_mtu = new_mtu + net->hard_header_len + 4;
  599. netdev_dbg(dev->net, "ax88178_change_mtu() new_mtu=%d\n", new_mtu);
  600. if (new_mtu <= 0 || ll_mtu > 16384)
  601. return -EINVAL;
  602. if ((ll_mtu % dev->maxpacket) == 0)
  603. return -EDOM;
  604. net->mtu = new_mtu;
  605. dev->hard_mtu = net->mtu + net->hard_header_len;
  606. ax88178_set_mfb(dev);
  607. return 0;
  608. }
  609. static const struct net_device_ops ax88178_netdev_ops = {
  610. .ndo_open = usbnet_open,
  611. .ndo_stop = usbnet_stop,
  612. .ndo_start_xmit = usbnet_start_xmit,
  613. .ndo_tx_timeout = usbnet_tx_timeout,
  614. .ndo_set_mac_address = asix_set_mac_address,
  615. .ndo_validate_addr = eth_validate_addr,
  616. .ndo_set_rx_mode = asix_set_multicast,
  617. .ndo_do_ioctl = asix_ioctl,
  618. .ndo_change_mtu = ax88178_change_mtu,
  619. };
  620. static int ax88178_bind(struct usbnet *dev, struct usb_interface *intf)
  621. {
  622. int ret;
  623. u8 buf[ETH_ALEN];
  624. usbnet_get_endpoints(dev,intf);
  625. /* Get the MAC address */
  626. ret = asix_read_cmd(dev, AX_CMD_READ_NODE_ID, 0, 0, ETH_ALEN, buf);
  627. if (ret < 0) {
  628. netdev_dbg(dev->net, "Failed to read MAC address: %d\n", ret);
  629. return ret;
  630. }
  631. asix_set_netdev_dev_addr(dev, buf);
  632. /* Initialize MII structure */
  633. dev->mii.dev = dev->net;
  634. dev->mii.mdio_read = asix_mdio_read;
  635. dev->mii.mdio_write = asix_mdio_write;
  636. dev->mii.phy_id_mask = 0x1f;
  637. dev->mii.reg_num_mask = 0xff;
  638. dev->mii.supports_gmii = 1;
  639. dev->mii.phy_id = asix_get_phy_addr(dev);
  640. dev->net->netdev_ops = &ax88178_netdev_ops;
  641. dev->net->ethtool_ops = &ax88178_ethtool_ops;
  642. /* Blink LEDS so users know driver saw dongle */
  643. asix_sw_reset(dev, 0);
  644. msleep(150);
  645. asix_sw_reset(dev, AX_SWRESET_PRL | AX_SWRESET_IPPD);
  646. msleep(150);
  647. /* Asix framing packs multiple eth frames into a 2K usb bulk transfer */
  648. if (dev->driver_info->flags & FLAG_FRAMING_AX) {
  649. /* hard_mtu is still the default - the device does not support
  650. jumbo eth frames */
  651. dev->rx_urb_size = 2048;
  652. }
  653. return 0;
  654. }
  655. static const struct driver_info ax8817x_info = {
  656. .description = "ASIX AX8817x USB 2.0 Ethernet",
  657. .bind = ax88172_bind,
  658. .status = asix_status,
  659. .link_reset = ax88172_link_reset,
  660. .reset = ax88172_link_reset,
  661. .flags = FLAG_ETHER | FLAG_LINK_INTR,
  662. .data = 0x00130103,
  663. };
  664. static const struct driver_info dlink_dub_e100_info = {
  665. .description = "DLink DUB-E100 USB Ethernet",
  666. .bind = ax88172_bind,
  667. .status = asix_status,
  668. .link_reset = ax88172_link_reset,
  669. .reset = ax88172_link_reset,
  670. .flags = FLAG_ETHER | FLAG_LINK_INTR,
  671. .data = 0x009f9d9f,
  672. };
  673. static const struct driver_info netgear_fa120_info = {
  674. .description = "Netgear FA-120 USB Ethernet",
  675. .bind = ax88172_bind,
  676. .status = asix_status,
  677. .link_reset = ax88172_link_reset,
  678. .reset = ax88172_link_reset,
  679. .flags = FLAG_ETHER | FLAG_LINK_INTR,
  680. .data = 0x00130103,
  681. };
  682. static const struct driver_info hawking_uf200_info = {
  683. .description = "Hawking UF200 USB Ethernet",
  684. .bind = ax88172_bind,
  685. .status = asix_status,
  686. .link_reset = ax88172_link_reset,
  687. .reset = ax88172_link_reset,
  688. .flags = FLAG_ETHER | FLAG_LINK_INTR,
  689. .data = 0x001f1d1f,
  690. };
  691. static const struct driver_info ax88772_info = {
  692. .description = "ASIX AX88772 USB 2.0 Ethernet",
  693. .bind = ax88772_bind,
  694. .status = asix_status,
  695. .link_reset = ax88772_link_reset,
  696. .reset = ax88772_reset,
  697. .flags = FLAG_ETHER | FLAG_FRAMING_AX | FLAG_LINK_INTR | FLAG_MULTI_PACKET,
  698. .rx_fixup = asix_rx_fixup,
  699. .tx_fixup = asix_tx_fixup,
  700. };
  701. static const struct driver_info ax88178_info = {
  702. .description = "ASIX AX88178 USB 2.0 Ethernet",
  703. .bind = ax88178_bind,
  704. .status = asix_status,
  705. .link_reset = ax88178_link_reset,
  706. .reset = ax88178_reset,
  707. .flags = FLAG_ETHER | FLAG_FRAMING_AX | FLAG_LINK_INTR,
  708. .rx_fixup = asix_rx_fixup,
  709. .tx_fixup = asix_tx_fixup,
  710. };
  711. extern const struct driver_info ax88172a_info;
  712. static const struct usb_device_id products [] = {
  713. {
  714. // Linksys USB200M
  715. USB_DEVICE (0x077b, 0x2226),
  716. .driver_info = (unsigned long) &ax8817x_info,
  717. }, {
  718. // Netgear FA120
  719. USB_DEVICE (0x0846, 0x1040),
  720. .driver_info = (unsigned long) &netgear_fa120_info,
  721. }, {
  722. // DLink DUB-E100
  723. USB_DEVICE (0x2001, 0x1a00),
  724. .driver_info = (unsigned long) &dlink_dub_e100_info,
  725. }, {
  726. // Intellinet, ST Lab USB Ethernet
  727. USB_DEVICE (0x0b95, 0x1720),
  728. .driver_info = (unsigned long) &ax8817x_info,
  729. }, {
  730. // Hawking UF200, TrendNet TU2-ET100
  731. USB_DEVICE (0x07b8, 0x420a),
  732. .driver_info = (unsigned long) &hawking_uf200_info,
  733. }, {
  734. // Billionton Systems, USB2AR
  735. USB_DEVICE (0x08dd, 0x90ff),
  736. .driver_info = (unsigned long) &ax8817x_info,
  737. }, {
  738. // ATEN UC210T
  739. USB_DEVICE (0x0557, 0x2009),
  740. .driver_info = (unsigned long) &ax8817x_info,
  741. }, {
  742. // Buffalo LUA-U2-KTX
  743. USB_DEVICE (0x0411, 0x003d),
  744. .driver_info = (unsigned long) &ax8817x_info,
  745. }, {
  746. // Buffalo LUA-U2-GT 10/100/1000
  747. USB_DEVICE (0x0411, 0x006e),
  748. .driver_info = (unsigned long) &ax88178_info,
  749. }, {
  750. // Sitecom LN-029 "USB 2.0 10/100 Ethernet adapter"
  751. USB_DEVICE (0x6189, 0x182d),
  752. .driver_info = (unsigned long) &ax8817x_info,
  753. }, {
  754. // Sitecom LN-031 "USB 2.0 10/100/1000 Ethernet adapter"
  755. USB_DEVICE (0x0df6, 0x0056),
  756. .driver_info = (unsigned long) &ax88178_info,
  757. }, {
  758. // corega FEther USB2-TX
  759. USB_DEVICE (0x07aa, 0x0017),
  760. .driver_info = (unsigned long) &ax8817x_info,
  761. }, {
  762. // Surecom EP-1427X-2
  763. USB_DEVICE (0x1189, 0x0893),
  764. .driver_info = (unsigned long) &ax8817x_info,
  765. }, {
  766. // goodway corp usb gwusb2e
  767. USB_DEVICE (0x1631, 0x6200),
  768. .driver_info = (unsigned long) &ax8817x_info,
  769. }, {
  770. // JVC MP-PRX1 Port Replicator
  771. USB_DEVICE (0x04f1, 0x3008),
  772. .driver_info = (unsigned long) &ax8817x_info,
  773. }, {
  774. // Lenovo U2L100P 10/100
  775. USB_DEVICE (0x17ef, 0x7203),
  776. .driver_info = (unsigned long) &ax88772_info,
  777. }, {
  778. // ASIX AX88772B 10/100
  779. USB_DEVICE (0x0b95, 0x772b),
  780. .driver_info = (unsigned long) &ax88772_info,
  781. }, {
  782. // ASIX AX88772 10/100
  783. USB_DEVICE (0x0b95, 0x7720),
  784. .driver_info = (unsigned long) &ax88772_info,
  785. }, {
  786. // ASIX AX88178 10/100/1000
  787. USB_DEVICE (0x0b95, 0x1780),
  788. .driver_info = (unsigned long) &ax88178_info,
  789. }, {
  790. // Logitec LAN-GTJ/U2A
  791. USB_DEVICE (0x0789, 0x0160),
  792. .driver_info = (unsigned long) &ax88178_info,
  793. }, {
  794. // Linksys USB200M Rev 2
  795. USB_DEVICE (0x13b1, 0x0018),
  796. .driver_info = (unsigned long) &ax88772_info,
  797. }, {
  798. // 0Q0 cable ethernet
  799. USB_DEVICE (0x1557, 0x7720),
  800. .driver_info = (unsigned long) &ax88772_info,
  801. }, {
  802. // DLink DUB-E100 H/W Ver B1
  803. USB_DEVICE (0x07d1, 0x3c05),
  804. .driver_info = (unsigned long) &ax88772_info,
  805. }, {
  806. // DLink DUB-E100 H/W Ver B1 Alternate
  807. USB_DEVICE (0x2001, 0x3c05),
  808. .driver_info = (unsigned long) &ax88772_info,
  809. }, {
  810. // DLink DUB-E100 H/W Ver C1
  811. USB_DEVICE (0x2001, 0x1a02),
  812. .driver_info = (unsigned long) &ax88772_info,
  813. }, {
  814. // Linksys USB1000
  815. USB_DEVICE (0x1737, 0x0039),
  816. .driver_info = (unsigned long) &ax88178_info,
  817. }, {
  818. // IO-DATA ETG-US2
  819. USB_DEVICE (0x04bb, 0x0930),
  820. .driver_info = (unsigned long) &ax88178_info,
  821. }, {
  822. // Belkin F5D5055
  823. USB_DEVICE(0x050d, 0x5055),
  824. .driver_info = (unsigned long) &ax88178_info,
  825. }, {
  826. // Apple USB Ethernet Adapter
  827. USB_DEVICE(0x05ac, 0x1402),
  828. .driver_info = (unsigned long) &ax88772_info,
  829. }, {
  830. // Cables-to-Go USB Ethernet Adapter
  831. USB_DEVICE(0x0b95, 0x772a),
  832. .driver_info = (unsigned long) &ax88772_info,
  833. }, {
  834. // ABOCOM for pci
  835. USB_DEVICE(0x14ea, 0xab11),
  836. .driver_info = (unsigned long) &ax88178_info,
  837. }, {
  838. // ASIX 88772a
  839. USB_DEVICE(0x0db0, 0xa877),
  840. .driver_info = (unsigned long) &ax88772_info,
  841. }, {
  842. // Asus USB Ethernet Adapter
  843. USB_DEVICE (0x0b95, 0x7e2b),
  844. .driver_info = (unsigned long) &ax88772_info,
  845. }, {
  846. /* ASIX 88172a demo board */
  847. USB_DEVICE(0x0b95, 0x172a),
  848. .driver_info = (unsigned long) &ax88172a_info,
  849. },
  850. { }, // END
  851. };
  852. MODULE_DEVICE_TABLE(usb, products);
  853. static struct usb_driver asix_driver = {
  854. .name = DRIVER_NAME,
  855. .id_table = products,
  856. .probe = usbnet_probe,
  857. .suspend = usbnet_suspend,
  858. .resume = usbnet_resume,
  859. .disconnect = usbnet_disconnect,
  860. .supports_autosuspend = 1,
  861. .disable_hub_initiated_lpm = 1,
  862. };
  863. module_usb_driver(asix_driver);
  864. MODULE_AUTHOR("David Hollis");
  865. MODULE_VERSION(DRIVER_VERSION);
  866. MODULE_DESCRIPTION("ASIX AX8817X based USB 2.0 Ethernet Devices");
  867. MODULE_LICENSE("GPL");