asix.c 22 KB

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