synopGMAC.c 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974
  1. /*
  2. * Copyright (c) 2006-2018, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2017-08-24 chinesebear first version
  9. */
  10. #include <rtthread.h>
  11. #include <rtdef.h>
  12. //#include <lwip/pbuf.h>
  13. #include "synopGMAC.h"
  14. #include "mii.c"
  15. #include "synopGMAC_debug.h"
  16. #include <ls1c.h>
  17. #include "ls1c_pin.h"
  18. #define RMII
  19. #define Gmac_base 0xbfe10000
  20. #define Buffer_Size 2048
  21. #define MAX_ADDR_LEN 6
  22. #define NAMESIZE 16
  23. #define LS1B_GMAC0_IRQ 34
  24. #define BUS_SIZE_ALIGN(x) ((x+15)&~15)
  25. #define DEFAULT_MAC_ADDRESS {0x00, 0x55, 0x7B, 0xB5, 0x7D, 0xF7}
  26. u32 regbase = 0xbfe10000;
  27. static u32 GMAC_Power_down;
  28. extern void *plat_alloc_consistent_dmaable_memory(synopGMACdevice *pcidev, u32 size, u32 *addr) ;
  29. extern s32 synopGMAC_check_phy_init(synopGMACPciNetworkAdapter *adapter) ;
  30. extern int init_phy(synopGMACdevice *gmacdev);
  31. dma_addr_t plat_dma_map_single(void *hwdev, void *ptr, u32 size);
  32. void eth_rx_irq(int irqno, void *param);
  33. static char Rx_Buffer[Buffer_Size];
  34. static char Tx_Buffer[Buffer_Size];
  35. struct rt_eth_dev
  36. {
  37. struct eth_device parent;
  38. rt_uint8_t dev_addr[MAX_ADDR_LEN];
  39. char *name;
  40. int iobase;
  41. int state;
  42. int index;
  43. struct rt_timer link_timer;
  44. struct rt_timer rx_poll_timer;
  45. void *priv;
  46. };
  47. static struct rt_eth_dev eth_dev;
  48. static struct rt_semaphore sem_ack, sem_lock;
  49. /**
  50. * This sets up the transmit Descriptor queue in ring or chain mode.
  51. * This function is tightly coupled to the platform and operating system
  52. * Device is interested only after the descriptors are setup. Therefore this function
  53. * is not included in the device driver API. This function should be treated as an
  54. * example code to design the descriptor structures for ring mode or chain mode.
  55. * This function depends on the pcidev structure for allocation consistent dma-able memory in case
  56. * of linux.
  57. * This limitation is due to the fact that linux uses pci structure to allocate a dmable memory
  58. * - Allocates the memory for the descriptors.
  59. * - Initialize the Busy and Next descriptors indices to 0(Indicating first descriptor).
  60. * - Initialize the Busy and Next descriptors to first descriptor address.
  61. * - Initialize the last descriptor with the endof ring in case of ring mode.
  62. * - Initialize the descriptors in chain mode.
  63. * @param[in] pointer to synopGMACdevice.
  64. * @param[in] pointer to pci_device structure.
  65. * @param[in] number of descriptor expected in tx descriptor queue.
  66. * @param[in] whether descriptors to be created in RING mode or CHAIN mode.
  67. * \return 0 upon success. Error code upon failure.
  68. * \note This function fails if allocation fails for required number of descriptors in Ring mode,
  69. * but in chain mode
  70. * function returns -ESYNOPGMACNOMEM in the process of descriptor chain creation. once returned from
  71. * this function
  72. * user should for gmacdev->TxDescCount to see how many descriptors are there in the chain. Should
  73. * continue further
  74. * only if the number of descriptors in the chain meets the requirements
  75. */
  76. s32 synopGMAC_setup_tx_desc_queue(synopGMACdevice *gmacdev, u32 no_of_desc, u32 desc_mode)
  77. {
  78. s32 i;
  79. DmaDesc *bf1;
  80. DmaDesc *first_desc = NULL;
  81. dma_addr_t dma_addr;
  82. gmacdev->TxDescCount = 0;
  83. first_desc = (DmaDesc *)plat_alloc_consistent_dmaable_memory(gmacdev, sizeof(DmaDesc) * no_of_desc, &dma_addr);
  84. if (first_desc == NULL)
  85. {
  86. rt_kprintf("Error in Tx Descriptors memory allocation\n");
  87. return -ESYNOPGMACNOMEM;
  88. }
  89. DEBUG_MES("tx_first_desc_addr = %p\n", first_desc);
  90. DEBUG_MES("dmaadr = %p\n", dma_addr);
  91. gmacdev->TxDescCount = no_of_desc;
  92. gmacdev->TxDesc = first_desc;
  93. gmacdev->TxDescDma = dma_addr;
  94. for (i = 0; i < gmacdev->TxDescCount; i++)
  95. {
  96. synopGMAC_tx_desc_init_ring(gmacdev->TxDesc + i, i == gmacdev->TxDescCount - 1);
  97. #if SYNOP_TOP_DEBUG
  98. rt_kprintf("\n%02d %08x \n", i, (unsigned int)(gmacdev->TxDesc + i));
  99. rt_kprintf("%08x ", (unsigned int)((gmacdev->TxDesc + i))->status);
  100. rt_kprintf("%08x ", (unsigned int)((gmacdev->TxDesc + i)->length));
  101. rt_kprintf("%08x ", (unsigned int)((gmacdev->TxDesc + i)->buffer1));
  102. rt_kprintf("%08x ", (unsigned int)((gmacdev->TxDesc + i)->buffer2));
  103. rt_kprintf("%08x ", (unsigned int)((gmacdev->TxDesc + i)->data1));
  104. rt_kprintf("%08x ", (unsigned int)((gmacdev->TxDesc + i)->data2));
  105. rt_kprintf("%08x ", (unsigned int)((gmacdev->TxDesc + i)->dummy1));
  106. rt_kprintf("%08x ", (unsigned int)((gmacdev->TxDesc + i)->dummy2));
  107. #endif
  108. }
  109. gmacdev->TxNext = 0;
  110. gmacdev->TxBusy = 0;
  111. gmacdev->TxNextDesc = gmacdev->TxDesc;
  112. gmacdev->TxBusyDesc = gmacdev->TxDesc;
  113. gmacdev->BusyTxDesc = 0;
  114. return -ESYNOPGMACNOERR;
  115. }
  116. /**
  117. * This sets up the receive Descriptor queue in ring or chain mode.
  118. * This function is tightly coupled to the platform and operating system
  119. * Device is interested only after the descriptors are setup. Therefore this function
  120. * is not included in the device driver API. This function should be treated as an
  121. * example code to design the descriptor structures in ring mode or chain mode.
  122. * This function depends on the pcidev structure for allocation of consistent dma-able memory in
  123. * case of linux.
  124. * This limitation is due to the fact that linux uses pci structure to allocate a dmable memory
  125. * - Allocates the memory for the descriptors.
  126. * - Initialize the Busy and Next descriptors indices to 0(Indicating first descriptor).
  127. * - Initialize the Busy and Next descriptors to first descriptor address.
  128. * - Initialize the last descriptor with the endof ring in case of ring mode.
  129. * - Initialize the descriptors in chain mode.
  130. * @param[in] pointer to synopGMACdevice.
  131. * @param[in] pointer to pci_device structure.
  132. * @param[in] number of descriptor expected in rx descriptor queue.
  133. * @param[in] whether descriptors to be created in RING mode or CHAIN mode.
  134. * \return 0 upon success. Error code upon failure.
  135. * \note This function fails if allocation fails for required number of descriptors in Ring mode,
  136. * but in chain mode
  137. * function returns -ESYNOPGMACNOMEM in the process of descriptor chain creation. once returned from
  138. * this function
  139. * user should for gmacdev->RxDescCount to see how many descriptors are there in the chain. Should
  140. * continue further
  141. * only if the number of descriptors in the chain meets the requirements
  142. */
  143. s32 synopGMAC_setup_rx_desc_queue(synopGMACdevice *gmacdev, u32 no_of_desc, u32 desc_mode)
  144. {
  145. s32 i;
  146. DmaDesc *bf1;
  147. DmaDesc *first_desc = NULL;
  148. dma_addr_t dma_addr;
  149. gmacdev->RxDescCount = 0;
  150. first_desc = (DmaDesc *)plat_alloc_consistent_dmaable_memory(gmacdev, sizeof(DmaDesc) * no_of_desc, &dma_addr);
  151. if (first_desc == NULL)
  152. {
  153. rt_kprintf("Error in Rx Descriptor Memory allocation in Ring mode\n");
  154. return -ESYNOPGMACNOMEM;
  155. }
  156. DEBUG_MES("rx_first_desc_addr = %p\n", first_desc);
  157. DEBUG_MES("dmaadr = %p\n", dma_addr);
  158. gmacdev->RxDescCount = no_of_desc;
  159. gmacdev->RxDesc = (DmaDesc *)first_desc;
  160. gmacdev->RxDescDma = dma_addr;
  161. for (i = 0; i < gmacdev->RxDescCount; i++)
  162. {
  163. synopGMAC_rx_desc_init_ring(gmacdev->RxDesc + i, i == gmacdev->RxDescCount - 1);
  164. }
  165. gmacdev->RxNext = 0;
  166. gmacdev->RxBusy = 0;
  167. gmacdev->RxNextDesc = gmacdev->RxDesc;
  168. gmacdev->RxBusyDesc = gmacdev->RxDesc;
  169. gmacdev->BusyRxDesc = 0;
  170. return -ESYNOPGMACNOERR;
  171. }
  172. void synopGMAC_linux_cable_unplug_function(void *adaptr)
  173. {
  174. s32 data;
  175. synopGMACPciNetworkAdapter *adapter = (synopGMACPciNetworkAdapter *)adaptr;
  176. synopGMACdevice *gmacdev = adapter->synopGMACdev;
  177. struct ethtool_cmd cmd;
  178. //rt_kprintf("%s\n",__FUNCTION__);
  179. if (!mii_link_ok(&adapter->mii))
  180. {
  181. if (gmacdev->LinkState)
  182. rt_kprintf("\r\nNo Link\r\n");
  183. gmacdev->DuplexMode = 0;
  184. gmacdev->Speed = 0;
  185. gmacdev->LoopBackMode = 0;
  186. gmacdev->LinkState = 0;
  187. }
  188. else
  189. {
  190. data = synopGMAC_check_phy_init(adapter);
  191. if (gmacdev->LinkState != data)
  192. {
  193. gmacdev->LinkState = data;
  194. synopGMAC_mac_init(gmacdev);
  195. rt_kprintf("Link is up in %s mode\n", (gmacdev->DuplexMode == FULLDUPLEX) ? "FULL DUPLEX" : "HALF DUPLEX");
  196. if (gmacdev->Speed == SPEED1000)
  197. rt_kprintf("Link is with 1000M Speed \r\n");
  198. if (gmacdev->Speed == SPEED100)
  199. rt_kprintf("Link is with 100M Speed \n");
  200. if (gmacdev->Speed == SPEED10)
  201. rt_kprintf("Link is with 10M Speed \n");
  202. }
  203. }
  204. }
  205. s32 synopGMAC_check_phy_init(synopGMACPciNetworkAdapter *adapter)
  206. {
  207. struct ethtool_cmd cmd;
  208. synopGMACdevice *gmacdev = adapter->synopGMACdev;
  209. if (!mii_link_ok(&adapter->mii))
  210. {
  211. gmacdev->DuplexMode = FULLDUPLEX;
  212. gmacdev->Speed = SPEED100;
  213. return 0;
  214. }
  215. else
  216. {
  217. mii_ethtool_gset(&adapter->mii, &cmd);
  218. gmacdev->DuplexMode = (cmd.duplex == DUPLEX_FULL) ? FULLDUPLEX : HALFDUPLEX ;
  219. if (cmd.speed == SPEED_1000)
  220. gmacdev->Speed = SPEED1000;
  221. else if (cmd.speed == SPEED_100)
  222. gmacdev->Speed = SPEED100;
  223. else
  224. gmacdev->Speed = SPEED10;
  225. }
  226. return gmacdev->Speed | (gmacdev->DuplexMode << 4);
  227. }
  228. static int Mac_change_check(u8 *macaddr0, u8 *macaddr1)
  229. {
  230. int i;
  231. for (i = 0; i < 6; i++)
  232. {
  233. if (macaddr0[i] != macaddr1[i])
  234. return 1;
  235. }
  236. return 0;
  237. }
  238. static rt_err_t eth_init(rt_device_t device)
  239. {
  240. struct eth_device *eth_device = (struct eth_device *)device;
  241. RT_ASSERT(eth_device != RT_NULL);
  242. s32 ijk;
  243. s32 status = 0;
  244. u64 dma_addr;
  245. u32 Mac_changed = 0;
  246. struct pbuf *pbuf;
  247. u8 macaddr[6] = DEFAULT_MAC_ADDRESS;
  248. struct rt_eth_dev *dev = &eth_dev;
  249. struct synopGMACNetworkAdapter *adapter = dev->priv;
  250. synopGMACdevice *gmacdev = (synopGMACdevice *)adapter->synopGMACdev;
  251. synopGMAC_reset(gmacdev);
  252. synopGMAC_attach(gmacdev, (regbase + MACBASE), (regbase + DMABASE), DEFAULT_PHY_BASE, macaddr);
  253. synopGMAC_read_version(gmacdev);
  254. synopGMAC_set_mdc_clk_div(gmacdev, GmiiCsrClk3);
  255. gmacdev->ClockDivMdc = synopGMAC_get_mdc_clk_div(gmacdev);
  256. init_phy(adapter->synopGMACdev);
  257. DEBUG_MES("tx desc_queue\n");
  258. synopGMAC_setup_tx_desc_queue(gmacdev, TRANSMIT_DESC_SIZE, RINGMODE);
  259. synopGMAC_init_tx_desc_base(gmacdev);
  260. DEBUG_MES("rx desc_queue\n");
  261. synopGMAC_setup_rx_desc_queue(gmacdev, RECEIVE_DESC_SIZE, RINGMODE);
  262. synopGMAC_init_rx_desc_base(gmacdev);
  263. DEBUG_MES("DmaRxBaseAddr = %08x\n", synopGMACReadReg(gmacdev->DmaBase, DmaRxBaseAddr));
  264. // u32 dmaRx_Base_addr = synopGMACReadReg(gmacdev->DmaBase,DmaRxBaseAddr);
  265. // rt_kprintf("first_desc_addr = 0x%x\n", dmaRx_Base_addr);
  266. #ifdef ENH_DESC_8W
  267. synopGMAC_dma_bus_mode_init(gmacdev, DmaBurstLength32 | DmaDescriptorSkip2 | DmaDescriptor8Words);
  268. #else
  269. //synopGMAC_dma_bus_mode_init(gmacdev, DmaBurstLength4 | DmaDescriptorSkip1);
  270. synopGMAC_dma_bus_mode_init(gmacdev, DmaBurstLength4 | DmaDescriptorSkip2);
  271. #endif
  272. synopGMAC_dma_control_init(gmacdev, DmaStoreAndForward | DmaTxSecondFrame | DmaRxThreshCtrl128);
  273. status = synopGMAC_check_phy_init(adapter);
  274. synopGMAC_mac_init(gmacdev);
  275. synopGMAC_pause_control(gmacdev);
  276. #ifdef IPC_OFFLOAD
  277. synopGMAC_enable_rx_chksum_offload(gmacdev);
  278. synopGMAC_rx_tcpip_chksum_drop_enable(gmacdev);
  279. #endif
  280. u32 skb;
  281. do
  282. {
  283. skb = (u32)plat_alloc_memory(RX_BUF_SIZE); //should skb aligned here?
  284. if (skb == RT_NULL)
  285. {
  286. rt_kprintf("ERROR in skb buffer allocation\n");
  287. break;
  288. }
  289. dma_addr = plat_dma_map_single(gmacdev, (void *)skb, RX_BUF_SIZE); //获取 skb 的 dma 地址
  290. status = synopGMAC_set_rx_qptr(gmacdev, dma_addr, RX_BUF_SIZE, (u32)skb, 0, 0, 0);
  291. if (status < 0)
  292. {
  293. rt_kprintf("status < 0!!\n");
  294. plat_free_memory((void *)skb);
  295. }
  296. }
  297. while (status >= 0 && (status < (RECEIVE_DESC_SIZE - 1)));
  298. synopGMAC_clear_interrupt(gmacdev);
  299. synopGMAC_disable_mmc_tx_interrupt(gmacdev, 0xFFFFFFFF);
  300. synopGMAC_disable_mmc_rx_interrupt(gmacdev, 0xFFFFFFFF);
  301. synopGMAC_disable_mmc_ipc_rx_interrupt(gmacdev, 0xFFFFFFFF);
  302. // synopGMAC_disable_interrupt_all(gmacdev);
  303. synopGMAC_enable_interrupt(gmacdev, DmaIntEnable);
  304. synopGMAC_enable_dma_rx(gmacdev);
  305. synopGMAC_enable_dma_tx(gmacdev);
  306. plat_delay(DEFAULT_LOOP_VARIABLE);
  307. synopGMAC_check_phy_init(adapter);
  308. synopGMAC_mac_init(gmacdev);
  309. rt_timer_init(&dev->link_timer, "link_timer",
  310. synopGMAC_linux_cable_unplug_function,
  311. (void *)adapter,
  312. RT_TICK_PER_SECOND,
  313. RT_TIMER_FLAG_PERIODIC);
  314. rt_timer_start(&dev->link_timer);
  315. #ifdef RT_USING_GMAC_INT_MODE
  316. /* installl isr */
  317. DEBUG_MES("%s\n", __FUNCTION__);
  318. rt_hw_interrupt_install(LS1C_MAC_IRQ, eth_rx_irq, RT_NULL, "e0_isr");
  319. rt_hw_interrupt_umask(LS1C_MAC_IRQ);
  320. #else
  321. rt_timer_init(&dev->rx_poll_timer, "rx_poll_timer",
  322. eth_rx_irq,
  323. (void *)adapter,
  324. 1,
  325. RT_TIMER_FLAG_PERIODIC);
  326. rt_timer_start(&dev->rx_poll_timer);
  327. #endif /*RT_USING_GMAC_INT_MODE*/
  328. rt_kprintf("eth_inited!\n");
  329. return RT_EOK;
  330. }
  331. static rt_err_t eth_open(rt_device_t dev, rt_uint16_t oflag)
  332. {
  333. rt_kprintf("eth_open!!\n");
  334. return RT_EOK;
  335. }
  336. static rt_err_t eth_close(rt_device_t dev)
  337. {
  338. return RT_EOK;
  339. }
  340. static rt_size_t eth_read(rt_device_t dev, rt_off_t pos, void *buffer, rt_size_t size)
  341. {
  342. rt_set_errno(-RT_ENOSYS);
  343. return 0;
  344. }
  345. static rt_size_t eth_write(rt_device_t dev, rt_off_t pos, const void *buffer, rt_size_t size)
  346. {
  347. rt_set_errno(-RT_ENOSYS);
  348. return 0;
  349. }
  350. static rt_err_t eth_control(rt_device_t dev, int cmd, void *args)
  351. {
  352. switch (cmd)
  353. {
  354. case NIOCTL_GADDR:
  355. if (args) rt_memcpy(args, eth_dev.dev_addr, 6);
  356. else return -RT_ERROR;
  357. break;
  358. default :
  359. break;
  360. }
  361. return RT_EOK;
  362. }
  363. rt_err_t rt_eth_tx(rt_device_t device, struct pbuf *p)
  364. {
  365. /* lock eth device */
  366. rt_sem_take(&sem_lock, RT_WAITING_FOREVER);
  367. DEBUG_MES("in %s\n", __FUNCTION__);
  368. s32 status;
  369. u32 pbuf;
  370. u64 dma_addr;
  371. u32 offload_needed = 0;
  372. u32 index;
  373. DmaDesc *dpr;
  374. struct rt_eth_dev *dev = (struct rt_eth_dev *) device;
  375. struct synopGMACNetworkAdapter *adapter;
  376. synopGMACdevice *gmacdev;
  377. adapter = (struct synopGMACNetworkAdapter *) dev->priv;
  378. if (adapter == NULL)
  379. return -1;
  380. gmacdev = (synopGMACdevice *) adapter->synopGMACdev;
  381. if (gmacdev == NULL)
  382. return -1;
  383. if (!synopGMAC_is_desc_owned_by_dma(gmacdev->TxNextDesc))
  384. {
  385. pbuf = (u32)plat_alloc_memory(p->tot_len);
  386. //pbuf = (u32)pbuf_alloc(PBUF_LINK, p->len, PBUF_RAM);
  387. if (pbuf == 0)
  388. {
  389. rt_kprintf("===error in alloc bf1\n");
  390. return -1;
  391. }
  392. DEBUG_MES("p->len = %d\n", p->len);
  393. pbuf_copy_partial(p, (void *)pbuf, p->tot_len, 0);
  394. dma_addr = plat_dma_map_single(gmacdev, (void *)pbuf, p->tot_len);
  395. status = synopGMAC_set_tx_qptr(gmacdev, dma_addr, p->tot_len, pbuf, 0, 0, 0, offload_needed, &index, dpr);
  396. if (status < 0)
  397. {
  398. rt_kprintf("%s No More Free Tx Descriptors\n", __FUNCTION__);
  399. plat_free_memory((void *)pbuf);
  400. return -16;
  401. }
  402. }
  403. synopGMAC_resume_dma_tx(gmacdev);
  404. s32 desc_index;
  405. u32 data1, data2;
  406. u32 dma_addr1, dma_addr2;
  407. u32 length1, length2;
  408. #ifdef ENH_DESC_8W
  409. u32 ext_status;
  410. u16 time_stamp_higher;
  411. u32 time_stamp_high;
  412. u32 time_stamp_low;
  413. #endif
  414. do
  415. {
  416. #ifdef ENH_DESC_8W
  417. desc_index = synopGMAC_get_tx_qptr(gmacdev, &status, &dma_addr1, &length1, &data1, &dma_addr2, &length2, &data2, &ext_status, &time_stamp_high, &time_stamp_low);
  418. synopGMAC_TS_read_timestamp_higher_val(gmacdev, &time_stamp_higher);
  419. #else
  420. desc_index = synopGMAC_get_tx_qptr(gmacdev, &status, &dma_addr1, &length1, &data1, &dma_addr2, &length2, &data2);
  421. #endif
  422. if (desc_index >= 0 && data1 != 0)
  423. {
  424. #ifdef IPC_OFFLOAD
  425. if (synopGMAC_is_tx_ipv4header_checksum_error(gmacdev, status))
  426. {
  427. rt_kprintf("Harware Failed to Insert IPV4 Header Checksum\n");
  428. }
  429. if (synopGMAC_is_tx_payload_checksum_error(gmacdev, status))
  430. {
  431. rt_kprintf("Harware Failed to Insert Payload Checksum\n");
  432. }
  433. #endif
  434. plat_free_memory((void *)(data1)); //sw: data1 = buffer1
  435. if (synopGMAC_is_desc_valid(status))
  436. {
  437. adapter->synopGMACNetStats.tx_bytes += length1;
  438. adapter->synopGMACNetStats.tx_packets++;
  439. }
  440. else
  441. {
  442. adapter->synopGMACNetStats.tx_errors++;
  443. adapter->synopGMACNetStats.tx_aborted_errors += synopGMAC_is_tx_aborted(status);
  444. adapter->synopGMACNetStats.tx_carrier_errors += synopGMAC_is_tx_carrier_error(status);
  445. }
  446. }
  447. adapter->synopGMACNetStats.collisions += synopGMAC_get_tx_collision_count(status);
  448. }
  449. while (desc_index >= 0);
  450. /* unlock eth device */
  451. rt_sem_release(&sem_lock);
  452. // rt_kprintf("output %d bytes\n", p->len);
  453. u32 test_data;
  454. test_data = synopGMACReadReg(gmacdev->DmaBase, DmaStatus);
  455. return RT_EOK;
  456. }
  457. struct pbuf *rt_eth_rx(rt_device_t device)
  458. {
  459. DEBUG_MES("%s : \n", __FUNCTION__);
  460. struct rt_eth_dev *dev = &eth_dev;
  461. struct synopGMACNetworkAdapter *adapter;
  462. synopGMACdevice *gmacdev;
  463. // struct PmonInet * pinetdev;
  464. s32 desc_index;
  465. int i;
  466. char *ptr;
  467. u32 bf1;
  468. u32 data1;
  469. u32 data2;
  470. u32 len;
  471. u32 status;
  472. u32 dma_addr1;
  473. u32 dma_addr2;
  474. struct pbuf *pbuf = RT_NULL;
  475. rt_sem_take(&sem_lock, RT_WAITING_FOREVER);
  476. adapter = (struct synopGMACNetworkAdapter *) dev->priv;
  477. if (adapter == NULL)
  478. {
  479. rt_kprintf("%S : Unknown Device !!\n", __FUNCTION__);
  480. return NULL;
  481. }
  482. gmacdev = (synopGMACdevice *) adapter->synopGMACdev;
  483. if (gmacdev == NULL)
  484. {
  485. rt_kprintf("%s : GMAC device structure is missing\n", __FUNCTION__);
  486. return NULL;
  487. }
  488. /*Handle the Receive Descriptors*/
  489. do
  490. {
  491. desc_index = synopGMAC_get_rx_qptr(gmacdev, &status, &dma_addr1, NULL, &data1, &dma_addr2, NULL, &data2);
  492. if (desc_index >= 0 && data1 != 0)
  493. {
  494. DEBUG_MES("Received Data at Rx Descriptor %d for skb 0x%08x whose status is %08x\n", desc_index, dma_addr1, status);
  495. if (synopGMAC_is_rx_desc_valid(status) || SYNOP_PHY_LOOPBACK)
  496. {
  497. pbuf = pbuf_alloc(PBUF_LINK, MAX_ETHERNET_PAYLOAD, PBUF_RAM);
  498. if (pbuf == 0) rt_kprintf("===error in pbuf_alloc\n");
  499. dma_addr1 = plat_dma_map_single(gmacdev, (void *)data1, RX_BUF_SIZE);
  500. len = synopGMAC_get_rx_desc_frame_length(status); //Not interested in Ethernet CRC bytes
  501. rt_memcpy(pbuf->payload, (char *)data1, len);
  502. DEBUG_MES("==get pkg len: %d\n", len);
  503. }
  504. else
  505. {
  506. rt_kprintf("s: %08x\n", status);
  507. adapter->synopGMACNetStats.rx_errors++;
  508. adapter->synopGMACNetStats.collisions += synopGMAC_is_rx_frame_collision(status);
  509. adapter->synopGMACNetStats.rx_crc_errors += synopGMAC_is_rx_crc(status);
  510. adapter->synopGMACNetStats.rx_frame_errors += synopGMAC_is_frame_dribbling_errors(status);
  511. adapter->synopGMACNetStats.rx_length_errors += synopGMAC_is_rx_frame_length_errors(status);
  512. }
  513. desc_index = synopGMAC_set_rx_qptr(gmacdev, dma_addr1, RX_BUF_SIZE, (u32)data1, 0, 0, 0);
  514. if (desc_index < 0)
  515. {
  516. #if SYNOP_RX_DEBUG
  517. rt_kprintf("Cannot set Rx Descriptor for data1 %08x\n", (u32)data1);
  518. #endif
  519. plat_free_memory((void *)data1);
  520. }
  521. }
  522. }while(desc_index >= 0);
  523. rt_sem_release(&sem_lock);
  524. DEBUG_MES("%s : before return \n", __FUNCTION__);
  525. return pbuf;
  526. }
  527. static int rtl88e1111_config_init(synopGMACdevice *gmacdev)
  528. {
  529. int retval, err;
  530. u16 data;
  531. DEBUG_MES("in %s\n", __FUNCTION__);
  532. synopGMAC_read_phy_reg(gmacdev->MacBase, gmacdev->PhyBase, 0x14, &data);
  533. data = data | 0x82;
  534. err = synopGMAC_write_phy_reg(gmacdev->MacBase, gmacdev->PhyBase, 0x14, data);
  535. synopGMAC_read_phy_reg(gmacdev->MacBase, gmacdev->PhyBase, 0x00, &data);
  536. data = data | 0x8000;
  537. err = synopGMAC_write_phy_reg(gmacdev->MacBase, gmacdev->PhyBase, 0x00, data);
  538. #if SYNOP_PHY_LOOPBACK
  539. synopGMAC_read_phy_reg(gmacdev->MacBase, gmacdev->PhyBase, 0x14, &data);
  540. data = data | 0x70;
  541. data = data & 0xffdf;
  542. err = synopGMAC_write_phy_reg(gmacdev->MacBase, gmacdev->PhyBase, 0x14, data);
  543. data = 0x8000;
  544. err = synopGMAC_write_phy_reg(gmacdev->MacBase, gmacdev->PhyBase, 0x00, data);
  545. data = 0x5140;
  546. err = synopGMAC_write_phy_reg(gmacdev->MacBase, gmacdev->PhyBase, 0x00, data);
  547. #endif
  548. if (err < 0)
  549. return err;
  550. return 0;
  551. }
  552. int init_phy(synopGMACdevice *gmacdev)
  553. {
  554. u16 data;
  555. synopGMAC_read_phy_reg(gmacdev->MacBase, gmacdev->PhyBase, 2, &data);
  556. /*set 88e1111 clock phase delay*/
  557. if (data == 0x141)
  558. rtl88e1111_config_init(gmacdev);
  559. #if defined (RMII)
  560. else if (data == 0x8201)
  561. {
  562. //RTL8201
  563. data = 0x400; // set RMII mode
  564. synopGMAC_write_phy_reg(gmacdev->MacBase, gmacdev->PhyBase, 0x19, data);
  565. synopGMAC_read_phy_reg(gmacdev->MacBase, gmacdev->PhyBase, 0x19, &data);
  566. TR("phy reg25 is %0x \n", data);
  567. data = 0x3100; //set 100M speed
  568. synopGMAC_write_phy_reg(gmacdev->MacBase, gmacdev->PhyBase, 0x0, data);
  569. }
  570. else if (data == 0x0180 || data == 0x0181)
  571. {
  572. //DM9161
  573. synopGMAC_read_phy_reg(gmacdev->MacBase, gmacdev->PhyBase, 0x10, &data);
  574. data |= (1 << 8); //set RMII mode
  575. synopGMAC_write_phy_reg(gmacdev->MacBase, gmacdev->PhyBase, 0x10, data); //set RMII mode
  576. synopGMAC_read_phy_reg(gmacdev->MacBase, gmacdev->PhyBase, 0x10, &data);
  577. TR("phy reg16 is 0x%0x \n", data);
  578. // synopGMAC_read_phy_reg(gmacdev->MacBase,gmacdev->PhyBase,0x0,&data);
  579. // data &= ~(1<<10);
  580. data = 0x3100; //set auto-
  581. //data = 0x0100; //set 10M speed
  582. synopGMAC_write_phy_reg(gmacdev->MacBase, gmacdev->PhyBase, 0x0, data);
  583. }
  584. #endif
  585. return 0;
  586. }
  587. u32 synopGMAC_wakeup_filter_config3[] =
  588. {
  589. 0x00000000,
  590. 0x000000FF,
  591. 0x00000000,
  592. 0x00000000,
  593. 0x00000100,
  594. 0x00003200,
  595. 0x7eED0000,
  596. 0x00000000
  597. };
  598. static void synopGMAC_linux_powerdown_mac(synopGMACdevice *gmacdev)
  599. {
  600. rt_kprintf("Put the GMAC to power down mode..\n");
  601. GMAC_Power_down = 1;
  602. synopGMAC_disable_dma_tx(gmacdev);
  603. plat_delay(10000);
  604. synopGMAC_tx_disable(gmacdev);
  605. synopGMAC_rx_disable(gmacdev);
  606. plat_delay(10000);
  607. synopGMAC_disable_dma_rx(gmacdev);
  608. synopGMAC_magic_packet_enable(gmacdev);
  609. synopGMAC_write_wakeup_frame_register(gmacdev, synopGMAC_wakeup_filter_config3);
  610. synopGMAC_wakeup_frame_enable(gmacdev);
  611. synopGMAC_rx_enable(gmacdev);
  612. synopGMAC_pmt_int_enable(gmacdev);
  613. synopGMAC_power_down_enable(gmacdev);
  614. return;
  615. }
  616. static void synopGMAC_linux_powerup_mac(synopGMACdevice *gmacdev)
  617. {
  618. GMAC_Power_down = 0;
  619. if (synopGMAC_is_magic_packet_received(gmacdev))
  620. rt_kprintf("GMAC wokeup due to Magic Pkt Received\n");
  621. if (synopGMAC_is_wakeup_frame_received(gmacdev))
  622. rt_kprintf("GMAC wokeup due to Wakeup Frame Received\n");
  623. synopGMAC_pmt_int_disable(gmacdev);
  624. synopGMAC_rx_enable(gmacdev);
  625. synopGMAC_enable_dma_rx(gmacdev);
  626. synopGMAC_tx_enable(gmacdev);
  627. synopGMAC_enable_dma_tx(gmacdev);
  628. return;
  629. }
  630. static int mdio_read(synopGMACPciNetworkAdapter *adapter, int addr, int reg)
  631. {
  632. synopGMACdevice *gmacdev;
  633. u16 data;
  634. gmacdev = adapter->synopGMACdev;
  635. synopGMAC_read_phy_reg(gmacdev->MacBase, addr, reg, &data);
  636. return data;
  637. }
  638. static void mdio_write(synopGMACPciNetworkAdapter *adapter, int addr, int reg, int data)
  639. {
  640. synopGMACdevice *gmacdev;
  641. gmacdev = adapter->synopGMACdev;
  642. synopGMAC_write_phy_reg(gmacdev->MacBase, addr, reg, data);
  643. }
  644. void eth_rx_irq(int irqno, void *param)
  645. {
  646. struct rt_eth_dev *dev = &eth_dev;
  647. struct synopGMACNetworkAdapter *adapter = dev->priv;
  648. //DEBUG_MES("in irq!!\n");
  649. #ifdef RT_USING_GMAC_INT_MODE
  650. int i ;
  651. for (i = 0; i < 7200; i++)
  652. ;
  653. #endif /*RT_USING_GMAC_INT_MODE*/
  654. synopGMACdevice *gmacdev = (synopGMACdevice *)adapter->synopGMACdev;
  655. u32 interrupt, dma_status_reg;
  656. s32 status;
  657. u32 dma_addr;
  658. //rt_kprintf("irq i = %d\n", i++);
  659. dma_status_reg = synopGMACReadReg(gmacdev->DmaBase, DmaStatus);
  660. if (dma_status_reg == 0)
  661. {
  662. rt_kprintf("dma_status ==0 \n");
  663. return;
  664. }
  665. //rt_kprintf("dma_status_reg is 0x%x\n", dma_status_reg);
  666. u32 gmacstatus;
  667. synopGMAC_disable_interrupt_all(gmacdev);
  668. gmacstatus = synopGMACReadReg(gmacdev->MacBase, GmacStatus);
  669. if (dma_status_reg & GmacPmtIntr)
  670. {
  671. rt_kprintf("%s:: Interrupt due to PMT module\n", __FUNCTION__);
  672. //synopGMAC_linux_powerup_mac(gmacdev);
  673. }
  674. if (dma_status_reg & GmacMmcIntr)
  675. {
  676. rt_kprintf("%s:: Interrupt due to MMC module\n", __FUNCTION__);
  677. DEBUG_MES("%s:: synopGMAC_rx_int_status = %08x\n", __FUNCTION__, synopGMAC_read_mmc_rx_int_status(gmacdev));
  678. DEBUG_MES("%s:: synopGMAC_tx_int_status = %08x\n", __FUNCTION__, synopGMAC_read_mmc_tx_int_status(gmacdev));
  679. }
  680. if (dma_status_reg & GmacLineIntfIntr)
  681. {
  682. rt_kprintf("%s:: Interrupt due to GMAC LINE module\n", __FUNCTION__);
  683. }
  684. interrupt = synopGMAC_get_interrupt_type(gmacdev);
  685. //rt_kprintf("%s:Interrupts to be handled: 0x%08x\n",__FUNCTION__,interrupt);
  686. if (interrupt & synopGMACDmaError)
  687. {
  688. u8 mac_addr0[6];
  689. rt_kprintf("%s::Fatal Bus Error Inetrrupt Seen\n", __FUNCTION__);
  690. memcpy(mac_addr0, dev->dev_addr, 6);
  691. synopGMAC_disable_dma_tx(gmacdev);
  692. synopGMAC_disable_dma_rx(gmacdev);
  693. synopGMAC_take_desc_ownership_tx(gmacdev);
  694. synopGMAC_take_desc_ownership_rx(gmacdev);
  695. synopGMAC_init_tx_rx_desc_queue(gmacdev);
  696. synopGMAC_reset(gmacdev);
  697. synopGMAC_set_mac_addr(gmacdev, GmacAddr0High, GmacAddr0Low, mac_addr0);
  698. synopGMAC_dma_bus_mode_init(gmacdev, DmaFixedBurstEnable | DmaBurstLength8 | DmaDescriptorSkip2);
  699. synopGMAC_dma_control_init(gmacdev, DmaStoreAndForward);
  700. synopGMAC_init_rx_desc_base(gmacdev);
  701. synopGMAC_init_tx_desc_base(gmacdev);
  702. synopGMAC_mac_init(gmacdev);
  703. synopGMAC_enable_dma_rx(gmacdev);
  704. synopGMAC_enable_dma_tx(gmacdev);
  705. }
  706. if (interrupt & synopGMACDmaRxNormal)
  707. {
  708. //DEBUG_MES("%s:: Rx Normal \n", __FUNCTION__);
  709. //synop_handle_received_data(netdev);
  710. eth_device_ready(&eth_dev.parent);
  711. }
  712. if (interrupt & synopGMACDmaRxAbnormal)
  713. {
  714. //rt_kprintf("%s::Abnormal Rx Interrupt Seen\n",__FUNCTION__);
  715. if (GMAC_Power_down == 0)
  716. {
  717. adapter->synopGMACNetStats.rx_over_errors++;
  718. synopGMACWriteReg(gmacdev->DmaBase, DmaStatus, 0x80);
  719. synopGMAC_resume_dma_rx(gmacdev);
  720. }
  721. }
  722. if (interrupt & synopGMACDmaRxStopped)
  723. {
  724. rt_kprintf("%s::Receiver stopped seeing Rx interrupts\n", __FUNCTION__); //Receiver gone in to stopped state
  725. }
  726. if (interrupt & synopGMACDmaTxNormal)
  727. {
  728. DEBUG_MES("%s::Finished Normal Transmission \n", __FUNCTION__);
  729. // synop_handle_transmit_over(netdev);
  730. }
  731. if (interrupt & synopGMACDmaTxAbnormal)
  732. {
  733. rt_kprintf("%s::Abnormal Tx Interrupt Seen\n", __FUNCTION__);
  734. }
  735. if (interrupt & synopGMACDmaTxStopped)
  736. {
  737. TR("%s::Transmitter stopped sending the packets\n", __FUNCTION__);
  738. if (GMAC_Power_down == 0) // If Mac is not in powerdown
  739. {
  740. synopGMAC_disable_dma_tx(gmacdev);
  741. synopGMAC_take_desc_ownership_tx(gmacdev);
  742. synopGMAC_enable_dma_tx(gmacdev);
  743. // netif_wake_queue(netdev);
  744. TR("%s::Transmission Resumed\n", __FUNCTION__);
  745. }
  746. }
  747. /* Enable the interrrupt before returning from ISR*/
  748. synopGMAC_enable_interrupt(gmacdev, DmaIntEnable);
  749. return;
  750. }
  751. int rt_hw_eth_init(void)
  752. {
  753. u64 base_addr = Gmac_base;
  754. struct synopGMACNetworkAdapter *synopGMACadapter;
  755. static u8 mac_addr0[6] = DEFAULT_MAC_ADDRESS;
  756. int index;
  757. rt_sem_init(&sem_ack, "tx_ack", 1, RT_IPC_FLAG_FIFO);
  758. rt_sem_init(&sem_lock, "eth_lock", 1, RT_IPC_FLAG_FIFO);
  759. for (index = 21; index <= 30; index++)
  760. {
  761. pin_set_purpose(index, PIN_PURPOSE_OTHER);
  762. pin_set_remap(index, PIN_REMAP_DEFAULT);
  763. }
  764. pin_set_purpose(35, PIN_PURPOSE_OTHER);
  765. pin_set_remap(35, PIN_REMAP_DEFAULT);
  766. *((volatile unsigned int *)0xbfd00424) &= ~(7 << 28);
  767. *((volatile unsigned int *)0xbfd00424) |= (1 << 30); //wl rmii
  768. memset(&eth_dev, 0, sizeof(eth_dev));
  769. synopGMACadapter = (struct synopGMACNetworkAdapter *)plat_alloc_memory(sizeof(struct synopGMACNetworkAdapter));
  770. if (!synopGMACadapter)
  771. {
  772. rt_kprintf("Error in Memory Allocataion, Founction : %s \n", __FUNCTION__);
  773. }
  774. memset((char *)synopGMACadapter, 0, sizeof(struct synopGMACNetworkAdapter));
  775. synopGMACadapter->synopGMACdev = NULL;
  776. synopGMACadapter->synopGMACdev = (synopGMACdevice *) plat_alloc_memory(sizeof(synopGMACdevice));
  777. if (!synopGMACadapter->synopGMACdev)
  778. {
  779. rt_kprintf("Error in Memory Allocataion, Founction : %s \n", __FUNCTION__);
  780. }
  781. memset((char *)synopGMACadapter->synopGMACdev, 0, sizeof(synopGMACdevice));
  782. /*
  783. * Attach the device to MAC struct This will configure all the required base addresses
  784. * such as Mac base, configuration base, phy base address(out of 32 possible phys)
  785. * */
  786. synopGMAC_attach(synopGMACadapter->synopGMACdev, (regbase + MACBASE), regbase + DMABASE, DEFAULT_PHY_BASE, mac_addr0);
  787. init_phy(synopGMACadapter->synopGMACdev);
  788. synopGMAC_reset(synopGMACadapter->synopGMACdev);
  789. /* MII setup */
  790. synopGMACadapter->mii.phy_id_mask = 0x1F;
  791. synopGMACadapter->mii.reg_num_mask = 0x1F;
  792. synopGMACadapter->mii.dev = synopGMACadapter;
  793. synopGMACadapter->mii.mdio_read = mdio_read;
  794. synopGMACadapter->mii.mdio_write = mdio_write;
  795. synopGMACadapter->mii.phy_id = synopGMACadapter->synopGMACdev->PhyBase;
  796. synopGMACadapter->mii.supports_gmii = mii_check_gmii_support(&synopGMACadapter->mii);
  797. eth_dev.iobase = base_addr;
  798. eth_dev.name = "e0";
  799. eth_dev.priv = synopGMACadapter;
  800. eth_dev.dev_addr[0] = mac_addr0[0];
  801. eth_dev.dev_addr[1] = mac_addr0[1];
  802. eth_dev.dev_addr[2] = mac_addr0[2];
  803. eth_dev.dev_addr[3] = mac_addr0[3];
  804. eth_dev.dev_addr[4] = mac_addr0[4];
  805. eth_dev.dev_addr[5] = mac_addr0[5];
  806. eth_dev.parent.parent.type = RT_Device_Class_NetIf;
  807. eth_dev.parent.parent.init = eth_init;
  808. eth_dev.parent.parent.open = eth_open;
  809. eth_dev.parent.parent.close = eth_close;
  810. eth_dev.parent.parent.read = eth_read;
  811. eth_dev.parent.parent.write = eth_write;
  812. eth_dev.parent.parent.control = eth_control;
  813. eth_dev.parent.parent.user_data = RT_NULL;
  814. eth_dev.parent.eth_tx = rt_eth_tx;
  815. eth_dev.parent.eth_rx = rt_eth_rx;
  816. eth_device_init(&(eth_dev.parent), "e0");
  817. eth_device_linkchange(&eth_dev.parent, RT_TRUE); //linkup the e0 for lwip to check
  818. return 0;
  819. }
  820. INIT_DEVICE_EXPORT(rt_hw_eth_init);