synopGMAC.c 32 KB

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