mvpp2.c 149 KB

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