b53_common.c 51 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069
  1. /*
  2. * B53 switch driver main logic
  3. *
  4. * Copyright (C) 2011-2013 Jonas Gorski <jogo@openwrt.org>
  5. * Copyright (C) 2016 Florian Fainelli <f.fainelli@gmail.com>
  6. *
  7. * Permission to use, copy, modify, and/or distribute this software for any
  8. * purpose with or without fee is hereby granted, provided that the above
  9. * copyright notice and this permission notice appear in all copies.
  10. *
  11. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  12. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  13. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  14. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  15. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  16. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  17. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  18. */
  19. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  20. #include <linux/delay.h>
  21. #include <linux/export.h>
  22. #include <linux/gpio.h>
  23. #include <linux/kernel.h>
  24. #include <linux/module.h>
  25. #include <linux/platform_data/b53.h>
  26. #include <linux/phy.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. struct dsa_switch *ds = dev->ds;
  296. u8 mgmt;
  297. b53_read8(dev, B53_CTRL_PAGE, B53_SWITCH_MODE, &mgmt);
  298. if (enable)
  299. mgmt |= SM_SW_FWD_EN;
  300. else
  301. mgmt &= ~SM_SW_FWD_EN;
  302. b53_write8(dev, B53_CTRL_PAGE, B53_SWITCH_MODE, mgmt);
  303. /* Include IMP port in dumb forwarding mode when no tagging protocol is
  304. * set
  305. */
  306. if (ds->ops->get_tag_protocol(ds) == DSA_TAG_PROTO_NONE) {
  307. b53_read8(dev, B53_CTRL_PAGE, B53_SWITCH_CTRL, &mgmt);
  308. mgmt |= B53_MII_DUMB_FWDG_EN;
  309. b53_write8(dev, B53_CTRL_PAGE, B53_SWITCH_CTRL, mgmt);
  310. }
  311. }
  312. static void b53_enable_vlan(struct b53_device *dev, bool enable)
  313. {
  314. u8 mgmt, vc0, vc1, vc4 = 0, vc5;
  315. b53_read8(dev, B53_CTRL_PAGE, B53_SWITCH_MODE, &mgmt);
  316. b53_read8(dev, B53_VLAN_PAGE, B53_VLAN_CTRL0, &vc0);
  317. b53_read8(dev, B53_VLAN_PAGE, B53_VLAN_CTRL1, &vc1);
  318. if (is5325(dev) || is5365(dev)) {
  319. b53_read8(dev, B53_VLAN_PAGE, B53_VLAN_CTRL4_25, &vc4);
  320. b53_read8(dev, B53_VLAN_PAGE, B53_VLAN_CTRL5_25, &vc5);
  321. } else if (is63xx(dev)) {
  322. b53_read8(dev, B53_VLAN_PAGE, B53_VLAN_CTRL4_63XX, &vc4);
  323. b53_read8(dev, B53_VLAN_PAGE, B53_VLAN_CTRL5_63XX, &vc5);
  324. } else {
  325. b53_read8(dev, B53_VLAN_PAGE, B53_VLAN_CTRL4, &vc4);
  326. b53_read8(dev, B53_VLAN_PAGE, B53_VLAN_CTRL5, &vc5);
  327. }
  328. mgmt &= ~SM_SW_FWD_MODE;
  329. if (enable) {
  330. vc0 |= VC0_VLAN_EN | VC0_VID_CHK_EN | VC0_VID_HASH_VID;
  331. vc1 |= VC1_RX_MCST_UNTAG_EN | VC1_RX_MCST_FWD_EN;
  332. vc4 &= ~VC4_ING_VID_CHECK_MASK;
  333. vc4 |= VC4_ING_VID_VIO_DROP << VC4_ING_VID_CHECK_S;
  334. vc5 |= VC5_DROP_VTABLE_MISS;
  335. if (is5325(dev))
  336. vc0 &= ~VC0_RESERVED_1;
  337. if (is5325(dev) || is5365(dev))
  338. vc1 |= VC1_RX_MCST_TAG_EN;
  339. } else {
  340. vc0 &= ~(VC0_VLAN_EN | VC0_VID_CHK_EN | VC0_VID_HASH_VID);
  341. vc1 &= ~(VC1_RX_MCST_UNTAG_EN | VC1_RX_MCST_FWD_EN);
  342. vc4 &= ~VC4_ING_VID_CHECK_MASK;
  343. vc5 &= ~VC5_DROP_VTABLE_MISS;
  344. if (is5325(dev) || is5365(dev))
  345. vc4 |= VC4_ING_VID_VIO_FWD << VC4_ING_VID_CHECK_S;
  346. else
  347. vc4 |= VC4_ING_VID_VIO_TO_IMP << VC4_ING_VID_CHECK_S;
  348. if (is5325(dev) || is5365(dev))
  349. vc1 &= ~VC1_RX_MCST_TAG_EN;
  350. }
  351. if (!is5325(dev) && !is5365(dev))
  352. vc5 &= ~VC5_VID_FFF_EN;
  353. b53_write8(dev, B53_VLAN_PAGE, B53_VLAN_CTRL0, vc0);
  354. b53_write8(dev, B53_VLAN_PAGE, B53_VLAN_CTRL1, vc1);
  355. if (is5325(dev) || is5365(dev)) {
  356. /* enable the high 8 bit vid check on 5325 */
  357. if (is5325(dev) && enable)
  358. b53_write8(dev, B53_VLAN_PAGE, B53_VLAN_CTRL3,
  359. VC3_HIGH_8BIT_EN);
  360. else
  361. b53_write8(dev, B53_VLAN_PAGE, B53_VLAN_CTRL3, 0);
  362. b53_write8(dev, B53_VLAN_PAGE, B53_VLAN_CTRL4_25, vc4);
  363. b53_write8(dev, B53_VLAN_PAGE, B53_VLAN_CTRL5_25, vc5);
  364. } else if (is63xx(dev)) {
  365. b53_write16(dev, B53_VLAN_PAGE, B53_VLAN_CTRL3_63XX, 0);
  366. b53_write8(dev, B53_VLAN_PAGE, B53_VLAN_CTRL4_63XX, vc4);
  367. b53_write8(dev, B53_VLAN_PAGE, B53_VLAN_CTRL5_63XX, vc5);
  368. } else {
  369. b53_write16(dev, B53_VLAN_PAGE, B53_VLAN_CTRL3, 0);
  370. b53_write8(dev, B53_VLAN_PAGE, B53_VLAN_CTRL4, vc4);
  371. b53_write8(dev, B53_VLAN_PAGE, B53_VLAN_CTRL5, vc5);
  372. }
  373. b53_write8(dev, B53_CTRL_PAGE, B53_SWITCH_MODE, mgmt);
  374. }
  375. static int b53_set_jumbo(struct b53_device *dev, bool enable, bool allow_10_100)
  376. {
  377. u32 port_mask = 0;
  378. u16 max_size = JMS_MIN_SIZE;
  379. if (is5325(dev) || is5365(dev))
  380. return -EINVAL;
  381. if (enable) {
  382. port_mask = dev->enabled_ports;
  383. max_size = JMS_MAX_SIZE;
  384. if (allow_10_100)
  385. port_mask |= JPM_10_100_JUMBO_EN;
  386. }
  387. b53_write32(dev, B53_JUMBO_PAGE, dev->jumbo_pm_reg, port_mask);
  388. return b53_write16(dev, B53_JUMBO_PAGE, dev->jumbo_size_reg, max_size);
  389. }
  390. static int b53_flush_arl(struct b53_device *dev, u8 mask)
  391. {
  392. unsigned int i;
  393. b53_write8(dev, B53_CTRL_PAGE, B53_FAST_AGE_CTRL,
  394. FAST_AGE_DONE | FAST_AGE_DYNAMIC | mask);
  395. for (i = 0; i < 10; i++) {
  396. u8 fast_age_ctrl;
  397. b53_read8(dev, B53_CTRL_PAGE, B53_FAST_AGE_CTRL,
  398. &fast_age_ctrl);
  399. if (!(fast_age_ctrl & FAST_AGE_DONE))
  400. goto out;
  401. msleep(1);
  402. }
  403. return -ETIMEDOUT;
  404. out:
  405. /* Only age dynamic entries (default behavior) */
  406. b53_write8(dev, B53_CTRL_PAGE, B53_FAST_AGE_CTRL, FAST_AGE_DYNAMIC);
  407. return 0;
  408. }
  409. static int b53_fast_age_port(struct b53_device *dev, int port)
  410. {
  411. b53_write8(dev, B53_CTRL_PAGE, B53_FAST_AGE_PORT_CTRL, port);
  412. return b53_flush_arl(dev, FAST_AGE_PORT);
  413. }
  414. static int b53_fast_age_vlan(struct b53_device *dev, u16 vid)
  415. {
  416. b53_write16(dev, B53_CTRL_PAGE, B53_FAST_AGE_VID_CTRL, vid);
  417. return b53_flush_arl(dev, FAST_AGE_VLAN);
  418. }
  419. void b53_imp_vlan_setup(struct dsa_switch *ds, int cpu_port)
  420. {
  421. struct b53_device *dev = ds->priv;
  422. unsigned int i;
  423. u16 pvlan;
  424. /* Enable the IMP port to be in the same VLAN as the other ports
  425. * on a per-port basis such that we only have Port i and IMP in
  426. * the same VLAN.
  427. */
  428. b53_for_each_port(dev, i) {
  429. b53_read16(dev, B53_PVLAN_PAGE, B53_PVLAN_PORT_MASK(i), &pvlan);
  430. pvlan |= BIT(cpu_port);
  431. b53_write16(dev, B53_PVLAN_PAGE, B53_PVLAN_PORT_MASK(i), pvlan);
  432. }
  433. }
  434. EXPORT_SYMBOL(b53_imp_vlan_setup);
  435. int b53_enable_port(struct dsa_switch *ds, int port, struct phy_device *phy)
  436. {
  437. struct b53_device *dev = ds->priv;
  438. unsigned int cpu_port = dev->cpu_port;
  439. u16 pvlan;
  440. /* Clear the Rx and Tx disable bits and set to no spanning tree */
  441. b53_write8(dev, B53_CTRL_PAGE, B53_PORT_CTRL(port), 0);
  442. /* Set this port, and only this one to be in the default VLAN,
  443. * if member of a bridge, restore its membership prior to
  444. * bringing down this port.
  445. */
  446. b53_read16(dev, B53_PVLAN_PAGE, B53_PVLAN_PORT_MASK(port), &pvlan);
  447. pvlan &= ~0x1ff;
  448. pvlan |= BIT(port);
  449. pvlan |= dev->ports[port].vlan_ctl_mask;
  450. b53_write16(dev, B53_PVLAN_PAGE, B53_PVLAN_PORT_MASK(port), pvlan);
  451. b53_imp_vlan_setup(ds, cpu_port);
  452. /* If EEE was enabled, restore it */
  453. if (dev->ports[port].eee.eee_enabled)
  454. b53_eee_enable_set(ds, port, true);
  455. return 0;
  456. }
  457. EXPORT_SYMBOL(b53_enable_port);
  458. void b53_disable_port(struct dsa_switch *ds, int port, struct phy_device *phy)
  459. {
  460. struct b53_device *dev = ds->priv;
  461. u8 reg;
  462. /* Disable Tx/Rx for the port */
  463. b53_read8(dev, B53_CTRL_PAGE, B53_PORT_CTRL(port), &reg);
  464. reg |= PORT_CTRL_RX_DISABLE | PORT_CTRL_TX_DISABLE;
  465. b53_write8(dev, B53_CTRL_PAGE, B53_PORT_CTRL(port), reg);
  466. }
  467. EXPORT_SYMBOL(b53_disable_port);
  468. void b53_brcm_hdr_setup(struct dsa_switch *ds, int port)
  469. {
  470. bool tag_en = !!(ds->ops->get_tag_protocol(ds) == DSA_TAG_PROTO_BRCM);
  471. struct b53_device *dev = ds->priv;
  472. u8 hdr_ctl, val;
  473. u16 reg;
  474. /* Resolve which bit controls the Broadcom tag */
  475. switch (port) {
  476. case 8:
  477. val = BRCM_HDR_P8_EN;
  478. break;
  479. case 7:
  480. val = BRCM_HDR_P7_EN;
  481. break;
  482. case 5:
  483. val = BRCM_HDR_P5_EN;
  484. break;
  485. default:
  486. val = 0;
  487. break;
  488. }
  489. /* Enable Broadcom tags for IMP port */
  490. b53_read8(dev, B53_MGMT_PAGE, B53_BRCM_HDR, &hdr_ctl);
  491. if (tag_en)
  492. hdr_ctl |= val;
  493. else
  494. hdr_ctl &= ~val;
  495. b53_write8(dev, B53_MGMT_PAGE, B53_BRCM_HDR, hdr_ctl);
  496. /* Registers below are only accessible on newer devices */
  497. if (!is58xx(dev))
  498. return;
  499. /* Enable reception Broadcom tag for CPU TX (switch RX) to
  500. * allow us to tag outgoing frames
  501. */
  502. b53_read16(dev, B53_MGMT_PAGE, B53_BRCM_HDR_RX_DIS, &reg);
  503. if (tag_en)
  504. reg &= ~BIT(port);
  505. else
  506. reg |= BIT(port);
  507. b53_write16(dev, B53_MGMT_PAGE, B53_BRCM_HDR_RX_DIS, reg);
  508. /* Enable transmission of Broadcom tags from the switch (CPU RX) to
  509. * allow delivering frames to the per-port net_devices
  510. */
  511. b53_read16(dev, B53_MGMT_PAGE, B53_BRCM_HDR_TX_DIS, &reg);
  512. if (tag_en)
  513. reg &= ~BIT(port);
  514. else
  515. reg |= BIT(port);
  516. b53_write16(dev, B53_MGMT_PAGE, B53_BRCM_HDR_TX_DIS, reg);
  517. }
  518. EXPORT_SYMBOL(b53_brcm_hdr_setup);
  519. static void b53_enable_cpu_port(struct b53_device *dev, int port)
  520. {
  521. u8 port_ctrl;
  522. /* BCM5325 CPU port is at 8 */
  523. if ((is5325(dev) || is5365(dev)) && port == B53_CPU_PORT_25)
  524. port = B53_CPU_PORT;
  525. port_ctrl = PORT_CTRL_RX_BCST_EN |
  526. PORT_CTRL_RX_MCST_EN |
  527. PORT_CTRL_RX_UCST_EN;
  528. b53_write8(dev, B53_CTRL_PAGE, B53_PORT_CTRL(port), port_ctrl);
  529. }
  530. static void b53_enable_mib(struct b53_device *dev)
  531. {
  532. u8 gc;
  533. b53_read8(dev, B53_MGMT_PAGE, B53_GLOBAL_CONFIG, &gc);
  534. gc &= ~(GC_RESET_MIB | GC_MIB_AC_EN);
  535. b53_write8(dev, B53_MGMT_PAGE, B53_GLOBAL_CONFIG, gc);
  536. }
  537. int b53_configure_vlan(struct dsa_switch *ds)
  538. {
  539. struct b53_device *dev = ds->priv;
  540. struct b53_vlan vl = { 0 };
  541. int i;
  542. /* clear all vlan entries */
  543. if (is5325(dev) || is5365(dev)) {
  544. for (i = 1; i < dev->num_vlans; i++)
  545. b53_set_vlan_entry(dev, i, &vl);
  546. } else {
  547. b53_do_vlan_op(dev, VTA_CMD_CLEAR);
  548. }
  549. b53_enable_vlan(dev, false);
  550. b53_for_each_port(dev, i)
  551. b53_write16(dev, B53_VLAN_PAGE,
  552. B53_VLAN_PORT_DEF_TAG(i), 1);
  553. if (!is5325(dev) && !is5365(dev))
  554. b53_set_jumbo(dev, dev->enable_jumbo, false);
  555. return 0;
  556. }
  557. EXPORT_SYMBOL(b53_configure_vlan);
  558. static void b53_switch_reset_gpio(struct b53_device *dev)
  559. {
  560. int gpio = dev->reset_gpio;
  561. if (gpio < 0)
  562. return;
  563. /* Reset sequence: RESET low(50ms)->high(20ms)
  564. */
  565. gpio_set_value(gpio, 0);
  566. mdelay(50);
  567. gpio_set_value(gpio, 1);
  568. mdelay(20);
  569. dev->current_page = 0xff;
  570. }
  571. static int b53_switch_reset(struct b53_device *dev)
  572. {
  573. unsigned int timeout = 1000;
  574. u8 mgmt, reg;
  575. b53_switch_reset_gpio(dev);
  576. if (is539x(dev)) {
  577. b53_write8(dev, B53_CTRL_PAGE, B53_SOFTRESET, 0x83);
  578. b53_write8(dev, B53_CTRL_PAGE, B53_SOFTRESET, 0x00);
  579. }
  580. /* This is specific to 58xx devices here, do not use is58xx() which
  581. * covers the larger Starfigther 2 family, including 7445/7278 which
  582. * still use this driver as a library and need to perform the reset
  583. * earlier.
  584. */
  585. if (dev->chip_id == BCM58XX_DEVICE_ID) {
  586. b53_read8(dev, B53_CTRL_PAGE, B53_SOFTRESET, &reg);
  587. reg |= SW_RST | EN_SW_RST | EN_CH_RST;
  588. b53_write8(dev, B53_CTRL_PAGE, B53_SOFTRESET, reg);
  589. do {
  590. b53_read8(dev, B53_CTRL_PAGE, B53_SOFTRESET, &reg);
  591. if (!(reg & SW_RST))
  592. break;
  593. usleep_range(1000, 2000);
  594. } while (timeout-- > 0);
  595. if (timeout == 0)
  596. return -ETIMEDOUT;
  597. }
  598. b53_read8(dev, B53_CTRL_PAGE, B53_SWITCH_MODE, &mgmt);
  599. if (!(mgmt & SM_SW_FWD_EN)) {
  600. mgmt &= ~SM_SW_FWD_MODE;
  601. mgmt |= SM_SW_FWD_EN;
  602. b53_write8(dev, B53_CTRL_PAGE, B53_SWITCH_MODE, mgmt);
  603. b53_read8(dev, B53_CTRL_PAGE, B53_SWITCH_MODE, &mgmt);
  604. if (!(mgmt & SM_SW_FWD_EN)) {
  605. dev_err(dev->dev, "Failed to enable switch!\n");
  606. return -EINVAL;
  607. }
  608. }
  609. b53_enable_mib(dev);
  610. return b53_flush_arl(dev, FAST_AGE_STATIC);
  611. }
  612. static int b53_phy_read16(struct dsa_switch *ds, int addr, int reg)
  613. {
  614. struct b53_device *priv = ds->priv;
  615. u16 value = 0;
  616. int ret;
  617. if (priv->ops->phy_read16)
  618. ret = priv->ops->phy_read16(priv, addr, reg, &value);
  619. else
  620. ret = b53_read16(priv, B53_PORT_MII_PAGE(addr),
  621. reg * 2, &value);
  622. return ret ? ret : value;
  623. }
  624. static int b53_phy_write16(struct dsa_switch *ds, int addr, int reg, u16 val)
  625. {
  626. struct b53_device *priv = ds->priv;
  627. if (priv->ops->phy_write16)
  628. return priv->ops->phy_write16(priv, addr, reg, val);
  629. return b53_write16(priv, B53_PORT_MII_PAGE(addr), reg * 2, val);
  630. }
  631. static int b53_reset_switch(struct b53_device *priv)
  632. {
  633. /* reset vlans */
  634. priv->enable_jumbo = false;
  635. memset(priv->vlans, 0, sizeof(*priv->vlans) * priv->num_vlans);
  636. memset(priv->ports, 0, sizeof(*priv->ports) * priv->num_ports);
  637. return b53_switch_reset(priv);
  638. }
  639. static int b53_apply_config(struct b53_device *priv)
  640. {
  641. /* disable switching */
  642. b53_set_forwarding(priv, 0);
  643. b53_configure_vlan(priv->ds);
  644. /* enable switching */
  645. b53_set_forwarding(priv, 1);
  646. return 0;
  647. }
  648. static void b53_reset_mib(struct b53_device *priv)
  649. {
  650. u8 gc;
  651. b53_read8(priv, B53_MGMT_PAGE, B53_GLOBAL_CONFIG, &gc);
  652. b53_write8(priv, B53_MGMT_PAGE, B53_GLOBAL_CONFIG, gc | GC_RESET_MIB);
  653. msleep(1);
  654. b53_write8(priv, B53_MGMT_PAGE, B53_GLOBAL_CONFIG, gc & ~GC_RESET_MIB);
  655. msleep(1);
  656. }
  657. static const struct b53_mib_desc *b53_get_mib(struct b53_device *dev)
  658. {
  659. if (is5365(dev))
  660. return b53_mibs_65;
  661. else if (is63xx(dev))
  662. return b53_mibs_63xx;
  663. else if (is58xx(dev))
  664. return b53_mibs_58xx;
  665. else
  666. return b53_mibs;
  667. }
  668. static unsigned int b53_get_mib_size(struct b53_device *dev)
  669. {
  670. if (is5365(dev))
  671. return B53_MIBS_65_SIZE;
  672. else if (is63xx(dev))
  673. return B53_MIBS_63XX_SIZE;
  674. else if (is58xx(dev))
  675. return B53_MIBS_58XX_SIZE;
  676. else
  677. return B53_MIBS_SIZE;
  678. }
  679. void b53_get_strings(struct dsa_switch *ds, int port, uint8_t *data)
  680. {
  681. struct b53_device *dev = ds->priv;
  682. const struct b53_mib_desc *mibs = b53_get_mib(dev);
  683. unsigned int mib_size = b53_get_mib_size(dev);
  684. unsigned int i;
  685. for (i = 0; i < mib_size; i++)
  686. memcpy(data + i * ETH_GSTRING_LEN,
  687. mibs[i].name, ETH_GSTRING_LEN);
  688. }
  689. EXPORT_SYMBOL(b53_get_strings);
  690. void b53_get_ethtool_stats(struct dsa_switch *ds, int port, uint64_t *data)
  691. {
  692. struct b53_device *dev = ds->priv;
  693. const struct b53_mib_desc *mibs = b53_get_mib(dev);
  694. unsigned int mib_size = b53_get_mib_size(dev);
  695. const struct b53_mib_desc *s;
  696. unsigned int i;
  697. u64 val = 0;
  698. if (is5365(dev) && port == 5)
  699. port = 8;
  700. mutex_lock(&dev->stats_mutex);
  701. for (i = 0; i < mib_size; i++) {
  702. s = &mibs[i];
  703. if (s->size == 8) {
  704. b53_read64(dev, B53_MIB_PAGE(port), s->offset, &val);
  705. } else {
  706. u32 val32;
  707. b53_read32(dev, B53_MIB_PAGE(port), s->offset,
  708. &val32);
  709. val = val32;
  710. }
  711. data[i] = (u64)val;
  712. }
  713. mutex_unlock(&dev->stats_mutex);
  714. }
  715. EXPORT_SYMBOL(b53_get_ethtool_stats);
  716. int b53_get_sset_count(struct dsa_switch *ds)
  717. {
  718. struct b53_device *dev = ds->priv;
  719. return b53_get_mib_size(dev);
  720. }
  721. EXPORT_SYMBOL(b53_get_sset_count);
  722. static int b53_setup(struct dsa_switch *ds)
  723. {
  724. struct b53_device *dev = ds->priv;
  725. unsigned int port;
  726. int ret;
  727. ret = b53_reset_switch(dev);
  728. if (ret) {
  729. dev_err(ds->dev, "failed to reset switch\n");
  730. return ret;
  731. }
  732. b53_reset_mib(dev);
  733. ret = b53_apply_config(dev);
  734. if (ret)
  735. dev_err(ds->dev, "failed to apply configuration\n");
  736. /* Configure IMP/CPU port, disable unused ports. Enabled
  737. * ports will be configured with .port_enable
  738. */
  739. for (port = 0; port < dev->num_ports; port++) {
  740. if (dsa_is_cpu_port(ds, port))
  741. b53_enable_cpu_port(dev, port);
  742. else if (dsa_is_unused_port(ds, port))
  743. b53_disable_port(ds, port, NULL);
  744. }
  745. return ret;
  746. }
  747. static void b53_adjust_link(struct dsa_switch *ds, int port,
  748. struct phy_device *phydev)
  749. {
  750. struct b53_device *dev = ds->priv;
  751. struct ethtool_eee *p = &dev->ports[port].eee;
  752. u8 rgmii_ctrl = 0, reg = 0, off;
  753. if (!phy_is_pseudo_fixed_link(phydev))
  754. return;
  755. /* Override the port settings */
  756. if (port == dev->cpu_port) {
  757. off = B53_PORT_OVERRIDE_CTRL;
  758. reg = PORT_OVERRIDE_EN;
  759. } else {
  760. off = B53_GMII_PORT_OVERRIDE_CTRL(port);
  761. reg = GMII_PO_EN;
  762. }
  763. /* Set the link UP */
  764. if (phydev->link)
  765. reg |= PORT_OVERRIDE_LINK;
  766. if (phydev->duplex == DUPLEX_FULL)
  767. reg |= PORT_OVERRIDE_FULL_DUPLEX;
  768. switch (phydev->speed) {
  769. case 2000:
  770. reg |= PORT_OVERRIDE_SPEED_2000M;
  771. /* fallthrough */
  772. case SPEED_1000:
  773. reg |= PORT_OVERRIDE_SPEED_1000M;
  774. break;
  775. case SPEED_100:
  776. reg |= PORT_OVERRIDE_SPEED_100M;
  777. break;
  778. case SPEED_10:
  779. reg |= PORT_OVERRIDE_SPEED_10M;
  780. break;
  781. default:
  782. dev_err(ds->dev, "unknown speed: %d\n", phydev->speed);
  783. return;
  784. }
  785. /* Enable flow control on BCM5301x's CPU port */
  786. if (is5301x(dev) && port == dev->cpu_port)
  787. reg |= PORT_OVERRIDE_RX_FLOW | PORT_OVERRIDE_TX_FLOW;
  788. if (phydev->pause) {
  789. if (phydev->asym_pause)
  790. reg |= PORT_OVERRIDE_TX_FLOW;
  791. reg |= PORT_OVERRIDE_RX_FLOW;
  792. }
  793. b53_write8(dev, B53_CTRL_PAGE, off, reg);
  794. if (is531x5(dev) && phy_interface_is_rgmii(phydev)) {
  795. if (port == 8)
  796. off = B53_RGMII_CTRL_IMP;
  797. else
  798. off = B53_RGMII_CTRL_P(port);
  799. /* Configure the port RGMII clock delay by DLL disabled and
  800. * tx_clk aligned timing (restoring to reset defaults)
  801. */
  802. b53_read8(dev, B53_CTRL_PAGE, off, &rgmii_ctrl);
  803. rgmii_ctrl &= ~(RGMII_CTRL_DLL_RXC | RGMII_CTRL_DLL_TXC |
  804. RGMII_CTRL_TIMING_SEL);
  805. /* PHY_INTERFACE_MODE_RGMII_TXID means TX internal delay, make
  806. * sure that we enable the port TX clock internal delay to
  807. * account for this internal delay that is inserted, otherwise
  808. * the switch won't be able to receive correctly.
  809. *
  810. * PHY_INTERFACE_MODE_RGMII means that we are not introducing
  811. * any delay neither on transmission nor reception, so the
  812. * BCM53125 must also be configured accordingly to account for
  813. * the lack of delay and introduce
  814. *
  815. * The BCM53125 switch has its RX clock and TX clock control
  816. * swapped, hence the reason why we modify the TX clock path in
  817. * the "RGMII" case
  818. */
  819. if (phydev->interface == PHY_INTERFACE_MODE_RGMII_TXID)
  820. rgmii_ctrl |= RGMII_CTRL_DLL_TXC;
  821. if (phydev->interface == PHY_INTERFACE_MODE_RGMII)
  822. rgmii_ctrl |= RGMII_CTRL_DLL_TXC | RGMII_CTRL_DLL_RXC;
  823. rgmii_ctrl |= RGMII_CTRL_TIMING_SEL;
  824. b53_write8(dev, B53_CTRL_PAGE, off, rgmii_ctrl);
  825. dev_info(ds->dev, "Configured port %d for %s\n", port,
  826. phy_modes(phydev->interface));
  827. }
  828. /* configure MII port if necessary */
  829. if (is5325(dev)) {
  830. b53_read8(dev, B53_CTRL_PAGE, B53_PORT_OVERRIDE_CTRL,
  831. &reg);
  832. /* reverse mii needs to be enabled */
  833. if (!(reg & PORT_OVERRIDE_RV_MII_25)) {
  834. b53_write8(dev, B53_CTRL_PAGE, B53_PORT_OVERRIDE_CTRL,
  835. reg | PORT_OVERRIDE_RV_MII_25);
  836. b53_read8(dev, B53_CTRL_PAGE, B53_PORT_OVERRIDE_CTRL,
  837. &reg);
  838. if (!(reg & PORT_OVERRIDE_RV_MII_25)) {
  839. dev_err(ds->dev,
  840. "Failed to enable reverse MII mode\n");
  841. return;
  842. }
  843. }
  844. } else if (is5301x(dev)) {
  845. if (port != dev->cpu_port) {
  846. u8 po_reg = B53_GMII_PORT_OVERRIDE_CTRL(dev->cpu_port);
  847. u8 gmii_po;
  848. b53_read8(dev, B53_CTRL_PAGE, po_reg, &gmii_po);
  849. gmii_po |= GMII_PO_LINK |
  850. GMII_PO_RX_FLOW |
  851. GMII_PO_TX_FLOW |
  852. GMII_PO_EN |
  853. GMII_PO_SPEED_2000M;
  854. b53_write8(dev, B53_CTRL_PAGE, po_reg, gmii_po);
  855. }
  856. }
  857. /* Re-negotiate EEE if it was enabled already */
  858. p->eee_enabled = b53_eee_init(ds, port, phydev);
  859. }
  860. int b53_vlan_filtering(struct dsa_switch *ds, int port, bool vlan_filtering)
  861. {
  862. return 0;
  863. }
  864. EXPORT_SYMBOL(b53_vlan_filtering);
  865. int b53_vlan_prepare(struct dsa_switch *ds, int port,
  866. const struct switchdev_obj_port_vlan *vlan,
  867. struct switchdev_trans *trans)
  868. {
  869. struct b53_device *dev = ds->priv;
  870. if ((is5325(dev) || is5365(dev)) && vlan->vid_begin == 0)
  871. return -EOPNOTSUPP;
  872. if (vlan->vid_end > dev->num_vlans)
  873. return -ERANGE;
  874. b53_enable_vlan(dev, true);
  875. return 0;
  876. }
  877. EXPORT_SYMBOL(b53_vlan_prepare);
  878. void b53_vlan_add(struct dsa_switch *ds, int port,
  879. const struct switchdev_obj_port_vlan *vlan,
  880. struct switchdev_trans *trans)
  881. {
  882. struct b53_device *dev = ds->priv;
  883. bool untagged = vlan->flags & BRIDGE_VLAN_INFO_UNTAGGED;
  884. bool pvid = vlan->flags & BRIDGE_VLAN_INFO_PVID;
  885. unsigned int cpu_port = dev->cpu_port;
  886. struct b53_vlan *vl;
  887. u16 vid;
  888. for (vid = vlan->vid_begin; vid <= vlan->vid_end; ++vid) {
  889. vl = &dev->vlans[vid];
  890. b53_get_vlan_entry(dev, vid, vl);
  891. vl->members |= BIT(port) | BIT(cpu_port);
  892. if (untagged)
  893. vl->untag |= BIT(port);
  894. else
  895. vl->untag &= ~BIT(port);
  896. vl->untag &= ~BIT(cpu_port);
  897. b53_set_vlan_entry(dev, vid, vl);
  898. b53_fast_age_vlan(dev, vid);
  899. }
  900. if (pvid) {
  901. b53_write16(dev, B53_VLAN_PAGE, B53_VLAN_PORT_DEF_TAG(port),
  902. vlan->vid_end);
  903. b53_fast_age_vlan(dev, vid);
  904. }
  905. }
  906. EXPORT_SYMBOL(b53_vlan_add);
  907. int b53_vlan_del(struct dsa_switch *ds, int port,
  908. const struct switchdev_obj_port_vlan *vlan)
  909. {
  910. struct b53_device *dev = ds->priv;
  911. bool untagged = vlan->flags & BRIDGE_VLAN_INFO_UNTAGGED;
  912. struct b53_vlan *vl;
  913. u16 vid;
  914. u16 pvid;
  915. b53_read16(dev, B53_VLAN_PAGE, B53_VLAN_PORT_DEF_TAG(port), &pvid);
  916. for (vid = vlan->vid_begin; vid <= vlan->vid_end; ++vid) {
  917. vl = &dev->vlans[vid];
  918. b53_get_vlan_entry(dev, vid, vl);
  919. vl->members &= ~BIT(port);
  920. if (pvid == vid) {
  921. if (is5325(dev) || is5365(dev))
  922. pvid = 1;
  923. else
  924. pvid = 0;
  925. }
  926. if (untagged)
  927. vl->untag &= ~(BIT(port));
  928. b53_set_vlan_entry(dev, vid, vl);
  929. b53_fast_age_vlan(dev, vid);
  930. }
  931. b53_write16(dev, B53_VLAN_PAGE, B53_VLAN_PORT_DEF_TAG(port), pvid);
  932. b53_fast_age_vlan(dev, pvid);
  933. return 0;
  934. }
  935. EXPORT_SYMBOL(b53_vlan_del);
  936. /* Address Resolution Logic routines */
  937. static int b53_arl_op_wait(struct b53_device *dev)
  938. {
  939. unsigned int timeout = 10;
  940. u8 reg;
  941. do {
  942. b53_read8(dev, B53_ARLIO_PAGE, B53_ARLTBL_RW_CTRL, &reg);
  943. if (!(reg & ARLTBL_START_DONE))
  944. return 0;
  945. usleep_range(1000, 2000);
  946. } while (timeout--);
  947. dev_warn(dev->dev, "timeout waiting for ARL to finish: 0x%02x\n", reg);
  948. return -ETIMEDOUT;
  949. }
  950. static int b53_arl_rw_op(struct b53_device *dev, unsigned int op)
  951. {
  952. u8 reg;
  953. if (op > ARLTBL_RW)
  954. return -EINVAL;
  955. b53_read8(dev, B53_ARLIO_PAGE, B53_ARLTBL_RW_CTRL, &reg);
  956. reg |= ARLTBL_START_DONE;
  957. if (op)
  958. reg |= ARLTBL_RW;
  959. else
  960. reg &= ~ARLTBL_RW;
  961. b53_write8(dev, B53_ARLIO_PAGE, B53_ARLTBL_RW_CTRL, reg);
  962. return b53_arl_op_wait(dev);
  963. }
  964. static int b53_arl_read(struct b53_device *dev, u64 mac,
  965. u16 vid, struct b53_arl_entry *ent, u8 *idx,
  966. bool is_valid)
  967. {
  968. unsigned int i;
  969. int ret;
  970. ret = b53_arl_op_wait(dev);
  971. if (ret)
  972. return ret;
  973. /* Read the bins */
  974. for (i = 0; i < dev->num_arl_entries; i++) {
  975. u64 mac_vid;
  976. u32 fwd_entry;
  977. b53_read64(dev, B53_ARLIO_PAGE,
  978. B53_ARLTBL_MAC_VID_ENTRY(i), &mac_vid);
  979. b53_read32(dev, B53_ARLIO_PAGE,
  980. B53_ARLTBL_DATA_ENTRY(i), &fwd_entry);
  981. b53_arl_to_entry(ent, mac_vid, fwd_entry);
  982. if (!(fwd_entry & ARLTBL_VALID))
  983. continue;
  984. if ((mac_vid & ARLTBL_MAC_MASK) != mac)
  985. continue;
  986. *idx = i;
  987. }
  988. return -ENOENT;
  989. }
  990. static int b53_arl_op(struct b53_device *dev, int op, int port,
  991. const unsigned char *addr, u16 vid, bool is_valid)
  992. {
  993. struct b53_arl_entry ent;
  994. u32 fwd_entry;
  995. u64 mac, mac_vid = 0;
  996. u8 idx = 0;
  997. int ret;
  998. /* Convert the array into a 64-bit MAC */
  999. mac = ether_addr_to_u64(addr);
  1000. /* Perform a read for the given MAC and VID */
  1001. b53_write48(dev, B53_ARLIO_PAGE, B53_MAC_ADDR_IDX, mac);
  1002. b53_write16(dev, B53_ARLIO_PAGE, B53_VLAN_ID_IDX, vid);
  1003. /* Issue a read operation for this MAC */
  1004. ret = b53_arl_rw_op(dev, 1);
  1005. if (ret)
  1006. return ret;
  1007. ret = b53_arl_read(dev, mac, vid, &ent, &idx, is_valid);
  1008. /* If this is a read, just finish now */
  1009. if (op)
  1010. return ret;
  1011. /* We could not find a matching MAC, so reset to a new entry */
  1012. if (ret) {
  1013. fwd_entry = 0;
  1014. idx = 1;
  1015. }
  1016. memset(&ent, 0, sizeof(ent));
  1017. ent.port = port;
  1018. ent.is_valid = is_valid;
  1019. ent.vid = vid;
  1020. ent.is_static = true;
  1021. memcpy(ent.mac, addr, ETH_ALEN);
  1022. b53_arl_from_entry(&mac_vid, &fwd_entry, &ent);
  1023. b53_write64(dev, B53_ARLIO_PAGE,
  1024. B53_ARLTBL_MAC_VID_ENTRY(idx), mac_vid);
  1025. b53_write32(dev, B53_ARLIO_PAGE,
  1026. B53_ARLTBL_DATA_ENTRY(idx), fwd_entry);
  1027. return b53_arl_rw_op(dev, 0);
  1028. }
  1029. int b53_fdb_add(struct dsa_switch *ds, int port,
  1030. const unsigned char *addr, u16 vid)
  1031. {
  1032. struct b53_device *priv = ds->priv;
  1033. /* 5325 and 5365 require some more massaging, but could
  1034. * be supported eventually
  1035. */
  1036. if (is5325(priv) || is5365(priv))
  1037. return -EOPNOTSUPP;
  1038. return b53_arl_op(priv, 0, port, addr, vid, true);
  1039. }
  1040. EXPORT_SYMBOL(b53_fdb_add);
  1041. int b53_fdb_del(struct dsa_switch *ds, int port,
  1042. const unsigned char *addr, u16 vid)
  1043. {
  1044. struct b53_device *priv = ds->priv;
  1045. return b53_arl_op(priv, 0, port, addr, vid, false);
  1046. }
  1047. EXPORT_SYMBOL(b53_fdb_del);
  1048. static int b53_arl_search_wait(struct b53_device *dev)
  1049. {
  1050. unsigned int timeout = 1000;
  1051. u8 reg;
  1052. do {
  1053. b53_read8(dev, B53_ARLIO_PAGE, B53_ARL_SRCH_CTL, &reg);
  1054. if (!(reg & ARL_SRCH_STDN))
  1055. return 0;
  1056. if (reg & ARL_SRCH_VLID)
  1057. return 0;
  1058. usleep_range(1000, 2000);
  1059. } while (timeout--);
  1060. return -ETIMEDOUT;
  1061. }
  1062. static void b53_arl_search_rd(struct b53_device *dev, u8 idx,
  1063. struct b53_arl_entry *ent)
  1064. {
  1065. u64 mac_vid;
  1066. u32 fwd_entry;
  1067. b53_read64(dev, B53_ARLIO_PAGE,
  1068. B53_ARL_SRCH_RSTL_MACVID(idx), &mac_vid);
  1069. b53_read32(dev, B53_ARLIO_PAGE,
  1070. B53_ARL_SRCH_RSTL(idx), &fwd_entry);
  1071. b53_arl_to_entry(ent, mac_vid, fwd_entry);
  1072. }
  1073. static int b53_fdb_copy(int port, const struct b53_arl_entry *ent,
  1074. dsa_fdb_dump_cb_t *cb, void *data)
  1075. {
  1076. if (!ent->is_valid)
  1077. return 0;
  1078. if (port != ent->port)
  1079. return 0;
  1080. return cb(ent->mac, ent->vid, ent->is_static, data);
  1081. }
  1082. int b53_fdb_dump(struct dsa_switch *ds, int port,
  1083. dsa_fdb_dump_cb_t *cb, void *data)
  1084. {
  1085. struct b53_device *priv = ds->priv;
  1086. struct b53_arl_entry results[2];
  1087. unsigned int count = 0;
  1088. int ret;
  1089. u8 reg;
  1090. /* Start search operation */
  1091. reg = ARL_SRCH_STDN;
  1092. b53_write8(priv, B53_ARLIO_PAGE, B53_ARL_SRCH_CTL, reg);
  1093. do {
  1094. ret = b53_arl_search_wait(priv);
  1095. if (ret)
  1096. return ret;
  1097. b53_arl_search_rd(priv, 0, &results[0]);
  1098. ret = b53_fdb_copy(port, &results[0], cb, data);
  1099. if (ret)
  1100. return ret;
  1101. if (priv->num_arl_entries > 2) {
  1102. b53_arl_search_rd(priv, 1, &results[1]);
  1103. ret = b53_fdb_copy(port, &results[1], cb, data);
  1104. if (ret)
  1105. return ret;
  1106. if (!results[0].is_valid && !results[1].is_valid)
  1107. break;
  1108. }
  1109. } while (count++ < 1024);
  1110. return 0;
  1111. }
  1112. EXPORT_SYMBOL(b53_fdb_dump);
  1113. int b53_br_join(struct dsa_switch *ds, int port, struct net_device *br)
  1114. {
  1115. struct b53_device *dev = ds->priv;
  1116. s8 cpu_port = ds->ports[port].cpu_dp->index;
  1117. u16 pvlan, reg;
  1118. unsigned int i;
  1119. /* Make this port leave the all VLANs join since we will have proper
  1120. * VLAN entries from now on
  1121. */
  1122. if (is58xx(dev)) {
  1123. b53_read16(dev, B53_VLAN_PAGE, B53_JOIN_ALL_VLAN_EN, &reg);
  1124. reg &= ~BIT(port);
  1125. if ((reg & BIT(cpu_port)) == BIT(cpu_port))
  1126. reg &= ~BIT(cpu_port);
  1127. b53_write16(dev, B53_VLAN_PAGE, B53_JOIN_ALL_VLAN_EN, reg);
  1128. }
  1129. b53_read16(dev, B53_PVLAN_PAGE, B53_PVLAN_PORT_MASK(port), &pvlan);
  1130. b53_for_each_port(dev, i) {
  1131. if (dsa_to_port(ds, i)->bridge_dev != br)
  1132. continue;
  1133. /* Add this local port to the remote port VLAN control
  1134. * membership and update the remote port bitmask
  1135. */
  1136. b53_read16(dev, B53_PVLAN_PAGE, B53_PVLAN_PORT_MASK(i), &reg);
  1137. reg |= BIT(port);
  1138. b53_write16(dev, B53_PVLAN_PAGE, B53_PVLAN_PORT_MASK(i), reg);
  1139. dev->ports[i].vlan_ctl_mask = reg;
  1140. pvlan |= BIT(i);
  1141. }
  1142. /* Configure the local port VLAN control membership to include
  1143. * remote ports and update the local port bitmask
  1144. */
  1145. b53_write16(dev, B53_PVLAN_PAGE, B53_PVLAN_PORT_MASK(port), pvlan);
  1146. dev->ports[port].vlan_ctl_mask = pvlan;
  1147. return 0;
  1148. }
  1149. EXPORT_SYMBOL(b53_br_join);
  1150. void b53_br_leave(struct dsa_switch *ds, int port, struct net_device *br)
  1151. {
  1152. struct b53_device *dev = ds->priv;
  1153. struct b53_vlan *vl = &dev->vlans[0];
  1154. s8 cpu_port = ds->ports[port].cpu_dp->index;
  1155. unsigned int i;
  1156. u16 pvlan, reg, pvid;
  1157. b53_read16(dev, B53_PVLAN_PAGE, B53_PVLAN_PORT_MASK(port), &pvlan);
  1158. b53_for_each_port(dev, i) {
  1159. /* Don't touch the remaining ports */
  1160. if (dsa_to_port(ds, i)->bridge_dev != br)
  1161. continue;
  1162. b53_read16(dev, B53_PVLAN_PAGE, B53_PVLAN_PORT_MASK(i), &reg);
  1163. reg &= ~BIT(port);
  1164. b53_write16(dev, B53_PVLAN_PAGE, B53_PVLAN_PORT_MASK(i), reg);
  1165. dev->ports[port].vlan_ctl_mask = reg;
  1166. /* Prevent self removal to preserve isolation */
  1167. if (port != i)
  1168. pvlan &= ~BIT(i);
  1169. }
  1170. b53_write16(dev, B53_PVLAN_PAGE, B53_PVLAN_PORT_MASK(port), pvlan);
  1171. dev->ports[port].vlan_ctl_mask = pvlan;
  1172. if (is5325(dev) || is5365(dev))
  1173. pvid = 1;
  1174. else
  1175. pvid = 0;
  1176. /* Make this port join all VLANs without VLAN entries */
  1177. if (is58xx(dev)) {
  1178. b53_read16(dev, B53_VLAN_PAGE, B53_JOIN_ALL_VLAN_EN, &reg);
  1179. reg |= BIT(port);
  1180. if (!(reg & BIT(cpu_port)))
  1181. reg |= BIT(cpu_port);
  1182. b53_write16(dev, B53_VLAN_PAGE, B53_JOIN_ALL_VLAN_EN, reg);
  1183. } else {
  1184. b53_get_vlan_entry(dev, pvid, vl);
  1185. vl->members |= BIT(port) | BIT(dev->cpu_port);
  1186. vl->untag |= BIT(port) | BIT(dev->cpu_port);
  1187. b53_set_vlan_entry(dev, pvid, vl);
  1188. }
  1189. }
  1190. EXPORT_SYMBOL(b53_br_leave);
  1191. void b53_br_set_stp_state(struct dsa_switch *ds, int port, u8 state)
  1192. {
  1193. struct b53_device *dev = ds->priv;
  1194. u8 hw_state;
  1195. u8 reg;
  1196. switch (state) {
  1197. case BR_STATE_DISABLED:
  1198. hw_state = PORT_CTRL_DIS_STATE;
  1199. break;
  1200. case BR_STATE_LISTENING:
  1201. hw_state = PORT_CTRL_LISTEN_STATE;
  1202. break;
  1203. case BR_STATE_LEARNING:
  1204. hw_state = PORT_CTRL_LEARN_STATE;
  1205. break;
  1206. case BR_STATE_FORWARDING:
  1207. hw_state = PORT_CTRL_FWD_STATE;
  1208. break;
  1209. case BR_STATE_BLOCKING:
  1210. hw_state = PORT_CTRL_BLOCK_STATE;
  1211. break;
  1212. default:
  1213. dev_err(ds->dev, "invalid STP state: %d\n", state);
  1214. return;
  1215. }
  1216. b53_read8(dev, B53_CTRL_PAGE, B53_PORT_CTRL(port), &reg);
  1217. reg &= ~PORT_CTRL_STP_STATE_MASK;
  1218. reg |= hw_state;
  1219. b53_write8(dev, B53_CTRL_PAGE, B53_PORT_CTRL(port), reg);
  1220. }
  1221. EXPORT_SYMBOL(b53_br_set_stp_state);
  1222. void b53_br_fast_age(struct dsa_switch *ds, int port)
  1223. {
  1224. struct b53_device *dev = ds->priv;
  1225. if (b53_fast_age_port(dev, port))
  1226. dev_err(ds->dev, "fast ageing failed\n");
  1227. }
  1228. EXPORT_SYMBOL(b53_br_fast_age);
  1229. static enum dsa_tag_protocol b53_get_tag_protocol(struct dsa_switch *ds)
  1230. {
  1231. return DSA_TAG_PROTO_NONE;
  1232. }
  1233. int b53_mirror_add(struct dsa_switch *ds, int port,
  1234. struct dsa_mall_mirror_tc_entry *mirror, bool ingress)
  1235. {
  1236. struct b53_device *dev = ds->priv;
  1237. u16 reg, loc;
  1238. if (ingress)
  1239. loc = B53_IG_MIR_CTL;
  1240. else
  1241. loc = B53_EG_MIR_CTL;
  1242. b53_read16(dev, B53_MGMT_PAGE, loc, &reg);
  1243. reg &= ~MIRROR_MASK;
  1244. reg |= BIT(port);
  1245. b53_write16(dev, B53_MGMT_PAGE, loc, reg);
  1246. b53_read16(dev, B53_MGMT_PAGE, B53_MIR_CAP_CTL, &reg);
  1247. reg &= ~CAP_PORT_MASK;
  1248. reg |= mirror->to_local_port;
  1249. reg |= MIRROR_EN;
  1250. b53_write16(dev, B53_MGMT_PAGE, B53_MIR_CAP_CTL, reg);
  1251. return 0;
  1252. }
  1253. EXPORT_SYMBOL(b53_mirror_add);
  1254. void b53_mirror_del(struct dsa_switch *ds, int port,
  1255. struct dsa_mall_mirror_tc_entry *mirror)
  1256. {
  1257. struct b53_device *dev = ds->priv;
  1258. bool loc_disable = false, other_loc_disable = false;
  1259. u16 reg, loc;
  1260. if (mirror->ingress)
  1261. loc = B53_IG_MIR_CTL;
  1262. else
  1263. loc = B53_EG_MIR_CTL;
  1264. /* Update the desired ingress/egress register */
  1265. b53_read16(dev, B53_MGMT_PAGE, loc, &reg);
  1266. reg &= ~BIT(port);
  1267. if (!(reg & MIRROR_MASK))
  1268. loc_disable = true;
  1269. b53_write16(dev, B53_MGMT_PAGE, loc, reg);
  1270. /* Now look at the other one to know if we can disable mirroring
  1271. * entirely
  1272. */
  1273. if (mirror->ingress)
  1274. b53_read16(dev, B53_MGMT_PAGE, B53_EG_MIR_CTL, &reg);
  1275. else
  1276. b53_read16(dev, B53_MGMT_PAGE, B53_IG_MIR_CTL, &reg);
  1277. if (!(reg & MIRROR_MASK))
  1278. other_loc_disable = true;
  1279. b53_read16(dev, B53_MGMT_PAGE, B53_MIR_CAP_CTL, &reg);
  1280. /* Both no longer have ports, let's disable mirroring */
  1281. if (loc_disable && other_loc_disable) {
  1282. reg &= ~MIRROR_EN;
  1283. reg &= ~mirror->to_local_port;
  1284. }
  1285. b53_write16(dev, B53_MGMT_PAGE, B53_MIR_CAP_CTL, reg);
  1286. }
  1287. EXPORT_SYMBOL(b53_mirror_del);
  1288. void b53_eee_enable_set(struct dsa_switch *ds, int port, bool enable)
  1289. {
  1290. struct b53_device *dev = ds->priv;
  1291. u16 reg;
  1292. b53_read16(dev, B53_EEE_PAGE, B53_EEE_EN_CTRL, &reg);
  1293. if (enable)
  1294. reg |= BIT(port);
  1295. else
  1296. reg &= ~BIT(port);
  1297. b53_write16(dev, B53_EEE_PAGE, B53_EEE_EN_CTRL, reg);
  1298. }
  1299. EXPORT_SYMBOL(b53_eee_enable_set);
  1300. /* Returns 0 if EEE was not enabled, or 1 otherwise
  1301. */
  1302. int b53_eee_init(struct dsa_switch *ds, int port, struct phy_device *phy)
  1303. {
  1304. int ret;
  1305. ret = phy_init_eee(phy, 0);
  1306. if (ret)
  1307. return 0;
  1308. b53_eee_enable_set(ds, port, true);
  1309. return 1;
  1310. }
  1311. EXPORT_SYMBOL(b53_eee_init);
  1312. int b53_get_mac_eee(struct dsa_switch *ds, int port, struct ethtool_eee *e)
  1313. {
  1314. struct b53_device *dev = ds->priv;
  1315. struct ethtool_eee *p = &dev->ports[port].eee;
  1316. u16 reg;
  1317. if (is5325(dev) || is5365(dev))
  1318. return -EOPNOTSUPP;
  1319. b53_read16(dev, B53_EEE_PAGE, B53_EEE_LPI_INDICATE, &reg);
  1320. e->eee_enabled = p->eee_enabled;
  1321. e->eee_active = !!(reg & BIT(port));
  1322. return 0;
  1323. }
  1324. EXPORT_SYMBOL(b53_get_mac_eee);
  1325. int b53_set_mac_eee(struct dsa_switch *ds, int port, struct ethtool_eee *e)
  1326. {
  1327. struct b53_device *dev = ds->priv;
  1328. struct ethtool_eee *p = &dev->ports[port].eee;
  1329. if (is5325(dev) || is5365(dev))
  1330. return -EOPNOTSUPP;
  1331. p->eee_enabled = e->eee_enabled;
  1332. b53_eee_enable_set(ds, port, e->eee_enabled);
  1333. return 0;
  1334. }
  1335. EXPORT_SYMBOL(b53_set_mac_eee);
  1336. static const struct dsa_switch_ops b53_switch_ops = {
  1337. .get_tag_protocol = b53_get_tag_protocol,
  1338. .setup = b53_setup,
  1339. .get_strings = b53_get_strings,
  1340. .get_ethtool_stats = b53_get_ethtool_stats,
  1341. .get_sset_count = b53_get_sset_count,
  1342. .phy_read = b53_phy_read16,
  1343. .phy_write = b53_phy_write16,
  1344. .adjust_link = b53_adjust_link,
  1345. .port_enable = b53_enable_port,
  1346. .port_disable = b53_disable_port,
  1347. .get_mac_eee = b53_get_mac_eee,
  1348. .set_mac_eee = b53_set_mac_eee,
  1349. .port_bridge_join = b53_br_join,
  1350. .port_bridge_leave = b53_br_leave,
  1351. .port_stp_state_set = b53_br_set_stp_state,
  1352. .port_fast_age = b53_br_fast_age,
  1353. .port_vlan_filtering = b53_vlan_filtering,
  1354. .port_vlan_prepare = b53_vlan_prepare,
  1355. .port_vlan_add = b53_vlan_add,
  1356. .port_vlan_del = b53_vlan_del,
  1357. .port_fdb_dump = b53_fdb_dump,
  1358. .port_fdb_add = b53_fdb_add,
  1359. .port_fdb_del = b53_fdb_del,
  1360. .port_mirror_add = b53_mirror_add,
  1361. .port_mirror_del = b53_mirror_del,
  1362. };
  1363. struct b53_chip_data {
  1364. u32 chip_id;
  1365. const char *dev_name;
  1366. u16 vlans;
  1367. u16 enabled_ports;
  1368. u8 cpu_port;
  1369. u8 vta_regs[3];
  1370. u8 arl_entries;
  1371. u8 duplex_reg;
  1372. u8 jumbo_pm_reg;
  1373. u8 jumbo_size_reg;
  1374. };
  1375. #define B53_VTA_REGS \
  1376. { B53_VT_ACCESS, B53_VT_INDEX, B53_VT_ENTRY }
  1377. #define B53_VTA_REGS_9798 \
  1378. { B53_VT_ACCESS_9798, B53_VT_INDEX_9798, B53_VT_ENTRY_9798 }
  1379. #define B53_VTA_REGS_63XX \
  1380. { B53_VT_ACCESS_63XX, B53_VT_INDEX_63XX, B53_VT_ENTRY_63XX }
  1381. static const struct b53_chip_data b53_switch_chips[] = {
  1382. {
  1383. .chip_id = BCM5325_DEVICE_ID,
  1384. .dev_name = "BCM5325",
  1385. .vlans = 16,
  1386. .enabled_ports = 0x1f,
  1387. .arl_entries = 2,
  1388. .cpu_port = B53_CPU_PORT_25,
  1389. .duplex_reg = B53_DUPLEX_STAT_FE,
  1390. },
  1391. {
  1392. .chip_id = BCM5365_DEVICE_ID,
  1393. .dev_name = "BCM5365",
  1394. .vlans = 256,
  1395. .enabled_ports = 0x1f,
  1396. .arl_entries = 2,
  1397. .cpu_port = B53_CPU_PORT_25,
  1398. .duplex_reg = B53_DUPLEX_STAT_FE,
  1399. },
  1400. {
  1401. .chip_id = BCM5395_DEVICE_ID,
  1402. .dev_name = "BCM5395",
  1403. .vlans = 4096,
  1404. .enabled_ports = 0x1f,
  1405. .arl_entries = 4,
  1406. .cpu_port = B53_CPU_PORT,
  1407. .vta_regs = B53_VTA_REGS,
  1408. .duplex_reg = B53_DUPLEX_STAT_GE,
  1409. .jumbo_pm_reg = B53_JUMBO_PORT_MASK,
  1410. .jumbo_size_reg = B53_JUMBO_MAX_SIZE,
  1411. },
  1412. {
  1413. .chip_id = BCM5397_DEVICE_ID,
  1414. .dev_name = "BCM5397",
  1415. .vlans = 4096,
  1416. .enabled_ports = 0x1f,
  1417. .arl_entries = 4,
  1418. .cpu_port = B53_CPU_PORT,
  1419. .vta_regs = B53_VTA_REGS_9798,
  1420. .duplex_reg = B53_DUPLEX_STAT_GE,
  1421. .jumbo_pm_reg = B53_JUMBO_PORT_MASK,
  1422. .jumbo_size_reg = B53_JUMBO_MAX_SIZE,
  1423. },
  1424. {
  1425. .chip_id = BCM5398_DEVICE_ID,
  1426. .dev_name = "BCM5398",
  1427. .vlans = 4096,
  1428. .enabled_ports = 0x7f,
  1429. .arl_entries = 4,
  1430. .cpu_port = B53_CPU_PORT,
  1431. .vta_regs = B53_VTA_REGS_9798,
  1432. .duplex_reg = B53_DUPLEX_STAT_GE,
  1433. .jumbo_pm_reg = B53_JUMBO_PORT_MASK,
  1434. .jumbo_size_reg = B53_JUMBO_MAX_SIZE,
  1435. },
  1436. {
  1437. .chip_id = BCM53115_DEVICE_ID,
  1438. .dev_name = "BCM53115",
  1439. .vlans = 4096,
  1440. .enabled_ports = 0x1f,
  1441. .arl_entries = 4,
  1442. .vta_regs = B53_VTA_REGS,
  1443. .cpu_port = B53_CPU_PORT,
  1444. .duplex_reg = B53_DUPLEX_STAT_GE,
  1445. .jumbo_pm_reg = B53_JUMBO_PORT_MASK,
  1446. .jumbo_size_reg = B53_JUMBO_MAX_SIZE,
  1447. },
  1448. {
  1449. .chip_id = BCM53125_DEVICE_ID,
  1450. .dev_name = "BCM53125",
  1451. .vlans = 4096,
  1452. .enabled_ports = 0xff,
  1453. .arl_entries = 4,
  1454. .cpu_port = B53_CPU_PORT,
  1455. .vta_regs = B53_VTA_REGS,
  1456. .duplex_reg = B53_DUPLEX_STAT_GE,
  1457. .jumbo_pm_reg = B53_JUMBO_PORT_MASK,
  1458. .jumbo_size_reg = B53_JUMBO_MAX_SIZE,
  1459. },
  1460. {
  1461. .chip_id = BCM53128_DEVICE_ID,
  1462. .dev_name = "BCM53128",
  1463. .vlans = 4096,
  1464. .enabled_ports = 0x1ff,
  1465. .arl_entries = 4,
  1466. .cpu_port = B53_CPU_PORT,
  1467. .vta_regs = B53_VTA_REGS,
  1468. .duplex_reg = B53_DUPLEX_STAT_GE,
  1469. .jumbo_pm_reg = B53_JUMBO_PORT_MASK,
  1470. .jumbo_size_reg = B53_JUMBO_MAX_SIZE,
  1471. },
  1472. {
  1473. .chip_id = BCM63XX_DEVICE_ID,
  1474. .dev_name = "BCM63xx",
  1475. .vlans = 4096,
  1476. .enabled_ports = 0, /* pdata must provide them */
  1477. .arl_entries = 4,
  1478. .cpu_port = B53_CPU_PORT,
  1479. .vta_regs = B53_VTA_REGS_63XX,
  1480. .duplex_reg = B53_DUPLEX_STAT_63XX,
  1481. .jumbo_pm_reg = B53_JUMBO_PORT_MASK_63XX,
  1482. .jumbo_size_reg = B53_JUMBO_MAX_SIZE_63XX,
  1483. },
  1484. {
  1485. .chip_id = BCM53010_DEVICE_ID,
  1486. .dev_name = "BCM53010",
  1487. .vlans = 4096,
  1488. .enabled_ports = 0x1f,
  1489. .arl_entries = 4,
  1490. .cpu_port = B53_CPU_PORT_25, /* TODO: auto detect */
  1491. .vta_regs = B53_VTA_REGS,
  1492. .duplex_reg = B53_DUPLEX_STAT_GE,
  1493. .jumbo_pm_reg = B53_JUMBO_PORT_MASK,
  1494. .jumbo_size_reg = B53_JUMBO_MAX_SIZE,
  1495. },
  1496. {
  1497. .chip_id = BCM53011_DEVICE_ID,
  1498. .dev_name = "BCM53011",
  1499. .vlans = 4096,
  1500. .enabled_ports = 0x1bf,
  1501. .arl_entries = 4,
  1502. .cpu_port = B53_CPU_PORT_25, /* TODO: auto detect */
  1503. .vta_regs = B53_VTA_REGS,
  1504. .duplex_reg = B53_DUPLEX_STAT_GE,
  1505. .jumbo_pm_reg = B53_JUMBO_PORT_MASK,
  1506. .jumbo_size_reg = B53_JUMBO_MAX_SIZE,
  1507. },
  1508. {
  1509. .chip_id = BCM53012_DEVICE_ID,
  1510. .dev_name = "BCM53012",
  1511. .vlans = 4096,
  1512. .enabled_ports = 0x1bf,
  1513. .arl_entries = 4,
  1514. .cpu_port = B53_CPU_PORT_25, /* TODO: auto detect */
  1515. .vta_regs = B53_VTA_REGS,
  1516. .duplex_reg = B53_DUPLEX_STAT_GE,
  1517. .jumbo_pm_reg = B53_JUMBO_PORT_MASK,
  1518. .jumbo_size_reg = B53_JUMBO_MAX_SIZE,
  1519. },
  1520. {
  1521. .chip_id = BCM53018_DEVICE_ID,
  1522. .dev_name = "BCM53018",
  1523. .vlans = 4096,
  1524. .enabled_ports = 0x1f,
  1525. .arl_entries = 4,
  1526. .cpu_port = B53_CPU_PORT_25, /* TODO: auto detect */
  1527. .vta_regs = B53_VTA_REGS,
  1528. .duplex_reg = B53_DUPLEX_STAT_GE,
  1529. .jumbo_pm_reg = B53_JUMBO_PORT_MASK,
  1530. .jumbo_size_reg = B53_JUMBO_MAX_SIZE,
  1531. },
  1532. {
  1533. .chip_id = BCM53019_DEVICE_ID,
  1534. .dev_name = "BCM53019",
  1535. .vlans = 4096,
  1536. .enabled_ports = 0x1f,
  1537. .arl_entries = 4,
  1538. .cpu_port = B53_CPU_PORT_25, /* TODO: auto detect */
  1539. .vta_regs = B53_VTA_REGS,
  1540. .duplex_reg = B53_DUPLEX_STAT_GE,
  1541. .jumbo_pm_reg = B53_JUMBO_PORT_MASK,
  1542. .jumbo_size_reg = B53_JUMBO_MAX_SIZE,
  1543. },
  1544. {
  1545. .chip_id = BCM58XX_DEVICE_ID,
  1546. .dev_name = "BCM585xx/586xx/88312",
  1547. .vlans = 4096,
  1548. .enabled_ports = 0x1ff,
  1549. .arl_entries = 4,
  1550. .cpu_port = B53_CPU_PORT,
  1551. .vta_regs = B53_VTA_REGS,
  1552. .duplex_reg = B53_DUPLEX_STAT_GE,
  1553. .jumbo_pm_reg = B53_JUMBO_PORT_MASK,
  1554. .jumbo_size_reg = B53_JUMBO_MAX_SIZE,
  1555. },
  1556. {
  1557. .chip_id = BCM7445_DEVICE_ID,
  1558. .dev_name = "BCM7445",
  1559. .vlans = 4096,
  1560. .enabled_ports = 0x1ff,
  1561. .arl_entries = 4,
  1562. .cpu_port = B53_CPU_PORT,
  1563. .vta_regs = B53_VTA_REGS,
  1564. .duplex_reg = B53_DUPLEX_STAT_GE,
  1565. .jumbo_pm_reg = B53_JUMBO_PORT_MASK,
  1566. .jumbo_size_reg = B53_JUMBO_MAX_SIZE,
  1567. },
  1568. {
  1569. .chip_id = BCM7278_DEVICE_ID,
  1570. .dev_name = "BCM7278",
  1571. .vlans = 4096,
  1572. .enabled_ports = 0x1ff,
  1573. .arl_entries= 4,
  1574. .cpu_port = B53_CPU_PORT,
  1575. .vta_regs = B53_VTA_REGS,
  1576. .duplex_reg = B53_DUPLEX_STAT_GE,
  1577. .jumbo_pm_reg = B53_JUMBO_PORT_MASK,
  1578. .jumbo_size_reg = B53_JUMBO_MAX_SIZE,
  1579. },
  1580. };
  1581. static int b53_switch_init(struct b53_device *dev)
  1582. {
  1583. unsigned int i;
  1584. int ret;
  1585. for (i = 0; i < ARRAY_SIZE(b53_switch_chips); i++) {
  1586. const struct b53_chip_data *chip = &b53_switch_chips[i];
  1587. if (chip->chip_id == dev->chip_id) {
  1588. if (!dev->enabled_ports)
  1589. dev->enabled_ports = chip->enabled_ports;
  1590. dev->name = chip->dev_name;
  1591. dev->duplex_reg = chip->duplex_reg;
  1592. dev->vta_regs[0] = chip->vta_regs[0];
  1593. dev->vta_regs[1] = chip->vta_regs[1];
  1594. dev->vta_regs[2] = chip->vta_regs[2];
  1595. dev->jumbo_pm_reg = chip->jumbo_pm_reg;
  1596. dev->cpu_port = chip->cpu_port;
  1597. dev->num_vlans = chip->vlans;
  1598. dev->num_arl_entries = chip->arl_entries;
  1599. break;
  1600. }
  1601. }
  1602. /* check which BCM5325x version we have */
  1603. if (is5325(dev)) {
  1604. u8 vc4;
  1605. b53_read8(dev, B53_VLAN_PAGE, B53_VLAN_CTRL4_25, &vc4);
  1606. /* check reserved bits */
  1607. switch (vc4 & 3) {
  1608. case 1:
  1609. /* BCM5325E */
  1610. break;
  1611. case 3:
  1612. /* BCM5325F - do not use port 4 */
  1613. dev->enabled_ports &= ~BIT(4);
  1614. break;
  1615. default:
  1616. /* On the BCM47XX SoCs this is the supported internal switch.*/
  1617. #ifndef CONFIG_BCM47XX
  1618. /* BCM5325M */
  1619. return -EINVAL;
  1620. #else
  1621. break;
  1622. #endif
  1623. }
  1624. } else if (dev->chip_id == BCM53115_DEVICE_ID) {
  1625. u64 strap_value;
  1626. b53_read48(dev, B53_STAT_PAGE, B53_STRAP_VALUE, &strap_value);
  1627. /* use second IMP port if GMII is enabled */
  1628. if (strap_value & SV_GMII_CTRL_115)
  1629. dev->cpu_port = 5;
  1630. }
  1631. /* cpu port is always last */
  1632. dev->num_ports = dev->cpu_port + 1;
  1633. dev->enabled_ports |= BIT(dev->cpu_port);
  1634. dev->ports = devm_kzalloc(dev->dev,
  1635. sizeof(struct b53_port) * dev->num_ports,
  1636. GFP_KERNEL);
  1637. if (!dev->ports)
  1638. return -ENOMEM;
  1639. dev->vlans = devm_kzalloc(dev->dev,
  1640. sizeof(struct b53_vlan) * dev->num_vlans,
  1641. GFP_KERNEL);
  1642. if (!dev->vlans)
  1643. return -ENOMEM;
  1644. dev->reset_gpio = b53_switch_get_reset_gpio(dev);
  1645. if (dev->reset_gpio >= 0) {
  1646. ret = devm_gpio_request_one(dev->dev, dev->reset_gpio,
  1647. GPIOF_OUT_INIT_HIGH, "robo_reset");
  1648. if (ret)
  1649. return ret;
  1650. }
  1651. return 0;
  1652. }
  1653. struct b53_device *b53_switch_alloc(struct device *base,
  1654. const struct b53_io_ops *ops,
  1655. void *priv)
  1656. {
  1657. struct dsa_switch *ds;
  1658. struct b53_device *dev;
  1659. ds = dsa_switch_alloc(base, DSA_MAX_PORTS);
  1660. if (!ds)
  1661. return NULL;
  1662. dev = devm_kzalloc(base, sizeof(*dev), GFP_KERNEL);
  1663. if (!dev)
  1664. return NULL;
  1665. ds->priv = dev;
  1666. dev->dev = base;
  1667. dev->ds = ds;
  1668. dev->priv = priv;
  1669. dev->ops = ops;
  1670. ds->ops = &b53_switch_ops;
  1671. mutex_init(&dev->reg_mutex);
  1672. mutex_init(&dev->stats_mutex);
  1673. return dev;
  1674. }
  1675. EXPORT_SYMBOL(b53_switch_alloc);
  1676. int b53_switch_detect(struct b53_device *dev)
  1677. {
  1678. u32 id32;
  1679. u16 tmp;
  1680. u8 id8;
  1681. int ret;
  1682. ret = b53_read8(dev, B53_MGMT_PAGE, B53_DEVICE_ID, &id8);
  1683. if (ret)
  1684. return ret;
  1685. switch (id8) {
  1686. case 0:
  1687. /* BCM5325 and BCM5365 do not have this register so reads
  1688. * return 0. But the read operation did succeed, so assume this
  1689. * is one of them.
  1690. *
  1691. * Next check if we can write to the 5325's VTA register; for
  1692. * 5365 it is read only.
  1693. */
  1694. b53_write16(dev, B53_VLAN_PAGE, B53_VLAN_TABLE_ACCESS_25, 0xf);
  1695. b53_read16(dev, B53_VLAN_PAGE, B53_VLAN_TABLE_ACCESS_25, &tmp);
  1696. if (tmp == 0xf)
  1697. dev->chip_id = BCM5325_DEVICE_ID;
  1698. else
  1699. dev->chip_id = BCM5365_DEVICE_ID;
  1700. break;
  1701. case BCM5395_DEVICE_ID:
  1702. case BCM5397_DEVICE_ID:
  1703. case BCM5398_DEVICE_ID:
  1704. dev->chip_id = id8;
  1705. break;
  1706. default:
  1707. ret = b53_read32(dev, B53_MGMT_PAGE, B53_DEVICE_ID, &id32);
  1708. if (ret)
  1709. return ret;
  1710. switch (id32) {
  1711. case BCM53115_DEVICE_ID:
  1712. case BCM53125_DEVICE_ID:
  1713. case BCM53128_DEVICE_ID:
  1714. case BCM53010_DEVICE_ID:
  1715. case BCM53011_DEVICE_ID:
  1716. case BCM53012_DEVICE_ID:
  1717. case BCM53018_DEVICE_ID:
  1718. case BCM53019_DEVICE_ID:
  1719. dev->chip_id = id32;
  1720. break;
  1721. default:
  1722. pr_err("unsupported switch detected (BCM53%02x/BCM%x)\n",
  1723. id8, id32);
  1724. return -ENODEV;
  1725. }
  1726. }
  1727. if (dev->chip_id == BCM5325_DEVICE_ID)
  1728. return b53_read8(dev, B53_STAT_PAGE, B53_REV_ID_25,
  1729. &dev->core_rev);
  1730. else
  1731. return b53_read8(dev, B53_MGMT_PAGE, B53_REV_ID,
  1732. &dev->core_rev);
  1733. }
  1734. EXPORT_SYMBOL(b53_switch_detect);
  1735. int b53_switch_register(struct b53_device *dev)
  1736. {
  1737. int ret;
  1738. if (dev->pdata) {
  1739. dev->chip_id = dev->pdata->chip_id;
  1740. dev->enabled_ports = dev->pdata->enabled_ports;
  1741. }
  1742. if (!dev->chip_id && b53_switch_detect(dev))
  1743. return -EINVAL;
  1744. ret = b53_switch_init(dev);
  1745. if (ret)
  1746. return ret;
  1747. pr_info("found switch: %s, rev %i\n", dev->name, dev->core_rev);
  1748. return dsa_register_switch(dev->ds);
  1749. }
  1750. EXPORT_SYMBOL(b53_switch_register);
  1751. MODULE_AUTHOR("Jonas Gorski <jogo@openwrt.org>");
  1752. MODULE_DESCRIPTION("B53 switch library");
  1753. MODULE_LICENSE("Dual BSD/GPL");