mvpp2.c 155 KB

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