b53_common.c 74 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021
  1. /*
  2. * B53 switch driver main logic
  3. *
  4. * Copyright (C) 2011-2013 Jonas Gorski <jogo@openwrt.org>
  5. * Copyright (C) 2016 Florian Fainelli <f.fainelli@gmail.com>
  6. *
  7. * Permission to use, copy, modify, and/or distribute this software for any
  8. * purpose with or without fee is hereby granted, provided that the above
  9. * copyright notice and this permission notice appear in all copies.
  10. *
  11. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  12. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  13. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  14. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  15. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  16. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  17. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  18. */
  19. #include <linux/delay.h>
  20. #include <linux/export.h>
  21. #include <linux/gpio.h>
  22. #include <linux/kernel.h>
  23. #include <linux/module.h>
  24. #include <linux/platform_data/b53.h>
  25. #include <linux/phy.h>
  26. #include <linux/phylink.h>
  27. #include <linux/etherdevice.h>
  28. #include <linux/if_bridge.h>
  29. #include <linux/if_vlan.h>
  30. #include <net/dsa.h>
  31. #include "b53_regs.h"
  32. #include "b53_priv.h"
  33. struct b53_mib_desc {
  34. u8 size;
  35. u8 offset;
  36. const char *name;
  37. };
  38. /* BCM5365 MIB counters */
  39. static const struct b53_mib_desc b53_mibs_65[] = {
  40. { 8, 0x00, "TxOctets" },
  41. { 4, 0x08, "TxDropPkts" },
  42. { 4, 0x10, "TxBroadcastPkts" },
  43. { 4, 0x14, "TxMulticastPkts" },
  44. { 4, 0x18, "TxUnicastPkts" },
  45. { 4, 0x1c, "TxCollisions" },
  46. { 4, 0x20, "TxSingleCollision" },
  47. { 4, 0x24, "TxMultipleCollision" },
  48. { 4, 0x28, "TxDeferredTransmit" },
  49. { 4, 0x2c, "TxLateCollision" },
  50. { 4, 0x30, "TxExcessiveCollision" },
  51. { 4, 0x38, "TxPausePkts" },
  52. { 8, 0x44, "RxOctets" },
  53. { 4, 0x4c, "RxUndersizePkts" },
  54. { 4, 0x50, "RxPausePkts" },
  55. { 4, 0x54, "Pkts64Octets" },
  56. { 4, 0x58, "Pkts65to127Octets" },
  57. { 4, 0x5c, "Pkts128to255Octets" },
  58. { 4, 0x60, "Pkts256to511Octets" },
  59. { 4, 0x64, "Pkts512to1023Octets" },
  60. { 4, 0x68, "Pkts1024to1522Octets" },
  61. { 4, 0x6c, "RxOversizePkts" },
  62. { 4, 0x70, "RxJabbers" },
  63. { 4, 0x74, "RxAlignmentErrors" },
  64. { 4, 0x78, "RxFCSErrors" },
  65. { 8, 0x7c, "RxGoodOctets" },
  66. { 4, 0x84, "RxDropPkts" },
  67. { 4, 0x88, "RxUnicastPkts" },
  68. { 4, 0x8c, "RxMulticastPkts" },
  69. { 4, 0x90, "RxBroadcastPkts" },
  70. { 4, 0x94, "RxSAChanges" },
  71. { 4, 0x98, "RxFragments" },
  72. };
  73. #define B53_MIBS_65_SIZE ARRAY_SIZE(b53_mibs_65)
  74. /* BCM63xx MIB counters */
  75. static const struct b53_mib_desc b53_mibs_63xx[] = {
  76. { 8, 0x00, "TxOctets" },
  77. { 4, 0x08, "TxDropPkts" },
  78. { 4, 0x0c, "TxQoSPkts" },
  79. { 4, 0x10, "TxBroadcastPkts" },
  80. { 4, 0x14, "TxMulticastPkts" },
  81. { 4, 0x18, "TxUnicastPkts" },
  82. { 4, 0x1c, "TxCollisions" },
  83. { 4, 0x20, "TxSingleCollision" },
  84. { 4, 0x24, "TxMultipleCollision" },
  85. { 4, 0x28, "TxDeferredTransmit" },
  86. { 4, 0x2c, "TxLateCollision" },
  87. { 4, 0x30, "TxExcessiveCollision" },
  88. { 4, 0x38, "TxPausePkts" },
  89. { 8, 0x3c, "TxQoSOctets" },
  90. { 8, 0x44, "RxOctets" },
  91. { 4, 0x4c, "RxUndersizePkts" },
  92. { 4, 0x50, "RxPausePkts" },
  93. { 4, 0x54, "Pkts64Octets" },
  94. { 4, 0x58, "Pkts65to127Octets" },
  95. { 4, 0x5c, "Pkts128to255Octets" },
  96. { 4, 0x60, "Pkts256to511Octets" },
  97. { 4, 0x64, "Pkts512to1023Octets" },
  98. { 4, 0x68, "Pkts1024to1522Octets" },
  99. { 4, 0x6c, "RxOversizePkts" },
  100. { 4, 0x70, "RxJabbers" },
  101. { 4, 0x74, "RxAlignmentErrors" },
  102. { 4, 0x78, "RxFCSErrors" },
  103. { 8, 0x7c, "RxGoodOctets" },
  104. { 4, 0x84, "RxDropPkts" },
  105. { 4, 0x88, "RxUnicastPkts" },
  106. { 4, 0x8c, "RxMulticastPkts" },
  107. { 4, 0x90, "RxBroadcastPkts" },
  108. { 4, 0x94, "RxSAChanges" },
  109. { 4, 0x98, "RxFragments" },
  110. { 4, 0xa0, "RxSymbolErrors" },
  111. { 4, 0xa4, "RxQoSPkts" },
  112. { 8, 0xa8, "RxQoSOctets" },
  113. { 4, 0xb0, "Pkts1523to2047Octets" },
  114. { 4, 0xb4, "Pkts2048to4095Octets" },
  115. { 4, 0xb8, "Pkts4096to8191Octets" },
  116. { 4, 0xbc, "Pkts8192to9728Octets" },
  117. { 4, 0xc0, "RxDiscarded" },
  118. };
  119. #define B53_MIBS_63XX_SIZE ARRAY_SIZE(b53_mibs_63xx)
  120. /* MIB counters */
  121. static const struct b53_mib_desc b53_mibs[] = {
  122. { 8, 0x00, "TxOctets" },
  123. { 4, 0x08, "TxDropPkts" },
  124. { 4, 0x10, "TxBroadcastPkts" },
  125. { 4, 0x14, "TxMulticastPkts" },
  126. { 4, 0x18, "TxUnicastPkts" },
  127. { 4, 0x1c, "TxCollisions" },
  128. { 4, 0x20, "TxSingleCollision" },
  129. { 4, 0x24, "TxMultipleCollision" },
  130. { 4, 0x28, "TxDeferredTransmit" },
  131. { 4, 0x2c, "TxLateCollision" },
  132. { 4, 0x30, "TxExcessiveCollision" },
  133. { 4, 0x38, "TxPausePkts" },
  134. { 8, 0x50, "RxOctets" },
  135. { 4, 0x58, "RxUndersizePkts" },
  136. { 4, 0x5c, "RxPausePkts" },
  137. { 4, 0x60, "Pkts64Octets" },
  138. { 4, 0x64, "Pkts65to127Octets" },
  139. { 4, 0x68, "Pkts128to255Octets" },
  140. { 4, 0x6c, "Pkts256to511Octets" },
  141. { 4, 0x70, "Pkts512to1023Octets" },
  142. { 4, 0x74, "Pkts1024to1522Octets" },
  143. { 4, 0x78, "RxOversizePkts" },
  144. { 4, 0x7c, "RxJabbers" },
  145. { 4, 0x80, "RxAlignmentErrors" },
  146. { 4, 0x84, "RxFCSErrors" },
  147. { 8, 0x88, "RxGoodOctets" },
  148. { 4, 0x90, "RxDropPkts" },
  149. { 4, 0x94, "RxUnicastPkts" },
  150. { 4, 0x98, "RxMulticastPkts" },
  151. { 4, 0x9c, "RxBroadcastPkts" },
  152. { 4, 0xa0, "RxSAChanges" },
  153. { 4, 0xa4, "RxFragments" },
  154. { 4, 0xa8, "RxJumboPkts" },
  155. { 4, 0xac, "RxSymbolErrors" },
  156. { 4, 0xc0, "RxDiscarded" },
  157. };
  158. #define B53_MIBS_SIZE ARRAY_SIZE(b53_mibs)
  159. static const struct b53_mib_desc b53_mibs_58xx[] = {
  160. { 8, 0x00, "TxOctets" },
  161. { 4, 0x08, "TxDropPkts" },
  162. { 4, 0x0c, "TxQPKTQ0" },
  163. { 4, 0x10, "TxBroadcastPkts" },
  164. { 4, 0x14, "TxMulticastPkts" },
  165. { 4, 0x18, "TxUnicastPKts" },
  166. { 4, 0x1c, "TxCollisions" },
  167. { 4, 0x20, "TxSingleCollision" },
  168. { 4, 0x24, "TxMultipleCollision" },
  169. { 4, 0x28, "TxDeferredCollision" },
  170. { 4, 0x2c, "TxLateCollision" },
  171. { 4, 0x30, "TxExcessiveCollision" },
  172. { 4, 0x34, "TxFrameInDisc" },
  173. { 4, 0x38, "TxPausePkts" },
  174. { 4, 0x3c, "TxQPKTQ1" },
  175. { 4, 0x40, "TxQPKTQ2" },
  176. { 4, 0x44, "TxQPKTQ3" },
  177. { 4, 0x48, "TxQPKTQ4" },
  178. { 4, 0x4c, "TxQPKTQ5" },
  179. { 8, 0x50, "RxOctets" },
  180. { 4, 0x58, "RxUndersizePkts" },
  181. { 4, 0x5c, "RxPausePkts" },
  182. { 4, 0x60, "RxPkts64Octets" },
  183. { 4, 0x64, "RxPkts65to127Octets" },
  184. { 4, 0x68, "RxPkts128to255Octets" },
  185. { 4, 0x6c, "RxPkts256to511Octets" },
  186. { 4, 0x70, "RxPkts512to1023Octets" },
  187. { 4, 0x74, "RxPkts1024toMaxPktsOctets" },
  188. { 4, 0x78, "RxOversizePkts" },
  189. { 4, 0x7c, "RxJabbers" },
  190. { 4, 0x80, "RxAlignmentErrors" },
  191. { 4, 0x84, "RxFCSErrors" },
  192. { 8, 0x88, "RxGoodOctets" },
  193. { 4, 0x90, "RxDropPkts" },
  194. { 4, 0x94, "RxUnicastPkts" },
  195. { 4, 0x98, "RxMulticastPkts" },
  196. { 4, 0x9c, "RxBroadcastPkts" },
  197. { 4, 0xa0, "RxSAChanges" },
  198. { 4, 0xa4, "RxFragments" },
  199. { 4, 0xa8, "RxJumboPkt" },
  200. { 4, 0xac, "RxSymblErr" },
  201. { 4, 0xb0, "InRangeErrCount" },
  202. { 4, 0xb4, "OutRangeErrCount" },
  203. { 4, 0xb8, "EEELpiEvent" },
  204. { 4, 0xbc, "EEELpiDuration" },
  205. { 4, 0xc0, "RxDiscard" },
  206. { 4, 0xc8, "TxQPKTQ6" },
  207. { 4, 0xcc, "TxQPKTQ7" },
  208. { 4, 0xd0, "TxPkts64Octets" },
  209. { 4, 0xd4, "TxPkts65to127Octets" },
  210. { 4, 0xd8, "TxPkts128to255Octets" },
  211. { 4, 0xdc, "TxPkts256to511Ocets" },
  212. { 4, 0xe0, "TxPkts512to1023Ocets" },
  213. { 4, 0xe4, "TxPkts1024toMaxPktOcets" },
  214. };
  215. #define B53_MIBS_58XX_SIZE ARRAY_SIZE(b53_mibs_58xx)
  216. #define B53_MAX_MTU_25 (1536 - ETH_HLEN - VLAN_HLEN - ETH_FCS_LEN)
  217. #define B53_MAX_MTU (9720 - ETH_HLEN - VLAN_HLEN - ETH_FCS_LEN)
  218. static int b53_do_vlan_op(struct b53_device *dev, u8 op)
  219. {
  220. unsigned int i;
  221. b53_write8(dev, B53_ARLIO_PAGE, dev->vta_regs[0], VTA_START_CMD | op);
  222. for (i = 0; i < 10; i++) {
  223. u8 vta;
  224. b53_read8(dev, B53_ARLIO_PAGE, dev->vta_regs[0], &vta);
  225. if (!(vta & VTA_START_CMD))
  226. return 0;
  227. usleep_range(100, 200);
  228. }
  229. return -EIO;
  230. }
  231. static void b53_set_vlan_entry(struct b53_device *dev, u16 vid,
  232. struct b53_vlan *vlan)
  233. {
  234. if (is5325(dev)) {
  235. u32 entry = 0;
  236. if (vlan->members) {
  237. entry = ((vlan->untag & VA_UNTAG_MASK_25) <<
  238. VA_UNTAG_S_25) | vlan->members;
  239. if (dev->core_rev >= 3)
  240. entry |= VA_VALID_25_R4 | vid << VA_VID_HIGH_S;
  241. else
  242. entry |= VA_VALID_25;
  243. }
  244. b53_write32(dev, B53_VLAN_PAGE, B53_VLAN_WRITE_25, entry);
  245. b53_write16(dev, B53_VLAN_PAGE, B53_VLAN_TABLE_ACCESS_25, vid |
  246. VTA_RW_STATE_WR | VTA_RW_OP_EN);
  247. } else if (is5365(dev)) {
  248. u16 entry = 0;
  249. if (vlan->members)
  250. entry = ((vlan->untag & VA_UNTAG_MASK_65) <<
  251. VA_UNTAG_S_65) | vlan->members | VA_VALID_65;
  252. b53_write16(dev, B53_VLAN_PAGE, B53_VLAN_WRITE_65, entry);
  253. b53_write16(dev, B53_VLAN_PAGE, B53_VLAN_TABLE_ACCESS_65, vid |
  254. VTA_RW_STATE_WR | VTA_RW_OP_EN);
  255. } else {
  256. b53_write16(dev, B53_ARLIO_PAGE, dev->vta_regs[1], vid);
  257. b53_write32(dev, B53_ARLIO_PAGE, dev->vta_regs[2],
  258. (vlan->untag << VTE_UNTAG_S) | vlan->members);
  259. b53_do_vlan_op(dev, VTA_CMD_WRITE);
  260. }
  261. dev_dbg(dev->ds->dev, "VID: %d, members: 0x%04x, untag: 0x%04x\n",
  262. vid, vlan->members, vlan->untag);
  263. }
  264. static void b53_get_vlan_entry(struct b53_device *dev, u16 vid,
  265. struct b53_vlan *vlan)
  266. {
  267. if (is5325(dev)) {
  268. u32 entry = 0;
  269. b53_write16(dev, B53_VLAN_PAGE, B53_VLAN_TABLE_ACCESS_25, vid |
  270. VTA_RW_STATE_RD | VTA_RW_OP_EN);
  271. b53_read32(dev, B53_VLAN_PAGE, B53_VLAN_WRITE_25, &entry);
  272. if (dev->core_rev >= 3)
  273. vlan->valid = !!(entry & VA_VALID_25_R4);
  274. else
  275. vlan->valid = !!(entry & VA_VALID_25);
  276. vlan->members = entry & VA_MEMBER_MASK;
  277. vlan->untag = (entry >> VA_UNTAG_S_25) & VA_UNTAG_MASK_25;
  278. } else if (is5365(dev)) {
  279. u16 entry = 0;
  280. b53_write16(dev, B53_VLAN_PAGE, B53_VLAN_TABLE_ACCESS_65, vid |
  281. VTA_RW_STATE_WR | VTA_RW_OP_EN);
  282. b53_read16(dev, B53_VLAN_PAGE, B53_VLAN_WRITE_65, &entry);
  283. vlan->valid = !!(entry & VA_VALID_65);
  284. vlan->members = entry & VA_MEMBER_MASK;
  285. vlan->untag = (entry >> VA_UNTAG_S_65) & VA_UNTAG_MASK_65;
  286. } else {
  287. u32 entry = 0;
  288. b53_write16(dev, B53_ARLIO_PAGE, dev->vta_regs[1], vid);
  289. b53_do_vlan_op(dev, VTA_CMD_READ);
  290. b53_read32(dev, B53_ARLIO_PAGE, dev->vta_regs[2], &entry);
  291. vlan->members = entry & VTE_MEMBERS;
  292. vlan->untag = (entry >> VTE_UNTAG_S) & VTE_MEMBERS;
  293. vlan->valid = true;
  294. }
  295. }
  296. static void b53_set_eap_mode(struct b53_device *dev, int port, int mode)
  297. {
  298. u64 eap_conf;
  299. if (is5325(dev) || is5365(dev) || dev->chip_id == BCM5389_DEVICE_ID)
  300. return;
  301. b53_read64(dev, B53_EAP_PAGE, B53_PORT_EAP_CONF(port), &eap_conf);
  302. if (is63xx(dev)) {
  303. eap_conf &= ~EAP_MODE_MASK_63XX;
  304. eap_conf |= (u64)mode << EAP_MODE_SHIFT_63XX;
  305. } else {
  306. eap_conf &= ~EAP_MODE_MASK;
  307. eap_conf |= (u64)mode << EAP_MODE_SHIFT;
  308. }
  309. b53_write64(dev, B53_EAP_PAGE, B53_PORT_EAP_CONF(port), eap_conf);
  310. }
  311. static void b53_set_forwarding(struct b53_device *dev, int enable)
  312. {
  313. u8 mgmt;
  314. b53_read8(dev, B53_CTRL_PAGE, B53_SWITCH_MODE, &mgmt);
  315. if (enable)
  316. mgmt |= SM_SW_FWD_EN;
  317. else
  318. mgmt &= ~SM_SW_FWD_EN;
  319. b53_write8(dev, B53_CTRL_PAGE, B53_SWITCH_MODE, mgmt);
  320. if (!is5325(dev)) {
  321. /* Include IMP port in dumb forwarding mode */
  322. b53_read8(dev, B53_CTRL_PAGE, B53_SWITCH_CTRL, &mgmt);
  323. mgmt |= B53_MII_DUMB_FWDG_EN;
  324. b53_write8(dev, B53_CTRL_PAGE, B53_SWITCH_CTRL, mgmt);
  325. /* Look at B53_UC_FWD_EN and B53_MC_FWD_EN to decide whether
  326. * frames should be flooded or not.
  327. */
  328. b53_read8(dev, B53_CTRL_PAGE, B53_IP_MULTICAST_CTRL, &mgmt);
  329. mgmt |= B53_UC_FWD_EN | B53_MC_FWD_EN | B53_IPMC_FWD_EN;
  330. b53_write8(dev, B53_CTRL_PAGE, B53_IP_MULTICAST_CTRL, mgmt);
  331. } else {
  332. b53_read8(dev, B53_CTRL_PAGE, B53_IP_MULTICAST_CTRL, &mgmt);
  333. mgmt |= B53_IP_MCAST_25;
  334. b53_write8(dev, B53_CTRL_PAGE, B53_IP_MULTICAST_CTRL, mgmt);
  335. }
  336. }
  337. static void b53_enable_vlan(struct b53_device *dev, int port, bool enable,
  338. bool enable_filtering)
  339. {
  340. u8 mgmt, vc0, vc1, vc4 = 0, vc5;
  341. b53_read8(dev, B53_CTRL_PAGE, B53_SWITCH_MODE, &mgmt);
  342. b53_read8(dev, B53_VLAN_PAGE, B53_VLAN_CTRL0, &vc0);
  343. b53_read8(dev, B53_VLAN_PAGE, B53_VLAN_CTRL1, &vc1);
  344. if (is5325(dev) || is5365(dev)) {
  345. b53_read8(dev, B53_VLAN_PAGE, B53_VLAN_CTRL4_25, &vc4);
  346. b53_read8(dev, B53_VLAN_PAGE, B53_VLAN_CTRL5_25, &vc5);
  347. } else if (is63xx(dev)) {
  348. b53_read8(dev, B53_VLAN_PAGE, B53_VLAN_CTRL4_63XX, &vc4);
  349. b53_read8(dev, B53_VLAN_PAGE, B53_VLAN_CTRL5_63XX, &vc5);
  350. } else {
  351. b53_read8(dev, B53_VLAN_PAGE, B53_VLAN_CTRL4, &vc4);
  352. b53_read8(dev, B53_VLAN_PAGE, B53_VLAN_CTRL5, &vc5);
  353. }
  354. vc1 &= ~VC1_RX_MCST_FWD_EN;
  355. if (enable) {
  356. vc0 |= VC0_VLAN_EN | VC0_VID_CHK_EN | VC0_VID_HASH_VID;
  357. vc1 |= VC1_RX_MCST_UNTAG_EN;
  358. vc4 &= ~VC4_ING_VID_CHECK_MASK;
  359. if (enable_filtering) {
  360. vc4 |= VC4_ING_VID_VIO_DROP << VC4_ING_VID_CHECK_S;
  361. vc5 |= VC5_DROP_VTABLE_MISS;
  362. } else {
  363. vc4 |= VC4_NO_ING_VID_CHK << VC4_ING_VID_CHECK_S;
  364. vc5 &= ~VC5_DROP_VTABLE_MISS;
  365. }
  366. if (is5325(dev))
  367. vc0 &= ~VC0_RESERVED_1;
  368. if (is5325(dev) || is5365(dev))
  369. vc1 |= VC1_RX_MCST_TAG_EN;
  370. } else {
  371. vc0 &= ~(VC0_VLAN_EN | VC0_VID_CHK_EN | VC0_VID_HASH_VID);
  372. vc1 &= ~VC1_RX_MCST_UNTAG_EN;
  373. vc4 &= ~VC4_ING_VID_CHECK_MASK;
  374. vc5 &= ~VC5_DROP_VTABLE_MISS;
  375. if (is5325(dev) || is5365(dev))
  376. vc4 |= VC4_ING_VID_VIO_FWD << VC4_ING_VID_CHECK_S;
  377. else
  378. vc4 |= VC4_ING_VID_VIO_TO_IMP << VC4_ING_VID_CHECK_S;
  379. if (is5325(dev) || is5365(dev))
  380. vc1 &= ~VC1_RX_MCST_TAG_EN;
  381. }
  382. if (!is5325(dev) && !is5365(dev))
  383. vc5 &= ~VC5_VID_FFF_EN;
  384. b53_write8(dev, B53_VLAN_PAGE, B53_VLAN_CTRL0, vc0);
  385. b53_write8(dev, B53_VLAN_PAGE, B53_VLAN_CTRL1, vc1);
  386. if (is5325(dev) || is5365(dev)) {
  387. /* enable the high 8 bit vid check on 5325 */
  388. if (is5325(dev) && enable)
  389. b53_write8(dev, B53_VLAN_PAGE, B53_VLAN_CTRL3,
  390. VC3_HIGH_8BIT_EN);
  391. else
  392. b53_write8(dev, B53_VLAN_PAGE, B53_VLAN_CTRL3, 0);
  393. b53_write8(dev, B53_VLAN_PAGE, B53_VLAN_CTRL4_25, vc4);
  394. b53_write8(dev, B53_VLAN_PAGE, B53_VLAN_CTRL5_25, vc5);
  395. } else if (is63xx(dev)) {
  396. b53_write16(dev, B53_VLAN_PAGE, B53_VLAN_CTRL3_63XX, 0);
  397. b53_write8(dev, B53_VLAN_PAGE, B53_VLAN_CTRL4_63XX, vc4);
  398. b53_write8(dev, B53_VLAN_PAGE, B53_VLAN_CTRL5_63XX, vc5);
  399. } else {
  400. b53_write16(dev, B53_VLAN_PAGE, B53_VLAN_CTRL3, 0);
  401. b53_write8(dev, B53_VLAN_PAGE, B53_VLAN_CTRL4, vc4);
  402. b53_write8(dev, B53_VLAN_PAGE, B53_VLAN_CTRL5, vc5);
  403. }
  404. b53_write8(dev, B53_CTRL_PAGE, B53_SWITCH_MODE, mgmt);
  405. dev->vlan_enabled = enable;
  406. dev_dbg(dev->dev, "Port %d VLAN enabled: %d, filtering: %d\n",
  407. port, enable, enable_filtering);
  408. }
  409. static int b53_set_jumbo(struct b53_device *dev, bool enable, bool allow_10_100)
  410. {
  411. u32 port_mask = 0;
  412. u16 max_size = JMS_MIN_SIZE;
  413. if (is5325(dev) || is5365(dev))
  414. return -EINVAL;
  415. if (enable) {
  416. port_mask = dev->enabled_ports;
  417. max_size = JMS_MAX_SIZE;
  418. if (allow_10_100)
  419. port_mask |= JPM_10_100_JUMBO_EN;
  420. }
  421. b53_write32(dev, B53_JUMBO_PAGE, dev->jumbo_pm_reg, port_mask);
  422. return b53_write16(dev, B53_JUMBO_PAGE, dev->jumbo_size_reg, max_size);
  423. }
  424. static int b53_flush_arl(struct b53_device *dev, u8 mask)
  425. {
  426. unsigned int i;
  427. b53_write8(dev, B53_CTRL_PAGE, B53_FAST_AGE_CTRL,
  428. FAST_AGE_DONE | FAST_AGE_DYNAMIC | mask);
  429. for (i = 0; i < 10; i++) {
  430. u8 fast_age_ctrl;
  431. b53_read8(dev, B53_CTRL_PAGE, B53_FAST_AGE_CTRL,
  432. &fast_age_ctrl);
  433. if (!(fast_age_ctrl & FAST_AGE_DONE))
  434. goto out;
  435. msleep(1);
  436. }
  437. return -ETIMEDOUT;
  438. out:
  439. /* Only age dynamic entries (default behavior) */
  440. b53_write8(dev, B53_CTRL_PAGE, B53_FAST_AGE_CTRL, FAST_AGE_DYNAMIC);
  441. return 0;
  442. }
  443. static int b53_fast_age_port(struct b53_device *dev, int port)
  444. {
  445. b53_write8(dev, B53_CTRL_PAGE, B53_FAST_AGE_PORT_CTRL, port);
  446. return b53_flush_arl(dev, FAST_AGE_PORT);
  447. }
  448. static int b53_fast_age_vlan(struct b53_device *dev, u16 vid)
  449. {
  450. b53_write16(dev, B53_CTRL_PAGE, B53_FAST_AGE_VID_CTRL, vid);
  451. return b53_flush_arl(dev, FAST_AGE_VLAN);
  452. }
  453. void b53_imp_vlan_setup(struct dsa_switch *ds, int cpu_port)
  454. {
  455. struct b53_device *dev = ds->priv;
  456. unsigned int i;
  457. u16 pvlan;
  458. /* BCM5325 CPU port is at 8 */
  459. if ((is5325(dev) || is5365(dev)) && cpu_port == B53_CPU_PORT_25)
  460. cpu_port = B53_CPU_PORT;
  461. /* Enable the IMP port to be in the same VLAN as the other ports
  462. * on a per-port basis such that we only have Port i and IMP in
  463. * the same VLAN.
  464. */
  465. b53_for_each_port(dev, i) {
  466. b53_read16(dev, B53_PVLAN_PAGE, B53_PVLAN_PORT_MASK(i), &pvlan);
  467. pvlan |= BIT(cpu_port);
  468. b53_write16(dev, B53_PVLAN_PAGE, B53_PVLAN_PORT_MASK(i), pvlan);
  469. }
  470. }
  471. EXPORT_SYMBOL(b53_imp_vlan_setup);
  472. static void b53_port_set_ucast_flood(struct b53_device *dev, int port,
  473. bool unicast)
  474. {
  475. u16 uc;
  476. b53_read16(dev, B53_CTRL_PAGE, B53_UC_FLOOD_MASK, &uc);
  477. if (unicast)
  478. uc |= BIT(port);
  479. else
  480. uc &= ~BIT(port);
  481. b53_write16(dev, B53_CTRL_PAGE, B53_UC_FLOOD_MASK, uc);
  482. }
  483. static void b53_port_set_mcast_flood(struct b53_device *dev, int port,
  484. bool multicast)
  485. {
  486. u16 mc;
  487. b53_read16(dev, B53_CTRL_PAGE, B53_MC_FLOOD_MASK, &mc);
  488. if (multicast)
  489. mc |= BIT(port);
  490. else
  491. mc &= ~BIT(port);
  492. b53_write16(dev, B53_CTRL_PAGE, B53_MC_FLOOD_MASK, mc);
  493. b53_read16(dev, B53_CTRL_PAGE, B53_IPMC_FLOOD_MASK, &mc);
  494. if (multicast)
  495. mc |= BIT(port);
  496. else
  497. mc &= ~BIT(port);
  498. b53_write16(dev, B53_CTRL_PAGE, B53_IPMC_FLOOD_MASK, mc);
  499. }
  500. static void b53_port_set_learning(struct b53_device *dev, int port,
  501. bool learning)
  502. {
  503. u16 reg;
  504. if (is5325(dev))
  505. return;
  506. b53_read16(dev, B53_CTRL_PAGE, B53_DIS_LEARNING, &reg);
  507. if (learning)
  508. reg &= ~BIT(port);
  509. else
  510. reg |= BIT(port);
  511. b53_write16(dev, B53_CTRL_PAGE, B53_DIS_LEARNING, reg);
  512. }
  513. static void b53_eee_enable_set(struct dsa_switch *ds, int port, bool enable)
  514. {
  515. struct b53_device *dev = ds->priv;
  516. u16 reg;
  517. b53_read16(dev, B53_EEE_PAGE, B53_EEE_EN_CTRL, &reg);
  518. if (enable)
  519. reg |= BIT(port);
  520. else
  521. reg &= ~BIT(port);
  522. b53_write16(dev, B53_EEE_PAGE, B53_EEE_EN_CTRL, reg);
  523. }
  524. int b53_setup_port(struct dsa_switch *ds, int port)
  525. {
  526. struct b53_device *dev = ds->priv;
  527. b53_port_set_ucast_flood(dev, port, true);
  528. b53_port_set_mcast_flood(dev, port, true);
  529. b53_port_set_learning(dev, port, false);
  530. /* Force all traffic to go to the CPU port to prevent the ASIC from
  531. * trying to forward to bridged ports on matching FDB entries, then
  532. * dropping frames because it isn't allowed to forward there.
  533. */
  534. if (dsa_is_user_port(ds, port))
  535. b53_set_eap_mode(dev, port, EAP_MODE_SIMPLIFIED);
  536. if (is5325(dev) &&
  537. in_range(port, 1, 4)) {
  538. u8 reg;
  539. b53_read8(dev, B53_CTRL_PAGE, B53_PD_MODE_CTRL_25, &reg);
  540. reg &= ~PD_MODE_POWER_DOWN_PORT(0);
  541. if (dsa_is_unused_port(ds, port))
  542. reg |= PD_MODE_POWER_DOWN_PORT(port);
  543. else
  544. reg &= ~PD_MODE_POWER_DOWN_PORT(port);
  545. b53_write8(dev, B53_CTRL_PAGE, B53_PD_MODE_CTRL_25, reg);
  546. }
  547. return 0;
  548. }
  549. EXPORT_SYMBOL(b53_setup_port);
  550. int b53_enable_port(struct dsa_switch *ds, int port, struct phy_device *phy)
  551. {
  552. struct b53_device *dev = ds->priv;
  553. unsigned int cpu_port;
  554. int ret = 0;
  555. u16 pvlan;
  556. if (!dsa_is_user_port(ds, port))
  557. return 0;
  558. cpu_port = dsa_to_port(ds, port)->cpu_dp->index;
  559. if (dev->ops->irq_enable)
  560. ret = dev->ops->irq_enable(dev, port);
  561. if (ret)
  562. return ret;
  563. /* Clear the Rx and Tx disable bits and set to no spanning tree */
  564. b53_write8(dev, B53_CTRL_PAGE, B53_PORT_CTRL(port), 0);
  565. /* Set this port, and only this one to be in the default VLAN,
  566. * if member of a bridge, restore its membership prior to
  567. * bringing down this port.
  568. */
  569. b53_read16(dev, B53_PVLAN_PAGE, B53_PVLAN_PORT_MASK(port), &pvlan);
  570. pvlan &= ~0x1ff;
  571. pvlan |= BIT(port);
  572. pvlan |= dev->ports[port].vlan_ctl_mask;
  573. b53_write16(dev, B53_PVLAN_PAGE, B53_PVLAN_PORT_MASK(port), pvlan);
  574. b53_imp_vlan_setup(ds, cpu_port);
  575. /* If EEE was enabled, restore it */
  576. if (dev->ports[port].eee.eee_enabled)
  577. b53_eee_enable_set(ds, port, true);
  578. return 0;
  579. }
  580. EXPORT_SYMBOL(b53_enable_port);
  581. void b53_disable_port(struct dsa_switch *ds, int port)
  582. {
  583. struct b53_device *dev = ds->priv;
  584. u8 reg;
  585. /* Disable Tx/Rx for the port */
  586. b53_read8(dev, B53_CTRL_PAGE, B53_PORT_CTRL(port), &reg);
  587. reg |= PORT_CTRL_RX_DISABLE | PORT_CTRL_TX_DISABLE;
  588. b53_write8(dev, B53_CTRL_PAGE, B53_PORT_CTRL(port), reg);
  589. if (dev->ops->irq_disable)
  590. dev->ops->irq_disable(dev, port);
  591. }
  592. EXPORT_SYMBOL(b53_disable_port);
  593. void b53_brcm_hdr_setup(struct dsa_switch *ds, int port)
  594. {
  595. struct b53_device *dev = ds->priv;
  596. bool tag_en = !(dev->tag_protocol == DSA_TAG_PROTO_NONE);
  597. u8 hdr_ctl, val;
  598. u16 reg;
  599. /* Resolve which bit controls the Broadcom tag */
  600. switch (port) {
  601. case 8:
  602. val = BRCM_HDR_P8_EN;
  603. break;
  604. case 7:
  605. val = BRCM_HDR_P7_EN;
  606. break;
  607. case 5:
  608. val = BRCM_HDR_P5_EN;
  609. break;
  610. default:
  611. val = 0;
  612. break;
  613. }
  614. /* Enable management mode if tagging is requested */
  615. b53_read8(dev, B53_CTRL_PAGE, B53_SWITCH_MODE, &hdr_ctl);
  616. if (tag_en)
  617. hdr_ctl |= SM_SW_FWD_MODE;
  618. else
  619. hdr_ctl &= ~SM_SW_FWD_MODE;
  620. b53_write8(dev, B53_CTRL_PAGE, B53_SWITCH_MODE, hdr_ctl);
  621. /* Configure the appropriate IMP port */
  622. b53_read8(dev, B53_MGMT_PAGE, B53_GLOBAL_CONFIG, &hdr_ctl);
  623. if (port == 8)
  624. hdr_ctl |= GC_FRM_MGMT_PORT_MII;
  625. else if (port == 5)
  626. hdr_ctl |= GC_FRM_MGMT_PORT_M;
  627. b53_write8(dev, B53_MGMT_PAGE, B53_GLOBAL_CONFIG, hdr_ctl);
  628. /* Enable Broadcom tags for IMP port */
  629. b53_read8(dev, B53_MGMT_PAGE, B53_BRCM_HDR, &hdr_ctl);
  630. if (tag_en)
  631. hdr_ctl |= val;
  632. else
  633. hdr_ctl &= ~val;
  634. b53_write8(dev, B53_MGMT_PAGE, B53_BRCM_HDR, hdr_ctl);
  635. /* Registers below are only accessible on newer devices */
  636. if (!is58xx(dev))
  637. return;
  638. /* Enable reception Broadcom tag for CPU TX (switch RX) to
  639. * allow us to tag outgoing frames
  640. */
  641. b53_read16(dev, B53_MGMT_PAGE, B53_BRCM_HDR_RX_DIS, &reg);
  642. if (tag_en)
  643. reg &= ~BIT(port);
  644. else
  645. reg |= BIT(port);
  646. b53_write16(dev, B53_MGMT_PAGE, B53_BRCM_HDR_RX_DIS, reg);
  647. /* Enable transmission of Broadcom tags from the switch (CPU RX) to
  648. * allow delivering frames to the per-port net_devices
  649. */
  650. b53_read16(dev, B53_MGMT_PAGE, B53_BRCM_HDR_TX_DIS, &reg);
  651. if (tag_en)
  652. reg &= ~BIT(port);
  653. else
  654. reg |= BIT(port);
  655. b53_write16(dev, B53_MGMT_PAGE, B53_BRCM_HDR_TX_DIS, reg);
  656. }
  657. EXPORT_SYMBOL(b53_brcm_hdr_setup);
  658. static void b53_enable_cpu_port(struct b53_device *dev, int port)
  659. {
  660. u8 port_ctrl;
  661. /* BCM5325 CPU port is at 8 */
  662. if ((is5325(dev) || is5365(dev)) && port == B53_CPU_PORT_25)
  663. port = B53_CPU_PORT;
  664. port_ctrl = PORT_CTRL_RX_BCST_EN |
  665. PORT_CTRL_RX_MCST_EN |
  666. PORT_CTRL_RX_UCST_EN;
  667. b53_write8(dev, B53_CTRL_PAGE, B53_PORT_CTRL(port), port_ctrl);
  668. b53_brcm_hdr_setup(dev->ds, port);
  669. }
  670. static void b53_enable_mib(struct b53_device *dev)
  671. {
  672. u8 gc;
  673. b53_read8(dev, B53_MGMT_PAGE, B53_GLOBAL_CONFIG, &gc);
  674. gc &= ~(GC_RESET_MIB | GC_MIB_AC_EN);
  675. b53_write8(dev, B53_MGMT_PAGE, B53_GLOBAL_CONFIG, gc);
  676. }
  677. static void b53_enable_stp(struct b53_device *dev)
  678. {
  679. u8 gc;
  680. b53_read8(dev, B53_MGMT_PAGE, B53_GLOBAL_CONFIG, &gc);
  681. gc |= GC_RX_BPDU_EN;
  682. b53_write8(dev, B53_MGMT_PAGE, B53_GLOBAL_CONFIG, gc);
  683. }
  684. static u16 b53_default_pvid(struct b53_device *dev)
  685. {
  686. if (is5325(dev) || is5365(dev))
  687. return 1;
  688. else
  689. return 0;
  690. }
  691. static bool b53_vlan_port_needs_forced_tagged(struct dsa_switch *ds, int port)
  692. {
  693. struct b53_device *dev = ds->priv;
  694. return dev->tag_protocol == DSA_TAG_PROTO_NONE && dsa_is_cpu_port(ds, port);
  695. }
  696. static bool b53_vlan_port_may_join_untagged(struct dsa_switch *ds, int port)
  697. {
  698. struct b53_device *dev = ds->priv;
  699. struct dsa_port *dp;
  700. if (!dev->vlan_filtering)
  701. return true;
  702. dp = dsa_to_port(ds, port);
  703. if (dsa_port_is_cpu(dp))
  704. return true;
  705. return dp->bridge == NULL;
  706. }
  707. int b53_configure_vlan(struct dsa_switch *ds)
  708. {
  709. struct b53_device *dev = ds->priv;
  710. struct b53_vlan vl = { 0 };
  711. struct b53_vlan *v;
  712. int i, def_vid;
  713. u16 vid;
  714. def_vid = b53_default_pvid(dev);
  715. /* clear all vlan entries */
  716. if (is5325(dev) || is5365(dev)) {
  717. for (i = def_vid; i < dev->num_vlans; i++)
  718. b53_set_vlan_entry(dev, i, &vl);
  719. } else {
  720. b53_do_vlan_op(dev, VTA_CMD_CLEAR);
  721. }
  722. b53_enable_vlan(dev, -1, dev->vlan_enabled, dev->vlan_filtering);
  723. /* Create an untagged VLAN entry for the default PVID in case
  724. * CONFIG_VLAN_8021Q is disabled and there are no calls to
  725. * dsa_user_vlan_rx_add_vid() to create the default VLAN
  726. * entry. Do this only when the tagging protocol is not
  727. * DSA_TAG_PROTO_NONE
  728. */
  729. v = &dev->vlans[def_vid];
  730. b53_for_each_port(dev, i) {
  731. if (!b53_vlan_port_may_join_untagged(ds, i))
  732. continue;
  733. vl.members |= BIT(i);
  734. if (!b53_vlan_port_needs_forced_tagged(ds, i))
  735. vl.untag = vl.members;
  736. b53_write16(dev, B53_VLAN_PAGE, B53_VLAN_PORT_DEF_TAG(i),
  737. def_vid);
  738. }
  739. b53_set_vlan_entry(dev, def_vid, &vl);
  740. if (dev->vlan_filtering) {
  741. /* Upon initial call we have not set-up any VLANs, but upon
  742. * system resume, we need to restore all VLAN entries.
  743. */
  744. for (vid = def_vid + 1; vid < dev->num_vlans; vid++) {
  745. v = &dev->vlans[vid];
  746. if (!v->members)
  747. continue;
  748. b53_set_vlan_entry(dev, vid, v);
  749. b53_fast_age_vlan(dev, vid);
  750. }
  751. b53_for_each_port(dev, i) {
  752. if (!dsa_is_cpu_port(ds, i))
  753. b53_write16(dev, B53_VLAN_PAGE,
  754. B53_VLAN_PORT_DEF_TAG(i),
  755. dev->ports[i].pvid);
  756. }
  757. }
  758. return 0;
  759. }
  760. EXPORT_SYMBOL(b53_configure_vlan);
  761. static void b53_switch_reset_gpio(struct b53_device *dev)
  762. {
  763. int gpio = dev->reset_gpio;
  764. if (gpio < 0)
  765. return;
  766. /* Reset sequence: RESET low(50ms)->high(20ms)
  767. */
  768. gpio_set_value(gpio, 0);
  769. mdelay(50);
  770. gpio_set_value(gpio, 1);
  771. mdelay(20);
  772. dev->current_page = 0xff;
  773. }
  774. static int b53_switch_reset(struct b53_device *dev)
  775. {
  776. unsigned int timeout = 1000;
  777. u8 mgmt, reg;
  778. b53_switch_reset_gpio(dev);
  779. if (is539x(dev)) {
  780. b53_write8(dev, B53_CTRL_PAGE, B53_SOFTRESET, 0x83);
  781. b53_write8(dev, B53_CTRL_PAGE, B53_SOFTRESET, 0x00);
  782. }
  783. /* This is specific to 58xx devices here, do not use is58xx() which
  784. * covers the larger Starfigther 2 family, including 7445/7278 which
  785. * still use this driver as a library and need to perform the reset
  786. * earlier.
  787. */
  788. if (dev->chip_id == BCM58XX_DEVICE_ID ||
  789. dev->chip_id == BCM583XX_DEVICE_ID) {
  790. b53_read8(dev, B53_CTRL_PAGE, B53_SOFTRESET, &reg);
  791. reg |= SW_RST | EN_SW_RST | EN_CH_RST;
  792. b53_write8(dev, B53_CTRL_PAGE, B53_SOFTRESET, reg);
  793. do {
  794. b53_read8(dev, B53_CTRL_PAGE, B53_SOFTRESET, &reg);
  795. if (!(reg & SW_RST))
  796. break;
  797. usleep_range(1000, 2000);
  798. } while (timeout-- > 0);
  799. if (timeout == 0) {
  800. dev_err(dev->dev,
  801. "Timeout waiting for SW_RST to clear!\n");
  802. return -ETIMEDOUT;
  803. }
  804. }
  805. b53_read8(dev, B53_CTRL_PAGE, B53_SWITCH_MODE, &mgmt);
  806. if (!(mgmt & SM_SW_FWD_EN)) {
  807. mgmt &= ~SM_SW_FWD_MODE;
  808. mgmt |= SM_SW_FWD_EN;
  809. b53_write8(dev, B53_CTRL_PAGE, B53_SWITCH_MODE, mgmt);
  810. b53_read8(dev, B53_CTRL_PAGE, B53_SWITCH_MODE, &mgmt);
  811. if (!(mgmt & SM_SW_FWD_EN)) {
  812. dev_err(dev->dev, "Failed to enable switch!\n");
  813. return -EINVAL;
  814. }
  815. }
  816. b53_enable_mib(dev);
  817. b53_enable_stp(dev);
  818. return b53_flush_arl(dev, FAST_AGE_STATIC);
  819. }
  820. static int b53_phy_read16(struct dsa_switch *ds, int addr, int reg)
  821. {
  822. struct b53_device *priv = ds->priv;
  823. u16 value = 0;
  824. int ret;
  825. if (priv->ops->phy_read16)
  826. ret = priv->ops->phy_read16(priv, addr, reg, &value);
  827. else
  828. ret = b53_read16(priv, B53_PORT_MII_PAGE(addr),
  829. reg * 2, &value);
  830. return ret ? ret : value;
  831. }
  832. static int b53_phy_write16(struct dsa_switch *ds, int addr, int reg, u16 val)
  833. {
  834. struct b53_device *priv = ds->priv;
  835. if (priv->ops->phy_write16)
  836. return priv->ops->phy_write16(priv, addr, reg, val);
  837. return b53_write16(priv, B53_PORT_MII_PAGE(addr), reg * 2, val);
  838. }
  839. static int b53_reset_switch(struct b53_device *priv)
  840. {
  841. /* reset vlans */
  842. memset(priv->vlans, 0, sizeof(*priv->vlans) * priv->num_vlans);
  843. memset(priv->ports, 0, sizeof(*priv->ports) * priv->num_ports);
  844. priv->serdes_lane = B53_INVALID_LANE;
  845. return b53_switch_reset(priv);
  846. }
  847. static int b53_apply_config(struct b53_device *priv)
  848. {
  849. /* disable switching */
  850. b53_set_forwarding(priv, 0);
  851. b53_configure_vlan(priv->ds);
  852. /* enable switching */
  853. b53_set_forwarding(priv, 1);
  854. return 0;
  855. }
  856. static void b53_reset_mib(struct b53_device *priv)
  857. {
  858. u8 gc;
  859. b53_read8(priv, B53_MGMT_PAGE, B53_GLOBAL_CONFIG, &gc);
  860. b53_write8(priv, B53_MGMT_PAGE, B53_GLOBAL_CONFIG, gc | GC_RESET_MIB);
  861. msleep(1);
  862. b53_write8(priv, B53_MGMT_PAGE, B53_GLOBAL_CONFIG, gc & ~GC_RESET_MIB);
  863. msleep(1);
  864. }
  865. static const struct b53_mib_desc *b53_get_mib(struct b53_device *dev)
  866. {
  867. if (is5365(dev))
  868. return b53_mibs_65;
  869. else if (is63xx(dev))
  870. return b53_mibs_63xx;
  871. else if (is58xx(dev))
  872. return b53_mibs_58xx;
  873. else
  874. return b53_mibs;
  875. }
  876. static unsigned int b53_get_mib_size(struct b53_device *dev)
  877. {
  878. if (is5365(dev))
  879. return B53_MIBS_65_SIZE;
  880. else if (is63xx(dev))
  881. return B53_MIBS_63XX_SIZE;
  882. else if (is58xx(dev))
  883. return B53_MIBS_58XX_SIZE;
  884. else
  885. return B53_MIBS_SIZE;
  886. }
  887. static struct phy_device *b53_get_phy_device(struct dsa_switch *ds, int port)
  888. {
  889. /* These ports typically do not have built-in PHYs */
  890. switch (port) {
  891. case B53_CPU_PORT_25:
  892. case 7:
  893. case B53_CPU_PORT:
  894. return NULL;
  895. }
  896. return mdiobus_get_phy(ds->user_mii_bus, port);
  897. }
  898. void b53_get_strings(struct dsa_switch *ds, int port, u32 stringset,
  899. uint8_t *data)
  900. {
  901. struct b53_device *dev = ds->priv;
  902. const struct b53_mib_desc *mibs = b53_get_mib(dev);
  903. unsigned int mib_size = b53_get_mib_size(dev);
  904. struct phy_device *phydev;
  905. unsigned int i;
  906. if (stringset == ETH_SS_STATS) {
  907. for (i = 0; i < mib_size; i++)
  908. strscpy(data + i * ETH_GSTRING_LEN,
  909. mibs[i].name, ETH_GSTRING_LEN);
  910. } else if (stringset == ETH_SS_PHY_STATS) {
  911. phydev = b53_get_phy_device(ds, port);
  912. if (!phydev)
  913. return;
  914. phy_ethtool_get_strings(phydev, data);
  915. }
  916. }
  917. EXPORT_SYMBOL(b53_get_strings);
  918. void b53_get_ethtool_stats(struct dsa_switch *ds, int port, uint64_t *data)
  919. {
  920. struct b53_device *dev = ds->priv;
  921. const struct b53_mib_desc *mibs = b53_get_mib(dev);
  922. unsigned int mib_size = b53_get_mib_size(dev);
  923. const struct b53_mib_desc *s;
  924. unsigned int i;
  925. u64 val = 0;
  926. if (is5365(dev) && port == 5)
  927. port = 8;
  928. mutex_lock(&dev->stats_mutex);
  929. for (i = 0; i < mib_size; i++) {
  930. s = &mibs[i];
  931. if (s->size == 8) {
  932. b53_read64(dev, B53_MIB_PAGE(port), s->offset, &val);
  933. } else {
  934. u32 val32;
  935. b53_read32(dev, B53_MIB_PAGE(port), s->offset,
  936. &val32);
  937. val = val32;
  938. }
  939. data[i] = (u64)val;
  940. }
  941. mutex_unlock(&dev->stats_mutex);
  942. }
  943. EXPORT_SYMBOL(b53_get_ethtool_stats);
  944. void b53_get_ethtool_phy_stats(struct dsa_switch *ds, int port, uint64_t *data)
  945. {
  946. struct phy_device *phydev;
  947. phydev = b53_get_phy_device(ds, port);
  948. if (!phydev)
  949. return;
  950. phy_ethtool_get_stats(phydev, NULL, data);
  951. }
  952. EXPORT_SYMBOL(b53_get_ethtool_phy_stats);
  953. int b53_get_sset_count(struct dsa_switch *ds, int port, int sset)
  954. {
  955. struct b53_device *dev = ds->priv;
  956. struct phy_device *phydev;
  957. if (sset == ETH_SS_STATS) {
  958. return b53_get_mib_size(dev);
  959. } else if (sset == ETH_SS_PHY_STATS) {
  960. phydev = b53_get_phy_device(ds, port);
  961. if (!phydev)
  962. return 0;
  963. return phy_ethtool_get_sset_count(phydev);
  964. }
  965. return 0;
  966. }
  967. EXPORT_SYMBOL(b53_get_sset_count);
  968. enum b53_devlink_resource_id {
  969. B53_DEVLINK_PARAM_ID_VLAN_TABLE,
  970. };
  971. static u64 b53_devlink_vlan_table_get(void *priv)
  972. {
  973. struct b53_device *dev = priv;
  974. struct b53_vlan *vl;
  975. unsigned int i;
  976. u64 count = 0;
  977. for (i = 0; i < dev->num_vlans; i++) {
  978. vl = &dev->vlans[i];
  979. if (vl->members)
  980. count++;
  981. }
  982. return count;
  983. }
  984. int b53_setup_devlink_resources(struct dsa_switch *ds)
  985. {
  986. struct devlink_resource_size_params size_params;
  987. struct b53_device *dev = ds->priv;
  988. int err;
  989. devlink_resource_size_params_init(&size_params, dev->num_vlans,
  990. dev->num_vlans,
  991. 1, DEVLINK_RESOURCE_UNIT_ENTRY);
  992. err = dsa_devlink_resource_register(ds, "VLAN", dev->num_vlans,
  993. B53_DEVLINK_PARAM_ID_VLAN_TABLE,
  994. DEVLINK_RESOURCE_ID_PARENT_TOP,
  995. &size_params);
  996. if (err)
  997. goto out;
  998. dsa_devlink_resource_occ_get_register(ds,
  999. B53_DEVLINK_PARAM_ID_VLAN_TABLE,
  1000. b53_devlink_vlan_table_get, dev);
  1001. return 0;
  1002. out:
  1003. dsa_devlink_resources_unregister(ds);
  1004. return err;
  1005. }
  1006. EXPORT_SYMBOL(b53_setup_devlink_resources);
  1007. static int b53_setup(struct dsa_switch *ds)
  1008. {
  1009. struct b53_device *dev = ds->priv;
  1010. struct b53_vlan *vl;
  1011. unsigned int port;
  1012. u16 pvid;
  1013. int ret;
  1014. /* Request bridge PVID untagged when DSA_TAG_PROTO_NONE is set
  1015. * which forces the CPU port to be tagged in all VLANs.
  1016. */
  1017. ds->untag_bridge_pvid = dev->tag_protocol == DSA_TAG_PROTO_NONE;
  1018. /* The switch does not tell us the original VLAN for untagged
  1019. * packets, so keep the CPU port always tagged.
  1020. */
  1021. ds->untag_vlan_aware_bridge_pvid = true;
  1022. ret = b53_reset_switch(dev);
  1023. if (ret) {
  1024. dev_err(ds->dev, "failed to reset switch\n");
  1025. return ret;
  1026. }
  1027. /* setup default vlan for filtering mode */
  1028. pvid = b53_default_pvid(dev);
  1029. vl = &dev->vlans[pvid];
  1030. b53_for_each_port(dev, port) {
  1031. vl->members |= BIT(port);
  1032. if (!b53_vlan_port_needs_forced_tagged(ds, port))
  1033. vl->untag |= BIT(port);
  1034. }
  1035. b53_reset_mib(dev);
  1036. ret = b53_apply_config(dev);
  1037. if (ret) {
  1038. dev_err(ds->dev, "failed to apply configuration\n");
  1039. return ret;
  1040. }
  1041. /* Configure IMP/CPU port, disable all other ports. Enabled
  1042. * ports will be configured with .port_enable
  1043. */
  1044. for (port = 0; port < dev->num_ports; port++) {
  1045. if (dsa_is_cpu_port(ds, port))
  1046. b53_enable_cpu_port(dev, port);
  1047. else
  1048. b53_disable_port(ds, port);
  1049. }
  1050. return b53_setup_devlink_resources(ds);
  1051. }
  1052. static void b53_teardown(struct dsa_switch *ds)
  1053. {
  1054. dsa_devlink_resources_unregister(ds);
  1055. }
  1056. static void b53_force_link(struct b53_device *dev, int port, int link)
  1057. {
  1058. u8 reg, val, off;
  1059. /* Override the port settings */
  1060. if (port == dev->imp_port) {
  1061. off = B53_PORT_OVERRIDE_CTRL;
  1062. val = PORT_OVERRIDE_EN;
  1063. } else if (is5325(dev)) {
  1064. return;
  1065. } else {
  1066. off = B53_GMII_PORT_OVERRIDE_CTRL(port);
  1067. val = GMII_PO_EN;
  1068. }
  1069. b53_read8(dev, B53_CTRL_PAGE, off, &reg);
  1070. reg |= val;
  1071. if (link)
  1072. reg |= PORT_OVERRIDE_LINK;
  1073. else
  1074. reg &= ~PORT_OVERRIDE_LINK;
  1075. b53_write8(dev, B53_CTRL_PAGE, off, reg);
  1076. }
  1077. static void b53_force_port_config(struct b53_device *dev, int port,
  1078. int speed, int duplex,
  1079. bool tx_pause, bool rx_pause)
  1080. {
  1081. u8 reg, val, off;
  1082. /* Override the port settings */
  1083. if (port == dev->imp_port) {
  1084. off = B53_PORT_OVERRIDE_CTRL;
  1085. val = PORT_OVERRIDE_EN;
  1086. } else if (is5325(dev)) {
  1087. return;
  1088. } else {
  1089. off = B53_GMII_PORT_OVERRIDE_CTRL(port);
  1090. val = GMII_PO_EN;
  1091. }
  1092. b53_read8(dev, B53_CTRL_PAGE, off, &reg);
  1093. reg |= val;
  1094. if (duplex == DUPLEX_FULL)
  1095. reg |= PORT_OVERRIDE_FULL_DUPLEX;
  1096. else
  1097. reg &= ~PORT_OVERRIDE_FULL_DUPLEX;
  1098. switch (speed) {
  1099. case 2000:
  1100. reg |= PORT_OVERRIDE_SPEED_2000M;
  1101. fallthrough;
  1102. case SPEED_1000:
  1103. reg |= PORT_OVERRIDE_SPEED_1000M;
  1104. break;
  1105. case SPEED_100:
  1106. reg |= PORT_OVERRIDE_SPEED_100M;
  1107. break;
  1108. case SPEED_10:
  1109. reg |= PORT_OVERRIDE_SPEED_10M;
  1110. break;
  1111. default:
  1112. dev_err(dev->dev, "unknown speed: %d\n", speed);
  1113. return;
  1114. }
  1115. if (rx_pause) {
  1116. if (is5325(dev))
  1117. reg |= PORT_OVERRIDE_LP_FLOW_25;
  1118. else
  1119. reg |= PORT_OVERRIDE_RX_FLOW;
  1120. }
  1121. if (tx_pause) {
  1122. if (is5325(dev))
  1123. reg |= PORT_OVERRIDE_LP_FLOW_25;
  1124. else
  1125. reg |= PORT_OVERRIDE_TX_FLOW;
  1126. }
  1127. b53_write8(dev, B53_CTRL_PAGE, off, reg);
  1128. }
  1129. static void b53_adjust_63xx_rgmii(struct dsa_switch *ds, int port,
  1130. phy_interface_t interface)
  1131. {
  1132. struct b53_device *dev = ds->priv;
  1133. u8 rgmii_ctrl = 0, off;
  1134. if (port == dev->imp_port)
  1135. off = B53_RGMII_CTRL_IMP;
  1136. else
  1137. off = B53_RGMII_CTRL_P(port);
  1138. b53_read8(dev, B53_CTRL_PAGE, off, &rgmii_ctrl);
  1139. rgmii_ctrl &= ~(RGMII_CTRL_DLL_RXC | RGMII_CTRL_DLL_TXC);
  1140. if (port != dev->imp_port) {
  1141. if (is63268(dev))
  1142. rgmii_ctrl |= RGMII_CTRL_MII_OVERRIDE;
  1143. rgmii_ctrl |= RGMII_CTRL_ENABLE_GMII;
  1144. }
  1145. b53_write8(dev, B53_CTRL_PAGE, off, rgmii_ctrl);
  1146. dev_dbg(ds->dev, "Configured port %d for %s\n", port,
  1147. phy_modes(interface));
  1148. }
  1149. static void b53_adjust_531x5_rgmii(struct dsa_switch *ds, int port,
  1150. phy_interface_t interface)
  1151. {
  1152. struct b53_device *dev = ds->priv;
  1153. u8 rgmii_ctrl = 0, off;
  1154. if (port == dev->imp_port)
  1155. off = B53_RGMII_CTRL_IMP;
  1156. else
  1157. off = B53_RGMII_CTRL_P(port);
  1158. /* Configure the port RGMII clock delay by DLL disabled and
  1159. * tx_clk aligned timing (restoring to reset defaults)
  1160. */
  1161. b53_read8(dev, B53_CTRL_PAGE, off, &rgmii_ctrl);
  1162. rgmii_ctrl &= ~(RGMII_CTRL_DLL_RXC | RGMII_CTRL_DLL_TXC);
  1163. /* PHY_INTERFACE_MODE_RGMII_TXID means TX internal delay, make
  1164. * sure that we enable the port TX clock internal delay to
  1165. * account for this internal delay that is inserted, otherwise
  1166. * the switch won't be able to receive correctly.
  1167. *
  1168. * PHY_INTERFACE_MODE_RGMII means that we are not introducing
  1169. * any delay neither on transmission nor reception, so the
  1170. * BCM53125 must also be configured accordingly to account for
  1171. * the lack of delay and introduce
  1172. *
  1173. * The BCM53125 switch has its RX clock and TX clock control
  1174. * swapped, hence the reason why we modify the TX clock path in
  1175. * the "RGMII" case
  1176. */
  1177. if (interface == PHY_INTERFACE_MODE_RGMII_TXID)
  1178. rgmii_ctrl |= RGMII_CTRL_DLL_TXC;
  1179. if (interface == PHY_INTERFACE_MODE_RGMII)
  1180. rgmii_ctrl |= RGMII_CTRL_DLL_TXC | RGMII_CTRL_DLL_RXC;
  1181. if (dev->chip_id != BCM53115_DEVICE_ID)
  1182. rgmii_ctrl |= RGMII_CTRL_TIMING_SEL;
  1183. b53_write8(dev, B53_CTRL_PAGE, off, rgmii_ctrl);
  1184. dev_info(ds->dev, "Configured port %d for %s\n", port,
  1185. phy_modes(interface));
  1186. }
  1187. static void b53_adjust_5325_mii(struct dsa_switch *ds, int port)
  1188. {
  1189. struct b53_device *dev = ds->priv;
  1190. u8 reg = 0;
  1191. b53_read8(dev, B53_CTRL_PAGE, B53_PORT_OVERRIDE_CTRL,
  1192. &reg);
  1193. /* reverse mii needs to be enabled */
  1194. if (!(reg & PORT_OVERRIDE_RV_MII_25)) {
  1195. b53_write8(dev, B53_CTRL_PAGE, B53_PORT_OVERRIDE_CTRL,
  1196. reg | PORT_OVERRIDE_RV_MII_25);
  1197. b53_read8(dev, B53_CTRL_PAGE, B53_PORT_OVERRIDE_CTRL,
  1198. &reg);
  1199. if (!(reg & PORT_OVERRIDE_RV_MII_25)) {
  1200. dev_err(ds->dev,
  1201. "Failed to enable reverse MII mode\n");
  1202. return;
  1203. }
  1204. }
  1205. }
  1206. void b53_port_event(struct dsa_switch *ds, int port)
  1207. {
  1208. struct b53_device *dev = ds->priv;
  1209. bool link;
  1210. u16 sts;
  1211. b53_read16(dev, B53_STAT_PAGE, B53_LINK_STAT, &sts);
  1212. link = !!(sts & BIT(port));
  1213. dsa_port_phylink_mac_change(ds, port, link);
  1214. }
  1215. EXPORT_SYMBOL(b53_port_event);
  1216. static void b53_phylink_get_caps(struct dsa_switch *ds, int port,
  1217. struct phylink_config *config)
  1218. {
  1219. struct b53_device *dev = ds->priv;
  1220. /* Internal ports need GMII for PHYLIB */
  1221. __set_bit(PHY_INTERFACE_MODE_GMII, config->supported_interfaces);
  1222. /* These switches appear to support MII and RevMII too, but beyond
  1223. * this, the code gives very few clues. FIXME: We probably need more
  1224. * interface modes here.
  1225. *
  1226. * According to b53_srab_mux_init(), ports 3..5 can support:
  1227. * SGMII, MII, GMII, RGMII or INTERNAL depending on the MUX setting.
  1228. * However, the interface mode read from the MUX configuration is
  1229. * not passed back to DSA, so phylink uses NA.
  1230. * DT can specify RGMII for ports 0, 1.
  1231. * For MDIO, port 8 can be RGMII_TXID.
  1232. */
  1233. __set_bit(PHY_INTERFACE_MODE_MII, config->supported_interfaces);
  1234. __set_bit(PHY_INTERFACE_MODE_REVMII, config->supported_interfaces);
  1235. /* BCM63xx RGMII ports support RGMII */
  1236. if (is63xx(dev) && in_range(port, B53_63XX_RGMII0, 4))
  1237. phy_interface_set_rgmii(config->supported_interfaces);
  1238. config->mac_capabilities = MAC_ASYM_PAUSE | MAC_SYM_PAUSE |
  1239. MAC_10 | MAC_100;
  1240. /* 5325/5365 are not capable of gigabit speeds, everything else is.
  1241. * Note: the original code also exclulded Gigagbit for MII, RevMII
  1242. * and 802.3z modes. MII and RevMII are not able to work above 100M,
  1243. * so will be excluded by the generic validator implementation.
  1244. * However, the exclusion of Gigabit for 802.3z just seems wrong.
  1245. */
  1246. if (!(is5325(dev) || is5365(dev)))
  1247. config->mac_capabilities |= MAC_1000;
  1248. /* Get the implementation specific capabilities */
  1249. if (dev->ops->phylink_get_caps)
  1250. dev->ops->phylink_get_caps(dev, port, config);
  1251. }
  1252. static struct phylink_pcs *b53_phylink_mac_select_pcs(struct phylink_config *config,
  1253. phy_interface_t interface)
  1254. {
  1255. struct dsa_port *dp = dsa_phylink_to_port(config);
  1256. struct b53_device *dev = dp->ds->priv;
  1257. if (!dev->ops->phylink_mac_select_pcs)
  1258. return NULL;
  1259. return dev->ops->phylink_mac_select_pcs(dev, dp->index, interface);
  1260. }
  1261. static void b53_phylink_mac_config(struct phylink_config *config,
  1262. unsigned int mode,
  1263. const struct phylink_link_state *state)
  1264. {
  1265. struct dsa_port *dp = dsa_phylink_to_port(config);
  1266. phy_interface_t interface = state->interface;
  1267. struct dsa_switch *ds = dp->ds;
  1268. struct b53_device *dev = ds->priv;
  1269. int port = dp->index;
  1270. if (is63xx(dev) && port >= B53_63XX_RGMII0)
  1271. b53_adjust_63xx_rgmii(ds, port, interface);
  1272. if (mode == MLO_AN_FIXED) {
  1273. if (is531x5(dev) && phy_interface_mode_is_rgmii(interface))
  1274. b53_adjust_531x5_rgmii(ds, port, interface);
  1275. /* configure MII port if necessary */
  1276. if (is5325(dev))
  1277. b53_adjust_5325_mii(ds, port);
  1278. }
  1279. }
  1280. static void b53_phylink_mac_link_down(struct phylink_config *config,
  1281. unsigned int mode,
  1282. phy_interface_t interface)
  1283. {
  1284. struct dsa_port *dp = dsa_phylink_to_port(config);
  1285. struct b53_device *dev = dp->ds->priv;
  1286. int port = dp->index;
  1287. if (mode == MLO_AN_PHY)
  1288. return;
  1289. if (mode == MLO_AN_FIXED) {
  1290. b53_force_link(dev, port, false);
  1291. return;
  1292. }
  1293. if (phy_interface_mode_is_8023z(interface) &&
  1294. dev->ops->serdes_link_set)
  1295. dev->ops->serdes_link_set(dev, port, mode, interface, false);
  1296. }
  1297. static void b53_phylink_mac_link_up(struct phylink_config *config,
  1298. struct phy_device *phydev,
  1299. unsigned int mode,
  1300. phy_interface_t interface,
  1301. int speed, int duplex,
  1302. bool tx_pause, bool rx_pause)
  1303. {
  1304. struct dsa_port *dp = dsa_phylink_to_port(config);
  1305. struct dsa_switch *ds = dp->ds;
  1306. struct b53_device *dev = ds->priv;
  1307. struct ethtool_keee *p = &dev->ports[dp->index].eee;
  1308. int port = dp->index;
  1309. if (mode == MLO_AN_PHY) {
  1310. /* Re-negotiate EEE if it was enabled already */
  1311. p->eee_enabled = b53_eee_init(ds, port, phydev);
  1312. return;
  1313. }
  1314. if (mode == MLO_AN_FIXED) {
  1315. /* Force flow control on BCM5301x's CPU port */
  1316. if (is5301x(dev) && dsa_is_cpu_port(ds, port))
  1317. tx_pause = rx_pause = true;
  1318. b53_force_port_config(dev, port, speed, duplex,
  1319. tx_pause, rx_pause);
  1320. b53_force_link(dev, port, true);
  1321. return;
  1322. }
  1323. if (phy_interface_mode_is_8023z(interface) &&
  1324. dev->ops->serdes_link_set)
  1325. dev->ops->serdes_link_set(dev, port, mode, interface, true);
  1326. }
  1327. int b53_vlan_filtering(struct dsa_switch *ds, int port, bool vlan_filtering,
  1328. struct netlink_ext_ack *extack)
  1329. {
  1330. struct b53_device *dev = ds->priv;
  1331. if (dev->vlan_filtering != vlan_filtering) {
  1332. dev->vlan_filtering = vlan_filtering;
  1333. b53_apply_config(dev);
  1334. }
  1335. return 0;
  1336. }
  1337. EXPORT_SYMBOL(b53_vlan_filtering);
  1338. static int b53_vlan_prepare(struct dsa_switch *ds, int port,
  1339. const struct switchdev_obj_port_vlan *vlan)
  1340. {
  1341. struct b53_device *dev = ds->priv;
  1342. if ((is5325(dev) || is5365(dev)) && vlan->vid == 0)
  1343. return -EOPNOTSUPP;
  1344. /* Port 7 on 7278 connects to the ASP's UniMAC which is not capable of
  1345. * receiving VLAN tagged frames at all, we can still allow the port to
  1346. * be configured for egress untagged.
  1347. */
  1348. if (dev->chip_id == BCM7278_DEVICE_ID && port == 7 &&
  1349. !(vlan->flags & BRIDGE_VLAN_INFO_UNTAGGED))
  1350. return -EINVAL;
  1351. if (vlan->vid >= dev->num_vlans)
  1352. return -ERANGE;
  1353. b53_enable_vlan(dev, port, true, dev->vlan_filtering);
  1354. return 0;
  1355. }
  1356. int b53_vlan_add(struct dsa_switch *ds, int port,
  1357. const struct switchdev_obj_port_vlan *vlan,
  1358. struct netlink_ext_ack *extack)
  1359. {
  1360. struct b53_device *dev = ds->priv;
  1361. bool untagged = vlan->flags & BRIDGE_VLAN_INFO_UNTAGGED;
  1362. bool pvid = vlan->flags & BRIDGE_VLAN_INFO_PVID;
  1363. struct b53_vlan *vl;
  1364. u16 old_pvid, new_pvid;
  1365. int err;
  1366. err = b53_vlan_prepare(ds, port, vlan);
  1367. if (err)
  1368. return err;
  1369. if (vlan->vid == 0)
  1370. return 0;
  1371. old_pvid = dev->ports[port].pvid;
  1372. if (pvid)
  1373. new_pvid = vlan->vid;
  1374. else if (!pvid && vlan->vid == old_pvid)
  1375. new_pvid = b53_default_pvid(dev);
  1376. else
  1377. new_pvid = old_pvid;
  1378. dev->ports[port].pvid = new_pvid;
  1379. vl = &dev->vlans[vlan->vid];
  1380. if (dsa_is_cpu_port(ds, port))
  1381. untagged = false;
  1382. vl->members |= BIT(port);
  1383. if (untagged && !b53_vlan_port_needs_forced_tagged(ds, port))
  1384. vl->untag |= BIT(port);
  1385. else
  1386. vl->untag &= ~BIT(port);
  1387. if (!dev->vlan_filtering)
  1388. return 0;
  1389. b53_set_vlan_entry(dev, vlan->vid, vl);
  1390. b53_fast_age_vlan(dev, vlan->vid);
  1391. if (!dsa_is_cpu_port(ds, port) && new_pvid != old_pvid) {
  1392. b53_write16(dev, B53_VLAN_PAGE, B53_VLAN_PORT_DEF_TAG(port),
  1393. new_pvid);
  1394. b53_fast_age_vlan(dev, old_pvid);
  1395. }
  1396. return 0;
  1397. }
  1398. EXPORT_SYMBOL(b53_vlan_add);
  1399. int b53_vlan_del(struct dsa_switch *ds, int port,
  1400. const struct switchdev_obj_port_vlan *vlan)
  1401. {
  1402. struct b53_device *dev = ds->priv;
  1403. bool untagged = vlan->flags & BRIDGE_VLAN_INFO_UNTAGGED;
  1404. struct b53_vlan *vl;
  1405. u16 pvid;
  1406. if (vlan->vid == 0)
  1407. return 0;
  1408. pvid = dev->ports[port].pvid;
  1409. vl = &dev->vlans[vlan->vid];
  1410. vl->members &= ~BIT(port);
  1411. if (pvid == vlan->vid)
  1412. pvid = b53_default_pvid(dev);
  1413. dev->ports[port].pvid = pvid;
  1414. if (untagged && !b53_vlan_port_needs_forced_tagged(ds, port))
  1415. vl->untag &= ~(BIT(port));
  1416. if (!dev->vlan_filtering)
  1417. return 0;
  1418. b53_set_vlan_entry(dev, vlan->vid, vl);
  1419. b53_fast_age_vlan(dev, vlan->vid);
  1420. b53_write16(dev, B53_VLAN_PAGE, B53_VLAN_PORT_DEF_TAG(port), pvid);
  1421. b53_fast_age_vlan(dev, pvid);
  1422. return 0;
  1423. }
  1424. EXPORT_SYMBOL(b53_vlan_del);
  1425. /* Address Resolution Logic routines. Caller must hold &dev->arl_mutex. */
  1426. static int b53_arl_op_wait(struct b53_device *dev)
  1427. {
  1428. unsigned int timeout = 10;
  1429. u8 reg;
  1430. do {
  1431. b53_read8(dev, B53_ARLIO_PAGE, B53_ARLTBL_RW_CTRL, &reg);
  1432. if (!(reg & ARLTBL_START_DONE))
  1433. return 0;
  1434. usleep_range(1000, 2000);
  1435. } while (timeout--);
  1436. dev_warn(dev->dev, "timeout waiting for ARL to finish: 0x%02x\n", reg);
  1437. return -ETIMEDOUT;
  1438. }
  1439. static int b53_arl_rw_op(struct b53_device *dev, unsigned int op)
  1440. {
  1441. u8 reg;
  1442. if (op > ARLTBL_RW)
  1443. return -EINVAL;
  1444. b53_read8(dev, B53_ARLIO_PAGE, B53_ARLTBL_RW_CTRL, &reg);
  1445. reg |= ARLTBL_START_DONE;
  1446. if (op)
  1447. reg |= ARLTBL_RW;
  1448. else
  1449. reg &= ~ARLTBL_RW;
  1450. if (dev->vlan_enabled)
  1451. reg &= ~ARLTBL_IVL_SVL_SELECT;
  1452. else
  1453. reg |= ARLTBL_IVL_SVL_SELECT;
  1454. b53_write8(dev, B53_ARLIO_PAGE, B53_ARLTBL_RW_CTRL, reg);
  1455. return b53_arl_op_wait(dev);
  1456. }
  1457. static int b53_arl_read(struct b53_device *dev, u64 mac,
  1458. u16 vid, struct b53_arl_entry *ent, u8 *idx)
  1459. {
  1460. DECLARE_BITMAP(free_bins, B53_ARLTBL_MAX_BIN_ENTRIES);
  1461. unsigned int i;
  1462. int ret;
  1463. ret = b53_arl_op_wait(dev);
  1464. if (ret)
  1465. return ret;
  1466. bitmap_zero(free_bins, dev->num_arl_bins);
  1467. /* Read the bins */
  1468. for (i = 0; i < dev->num_arl_bins; i++) {
  1469. u64 mac_vid;
  1470. u32 fwd_entry;
  1471. b53_read64(dev, B53_ARLIO_PAGE,
  1472. B53_ARLTBL_MAC_VID_ENTRY(i), &mac_vid);
  1473. b53_read32(dev, B53_ARLIO_PAGE,
  1474. B53_ARLTBL_DATA_ENTRY(i), &fwd_entry);
  1475. b53_arl_to_entry(ent, mac_vid, fwd_entry);
  1476. if (!(fwd_entry & ARLTBL_VALID)) {
  1477. set_bit(i, free_bins);
  1478. continue;
  1479. }
  1480. if ((mac_vid & ARLTBL_MAC_MASK) != mac)
  1481. continue;
  1482. if (dev->vlan_enabled &&
  1483. ((mac_vid >> ARLTBL_VID_S) & ARLTBL_VID_MASK) != vid)
  1484. continue;
  1485. *idx = i;
  1486. return 0;
  1487. }
  1488. *idx = find_first_bit(free_bins, dev->num_arl_bins);
  1489. return *idx >= dev->num_arl_bins ? -ENOSPC : -ENOENT;
  1490. }
  1491. static int b53_arl_op(struct b53_device *dev, int op, int port,
  1492. const unsigned char *addr, u16 vid, bool is_valid)
  1493. {
  1494. struct b53_arl_entry ent;
  1495. u32 fwd_entry;
  1496. u64 mac, mac_vid = 0;
  1497. u8 idx = 0;
  1498. int ret;
  1499. /* Convert the array into a 64-bit MAC */
  1500. mac = ether_addr_to_u64(addr);
  1501. /* Perform a read for the given MAC and VID */
  1502. b53_write48(dev, B53_ARLIO_PAGE, B53_MAC_ADDR_IDX, mac);
  1503. b53_write16(dev, B53_ARLIO_PAGE, B53_VLAN_ID_IDX, vid);
  1504. /* Issue a read operation for this MAC */
  1505. ret = b53_arl_rw_op(dev, 1);
  1506. if (ret)
  1507. return ret;
  1508. ret = b53_arl_read(dev, mac, vid, &ent, &idx);
  1509. /* If this is a read, just finish now */
  1510. if (op)
  1511. return ret;
  1512. switch (ret) {
  1513. case -ETIMEDOUT:
  1514. return ret;
  1515. case -ENOSPC:
  1516. dev_dbg(dev->dev, "{%pM,%.4d} no space left in ARL\n",
  1517. addr, vid);
  1518. return is_valid ? ret : 0;
  1519. case -ENOENT:
  1520. /* We could not find a matching MAC, so reset to a new entry */
  1521. dev_dbg(dev->dev, "{%pM,%.4d} not found, using idx: %d\n",
  1522. addr, vid, idx);
  1523. fwd_entry = 0;
  1524. break;
  1525. default:
  1526. dev_dbg(dev->dev, "{%pM,%.4d} found, using idx: %d\n",
  1527. addr, vid, idx);
  1528. break;
  1529. }
  1530. /* For multicast address, the port is a bitmask and the validity
  1531. * is determined by having at least one port being still active
  1532. */
  1533. if (!is_multicast_ether_addr(addr)) {
  1534. ent.port = port;
  1535. ent.is_valid = is_valid;
  1536. } else {
  1537. if (is_valid)
  1538. ent.port |= BIT(port);
  1539. else
  1540. ent.port &= ~BIT(port);
  1541. ent.is_valid = !!(ent.port);
  1542. }
  1543. ent.vid = vid;
  1544. ent.is_static = true;
  1545. ent.is_age = false;
  1546. memcpy(ent.mac, addr, ETH_ALEN);
  1547. b53_arl_from_entry(&mac_vid, &fwd_entry, &ent);
  1548. b53_write64(dev, B53_ARLIO_PAGE,
  1549. B53_ARLTBL_MAC_VID_ENTRY(idx), mac_vid);
  1550. b53_write32(dev, B53_ARLIO_PAGE,
  1551. B53_ARLTBL_DATA_ENTRY(idx), fwd_entry);
  1552. return b53_arl_rw_op(dev, 0);
  1553. }
  1554. int b53_fdb_add(struct dsa_switch *ds, int port,
  1555. const unsigned char *addr, u16 vid,
  1556. struct dsa_db db)
  1557. {
  1558. struct b53_device *priv = ds->priv;
  1559. int ret;
  1560. /* 5325 and 5365 require some more massaging, but could
  1561. * be supported eventually
  1562. */
  1563. if (is5325(priv) || is5365(priv))
  1564. return -EOPNOTSUPP;
  1565. mutex_lock(&priv->arl_mutex);
  1566. ret = b53_arl_op(priv, 0, port, addr, vid, true);
  1567. mutex_unlock(&priv->arl_mutex);
  1568. return ret;
  1569. }
  1570. EXPORT_SYMBOL(b53_fdb_add);
  1571. int b53_fdb_del(struct dsa_switch *ds, int port,
  1572. const unsigned char *addr, u16 vid,
  1573. struct dsa_db db)
  1574. {
  1575. struct b53_device *priv = ds->priv;
  1576. int ret;
  1577. mutex_lock(&priv->arl_mutex);
  1578. ret = b53_arl_op(priv, 0, port, addr, vid, false);
  1579. mutex_unlock(&priv->arl_mutex);
  1580. return ret;
  1581. }
  1582. EXPORT_SYMBOL(b53_fdb_del);
  1583. static int b53_arl_search_wait(struct b53_device *dev)
  1584. {
  1585. unsigned int timeout = 1000;
  1586. u8 reg;
  1587. do {
  1588. b53_read8(dev, B53_ARLIO_PAGE, B53_ARL_SRCH_CTL, &reg);
  1589. if (!(reg & ARL_SRCH_STDN))
  1590. return 0;
  1591. if (reg & ARL_SRCH_VLID)
  1592. return 0;
  1593. usleep_range(1000, 2000);
  1594. } while (timeout--);
  1595. return -ETIMEDOUT;
  1596. }
  1597. static void b53_arl_search_rd(struct b53_device *dev, u8 idx,
  1598. struct b53_arl_entry *ent)
  1599. {
  1600. u64 mac_vid;
  1601. u32 fwd_entry;
  1602. b53_read64(dev, B53_ARLIO_PAGE,
  1603. B53_ARL_SRCH_RSTL_MACVID(idx), &mac_vid);
  1604. b53_read32(dev, B53_ARLIO_PAGE,
  1605. B53_ARL_SRCH_RSTL(idx), &fwd_entry);
  1606. b53_arl_to_entry(ent, mac_vid, fwd_entry);
  1607. }
  1608. static int b53_fdb_copy(int port, const struct b53_arl_entry *ent,
  1609. dsa_fdb_dump_cb_t *cb, void *data)
  1610. {
  1611. if (!ent->is_valid)
  1612. return 0;
  1613. if (port != ent->port)
  1614. return 0;
  1615. return cb(ent->mac, ent->vid, ent->is_static, data);
  1616. }
  1617. int b53_fdb_dump(struct dsa_switch *ds, int port,
  1618. dsa_fdb_dump_cb_t *cb, void *data)
  1619. {
  1620. struct b53_device *priv = ds->priv;
  1621. struct b53_arl_entry results[2];
  1622. unsigned int count = 0;
  1623. int ret;
  1624. u8 reg;
  1625. mutex_lock(&priv->arl_mutex);
  1626. /* Start search operation */
  1627. reg = ARL_SRCH_STDN;
  1628. b53_write8(priv, B53_ARLIO_PAGE, B53_ARL_SRCH_CTL, reg);
  1629. do {
  1630. ret = b53_arl_search_wait(priv);
  1631. if (ret)
  1632. break;
  1633. b53_arl_search_rd(priv, 0, &results[0]);
  1634. ret = b53_fdb_copy(port, &results[0], cb, data);
  1635. if (ret)
  1636. break;
  1637. if (priv->num_arl_bins > 2) {
  1638. b53_arl_search_rd(priv, 1, &results[1]);
  1639. ret = b53_fdb_copy(port, &results[1], cb, data);
  1640. if (ret)
  1641. break;
  1642. if (!results[0].is_valid && !results[1].is_valid)
  1643. break;
  1644. }
  1645. } while (count++ < b53_max_arl_entries(priv) / 2);
  1646. mutex_unlock(&priv->arl_mutex);
  1647. return 0;
  1648. }
  1649. EXPORT_SYMBOL(b53_fdb_dump);
  1650. int b53_mdb_add(struct dsa_switch *ds, int port,
  1651. const struct switchdev_obj_port_mdb *mdb,
  1652. struct dsa_db db)
  1653. {
  1654. struct b53_device *priv = ds->priv;
  1655. int ret;
  1656. /* 5325 and 5365 require some more massaging, but could
  1657. * be supported eventually
  1658. */
  1659. if (is5325(priv) || is5365(priv))
  1660. return -EOPNOTSUPP;
  1661. mutex_lock(&priv->arl_mutex);
  1662. ret = b53_arl_op(priv, 0, port, mdb->addr, mdb->vid, true);
  1663. mutex_unlock(&priv->arl_mutex);
  1664. return ret;
  1665. }
  1666. EXPORT_SYMBOL(b53_mdb_add);
  1667. int b53_mdb_del(struct dsa_switch *ds, int port,
  1668. const struct switchdev_obj_port_mdb *mdb,
  1669. struct dsa_db db)
  1670. {
  1671. struct b53_device *priv = ds->priv;
  1672. int ret;
  1673. mutex_lock(&priv->arl_mutex);
  1674. ret = b53_arl_op(priv, 0, port, mdb->addr, mdb->vid, false);
  1675. mutex_unlock(&priv->arl_mutex);
  1676. if (ret)
  1677. dev_err(ds->dev, "failed to delete MDB entry\n");
  1678. return ret;
  1679. }
  1680. EXPORT_SYMBOL(b53_mdb_del);
  1681. int b53_br_join(struct dsa_switch *ds, int port, struct dsa_bridge bridge,
  1682. bool *tx_fwd_offload, struct netlink_ext_ack *extack)
  1683. {
  1684. struct b53_device *dev = ds->priv;
  1685. struct b53_vlan *vl;
  1686. s8 cpu_port = dsa_to_port(ds, port)->cpu_dp->index;
  1687. u16 pvlan, reg, pvid;
  1688. unsigned int i;
  1689. /* On 7278, port 7 which connects to the ASP should only receive
  1690. * traffic from matching CFP rules.
  1691. */
  1692. if (dev->chip_id == BCM7278_DEVICE_ID && port == 7)
  1693. return -EINVAL;
  1694. pvid = b53_default_pvid(dev);
  1695. vl = &dev->vlans[pvid];
  1696. if (dev->vlan_filtering) {
  1697. /* Make this port leave the all VLANs join since we will have
  1698. * proper VLAN entries from now on
  1699. */
  1700. if (is58xx(dev)) {
  1701. b53_read16(dev, B53_VLAN_PAGE, B53_JOIN_ALL_VLAN_EN,
  1702. &reg);
  1703. reg &= ~BIT(port);
  1704. if ((reg & BIT(cpu_port)) == BIT(cpu_port))
  1705. reg &= ~BIT(cpu_port);
  1706. b53_write16(dev, B53_VLAN_PAGE, B53_JOIN_ALL_VLAN_EN,
  1707. reg);
  1708. }
  1709. b53_get_vlan_entry(dev, pvid, vl);
  1710. vl->members &= ~BIT(port);
  1711. b53_set_vlan_entry(dev, pvid, vl);
  1712. }
  1713. b53_read16(dev, B53_PVLAN_PAGE, B53_PVLAN_PORT_MASK(port), &pvlan);
  1714. b53_for_each_port(dev, i) {
  1715. if (!dsa_port_offloads_bridge(dsa_to_port(ds, i), &bridge))
  1716. continue;
  1717. /* Add this local port to the remote port VLAN control
  1718. * membership and update the remote port bitmask
  1719. */
  1720. b53_read16(dev, B53_PVLAN_PAGE, B53_PVLAN_PORT_MASK(i), &reg);
  1721. reg |= BIT(port);
  1722. b53_write16(dev, B53_PVLAN_PAGE, B53_PVLAN_PORT_MASK(i), reg);
  1723. dev->ports[i].vlan_ctl_mask = reg;
  1724. pvlan |= BIT(i);
  1725. }
  1726. /* Disable redirection of unknown SA to the CPU port */
  1727. b53_set_eap_mode(dev, port, EAP_MODE_BASIC);
  1728. /* Configure the local port VLAN control membership to include
  1729. * remote ports and update the local port bitmask
  1730. */
  1731. b53_write16(dev, B53_PVLAN_PAGE, B53_PVLAN_PORT_MASK(port), pvlan);
  1732. dev->ports[port].vlan_ctl_mask = pvlan;
  1733. return 0;
  1734. }
  1735. EXPORT_SYMBOL(b53_br_join);
  1736. void b53_br_leave(struct dsa_switch *ds, int port, struct dsa_bridge bridge)
  1737. {
  1738. struct b53_device *dev = ds->priv;
  1739. struct b53_vlan *vl;
  1740. s8 cpu_port = dsa_to_port(ds, port)->cpu_dp->index;
  1741. unsigned int i;
  1742. u16 pvlan, reg, pvid;
  1743. b53_read16(dev, B53_PVLAN_PAGE, B53_PVLAN_PORT_MASK(port), &pvlan);
  1744. b53_for_each_port(dev, i) {
  1745. /* Don't touch the remaining ports */
  1746. if (!dsa_port_offloads_bridge(dsa_to_port(ds, i), &bridge))
  1747. continue;
  1748. b53_read16(dev, B53_PVLAN_PAGE, B53_PVLAN_PORT_MASK(i), &reg);
  1749. reg &= ~BIT(port);
  1750. b53_write16(dev, B53_PVLAN_PAGE, B53_PVLAN_PORT_MASK(i), reg);
  1751. dev->ports[port].vlan_ctl_mask = reg;
  1752. /* Prevent self removal to preserve isolation */
  1753. if (port != i)
  1754. pvlan &= ~BIT(i);
  1755. }
  1756. /* Enable redirection of unknown SA to the CPU port */
  1757. b53_set_eap_mode(dev, port, EAP_MODE_SIMPLIFIED);
  1758. b53_write16(dev, B53_PVLAN_PAGE, B53_PVLAN_PORT_MASK(port), pvlan);
  1759. dev->ports[port].vlan_ctl_mask = pvlan;
  1760. pvid = b53_default_pvid(dev);
  1761. vl = &dev->vlans[pvid];
  1762. if (dev->vlan_filtering) {
  1763. /* Make this port join all VLANs without VLAN entries */
  1764. if (is58xx(dev)) {
  1765. b53_read16(dev, B53_VLAN_PAGE, B53_JOIN_ALL_VLAN_EN, &reg);
  1766. reg |= BIT(port);
  1767. if (!(reg & BIT(cpu_port)))
  1768. reg |= BIT(cpu_port);
  1769. b53_write16(dev, B53_VLAN_PAGE, B53_JOIN_ALL_VLAN_EN, reg);
  1770. }
  1771. b53_get_vlan_entry(dev, pvid, vl);
  1772. vl->members |= BIT(port);
  1773. b53_set_vlan_entry(dev, pvid, vl);
  1774. }
  1775. }
  1776. EXPORT_SYMBOL(b53_br_leave);
  1777. void b53_br_set_stp_state(struct dsa_switch *ds, int port, u8 state)
  1778. {
  1779. struct b53_device *dev = ds->priv;
  1780. u8 hw_state;
  1781. u8 reg;
  1782. switch (state) {
  1783. case BR_STATE_DISABLED:
  1784. hw_state = PORT_CTRL_DIS_STATE;
  1785. break;
  1786. case BR_STATE_LISTENING:
  1787. hw_state = PORT_CTRL_LISTEN_STATE;
  1788. break;
  1789. case BR_STATE_LEARNING:
  1790. hw_state = PORT_CTRL_LEARN_STATE;
  1791. break;
  1792. case BR_STATE_FORWARDING:
  1793. hw_state = PORT_CTRL_FWD_STATE;
  1794. break;
  1795. case BR_STATE_BLOCKING:
  1796. hw_state = PORT_CTRL_BLOCK_STATE;
  1797. break;
  1798. default:
  1799. dev_err(ds->dev, "invalid STP state: %d\n", state);
  1800. return;
  1801. }
  1802. b53_read8(dev, B53_CTRL_PAGE, B53_PORT_CTRL(port), &reg);
  1803. reg &= ~PORT_CTRL_STP_STATE_MASK;
  1804. reg |= hw_state;
  1805. b53_write8(dev, B53_CTRL_PAGE, B53_PORT_CTRL(port), reg);
  1806. }
  1807. EXPORT_SYMBOL(b53_br_set_stp_state);
  1808. void b53_br_fast_age(struct dsa_switch *ds, int port)
  1809. {
  1810. struct b53_device *dev = ds->priv;
  1811. if (b53_fast_age_port(dev, port))
  1812. dev_err(ds->dev, "fast ageing failed\n");
  1813. }
  1814. EXPORT_SYMBOL(b53_br_fast_age);
  1815. int b53_br_flags_pre(struct dsa_switch *ds, int port,
  1816. struct switchdev_brport_flags flags,
  1817. struct netlink_ext_ack *extack)
  1818. {
  1819. struct b53_device *dev = ds->priv;
  1820. unsigned long mask = (BR_FLOOD | BR_MCAST_FLOOD);
  1821. if (!is5325(dev))
  1822. mask |= BR_LEARNING;
  1823. if (flags.mask & ~mask)
  1824. return -EINVAL;
  1825. return 0;
  1826. }
  1827. EXPORT_SYMBOL(b53_br_flags_pre);
  1828. int b53_br_flags(struct dsa_switch *ds, int port,
  1829. struct switchdev_brport_flags flags,
  1830. struct netlink_ext_ack *extack)
  1831. {
  1832. if (flags.mask & BR_FLOOD)
  1833. b53_port_set_ucast_flood(ds->priv, port,
  1834. !!(flags.val & BR_FLOOD));
  1835. if (flags.mask & BR_MCAST_FLOOD)
  1836. b53_port_set_mcast_flood(ds->priv, port,
  1837. !!(flags.val & BR_MCAST_FLOOD));
  1838. if (flags.mask & BR_LEARNING)
  1839. b53_port_set_learning(ds->priv, port,
  1840. !!(flags.val & BR_LEARNING));
  1841. return 0;
  1842. }
  1843. EXPORT_SYMBOL(b53_br_flags);
  1844. static bool b53_possible_cpu_port(struct dsa_switch *ds, int port)
  1845. {
  1846. /* Broadcom switches will accept enabling Broadcom tags on the
  1847. * following ports: 5, 7 and 8, any other port is not supported
  1848. */
  1849. switch (port) {
  1850. case B53_CPU_PORT_25:
  1851. case 7:
  1852. case B53_CPU_PORT:
  1853. return true;
  1854. }
  1855. return false;
  1856. }
  1857. static bool b53_can_enable_brcm_tags(struct dsa_switch *ds, int port,
  1858. enum dsa_tag_protocol tag_protocol)
  1859. {
  1860. bool ret = b53_possible_cpu_port(ds, port);
  1861. if (!ret) {
  1862. dev_warn(ds->dev, "Port %d is not Broadcom tag capable\n",
  1863. port);
  1864. return ret;
  1865. }
  1866. switch (tag_protocol) {
  1867. case DSA_TAG_PROTO_BRCM:
  1868. case DSA_TAG_PROTO_BRCM_PREPEND:
  1869. dev_warn(ds->dev,
  1870. "Port %d is stacked to Broadcom tag switch\n", port);
  1871. ret = false;
  1872. break;
  1873. default:
  1874. ret = true;
  1875. break;
  1876. }
  1877. return ret;
  1878. }
  1879. enum dsa_tag_protocol b53_get_tag_protocol(struct dsa_switch *ds, int port,
  1880. enum dsa_tag_protocol mprot)
  1881. {
  1882. struct b53_device *dev = ds->priv;
  1883. if (!b53_can_enable_brcm_tags(ds, port, mprot)) {
  1884. dev->tag_protocol = DSA_TAG_PROTO_NONE;
  1885. goto out;
  1886. }
  1887. /* Older models require a different 6 byte tag */
  1888. if (is5325(dev) || is5365(dev) || is63xx(dev)) {
  1889. dev->tag_protocol = DSA_TAG_PROTO_BRCM_LEGACY;
  1890. goto out;
  1891. }
  1892. /* Broadcom BCM58xx chips have a flow accelerator on Port 8
  1893. * which requires us to use the prepended Broadcom tag type
  1894. */
  1895. if (dev->chip_id == BCM58XX_DEVICE_ID && port == B53_CPU_PORT) {
  1896. dev->tag_protocol = DSA_TAG_PROTO_BRCM_PREPEND;
  1897. goto out;
  1898. }
  1899. dev->tag_protocol = DSA_TAG_PROTO_BRCM;
  1900. out:
  1901. return dev->tag_protocol;
  1902. }
  1903. EXPORT_SYMBOL(b53_get_tag_protocol);
  1904. int b53_mirror_add(struct dsa_switch *ds, int port,
  1905. struct dsa_mall_mirror_tc_entry *mirror, bool ingress,
  1906. struct netlink_ext_ack *extack)
  1907. {
  1908. struct b53_device *dev = ds->priv;
  1909. u16 reg, loc;
  1910. if (ingress)
  1911. loc = B53_IG_MIR_CTL;
  1912. else
  1913. loc = B53_EG_MIR_CTL;
  1914. b53_read16(dev, B53_MGMT_PAGE, loc, &reg);
  1915. reg |= BIT(port);
  1916. b53_write16(dev, B53_MGMT_PAGE, loc, reg);
  1917. b53_read16(dev, B53_MGMT_PAGE, B53_MIR_CAP_CTL, &reg);
  1918. reg &= ~CAP_PORT_MASK;
  1919. reg |= mirror->to_local_port;
  1920. reg |= MIRROR_EN;
  1921. b53_write16(dev, B53_MGMT_PAGE, B53_MIR_CAP_CTL, reg);
  1922. return 0;
  1923. }
  1924. EXPORT_SYMBOL(b53_mirror_add);
  1925. void b53_mirror_del(struct dsa_switch *ds, int port,
  1926. struct dsa_mall_mirror_tc_entry *mirror)
  1927. {
  1928. struct b53_device *dev = ds->priv;
  1929. bool loc_disable = false, other_loc_disable = false;
  1930. u16 reg, loc;
  1931. if (mirror->ingress)
  1932. loc = B53_IG_MIR_CTL;
  1933. else
  1934. loc = B53_EG_MIR_CTL;
  1935. /* Update the desired ingress/egress register */
  1936. b53_read16(dev, B53_MGMT_PAGE, loc, &reg);
  1937. reg &= ~BIT(port);
  1938. if (!(reg & MIRROR_MASK))
  1939. loc_disable = true;
  1940. b53_write16(dev, B53_MGMT_PAGE, loc, reg);
  1941. /* Now look at the other one to know if we can disable mirroring
  1942. * entirely
  1943. */
  1944. if (mirror->ingress)
  1945. b53_read16(dev, B53_MGMT_PAGE, B53_EG_MIR_CTL, &reg);
  1946. else
  1947. b53_read16(dev, B53_MGMT_PAGE, B53_IG_MIR_CTL, &reg);
  1948. if (!(reg & MIRROR_MASK))
  1949. other_loc_disable = true;
  1950. b53_read16(dev, B53_MGMT_PAGE, B53_MIR_CAP_CTL, &reg);
  1951. /* Both no longer have ports, let's disable mirroring */
  1952. if (loc_disable && other_loc_disable) {
  1953. reg &= ~MIRROR_EN;
  1954. reg &= ~mirror->to_local_port;
  1955. }
  1956. b53_write16(dev, B53_MGMT_PAGE, B53_MIR_CAP_CTL, reg);
  1957. }
  1958. EXPORT_SYMBOL(b53_mirror_del);
  1959. /* Returns 0 if EEE was not enabled, or 1 otherwise
  1960. */
  1961. int b53_eee_init(struct dsa_switch *ds, int port, struct phy_device *phy)
  1962. {
  1963. int ret;
  1964. if (!b53_support_eee(ds, port))
  1965. return 0;
  1966. ret = phy_init_eee(phy, false);
  1967. if (ret)
  1968. return 0;
  1969. b53_eee_enable_set(ds, port, true);
  1970. return 1;
  1971. }
  1972. EXPORT_SYMBOL(b53_eee_init);
  1973. bool b53_support_eee(struct dsa_switch *ds, int port)
  1974. {
  1975. struct b53_device *dev = ds->priv;
  1976. return !is5325(dev) && !is5365(dev) && !is63xx(dev);
  1977. }
  1978. EXPORT_SYMBOL(b53_support_eee);
  1979. int b53_get_mac_eee(struct dsa_switch *ds, int port, struct ethtool_keee *e)
  1980. {
  1981. return 0;
  1982. }
  1983. EXPORT_SYMBOL(b53_get_mac_eee);
  1984. int b53_set_mac_eee(struct dsa_switch *ds, int port, struct ethtool_keee *e)
  1985. {
  1986. struct b53_device *dev = ds->priv;
  1987. struct ethtool_keee *p = &dev->ports[port].eee;
  1988. p->eee_enabled = e->eee_enabled;
  1989. b53_eee_enable_set(ds, port, e->eee_enabled);
  1990. return 0;
  1991. }
  1992. EXPORT_SYMBOL(b53_set_mac_eee);
  1993. static int b53_change_mtu(struct dsa_switch *ds, int port, int mtu)
  1994. {
  1995. struct b53_device *dev = ds->priv;
  1996. bool enable_jumbo;
  1997. bool allow_10_100;
  1998. if (is5325(dev) || is5365(dev))
  1999. return 0;
  2000. if (!dsa_is_cpu_port(ds, port))
  2001. return 0;
  2002. enable_jumbo = (mtu > ETH_DATA_LEN);
  2003. allow_10_100 = !is63xx(dev);
  2004. return b53_set_jumbo(dev, enable_jumbo, allow_10_100);
  2005. }
  2006. static int b53_get_max_mtu(struct dsa_switch *ds, int port)
  2007. {
  2008. struct b53_device *dev = ds->priv;
  2009. if (is5325(dev) || is5365(dev))
  2010. return B53_MAX_MTU_25;
  2011. return B53_MAX_MTU;
  2012. }
  2013. static const struct phylink_mac_ops b53_phylink_mac_ops = {
  2014. .mac_select_pcs = b53_phylink_mac_select_pcs,
  2015. .mac_config = b53_phylink_mac_config,
  2016. .mac_link_down = b53_phylink_mac_link_down,
  2017. .mac_link_up = b53_phylink_mac_link_up,
  2018. };
  2019. static const struct dsa_switch_ops b53_switch_ops = {
  2020. .get_tag_protocol = b53_get_tag_protocol,
  2021. .setup = b53_setup,
  2022. .teardown = b53_teardown,
  2023. .get_strings = b53_get_strings,
  2024. .get_ethtool_stats = b53_get_ethtool_stats,
  2025. .get_sset_count = b53_get_sset_count,
  2026. .get_ethtool_phy_stats = b53_get_ethtool_phy_stats,
  2027. .phy_read = b53_phy_read16,
  2028. .phy_write = b53_phy_write16,
  2029. .phylink_get_caps = b53_phylink_get_caps,
  2030. .port_setup = b53_setup_port,
  2031. .port_enable = b53_enable_port,
  2032. .port_disable = b53_disable_port,
  2033. .support_eee = b53_support_eee,
  2034. .get_mac_eee = b53_get_mac_eee,
  2035. .set_mac_eee = b53_set_mac_eee,
  2036. .port_bridge_join = b53_br_join,
  2037. .port_bridge_leave = b53_br_leave,
  2038. .port_pre_bridge_flags = b53_br_flags_pre,
  2039. .port_bridge_flags = b53_br_flags,
  2040. .port_stp_state_set = b53_br_set_stp_state,
  2041. .port_fast_age = b53_br_fast_age,
  2042. .port_vlan_filtering = b53_vlan_filtering,
  2043. .port_vlan_add = b53_vlan_add,
  2044. .port_vlan_del = b53_vlan_del,
  2045. .port_fdb_dump = b53_fdb_dump,
  2046. .port_fdb_add = b53_fdb_add,
  2047. .port_fdb_del = b53_fdb_del,
  2048. .port_mirror_add = b53_mirror_add,
  2049. .port_mirror_del = b53_mirror_del,
  2050. .port_mdb_add = b53_mdb_add,
  2051. .port_mdb_del = b53_mdb_del,
  2052. .port_max_mtu = b53_get_max_mtu,
  2053. .port_change_mtu = b53_change_mtu,
  2054. };
  2055. struct b53_chip_data {
  2056. u32 chip_id;
  2057. const char *dev_name;
  2058. u16 vlans;
  2059. u16 enabled_ports;
  2060. u8 imp_port;
  2061. u8 cpu_port;
  2062. u8 vta_regs[3];
  2063. u8 arl_bins;
  2064. u16 arl_buckets;
  2065. u8 duplex_reg;
  2066. u8 jumbo_pm_reg;
  2067. u8 jumbo_size_reg;
  2068. };
  2069. #define B53_VTA_REGS \
  2070. { B53_VT_ACCESS, B53_VT_INDEX, B53_VT_ENTRY }
  2071. #define B53_VTA_REGS_9798 \
  2072. { B53_VT_ACCESS_9798, B53_VT_INDEX_9798, B53_VT_ENTRY_9798 }
  2073. #define B53_VTA_REGS_63XX \
  2074. { B53_VT_ACCESS_63XX, B53_VT_INDEX_63XX, B53_VT_ENTRY_63XX }
  2075. static const struct b53_chip_data b53_switch_chips[] = {
  2076. {
  2077. .chip_id = BCM5325_DEVICE_ID,
  2078. .dev_name = "BCM5325",
  2079. .vlans = 16,
  2080. .enabled_ports = 0x3f,
  2081. .arl_bins = 2,
  2082. .arl_buckets = 1024,
  2083. .imp_port = 5,
  2084. .duplex_reg = B53_DUPLEX_STAT_FE,
  2085. },
  2086. {
  2087. .chip_id = BCM5365_DEVICE_ID,
  2088. .dev_name = "BCM5365",
  2089. .vlans = 256,
  2090. .enabled_ports = 0x3f,
  2091. .arl_bins = 2,
  2092. .arl_buckets = 1024,
  2093. .imp_port = 5,
  2094. .duplex_reg = B53_DUPLEX_STAT_FE,
  2095. },
  2096. {
  2097. .chip_id = BCM5389_DEVICE_ID,
  2098. .dev_name = "BCM5389",
  2099. .vlans = 4096,
  2100. .enabled_ports = 0x11f,
  2101. .arl_bins = 4,
  2102. .arl_buckets = 1024,
  2103. .imp_port = 8,
  2104. .vta_regs = B53_VTA_REGS,
  2105. .duplex_reg = B53_DUPLEX_STAT_GE,
  2106. .jumbo_pm_reg = B53_JUMBO_PORT_MASK,
  2107. .jumbo_size_reg = B53_JUMBO_MAX_SIZE,
  2108. },
  2109. {
  2110. .chip_id = BCM5395_DEVICE_ID,
  2111. .dev_name = "BCM5395",
  2112. .vlans = 4096,
  2113. .enabled_ports = 0x11f,
  2114. .arl_bins = 4,
  2115. .arl_buckets = 1024,
  2116. .imp_port = 8,
  2117. .vta_regs = B53_VTA_REGS,
  2118. .duplex_reg = B53_DUPLEX_STAT_GE,
  2119. .jumbo_pm_reg = B53_JUMBO_PORT_MASK,
  2120. .jumbo_size_reg = B53_JUMBO_MAX_SIZE,
  2121. },
  2122. {
  2123. .chip_id = BCM5397_DEVICE_ID,
  2124. .dev_name = "BCM5397",
  2125. .vlans = 4096,
  2126. .enabled_ports = 0x11f,
  2127. .arl_bins = 4,
  2128. .arl_buckets = 1024,
  2129. .imp_port = 8,
  2130. .vta_regs = B53_VTA_REGS_9798,
  2131. .duplex_reg = B53_DUPLEX_STAT_GE,
  2132. .jumbo_pm_reg = B53_JUMBO_PORT_MASK,
  2133. .jumbo_size_reg = B53_JUMBO_MAX_SIZE,
  2134. },
  2135. {
  2136. .chip_id = BCM5398_DEVICE_ID,
  2137. .dev_name = "BCM5398",
  2138. .vlans = 4096,
  2139. .enabled_ports = 0x17f,
  2140. .arl_bins = 4,
  2141. .arl_buckets = 1024,
  2142. .imp_port = 8,
  2143. .vta_regs = B53_VTA_REGS_9798,
  2144. .duplex_reg = B53_DUPLEX_STAT_GE,
  2145. .jumbo_pm_reg = B53_JUMBO_PORT_MASK,
  2146. .jumbo_size_reg = B53_JUMBO_MAX_SIZE,
  2147. },
  2148. {
  2149. .chip_id = BCM53115_DEVICE_ID,
  2150. .dev_name = "BCM53115",
  2151. .vlans = 4096,
  2152. .enabled_ports = 0x11f,
  2153. .arl_bins = 4,
  2154. .arl_buckets = 1024,
  2155. .vta_regs = B53_VTA_REGS,
  2156. .imp_port = 8,
  2157. .duplex_reg = B53_DUPLEX_STAT_GE,
  2158. .jumbo_pm_reg = B53_JUMBO_PORT_MASK,
  2159. .jumbo_size_reg = B53_JUMBO_MAX_SIZE,
  2160. },
  2161. {
  2162. .chip_id = BCM53125_DEVICE_ID,
  2163. .dev_name = "BCM53125",
  2164. .vlans = 4096,
  2165. .enabled_ports = 0x1ff,
  2166. .arl_bins = 4,
  2167. .arl_buckets = 1024,
  2168. .imp_port = 8,
  2169. .vta_regs = B53_VTA_REGS,
  2170. .duplex_reg = B53_DUPLEX_STAT_GE,
  2171. .jumbo_pm_reg = B53_JUMBO_PORT_MASK,
  2172. .jumbo_size_reg = B53_JUMBO_MAX_SIZE,
  2173. },
  2174. {
  2175. .chip_id = BCM53128_DEVICE_ID,
  2176. .dev_name = "BCM53128",
  2177. .vlans = 4096,
  2178. .enabled_ports = 0x1ff,
  2179. .arl_bins = 4,
  2180. .arl_buckets = 1024,
  2181. .imp_port = 8,
  2182. .vta_regs = B53_VTA_REGS,
  2183. .duplex_reg = B53_DUPLEX_STAT_GE,
  2184. .jumbo_pm_reg = B53_JUMBO_PORT_MASK,
  2185. .jumbo_size_reg = B53_JUMBO_MAX_SIZE,
  2186. },
  2187. {
  2188. .chip_id = BCM63XX_DEVICE_ID,
  2189. .dev_name = "BCM63xx",
  2190. .vlans = 4096,
  2191. .enabled_ports = 0, /* pdata must provide them */
  2192. .arl_bins = 4,
  2193. .arl_buckets = 1024,
  2194. .imp_port = 8,
  2195. .vta_regs = B53_VTA_REGS_63XX,
  2196. .duplex_reg = B53_DUPLEX_STAT_63XX,
  2197. .jumbo_pm_reg = B53_JUMBO_PORT_MASK_63XX,
  2198. .jumbo_size_reg = B53_JUMBO_MAX_SIZE_63XX,
  2199. },
  2200. {
  2201. .chip_id = BCM63268_DEVICE_ID,
  2202. .dev_name = "BCM63268",
  2203. .vlans = 4096,
  2204. .enabled_ports = 0, /* pdata must provide them */
  2205. .arl_bins = 4,
  2206. .arl_buckets = 1024,
  2207. .imp_port = 8,
  2208. .vta_regs = B53_VTA_REGS_63XX,
  2209. .duplex_reg = B53_DUPLEX_STAT_63XX,
  2210. .jumbo_pm_reg = B53_JUMBO_PORT_MASK_63XX,
  2211. .jumbo_size_reg = B53_JUMBO_MAX_SIZE_63XX,
  2212. },
  2213. {
  2214. .chip_id = BCM53010_DEVICE_ID,
  2215. .dev_name = "BCM53010",
  2216. .vlans = 4096,
  2217. .enabled_ports = 0x1bf,
  2218. .arl_bins = 4,
  2219. .arl_buckets = 1024,
  2220. .imp_port = 8,
  2221. .vta_regs = B53_VTA_REGS,
  2222. .duplex_reg = B53_DUPLEX_STAT_GE,
  2223. .jumbo_pm_reg = B53_JUMBO_PORT_MASK,
  2224. .jumbo_size_reg = B53_JUMBO_MAX_SIZE,
  2225. },
  2226. {
  2227. .chip_id = BCM53011_DEVICE_ID,
  2228. .dev_name = "BCM53011",
  2229. .vlans = 4096,
  2230. .enabled_ports = 0x1bf,
  2231. .arl_bins = 4,
  2232. .arl_buckets = 1024,
  2233. .imp_port = 8,
  2234. .vta_regs = B53_VTA_REGS,
  2235. .duplex_reg = B53_DUPLEX_STAT_GE,
  2236. .jumbo_pm_reg = B53_JUMBO_PORT_MASK,
  2237. .jumbo_size_reg = B53_JUMBO_MAX_SIZE,
  2238. },
  2239. {
  2240. .chip_id = BCM53012_DEVICE_ID,
  2241. .dev_name = "BCM53012",
  2242. .vlans = 4096,
  2243. .enabled_ports = 0x1bf,
  2244. .arl_bins = 4,
  2245. .arl_buckets = 1024,
  2246. .imp_port = 8,
  2247. .vta_regs = B53_VTA_REGS,
  2248. .duplex_reg = B53_DUPLEX_STAT_GE,
  2249. .jumbo_pm_reg = B53_JUMBO_PORT_MASK,
  2250. .jumbo_size_reg = B53_JUMBO_MAX_SIZE,
  2251. },
  2252. {
  2253. .chip_id = BCM53018_DEVICE_ID,
  2254. .dev_name = "BCM53018",
  2255. .vlans = 4096,
  2256. .enabled_ports = 0x1bf,
  2257. .arl_bins = 4,
  2258. .arl_buckets = 1024,
  2259. .imp_port = 8,
  2260. .vta_regs = B53_VTA_REGS,
  2261. .duplex_reg = B53_DUPLEX_STAT_GE,
  2262. .jumbo_pm_reg = B53_JUMBO_PORT_MASK,
  2263. .jumbo_size_reg = B53_JUMBO_MAX_SIZE,
  2264. },
  2265. {
  2266. .chip_id = BCM53019_DEVICE_ID,
  2267. .dev_name = "BCM53019",
  2268. .vlans = 4096,
  2269. .enabled_ports = 0x1bf,
  2270. .arl_bins = 4,
  2271. .arl_buckets = 1024,
  2272. .imp_port = 8,
  2273. .vta_regs = B53_VTA_REGS,
  2274. .duplex_reg = B53_DUPLEX_STAT_GE,
  2275. .jumbo_pm_reg = B53_JUMBO_PORT_MASK,
  2276. .jumbo_size_reg = B53_JUMBO_MAX_SIZE,
  2277. },
  2278. {
  2279. .chip_id = BCM58XX_DEVICE_ID,
  2280. .dev_name = "BCM585xx/586xx/88312",
  2281. .vlans = 4096,
  2282. .enabled_ports = 0x1ff,
  2283. .arl_bins = 4,
  2284. .arl_buckets = 1024,
  2285. .imp_port = 8,
  2286. .vta_regs = B53_VTA_REGS,
  2287. .duplex_reg = B53_DUPLEX_STAT_GE,
  2288. .jumbo_pm_reg = B53_JUMBO_PORT_MASK,
  2289. .jumbo_size_reg = B53_JUMBO_MAX_SIZE,
  2290. },
  2291. {
  2292. .chip_id = BCM583XX_DEVICE_ID,
  2293. .dev_name = "BCM583xx/11360",
  2294. .vlans = 4096,
  2295. .enabled_ports = 0x103,
  2296. .arl_bins = 4,
  2297. .arl_buckets = 1024,
  2298. .imp_port = 8,
  2299. .vta_regs = B53_VTA_REGS,
  2300. .duplex_reg = B53_DUPLEX_STAT_GE,
  2301. .jumbo_pm_reg = B53_JUMBO_PORT_MASK,
  2302. .jumbo_size_reg = B53_JUMBO_MAX_SIZE,
  2303. },
  2304. /* Starfighter 2 */
  2305. {
  2306. .chip_id = BCM4908_DEVICE_ID,
  2307. .dev_name = "BCM4908",
  2308. .vlans = 4096,
  2309. .enabled_ports = 0x1bf,
  2310. .arl_bins = 4,
  2311. .arl_buckets = 256,
  2312. .imp_port = 8,
  2313. .vta_regs = B53_VTA_REGS,
  2314. .duplex_reg = B53_DUPLEX_STAT_GE,
  2315. .jumbo_pm_reg = B53_JUMBO_PORT_MASK,
  2316. .jumbo_size_reg = B53_JUMBO_MAX_SIZE,
  2317. },
  2318. {
  2319. .chip_id = BCM7445_DEVICE_ID,
  2320. .dev_name = "BCM7445",
  2321. .vlans = 4096,
  2322. .enabled_ports = 0x1ff,
  2323. .arl_bins = 4,
  2324. .arl_buckets = 1024,
  2325. .imp_port = 8,
  2326. .vta_regs = B53_VTA_REGS,
  2327. .duplex_reg = B53_DUPLEX_STAT_GE,
  2328. .jumbo_pm_reg = B53_JUMBO_PORT_MASK,
  2329. .jumbo_size_reg = B53_JUMBO_MAX_SIZE,
  2330. },
  2331. {
  2332. .chip_id = BCM7278_DEVICE_ID,
  2333. .dev_name = "BCM7278",
  2334. .vlans = 4096,
  2335. .enabled_ports = 0x1ff,
  2336. .arl_bins = 4,
  2337. .arl_buckets = 256,
  2338. .imp_port = 8,
  2339. .vta_regs = B53_VTA_REGS,
  2340. .duplex_reg = B53_DUPLEX_STAT_GE,
  2341. .jumbo_pm_reg = B53_JUMBO_PORT_MASK,
  2342. .jumbo_size_reg = B53_JUMBO_MAX_SIZE,
  2343. },
  2344. {
  2345. .chip_id = BCM53134_DEVICE_ID,
  2346. .dev_name = "BCM53134",
  2347. .vlans = 4096,
  2348. .enabled_ports = 0x12f,
  2349. .imp_port = 8,
  2350. .cpu_port = B53_CPU_PORT,
  2351. .vta_regs = B53_VTA_REGS,
  2352. .arl_bins = 4,
  2353. .arl_buckets = 1024,
  2354. .duplex_reg = B53_DUPLEX_STAT_GE,
  2355. .jumbo_pm_reg = B53_JUMBO_PORT_MASK,
  2356. .jumbo_size_reg = B53_JUMBO_MAX_SIZE,
  2357. },
  2358. };
  2359. static int b53_switch_init(struct b53_device *dev)
  2360. {
  2361. unsigned int i;
  2362. int ret;
  2363. for (i = 0; i < ARRAY_SIZE(b53_switch_chips); i++) {
  2364. const struct b53_chip_data *chip = &b53_switch_chips[i];
  2365. if (chip->chip_id == dev->chip_id) {
  2366. if (!dev->enabled_ports)
  2367. dev->enabled_ports = chip->enabled_ports;
  2368. dev->name = chip->dev_name;
  2369. dev->duplex_reg = chip->duplex_reg;
  2370. dev->vta_regs[0] = chip->vta_regs[0];
  2371. dev->vta_regs[1] = chip->vta_regs[1];
  2372. dev->vta_regs[2] = chip->vta_regs[2];
  2373. dev->jumbo_pm_reg = chip->jumbo_pm_reg;
  2374. dev->imp_port = chip->imp_port;
  2375. dev->num_vlans = chip->vlans;
  2376. dev->num_arl_bins = chip->arl_bins;
  2377. dev->num_arl_buckets = chip->arl_buckets;
  2378. break;
  2379. }
  2380. }
  2381. /* check which BCM5325x version we have */
  2382. if (is5325(dev)) {
  2383. u8 vc4;
  2384. b53_read8(dev, B53_VLAN_PAGE, B53_VLAN_CTRL4_25, &vc4);
  2385. /* check reserved bits */
  2386. switch (vc4 & 3) {
  2387. case 1:
  2388. /* BCM5325E */
  2389. break;
  2390. case 3:
  2391. /* BCM5325F - do not use port 4 */
  2392. dev->enabled_ports &= ~BIT(4);
  2393. break;
  2394. default:
  2395. /* On the BCM47XX SoCs this is the supported internal switch.*/
  2396. #ifndef CONFIG_BCM47XX
  2397. /* BCM5325M */
  2398. return -EINVAL;
  2399. #else
  2400. break;
  2401. #endif
  2402. }
  2403. }
  2404. dev->num_ports = fls(dev->enabled_ports);
  2405. dev->ds->num_ports = min_t(unsigned int, dev->num_ports, DSA_MAX_PORTS);
  2406. /* Include non standard CPU port built-in PHYs to be probed */
  2407. if (is539x(dev) || is531x5(dev)) {
  2408. for (i = 0; i < dev->num_ports; i++) {
  2409. if (!(dev->ds->phys_mii_mask & BIT(i)) &&
  2410. !b53_possible_cpu_port(dev->ds, i))
  2411. dev->ds->phys_mii_mask |= BIT(i);
  2412. }
  2413. }
  2414. dev->ports = devm_kcalloc(dev->dev,
  2415. dev->num_ports, sizeof(struct b53_port),
  2416. GFP_KERNEL);
  2417. if (!dev->ports)
  2418. return -ENOMEM;
  2419. dev->vlans = devm_kcalloc(dev->dev,
  2420. dev->num_vlans, sizeof(struct b53_vlan),
  2421. GFP_KERNEL);
  2422. if (!dev->vlans)
  2423. return -ENOMEM;
  2424. dev->reset_gpio = b53_switch_get_reset_gpio(dev);
  2425. if (dev->reset_gpio >= 0) {
  2426. ret = devm_gpio_request_one(dev->dev, dev->reset_gpio,
  2427. GPIOF_OUT_INIT_HIGH, "robo_reset");
  2428. if (ret)
  2429. return ret;
  2430. }
  2431. return 0;
  2432. }
  2433. struct b53_device *b53_switch_alloc(struct device *base,
  2434. const struct b53_io_ops *ops,
  2435. void *priv)
  2436. {
  2437. struct dsa_switch *ds;
  2438. struct b53_device *dev;
  2439. ds = devm_kzalloc(base, sizeof(*ds), GFP_KERNEL);
  2440. if (!ds)
  2441. return NULL;
  2442. ds->dev = base;
  2443. dev = devm_kzalloc(base, sizeof(*dev), GFP_KERNEL);
  2444. if (!dev)
  2445. return NULL;
  2446. ds->priv = dev;
  2447. dev->dev = base;
  2448. dev->ds = ds;
  2449. dev->priv = priv;
  2450. dev->ops = ops;
  2451. ds->ops = &b53_switch_ops;
  2452. ds->phylink_mac_ops = &b53_phylink_mac_ops;
  2453. dev->vlan_enabled = true;
  2454. dev->vlan_filtering = false;
  2455. /* Let DSA handle the case were multiple bridges span the same switch
  2456. * device and different VLAN awareness settings are requested, which
  2457. * would be breaking filtering semantics for any of the other bridge
  2458. * devices. (not hardware supported)
  2459. */
  2460. ds->vlan_filtering_is_global = true;
  2461. mutex_init(&dev->reg_mutex);
  2462. mutex_init(&dev->stats_mutex);
  2463. mutex_init(&dev->arl_mutex);
  2464. return dev;
  2465. }
  2466. EXPORT_SYMBOL(b53_switch_alloc);
  2467. int b53_switch_detect(struct b53_device *dev)
  2468. {
  2469. u32 id32;
  2470. u16 tmp;
  2471. u8 id8;
  2472. int ret;
  2473. ret = b53_read8(dev, B53_MGMT_PAGE, B53_DEVICE_ID, &id8);
  2474. if (ret)
  2475. return ret;
  2476. switch (id8) {
  2477. case 0:
  2478. /* BCM5325 and BCM5365 do not have this register so reads
  2479. * return 0. But the read operation did succeed, so assume this
  2480. * is one of them.
  2481. *
  2482. * Next check if we can write to the 5325's VTA register; for
  2483. * 5365 it is read only.
  2484. */
  2485. b53_write16(dev, B53_VLAN_PAGE, B53_VLAN_TABLE_ACCESS_25, 0xf);
  2486. b53_read16(dev, B53_VLAN_PAGE, B53_VLAN_TABLE_ACCESS_25, &tmp);
  2487. if (tmp == 0xf)
  2488. dev->chip_id = BCM5325_DEVICE_ID;
  2489. else
  2490. dev->chip_id = BCM5365_DEVICE_ID;
  2491. break;
  2492. case BCM5389_DEVICE_ID:
  2493. case BCM5395_DEVICE_ID:
  2494. case BCM5397_DEVICE_ID:
  2495. case BCM5398_DEVICE_ID:
  2496. dev->chip_id = id8;
  2497. break;
  2498. default:
  2499. ret = b53_read32(dev, B53_MGMT_PAGE, B53_DEVICE_ID, &id32);
  2500. if (ret)
  2501. return ret;
  2502. switch (id32) {
  2503. case BCM53115_DEVICE_ID:
  2504. case BCM53125_DEVICE_ID:
  2505. case BCM53128_DEVICE_ID:
  2506. case BCM53010_DEVICE_ID:
  2507. case BCM53011_DEVICE_ID:
  2508. case BCM53012_DEVICE_ID:
  2509. case BCM53018_DEVICE_ID:
  2510. case BCM53019_DEVICE_ID:
  2511. case BCM53134_DEVICE_ID:
  2512. dev->chip_id = id32;
  2513. break;
  2514. default:
  2515. dev_err(dev->dev,
  2516. "unsupported switch detected (BCM53%02x/BCM%x)\n",
  2517. id8, id32);
  2518. return -ENODEV;
  2519. }
  2520. }
  2521. if (dev->chip_id == BCM5325_DEVICE_ID)
  2522. return b53_read8(dev, B53_STAT_PAGE, B53_REV_ID_25,
  2523. &dev->core_rev);
  2524. else
  2525. return b53_read8(dev, B53_MGMT_PAGE, B53_REV_ID,
  2526. &dev->core_rev);
  2527. }
  2528. EXPORT_SYMBOL(b53_switch_detect);
  2529. int b53_switch_register(struct b53_device *dev)
  2530. {
  2531. int ret;
  2532. if (dev->pdata) {
  2533. dev->chip_id = dev->pdata->chip_id;
  2534. dev->enabled_ports = dev->pdata->enabled_ports;
  2535. }
  2536. if (!dev->chip_id && b53_switch_detect(dev))
  2537. return -EINVAL;
  2538. ret = b53_switch_init(dev);
  2539. if (ret)
  2540. return ret;
  2541. dev_info(dev->dev, "found switch: %s, rev %i\n",
  2542. dev->name, dev->core_rev);
  2543. return dsa_register_switch(dev->ds);
  2544. }
  2545. EXPORT_SYMBOL(b53_switch_register);
  2546. MODULE_AUTHOR("Jonas Gorski <jogo@openwrt.org>");
  2547. MODULE_DESCRIPTION("B53 switch library");
  2548. MODULE_LICENSE("Dual BSD/GPL");