asix_devices.c 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034
  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. /* Get the PHY Identifier from the PHYSID1 & PHYSID2 MII registers */
  57. static u32 asix_get_phyid(struct usbnet *dev)
  58. {
  59. int phy_reg;
  60. u32 phy_id;
  61. int i;
  62. /* Poll for the rare case the FW or phy isn't ready yet. */
  63. for (i = 0; i < 100; i++) {
  64. phy_reg = asix_mdio_read(dev->net, dev->mii.phy_id, MII_PHYSID1);
  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. .get_settings = usbnet_get_settings,
  102. .set_settings = usbnet_set_settings,
  103. .nway_reset = usbnet_nway_reset,
  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);
  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_set_mac_address = eth_mac_addr,
  158. .ndo_validate_addr = eth_validate_addr,
  159. .ndo_do_ioctl = asix_ioctl,
  160. .ndo_set_rx_mode = ax88172_set_multicast,
  161. };
  162. static int ax88172_bind(struct usbnet *dev, struct usb_interface *intf)
  163. {
  164. int ret = 0;
  165. u8 buf[ETH_ALEN];
  166. int i;
  167. unsigned long gpio_bits = dev->driver_info->data;
  168. usbnet_get_endpoints(dev,intf);
  169. /* Toggle the GPIOs in a manufacturer/model specific way */
  170. for (i = 2; i >= 0; i--) {
  171. ret = asix_write_cmd(dev, AX_CMD_WRITE_GPIOS,
  172. (gpio_bits >> (i * 8)) & 0xff, 0, 0, NULL);
  173. if (ret < 0)
  174. goto out;
  175. msleep(5);
  176. }
  177. ret = asix_write_rx_ctl(dev, 0x80);
  178. if (ret < 0)
  179. goto out;
  180. /* Get the MAC address */
  181. ret = asix_read_cmd(dev, AX88172_CMD_READ_NODE_ID, 0, 0, ETH_ALEN, buf);
  182. if (ret < 0) {
  183. netdev_dbg(dev->net, "read AX_CMD_READ_NODE_ID failed: %d\n",
  184. ret);
  185. goto out;
  186. }
  187. memcpy(dev->net->dev_addr, buf, ETH_ALEN);
  188. /* Initialize MII structure */
  189. dev->mii.dev = dev->net;
  190. dev->mii.mdio_read = asix_mdio_read;
  191. dev->mii.mdio_write = asix_mdio_write;
  192. dev->mii.phy_id_mask = 0x3f;
  193. dev->mii.reg_num_mask = 0x1f;
  194. dev->mii.phy_id = asix_get_phy_addr(dev);
  195. dev->net->netdev_ops = &ax88172_netdev_ops;
  196. dev->net->ethtool_ops = &ax88172_ethtool_ops;
  197. dev->net->needed_headroom = 4; /* cf asix_tx_fixup() */
  198. dev->net->needed_tailroom = 4; /* cf asix_tx_fixup() */
  199. asix_mdio_write(dev->net, dev->mii.phy_id, MII_BMCR, BMCR_RESET);
  200. asix_mdio_write(dev->net, dev->mii.phy_id, MII_ADVERTISE,
  201. ADVERTISE_ALL | ADVERTISE_CSMA | ADVERTISE_PAUSE_CAP);
  202. mii_nway_restart(&dev->mii);
  203. return 0;
  204. out:
  205. return ret;
  206. }
  207. static const struct ethtool_ops ax88772_ethtool_ops = {
  208. .get_drvinfo = asix_get_drvinfo,
  209. .get_link = asix_get_link,
  210. .get_msglevel = usbnet_get_msglevel,
  211. .set_msglevel = usbnet_set_msglevel,
  212. .get_wol = asix_get_wol,
  213. .set_wol = asix_set_wol,
  214. .get_eeprom_len = asix_get_eeprom_len,
  215. .get_eeprom = asix_get_eeprom,
  216. .set_eeprom = asix_set_eeprom,
  217. .get_settings = usbnet_get_settings,
  218. .set_settings = usbnet_set_settings,
  219. .nway_reset = usbnet_nway_reset,
  220. };
  221. static int ax88772_link_reset(struct usbnet *dev)
  222. {
  223. u16 mode;
  224. struct ethtool_cmd ecmd = { .cmd = ETHTOOL_GSET };
  225. mii_check_media(&dev->mii, 1, 1);
  226. mii_ethtool_gset(&dev->mii, &ecmd);
  227. mode = AX88772_MEDIUM_DEFAULT;
  228. if (ethtool_cmd_speed(&ecmd) != SPEED_100)
  229. mode &= ~AX_MEDIUM_PS;
  230. if (ecmd.duplex != DUPLEX_FULL)
  231. mode &= ~AX_MEDIUM_FD;
  232. netdev_dbg(dev->net, "ax88772_link_reset() speed: %u duplex: %d setting mode to 0x%04x\n",
  233. ethtool_cmd_speed(&ecmd), ecmd.duplex, mode);
  234. asix_write_medium_mode(dev, mode);
  235. return 0;
  236. }
  237. static int ax88772_reset(struct usbnet *dev)
  238. {
  239. struct asix_data *data = (struct asix_data *)&dev->data;
  240. int ret, embd_phy;
  241. u16 rx_ctl;
  242. ret = asix_write_gpio(dev,
  243. AX_GPIO_RSE | AX_GPIO_GPO_2 | AX_GPIO_GPO2EN, 5);
  244. if (ret < 0)
  245. goto out;
  246. embd_phy = ((asix_get_phy_addr(dev) & 0x1f) == 0x10 ? 1 : 0);
  247. ret = asix_write_cmd(dev, AX_CMD_SW_PHY_SELECT, embd_phy, 0, 0, NULL);
  248. if (ret < 0) {
  249. netdev_dbg(dev->net, "Select PHY #1 failed: %d\n", ret);
  250. goto out;
  251. }
  252. ret = asix_sw_reset(dev, AX_SWRESET_IPPD | AX_SWRESET_PRL);
  253. if (ret < 0)
  254. goto out;
  255. msleep(150);
  256. ret = asix_sw_reset(dev, AX_SWRESET_CLEAR);
  257. if (ret < 0)
  258. goto out;
  259. msleep(150);
  260. if (embd_phy) {
  261. ret = asix_sw_reset(dev, AX_SWRESET_IPRL);
  262. if (ret < 0)
  263. goto out;
  264. } else {
  265. ret = asix_sw_reset(dev, AX_SWRESET_PRTE);
  266. if (ret < 0)
  267. goto out;
  268. }
  269. msleep(150);
  270. rx_ctl = asix_read_rx_ctl(dev);
  271. netdev_dbg(dev->net, "RX_CTL is 0x%04x after software reset\n", rx_ctl);
  272. ret = asix_write_rx_ctl(dev, 0x0000);
  273. if (ret < 0)
  274. goto out;
  275. rx_ctl = asix_read_rx_ctl(dev);
  276. netdev_dbg(dev->net, "RX_CTL is 0x%04x setting to 0x0000\n", rx_ctl);
  277. ret = asix_sw_reset(dev, AX_SWRESET_PRL);
  278. if (ret < 0)
  279. goto out;
  280. msleep(150);
  281. ret = asix_sw_reset(dev, AX_SWRESET_IPRL | AX_SWRESET_PRL);
  282. if (ret < 0)
  283. goto out;
  284. msleep(150);
  285. asix_mdio_write(dev->net, dev->mii.phy_id, MII_BMCR, BMCR_RESET);
  286. asix_mdio_write(dev->net, dev->mii.phy_id, MII_ADVERTISE,
  287. ADVERTISE_ALL | ADVERTISE_CSMA);
  288. mii_nway_restart(&dev->mii);
  289. ret = asix_write_medium_mode(dev, AX88772_MEDIUM_DEFAULT);
  290. if (ret < 0)
  291. goto out;
  292. ret = asix_write_cmd(dev, AX_CMD_WRITE_IPG0,
  293. AX88772_IPG0_DEFAULT | AX88772_IPG1_DEFAULT,
  294. AX88772_IPG2_DEFAULT, 0, NULL);
  295. if (ret < 0) {
  296. netdev_dbg(dev->net, "Write IPG,IPG1,IPG2 failed: %d\n", ret);
  297. goto out;
  298. }
  299. /* Rewrite MAC address */
  300. memcpy(data->mac_addr, dev->net->dev_addr, ETH_ALEN);
  301. ret = asix_write_cmd(dev, AX_CMD_WRITE_NODE_ID, 0, 0, ETH_ALEN,
  302. data->mac_addr);
  303. if (ret < 0)
  304. goto out;
  305. /* Set RX_CTL to default values with 2k buffer, and enable cactus */
  306. ret = asix_write_rx_ctl(dev, AX_DEFAULT_RX_CTL);
  307. if (ret < 0)
  308. goto out;
  309. rx_ctl = asix_read_rx_ctl(dev);
  310. netdev_dbg(dev->net, "RX_CTL is 0x%04x after all initializations\n",
  311. rx_ctl);
  312. rx_ctl = asix_read_medium_status(dev);
  313. netdev_dbg(dev->net,
  314. "Medium Status is 0x%04x after all initializations\n",
  315. rx_ctl);
  316. return 0;
  317. out:
  318. return ret;
  319. }
  320. static const struct net_device_ops ax88772_netdev_ops = {
  321. .ndo_open = usbnet_open,
  322. .ndo_stop = usbnet_stop,
  323. .ndo_start_xmit = usbnet_start_xmit,
  324. .ndo_tx_timeout = usbnet_tx_timeout,
  325. .ndo_change_mtu = usbnet_change_mtu,
  326. .ndo_set_mac_address = asix_set_mac_address,
  327. .ndo_validate_addr = eth_validate_addr,
  328. .ndo_do_ioctl = asix_ioctl,
  329. .ndo_set_rx_mode = asix_set_multicast,
  330. };
  331. static int ax88772_bind(struct usbnet *dev, struct usb_interface *intf)
  332. {
  333. int ret, embd_phy;
  334. u8 buf[ETH_ALEN];
  335. u32 phyid;
  336. usbnet_get_endpoints(dev,intf);
  337. /* Get the MAC address */
  338. ret = asix_read_cmd(dev, AX_CMD_READ_NODE_ID, 0, 0, ETH_ALEN, buf);
  339. if (ret < 0) {
  340. netdev_dbg(dev->net, "Failed to read MAC address: %d\n", ret);
  341. return ret;
  342. }
  343. memcpy(dev->net->dev_addr, buf, ETH_ALEN);
  344. /* Initialize MII structure */
  345. dev->mii.dev = dev->net;
  346. dev->mii.mdio_read = asix_mdio_read;
  347. dev->mii.mdio_write = asix_mdio_write;
  348. dev->mii.phy_id_mask = 0x1f;
  349. dev->mii.reg_num_mask = 0x1f;
  350. dev->mii.phy_id = asix_get_phy_addr(dev);
  351. dev->net->netdev_ops = &ax88772_netdev_ops;
  352. dev->net->ethtool_ops = &ax88772_ethtool_ops;
  353. dev->net->needed_headroom = 4; /* cf asix_tx_fixup() */
  354. dev->net->needed_tailroom = 4; /* cf asix_tx_fixup() */
  355. embd_phy = ((dev->mii.phy_id & 0x1f) == 0x10 ? 1 : 0);
  356. /* Reset the PHY to normal operation mode */
  357. ret = asix_write_cmd(dev, AX_CMD_SW_PHY_SELECT, embd_phy, 0, 0, NULL);
  358. if (ret < 0) {
  359. netdev_dbg(dev->net, "Select PHY #1 failed: %d\n", ret);
  360. return ret;
  361. }
  362. ret = asix_sw_reset(dev, AX_SWRESET_IPPD | AX_SWRESET_PRL);
  363. if (ret < 0)
  364. return ret;
  365. msleep(150);
  366. ret = asix_sw_reset(dev, AX_SWRESET_CLEAR);
  367. if (ret < 0)
  368. return ret;
  369. msleep(150);
  370. ret = asix_sw_reset(dev, embd_phy ? AX_SWRESET_IPRL : AX_SWRESET_PRTE);
  371. /* Read PHYID register *AFTER* the PHY was reset properly */
  372. phyid = asix_get_phyid(dev);
  373. netdev_dbg(dev->net, "PHYID=0x%08x\n", phyid);
  374. /* Asix framing packs multiple eth frames into a 2K usb bulk transfer */
  375. if (dev->driver_info->flags & FLAG_FRAMING_AX) {
  376. /* hard_mtu is still the default - the device does not support
  377. jumbo eth frames */
  378. dev->rx_urb_size = 2048;
  379. }
  380. return 0;
  381. }
  382. static const struct ethtool_ops ax88178_ethtool_ops = {
  383. .get_drvinfo = asix_get_drvinfo,
  384. .get_link = asix_get_link,
  385. .get_msglevel = usbnet_get_msglevel,
  386. .set_msglevel = usbnet_set_msglevel,
  387. .get_wol = asix_get_wol,
  388. .set_wol = asix_set_wol,
  389. .get_eeprom_len = asix_get_eeprom_len,
  390. .get_eeprom = asix_get_eeprom,
  391. .set_eeprom = asix_set_eeprom,
  392. .get_settings = usbnet_get_settings,
  393. .set_settings = usbnet_set_settings,
  394. .nway_reset = usbnet_nway_reset,
  395. };
  396. static int marvell_phy_init(struct usbnet *dev)
  397. {
  398. struct asix_data *data = (struct asix_data *)&dev->data;
  399. u16 reg;
  400. netdev_dbg(dev->net, "marvell_phy_init()\n");
  401. reg = asix_mdio_read(dev->net, dev->mii.phy_id, MII_MARVELL_STATUS);
  402. netdev_dbg(dev->net, "MII_MARVELL_STATUS = 0x%04x\n", reg);
  403. asix_mdio_write(dev->net, dev->mii.phy_id, MII_MARVELL_CTRL,
  404. MARVELL_CTRL_RXDELAY | MARVELL_CTRL_TXDELAY);
  405. if (data->ledmode) {
  406. reg = asix_mdio_read(dev->net, dev->mii.phy_id,
  407. MII_MARVELL_LED_CTRL);
  408. netdev_dbg(dev->net, "MII_MARVELL_LED_CTRL (1) = 0x%04x\n", reg);
  409. reg &= 0xf8ff;
  410. reg |= (1 + 0x0100);
  411. asix_mdio_write(dev->net, dev->mii.phy_id,
  412. MII_MARVELL_LED_CTRL, reg);
  413. reg = asix_mdio_read(dev->net, dev->mii.phy_id,
  414. MII_MARVELL_LED_CTRL);
  415. netdev_dbg(dev->net, "MII_MARVELL_LED_CTRL (2) = 0x%04x\n", reg);
  416. reg &= 0xfc0f;
  417. }
  418. return 0;
  419. }
  420. static int rtl8211cl_phy_init(struct usbnet *dev)
  421. {
  422. struct asix_data *data = (struct asix_data *)&dev->data;
  423. netdev_dbg(dev->net, "rtl8211cl_phy_init()\n");
  424. asix_mdio_write (dev->net, dev->mii.phy_id, 0x1f, 0x0005);
  425. asix_mdio_write (dev->net, dev->mii.phy_id, 0x0c, 0);
  426. asix_mdio_write (dev->net, dev->mii.phy_id, 0x01,
  427. asix_mdio_read (dev->net, dev->mii.phy_id, 0x01) | 0x0080);
  428. asix_mdio_write (dev->net, dev->mii.phy_id, 0x1f, 0);
  429. if (data->ledmode == 12) {
  430. asix_mdio_write (dev->net, dev->mii.phy_id, 0x1f, 0x0002);
  431. asix_mdio_write (dev->net, dev->mii.phy_id, 0x1a, 0x00cb);
  432. asix_mdio_write (dev->net, dev->mii.phy_id, 0x1f, 0);
  433. }
  434. return 0;
  435. }
  436. static int marvell_led_status(struct usbnet *dev, u16 speed)
  437. {
  438. u16 reg = asix_mdio_read(dev->net, dev->mii.phy_id, MARVELL_LED_MANUAL);
  439. netdev_dbg(dev->net, "marvell_led_status() read 0x%04x\n", reg);
  440. /* Clear out the center LED bits - 0x03F0 */
  441. reg &= 0xfc0f;
  442. switch (speed) {
  443. case SPEED_1000:
  444. reg |= 0x03e0;
  445. break;
  446. case SPEED_100:
  447. reg |= 0x03b0;
  448. break;
  449. default:
  450. reg |= 0x02f0;
  451. }
  452. netdev_dbg(dev->net, "marvell_led_status() writing 0x%04x\n", reg);
  453. asix_mdio_write(dev->net, dev->mii.phy_id, MARVELL_LED_MANUAL, reg);
  454. return 0;
  455. }
  456. static int ax88178_reset(struct usbnet *dev)
  457. {
  458. struct asix_data *data = (struct asix_data *)&dev->data;
  459. int ret;
  460. __le16 eeprom;
  461. u8 status;
  462. int gpio0 = 0;
  463. u32 phyid;
  464. asix_read_cmd(dev, AX_CMD_READ_GPIOS, 0, 0, 1, &status);
  465. netdev_dbg(dev->net, "GPIO Status: 0x%04x\n", status);
  466. asix_write_cmd(dev, AX_CMD_WRITE_ENABLE, 0, 0, 0, NULL);
  467. asix_read_cmd(dev, AX_CMD_READ_EEPROM, 0x0017, 0, 2, &eeprom);
  468. asix_write_cmd(dev, AX_CMD_WRITE_DISABLE, 0, 0, 0, NULL);
  469. netdev_dbg(dev->net, "EEPROM index 0x17 is 0x%04x\n", eeprom);
  470. if (eeprom == cpu_to_le16(0xffff)) {
  471. data->phymode = PHY_MODE_MARVELL;
  472. data->ledmode = 0;
  473. gpio0 = 1;
  474. } else {
  475. data->phymode = le16_to_cpu(eeprom) & 0x7F;
  476. data->ledmode = le16_to_cpu(eeprom) >> 8;
  477. gpio0 = (le16_to_cpu(eeprom) & 0x80) ? 0 : 1;
  478. }
  479. netdev_dbg(dev->net, "GPIO0: %d, PhyMode: %d\n", gpio0, data->phymode);
  480. /* Power up external GigaPHY through AX88178 GPIO pin */
  481. asix_write_gpio(dev, AX_GPIO_RSE | AX_GPIO_GPO_1 | AX_GPIO_GPO1EN, 40);
  482. if ((le16_to_cpu(eeprom) >> 8) != 1) {
  483. asix_write_gpio(dev, 0x003c, 30);
  484. asix_write_gpio(dev, 0x001c, 300);
  485. asix_write_gpio(dev, 0x003c, 30);
  486. } else {
  487. netdev_dbg(dev->net, "gpio phymode == 1 path\n");
  488. asix_write_gpio(dev, AX_GPIO_GPO1EN, 30);
  489. asix_write_gpio(dev, AX_GPIO_GPO1EN | AX_GPIO_GPO_1, 30);
  490. }
  491. /* Read PHYID register *AFTER* powering up PHY */
  492. phyid = asix_get_phyid(dev);
  493. netdev_dbg(dev->net, "PHYID=0x%08x\n", phyid);
  494. /* Set AX88178 to enable MII/GMII/RGMII interface for external PHY */
  495. asix_write_cmd(dev, AX_CMD_SW_PHY_SELECT, 0, 0, 0, NULL);
  496. asix_sw_reset(dev, 0);
  497. msleep(150);
  498. asix_sw_reset(dev, AX_SWRESET_PRL | AX_SWRESET_IPPD);
  499. msleep(150);
  500. asix_write_rx_ctl(dev, 0);
  501. if (data->phymode == PHY_MODE_MARVELL) {
  502. marvell_phy_init(dev);
  503. msleep(60);
  504. } else if (data->phymode == PHY_MODE_RTL8211CL)
  505. rtl8211cl_phy_init(dev);
  506. asix_mdio_write(dev->net, dev->mii.phy_id, MII_BMCR,
  507. BMCR_RESET | BMCR_ANENABLE);
  508. asix_mdio_write(dev->net, dev->mii.phy_id, MII_ADVERTISE,
  509. ADVERTISE_ALL | ADVERTISE_CSMA | ADVERTISE_PAUSE_CAP);
  510. asix_mdio_write(dev->net, dev->mii.phy_id, MII_CTRL1000,
  511. ADVERTISE_1000FULL);
  512. mii_nway_restart(&dev->mii);
  513. ret = asix_write_medium_mode(dev, AX88178_MEDIUM_DEFAULT);
  514. if (ret < 0)
  515. return ret;
  516. /* Rewrite MAC address */
  517. memcpy(data->mac_addr, dev->net->dev_addr, ETH_ALEN);
  518. ret = asix_write_cmd(dev, AX_CMD_WRITE_NODE_ID, 0, 0, ETH_ALEN,
  519. data->mac_addr);
  520. if (ret < 0)
  521. return ret;
  522. ret = asix_write_rx_ctl(dev, AX_DEFAULT_RX_CTL);
  523. if (ret < 0)
  524. return ret;
  525. return 0;
  526. }
  527. static int ax88178_link_reset(struct usbnet *dev)
  528. {
  529. u16 mode;
  530. struct ethtool_cmd ecmd = { .cmd = ETHTOOL_GSET };
  531. struct asix_data *data = (struct asix_data *)&dev->data;
  532. u32 speed;
  533. netdev_dbg(dev->net, "ax88178_link_reset()\n");
  534. mii_check_media(&dev->mii, 1, 1);
  535. mii_ethtool_gset(&dev->mii, &ecmd);
  536. mode = AX88178_MEDIUM_DEFAULT;
  537. speed = ethtool_cmd_speed(&ecmd);
  538. if (speed == SPEED_1000)
  539. mode |= AX_MEDIUM_GM;
  540. else if (speed == SPEED_100)
  541. mode |= AX_MEDIUM_PS;
  542. else
  543. mode &= ~(AX_MEDIUM_PS | AX_MEDIUM_GM);
  544. mode |= AX_MEDIUM_ENCK;
  545. if (ecmd.duplex == DUPLEX_FULL)
  546. mode |= AX_MEDIUM_FD;
  547. else
  548. mode &= ~AX_MEDIUM_FD;
  549. netdev_dbg(dev->net, "ax88178_link_reset() speed: %u duplex: %d setting mode to 0x%04x\n",
  550. speed, ecmd.duplex, mode);
  551. asix_write_medium_mode(dev, mode);
  552. if (data->phymode == PHY_MODE_MARVELL && data->ledmode)
  553. marvell_led_status(dev, speed);
  554. return 0;
  555. }
  556. static void ax88178_set_mfb(struct usbnet *dev)
  557. {
  558. u16 mfb = AX_RX_CTL_MFB_16384;
  559. u16 rxctl;
  560. u16 medium;
  561. int old_rx_urb_size = dev->rx_urb_size;
  562. if (dev->hard_mtu < 2048) {
  563. dev->rx_urb_size = 2048;
  564. mfb = AX_RX_CTL_MFB_2048;
  565. } else if (dev->hard_mtu < 4096) {
  566. dev->rx_urb_size = 4096;
  567. mfb = AX_RX_CTL_MFB_4096;
  568. } else if (dev->hard_mtu < 8192) {
  569. dev->rx_urb_size = 8192;
  570. mfb = AX_RX_CTL_MFB_8192;
  571. } else if (dev->hard_mtu < 16384) {
  572. dev->rx_urb_size = 16384;
  573. mfb = AX_RX_CTL_MFB_16384;
  574. }
  575. rxctl = asix_read_rx_ctl(dev);
  576. asix_write_rx_ctl(dev, (rxctl & ~AX_RX_CTL_MFB_16384) | mfb);
  577. medium = asix_read_medium_status(dev);
  578. if (dev->net->mtu > 1500)
  579. medium |= AX_MEDIUM_JFE;
  580. else
  581. medium &= ~AX_MEDIUM_JFE;
  582. asix_write_medium_mode(dev, medium);
  583. if (dev->rx_urb_size > old_rx_urb_size)
  584. usbnet_unlink_rx_urbs(dev);
  585. }
  586. static int ax88178_change_mtu(struct net_device *net, int new_mtu)
  587. {
  588. struct usbnet *dev = netdev_priv(net);
  589. int ll_mtu = new_mtu + net->hard_header_len + 4;
  590. netdev_dbg(dev->net, "ax88178_change_mtu() new_mtu=%d\n", new_mtu);
  591. if (new_mtu <= 0 || ll_mtu > 16384)
  592. return -EINVAL;
  593. if ((ll_mtu % dev->maxpacket) == 0)
  594. return -EDOM;
  595. net->mtu = new_mtu;
  596. dev->hard_mtu = net->mtu + net->hard_header_len;
  597. ax88178_set_mfb(dev);
  598. return 0;
  599. }
  600. static const struct net_device_ops ax88178_netdev_ops = {
  601. .ndo_open = usbnet_open,
  602. .ndo_stop = usbnet_stop,
  603. .ndo_start_xmit = usbnet_start_xmit,
  604. .ndo_tx_timeout = usbnet_tx_timeout,
  605. .ndo_set_mac_address = asix_set_mac_address,
  606. .ndo_validate_addr = eth_validate_addr,
  607. .ndo_set_rx_mode = asix_set_multicast,
  608. .ndo_do_ioctl = asix_ioctl,
  609. .ndo_change_mtu = ax88178_change_mtu,
  610. };
  611. static int ax88178_bind(struct usbnet *dev, struct usb_interface *intf)
  612. {
  613. int ret;
  614. u8 buf[ETH_ALEN];
  615. usbnet_get_endpoints(dev,intf);
  616. /* Get the MAC address */
  617. ret = asix_read_cmd(dev, AX_CMD_READ_NODE_ID, 0, 0, ETH_ALEN, buf);
  618. if (ret < 0) {
  619. netdev_dbg(dev->net, "Failed to read MAC address: %d\n", ret);
  620. return ret;
  621. }
  622. memcpy(dev->net->dev_addr, buf, ETH_ALEN);
  623. /* Initialize MII structure */
  624. dev->mii.dev = dev->net;
  625. dev->mii.mdio_read = asix_mdio_read;
  626. dev->mii.mdio_write = asix_mdio_write;
  627. dev->mii.phy_id_mask = 0x1f;
  628. dev->mii.reg_num_mask = 0xff;
  629. dev->mii.supports_gmii = 1;
  630. dev->mii.phy_id = asix_get_phy_addr(dev);
  631. dev->net->netdev_ops = &ax88178_netdev_ops;
  632. dev->net->ethtool_ops = &ax88178_ethtool_ops;
  633. /* Blink LEDS so users know driver saw dongle */
  634. asix_sw_reset(dev, 0);
  635. msleep(150);
  636. asix_sw_reset(dev, AX_SWRESET_PRL | AX_SWRESET_IPPD);
  637. msleep(150);
  638. /* Asix framing packs multiple eth frames into a 2K usb bulk transfer */
  639. if (dev->driver_info->flags & FLAG_FRAMING_AX) {
  640. /* hard_mtu is still the default - the device does not support
  641. jumbo eth frames */
  642. dev->rx_urb_size = 2048;
  643. }
  644. return 0;
  645. }
  646. static const struct driver_info ax8817x_info = {
  647. .description = "ASIX AX8817x USB 2.0 Ethernet",
  648. .bind = ax88172_bind,
  649. .status = asix_status,
  650. .link_reset = ax88172_link_reset,
  651. .reset = ax88172_link_reset,
  652. .flags = FLAG_ETHER | FLAG_LINK_INTR,
  653. .data = 0x00130103,
  654. };
  655. static const struct driver_info dlink_dub_e100_info = {
  656. .description = "DLink DUB-E100 USB 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 = 0x009f9d9f,
  663. };
  664. static const struct driver_info netgear_fa120_info = {
  665. .description = "Netgear FA-120 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 = 0x00130103,
  672. };
  673. static const struct driver_info hawking_uf200_info = {
  674. .description = "Hawking UF200 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 = 0x001f1d1f,
  681. };
  682. static const struct driver_info ax88772_info = {
  683. .description = "ASIX AX88772 USB 2.0 Ethernet",
  684. .bind = ax88772_bind,
  685. .status = asix_status,
  686. .link_reset = ax88772_link_reset,
  687. .reset = ax88772_reset,
  688. .flags = FLAG_ETHER | FLAG_FRAMING_AX | FLAG_LINK_INTR | FLAG_MULTI_PACKET,
  689. .rx_fixup = asix_rx_fixup,
  690. .tx_fixup = asix_tx_fixup,
  691. };
  692. static const struct driver_info ax88178_info = {
  693. .description = "ASIX AX88178 USB 2.0 Ethernet",
  694. .bind = ax88178_bind,
  695. .status = asix_status,
  696. .link_reset = ax88178_link_reset,
  697. .reset = ax88178_reset,
  698. .flags = FLAG_ETHER | FLAG_FRAMING_AX | FLAG_LINK_INTR,
  699. .rx_fixup = asix_rx_fixup,
  700. .tx_fixup = asix_tx_fixup,
  701. };
  702. extern const struct driver_info ax88172a_info;
  703. static const struct usb_device_id products [] = {
  704. {
  705. // Linksys USB200M
  706. USB_DEVICE (0x077b, 0x2226),
  707. .driver_info = (unsigned long) &ax8817x_info,
  708. }, {
  709. // Netgear FA120
  710. USB_DEVICE (0x0846, 0x1040),
  711. .driver_info = (unsigned long) &netgear_fa120_info,
  712. }, {
  713. // DLink DUB-E100
  714. USB_DEVICE (0x2001, 0x1a00),
  715. .driver_info = (unsigned long) &dlink_dub_e100_info,
  716. }, {
  717. // Intellinet, ST Lab USB Ethernet
  718. USB_DEVICE (0x0b95, 0x1720),
  719. .driver_info = (unsigned long) &ax8817x_info,
  720. }, {
  721. // Hawking UF200, TrendNet TU2-ET100
  722. USB_DEVICE (0x07b8, 0x420a),
  723. .driver_info = (unsigned long) &hawking_uf200_info,
  724. }, {
  725. // Billionton Systems, USB2AR
  726. USB_DEVICE (0x08dd, 0x90ff),
  727. .driver_info = (unsigned long) &ax8817x_info,
  728. }, {
  729. // ATEN UC210T
  730. USB_DEVICE (0x0557, 0x2009),
  731. .driver_info = (unsigned long) &ax8817x_info,
  732. }, {
  733. // Buffalo LUA-U2-KTX
  734. USB_DEVICE (0x0411, 0x003d),
  735. .driver_info = (unsigned long) &ax8817x_info,
  736. }, {
  737. // Buffalo LUA-U2-GT 10/100/1000
  738. USB_DEVICE (0x0411, 0x006e),
  739. .driver_info = (unsigned long) &ax88178_info,
  740. }, {
  741. // Sitecom LN-029 "USB 2.0 10/100 Ethernet adapter"
  742. USB_DEVICE (0x6189, 0x182d),
  743. .driver_info = (unsigned long) &ax8817x_info,
  744. }, {
  745. // Sitecom LN-031 "USB 2.0 10/100/1000 Ethernet adapter"
  746. USB_DEVICE (0x0df6, 0x0056),
  747. .driver_info = (unsigned long) &ax88178_info,
  748. }, {
  749. // corega FEther USB2-TX
  750. USB_DEVICE (0x07aa, 0x0017),
  751. .driver_info = (unsigned long) &ax8817x_info,
  752. }, {
  753. // Surecom EP-1427X-2
  754. USB_DEVICE (0x1189, 0x0893),
  755. .driver_info = (unsigned long) &ax8817x_info,
  756. }, {
  757. // goodway corp usb gwusb2e
  758. USB_DEVICE (0x1631, 0x6200),
  759. .driver_info = (unsigned long) &ax8817x_info,
  760. }, {
  761. // JVC MP-PRX1 Port Replicator
  762. USB_DEVICE (0x04f1, 0x3008),
  763. .driver_info = (unsigned long) &ax8817x_info,
  764. }, {
  765. // Lenovo U2L100P 10/100
  766. USB_DEVICE (0x17ef, 0x7203),
  767. .driver_info = (unsigned long) &ax88772_info,
  768. }, {
  769. // ASIX AX88772B 10/100
  770. USB_DEVICE (0x0b95, 0x772b),
  771. .driver_info = (unsigned long) &ax88772_info,
  772. }, {
  773. // ASIX AX88772 10/100
  774. USB_DEVICE (0x0b95, 0x7720),
  775. .driver_info = (unsigned long) &ax88772_info,
  776. }, {
  777. // ASIX AX88178 10/100/1000
  778. USB_DEVICE (0x0b95, 0x1780),
  779. .driver_info = (unsigned long) &ax88178_info,
  780. }, {
  781. // Logitec LAN-GTJ/U2A
  782. USB_DEVICE (0x0789, 0x0160),
  783. .driver_info = (unsigned long) &ax88178_info,
  784. }, {
  785. // Linksys USB200M Rev 2
  786. USB_DEVICE (0x13b1, 0x0018),
  787. .driver_info = (unsigned long) &ax88772_info,
  788. }, {
  789. // 0Q0 cable ethernet
  790. USB_DEVICE (0x1557, 0x7720),
  791. .driver_info = (unsigned long) &ax88772_info,
  792. }, {
  793. // DLink DUB-E100 H/W Ver B1
  794. USB_DEVICE (0x07d1, 0x3c05),
  795. .driver_info = (unsigned long) &ax88772_info,
  796. }, {
  797. // DLink DUB-E100 H/W Ver B1 Alternate
  798. USB_DEVICE (0x2001, 0x3c05),
  799. .driver_info = (unsigned long) &ax88772_info,
  800. }, {
  801. // DLink DUB-E100 H/W Ver C1
  802. USB_DEVICE (0x2001, 0x1a02),
  803. .driver_info = (unsigned long) &ax88772_info,
  804. }, {
  805. // Linksys USB1000
  806. USB_DEVICE (0x1737, 0x0039),
  807. .driver_info = (unsigned long) &ax88178_info,
  808. }, {
  809. // IO-DATA ETG-US2
  810. USB_DEVICE (0x04bb, 0x0930),
  811. .driver_info = (unsigned long) &ax88178_info,
  812. }, {
  813. // Belkin F5D5055
  814. USB_DEVICE(0x050d, 0x5055),
  815. .driver_info = (unsigned long) &ax88178_info,
  816. }, {
  817. // Apple USB Ethernet Adapter
  818. USB_DEVICE(0x05ac, 0x1402),
  819. .driver_info = (unsigned long) &ax88772_info,
  820. }, {
  821. // Cables-to-Go USB Ethernet Adapter
  822. USB_DEVICE(0x0b95, 0x772a),
  823. .driver_info = (unsigned long) &ax88772_info,
  824. }, {
  825. // ABOCOM for pci
  826. USB_DEVICE(0x14ea, 0xab11),
  827. .driver_info = (unsigned long) &ax88178_info,
  828. }, {
  829. // ASIX 88772a
  830. USB_DEVICE(0x0db0, 0xa877),
  831. .driver_info = (unsigned long) &ax88772_info,
  832. }, {
  833. // Asus USB Ethernet Adapter
  834. USB_DEVICE (0x0b95, 0x7e2b),
  835. .driver_info = (unsigned long) &ax88772_info,
  836. }, {
  837. /* ASIX 88172a demo board */
  838. USB_DEVICE(0x0b95, 0x172a),
  839. .driver_info = (unsigned long) &ax88172a_info,
  840. },
  841. { }, // END
  842. };
  843. MODULE_DEVICE_TABLE(usb, products);
  844. static struct usb_driver asix_driver = {
  845. .name = DRIVER_NAME,
  846. .id_table = products,
  847. .probe = usbnet_probe,
  848. .suspend = usbnet_suspend,
  849. .resume = usbnet_resume,
  850. .disconnect = usbnet_disconnect,
  851. .supports_autosuspend = 1,
  852. .disable_hub_initiated_lpm = 1,
  853. };
  854. module_usb_driver(asix_driver);
  855. MODULE_AUTHOR("David Hollis");
  856. MODULE_VERSION(DRIVER_VERSION);
  857. MODULE_DESCRIPTION("ASIX AX8817X based USB 2.0 Ethernet Devices");
  858. MODULE_LICENSE("GPL");