b53_common.c 47 KB

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