synopGMAC.c 33 KB

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