dw_eth_mac.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759
  1. /*
  2. * Copyright (C) Cvitek Co., Ltd. 2019-2020. All rights reserved.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include <rtthread.h>
  17. #include <mmu.h>
  18. #include "dw_eth_mac.h"
  19. #include "cache.h"
  20. #include "string.h"
  21. #include "drv_ioremap.h"
  22. #define roundup(x, y) ( \
  23. { \
  24. const typeof(y) __y = y; \
  25. (((x) + (__y - 1)) / __y) * __y; \
  26. } \
  27. )
  28. #define CONFIG_GMAC_NUM 2
  29. static gmac_dev_t gmac_instance[CONFIG_GMAC_NUM];
  30. static int32_t designware_read_hwaddr(eth_mac_handle_t handle)
  31. {
  32. gmac_dev_t *mac_dev = (gmac_dev_t *)handle;
  33. struct dw_gmac_mac_regs *mac_reg = mac_dev->priv->mac_regs_p;
  34. uint32_t macid_lo, macid_hi;
  35. uint8_t mac_id[6] = {0};
  36. macid_hi = mac_reg->macaddr0hi;
  37. macid_lo = mac_reg->macaddr0lo;
  38. mac_id[0] = macid_lo & 0xff;
  39. mac_id[1] = (macid_lo >> 8) & 0xff;
  40. mac_id[2] = (macid_lo >> 16) & 0xff;
  41. mac_id[3] = (macid_lo >> 24) & 0xff;
  42. mac_id[4] = macid_hi & 0xff;
  43. mac_id[5] = (macid_hi >> 8) & 0xff;
  44. memcpy(mac_dev->mac_addr, mac_id, sizeof(mac_id));
  45. return 0;
  46. }
  47. static int32_t designware_write_hwaddr(eth_mac_handle_t handle)
  48. {
  49. gmac_dev_t *mac_dev = (gmac_dev_t *)handle;
  50. struct dw_gmac_mac_regs *mac_reg = mac_dev->priv->mac_regs_p;
  51. uint32_t macid_lo, macid_hi;
  52. uint8_t *mac_id = mac_dev->mac_addr;
  53. macid_lo = mac_id[0] + (mac_id[1] << 8) + (mac_id[2] << 16) +
  54. (mac_id[3] << 24);
  55. macid_hi = mac_id[4] + (mac_id[5] << 8);
  56. mac_reg->macaddr0hi = macid_hi;
  57. mac_reg->macaddr0lo = macid_lo;
  58. return 0;
  59. }
  60. static void tx_descs_init(eth_mac_handle_t handle)
  61. {
  62. gmac_dev_t *mac_dev = (gmac_dev_t *)handle;
  63. struct dw_gmac_priv *priv = mac_dev->priv;
  64. struct dw_gmac_dma_regs *dma_reg = priv->dma_regs_p;
  65. struct dmamacdescr *desc_table_p = &priv->tx_mac_descrtable[0];
  66. char *txbuffs = &priv->txbuffs[0];
  67. struct dmamacdescr *desc_p;
  68. uint32_t idx;
  69. for (idx = 0; idx < CVI_CONFIG_TX_DESCR_NUM; idx ++) {
  70. desc_p = &desc_table_p[idx];
  71. #ifdef RT_USING_SMART
  72. desc_p->dmamac_addr = (unsigned long)rt_kmem_v2p((void *)&txbuffs[idx * CVI_CONFIG_ETH_BUFSIZE]);
  73. desc_p->dmamac_next = (unsigned long)rt_kmem_v2p((void *)&desc_table_p[idx + 1]);
  74. #else
  75. desc_p->dmamac_addr = (unsigned long)&txbuffs[idx * CVI_CONFIG_ETH_BUFSIZE];
  76. desc_p->dmamac_next = (unsigned long)&desc_table_p[idx + 1];
  77. #endif
  78. #if defined(CONFIG_DW_ALTDESCRIPTOR)
  79. desc_p->txrx_status &= ~(CVI_DESC_TXSTS_TXINT | CVI_DESC_TXSTS_TXLAST |
  80. CVI_DESC_TXSTS_TXFIRST | CVI_DESC_TXSTS_TXCRCDIS |
  81. CVI_DESC_TXSTS_TXCHECKINSCTRL |
  82. CVI_DESC_TXSTS_TXRINGEND | CVI_DESC_TXSTS_TXPADDIS);
  83. desc_p->txrx_status |= CVI_DESC_TXSTS_TXCHAIN;
  84. desc_p->dmamac_cntl = 0;
  85. desc_p->txrx_status &= ~(CVI_DESC_TXSTS_MSK | CVI_DESC_TXSTS_OWNBYDMA);
  86. #else
  87. desc_p->dmamac_cntl = CVI_DESC_TXCTRL_TXCHAIN;
  88. desc_p->txrx_status = 0;
  89. #endif
  90. }
  91. /* Correcting the last pointer of the chain */
  92. #ifdef RT_USING_SMART
  93. desc_p->dmamac_next = (unsigned long)rt_kmem_v2p((void *)&desc_table_p[0]);
  94. #else
  95. desc_p->dmamac_next = (unsigned long)&desc_table_p[0];
  96. #endif
  97. /* Flush all Tx buffer descriptors at once */
  98. rt_hw_cpu_dcache_clean((void *)priv->tx_mac_descrtable, sizeof(priv->tx_mac_descrtable));
  99. #ifdef RT_USING_SMART
  100. dma_reg->txdesclistaddr = (rt_ubase_t)rt_kmem_v2p((void *)&desc_table_p[0]);
  101. #else
  102. dma_reg->txdesclistaddr = (reg_type)&desc_table_p[0];
  103. #endif
  104. priv->tx_currdescnum = 0;
  105. }
  106. static void rx_descs_init(eth_mac_handle_t handle)
  107. {
  108. gmac_dev_t *mac_dev = (gmac_dev_t *)handle;
  109. struct dw_gmac_priv *priv = mac_dev->priv;
  110. struct dw_gmac_dma_regs *dma_reg = priv->dma_regs_p;
  111. struct dmamacdescr *desc_table_p = &priv->rx_mac_descrtable[0];
  112. char *rxbuffs = &priv->rxbuffs[0];
  113. struct dmamacdescr *desc_p;
  114. uint32_t idx;
  115. /* Before passing buffers to GMAC we need to make sure zeros
  116. * written there right after "priv" structure allocation were
  117. * flushed into RAM.
  118. * Otherwise there's a chance to get some of them flushed in RAM when
  119. * GMAC is already pushing data to RAM via DMA. This way incoming from
  120. * GMAC data will be corrupted. */
  121. rt_hw_cpu_dcache_clean((void *)rxbuffs, CVI_RX_TOTAL_BUFSIZE);
  122. for (idx = 0; idx < CVI_CONFIG_RX_DESCR_NUM; idx++) {
  123. desc_p = &desc_table_p[idx];
  124. #ifdef RT_USING_SMART
  125. desc_p->dmamac_addr = (unsigned long)rt_kmem_v2p((void *)&rxbuffs[idx * CVI_CONFIG_ETH_BUFSIZE]);
  126. desc_p->dmamac_next = (unsigned long)rt_kmem_v2p((void *)&desc_table_p[idx + 1]);
  127. #else
  128. desc_p->dmamac_addr = (unsigned long)&rxbuffs[idx * CVI_CONFIG_ETH_BUFSIZE];
  129. desc_p->dmamac_next = (unsigned long)&desc_table_p[idx + 1];
  130. #endif
  131. desc_p->dmamac_cntl = (CVI_MAC_MAX_FRAME_SZ & CVI_DESC_RXCTRL_SIZE1MASK) | CVI_DESC_RXCTRL_RXCHAIN;
  132. desc_p->txrx_status = CVI_DESC_RXSTS_OWNBYDMA;
  133. }
  134. /* Correcting the last pointer of the chain */
  135. #ifdef RT_USING_SMART
  136. desc_p->dmamac_next = (unsigned long)rt_kmem_v2p((void *)&desc_table_p[0]);
  137. #else
  138. desc_p->dmamac_next = (unsigned long)&desc_table_p[0];
  139. #endif
  140. /* Flush all Rx buffer descriptors at once */
  141. rt_hw_cpu_dcache_clean((void *)priv->rx_mac_descrtable, sizeof(priv->rx_mac_descrtable));
  142. #ifdef RT_USING_SMART
  143. dma_reg->rxdesclistaddr = (rt_ubase_t)rt_kmem_v2p((void *)&desc_table_p[0]);
  144. #else
  145. dma_reg->rxdesclistaddr = (reg_type)&desc_table_p[0];
  146. #endif
  147. priv->rx_currdescnum = 0;
  148. }
  149. static int32_t designware_adjust_link(eth_mac_handle_t handle)
  150. {
  151. gmac_dev_t *mac_dev = (gmac_dev_t *)handle;
  152. struct dw_gmac_priv *priv = mac_dev->priv;
  153. struct dw_gmac_mac_regs *mac_reg = priv->mac_regs_p;
  154. eth_link_info_t *link_info = &mac_dev->phy_dev->priv->link_info;
  155. eth_link_state_t link_state = mac_dev->phy_dev->link_state;
  156. uint32_t conf = mac_reg->conf | CVI_FRAMEBURSTENABLE | CVI_DISABLERXOWN;
  157. if (!link_state) {
  158. rt_kprintf("eth No link.\n");
  159. return 0;
  160. }
  161. if (link_info->speed != CSI_ETH_SPEED_1G)
  162. conf |= CVI_MII_PORTSELECT;
  163. else
  164. conf &= ~CVI_MII_PORTSELECT;
  165. if (link_info->speed == CSI_ETH_SPEED_100M)
  166. conf |= CVI_FES_100;
  167. if (link_info->duplex)
  168. conf |= CVI_FULLDPLXMODE;
  169. mac_reg->conf = conf;
  170. rt_kprintf("Speed: %s, duplex: %s\n",
  171. (link_info->speed) ? "100M" : "10M",
  172. (link_info->duplex) ? "full" : "half");
  173. return 0;
  174. }
  175. static int32_t designware_eth_init(eth_mac_handle_t handle)
  176. {
  177. gmac_dev_t *mac_dev = (gmac_dev_t *)handle;
  178. struct dw_gmac_mac_regs *mac_reg = mac_dev->priv->mac_regs_p;
  179. struct dw_gmac_dma_regs *dma_reg = mac_dev->priv->dma_regs_p;
  180. uint32_t start;
  181. dma_reg->busmode |= CVI_DMAMAC_SRST;
  182. start = rt_tick_get_millisecond();
  183. while (dma_reg->busmode & CVI_DMAMAC_SRST) {
  184. if ((rt_tick_get_millisecond() - start) >= CVI_CONFIG_MACRESET_TIMEOUT) {
  185. rt_kprintf("DMA reset timeout\n");
  186. return -ETIMEDOUT;
  187. }
  188. rt_thread_mdelay(100);
  189. };
  190. /*
  191. * Soft reset above clears HW address registers.
  192. * So we have to set it here once again.
  193. */
  194. // designware_read_hwaddr(handle);
  195. // designware_write_hwaddr(handle);
  196. rx_descs_init(handle);
  197. tx_descs_init(handle);
  198. dma_reg->busmode = (CVI_FIXEDBURST | CVI_PRIORXTX_41 | CVI_DMA_PBL);
  199. // mac_reg->framefilt = 0x10;
  200. // mac_reg->flowcontrol = 0x8;
  201. // dma_reg->wdtforri = 0xff;
  202. // dma_reg->axibus = 0x0012100F;
  203. #ifndef CONFIG_DW_MAC_FORCE_THRESHOLD_MODE
  204. dma_reg->opmode |= (CVI_FLUSHTXFIFO | CVI_STOREFORWARD);
  205. #else
  206. dma_reg->opmode |= CVI_FLUSHTXFIFO;
  207. #endif
  208. dma_reg->opmode |= (CVI_RXSTART | CVI_TXSTART);
  209. dma_reg->opmode = 0x2202906;
  210. dma_reg->busmode = 0x3900800;
  211. mac_reg->conf = 0x41cc00;
  212. dma_reg->intenable = 0x10040;
  213. #ifdef CONFIG_DW_AXI_BURST_LEN
  214. dma_reg->axibus = (CONFIG_DW_AXI_BURST_LEN & 0x1FF >> 1);
  215. #endif
  216. /* Start up the PHY */
  217. /* adjust link */
  218. return 0;
  219. }
  220. static int32_t designware_eth_enable(eth_mac_handle_t handle, int32_t control)
  221. {
  222. gmac_dev_t *mac_dev = (gmac_dev_t *)handle;
  223. struct dw_gmac_mac_regs *mac_reg = mac_dev->priv->mac_regs_p;
  224. eth_link_state_t link_state = mac_dev->phy_dev->link_state;
  225. // if (link_state == ETH_LINK_DOWN)
  226. // return -1;
  227. switch (control) {
  228. case CSI_ETH_MAC_CONTROL_TX:
  229. mac_reg->conf |= CVI_TXENABLE;
  230. break;
  231. case CSI_ETH_MAC_CONTROL_RX:
  232. mac_reg->conf |= CVI_RXENABLE;
  233. break;
  234. default:
  235. break;
  236. }
  237. return 0;
  238. }
  239. static int32_t designware_eth_disable(eth_mac_handle_t handle, int32_t arg)
  240. {
  241. gmac_dev_t *mac_dev = (gmac_dev_t *)handle;
  242. struct dw_gmac_mac_regs *mac_reg = mac_dev->priv->mac_regs_p;
  243. switch (arg) {
  244. case CSI_ETH_MAC_CONTROL_TX:
  245. mac_reg->conf &= ~CVI_TXENABLE;
  246. break;
  247. case CSI_ETH_MAC_CONTROL_RX:
  248. mac_reg->conf &= ~CVI_RXENABLE;
  249. break;
  250. default:
  251. break;
  252. }
  253. return 0;
  254. }
  255. static int32_t designware_eth_start(eth_mac_handle_t handle)
  256. {
  257. int32_t ret;
  258. ret = designware_eth_init(handle);
  259. if (ret)
  260. return ret;
  261. return 0;
  262. }
  263. void designware_eth_stop(eth_mac_handle_t handle)
  264. {
  265. gmac_dev_t *mac_dev = (gmac_dev_t *)handle;
  266. struct dw_gmac_mac_regs *mac_reg = mac_dev->priv->mac_regs_p;
  267. struct dw_gmac_dma_regs *dma_reg = mac_dev->priv->dma_regs_p;
  268. mac_reg->conf &= ~(CVI_RXENABLE | CVI_TXENABLE);
  269. dma_reg->opmode &= ~(CVI_RXSTART | CVI_TXSTART);
  270. //phy_shutdown(priv->phydev);
  271. }
  272. static int32_t designware_eth_send(eth_mac_handle_t handle, const uint8_t *frame, uint32_t length)
  273. {
  274. gmac_dev_t *mac_dev = (gmac_dev_t *)handle;
  275. struct dw_gmac_priv *priv = mac_dev->priv;
  276. struct dw_gmac_dma_regs *dma_reg = mac_dev->priv->dma_regs_p;
  277. uint32_t desc_num = priv->tx_currdescnum;
  278. struct dmamacdescr *desc_p = &priv->tx_mac_descrtable[desc_num];
  279. uint64_t desc_start = (uint64_t)desc_p;
  280. uint64_t desc_end = desc_start + roundup(sizeof(*desc_p), DW_GMAC_DMA_ALIGN);
  281. uint64_t data_start = (uint64_t)DRV_IOREMAP((void *)desc_p->dmamac_addr, 0x1000);
  282. uint64_t data_end = data_start + roundup(length, DW_GMAC_DMA_ALIGN);
  283. uint32_t count = 0;
  284. /*
  285. * Strictly we only need to invalidate the "txrx_status" field
  286. * for the following check, but on some platforms we cannot
  287. * invalidate only 4 bytes, so we flush the entire descriptor,
  288. * which is 16 bytes in total. This is safe because the
  289. * individual descriptors in the array are each aligned to
  290. * DW_GMAC_DMA_ALIGN and padded appropriately.
  291. */
  292. /* Check if the descriptor is owned by CPU */
  293. while (1)
  294. {
  295. rt_hw_cpu_dcache_invalidate((void *)desc_start, desc_end - desc_start);
  296. if (!(desc_p->txrx_status & CVI_DESC_TXSTS_OWNBYDMA))
  297. {
  298. break;
  299. }
  300. if (count > 1000)
  301. {
  302. rt_kprintf("desc onwer is DMA\n");
  303. return -1;
  304. }
  305. count ++;
  306. rt_thread_mdelay(1);
  307. }
  308. memcpy((void *)data_start, frame, length);
  309. /* Flush data to be sent */
  310. rt_hw_cpu_dcache_clean((void *)data_start, data_end - data_start);
  311. #if defined(CONFIG_DW_ALTDESCRIPTOR)
  312. desc_p->txrx_status |= CVI_DESC_TXSTS_TXFIRST | CVI_DESC_TXSTS_TXLAST;
  313. desc_p->dmamac_cntl &= ~CVI_DESC_TXCTRL_SIZE1MASK;
  314. desc_p->dmamac_cntl |= (length << CVI_DESC_TXCTRL_SIZE1SHFT) &
  315. CVI_DESC_TXCTRL_SIZE1MASK;
  316. desc_p->txrx_status &= ~(CVI_DESC_TXSTS_MSK);
  317. desc_p->txrx_status |= CVI_DESC_TXSTS_OWNBYDMA;
  318. #else
  319. desc_p->dmamac_cntl &= ~CVI_DESC_TXCTRL_SIZE1MASK;
  320. desc_p->dmamac_cntl |= ((length << CVI_DESC_TXCTRL_SIZE1SHFT) &
  321. CVI_DESC_TXCTRL_SIZE1MASK) | CVI_DESC_TXCTRL_TXLAST |
  322. CVI_DESC_TXCTRL_TXFIRST;
  323. desc_p->txrx_status = CVI_DESC_TXSTS_OWNBYDMA;
  324. #endif
  325. /* Flush modified buffer descriptor */
  326. rt_hw_cpu_dcache_clean((void *)desc_start, desc_end - desc_start);
  327. /* Test the wrap-around condition. */
  328. if (++desc_num >= CVI_CONFIG_TX_DESCR_NUM)
  329. desc_num = 0;
  330. priv->tx_currdescnum = desc_num;
  331. /* Start the transmission */
  332. dma_reg->txpolldemand = CVI_POLL_DATA;
  333. return 0;
  334. }
  335. static int32_t designware_eth_recv(eth_mac_handle_t handle, uint8_t **packetp)
  336. {
  337. gmac_dev_t *mac_dev = (gmac_dev_t *)handle;
  338. struct dw_gmac_priv *priv = mac_dev->priv;
  339. uint32_t status, desc_num = priv->rx_currdescnum;
  340. struct dmamacdescr *desc_p = &priv->rx_mac_descrtable[desc_num];
  341. int32_t length = -1;
  342. uint64_t desc_start = (uint64_t)desc_p;
  343. uint64_t desc_end = desc_start + roundup(sizeof(*desc_p), DW_GMAC_DMA_ALIGN);
  344. uint64_t data_start = (uint64_t)DRV_IOREMAP((void *)desc_p->dmamac_addr, 0x1000);
  345. uint64_t data_end;
  346. /* Invalidate entire buffer descriptor */
  347. rt_hw_cpu_dcache_invalidate((void *)desc_start, desc_end - desc_start);
  348. status = desc_p->txrx_status;
  349. /* Check if the owner is the CPU */
  350. if (!(status & CVI_DESC_RXSTS_OWNBYDMA))
  351. {
  352. length = (status & CVI_DESC_RXSTS_FRMLENMSK) >> CVI_DESC_RXSTS_FRMLENSHFT;
  353. /* Invalidate received data */
  354. data_end = data_start + roundup(length, DW_GMAC_DMA_ALIGN);
  355. rt_hw_cpu_dcache_invalidate((void *)data_start, data_end - data_start);
  356. *packetp = (uint8_t *)data_start;
  357. }
  358. return length;
  359. }
  360. static int32_t designware_free_pkt(eth_mac_handle_t handle)
  361. {
  362. gmac_dev_t *mac_dev = (gmac_dev_t *)handle;
  363. struct dw_gmac_priv *priv = mac_dev->priv;
  364. uint32_t desc_num = priv->rx_currdescnum;
  365. struct dmamacdescr *desc_p = &priv->rx_mac_descrtable[desc_num];
  366. uint64_t desc_start = (uint64_t)desc_p;
  367. uint64_t desc_end = desc_start + roundup(sizeof(*desc_p), DW_GMAC_DMA_ALIGN);
  368. /*
  369. * Make the current descriptor valid again and go to
  370. * the next one
  371. */
  372. desc_p->txrx_status |= CVI_DESC_RXSTS_OWNBYDMA;
  373. /* Flush only status field - others weren't changed */
  374. rt_hw_cpu_dcache_clean((void *)desc_start, desc_end - desc_start);
  375. /* Test the wrap-around condition. */
  376. if (++desc_num >= CVI_CONFIG_RX_DESCR_NUM)
  377. desc_num = 0;
  378. priv->rx_currdescnum = desc_num;
  379. return 0;
  380. }
  381. /**
  382. \brief Connect phy device to mac device.
  383. \param[in] handle_mac mac handle
  384. \param[in] handle_phy phy handle
  385. */
  386. void dw_eth_mac_connect_phy(eth_mac_handle_t handle_mac, eth_phy_handle_t handle_phy)
  387. {
  388. RT_ASSERT(handle_mac);
  389. RT_ASSERT(handle_phy);
  390. gmac_dev_t *mac_dev = (gmac_dev_t *)handle_mac;
  391. eth_phy_dev_t *phy_dev = (eth_phy_dev_t *)handle_phy;
  392. mac_dev->phy_dev = phy_dev;
  393. }
  394. /**
  395. \brief Read Ethernet PHY Register through Management Interface.
  396. \param[in] handle ethernet handle
  397. \param[in] phy_addr 5-bit device address
  398. \param[in] reg_addr 5-bit register address
  399. \param[out] data Pointer where the result is written to
  400. \return error code
  401. */
  402. int32_t dw_eth_mac_phy_read(eth_mac_handle_t handle, uint8_t phy_addr, uint8_t reg_addr, uint16_t *data)
  403. {
  404. RT_ASSERT(handle);
  405. RT_ASSERT(data);
  406. gmac_dev_t *mac_dev = (gmac_dev_t *)handle;
  407. struct dw_gmac_priv *priv = mac_dev->priv;
  408. struct dw_gmac_mac_regs *mac_reg = priv->mac_regs_p;
  409. uint16_t miiaddr;
  410. int32_t start;
  411. miiaddr = ((phy_addr << CVI_MIIADDRSHIFT) & CVI_MII_ADDRMSK) | ((reg_addr << CVI_MIIREGSHIFT) & CVI_MII_REGMSK);
  412. mac_reg->miiaddr = (miiaddr | CVI_MII_CLKRANGE_150_250M | CVI_MII_BUSY);
  413. start = rt_tick_get_millisecond();
  414. while ((rt_tick_get_millisecond() - start) < CVI_CONFIG_MDIO_TIMEOUT)
  415. {
  416. if (!(mac_reg->miiaddr & CVI_MII_BUSY))
  417. {
  418. *data = mac_reg->miidata;
  419. return 0;
  420. }
  421. rt_hw_us_delay(10);
  422. };
  423. return -1;
  424. }
  425. /**
  426. \brief Write Ethernet PHY Register through Management Interface.
  427. \param[in] handle ethernet handle
  428. \param[in] phy_addr 5-bit device address
  429. \param[in] reg_addr 5-bit register address
  430. \param[in] data 16-bit data to write
  431. \return error code
  432. */
  433. int32_t dw_eth_mac_phy_write(eth_mac_handle_t handle, uint8_t phy_addr, uint8_t reg_addr, uint16_t data)
  434. {
  435. RT_ASSERT(handle);
  436. gmac_dev_t *mac_dev = (gmac_dev_t *)handle;
  437. struct dw_gmac_priv *priv = mac_dev->priv;
  438. struct dw_gmac_mac_regs *mac_reg = priv->mac_regs_p;
  439. uint16_t miiaddr;
  440. int32_t start;
  441. mac_reg->miidata = data;
  442. miiaddr = ((phy_addr << CVI_MIIADDRSHIFT) & CVI_MII_ADDRMSK) |
  443. ((reg_addr << CVI_MIIREGSHIFT) & CVI_MII_REGMSK) | CVI_MII_WRITE;
  444. mac_reg->miiaddr = (miiaddr | CVI_MII_CLKRANGE_150_250M | CVI_MII_BUSY);
  445. start = rt_tick_get_millisecond();
  446. while ((rt_tick_get_millisecond() - start) < CVI_CONFIG_MDIO_TIMEOUT) {
  447. if (!(mac_reg->miiaddr & CVI_MII_BUSY)) {
  448. return 0;
  449. }
  450. rt_hw_us_delay(10);
  451. };
  452. return -1;
  453. }
  454. /**
  455. \brief Control Ethernet Interface.
  456. \param[in] handle ethernet handle
  457. \param[in] control Operation
  458. \param[in] arg Argument of operation (optional)
  459. \return error code
  460. */
  461. int32_t cvi_eth_mac_control(eth_mac_handle_t handle, uint32_t control, uint32_t arg)
  462. {
  463. RT_ASSERT(handle);
  464. gmac_dev_t *mac_dev = (gmac_dev_t *)handle;
  465. int32_t ret = 0;
  466. RT_ASSERT(mac_dev->phy_dev);
  467. switch (control) {
  468. case CSI_ETH_MAC_CONFIGURE:
  469. if (arg) {
  470. /* startup mac */
  471. ret = designware_eth_start(handle);
  472. } else {
  473. /* stop mac */
  474. designware_eth_stop(handle);
  475. }
  476. break;
  477. case DRV_ETH_MAC_ADJUST_LINK:
  478. ret = designware_adjust_link(handle);
  479. break;
  480. case CSI_ETH_MAC_CONTROL_TX:
  481. if (arg) {
  482. /* enable TX */
  483. ret = designware_eth_enable(handle, CSI_ETH_MAC_CONTROL_TX);
  484. } else {
  485. /* disable TX */
  486. ret = designware_eth_disable(handle, CSI_ETH_MAC_CONTROL_TX);
  487. }
  488. break;
  489. case CSI_ETH_MAC_CONTROL_RX:
  490. if (arg) {
  491. /* enable RX */
  492. ret = designware_eth_enable(handle, CSI_ETH_MAC_CONTROL_RX);
  493. } else {
  494. /* disable RX */
  495. ret = designware_eth_disable(handle, CSI_ETH_MAC_CONTROL_RX);
  496. }
  497. break;
  498. case DRV_ETH_MAC_CONTROL_IRQ:
  499. if (arg) {
  500. /* enable interrupt */
  501. } else {
  502. /* disable interrupt */
  503. }
  504. break;
  505. default:
  506. break;
  507. };
  508. return ret;
  509. }
  510. /**
  511. \brief Get Ethernet MAC Address.
  512. \param[in] handle ethernet handle
  513. \param[in] mac Pointer to address
  514. \return error code
  515. */
  516. int32_t cvi_eth_mac_get_macaddr(eth_mac_handle_t handle, eth_mac_addr_t *mac)
  517. {
  518. RT_ASSERT(handle);
  519. RT_ASSERT(mac);
  520. gmac_dev_t *mac_dev = (gmac_dev_t *)handle;
  521. designware_read_hwaddr(handle);
  522. memcpy(mac->b, mac_dev->mac_addr, sizeof(mac_dev->mac_addr));
  523. return 0;
  524. }
  525. /**
  526. \brief Set Ethernet MAC Address.
  527. \param[in] handle ethernet handle
  528. \param[in] mac Pointer to address
  529. \return error code
  530. */
  531. int32_t cvi_eth_mac_set_macaddr(eth_mac_handle_t handle, const eth_mac_addr_t *mac)
  532. {
  533. RT_ASSERT(handle);
  534. RT_ASSERT(mac);
  535. gmac_dev_t *mac_dev = (gmac_dev_t *)handle;
  536. memcpy(mac_dev->mac_addr, mac->b, sizeof(mac->b));
  537. designware_write_hwaddr(handle);
  538. return 0;
  539. }
  540. /**
  541. \brief Send Ethernet frame.
  542. \param[in] handle ethernet handle
  543. \param[in] frame Pointer to frame buffer with data to send
  544. \param[in] len Frame buffer length in bytes
  545. \return error code
  546. */
  547. int32_t cvi_eth_mac_send_frame(eth_mac_handle_t handle, const uint8_t *frame, uint32_t len)
  548. {
  549. RT_ASSERT(handle);
  550. RT_ASSERT(frame);
  551. return designware_eth_send(handle, frame, len);
  552. }
  553. /**
  554. \brief Read data of received Ethernet frame.
  555. \param[in] handle ethernet handle
  556. \param[in] frame Pointer to frame buffer for data to read into
  557. \param[in] len Frame buffer length in bytes
  558. \return number of data bytes read or execution status
  559. - value >= 0: number of data bytes read
  560. - value < 0: error occurred, value is execution status as defined with execution_status
  561. */
  562. int32_t cvi_eth_mac_read_frame(eth_mac_handle_t handle, uint8_t *frame, uint32_t len)
  563. {
  564. RT_ASSERT(handle);
  565. RT_ASSERT(frame);
  566. uint8_t *packet = NULL;
  567. int32_t actual_length;
  568. actual_length = designware_eth_recv(handle, &packet);
  569. if (actual_length < 0) {
  570. return -1;
  571. }
  572. /* process received packet */
  573. actual_length = (actual_length > len) ? len : actual_length;
  574. if (packet != NULL) {
  575. memcpy(frame, packet, actual_length);
  576. }
  577. designware_free_pkt(handle);
  578. return actual_length;
  579. }
  580. /**
  581. \brief This function is used to initialize Ethernet device and register an event callback.
  582. \param[in] idx device id
  583. \param[in] cb callback to handle ethernet event
  584. \return return ethernet handle if success
  585. */
  586. eth_mac_handle_t cvi_eth_mac_init(rt_ubase_t base)
  587. {
  588. gmac_dev_t *mac_dev = &gmac_instance[0];
  589. struct dw_gmac_priv *priv, *priv_unalign;
  590. mac_dev->base = base;
  591. priv = memalign(DW_GMAC_DMA_ALIGN, sizeof(struct dw_gmac_priv), (void **)&priv_unalign);
  592. if (!priv)
  593. {
  594. rt_kprintf("malloc fail\n");
  595. return NULL;
  596. }
  597. memset(priv_unalign, 0, sizeof(struct dw_gmac_priv) + DW_GMAC_DMA_ALIGN);
  598. priv->mac_regs_p = (struct dw_gmac_mac_regs *)mac_dev->base;
  599. priv->dma_regs_p = (struct dw_gmac_dma_regs *)(mac_dev->base + CVI_DW_DMA_BASE_OFFSET);
  600. mac_dev->priv_unalign = priv_unalign;
  601. mac_dev->priv = priv;
  602. return (eth_mac_handle_t)mac_dev;
  603. }
  604. /**
  605. \brief This function is used to de-initialize Ethernet device.
  606. \param[in] handle ethernet handle
  607. \return error code
  608. */
  609. void de_eth_gmac_deinit(eth_mac_handle_t handle)
  610. {
  611. RT_ASSERT(handle);
  612. gmac_dev_t *mac_dev = (gmac_dev_t *)handle;
  613. if (mac_dev->priv_unalign)
  614. {
  615. rt_free(mac_dev->priv_unalign);
  616. mac_dev->priv_unalign = RT_NULL;
  617. }
  618. }