b53_common.c 49 KB

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