b53_common.c 57 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293
  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 "b53_regs.h"
  31. #include "b53_priv.h"
  32. struct b53_mib_desc {
  33. u8 size;
  34. u8 offset;
  35. const char *name;
  36. };
  37. /* BCM5365 MIB counters */
  38. static const struct b53_mib_desc b53_mibs_65[] = {
  39. { 8, 0x00, "TxOctets" },
  40. { 4, 0x08, "TxDropPkts" },
  41. { 4, 0x10, "TxBroadcastPkts" },
  42. { 4, 0x14, "TxMulticastPkts" },
  43. { 4, 0x18, "TxUnicastPkts" },
  44. { 4, 0x1c, "TxCollisions" },
  45. { 4, 0x20, "TxSingleCollision" },
  46. { 4, 0x24, "TxMultipleCollision" },
  47. { 4, 0x28, "TxDeferredTransmit" },
  48. { 4, 0x2c, "TxLateCollision" },
  49. { 4, 0x30, "TxExcessiveCollision" },
  50. { 4, 0x38, "TxPausePkts" },
  51. { 8, 0x44, "RxOctets" },
  52. { 4, 0x4c, "RxUndersizePkts" },
  53. { 4, 0x50, "RxPausePkts" },
  54. { 4, 0x54, "Pkts64Octets" },
  55. { 4, 0x58, "Pkts65to127Octets" },
  56. { 4, 0x5c, "Pkts128to255Octets" },
  57. { 4, 0x60, "Pkts256to511Octets" },
  58. { 4, 0x64, "Pkts512to1023Octets" },
  59. { 4, 0x68, "Pkts1024to1522Octets" },
  60. { 4, 0x6c, "RxOversizePkts" },
  61. { 4, 0x70, "RxJabbers" },
  62. { 4, 0x74, "RxAlignmentErrors" },
  63. { 4, 0x78, "RxFCSErrors" },
  64. { 8, 0x7c, "RxGoodOctets" },
  65. { 4, 0x84, "RxDropPkts" },
  66. { 4, 0x88, "RxUnicastPkts" },
  67. { 4, 0x8c, "RxMulticastPkts" },
  68. { 4, 0x90, "RxBroadcastPkts" },
  69. { 4, 0x94, "RxSAChanges" },
  70. { 4, 0x98, "RxFragments" },
  71. };
  72. #define B53_MIBS_65_SIZE ARRAY_SIZE(b53_mibs_65)
  73. /* BCM63xx MIB counters */
  74. static const struct b53_mib_desc b53_mibs_63xx[] = {
  75. { 8, 0x00, "TxOctets" },
  76. { 4, 0x08, "TxDropPkts" },
  77. { 4, 0x0c, "TxQoSPkts" },
  78. { 4, 0x10, "TxBroadcastPkts" },
  79. { 4, 0x14, "TxMulticastPkts" },
  80. { 4, 0x18, "TxUnicastPkts" },
  81. { 4, 0x1c, "TxCollisions" },
  82. { 4, 0x20, "TxSingleCollision" },
  83. { 4, 0x24, "TxMultipleCollision" },
  84. { 4, 0x28, "TxDeferredTransmit" },
  85. { 4, 0x2c, "TxLateCollision" },
  86. { 4, 0x30, "TxExcessiveCollision" },
  87. { 4, 0x38, "TxPausePkts" },
  88. { 8, 0x3c, "TxQoSOctets" },
  89. { 8, 0x44, "RxOctets" },
  90. { 4, 0x4c, "RxUndersizePkts" },
  91. { 4, 0x50, "RxPausePkts" },
  92. { 4, 0x54, "Pkts64Octets" },
  93. { 4, 0x58, "Pkts65to127Octets" },
  94. { 4, 0x5c, "Pkts128to255Octets" },
  95. { 4, 0x60, "Pkts256to511Octets" },
  96. { 4, 0x64, "Pkts512to1023Octets" },
  97. { 4, 0x68, "Pkts1024to1522Octets" },
  98. { 4, 0x6c, "RxOversizePkts" },
  99. { 4, 0x70, "RxJabbers" },
  100. { 4, 0x74, "RxAlignmentErrors" },
  101. { 4, 0x78, "RxFCSErrors" },
  102. { 8, 0x7c, "RxGoodOctets" },
  103. { 4, 0x84, "RxDropPkts" },
  104. { 4, 0x88, "RxUnicastPkts" },
  105. { 4, 0x8c, "RxMulticastPkts" },
  106. { 4, 0x90, "RxBroadcastPkts" },
  107. { 4, 0x94, "RxSAChanges" },
  108. { 4, 0x98, "RxFragments" },
  109. { 4, 0xa0, "RxSymbolErrors" },
  110. { 4, 0xa4, "RxQoSPkts" },
  111. { 8, 0xa8, "RxQoSOctets" },
  112. { 4, 0xb0, "Pkts1523to2047Octets" },
  113. { 4, 0xb4, "Pkts2048to4095Octets" },
  114. { 4, 0xb8, "Pkts4096to8191Octets" },
  115. { 4, 0xbc, "Pkts8192to9728Octets" },
  116. { 4, 0xc0, "RxDiscarded" },
  117. };
  118. #define B53_MIBS_63XX_SIZE ARRAY_SIZE(b53_mibs_63xx)
  119. /* MIB counters */
  120. static const struct b53_mib_desc b53_mibs[] = {
  121. { 8, 0x00, "TxOctets" },
  122. { 4, 0x08, "TxDropPkts" },
  123. { 4, 0x10, "TxBroadcastPkts" },
  124. { 4, 0x14, "TxMulticastPkts" },
  125. { 4, 0x18, "TxUnicastPkts" },
  126. { 4, 0x1c, "TxCollisions" },
  127. { 4, 0x20, "TxSingleCollision" },
  128. { 4, 0x24, "TxMultipleCollision" },
  129. { 4, 0x28, "TxDeferredTransmit" },
  130. { 4, 0x2c, "TxLateCollision" },
  131. { 4, 0x30, "TxExcessiveCollision" },
  132. { 4, 0x38, "TxPausePkts" },
  133. { 8, 0x50, "RxOctets" },
  134. { 4, 0x58, "RxUndersizePkts" },
  135. { 4, 0x5c, "RxPausePkts" },
  136. { 4, 0x60, "Pkts64Octets" },
  137. { 4, 0x64, "Pkts65to127Octets" },
  138. { 4, 0x68, "Pkts128to255Octets" },
  139. { 4, 0x6c, "Pkts256to511Octets" },
  140. { 4, 0x70, "Pkts512to1023Octets" },
  141. { 4, 0x74, "Pkts1024to1522Octets" },
  142. { 4, 0x78, "RxOversizePkts" },
  143. { 4, 0x7c, "RxJabbers" },
  144. { 4, 0x80, "RxAlignmentErrors" },
  145. { 4, 0x84, "RxFCSErrors" },
  146. { 8, 0x88, "RxGoodOctets" },
  147. { 4, 0x90, "RxDropPkts" },
  148. { 4, 0x94, "RxUnicastPkts" },
  149. { 4, 0x98, "RxMulticastPkts" },
  150. { 4, 0x9c, "RxBroadcastPkts" },
  151. { 4, 0xa0, "RxSAChanges" },
  152. { 4, 0xa4, "RxFragments" },
  153. { 4, 0xa8, "RxJumboPkts" },
  154. { 4, 0xac, "RxSymbolErrors" },
  155. { 4, 0xc0, "RxDiscarded" },
  156. };
  157. #define B53_MIBS_SIZE ARRAY_SIZE(b53_mibs)
  158. static const struct b53_mib_desc b53_mibs_58xx[] = {
  159. { 8, 0x00, "TxOctets" },
  160. { 4, 0x08, "TxDropPkts" },
  161. { 4, 0x0c, "TxQPKTQ0" },
  162. { 4, 0x10, "TxBroadcastPkts" },
  163. { 4, 0x14, "TxMulticastPkts" },
  164. { 4, 0x18, "TxUnicastPKts" },
  165. { 4, 0x1c, "TxCollisions" },
  166. { 4, 0x20, "TxSingleCollision" },
  167. { 4, 0x24, "TxMultipleCollision" },
  168. { 4, 0x28, "TxDeferredCollision" },
  169. { 4, 0x2c, "TxLateCollision" },
  170. { 4, 0x30, "TxExcessiveCollision" },
  171. { 4, 0x34, "TxFrameInDisc" },
  172. { 4, 0x38, "TxPausePkts" },
  173. { 4, 0x3c, "TxQPKTQ1" },
  174. { 4, 0x40, "TxQPKTQ2" },
  175. { 4, 0x44, "TxQPKTQ3" },
  176. { 4, 0x48, "TxQPKTQ4" },
  177. { 4, 0x4c, "TxQPKTQ5" },
  178. { 8, 0x50, "RxOctets" },
  179. { 4, 0x58, "RxUndersizePkts" },
  180. { 4, 0x5c, "RxPausePkts" },
  181. { 4, 0x60, "RxPkts64Octets" },
  182. { 4, 0x64, "RxPkts65to127Octets" },
  183. { 4, 0x68, "RxPkts128to255Octets" },
  184. { 4, 0x6c, "RxPkts256to511Octets" },
  185. { 4, 0x70, "RxPkts512to1023Octets" },
  186. { 4, 0x74, "RxPkts1024toMaxPktsOctets" },
  187. { 4, 0x78, "RxOversizePkts" },
  188. { 4, 0x7c, "RxJabbers" },
  189. { 4, 0x80, "RxAlignmentErrors" },
  190. { 4, 0x84, "RxFCSErrors" },
  191. { 8, 0x88, "RxGoodOctets" },
  192. { 4, 0x90, "RxDropPkts" },
  193. { 4, 0x94, "RxUnicastPkts" },
  194. { 4, 0x98, "RxMulticastPkts" },
  195. { 4, 0x9c, "RxBroadcastPkts" },
  196. { 4, 0xa0, "RxSAChanges" },
  197. { 4, 0xa4, "RxFragments" },
  198. { 4, 0xa8, "RxJumboPkt" },
  199. { 4, 0xac, "RxSymblErr" },
  200. { 4, 0xb0, "InRangeErrCount" },
  201. { 4, 0xb4, "OutRangeErrCount" },
  202. { 4, 0xb8, "EEELpiEvent" },
  203. { 4, 0xbc, "EEELpiDuration" },
  204. { 4, 0xc0, "RxDiscard" },
  205. { 4, 0xc8, "TxQPKTQ6" },
  206. { 4, 0xcc, "TxQPKTQ7" },
  207. { 4, 0xd0, "TxPkts64Octets" },
  208. { 4, 0xd4, "TxPkts65to127Octets" },
  209. { 4, 0xd8, "TxPkts128to255Octets" },
  210. { 4, 0xdc, "TxPkts256to511Ocets" },
  211. { 4, 0xe0, "TxPkts512to1023Ocets" },
  212. { 4, 0xe4, "TxPkts1024toMaxPktOcets" },
  213. };
  214. #define B53_MIBS_58XX_SIZE ARRAY_SIZE(b53_mibs_58xx)
  215. static int b53_do_vlan_op(struct b53_device *dev, u8 op)
  216. {
  217. unsigned int i;
  218. b53_write8(dev, B53_ARLIO_PAGE, dev->vta_regs[0], VTA_START_CMD | op);
  219. for (i = 0; i < 10; i++) {
  220. u8 vta;
  221. b53_read8(dev, B53_ARLIO_PAGE, dev->vta_regs[0], &vta);
  222. if (!(vta & VTA_START_CMD))
  223. return 0;
  224. usleep_range(100, 200);
  225. }
  226. return -EIO;
  227. }
  228. static void b53_set_vlan_entry(struct b53_device *dev, u16 vid,
  229. struct b53_vlan *vlan)
  230. {
  231. if (is5325(dev)) {
  232. u32 entry = 0;
  233. if (vlan->members) {
  234. entry = ((vlan->untag & VA_UNTAG_MASK_25) <<
  235. VA_UNTAG_S_25) | vlan->members;
  236. if (dev->core_rev >= 3)
  237. entry |= VA_VALID_25_R4 | vid << VA_VID_HIGH_S;
  238. else
  239. entry |= VA_VALID_25;
  240. }
  241. b53_write32(dev, B53_VLAN_PAGE, B53_VLAN_WRITE_25, entry);
  242. b53_write16(dev, B53_VLAN_PAGE, B53_VLAN_TABLE_ACCESS_25, vid |
  243. VTA_RW_STATE_WR | VTA_RW_OP_EN);
  244. } else if (is5365(dev)) {
  245. u16 entry = 0;
  246. if (vlan->members)
  247. entry = ((vlan->untag & VA_UNTAG_MASK_65) <<
  248. VA_UNTAG_S_65) | vlan->members | VA_VALID_65;
  249. b53_write16(dev, B53_VLAN_PAGE, B53_VLAN_WRITE_65, entry);
  250. b53_write16(dev, B53_VLAN_PAGE, B53_VLAN_TABLE_ACCESS_65, vid |
  251. VTA_RW_STATE_WR | VTA_RW_OP_EN);
  252. } else {
  253. b53_write16(dev, B53_ARLIO_PAGE, dev->vta_regs[1], vid);
  254. b53_write32(dev, B53_ARLIO_PAGE, dev->vta_regs[2],
  255. (vlan->untag << VTE_UNTAG_S) | vlan->members);
  256. b53_do_vlan_op(dev, VTA_CMD_WRITE);
  257. }
  258. dev_dbg(dev->ds->dev, "VID: %d, members: 0x%04x, untag: 0x%04x\n",
  259. vid, vlan->members, vlan->untag);
  260. }
  261. static void b53_get_vlan_entry(struct b53_device *dev, u16 vid,
  262. struct b53_vlan *vlan)
  263. {
  264. if (is5325(dev)) {
  265. u32 entry = 0;
  266. b53_write16(dev, B53_VLAN_PAGE, B53_VLAN_TABLE_ACCESS_25, vid |
  267. VTA_RW_STATE_RD | VTA_RW_OP_EN);
  268. b53_read32(dev, B53_VLAN_PAGE, B53_VLAN_WRITE_25, &entry);
  269. if (dev->core_rev >= 3)
  270. vlan->valid = !!(entry & VA_VALID_25_R4);
  271. else
  272. vlan->valid = !!(entry & VA_VALID_25);
  273. vlan->members = entry & VA_MEMBER_MASK;
  274. vlan->untag = (entry >> VA_UNTAG_S_25) & VA_UNTAG_MASK_25;
  275. } else if (is5365(dev)) {
  276. u16 entry = 0;
  277. b53_write16(dev, B53_VLAN_PAGE, B53_VLAN_TABLE_ACCESS_65, vid |
  278. VTA_RW_STATE_WR | VTA_RW_OP_EN);
  279. b53_read16(dev, B53_VLAN_PAGE, B53_VLAN_WRITE_65, &entry);
  280. vlan->valid = !!(entry & VA_VALID_65);
  281. vlan->members = entry & VA_MEMBER_MASK;
  282. vlan->untag = (entry >> VA_UNTAG_S_65) & VA_UNTAG_MASK_65;
  283. } else {
  284. u32 entry = 0;
  285. b53_write16(dev, B53_ARLIO_PAGE, dev->vta_regs[1], vid);
  286. b53_do_vlan_op(dev, VTA_CMD_READ);
  287. b53_read32(dev, B53_ARLIO_PAGE, dev->vta_regs[2], &entry);
  288. vlan->members = entry & VTE_MEMBERS;
  289. vlan->untag = (entry >> VTE_UNTAG_S) & VTE_MEMBERS;
  290. vlan->valid = true;
  291. }
  292. }
  293. static void b53_set_forwarding(struct b53_device *dev, int enable)
  294. {
  295. u8 mgmt;
  296. b53_read8(dev, B53_CTRL_PAGE, B53_SWITCH_MODE, &mgmt);
  297. if (enable)
  298. mgmt |= SM_SW_FWD_EN;
  299. else
  300. mgmt &= ~SM_SW_FWD_EN;
  301. b53_write8(dev, B53_CTRL_PAGE, B53_SWITCH_MODE, mgmt);
  302. /* Include IMP port in dumb forwarding mode
  303. */
  304. b53_read8(dev, B53_CTRL_PAGE, B53_SWITCH_CTRL, &mgmt);
  305. mgmt |= B53_MII_DUMB_FWDG_EN;
  306. b53_write8(dev, B53_CTRL_PAGE, B53_SWITCH_CTRL, mgmt);
  307. }
  308. static void b53_enable_vlan(struct b53_device *dev, bool enable,
  309. bool enable_filtering)
  310. {
  311. u8 mgmt, vc0, vc1, vc4 = 0, vc5;
  312. b53_read8(dev, B53_CTRL_PAGE, B53_SWITCH_MODE, &mgmt);
  313. b53_read8(dev, B53_VLAN_PAGE, B53_VLAN_CTRL0, &vc0);
  314. b53_read8(dev, B53_VLAN_PAGE, B53_VLAN_CTRL1, &vc1);
  315. if (is5325(dev) || is5365(dev)) {
  316. b53_read8(dev, B53_VLAN_PAGE, B53_VLAN_CTRL4_25, &vc4);
  317. b53_read8(dev, B53_VLAN_PAGE, B53_VLAN_CTRL5_25, &vc5);
  318. } else if (is63xx(dev)) {
  319. b53_read8(dev, B53_VLAN_PAGE, B53_VLAN_CTRL4_63XX, &vc4);
  320. b53_read8(dev, B53_VLAN_PAGE, B53_VLAN_CTRL5_63XX, &vc5);
  321. } else {
  322. b53_read8(dev, B53_VLAN_PAGE, B53_VLAN_CTRL4, &vc4);
  323. b53_read8(dev, B53_VLAN_PAGE, B53_VLAN_CTRL5, &vc5);
  324. }
  325. mgmt &= ~SM_SW_FWD_MODE;
  326. if (enable) {
  327. vc0 |= VC0_VLAN_EN | VC0_VID_CHK_EN | VC0_VID_HASH_VID;
  328. vc1 |= VC1_RX_MCST_UNTAG_EN | VC1_RX_MCST_FWD_EN;
  329. vc4 &= ~VC4_ING_VID_CHECK_MASK;
  330. if (enable_filtering) {
  331. vc4 |= VC4_ING_VID_VIO_DROP << VC4_ING_VID_CHECK_S;
  332. vc5 |= VC5_DROP_VTABLE_MISS;
  333. } else {
  334. vc4 |= VC4_ING_VID_VIO_FWD << VC4_ING_VID_CHECK_S;
  335. vc5 &= ~VC5_DROP_VTABLE_MISS;
  336. }
  337. if (is5325(dev))
  338. vc0 &= ~VC0_RESERVED_1;
  339. if (is5325(dev) || is5365(dev))
  340. vc1 |= VC1_RX_MCST_TAG_EN;
  341. } else {
  342. vc0 &= ~(VC0_VLAN_EN | VC0_VID_CHK_EN | VC0_VID_HASH_VID);
  343. vc1 &= ~(VC1_RX_MCST_UNTAG_EN | VC1_RX_MCST_FWD_EN);
  344. vc4 &= ~VC4_ING_VID_CHECK_MASK;
  345. vc5 &= ~VC5_DROP_VTABLE_MISS;
  346. if (is5325(dev) || is5365(dev))
  347. vc4 |= VC4_ING_VID_VIO_FWD << VC4_ING_VID_CHECK_S;
  348. else
  349. vc4 |= VC4_ING_VID_VIO_TO_IMP << VC4_ING_VID_CHECK_S;
  350. if (is5325(dev) || is5365(dev))
  351. vc1 &= ~VC1_RX_MCST_TAG_EN;
  352. }
  353. if (!is5325(dev) && !is5365(dev))
  354. vc5 &= ~VC5_VID_FFF_EN;
  355. b53_write8(dev, B53_VLAN_PAGE, B53_VLAN_CTRL0, vc0);
  356. b53_write8(dev, B53_VLAN_PAGE, B53_VLAN_CTRL1, vc1);
  357. if (is5325(dev) || is5365(dev)) {
  358. /* enable the high 8 bit vid check on 5325 */
  359. if (is5325(dev) && enable)
  360. b53_write8(dev, B53_VLAN_PAGE, B53_VLAN_CTRL3,
  361. VC3_HIGH_8BIT_EN);
  362. else
  363. b53_write8(dev, B53_VLAN_PAGE, B53_VLAN_CTRL3, 0);
  364. b53_write8(dev, B53_VLAN_PAGE, B53_VLAN_CTRL4_25, vc4);
  365. b53_write8(dev, B53_VLAN_PAGE, B53_VLAN_CTRL5_25, vc5);
  366. } else if (is63xx(dev)) {
  367. b53_write16(dev, B53_VLAN_PAGE, B53_VLAN_CTRL3_63XX, 0);
  368. b53_write8(dev, B53_VLAN_PAGE, B53_VLAN_CTRL4_63XX, vc4);
  369. b53_write8(dev, B53_VLAN_PAGE, B53_VLAN_CTRL5_63XX, vc5);
  370. } else {
  371. b53_write16(dev, B53_VLAN_PAGE, B53_VLAN_CTRL3, 0);
  372. b53_write8(dev, B53_VLAN_PAGE, B53_VLAN_CTRL4, vc4);
  373. b53_write8(dev, B53_VLAN_PAGE, B53_VLAN_CTRL5, vc5);
  374. }
  375. b53_write8(dev, B53_CTRL_PAGE, B53_SWITCH_MODE, mgmt);
  376. dev->vlan_enabled = enable;
  377. dev->vlan_filtering_enabled = enable_filtering;
  378. }
  379. static int b53_set_jumbo(struct b53_device *dev, bool enable, bool allow_10_100)
  380. {
  381. u32 port_mask = 0;
  382. u16 max_size = JMS_MIN_SIZE;
  383. if (is5325(dev) || is5365(dev))
  384. return -EINVAL;
  385. if (enable) {
  386. port_mask = dev->enabled_ports;
  387. max_size = JMS_MAX_SIZE;
  388. if (allow_10_100)
  389. port_mask |= JPM_10_100_JUMBO_EN;
  390. }
  391. b53_write32(dev, B53_JUMBO_PAGE, dev->jumbo_pm_reg, port_mask);
  392. return b53_write16(dev, B53_JUMBO_PAGE, dev->jumbo_size_reg, max_size);
  393. }
  394. static int b53_flush_arl(struct b53_device *dev, u8 mask)
  395. {
  396. unsigned int i;
  397. b53_write8(dev, B53_CTRL_PAGE, B53_FAST_AGE_CTRL,
  398. FAST_AGE_DONE | FAST_AGE_DYNAMIC | mask);
  399. for (i = 0; i < 10; i++) {
  400. u8 fast_age_ctrl;
  401. b53_read8(dev, B53_CTRL_PAGE, B53_FAST_AGE_CTRL,
  402. &fast_age_ctrl);
  403. if (!(fast_age_ctrl & FAST_AGE_DONE))
  404. goto out;
  405. msleep(1);
  406. }
  407. return -ETIMEDOUT;
  408. out:
  409. /* Only age dynamic entries (default behavior) */
  410. b53_write8(dev, B53_CTRL_PAGE, B53_FAST_AGE_CTRL, FAST_AGE_DYNAMIC);
  411. return 0;
  412. }
  413. static int b53_fast_age_port(struct b53_device *dev, int port)
  414. {
  415. b53_write8(dev, B53_CTRL_PAGE, B53_FAST_AGE_PORT_CTRL, port);
  416. return b53_flush_arl(dev, FAST_AGE_PORT);
  417. }
  418. static int b53_fast_age_vlan(struct b53_device *dev, u16 vid)
  419. {
  420. b53_write16(dev, B53_CTRL_PAGE, B53_FAST_AGE_VID_CTRL, vid);
  421. return b53_flush_arl(dev, FAST_AGE_VLAN);
  422. }
  423. void b53_imp_vlan_setup(struct dsa_switch *ds, int cpu_port)
  424. {
  425. struct b53_device *dev = ds->priv;
  426. unsigned int i;
  427. u16 pvlan;
  428. /* Enable the IMP port to be in the same VLAN as the other ports
  429. * on a per-port basis such that we only have Port i and IMP in
  430. * the same VLAN.
  431. */
  432. b53_for_each_port(dev, i) {
  433. b53_read16(dev, B53_PVLAN_PAGE, B53_PVLAN_PORT_MASK(i), &pvlan);
  434. pvlan |= BIT(cpu_port);
  435. b53_write16(dev, B53_PVLAN_PAGE, B53_PVLAN_PORT_MASK(i), pvlan);
  436. }
  437. }
  438. EXPORT_SYMBOL(b53_imp_vlan_setup);
  439. static void b53_port_set_learning(struct b53_device *dev, int port,
  440. bool learning)
  441. {
  442. u16 reg;
  443. b53_read16(dev, B53_CTRL_PAGE, B53_DIS_LEARNING, &reg);
  444. if (learning)
  445. reg &= ~BIT(port);
  446. else
  447. reg |= BIT(port);
  448. b53_write16(dev, B53_CTRL_PAGE, B53_DIS_LEARNING, reg);
  449. }
  450. int b53_enable_port(struct dsa_switch *ds, int port, struct phy_device *phy)
  451. {
  452. struct b53_device *dev = ds->priv;
  453. unsigned int cpu_port = ds->ports[port].cpu_dp->index;
  454. u16 pvlan;
  455. b53_port_set_learning(dev, port, false);
  456. /* Clear the Rx and Tx disable bits and set to no spanning tree */
  457. b53_write8(dev, B53_CTRL_PAGE, B53_PORT_CTRL(port), 0);
  458. /* Set this port, and only this one to be in the default VLAN,
  459. * if member of a bridge, restore its membership prior to
  460. * bringing down this port.
  461. */
  462. b53_read16(dev, B53_PVLAN_PAGE, B53_PVLAN_PORT_MASK(port), &pvlan);
  463. pvlan &= ~0x1ff;
  464. pvlan |= BIT(port);
  465. pvlan |= dev->ports[port].vlan_ctl_mask;
  466. b53_write16(dev, B53_PVLAN_PAGE, B53_PVLAN_PORT_MASK(port), pvlan);
  467. b53_imp_vlan_setup(ds, cpu_port);
  468. /* If EEE was enabled, restore it */
  469. if (dev->ports[port].eee.eee_enabled)
  470. b53_eee_enable_set(ds, port, true);
  471. return 0;
  472. }
  473. EXPORT_SYMBOL(b53_enable_port);
  474. void b53_disable_port(struct dsa_switch *ds, int port, struct phy_device *phy)
  475. {
  476. struct b53_device *dev = ds->priv;
  477. u8 reg;
  478. /* Disable Tx/Rx for the port */
  479. b53_read8(dev, B53_CTRL_PAGE, B53_PORT_CTRL(port), &reg);
  480. reg |= PORT_CTRL_RX_DISABLE | PORT_CTRL_TX_DISABLE;
  481. b53_write8(dev, B53_CTRL_PAGE, B53_PORT_CTRL(port), reg);
  482. }
  483. EXPORT_SYMBOL(b53_disable_port);
  484. void b53_brcm_hdr_setup(struct dsa_switch *ds, int port)
  485. {
  486. bool tag_en = !(ds->ops->get_tag_protocol(ds, port) ==
  487. DSA_TAG_PROTO_NONE);
  488. struct b53_device *dev = ds->priv;
  489. u8 hdr_ctl, val;
  490. u16 reg;
  491. /* Resolve which bit controls the Broadcom tag */
  492. switch (port) {
  493. case 8:
  494. val = BRCM_HDR_P8_EN;
  495. break;
  496. case 7:
  497. val = BRCM_HDR_P7_EN;
  498. break;
  499. case 5:
  500. val = BRCM_HDR_P5_EN;
  501. break;
  502. default:
  503. val = 0;
  504. break;
  505. }
  506. /* Enable Broadcom tags for IMP port */
  507. b53_read8(dev, B53_MGMT_PAGE, B53_BRCM_HDR, &hdr_ctl);
  508. if (tag_en)
  509. hdr_ctl |= val;
  510. else
  511. hdr_ctl &= ~val;
  512. b53_write8(dev, B53_MGMT_PAGE, B53_BRCM_HDR, hdr_ctl);
  513. /* Registers below are only accessible on newer devices */
  514. if (!is58xx(dev))
  515. return;
  516. /* Enable reception Broadcom tag for CPU TX (switch RX) to
  517. * allow us to tag outgoing frames
  518. */
  519. b53_read16(dev, B53_MGMT_PAGE, B53_BRCM_HDR_RX_DIS, &reg);
  520. if (tag_en)
  521. reg &= ~BIT(port);
  522. else
  523. reg |= BIT(port);
  524. b53_write16(dev, B53_MGMT_PAGE, B53_BRCM_HDR_RX_DIS, reg);
  525. /* Enable transmission of Broadcom tags from the switch (CPU RX) to
  526. * allow delivering frames to the per-port net_devices
  527. */
  528. b53_read16(dev, B53_MGMT_PAGE, B53_BRCM_HDR_TX_DIS, &reg);
  529. if (tag_en)
  530. reg &= ~BIT(port);
  531. else
  532. reg |= BIT(port);
  533. b53_write16(dev, B53_MGMT_PAGE, B53_BRCM_HDR_TX_DIS, reg);
  534. }
  535. EXPORT_SYMBOL(b53_brcm_hdr_setup);
  536. static void b53_enable_cpu_port(struct b53_device *dev, int port)
  537. {
  538. u8 port_ctrl;
  539. /* BCM5325 CPU port is at 8 */
  540. if ((is5325(dev) || is5365(dev)) && port == B53_CPU_PORT_25)
  541. port = B53_CPU_PORT;
  542. port_ctrl = PORT_CTRL_RX_BCST_EN |
  543. PORT_CTRL_RX_MCST_EN |
  544. PORT_CTRL_RX_UCST_EN;
  545. b53_write8(dev, B53_CTRL_PAGE, B53_PORT_CTRL(port), port_ctrl);
  546. b53_brcm_hdr_setup(dev->ds, port);
  547. b53_port_set_learning(dev, port, false);
  548. }
  549. static void b53_enable_mib(struct b53_device *dev)
  550. {
  551. u8 gc;
  552. b53_read8(dev, B53_MGMT_PAGE, B53_GLOBAL_CONFIG, &gc);
  553. gc &= ~(GC_RESET_MIB | GC_MIB_AC_EN);
  554. b53_write8(dev, B53_MGMT_PAGE, B53_GLOBAL_CONFIG, gc);
  555. }
  556. static u16 b53_default_pvid(struct b53_device *dev)
  557. {
  558. if (is5325(dev) || is5365(dev))
  559. return 1;
  560. else
  561. return 0;
  562. }
  563. int b53_configure_vlan(struct dsa_switch *ds)
  564. {
  565. struct b53_device *dev = ds->priv;
  566. struct b53_vlan vl = { 0 };
  567. int i, def_vid;
  568. def_vid = b53_default_pvid(dev);
  569. /* clear all vlan entries */
  570. if (is5325(dev) || is5365(dev)) {
  571. for (i = def_vid; i < dev->num_vlans; i++)
  572. b53_set_vlan_entry(dev, i, &vl);
  573. } else {
  574. b53_do_vlan_op(dev, VTA_CMD_CLEAR);
  575. }
  576. b53_enable_vlan(dev, dev->vlan_enabled, dev->vlan_filtering_enabled);
  577. b53_for_each_port(dev, i)
  578. b53_write16(dev, B53_VLAN_PAGE,
  579. B53_VLAN_PORT_DEF_TAG(i), def_vid);
  580. if (!is5325(dev) && !is5365(dev))
  581. b53_set_jumbo(dev, dev->enable_jumbo, false);
  582. return 0;
  583. }
  584. EXPORT_SYMBOL(b53_configure_vlan);
  585. static void b53_switch_reset_gpio(struct b53_device *dev)
  586. {
  587. int gpio = dev->reset_gpio;
  588. if (gpio < 0)
  589. return;
  590. /* Reset sequence: RESET low(50ms)->high(20ms)
  591. */
  592. gpio_set_value(gpio, 0);
  593. mdelay(50);
  594. gpio_set_value(gpio, 1);
  595. mdelay(20);
  596. dev->current_page = 0xff;
  597. }
  598. static int b53_switch_reset(struct b53_device *dev)
  599. {
  600. unsigned int timeout = 1000;
  601. u8 mgmt, reg;
  602. b53_switch_reset_gpio(dev);
  603. if (is539x(dev)) {
  604. b53_write8(dev, B53_CTRL_PAGE, B53_SOFTRESET, 0x83);
  605. b53_write8(dev, B53_CTRL_PAGE, B53_SOFTRESET, 0x00);
  606. }
  607. /* This is specific to 58xx devices here, do not use is58xx() which
  608. * covers the larger Starfigther 2 family, including 7445/7278 which
  609. * still use this driver as a library and need to perform the reset
  610. * earlier.
  611. */
  612. if (dev->chip_id == BCM58XX_DEVICE_ID ||
  613. dev->chip_id == BCM583XX_DEVICE_ID) {
  614. b53_read8(dev, B53_CTRL_PAGE, B53_SOFTRESET, &reg);
  615. reg |= SW_RST | EN_SW_RST | EN_CH_RST;
  616. b53_write8(dev, B53_CTRL_PAGE, B53_SOFTRESET, reg);
  617. do {
  618. b53_read8(dev, B53_CTRL_PAGE, B53_SOFTRESET, &reg);
  619. if (!(reg & SW_RST))
  620. break;
  621. usleep_range(1000, 2000);
  622. } while (timeout-- > 0);
  623. if (timeout == 0)
  624. return -ETIMEDOUT;
  625. }
  626. b53_read8(dev, B53_CTRL_PAGE, B53_SWITCH_MODE, &mgmt);
  627. if (!(mgmt & SM_SW_FWD_EN)) {
  628. mgmt &= ~SM_SW_FWD_MODE;
  629. mgmt |= SM_SW_FWD_EN;
  630. b53_write8(dev, B53_CTRL_PAGE, B53_SWITCH_MODE, mgmt);
  631. b53_read8(dev, B53_CTRL_PAGE, B53_SWITCH_MODE, &mgmt);
  632. if (!(mgmt & SM_SW_FWD_EN)) {
  633. dev_err(dev->dev, "Failed to enable switch!\n");
  634. return -EINVAL;
  635. }
  636. }
  637. b53_enable_mib(dev);
  638. return b53_flush_arl(dev, FAST_AGE_STATIC);
  639. }
  640. static int b53_phy_read16(struct dsa_switch *ds, int addr, int reg)
  641. {
  642. struct b53_device *priv = ds->priv;
  643. u16 value = 0;
  644. int ret;
  645. if (priv->ops->phy_read16)
  646. ret = priv->ops->phy_read16(priv, addr, reg, &value);
  647. else
  648. ret = b53_read16(priv, B53_PORT_MII_PAGE(addr),
  649. reg * 2, &value);
  650. return ret ? ret : value;
  651. }
  652. static int b53_phy_write16(struct dsa_switch *ds, int addr, int reg, u16 val)
  653. {
  654. struct b53_device *priv = ds->priv;
  655. if (priv->ops->phy_write16)
  656. return priv->ops->phy_write16(priv, addr, reg, val);
  657. return b53_write16(priv, B53_PORT_MII_PAGE(addr), reg * 2, val);
  658. }
  659. static int b53_reset_switch(struct b53_device *priv)
  660. {
  661. /* reset vlans */
  662. priv->enable_jumbo = false;
  663. memset(priv->vlans, 0, sizeof(*priv->vlans) * priv->num_vlans);
  664. memset(priv->ports, 0, sizeof(*priv->ports) * priv->num_ports);
  665. return b53_switch_reset(priv);
  666. }
  667. static int b53_apply_config(struct b53_device *priv)
  668. {
  669. /* disable switching */
  670. b53_set_forwarding(priv, 0);
  671. b53_configure_vlan(priv->ds);
  672. /* enable switching */
  673. b53_set_forwarding(priv, 1);
  674. return 0;
  675. }
  676. static void b53_reset_mib(struct b53_device *priv)
  677. {
  678. u8 gc;
  679. b53_read8(priv, B53_MGMT_PAGE, B53_GLOBAL_CONFIG, &gc);
  680. b53_write8(priv, B53_MGMT_PAGE, B53_GLOBAL_CONFIG, gc | GC_RESET_MIB);
  681. msleep(1);
  682. b53_write8(priv, B53_MGMT_PAGE, B53_GLOBAL_CONFIG, gc & ~GC_RESET_MIB);
  683. msleep(1);
  684. }
  685. static const struct b53_mib_desc *b53_get_mib(struct b53_device *dev)
  686. {
  687. if (is5365(dev))
  688. return b53_mibs_65;
  689. else if (is63xx(dev))
  690. return b53_mibs_63xx;
  691. else if (is58xx(dev))
  692. return b53_mibs_58xx;
  693. else
  694. return b53_mibs;
  695. }
  696. static unsigned int b53_get_mib_size(struct b53_device *dev)
  697. {
  698. if (is5365(dev))
  699. return B53_MIBS_65_SIZE;
  700. else if (is63xx(dev))
  701. return B53_MIBS_63XX_SIZE;
  702. else if (is58xx(dev))
  703. return B53_MIBS_58XX_SIZE;
  704. else
  705. return B53_MIBS_SIZE;
  706. }
  707. static struct phy_device *b53_get_phy_device(struct dsa_switch *ds, int port)
  708. {
  709. /* These ports typically do not have built-in PHYs */
  710. switch (port) {
  711. case B53_CPU_PORT_25:
  712. case 7:
  713. case B53_CPU_PORT:
  714. return NULL;
  715. }
  716. return mdiobus_get_phy(ds->slave_mii_bus, port);
  717. }
  718. void b53_get_strings(struct dsa_switch *ds, int port, u32 stringset,
  719. uint8_t *data)
  720. {
  721. struct b53_device *dev = ds->priv;
  722. const struct b53_mib_desc *mibs = b53_get_mib(dev);
  723. unsigned int mib_size = b53_get_mib_size(dev);
  724. struct phy_device *phydev;
  725. unsigned int i;
  726. if (stringset == ETH_SS_STATS) {
  727. for (i = 0; i < mib_size; i++)
  728. strlcpy(data + i * ETH_GSTRING_LEN,
  729. mibs[i].name, ETH_GSTRING_LEN);
  730. } else if (stringset == ETH_SS_PHY_STATS) {
  731. phydev = b53_get_phy_device(ds, port);
  732. if (!phydev)
  733. return;
  734. phy_ethtool_get_strings(phydev, data);
  735. }
  736. }
  737. EXPORT_SYMBOL(b53_get_strings);
  738. void b53_get_ethtool_stats(struct dsa_switch *ds, int port, uint64_t *data)
  739. {
  740. struct b53_device *dev = ds->priv;
  741. const struct b53_mib_desc *mibs = b53_get_mib(dev);
  742. unsigned int mib_size = b53_get_mib_size(dev);
  743. const struct b53_mib_desc *s;
  744. unsigned int i;
  745. u64 val = 0;
  746. if (is5365(dev) && port == 5)
  747. port = 8;
  748. mutex_lock(&dev->stats_mutex);
  749. for (i = 0; i < mib_size; i++) {
  750. s = &mibs[i];
  751. if (s->size == 8) {
  752. b53_read64(dev, B53_MIB_PAGE(port), s->offset, &val);
  753. } else {
  754. u32 val32;
  755. b53_read32(dev, B53_MIB_PAGE(port), s->offset,
  756. &val32);
  757. val = val32;
  758. }
  759. data[i] = (u64)val;
  760. }
  761. mutex_unlock(&dev->stats_mutex);
  762. }
  763. EXPORT_SYMBOL(b53_get_ethtool_stats);
  764. void b53_get_ethtool_phy_stats(struct dsa_switch *ds, int port, uint64_t *data)
  765. {
  766. struct phy_device *phydev;
  767. phydev = b53_get_phy_device(ds, port);
  768. if (!phydev)
  769. return;
  770. phy_ethtool_get_stats(phydev, NULL, data);
  771. }
  772. EXPORT_SYMBOL(b53_get_ethtool_phy_stats);
  773. int b53_get_sset_count(struct dsa_switch *ds, int port, int sset)
  774. {
  775. struct b53_device *dev = ds->priv;
  776. struct phy_device *phydev;
  777. if (sset == ETH_SS_STATS) {
  778. return b53_get_mib_size(dev);
  779. } else if (sset == ETH_SS_PHY_STATS) {
  780. phydev = b53_get_phy_device(ds, port);
  781. if (!phydev)
  782. return 0;
  783. return phy_ethtool_get_sset_count(phydev);
  784. }
  785. return 0;
  786. }
  787. EXPORT_SYMBOL(b53_get_sset_count);
  788. static int b53_setup(struct dsa_switch *ds)
  789. {
  790. struct b53_device *dev = ds->priv;
  791. unsigned int port;
  792. int ret;
  793. ret = b53_reset_switch(dev);
  794. if (ret) {
  795. dev_err(ds->dev, "failed to reset switch\n");
  796. return ret;
  797. }
  798. b53_reset_mib(dev);
  799. ret = b53_apply_config(dev);
  800. if (ret)
  801. dev_err(ds->dev, "failed to apply configuration\n");
  802. /* Configure IMP/CPU port, disable unused ports. Enabled
  803. * ports will be configured with .port_enable
  804. */
  805. for (port = 0; port < dev->num_ports; port++) {
  806. if (dsa_is_cpu_port(ds, port))
  807. b53_enable_cpu_port(dev, port);
  808. else if (dsa_is_unused_port(ds, port))
  809. b53_disable_port(ds, port, NULL);
  810. }
  811. return ret;
  812. }
  813. static void b53_adjust_link(struct dsa_switch *ds, int port,
  814. struct phy_device *phydev)
  815. {
  816. struct b53_device *dev = ds->priv;
  817. struct ethtool_eee *p = &dev->ports[port].eee;
  818. u8 rgmii_ctrl = 0, reg = 0, off;
  819. if (!phy_is_pseudo_fixed_link(phydev))
  820. return;
  821. /* Override the port settings */
  822. if (port == dev->cpu_port) {
  823. off = B53_PORT_OVERRIDE_CTRL;
  824. reg = PORT_OVERRIDE_EN;
  825. } else {
  826. off = B53_GMII_PORT_OVERRIDE_CTRL(port);
  827. reg = GMII_PO_EN;
  828. }
  829. /* Set the link UP */
  830. if (phydev->link)
  831. reg |= PORT_OVERRIDE_LINK;
  832. if (phydev->duplex == DUPLEX_FULL)
  833. reg |= PORT_OVERRIDE_FULL_DUPLEX;
  834. switch (phydev->speed) {
  835. case 2000:
  836. reg |= PORT_OVERRIDE_SPEED_2000M;
  837. /* fallthrough */
  838. case SPEED_1000:
  839. reg |= PORT_OVERRIDE_SPEED_1000M;
  840. break;
  841. case SPEED_100:
  842. reg |= PORT_OVERRIDE_SPEED_100M;
  843. break;
  844. case SPEED_10:
  845. reg |= PORT_OVERRIDE_SPEED_10M;
  846. break;
  847. default:
  848. dev_err(ds->dev, "unknown speed: %d\n", phydev->speed);
  849. return;
  850. }
  851. /* Enable flow control on BCM5301x's CPU port */
  852. if (is5301x(dev) && port == dev->cpu_port)
  853. reg |= PORT_OVERRIDE_RX_FLOW | PORT_OVERRIDE_TX_FLOW;
  854. if (phydev->pause) {
  855. if (phydev->asym_pause)
  856. reg |= PORT_OVERRIDE_TX_FLOW;
  857. reg |= PORT_OVERRIDE_RX_FLOW;
  858. }
  859. b53_write8(dev, B53_CTRL_PAGE, off, reg);
  860. if (is531x5(dev) && phy_interface_is_rgmii(phydev)) {
  861. if (port == 8)
  862. off = B53_RGMII_CTRL_IMP;
  863. else
  864. off = B53_RGMII_CTRL_P(port);
  865. /* Configure the port RGMII clock delay by DLL disabled and
  866. * tx_clk aligned timing (restoring to reset defaults)
  867. */
  868. b53_read8(dev, B53_CTRL_PAGE, off, &rgmii_ctrl);
  869. rgmii_ctrl &= ~(RGMII_CTRL_DLL_RXC | RGMII_CTRL_DLL_TXC |
  870. RGMII_CTRL_TIMING_SEL);
  871. /* PHY_INTERFACE_MODE_RGMII_TXID means TX internal delay, make
  872. * sure that we enable the port TX clock internal delay to
  873. * account for this internal delay that is inserted, otherwise
  874. * the switch won't be able to receive correctly.
  875. *
  876. * PHY_INTERFACE_MODE_RGMII means that we are not introducing
  877. * any delay neither on transmission nor reception, so the
  878. * BCM53125 must also be configured accordingly to account for
  879. * the lack of delay and introduce
  880. *
  881. * The BCM53125 switch has its RX clock and TX clock control
  882. * swapped, hence the reason why we modify the TX clock path in
  883. * the "RGMII" case
  884. */
  885. if (phydev->interface == PHY_INTERFACE_MODE_RGMII_TXID)
  886. rgmii_ctrl |= RGMII_CTRL_DLL_TXC;
  887. if (phydev->interface == PHY_INTERFACE_MODE_RGMII)
  888. rgmii_ctrl |= RGMII_CTRL_DLL_TXC | RGMII_CTRL_DLL_RXC;
  889. rgmii_ctrl |= RGMII_CTRL_TIMING_SEL;
  890. b53_write8(dev, B53_CTRL_PAGE, off, rgmii_ctrl);
  891. dev_info(ds->dev, "Configured port %d for %s\n", port,
  892. phy_modes(phydev->interface));
  893. }
  894. /* configure MII port if necessary */
  895. if (is5325(dev)) {
  896. b53_read8(dev, B53_CTRL_PAGE, B53_PORT_OVERRIDE_CTRL,
  897. &reg);
  898. /* reverse mii needs to be enabled */
  899. if (!(reg & PORT_OVERRIDE_RV_MII_25)) {
  900. b53_write8(dev, B53_CTRL_PAGE, B53_PORT_OVERRIDE_CTRL,
  901. reg | PORT_OVERRIDE_RV_MII_25);
  902. b53_read8(dev, B53_CTRL_PAGE, B53_PORT_OVERRIDE_CTRL,
  903. &reg);
  904. if (!(reg & PORT_OVERRIDE_RV_MII_25)) {
  905. dev_err(ds->dev,
  906. "Failed to enable reverse MII mode\n");
  907. return;
  908. }
  909. }
  910. } else if (is5301x(dev)) {
  911. if (port != dev->cpu_port) {
  912. u8 po_reg = B53_GMII_PORT_OVERRIDE_CTRL(dev->cpu_port);
  913. u8 gmii_po;
  914. b53_read8(dev, B53_CTRL_PAGE, po_reg, &gmii_po);
  915. gmii_po |= GMII_PO_LINK |
  916. GMII_PO_RX_FLOW |
  917. GMII_PO_TX_FLOW |
  918. GMII_PO_EN |
  919. GMII_PO_SPEED_2000M;
  920. b53_write8(dev, B53_CTRL_PAGE, po_reg, gmii_po);
  921. }
  922. }
  923. /* Re-negotiate EEE if it was enabled already */
  924. p->eee_enabled = b53_eee_init(ds, port, phydev);
  925. }
  926. int b53_vlan_filtering(struct dsa_switch *ds, int port, bool vlan_filtering)
  927. {
  928. struct b53_device *dev = ds->priv;
  929. struct net_device *bridge_dev;
  930. unsigned int i;
  931. u16 pvid, new_pvid;
  932. /* Handle the case were multiple bridges span the same switch device
  933. * and one of them has a different setting than what is being requested
  934. * which would be breaking filtering semantics for any of the other
  935. * bridge devices.
  936. */
  937. b53_for_each_port(dev, i) {
  938. bridge_dev = dsa_to_port(ds, i)->bridge_dev;
  939. if (bridge_dev &&
  940. bridge_dev != dsa_to_port(ds, port)->bridge_dev &&
  941. br_vlan_enabled(bridge_dev) != vlan_filtering) {
  942. netdev_err(bridge_dev,
  943. "VLAN filtering is global to the switch!\n");
  944. return -EINVAL;
  945. }
  946. }
  947. b53_read16(dev, B53_VLAN_PAGE, B53_VLAN_PORT_DEF_TAG(port), &pvid);
  948. new_pvid = pvid;
  949. if (dev->vlan_filtering_enabled && !vlan_filtering) {
  950. /* Filtering is currently enabled, use the default PVID since
  951. * the bridge does not expect tagging anymore
  952. */
  953. dev->ports[port].pvid = pvid;
  954. new_pvid = b53_default_pvid(dev);
  955. } else if (!dev->vlan_filtering_enabled && vlan_filtering) {
  956. /* Filtering is currently disabled, restore the previous PVID */
  957. new_pvid = dev->ports[port].pvid;
  958. }
  959. if (pvid != new_pvid)
  960. b53_write16(dev, B53_VLAN_PAGE, B53_VLAN_PORT_DEF_TAG(port),
  961. new_pvid);
  962. b53_enable_vlan(dev, dev->vlan_enabled, vlan_filtering);
  963. return 0;
  964. }
  965. EXPORT_SYMBOL(b53_vlan_filtering);
  966. int b53_vlan_prepare(struct dsa_switch *ds, int port,
  967. const struct switchdev_obj_port_vlan *vlan)
  968. {
  969. struct b53_device *dev = ds->priv;
  970. if ((is5325(dev) || is5365(dev)) && vlan->vid_begin == 0)
  971. return -EOPNOTSUPP;
  972. if (vlan->vid_end >= dev->num_vlans)
  973. return -ERANGE;
  974. b53_enable_vlan(dev, true, dev->vlan_filtering_enabled);
  975. return 0;
  976. }
  977. EXPORT_SYMBOL(b53_vlan_prepare);
  978. void b53_vlan_add(struct dsa_switch *ds, int port,
  979. const struct switchdev_obj_port_vlan *vlan)
  980. {
  981. struct b53_device *dev = ds->priv;
  982. bool untagged = vlan->flags & BRIDGE_VLAN_INFO_UNTAGGED;
  983. bool pvid = vlan->flags & BRIDGE_VLAN_INFO_PVID;
  984. struct b53_vlan *vl;
  985. u16 vid;
  986. for (vid = vlan->vid_begin; vid <= vlan->vid_end; ++vid) {
  987. vl = &dev->vlans[vid];
  988. b53_get_vlan_entry(dev, vid, vl);
  989. if (vid == 0 && vid == b53_default_pvid(dev))
  990. untagged = true;
  991. vl->members |= BIT(port);
  992. if (untagged && !dsa_is_cpu_port(ds, port))
  993. vl->untag |= BIT(port);
  994. else
  995. vl->untag &= ~BIT(port);
  996. b53_set_vlan_entry(dev, vid, vl);
  997. b53_fast_age_vlan(dev, vid);
  998. }
  999. if (pvid && !dsa_is_cpu_port(ds, port)) {
  1000. b53_write16(dev, B53_VLAN_PAGE, B53_VLAN_PORT_DEF_TAG(port),
  1001. vlan->vid_end);
  1002. b53_fast_age_vlan(dev, vid);
  1003. }
  1004. }
  1005. EXPORT_SYMBOL(b53_vlan_add);
  1006. int b53_vlan_del(struct dsa_switch *ds, int port,
  1007. const struct switchdev_obj_port_vlan *vlan)
  1008. {
  1009. struct b53_device *dev = ds->priv;
  1010. bool untagged = vlan->flags & BRIDGE_VLAN_INFO_UNTAGGED;
  1011. struct b53_vlan *vl;
  1012. u16 vid;
  1013. u16 pvid;
  1014. b53_read16(dev, B53_VLAN_PAGE, B53_VLAN_PORT_DEF_TAG(port), &pvid);
  1015. for (vid = vlan->vid_begin; vid <= vlan->vid_end; ++vid) {
  1016. vl = &dev->vlans[vid];
  1017. b53_get_vlan_entry(dev, vid, vl);
  1018. vl->members &= ~BIT(port);
  1019. if (pvid == vid)
  1020. pvid = b53_default_pvid(dev);
  1021. if (untagged && !dsa_is_cpu_port(ds, port))
  1022. vl->untag &= ~(BIT(port));
  1023. b53_set_vlan_entry(dev, vid, vl);
  1024. b53_fast_age_vlan(dev, vid);
  1025. }
  1026. b53_write16(dev, B53_VLAN_PAGE, B53_VLAN_PORT_DEF_TAG(port), pvid);
  1027. b53_fast_age_vlan(dev, pvid);
  1028. return 0;
  1029. }
  1030. EXPORT_SYMBOL(b53_vlan_del);
  1031. /* Address Resolution Logic routines */
  1032. static int b53_arl_op_wait(struct b53_device *dev)
  1033. {
  1034. unsigned int timeout = 10;
  1035. u8 reg;
  1036. do {
  1037. b53_read8(dev, B53_ARLIO_PAGE, B53_ARLTBL_RW_CTRL, &reg);
  1038. if (!(reg & ARLTBL_START_DONE))
  1039. return 0;
  1040. usleep_range(1000, 2000);
  1041. } while (timeout--);
  1042. dev_warn(dev->dev, "timeout waiting for ARL to finish: 0x%02x\n", reg);
  1043. return -ETIMEDOUT;
  1044. }
  1045. static int b53_arl_rw_op(struct b53_device *dev, unsigned int op)
  1046. {
  1047. u8 reg;
  1048. if (op > ARLTBL_RW)
  1049. return -EINVAL;
  1050. b53_read8(dev, B53_ARLIO_PAGE, B53_ARLTBL_RW_CTRL, &reg);
  1051. reg |= ARLTBL_START_DONE;
  1052. if (op)
  1053. reg |= ARLTBL_RW;
  1054. else
  1055. reg &= ~ARLTBL_RW;
  1056. if (dev->vlan_enabled)
  1057. reg &= ~ARLTBL_IVL_SVL_SELECT;
  1058. else
  1059. reg |= ARLTBL_IVL_SVL_SELECT;
  1060. b53_write8(dev, B53_ARLIO_PAGE, B53_ARLTBL_RW_CTRL, reg);
  1061. return b53_arl_op_wait(dev);
  1062. }
  1063. static int b53_arl_read(struct b53_device *dev, u64 mac,
  1064. u16 vid, struct b53_arl_entry *ent, u8 *idx,
  1065. bool is_valid)
  1066. {
  1067. DECLARE_BITMAP(free_bins, B53_ARLTBL_MAX_BIN_ENTRIES);
  1068. unsigned int i;
  1069. int ret;
  1070. ret = b53_arl_op_wait(dev);
  1071. if (ret)
  1072. return ret;
  1073. bitmap_zero(free_bins, dev->num_arl_entries);
  1074. /* Read the bins */
  1075. for (i = 0; i < dev->num_arl_entries; i++) {
  1076. u64 mac_vid;
  1077. u32 fwd_entry;
  1078. b53_read64(dev, B53_ARLIO_PAGE,
  1079. B53_ARLTBL_MAC_VID_ENTRY(i), &mac_vid);
  1080. b53_read32(dev, B53_ARLIO_PAGE,
  1081. B53_ARLTBL_DATA_ENTRY(i), &fwd_entry);
  1082. b53_arl_to_entry(ent, mac_vid, fwd_entry);
  1083. if (!(fwd_entry & ARLTBL_VALID)) {
  1084. set_bit(i, free_bins);
  1085. continue;
  1086. }
  1087. if ((mac_vid & ARLTBL_MAC_MASK) != mac)
  1088. continue;
  1089. if (dev->vlan_enabled &&
  1090. ((mac_vid >> ARLTBL_VID_S) & ARLTBL_VID_MASK) != vid)
  1091. continue;
  1092. *idx = i;
  1093. return 0;
  1094. }
  1095. if (bitmap_weight(free_bins, dev->num_arl_entries) == 0)
  1096. return -ENOSPC;
  1097. *idx = find_first_bit(free_bins, dev->num_arl_entries);
  1098. return -ENOENT;
  1099. }
  1100. static int b53_arl_op(struct b53_device *dev, int op, int port,
  1101. const unsigned char *addr, u16 vid, bool is_valid)
  1102. {
  1103. struct b53_arl_entry ent;
  1104. u32 fwd_entry;
  1105. u64 mac, mac_vid = 0;
  1106. u8 idx = 0;
  1107. int ret;
  1108. /* Convert the array into a 64-bit MAC */
  1109. mac = ether_addr_to_u64(addr);
  1110. /* Perform a read for the given MAC and VID */
  1111. b53_write48(dev, B53_ARLIO_PAGE, B53_MAC_ADDR_IDX, mac);
  1112. b53_write16(dev, B53_ARLIO_PAGE, B53_VLAN_ID_IDX, vid);
  1113. /* Issue a read operation for this MAC */
  1114. ret = b53_arl_rw_op(dev, 1);
  1115. if (ret)
  1116. return ret;
  1117. ret = b53_arl_read(dev, mac, vid, &ent, &idx, is_valid);
  1118. /* If this is a read, just finish now */
  1119. if (op)
  1120. return ret;
  1121. switch (ret) {
  1122. case -ETIMEDOUT:
  1123. return ret;
  1124. case -ENOSPC:
  1125. dev_dbg(dev->dev, "{%pM,%.4d} no space left in ARL\n",
  1126. addr, vid);
  1127. return is_valid ? ret : 0;
  1128. case -ENOENT:
  1129. /* We could not find a matching MAC, so reset to a new entry */
  1130. dev_dbg(dev->dev, "{%pM,%.4d} not found, using idx: %d\n",
  1131. addr, vid, idx);
  1132. fwd_entry = 0;
  1133. break;
  1134. default:
  1135. dev_dbg(dev->dev, "{%pM,%.4d} found, using idx: %d\n",
  1136. addr, vid, idx);
  1137. break;
  1138. }
  1139. memset(&ent, 0, sizeof(ent));
  1140. ent.port = port;
  1141. ent.is_valid = is_valid;
  1142. ent.vid = vid;
  1143. ent.is_static = true;
  1144. memcpy(ent.mac, addr, ETH_ALEN);
  1145. b53_arl_from_entry(&mac_vid, &fwd_entry, &ent);
  1146. b53_write64(dev, B53_ARLIO_PAGE,
  1147. B53_ARLTBL_MAC_VID_ENTRY(idx), mac_vid);
  1148. b53_write32(dev, B53_ARLIO_PAGE,
  1149. B53_ARLTBL_DATA_ENTRY(idx), fwd_entry);
  1150. return b53_arl_rw_op(dev, 0);
  1151. }
  1152. int b53_fdb_add(struct dsa_switch *ds, int port,
  1153. const unsigned char *addr, u16 vid)
  1154. {
  1155. struct b53_device *priv = ds->priv;
  1156. /* 5325 and 5365 require some more massaging, but could
  1157. * be supported eventually
  1158. */
  1159. if (is5325(priv) || is5365(priv))
  1160. return -EOPNOTSUPP;
  1161. return b53_arl_op(priv, 0, port, addr, vid, true);
  1162. }
  1163. EXPORT_SYMBOL(b53_fdb_add);
  1164. int b53_fdb_del(struct dsa_switch *ds, int port,
  1165. const unsigned char *addr, u16 vid)
  1166. {
  1167. struct b53_device *priv = ds->priv;
  1168. return b53_arl_op(priv, 0, port, addr, vid, false);
  1169. }
  1170. EXPORT_SYMBOL(b53_fdb_del);
  1171. static int b53_arl_search_wait(struct b53_device *dev)
  1172. {
  1173. unsigned int timeout = 1000;
  1174. u8 reg;
  1175. do {
  1176. b53_read8(dev, B53_ARLIO_PAGE, B53_ARL_SRCH_CTL, &reg);
  1177. if (!(reg & ARL_SRCH_STDN))
  1178. return 0;
  1179. if (reg & ARL_SRCH_VLID)
  1180. return 0;
  1181. usleep_range(1000, 2000);
  1182. } while (timeout--);
  1183. return -ETIMEDOUT;
  1184. }
  1185. static void b53_arl_search_rd(struct b53_device *dev, u8 idx,
  1186. struct b53_arl_entry *ent)
  1187. {
  1188. u64 mac_vid;
  1189. u32 fwd_entry;
  1190. b53_read64(dev, B53_ARLIO_PAGE,
  1191. B53_ARL_SRCH_RSTL_MACVID(idx), &mac_vid);
  1192. b53_read32(dev, B53_ARLIO_PAGE,
  1193. B53_ARL_SRCH_RSTL(idx), &fwd_entry);
  1194. b53_arl_to_entry(ent, mac_vid, fwd_entry);
  1195. }
  1196. static int b53_fdb_copy(int port, const struct b53_arl_entry *ent,
  1197. dsa_fdb_dump_cb_t *cb, void *data)
  1198. {
  1199. if (!ent->is_valid)
  1200. return 0;
  1201. if (port != ent->port)
  1202. return 0;
  1203. return cb(ent->mac, ent->vid, ent->is_static, data);
  1204. }
  1205. int b53_fdb_dump(struct dsa_switch *ds, int port,
  1206. dsa_fdb_dump_cb_t *cb, void *data)
  1207. {
  1208. struct b53_device *priv = ds->priv;
  1209. struct b53_arl_entry results[2];
  1210. unsigned int count = 0;
  1211. int ret;
  1212. u8 reg;
  1213. /* Start search operation */
  1214. reg = ARL_SRCH_STDN;
  1215. b53_write8(priv, B53_ARLIO_PAGE, B53_ARL_SRCH_CTL, reg);
  1216. do {
  1217. ret = b53_arl_search_wait(priv);
  1218. if (ret)
  1219. return ret;
  1220. b53_arl_search_rd(priv, 0, &results[0]);
  1221. ret = b53_fdb_copy(port, &results[0], cb, data);
  1222. if (ret)
  1223. return ret;
  1224. if (priv->num_arl_entries > 2) {
  1225. b53_arl_search_rd(priv, 1, &results[1]);
  1226. ret = b53_fdb_copy(port, &results[1], cb, data);
  1227. if (ret)
  1228. return ret;
  1229. if (!results[0].is_valid && !results[1].is_valid)
  1230. break;
  1231. }
  1232. } while (count++ < 1024);
  1233. return 0;
  1234. }
  1235. EXPORT_SYMBOL(b53_fdb_dump);
  1236. int b53_br_join(struct dsa_switch *ds, int port, struct net_device *br)
  1237. {
  1238. struct b53_device *dev = ds->priv;
  1239. s8 cpu_port = ds->ports[port].cpu_dp->index;
  1240. u16 pvlan, reg;
  1241. unsigned int i;
  1242. /* Make this port leave the all VLANs join since we will have proper
  1243. * VLAN entries from now on
  1244. */
  1245. if (is58xx(dev)) {
  1246. b53_read16(dev, B53_VLAN_PAGE, B53_JOIN_ALL_VLAN_EN, &reg);
  1247. reg &= ~BIT(port);
  1248. if ((reg & BIT(cpu_port)) == BIT(cpu_port))
  1249. reg &= ~BIT(cpu_port);
  1250. b53_write16(dev, B53_VLAN_PAGE, B53_JOIN_ALL_VLAN_EN, reg);
  1251. }
  1252. b53_read16(dev, B53_PVLAN_PAGE, B53_PVLAN_PORT_MASK(port), &pvlan);
  1253. b53_for_each_port(dev, i) {
  1254. if (dsa_to_port(ds, i)->bridge_dev != br)
  1255. continue;
  1256. /* Add this local port to the remote port VLAN control
  1257. * membership and update the remote port bitmask
  1258. */
  1259. b53_read16(dev, B53_PVLAN_PAGE, B53_PVLAN_PORT_MASK(i), &reg);
  1260. reg |= BIT(port);
  1261. b53_write16(dev, B53_PVLAN_PAGE, B53_PVLAN_PORT_MASK(i), reg);
  1262. dev->ports[i].vlan_ctl_mask = reg;
  1263. pvlan |= BIT(i);
  1264. }
  1265. /* Configure the local port VLAN control membership to include
  1266. * remote ports and update the local port bitmask
  1267. */
  1268. b53_write16(dev, B53_PVLAN_PAGE, B53_PVLAN_PORT_MASK(port), pvlan);
  1269. dev->ports[port].vlan_ctl_mask = pvlan;
  1270. b53_port_set_learning(dev, port, true);
  1271. return 0;
  1272. }
  1273. EXPORT_SYMBOL(b53_br_join);
  1274. void b53_br_leave(struct dsa_switch *ds, int port, struct net_device *br)
  1275. {
  1276. struct b53_device *dev = ds->priv;
  1277. struct b53_vlan *vl = &dev->vlans[0];
  1278. s8 cpu_port = ds->ports[port].cpu_dp->index;
  1279. unsigned int i;
  1280. u16 pvlan, reg, pvid;
  1281. b53_read16(dev, B53_PVLAN_PAGE, B53_PVLAN_PORT_MASK(port), &pvlan);
  1282. b53_for_each_port(dev, i) {
  1283. /* Don't touch the remaining ports */
  1284. if (dsa_to_port(ds, i)->bridge_dev != br)
  1285. continue;
  1286. b53_read16(dev, B53_PVLAN_PAGE, B53_PVLAN_PORT_MASK(i), &reg);
  1287. reg &= ~BIT(port);
  1288. b53_write16(dev, B53_PVLAN_PAGE, B53_PVLAN_PORT_MASK(i), reg);
  1289. dev->ports[port].vlan_ctl_mask = reg;
  1290. /* Prevent self removal to preserve isolation */
  1291. if (port != i)
  1292. pvlan &= ~BIT(i);
  1293. }
  1294. b53_write16(dev, B53_PVLAN_PAGE, B53_PVLAN_PORT_MASK(port), pvlan);
  1295. dev->ports[port].vlan_ctl_mask = pvlan;
  1296. pvid = b53_default_pvid(dev);
  1297. /* Make this port join all VLANs without VLAN entries */
  1298. if (is58xx(dev)) {
  1299. b53_read16(dev, B53_VLAN_PAGE, B53_JOIN_ALL_VLAN_EN, &reg);
  1300. reg |= BIT(port);
  1301. if (!(reg & BIT(cpu_port)))
  1302. reg |= BIT(cpu_port);
  1303. b53_write16(dev, B53_VLAN_PAGE, B53_JOIN_ALL_VLAN_EN, reg);
  1304. } else {
  1305. b53_get_vlan_entry(dev, pvid, vl);
  1306. vl->members |= BIT(port) | BIT(cpu_port);
  1307. vl->untag |= BIT(port) | BIT(cpu_port);
  1308. b53_set_vlan_entry(dev, pvid, vl);
  1309. }
  1310. b53_port_set_learning(dev, port, false);
  1311. }
  1312. EXPORT_SYMBOL(b53_br_leave);
  1313. void b53_br_set_stp_state(struct dsa_switch *ds, int port, u8 state)
  1314. {
  1315. struct b53_device *dev = ds->priv;
  1316. u8 hw_state;
  1317. u8 reg;
  1318. switch (state) {
  1319. case BR_STATE_DISABLED:
  1320. hw_state = PORT_CTRL_DIS_STATE;
  1321. break;
  1322. case BR_STATE_LISTENING:
  1323. hw_state = PORT_CTRL_LISTEN_STATE;
  1324. break;
  1325. case BR_STATE_LEARNING:
  1326. hw_state = PORT_CTRL_LEARN_STATE;
  1327. break;
  1328. case BR_STATE_FORWARDING:
  1329. hw_state = PORT_CTRL_FWD_STATE;
  1330. break;
  1331. case BR_STATE_BLOCKING:
  1332. hw_state = PORT_CTRL_BLOCK_STATE;
  1333. break;
  1334. default:
  1335. dev_err(ds->dev, "invalid STP state: %d\n", state);
  1336. return;
  1337. }
  1338. b53_read8(dev, B53_CTRL_PAGE, B53_PORT_CTRL(port), &reg);
  1339. reg &= ~PORT_CTRL_STP_STATE_MASK;
  1340. reg |= hw_state;
  1341. b53_write8(dev, B53_CTRL_PAGE, B53_PORT_CTRL(port), reg);
  1342. }
  1343. EXPORT_SYMBOL(b53_br_set_stp_state);
  1344. void b53_br_fast_age(struct dsa_switch *ds, int port)
  1345. {
  1346. struct b53_device *dev = ds->priv;
  1347. if (b53_fast_age_port(dev, port))
  1348. dev_err(ds->dev, "fast ageing failed\n");
  1349. }
  1350. EXPORT_SYMBOL(b53_br_fast_age);
  1351. static bool b53_possible_cpu_port(struct dsa_switch *ds, int port)
  1352. {
  1353. /* Broadcom switches will accept enabling Broadcom tags on the
  1354. * following ports: 5, 7 and 8, any other port is not supported
  1355. */
  1356. switch (port) {
  1357. case B53_CPU_PORT_25:
  1358. case 7:
  1359. case B53_CPU_PORT:
  1360. return true;
  1361. }
  1362. return false;
  1363. }
  1364. static bool b53_can_enable_brcm_tags(struct dsa_switch *ds, int port)
  1365. {
  1366. bool ret = b53_possible_cpu_port(ds, port);
  1367. if (!ret)
  1368. dev_warn(ds->dev, "Port %d is not Broadcom tag capable\n",
  1369. port);
  1370. return ret;
  1371. }
  1372. enum dsa_tag_protocol b53_get_tag_protocol(struct dsa_switch *ds, int port)
  1373. {
  1374. struct b53_device *dev = ds->priv;
  1375. /* Older models (5325, 5365) support a different tag format that we do
  1376. * not support in net/dsa/tag_brcm.c yet. 539x and 531x5 require managed
  1377. * mode to be turned on which means we need to specifically manage ARL
  1378. * misses on multicast addresses (TBD).
  1379. */
  1380. if (is5325(dev) || is5365(dev) || is539x(dev) || is531x5(dev) ||
  1381. !b53_can_enable_brcm_tags(ds, port))
  1382. return DSA_TAG_PROTO_NONE;
  1383. /* Broadcom BCM58xx chips have a flow accelerator on Port 8
  1384. * which requires us to use the prepended Broadcom tag type
  1385. */
  1386. if (dev->chip_id == BCM58XX_DEVICE_ID && port == B53_CPU_PORT)
  1387. return DSA_TAG_PROTO_BRCM_PREPEND;
  1388. return DSA_TAG_PROTO_BRCM;
  1389. }
  1390. EXPORT_SYMBOL(b53_get_tag_protocol);
  1391. int b53_mirror_add(struct dsa_switch *ds, int port,
  1392. struct dsa_mall_mirror_tc_entry *mirror, bool ingress)
  1393. {
  1394. struct b53_device *dev = ds->priv;
  1395. u16 reg, loc;
  1396. if (ingress)
  1397. loc = B53_IG_MIR_CTL;
  1398. else
  1399. loc = B53_EG_MIR_CTL;
  1400. b53_read16(dev, B53_MGMT_PAGE, loc, &reg);
  1401. reg |= BIT(port);
  1402. b53_write16(dev, B53_MGMT_PAGE, loc, reg);
  1403. b53_read16(dev, B53_MGMT_PAGE, B53_MIR_CAP_CTL, &reg);
  1404. reg &= ~CAP_PORT_MASK;
  1405. reg |= mirror->to_local_port;
  1406. reg |= MIRROR_EN;
  1407. b53_write16(dev, B53_MGMT_PAGE, B53_MIR_CAP_CTL, reg);
  1408. return 0;
  1409. }
  1410. EXPORT_SYMBOL(b53_mirror_add);
  1411. void b53_mirror_del(struct dsa_switch *ds, int port,
  1412. struct dsa_mall_mirror_tc_entry *mirror)
  1413. {
  1414. struct b53_device *dev = ds->priv;
  1415. bool loc_disable = false, other_loc_disable = false;
  1416. u16 reg, loc;
  1417. if (mirror->ingress)
  1418. loc = B53_IG_MIR_CTL;
  1419. else
  1420. loc = B53_EG_MIR_CTL;
  1421. /* Update the desired ingress/egress register */
  1422. b53_read16(dev, B53_MGMT_PAGE, loc, &reg);
  1423. reg &= ~BIT(port);
  1424. if (!(reg & MIRROR_MASK))
  1425. loc_disable = true;
  1426. b53_write16(dev, B53_MGMT_PAGE, loc, reg);
  1427. /* Now look at the other one to know if we can disable mirroring
  1428. * entirely
  1429. */
  1430. if (mirror->ingress)
  1431. b53_read16(dev, B53_MGMT_PAGE, B53_EG_MIR_CTL, &reg);
  1432. else
  1433. b53_read16(dev, B53_MGMT_PAGE, B53_IG_MIR_CTL, &reg);
  1434. if (!(reg & MIRROR_MASK))
  1435. other_loc_disable = true;
  1436. b53_read16(dev, B53_MGMT_PAGE, B53_MIR_CAP_CTL, &reg);
  1437. /* Both no longer have ports, let's disable mirroring */
  1438. if (loc_disable && other_loc_disable) {
  1439. reg &= ~MIRROR_EN;
  1440. reg &= ~mirror->to_local_port;
  1441. }
  1442. b53_write16(dev, B53_MGMT_PAGE, B53_MIR_CAP_CTL, reg);
  1443. }
  1444. EXPORT_SYMBOL(b53_mirror_del);
  1445. void b53_eee_enable_set(struct dsa_switch *ds, int port, bool enable)
  1446. {
  1447. struct b53_device *dev = ds->priv;
  1448. u16 reg;
  1449. b53_read16(dev, B53_EEE_PAGE, B53_EEE_EN_CTRL, &reg);
  1450. if (enable)
  1451. reg |= BIT(port);
  1452. else
  1453. reg &= ~BIT(port);
  1454. b53_write16(dev, B53_EEE_PAGE, B53_EEE_EN_CTRL, reg);
  1455. }
  1456. EXPORT_SYMBOL(b53_eee_enable_set);
  1457. /* Returns 0 if EEE was not enabled, or 1 otherwise
  1458. */
  1459. int b53_eee_init(struct dsa_switch *ds, int port, struct phy_device *phy)
  1460. {
  1461. int ret;
  1462. ret = phy_init_eee(phy, 0);
  1463. if (ret)
  1464. return 0;
  1465. b53_eee_enable_set(ds, port, true);
  1466. return 1;
  1467. }
  1468. EXPORT_SYMBOL(b53_eee_init);
  1469. int b53_get_mac_eee(struct dsa_switch *ds, int port, struct ethtool_eee *e)
  1470. {
  1471. struct b53_device *dev = ds->priv;
  1472. struct ethtool_eee *p = &dev->ports[port].eee;
  1473. u16 reg;
  1474. if (is5325(dev) || is5365(dev))
  1475. return -EOPNOTSUPP;
  1476. b53_read16(dev, B53_EEE_PAGE, B53_EEE_LPI_INDICATE, &reg);
  1477. e->eee_enabled = p->eee_enabled;
  1478. e->eee_active = !!(reg & BIT(port));
  1479. return 0;
  1480. }
  1481. EXPORT_SYMBOL(b53_get_mac_eee);
  1482. int b53_set_mac_eee(struct dsa_switch *ds, int port, struct ethtool_eee *e)
  1483. {
  1484. struct b53_device *dev = ds->priv;
  1485. struct ethtool_eee *p = &dev->ports[port].eee;
  1486. if (is5325(dev) || is5365(dev))
  1487. return -EOPNOTSUPP;
  1488. p->eee_enabled = e->eee_enabled;
  1489. b53_eee_enable_set(ds, port, e->eee_enabled);
  1490. return 0;
  1491. }
  1492. EXPORT_SYMBOL(b53_set_mac_eee);
  1493. static const struct dsa_switch_ops b53_switch_ops = {
  1494. .get_tag_protocol = b53_get_tag_protocol,
  1495. .setup = b53_setup,
  1496. .get_strings = b53_get_strings,
  1497. .get_ethtool_stats = b53_get_ethtool_stats,
  1498. .get_sset_count = b53_get_sset_count,
  1499. .get_ethtool_phy_stats = b53_get_ethtool_phy_stats,
  1500. .phy_read = b53_phy_read16,
  1501. .phy_write = b53_phy_write16,
  1502. .adjust_link = b53_adjust_link,
  1503. .port_enable = b53_enable_port,
  1504. .port_disable = b53_disable_port,
  1505. .get_mac_eee = b53_get_mac_eee,
  1506. .set_mac_eee = b53_set_mac_eee,
  1507. .port_bridge_join = b53_br_join,
  1508. .port_bridge_leave = b53_br_leave,
  1509. .port_stp_state_set = b53_br_set_stp_state,
  1510. .port_fast_age = b53_br_fast_age,
  1511. .port_vlan_filtering = b53_vlan_filtering,
  1512. .port_vlan_prepare = b53_vlan_prepare,
  1513. .port_vlan_add = b53_vlan_add,
  1514. .port_vlan_del = b53_vlan_del,
  1515. .port_fdb_dump = b53_fdb_dump,
  1516. .port_fdb_add = b53_fdb_add,
  1517. .port_fdb_del = b53_fdb_del,
  1518. .port_mirror_add = b53_mirror_add,
  1519. .port_mirror_del = b53_mirror_del,
  1520. };
  1521. struct b53_chip_data {
  1522. u32 chip_id;
  1523. const char *dev_name;
  1524. u16 vlans;
  1525. u16 enabled_ports;
  1526. u8 cpu_port;
  1527. u8 vta_regs[3];
  1528. u8 arl_entries;
  1529. u8 duplex_reg;
  1530. u8 jumbo_pm_reg;
  1531. u8 jumbo_size_reg;
  1532. };
  1533. #define B53_VTA_REGS \
  1534. { B53_VT_ACCESS, B53_VT_INDEX, B53_VT_ENTRY }
  1535. #define B53_VTA_REGS_9798 \
  1536. { B53_VT_ACCESS_9798, B53_VT_INDEX_9798, B53_VT_ENTRY_9798 }
  1537. #define B53_VTA_REGS_63XX \
  1538. { B53_VT_ACCESS_63XX, B53_VT_INDEX_63XX, B53_VT_ENTRY_63XX }
  1539. static const struct b53_chip_data b53_switch_chips[] = {
  1540. {
  1541. .chip_id = BCM5325_DEVICE_ID,
  1542. .dev_name = "BCM5325",
  1543. .vlans = 16,
  1544. .enabled_ports = 0x1f,
  1545. .arl_entries = 2,
  1546. .cpu_port = B53_CPU_PORT_25,
  1547. .duplex_reg = B53_DUPLEX_STAT_FE,
  1548. },
  1549. {
  1550. .chip_id = BCM5365_DEVICE_ID,
  1551. .dev_name = "BCM5365",
  1552. .vlans = 256,
  1553. .enabled_ports = 0x1f,
  1554. .arl_entries = 2,
  1555. .cpu_port = B53_CPU_PORT_25,
  1556. .duplex_reg = B53_DUPLEX_STAT_FE,
  1557. },
  1558. {
  1559. .chip_id = BCM5389_DEVICE_ID,
  1560. .dev_name = "BCM5389",
  1561. .vlans = 4096,
  1562. .enabled_ports = 0x1f,
  1563. .arl_entries = 4,
  1564. .cpu_port = B53_CPU_PORT,
  1565. .vta_regs = B53_VTA_REGS,
  1566. .duplex_reg = B53_DUPLEX_STAT_GE,
  1567. .jumbo_pm_reg = B53_JUMBO_PORT_MASK,
  1568. .jumbo_size_reg = B53_JUMBO_MAX_SIZE,
  1569. },
  1570. {
  1571. .chip_id = BCM5395_DEVICE_ID,
  1572. .dev_name = "BCM5395",
  1573. .vlans = 4096,
  1574. .enabled_ports = 0x1f,
  1575. .arl_entries = 4,
  1576. .cpu_port = B53_CPU_PORT,
  1577. .vta_regs = B53_VTA_REGS,
  1578. .duplex_reg = B53_DUPLEX_STAT_GE,
  1579. .jumbo_pm_reg = B53_JUMBO_PORT_MASK,
  1580. .jumbo_size_reg = B53_JUMBO_MAX_SIZE,
  1581. },
  1582. {
  1583. .chip_id = BCM5397_DEVICE_ID,
  1584. .dev_name = "BCM5397",
  1585. .vlans = 4096,
  1586. .enabled_ports = 0x1f,
  1587. .arl_entries = 4,
  1588. .cpu_port = B53_CPU_PORT,
  1589. .vta_regs = B53_VTA_REGS_9798,
  1590. .duplex_reg = B53_DUPLEX_STAT_GE,
  1591. .jumbo_pm_reg = B53_JUMBO_PORT_MASK,
  1592. .jumbo_size_reg = B53_JUMBO_MAX_SIZE,
  1593. },
  1594. {
  1595. .chip_id = BCM5398_DEVICE_ID,
  1596. .dev_name = "BCM5398",
  1597. .vlans = 4096,
  1598. .enabled_ports = 0x7f,
  1599. .arl_entries = 4,
  1600. .cpu_port = B53_CPU_PORT,
  1601. .vta_regs = B53_VTA_REGS_9798,
  1602. .duplex_reg = B53_DUPLEX_STAT_GE,
  1603. .jumbo_pm_reg = B53_JUMBO_PORT_MASK,
  1604. .jumbo_size_reg = B53_JUMBO_MAX_SIZE,
  1605. },
  1606. {
  1607. .chip_id = BCM53115_DEVICE_ID,
  1608. .dev_name = "BCM53115",
  1609. .vlans = 4096,
  1610. .enabled_ports = 0x1f,
  1611. .arl_entries = 4,
  1612. .vta_regs = B53_VTA_REGS,
  1613. .cpu_port = B53_CPU_PORT,
  1614. .duplex_reg = B53_DUPLEX_STAT_GE,
  1615. .jumbo_pm_reg = B53_JUMBO_PORT_MASK,
  1616. .jumbo_size_reg = B53_JUMBO_MAX_SIZE,
  1617. },
  1618. {
  1619. .chip_id = BCM53125_DEVICE_ID,
  1620. .dev_name = "BCM53125",
  1621. .vlans = 4096,
  1622. .enabled_ports = 0xff,
  1623. .arl_entries = 4,
  1624. .cpu_port = B53_CPU_PORT,
  1625. .vta_regs = B53_VTA_REGS,
  1626. .duplex_reg = B53_DUPLEX_STAT_GE,
  1627. .jumbo_pm_reg = B53_JUMBO_PORT_MASK,
  1628. .jumbo_size_reg = B53_JUMBO_MAX_SIZE,
  1629. },
  1630. {
  1631. .chip_id = BCM53128_DEVICE_ID,
  1632. .dev_name = "BCM53128",
  1633. .vlans = 4096,
  1634. .enabled_ports = 0x1ff,
  1635. .arl_entries = 4,
  1636. .cpu_port = B53_CPU_PORT,
  1637. .vta_regs = B53_VTA_REGS,
  1638. .duplex_reg = B53_DUPLEX_STAT_GE,
  1639. .jumbo_pm_reg = B53_JUMBO_PORT_MASK,
  1640. .jumbo_size_reg = B53_JUMBO_MAX_SIZE,
  1641. },
  1642. {
  1643. .chip_id = BCM63XX_DEVICE_ID,
  1644. .dev_name = "BCM63xx",
  1645. .vlans = 4096,
  1646. .enabled_ports = 0, /* pdata must provide them */
  1647. .arl_entries = 4,
  1648. .cpu_port = B53_CPU_PORT,
  1649. .vta_regs = B53_VTA_REGS_63XX,
  1650. .duplex_reg = B53_DUPLEX_STAT_63XX,
  1651. .jumbo_pm_reg = B53_JUMBO_PORT_MASK_63XX,
  1652. .jumbo_size_reg = B53_JUMBO_MAX_SIZE_63XX,
  1653. },
  1654. {
  1655. .chip_id = BCM53010_DEVICE_ID,
  1656. .dev_name = "BCM53010",
  1657. .vlans = 4096,
  1658. .enabled_ports = 0x1f,
  1659. .arl_entries = 4,
  1660. .cpu_port = B53_CPU_PORT_25, /* TODO: auto detect */
  1661. .vta_regs = B53_VTA_REGS,
  1662. .duplex_reg = B53_DUPLEX_STAT_GE,
  1663. .jumbo_pm_reg = B53_JUMBO_PORT_MASK,
  1664. .jumbo_size_reg = B53_JUMBO_MAX_SIZE,
  1665. },
  1666. {
  1667. .chip_id = BCM53011_DEVICE_ID,
  1668. .dev_name = "BCM53011",
  1669. .vlans = 4096,
  1670. .enabled_ports = 0x1bf,
  1671. .arl_entries = 4,
  1672. .cpu_port = B53_CPU_PORT_25, /* TODO: auto detect */
  1673. .vta_regs = B53_VTA_REGS,
  1674. .duplex_reg = B53_DUPLEX_STAT_GE,
  1675. .jumbo_pm_reg = B53_JUMBO_PORT_MASK,
  1676. .jumbo_size_reg = B53_JUMBO_MAX_SIZE,
  1677. },
  1678. {
  1679. .chip_id = BCM53012_DEVICE_ID,
  1680. .dev_name = "BCM53012",
  1681. .vlans = 4096,
  1682. .enabled_ports = 0x1bf,
  1683. .arl_entries = 4,
  1684. .cpu_port = B53_CPU_PORT_25, /* TODO: auto detect */
  1685. .vta_regs = B53_VTA_REGS,
  1686. .duplex_reg = B53_DUPLEX_STAT_GE,
  1687. .jumbo_pm_reg = B53_JUMBO_PORT_MASK,
  1688. .jumbo_size_reg = B53_JUMBO_MAX_SIZE,
  1689. },
  1690. {
  1691. .chip_id = BCM53018_DEVICE_ID,
  1692. .dev_name = "BCM53018",
  1693. .vlans = 4096,
  1694. .enabled_ports = 0x1f,
  1695. .arl_entries = 4,
  1696. .cpu_port = B53_CPU_PORT_25, /* TODO: auto detect */
  1697. .vta_regs = B53_VTA_REGS,
  1698. .duplex_reg = B53_DUPLEX_STAT_GE,
  1699. .jumbo_pm_reg = B53_JUMBO_PORT_MASK,
  1700. .jumbo_size_reg = B53_JUMBO_MAX_SIZE,
  1701. },
  1702. {
  1703. .chip_id = BCM53019_DEVICE_ID,
  1704. .dev_name = "BCM53019",
  1705. .vlans = 4096,
  1706. .enabled_ports = 0x1f,
  1707. .arl_entries = 4,
  1708. .cpu_port = B53_CPU_PORT_25, /* TODO: auto detect */
  1709. .vta_regs = B53_VTA_REGS,
  1710. .duplex_reg = B53_DUPLEX_STAT_GE,
  1711. .jumbo_pm_reg = B53_JUMBO_PORT_MASK,
  1712. .jumbo_size_reg = B53_JUMBO_MAX_SIZE,
  1713. },
  1714. {
  1715. .chip_id = BCM58XX_DEVICE_ID,
  1716. .dev_name = "BCM585xx/586xx/88312",
  1717. .vlans = 4096,
  1718. .enabled_ports = 0x1ff,
  1719. .arl_entries = 4,
  1720. .cpu_port = B53_CPU_PORT,
  1721. .vta_regs = B53_VTA_REGS,
  1722. .duplex_reg = B53_DUPLEX_STAT_GE,
  1723. .jumbo_pm_reg = B53_JUMBO_PORT_MASK,
  1724. .jumbo_size_reg = B53_JUMBO_MAX_SIZE,
  1725. },
  1726. {
  1727. .chip_id = BCM583XX_DEVICE_ID,
  1728. .dev_name = "BCM583xx/11360",
  1729. .vlans = 4096,
  1730. .enabled_ports = 0x103,
  1731. .arl_entries = 4,
  1732. .cpu_port = B53_CPU_PORT,
  1733. .vta_regs = B53_VTA_REGS,
  1734. .duplex_reg = B53_DUPLEX_STAT_GE,
  1735. .jumbo_pm_reg = B53_JUMBO_PORT_MASK,
  1736. .jumbo_size_reg = B53_JUMBO_MAX_SIZE,
  1737. },
  1738. {
  1739. .chip_id = BCM7445_DEVICE_ID,
  1740. .dev_name = "BCM7445",
  1741. .vlans = 4096,
  1742. .enabled_ports = 0x1ff,
  1743. .arl_entries = 4,
  1744. .cpu_port = B53_CPU_PORT,
  1745. .vta_regs = B53_VTA_REGS,
  1746. .duplex_reg = B53_DUPLEX_STAT_GE,
  1747. .jumbo_pm_reg = B53_JUMBO_PORT_MASK,
  1748. .jumbo_size_reg = B53_JUMBO_MAX_SIZE,
  1749. },
  1750. {
  1751. .chip_id = BCM7278_DEVICE_ID,
  1752. .dev_name = "BCM7278",
  1753. .vlans = 4096,
  1754. .enabled_ports = 0x1ff,
  1755. .arl_entries= 4,
  1756. .cpu_port = B53_CPU_PORT,
  1757. .vta_regs = B53_VTA_REGS,
  1758. .duplex_reg = B53_DUPLEX_STAT_GE,
  1759. .jumbo_pm_reg = B53_JUMBO_PORT_MASK,
  1760. .jumbo_size_reg = B53_JUMBO_MAX_SIZE,
  1761. },
  1762. };
  1763. static int b53_switch_init(struct b53_device *dev)
  1764. {
  1765. unsigned int i;
  1766. int ret;
  1767. for (i = 0; i < ARRAY_SIZE(b53_switch_chips); i++) {
  1768. const struct b53_chip_data *chip = &b53_switch_chips[i];
  1769. if (chip->chip_id == dev->chip_id) {
  1770. if (!dev->enabled_ports)
  1771. dev->enabled_ports = chip->enabled_ports;
  1772. dev->name = chip->dev_name;
  1773. dev->duplex_reg = chip->duplex_reg;
  1774. dev->vta_regs[0] = chip->vta_regs[0];
  1775. dev->vta_regs[1] = chip->vta_regs[1];
  1776. dev->vta_regs[2] = chip->vta_regs[2];
  1777. dev->jumbo_pm_reg = chip->jumbo_pm_reg;
  1778. dev->cpu_port = chip->cpu_port;
  1779. dev->num_vlans = chip->vlans;
  1780. dev->num_arl_entries = chip->arl_entries;
  1781. break;
  1782. }
  1783. }
  1784. /* check which BCM5325x version we have */
  1785. if (is5325(dev)) {
  1786. u8 vc4;
  1787. b53_read8(dev, B53_VLAN_PAGE, B53_VLAN_CTRL4_25, &vc4);
  1788. /* check reserved bits */
  1789. switch (vc4 & 3) {
  1790. case 1:
  1791. /* BCM5325E */
  1792. break;
  1793. case 3:
  1794. /* BCM5325F - do not use port 4 */
  1795. dev->enabled_ports &= ~BIT(4);
  1796. break;
  1797. default:
  1798. /* On the BCM47XX SoCs this is the supported internal switch.*/
  1799. #ifndef CONFIG_BCM47XX
  1800. /* BCM5325M */
  1801. return -EINVAL;
  1802. #else
  1803. break;
  1804. #endif
  1805. }
  1806. } else if (dev->chip_id == BCM53115_DEVICE_ID) {
  1807. u64 strap_value;
  1808. b53_read48(dev, B53_STAT_PAGE, B53_STRAP_VALUE, &strap_value);
  1809. /* use second IMP port if GMII is enabled */
  1810. if (strap_value & SV_GMII_CTRL_115)
  1811. dev->cpu_port = 5;
  1812. }
  1813. /* cpu port is always last */
  1814. dev->num_ports = dev->cpu_port + 1;
  1815. dev->enabled_ports |= BIT(dev->cpu_port);
  1816. /* Include non standard CPU port built-in PHYs to be probed */
  1817. if (is539x(dev) || is531x5(dev)) {
  1818. for (i = 0; i < dev->num_ports; i++) {
  1819. if (!(dev->ds->phys_mii_mask & BIT(i)) &&
  1820. !b53_possible_cpu_port(dev->ds, i))
  1821. dev->ds->phys_mii_mask |= BIT(i);
  1822. }
  1823. }
  1824. dev->ports = devm_kcalloc(dev->dev,
  1825. dev->num_ports, sizeof(struct b53_port),
  1826. GFP_KERNEL);
  1827. if (!dev->ports)
  1828. return -ENOMEM;
  1829. dev->vlans = devm_kcalloc(dev->dev,
  1830. dev->num_vlans, sizeof(struct b53_vlan),
  1831. GFP_KERNEL);
  1832. if (!dev->vlans)
  1833. return -ENOMEM;
  1834. dev->reset_gpio = b53_switch_get_reset_gpio(dev);
  1835. if (dev->reset_gpio >= 0) {
  1836. ret = devm_gpio_request_one(dev->dev, dev->reset_gpio,
  1837. GPIOF_OUT_INIT_HIGH, "robo_reset");
  1838. if (ret)
  1839. return ret;
  1840. }
  1841. return 0;
  1842. }
  1843. struct b53_device *b53_switch_alloc(struct device *base,
  1844. const struct b53_io_ops *ops,
  1845. void *priv)
  1846. {
  1847. struct dsa_switch *ds;
  1848. struct b53_device *dev;
  1849. ds = dsa_switch_alloc(base, DSA_MAX_PORTS);
  1850. if (!ds)
  1851. return NULL;
  1852. dev = devm_kzalloc(base, sizeof(*dev), GFP_KERNEL);
  1853. if (!dev)
  1854. return NULL;
  1855. ds->priv = dev;
  1856. dev->dev = base;
  1857. dev->ds = ds;
  1858. dev->priv = priv;
  1859. dev->ops = ops;
  1860. ds->ops = &b53_switch_ops;
  1861. mutex_init(&dev->reg_mutex);
  1862. mutex_init(&dev->stats_mutex);
  1863. return dev;
  1864. }
  1865. EXPORT_SYMBOL(b53_switch_alloc);
  1866. int b53_switch_detect(struct b53_device *dev)
  1867. {
  1868. u32 id32;
  1869. u16 tmp;
  1870. u8 id8;
  1871. int ret;
  1872. ret = b53_read8(dev, B53_MGMT_PAGE, B53_DEVICE_ID, &id8);
  1873. if (ret)
  1874. return ret;
  1875. switch (id8) {
  1876. case 0:
  1877. /* BCM5325 and BCM5365 do not have this register so reads
  1878. * return 0. But the read operation did succeed, so assume this
  1879. * is one of them.
  1880. *
  1881. * Next check if we can write to the 5325's VTA register; for
  1882. * 5365 it is read only.
  1883. */
  1884. b53_write16(dev, B53_VLAN_PAGE, B53_VLAN_TABLE_ACCESS_25, 0xf);
  1885. b53_read16(dev, B53_VLAN_PAGE, B53_VLAN_TABLE_ACCESS_25, &tmp);
  1886. if (tmp == 0xf)
  1887. dev->chip_id = BCM5325_DEVICE_ID;
  1888. else
  1889. dev->chip_id = BCM5365_DEVICE_ID;
  1890. break;
  1891. case BCM5389_DEVICE_ID:
  1892. case BCM5395_DEVICE_ID:
  1893. case BCM5397_DEVICE_ID:
  1894. case BCM5398_DEVICE_ID:
  1895. dev->chip_id = id8;
  1896. break;
  1897. default:
  1898. ret = b53_read32(dev, B53_MGMT_PAGE, B53_DEVICE_ID, &id32);
  1899. if (ret)
  1900. return ret;
  1901. switch (id32) {
  1902. case BCM53115_DEVICE_ID:
  1903. case BCM53125_DEVICE_ID:
  1904. case BCM53128_DEVICE_ID:
  1905. case BCM53010_DEVICE_ID:
  1906. case BCM53011_DEVICE_ID:
  1907. case BCM53012_DEVICE_ID:
  1908. case BCM53018_DEVICE_ID:
  1909. case BCM53019_DEVICE_ID:
  1910. dev->chip_id = id32;
  1911. break;
  1912. default:
  1913. pr_err("unsupported switch detected (BCM53%02x/BCM%x)\n",
  1914. id8, id32);
  1915. return -ENODEV;
  1916. }
  1917. }
  1918. if (dev->chip_id == BCM5325_DEVICE_ID)
  1919. return b53_read8(dev, B53_STAT_PAGE, B53_REV_ID_25,
  1920. &dev->core_rev);
  1921. else
  1922. return b53_read8(dev, B53_MGMT_PAGE, B53_REV_ID,
  1923. &dev->core_rev);
  1924. }
  1925. EXPORT_SYMBOL(b53_switch_detect);
  1926. int b53_switch_register(struct b53_device *dev)
  1927. {
  1928. int ret;
  1929. if (dev->pdata) {
  1930. dev->chip_id = dev->pdata->chip_id;
  1931. dev->enabled_ports = dev->pdata->enabled_ports;
  1932. }
  1933. if (!dev->chip_id && b53_switch_detect(dev))
  1934. return -EINVAL;
  1935. ret = b53_switch_init(dev);
  1936. if (ret)
  1937. return ret;
  1938. pr_info("found switch: %s, rev %i\n", dev->name, dev->core_rev);
  1939. return dsa_register_switch(dev->ds);
  1940. }
  1941. EXPORT_SYMBOL(b53_switch_register);
  1942. MODULE_AUTHOR("Jonas Gorski <jogo@openwrt.org>");
  1943. MODULE_DESCRIPTION("B53 switch library");
  1944. MODULE_LICENSE("Dual BSD/GPL");