mvpp2.c 117 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190
  1. /*
  2. * Driver for Marvell PPv2 network controller for Armada 375 SoC.
  3. *
  4. * Copyright (C) 2014 Marvell
  5. *
  6. * Marcin Wojtas <mw@semihalf.com>
  7. *
  8. * U-Boot version:
  9. * Copyright (C) 2016 Stefan Roese <sr@denx.de>
  10. *
  11. * This file is licensed under the terms of the GNU General Public
  12. * License version 2. This program is licensed "as is" without any
  13. * warranty of any kind, whether express or implied.
  14. */
  15. #include <common.h>
  16. #include <dm.h>
  17. #include <dm/device-internal.h>
  18. #include <dm/lists.h>
  19. #include <net.h>
  20. #include <netdev.h>
  21. #include <config.h>
  22. #include <malloc.h>
  23. #include <asm/io.h>
  24. #include <linux/errno.h>
  25. #include <phy.h>
  26. #include <miiphy.h>
  27. #include <watchdog.h>
  28. #include <asm/arch/cpu.h>
  29. #include <asm/arch/soc.h>
  30. #include <linux/compat.h>
  31. #include <linux/mbus.h>
  32. DECLARE_GLOBAL_DATA_PTR;
  33. /* Some linux -> U-Boot compatibility stuff */
  34. #define netdev_err(dev, fmt, args...) \
  35. printf(fmt, ##args)
  36. #define netdev_warn(dev, fmt, args...) \
  37. printf(fmt, ##args)
  38. #define netdev_info(dev, fmt, args...) \
  39. printf(fmt, ##args)
  40. #define netdev_dbg(dev, fmt, args...) \
  41. printf(fmt, ##args)
  42. #define ETH_ALEN 6 /* Octets in one ethernet addr */
  43. #define __verify_pcpu_ptr(ptr) \
  44. do { \
  45. const void __percpu *__vpp_verify = (typeof((ptr) + 0))NULL; \
  46. (void)__vpp_verify; \
  47. } while (0)
  48. #define VERIFY_PERCPU_PTR(__p) \
  49. ({ \
  50. __verify_pcpu_ptr(__p); \
  51. (typeof(*(__p)) __kernel __force *)(__p); \
  52. })
  53. #define per_cpu_ptr(ptr, cpu) ({ (void)(cpu); VERIFY_PERCPU_PTR(ptr); })
  54. #define smp_processor_id() 0
  55. #define num_present_cpus() 1
  56. #define for_each_present_cpu(cpu) \
  57. for ((cpu) = 0; (cpu) < 1; (cpu)++)
  58. #define NET_SKB_PAD max(32, MVPP2_CPU_D_CACHE_LINE_SIZE)
  59. #define CONFIG_NR_CPUS 1
  60. #define ETH_HLEN ETHER_HDR_SIZE /* Total octets in header */
  61. /* 2(HW hdr) 14(MAC hdr) 4(CRC) 32(extra for cache prefetch) */
  62. #define WRAP (2 + ETH_HLEN + 4 + 32)
  63. #define MTU 1500
  64. #define RX_BUFFER_SIZE (ALIGN(MTU + WRAP, ARCH_DMA_MINALIGN))
  65. #define MVPP2_SMI_TIMEOUT 10000
  66. /* RX Fifo Registers */
  67. #define MVPP2_RX_DATA_FIFO_SIZE_REG(port) (0x00 + 4 * (port))
  68. #define MVPP2_RX_ATTR_FIFO_SIZE_REG(port) (0x20 + 4 * (port))
  69. #define MVPP2_RX_MIN_PKT_SIZE_REG 0x60
  70. #define MVPP2_RX_FIFO_INIT_REG 0x64
  71. /* RX DMA Top Registers */
  72. #define MVPP2_RX_CTRL_REG(port) (0x140 + 4 * (port))
  73. #define MVPP2_RX_LOW_LATENCY_PKT_SIZE(s) (((s) & 0xfff) << 16)
  74. #define MVPP2_RX_USE_PSEUDO_FOR_CSUM_MASK BIT(31)
  75. #define MVPP2_POOL_BUF_SIZE_REG(pool) (0x180 + 4 * (pool))
  76. #define MVPP2_POOL_BUF_SIZE_OFFSET 5
  77. #define MVPP2_RXQ_CONFIG_REG(rxq) (0x800 + 4 * (rxq))
  78. #define MVPP2_SNOOP_PKT_SIZE_MASK 0x1ff
  79. #define MVPP2_SNOOP_BUF_HDR_MASK BIT(9)
  80. #define MVPP2_RXQ_POOL_SHORT_OFFS 20
  81. #define MVPP2_RXQ_POOL_SHORT_MASK 0x700000
  82. #define MVPP2_RXQ_POOL_LONG_OFFS 24
  83. #define MVPP2_RXQ_POOL_LONG_MASK 0x7000000
  84. #define MVPP2_RXQ_PACKET_OFFSET_OFFS 28
  85. #define MVPP2_RXQ_PACKET_OFFSET_MASK 0x70000000
  86. #define MVPP2_RXQ_DISABLE_MASK BIT(31)
  87. /* Parser Registers */
  88. #define MVPP2_PRS_INIT_LOOKUP_REG 0x1000
  89. #define MVPP2_PRS_PORT_LU_MAX 0xf
  90. #define MVPP2_PRS_PORT_LU_MASK(port) (0xff << ((port) * 4))
  91. #define MVPP2_PRS_PORT_LU_VAL(port, val) ((val) << ((port) * 4))
  92. #define MVPP2_PRS_INIT_OFFS_REG(port) (0x1004 + ((port) & 4))
  93. #define MVPP2_PRS_INIT_OFF_MASK(port) (0x3f << (((port) % 4) * 8))
  94. #define MVPP2_PRS_INIT_OFF_VAL(port, val) ((val) << (((port) % 4) * 8))
  95. #define MVPP2_PRS_MAX_LOOP_REG(port) (0x100c + ((port) & 4))
  96. #define MVPP2_PRS_MAX_LOOP_MASK(port) (0xff << (((port) % 4) * 8))
  97. #define MVPP2_PRS_MAX_LOOP_VAL(port, val) ((val) << (((port) % 4) * 8))
  98. #define MVPP2_PRS_TCAM_IDX_REG 0x1100
  99. #define MVPP2_PRS_TCAM_DATA_REG(idx) (0x1104 + (idx) * 4)
  100. #define MVPP2_PRS_TCAM_INV_MASK BIT(31)
  101. #define MVPP2_PRS_SRAM_IDX_REG 0x1200
  102. #define MVPP2_PRS_SRAM_DATA_REG(idx) (0x1204 + (idx) * 4)
  103. #define MVPP2_PRS_TCAM_CTRL_REG 0x1230
  104. #define MVPP2_PRS_TCAM_EN_MASK BIT(0)
  105. /* Classifier Registers */
  106. #define MVPP2_CLS_MODE_REG 0x1800
  107. #define MVPP2_CLS_MODE_ACTIVE_MASK BIT(0)
  108. #define MVPP2_CLS_PORT_WAY_REG 0x1810
  109. #define MVPP2_CLS_PORT_WAY_MASK(port) (1 << (port))
  110. #define MVPP2_CLS_LKP_INDEX_REG 0x1814
  111. #define MVPP2_CLS_LKP_INDEX_WAY_OFFS 6
  112. #define MVPP2_CLS_LKP_TBL_REG 0x1818
  113. #define MVPP2_CLS_LKP_TBL_RXQ_MASK 0xff
  114. #define MVPP2_CLS_LKP_TBL_LOOKUP_EN_MASK BIT(25)
  115. #define MVPP2_CLS_FLOW_INDEX_REG 0x1820
  116. #define MVPP2_CLS_FLOW_TBL0_REG 0x1824
  117. #define MVPP2_CLS_FLOW_TBL1_REG 0x1828
  118. #define MVPP2_CLS_FLOW_TBL2_REG 0x182c
  119. #define MVPP2_CLS_OVERSIZE_RXQ_LOW_REG(port) (0x1980 + ((port) * 4))
  120. #define MVPP2_CLS_OVERSIZE_RXQ_LOW_BITS 3
  121. #define MVPP2_CLS_OVERSIZE_RXQ_LOW_MASK 0x7
  122. #define MVPP2_CLS_SWFWD_P2HQ_REG(port) (0x19b0 + ((port) * 4))
  123. #define MVPP2_CLS_SWFWD_PCTRL_REG 0x19d0
  124. #define MVPP2_CLS_SWFWD_PCTRL_MASK(port) (1 << (port))
  125. /* Descriptor Manager Top Registers */
  126. #define MVPP2_RXQ_NUM_REG 0x2040
  127. #define MVPP2_RXQ_DESC_ADDR_REG 0x2044
  128. #define MVPP2_RXQ_DESC_SIZE_REG 0x2048
  129. #define MVPP2_RXQ_DESC_SIZE_MASK 0x3ff0
  130. #define MVPP2_RXQ_STATUS_UPDATE_REG(rxq) (0x3000 + 4 * (rxq))
  131. #define MVPP2_RXQ_NUM_PROCESSED_OFFSET 0
  132. #define MVPP2_RXQ_NUM_NEW_OFFSET 16
  133. #define MVPP2_RXQ_STATUS_REG(rxq) (0x3400 + 4 * (rxq))
  134. #define MVPP2_RXQ_OCCUPIED_MASK 0x3fff
  135. #define MVPP2_RXQ_NON_OCCUPIED_OFFSET 16
  136. #define MVPP2_RXQ_NON_OCCUPIED_MASK 0x3fff0000
  137. #define MVPP2_RXQ_THRESH_REG 0x204c
  138. #define MVPP2_OCCUPIED_THRESH_OFFSET 0
  139. #define MVPP2_OCCUPIED_THRESH_MASK 0x3fff
  140. #define MVPP2_RXQ_INDEX_REG 0x2050
  141. #define MVPP2_TXQ_NUM_REG 0x2080
  142. #define MVPP2_TXQ_DESC_ADDR_REG 0x2084
  143. #define MVPP2_TXQ_DESC_SIZE_REG 0x2088
  144. #define MVPP2_TXQ_DESC_SIZE_MASK 0x3ff0
  145. #define MVPP2_AGGR_TXQ_UPDATE_REG 0x2090
  146. #define MVPP2_TXQ_THRESH_REG 0x2094
  147. #define MVPP2_TRANSMITTED_THRESH_OFFSET 16
  148. #define MVPP2_TRANSMITTED_THRESH_MASK 0x3fff0000
  149. #define MVPP2_TXQ_INDEX_REG 0x2098
  150. #define MVPP2_TXQ_PREF_BUF_REG 0x209c
  151. #define MVPP2_PREF_BUF_PTR(desc) ((desc) & 0xfff)
  152. #define MVPP2_PREF_BUF_SIZE_4 (BIT(12) | BIT(13))
  153. #define MVPP2_PREF_BUF_SIZE_16 (BIT(12) | BIT(14))
  154. #define MVPP2_PREF_BUF_THRESH(val) ((val) << 17)
  155. #define MVPP2_TXQ_DRAIN_EN_MASK BIT(31)
  156. #define MVPP2_TXQ_PENDING_REG 0x20a0
  157. #define MVPP2_TXQ_PENDING_MASK 0x3fff
  158. #define MVPP2_TXQ_INT_STATUS_REG 0x20a4
  159. #define MVPP2_TXQ_SENT_REG(txq) (0x3c00 + 4 * (txq))
  160. #define MVPP2_TRANSMITTED_COUNT_OFFSET 16
  161. #define MVPP2_TRANSMITTED_COUNT_MASK 0x3fff0000
  162. #define MVPP2_TXQ_RSVD_REQ_REG 0x20b0
  163. #define MVPP2_TXQ_RSVD_REQ_Q_OFFSET 16
  164. #define MVPP2_TXQ_RSVD_RSLT_REG 0x20b4
  165. #define MVPP2_TXQ_RSVD_RSLT_MASK 0x3fff
  166. #define MVPP2_TXQ_RSVD_CLR_REG 0x20b8
  167. #define MVPP2_TXQ_RSVD_CLR_OFFSET 16
  168. #define MVPP2_AGGR_TXQ_DESC_ADDR_REG(cpu) (0x2100 + 4 * (cpu))
  169. #define MVPP2_AGGR_TXQ_DESC_SIZE_REG(cpu) (0x2140 + 4 * (cpu))
  170. #define MVPP2_AGGR_TXQ_DESC_SIZE_MASK 0x3ff0
  171. #define MVPP2_AGGR_TXQ_STATUS_REG(cpu) (0x2180 + 4 * (cpu))
  172. #define MVPP2_AGGR_TXQ_PENDING_MASK 0x3fff
  173. #define MVPP2_AGGR_TXQ_INDEX_REG(cpu) (0x21c0 + 4 * (cpu))
  174. /* MBUS bridge registers */
  175. #define MVPP2_WIN_BASE(w) (0x4000 + ((w) << 2))
  176. #define MVPP2_WIN_SIZE(w) (0x4020 + ((w) << 2))
  177. #define MVPP2_WIN_REMAP(w) (0x4040 + ((w) << 2))
  178. #define MVPP2_BASE_ADDR_ENABLE 0x4060
  179. /* Interrupt Cause and Mask registers */
  180. #define MVPP2_ISR_RX_THRESHOLD_REG(rxq) (0x5200 + 4 * (rxq))
  181. #define MVPP2_ISR_RXQ_GROUP_REG(rxq) (0x5400 + 4 * (rxq))
  182. #define MVPP2_ISR_ENABLE_REG(port) (0x5420 + 4 * (port))
  183. #define MVPP2_ISR_ENABLE_INTERRUPT(mask) ((mask) & 0xffff)
  184. #define MVPP2_ISR_DISABLE_INTERRUPT(mask) (((mask) << 16) & 0xffff0000)
  185. #define MVPP2_ISR_RX_TX_CAUSE_REG(port) (0x5480 + 4 * (port))
  186. #define MVPP2_CAUSE_RXQ_OCCUP_DESC_ALL_MASK 0xffff
  187. #define MVPP2_CAUSE_TXQ_OCCUP_DESC_ALL_MASK 0xff0000
  188. #define MVPP2_CAUSE_RX_FIFO_OVERRUN_MASK BIT(24)
  189. #define MVPP2_CAUSE_FCS_ERR_MASK BIT(25)
  190. #define MVPP2_CAUSE_TX_FIFO_UNDERRUN_MASK BIT(26)
  191. #define MVPP2_CAUSE_TX_EXCEPTION_SUM_MASK BIT(29)
  192. #define MVPP2_CAUSE_RX_EXCEPTION_SUM_MASK BIT(30)
  193. #define MVPP2_CAUSE_MISC_SUM_MASK BIT(31)
  194. #define MVPP2_ISR_RX_TX_MASK_REG(port) (0x54a0 + 4 * (port))
  195. #define MVPP2_ISR_PON_RX_TX_MASK_REG 0x54bc
  196. #define MVPP2_PON_CAUSE_RXQ_OCCUP_DESC_ALL_MASK 0xffff
  197. #define MVPP2_PON_CAUSE_TXP_OCCUP_DESC_ALL_MASK 0x3fc00000
  198. #define MVPP2_PON_CAUSE_MISC_SUM_MASK BIT(31)
  199. #define MVPP2_ISR_MISC_CAUSE_REG 0x55b0
  200. /* Buffer Manager registers */
  201. #define MVPP2_BM_POOL_BASE_REG(pool) (0x6000 + ((pool) * 4))
  202. #define MVPP2_BM_POOL_BASE_ADDR_MASK 0xfffff80
  203. #define MVPP2_BM_POOL_SIZE_REG(pool) (0x6040 + ((pool) * 4))
  204. #define MVPP2_BM_POOL_SIZE_MASK 0xfff0
  205. #define MVPP2_BM_POOL_READ_PTR_REG(pool) (0x6080 + ((pool) * 4))
  206. #define MVPP2_BM_POOL_GET_READ_PTR_MASK 0xfff0
  207. #define MVPP2_BM_POOL_PTRS_NUM_REG(pool) (0x60c0 + ((pool) * 4))
  208. #define MVPP2_BM_POOL_PTRS_NUM_MASK 0xfff0
  209. #define MVPP2_BM_BPPI_READ_PTR_REG(pool) (0x6100 + ((pool) * 4))
  210. #define MVPP2_BM_BPPI_PTRS_NUM_REG(pool) (0x6140 + ((pool) * 4))
  211. #define MVPP2_BM_BPPI_PTR_NUM_MASK 0x7ff
  212. #define MVPP2_BM_BPPI_PREFETCH_FULL_MASK BIT(16)
  213. #define MVPP2_BM_POOL_CTRL_REG(pool) (0x6200 + ((pool) * 4))
  214. #define MVPP2_BM_START_MASK BIT(0)
  215. #define MVPP2_BM_STOP_MASK BIT(1)
  216. #define MVPP2_BM_STATE_MASK BIT(4)
  217. #define MVPP2_BM_LOW_THRESH_OFFS 8
  218. #define MVPP2_BM_LOW_THRESH_MASK 0x7f00
  219. #define MVPP2_BM_LOW_THRESH_VALUE(val) ((val) << \
  220. MVPP2_BM_LOW_THRESH_OFFS)
  221. #define MVPP2_BM_HIGH_THRESH_OFFS 16
  222. #define MVPP2_BM_HIGH_THRESH_MASK 0x7f0000
  223. #define MVPP2_BM_HIGH_THRESH_VALUE(val) ((val) << \
  224. MVPP2_BM_HIGH_THRESH_OFFS)
  225. #define MVPP2_BM_INTR_CAUSE_REG(pool) (0x6240 + ((pool) * 4))
  226. #define MVPP2_BM_RELEASED_DELAY_MASK BIT(0)
  227. #define MVPP2_BM_ALLOC_FAILED_MASK BIT(1)
  228. #define MVPP2_BM_BPPE_EMPTY_MASK BIT(2)
  229. #define MVPP2_BM_BPPE_FULL_MASK BIT(3)
  230. #define MVPP2_BM_AVAILABLE_BP_LOW_MASK BIT(4)
  231. #define MVPP2_BM_INTR_MASK_REG(pool) (0x6280 + ((pool) * 4))
  232. #define MVPP2_BM_PHY_ALLOC_REG(pool) (0x6400 + ((pool) * 4))
  233. #define MVPP2_BM_PHY_ALLOC_GRNTD_MASK BIT(0)
  234. #define MVPP2_BM_VIRT_ALLOC_REG 0x6440
  235. #define MVPP2_BM_PHY_RLS_REG(pool) (0x6480 + ((pool) * 4))
  236. #define MVPP2_BM_PHY_RLS_MC_BUFF_MASK BIT(0)
  237. #define MVPP2_BM_PHY_RLS_PRIO_EN_MASK BIT(1)
  238. #define MVPP2_BM_PHY_RLS_GRNTD_MASK BIT(2)
  239. #define MVPP2_BM_VIRT_RLS_REG 0x64c0
  240. #define MVPP2_BM_MC_RLS_REG 0x64c4
  241. #define MVPP2_BM_MC_ID_MASK 0xfff
  242. #define MVPP2_BM_FORCE_RELEASE_MASK BIT(12)
  243. /* TX Scheduler registers */
  244. #define MVPP2_TXP_SCHED_PORT_INDEX_REG 0x8000
  245. #define MVPP2_TXP_SCHED_Q_CMD_REG 0x8004
  246. #define MVPP2_TXP_SCHED_ENQ_MASK 0xff
  247. #define MVPP2_TXP_SCHED_DISQ_OFFSET 8
  248. #define MVPP2_TXP_SCHED_CMD_1_REG 0x8010
  249. #define MVPP2_TXP_SCHED_PERIOD_REG 0x8018
  250. #define MVPP2_TXP_SCHED_MTU_REG 0x801c
  251. #define MVPP2_TXP_MTU_MAX 0x7FFFF
  252. #define MVPP2_TXP_SCHED_REFILL_REG 0x8020
  253. #define MVPP2_TXP_REFILL_TOKENS_ALL_MASK 0x7ffff
  254. #define MVPP2_TXP_REFILL_PERIOD_ALL_MASK 0x3ff00000
  255. #define MVPP2_TXP_REFILL_PERIOD_MASK(v) ((v) << 20)
  256. #define MVPP2_TXP_SCHED_TOKEN_SIZE_REG 0x8024
  257. #define MVPP2_TXP_TOKEN_SIZE_MAX 0xffffffff
  258. #define MVPP2_TXQ_SCHED_REFILL_REG(q) (0x8040 + ((q) << 2))
  259. #define MVPP2_TXQ_REFILL_TOKENS_ALL_MASK 0x7ffff
  260. #define MVPP2_TXQ_REFILL_PERIOD_ALL_MASK 0x3ff00000
  261. #define MVPP2_TXQ_REFILL_PERIOD_MASK(v) ((v) << 20)
  262. #define MVPP2_TXQ_SCHED_TOKEN_SIZE_REG(q) (0x8060 + ((q) << 2))
  263. #define MVPP2_TXQ_TOKEN_SIZE_MAX 0x7fffffff
  264. #define MVPP2_TXQ_SCHED_TOKEN_CNTR_REG(q) (0x8080 + ((q) << 2))
  265. #define MVPP2_TXQ_TOKEN_CNTR_MAX 0xffffffff
  266. /* TX general registers */
  267. #define MVPP2_TX_SNOOP_REG 0x8800
  268. #define MVPP2_TX_PORT_FLUSH_REG 0x8810
  269. #define MVPP2_TX_PORT_FLUSH_MASK(port) (1 << (port))
  270. /* LMS registers */
  271. #define MVPP2_SRC_ADDR_MIDDLE 0x24
  272. #define MVPP2_SRC_ADDR_HIGH 0x28
  273. #define MVPP2_PHY_AN_CFG0_REG 0x34
  274. #define MVPP2_PHY_AN_STOP_SMI0_MASK BIT(7)
  275. #define MVPP2_MIB_COUNTERS_BASE(port) (0x1000 + ((port) >> 1) * \
  276. 0x400 + (port) * 0x400)
  277. #define MVPP2_MIB_LATE_COLLISION 0x7c
  278. #define MVPP2_ISR_SUM_MASK_REG 0x220c
  279. #define MVPP2_MNG_EXTENDED_GLOBAL_CTRL_REG 0x305c
  280. #define MVPP2_EXT_GLOBAL_CTRL_DEFAULT 0x27
  281. /* Per-port registers */
  282. #define MVPP2_GMAC_CTRL_0_REG 0x0
  283. #define MVPP2_GMAC_PORT_EN_MASK BIT(0)
  284. #define MVPP2_GMAC_MAX_RX_SIZE_OFFS 2
  285. #define MVPP2_GMAC_MAX_RX_SIZE_MASK 0x7ffc
  286. #define MVPP2_GMAC_MIB_CNTR_EN_MASK BIT(15)
  287. #define MVPP2_GMAC_CTRL_1_REG 0x4
  288. #define MVPP2_GMAC_PERIODIC_XON_EN_MASK BIT(1)
  289. #define MVPP2_GMAC_GMII_LB_EN_MASK BIT(5)
  290. #define MVPP2_GMAC_PCS_LB_EN_BIT 6
  291. #define MVPP2_GMAC_PCS_LB_EN_MASK BIT(6)
  292. #define MVPP2_GMAC_SA_LOW_OFFS 7
  293. #define MVPP2_GMAC_CTRL_2_REG 0x8
  294. #define MVPP2_GMAC_INBAND_AN_MASK BIT(0)
  295. #define MVPP2_GMAC_PCS_ENABLE_MASK BIT(3)
  296. #define MVPP2_GMAC_PORT_RGMII_MASK BIT(4)
  297. #define MVPP2_GMAC_PORT_RESET_MASK BIT(6)
  298. #define MVPP2_GMAC_AUTONEG_CONFIG 0xc
  299. #define MVPP2_GMAC_FORCE_LINK_DOWN BIT(0)
  300. #define MVPP2_GMAC_FORCE_LINK_PASS BIT(1)
  301. #define MVPP2_GMAC_CONFIG_MII_SPEED BIT(5)
  302. #define MVPP2_GMAC_CONFIG_GMII_SPEED BIT(6)
  303. #define MVPP2_GMAC_AN_SPEED_EN BIT(7)
  304. #define MVPP2_GMAC_FC_ADV_EN BIT(9)
  305. #define MVPP2_GMAC_CONFIG_FULL_DUPLEX BIT(12)
  306. #define MVPP2_GMAC_AN_DUPLEX_EN BIT(13)
  307. #define MVPP2_GMAC_PORT_FIFO_CFG_1_REG 0x1c
  308. #define MVPP2_GMAC_TX_FIFO_MIN_TH_OFFS 6
  309. #define MVPP2_GMAC_TX_FIFO_MIN_TH_ALL_MASK 0x1fc0
  310. #define MVPP2_GMAC_TX_FIFO_MIN_TH_MASK(v) (((v) << 6) & \
  311. MVPP2_GMAC_TX_FIFO_MIN_TH_ALL_MASK)
  312. #define MVPP2_CAUSE_TXQ_SENT_DESC_ALL_MASK 0xff
  313. /* Descriptor ring Macros */
  314. #define MVPP2_QUEUE_NEXT_DESC(q, index) \
  315. (((index) < (q)->last_desc) ? ((index) + 1) : 0)
  316. /* SMI: 0xc0054 -> offset 0x54 to lms_base */
  317. #define MVPP2_SMI 0x0054
  318. #define MVPP2_PHY_REG_MASK 0x1f
  319. /* SMI register fields */
  320. #define MVPP2_SMI_DATA_OFFS 0 /* Data */
  321. #define MVPP2_SMI_DATA_MASK (0xffff << MVPP2_SMI_DATA_OFFS)
  322. #define MVPP2_SMI_DEV_ADDR_OFFS 16 /* PHY device address */
  323. #define MVPP2_SMI_REG_ADDR_OFFS 21 /* PHY device reg addr*/
  324. #define MVPP2_SMI_OPCODE_OFFS 26 /* Write/Read opcode */
  325. #define MVPP2_SMI_OPCODE_READ (1 << MVPP2_SMI_OPCODE_OFFS)
  326. #define MVPP2_SMI_READ_VALID (1 << 27) /* Read Valid */
  327. #define MVPP2_SMI_BUSY (1 << 28) /* Busy */
  328. #define MVPP2_PHY_ADDR_MASK 0x1f
  329. #define MVPP2_PHY_REG_MASK 0x1f
  330. /* Various constants */
  331. /* Coalescing */
  332. #define MVPP2_TXDONE_COAL_PKTS_THRESH 15
  333. #define MVPP2_TXDONE_HRTIMER_PERIOD_NS 1000000UL
  334. #define MVPP2_RX_COAL_PKTS 32
  335. #define MVPP2_RX_COAL_USEC 100
  336. /* The two bytes Marvell header. Either contains a special value used
  337. * by Marvell switches when a specific hardware mode is enabled (not
  338. * supported by this driver) or is filled automatically by zeroes on
  339. * the RX side. Those two bytes being at the front of the Ethernet
  340. * header, they allow to have the IP header aligned on a 4 bytes
  341. * boundary automatically: the hardware skips those two bytes on its
  342. * own.
  343. */
  344. #define MVPP2_MH_SIZE 2
  345. #define MVPP2_ETH_TYPE_LEN 2
  346. #define MVPP2_PPPOE_HDR_SIZE 8
  347. #define MVPP2_VLAN_TAG_LEN 4
  348. /* Lbtd 802.3 type */
  349. #define MVPP2_IP_LBDT_TYPE 0xfffa
  350. #define MVPP2_CPU_D_CACHE_LINE_SIZE 32
  351. #define MVPP2_TX_CSUM_MAX_SIZE 9800
  352. /* Timeout constants */
  353. #define MVPP2_TX_DISABLE_TIMEOUT_MSEC 1000
  354. #define MVPP2_TX_PENDING_TIMEOUT_MSEC 1000
  355. #define MVPP2_TX_MTU_MAX 0x7ffff
  356. /* Maximum number of T-CONTs of PON port */
  357. #define MVPP2_MAX_TCONT 16
  358. /* Maximum number of supported ports */
  359. #define MVPP2_MAX_PORTS 4
  360. /* Maximum number of TXQs used by single port */
  361. #define MVPP2_MAX_TXQ 8
  362. /* Maximum number of RXQs used by single port */
  363. #define MVPP2_MAX_RXQ 8
  364. /* Default number of TXQs in use */
  365. #define MVPP2_DEFAULT_TXQ 1
  366. /* Dfault number of RXQs in use */
  367. #define MVPP2_DEFAULT_RXQ 1
  368. #define CONFIG_MV_ETH_RXQ 8 /* increment by 8 */
  369. /* Total number of RXQs available to all ports */
  370. #define MVPP2_RXQ_TOTAL_NUM (MVPP2_MAX_PORTS * MVPP2_MAX_RXQ)
  371. /* Max number of Rx descriptors */
  372. #define MVPP2_MAX_RXD 16
  373. /* Max number of Tx descriptors */
  374. #define MVPP2_MAX_TXD 16
  375. /* Amount of Tx descriptors that can be reserved at once by CPU */
  376. #define MVPP2_CPU_DESC_CHUNK 64
  377. /* Max number of Tx descriptors in each aggregated queue */
  378. #define MVPP2_AGGR_TXQ_SIZE 256
  379. /* Descriptor aligned size */
  380. #define MVPP2_DESC_ALIGNED_SIZE 32
  381. /* Descriptor alignment mask */
  382. #define MVPP2_TX_DESC_ALIGN (MVPP2_DESC_ALIGNED_SIZE - 1)
  383. /* RX FIFO constants */
  384. #define MVPP2_RX_FIFO_PORT_DATA_SIZE 0x2000
  385. #define MVPP2_RX_FIFO_PORT_ATTR_SIZE 0x80
  386. #define MVPP2_RX_FIFO_PORT_MIN_PKT 0x80
  387. /* RX buffer constants */
  388. #define MVPP2_SKB_SHINFO_SIZE \
  389. 0
  390. #define MVPP2_RX_PKT_SIZE(mtu) \
  391. ALIGN((mtu) + MVPP2_MH_SIZE + MVPP2_VLAN_TAG_LEN + \
  392. ETH_HLEN + ETH_FCS_LEN, MVPP2_CPU_D_CACHE_LINE_SIZE)
  393. #define MVPP2_RX_BUF_SIZE(pkt_size) ((pkt_size) + NET_SKB_PAD)
  394. #define MVPP2_RX_TOTAL_SIZE(buf_size) ((buf_size) + MVPP2_SKB_SHINFO_SIZE)
  395. #define MVPP2_RX_MAX_PKT_SIZE(total_size) \
  396. ((total_size) - NET_SKB_PAD - MVPP2_SKB_SHINFO_SIZE)
  397. #define MVPP2_BIT_TO_BYTE(bit) ((bit) / 8)
  398. /* IPv6 max L3 address size */
  399. #define MVPP2_MAX_L3_ADDR_SIZE 16
  400. /* Port flags */
  401. #define MVPP2_F_LOOPBACK BIT(0)
  402. /* Marvell tag types */
  403. enum mvpp2_tag_type {
  404. MVPP2_TAG_TYPE_NONE = 0,
  405. MVPP2_TAG_TYPE_MH = 1,
  406. MVPP2_TAG_TYPE_DSA = 2,
  407. MVPP2_TAG_TYPE_EDSA = 3,
  408. MVPP2_TAG_TYPE_VLAN = 4,
  409. MVPP2_TAG_TYPE_LAST = 5
  410. };
  411. /* Parser constants */
  412. #define MVPP2_PRS_TCAM_SRAM_SIZE 256
  413. #define MVPP2_PRS_TCAM_WORDS 6
  414. #define MVPP2_PRS_SRAM_WORDS 4
  415. #define MVPP2_PRS_FLOW_ID_SIZE 64
  416. #define MVPP2_PRS_FLOW_ID_MASK 0x3f
  417. #define MVPP2_PRS_TCAM_ENTRY_INVALID 1
  418. #define MVPP2_PRS_TCAM_DSA_TAGGED_BIT BIT(5)
  419. #define MVPP2_PRS_IPV4_HEAD 0x40
  420. #define MVPP2_PRS_IPV4_HEAD_MASK 0xf0
  421. #define MVPP2_PRS_IPV4_MC 0xe0
  422. #define MVPP2_PRS_IPV4_MC_MASK 0xf0
  423. #define MVPP2_PRS_IPV4_BC_MASK 0xff
  424. #define MVPP2_PRS_IPV4_IHL 0x5
  425. #define MVPP2_PRS_IPV4_IHL_MASK 0xf
  426. #define MVPP2_PRS_IPV6_MC 0xff
  427. #define MVPP2_PRS_IPV6_MC_MASK 0xff
  428. #define MVPP2_PRS_IPV6_HOP_MASK 0xff
  429. #define MVPP2_PRS_TCAM_PROTO_MASK 0xff
  430. #define MVPP2_PRS_TCAM_PROTO_MASK_L 0x3f
  431. #define MVPP2_PRS_DBL_VLANS_MAX 100
  432. /* Tcam structure:
  433. * - lookup ID - 4 bits
  434. * - port ID - 1 byte
  435. * - additional information - 1 byte
  436. * - header data - 8 bytes
  437. * The fields are represented by MVPP2_PRS_TCAM_DATA_REG(5)->(0).
  438. */
  439. #define MVPP2_PRS_AI_BITS 8
  440. #define MVPP2_PRS_PORT_MASK 0xff
  441. #define MVPP2_PRS_LU_MASK 0xf
  442. #define MVPP2_PRS_TCAM_DATA_BYTE(offs) \
  443. (((offs) - ((offs) % 2)) * 2 + ((offs) % 2))
  444. #define MVPP2_PRS_TCAM_DATA_BYTE_EN(offs) \
  445. (((offs) * 2) - ((offs) % 2) + 2)
  446. #define MVPP2_PRS_TCAM_AI_BYTE 16
  447. #define MVPP2_PRS_TCAM_PORT_BYTE 17
  448. #define MVPP2_PRS_TCAM_LU_BYTE 20
  449. #define MVPP2_PRS_TCAM_EN_OFFS(offs) ((offs) + 2)
  450. #define MVPP2_PRS_TCAM_INV_WORD 5
  451. /* Tcam entries ID */
  452. #define MVPP2_PE_DROP_ALL 0
  453. #define MVPP2_PE_FIRST_FREE_TID 1
  454. #define MVPP2_PE_LAST_FREE_TID (MVPP2_PRS_TCAM_SRAM_SIZE - 31)
  455. #define MVPP2_PE_IP6_EXT_PROTO_UN (MVPP2_PRS_TCAM_SRAM_SIZE - 30)
  456. #define MVPP2_PE_MAC_MC_IP6 (MVPP2_PRS_TCAM_SRAM_SIZE - 29)
  457. #define MVPP2_PE_IP6_ADDR_UN (MVPP2_PRS_TCAM_SRAM_SIZE - 28)
  458. #define MVPP2_PE_IP4_ADDR_UN (MVPP2_PRS_TCAM_SRAM_SIZE - 27)
  459. #define MVPP2_PE_LAST_DEFAULT_FLOW (MVPP2_PRS_TCAM_SRAM_SIZE - 26)
  460. #define MVPP2_PE_FIRST_DEFAULT_FLOW (MVPP2_PRS_TCAM_SRAM_SIZE - 19)
  461. #define MVPP2_PE_EDSA_TAGGED (MVPP2_PRS_TCAM_SRAM_SIZE - 18)
  462. #define MVPP2_PE_EDSA_UNTAGGED (MVPP2_PRS_TCAM_SRAM_SIZE - 17)
  463. #define MVPP2_PE_DSA_TAGGED (MVPP2_PRS_TCAM_SRAM_SIZE - 16)
  464. #define MVPP2_PE_DSA_UNTAGGED (MVPP2_PRS_TCAM_SRAM_SIZE - 15)
  465. #define MVPP2_PE_ETYPE_EDSA_TAGGED (MVPP2_PRS_TCAM_SRAM_SIZE - 14)
  466. #define MVPP2_PE_ETYPE_EDSA_UNTAGGED (MVPP2_PRS_TCAM_SRAM_SIZE - 13)
  467. #define MVPP2_PE_ETYPE_DSA_TAGGED (MVPP2_PRS_TCAM_SRAM_SIZE - 12)
  468. #define MVPP2_PE_ETYPE_DSA_UNTAGGED (MVPP2_PRS_TCAM_SRAM_SIZE - 11)
  469. #define MVPP2_PE_MH_DEFAULT (MVPP2_PRS_TCAM_SRAM_SIZE - 10)
  470. #define MVPP2_PE_DSA_DEFAULT (MVPP2_PRS_TCAM_SRAM_SIZE - 9)
  471. #define MVPP2_PE_IP6_PROTO_UN (MVPP2_PRS_TCAM_SRAM_SIZE - 8)
  472. #define MVPP2_PE_IP4_PROTO_UN (MVPP2_PRS_TCAM_SRAM_SIZE - 7)
  473. #define MVPP2_PE_ETH_TYPE_UN (MVPP2_PRS_TCAM_SRAM_SIZE - 6)
  474. #define MVPP2_PE_VLAN_DBL (MVPP2_PRS_TCAM_SRAM_SIZE - 5)
  475. #define MVPP2_PE_VLAN_NONE (MVPP2_PRS_TCAM_SRAM_SIZE - 4)
  476. #define MVPP2_PE_MAC_MC_ALL (MVPP2_PRS_TCAM_SRAM_SIZE - 3)
  477. #define MVPP2_PE_MAC_PROMISCUOUS (MVPP2_PRS_TCAM_SRAM_SIZE - 2)
  478. #define MVPP2_PE_MAC_NON_PROMISCUOUS (MVPP2_PRS_TCAM_SRAM_SIZE - 1)
  479. /* Sram structure
  480. * The fields are represented by MVPP2_PRS_TCAM_DATA_REG(3)->(0).
  481. */
  482. #define MVPP2_PRS_SRAM_RI_OFFS 0
  483. #define MVPP2_PRS_SRAM_RI_WORD 0
  484. #define MVPP2_PRS_SRAM_RI_CTRL_OFFS 32
  485. #define MVPP2_PRS_SRAM_RI_CTRL_WORD 1
  486. #define MVPP2_PRS_SRAM_RI_CTRL_BITS 32
  487. #define MVPP2_PRS_SRAM_SHIFT_OFFS 64
  488. #define MVPP2_PRS_SRAM_SHIFT_SIGN_BIT 72
  489. #define MVPP2_PRS_SRAM_UDF_OFFS 73
  490. #define MVPP2_PRS_SRAM_UDF_BITS 8
  491. #define MVPP2_PRS_SRAM_UDF_MASK 0xff
  492. #define MVPP2_PRS_SRAM_UDF_SIGN_BIT 81
  493. #define MVPP2_PRS_SRAM_UDF_TYPE_OFFS 82
  494. #define MVPP2_PRS_SRAM_UDF_TYPE_MASK 0x7
  495. #define MVPP2_PRS_SRAM_UDF_TYPE_L3 1
  496. #define MVPP2_PRS_SRAM_UDF_TYPE_L4 4
  497. #define MVPP2_PRS_SRAM_OP_SEL_SHIFT_OFFS 85
  498. #define MVPP2_PRS_SRAM_OP_SEL_SHIFT_MASK 0x3
  499. #define MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD 1
  500. #define MVPP2_PRS_SRAM_OP_SEL_SHIFT_IP4_ADD 2
  501. #define MVPP2_PRS_SRAM_OP_SEL_SHIFT_IP6_ADD 3
  502. #define MVPP2_PRS_SRAM_OP_SEL_UDF_OFFS 87
  503. #define MVPP2_PRS_SRAM_OP_SEL_UDF_BITS 2
  504. #define MVPP2_PRS_SRAM_OP_SEL_UDF_MASK 0x3
  505. #define MVPP2_PRS_SRAM_OP_SEL_UDF_ADD 0
  506. #define MVPP2_PRS_SRAM_OP_SEL_UDF_IP4_ADD 2
  507. #define MVPP2_PRS_SRAM_OP_SEL_UDF_IP6_ADD 3
  508. #define MVPP2_PRS_SRAM_OP_SEL_BASE_OFFS 89
  509. #define MVPP2_PRS_SRAM_AI_OFFS 90
  510. #define MVPP2_PRS_SRAM_AI_CTRL_OFFS 98
  511. #define MVPP2_PRS_SRAM_AI_CTRL_BITS 8
  512. #define MVPP2_PRS_SRAM_AI_MASK 0xff
  513. #define MVPP2_PRS_SRAM_NEXT_LU_OFFS 106
  514. #define MVPP2_PRS_SRAM_NEXT_LU_MASK 0xf
  515. #define MVPP2_PRS_SRAM_LU_DONE_BIT 110
  516. #define MVPP2_PRS_SRAM_LU_GEN_BIT 111
  517. /* Sram result info bits assignment */
  518. #define MVPP2_PRS_RI_MAC_ME_MASK 0x1
  519. #define MVPP2_PRS_RI_DSA_MASK 0x2
  520. #define MVPP2_PRS_RI_VLAN_MASK 0xc
  521. #define MVPP2_PRS_RI_VLAN_NONE ~(BIT(2) | BIT(3))
  522. #define MVPP2_PRS_RI_VLAN_SINGLE BIT(2)
  523. #define MVPP2_PRS_RI_VLAN_DOUBLE BIT(3)
  524. #define MVPP2_PRS_RI_VLAN_TRIPLE (BIT(2) | BIT(3))
  525. #define MVPP2_PRS_RI_CPU_CODE_MASK 0x70
  526. #define MVPP2_PRS_RI_CPU_CODE_RX_SPEC BIT(4)
  527. #define MVPP2_PRS_RI_L2_CAST_MASK 0x600
  528. #define MVPP2_PRS_RI_L2_UCAST ~(BIT(9) | BIT(10))
  529. #define MVPP2_PRS_RI_L2_MCAST BIT(9)
  530. #define MVPP2_PRS_RI_L2_BCAST BIT(10)
  531. #define MVPP2_PRS_RI_PPPOE_MASK 0x800
  532. #define MVPP2_PRS_RI_L3_PROTO_MASK 0x7000
  533. #define MVPP2_PRS_RI_L3_UN ~(BIT(12) | BIT(13) | BIT(14))
  534. #define MVPP2_PRS_RI_L3_IP4 BIT(12)
  535. #define MVPP2_PRS_RI_L3_IP4_OPT BIT(13)
  536. #define MVPP2_PRS_RI_L3_IP4_OTHER (BIT(12) | BIT(13))
  537. #define MVPP2_PRS_RI_L3_IP6 BIT(14)
  538. #define MVPP2_PRS_RI_L3_IP6_EXT (BIT(12) | BIT(14))
  539. #define MVPP2_PRS_RI_L3_ARP (BIT(13) | BIT(14))
  540. #define MVPP2_PRS_RI_L3_ADDR_MASK 0x18000
  541. #define MVPP2_PRS_RI_L3_UCAST ~(BIT(15) | BIT(16))
  542. #define MVPP2_PRS_RI_L3_MCAST BIT(15)
  543. #define MVPP2_PRS_RI_L3_BCAST (BIT(15) | BIT(16))
  544. #define MVPP2_PRS_RI_IP_FRAG_MASK 0x20000
  545. #define MVPP2_PRS_RI_UDF3_MASK 0x300000
  546. #define MVPP2_PRS_RI_UDF3_RX_SPECIAL BIT(21)
  547. #define MVPP2_PRS_RI_L4_PROTO_MASK 0x1c00000
  548. #define MVPP2_PRS_RI_L4_TCP BIT(22)
  549. #define MVPP2_PRS_RI_L4_UDP BIT(23)
  550. #define MVPP2_PRS_RI_L4_OTHER (BIT(22) | BIT(23))
  551. #define MVPP2_PRS_RI_UDF7_MASK 0x60000000
  552. #define MVPP2_PRS_RI_UDF7_IP6_LITE BIT(29)
  553. #define MVPP2_PRS_RI_DROP_MASK 0x80000000
  554. /* Sram additional info bits assignment */
  555. #define MVPP2_PRS_IPV4_DIP_AI_BIT BIT(0)
  556. #define MVPP2_PRS_IPV6_NO_EXT_AI_BIT BIT(0)
  557. #define MVPP2_PRS_IPV6_EXT_AI_BIT BIT(1)
  558. #define MVPP2_PRS_IPV6_EXT_AH_AI_BIT BIT(2)
  559. #define MVPP2_PRS_IPV6_EXT_AH_LEN_AI_BIT BIT(3)
  560. #define MVPP2_PRS_IPV6_EXT_AH_L4_AI_BIT BIT(4)
  561. #define MVPP2_PRS_SINGLE_VLAN_AI 0
  562. #define MVPP2_PRS_DBL_VLAN_AI_BIT BIT(7)
  563. /* DSA/EDSA type */
  564. #define MVPP2_PRS_TAGGED true
  565. #define MVPP2_PRS_UNTAGGED false
  566. #define MVPP2_PRS_EDSA true
  567. #define MVPP2_PRS_DSA false
  568. /* MAC entries, shadow udf */
  569. enum mvpp2_prs_udf {
  570. MVPP2_PRS_UDF_MAC_DEF,
  571. MVPP2_PRS_UDF_MAC_RANGE,
  572. MVPP2_PRS_UDF_L2_DEF,
  573. MVPP2_PRS_UDF_L2_DEF_COPY,
  574. MVPP2_PRS_UDF_L2_USER,
  575. };
  576. /* Lookup ID */
  577. enum mvpp2_prs_lookup {
  578. MVPP2_PRS_LU_MH,
  579. MVPP2_PRS_LU_MAC,
  580. MVPP2_PRS_LU_DSA,
  581. MVPP2_PRS_LU_VLAN,
  582. MVPP2_PRS_LU_L2,
  583. MVPP2_PRS_LU_PPPOE,
  584. MVPP2_PRS_LU_IP4,
  585. MVPP2_PRS_LU_IP6,
  586. MVPP2_PRS_LU_FLOWS,
  587. MVPP2_PRS_LU_LAST,
  588. };
  589. /* L3 cast enum */
  590. enum mvpp2_prs_l3_cast {
  591. MVPP2_PRS_L3_UNI_CAST,
  592. MVPP2_PRS_L3_MULTI_CAST,
  593. MVPP2_PRS_L3_BROAD_CAST
  594. };
  595. /* Classifier constants */
  596. #define MVPP2_CLS_FLOWS_TBL_SIZE 512
  597. #define MVPP2_CLS_FLOWS_TBL_DATA_WORDS 3
  598. #define MVPP2_CLS_LKP_TBL_SIZE 64
  599. /* BM constants */
  600. #define MVPP2_BM_POOLS_NUM 1
  601. #define MVPP2_BM_LONG_BUF_NUM 16
  602. #define MVPP2_BM_SHORT_BUF_NUM 16
  603. #define MVPP2_BM_POOL_SIZE_MAX (16*1024 - MVPP2_BM_POOL_PTR_ALIGN/4)
  604. #define MVPP2_BM_POOL_PTR_ALIGN 128
  605. #define MVPP2_BM_SWF_LONG_POOL(port) 0
  606. /* BM cookie (32 bits) definition */
  607. #define MVPP2_BM_COOKIE_POOL_OFFS 8
  608. #define MVPP2_BM_COOKIE_CPU_OFFS 24
  609. /* BM short pool packet size
  610. * These value assure that for SWF the total number
  611. * of bytes allocated for each buffer will be 512
  612. */
  613. #define MVPP2_BM_SHORT_PKT_SIZE MVPP2_RX_MAX_PKT_SIZE(512)
  614. enum mvpp2_bm_type {
  615. MVPP2_BM_FREE,
  616. MVPP2_BM_SWF_LONG,
  617. MVPP2_BM_SWF_SHORT
  618. };
  619. /* Definitions */
  620. /* Shared Packet Processor resources */
  621. struct mvpp2 {
  622. /* Shared registers' base addresses */
  623. void __iomem *base;
  624. void __iomem *lms_base;
  625. /* List of pointers to port structures */
  626. struct mvpp2_port **port_list;
  627. /* Aggregated TXQs */
  628. struct mvpp2_tx_queue *aggr_txqs;
  629. /* BM pools */
  630. struct mvpp2_bm_pool *bm_pools;
  631. /* PRS shadow table */
  632. struct mvpp2_prs_shadow *prs_shadow;
  633. /* PRS auxiliary table for double vlan entries control */
  634. bool *prs_double_vlans;
  635. /* Tclk value */
  636. u32 tclk;
  637. struct mii_dev *bus;
  638. };
  639. struct mvpp2_pcpu_stats {
  640. u64 rx_packets;
  641. u64 rx_bytes;
  642. u64 tx_packets;
  643. u64 tx_bytes;
  644. };
  645. struct mvpp2_port {
  646. u8 id;
  647. int irq;
  648. struct mvpp2 *priv;
  649. /* Per-port registers' base address */
  650. void __iomem *base;
  651. struct mvpp2_rx_queue **rxqs;
  652. struct mvpp2_tx_queue **txqs;
  653. int pkt_size;
  654. u32 pending_cause_rx;
  655. /* Per-CPU port control */
  656. struct mvpp2_port_pcpu __percpu *pcpu;
  657. /* Flags */
  658. unsigned long flags;
  659. u16 tx_ring_size;
  660. u16 rx_ring_size;
  661. struct mvpp2_pcpu_stats __percpu *stats;
  662. struct phy_device *phy_dev;
  663. phy_interface_t phy_interface;
  664. int phy_node;
  665. int phyaddr;
  666. int init;
  667. unsigned int link;
  668. unsigned int duplex;
  669. unsigned int speed;
  670. struct mvpp2_bm_pool *pool_long;
  671. struct mvpp2_bm_pool *pool_short;
  672. /* Index of first port's physical RXQ */
  673. u8 first_rxq;
  674. u8 dev_addr[ETH_ALEN];
  675. };
  676. /* The mvpp2_tx_desc and mvpp2_rx_desc structures describe the
  677. * layout of the transmit and reception DMA descriptors, and their
  678. * layout is therefore defined by the hardware design
  679. */
  680. #define MVPP2_TXD_L3_OFF_SHIFT 0
  681. #define MVPP2_TXD_IP_HLEN_SHIFT 8
  682. #define MVPP2_TXD_L4_CSUM_FRAG BIT(13)
  683. #define MVPP2_TXD_L4_CSUM_NOT BIT(14)
  684. #define MVPP2_TXD_IP_CSUM_DISABLE BIT(15)
  685. #define MVPP2_TXD_PADDING_DISABLE BIT(23)
  686. #define MVPP2_TXD_L4_UDP BIT(24)
  687. #define MVPP2_TXD_L3_IP6 BIT(26)
  688. #define MVPP2_TXD_L_DESC BIT(28)
  689. #define MVPP2_TXD_F_DESC BIT(29)
  690. #define MVPP2_RXD_ERR_SUMMARY BIT(15)
  691. #define MVPP2_RXD_ERR_CODE_MASK (BIT(13) | BIT(14))
  692. #define MVPP2_RXD_ERR_CRC 0x0
  693. #define MVPP2_RXD_ERR_OVERRUN BIT(13)
  694. #define MVPP2_RXD_ERR_RESOURCE (BIT(13) | BIT(14))
  695. #define MVPP2_RXD_BM_POOL_ID_OFFS 16
  696. #define MVPP2_RXD_BM_POOL_ID_MASK (BIT(16) | BIT(17) | BIT(18))
  697. #define MVPP2_RXD_HWF_SYNC BIT(21)
  698. #define MVPP2_RXD_L4_CSUM_OK BIT(22)
  699. #define MVPP2_RXD_IP4_HEADER_ERR BIT(24)
  700. #define MVPP2_RXD_L4_TCP BIT(25)
  701. #define MVPP2_RXD_L4_UDP BIT(26)
  702. #define MVPP2_RXD_L3_IP4 BIT(28)
  703. #define MVPP2_RXD_L3_IP6 BIT(30)
  704. #define MVPP2_RXD_BUF_HDR BIT(31)
  705. struct mvpp2_tx_desc {
  706. u32 command; /* Options used by HW for packet transmitting.*/
  707. u8 packet_offset; /* the offset from the buffer beginning */
  708. u8 phys_txq; /* destination queue ID */
  709. u16 data_size; /* data size of transmitted packet in bytes */
  710. u32 buf_phys_addr; /* physical addr of transmitted buffer */
  711. u32 buf_cookie; /* cookie for access to TX buffer in tx path */
  712. u32 reserved1[3]; /* hw_cmd (for future use, BM, PON, PNC) */
  713. u32 reserved2; /* reserved (for future use) */
  714. };
  715. struct mvpp2_rx_desc {
  716. u32 status; /* info about received packet */
  717. u16 reserved1; /* parser_info (for future use, PnC) */
  718. u16 data_size; /* size of received packet in bytes */
  719. u32 buf_phys_addr; /* physical address of the buffer */
  720. u32 buf_cookie; /* cookie for access to RX buffer in rx path */
  721. u16 reserved2; /* gem_port_id (for future use, PON) */
  722. u16 reserved3; /* csum_l4 (for future use, PnC) */
  723. u8 reserved4; /* bm_qset (for future use, BM) */
  724. u8 reserved5;
  725. u16 reserved6; /* classify_info (for future use, PnC) */
  726. u32 reserved7; /* flow_id (for future use, PnC) */
  727. u32 reserved8;
  728. };
  729. /* Per-CPU Tx queue control */
  730. struct mvpp2_txq_pcpu {
  731. int cpu;
  732. /* Number of Tx DMA descriptors in the descriptor ring */
  733. int size;
  734. /* Number of currently used Tx DMA descriptor in the
  735. * descriptor ring
  736. */
  737. int count;
  738. /* Number of Tx DMA descriptors reserved for each CPU */
  739. int reserved_num;
  740. /* Index of last TX DMA descriptor that was inserted */
  741. int txq_put_index;
  742. /* Index of the TX DMA descriptor to be cleaned up */
  743. int txq_get_index;
  744. };
  745. struct mvpp2_tx_queue {
  746. /* Physical number of this Tx queue */
  747. u8 id;
  748. /* Logical number of this Tx queue */
  749. u8 log_id;
  750. /* Number of Tx DMA descriptors in the descriptor ring */
  751. int size;
  752. /* Number of currently used Tx DMA descriptor in the descriptor ring */
  753. int count;
  754. /* Per-CPU control of physical Tx queues */
  755. struct mvpp2_txq_pcpu __percpu *pcpu;
  756. u32 done_pkts_coal;
  757. /* Virtual address of thex Tx DMA descriptors array */
  758. struct mvpp2_tx_desc *descs;
  759. /* DMA address of the Tx DMA descriptors array */
  760. dma_addr_t descs_phys;
  761. /* Index of the last Tx DMA descriptor */
  762. int last_desc;
  763. /* Index of the next Tx DMA descriptor to process */
  764. int next_desc_to_proc;
  765. };
  766. struct mvpp2_rx_queue {
  767. /* RX queue number, in the range 0-31 for physical RXQs */
  768. u8 id;
  769. /* Num of rx descriptors in the rx descriptor ring */
  770. int size;
  771. u32 pkts_coal;
  772. u32 time_coal;
  773. /* Virtual address of the RX DMA descriptors array */
  774. struct mvpp2_rx_desc *descs;
  775. /* DMA address of the RX DMA descriptors array */
  776. dma_addr_t descs_phys;
  777. /* Index of the last RX DMA descriptor */
  778. int last_desc;
  779. /* Index of the next RX DMA descriptor to process */
  780. int next_desc_to_proc;
  781. /* ID of port to which physical RXQ is mapped */
  782. int port;
  783. /* Port's logic RXQ number to which physical RXQ is mapped */
  784. int logic_rxq;
  785. };
  786. union mvpp2_prs_tcam_entry {
  787. u32 word[MVPP2_PRS_TCAM_WORDS];
  788. u8 byte[MVPP2_PRS_TCAM_WORDS * 4];
  789. };
  790. union mvpp2_prs_sram_entry {
  791. u32 word[MVPP2_PRS_SRAM_WORDS];
  792. u8 byte[MVPP2_PRS_SRAM_WORDS * 4];
  793. };
  794. struct mvpp2_prs_entry {
  795. u32 index;
  796. union mvpp2_prs_tcam_entry tcam;
  797. union mvpp2_prs_sram_entry sram;
  798. };
  799. struct mvpp2_prs_shadow {
  800. bool valid;
  801. bool finish;
  802. /* Lookup ID */
  803. int lu;
  804. /* User defined offset */
  805. int udf;
  806. /* Result info */
  807. u32 ri;
  808. u32 ri_mask;
  809. };
  810. struct mvpp2_cls_flow_entry {
  811. u32 index;
  812. u32 data[MVPP2_CLS_FLOWS_TBL_DATA_WORDS];
  813. };
  814. struct mvpp2_cls_lookup_entry {
  815. u32 lkpid;
  816. u32 way;
  817. u32 data;
  818. };
  819. struct mvpp2_bm_pool {
  820. /* Pool number in the range 0-7 */
  821. int id;
  822. enum mvpp2_bm_type type;
  823. /* Buffer Pointers Pool External (BPPE) size */
  824. int size;
  825. /* Number of buffers for this pool */
  826. int buf_num;
  827. /* Pool buffer size */
  828. int buf_size;
  829. /* Packet size */
  830. int pkt_size;
  831. /* BPPE virtual base address */
  832. u32 *virt_addr;
  833. /* BPPE physical base address */
  834. dma_addr_t phys_addr;
  835. /* Ports using BM pool */
  836. u32 port_map;
  837. /* Occupied buffers indicator */
  838. int in_use_thresh;
  839. };
  840. struct mvpp2_buff_hdr {
  841. u32 next_buff_phys_addr;
  842. u32 next_buff_virt_addr;
  843. u16 byte_count;
  844. u16 info;
  845. u8 reserved1; /* bm_qset (for future use, BM) */
  846. };
  847. /* Buffer header info bits */
  848. #define MVPP2_B_HDR_INFO_MC_ID_MASK 0xfff
  849. #define MVPP2_B_HDR_INFO_MC_ID(info) ((info) & MVPP2_B_HDR_INFO_MC_ID_MASK)
  850. #define MVPP2_B_HDR_INFO_LAST_OFFS 12
  851. #define MVPP2_B_HDR_INFO_LAST_MASK BIT(12)
  852. #define MVPP2_B_HDR_INFO_IS_LAST(info) \
  853. ((info & MVPP2_B_HDR_INFO_LAST_MASK) >> MVPP2_B_HDR_INFO_LAST_OFFS)
  854. /* Static declaractions */
  855. /* Number of RXQs used by single port */
  856. static int rxq_number = MVPP2_DEFAULT_RXQ;
  857. /* Number of TXQs used by single port */
  858. static int txq_number = MVPP2_DEFAULT_TXQ;
  859. #define MVPP2_DRIVER_NAME "mvpp2"
  860. #define MVPP2_DRIVER_VERSION "1.0"
  861. /*
  862. * U-Boot internal data, mostly uncached buffers for descriptors and data
  863. */
  864. struct buffer_location {
  865. struct mvpp2_tx_desc *aggr_tx_descs;
  866. struct mvpp2_tx_desc *tx_descs;
  867. struct mvpp2_rx_desc *rx_descs;
  868. u32 *bm_pool[MVPP2_BM_POOLS_NUM];
  869. u32 *rx_buffer[MVPP2_BM_LONG_BUF_NUM];
  870. int first_rxq;
  871. };
  872. /*
  873. * All 4 interfaces use the same global buffer, since only one interface
  874. * can be enabled at once
  875. */
  876. static struct buffer_location buffer_loc;
  877. /*
  878. * Page table entries are set to 1MB, or multiples of 1MB
  879. * (not < 1MB). driver uses less bd's so use 1MB bdspace.
  880. */
  881. #define BD_SPACE (1 << 20)
  882. /* Utility/helper methods */
  883. static void mvpp2_write(struct mvpp2 *priv, u32 offset, u32 data)
  884. {
  885. writel(data, priv->base + offset);
  886. }
  887. static u32 mvpp2_read(struct mvpp2 *priv, u32 offset)
  888. {
  889. return readl(priv->base + offset);
  890. }
  891. static void mvpp2_txq_inc_get(struct mvpp2_txq_pcpu *txq_pcpu)
  892. {
  893. txq_pcpu->txq_get_index++;
  894. if (txq_pcpu->txq_get_index == txq_pcpu->size)
  895. txq_pcpu->txq_get_index = 0;
  896. }
  897. /* Get number of physical egress port */
  898. static inline int mvpp2_egress_port(struct mvpp2_port *port)
  899. {
  900. return MVPP2_MAX_TCONT + port->id;
  901. }
  902. /* Get number of physical TXQ */
  903. static inline int mvpp2_txq_phys(int port, int txq)
  904. {
  905. return (MVPP2_MAX_TCONT + port) * MVPP2_MAX_TXQ + txq;
  906. }
  907. /* Parser configuration routines */
  908. /* Update parser tcam and sram hw entries */
  909. static int mvpp2_prs_hw_write(struct mvpp2 *priv, struct mvpp2_prs_entry *pe)
  910. {
  911. int i;
  912. if (pe->index > MVPP2_PRS_TCAM_SRAM_SIZE - 1)
  913. return -EINVAL;
  914. /* Clear entry invalidation bit */
  915. pe->tcam.word[MVPP2_PRS_TCAM_INV_WORD] &= ~MVPP2_PRS_TCAM_INV_MASK;
  916. /* Write tcam index - indirect access */
  917. mvpp2_write(priv, MVPP2_PRS_TCAM_IDX_REG, pe->index);
  918. for (i = 0; i < MVPP2_PRS_TCAM_WORDS; i++)
  919. mvpp2_write(priv, MVPP2_PRS_TCAM_DATA_REG(i), pe->tcam.word[i]);
  920. /* Write sram index - indirect access */
  921. mvpp2_write(priv, MVPP2_PRS_SRAM_IDX_REG, pe->index);
  922. for (i = 0; i < MVPP2_PRS_SRAM_WORDS; i++)
  923. mvpp2_write(priv, MVPP2_PRS_SRAM_DATA_REG(i), pe->sram.word[i]);
  924. return 0;
  925. }
  926. /* Read tcam entry from hw */
  927. static int mvpp2_prs_hw_read(struct mvpp2 *priv, struct mvpp2_prs_entry *pe)
  928. {
  929. int i;
  930. if (pe->index > MVPP2_PRS_TCAM_SRAM_SIZE - 1)
  931. return -EINVAL;
  932. /* Write tcam index - indirect access */
  933. mvpp2_write(priv, MVPP2_PRS_TCAM_IDX_REG, pe->index);
  934. pe->tcam.word[MVPP2_PRS_TCAM_INV_WORD] = mvpp2_read(priv,
  935. MVPP2_PRS_TCAM_DATA_REG(MVPP2_PRS_TCAM_INV_WORD));
  936. if (pe->tcam.word[MVPP2_PRS_TCAM_INV_WORD] & MVPP2_PRS_TCAM_INV_MASK)
  937. return MVPP2_PRS_TCAM_ENTRY_INVALID;
  938. for (i = 0; i < MVPP2_PRS_TCAM_WORDS; i++)
  939. pe->tcam.word[i] = mvpp2_read(priv, MVPP2_PRS_TCAM_DATA_REG(i));
  940. /* Write sram index - indirect access */
  941. mvpp2_write(priv, MVPP2_PRS_SRAM_IDX_REG, pe->index);
  942. for (i = 0; i < MVPP2_PRS_SRAM_WORDS; i++)
  943. pe->sram.word[i] = mvpp2_read(priv, MVPP2_PRS_SRAM_DATA_REG(i));
  944. return 0;
  945. }
  946. /* Invalidate tcam hw entry */
  947. static void mvpp2_prs_hw_inv(struct mvpp2 *priv, int index)
  948. {
  949. /* Write index - indirect access */
  950. mvpp2_write(priv, MVPP2_PRS_TCAM_IDX_REG, index);
  951. mvpp2_write(priv, MVPP2_PRS_TCAM_DATA_REG(MVPP2_PRS_TCAM_INV_WORD),
  952. MVPP2_PRS_TCAM_INV_MASK);
  953. }
  954. /* Enable shadow table entry and set its lookup ID */
  955. static void mvpp2_prs_shadow_set(struct mvpp2 *priv, int index, int lu)
  956. {
  957. priv->prs_shadow[index].valid = true;
  958. priv->prs_shadow[index].lu = lu;
  959. }
  960. /* Update ri fields in shadow table entry */
  961. static void mvpp2_prs_shadow_ri_set(struct mvpp2 *priv, int index,
  962. unsigned int ri, unsigned int ri_mask)
  963. {
  964. priv->prs_shadow[index].ri_mask = ri_mask;
  965. priv->prs_shadow[index].ri = ri;
  966. }
  967. /* Update lookup field in tcam sw entry */
  968. static void mvpp2_prs_tcam_lu_set(struct mvpp2_prs_entry *pe, unsigned int lu)
  969. {
  970. int enable_off = MVPP2_PRS_TCAM_EN_OFFS(MVPP2_PRS_TCAM_LU_BYTE);
  971. pe->tcam.byte[MVPP2_PRS_TCAM_LU_BYTE] = lu;
  972. pe->tcam.byte[enable_off] = MVPP2_PRS_LU_MASK;
  973. }
  974. /* Update mask for single port in tcam sw entry */
  975. static void mvpp2_prs_tcam_port_set(struct mvpp2_prs_entry *pe,
  976. unsigned int port, bool add)
  977. {
  978. int enable_off = MVPP2_PRS_TCAM_EN_OFFS(MVPP2_PRS_TCAM_PORT_BYTE);
  979. if (add)
  980. pe->tcam.byte[enable_off] &= ~(1 << port);
  981. else
  982. pe->tcam.byte[enable_off] |= 1 << port;
  983. }
  984. /* Update port map in tcam sw entry */
  985. static void mvpp2_prs_tcam_port_map_set(struct mvpp2_prs_entry *pe,
  986. unsigned int ports)
  987. {
  988. unsigned char port_mask = MVPP2_PRS_PORT_MASK;
  989. int enable_off = MVPP2_PRS_TCAM_EN_OFFS(MVPP2_PRS_TCAM_PORT_BYTE);
  990. pe->tcam.byte[MVPP2_PRS_TCAM_PORT_BYTE] = 0;
  991. pe->tcam.byte[enable_off] &= ~port_mask;
  992. pe->tcam.byte[enable_off] |= ~ports & MVPP2_PRS_PORT_MASK;
  993. }
  994. /* Obtain port map from tcam sw entry */
  995. static unsigned int mvpp2_prs_tcam_port_map_get(struct mvpp2_prs_entry *pe)
  996. {
  997. int enable_off = MVPP2_PRS_TCAM_EN_OFFS(MVPP2_PRS_TCAM_PORT_BYTE);
  998. return ~(pe->tcam.byte[enable_off]) & MVPP2_PRS_PORT_MASK;
  999. }
  1000. /* Set byte of data and its enable bits in tcam sw entry */
  1001. static void mvpp2_prs_tcam_data_byte_set(struct mvpp2_prs_entry *pe,
  1002. unsigned int offs, unsigned char byte,
  1003. unsigned char enable)
  1004. {
  1005. pe->tcam.byte[MVPP2_PRS_TCAM_DATA_BYTE(offs)] = byte;
  1006. pe->tcam.byte[MVPP2_PRS_TCAM_DATA_BYTE_EN(offs)] = enable;
  1007. }
  1008. /* Get byte of data and its enable bits from tcam sw entry */
  1009. static void mvpp2_prs_tcam_data_byte_get(struct mvpp2_prs_entry *pe,
  1010. unsigned int offs, unsigned char *byte,
  1011. unsigned char *enable)
  1012. {
  1013. *byte = pe->tcam.byte[MVPP2_PRS_TCAM_DATA_BYTE(offs)];
  1014. *enable = pe->tcam.byte[MVPP2_PRS_TCAM_DATA_BYTE_EN(offs)];
  1015. }
  1016. /* Set ethertype in tcam sw entry */
  1017. static void mvpp2_prs_match_etype(struct mvpp2_prs_entry *pe, int offset,
  1018. unsigned short ethertype)
  1019. {
  1020. mvpp2_prs_tcam_data_byte_set(pe, offset + 0, ethertype >> 8, 0xff);
  1021. mvpp2_prs_tcam_data_byte_set(pe, offset + 1, ethertype & 0xff, 0xff);
  1022. }
  1023. /* Set bits in sram sw entry */
  1024. static void mvpp2_prs_sram_bits_set(struct mvpp2_prs_entry *pe, int bit_num,
  1025. int val)
  1026. {
  1027. pe->sram.byte[MVPP2_BIT_TO_BYTE(bit_num)] |= (val << (bit_num % 8));
  1028. }
  1029. /* Clear bits in sram sw entry */
  1030. static void mvpp2_prs_sram_bits_clear(struct mvpp2_prs_entry *pe, int bit_num,
  1031. int val)
  1032. {
  1033. pe->sram.byte[MVPP2_BIT_TO_BYTE(bit_num)] &= ~(val << (bit_num % 8));
  1034. }
  1035. /* Update ri bits in sram sw entry */
  1036. static void mvpp2_prs_sram_ri_update(struct mvpp2_prs_entry *pe,
  1037. unsigned int bits, unsigned int mask)
  1038. {
  1039. unsigned int i;
  1040. for (i = 0; i < MVPP2_PRS_SRAM_RI_CTRL_BITS; i++) {
  1041. int ri_off = MVPP2_PRS_SRAM_RI_OFFS;
  1042. if (!(mask & BIT(i)))
  1043. continue;
  1044. if (bits & BIT(i))
  1045. mvpp2_prs_sram_bits_set(pe, ri_off + i, 1);
  1046. else
  1047. mvpp2_prs_sram_bits_clear(pe, ri_off + i, 1);
  1048. mvpp2_prs_sram_bits_set(pe, MVPP2_PRS_SRAM_RI_CTRL_OFFS + i, 1);
  1049. }
  1050. }
  1051. /* Update ai bits in sram sw entry */
  1052. static void mvpp2_prs_sram_ai_update(struct mvpp2_prs_entry *pe,
  1053. unsigned int bits, unsigned int mask)
  1054. {
  1055. unsigned int i;
  1056. int ai_off = MVPP2_PRS_SRAM_AI_OFFS;
  1057. for (i = 0; i < MVPP2_PRS_SRAM_AI_CTRL_BITS; i++) {
  1058. if (!(mask & BIT(i)))
  1059. continue;
  1060. if (bits & BIT(i))
  1061. mvpp2_prs_sram_bits_set(pe, ai_off + i, 1);
  1062. else
  1063. mvpp2_prs_sram_bits_clear(pe, ai_off + i, 1);
  1064. mvpp2_prs_sram_bits_set(pe, MVPP2_PRS_SRAM_AI_CTRL_OFFS + i, 1);
  1065. }
  1066. }
  1067. /* Read ai bits from sram sw entry */
  1068. static int mvpp2_prs_sram_ai_get(struct mvpp2_prs_entry *pe)
  1069. {
  1070. u8 bits;
  1071. int ai_off = MVPP2_BIT_TO_BYTE(MVPP2_PRS_SRAM_AI_OFFS);
  1072. int ai_en_off = ai_off + 1;
  1073. int ai_shift = MVPP2_PRS_SRAM_AI_OFFS % 8;
  1074. bits = (pe->sram.byte[ai_off] >> ai_shift) |
  1075. (pe->sram.byte[ai_en_off] << (8 - ai_shift));
  1076. return bits;
  1077. }
  1078. /* In sram sw entry set lookup ID field of the tcam key to be used in the next
  1079. * lookup interation
  1080. */
  1081. static void mvpp2_prs_sram_next_lu_set(struct mvpp2_prs_entry *pe,
  1082. unsigned int lu)
  1083. {
  1084. int sram_next_off = MVPP2_PRS_SRAM_NEXT_LU_OFFS;
  1085. mvpp2_prs_sram_bits_clear(pe, sram_next_off,
  1086. MVPP2_PRS_SRAM_NEXT_LU_MASK);
  1087. mvpp2_prs_sram_bits_set(pe, sram_next_off, lu);
  1088. }
  1089. /* In the sram sw entry set sign and value of the next lookup offset
  1090. * and the offset value generated to the classifier
  1091. */
  1092. static void mvpp2_prs_sram_shift_set(struct mvpp2_prs_entry *pe, int shift,
  1093. unsigned int op)
  1094. {
  1095. /* Set sign */
  1096. if (shift < 0) {
  1097. mvpp2_prs_sram_bits_set(pe, MVPP2_PRS_SRAM_SHIFT_SIGN_BIT, 1);
  1098. shift = 0 - shift;
  1099. } else {
  1100. mvpp2_prs_sram_bits_clear(pe, MVPP2_PRS_SRAM_SHIFT_SIGN_BIT, 1);
  1101. }
  1102. /* Set value */
  1103. pe->sram.byte[MVPP2_BIT_TO_BYTE(MVPP2_PRS_SRAM_SHIFT_OFFS)] =
  1104. (unsigned char)shift;
  1105. /* Reset and set operation */
  1106. mvpp2_prs_sram_bits_clear(pe, MVPP2_PRS_SRAM_OP_SEL_SHIFT_OFFS,
  1107. MVPP2_PRS_SRAM_OP_SEL_SHIFT_MASK);
  1108. mvpp2_prs_sram_bits_set(pe, MVPP2_PRS_SRAM_OP_SEL_SHIFT_OFFS, op);
  1109. /* Set base offset as current */
  1110. mvpp2_prs_sram_bits_clear(pe, MVPP2_PRS_SRAM_OP_SEL_BASE_OFFS, 1);
  1111. }
  1112. /* In the sram sw entry set sign and value of the user defined offset
  1113. * generated to the classifier
  1114. */
  1115. static void mvpp2_prs_sram_offset_set(struct mvpp2_prs_entry *pe,
  1116. unsigned int type, int offset,
  1117. unsigned int op)
  1118. {
  1119. /* Set sign */
  1120. if (offset < 0) {
  1121. mvpp2_prs_sram_bits_set(pe, MVPP2_PRS_SRAM_UDF_SIGN_BIT, 1);
  1122. offset = 0 - offset;
  1123. } else {
  1124. mvpp2_prs_sram_bits_clear(pe, MVPP2_PRS_SRAM_UDF_SIGN_BIT, 1);
  1125. }
  1126. /* Set value */
  1127. mvpp2_prs_sram_bits_clear(pe, MVPP2_PRS_SRAM_UDF_OFFS,
  1128. MVPP2_PRS_SRAM_UDF_MASK);
  1129. mvpp2_prs_sram_bits_set(pe, MVPP2_PRS_SRAM_UDF_OFFS, offset);
  1130. pe->sram.byte[MVPP2_BIT_TO_BYTE(MVPP2_PRS_SRAM_UDF_OFFS +
  1131. MVPP2_PRS_SRAM_UDF_BITS)] &=
  1132. ~(MVPP2_PRS_SRAM_UDF_MASK >> (8 - (MVPP2_PRS_SRAM_UDF_OFFS % 8)));
  1133. pe->sram.byte[MVPP2_BIT_TO_BYTE(MVPP2_PRS_SRAM_UDF_OFFS +
  1134. MVPP2_PRS_SRAM_UDF_BITS)] |=
  1135. (offset >> (8 - (MVPP2_PRS_SRAM_UDF_OFFS % 8)));
  1136. /* Set offset type */
  1137. mvpp2_prs_sram_bits_clear(pe, MVPP2_PRS_SRAM_UDF_TYPE_OFFS,
  1138. MVPP2_PRS_SRAM_UDF_TYPE_MASK);
  1139. mvpp2_prs_sram_bits_set(pe, MVPP2_PRS_SRAM_UDF_TYPE_OFFS, type);
  1140. /* Set offset operation */
  1141. mvpp2_prs_sram_bits_clear(pe, MVPP2_PRS_SRAM_OP_SEL_UDF_OFFS,
  1142. MVPP2_PRS_SRAM_OP_SEL_UDF_MASK);
  1143. mvpp2_prs_sram_bits_set(pe, MVPP2_PRS_SRAM_OP_SEL_UDF_OFFS, op);
  1144. pe->sram.byte[MVPP2_BIT_TO_BYTE(MVPP2_PRS_SRAM_OP_SEL_UDF_OFFS +
  1145. MVPP2_PRS_SRAM_OP_SEL_UDF_BITS)] &=
  1146. ~(MVPP2_PRS_SRAM_OP_SEL_UDF_MASK >>
  1147. (8 - (MVPP2_PRS_SRAM_OP_SEL_UDF_OFFS % 8)));
  1148. pe->sram.byte[MVPP2_BIT_TO_BYTE(MVPP2_PRS_SRAM_OP_SEL_UDF_OFFS +
  1149. MVPP2_PRS_SRAM_OP_SEL_UDF_BITS)] |=
  1150. (op >> (8 - (MVPP2_PRS_SRAM_OP_SEL_UDF_OFFS % 8)));
  1151. /* Set base offset as current */
  1152. mvpp2_prs_sram_bits_clear(pe, MVPP2_PRS_SRAM_OP_SEL_BASE_OFFS, 1);
  1153. }
  1154. /* Find parser flow entry */
  1155. static struct mvpp2_prs_entry *mvpp2_prs_flow_find(struct mvpp2 *priv, int flow)
  1156. {
  1157. struct mvpp2_prs_entry *pe;
  1158. int tid;
  1159. pe = kzalloc(sizeof(*pe), GFP_KERNEL);
  1160. if (!pe)
  1161. return NULL;
  1162. mvpp2_prs_tcam_lu_set(pe, MVPP2_PRS_LU_FLOWS);
  1163. /* Go through the all entires with MVPP2_PRS_LU_FLOWS */
  1164. for (tid = MVPP2_PRS_TCAM_SRAM_SIZE - 1; tid >= 0; tid--) {
  1165. u8 bits;
  1166. if (!priv->prs_shadow[tid].valid ||
  1167. priv->prs_shadow[tid].lu != MVPP2_PRS_LU_FLOWS)
  1168. continue;
  1169. pe->index = tid;
  1170. mvpp2_prs_hw_read(priv, pe);
  1171. bits = mvpp2_prs_sram_ai_get(pe);
  1172. /* Sram store classification lookup ID in AI bits [5:0] */
  1173. if ((bits & MVPP2_PRS_FLOW_ID_MASK) == flow)
  1174. return pe;
  1175. }
  1176. kfree(pe);
  1177. return NULL;
  1178. }
  1179. /* Return first free tcam index, seeking from start to end */
  1180. static int mvpp2_prs_tcam_first_free(struct mvpp2 *priv, unsigned char start,
  1181. unsigned char end)
  1182. {
  1183. int tid;
  1184. if (start > end)
  1185. swap(start, end);
  1186. if (end >= MVPP2_PRS_TCAM_SRAM_SIZE)
  1187. end = MVPP2_PRS_TCAM_SRAM_SIZE - 1;
  1188. for (tid = start; tid <= end; tid++) {
  1189. if (!priv->prs_shadow[tid].valid)
  1190. return tid;
  1191. }
  1192. return -EINVAL;
  1193. }
  1194. /* Enable/disable dropping all mac da's */
  1195. static void mvpp2_prs_mac_drop_all_set(struct mvpp2 *priv, int port, bool add)
  1196. {
  1197. struct mvpp2_prs_entry pe;
  1198. if (priv->prs_shadow[MVPP2_PE_DROP_ALL].valid) {
  1199. /* Entry exist - update port only */
  1200. pe.index = MVPP2_PE_DROP_ALL;
  1201. mvpp2_prs_hw_read(priv, &pe);
  1202. } else {
  1203. /* Entry doesn't exist - create new */
  1204. memset(&pe, 0, sizeof(struct mvpp2_prs_entry));
  1205. mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_MAC);
  1206. pe.index = MVPP2_PE_DROP_ALL;
  1207. /* Non-promiscuous mode for all ports - DROP unknown packets */
  1208. mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_DROP_MASK,
  1209. MVPP2_PRS_RI_DROP_MASK);
  1210. mvpp2_prs_sram_bits_set(&pe, MVPP2_PRS_SRAM_LU_GEN_BIT, 1);
  1211. mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_FLOWS);
  1212. /* Update shadow table */
  1213. mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_MAC);
  1214. /* Mask all ports */
  1215. mvpp2_prs_tcam_port_map_set(&pe, 0);
  1216. }
  1217. /* Update port mask */
  1218. mvpp2_prs_tcam_port_set(&pe, port, add);
  1219. mvpp2_prs_hw_write(priv, &pe);
  1220. }
  1221. /* Set port to promiscuous mode */
  1222. static void mvpp2_prs_mac_promisc_set(struct mvpp2 *priv, int port, bool add)
  1223. {
  1224. struct mvpp2_prs_entry pe;
  1225. /* Promiscuous mode - Accept unknown packets */
  1226. if (priv->prs_shadow[MVPP2_PE_MAC_PROMISCUOUS].valid) {
  1227. /* Entry exist - update port only */
  1228. pe.index = MVPP2_PE_MAC_PROMISCUOUS;
  1229. mvpp2_prs_hw_read(priv, &pe);
  1230. } else {
  1231. /* Entry doesn't exist - create new */
  1232. memset(&pe, 0, sizeof(struct mvpp2_prs_entry));
  1233. mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_MAC);
  1234. pe.index = MVPP2_PE_MAC_PROMISCUOUS;
  1235. /* Continue - set next lookup */
  1236. mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_DSA);
  1237. /* Set result info bits */
  1238. mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L2_UCAST,
  1239. MVPP2_PRS_RI_L2_CAST_MASK);
  1240. /* Shift to ethertype */
  1241. mvpp2_prs_sram_shift_set(&pe, 2 * ETH_ALEN,
  1242. MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
  1243. /* Mask all ports */
  1244. mvpp2_prs_tcam_port_map_set(&pe, 0);
  1245. /* Update shadow table */
  1246. mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_MAC);
  1247. }
  1248. /* Update port mask */
  1249. mvpp2_prs_tcam_port_set(&pe, port, add);
  1250. mvpp2_prs_hw_write(priv, &pe);
  1251. }
  1252. /* Accept multicast */
  1253. static void mvpp2_prs_mac_multi_set(struct mvpp2 *priv, int port, int index,
  1254. bool add)
  1255. {
  1256. struct mvpp2_prs_entry pe;
  1257. unsigned char da_mc;
  1258. /* Ethernet multicast address first byte is
  1259. * 0x01 for IPv4 and 0x33 for IPv6
  1260. */
  1261. da_mc = (index == MVPP2_PE_MAC_MC_ALL) ? 0x01 : 0x33;
  1262. if (priv->prs_shadow[index].valid) {
  1263. /* Entry exist - update port only */
  1264. pe.index = index;
  1265. mvpp2_prs_hw_read(priv, &pe);
  1266. } else {
  1267. /* Entry doesn't exist - create new */
  1268. memset(&pe, 0, sizeof(struct mvpp2_prs_entry));
  1269. mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_MAC);
  1270. pe.index = index;
  1271. /* Continue - set next lookup */
  1272. mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_DSA);
  1273. /* Set result info bits */
  1274. mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L2_MCAST,
  1275. MVPP2_PRS_RI_L2_CAST_MASK);
  1276. /* Update tcam entry data first byte */
  1277. mvpp2_prs_tcam_data_byte_set(&pe, 0, da_mc, 0xff);
  1278. /* Shift to ethertype */
  1279. mvpp2_prs_sram_shift_set(&pe, 2 * ETH_ALEN,
  1280. MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
  1281. /* Mask all ports */
  1282. mvpp2_prs_tcam_port_map_set(&pe, 0);
  1283. /* Update shadow table */
  1284. mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_MAC);
  1285. }
  1286. /* Update port mask */
  1287. mvpp2_prs_tcam_port_set(&pe, port, add);
  1288. mvpp2_prs_hw_write(priv, &pe);
  1289. }
  1290. /* Parser per-port initialization */
  1291. static void mvpp2_prs_hw_port_init(struct mvpp2 *priv, int port, int lu_first,
  1292. int lu_max, int offset)
  1293. {
  1294. u32 val;
  1295. /* Set lookup ID */
  1296. val = mvpp2_read(priv, MVPP2_PRS_INIT_LOOKUP_REG);
  1297. val &= ~MVPP2_PRS_PORT_LU_MASK(port);
  1298. val |= MVPP2_PRS_PORT_LU_VAL(port, lu_first);
  1299. mvpp2_write(priv, MVPP2_PRS_INIT_LOOKUP_REG, val);
  1300. /* Set maximum number of loops for packet received from port */
  1301. val = mvpp2_read(priv, MVPP2_PRS_MAX_LOOP_REG(port));
  1302. val &= ~MVPP2_PRS_MAX_LOOP_MASK(port);
  1303. val |= MVPP2_PRS_MAX_LOOP_VAL(port, lu_max);
  1304. mvpp2_write(priv, MVPP2_PRS_MAX_LOOP_REG(port), val);
  1305. /* Set initial offset for packet header extraction for the first
  1306. * searching loop
  1307. */
  1308. val = mvpp2_read(priv, MVPP2_PRS_INIT_OFFS_REG(port));
  1309. val &= ~MVPP2_PRS_INIT_OFF_MASK(port);
  1310. val |= MVPP2_PRS_INIT_OFF_VAL(port, offset);
  1311. mvpp2_write(priv, MVPP2_PRS_INIT_OFFS_REG(port), val);
  1312. }
  1313. /* Default flow entries initialization for all ports */
  1314. static void mvpp2_prs_def_flow_init(struct mvpp2 *priv)
  1315. {
  1316. struct mvpp2_prs_entry pe;
  1317. int port;
  1318. for (port = 0; port < MVPP2_MAX_PORTS; port++) {
  1319. memset(&pe, 0, sizeof(struct mvpp2_prs_entry));
  1320. mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_FLOWS);
  1321. pe.index = MVPP2_PE_FIRST_DEFAULT_FLOW - port;
  1322. /* Mask all ports */
  1323. mvpp2_prs_tcam_port_map_set(&pe, 0);
  1324. /* Set flow ID*/
  1325. mvpp2_prs_sram_ai_update(&pe, port, MVPP2_PRS_FLOW_ID_MASK);
  1326. mvpp2_prs_sram_bits_set(&pe, MVPP2_PRS_SRAM_LU_DONE_BIT, 1);
  1327. /* Update shadow table and hw entry */
  1328. mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_FLOWS);
  1329. mvpp2_prs_hw_write(priv, &pe);
  1330. }
  1331. }
  1332. /* Set default entry for Marvell Header field */
  1333. static void mvpp2_prs_mh_init(struct mvpp2 *priv)
  1334. {
  1335. struct mvpp2_prs_entry pe;
  1336. memset(&pe, 0, sizeof(struct mvpp2_prs_entry));
  1337. pe.index = MVPP2_PE_MH_DEFAULT;
  1338. mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_MH);
  1339. mvpp2_prs_sram_shift_set(&pe, MVPP2_MH_SIZE,
  1340. MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
  1341. mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_MAC);
  1342. /* Unmask all ports */
  1343. mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
  1344. /* Update shadow table and hw entry */
  1345. mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_MH);
  1346. mvpp2_prs_hw_write(priv, &pe);
  1347. }
  1348. /* Set default entires (place holder) for promiscuous, non-promiscuous and
  1349. * multicast MAC addresses
  1350. */
  1351. static void mvpp2_prs_mac_init(struct mvpp2 *priv)
  1352. {
  1353. struct mvpp2_prs_entry pe;
  1354. memset(&pe, 0, sizeof(struct mvpp2_prs_entry));
  1355. /* Non-promiscuous mode for all ports - DROP unknown packets */
  1356. pe.index = MVPP2_PE_MAC_NON_PROMISCUOUS;
  1357. mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_MAC);
  1358. mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_DROP_MASK,
  1359. MVPP2_PRS_RI_DROP_MASK);
  1360. mvpp2_prs_sram_bits_set(&pe, MVPP2_PRS_SRAM_LU_GEN_BIT, 1);
  1361. mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_FLOWS);
  1362. /* Unmask all ports */
  1363. mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
  1364. /* Update shadow table and hw entry */
  1365. mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_MAC);
  1366. mvpp2_prs_hw_write(priv, &pe);
  1367. /* place holders only - no ports */
  1368. mvpp2_prs_mac_drop_all_set(priv, 0, false);
  1369. mvpp2_prs_mac_promisc_set(priv, 0, false);
  1370. mvpp2_prs_mac_multi_set(priv, MVPP2_PE_MAC_MC_ALL, 0, false);
  1371. mvpp2_prs_mac_multi_set(priv, MVPP2_PE_MAC_MC_IP6, 0, false);
  1372. }
  1373. /* Match basic ethertypes */
  1374. static int mvpp2_prs_etype_init(struct mvpp2 *priv)
  1375. {
  1376. struct mvpp2_prs_entry pe;
  1377. int tid;
  1378. /* Ethertype: PPPoE */
  1379. tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
  1380. MVPP2_PE_LAST_FREE_TID);
  1381. if (tid < 0)
  1382. return tid;
  1383. memset(&pe, 0, sizeof(struct mvpp2_prs_entry));
  1384. mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_L2);
  1385. pe.index = tid;
  1386. mvpp2_prs_match_etype(&pe, 0, PROT_PPP_SES);
  1387. mvpp2_prs_sram_shift_set(&pe, MVPP2_PPPOE_HDR_SIZE,
  1388. MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
  1389. mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_PPPOE);
  1390. mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_PPPOE_MASK,
  1391. MVPP2_PRS_RI_PPPOE_MASK);
  1392. /* Update shadow table and hw entry */
  1393. mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_L2);
  1394. priv->prs_shadow[pe.index].udf = MVPP2_PRS_UDF_L2_DEF;
  1395. priv->prs_shadow[pe.index].finish = false;
  1396. mvpp2_prs_shadow_ri_set(priv, pe.index, MVPP2_PRS_RI_PPPOE_MASK,
  1397. MVPP2_PRS_RI_PPPOE_MASK);
  1398. mvpp2_prs_hw_write(priv, &pe);
  1399. /* Ethertype: ARP */
  1400. tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
  1401. MVPP2_PE_LAST_FREE_TID);
  1402. if (tid < 0)
  1403. return tid;
  1404. memset(&pe, 0, sizeof(struct mvpp2_prs_entry));
  1405. mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_L2);
  1406. pe.index = tid;
  1407. mvpp2_prs_match_etype(&pe, 0, PROT_ARP);
  1408. /* Generate flow in the next iteration*/
  1409. mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_FLOWS);
  1410. mvpp2_prs_sram_bits_set(&pe, MVPP2_PRS_SRAM_LU_GEN_BIT, 1);
  1411. mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_ARP,
  1412. MVPP2_PRS_RI_L3_PROTO_MASK);
  1413. /* Set L3 offset */
  1414. mvpp2_prs_sram_offset_set(&pe, MVPP2_PRS_SRAM_UDF_TYPE_L3,
  1415. MVPP2_ETH_TYPE_LEN,
  1416. MVPP2_PRS_SRAM_OP_SEL_UDF_ADD);
  1417. /* Update shadow table and hw entry */
  1418. mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_L2);
  1419. priv->prs_shadow[pe.index].udf = MVPP2_PRS_UDF_L2_DEF;
  1420. priv->prs_shadow[pe.index].finish = true;
  1421. mvpp2_prs_shadow_ri_set(priv, pe.index, MVPP2_PRS_RI_L3_ARP,
  1422. MVPP2_PRS_RI_L3_PROTO_MASK);
  1423. mvpp2_prs_hw_write(priv, &pe);
  1424. /* Ethertype: LBTD */
  1425. tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
  1426. MVPP2_PE_LAST_FREE_TID);
  1427. if (tid < 0)
  1428. return tid;
  1429. memset(&pe, 0, sizeof(struct mvpp2_prs_entry));
  1430. mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_L2);
  1431. pe.index = tid;
  1432. mvpp2_prs_match_etype(&pe, 0, MVPP2_IP_LBDT_TYPE);
  1433. /* Generate flow in the next iteration*/
  1434. mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_FLOWS);
  1435. mvpp2_prs_sram_bits_set(&pe, MVPP2_PRS_SRAM_LU_GEN_BIT, 1);
  1436. mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_CPU_CODE_RX_SPEC |
  1437. MVPP2_PRS_RI_UDF3_RX_SPECIAL,
  1438. MVPP2_PRS_RI_CPU_CODE_MASK |
  1439. MVPP2_PRS_RI_UDF3_MASK);
  1440. /* Set L3 offset */
  1441. mvpp2_prs_sram_offset_set(&pe, MVPP2_PRS_SRAM_UDF_TYPE_L3,
  1442. MVPP2_ETH_TYPE_LEN,
  1443. MVPP2_PRS_SRAM_OP_SEL_UDF_ADD);
  1444. /* Update shadow table and hw entry */
  1445. mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_L2);
  1446. priv->prs_shadow[pe.index].udf = MVPP2_PRS_UDF_L2_DEF;
  1447. priv->prs_shadow[pe.index].finish = true;
  1448. mvpp2_prs_shadow_ri_set(priv, pe.index, MVPP2_PRS_RI_CPU_CODE_RX_SPEC |
  1449. MVPP2_PRS_RI_UDF3_RX_SPECIAL,
  1450. MVPP2_PRS_RI_CPU_CODE_MASK |
  1451. MVPP2_PRS_RI_UDF3_MASK);
  1452. mvpp2_prs_hw_write(priv, &pe);
  1453. /* Ethertype: IPv4 without options */
  1454. tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
  1455. MVPP2_PE_LAST_FREE_TID);
  1456. if (tid < 0)
  1457. return tid;
  1458. memset(&pe, 0, sizeof(struct mvpp2_prs_entry));
  1459. mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_L2);
  1460. pe.index = tid;
  1461. mvpp2_prs_match_etype(&pe, 0, PROT_IP);
  1462. mvpp2_prs_tcam_data_byte_set(&pe, MVPP2_ETH_TYPE_LEN,
  1463. MVPP2_PRS_IPV4_HEAD | MVPP2_PRS_IPV4_IHL,
  1464. MVPP2_PRS_IPV4_HEAD_MASK |
  1465. MVPP2_PRS_IPV4_IHL_MASK);
  1466. mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_IP4);
  1467. mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_IP4,
  1468. MVPP2_PRS_RI_L3_PROTO_MASK);
  1469. /* Skip eth_type + 4 bytes of IP header */
  1470. mvpp2_prs_sram_shift_set(&pe, MVPP2_ETH_TYPE_LEN + 4,
  1471. MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
  1472. /* Set L3 offset */
  1473. mvpp2_prs_sram_offset_set(&pe, MVPP2_PRS_SRAM_UDF_TYPE_L3,
  1474. MVPP2_ETH_TYPE_LEN,
  1475. MVPP2_PRS_SRAM_OP_SEL_UDF_ADD);
  1476. /* Update shadow table and hw entry */
  1477. mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_L2);
  1478. priv->prs_shadow[pe.index].udf = MVPP2_PRS_UDF_L2_DEF;
  1479. priv->prs_shadow[pe.index].finish = false;
  1480. mvpp2_prs_shadow_ri_set(priv, pe.index, MVPP2_PRS_RI_L3_IP4,
  1481. MVPP2_PRS_RI_L3_PROTO_MASK);
  1482. mvpp2_prs_hw_write(priv, &pe);
  1483. /* Ethertype: IPv4 with options */
  1484. tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
  1485. MVPP2_PE_LAST_FREE_TID);
  1486. if (tid < 0)
  1487. return tid;
  1488. pe.index = tid;
  1489. /* Clear tcam data before updating */
  1490. pe.tcam.byte[MVPP2_PRS_TCAM_DATA_BYTE(MVPP2_ETH_TYPE_LEN)] = 0x0;
  1491. pe.tcam.byte[MVPP2_PRS_TCAM_DATA_BYTE_EN(MVPP2_ETH_TYPE_LEN)] = 0x0;
  1492. mvpp2_prs_tcam_data_byte_set(&pe, MVPP2_ETH_TYPE_LEN,
  1493. MVPP2_PRS_IPV4_HEAD,
  1494. MVPP2_PRS_IPV4_HEAD_MASK);
  1495. /* Clear ri before updating */
  1496. pe.sram.word[MVPP2_PRS_SRAM_RI_WORD] = 0x0;
  1497. pe.sram.word[MVPP2_PRS_SRAM_RI_CTRL_WORD] = 0x0;
  1498. mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_IP4_OPT,
  1499. MVPP2_PRS_RI_L3_PROTO_MASK);
  1500. /* Update shadow table and hw entry */
  1501. mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_L2);
  1502. priv->prs_shadow[pe.index].udf = MVPP2_PRS_UDF_L2_DEF;
  1503. priv->prs_shadow[pe.index].finish = false;
  1504. mvpp2_prs_shadow_ri_set(priv, pe.index, MVPP2_PRS_RI_L3_IP4_OPT,
  1505. MVPP2_PRS_RI_L3_PROTO_MASK);
  1506. mvpp2_prs_hw_write(priv, &pe);
  1507. /* Ethertype: IPv6 without options */
  1508. tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
  1509. MVPP2_PE_LAST_FREE_TID);
  1510. if (tid < 0)
  1511. return tid;
  1512. memset(&pe, 0, sizeof(struct mvpp2_prs_entry));
  1513. mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_L2);
  1514. pe.index = tid;
  1515. mvpp2_prs_match_etype(&pe, 0, PROT_IPV6);
  1516. /* Skip DIP of IPV6 header */
  1517. mvpp2_prs_sram_shift_set(&pe, MVPP2_ETH_TYPE_LEN + 8 +
  1518. MVPP2_MAX_L3_ADDR_SIZE,
  1519. MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
  1520. mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_IP6);
  1521. mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_IP6,
  1522. MVPP2_PRS_RI_L3_PROTO_MASK);
  1523. /* Set L3 offset */
  1524. mvpp2_prs_sram_offset_set(&pe, MVPP2_PRS_SRAM_UDF_TYPE_L3,
  1525. MVPP2_ETH_TYPE_LEN,
  1526. MVPP2_PRS_SRAM_OP_SEL_UDF_ADD);
  1527. mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_L2);
  1528. priv->prs_shadow[pe.index].udf = MVPP2_PRS_UDF_L2_DEF;
  1529. priv->prs_shadow[pe.index].finish = false;
  1530. mvpp2_prs_shadow_ri_set(priv, pe.index, MVPP2_PRS_RI_L3_IP6,
  1531. MVPP2_PRS_RI_L3_PROTO_MASK);
  1532. mvpp2_prs_hw_write(priv, &pe);
  1533. /* Default entry for MVPP2_PRS_LU_L2 - Unknown ethtype */
  1534. memset(&pe, 0, sizeof(struct mvpp2_prs_entry));
  1535. mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_L2);
  1536. pe.index = MVPP2_PE_ETH_TYPE_UN;
  1537. /* Unmask all ports */
  1538. mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
  1539. /* Generate flow in the next iteration*/
  1540. mvpp2_prs_sram_bits_set(&pe, MVPP2_PRS_SRAM_LU_GEN_BIT, 1);
  1541. mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_FLOWS);
  1542. mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_UN,
  1543. MVPP2_PRS_RI_L3_PROTO_MASK);
  1544. /* Set L3 offset even it's unknown L3 */
  1545. mvpp2_prs_sram_offset_set(&pe, MVPP2_PRS_SRAM_UDF_TYPE_L3,
  1546. MVPP2_ETH_TYPE_LEN,
  1547. MVPP2_PRS_SRAM_OP_SEL_UDF_ADD);
  1548. /* Update shadow table and hw entry */
  1549. mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_L2);
  1550. priv->prs_shadow[pe.index].udf = MVPP2_PRS_UDF_L2_DEF;
  1551. priv->prs_shadow[pe.index].finish = true;
  1552. mvpp2_prs_shadow_ri_set(priv, pe.index, MVPP2_PRS_RI_L3_UN,
  1553. MVPP2_PRS_RI_L3_PROTO_MASK);
  1554. mvpp2_prs_hw_write(priv, &pe);
  1555. return 0;
  1556. }
  1557. /* Parser default initialization */
  1558. static int mvpp2_prs_default_init(struct udevice *dev,
  1559. struct mvpp2 *priv)
  1560. {
  1561. int err, index, i;
  1562. /* Enable tcam table */
  1563. mvpp2_write(priv, MVPP2_PRS_TCAM_CTRL_REG, MVPP2_PRS_TCAM_EN_MASK);
  1564. /* Clear all tcam and sram entries */
  1565. for (index = 0; index < MVPP2_PRS_TCAM_SRAM_SIZE; index++) {
  1566. mvpp2_write(priv, MVPP2_PRS_TCAM_IDX_REG, index);
  1567. for (i = 0; i < MVPP2_PRS_TCAM_WORDS; i++)
  1568. mvpp2_write(priv, MVPP2_PRS_TCAM_DATA_REG(i), 0);
  1569. mvpp2_write(priv, MVPP2_PRS_SRAM_IDX_REG, index);
  1570. for (i = 0; i < MVPP2_PRS_SRAM_WORDS; i++)
  1571. mvpp2_write(priv, MVPP2_PRS_SRAM_DATA_REG(i), 0);
  1572. }
  1573. /* Invalidate all tcam entries */
  1574. for (index = 0; index < MVPP2_PRS_TCAM_SRAM_SIZE; index++)
  1575. mvpp2_prs_hw_inv(priv, index);
  1576. priv->prs_shadow = devm_kcalloc(dev, MVPP2_PRS_TCAM_SRAM_SIZE,
  1577. sizeof(struct mvpp2_prs_shadow),
  1578. GFP_KERNEL);
  1579. if (!priv->prs_shadow)
  1580. return -ENOMEM;
  1581. /* Always start from lookup = 0 */
  1582. for (index = 0; index < MVPP2_MAX_PORTS; index++)
  1583. mvpp2_prs_hw_port_init(priv, index, MVPP2_PRS_LU_MH,
  1584. MVPP2_PRS_PORT_LU_MAX, 0);
  1585. mvpp2_prs_def_flow_init(priv);
  1586. mvpp2_prs_mh_init(priv);
  1587. mvpp2_prs_mac_init(priv);
  1588. err = mvpp2_prs_etype_init(priv);
  1589. if (err)
  1590. return err;
  1591. return 0;
  1592. }
  1593. /* Compare MAC DA with tcam entry data */
  1594. static bool mvpp2_prs_mac_range_equals(struct mvpp2_prs_entry *pe,
  1595. const u8 *da, unsigned char *mask)
  1596. {
  1597. unsigned char tcam_byte, tcam_mask;
  1598. int index;
  1599. for (index = 0; index < ETH_ALEN; index++) {
  1600. mvpp2_prs_tcam_data_byte_get(pe, index, &tcam_byte, &tcam_mask);
  1601. if (tcam_mask != mask[index])
  1602. return false;
  1603. if ((tcam_mask & tcam_byte) != (da[index] & mask[index]))
  1604. return false;
  1605. }
  1606. return true;
  1607. }
  1608. /* Find tcam entry with matched pair <MAC DA, port> */
  1609. static struct mvpp2_prs_entry *
  1610. mvpp2_prs_mac_da_range_find(struct mvpp2 *priv, int pmap, const u8 *da,
  1611. unsigned char *mask, int udf_type)
  1612. {
  1613. struct mvpp2_prs_entry *pe;
  1614. int tid;
  1615. pe = kzalloc(sizeof(*pe), GFP_KERNEL);
  1616. if (!pe)
  1617. return NULL;
  1618. mvpp2_prs_tcam_lu_set(pe, MVPP2_PRS_LU_MAC);
  1619. /* Go through the all entires with MVPP2_PRS_LU_MAC */
  1620. for (tid = MVPP2_PE_FIRST_FREE_TID;
  1621. tid <= MVPP2_PE_LAST_FREE_TID; tid++) {
  1622. unsigned int entry_pmap;
  1623. if (!priv->prs_shadow[tid].valid ||
  1624. (priv->prs_shadow[tid].lu != MVPP2_PRS_LU_MAC) ||
  1625. (priv->prs_shadow[tid].udf != udf_type))
  1626. continue;
  1627. pe->index = tid;
  1628. mvpp2_prs_hw_read(priv, pe);
  1629. entry_pmap = mvpp2_prs_tcam_port_map_get(pe);
  1630. if (mvpp2_prs_mac_range_equals(pe, da, mask) &&
  1631. entry_pmap == pmap)
  1632. return pe;
  1633. }
  1634. kfree(pe);
  1635. return NULL;
  1636. }
  1637. /* Update parser's mac da entry */
  1638. static int mvpp2_prs_mac_da_accept(struct mvpp2 *priv, int port,
  1639. const u8 *da, bool add)
  1640. {
  1641. struct mvpp2_prs_entry *pe;
  1642. unsigned int pmap, len, ri;
  1643. unsigned char mask[ETH_ALEN] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
  1644. int tid;
  1645. /* Scan TCAM and see if entry with this <MAC DA, port> already exist */
  1646. pe = mvpp2_prs_mac_da_range_find(priv, (1 << port), da, mask,
  1647. MVPP2_PRS_UDF_MAC_DEF);
  1648. /* No such entry */
  1649. if (!pe) {
  1650. if (!add)
  1651. return 0;
  1652. /* Create new TCAM entry */
  1653. /* Find first range mac entry*/
  1654. for (tid = MVPP2_PE_FIRST_FREE_TID;
  1655. tid <= MVPP2_PE_LAST_FREE_TID; tid++)
  1656. if (priv->prs_shadow[tid].valid &&
  1657. (priv->prs_shadow[tid].lu == MVPP2_PRS_LU_MAC) &&
  1658. (priv->prs_shadow[tid].udf ==
  1659. MVPP2_PRS_UDF_MAC_RANGE))
  1660. break;
  1661. /* Go through the all entries from first to last */
  1662. tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
  1663. tid - 1);
  1664. if (tid < 0)
  1665. return tid;
  1666. pe = kzalloc(sizeof(*pe), GFP_KERNEL);
  1667. if (!pe)
  1668. return -1;
  1669. mvpp2_prs_tcam_lu_set(pe, MVPP2_PRS_LU_MAC);
  1670. pe->index = tid;
  1671. /* Mask all ports */
  1672. mvpp2_prs_tcam_port_map_set(pe, 0);
  1673. }
  1674. /* Update port mask */
  1675. mvpp2_prs_tcam_port_set(pe, port, add);
  1676. /* Invalidate the entry if no ports are left enabled */
  1677. pmap = mvpp2_prs_tcam_port_map_get(pe);
  1678. if (pmap == 0) {
  1679. if (add) {
  1680. kfree(pe);
  1681. return -1;
  1682. }
  1683. mvpp2_prs_hw_inv(priv, pe->index);
  1684. priv->prs_shadow[pe->index].valid = false;
  1685. kfree(pe);
  1686. return 0;
  1687. }
  1688. /* Continue - set next lookup */
  1689. mvpp2_prs_sram_next_lu_set(pe, MVPP2_PRS_LU_DSA);
  1690. /* Set match on DA */
  1691. len = ETH_ALEN;
  1692. while (len--)
  1693. mvpp2_prs_tcam_data_byte_set(pe, len, da[len], 0xff);
  1694. /* Set result info bits */
  1695. ri = MVPP2_PRS_RI_L2_UCAST | MVPP2_PRS_RI_MAC_ME_MASK;
  1696. mvpp2_prs_sram_ri_update(pe, ri, MVPP2_PRS_RI_L2_CAST_MASK |
  1697. MVPP2_PRS_RI_MAC_ME_MASK);
  1698. mvpp2_prs_shadow_ri_set(priv, pe->index, ri, MVPP2_PRS_RI_L2_CAST_MASK |
  1699. MVPP2_PRS_RI_MAC_ME_MASK);
  1700. /* Shift to ethertype */
  1701. mvpp2_prs_sram_shift_set(pe, 2 * ETH_ALEN,
  1702. MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
  1703. /* Update shadow table and hw entry */
  1704. priv->prs_shadow[pe->index].udf = MVPP2_PRS_UDF_MAC_DEF;
  1705. mvpp2_prs_shadow_set(priv, pe->index, MVPP2_PRS_LU_MAC);
  1706. mvpp2_prs_hw_write(priv, pe);
  1707. kfree(pe);
  1708. return 0;
  1709. }
  1710. static int mvpp2_prs_update_mac_da(struct mvpp2_port *port, const u8 *da)
  1711. {
  1712. int err;
  1713. /* Remove old parser entry */
  1714. err = mvpp2_prs_mac_da_accept(port->priv, port->id, port->dev_addr,
  1715. false);
  1716. if (err)
  1717. return err;
  1718. /* Add new parser entry */
  1719. err = mvpp2_prs_mac_da_accept(port->priv, port->id, da, true);
  1720. if (err)
  1721. return err;
  1722. /* Set addr in the device */
  1723. memcpy(port->dev_addr, da, ETH_ALEN);
  1724. return 0;
  1725. }
  1726. /* Set prs flow for the port */
  1727. static int mvpp2_prs_def_flow(struct mvpp2_port *port)
  1728. {
  1729. struct mvpp2_prs_entry *pe;
  1730. int tid;
  1731. pe = mvpp2_prs_flow_find(port->priv, port->id);
  1732. /* Such entry not exist */
  1733. if (!pe) {
  1734. /* Go through the all entires from last to first */
  1735. tid = mvpp2_prs_tcam_first_free(port->priv,
  1736. MVPP2_PE_LAST_FREE_TID,
  1737. MVPP2_PE_FIRST_FREE_TID);
  1738. if (tid < 0)
  1739. return tid;
  1740. pe = kzalloc(sizeof(*pe), GFP_KERNEL);
  1741. if (!pe)
  1742. return -ENOMEM;
  1743. mvpp2_prs_tcam_lu_set(pe, MVPP2_PRS_LU_FLOWS);
  1744. pe->index = tid;
  1745. /* Set flow ID*/
  1746. mvpp2_prs_sram_ai_update(pe, port->id, MVPP2_PRS_FLOW_ID_MASK);
  1747. mvpp2_prs_sram_bits_set(pe, MVPP2_PRS_SRAM_LU_DONE_BIT, 1);
  1748. /* Update shadow table */
  1749. mvpp2_prs_shadow_set(port->priv, pe->index, MVPP2_PRS_LU_FLOWS);
  1750. }
  1751. mvpp2_prs_tcam_port_map_set(pe, (1 << port->id));
  1752. mvpp2_prs_hw_write(port->priv, pe);
  1753. kfree(pe);
  1754. return 0;
  1755. }
  1756. /* Classifier configuration routines */
  1757. /* Update classification flow table registers */
  1758. static void mvpp2_cls_flow_write(struct mvpp2 *priv,
  1759. struct mvpp2_cls_flow_entry *fe)
  1760. {
  1761. mvpp2_write(priv, MVPP2_CLS_FLOW_INDEX_REG, fe->index);
  1762. mvpp2_write(priv, MVPP2_CLS_FLOW_TBL0_REG, fe->data[0]);
  1763. mvpp2_write(priv, MVPP2_CLS_FLOW_TBL1_REG, fe->data[1]);
  1764. mvpp2_write(priv, MVPP2_CLS_FLOW_TBL2_REG, fe->data[2]);
  1765. }
  1766. /* Update classification lookup table register */
  1767. static void mvpp2_cls_lookup_write(struct mvpp2 *priv,
  1768. struct mvpp2_cls_lookup_entry *le)
  1769. {
  1770. u32 val;
  1771. val = (le->way << MVPP2_CLS_LKP_INDEX_WAY_OFFS) | le->lkpid;
  1772. mvpp2_write(priv, MVPP2_CLS_LKP_INDEX_REG, val);
  1773. mvpp2_write(priv, MVPP2_CLS_LKP_TBL_REG, le->data);
  1774. }
  1775. /* Classifier default initialization */
  1776. static void mvpp2_cls_init(struct mvpp2 *priv)
  1777. {
  1778. struct mvpp2_cls_lookup_entry le;
  1779. struct mvpp2_cls_flow_entry fe;
  1780. int index;
  1781. /* Enable classifier */
  1782. mvpp2_write(priv, MVPP2_CLS_MODE_REG, MVPP2_CLS_MODE_ACTIVE_MASK);
  1783. /* Clear classifier flow table */
  1784. memset(&fe.data, 0, MVPP2_CLS_FLOWS_TBL_DATA_WORDS);
  1785. for (index = 0; index < MVPP2_CLS_FLOWS_TBL_SIZE; index++) {
  1786. fe.index = index;
  1787. mvpp2_cls_flow_write(priv, &fe);
  1788. }
  1789. /* Clear classifier lookup table */
  1790. le.data = 0;
  1791. for (index = 0; index < MVPP2_CLS_LKP_TBL_SIZE; index++) {
  1792. le.lkpid = index;
  1793. le.way = 0;
  1794. mvpp2_cls_lookup_write(priv, &le);
  1795. le.way = 1;
  1796. mvpp2_cls_lookup_write(priv, &le);
  1797. }
  1798. }
  1799. static void mvpp2_cls_port_config(struct mvpp2_port *port)
  1800. {
  1801. struct mvpp2_cls_lookup_entry le;
  1802. u32 val;
  1803. /* Set way for the port */
  1804. val = mvpp2_read(port->priv, MVPP2_CLS_PORT_WAY_REG);
  1805. val &= ~MVPP2_CLS_PORT_WAY_MASK(port->id);
  1806. mvpp2_write(port->priv, MVPP2_CLS_PORT_WAY_REG, val);
  1807. /* Pick the entry to be accessed in lookup ID decoding table
  1808. * according to the way and lkpid.
  1809. */
  1810. le.lkpid = port->id;
  1811. le.way = 0;
  1812. le.data = 0;
  1813. /* Set initial CPU queue for receiving packets */
  1814. le.data &= ~MVPP2_CLS_LKP_TBL_RXQ_MASK;
  1815. le.data |= port->first_rxq;
  1816. /* Disable classification engines */
  1817. le.data &= ~MVPP2_CLS_LKP_TBL_LOOKUP_EN_MASK;
  1818. /* Update lookup ID table entry */
  1819. mvpp2_cls_lookup_write(port->priv, &le);
  1820. }
  1821. /* Set CPU queue number for oversize packets */
  1822. static void mvpp2_cls_oversize_rxq_set(struct mvpp2_port *port)
  1823. {
  1824. u32 val;
  1825. mvpp2_write(port->priv, MVPP2_CLS_OVERSIZE_RXQ_LOW_REG(port->id),
  1826. port->first_rxq & MVPP2_CLS_OVERSIZE_RXQ_LOW_MASK);
  1827. mvpp2_write(port->priv, MVPP2_CLS_SWFWD_P2HQ_REG(port->id),
  1828. (port->first_rxq >> MVPP2_CLS_OVERSIZE_RXQ_LOW_BITS));
  1829. val = mvpp2_read(port->priv, MVPP2_CLS_SWFWD_PCTRL_REG);
  1830. val |= MVPP2_CLS_SWFWD_PCTRL_MASK(port->id);
  1831. mvpp2_write(port->priv, MVPP2_CLS_SWFWD_PCTRL_REG, val);
  1832. }
  1833. /* Buffer Manager configuration routines */
  1834. /* Create pool */
  1835. static int mvpp2_bm_pool_create(struct udevice *dev,
  1836. struct mvpp2 *priv,
  1837. struct mvpp2_bm_pool *bm_pool, int size)
  1838. {
  1839. u32 val;
  1840. bm_pool->virt_addr = buffer_loc.bm_pool[bm_pool->id];
  1841. bm_pool->phys_addr = (dma_addr_t)buffer_loc.bm_pool[bm_pool->id];
  1842. if (!bm_pool->virt_addr)
  1843. return -ENOMEM;
  1844. if (!IS_ALIGNED((u32)bm_pool->virt_addr, MVPP2_BM_POOL_PTR_ALIGN)) {
  1845. dev_err(&pdev->dev, "BM pool %d is not %d bytes aligned\n",
  1846. bm_pool->id, MVPP2_BM_POOL_PTR_ALIGN);
  1847. return -ENOMEM;
  1848. }
  1849. mvpp2_write(priv, MVPP2_BM_POOL_BASE_REG(bm_pool->id),
  1850. bm_pool->phys_addr);
  1851. mvpp2_write(priv, MVPP2_BM_POOL_SIZE_REG(bm_pool->id), size);
  1852. val = mvpp2_read(priv, MVPP2_BM_POOL_CTRL_REG(bm_pool->id));
  1853. val |= MVPP2_BM_START_MASK;
  1854. mvpp2_write(priv, MVPP2_BM_POOL_CTRL_REG(bm_pool->id), val);
  1855. bm_pool->type = MVPP2_BM_FREE;
  1856. bm_pool->size = size;
  1857. bm_pool->pkt_size = 0;
  1858. bm_pool->buf_num = 0;
  1859. return 0;
  1860. }
  1861. /* Set pool buffer size */
  1862. static void mvpp2_bm_pool_bufsize_set(struct mvpp2 *priv,
  1863. struct mvpp2_bm_pool *bm_pool,
  1864. int buf_size)
  1865. {
  1866. u32 val;
  1867. bm_pool->buf_size = buf_size;
  1868. val = ALIGN(buf_size, 1 << MVPP2_POOL_BUF_SIZE_OFFSET);
  1869. mvpp2_write(priv, MVPP2_POOL_BUF_SIZE_REG(bm_pool->id), val);
  1870. }
  1871. /* Free all buffers from the pool */
  1872. static void mvpp2_bm_bufs_free(struct udevice *dev, struct mvpp2 *priv,
  1873. struct mvpp2_bm_pool *bm_pool)
  1874. {
  1875. bm_pool->buf_num = 0;
  1876. }
  1877. /* Cleanup pool */
  1878. static int mvpp2_bm_pool_destroy(struct udevice *dev,
  1879. struct mvpp2 *priv,
  1880. struct mvpp2_bm_pool *bm_pool)
  1881. {
  1882. u32 val;
  1883. mvpp2_bm_bufs_free(dev, priv, bm_pool);
  1884. if (bm_pool->buf_num) {
  1885. dev_err(dev, "cannot free all buffers in pool %d\n", bm_pool->id);
  1886. return 0;
  1887. }
  1888. val = mvpp2_read(priv, MVPP2_BM_POOL_CTRL_REG(bm_pool->id));
  1889. val |= MVPP2_BM_STOP_MASK;
  1890. mvpp2_write(priv, MVPP2_BM_POOL_CTRL_REG(bm_pool->id), val);
  1891. return 0;
  1892. }
  1893. static int mvpp2_bm_pools_init(struct udevice *dev,
  1894. struct mvpp2 *priv)
  1895. {
  1896. int i, err, size;
  1897. struct mvpp2_bm_pool *bm_pool;
  1898. /* Create all pools with maximum size */
  1899. size = MVPP2_BM_POOL_SIZE_MAX;
  1900. for (i = 0; i < MVPP2_BM_POOLS_NUM; i++) {
  1901. bm_pool = &priv->bm_pools[i];
  1902. bm_pool->id = i;
  1903. err = mvpp2_bm_pool_create(dev, priv, bm_pool, size);
  1904. if (err)
  1905. goto err_unroll_pools;
  1906. mvpp2_bm_pool_bufsize_set(priv, bm_pool, 0);
  1907. }
  1908. return 0;
  1909. err_unroll_pools:
  1910. dev_err(&pdev->dev, "failed to create BM pool %d, size %d\n", i, size);
  1911. for (i = i - 1; i >= 0; i--)
  1912. mvpp2_bm_pool_destroy(dev, priv, &priv->bm_pools[i]);
  1913. return err;
  1914. }
  1915. static int mvpp2_bm_init(struct udevice *dev, struct mvpp2 *priv)
  1916. {
  1917. int i, err;
  1918. for (i = 0; i < MVPP2_BM_POOLS_NUM; i++) {
  1919. /* Mask BM all interrupts */
  1920. mvpp2_write(priv, MVPP2_BM_INTR_MASK_REG(i), 0);
  1921. /* Clear BM cause register */
  1922. mvpp2_write(priv, MVPP2_BM_INTR_CAUSE_REG(i), 0);
  1923. }
  1924. /* Allocate and initialize BM pools */
  1925. priv->bm_pools = devm_kcalloc(dev, MVPP2_BM_POOLS_NUM,
  1926. sizeof(struct mvpp2_bm_pool), GFP_KERNEL);
  1927. if (!priv->bm_pools)
  1928. return -ENOMEM;
  1929. err = mvpp2_bm_pools_init(dev, priv);
  1930. if (err < 0)
  1931. return err;
  1932. return 0;
  1933. }
  1934. /* Attach long pool to rxq */
  1935. static void mvpp2_rxq_long_pool_set(struct mvpp2_port *port,
  1936. int lrxq, int long_pool)
  1937. {
  1938. u32 val;
  1939. int prxq;
  1940. /* Get queue physical ID */
  1941. prxq = port->rxqs[lrxq]->id;
  1942. val = mvpp2_read(port->priv, MVPP2_RXQ_CONFIG_REG(prxq));
  1943. val &= ~MVPP2_RXQ_POOL_LONG_MASK;
  1944. val |= ((long_pool << MVPP2_RXQ_POOL_LONG_OFFS) &
  1945. MVPP2_RXQ_POOL_LONG_MASK);
  1946. mvpp2_write(port->priv, MVPP2_RXQ_CONFIG_REG(prxq), val);
  1947. }
  1948. /* Set pool number in a BM cookie */
  1949. static inline u32 mvpp2_bm_cookie_pool_set(u32 cookie, int pool)
  1950. {
  1951. u32 bm;
  1952. bm = cookie & ~(0xFF << MVPP2_BM_COOKIE_POOL_OFFS);
  1953. bm |= ((pool & 0xFF) << MVPP2_BM_COOKIE_POOL_OFFS);
  1954. return bm;
  1955. }
  1956. /* Get pool number from a BM cookie */
  1957. static inline int mvpp2_bm_cookie_pool_get(u32 cookie)
  1958. {
  1959. return (cookie >> MVPP2_BM_COOKIE_POOL_OFFS) & 0xFF;
  1960. }
  1961. /* Release buffer to BM */
  1962. static inline void mvpp2_bm_pool_put(struct mvpp2_port *port, int pool,
  1963. u32 buf_phys_addr, u32 buf_virt_addr)
  1964. {
  1965. mvpp2_write(port->priv, MVPP2_BM_VIRT_RLS_REG, buf_virt_addr);
  1966. mvpp2_write(port->priv, MVPP2_BM_PHY_RLS_REG(pool), buf_phys_addr);
  1967. }
  1968. /* Refill BM pool */
  1969. static void mvpp2_pool_refill(struct mvpp2_port *port, u32 bm,
  1970. u32 phys_addr, u32 cookie)
  1971. {
  1972. int pool = mvpp2_bm_cookie_pool_get(bm);
  1973. mvpp2_bm_pool_put(port, pool, phys_addr, cookie);
  1974. }
  1975. /* Allocate buffers for the pool */
  1976. static int mvpp2_bm_bufs_add(struct mvpp2_port *port,
  1977. struct mvpp2_bm_pool *bm_pool, int buf_num)
  1978. {
  1979. int i;
  1980. u32 bm;
  1981. if (buf_num < 0 ||
  1982. (buf_num + bm_pool->buf_num > bm_pool->size)) {
  1983. netdev_err(port->dev,
  1984. "cannot allocate %d buffers for pool %d\n",
  1985. buf_num, bm_pool->id);
  1986. return 0;
  1987. }
  1988. bm = mvpp2_bm_cookie_pool_set(0, bm_pool->id);
  1989. for (i = 0; i < buf_num; i++) {
  1990. mvpp2_pool_refill(port, bm, (u32)buffer_loc.rx_buffer[i],
  1991. (u32)buffer_loc.rx_buffer[i]);
  1992. }
  1993. /* Update BM driver with number of buffers added to pool */
  1994. bm_pool->buf_num += i;
  1995. bm_pool->in_use_thresh = bm_pool->buf_num / 4;
  1996. return i;
  1997. }
  1998. /* Notify the driver that BM pool is being used as specific type and return the
  1999. * pool pointer on success
  2000. */
  2001. static struct mvpp2_bm_pool *
  2002. mvpp2_bm_pool_use(struct mvpp2_port *port, int pool, enum mvpp2_bm_type type,
  2003. int pkt_size)
  2004. {
  2005. struct mvpp2_bm_pool *new_pool = &port->priv->bm_pools[pool];
  2006. int num;
  2007. if (new_pool->type != MVPP2_BM_FREE && new_pool->type != type) {
  2008. netdev_err(port->dev, "mixing pool types is forbidden\n");
  2009. return NULL;
  2010. }
  2011. if (new_pool->type == MVPP2_BM_FREE)
  2012. new_pool->type = type;
  2013. /* Allocate buffers in case BM pool is used as long pool, but packet
  2014. * size doesn't match MTU or BM pool hasn't being used yet
  2015. */
  2016. if (((type == MVPP2_BM_SWF_LONG) && (pkt_size > new_pool->pkt_size)) ||
  2017. (new_pool->pkt_size == 0)) {
  2018. int pkts_num;
  2019. /* Set default buffer number or free all the buffers in case
  2020. * the pool is not empty
  2021. */
  2022. pkts_num = new_pool->buf_num;
  2023. if (pkts_num == 0)
  2024. pkts_num = type == MVPP2_BM_SWF_LONG ?
  2025. MVPP2_BM_LONG_BUF_NUM :
  2026. MVPP2_BM_SHORT_BUF_NUM;
  2027. else
  2028. mvpp2_bm_bufs_free(NULL,
  2029. port->priv, new_pool);
  2030. new_pool->pkt_size = pkt_size;
  2031. /* Allocate buffers for this pool */
  2032. num = mvpp2_bm_bufs_add(port, new_pool, pkts_num);
  2033. if (num != pkts_num) {
  2034. dev_err(dev, "pool %d: %d of %d allocated\n",
  2035. new_pool->id, num, pkts_num);
  2036. return NULL;
  2037. }
  2038. }
  2039. mvpp2_bm_pool_bufsize_set(port->priv, new_pool,
  2040. MVPP2_RX_BUF_SIZE(new_pool->pkt_size));
  2041. return new_pool;
  2042. }
  2043. /* Initialize pools for swf */
  2044. static int mvpp2_swf_bm_pool_init(struct mvpp2_port *port)
  2045. {
  2046. int rxq;
  2047. if (!port->pool_long) {
  2048. port->pool_long =
  2049. mvpp2_bm_pool_use(port, MVPP2_BM_SWF_LONG_POOL(port->id),
  2050. MVPP2_BM_SWF_LONG,
  2051. port->pkt_size);
  2052. if (!port->pool_long)
  2053. return -ENOMEM;
  2054. port->pool_long->port_map |= (1 << port->id);
  2055. for (rxq = 0; rxq < rxq_number; rxq++)
  2056. mvpp2_rxq_long_pool_set(port, rxq, port->pool_long->id);
  2057. }
  2058. return 0;
  2059. }
  2060. /* Port configuration routines */
  2061. static void mvpp2_port_mii_set(struct mvpp2_port *port)
  2062. {
  2063. u32 val;
  2064. val = readl(port->base + MVPP2_GMAC_CTRL_2_REG);
  2065. switch (port->phy_interface) {
  2066. case PHY_INTERFACE_MODE_SGMII:
  2067. val |= MVPP2_GMAC_INBAND_AN_MASK;
  2068. break;
  2069. case PHY_INTERFACE_MODE_RGMII:
  2070. val |= MVPP2_GMAC_PORT_RGMII_MASK;
  2071. default:
  2072. val &= ~MVPP2_GMAC_PCS_ENABLE_MASK;
  2073. }
  2074. writel(val, port->base + MVPP2_GMAC_CTRL_2_REG);
  2075. }
  2076. static void mvpp2_port_fc_adv_enable(struct mvpp2_port *port)
  2077. {
  2078. u32 val;
  2079. val = readl(port->base + MVPP2_GMAC_AUTONEG_CONFIG);
  2080. val |= MVPP2_GMAC_FC_ADV_EN;
  2081. writel(val, port->base + MVPP2_GMAC_AUTONEG_CONFIG);
  2082. }
  2083. static void mvpp2_port_enable(struct mvpp2_port *port)
  2084. {
  2085. u32 val;
  2086. val = readl(port->base + MVPP2_GMAC_CTRL_0_REG);
  2087. val |= MVPP2_GMAC_PORT_EN_MASK;
  2088. val |= MVPP2_GMAC_MIB_CNTR_EN_MASK;
  2089. writel(val, port->base + MVPP2_GMAC_CTRL_0_REG);
  2090. }
  2091. static void mvpp2_port_disable(struct mvpp2_port *port)
  2092. {
  2093. u32 val;
  2094. val = readl(port->base + MVPP2_GMAC_CTRL_0_REG);
  2095. val &= ~(MVPP2_GMAC_PORT_EN_MASK);
  2096. writel(val, port->base + MVPP2_GMAC_CTRL_0_REG);
  2097. }
  2098. /* Set IEEE 802.3x Flow Control Xon Packet Transmission Mode */
  2099. static void mvpp2_port_periodic_xon_disable(struct mvpp2_port *port)
  2100. {
  2101. u32 val;
  2102. val = readl(port->base + MVPP2_GMAC_CTRL_1_REG) &
  2103. ~MVPP2_GMAC_PERIODIC_XON_EN_MASK;
  2104. writel(val, port->base + MVPP2_GMAC_CTRL_1_REG);
  2105. }
  2106. /* Configure loopback port */
  2107. static void mvpp2_port_loopback_set(struct mvpp2_port *port)
  2108. {
  2109. u32 val;
  2110. val = readl(port->base + MVPP2_GMAC_CTRL_1_REG);
  2111. if (port->speed == 1000)
  2112. val |= MVPP2_GMAC_GMII_LB_EN_MASK;
  2113. else
  2114. val &= ~MVPP2_GMAC_GMII_LB_EN_MASK;
  2115. if (port->phy_interface == PHY_INTERFACE_MODE_SGMII)
  2116. val |= MVPP2_GMAC_PCS_LB_EN_MASK;
  2117. else
  2118. val &= ~MVPP2_GMAC_PCS_LB_EN_MASK;
  2119. writel(val, port->base + MVPP2_GMAC_CTRL_1_REG);
  2120. }
  2121. static void mvpp2_port_reset(struct mvpp2_port *port)
  2122. {
  2123. u32 val;
  2124. val = readl(port->base + MVPP2_GMAC_CTRL_2_REG) &
  2125. ~MVPP2_GMAC_PORT_RESET_MASK;
  2126. writel(val, port->base + MVPP2_GMAC_CTRL_2_REG);
  2127. while (readl(port->base + MVPP2_GMAC_CTRL_2_REG) &
  2128. MVPP2_GMAC_PORT_RESET_MASK)
  2129. continue;
  2130. }
  2131. /* Change maximum receive size of the port */
  2132. static inline void mvpp2_gmac_max_rx_size_set(struct mvpp2_port *port)
  2133. {
  2134. u32 val;
  2135. val = readl(port->base + MVPP2_GMAC_CTRL_0_REG);
  2136. val &= ~MVPP2_GMAC_MAX_RX_SIZE_MASK;
  2137. val |= (((port->pkt_size - MVPP2_MH_SIZE) / 2) <<
  2138. MVPP2_GMAC_MAX_RX_SIZE_OFFS);
  2139. writel(val, port->base + MVPP2_GMAC_CTRL_0_REG);
  2140. }
  2141. /* Set defaults to the MVPP2 port */
  2142. static void mvpp2_defaults_set(struct mvpp2_port *port)
  2143. {
  2144. int tx_port_num, val, queue, ptxq, lrxq;
  2145. /* Configure port to loopback if needed */
  2146. if (port->flags & MVPP2_F_LOOPBACK)
  2147. mvpp2_port_loopback_set(port);
  2148. /* Update TX FIFO MIN Threshold */
  2149. val = readl(port->base + MVPP2_GMAC_PORT_FIFO_CFG_1_REG);
  2150. val &= ~MVPP2_GMAC_TX_FIFO_MIN_TH_ALL_MASK;
  2151. /* Min. TX threshold must be less than minimal packet length */
  2152. val |= MVPP2_GMAC_TX_FIFO_MIN_TH_MASK(64 - 4 - 2);
  2153. writel(val, port->base + MVPP2_GMAC_PORT_FIFO_CFG_1_REG);
  2154. /* Disable Legacy WRR, Disable EJP, Release from reset */
  2155. tx_port_num = mvpp2_egress_port(port);
  2156. mvpp2_write(port->priv, MVPP2_TXP_SCHED_PORT_INDEX_REG,
  2157. tx_port_num);
  2158. mvpp2_write(port->priv, MVPP2_TXP_SCHED_CMD_1_REG, 0);
  2159. /* Close bandwidth for all queues */
  2160. for (queue = 0; queue < MVPP2_MAX_TXQ; queue++) {
  2161. ptxq = mvpp2_txq_phys(port->id, queue);
  2162. mvpp2_write(port->priv,
  2163. MVPP2_TXQ_SCHED_TOKEN_CNTR_REG(ptxq), 0);
  2164. }
  2165. /* Set refill period to 1 usec, refill tokens
  2166. * and bucket size to maximum
  2167. */
  2168. mvpp2_write(port->priv, MVPP2_TXP_SCHED_PERIOD_REG, 0xc8);
  2169. val = mvpp2_read(port->priv, MVPP2_TXP_SCHED_REFILL_REG);
  2170. val &= ~MVPP2_TXP_REFILL_PERIOD_ALL_MASK;
  2171. val |= MVPP2_TXP_REFILL_PERIOD_MASK(1);
  2172. val |= MVPP2_TXP_REFILL_TOKENS_ALL_MASK;
  2173. mvpp2_write(port->priv, MVPP2_TXP_SCHED_REFILL_REG, val);
  2174. val = MVPP2_TXP_TOKEN_SIZE_MAX;
  2175. mvpp2_write(port->priv, MVPP2_TXP_SCHED_TOKEN_SIZE_REG, val);
  2176. /* Set MaximumLowLatencyPacketSize value to 256 */
  2177. mvpp2_write(port->priv, MVPP2_RX_CTRL_REG(port->id),
  2178. MVPP2_RX_USE_PSEUDO_FOR_CSUM_MASK |
  2179. MVPP2_RX_LOW_LATENCY_PKT_SIZE(256));
  2180. /* Enable Rx cache snoop */
  2181. for (lrxq = 0; lrxq < rxq_number; lrxq++) {
  2182. queue = port->rxqs[lrxq]->id;
  2183. val = mvpp2_read(port->priv, MVPP2_RXQ_CONFIG_REG(queue));
  2184. val |= MVPP2_SNOOP_PKT_SIZE_MASK |
  2185. MVPP2_SNOOP_BUF_HDR_MASK;
  2186. mvpp2_write(port->priv, MVPP2_RXQ_CONFIG_REG(queue), val);
  2187. }
  2188. }
  2189. /* Enable/disable receiving packets */
  2190. static void mvpp2_ingress_enable(struct mvpp2_port *port)
  2191. {
  2192. u32 val;
  2193. int lrxq, queue;
  2194. for (lrxq = 0; lrxq < rxq_number; lrxq++) {
  2195. queue = port->rxqs[lrxq]->id;
  2196. val = mvpp2_read(port->priv, MVPP2_RXQ_CONFIG_REG(queue));
  2197. val &= ~MVPP2_RXQ_DISABLE_MASK;
  2198. mvpp2_write(port->priv, MVPP2_RXQ_CONFIG_REG(queue), val);
  2199. }
  2200. }
  2201. static void mvpp2_ingress_disable(struct mvpp2_port *port)
  2202. {
  2203. u32 val;
  2204. int lrxq, queue;
  2205. for (lrxq = 0; lrxq < rxq_number; lrxq++) {
  2206. queue = port->rxqs[lrxq]->id;
  2207. val = mvpp2_read(port->priv, MVPP2_RXQ_CONFIG_REG(queue));
  2208. val |= MVPP2_RXQ_DISABLE_MASK;
  2209. mvpp2_write(port->priv, MVPP2_RXQ_CONFIG_REG(queue), val);
  2210. }
  2211. }
  2212. /* Enable transmit via physical egress queue
  2213. * - HW starts take descriptors from DRAM
  2214. */
  2215. static void mvpp2_egress_enable(struct mvpp2_port *port)
  2216. {
  2217. u32 qmap;
  2218. int queue;
  2219. int tx_port_num = mvpp2_egress_port(port);
  2220. /* Enable all initialized TXs. */
  2221. qmap = 0;
  2222. for (queue = 0; queue < txq_number; queue++) {
  2223. struct mvpp2_tx_queue *txq = port->txqs[queue];
  2224. if (txq->descs != NULL)
  2225. qmap |= (1 << queue);
  2226. }
  2227. mvpp2_write(port->priv, MVPP2_TXP_SCHED_PORT_INDEX_REG, tx_port_num);
  2228. mvpp2_write(port->priv, MVPP2_TXP_SCHED_Q_CMD_REG, qmap);
  2229. }
  2230. /* Disable transmit via physical egress queue
  2231. * - HW doesn't take descriptors from DRAM
  2232. */
  2233. static void mvpp2_egress_disable(struct mvpp2_port *port)
  2234. {
  2235. u32 reg_data;
  2236. int delay;
  2237. int tx_port_num = mvpp2_egress_port(port);
  2238. /* Issue stop command for active channels only */
  2239. mvpp2_write(port->priv, MVPP2_TXP_SCHED_PORT_INDEX_REG, tx_port_num);
  2240. reg_data = (mvpp2_read(port->priv, MVPP2_TXP_SCHED_Q_CMD_REG)) &
  2241. MVPP2_TXP_SCHED_ENQ_MASK;
  2242. if (reg_data != 0)
  2243. mvpp2_write(port->priv, MVPP2_TXP_SCHED_Q_CMD_REG,
  2244. (reg_data << MVPP2_TXP_SCHED_DISQ_OFFSET));
  2245. /* Wait for all Tx activity to terminate. */
  2246. delay = 0;
  2247. do {
  2248. if (delay >= MVPP2_TX_DISABLE_TIMEOUT_MSEC) {
  2249. netdev_warn(port->dev,
  2250. "Tx stop timed out, status=0x%08x\n",
  2251. reg_data);
  2252. break;
  2253. }
  2254. mdelay(1);
  2255. delay++;
  2256. /* Check port TX Command register that all
  2257. * Tx queues are stopped
  2258. */
  2259. reg_data = mvpp2_read(port->priv, MVPP2_TXP_SCHED_Q_CMD_REG);
  2260. } while (reg_data & MVPP2_TXP_SCHED_ENQ_MASK);
  2261. }
  2262. /* Rx descriptors helper methods */
  2263. /* Get number of Rx descriptors occupied by received packets */
  2264. static inline int
  2265. mvpp2_rxq_received(struct mvpp2_port *port, int rxq_id)
  2266. {
  2267. u32 val = mvpp2_read(port->priv, MVPP2_RXQ_STATUS_REG(rxq_id));
  2268. return val & MVPP2_RXQ_OCCUPIED_MASK;
  2269. }
  2270. /* Update Rx queue status with the number of occupied and available
  2271. * Rx descriptor slots.
  2272. */
  2273. static inline void
  2274. mvpp2_rxq_status_update(struct mvpp2_port *port, int rxq_id,
  2275. int used_count, int free_count)
  2276. {
  2277. /* Decrement the number of used descriptors and increment count
  2278. * increment the number of free descriptors.
  2279. */
  2280. u32 val = used_count | (free_count << MVPP2_RXQ_NUM_NEW_OFFSET);
  2281. mvpp2_write(port->priv, MVPP2_RXQ_STATUS_UPDATE_REG(rxq_id), val);
  2282. }
  2283. /* Get pointer to next RX descriptor to be processed by SW */
  2284. static inline struct mvpp2_rx_desc *
  2285. mvpp2_rxq_next_desc_get(struct mvpp2_rx_queue *rxq)
  2286. {
  2287. int rx_desc = rxq->next_desc_to_proc;
  2288. rxq->next_desc_to_proc = MVPP2_QUEUE_NEXT_DESC(rxq, rx_desc);
  2289. prefetch(rxq->descs + rxq->next_desc_to_proc);
  2290. return rxq->descs + rx_desc;
  2291. }
  2292. /* Set rx queue offset */
  2293. static void mvpp2_rxq_offset_set(struct mvpp2_port *port,
  2294. int prxq, int offset)
  2295. {
  2296. u32 val;
  2297. /* Convert offset from bytes to units of 32 bytes */
  2298. offset = offset >> 5;
  2299. val = mvpp2_read(port->priv, MVPP2_RXQ_CONFIG_REG(prxq));
  2300. val &= ~MVPP2_RXQ_PACKET_OFFSET_MASK;
  2301. /* Offset is in */
  2302. val |= ((offset << MVPP2_RXQ_PACKET_OFFSET_OFFS) &
  2303. MVPP2_RXQ_PACKET_OFFSET_MASK);
  2304. mvpp2_write(port->priv, MVPP2_RXQ_CONFIG_REG(prxq), val);
  2305. }
  2306. /* Obtain BM cookie information from descriptor */
  2307. static u32 mvpp2_bm_cookie_build(struct mvpp2_rx_desc *rx_desc)
  2308. {
  2309. int pool = (rx_desc->status & MVPP2_RXD_BM_POOL_ID_MASK) >>
  2310. MVPP2_RXD_BM_POOL_ID_OFFS;
  2311. int cpu = smp_processor_id();
  2312. return ((pool & 0xFF) << MVPP2_BM_COOKIE_POOL_OFFS) |
  2313. ((cpu & 0xFF) << MVPP2_BM_COOKIE_CPU_OFFS);
  2314. }
  2315. /* Tx descriptors helper methods */
  2316. /* Get number of Tx descriptors waiting to be transmitted by HW */
  2317. static int mvpp2_txq_pend_desc_num_get(struct mvpp2_port *port,
  2318. struct mvpp2_tx_queue *txq)
  2319. {
  2320. u32 val;
  2321. mvpp2_write(port->priv, MVPP2_TXQ_NUM_REG, txq->id);
  2322. val = mvpp2_read(port->priv, MVPP2_TXQ_PENDING_REG);
  2323. return val & MVPP2_TXQ_PENDING_MASK;
  2324. }
  2325. /* Get pointer to next Tx descriptor to be processed (send) by HW */
  2326. static struct mvpp2_tx_desc *
  2327. mvpp2_txq_next_desc_get(struct mvpp2_tx_queue *txq)
  2328. {
  2329. int tx_desc = txq->next_desc_to_proc;
  2330. txq->next_desc_to_proc = MVPP2_QUEUE_NEXT_DESC(txq, tx_desc);
  2331. return txq->descs + tx_desc;
  2332. }
  2333. /* Update HW with number of aggregated Tx descriptors to be sent */
  2334. static void mvpp2_aggr_txq_pend_desc_add(struct mvpp2_port *port, int pending)
  2335. {
  2336. /* aggregated access - relevant TXQ number is written in TX desc */
  2337. mvpp2_write(port->priv, MVPP2_AGGR_TXQ_UPDATE_REG, pending);
  2338. }
  2339. /* Get number of sent descriptors and decrement counter.
  2340. * The number of sent descriptors is returned.
  2341. * Per-CPU access
  2342. */
  2343. static inline int mvpp2_txq_sent_desc_proc(struct mvpp2_port *port,
  2344. struct mvpp2_tx_queue *txq)
  2345. {
  2346. u32 val;
  2347. /* Reading status reg resets transmitted descriptor counter */
  2348. val = mvpp2_read(port->priv, MVPP2_TXQ_SENT_REG(txq->id));
  2349. return (val & MVPP2_TRANSMITTED_COUNT_MASK) >>
  2350. MVPP2_TRANSMITTED_COUNT_OFFSET;
  2351. }
  2352. static void mvpp2_txq_sent_counter_clear(void *arg)
  2353. {
  2354. struct mvpp2_port *port = arg;
  2355. int queue;
  2356. for (queue = 0; queue < txq_number; queue++) {
  2357. int id = port->txqs[queue]->id;
  2358. mvpp2_read(port->priv, MVPP2_TXQ_SENT_REG(id));
  2359. }
  2360. }
  2361. /* Set max sizes for Tx queues */
  2362. static void mvpp2_txp_max_tx_size_set(struct mvpp2_port *port)
  2363. {
  2364. u32 val, size, mtu;
  2365. int txq, tx_port_num;
  2366. mtu = port->pkt_size * 8;
  2367. if (mtu > MVPP2_TXP_MTU_MAX)
  2368. mtu = MVPP2_TXP_MTU_MAX;
  2369. /* WA for wrong Token bucket update: Set MTU value = 3*real MTU value */
  2370. mtu = 3 * mtu;
  2371. /* Indirect access to registers */
  2372. tx_port_num = mvpp2_egress_port(port);
  2373. mvpp2_write(port->priv, MVPP2_TXP_SCHED_PORT_INDEX_REG, tx_port_num);
  2374. /* Set MTU */
  2375. val = mvpp2_read(port->priv, MVPP2_TXP_SCHED_MTU_REG);
  2376. val &= ~MVPP2_TXP_MTU_MAX;
  2377. val |= mtu;
  2378. mvpp2_write(port->priv, MVPP2_TXP_SCHED_MTU_REG, val);
  2379. /* TXP token size and all TXQs token size must be larger that MTU */
  2380. val = mvpp2_read(port->priv, MVPP2_TXP_SCHED_TOKEN_SIZE_REG);
  2381. size = val & MVPP2_TXP_TOKEN_SIZE_MAX;
  2382. if (size < mtu) {
  2383. size = mtu;
  2384. val &= ~MVPP2_TXP_TOKEN_SIZE_MAX;
  2385. val |= size;
  2386. mvpp2_write(port->priv, MVPP2_TXP_SCHED_TOKEN_SIZE_REG, val);
  2387. }
  2388. for (txq = 0; txq < txq_number; txq++) {
  2389. val = mvpp2_read(port->priv,
  2390. MVPP2_TXQ_SCHED_TOKEN_SIZE_REG(txq));
  2391. size = val & MVPP2_TXQ_TOKEN_SIZE_MAX;
  2392. if (size < mtu) {
  2393. size = mtu;
  2394. val &= ~MVPP2_TXQ_TOKEN_SIZE_MAX;
  2395. val |= size;
  2396. mvpp2_write(port->priv,
  2397. MVPP2_TXQ_SCHED_TOKEN_SIZE_REG(txq),
  2398. val);
  2399. }
  2400. }
  2401. }
  2402. /* Free Tx queue skbuffs */
  2403. static void mvpp2_txq_bufs_free(struct mvpp2_port *port,
  2404. struct mvpp2_tx_queue *txq,
  2405. struct mvpp2_txq_pcpu *txq_pcpu, int num)
  2406. {
  2407. int i;
  2408. for (i = 0; i < num; i++)
  2409. mvpp2_txq_inc_get(txq_pcpu);
  2410. }
  2411. static inline struct mvpp2_rx_queue *mvpp2_get_rx_queue(struct mvpp2_port *port,
  2412. u32 cause)
  2413. {
  2414. int queue = fls(cause) - 1;
  2415. return port->rxqs[queue];
  2416. }
  2417. static inline struct mvpp2_tx_queue *mvpp2_get_tx_queue(struct mvpp2_port *port,
  2418. u32 cause)
  2419. {
  2420. int queue = fls(cause) - 1;
  2421. return port->txqs[queue];
  2422. }
  2423. /* Rx/Tx queue initialization/cleanup methods */
  2424. /* Allocate and initialize descriptors for aggr TXQ */
  2425. static int mvpp2_aggr_txq_init(struct udevice *dev,
  2426. struct mvpp2_tx_queue *aggr_txq,
  2427. int desc_num, int cpu,
  2428. struct mvpp2 *priv)
  2429. {
  2430. /* Allocate memory for TX descriptors */
  2431. aggr_txq->descs = buffer_loc.aggr_tx_descs;
  2432. aggr_txq->descs_phys = (dma_addr_t)buffer_loc.aggr_tx_descs;
  2433. if (!aggr_txq->descs)
  2434. return -ENOMEM;
  2435. /* Make sure descriptor address is cache line size aligned */
  2436. BUG_ON(aggr_txq->descs !=
  2437. PTR_ALIGN(aggr_txq->descs, MVPP2_CPU_D_CACHE_LINE_SIZE));
  2438. aggr_txq->last_desc = aggr_txq->size - 1;
  2439. /* Aggr TXQ no reset WA */
  2440. aggr_txq->next_desc_to_proc = mvpp2_read(priv,
  2441. MVPP2_AGGR_TXQ_INDEX_REG(cpu));
  2442. /* Set Tx descriptors queue starting address */
  2443. /* indirect access */
  2444. mvpp2_write(priv, MVPP2_AGGR_TXQ_DESC_ADDR_REG(cpu),
  2445. aggr_txq->descs_phys);
  2446. mvpp2_write(priv, MVPP2_AGGR_TXQ_DESC_SIZE_REG(cpu), desc_num);
  2447. return 0;
  2448. }
  2449. /* Create a specified Rx queue */
  2450. static int mvpp2_rxq_init(struct mvpp2_port *port,
  2451. struct mvpp2_rx_queue *rxq)
  2452. {
  2453. rxq->size = port->rx_ring_size;
  2454. /* Allocate memory for RX descriptors */
  2455. rxq->descs = buffer_loc.rx_descs;
  2456. rxq->descs_phys = (dma_addr_t)buffer_loc.rx_descs;
  2457. if (!rxq->descs)
  2458. return -ENOMEM;
  2459. BUG_ON(rxq->descs !=
  2460. PTR_ALIGN(rxq->descs, MVPP2_CPU_D_CACHE_LINE_SIZE));
  2461. rxq->last_desc = rxq->size - 1;
  2462. /* Zero occupied and non-occupied counters - direct access */
  2463. mvpp2_write(port->priv, MVPP2_RXQ_STATUS_REG(rxq->id), 0);
  2464. /* Set Rx descriptors queue starting address - indirect access */
  2465. mvpp2_write(port->priv, MVPP2_RXQ_NUM_REG, rxq->id);
  2466. mvpp2_write(port->priv, MVPP2_RXQ_DESC_ADDR_REG, rxq->descs_phys);
  2467. mvpp2_write(port->priv, MVPP2_RXQ_DESC_SIZE_REG, rxq->size);
  2468. mvpp2_write(port->priv, MVPP2_RXQ_INDEX_REG, 0);
  2469. /* Set Offset */
  2470. mvpp2_rxq_offset_set(port, rxq->id, NET_SKB_PAD);
  2471. /* Add number of descriptors ready for receiving packets */
  2472. mvpp2_rxq_status_update(port, rxq->id, 0, rxq->size);
  2473. return 0;
  2474. }
  2475. /* Push packets received by the RXQ to BM pool */
  2476. static void mvpp2_rxq_drop_pkts(struct mvpp2_port *port,
  2477. struct mvpp2_rx_queue *rxq)
  2478. {
  2479. int rx_received, i;
  2480. rx_received = mvpp2_rxq_received(port, rxq->id);
  2481. if (!rx_received)
  2482. return;
  2483. for (i = 0; i < rx_received; i++) {
  2484. struct mvpp2_rx_desc *rx_desc = mvpp2_rxq_next_desc_get(rxq);
  2485. u32 bm = mvpp2_bm_cookie_build(rx_desc);
  2486. mvpp2_pool_refill(port, bm, rx_desc->buf_phys_addr,
  2487. rx_desc->buf_cookie);
  2488. }
  2489. mvpp2_rxq_status_update(port, rxq->id, rx_received, rx_received);
  2490. }
  2491. /* Cleanup Rx queue */
  2492. static void mvpp2_rxq_deinit(struct mvpp2_port *port,
  2493. struct mvpp2_rx_queue *rxq)
  2494. {
  2495. mvpp2_rxq_drop_pkts(port, rxq);
  2496. rxq->descs = NULL;
  2497. rxq->last_desc = 0;
  2498. rxq->next_desc_to_proc = 0;
  2499. rxq->descs_phys = 0;
  2500. /* Clear Rx descriptors queue starting address and size;
  2501. * free descriptor number
  2502. */
  2503. mvpp2_write(port->priv, MVPP2_RXQ_STATUS_REG(rxq->id), 0);
  2504. mvpp2_write(port->priv, MVPP2_RXQ_NUM_REG, rxq->id);
  2505. mvpp2_write(port->priv, MVPP2_RXQ_DESC_ADDR_REG, 0);
  2506. mvpp2_write(port->priv, MVPP2_RXQ_DESC_SIZE_REG, 0);
  2507. }
  2508. /* Create and initialize a Tx queue */
  2509. static int mvpp2_txq_init(struct mvpp2_port *port,
  2510. struct mvpp2_tx_queue *txq)
  2511. {
  2512. u32 val;
  2513. int cpu, desc, desc_per_txq, tx_port_num;
  2514. struct mvpp2_txq_pcpu *txq_pcpu;
  2515. txq->size = port->tx_ring_size;
  2516. /* Allocate memory for Tx descriptors */
  2517. txq->descs = buffer_loc.tx_descs;
  2518. txq->descs_phys = (dma_addr_t)buffer_loc.tx_descs;
  2519. if (!txq->descs)
  2520. return -ENOMEM;
  2521. /* Make sure descriptor address is cache line size aligned */
  2522. BUG_ON(txq->descs !=
  2523. PTR_ALIGN(txq->descs, MVPP2_CPU_D_CACHE_LINE_SIZE));
  2524. txq->last_desc = txq->size - 1;
  2525. /* Set Tx descriptors queue starting address - indirect access */
  2526. mvpp2_write(port->priv, MVPP2_TXQ_NUM_REG, txq->id);
  2527. mvpp2_write(port->priv, MVPP2_TXQ_DESC_ADDR_REG, txq->descs_phys);
  2528. mvpp2_write(port->priv, MVPP2_TXQ_DESC_SIZE_REG, txq->size &
  2529. MVPP2_TXQ_DESC_SIZE_MASK);
  2530. mvpp2_write(port->priv, MVPP2_TXQ_INDEX_REG, 0);
  2531. mvpp2_write(port->priv, MVPP2_TXQ_RSVD_CLR_REG,
  2532. txq->id << MVPP2_TXQ_RSVD_CLR_OFFSET);
  2533. val = mvpp2_read(port->priv, MVPP2_TXQ_PENDING_REG);
  2534. val &= ~MVPP2_TXQ_PENDING_MASK;
  2535. mvpp2_write(port->priv, MVPP2_TXQ_PENDING_REG, val);
  2536. /* Calculate base address in prefetch buffer. We reserve 16 descriptors
  2537. * for each existing TXQ.
  2538. * TCONTS for PON port must be continuous from 0 to MVPP2_MAX_TCONT
  2539. * GBE ports assumed to be continious from 0 to MVPP2_MAX_PORTS
  2540. */
  2541. desc_per_txq = 16;
  2542. desc = (port->id * MVPP2_MAX_TXQ * desc_per_txq) +
  2543. (txq->log_id * desc_per_txq);
  2544. mvpp2_write(port->priv, MVPP2_TXQ_PREF_BUF_REG,
  2545. MVPP2_PREF_BUF_PTR(desc) | MVPP2_PREF_BUF_SIZE_16 |
  2546. MVPP2_PREF_BUF_THRESH(desc_per_txq/2));
  2547. /* WRR / EJP configuration - indirect access */
  2548. tx_port_num = mvpp2_egress_port(port);
  2549. mvpp2_write(port->priv, MVPP2_TXP_SCHED_PORT_INDEX_REG, tx_port_num);
  2550. val = mvpp2_read(port->priv, MVPP2_TXQ_SCHED_REFILL_REG(txq->log_id));
  2551. val &= ~MVPP2_TXQ_REFILL_PERIOD_ALL_MASK;
  2552. val |= MVPP2_TXQ_REFILL_PERIOD_MASK(1);
  2553. val |= MVPP2_TXQ_REFILL_TOKENS_ALL_MASK;
  2554. mvpp2_write(port->priv, MVPP2_TXQ_SCHED_REFILL_REG(txq->log_id), val);
  2555. val = MVPP2_TXQ_TOKEN_SIZE_MAX;
  2556. mvpp2_write(port->priv, MVPP2_TXQ_SCHED_TOKEN_SIZE_REG(txq->log_id),
  2557. val);
  2558. for_each_present_cpu(cpu) {
  2559. txq_pcpu = per_cpu_ptr(txq->pcpu, cpu);
  2560. txq_pcpu->size = txq->size;
  2561. }
  2562. return 0;
  2563. }
  2564. /* Free allocated TXQ resources */
  2565. static void mvpp2_txq_deinit(struct mvpp2_port *port,
  2566. struct mvpp2_tx_queue *txq)
  2567. {
  2568. txq->descs = NULL;
  2569. txq->last_desc = 0;
  2570. txq->next_desc_to_proc = 0;
  2571. txq->descs_phys = 0;
  2572. /* Set minimum bandwidth for disabled TXQs */
  2573. mvpp2_write(port->priv, MVPP2_TXQ_SCHED_TOKEN_CNTR_REG(txq->id), 0);
  2574. /* Set Tx descriptors queue starting address and size */
  2575. mvpp2_write(port->priv, MVPP2_TXQ_NUM_REG, txq->id);
  2576. mvpp2_write(port->priv, MVPP2_TXQ_DESC_ADDR_REG, 0);
  2577. mvpp2_write(port->priv, MVPP2_TXQ_DESC_SIZE_REG, 0);
  2578. }
  2579. /* Cleanup Tx ports */
  2580. static void mvpp2_txq_clean(struct mvpp2_port *port, struct mvpp2_tx_queue *txq)
  2581. {
  2582. struct mvpp2_txq_pcpu *txq_pcpu;
  2583. int delay, pending, cpu;
  2584. u32 val;
  2585. mvpp2_write(port->priv, MVPP2_TXQ_NUM_REG, txq->id);
  2586. val = mvpp2_read(port->priv, MVPP2_TXQ_PREF_BUF_REG);
  2587. val |= MVPP2_TXQ_DRAIN_EN_MASK;
  2588. mvpp2_write(port->priv, MVPP2_TXQ_PREF_BUF_REG, val);
  2589. /* The napi queue has been stopped so wait for all packets
  2590. * to be transmitted.
  2591. */
  2592. delay = 0;
  2593. do {
  2594. if (delay >= MVPP2_TX_PENDING_TIMEOUT_MSEC) {
  2595. netdev_warn(port->dev,
  2596. "port %d: cleaning queue %d timed out\n",
  2597. port->id, txq->log_id);
  2598. break;
  2599. }
  2600. mdelay(1);
  2601. delay++;
  2602. pending = mvpp2_txq_pend_desc_num_get(port, txq);
  2603. } while (pending);
  2604. val &= ~MVPP2_TXQ_DRAIN_EN_MASK;
  2605. mvpp2_write(port->priv, MVPP2_TXQ_PREF_BUF_REG, val);
  2606. for_each_present_cpu(cpu) {
  2607. txq_pcpu = per_cpu_ptr(txq->pcpu, cpu);
  2608. /* Release all packets */
  2609. mvpp2_txq_bufs_free(port, txq, txq_pcpu, txq_pcpu->count);
  2610. /* Reset queue */
  2611. txq_pcpu->count = 0;
  2612. txq_pcpu->txq_put_index = 0;
  2613. txq_pcpu->txq_get_index = 0;
  2614. }
  2615. }
  2616. /* Cleanup all Tx queues */
  2617. static void mvpp2_cleanup_txqs(struct mvpp2_port *port)
  2618. {
  2619. struct mvpp2_tx_queue *txq;
  2620. int queue;
  2621. u32 val;
  2622. val = mvpp2_read(port->priv, MVPP2_TX_PORT_FLUSH_REG);
  2623. /* Reset Tx ports and delete Tx queues */
  2624. val |= MVPP2_TX_PORT_FLUSH_MASK(port->id);
  2625. mvpp2_write(port->priv, MVPP2_TX_PORT_FLUSH_REG, val);
  2626. for (queue = 0; queue < txq_number; queue++) {
  2627. txq = port->txqs[queue];
  2628. mvpp2_txq_clean(port, txq);
  2629. mvpp2_txq_deinit(port, txq);
  2630. }
  2631. mvpp2_txq_sent_counter_clear(port);
  2632. val &= ~MVPP2_TX_PORT_FLUSH_MASK(port->id);
  2633. mvpp2_write(port->priv, MVPP2_TX_PORT_FLUSH_REG, val);
  2634. }
  2635. /* Cleanup all Rx queues */
  2636. static void mvpp2_cleanup_rxqs(struct mvpp2_port *port)
  2637. {
  2638. int queue;
  2639. for (queue = 0; queue < rxq_number; queue++)
  2640. mvpp2_rxq_deinit(port, port->rxqs[queue]);
  2641. }
  2642. /* Init all Rx queues for port */
  2643. static int mvpp2_setup_rxqs(struct mvpp2_port *port)
  2644. {
  2645. int queue, err;
  2646. for (queue = 0; queue < rxq_number; queue++) {
  2647. err = mvpp2_rxq_init(port, port->rxqs[queue]);
  2648. if (err)
  2649. goto err_cleanup;
  2650. }
  2651. return 0;
  2652. err_cleanup:
  2653. mvpp2_cleanup_rxqs(port);
  2654. return err;
  2655. }
  2656. /* Init all tx queues for port */
  2657. static int mvpp2_setup_txqs(struct mvpp2_port *port)
  2658. {
  2659. struct mvpp2_tx_queue *txq;
  2660. int queue, err;
  2661. for (queue = 0; queue < txq_number; queue++) {
  2662. txq = port->txqs[queue];
  2663. err = mvpp2_txq_init(port, txq);
  2664. if (err)
  2665. goto err_cleanup;
  2666. }
  2667. mvpp2_txq_sent_counter_clear(port);
  2668. return 0;
  2669. err_cleanup:
  2670. mvpp2_cleanup_txqs(port);
  2671. return err;
  2672. }
  2673. /* Adjust link */
  2674. static void mvpp2_link_event(struct mvpp2_port *port)
  2675. {
  2676. struct phy_device *phydev = port->phy_dev;
  2677. int status_change = 0;
  2678. u32 val;
  2679. if (phydev->link) {
  2680. if ((port->speed != phydev->speed) ||
  2681. (port->duplex != phydev->duplex)) {
  2682. u32 val;
  2683. val = readl(port->base + MVPP2_GMAC_AUTONEG_CONFIG);
  2684. val &= ~(MVPP2_GMAC_CONFIG_MII_SPEED |
  2685. MVPP2_GMAC_CONFIG_GMII_SPEED |
  2686. MVPP2_GMAC_CONFIG_FULL_DUPLEX |
  2687. MVPP2_GMAC_AN_SPEED_EN |
  2688. MVPP2_GMAC_AN_DUPLEX_EN);
  2689. if (phydev->duplex)
  2690. val |= MVPP2_GMAC_CONFIG_FULL_DUPLEX;
  2691. if (phydev->speed == SPEED_1000)
  2692. val |= MVPP2_GMAC_CONFIG_GMII_SPEED;
  2693. else if (phydev->speed == SPEED_100)
  2694. val |= MVPP2_GMAC_CONFIG_MII_SPEED;
  2695. writel(val, port->base + MVPP2_GMAC_AUTONEG_CONFIG);
  2696. port->duplex = phydev->duplex;
  2697. port->speed = phydev->speed;
  2698. }
  2699. }
  2700. if (phydev->link != port->link) {
  2701. if (!phydev->link) {
  2702. port->duplex = -1;
  2703. port->speed = 0;
  2704. }
  2705. port->link = phydev->link;
  2706. status_change = 1;
  2707. }
  2708. if (status_change) {
  2709. if (phydev->link) {
  2710. val = readl(port->base + MVPP2_GMAC_AUTONEG_CONFIG);
  2711. val |= (MVPP2_GMAC_FORCE_LINK_PASS |
  2712. MVPP2_GMAC_FORCE_LINK_DOWN);
  2713. writel(val, port->base + MVPP2_GMAC_AUTONEG_CONFIG);
  2714. mvpp2_egress_enable(port);
  2715. mvpp2_ingress_enable(port);
  2716. } else {
  2717. mvpp2_ingress_disable(port);
  2718. mvpp2_egress_disable(port);
  2719. }
  2720. }
  2721. }
  2722. /* Main RX/TX processing routines */
  2723. /* Display more error info */
  2724. static void mvpp2_rx_error(struct mvpp2_port *port,
  2725. struct mvpp2_rx_desc *rx_desc)
  2726. {
  2727. u32 status = rx_desc->status;
  2728. switch (status & MVPP2_RXD_ERR_CODE_MASK) {
  2729. case MVPP2_RXD_ERR_CRC:
  2730. netdev_err(port->dev, "bad rx status %08x (crc error), size=%d\n",
  2731. status, rx_desc->data_size);
  2732. break;
  2733. case MVPP2_RXD_ERR_OVERRUN:
  2734. netdev_err(port->dev, "bad rx status %08x (overrun error), size=%d\n",
  2735. status, rx_desc->data_size);
  2736. break;
  2737. case MVPP2_RXD_ERR_RESOURCE:
  2738. netdev_err(port->dev, "bad rx status %08x (resource error), size=%d\n",
  2739. status, rx_desc->data_size);
  2740. break;
  2741. }
  2742. }
  2743. /* Reuse skb if possible, or allocate a new skb and add it to BM pool */
  2744. static int mvpp2_rx_refill(struct mvpp2_port *port,
  2745. struct mvpp2_bm_pool *bm_pool,
  2746. u32 bm, u32 phys_addr)
  2747. {
  2748. mvpp2_pool_refill(port, bm, phys_addr, phys_addr);
  2749. return 0;
  2750. }
  2751. /* Set hw internals when starting port */
  2752. static void mvpp2_start_dev(struct mvpp2_port *port)
  2753. {
  2754. mvpp2_gmac_max_rx_size_set(port);
  2755. mvpp2_txp_max_tx_size_set(port);
  2756. mvpp2_port_enable(port);
  2757. }
  2758. /* Set hw internals when stopping port */
  2759. static void mvpp2_stop_dev(struct mvpp2_port *port)
  2760. {
  2761. /* Stop new packets from arriving to RXQs */
  2762. mvpp2_ingress_disable(port);
  2763. mvpp2_egress_disable(port);
  2764. mvpp2_port_disable(port);
  2765. }
  2766. static int mvpp2_phy_connect(struct udevice *dev, struct mvpp2_port *port)
  2767. {
  2768. struct phy_device *phy_dev;
  2769. if (!port->init || port->link == 0) {
  2770. phy_dev = phy_connect(port->priv->bus, port->phyaddr, dev,
  2771. port->phy_interface);
  2772. port->phy_dev = phy_dev;
  2773. if (!phy_dev) {
  2774. netdev_err(port->dev, "cannot connect to phy\n");
  2775. return -ENODEV;
  2776. }
  2777. phy_dev->supported &= PHY_GBIT_FEATURES;
  2778. phy_dev->advertising = phy_dev->supported;
  2779. port->phy_dev = phy_dev;
  2780. port->link = 0;
  2781. port->duplex = 0;
  2782. port->speed = 0;
  2783. phy_config(phy_dev);
  2784. phy_startup(phy_dev);
  2785. if (!phy_dev->link) {
  2786. printf("%s: No link\n", phy_dev->dev->name);
  2787. return -1;
  2788. }
  2789. port->init = 1;
  2790. } else {
  2791. mvpp2_egress_enable(port);
  2792. mvpp2_ingress_enable(port);
  2793. }
  2794. return 0;
  2795. }
  2796. static int mvpp2_open(struct udevice *dev, struct mvpp2_port *port)
  2797. {
  2798. unsigned char mac_bcast[ETH_ALEN] = {
  2799. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
  2800. int err;
  2801. err = mvpp2_prs_mac_da_accept(port->priv, port->id, mac_bcast, true);
  2802. if (err) {
  2803. netdev_err(dev, "mvpp2_prs_mac_da_accept BC failed\n");
  2804. return err;
  2805. }
  2806. err = mvpp2_prs_mac_da_accept(port->priv, port->id,
  2807. port->dev_addr, true);
  2808. if (err) {
  2809. netdev_err(dev, "mvpp2_prs_mac_da_accept MC failed\n");
  2810. return err;
  2811. }
  2812. err = mvpp2_prs_def_flow(port);
  2813. if (err) {
  2814. netdev_err(dev, "mvpp2_prs_def_flow failed\n");
  2815. return err;
  2816. }
  2817. /* Allocate the Rx/Tx queues */
  2818. err = mvpp2_setup_rxqs(port);
  2819. if (err) {
  2820. netdev_err(port->dev, "cannot allocate Rx queues\n");
  2821. return err;
  2822. }
  2823. err = mvpp2_setup_txqs(port);
  2824. if (err) {
  2825. netdev_err(port->dev, "cannot allocate Tx queues\n");
  2826. return err;
  2827. }
  2828. err = mvpp2_phy_connect(dev, port);
  2829. if (err < 0)
  2830. return err;
  2831. mvpp2_link_event(port);
  2832. mvpp2_start_dev(port);
  2833. return 0;
  2834. }
  2835. /* No Device ops here in U-Boot */
  2836. /* Driver initialization */
  2837. static void mvpp2_port_power_up(struct mvpp2_port *port)
  2838. {
  2839. mvpp2_port_mii_set(port);
  2840. mvpp2_port_periodic_xon_disable(port);
  2841. mvpp2_port_fc_adv_enable(port);
  2842. mvpp2_port_reset(port);
  2843. }
  2844. /* Initialize port HW */
  2845. static int mvpp2_port_init(struct udevice *dev, struct mvpp2_port *port)
  2846. {
  2847. struct mvpp2 *priv = port->priv;
  2848. struct mvpp2_txq_pcpu *txq_pcpu;
  2849. int queue, cpu, err;
  2850. if (port->first_rxq + rxq_number > MVPP2_RXQ_TOTAL_NUM)
  2851. return -EINVAL;
  2852. /* Disable port */
  2853. mvpp2_egress_disable(port);
  2854. mvpp2_port_disable(port);
  2855. port->txqs = devm_kcalloc(dev, txq_number, sizeof(*port->txqs),
  2856. GFP_KERNEL);
  2857. if (!port->txqs)
  2858. return -ENOMEM;
  2859. /* Associate physical Tx queues to this port and initialize.
  2860. * The mapping is predefined.
  2861. */
  2862. for (queue = 0; queue < txq_number; queue++) {
  2863. int queue_phy_id = mvpp2_txq_phys(port->id, queue);
  2864. struct mvpp2_tx_queue *txq;
  2865. txq = devm_kzalloc(dev, sizeof(*txq), GFP_KERNEL);
  2866. if (!txq)
  2867. return -ENOMEM;
  2868. txq->pcpu = devm_kzalloc(dev, sizeof(struct mvpp2_txq_pcpu),
  2869. GFP_KERNEL);
  2870. if (!txq->pcpu)
  2871. return -ENOMEM;
  2872. txq->id = queue_phy_id;
  2873. txq->log_id = queue;
  2874. txq->done_pkts_coal = MVPP2_TXDONE_COAL_PKTS_THRESH;
  2875. for_each_present_cpu(cpu) {
  2876. txq_pcpu = per_cpu_ptr(txq->pcpu, cpu);
  2877. txq_pcpu->cpu = cpu;
  2878. }
  2879. port->txqs[queue] = txq;
  2880. }
  2881. port->rxqs = devm_kcalloc(dev, rxq_number, sizeof(*port->rxqs),
  2882. GFP_KERNEL);
  2883. if (!port->rxqs)
  2884. return -ENOMEM;
  2885. /* Allocate and initialize Rx queue for this port */
  2886. for (queue = 0; queue < rxq_number; queue++) {
  2887. struct mvpp2_rx_queue *rxq;
  2888. /* Map physical Rx queue to port's logical Rx queue */
  2889. rxq = devm_kzalloc(dev, sizeof(*rxq), GFP_KERNEL);
  2890. if (!rxq)
  2891. return -ENOMEM;
  2892. /* Map this Rx queue to a physical queue */
  2893. rxq->id = port->first_rxq + queue;
  2894. rxq->port = port->id;
  2895. rxq->logic_rxq = queue;
  2896. port->rxqs[queue] = rxq;
  2897. }
  2898. /* Configure Rx queue group interrupt for this port */
  2899. mvpp2_write(priv, MVPP2_ISR_RXQ_GROUP_REG(port->id), CONFIG_MV_ETH_RXQ);
  2900. /* Create Rx descriptor rings */
  2901. for (queue = 0; queue < rxq_number; queue++) {
  2902. struct mvpp2_rx_queue *rxq = port->rxqs[queue];
  2903. rxq->size = port->rx_ring_size;
  2904. rxq->pkts_coal = MVPP2_RX_COAL_PKTS;
  2905. rxq->time_coal = MVPP2_RX_COAL_USEC;
  2906. }
  2907. mvpp2_ingress_disable(port);
  2908. /* Port default configuration */
  2909. mvpp2_defaults_set(port);
  2910. /* Port's classifier configuration */
  2911. mvpp2_cls_oversize_rxq_set(port);
  2912. mvpp2_cls_port_config(port);
  2913. /* Provide an initial Rx packet size */
  2914. port->pkt_size = MVPP2_RX_PKT_SIZE(PKTSIZE_ALIGN);
  2915. /* Initialize pools for swf */
  2916. err = mvpp2_swf_bm_pool_init(port);
  2917. if (err)
  2918. return err;
  2919. return 0;
  2920. }
  2921. /* Ports initialization */
  2922. static int mvpp2_port_probe(struct udevice *dev,
  2923. struct mvpp2_port *port,
  2924. int port_node,
  2925. struct mvpp2 *priv,
  2926. int *next_first_rxq)
  2927. {
  2928. int phy_node;
  2929. u32 id;
  2930. u32 phyaddr;
  2931. const char *phy_mode_str;
  2932. int phy_mode = -1;
  2933. int priv_common_regs_num = 2;
  2934. int err;
  2935. phy_node = fdtdec_lookup_phandle(gd->fdt_blob, port_node, "phy");
  2936. if (phy_node < 0) {
  2937. dev_err(&pdev->dev, "missing phy\n");
  2938. return -ENODEV;
  2939. }
  2940. phy_mode_str = fdt_getprop(gd->fdt_blob, port_node, "phy-mode", NULL);
  2941. if (phy_mode_str)
  2942. phy_mode = phy_get_interface_by_name(phy_mode_str);
  2943. if (phy_mode == -1) {
  2944. dev_err(&pdev->dev, "incorrect phy mode\n");
  2945. return -EINVAL;
  2946. }
  2947. id = fdtdec_get_int(gd->fdt_blob, port_node, "port-id", -1);
  2948. if (id == -1) {
  2949. dev_err(&pdev->dev, "missing port-id value\n");
  2950. return -EINVAL;
  2951. }
  2952. phyaddr = fdtdec_get_int(gd->fdt_blob, phy_node, "reg", 0);
  2953. port->priv = priv;
  2954. port->id = id;
  2955. port->first_rxq = *next_first_rxq;
  2956. port->phy_node = phy_node;
  2957. port->phy_interface = phy_mode;
  2958. port->phyaddr = phyaddr;
  2959. port->base = (void __iomem *)dev_get_addr_index(dev->parent,
  2960. priv_common_regs_num
  2961. + id);
  2962. if (IS_ERR(port->base))
  2963. return PTR_ERR(port->base);
  2964. port->tx_ring_size = MVPP2_MAX_TXD;
  2965. port->rx_ring_size = MVPP2_MAX_RXD;
  2966. err = mvpp2_port_init(dev, port);
  2967. if (err < 0) {
  2968. dev_err(&pdev->dev, "failed to init port %d\n", id);
  2969. return err;
  2970. }
  2971. mvpp2_port_power_up(port);
  2972. /* Increment the first Rx queue number to be used by the next port */
  2973. *next_first_rxq += CONFIG_MV_ETH_RXQ;
  2974. priv->port_list[id] = port;
  2975. return 0;
  2976. }
  2977. /* Initialize decoding windows */
  2978. static void mvpp2_conf_mbus_windows(const struct mbus_dram_target_info *dram,
  2979. struct mvpp2 *priv)
  2980. {
  2981. u32 win_enable;
  2982. int i;
  2983. for (i = 0; i < 6; i++) {
  2984. mvpp2_write(priv, MVPP2_WIN_BASE(i), 0);
  2985. mvpp2_write(priv, MVPP2_WIN_SIZE(i), 0);
  2986. if (i < 4)
  2987. mvpp2_write(priv, MVPP2_WIN_REMAP(i), 0);
  2988. }
  2989. win_enable = 0;
  2990. for (i = 0; i < dram->num_cs; i++) {
  2991. const struct mbus_dram_window *cs = dram->cs + i;
  2992. mvpp2_write(priv, MVPP2_WIN_BASE(i),
  2993. (cs->base & 0xffff0000) | (cs->mbus_attr << 8) |
  2994. dram->mbus_dram_target_id);
  2995. mvpp2_write(priv, MVPP2_WIN_SIZE(i),
  2996. (cs->size - 1) & 0xffff0000);
  2997. win_enable |= (1 << i);
  2998. }
  2999. mvpp2_write(priv, MVPP2_BASE_ADDR_ENABLE, win_enable);
  3000. }
  3001. /* Initialize Rx FIFO's */
  3002. static void mvpp2_rx_fifo_init(struct mvpp2 *priv)
  3003. {
  3004. int port;
  3005. for (port = 0; port < MVPP2_MAX_PORTS; port++) {
  3006. mvpp2_write(priv, MVPP2_RX_DATA_FIFO_SIZE_REG(port),
  3007. MVPP2_RX_FIFO_PORT_DATA_SIZE);
  3008. mvpp2_write(priv, MVPP2_RX_ATTR_FIFO_SIZE_REG(port),
  3009. MVPP2_RX_FIFO_PORT_ATTR_SIZE);
  3010. }
  3011. mvpp2_write(priv, MVPP2_RX_MIN_PKT_SIZE_REG,
  3012. MVPP2_RX_FIFO_PORT_MIN_PKT);
  3013. mvpp2_write(priv, MVPP2_RX_FIFO_INIT_REG, 0x1);
  3014. }
  3015. /* Initialize network controller common part HW */
  3016. static int mvpp2_init(struct udevice *dev, struct mvpp2 *priv)
  3017. {
  3018. const struct mbus_dram_target_info *dram_target_info;
  3019. int err, i;
  3020. u32 val;
  3021. /* Checks for hardware constraints (U-Boot uses only one rxq) */
  3022. if ((rxq_number > MVPP2_MAX_RXQ) || (txq_number > MVPP2_MAX_TXQ)) {
  3023. dev_err(&pdev->dev, "invalid queue size parameter\n");
  3024. return -EINVAL;
  3025. }
  3026. /* MBUS windows configuration */
  3027. dram_target_info = mvebu_mbus_dram_info();
  3028. if (dram_target_info)
  3029. mvpp2_conf_mbus_windows(dram_target_info, priv);
  3030. /* Disable HW PHY polling */
  3031. val = readl(priv->lms_base + MVPP2_PHY_AN_CFG0_REG);
  3032. val |= MVPP2_PHY_AN_STOP_SMI0_MASK;
  3033. writel(val, priv->lms_base + MVPP2_PHY_AN_CFG0_REG);
  3034. /* Allocate and initialize aggregated TXQs */
  3035. priv->aggr_txqs = devm_kcalloc(dev, num_present_cpus(),
  3036. sizeof(struct mvpp2_tx_queue),
  3037. GFP_KERNEL);
  3038. if (!priv->aggr_txqs)
  3039. return -ENOMEM;
  3040. for_each_present_cpu(i) {
  3041. priv->aggr_txqs[i].id = i;
  3042. priv->aggr_txqs[i].size = MVPP2_AGGR_TXQ_SIZE;
  3043. err = mvpp2_aggr_txq_init(dev, &priv->aggr_txqs[i],
  3044. MVPP2_AGGR_TXQ_SIZE, i, priv);
  3045. if (err < 0)
  3046. return err;
  3047. }
  3048. /* Rx Fifo Init */
  3049. mvpp2_rx_fifo_init(priv);
  3050. /* Reset Rx queue group interrupt configuration */
  3051. for (i = 0; i < MVPP2_MAX_PORTS; i++)
  3052. mvpp2_write(priv, MVPP2_ISR_RXQ_GROUP_REG(i),
  3053. CONFIG_MV_ETH_RXQ);
  3054. writel(MVPP2_EXT_GLOBAL_CTRL_DEFAULT,
  3055. priv->lms_base + MVPP2_MNG_EXTENDED_GLOBAL_CTRL_REG);
  3056. /* Allow cache snoop when transmiting packets */
  3057. mvpp2_write(priv, MVPP2_TX_SNOOP_REG, 0x1);
  3058. /* Buffer Manager initialization */
  3059. err = mvpp2_bm_init(dev, priv);
  3060. if (err < 0)
  3061. return err;
  3062. /* Parser default initialization */
  3063. err = mvpp2_prs_default_init(dev, priv);
  3064. if (err < 0)
  3065. return err;
  3066. /* Classifier default initialization */
  3067. mvpp2_cls_init(priv);
  3068. return 0;
  3069. }
  3070. /* SMI / MDIO functions */
  3071. static int smi_wait_ready(struct mvpp2 *priv)
  3072. {
  3073. u32 timeout = MVPP2_SMI_TIMEOUT;
  3074. u32 smi_reg;
  3075. /* wait till the SMI is not busy */
  3076. do {
  3077. /* read smi register */
  3078. smi_reg = readl(priv->lms_base + MVPP2_SMI);
  3079. if (timeout-- == 0) {
  3080. printf("Error: SMI busy timeout\n");
  3081. return -EFAULT;
  3082. }
  3083. } while (smi_reg & MVPP2_SMI_BUSY);
  3084. return 0;
  3085. }
  3086. /*
  3087. * mpp2_mdio_read - miiphy_read callback function.
  3088. *
  3089. * Returns 16bit phy register value, or 0xffff on error
  3090. */
  3091. static int mpp2_mdio_read(struct mii_dev *bus, int addr, int devad, int reg)
  3092. {
  3093. struct mvpp2 *priv = bus->priv;
  3094. u32 smi_reg;
  3095. u32 timeout;
  3096. /* check parameters */
  3097. if (addr > MVPP2_PHY_ADDR_MASK) {
  3098. printf("Error: Invalid PHY address %d\n", addr);
  3099. return -EFAULT;
  3100. }
  3101. if (reg > MVPP2_PHY_REG_MASK) {
  3102. printf("Err: Invalid register offset %d\n", reg);
  3103. return -EFAULT;
  3104. }
  3105. /* wait till the SMI is not busy */
  3106. if (smi_wait_ready(priv) < 0)
  3107. return -EFAULT;
  3108. /* fill the phy address and regiser offset and read opcode */
  3109. smi_reg = (addr << MVPP2_SMI_DEV_ADDR_OFFS)
  3110. | (reg << MVPP2_SMI_REG_ADDR_OFFS)
  3111. | MVPP2_SMI_OPCODE_READ;
  3112. /* write the smi register */
  3113. writel(smi_reg, priv->lms_base + MVPP2_SMI);
  3114. /* wait till read value is ready */
  3115. timeout = MVPP2_SMI_TIMEOUT;
  3116. do {
  3117. /* read smi register */
  3118. smi_reg = readl(priv->lms_base + MVPP2_SMI);
  3119. if (timeout-- == 0) {
  3120. printf("Err: SMI read ready timeout\n");
  3121. return -EFAULT;
  3122. }
  3123. } while (!(smi_reg & MVPP2_SMI_READ_VALID));
  3124. /* Wait for the data to update in the SMI register */
  3125. for (timeout = 0; timeout < MVPP2_SMI_TIMEOUT; timeout++)
  3126. ;
  3127. return readl(priv->lms_base + MVPP2_SMI) & MVPP2_SMI_DATA_MASK;
  3128. }
  3129. /*
  3130. * mpp2_mdio_write - miiphy_write callback function.
  3131. *
  3132. * Returns 0 if write succeed, -EINVAL on bad parameters
  3133. * -ETIME on timeout
  3134. */
  3135. static int mpp2_mdio_write(struct mii_dev *bus, int addr, int devad, int reg,
  3136. u16 value)
  3137. {
  3138. struct mvpp2 *priv = bus->priv;
  3139. u32 smi_reg;
  3140. /* check parameters */
  3141. if (addr > MVPP2_PHY_ADDR_MASK) {
  3142. printf("Error: Invalid PHY address %d\n", addr);
  3143. return -EFAULT;
  3144. }
  3145. if (reg > MVPP2_PHY_REG_MASK) {
  3146. printf("Err: Invalid register offset %d\n", reg);
  3147. return -EFAULT;
  3148. }
  3149. /* wait till the SMI is not busy */
  3150. if (smi_wait_ready(priv) < 0)
  3151. return -EFAULT;
  3152. /* fill the phy addr and reg offset and write opcode and data */
  3153. smi_reg = value << MVPP2_SMI_DATA_OFFS;
  3154. smi_reg |= (addr << MVPP2_SMI_DEV_ADDR_OFFS)
  3155. | (reg << MVPP2_SMI_REG_ADDR_OFFS);
  3156. smi_reg &= ~MVPP2_SMI_OPCODE_READ;
  3157. /* write the smi register */
  3158. writel(smi_reg, priv->lms_base + MVPP2_SMI);
  3159. return 0;
  3160. }
  3161. static int mvpp2_recv(struct udevice *dev, int flags, uchar **packetp)
  3162. {
  3163. struct mvpp2_port *port = dev_get_priv(dev);
  3164. struct mvpp2_rx_desc *rx_desc;
  3165. struct mvpp2_bm_pool *bm_pool;
  3166. dma_addr_t phys_addr;
  3167. u32 bm, rx_status;
  3168. int pool, rx_bytes, err;
  3169. int rx_received;
  3170. struct mvpp2_rx_queue *rxq;
  3171. u32 cause_rx_tx, cause_rx, cause_misc;
  3172. u8 *data;
  3173. cause_rx_tx = mvpp2_read(port->priv,
  3174. MVPP2_ISR_RX_TX_CAUSE_REG(port->id));
  3175. cause_rx_tx &= ~MVPP2_CAUSE_TXQ_OCCUP_DESC_ALL_MASK;
  3176. cause_misc = cause_rx_tx & MVPP2_CAUSE_MISC_SUM_MASK;
  3177. if (!cause_rx_tx && !cause_misc)
  3178. return 0;
  3179. cause_rx = cause_rx_tx & MVPP2_CAUSE_RXQ_OCCUP_DESC_ALL_MASK;
  3180. /* Process RX packets */
  3181. cause_rx |= port->pending_cause_rx;
  3182. rxq = mvpp2_get_rx_queue(port, cause_rx);
  3183. /* Get number of received packets and clamp the to-do */
  3184. rx_received = mvpp2_rxq_received(port, rxq->id);
  3185. /* Return if no packets are received */
  3186. if (!rx_received)
  3187. return 0;
  3188. rx_desc = mvpp2_rxq_next_desc_get(rxq);
  3189. rx_status = rx_desc->status;
  3190. rx_bytes = rx_desc->data_size - MVPP2_MH_SIZE;
  3191. phys_addr = rx_desc->buf_phys_addr;
  3192. bm = mvpp2_bm_cookie_build(rx_desc);
  3193. pool = mvpp2_bm_cookie_pool_get(bm);
  3194. bm_pool = &port->priv->bm_pools[pool];
  3195. /* Check if buffer header is used */
  3196. if (rx_status & MVPP2_RXD_BUF_HDR)
  3197. return 0;
  3198. /* In case of an error, release the requested buffer pointer
  3199. * to the Buffer Manager. This request process is controlled
  3200. * by the hardware, and the information about the buffer is
  3201. * comprised by the RX descriptor.
  3202. */
  3203. if (rx_status & MVPP2_RXD_ERR_SUMMARY) {
  3204. mvpp2_rx_error(port, rx_desc);
  3205. /* Return the buffer to the pool */
  3206. mvpp2_pool_refill(port, bm, rx_desc->buf_phys_addr,
  3207. rx_desc->buf_cookie);
  3208. return 0;
  3209. }
  3210. err = mvpp2_rx_refill(port, bm_pool, bm, phys_addr);
  3211. if (err) {
  3212. netdev_err(port->dev, "failed to refill BM pools\n");
  3213. return 0;
  3214. }
  3215. /* Update Rx queue management counters */
  3216. mb();
  3217. mvpp2_rxq_status_update(port, rxq->id, 1, 1);
  3218. /* give packet to stack - skip on first n bytes */
  3219. data = (u8 *)phys_addr + 2 + 32;
  3220. if (rx_bytes <= 0)
  3221. return 0;
  3222. /*
  3223. * No cache invalidation needed here, since the rx_buffer's are
  3224. * located in a uncached memory region
  3225. */
  3226. *packetp = data;
  3227. return rx_bytes;
  3228. }
  3229. /* Drain Txq */
  3230. static void mvpp2_txq_drain(struct mvpp2_port *port, struct mvpp2_tx_queue *txq,
  3231. int enable)
  3232. {
  3233. u32 val;
  3234. mvpp2_write(port->priv, MVPP2_TXQ_NUM_REG, txq->id);
  3235. val = mvpp2_read(port->priv, MVPP2_TXQ_PREF_BUF_REG);
  3236. if (enable)
  3237. val |= MVPP2_TXQ_DRAIN_EN_MASK;
  3238. else
  3239. val &= ~MVPP2_TXQ_DRAIN_EN_MASK;
  3240. mvpp2_write(port->priv, MVPP2_TXQ_PREF_BUF_REG, val);
  3241. }
  3242. static int mvpp2_send(struct udevice *dev, void *packet, int length)
  3243. {
  3244. struct mvpp2_port *port = dev_get_priv(dev);
  3245. struct mvpp2_tx_queue *txq, *aggr_txq;
  3246. struct mvpp2_tx_desc *tx_desc;
  3247. int tx_done;
  3248. int timeout;
  3249. txq = port->txqs[0];
  3250. aggr_txq = &port->priv->aggr_txqs[smp_processor_id()];
  3251. /* Get a descriptor for the first part of the packet */
  3252. tx_desc = mvpp2_txq_next_desc_get(aggr_txq);
  3253. tx_desc->phys_txq = txq->id;
  3254. tx_desc->data_size = length;
  3255. tx_desc->packet_offset = (u32)packet & MVPP2_TX_DESC_ALIGN;
  3256. tx_desc->buf_phys_addr = (u32)packet & ~MVPP2_TX_DESC_ALIGN;
  3257. /* First and Last descriptor */
  3258. tx_desc->command = MVPP2_TXD_L4_CSUM_NOT | MVPP2_TXD_IP_CSUM_DISABLE
  3259. | MVPP2_TXD_F_DESC | MVPP2_TXD_L_DESC;
  3260. /* Flush tx data */
  3261. flush_dcache_range((u32)packet, (u32)packet + length);
  3262. /* Enable transmit */
  3263. mb();
  3264. mvpp2_aggr_txq_pend_desc_add(port, 1);
  3265. mvpp2_write(port->priv, MVPP2_TXQ_NUM_REG, txq->id);
  3266. timeout = 0;
  3267. do {
  3268. if (timeout++ > 10000) {
  3269. printf("timeout: packet not sent from aggregated to phys TXQ\n");
  3270. return 0;
  3271. }
  3272. tx_done = mvpp2_txq_pend_desc_num_get(port, txq);
  3273. } while (tx_done);
  3274. /* Enable TXQ drain */
  3275. mvpp2_txq_drain(port, txq, 1);
  3276. timeout = 0;
  3277. do {
  3278. if (timeout++ > 10000) {
  3279. printf("timeout: packet not sent\n");
  3280. return 0;
  3281. }
  3282. tx_done = mvpp2_txq_sent_desc_proc(port, txq);
  3283. } while (!tx_done);
  3284. /* Disable TXQ drain */
  3285. mvpp2_txq_drain(port, txq, 0);
  3286. return 0;
  3287. }
  3288. static int mvpp2_start(struct udevice *dev)
  3289. {
  3290. struct eth_pdata *pdata = dev_get_platdata(dev);
  3291. struct mvpp2_port *port = dev_get_priv(dev);
  3292. /* Load current MAC address */
  3293. memcpy(port->dev_addr, pdata->enetaddr, ETH_ALEN);
  3294. /* Reconfigure parser accept the original MAC address */
  3295. mvpp2_prs_update_mac_da(port, port->dev_addr);
  3296. mvpp2_port_power_up(port);
  3297. mvpp2_open(dev, port);
  3298. return 0;
  3299. }
  3300. static void mvpp2_stop(struct udevice *dev)
  3301. {
  3302. struct mvpp2_port *port = dev_get_priv(dev);
  3303. mvpp2_stop_dev(port);
  3304. mvpp2_cleanup_rxqs(port);
  3305. mvpp2_cleanup_txqs(port);
  3306. }
  3307. static int mvpp2_probe(struct udevice *dev)
  3308. {
  3309. struct mvpp2_port *port = dev_get_priv(dev);
  3310. struct mvpp2 *priv = dev_get_priv(dev->parent);
  3311. int err;
  3312. /* Initialize network controller */
  3313. err = mvpp2_init(dev, priv);
  3314. if (err < 0) {
  3315. dev_err(&pdev->dev, "failed to initialize controller\n");
  3316. return err;
  3317. }
  3318. return mvpp2_port_probe(dev, port, dev->of_offset, priv,
  3319. &buffer_loc.first_rxq);
  3320. }
  3321. static const struct eth_ops mvpp2_ops = {
  3322. .start = mvpp2_start,
  3323. .send = mvpp2_send,
  3324. .recv = mvpp2_recv,
  3325. .stop = mvpp2_stop,
  3326. };
  3327. static struct driver mvpp2_driver = {
  3328. .name = "mvpp2",
  3329. .id = UCLASS_ETH,
  3330. .probe = mvpp2_probe,
  3331. .ops = &mvpp2_ops,
  3332. .priv_auto_alloc_size = sizeof(struct mvpp2_port),
  3333. .platdata_auto_alloc_size = sizeof(struct eth_pdata),
  3334. };
  3335. /*
  3336. * Use a MISC device to bind the n instances (child nodes) of the
  3337. * network base controller in UCLASS_ETH.
  3338. */
  3339. static int mvpp2_base_probe(struct udevice *dev)
  3340. {
  3341. struct mvpp2 *priv = dev_get_priv(dev);
  3342. struct mii_dev *bus;
  3343. void *bd_space;
  3344. u32 size = 0;
  3345. int i;
  3346. /*
  3347. * U-Boot special buffer handling:
  3348. *
  3349. * Allocate buffer area for descs and rx_buffers. This is only
  3350. * done once for all interfaces. As only one interface can
  3351. * be active. Make this area DMA-safe by disabling the D-cache
  3352. */
  3353. /* Align buffer area for descs and rx_buffers to 1MiB */
  3354. bd_space = memalign(1 << MMU_SECTION_SHIFT, BD_SPACE);
  3355. mmu_set_region_dcache_behaviour((u32)bd_space, BD_SPACE, DCACHE_OFF);
  3356. buffer_loc.aggr_tx_descs = (struct mvpp2_tx_desc *)bd_space;
  3357. size += MVPP2_AGGR_TXQ_SIZE * MVPP2_DESC_ALIGNED_SIZE;
  3358. buffer_loc.tx_descs = (struct mvpp2_tx_desc *)((u32)bd_space + size);
  3359. size += MVPP2_MAX_TXD * MVPP2_DESC_ALIGNED_SIZE;
  3360. buffer_loc.rx_descs = (struct mvpp2_rx_desc *)((u32)bd_space + size);
  3361. size += MVPP2_MAX_RXD * MVPP2_DESC_ALIGNED_SIZE;
  3362. for (i = 0; i < MVPP2_BM_POOLS_NUM; i++) {
  3363. buffer_loc.bm_pool[i] = (u32 *)((u32)bd_space + size);
  3364. size += MVPP2_BM_POOL_SIZE_MAX * sizeof(u32);
  3365. }
  3366. for (i = 0; i < MVPP2_BM_LONG_BUF_NUM; i++) {
  3367. buffer_loc.rx_buffer[i] = (u32 *)((u32)bd_space + size);
  3368. size += RX_BUFFER_SIZE;
  3369. }
  3370. /* Save base addresses for later use */
  3371. priv->base = (void *)dev_get_addr_index(dev, 0);
  3372. if (IS_ERR(priv->base))
  3373. return PTR_ERR(priv->base);
  3374. priv->lms_base = (void *)dev_get_addr_index(dev, 1);
  3375. if (IS_ERR(priv->lms_base))
  3376. return PTR_ERR(priv->lms_base);
  3377. /* Finally create and register the MDIO bus driver */
  3378. bus = mdio_alloc();
  3379. if (!bus) {
  3380. printf("Failed to allocate MDIO bus\n");
  3381. return -ENOMEM;
  3382. }
  3383. bus->read = mpp2_mdio_read;
  3384. bus->write = mpp2_mdio_write;
  3385. snprintf(bus->name, sizeof(bus->name), dev->name);
  3386. bus->priv = (void *)priv;
  3387. priv->bus = bus;
  3388. return mdio_register(bus);
  3389. }
  3390. static int mvpp2_base_bind(struct udevice *parent)
  3391. {
  3392. const void *blob = gd->fdt_blob;
  3393. int node = parent->of_offset;
  3394. struct uclass_driver *drv;
  3395. struct udevice *dev;
  3396. struct eth_pdata *plat;
  3397. char *name;
  3398. int subnode;
  3399. u32 id;
  3400. /* Lookup eth driver */
  3401. drv = lists_uclass_lookup(UCLASS_ETH);
  3402. if (!drv) {
  3403. puts("Cannot find eth driver\n");
  3404. return -ENOENT;
  3405. }
  3406. fdt_for_each_subnode(subnode, blob, node) {
  3407. /* Skip disabled ports */
  3408. if (!fdtdec_get_is_enabled(blob, subnode))
  3409. continue;
  3410. plat = calloc(1, sizeof(*plat));
  3411. if (!plat)
  3412. return -ENOMEM;
  3413. id = fdtdec_get_int(blob, subnode, "port-id", -1);
  3414. name = calloc(1, 16);
  3415. sprintf(name, "mvpp2-%d", id);
  3416. /* Create child device UCLASS_ETH and bind it */
  3417. device_bind(parent, &mvpp2_driver, name, plat, subnode, &dev);
  3418. dev->of_offset = subnode;
  3419. }
  3420. return 0;
  3421. }
  3422. static const struct udevice_id mvpp2_ids[] = {
  3423. { .compatible = "marvell,armada-375-pp2" },
  3424. { }
  3425. };
  3426. U_BOOT_DRIVER(mvpp2_base) = {
  3427. .name = "mvpp2_base",
  3428. .id = UCLASS_MISC,
  3429. .of_match = mvpp2_ids,
  3430. .bind = mvpp2_base_bind,
  3431. .probe = mvpp2_base_probe,
  3432. .priv_auto_alloc_size = sizeof(struct mvpp2),
  3433. };