mvpp2.c 153 KB

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