drv_eth.c 44 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444
  1. /*
  2. * File : drv_eth.c
  3. * This file is part of RT-Thread RTOS
  4. * COPYRIGHT (C) 2009-2013 RT-Thread Develop Team
  5. *
  6. * The license and distribution terms for this file may be
  7. * found in the file LICENSE in this distribution or at
  8. * http://www.rt-thread.org/license/LICENSE
  9. *
  10. * Change Logs:
  11. * Date Author Notes
  12. * 2014-07-25 ArdaFu Port to TM4C129X
  13. */
  14. /**
  15. * @file - tivaif.c
  16. * lwIP Ethernet interface for Stellaris LM4F Devices
  17. *
  18. */
  19. /**
  20. * Copyright (c) 2001-2004 Swedish Institute of Computer Science.
  21. * All rights reserved.
  22. *
  23. * Redistribution and use in source and binary forms, with or without modification,
  24. * are permitted provided that the following conditions are met:
  25. *
  26. * 1. Redistributions of source code must retain the above copyright notice,
  27. * this list of conditions and the following disclaimer.
  28. * 2. Redistributions in binary form must reproduce the above copyright notice,
  29. * this list of conditions and the following disclaimer in the documentation
  30. * and/or other materials provided with the distribution.
  31. * 3. The name of the author may not be used to endorse or promote products
  32. * derived from this software without specific prior written permission.
  33. *
  34. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
  35. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  36. * MERCHANTABILITY AND FITNESS FOR A PARTICui32AR PURPOSE ARE DISCLAIMED. IN NO EVENT
  37. * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  38. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
  39. * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  40. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  41. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
  42. * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
  43. * OF SUCH DAMAGE.
  44. *
  45. * This file is part of the lwIP TCP/IP stack.
  46. *
  47. * Author: Adam Dunkels <adam@sics.se>
  48. *
  49. */
  50. /**
  51. * Copyright (c) 2008-2012 Texas Instruments Incorporated
  52. *
  53. * This file is derived from the ``ethernetif.c'' skeleton Ethernet network
  54. * interface driver for lwIP.
  55. *
  56. */
  57. #include "lwip/opt.h"
  58. #include "lwip/def.h"
  59. #include "lwip/mem.h"
  60. #include "lwip/pbuf.h"
  61. #include "lwip/sys.h"
  62. #include <lwip/stats.h>
  63. #include <lwip/snmp.h>
  64. #include "lwip/tcpip.h"
  65. #include "netif/etharp.h"
  66. #include "netif/ppp_oe.h"
  67. /**
  68. * Sanity Check: This interface driver will NOT work if the following defines
  69. * are incorrect.
  70. *
  71. */
  72. #if (PBUF_LINK_HLEN != 16)
  73. #error "PBUF_LINK_HLEN must be 16 for this interface driver!"
  74. #endif
  75. #if (ETH_PAD_SIZE != 0)
  76. #error "ETH_PAD_SIZE must be 0 for this interface driver!"
  77. #endif
  78. #if (!SYS_LIGHTWEIGHT_PROT)
  79. #error "SYS_LIGHTWEIGHT_PROT must be enabled for this interface driver!"
  80. #endif
  81. /**
  82. * Set the physical address of the PHY we will be using if this is not
  83. * specified in lwipopts.h. We assume 0 for the internal PHY.
  84. */
  85. #ifndef PHY_PHYS_ADDR
  86. #define PHY_PHYS_ADDR 0
  87. #endif
  88. #ifndef EMAC_PHY_CONFIG
  89. #define EMAC_PHY_CONFIG (EMAC_PHY_TYPE_INTERNAL | EMAC_PHY_INT_MDIX_EN | \
  90. EMAC_PHY_AN_100B_T_FULL_DUPLEX)
  91. #endif
  92. /**
  93. * If necessary, set the defaui32t number of transmit and receive DMA descriptors
  94. * used by the Ethernet MAC.
  95. *
  96. */
  97. #ifndef NUM_RX_DESCRIPTORS
  98. #define NUM_RX_DESCRIPTORS 4
  99. #endif
  100. #ifndef NUM_TX_DESCRIPTORS
  101. #define NUM_TX_DESCRIPTORS 8
  102. #endif
  103. /**
  104. * Setup processing for PTP (IEEE-1588).
  105. *
  106. */
  107. #if LWIP_PTPD
  108. extern uint32_t g_ui32SysClk;
  109. extern uint32_t g_ui32PTPTickRate;
  110. extern void lwIPHostGetTime(u32_t *time_s, u32_t *time_ns);
  111. #endif
  112. /**
  113. * Stellaris DriverLib Header Files required for this interface driver.
  114. *
  115. */
  116. #include <stdint.h>
  117. #include <stdbool.h>
  118. #include "inc/hw_emac.h"
  119. #include "inc/hw_ints.h"
  120. #include "inc/hw_memmap.h"
  121. #include "inc/hw_types.h"
  122. #include "driverlib/emac.h"
  123. #include "driverlib/interrupt.h"
  124. #include "driverlib/sysctl.h"
  125. #include "driverlib/flash.h"
  126. #include "driverlib/interrupt.h"
  127. #include "driverlib/pin_map.h"
  128. #include "driverlib/rom_map.h"
  129. #include "driverlib/gpio.h"
  130. #include <netif/ethernetif.h>
  131. #include "lwipopts.h"
  132. #include "drv_eth.h"
  133. /**
  134. * A structure used to keep track of driver state and error counts.
  135. */
  136. typedef struct {
  137. uint32_t ui32TXCount;
  138. uint32_t ui32TXCopyCount;
  139. uint32_t ui32TXCopyFailCount;
  140. uint32_t ui32TXNoDescCount;
  141. uint32_t ui32TXBufQueuedCount;
  142. uint32_t ui32TXBufFreedCount;
  143. uint32_t ui32RXBufReadCount;
  144. uint32_t ui32RXPacketReadCount;
  145. uint32_t ui32RXPacketErrCount;
  146. uint32_t ui32RXPacketCBErrCount;
  147. uint32_t ui32RXNoBufCount;
  148. }
  149. tDriverStats;
  150. tDriverStats g_sDriverStats;
  151. #ifdef DEBUG
  152. /**
  153. * Note: This rather weird construction where we invoke the macro with the
  154. * name of the field minus its Hungarian prefix is a workaround for a problem
  155. * experienced with GCC which does not like concatenating tokens after an
  156. * operator, specifically '.' or '->', in a macro.
  157. */
  158. #define DRIVER_STATS_INC(x) do{ g_sDriverStats.ui32##x++; } while(0)
  159. #define DRIVER_STATS_DEC(x) do{ g_sDriverStats.ui32##x--; } while(0)
  160. #define DRIVER_STATS_ADD(x, inc) do{ g_sDriverStats.ui32##x += (inc); } while(0)
  161. #define DRIVER_STATS_SUB(x, dec) do{ g_sDriverStats.ui32##x -= (dec); } while(0)
  162. #else
  163. #define DRIVER_STATS_INC(x)
  164. #define DRIVER_STATS_DEC(x)
  165. #define DRIVER_STATS_ADD(x, inc)
  166. #define DRIVER_STATS_SUB(x, dec)
  167. #endif
  168. /**
  169. * Helper struct holding a DMA descriptor and the pbuf it currently refers
  170. * to.
  171. */
  172. typedef struct {
  173. tEMACDMADescriptor Desc;
  174. struct pbuf *pBuf;
  175. } tDescriptor;
  176. typedef struct {
  177. tDescriptor *pDescriptors;
  178. uint32_t ui32NumDescs;
  179. uint32_t ui32Read;
  180. uint32_t ui32Write;
  181. } tDescriptorList;
  182. /**
  183. * Helper struct to hold private data used to operate your ethernet interface.
  184. * Keeping the ethernet address of the MAC in this struct is not necessary
  185. * as it is already kept in the struct netif.
  186. * But this is only an example, anyway...
  187. */
  188. typedef struct {
  189. struct eth_addr *ethaddr;
  190. /* Add whatever per-interface state that is needed here. */
  191. tDescriptorList *pTxDescList;
  192. tDescriptorList *pRxDescList;
  193. } tStellarisIF;
  194. /**
  195. * Global variable for this interface's private data. Needed to allow
  196. * the interrupt handlers access to this information outside of the
  197. * context of the lwIP netif.
  198. *
  199. */
  200. tDescriptor g_pTxDescriptors[NUM_TX_DESCRIPTORS];
  201. tDescriptor g_pRxDescriptors[NUM_RX_DESCRIPTORS];
  202. tDescriptorList g_TxDescList = {
  203. g_pTxDescriptors, NUM_TX_DESCRIPTORS, 0, 0
  204. };
  205. tDescriptorList g_RxDescList = {
  206. g_pRxDescriptors, NUM_RX_DESCRIPTORS, 0, 0
  207. };
  208. static tStellarisIF g_StellarisIFData = {
  209. 0, &g_TxDescList, &g_RxDescList
  210. };
  211. /**
  212. * Interrupt counters (for debug purposes).
  213. */
  214. volatile uint32_t g_ui32NormalInts;
  215. volatile uint32_t g_ui32AbnormalInts;
  216. /**
  217. * A macro which determines whether a pointer is within the SRAM address
  218. * space and, hence, points to a buffer that the Ethernet MAC can directly
  219. * DMA from.
  220. */
  221. #define PTR_SAFE_FOR_EMAC_DMA(ptr) (((uint32_t)(ptr) >= 0x2000000) && \
  222. ((uint32_t)(ptr) < 0x20070000))
  223. typedef struct
  224. {
  225. /* inherit from ethernet device */
  226. struct eth_device parent;
  227. tStellarisIF* dma_if;
  228. /* for rx_thread async get pbuf */
  229. rt_mailbox_t rx_pbuf_mb;
  230. } net_device;
  231. typedef net_device* net_device_t;
  232. static char rx_pbuf_mb_pool[8*4];
  233. static struct rt_mailbox eth_rx_pbuf_mb;
  234. static net_device eth_dev_entry;
  235. static net_device_t eth_dev = &eth_dev_entry;
  236. /**
  237. * Initialize the transmit and receive DMA descriptor lists.
  238. */
  239. void
  240. InitDMADescriptors(void)
  241. {
  242. uint32_t ui32Loop;
  243. /* Transmit list - mark all descriptors as not owned by the hardware */
  244. for(ui32Loop = 0; ui32Loop < NUM_TX_DESCRIPTORS; ui32Loop++)
  245. {
  246. g_pTxDescriptors[ui32Loop].pBuf = (struct pbuf *)0;
  247. g_pTxDescriptors[ui32Loop].Desc.ui32Count = 0;
  248. g_pTxDescriptors[ui32Loop].Desc.pvBuffer1 = 0;
  249. g_pTxDescriptors[ui32Loop].Desc.DES3.pLink =
  250. ((ui32Loop == (NUM_TX_DESCRIPTORS - 1)) ?
  251. &g_pTxDescriptors[0].Desc : &g_pTxDescriptors[ui32Loop + 1].Desc);
  252. g_pTxDescriptors[ui32Loop].Desc.ui32CtrlStatus = DES0_TX_CTRL_INTERRUPT |
  253. DES0_TX_CTRL_CHAINED | DES0_TX_CTRL_IP_ALL_CKHSUMS;
  254. }
  255. g_TxDescList.ui32Read = 0;
  256. g_TxDescList.ui32Write = 0;
  257. /* Receive list - tag each descriptor with a pbuf and set all fields to
  258. * allow packets to be received.
  259. */
  260. for(ui32Loop = 0; ui32Loop < NUM_RX_DESCRIPTORS; ui32Loop++)
  261. {
  262. g_pRxDescriptors[ui32Loop].pBuf = pbuf_alloc(PBUF_RAW, PBUF_POOL_BUFSIZE,
  263. PBUF_POOL);
  264. g_pRxDescriptors[ui32Loop].Desc.ui32Count = DES1_RX_CTRL_CHAINED;
  265. if(g_pRxDescriptors[ui32Loop].pBuf)
  266. {
  267. /* Set the DMA to write directly into the pbuf payload. */
  268. g_pRxDescriptors[ui32Loop].Desc.pvBuffer1 =
  269. g_pRxDescriptors[ui32Loop].pBuf->payload;
  270. g_pRxDescriptors[ui32Loop].Desc.ui32Count |=
  271. (g_pRxDescriptors[ui32Loop].pBuf->len << DES1_RX_CTRL_BUFF1_SIZE_S);
  272. g_pRxDescriptors[ui32Loop].Desc.ui32CtrlStatus = DES0_RX_CTRL_OWN;
  273. }
  274. else
  275. {
  276. LWIP_DEBUGF(NETIF_DEBUG, ("tivaif_init: pbuf_alloc error\n"));
  277. /* No pbuf available so leave the buffer pointer empty. */
  278. g_pRxDescriptors[ui32Loop].Desc.pvBuffer1 = 0;
  279. g_pRxDescriptors[ui32Loop].Desc.ui32CtrlStatus = 0;
  280. }
  281. g_pRxDescriptors[ui32Loop].Desc.DES3.pLink =
  282. ((ui32Loop == (NUM_RX_DESCRIPTORS - 1)) ?
  283. &g_pRxDescriptors[0].Desc : &g_pRxDescriptors[ui32Loop + 1].Desc);
  284. }
  285. g_TxDescList.ui32Read = 0;
  286. g_TxDescList.ui32Write = 0;
  287. //
  288. // Set the descriptor pointers in the hardware.
  289. //
  290. EMACRxDMADescriptorListSet(EMAC0_BASE, &g_pRxDescriptors[0].Desc);
  291. EMACTxDMADescriptorListSet(EMAC0_BASE, &g_pTxDescriptors[0].Desc);
  292. }
  293. /**
  294. * In this function, the hardware should be initialized.
  295. * Called from tivaif_init().
  296. *
  297. * @param netif the already initialized lwip network interface structure
  298. * for this ethernetif
  299. */
  300. static void
  301. tivaif_hwinit(struct netif *psNetif)
  302. {
  303. uint16_t ui16Val;
  304. /* Initialize the DMA descriptors. */
  305. InitDMADescriptors();
  306. /* Clear any stray PHY interrupts that may be set. */
  307. ui16Val = EMACPHYRead(EMAC0_BASE, PHY_PHYS_ADDR, EPHY_MISR1);
  308. ui16Val = EMACPHYRead(EMAC0_BASE, PHY_PHYS_ADDR, EPHY_MISR2);
  309. /* Configure and enable the link status change interrupt in the PHY. */
  310. ui16Val = EMACPHYRead(EMAC0_BASE, PHY_PHYS_ADDR, EPHY_SCR);
  311. ui16Val |= (EPHY_SCR_INTEN_EXT | EPHY_SCR_INTOE_EXT);
  312. EMACPHYWrite(EMAC0_BASE, PHY_PHYS_ADDR, EPHY_SCR, ui16Val);
  313. EMACPHYWrite(EMAC0_BASE, PHY_PHYS_ADDR, EPHY_MISR1, (EPHY_MISR1_LINKSTATEN |
  314. EPHY_MISR1_SPEEDEN | EPHY_MISR1_DUPLEXMEN | EPHY_MISR1_ANCEN));
  315. /* Read the PHY interrupt status to clear any stray events. */
  316. ui16Val = EMACPHYRead(EMAC0_BASE, PHY_PHYS_ADDR, EPHY_MISR1);
  317. /**
  318. * Set MAC filtering options. We receive all broadcast and mui32ticast
  319. * packets along with those addressed specifically for us.
  320. */
  321. EMACFrameFilterSet(EMAC0_BASE, (EMAC_FRMFILTER_HASH_AND_PERFECT |
  322. EMAC_FRMFILTER_PASS_MULTICAST));
  323. #if LWIP_PTPD
  324. //
  325. // Enable timestamping on all received packets.
  326. //
  327. // We set the fine clock adjustment mode and configure the subsecond
  328. // increment to half the 25MHz PTPD clock. This will give us maximum control
  329. // over the clock rate adjustment and keep the arithmetic easy later. It
  330. // should be possible to synchronize with higher accuracy than this with
  331. // appropriate juggling of the subsecond increment count and the addend
  332. // register value, though.
  333. //
  334. EMACTimestampConfigSet(EMAC0_BASE, (EMAC_TS_ALL_RX_FRAMES |
  335. EMAC_TS_DIGITAL_ROLLOVER |
  336. EMAC_TS_PROCESS_IPV4_UDP | EMAC_TS_ALL |
  337. EMAC_TS_PTP_VERSION_1 | EMAC_TS_UPDATE_FINE),
  338. (1000000000 / (25000000 / 2)));
  339. EMACTimestampAddendSet(EMAC0_BASE, 0x80000000);
  340. EMACTimestampEnable(EMAC0_BASE);
  341. #endif
  342. /* Clear any pending MAC interrupts. */
  343. EMACIntClear(EMAC0_BASE, EMACIntStatus(EMAC0_BASE, false));
  344. /* Enable the Ethernet MAC transmitter and receiver. */
  345. EMACTxEnable(EMAC0_BASE);
  346. EMACRxEnable(EMAC0_BASE);
  347. /* Enable the Ethernet RX and TX interrupt source. */
  348. EMACIntEnable(EMAC0_BASE, (EMAC_INT_RECEIVE | EMAC_INT_TRANSMIT |
  349. EMAC_INT_TX_STOPPED | EMAC_INT_RX_NO_BUFFER |
  350. EMAC_INT_RX_STOPPED | EMAC_INT_PHY));
  351. /* Enable the Ethernet interrupt. */
  352. IntEnable(INT_EMAC0);
  353. /* Enable all processor interrupts. */
  354. IntMasterEnable();
  355. /* Tell the PHY to start an auto-negotiation cycle. */
  356. EMACPHYWrite(EMAC0_BASE, PHY_PHYS_ADDR, EPHY_BMCR, (EPHY_BMCR_ANEN |
  357. EPHY_BMCR_RESTARTAN));
  358. }
  359. #ifdef DEBUG
  360. /**
  361. * Dump the chain of pbuf pointers to the debug output.
  362. */
  363. void
  364. tivaif_trace_pbuf(const char *pcTitle, struct pbuf *p)
  365. {
  366. LWIP_DEBUGF(NETIF_DEBUG, ("%s %08x (%d, %d)", pcTitle, p, p->tot_len,
  367. p->len));
  368. do
  369. {
  370. p = p->next;
  371. if(p)
  372. {
  373. LWIP_DEBUGF(NETIF_DEBUG, ("->%08x(%d)", p, p->len));
  374. }
  375. else
  376. {
  377. LWIP_DEBUGF(NETIF_DEBUG, ("->%08x", p));
  378. }
  379. } while((p != NULL) && (p->tot_len != p->len));
  380. LWIP_DEBUGF(NETIF_DEBUG, ("\n"));
  381. }
  382. #endif
  383. /**
  384. * This function is used to check whether a passed pbuf contains only buffers
  385. * resident in regions of memory that the Ethernet MAC can access. If any
  386. * buffers in the chain are outside a directly-DMAable section of memory,
  387. * the pbuf is copied to SRAM and a different pointer returned. If all
  388. * buffers are safe, the pbuf reference count is incremented and the original
  389. * pointer returned.
  390. */
  391. static struct pbuf *
  392. tivaif_check_pbuf(struct pbuf *p)
  393. {
  394. struct pbuf *pBuf;
  395. rt_err_t Err;
  396. pBuf = p;
  397. #ifdef DEBUG
  398. tivaif_trace_pbuf("Original:", p);
  399. #endif
  400. /* Walk the list of buffers in the pbuf checking each. */
  401. do
  402. {
  403. /* Does this pbuf's payload reside in memory that the Ethernet DMA
  404. * can access?
  405. */
  406. if(!PTR_SAFE_FOR_EMAC_DMA(pBuf->payload))
  407. {
  408. /* This buffer is outside the DMA-able memory space so we need
  409. * to copy the pbuf.
  410. */
  411. pBuf = pbuf_alloc(PBUF_RAW, p->tot_len, PBUF_POOL);
  412. /* If we got a new pbuf... */
  413. if(pBuf)
  414. {
  415. /* ...copy the old pbuf into the new one. */
  416. Err = pbuf_copy(pBuf, p);
  417. /* If we failed to copy the pbuf, free the newly allocated one
  418. * and make sure we return a NULL to show a problem.
  419. */
  420. if(Err != RT_EOK)
  421. {
  422. DRIVER_STATS_INC(TXCopyFailCount);
  423. pbuf_free(pBuf);
  424. pBuf = NULL;
  425. }
  426. else
  427. {
  428. #ifdef DEBUG
  429. tivaif_trace_pbuf("Copied:", pBuf);
  430. #endif
  431. DRIVER_STATS_INC(TXCopyCount);
  432. /* Reduce the reference count on the original pbuf since
  433. * we're not going to hold on to it after returning from
  434. * tivaif_transmit. Note that we already bumped
  435. * the reference count at the top of tivaif_transmit.
  436. */
  437. pbuf_free(p);
  438. }
  439. }
  440. /* Send back the new pbuf pointer or NULL if an error occurred. */
  441. return(pBuf);
  442. }
  443. /* Move on to the next buffer in the queue */
  444. pBuf = pBuf->next;
  445. }
  446. while(pBuf);
  447. /**
  448. * If we get here, the passed pbuf can be safely used without needing to
  449. * be copied.
  450. */
  451. return(p);
  452. }
  453. /**
  454. * This function should do the actual transmission of the packet. The packet is
  455. * contained in the pbuf that is passed to the function. This pbuf might be
  456. * chained.
  457. *
  458. * @param psNetif the lwip network interface structure for this ethernetif
  459. * @param p the MAC packet to send (e.g. IP packet including MAC addresses and type)
  460. * @return RT_EOK if the packet coui32d be sent
  461. * an err_t value if the packet coui32dn't be sent
  462. */
  463. static rt_err_t
  464. tivaif_transmit(net_device_t dev, struct pbuf *p)
  465. {
  466. tStellarisIF *pIF;
  467. tDescriptor *pDesc;
  468. struct pbuf *pBuf;
  469. uint32_t ui32NumChained, ui32NumDescs;
  470. bool bFirst;
  471. SYS_ARCH_DECL_PROTECT(lev);
  472. LWIP_DEBUGF(NETIF_DEBUG, ("tivaif_transmit 0x%08x, len %d\n", p,
  473. p->tot_len));
  474. /**
  475. * This entire function must run within a "critical section" to preserve
  476. * the integrity of the transmit pbuf queue.
  477. */
  478. SYS_ARCH_PROTECT(lev);
  479. /* Update our transmit attempt counter. */
  480. DRIVER_STATS_INC(TXCount);
  481. /**
  482. * Increase the reference count on the packet provided so that we can
  483. * hold on to it until we are finished transmitting its content.
  484. */
  485. pbuf_ref(p);
  486. /**
  487. * Determine whether all buffers passed are within SRAM and, if not, copy
  488. * the pbuf into SRAM-resident buffers so that the Ethernet DMA can access
  489. * the data.
  490. */
  491. p = tivaif_check_pbuf(p);
  492. /* Make sure we still have a valid buffer (it may have been copied) */
  493. if(!p)
  494. {
  495. LINK_STATS_INC(link.memerr);
  496. SYS_ARCH_UNPROTECT(lev);
  497. return(-RT_ENOMEM);
  498. }
  499. /* Get our state data from the netif structure we were passed. */
  500. //pIF = (tStellarisIF *)psNetif->state;
  501. pIF = dev->dma_if;
  502. /* Make sure that the transmit descriptors are not all in use */
  503. pDesc = &(pIF->pTxDescList->pDescriptors[pIF->pTxDescList->ui32Write]);
  504. if(pDesc->pBuf)
  505. {
  506. /**
  507. * The current write descriptor has a pbuf attached to it so this
  508. * implies that the ring is fui32l. Reject this transmit request with a
  509. * memory error since we can't satisfy it just now.
  510. */
  511. pbuf_free(p);
  512. LINK_STATS_INC(link.memerr);
  513. DRIVER_STATS_INC(TXNoDescCount);
  514. SYS_ARCH_UNPROTECT(lev);
  515. return (-RT_ENOMEM);
  516. }
  517. /* How many pbufs are in the chain passed? */
  518. ui32NumChained = (uint32_t)pbuf_clen(p);
  519. /* How many free transmit descriptors do we have? */
  520. ui32NumDescs = (pIF->pTxDescList->ui32Read > pIF->pTxDescList->ui32Write) ?
  521. (pIF->pTxDescList->ui32Read - pIF->pTxDescList->ui32Write) :
  522. ((NUM_TX_DESCRIPTORS - pIF->pTxDescList->ui32Write) +
  523. pIF->pTxDescList->ui32Read);
  524. /* Do we have enough free descriptors to send the whole packet? */
  525. if(ui32NumDescs < ui32NumChained)
  526. {
  527. /* No - we can't transmit this whole packet so return an error. */
  528. pbuf_free(p);
  529. LINK_STATS_INC(link.memerr);
  530. DRIVER_STATS_INC(TXNoDescCount);
  531. SYS_ARCH_UNPROTECT(lev);
  532. return (-RT_ENOMEM);
  533. }
  534. /* Tag the first descriptor as the start of the packet. */
  535. bFirst = true;
  536. pDesc->Desc.ui32CtrlStatus = DES0_TX_CTRL_FIRST_SEG;
  537. /* Here, we know we can send the packet so write it to the descriptors */
  538. pBuf = p;
  539. while(ui32NumChained)
  540. {
  541. /* Get a pointer to the descriptor we will write next. */
  542. pDesc = &(pIF->pTxDescList->pDescriptors[pIF->pTxDescList->ui32Write]);
  543. /* Fill in the buffer pointer and length */
  544. pDesc->Desc.ui32Count = (uint32_t)pBuf->len;
  545. pDesc->Desc.pvBuffer1 = pBuf->payload;
  546. /* Tag the first descriptor as the start of the packet. */
  547. if(bFirst)
  548. {
  549. bFirst = false;
  550. pDesc->Desc.ui32CtrlStatus = DES0_TX_CTRL_FIRST_SEG;
  551. }
  552. else
  553. {
  554. pDesc->Desc.ui32CtrlStatus = 0;
  555. }
  556. pDesc->Desc.ui32CtrlStatus |= (DES0_TX_CTRL_IP_ALL_CKHSUMS |
  557. DES0_TX_CTRL_CHAINED);
  558. /* Decrement our descriptor counter, move on to the next buffer in the
  559. * pbuf chain. */
  560. ui32NumChained--;
  561. pBuf = pBuf->next;
  562. /* Update the descriptor list write index. */
  563. pIF->pTxDescList->ui32Write++;
  564. if(pIF->pTxDescList->ui32Write == NUM_TX_DESCRIPTORS)
  565. {
  566. pIF->pTxDescList->ui32Write = 0;
  567. }
  568. /* If this is the last descriptor, mark it as the end of the packet. */
  569. if(!ui32NumChained)
  570. {
  571. pDesc->Desc.ui32CtrlStatus |= (DES0_TX_CTRL_LAST_SEG |
  572. DES0_TX_CTRL_INTERRUPT);
  573. /* Tag the descriptor with the original pbuf pointer. */
  574. pDesc->pBuf = p;
  575. }
  576. else
  577. {
  578. /* Set the lsb of the pbuf pointer. We use this as a signal that
  579. * we should not free the pbuf when we are walking the descriptor
  580. * list while processing the transmit interrupt. We only free the
  581. * pbuf when processing the last descriptor used to transmit its
  582. * chain.
  583. */
  584. pDesc->pBuf = (struct pbuf *)((uint32_t)p + 1);
  585. }
  586. DRIVER_STATS_INC(TXBufQueuedCount);
  587. /* Hand the descriptor over to the hardware. */
  588. pDesc->Desc.ui32CtrlStatus |= DES0_TX_CTRL_OWN;
  589. }
  590. /* Tell the transmitter to start (in case it had stopped). */
  591. EMACTxDMAPollDemand(EMAC0_BASE);
  592. /* Update lwIP statistics */
  593. LINK_STATS_INC(link.xmit);
  594. SYS_ARCH_UNPROTECT(lev);
  595. return(RT_EOK);
  596. }
  597. /**
  598. * This function will process all transmit descriptors and free pbufs attached
  599. * to any that have been transmitted since we last checked.
  600. *
  601. * This function is called only from the Ethernet interrupt handler.
  602. *
  603. * @param netif the lwip network interface structure for this ethernetif
  604. * @return None.
  605. */
  606. static void
  607. tivaif_process_transmit(tStellarisIF *pIF)
  608. {
  609. tDescriptorList *pDescList;
  610. uint32_t ui32NumDescs;
  611. /* Get a pointer to the transmit descriptor list. */
  612. pDescList = pIF->pTxDescList;
  613. /* Walk the list until we have checked all descriptors or we reach the
  614. * write pointer or find a descriptor that the hardware is still working
  615. * on.
  616. */
  617. for(ui32NumDescs = 0; ui32NumDescs < pDescList->ui32NumDescs; ui32NumDescs++)
  618. {
  619. /* Has the buffer attached to this descriptor been transmitted? */
  620. if(pDescList->pDescriptors[pDescList->ui32Read].Desc.ui32CtrlStatus &
  621. DES0_TX_CTRL_OWN)
  622. {
  623. /* No - we're finished. */
  624. break;
  625. }
  626. /* Does this descriptor have a buffer attached to it? */
  627. if(pDescList->pDescriptors[pDescList->ui32Read].pBuf)
  628. {
  629. /* Yes - free it if it's not marked as an intermediate pbuf */
  630. if(!((uint32_t)(pDescList->pDescriptors[pDescList->ui32Read].pBuf) & 1))
  631. {
  632. pbuf_free(pDescList->pDescriptors[pDescList->ui32Read].pBuf);
  633. DRIVER_STATS_INC(TXBufFreedCount);
  634. }
  635. pDescList->pDescriptors[pDescList->ui32Read].pBuf = NULL;
  636. }
  637. else
  638. {
  639. /* If the descriptor has no buffer, we are finished. */
  640. break;
  641. }
  642. /* Move on to the next descriptor. */
  643. pDescList->ui32Read++;
  644. if(pDescList->ui32Read == pDescList->ui32NumDescs)
  645. {
  646. pDescList->ui32Read = 0;
  647. }
  648. }
  649. }
  650. /**
  651. * This function will process all receive descriptors that contain newly read
  652. * data and pass complete frames up the lwIP stack as they are found. The
  653. * timestamp of the packet will be placed into the pbuf structure if PTPD is
  654. * enabled.
  655. *
  656. * This function is called only from the Ethernet interrupt handler.
  657. *
  658. * @param psNetif the lwip network interface structure for this ethernetif
  659. * @return None.
  660. */
  661. static void
  662. tivaif_receive(net_device_t dev)
  663. {
  664. tDescriptorList *pDescList;
  665. tStellarisIF *pIF;
  666. struct pbuf *pBuf;
  667. uint32_t ui32DescEnd;
  668. /* Get a pointer to our state data */
  669. pIF = dev->dma_if;
  670. /* Get a pointer to the receive descriptor list. */
  671. pDescList = pIF->pRxDescList;
  672. /* Start with a NULL pbuf so that we don't try to link chain the first
  673. * time round.
  674. */
  675. pBuf = NULL;
  676. /* Determine where we start and end our walk of the descriptor list */
  677. ui32DescEnd = pDescList->ui32Read ? (pDescList->ui32Read - 1) : (pDescList->ui32NumDescs - 1);
  678. /* Step through the descriptors that are marked for CPU attention. */
  679. while(pDescList->ui32Read != ui32DescEnd)
  680. {
  681. /* Does the current descriptor have a buffer attached to it? */
  682. if(pDescList->pDescriptors[pDescList->ui32Read].pBuf)
  683. {
  684. /* Yes - determine if the host has filled it yet. */
  685. if(pDescList->pDescriptors[pDescList->ui32Read].Desc.ui32CtrlStatus &
  686. DES0_RX_CTRL_OWN)
  687. {
  688. /* The DMA engine still owns the descriptor so we are finished */
  689. break;
  690. }
  691. DRIVER_STATS_INC(RXBufReadCount);
  692. /* If this descriptor contains the end of the packet, fix up the
  693. * buffer size accordingly.
  694. */
  695. if(pDescList->pDescriptors[pDescList->ui32Read].Desc.ui32CtrlStatus &
  696. DES0_RX_STAT_LAST_DESC)
  697. {
  698. /* This is the last descriptor for the frame so fix up the
  699. * length. It is safe for us to modify the internal fields
  700. * directly here (rather than calling pbuf_realloc) since we
  701. * know each of these pbufs is never chained.
  702. */
  703. pDescList->pDescriptors[pDescList->ui32Read].pBuf->len =
  704. (pDescList->pDescriptors[pDescList->ui32Read].Desc.ui32CtrlStatus &
  705. DES0_RX_STAT_FRAME_LENGTH_M) >>
  706. DES0_RX_STAT_FRAME_LENGTH_S;
  707. pDescList->pDescriptors[pDescList->ui32Read].pBuf->tot_len =
  708. pDescList->pDescriptors[pDescList->ui32Read].pBuf->len;
  709. }
  710. if(pBuf)
  711. {
  712. /* Link this pbuf to the last one we looked at since this buffer
  713. * is a continuation of an existing frame (split across mui32tiple
  714. * pbufs). Note that we use pbuf_cat() here rather than
  715. * pbuf_chain() since we don't want to increase the reference
  716. * count of either pbuf - we only want to link them together.
  717. */
  718. pbuf_cat(pBuf, pDescList->pDescriptors[pDescList->ui32Read].pBuf);
  719. pDescList->pDescriptors[pDescList->ui32Read].pBuf = pBuf;
  720. }
  721. /* Remember the buffer associated with this descriptor. */
  722. pBuf = pDescList->pDescriptors[pDescList->ui32Read].pBuf;
  723. /* Is this the last descriptor for the current frame? */
  724. if(pDescList->pDescriptors[pDescList->ui32Read].Desc.ui32CtrlStatus &
  725. DES0_RX_STAT_LAST_DESC)
  726. {
  727. /* Yes - does the frame contain errors? */
  728. if(pDescList->pDescriptors[pDescList->ui32Read].Desc.ui32CtrlStatus &
  729. DES0_RX_STAT_ERR)
  730. {
  731. /* This is a bad frame so discard it and update the relevant
  732. * statistics.
  733. */
  734. LWIP_DEBUGF(NETIF_DEBUG, ("tivaif_receive: packet error\n"));
  735. pbuf_free(pBuf);
  736. LINK_STATS_INC(link.drop);
  737. DRIVER_STATS_INC(RXPacketErrCount);
  738. }
  739. else
  740. {
  741. /* This is a good frame so pass it up the stack. */
  742. LINK_STATS_INC(link.recv);
  743. DRIVER_STATS_INC(RXPacketReadCount);
  744. #if LWIP_PTPD
  745. /* Place the timestamp in the PBUF if PTPD is enabled */
  746. pBuf->time_s =
  747. pDescList->pDescriptors[pDescList->ui32Read].Desc.ui32IEEE1588TimeHi;
  748. pBuf->time_ns =
  749. pDescList->pDescriptors[pDescList->ui32Read].Desc.ui32IEEE1588TimeLo;
  750. #endif
  751. #if NO_SYS
  752. if(ethernet_input(pBuf, psNetif) != RT_EOK)
  753. {
  754. #else
  755. //if(tcpip_input(pBuf, psNetif) != RT_EOK)
  756. if((rt_mb_send(dev->rx_pbuf_mb, (rt_uint32_t)pBuf) != RT_EOK) ||
  757. (eth_device_ready(&(dev->parent)) != RT_EOK))
  758. {
  759. #endif
  760. /* drop the packet */
  761. LWIP_DEBUGF(NETIF_DEBUG, ("tivaif_input: input error\n"));
  762. pbuf_free(pBuf);
  763. /* Adjust the link statistics */
  764. LINK_STATS_INC(link.memerr);
  765. LINK_STATS_INC(link.drop);
  766. DRIVER_STATS_INC(RXPacketCBErrCount);
  767. }
  768. /* We're finished with this packet so make sure we don't try
  769. * to link the next buffer to it.
  770. */
  771. pBuf = NULL;
  772. }
  773. }
  774. }
  775. /* Allocate a new buffer for this descriptor */
  776. pDescList->pDescriptors[pDescList->ui32Read].pBuf = pbuf_alloc(PBUF_RAW,
  777. PBUF_POOL_BUFSIZE,
  778. PBUF_POOL);
  779. pDescList->pDescriptors[pDescList->ui32Read].Desc.ui32Count =
  780. DES1_RX_CTRL_CHAINED;
  781. if(pDescList->pDescriptors[pDescList->ui32Read].pBuf)
  782. {
  783. /* We got a buffer so fill in the payload pointer and size. */
  784. pDescList->pDescriptors[pDescList->ui32Read].Desc.pvBuffer1 =
  785. pDescList->pDescriptors[pDescList->ui32Read].pBuf->payload;
  786. pDescList->pDescriptors[pDescList->ui32Read].Desc.ui32Count |=
  787. (pDescList->pDescriptors[pDescList->ui32Read].pBuf->len <<
  788. DES1_RX_CTRL_BUFF1_SIZE_S);
  789. /* Give this descriptor back to the hardware */
  790. pDescList->pDescriptors[pDescList->ui32Read].Desc.ui32CtrlStatus =
  791. DES0_RX_CTRL_OWN;
  792. }
  793. else
  794. {
  795. LWIP_DEBUGF(NETIF_DEBUG, ("tivaif_receive: pbuf_alloc error\n"));
  796. pDescList->pDescriptors[pDescList->ui32Read].Desc.pvBuffer1 = 0;
  797. /* Update the stats to show we coui32dn't allocate a pbuf. */
  798. DRIVER_STATS_INC(RXNoBufCount);
  799. LINK_STATS_INC(link.memerr);
  800. /* Stop parsing here since we can't leave a broken descriptor in
  801. * the chain.
  802. */
  803. break;
  804. }
  805. /* Move on to the next descriptor in the chain, taking care to wrap. */
  806. pDescList->ui32Read++;
  807. if(pDescList->ui32Read == pDescList->ui32NumDescs)
  808. {
  809. pDescList->ui32Read = 0;
  810. }
  811. }
  812. }
  813. /**
  814. * Process interrupts from the PHY.
  815. *
  816. * should be called from the Stellaris Ethernet Interrupt Handler. This
  817. * function will read packets from the Stellaris Ethernet fifo and place them
  818. * into a pbuf queue. If the transmitter is idle and there is at least one packet
  819. * on the transmit queue, it will place it in the transmit fifo and start the
  820. * transmitter.
  821. *
  822. */
  823. void
  824. tivaif_process_phy_interrupt(net_device_t dev)
  825. {
  826. uint16_t ui16Val, ui16Status;
  827. uint32_t ui32Config, ui32Mode, ui32RxMaxFrameSize;
  828. /* Read the PHY interrupt status. This clears all interrupt sources.
  829. * Note that we are only enabling sources in EPHY_MISR1 so we don't
  830. * read EPHY_MISR2.
  831. */
  832. ui16Val = EMACPHYRead(EMAC0_BASE, PHY_PHYS_ADDR, EPHY_MISR1);
  833. /*
  834. * Dummy read PHY REG EPHY_BMSR, it will force update the EPHY_STS register
  835. */
  836. EMACPHYRead(EMAC0_BASE, PHY_PHYS_ADDR, EPHY_BMSR);
  837. /* Read the current PHY status. */
  838. ui16Status = EMACPHYRead(EMAC0_BASE, PHY_PHYS_ADDR, EPHY_STS);
  839. /* Has the link status changed? */
  840. if(ui16Val & EPHY_MISR1_LINKSTAT)
  841. {
  842. /* Is link up or down now? */
  843. if(ui16Status & EPHY_STS_LINK)
  844. {
  845. /* Tell lwIP the link is up. */
  846. #if NO_SYS
  847. netif_set_link_up(psNetif);
  848. #else
  849. //tcpip_callback((tcpip_callback_fn)netif_set_link_up, psNetif);
  850. eth_device_linkchange(&(dev->parent), RT_TRUE);
  851. #endif
  852. /* In this case we drop through since we may need to reconfigure
  853. * the MAC depending upon the speed and half/fui32l-duplex settings.
  854. */
  855. }
  856. else
  857. {
  858. /* Tell lwIP the link is down */
  859. #if NO_SYS
  860. netif_set_link_down(psNetif);
  861. #else
  862. //tcpip_callback((tcpip_callback_fn)netif_set_link_down, psNetif);
  863. eth_device_linkchange(&(dev->parent), RT_FALSE);
  864. #endif
  865. }
  866. }
  867. /* Has the speed or duplex status changed? */
  868. if(ui16Val & (EPHY_MISR1_SPEED | EPHY_MISR1_SPEED | EPHY_MISR1_ANC))
  869. {
  870. /* Get the current MAC configuration. */
  871. EMACConfigGet(EMAC0_BASE, &ui32Config, &ui32Mode,
  872. &ui32RxMaxFrameSize);
  873. /* What speed is the interface running at now?
  874. */
  875. if(ui16Status & EPHY_STS_SPEED)
  876. {
  877. /* 10Mbps is selected */
  878. ui32Config &= ~EMAC_CONFIG_100MBPS;
  879. }
  880. else
  881. {
  882. /* 100Mbps is selected */
  883. ui32Config |= EMAC_CONFIG_100MBPS;
  884. }
  885. /* Are we in fui32l- or half-duplex mode? */
  886. if(ui16Status & EPHY_STS_DUPLEX)
  887. {
  888. /* Fui32l duplex. */
  889. ui32Config |= EMAC_CONFIG_FULL_DUPLEX;
  890. }
  891. else
  892. {
  893. /* Half duplex. */
  894. ui32Config &= ~EMAC_CONFIG_FULL_DUPLEX;
  895. }
  896. /* Reconfigure the MAC */
  897. EMACConfigSet(EMAC0_BASE, ui32Config, ui32Mode, ui32RxMaxFrameSize);
  898. }
  899. }
  900. /**
  901. * Process tx and rx packets at the low-level interrupt.
  902. *
  903. * should be called from the Stellaris Ethernet Interrupt Handler. This
  904. * function will read packets from the Stellaris Ethernet fifo and place them
  905. * into a pbuf queue. If the transmitter is idle and there is at least one packet
  906. * on the transmit queue, it will place it in the transmit fifo and start the
  907. * transmitter.
  908. *
  909. */
  910. void
  911. tivaif_interrupt(net_device_t dev, uint32_t ui32Status)
  912. {
  913. /* Update our debug interrupt counters. */
  914. if(ui32Status & EMAC_INT_NORMAL_INT)
  915. {
  916. g_ui32NormalInts++;
  917. }
  918. if(ui32Status & EMAC_INT_ABNORMAL_INT)
  919. {
  920. g_ui32AbnormalInts++;
  921. }
  922. /* Is this an interrupt from the PHY? */
  923. if(ui32Status & EMAC_INT_PHY)
  924. {
  925. tivaif_process_phy_interrupt(dev);
  926. }
  927. /* Process the transmit DMA list, freeing any buffers that have been
  928. * transmitted since our last interrupt.
  929. */
  930. if(ui32Status & EMAC_INT_TRANSMIT)
  931. {
  932. tivaif_process_transmit(dev->dma_if);
  933. }
  934. /**
  935. * Process the receive DMA list and pass all successfui32ly received packets
  936. * up the stack. We also call this function in cases where the receiver has
  937. * stalled due to missing buffers since the receive function will attempt to
  938. * allocate new pbufs for descriptor entries which have none.
  939. */
  940. if(ui32Status & (EMAC_INT_RECEIVE | EMAC_INT_RX_NO_BUFFER |
  941. EMAC_INT_RX_STOPPED))
  942. {
  943. tivaif_receive(dev);
  944. }
  945. }
  946. #if NETIF_DEBUG
  947. /* Print an IP header by using LWIP_DEBUGF
  948. * @param p an IP packet, p->payload pointing to the IP header
  949. */
  950. void
  951. tivaif_debug_print(struct pbuf *p)
  952. {
  953. struct eth_hdr *ethhdr = (struct eth_hdr *)p->payload;
  954. u16_t *plen = (u16_t *)p->payload;
  955. LWIP_DEBUGF(NETIF_DEBUG, ("ETH header:\n"));
  956. LWIP_DEBUGF(NETIF_DEBUG, ("Packet Length:%5"U16_F" \n",*plen));
  957. LWIP_DEBUGF(NETIF_DEBUG, ("Destination: %02"X8_F"-%02"X8_F"-%02"X8_F"-%02"X8_F"-%02"X8_F"-%02"X8_F"\n",
  958. ethhdr->dest.addr[0],
  959. ethhdr->dest.addr[1],
  960. ethhdr->dest.addr[2],
  961. ethhdr->dest.addr[3],
  962. ethhdr->dest.addr[4],
  963. ethhdr->dest.addr[5]));
  964. LWIP_DEBUGF(NETIF_DEBUG, ("Source: %02"X8_F"-%02"X8_F"-%02"X8_F"-%02"X8_F"-%02"X8_F"-%02"X8_F"\n",
  965. ethhdr->src.addr[0],
  966. ethhdr->src.addr[1],
  967. ethhdr->src.addr[2],
  968. ethhdr->src.addr[3],
  969. ethhdr->src.addr[4],
  970. ethhdr->src.addr[5]));
  971. LWIP_DEBUGF(NETIF_DEBUG, ("Packet Type:0x%04"U16_F" \n", ethhdr->type));
  972. }
  973. #endif /* NETIF_DEBUG */
  974. void lwIPEthernetIntHandler(void)
  975. {
  976. uint32_t ui32Status;
  977. #ifdef DEF_INT_TEMPSTAMP
  978. uint32_t ui32TimerStatus;
  979. #endif
  980. //
  981. // Read and Clear the interrupt.
  982. //
  983. ui32Status = MAP_EMACIntStatus(EMAC0_BASE, true);
  984. //
  985. // If the interrupt really came from the Ethernet and not our
  986. // timer, clear it.
  987. //
  988. if(ui32Status)
  989. {
  990. MAP_EMACIntClear(EMAC0_BASE, ui32Status);
  991. }
  992. #ifdef DEF_INT_TEMPSTAMP
  993. //
  994. // Check to see whether a hardware timer interrupt has been reported.
  995. //
  996. if(ui32Status & EMAC_INT_TIMESTAMP)
  997. {
  998. //
  999. // Yes - read and clear the timestamp interrupt status.
  1000. //
  1001. ui32TimerStatus = EMACTimestampIntStatus(EMAC0_BASE);
  1002. //
  1003. // If a timer interrupt handler has been registered, call it.
  1004. //
  1005. if(g_pfnTimerHandler)
  1006. {
  1007. g_pfnTimerHandler(EMAC0_BASE, ui32TimerStatus);
  1008. }
  1009. }
  1010. #endif
  1011. //
  1012. // The handling of the interrupt is different based on the use of a RTOS.
  1013. //
  1014. //
  1015. // No RTOS is being used. If a transmit/receive interrupt was active,
  1016. // run the low-level interrupt handler.
  1017. //
  1018. if(ui32Status)
  1019. {
  1020. tivaif_interrupt(eth_dev, ui32Status);
  1021. }
  1022. //
  1023. // Service the lwIP timers.
  1024. //
  1025. //lwIPServiceTimers();
  1026. }
  1027. // OUI:00-12-37 (hex) Texas Instruments, only for test
  1028. static int tiva_eth_mac_addr_init(void)
  1029. {
  1030. int retVal =0;
  1031. uint32_t ulUser[2];
  1032. uint8_t mac_addr[6];
  1033. MAP_FlashUserGet(&ulUser[0], &ulUser[1]);
  1034. if((ulUser[0] == 0xffffffff) || (ulUser[1] == 0xffffffff))
  1035. {
  1036. rt_kprintf("Fail to get mac address from eeprom.\n");
  1037. rt_kprintf("Using default mac address\n");
  1038. // OUI:00-12-37 (hex) Texas Instruments, only for test
  1039. // Configure the hardware MAC address
  1040. ulUser[0] = 0x00371200;
  1041. ulUser[1] = 0x00563412;
  1042. //FlashUserSet(ulUser0, ulUser1);
  1043. retVal =-1;
  1044. }
  1045. //Convert the 24/24 split MAC address from NV ram into a 32/16 split MAC
  1046. //address needed to program the hardware registers, then program the MAC
  1047. //address into the Ethernet Controller registers.
  1048. mac_addr[0] = ((ulUser[0] >> 0) & 0xff);
  1049. mac_addr[1] = ((ulUser[0] >> 8) & 0xff);
  1050. mac_addr[2] = ((ulUser[0] >> 16) & 0xff);
  1051. mac_addr[3] = ((ulUser[1] >> 0) & 0xff);
  1052. mac_addr[4] = ((ulUser[1] >> 8) & 0xff);
  1053. mac_addr[5] = ((ulUser[1] >> 16) & 0xff);
  1054. //
  1055. // Program the hardware with its MAC address (for filtering).
  1056. //
  1057. MAP_EMACAddrSet(EMAC0_BASE, 0, mac_addr);
  1058. return retVal;
  1059. }
  1060. void tiva_eth_lowlevel_init(void)
  1061. {
  1062. MAP_SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOF);
  1063. //
  1064. // PF1/PK4/PK6 are used for Ethernet LEDs.
  1065. //
  1066. MAP_GPIOPinConfigure(GPIO_PF0_EN0LED0);
  1067. MAP_GPIOPinConfigure(GPIO_PF4_EN0LED1);
  1068. GPIOPinTypeEthernetLED(GPIO_PORTF_BASE, GPIO_PIN_0);
  1069. GPIOPinTypeEthernetLED(GPIO_PORTF_BASE, GPIO_PIN_4);
  1070. //
  1071. // Enable the ethernet peripheral.
  1072. //
  1073. MAP_SysCtlPeripheralEnable(SYSCTL_PERIPH_EMAC0);
  1074. MAP_SysCtlPeripheralReset(SYSCTL_PERIPH_EMAC0);
  1075. //
  1076. // Enable the internal PHY if it's present and we're being
  1077. // asked to use it.
  1078. //
  1079. if((EMAC_PHY_CONFIG & EMAC_PHY_TYPE_MASK) == EMAC_PHY_TYPE_INTERNAL)
  1080. {
  1081. //
  1082. // We've been asked to configure for use with the internal
  1083. // PHY. Is it present?
  1084. //
  1085. if(SysCtlPeripheralPresent(SYSCTL_PERIPH_EPHY0))
  1086. {
  1087. //
  1088. // Yes - enable and reset it.
  1089. //
  1090. MAP_SysCtlPeripheralEnable(SYSCTL_PERIPH_EPHY0);
  1091. MAP_SysCtlPeripheralReset(SYSCTL_PERIPH_EPHY0);
  1092. }
  1093. else
  1094. {
  1095. //
  1096. // Internal PHY is not present on this part so hang here.
  1097. //
  1098. rt_kprintf("Internal PHY is not present on this part.\n");
  1099. while(1)
  1100. {
  1101. }
  1102. }
  1103. }
  1104. //
  1105. // Wait for the MAC to come out of reset.
  1106. //
  1107. while(!MAP_SysCtlPeripheralReady(SYSCTL_PERIPH_EMAC0))
  1108. {
  1109. }
  1110. //
  1111. // Configure for use with whichever PHY the user requires.
  1112. //
  1113. MAP_EMACPHYConfigSet(EMAC0_BASE, EMAC_PHY_CONFIG);
  1114. //
  1115. // Initialize the MAC and set the DMA mode.
  1116. //
  1117. MAP_EMACInit(EMAC0_BASE, 120000000, //system clock = 120MHz
  1118. EMAC_BCONFIG_MIXED_BURST | EMAC_BCONFIG_PRIORITY_FIXED,
  1119. 4, 4, 0);
  1120. //
  1121. // Set MAC configuration options.
  1122. //
  1123. MAP_EMACConfigSet(EMAC0_BASE, (EMAC_CONFIG_FULL_DUPLEX |
  1124. EMAC_CONFIG_CHECKSUM_OFFLOAD |
  1125. EMAC_CONFIG_7BYTE_PREAMBLE |
  1126. EMAC_CONFIG_IF_GAP_96BITS |
  1127. EMAC_CONFIG_USE_MACADDR0 |
  1128. EMAC_CONFIG_SA_FROM_DESCRIPTOR |
  1129. EMAC_CONFIG_BO_LIMIT_1024),
  1130. (EMAC_MODE_RX_STORE_FORWARD |
  1131. EMAC_MODE_TX_STORE_FORWARD |
  1132. EMAC_MODE_TX_THRESHOLD_64_BYTES |
  1133. EMAC_MODE_RX_THRESHOLD_64_BYTES), 0);
  1134. EMACIntRegister(EMAC0_BASE, lwIPEthernetIntHandler);
  1135. }
  1136. static rt_err_t eth_dev_init(rt_device_t device)
  1137. {
  1138. net_device_t net_dev = (net_device_t)device;
  1139. struct netif *psNetif = (net_dev->parent.netif);
  1140. LWIP_ASSERT("psNetif != NULL", (psNetif != NULL));
  1141. #if LWIP_NETIF_HOSTNAME
  1142. /* Initialize interface hostname */
  1143. psNetif->hostname = "t4mc";
  1144. #endif /* LWIP_NETIF_HOSTNAME */
  1145. /*
  1146. * Initialize the snmp variables and counters inside the struct netif.
  1147. * The last argument should be replaced with your link speed, in units
  1148. * of bits per second.
  1149. */
  1150. //NETIF_INIT_SNMP(psNetif, snmp_ifType_ethernet_csmacd, 1000000);
  1151. net_dev->dma_if = &g_StellarisIFData;
  1152. /* Remember our MAC address. */
  1153. g_StellarisIFData.ethaddr = (struct eth_addr *)&(psNetif->hwaddr[0]);
  1154. /* Initialize the hardware */
  1155. tivaif_hwinit(psNetif);
  1156. return RT_EOK;
  1157. }
  1158. /* control the interface */
  1159. static rt_err_t eth_dev_control(rt_device_t dev, int cmd, void *args)
  1160. {
  1161. switch(cmd)
  1162. {
  1163. case NIOCTL_GADDR:
  1164. /* get mac address */
  1165. if(args)
  1166. MAP_EMACAddrGet(EMAC0_BASE, 0, (uint8_t*)args);
  1167. else
  1168. return -RT_ERROR;
  1169. break;
  1170. default :
  1171. break;
  1172. }
  1173. return RT_EOK;
  1174. }
  1175. /* Open the interface */
  1176. static rt_err_t eth_dev_open(rt_device_t dev, rt_uint16_t oflag)
  1177. {
  1178. return RT_EOK;
  1179. }
  1180. /* Close the interface */
  1181. static rt_err_t eth_dev_close(rt_device_t dev)
  1182. {
  1183. return RT_EOK;
  1184. }
  1185. /* Read */
  1186. static rt_size_t eth_dev_read(rt_device_t dev, rt_off_t pos, void* buffer, rt_size_t size)
  1187. {
  1188. rt_set_errno(-RT_ENOSYS);
  1189. return 0;
  1190. }
  1191. /* Write */
  1192. static rt_size_t eth_dev_write(rt_device_t dev, rt_off_t pos, const void* buffer, rt_size_t size)
  1193. {
  1194. rt_set_errno(-RT_ENOSYS);
  1195. return 0;
  1196. }
  1197. static rt_err_t eth_dev_tx(rt_device_t dev, struct pbuf *p)
  1198. {
  1199. return tivaif_transmit((net_device_t)dev, p);
  1200. }
  1201. static struct pbuf* eth_dev_rx(rt_device_t dev)
  1202. {
  1203. rt_err_t result;
  1204. rt_uint32_t temp =0;
  1205. net_device_t net_dev = (net_device_t)dev;
  1206. result = rt_mb_recv(net_dev->rx_pbuf_mb, &temp, RT_WAITING_NO);
  1207. return (result == RT_EOK)? (struct pbuf*)temp : RT_NULL;
  1208. }
  1209. int rt_hw_tiva_eth_init(void)
  1210. {
  1211. rt_err_t result;
  1212. /* Clock GPIO and etc */
  1213. tiva_eth_lowlevel_init();
  1214. tiva_eth_mac_addr_init();
  1215. /* init rt-thread device interface */
  1216. eth_dev->parent.parent.init = eth_dev_init;
  1217. eth_dev->parent.parent.open = eth_dev_open;
  1218. eth_dev->parent.parent.close = eth_dev_close;
  1219. eth_dev->parent.parent.read = eth_dev_read;
  1220. eth_dev->parent.parent.write = eth_dev_write;
  1221. eth_dev->parent.parent.control = eth_dev_control;
  1222. eth_dev->parent.eth_rx = eth_dev_rx;
  1223. eth_dev->parent.eth_tx = eth_dev_tx;
  1224. result = rt_mb_init(&eth_rx_pbuf_mb, "epbuf",
  1225. &rx_pbuf_mb_pool[0], sizeof(rx_pbuf_mb_pool)/4,
  1226. RT_IPC_FLAG_FIFO);
  1227. RT_ASSERT(result == RT_EOK);
  1228. eth_dev->rx_pbuf_mb = &eth_rx_pbuf_mb;
  1229. result = eth_device_init(&(eth_dev->parent), "e0");
  1230. return result;
  1231. }
  1232. // eth_device_init using malloc
  1233. // We use INIT_COMPONENT_EXPORT insted of INIT_BOARD_EXPORT
  1234. INIT_COMPONENT_EXPORT(rt_hw_tiva_eth_init);
  1235. #if 0
  1236. #ifdef RT_USING_FINSH
  1237. #include "finsh.h"
  1238. void PHY_Read(uint8_t addr)
  1239. {
  1240. uint16_t data = EMACPHYRead(EMAC0_BASE, PHY_PHYS_ADDR, addr);
  1241. rt_kprintf("R PHY_REG[0x%02X] = 0x%04X\n", addr, data);
  1242. }
  1243. FINSH_FUNCTION_EXPORT(PHY_Read, (add));
  1244. void PHY_Write(uint8_t addr , uint16_t data)
  1245. {
  1246. EMACPHYWrite(EMAC0_BASE, PHY_PHYS_ADDR, addr, data);
  1247. rt_kprintf("W PHY_REG[0x%02X] = 0x%04X\n", addr, data);
  1248. }
  1249. FINSH_FUNCTION_EXPORT(PHY_Write, (add, data));
  1250. void PHY_SetAdd(uint8_t addr0, uint8_t addr1, uint8_t addr2,
  1251. uint8_t addr3, uint8_t addr4, uint8_t addr5)
  1252. {
  1253. uint32_t ulUser[2];
  1254. ulUser[0] = (((addr2<<8)|addr1)<<8)|addr0;
  1255. ulUser[1] = (((addr5<<8)|addr4)<<8)|addr3;
  1256. MAP_FlashUserSet(ulUser[0], ulUser[1]);
  1257. MAP_FlashUserSave();
  1258. rt_kprintf("Save to EEPROM. please reboot.");
  1259. }
  1260. FINSH_FUNCTION_EXPORT(PHY_SetAdd, (add0-add5));
  1261. #endif //RT_USING_FINSH
  1262. #endif