emac.c 189 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690
  1. //*****************************************************************************
  2. //
  3. // emac.c - Driver for the Integrated Ethernet Controller on Snowflake-class
  4. // Tiva devices.
  5. //
  6. // Copyright (c) 2013-2014 Texas Instruments Incorporated. All rights reserved.
  7. // Software License Agreement
  8. //
  9. // Redistribution and use in source and binary forms, with or without
  10. // modification, are permitted provided that the following conditions
  11. // are met:
  12. //
  13. // Redistributions of source code must retain the above copyright
  14. // notice, this list of conditions and the following disclaimer.
  15. //
  16. // Redistributions in binary form must reproduce the above copyright
  17. // notice, this list of conditions and the following disclaimer in the
  18. // documentation and/or other materials provided with the
  19. // distribution.
  20. //
  21. // Neither the name of Texas Instruments Incorporated nor the names of
  22. // its contributors may be used to endorse or promote products derived
  23. // from this software without specific prior written permission.
  24. //
  25. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  26. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  27. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  28. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  29. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  30. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  31. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  32. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  33. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  34. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  35. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  36. //
  37. // This is part of revision 2.1.0.12573 of the Tiva Peripheral Driver Library.
  38. //
  39. //*****************************************************************************
  40. //*****************************************************************************
  41. //
  42. //! \addtogroup emac_api
  43. //! @{
  44. //
  45. //*****************************************************************************
  46. #include <stdbool.h>
  47. #include <stdint.h>
  48. #include "inc/hw_ints.h"
  49. #include "inc/hw_memmap.h"
  50. #include "inc/hw_types.h"
  51. #include "inc/hw_emac.h"
  52. #include "driverlib/debug.h"
  53. #include "driverlib/emac.h"
  54. #include "driverlib/sysctl.h"
  55. #include "driverlib/interrupt.h"
  56. #include "driverlib/sw_crc.h"
  57. //*****************************************************************************
  58. //
  59. // Combined defines used in parameter validity checks.
  60. //
  61. //*****************************************************************************
  62. //*****************************************************************************
  63. //
  64. // Combined valid configuration flags.
  65. //
  66. //*****************************************************************************
  67. #define VALID_CONFIG_FLAGS (EMAC_CONFIG_USE_MACADDR1 | \
  68. EMAC_CONFIG_SA_INSERT | \
  69. EMAC_CONFIG_SA_REPLACE | \
  70. EMAC_CONFIG_2K_PACKETS | \
  71. EMAC_CONFIG_STRIP_CRC | \
  72. EMAC_CONFIG_JABBER_DISABLE | \
  73. EMAC_CONFIG_JUMBO_ENABLE | \
  74. EMAC_CONFIG_IF_GAP_MASK | \
  75. EMAC_CONFIG_CS_DISABLE | \
  76. EMAC_CONFIG_100MBPS | \
  77. EMAC_CONFIG_RX_OWN_DISABLE | \
  78. EMAC_CONFIG_LOOPBACK | \
  79. EMAC_CONFIG_FULL_DUPLEX | \
  80. EMAC_CONFIG_CHECKSUM_OFFLOAD | \
  81. EMAC_CONFIG_RETRY_DISABLE | \
  82. EMAC_CONFIG_AUTO_CRC_STRIPPING | \
  83. EMAC_CONFIG_BO_MASK | \
  84. EMAC_CONFIG_DEFERRAL_CHK_ENABLE | \
  85. EMAC_CONFIG_PREAMBLE_MASK)
  86. //*****************************************************************************
  87. //
  88. // Combined valid frame filter flags.
  89. //
  90. //*****************************************************************************
  91. #define VALID_FRMFILTER_FLAGS (EMAC_FRMFILTER_RX_ALL | \
  92. EMAC_FRMFILTER_VLAN | \
  93. EMAC_FRMFILTER_HASH_AND_PERFECT | \
  94. EMAC_FRMFILTER_SADDR | \
  95. EMAC_FRMFILTER_INV_SADDR | \
  96. EMAC_FRMFILTER_PASS_NO_PAUSE | \
  97. EMAC_FRMFILTER_PASS_ALL_CTRL | \
  98. EMAC_FRMFILTER_PASS_ADDR_CTRL | \
  99. EMAC_FRMFILTER_BROADCAST | \
  100. EMAC_FRMFILTER_PASS_MULTICAST | \
  101. EMAC_FRMFILTER_INV_DADDR | \
  102. EMAC_FRMFILTER_HASH_MULTICAST | \
  103. EMAC_FRMFILTER_HASH_UNICAST | \
  104. EMAC_FRMFILTER_PROMISCUOUS)
  105. //*****************************************************************************
  106. //
  107. // Combined valid maskable interrupts.
  108. //
  109. //*****************************************************************************
  110. #define EMAC_MASKABLE_INTS (EMAC_INT_EARLY_RECEIVE | \
  111. EMAC_INT_BUS_ERROR | \
  112. EMAC_INT_EARLY_TRANSMIT | \
  113. EMAC_INT_RX_WATCHDOG | \
  114. EMAC_INT_RX_STOPPED | \
  115. EMAC_INT_RX_NO_BUFFER | \
  116. EMAC_INT_RECEIVE | \
  117. EMAC_INT_TX_UNDERFLOW | \
  118. EMAC_INT_RX_OVERFLOW | \
  119. EMAC_INT_TX_JABBER | \
  120. EMAC_INT_TX_NO_BUFFER | \
  121. EMAC_INT_TX_STOPPED | \
  122. EMAC_INT_TRANSMIT | \
  123. EMAC_INT_NORMAL_INT | \
  124. EMAC_INT_ABNORMAL_INT | \
  125. EMAC_INT_PHY)
  126. //*****************************************************************************
  127. //
  128. // Combined valid normal interrupts.
  129. //
  130. //*****************************************************************************
  131. #define EMAC_NORMAL_INTS (EMAC_INT_TRANSMIT | \
  132. EMAC_INT_RECEIVE | \
  133. EMAC_INT_EARLY_RECEIVE | \
  134. EMAC_INT_TX_NO_BUFFER)
  135. //*****************************************************************************
  136. //
  137. // Combined valid abnormal interrupts.
  138. //
  139. //*****************************************************************************
  140. #define EMAC_ABNORMAL_INTS (EMAC_INT_TX_STOPPED | \
  141. EMAC_INT_TX_JABBER | \
  142. EMAC_INT_RX_OVERFLOW | \
  143. EMAC_INT_TX_UNDERFLOW | \
  144. EMAC_INT_RX_NO_BUFFER | \
  145. EMAC_INT_RX_STOPPED | \
  146. EMAC_INT_RX_WATCHDOG | \
  147. EMAC_INT_EARLY_TRANSMIT | \
  148. EMAC_INT_BUS_ERROR)
  149. //*****************************************************************************
  150. //
  151. // Interrupt sources reported via the DMARIS register but which are not
  152. // masked (or enabled) via the DMAIM register.
  153. //
  154. //*****************************************************************************
  155. #define EMAC_NON_MASKED_INTS (EMAC_DMARIS_TT | \
  156. EMAC_DMARIS_PMT | \
  157. EMAC_DMARIS_MMC)
  158. //*****************************************************************************
  159. //
  160. // The number of MAC addresses the module can store for filtering purposes.
  161. //
  162. //*****************************************************************************
  163. #define NUM_MAC_ADDR 4
  164. //*****************************************************************************
  165. //
  166. // Macros aiding access to the MAC address registers.
  167. //
  168. //*****************************************************************************
  169. #define MAC_ADDR_OFFSET (EMAC_O_ADDR1L - EMAC_O_ADDR0L)
  170. #define EMAC_O_ADDRL(n) (EMAC_O_ADDR0L + (MAC_ADDR_OFFSET * (n)))
  171. #define EMAC_O_ADDRH(n) (EMAC_O_ADDR0H + (MAC_ADDR_OFFSET * (n)))
  172. //*****************************************************************************
  173. //
  174. // A structure used to help in choosing the correct clock divisor for the MII
  175. // based on the current system clock rate.
  176. //
  177. //*****************************************************************************
  178. static const struct
  179. {
  180. uint32_t ui32SysClockMax;
  181. uint32_t ui32Divisor;
  182. }
  183. g_pi16MIIClockDiv[] =
  184. {
  185. { 64000000, EMAC_MIIADDR_CR_35_60 },
  186. { 104000000, EMAC_MIIADDR_CR_60_100 },
  187. { 150000000, EMAC_MIIADDR_CR_100_150 }
  188. };
  189. //*****************************************************************************
  190. //
  191. // The number of clock divisors in the above table.
  192. //
  193. //*****************************************************************************
  194. #define NUM_CLOCK_DIVISORS (sizeof(g_pi16MIIClockDiv) / \
  195. sizeof(g_pi16MIIClockDiv[0]))
  196. //*****************************************************************************
  197. //
  198. //! Initializes the Ethernet MAC and sets bus-related DMA parameters.
  199. //!
  200. //! \param ui32Base is the base address of the Ethernet controller.
  201. //! \param ui32SysClk is the current system clock frequency in Hertz.
  202. //! \param ui32BusConfig defines the bus operating mode for the Ethernet MAC
  203. //! DMA controller.
  204. //! \param ui32RxBurst is the maximum receive burst size in words.
  205. //! \param ui32TxBurst is the maximum transmit burst size in words.
  206. //! \param ui32DescSkipSize is the number of 32-bit words to skip between
  207. //! two unchained DMA descriptors. Values in the range 0 to 31 are valid.
  208. //!
  209. //! This function sets bus-related parameters for the Ethernet MAC DMA
  210. //! engines. It must be called after EMACPHYConfigSet() and called again
  211. //! after any subsequent call to EMACPHYConfigSet().
  212. //!
  213. //! The \e ui32BusConfig parameter is the logical OR of various fields. The
  214. //! first sets the DMA channel priority weight:
  215. //!
  216. //! - \b EMAC_BCONFIG_DMA_PRIO_WEIGHT_1
  217. //! - \b EMAC_BCONFIG_DMA_PRIO_WEIGHT_2
  218. //! - \b EMAC_BCONFIG_DMA_PRIO_WEIGHT_3
  219. //! - \b EMAC_BCONFIG_DMA_PRIO_WEIGHT_4
  220. //!
  221. //! The second field sets the receive and transmit priorities used when
  222. //! arbitrating between the Rx and Tx DMA. The priorities are Rx:Tx unless
  223. //! \b EMAC_BCONFIG_TX_PRIORITY is also specified, in which case they become
  224. //! Tx:Rx. The priority provided here is ignored if
  225. //! \b EMAC_BCONFIG_PRIORITY_FIXED is specified.
  226. //!
  227. //! - \b EMAC_BCONFIG_PRIORITY_1_1
  228. //! - \b EMAC_BCONFIG_PRIORITY_2_1
  229. //! - \b EMAC_BCONFIG_PRIORITY_3_1
  230. //! - \b EMAC_BCONFIG_PRIORITY_4_1
  231. //!
  232. //! The following additional flags may also be defined:
  233. //!
  234. //! - \b EMAC_BCONFIG_TX_PRIORITY indicates that the transmit DMA should be
  235. //! higher priority in all arbitration for the system-side bus. If this is not
  236. //! defined, the receive DMA has higher priority.
  237. //! - \b EMAC_BCONFIG_ADDR_ALIGNED works in tandem with
  238. //! \b EMAC_BCONFIG_FIXED_BURST to control address alignment of AHB bursts.
  239. //! When both flags are specified, all bursts are aligned to the start address
  240. //! least significant bits. If \b EMAC_BCONFIG_FIXED_BURST is not specified,
  241. //! the first burst is unaligned but subsequent bursts are aligned to the
  242. //! address.
  243. //! - \b EMAC_BCONFIG_ALT_DESCRIPTORS indicates that the DMA engine should
  244. //! use the alternate descriptor format as defined in type
  245. //! \b tEMACDMADescriptor. If absent, the basic descriptor type is used.
  246. //! Alternate descriptors are required if using IEEE 1588-2008 advanced
  247. //! timestamping, VLAN or TCP/UDP/ICMP CRC insertion features. Note that,
  248. //! for clarity, emac.h does not contain type definitions for the basic
  249. //! descriptor type. Please see the part datasheet for information on basic
  250. //! descriptor structures.
  251. //! - \b EMAC_BCONFIG_PRIORITY_FIXED indicates that a fixed priority scheme
  252. //! should be employed when arbitrating between the transmit and receive DMA
  253. //! for system-side bus access. In this case, the receive channel always has
  254. //! priority unless \b EMAC_BCONFIG_TX_PRIORITY is set, in which case the
  255. //! transmit channel has priority. If \b EMAC_BCONFIG_PRIORITY_FIXED is not
  256. //! specified, a weighted round-robin arbitration scheme is used with the
  257. //! weighting defined using \b EMAC_BCONFIG_PRIORITY_1_1,
  258. //! \b EMAC_BCONFIG_PRIORITY_2_1, \b EMAC_BCONFIG_PRIORITY_3_1 or
  259. //! \b EMAC_BCONFIG_PRIORITY_4_1, and \b EMAC_BCONFIG_TX_PRIORITY.
  260. //! - \b EMAC_BCONFIG_FIXED_BURST indicates that fixed burst transfers should
  261. //! be used.
  262. //! - \b EMAC_BCONFIG_MIXED_BURST indicates that the DMA engine should use
  263. //! mixed burst types depending on the length of data to be transferred
  264. //! across the system bus.
  265. //!
  266. //! The \e ui32RxBurst and \e ui32TxBurst parameters indicate the maximum
  267. //! number of words that the relevant DMA should transfer in a single
  268. //! transaction. Valid values are 1, 2, 4, 8, 16 and 32. Any other value
  269. //! results in undefined behavior.
  270. //!
  271. //! The \e ui32DescSkipSize parameter is used when the descriptor lists are
  272. //! using ring mode (where descriptors are contiguous in memory with the last
  273. //! descriptor marked with the \b END_OF_RING flag) rather than chained mode
  274. //! (where each descriptor includes a field that points to the next descriptor
  275. //! in the list). In ring mode, the hardware uses the \e ui32DescSkipSize to
  276. //! skip past any application-defined fields after the end of the hardware-
  277. //! defined descriptor fields. The parameter value indicates the number of
  278. //! 32-bit words to skip after the last field of the hardware-defined
  279. //! descriptor to get to the first field of the next descriptor. When using
  280. //! arrays of either the \b tEMACDMADescriptor or \b tEMACAltDMADescriptor
  281. //! types defined for this driver, \e ui32DescSkipSize must be set to 1 to skip
  282. //! the \e pvNext pointer added to the end of each of these structures.
  283. //! Applications may modify these structure definitions to include their own
  284. //! application-specific data and modify \e ui32DescSkipSize appropriately if
  285. //! desired.
  286. //!
  287. //! \return None.
  288. //
  289. //*****************************************************************************
  290. void
  291. EMACInit(uint32_t ui32Base, uint32_t ui32SysClk, uint32_t ui32BusConfig,
  292. uint32_t ui32RxBurst, uint32_t ui32TxBurst, uint32_t ui32DescSkipSize)
  293. {
  294. uint32_t ui32Val, ui32Div;
  295. //
  296. // Parameter sanity checks.
  297. //
  298. ASSERT(ui32DescSkipSize < 32);
  299. ASSERT(ui32TxBurst < (32 * 8));
  300. ASSERT(ui32RxBurst < (32 * 8));
  301. //
  302. // Make sure that the DMA software reset is clear before continuing.
  303. //
  304. while(HWREG(EMAC0_BASE + EMAC_O_DMABUSMOD) & EMAC_DMABUSMOD_SWR)
  305. {
  306. }
  307. //
  308. // Set common flags. Note that this driver assumes we are always using
  309. // 8 word descriptors so we need to OR in EMAC_DMABUSMOD_ATDS here.
  310. //
  311. ui32Val = (ui32BusConfig | (ui32DescSkipSize << EMAC_DMABUSMOD_DSL_S) |
  312. EMAC_DMABUSMOD_ATDS);
  313. //
  314. // Do we need to use the 8X burst length multiplier?
  315. //
  316. if((ui32TxBurst > 32) || (ui32RxBurst > 32))
  317. {
  318. //
  319. // Divide both burst lengths by 8 and set the 8X burst length
  320. // multiplier.
  321. //
  322. ui32Val |= EMAC_DMABUSMOD_8XPBL;
  323. ui32TxBurst >>= 3;
  324. ui32RxBurst >>= 3;
  325. //
  326. // Sanity check - neither burst length should have become zero. If
  327. // they did, this indicates that the values passed are invalid.
  328. //
  329. ASSERT(ui32RxBurst);
  330. ASSERT(ui32TxBurst);
  331. }
  332. //
  333. // Are the receive and transmit burst lengths the same?
  334. //
  335. if(ui32RxBurst == ui32TxBurst)
  336. {
  337. //
  338. // Yes - set up to use a single burst length.
  339. //
  340. ui32Val |= (ui32TxBurst << EMAC_DMABUSMOD_PBL_S);
  341. }
  342. else
  343. {
  344. //
  345. // No - we need to use separate burst lengths for each.
  346. //
  347. ui32Val |= (EMAC_DMABUSMOD_USP |
  348. (ui32TxBurst << EMAC_DMABUSMOD_PBL_S) |
  349. (ui32RxBurst << EMAC_DMABUSMOD_RPBL_S));
  350. }
  351. //
  352. // Finally, write the bus mode register.
  353. //
  354. HWREG(ui32Base + EMAC_O_DMABUSMOD) = ui32Val;
  355. //
  356. // Default the MII CSR clock divider based on the fastest system clock.
  357. //
  358. ui32Div = g_pi16MIIClockDiv[NUM_CLOCK_DIVISORS - 1].ui32Divisor;
  359. //
  360. // Find the MII CSR clock divider to use based on the current system clock.
  361. //
  362. for(ui32Val = 0; ui32Val < NUM_CLOCK_DIVISORS; ui32Val++)
  363. {
  364. if(ui32SysClk <= g_pi16MIIClockDiv[ui32Val].ui32SysClockMax)
  365. {
  366. ui32Div = g_pi16MIIClockDiv[ui32Val].ui32Divisor;
  367. break;
  368. }
  369. }
  370. //
  371. // Set the MII CSR clock speed.
  372. //
  373. HWREG(ui32Base + EMAC_O_MIIADDR) = ((HWREG(ui32Base + EMAC_O_MIIADDR) &
  374. ~EMAC_MIIADDR_CR_M) | ui32Div);
  375. //
  376. // Disable all the MMC interrupts as these are enabled by default at reset.
  377. //
  378. HWREG(ui32Base + EMAC_O_MMCRXIM) = 0xFFFFFFFF;
  379. HWREG(ui32Base + EMAC_O_MMCTXIM) = 0xFFFFFFFF;
  380. }
  381. //*****************************************************************************
  382. //
  383. //! Resets the Ethernet MAC.
  384. //!
  385. //! \param ui32Base is the base address of the Ethernet controller.
  386. //!
  387. //! This function performs a reset of the Ethernet MAC by resetting all logic
  388. //! and returning all registers to their default values. The function returns
  389. //! only after the hardware indicates that the reset has completed.
  390. //!
  391. //! \note To ensure that the reset completes, the selected PHY clock must be
  392. //! enabled when this function is called. If the PHY clock is absent, this
  393. //! function does not return.
  394. //!
  395. //! \return None.
  396. //
  397. //*****************************************************************************
  398. void
  399. EMACReset(uint32_t ui32Base)
  400. {
  401. //
  402. // Reset the Ethernet MAC.
  403. //
  404. HWREG(ui32Base + EMAC_O_DMABUSMOD) |= EMAC_DMABUSMOD_SWR;
  405. //
  406. // Wait for the reset to complete.
  407. //
  408. while(HWREG(ui32Base + EMAC_O_DMABUSMOD) & EMAC_DMABUSMOD_SWR)
  409. {
  410. }
  411. }
  412. //*****************************************************************************
  413. //
  414. //! Selects the Ethernet PHY in use.
  415. //!
  416. //! \param ui32Base is the base address of the Ethernet controller.
  417. //! \param ui32Config selects the PHY in use and, when using the internal
  418. //! PHY, allows various various PHY parameters to be configured.
  419. //!
  420. //! This function must be called prior to EMACInit() and EMACConfigSet() to
  421. //! select the Ethernet PHY to be used. If the internal PHY is selected, the
  422. //! function also allows configuration of various PHY parameters. Note that
  423. //! the Ethernet MAC is reset during this function call because parameters used
  424. //! by this function are latched by the hardware only on a MAC reset. The call
  425. //! sequence to select and configure the PHY, therefore, must be as follows:
  426. //!
  427. //! \verbatim
  428. //! // Enable and reset the MAC.
  429. //! SysCtlPeripheralEnable(SYSCTL_PERIPH_EMAC0);
  430. //! SysCtlPeripheralReset(SYSCTL_PERIPH_EMAC0);
  431. //! if(<using internal PHY>)
  432. //! {
  433. //! // Enable and reset the internal PHY.
  434. //! SysCtlPeripheralEnable(SYSCTL_PERIPH_EPHY0);
  435. //! SysCtlPeripheralReset(SYSCTL_PERIPH_EPHY0);
  436. //! }
  437. //!
  438. //! // Ensure the MAC is completed its reset.
  439. //! while(!MAP_SysCtlPeripheralReady(SYSCTL_PERIPH_EMAC0))
  440. //! {
  441. //! }
  442. //!
  443. //! // Set the PHY type and configuration options.
  444. //! EMACPHYConfigSet(EMAC0_BASE, <config>);
  445. //!
  446. //! // Initialize and configure the MAC.
  447. //! EMACInit(EMAC0_BASE, <system clock rate>, <bus config>,
  448. //! <Rx burst size>, <Tx burst size>, <desc skip>);
  449. //! EMACConfigSet(EMAC0_BASE, <parameters>);
  450. //! \endverbatim
  451. //!
  452. //! The \e ui32Config parameter must specify one of the following values:
  453. //!
  454. //! - \b EMAC_PHY_TYPE_INTERNAL selects the internal Ethernet PHY.
  455. //! - \b EMAC_PHY_TYPE_EXTERNAL_MII selects an external PHY connected via the
  456. //! MII interface.
  457. //! - \b EMAC_PHY_TYPE_EXTERNAL_RMII selects an external PHY connected via the
  458. //! RMII interface.
  459. //!
  460. //! If \b EMAC_PHY_TYPE_INTERNAL is selected, the following flags may be ORed
  461. //! into \e ui32Config to control various PHY features and modes. These flags
  462. //! are ignored if an external PHY is selected.
  463. //!
  464. //! - \b EMAC_PHY_INT_NIB_TXERR_DET_DIS disables odd nibble transmit error
  465. //! detection (sets the default value of PHY register MR10, bit 1).
  466. //! - \b EMAC_PHY_INT_RX_ER_DURING_IDLE enables receive error detection during
  467. //! idle (sets the default value of PHY register MR10, bit 2).
  468. //! - \b EMAC_PHY_INT_ISOLATE_MII_LLOSS ties the MII outputs low if no link is
  469. //! established in 100B-T and full duplex modes (sets the default value of PHY
  470. //! register MR10, bit 3).
  471. //! - \b EMAC_PHY_INT_LINK_LOSS_RECOVERY enables link loss recovery (sets the
  472. //! default value of PHY register MR9, bit 7).
  473. //! - \b EMAC_PHY_INT_TDRRUN enables execution of the TDR procedure after a link
  474. //! down event (sets the default value of PHY register MR9, bit 8).
  475. //! - \b EMAC_PHY_INT_LD_ON_RX_ERR_COUNT enables link down if the receiver
  476. //! error count reaches 32 within a 10-us interval (sets the default value of
  477. //! PHY register MR11 bit 3).
  478. //! - \b EMAC_PHY_INT_LD_ON_MTL3_ERR_COUNT enables link down if the MTL3 error
  479. //! count reaches 20 in a 10 us-interval (sets the default value of PHY register
  480. //! MR11 bit 2).
  481. //! - \b EMAC_PHY_INT_LD_ON_LOW_SNR enables link down if the low SNR threshold
  482. //! is crossed 20 times in a 10 us-interval (sets the default value of PHY
  483. //! register MR11 bit 1).
  484. //! - \b EMAC_PHY_INT_LD_ON_SIGNAL_ENERGY enables link down if energy detector
  485. //! indicates Energy Loss (sets the default value of PHY register MR11 bit 0).
  486. //! - \b EMAC_PHY_INT_POLARITY_SWAP inverts the polarity on both TPTD and TPRD
  487. //! pairs (sets the default value of PHY register MR11 bit 5).
  488. //! - \b EMAC_PHY_INT_MDI_SWAP swaps the MDI pairs putting receive on the TPTD
  489. //! pair and transmit on TPRD (sets the default value of PHY register MR11 bit
  490. //! 6).
  491. //! - \b EMAC_PHY_INT_ROBUST_MDIX enables robust auto MDI-X resolution (sets the
  492. //! default value of PHY register MR9 bit 5).
  493. //! - \b EMAC_PHY_INT_FAST_MDIX enables fast auto-MDI/MDIX resolution (sets the
  494. //! default value of PHY register MR9 bit 6).
  495. //! - \b EMAC_PHY_INT_MDIX_EN enables auto-MDI/MDIX crossover (sets the
  496. //! default value of PHY register MR9 bit 14).
  497. //! - \b EMAC_PHY_INT_FAST_RXDV_DETECT enables fast RXDV detection (set the
  498. //! default value of PHY register MR9 bit 1).
  499. //! - \b EMAC_PHY_INT_FAST_L_UP_DETECT enables fast link-up time during parallel
  500. //! detection (sets the default value of PHY register MR10 bit 6)
  501. //! - \b EMAC_PHY_INT_EXT_FULL_DUPLEX forces full-duplex while working with a
  502. //! link partner in forced 100B-TX (sets the default value of PHY register
  503. //! MR10 bit 5).
  504. //! - \b EMAC_PHY_INT_FAST_AN_80_50_35 enables fast auto-negotiation using
  505. //! break link, link fail inhibit and wait timers set to 80, 50 and 35
  506. //! respectively (sets the default value of PHY register MR9 bits [4:2] to
  507. //! 3b100).
  508. //! - \b EMAC_PHY_INT_FAST_AN_120_75_50 enables fast auto-negotiation using
  509. //! break link, link fail inhibit and wait timers set to 120, 75 and 50
  510. //! respectively (sets the default value of PHY register MR9 bits [4:2] to
  511. //! 3b101).
  512. //! - \b EMAC_PHY_INT_FAST_AN_140_150_100 enables fast auto-negotiation using
  513. //! break link, link fail inhibit and wait timers set to 140, 150 and 100
  514. //! respectively (sets the default value of PHY register MR9 bits [4:2] to
  515. //! 3b110).
  516. //! - \b EMAC_PHY_FORCE_10B_T_HALF_DUPLEX disables auto-negotiation and forces
  517. //! operation in 10Base-T, half duplex mode (sets the default value of PHY
  518. //! register MR9 bits [13:11] to 3b000).
  519. //! - \b EMAC_PHY_FORCE_10B_T_FULL_DUPLEX disables auto-negotiation and forces
  520. //! operation in 10Base-T, full duplex mode (sets the default value of PHY
  521. //! register MR9 bits [13:11] to 3b001).
  522. //! - \b EMAC_PHY_FORCE_100B_T_HALF_DUPLEX disables auto-negotiation and forces
  523. //! operation in 100Base-T, half duplex mode (sets the default value of PHY
  524. //! register MR9 bits [13:11] to 3b010).
  525. //! - \b EMAC_PHY_FORCE_100B_T_FULL_DUPLEX disables auto-negotiation and forces
  526. //! operation in 100Base-T, full duplex mode (sets the default value of PHY
  527. //! register MR9 bits [13:11] to 3b011).
  528. //! - \b EMAC_PHY_AN_10B_T_HALF_DUPLEX enables auto-negotiation and advertises
  529. //! 10Base-T, half duplex mode (sets the default value of PHY register MR9 bits
  530. //! [13:11] to 3b100).
  531. //! - \b EMAC_PHY_AN_10B_T_FULL_DUPLEX enables auto-negotiation and advertises
  532. //! 10Base-T half or full duplex modes (sets the default value of PHY register
  533. //! MR9 bits [13:11] to 3b101).
  534. //! - \b EMAC_PHY_AN_100B_T_HALF_DUPLEX enables auto-negotiation and advertises
  535. //! 10Base-T half or full duplex, and 100Base-T half duplex modes (sets the
  536. //! default value of PHY register MR9 bits [13:11] to 3b110).
  537. //! - \b EMAC_PHY_AN_100B_T_FULL_DUPLEX enables auto-negotiation and advertises
  538. //! 10Base-T half or full duplex, and 100Base-T half or full duplex modes (sets
  539. //! the default value of PHY register MR9 bits [13:11] to 3b111).
  540. //! - \b EMAC_PHY_INT_HOLD prevents the PHY from transmitting energy on the
  541. //! line.
  542. //!
  543. //! As a side effect of this function, the Ethernet MAC is reset so any
  544. //! previous MAC configuration is lost.
  545. //!
  546. //! \return None.
  547. //
  548. //*****************************************************************************
  549. void
  550. EMACPHYConfigSet(uint32_t ui32Base, uint32_t ui32Config)
  551. {
  552. //
  553. // Write the Ethernet PHY configuration to the peripheral configuration
  554. // register.
  555. //
  556. HWREG(ui32Base + EMAC_O_PC) = ui32Config;
  557. //
  558. // If using the internal PHY, reset it to ensure that new configuration is
  559. // latched there.
  560. //
  561. if((ui32Config & EMAC_PHY_TYPE_MASK) == EMAC_PHY_TYPE_INTERNAL)
  562. {
  563. SysCtlPeripheralReset(SYSCTL_PERIPH_EPHY0);
  564. while(!SysCtlPeripheralReady(SYSCTL_PERIPH_EPHY0))
  565. {
  566. //
  567. // Wait for the PHY reset to complete.
  568. //
  569. }
  570. //
  571. // Delay a bit longer to ensure that the PHY reset has completed.
  572. //
  573. SysCtlDelay(10000);
  574. }
  575. //
  576. // If using an external RMII PHY, we must set 2 bits in the Ethernet MAC
  577. // Clock Configuration Register.
  578. //
  579. if((ui32Config & EMAC_PHY_TYPE_MASK) == EMAC_PHY_TYPE_EXTERNAL_RMII)
  580. {
  581. //
  582. // Select and enable the external clock from the RMII PHY.
  583. //
  584. HWREG(EMAC0_BASE + EMAC_O_CC) |= EMAC_CC_CLKEN;
  585. }
  586. else
  587. {
  588. //
  589. // Disable the external clock.
  590. //
  591. HWREG(EMAC0_BASE + EMAC_O_CC) &= ~EMAC_CC_CLKEN;
  592. }
  593. //
  594. // Reset the MAC regardless of whether the PHY connection changed or not.
  595. //
  596. EMACReset(EMAC0_BASE);
  597. SysCtlDelay(1000);
  598. }
  599. //*****************************************************************************
  600. //
  601. //! Configures basic Ethernet MAC operation parameters.
  602. //!
  603. //! \param ui32Base is the base address of the Ethernet controller.
  604. //! \param ui32Config provides various flags and values configuring the MAC.
  605. //! \param ui32ModeFlags provides configuration relating to the transmit and
  606. //! receive DMA engines.
  607. //! \param ui32RxMaxFrameSize sets the maximum receive frame size above which
  608. //! an error is reported.
  609. //!
  610. //! This function is called to configure basic operating parameters for the
  611. //! MAC and its DMA engines.
  612. //!
  613. //! The \e ui32Config parameter is the logical OR of various fields and
  614. //! flags. The first field determines which MAC address is used during
  615. //! insertion or replacement for all transmitted frames. Valid options are
  616. //!
  617. //! - \b EMAC_CONFIG_USE_MACADDR1 and
  618. //! - \b EMAC_CONFIG_USE_MACADDR0
  619. //!
  620. //! The interframe gap between transmitted frames is controlled using one of
  621. //! the following values:
  622. //!
  623. //! - \b EMAC_CONFIG_IF_GAP_96BITS
  624. //! - \b EMAC_CONFIG_IF_GAP_88BITS
  625. //! - \b EMAC_CONFIG_IF_GAP_80BITS
  626. //! - \b EMAC_CONFIG_IF_GAP_72BITS
  627. //! - \b EMAC_CONFIG_IF_GAP_64BITS
  628. //! - \b EMAC_CONFIG_IF_GAP_56BITS
  629. //! - \b EMAC_CONFIG_IF_GAP_48BITS
  630. //! - \b EMAC_CONFIG_IF_GAP_40BITS
  631. //!
  632. //! The number of bytes of preamble added to the beginning of every transmitted
  633. //! frame is selected using one of the following values:
  634. //!
  635. //! - \b EMAC_CONFIG_7BYTE_PREAMBLE
  636. //! - \b EMAC_CONFIG_5BYTE_PREAMBLE
  637. //! - \b EMAC_CONFIG_3BYTE_PREAMBLE
  638. //!
  639. //! The back-off limit determines the range of the random time that the MAC
  640. //! delays after a collision and before attempting to retransmit a frame. One
  641. //! of the following values must be used to select this limit. In each case,
  642. //! the retransmission delay in terms of 512 bit time slots, is the lower of
  643. //! (2 ** N) and a random number between 0 and the selected backoff-limit.
  644. //!
  645. //! - \b EMAC_CONFIG_BO_LIMIT_1024
  646. //! - \b EMAC_CONFIG_BO_LIMIT_256
  647. //! - \b EMAC_CONFIG_BO_LIMIT_16
  648. //! - \b EMAC_CONFIG_BO_LIMIT_2
  649. //!
  650. //! Control over insertion or replacement of the source address in all
  651. //! transmitted frames is provided by using one of the following fields:
  652. //!
  653. //! - \b EMAC_CONFIG_SA_INSERT causes the MAC address (0 or 1 depending
  654. //! on whether \b EMAC_CONFIG_USE_MACADDR0 or \b EMAC_CONFIG_USE_MACADDR1
  655. //! was specified) to be inserted into all transmitted frames.
  656. //! - \b EMAC_CONFIG_SA_REPLACE causes the MAC address to be replaced with
  657. //! the selected address in all transmitted frames.
  658. //! - \b EMAC_CONFIG_SA_FROM_DESCRIPTOR causes control of source address
  659. //! insertion or deletion to be controlled by fields in the DMA transmit
  660. //! descriptor, allowing control on a frame-by-frame basis.
  661. //!
  662. //! Whether the interface attempts to operate in full- or half-duplex mode is
  663. //! controlled by one of the following flags:
  664. //!
  665. //! - \b EMAC_CONFIG_FULL_DUPLEX
  666. //! - \b EMAC_CONFIG_HALF_DUPLEX
  667. //!
  668. //! The following additional flags may also be specified:
  669. //!
  670. //! - \b EMAC_CONFIG_2K_PACKETS enables IEEE802.3as support for 2K packets.
  671. //! When specified, the MAC considers all frames up to 2000 bytes in length as
  672. //! normal packets. When \b EMAC_CONFIG_JUMBO_ENABLE is not specified, all
  673. //! frames larger than 2000 bytes are treated as Giant frames. This flag is
  674. //! ignored if \b EMAC_CONFIG_JUMBO_ENABLE is specified.
  675. //! - \b EMAC_CONFIG_STRIP_CRC causes the 4-byte CRC of all Ethernet type
  676. //! frames to be stripped and dropped before the frame is forwarded to the
  677. //! application.
  678. //! - \b EMAC_CONFIG_JABBER_DISABLE disables the jabber timer on the
  679. //! transmitter and enables frames of up to 16384 bytes to be transmitted. If
  680. //! this flag is absent, the MAC does not allow more than 2048 (or 10240 if
  681. //! \b EMAC_CONFIG_JUMBO_ENABLE is specified) bytes to be sent in any one
  682. //! frame.
  683. //! - \b EMAC_CONFIG_JUMBO_ENABLE enables Jumbo Frames, allowing frames of
  684. //! up to 9018 (or 9022 if using VLAN tagging) to be handled without reporting
  685. //! giant frame errors.
  686. //! - \b EMAC_CONFIG_100MBPS forces the MAC to communicate with the PHY using
  687. //! 100Mbps signaling. If this option is not specified, the MAC uses 10Mbps
  688. //! signaling. This speed setting is important when using an external RMII
  689. //! PHY where the selected rate must match the PHY's setting which may have
  690. //! been made as a result of auto-negotiation. When using the internal PHY
  691. //! or an external MII PHY, the signaling rate is controlled by the PHY-
  692. //! provided transmit and receive clocks.
  693. //! - \b EMAC_CONFIG_CS_DISABLE disables Carrier Sense during transmission
  694. //! when operating in half-duplex mode.
  695. //! - \b EMAC_CONFIG_RX_OWN_DISABLE disables reception of transmitted frames
  696. //! when operating in half-duplex mode.
  697. //! - \b EMAC_CONFIG_LOOPBACK enables internal loopback.
  698. //! - \b EMAC_CONFIG_CHECKSUM_OFFLOAD enables IPv4 header checksum checking
  699. //! and IPv4 or IPv6 TCP, UPD or ICMP payload checksum checking. The results
  700. //! of the checksum calculations are reported via status fields in the DMA
  701. //! receive descriptors.
  702. //! - \b EMAC_CONFIG_RETRY_DISABLE disables retransmission in cases where
  703. //! half-duplex mode is in use and a collision occurs. This condition causes
  704. //! the current frame to be ignored and a frame abort to be reported in the
  705. //! transmit frame status.
  706. //! - \b EMAC_CONFIG_AUTO_CRC_STRIPPING strips the last 4 bytes (frame check
  707. //! sequence) from all Ether type frames before forwarding the frames to the
  708. //! application.
  709. //! - \b EMAC_CONFIG_DEFERRAL_CHK_ENABLE enables transmit deferral checking
  710. //! in half-duplex mode. When enabled, the transmitter reports an error if it
  711. //! is unable to transmit a frame for more than 24288 bit times (or 155680
  712. //! bit times in Jumbo frame mode) due to an active carrier sense signal on
  713. //! the MII.
  714. //!
  715. //! The \e ui32ModeFlags parameter sets operating parameters related to the
  716. //! internal MAC FIFOs. It comprises a logical OR of the following fields.
  717. //! The first selects the transmit FIFO threshold. Transmission of a frame
  718. //! begins when this amount of data or a full frame exists in the transmit
  719. //! FIFO. This field is ignored if \b EMAC_MODE_TX_STORE_FORWARD is
  720. //! included. One of the following must be specified:
  721. //!
  722. //! - \b EMAC_MODE_TX_THRESHOLD_16_BYTES
  723. //! - \b EMAC_MODE_TX_THRESHOLD_24_BYTES
  724. //! - \b EMAC_MODE_TX_THRESHOLD_32_BYTES
  725. //! - \b EMAC_MODE_TX_THRESHOLD_40_BYTES
  726. //! - \b EMAC_MODE_TX_THRESHOLD_64_BYTES
  727. //! - \b EMAC_MODE_TX_THRESHOLD_128_BYTES
  728. //! - \b EMAC_MODE_TX_THRESHOLD_192_BYTES
  729. //! - \b EMAC_MODE_TX_THRESHOLD_256_BYTES
  730. //!
  731. //! The second field controls the receive FIFO threshold. DMA transfers of
  732. //! received data begin either when the receive FIFO contains a full frame
  733. //! or this number of bytes. This field is ignored if
  734. //! \b EMAC_MODE_RX_STORE_FORWARD is included. One of the following must be
  735. //! specified:
  736. //!
  737. //! - \b EMAC_MODE_RX_THRESHOLD_64_BYTES
  738. //! - \b EMAC_MODE_RX_THRESHOLD_32_BYTES
  739. //! - \b EMAC_MODE_RX_THRESHOLD_96_BYTES
  740. //! - \b EMAC_MODE_RX_THRESHOLD_128_BYTES
  741. //!
  742. //! The following additional flags may be specified:
  743. //!
  744. //! - \b EMAC_MODE_KEEP_BAD_CRC causes frames with TCP/IP checksum errors
  745. //! to be forwarded to the application if those frames do not have any errors
  746. //! (including FCS errors) in the Ethernet framing. In these cases, the frames
  747. //! have errors only in the payload. If this flag is not specified, all frames
  748. //! with any detected error are discarded unless \b EMAC_MODE_RX_ERROR_FRAMES
  749. //! is also specified.
  750. //! - \b EMAC_MODE_RX_STORE_FORWARD causes the receive DMA to read frames
  751. //! from the FIFO only after the complete frame has been written to it. If
  752. //! this mode is enabled, the receive threshold is ignored.
  753. //! - \b EMAC_MODE_RX_FLUSH_DISABLE disables the flushing of received frames
  754. //! in cases where receive descriptors or buffers are unavailable.
  755. //! - \b EMAC_MODE_TX_STORE_FORWARD causes the transmitter to start
  756. //! transmitting a frame only after the whole frame has been written to the
  757. //! transmit FIFO. If this mode is enabled, the transmit threshold is ignored.
  758. //! - \b EMAC_MODE_RX_ERROR_FRAMES causes all frames other than runt error
  759. //! frames to be forwarded to the receive DMA regardless of any errors detected
  760. //! in the frames.
  761. //! - \b EMAC_MODE_RX_UNDERSIZED_FRAMES causes undersized frames (frames
  762. //! shorter than 64 bytes but with no errors) to the application. If this
  763. //! option is not selected, all undersized frames are dropped by the receiver
  764. //! unless it has already started transferring them to the receive FIFO due to
  765. //! the receive threshold setting.
  766. //! - \b EMAC_MODE_OPERATE_2ND_FRAME enables the transmit DMA to operate on a
  767. //! second frame while waiting for the previous frame to be transmitted and
  768. //! associated status and timestamps to be reported. If absent, the transmit
  769. //! DMA works on a single frame at any one time, waiting for that frame to be
  770. //! transmitted and its status to be received before moving on to the next
  771. //! frame.
  772. //!
  773. //! The \e ui32RxMaxFrameSize parameter may be used to override the default
  774. //! setting for the maximum number of bytes that can be received in a frame
  775. //! before that frame is flagged as being in error. If the parameter is set
  776. //! to 0, the default hardware settings are applied. If non-zero, any frame
  777. //! received which is longer than the \e ui32RxMaxFrameSize, regardless of
  778. //! whether the MAC is configured for normal or Jumbo frame operation, is
  779. //! flagged as an error.
  780. //!
  781. //! \return None.
  782. //
  783. //*****************************************************************************
  784. void
  785. EMACConfigSet(uint32_t ui32Base, uint32_t ui32Config, uint32_t ui32ModeFlags,
  786. uint32_t ui32RxMaxFrameSize)
  787. {
  788. //
  789. // Parameter sanity check. Note that we allow TX_ENABLED and RX_ENABLED
  790. // here because we'll mask them off before writing the value and this
  791. // makes back-to-back EMACConfigGet/EMACConfigSet calls work without the
  792. // caller needing to explicitly remove these bits from the parameter.
  793. //
  794. ASSERT((ui32Config & ~(VALID_CONFIG_FLAGS | EMAC_CONFIG_TX_ENABLED |
  795. EMAC_CONFIG_RX_ENABLED)) == 0);
  796. ASSERT(!ui32RxMaxFrameSize || ((ui32RxMaxFrameSize < 0x4000) &&
  797. (ui32RxMaxFrameSize > 1522)));
  798. //
  799. // Set the configuration flags as specified. Note that we unconditionally
  800. // OR in the EMAC_CFG_PS bit here since this implementation supports only
  801. // MII and RMII interfaces to the PHYs.
  802. //
  803. HWREG(ui32Base + EMAC_O_CFG) =
  804. ((HWREG(ui32Base + EMAC_O_CFG) & ~VALID_CONFIG_FLAGS) | ui32Config |
  805. EMAC_CFG_PS);
  806. //
  807. // Set the maximum receive frame size. If 0 is passed, this implies
  808. // that the default maximum frame size should be used so just turn off
  809. // the override.
  810. //
  811. if(ui32RxMaxFrameSize)
  812. {
  813. HWREG(ui32Base + EMAC_O_WDOGTO) = ui32RxMaxFrameSize | EMAC_WDOGTO_PWE;
  814. }
  815. else
  816. {
  817. HWREG(ui32Base + EMAC_O_WDOGTO) &= ~EMAC_WDOGTO_PWE;
  818. }
  819. //
  820. // Set the operating mode register.
  821. //
  822. HWREG(ui32Base + EMAC_O_DMAOPMODE) = ui32ModeFlags;
  823. }
  824. //*****************************************************************************
  825. //
  826. //! Returns the Ethernet MAC's current basic configuration parameters.
  827. //!
  828. //! \param ui32Base is the base address of the Ethernet controller.
  829. //! \param pui32Config points to storage that is written with Ethernet MAC
  830. //! configuration.
  831. //! \param pui32Mode points to storage that is written with Ethernet MAC mode
  832. //! information.
  833. //! \param pui32RxMaxFrameSize points to storage that is written with the
  834. //! maximum receive frame size.
  835. //!
  836. //! This function is called to query the basic operating parameters for the
  837. //! MAC and its DMA engines.
  838. //!
  839. //! The \e pui32Config parameter is written with the logical OR of various
  840. //! fields and flags. The first field describes which MAC address is used
  841. //! during insertion or replacement for all transmitted frames. Valid options
  842. //! are
  843. //!
  844. //! - \b EMAC_CONFIG_USE_MACADDR1
  845. //! - \b EMAC_CONFIG_USE_MACADDR0
  846. //!
  847. //! The interframe gap between transmitted frames is given using one of the
  848. //! following values:
  849. //!
  850. //! - \b EMAC_CONFIG_IF_GAP_96BITS
  851. //! - \b EMAC_CONFIG_IF_GAP_88BITS
  852. //! - \b EMAC_CONFIG_IF_GAP_80BITS
  853. //! - \b EMAC_CONFIG_IF_GAP_72BITS
  854. //! - \b EMAC_CONFIG_IF_GAP_64BITS
  855. //! - \b EMAC_CONFIG_IF_GAP_56BITS
  856. //! - \b EMAC_CONFIG_IF_GAP_48BITS
  857. //! - \b EMAC_CONFIG_IF_GAP_40BITS
  858. //!
  859. //! The number of bytes of preamble added to the beginning of every transmitted
  860. //! frame is described using one of the following values:
  861. //!
  862. //! - \b EMAC_CONFIG_7BYTE_PREAMBLE
  863. //! - \b EMAC_CONFIG_5BYTE_PREAMBLE
  864. //! - \b EMAC_CONFIG_3BYTE_PREAMBLE
  865. //!
  866. //! The back-off limit determines the range of the random time that the MAC
  867. //! delays after a collision and before attempting to retransmit a frame. One
  868. //! of the following values provides the currently selected limit. In each
  869. //! case the retransmission delay in terms of 512 bit time slots, is the
  870. //! lower of (2 ** N) and a random number between 0 and the reported
  871. //! backoff-limit.
  872. //!
  873. //! - \b EMAC_CONFIG_BO_LIMIT_1024
  874. //! - \b EMAC_CONFIG_BO_LIMIT_256
  875. //! - \b EMAC_CONFIG_BO_LIMIT_16
  876. //! - \b EMAC_CONFIG_BO_LIMIT_2
  877. //!
  878. //! Handling of insertion or replacement of the source address in all
  879. //! transmitted frames is described by one of the following fields:
  880. //!
  881. //! - \b EMAC_CONFIG_SA_INSERT causes the MAC address (0 or 1 depending
  882. //! on whether \b EMAC_CONFIG_USE_MACADDR0 or \b EMAC_CONFIG_USE_MACADDR1
  883. //! was specified) to be inserted into all transmitted frames.
  884. //! - \b EMAC_CONFIG_SA_REPLACE causes the MAC address to be replaced with
  885. //! the selected address in all transmitted frames.
  886. //! - \b EMAC_CONFIG_SA_FROM_DESCRIPTOR causes control of source address
  887. //! insertion or deletion to be controlled by fields in the DMA transmit
  888. //! descriptor, allowing control on a frame-by-frame basis.
  889. //!
  890. //! Whether the interface attempts to operate in full- or half-duplex mode is
  891. //! reported by one of the following flags:
  892. //!
  893. //! - \b EMAC_CONFIG_FULL_DUPLEX
  894. //! - \b EMAC_CONFIG_HALF_DUPLEX
  895. //!
  896. //! The following additional flags may also be included:
  897. //!
  898. //! - \b EMAC_CONFIG_2K_PACKETS indicates that IEEE802.3as support for 2K
  899. //! packets is enabled. When present, the MAC considers all frames up to 2000
  900. //! bytes in length as normal packets. When \b EMAC_CONFIG_JUMBO_ENABLE is
  901. //! not reported, all frames larger than 2000 bytes are treated as Giant
  902. //! frames. The value of this flag should be ignored if
  903. //! \b EMAC_CONFIG_JUMBO_ENABLE is also reported.
  904. //! - \b EMAC_CONFIG_STRIP_CRC indicates that the 4-byte CRC of all Ethernet
  905. //! type frames is being stripped and dropped before the frame is forwarded to
  906. //! the application.
  907. //! - \b EMAC_CONFIG_JABBER_DISABLE indicates that the the jabber timer on the
  908. //! transmitter is disabled, allowing frames of up to 16384 bytes to be
  909. //! transmitted. If this flag is absent, the MAC does not allow more than 2048
  910. //! (or 10240 if \b EMAC_CONFIG_JUMBO_ENABLE is reported) bytes to be sent in
  911. //! any one frame.
  912. //! - \b EMAC_CONFIG_JUMBO_ENABLE indicates that Jumbo Frames of up to 9018
  913. //! (or 9022 if using VLAN tagging) are enabled.
  914. //! - \b EMAC_CONFIG_CS_DISABLE indicates that Carrier Sense is disabled
  915. //! during transmission when operating in half-duplex mode.
  916. //! - \b EMAC_CONFIG_100MBPS indicates that the MAC is using 100Mbps
  917. //! signaling to communicate with the PHY.
  918. //! - \b EMAC_CONFIG_RX_OWN_DISABLE indicates that reception of transmitted
  919. //! frames is disabled when operating in half-duplex mode.
  920. //! - \b EMAC_CONFIG_LOOPBACK indicates that internal loopback is enabled.
  921. //! - \b EMAC_CONFIG_CHECKSUM_OFFLOAD indicates that IPv4 header checksum
  922. //! checking and IPv4 or IPv6 TCP, UPD or ICMP payload checksum checking is
  923. //! enabled. The results of the checksum calculations are reported via status
  924. //! fields in the DMA receive descriptors.
  925. //! - \b EMAC_CONFIG_RETRY_DISABLE indicates that retransmission is disabled
  926. //! in cases where half-duplex mode is in use and a collision occurs. This
  927. //! condition causes the current frame to be ignored and a frame abort to be
  928. //! reported in the transmit frame status.
  929. //! - \b EMAC_CONFIG_AUTO_CRC_STRIPPING indicates that the last 4 bytes
  930. //! (frame check sequence) from all Ether type frames are being stripped before
  931. //! frames are forwarded to the application.
  932. //! - \b EMAC_CONFIG_DEFERRAL_CHK_ENABLE indicates that transmit deferral
  933. //! checking is disabled in half-duplex mode. When enabled, the transmitter
  934. //! reports an error if it is unable to transmit a frame for more than 24288
  935. //! bit times (or 155680 bit times in Jumbo frame mode) due to an active
  936. //! carrier sense signal on the MII.
  937. //! - \b EMAC_CONFIG_TX_ENABLED indicates that the MAC transmitter is
  938. //! currently enabled.
  939. //! - \b EMAC_CONFIG_RX_ENABLED indicates that the MAC receiver is
  940. //! currently enabled.
  941. //!
  942. //! The \e pui32ModeFlags parameter is written with operating parameters
  943. //! related to the internal MAC FIFOs. It comprises a logical OR of the
  944. //! following fields. The first field reports the transmit FIFO threshold.
  945. //! Transmission of a frame begins when this amount of data or a full frame
  946. //! exists in the transmit FIFO. This field should be ignored if
  947. //! \b EMAC_MODE_TX_STORE_FORWARD is also reported. One of the following
  948. //! values is reported:
  949. //!
  950. //! - \b EMAC_MODE_TX_THRESHOLD_16_BYTES
  951. //! - \b EMAC_MODE_TX_THRESHOLD_24_BYTES
  952. //! - \b EMAC_MODE_TX_THRESHOLD_32_BYTES
  953. //! - \b EMAC_MODE_TX_THRESHOLD_40_BYTES
  954. //! - \b EMAC_MODE_TX_THRESHOLD_64_BYTES
  955. //! - \b EMAC_MODE_TX_THRESHOLD_128_BYTES
  956. //! - \b EMAC_MODE_TX_THRESHOLD_192_BYTES
  957. //! - \b EMAC_MODE_TX_THRESHOLD_256_BYTES
  958. //!
  959. //! The second field reports the receive FIFO threshold. DMA transfers of
  960. //! received data begin either when the receive FIFO contains a full frame
  961. //! or this number of bytes. This field should be ignored if
  962. //! \b EMAC_MODE_RX_STORE_FORWARD is included. One of the following values
  963. //! is reported:
  964. //!
  965. //! - \b EMAC_MODE_RX_THRESHOLD_64_BYTES
  966. //! - \b EMAC_MODE_RX_THRESHOLD_32_BYTES
  967. //! - \b EMAC_MODE_RX_THRESHOLD_96_BYTES
  968. //! - \b EMAC_MODE_RX_THRESHOLD_128_BYTES
  969. //!
  970. //! The following additional flags may be included:
  971. //!
  972. //! - \b EMAC_MODE_KEEP_BAD_CRC indicates that frames with TCP/IP checksum
  973. //! errors are being forwarded to the application if those frames do not have
  974. //! any errors (including FCS errors) in the Ethernet framing. In these cases,
  975. //! the frames have errors only in the payload. If this flag is not reported,
  976. //! all frames with any detected error are discarded unless
  977. //! \b EMAC_MODE_RX_ERROR_FRAMES is also reported.
  978. //! - \b EMAC_MODE_RX_STORE_FORWARD indicates that the receive DMA is
  979. //! configured to read frames from the FIFO only after the complete frame has
  980. //! been written to it. If this mode is enabled, the receive threshold is
  981. //! ignored.
  982. //! - \b EMAC_MODE_RX_FLUSH_DISABLE indicates that the flushing of received
  983. //! frames is disabled in cases where receive descriptors or buffers are
  984. //! unavailable.
  985. //! - \b EMAC_MODE_TX_STORE_FORWARD indicates that the transmitter is
  986. //! configured to transmit a frame only after the whole frame has been written
  987. //! to the transmit FIFO. If this mode is enabled, the transmit threshold is
  988. //! ignored.
  989. //! - \b EMAC_MODE_RX_ERROR_FRAMES indicates that all frames other than runt
  990. //! error frames are being forwarded to the receive DMA regardless of any
  991. //! errors detected in the frames.
  992. //! - \b EMAC_MODE_RX_UNDERSIZED_FRAMES indicates that undersized frames
  993. //! (frames shorter than 64 bytes but with no errors) are being forwarded to
  994. //! the application. If this option is not reported, all undersized frames are
  995. //! dropped by the receiver unless it has already started transferring them to
  996. //! the receive FIFO due to the receive threshold setting.
  997. //! - \b EMAC_MODE_OPERATE_2ND_FRAME indicates that the transmit DMA is
  998. //! configured to operate on a second frame while waiting for the previous
  999. //! frame to be transmitted and associated status and timestamps to be reported.
  1000. //! If absent, the transmit DMA works on a single frame at any one time,
  1001. //! waiting for that frame to be transmitted and its status to be received
  1002. //! before moving on to the next frame.
  1003. //! - \b EMAC_MODE_TX_DMA_ENABLED indicates that the transmit DMA engine is
  1004. //! currently enabled.
  1005. //! - \b EMAC_MODE_RX_DMA_ENABLED indicates that the receive DMA engine is
  1006. //! currently enabled.
  1007. //!
  1008. //! The \e pui32RxMaxFrameSize is written with the currently configured maximum
  1009. //! receive packet size. Packets larger than this are flagged as being in
  1010. //! error.
  1011. //!
  1012. //! \return None.
  1013. //
  1014. //*****************************************************************************
  1015. void
  1016. EMACConfigGet(uint32_t ui32Base, uint32_t *pui32Config, uint32_t *pui32Mode,
  1017. uint32_t *pui32RxMaxFrameSize)
  1018. {
  1019. uint32_t ui32Value;
  1020. //
  1021. // Parameter sanity check.
  1022. //
  1023. ASSERT(pui32Mode);
  1024. ASSERT(pui32Config);
  1025. ASSERT(pui32RxMaxFrameSize);
  1026. //
  1027. // Return the mode information from the operation mode register.
  1028. //
  1029. *pui32Mode = HWREG(ui32Base + EMAC_O_DMAOPMODE);
  1030. //
  1031. // Return the current configuration flags from the EMAC_O_CFG register.
  1032. //
  1033. *pui32Config = (HWREG(ui32Base + EMAC_O_CFG) &
  1034. (VALID_CONFIG_FLAGS | EMAC_CONFIG_TX_ENABLED |
  1035. EMAC_CONFIG_RX_ENABLED));
  1036. //
  1037. // Get the receive packet size watchdog value.
  1038. //
  1039. ui32Value = HWREG(ui32Base + EMAC_O_WDOGTO);
  1040. if(ui32Value & EMAC_WDOGTO_PWE)
  1041. {
  1042. //
  1043. // The watchdog is enables so the maximum packet length can be read
  1044. // from the watchdog timeout register.
  1045. //
  1046. *pui32RxMaxFrameSize = ui32Value & EMAC_WDOGTO_WTO_M;
  1047. }
  1048. else
  1049. {
  1050. //
  1051. // The maximum packet size override found in the watchdog timer
  1052. // register is not enabled so the maximum packet size is determined
  1053. // by whether or not jumbo frame mode is enabled.
  1054. //
  1055. if(HWREG(ui32Base + EMAC_O_CFG) & EMAC_CFG_JFEN)
  1056. {
  1057. //
  1058. // Jumbo frames are enabled so the watchdog kicks in at 10240
  1059. // bytes.
  1060. //
  1061. *pui32RxMaxFrameSize = 10240;
  1062. }
  1063. else
  1064. {
  1065. //
  1066. // Jumbo frames are not enabled so the watchdog kicks in at
  1067. // 2048 bytes.
  1068. //
  1069. *pui32RxMaxFrameSize = 2048;
  1070. }
  1071. }
  1072. }
  1073. //*****************************************************************************
  1074. //
  1075. //! Sets the MAC address of the Ethernet controller.
  1076. //!
  1077. //! \param ui32Base is the base address of the Ethernet controller.
  1078. //! \param ui32Index is the zero-based index of the MAC address to set.
  1079. //! \param pui8MACAddr is the pointer to the array of MAC-48 address octets.
  1080. //!
  1081. //! This function programs the IEEE-defined MAC-48 address specified in
  1082. //! \e pui8MACAddr into the Ethernet controller. This address is used by the
  1083. //! Ethernet controller for hardware-level filtering of incoming Ethernet
  1084. //! packets (when promiscuous mode is not enabled). Index 0 is used to hold
  1085. //! the local node's MAC address which is inserted into all transmitted
  1086. //! packets.
  1087. //!
  1088. //! The controller may support several Ethernet MAC address slots, each of which
  1089. //! may be programmed independently and used to filter incoming packets. The
  1090. //! number of MAC addresses that the hardware supports may be queried using a
  1091. //! call to EMACNumAddrGet(). The value of the \e ui32Index parameter must
  1092. //! lie in the range from 0 to (number of MAC addresses - 1) inclusive.
  1093. //!
  1094. //! The MAC-48 address is defined as 6 octets, illustrated by the following
  1095. //! example address. The numbers are shown in hexadecimal format.
  1096. //!
  1097. //! AC-DE-48-00-00-80
  1098. //!
  1099. //! In this representation, the first three octets (AC-DE-48) are the
  1100. //! Organizationally Unique Identifier (OUI). This is a number assigned by
  1101. //! the IEEE to an organization that requests a block of MAC addresses. The
  1102. //! last three octets (00-00-80) are a 24-bit number managed by the OUI owner
  1103. //! to uniquely identify a piece of hardware within that organization that is
  1104. //! to be connected to the Ethernet.
  1105. //!
  1106. //! In this representation, the octets are transmitted from left to right,
  1107. //! with the ``AC'' octet being transmitted first and the ``80'' octet being
  1108. //! transmitted last. Within an octet, the bits are transmitted LSB to MSB.
  1109. //! For this address, the first bit to be transmitted would be ``0'', the LSB
  1110. //! of ``AC'', and the last bit to be transmitted would be ``1'', the MSB of
  1111. //! ``80''.
  1112. //!
  1113. //! The address passed to this function in the \e pui8MACAddr array is
  1114. //! ordered with the first byte to be transmitted in the first array entry.
  1115. //! For example, the address given above could be represented using the
  1116. //! following array:
  1117. //!
  1118. //! uint8_t g_pui8MACAddr[] = { 0xAC, 0xDE, 0x48, 0x00, 0x00, 0x80 };
  1119. //!
  1120. //! If the MAC address set by this function is currently enabled, it remains
  1121. //! enabled following this call. Similarly, any filter configured for
  1122. //! the MAC address remains unaffected by a change in the address.
  1123. //!
  1124. //! \return None.
  1125. //
  1126. //*****************************************************************************
  1127. void
  1128. EMACAddrSet(uint32_t ui32Base, uint32_t ui32Index, const uint8_t *pui8MACAddr)
  1129. {
  1130. //
  1131. // Parameter sanity check.
  1132. //
  1133. ASSERT(ui32Index < NUM_MAC_ADDR);
  1134. ASSERT(pui8MACAddr);
  1135. //
  1136. // Set the high 2 bytes of the MAC address. Note that we must set the
  1137. // registers in this order since the address is latched internally
  1138. // on the write to EMAC_O_ADDRL.
  1139. //
  1140. HWREG(ui32Base + EMAC_O_ADDRH(ui32Index)) =
  1141. ((HWREG(ui32Base + EMAC_O_ADDRH(ui32Index)) & 0xFFFF0000) |
  1142. pui8MACAddr[4] | (pui8MACAddr[5] << 8));
  1143. //
  1144. // Set the first 4 bytes of the MAC address
  1145. //
  1146. HWREG(ui32Base + EMAC_O_ADDRL(ui32Index)) =
  1147. (pui8MACAddr[0] | (pui8MACAddr[1] << 8) | (pui8MACAddr[2] << 16) |
  1148. (pui8MACAddr[3] << 24));
  1149. }
  1150. //*****************************************************************************
  1151. //
  1152. //! Gets one of the MAC addresses stored in the Ethernet controller.
  1153. //!
  1154. //! \param ui32Base is the base address of the controller.
  1155. //! \param ui32Index is the zero-based index of the MAC address to return.
  1156. //! \param pui8MACAddr is the pointer to the location in which to store the
  1157. //! array of MAC-48 address octets.
  1158. //!
  1159. //! This function reads the currently programmed MAC address into the
  1160. //! \e pui8MACAddr buffer. The \e ui32Index parameter defines which of the
  1161. //! hardware's MAC addresses to return. The number of MAC addresses supported
  1162. //! by the controller may be queried using a call to EMACNumAddrGet().
  1163. //! Index 0 refers to the MAC address of the local node. Other indices are
  1164. //! used to define MAC addresses when filtering incoming packets.
  1165. //!
  1166. //! The address is written to the pui8MACAddr array ordered with the first byte
  1167. //! to be transmitted in the first array entry. For example, if the address
  1168. //! is written in its usual form with the Organizationally Unique Identifier
  1169. //! (OUI) shown first as:
  1170. //!
  1171. //! AC-DE-48-00-00-80
  1172. //!
  1173. //! the data is returned with 0xAC in the first byte of the array, 0xDE in
  1174. //! the second, 0x48 in the third and so on.
  1175. //!
  1176. //! \return None.
  1177. //
  1178. //*****************************************************************************
  1179. void
  1180. EMACAddrGet(uint32_t ui32Base, uint32_t ui32Index, uint8_t *pui8MACAddr)
  1181. {
  1182. uint32_t ui32Val;
  1183. //
  1184. // Parameter sanity check.
  1185. //
  1186. ASSERT(ui32Index < NUM_MAC_ADDR);
  1187. ASSERT(pui8MACAddr);
  1188. //
  1189. // Get the first 4 bytes of the MAC address.
  1190. //
  1191. ui32Val = HWREG(ui32Base + EMAC_O_ADDRL(ui32Index));
  1192. pui8MACAddr[0] = ui32Val & 0xFF;
  1193. pui8MACAddr[1] = (ui32Val >> 8) & 0xFF;
  1194. pui8MACAddr[2] = (ui32Val >> 16) & 0xFF;
  1195. pui8MACAddr[3] = (ui32Val >> 24) & 0xFF;
  1196. //
  1197. // Get the last 2 bytes of the MAC address.
  1198. //
  1199. ui32Val = HWREG(ui32Base + EMAC_O_ADDRH(ui32Index));
  1200. pui8MACAddr[4] = ui32Val & 0xFF;
  1201. pui8MACAddr[5] = (ui32Val >> 8) & 0xFF;
  1202. }
  1203. //*****************************************************************************
  1204. //
  1205. //! Returns the number of MAC addresses supported by the Ethernet controller.
  1206. //!
  1207. //! \param ui32Base is the base address of the Ethernet controller.
  1208. //!
  1209. //! This function may be used to determine the number of MAC addresses that the
  1210. //! given controller supports. MAC address slots may be used when performing
  1211. //! perfect (rather than hash table) filtering of packets.
  1212. //!
  1213. //! \return Returns the number of supported MAC addresses.
  1214. //
  1215. //*****************************************************************************
  1216. uint32_t
  1217. EMACNumAddrGet(uint32_t ui32Base)
  1218. {
  1219. //
  1220. // The only Ethernet controller on Snowflake supports 4 MAC addresses.
  1221. //
  1222. return(NUM_MAC_ADDR);
  1223. }
  1224. //*****************************************************************************
  1225. //
  1226. //! Sets filtering parameters associated with one of the configured MAC
  1227. //! addresses.
  1228. //!
  1229. //! \param ui32Base is the base address of the controller.
  1230. //! \param ui32Index is the index of the MAC address slot for which the filter
  1231. //! is to be set.
  1232. //! \param ui32Config sets the filter parameters for the given MAC address.
  1233. //!
  1234. //! This function sets filtering parameters associated with one of the MAC
  1235. //! address slots that the controller supports. This configuration is used
  1236. //! when perfect filtering (rather than hash table filtering) is selected.
  1237. //!
  1238. //! Valid values for \e ui32Index are from 1 to (number of MAC address
  1239. //! slots - 1). The number of supported MAC address slots may be found by
  1240. //! calling EMACNumAddrGet(). MAC index 0 is the local MAC address and does
  1241. //! not have filtering parameters associated with it.
  1242. //!
  1243. //! The \e ui32Config parameter determines how the given MAC address is used
  1244. //! when filtering incoming Ethernet frames. It is comprised of a logical OR
  1245. //! of the fields:
  1246. //!
  1247. //! - \b EMAC_FILTER_ADDR_ENABLE indicates that this MAC address is enabled
  1248. //! and should be used when performing perfect filtering. If this flag is
  1249. //! absent, the MAC address at the given index is disabled and is not used
  1250. //! in filtering.
  1251. //! - \b EMAC_FILTER_SOURCE_ADDR indicates that the MAC address at the given
  1252. //! index is compared to the source address of incoming frames while
  1253. //! performing perfect filtering. If absent, the MAC address is compared
  1254. //! against the destination address.
  1255. //! - \b EMAC_FILTER_MASK_BYTE_6 indicates that the MAC should ignore the
  1256. //! sixth byte of the source or destination address when filtering.
  1257. //! - \b EMAC_FILTER_MASK_BYTE_5 indicates that the MAC should ignore the
  1258. //! fifth byte of the source or destination address when filtering.
  1259. //! - \b EMAC_FILTER_MASK_BYTE_4 indicates that the MAC should ignore the
  1260. //! fourth byte of the source or destination address when filtering.
  1261. //! - \b EMAC_FILTER_MASK_BYTE_3 indicates that the MAC should ignore the
  1262. //! third byte of the source or destination address when filtering.
  1263. //! - \b EMAC_FILTER_MASK_BYTE_2 indicates that the MAC should ignore the
  1264. //! second byte of the source or destination address when filtering.
  1265. //! - \b EMAC_FILTER_MASK_BYTE_1 indicates that the MAC should ignore the
  1266. //! first byte of the source or destination address when filtering.
  1267. //!
  1268. //! \return None.
  1269. //
  1270. //*****************************************************************************
  1271. void
  1272. EMACAddrFilterSet(uint32_t ui32Base, uint32_t ui32Index, uint32_t ui32Config)
  1273. {
  1274. uint32_t ui32Val;
  1275. //
  1276. // Parameter sanity check.
  1277. //
  1278. ASSERT(ui32Index < NUM_MAC_ADDR);
  1279. ASSERT((ui32Config & ~(EMAC_FILTER_BYTE_MASK_M |
  1280. EMAC_FILTER_ADDR_ENABLE |
  1281. EMAC_FILTER_SOURCE_ADDR)) == 0);
  1282. ASSERT(ui32Index);
  1283. //
  1284. // Set the filter configuration for a particular MAC address.
  1285. //
  1286. HWREG(ui32Base + EMAC_O_ADDRH(ui32Index)) =
  1287. (HWREG(ui32Base + EMAC_O_ADDRH(ui32Index)) & 0xFFFF) | ui32Config;
  1288. //
  1289. // Read and rewrite the low half of the MAC address register to ensure
  1290. // that the upper half's data is latched.
  1291. //
  1292. ui32Val = HWREG(ui32Base + EMAC_O_ADDRL(ui32Index));
  1293. HWREG(ui32Base + EMAC_O_ADDRL(ui32Index)) = ui32Val;
  1294. }
  1295. //*****************************************************************************
  1296. //
  1297. //! Gets filtering parameters associated with one of the configured MAC
  1298. //! addresses.
  1299. //!
  1300. //! \param ui32Base is the base address of the controller.
  1301. //! \param ui32Index is the index of the MAC address slot for which the filter
  1302. //! is to be queried.
  1303. //!
  1304. //! This function returns filtering parameters associated with one of the MAC
  1305. //! address slots that the controller supports. This configuration is used
  1306. //! when perfect filtering (rather than hash table filtering) is selected.
  1307. //!
  1308. //! Valid values for \e ui32Index are from 1 to (number of MAC address
  1309. //! slots - 1). The number of supported MAC address slots may be found by
  1310. //! calling EMACNumAddrGet(). MAC index 0 is the local MAC address and does
  1311. //! not have filtering parameters associated with it.
  1312. //!
  1313. //! \return Returns the filter configuration as the logical OR of the
  1314. //! following labels:
  1315. //!
  1316. //! - \b EMAC_FILTER_ADDR_ENABLE indicates that this MAC address is enabled
  1317. //! and is used when performing perfect filtering. If this flag is absent,
  1318. //! the MAC address at the given index is disabled and is not used in
  1319. //! filtering.
  1320. //! - \b EMAC_FILTER_SOURCE_ADDR indicates that the MAC address at the given
  1321. //! index is compared to the source address of incoming frames while performing
  1322. //! perfect filtering. If absent, the MAC address is compared against the
  1323. //! destination address.
  1324. //! - \b EMAC_FILTER_MASK_BYTE_6 indicates that the MAC ignores the
  1325. //! sixth byte of the source or destination address when filtering.
  1326. //! - \b EMAC_FILTER_MASK_BYTE_5 indicates that the MAC ignores the
  1327. //! fifth byte of the source or destination address when filtering.
  1328. //! - \b EMAC_FILTER_MASK_BYTE_4 indicates that the MAC ignores the
  1329. //! fourth byte of the source or destination address when filtering.
  1330. //! - \b EMAC_FILTER_MASK_BYTE_3 indicates that the MAC ignores the
  1331. //! third byte of the source or destination address when filtering.
  1332. //! - \b EMAC_FILTER_MASK_BYTE_2 indicates that the MAC ignores the
  1333. //! second byte of the source or destination address when filtering.
  1334. //! - \b EMAC_FILTER_MASK_BYTE_1 indicates that the MAC ignores the
  1335. //! first byte of the source or destination address when filtering.
  1336. //
  1337. //*****************************************************************************
  1338. uint32_t
  1339. EMACAddrFilterGet(uint32_t ui32Base, uint32_t ui32Index)
  1340. {
  1341. //
  1342. // Parameter sanity check.
  1343. //
  1344. ASSERT(ui32Index < NUM_MAC_ADDR);
  1345. ASSERT(ui32Index);
  1346. //
  1347. // Read and return the filter settings for the requested MAC address slot.
  1348. //
  1349. return(HWREG(ui32Base + EMAC_O_ADDRH(ui32Index)) &
  1350. (EMAC_FILTER_BYTE_MASK_M | EMAC_FILTER_ADDR_ENABLE |
  1351. EMAC_FILTER_SOURCE_ADDR));
  1352. }
  1353. //*****************************************************************************
  1354. //
  1355. //! Sets options related to Ethernet frame filtering.
  1356. //!
  1357. //! \param ui32Base is the base address of the controller.
  1358. //! \param ui32FilterOpts is a logical OR of flags defining the required MAC
  1359. //! address filtering options.
  1360. //!
  1361. //! This function allows various filtering options to be defined and allows
  1362. //! an application to control which frames are received based on various
  1363. //! criteria related to the frame source and destination MAC addresses or VLAN
  1364. //! tagging.
  1365. //!
  1366. //! The \e ui32FilterOpts parameter is a logical OR of any of the following
  1367. //! flags:
  1368. //!
  1369. //! - \b EMAC_FRMFILTER_RX_ALL configures the MAC to pass all received frames
  1370. //! regardless of whether or not they pass any address filter that is
  1371. //! configured. The receive status word in the relevant DMA descriptor is
  1372. //! updated to indicate whether the configured filter passed or failed for
  1373. //! the frame.
  1374. //! - \b EMAC_FRMFILTER_VLAN configures the MAC to drop any frames that do
  1375. //! not pass the VLAN tag comparison.
  1376. //! - \b EMAC_FRMFILTER_HASH_AND_PERFECT configures the MAC to filter frames
  1377. //! based on both any perfect filters set and the hash filter if enabled using
  1378. //! \b EMAC_FRMFILTER_HASH_UNICAST or \b EMAC_FRMFILTER_HASH_MULTICAST. In
  1379. //! this case, only if both filters fail is the packet rejected. If this
  1380. //! option is absent, only one of the filter types is used, as controlled by
  1381. //! \b EMAC_FRMFILTER_HASH_UNICAST and \b EMAC_FRMFILTER_HASH_MULTICAST
  1382. //! for unicast and multicast frames respectively.
  1383. //! - \b EMAC_FRMFILTER_SADDR configures the MAC to drop received frames
  1384. //! when the source address field in the frame does not match the values
  1385. //! programmed into the enabled SA registers.
  1386. //! - \b EMAC_FRMFILTER_INV_SADDR enables inverse source address filtering.
  1387. //! When this option is specified, frames for which the SA does not match the
  1388. //! SA registers are marked as passing the source address filter.
  1389. //! - \b EMAC_FRMFILTER_BROADCAST configures the MAC to discard all incoming
  1390. //! broadcast frames.
  1391. //! - \b EMAC_FRMFILTER_PASS_MULTICAST configures the MAC to pass all
  1392. //! incoming frames with multicast destinations addresses.
  1393. //! - \b EMAC_FRMFILTER_INV_DADDR inverts the sense of the destination
  1394. //! address filtering for both unicast and multicast frames.
  1395. //! - \b EMAC_FRMFILTER_HASH_MULTICAST enables destination address filtering
  1396. //! of received multicast frames using the hash table. If absent, perfect
  1397. //! destination address filtering is used. If used in conjunction with \b
  1398. //! EMAC_FRMFILTER_HASH_AND_PERFECT, this flag indicates that the hash filter
  1399. //! should be used for incoming multicast packets along with the perfect
  1400. //! filter.
  1401. //! - \b EMAC_FRMFILTER_HASH_UNICAST enables destination address filtering
  1402. //! of received unicast frames using the hash table. If absent, perfect
  1403. //! destination address filtering is used. If used in conjunction with \b
  1404. //! EMAC_FRMFILTER_HASH_AND_PERFECT, this flag indicates that the hash filter
  1405. //! should be used for incoming unicast packets along with the perfect filter.
  1406. //! - \b EMAC_FRMFILTER_PROMISCUOUS configures the MAC to operate in
  1407. //! promiscuous mode where all received frames are passed to the application
  1408. //! and the SA and DA filter status bits of the descriptor receive status word
  1409. //! are always cleared.
  1410. //!
  1411. //! Control frame filtering may be configured by ORing one of the following
  1412. //! values into \e ui32FilterOpts:
  1413. //!
  1414. //! - \b EMAC_FRMFILTER_PASS_NO_CTRL prevents any control frame from reaching
  1415. //! the application.
  1416. //! - \b EMAC_FRMFILTER_PASS_NO_PAUSE passes all control frames other than
  1417. //! PAUSE even if they fail the configured address filter.
  1418. //! - \b EMAC_FRMFILTER_PASS_ALL_CTRL passes all control frames, including
  1419. //! PAUSE even if they fail the configured address filter.
  1420. //! - \b EMAC_FRMFILTER_PASS_ADDR_CTRL passes all control frames only if they
  1421. //! pass the configured address filter.
  1422. //!
  1423. //! \return None.
  1424. //
  1425. //*****************************************************************************
  1426. void
  1427. EMACFrameFilterSet(uint32_t ui32Base, uint32_t ui32FilterOpts)
  1428. {
  1429. ASSERT((ui32FilterOpts & ~VALID_FRMFILTER_FLAGS) == 0);
  1430. //
  1431. // Set the Ethernet MAC frame filter according to the flags passed.
  1432. //
  1433. HWREG(ui32Base + EMAC_O_FRAMEFLTR) =
  1434. ((HWREG(ui32Base + EMAC_O_FRAMEFLTR) & ~VALID_FRMFILTER_FLAGS) |
  1435. ui32FilterOpts);
  1436. }
  1437. //*****************************************************************************
  1438. //
  1439. //! Returns the current Ethernet frame filtering settings.
  1440. //!
  1441. //! \param ui32Base is the base address of the controller.
  1442. //!
  1443. //! This function may be called to retrieve the frame filtering configuration
  1444. //! set using a prior call to EMACFrameFilterSet().
  1445. //!
  1446. //! \return Returns a value comprising the logical OR of various flags
  1447. //! indicating the frame filtering options in use. Possible flags are:
  1448. //!
  1449. //! - \b EMAC_FRMFILTER_RX_ALL indicates that the MAC to is configured to
  1450. //! pass all received frames regardless of whether or not they pass any
  1451. //! address filter that is configured. The receive status word in the
  1452. //! relevant DMA descriptor is updated to indicate whether the configured
  1453. //! filter passed or failed for the frame.
  1454. //! - \b EMAC_FRMFILTER_VLAN indicates that the MAC is configured to drop any
  1455. //! frames which do not pass the VLAN tag comparison.
  1456. //! - \b EMAC_FRMFILTER_HASH_AND_PERFECT indicates that the MAC is configured
  1457. //! to pass frames if they match either the hash filter or the perfect filter.
  1458. //! If this flag is absent, frames passing based on the result of a single
  1459. //! filter, the perfect filter if \b EMAC_FRMFILTER_HASH_MULTICAST or
  1460. //! \b EMAC_FRMFILTER_HASH_UNICAST are clear or the hash filter otherwise.
  1461. //! - \b EMAC_FRMFILTER_SADDR indicates that the MAC is configured to drop
  1462. //! received frames when the source address field in the frame does not match
  1463. //! the values programmed into the enabled SA registers.
  1464. //! - \b EMAC_FRMFILTER_INV_SADDR enables inverse source address filtering.
  1465. //! When this option is specified, frames for which the SA does not match the
  1466. //! SA registers are marked as passing the source address filter.
  1467. //! - \b EMAC_FRMFILTER_BROADCAST indicates that the MAC is configured to
  1468. //! discard all incoming broadcast frames.
  1469. //! - \b EMAC_FRMFILTER_PASS_MULTICAST indicates that the MAC is configured
  1470. //! to pass all incoming frames with multicast destinations addresses.
  1471. //! - \b EMAC_FRMFILTER_INV_DADDR indicates that the sense of the destination
  1472. //! address filtering for both unicast and multicast frames is inverted.
  1473. //! - \b EMAC_FRMFILTER_HASH_MULTICAST indicates that destination address
  1474. //! filtering of received multicast frames is enabled using the hash table. If
  1475. //! absent, perfect destination address filtering is used. If used in
  1476. //! conjunction with \b EMAC_FRMFILTER_HASH_AND_PERFECT, this flag indicates
  1477. //! that the hash filter should be used for incoming multicast packets along
  1478. //! with the perfect filter.
  1479. //! - \b EMAC_FRMFILTER_HASH_UNICAST indicates that destination address
  1480. //! filtering of received unicast frames is enabled using the hash table. If
  1481. //! absent, perfect destination address filtering is used. If used in
  1482. //! conjunction with \b EMAC_FRMFILTER_HASH_AND_PERFECT, this flag indicates
  1483. //! that the hash filter should be used for incoming unicast packets along with
  1484. //! the perfect filter.
  1485. //! - \b EMAC_FRMFILTER_PROMISCUOUS indicates that the MAC is configured to
  1486. //! operate in promiscuous mode where all received frames are passed to the
  1487. //! application and the SA and DA filter status bits of the descriptor receive
  1488. //! status word are always cleared.
  1489. //!
  1490. //! Control frame filtering configuration is indicated by one of the following
  1491. //! values which may be extracted from the returned value using the mask
  1492. //! \b EMAC_FRMFILTER_PASS_MASK:
  1493. //!
  1494. //! - \b EMAC_FRMFILTER_PASS_NO_CTRL prevents any control frame from reaching
  1495. //! the application.
  1496. //! - \b EMAC_FRMFILTER_PASS_NO_PAUSE passes all control frames other than
  1497. //! PAUSE even if they fail the configured address filter.
  1498. //! - \b EMAC_FRMFILTER_PASS_ALL_CTRL passes all control frames, including
  1499. //! PAUSE even if they fail the configured address filter.
  1500. //! - \b EMAC_FRMFILTER_PASS_ADDR_CTRL passes all control frames only if they
  1501. //! pass the configured address filter.
  1502. //
  1503. //*****************************************************************************
  1504. uint32_t
  1505. EMACFrameFilterGet(uint32_t ui32Base)
  1506. {
  1507. //
  1508. // Return the current MAC frame filter setting.
  1509. //
  1510. return(HWREG(ui32Base + EMAC_O_FRAMEFLTR) & VALID_FRMFILTER_FLAGS);
  1511. }
  1512. //*****************************************************************************
  1513. //
  1514. //! Sets the MAC address hash filter table.
  1515. //!
  1516. //! \param ui32Base is the base address of the controller.
  1517. //! \param ui32HashHi is the upper 32 bits of the current 64-bit hash filter
  1518. //! table to set.
  1519. //! \param ui32HashLo is the lower 32 bits of the current 64-bit hash filter
  1520. //! table to set.
  1521. //!
  1522. //! This function may be used to set the current 64-bit hash filter table
  1523. //! used by the MAC to filter incoming packets when hash filtering is enabled.
  1524. //! Hash filtering is enabled by passing \b EMAC_FRMFILTER_HASH_UNICAST
  1525. //! and/or \b EMAC_FRMFILTER_HASH_MULTICAST in the \e ui32FilterOpts parameter
  1526. //! to EMACFrameFilterSet(). The current hash filter may be retrieved
  1527. //! by calling EMACHashFilterGet().
  1528. //!
  1529. //! Hash table filtering allows many different MAC addresses to be filtered
  1530. //! simultaneously at the cost of some false-positive results (in the form of
  1531. //! packets passing the filter when their MAC address was not one of those
  1532. //! required). A CRC of the packet source or destination MAC address is
  1533. //! calculated and the bottom 6 bits are used as a bit index into the 64-bit
  1534. //! hash filter table. If the bit in the hash table is set, the filter is
  1535. //! considered to have passed. If the bit is clear, the filter fails and the
  1536. //! packet is rejected (assuming normal rather than inverse filtering is
  1537. //! configured).
  1538. //!
  1539. //! \return None.
  1540. //
  1541. //*****************************************************************************
  1542. void
  1543. EMACHashFilterSet(uint32_t ui32Base, uint32_t ui32HashHi, uint32_t ui32HashLo)
  1544. {
  1545. // Set the hash table with the values provided.
  1546. HWREG(ui32Base + EMAC_O_HASHTBLL) = ui32HashLo;
  1547. HWREG(ui32Base + EMAC_O_HASHTBLH) = ui32HashHi;
  1548. }
  1549. //*****************************************************************************
  1550. //
  1551. //! Returns the current MAC address hash filter table.
  1552. //!
  1553. //! \param ui32Base is the base address of the controller.
  1554. //! \param pui32HashHi points to storage to be written with the upper 32 bits
  1555. //! of the current 64-bit hash filter table.
  1556. //! \param pui32HashLo points to storage to be written with the lower 32 bits
  1557. //! of the current 64-bit hash filter table.
  1558. //!
  1559. //! This function may be used to retrieve the current 64-bit hash filter table
  1560. //! from the MAC prior to making changes and setting the new hash filter via a
  1561. //! call to EMACHashFilterSet().
  1562. //!
  1563. //! Hash table filtering allows many different MAC addresses to be filtered
  1564. //! simultaneously at the cost of some false-positive results in the form of
  1565. //! packets passing the filter when their MAC address was not one of those
  1566. //! required. A CRC of the packet source or destination MAC address is
  1567. //! calculated and the bottom 6 bits are used as a bit index into the 64-bit
  1568. //! hash filter table. If the bit in the hash table is set, the filter is
  1569. //! considered to have passed. If the bit is clear, the filter fails and the
  1570. //! packet is rejected (assuming normal rather than inverse filtering is
  1571. //! configured).
  1572. //!
  1573. //! \return None.
  1574. //
  1575. //*****************************************************************************
  1576. void
  1577. EMACHashFilterGet(uint32_t ui32Base, uint32_t *pui32HashHi,
  1578. uint32_t *pui32HashLo)
  1579. {
  1580. ASSERT(pui32HashHi);
  1581. ASSERT(pui32HashLo);
  1582. //
  1583. // Get the current hash table values.
  1584. //
  1585. *pui32HashLo = HWREG(ui32Base + EMAC_O_HASHTBLL);
  1586. *pui32HashHi = HWREG(ui32Base + EMAC_O_HASHTBLH);
  1587. }
  1588. //*****************************************************************************
  1589. //
  1590. //! Returns the bit number to set in the MAC hash filter corresponding to a
  1591. //! given MAC address.
  1592. //!
  1593. //! \param pui8MACAddr points to a buffer containing the 6-byte MAC address
  1594. //! for which the hash filter bit is to be determined.
  1595. //!
  1596. //! This function may be used to determine which bit in the MAC address hash
  1597. //! filter to set to describe a given 6-byte MAC address. The returned value is
  1598. //! a 6-bit number where bit 5 indicates which of the two hash table words is
  1599. //! affected and the bottom 5 bits indicate the bit number to set within that
  1600. //! word. For example, if 0x22 (100010b) is returned, this indicates that bit
  1601. //! 2 of word 1 (\e ui32HashHi as passed to EMACHashFilterSet()) must be set
  1602. //! to describe the passed MAC address.
  1603. //!
  1604. //! \return Returns the bit number to set in the MAC hash table to describe the
  1605. //! passed MAC address.
  1606. //
  1607. //*****************************************************************************
  1608. uint32_t
  1609. EMACHashFilterBitCalculate(uint8_t *pui8MACAddr)
  1610. {
  1611. uint32_t ui32CRC, ui32Mask, ui32Loop;
  1612. //
  1613. // Parameter sanity check.
  1614. //
  1615. ASSERT(pui8MACAddr);
  1616. //
  1617. // Calculate the CRC for the MAC address.
  1618. //
  1619. ui32CRC = Crc32(0xFFFFFFFF, pui8MACAddr, 6);
  1620. ui32CRC ^= 0xFFFFFFFF;
  1621. //
  1622. // Determine the hash bit to use from the calculated CRC. This is the
  1623. // top 6 bits of the reversed CRC (or the bottom 6 bits of the calculated
  1624. // CRC with the bit order of those 6 bits reversed).
  1625. //
  1626. ui32Mask = 0;
  1627. //
  1628. // Reverse the order of the bottom 6 bits of the calculated CRC.
  1629. //
  1630. for(ui32Loop = 0; ui32Loop < 6; ui32Loop++)
  1631. {
  1632. ui32Mask <<= 1;
  1633. ui32Mask |= (ui32CRC & 1);
  1634. ui32CRC >>= 1;
  1635. }
  1636. //
  1637. // Return the final hash table bit index.
  1638. //
  1639. return(ui32Mask);
  1640. }
  1641. //*****************************************************************************
  1642. //
  1643. //! Sets the receive interrupt watchdog timer period.
  1644. //!
  1645. //! \param ui32Base is the base address of the Ethernet controller.
  1646. //! \param ui8Timeout is the desired timeout expressed as a number of 256
  1647. //! system clock periods.
  1648. //!
  1649. //! This function configures the receive interrupt watchdog timer.
  1650. //! The \e uiTimeout parameter specifies the number of 256 system clock periods
  1651. //! that elapse before the timer expires. In cases where the DMA has
  1652. //! transferred a frame using a descriptor that has
  1653. //! \b DES1_RX_CTRL_DISABLE_INT set, the watchdog causes a receive
  1654. //! interrupt to be generated when it times out. The watchdog timer is reset
  1655. //! whenever a packet is transferred to memory using a DMA descriptor that
  1656. //! does not disable the receive interrupt.
  1657. //!
  1658. //! To disable the receive interrupt watchdog function, set \e ui8Timeout to 0.
  1659. //!
  1660. //! \return None.
  1661. //
  1662. //*****************************************************************************
  1663. void
  1664. EMACRxWatchdogTimerSet(uint32_t ui32Base, uint8_t ui8Timeout)
  1665. {
  1666. //
  1667. // Set the receive interrupt watchdog timeout period.
  1668. //
  1669. HWREG(ui32Base + EMAC_O_RXINTWDT) = (uint32_t)ui8Timeout;
  1670. }
  1671. //*****************************************************************************
  1672. //
  1673. //! Returns the current Ethernet MAC status.
  1674. //!
  1675. //! \param ui32Base is the base address of the Ethernet controller.
  1676. //!
  1677. //! This function returns information on the current status of all the main
  1678. //! modules in the MAC transmit and receive data paths.
  1679. //!
  1680. //! \return Returns the current MAC status as a logical OR of any of the
  1681. //! following flags:
  1682. //!
  1683. //! - \b EMAC_STATUS_TX_NOT_EMPTY
  1684. //! - \b EMAC_STATUS_TX_WRITING_FIFO
  1685. //! - \b EMAC_STATUS_TX_PAUSED
  1686. //! - \b EMAC_STATUS_MAC_NOT_IDLE
  1687. //! - \b EMAC_STATUS_RWC_ACTIVE
  1688. //! - \b EMAC_STATUS_RPE_ACTIVE
  1689. //!
  1690. //! The transmit frame controller status can be extracted from the returned
  1691. //! value by ANDing with \b EMAC_STATUS_TFC_STATE_MASK and is one of the
  1692. //! following:
  1693. //!
  1694. //! - \b EMAC_STATUS_TFC_STATE_IDLE
  1695. //! - \b EMAC_STATUS_TFC_STATE_WAITING
  1696. //! - \b EMAC_STATUS_TFC_STATE_PAUSING
  1697. //! - \b EMAC_STATUS_TFC_STATE_WRITING
  1698. //!
  1699. //! The transmit FIFO read controller status can be extracted from the returned
  1700. //! value by ANDing with \b EMAC_STATUS_TRC_STATE_MASK and is one of the
  1701. //! following:
  1702. //!
  1703. //! - \b EMAC_STATUS_TRC_STATE_IDLE
  1704. //! - \b EMAC_STATUS_TRC_STATE_READING
  1705. //! - \b EMAC_STATUS_TRC_STATE_WAITING
  1706. //! - \b EMAC_STATUS_TRC_STATE_STATUS
  1707. //!
  1708. //! The current receive FIFO levels can be extracted from the returned value
  1709. //! by ANDing with \b EMAC_STATUS_RX_FIFO_LEVEL_MASK and is one of the
  1710. //! following:
  1711. //!
  1712. //! - \b EMAC_STATUS_RX_FIFO_EMPTY indicating that the FIFO is empty.
  1713. //! - \b EMAC_STATUS_RX_FIFO_BELOW indicating that the FIFO fill level is
  1714. //! below the flow-control deactivate threshold.
  1715. //! - \b EMAC_STATUS_RX_FIFO_ABOVE indicating that the FIFO fill level is
  1716. //! above the flow-control activate threshold.
  1717. //! - \b EMAC_STATUS_RX_FIFO_FULL indicating that the FIFO is full.
  1718. //!
  1719. //! The current receive FIFO state can be extracted from the returned value
  1720. //! by ANDing with \b EMAC_STATUS_RX_FIFO_STATE_MASK and is one of the
  1721. //! following:
  1722. //!
  1723. //! - \b EMAC_STATUS_RX_FIFO_IDLE
  1724. //! - \b EMAC_STATUS_RX_FIFO_READING
  1725. //! - \b EMAC_STATUS_RX_FIFO_STATUS
  1726. //! - \b EMAC_STATUS_RX_FIFO_FLUSHING
  1727. //
  1728. //*****************************************************************************
  1729. uint32_t
  1730. EMACStatusGet(uint32_t ui32Base)
  1731. {
  1732. //
  1733. // Read and return the MAC status register content.
  1734. //
  1735. return(HWREG(ui32Base + EMAC_O_STATUS));
  1736. }
  1737. //*****************************************************************************
  1738. //
  1739. //! Orders the MAC DMA controller to attempt to acquire the next transmit
  1740. //! descriptor.
  1741. //!
  1742. //! \param ui32Base is the base address of the Ethernet controller.
  1743. //!
  1744. //! This function must be called to restart the transmitter if it has been
  1745. //! suspended due to the current transmit DMA descriptor being owned by the
  1746. //! host. Once the application writes new values to the descriptor and marks
  1747. //! it as being owned by the MAC DMA, this function causes the hardware to
  1748. //! attempt to acquire the descriptor and start transmission of the new
  1749. //! data.
  1750. //!
  1751. //! \return None.
  1752. //
  1753. //*****************************************************************************
  1754. void
  1755. EMACTxDMAPollDemand(uint32_t ui32Base)
  1756. {
  1757. //
  1758. // Any write to the MACTXPOLLD register causes the transmit DMA to attempt
  1759. // to resume.
  1760. //
  1761. HWREG(ui32Base + EMAC_O_TXPOLLD) = 0;
  1762. }
  1763. //*****************************************************************************
  1764. //
  1765. //! Orders the MAC DMA controller to attempt to acquire the next receive
  1766. //! descriptor.
  1767. //!
  1768. //! \param ui32Base is the base address of the Ethernet controller.
  1769. //!
  1770. //! This function must be called to restart the receiver if it has been
  1771. //! suspended due to the current receive DMA descriptor being owned by the
  1772. //! host. Once the application reads any data from the descriptor and marks
  1773. //! it as being owned by the MAC DMA, this function causes the hardware to
  1774. //! attempt to acquire the descriptor before writing the next received packet
  1775. //! into its buffer(s).
  1776. //!
  1777. //! \return None.
  1778. //
  1779. //*****************************************************************************
  1780. void
  1781. EMACRxDMAPollDemand(uint32_t ui32Base)
  1782. {
  1783. //
  1784. // Any write to the MACRXPOLLD register causes the receive DMA to attempt
  1785. // to resume.
  1786. //
  1787. HWREG(ui32Base + EMAC_O_RXPOLLD) = 0;
  1788. }
  1789. //*****************************************************************************
  1790. //
  1791. //! Sets the DMA receive descriptor list pointer.
  1792. //!
  1793. //! \param ui32Base is the base address of the controller.
  1794. //! \param pDescriptor points to the first DMA descriptor in the list to
  1795. //! be passed to the receive DMA engine.
  1796. //!
  1797. //! This function sets the Ethernet MAC's receive DMA descriptor list pointer.
  1798. //! The \e pDescriptor pointer must point to one or more descriptor
  1799. //! structures.
  1800. //!
  1801. //! When multiple descriptors are provided, they can be either chained or
  1802. //! unchained. Chained descriptors are indicated by setting the
  1803. //! \b DES0_TX_CTRL_CHAINED or \b DES1_RX_CTRL_CHAINED bit in the relevant
  1804. //! word of the transmit or receive descriptor. If this bit is clear,
  1805. //! unchained descriptors are assumed.
  1806. //!
  1807. //! Chained descriptors use a link pointer in each descriptor to
  1808. //! point to the next descriptor in the chain.
  1809. //!
  1810. //! Unchained descriptors are assumed to be contiguous in memory with a
  1811. //! consistent offset between the start of one descriptor and the next.
  1812. //! If unchained descriptors are used, the \e pvLink field in the descriptor
  1813. //! becomes available to store a second buffer pointer, allowing each
  1814. //! descriptor to point to two buffers rather than one. In this case,
  1815. //! the \e ui32DescSkipSize parameter to EMACInit() must previously have
  1816. //! been set to the number of words between the end of one descriptor and
  1817. //! the start of the next. This value must be 0 in cases where a packed array
  1818. //! of \b tEMACDMADescriptor structures is used. If the application wishes to
  1819. //! add new state fields to the end of the descriptor structure, the skip size
  1820. //! should be set to accommodate the newly sized structure.
  1821. //!
  1822. //! Applications are responsible for initializing all descriptor fields
  1823. //! appropriately before passing the descriptor list to the hardware.
  1824. //!
  1825. //! \return None.
  1826. //
  1827. //*****************************************************************************
  1828. void
  1829. EMACRxDMADescriptorListSet(uint32_t ui32Base, tEMACDMADescriptor *pDescriptor)
  1830. {
  1831. //
  1832. // Parameter sanity check.
  1833. //
  1834. ASSERT(pDescriptor);
  1835. ASSERT(((uint32_t)pDescriptor & 3) == 0);
  1836. //
  1837. // Write the supplied address to the MACRXDLADDR register.
  1838. //
  1839. HWREG(ui32Base + EMAC_O_RXDLADDR) = (uint32_t)pDescriptor;
  1840. }
  1841. //*****************************************************************************
  1842. //
  1843. //! Returns a pointer to the start of the DMA receive descriptor list.
  1844. //!
  1845. //! \param ui32Base is the base address of the controller.
  1846. //!
  1847. //! This function returns a pointer to the head of the Ethernet MAC's receive
  1848. //! DMA descriptor list. This value corresponds to the pointer originally set
  1849. //! using a call to EMACRxDMADescriptorListSet().
  1850. //!
  1851. //! \return Returns a pointer to the start of the DMA receive descriptor list.
  1852. //
  1853. //*****************************************************************************
  1854. tEMACDMADescriptor *
  1855. EMACRxDMADescriptorListGet(uint32_t ui32Base)
  1856. {
  1857. //
  1858. // Return the current receive DMA descriptor list pointer.
  1859. //
  1860. return((tEMACDMADescriptor *)HWREG(ui32Base + EMAC_O_RXDLADDR));
  1861. }
  1862. //*****************************************************************************
  1863. //
  1864. //! Returns the current DMA receive descriptor pointer.
  1865. //!
  1866. //! \param ui32Base is the base address of the controller.
  1867. //!
  1868. //! This function returns a pointer to the current Ethernet receive descriptor
  1869. //! read by the DMA.
  1870. //!
  1871. //! \return Returns a pointer to the start of the current receive DMA
  1872. //! descriptor.
  1873. //
  1874. //*****************************************************************************
  1875. tEMACDMADescriptor *
  1876. EMACRxDMACurrentDescriptorGet(uint32_t ui32Base)
  1877. {
  1878. //
  1879. // Return the address of the current receive descriptor written by the DMA.
  1880. //
  1881. return((tEMACDMADescriptor *)HWREG(ui32Base + EMAC_O_HOSRXDESC));
  1882. }
  1883. //*****************************************************************************
  1884. //
  1885. //! Returns the current DMA receive buffer pointer.
  1886. //!
  1887. //! \param ui32Base is the base address of the controller.
  1888. //!
  1889. //! This function may be called to determine which buffer the receive DMA
  1890. //! engine is currently writing to.
  1891. //!
  1892. //! \return Returns the receive buffer address currently being written by
  1893. //! the DMA engine.
  1894. //
  1895. //*****************************************************************************
  1896. uint8_t *
  1897. EMACRxDMACurrentBufferGet(uint32_t ui32Base)
  1898. {
  1899. //
  1900. // Return the receive buffer address currently being written by the DMA.
  1901. //
  1902. return((uint8_t *)HWREG(ui32Base + EMAC_O_HOSRXBA));
  1903. }
  1904. //*****************************************************************************
  1905. //
  1906. //! Sets the DMA transmit descriptor list pointer.
  1907. //!
  1908. //! \param ui32Base is the base address of the controller.
  1909. //! \param pDescriptor points to the first DMA descriptor in the list to
  1910. //! be passed to the transmit DMA engine.
  1911. //!
  1912. //! This function sets the Ethernet MAC's transmit DMA descriptor list pointer.
  1913. //! The \e pDescriptor pointer must point to one or more descriptor
  1914. //! structures.
  1915. //!
  1916. //! When multiple descriptors are provided, they can be either chained or
  1917. //! unchained. Chained descriptors are indicated by setting the
  1918. //! \b DES0_TX_CTRL_CHAINED or \b DES1_RX_CTRL_CHAINED bit in the relevant
  1919. //! word of the transmit or receive descriptor. If this bit is clear,
  1920. //! unchained descriptors are assumed.
  1921. //!
  1922. //! Chained descriptors use a link pointer in each descriptor to
  1923. //! point to the next descriptor in the chain.
  1924. //!
  1925. //! Unchained descriptors are assumed to be contiguous in memory with a
  1926. //! consistent offset between the start of one descriptor and the next.
  1927. //! If unchained descriptors are used, the \e pvLink field in the descriptor
  1928. //! becomes available to store a second buffer pointer, allowing each
  1929. //! descriptor to point to two buffers rather than one. In this case,
  1930. //! the \e ui32DescSkipSize parameter to EMACInit() must previously have
  1931. //! been set to the number of words between the end of one descriptor and
  1932. //! the start of the next. This value must be 0 in cases where a packed array
  1933. //! of \b tEMACDMADescriptor structures is used. If the application wishes to
  1934. //! add new state fields to the end of the descriptor structure, the skip size
  1935. //! should be set to accommodate the newly sized structure.
  1936. //!
  1937. //! Applications are responsible for initializing all descriptor fields
  1938. //! appropriately before passing the descriptor list to the hardware.
  1939. //!
  1940. //! \return None.
  1941. //
  1942. //*****************************************************************************
  1943. void
  1944. EMACTxDMADescriptorListSet(uint32_t ui32Base, tEMACDMADescriptor *pDescriptor)
  1945. {
  1946. //
  1947. // Parameter sanity check.
  1948. //
  1949. ASSERT(pDescriptor);
  1950. ASSERT(((uint32_t)pDescriptor & 3) == 0);
  1951. //
  1952. // Write the supplied address to the MACTXDLADDR register.
  1953. //
  1954. HWREG(ui32Base + EMAC_O_TXDLADDR) = (uint32_t)pDescriptor;
  1955. }
  1956. //*****************************************************************************
  1957. //
  1958. //! Returns a pointer to the start of the DMA transmit descriptor list.
  1959. //!
  1960. //! \param ui32Base is the base address of the controller.
  1961. //!
  1962. //! This function returns a pointer to the head of the Ethernet MAC's transmit
  1963. //! DMA descriptor list. This value corresponds to the pointer originally set
  1964. //! using a call to EMACTxDMADescriptorListSet().
  1965. //!
  1966. //! \return Returns a pointer to the start of the DMA transmit descriptor list.
  1967. //
  1968. //*****************************************************************************
  1969. tEMACDMADescriptor *
  1970. EMACTxDMADescriptorListGet(uint32_t ui32Base)
  1971. {
  1972. //
  1973. // Return the current transmit DMA descriptor list pointer.
  1974. //
  1975. return((tEMACDMADescriptor *)HWREG(ui32Base + EMAC_O_TXDLADDR));
  1976. }
  1977. //*****************************************************************************
  1978. //
  1979. //! Returns the current DMA transmit descriptor pointer.
  1980. //!
  1981. //! \param ui32Base is the base address of the controller.
  1982. //!
  1983. //! This function returns a pointer to the current Ethernet transmit descriptor
  1984. //! read by the DMA.
  1985. //!
  1986. //! \return Returns a pointer to the start of the current transmit DMA
  1987. //! descriptor.
  1988. //
  1989. //*****************************************************************************
  1990. tEMACDMADescriptor *
  1991. EMACTxDMACurrentDescriptorGet(uint32_t ui32Base)
  1992. {
  1993. //
  1994. // Return the address of the current transmit descriptor read by the DMA.
  1995. //
  1996. return((tEMACDMADescriptor *)HWREG(ui32Base + EMAC_O_HOSTXDESC));
  1997. }
  1998. //*****************************************************************************
  1999. //
  2000. //! Returns the current DMA transmit buffer pointer.
  2001. //!
  2002. //! \param ui32Base is the base address of the controller.
  2003. //!
  2004. //! This function may be called to determine which buffer the transmit DMA
  2005. //! engine is currently reading from.
  2006. //!
  2007. //! \return Returns the transmit buffer address currently being read by the
  2008. //! DMA engine.
  2009. //
  2010. //*****************************************************************************
  2011. uint8_t *
  2012. EMACTxDMACurrentBufferGet(uint32_t ui32Base)
  2013. {
  2014. //
  2015. // Return the transmit buffer address currently being read by the DMA.
  2016. //
  2017. return((uint8_t *)HWREG(ui32Base + EMAC_O_HOSTXBA));
  2018. }
  2019. //*****************************************************************************
  2020. //
  2021. //! Returns the current states of the Ethernet MAC transmit and receive DMA
  2022. //! engines.
  2023. //!
  2024. //! \param ui32Base is the base address of the controller.
  2025. //!
  2026. //! This function may be used to query the current states of the transmit and
  2027. //! receive DMA engines. The return value contains two fields, one providing
  2028. //! the transmit state and the other the receive state. Macros
  2029. //! \b EMAC_TX_DMA_STATE() and \b EMAC_RX_DMA_STATE() may be used to
  2030. //! extract these fields from the returned value. Alternatively, masks
  2031. //! \b EMAC_DMA_TXSTAT_MASK and \b EMAC_DMA_RXSTAT_MASK may be used
  2032. //! directly to mask out the individual states from the returned value.
  2033. //!
  2034. //! \return Returns the states of the transmit and receive DMA engines. These
  2035. //! states are ORed together into a single word containing one of:
  2036. //!
  2037. //! - \b EMAC_DMA_TXSTAT_STOPPED indicating that the transmit engine is
  2038. //! stopped.
  2039. //! - \b EMAC_DMA_TXSTAT_RUN_FETCH_DESC indicating that the transmit engine
  2040. //! is fetching the next descriptor.
  2041. //! - \b EMAC_DMA_TXSTAT_RUN_WAIT_STATUS indicating that the transmit engine
  2042. //! is waiting for status from the MAC.
  2043. //! - \b EMAC_DMA_TXSTAT_RUN_READING indicating that the transmit engine is
  2044. //! currently transferring data from memory to the MAC transmit FIFO.
  2045. //! - \b EMAC_DMA_TXSTAT_RUN_CLOSE_DESC indicating that the transmit engine
  2046. //! is closing the descriptor after transmission of the buffer data.
  2047. //! - \b EMAC_DMA_TXSTAT_TS_WRITE indicating that the transmit engine is
  2048. //! currently writing timestamp information to the descriptor.
  2049. //! - \b EMAC_DMA_TXSTAT_SUSPENDED indicating that the transmit engine is
  2050. //! suspended due to the next descriptor being unavailable (owned by the host)
  2051. //! or a transmit buffer underflow.
  2052. //!
  2053. //! and one of:
  2054. //!
  2055. //! - \b EMAC_DMA_RXSTAT_STOPPED indicating that the receive engine is
  2056. //! stopped.
  2057. //! - \b EMAC_DMA_RXSTAT_RUN_FETCH_DESC indicating that the receive engine
  2058. //! is fetching the next descriptor.
  2059. //! - \b EMAC_DMA_RXSTAT_RUN_WAIT_PACKET indicating that the receive engine
  2060. //! is waiting for the next packet.
  2061. //! - \b EMAC_DMA_RXSTAT_SUSPENDED indicating that the receive engine is
  2062. //! suspended due to the next descriptor being unavailable.
  2063. //! - \b EMAC_DMA_RXSTAT_RUN_CLOSE_DESC indicating that the receive engine
  2064. //! is closing the descriptor after receiving a buffer of data.
  2065. //! - \b EMAC_DMA_RXSTAT_TS_WRITE indicating that the transmit engine is
  2066. //! currently writing timestamp information to the descriptor.
  2067. //! - \b EMAC_DMA_RXSTAT_RUN_RECEIVING indicating that the receive engine is
  2068. //! currently transferring data from the MAC receive FIFO to memory.
  2069. //!
  2070. //! Additionally, a DMA bus error may be signaled using \b EMAC_DMA_ERROR.
  2071. //! If this flag is present, the source of the error is identified using one
  2072. //! of the following values which may be extracted from the return value using
  2073. //! \b EMAC_DMA_ERR_MASK:
  2074. //!
  2075. //! - \b EMAC_DMA_ERR_RX_DATA_WRITE indicates that an error occurred when
  2076. //! writing received data to memory.
  2077. //! - \b EMAC_DMA_ERR_TX_DATA_READ indicates that an error occurred when
  2078. //! reading data from memory for transmission.
  2079. //! - \b EMAC_DMA_ERR_RX_DESC_WRITE indicates that an error occurred when
  2080. //! writing to the receive descriptor.
  2081. //! - \b EMAC_DMA_ERR_TX_DESC_WRITE indicates that an error occurred when
  2082. //! writing to the transmit descriptor.
  2083. //! - \b EMAC_DMA_ERR_RX_DESC_READ indicates that an error occurred when
  2084. //! reading the receive descriptor.
  2085. //! - \b EMAC_DMA_ERR_TX_DESC_READ indicates that an error occurred when
  2086. //! reading the transmit descriptor.
  2087. //
  2088. //*****************************************************************************
  2089. uint32_t
  2090. EMACDMAStateGet(uint32_t ui32Base)
  2091. {
  2092. //
  2093. // Return the status of the DMA channels.
  2094. //
  2095. return(HWREG(ui32Base + EMAC_O_DMARIS) &
  2096. (EMAC_DMARIS_FBI | EMAC_DMARIS_AE_M | EMAC_DMARIS_RS_M |
  2097. EMAC_DMARIS_TS_M));
  2098. }
  2099. //*****************************************************************************
  2100. //
  2101. //! Flushes the Ethernet controller transmit FIFO.
  2102. //!
  2103. //! \param ui32Base is the base address of the controller.
  2104. //!
  2105. //! This function flushes any data currently held in the Ethernet transmit
  2106. //! FIFO. Data that has already been passed to the MAC for transmission is
  2107. //! transmitted, possibly resulting in a transmit underflow or runt frame
  2108. //! transmission.
  2109. //!
  2110. //! \return None.
  2111. //
  2112. //*****************************************************************************
  2113. void
  2114. EMACTxFlush(uint32_t ui32Base)
  2115. {
  2116. //
  2117. // Check to make sure that the FIFO is not already empty.
  2118. //
  2119. if(HWREG(ui32Base + EMAC_O_STATUS) & EMAC_STATUS_TXFE)
  2120. {
  2121. //
  2122. // Flush the transmit FIFO since it is not currently empty.
  2123. //
  2124. HWREG(ui32Base + EMAC_O_DMAOPMODE) |= EMAC_DMAOPMODE_FTF;
  2125. //
  2126. // Wait for the flush to complete.
  2127. //
  2128. while(HWREG(ui32Base + EMAC_O_DMAOPMODE) & EMAC_DMAOPMODE_FTF)
  2129. {
  2130. }
  2131. }
  2132. }
  2133. //*****************************************************************************
  2134. //
  2135. //! Enables the Ethernet controller transmitter.
  2136. //!
  2137. //! \param ui32Base is the base address of the controller.
  2138. //!
  2139. //! When starting operations on the Ethernet interface, this function should
  2140. //! be called to enable the transmitter after all configuration has been
  2141. //! completed.
  2142. //!
  2143. //! \return None.
  2144. //
  2145. //*****************************************************************************
  2146. void
  2147. EMACTxEnable(uint32_t ui32Base)
  2148. {
  2149. //
  2150. // Enable the MAC transmit path in the opmode register.
  2151. //
  2152. HWREG(ui32Base + EMAC_O_DMAOPMODE) |= EMAC_DMAOPMODE_ST;
  2153. //
  2154. // Enable transmission in the MAC configuration register.
  2155. //
  2156. HWREG(ui32Base + EMAC_O_CFG) |= EMAC_CFG_TE;
  2157. }
  2158. //*****************************************************************************
  2159. //
  2160. //! Disables the Ethernet controller transmitter.
  2161. //!
  2162. //! \param ui32Base is the base address of the controller.
  2163. //!
  2164. //! When terminating operations on the Ethernet interface, this function should
  2165. //! be called. This function disables the transmitter.
  2166. //!
  2167. //! \return None.
  2168. //
  2169. //*****************************************************************************
  2170. void
  2171. EMACTxDisable(uint32_t ui32Base)
  2172. {
  2173. //
  2174. // Disable transmission in the MAC configuration register.
  2175. //
  2176. HWREG(ui32Base + EMAC_O_CFG) &= ~EMAC_CFG_TE;
  2177. //
  2178. // Disable the MAC transmit path in the opmode register.
  2179. //
  2180. HWREG(ui32Base + EMAC_O_DMAOPMODE) &= ~EMAC_DMAOPMODE_ST;
  2181. }
  2182. //*****************************************************************************
  2183. //
  2184. //! Enables the Ethernet controller receiver.
  2185. //!
  2186. //! \param ui32Base is the base address of the controller.
  2187. //!
  2188. //! When starting operations on the Ethernet interface, this function should
  2189. //! be called to enable the receiver after all configuration has been
  2190. //! completed.
  2191. //!
  2192. //! \return None.
  2193. //
  2194. //*****************************************************************************
  2195. void
  2196. EMACRxEnable(uint32_t ui32Base)
  2197. {
  2198. //
  2199. // Enable the MAC receive path.
  2200. //
  2201. HWREG(ui32Base + EMAC_O_DMAOPMODE) |= EMAC_DMAOPMODE_SR;
  2202. //
  2203. // Enable receive in the MAC configuration register.
  2204. //
  2205. HWREG(ui32Base + EMAC_O_CFG) |= EMAC_CFG_RE;
  2206. }
  2207. //*****************************************************************************
  2208. //
  2209. //! Disables the Ethernet controller receiver.
  2210. //!
  2211. //! \param ui32Base is the base address of the controller.
  2212. //!
  2213. //! When terminating operations on the Ethernet interface, this function should
  2214. //! be called. This function disables the receiver.
  2215. //!
  2216. //! \return None.
  2217. //
  2218. //*****************************************************************************
  2219. void
  2220. EMACRxDisable(uint32_t ui32Base)
  2221. {
  2222. //
  2223. // Disable reception in the MAC configuration register.
  2224. //
  2225. HWREG(ui32Base + EMAC_O_CFG) &= ~EMAC_CFG_RE;
  2226. //
  2227. // Disable the MAC receive path.
  2228. //
  2229. HWREG(ui32Base + EMAC_O_DMAOPMODE) &= ~EMAC_DMAOPMODE_SR;
  2230. }
  2231. //*****************************************************************************
  2232. //
  2233. //! Registers an interrupt handler for an Ethernet interrupt.
  2234. //!
  2235. //! \param ui32Base is the base address of the controller.
  2236. //! \param pfnHandler is a pointer to the function to be called when the
  2237. //! enabled Ethernet interrupts occur.
  2238. //!
  2239. //! This function sets the handler to be called when the Ethernet interrupt
  2240. //! occurs. This function enables the global interrupt in the interrupt
  2241. //! controller; specific Ethernet interrupts must be enabled via
  2242. //! EMACIntEnable(). It is the interrupt handler's responsibility to clear
  2243. //! the interrupt source.
  2244. //!
  2245. //! \sa IntRegister() for important information about registering interrupt
  2246. //! handlers.
  2247. //!
  2248. //! \return None.
  2249. //
  2250. //*****************************************************************************
  2251. void
  2252. EMACIntRegister(uint32_t ui32Base, void (*pfnHandler)(void))
  2253. {
  2254. //
  2255. // Check the arguments.
  2256. //
  2257. ASSERT(pfnHandler != 0);
  2258. //
  2259. // Register the interrupt handler.
  2260. //
  2261. IntRegister(INT_EMAC0_TM4C129, pfnHandler);
  2262. //
  2263. // Enable the Ethernet interrupt.
  2264. //
  2265. IntEnable(INT_EMAC0_TM4C129);
  2266. }
  2267. //*****************************************************************************
  2268. //
  2269. //! Unregisters an interrupt handler for an Ethernet interrupt.
  2270. //!
  2271. //! \param ui32Base is the base address of the controller.
  2272. //!
  2273. //! This function unregisters the interrupt handler. This function disables
  2274. //! the global interrupt in the interrupt controller so that the interrupt
  2275. //! handler is no longer called.
  2276. //!
  2277. //! \sa IntRegister() for important information about registering interrupt
  2278. //! handlers.
  2279. //!
  2280. //! \return None.
  2281. //
  2282. //*****************************************************************************
  2283. void
  2284. EMACIntUnregister(uint32_t ui32Base)
  2285. {
  2286. //
  2287. // Disable the interrupt.
  2288. //
  2289. IntDisable(INT_EMAC0_TM4C129);
  2290. //
  2291. // Unregister the interrupt handler.
  2292. //
  2293. IntUnregister(INT_EMAC0_TM4C129);
  2294. }
  2295. //*****************************************************************************
  2296. //
  2297. //! Enables individual Ethernet MAC interrupt sources.
  2298. //!
  2299. //! \param ui32Base is the base address of the Ethernet MAC.
  2300. //! \param ui32IntFlags is the bit mask of the interrupt sources to be enabled.
  2301. //!
  2302. //! This function enables the indicated Ethernet MAC interrupt sources. Only
  2303. //! the sources that are enabled can be reflected to the processor interrupt;
  2304. //! disabled sources have no effect on the processor.
  2305. //!
  2306. //! The \e ui32IntFlags parameter is the logical OR of any of the following:
  2307. //!
  2308. //! - \b EMAC_INT_PHY indicates that the PHY has signaled a change of state.
  2309. //! Software must read and write the appropriate PHY registers to enable and
  2310. //! disable particular notifications.
  2311. //! - \b EMAC_INT_EARLY_RECEIVE indicates that the DMA engine has filled the
  2312. //! first data buffer of a packet.
  2313. //! - \b EMAC_INT_BUS_ERROR indicates that a fatal bus error has occurred and
  2314. //! that the DMA engine has been disabled.
  2315. //! - \b EMAC_INT_EARLY_TRANSMIT indicates that a frame to be transmitted has
  2316. //! been fully written from memory into the MAC transmit FIFO.
  2317. //! - \b EMAC_INT_RX_WATCHDOG indicates that a frame with length greater than
  2318. //! 2048 bytes (of 10240 bytes in Jumbo Frame mode) was received.
  2319. //! - \b EMAC_INT_RX_STOPPED indicates that the receive process has entered
  2320. //! the stopped state.
  2321. //! - \b EMAC_INT_RX_NO_BUFFER indicates that the host owns the next buffer
  2322. //! in the DMA's receive descriptor list and the DMA cannot, therefore, acquire
  2323. //! a buffer. The receive process is suspended and can be resumed by changing
  2324. //! the descriptor ownership and calling EMACRxDMAPollDemand().
  2325. //! - \b EMAC_INT_RECEIVE indicates that reception of a frame has completed
  2326. //! and all requested status has been written to the appropriate DMA receive
  2327. //! descriptor.
  2328. //! - \b EMAC_INT_TX_UNDERFLOW indicates that the transmitter experienced an
  2329. //! underflow during transmission. The transmit process is suspended.
  2330. //! - \b EMAC_INT_RX_OVERFLOW indicates that an overflow was experienced
  2331. //! during reception.
  2332. //! - \b EMAC_INT_TX_JABBER indicates that the transmit jabber timer expired.
  2333. //! This condition occurs when the frame size exceeds 2048 bytes (or 10240
  2334. //! bytes in Jumbo Frame mode) and causes the transmit process to abort and
  2335. //! enter the Stopped state.
  2336. //! - \b EMAC_INT_TX_NO_BUFFER indicates that the host owns the next buffer
  2337. //! in the DMA's transmit descriptor list and that the DMA cannot, therefore,
  2338. //! acquire a buffer. Transmission is suspended and can be resumed by changing
  2339. //! the descriptor ownership and calling EMACTxDMAPollDemand().
  2340. //! - \b EMAC_INT_TX_STOPPED indicates that the transmit process has stopped.
  2341. //! - \b EMAC_INT_TRANSMIT indicates that transmission of a frame has
  2342. //! completed and that all requested status has been updated in the descriptor.
  2343. //!
  2344. //! Summary interrupt bits \b EMAC_INT_NORMAL_INT and
  2345. //! \b EMAC_INT_ABNORMAL_INT are enabled automatically by the driver if any
  2346. //! of their constituent sources are enabled. Applications do not need to
  2347. //! explicitly enable these bits.
  2348. //!
  2349. //! \note Timestamp-related interrupts from the IEEE 1588 module must be
  2350. //! enabled independently by using a call to EMACTimestampTargetIntEnable().
  2351. //!
  2352. //! \return None.
  2353. //
  2354. //*****************************************************************************
  2355. void
  2356. EMACIntEnable(uint32_t ui32Base, uint32_t ui32IntFlags)
  2357. {
  2358. //
  2359. // Parameter sanity check.
  2360. //
  2361. ASSERT((ui32IntFlags & ~EMAC_MASKABLE_INTS) == 0);
  2362. //
  2363. // Enable the normal interrupt if any of its individual sources are
  2364. // enabled.
  2365. //
  2366. if(ui32IntFlags & EMAC_NORMAL_INTS)
  2367. {
  2368. ui32IntFlags |= EMAC_INT_NORMAL_INT;
  2369. }
  2370. //
  2371. // Similarly, enable the abnormal interrupt if any of its individual
  2372. // sources are enabled.
  2373. //
  2374. if(ui32IntFlags & EMAC_ABNORMAL_INTS)
  2375. {
  2376. ui32IntFlags |= EMAC_INT_ABNORMAL_INT;
  2377. }
  2378. //
  2379. // Set the MAC DMA interrupt mask appropriately if any of the sources
  2380. // we've been asked to enable are found in that register.
  2381. //
  2382. if(ui32IntFlags & ~EMAC_INT_PHY)
  2383. {
  2384. HWREG(ui32Base + EMAC_O_DMAIM) |= ui32IntFlags & ~EMAC_INT_PHY;
  2385. }
  2386. //
  2387. // Enable the PHY interrupt if we've been asked to do this.
  2388. //
  2389. if(ui32IntFlags & EMAC_INT_PHY)
  2390. {
  2391. HWREG(ui32Base + EMAC_O_EPHYIM) |= EMAC_EPHYIM_INT;
  2392. }
  2393. }
  2394. //*****************************************************************************
  2395. //
  2396. //! Disables individual Ethernet MAC interrupt sources.
  2397. //!
  2398. //! \param ui32Base is the base address of the Ethernet MAC.
  2399. //! \param ui32IntFlags is the bit mask of the interrupt sources to be disabled.
  2400. //!
  2401. //! This function disables the indicated Ethernet MAC interrupt sources.
  2402. //!
  2403. //! The \e ui32IntFlags parameter is the logical OR of any of the following:
  2404. //!
  2405. //! - \b EMAC_INT_PHY indicates that the PHY has signaled a change of state.
  2406. //! Software must read and write the appropriate PHY registers to enable and
  2407. //! disable particular notifications.
  2408. //! - \b EMAC_INT_EARLY_RECEIVE indicates that the DMA engine has filled the
  2409. //! first data buffer of a packet.
  2410. //! - \b EMAC_INT_BUS_ERROR indicates that a fatal bus error has occurred and
  2411. //! that the DMA engine has been disabled.
  2412. //! - \b EMAC_INT_EARLY_TRANSMIT indicates that a frame to be transmitted has
  2413. //! been fully written from memory into the MAC transmit FIFO.
  2414. //! - \b EMAC_INT_RX_WATCHDOG indicates that a frame with length greater than
  2415. //! 2048 bytes (of 10240 bytes in Jumbo Frame mode) was received.
  2416. //! - \b EMAC_INT_RX_STOPPED indicates that the receive process has entered
  2417. //! the stopped state.
  2418. //! - \b EMAC_INT_RX_NO_BUFFER indicates that the host owns the next buffer
  2419. //! in the DMA's receive descriptor list and the DMA cannot, therefore, acquire
  2420. //! a buffer. The receive process is suspended and can be resumed by changing
  2421. //! the descriptor ownership and calling EMACRxDMAPollDemand().
  2422. //! - \b EMAC_INT_RECEIVE indicates that reception of a frame has completed
  2423. //! and all requested status has been written to the appropriate DMA receive
  2424. //! descriptor.
  2425. //! - \b EMAC_INT_TX_UNDERFLOW indicates that the transmitter experienced an
  2426. //! underflow during transmission. The transmit process is suspended.
  2427. //! - \b EMAC_INT_RX_OVERFLOW indicates that an overflow was experienced
  2428. //! during reception.
  2429. //! - \b EMAC_INT_TX_JABBER indicates that the transmit jabber timer expired.
  2430. //! This condition occurs when the frame size exceeds 2048 bytes (or 10240
  2431. //! bytes in Jumbo Frame mode) and causes the transmit process to abort and
  2432. //! enter the Stopped state.
  2433. //! - \b EMAC_INT_TX_NO_BUFFER indicates that the host owns the next buffer
  2434. //! in the DMA's transmit descriptor list and that the DMA cannot, therefore,
  2435. //! acquire a buffer. Transmission is suspended and can be resumed by changing
  2436. //! the descriptor ownership and calling EMACTxDMAPollDemand().
  2437. //! - \b EMAC_INT_TX_STOPPED indicates that the transmit process has stopped.
  2438. //! - \b EMAC_INT_TRANSMIT indicates that transmission of a frame has
  2439. //! completed and that all requested status has been updated in the descriptor.
  2440. //! - \b EMAC_INT_TIMESTAMP indicates that an interrupt from the timestamp
  2441. //! module has occurred. This precise source of the interrupt can be
  2442. //! determined by calling EMACTimestampIntStatus(), which also clears this
  2443. //! bit.
  2444. //!
  2445. //! Summary interrupt bits \b EMAC_INT_NORMAL_INT and
  2446. //! \b EMAC_INT_ABNORMAL_INT are disabled automatically by the driver if none
  2447. //! of their constituent sources are enabled. Applications do not need to
  2448. //! explicitly disable these bits.
  2449. //!
  2450. //! \note Timestamp-related interrupts from the IEEE 1588 module must be
  2451. //! disabled independently by using a call to EMACTimestampTargetIntDisable().
  2452. //!
  2453. //! \return None.
  2454. //
  2455. //*****************************************************************************
  2456. void
  2457. EMACIntDisable(uint32_t ui32Base, uint32_t ui32IntFlags)
  2458. {
  2459. uint32_t ui32Mask;
  2460. //
  2461. // Parameter sanity check.
  2462. //
  2463. ASSERT(ui32Base == EMAC0_BASE);
  2464. ASSERT((ui32IntFlags & ~EMAC_MASKABLE_INTS) == 0);
  2465. //
  2466. // Get the current interrupt mask.
  2467. //
  2468. ui32Mask = HWREG(ui32Base + EMAC_O_DMAIM);
  2469. //
  2470. // Clear the requested bits.
  2471. //
  2472. ui32Mask &= ~(ui32IntFlags & ~EMAC_INT_PHY);
  2473. //
  2474. // If none of the normal interrupt sources are enabled, disable the
  2475. // normal interrupt.
  2476. //
  2477. if(!(ui32Mask & EMAC_NORMAL_INTS))
  2478. {
  2479. ui32Mask &= ~EMAC_INT_NORMAL_INT;
  2480. }
  2481. //
  2482. // Similarly, if none of the abnormal interrupt sources are enabled,
  2483. // disable the abnormal interrupt.
  2484. //
  2485. if(!(ui32Mask & EMAC_ABNORMAL_INTS))
  2486. {
  2487. ui32Mask &= ~EMAC_INT_ABNORMAL_INT;
  2488. }
  2489. //
  2490. // Write the new mask back to the hardware.
  2491. //
  2492. HWREG(ui32Base + EMAC_O_DMAIM) = ui32Mask;
  2493. //
  2494. // Disable the PHY interrupt if we've been asked to do this.
  2495. //
  2496. if(ui32IntFlags & EMAC_INT_PHY)
  2497. {
  2498. HWREG(ui32Base + EMAC_O_EPHYIM) &= ~EMAC_EPHYIM_INT;
  2499. }
  2500. }
  2501. //*****************************************************************************
  2502. //
  2503. //! Gets the current Ethernet MAC interrupt status.
  2504. //!
  2505. //! \param ui32Base is the base address of the Ethernet MAC.
  2506. //! \param bMasked is \b true to return the masked interrupt status or \b false
  2507. //! to return the unmasked status.
  2508. //!
  2509. //! This function returns the interrupt status for the Ethernet MAC. Either
  2510. //! the raw interrupt status or the status of interrupts that are allowed
  2511. //! to reflect to the processor can be returned.
  2512. //!
  2513. //! \return Returns the current interrupt status as the logical OR of any of
  2514. //! the following:
  2515. //!
  2516. //! - \b EMAC_INT_PHY indicates that the PHY interrupt has occurred.
  2517. //! Software must read the relevant PHY interrupt status register to determine
  2518. //! the cause.
  2519. //! - \b EMAC_INT_EARLY_RECEIVE indicates that the DMA engine has filled the
  2520. //! first data buffer of a packet.
  2521. //! - \b EMAC_INT_BUS_ERROR indicates that a fatal bus error has occurred and
  2522. //! that the DMA engine has been disabled. The cause of the error can be
  2523. //! determined by calling EMACDMAStateGet().
  2524. //! - \b EMAC_INT_EARLY_TRANSMIT indicates that a frame to be transmitted has
  2525. //! been fully written from memory into the MAC transmit FIFO.
  2526. //! - \b EMAC_INT_RX_WATCHDOG indicates that a frame with length greater than
  2527. //! 2048 bytes (of 10240 bytes in Jumbo Frame mode) was received.
  2528. //! - \b EMAC_INT_RX_STOPPED indicates that the receive process has entered
  2529. //! the stopped state.
  2530. //! - \b EMAC_INT_RX_NO_BUFFER indicates that the host owns the next buffer
  2531. //! in the DMA's receive descriptor list and the DMA cannot, therefore, acquire
  2532. //! a buffer. The receive process is suspended and can be resumed by changing
  2533. //! the descriptor ownership and calling EMACRxDMAPollDemand().
  2534. //! - \b EMAC_INT_RECEIVE indicates that reception of a frame has completed
  2535. //! and all requested status has been written to the appropriate DMA receive
  2536. //! descriptor.
  2537. //! - \b EMAC_INT_TX_UNDERFLOW indicates that the transmitter experienced an
  2538. //! underflow during transmission. The transmit process is suspended.
  2539. //! - \b EMAC_INT_RX_OVERFLOW indicates that an overflow was experienced
  2540. //! during reception.
  2541. //! - \b EMAC_INT_TX_JABBER indicates that the transmit jabber timer expired.
  2542. //! This condition occurs when the frame size exceeds 2048 bytes (or 10240
  2543. //! bytes in Jumbo Frame mode) and causes the transmit process to abort and
  2544. //! enter the Stopped state.
  2545. //! - \b EMAC_INT_TX_NO_BUFFER indicates that the host owns the next buffer
  2546. //! in the DMA's transmit descriptor list and that the DMA cannot, therefore,
  2547. //! acquire a buffer. Transmission is suspended and can be resumed by changing
  2548. //! the descriptor ownership and calling EMACTxDMAPollDemand().
  2549. //! - \b EMAC_INT_TX_STOPPED indicates that the transmit process has stopped.
  2550. //! - \b EMAC_INT_TRANSMIT indicates that transmission of a frame has
  2551. //! completed and that all requested status has been updated in the descriptor.
  2552. //! - \b EMAC_INT_NORMAL_INT is a summary interrupt comprising the logical
  2553. //! OR of the masked state of \b EMAC_INT_TRANSMIT, \b EMAC_INT_RECEIVE,
  2554. //! \b EMAC_INT_TX_NO_BUFFER and \b EMAC_INT_EARLY_RECEIVE.
  2555. //! - \b EMAC_INT_ABNORMAL_INT is a summary interrupt comprising the logical
  2556. //! OR of the masked state of \b EMAC_INT_TX_STOPPED, \b EMAC_INT_TX_JABBER,
  2557. //! \b EMAC_INT_RX_OVERFLOW, \b EMAC_INT_TX_UNDERFLOW,
  2558. //! \b EMAC_INT_RX_NO_BUFFER, \b EMAC_INT_RX_STOPPED,
  2559. //! \b EMAC_INT_RX_WATCHDOG, \b EMAC_INT_EARLY_TRANSMIT and
  2560. //! \b EMAC_INT_BUS_ERROR.
  2561. //
  2562. //*****************************************************************************
  2563. uint32_t
  2564. EMACIntStatus(uint32_t ui32Base, bool bMasked)
  2565. {
  2566. uint32_t ui32Val, ui32PHYStat;
  2567. //
  2568. // Parameter sanity check.
  2569. //
  2570. ASSERT(ui32Base == EMAC0_BASE);
  2571. //
  2572. // Get the unmasked interrupt status and clear any unwanted status fields.
  2573. //
  2574. ui32Val = HWREG(ui32Base + EMAC_O_DMARIS);
  2575. ui32Val &= ~(EMAC_DMARIS_AE_M | EMAC_DMARIS_TS_M | EMAC_DMARIS_RS_M);
  2576. //
  2577. // This peripheral doesn't have a masked interrupt status register
  2578. // so perform the masking manually. Note that only the bottom 16 bits
  2579. // of the register can be masked so make sure we take this into account.
  2580. //
  2581. if(bMasked)
  2582. {
  2583. ui32Val &= (EMAC_NON_MASKED_INTS | HWREG(ui32Base + EMAC_O_DMAIM));
  2584. }
  2585. //
  2586. // Read the PHY interrupt status.
  2587. //
  2588. if(bMasked)
  2589. {
  2590. ui32PHYStat = HWREG(ui32Base + EMAC_O_EPHYMISC);
  2591. }
  2592. else
  2593. {
  2594. ui32PHYStat = HWREG(ui32Base + EMAC_O_EPHYRIS);
  2595. }
  2596. //
  2597. // If the PHY interrupt is reported, add the appropriate flag to the
  2598. // return value.
  2599. //
  2600. if(ui32PHYStat & EMAC_EPHYMISC_INT)
  2601. {
  2602. ui32Val |= EMAC_INT_PHY;
  2603. }
  2604. return(ui32Val);
  2605. }
  2606. //*****************************************************************************
  2607. //
  2608. //! Clears individual Ethernet MAC interrupt sources.
  2609. //!
  2610. //! \param ui32Base is the base address of the Ethernet MAC.
  2611. //! \param ui32IntFlags is the bit mask of the interrupt sources to be cleared.
  2612. //!
  2613. //! This function disables the indicated Ethernet MAC interrupt sources.
  2614. //!
  2615. //! The \e ui32IntFlags parameter is the logical OR of any of the following:
  2616. //!
  2617. //! - \b EMAC_INT_PHY indicates that the PHY has signaled a change of state.
  2618. //! Software must read and write the appropriate PHY registers to enable,
  2619. //! disable and clear particular notifications.
  2620. //! - \b EMAC_INT_EARLY_RECEIVE indicates that the DMA engine has filled the
  2621. //! first data buffer of a packet.
  2622. //! - \b EMAC_INT_BUS_ERROR indicates that a fatal bus error has occurred and
  2623. //! that the DMA engine has been disabled.
  2624. //! - \b EMAC_INT_EARLY_TRANSMIT indicates that a frame to be transmitted has
  2625. //! been fully written from memory into the MAC transmit FIFO.
  2626. //! - \b EMAC_INT_RX_WATCHDOG indicates that a frame with length greater than
  2627. //! 2048 bytes (of 10240 bytes in Jumbo Frame mode) was received.
  2628. //! - \b EMAC_INT_RX_STOPPED indicates that the receive process has entered
  2629. //! the stopped state.
  2630. //! - \b EMAC_INT_RX_NO_BUFFER indicates that the host owns the next buffer
  2631. //! in the DMA's receive descriptor list and the DMA cannot, therefore, acquire
  2632. //! a buffer. The receive process is suspended and can be resumed by changing
  2633. //! the descriptor ownership and calling EMACRxDMAPollDemand().
  2634. //! - \b EMAC_INT_RECEIVE indicates that reception of a frame has completed
  2635. //! and all requested status has been written to the appropriate DMA receive
  2636. //! descriptor.
  2637. //! - \b EMAC_INT_TX_UNDERFLOW indicates that the transmitter experienced an
  2638. //! underflow during transmission. The transmit process is suspended.
  2639. //! - \b EMAC_INT_RX_OVERFLOW indicates that an overflow was experienced
  2640. //! during reception.
  2641. //! - \b EMAC_INT_TX_JABBER indicates that the transmit jabber timer expired.
  2642. //! This condition occurs when the frame size exceeds 2048 bytes (or 10240
  2643. //! bytes in Jumbo Frame mode) and causes the transmit process to abort and
  2644. //! enter the Stopped state.
  2645. //! - \b EMAC_INT_TX_NO_BUFFER indicates that the host owns the next buffer
  2646. //! in the DMA's transmit descriptor list and that the DMA cannot, therefore,
  2647. //! acquire a buffer. Transmission is suspended and can be resumed by changing
  2648. //! the descriptor ownership and calling EMACTxDMAPollDemand().
  2649. //! - \b EMAC_INT_TX_STOPPED indicates that the transmit process has stopped.
  2650. //! - \b EMAC_INT_TRANSMIT indicates that transmission of a frame has
  2651. //! completed and that all requested status has been updated in the descriptor.
  2652. //!
  2653. //! Summary interrupt bits \b EMAC_INT_NORMAL_INT and
  2654. //! \b EMAC_INT_ABNORMAL_INT are cleared automatically by the driver if any
  2655. //! of their constituent sources are cleared. Applications do not need to
  2656. //! explicitly clear these bits.
  2657. //!
  2658. //! \return None.
  2659. //
  2660. //*****************************************************************************
  2661. void
  2662. EMACIntClear(uint32_t ui32Base, uint32_t ui32IntFlags)
  2663. {
  2664. //
  2665. // Parameter sanity check.
  2666. //
  2667. ASSERT(ui32Base == EMAC0_BASE);
  2668. //
  2669. // Mask in the normal interrupt if one of the sources it relates to is
  2670. // specified.
  2671. //
  2672. if(ui32IntFlags & EMAC_NORMAL_INTS)
  2673. {
  2674. ui32IntFlags |= EMAC_INT_NORMAL_INT;
  2675. }
  2676. //
  2677. // Similarly, mask in the abnormal interrupt if one of the sources it
  2678. // relates to is specified.
  2679. //
  2680. if(ui32IntFlags & EMAC_ABNORMAL_INTS)
  2681. {
  2682. ui32IntFlags |= EMAC_INT_ABNORMAL_INT;
  2683. }
  2684. //
  2685. // Clear the maskable interrupt sources. We write exactly the value passed
  2686. // (with the summary sources added if necessary) but remember that only
  2687. // the bottom 17 bits of the register are actually clearable. Only do
  2688. // this if some bits are actually set that refer to the DMA interrupt
  2689. // sources.
  2690. //
  2691. if(ui32IntFlags & ~EMAC_INT_PHY)
  2692. {
  2693. HWREG(ui32Base + EMAC_O_DMARIS) = (ui32IntFlags & ~EMAC_INT_PHY);
  2694. }
  2695. //
  2696. // Clear the PHY interrupt if we've been asked to do this.
  2697. //
  2698. if(ui32IntFlags & EMAC_INT_PHY)
  2699. {
  2700. HWREG(ui32Base + EMAC_O_EPHYMISC) |= EMAC_EPHYMISC_INT;
  2701. }
  2702. }
  2703. //*****************************************************************************
  2704. //
  2705. //! Writes to the PHY register.
  2706. //!
  2707. //! \param ui32Base is the base address of the controller.
  2708. //! \param ui8PhyAddr is the physical address of the PHY to access.
  2709. //! \param ui8RegAddr is the address of the PHY register to be accessed.
  2710. //! \param ui16Data is the data to be written to the PHY register.
  2711. //!
  2712. //! This function writes the \e ui16Data value to the PHY register specified by
  2713. //! \e ui8RegAddr.
  2714. //!
  2715. //! \return None.
  2716. //
  2717. //*****************************************************************************
  2718. void
  2719. EMACPHYWrite(uint32_t ui32Base, uint8_t ui8PhyAddr, uint8_t ui8RegAddr,
  2720. uint16_t ui16Data)
  2721. {
  2722. //
  2723. // Parameter sanity check.
  2724. //
  2725. ASSERT(ui32Base == EMAC0_BASE);
  2726. //
  2727. // Parameter sanity check.
  2728. //
  2729. ASSERT(ui8PhyAddr < 32);
  2730. //
  2731. // Make sure the MII is idle.
  2732. //
  2733. while(HWREG(ui32Base + EMAC_O_MIIADDR) & EMAC_MIIADDR_MIIB)
  2734. {
  2735. }
  2736. //
  2737. // Write the value provided.
  2738. //
  2739. HWREG(ui32Base + EMAC_O_MIIDATA) = ui16Data;
  2740. //
  2741. // Tell the MAC to write the given PHY register.
  2742. //
  2743. HWREG(ui32Base + EMAC_O_MIIADDR) =
  2744. ((HWREG(ui32Base + EMAC_O_MIIADDR) &
  2745. EMAC_MIIADDR_CR_M) | (ui8RegAddr << EMAC_MIIADDR_MII_S) |
  2746. (ui8PhyAddr << EMAC_MIIADDR_PLA_S) | EMAC_MIIADDR_MIIW |
  2747. EMAC_MIIADDR_MIIB);
  2748. //
  2749. // Wait for the write to complete.
  2750. //
  2751. while(HWREG(ui32Base + EMAC_O_MIIADDR) & EMAC_MIIADDR_MIIB)
  2752. {
  2753. }
  2754. }
  2755. //*****************************************************************************
  2756. //
  2757. //! Reads from a PHY register.
  2758. //!
  2759. //! \param ui32Base is the base address of the controller.
  2760. //! \param ui8PhyAddr is the physical address of the PHY to access.
  2761. //! \param ui8RegAddr is the address of the PHY register to be accessed.
  2762. //!
  2763. //! This function returns the contents of the PHY register specified by
  2764. //! \e ui8RegAddr.
  2765. //!
  2766. //! \return Returns the 16-bit value read from the PHY.
  2767. //
  2768. //*****************************************************************************
  2769. uint16_t
  2770. EMACPHYRead(uint32_t ui32Base, uint8_t ui8PhyAddr, uint8_t ui8RegAddr)
  2771. {
  2772. //
  2773. // Parameter sanity check.
  2774. //
  2775. ASSERT(ui8PhyAddr < 32);
  2776. ASSERT(ui32Base == EMAC0_BASE);
  2777. //
  2778. // Make sure the MII is idle.
  2779. //
  2780. while(HWREG(ui32Base + EMAC_O_MIIADDR) & EMAC_MIIADDR_MIIB)
  2781. {
  2782. }
  2783. //
  2784. // Tell the MAC to read the given PHY register.
  2785. //
  2786. HWREG(ui32Base + EMAC_O_MIIADDR) =
  2787. ((HWREG(ui32Base + EMAC_O_MIIADDR) & EMAC_MIIADDR_CR_M) |
  2788. EMAC_MIIADDR_CR_100_150 |
  2789. (ui8RegAddr << EMAC_MIIADDR_MII_S) |
  2790. (ui8PhyAddr << EMAC_MIIADDR_PLA_S) | EMAC_MIIADDR_MIIB);
  2791. //
  2792. // Wait for the read to complete.
  2793. //
  2794. while(HWREG(ui32Base + EMAC_O_MIIADDR) & EMAC_MIIADDR_MIIB)
  2795. {
  2796. }
  2797. //
  2798. // Return the result.
  2799. //
  2800. return(HWREG(ui32Base + EMAC_O_MIIDATA) & EMAC_MIIDATA_DATA_M);
  2801. }
  2802. //*****************************************************************************
  2803. //
  2804. //! Reads from an extended PHY register.
  2805. //!
  2806. //! \param ui32Base is the base address of the controller.
  2807. //! \param ui8PhyAddr is the physical address of the PHY to access.
  2808. //! \param ui16RegAddr is the address of the PHY extended register to be
  2809. //! accessed.
  2810. //!
  2811. //! When using the internal PHY or when connected to an external PHY
  2812. //! supporting extended registers, this function returns the contents of the
  2813. //! extended PHY register specified by \e ui16RegAddr.
  2814. //!
  2815. //! \return Returns the 16-bit value read from the PHY.
  2816. //
  2817. //*****************************************************************************
  2818. uint16_t
  2819. EMACPHYExtendedRead(uint32_t ui32Base, uint8_t ui8PhyAddr,
  2820. uint16_t ui16RegAddr)
  2821. {
  2822. //
  2823. // Parameter sanity check.
  2824. //
  2825. ASSERT(ui8PhyAddr < 32);
  2826. ASSERT(ui32Base == EMAC0_BASE);
  2827. //
  2828. // Set the address of the register we're about to read.
  2829. //
  2830. EMACPHYWrite(EMAC0_BASE, ui8PhyAddr, EPHY_REGCTL, 0x001F);
  2831. EMACPHYWrite(EMAC0_BASE, ui8PhyAddr, EPHY_ADDAR, ui16RegAddr);
  2832. //
  2833. // Read the extended register value.
  2834. //
  2835. EMACPHYWrite(EMAC0_BASE, ui8PhyAddr, EPHY_REGCTL, 0x401F);
  2836. return(EMACPHYRead(EMAC0_BASE, ui8PhyAddr, EPHY_ADDAR));
  2837. }
  2838. //*****************************************************************************
  2839. //
  2840. //! Writes a value to an extended PHY register.
  2841. //!
  2842. //! \param ui32Base is the base address of the controller.
  2843. //! \param ui8PhyAddr is the physical address of the PHY to access.
  2844. //! \param ui16RegAddr is the address of the PHY extended register to be
  2845. //! accessed.
  2846. //! \param ui16Value is the value to write to the register.
  2847. //!
  2848. //! When using the internal PHY or when connected to an external PHY
  2849. //! supporting extended registers, this function allows a value to be written
  2850. //! to the extended PHY register specified by \e ui16RegAddr.
  2851. //!
  2852. //! \return None.
  2853. //
  2854. //*****************************************************************************
  2855. void
  2856. EMACPHYExtendedWrite(uint32_t ui32Base, uint8_t ui8PhyAddr,
  2857. uint16_t ui16RegAddr, uint16_t ui16Value)
  2858. {
  2859. //
  2860. // Parameter sanity check.
  2861. //
  2862. ASSERT(ui8PhyAddr < 32);
  2863. ASSERT(ui32Base == EMAC0_BASE);
  2864. //
  2865. // Set the address of the register we're about to write.
  2866. //
  2867. EMACPHYWrite(EMAC0_BASE, ui8PhyAddr, EPHY_REGCTL, 0x001F);
  2868. EMACPHYWrite(EMAC0_BASE, ui8PhyAddr, EPHY_ADDAR, ui16RegAddr);
  2869. //
  2870. // Write the extended register.
  2871. //
  2872. EMACPHYWrite(EMAC0_BASE, ui8PhyAddr, EPHY_REGCTL, 0x401F);
  2873. EMACPHYWrite(EMAC0_BASE, ui8PhyAddr, EPHY_ADDAR, ui16Value);
  2874. }
  2875. //*****************************************************************************
  2876. //
  2877. //! Powers off the Ethernet PHY.
  2878. //!
  2879. //! \param ui32Base is the base address of the controller.
  2880. //! \param ui8PhyAddr is the physical address of the PHY to power down.
  2881. //!
  2882. //! This function powers off the Ethernet PHY, reducing the current
  2883. //! consumption of the device. While in the powered-off state, the Ethernet
  2884. //! controller is unable to connect to Ethernet.
  2885. //!
  2886. //! \return None.
  2887. //
  2888. //*****************************************************************************
  2889. void
  2890. EMACPHYPowerOff(uint32_t ui32Base, uint8_t ui8PhyAddr)
  2891. {
  2892. //
  2893. // Set the PWRDN bit and clear the ANEN bit in the PHY, putting it into
  2894. // its low power mode.
  2895. //
  2896. EMACPHYWrite(ui32Base, ui8PhyAddr, EPHY_BMCR,
  2897. (EMACPHYRead(ui32Base, ui8PhyAddr, EPHY_BMCR) &
  2898. ~EPHY_BMCR_ANEN) | EPHY_BMCR_PWRDWN);
  2899. }
  2900. //*****************************************************************************
  2901. //
  2902. //! Powers on the Ethernet PHY.
  2903. //!
  2904. //! \param ui32Base is the base address of the controller.
  2905. //! \param ui8PhyAddr is the physical address of the PHY to power up.
  2906. //!
  2907. //! This function powers on the Ethernet PHY, enabling it return to normal
  2908. //! operation. By default, the PHY is powered on, so this function is only
  2909. //! called if EMACPHYPowerOff() has previously been called.
  2910. //!
  2911. //! \return None.
  2912. //
  2913. //*****************************************************************************
  2914. void
  2915. EMACPHYPowerOn(uint32_t ui32Base, uint8_t ui8PhyAddr)
  2916. {
  2917. //
  2918. // Clear the PWRDN bit and set the ANEGEN bit in the PHY, putting it into
  2919. // normal operating mode.
  2920. //
  2921. EMACPHYWrite(ui32Base, ui8PhyAddr, EPHY_BMCR,
  2922. (EMACPHYRead(ui32Base, ui8PhyAddr, EPHY_BMCR) &
  2923. ~EPHY_BMCR_PWRDWN) | EPHY_BMCR_ANEN);
  2924. }
  2925. //*****************************************************************************
  2926. //
  2927. //! Configures the Ethernet MAC's IEEE 1588 timestamping options.
  2928. //!
  2929. //! \param ui32Base is the base address of the controller.
  2930. //! \param ui32Config contains flags selecting particular configuration
  2931. //! options.
  2932. //! \param ui32SubSecondInc is the number that the IEEE 1588 subsecond clock
  2933. //! should increment on each tick.
  2934. //!
  2935. //! This function is used to configure the operation of the Ethernet MAC's
  2936. //! internal timestamping clock. This clock is used to timestamp incoming
  2937. //! and outgoing packets and as an accurate system time reference when
  2938. //! IEEE 1588 Precision Time Protocol is in use.
  2939. //!
  2940. //! The \e ui32Config parameter contains a collection of flags selecting the
  2941. //! desired options. Valid flags are:
  2942. //!
  2943. //! One of the following to determine whether IEEE 1588 version 1 or version 2
  2944. //! packet format is to be processed:
  2945. //!
  2946. //! - \b EMAC_TS_PTP_VERSION_2
  2947. //! - \b EMAC_TS_PTP_VERSION_1
  2948. //!
  2949. //! One of the following to determine how the IEEE 1588 clock's subsecond
  2950. //! value should be interpreted and handled:
  2951. //!
  2952. //! - \b EMAC_TS_DIGITAL_ROLLOVER causes the clock's subsecond value to roll
  2953. //! over at 0x3BA9C9FF (999999999 decimal). In this mode, it can be considered
  2954. //! as a nanosecond counter with each digit representing 1 ns.
  2955. //! - \b EMAC_TS_BINARY_ROLLOVER causes the clock's subsecond value to roll
  2956. //! over at 0x7FFFFFFF. In this mode, the subsecond value counts 0.465 ns
  2957. //! periods.
  2958. //!
  2959. //! One of the following to enable or disable MAC address filtering. When
  2960. //! enabled, PTP frames are filtered unless the destination MAC address matches
  2961. //! any of the currently programmed MAC addresses.
  2962. //!
  2963. //! - \b EMAC_TS_MAC_FILTER_ENABLE
  2964. //! - \b EMAC_TS_MAC_FILTER_DISABLE
  2965. //!
  2966. //! One of the following to determine how the clock is updated:
  2967. //! - \b EMAC_TS_UPDATE_COARSE causes the IEEE 1588 clock to advance by
  2968. //! the value supplied in the \e ui32SubSecondInc parameter on each main
  2969. //! oscillator clock cycle.
  2970. //! - \b EMAC_TS_UPDATE_FINE selects the fine update method which causes the
  2971. //! IEEE 1588 clock to advance by the the value supplied in the
  2972. //! \e ui32SubSecondInc parameter each time a carry is generated from the
  2973. //! addend accumulator register.
  2974. //!
  2975. //! One of the following to determine which IEEE 1588 messages are timestamped:
  2976. //!
  2977. //! - \b EMAC_TS_SYNC_FOLLOW_DREQ_DRESP timestamps SYNC, Follow_Up, Delay_Req
  2978. //! and Delay_Resp messages.
  2979. //! - \b EMAC_TS_SYNC_ONLY timestamps only SYNC messages.
  2980. //! - \b EMAC_TS_DELAYREQ_ONLY timestamps only Delay_Req messages.
  2981. //! - \b EMAC_TS_ALL timestamps all IEEE 1588 messages.
  2982. //! - \b EMAC_TS_SYNC_PDREQ_PDRESP timestamps only SYNC, Pdelay_Req and
  2983. //! Pdelay_Resp messages.
  2984. //! - \b EMAC_TS_DREQ_PDREQ_PDRESP timestamps only Delay_Req, Pdelay_Req and
  2985. //! Pdelay_Resp messages.
  2986. //! - \b EMAC_TS_SYNC_DELAYREQ timestamps only Delay_Req messages.
  2987. //! - \b EMAC_TS_PDREQ_PDRESP timestamps only Pdelay_Req and Pdelay_Resp
  2988. //! messages.
  2989. //!
  2990. //! Optional, additional flags are:
  2991. //!
  2992. //! - \b EMAC_TS_PROCESS_IPV4_UDP processes PTP packets encapsulated in UDP
  2993. //! over IPv4 packets. If absent, the MAC ignores these frames.
  2994. //! - \b EMAC_TS_PROCESS_IPV6_UDP processes PTP packets encapsulated in UDP
  2995. //! over IPv6 packets. If absent, the MAC ignores these frames.
  2996. //! - \b EMAC_TS_PROCESS_ETHERNET processes PTP packets encapsulated directly
  2997. //! in Ethernet frames. If absent, the MAC ignores these frames.
  2998. //! - \b EMAC_TS_ALL_RX_FRAMES enables timestamping for all frames received
  2999. //! by the MAC, regardless of type.
  3000. //!
  3001. //! The \e ui32SubSecondInc controls the rate at which the timestamp clock's
  3002. //! subsecond count increments. Its meaning depends on which of \b
  3003. //! EMAC_TS_DIGITAL_ROLLOVER or \b EMAC_TS_BINARY_ROLLOVER and
  3004. //! \b EMAC_TS_UPDATE_FINE or \b EMAC_TS_UPDATE_COARSE were included
  3005. //! in \e ui32Config.
  3006. //!
  3007. //! The timestamp second counter is incremented each time the subsecond counter
  3008. //! rolls over. In digital rollover mode, the subsecond counter acts as a
  3009. //! simple 31-bit counter, rolling over to 0 after reaching 0x7FFFFFFF. In
  3010. //! this case, each lsb of the subsecond counter represents 0.465 ns (assuming
  3011. //! the definition of 1 second resolution for the seconds counter). When
  3012. //! binary rollover mode is selected, the subsecond counter acts as a
  3013. //! nanosecond counter and rolls over to 0 after reaching 999,999,999 making
  3014. //! each lsb represent 1 nanosecond.
  3015. //!
  3016. //! In coarse update mode, the timestamp subsecond counter is incremented by
  3017. //! \e ui32SubSecondInc on each main oscillator clock tick. Setting
  3018. //! \e ui32SubSecondInc to the main oscillator clock period in either 1 ns or
  3019. //! 0.465 ns units ensures that the time stamp, read as seconds and
  3020. //! subseconds, increments at the same rate as the main oscillator clock. For
  3021. //! example, if the main oscillator is 25 MHz, \e ui32SubSecondInc is set to 40
  3022. //! if digital rollover mode is selected or (40 / 0.465) = 86 in binary
  3023. //! rollover mode.
  3024. //!
  3025. //! In fine update mode, the subsecond increment value must be set according
  3026. //! to the desired accuracy of the recovered IEEE 1588 clock which must be
  3027. //! lower than the system clock rate. Fine update mode is typically used when
  3028. //! synchronizing the local clock to the IEEE 1588 master clock. The subsecond
  3029. //! counter is incremented by \e ui32SubSecondInc counts each time a 32-bit
  3030. //! accumulator register generates a carry. The accumulator register is
  3031. //! incremented by the addend value on each main oscillator tick and this
  3032. //! addend value is modified to allow fine control over the rate of change of
  3033. //! the timestamp counter. The addend value is calculated using the ratio of
  3034. //! the main oscillator clock rate and the desired IEEE 1588 clock rate and the
  3035. //! \e ui32SubSecondInc value is set to correspond to the desired IEEE 1588
  3036. //! clock rate. As an example, using digital rollover mode and a 25-MHz
  3037. //! main oscillator clock with a desired IEEE 1588 clock accuracy of 12.5 MHz,
  3038. //! we would set \e ui32SubSecondInc to the 12.5-MHz clock period of 80 ns and
  3039. //! set the initial addend value to 0x80000000 to generate a carry on every
  3040. //! second system clock.
  3041. //!
  3042. //! \sa EMACTimestampAddendSet()
  3043. //!
  3044. //! \return None.
  3045. //
  3046. //*****************************************************************************
  3047. void
  3048. EMACTimestampConfigSet(uint32_t ui32Base, uint32_t ui32Config,
  3049. uint32_t ui32SubSecondInc)
  3050. {
  3051. //
  3052. // Parameter sanity check.
  3053. //
  3054. ASSERT(ui32Base == EMAC0_BASE);
  3055. //
  3056. // Ensure that the PTP module clock is enabled.
  3057. //
  3058. HWREG(ui32Base + EMAC_O_CC) |= EMAC_CC_PTPCEN;
  3059. //
  3060. // Write the subsecond increment value.
  3061. //
  3062. HWREG(ui32Base + EMAC_O_SUBSECINC) = ((ui32SubSecondInc <<
  3063. EMAC_SUBSECINC_SSINC_S) &
  3064. EMAC_SUBSECINC_SSINC_M);
  3065. //
  3066. // Set the timestamp configuration.
  3067. //
  3068. HWREG(ui32Base + EMAC_O_TIMSTCTRL) = ui32Config;
  3069. }
  3070. //*****************************************************************************
  3071. //
  3072. //! Returns the current IEEE 1588 timestamping configuration.
  3073. //!
  3074. //! \param ui32Base is the base address of the controller.
  3075. //! \param pui32SubSecondInc points to storage that is written with the
  3076. //! current subsecond increment value for the IEEE 1588 clock.
  3077. //!
  3078. //! This function may be used to retreive the current MAC timestamping
  3079. //! configuration.
  3080. //!
  3081. //! \sa EMACTimestampConfigSet()
  3082. //!
  3083. //! \return Returns the current timestamping configuration as a logical OR of
  3084. //! the following flags:
  3085. //!
  3086. //! - \b EMAC_TS_PTP_VERSION_2 indicates that the MAC is processing PTP
  3087. //! version 2 messages. If this flag is absent, PTP version 1 messages are
  3088. //! expected.
  3089. //! - \b EMAC_TS_DIGITAL_ROLLOVER causes the clock's subsecond value to roll
  3090. //! over at 0x3BA9C9FF (999999999 decimal). In this mode, it can be considered
  3091. //! as a nanosecond counter with each digit representing 1 ns. If this flag is
  3092. //! absent, the subsecond value rolls over at 0x7FFFFFFF, effectively counting
  3093. //! increments of 0.465 ns.
  3094. //! - \b EMAC_TS_MAC_FILTER_ENABLE indicates that incoming PTP messages
  3095. //! are filtered using any of the configured MAC addresses. Messages with a
  3096. //! destination address programmed into the MAC address filter are passed,
  3097. //! others are discarded. If this flag is absent, the MAC address is ignored.
  3098. //! - \b EMAC_TS_UPDATE_FINE implements the fine update method that causes the
  3099. //! IEEE 1588 clock to advance by the the value returned in the
  3100. //! \e *pui32SubSecondInc parameter each time a carry is generated from the
  3101. //! addend accumulator register. If this flag is absent, the coarse update
  3102. //! method is in use and the clock is advanced by the \e *pui32SubSecondInc
  3103. //! value on each system clock tick.
  3104. //! - \b EMAC_TS_SYNC_ONLY indicates that timestamps are only generated for
  3105. //! SYNC messages.
  3106. //! - \b EMAC_TS_DELAYREQ_ONLY indicates that timestamps are only generated
  3107. //! for Delay_Req messages.
  3108. //! - \b EMAC_TS_ALL indicates that timestamps are generated for all
  3109. //! IEEE 1588 messages.
  3110. //! - \b EMAC_TS_SYNC_PDREQ_PDRESP timestamps only SYNC, Pdelay_Req and
  3111. //! Pdelay_Resp messages.
  3112. //! - \b EMAC_TS_DREQ_PDREQ_PDRESP indicates that timestamps are only
  3113. //! generated for Delay_Req, Pdelay_Req and Pdelay_Resp messages.
  3114. //! - \b EMAC_TS_SYNC_DELAYREQ indicates that timestamps are only generated
  3115. //! for Delay_Req messages.
  3116. //! - \b EMAC_TS_PDREQ_PDRESP indicates that timestamps are only generated
  3117. //! for Pdelay_Req and Pdelay_Resp messages.
  3118. //! - \b EMAC_TS_PROCESS_IPV4_UDP indicates that PTP packets encapsulated in
  3119. //! UDP over IPv4 packets are being processed. If absent, the MAC ignores
  3120. //! these frames.
  3121. //! - \b EMAC_TS_PROCESS_IPV6_UDP indicates that PTP packets encapsulated in
  3122. //! UDP over IPv6 packets are being processed. If absent, the MAC ignores
  3123. //! these frames.
  3124. //! - \b EMAC_TS_PROCESS_ETHERNET indicates that PTP packets encapsulated
  3125. //! directly in Ethernet frames are being processd. If absent, the MAC ignores
  3126. //! these frames.
  3127. //! - \b EMAC_TS_ALL_RX_FRAMES indicates that timestamping is enabled for all
  3128. //! frames received by the MAC, regardless of type.
  3129. //!
  3130. //! If \b EMAC_TS_ALL_RX_FRAMES and none of the options specifying subsets
  3131. //! of PTP packets to timestamp are set, the MAC is configured to timestamp
  3132. //! SYNC, Follow_Up, Delay_Req and Delay_Resp messages only.
  3133. //
  3134. //*****************************************************************************
  3135. uint32_t
  3136. EMACTimestampConfigGet(uint32_t ui32Base, uint32_t *pui32SubSecondInc)
  3137. {
  3138. //
  3139. // Parameter sanity check.
  3140. //
  3141. ASSERT(ui32Base == EMAC0_BASE);
  3142. ASSERT(pui32SubSecondInc);
  3143. //
  3144. // Read the current subsecond increment value.
  3145. //
  3146. *pui32SubSecondInc = (HWREG(ui32Base + EMAC_O_SUBSECINC) &
  3147. EMAC_SUBSECINC_SSINC_M) >> EMAC_SUBSECINC_SSINC_S;
  3148. //
  3149. // Return the current timestamp configuration.
  3150. //
  3151. return(HWREG(ui32Base + EMAC_O_TIMSTCTRL));
  3152. }
  3153. //*****************************************************************************
  3154. //
  3155. //! Enables packet timestamping and starts the system clock running.
  3156. //!
  3157. //! \param ui32Base is the base address of the controller.
  3158. //!
  3159. //! This function is used to enable the system clock used to timestamp
  3160. //! Ethernet frames and to enable that timestamping.
  3161. //!
  3162. //! \return None.
  3163. //
  3164. //*****************************************************************************
  3165. void
  3166. EMACTimestampEnable(uint32_t ui32Base)
  3167. {
  3168. //
  3169. // Parameter sanity check.
  3170. //
  3171. ASSERT(ui32Base == EMAC0_BASE);
  3172. //
  3173. // Enable IEEE 1588 timestamping.
  3174. //
  3175. HWREG(ui32Base + EMAC_O_TIMSTCTRL) |= EMAC_TIMSTCTRL_TSEN;
  3176. //
  3177. // If necessary, initialize the timestamping system. This bit self-clears
  3178. // once the system time is loaded. Only do this if initialization is not
  3179. // currently ongoing.
  3180. //
  3181. if(!(HWREG(ui32Base + EMAC_O_TIMSTCTRL) & EMAC_TIMSTCTRL_TSINIT))
  3182. {
  3183. HWREG(ui32Base + EMAC_O_TIMSTCTRL) |= EMAC_TIMSTCTRL_TSINIT;
  3184. }
  3185. }
  3186. //*****************************************************************************
  3187. //
  3188. //! Disables packet timestamping and stops the system clock.
  3189. //!
  3190. //! \param ui32Base is the base address of the controller.
  3191. //!
  3192. //! This function is used to stop the system clock used to timestamp
  3193. //! Ethernet frames and to disable timestamping.
  3194. //!
  3195. //! \return None.
  3196. //
  3197. //*****************************************************************************
  3198. void
  3199. EMACTimestampDisable(uint32_t ui32Base)
  3200. {
  3201. //
  3202. // Parameter sanity check.
  3203. //
  3204. ASSERT(ui32Base == EMAC0_BASE);
  3205. //
  3206. // Disable IEEE 1588 timestamping.
  3207. //
  3208. HWREG(ui32Base + EMAC_O_TIMSTCTRL) &= ~EMAC_TIMSTCTRL_TSEN;
  3209. }
  3210. //*****************************************************************************
  3211. //
  3212. //! Sets the current system time.
  3213. //!
  3214. //! \param ui32Base is the base address of the controller.
  3215. //! \param ui32Seconds is the seconds value of the new system clock setting.
  3216. //! \param ui32SubSeconds is the subseconds value of the new system clock
  3217. //! setting.
  3218. //!
  3219. //! This function may be used to set the current system time. The system
  3220. //! clock is set to the value passed in the \e ui32Seconds and
  3221. //! \e ui32SubSeconds parameters.
  3222. //!
  3223. //! The meaning of \e ui32SubSeconds depends on the current system time
  3224. //! configuration. If EMACTimestampConfigSet() was previously called with
  3225. //! the \e EMAC_TS_DIGITAL_ROLLOVER configuration option, each bit in the
  3226. //! \e ui32SubSeconds value represents 1 ns. If \e EMAC_TS_BINARY_ROLLOVER was
  3227. //! specified instead, a \e ui32SubSeconds bit represents 0.46 ns.
  3228. //!
  3229. //! \return None.
  3230. //
  3231. //*****************************************************************************
  3232. void
  3233. EMACTimestampSysTimeSet(uint32_t ui32Base, uint32_t ui32Seconds,
  3234. uint32_t ui32SubSeconds)
  3235. {
  3236. //
  3237. // Parameter sanity check.
  3238. //
  3239. ASSERT(ui32Base == EMAC0_BASE);
  3240. //
  3241. // Write the new time to the system time update registers.
  3242. //
  3243. HWREG(ui32Base + EMAC_O_TIMSECU) = ui32Seconds;
  3244. HWREG(ui32Base + EMAC_O_TIMNANOU) = ui32SubSeconds;
  3245. //
  3246. // Wait for any previous update to complete.
  3247. //
  3248. while(HWREG(ui32Base + EMAC_O_TIMSTCTRL) & EMAC_TIMSTCTRL_TSINIT)
  3249. {
  3250. //
  3251. // Spin for a while.
  3252. //
  3253. }
  3254. //
  3255. // Force the system clock to reset.
  3256. //
  3257. HWREG(ui32Base + EMAC_O_TIMSTCTRL) |= EMAC_TIMSTCTRL_TSINIT;
  3258. }
  3259. //*****************************************************************************
  3260. //
  3261. //! Gets the current system time.
  3262. //!
  3263. //! \param ui32Base is the base address of the controller.
  3264. //! \param pui32Seconds points to storage for the current seconds value.
  3265. //! \param pui32SubSeconds points to storage for the current subseconds value.
  3266. //!
  3267. //! This function may be used to get the current system time.
  3268. //!
  3269. //! The meaning of \e ui32SubSeconds depends on the current system time
  3270. //! configuration. If EMACTimestampConfigSet() was previously called with
  3271. //! the \e EMAC_TS_DIGITAL_ROLLOVER configuration option, each bit in the
  3272. //! \e ui32SubSeconds value represents 1 ns. If \e EMAC_TS_BINARY_ROLLOVER was
  3273. //! specified instead, a \e ui32SubSeconds bit represents 0.46 ns.
  3274. //!
  3275. //! \return None.
  3276. //
  3277. //*****************************************************************************
  3278. void
  3279. EMACTimestampSysTimeGet(uint32_t ui32Base, uint32_t *pui32Seconds,
  3280. uint32_t *pui32SubSeconds)
  3281. {
  3282. //
  3283. // Parameter sanity check.
  3284. //
  3285. ASSERT(ui32Base == EMAC0_BASE);
  3286. ASSERT(pui32Seconds);
  3287. ASSERT(pui32SubSeconds);
  3288. //
  3289. // Read the two-part system time from the seconds and nanoseconds
  3290. // registers. We do this in a way that should guard against us reading
  3291. // the registers across a nanosecond wrap.
  3292. //
  3293. do
  3294. {
  3295. *pui32Seconds = HWREG(ui32Base + EMAC_O_TIMSEC);
  3296. *pui32SubSeconds = HWREG(ui32Base + EMAC_O_TIMNANO);
  3297. }
  3298. while(*pui32SubSeconds > HWREG(ui32Base + EMAC_O_TIMNANO));
  3299. }
  3300. //*****************************************************************************
  3301. //
  3302. //! Adjusts the current system time upwards or downwards by a given amount.
  3303. //!
  3304. //! \param ui32Base is the base address of the controller.
  3305. //! \param ui32Seconds is the seconds value of the time update to apply.
  3306. //! \param ui32SubSeconds is the subseconds value of the time update to apply.
  3307. //! \param bInc defines the direction of the update.
  3308. //!
  3309. //! This function may be used to adjust the current system time either upwards
  3310. //! or downwards by a given amount. The size of the adjustment is given by
  3311. //! the \e ui32Seconds and \e ui32SubSeconds parameter and the direction
  3312. //! by the \e bInc parameter. When \e bInc is \e true, the system time is
  3313. //! advanced by the interval given. When it is \e false, the time is retarded
  3314. //! by the interval.
  3315. //!
  3316. //! The meaning of \e ui32SubSeconds depends on the current system time
  3317. //! configuration. If EMACTimestampConfigSet() was previously called with
  3318. //! the \e EMAC_TS_DIGITAL_ROLLOVER configuration option, each bit in the
  3319. //! subsecond value represents 1 ns. If \e EMAC_TS_BINARY_ROLLOVER was
  3320. //! specified instead, a subsecond bit represents 0.46 ns.
  3321. //!
  3322. //! \return None.
  3323. //
  3324. //*****************************************************************************
  3325. void
  3326. EMACTimestampSysTimeUpdate(uint32_t ui32Base, uint32_t ui32Seconds,
  3327. uint32_t ui32SubSeconds, bool bInc)
  3328. {
  3329. //
  3330. // Parameter sanity check.
  3331. //
  3332. ASSERT(ui32Base == EMAC0_BASE);
  3333. //
  3334. // Write the new time to the system time update registers.
  3335. //
  3336. HWREG(ui32Base + EMAC_O_TIMSECU) = ui32Seconds;
  3337. HWREG(ui32Base + EMAC_O_TIMNANOU) = ui32SubSeconds |
  3338. (bInc ? 0 : EMAC_TIMNANOU_ADDSUB);
  3339. //
  3340. // Wait for any previous update to complete.
  3341. //
  3342. while(HWREG(ui32Base + EMAC_O_TIMSTCTRL) & EMAC_TIMSTCTRL_TSUPDT)
  3343. {
  3344. //
  3345. // Spin for a while.
  3346. //
  3347. }
  3348. //
  3349. // Force the system clock to update by the value provided.
  3350. //
  3351. HWREG(ui32Base + EMAC_O_TIMSTCTRL) |= EMAC_TIMSTCTRL_TSUPDT;
  3352. }
  3353. //*****************************************************************************
  3354. //
  3355. //! Adjusts the system time update rate when using the fine correction method.
  3356. //!
  3357. //! \param ui32Base is the base address of the controller.
  3358. //! \param ui32Increment is the number to add to the accumulator register on
  3359. //! each tick of the 25-MHz main oscillator.
  3360. //!
  3361. //! This function is used to control the rate of update of the system time
  3362. //! when in fine update mode. Fine correction mode is selected if
  3363. //! \e EMAC_TS_UPDATE_FINE is supplied in the \e ui32Config parameter passed
  3364. //! to a previous call to EMACTimestampConfigSet(). Fine update mode is
  3365. //! typically used when synchronizing the local clock to the IEEE 1588 master
  3366. //! clock. The subsecond counter is incremented by the number passed to
  3367. //! EMACTimestampConfigSet() in the \e ui32SubSecondInc parameter each time a
  3368. //! 32-bit accumulator register generates a carry. The accumulator register is
  3369. //! incremented by the "addend" value on each main oscillator tick, and this
  3370. //! addend value is modified to allow fine control over the rate of change of
  3371. //! the timestamp counter. The addend value is calculated using the ratio of
  3372. //! the main oscillator clock rate and the desired IEEE 1588 clock rate and the
  3373. //! \e ui32SubSecondInc value is set to correspond to the desired IEEE 1588
  3374. //! clock rate.
  3375. //!
  3376. //! As an example, using digital rollover mode and a 25-MHz main oscillator
  3377. //! clock with a desired IEEE 1588 clock accuracy of 12.5 MHz, and having made
  3378. //! a previous call to EMACTimestampConfigSet() with \e ui32SubSecondInc set to
  3379. //! the 12.5-MHz clock period of 80 ns, the initial \e ui32Increment value
  3380. //! would be set to 0x80000000 to generate a carry on every second main
  3381. //! oscillator tick. Because the system time updates each time the accumulator
  3382. //! overflows, small changes in the \e ui32Increment value can be used to very
  3383. //! finely control the system time rate.
  3384. //!
  3385. //! \return None.
  3386. //!
  3387. //! \sa EMACTimestampConfigSet()
  3388. //
  3389. //*****************************************************************************
  3390. void
  3391. EMACTimestampAddendSet(uint32_t ui32Base, uint32_t ui32Increment)
  3392. {
  3393. //
  3394. // Parameter sanity check.
  3395. //
  3396. ASSERT(ui32Base == EMAC0_BASE);
  3397. HWREG(ui32Base + EMAC_O_TIMADD) = ui32Increment;
  3398. //
  3399. // Wait for any previous update to complete.
  3400. //
  3401. while(HWREG(ui32Base + EMAC_O_TIMSTCTRL) & EMAC_TIMSTCTRL_ADDREGUP)
  3402. {
  3403. //
  3404. // Spin for a while.
  3405. //
  3406. }
  3407. //
  3408. // Force the system clock to update by the value provided.
  3409. //
  3410. HWREG(ui32Base + EMAC_O_TIMSTCTRL) |= EMAC_TIMSTCTRL_ADDREGUP;
  3411. }
  3412. //*****************************************************************************
  3413. //
  3414. //! Sets the target system time at which the next Ethernet timer interrupt is
  3415. //! generated.
  3416. //!
  3417. //! \param ui32Base is the base address of the controller.
  3418. //! \param ui32Seconds is the second value of the desired target time.
  3419. //! \param ui32SubSeconds is the subseconds value of the desired target time.
  3420. //!
  3421. //! This function may be used to schedule an interrupt at some future time.
  3422. //! The time reference for the function is the IEEE 1588 time as returned by
  3423. //! EMACTimestampSysTimeGet(). To generate an interrupt when the system
  3424. //! time exceeds a given value, call this function to set the desired time,
  3425. //! then EMACTimestampTargetIntEnable() to enable the interrupt. When the
  3426. //! system time increments past the target time, an Ethernet interrupt with
  3427. //! status \b EMAC_INT_TIMESTAMP is generated.
  3428. //!
  3429. //! The accuracy of the interrupt timing depends on the Ethernet timer
  3430. //! update frequency and the subsecond increment value currently in use. The
  3431. //! interrupt is generated on the first timer increment that causes the
  3432. //! system time to be greater than or equal to the target time set.
  3433. //!
  3434. //! \return None.
  3435. //
  3436. //*****************************************************************************
  3437. void
  3438. EMACTimestampTargetSet(uint32_t ui32Base, uint32_t ui32Seconds,
  3439. uint32_t ui32SubSeconds)
  3440. {
  3441. //
  3442. // Parameter sanity check.
  3443. //
  3444. ASSERT(ui32Base == EMAC0_BASE);
  3445. //
  3446. // Wait for any previous write to complete.
  3447. //
  3448. while(HWREG(ui32Base + EMAC_O_TARGNANO) & EMAC_TARGNANO_TRGTBUSY)
  3449. {
  3450. }
  3451. //
  3452. // Write the new target time.
  3453. //
  3454. HWREG(ui32Base + EMAC_O_TARGSEC) = ui32Seconds;
  3455. HWREG(ui32Base + EMAC_O_TARGNANO) = ui32SubSeconds;
  3456. }
  3457. //*****************************************************************************
  3458. //
  3459. //! Enables the Ethernet system time interrupt.
  3460. //!
  3461. //! \param ui32Base is the base address of the controller.
  3462. //!
  3463. //! This function may be used after EMACTimestampTargetSet() to schedule an
  3464. //! interrupt at some future time. The time reference for the function is
  3465. //! the IEEE 1588 time as returned by EMACTimestampSysTimeGet(). To generate
  3466. //! an interrupt when the system time exceeds a given value, call this function
  3467. //! to set the desired time, then EMACTimestampTargetIntEnable() to enable the
  3468. //! interrupt. When the system time increments past the target time, an
  3469. //! Ethernet interrupt with status \b EMAC_INT_TIMESTAMP is generated.
  3470. //!
  3471. //! \return None.
  3472. //
  3473. //*****************************************************************************
  3474. void
  3475. EMACTimestampTargetIntEnable(uint32_t ui32Base)
  3476. {
  3477. //
  3478. // Parameter sanity check.
  3479. //
  3480. ASSERT(ui32Base == EMAC0_BASE);
  3481. //
  3482. // Set the bit to enable the timestamp target interrupt. This bit clears
  3483. // automatically when the interrupt fires after which point, you must
  3484. // set a new target time and re-enable the interrupts.
  3485. //
  3486. HWREG(ui32Base + EMAC_O_TIMSTCTRL) |= EMAC_TIMSTCTRL_INTTRIG;
  3487. }
  3488. //*****************************************************************************
  3489. //
  3490. //! Disables the Ethernet system time interrupt.
  3491. //!
  3492. //! \param ui32Base is the base address of the controller.
  3493. //!
  3494. //! This function may be used to disable any pending Ethernet system time
  3495. //! interrupt previously scheduled using calls to EMACTimestampTargetSet()
  3496. //! and EMACTimestampTargetIntEnable().
  3497. //!
  3498. //! \return None.
  3499. //
  3500. //*****************************************************************************
  3501. void
  3502. EMACTimestampTargetIntDisable(uint32_t ui32Base)
  3503. {
  3504. //
  3505. // Parameter sanity check.
  3506. //
  3507. ASSERT(ui32Base == EMAC0_BASE);
  3508. //
  3509. // Clear the bit to disable the timestamp target interrupt. This bit
  3510. // clears automatically when the interrupt fires, so it only must be
  3511. // disabled if you want to cancel a previously-set interrupt.
  3512. //
  3513. HWREG(ui32Base + EMAC_O_TIMSTCTRL) &= ~EMAC_TIMSTCTRL_INTTRIG;
  3514. }
  3515. //*****************************************************************************
  3516. //
  3517. //! Reads the status of the Ethernet system time interrupt.
  3518. //!
  3519. //! \param ui32Base is the base address of the controller.
  3520. //!
  3521. //! When an Ethernet interrupt occurs and \b EMAC_INT_TIMESTAMP is reported
  3522. //! bu EMACIntStatus(), this function must be called to read and clear the
  3523. //! timer interrupt status.
  3524. //!
  3525. //! \return The return value is the logical OR of the values
  3526. //! \b EMAC_TS_INT_TS_SEC_OVERFLOW and \b EMAC_TS_INT_TARGET_REACHED.
  3527. //!
  3528. //! - \b EMAC_TS_INT_TS_SEC_OVERFLOW indicates that the second counter in the
  3529. //! hardware timer has rolled over.
  3530. //! - \b EMAC_TS_INT_TARGET_REACHED indicates that the system time incremented
  3531. //! past the value set in an earlier call to EMACTimestampTargetSet(). When
  3532. //! this occurs, a new target time may be set and the interrupt re-enabled
  3533. //! using calls to EMACTimestampTargetSet() and
  3534. //! EMACTimestampTargetIntEnable().
  3535. //
  3536. //*****************************************************************************
  3537. uint32_t
  3538. EMACTimestampIntStatus(uint32_t ui32Base)
  3539. {
  3540. //
  3541. // Parameter sanity check.
  3542. //
  3543. ASSERT(ui32Base == EMAC0_BASE);
  3544. //
  3545. // Return the current interrupt status from the timestamp module.
  3546. //
  3547. return(HWREG(ui32Base + EMAC_O_TIMSTAT));
  3548. }
  3549. //*****************************************************************************
  3550. //
  3551. //! Configures the Ethernet MAC PPS output in simple mode.
  3552. //!
  3553. //! \param ui32Base is the base address of the controller.
  3554. //! \param ui32FreqConfig determines the frequency of the output generated on
  3555. //! the PPS pin.
  3556. //!
  3557. //! This function configures the Ethernet MAC PPS (Pulse Per Second) engine to
  3558. //! operate in its simple mode which allows the generation of a few, fixed
  3559. //! frequencies and pulse widths on the PPS pin. If more complex pulse
  3560. //! train generation is required, the MAC also provides a command-based
  3561. //! PPS control mode that can be selected by calling
  3562. //! EMACTimestampPPSCommandModeSet().
  3563. //!
  3564. //! The \e ui32FreqConfig parameter may take one of the following values:
  3565. //!
  3566. //! - \b EMAC_PPS_SINGLE_PULSE generates a single high pulse on the PPS
  3567. //! output once per second. The pulse width is the same as the system clock
  3568. //! period.
  3569. //! - \b EMAC_PPS_1HZ generates a 1Hz signal on the PPS output. This option
  3570. //! is not available if the system time subsecond counter is currently
  3571. //! configured to operate in binary rollover mode.
  3572. //! - \b EMAC_PPS_2HZ, \b EMAC_PPS_4HZ, \b EMAC_PPS_8HZ,
  3573. //! \b EMAC_PPS_16HZ, \b EMAC_PPS_32HZ, \b EMAC_PPS_64HZ,
  3574. //! \b EMAC_PPS_128HZ, \b EMAC_PPS_256HZ, \b EMAC_PPS_512HZ,
  3575. //! \b EMAC_PPS_1024HZ, \b EMAC_PPS_2048HZ, \b EMAC_PPS_4096HZ,
  3576. //! \b EMAC_PPS_8192HZ, \b EMAC_PPS_16384HZ generate the requested
  3577. //! frequency on the PPS output in both binary and digital rollover modes.
  3578. //! - \b EMAC_PPS_32768HZ generates a 32KHz signal on the PPS output. This
  3579. //! option is not available if the system time subsecond counter is currently
  3580. //! configured to operate in digital rollover mode.
  3581. //!
  3582. //! Except when \b EMAC_PPS_SINGLE_PULSE is specified, the signal generated
  3583. //! on PPS has a duty cycle of 50% when binary rollover mode is used for the
  3584. //! system time subsecond count. In digital mode, the output frequency
  3585. //! averages the value requested and is resynchronized each second. For
  3586. //! example, if \b EMAC_PPS_4HZ is selected in digital rollover mode, the
  3587. //! output generates three clocks with 50 percent duty cycle and 268 ms
  3588. //! period followed by a fourth clock of 195 ms period, 134 ms low and 61 ms high.
  3589. //!
  3590. //! \return None.
  3591. //
  3592. //*****************************************************************************
  3593. void
  3594. EMACTimestampPPSSimpleModeSet(uint32_t ui32Base, uint32_t ui32FreqConfig)
  3595. {
  3596. bool bDigital;
  3597. //
  3598. // Parameter sanity check.
  3599. //
  3600. ASSERT(ui32Base == EMAC0_BASE);
  3601. //
  3602. // Are we currently running the clock in digital or binary rollover mode?
  3603. //
  3604. bDigital = (HWREG(ui32Base + EMAC_O_TIMSTCTRL) &
  3605. EMAC_TS_DIGITAL_ROLLOVER) ? true : false;
  3606. //
  3607. // Weed out some unsupported frequencies. The hardware can't produce a
  3608. // 1Hz output when we are in binary rollover mode and can't produce a
  3609. // 32KHz output when we are digital rollover mode.
  3610. //
  3611. ASSERT(bDigital || (ui32FreqConfig != EMAC_PPS_1HZ));
  3612. ASSERT(!bDigital || (ui32FreqConfig != EMAC_PPS_32768HZ));
  3613. //
  3614. // Adjust the supplied frequency if we are currently in binary update mode
  3615. // where the control value generates an output that is twice as fast as
  3616. // in digital mode.
  3617. //
  3618. if((ui32FreqConfig != EMAC_PPS_SINGLE_PULSE) && !bDigital)
  3619. {
  3620. ui32FreqConfig--;
  3621. }
  3622. //
  3623. // Write the frequency control value to the PPS control register, clearing
  3624. // the PPSEN0 bit to ensure that the PPS engine is in simple mode and not
  3625. // waiting for a command. We also clear the TRGMODS0 field to revert to
  3626. // the default operation of the target time registers.
  3627. //
  3628. HWREG(ui32Base + EMAC_O_PPSCTRL) = ui32FreqConfig;
  3629. }
  3630. //*****************************************************************************
  3631. //
  3632. //! Configures the Ethernet MAC PPS output in command mode.
  3633. //! \param ui32Base is the base address of the controller.
  3634. //! \param ui32Config determines how the system target time is used.
  3635. //!
  3636. //! The simple mode of operation offered by the PPS (Pulse Per Second) engine
  3637. //! may be too restrictive for some applications. The second mode, however,
  3638. //! allows complex pulse trains to be generated using commands that tell the
  3639. //! engine to send individual pulses or start and stop trains if pulses. In
  3640. //! this mode, the pulse width and period may be set arbitrarily based on
  3641. //! ticks of the clock used to update the system time. Commands are triggered
  3642. //! at specific times using the target time last set using a call to
  3643. //! EMACTimestampTargetSet().
  3644. //!
  3645. //! The \e ui32Config parameter may be used to control whether the target
  3646. //! time is used to trigger commands only or can also generate an interrupt
  3647. //! to the CPU. Valid values are:
  3648. //!
  3649. //! - \b EMAC_PPS_TARGET_INT configures the target time to only raise
  3650. //! an interrupt and not to trigger any pending PPS command.
  3651. //! - \b EMAC_PPS_TARGET_PPS configures the target time to trigger a pending
  3652. //! PPS command but not raise an interrupt.
  3653. //! - \b EMAC_PPS_TARGET_BOTH configures the target time to trigger any
  3654. //! pending PPS command and also raise an interrupt.
  3655. //!
  3656. //! To use command mode, an application must call this function to enable the
  3657. //! mode, then call:
  3658. //!
  3659. //! - EMACTimestampPPSPeriodSet() to set the desired pulse width and period
  3660. //! then
  3661. //! - EMACTimestampTargetSet() to set the time at which the next command is
  3662. //! executed, and finally
  3663. //! - EMACTimestampPPSCommand() to send a command to cause the pulse or
  3664. //! pulse train to be started at the required time.
  3665. //!
  3666. //! \return None.
  3667. //
  3668. //*****************************************************************************
  3669. void
  3670. EMACTimestampPPSCommandModeSet(uint32_t ui32Base, uint32_t ui32Config)
  3671. {
  3672. //
  3673. // Parameter sanity check.
  3674. //
  3675. ASSERT(ui32Base == EMAC0_BASE);
  3676. ASSERT(!(ui32Config & (EMAC_PPS_TARGET_INT | EMAC_PPS_TARGET_PPS |
  3677. EMAC_PPS_TARGET_BOTH)));
  3678. //
  3679. // Wait for any previous command write to complete.
  3680. //
  3681. while(HWREG(ui32Base + EMAC_O_PPSCTRL) & EMAC_PPSCTRL_PPSCTRL_M)
  3682. {
  3683. //
  3684. // Wait a bit.
  3685. //
  3686. }
  3687. //
  3688. // Write the configuration value to the PPS control register, setting the
  3689. // PPSEN0 bit to ensure that the PPS engine is in command mode and
  3690. // clearing the command in the PPSCTRL field.
  3691. //
  3692. HWREG(ui32Base + EMAC_O_PPSCTRL) = (EMAC_PPSCTRL_PPSEN0 | ui32Config);
  3693. }
  3694. //*****************************************************************************
  3695. //
  3696. //! Sends a command to control the PPS output from the Ethernet MAC.
  3697. //!
  3698. //! \param ui32Base is the base address of the controller.
  3699. //! \param ui8Cmd identifies the command to be sent.
  3700. //!
  3701. //! This function may be used to send a command to the MAC PPS (Pulse Per
  3702. //! Second) controller when it is operating in command mode. Command mode
  3703. //! is selected by calling EMACTimestampPPSCommandModeSet(). Valid
  3704. //! commands are as follow:
  3705. //!
  3706. //! - \b EMAC_PPS_COMMAND_NONE indicates no command.
  3707. //! - \b EMAC_PPS_COMMAND_START_SINGLE indicates that a single high pulse
  3708. //! should be generated when the system time reaches the current target time.
  3709. //! - \b EMAC_PPS_COMMAND_START_TRAIN indicates that a train of pulses
  3710. //! should be started when the system time reaches the current target time.
  3711. //! - \b EMAC_PPS_COMMAND_CANCEL_START cancels any pending start command if
  3712. //! the system time has not yet reached the programmed target time.
  3713. //! - \b EMAC_PPS_COMMAND_STOP_AT_TIME indicates that the current pulse
  3714. //! train should be stopped when the system time reaches the current target
  3715. //! time.
  3716. //! - \b EMAC_PPS_COMMAND_STOP_NOW indicates that the current pulse train
  3717. //! should be stopped immediately.
  3718. //! - \b EMAC_PPS_COMMAND_CANCEL_STOP cancels any pending stop command if
  3719. //! the system time has not yet reached the programmed target time.
  3720. //!
  3721. //! In all cases, the width of the pulses generated is governed by the
  3722. //! \e ui32Width parameter passed to EMACTimestampPPSPeriodSet(). If a
  3723. //! command starts a train of pulses, the period of the pulses is governed
  3724. //! by the \e ui32Period parameter passed to the same function.
  3725. //! Target times associated with PPS commands are set by calling
  3726. //! EMACTimestampTargetSet().
  3727. //!
  3728. //! \return None.
  3729. //
  3730. //*****************************************************************************
  3731. void
  3732. EMACTimestampPPSCommand(uint32_t ui32Base, uint8_t ui8Cmd)
  3733. {
  3734. //
  3735. // Parameter sanity check.
  3736. //
  3737. ASSERT(ui32Base == EMAC0_BASE);
  3738. //
  3739. // Wait for any previous command write to complete.
  3740. //
  3741. while(HWREG(ui32Base + EMAC_O_PPSCTRL) & EMAC_PPSCTRL_PPSCTRL_M)
  3742. {
  3743. //
  3744. // Wait a bit.
  3745. //
  3746. }
  3747. //
  3748. // Write the command to the PPS control register.
  3749. //
  3750. HWREG(ui32Base + EMAC_O_PPSCTRL) = (EMAC_PPSCTRL_PPSEN0 | ui8Cmd);
  3751. }
  3752. //*****************************************************************************
  3753. //
  3754. //! Sets the period and width of the pulses on the Ethernet MAC PPS output.
  3755. //!
  3756. //! \param ui32Base is the base address of the controller.
  3757. //! \param ui32Period is the period of the PPS output expressed in terms of
  3758. //! system time update ticks.
  3759. //! \param ui32Width is the width of the high portion of the PPS output
  3760. //! expressed in terms of system time update ticks.
  3761. //!
  3762. //! This function may be used to control the period and duty cycle of the
  3763. //! signal output on the Ethernet MAC PPS pin when the PPS generator is
  3764. //! operating in command mode and a command to send one or more pulses has been
  3765. //! executed. Command mode is selected by calling
  3766. //! EMACTimestampPPSCommandModeSet().
  3767. //!
  3768. //! In simple mode, the PPS output signal frequency is controlled by the
  3769. //! \e ui32FreqConfig parameter passed to EMACTimestampPPSSimpleModeSet().
  3770. //!
  3771. //! The \e ui32Period and \e ui32Width parameters are expressed in terms of
  3772. //! system time update ticks. When the system time is operating in coarse
  3773. //! update mode, each tick is equivalent to the system clock. In fine update
  3774. //! mode, a tick occurs every time the 32-bit system time accumulator overflows
  3775. //! and this, in turn, is determined by the value passed to the function
  3776. //! EMACTimestampAddendSet(). Regardless of the tick source, each tick
  3777. //! increments the actual system time, queried using EMACTimestampSysTimeGet()
  3778. //! by the subsecond increment value passed in the \e ui32SubSecondInc to
  3779. //! EMACTimestampConfigSet().
  3780. //!
  3781. //! \return None.
  3782. //
  3783. //*****************************************************************************
  3784. void
  3785. EMACTimestampPPSPeriodSet(uint32_t ui32Base, uint32_t ui32Period,
  3786. uint32_t ui32Width)
  3787. {
  3788. //
  3789. // Parameter sanity check.
  3790. //
  3791. ASSERT(ui32Base == EMAC0_BASE);
  3792. //
  3793. // Write the desired PPS period and pulse width.
  3794. //
  3795. HWREG(ui32Base + EMAC_O_PPS0INTVL) = ui32Period;
  3796. HWREG(ui32Base + EMAC_O_PPS0WIDTH) = ui32Width;
  3797. }
  3798. //*****************************************************************************
  3799. //
  3800. //! Sets options related to reception of VLAN-tagged frames.
  3801. //!
  3802. //! \param ui32Base is the base address of the controller.
  3803. //! \param ui16Tag is the IEEE 802.1Q VLAN tag expected for incoming frames.
  3804. //! \param ui32Config determines how the receiver handles VLAN-tagged frames.
  3805. //!
  3806. //! This function configures the receiver's handling of IEEE 802.1Q VLAN
  3807. //! tagged frames. Incoming tagged frames are filtered using either a perfect
  3808. //! filter or a hash filter. When hash filtering is disabled, VLAN frames
  3809. //! tagged with the value of \e ui16Tag pass the filter and all others are
  3810. //! rejected. The tag comparison may involve all 16 bits or only the 12-bit
  3811. //! VLAN ID portion of the tag.
  3812. //!
  3813. //! The \e ui32Config parameter is a logical OR of the following values:
  3814. //!
  3815. //! - \b EMAC_VLAN_RX_HASH_ENABLE enables hash filtering for VLAN tags. If
  3816. //! this flag is absent, perfect filtering using the tag supplied in \e ui16Tag
  3817. //! is performed. The hash filter may be set using EMACVLANHashFilterSet(),
  3818. //! and EMACVLANHashFilterBitCalculate() may be used to determine which bits
  3819. //! to set in the filter for given VLAN tags.
  3820. //! - \b EMAC_VLAN_RX_SVLAN_ENABLE causes the receiver to recognize S-VLAN
  3821. //! (Type = 0x88A8) frames as valid VLAN-tagged frames. If absent, only
  3822. //! frames with type 0x8100 are considered valid VLAN frames.
  3823. //! - \b EMAC_VLAN_RX_INVERSE_MATCH causes the receiver to pass all VLAN
  3824. //! frames for which the tags do not match the supplied \e ui16Tag value. If
  3825. //! this flag is absent, only tagged frames matching \e ui16Tag are passed.
  3826. //! - \b EMAC_VLAN_RX_12BIT_TAG causes the receiver to compare only the
  3827. //! bottom 12 bits of \e ui16Tag when performing either perfect or hash
  3828. //! filtering of VLAN frames. If this flag is absent, all 16 bits of the frame
  3829. //! tag are examined when filtering. If this flag is set and \e ui16Tag has
  3830. //! all bottom 12 bits clear, the receiver passes all frames with types
  3831. //! 0x8100 or 0x88A8 regardless of the tag values they contain.
  3832. //!
  3833. //! \note To ensure that VLAN frames that fail the tag filter are dropped
  3834. //! by the MAC, EMACFrameFilterSet() must be called with the \b
  3835. //! EMAC_FRMFILTER_VLAN flag set in the \e ui32FilterOpts parameter. If
  3836. //! this flag is not set, failing VLAN packets are received by the
  3837. //! application, but bit 10 of RDES0 (\b EMAC_FRMFILTER_VLAN) is clear
  3838. //! indicating that the packet did not match the current VLAG tag filter.
  3839. //!
  3840. //! \sa EMACVLANRxConfigGet()
  3841. //!
  3842. //! \return None
  3843. //
  3844. //*****************************************************************************
  3845. void
  3846. EMACVLANRxConfigSet(uint32_t ui32Base, uint16_t ui16Tag, uint32_t ui32Config)
  3847. {
  3848. //
  3849. // Parameter sanity check.
  3850. //
  3851. ASSERT(ui32Base == EMAC0_BASE);
  3852. //
  3853. // Write the VLAN tag register.
  3854. //
  3855. HWREG(ui32Base + EMAC_O_VLANTG) =
  3856. ui32Config | (((uint32_t)ui16Tag) << EMAC_VLANTG_VL_S);
  3857. }
  3858. //*****************************************************************************
  3859. //
  3860. //! Returns the currently-set options related to reception of VLAN-tagged
  3861. //! frames.
  3862. //!
  3863. //! \param ui32Base is the base address of the controller.
  3864. //! \param pui16Tag points to storage which is written with the currently
  3865. //! configured VLAN tag used for perfect filtering.
  3866. //!
  3867. //! This function returns information on how the receiver is currently
  3868. //! handling IEEE 802.1Q VLAN-tagged frames.
  3869. //!
  3870. //! \sa EMACVLANRxConfigSet()
  3871. //!
  3872. //! \return Returns flags defining how VLAN-tagged frames are handled. The
  3873. //! value is a logical OR of the following flags:
  3874. //!
  3875. //! - \b EMAC_VLAN_RX_HASH_ENABLE indicates that hash filtering is enabled
  3876. //! for VLAN tags. If this flag is absent, perfect filtering using the tag
  3877. //! returned in \e *pui16Tag is performed.
  3878. //! - \b EMAC_VLAN_RX_SVLAN_ENABLE indicates that the receiver recognizes
  3879. //! S-VLAN (Type = 0x88A8) frames as valid VLAN-tagged frames. If absent, only
  3880. //! frames with type 0x8100 are considered valid VLAN frames.
  3881. //! - \b EMAC_VLAN_RX_INVERSE_MATCH indicates that the receiver passes all
  3882. //! VLAN frames for which the tags do not match the \e *pui16Tag value. If
  3883. //! this flag is absent, only tagged frames matching \e *pui16Tag are passed.
  3884. //! - \b EMAC_VLAN_RX_12BIT_TAG indicates that the receiver is comparing only
  3885. //! the bottom 12 bits of \e *pui16Tag when performing either perfect or hash
  3886. //! filtering of VLAN frames. If this flag is absent, all 16 bits of the frame
  3887. //! tag are examined when filtering. If this flag is set and \e *pui16Tag has
  3888. //! all bottom 12 bits clear, the receiver passes all frames with types
  3889. //! 0x8100 or 0x88A8 regardless of the tag values they contain.
  3890. //
  3891. //*****************************************************************************
  3892. uint32_t
  3893. EMACVLANRxConfigGet(uint32_t ui32Base, uint16_t *pui16Tag)
  3894. {
  3895. uint32_t ui32Value;
  3896. //
  3897. // Parameter sanity check.
  3898. //
  3899. ASSERT(ui32Base == EMAC0_BASE);
  3900. ASSERT(pui16Tag);
  3901. //
  3902. // Read the VLAN tag register.
  3903. //
  3904. ui32Value = HWREG(ui32Base + EMAC_O_VLANTG);
  3905. //
  3906. // Extract the VLAN tag from the register.
  3907. //
  3908. *pui16Tag = (ui32Value & EMAC_VLANTG_VL_M) >> EMAC_VLANTG_VL_S;
  3909. //
  3910. // Return the configuration flags.
  3911. //
  3912. return(ui32Value & ~EMAC_VLANTG_VL_M);
  3913. }
  3914. //*****************************************************************************
  3915. //
  3916. //! Sets options related to transmission of VLAN-tagged frames.
  3917. //!
  3918. //! \param ui32Base is the base address of the controller.
  3919. //! \param ui16Tag is the VLAN tag to be used when inserting or replacing tags
  3920. //! in transmitted frames.
  3921. //! \param ui32Config determines the VLAN-related processing performed by
  3922. //! the transmitter.
  3923. //!
  3924. //! This function is used to configure transmitter options relating to
  3925. //! IEEE 802.1Q VLAN tagging. The transmitter may be set to insert tagging
  3926. //! into untagged frames or replace existing tags with new values.
  3927. //!
  3928. //! The \e ui16Tag parameter contains the VLAN tag to be used in outgoing
  3929. //! tagged frames. The \e ui32Config parameter is a logical OR of the
  3930. //! following labels:
  3931. //!
  3932. //! - \b EMAC_VLAN_TX_SVLAN uses the S-VLAN type (0x88A8) when inserting or
  3933. //! replacing tags in transmitted frames. If this label is absent, C-VLAN
  3934. //! type (0x8100) is used.
  3935. //! - \b EMAC_VLAN_TX_USE_VLC informs the transmitter that the VLAN tag
  3936. //! handling should be defined by the VLAN control (VLC) value provided in
  3937. //! this function call. If this tag is absent, VLAN handling is controlled
  3938. //! by fields in the transmit descriptor.
  3939. //!
  3940. //! If \b EMAC_VLAN_TX_USE_VLC is set, one of the following four labels
  3941. //! must also be included to define the transmit VLAN tag handling:
  3942. //!
  3943. //! - \b EMAC_VLAN_TX_VLC_NONE instructs the transmitter to perform no VLAN
  3944. //! tag insertion, deletion or replacement.
  3945. //! - \b EMAC_VLAN_TX_VLC_DELETE instructs the transmitter to remove VLAN
  3946. //! tags from all transmitted frames that contain them. As a result, bytes
  3947. //! 13, 14, 15 and 16 are removed from all frames with types 0x8100 or 0x88A8.
  3948. //! - \b EMAC_VLAN_TX_VLC_INSERT instructs the transmitter to insert a VLAN
  3949. //! type and tag into all outgoing frames regardless of whether or not they
  3950. //! already contain a VLAN tag.
  3951. //! - \b EMAC_VLAN_TX_VLC_REPLACE instructs the transmitter to replace the
  3952. //! VLAN tag in all frames of type 0x8100 or 0x88A8 with the value provided to
  3953. //! this function in the \e ui16Tag parameter.
  3954. //!
  3955. //! \return None
  3956. //
  3957. //*****************************************************************************
  3958. void
  3959. EMACVLANTxConfigSet(uint32_t ui32Base, uint16_t ui16Tag, uint32_t ui32Config)
  3960. {
  3961. //
  3962. // Parameter sanity check.
  3963. //
  3964. ASSERT(ui32Base == EMAC0_BASE);
  3965. //
  3966. // Write the VLAN Tag Inclusion or Replacement register.
  3967. //
  3968. HWREG(ui32Base + EMAC_O_VLNINCREP) =
  3969. ui32Config | ((uint32_t)ui16Tag << EMAC_VLNINCREP_VLT_S);
  3970. }
  3971. //*****************************************************************************
  3972. //
  3973. //! Returns currently-selected options related to transmission of VLAN-tagged
  3974. //! frames.
  3975. //!
  3976. //! \param ui32Base is the base address of the controller.
  3977. //! \param pui16Tag points to storage that is written with the VLAN tag
  3978. //! currently being used for insertion or replacement.
  3979. //!
  3980. //! This function returns information on the current settings related to VLAN
  3981. //! tagging of transmitted frames.
  3982. //!
  3983. //! \sa EMACVLANTxConfigSet()
  3984. //!
  3985. //! \return Returns flags describing the current VLAN configuration relating
  3986. //! to frame transmission. The return value is a logical OR of the following
  3987. //! values:
  3988. //!
  3989. //! - \b EMAC_VLAN_TX_SVLAN indicates that the S-VLAN type (0x88A8) is
  3990. //! being used when inserting or replacing tags in transmitted frames. If
  3991. //! this label is absent, C-VLAN type (0x8100) is being used.
  3992. //! - \b EMAC_VLAN_TX_USE_VLC indicates that the transmitter is processing
  3993. //! VLAN frames according to the VLAN control (VLC) value returned here. If
  3994. //! this tag is absent, VLAN handling is controlled by fields in the transmit
  3995. //! descriptor.
  3996. //!
  3997. //! If \b EMAC_VLAN_TX_USE_VLC is returned, one of the following four labels
  3998. //! is also included to define the transmit VLAN tag handling. Note that this
  3999. //! value may be extracted from the return value using the mask \b
  4000. //! EMAC_VLAN_TX_VLC_MASK.
  4001. //!
  4002. //! - \b EMAC_VLAN_TX_VLC_NONE indicates that the transmitter is not
  4003. //! performing VLAN tag insertion, deletion or replacement.
  4004. //! - \b EMAC_VLAN_TX_VLC_DELETE indicates that the transmitter is removing
  4005. //! VLAN tags from all transmitted frames which contain them.
  4006. //! - \b EMAC_VLAN_TX_VLC_INSERT indicates that the transmitter is inserting
  4007. //! a VLAN type and tag into all outgoing frames regardless of whether or not
  4008. //! they already contain a VLAN tag.
  4009. //! - \b EMAC_VLAN_TX_VLC_REPLACE indicates that the transmitter is replacing
  4010. //! the VLAN tag in all transmitted frames of type 0x8100 or 0x88A8 with the
  4011. //! value returned in \e *pui16Tag.
  4012. //
  4013. //*****************************************************************************
  4014. uint32_t
  4015. EMACVLANTxConfigGet(uint32_t ui32Base, uint16_t *pui16Tag)
  4016. {
  4017. uint32_t ui32Value;
  4018. //
  4019. // Parameter sanity check.
  4020. //
  4021. ASSERT(ui32Base == EMAC0_BASE);
  4022. ASSERT(pui16Tag);
  4023. //
  4024. // Read the VLAN Tag Inclusion or Replacement register.
  4025. //
  4026. ui32Value = HWREG(ui32Base + EMAC_O_VLNINCREP);
  4027. //
  4028. // Extract the tag.
  4029. //
  4030. *pui16Tag = (uint16_t)((ui32Value & EMAC_VLNINCREP_VLT_M) >>
  4031. EMAC_VLNINCREP_VLT_S);
  4032. //
  4033. // Return the configuration flags.
  4034. //
  4035. return(ui32Value & ~EMAC_VLNINCREP_VLT_M);
  4036. }
  4037. //*****************************************************************************
  4038. //
  4039. //! Returns the bit number to set in the VLAN hash filter corresponding to a
  4040. //! given tag.
  4041. //!
  4042. //! \param ui16Tag is the VLAN tag for which the hash filter bit number is to
  4043. //! be determined.
  4044. //!
  4045. //! This function may be used to determine which bit in the VLAN hash filter
  4046. //! to set to describe a given 12- or 16-bit VLAN tag. The returned value is
  4047. //! a 4-bit value indicating the bit number to set within the 16-bit VLAN
  4048. //! hash filter. For example, if 0x02 is returned, this indicates that bit
  4049. //! 2 of the hash filter must be set to pass the supplied VLAN tag.
  4050. //!
  4051. //! \return Returns the bit number to set in the VLAN hash filter to describe
  4052. //! the passed tag.
  4053. //
  4054. //*****************************************************************************
  4055. uint32_t
  4056. EMACVLANHashFilterBitCalculate(uint16_t ui16Tag)
  4057. {
  4058. uint32_t ui32CRC, ui32Mask, ui32Loop;
  4059. //
  4060. // Calculate the CRC for the MAC address.
  4061. //
  4062. ui32CRC = Crc32(0xFFFFFFFF, (uint8_t *)&ui16Tag, 2);
  4063. ui32CRC ^= 0xFFFFFFFF;
  4064. //
  4065. // Determine the hash bit to use from the calculated CRC. This is the
  4066. // top 4 bits of the reversed CRC (or the bottom 4 bits of the calculated
  4067. // CRC with the bit order of those 4 bits reversed).
  4068. //
  4069. ui32Mask = 0;
  4070. //
  4071. // Reverse the order of the bottom 4 bits of the calculated CRC.
  4072. //
  4073. for(ui32Loop = 0; ui32Loop < 4; ui32Loop++)
  4074. {
  4075. ui32Mask <<= 1;
  4076. ui32Mask |= (ui32CRC & 1);
  4077. ui32CRC >>= 1;
  4078. }
  4079. //
  4080. // Return the final hash filter bit index.
  4081. //
  4082. return(ui32Mask);
  4083. }
  4084. //*****************************************************************************
  4085. //
  4086. //! Sets the hash filter used to control reception of VLAN-tagged frames.
  4087. //!
  4088. //! \param ui32Base is the base address of the controller.
  4089. //! \param ui32Hash is the hash filter value to set.
  4090. //!
  4091. //! This function allows the VLAG tag hash filter to be set. By using hash
  4092. //! filtering, several different VLAN tags can be filtered very easily at the
  4093. //! cost of some false positive results that must be removed by software.
  4094. //!
  4095. //! The hash filter value passed in \e ui32Hash may be built up by calling
  4096. //! EMACVLANHashFilterBitCalculate() for each VLAN tag that is to pass the
  4097. //! filter and then set each of the bits for which the numbers are returned by
  4098. //! that function. Care must be taken when clearing bits in the hash filter
  4099. //! due to the fact that there is a many-to-one correspondence between VLAN
  4100. //! tags and hash filter bits.
  4101. //!
  4102. //! \return None
  4103. //
  4104. //*****************************************************************************
  4105. void
  4106. EMACVLANHashFilterSet(uint32_t ui32Base, uint32_t ui32Hash)
  4107. {
  4108. //
  4109. // Parameter sanity check.
  4110. //
  4111. ASSERT(ui32Base == EMAC0_BASE);
  4112. //
  4113. // Write the VLAN Hash Table register.
  4114. //
  4115. HWREG(ui32Base + EMAC_O_VLANHASH) = ui32Hash;
  4116. }
  4117. //*****************************************************************************
  4118. //
  4119. //! Returns the current value of the hash filter used to control reception of
  4120. //! VLAN-tagged frames.
  4121. //!
  4122. //! \param ui32Base is the base address of the controller.
  4123. //!
  4124. //! This function allows the current VLAN tag hash filter value to be returned.
  4125. //! Additional VLAN tags may be added to this filter by setting the appropriate
  4126. //! bits, determined by calling EMACVLANHashFilterBitCalculate(), and then
  4127. //! calling EMACVLANHashFilterSet() to set the new filter value.
  4128. //!
  4129. //! \return Returns the current value of the VLAN hash filter.
  4130. //
  4131. //*****************************************************************************
  4132. uint32_t
  4133. EMACVLANHashFilterGet(uint32_t ui32Base)
  4134. {
  4135. //
  4136. // Parameter sanity check.
  4137. //
  4138. ASSERT(ui32Base == EMAC0_BASE);
  4139. //
  4140. // Return the VLAN Hash Table register.
  4141. //
  4142. return(HWREG(ui32Base + EMAC_O_VLANHASH));
  4143. }
  4144. //*****************************************************************************
  4145. //
  4146. //! Sets values defining up to four frames used to trigger a remote wake-up.
  4147. //!
  4148. //! \param ui32Base is the base address of the controller.
  4149. //! \param pFilter points to the structure containing remote wake-up frame
  4150. //! filter information.
  4151. //!
  4152. //! This function may be used to define up to four different frames that
  4153. //! are considered by the Ethernet MAC to be remote wake-up signals. The
  4154. //! data passed to the function describes a wake-up frame in terms of a CRC
  4155. //! calculated on up to 31 payload bytes in the frame. The actual bytes used
  4156. //! in the CRC calculation are defined by means of a bit mask where a ``1''
  4157. //! indicates that a byte in the frame should contribute to the CRC
  4158. //! calculation and a ``0'' indicates that the byte should be skipped, as well
  4159. //! as an offset from the start of the frame to the payload byte that represents
  4160. //! the first byte in the 31-byte CRC-checked sequence.
  4161. //!
  4162. //! The \e pFilter parameter points to a structure containing the information
  4163. //! necessary to set up the filters. This structure contains the following
  4164. //! fields, each of which is replicated 4 times, once for each possible wake-up
  4165. //! frame:
  4166. //!
  4167. //! - \b pui32ByteMask defines whether a given byte in the chosen 31-byte
  4168. //! sequence within the frame should contribute to the CRC calculation or not.
  4169. //! A 1 indicates that the byte should contribute to the calculation, a 0
  4170. //! causes the byte to be skipped.
  4171. //! - \b pui8Command contains flags defining whether this filter is enabled
  4172. //! and, if so, whether it refers to unicast or multicast packets. Valid
  4173. //! values are one of \b EMAC_RWU_FILTER_MULTICAST or \b
  4174. //! EMAC_RWU_FILTER_UNICAST ORed with one of \b EMAC_RWU_FILTER_ENABLE or
  4175. //! \b EMAC_RWU_FILTER_DISABLE.
  4176. //! - \b pui8Offset defines the zero-based index of the byte within the frame
  4177. //! at which CRC checking defined by \b pui32ByteMask begins.
  4178. //! Alternatively, this value can be thought of as the number of bytes in the
  4179. //! frame that the MAC skips before accumulating the CRC based on the pattern
  4180. //! in \b pui32ByteMask.
  4181. //! - \b pui16CRC provides the value of the calculated CRC for a valid remote
  4182. //! wake-up frame. If the incoming frame is processed according to the filter
  4183. //! values provided and the final CRC calculation equals this value, the
  4184. //! frame is considered to be a valid remote wake-up frame.
  4185. //!
  4186. //! Note that this filter uses CRC16 rather than CRC32 as used in frame
  4187. //! checksums. The required CRC uses a direct algorithm with polynomial 0x8005,
  4188. //! initial seed value 0xFFFF, no final XOR and reversed data order. CRCs
  4189. //! for use in this function may be determined using the online calculator
  4190. //! found at http://www.zorc.breitbandkatze.de/crc.html.
  4191. //!
  4192. //! \return None.
  4193. //
  4194. //*****************************************************************************
  4195. void
  4196. EMACRemoteWakeUpFrameFilterSet(uint32_t ui32Base,
  4197. const tEMACWakeUpFrameFilter *pFilter)
  4198. {
  4199. uint32_t *pui32Data;
  4200. uint32_t ui32Loop;
  4201. //
  4202. // Parameter sanity check.
  4203. //
  4204. ASSERT(ui32Base == EMAC0_BASE);
  4205. ASSERT(pFilter);
  4206. //
  4207. // Make sure that the internal register counter for the frame filter
  4208. // is reset. This bit automatically resets after 1 clock cycle.
  4209. //
  4210. HWREG(ui32Base + EMAC_O_PMTCTLSTAT) |= EMAC_PMTCTLSTAT_WUPFRRST;
  4211. //
  4212. // Get a word pointer to the supplied structure.
  4213. //
  4214. pui32Data = (uint32_t *)pFilter;
  4215. //
  4216. // Write the 8 words of the wake-up filter definition to the hardware.
  4217. //
  4218. for(ui32Loop = 0; ui32Loop < 8; ui32Loop++)
  4219. {
  4220. //
  4221. // Write a word of the filter definition.
  4222. //
  4223. HWREG(ui32Base + EMAC_O_RWUFF) = pui32Data[ui32Loop];
  4224. }
  4225. }
  4226. //*****************************************************************************
  4227. //
  4228. //! Returns the current remote wake-up frame filter configuration.
  4229. //!
  4230. //! \param ui32Base is the base address of the controller.
  4231. //! \param pFilter points to the structure that is written with the current
  4232. //! remote wake-up frame filter information.
  4233. //!
  4234. //! This function may be used to read the current wake-up frame filter
  4235. //! settings. The data returned by the function describes wake-up frames in
  4236. //! terms of a CRC calculated on up to 31 payload bytes in the frame. The
  4237. //! actual bytes used in the CRC calculation are defined by means of a bit mask
  4238. //! where a ``1'' indicates that a byte in the frame should contribute to the
  4239. //! CRC calculation and a ``0'' indicates that the byte should be skipped, and
  4240. //! an offset from the start of the frame to the payload byte that represents
  4241. //! the first byte in the 31-byte CRC-checked sequence.
  4242. //!
  4243. //! The \e pFilter parameter points to storage that is written with a
  4244. //! structure containing the information defining the frame filters. This
  4245. //! structure contains the following fields, each of which is replicated 4
  4246. //! times, once for each possible wake-up frame:
  4247. //!
  4248. //! - \b pui32ByteMask defines whether a given byte in the chosen 31-byte
  4249. //! sequence within the frame should contribute to the CRC calculation or not.
  4250. //! A 1 indicates that the byte should contribute to the calculation, a 0
  4251. //! causes the byte to be skipped.
  4252. //! - \b pui8Command contains flags defining whether this filter is enabled
  4253. //! and, if so, whether it refers to unicast or multicast packets. Valid
  4254. //! values are one of \b EMAC_RWU_FILTER_MULTICAST or \b
  4255. //! EMAC_RWU_FILTER_UNICAST ORed with one of \b EMAC_RWU_FILTER_ENABLE or
  4256. //! \b EMAC_RWU_FILTER_DISABLE.
  4257. //! - \b pui8Offset defines the zero-based index of the byte within the frame
  4258. //! at which CRC checking defined by \b pui32ByteMask begins.
  4259. //! Alternatively, this value can be thought of as the number of bytes in the
  4260. //! frame that the MAC skips before accumulating the CRC based on the pattern
  4261. //! in \b pui32ByteMask.
  4262. //! - \b pui16CRC provides the value of the calculated CRC for a valid remote
  4263. //! wake-up frame. If the incoming frame is processed according to the filter
  4264. //! values provided and the final CRC calculation equals this value, the
  4265. //! frame is considered to be a valid remote wake-up frame.
  4266. //!
  4267. //! Note that this filter uses CRC16 rather than CRC32 as used in frame
  4268. //! checksums.
  4269. //!
  4270. //! \return None.
  4271. //
  4272. //*****************************************************************************
  4273. void
  4274. EMACRemoteWakeUpFrameFilterGet(uint32_t ui32Base,
  4275. tEMACWakeUpFrameFilter *pFilter)
  4276. {
  4277. uint32_t *pui32Data;
  4278. uint32_t ui32Loop;
  4279. //
  4280. // Parameter sanity check.
  4281. //
  4282. ASSERT(ui32Base == EMAC0_BASE);
  4283. ASSERT(pFilter);
  4284. //
  4285. // Make sure that the internal register counter for the frame filter
  4286. // is reset. This bit automatically resets after 1 clock cycle.
  4287. //
  4288. HWREG(ui32Base + EMAC_O_PMTCTLSTAT) |= EMAC_PMTCTLSTAT_WUPFRRST;
  4289. //
  4290. // Get a word pointer to the supplied structure.
  4291. //
  4292. pui32Data = (uint32_t *)pFilter;
  4293. //
  4294. // Read the 8 words of the wake-up filter definition from the hardware.
  4295. //
  4296. for(ui32Loop = 0; ui32Loop < 8; ui32Loop++)
  4297. {
  4298. //
  4299. // Read a word of the filter definition.
  4300. //
  4301. pui32Data[ui32Loop] = HWREG(ui32Base + EMAC_O_RWUFF);
  4302. }
  4303. }
  4304. //*****************************************************************************
  4305. //
  4306. //! Sets the Ethernet MAC remote wake-up configuration.
  4307. //!
  4308. //! \param ui32Base is the base address of the controller.
  4309. //! \param ui32Flags defines which types of frame should trigger a remote
  4310. //! wake-up and allows the MAC to be put into power-down mode.
  4311. //!
  4312. //! This function allows the MAC's remote wake-up features to be configured,
  4313. //! determining which types of frame should trigger a wake-up event and
  4314. //! allowing an application to place the MAC in power-down mode. In this
  4315. //! mode, the MAC ignores all received frames until one matching a
  4316. //! configured remote wake-up frame is received, at which point the MAC
  4317. //! automatically exits power-down mode and continues to receive frames.
  4318. //!
  4319. //! The \e ui32Flags parameter is a logical OR of the following flags:
  4320. //!
  4321. //! - \b EMAC_PMT_GLOBAL_UNICAST_ENABLE instructs the MAC to wake up when any
  4322. //! unicast frame matching the MAC destination address filter is received.
  4323. //! - \b EMAC_PMT_WAKEUP_PACKET_ENABLE instructs the MAC to wake up when any
  4324. //! received frame matches the remote wake-up filter configured via a call
  4325. //! to EMACRemoteWakeUpFrameFilterSet().
  4326. //! - \b EMAC_PMT_MAGIC_PACKET_ENABLE instructs the MAC to wake up when a
  4327. //! standard Wake-on-LAN "magic packet" is received. The magic packet contains
  4328. //! 6 bytes of 0xFF followed immediately by 16 repetitions of the destination
  4329. //! MAC address.
  4330. //! - \b EMAC_PMT_POWER_DOWN instructs the MAC to enter power-down mode and
  4331. //! wait for an incoming frame matching the remote wake-up frames as described
  4332. //! by other flags and via the remote wake-up filter. This flag should only
  4333. //! set set if at least one other flag is specified to configure a wake-up
  4334. //! frame type.
  4335. //!
  4336. //! When the MAC is in power-down mode, software may exit the mode by calling
  4337. //! this function with the \b EMAC_PMT_POWER_DOWN flag absent from \e ui32Flags.
  4338. //! If a configured wake-up frame is received while in power-down mode, the
  4339. //! \b EMAC_INT_POWER_MGMNT interrupt is signaled and may be cleared by reading
  4340. //! the status using EMACPowerManagementStatusGet().
  4341. //!
  4342. //! \note While it is possible to gate the clock to the MAC while it is in
  4343. //! power-down mode, doing so prevents the reading of the registers required
  4344. //! to determine the interrupt status and also prevents power-down mode from
  4345. //! exiting via another call to this function.
  4346. //!
  4347. //! \return None.
  4348. //
  4349. //*****************************************************************************
  4350. void
  4351. EMACPowerManagementControlSet(uint32_t ui32Base, uint32_t ui32Flags)
  4352. {
  4353. uint32_t ui32Value;
  4354. //
  4355. // Parameter sanity check.
  4356. //
  4357. ASSERT(ui32Base == EMAC0_BASE);
  4358. ASSERT(~(ui32Flags & ~(EMAC_PMT_GLOBAL_UNICAST_ENABLE |
  4359. EMAC_PMT_WAKEUP_PACKET_ENABLE |
  4360. EMAC_PMT_MAGIC_PACKET_ENABLE |
  4361. EMAC_PMT_POWER_DOWN)));
  4362. //
  4363. // Read the control/status register, clear all the bits we can set, mask
  4364. // in the new values then rewrite the new register value.
  4365. //
  4366. ui32Value = HWREG(ui32Base + EMAC_O_PMTCTLSTAT);
  4367. ui32Value &= ~(EMAC_PMTCTLSTAT_GLBLUCAST | EMAC_PMTCTLSTAT_WUPFREN |
  4368. EMAC_PMTCTLSTAT_MGKPKTEN | EMAC_PMTCTLSTAT_PWRDWN);
  4369. ui32Value |= ui32Flags;
  4370. HWREG(ui32Base + EMAC_O_PMTCTLSTAT) = ui32Value;
  4371. }
  4372. //*****************************************************************************
  4373. //
  4374. //! Queries the current Ethernet MAC remote wake-up configuration.
  4375. //!
  4376. //! \param ui32Base is the base address of the controller.
  4377. //!
  4378. //! This function allows the MAC's remote wake-up settings to be queried.
  4379. //! These settings determine which types of frame should trigger a remote
  4380. //! wake-up event
  4381. //!
  4382. //! \return Returns a logical OR of the following flags:
  4383. //!
  4384. //! - \b EMAC_PMT_GLOBAL_UNICAST_ENABLE indicates that the MAC wakes up when
  4385. //! any unicast frame matching the MAC destination address filter is received.
  4386. //! - \b EMAC_PMT_WAKEUP_PACKET_ENABLE indicates that the MAC wakes up when any
  4387. //! received frame matches the remote wake-up filter configured via a call
  4388. //! to EMACRemoteWakeUpFrameFilterSet().
  4389. //! - \b EMAC_PMT_MAGIC_PACKET_ENABLE indicates that the MAC wakes up when a
  4390. //! standard Wake-on-LAN "magic packet" is received. The magic packet contains
  4391. //! 6 bytes of 0xFF followed immediately by 16 repetitions of the destination
  4392. //! MAC address.
  4393. //! - \b EMAC_PMT_POWER_DOWN indicates that the MAC is currently in power-down
  4394. //! mode and is waiting for an incoming frame matching the remote wake-up
  4395. //! frames as described by other returned flags and via the remote wake-up
  4396. //! filter.
  4397. //
  4398. //*****************************************************************************
  4399. uint32_t
  4400. EMACPowerManagementControlGet(uint32_t ui32Base)
  4401. {
  4402. //
  4403. // Parameter sanity check.
  4404. //
  4405. ASSERT(ui32Base == EMAC0_BASE);
  4406. //
  4407. // Read the control/status register and mask off the control bits to return
  4408. // them to the caller.
  4409. //
  4410. return(HWREG(ui32Base + EMAC_O_PMTCTLSTAT) &
  4411. (EMAC_PMTCTLSTAT_GLBLUCAST | EMAC_PMTCTLSTAT_WUPFREN |
  4412. EMAC_PMTCTLSTAT_MGKPKTEN | EMAC_PMTCTLSTAT_PWRDWN));
  4413. }
  4414. //*****************************************************************************
  4415. //
  4416. //! Queries the current Ethernet MAC remote wake-up status.
  4417. //!
  4418. //! \param ui32Base is the base address of the controller.
  4419. //!
  4420. //! This function returns information on the remote wake-up state of the
  4421. //! Ethernet MAC. If the MAC has been woken up since the last call, the
  4422. //! returned value indicates the type of received frame that caused the MAC
  4423. //! to exit power-down state.
  4424. //!
  4425. //! \return Returns a logical OR of the following flags:
  4426. //!
  4427. //! - \b EMAC_PMT_POWER_DOWN indicates that the MAC is currently in power-down
  4428. //! mode.
  4429. //! - \b EMAC_PMT_WAKEUP_PACKET_RECEIVED indicates that the MAC exited
  4430. //! power-down mode due to a remote wake-up frame being received. This
  4431. //! function call clears this flag.
  4432. //! - \b EMAC_PMT_MAGIC_PACKET_RECEIVED indicates that the MAC exited
  4433. //! power-down mode due to a wake-on-LAN magic packet being received. This
  4434. //! function call clears this flag.
  4435. //
  4436. //*****************************************************************************
  4437. uint32_t
  4438. EMACPowerManagementStatusGet(uint32_t ui32Base)
  4439. {
  4440. //
  4441. // Parameter sanity check.
  4442. //
  4443. ASSERT(ui32Base == EMAC0_BASE);
  4444. //
  4445. // Read the control/status register and mask off the status bits to return
  4446. // them to the caller.
  4447. //
  4448. return(HWREG(ui32Base + EMAC_O_PMTCTLSTAT) &
  4449. (EMAC_PMTCTLSTAT_WUPRX | EMAC_PMTCTLSTAT_MGKPRX |
  4450. EMAC_PMTCTLSTAT_PWRDWN));
  4451. }
  4452. //*****************************************************************************
  4453. //
  4454. // Close the Doxygen group.
  4455. //! @}
  4456. //
  4457. //*****************************************************************************