b53_common.c 44 KB

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