mvpp2.c 153 KB

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