b53_common.c 50 KB

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