asix.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926
  1. /*
  2. * Copyright (c) 2011 The Chromium OS Authors.
  3. *
  4. * Patched for AX88772B by Antmicro Ltd <www.antmicro.com>
  5. *
  6. * SPDX-License-Identifier: GPL-2.0+
  7. */
  8. #include <common.h>
  9. #include <dm.h>
  10. #include <usb.h>
  11. #include <malloc.h>
  12. #include <memalign.h>
  13. #include <linux/mii.h>
  14. #include "usb_ether.h"
  15. /* ASIX AX8817X based USB 2.0 Ethernet Devices */
  16. #define AX_CMD_SET_SW_MII 0x06
  17. #define AX_CMD_READ_MII_REG 0x07
  18. #define AX_CMD_WRITE_MII_REG 0x08
  19. #define AX_CMD_SET_HW_MII 0x0a
  20. #define AX_CMD_READ_EEPROM 0x0b
  21. #define AX_CMD_READ_RX_CTL 0x0f
  22. #define AX_CMD_WRITE_RX_CTL 0x10
  23. #define AX_CMD_WRITE_IPG0 0x12
  24. #define AX_CMD_READ_NODE_ID 0x13
  25. #define AX_CMD_WRITE_NODE_ID 0x14
  26. #define AX_CMD_READ_PHY_ID 0x19
  27. #define AX_CMD_WRITE_MEDIUM_MODE 0x1b
  28. #define AX_CMD_WRITE_GPIOS 0x1f
  29. #define AX_CMD_SW_RESET 0x20
  30. #define AX_CMD_SW_PHY_SELECT 0x22
  31. #define AX_SWRESET_CLEAR 0x00
  32. #define AX_SWRESET_PRTE 0x04
  33. #define AX_SWRESET_PRL 0x08
  34. #define AX_SWRESET_IPRL 0x20
  35. #define AX_SWRESET_IPPD 0x40
  36. #define AX88772_IPG0_DEFAULT 0x15
  37. #define AX88772_IPG1_DEFAULT 0x0c
  38. #define AX88772_IPG2_DEFAULT 0x12
  39. /* AX88772 & AX88178 Medium Mode Register */
  40. #define AX_MEDIUM_PF 0x0080
  41. #define AX_MEDIUM_JFE 0x0040
  42. #define AX_MEDIUM_TFC 0x0020
  43. #define AX_MEDIUM_RFC 0x0010
  44. #define AX_MEDIUM_ENCK 0x0008
  45. #define AX_MEDIUM_AC 0x0004
  46. #define AX_MEDIUM_FD 0x0002
  47. #define AX_MEDIUM_GM 0x0001
  48. #define AX_MEDIUM_SM 0x1000
  49. #define AX_MEDIUM_SBP 0x0800
  50. #define AX_MEDIUM_PS 0x0200
  51. #define AX_MEDIUM_RE 0x0100
  52. #define AX88178_MEDIUM_DEFAULT \
  53. (AX_MEDIUM_PS | AX_MEDIUM_FD | AX_MEDIUM_AC | \
  54. AX_MEDIUM_RFC | AX_MEDIUM_TFC | AX_MEDIUM_JFE | \
  55. AX_MEDIUM_RE)
  56. #define AX88772_MEDIUM_DEFAULT \
  57. (AX_MEDIUM_FD | AX_MEDIUM_RFC | \
  58. AX_MEDIUM_TFC | AX_MEDIUM_PS | \
  59. AX_MEDIUM_AC | AX_MEDIUM_RE)
  60. /* AX88772 & AX88178 RX_CTL values */
  61. #define AX_RX_CTL_RH2M 0x0200 /* 32-bit aligned RX IP header */
  62. #define AX_RX_CTL_RH1M 0x0100 /* Enable RX header format type 1 */
  63. #define AX_RX_CTL_SO 0x0080
  64. #define AX_RX_CTL_AB 0x0008
  65. #define AX_RX_HEADER_DEFAULT (AX_RX_CTL_RH1M | AX_RX_CTL_RH2M)
  66. #define AX_DEFAULT_RX_CTL \
  67. (AX_RX_CTL_SO | AX_RX_CTL_AB)
  68. /* GPIO 2 toggles */
  69. #define AX_GPIO_GPO2EN 0x10 /* GPIO2 Output enable */
  70. #define AX_GPIO_GPO_2 0x20 /* GPIO2 Output value */
  71. #define AX_GPIO_RSE 0x80 /* Reload serial EEPROM */
  72. /* local defines */
  73. #define ASIX_BASE_NAME "asx"
  74. #define USB_CTRL_SET_TIMEOUT 5000
  75. #define USB_CTRL_GET_TIMEOUT 5000
  76. #define USB_BULK_SEND_TIMEOUT 5000
  77. #define USB_BULK_RECV_TIMEOUT 5000
  78. #define AX_RX_URB_SIZE 2048
  79. #define PHY_CONNECT_TIMEOUT 5000
  80. /* asix_flags defines */
  81. #define FLAG_NONE 0
  82. #define FLAG_TYPE_AX88172 (1U << 0)
  83. #define FLAG_TYPE_AX88772 (1U << 1)
  84. #define FLAG_TYPE_AX88772B (1U << 2)
  85. #define FLAG_EEPROM_MAC (1U << 3) /* initial mac address in eeprom */
  86. #define ASIX_USB_VENDOR_ID 0x0b95
  87. #define AX88772B_USB_PRODUCT_ID 0x772b
  88. /* driver private */
  89. struct asix_private {
  90. int flags;
  91. #ifdef CONFIG_DM_ETH
  92. struct ueth_data ueth;
  93. #endif
  94. };
  95. #ifndef CONFIG_DM_ETH
  96. /* local vars */
  97. static int curr_eth_dev; /* index for name of next device detected */
  98. #endif
  99. /*
  100. * Asix infrastructure commands
  101. */
  102. static int asix_write_cmd(struct ueth_data *dev, u8 cmd, u16 value, u16 index,
  103. u16 size, void *data)
  104. {
  105. int len;
  106. debug("asix_write_cmd() cmd=0x%02x value=0x%04x index=0x%04x "
  107. "size=%d\n", cmd, value, index, size);
  108. len = usb_control_msg(
  109. dev->pusb_dev,
  110. usb_sndctrlpipe(dev->pusb_dev, 0),
  111. cmd,
  112. USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
  113. value,
  114. index,
  115. data,
  116. size,
  117. USB_CTRL_SET_TIMEOUT);
  118. return len == size ? 0 : -1;
  119. }
  120. static int asix_read_cmd(struct ueth_data *dev, u8 cmd, u16 value, u16 index,
  121. u16 size, void *data)
  122. {
  123. int len;
  124. debug("asix_read_cmd() cmd=0x%02x value=0x%04x index=0x%04x size=%d\n",
  125. cmd, value, index, size);
  126. len = usb_control_msg(
  127. dev->pusb_dev,
  128. usb_rcvctrlpipe(dev->pusb_dev, 0),
  129. cmd,
  130. USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
  131. value,
  132. index,
  133. data,
  134. size,
  135. USB_CTRL_GET_TIMEOUT);
  136. return len == size ? 0 : -1;
  137. }
  138. static inline int asix_set_sw_mii(struct ueth_data *dev)
  139. {
  140. int ret;
  141. ret = asix_write_cmd(dev, AX_CMD_SET_SW_MII, 0x0000, 0, 0, NULL);
  142. if (ret < 0)
  143. debug("Failed to enable software MII access\n");
  144. return ret;
  145. }
  146. static inline int asix_set_hw_mii(struct ueth_data *dev)
  147. {
  148. int ret;
  149. ret = asix_write_cmd(dev, AX_CMD_SET_HW_MII, 0x0000, 0, 0, NULL);
  150. if (ret < 0)
  151. debug("Failed to enable hardware MII access\n");
  152. return ret;
  153. }
  154. static int asix_mdio_read(struct ueth_data *dev, int phy_id, int loc)
  155. {
  156. ALLOC_CACHE_ALIGN_BUFFER(__le16, res, 1);
  157. asix_set_sw_mii(dev);
  158. asix_read_cmd(dev, AX_CMD_READ_MII_REG, phy_id, (__u16)loc, 2, res);
  159. asix_set_hw_mii(dev);
  160. debug("asix_mdio_read() phy_id=0x%02x, loc=0x%02x, returns=0x%04x\n",
  161. phy_id, loc, le16_to_cpu(*res));
  162. return le16_to_cpu(*res);
  163. }
  164. static void
  165. asix_mdio_write(struct ueth_data *dev, int phy_id, int loc, int val)
  166. {
  167. ALLOC_CACHE_ALIGN_BUFFER(__le16, res, 1);
  168. *res = cpu_to_le16(val);
  169. debug("asix_mdio_write() phy_id=0x%02x, loc=0x%02x, val=0x%04x\n",
  170. phy_id, loc, val);
  171. asix_set_sw_mii(dev);
  172. asix_write_cmd(dev, AX_CMD_WRITE_MII_REG, phy_id, (__u16)loc, 2, res);
  173. asix_set_hw_mii(dev);
  174. }
  175. /*
  176. * Asix "high level" commands
  177. */
  178. static int asix_sw_reset(struct ueth_data *dev, u8 flags)
  179. {
  180. int ret;
  181. ret = asix_write_cmd(dev, AX_CMD_SW_RESET, flags, 0, 0, NULL);
  182. if (ret < 0)
  183. debug("Failed to send software reset: %02x\n", ret);
  184. else
  185. udelay(150 * 1000);
  186. return ret;
  187. }
  188. static inline int asix_get_phy_addr(struct ueth_data *dev)
  189. {
  190. ALLOC_CACHE_ALIGN_BUFFER(u8, buf, 2);
  191. int ret = asix_read_cmd(dev, AX_CMD_READ_PHY_ID, 0, 0, 2, buf);
  192. debug("asix_get_phy_addr()\n");
  193. if (ret < 0) {
  194. debug("Error reading PHYID register: %02x\n", ret);
  195. goto out;
  196. }
  197. debug("asix_get_phy_addr() returning 0x%02x%02x\n", buf[0], buf[1]);
  198. ret = buf[1];
  199. out:
  200. return ret;
  201. }
  202. static int asix_write_medium_mode(struct ueth_data *dev, u16 mode)
  203. {
  204. int ret;
  205. debug("asix_write_medium_mode() - mode = 0x%04x\n", mode);
  206. ret = asix_write_cmd(dev, AX_CMD_WRITE_MEDIUM_MODE, mode,
  207. 0, 0, NULL);
  208. if (ret < 0) {
  209. debug("Failed to write Medium Mode mode to 0x%04x: %02x\n",
  210. mode, ret);
  211. }
  212. return ret;
  213. }
  214. static u16 asix_read_rx_ctl(struct ueth_data *dev)
  215. {
  216. ALLOC_CACHE_ALIGN_BUFFER(__le16, v, 1);
  217. int ret = asix_read_cmd(dev, AX_CMD_READ_RX_CTL, 0, 0, 2, v);
  218. if (ret < 0)
  219. debug("Error reading RX_CTL register: %02x\n", ret);
  220. else
  221. ret = le16_to_cpu(*v);
  222. return ret;
  223. }
  224. static int asix_write_rx_ctl(struct ueth_data *dev, u16 mode)
  225. {
  226. int ret;
  227. debug("asix_write_rx_ctl() - mode = 0x%04x\n", mode);
  228. ret = asix_write_cmd(dev, AX_CMD_WRITE_RX_CTL, mode, 0, 0, NULL);
  229. if (ret < 0) {
  230. debug("Failed to write RX_CTL mode to 0x%04x: %02x\n",
  231. mode, ret);
  232. }
  233. return ret;
  234. }
  235. static int asix_write_gpio(struct ueth_data *dev, u16 value, int sleep)
  236. {
  237. int ret;
  238. debug("asix_write_gpio() - value = 0x%04x\n", value);
  239. ret = asix_write_cmd(dev, AX_CMD_WRITE_GPIOS, value, 0, 0, NULL);
  240. if (ret < 0) {
  241. debug("Failed to write GPIO value 0x%04x: %02x\n",
  242. value, ret);
  243. }
  244. if (sleep)
  245. udelay(sleep * 1000);
  246. return ret;
  247. }
  248. static int asix_write_hwaddr_common(struct ueth_data *dev, uint8_t *enetaddr)
  249. {
  250. int ret;
  251. ALLOC_CACHE_ALIGN_BUFFER(unsigned char, buf, ETH_ALEN);
  252. memcpy(buf, enetaddr, ETH_ALEN);
  253. ret = asix_write_cmd(dev, AX_CMD_WRITE_NODE_ID, 0, 0, ETH_ALEN, buf);
  254. if (ret < 0)
  255. debug("Failed to set MAC address: %02x\n", ret);
  256. return ret;
  257. }
  258. /*
  259. * mii commands
  260. */
  261. /*
  262. * mii_nway_restart - restart NWay (autonegotiation) for this interface
  263. *
  264. * Returns 0 on success, negative on error.
  265. */
  266. static int mii_nway_restart(struct ueth_data *dev)
  267. {
  268. int bmcr;
  269. int r = -1;
  270. /* if autoneg is off, it's an error */
  271. bmcr = asix_mdio_read(dev, dev->phy_id, MII_BMCR);
  272. if (bmcr & BMCR_ANENABLE) {
  273. bmcr |= BMCR_ANRESTART;
  274. asix_mdio_write(dev, dev->phy_id, MII_BMCR, bmcr);
  275. r = 0;
  276. }
  277. return r;
  278. }
  279. static int asix_read_mac_common(struct ueth_data *dev,
  280. struct asix_private *priv, uint8_t *enetaddr)
  281. {
  282. ALLOC_CACHE_ALIGN_BUFFER(unsigned char, buf, ETH_ALEN);
  283. int i;
  284. if (priv->flags & FLAG_EEPROM_MAC) {
  285. for (i = 0; i < (ETH_ALEN >> 1); i++) {
  286. if (asix_read_cmd(dev, AX_CMD_READ_EEPROM,
  287. 0x04 + i, 0, 2, buf) < 0) {
  288. debug("Failed to read SROM address 04h.\n");
  289. return -1;
  290. }
  291. memcpy(enetaddr + i * 2, buf, 2);
  292. }
  293. } else {
  294. if (asix_read_cmd(dev, AX_CMD_READ_NODE_ID, 0, 0, ETH_ALEN, buf)
  295. < 0) {
  296. debug("Failed to read MAC address.\n");
  297. return -1;
  298. }
  299. memcpy(enetaddr, buf, ETH_ALEN);
  300. }
  301. return 0;
  302. }
  303. static int asix_basic_reset(struct ueth_data *dev)
  304. {
  305. int embd_phy;
  306. u16 rx_ctl;
  307. if (asix_write_gpio(dev,
  308. AX_GPIO_RSE | AX_GPIO_GPO_2 | AX_GPIO_GPO2EN, 5) < 0)
  309. return -1;
  310. /* 0x10 is the phy id of the embedded 10/100 ethernet phy */
  311. embd_phy = ((asix_get_phy_addr(dev) & 0x1f) == 0x10 ? 1 : 0);
  312. if (asix_write_cmd(dev, AX_CMD_SW_PHY_SELECT,
  313. embd_phy, 0, 0, NULL) < 0) {
  314. debug("Select PHY #1 failed\n");
  315. return -1;
  316. }
  317. if (asix_sw_reset(dev, AX_SWRESET_IPPD | AX_SWRESET_PRL) < 0)
  318. return -1;
  319. if (asix_sw_reset(dev, AX_SWRESET_CLEAR) < 0)
  320. return -1;
  321. if (embd_phy) {
  322. if (asix_sw_reset(dev, AX_SWRESET_IPRL) < 0)
  323. return -1;
  324. } else {
  325. if (asix_sw_reset(dev, AX_SWRESET_PRTE) < 0)
  326. return -1;
  327. }
  328. rx_ctl = asix_read_rx_ctl(dev);
  329. debug("RX_CTL is 0x%04x after software reset\n", rx_ctl);
  330. if (asix_write_rx_ctl(dev, 0x0000) < 0)
  331. return -1;
  332. rx_ctl = asix_read_rx_ctl(dev);
  333. debug("RX_CTL is 0x%04x setting to 0x0000\n", rx_ctl);
  334. dev->phy_id = asix_get_phy_addr(dev);
  335. if (dev->phy_id < 0)
  336. debug("Failed to read phy id\n");
  337. asix_mdio_write(dev, dev->phy_id, MII_BMCR, BMCR_RESET);
  338. asix_mdio_write(dev, dev->phy_id, MII_ADVERTISE,
  339. ADVERTISE_ALL | ADVERTISE_CSMA);
  340. mii_nway_restart(dev);
  341. if (asix_write_medium_mode(dev, AX88772_MEDIUM_DEFAULT) < 0)
  342. return -1;
  343. if (asix_write_cmd(dev, AX_CMD_WRITE_IPG0,
  344. AX88772_IPG0_DEFAULT | AX88772_IPG1_DEFAULT,
  345. AX88772_IPG2_DEFAULT, 0, NULL) < 0) {
  346. debug("Write IPG,IPG1,IPG2 failed\n");
  347. return -1;
  348. }
  349. return 0;
  350. }
  351. static int asix_init_common(struct ueth_data *dev, uint8_t *enetaddr)
  352. {
  353. int timeout = 0;
  354. #define TIMEOUT_RESOLUTION 50 /* ms */
  355. int link_detected;
  356. u32 ctl = AX_DEFAULT_RX_CTL;
  357. debug("** %s()\n", __func__);
  358. if ((dev->pusb_dev->descriptor.idVendor == ASIX_USB_VENDOR_ID) &&
  359. (dev->pusb_dev->descriptor.idProduct == AX88772B_USB_PRODUCT_ID))
  360. ctl |= AX_RX_HEADER_DEFAULT;
  361. if (asix_write_rx_ctl(dev, ctl) < 0)
  362. goto out_err;
  363. if (asix_write_hwaddr_common(dev, enetaddr) < 0)
  364. goto out_err;
  365. do {
  366. link_detected = asix_mdio_read(dev, dev->phy_id, MII_BMSR) &
  367. BMSR_LSTATUS;
  368. if (!link_detected) {
  369. if (timeout == 0)
  370. printf("Waiting for Ethernet connection... ");
  371. udelay(TIMEOUT_RESOLUTION * 1000);
  372. timeout += TIMEOUT_RESOLUTION;
  373. }
  374. } while (!link_detected && timeout < PHY_CONNECT_TIMEOUT);
  375. if (link_detected) {
  376. if (timeout != 0)
  377. printf("done.\n");
  378. } else {
  379. printf("unable to connect.\n");
  380. goto out_err;
  381. }
  382. /*
  383. * Wait some more to avoid timeout on first transfer
  384. * (e.g. EHCI timed out on TD - token=0x8008d80)
  385. */
  386. mdelay(25);
  387. return 0;
  388. out_err:
  389. return -1;
  390. }
  391. static int asix_send_common(struct ueth_data *dev, void *packet, int length)
  392. {
  393. int err;
  394. u32 packet_len;
  395. int actual_len;
  396. ALLOC_CACHE_ALIGN_BUFFER(unsigned char, msg,
  397. PKTSIZE + sizeof(packet_len));
  398. debug("** %s(), len %d\n", __func__, length);
  399. packet_len = (((length) ^ 0x0000ffff) << 16) + (length);
  400. cpu_to_le32s(&packet_len);
  401. memcpy(msg, &packet_len, sizeof(packet_len));
  402. memcpy(msg + sizeof(packet_len), (void *)packet, length);
  403. err = usb_bulk_msg(dev->pusb_dev,
  404. usb_sndbulkpipe(dev->pusb_dev, dev->ep_out),
  405. (void *)msg,
  406. length + sizeof(packet_len),
  407. &actual_len,
  408. USB_BULK_SEND_TIMEOUT);
  409. debug("Tx: len = %zu, actual = %u, err = %d\n",
  410. length + sizeof(packet_len), actual_len, err);
  411. return err;
  412. }
  413. #ifndef CONFIG_DM_ETH
  414. /*
  415. * Asix callbacks
  416. */
  417. static int asix_init(struct eth_device *eth, bd_t *bd)
  418. {
  419. struct ueth_data *dev = (struct ueth_data *)eth->priv;
  420. return asix_init_common(dev, eth->enetaddr);
  421. }
  422. static int asix_send(struct eth_device *eth, void *packet, int length)
  423. {
  424. struct ueth_data *dev = (struct ueth_data *)eth->priv;
  425. return asix_send_common(dev, packet, length);
  426. }
  427. static int asix_recv(struct eth_device *eth)
  428. {
  429. struct ueth_data *dev = (struct ueth_data *)eth->priv;
  430. ALLOC_CACHE_ALIGN_BUFFER(unsigned char, recv_buf, AX_RX_URB_SIZE);
  431. unsigned char *buf_ptr;
  432. int err;
  433. int actual_len;
  434. u32 packet_len;
  435. debug("** %s()\n", __func__);
  436. err = usb_bulk_msg(dev->pusb_dev,
  437. usb_rcvbulkpipe(dev->pusb_dev, dev->ep_in),
  438. (void *)recv_buf,
  439. AX_RX_URB_SIZE,
  440. &actual_len,
  441. USB_BULK_RECV_TIMEOUT);
  442. debug("Rx: len = %u, actual = %u, err = %d\n", AX_RX_URB_SIZE,
  443. actual_len, err);
  444. if (err != 0) {
  445. debug("Rx: failed to receive\n");
  446. return -1;
  447. }
  448. if (actual_len > AX_RX_URB_SIZE) {
  449. debug("Rx: received too many bytes %d\n", actual_len);
  450. return -1;
  451. }
  452. buf_ptr = recv_buf;
  453. while (actual_len > 0) {
  454. /*
  455. * 1st 4 bytes contain the length of the actual data as two
  456. * complementary 16-bit words. Extract the length of the data.
  457. */
  458. if (actual_len < sizeof(packet_len)) {
  459. debug("Rx: incomplete packet length\n");
  460. return -1;
  461. }
  462. memcpy(&packet_len, buf_ptr, sizeof(packet_len));
  463. le32_to_cpus(&packet_len);
  464. if (((~packet_len >> 16) & 0x7ff) != (packet_len & 0x7ff)) {
  465. debug("Rx: malformed packet length: %#x (%#x:%#x)\n",
  466. packet_len, (~packet_len >> 16) & 0x7ff,
  467. packet_len & 0x7ff);
  468. return -1;
  469. }
  470. packet_len = packet_len & 0x7ff;
  471. if (packet_len > actual_len - sizeof(packet_len)) {
  472. debug("Rx: too large packet: %d\n", packet_len);
  473. return -1;
  474. }
  475. if ((dev->pusb_dev->descriptor.idVendor ==
  476. ASIX_USB_VENDOR_ID) &&
  477. (dev->pusb_dev->descriptor.idProduct ==
  478. AX88772B_USB_PRODUCT_ID))
  479. buf_ptr += 2;
  480. /* Notify net stack */
  481. net_process_received_packet(buf_ptr + sizeof(packet_len),
  482. packet_len);
  483. /* Adjust for next iteration. Packets are padded to 16-bits */
  484. if (packet_len & 1)
  485. packet_len++;
  486. actual_len -= sizeof(packet_len) + packet_len;
  487. buf_ptr += sizeof(packet_len) + packet_len;
  488. }
  489. return err;
  490. }
  491. static void asix_halt(struct eth_device *eth)
  492. {
  493. debug("** %s()\n", __func__);
  494. }
  495. static int asix_write_hwaddr(struct eth_device *eth)
  496. {
  497. struct ueth_data *dev = (struct ueth_data *)eth->priv;
  498. return asix_write_hwaddr_common(dev, eth->enetaddr);
  499. }
  500. /*
  501. * Asix probing functions
  502. */
  503. void asix_eth_before_probe(void)
  504. {
  505. curr_eth_dev = 0;
  506. }
  507. struct asix_dongle {
  508. unsigned short vendor;
  509. unsigned short product;
  510. int flags;
  511. };
  512. static const struct asix_dongle asix_dongles[] = {
  513. { 0x05ac, 0x1402, FLAG_TYPE_AX88772 }, /* Apple USB Ethernet Adapter */
  514. { 0x07d1, 0x3c05, FLAG_TYPE_AX88772 }, /* D-Link DUB-E100 H/W Ver B1 */
  515. { 0x2001, 0x1a02, FLAG_TYPE_AX88772 }, /* D-Link DUB-E100 H/W Ver C1 */
  516. /* Cables-to-Go USB Ethernet Adapter */
  517. { 0x0b95, 0x772a, FLAG_TYPE_AX88772 },
  518. { 0x0b95, 0x7720, FLAG_TYPE_AX88772 }, /* Trendnet TU2-ET100 V3.0R */
  519. { 0x0b95, 0x1720, FLAG_TYPE_AX88172 }, /* SMC */
  520. { 0x0db0, 0xa877, FLAG_TYPE_AX88772 }, /* MSI - ASIX 88772a */
  521. { 0x13b1, 0x0018, FLAG_TYPE_AX88172 }, /* Linksys 200M v2.1 */
  522. { 0x1557, 0x7720, FLAG_TYPE_AX88772 }, /* 0Q0 cable ethernet */
  523. /* DLink DUB-E100 H/W Ver B1 Alternate */
  524. { 0x2001, 0x3c05, FLAG_TYPE_AX88772 },
  525. /* ASIX 88772B */
  526. { 0x0b95, 0x772b, FLAG_TYPE_AX88772B | FLAG_EEPROM_MAC },
  527. { 0x0b95, 0x7e2b, FLAG_TYPE_AX88772B },
  528. { 0x0000, 0x0000, FLAG_NONE } /* END - Do not remove */
  529. };
  530. /* Probe to see if a new device is actually an asix device */
  531. int asix_eth_probe(struct usb_device *dev, unsigned int ifnum,
  532. struct ueth_data *ss)
  533. {
  534. struct usb_interface *iface;
  535. struct usb_interface_descriptor *iface_desc;
  536. int ep_in_found = 0, ep_out_found = 0;
  537. int i;
  538. /* let's examine the device now */
  539. iface = &dev->config.if_desc[ifnum];
  540. iface_desc = &dev->config.if_desc[ifnum].desc;
  541. for (i = 0; asix_dongles[i].vendor != 0; i++) {
  542. if (dev->descriptor.idVendor == asix_dongles[i].vendor &&
  543. dev->descriptor.idProduct == asix_dongles[i].product)
  544. /* Found a supported dongle */
  545. break;
  546. }
  547. if (asix_dongles[i].vendor == 0)
  548. return 0;
  549. memset(ss, 0, sizeof(struct ueth_data));
  550. /* At this point, we know we've got a live one */
  551. debug("\n\nUSB Ethernet device detected: %#04x:%#04x\n",
  552. dev->descriptor.idVendor, dev->descriptor.idProduct);
  553. /* Initialize the ueth_data structure with some useful info */
  554. ss->ifnum = ifnum;
  555. ss->pusb_dev = dev;
  556. ss->subclass = iface_desc->bInterfaceSubClass;
  557. ss->protocol = iface_desc->bInterfaceProtocol;
  558. /* alloc driver private */
  559. ss->dev_priv = calloc(1, sizeof(struct asix_private));
  560. if (!ss->dev_priv)
  561. return 0;
  562. ((struct asix_private *)ss->dev_priv)->flags = asix_dongles[i].flags;
  563. /*
  564. * We are expecting a minimum of 3 endpoints - in, out (bulk), and
  565. * int. We will ignore any others.
  566. */
  567. for (i = 0; i < iface_desc->bNumEndpoints; i++) {
  568. /* is it an BULK endpoint? */
  569. if ((iface->ep_desc[i].bmAttributes &
  570. USB_ENDPOINT_XFERTYPE_MASK) == USB_ENDPOINT_XFER_BULK) {
  571. u8 ep_addr = iface->ep_desc[i].bEndpointAddress;
  572. if (ep_addr & USB_DIR_IN) {
  573. if (!ep_in_found) {
  574. ss->ep_in = ep_addr &
  575. USB_ENDPOINT_NUMBER_MASK;
  576. ep_in_found = 1;
  577. }
  578. } else {
  579. if (!ep_out_found) {
  580. ss->ep_out = ep_addr &
  581. USB_ENDPOINT_NUMBER_MASK;
  582. ep_out_found = 1;
  583. }
  584. }
  585. }
  586. /* is it an interrupt endpoint? */
  587. if ((iface->ep_desc[i].bmAttributes &
  588. USB_ENDPOINT_XFERTYPE_MASK) == USB_ENDPOINT_XFER_INT) {
  589. ss->ep_int = iface->ep_desc[i].bEndpointAddress &
  590. USB_ENDPOINT_NUMBER_MASK;
  591. ss->irqinterval = iface->ep_desc[i].bInterval;
  592. }
  593. }
  594. debug("Endpoints In %d Out %d Int %d\n",
  595. ss->ep_in, ss->ep_out, ss->ep_int);
  596. /* Do some basic sanity checks, and bail if we find a problem */
  597. if (usb_set_interface(dev, iface_desc->bInterfaceNumber, 0) ||
  598. !ss->ep_in || !ss->ep_out || !ss->ep_int) {
  599. debug("Problems with device\n");
  600. return 0;
  601. }
  602. dev->privptr = (void *)ss;
  603. return 1;
  604. }
  605. int asix_eth_get_info(struct usb_device *dev, struct ueth_data *ss,
  606. struct eth_device *eth)
  607. {
  608. struct asix_private *priv = (struct asix_private *)ss->dev_priv;
  609. if (!eth) {
  610. debug("%s: missing parameter.\n", __func__);
  611. return 0;
  612. }
  613. sprintf(eth->name, "%s%d", ASIX_BASE_NAME, curr_eth_dev++);
  614. eth->init = asix_init;
  615. eth->send = asix_send;
  616. eth->recv = asix_recv;
  617. eth->halt = asix_halt;
  618. if (!(priv->flags & FLAG_TYPE_AX88172))
  619. eth->write_hwaddr = asix_write_hwaddr;
  620. eth->priv = ss;
  621. if (asix_basic_reset(ss))
  622. return 0;
  623. /* Get the MAC address */
  624. if (asix_read_mac_common(ss, priv, eth->enetaddr))
  625. return 0;
  626. debug("MAC %pM\n", eth->enetaddr);
  627. return 1;
  628. }
  629. #endif
  630. #ifdef CONFIG_DM_ETH
  631. static int asix_eth_start(struct udevice *dev)
  632. {
  633. struct eth_pdata *pdata = dev_get_platdata(dev);
  634. struct asix_private *priv = dev_get_priv(dev);
  635. return asix_init_common(&priv->ueth, pdata->enetaddr);
  636. }
  637. void asix_eth_stop(struct udevice *dev)
  638. {
  639. debug("** %s()\n", __func__);
  640. }
  641. int asix_eth_send(struct udevice *dev, void *packet, int length)
  642. {
  643. struct asix_private *priv = dev_get_priv(dev);
  644. return asix_send_common(&priv->ueth, packet, length);
  645. }
  646. int asix_eth_recv(struct udevice *dev, int flags, uchar **packetp)
  647. {
  648. struct asix_private *priv = dev_get_priv(dev);
  649. struct ueth_data *ueth = &priv->ueth;
  650. uint8_t *ptr;
  651. int ret, len;
  652. u32 packet_len;
  653. len = usb_ether_get_rx_bytes(ueth, &ptr);
  654. debug("%s: first try, len=%d\n", __func__, len);
  655. if (!len) {
  656. if (!(flags & ETH_RECV_CHECK_DEVICE))
  657. return -EAGAIN;
  658. ret = usb_ether_receive(ueth, AX_RX_URB_SIZE);
  659. if (ret == -EAGAIN)
  660. return ret;
  661. len = usb_ether_get_rx_bytes(ueth, &ptr);
  662. debug("%s: second try, len=%d\n", __func__, len);
  663. }
  664. /*
  665. * 1st 4 bytes contain the length of the actual data as two
  666. * complementary 16-bit words. Extract the length of the data.
  667. */
  668. if (len < sizeof(packet_len)) {
  669. debug("Rx: incomplete packet length\n");
  670. goto err;
  671. }
  672. memcpy(&packet_len, ptr, sizeof(packet_len));
  673. le32_to_cpus(&packet_len);
  674. if (((~packet_len >> 16) & 0x7ff) != (packet_len & 0x7ff)) {
  675. debug("Rx: malformed packet length: %#x (%#x:%#x)\n",
  676. packet_len, (~packet_len >> 16) & 0x7ff,
  677. packet_len & 0x7ff);
  678. goto err;
  679. }
  680. packet_len = packet_len & 0x7ff;
  681. if (packet_len > len - sizeof(packet_len)) {
  682. debug("Rx: too large packet: %d\n", packet_len);
  683. goto err;
  684. }
  685. *packetp = ptr + sizeof(packet_len);
  686. return packet_len;
  687. err:
  688. usb_ether_advance_rxbuf(ueth, -1);
  689. return -EINVAL;
  690. }
  691. static int asix_free_pkt(struct udevice *dev, uchar *packet, int packet_len)
  692. {
  693. struct asix_private *priv = dev_get_priv(dev);
  694. if (packet_len & 1)
  695. packet_len++;
  696. usb_ether_advance_rxbuf(&priv->ueth, sizeof(u32) + packet_len);
  697. return 0;
  698. }
  699. int asix_write_hwaddr(struct udevice *dev)
  700. {
  701. struct eth_pdata *pdata = dev_get_platdata(dev);
  702. struct asix_private *priv = dev_get_priv(dev);
  703. if (priv->flags & FLAG_TYPE_AX88172)
  704. return -ENOSYS;
  705. return asix_write_hwaddr_common(&priv->ueth, pdata->enetaddr);
  706. }
  707. static int asix_eth_probe(struct udevice *dev)
  708. {
  709. struct eth_pdata *pdata = dev_get_platdata(dev);
  710. struct asix_private *priv = dev_get_priv(dev);
  711. struct ueth_data *ss = &priv->ueth;
  712. int ret;
  713. priv->flags = dev->driver_data;
  714. ret = usb_ether_register(dev, ss, AX_RX_URB_SIZE);
  715. if (ret)
  716. return ret;
  717. ret = asix_basic_reset(ss);
  718. if (ret)
  719. goto err;
  720. /* Get the MAC address */
  721. ret = asix_read_mac_common(ss, priv, pdata->enetaddr);
  722. if (ret)
  723. goto err;
  724. debug("MAC %pM\n", pdata->enetaddr);
  725. return 0;
  726. err:
  727. return usb_ether_deregister(ss);
  728. }
  729. static const struct eth_ops asix_eth_ops = {
  730. .start = asix_eth_start,
  731. .send = asix_eth_send,
  732. .recv = asix_eth_recv,
  733. .free_pkt = asix_free_pkt,
  734. .stop = asix_eth_stop,
  735. .write_hwaddr = asix_write_hwaddr,
  736. };
  737. U_BOOT_DRIVER(asix_eth) = {
  738. .name = "asix_eth",
  739. .id = UCLASS_ETH,
  740. .probe = asix_eth_probe,
  741. .ops = &asix_eth_ops,
  742. .priv_auto_alloc_size = sizeof(struct asix_private),
  743. .platdata_auto_alloc_size = sizeof(struct eth_pdata),
  744. };
  745. static const struct usb_device_id asix_eth_id_table[] = {
  746. /* Apple USB Ethernet Adapter */
  747. { USB_DEVICE(0x05ac, 0x1402), .driver_info = FLAG_TYPE_AX88772 },
  748. /* D-Link DUB-E100 H/W Ver B1 */
  749. { USB_DEVICE(0x07d1, 0x3c05), .driver_info = FLAG_TYPE_AX88772 },
  750. /* D-Link DUB-E100 H/W Ver C1 */
  751. { USB_DEVICE(0x2001, 0x1a02), .driver_info = FLAG_TYPE_AX88772 },
  752. /* Cables-to-Go USB Ethernet Adapter */
  753. { USB_DEVICE(0x0b95, 0x772a), .driver_info = FLAG_TYPE_AX88772 },
  754. /* Trendnet TU2-ET100 V3.0R */
  755. { USB_DEVICE(0x0b95, 0x7720), .driver_info = FLAG_TYPE_AX88772 },
  756. /* SMC */
  757. { USB_DEVICE(0x0b95, 0x1720), .driver_info = FLAG_TYPE_AX88172 },
  758. /* MSI - ASIX 88772a */
  759. { USB_DEVICE(0x0db0, 0xa877), .driver_info = FLAG_TYPE_AX88772 },
  760. /* Linksys 200M v2.1 */
  761. { USB_DEVICE(0x13b1, 0x0018), .driver_info = FLAG_TYPE_AX88172 },
  762. /* 0Q0 cable ethernet */
  763. { USB_DEVICE(0x1557, 0x7720), .driver_info = FLAG_TYPE_AX88772 },
  764. /* DLink DUB-E100 H/W Ver B1 Alternate */
  765. { USB_DEVICE(0x2001, 0x3c05), .driver_info = FLAG_TYPE_AX88772 },
  766. /* ASIX 88772B */
  767. { USB_DEVICE(0x0b95, 0x772b),
  768. .driver_info = FLAG_TYPE_AX88772B | FLAG_EEPROM_MAC },
  769. { USB_DEVICE(0x0b95, 0x7e2b), .driver_info = FLAG_TYPE_AX88772B },
  770. { } /* Terminating entry */
  771. };
  772. U_BOOT_USB_DEVICE(asix_eth, asix_eth_id_table);
  773. #endif