asix.c 22 KB

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