b53_common.c 69 KB

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