usbh_asix.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825
  1. /*
  2. * Copyright (c) 2024, sakumisu
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #include "usbh_core.h"
  7. #include "usbh_asix.h"
  8. #include "usb_cdc.h"
  9. #undef USB_DBG_TAG
  10. #define USB_DBG_TAG "asix"
  11. #include "usb_log.h"
  12. #define DEV_FORMAT "/dev/asix"
  13. static struct usbh_asix g_asix_class;
  14. static USB_NOCACHE_RAM_SECTION USB_MEM_ALIGNX uint8_t g_asix_rx_buffer[CONFIG_USBHOST_ASIX_ETH_MAX_TX_SIZE];
  15. static USB_NOCACHE_RAM_SECTION USB_MEM_ALIGNX uint8_t g_asix_tx_buffer[CONFIG_USBHOST_ASIX_ETH_MAX_RX_SIZE];
  16. static USB_NOCACHE_RAM_SECTION USB_MEM_ALIGNX uint8_t g_asix_inttx_buffer[16];
  17. USB_NOCACHE_RAM_SECTION USB_MEM_ALIGNX uint8_t g_asix_buf[32];
  18. #define ETH_ALEN 6
  19. #define PHY_MODE_MARVELL 0x0000
  20. #define MII_MARVELL_LED_CTRL 0x0018
  21. #define MII_MARVELL_STATUS 0x001b
  22. #define MII_MARVELL_CTRL 0x0014
  23. #define MARVELL_LED_MANUAL 0x0019
  24. #define MARVELL_STATUS_HWCFG 0x0004
  25. #define MARVELL_CTRL_TXDELAY 0x0002
  26. #define MARVELL_CTRL_RXDELAY 0x0080
  27. #define PHY_MODE_RTL8211CL 0x000C
  28. #define AX88772A_PHY14H 0x14
  29. #define AX88772A_PHY14H_DEFAULT 0x442C
  30. #define AX88772A_PHY15H 0x15
  31. #define AX88772A_PHY15H_DEFAULT 0x03C8
  32. #define AX88772A_PHY16H 0x16
  33. #define AX88772A_PHY16H_DEFAULT 0x4044
  34. #define SPEED_100 0
  35. #define SPEED_10 1
  36. static int usbh_asix_read_cmd(struct usbh_asix *asix_class,
  37. uint8_t cmd,
  38. uint16_t value,
  39. uint16_t index,
  40. void *data,
  41. uint16_t size)
  42. {
  43. struct usb_setup_packet *setup;
  44. int ret;
  45. if (!asix_class || !asix_class->hport) {
  46. return -USB_ERR_INVAL;
  47. }
  48. setup = asix_class->hport->setup;
  49. setup->bmRequestType = USB_REQUEST_DIR_IN | USB_REQUEST_VENDOR | USB_REQUEST_RECIPIENT_DEVICE;
  50. setup->bRequest = cmd;
  51. setup->wValue = value;
  52. setup->wIndex = index;
  53. setup->wLength = size;
  54. ret = usbh_control_transfer(asix_class->hport, setup, g_asix_buf);
  55. if (ret < 8) {
  56. return ret;
  57. }
  58. memcpy(data, g_asix_buf, ret - 8);
  59. return ret;
  60. }
  61. static int usbh_asix_write_cmd(struct usbh_asix *asix_class,
  62. uint8_t cmd,
  63. uint16_t value,
  64. uint16_t index,
  65. void *data,
  66. uint16_t size)
  67. {
  68. struct usb_setup_packet *setup;
  69. if (!asix_class || !asix_class->hport) {
  70. return -USB_ERR_INVAL;
  71. }
  72. setup = asix_class->hport->setup;
  73. setup->bmRequestType = USB_REQUEST_DIR_OUT | USB_REQUEST_VENDOR | USB_REQUEST_RECIPIENT_DEVICE;
  74. setup->bRequest = cmd;
  75. setup->wValue = value;
  76. setup->wIndex = index;
  77. setup->wLength = size;
  78. if (data && size) {
  79. memcpy(g_asix_buf, data, size);
  80. return usbh_control_transfer(asix_class->hport, setup, g_asix_buf);
  81. } else {
  82. return usbh_control_transfer(asix_class->hport, setup, NULL);
  83. }
  84. }
  85. static int usbh_asix_mdio_write(struct usbh_asix *asix_class, int phy_id, int loc, int val)
  86. {
  87. uint8_t smsr;
  88. uint16_t res = (uint16_t)val;
  89. int ret;
  90. for (uint8_t i = 0; i < 10; i++) {
  91. ret = usbh_asix_write_cmd(asix_class, AX_CMD_SET_SW_MII, 0, 0, NULL, 0);
  92. if (ret < 0) {
  93. return ret;
  94. }
  95. usb_osal_msleep(1);
  96. ret = usbh_asix_read_cmd(asix_class, AX_CMD_STATMNGSTS_REG, 0, 0, &smsr, 1);
  97. if (ret < 0) {
  98. return ret;
  99. }
  100. if (smsr & AX_HOST_EN) {
  101. break;
  102. }
  103. }
  104. ret = usbh_asix_write_cmd(asix_class, AX_CMD_WRITE_MII_REG, phy_id, loc, &res, 2);
  105. if (ret < 0) {
  106. return ret;
  107. }
  108. ret = usbh_asix_write_cmd(asix_class, AX_CMD_SET_HW_MII, 0, 0, NULL, 0);
  109. if (ret < 0) {
  110. return ret;
  111. }
  112. return 0;
  113. }
  114. static int usbh_asix_mdio_read(struct usbh_asix *asix_class, int phy_id, int loc)
  115. {
  116. uint8_t smsr;
  117. uint16_t res;
  118. int ret;
  119. for (uint8_t i = 0; i < 10; i++) {
  120. ret = usbh_asix_write_cmd(asix_class, AX_CMD_SET_SW_MII, 0, 0, NULL, 0);
  121. if (ret < 0) {
  122. return ret;
  123. }
  124. usb_osal_msleep(1);
  125. ret = usbh_asix_read_cmd(asix_class, AX_CMD_STATMNGSTS_REG, 0, 0, &smsr, 1);
  126. if (ret < 0) {
  127. return ret;
  128. }
  129. if (smsr & AX_HOST_EN) {
  130. break;
  131. }
  132. }
  133. ret = usbh_asix_read_cmd(asix_class, AX_CMD_READ_MII_REG, phy_id, loc, &res, 2);
  134. if (ret < 0) {
  135. return ret;
  136. }
  137. ret = usbh_asix_write_cmd(asix_class, AX_CMD_SET_HW_MII, 0, 0, NULL, 0);
  138. if (ret < 0) {
  139. return ret;
  140. }
  141. return res;
  142. }
  143. static int usbh_asix_read_phy_addr(struct usbh_asix *asix_class, bool internal)
  144. {
  145. int ret, offset;
  146. uint8_t buf[2];
  147. ret = usbh_asix_read_cmd(asix_class, AX_CMD_READ_PHY_ID, 0, 0, buf, 2);
  148. if (ret < 0) {
  149. return ret;
  150. }
  151. offset = (internal ? 1 : 0);
  152. ret = buf[offset];
  153. USB_LOG_INFO("%s PHY address 0x%x\r\n", internal ? "internal" : "external", ret);
  154. return ret;
  155. }
  156. static int usbh_asix_sw_reset(struct usbh_asix *asix_class, uint8_t flags)
  157. {
  158. int ret;
  159. ret = usbh_asix_write_cmd(asix_class, AX_CMD_SW_RESET, flags, 0, NULL, 0);
  160. if (ret < 0)
  161. USB_LOG_ERR("Failed to send software reset: %d\r\n", ret);
  162. return ret;
  163. }
  164. static uint16_t usbh_asix_read_rx_ctl(struct usbh_asix *asix_class)
  165. {
  166. uint16_t v;
  167. int ret = usbh_asix_read_cmd(asix_class, AX_CMD_READ_RX_CTL, 0, 0, &v, 2);
  168. if (ret < 0) {
  169. return ret;
  170. }
  171. return v;
  172. }
  173. static int usbh_asix_write_rx_ctl(struct usbh_asix *asix_class, uint16_t mode)
  174. {
  175. int ret;
  176. USB_LOG_DBG("asix_write_rx_ctl() - mode = 0x%04x\r\n", mode);
  177. ret = usbh_asix_write_cmd(asix_class, AX_CMD_WRITE_RX_CTL, mode, 0, NULL, 0);
  178. if (ret < 0)
  179. USB_LOG_ERR("Failed to write RX_CTL mode to 0x%04x: %02x\r\n",
  180. mode, ret);
  181. return ret;
  182. }
  183. static uint16_t usbh_asix_read_medium_status(struct usbh_asix *asix_class)
  184. {
  185. uint16_t v;
  186. int ret = usbh_asix_read_cmd(asix_class, AX_CMD_READ_MEDIUM_STATUS, 0, 0, &v, 2);
  187. if (ret < 0) {
  188. USB_LOG_ERR("Error reading Medium Status register: %02x\r\n",
  189. ret);
  190. return ret; /* TODO: callers not checking for error ret */
  191. }
  192. return v;
  193. }
  194. static int usbh_asix_write_medium_mode(struct usbh_asix *asix_class, uint16_t mode)
  195. {
  196. int ret;
  197. USB_LOG_DBG("asix_write_medium_mode() - mode = 0x%04x\r\n", mode);
  198. ret = usbh_asix_write_cmd(asix_class, AX_CMD_WRITE_MEDIUM_MODE, mode, 0, NULL, 0);
  199. if (ret < 0)
  200. USB_LOG_ERR("Failed to write Medium Mode mode to 0x%04x: %02x\r\n",
  201. mode, ret);
  202. return ret;
  203. }
  204. static int usbh_asix_write_gpio(struct usbh_asix *asix_class, uint16_t value, int sleep)
  205. {
  206. int ret;
  207. USB_LOG_DBG("asix_write_gpio() - value = 0x%04x\r\n", value);
  208. ret = usbh_asix_write_cmd(asix_class, AX_CMD_WRITE_GPIOS, value, 0, NULL, 0);
  209. if (ret < 0)
  210. USB_LOG_ERR("Failed to write GPIO value 0x%04x: %d\r\n",
  211. value, ret);
  212. if (sleep)
  213. usb_osal_msleep(sleep);
  214. return ret;
  215. }
  216. /*
  217. * AX88772 & AX88178 have a 16-bit RX_CTL value
  218. */
  219. static void usbh_asix_set_multicast(struct usbh_asix *asix_class)
  220. {
  221. uint16_t rx_ctl = AX_DEFAULT_RX_CTL | AX_RX_CTL_AM;
  222. #if CONFIG_USBHOST_ASIX_ETH_MAX_RX_SIZE == 4096
  223. rx_ctl |= AX_RX_CTL_MFB_4096;
  224. #elif CONFIG_USBHOST_ASIX_ETH_MAX_RX_SIZE == 8192
  225. rx_ctl |= AX_RX_CTL_MFB_8192;
  226. #elif CONFIG_USBHOST_ASIX_ETH_MAX_RX_SIZE == 16384
  227. rx_ctl |= AX_RX_CTL_MFB_16384;
  228. #else
  229. rx_ctl |= AX_RX_CTL_MFB_2048;
  230. #endif
  231. const uint8_t multi_filter[] = { 0x00, 0x00, 0x20, 0x80, 0x00, 0x00, 0x00, 0x40 };
  232. usbh_asix_write_cmd(asix_class, AX_CMD_WRITE_MULTI_FILTER, 0, 0, (uint8_t *)multi_filter, AX_MCAST_FILTER_SIZE);
  233. usbh_asix_write_cmd(asix_class, AX_CMD_WRITE_RX_CTL, rx_ctl, 0, NULL, 0);
  234. }
  235. static int usbh_ax88772_hw_reset(struct usbh_asix *asix_class)
  236. {
  237. uint16_t rx_ctl;
  238. int ret;
  239. ret = usbh_asix_write_gpio(asix_class, AX_GPIO_RSE | AX_GPIO_GPO_2 | AX_GPIO_GPO2EN, 5);
  240. if (ret < 0)
  241. goto out;
  242. ret = usbh_asix_write_cmd(asix_class, AX_CMD_SW_PHY_SELECT, asix_class->embd_phy,
  243. 0, NULL, 0);
  244. if (ret < 0) {
  245. USB_LOG_ERR("Select PHY #1 failed: %d\r\n", ret);
  246. goto out;
  247. }
  248. if (asix_class->embd_phy) {
  249. ret = usbh_asix_sw_reset(asix_class, AX_SWRESET_IPPD);
  250. if (ret < 0)
  251. goto out;
  252. usb_osal_msleep(10);
  253. ret = usbh_asix_sw_reset(asix_class, AX_SWRESET_CLEAR);
  254. if (ret < 0)
  255. goto out;
  256. usb_osal_msleep(60);
  257. ret = usbh_asix_sw_reset(asix_class, AX_SWRESET_IPRL | AX_SWRESET_PRL);
  258. if (ret < 0)
  259. goto out;
  260. } else {
  261. ret = usbh_asix_sw_reset(asix_class, AX_SWRESET_IPPD | AX_SWRESET_PRL);
  262. if (ret < 0)
  263. goto out;
  264. }
  265. usb_osal_msleep(150);
  266. ret = usbh_asix_write_rx_ctl(asix_class, AX_DEFAULT_RX_CTL);
  267. if (ret < 0)
  268. goto out;
  269. ret = usbh_asix_write_medium_mode(asix_class, AX88772_MEDIUM_DEFAULT);
  270. if (ret < 0)
  271. goto out;
  272. ret = usbh_asix_write_cmd(asix_class, AX_CMD_WRITE_IPG0,
  273. AX88772_IPG0_DEFAULT | AX88772_IPG1_DEFAULT,
  274. AX88772_IPG2_DEFAULT, NULL, 0);
  275. if (ret < 0) {
  276. USB_LOG_ERR("Write IPG,IPG1,IPG2 failed: %d\r\n", ret);
  277. goto out;
  278. }
  279. /* Rewrite MAC address */
  280. ret = usbh_asix_write_cmd(asix_class, AX_CMD_WRITE_NODE_ID, 0, 0, asix_class->mac, ETH_ALEN);
  281. if (ret < 0)
  282. goto out;
  283. /* Set RX_CTL to default values with 2k buffer, and enable cactus */
  284. ret = usbh_asix_write_rx_ctl(asix_class, AX_DEFAULT_RX_CTL);
  285. if (ret < 0)
  286. goto out;
  287. rx_ctl = usbh_asix_read_rx_ctl(asix_class);
  288. USB_LOG_INFO("RX_CTL is 0x%04x after all initializations\r\n",
  289. rx_ctl);
  290. rx_ctl = usbh_asix_read_medium_status(asix_class);
  291. USB_LOG_INFO("Medium Status is 0x%04x after all initializations\r\n",
  292. rx_ctl);
  293. return 0;
  294. out:
  295. return ret;
  296. }
  297. static int usbh_ax88772a_hw_reset(struct usbh_asix *asix_class)
  298. {
  299. uint16_t rx_ctl, phy14h, phy15h, phy16h;
  300. int ret;
  301. ret = usbh_asix_write_gpio(asix_class, AX_GPIO_RSE, 5);
  302. if (ret < 0)
  303. goto out;
  304. ret = usbh_asix_write_cmd(asix_class, AX_CMD_SW_PHY_SELECT, asix_class->embd_phy | AX_PHYSEL_SSEN, 0, NULL, 0);
  305. if (ret < 0) {
  306. USB_LOG_ERR("Select PHY #1 failed: %d\r\n", ret);
  307. goto out;
  308. }
  309. usb_osal_msleep(10);
  310. ret = usbh_asix_sw_reset(asix_class, AX_SWRESET_IPPD | AX_SWRESET_IPRL);
  311. if (ret < 0)
  312. goto out;
  313. usb_osal_msleep(10);
  314. ret = usbh_asix_sw_reset(asix_class, AX_SWRESET_IPRL);
  315. if (ret < 0)
  316. goto out;
  317. usb_osal_msleep(160);
  318. ret = usbh_asix_sw_reset(asix_class, AX_SWRESET_CLEAR);
  319. if (ret < 0)
  320. goto out;
  321. ret = usbh_asix_sw_reset(asix_class, AX_SWRESET_IPRL);
  322. if (ret < 0)
  323. goto out;
  324. usb_osal_msleep(200);
  325. if (asix_class->chipcode == AX_AX88772B_CHIPCODE) {
  326. ret = usbh_asix_write_cmd(asix_class, AX_QCTCTRL, 0x8000, 0x8001, NULL, 0);
  327. if (ret < 0) {
  328. USB_LOG_ERR("Write BQ setting failed: %d\r\n", ret);
  329. goto out;
  330. }
  331. } else if (asix_class->chipcode == AX_AX88772A_CHIPCODE) {
  332. /* Check if the PHY registers have default settings */
  333. phy14h = usbh_asix_mdio_read(asix_class, asix_class->phy_addr,
  334. AX88772A_PHY14H);
  335. phy15h = usbh_asix_mdio_read(asix_class, asix_class->phy_addr,
  336. AX88772A_PHY15H);
  337. phy16h = usbh_asix_mdio_read(asix_class, asix_class->phy_addr,
  338. AX88772A_PHY16H);
  339. USB_LOG_DBG("772a_hw_reset: MR20=0x%x MR21=0x%x MR22=0x%x\r\n",
  340. phy14h, phy15h, phy16h);
  341. /* Restore PHY registers default setting if not */
  342. if (phy14h != AX88772A_PHY14H_DEFAULT)
  343. usbh_asix_mdio_write(asix_class, asix_class->phy_addr,
  344. AX88772A_PHY14H,
  345. AX88772A_PHY14H_DEFAULT);
  346. if (phy15h != AX88772A_PHY15H_DEFAULT)
  347. usbh_asix_mdio_write(asix_class, asix_class->phy_addr,
  348. AX88772A_PHY15H,
  349. AX88772A_PHY15H_DEFAULT);
  350. if (phy16h != AX88772A_PHY16H_DEFAULT)
  351. usbh_asix_mdio_write(asix_class, asix_class->phy_addr,
  352. AX88772A_PHY16H,
  353. AX88772A_PHY16H_DEFAULT);
  354. }
  355. ret = usbh_asix_write_cmd(asix_class, AX_CMD_WRITE_IPG0,
  356. AX88772_IPG0_DEFAULT | AX88772_IPG1_DEFAULT,
  357. AX88772_IPG2_DEFAULT, NULL, 0);
  358. if (ret < 0) {
  359. USB_LOG_ERR("Write IPG,IPG1,IPG2 failed: %d\r\n", ret);
  360. goto out;
  361. }
  362. /* Rewrite MAC address */
  363. ret = usbh_asix_write_cmd(asix_class, AX_CMD_WRITE_NODE_ID, 0, 0, asix_class->mac, ETH_ALEN);
  364. if (ret < 0)
  365. goto out;
  366. /* Set RX_CTL to default values with 2k buffer, and enable cactus */
  367. ret = usbh_asix_write_rx_ctl(asix_class, AX_DEFAULT_RX_CTL);
  368. if (ret < 0)
  369. goto out;
  370. ret = usbh_asix_write_medium_mode(asix_class, AX88772_MEDIUM_DEFAULT);
  371. if (ret < 0)
  372. return ret;
  373. /* Set RX_CTL to default values with 2k buffer, and enable cactus */
  374. ret = usbh_asix_write_rx_ctl(asix_class, AX_DEFAULT_RX_CTL);
  375. if (ret < 0)
  376. goto out;
  377. rx_ctl = usbh_asix_read_rx_ctl(asix_class);
  378. USB_LOG_INFO("RX_CTL is 0x%04x after all initializations\r\n", rx_ctl);
  379. rx_ctl = usbh_asix_read_medium_status(asix_class);
  380. USB_LOG_INFO("Medium Status is 0x%04x after all initializations\r\n", rx_ctl);
  381. return 0;
  382. out:
  383. return ret;
  384. }
  385. static void usbh_ax88772_mac_link_down(struct usbh_asix *asix_class)
  386. {
  387. usbh_asix_write_medium_mode(asix_class, 0);
  388. }
  389. static void usbh_ax88772_mac_link_up(struct usbh_asix *asix_class, int speed, int duplex, bool tx_pause, bool rx_pause)
  390. {
  391. uint16_t m = AX_MEDIUM_AC | AX_MEDIUM_RE;
  392. m |= duplex ? AX_MEDIUM_FD : 0;
  393. switch (speed) {
  394. case SPEED_100:
  395. m |= AX_MEDIUM_PS;
  396. break;
  397. case SPEED_10:
  398. break;
  399. default:
  400. return;
  401. }
  402. if (tx_pause)
  403. m |= AX_MEDIUM_TFC;
  404. if (rx_pause)
  405. m |= AX_MEDIUM_RFC;
  406. usbh_asix_write_medium_mode(asix_class, m);
  407. }
  408. static int usbh_asix_connect(struct usbh_hubport *hport, uint8_t intf)
  409. {
  410. struct usb_endpoint_descriptor *ep_desc;
  411. int ret;
  412. struct usbh_asix *asix_class = &g_asix_class;
  413. memset(asix_class, 0, sizeof(struct usbh_asix));
  414. asix_class->hport = hport;
  415. asix_class->intf = intf;
  416. hport->config.intf[intf].priv = asix_class;
  417. if ((hport->device_desc.idVendor == 0x0b95) && (hport->device_desc.idProduct == 0x772b)) {
  418. asix_class->name = "ASIX AX88772B";
  419. } else if ((hport->device_desc.idVendor == 0x0b95) && (hport->device_desc.idProduct == 0x7720)) {
  420. asix_class->name = "ASIX AX88772";
  421. } else if ((hport->device_desc.idVendor == 0x0b95) && (hport->device_desc.idProduct == 0x1780)) {
  422. asix_class->name = "ASIX AX88178";
  423. }
  424. for (uint8_t i = 0; i < (ETH_ALEN >> 1); i++) {
  425. ret = usbh_asix_read_cmd(asix_class, AX_CMD_READ_EEPROM,
  426. 0x04 + i, 0, &asix_class->mac[i * 2], 2);
  427. if (ret < 0) {
  428. return ret;
  429. }
  430. }
  431. USB_LOG_INFO("asix MAC address %02x:%02x:%02x:%02x:%02x:%02x\r\n",
  432. asix_class->mac[0],
  433. asix_class->mac[1],
  434. asix_class->mac[2],
  435. asix_class->mac[3],
  436. asix_class->mac[4],
  437. asix_class->mac[5]);
  438. ret = usbh_asix_read_phy_addr(asix_class, true);
  439. if (ret < 0) {
  440. USB_LOG_ERR("Failed to read phy addr: %d\r\n", ret);
  441. return ret;
  442. }
  443. asix_class->phy_addr = ret;
  444. asix_class->embd_phy = ((ret & 0x1f) == AX_EMBD_PHY_ADDR);
  445. ret = usbh_asix_read_cmd(asix_class, AX_CMD_STATMNGSTS_REG, 0, 0, &asix_class->chipcode, 1);
  446. if (ret < 0) {
  447. USB_LOG_ERR("Failed to read STATMNGSTS_REG: %d\r\n", ret);
  448. return ret;
  449. }
  450. asix_class->chipcode &= AX_CHIPCODE_MASK;
  451. USB_LOG_INFO("asix chipcode 0x%x\r\n", asix_class->chipcode);
  452. if (asix_class->chipcode == AX_AX88772_CHIPCODE) {
  453. usbh_ax88772_hw_reset(asix_class);
  454. } else {
  455. usbh_ax88772a_hw_reset(asix_class);
  456. }
  457. for (uint8_t i = 0; i < hport->config.intf[intf].altsetting[0].intf_desc.bNumEndpoints; i++) {
  458. ep_desc = &hport->config.intf[intf].altsetting[0].ep[i].ep_desc;
  459. if (USB_GET_ENDPOINT_TYPE(ep_desc->bmAttributes) == USB_ENDPOINT_TYPE_INTERRUPT) {
  460. if (ep_desc->bEndpointAddress & 0x80) {
  461. USBH_EP_INIT(asix_class->intin, ep_desc);
  462. } else {
  463. return -USB_ERR_NOTSUPP;
  464. }
  465. } else {
  466. if (ep_desc->bEndpointAddress & 0x80) {
  467. USBH_EP_INIT(asix_class->bulkin, ep_desc);
  468. } else {
  469. USBH_EP_INIT(asix_class->bulkout, ep_desc);
  470. }
  471. }
  472. }
  473. if (asix_class->chipcode == AX_AX88772B_CHIPCODE) {
  474. usbh_asix_mdio_write(asix_class, asix_class->phy_addr, 0, 0);
  475. usbh_asix_mdio_read(asix_class, asix_class->phy_addr, 0);
  476. usbh_asix_mdio_write(asix_class, asix_class->phy_addr, 0, 0x8200);
  477. usbh_asix_mdio_read(asix_class, asix_class->phy_addr, 0);
  478. usbh_asix_mdio_write(asix_class, asix_class->phy_addr, 0, 0x3900);
  479. usbh_asix_mdio_read(asix_class, asix_class->phy_addr, 0);
  480. usbh_asix_mdio_write(asix_class, asix_class->phy_addr, 0, 0x3100);
  481. usbh_asix_mdio_read(asix_class, asix_class->phy_addr, 4);
  482. usbh_asix_mdio_write(asix_class, asix_class->phy_addr, 4, 0x01e1);
  483. usbh_asix_mdio_read(asix_class, asix_class->phy_addr, 1);
  484. usbh_asix_mdio_write(asix_class, asix_class->phy_addr, 0, 0x3300);
  485. usbh_asix_mdio_read(asix_class, asix_class->phy_addr, 0);
  486. }
  487. USB_LOG_INFO("Init %s done\r\n", asix_class->name);
  488. strncpy(hport->config.intf[intf].devname, DEV_FORMAT, CONFIG_USBHOST_DEV_NAMELEN);
  489. USB_LOG_INFO("Register ASIX Class:%s\r\n", hport->config.intf[intf].devname);
  490. usbh_asix_run(asix_class);
  491. return ret;
  492. }
  493. static int usbh_asix_disconnect(struct usbh_hubport *hport, uint8_t intf)
  494. {
  495. int ret = 0;
  496. struct usbh_asix *asix_class = (struct usbh_asix *)hport->config.intf[intf].priv;
  497. if (asix_class) {
  498. if (asix_class->bulkin) {
  499. usbh_kill_urb(&asix_class->bulkin_urb);
  500. }
  501. if (asix_class->bulkout) {
  502. usbh_kill_urb(&asix_class->bulkout_urb);
  503. }
  504. if (asix_class->intin) {
  505. usbh_kill_urb(&asix_class->intin_urb);
  506. }
  507. if (hport->config.intf[intf].devname[0] != '\0') {
  508. USB_LOG_INFO("Unregister ASIX Class:%s\r\n", hport->config.intf[intf].devname);
  509. usbh_asix_stop(asix_class);
  510. }
  511. memset(asix_class, 0, sizeof(struct usbh_asix));
  512. }
  513. return ret;
  514. }
  515. int usbh_asix_get_connect_status(struct usbh_asix *asix_class)
  516. {
  517. int ret;
  518. usbh_int_urb_fill(&asix_class->intin_urb, asix_class->hport, asix_class->intin, g_asix_inttx_buffer, 8, USB_OSAL_WAITING_FOREVER, NULL, NULL);
  519. ret = usbh_submit_urb(&asix_class->intin_urb);
  520. if (ret < 0) {
  521. return ret;
  522. }
  523. if (g_asix_inttx_buffer[1] == 0x00) {
  524. if (g_asix_inttx_buffer[2] & 0x01) {
  525. asix_class->connect_status = true;
  526. usbh_ax88772_mac_link_up(asix_class, SPEED_100, 1, 1, 1);
  527. usbh_asix_set_multicast(asix_class);
  528. } else {
  529. asix_class->connect_status = false;
  530. usbh_ax88772_mac_link_down(asix_class);
  531. }
  532. }
  533. return 0;
  534. }
  535. void usbh_asix_rx_thread(CONFIG_USB_OSAL_THREAD_SET_ARGV)
  536. {
  537. uint32_t g_asix_rx_length;
  538. int ret;
  539. uint16_t len;
  540. uint16_t len_crc;
  541. uint32_t data_offset;
  542. #if CONFIG_USBHOST_ASIX_ETH_MAX_RX_SIZE <= (16 * 1024)
  543. uint32_t transfer_size = CONFIG_USBHOST_ASIX_ETH_MAX_RX_SIZE;
  544. #else
  545. uint32_t transfer_size = (16 * 1024);
  546. #endif
  547. (void)CONFIG_USB_OSAL_THREAD_GET_ARGV;
  548. USB_LOG_INFO("Create asix rx thread\r\n");
  549. // clang-format off
  550. find_class:
  551. // clang-format on
  552. g_asix_class.connect_status = false;
  553. if (usbh_find_class_instance("/dev/asix") == NULL) {
  554. goto delete;
  555. }
  556. while (g_asix_class.connect_status == false) {
  557. ret = usbh_asix_get_connect_status(&g_asix_class);
  558. if (ret < 0) {
  559. usb_osal_msleep(100);
  560. goto find_class;
  561. }
  562. usb_osal_msleep(128);
  563. }
  564. g_asix_rx_length = 0;
  565. while (1) {
  566. usbh_bulk_urb_fill(&g_asix_class.bulkin_urb, g_asix_class.hport, g_asix_class.bulkin, &g_asix_rx_buffer[g_asix_rx_length], transfer_size, USB_OSAL_WAITING_FOREVER, NULL, NULL);
  567. ret = usbh_submit_urb(&g_asix_class.bulkin_urb);
  568. if (ret < 0) {
  569. goto find_class;
  570. }
  571. g_asix_rx_length += g_asix_class.bulkin_urb.actual_length;
  572. /* A transfer is complete because last packet is a short packet.
  573. * Short packet is not zero, match g_asix_rx_length % USB_GET_MAXPACKETSIZE(g_asix_class.bulkin->wMaxPacketSize).
  574. * Short packet is zero, check if g_asix_class.bulkin_urb.actual_length < transfer_size, for example transfer is complete with size is 1024 < 2048.
  575. */
  576. if (g_asix_rx_length % USB_GET_MAXPACKETSIZE(g_asix_class.bulkin->wMaxPacketSize) ||
  577. (g_asix_class.bulkin_urb.actual_length < transfer_size)) {
  578. USB_LOG_DBG("rxlen:%d\r\n", g_asix_rx_length);
  579. data_offset = 0;
  580. while (g_asix_rx_length > 0) {
  581. len = ((uint16_t)g_asix_rx_buffer[data_offset + 0] | ((uint16_t)(g_asix_rx_buffer[data_offset + 1]) << 8)) & 0x7ff;
  582. len_crc = g_asix_rx_buffer[data_offset + 2] | ((uint16_t)(g_asix_rx_buffer[data_offset + 3]) << 8);
  583. if (len != (~len_crc & 0x7ff)) {
  584. USB_LOG_ERR("rx header error\r\n");
  585. g_asix_rx_length = 0;
  586. continue;
  587. }
  588. uint8_t *buf = (uint8_t *)&g_asix_rx_buffer[data_offset + 4];
  589. usbh_asix_eth_input(buf, len);
  590. g_asix_rx_length -= (len + 4);
  591. data_offset += (len + 4);
  592. if (g_asix_rx_length < 4) {
  593. g_asix_rx_length = 0;
  594. }
  595. }
  596. } else {
  597. #if CONFIG_USBHOST_ASIX_ETH_MAX_RX_SIZE <= (16 * 1024)
  598. if (g_asix_rx_length == CONFIG_USBHOST_ASIX_ETH_MAX_RX_SIZE) {
  599. #else
  600. if ((g_asix_rx_length + (16 * 1024)) > CONFIG_USBHOST_ASIX_ETH_MAX_RX_SIZE) {
  601. #endif
  602. USB_LOG_ERR("Rx packet is overflow, please reduce tcp window size or increase CONFIG_USBHOST_ASIX_ETH_MAX_RX_SIZE\r\n");
  603. while (1) {
  604. }
  605. }
  606. }
  607. }
  608. // clang-format off
  609. delete:
  610. USB_LOG_INFO("Delete asix rx thread\r\n");
  611. usb_osal_thread_delete(NULL);
  612. // clang-format on
  613. }
  614. uint8_t *usbh_asix_get_eth_txbuf(void)
  615. {
  616. return &g_asix_tx_buffer[4];
  617. }
  618. int usbh_asix_eth_output(uint32_t buflen)
  619. {
  620. uint16_t actual_len;
  621. if (g_asix_class.connect_status == false) {
  622. return -USB_ERR_NOTCONN;
  623. }
  624. g_asix_tx_buffer[0] = buflen & 0xff;
  625. g_asix_tx_buffer[1] = (buflen >> 8) & 0xff;
  626. g_asix_tx_buffer[2] = ~g_asix_tx_buffer[0];
  627. g_asix_tx_buffer[3] = ~g_asix_tx_buffer[1];
  628. if (!(buflen + 4) % USB_GET_MAXPACKETSIZE(g_asix_class.bulkout->wMaxPacketSize)) {
  629. USB_LOG_DBG("txlen:%d\r\n", buflen + 8);
  630. g_asix_tx_buffer[buflen + 4 + 0] = 0x00;
  631. g_asix_tx_buffer[buflen + 4 + 1] = 0x00;
  632. g_asix_tx_buffer[buflen + 4 + 2] = 0xff;
  633. g_asix_tx_buffer[buflen + 4 + 3] = 0xff;
  634. actual_len = buflen + 8;
  635. } else {
  636. USB_LOG_DBG("txlen:%d\r\n", buflen + 4);
  637. actual_len = buflen + 4;
  638. }
  639. usbh_bulk_urb_fill(&g_asix_class.bulkout_urb, g_asix_class.hport, g_asix_class.bulkout, g_asix_tx_buffer, actual_len, USB_OSAL_WAITING_FOREVER, NULL, NULL);
  640. return usbh_submit_urb(&g_asix_class.bulkout_urb);
  641. }
  642. __WEAK void usbh_asix_run(struct usbh_asix *asix_class)
  643. {
  644. (void)asix_class;
  645. }
  646. __WEAK void usbh_asix_stop(struct usbh_asix *asix_class)
  647. {
  648. (void)asix_class;
  649. }
  650. static const uint16_t asix_id_table[][2] = {
  651. { 0x0B95, 0x772B },
  652. { 0x0B95, 0x7720 },
  653. { 0, 0 },
  654. };
  655. static const struct usbh_class_driver asix_class_driver = {
  656. .driver_name = "asix",
  657. .connect = usbh_asix_connect,
  658. .disconnect = usbh_asix_disconnect
  659. };
  660. CLASS_INFO_DEFINE const struct usbh_class_info asix_class_info = {
  661. .match_flags = USB_CLASS_MATCH_VID_PID | USB_CLASS_MATCH_INTF_CLASS,
  662. .bInterfaceClass = 0xff,
  663. .bInterfaceSubClass = 0x00,
  664. .bInterfaceProtocol = 0x00,
  665. .id_table = asix_id_table,
  666. .class_driver = &asix_class_driver
  667. };