mvpp2.c 121 KB

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