b53_common.c 46 KB

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