mvpp2.c 155 KB

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