mvpp2.c 153 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589
  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-2017 Stefan Roese <sr@denx.de>
  10. *
  11. * This file is licensed under the terms of the GNU General Public
  12. * License version 2. This program is licensed "as is" without any
  13. * warranty of any kind, whether express or implied.
  14. */
  15. #include <common.h>
  16. #include <dm.h>
  17. #include <dm/device-internal.h>
  18. #include <dm/lists.h>
  19. #include <net.h>
  20. #include <netdev.h>
  21. #include <config.h>
  22. #include <malloc.h>
  23. #include <asm/io.h>
  24. #include <linux/errno.h>
  25. #include <phy.h>
  26. #include <miiphy.h>
  27. #include <watchdog.h>
  28. #include <asm/arch/cpu.h>
  29. #include <asm/arch/soc.h>
  30. #include <linux/compat.h>
  31. #include <linux/mbus.h>
  32. DECLARE_GLOBAL_DATA_PTR;
  33. /* Some linux -> U-Boot compatibility stuff */
  34. #define netdev_err(dev, fmt, args...) \
  35. printf(fmt, ##args)
  36. #define netdev_warn(dev, fmt, args...) \
  37. printf(fmt, ##args)
  38. #define netdev_info(dev, fmt, args...) \
  39. printf(fmt, ##args)
  40. #define netdev_dbg(dev, fmt, args...) \
  41. printf(fmt, ##args)
  42. #define ETH_ALEN 6 /* Octets in one ethernet addr */
  43. #define __verify_pcpu_ptr(ptr) \
  44. do { \
  45. const void __percpu *__vpp_verify = (typeof((ptr) + 0))NULL; \
  46. (void)__vpp_verify; \
  47. } while (0)
  48. #define VERIFY_PERCPU_PTR(__p) \
  49. ({ \
  50. __verify_pcpu_ptr(__p); \
  51. (typeof(*(__p)) __kernel __force *)(__p); \
  52. })
  53. #define per_cpu_ptr(ptr, cpu) ({ (void)(cpu); VERIFY_PERCPU_PTR(ptr); })
  54. #define smp_processor_id() 0
  55. #define num_present_cpus() 1
  56. #define for_each_present_cpu(cpu) \
  57. for ((cpu) = 0; (cpu) < 1; (cpu)++)
  58. #define NET_SKB_PAD max(32, MVPP2_CPU_D_CACHE_LINE_SIZE)
  59. #define CONFIG_NR_CPUS 1
  60. #define ETH_HLEN ETHER_HDR_SIZE /* Total octets in header */
  61. /* 2(HW hdr) 14(MAC hdr) 4(CRC) 32(extra for cache prefetch) */
  62. #define WRAP (2 + ETH_HLEN + 4 + 32)
  63. #define MTU 1500
  64. #define RX_BUFFER_SIZE (ALIGN(MTU + WRAP, ARCH_DMA_MINALIGN))
  65. #define MVPP2_SMI_TIMEOUT 10000
  66. /* RX Fifo Registers */
  67. #define MVPP2_RX_DATA_FIFO_SIZE_REG(port) (0x00 + 4 * (port))
  68. #define MVPP2_RX_ATTR_FIFO_SIZE_REG(port) (0x20 + 4 * (port))
  69. #define MVPP2_RX_MIN_PKT_SIZE_REG 0x60
  70. #define MVPP2_RX_FIFO_INIT_REG 0x64
  71. /* RX DMA Top Registers */
  72. #define MVPP2_RX_CTRL_REG(port) (0x140 + 4 * (port))
  73. #define MVPP2_RX_LOW_LATENCY_PKT_SIZE(s) (((s) & 0xfff) << 16)
  74. #define MVPP2_RX_USE_PSEUDO_FOR_CSUM_MASK BIT(31)
  75. #define MVPP2_POOL_BUF_SIZE_REG(pool) (0x180 + 4 * (pool))
  76. #define MVPP2_POOL_BUF_SIZE_OFFSET 5
  77. #define MVPP2_RXQ_CONFIG_REG(rxq) (0x800 + 4 * (rxq))
  78. #define MVPP2_SNOOP_PKT_SIZE_MASK 0x1ff
  79. #define MVPP2_SNOOP_BUF_HDR_MASK BIT(9)
  80. #define MVPP2_RXQ_POOL_SHORT_OFFS 20
  81. #define MVPP21_RXQ_POOL_SHORT_MASK 0x700000
  82. #define MVPP22_RXQ_POOL_SHORT_MASK 0xf00000
  83. #define MVPP2_RXQ_POOL_LONG_OFFS 24
  84. #define MVPP21_RXQ_POOL_LONG_MASK 0x7000000
  85. #define MVPP22_RXQ_POOL_LONG_MASK 0xf000000
  86. #define MVPP2_RXQ_PACKET_OFFSET_OFFS 28
  87. #define MVPP2_RXQ_PACKET_OFFSET_MASK 0x70000000
  88. #define MVPP2_RXQ_DISABLE_MASK BIT(31)
  89. /* Parser Registers */
  90. #define MVPP2_PRS_INIT_LOOKUP_REG 0x1000
  91. #define MVPP2_PRS_PORT_LU_MAX 0xf
  92. #define MVPP2_PRS_PORT_LU_MASK(port) (0xff << ((port) * 4))
  93. #define MVPP2_PRS_PORT_LU_VAL(port, val) ((val) << ((port) * 4))
  94. #define MVPP2_PRS_INIT_OFFS_REG(port) (0x1004 + ((port) & 4))
  95. #define MVPP2_PRS_INIT_OFF_MASK(port) (0x3f << (((port) % 4) * 8))
  96. #define MVPP2_PRS_INIT_OFF_VAL(port, val) ((val) << (((port) % 4) * 8))
  97. #define MVPP2_PRS_MAX_LOOP_REG(port) (0x100c + ((port) & 4))
  98. #define MVPP2_PRS_MAX_LOOP_MASK(port) (0xff << (((port) % 4) * 8))
  99. #define MVPP2_PRS_MAX_LOOP_VAL(port, val) ((val) << (((port) % 4) * 8))
  100. #define MVPP2_PRS_TCAM_IDX_REG 0x1100
  101. #define MVPP2_PRS_TCAM_DATA_REG(idx) (0x1104 + (idx) * 4)
  102. #define MVPP2_PRS_TCAM_INV_MASK BIT(31)
  103. #define MVPP2_PRS_SRAM_IDX_REG 0x1200
  104. #define MVPP2_PRS_SRAM_DATA_REG(idx) (0x1204 + (idx) * 4)
  105. #define MVPP2_PRS_TCAM_CTRL_REG 0x1230
  106. #define MVPP2_PRS_TCAM_EN_MASK BIT(0)
  107. /* Classifier Registers */
  108. #define MVPP2_CLS_MODE_REG 0x1800
  109. #define MVPP2_CLS_MODE_ACTIVE_MASK BIT(0)
  110. #define MVPP2_CLS_PORT_WAY_REG 0x1810
  111. #define MVPP2_CLS_PORT_WAY_MASK(port) (1 << (port))
  112. #define MVPP2_CLS_LKP_INDEX_REG 0x1814
  113. #define MVPP2_CLS_LKP_INDEX_WAY_OFFS 6
  114. #define MVPP2_CLS_LKP_TBL_REG 0x1818
  115. #define MVPP2_CLS_LKP_TBL_RXQ_MASK 0xff
  116. #define MVPP2_CLS_LKP_TBL_LOOKUP_EN_MASK BIT(25)
  117. #define MVPP2_CLS_FLOW_INDEX_REG 0x1820
  118. #define MVPP2_CLS_FLOW_TBL0_REG 0x1824
  119. #define MVPP2_CLS_FLOW_TBL1_REG 0x1828
  120. #define MVPP2_CLS_FLOW_TBL2_REG 0x182c
  121. #define MVPP2_CLS_OVERSIZE_RXQ_LOW_REG(port) (0x1980 + ((port) * 4))
  122. #define MVPP2_CLS_OVERSIZE_RXQ_LOW_BITS 3
  123. #define MVPP2_CLS_OVERSIZE_RXQ_LOW_MASK 0x7
  124. #define MVPP2_CLS_SWFWD_P2HQ_REG(port) (0x19b0 + ((port) * 4))
  125. #define MVPP2_CLS_SWFWD_PCTRL_REG 0x19d0
  126. #define MVPP2_CLS_SWFWD_PCTRL_MASK(port) (1 << (port))
  127. /* Descriptor Manager Top Registers */
  128. #define MVPP2_RXQ_NUM_REG 0x2040
  129. #define MVPP2_RXQ_DESC_ADDR_REG 0x2044
  130. #define MVPP22_DESC_ADDR_OFFS 8
  131. #define MVPP2_RXQ_DESC_SIZE_REG 0x2048
  132. #define MVPP2_RXQ_DESC_SIZE_MASK 0x3ff0
  133. #define MVPP2_RXQ_STATUS_UPDATE_REG(rxq) (0x3000 + 4 * (rxq))
  134. #define MVPP2_RXQ_NUM_PROCESSED_OFFSET 0
  135. #define MVPP2_RXQ_NUM_NEW_OFFSET 16
  136. #define MVPP2_RXQ_STATUS_REG(rxq) (0x3400 + 4 * (rxq))
  137. #define MVPP2_RXQ_OCCUPIED_MASK 0x3fff
  138. #define MVPP2_RXQ_NON_OCCUPIED_OFFSET 16
  139. #define MVPP2_RXQ_NON_OCCUPIED_MASK 0x3fff0000
  140. #define MVPP2_RXQ_THRESH_REG 0x204c
  141. #define MVPP2_OCCUPIED_THRESH_OFFSET 0
  142. #define MVPP2_OCCUPIED_THRESH_MASK 0x3fff
  143. #define MVPP2_RXQ_INDEX_REG 0x2050
  144. #define MVPP2_TXQ_NUM_REG 0x2080
  145. #define MVPP2_TXQ_DESC_ADDR_REG 0x2084
  146. #define MVPP2_TXQ_DESC_SIZE_REG 0x2088
  147. #define MVPP2_TXQ_DESC_SIZE_MASK 0x3ff0
  148. #define MVPP2_AGGR_TXQ_UPDATE_REG 0x2090
  149. #define MVPP2_TXQ_THRESH_REG 0x2094
  150. #define MVPP2_TRANSMITTED_THRESH_OFFSET 16
  151. #define MVPP2_TRANSMITTED_THRESH_MASK 0x3fff0000
  152. #define MVPP2_TXQ_INDEX_REG 0x2098
  153. #define MVPP2_TXQ_PREF_BUF_REG 0x209c
  154. #define MVPP2_PREF_BUF_PTR(desc) ((desc) & 0xfff)
  155. #define MVPP2_PREF_BUF_SIZE_4 (BIT(12) | BIT(13))
  156. #define MVPP2_PREF_BUF_SIZE_16 (BIT(12) | BIT(14))
  157. #define MVPP2_PREF_BUF_THRESH(val) ((val) << 17)
  158. #define MVPP2_TXQ_DRAIN_EN_MASK BIT(31)
  159. #define MVPP2_TXQ_PENDING_REG 0x20a0
  160. #define MVPP2_TXQ_PENDING_MASK 0x3fff
  161. #define MVPP2_TXQ_INT_STATUS_REG 0x20a4
  162. #define MVPP2_TXQ_SENT_REG(txq) (0x3c00 + 4 * (txq))
  163. #define MVPP2_TRANSMITTED_COUNT_OFFSET 16
  164. #define MVPP2_TRANSMITTED_COUNT_MASK 0x3fff0000
  165. #define MVPP2_TXQ_RSVD_REQ_REG 0x20b0
  166. #define MVPP2_TXQ_RSVD_REQ_Q_OFFSET 16
  167. #define MVPP2_TXQ_RSVD_RSLT_REG 0x20b4
  168. #define MVPP2_TXQ_RSVD_RSLT_MASK 0x3fff
  169. #define MVPP2_TXQ_RSVD_CLR_REG 0x20b8
  170. #define MVPP2_TXQ_RSVD_CLR_OFFSET 16
  171. #define MVPP2_AGGR_TXQ_DESC_ADDR_REG(cpu) (0x2100 + 4 * (cpu))
  172. #define MVPP22_AGGR_TXQ_DESC_ADDR_OFFS 8
  173. #define MVPP2_AGGR_TXQ_DESC_SIZE_REG(cpu) (0x2140 + 4 * (cpu))
  174. #define MVPP2_AGGR_TXQ_DESC_SIZE_MASK 0x3ff0
  175. #define MVPP2_AGGR_TXQ_STATUS_REG(cpu) (0x2180 + 4 * (cpu))
  176. #define MVPP2_AGGR_TXQ_PENDING_MASK 0x3fff
  177. #define MVPP2_AGGR_TXQ_INDEX_REG(cpu) (0x21c0 + 4 * (cpu))
  178. /* MBUS bridge registers */
  179. #define MVPP2_WIN_BASE(w) (0x4000 + ((w) << 2))
  180. #define MVPP2_WIN_SIZE(w) (0x4020 + ((w) << 2))
  181. #define MVPP2_WIN_REMAP(w) (0x4040 + ((w) << 2))
  182. #define MVPP2_BASE_ADDR_ENABLE 0x4060
  183. /* AXI Bridge Registers */
  184. #define MVPP22_AXI_BM_WR_ATTR_REG 0x4100
  185. #define MVPP22_AXI_BM_RD_ATTR_REG 0x4104
  186. #define MVPP22_AXI_AGGRQ_DESCR_RD_ATTR_REG 0x4110
  187. #define MVPP22_AXI_TXQ_DESCR_WR_ATTR_REG 0x4114
  188. #define MVPP22_AXI_TXQ_DESCR_RD_ATTR_REG 0x4118
  189. #define MVPP22_AXI_RXQ_DESCR_WR_ATTR_REG 0x411c
  190. #define MVPP22_AXI_RX_DATA_WR_ATTR_REG 0x4120
  191. #define MVPP22_AXI_TX_DATA_RD_ATTR_REG 0x4130
  192. #define MVPP22_AXI_RD_NORMAL_CODE_REG 0x4150
  193. #define MVPP22_AXI_RD_SNOOP_CODE_REG 0x4154
  194. #define MVPP22_AXI_WR_NORMAL_CODE_REG 0x4160
  195. #define MVPP22_AXI_WR_SNOOP_CODE_REG 0x4164
  196. /* Values for AXI Bridge registers */
  197. #define MVPP22_AXI_ATTR_CACHE_OFFS 0
  198. #define MVPP22_AXI_ATTR_DOMAIN_OFFS 12
  199. #define MVPP22_AXI_CODE_CACHE_OFFS 0
  200. #define MVPP22_AXI_CODE_DOMAIN_OFFS 4
  201. #define MVPP22_AXI_CODE_CACHE_NON_CACHE 0x3
  202. #define MVPP22_AXI_CODE_CACHE_WR_CACHE 0x7
  203. #define MVPP22_AXI_CODE_CACHE_RD_CACHE 0xb
  204. #define MVPP22_AXI_CODE_DOMAIN_OUTER_DOM 2
  205. #define MVPP22_AXI_CODE_DOMAIN_SYSTEM 3
  206. /* Interrupt Cause and Mask registers */
  207. #define MVPP2_ISR_RX_THRESHOLD_REG(rxq) (0x5200 + 4 * (rxq))
  208. #define MVPP21_ISR_RXQ_GROUP_REG(rxq) (0x5400 + 4 * (rxq))
  209. #define MVPP22_ISR_RXQ_GROUP_INDEX_REG 0x5400
  210. #define MVPP22_ISR_RXQ_GROUP_INDEX_SUBGROUP_MASK 0xf
  211. #define MVPP22_ISR_RXQ_GROUP_INDEX_GROUP_MASK 0x380
  212. #define MVPP22_ISR_RXQ_GROUP_INDEX_GROUP_OFFSET 7
  213. #define MVPP22_ISR_RXQ_GROUP_INDEX_SUBGROUP_MASK 0xf
  214. #define MVPP22_ISR_RXQ_GROUP_INDEX_GROUP_MASK 0x380
  215. #define MVPP22_ISR_RXQ_SUB_GROUP_CONFIG_REG 0x5404
  216. #define MVPP22_ISR_RXQ_SUB_GROUP_STARTQ_MASK 0x1f
  217. #define MVPP22_ISR_RXQ_SUB_GROUP_SIZE_MASK 0xf00
  218. #define MVPP22_ISR_RXQ_SUB_GROUP_SIZE_OFFSET 8
  219. #define MVPP2_ISR_ENABLE_REG(port) (0x5420 + 4 * (port))
  220. #define MVPP2_ISR_ENABLE_INTERRUPT(mask) ((mask) & 0xffff)
  221. #define MVPP2_ISR_DISABLE_INTERRUPT(mask) (((mask) << 16) & 0xffff0000)
  222. #define MVPP2_ISR_RX_TX_CAUSE_REG(port) (0x5480 + 4 * (port))
  223. #define MVPP2_CAUSE_RXQ_OCCUP_DESC_ALL_MASK 0xffff
  224. #define MVPP2_CAUSE_TXQ_OCCUP_DESC_ALL_MASK 0xff0000
  225. #define MVPP2_CAUSE_RX_FIFO_OVERRUN_MASK BIT(24)
  226. #define MVPP2_CAUSE_FCS_ERR_MASK BIT(25)
  227. #define MVPP2_CAUSE_TX_FIFO_UNDERRUN_MASK BIT(26)
  228. #define MVPP2_CAUSE_TX_EXCEPTION_SUM_MASK BIT(29)
  229. #define MVPP2_CAUSE_RX_EXCEPTION_SUM_MASK BIT(30)
  230. #define MVPP2_CAUSE_MISC_SUM_MASK BIT(31)
  231. #define MVPP2_ISR_RX_TX_MASK_REG(port) (0x54a0 + 4 * (port))
  232. #define MVPP2_ISR_PON_RX_TX_MASK_REG 0x54bc
  233. #define MVPP2_PON_CAUSE_RXQ_OCCUP_DESC_ALL_MASK 0xffff
  234. #define MVPP2_PON_CAUSE_TXP_OCCUP_DESC_ALL_MASK 0x3fc00000
  235. #define MVPP2_PON_CAUSE_MISC_SUM_MASK BIT(31)
  236. #define MVPP2_ISR_MISC_CAUSE_REG 0x55b0
  237. /* Buffer Manager registers */
  238. #define MVPP2_BM_POOL_BASE_REG(pool) (0x6000 + ((pool) * 4))
  239. #define MVPP2_BM_POOL_BASE_ADDR_MASK 0xfffff80
  240. #define MVPP2_BM_POOL_SIZE_REG(pool) (0x6040 + ((pool) * 4))
  241. #define MVPP2_BM_POOL_SIZE_MASK 0xfff0
  242. #define MVPP2_BM_POOL_READ_PTR_REG(pool) (0x6080 + ((pool) * 4))
  243. #define MVPP2_BM_POOL_GET_READ_PTR_MASK 0xfff0
  244. #define MVPP2_BM_POOL_PTRS_NUM_REG(pool) (0x60c0 + ((pool) * 4))
  245. #define MVPP2_BM_POOL_PTRS_NUM_MASK 0xfff0
  246. #define MVPP2_BM_BPPI_READ_PTR_REG(pool) (0x6100 + ((pool) * 4))
  247. #define MVPP2_BM_BPPI_PTRS_NUM_REG(pool) (0x6140 + ((pool) * 4))
  248. #define MVPP2_BM_BPPI_PTR_NUM_MASK 0x7ff
  249. #define MVPP2_BM_BPPI_PREFETCH_FULL_MASK BIT(16)
  250. #define MVPP2_BM_POOL_CTRL_REG(pool) (0x6200 + ((pool) * 4))
  251. #define MVPP2_BM_START_MASK BIT(0)
  252. #define MVPP2_BM_STOP_MASK BIT(1)
  253. #define MVPP2_BM_STATE_MASK BIT(4)
  254. #define MVPP2_BM_LOW_THRESH_OFFS 8
  255. #define MVPP2_BM_LOW_THRESH_MASK 0x7f00
  256. #define MVPP2_BM_LOW_THRESH_VALUE(val) ((val) << \
  257. MVPP2_BM_LOW_THRESH_OFFS)
  258. #define MVPP2_BM_HIGH_THRESH_OFFS 16
  259. #define MVPP2_BM_HIGH_THRESH_MASK 0x7f0000
  260. #define MVPP2_BM_HIGH_THRESH_VALUE(val) ((val) << \
  261. MVPP2_BM_HIGH_THRESH_OFFS)
  262. #define MVPP2_BM_INTR_CAUSE_REG(pool) (0x6240 + ((pool) * 4))
  263. #define MVPP2_BM_RELEASED_DELAY_MASK BIT(0)
  264. #define MVPP2_BM_ALLOC_FAILED_MASK BIT(1)
  265. #define MVPP2_BM_BPPE_EMPTY_MASK BIT(2)
  266. #define MVPP2_BM_BPPE_FULL_MASK BIT(3)
  267. #define MVPP2_BM_AVAILABLE_BP_LOW_MASK BIT(4)
  268. #define MVPP2_BM_INTR_MASK_REG(pool) (0x6280 + ((pool) * 4))
  269. #define MVPP2_BM_PHY_ALLOC_REG(pool) (0x6400 + ((pool) * 4))
  270. #define MVPP2_BM_PHY_ALLOC_GRNTD_MASK BIT(0)
  271. #define MVPP2_BM_VIRT_ALLOC_REG 0x6440
  272. #define MVPP2_BM_ADDR_HIGH_ALLOC 0x6444
  273. #define MVPP2_BM_ADDR_HIGH_PHYS_MASK 0xff
  274. #define MVPP2_BM_ADDR_HIGH_VIRT_MASK 0xff00
  275. #define MVPP2_BM_ADDR_HIGH_VIRT_SHIFT 8
  276. #define MVPP2_BM_PHY_RLS_REG(pool) (0x6480 + ((pool) * 4))
  277. #define MVPP2_BM_PHY_RLS_MC_BUFF_MASK BIT(0)
  278. #define MVPP2_BM_PHY_RLS_PRIO_EN_MASK BIT(1)
  279. #define MVPP2_BM_PHY_RLS_GRNTD_MASK BIT(2)
  280. #define MVPP2_BM_VIRT_RLS_REG 0x64c0
  281. #define MVPP21_BM_MC_RLS_REG 0x64c4
  282. #define MVPP2_BM_MC_ID_MASK 0xfff
  283. #define MVPP2_BM_FORCE_RELEASE_MASK BIT(12)
  284. #define MVPP22_BM_ADDR_HIGH_RLS_REG 0x64c4
  285. #define MVPP22_BM_ADDR_HIGH_PHYS_RLS_MASK 0xff
  286. #define MVPP22_BM_ADDR_HIGH_VIRT_RLS_MASK 0xff00
  287. #define MVPP22_BM_ADDR_HIGH_VIRT_RLS_SHIFT 8
  288. #define MVPP22_BM_MC_RLS_REG 0x64d4
  289. /* TX Scheduler registers */
  290. #define MVPP2_TXP_SCHED_PORT_INDEX_REG 0x8000
  291. #define MVPP2_TXP_SCHED_Q_CMD_REG 0x8004
  292. #define MVPP2_TXP_SCHED_ENQ_MASK 0xff
  293. #define MVPP2_TXP_SCHED_DISQ_OFFSET 8
  294. #define MVPP2_TXP_SCHED_CMD_1_REG 0x8010
  295. #define MVPP2_TXP_SCHED_PERIOD_REG 0x8018
  296. #define MVPP2_TXP_SCHED_MTU_REG 0x801c
  297. #define MVPP2_TXP_MTU_MAX 0x7FFFF
  298. #define MVPP2_TXP_SCHED_REFILL_REG 0x8020
  299. #define MVPP2_TXP_REFILL_TOKENS_ALL_MASK 0x7ffff
  300. #define MVPP2_TXP_REFILL_PERIOD_ALL_MASK 0x3ff00000
  301. #define MVPP2_TXP_REFILL_PERIOD_MASK(v) ((v) << 20)
  302. #define MVPP2_TXP_SCHED_TOKEN_SIZE_REG 0x8024
  303. #define MVPP2_TXP_TOKEN_SIZE_MAX 0xffffffff
  304. #define MVPP2_TXQ_SCHED_REFILL_REG(q) (0x8040 + ((q) << 2))
  305. #define MVPP2_TXQ_REFILL_TOKENS_ALL_MASK 0x7ffff
  306. #define MVPP2_TXQ_REFILL_PERIOD_ALL_MASK 0x3ff00000
  307. #define MVPP2_TXQ_REFILL_PERIOD_MASK(v) ((v) << 20)
  308. #define MVPP2_TXQ_SCHED_TOKEN_SIZE_REG(q) (0x8060 + ((q) << 2))
  309. #define MVPP2_TXQ_TOKEN_SIZE_MAX 0x7fffffff
  310. #define MVPP2_TXQ_SCHED_TOKEN_CNTR_REG(q) (0x8080 + ((q) << 2))
  311. #define MVPP2_TXQ_TOKEN_CNTR_MAX 0xffffffff
  312. /* TX general registers */
  313. #define MVPP2_TX_SNOOP_REG 0x8800
  314. #define MVPP2_TX_PORT_FLUSH_REG 0x8810
  315. #define MVPP2_TX_PORT_FLUSH_MASK(port) (1 << (port))
  316. /* LMS registers */
  317. #define MVPP2_SRC_ADDR_MIDDLE 0x24
  318. #define MVPP2_SRC_ADDR_HIGH 0x28
  319. #define MVPP2_PHY_AN_CFG0_REG 0x34
  320. #define MVPP2_PHY_AN_STOP_SMI0_MASK BIT(7)
  321. #define MVPP2_MNG_EXTENDED_GLOBAL_CTRL_REG 0x305c
  322. #define MVPP2_EXT_GLOBAL_CTRL_DEFAULT 0x27
  323. /* Per-port registers */
  324. #define MVPP2_GMAC_CTRL_0_REG 0x0
  325. #define MVPP2_GMAC_PORT_EN_MASK BIT(0)
  326. #define MVPP2_GMAC_PORT_TYPE_MASK BIT(1)
  327. #define MVPP2_GMAC_MAX_RX_SIZE_OFFS 2
  328. #define MVPP2_GMAC_MAX_RX_SIZE_MASK 0x7ffc
  329. #define MVPP2_GMAC_MIB_CNTR_EN_MASK BIT(15)
  330. #define MVPP2_GMAC_CTRL_1_REG 0x4
  331. #define MVPP2_GMAC_PERIODIC_XON_EN_MASK BIT(1)
  332. #define MVPP2_GMAC_GMII_LB_EN_MASK BIT(5)
  333. #define MVPP2_GMAC_PCS_LB_EN_BIT 6
  334. #define MVPP2_GMAC_PCS_LB_EN_MASK BIT(6)
  335. #define MVPP2_GMAC_SA_LOW_OFFS 7
  336. #define MVPP2_GMAC_CTRL_2_REG 0x8
  337. #define MVPP2_GMAC_INBAND_AN_MASK BIT(0)
  338. #define MVPP2_GMAC_SGMII_MODE_MASK BIT(0)
  339. #define MVPP2_GMAC_PCS_ENABLE_MASK BIT(3)
  340. #define MVPP2_GMAC_PORT_RGMII_MASK BIT(4)
  341. #define MVPP2_GMAC_PORT_DIS_PADING_MASK BIT(5)
  342. #define MVPP2_GMAC_PORT_RESET_MASK BIT(6)
  343. #define MVPP2_GMAC_CLK_125_BYPS_EN_MASK BIT(9)
  344. #define MVPP2_GMAC_AUTONEG_CONFIG 0xc
  345. #define MVPP2_GMAC_FORCE_LINK_DOWN BIT(0)
  346. #define MVPP2_GMAC_FORCE_LINK_PASS BIT(1)
  347. #define MVPP2_GMAC_EN_PCS_AN BIT(2)
  348. #define MVPP2_GMAC_AN_BYPASS_EN BIT(3)
  349. #define MVPP2_GMAC_CONFIG_MII_SPEED BIT(5)
  350. #define MVPP2_GMAC_CONFIG_GMII_SPEED BIT(6)
  351. #define MVPP2_GMAC_AN_SPEED_EN BIT(7)
  352. #define MVPP2_GMAC_FC_ADV_EN BIT(9)
  353. #define MVPP2_GMAC_EN_FC_AN BIT(11)
  354. #define MVPP2_GMAC_CONFIG_FULL_DUPLEX BIT(12)
  355. #define MVPP2_GMAC_AN_DUPLEX_EN BIT(13)
  356. #define MVPP2_GMAC_CHOOSE_SAMPLE_TX_CONFIG BIT(15)
  357. #define MVPP2_GMAC_PORT_FIFO_CFG_1_REG 0x1c
  358. #define MVPP2_GMAC_TX_FIFO_MIN_TH_OFFS 6
  359. #define MVPP2_GMAC_TX_FIFO_MIN_TH_ALL_MASK 0x1fc0
  360. #define MVPP2_GMAC_TX_FIFO_MIN_TH_MASK(v) (((v) << 6) & \
  361. MVPP2_GMAC_TX_FIFO_MIN_TH_ALL_MASK)
  362. #define MVPP2_GMAC_CTRL_4_REG 0x90
  363. #define MVPP2_GMAC_CTRL4_EXT_PIN_GMII_SEL_MASK BIT(0)
  364. #define MVPP2_GMAC_CTRL4_DP_CLK_SEL_MASK BIT(5)
  365. #define MVPP2_GMAC_CTRL4_SYNC_BYPASS_MASK BIT(6)
  366. #define MVPP2_GMAC_CTRL4_QSGMII_BYPASS_ACTIVE_MASK BIT(7)
  367. /*
  368. * Per-port XGMAC registers. PPv2.2 only, only for GOP port 0,
  369. * relative to port->base.
  370. */
  371. /* Port Mac Control0 */
  372. #define MVPP22_XLG_CTRL0_REG 0x100
  373. #define MVPP22_XLG_PORT_EN BIT(0)
  374. #define MVPP22_XLG_MAC_RESETN BIT(1)
  375. #define MVPP22_XLG_RX_FC_EN BIT(7)
  376. #define MVPP22_XLG_MIBCNT_DIS BIT(13)
  377. /* Port Mac Control1 */
  378. #define MVPP22_XLG_CTRL1_REG 0x104
  379. #define MVPP22_XLG_MAX_RX_SIZE_OFFS 0
  380. #define MVPP22_XLG_MAX_RX_SIZE_MASK 0x1fff
  381. /* Port Interrupt Mask */
  382. #define MVPP22_XLG_INTERRUPT_MASK_REG 0x118
  383. #define MVPP22_XLG_INTERRUPT_LINK_CHANGE BIT(1)
  384. /* Port Mac Control3 */
  385. #define MVPP22_XLG_CTRL3_REG 0x11c
  386. #define MVPP22_XLG_CTRL3_MACMODESELECT_MASK (7 << 13)
  387. #define MVPP22_XLG_CTRL3_MACMODESELECT_GMAC (0 << 13)
  388. #define MVPP22_XLG_CTRL3_MACMODESELECT_10GMAC (1 << 13)
  389. /* Port Mac Control4 */
  390. #define MVPP22_XLG_CTRL4_REG 0x184
  391. #define MVPP22_XLG_FORWARD_802_3X_FC_EN BIT(5)
  392. #define MVPP22_XLG_FORWARD_PFC_EN BIT(6)
  393. #define MVPP22_XLG_MODE_DMA_1G BIT(12)
  394. #define MVPP22_XLG_EN_IDLE_CHECK_FOR_LINK BIT(14)
  395. /* XPCS registers */
  396. /* Global Configuration 0 */
  397. #define MVPP22_XPCS_GLOBAL_CFG_0_REG 0x0
  398. #define MVPP22_XPCS_PCSRESET BIT(0)
  399. #define MVPP22_XPCS_PCSMODE_OFFS 3
  400. #define MVPP22_XPCS_PCSMODE_MASK (0x3 << \
  401. MVPP22_XPCS_PCSMODE_OFFS)
  402. #define MVPP22_XPCS_LANEACTIVE_OFFS 5
  403. #define MVPP22_XPCS_LANEACTIVE_MASK (0x3 << \
  404. MVPP22_XPCS_LANEACTIVE_OFFS)
  405. /* MPCS registers */
  406. #define PCS40G_COMMON_CONTROL 0x14
  407. #define FORWARD_ERROR_CORRECTION_MASK BIT(1)
  408. #define PCS_CLOCK_RESET 0x14c
  409. #define TX_SD_CLK_RESET_MASK BIT(0)
  410. #define RX_SD_CLK_RESET_MASK BIT(1)
  411. #define MAC_CLK_RESET_MASK BIT(2)
  412. #define CLK_DIVISION_RATIO_OFFS 4
  413. #define CLK_DIVISION_RATIO_MASK (0x7 << CLK_DIVISION_RATIO_OFFS)
  414. #define CLK_DIV_PHASE_SET_MASK BIT(11)
  415. /* System Soft Reset 1 */
  416. #define GOP_SOFT_RESET_1_REG 0x108
  417. #define NETC_GOP_SOFT_RESET_OFFS 6
  418. #define NETC_GOP_SOFT_RESET_MASK (0x1 << \
  419. NETC_GOP_SOFT_RESET_OFFS)
  420. /* Ports Control 0 */
  421. #define NETCOMP_PORTS_CONTROL_0_REG 0x110
  422. #define NETC_BUS_WIDTH_SELECT_OFFS 1
  423. #define NETC_BUS_WIDTH_SELECT_MASK (0x1 << \
  424. NETC_BUS_WIDTH_SELECT_OFFS)
  425. #define NETC_GIG_RX_DATA_SAMPLE_OFFS 29
  426. #define NETC_GIG_RX_DATA_SAMPLE_MASK (0x1 << \
  427. NETC_GIG_RX_DATA_SAMPLE_OFFS)
  428. #define NETC_CLK_DIV_PHASE_OFFS 31
  429. #define NETC_CLK_DIV_PHASE_MASK (0x1 << NETC_CLK_DIV_PHASE_OFFS)
  430. /* Ports Control 1 */
  431. #define NETCOMP_PORTS_CONTROL_1_REG 0x114
  432. #define NETC_PORTS_ACTIVE_OFFSET(p) (0 + p)
  433. #define NETC_PORTS_ACTIVE_MASK(p) (0x1 << \
  434. NETC_PORTS_ACTIVE_OFFSET(p))
  435. #define NETC_PORT_GIG_RF_RESET_OFFS(p) (28 + p)
  436. #define NETC_PORT_GIG_RF_RESET_MASK(p) (0x1 << \
  437. NETC_PORT_GIG_RF_RESET_OFFS(p))
  438. #define NETCOMP_CONTROL_0_REG 0x120
  439. #define NETC_GBE_PORT0_SGMII_MODE_OFFS 0
  440. #define NETC_GBE_PORT0_SGMII_MODE_MASK (0x1 << \
  441. NETC_GBE_PORT0_SGMII_MODE_OFFS)
  442. #define NETC_GBE_PORT1_SGMII_MODE_OFFS 1
  443. #define NETC_GBE_PORT1_SGMII_MODE_MASK (0x1 << \
  444. NETC_GBE_PORT1_SGMII_MODE_OFFS)
  445. #define NETC_GBE_PORT1_MII_MODE_OFFS 2
  446. #define NETC_GBE_PORT1_MII_MODE_MASK (0x1 << \
  447. NETC_GBE_PORT1_MII_MODE_OFFS)
  448. #define MVPP22_SMI_MISC_CFG_REG (MVPP22_SMI + 0x04)
  449. #define MVPP22_SMI_POLLING_EN BIT(10)
  450. #define MVPP22_SMI_PHY_ADDR_REG(port) (MVPP22_SMI + 0x04 + \
  451. (0x4 * (port)))
  452. #define MVPP2_CAUSE_TXQ_SENT_DESC_ALL_MASK 0xff
  453. /* Descriptor ring Macros */
  454. #define MVPP2_QUEUE_NEXT_DESC(q, index) \
  455. (((index) < (q)->last_desc) ? ((index) + 1) : 0)
  456. /* SMI: 0xc0054 -> offset 0x54 to lms_base */
  457. #define MVPP21_SMI 0x0054
  458. /* PP2.2: SMI: 0x12a200 -> offset 0x1200 to iface_base */
  459. #define MVPP22_SMI 0x1200
  460. #define MVPP2_PHY_REG_MASK 0x1f
  461. /* SMI register fields */
  462. #define MVPP2_SMI_DATA_OFFS 0 /* Data */
  463. #define MVPP2_SMI_DATA_MASK (0xffff << MVPP2_SMI_DATA_OFFS)
  464. #define MVPP2_SMI_DEV_ADDR_OFFS 16 /* PHY device address */
  465. #define MVPP2_SMI_REG_ADDR_OFFS 21 /* PHY device reg addr*/
  466. #define MVPP2_SMI_OPCODE_OFFS 26 /* Write/Read opcode */
  467. #define MVPP2_SMI_OPCODE_READ (1 << MVPP2_SMI_OPCODE_OFFS)
  468. #define MVPP2_SMI_READ_VALID (1 << 27) /* Read Valid */
  469. #define MVPP2_SMI_BUSY (1 << 28) /* Busy */
  470. #define MVPP2_PHY_ADDR_MASK 0x1f
  471. #define MVPP2_PHY_REG_MASK 0x1f
  472. /* Additional PPv2.2 offsets */
  473. #define MVPP22_MPCS 0x007000
  474. #define MVPP22_XPCS 0x007400
  475. #define MVPP22_PORT_BASE 0x007e00
  476. #define MVPP22_PORT_OFFSET 0x001000
  477. #define MVPP22_RFU1 0x318000
  478. /* Maximum number of ports */
  479. #define MVPP22_GOP_MAC_NUM 4
  480. /* Sets the field located at the specified in data */
  481. #define MVPP2_RGMII_TX_FIFO_MIN_TH 0x41
  482. #define MVPP2_SGMII_TX_FIFO_MIN_TH 0x5
  483. #define MVPP2_SGMII2_5_TX_FIFO_MIN_TH 0xb
  484. /* Net Complex */
  485. enum mv_netc_topology {
  486. MV_NETC_GE_MAC2_SGMII = BIT(0),
  487. MV_NETC_GE_MAC3_SGMII = BIT(1),
  488. MV_NETC_GE_MAC3_RGMII = BIT(2),
  489. };
  490. enum mv_netc_phase {
  491. MV_NETC_FIRST_PHASE,
  492. MV_NETC_SECOND_PHASE,
  493. };
  494. enum mv_netc_sgmii_xmi_mode {
  495. MV_NETC_GBE_SGMII,
  496. MV_NETC_GBE_XMII,
  497. };
  498. enum mv_netc_mii_mode {
  499. MV_NETC_GBE_RGMII,
  500. MV_NETC_GBE_MII,
  501. };
  502. enum mv_netc_lanes {
  503. MV_NETC_LANE_23,
  504. MV_NETC_LANE_45,
  505. };
  506. /* Various constants */
  507. /* Coalescing */
  508. #define MVPP2_TXDONE_COAL_PKTS_THRESH 15
  509. #define MVPP2_TXDONE_HRTIMER_PERIOD_NS 1000000UL
  510. #define MVPP2_RX_COAL_PKTS 32
  511. #define MVPP2_RX_COAL_USEC 100
  512. /* The two bytes Marvell header. Either contains a special value used
  513. * by Marvell switches when a specific hardware mode is enabled (not
  514. * supported by this driver) or is filled automatically by zeroes on
  515. * the RX side. Those two bytes being at the front of the Ethernet
  516. * header, they allow to have the IP header aligned on a 4 bytes
  517. * boundary automatically: the hardware skips those two bytes on its
  518. * own.
  519. */
  520. #define MVPP2_MH_SIZE 2
  521. #define MVPP2_ETH_TYPE_LEN 2
  522. #define MVPP2_PPPOE_HDR_SIZE 8
  523. #define MVPP2_VLAN_TAG_LEN 4
  524. /* Lbtd 802.3 type */
  525. #define MVPP2_IP_LBDT_TYPE 0xfffa
  526. #define MVPP2_CPU_D_CACHE_LINE_SIZE 32
  527. #define MVPP2_TX_CSUM_MAX_SIZE 9800
  528. /* Timeout constants */
  529. #define MVPP2_TX_DISABLE_TIMEOUT_MSEC 1000
  530. #define MVPP2_TX_PENDING_TIMEOUT_MSEC 1000
  531. #define MVPP2_TX_MTU_MAX 0x7ffff
  532. /* Maximum number of T-CONTs of PON port */
  533. #define MVPP2_MAX_TCONT 16
  534. /* Maximum number of supported ports */
  535. #define MVPP2_MAX_PORTS 4
  536. /* Maximum number of TXQs used by single port */
  537. #define MVPP2_MAX_TXQ 8
  538. /* Default number of TXQs in use */
  539. #define MVPP2_DEFAULT_TXQ 1
  540. /* Dfault number of RXQs in use */
  541. #define MVPP2_DEFAULT_RXQ 1
  542. #define CONFIG_MV_ETH_RXQ 8 /* increment by 8 */
  543. /* Max number of Rx descriptors */
  544. #define MVPP2_MAX_RXD 16
  545. /* Max number of Tx descriptors */
  546. #define MVPP2_MAX_TXD 16
  547. /* Amount of Tx descriptors that can be reserved at once by CPU */
  548. #define MVPP2_CPU_DESC_CHUNK 64
  549. /* Max number of Tx descriptors in each aggregated queue */
  550. #define MVPP2_AGGR_TXQ_SIZE 256
  551. /* Descriptor aligned size */
  552. #define MVPP2_DESC_ALIGNED_SIZE 32
  553. /* Descriptor alignment mask */
  554. #define MVPP2_TX_DESC_ALIGN (MVPP2_DESC_ALIGNED_SIZE - 1)
  555. /* RX FIFO constants */
  556. #define MVPP21_RX_FIFO_PORT_DATA_SIZE 0x2000
  557. #define MVPP21_RX_FIFO_PORT_ATTR_SIZE 0x80
  558. #define MVPP22_RX_FIFO_10GB_PORT_DATA_SIZE 0x8000
  559. #define MVPP22_RX_FIFO_2_5GB_PORT_DATA_SIZE 0x2000
  560. #define MVPP22_RX_FIFO_1GB_PORT_DATA_SIZE 0x1000
  561. #define MVPP22_RX_FIFO_10GB_PORT_ATTR_SIZE 0x200
  562. #define MVPP22_RX_FIFO_2_5GB_PORT_ATTR_SIZE 0x80
  563. #define MVPP22_RX_FIFO_1GB_PORT_ATTR_SIZE 0x40
  564. #define MVPP2_RX_FIFO_PORT_MIN_PKT 0x80
  565. /* TX general registers */
  566. #define MVPP22_TX_FIFO_SIZE_REG(eth_tx_port) (0x8860 + ((eth_tx_port) << 2))
  567. #define MVPP22_TX_FIFO_SIZE_MASK 0xf
  568. /* TX FIFO constants */
  569. #define MVPP2_TX_FIFO_DATA_SIZE_10KB 0xa
  570. #define MVPP2_TX_FIFO_DATA_SIZE_3KB 0x3
  571. /* RX buffer constants */
  572. #define MVPP2_SKB_SHINFO_SIZE \
  573. 0
  574. #define MVPP2_RX_PKT_SIZE(mtu) \
  575. ALIGN((mtu) + MVPP2_MH_SIZE + MVPP2_VLAN_TAG_LEN + \
  576. ETH_HLEN + ETH_FCS_LEN, MVPP2_CPU_D_CACHE_LINE_SIZE)
  577. #define MVPP2_RX_BUF_SIZE(pkt_size) ((pkt_size) + NET_SKB_PAD)
  578. #define MVPP2_RX_TOTAL_SIZE(buf_size) ((buf_size) + MVPP2_SKB_SHINFO_SIZE)
  579. #define MVPP2_RX_MAX_PKT_SIZE(total_size) \
  580. ((total_size) - NET_SKB_PAD - MVPP2_SKB_SHINFO_SIZE)
  581. #define MVPP2_BIT_TO_BYTE(bit) ((bit) / 8)
  582. /* IPv6 max L3 address size */
  583. #define MVPP2_MAX_L3_ADDR_SIZE 16
  584. /* Port flags */
  585. #define MVPP2_F_LOOPBACK BIT(0)
  586. /* Marvell tag types */
  587. enum mvpp2_tag_type {
  588. MVPP2_TAG_TYPE_NONE = 0,
  589. MVPP2_TAG_TYPE_MH = 1,
  590. MVPP2_TAG_TYPE_DSA = 2,
  591. MVPP2_TAG_TYPE_EDSA = 3,
  592. MVPP2_TAG_TYPE_VLAN = 4,
  593. MVPP2_TAG_TYPE_LAST = 5
  594. };
  595. /* Parser constants */
  596. #define MVPP2_PRS_TCAM_SRAM_SIZE 256
  597. #define MVPP2_PRS_TCAM_WORDS 6
  598. #define MVPP2_PRS_SRAM_WORDS 4
  599. #define MVPP2_PRS_FLOW_ID_SIZE 64
  600. #define MVPP2_PRS_FLOW_ID_MASK 0x3f
  601. #define MVPP2_PRS_TCAM_ENTRY_INVALID 1
  602. #define MVPP2_PRS_TCAM_DSA_TAGGED_BIT BIT(5)
  603. #define MVPP2_PRS_IPV4_HEAD 0x40
  604. #define MVPP2_PRS_IPV4_HEAD_MASK 0xf0
  605. #define MVPP2_PRS_IPV4_MC 0xe0
  606. #define MVPP2_PRS_IPV4_MC_MASK 0xf0
  607. #define MVPP2_PRS_IPV4_BC_MASK 0xff
  608. #define MVPP2_PRS_IPV4_IHL 0x5
  609. #define MVPP2_PRS_IPV4_IHL_MASK 0xf
  610. #define MVPP2_PRS_IPV6_MC 0xff
  611. #define MVPP2_PRS_IPV6_MC_MASK 0xff
  612. #define MVPP2_PRS_IPV6_HOP_MASK 0xff
  613. #define MVPP2_PRS_TCAM_PROTO_MASK 0xff
  614. #define MVPP2_PRS_TCAM_PROTO_MASK_L 0x3f
  615. #define MVPP2_PRS_DBL_VLANS_MAX 100
  616. /* Tcam structure:
  617. * - lookup ID - 4 bits
  618. * - port ID - 1 byte
  619. * - additional information - 1 byte
  620. * - header data - 8 bytes
  621. * The fields are represented by MVPP2_PRS_TCAM_DATA_REG(5)->(0).
  622. */
  623. #define MVPP2_PRS_AI_BITS 8
  624. #define MVPP2_PRS_PORT_MASK 0xff
  625. #define MVPP2_PRS_LU_MASK 0xf
  626. #define MVPP2_PRS_TCAM_DATA_BYTE(offs) \
  627. (((offs) - ((offs) % 2)) * 2 + ((offs) % 2))
  628. #define MVPP2_PRS_TCAM_DATA_BYTE_EN(offs) \
  629. (((offs) * 2) - ((offs) % 2) + 2)
  630. #define MVPP2_PRS_TCAM_AI_BYTE 16
  631. #define MVPP2_PRS_TCAM_PORT_BYTE 17
  632. #define MVPP2_PRS_TCAM_LU_BYTE 20
  633. #define MVPP2_PRS_TCAM_EN_OFFS(offs) ((offs) + 2)
  634. #define MVPP2_PRS_TCAM_INV_WORD 5
  635. /* Tcam entries ID */
  636. #define MVPP2_PE_DROP_ALL 0
  637. #define MVPP2_PE_FIRST_FREE_TID 1
  638. #define MVPP2_PE_LAST_FREE_TID (MVPP2_PRS_TCAM_SRAM_SIZE - 31)
  639. #define MVPP2_PE_IP6_EXT_PROTO_UN (MVPP2_PRS_TCAM_SRAM_SIZE - 30)
  640. #define MVPP2_PE_MAC_MC_IP6 (MVPP2_PRS_TCAM_SRAM_SIZE - 29)
  641. #define MVPP2_PE_IP6_ADDR_UN (MVPP2_PRS_TCAM_SRAM_SIZE - 28)
  642. #define MVPP2_PE_IP4_ADDR_UN (MVPP2_PRS_TCAM_SRAM_SIZE - 27)
  643. #define MVPP2_PE_LAST_DEFAULT_FLOW (MVPP2_PRS_TCAM_SRAM_SIZE - 26)
  644. #define MVPP2_PE_FIRST_DEFAULT_FLOW (MVPP2_PRS_TCAM_SRAM_SIZE - 19)
  645. #define MVPP2_PE_EDSA_TAGGED (MVPP2_PRS_TCAM_SRAM_SIZE - 18)
  646. #define MVPP2_PE_EDSA_UNTAGGED (MVPP2_PRS_TCAM_SRAM_SIZE - 17)
  647. #define MVPP2_PE_DSA_TAGGED (MVPP2_PRS_TCAM_SRAM_SIZE - 16)
  648. #define MVPP2_PE_DSA_UNTAGGED (MVPP2_PRS_TCAM_SRAM_SIZE - 15)
  649. #define MVPP2_PE_ETYPE_EDSA_TAGGED (MVPP2_PRS_TCAM_SRAM_SIZE - 14)
  650. #define MVPP2_PE_ETYPE_EDSA_UNTAGGED (MVPP2_PRS_TCAM_SRAM_SIZE - 13)
  651. #define MVPP2_PE_ETYPE_DSA_TAGGED (MVPP2_PRS_TCAM_SRAM_SIZE - 12)
  652. #define MVPP2_PE_ETYPE_DSA_UNTAGGED (MVPP2_PRS_TCAM_SRAM_SIZE - 11)
  653. #define MVPP2_PE_MH_DEFAULT (MVPP2_PRS_TCAM_SRAM_SIZE - 10)
  654. #define MVPP2_PE_DSA_DEFAULT (MVPP2_PRS_TCAM_SRAM_SIZE - 9)
  655. #define MVPP2_PE_IP6_PROTO_UN (MVPP2_PRS_TCAM_SRAM_SIZE - 8)
  656. #define MVPP2_PE_IP4_PROTO_UN (MVPP2_PRS_TCAM_SRAM_SIZE - 7)
  657. #define MVPP2_PE_ETH_TYPE_UN (MVPP2_PRS_TCAM_SRAM_SIZE - 6)
  658. #define MVPP2_PE_VLAN_DBL (MVPP2_PRS_TCAM_SRAM_SIZE - 5)
  659. #define MVPP2_PE_VLAN_NONE (MVPP2_PRS_TCAM_SRAM_SIZE - 4)
  660. #define MVPP2_PE_MAC_MC_ALL (MVPP2_PRS_TCAM_SRAM_SIZE - 3)
  661. #define MVPP2_PE_MAC_PROMISCUOUS (MVPP2_PRS_TCAM_SRAM_SIZE - 2)
  662. #define MVPP2_PE_MAC_NON_PROMISCUOUS (MVPP2_PRS_TCAM_SRAM_SIZE - 1)
  663. /* Sram structure
  664. * The fields are represented by MVPP2_PRS_TCAM_DATA_REG(3)->(0).
  665. */
  666. #define MVPP2_PRS_SRAM_RI_OFFS 0
  667. #define MVPP2_PRS_SRAM_RI_WORD 0
  668. #define MVPP2_PRS_SRAM_RI_CTRL_OFFS 32
  669. #define MVPP2_PRS_SRAM_RI_CTRL_WORD 1
  670. #define MVPP2_PRS_SRAM_RI_CTRL_BITS 32
  671. #define MVPP2_PRS_SRAM_SHIFT_OFFS 64
  672. #define MVPP2_PRS_SRAM_SHIFT_SIGN_BIT 72
  673. #define MVPP2_PRS_SRAM_UDF_OFFS 73
  674. #define MVPP2_PRS_SRAM_UDF_BITS 8
  675. #define MVPP2_PRS_SRAM_UDF_MASK 0xff
  676. #define MVPP2_PRS_SRAM_UDF_SIGN_BIT 81
  677. #define MVPP2_PRS_SRAM_UDF_TYPE_OFFS 82
  678. #define MVPP2_PRS_SRAM_UDF_TYPE_MASK 0x7
  679. #define MVPP2_PRS_SRAM_UDF_TYPE_L3 1
  680. #define MVPP2_PRS_SRAM_UDF_TYPE_L4 4
  681. #define MVPP2_PRS_SRAM_OP_SEL_SHIFT_OFFS 85
  682. #define MVPP2_PRS_SRAM_OP_SEL_SHIFT_MASK 0x3
  683. #define MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD 1
  684. #define MVPP2_PRS_SRAM_OP_SEL_SHIFT_IP4_ADD 2
  685. #define MVPP2_PRS_SRAM_OP_SEL_SHIFT_IP6_ADD 3
  686. #define MVPP2_PRS_SRAM_OP_SEL_UDF_OFFS 87
  687. #define MVPP2_PRS_SRAM_OP_SEL_UDF_BITS 2
  688. #define MVPP2_PRS_SRAM_OP_SEL_UDF_MASK 0x3
  689. #define MVPP2_PRS_SRAM_OP_SEL_UDF_ADD 0
  690. #define MVPP2_PRS_SRAM_OP_SEL_UDF_IP4_ADD 2
  691. #define MVPP2_PRS_SRAM_OP_SEL_UDF_IP6_ADD 3
  692. #define MVPP2_PRS_SRAM_OP_SEL_BASE_OFFS 89
  693. #define MVPP2_PRS_SRAM_AI_OFFS 90
  694. #define MVPP2_PRS_SRAM_AI_CTRL_OFFS 98
  695. #define MVPP2_PRS_SRAM_AI_CTRL_BITS 8
  696. #define MVPP2_PRS_SRAM_AI_MASK 0xff
  697. #define MVPP2_PRS_SRAM_NEXT_LU_OFFS 106
  698. #define MVPP2_PRS_SRAM_NEXT_LU_MASK 0xf
  699. #define MVPP2_PRS_SRAM_LU_DONE_BIT 110
  700. #define MVPP2_PRS_SRAM_LU_GEN_BIT 111
  701. /* Sram result info bits assignment */
  702. #define MVPP2_PRS_RI_MAC_ME_MASK 0x1
  703. #define MVPP2_PRS_RI_DSA_MASK 0x2
  704. #define MVPP2_PRS_RI_VLAN_MASK (BIT(2) | BIT(3))
  705. #define MVPP2_PRS_RI_VLAN_NONE 0x0
  706. #define MVPP2_PRS_RI_VLAN_SINGLE BIT(2)
  707. #define MVPP2_PRS_RI_VLAN_DOUBLE BIT(3)
  708. #define MVPP2_PRS_RI_VLAN_TRIPLE (BIT(2) | BIT(3))
  709. #define MVPP2_PRS_RI_CPU_CODE_MASK 0x70
  710. #define MVPP2_PRS_RI_CPU_CODE_RX_SPEC BIT(4)
  711. #define MVPP2_PRS_RI_L2_CAST_MASK (BIT(9) | BIT(10))
  712. #define MVPP2_PRS_RI_L2_UCAST 0x0
  713. #define MVPP2_PRS_RI_L2_MCAST BIT(9)
  714. #define MVPP2_PRS_RI_L2_BCAST BIT(10)
  715. #define MVPP2_PRS_RI_PPPOE_MASK 0x800
  716. #define MVPP2_PRS_RI_L3_PROTO_MASK (BIT(12) | BIT(13) | BIT(14))
  717. #define MVPP2_PRS_RI_L3_UN 0x0
  718. #define MVPP2_PRS_RI_L3_IP4 BIT(12)
  719. #define MVPP2_PRS_RI_L3_IP4_OPT BIT(13)
  720. #define MVPP2_PRS_RI_L3_IP4_OTHER (BIT(12) | BIT(13))
  721. #define MVPP2_PRS_RI_L3_IP6 BIT(14)
  722. #define MVPP2_PRS_RI_L3_IP6_EXT (BIT(12) | BIT(14))
  723. #define MVPP2_PRS_RI_L3_ARP (BIT(13) | BIT(14))
  724. #define MVPP2_PRS_RI_L3_ADDR_MASK (BIT(15) | BIT(16))
  725. #define MVPP2_PRS_RI_L3_UCAST 0x0
  726. #define MVPP2_PRS_RI_L3_MCAST BIT(15)
  727. #define MVPP2_PRS_RI_L3_BCAST (BIT(15) | BIT(16))
  728. #define MVPP2_PRS_RI_IP_FRAG_MASK 0x20000
  729. #define MVPP2_PRS_RI_UDF3_MASK 0x300000
  730. #define MVPP2_PRS_RI_UDF3_RX_SPECIAL BIT(21)
  731. #define MVPP2_PRS_RI_L4_PROTO_MASK 0x1c00000
  732. #define MVPP2_PRS_RI_L4_TCP BIT(22)
  733. #define MVPP2_PRS_RI_L4_UDP BIT(23)
  734. #define MVPP2_PRS_RI_L4_OTHER (BIT(22) | BIT(23))
  735. #define MVPP2_PRS_RI_UDF7_MASK 0x60000000
  736. #define MVPP2_PRS_RI_UDF7_IP6_LITE BIT(29)
  737. #define MVPP2_PRS_RI_DROP_MASK 0x80000000
  738. /* Sram additional info bits assignment */
  739. #define MVPP2_PRS_IPV4_DIP_AI_BIT BIT(0)
  740. #define MVPP2_PRS_IPV6_NO_EXT_AI_BIT BIT(0)
  741. #define MVPP2_PRS_IPV6_EXT_AI_BIT BIT(1)
  742. #define MVPP2_PRS_IPV6_EXT_AH_AI_BIT BIT(2)
  743. #define MVPP2_PRS_IPV6_EXT_AH_LEN_AI_BIT BIT(3)
  744. #define MVPP2_PRS_IPV6_EXT_AH_L4_AI_BIT BIT(4)
  745. #define MVPP2_PRS_SINGLE_VLAN_AI 0
  746. #define MVPP2_PRS_DBL_VLAN_AI_BIT BIT(7)
  747. /* DSA/EDSA type */
  748. #define MVPP2_PRS_TAGGED true
  749. #define MVPP2_PRS_UNTAGGED false
  750. #define MVPP2_PRS_EDSA true
  751. #define MVPP2_PRS_DSA false
  752. /* MAC entries, shadow udf */
  753. enum mvpp2_prs_udf {
  754. MVPP2_PRS_UDF_MAC_DEF,
  755. MVPP2_PRS_UDF_MAC_RANGE,
  756. MVPP2_PRS_UDF_L2_DEF,
  757. MVPP2_PRS_UDF_L2_DEF_COPY,
  758. MVPP2_PRS_UDF_L2_USER,
  759. };
  760. /* Lookup ID */
  761. enum mvpp2_prs_lookup {
  762. MVPP2_PRS_LU_MH,
  763. MVPP2_PRS_LU_MAC,
  764. MVPP2_PRS_LU_DSA,
  765. MVPP2_PRS_LU_VLAN,
  766. MVPP2_PRS_LU_L2,
  767. MVPP2_PRS_LU_PPPOE,
  768. MVPP2_PRS_LU_IP4,
  769. MVPP2_PRS_LU_IP6,
  770. MVPP2_PRS_LU_FLOWS,
  771. MVPP2_PRS_LU_LAST,
  772. };
  773. /* L3 cast enum */
  774. enum mvpp2_prs_l3_cast {
  775. MVPP2_PRS_L3_UNI_CAST,
  776. MVPP2_PRS_L3_MULTI_CAST,
  777. MVPP2_PRS_L3_BROAD_CAST
  778. };
  779. /* Classifier constants */
  780. #define MVPP2_CLS_FLOWS_TBL_SIZE 512
  781. #define MVPP2_CLS_FLOWS_TBL_DATA_WORDS 3
  782. #define MVPP2_CLS_LKP_TBL_SIZE 64
  783. /* BM constants */
  784. #define MVPP2_BM_POOLS_NUM 1
  785. #define MVPP2_BM_LONG_BUF_NUM 16
  786. #define MVPP2_BM_SHORT_BUF_NUM 16
  787. #define MVPP2_BM_POOL_SIZE_MAX (16*1024 - MVPP2_BM_POOL_PTR_ALIGN/4)
  788. #define MVPP2_BM_POOL_PTR_ALIGN 128
  789. #define MVPP2_BM_SWF_LONG_POOL(port) 0
  790. /* BM cookie (32 bits) definition */
  791. #define MVPP2_BM_COOKIE_POOL_OFFS 8
  792. #define MVPP2_BM_COOKIE_CPU_OFFS 24
  793. /* BM short pool packet size
  794. * These value assure that for SWF the total number
  795. * of bytes allocated for each buffer will be 512
  796. */
  797. #define MVPP2_BM_SHORT_PKT_SIZE MVPP2_RX_MAX_PKT_SIZE(512)
  798. enum mvpp2_bm_type {
  799. MVPP2_BM_FREE,
  800. MVPP2_BM_SWF_LONG,
  801. MVPP2_BM_SWF_SHORT
  802. };
  803. /* Definitions */
  804. /* Shared Packet Processor resources */
  805. struct mvpp2 {
  806. /* Shared registers' base addresses */
  807. void __iomem *base;
  808. void __iomem *lms_base;
  809. void __iomem *iface_base;
  810. void __iomem *mdio_base;
  811. void __iomem *mpcs_base;
  812. void __iomem *xpcs_base;
  813. void __iomem *rfu1_base;
  814. u32 netc_config;
  815. /* List of pointers to port structures */
  816. struct mvpp2_port **port_list;
  817. /* Aggregated TXQs */
  818. struct mvpp2_tx_queue *aggr_txqs;
  819. /* BM pools */
  820. struct mvpp2_bm_pool *bm_pools;
  821. /* PRS shadow table */
  822. struct mvpp2_prs_shadow *prs_shadow;
  823. /* PRS auxiliary table for double vlan entries control */
  824. bool *prs_double_vlans;
  825. /* Tclk value */
  826. u32 tclk;
  827. /* HW version */
  828. enum { MVPP21, MVPP22 } hw_version;
  829. /* Maximum number of RXQs per port */
  830. unsigned int max_port_rxqs;
  831. struct mii_dev *bus;
  832. int probe_done;
  833. };
  834. struct mvpp2_pcpu_stats {
  835. u64 rx_packets;
  836. u64 rx_bytes;
  837. u64 tx_packets;
  838. u64 tx_bytes;
  839. };
  840. struct mvpp2_port {
  841. u8 id;
  842. /* Index of the port from the "group of ports" complex point
  843. * of view
  844. */
  845. int gop_id;
  846. int irq;
  847. struct mvpp2 *priv;
  848. /* Per-port registers' base address */
  849. void __iomem *base;
  850. struct mvpp2_rx_queue **rxqs;
  851. struct mvpp2_tx_queue **txqs;
  852. int pkt_size;
  853. u32 pending_cause_rx;
  854. /* Per-CPU port control */
  855. struct mvpp2_port_pcpu __percpu *pcpu;
  856. /* Flags */
  857. unsigned long flags;
  858. u16 tx_ring_size;
  859. u16 rx_ring_size;
  860. struct mvpp2_pcpu_stats __percpu *stats;
  861. struct phy_device *phy_dev;
  862. phy_interface_t phy_interface;
  863. int phy_node;
  864. int phyaddr;
  865. int init;
  866. unsigned int link;
  867. unsigned int duplex;
  868. unsigned int speed;
  869. unsigned int phy_speed; /* SGMII 1Gbps vs 2.5Gbps */
  870. struct mvpp2_bm_pool *pool_long;
  871. struct mvpp2_bm_pool *pool_short;
  872. /* Index of first port's physical RXQ */
  873. u8 first_rxq;
  874. u8 dev_addr[ETH_ALEN];
  875. };
  876. /* The mvpp2_tx_desc and mvpp2_rx_desc structures describe the
  877. * layout of the transmit and reception DMA descriptors, and their
  878. * layout is therefore defined by the hardware design
  879. */
  880. #define MVPP2_TXD_L3_OFF_SHIFT 0
  881. #define MVPP2_TXD_IP_HLEN_SHIFT 8
  882. #define MVPP2_TXD_L4_CSUM_FRAG BIT(13)
  883. #define MVPP2_TXD_L4_CSUM_NOT BIT(14)
  884. #define MVPP2_TXD_IP_CSUM_DISABLE BIT(15)
  885. #define MVPP2_TXD_PADDING_DISABLE BIT(23)
  886. #define MVPP2_TXD_L4_UDP BIT(24)
  887. #define MVPP2_TXD_L3_IP6 BIT(26)
  888. #define MVPP2_TXD_L_DESC BIT(28)
  889. #define MVPP2_TXD_F_DESC BIT(29)
  890. #define MVPP2_RXD_ERR_SUMMARY BIT(15)
  891. #define MVPP2_RXD_ERR_CODE_MASK (BIT(13) | BIT(14))
  892. #define MVPP2_RXD_ERR_CRC 0x0
  893. #define MVPP2_RXD_ERR_OVERRUN BIT(13)
  894. #define MVPP2_RXD_ERR_RESOURCE (BIT(13) | BIT(14))
  895. #define MVPP2_RXD_BM_POOL_ID_OFFS 16
  896. #define MVPP2_RXD_BM_POOL_ID_MASK (BIT(16) | BIT(17) | BIT(18))
  897. #define MVPP2_RXD_HWF_SYNC BIT(21)
  898. #define MVPP2_RXD_L4_CSUM_OK BIT(22)
  899. #define MVPP2_RXD_IP4_HEADER_ERR BIT(24)
  900. #define MVPP2_RXD_L4_TCP BIT(25)
  901. #define MVPP2_RXD_L4_UDP BIT(26)
  902. #define MVPP2_RXD_L3_IP4 BIT(28)
  903. #define MVPP2_RXD_L3_IP6 BIT(30)
  904. #define MVPP2_RXD_BUF_HDR BIT(31)
  905. /* HW TX descriptor for PPv2.1 */
  906. struct mvpp21_tx_desc {
  907. u32 command; /* Options used by HW for packet transmitting.*/
  908. u8 packet_offset; /* the offset from the buffer beginning */
  909. u8 phys_txq; /* destination queue ID */
  910. u16 data_size; /* data size of transmitted packet in bytes */
  911. u32 buf_dma_addr; /* physical addr of transmitted buffer */
  912. u32 buf_cookie; /* cookie for access to TX buffer in tx path */
  913. u32 reserved1[3]; /* hw_cmd (for future use, BM, PON, PNC) */
  914. u32 reserved2; /* reserved (for future use) */
  915. };
  916. /* HW RX descriptor for PPv2.1 */
  917. struct mvpp21_rx_desc {
  918. u32 status; /* info about received packet */
  919. u16 reserved1; /* parser_info (for future use, PnC) */
  920. u16 data_size; /* size of received packet in bytes */
  921. u32 buf_dma_addr; /* physical address of the buffer */
  922. u32 buf_cookie; /* cookie for access to RX buffer in rx path */
  923. u16 reserved2; /* gem_port_id (for future use, PON) */
  924. u16 reserved3; /* csum_l4 (for future use, PnC) */
  925. u8 reserved4; /* bm_qset (for future use, BM) */
  926. u8 reserved5;
  927. u16 reserved6; /* classify_info (for future use, PnC) */
  928. u32 reserved7; /* flow_id (for future use, PnC) */
  929. u32 reserved8;
  930. };
  931. /* HW TX descriptor for PPv2.2 */
  932. struct mvpp22_tx_desc {
  933. u32 command;
  934. u8 packet_offset;
  935. u8 phys_txq;
  936. u16 data_size;
  937. u64 reserved1;
  938. u64 buf_dma_addr_ptp;
  939. u64 buf_cookie_misc;
  940. };
  941. /* HW RX descriptor for PPv2.2 */
  942. struct mvpp22_rx_desc {
  943. u32 status;
  944. u16 reserved1;
  945. u16 data_size;
  946. u32 reserved2;
  947. u32 reserved3;
  948. u64 buf_dma_addr_key_hash;
  949. u64 buf_cookie_misc;
  950. };
  951. /* Opaque type used by the driver to manipulate the HW TX and RX
  952. * descriptors
  953. */
  954. struct mvpp2_tx_desc {
  955. union {
  956. struct mvpp21_tx_desc pp21;
  957. struct mvpp22_tx_desc pp22;
  958. };
  959. };
  960. struct mvpp2_rx_desc {
  961. union {
  962. struct mvpp21_rx_desc pp21;
  963. struct mvpp22_rx_desc pp22;
  964. };
  965. };
  966. /* Per-CPU Tx queue control */
  967. struct mvpp2_txq_pcpu {
  968. int cpu;
  969. /* Number of Tx DMA descriptors in the descriptor ring */
  970. int size;
  971. /* Number of currently used Tx DMA descriptor in the
  972. * descriptor ring
  973. */
  974. int count;
  975. /* Number of Tx DMA descriptors reserved for each CPU */
  976. int reserved_num;
  977. /* Index of last TX DMA descriptor that was inserted */
  978. int txq_put_index;
  979. /* Index of the TX DMA descriptor to be cleaned up */
  980. int txq_get_index;
  981. };
  982. struct mvpp2_tx_queue {
  983. /* Physical number of this Tx queue */
  984. u8 id;
  985. /* Logical number of this Tx queue */
  986. u8 log_id;
  987. /* Number of Tx DMA descriptors in the descriptor ring */
  988. int size;
  989. /* Number of currently used Tx DMA descriptor in the descriptor ring */
  990. int count;
  991. /* Per-CPU control of physical Tx queues */
  992. struct mvpp2_txq_pcpu __percpu *pcpu;
  993. u32 done_pkts_coal;
  994. /* Virtual address of thex Tx DMA descriptors array */
  995. struct mvpp2_tx_desc *descs;
  996. /* DMA address of the Tx DMA descriptors array */
  997. dma_addr_t descs_dma;
  998. /* Index of the last Tx DMA descriptor */
  999. int last_desc;
  1000. /* Index of the next Tx DMA descriptor to process */
  1001. int next_desc_to_proc;
  1002. };
  1003. struct mvpp2_rx_queue {
  1004. /* RX queue number, in the range 0-31 for physical RXQs */
  1005. u8 id;
  1006. /* Num of rx descriptors in the rx descriptor ring */
  1007. int size;
  1008. u32 pkts_coal;
  1009. u32 time_coal;
  1010. /* Virtual address of the RX DMA descriptors array */
  1011. struct mvpp2_rx_desc *descs;
  1012. /* DMA address of the RX DMA descriptors array */
  1013. dma_addr_t descs_dma;
  1014. /* Index of the last RX DMA descriptor */
  1015. int last_desc;
  1016. /* Index of the next RX DMA descriptor to process */
  1017. int next_desc_to_proc;
  1018. /* ID of port to which physical RXQ is mapped */
  1019. int port;
  1020. /* Port's logic RXQ number to which physical RXQ is mapped */
  1021. int logic_rxq;
  1022. };
  1023. union mvpp2_prs_tcam_entry {
  1024. u32 word[MVPP2_PRS_TCAM_WORDS];
  1025. u8 byte[MVPP2_PRS_TCAM_WORDS * 4];
  1026. };
  1027. union mvpp2_prs_sram_entry {
  1028. u32 word[MVPP2_PRS_SRAM_WORDS];
  1029. u8 byte[MVPP2_PRS_SRAM_WORDS * 4];
  1030. };
  1031. struct mvpp2_prs_entry {
  1032. u32 index;
  1033. union mvpp2_prs_tcam_entry tcam;
  1034. union mvpp2_prs_sram_entry sram;
  1035. };
  1036. struct mvpp2_prs_shadow {
  1037. bool valid;
  1038. bool finish;
  1039. /* Lookup ID */
  1040. int lu;
  1041. /* User defined offset */
  1042. int udf;
  1043. /* Result info */
  1044. u32 ri;
  1045. u32 ri_mask;
  1046. };
  1047. struct mvpp2_cls_flow_entry {
  1048. u32 index;
  1049. u32 data[MVPP2_CLS_FLOWS_TBL_DATA_WORDS];
  1050. };
  1051. struct mvpp2_cls_lookup_entry {
  1052. u32 lkpid;
  1053. u32 way;
  1054. u32 data;
  1055. };
  1056. struct mvpp2_bm_pool {
  1057. /* Pool number in the range 0-7 */
  1058. int id;
  1059. enum mvpp2_bm_type type;
  1060. /* Buffer Pointers Pool External (BPPE) size */
  1061. int size;
  1062. /* Number of buffers for this pool */
  1063. int buf_num;
  1064. /* Pool buffer size */
  1065. int buf_size;
  1066. /* Packet size */
  1067. int pkt_size;
  1068. /* BPPE virtual base address */
  1069. unsigned long *virt_addr;
  1070. /* BPPE DMA base address */
  1071. dma_addr_t dma_addr;
  1072. /* Ports using BM pool */
  1073. u32 port_map;
  1074. };
  1075. /* Static declaractions */
  1076. /* Number of RXQs used by single port */
  1077. static int rxq_number = MVPP2_DEFAULT_RXQ;
  1078. /* Number of TXQs used by single port */
  1079. static int txq_number = MVPP2_DEFAULT_TXQ;
  1080. static int base_id;
  1081. #define MVPP2_DRIVER_NAME "mvpp2"
  1082. #define MVPP2_DRIVER_VERSION "1.0"
  1083. /*
  1084. * U-Boot internal data, mostly uncached buffers for descriptors and data
  1085. */
  1086. struct buffer_location {
  1087. struct mvpp2_tx_desc *aggr_tx_descs;
  1088. struct mvpp2_tx_desc *tx_descs;
  1089. struct mvpp2_rx_desc *rx_descs;
  1090. unsigned long *bm_pool[MVPP2_BM_POOLS_NUM];
  1091. unsigned long *rx_buffer[MVPP2_BM_LONG_BUF_NUM];
  1092. int first_rxq;
  1093. };
  1094. /*
  1095. * All 4 interfaces use the same global buffer, since only one interface
  1096. * can be enabled at once
  1097. */
  1098. static struct buffer_location buffer_loc;
  1099. /*
  1100. * Page table entries are set to 1MB, or multiples of 1MB
  1101. * (not < 1MB). driver uses less bd's so use 1MB bdspace.
  1102. */
  1103. #define BD_SPACE (1 << 20)
  1104. /* Utility/helper methods */
  1105. static void mvpp2_write(struct mvpp2 *priv, u32 offset, u32 data)
  1106. {
  1107. writel(data, priv->base + offset);
  1108. }
  1109. static u32 mvpp2_read(struct mvpp2 *priv, u32 offset)
  1110. {
  1111. return readl(priv->base + offset);
  1112. }
  1113. static void mvpp2_txdesc_dma_addr_set(struct mvpp2_port *port,
  1114. struct mvpp2_tx_desc *tx_desc,
  1115. dma_addr_t dma_addr)
  1116. {
  1117. if (port->priv->hw_version == MVPP21) {
  1118. tx_desc->pp21.buf_dma_addr = dma_addr;
  1119. } else {
  1120. u64 val = (u64)dma_addr;
  1121. tx_desc->pp22.buf_dma_addr_ptp &= ~GENMASK_ULL(40, 0);
  1122. tx_desc->pp22.buf_dma_addr_ptp |= val;
  1123. }
  1124. }
  1125. static void mvpp2_txdesc_size_set(struct mvpp2_port *port,
  1126. struct mvpp2_tx_desc *tx_desc,
  1127. size_t size)
  1128. {
  1129. if (port->priv->hw_version == MVPP21)
  1130. tx_desc->pp21.data_size = size;
  1131. else
  1132. tx_desc->pp22.data_size = size;
  1133. }
  1134. static void mvpp2_txdesc_txq_set(struct mvpp2_port *port,
  1135. struct mvpp2_tx_desc *tx_desc,
  1136. unsigned int txq)
  1137. {
  1138. if (port->priv->hw_version == MVPP21)
  1139. tx_desc->pp21.phys_txq = txq;
  1140. else
  1141. tx_desc->pp22.phys_txq = txq;
  1142. }
  1143. static void mvpp2_txdesc_cmd_set(struct mvpp2_port *port,
  1144. struct mvpp2_tx_desc *tx_desc,
  1145. unsigned int command)
  1146. {
  1147. if (port->priv->hw_version == MVPP21)
  1148. tx_desc->pp21.command = command;
  1149. else
  1150. tx_desc->pp22.command = command;
  1151. }
  1152. static void mvpp2_txdesc_offset_set(struct mvpp2_port *port,
  1153. struct mvpp2_tx_desc *tx_desc,
  1154. unsigned int offset)
  1155. {
  1156. if (port->priv->hw_version == MVPP21)
  1157. tx_desc->pp21.packet_offset = offset;
  1158. else
  1159. tx_desc->pp22.packet_offset = offset;
  1160. }
  1161. static dma_addr_t mvpp2_rxdesc_dma_addr_get(struct mvpp2_port *port,
  1162. struct mvpp2_rx_desc *rx_desc)
  1163. {
  1164. if (port->priv->hw_version == MVPP21)
  1165. return rx_desc->pp21.buf_dma_addr;
  1166. else
  1167. return rx_desc->pp22.buf_dma_addr_key_hash & GENMASK_ULL(40, 0);
  1168. }
  1169. static unsigned long mvpp2_rxdesc_cookie_get(struct mvpp2_port *port,
  1170. struct mvpp2_rx_desc *rx_desc)
  1171. {
  1172. if (port->priv->hw_version == MVPP21)
  1173. return rx_desc->pp21.buf_cookie;
  1174. else
  1175. return rx_desc->pp22.buf_cookie_misc & GENMASK_ULL(40, 0);
  1176. }
  1177. static size_t mvpp2_rxdesc_size_get(struct mvpp2_port *port,
  1178. struct mvpp2_rx_desc *rx_desc)
  1179. {
  1180. if (port->priv->hw_version == MVPP21)
  1181. return rx_desc->pp21.data_size;
  1182. else
  1183. return rx_desc->pp22.data_size;
  1184. }
  1185. static u32 mvpp2_rxdesc_status_get(struct mvpp2_port *port,
  1186. struct mvpp2_rx_desc *rx_desc)
  1187. {
  1188. if (port->priv->hw_version == MVPP21)
  1189. return rx_desc->pp21.status;
  1190. else
  1191. return rx_desc->pp22.status;
  1192. }
  1193. static void mvpp2_txq_inc_get(struct mvpp2_txq_pcpu *txq_pcpu)
  1194. {
  1195. txq_pcpu->txq_get_index++;
  1196. if (txq_pcpu->txq_get_index == txq_pcpu->size)
  1197. txq_pcpu->txq_get_index = 0;
  1198. }
  1199. /* Get number of physical egress port */
  1200. static inline int mvpp2_egress_port(struct mvpp2_port *port)
  1201. {
  1202. return MVPP2_MAX_TCONT + port->id;
  1203. }
  1204. /* Get number of physical TXQ */
  1205. static inline int mvpp2_txq_phys(int port, int txq)
  1206. {
  1207. return (MVPP2_MAX_TCONT + port) * MVPP2_MAX_TXQ + txq;
  1208. }
  1209. /* Parser configuration routines */
  1210. /* Update parser tcam and sram hw entries */
  1211. static int mvpp2_prs_hw_write(struct mvpp2 *priv, struct mvpp2_prs_entry *pe)
  1212. {
  1213. int i;
  1214. if (pe->index > MVPP2_PRS_TCAM_SRAM_SIZE - 1)
  1215. return -EINVAL;
  1216. /* Clear entry invalidation bit */
  1217. pe->tcam.word[MVPP2_PRS_TCAM_INV_WORD] &= ~MVPP2_PRS_TCAM_INV_MASK;
  1218. /* Write tcam index - indirect access */
  1219. mvpp2_write(priv, MVPP2_PRS_TCAM_IDX_REG, pe->index);
  1220. for (i = 0; i < MVPP2_PRS_TCAM_WORDS; i++)
  1221. mvpp2_write(priv, MVPP2_PRS_TCAM_DATA_REG(i), pe->tcam.word[i]);
  1222. /* Write sram index - indirect access */
  1223. mvpp2_write(priv, MVPP2_PRS_SRAM_IDX_REG, pe->index);
  1224. for (i = 0; i < MVPP2_PRS_SRAM_WORDS; i++)
  1225. mvpp2_write(priv, MVPP2_PRS_SRAM_DATA_REG(i), pe->sram.word[i]);
  1226. return 0;
  1227. }
  1228. /* Read tcam entry from hw */
  1229. static int mvpp2_prs_hw_read(struct mvpp2 *priv, struct mvpp2_prs_entry *pe)
  1230. {
  1231. int i;
  1232. if (pe->index > MVPP2_PRS_TCAM_SRAM_SIZE - 1)
  1233. return -EINVAL;
  1234. /* Write tcam index - indirect access */
  1235. mvpp2_write(priv, MVPP2_PRS_TCAM_IDX_REG, pe->index);
  1236. pe->tcam.word[MVPP2_PRS_TCAM_INV_WORD] = mvpp2_read(priv,
  1237. MVPP2_PRS_TCAM_DATA_REG(MVPP2_PRS_TCAM_INV_WORD));
  1238. if (pe->tcam.word[MVPP2_PRS_TCAM_INV_WORD] & MVPP2_PRS_TCAM_INV_MASK)
  1239. return MVPP2_PRS_TCAM_ENTRY_INVALID;
  1240. for (i = 0; i < MVPP2_PRS_TCAM_WORDS; i++)
  1241. pe->tcam.word[i] = mvpp2_read(priv, MVPP2_PRS_TCAM_DATA_REG(i));
  1242. /* Write sram index - indirect access */
  1243. mvpp2_write(priv, MVPP2_PRS_SRAM_IDX_REG, pe->index);
  1244. for (i = 0; i < MVPP2_PRS_SRAM_WORDS; i++)
  1245. pe->sram.word[i] = mvpp2_read(priv, MVPP2_PRS_SRAM_DATA_REG(i));
  1246. return 0;
  1247. }
  1248. /* Invalidate tcam hw entry */
  1249. static void mvpp2_prs_hw_inv(struct mvpp2 *priv, int index)
  1250. {
  1251. /* Write index - indirect access */
  1252. mvpp2_write(priv, MVPP2_PRS_TCAM_IDX_REG, index);
  1253. mvpp2_write(priv, MVPP2_PRS_TCAM_DATA_REG(MVPP2_PRS_TCAM_INV_WORD),
  1254. MVPP2_PRS_TCAM_INV_MASK);
  1255. }
  1256. /* Enable shadow table entry and set its lookup ID */
  1257. static void mvpp2_prs_shadow_set(struct mvpp2 *priv, int index, int lu)
  1258. {
  1259. priv->prs_shadow[index].valid = true;
  1260. priv->prs_shadow[index].lu = lu;
  1261. }
  1262. /* Update ri fields in shadow table entry */
  1263. static void mvpp2_prs_shadow_ri_set(struct mvpp2 *priv, int index,
  1264. unsigned int ri, unsigned int ri_mask)
  1265. {
  1266. priv->prs_shadow[index].ri_mask = ri_mask;
  1267. priv->prs_shadow[index].ri = ri;
  1268. }
  1269. /* Update lookup field in tcam sw entry */
  1270. static void mvpp2_prs_tcam_lu_set(struct mvpp2_prs_entry *pe, unsigned int lu)
  1271. {
  1272. int enable_off = MVPP2_PRS_TCAM_EN_OFFS(MVPP2_PRS_TCAM_LU_BYTE);
  1273. pe->tcam.byte[MVPP2_PRS_TCAM_LU_BYTE] = lu;
  1274. pe->tcam.byte[enable_off] = MVPP2_PRS_LU_MASK;
  1275. }
  1276. /* Update mask for single port in tcam sw entry */
  1277. static void mvpp2_prs_tcam_port_set(struct mvpp2_prs_entry *pe,
  1278. unsigned int port, bool add)
  1279. {
  1280. int enable_off = MVPP2_PRS_TCAM_EN_OFFS(MVPP2_PRS_TCAM_PORT_BYTE);
  1281. if (add)
  1282. pe->tcam.byte[enable_off] &= ~(1 << port);
  1283. else
  1284. pe->tcam.byte[enable_off] |= 1 << port;
  1285. }
  1286. /* Update port map in tcam sw entry */
  1287. static void mvpp2_prs_tcam_port_map_set(struct mvpp2_prs_entry *pe,
  1288. unsigned int ports)
  1289. {
  1290. unsigned char port_mask = MVPP2_PRS_PORT_MASK;
  1291. int enable_off = MVPP2_PRS_TCAM_EN_OFFS(MVPP2_PRS_TCAM_PORT_BYTE);
  1292. pe->tcam.byte[MVPP2_PRS_TCAM_PORT_BYTE] = 0;
  1293. pe->tcam.byte[enable_off] &= ~port_mask;
  1294. pe->tcam.byte[enable_off] |= ~ports & MVPP2_PRS_PORT_MASK;
  1295. }
  1296. /* Obtain port map from tcam sw entry */
  1297. static unsigned int mvpp2_prs_tcam_port_map_get(struct mvpp2_prs_entry *pe)
  1298. {
  1299. int enable_off = MVPP2_PRS_TCAM_EN_OFFS(MVPP2_PRS_TCAM_PORT_BYTE);
  1300. return ~(pe->tcam.byte[enable_off]) & MVPP2_PRS_PORT_MASK;
  1301. }
  1302. /* Set byte of data and its enable bits in tcam sw entry */
  1303. static void mvpp2_prs_tcam_data_byte_set(struct mvpp2_prs_entry *pe,
  1304. unsigned int offs, unsigned char byte,
  1305. unsigned char enable)
  1306. {
  1307. pe->tcam.byte[MVPP2_PRS_TCAM_DATA_BYTE(offs)] = byte;
  1308. pe->tcam.byte[MVPP2_PRS_TCAM_DATA_BYTE_EN(offs)] = enable;
  1309. }
  1310. /* Get byte of data and its enable bits from tcam sw entry */
  1311. static void mvpp2_prs_tcam_data_byte_get(struct mvpp2_prs_entry *pe,
  1312. unsigned int offs, unsigned char *byte,
  1313. unsigned char *enable)
  1314. {
  1315. *byte = pe->tcam.byte[MVPP2_PRS_TCAM_DATA_BYTE(offs)];
  1316. *enable = pe->tcam.byte[MVPP2_PRS_TCAM_DATA_BYTE_EN(offs)];
  1317. }
  1318. /* Set ethertype in tcam sw entry */
  1319. static void mvpp2_prs_match_etype(struct mvpp2_prs_entry *pe, int offset,
  1320. unsigned short ethertype)
  1321. {
  1322. mvpp2_prs_tcam_data_byte_set(pe, offset + 0, ethertype >> 8, 0xff);
  1323. mvpp2_prs_tcam_data_byte_set(pe, offset + 1, ethertype & 0xff, 0xff);
  1324. }
  1325. /* Set bits in sram sw entry */
  1326. static void mvpp2_prs_sram_bits_set(struct mvpp2_prs_entry *pe, int bit_num,
  1327. int val)
  1328. {
  1329. pe->sram.byte[MVPP2_BIT_TO_BYTE(bit_num)] |= (val << (bit_num % 8));
  1330. }
  1331. /* Clear bits in sram sw entry */
  1332. static void mvpp2_prs_sram_bits_clear(struct mvpp2_prs_entry *pe, int bit_num,
  1333. int val)
  1334. {
  1335. pe->sram.byte[MVPP2_BIT_TO_BYTE(bit_num)] &= ~(val << (bit_num % 8));
  1336. }
  1337. /* Update ri bits in sram sw entry */
  1338. static void mvpp2_prs_sram_ri_update(struct mvpp2_prs_entry *pe,
  1339. unsigned int bits, unsigned int mask)
  1340. {
  1341. unsigned int i;
  1342. for (i = 0; i < MVPP2_PRS_SRAM_RI_CTRL_BITS; i++) {
  1343. int ri_off = MVPP2_PRS_SRAM_RI_OFFS;
  1344. if (!(mask & BIT(i)))
  1345. continue;
  1346. if (bits & BIT(i))
  1347. mvpp2_prs_sram_bits_set(pe, ri_off + i, 1);
  1348. else
  1349. mvpp2_prs_sram_bits_clear(pe, ri_off + i, 1);
  1350. mvpp2_prs_sram_bits_set(pe, MVPP2_PRS_SRAM_RI_CTRL_OFFS + i, 1);
  1351. }
  1352. }
  1353. /* Update ai bits in sram sw entry */
  1354. static void mvpp2_prs_sram_ai_update(struct mvpp2_prs_entry *pe,
  1355. unsigned int bits, unsigned int mask)
  1356. {
  1357. unsigned int i;
  1358. int ai_off = MVPP2_PRS_SRAM_AI_OFFS;
  1359. for (i = 0; i < MVPP2_PRS_SRAM_AI_CTRL_BITS; i++) {
  1360. if (!(mask & BIT(i)))
  1361. continue;
  1362. if (bits & BIT(i))
  1363. mvpp2_prs_sram_bits_set(pe, ai_off + i, 1);
  1364. else
  1365. mvpp2_prs_sram_bits_clear(pe, ai_off + i, 1);
  1366. mvpp2_prs_sram_bits_set(pe, MVPP2_PRS_SRAM_AI_CTRL_OFFS + i, 1);
  1367. }
  1368. }
  1369. /* Read ai bits from sram sw entry */
  1370. static int mvpp2_prs_sram_ai_get(struct mvpp2_prs_entry *pe)
  1371. {
  1372. u8 bits;
  1373. int ai_off = MVPP2_BIT_TO_BYTE(MVPP2_PRS_SRAM_AI_OFFS);
  1374. int ai_en_off = ai_off + 1;
  1375. int ai_shift = MVPP2_PRS_SRAM_AI_OFFS % 8;
  1376. bits = (pe->sram.byte[ai_off] >> ai_shift) |
  1377. (pe->sram.byte[ai_en_off] << (8 - ai_shift));
  1378. return bits;
  1379. }
  1380. /* In sram sw entry set lookup ID field of the tcam key to be used in the next
  1381. * lookup interation
  1382. */
  1383. static void mvpp2_prs_sram_next_lu_set(struct mvpp2_prs_entry *pe,
  1384. unsigned int lu)
  1385. {
  1386. int sram_next_off = MVPP2_PRS_SRAM_NEXT_LU_OFFS;
  1387. mvpp2_prs_sram_bits_clear(pe, sram_next_off,
  1388. MVPP2_PRS_SRAM_NEXT_LU_MASK);
  1389. mvpp2_prs_sram_bits_set(pe, sram_next_off, lu);
  1390. }
  1391. /* In the sram sw entry set sign and value of the next lookup offset
  1392. * and the offset value generated to the classifier
  1393. */
  1394. static void mvpp2_prs_sram_shift_set(struct mvpp2_prs_entry *pe, int shift,
  1395. unsigned int op)
  1396. {
  1397. /* Set sign */
  1398. if (shift < 0) {
  1399. mvpp2_prs_sram_bits_set(pe, MVPP2_PRS_SRAM_SHIFT_SIGN_BIT, 1);
  1400. shift = 0 - shift;
  1401. } else {
  1402. mvpp2_prs_sram_bits_clear(pe, MVPP2_PRS_SRAM_SHIFT_SIGN_BIT, 1);
  1403. }
  1404. /* Set value */
  1405. pe->sram.byte[MVPP2_BIT_TO_BYTE(MVPP2_PRS_SRAM_SHIFT_OFFS)] =
  1406. (unsigned char)shift;
  1407. /* Reset and set operation */
  1408. mvpp2_prs_sram_bits_clear(pe, MVPP2_PRS_SRAM_OP_SEL_SHIFT_OFFS,
  1409. MVPP2_PRS_SRAM_OP_SEL_SHIFT_MASK);
  1410. mvpp2_prs_sram_bits_set(pe, MVPP2_PRS_SRAM_OP_SEL_SHIFT_OFFS, op);
  1411. /* Set base offset as current */
  1412. mvpp2_prs_sram_bits_clear(pe, MVPP2_PRS_SRAM_OP_SEL_BASE_OFFS, 1);
  1413. }
  1414. /* In the sram sw entry set sign and value of the user defined offset
  1415. * generated to the classifier
  1416. */
  1417. static void mvpp2_prs_sram_offset_set(struct mvpp2_prs_entry *pe,
  1418. unsigned int type, int offset,
  1419. unsigned int op)
  1420. {
  1421. /* Set sign */
  1422. if (offset < 0) {
  1423. mvpp2_prs_sram_bits_set(pe, MVPP2_PRS_SRAM_UDF_SIGN_BIT, 1);
  1424. offset = 0 - offset;
  1425. } else {
  1426. mvpp2_prs_sram_bits_clear(pe, MVPP2_PRS_SRAM_UDF_SIGN_BIT, 1);
  1427. }
  1428. /* Set value */
  1429. mvpp2_prs_sram_bits_clear(pe, MVPP2_PRS_SRAM_UDF_OFFS,
  1430. MVPP2_PRS_SRAM_UDF_MASK);
  1431. mvpp2_prs_sram_bits_set(pe, MVPP2_PRS_SRAM_UDF_OFFS, offset);
  1432. pe->sram.byte[MVPP2_BIT_TO_BYTE(MVPP2_PRS_SRAM_UDF_OFFS +
  1433. MVPP2_PRS_SRAM_UDF_BITS)] &=
  1434. ~(MVPP2_PRS_SRAM_UDF_MASK >> (8 - (MVPP2_PRS_SRAM_UDF_OFFS % 8)));
  1435. pe->sram.byte[MVPP2_BIT_TO_BYTE(MVPP2_PRS_SRAM_UDF_OFFS +
  1436. MVPP2_PRS_SRAM_UDF_BITS)] |=
  1437. (offset >> (8 - (MVPP2_PRS_SRAM_UDF_OFFS % 8)));
  1438. /* Set offset type */
  1439. mvpp2_prs_sram_bits_clear(pe, MVPP2_PRS_SRAM_UDF_TYPE_OFFS,
  1440. MVPP2_PRS_SRAM_UDF_TYPE_MASK);
  1441. mvpp2_prs_sram_bits_set(pe, MVPP2_PRS_SRAM_UDF_TYPE_OFFS, type);
  1442. /* Set offset operation */
  1443. mvpp2_prs_sram_bits_clear(pe, MVPP2_PRS_SRAM_OP_SEL_UDF_OFFS,
  1444. MVPP2_PRS_SRAM_OP_SEL_UDF_MASK);
  1445. mvpp2_prs_sram_bits_set(pe, MVPP2_PRS_SRAM_OP_SEL_UDF_OFFS, op);
  1446. pe->sram.byte[MVPP2_BIT_TO_BYTE(MVPP2_PRS_SRAM_OP_SEL_UDF_OFFS +
  1447. MVPP2_PRS_SRAM_OP_SEL_UDF_BITS)] &=
  1448. ~(MVPP2_PRS_SRAM_OP_SEL_UDF_MASK >>
  1449. (8 - (MVPP2_PRS_SRAM_OP_SEL_UDF_OFFS % 8)));
  1450. pe->sram.byte[MVPP2_BIT_TO_BYTE(MVPP2_PRS_SRAM_OP_SEL_UDF_OFFS +
  1451. MVPP2_PRS_SRAM_OP_SEL_UDF_BITS)] |=
  1452. (op >> (8 - (MVPP2_PRS_SRAM_OP_SEL_UDF_OFFS % 8)));
  1453. /* Set base offset as current */
  1454. mvpp2_prs_sram_bits_clear(pe, MVPP2_PRS_SRAM_OP_SEL_BASE_OFFS, 1);
  1455. }
  1456. /* Find parser flow entry */
  1457. static struct mvpp2_prs_entry *mvpp2_prs_flow_find(struct mvpp2 *priv, int flow)
  1458. {
  1459. struct mvpp2_prs_entry *pe;
  1460. int tid;
  1461. pe = kzalloc(sizeof(*pe), GFP_KERNEL);
  1462. if (!pe)
  1463. return NULL;
  1464. mvpp2_prs_tcam_lu_set(pe, MVPP2_PRS_LU_FLOWS);
  1465. /* Go through the all entires with MVPP2_PRS_LU_FLOWS */
  1466. for (tid = MVPP2_PRS_TCAM_SRAM_SIZE - 1; tid >= 0; tid--) {
  1467. u8 bits;
  1468. if (!priv->prs_shadow[tid].valid ||
  1469. priv->prs_shadow[tid].lu != MVPP2_PRS_LU_FLOWS)
  1470. continue;
  1471. pe->index = tid;
  1472. mvpp2_prs_hw_read(priv, pe);
  1473. bits = mvpp2_prs_sram_ai_get(pe);
  1474. /* Sram store classification lookup ID in AI bits [5:0] */
  1475. if ((bits & MVPP2_PRS_FLOW_ID_MASK) == flow)
  1476. return pe;
  1477. }
  1478. kfree(pe);
  1479. return NULL;
  1480. }
  1481. /* Return first free tcam index, seeking from start to end */
  1482. static int mvpp2_prs_tcam_first_free(struct mvpp2 *priv, unsigned char start,
  1483. unsigned char end)
  1484. {
  1485. int tid;
  1486. if (start > end)
  1487. swap(start, end);
  1488. if (end >= MVPP2_PRS_TCAM_SRAM_SIZE)
  1489. end = MVPP2_PRS_TCAM_SRAM_SIZE - 1;
  1490. for (tid = start; tid <= end; tid++) {
  1491. if (!priv->prs_shadow[tid].valid)
  1492. return tid;
  1493. }
  1494. return -EINVAL;
  1495. }
  1496. /* Enable/disable dropping all mac da's */
  1497. static void mvpp2_prs_mac_drop_all_set(struct mvpp2 *priv, int port, bool add)
  1498. {
  1499. struct mvpp2_prs_entry pe;
  1500. if (priv->prs_shadow[MVPP2_PE_DROP_ALL].valid) {
  1501. /* Entry exist - update port only */
  1502. pe.index = MVPP2_PE_DROP_ALL;
  1503. mvpp2_prs_hw_read(priv, &pe);
  1504. } else {
  1505. /* Entry doesn't exist - create new */
  1506. memset(&pe, 0, sizeof(struct mvpp2_prs_entry));
  1507. mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_MAC);
  1508. pe.index = MVPP2_PE_DROP_ALL;
  1509. /* Non-promiscuous mode for all ports - DROP unknown packets */
  1510. mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_DROP_MASK,
  1511. MVPP2_PRS_RI_DROP_MASK);
  1512. mvpp2_prs_sram_bits_set(&pe, MVPP2_PRS_SRAM_LU_GEN_BIT, 1);
  1513. mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_FLOWS);
  1514. /* Update shadow table */
  1515. mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_MAC);
  1516. /* Mask all ports */
  1517. mvpp2_prs_tcam_port_map_set(&pe, 0);
  1518. }
  1519. /* Update port mask */
  1520. mvpp2_prs_tcam_port_set(&pe, port, add);
  1521. mvpp2_prs_hw_write(priv, &pe);
  1522. }
  1523. /* Set port to promiscuous mode */
  1524. static void mvpp2_prs_mac_promisc_set(struct mvpp2 *priv, int port, bool add)
  1525. {
  1526. struct mvpp2_prs_entry pe;
  1527. /* Promiscuous mode - Accept unknown packets */
  1528. if (priv->prs_shadow[MVPP2_PE_MAC_PROMISCUOUS].valid) {
  1529. /* Entry exist - update port only */
  1530. pe.index = MVPP2_PE_MAC_PROMISCUOUS;
  1531. mvpp2_prs_hw_read(priv, &pe);
  1532. } else {
  1533. /* Entry doesn't exist - create new */
  1534. memset(&pe, 0, sizeof(struct mvpp2_prs_entry));
  1535. mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_MAC);
  1536. pe.index = MVPP2_PE_MAC_PROMISCUOUS;
  1537. /* Continue - set next lookup */
  1538. mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_DSA);
  1539. /* Set result info bits */
  1540. mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L2_UCAST,
  1541. MVPP2_PRS_RI_L2_CAST_MASK);
  1542. /* Shift to ethertype */
  1543. mvpp2_prs_sram_shift_set(&pe, 2 * ETH_ALEN,
  1544. MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
  1545. /* Mask all ports */
  1546. mvpp2_prs_tcam_port_map_set(&pe, 0);
  1547. /* Update shadow table */
  1548. mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_MAC);
  1549. }
  1550. /* Update port mask */
  1551. mvpp2_prs_tcam_port_set(&pe, port, add);
  1552. mvpp2_prs_hw_write(priv, &pe);
  1553. }
  1554. /* Accept multicast */
  1555. static void mvpp2_prs_mac_multi_set(struct mvpp2 *priv, int port, int index,
  1556. bool add)
  1557. {
  1558. struct mvpp2_prs_entry pe;
  1559. unsigned char da_mc;
  1560. /* Ethernet multicast address first byte is
  1561. * 0x01 for IPv4 and 0x33 for IPv6
  1562. */
  1563. da_mc = (index == MVPP2_PE_MAC_MC_ALL) ? 0x01 : 0x33;
  1564. if (priv->prs_shadow[index].valid) {
  1565. /* Entry exist - update port only */
  1566. pe.index = index;
  1567. mvpp2_prs_hw_read(priv, &pe);
  1568. } else {
  1569. /* Entry doesn't exist - create new */
  1570. memset(&pe, 0, sizeof(struct mvpp2_prs_entry));
  1571. mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_MAC);
  1572. pe.index = index;
  1573. /* Continue - set next lookup */
  1574. mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_DSA);
  1575. /* Set result info bits */
  1576. mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L2_MCAST,
  1577. MVPP2_PRS_RI_L2_CAST_MASK);
  1578. /* Update tcam entry data first byte */
  1579. mvpp2_prs_tcam_data_byte_set(&pe, 0, da_mc, 0xff);
  1580. /* Shift to ethertype */
  1581. mvpp2_prs_sram_shift_set(&pe, 2 * ETH_ALEN,
  1582. MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
  1583. /* Mask all ports */
  1584. mvpp2_prs_tcam_port_map_set(&pe, 0);
  1585. /* Update shadow table */
  1586. mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_MAC);
  1587. }
  1588. /* Update port mask */
  1589. mvpp2_prs_tcam_port_set(&pe, port, add);
  1590. mvpp2_prs_hw_write(priv, &pe);
  1591. }
  1592. /* Parser per-port initialization */
  1593. static void mvpp2_prs_hw_port_init(struct mvpp2 *priv, int port, int lu_first,
  1594. int lu_max, int offset)
  1595. {
  1596. u32 val;
  1597. /* Set lookup ID */
  1598. val = mvpp2_read(priv, MVPP2_PRS_INIT_LOOKUP_REG);
  1599. val &= ~MVPP2_PRS_PORT_LU_MASK(port);
  1600. val |= MVPP2_PRS_PORT_LU_VAL(port, lu_first);
  1601. mvpp2_write(priv, MVPP2_PRS_INIT_LOOKUP_REG, val);
  1602. /* Set maximum number of loops for packet received from port */
  1603. val = mvpp2_read(priv, MVPP2_PRS_MAX_LOOP_REG(port));
  1604. val &= ~MVPP2_PRS_MAX_LOOP_MASK(port);
  1605. val |= MVPP2_PRS_MAX_LOOP_VAL(port, lu_max);
  1606. mvpp2_write(priv, MVPP2_PRS_MAX_LOOP_REG(port), val);
  1607. /* Set initial offset for packet header extraction for the first
  1608. * searching loop
  1609. */
  1610. val = mvpp2_read(priv, MVPP2_PRS_INIT_OFFS_REG(port));
  1611. val &= ~MVPP2_PRS_INIT_OFF_MASK(port);
  1612. val |= MVPP2_PRS_INIT_OFF_VAL(port, offset);
  1613. mvpp2_write(priv, MVPP2_PRS_INIT_OFFS_REG(port), val);
  1614. }
  1615. /* Default flow entries initialization for all ports */
  1616. static void mvpp2_prs_def_flow_init(struct mvpp2 *priv)
  1617. {
  1618. struct mvpp2_prs_entry pe;
  1619. int port;
  1620. for (port = 0; port < MVPP2_MAX_PORTS; port++) {
  1621. memset(&pe, 0, sizeof(struct mvpp2_prs_entry));
  1622. mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_FLOWS);
  1623. pe.index = MVPP2_PE_FIRST_DEFAULT_FLOW - port;
  1624. /* Mask all ports */
  1625. mvpp2_prs_tcam_port_map_set(&pe, 0);
  1626. /* Set flow ID*/
  1627. mvpp2_prs_sram_ai_update(&pe, port, MVPP2_PRS_FLOW_ID_MASK);
  1628. mvpp2_prs_sram_bits_set(&pe, MVPP2_PRS_SRAM_LU_DONE_BIT, 1);
  1629. /* Update shadow table and hw entry */
  1630. mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_FLOWS);
  1631. mvpp2_prs_hw_write(priv, &pe);
  1632. }
  1633. }
  1634. /* Set default entry for Marvell Header field */
  1635. static void mvpp2_prs_mh_init(struct mvpp2 *priv)
  1636. {
  1637. struct mvpp2_prs_entry pe;
  1638. memset(&pe, 0, sizeof(struct mvpp2_prs_entry));
  1639. pe.index = MVPP2_PE_MH_DEFAULT;
  1640. mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_MH);
  1641. mvpp2_prs_sram_shift_set(&pe, MVPP2_MH_SIZE,
  1642. MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
  1643. mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_MAC);
  1644. /* Unmask all ports */
  1645. mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
  1646. /* Update shadow table and hw entry */
  1647. mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_MH);
  1648. mvpp2_prs_hw_write(priv, &pe);
  1649. }
  1650. /* Set default entires (place holder) for promiscuous, non-promiscuous and
  1651. * multicast MAC addresses
  1652. */
  1653. static void mvpp2_prs_mac_init(struct mvpp2 *priv)
  1654. {
  1655. struct mvpp2_prs_entry pe;
  1656. memset(&pe, 0, sizeof(struct mvpp2_prs_entry));
  1657. /* Non-promiscuous mode for all ports - DROP unknown packets */
  1658. pe.index = MVPP2_PE_MAC_NON_PROMISCUOUS;
  1659. mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_MAC);
  1660. mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_DROP_MASK,
  1661. MVPP2_PRS_RI_DROP_MASK);
  1662. mvpp2_prs_sram_bits_set(&pe, MVPP2_PRS_SRAM_LU_GEN_BIT, 1);
  1663. mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_FLOWS);
  1664. /* Unmask all ports */
  1665. mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
  1666. /* Update shadow table and hw entry */
  1667. mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_MAC);
  1668. mvpp2_prs_hw_write(priv, &pe);
  1669. /* place holders only - no ports */
  1670. mvpp2_prs_mac_drop_all_set(priv, 0, false);
  1671. mvpp2_prs_mac_promisc_set(priv, 0, false);
  1672. mvpp2_prs_mac_multi_set(priv, MVPP2_PE_MAC_MC_ALL, 0, false);
  1673. mvpp2_prs_mac_multi_set(priv, MVPP2_PE_MAC_MC_IP6, 0, false);
  1674. }
  1675. /* Match basic ethertypes */
  1676. static int mvpp2_prs_etype_init(struct mvpp2 *priv)
  1677. {
  1678. struct mvpp2_prs_entry pe;
  1679. int tid;
  1680. /* Ethertype: PPPoE */
  1681. tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
  1682. MVPP2_PE_LAST_FREE_TID);
  1683. if (tid < 0)
  1684. return tid;
  1685. memset(&pe, 0, sizeof(struct mvpp2_prs_entry));
  1686. mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_L2);
  1687. pe.index = tid;
  1688. mvpp2_prs_match_etype(&pe, 0, PROT_PPP_SES);
  1689. mvpp2_prs_sram_shift_set(&pe, MVPP2_PPPOE_HDR_SIZE,
  1690. MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
  1691. mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_PPPOE);
  1692. mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_PPPOE_MASK,
  1693. MVPP2_PRS_RI_PPPOE_MASK);
  1694. /* Update shadow table and hw entry */
  1695. mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_L2);
  1696. priv->prs_shadow[pe.index].udf = MVPP2_PRS_UDF_L2_DEF;
  1697. priv->prs_shadow[pe.index].finish = false;
  1698. mvpp2_prs_shadow_ri_set(priv, pe.index, MVPP2_PRS_RI_PPPOE_MASK,
  1699. MVPP2_PRS_RI_PPPOE_MASK);
  1700. mvpp2_prs_hw_write(priv, &pe);
  1701. /* Ethertype: ARP */
  1702. tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
  1703. MVPP2_PE_LAST_FREE_TID);
  1704. if (tid < 0)
  1705. return tid;
  1706. memset(&pe, 0, sizeof(struct mvpp2_prs_entry));
  1707. mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_L2);
  1708. pe.index = tid;
  1709. mvpp2_prs_match_etype(&pe, 0, PROT_ARP);
  1710. /* Generate flow in the next iteration*/
  1711. mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_FLOWS);
  1712. mvpp2_prs_sram_bits_set(&pe, MVPP2_PRS_SRAM_LU_GEN_BIT, 1);
  1713. mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_ARP,
  1714. MVPP2_PRS_RI_L3_PROTO_MASK);
  1715. /* Set L3 offset */
  1716. mvpp2_prs_sram_offset_set(&pe, MVPP2_PRS_SRAM_UDF_TYPE_L3,
  1717. MVPP2_ETH_TYPE_LEN,
  1718. MVPP2_PRS_SRAM_OP_SEL_UDF_ADD);
  1719. /* Update shadow table and hw entry */
  1720. mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_L2);
  1721. priv->prs_shadow[pe.index].udf = MVPP2_PRS_UDF_L2_DEF;
  1722. priv->prs_shadow[pe.index].finish = true;
  1723. mvpp2_prs_shadow_ri_set(priv, pe.index, MVPP2_PRS_RI_L3_ARP,
  1724. MVPP2_PRS_RI_L3_PROTO_MASK);
  1725. mvpp2_prs_hw_write(priv, &pe);
  1726. /* Ethertype: LBTD */
  1727. tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
  1728. MVPP2_PE_LAST_FREE_TID);
  1729. if (tid < 0)
  1730. return tid;
  1731. memset(&pe, 0, sizeof(struct mvpp2_prs_entry));
  1732. mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_L2);
  1733. pe.index = tid;
  1734. mvpp2_prs_match_etype(&pe, 0, MVPP2_IP_LBDT_TYPE);
  1735. /* Generate flow in the next iteration*/
  1736. mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_FLOWS);
  1737. mvpp2_prs_sram_bits_set(&pe, MVPP2_PRS_SRAM_LU_GEN_BIT, 1);
  1738. mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_CPU_CODE_RX_SPEC |
  1739. MVPP2_PRS_RI_UDF3_RX_SPECIAL,
  1740. MVPP2_PRS_RI_CPU_CODE_MASK |
  1741. MVPP2_PRS_RI_UDF3_MASK);
  1742. /* Set L3 offset */
  1743. mvpp2_prs_sram_offset_set(&pe, MVPP2_PRS_SRAM_UDF_TYPE_L3,
  1744. MVPP2_ETH_TYPE_LEN,
  1745. MVPP2_PRS_SRAM_OP_SEL_UDF_ADD);
  1746. /* Update shadow table and hw entry */
  1747. mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_L2);
  1748. priv->prs_shadow[pe.index].udf = MVPP2_PRS_UDF_L2_DEF;
  1749. priv->prs_shadow[pe.index].finish = true;
  1750. mvpp2_prs_shadow_ri_set(priv, pe.index, MVPP2_PRS_RI_CPU_CODE_RX_SPEC |
  1751. MVPP2_PRS_RI_UDF3_RX_SPECIAL,
  1752. MVPP2_PRS_RI_CPU_CODE_MASK |
  1753. MVPP2_PRS_RI_UDF3_MASK);
  1754. mvpp2_prs_hw_write(priv, &pe);
  1755. /* Ethertype: IPv4 without options */
  1756. tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
  1757. MVPP2_PE_LAST_FREE_TID);
  1758. if (tid < 0)
  1759. return tid;
  1760. memset(&pe, 0, sizeof(struct mvpp2_prs_entry));
  1761. mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_L2);
  1762. pe.index = tid;
  1763. mvpp2_prs_match_etype(&pe, 0, PROT_IP);
  1764. mvpp2_prs_tcam_data_byte_set(&pe, MVPP2_ETH_TYPE_LEN,
  1765. MVPP2_PRS_IPV4_HEAD | MVPP2_PRS_IPV4_IHL,
  1766. MVPP2_PRS_IPV4_HEAD_MASK |
  1767. MVPP2_PRS_IPV4_IHL_MASK);
  1768. mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_IP4);
  1769. mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_IP4,
  1770. MVPP2_PRS_RI_L3_PROTO_MASK);
  1771. /* Skip eth_type + 4 bytes of IP header */
  1772. mvpp2_prs_sram_shift_set(&pe, MVPP2_ETH_TYPE_LEN + 4,
  1773. MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
  1774. /* Set L3 offset */
  1775. mvpp2_prs_sram_offset_set(&pe, MVPP2_PRS_SRAM_UDF_TYPE_L3,
  1776. MVPP2_ETH_TYPE_LEN,
  1777. MVPP2_PRS_SRAM_OP_SEL_UDF_ADD);
  1778. /* Update shadow table and hw entry */
  1779. mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_L2);
  1780. priv->prs_shadow[pe.index].udf = MVPP2_PRS_UDF_L2_DEF;
  1781. priv->prs_shadow[pe.index].finish = false;
  1782. mvpp2_prs_shadow_ri_set(priv, pe.index, MVPP2_PRS_RI_L3_IP4,
  1783. MVPP2_PRS_RI_L3_PROTO_MASK);
  1784. mvpp2_prs_hw_write(priv, &pe);
  1785. /* Ethertype: IPv4 with options */
  1786. tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
  1787. MVPP2_PE_LAST_FREE_TID);
  1788. if (tid < 0)
  1789. return tid;
  1790. pe.index = tid;
  1791. /* Clear tcam data before updating */
  1792. pe.tcam.byte[MVPP2_PRS_TCAM_DATA_BYTE(MVPP2_ETH_TYPE_LEN)] = 0x0;
  1793. pe.tcam.byte[MVPP2_PRS_TCAM_DATA_BYTE_EN(MVPP2_ETH_TYPE_LEN)] = 0x0;
  1794. mvpp2_prs_tcam_data_byte_set(&pe, MVPP2_ETH_TYPE_LEN,
  1795. MVPP2_PRS_IPV4_HEAD,
  1796. MVPP2_PRS_IPV4_HEAD_MASK);
  1797. /* Clear ri before updating */
  1798. pe.sram.word[MVPP2_PRS_SRAM_RI_WORD] = 0x0;
  1799. pe.sram.word[MVPP2_PRS_SRAM_RI_CTRL_WORD] = 0x0;
  1800. mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_IP4_OPT,
  1801. MVPP2_PRS_RI_L3_PROTO_MASK);
  1802. /* Update shadow table and hw entry */
  1803. mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_L2);
  1804. priv->prs_shadow[pe.index].udf = MVPP2_PRS_UDF_L2_DEF;
  1805. priv->prs_shadow[pe.index].finish = false;
  1806. mvpp2_prs_shadow_ri_set(priv, pe.index, MVPP2_PRS_RI_L3_IP4_OPT,
  1807. MVPP2_PRS_RI_L3_PROTO_MASK);
  1808. mvpp2_prs_hw_write(priv, &pe);
  1809. /* Ethertype: IPv6 without options */
  1810. tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
  1811. MVPP2_PE_LAST_FREE_TID);
  1812. if (tid < 0)
  1813. return tid;
  1814. memset(&pe, 0, sizeof(struct mvpp2_prs_entry));
  1815. mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_L2);
  1816. pe.index = tid;
  1817. mvpp2_prs_match_etype(&pe, 0, PROT_IPV6);
  1818. /* Skip DIP of IPV6 header */
  1819. mvpp2_prs_sram_shift_set(&pe, MVPP2_ETH_TYPE_LEN + 8 +
  1820. MVPP2_MAX_L3_ADDR_SIZE,
  1821. MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
  1822. mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_IP6);
  1823. mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_IP6,
  1824. MVPP2_PRS_RI_L3_PROTO_MASK);
  1825. /* Set L3 offset */
  1826. mvpp2_prs_sram_offset_set(&pe, MVPP2_PRS_SRAM_UDF_TYPE_L3,
  1827. MVPP2_ETH_TYPE_LEN,
  1828. MVPP2_PRS_SRAM_OP_SEL_UDF_ADD);
  1829. mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_L2);
  1830. priv->prs_shadow[pe.index].udf = MVPP2_PRS_UDF_L2_DEF;
  1831. priv->prs_shadow[pe.index].finish = false;
  1832. mvpp2_prs_shadow_ri_set(priv, pe.index, MVPP2_PRS_RI_L3_IP6,
  1833. MVPP2_PRS_RI_L3_PROTO_MASK);
  1834. mvpp2_prs_hw_write(priv, &pe);
  1835. /* Default entry for MVPP2_PRS_LU_L2 - Unknown ethtype */
  1836. memset(&pe, 0, sizeof(struct mvpp2_prs_entry));
  1837. mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_L2);
  1838. pe.index = MVPP2_PE_ETH_TYPE_UN;
  1839. /* Unmask all ports */
  1840. mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
  1841. /* Generate flow in the next iteration*/
  1842. mvpp2_prs_sram_bits_set(&pe, MVPP2_PRS_SRAM_LU_GEN_BIT, 1);
  1843. mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_FLOWS);
  1844. mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_UN,
  1845. MVPP2_PRS_RI_L3_PROTO_MASK);
  1846. /* Set L3 offset even it's unknown L3 */
  1847. mvpp2_prs_sram_offset_set(&pe, MVPP2_PRS_SRAM_UDF_TYPE_L3,
  1848. MVPP2_ETH_TYPE_LEN,
  1849. MVPP2_PRS_SRAM_OP_SEL_UDF_ADD);
  1850. /* Update shadow table and hw entry */
  1851. mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_L2);
  1852. priv->prs_shadow[pe.index].udf = MVPP2_PRS_UDF_L2_DEF;
  1853. priv->prs_shadow[pe.index].finish = true;
  1854. mvpp2_prs_shadow_ri_set(priv, pe.index, MVPP2_PRS_RI_L3_UN,
  1855. MVPP2_PRS_RI_L3_PROTO_MASK);
  1856. mvpp2_prs_hw_write(priv, &pe);
  1857. return 0;
  1858. }
  1859. /* Parser default initialization */
  1860. static int mvpp2_prs_default_init(struct udevice *dev,
  1861. struct mvpp2 *priv)
  1862. {
  1863. int err, index, i;
  1864. /* Enable tcam table */
  1865. mvpp2_write(priv, MVPP2_PRS_TCAM_CTRL_REG, MVPP2_PRS_TCAM_EN_MASK);
  1866. /* Clear all tcam and sram entries */
  1867. for (index = 0; index < MVPP2_PRS_TCAM_SRAM_SIZE; index++) {
  1868. mvpp2_write(priv, MVPP2_PRS_TCAM_IDX_REG, index);
  1869. for (i = 0; i < MVPP2_PRS_TCAM_WORDS; i++)
  1870. mvpp2_write(priv, MVPP2_PRS_TCAM_DATA_REG(i), 0);
  1871. mvpp2_write(priv, MVPP2_PRS_SRAM_IDX_REG, index);
  1872. for (i = 0; i < MVPP2_PRS_SRAM_WORDS; i++)
  1873. mvpp2_write(priv, MVPP2_PRS_SRAM_DATA_REG(i), 0);
  1874. }
  1875. /* Invalidate all tcam entries */
  1876. for (index = 0; index < MVPP2_PRS_TCAM_SRAM_SIZE; index++)
  1877. mvpp2_prs_hw_inv(priv, index);
  1878. priv->prs_shadow = devm_kcalloc(dev, MVPP2_PRS_TCAM_SRAM_SIZE,
  1879. sizeof(struct mvpp2_prs_shadow),
  1880. GFP_KERNEL);
  1881. if (!priv->prs_shadow)
  1882. return -ENOMEM;
  1883. /* Always start from lookup = 0 */
  1884. for (index = 0; index < MVPP2_MAX_PORTS; index++)
  1885. mvpp2_prs_hw_port_init(priv, index, MVPP2_PRS_LU_MH,
  1886. MVPP2_PRS_PORT_LU_MAX, 0);
  1887. mvpp2_prs_def_flow_init(priv);
  1888. mvpp2_prs_mh_init(priv);
  1889. mvpp2_prs_mac_init(priv);
  1890. err = mvpp2_prs_etype_init(priv);
  1891. if (err)
  1892. return err;
  1893. return 0;
  1894. }
  1895. /* Compare MAC DA with tcam entry data */
  1896. static bool mvpp2_prs_mac_range_equals(struct mvpp2_prs_entry *pe,
  1897. const u8 *da, unsigned char *mask)
  1898. {
  1899. unsigned char tcam_byte, tcam_mask;
  1900. int index;
  1901. for (index = 0; index < ETH_ALEN; index++) {
  1902. mvpp2_prs_tcam_data_byte_get(pe, index, &tcam_byte, &tcam_mask);
  1903. if (tcam_mask != mask[index])
  1904. return false;
  1905. if ((tcam_mask & tcam_byte) != (da[index] & mask[index]))
  1906. return false;
  1907. }
  1908. return true;
  1909. }
  1910. /* Find tcam entry with matched pair <MAC DA, port> */
  1911. static struct mvpp2_prs_entry *
  1912. mvpp2_prs_mac_da_range_find(struct mvpp2 *priv, int pmap, const u8 *da,
  1913. unsigned char *mask, int udf_type)
  1914. {
  1915. struct mvpp2_prs_entry *pe;
  1916. int tid;
  1917. pe = kzalloc(sizeof(*pe), GFP_KERNEL);
  1918. if (!pe)
  1919. return NULL;
  1920. mvpp2_prs_tcam_lu_set(pe, MVPP2_PRS_LU_MAC);
  1921. /* Go through the all entires with MVPP2_PRS_LU_MAC */
  1922. for (tid = MVPP2_PE_FIRST_FREE_TID;
  1923. tid <= MVPP2_PE_LAST_FREE_TID; tid++) {
  1924. unsigned int entry_pmap;
  1925. if (!priv->prs_shadow[tid].valid ||
  1926. (priv->prs_shadow[tid].lu != MVPP2_PRS_LU_MAC) ||
  1927. (priv->prs_shadow[tid].udf != udf_type))
  1928. continue;
  1929. pe->index = tid;
  1930. mvpp2_prs_hw_read(priv, pe);
  1931. entry_pmap = mvpp2_prs_tcam_port_map_get(pe);
  1932. if (mvpp2_prs_mac_range_equals(pe, da, mask) &&
  1933. entry_pmap == pmap)
  1934. return pe;
  1935. }
  1936. kfree(pe);
  1937. return NULL;
  1938. }
  1939. /* Update parser's mac da entry */
  1940. static int mvpp2_prs_mac_da_accept(struct mvpp2 *priv, int port,
  1941. const u8 *da, bool add)
  1942. {
  1943. struct mvpp2_prs_entry *pe;
  1944. unsigned int pmap, len, ri;
  1945. unsigned char mask[ETH_ALEN] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
  1946. int tid;
  1947. /* Scan TCAM and see if entry with this <MAC DA, port> already exist */
  1948. pe = mvpp2_prs_mac_da_range_find(priv, (1 << port), da, mask,
  1949. MVPP2_PRS_UDF_MAC_DEF);
  1950. /* No such entry */
  1951. if (!pe) {
  1952. if (!add)
  1953. return 0;
  1954. /* Create new TCAM entry */
  1955. /* Find first range mac entry*/
  1956. for (tid = MVPP2_PE_FIRST_FREE_TID;
  1957. tid <= MVPP2_PE_LAST_FREE_TID; tid++)
  1958. if (priv->prs_shadow[tid].valid &&
  1959. (priv->prs_shadow[tid].lu == MVPP2_PRS_LU_MAC) &&
  1960. (priv->prs_shadow[tid].udf ==
  1961. MVPP2_PRS_UDF_MAC_RANGE))
  1962. break;
  1963. /* Go through the all entries from first to last */
  1964. tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
  1965. tid - 1);
  1966. if (tid < 0)
  1967. return tid;
  1968. pe = kzalloc(sizeof(*pe), GFP_KERNEL);
  1969. if (!pe)
  1970. return -1;
  1971. mvpp2_prs_tcam_lu_set(pe, MVPP2_PRS_LU_MAC);
  1972. pe->index = tid;
  1973. /* Mask all ports */
  1974. mvpp2_prs_tcam_port_map_set(pe, 0);
  1975. }
  1976. /* Update port mask */
  1977. mvpp2_prs_tcam_port_set(pe, port, add);
  1978. /* Invalidate the entry if no ports are left enabled */
  1979. pmap = mvpp2_prs_tcam_port_map_get(pe);
  1980. if (pmap == 0) {
  1981. if (add) {
  1982. kfree(pe);
  1983. return -1;
  1984. }
  1985. mvpp2_prs_hw_inv(priv, pe->index);
  1986. priv->prs_shadow[pe->index].valid = false;
  1987. kfree(pe);
  1988. return 0;
  1989. }
  1990. /* Continue - set next lookup */
  1991. mvpp2_prs_sram_next_lu_set(pe, MVPP2_PRS_LU_DSA);
  1992. /* Set match on DA */
  1993. len = ETH_ALEN;
  1994. while (len--)
  1995. mvpp2_prs_tcam_data_byte_set(pe, len, da[len], 0xff);
  1996. /* Set result info bits */
  1997. ri = MVPP2_PRS_RI_L2_UCAST | MVPP2_PRS_RI_MAC_ME_MASK;
  1998. mvpp2_prs_sram_ri_update(pe, ri, MVPP2_PRS_RI_L2_CAST_MASK |
  1999. MVPP2_PRS_RI_MAC_ME_MASK);
  2000. mvpp2_prs_shadow_ri_set(priv, pe->index, ri, MVPP2_PRS_RI_L2_CAST_MASK |
  2001. MVPP2_PRS_RI_MAC_ME_MASK);
  2002. /* Shift to ethertype */
  2003. mvpp2_prs_sram_shift_set(pe, 2 * ETH_ALEN,
  2004. MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
  2005. /* Update shadow table and hw entry */
  2006. priv->prs_shadow[pe->index].udf = MVPP2_PRS_UDF_MAC_DEF;
  2007. mvpp2_prs_shadow_set(priv, pe->index, MVPP2_PRS_LU_MAC);
  2008. mvpp2_prs_hw_write(priv, pe);
  2009. kfree(pe);
  2010. return 0;
  2011. }
  2012. static int mvpp2_prs_update_mac_da(struct mvpp2_port *port, const u8 *da)
  2013. {
  2014. int err;
  2015. /* Remove old parser entry */
  2016. err = mvpp2_prs_mac_da_accept(port->priv, port->id, port->dev_addr,
  2017. false);
  2018. if (err)
  2019. return err;
  2020. /* Add new parser entry */
  2021. err = mvpp2_prs_mac_da_accept(port->priv, port->id, da, true);
  2022. if (err)
  2023. return err;
  2024. /* Set addr in the device */
  2025. memcpy(port->dev_addr, da, ETH_ALEN);
  2026. return 0;
  2027. }
  2028. /* Set prs flow for the port */
  2029. static int mvpp2_prs_def_flow(struct mvpp2_port *port)
  2030. {
  2031. struct mvpp2_prs_entry *pe;
  2032. int tid;
  2033. pe = mvpp2_prs_flow_find(port->priv, port->id);
  2034. /* Such entry not exist */
  2035. if (!pe) {
  2036. /* Go through the all entires from last to first */
  2037. tid = mvpp2_prs_tcam_first_free(port->priv,
  2038. MVPP2_PE_LAST_FREE_TID,
  2039. MVPP2_PE_FIRST_FREE_TID);
  2040. if (tid < 0)
  2041. return tid;
  2042. pe = kzalloc(sizeof(*pe), GFP_KERNEL);
  2043. if (!pe)
  2044. return -ENOMEM;
  2045. mvpp2_prs_tcam_lu_set(pe, MVPP2_PRS_LU_FLOWS);
  2046. pe->index = tid;
  2047. /* Set flow ID*/
  2048. mvpp2_prs_sram_ai_update(pe, port->id, MVPP2_PRS_FLOW_ID_MASK);
  2049. mvpp2_prs_sram_bits_set(pe, MVPP2_PRS_SRAM_LU_DONE_BIT, 1);
  2050. /* Update shadow table */
  2051. mvpp2_prs_shadow_set(port->priv, pe->index, MVPP2_PRS_LU_FLOWS);
  2052. }
  2053. mvpp2_prs_tcam_port_map_set(pe, (1 << port->id));
  2054. mvpp2_prs_hw_write(port->priv, pe);
  2055. kfree(pe);
  2056. return 0;
  2057. }
  2058. /* Classifier configuration routines */
  2059. /* Update classification flow table registers */
  2060. static void mvpp2_cls_flow_write(struct mvpp2 *priv,
  2061. struct mvpp2_cls_flow_entry *fe)
  2062. {
  2063. mvpp2_write(priv, MVPP2_CLS_FLOW_INDEX_REG, fe->index);
  2064. mvpp2_write(priv, MVPP2_CLS_FLOW_TBL0_REG, fe->data[0]);
  2065. mvpp2_write(priv, MVPP2_CLS_FLOW_TBL1_REG, fe->data[1]);
  2066. mvpp2_write(priv, MVPP2_CLS_FLOW_TBL2_REG, fe->data[2]);
  2067. }
  2068. /* Update classification lookup table register */
  2069. static void mvpp2_cls_lookup_write(struct mvpp2 *priv,
  2070. struct mvpp2_cls_lookup_entry *le)
  2071. {
  2072. u32 val;
  2073. val = (le->way << MVPP2_CLS_LKP_INDEX_WAY_OFFS) | le->lkpid;
  2074. mvpp2_write(priv, MVPP2_CLS_LKP_INDEX_REG, val);
  2075. mvpp2_write(priv, MVPP2_CLS_LKP_TBL_REG, le->data);
  2076. }
  2077. /* Classifier default initialization */
  2078. static void mvpp2_cls_init(struct mvpp2 *priv)
  2079. {
  2080. struct mvpp2_cls_lookup_entry le;
  2081. struct mvpp2_cls_flow_entry fe;
  2082. int index;
  2083. /* Enable classifier */
  2084. mvpp2_write(priv, MVPP2_CLS_MODE_REG, MVPP2_CLS_MODE_ACTIVE_MASK);
  2085. /* Clear classifier flow table */
  2086. memset(&fe.data, 0, MVPP2_CLS_FLOWS_TBL_DATA_WORDS);
  2087. for (index = 0; index < MVPP2_CLS_FLOWS_TBL_SIZE; index++) {
  2088. fe.index = index;
  2089. mvpp2_cls_flow_write(priv, &fe);
  2090. }
  2091. /* Clear classifier lookup table */
  2092. le.data = 0;
  2093. for (index = 0; index < MVPP2_CLS_LKP_TBL_SIZE; index++) {
  2094. le.lkpid = index;
  2095. le.way = 0;
  2096. mvpp2_cls_lookup_write(priv, &le);
  2097. le.way = 1;
  2098. mvpp2_cls_lookup_write(priv, &le);
  2099. }
  2100. }
  2101. static void mvpp2_cls_port_config(struct mvpp2_port *port)
  2102. {
  2103. struct mvpp2_cls_lookup_entry le;
  2104. u32 val;
  2105. /* Set way for the port */
  2106. val = mvpp2_read(port->priv, MVPP2_CLS_PORT_WAY_REG);
  2107. val &= ~MVPP2_CLS_PORT_WAY_MASK(port->id);
  2108. mvpp2_write(port->priv, MVPP2_CLS_PORT_WAY_REG, val);
  2109. /* Pick the entry to be accessed in lookup ID decoding table
  2110. * according to the way and lkpid.
  2111. */
  2112. le.lkpid = port->id;
  2113. le.way = 0;
  2114. le.data = 0;
  2115. /* Set initial CPU queue for receiving packets */
  2116. le.data &= ~MVPP2_CLS_LKP_TBL_RXQ_MASK;
  2117. le.data |= port->first_rxq;
  2118. /* Disable classification engines */
  2119. le.data &= ~MVPP2_CLS_LKP_TBL_LOOKUP_EN_MASK;
  2120. /* Update lookup ID table entry */
  2121. mvpp2_cls_lookup_write(port->priv, &le);
  2122. }
  2123. /* Set CPU queue number for oversize packets */
  2124. static void mvpp2_cls_oversize_rxq_set(struct mvpp2_port *port)
  2125. {
  2126. u32 val;
  2127. mvpp2_write(port->priv, MVPP2_CLS_OVERSIZE_RXQ_LOW_REG(port->id),
  2128. port->first_rxq & MVPP2_CLS_OVERSIZE_RXQ_LOW_MASK);
  2129. mvpp2_write(port->priv, MVPP2_CLS_SWFWD_P2HQ_REG(port->id),
  2130. (port->first_rxq >> MVPP2_CLS_OVERSIZE_RXQ_LOW_BITS));
  2131. val = mvpp2_read(port->priv, MVPP2_CLS_SWFWD_PCTRL_REG);
  2132. val |= MVPP2_CLS_SWFWD_PCTRL_MASK(port->id);
  2133. mvpp2_write(port->priv, MVPP2_CLS_SWFWD_PCTRL_REG, val);
  2134. }
  2135. /* Buffer Manager configuration routines */
  2136. /* Create pool */
  2137. static int mvpp2_bm_pool_create(struct udevice *dev,
  2138. struct mvpp2 *priv,
  2139. struct mvpp2_bm_pool *bm_pool, int size)
  2140. {
  2141. u32 val;
  2142. /* Number of buffer pointers must be a multiple of 16, as per
  2143. * hardware constraints
  2144. */
  2145. if (!IS_ALIGNED(size, 16))
  2146. return -EINVAL;
  2147. bm_pool->virt_addr = buffer_loc.bm_pool[bm_pool->id];
  2148. bm_pool->dma_addr = (dma_addr_t)buffer_loc.bm_pool[bm_pool->id];
  2149. if (!bm_pool->virt_addr)
  2150. return -ENOMEM;
  2151. if (!IS_ALIGNED((unsigned long)bm_pool->virt_addr,
  2152. MVPP2_BM_POOL_PTR_ALIGN)) {
  2153. dev_err(&pdev->dev, "BM pool %d is not %d bytes aligned\n",
  2154. bm_pool->id, MVPP2_BM_POOL_PTR_ALIGN);
  2155. return -ENOMEM;
  2156. }
  2157. mvpp2_write(priv, MVPP2_BM_POOL_BASE_REG(bm_pool->id),
  2158. lower_32_bits(bm_pool->dma_addr));
  2159. mvpp2_write(priv, MVPP2_BM_POOL_SIZE_REG(bm_pool->id), size);
  2160. val = mvpp2_read(priv, MVPP2_BM_POOL_CTRL_REG(bm_pool->id));
  2161. val |= MVPP2_BM_START_MASK;
  2162. mvpp2_write(priv, MVPP2_BM_POOL_CTRL_REG(bm_pool->id), val);
  2163. bm_pool->type = MVPP2_BM_FREE;
  2164. bm_pool->size = size;
  2165. bm_pool->pkt_size = 0;
  2166. bm_pool->buf_num = 0;
  2167. return 0;
  2168. }
  2169. /* Set pool buffer size */
  2170. static void mvpp2_bm_pool_bufsize_set(struct mvpp2 *priv,
  2171. struct mvpp2_bm_pool *bm_pool,
  2172. int buf_size)
  2173. {
  2174. u32 val;
  2175. bm_pool->buf_size = buf_size;
  2176. val = ALIGN(buf_size, 1 << MVPP2_POOL_BUF_SIZE_OFFSET);
  2177. mvpp2_write(priv, MVPP2_POOL_BUF_SIZE_REG(bm_pool->id), val);
  2178. }
  2179. /* Free all buffers from the pool */
  2180. static void mvpp2_bm_bufs_free(struct udevice *dev, struct mvpp2 *priv,
  2181. struct mvpp2_bm_pool *bm_pool)
  2182. {
  2183. bm_pool->buf_num = 0;
  2184. }
  2185. /* Cleanup pool */
  2186. static int mvpp2_bm_pool_destroy(struct udevice *dev,
  2187. struct mvpp2 *priv,
  2188. struct mvpp2_bm_pool *bm_pool)
  2189. {
  2190. u32 val;
  2191. mvpp2_bm_bufs_free(dev, priv, bm_pool);
  2192. if (bm_pool->buf_num) {
  2193. dev_err(dev, "cannot free all buffers in pool %d\n", bm_pool->id);
  2194. return 0;
  2195. }
  2196. val = mvpp2_read(priv, MVPP2_BM_POOL_CTRL_REG(bm_pool->id));
  2197. val |= MVPP2_BM_STOP_MASK;
  2198. mvpp2_write(priv, MVPP2_BM_POOL_CTRL_REG(bm_pool->id), val);
  2199. return 0;
  2200. }
  2201. static int mvpp2_bm_pools_init(struct udevice *dev,
  2202. struct mvpp2 *priv)
  2203. {
  2204. int i, err, size;
  2205. struct mvpp2_bm_pool *bm_pool;
  2206. /* Create all pools with maximum size */
  2207. size = MVPP2_BM_POOL_SIZE_MAX;
  2208. for (i = 0; i < MVPP2_BM_POOLS_NUM; i++) {
  2209. bm_pool = &priv->bm_pools[i];
  2210. bm_pool->id = i;
  2211. err = mvpp2_bm_pool_create(dev, priv, bm_pool, size);
  2212. if (err)
  2213. goto err_unroll_pools;
  2214. mvpp2_bm_pool_bufsize_set(priv, bm_pool, 0);
  2215. }
  2216. return 0;
  2217. err_unroll_pools:
  2218. dev_err(&pdev->dev, "failed to create BM pool %d, size %d\n", i, size);
  2219. for (i = i - 1; i >= 0; i--)
  2220. mvpp2_bm_pool_destroy(dev, priv, &priv->bm_pools[i]);
  2221. return err;
  2222. }
  2223. static int mvpp2_bm_init(struct udevice *dev, struct mvpp2 *priv)
  2224. {
  2225. int i, err;
  2226. for (i = 0; i < MVPP2_BM_POOLS_NUM; i++) {
  2227. /* Mask BM all interrupts */
  2228. mvpp2_write(priv, MVPP2_BM_INTR_MASK_REG(i), 0);
  2229. /* Clear BM cause register */
  2230. mvpp2_write(priv, MVPP2_BM_INTR_CAUSE_REG(i), 0);
  2231. }
  2232. /* Allocate and initialize BM pools */
  2233. priv->bm_pools = devm_kcalloc(dev, MVPP2_BM_POOLS_NUM,
  2234. sizeof(struct mvpp2_bm_pool), GFP_KERNEL);
  2235. if (!priv->bm_pools)
  2236. return -ENOMEM;
  2237. err = mvpp2_bm_pools_init(dev, priv);
  2238. if (err < 0)
  2239. return err;
  2240. return 0;
  2241. }
  2242. /* Attach long pool to rxq */
  2243. static void mvpp2_rxq_long_pool_set(struct mvpp2_port *port,
  2244. int lrxq, int long_pool)
  2245. {
  2246. u32 val, mask;
  2247. int prxq;
  2248. /* Get queue physical ID */
  2249. prxq = port->rxqs[lrxq]->id;
  2250. if (port->priv->hw_version == MVPP21)
  2251. mask = MVPP21_RXQ_POOL_LONG_MASK;
  2252. else
  2253. mask = MVPP22_RXQ_POOL_LONG_MASK;
  2254. val = mvpp2_read(port->priv, MVPP2_RXQ_CONFIG_REG(prxq));
  2255. val &= ~mask;
  2256. val |= (long_pool << MVPP2_RXQ_POOL_LONG_OFFS) & mask;
  2257. mvpp2_write(port->priv, MVPP2_RXQ_CONFIG_REG(prxq), val);
  2258. }
  2259. /* Set pool number in a BM cookie */
  2260. static inline u32 mvpp2_bm_cookie_pool_set(u32 cookie, int pool)
  2261. {
  2262. u32 bm;
  2263. bm = cookie & ~(0xFF << MVPP2_BM_COOKIE_POOL_OFFS);
  2264. bm |= ((pool & 0xFF) << MVPP2_BM_COOKIE_POOL_OFFS);
  2265. return bm;
  2266. }
  2267. /* Get pool number from a BM cookie */
  2268. static inline int mvpp2_bm_cookie_pool_get(unsigned long cookie)
  2269. {
  2270. return (cookie >> MVPP2_BM_COOKIE_POOL_OFFS) & 0xFF;
  2271. }
  2272. /* Release buffer to BM */
  2273. static inline void mvpp2_bm_pool_put(struct mvpp2_port *port, int pool,
  2274. dma_addr_t buf_dma_addr,
  2275. unsigned long buf_phys_addr)
  2276. {
  2277. if (port->priv->hw_version == MVPP22) {
  2278. u32 val = 0;
  2279. if (sizeof(dma_addr_t) == 8)
  2280. val |= upper_32_bits(buf_dma_addr) &
  2281. MVPP22_BM_ADDR_HIGH_PHYS_RLS_MASK;
  2282. if (sizeof(phys_addr_t) == 8)
  2283. val |= (upper_32_bits(buf_phys_addr)
  2284. << MVPP22_BM_ADDR_HIGH_VIRT_RLS_SHIFT) &
  2285. MVPP22_BM_ADDR_HIGH_VIRT_RLS_MASK;
  2286. mvpp2_write(port->priv, MVPP22_BM_ADDR_HIGH_RLS_REG, val);
  2287. }
  2288. /* MVPP2_BM_VIRT_RLS_REG is not interpreted by HW, and simply
  2289. * returned in the "cookie" field of the RX
  2290. * descriptor. Instead of storing the virtual address, we
  2291. * store the physical address
  2292. */
  2293. mvpp2_write(port->priv, MVPP2_BM_VIRT_RLS_REG, buf_phys_addr);
  2294. mvpp2_write(port->priv, MVPP2_BM_PHY_RLS_REG(pool), buf_dma_addr);
  2295. }
  2296. /* Refill BM pool */
  2297. static void mvpp2_pool_refill(struct mvpp2_port *port, u32 bm,
  2298. dma_addr_t dma_addr,
  2299. phys_addr_t phys_addr)
  2300. {
  2301. int pool = mvpp2_bm_cookie_pool_get(bm);
  2302. mvpp2_bm_pool_put(port, pool, dma_addr, phys_addr);
  2303. }
  2304. /* Allocate buffers for the pool */
  2305. static int mvpp2_bm_bufs_add(struct mvpp2_port *port,
  2306. struct mvpp2_bm_pool *bm_pool, int buf_num)
  2307. {
  2308. int i;
  2309. if (buf_num < 0 ||
  2310. (buf_num + bm_pool->buf_num > bm_pool->size)) {
  2311. netdev_err(port->dev,
  2312. "cannot allocate %d buffers for pool %d\n",
  2313. buf_num, bm_pool->id);
  2314. return 0;
  2315. }
  2316. for (i = 0; i < buf_num; i++) {
  2317. mvpp2_bm_pool_put(port, bm_pool->id,
  2318. (dma_addr_t)buffer_loc.rx_buffer[i],
  2319. (unsigned long)buffer_loc.rx_buffer[i]);
  2320. }
  2321. /* Update BM driver with number of buffers added to pool */
  2322. bm_pool->buf_num += i;
  2323. return i;
  2324. }
  2325. /* Notify the driver that BM pool is being used as specific type and return the
  2326. * pool pointer on success
  2327. */
  2328. static struct mvpp2_bm_pool *
  2329. mvpp2_bm_pool_use(struct mvpp2_port *port, int pool, enum mvpp2_bm_type type,
  2330. int pkt_size)
  2331. {
  2332. struct mvpp2_bm_pool *new_pool = &port->priv->bm_pools[pool];
  2333. int num;
  2334. if (new_pool->type != MVPP2_BM_FREE && new_pool->type != type) {
  2335. netdev_err(port->dev, "mixing pool types is forbidden\n");
  2336. return NULL;
  2337. }
  2338. if (new_pool->type == MVPP2_BM_FREE)
  2339. new_pool->type = type;
  2340. /* Allocate buffers in case BM pool is used as long pool, but packet
  2341. * size doesn't match MTU or BM pool hasn't being used yet
  2342. */
  2343. if (((type == MVPP2_BM_SWF_LONG) && (pkt_size > new_pool->pkt_size)) ||
  2344. (new_pool->pkt_size == 0)) {
  2345. int pkts_num;
  2346. /* Set default buffer number or free all the buffers in case
  2347. * the pool is not empty
  2348. */
  2349. pkts_num = new_pool->buf_num;
  2350. if (pkts_num == 0)
  2351. pkts_num = type == MVPP2_BM_SWF_LONG ?
  2352. MVPP2_BM_LONG_BUF_NUM :
  2353. MVPP2_BM_SHORT_BUF_NUM;
  2354. else
  2355. mvpp2_bm_bufs_free(NULL,
  2356. port->priv, new_pool);
  2357. new_pool->pkt_size = pkt_size;
  2358. /* Allocate buffers for this pool */
  2359. num = mvpp2_bm_bufs_add(port, new_pool, pkts_num);
  2360. if (num != pkts_num) {
  2361. dev_err(dev, "pool %d: %d of %d allocated\n",
  2362. new_pool->id, num, pkts_num);
  2363. return NULL;
  2364. }
  2365. }
  2366. mvpp2_bm_pool_bufsize_set(port->priv, new_pool,
  2367. MVPP2_RX_BUF_SIZE(new_pool->pkt_size));
  2368. return new_pool;
  2369. }
  2370. /* Initialize pools for swf */
  2371. static int mvpp2_swf_bm_pool_init(struct mvpp2_port *port)
  2372. {
  2373. int rxq;
  2374. if (!port->pool_long) {
  2375. port->pool_long =
  2376. mvpp2_bm_pool_use(port, MVPP2_BM_SWF_LONG_POOL(port->id),
  2377. MVPP2_BM_SWF_LONG,
  2378. port->pkt_size);
  2379. if (!port->pool_long)
  2380. return -ENOMEM;
  2381. port->pool_long->port_map |= (1 << port->id);
  2382. for (rxq = 0; rxq < rxq_number; rxq++)
  2383. mvpp2_rxq_long_pool_set(port, rxq, port->pool_long->id);
  2384. }
  2385. return 0;
  2386. }
  2387. /* Port configuration routines */
  2388. static void mvpp2_port_mii_set(struct mvpp2_port *port)
  2389. {
  2390. u32 val;
  2391. val = readl(port->base + MVPP2_GMAC_CTRL_2_REG);
  2392. switch (port->phy_interface) {
  2393. case PHY_INTERFACE_MODE_SGMII:
  2394. val |= MVPP2_GMAC_INBAND_AN_MASK;
  2395. break;
  2396. case PHY_INTERFACE_MODE_RGMII:
  2397. case PHY_INTERFACE_MODE_RGMII_ID:
  2398. val |= MVPP2_GMAC_PORT_RGMII_MASK;
  2399. default:
  2400. val &= ~MVPP2_GMAC_PCS_ENABLE_MASK;
  2401. }
  2402. writel(val, port->base + MVPP2_GMAC_CTRL_2_REG);
  2403. }
  2404. static void mvpp2_port_fc_adv_enable(struct mvpp2_port *port)
  2405. {
  2406. u32 val;
  2407. val = readl(port->base + MVPP2_GMAC_AUTONEG_CONFIG);
  2408. val |= MVPP2_GMAC_FC_ADV_EN;
  2409. writel(val, port->base + MVPP2_GMAC_AUTONEG_CONFIG);
  2410. }
  2411. static void mvpp2_port_enable(struct mvpp2_port *port)
  2412. {
  2413. u32 val;
  2414. val = readl(port->base + MVPP2_GMAC_CTRL_0_REG);
  2415. val |= MVPP2_GMAC_PORT_EN_MASK;
  2416. val |= MVPP2_GMAC_MIB_CNTR_EN_MASK;
  2417. writel(val, port->base + MVPP2_GMAC_CTRL_0_REG);
  2418. }
  2419. static void mvpp2_port_disable(struct mvpp2_port *port)
  2420. {
  2421. u32 val;
  2422. val = readl(port->base + MVPP2_GMAC_CTRL_0_REG);
  2423. val &= ~(MVPP2_GMAC_PORT_EN_MASK);
  2424. writel(val, port->base + MVPP2_GMAC_CTRL_0_REG);
  2425. }
  2426. /* Set IEEE 802.3x Flow Control Xon Packet Transmission Mode */
  2427. static void mvpp2_port_periodic_xon_disable(struct mvpp2_port *port)
  2428. {
  2429. u32 val;
  2430. val = readl(port->base + MVPP2_GMAC_CTRL_1_REG) &
  2431. ~MVPP2_GMAC_PERIODIC_XON_EN_MASK;
  2432. writel(val, port->base + MVPP2_GMAC_CTRL_1_REG);
  2433. }
  2434. /* Configure loopback port */
  2435. static void mvpp2_port_loopback_set(struct mvpp2_port *port)
  2436. {
  2437. u32 val;
  2438. val = readl(port->base + MVPP2_GMAC_CTRL_1_REG);
  2439. if (port->speed == 1000)
  2440. val |= MVPP2_GMAC_GMII_LB_EN_MASK;
  2441. else
  2442. val &= ~MVPP2_GMAC_GMII_LB_EN_MASK;
  2443. if (port->phy_interface == PHY_INTERFACE_MODE_SGMII)
  2444. val |= MVPP2_GMAC_PCS_LB_EN_MASK;
  2445. else
  2446. val &= ~MVPP2_GMAC_PCS_LB_EN_MASK;
  2447. writel(val, port->base + MVPP2_GMAC_CTRL_1_REG);
  2448. }
  2449. static void mvpp2_port_reset(struct mvpp2_port *port)
  2450. {
  2451. u32 val;
  2452. val = readl(port->base + MVPP2_GMAC_CTRL_2_REG) &
  2453. ~MVPP2_GMAC_PORT_RESET_MASK;
  2454. writel(val, port->base + MVPP2_GMAC_CTRL_2_REG);
  2455. while (readl(port->base + MVPP2_GMAC_CTRL_2_REG) &
  2456. MVPP2_GMAC_PORT_RESET_MASK)
  2457. continue;
  2458. }
  2459. /* Change maximum receive size of the port */
  2460. static inline void mvpp2_gmac_max_rx_size_set(struct mvpp2_port *port)
  2461. {
  2462. u32 val;
  2463. val = readl(port->base + MVPP2_GMAC_CTRL_0_REG);
  2464. val &= ~MVPP2_GMAC_MAX_RX_SIZE_MASK;
  2465. val |= (((port->pkt_size - MVPP2_MH_SIZE) / 2) <<
  2466. MVPP2_GMAC_MAX_RX_SIZE_OFFS);
  2467. writel(val, port->base + MVPP2_GMAC_CTRL_0_REG);
  2468. }
  2469. /* PPv2.2 GoP/GMAC config */
  2470. /* Set the MAC to reset or exit from reset */
  2471. static int gop_gmac_reset(struct mvpp2_port *port, int reset)
  2472. {
  2473. u32 val;
  2474. /* read - modify - write */
  2475. val = readl(port->base + MVPP2_GMAC_CTRL_2_REG);
  2476. if (reset)
  2477. val |= MVPP2_GMAC_PORT_RESET_MASK;
  2478. else
  2479. val &= ~MVPP2_GMAC_PORT_RESET_MASK;
  2480. writel(val, port->base + MVPP2_GMAC_CTRL_2_REG);
  2481. return 0;
  2482. }
  2483. /*
  2484. * gop_gpcs_mode_cfg
  2485. *
  2486. * Configure port to working with Gig PCS or don't.
  2487. */
  2488. static int gop_gpcs_mode_cfg(struct mvpp2_port *port, int en)
  2489. {
  2490. u32 val;
  2491. val = readl(port->base + MVPP2_GMAC_CTRL_2_REG);
  2492. if (en)
  2493. val |= MVPP2_GMAC_PCS_ENABLE_MASK;
  2494. else
  2495. val &= ~MVPP2_GMAC_PCS_ENABLE_MASK;
  2496. /* enable / disable PCS on this port */
  2497. writel(val, port->base + MVPP2_GMAC_CTRL_2_REG);
  2498. return 0;
  2499. }
  2500. static int gop_bypass_clk_cfg(struct mvpp2_port *port, int en)
  2501. {
  2502. u32 val;
  2503. val = readl(port->base + MVPP2_GMAC_CTRL_2_REG);
  2504. if (en)
  2505. val |= MVPP2_GMAC_CLK_125_BYPS_EN_MASK;
  2506. else
  2507. val &= ~MVPP2_GMAC_CLK_125_BYPS_EN_MASK;
  2508. /* enable / disable PCS on this port */
  2509. writel(val, port->base + MVPP2_GMAC_CTRL_2_REG);
  2510. return 0;
  2511. }
  2512. static void gop_gmac_sgmii2_5_cfg(struct mvpp2_port *port)
  2513. {
  2514. u32 val, thresh;
  2515. /*
  2516. * Configure minimal level of the Tx FIFO before the lower part
  2517. * starts to read a packet
  2518. */
  2519. thresh = MVPP2_SGMII2_5_TX_FIFO_MIN_TH;
  2520. val = readl(port->base + MVPP2_GMAC_PORT_FIFO_CFG_1_REG);
  2521. val &= ~MVPP2_GMAC_TX_FIFO_MIN_TH_ALL_MASK;
  2522. val |= MVPP2_GMAC_TX_FIFO_MIN_TH_MASK(thresh);
  2523. writel(val, port->base + MVPP2_GMAC_PORT_FIFO_CFG_1_REG);
  2524. /* Disable bypass of sync module */
  2525. val = readl(port->base + MVPP2_GMAC_CTRL_4_REG);
  2526. val |= MVPP2_GMAC_CTRL4_SYNC_BYPASS_MASK;
  2527. /* configure DP clock select according to mode */
  2528. val |= MVPP2_GMAC_CTRL4_DP_CLK_SEL_MASK;
  2529. /* configure QSGMII bypass according to mode */
  2530. val |= MVPP2_GMAC_CTRL4_QSGMII_BYPASS_ACTIVE_MASK;
  2531. writel(val, port->base + MVPP2_GMAC_CTRL_4_REG);
  2532. val = readl(port->base + MVPP2_GMAC_CTRL_2_REG);
  2533. val |= MVPP2_GMAC_PORT_DIS_PADING_MASK;
  2534. writel(val, port->base + MVPP2_GMAC_CTRL_2_REG);
  2535. val = readl(port->base + MVPP2_GMAC_CTRL_0_REG);
  2536. /*
  2537. * Configure GIG MAC to 1000Base-X mode connected to a fiber
  2538. * transceiver
  2539. */
  2540. val |= MVPP2_GMAC_PORT_TYPE_MASK;
  2541. writel(val, port->base + MVPP2_GMAC_CTRL_0_REG);
  2542. /* configure AN 0x9268 */
  2543. val = MVPP2_GMAC_EN_PCS_AN |
  2544. MVPP2_GMAC_AN_BYPASS_EN |
  2545. MVPP2_GMAC_CONFIG_MII_SPEED |
  2546. MVPP2_GMAC_CONFIG_GMII_SPEED |
  2547. MVPP2_GMAC_FC_ADV_EN |
  2548. MVPP2_GMAC_CONFIG_FULL_DUPLEX |
  2549. MVPP2_GMAC_CHOOSE_SAMPLE_TX_CONFIG;
  2550. writel(val, port->base + MVPP2_GMAC_AUTONEG_CONFIG);
  2551. }
  2552. static void gop_gmac_sgmii_cfg(struct mvpp2_port *port)
  2553. {
  2554. u32 val, thresh;
  2555. /*
  2556. * Configure minimal level of the Tx FIFO before the lower part
  2557. * starts to read a packet
  2558. */
  2559. thresh = MVPP2_SGMII_TX_FIFO_MIN_TH;
  2560. val = readl(port->base + MVPP2_GMAC_PORT_FIFO_CFG_1_REG);
  2561. val &= ~MVPP2_GMAC_TX_FIFO_MIN_TH_ALL_MASK;
  2562. val |= MVPP2_GMAC_TX_FIFO_MIN_TH_MASK(thresh);
  2563. writel(val, port->base + MVPP2_GMAC_PORT_FIFO_CFG_1_REG);
  2564. /* Disable bypass of sync module */
  2565. val = readl(port->base + MVPP2_GMAC_CTRL_4_REG);
  2566. val |= MVPP2_GMAC_CTRL4_SYNC_BYPASS_MASK;
  2567. /* configure DP clock select according to mode */
  2568. val &= ~MVPP2_GMAC_CTRL4_DP_CLK_SEL_MASK;
  2569. /* configure QSGMII bypass according to mode */
  2570. val |= MVPP2_GMAC_CTRL4_QSGMII_BYPASS_ACTIVE_MASK;
  2571. writel(val, port->base + MVPP2_GMAC_CTRL_4_REG);
  2572. val = readl(port->base + MVPP2_GMAC_CTRL_2_REG);
  2573. val |= MVPP2_GMAC_PORT_DIS_PADING_MASK;
  2574. writel(val, port->base + MVPP2_GMAC_CTRL_2_REG);
  2575. val = readl(port->base + MVPP2_GMAC_CTRL_0_REG);
  2576. /* configure GIG MAC to SGMII mode */
  2577. val &= ~MVPP2_GMAC_PORT_TYPE_MASK;
  2578. writel(val, port->base + MVPP2_GMAC_CTRL_0_REG);
  2579. /* configure AN */
  2580. val = MVPP2_GMAC_EN_PCS_AN |
  2581. MVPP2_GMAC_AN_BYPASS_EN |
  2582. MVPP2_GMAC_AN_SPEED_EN |
  2583. MVPP2_GMAC_EN_FC_AN |
  2584. MVPP2_GMAC_AN_DUPLEX_EN |
  2585. MVPP2_GMAC_CHOOSE_SAMPLE_TX_CONFIG;
  2586. writel(val, port->base + MVPP2_GMAC_AUTONEG_CONFIG);
  2587. }
  2588. static void gop_gmac_rgmii_cfg(struct mvpp2_port *port)
  2589. {
  2590. u32 val, thresh;
  2591. /*
  2592. * Configure minimal level of the Tx FIFO before the lower part
  2593. * starts to read a packet
  2594. */
  2595. thresh = MVPP2_RGMII_TX_FIFO_MIN_TH;
  2596. val = readl(port->base + MVPP2_GMAC_PORT_FIFO_CFG_1_REG);
  2597. val &= ~MVPP2_GMAC_TX_FIFO_MIN_TH_ALL_MASK;
  2598. val |= MVPP2_GMAC_TX_FIFO_MIN_TH_MASK(thresh);
  2599. writel(val, port->base + MVPP2_GMAC_PORT_FIFO_CFG_1_REG);
  2600. /* Disable bypass of sync module */
  2601. val = readl(port->base + MVPP2_GMAC_CTRL_4_REG);
  2602. val |= MVPP2_GMAC_CTRL4_SYNC_BYPASS_MASK;
  2603. /* configure DP clock select according to mode */
  2604. val &= ~MVPP2_GMAC_CTRL4_DP_CLK_SEL_MASK;
  2605. val |= MVPP2_GMAC_CTRL4_QSGMII_BYPASS_ACTIVE_MASK;
  2606. val |= MVPP2_GMAC_CTRL4_EXT_PIN_GMII_SEL_MASK;
  2607. writel(val, port->base + MVPP2_GMAC_CTRL_4_REG);
  2608. val = readl(port->base + MVPP2_GMAC_CTRL_2_REG);
  2609. val &= ~MVPP2_GMAC_PORT_DIS_PADING_MASK;
  2610. writel(val, port->base + MVPP2_GMAC_CTRL_2_REG);
  2611. val = readl(port->base + MVPP2_GMAC_CTRL_0_REG);
  2612. /* configure GIG MAC to SGMII mode */
  2613. val &= ~MVPP2_GMAC_PORT_TYPE_MASK;
  2614. writel(val, port->base + MVPP2_GMAC_CTRL_0_REG);
  2615. /* configure AN 0xb8e8 */
  2616. val = MVPP2_GMAC_AN_BYPASS_EN |
  2617. MVPP2_GMAC_AN_SPEED_EN |
  2618. MVPP2_GMAC_EN_FC_AN |
  2619. MVPP2_GMAC_AN_DUPLEX_EN |
  2620. MVPP2_GMAC_CHOOSE_SAMPLE_TX_CONFIG;
  2621. writel(val, port->base + MVPP2_GMAC_AUTONEG_CONFIG);
  2622. }
  2623. /* Set the internal mux's to the required MAC in the GOP */
  2624. static int gop_gmac_mode_cfg(struct mvpp2_port *port)
  2625. {
  2626. u32 val;
  2627. /* Set TX FIFO thresholds */
  2628. switch (port->phy_interface) {
  2629. case PHY_INTERFACE_MODE_SGMII:
  2630. if (port->phy_speed == 2500)
  2631. gop_gmac_sgmii2_5_cfg(port);
  2632. else
  2633. gop_gmac_sgmii_cfg(port);
  2634. break;
  2635. case PHY_INTERFACE_MODE_RGMII:
  2636. case PHY_INTERFACE_MODE_RGMII_ID:
  2637. gop_gmac_rgmii_cfg(port);
  2638. break;
  2639. default:
  2640. return -1;
  2641. }
  2642. /* Jumbo frame support - 0x1400*2= 0x2800 bytes */
  2643. val = readl(port->base + MVPP2_GMAC_CTRL_0_REG);
  2644. val &= ~MVPP2_GMAC_MAX_RX_SIZE_MASK;
  2645. val |= 0x1400 << MVPP2_GMAC_MAX_RX_SIZE_OFFS;
  2646. writel(val, port->base + MVPP2_GMAC_CTRL_0_REG);
  2647. /* PeriodicXonEn disable */
  2648. val = readl(port->base + MVPP2_GMAC_CTRL_1_REG);
  2649. val &= ~MVPP2_GMAC_PERIODIC_XON_EN_MASK;
  2650. writel(val, port->base + MVPP2_GMAC_CTRL_1_REG);
  2651. return 0;
  2652. }
  2653. static void gop_xlg_2_gig_mac_cfg(struct mvpp2_port *port)
  2654. {
  2655. u32 val;
  2656. /* relevant only for MAC0 (XLG0 and GMAC0) */
  2657. if (port->gop_id > 0)
  2658. return;
  2659. /* configure 1Gig MAC mode */
  2660. val = readl(port->base + MVPP22_XLG_CTRL3_REG);
  2661. val &= ~MVPP22_XLG_CTRL3_MACMODESELECT_MASK;
  2662. val |= MVPP22_XLG_CTRL3_MACMODESELECT_GMAC;
  2663. writel(val, port->base + MVPP22_XLG_CTRL3_REG);
  2664. }
  2665. static int gop_gpcs_reset(struct mvpp2_port *port, int reset)
  2666. {
  2667. u32 val;
  2668. val = readl(port->base + MVPP2_GMAC_CTRL_2_REG);
  2669. if (reset)
  2670. val &= ~MVPP2_GMAC_SGMII_MODE_MASK;
  2671. else
  2672. val |= MVPP2_GMAC_SGMII_MODE_MASK;
  2673. writel(val, port->base + MVPP2_GMAC_CTRL_2_REG);
  2674. return 0;
  2675. }
  2676. /* Set the internal mux's to the required PCS in the PI */
  2677. static int gop_xpcs_mode(struct mvpp2_port *port, int num_of_lanes)
  2678. {
  2679. u32 val;
  2680. int lane;
  2681. switch (num_of_lanes) {
  2682. case 1:
  2683. lane = 0;
  2684. break;
  2685. case 2:
  2686. lane = 1;
  2687. break;
  2688. case 4:
  2689. lane = 2;
  2690. break;
  2691. default:
  2692. return -1;
  2693. }
  2694. /* configure XG MAC mode */
  2695. val = readl(port->priv->xpcs_base + MVPP22_XPCS_GLOBAL_CFG_0_REG);
  2696. val &= ~MVPP22_XPCS_PCSMODE_OFFS;
  2697. val &= ~MVPP22_XPCS_LANEACTIVE_MASK;
  2698. val |= (2 * lane) << MVPP22_XPCS_LANEACTIVE_OFFS;
  2699. writel(val, port->priv->xpcs_base + MVPP22_XPCS_GLOBAL_CFG_0_REG);
  2700. return 0;
  2701. }
  2702. static int gop_mpcs_mode(struct mvpp2_port *port)
  2703. {
  2704. u32 val;
  2705. /* configure PCS40G COMMON CONTROL */
  2706. val = readl(port->priv->mpcs_base + PCS40G_COMMON_CONTROL);
  2707. val &= ~FORWARD_ERROR_CORRECTION_MASK;
  2708. writel(val, port->priv->mpcs_base + PCS40G_COMMON_CONTROL);
  2709. /* configure PCS CLOCK RESET */
  2710. val = readl(port->priv->mpcs_base + PCS_CLOCK_RESET);
  2711. val &= ~CLK_DIVISION_RATIO_MASK;
  2712. val |= 1 << CLK_DIVISION_RATIO_OFFS;
  2713. writel(val, port->priv->mpcs_base + PCS_CLOCK_RESET);
  2714. val &= ~CLK_DIV_PHASE_SET_MASK;
  2715. val |= MAC_CLK_RESET_MASK;
  2716. val |= RX_SD_CLK_RESET_MASK;
  2717. val |= TX_SD_CLK_RESET_MASK;
  2718. writel(val, port->priv->mpcs_base + PCS_CLOCK_RESET);
  2719. return 0;
  2720. }
  2721. /* Set the internal mux's to the required MAC in the GOP */
  2722. static int gop_xlg_mac_mode_cfg(struct mvpp2_port *port, int num_of_act_lanes)
  2723. {
  2724. u32 val;
  2725. /* configure 10G MAC mode */
  2726. val = readl(port->base + MVPP22_XLG_CTRL0_REG);
  2727. val |= MVPP22_XLG_RX_FC_EN;
  2728. writel(val, port->base + MVPP22_XLG_CTRL0_REG);
  2729. val = readl(port->base + MVPP22_XLG_CTRL3_REG);
  2730. val &= ~MVPP22_XLG_CTRL3_MACMODESELECT_MASK;
  2731. val |= MVPP22_XLG_CTRL3_MACMODESELECT_10GMAC;
  2732. writel(val, port->base + MVPP22_XLG_CTRL3_REG);
  2733. /* read - modify - write */
  2734. val = readl(port->base + MVPP22_XLG_CTRL4_REG);
  2735. val &= ~MVPP22_XLG_MODE_DMA_1G;
  2736. val |= MVPP22_XLG_FORWARD_PFC_EN;
  2737. val |= MVPP22_XLG_FORWARD_802_3X_FC_EN;
  2738. val &= ~MVPP22_XLG_EN_IDLE_CHECK_FOR_LINK;
  2739. writel(val, port->base + MVPP22_XLG_CTRL4_REG);
  2740. /* Jumbo frame support: 0x1400 * 2 = 0x2800 bytes */
  2741. val = readl(port->base + MVPP22_XLG_CTRL1_REG);
  2742. val &= ~MVPP22_XLG_MAX_RX_SIZE_MASK;
  2743. val |= 0x1400 << MVPP22_XLG_MAX_RX_SIZE_OFFS;
  2744. writel(val, port->base + MVPP22_XLG_CTRL1_REG);
  2745. /* unmask link change interrupt */
  2746. val = readl(port->base + MVPP22_XLG_INTERRUPT_MASK_REG);
  2747. val |= MVPP22_XLG_INTERRUPT_LINK_CHANGE;
  2748. val |= 1; /* unmask summary bit */
  2749. writel(val, port->base + MVPP22_XLG_INTERRUPT_MASK_REG);
  2750. return 0;
  2751. }
  2752. /* Set PCS to reset or exit from reset */
  2753. static int gop_xpcs_reset(struct mvpp2_port *port, int reset)
  2754. {
  2755. u32 val;
  2756. /* read - modify - write */
  2757. val = readl(port->priv->xpcs_base + MVPP22_XPCS_GLOBAL_CFG_0_REG);
  2758. if (reset)
  2759. val &= ~MVPP22_XPCS_PCSRESET;
  2760. else
  2761. val |= MVPP22_XPCS_PCSRESET;
  2762. writel(val, port->priv->xpcs_base + MVPP22_XPCS_GLOBAL_CFG_0_REG);
  2763. return 0;
  2764. }
  2765. /* Set the MAC to reset or exit from reset */
  2766. static int gop_xlg_mac_reset(struct mvpp2_port *port, int reset)
  2767. {
  2768. u32 val;
  2769. /* read - modify - write */
  2770. val = readl(port->base + MVPP22_XLG_CTRL0_REG);
  2771. if (reset)
  2772. val &= ~MVPP22_XLG_MAC_RESETN;
  2773. else
  2774. val |= MVPP22_XLG_MAC_RESETN;
  2775. writel(val, port->base + MVPP22_XLG_CTRL0_REG);
  2776. return 0;
  2777. }
  2778. /*
  2779. * gop_port_init
  2780. *
  2781. * Init physical port. Configures the port mode and all it's elements
  2782. * accordingly.
  2783. * Does not verify that the selected mode/port number is valid at the
  2784. * core level.
  2785. */
  2786. static int gop_port_init(struct mvpp2_port *port)
  2787. {
  2788. int mac_num = port->gop_id;
  2789. int num_of_act_lanes;
  2790. if (mac_num >= MVPP22_GOP_MAC_NUM) {
  2791. netdev_err(NULL, "%s: illegal port number %d", __func__,
  2792. mac_num);
  2793. return -1;
  2794. }
  2795. switch (port->phy_interface) {
  2796. case PHY_INTERFACE_MODE_RGMII:
  2797. case PHY_INTERFACE_MODE_RGMII_ID:
  2798. gop_gmac_reset(port, 1);
  2799. /* configure PCS */
  2800. gop_gpcs_mode_cfg(port, 0);
  2801. gop_bypass_clk_cfg(port, 1);
  2802. /* configure MAC */
  2803. gop_gmac_mode_cfg(port);
  2804. /* pcs unreset */
  2805. gop_gpcs_reset(port, 0);
  2806. /* mac unreset */
  2807. gop_gmac_reset(port, 0);
  2808. break;
  2809. case PHY_INTERFACE_MODE_SGMII:
  2810. /* configure PCS */
  2811. gop_gpcs_mode_cfg(port, 1);
  2812. /* configure MAC */
  2813. gop_gmac_mode_cfg(port);
  2814. /* select proper Mac mode */
  2815. gop_xlg_2_gig_mac_cfg(port);
  2816. /* pcs unreset */
  2817. gop_gpcs_reset(port, 0);
  2818. /* mac unreset */
  2819. gop_gmac_reset(port, 0);
  2820. break;
  2821. case PHY_INTERFACE_MODE_SFI:
  2822. num_of_act_lanes = 2;
  2823. mac_num = 0;
  2824. /* configure PCS */
  2825. gop_xpcs_mode(port, num_of_act_lanes);
  2826. gop_mpcs_mode(port);
  2827. /* configure MAC */
  2828. gop_xlg_mac_mode_cfg(port, num_of_act_lanes);
  2829. /* pcs unreset */
  2830. gop_xpcs_reset(port, 0);
  2831. /* mac unreset */
  2832. gop_xlg_mac_reset(port, 0);
  2833. break;
  2834. default:
  2835. netdev_err(NULL, "%s: Requested port mode (%d) not supported\n",
  2836. __func__, port->phy_interface);
  2837. return -1;
  2838. }
  2839. return 0;
  2840. }
  2841. static void gop_xlg_mac_port_enable(struct mvpp2_port *port, int enable)
  2842. {
  2843. u32 val;
  2844. val = readl(port->base + MVPP22_XLG_CTRL0_REG);
  2845. if (enable) {
  2846. /* Enable port and MIB counters update */
  2847. val |= MVPP22_XLG_PORT_EN;
  2848. val &= ~MVPP22_XLG_MIBCNT_DIS;
  2849. } else {
  2850. /* Disable port */
  2851. val &= ~MVPP22_XLG_PORT_EN;
  2852. }
  2853. writel(val, port->base + MVPP22_XLG_CTRL0_REG);
  2854. }
  2855. static void gop_port_enable(struct mvpp2_port *port, int enable)
  2856. {
  2857. switch (port->phy_interface) {
  2858. case PHY_INTERFACE_MODE_RGMII:
  2859. case PHY_INTERFACE_MODE_RGMII_ID:
  2860. case PHY_INTERFACE_MODE_SGMII:
  2861. if (enable)
  2862. mvpp2_port_enable(port);
  2863. else
  2864. mvpp2_port_disable(port);
  2865. break;
  2866. case PHY_INTERFACE_MODE_SFI:
  2867. gop_xlg_mac_port_enable(port, enable);
  2868. break;
  2869. default:
  2870. netdev_err(NULL, "%s: Wrong port mode (%d)\n", __func__,
  2871. port->phy_interface);
  2872. return;
  2873. }
  2874. }
  2875. /* RFU1 functions */
  2876. static inline u32 gop_rfu1_read(struct mvpp2 *priv, u32 offset)
  2877. {
  2878. return readl(priv->rfu1_base + offset);
  2879. }
  2880. static inline void gop_rfu1_write(struct mvpp2 *priv, u32 offset, u32 data)
  2881. {
  2882. writel(data, priv->rfu1_base + offset);
  2883. }
  2884. static u32 mvpp2_netc_cfg_create(int gop_id, phy_interface_t phy_type)
  2885. {
  2886. u32 val = 0;
  2887. if (gop_id == 2) {
  2888. if (phy_type == PHY_INTERFACE_MODE_SGMII)
  2889. val |= MV_NETC_GE_MAC2_SGMII;
  2890. }
  2891. if (gop_id == 3) {
  2892. if (phy_type == PHY_INTERFACE_MODE_SGMII)
  2893. val |= MV_NETC_GE_MAC3_SGMII;
  2894. else if (phy_type == PHY_INTERFACE_MODE_RGMII ||
  2895. phy_type == PHY_INTERFACE_MODE_RGMII_ID)
  2896. val |= MV_NETC_GE_MAC3_RGMII;
  2897. }
  2898. return val;
  2899. }
  2900. static void gop_netc_active_port(struct mvpp2 *priv, int gop_id, u32 val)
  2901. {
  2902. u32 reg;
  2903. reg = gop_rfu1_read(priv, NETCOMP_PORTS_CONTROL_1_REG);
  2904. reg &= ~(NETC_PORTS_ACTIVE_MASK(gop_id));
  2905. val <<= NETC_PORTS_ACTIVE_OFFSET(gop_id);
  2906. val &= NETC_PORTS_ACTIVE_MASK(gop_id);
  2907. reg |= val;
  2908. gop_rfu1_write(priv, NETCOMP_PORTS_CONTROL_1_REG, reg);
  2909. }
  2910. static void gop_netc_mii_mode(struct mvpp2 *priv, int gop_id, u32 val)
  2911. {
  2912. u32 reg;
  2913. reg = gop_rfu1_read(priv, NETCOMP_CONTROL_0_REG);
  2914. reg &= ~NETC_GBE_PORT1_MII_MODE_MASK;
  2915. val <<= NETC_GBE_PORT1_MII_MODE_OFFS;
  2916. val &= NETC_GBE_PORT1_MII_MODE_MASK;
  2917. reg |= val;
  2918. gop_rfu1_write(priv, NETCOMP_CONTROL_0_REG, reg);
  2919. }
  2920. static void gop_netc_gop_reset(struct mvpp2 *priv, u32 val)
  2921. {
  2922. u32 reg;
  2923. reg = gop_rfu1_read(priv, GOP_SOFT_RESET_1_REG);
  2924. reg &= ~NETC_GOP_SOFT_RESET_MASK;
  2925. val <<= NETC_GOP_SOFT_RESET_OFFS;
  2926. val &= NETC_GOP_SOFT_RESET_MASK;
  2927. reg |= val;
  2928. gop_rfu1_write(priv, GOP_SOFT_RESET_1_REG, reg);
  2929. }
  2930. static void gop_netc_gop_clock_logic_set(struct mvpp2 *priv, u32 val)
  2931. {
  2932. u32 reg;
  2933. reg = gop_rfu1_read(priv, NETCOMP_PORTS_CONTROL_0_REG);
  2934. reg &= ~NETC_CLK_DIV_PHASE_MASK;
  2935. val <<= NETC_CLK_DIV_PHASE_OFFS;
  2936. val &= NETC_CLK_DIV_PHASE_MASK;
  2937. reg |= val;
  2938. gop_rfu1_write(priv, NETCOMP_PORTS_CONTROL_0_REG, reg);
  2939. }
  2940. static void gop_netc_port_rf_reset(struct mvpp2 *priv, int gop_id, u32 val)
  2941. {
  2942. u32 reg;
  2943. reg = gop_rfu1_read(priv, NETCOMP_PORTS_CONTROL_1_REG);
  2944. reg &= ~(NETC_PORT_GIG_RF_RESET_MASK(gop_id));
  2945. val <<= NETC_PORT_GIG_RF_RESET_OFFS(gop_id);
  2946. val &= NETC_PORT_GIG_RF_RESET_MASK(gop_id);
  2947. reg |= val;
  2948. gop_rfu1_write(priv, NETCOMP_PORTS_CONTROL_1_REG, reg);
  2949. }
  2950. static void gop_netc_gbe_sgmii_mode_select(struct mvpp2 *priv, int gop_id,
  2951. u32 val)
  2952. {
  2953. u32 reg, mask, offset;
  2954. if (gop_id == 2) {
  2955. mask = NETC_GBE_PORT0_SGMII_MODE_MASK;
  2956. offset = NETC_GBE_PORT0_SGMII_MODE_OFFS;
  2957. } else {
  2958. mask = NETC_GBE_PORT1_SGMII_MODE_MASK;
  2959. offset = NETC_GBE_PORT1_SGMII_MODE_OFFS;
  2960. }
  2961. reg = gop_rfu1_read(priv, NETCOMP_CONTROL_0_REG);
  2962. reg &= ~mask;
  2963. val <<= offset;
  2964. val &= mask;
  2965. reg |= val;
  2966. gop_rfu1_write(priv, NETCOMP_CONTROL_0_REG, reg);
  2967. }
  2968. static void gop_netc_bus_width_select(struct mvpp2 *priv, u32 val)
  2969. {
  2970. u32 reg;
  2971. reg = gop_rfu1_read(priv, NETCOMP_PORTS_CONTROL_0_REG);
  2972. reg &= ~NETC_BUS_WIDTH_SELECT_MASK;
  2973. val <<= NETC_BUS_WIDTH_SELECT_OFFS;
  2974. val &= NETC_BUS_WIDTH_SELECT_MASK;
  2975. reg |= val;
  2976. gop_rfu1_write(priv, NETCOMP_PORTS_CONTROL_0_REG, reg);
  2977. }
  2978. static void gop_netc_sample_stages_timing(struct mvpp2 *priv, u32 val)
  2979. {
  2980. u32 reg;
  2981. reg = gop_rfu1_read(priv, NETCOMP_PORTS_CONTROL_0_REG);
  2982. reg &= ~NETC_GIG_RX_DATA_SAMPLE_MASK;
  2983. val <<= NETC_GIG_RX_DATA_SAMPLE_OFFS;
  2984. val &= NETC_GIG_RX_DATA_SAMPLE_MASK;
  2985. reg |= val;
  2986. gop_rfu1_write(priv, NETCOMP_PORTS_CONTROL_0_REG, reg);
  2987. }
  2988. static void gop_netc_mac_to_xgmii(struct mvpp2 *priv, int gop_id,
  2989. enum mv_netc_phase phase)
  2990. {
  2991. switch (phase) {
  2992. case MV_NETC_FIRST_PHASE:
  2993. /* Set Bus Width to HB mode = 1 */
  2994. gop_netc_bus_width_select(priv, 1);
  2995. /* Select RGMII mode */
  2996. gop_netc_gbe_sgmii_mode_select(priv, gop_id, MV_NETC_GBE_XMII);
  2997. break;
  2998. case MV_NETC_SECOND_PHASE:
  2999. /* De-assert the relevant port HB reset */
  3000. gop_netc_port_rf_reset(priv, gop_id, 1);
  3001. break;
  3002. }
  3003. }
  3004. static void gop_netc_mac_to_sgmii(struct mvpp2 *priv, int gop_id,
  3005. enum mv_netc_phase phase)
  3006. {
  3007. switch (phase) {
  3008. case MV_NETC_FIRST_PHASE:
  3009. /* Set Bus Width to HB mode = 1 */
  3010. gop_netc_bus_width_select(priv, 1);
  3011. /* Select SGMII mode */
  3012. if (gop_id >= 1) {
  3013. gop_netc_gbe_sgmii_mode_select(priv, gop_id,
  3014. MV_NETC_GBE_SGMII);
  3015. }
  3016. /* Configure the sample stages */
  3017. gop_netc_sample_stages_timing(priv, 0);
  3018. /* Configure the ComPhy Selector */
  3019. /* gop_netc_com_phy_selector_config(netComplex); */
  3020. break;
  3021. case MV_NETC_SECOND_PHASE:
  3022. /* De-assert the relevant port HB reset */
  3023. gop_netc_port_rf_reset(priv, gop_id, 1);
  3024. break;
  3025. }
  3026. }
  3027. static int gop_netc_init(struct mvpp2 *priv, enum mv_netc_phase phase)
  3028. {
  3029. u32 c = priv->netc_config;
  3030. if (c & MV_NETC_GE_MAC2_SGMII)
  3031. gop_netc_mac_to_sgmii(priv, 2, phase);
  3032. else
  3033. gop_netc_mac_to_xgmii(priv, 2, phase);
  3034. if (c & MV_NETC_GE_MAC3_SGMII) {
  3035. gop_netc_mac_to_sgmii(priv, 3, phase);
  3036. } else {
  3037. gop_netc_mac_to_xgmii(priv, 3, phase);
  3038. if (c & MV_NETC_GE_MAC3_RGMII)
  3039. gop_netc_mii_mode(priv, 3, MV_NETC_GBE_RGMII);
  3040. else
  3041. gop_netc_mii_mode(priv, 3, MV_NETC_GBE_MII);
  3042. }
  3043. /* Activate gop ports 0, 2, 3 */
  3044. gop_netc_active_port(priv, 0, 1);
  3045. gop_netc_active_port(priv, 2, 1);
  3046. gop_netc_active_port(priv, 3, 1);
  3047. if (phase == MV_NETC_SECOND_PHASE) {
  3048. /* Enable the GOP internal clock logic */
  3049. gop_netc_gop_clock_logic_set(priv, 1);
  3050. /* De-assert GOP unit reset */
  3051. gop_netc_gop_reset(priv, 1);
  3052. }
  3053. return 0;
  3054. }
  3055. /* Set defaults to the MVPP2 port */
  3056. static void mvpp2_defaults_set(struct mvpp2_port *port)
  3057. {
  3058. int tx_port_num, val, queue, ptxq, lrxq;
  3059. if (port->priv->hw_version == MVPP21) {
  3060. /* Configure port to loopback if needed */
  3061. if (port->flags & MVPP2_F_LOOPBACK)
  3062. mvpp2_port_loopback_set(port);
  3063. /* Update TX FIFO MIN Threshold */
  3064. val = readl(port->base + MVPP2_GMAC_PORT_FIFO_CFG_1_REG);
  3065. val &= ~MVPP2_GMAC_TX_FIFO_MIN_TH_ALL_MASK;
  3066. /* Min. TX threshold must be less than minimal packet length */
  3067. val |= MVPP2_GMAC_TX_FIFO_MIN_TH_MASK(64 - 4 - 2);
  3068. writel(val, port->base + MVPP2_GMAC_PORT_FIFO_CFG_1_REG);
  3069. }
  3070. /* Disable Legacy WRR, Disable EJP, Release from reset */
  3071. tx_port_num = mvpp2_egress_port(port);
  3072. mvpp2_write(port->priv, MVPP2_TXP_SCHED_PORT_INDEX_REG,
  3073. tx_port_num);
  3074. mvpp2_write(port->priv, MVPP2_TXP_SCHED_CMD_1_REG, 0);
  3075. /* Close bandwidth for all queues */
  3076. for (queue = 0; queue < MVPP2_MAX_TXQ; queue++) {
  3077. ptxq = mvpp2_txq_phys(port->id, queue);
  3078. mvpp2_write(port->priv,
  3079. MVPP2_TXQ_SCHED_TOKEN_CNTR_REG(ptxq), 0);
  3080. }
  3081. /* Set refill period to 1 usec, refill tokens
  3082. * and bucket size to maximum
  3083. */
  3084. mvpp2_write(port->priv, MVPP2_TXP_SCHED_PERIOD_REG, 0xc8);
  3085. val = mvpp2_read(port->priv, MVPP2_TXP_SCHED_REFILL_REG);
  3086. val &= ~MVPP2_TXP_REFILL_PERIOD_ALL_MASK;
  3087. val |= MVPP2_TXP_REFILL_PERIOD_MASK(1);
  3088. val |= MVPP2_TXP_REFILL_TOKENS_ALL_MASK;
  3089. mvpp2_write(port->priv, MVPP2_TXP_SCHED_REFILL_REG, val);
  3090. val = MVPP2_TXP_TOKEN_SIZE_MAX;
  3091. mvpp2_write(port->priv, MVPP2_TXP_SCHED_TOKEN_SIZE_REG, val);
  3092. /* Set MaximumLowLatencyPacketSize value to 256 */
  3093. mvpp2_write(port->priv, MVPP2_RX_CTRL_REG(port->id),
  3094. MVPP2_RX_USE_PSEUDO_FOR_CSUM_MASK |
  3095. MVPP2_RX_LOW_LATENCY_PKT_SIZE(256));
  3096. /* Enable Rx cache snoop */
  3097. for (lrxq = 0; lrxq < rxq_number; lrxq++) {
  3098. queue = port->rxqs[lrxq]->id;
  3099. val = mvpp2_read(port->priv, MVPP2_RXQ_CONFIG_REG(queue));
  3100. val |= MVPP2_SNOOP_PKT_SIZE_MASK |
  3101. MVPP2_SNOOP_BUF_HDR_MASK;
  3102. mvpp2_write(port->priv, MVPP2_RXQ_CONFIG_REG(queue), val);
  3103. }
  3104. }
  3105. /* Enable/disable receiving packets */
  3106. static void mvpp2_ingress_enable(struct mvpp2_port *port)
  3107. {
  3108. u32 val;
  3109. int lrxq, queue;
  3110. for (lrxq = 0; lrxq < rxq_number; lrxq++) {
  3111. queue = port->rxqs[lrxq]->id;
  3112. val = mvpp2_read(port->priv, MVPP2_RXQ_CONFIG_REG(queue));
  3113. val &= ~MVPP2_RXQ_DISABLE_MASK;
  3114. mvpp2_write(port->priv, MVPP2_RXQ_CONFIG_REG(queue), val);
  3115. }
  3116. }
  3117. static void mvpp2_ingress_disable(struct mvpp2_port *port)
  3118. {
  3119. u32 val;
  3120. int lrxq, queue;
  3121. for (lrxq = 0; lrxq < rxq_number; lrxq++) {
  3122. queue = port->rxqs[lrxq]->id;
  3123. val = mvpp2_read(port->priv, MVPP2_RXQ_CONFIG_REG(queue));
  3124. val |= MVPP2_RXQ_DISABLE_MASK;
  3125. mvpp2_write(port->priv, MVPP2_RXQ_CONFIG_REG(queue), val);
  3126. }
  3127. }
  3128. /* Enable transmit via physical egress queue
  3129. * - HW starts take descriptors from DRAM
  3130. */
  3131. static void mvpp2_egress_enable(struct mvpp2_port *port)
  3132. {
  3133. u32 qmap;
  3134. int queue;
  3135. int tx_port_num = mvpp2_egress_port(port);
  3136. /* Enable all initialized TXs. */
  3137. qmap = 0;
  3138. for (queue = 0; queue < txq_number; queue++) {
  3139. struct mvpp2_tx_queue *txq = port->txqs[queue];
  3140. if (txq->descs != NULL)
  3141. qmap |= (1 << queue);
  3142. }
  3143. mvpp2_write(port->priv, MVPP2_TXP_SCHED_PORT_INDEX_REG, tx_port_num);
  3144. mvpp2_write(port->priv, MVPP2_TXP_SCHED_Q_CMD_REG, qmap);
  3145. }
  3146. /* Disable transmit via physical egress queue
  3147. * - HW doesn't take descriptors from DRAM
  3148. */
  3149. static void mvpp2_egress_disable(struct mvpp2_port *port)
  3150. {
  3151. u32 reg_data;
  3152. int delay;
  3153. int tx_port_num = mvpp2_egress_port(port);
  3154. /* Issue stop command for active channels only */
  3155. mvpp2_write(port->priv, MVPP2_TXP_SCHED_PORT_INDEX_REG, tx_port_num);
  3156. reg_data = (mvpp2_read(port->priv, MVPP2_TXP_SCHED_Q_CMD_REG)) &
  3157. MVPP2_TXP_SCHED_ENQ_MASK;
  3158. if (reg_data != 0)
  3159. mvpp2_write(port->priv, MVPP2_TXP_SCHED_Q_CMD_REG,
  3160. (reg_data << MVPP2_TXP_SCHED_DISQ_OFFSET));
  3161. /* Wait for all Tx activity to terminate. */
  3162. delay = 0;
  3163. do {
  3164. if (delay >= MVPP2_TX_DISABLE_TIMEOUT_MSEC) {
  3165. netdev_warn(port->dev,
  3166. "Tx stop timed out, status=0x%08x\n",
  3167. reg_data);
  3168. break;
  3169. }
  3170. mdelay(1);
  3171. delay++;
  3172. /* Check port TX Command register that all
  3173. * Tx queues are stopped
  3174. */
  3175. reg_data = mvpp2_read(port->priv, MVPP2_TXP_SCHED_Q_CMD_REG);
  3176. } while (reg_data & MVPP2_TXP_SCHED_ENQ_MASK);
  3177. }
  3178. /* Rx descriptors helper methods */
  3179. /* Get number of Rx descriptors occupied by received packets */
  3180. static inline int
  3181. mvpp2_rxq_received(struct mvpp2_port *port, int rxq_id)
  3182. {
  3183. u32 val = mvpp2_read(port->priv, MVPP2_RXQ_STATUS_REG(rxq_id));
  3184. return val & MVPP2_RXQ_OCCUPIED_MASK;
  3185. }
  3186. /* Update Rx queue status with the number of occupied and available
  3187. * Rx descriptor slots.
  3188. */
  3189. static inline void
  3190. mvpp2_rxq_status_update(struct mvpp2_port *port, int rxq_id,
  3191. int used_count, int free_count)
  3192. {
  3193. /* Decrement the number of used descriptors and increment count
  3194. * increment the number of free descriptors.
  3195. */
  3196. u32 val = used_count | (free_count << MVPP2_RXQ_NUM_NEW_OFFSET);
  3197. mvpp2_write(port->priv, MVPP2_RXQ_STATUS_UPDATE_REG(rxq_id), val);
  3198. }
  3199. /* Get pointer to next RX descriptor to be processed by SW */
  3200. static inline struct mvpp2_rx_desc *
  3201. mvpp2_rxq_next_desc_get(struct mvpp2_rx_queue *rxq)
  3202. {
  3203. int rx_desc = rxq->next_desc_to_proc;
  3204. rxq->next_desc_to_proc = MVPP2_QUEUE_NEXT_DESC(rxq, rx_desc);
  3205. prefetch(rxq->descs + rxq->next_desc_to_proc);
  3206. return rxq->descs + rx_desc;
  3207. }
  3208. /* Set rx queue offset */
  3209. static void mvpp2_rxq_offset_set(struct mvpp2_port *port,
  3210. int prxq, int offset)
  3211. {
  3212. u32 val;
  3213. /* Convert offset from bytes to units of 32 bytes */
  3214. offset = offset >> 5;
  3215. val = mvpp2_read(port->priv, MVPP2_RXQ_CONFIG_REG(prxq));
  3216. val &= ~MVPP2_RXQ_PACKET_OFFSET_MASK;
  3217. /* Offset is in */
  3218. val |= ((offset << MVPP2_RXQ_PACKET_OFFSET_OFFS) &
  3219. MVPP2_RXQ_PACKET_OFFSET_MASK);
  3220. mvpp2_write(port->priv, MVPP2_RXQ_CONFIG_REG(prxq), val);
  3221. }
  3222. /* Obtain BM cookie information from descriptor */
  3223. static u32 mvpp2_bm_cookie_build(struct mvpp2_port *port,
  3224. struct mvpp2_rx_desc *rx_desc)
  3225. {
  3226. int cpu = smp_processor_id();
  3227. int pool;
  3228. pool = (mvpp2_rxdesc_status_get(port, rx_desc) &
  3229. MVPP2_RXD_BM_POOL_ID_MASK) >>
  3230. MVPP2_RXD_BM_POOL_ID_OFFS;
  3231. return ((pool & 0xFF) << MVPP2_BM_COOKIE_POOL_OFFS) |
  3232. ((cpu & 0xFF) << MVPP2_BM_COOKIE_CPU_OFFS);
  3233. }
  3234. /* Tx descriptors helper methods */
  3235. /* Get number of Tx descriptors waiting to be transmitted by HW */
  3236. static int mvpp2_txq_pend_desc_num_get(struct mvpp2_port *port,
  3237. struct mvpp2_tx_queue *txq)
  3238. {
  3239. u32 val;
  3240. mvpp2_write(port->priv, MVPP2_TXQ_NUM_REG, txq->id);
  3241. val = mvpp2_read(port->priv, MVPP2_TXQ_PENDING_REG);
  3242. return val & MVPP2_TXQ_PENDING_MASK;
  3243. }
  3244. /* Get pointer to next Tx descriptor to be processed (send) by HW */
  3245. static struct mvpp2_tx_desc *
  3246. mvpp2_txq_next_desc_get(struct mvpp2_tx_queue *txq)
  3247. {
  3248. int tx_desc = txq->next_desc_to_proc;
  3249. txq->next_desc_to_proc = MVPP2_QUEUE_NEXT_DESC(txq, tx_desc);
  3250. return txq->descs + tx_desc;
  3251. }
  3252. /* Update HW with number of aggregated Tx descriptors to be sent */
  3253. static void mvpp2_aggr_txq_pend_desc_add(struct mvpp2_port *port, int pending)
  3254. {
  3255. /* aggregated access - relevant TXQ number is written in TX desc */
  3256. mvpp2_write(port->priv, MVPP2_AGGR_TXQ_UPDATE_REG, pending);
  3257. }
  3258. /* Get number of sent descriptors and decrement counter.
  3259. * The number of sent descriptors is returned.
  3260. * Per-CPU access
  3261. */
  3262. static inline int mvpp2_txq_sent_desc_proc(struct mvpp2_port *port,
  3263. struct mvpp2_tx_queue *txq)
  3264. {
  3265. u32 val;
  3266. /* Reading status reg resets transmitted descriptor counter */
  3267. val = mvpp2_read(port->priv, MVPP2_TXQ_SENT_REG(txq->id));
  3268. return (val & MVPP2_TRANSMITTED_COUNT_MASK) >>
  3269. MVPP2_TRANSMITTED_COUNT_OFFSET;
  3270. }
  3271. static void mvpp2_txq_sent_counter_clear(void *arg)
  3272. {
  3273. struct mvpp2_port *port = arg;
  3274. int queue;
  3275. for (queue = 0; queue < txq_number; queue++) {
  3276. int id = port->txqs[queue]->id;
  3277. mvpp2_read(port->priv, MVPP2_TXQ_SENT_REG(id));
  3278. }
  3279. }
  3280. /* Set max sizes for Tx queues */
  3281. static void mvpp2_txp_max_tx_size_set(struct mvpp2_port *port)
  3282. {
  3283. u32 val, size, mtu;
  3284. int txq, tx_port_num;
  3285. mtu = port->pkt_size * 8;
  3286. if (mtu > MVPP2_TXP_MTU_MAX)
  3287. mtu = MVPP2_TXP_MTU_MAX;
  3288. /* WA for wrong Token bucket update: Set MTU value = 3*real MTU value */
  3289. mtu = 3 * mtu;
  3290. /* Indirect access to registers */
  3291. tx_port_num = mvpp2_egress_port(port);
  3292. mvpp2_write(port->priv, MVPP2_TXP_SCHED_PORT_INDEX_REG, tx_port_num);
  3293. /* Set MTU */
  3294. val = mvpp2_read(port->priv, MVPP2_TXP_SCHED_MTU_REG);
  3295. val &= ~MVPP2_TXP_MTU_MAX;
  3296. val |= mtu;
  3297. mvpp2_write(port->priv, MVPP2_TXP_SCHED_MTU_REG, val);
  3298. /* TXP token size and all TXQs token size must be larger that MTU */
  3299. val = mvpp2_read(port->priv, MVPP2_TXP_SCHED_TOKEN_SIZE_REG);
  3300. size = val & MVPP2_TXP_TOKEN_SIZE_MAX;
  3301. if (size < mtu) {
  3302. size = mtu;
  3303. val &= ~MVPP2_TXP_TOKEN_SIZE_MAX;
  3304. val |= size;
  3305. mvpp2_write(port->priv, MVPP2_TXP_SCHED_TOKEN_SIZE_REG, val);
  3306. }
  3307. for (txq = 0; txq < txq_number; txq++) {
  3308. val = mvpp2_read(port->priv,
  3309. MVPP2_TXQ_SCHED_TOKEN_SIZE_REG(txq));
  3310. size = val & MVPP2_TXQ_TOKEN_SIZE_MAX;
  3311. if (size < mtu) {
  3312. size = mtu;
  3313. val &= ~MVPP2_TXQ_TOKEN_SIZE_MAX;
  3314. val |= size;
  3315. mvpp2_write(port->priv,
  3316. MVPP2_TXQ_SCHED_TOKEN_SIZE_REG(txq),
  3317. val);
  3318. }
  3319. }
  3320. }
  3321. /* Free Tx queue skbuffs */
  3322. static void mvpp2_txq_bufs_free(struct mvpp2_port *port,
  3323. struct mvpp2_tx_queue *txq,
  3324. struct mvpp2_txq_pcpu *txq_pcpu, int num)
  3325. {
  3326. int i;
  3327. for (i = 0; i < num; i++)
  3328. mvpp2_txq_inc_get(txq_pcpu);
  3329. }
  3330. static inline struct mvpp2_rx_queue *mvpp2_get_rx_queue(struct mvpp2_port *port,
  3331. u32 cause)
  3332. {
  3333. int queue = fls(cause) - 1;
  3334. return port->rxqs[queue];
  3335. }
  3336. static inline struct mvpp2_tx_queue *mvpp2_get_tx_queue(struct mvpp2_port *port,
  3337. u32 cause)
  3338. {
  3339. int queue = fls(cause) - 1;
  3340. return port->txqs[queue];
  3341. }
  3342. /* Rx/Tx queue initialization/cleanup methods */
  3343. /* Allocate and initialize descriptors for aggr TXQ */
  3344. static int mvpp2_aggr_txq_init(struct udevice *dev,
  3345. struct mvpp2_tx_queue *aggr_txq,
  3346. int desc_num, int cpu,
  3347. struct mvpp2 *priv)
  3348. {
  3349. u32 txq_dma;
  3350. /* Allocate memory for TX descriptors */
  3351. aggr_txq->descs = buffer_loc.aggr_tx_descs;
  3352. aggr_txq->descs_dma = (dma_addr_t)buffer_loc.aggr_tx_descs;
  3353. if (!aggr_txq->descs)
  3354. return -ENOMEM;
  3355. /* Make sure descriptor address is cache line size aligned */
  3356. BUG_ON(aggr_txq->descs !=
  3357. PTR_ALIGN(aggr_txq->descs, MVPP2_CPU_D_CACHE_LINE_SIZE));
  3358. aggr_txq->last_desc = aggr_txq->size - 1;
  3359. /* Aggr TXQ no reset WA */
  3360. aggr_txq->next_desc_to_proc = mvpp2_read(priv,
  3361. MVPP2_AGGR_TXQ_INDEX_REG(cpu));
  3362. /* Set Tx descriptors queue starting address indirect
  3363. * access
  3364. */
  3365. if (priv->hw_version == MVPP21)
  3366. txq_dma = aggr_txq->descs_dma;
  3367. else
  3368. txq_dma = aggr_txq->descs_dma >>
  3369. MVPP22_AGGR_TXQ_DESC_ADDR_OFFS;
  3370. mvpp2_write(priv, MVPP2_AGGR_TXQ_DESC_ADDR_REG(cpu), txq_dma);
  3371. mvpp2_write(priv, MVPP2_AGGR_TXQ_DESC_SIZE_REG(cpu), desc_num);
  3372. return 0;
  3373. }
  3374. /* Create a specified Rx queue */
  3375. static int mvpp2_rxq_init(struct mvpp2_port *port,
  3376. struct mvpp2_rx_queue *rxq)
  3377. {
  3378. u32 rxq_dma;
  3379. rxq->size = port->rx_ring_size;
  3380. /* Allocate memory for RX descriptors */
  3381. rxq->descs = buffer_loc.rx_descs;
  3382. rxq->descs_dma = (dma_addr_t)buffer_loc.rx_descs;
  3383. if (!rxq->descs)
  3384. return -ENOMEM;
  3385. BUG_ON(rxq->descs !=
  3386. PTR_ALIGN(rxq->descs, MVPP2_CPU_D_CACHE_LINE_SIZE));
  3387. rxq->last_desc = rxq->size - 1;
  3388. /* Zero occupied and non-occupied counters - direct access */
  3389. mvpp2_write(port->priv, MVPP2_RXQ_STATUS_REG(rxq->id), 0);
  3390. /* Set Rx descriptors queue starting address - indirect access */
  3391. mvpp2_write(port->priv, MVPP2_RXQ_NUM_REG, rxq->id);
  3392. if (port->priv->hw_version == MVPP21)
  3393. rxq_dma = rxq->descs_dma;
  3394. else
  3395. rxq_dma = rxq->descs_dma >> MVPP22_DESC_ADDR_OFFS;
  3396. mvpp2_write(port->priv, MVPP2_RXQ_DESC_ADDR_REG, rxq_dma);
  3397. mvpp2_write(port->priv, MVPP2_RXQ_DESC_SIZE_REG, rxq->size);
  3398. mvpp2_write(port->priv, MVPP2_RXQ_INDEX_REG, 0);
  3399. /* Set Offset */
  3400. mvpp2_rxq_offset_set(port, rxq->id, NET_SKB_PAD);
  3401. /* Add number of descriptors ready for receiving packets */
  3402. mvpp2_rxq_status_update(port, rxq->id, 0, rxq->size);
  3403. return 0;
  3404. }
  3405. /* Push packets received by the RXQ to BM pool */
  3406. static void mvpp2_rxq_drop_pkts(struct mvpp2_port *port,
  3407. struct mvpp2_rx_queue *rxq)
  3408. {
  3409. int rx_received, i;
  3410. rx_received = mvpp2_rxq_received(port, rxq->id);
  3411. if (!rx_received)
  3412. return;
  3413. for (i = 0; i < rx_received; i++) {
  3414. struct mvpp2_rx_desc *rx_desc = mvpp2_rxq_next_desc_get(rxq);
  3415. u32 bm = mvpp2_bm_cookie_build(port, rx_desc);
  3416. mvpp2_pool_refill(port, bm,
  3417. mvpp2_rxdesc_dma_addr_get(port, rx_desc),
  3418. mvpp2_rxdesc_cookie_get(port, rx_desc));
  3419. }
  3420. mvpp2_rxq_status_update(port, rxq->id, rx_received, rx_received);
  3421. }
  3422. /* Cleanup Rx queue */
  3423. static void mvpp2_rxq_deinit(struct mvpp2_port *port,
  3424. struct mvpp2_rx_queue *rxq)
  3425. {
  3426. mvpp2_rxq_drop_pkts(port, rxq);
  3427. rxq->descs = NULL;
  3428. rxq->last_desc = 0;
  3429. rxq->next_desc_to_proc = 0;
  3430. rxq->descs_dma = 0;
  3431. /* Clear Rx descriptors queue starting address and size;
  3432. * free descriptor number
  3433. */
  3434. mvpp2_write(port->priv, MVPP2_RXQ_STATUS_REG(rxq->id), 0);
  3435. mvpp2_write(port->priv, MVPP2_RXQ_NUM_REG, rxq->id);
  3436. mvpp2_write(port->priv, MVPP2_RXQ_DESC_ADDR_REG, 0);
  3437. mvpp2_write(port->priv, MVPP2_RXQ_DESC_SIZE_REG, 0);
  3438. }
  3439. /* Create and initialize a Tx queue */
  3440. static int mvpp2_txq_init(struct mvpp2_port *port,
  3441. struct mvpp2_tx_queue *txq)
  3442. {
  3443. u32 val;
  3444. int cpu, desc, desc_per_txq, tx_port_num;
  3445. struct mvpp2_txq_pcpu *txq_pcpu;
  3446. txq->size = port->tx_ring_size;
  3447. /* Allocate memory for Tx descriptors */
  3448. txq->descs = buffer_loc.tx_descs;
  3449. txq->descs_dma = (dma_addr_t)buffer_loc.tx_descs;
  3450. if (!txq->descs)
  3451. return -ENOMEM;
  3452. /* Make sure descriptor address is cache line size aligned */
  3453. BUG_ON(txq->descs !=
  3454. PTR_ALIGN(txq->descs, MVPP2_CPU_D_CACHE_LINE_SIZE));
  3455. txq->last_desc = txq->size - 1;
  3456. /* Set Tx descriptors queue starting address - indirect access */
  3457. mvpp2_write(port->priv, MVPP2_TXQ_NUM_REG, txq->id);
  3458. mvpp2_write(port->priv, MVPP2_TXQ_DESC_ADDR_REG, txq->descs_dma);
  3459. mvpp2_write(port->priv, MVPP2_TXQ_DESC_SIZE_REG, txq->size &
  3460. MVPP2_TXQ_DESC_SIZE_MASK);
  3461. mvpp2_write(port->priv, MVPP2_TXQ_INDEX_REG, 0);
  3462. mvpp2_write(port->priv, MVPP2_TXQ_RSVD_CLR_REG,
  3463. txq->id << MVPP2_TXQ_RSVD_CLR_OFFSET);
  3464. val = mvpp2_read(port->priv, MVPP2_TXQ_PENDING_REG);
  3465. val &= ~MVPP2_TXQ_PENDING_MASK;
  3466. mvpp2_write(port->priv, MVPP2_TXQ_PENDING_REG, val);
  3467. /* Calculate base address in prefetch buffer. We reserve 16 descriptors
  3468. * for each existing TXQ.
  3469. * TCONTS for PON port must be continuous from 0 to MVPP2_MAX_TCONT
  3470. * GBE ports assumed to be continious from 0 to MVPP2_MAX_PORTS
  3471. */
  3472. desc_per_txq = 16;
  3473. desc = (port->id * MVPP2_MAX_TXQ * desc_per_txq) +
  3474. (txq->log_id * desc_per_txq);
  3475. mvpp2_write(port->priv, MVPP2_TXQ_PREF_BUF_REG,
  3476. MVPP2_PREF_BUF_PTR(desc) | MVPP2_PREF_BUF_SIZE_16 |
  3477. MVPP2_PREF_BUF_THRESH(desc_per_txq / 2));
  3478. /* WRR / EJP configuration - indirect access */
  3479. tx_port_num = mvpp2_egress_port(port);
  3480. mvpp2_write(port->priv, MVPP2_TXP_SCHED_PORT_INDEX_REG, tx_port_num);
  3481. val = mvpp2_read(port->priv, MVPP2_TXQ_SCHED_REFILL_REG(txq->log_id));
  3482. val &= ~MVPP2_TXQ_REFILL_PERIOD_ALL_MASK;
  3483. val |= MVPP2_TXQ_REFILL_PERIOD_MASK(1);
  3484. val |= MVPP2_TXQ_REFILL_TOKENS_ALL_MASK;
  3485. mvpp2_write(port->priv, MVPP2_TXQ_SCHED_REFILL_REG(txq->log_id), val);
  3486. val = MVPP2_TXQ_TOKEN_SIZE_MAX;
  3487. mvpp2_write(port->priv, MVPP2_TXQ_SCHED_TOKEN_SIZE_REG(txq->log_id),
  3488. val);
  3489. for_each_present_cpu(cpu) {
  3490. txq_pcpu = per_cpu_ptr(txq->pcpu, cpu);
  3491. txq_pcpu->size = txq->size;
  3492. }
  3493. return 0;
  3494. }
  3495. /* Free allocated TXQ resources */
  3496. static void mvpp2_txq_deinit(struct mvpp2_port *port,
  3497. struct mvpp2_tx_queue *txq)
  3498. {
  3499. txq->descs = NULL;
  3500. txq->last_desc = 0;
  3501. txq->next_desc_to_proc = 0;
  3502. txq->descs_dma = 0;
  3503. /* Set minimum bandwidth for disabled TXQs */
  3504. mvpp2_write(port->priv, MVPP2_TXQ_SCHED_TOKEN_CNTR_REG(txq->id), 0);
  3505. /* Set Tx descriptors queue starting address and size */
  3506. mvpp2_write(port->priv, MVPP2_TXQ_NUM_REG, txq->id);
  3507. mvpp2_write(port->priv, MVPP2_TXQ_DESC_ADDR_REG, 0);
  3508. mvpp2_write(port->priv, MVPP2_TXQ_DESC_SIZE_REG, 0);
  3509. }
  3510. /* Cleanup Tx ports */
  3511. static void mvpp2_txq_clean(struct mvpp2_port *port, struct mvpp2_tx_queue *txq)
  3512. {
  3513. struct mvpp2_txq_pcpu *txq_pcpu;
  3514. int delay, pending, cpu;
  3515. u32 val;
  3516. mvpp2_write(port->priv, MVPP2_TXQ_NUM_REG, txq->id);
  3517. val = mvpp2_read(port->priv, MVPP2_TXQ_PREF_BUF_REG);
  3518. val |= MVPP2_TXQ_DRAIN_EN_MASK;
  3519. mvpp2_write(port->priv, MVPP2_TXQ_PREF_BUF_REG, val);
  3520. /* The napi queue has been stopped so wait for all packets
  3521. * to be transmitted.
  3522. */
  3523. delay = 0;
  3524. do {
  3525. if (delay >= MVPP2_TX_PENDING_TIMEOUT_MSEC) {
  3526. netdev_warn(port->dev,
  3527. "port %d: cleaning queue %d timed out\n",
  3528. port->id, txq->log_id);
  3529. break;
  3530. }
  3531. mdelay(1);
  3532. delay++;
  3533. pending = mvpp2_txq_pend_desc_num_get(port, txq);
  3534. } while (pending);
  3535. val &= ~MVPP2_TXQ_DRAIN_EN_MASK;
  3536. mvpp2_write(port->priv, MVPP2_TXQ_PREF_BUF_REG, val);
  3537. for_each_present_cpu(cpu) {
  3538. txq_pcpu = per_cpu_ptr(txq->pcpu, cpu);
  3539. /* Release all packets */
  3540. mvpp2_txq_bufs_free(port, txq, txq_pcpu, txq_pcpu->count);
  3541. /* Reset queue */
  3542. txq_pcpu->count = 0;
  3543. txq_pcpu->txq_put_index = 0;
  3544. txq_pcpu->txq_get_index = 0;
  3545. }
  3546. }
  3547. /* Cleanup all Tx queues */
  3548. static void mvpp2_cleanup_txqs(struct mvpp2_port *port)
  3549. {
  3550. struct mvpp2_tx_queue *txq;
  3551. int queue;
  3552. u32 val;
  3553. val = mvpp2_read(port->priv, MVPP2_TX_PORT_FLUSH_REG);
  3554. /* Reset Tx ports and delete Tx queues */
  3555. val |= MVPP2_TX_PORT_FLUSH_MASK(port->id);
  3556. mvpp2_write(port->priv, MVPP2_TX_PORT_FLUSH_REG, val);
  3557. for (queue = 0; queue < txq_number; queue++) {
  3558. txq = port->txqs[queue];
  3559. mvpp2_txq_clean(port, txq);
  3560. mvpp2_txq_deinit(port, txq);
  3561. }
  3562. mvpp2_txq_sent_counter_clear(port);
  3563. val &= ~MVPP2_TX_PORT_FLUSH_MASK(port->id);
  3564. mvpp2_write(port->priv, MVPP2_TX_PORT_FLUSH_REG, val);
  3565. }
  3566. /* Cleanup all Rx queues */
  3567. static void mvpp2_cleanup_rxqs(struct mvpp2_port *port)
  3568. {
  3569. int queue;
  3570. for (queue = 0; queue < rxq_number; queue++)
  3571. mvpp2_rxq_deinit(port, port->rxqs[queue]);
  3572. }
  3573. /* Init all Rx queues for port */
  3574. static int mvpp2_setup_rxqs(struct mvpp2_port *port)
  3575. {
  3576. int queue, err;
  3577. for (queue = 0; queue < rxq_number; queue++) {
  3578. err = mvpp2_rxq_init(port, port->rxqs[queue]);
  3579. if (err)
  3580. goto err_cleanup;
  3581. }
  3582. return 0;
  3583. err_cleanup:
  3584. mvpp2_cleanup_rxqs(port);
  3585. return err;
  3586. }
  3587. /* Init all tx queues for port */
  3588. static int mvpp2_setup_txqs(struct mvpp2_port *port)
  3589. {
  3590. struct mvpp2_tx_queue *txq;
  3591. int queue, err;
  3592. for (queue = 0; queue < txq_number; queue++) {
  3593. txq = port->txqs[queue];
  3594. err = mvpp2_txq_init(port, txq);
  3595. if (err)
  3596. goto err_cleanup;
  3597. }
  3598. mvpp2_txq_sent_counter_clear(port);
  3599. return 0;
  3600. err_cleanup:
  3601. mvpp2_cleanup_txqs(port);
  3602. return err;
  3603. }
  3604. /* Adjust link */
  3605. static void mvpp2_link_event(struct mvpp2_port *port)
  3606. {
  3607. struct phy_device *phydev = port->phy_dev;
  3608. int status_change = 0;
  3609. u32 val;
  3610. if (phydev->link) {
  3611. if ((port->speed != phydev->speed) ||
  3612. (port->duplex != phydev->duplex)) {
  3613. u32 val;
  3614. val = readl(port->base + MVPP2_GMAC_AUTONEG_CONFIG);
  3615. val &= ~(MVPP2_GMAC_CONFIG_MII_SPEED |
  3616. MVPP2_GMAC_CONFIG_GMII_SPEED |
  3617. MVPP2_GMAC_CONFIG_FULL_DUPLEX |
  3618. MVPP2_GMAC_AN_SPEED_EN |
  3619. MVPP2_GMAC_AN_DUPLEX_EN);
  3620. if (phydev->duplex)
  3621. val |= MVPP2_GMAC_CONFIG_FULL_DUPLEX;
  3622. if (phydev->speed == SPEED_1000)
  3623. val |= MVPP2_GMAC_CONFIG_GMII_SPEED;
  3624. else if (phydev->speed == SPEED_100)
  3625. val |= MVPP2_GMAC_CONFIG_MII_SPEED;
  3626. writel(val, port->base + MVPP2_GMAC_AUTONEG_CONFIG);
  3627. port->duplex = phydev->duplex;
  3628. port->speed = phydev->speed;
  3629. }
  3630. }
  3631. if (phydev->link != port->link) {
  3632. if (!phydev->link) {
  3633. port->duplex = -1;
  3634. port->speed = 0;
  3635. }
  3636. port->link = phydev->link;
  3637. status_change = 1;
  3638. }
  3639. if (status_change) {
  3640. if (phydev->link) {
  3641. val = readl(port->base + MVPP2_GMAC_AUTONEG_CONFIG);
  3642. val |= (MVPP2_GMAC_FORCE_LINK_PASS |
  3643. MVPP2_GMAC_FORCE_LINK_DOWN);
  3644. writel(val, port->base + MVPP2_GMAC_AUTONEG_CONFIG);
  3645. mvpp2_egress_enable(port);
  3646. mvpp2_ingress_enable(port);
  3647. } else {
  3648. mvpp2_ingress_disable(port);
  3649. mvpp2_egress_disable(port);
  3650. }
  3651. }
  3652. }
  3653. /* Main RX/TX processing routines */
  3654. /* Display more error info */
  3655. static void mvpp2_rx_error(struct mvpp2_port *port,
  3656. struct mvpp2_rx_desc *rx_desc)
  3657. {
  3658. u32 status = mvpp2_rxdesc_status_get(port, rx_desc);
  3659. size_t sz = mvpp2_rxdesc_size_get(port, rx_desc);
  3660. switch (status & MVPP2_RXD_ERR_CODE_MASK) {
  3661. case MVPP2_RXD_ERR_CRC:
  3662. netdev_err(port->dev, "bad rx status %08x (crc error), size=%zu\n",
  3663. status, sz);
  3664. break;
  3665. case MVPP2_RXD_ERR_OVERRUN:
  3666. netdev_err(port->dev, "bad rx status %08x (overrun error), size=%zu\n",
  3667. status, sz);
  3668. break;
  3669. case MVPP2_RXD_ERR_RESOURCE:
  3670. netdev_err(port->dev, "bad rx status %08x (resource error), size=%zu\n",
  3671. status, sz);
  3672. break;
  3673. }
  3674. }
  3675. /* Reuse skb if possible, or allocate a new skb and add it to BM pool */
  3676. static int mvpp2_rx_refill(struct mvpp2_port *port,
  3677. struct mvpp2_bm_pool *bm_pool,
  3678. u32 bm, dma_addr_t dma_addr)
  3679. {
  3680. mvpp2_pool_refill(port, bm, dma_addr, (unsigned long)dma_addr);
  3681. return 0;
  3682. }
  3683. /* Set hw internals when starting port */
  3684. static void mvpp2_start_dev(struct mvpp2_port *port)
  3685. {
  3686. mvpp2_gmac_max_rx_size_set(port);
  3687. mvpp2_txp_max_tx_size_set(port);
  3688. if (port->priv->hw_version == MVPP21)
  3689. mvpp2_port_enable(port);
  3690. else
  3691. gop_port_enable(port, 1);
  3692. }
  3693. /* Set hw internals when stopping port */
  3694. static void mvpp2_stop_dev(struct mvpp2_port *port)
  3695. {
  3696. /* Stop new packets from arriving to RXQs */
  3697. mvpp2_ingress_disable(port);
  3698. mvpp2_egress_disable(port);
  3699. if (port->priv->hw_version == MVPP21)
  3700. mvpp2_port_disable(port);
  3701. else
  3702. gop_port_enable(port, 0);
  3703. }
  3704. static int mvpp2_phy_connect(struct udevice *dev, struct mvpp2_port *port)
  3705. {
  3706. struct phy_device *phy_dev;
  3707. if (!port->init || port->link == 0) {
  3708. phy_dev = phy_connect(port->priv->bus, port->phyaddr, dev,
  3709. port->phy_interface);
  3710. port->phy_dev = phy_dev;
  3711. if (!phy_dev) {
  3712. netdev_err(port->dev, "cannot connect to phy\n");
  3713. return -ENODEV;
  3714. }
  3715. phy_dev->supported &= PHY_GBIT_FEATURES;
  3716. phy_dev->advertising = phy_dev->supported;
  3717. port->phy_dev = phy_dev;
  3718. port->link = 0;
  3719. port->duplex = 0;
  3720. port->speed = 0;
  3721. phy_config(phy_dev);
  3722. phy_startup(phy_dev);
  3723. if (!phy_dev->link) {
  3724. printf("%s: No link\n", phy_dev->dev->name);
  3725. return -1;
  3726. }
  3727. port->init = 1;
  3728. } else {
  3729. mvpp2_egress_enable(port);
  3730. mvpp2_ingress_enable(port);
  3731. }
  3732. return 0;
  3733. }
  3734. static int mvpp2_open(struct udevice *dev, struct mvpp2_port *port)
  3735. {
  3736. unsigned char mac_bcast[ETH_ALEN] = {
  3737. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
  3738. int err;
  3739. err = mvpp2_prs_mac_da_accept(port->priv, port->id, mac_bcast, true);
  3740. if (err) {
  3741. netdev_err(dev, "mvpp2_prs_mac_da_accept BC failed\n");
  3742. return err;
  3743. }
  3744. err = mvpp2_prs_mac_da_accept(port->priv, port->id,
  3745. port->dev_addr, true);
  3746. if (err) {
  3747. netdev_err(dev, "mvpp2_prs_mac_da_accept MC failed\n");
  3748. return err;
  3749. }
  3750. err = mvpp2_prs_def_flow(port);
  3751. if (err) {
  3752. netdev_err(dev, "mvpp2_prs_def_flow failed\n");
  3753. return err;
  3754. }
  3755. /* Allocate the Rx/Tx queues */
  3756. err = mvpp2_setup_rxqs(port);
  3757. if (err) {
  3758. netdev_err(port->dev, "cannot allocate Rx queues\n");
  3759. return err;
  3760. }
  3761. err = mvpp2_setup_txqs(port);
  3762. if (err) {
  3763. netdev_err(port->dev, "cannot allocate Tx queues\n");
  3764. return err;
  3765. }
  3766. err = mvpp2_phy_connect(dev, port);
  3767. if (err < 0)
  3768. return err;
  3769. mvpp2_link_event(port);
  3770. mvpp2_start_dev(port);
  3771. return 0;
  3772. }
  3773. /* No Device ops here in U-Boot */
  3774. /* Driver initialization */
  3775. static void mvpp2_port_power_up(struct mvpp2_port *port)
  3776. {
  3777. struct mvpp2 *priv = port->priv;
  3778. /* On PPv2.2 the GoP / interface configuration has already been done */
  3779. if (priv->hw_version == MVPP21)
  3780. mvpp2_port_mii_set(port);
  3781. mvpp2_port_periodic_xon_disable(port);
  3782. if (priv->hw_version == MVPP21)
  3783. mvpp2_port_fc_adv_enable(port);
  3784. mvpp2_port_reset(port);
  3785. }
  3786. /* Initialize port HW */
  3787. static int mvpp2_port_init(struct udevice *dev, struct mvpp2_port *port)
  3788. {
  3789. struct mvpp2 *priv = port->priv;
  3790. struct mvpp2_txq_pcpu *txq_pcpu;
  3791. int queue, cpu, err;
  3792. if (port->first_rxq + rxq_number >
  3793. MVPP2_MAX_PORTS * priv->max_port_rxqs)
  3794. return -EINVAL;
  3795. /* Disable port */
  3796. mvpp2_egress_disable(port);
  3797. if (priv->hw_version == MVPP21)
  3798. mvpp2_port_disable(port);
  3799. else
  3800. gop_port_enable(port, 0);
  3801. port->txqs = devm_kcalloc(dev, txq_number, sizeof(*port->txqs),
  3802. GFP_KERNEL);
  3803. if (!port->txqs)
  3804. return -ENOMEM;
  3805. /* Associate physical Tx queues to this port and initialize.
  3806. * The mapping is predefined.
  3807. */
  3808. for (queue = 0; queue < txq_number; queue++) {
  3809. int queue_phy_id = mvpp2_txq_phys(port->id, queue);
  3810. struct mvpp2_tx_queue *txq;
  3811. txq = devm_kzalloc(dev, sizeof(*txq), GFP_KERNEL);
  3812. if (!txq)
  3813. return -ENOMEM;
  3814. txq->pcpu = devm_kzalloc(dev, sizeof(struct mvpp2_txq_pcpu),
  3815. GFP_KERNEL);
  3816. if (!txq->pcpu)
  3817. return -ENOMEM;
  3818. txq->id = queue_phy_id;
  3819. txq->log_id = queue;
  3820. txq->done_pkts_coal = MVPP2_TXDONE_COAL_PKTS_THRESH;
  3821. for_each_present_cpu(cpu) {
  3822. txq_pcpu = per_cpu_ptr(txq->pcpu, cpu);
  3823. txq_pcpu->cpu = cpu;
  3824. }
  3825. port->txqs[queue] = txq;
  3826. }
  3827. port->rxqs = devm_kcalloc(dev, rxq_number, sizeof(*port->rxqs),
  3828. GFP_KERNEL);
  3829. if (!port->rxqs)
  3830. return -ENOMEM;
  3831. /* Allocate and initialize Rx queue for this port */
  3832. for (queue = 0; queue < rxq_number; queue++) {
  3833. struct mvpp2_rx_queue *rxq;
  3834. /* Map physical Rx queue to port's logical Rx queue */
  3835. rxq = devm_kzalloc(dev, sizeof(*rxq), GFP_KERNEL);
  3836. if (!rxq)
  3837. return -ENOMEM;
  3838. /* Map this Rx queue to a physical queue */
  3839. rxq->id = port->first_rxq + queue;
  3840. rxq->port = port->id;
  3841. rxq->logic_rxq = queue;
  3842. port->rxqs[queue] = rxq;
  3843. }
  3844. /* Configure Rx queue group interrupt for this port */
  3845. if (priv->hw_version == MVPP21) {
  3846. mvpp2_write(priv, MVPP21_ISR_RXQ_GROUP_REG(port->id),
  3847. CONFIG_MV_ETH_RXQ);
  3848. } else {
  3849. u32 val;
  3850. val = (port->id << MVPP22_ISR_RXQ_GROUP_INDEX_GROUP_OFFSET);
  3851. mvpp2_write(priv, MVPP22_ISR_RXQ_GROUP_INDEX_REG, val);
  3852. val = (CONFIG_MV_ETH_RXQ <<
  3853. MVPP22_ISR_RXQ_SUB_GROUP_SIZE_OFFSET);
  3854. mvpp2_write(priv, MVPP22_ISR_RXQ_SUB_GROUP_CONFIG_REG, val);
  3855. }
  3856. /* Create Rx descriptor rings */
  3857. for (queue = 0; queue < rxq_number; queue++) {
  3858. struct mvpp2_rx_queue *rxq = port->rxqs[queue];
  3859. rxq->size = port->rx_ring_size;
  3860. rxq->pkts_coal = MVPP2_RX_COAL_PKTS;
  3861. rxq->time_coal = MVPP2_RX_COAL_USEC;
  3862. }
  3863. mvpp2_ingress_disable(port);
  3864. /* Port default configuration */
  3865. mvpp2_defaults_set(port);
  3866. /* Port's classifier configuration */
  3867. mvpp2_cls_oversize_rxq_set(port);
  3868. mvpp2_cls_port_config(port);
  3869. /* Provide an initial Rx packet size */
  3870. port->pkt_size = MVPP2_RX_PKT_SIZE(PKTSIZE_ALIGN);
  3871. /* Initialize pools for swf */
  3872. err = mvpp2_swf_bm_pool_init(port);
  3873. if (err)
  3874. return err;
  3875. return 0;
  3876. }
  3877. static int phy_info_parse(struct udevice *dev, struct mvpp2_port *port)
  3878. {
  3879. int port_node = dev_of_offset(dev);
  3880. const char *phy_mode_str;
  3881. int phy_node;
  3882. u32 id;
  3883. u32 phyaddr;
  3884. int phy_mode = -1;
  3885. phy_node = fdtdec_lookup_phandle(gd->fdt_blob, port_node, "phy");
  3886. if (phy_node < 0) {
  3887. dev_err(&pdev->dev, "missing phy\n");
  3888. return -ENODEV;
  3889. }
  3890. phy_mode_str = fdt_getprop(gd->fdt_blob, port_node, "phy-mode", NULL);
  3891. if (phy_mode_str)
  3892. phy_mode = phy_get_interface_by_name(phy_mode_str);
  3893. if (phy_mode == -1) {
  3894. dev_err(&pdev->dev, "incorrect phy mode\n");
  3895. return -EINVAL;
  3896. }
  3897. id = fdtdec_get_int(gd->fdt_blob, port_node, "port-id", -1);
  3898. if (id == -1) {
  3899. dev_err(&pdev->dev, "missing port-id value\n");
  3900. return -EINVAL;
  3901. }
  3902. /*
  3903. * ToDo:
  3904. * Not sure if this DT property "phy-speed" will get accepted, so
  3905. * this might change later
  3906. */
  3907. /* Get phy-speed for SGMII 2.5Gbps vs 1Gbps setup */
  3908. port->phy_speed = fdtdec_get_int(gd->fdt_blob, port_node,
  3909. "phy-speed", 1000);
  3910. phyaddr = fdtdec_get_int(gd->fdt_blob, phy_node, "reg", 0);
  3911. port->id = id;
  3912. if (port->priv->hw_version == MVPP21)
  3913. port->first_rxq = port->id * rxq_number;
  3914. else
  3915. port->first_rxq = port->id * port->priv->max_port_rxqs;
  3916. port->phy_node = phy_node;
  3917. port->phy_interface = phy_mode;
  3918. port->phyaddr = phyaddr;
  3919. return 0;
  3920. }
  3921. /* Ports initialization */
  3922. static int mvpp2_port_probe(struct udevice *dev,
  3923. struct mvpp2_port *port,
  3924. int port_node,
  3925. struct mvpp2 *priv)
  3926. {
  3927. int err;
  3928. port->tx_ring_size = MVPP2_MAX_TXD;
  3929. port->rx_ring_size = MVPP2_MAX_RXD;
  3930. err = mvpp2_port_init(dev, port);
  3931. if (err < 0) {
  3932. dev_err(&pdev->dev, "failed to init port %d\n", port->id);
  3933. return err;
  3934. }
  3935. mvpp2_port_power_up(port);
  3936. priv->port_list[port->id] = port;
  3937. return 0;
  3938. }
  3939. /* Initialize decoding windows */
  3940. static void mvpp2_conf_mbus_windows(const struct mbus_dram_target_info *dram,
  3941. struct mvpp2 *priv)
  3942. {
  3943. u32 win_enable;
  3944. int i;
  3945. for (i = 0; i < 6; i++) {
  3946. mvpp2_write(priv, MVPP2_WIN_BASE(i), 0);
  3947. mvpp2_write(priv, MVPP2_WIN_SIZE(i), 0);
  3948. if (i < 4)
  3949. mvpp2_write(priv, MVPP2_WIN_REMAP(i), 0);
  3950. }
  3951. win_enable = 0;
  3952. for (i = 0; i < dram->num_cs; i++) {
  3953. const struct mbus_dram_window *cs = dram->cs + i;
  3954. mvpp2_write(priv, MVPP2_WIN_BASE(i),
  3955. (cs->base & 0xffff0000) | (cs->mbus_attr << 8) |
  3956. dram->mbus_dram_target_id);
  3957. mvpp2_write(priv, MVPP2_WIN_SIZE(i),
  3958. (cs->size - 1) & 0xffff0000);
  3959. win_enable |= (1 << i);
  3960. }
  3961. mvpp2_write(priv, MVPP2_BASE_ADDR_ENABLE, win_enable);
  3962. }
  3963. /* Initialize Rx FIFO's */
  3964. static void mvpp2_rx_fifo_init(struct mvpp2 *priv)
  3965. {
  3966. int port;
  3967. for (port = 0; port < MVPP2_MAX_PORTS; port++) {
  3968. if (priv->hw_version == MVPP22) {
  3969. if (port == 0) {
  3970. mvpp2_write(priv,
  3971. MVPP2_RX_DATA_FIFO_SIZE_REG(port),
  3972. MVPP22_RX_FIFO_10GB_PORT_DATA_SIZE);
  3973. mvpp2_write(priv,
  3974. MVPP2_RX_ATTR_FIFO_SIZE_REG(port),
  3975. MVPP22_RX_FIFO_10GB_PORT_ATTR_SIZE);
  3976. } else if (port == 1) {
  3977. mvpp2_write(priv,
  3978. MVPP2_RX_DATA_FIFO_SIZE_REG(port),
  3979. MVPP22_RX_FIFO_2_5GB_PORT_DATA_SIZE);
  3980. mvpp2_write(priv,
  3981. MVPP2_RX_ATTR_FIFO_SIZE_REG(port),
  3982. MVPP22_RX_FIFO_2_5GB_PORT_ATTR_SIZE);
  3983. } else {
  3984. mvpp2_write(priv,
  3985. MVPP2_RX_DATA_FIFO_SIZE_REG(port),
  3986. MVPP22_RX_FIFO_1GB_PORT_DATA_SIZE);
  3987. mvpp2_write(priv,
  3988. MVPP2_RX_ATTR_FIFO_SIZE_REG(port),
  3989. MVPP22_RX_FIFO_1GB_PORT_ATTR_SIZE);
  3990. }
  3991. } else {
  3992. mvpp2_write(priv, MVPP2_RX_DATA_FIFO_SIZE_REG(port),
  3993. MVPP21_RX_FIFO_PORT_DATA_SIZE);
  3994. mvpp2_write(priv, MVPP2_RX_ATTR_FIFO_SIZE_REG(port),
  3995. MVPP21_RX_FIFO_PORT_ATTR_SIZE);
  3996. }
  3997. }
  3998. mvpp2_write(priv, MVPP2_RX_MIN_PKT_SIZE_REG,
  3999. MVPP2_RX_FIFO_PORT_MIN_PKT);
  4000. mvpp2_write(priv, MVPP2_RX_FIFO_INIT_REG, 0x1);
  4001. }
  4002. /* Initialize Tx FIFO's */
  4003. static void mvpp2_tx_fifo_init(struct mvpp2 *priv)
  4004. {
  4005. int port, val;
  4006. for (port = 0; port < MVPP2_MAX_PORTS; port++) {
  4007. /* Port 0 supports 10KB TX FIFO */
  4008. if (port == 0) {
  4009. val = MVPP2_TX_FIFO_DATA_SIZE_10KB &
  4010. MVPP22_TX_FIFO_SIZE_MASK;
  4011. } else {
  4012. val = MVPP2_TX_FIFO_DATA_SIZE_3KB &
  4013. MVPP22_TX_FIFO_SIZE_MASK;
  4014. }
  4015. mvpp2_write(priv, MVPP22_TX_FIFO_SIZE_REG(port), val);
  4016. }
  4017. }
  4018. static void mvpp2_axi_init(struct mvpp2 *priv)
  4019. {
  4020. u32 val, rdval, wrval;
  4021. mvpp2_write(priv, MVPP22_BM_ADDR_HIGH_RLS_REG, 0x0);
  4022. /* AXI Bridge Configuration */
  4023. rdval = MVPP22_AXI_CODE_CACHE_RD_CACHE
  4024. << MVPP22_AXI_ATTR_CACHE_OFFS;
  4025. rdval |= MVPP22_AXI_CODE_DOMAIN_OUTER_DOM
  4026. << MVPP22_AXI_ATTR_DOMAIN_OFFS;
  4027. wrval = MVPP22_AXI_CODE_CACHE_WR_CACHE
  4028. << MVPP22_AXI_ATTR_CACHE_OFFS;
  4029. wrval |= MVPP22_AXI_CODE_DOMAIN_OUTER_DOM
  4030. << MVPP22_AXI_ATTR_DOMAIN_OFFS;
  4031. /* BM */
  4032. mvpp2_write(priv, MVPP22_AXI_BM_WR_ATTR_REG, wrval);
  4033. mvpp2_write(priv, MVPP22_AXI_BM_RD_ATTR_REG, rdval);
  4034. /* Descriptors */
  4035. mvpp2_write(priv, MVPP22_AXI_AGGRQ_DESCR_RD_ATTR_REG, rdval);
  4036. mvpp2_write(priv, MVPP22_AXI_TXQ_DESCR_WR_ATTR_REG, wrval);
  4037. mvpp2_write(priv, MVPP22_AXI_TXQ_DESCR_RD_ATTR_REG, rdval);
  4038. mvpp2_write(priv, MVPP22_AXI_RXQ_DESCR_WR_ATTR_REG, wrval);
  4039. /* Buffer Data */
  4040. mvpp2_write(priv, MVPP22_AXI_TX_DATA_RD_ATTR_REG, rdval);
  4041. mvpp2_write(priv, MVPP22_AXI_RX_DATA_WR_ATTR_REG, wrval);
  4042. val = MVPP22_AXI_CODE_CACHE_NON_CACHE
  4043. << MVPP22_AXI_CODE_CACHE_OFFS;
  4044. val |= MVPP22_AXI_CODE_DOMAIN_SYSTEM
  4045. << MVPP22_AXI_CODE_DOMAIN_OFFS;
  4046. mvpp2_write(priv, MVPP22_AXI_RD_NORMAL_CODE_REG, val);
  4047. mvpp2_write(priv, MVPP22_AXI_WR_NORMAL_CODE_REG, val);
  4048. val = MVPP22_AXI_CODE_CACHE_RD_CACHE
  4049. << MVPP22_AXI_CODE_CACHE_OFFS;
  4050. val |= MVPP22_AXI_CODE_DOMAIN_OUTER_DOM
  4051. << MVPP22_AXI_CODE_DOMAIN_OFFS;
  4052. mvpp2_write(priv, MVPP22_AXI_RD_SNOOP_CODE_REG, val);
  4053. val = MVPP22_AXI_CODE_CACHE_WR_CACHE
  4054. << MVPP22_AXI_CODE_CACHE_OFFS;
  4055. val |= MVPP22_AXI_CODE_DOMAIN_OUTER_DOM
  4056. << MVPP22_AXI_CODE_DOMAIN_OFFS;
  4057. mvpp2_write(priv, MVPP22_AXI_WR_SNOOP_CODE_REG, val);
  4058. }
  4059. /* Initialize network controller common part HW */
  4060. static int mvpp2_init(struct udevice *dev, struct mvpp2 *priv)
  4061. {
  4062. const struct mbus_dram_target_info *dram_target_info;
  4063. int err, i;
  4064. u32 val;
  4065. /* Checks for hardware constraints (U-Boot uses only one rxq) */
  4066. if ((rxq_number > priv->max_port_rxqs) ||
  4067. (txq_number > MVPP2_MAX_TXQ)) {
  4068. dev_err(&pdev->dev, "invalid queue size parameter\n");
  4069. return -EINVAL;
  4070. }
  4071. /* MBUS windows configuration */
  4072. dram_target_info = mvebu_mbus_dram_info();
  4073. if (dram_target_info)
  4074. mvpp2_conf_mbus_windows(dram_target_info, priv);
  4075. if (priv->hw_version == MVPP22)
  4076. mvpp2_axi_init(priv);
  4077. if (priv->hw_version == MVPP21) {
  4078. /* Disable HW PHY polling */
  4079. val = readl(priv->lms_base + MVPP2_PHY_AN_CFG0_REG);
  4080. val |= MVPP2_PHY_AN_STOP_SMI0_MASK;
  4081. writel(val, priv->lms_base + MVPP2_PHY_AN_CFG0_REG);
  4082. } else {
  4083. /* Enable HW PHY polling */
  4084. val = readl(priv->iface_base + MVPP22_SMI_MISC_CFG_REG);
  4085. val |= MVPP22_SMI_POLLING_EN;
  4086. writel(val, priv->iface_base + MVPP22_SMI_MISC_CFG_REG);
  4087. }
  4088. /* Allocate and initialize aggregated TXQs */
  4089. priv->aggr_txqs = devm_kcalloc(dev, num_present_cpus(),
  4090. sizeof(struct mvpp2_tx_queue),
  4091. GFP_KERNEL);
  4092. if (!priv->aggr_txqs)
  4093. return -ENOMEM;
  4094. for_each_present_cpu(i) {
  4095. priv->aggr_txqs[i].id = i;
  4096. priv->aggr_txqs[i].size = MVPP2_AGGR_TXQ_SIZE;
  4097. err = mvpp2_aggr_txq_init(dev, &priv->aggr_txqs[i],
  4098. MVPP2_AGGR_TXQ_SIZE, i, priv);
  4099. if (err < 0)
  4100. return err;
  4101. }
  4102. /* Rx Fifo Init */
  4103. mvpp2_rx_fifo_init(priv);
  4104. /* Tx Fifo Init */
  4105. if (priv->hw_version == MVPP22)
  4106. mvpp2_tx_fifo_init(priv);
  4107. /* Reset Rx queue group interrupt configuration */
  4108. for (i = 0; i < MVPP2_MAX_PORTS; i++) {
  4109. if (priv->hw_version == MVPP21) {
  4110. mvpp2_write(priv, MVPP21_ISR_RXQ_GROUP_REG(i),
  4111. CONFIG_MV_ETH_RXQ);
  4112. continue;
  4113. } else {
  4114. u32 val;
  4115. val = (i << MVPP22_ISR_RXQ_GROUP_INDEX_GROUP_OFFSET);
  4116. mvpp2_write(priv, MVPP22_ISR_RXQ_GROUP_INDEX_REG, val);
  4117. val = (CONFIG_MV_ETH_RXQ <<
  4118. MVPP22_ISR_RXQ_SUB_GROUP_SIZE_OFFSET);
  4119. mvpp2_write(priv,
  4120. MVPP22_ISR_RXQ_SUB_GROUP_CONFIG_REG, val);
  4121. }
  4122. }
  4123. if (priv->hw_version == MVPP21)
  4124. writel(MVPP2_EXT_GLOBAL_CTRL_DEFAULT,
  4125. priv->lms_base + MVPP2_MNG_EXTENDED_GLOBAL_CTRL_REG);
  4126. /* Allow cache snoop when transmiting packets */
  4127. mvpp2_write(priv, MVPP2_TX_SNOOP_REG, 0x1);
  4128. /* Buffer Manager initialization */
  4129. err = mvpp2_bm_init(dev, priv);
  4130. if (err < 0)
  4131. return err;
  4132. /* Parser default initialization */
  4133. err = mvpp2_prs_default_init(dev, priv);
  4134. if (err < 0)
  4135. return err;
  4136. /* Classifier default initialization */
  4137. mvpp2_cls_init(priv);
  4138. return 0;
  4139. }
  4140. /* SMI / MDIO functions */
  4141. static int smi_wait_ready(struct mvpp2 *priv)
  4142. {
  4143. u32 timeout = MVPP2_SMI_TIMEOUT;
  4144. u32 smi_reg;
  4145. /* wait till the SMI is not busy */
  4146. do {
  4147. /* read smi register */
  4148. smi_reg = readl(priv->mdio_base);
  4149. if (timeout-- == 0) {
  4150. printf("Error: SMI busy timeout\n");
  4151. return -EFAULT;
  4152. }
  4153. } while (smi_reg & MVPP2_SMI_BUSY);
  4154. return 0;
  4155. }
  4156. /*
  4157. * mpp2_mdio_read - miiphy_read callback function.
  4158. *
  4159. * Returns 16bit phy register value, or 0xffff on error
  4160. */
  4161. static int mpp2_mdio_read(struct mii_dev *bus, int addr, int devad, int reg)
  4162. {
  4163. struct mvpp2 *priv = bus->priv;
  4164. u32 smi_reg;
  4165. u32 timeout;
  4166. /* check parameters */
  4167. if (addr > MVPP2_PHY_ADDR_MASK) {
  4168. printf("Error: Invalid PHY address %d\n", addr);
  4169. return -EFAULT;
  4170. }
  4171. if (reg > MVPP2_PHY_REG_MASK) {
  4172. printf("Err: Invalid register offset %d\n", reg);
  4173. return -EFAULT;
  4174. }
  4175. /* wait till the SMI is not busy */
  4176. if (smi_wait_ready(priv) < 0)
  4177. return -EFAULT;
  4178. /* fill the phy address and regiser offset and read opcode */
  4179. smi_reg = (addr << MVPP2_SMI_DEV_ADDR_OFFS)
  4180. | (reg << MVPP2_SMI_REG_ADDR_OFFS)
  4181. | MVPP2_SMI_OPCODE_READ;
  4182. /* write the smi register */
  4183. writel(smi_reg, priv->mdio_base);
  4184. /* wait till read value is ready */
  4185. timeout = MVPP2_SMI_TIMEOUT;
  4186. do {
  4187. /* read smi register */
  4188. smi_reg = readl(priv->mdio_base);
  4189. if (timeout-- == 0) {
  4190. printf("Err: SMI read ready timeout\n");
  4191. return -EFAULT;
  4192. }
  4193. } while (!(smi_reg & MVPP2_SMI_READ_VALID));
  4194. /* Wait for the data to update in the SMI register */
  4195. for (timeout = 0; timeout < MVPP2_SMI_TIMEOUT; timeout++)
  4196. ;
  4197. return readl(priv->mdio_base) & MVPP2_SMI_DATA_MASK;
  4198. }
  4199. /*
  4200. * mpp2_mdio_write - miiphy_write callback function.
  4201. *
  4202. * Returns 0 if write succeed, -EINVAL on bad parameters
  4203. * -ETIME on timeout
  4204. */
  4205. static int mpp2_mdio_write(struct mii_dev *bus, int addr, int devad, int reg,
  4206. u16 value)
  4207. {
  4208. struct mvpp2 *priv = bus->priv;
  4209. u32 smi_reg;
  4210. /* check parameters */
  4211. if (addr > MVPP2_PHY_ADDR_MASK) {
  4212. printf("Error: Invalid PHY address %d\n", addr);
  4213. return -EFAULT;
  4214. }
  4215. if (reg > MVPP2_PHY_REG_MASK) {
  4216. printf("Err: Invalid register offset %d\n", reg);
  4217. return -EFAULT;
  4218. }
  4219. /* wait till the SMI is not busy */
  4220. if (smi_wait_ready(priv) < 0)
  4221. return -EFAULT;
  4222. /* fill the phy addr and reg offset and write opcode and data */
  4223. smi_reg = value << MVPP2_SMI_DATA_OFFS;
  4224. smi_reg |= (addr << MVPP2_SMI_DEV_ADDR_OFFS)
  4225. | (reg << MVPP2_SMI_REG_ADDR_OFFS);
  4226. smi_reg &= ~MVPP2_SMI_OPCODE_READ;
  4227. /* write the smi register */
  4228. writel(smi_reg, priv->mdio_base);
  4229. return 0;
  4230. }
  4231. static int mvpp2_recv(struct udevice *dev, int flags, uchar **packetp)
  4232. {
  4233. struct mvpp2_port *port = dev_get_priv(dev);
  4234. struct mvpp2_rx_desc *rx_desc;
  4235. struct mvpp2_bm_pool *bm_pool;
  4236. dma_addr_t dma_addr;
  4237. u32 bm, rx_status;
  4238. int pool, rx_bytes, err;
  4239. int rx_received;
  4240. struct mvpp2_rx_queue *rxq;
  4241. u32 cause_rx_tx, cause_rx, cause_misc;
  4242. u8 *data;
  4243. cause_rx_tx = mvpp2_read(port->priv,
  4244. MVPP2_ISR_RX_TX_CAUSE_REG(port->id));
  4245. cause_rx_tx &= ~MVPP2_CAUSE_TXQ_OCCUP_DESC_ALL_MASK;
  4246. cause_misc = cause_rx_tx & MVPP2_CAUSE_MISC_SUM_MASK;
  4247. if (!cause_rx_tx && !cause_misc)
  4248. return 0;
  4249. cause_rx = cause_rx_tx & MVPP2_CAUSE_RXQ_OCCUP_DESC_ALL_MASK;
  4250. /* Process RX packets */
  4251. cause_rx |= port->pending_cause_rx;
  4252. rxq = mvpp2_get_rx_queue(port, cause_rx);
  4253. /* Get number of received packets and clamp the to-do */
  4254. rx_received = mvpp2_rxq_received(port, rxq->id);
  4255. /* Return if no packets are received */
  4256. if (!rx_received)
  4257. return 0;
  4258. rx_desc = mvpp2_rxq_next_desc_get(rxq);
  4259. rx_status = mvpp2_rxdesc_status_get(port, rx_desc);
  4260. rx_bytes = mvpp2_rxdesc_size_get(port, rx_desc);
  4261. rx_bytes -= MVPP2_MH_SIZE;
  4262. dma_addr = mvpp2_rxdesc_dma_addr_get(port, rx_desc);
  4263. bm = mvpp2_bm_cookie_build(port, rx_desc);
  4264. pool = mvpp2_bm_cookie_pool_get(bm);
  4265. bm_pool = &port->priv->bm_pools[pool];
  4266. /* In case of an error, release the requested buffer pointer
  4267. * to the Buffer Manager. This request process is controlled
  4268. * by the hardware, and the information about the buffer is
  4269. * comprised by the RX descriptor.
  4270. */
  4271. if (rx_status & MVPP2_RXD_ERR_SUMMARY) {
  4272. mvpp2_rx_error(port, rx_desc);
  4273. /* Return the buffer to the pool */
  4274. mvpp2_pool_refill(port, bm, dma_addr, dma_addr);
  4275. return 0;
  4276. }
  4277. err = mvpp2_rx_refill(port, bm_pool, bm, dma_addr);
  4278. if (err) {
  4279. netdev_err(port->dev, "failed to refill BM pools\n");
  4280. return 0;
  4281. }
  4282. /* Update Rx queue management counters */
  4283. mb();
  4284. mvpp2_rxq_status_update(port, rxq->id, 1, 1);
  4285. /* give packet to stack - skip on first n bytes */
  4286. data = (u8 *)dma_addr + 2 + 32;
  4287. if (rx_bytes <= 0)
  4288. return 0;
  4289. /*
  4290. * No cache invalidation needed here, since the rx_buffer's are
  4291. * located in a uncached memory region
  4292. */
  4293. *packetp = data;
  4294. return rx_bytes;
  4295. }
  4296. /* Drain Txq */
  4297. static void mvpp2_txq_drain(struct mvpp2_port *port, struct mvpp2_tx_queue *txq,
  4298. int enable)
  4299. {
  4300. u32 val;
  4301. mvpp2_write(port->priv, MVPP2_TXQ_NUM_REG, txq->id);
  4302. val = mvpp2_read(port->priv, MVPP2_TXQ_PREF_BUF_REG);
  4303. if (enable)
  4304. val |= MVPP2_TXQ_DRAIN_EN_MASK;
  4305. else
  4306. val &= ~MVPP2_TXQ_DRAIN_EN_MASK;
  4307. mvpp2_write(port->priv, MVPP2_TXQ_PREF_BUF_REG, val);
  4308. }
  4309. static int mvpp2_send(struct udevice *dev, void *packet, int length)
  4310. {
  4311. struct mvpp2_port *port = dev_get_priv(dev);
  4312. struct mvpp2_tx_queue *txq, *aggr_txq;
  4313. struct mvpp2_tx_desc *tx_desc;
  4314. int tx_done;
  4315. int timeout;
  4316. txq = port->txqs[0];
  4317. aggr_txq = &port->priv->aggr_txqs[smp_processor_id()];
  4318. /* Get a descriptor for the first part of the packet */
  4319. tx_desc = mvpp2_txq_next_desc_get(aggr_txq);
  4320. mvpp2_txdesc_txq_set(port, tx_desc, txq->id);
  4321. mvpp2_txdesc_size_set(port, tx_desc, length);
  4322. mvpp2_txdesc_offset_set(port, tx_desc,
  4323. (dma_addr_t)packet & MVPP2_TX_DESC_ALIGN);
  4324. mvpp2_txdesc_dma_addr_set(port, tx_desc,
  4325. (dma_addr_t)packet & ~MVPP2_TX_DESC_ALIGN);
  4326. /* First and Last descriptor */
  4327. mvpp2_txdesc_cmd_set(port, tx_desc,
  4328. MVPP2_TXD_L4_CSUM_NOT | MVPP2_TXD_IP_CSUM_DISABLE
  4329. | MVPP2_TXD_F_DESC | MVPP2_TXD_L_DESC);
  4330. /* Flush tx data */
  4331. flush_dcache_range((unsigned long)packet,
  4332. (unsigned long)packet + ALIGN(length, PKTALIGN));
  4333. /* Enable transmit */
  4334. mb();
  4335. mvpp2_aggr_txq_pend_desc_add(port, 1);
  4336. mvpp2_write(port->priv, MVPP2_TXQ_NUM_REG, txq->id);
  4337. timeout = 0;
  4338. do {
  4339. if (timeout++ > 10000) {
  4340. printf("timeout: packet not sent from aggregated to phys TXQ\n");
  4341. return 0;
  4342. }
  4343. tx_done = mvpp2_txq_pend_desc_num_get(port, txq);
  4344. } while (tx_done);
  4345. /* Enable TXQ drain */
  4346. mvpp2_txq_drain(port, txq, 1);
  4347. timeout = 0;
  4348. do {
  4349. if (timeout++ > 10000) {
  4350. printf("timeout: packet not sent\n");
  4351. return 0;
  4352. }
  4353. tx_done = mvpp2_txq_sent_desc_proc(port, txq);
  4354. } while (!tx_done);
  4355. /* Disable TXQ drain */
  4356. mvpp2_txq_drain(port, txq, 0);
  4357. return 0;
  4358. }
  4359. static int mvpp2_start(struct udevice *dev)
  4360. {
  4361. struct eth_pdata *pdata = dev_get_platdata(dev);
  4362. struct mvpp2_port *port = dev_get_priv(dev);
  4363. /* Load current MAC address */
  4364. memcpy(port->dev_addr, pdata->enetaddr, ETH_ALEN);
  4365. /* Reconfigure parser accept the original MAC address */
  4366. mvpp2_prs_update_mac_da(port, port->dev_addr);
  4367. mvpp2_port_power_up(port);
  4368. mvpp2_open(dev, port);
  4369. return 0;
  4370. }
  4371. static void mvpp2_stop(struct udevice *dev)
  4372. {
  4373. struct mvpp2_port *port = dev_get_priv(dev);
  4374. mvpp2_stop_dev(port);
  4375. mvpp2_cleanup_rxqs(port);
  4376. mvpp2_cleanup_txqs(port);
  4377. }
  4378. static int mvpp22_smi_phy_addr_cfg(struct mvpp2_port *port)
  4379. {
  4380. writel(port->phyaddr, port->priv->iface_base +
  4381. MVPP22_SMI_PHY_ADDR_REG(port->gop_id));
  4382. return 0;
  4383. }
  4384. static int mvpp2_base_probe(struct udevice *dev)
  4385. {
  4386. struct mvpp2 *priv = dev_get_priv(dev);
  4387. struct mii_dev *bus;
  4388. void *bd_space;
  4389. u32 size = 0;
  4390. int i;
  4391. /* Save hw-version */
  4392. priv->hw_version = dev_get_driver_data(dev);
  4393. /*
  4394. * U-Boot special buffer handling:
  4395. *
  4396. * Allocate buffer area for descs and rx_buffers. This is only
  4397. * done once for all interfaces. As only one interface can
  4398. * be active. Make this area DMA-safe by disabling the D-cache
  4399. */
  4400. /* Align buffer area for descs and rx_buffers to 1MiB */
  4401. bd_space = memalign(1 << MMU_SECTION_SHIFT, BD_SPACE);
  4402. mmu_set_region_dcache_behaviour((unsigned long)bd_space,
  4403. BD_SPACE, DCACHE_OFF);
  4404. buffer_loc.aggr_tx_descs = (struct mvpp2_tx_desc *)bd_space;
  4405. size += MVPP2_AGGR_TXQ_SIZE * MVPP2_DESC_ALIGNED_SIZE;
  4406. buffer_loc.tx_descs =
  4407. (struct mvpp2_tx_desc *)((unsigned long)bd_space + size);
  4408. size += MVPP2_MAX_TXD * MVPP2_DESC_ALIGNED_SIZE;
  4409. buffer_loc.rx_descs =
  4410. (struct mvpp2_rx_desc *)((unsigned long)bd_space + size);
  4411. size += MVPP2_MAX_RXD * MVPP2_DESC_ALIGNED_SIZE;
  4412. for (i = 0; i < MVPP2_BM_POOLS_NUM; i++) {
  4413. buffer_loc.bm_pool[i] =
  4414. (unsigned long *)((unsigned long)bd_space + size);
  4415. if (priv->hw_version == MVPP21)
  4416. size += MVPP2_BM_POOL_SIZE_MAX * 2 * sizeof(u32);
  4417. else
  4418. size += MVPP2_BM_POOL_SIZE_MAX * 2 * sizeof(u64);
  4419. }
  4420. for (i = 0; i < MVPP2_BM_LONG_BUF_NUM; i++) {
  4421. buffer_loc.rx_buffer[i] =
  4422. (unsigned long *)((unsigned long)bd_space + size);
  4423. size += RX_BUFFER_SIZE;
  4424. }
  4425. /* Clear the complete area so that all descriptors are cleared */
  4426. memset(bd_space, 0, size);
  4427. /* Save base addresses for later use */
  4428. priv->base = (void *)dev_get_addr_index(dev, 0);
  4429. if (IS_ERR(priv->base))
  4430. return PTR_ERR(priv->base);
  4431. if (priv->hw_version == MVPP21) {
  4432. priv->lms_base = (void *)dev_get_addr_index(dev, 1);
  4433. if (IS_ERR(priv->lms_base))
  4434. return PTR_ERR(priv->lms_base);
  4435. priv->mdio_base = priv->lms_base + MVPP21_SMI;
  4436. } else {
  4437. priv->iface_base = (void *)dev_get_addr_index(dev, 1);
  4438. if (IS_ERR(priv->iface_base))
  4439. return PTR_ERR(priv->iface_base);
  4440. priv->mdio_base = priv->iface_base + MVPP22_SMI;
  4441. /* Store common base addresses for all ports */
  4442. priv->mpcs_base = priv->iface_base + MVPP22_MPCS;
  4443. priv->xpcs_base = priv->iface_base + MVPP22_XPCS;
  4444. priv->rfu1_base = priv->iface_base + MVPP22_RFU1;
  4445. }
  4446. if (priv->hw_version == MVPP21)
  4447. priv->max_port_rxqs = 8;
  4448. else
  4449. priv->max_port_rxqs = 32;
  4450. /* Finally create and register the MDIO bus driver */
  4451. bus = mdio_alloc();
  4452. if (!bus) {
  4453. printf("Failed to allocate MDIO bus\n");
  4454. return -ENOMEM;
  4455. }
  4456. bus->read = mpp2_mdio_read;
  4457. bus->write = mpp2_mdio_write;
  4458. snprintf(bus->name, sizeof(bus->name), dev->name);
  4459. bus->priv = (void *)priv;
  4460. priv->bus = bus;
  4461. return mdio_register(bus);
  4462. }
  4463. static int mvpp2_probe(struct udevice *dev)
  4464. {
  4465. struct mvpp2_port *port = dev_get_priv(dev);
  4466. struct mvpp2 *priv = dev_get_priv(dev->parent);
  4467. int err;
  4468. /* Only call the probe function for the parent once */
  4469. if (!priv->probe_done) {
  4470. err = mvpp2_base_probe(dev->parent);
  4471. priv->probe_done = 1;
  4472. }
  4473. port->priv = dev_get_priv(dev->parent);
  4474. err = phy_info_parse(dev, port);
  4475. if (err)
  4476. return err;
  4477. /*
  4478. * We need the port specific io base addresses at this stage, since
  4479. * gop_port_init() accesses these registers
  4480. */
  4481. if (priv->hw_version == MVPP21) {
  4482. int priv_common_regs_num = 2;
  4483. port->base = (void __iomem *)dev_get_addr_index(
  4484. dev->parent, priv_common_regs_num + port->id);
  4485. if (IS_ERR(port->base))
  4486. return PTR_ERR(port->base);
  4487. } else {
  4488. port->gop_id = fdtdec_get_int(gd->fdt_blob, dev_of_offset(dev),
  4489. "gop-port-id", -1);
  4490. if (port->id == -1) {
  4491. dev_err(&pdev->dev, "missing gop-port-id value\n");
  4492. return -EINVAL;
  4493. }
  4494. port->base = priv->iface_base + MVPP22_PORT_BASE +
  4495. port->gop_id * MVPP22_PORT_OFFSET;
  4496. /* Set phy address of the port */
  4497. mvpp22_smi_phy_addr_cfg(port);
  4498. /* GoP Init */
  4499. gop_port_init(port);
  4500. }
  4501. /* Initialize network controller */
  4502. err = mvpp2_init(dev, priv);
  4503. if (err < 0) {
  4504. dev_err(&pdev->dev, "failed to initialize controller\n");
  4505. return err;
  4506. }
  4507. err = mvpp2_port_probe(dev, port, dev_of_offset(dev), priv);
  4508. if (err)
  4509. return err;
  4510. if (priv->hw_version == MVPP22) {
  4511. priv->netc_config |= mvpp2_netc_cfg_create(port->gop_id,
  4512. port->phy_interface);
  4513. /* Netcomplex configurations for all ports */
  4514. gop_netc_init(priv, MV_NETC_FIRST_PHASE);
  4515. gop_netc_init(priv, MV_NETC_SECOND_PHASE);
  4516. }
  4517. return 0;
  4518. }
  4519. static const struct eth_ops mvpp2_ops = {
  4520. .start = mvpp2_start,
  4521. .send = mvpp2_send,
  4522. .recv = mvpp2_recv,
  4523. .stop = mvpp2_stop,
  4524. };
  4525. static struct driver mvpp2_driver = {
  4526. .name = "mvpp2",
  4527. .id = UCLASS_ETH,
  4528. .probe = mvpp2_probe,
  4529. .ops = &mvpp2_ops,
  4530. .priv_auto_alloc_size = sizeof(struct mvpp2_port),
  4531. .platdata_auto_alloc_size = sizeof(struct eth_pdata),
  4532. };
  4533. /*
  4534. * Use a MISC device to bind the n instances (child nodes) of the
  4535. * network base controller in UCLASS_ETH.
  4536. */
  4537. static int mvpp2_base_bind(struct udevice *parent)
  4538. {
  4539. const void *blob = gd->fdt_blob;
  4540. int node = dev_of_offset(parent);
  4541. struct uclass_driver *drv;
  4542. struct udevice *dev;
  4543. struct eth_pdata *plat;
  4544. char *name;
  4545. int subnode;
  4546. u32 id;
  4547. int base_id_add;
  4548. /* Lookup eth driver */
  4549. drv = lists_uclass_lookup(UCLASS_ETH);
  4550. if (!drv) {
  4551. puts("Cannot find eth driver\n");
  4552. return -ENOENT;
  4553. }
  4554. base_id_add = base_id;
  4555. fdt_for_each_subnode(subnode, blob, node) {
  4556. /* Increment base_id for all subnodes, also the disabled ones */
  4557. base_id++;
  4558. /* Skip disabled ports */
  4559. if (!fdtdec_get_is_enabled(blob, subnode))
  4560. continue;
  4561. plat = calloc(1, sizeof(*plat));
  4562. if (!plat)
  4563. return -ENOMEM;
  4564. id = fdtdec_get_int(blob, subnode, "port-id", -1);
  4565. id += base_id_add;
  4566. name = calloc(1, 16);
  4567. sprintf(name, "mvpp2-%d", id);
  4568. /* Create child device UCLASS_ETH and bind it */
  4569. device_bind(parent, &mvpp2_driver, name, plat, subnode, &dev);
  4570. dev_set_of_offset(dev, subnode);
  4571. }
  4572. return 0;
  4573. }
  4574. static const struct udevice_id mvpp2_ids[] = {
  4575. {
  4576. .compatible = "marvell,armada-375-pp2",
  4577. .data = MVPP21,
  4578. },
  4579. {
  4580. .compatible = "marvell,armada-7k-pp22",
  4581. .data = MVPP22,
  4582. },
  4583. { }
  4584. };
  4585. U_BOOT_DRIVER(mvpp2_base) = {
  4586. .name = "mvpp2_base",
  4587. .id = UCLASS_MISC,
  4588. .of_match = mvpp2_ids,
  4589. .bind = mvpp2_base_bind,
  4590. .priv_auto_alloc_size = sizeof(struct mvpp2),
  4591. };