b53_common.c 47 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886
  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_setup(struct dsa_switch *ds)
  638. {
  639. struct b53_device *dev = ds->priv;
  640. unsigned int port;
  641. int ret;
  642. ret = b53_reset_switch(dev);
  643. if (ret) {
  644. dev_err(ds->dev, "failed to reset switch\n");
  645. return ret;
  646. }
  647. b53_reset_mib(dev);
  648. ret = b53_apply_config(dev);
  649. if (ret)
  650. dev_err(ds->dev, "failed to apply configuration\n");
  651. for (port = 0; port < dev->num_ports; port++) {
  652. if (BIT(port) & ds->enabled_port_mask)
  653. b53_enable_port(ds, port, NULL);
  654. else if (dsa_is_cpu_port(ds, port))
  655. b53_enable_cpu_port(dev);
  656. else
  657. b53_disable_port(ds, port, NULL);
  658. }
  659. return ret;
  660. }
  661. static void b53_adjust_link(struct dsa_switch *ds, int port,
  662. struct phy_device *phydev)
  663. {
  664. struct b53_device *dev = ds->priv;
  665. u8 rgmii_ctrl = 0, reg = 0, off;
  666. if (!phy_is_pseudo_fixed_link(phydev))
  667. return;
  668. /* Override the port settings */
  669. if (port == dev->cpu_port) {
  670. off = B53_PORT_OVERRIDE_CTRL;
  671. reg = PORT_OVERRIDE_EN;
  672. } else {
  673. off = B53_GMII_PORT_OVERRIDE_CTRL(port);
  674. reg = GMII_PO_EN;
  675. }
  676. /* Set the link UP */
  677. if (phydev->link)
  678. reg |= PORT_OVERRIDE_LINK;
  679. if (phydev->duplex == DUPLEX_FULL)
  680. reg |= PORT_OVERRIDE_FULL_DUPLEX;
  681. switch (phydev->speed) {
  682. case 2000:
  683. reg |= PORT_OVERRIDE_SPEED_2000M;
  684. /* fallthrough */
  685. case SPEED_1000:
  686. reg |= PORT_OVERRIDE_SPEED_1000M;
  687. break;
  688. case SPEED_100:
  689. reg |= PORT_OVERRIDE_SPEED_100M;
  690. break;
  691. case SPEED_10:
  692. reg |= PORT_OVERRIDE_SPEED_10M;
  693. break;
  694. default:
  695. dev_err(ds->dev, "unknown speed: %d\n", phydev->speed);
  696. return;
  697. }
  698. /* Enable flow control on BCM5301x's CPU port */
  699. if (is5301x(dev) && port == dev->cpu_port)
  700. reg |= PORT_OVERRIDE_RX_FLOW | PORT_OVERRIDE_TX_FLOW;
  701. if (phydev->pause) {
  702. if (phydev->asym_pause)
  703. reg |= PORT_OVERRIDE_TX_FLOW;
  704. reg |= PORT_OVERRIDE_RX_FLOW;
  705. }
  706. b53_write8(dev, B53_CTRL_PAGE, off, reg);
  707. if (is531x5(dev) && phy_interface_is_rgmii(phydev)) {
  708. if (port == 8)
  709. off = B53_RGMII_CTRL_IMP;
  710. else
  711. off = B53_RGMII_CTRL_P(port);
  712. /* Configure the port RGMII clock delay by DLL disabled and
  713. * tx_clk aligned timing (restoring to reset defaults)
  714. */
  715. b53_read8(dev, B53_CTRL_PAGE, off, &rgmii_ctrl);
  716. rgmii_ctrl &= ~(RGMII_CTRL_DLL_RXC | RGMII_CTRL_DLL_TXC |
  717. RGMII_CTRL_TIMING_SEL);
  718. /* PHY_INTERFACE_MODE_RGMII_TXID means TX internal delay, make
  719. * sure that we enable the port TX clock internal delay to
  720. * account for this internal delay that is inserted, otherwise
  721. * the switch won't be able to receive correctly.
  722. *
  723. * PHY_INTERFACE_MODE_RGMII means that we are not introducing
  724. * any delay neither on transmission nor reception, so the
  725. * BCM53125 must also be configured accordingly to account for
  726. * the lack of delay and introduce
  727. *
  728. * The BCM53125 switch has its RX clock and TX clock control
  729. * swapped, hence the reason why we modify the TX clock path in
  730. * the "RGMII" case
  731. */
  732. if (phydev->interface == PHY_INTERFACE_MODE_RGMII_TXID)
  733. rgmii_ctrl |= RGMII_CTRL_DLL_TXC;
  734. if (phydev->interface == PHY_INTERFACE_MODE_RGMII)
  735. rgmii_ctrl |= RGMII_CTRL_DLL_TXC | RGMII_CTRL_DLL_RXC;
  736. rgmii_ctrl |= RGMII_CTRL_TIMING_SEL;
  737. b53_write8(dev, B53_CTRL_PAGE, off, rgmii_ctrl);
  738. dev_info(ds->dev, "Configured port %d for %s\n", port,
  739. phy_modes(phydev->interface));
  740. }
  741. /* configure MII port if necessary */
  742. if (is5325(dev)) {
  743. b53_read8(dev, B53_CTRL_PAGE, B53_PORT_OVERRIDE_CTRL,
  744. &reg);
  745. /* reverse mii needs to be enabled */
  746. if (!(reg & PORT_OVERRIDE_RV_MII_25)) {
  747. b53_write8(dev, B53_CTRL_PAGE, B53_PORT_OVERRIDE_CTRL,
  748. reg | PORT_OVERRIDE_RV_MII_25);
  749. b53_read8(dev, B53_CTRL_PAGE, B53_PORT_OVERRIDE_CTRL,
  750. &reg);
  751. if (!(reg & PORT_OVERRIDE_RV_MII_25)) {
  752. dev_err(ds->dev,
  753. "Failed to enable reverse MII mode\n");
  754. return;
  755. }
  756. }
  757. } else if (is5301x(dev)) {
  758. if (port != dev->cpu_port) {
  759. u8 po_reg = B53_GMII_PORT_OVERRIDE_CTRL(dev->cpu_port);
  760. u8 gmii_po;
  761. b53_read8(dev, B53_CTRL_PAGE, po_reg, &gmii_po);
  762. gmii_po |= GMII_PO_LINK |
  763. GMII_PO_RX_FLOW |
  764. GMII_PO_TX_FLOW |
  765. GMII_PO_EN |
  766. GMII_PO_SPEED_2000M;
  767. b53_write8(dev, B53_CTRL_PAGE, po_reg, gmii_po);
  768. }
  769. }
  770. }
  771. static int b53_vlan_filtering(struct dsa_switch *ds, int port,
  772. bool vlan_filtering)
  773. {
  774. return 0;
  775. }
  776. static int b53_vlan_prepare(struct dsa_switch *ds, int port,
  777. const struct switchdev_obj_port_vlan *vlan,
  778. struct switchdev_trans *trans)
  779. {
  780. struct b53_device *dev = ds->priv;
  781. if ((is5325(dev) || is5365(dev)) && vlan->vid_begin == 0)
  782. return -EOPNOTSUPP;
  783. if (vlan->vid_end > dev->num_vlans)
  784. return -ERANGE;
  785. b53_enable_vlan(dev, true);
  786. return 0;
  787. }
  788. static void b53_vlan_add(struct dsa_switch *ds, int port,
  789. const struct switchdev_obj_port_vlan *vlan,
  790. struct switchdev_trans *trans)
  791. {
  792. struct b53_device *dev = ds->priv;
  793. bool untagged = vlan->flags & BRIDGE_VLAN_INFO_UNTAGGED;
  794. bool pvid = vlan->flags & BRIDGE_VLAN_INFO_PVID;
  795. unsigned int cpu_port = dev->cpu_port;
  796. struct b53_vlan *vl;
  797. u16 vid;
  798. for (vid = vlan->vid_begin; vid <= vlan->vid_end; ++vid) {
  799. vl = &dev->vlans[vid];
  800. b53_get_vlan_entry(dev, vid, vl);
  801. vl->members |= BIT(port) | BIT(cpu_port);
  802. if (untagged)
  803. vl->untag |= BIT(port) | BIT(cpu_port);
  804. else
  805. vl->untag &= ~(BIT(port) | BIT(cpu_port));
  806. b53_set_vlan_entry(dev, vid, vl);
  807. b53_fast_age_vlan(dev, vid);
  808. }
  809. if (pvid) {
  810. b53_write16(dev, B53_VLAN_PAGE, B53_VLAN_PORT_DEF_TAG(port),
  811. vlan->vid_end);
  812. b53_write16(dev, B53_VLAN_PAGE, B53_VLAN_PORT_DEF_TAG(cpu_port),
  813. vlan->vid_end);
  814. b53_fast_age_vlan(dev, vid);
  815. }
  816. }
  817. static int b53_vlan_del(struct dsa_switch *ds, int port,
  818. const struct switchdev_obj_port_vlan *vlan)
  819. {
  820. struct b53_device *dev = ds->priv;
  821. bool untagged = vlan->flags & BRIDGE_VLAN_INFO_UNTAGGED;
  822. unsigned int cpu_port = dev->cpu_port;
  823. struct b53_vlan *vl;
  824. u16 vid;
  825. u16 pvid;
  826. b53_read16(dev, B53_VLAN_PAGE, B53_VLAN_PORT_DEF_TAG(port), &pvid);
  827. for (vid = vlan->vid_begin; vid <= vlan->vid_end; ++vid) {
  828. vl = &dev->vlans[vid];
  829. b53_get_vlan_entry(dev, vid, vl);
  830. vl->members &= ~BIT(port);
  831. if ((vl->members & BIT(cpu_port)) == BIT(cpu_port))
  832. vl->members = 0;
  833. if (pvid == vid) {
  834. if (is5325(dev) || is5365(dev))
  835. pvid = 1;
  836. else
  837. pvid = 0;
  838. }
  839. if (untagged) {
  840. vl->untag &= ~(BIT(port));
  841. if ((vl->untag & BIT(cpu_port)) == BIT(cpu_port))
  842. vl->untag = 0;
  843. }
  844. b53_set_vlan_entry(dev, vid, vl);
  845. b53_fast_age_vlan(dev, vid);
  846. }
  847. b53_write16(dev, B53_VLAN_PAGE, B53_VLAN_PORT_DEF_TAG(port), pvid);
  848. b53_write16(dev, B53_VLAN_PAGE, B53_VLAN_PORT_DEF_TAG(cpu_port), pvid);
  849. b53_fast_age_vlan(dev, pvid);
  850. return 0;
  851. }
  852. static int b53_vlan_dump(struct dsa_switch *ds, int port,
  853. struct switchdev_obj_port_vlan *vlan,
  854. int (*cb)(struct switchdev_obj *obj))
  855. {
  856. struct b53_device *dev = ds->priv;
  857. u16 vid, vid_start = 0, pvid;
  858. struct b53_vlan *vl;
  859. int err = 0;
  860. if (is5325(dev) || is5365(dev))
  861. vid_start = 1;
  862. b53_read16(dev, B53_VLAN_PAGE, B53_VLAN_PORT_DEF_TAG(port), &pvid);
  863. /* Use our software cache for dumps, since we do not have any HW
  864. * operation returning only the used/valid VLANs
  865. */
  866. for (vid = vid_start; vid < dev->num_vlans; vid++) {
  867. vl = &dev->vlans[vid];
  868. if (!vl->valid)
  869. continue;
  870. if (!(vl->members & BIT(port)))
  871. continue;
  872. vlan->vid_begin = vlan->vid_end = vid;
  873. vlan->flags = 0;
  874. if (vl->untag & BIT(port))
  875. vlan->flags |= BRIDGE_VLAN_INFO_UNTAGGED;
  876. if (pvid == vid)
  877. vlan->flags |= BRIDGE_VLAN_INFO_PVID;
  878. err = cb(&vlan->obj);
  879. if (err)
  880. break;
  881. }
  882. return err;
  883. }
  884. /* Address Resolution Logic routines */
  885. static int b53_arl_op_wait(struct b53_device *dev)
  886. {
  887. unsigned int timeout = 10;
  888. u8 reg;
  889. do {
  890. b53_read8(dev, B53_ARLIO_PAGE, B53_ARLTBL_RW_CTRL, &reg);
  891. if (!(reg & ARLTBL_START_DONE))
  892. return 0;
  893. usleep_range(1000, 2000);
  894. } while (timeout--);
  895. dev_warn(dev->dev, "timeout waiting for ARL to finish: 0x%02x\n", reg);
  896. return -ETIMEDOUT;
  897. }
  898. static int b53_arl_rw_op(struct b53_device *dev, unsigned int op)
  899. {
  900. u8 reg;
  901. if (op > ARLTBL_RW)
  902. return -EINVAL;
  903. b53_read8(dev, B53_ARLIO_PAGE, B53_ARLTBL_RW_CTRL, &reg);
  904. reg |= ARLTBL_START_DONE;
  905. if (op)
  906. reg |= ARLTBL_RW;
  907. else
  908. reg &= ~ARLTBL_RW;
  909. b53_write8(dev, B53_ARLIO_PAGE, B53_ARLTBL_RW_CTRL, reg);
  910. return b53_arl_op_wait(dev);
  911. }
  912. static int b53_arl_read(struct b53_device *dev, u64 mac,
  913. u16 vid, struct b53_arl_entry *ent, u8 *idx,
  914. bool is_valid)
  915. {
  916. unsigned int i;
  917. int ret;
  918. ret = b53_arl_op_wait(dev);
  919. if (ret)
  920. return ret;
  921. /* Read the bins */
  922. for (i = 0; i < dev->num_arl_entries; i++) {
  923. u64 mac_vid;
  924. u32 fwd_entry;
  925. b53_read64(dev, B53_ARLIO_PAGE,
  926. B53_ARLTBL_MAC_VID_ENTRY(i), &mac_vid);
  927. b53_read32(dev, B53_ARLIO_PAGE,
  928. B53_ARLTBL_DATA_ENTRY(i), &fwd_entry);
  929. b53_arl_to_entry(ent, mac_vid, fwd_entry);
  930. if (!(fwd_entry & ARLTBL_VALID))
  931. continue;
  932. if ((mac_vid & ARLTBL_MAC_MASK) != mac)
  933. continue;
  934. *idx = i;
  935. }
  936. return -ENOENT;
  937. }
  938. static int b53_arl_op(struct b53_device *dev, int op, int port,
  939. const unsigned char *addr, u16 vid, bool is_valid)
  940. {
  941. struct b53_arl_entry ent;
  942. u32 fwd_entry;
  943. u64 mac, mac_vid = 0;
  944. u8 idx = 0;
  945. int ret;
  946. /* Convert the array into a 64-bit MAC */
  947. mac = b53_mac_to_u64(addr);
  948. /* Perform a read for the given MAC and VID */
  949. b53_write48(dev, B53_ARLIO_PAGE, B53_MAC_ADDR_IDX, mac);
  950. b53_write16(dev, B53_ARLIO_PAGE, B53_VLAN_ID_IDX, vid);
  951. /* Issue a read operation for this MAC */
  952. ret = b53_arl_rw_op(dev, 1);
  953. if (ret)
  954. return ret;
  955. ret = b53_arl_read(dev, mac, vid, &ent, &idx, is_valid);
  956. /* If this is a read, just finish now */
  957. if (op)
  958. return ret;
  959. /* We could not find a matching MAC, so reset to a new entry */
  960. if (ret) {
  961. fwd_entry = 0;
  962. idx = 1;
  963. }
  964. memset(&ent, 0, sizeof(ent));
  965. ent.port = port;
  966. ent.is_valid = is_valid;
  967. ent.vid = vid;
  968. ent.is_static = true;
  969. memcpy(ent.mac, addr, ETH_ALEN);
  970. b53_arl_from_entry(&mac_vid, &fwd_entry, &ent);
  971. b53_write64(dev, B53_ARLIO_PAGE,
  972. B53_ARLTBL_MAC_VID_ENTRY(idx), mac_vid);
  973. b53_write32(dev, B53_ARLIO_PAGE,
  974. B53_ARLTBL_DATA_ENTRY(idx), fwd_entry);
  975. return b53_arl_rw_op(dev, 0);
  976. }
  977. static int b53_fdb_prepare(struct dsa_switch *ds, int port,
  978. const struct switchdev_obj_port_fdb *fdb,
  979. struct switchdev_trans *trans)
  980. {
  981. struct b53_device *priv = ds->priv;
  982. /* 5325 and 5365 require some more massaging, but could
  983. * be supported eventually
  984. */
  985. if (is5325(priv) || is5365(priv))
  986. return -EOPNOTSUPP;
  987. return 0;
  988. }
  989. static void b53_fdb_add(struct dsa_switch *ds, int port,
  990. const struct switchdev_obj_port_fdb *fdb,
  991. struct switchdev_trans *trans)
  992. {
  993. struct b53_device *priv = ds->priv;
  994. if (b53_arl_op(priv, 0, port, fdb->addr, fdb->vid, true))
  995. pr_err("%s: failed to add MAC address\n", __func__);
  996. }
  997. static int b53_fdb_del(struct dsa_switch *ds, int port,
  998. const struct switchdev_obj_port_fdb *fdb)
  999. {
  1000. struct b53_device *priv = ds->priv;
  1001. return b53_arl_op(priv, 0, port, fdb->addr, fdb->vid, false);
  1002. }
  1003. static int b53_arl_search_wait(struct b53_device *dev)
  1004. {
  1005. unsigned int timeout = 1000;
  1006. u8 reg;
  1007. do {
  1008. b53_read8(dev, B53_ARLIO_PAGE, B53_ARL_SRCH_CTL, &reg);
  1009. if (!(reg & ARL_SRCH_STDN))
  1010. return 0;
  1011. if (reg & ARL_SRCH_VLID)
  1012. return 0;
  1013. usleep_range(1000, 2000);
  1014. } while (timeout--);
  1015. return -ETIMEDOUT;
  1016. }
  1017. static void b53_arl_search_rd(struct b53_device *dev, u8 idx,
  1018. struct b53_arl_entry *ent)
  1019. {
  1020. u64 mac_vid;
  1021. u32 fwd_entry;
  1022. b53_read64(dev, B53_ARLIO_PAGE,
  1023. B53_ARL_SRCH_RSTL_MACVID(idx), &mac_vid);
  1024. b53_read32(dev, B53_ARLIO_PAGE,
  1025. B53_ARL_SRCH_RSTL(idx), &fwd_entry);
  1026. b53_arl_to_entry(ent, mac_vid, fwd_entry);
  1027. }
  1028. static int b53_fdb_copy(struct net_device *dev, int port,
  1029. const struct b53_arl_entry *ent,
  1030. struct switchdev_obj_port_fdb *fdb,
  1031. int (*cb)(struct switchdev_obj *obj))
  1032. {
  1033. if (!ent->is_valid)
  1034. return 0;
  1035. if (port != ent->port)
  1036. return 0;
  1037. ether_addr_copy(fdb->addr, ent->mac);
  1038. fdb->vid = ent->vid;
  1039. fdb->ndm_state = ent->is_static ? NUD_NOARP : NUD_REACHABLE;
  1040. return cb(&fdb->obj);
  1041. }
  1042. static int b53_fdb_dump(struct dsa_switch *ds, int port,
  1043. struct switchdev_obj_port_fdb *fdb,
  1044. int (*cb)(struct switchdev_obj *obj))
  1045. {
  1046. struct b53_device *priv = ds->priv;
  1047. struct net_device *dev = ds->ports[port].netdev;
  1048. struct b53_arl_entry results[2];
  1049. unsigned int count = 0;
  1050. int ret;
  1051. u8 reg;
  1052. /* Start search operation */
  1053. reg = ARL_SRCH_STDN;
  1054. b53_write8(priv, B53_ARLIO_PAGE, B53_ARL_SRCH_CTL, reg);
  1055. do {
  1056. ret = b53_arl_search_wait(priv);
  1057. if (ret)
  1058. return ret;
  1059. b53_arl_search_rd(priv, 0, &results[0]);
  1060. ret = b53_fdb_copy(dev, port, &results[0], fdb, cb);
  1061. if (ret)
  1062. return ret;
  1063. if (priv->num_arl_entries > 2) {
  1064. b53_arl_search_rd(priv, 1, &results[1]);
  1065. ret = b53_fdb_copy(dev, port, &results[1], fdb, cb);
  1066. if (ret)
  1067. return ret;
  1068. if (!results[0].is_valid && !results[1].is_valid)
  1069. break;
  1070. }
  1071. } while (count++ < 1024);
  1072. return 0;
  1073. }
  1074. static int b53_br_join(struct dsa_switch *ds, int port,
  1075. struct net_device *bridge)
  1076. {
  1077. struct b53_device *dev = ds->priv;
  1078. s8 cpu_port = ds->dst->cpu_port;
  1079. u16 pvlan, reg;
  1080. unsigned int i;
  1081. /* Make this port leave the all VLANs join since we will have proper
  1082. * VLAN entries from now on
  1083. */
  1084. if (is58xx(dev)) {
  1085. b53_read16(dev, B53_VLAN_PAGE, B53_JOIN_ALL_VLAN_EN, &reg);
  1086. reg &= ~BIT(port);
  1087. if ((reg & BIT(cpu_port)) == BIT(cpu_port))
  1088. reg &= ~BIT(cpu_port);
  1089. b53_write16(dev, B53_VLAN_PAGE, B53_JOIN_ALL_VLAN_EN, reg);
  1090. }
  1091. dev->ports[port].bridge_dev = bridge;
  1092. b53_read16(dev, B53_PVLAN_PAGE, B53_PVLAN_PORT_MASK(port), &pvlan);
  1093. b53_for_each_port(dev, i) {
  1094. if (dev->ports[i].bridge_dev != bridge)
  1095. continue;
  1096. /* Add this local port to the remote port VLAN control
  1097. * membership and update the remote port bitmask
  1098. */
  1099. b53_read16(dev, B53_PVLAN_PAGE, B53_PVLAN_PORT_MASK(i), &reg);
  1100. reg |= BIT(port);
  1101. b53_write16(dev, B53_PVLAN_PAGE, B53_PVLAN_PORT_MASK(i), reg);
  1102. dev->ports[i].vlan_ctl_mask = reg;
  1103. pvlan |= BIT(i);
  1104. }
  1105. /* Configure the local port VLAN control membership to include
  1106. * remote ports and update the local port bitmask
  1107. */
  1108. b53_write16(dev, B53_PVLAN_PAGE, B53_PVLAN_PORT_MASK(port), pvlan);
  1109. dev->ports[port].vlan_ctl_mask = pvlan;
  1110. return 0;
  1111. }
  1112. static void b53_br_leave(struct dsa_switch *ds, int port)
  1113. {
  1114. struct b53_device *dev = ds->priv;
  1115. struct net_device *bridge = dev->ports[port].bridge_dev;
  1116. struct b53_vlan *vl = &dev->vlans[0];
  1117. s8 cpu_port = ds->dst->cpu_port;
  1118. unsigned int i;
  1119. u16 pvlan, reg, pvid;
  1120. b53_read16(dev, B53_PVLAN_PAGE, B53_PVLAN_PORT_MASK(port), &pvlan);
  1121. b53_for_each_port(dev, i) {
  1122. /* Don't touch the remaining ports */
  1123. if (dev->ports[i].bridge_dev != bridge)
  1124. continue;
  1125. b53_read16(dev, B53_PVLAN_PAGE, B53_PVLAN_PORT_MASK(i), &reg);
  1126. reg &= ~BIT(port);
  1127. b53_write16(dev, B53_PVLAN_PAGE, B53_PVLAN_PORT_MASK(i), reg);
  1128. dev->ports[port].vlan_ctl_mask = reg;
  1129. /* Prevent self removal to preserve isolation */
  1130. if (port != i)
  1131. pvlan &= ~BIT(i);
  1132. }
  1133. b53_write16(dev, B53_PVLAN_PAGE, B53_PVLAN_PORT_MASK(port), pvlan);
  1134. dev->ports[port].vlan_ctl_mask = pvlan;
  1135. dev->ports[port].bridge_dev = NULL;
  1136. if (is5325(dev) || is5365(dev))
  1137. pvid = 1;
  1138. else
  1139. pvid = 0;
  1140. /* Make this port join all VLANs without VLAN entries */
  1141. if (is58xx(dev)) {
  1142. b53_read16(dev, B53_VLAN_PAGE, B53_JOIN_ALL_VLAN_EN, &reg);
  1143. reg |= BIT(port);
  1144. if (!(reg & BIT(cpu_port)))
  1145. reg |= BIT(cpu_port);
  1146. b53_write16(dev, B53_VLAN_PAGE, B53_JOIN_ALL_VLAN_EN, reg);
  1147. } else {
  1148. b53_get_vlan_entry(dev, pvid, vl);
  1149. vl->members |= BIT(port) | BIT(dev->cpu_port);
  1150. vl->untag |= BIT(port) | BIT(dev->cpu_port);
  1151. b53_set_vlan_entry(dev, pvid, vl);
  1152. }
  1153. }
  1154. static void b53_br_set_stp_state(struct dsa_switch *ds, int port, u8 state)
  1155. {
  1156. struct b53_device *dev = ds->priv;
  1157. u8 hw_state;
  1158. u8 reg;
  1159. switch (state) {
  1160. case BR_STATE_DISABLED:
  1161. hw_state = PORT_CTRL_DIS_STATE;
  1162. break;
  1163. case BR_STATE_LISTENING:
  1164. hw_state = PORT_CTRL_LISTEN_STATE;
  1165. break;
  1166. case BR_STATE_LEARNING:
  1167. hw_state = PORT_CTRL_LEARN_STATE;
  1168. break;
  1169. case BR_STATE_FORWARDING:
  1170. hw_state = PORT_CTRL_FWD_STATE;
  1171. break;
  1172. case BR_STATE_BLOCKING:
  1173. hw_state = PORT_CTRL_BLOCK_STATE;
  1174. break;
  1175. default:
  1176. dev_err(ds->dev, "invalid STP state: %d\n", state);
  1177. return;
  1178. }
  1179. b53_read8(dev, B53_CTRL_PAGE, B53_PORT_CTRL(port), &reg);
  1180. reg &= ~PORT_CTRL_STP_STATE_MASK;
  1181. reg |= hw_state;
  1182. b53_write8(dev, B53_CTRL_PAGE, B53_PORT_CTRL(port), reg);
  1183. }
  1184. static void b53_br_fast_age(struct dsa_switch *ds, int port)
  1185. {
  1186. struct b53_device *dev = ds->priv;
  1187. if (b53_fast_age_port(dev, port))
  1188. dev_err(ds->dev, "fast ageing failed\n");
  1189. }
  1190. static enum dsa_tag_protocol b53_get_tag_protocol(struct dsa_switch *ds)
  1191. {
  1192. return DSA_TAG_PROTO_NONE;
  1193. }
  1194. static struct dsa_switch_ops b53_switch_ops = {
  1195. .get_tag_protocol = b53_get_tag_protocol,
  1196. .setup = b53_setup,
  1197. .get_strings = b53_get_strings,
  1198. .get_ethtool_stats = b53_get_ethtool_stats,
  1199. .get_sset_count = b53_get_sset_count,
  1200. .phy_read = b53_phy_read16,
  1201. .phy_write = b53_phy_write16,
  1202. .adjust_link = b53_adjust_link,
  1203. .port_enable = b53_enable_port,
  1204. .port_disable = b53_disable_port,
  1205. .port_bridge_join = b53_br_join,
  1206. .port_bridge_leave = b53_br_leave,
  1207. .port_stp_state_set = b53_br_set_stp_state,
  1208. .port_fast_age = b53_br_fast_age,
  1209. .port_vlan_filtering = b53_vlan_filtering,
  1210. .port_vlan_prepare = b53_vlan_prepare,
  1211. .port_vlan_add = b53_vlan_add,
  1212. .port_vlan_del = b53_vlan_del,
  1213. .port_vlan_dump = b53_vlan_dump,
  1214. .port_fdb_prepare = b53_fdb_prepare,
  1215. .port_fdb_dump = b53_fdb_dump,
  1216. .port_fdb_add = b53_fdb_add,
  1217. .port_fdb_del = b53_fdb_del,
  1218. };
  1219. struct b53_chip_data {
  1220. u32 chip_id;
  1221. const char *dev_name;
  1222. u16 vlans;
  1223. u16 enabled_ports;
  1224. u8 cpu_port;
  1225. u8 vta_regs[3];
  1226. u8 arl_entries;
  1227. u8 duplex_reg;
  1228. u8 jumbo_pm_reg;
  1229. u8 jumbo_size_reg;
  1230. };
  1231. #define B53_VTA_REGS \
  1232. { B53_VT_ACCESS, B53_VT_INDEX, B53_VT_ENTRY }
  1233. #define B53_VTA_REGS_9798 \
  1234. { B53_VT_ACCESS_9798, B53_VT_INDEX_9798, B53_VT_ENTRY_9798 }
  1235. #define B53_VTA_REGS_63XX \
  1236. { B53_VT_ACCESS_63XX, B53_VT_INDEX_63XX, B53_VT_ENTRY_63XX }
  1237. static const struct b53_chip_data b53_switch_chips[] = {
  1238. {
  1239. .chip_id = BCM5325_DEVICE_ID,
  1240. .dev_name = "BCM5325",
  1241. .vlans = 16,
  1242. .enabled_ports = 0x1f,
  1243. .arl_entries = 2,
  1244. .cpu_port = B53_CPU_PORT_25,
  1245. .duplex_reg = B53_DUPLEX_STAT_FE,
  1246. },
  1247. {
  1248. .chip_id = BCM5365_DEVICE_ID,
  1249. .dev_name = "BCM5365",
  1250. .vlans = 256,
  1251. .enabled_ports = 0x1f,
  1252. .arl_entries = 2,
  1253. .cpu_port = B53_CPU_PORT_25,
  1254. .duplex_reg = B53_DUPLEX_STAT_FE,
  1255. },
  1256. {
  1257. .chip_id = BCM5395_DEVICE_ID,
  1258. .dev_name = "BCM5395",
  1259. .vlans = 4096,
  1260. .enabled_ports = 0x1f,
  1261. .arl_entries = 4,
  1262. .cpu_port = B53_CPU_PORT,
  1263. .vta_regs = B53_VTA_REGS,
  1264. .duplex_reg = B53_DUPLEX_STAT_GE,
  1265. .jumbo_pm_reg = B53_JUMBO_PORT_MASK,
  1266. .jumbo_size_reg = B53_JUMBO_MAX_SIZE,
  1267. },
  1268. {
  1269. .chip_id = BCM5397_DEVICE_ID,
  1270. .dev_name = "BCM5397",
  1271. .vlans = 4096,
  1272. .enabled_ports = 0x1f,
  1273. .arl_entries = 4,
  1274. .cpu_port = B53_CPU_PORT,
  1275. .vta_regs = B53_VTA_REGS_9798,
  1276. .duplex_reg = B53_DUPLEX_STAT_GE,
  1277. .jumbo_pm_reg = B53_JUMBO_PORT_MASK,
  1278. .jumbo_size_reg = B53_JUMBO_MAX_SIZE,
  1279. },
  1280. {
  1281. .chip_id = BCM5398_DEVICE_ID,
  1282. .dev_name = "BCM5398",
  1283. .vlans = 4096,
  1284. .enabled_ports = 0x7f,
  1285. .arl_entries = 4,
  1286. .cpu_port = B53_CPU_PORT,
  1287. .vta_regs = B53_VTA_REGS_9798,
  1288. .duplex_reg = B53_DUPLEX_STAT_GE,
  1289. .jumbo_pm_reg = B53_JUMBO_PORT_MASK,
  1290. .jumbo_size_reg = B53_JUMBO_MAX_SIZE,
  1291. },
  1292. {
  1293. .chip_id = BCM53115_DEVICE_ID,
  1294. .dev_name = "BCM53115",
  1295. .vlans = 4096,
  1296. .enabled_ports = 0x1f,
  1297. .arl_entries = 4,
  1298. .vta_regs = B53_VTA_REGS,
  1299. .cpu_port = B53_CPU_PORT,
  1300. .duplex_reg = B53_DUPLEX_STAT_GE,
  1301. .jumbo_pm_reg = B53_JUMBO_PORT_MASK,
  1302. .jumbo_size_reg = B53_JUMBO_MAX_SIZE,
  1303. },
  1304. {
  1305. .chip_id = BCM53125_DEVICE_ID,
  1306. .dev_name = "BCM53125",
  1307. .vlans = 4096,
  1308. .enabled_ports = 0xff,
  1309. .cpu_port = B53_CPU_PORT,
  1310. .vta_regs = B53_VTA_REGS,
  1311. .duplex_reg = B53_DUPLEX_STAT_GE,
  1312. .jumbo_pm_reg = B53_JUMBO_PORT_MASK,
  1313. .jumbo_size_reg = B53_JUMBO_MAX_SIZE,
  1314. },
  1315. {
  1316. .chip_id = BCM53128_DEVICE_ID,
  1317. .dev_name = "BCM53128",
  1318. .vlans = 4096,
  1319. .enabled_ports = 0x1ff,
  1320. .arl_entries = 4,
  1321. .cpu_port = B53_CPU_PORT,
  1322. .vta_regs = B53_VTA_REGS,
  1323. .duplex_reg = B53_DUPLEX_STAT_GE,
  1324. .jumbo_pm_reg = B53_JUMBO_PORT_MASK,
  1325. .jumbo_size_reg = B53_JUMBO_MAX_SIZE,
  1326. },
  1327. {
  1328. .chip_id = BCM63XX_DEVICE_ID,
  1329. .dev_name = "BCM63xx",
  1330. .vlans = 4096,
  1331. .enabled_ports = 0, /* pdata must provide them */
  1332. .arl_entries = 4,
  1333. .cpu_port = B53_CPU_PORT,
  1334. .vta_regs = B53_VTA_REGS_63XX,
  1335. .duplex_reg = B53_DUPLEX_STAT_63XX,
  1336. .jumbo_pm_reg = B53_JUMBO_PORT_MASK_63XX,
  1337. .jumbo_size_reg = B53_JUMBO_MAX_SIZE_63XX,
  1338. },
  1339. {
  1340. .chip_id = BCM53010_DEVICE_ID,
  1341. .dev_name = "BCM53010",
  1342. .vlans = 4096,
  1343. .enabled_ports = 0x1f,
  1344. .arl_entries = 4,
  1345. .cpu_port = B53_CPU_PORT_25, /* TODO: auto detect */
  1346. .vta_regs = B53_VTA_REGS,
  1347. .duplex_reg = B53_DUPLEX_STAT_GE,
  1348. .jumbo_pm_reg = B53_JUMBO_PORT_MASK,
  1349. .jumbo_size_reg = B53_JUMBO_MAX_SIZE,
  1350. },
  1351. {
  1352. .chip_id = BCM53011_DEVICE_ID,
  1353. .dev_name = "BCM53011",
  1354. .vlans = 4096,
  1355. .enabled_ports = 0x1bf,
  1356. .arl_entries = 4,
  1357. .cpu_port = B53_CPU_PORT_25, /* TODO: auto detect */
  1358. .vta_regs = B53_VTA_REGS,
  1359. .duplex_reg = B53_DUPLEX_STAT_GE,
  1360. .jumbo_pm_reg = B53_JUMBO_PORT_MASK,
  1361. .jumbo_size_reg = B53_JUMBO_MAX_SIZE,
  1362. },
  1363. {
  1364. .chip_id = BCM53012_DEVICE_ID,
  1365. .dev_name = "BCM53012",
  1366. .vlans = 4096,
  1367. .enabled_ports = 0x1bf,
  1368. .arl_entries = 4,
  1369. .cpu_port = B53_CPU_PORT_25, /* TODO: auto detect */
  1370. .vta_regs = B53_VTA_REGS,
  1371. .duplex_reg = B53_DUPLEX_STAT_GE,
  1372. .jumbo_pm_reg = B53_JUMBO_PORT_MASK,
  1373. .jumbo_size_reg = B53_JUMBO_MAX_SIZE,
  1374. },
  1375. {
  1376. .chip_id = BCM53018_DEVICE_ID,
  1377. .dev_name = "BCM53018",
  1378. .vlans = 4096,
  1379. .enabled_ports = 0x1f,
  1380. .arl_entries = 4,
  1381. .cpu_port = B53_CPU_PORT_25, /* TODO: auto detect */
  1382. .vta_regs = B53_VTA_REGS,
  1383. .duplex_reg = B53_DUPLEX_STAT_GE,
  1384. .jumbo_pm_reg = B53_JUMBO_PORT_MASK,
  1385. .jumbo_size_reg = B53_JUMBO_MAX_SIZE,
  1386. },
  1387. {
  1388. .chip_id = BCM53019_DEVICE_ID,
  1389. .dev_name = "BCM53019",
  1390. .vlans = 4096,
  1391. .enabled_ports = 0x1f,
  1392. .arl_entries = 4,
  1393. .cpu_port = B53_CPU_PORT_25, /* TODO: auto detect */
  1394. .vta_regs = B53_VTA_REGS,
  1395. .duplex_reg = B53_DUPLEX_STAT_GE,
  1396. .jumbo_pm_reg = B53_JUMBO_PORT_MASK,
  1397. .jumbo_size_reg = B53_JUMBO_MAX_SIZE,
  1398. },
  1399. {
  1400. .chip_id = BCM58XX_DEVICE_ID,
  1401. .dev_name = "BCM585xx/586xx/88312",
  1402. .vlans = 4096,
  1403. .enabled_ports = 0x1ff,
  1404. .arl_entries = 4,
  1405. .cpu_port = B53_CPU_PORT_25,
  1406. .vta_regs = B53_VTA_REGS,
  1407. .duplex_reg = B53_DUPLEX_STAT_GE,
  1408. .jumbo_pm_reg = B53_JUMBO_PORT_MASK,
  1409. .jumbo_size_reg = B53_JUMBO_MAX_SIZE,
  1410. },
  1411. {
  1412. .chip_id = BCM7445_DEVICE_ID,
  1413. .dev_name = "BCM7445",
  1414. .vlans = 4096,
  1415. .enabled_ports = 0x1ff,
  1416. .arl_entries = 4,
  1417. .cpu_port = B53_CPU_PORT,
  1418. .vta_regs = B53_VTA_REGS,
  1419. .duplex_reg = B53_DUPLEX_STAT_GE,
  1420. .jumbo_pm_reg = B53_JUMBO_PORT_MASK,
  1421. .jumbo_size_reg = B53_JUMBO_MAX_SIZE,
  1422. },
  1423. };
  1424. static int b53_switch_init(struct b53_device *dev)
  1425. {
  1426. unsigned int i;
  1427. int ret;
  1428. for (i = 0; i < ARRAY_SIZE(b53_switch_chips); i++) {
  1429. const struct b53_chip_data *chip = &b53_switch_chips[i];
  1430. if (chip->chip_id == dev->chip_id) {
  1431. if (!dev->enabled_ports)
  1432. dev->enabled_ports = chip->enabled_ports;
  1433. dev->name = chip->dev_name;
  1434. dev->duplex_reg = chip->duplex_reg;
  1435. dev->vta_regs[0] = chip->vta_regs[0];
  1436. dev->vta_regs[1] = chip->vta_regs[1];
  1437. dev->vta_regs[2] = chip->vta_regs[2];
  1438. dev->jumbo_pm_reg = chip->jumbo_pm_reg;
  1439. dev->cpu_port = chip->cpu_port;
  1440. dev->num_vlans = chip->vlans;
  1441. dev->num_arl_entries = chip->arl_entries;
  1442. break;
  1443. }
  1444. }
  1445. /* check which BCM5325x version we have */
  1446. if (is5325(dev)) {
  1447. u8 vc4;
  1448. b53_read8(dev, B53_VLAN_PAGE, B53_VLAN_CTRL4_25, &vc4);
  1449. /* check reserved bits */
  1450. switch (vc4 & 3) {
  1451. case 1:
  1452. /* BCM5325E */
  1453. break;
  1454. case 3:
  1455. /* BCM5325F - do not use port 4 */
  1456. dev->enabled_ports &= ~BIT(4);
  1457. break;
  1458. default:
  1459. /* On the BCM47XX SoCs this is the supported internal switch.*/
  1460. #ifndef CONFIG_BCM47XX
  1461. /* BCM5325M */
  1462. return -EINVAL;
  1463. #else
  1464. break;
  1465. #endif
  1466. }
  1467. } else if (dev->chip_id == BCM53115_DEVICE_ID) {
  1468. u64 strap_value;
  1469. b53_read48(dev, B53_STAT_PAGE, B53_STRAP_VALUE, &strap_value);
  1470. /* use second IMP port if GMII is enabled */
  1471. if (strap_value & SV_GMII_CTRL_115)
  1472. dev->cpu_port = 5;
  1473. }
  1474. /* cpu port is always last */
  1475. dev->num_ports = dev->cpu_port + 1;
  1476. dev->enabled_ports |= BIT(dev->cpu_port);
  1477. dev->ports = devm_kzalloc(dev->dev,
  1478. sizeof(struct b53_port) * dev->num_ports,
  1479. GFP_KERNEL);
  1480. if (!dev->ports)
  1481. return -ENOMEM;
  1482. dev->vlans = devm_kzalloc(dev->dev,
  1483. sizeof(struct b53_vlan) * dev->num_vlans,
  1484. GFP_KERNEL);
  1485. if (!dev->vlans)
  1486. return -ENOMEM;
  1487. dev->reset_gpio = b53_switch_get_reset_gpio(dev);
  1488. if (dev->reset_gpio >= 0) {
  1489. ret = devm_gpio_request_one(dev->dev, dev->reset_gpio,
  1490. GPIOF_OUT_INIT_HIGH, "robo_reset");
  1491. if (ret)
  1492. return ret;
  1493. }
  1494. return 0;
  1495. }
  1496. struct b53_device *b53_switch_alloc(struct device *base,
  1497. const struct b53_io_ops *ops,
  1498. void *priv)
  1499. {
  1500. struct dsa_switch *ds;
  1501. struct b53_device *dev;
  1502. ds = devm_kzalloc(base, sizeof(*ds) + sizeof(*dev), GFP_KERNEL);
  1503. if (!ds)
  1504. return NULL;
  1505. dev = (struct b53_device *)(ds + 1);
  1506. ds->priv = dev;
  1507. ds->dev = base;
  1508. dev->dev = base;
  1509. dev->ds = ds;
  1510. dev->priv = priv;
  1511. dev->ops = ops;
  1512. ds->ops = &b53_switch_ops;
  1513. mutex_init(&dev->reg_mutex);
  1514. mutex_init(&dev->stats_mutex);
  1515. return dev;
  1516. }
  1517. EXPORT_SYMBOL(b53_switch_alloc);
  1518. int b53_switch_detect(struct b53_device *dev)
  1519. {
  1520. u32 id32;
  1521. u16 tmp;
  1522. u8 id8;
  1523. int ret;
  1524. ret = b53_read8(dev, B53_MGMT_PAGE, B53_DEVICE_ID, &id8);
  1525. if (ret)
  1526. return ret;
  1527. switch (id8) {
  1528. case 0:
  1529. /* BCM5325 and BCM5365 do not have this register so reads
  1530. * return 0. But the read operation did succeed, so assume this
  1531. * is one of them.
  1532. *
  1533. * Next check if we can write to the 5325's VTA register; for
  1534. * 5365 it is read only.
  1535. */
  1536. b53_write16(dev, B53_VLAN_PAGE, B53_VLAN_TABLE_ACCESS_25, 0xf);
  1537. b53_read16(dev, B53_VLAN_PAGE, B53_VLAN_TABLE_ACCESS_25, &tmp);
  1538. if (tmp == 0xf)
  1539. dev->chip_id = BCM5325_DEVICE_ID;
  1540. else
  1541. dev->chip_id = BCM5365_DEVICE_ID;
  1542. break;
  1543. case BCM5395_DEVICE_ID:
  1544. case BCM5397_DEVICE_ID:
  1545. case BCM5398_DEVICE_ID:
  1546. dev->chip_id = id8;
  1547. break;
  1548. default:
  1549. ret = b53_read32(dev, B53_MGMT_PAGE, B53_DEVICE_ID, &id32);
  1550. if (ret)
  1551. return ret;
  1552. switch (id32) {
  1553. case BCM53115_DEVICE_ID:
  1554. case BCM53125_DEVICE_ID:
  1555. case BCM53128_DEVICE_ID:
  1556. case BCM53010_DEVICE_ID:
  1557. case BCM53011_DEVICE_ID:
  1558. case BCM53012_DEVICE_ID:
  1559. case BCM53018_DEVICE_ID:
  1560. case BCM53019_DEVICE_ID:
  1561. dev->chip_id = id32;
  1562. break;
  1563. default:
  1564. pr_err("unsupported switch detected (BCM53%02x/BCM%x)\n",
  1565. id8, id32);
  1566. return -ENODEV;
  1567. }
  1568. }
  1569. if (dev->chip_id == BCM5325_DEVICE_ID)
  1570. return b53_read8(dev, B53_STAT_PAGE, B53_REV_ID_25,
  1571. &dev->core_rev);
  1572. else
  1573. return b53_read8(dev, B53_MGMT_PAGE, B53_REV_ID,
  1574. &dev->core_rev);
  1575. }
  1576. EXPORT_SYMBOL(b53_switch_detect);
  1577. int b53_switch_register(struct b53_device *dev)
  1578. {
  1579. int ret;
  1580. if (dev->pdata) {
  1581. dev->chip_id = dev->pdata->chip_id;
  1582. dev->enabled_ports = dev->pdata->enabled_ports;
  1583. }
  1584. if (!dev->chip_id && b53_switch_detect(dev))
  1585. return -EINVAL;
  1586. ret = b53_switch_init(dev);
  1587. if (ret)
  1588. return ret;
  1589. pr_info("found switch: %s, rev %i\n", dev->name, dev->core_rev);
  1590. return dsa_register_switch(dev->ds, dev->ds->dev->of_node);
  1591. }
  1592. EXPORT_SYMBOL(b53_switch_register);
  1593. MODULE_AUTHOR("Jonas Gorski <jogo@openwrt.org>");
  1594. MODULE_DESCRIPTION("B53 switch library");
  1595. MODULE_LICENSE("Dual BSD/GPL");