b53_common.c 82 KB

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