b53_common.c 47 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901
  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 <net/switchdev.h>
  31. #include "b53_regs.h"
  32. #include "b53_priv.h"
  33. struct b53_mib_desc {
  34. u8 size;
  35. u8 offset;
  36. const char *name;
  37. };
  38. /* BCM5365 MIB counters */
  39. static const struct b53_mib_desc b53_mibs_65[] = {
  40. { 8, 0x00, "TxOctets" },
  41. { 4, 0x08, "TxDropPkts" },
  42. { 4, 0x10, "TxBroadcastPkts" },
  43. { 4, 0x14, "TxMulticastPkts" },
  44. { 4, 0x18, "TxUnicastPkts" },
  45. { 4, 0x1c, "TxCollisions" },
  46. { 4, 0x20, "TxSingleCollision" },
  47. { 4, 0x24, "TxMultipleCollision" },
  48. { 4, 0x28, "TxDeferredTransmit" },
  49. { 4, 0x2c, "TxLateCollision" },
  50. { 4, 0x30, "TxExcessiveCollision" },
  51. { 4, 0x38, "TxPausePkts" },
  52. { 8, 0x44, "RxOctets" },
  53. { 4, 0x4c, "RxUndersizePkts" },
  54. { 4, 0x50, "RxPausePkts" },
  55. { 4, 0x54, "Pkts64Octets" },
  56. { 4, 0x58, "Pkts65to127Octets" },
  57. { 4, 0x5c, "Pkts128to255Octets" },
  58. { 4, 0x60, "Pkts256to511Octets" },
  59. { 4, 0x64, "Pkts512to1023Octets" },
  60. { 4, 0x68, "Pkts1024to1522Octets" },
  61. { 4, 0x6c, "RxOversizePkts" },
  62. { 4, 0x70, "RxJabbers" },
  63. { 4, 0x74, "RxAlignmentErrors" },
  64. { 4, 0x78, "RxFCSErrors" },
  65. { 8, 0x7c, "RxGoodOctets" },
  66. { 4, 0x84, "RxDropPkts" },
  67. { 4, 0x88, "RxUnicastPkts" },
  68. { 4, 0x8c, "RxMulticastPkts" },
  69. { 4, 0x90, "RxBroadcastPkts" },
  70. { 4, 0x94, "RxSAChanges" },
  71. { 4, 0x98, "RxFragments" },
  72. };
  73. #define B53_MIBS_65_SIZE ARRAY_SIZE(b53_mibs_65)
  74. /* BCM63xx MIB counters */
  75. static const struct b53_mib_desc b53_mibs_63xx[] = {
  76. { 8, 0x00, "TxOctets" },
  77. { 4, 0x08, "TxDropPkts" },
  78. { 4, 0x0c, "TxQoSPkts" },
  79. { 4, 0x10, "TxBroadcastPkts" },
  80. { 4, 0x14, "TxMulticastPkts" },
  81. { 4, 0x18, "TxUnicastPkts" },
  82. { 4, 0x1c, "TxCollisions" },
  83. { 4, 0x20, "TxSingleCollision" },
  84. { 4, 0x24, "TxMultipleCollision" },
  85. { 4, 0x28, "TxDeferredTransmit" },
  86. { 4, 0x2c, "TxLateCollision" },
  87. { 4, 0x30, "TxExcessiveCollision" },
  88. { 4, 0x38, "TxPausePkts" },
  89. { 8, 0x3c, "TxQoSOctets" },
  90. { 8, 0x44, "RxOctets" },
  91. { 4, 0x4c, "RxUndersizePkts" },
  92. { 4, 0x50, "RxPausePkts" },
  93. { 4, 0x54, "Pkts64Octets" },
  94. { 4, 0x58, "Pkts65to127Octets" },
  95. { 4, 0x5c, "Pkts128to255Octets" },
  96. { 4, 0x60, "Pkts256to511Octets" },
  97. { 4, 0x64, "Pkts512to1023Octets" },
  98. { 4, 0x68, "Pkts1024to1522Octets" },
  99. { 4, 0x6c, "RxOversizePkts" },
  100. { 4, 0x70, "RxJabbers" },
  101. { 4, 0x74, "RxAlignmentErrors" },
  102. { 4, 0x78, "RxFCSErrors" },
  103. { 8, 0x7c, "RxGoodOctets" },
  104. { 4, 0x84, "RxDropPkts" },
  105. { 4, 0x88, "RxUnicastPkts" },
  106. { 4, 0x8c, "RxMulticastPkts" },
  107. { 4, 0x90, "RxBroadcastPkts" },
  108. { 4, 0x94, "RxSAChanges" },
  109. { 4, 0x98, "RxFragments" },
  110. { 4, 0xa0, "RxSymbolErrors" },
  111. { 4, 0xa4, "RxQoSPkts" },
  112. { 8, 0xa8, "RxQoSOctets" },
  113. { 4, 0xb0, "Pkts1523to2047Octets" },
  114. { 4, 0xb4, "Pkts2048to4095Octets" },
  115. { 4, 0xb8, "Pkts4096to8191Octets" },
  116. { 4, 0xbc, "Pkts8192to9728Octets" },
  117. { 4, 0xc0, "RxDiscarded" },
  118. };
  119. #define B53_MIBS_63XX_SIZE ARRAY_SIZE(b53_mibs_63xx)
  120. /* MIB counters */
  121. static const struct b53_mib_desc b53_mibs[] = {
  122. { 8, 0x00, "TxOctets" },
  123. { 4, 0x08, "TxDropPkts" },
  124. { 4, 0x10, "TxBroadcastPkts" },
  125. { 4, 0x14, "TxMulticastPkts" },
  126. { 4, 0x18, "TxUnicastPkts" },
  127. { 4, 0x1c, "TxCollisions" },
  128. { 4, 0x20, "TxSingleCollision" },
  129. { 4, 0x24, "TxMultipleCollision" },
  130. { 4, 0x28, "TxDeferredTransmit" },
  131. { 4, 0x2c, "TxLateCollision" },
  132. { 4, 0x30, "TxExcessiveCollision" },
  133. { 4, 0x38, "TxPausePkts" },
  134. { 8, 0x50, "RxOctets" },
  135. { 4, 0x58, "RxUndersizePkts" },
  136. { 4, 0x5c, "RxPausePkts" },
  137. { 4, 0x60, "Pkts64Octets" },
  138. { 4, 0x64, "Pkts65to127Octets" },
  139. { 4, 0x68, "Pkts128to255Octets" },
  140. { 4, 0x6c, "Pkts256to511Octets" },
  141. { 4, 0x70, "Pkts512to1023Octets" },
  142. { 4, 0x74, "Pkts1024to1522Octets" },
  143. { 4, 0x78, "RxOversizePkts" },
  144. { 4, 0x7c, "RxJabbers" },
  145. { 4, 0x80, "RxAlignmentErrors" },
  146. { 4, 0x84, "RxFCSErrors" },
  147. { 8, 0x88, "RxGoodOctets" },
  148. { 4, 0x90, "RxDropPkts" },
  149. { 4, 0x94, "RxUnicastPkts" },
  150. { 4, 0x98, "RxMulticastPkts" },
  151. { 4, 0x9c, "RxBroadcastPkts" },
  152. { 4, 0xa0, "RxSAChanges" },
  153. { 4, 0xa4, "RxFragments" },
  154. { 4, 0xa8, "RxJumboPkts" },
  155. { 4, 0xac, "RxSymbolErrors" },
  156. { 4, 0xc0, "RxDiscarded" },
  157. };
  158. #define B53_MIBS_SIZE ARRAY_SIZE(b53_mibs)
  159. static const struct b53_mib_desc b53_mibs_58xx[] = {
  160. { 8, 0x00, "TxOctets" },
  161. { 4, 0x08, "TxDropPkts" },
  162. { 4, 0x0c, "TxQPKTQ0" },
  163. { 4, 0x10, "TxBroadcastPkts" },
  164. { 4, 0x14, "TxMulticastPkts" },
  165. { 4, 0x18, "TxUnicastPKts" },
  166. { 4, 0x1c, "TxCollisions" },
  167. { 4, 0x20, "TxSingleCollision" },
  168. { 4, 0x24, "TxMultipleCollision" },
  169. { 4, 0x28, "TxDeferredCollision" },
  170. { 4, 0x2c, "TxLateCollision" },
  171. { 4, 0x30, "TxExcessiveCollision" },
  172. { 4, 0x34, "TxFrameInDisc" },
  173. { 4, 0x38, "TxPausePkts" },
  174. { 4, 0x3c, "TxQPKTQ1" },
  175. { 4, 0x40, "TxQPKTQ2" },
  176. { 4, 0x44, "TxQPKTQ3" },
  177. { 4, 0x48, "TxQPKTQ4" },
  178. { 4, 0x4c, "TxQPKTQ5" },
  179. { 8, 0x50, "RxOctets" },
  180. { 4, 0x58, "RxUndersizePkts" },
  181. { 4, 0x5c, "RxPausePkts" },
  182. { 4, 0x60, "RxPkts64Octets" },
  183. { 4, 0x64, "RxPkts65to127Octets" },
  184. { 4, 0x68, "RxPkts128to255Octets" },
  185. { 4, 0x6c, "RxPkts256to511Octets" },
  186. { 4, 0x70, "RxPkts512to1023Octets" },
  187. { 4, 0x74, "RxPkts1024toMaxPktsOctets" },
  188. { 4, 0x78, "RxOversizePkts" },
  189. { 4, 0x7c, "RxJabbers" },
  190. { 4, 0x80, "RxAlignmentErrors" },
  191. { 4, 0x84, "RxFCSErrors" },
  192. { 8, 0x88, "RxGoodOctets" },
  193. { 4, 0x90, "RxDropPkts" },
  194. { 4, 0x94, "RxUnicastPkts" },
  195. { 4, 0x98, "RxMulticastPkts" },
  196. { 4, 0x9c, "RxBroadcastPkts" },
  197. { 4, 0xa0, "RxSAChanges" },
  198. { 4, 0xa4, "RxFragments" },
  199. { 4, 0xa8, "RxJumboPkt" },
  200. { 4, 0xac, "RxSymblErr" },
  201. { 4, 0xb0, "InRangeErrCount" },
  202. { 4, 0xb4, "OutRangeErrCount" },
  203. { 4, 0xb8, "EEELpiEvent" },
  204. { 4, 0xbc, "EEELpiDuration" },
  205. { 4, 0xc0, "RxDiscard" },
  206. { 4, 0xc8, "TxQPKTQ6" },
  207. { 4, 0xcc, "TxQPKTQ7" },
  208. { 4, 0xd0, "TxPkts64Octets" },
  209. { 4, 0xd4, "TxPkts65to127Octets" },
  210. { 4, 0xd8, "TxPkts128to255Octets" },
  211. { 4, 0xdc, "TxPkts256to511Ocets" },
  212. { 4, 0xe0, "TxPkts512to1023Ocets" },
  213. { 4, 0xe4, "TxPkts1024toMaxPktOcets" },
  214. };
  215. #define B53_MIBS_58XX_SIZE ARRAY_SIZE(b53_mibs_58xx)
  216. static int b53_do_vlan_op(struct b53_device *dev, u8 op)
  217. {
  218. unsigned int i;
  219. b53_write8(dev, B53_ARLIO_PAGE, dev->vta_regs[0], VTA_START_CMD | op);
  220. for (i = 0; i < 10; i++) {
  221. u8 vta;
  222. b53_read8(dev, B53_ARLIO_PAGE, dev->vta_regs[0], &vta);
  223. if (!(vta & VTA_START_CMD))
  224. return 0;
  225. usleep_range(100, 200);
  226. }
  227. return -EIO;
  228. }
  229. static void b53_set_vlan_entry(struct b53_device *dev, u16 vid,
  230. struct b53_vlan *vlan)
  231. {
  232. if (is5325(dev)) {
  233. u32 entry = 0;
  234. if (vlan->members) {
  235. entry = ((vlan->untag & VA_UNTAG_MASK_25) <<
  236. VA_UNTAG_S_25) | vlan->members;
  237. if (dev->core_rev >= 3)
  238. entry |= VA_VALID_25_R4 | vid << VA_VID_HIGH_S;
  239. else
  240. entry |= VA_VALID_25;
  241. }
  242. b53_write32(dev, B53_VLAN_PAGE, B53_VLAN_WRITE_25, entry);
  243. b53_write16(dev, B53_VLAN_PAGE, B53_VLAN_TABLE_ACCESS_25, vid |
  244. VTA_RW_STATE_WR | VTA_RW_OP_EN);
  245. } else if (is5365(dev)) {
  246. u16 entry = 0;
  247. if (vlan->members)
  248. entry = ((vlan->untag & VA_UNTAG_MASK_65) <<
  249. VA_UNTAG_S_65) | vlan->members | VA_VALID_65;
  250. b53_write16(dev, B53_VLAN_PAGE, B53_VLAN_WRITE_65, entry);
  251. b53_write16(dev, B53_VLAN_PAGE, B53_VLAN_TABLE_ACCESS_65, vid |
  252. VTA_RW_STATE_WR | VTA_RW_OP_EN);
  253. } else {
  254. b53_write16(dev, B53_ARLIO_PAGE, dev->vta_regs[1], vid);
  255. b53_write32(dev, B53_ARLIO_PAGE, dev->vta_regs[2],
  256. (vlan->untag << VTE_UNTAG_S) | vlan->members);
  257. b53_do_vlan_op(dev, VTA_CMD_WRITE);
  258. }
  259. dev_dbg(dev->ds->dev, "VID: %d, members: 0x%04x, untag: 0x%04x\n",
  260. vid, vlan->members, vlan->untag);
  261. }
  262. static void b53_get_vlan_entry(struct b53_device *dev, u16 vid,
  263. struct b53_vlan *vlan)
  264. {
  265. if (is5325(dev)) {
  266. u32 entry = 0;
  267. b53_write16(dev, B53_VLAN_PAGE, B53_VLAN_TABLE_ACCESS_25, vid |
  268. VTA_RW_STATE_RD | VTA_RW_OP_EN);
  269. b53_read32(dev, B53_VLAN_PAGE, B53_VLAN_WRITE_25, &entry);
  270. if (dev->core_rev >= 3)
  271. vlan->valid = !!(entry & VA_VALID_25_R4);
  272. else
  273. vlan->valid = !!(entry & VA_VALID_25);
  274. vlan->members = entry & VA_MEMBER_MASK;
  275. vlan->untag = (entry >> VA_UNTAG_S_25) & VA_UNTAG_MASK_25;
  276. } else if (is5365(dev)) {
  277. u16 entry = 0;
  278. b53_write16(dev, B53_VLAN_PAGE, B53_VLAN_TABLE_ACCESS_65, vid |
  279. VTA_RW_STATE_WR | VTA_RW_OP_EN);
  280. b53_read16(dev, B53_VLAN_PAGE, B53_VLAN_WRITE_65, &entry);
  281. vlan->valid = !!(entry & VA_VALID_65);
  282. vlan->members = entry & VA_MEMBER_MASK;
  283. vlan->untag = (entry >> VA_UNTAG_S_65) & VA_UNTAG_MASK_65;
  284. } else {
  285. u32 entry = 0;
  286. b53_write16(dev, B53_ARLIO_PAGE, dev->vta_regs[1], vid);
  287. b53_do_vlan_op(dev, VTA_CMD_READ);
  288. b53_read32(dev, B53_ARLIO_PAGE, dev->vta_regs[2], &entry);
  289. vlan->members = entry & VTE_MEMBERS;
  290. vlan->untag = (entry >> VTE_UNTAG_S) & VTE_MEMBERS;
  291. vlan->valid = true;
  292. }
  293. }
  294. static void b53_set_forwarding(struct b53_device *dev, int enable)
  295. {
  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. }
  304. static void b53_enable_vlan(struct b53_device *dev, bool enable)
  305. {
  306. u8 mgmt, vc0, vc1, vc4 = 0, vc5;
  307. b53_read8(dev, B53_CTRL_PAGE, B53_SWITCH_MODE, &mgmt);
  308. b53_read8(dev, B53_VLAN_PAGE, B53_VLAN_CTRL0, &vc0);
  309. b53_read8(dev, B53_VLAN_PAGE, B53_VLAN_CTRL1, &vc1);
  310. if (is5325(dev) || is5365(dev)) {
  311. b53_read8(dev, B53_VLAN_PAGE, B53_VLAN_CTRL4_25, &vc4);
  312. b53_read8(dev, B53_VLAN_PAGE, B53_VLAN_CTRL5_25, &vc5);
  313. } else if (is63xx(dev)) {
  314. b53_read8(dev, B53_VLAN_PAGE, B53_VLAN_CTRL4_63XX, &vc4);
  315. b53_read8(dev, B53_VLAN_PAGE, B53_VLAN_CTRL5_63XX, &vc5);
  316. } else {
  317. b53_read8(dev, B53_VLAN_PAGE, B53_VLAN_CTRL4, &vc4);
  318. b53_read8(dev, B53_VLAN_PAGE, B53_VLAN_CTRL5, &vc5);
  319. }
  320. mgmt &= ~SM_SW_FWD_MODE;
  321. if (enable) {
  322. vc0 |= VC0_VLAN_EN | VC0_VID_CHK_EN | VC0_VID_HASH_VID;
  323. vc1 |= VC1_RX_MCST_UNTAG_EN | VC1_RX_MCST_FWD_EN;
  324. vc4 &= ~VC4_ING_VID_CHECK_MASK;
  325. vc4 |= VC4_ING_VID_VIO_DROP << VC4_ING_VID_CHECK_S;
  326. vc5 |= VC5_DROP_VTABLE_MISS;
  327. if (is5325(dev))
  328. vc0 &= ~VC0_RESERVED_1;
  329. if (is5325(dev) || is5365(dev))
  330. vc1 |= VC1_RX_MCST_TAG_EN;
  331. } else {
  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. vc5 &= ~VC5_DROP_VTABLE_MISS;
  336. if (is5325(dev) || is5365(dev))
  337. vc4 |= VC4_ING_VID_VIO_FWD << VC4_ING_VID_CHECK_S;
  338. else
  339. vc4 |= VC4_ING_VID_VIO_TO_IMP << VC4_ING_VID_CHECK_S;
  340. if (is5325(dev) || is5365(dev))
  341. vc1 &= ~VC1_RX_MCST_TAG_EN;
  342. }
  343. if (!is5325(dev) && !is5365(dev))
  344. vc5 &= ~VC5_VID_FFF_EN;
  345. b53_write8(dev, B53_VLAN_PAGE, B53_VLAN_CTRL0, vc0);
  346. b53_write8(dev, B53_VLAN_PAGE, B53_VLAN_CTRL1, vc1);
  347. if (is5325(dev) || is5365(dev)) {
  348. /* enable the high 8 bit vid check on 5325 */
  349. if (is5325(dev) && enable)
  350. b53_write8(dev, B53_VLAN_PAGE, B53_VLAN_CTRL3,
  351. VC3_HIGH_8BIT_EN);
  352. else
  353. b53_write8(dev, B53_VLAN_PAGE, B53_VLAN_CTRL3, 0);
  354. b53_write8(dev, B53_VLAN_PAGE, B53_VLAN_CTRL4_25, vc4);
  355. b53_write8(dev, B53_VLAN_PAGE, B53_VLAN_CTRL5_25, vc5);
  356. } else if (is63xx(dev)) {
  357. b53_write16(dev, B53_VLAN_PAGE, B53_VLAN_CTRL3_63XX, 0);
  358. b53_write8(dev, B53_VLAN_PAGE, B53_VLAN_CTRL4_63XX, vc4);
  359. b53_write8(dev, B53_VLAN_PAGE, B53_VLAN_CTRL5_63XX, vc5);
  360. } else {
  361. b53_write16(dev, B53_VLAN_PAGE, B53_VLAN_CTRL3, 0);
  362. b53_write8(dev, B53_VLAN_PAGE, B53_VLAN_CTRL4, vc4);
  363. b53_write8(dev, B53_VLAN_PAGE, B53_VLAN_CTRL5, vc5);
  364. }
  365. b53_write8(dev, B53_CTRL_PAGE, B53_SWITCH_MODE, mgmt);
  366. }
  367. static int b53_set_jumbo(struct b53_device *dev, bool enable, bool allow_10_100)
  368. {
  369. u32 port_mask = 0;
  370. u16 max_size = JMS_MIN_SIZE;
  371. if (is5325(dev) || is5365(dev))
  372. return -EINVAL;
  373. if (enable) {
  374. port_mask = dev->enabled_ports;
  375. max_size = JMS_MAX_SIZE;
  376. if (allow_10_100)
  377. port_mask |= JPM_10_100_JUMBO_EN;
  378. }
  379. b53_write32(dev, B53_JUMBO_PAGE, dev->jumbo_pm_reg, port_mask);
  380. return b53_write16(dev, B53_JUMBO_PAGE, dev->jumbo_size_reg, max_size);
  381. }
  382. static int b53_flush_arl(struct b53_device *dev, u8 mask)
  383. {
  384. unsigned int i;
  385. b53_write8(dev, B53_CTRL_PAGE, B53_FAST_AGE_CTRL,
  386. FAST_AGE_DONE | FAST_AGE_DYNAMIC | mask);
  387. for (i = 0; i < 10; i++) {
  388. u8 fast_age_ctrl;
  389. b53_read8(dev, B53_CTRL_PAGE, B53_FAST_AGE_CTRL,
  390. &fast_age_ctrl);
  391. if (!(fast_age_ctrl & FAST_AGE_DONE))
  392. goto out;
  393. msleep(1);
  394. }
  395. return -ETIMEDOUT;
  396. out:
  397. /* Only age dynamic entries (default behavior) */
  398. b53_write8(dev, B53_CTRL_PAGE, B53_FAST_AGE_CTRL, FAST_AGE_DYNAMIC);
  399. return 0;
  400. }
  401. static int b53_fast_age_port(struct b53_device *dev, int port)
  402. {
  403. b53_write8(dev, B53_CTRL_PAGE, B53_FAST_AGE_PORT_CTRL, port);
  404. return b53_flush_arl(dev, FAST_AGE_PORT);
  405. }
  406. static int b53_fast_age_vlan(struct b53_device *dev, u16 vid)
  407. {
  408. b53_write16(dev, B53_CTRL_PAGE, B53_FAST_AGE_VID_CTRL, vid);
  409. return b53_flush_arl(dev, FAST_AGE_VLAN);
  410. }
  411. static void b53_imp_vlan_setup(struct dsa_switch *ds, int cpu_port)
  412. {
  413. struct b53_device *dev = ds->priv;
  414. unsigned int i;
  415. u16 pvlan;
  416. /* Enable the IMP port to be in the same VLAN as the other ports
  417. * on a per-port basis such that we only have Port i and IMP in
  418. * the same VLAN.
  419. */
  420. b53_for_each_port(dev, i) {
  421. b53_read16(dev, B53_PVLAN_PAGE, B53_PVLAN_PORT_MASK(i), &pvlan);
  422. pvlan |= BIT(cpu_port);
  423. b53_write16(dev, B53_PVLAN_PAGE, B53_PVLAN_PORT_MASK(i), pvlan);
  424. }
  425. }
  426. static int b53_enable_port(struct dsa_switch *ds, int port,
  427. struct phy_device *phy)
  428. {
  429. struct b53_device *dev = ds->priv;
  430. unsigned int cpu_port = dev->cpu_port;
  431. u16 pvlan;
  432. /* Clear the Rx and Tx disable bits and set to no spanning tree */
  433. b53_write8(dev, B53_CTRL_PAGE, B53_PORT_CTRL(port), 0);
  434. /* Set this port, and only this one to be in the default VLAN,
  435. * if member of a bridge, restore its membership prior to
  436. * bringing down this port.
  437. */
  438. b53_read16(dev, B53_PVLAN_PAGE, B53_PVLAN_PORT_MASK(port), &pvlan);
  439. pvlan &= ~0x1ff;
  440. pvlan |= BIT(port);
  441. pvlan |= dev->ports[port].vlan_ctl_mask;
  442. b53_write16(dev, B53_PVLAN_PAGE, B53_PVLAN_PORT_MASK(port), pvlan);
  443. b53_imp_vlan_setup(ds, cpu_port);
  444. return 0;
  445. }
  446. static void b53_disable_port(struct dsa_switch *ds, int port,
  447. struct phy_device *phy)
  448. {
  449. struct b53_device *dev = ds->priv;
  450. u8 reg;
  451. /* Disable Tx/Rx for the port */
  452. b53_read8(dev, B53_CTRL_PAGE, B53_PORT_CTRL(port), &reg);
  453. reg |= PORT_CTRL_RX_DISABLE | PORT_CTRL_TX_DISABLE;
  454. b53_write8(dev, B53_CTRL_PAGE, B53_PORT_CTRL(port), reg);
  455. }
  456. static void b53_enable_cpu_port(struct b53_device *dev)
  457. {
  458. unsigned int cpu_port = dev->cpu_port;
  459. u8 port_ctrl;
  460. /* BCM5325 CPU port is at 8 */
  461. if ((is5325(dev) || is5365(dev)) && cpu_port == B53_CPU_PORT_25)
  462. cpu_port = B53_CPU_PORT;
  463. port_ctrl = PORT_CTRL_RX_BCST_EN |
  464. PORT_CTRL_RX_MCST_EN |
  465. PORT_CTRL_RX_UCST_EN;
  466. b53_write8(dev, B53_CTRL_PAGE, B53_PORT_CTRL(cpu_port), port_ctrl);
  467. }
  468. static void b53_enable_mib(struct b53_device *dev)
  469. {
  470. u8 gc;
  471. b53_read8(dev, B53_MGMT_PAGE, B53_GLOBAL_CONFIG, &gc);
  472. gc &= ~(GC_RESET_MIB | GC_MIB_AC_EN);
  473. b53_write8(dev, B53_MGMT_PAGE, B53_GLOBAL_CONFIG, gc);
  474. }
  475. static int b53_configure_vlan(struct b53_device *dev)
  476. {
  477. struct b53_vlan vl = { 0 };
  478. int i;
  479. /* clear all vlan entries */
  480. if (is5325(dev) || is5365(dev)) {
  481. for (i = 1; i < dev->num_vlans; i++)
  482. b53_set_vlan_entry(dev, i, &vl);
  483. } else {
  484. b53_do_vlan_op(dev, VTA_CMD_CLEAR);
  485. }
  486. b53_enable_vlan(dev, false);
  487. b53_for_each_port(dev, i)
  488. b53_write16(dev, B53_VLAN_PAGE,
  489. B53_VLAN_PORT_DEF_TAG(i), 1);
  490. if (!is5325(dev) && !is5365(dev))
  491. b53_set_jumbo(dev, dev->enable_jumbo, false);
  492. return 0;
  493. }
  494. static void b53_switch_reset_gpio(struct b53_device *dev)
  495. {
  496. int gpio = dev->reset_gpio;
  497. if (gpio < 0)
  498. return;
  499. /* Reset sequence: RESET low(50ms)->high(20ms)
  500. */
  501. gpio_set_value(gpio, 0);
  502. mdelay(50);
  503. gpio_set_value(gpio, 1);
  504. mdelay(20);
  505. dev->current_page = 0xff;
  506. }
  507. static int b53_switch_reset(struct b53_device *dev)
  508. {
  509. u8 mgmt;
  510. b53_switch_reset_gpio(dev);
  511. if (is539x(dev)) {
  512. b53_write8(dev, B53_CTRL_PAGE, B53_SOFTRESET, 0x83);
  513. b53_write8(dev, B53_CTRL_PAGE, B53_SOFTRESET, 0x00);
  514. }
  515. b53_read8(dev, B53_CTRL_PAGE, B53_SWITCH_MODE, &mgmt);
  516. if (!(mgmt & SM_SW_FWD_EN)) {
  517. mgmt &= ~SM_SW_FWD_MODE;
  518. mgmt |= SM_SW_FWD_EN;
  519. b53_write8(dev, B53_CTRL_PAGE, B53_SWITCH_MODE, mgmt);
  520. b53_read8(dev, B53_CTRL_PAGE, B53_SWITCH_MODE, &mgmt);
  521. if (!(mgmt & SM_SW_FWD_EN)) {
  522. dev_err(dev->dev, "Failed to enable switch!\n");
  523. return -EINVAL;
  524. }
  525. }
  526. b53_enable_mib(dev);
  527. return b53_flush_arl(dev, FAST_AGE_STATIC);
  528. }
  529. static int b53_phy_read16(struct dsa_switch *ds, int addr, int reg)
  530. {
  531. struct b53_device *priv = ds->priv;
  532. u16 value = 0;
  533. int ret;
  534. if (priv->ops->phy_read16)
  535. ret = priv->ops->phy_read16(priv, addr, reg, &value);
  536. else
  537. ret = b53_read16(priv, B53_PORT_MII_PAGE(addr),
  538. reg * 2, &value);
  539. return ret ? ret : value;
  540. }
  541. static int b53_phy_write16(struct dsa_switch *ds, int addr, int reg, u16 val)
  542. {
  543. struct b53_device *priv = ds->priv;
  544. if (priv->ops->phy_write16)
  545. return priv->ops->phy_write16(priv, addr, reg, val);
  546. return b53_write16(priv, B53_PORT_MII_PAGE(addr), reg * 2, val);
  547. }
  548. static int b53_reset_switch(struct b53_device *priv)
  549. {
  550. /* reset vlans */
  551. priv->enable_jumbo = false;
  552. memset(priv->vlans, 0, sizeof(*priv->vlans) * priv->num_vlans);
  553. memset(priv->ports, 0, sizeof(*priv->ports) * priv->num_ports);
  554. return b53_switch_reset(priv);
  555. }
  556. static int b53_apply_config(struct b53_device *priv)
  557. {
  558. /* disable switching */
  559. b53_set_forwarding(priv, 0);
  560. b53_configure_vlan(priv);
  561. /* enable switching */
  562. b53_set_forwarding(priv, 1);
  563. return 0;
  564. }
  565. static void b53_reset_mib(struct b53_device *priv)
  566. {
  567. u8 gc;
  568. b53_read8(priv, B53_MGMT_PAGE, B53_GLOBAL_CONFIG, &gc);
  569. b53_write8(priv, B53_MGMT_PAGE, B53_GLOBAL_CONFIG, gc | GC_RESET_MIB);
  570. msleep(1);
  571. b53_write8(priv, B53_MGMT_PAGE, B53_GLOBAL_CONFIG, gc & ~GC_RESET_MIB);
  572. msleep(1);
  573. }
  574. static const struct b53_mib_desc *b53_get_mib(struct b53_device *dev)
  575. {
  576. if (is5365(dev))
  577. return b53_mibs_65;
  578. else if (is63xx(dev))
  579. return b53_mibs_63xx;
  580. else if (is58xx(dev))
  581. return b53_mibs_58xx;
  582. else
  583. return b53_mibs;
  584. }
  585. static unsigned int b53_get_mib_size(struct b53_device *dev)
  586. {
  587. if (is5365(dev))
  588. return B53_MIBS_65_SIZE;
  589. else if (is63xx(dev))
  590. return B53_MIBS_63XX_SIZE;
  591. else if (is58xx(dev))
  592. return B53_MIBS_58XX_SIZE;
  593. else
  594. return B53_MIBS_SIZE;
  595. }
  596. static void b53_get_strings(struct dsa_switch *ds, int port, uint8_t *data)
  597. {
  598. struct b53_device *dev = ds->priv;
  599. const struct b53_mib_desc *mibs = b53_get_mib(dev);
  600. unsigned int mib_size = b53_get_mib_size(dev);
  601. unsigned int i;
  602. for (i = 0; i < mib_size; i++)
  603. memcpy(data + i * ETH_GSTRING_LEN,
  604. mibs[i].name, ETH_GSTRING_LEN);
  605. }
  606. static void b53_get_ethtool_stats(struct dsa_switch *ds, int port,
  607. uint64_t *data)
  608. {
  609. struct b53_device *dev = ds->priv;
  610. const struct b53_mib_desc *mibs = b53_get_mib(dev);
  611. unsigned int mib_size = b53_get_mib_size(dev);
  612. const struct b53_mib_desc *s;
  613. unsigned int i;
  614. u64 val = 0;
  615. if (is5365(dev) && port == 5)
  616. port = 8;
  617. mutex_lock(&dev->stats_mutex);
  618. for (i = 0; i < mib_size; i++) {
  619. s = &mibs[i];
  620. if (s->size == 8) {
  621. b53_read64(dev, B53_MIB_PAGE(port), s->offset, &val);
  622. } else {
  623. u32 val32;
  624. b53_read32(dev, B53_MIB_PAGE(port), s->offset,
  625. &val32);
  626. val = val32;
  627. }
  628. data[i] = (u64)val;
  629. }
  630. mutex_unlock(&dev->stats_mutex);
  631. }
  632. static int b53_get_sset_count(struct dsa_switch *ds)
  633. {
  634. struct b53_device *dev = ds->priv;
  635. return b53_get_mib_size(dev);
  636. }
  637. static int b53_set_addr(struct dsa_switch *ds, u8 *addr)
  638. {
  639. return 0;
  640. }
  641. static int b53_setup(struct dsa_switch *ds)
  642. {
  643. struct b53_device *dev = ds->priv;
  644. unsigned int port;
  645. int ret;
  646. ret = b53_reset_switch(dev);
  647. if (ret) {
  648. dev_err(ds->dev, "failed to reset switch\n");
  649. return ret;
  650. }
  651. b53_reset_mib(dev);
  652. ret = b53_apply_config(dev);
  653. if (ret)
  654. dev_err(ds->dev, "failed to apply configuration\n");
  655. for (port = 0; port < dev->num_ports; port++) {
  656. if (BIT(port) & ds->enabled_port_mask)
  657. b53_enable_port(ds, port, NULL);
  658. else if (dsa_is_cpu_port(ds, port))
  659. b53_enable_cpu_port(dev);
  660. else
  661. b53_disable_port(ds, port, NULL);
  662. }
  663. return ret;
  664. }
  665. static void b53_adjust_link(struct dsa_switch *ds, int port,
  666. struct phy_device *phydev)
  667. {
  668. struct b53_device *dev = ds->priv;
  669. u8 rgmii_ctrl = 0, reg = 0, off;
  670. if (!phy_is_pseudo_fixed_link(phydev))
  671. return;
  672. /* Override the port settings */
  673. if (port == dev->cpu_port) {
  674. off = B53_PORT_OVERRIDE_CTRL;
  675. reg = PORT_OVERRIDE_EN;
  676. } else {
  677. off = B53_GMII_PORT_OVERRIDE_CTRL(port);
  678. reg = GMII_PO_EN;
  679. }
  680. /* Set the link UP */
  681. if (phydev->link)
  682. reg |= PORT_OVERRIDE_LINK;
  683. if (phydev->duplex == DUPLEX_FULL)
  684. reg |= PORT_OVERRIDE_FULL_DUPLEX;
  685. switch (phydev->speed) {
  686. case 2000:
  687. reg |= PORT_OVERRIDE_SPEED_2000M;
  688. /* fallthrough */
  689. case SPEED_1000:
  690. reg |= PORT_OVERRIDE_SPEED_1000M;
  691. break;
  692. case SPEED_100:
  693. reg |= PORT_OVERRIDE_SPEED_100M;
  694. break;
  695. case SPEED_10:
  696. reg |= PORT_OVERRIDE_SPEED_10M;
  697. break;
  698. default:
  699. dev_err(ds->dev, "unknown speed: %d\n", phydev->speed);
  700. return;
  701. }
  702. /* Enable flow control on BCM5301x's CPU port */
  703. if (is5301x(dev) && port == dev->cpu_port)
  704. reg |= PORT_OVERRIDE_RX_FLOW | PORT_OVERRIDE_TX_FLOW;
  705. if (phydev->pause) {
  706. if (phydev->asym_pause)
  707. reg |= PORT_OVERRIDE_TX_FLOW;
  708. reg |= PORT_OVERRIDE_RX_FLOW;
  709. }
  710. b53_write8(dev, B53_CTRL_PAGE, off, reg);
  711. if (is531x5(dev) && phy_interface_is_rgmii(phydev)) {
  712. if (port == 8)
  713. off = B53_RGMII_CTRL_IMP;
  714. else
  715. off = B53_RGMII_CTRL_P(port);
  716. /* Configure the port RGMII clock delay by DLL disabled and
  717. * tx_clk aligned timing (restoring to reset defaults)
  718. */
  719. b53_read8(dev, B53_CTRL_PAGE, off, &rgmii_ctrl);
  720. rgmii_ctrl &= ~(RGMII_CTRL_DLL_RXC | RGMII_CTRL_DLL_TXC |
  721. RGMII_CTRL_TIMING_SEL);
  722. /* PHY_INTERFACE_MODE_RGMII_TXID means TX internal delay, make
  723. * sure that we enable the port TX clock internal delay to
  724. * account for this internal delay that is inserted, otherwise
  725. * the switch won't be able to receive correctly.
  726. *
  727. * PHY_INTERFACE_MODE_RGMII means that we are not introducing
  728. * any delay neither on transmission nor reception, so the
  729. * BCM53125 must also be configured accordingly to account for
  730. * the lack of delay and introduce
  731. *
  732. * The BCM53125 switch has its RX clock and TX clock control
  733. * swapped, hence the reason why we modify the TX clock path in
  734. * the "RGMII" case
  735. */
  736. if (phydev->interface == PHY_INTERFACE_MODE_RGMII_TXID)
  737. rgmii_ctrl |= RGMII_CTRL_DLL_TXC;
  738. if (phydev->interface == PHY_INTERFACE_MODE_RGMII)
  739. rgmii_ctrl |= RGMII_CTRL_DLL_TXC | RGMII_CTRL_DLL_RXC;
  740. rgmii_ctrl |= RGMII_CTRL_TIMING_SEL;
  741. b53_write8(dev, B53_CTRL_PAGE, off, rgmii_ctrl);
  742. dev_info(ds->dev, "Configured port %d for %s\n", port,
  743. phy_modes(phydev->interface));
  744. }
  745. /* configure MII port if necessary */
  746. if (is5325(dev)) {
  747. b53_read8(dev, B53_CTRL_PAGE, B53_PORT_OVERRIDE_CTRL,
  748. &reg);
  749. /* reverse mii needs to be enabled */
  750. if (!(reg & PORT_OVERRIDE_RV_MII_25)) {
  751. b53_write8(dev, B53_CTRL_PAGE, B53_PORT_OVERRIDE_CTRL,
  752. reg | PORT_OVERRIDE_RV_MII_25);
  753. b53_read8(dev, B53_CTRL_PAGE, B53_PORT_OVERRIDE_CTRL,
  754. &reg);
  755. if (!(reg & PORT_OVERRIDE_RV_MII_25)) {
  756. dev_err(ds->dev,
  757. "Failed to enable reverse MII mode\n");
  758. return;
  759. }
  760. }
  761. } else if (is5301x(dev)) {
  762. if (port != dev->cpu_port) {
  763. u8 po_reg = B53_GMII_PORT_OVERRIDE_CTRL(dev->cpu_port);
  764. u8 gmii_po;
  765. b53_read8(dev, B53_CTRL_PAGE, po_reg, &gmii_po);
  766. gmii_po |= GMII_PO_LINK |
  767. GMII_PO_RX_FLOW |
  768. GMII_PO_TX_FLOW |
  769. GMII_PO_EN |
  770. GMII_PO_SPEED_2000M;
  771. b53_write8(dev, B53_CTRL_PAGE, po_reg, gmii_po);
  772. }
  773. }
  774. }
  775. static int b53_vlan_filtering(struct dsa_switch *ds, int port,
  776. bool vlan_filtering)
  777. {
  778. return 0;
  779. }
  780. static int b53_vlan_prepare(struct dsa_switch *ds, int port,
  781. const struct switchdev_obj_port_vlan *vlan,
  782. struct switchdev_trans *trans)
  783. {
  784. struct b53_device *dev = ds->priv;
  785. if ((is5325(dev) || is5365(dev)) && vlan->vid_begin == 0)
  786. return -EOPNOTSUPP;
  787. if (vlan->vid_end > dev->num_vlans)
  788. return -ERANGE;
  789. b53_enable_vlan(dev, true);
  790. return 0;
  791. }
  792. static void b53_vlan_add(struct dsa_switch *ds, int port,
  793. const struct switchdev_obj_port_vlan *vlan,
  794. struct switchdev_trans *trans)
  795. {
  796. struct b53_device *dev = ds->priv;
  797. bool untagged = vlan->flags & BRIDGE_VLAN_INFO_UNTAGGED;
  798. bool pvid = vlan->flags & BRIDGE_VLAN_INFO_PVID;
  799. unsigned int cpu_port = dev->cpu_port;
  800. struct b53_vlan *vl;
  801. u16 vid;
  802. for (vid = vlan->vid_begin; vid <= vlan->vid_end; ++vid) {
  803. vl = &dev->vlans[vid];
  804. b53_get_vlan_entry(dev, vid, vl);
  805. vl->members |= BIT(port) | BIT(cpu_port);
  806. if (untagged)
  807. vl->untag |= BIT(port) | BIT(cpu_port);
  808. else
  809. vl->untag &= ~(BIT(port) | BIT(cpu_port));
  810. b53_set_vlan_entry(dev, vid, vl);
  811. b53_fast_age_vlan(dev, vid);
  812. }
  813. if (pvid) {
  814. b53_write16(dev, B53_VLAN_PAGE, B53_VLAN_PORT_DEF_TAG(port),
  815. vlan->vid_end);
  816. b53_write16(dev, B53_VLAN_PAGE, B53_VLAN_PORT_DEF_TAG(cpu_port),
  817. vlan->vid_end);
  818. b53_fast_age_vlan(dev, vid);
  819. }
  820. }
  821. static int b53_vlan_del(struct dsa_switch *ds, int port,
  822. const struct switchdev_obj_port_vlan *vlan)
  823. {
  824. struct b53_device *dev = ds->priv;
  825. bool untagged = vlan->flags & BRIDGE_VLAN_INFO_UNTAGGED;
  826. unsigned int cpu_port = dev->cpu_port;
  827. struct b53_vlan *vl;
  828. u16 vid;
  829. u16 pvid;
  830. b53_read16(dev, B53_VLAN_PAGE, B53_VLAN_PORT_DEF_TAG(port), &pvid);
  831. for (vid = vlan->vid_begin; vid <= vlan->vid_end; ++vid) {
  832. vl = &dev->vlans[vid];
  833. b53_get_vlan_entry(dev, vid, vl);
  834. vl->members &= ~BIT(port);
  835. if ((vl->members & BIT(cpu_port)) == BIT(cpu_port))
  836. vl->members = 0;
  837. if (pvid == vid) {
  838. if (is5325(dev) || is5365(dev))
  839. pvid = 1;
  840. else
  841. pvid = 0;
  842. }
  843. if (untagged) {
  844. vl->untag &= ~(BIT(port));
  845. if ((vl->untag & BIT(cpu_port)) == BIT(cpu_port))
  846. vl->untag = 0;
  847. }
  848. b53_set_vlan_entry(dev, vid, vl);
  849. b53_fast_age_vlan(dev, vid);
  850. }
  851. b53_write16(dev, B53_VLAN_PAGE, B53_VLAN_PORT_DEF_TAG(port), pvid);
  852. b53_write16(dev, B53_VLAN_PAGE, B53_VLAN_PORT_DEF_TAG(cpu_port), pvid);
  853. b53_fast_age_vlan(dev, pvid);
  854. return 0;
  855. }
  856. static int b53_vlan_dump(struct dsa_switch *ds, int port,
  857. struct switchdev_obj_port_vlan *vlan,
  858. int (*cb)(struct switchdev_obj *obj))
  859. {
  860. struct b53_device *dev = ds->priv;
  861. u16 vid, vid_start = 0, pvid;
  862. struct b53_vlan *vl;
  863. int err = 0;
  864. if (is5325(dev) || is5365(dev))
  865. vid_start = 1;
  866. b53_read16(dev, B53_VLAN_PAGE, B53_VLAN_PORT_DEF_TAG(port), &pvid);
  867. /* Use our software cache for dumps, since we do not have any HW
  868. * operation returning only the used/valid VLANs
  869. */
  870. for (vid = vid_start; vid < dev->num_vlans; vid++) {
  871. vl = &dev->vlans[vid];
  872. if (!vl->valid)
  873. continue;
  874. if (!(vl->members & BIT(port)))
  875. continue;
  876. vlan->vid_begin = vlan->vid_end = vid;
  877. vlan->flags = 0;
  878. if (vl->untag & BIT(port))
  879. vlan->flags |= BRIDGE_VLAN_INFO_UNTAGGED;
  880. if (pvid == vid)
  881. vlan->flags |= BRIDGE_VLAN_INFO_PVID;
  882. err = cb(&vlan->obj);
  883. if (err)
  884. break;
  885. }
  886. return err;
  887. }
  888. /* Address Resolution Logic routines */
  889. static int b53_arl_op_wait(struct b53_device *dev)
  890. {
  891. unsigned int timeout = 10;
  892. u8 reg;
  893. do {
  894. b53_read8(dev, B53_ARLIO_PAGE, B53_ARLTBL_RW_CTRL, &reg);
  895. if (!(reg & ARLTBL_START_DONE))
  896. return 0;
  897. usleep_range(1000, 2000);
  898. } while (timeout--);
  899. dev_warn(dev->dev, "timeout waiting for ARL to finish: 0x%02x\n", reg);
  900. return -ETIMEDOUT;
  901. }
  902. static int b53_arl_rw_op(struct b53_device *dev, unsigned int op)
  903. {
  904. u8 reg;
  905. if (op > ARLTBL_RW)
  906. return -EINVAL;
  907. b53_read8(dev, B53_ARLIO_PAGE, B53_ARLTBL_RW_CTRL, &reg);
  908. reg |= ARLTBL_START_DONE;
  909. if (op)
  910. reg |= ARLTBL_RW;
  911. else
  912. reg &= ~ARLTBL_RW;
  913. b53_write8(dev, B53_ARLIO_PAGE, B53_ARLTBL_RW_CTRL, reg);
  914. return b53_arl_op_wait(dev);
  915. }
  916. static int b53_arl_read(struct b53_device *dev, u64 mac,
  917. u16 vid, struct b53_arl_entry *ent, u8 *idx,
  918. bool is_valid)
  919. {
  920. unsigned int i;
  921. int ret;
  922. ret = b53_arl_op_wait(dev);
  923. if (ret)
  924. return ret;
  925. /* Read the bins */
  926. for (i = 0; i < dev->num_arl_entries; i++) {
  927. u64 mac_vid;
  928. u32 fwd_entry;
  929. b53_read64(dev, B53_ARLIO_PAGE,
  930. B53_ARLTBL_MAC_VID_ENTRY(i), &mac_vid);
  931. b53_read32(dev, B53_ARLIO_PAGE,
  932. B53_ARLTBL_DATA_ENTRY(i), &fwd_entry);
  933. b53_arl_to_entry(ent, mac_vid, fwd_entry);
  934. if (!(fwd_entry & ARLTBL_VALID))
  935. continue;
  936. if ((mac_vid & ARLTBL_MAC_MASK) != mac)
  937. continue;
  938. *idx = i;
  939. }
  940. return -ENOENT;
  941. }
  942. static int b53_arl_op(struct b53_device *dev, int op, int port,
  943. const unsigned char *addr, u16 vid, bool is_valid)
  944. {
  945. struct b53_arl_entry ent;
  946. u32 fwd_entry;
  947. u64 mac, mac_vid = 0;
  948. u8 idx = 0;
  949. int ret;
  950. /* Convert the array into a 64-bit MAC */
  951. mac = b53_mac_to_u64(addr);
  952. /* Perform a read for the given MAC and VID */
  953. b53_write48(dev, B53_ARLIO_PAGE, B53_MAC_ADDR_IDX, mac);
  954. b53_write16(dev, B53_ARLIO_PAGE, B53_VLAN_ID_IDX, vid);
  955. /* Issue a read operation for this MAC */
  956. ret = b53_arl_rw_op(dev, 1);
  957. if (ret)
  958. return ret;
  959. ret = b53_arl_read(dev, mac, vid, &ent, &idx, is_valid);
  960. /* If this is a read, just finish now */
  961. if (op)
  962. return ret;
  963. /* We could not find a matching MAC, so reset to a new entry */
  964. if (ret) {
  965. fwd_entry = 0;
  966. idx = 1;
  967. }
  968. memset(&ent, 0, sizeof(ent));
  969. ent.port = port;
  970. ent.is_valid = is_valid;
  971. ent.vid = vid;
  972. ent.is_static = true;
  973. memcpy(ent.mac, addr, ETH_ALEN);
  974. b53_arl_from_entry(&mac_vid, &fwd_entry, &ent);
  975. b53_write64(dev, B53_ARLIO_PAGE,
  976. B53_ARLTBL_MAC_VID_ENTRY(idx), mac_vid);
  977. b53_write32(dev, B53_ARLIO_PAGE,
  978. B53_ARLTBL_DATA_ENTRY(idx), fwd_entry);
  979. return b53_arl_rw_op(dev, 0);
  980. }
  981. static int b53_fdb_prepare(struct dsa_switch *ds, int port,
  982. const struct switchdev_obj_port_fdb *fdb,
  983. struct switchdev_trans *trans)
  984. {
  985. struct b53_device *priv = ds->priv;
  986. /* 5325 and 5365 require some more massaging, but could
  987. * be supported eventually
  988. */
  989. if (is5325(priv) || is5365(priv))
  990. return -EOPNOTSUPP;
  991. return 0;
  992. }
  993. static void b53_fdb_add(struct dsa_switch *ds, int port,
  994. const struct switchdev_obj_port_fdb *fdb,
  995. struct switchdev_trans *trans)
  996. {
  997. struct b53_device *priv = ds->priv;
  998. if (b53_arl_op(priv, 0, port, fdb->addr, fdb->vid, true))
  999. pr_err("%s: failed to add MAC address\n", __func__);
  1000. }
  1001. static int b53_fdb_del(struct dsa_switch *ds, int port,
  1002. const struct switchdev_obj_port_fdb *fdb)
  1003. {
  1004. struct b53_device *priv = ds->priv;
  1005. return b53_arl_op(priv, 0, port, fdb->addr, fdb->vid, false);
  1006. }
  1007. static int b53_arl_search_wait(struct b53_device *dev)
  1008. {
  1009. unsigned int timeout = 1000;
  1010. u8 reg;
  1011. do {
  1012. b53_read8(dev, B53_ARLIO_PAGE, B53_ARL_SRCH_CTL, &reg);
  1013. if (!(reg & ARL_SRCH_STDN))
  1014. return 0;
  1015. if (reg & ARL_SRCH_VLID)
  1016. return 0;
  1017. usleep_range(1000, 2000);
  1018. } while (timeout--);
  1019. return -ETIMEDOUT;
  1020. }
  1021. static void b53_arl_search_rd(struct b53_device *dev, u8 idx,
  1022. struct b53_arl_entry *ent)
  1023. {
  1024. u64 mac_vid;
  1025. u32 fwd_entry;
  1026. b53_read64(dev, B53_ARLIO_PAGE,
  1027. B53_ARL_SRCH_RSTL_MACVID(idx), &mac_vid);
  1028. b53_read32(dev, B53_ARLIO_PAGE,
  1029. B53_ARL_SRCH_RSTL(idx), &fwd_entry);
  1030. b53_arl_to_entry(ent, mac_vid, fwd_entry);
  1031. }
  1032. static int b53_fdb_copy(struct net_device *dev, int port,
  1033. const struct b53_arl_entry *ent,
  1034. struct switchdev_obj_port_fdb *fdb,
  1035. int (*cb)(struct switchdev_obj *obj))
  1036. {
  1037. if (!ent->is_valid)
  1038. return 0;
  1039. if (port != ent->port)
  1040. return 0;
  1041. ether_addr_copy(fdb->addr, ent->mac);
  1042. fdb->vid = ent->vid;
  1043. fdb->ndm_state = ent->is_static ? NUD_NOARP : NUD_REACHABLE;
  1044. return cb(&fdb->obj);
  1045. }
  1046. static int b53_fdb_dump(struct dsa_switch *ds, int port,
  1047. struct switchdev_obj_port_fdb *fdb,
  1048. int (*cb)(struct switchdev_obj *obj))
  1049. {
  1050. struct b53_device *priv = ds->priv;
  1051. struct net_device *dev = ds->ports[port].netdev;
  1052. struct b53_arl_entry results[2];
  1053. unsigned int count = 0;
  1054. int ret;
  1055. u8 reg;
  1056. /* Start search operation */
  1057. reg = ARL_SRCH_STDN;
  1058. b53_write8(priv, B53_ARLIO_PAGE, B53_ARL_SRCH_CTL, reg);
  1059. do {
  1060. ret = b53_arl_search_wait(priv);
  1061. if (ret)
  1062. return ret;
  1063. b53_arl_search_rd(priv, 0, &results[0]);
  1064. ret = b53_fdb_copy(dev, port, &results[0], fdb, cb);
  1065. if (ret)
  1066. return ret;
  1067. if (priv->num_arl_entries > 2) {
  1068. b53_arl_search_rd(priv, 1, &results[1]);
  1069. ret = b53_fdb_copy(dev, port, &results[1], fdb, cb);
  1070. if (ret)
  1071. return ret;
  1072. if (!results[0].is_valid && !results[1].is_valid)
  1073. break;
  1074. }
  1075. } while (count++ < 1024);
  1076. return 0;
  1077. }
  1078. static int b53_br_join(struct dsa_switch *ds, int port,
  1079. struct net_device *bridge)
  1080. {
  1081. struct b53_device *dev = ds->priv;
  1082. s8 cpu_port = ds->dst->cpu_port;
  1083. u16 pvlan, reg;
  1084. unsigned int i;
  1085. /* Make this port leave the all VLANs join since we will have proper
  1086. * VLAN entries from now on
  1087. */
  1088. if (is58xx(dev)) {
  1089. b53_read16(dev, B53_VLAN_PAGE, B53_JOIN_ALL_VLAN_EN, &reg);
  1090. reg &= ~BIT(port);
  1091. if ((reg & BIT(cpu_port)) == BIT(cpu_port))
  1092. reg &= ~BIT(cpu_port);
  1093. b53_write16(dev, B53_VLAN_PAGE, B53_JOIN_ALL_VLAN_EN, reg);
  1094. }
  1095. dev->ports[port].bridge_dev = bridge;
  1096. b53_read16(dev, B53_PVLAN_PAGE, B53_PVLAN_PORT_MASK(port), &pvlan);
  1097. b53_for_each_port(dev, i) {
  1098. if (dev->ports[i].bridge_dev != bridge)
  1099. continue;
  1100. /* Add this local port to the remote port VLAN control
  1101. * membership and update the remote port bitmask
  1102. */
  1103. b53_read16(dev, B53_PVLAN_PAGE, B53_PVLAN_PORT_MASK(i), &reg);
  1104. reg |= BIT(port);
  1105. b53_write16(dev, B53_PVLAN_PAGE, B53_PVLAN_PORT_MASK(i), reg);
  1106. dev->ports[i].vlan_ctl_mask = reg;
  1107. pvlan |= BIT(i);
  1108. }
  1109. /* Configure the local port VLAN control membership to include
  1110. * remote ports and update the local port bitmask
  1111. */
  1112. b53_write16(dev, B53_PVLAN_PAGE, B53_PVLAN_PORT_MASK(port), pvlan);
  1113. dev->ports[port].vlan_ctl_mask = pvlan;
  1114. return 0;
  1115. }
  1116. static void b53_br_leave(struct dsa_switch *ds, int port)
  1117. {
  1118. struct b53_device *dev = ds->priv;
  1119. struct net_device *bridge = dev->ports[port].bridge_dev;
  1120. struct b53_vlan *vl = &dev->vlans[0];
  1121. s8 cpu_port = ds->dst->cpu_port;
  1122. unsigned int i;
  1123. u16 pvlan, reg, pvid;
  1124. b53_read16(dev, B53_PVLAN_PAGE, B53_PVLAN_PORT_MASK(port), &pvlan);
  1125. b53_for_each_port(dev, i) {
  1126. /* Don't touch the remaining ports */
  1127. if (dev->ports[i].bridge_dev != bridge)
  1128. continue;
  1129. b53_read16(dev, B53_PVLAN_PAGE, B53_PVLAN_PORT_MASK(i), &reg);
  1130. reg &= ~BIT(port);
  1131. b53_write16(dev, B53_PVLAN_PAGE, B53_PVLAN_PORT_MASK(i), reg);
  1132. dev->ports[port].vlan_ctl_mask = reg;
  1133. /* Prevent self removal to preserve isolation */
  1134. if (port != i)
  1135. pvlan &= ~BIT(i);
  1136. }
  1137. b53_write16(dev, B53_PVLAN_PAGE, B53_PVLAN_PORT_MASK(port), pvlan);
  1138. dev->ports[port].vlan_ctl_mask = pvlan;
  1139. dev->ports[port].bridge_dev = NULL;
  1140. if (is5325(dev) || is5365(dev))
  1141. pvid = 1;
  1142. else
  1143. pvid = 0;
  1144. /* Make this port join all VLANs without VLAN entries */
  1145. if (is58xx(dev)) {
  1146. b53_read16(dev, B53_VLAN_PAGE, B53_JOIN_ALL_VLAN_EN, &reg);
  1147. reg |= BIT(port);
  1148. if (!(reg & BIT(cpu_port)))
  1149. reg |= BIT(cpu_port);
  1150. b53_write16(dev, B53_VLAN_PAGE, B53_JOIN_ALL_VLAN_EN, reg);
  1151. } else {
  1152. b53_get_vlan_entry(dev, pvid, vl);
  1153. vl->members |= BIT(port) | BIT(dev->cpu_port);
  1154. vl->untag |= BIT(port) | BIT(dev->cpu_port);
  1155. b53_set_vlan_entry(dev, pvid, vl);
  1156. }
  1157. }
  1158. static void b53_br_set_stp_state(struct dsa_switch *ds, int port,
  1159. u8 state)
  1160. {
  1161. struct b53_device *dev = ds->priv;
  1162. u8 hw_state, cur_hw_state;
  1163. u8 reg;
  1164. b53_read8(dev, B53_CTRL_PAGE, B53_PORT_CTRL(port), &reg);
  1165. cur_hw_state = reg & PORT_CTRL_STP_STATE_MASK;
  1166. switch (state) {
  1167. case BR_STATE_DISABLED:
  1168. hw_state = PORT_CTRL_DIS_STATE;
  1169. break;
  1170. case BR_STATE_LISTENING:
  1171. hw_state = PORT_CTRL_LISTEN_STATE;
  1172. break;
  1173. case BR_STATE_LEARNING:
  1174. hw_state = PORT_CTRL_LEARN_STATE;
  1175. break;
  1176. case BR_STATE_FORWARDING:
  1177. hw_state = PORT_CTRL_FWD_STATE;
  1178. break;
  1179. case BR_STATE_BLOCKING:
  1180. hw_state = PORT_CTRL_BLOCK_STATE;
  1181. break;
  1182. default:
  1183. dev_err(ds->dev, "invalid STP state: %d\n", state);
  1184. return;
  1185. }
  1186. /* Fast-age ARL entries if we are moving a port from Learning or
  1187. * Forwarding (cur_hw_state) state to Disabled, Blocking or Listening
  1188. * state (hw_state)
  1189. */
  1190. if (cur_hw_state != hw_state) {
  1191. if (cur_hw_state >= PORT_CTRL_LEARN_STATE &&
  1192. hw_state <= PORT_CTRL_LISTEN_STATE) {
  1193. if (b53_fast_age_port(dev, port)) {
  1194. dev_err(ds->dev, "fast ageing failed\n");
  1195. return;
  1196. }
  1197. }
  1198. }
  1199. b53_read8(dev, B53_CTRL_PAGE, B53_PORT_CTRL(port), &reg);
  1200. reg &= ~PORT_CTRL_STP_STATE_MASK;
  1201. reg |= hw_state;
  1202. b53_write8(dev, B53_CTRL_PAGE, B53_PORT_CTRL(port), reg);
  1203. }
  1204. static enum dsa_tag_protocol b53_get_tag_protocol(struct dsa_switch *ds)
  1205. {
  1206. return DSA_TAG_PROTO_NONE;
  1207. }
  1208. static struct dsa_switch_ops b53_switch_ops = {
  1209. .get_tag_protocol = b53_get_tag_protocol,
  1210. .setup = b53_setup,
  1211. .set_addr = b53_set_addr,
  1212. .get_strings = b53_get_strings,
  1213. .get_ethtool_stats = b53_get_ethtool_stats,
  1214. .get_sset_count = b53_get_sset_count,
  1215. .phy_read = b53_phy_read16,
  1216. .phy_write = b53_phy_write16,
  1217. .adjust_link = b53_adjust_link,
  1218. .port_enable = b53_enable_port,
  1219. .port_disable = b53_disable_port,
  1220. .port_bridge_join = b53_br_join,
  1221. .port_bridge_leave = b53_br_leave,
  1222. .port_stp_state_set = b53_br_set_stp_state,
  1223. .port_vlan_filtering = b53_vlan_filtering,
  1224. .port_vlan_prepare = b53_vlan_prepare,
  1225. .port_vlan_add = b53_vlan_add,
  1226. .port_vlan_del = b53_vlan_del,
  1227. .port_vlan_dump = b53_vlan_dump,
  1228. .port_fdb_prepare = b53_fdb_prepare,
  1229. .port_fdb_dump = b53_fdb_dump,
  1230. .port_fdb_add = b53_fdb_add,
  1231. .port_fdb_del = b53_fdb_del,
  1232. };
  1233. struct b53_chip_data {
  1234. u32 chip_id;
  1235. const char *dev_name;
  1236. u16 vlans;
  1237. u16 enabled_ports;
  1238. u8 cpu_port;
  1239. u8 vta_regs[3];
  1240. u8 arl_entries;
  1241. u8 duplex_reg;
  1242. u8 jumbo_pm_reg;
  1243. u8 jumbo_size_reg;
  1244. };
  1245. #define B53_VTA_REGS \
  1246. { B53_VT_ACCESS, B53_VT_INDEX, B53_VT_ENTRY }
  1247. #define B53_VTA_REGS_9798 \
  1248. { B53_VT_ACCESS_9798, B53_VT_INDEX_9798, B53_VT_ENTRY_9798 }
  1249. #define B53_VTA_REGS_63XX \
  1250. { B53_VT_ACCESS_63XX, B53_VT_INDEX_63XX, B53_VT_ENTRY_63XX }
  1251. static const struct b53_chip_data b53_switch_chips[] = {
  1252. {
  1253. .chip_id = BCM5325_DEVICE_ID,
  1254. .dev_name = "BCM5325",
  1255. .vlans = 16,
  1256. .enabled_ports = 0x1f,
  1257. .arl_entries = 2,
  1258. .cpu_port = B53_CPU_PORT_25,
  1259. .duplex_reg = B53_DUPLEX_STAT_FE,
  1260. },
  1261. {
  1262. .chip_id = BCM5365_DEVICE_ID,
  1263. .dev_name = "BCM5365",
  1264. .vlans = 256,
  1265. .enabled_ports = 0x1f,
  1266. .arl_entries = 2,
  1267. .cpu_port = B53_CPU_PORT_25,
  1268. .duplex_reg = B53_DUPLEX_STAT_FE,
  1269. },
  1270. {
  1271. .chip_id = BCM5395_DEVICE_ID,
  1272. .dev_name = "BCM5395",
  1273. .vlans = 4096,
  1274. .enabled_ports = 0x1f,
  1275. .arl_entries = 4,
  1276. .cpu_port = B53_CPU_PORT,
  1277. .vta_regs = B53_VTA_REGS,
  1278. .duplex_reg = B53_DUPLEX_STAT_GE,
  1279. .jumbo_pm_reg = B53_JUMBO_PORT_MASK,
  1280. .jumbo_size_reg = B53_JUMBO_MAX_SIZE,
  1281. },
  1282. {
  1283. .chip_id = BCM5397_DEVICE_ID,
  1284. .dev_name = "BCM5397",
  1285. .vlans = 4096,
  1286. .enabled_ports = 0x1f,
  1287. .arl_entries = 4,
  1288. .cpu_port = B53_CPU_PORT,
  1289. .vta_regs = B53_VTA_REGS_9798,
  1290. .duplex_reg = B53_DUPLEX_STAT_GE,
  1291. .jumbo_pm_reg = B53_JUMBO_PORT_MASK,
  1292. .jumbo_size_reg = B53_JUMBO_MAX_SIZE,
  1293. },
  1294. {
  1295. .chip_id = BCM5398_DEVICE_ID,
  1296. .dev_name = "BCM5398",
  1297. .vlans = 4096,
  1298. .enabled_ports = 0x7f,
  1299. .arl_entries = 4,
  1300. .cpu_port = B53_CPU_PORT,
  1301. .vta_regs = B53_VTA_REGS_9798,
  1302. .duplex_reg = B53_DUPLEX_STAT_GE,
  1303. .jumbo_pm_reg = B53_JUMBO_PORT_MASK,
  1304. .jumbo_size_reg = B53_JUMBO_MAX_SIZE,
  1305. },
  1306. {
  1307. .chip_id = BCM53115_DEVICE_ID,
  1308. .dev_name = "BCM53115",
  1309. .vlans = 4096,
  1310. .enabled_ports = 0x1f,
  1311. .arl_entries = 4,
  1312. .vta_regs = B53_VTA_REGS,
  1313. .cpu_port = B53_CPU_PORT,
  1314. .duplex_reg = B53_DUPLEX_STAT_GE,
  1315. .jumbo_pm_reg = B53_JUMBO_PORT_MASK,
  1316. .jumbo_size_reg = B53_JUMBO_MAX_SIZE,
  1317. },
  1318. {
  1319. .chip_id = BCM53125_DEVICE_ID,
  1320. .dev_name = "BCM53125",
  1321. .vlans = 4096,
  1322. .enabled_ports = 0xff,
  1323. .cpu_port = B53_CPU_PORT,
  1324. .vta_regs = B53_VTA_REGS,
  1325. .duplex_reg = B53_DUPLEX_STAT_GE,
  1326. .jumbo_pm_reg = B53_JUMBO_PORT_MASK,
  1327. .jumbo_size_reg = B53_JUMBO_MAX_SIZE,
  1328. },
  1329. {
  1330. .chip_id = BCM53128_DEVICE_ID,
  1331. .dev_name = "BCM53128",
  1332. .vlans = 4096,
  1333. .enabled_ports = 0x1ff,
  1334. .arl_entries = 4,
  1335. .cpu_port = B53_CPU_PORT,
  1336. .vta_regs = B53_VTA_REGS,
  1337. .duplex_reg = B53_DUPLEX_STAT_GE,
  1338. .jumbo_pm_reg = B53_JUMBO_PORT_MASK,
  1339. .jumbo_size_reg = B53_JUMBO_MAX_SIZE,
  1340. },
  1341. {
  1342. .chip_id = BCM63XX_DEVICE_ID,
  1343. .dev_name = "BCM63xx",
  1344. .vlans = 4096,
  1345. .enabled_ports = 0, /* pdata must provide them */
  1346. .arl_entries = 4,
  1347. .cpu_port = B53_CPU_PORT,
  1348. .vta_regs = B53_VTA_REGS_63XX,
  1349. .duplex_reg = B53_DUPLEX_STAT_63XX,
  1350. .jumbo_pm_reg = B53_JUMBO_PORT_MASK_63XX,
  1351. .jumbo_size_reg = B53_JUMBO_MAX_SIZE_63XX,
  1352. },
  1353. {
  1354. .chip_id = BCM53010_DEVICE_ID,
  1355. .dev_name = "BCM53010",
  1356. .vlans = 4096,
  1357. .enabled_ports = 0x1f,
  1358. .arl_entries = 4,
  1359. .cpu_port = B53_CPU_PORT_25, /* TODO: auto detect */
  1360. .vta_regs = B53_VTA_REGS,
  1361. .duplex_reg = B53_DUPLEX_STAT_GE,
  1362. .jumbo_pm_reg = B53_JUMBO_PORT_MASK,
  1363. .jumbo_size_reg = B53_JUMBO_MAX_SIZE,
  1364. },
  1365. {
  1366. .chip_id = BCM53011_DEVICE_ID,
  1367. .dev_name = "BCM53011",
  1368. .vlans = 4096,
  1369. .enabled_ports = 0x1bf,
  1370. .arl_entries = 4,
  1371. .cpu_port = B53_CPU_PORT_25, /* TODO: auto detect */
  1372. .vta_regs = B53_VTA_REGS,
  1373. .duplex_reg = B53_DUPLEX_STAT_GE,
  1374. .jumbo_pm_reg = B53_JUMBO_PORT_MASK,
  1375. .jumbo_size_reg = B53_JUMBO_MAX_SIZE,
  1376. },
  1377. {
  1378. .chip_id = BCM53012_DEVICE_ID,
  1379. .dev_name = "BCM53012",
  1380. .vlans = 4096,
  1381. .enabled_ports = 0x1bf,
  1382. .arl_entries = 4,
  1383. .cpu_port = B53_CPU_PORT_25, /* TODO: auto detect */
  1384. .vta_regs = B53_VTA_REGS,
  1385. .duplex_reg = B53_DUPLEX_STAT_GE,
  1386. .jumbo_pm_reg = B53_JUMBO_PORT_MASK,
  1387. .jumbo_size_reg = B53_JUMBO_MAX_SIZE,
  1388. },
  1389. {
  1390. .chip_id = BCM53018_DEVICE_ID,
  1391. .dev_name = "BCM53018",
  1392. .vlans = 4096,
  1393. .enabled_ports = 0x1f,
  1394. .arl_entries = 4,
  1395. .cpu_port = B53_CPU_PORT_25, /* TODO: auto detect */
  1396. .vta_regs = B53_VTA_REGS,
  1397. .duplex_reg = B53_DUPLEX_STAT_GE,
  1398. .jumbo_pm_reg = B53_JUMBO_PORT_MASK,
  1399. .jumbo_size_reg = B53_JUMBO_MAX_SIZE,
  1400. },
  1401. {
  1402. .chip_id = BCM53019_DEVICE_ID,
  1403. .dev_name = "BCM53019",
  1404. .vlans = 4096,
  1405. .enabled_ports = 0x1f,
  1406. .arl_entries = 4,
  1407. .cpu_port = B53_CPU_PORT_25, /* TODO: auto detect */
  1408. .vta_regs = B53_VTA_REGS,
  1409. .duplex_reg = B53_DUPLEX_STAT_GE,
  1410. .jumbo_pm_reg = B53_JUMBO_PORT_MASK,
  1411. .jumbo_size_reg = B53_JUMBO_MAX_SIZE,
  1412. },
  1413. {
  1414. .chip_id = BCM58XX_DEVICE_ID,
  1415. .dev_name = "BCM585xx/586xx/88312",
  1416. .vlans = 4096,
  1417. .enabled_ports = 0x1ff,
  1418. .arl_entries = 4,
  1419. .cpu_port = B53_CPU_PORT_25,
  1420. .vta_regs = B53_VTA_REGS,
  1421. .duplex_reg = B53_DUPLEX_STAT_GE,
  1422. .jumbo_pm_reg = B53_JUMBO_PORT_MASK,
  1423. .jumbo_size_reg = B53_JUMBO_MAX_SIZE,
  1424. },
  1425. {
  1426. .chip_id = BCM7445_DEVICE_ID,
  1427. .dev_name = "BCM7445",
  1428. .vlans = 4096,
  1429. .enabled_ports = 0x1ff,
  1430. .arl_entries = 4,
  1431. .cpu_port = B53_CPU_PORT,
  1432. .vta_regs = B53_VTA_REGS,
  1433. .duplex_reg = B53_DUPLEX_STAT_GE,
  1434. .jumbo_pm_reg = B53_JUMBO_PORT_MASK,
  1435. .jumbo_size_reg = B53_JUMBO_MAX_SIZE,
  1436. },
  1437. };
  1438. static int b53_switch_init(struct b53_device *dev)
  1439. {
  1440. unsigned int i;
  1441. int ret;
  1442. for (i = 0; i < ARRAY_SIZE(b53_switch_chips); i++) {
  1443. const struct b53_chip_data *chip = &b53_switch_chips[i];
  1444. if (chip->chip_id == dev->chip_id) {
  1445. if (!dev->enabled_ports)
  1446. dev->enabled_ports = chip->enabled_ports;
  1447. dev->name = chip->dev_name;
  1448. dev->duplex_reg = chip->duplex_reg;
  1449. dev->vta_regs[0] = chip->vta_regs[0];
  1450. dev->vta_regs[1] = chip->vta_regs[1];
  1451. dev->vta_regs[2] = chip->vta_regs[2];
  1452. dev->jumbo_pm_reg = chip->jumbo_pm_reg;
  1453. dev->cpu_port = chip->cpu_port;
  1454. dev->num_vlans = chip->vlans;
  1455. dev->num_arl_entries = chip->arl_entries;
  1456. break;
  1457. }
  1458. }
  1459. /* check which BCM5325x version we have */
  1460. if (is5325(dev)) {
  1461. u8 vc4;
  1462. b53_read8(dev, B53_VLAN_PAGE, B53_VLAN_CTRL4_25, &vc4);
  1463. /* check reserved bits */
  1464. switch (vc4 & 3) {
  1465. case 1:
  1466. /* BCM5325E */
  1467. break;
  1468. case 3:
  1469. /* BCM5325F - do not use port 4 */
  1470. dev->enabled_ports &= ~BIT(4);
  1471. break;
  1472. default:
  1473. /* On the BCM47XX SoCs this is the supported internal switch.*/
  1474. #ifndef CONFIG_BCM47XX
  1475. /* BCM5325M */
  1476. return -EINVAL;
  1477. #else
  1478. break;
  1479. #endif
  1480. }
  1481. } else if (dev->chip_id == BCM53115_DEVICE_ID) {
  1482. u64 strap_value;
  1483. b53_read48(dev, B53_STAT_PAGE, B53_STRAP_VALUE, &strap_value);
  1484. /* use second IMP port if GMII is enabled */
  1485. if (strap_value & SV_GMII_CTRL_115)
  1486. dev->cpu_port = 5;
  1487. }
  1488. /* cpu port is always last */
  1489. dev->num_ports = dev->cpu_port + 1;
  1490. dev->enabled_ports |= BIT(dev->cpu_port);
  1491. dev->ports = devm_kzalloc(dev->dev,
  1492. sizeof(struct b53_port) * dev->num_ports,
  1493. GFP_KERNEL);
  1494. if (!dev->ports)
  1495. return -ENOMEM;
  1496. dev->vlans = devm_kzalloc(dev->dev,
  1497. sizeof(struct b53_vlan) * dev->num_vlans,
  1498. GFP_KERNEL);
  1499. if (!dev->vlans)
  1500. return -ENOMEM;
  1501. dev->reset_gpio = b53_switch_get_reset_gpio(dev);
  1502. if (dev->reset_gpio >= 0) {
  1503. ret = devm_gpio_request_one(dev->dev, dev->reset_gpio,
  1504. GPIOF_OUT_INIT_HIGH, "robo_reset");
  1505. if (ret)
  1506. return ret;
  1507. }
  1508. return 0;
  1509. }
  1510. struct b53_device *b53_switch_alloc(struct device *base,
  1511. const struct b53_io_ops *ops,
  1512. void *priv)
  1513. {
  1514. struct dsa_switch *ds;
  1515. struct b53_device *dev;
  1516. ds = devm_kzalloc(base, sizeof(*ds) + sizeof(*dev), GFP_KERNEL);
  1517. if (!ds)
  1518. return NULL;
  1519. dev = (struct b53_device *)(ds + 1);
  1520. ds->priv = dev;
  1521. ds->dev = base;
  1522. dev->dev = base;
  1523. dev->ds = ds;
  1524. dev->priv = priv;
  1525. dev->ops = ops;
  1526. ds->ops = &b53_switch_ops;
  1527. mutex_init(&dev->reg_mutex);
  1528. mutex_init(&dev->stats_mutex);
  1529. return dev;
  1530. }
  1531. EXPORT_SYMBOL(b53_switch_alloc);
  1532. int b53_switch_detect(struct b53_device *dev)
  1533. {
  1534. u32 id32;
  1535. u16 tmp;
  1536. u8 id8;
  1537. int ret;
  1538. ret = b53_read8(dev, B53_MGMT_PAGE, B53_DEVICE_ID, &id8);
  1539. if (ret)
  1540. return ret;
  1541. switch (id8) {
  1542. case 0:
  1543. /* BCM5325 and BCM5365 do not have this register so reads
  1544. * return 0. But the read operation did succeed, so assume this
  1545. * is one of them.
  1546. *
  1547. * Next check if we can write to the 5325's VTA register; for
  1548. * 5365 it is read only.
  1549. */
  1550. b53_write16(dev, B53_VLAN_PAGE, B53_VLAN_TABLE_ACCESS_25, 0xf);
  1551. b53_read16(dev, B53_VLAN_PAGE, B53_VLAN_TABLE_ACCESS_25, &tmp);
  1552. if (tmp == 0xf)
  1553. dev->chip_id = BCM5325_DEVICE_ID;
  1554. else
  1555. dev->chip_id = BCM5365_DEVICE_ID;
  1556. break;
  1557. case BCM5395_DEVICE_ID:
  1558. case BCM5397_DEVICE_ID:
  1559. case BCM5398_DEVICE_ID:
  1560. dev->chip_id = id8;
  1561. break;
  1562. default:
  1563. ret = b53_read32(dev, B53_MGMT_PAGE, B53_DEVICE_ID, &id32);
  1564. if (ret)
  1565. return ret;
  1566. switch (id32) {
  1567. case BCM53115_DEVICE_ID:
  1568. case BCM53125_DEVICE_ID:
  1569. case BCM53128_DEVICE_ID:
  1570. case BCM53010_DEVICE_ID:
  1571. case BCM53011_DEVICE_ID:
  1572. case BCM53012_DEVICE_ID:
  1573. case BCM53018_DEVICE_ID:
  1574. case BCM53019_DEVICE_ID:
  1575. dev->chip_id = id32;
  1576. break;
  1577. default:
  1578. pr_err("unsupported switch detected (BCM53%02x/BCM%x)\n",
  1579. id8, id32);
  1580. return -ENODEV;
  1581. }
  1582. }
  1583. if (dev->chip_id == BCM5325_DEVICE_ID)
  1584. return b53_read8(dev, B53_STAT_PAGE, B53_REV_ID_25,
  1585. &dev->core_rev);
  1586. else
  1587. return b53_read8(dev, B53_MGMT_PAGE, B53_REV_ID,
  1588. &dev->core_rev);
  1589. }
  1590. EXPORT_SYMBOL(b53_switch_detect);
  1591. int b53_switch_register(struct b53_device *dev)
  1592. {
  1593. int ret;
  1594. if (dev->pdata) {
  1595. dev->chip_id = dev->pdata->chip_id;
  1596. dev->enabled_ports = dev->pdata->enabled_ports;
  1597. }
  1598. if (!dev->chip_id && b53_switch_detect(dev))
  1599. return -EINVAL;
  1600. ret = b53_switch_init(dev);
  1601. if (ret)
  1602. return ret;
  1603. pr_info("found switch: %s, rev %i\n", dev->name, dev->core_rev);
  1604. return dsa_register_switch(dev->ds, dev->ds->dev->of_node);
  1605. }
  1606. EXPORT_SYMBOL(b53_switch_register);
  1607. MODULE_AUTHOR("Jonas Gorski <jogo@openwrt.org>");
  1608. MODULE_DESCRIPTION("B53 switch library");
  1609. MODULE_LICENSE("Dual BSD/GPL");