mvpp2.c 121 KB

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