mvpp2.c 155 KB

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