1
0

synopGMAC.c 29 KB

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