lantiq_gswip.c 64 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Lantiq / Intel GSWIP switch driver for VRX200, xRX300 and xRX330 SoCs
  4. *
  5. * Copyright (C) 2010 Lantiq Deutschland
  6. * Copyright (C) 2012 John Crispin <john@phrozen.org>
  7. * Copyright (C) 2017 - 2019 Hauke Mehrtens <hauke@hauke-m.de>
  8. *
  9. * The VLAN and bridge model the GSWIP hardware uses does not directly
  10. * matches the model DSA uses.
  11. *
  12. * The hardware has 64 possible table entries for bridges with one VLAN
  13. * ID, one flow id and a list of ports for each bridge. All entries which
  14. * match the same flow ID are combined in the mac learning table, they
  15. * act as one global bridge.
  16. * The hardware does not support VLAN filter on the port, but on the
  17. * bridge, this driver converts the DSA model to the hardware.
  18. *
  19. * The CPU gets all the exception frames which do not match any forwarding
  20. * rule and the CPU port is also added to all bridges. This makes it possible
  21. * to handle all the special cases easily in software.
  22. * At the initialization the driver allocates one bridge table entry for
  23. * each switch port which is used when the port is used without an
  24. * explicit bridge. This prevents the frames from being forwarded
  25. * between all LAN ports by default.
  26. */
  27. #include <linux/clk.h>
  28. #include <linux/delay.h>
  29. #include <linux/etherdevice.h>
  30. #include <linux/firmware.h>
  31. #include <linux/if_bridge.h>
  32. #include <linux/if_vlan.h>
  33. #include <linux/iopoll.h>
  34. #include <linux/mfd/syscon.h>
  35. #include <linux/module.h>
  36. #include <linux/of_mdio.h>
  37. #include <linux/of_net.h>
  38. #include <linux/of_platform.h>
  39. #include <linux/phy.h>
  40. #include <linux/phylink.h>
  41. #include <linux/platform_device.h>
  42. #include <linux/regmap.h>
  43. #include <linux/reset.h>
  44. #include <net/dsa.h>
  45. #include <dt-bindings/mips/lantiq_rcu_gphy.h>
  46. #include "lantiq_pce.h"
  47. /* GSWIP MDIO Registers */
  48. #define GSWIP_MDIO_GLOB 0x00
  49. #define GSWIP_MDIO_GLOB_ENABLE BIT(15)
  50. #define GSWIP_MDIO_CTRL 0x08
  51. #define GSWIP_MDIO_CTRL_BUSY BIT(12)
  52. #define GSWIP_MDIO_CTRL_RD BIT(11)
  53. #define GSWIP_MDIO_CTRL_WR BIT(10)
  54. #define GSWIP_MDIO_CTRL_PHYAD_MASK 0x1f
  55. #define GSWIP_MDIO_CTRL_PHYAD_SHIFT 5
  56. #define GSWIP_MDIO_CTRL_REGAD_MASK 0x1f
  57. #define GSWIP_MDIO_READ 0x09
  58. #define GSWIP_MDIO_WRITE 0x0A
  59. #define GSWIP_MDIO_MDC_CFG0 0x0B
  60. #define GSWIP_MDIO_MDC_CFG1 0x0C
  61. #define GSWIP_MDIO_PHYp(p) (0x15 - (p))
  62. #define GSWIP_MDIO_PHY_LINK_MASK 0x6000
  63. #define GSWIP_MDIO_PHY_LINK_AUTO 0x0000
  64. #define GSWIP_MDIO_PHY_LINK_DOWN 0x4000
  65. #define GSWIP_MDIO_PHY_LINK_UP 0x2000
  66. #define GSWIP_MDIO_PHY_SPEED_MASK 0x1800
  67. #define GSWIP_MDIO_PHY_SPEED_AUTO 0x1800
  68. #define GSWIP_MDIO_PHY_SPEED_M10 0x0000
  69. #define GSWIP_MDIO_PHY_SPEED_M100 0x0800
  70. #define GSWIP_MDIO_PHY_SPEED_G1 0x1000
  71. #define GSWIP_MDIO_PHY_FDUP_MASK 0x0600
  72. #define GSWIP_MDIO_PHY_FDUP_AUTO 0x0000
  73. #define GSWIP_MDIO_PHY_FDUP_EN 0x0200
  74. #define GSWIP_MDIO_PHY_FDUP_DIS 0x0600
  75. #define GSWIP_MDIO_PHY_FCONTX_MASK 0x0180
  76. #define GSWIP_MDIO_PHY_FCONTX_AUTO 0x0000
  77. #define GSWIP_MDIO_PHY_FCONTX_EN 0x0100
  78. #define GSWIP_MDIO_PHY_FCONTX_DIS 0x0180
  79. #define GSWIP_MDIO_PHY_FCONRX_MASK 0x0060
  80. #define GSWIP_MDIO_PHY_FCONRX_AUTO 0x0000
  81. #define GSWIP_MDIO_PHY_FCONRX_EN 0x0020
  82. #define GSWIP_MDIO_PHY_FCONRX_DIS 0x0060
  83. #define GSWIP_MDIO_PHY_ADDR_MASK 0x001f
  84. #define GSWIP_MDIO_PHY_MASK (GSWIP_MDIO_PHY_ADDR_MASK | \
  85. GSWIP_MDIO_PHY_FCONRX_MASK | \
  86. GSWIP_MDIO_PHY_FCONTX_MASK | \
  87. GSWIP_MDIO_PHY_LINK_MASK | \
  88. GSWIP_MDIO_PHY_SPEED_MASK | \
  89. GSWIP_MDIO_PHY_FDUP_MASK)
  90. /* GSWIP MII Registers */
  91. #define GSWIP_MII_CFGp(p) (0x2 * (p))
  92. #define GSWIP_MII_CFG_RESET BIT(15)
  93. #define GSWIP_MII_CFG_EN BIT(14)
  94. #define GSWIP_MII_CFG_ISOLATE BIT(13)
  95. #define GSWIP_MII_CFG_LDCLKDIS BIT(12)
  96. #define GSWIP_MII_CFG_RGMII_IBS BIT(8)
  97. #define GSWIP_MII_CFG_RMII_CLK BIT(7)
  98. #define GSWIP_MII_CFG_MODE_MIIP 0x0
  99. #define GSWIP_MII_CFG_MODE_MIIM 0x1
  100. #define GSWIP_MII_CFG_MODE_RMIIP 0x2
  101. #define GSWIP_MII_CFG_MODE_RMIIM 0x3
  102. #define GSWIP_MII_CFG_MODE_RGMII 0x4
  103. #define GSWIP_MII_CFG_MODE_GMII 0x9
  104. #define GSWIP_MII_CFG_MODE_MASK 0xf
  105. #define GSWIP_MII_CFG_RATE_M2P5 0x00
  106. #define GSWIP_MII_CFG_RATE_M25 0x10
  107. #define GSWIP_MII_CFG_RATE_M125 0x20
  108. #define GSWIP_MII_CFG_RATE_M50 0x30
  109. #define GSWIP_MII_CFG_RATE_AUTO 0x40
  110. #define GSWIP_MII_CFG_RATE_MASK 0x70
  111. #define GSWIP_MII_PCDU0 0x01
  112. #define GSWIP_MII_PCDU1 0x03
  113. #define GSWIP_MII_PCDU5 0x05
  114. #define GSWIP_MII_PCDU_TXDLY_MASK GENMASK(2, 0)
  115. #define GSWIP_MII_PCDU_RXDLY_MASK GENMASK(9, 7)
  116. /* GSWIP Core Registers */
  117. #define GSWIP_SWRES 0x000
  118. #define GSWIP_SWRES_R1 BIT(1) /* GSWIP Software reset */
  119. #define GSWIP_SWRES_R0 BIT(0) /* GSWIP Hardware reset */
  120. #define GSWIP_VERSION 0x013
  121. #define GSWIP_VERSION_REV_SHIFT 0
  122. #define GSWIP_VERSION_REV_MASK GENMASK(7, 0)
  123. #define GSWIP_VERSION_MOD_SHIFT 8
  124. #define GSWIP_VERSION_MOD_MASK GENMASK(15, 8)
  125. #define GSWIP_VERSION_2_0 0x100
  126. #define GSWIP_VERSION_2_1 0x021
  127. #define GSWIP_VERSION_2_2 0x122
  128. #define GSWIP_VERSION_2_2_ETC 0x022
  129. #define GSWIP_BM_RAM_VAL(x) (0x043 - (x))
  130. #define GSWIP_BM_RAM_ADDR 0x044
  131. #define GSWIP_BM_RAM_CTRL 0x045
  132. #define GSWIP_BM_RAM_CTRL_BAS BIT(15)
  133. #define GSWIP_BM_RAM_CTRL_OPMOD BIT(5)
  134. #define GSWIP_BM_RAM_CTRL_ADDR_MASK GENMASK(4, 0)
  135. #define GSWIP_BM_QUEUE_GCTRL 0x04A
  136. #define GSWIP_BM_QUEUE_GCTRL_GL_MOD BIT(10)
  137. /* buffer management Port Configuration Register */
  138. #define GSWIP_BM_PCFGp(p) (0x080 + ((p) * 2))
  139. #define GSWIP_BM_PCFG_CNTEN BIT(0) /* RMON Counter Enable */
  140. #define GSWIP_BM_PCFG_IGCNT BIT(1) /* Ingres Special Tag RMON count */
  141. /* buffer management Port Control Register */
  142. #define GSWIP_BM_RMON_CTRLp(p) (0x81 + ((p) * 2))
  143. #define GSWIP_BM_CTRL_RMON_RAM1_RES BIT(0) /* Software Reset for RMON RAM 1 */
  144. #define GSWIP_BM_CTRL_RMON_RAM2_RES BIT(1) /* Software Reset for RMON RAM 2 */
  145. /* PCE */
  146. #define GSWIP_PCE_TBL_KEY(x) (0x447 - (x))
  147. #define GSWIP_PCE_TBL_MASK 0x448
  148. #define GSWIP_PCE_TBL_VAL(x) (0x44D - (x))
  149. #define GSWIP_PCE_TBL_ADDR 0x44E
  150. #define GSWIP_PCE_TBL_CTRL 0x44F
  151. #define GSWIP_PCE_TBL_CTRL_BAS BIT(15)
  152. #define GSWIP_PCE_TBL_CTRL_TYPE BIT(13)
  153. #define GSWIP_PCE_TBL_CTRL_VLD BIT(12)
  154. #define GSWIP_PCE_TBL_CTRL_KEYFORM BIT(11)
  155. #define GSWIP_PCE_TBL_CTRL_GMAP_MASK GENMASK(10, 7)
  156. #define GSWIP_PCE_TBL_CTRL_OPMOD_MASK GENMASK(6, 5)
  157. #define GSWIP_PCE_TBL_CTRL_OPMOD_ADRD 0x00
  158. #define GSWIP_PCE_TBL_CTRL_OPMOD_ADWR 0x20
  159. #define GSWIP_PCE_TBL_CTRL_OPMOD_KSRD 0x40
  160. #define GSWIP_PCE_TBL_CTRL_OPMOD_KSWR 0x60
  161. #define GSWIP_PCE_TBL_CTRL_ADDR_MASK GENMASK(4, 0)
  162. #define GSWIP_PCE_PMAP1 0x453 /* Monitoring port map */
  163. #define GSWIP_PCE_PMAP2 0x454 /* Default Multicast port map */
  164. #define GSWIP_PCE_PMAP3 0x455 /* Default Unknown Unicast port map */
  165. #define GSWIP_PCE_GCTRL_0 0x456
  166. #define GSWIP_PCE_GCTRL_0_MTFL BIT(0) /* MAC Table Flushing */
  167. #define GSWIP_PCE_GCTRL_0_MC_VALID BIT(3)
  168. #define GSWIP_PCE_GCTRL_0_VLAN BIT(14) /* VLAN aware Switching */
  169. #define GSWIP_PCE_GCTRL_1 0x457
  170. #define GSWIP_PCE_GCTRL_1_MAC_GLOCK BIT(2) /* MAC Address table lock */
  171. #define GSWIP_PCE_GCTRL_1_MAC_GLOCK_MOD BIT(3) /* Mac address table lock forwarding mode */
  172. #define GSWIP_PCE_PCTRL_0p(p) (0x480 + ((p) * 0xA))
  173. #define GSWIP_PCE_PCTRL_0_TVM BIT(5) /* Transparent VLAN mode */
  174. #define GSWIP_PCE_PCTRL_0_VREP BIT(6) /* VLAN Replace Mode */
  175. #define GSWIP_PCE_PCTRL_0_INGRESS BIT(11) /* Accept special tag in ingress */
  176. #define GSWIP_PCE_PCTRL_0_PSTATE_LISTEN 0x0
  177. #define GSWIP_PCE_PCTRL_0_PSTATE_RX 0x1
  178. #define GSWIP_PCE_PCTRL_0_PSTATE_TX 0x2
  179. #define GSWIP_PCE_PCTRL_0_PSTATE_LEARNING 0x3
  180. #define GSWIP_PCE_PCTRL_0_PSTATE_FORWARDING 0x7
  181. #define GSWIP_PCE_PCTRL_0_PSTATE_MASK GENMASK(2, 0)
  182. #define GSWIP_PCE_VCTRL(p) (0x485 + ((p) * 0xA))
  183. #define GSWIP_PCE_VCTRL_UVR BIT(0) /* Unknown VLAN Rule */
  184. #define GSWIP_PCE_VCTRL_VIMR BIT(3) /* VLAN Ingress Member violation rule */
  185. #define GSWIP_PCE_VCTRL_VEMR BIT(4) /* VLAN Egress Member violation rule */
  186. #define GSWIP_PCE_VCTRL_VSR BIT(5) /* VLAN Security */
  187. #define GSWIP_PCE_VCTRL_VID0 BIT(6) /* Priority Tagged Rule */
  188. #define GSWIP_PCE_DEFPVID(p) (0x486 + ((p) * 0xA))
  189. #define GSWIP_MAC_FLEN 0x8C5
  190. #define GSWIP_MAC_CTRL_0p(p) (0x903 + ((p) * 0xC))
  191. #define GSWIP_MAC_CTRL_0_PADEN BIT(8)
  192. #define GSWIP_MAC_CTRL_0_FCS_EN BIT(7)
  193. #define GSWIP_MAC_CTRL_0_FCON_MASK 0x0070
  194. #define GSWIP_MAC_CTRL_0_FCON_AUTO 0x0000
  195. #define GSWIP_MAC_CTRL_0_FCON_RX 0x0010
  196. #define GSWIP_MAC_CTRL_0_FCON_TX 0x0020
  197. #define GSWIP_MAC_CTRL_0_FCON_RXTX 0x0030
  198. #define GSWIP_MAC_CTRL_0_FCON_NONE 0x0040
  199. #define GSWIP_MAC_CTRL_0_FDUP_MASK 0x000C
  200. #define GSWIP_MAC_CTRL_0_FDUP_AUTO 0x0000
  201. #define GSWIP_MAC_CTRL_0_FDUP_EN 0x0004
  202. #define GSWIP_MAC_CTRL_0_FDUP_DIS 0x000C
  203. #define GSWIP_MAC_CTRL_0_GMII_MASK 0x0003
  204. #define GSWIP_MAC_CTRL_0_GMII_AUTO 0x0000
  205. #define GSWIP_MAC_CTRL_0_GMII_MII 0x0001
  206. #define GSWIP_MAC_CTRL_0_GMII_RGMII 0x0002
  207. #define GSWIP_MAC_CTRL_2p(p) (0x905 + ((p) * 0xC))
  208. #define GSWIP_MAC_CTRL_2_LCHKL BIT(2) /* Frame Length Check Long Enable */
  209. #define GSWIP_MAC_CTRL_2_MLEN BIT(3) /* Maximum Untagged Frame Lnegth */
  210. /* Ethernet Switch Fetch DMA Port Control Register */
  211. #define GSWIP_FDMA_PCTRLp(p) (0xA80 + ((p) * 0x6))
  212. #define GSWIP_FDMA_PCTRL_EN BIT(0) /* FDMA Port Enable */
  213. #define GSWIP_FDMA_PCTRL_STEN BIT(1) /* Special Tag Insertion Enable */
  214. #define GSWIP_FDMA_PCTRL_VLANMOD_MASK GENMASK(4, 3) /* VLAN Modification Control */
  215. #define GSWIP_FDMA_PCTRL_VLANMOD_SHIFT 3 /* VLAN Modification Control */
  216. #define GSWIP_FDMA_PCTRL_VLANMOD_DIS (0x0 << GSWIP_FDMA_PCTRL_VLANMOD_SHIFT)
  217. #define GSWIP_FDMA_PCTRL_VLANMOD_PRIO (0x1 << GSWIP_FDMA_PCTRL_VLANMOD_SHIFT)
  218. #define GSWIP_FDMA_PCTRL_VLANMOD_ID (0x2 << GSWIP_FDMA_PCTRL_VLANMOD_SHIFT)
  219. #define GSWIP_FDMA_PCTRL_VLANMOD_BOTH (0x3 << GSWIP_FDMA_PCTRL_VLANMOD_SHIFT)
  220. /* Ethernet Switch Store DMA Port Control Register */
  221. #define GSWIP_SDMA_PCTRLp(p) (0xBC0 + ((p) * 0x6))
  222. #define GSWIP_SDMA_PCTRL_EN BIT(0) /* SDMA Port Enable */
  223. #define GSWIP_SDMA_PCTRL_FCEN BIT(1) /* Flow Control Enable */
  224. #define GSWIP_SDMA_PCTRL_PAUFWD BIT(3) /* Pause Frame Forwarding */
  225. #define GSWIP_TABLE_ACTIVE_VLAN 0x01
  226. #define GSWIP_TABLE_VLAN_MAPPING 0x02
  227. #define GSWIP_TABLE_MAC_BRIDGE 0x0b
  228. #define GSWIP_TABLE_MAC_BRIDGE_KEY3_FID GENMASK(5, 0) /* Filtering identifier */
  229. #define GSWIP_TABLE_MAC_BRIDGE_VAL0_PORT GENMASK(7, 4) /* Port on learned entries */
  230. #define GSWIP_TABLE_MAC_BRIDGE_VAL1_STATIC BIT(0) /* Static, non-aging entry */
  231. #define XRX200_GPHY_FW_ALIGN (16 * 1024)
  232. /* Maximum packet size supported by the switch. In theory this should be 10240,
  233. * but long packets currently cause lock-ups with an MTU of over 2526. Medium
  234. * packets are sometimes dropped (e.g. TCP over 2477, UDP over 2516-2519, ICMP
  235. * over 2526), hence an MTU value of 2400 seems safe. This issue only affects
  236. * packet reception. This is probably caused by the PPA engine, which is on the
  237. * RX part of the device. Packet transmission works properly up to 10240.
  238. */
  239. #define GSWIP_MAX_PACKET_LENGTH 2400
  240. struct gswip_hw_info {
  241. int max_ports;
  242. int cpu_port;
  243. const struct dsa_switch_ops *ops;
  244. };
  245. struct xway_gphy_match_data {
  246. char *fe_firmware_name;
  247. char *ge_firmware_name;
  248. };
  249. struct gswip_gphy_fw {
  250. struct clk *clk_gate;
  251. struct reset_control *reset;
  252. u32 fw_addr_offset;
  253. char *fw_name;
  254. };
  255. struct gswip_vlan {
  256. struct net_device *bridge;
  257. u16 vid;
  258. u8 fid;
  259. };
  260. struct gswip_priv {
  261. __iomem void *gswip;
  262. __iomem void *mdio;
  263. __iomem void *mii;
  264. const struct gswip_hw_info *hw_info;
  265. const struct xway_gphy_match_data *gphy_fw_name_cfg;
  266. struct dsa_switch *ds;
  267. struct device *dev;
  268. struct regmap *rcu_regmap;
  269. struct gswip_vlan vlans[64];
  270. int num_gphy_fw;
  271. struct gswip_gphy_fw *gphy_fw;
  272. u32 port_vlan_filter;
  273. struct mutex pce_table_lock;
  274. };
  275. struct gswip_pce_table_entry {
  276. u16 index; // PCE_TBL_ADDR.ADDR = pData->table_index
  277. u16 table; // PCE_TBL_CTRL.ADDR = pData->table
  278. u16 key[8];
  279. u16 val[5];
  280. u16 mask;
  281. u8 gmap;
  282. bool type;
  283. bool valid;
  284. bool key_mode;
  285. };
  286. struct gswip_rmon_cnt_desc {
  287. unsigned int size;
  288. unsigned int offset;
  289. const char *name;
  290. };
  291. #define MIB_DESC(_size, _offset, _name) {.size = _size, .offset = _offset, .name = _name}
  292. static const struct gswip_rmon_cnt_desc gswip_rmon_cnt[] = {
  293. /** Receive Packet Count (only packets that are accepted and not discarded). */
  294. MIB_DESC(1, 0x1F, "RxGoodPkts"),
  295. MIB_DESC(1, 0x23, "RxUnicastPkts"),
  296. MIB_DESC(1, 0x22, "RxMulticastPkts"),
  297. MIB_DESC(1, 0x21, "RxFCSErrorPkts"),
  298. MIB_DESC(1, 0x1D, "RxUnderSizeGoodPkts"),
  299. MIB_DESC(1, 0x1E, "RxUnderSizeErrorPkts"),
  300. MIB_DESC(1, 0x1B, "RxOversizeGoodPkts"),
  301. MIB_DESC(1, 0x1C, "RxOversizeErrorPkts"),
  302. MIB_DESC(1, 0x20, "RxGoodPausePkts"),
  303. MIB_DESC(1, 0x1A, "RxAlignErrorPkts"),
  304. MIB_DESC(1, 0x12, "Rx64BytePkts"),
  305. MIB_DESC(1, 0x13, "Rx127BytePkts"),
  306. MIB_DESC(1, 0x14, "Rx255BytePkts"),
  307. MIB_DESC(1, 0x15, "Rx511BytePkts"),
  308. MIB_DESC(1, 0x16, "Rx1023BytePkts"),
  309. /** Receive Size 1024-1522 (or more, if configured) Packet Count. */
  310. MIB_DESC(1, 0x17, "RxMaxBytePkts"),
  311. MIB_DESC(1, 0x18, "RxDroppedPkts"),
  312. MIB_DESC(1, 0x19, "RxFilteredPkts"),
  313. MIB_DESC(2, 0x24, "RxGoodBytes"),
  314. MIB_DESC(2, 0x26, "RxBadBytes"),
  315. MIB_DESC(1, 0x11, "TxAcmDroppedPkts"),
  316. MIB_DESC(1, 0x0C, "TxGoodPkts"),
  317. MIB_DESC(1, 0x06, "TxUnicastPkts"),
  318. MIB_DESC(1, 0x07, "TxMulticastPkts"),
  319. MIB_DESC(1, 0x00, "Tx64BytePkts"),
  320. MIB_DESC(1, 0x01, "Tx127BytePkts"),
  321. MIB_DESC(1, 0x02, "Tx255BytePkts"),
  322. MIB_DESC(1, 0x03, "Tx511BytePkts"),
  323. MIB_DESC(1, 0x04, "Tx1023BytePkts"),
  324. /** Transmit Size 1024-1522 (or more, if configured) Packet Count. */
  325. MIB_DESC(1, 0x05, "TxMaxBytePkts"),
  326. MIB_DESC(1, 0x08, "TxSingleCollCount"),
  327. MIB_DESC(1, 0x09, "TxMultCollCount"),
  328. MIB_DESC(1, 0x0A, "TxLateCollCount"),
  329. MIB_DESC(1, 0x0B, "TxExcessCollCount"),
  330. MIB_DESC(1, 0x0D, "TxPauseCount"),
  331. MIB_DESC(1, 0x10, "TxDroppedPkts"),
  332. MIB_DESC(2, 0x0E, "TxGoodBytes"),
  333. };
  334. static u32 gswip_switch_r(struct gswip_priv *priv, u32 offset)
  335. {
  336. return __raw_readl(priv->gswip + (offset * 4));
  337. }
  338. static void gswip_switch_w(struct gswip_priv *priv, u32 val, u32 offset)
  339. {
  340. __raw_writel(val, priv->gswip + (offset * 4));
  341. }
  342. static void gswip_switch_mask(struct gswip_priv *priv, u32 clear, u32 set,
  343. u32 offset)
  344. {
  345. u32 val = gswip_switch_r(priv, offset);
  346. val &= ~(clear);
  347. val |= set;
  348. gswip_switch_w(priv, val, offset);
  349. }
  350. static u32 gswip_switch_r_timeout(struct gswip_priv *priv, u32 offset,
  351. u32 cleared)
  352. {
  353. u32 val;
  354. return readx_poll_timeout(__raw_readl, priv->gswip + (offset * 4), val,
  355. (val & cleared) == 0, 20, 50000);
  356. }
  357. static u32 gswip_mdio_r(struct gswip_priv *priv, u32 offset)
  358. {
  359. return __raw_readl(priv->mdio + (offset * 4));
  360. }
  361. static void gswip_mdio_w(struct gswip_priv *priv, u32 val, u32 offset)
  362. {
  363. __raw_writel(val, priv->mdio + (offset * 4));
  364. }
  365. static void gswip_mdio_mask(struct gswip_priv *priv, u32 clear, u32 set,
  366. u32 offset)
  367. {
  368. u32 val = gswip_mdio_r(priv, offset);
  369. val &= ~(clear);
  370. val |= set;
  371. gswip_mdio_w(priv, val, offset);
  372. }
  373. static u32 gswip_mii_r(struct gswip_priv *priv, u32 offset)
  374. {
  375. return __raw_readl(priv->mii + (offset * 4));
  376. }
  377. static void gswip_mii_w(struct gswip_priv *priv, u32 val, u32 offset)
  378. {
  379. __raw_writel(val, priv->mii + (offset * 4));
  380. }
  381. static void gswip_mii_mask(struct gswip_priv *priv, u32 clear, u32 set,
  382. u32 offset)
  383. {
  384. u32 val = gswip_mii_r(priv, offset);
  385. val &= ~(clear);
  386. val |= set;
  387. gswip_mii_w(priv, val, offset);
  388. }
  389. static void gswip_mii_mask_cfg(struct gswip_priv *priv, u32 clear, u32 set,
  390. int port)
  391. {
  392. /* There's no MII_CFG register for the CPU port */
  393. if (!dsa_is_cpu_port(priv->ds, port))
  394. gswip_mii_mask(priv, clear, set, GSWIP_MII_CFGp(port));
  395. }
  396. static void gswip_mii_mask_pcdu(struct gswip_priv *priv, u32 clear, u32 set,
  397. int port)
  398. {
  399. switch (port) {
  400. case 0:
  401. gswip_mii_mask(priv, clear, set, GSWIP_MII_PCDU0);
  402. break;
  403. case 1:
  404. gswip_mii_mask(priv, clear, set, GSWIP_MII_PCDU1);
  405. break;
  406. case 5:
  407. gswip_mii_mask(priv, clear, set, GSWIP_MII_PCDU5);
  408. break;
  409. }
  410. }
  411. static int gswip_mdio_poll(struct gswip_priv *priv)
  412. {
  413. int cnt = 100;
  414. while (likely(cnt--)) {
  415. u32 ctrl = gswip_mdio_r(priv, GSWIP_MDIO_CTRL);
  416. if ((ctrl & GSWIP_MDIO_CTRL_BUSY) == 0)
  417. return 0;
  418. usleep_range(20, 40);
  419. }
  420. return -ETIMEDOUT;
  421. }
  422. static int gswip_mdio_wr(struct mii_bus *bus, int addr, int reg, u16 val)
  423. {
  424. struct gswip_priv *priv = bus->priv;
  425. int err;
  426. err = gswip_mdio_poll(priv);
  427. if (err) {
  428. dev_err(&bus->dev, "waiting for MDIO bus busy timed out\n");
  429. return err;
  430. }
  431. gswip_mdio_w(priv, val, GSWIP_MDIO_WRITE);
  432. gswip_mdio_w(priv, GSWIP_MDIO_CTRL_BUSY | GSWIP_MDIO_CTRL_WR |
  433. ((addr & GSWIP_MDIO_CTRL_PHYAD_MASK) << GSWIP_MDIO_CTRL_PHYAD_SHIFT) |
  434. (reg & GSWIP_MDIO_CTRL_REGAD_MASK),
  435. GSWIP_MDIO_CTRL);
  436. return 0;
  437. }
  438. static int gswip_mdio_rd(struct mii_bus *bus, int addr, int reg)
  439. {
  440. struct gswip_priv *priv = bus->priv;
  441. int err;
  442. err = gswip_mdio_poll(priv);
  443. if (err) {
  444. dev_err(&bus->dev, "waiting for MDIO bus busy timed out\n");
  445. return err;
  446. }
  447. gswip_mdio_w(priv, GSWIP_MDIO_CTRL_BUSY | GSWIP_MDIO_CTRL_RD |
  448. ((addr & GSWIP_MDIO_CTRL_PHYAD_MASK) << GSWIP_MDIO_CTRL_PHYAD_SHIFT) |
  449. (reg & GSWIP_MDIO_CTRL_REGAD_MASK),
  450. GSWIP_MDIO_CTRL);
  451. err = gswip_mdio_poll(priv);
  452. if (err) {
  453. dev_err(&bus->dev, "waiting for MDIO bus busy timed out\n");
  454. return err;
  455. }
  456. return gswip_mdio_r(priv, GSWIP_MDIO_READ);
  457. }
  458. static int gswip_mdio(struct gswip_priv *priv)
  459. {
  460. struct device_node *mdio_np, *switch_np = priv->dev->of_node;
  461. struct device *dev = priv->dev;
  462. struct mii_bus *bus;
  463. int err = 0;
  464. mdio_np = of_get_compatible_child(switch_np, "lantiq,xrx200-mdio");
  465. if (!of_device_is_available(mdio_np))
  466. goto out_put_node;
  467. bus = devm_mdiobus_alloc(dev);
  468. if (!bus) {
  469. err = -ENOMEM;
  470. goto out_put_node;
  471. }
  472. bus->priv = priv;
  473. bus->read = gswip_mdio_rd;
  474. bus->write = gswip_mdio_wr;
  475. bus->name = "lantiq,xrx200-mdio";
  476. snprintf(bus->id, MII_BUS_ID_SIZE, "%s-mii", dev_name(priv->dev));
  477. bus->parent = priv->dev;
  478. err = devm_of_mdiobus_register(dev, bus, mdio_np);
  479. out_put_node:
  480. of_node_put(mdio_np);
  481. return err;
  482. }
  483. static int gswip_pce_table_entry_read(struct gswip_priv *priv,
  484. struct gswip_pce_table_entry *tbl)
  485. {
  486. int i;
  487. int err;
  488. u16 crtl;
  489. u16 addr_mode = tbl->key_mode ? GSWIP_PCE_TBL_CTRL_OPMOD_KSRD :
  490. GSWIP_PCE_TBL_CTRL_OPMOD_ADRD;
  491. mutex_lock(&priv->pce_table_lock);
  492. err = gswip_switch_r_timeout(priv, GSWIP_PCE_TBL_CTRL,
  493. GSWIP_PCE_TBL_CTRL_BAS);
  494. if (err) {
  495. mutex_unlock(&priv->pce_table_lock);
  496. return err;
  497. }
  498. gswip_switch_w(priv, tbl->index, GSWIP_PCE_TBL_ADDR);
  499. gswip_switch_mask(priv, GSWIP_PCE_TBL_CTRL_ADDR_MASK |
  500. GSWIP_PCE_TBL_CTRL_OPMOD_MASK,
  501. tbl->table | addr_mode | GSWIP_PCE_TBL_CTRL_BAS,
  502. GSWIP_PCE_TBL_CTRL);
  503. err = gswip_switch_r_timeout(priv, GSWIP_PCE_TBL_CTRL,
  504. GSWIP_PCE_TBL_CTRL_BAS);
  505. if (err) {
  506. mutex_unlock(&priv->pce_table_lock);
  507. return err;
  508. }
  509. for (i = 0; i < ARRAY_SIZE(tbl->key); i++)
  510. tbl->key[i] = gswip_switch_r(priv, GSWIP_PCE_TBL_KEY(i));
  511. for (i = 0; i < ARRAY_SIZE(tbl->val); i++)
  512. tbl->val[i] = gswip_switch_r(priv, GSWIP_PCE_TBL_VAL(i));
  513. tbl->mask = gswip_switch_r(priv, GSWIP_PCE_TBL_MASK);
  514. crtl = gswip_switch_r(priv, GSWIP_PCE_TBL_CTRL);
  515. tbl->type = !!(crtl & GSWIP_PCE_TBL_CTRL_TYPE);
  516. tbl->valid = !!(crtl & GSWIP_PCE_TBL_CTRL_VLD);
  517. tbl->gmap = (crtl & GSWIP_PCE_TBL_CTRL_GMAP_MASK) >> 7;
  518. mutex_unlock(&priv->pce_table_lock);
  519. return 0;
  520. }
  521. static int gswip_pce_table_entry_write(struct gswip_priv *priv,
  522. struct gswip_pce_table_entry *tbl)
  523. {
  524. int i;
  525. int err;
  526. u16 crtl;
  527. u16 addr_mode = tbl->key_mode ? GSWIP_PCE_TBL_CTRL_OPMOD_KSWR :
  528. GSWIP_PCE_TBL_CTRL_OPMOD_ADWR;
  529. mutex_lock(&priv->pce_table_lock);
  530. err = gswip_switch_r_timeout(priv, GSWIP_PCE_TBL_CTRL,
  531. GSWIP_PCE_TBL_CTRL_BAS);
  532. if (err) {
  533. mutex_unlock(&priv->pce_table_lock);
  534. return err;
  535. }
  536. gswip_switch_w(priv, tbl->index, GSWIP_PCE_TBL_ADDR);
  537. gswip_switch_mask(priv, GSWIP_PCE_TBL_CTRL_ADDR_MASK |
  538. GSWIP_PCE_TBL_CTRL_OPMOD_MASK,
  539. tbl->table | addr_mode,
  540. GSWIP_PCE_TBL_CTRL);
  541. for (i = 0; i < ARRAY_SIZE(tbl->key); i++)
  542. gswip_switch_w(priv, tbl->key[i], GSWIP_PCE_TBL_KEY(i));
  543. for (i = 0; i < ARRAY_SIZE(tbl->val); i++)
  544. gswip_switch_w(priv, tbl->val[i], GSWIP_PCE_TBL_VAL(i));
  545. gswip_switch_mask(priv, GSWIP_PCE_TBL_CTRL_ADDR_MASK |
  546. GSWIP_PCE_TBL_CTRL_OPMOD_MASK,
  547. tbl->table | addr_mode,
  548. GSWIP_PCE_TBL_CTRL);
  549. gswip_switch_w(priv, tbl->mask, GSWIP_PCE_TBL_MASK);
  550. crtl = gswip_switch_r(priv, GSWIP_PCE_TBL_CTRL);
  551. crtl &= ~(GSWIP_PCE_TBL_CTRL_TYPE | GSWIP_PCE_TBL_CTRL_VLD |
  552. GSWIP_PCE_TBL_CTRL_GMAP_MASK);
  553. if (tbl->type)
  554. crtl |= GSWIP_PCE_TBL_CTRL_TYPE;
  555. if (tbl->valid)
  556. crtl |= GSWIP_PCE_TBL_CTRL_VLD;
  557. crtl |= (tbl->gmap << 7) & GSWIP_PCE_TBL_CTRL_GMAP_MASK;
  558. crtl |= GSWIP_PCE_TBL_CTRL_BAS;
  559. gswip_switch_w(priv, crtl, GSWIP_PCE_TBL_CTRL);
  560. err = gswip_switch_r_timeout(priv, GSWIP_PCE_TBL_CTRL,
  561. GSWIP_PCE_TBL_CTRL_BAS);
  562. mutex_unlock(&priv->pce_table_lock);
  563. return err;
  564. }
  565. /* Add the LAN port into a bridge with the CPU port by
  566. * default. This prevents automatic forwarding of
  567. * packages between the LAN ports when no explicit
  568. * bridge is configured.
  569. */
  570. static int gswip_add_single_port_br(struct gswip_priv *priv, int port, bool add)
  571. {
  572. struct gswip_pce_table_entry vlan_active = {0,};
  573. struct gswip_pce_table_entry vlan_mapping = {0,};
  574. unsigned int cpu_port = priv->hw_info->cpu_port;
  575. int err;
  576. vlan_active.index = port + 1;
  577. vlan_active.table = GSWIP_TABLE_ACTIVE_VLAN;
  578. vlan_active.key[0] = 0; /* vid */
  579. vlan_active.val[0] = port + 1 /* fid */;
  580. vlan_active.valid = add;
  581. err = gswip_pce_table_entry_write(priv, &vlan_active);
  582. if (err) {
  583. dev_err(priv->dev, "failed to write active VLAN: %d\n", err);
  584. return err;
  585. }
  586. if (!add)
  587. return 0;
  588. vlan_mapping.index = port + 1;
  589. vlan_mapping.table = GSWIP_TABLE_VLAN_MAPPING;
  590. vlan_mapping.val[0] = 0 /* vid */;
  591. vlan_mapping.val[1] = BIT(port) | BIT(cpu_port);
  592. vlan_mapping.val[2] = 0;
  593. err = gswip_pce_table_entry_write(priv, &vlan_mapping);
  594. if (err) {
  595. dev_err(priv->dev, "failed to write VLAN mapping: %d\n", err);
  596. return err;
  597. }
  598. return 0;
  599. }
  600. static int gswip_port_setup(struct dsa_switch *ds, int port)
  601. {
  602. struct gswip_priv *priv = ds->priv;
  603. int err;
  604. if (!dsa_is_cpu_port(ds, port)) {
  605. err = gswip_add_single_port_br(priv, port, true);
  606. if (err)
  607. return err;
  608. }
  609. return 0;
  610. }
  611. static int gswip_port_enable(struct dsa_switch *ds, int port,
  612. struct phy_device *phydev)
  613. {
  614. struct gswip_priv *priv = ds->priv;
  615. if (!dsa_is_cpu_port(ds, port)) {
  616. u32 mdio_phy = 0;
  617. if (phydev)
  618. mdio_phy = phydev->mdio.addr & GSWIP_MDIO_PHY_ADDR_MASK;
  619. gswip_mdio_mask(priv, GSWIP_MDIO_PHY_ADDR_MASK, mdio_phy,
  620. GSWIP_MDIO_PHYp(port));
  621. }
  622. /* RMON Counter Enable for port */
  623. gswip_switch_w(priv, GSWIP_BM_PCFG_CNTEN, GSWIP_BM_PCFGp(port));
  624. /* enable port fetch/store dma & VLAN Modification */
  625. gswip_switch_mask(priv, 0, GSWIP_FDMA_PCTRL_EN |
  626. GSWIP_FDMA_PCTRL_VLANMOD_BOTH,
  627. GSWIP_FDMA_PCTRLp(port));
  628. gswip_switch_mask(priv, 0, GSWIP_SDMA_PCTRL_EN,
  629. GSWIP_SDMA_PCTRLp(port));
  630. return 0;
  631. }
  632. static void gswip_port_disable(struct dsa_switch *ds, int port)
  633. {
  634. struct gswip_priv *priv = ds->priv;
  635. gswip_switch_mask(priv, GSWIP_FDMA_PCTRL_EN, 0,
  636. GSWIP_FDMA_PCTRLp(port));
  637. gswip_switch_mask(priv, GSWIP_SDMA_PCTRL_EN, 0,
  638. GSWIP_SDMA_PCTRLp(port));
  639. }
  640. static int gswip_pce_load_microcode(struct gswip_priv *priv)
  641. {
  642. int i;
  643. int err;
  644. gswip_switch_mask(priv, GSWIP_PCE_TBL_CTRL_ADDR_MASK |
  645. GSWIP_PCE_TBL_CTRL_OPMOD_MASK,
  646. GSWIP_PCE_TBL_CTRL_OPMOD_ADWR, GSWIP_PCE_TBL_CTRL);
  647. gswip_switch_w(priv, 0, GSWIP_PCE_TBL_MASK);
  648. for (i = 0; i < ARRAY_SIZE(gswip_pce_microcode); i++) {
  649. gswip_switch_w(priv, i, GSWIP_PCE_TBL_ADDR);
  650. gswip_switch_w(priv, gswip_pce_microcode[i].val_0,
  651. GSWIP_PCE_TBL_VAL(0));
  652. gswip_switch_w(priv, gswip_pce_microcode[i].val_1,
  653. GSWIP_PCE_TBL_VAL(1));
  654. gswip_switch_w(priv, gswip_pce_microcode[i].val_2,
  655. GSWIP_PCE_TBL_VAL(2));
  656. gswip_switch_w(priv, gswip_pce_microcode[i].val_3,
  657. GSWIP_PCE_TBL_VAL(3));
  658. /* start the table access: */
  659. gswip_switch_mask(priv, 0, GSWIP_PCE_TBL_CTRL_BAS,
  660. GSWIP_PCE_TBL_CTRL);
  661. err = gswip_switch_r_timeout(priv, GSWIP_PCE_TBL_CTRL,
  662. GSWIP_PCE_TBL_CTRL_BAS);
  663. if (err)
  664. return err;
  665. }
  666. /* tell the switch that the microcode is loaded */
  667. gswip_switch_mask(priv, 0, GSWIP_PCE_GCTRL_0_MC_VALID,
  668. GSWIP_PCE_GCTRL_0);
  669. return 0;
  670. }
  671. static int gswip_port_vlan_filtering(struct dsa_switch *ds, int port,
  672. bool vlan_filtering,
  673. struct netlink_ext_ack *extack)
  674. {
  675. struct net_device *bridge = dsa_port_bridge_dev_get(dsa_to_port(ds, port));
  676. struct gswip_priv *priv = ds->priv;
  677. /* Do not allow changing the VLAN filtering options while in bridge */
  678. if (bridge && !!(priv->port_vlan_filter & BIT(port)) != vlan_filtering) {
  679. NL_SET_ERR_MSG_MOD(extack,
  680. "Dynamic toggling of vlan_filtering not supported");
  681. return -EIO;
  682. }
  683. if (vlan_filtering) {
  684. /* Use tag based VLAN */
  685. gswip_switch_mask(priv,
  686. GSWIP_PCE_VCTRL_VSR,
  687. GSWIP_PCE_VCTRL_UVR | GSWIP_PCE_VCTRL_VIMR |
  688. GSWIP_PCE_VCTRL_VEMR,
  689. GSWIP_PCE_VCTRL(port));
  690. gswip_switch_mask(priv, GSWIP_PCE_PCTRL_0_TVM, 0,
  691. GSWIP_PCE_PCTRL_0p(port));
  692. } else {
  693. /* Use port based VLAN */
  694. gswip_switch_mask(priv,
  695. GSWIP_PCE_VCTRL_UVR | GSWIP_PCE_VCTRL_VIMR |
  696. GSWIP_PCE_VCTRL_VEMR,
  697. GSWIP_PCE_VCTRL_VSR,
  698. GSWIP_PCE_VCTRL(port));
  699. gswip_switch_mask(priv, 0, GSWIP_PCE_PCTRL_0_TVM,
  700. GSWIP_PCE_PCTRL_0p(port));
  701. }
  702. return 0;
  703. }
  704. static int gswip_setup(struct dsa_switch *ds)
  705. {
  706. struct gswip_priv *priv = ds->priv;
  707. unsigned int cpu_port = priv->hw_info->cpu_port;
  708. int i;
  709. int err;
  710. gswip_switch_w(priv, GSWIP_SWRES_R0, GSWIP_SWRES);
  711. usleep_range(5000, 10000);
  712. gswip_switch_w(priv, 0, GSWIP_SWRES);
  713. /* disable port fetch/store dma on all ports */
  714. for (i = 0; i < priv->hw_info->max_ports; i++) {
  715. gswip_port_disable(ds, i);
  716. gswip_port_vlan_filtering(ds, i, false, NULL);
  717. }
  718. /* enable Switch */
  719. gswip_mdio_mask(priv, 0, GSWIP_MDIO_GLOB_ENABLE, GSWIP_MDIO_GLOB);
  720. err = gswip_pce_load_microcode(priv);
  721. if (err) {
  722. dev_err(priv->dev, "writing PCE microcode failed, %i\n", err);
  723. return err;
  724. }
  725. /* Default unknown Broadcast/Multicast/Unicast port maps */
  726. gswip_switch_w(priv, BIT(cpu_port), GSWIP_PCE_PMAP1);
  727. gswip_switch_w(priv, BIT(cpu_port), GSWIP_PCE_PMAP2);
  728. gswip_switch_w(priv, BIT(cpu_port), GSWIP_PCE_PMAP3);
  729. /* Deactivate MDIO PHY auto polling. Some PHYs as the AR8030 have an
  730. * interoperability problem with this auto polling mechanism because
  731. * their status registers think that the link is in a different state
  732. * than it actually is. For the AR8030 it has the BMSR_ESTATEN bit set
  733. * as well as ESTATUS_1000_TFULL and ESTATUS_1000_XFULL. This makes the
  734. * auto polling state machine consider the link being negotiated with
  735. * 1Gbit/s. Since the PHY itself is a Fast Ethernet RMII PHY this leads
  736. * to the switch port being completely dead (RX and TX are both not
  737. * working).
  738. * Also with various other PHY / port combinations (PHY11G GPHY, PHY22F
  739. * GPHY, external RGMII PEF7071/7072) any traffic would stop. Sometimes
  740. * it would work fine for a few minutes to hours and then stop, on
  741. * other device it would no traffic could be sent or received at all.
  742. * Testing shows that when PHY auto polling is disabled these problems
  743. * go away.
  744. */
  745. gswip_mdio_w(priv, 0x0, GSWIP_MDIO_MDC_CFG0);
  746. /* Configure the MDIO Clock 2.5 MHz */
  747. gswip_mdio_mask(priv, 0xff, 0x09, GSWIP_MDIO_MDC_CFG1);
  748. /* Disable the xMII interface and clear it's isolation bit */
  749. for (i = 0; i < priv->hw_info->max_ports; i++)
  750. gswip_mii_mask_cfg(priv,
  751. GSWIP_MII_CFG_EN | GSWIP_MII_CFG_ISOLATE,
  752. 0, i);
  753. /* enable special tag insertion on cpu port */
  754. gswip_switch_mask(priv, 0, GSWIP_FDMA_PCTRL_STEN,
  755. GSWIP_FDMA_PCTRLp(cpu_port));
  756. /* accept special tag in ingress direction */
  757. gswip_switch_mask(priv, 0, GSWIP_PCE_PCTRL_0_INGRESS,
  758. GSWIP_PCE_PCTRL_0p(cpu_port));
  759. gswip_switch_mask(priv, 0, GSWIP_BM_QUEUE_GCTRL_GL_MOD,
  760. GSWIP_BM_QUEUE_GCTRL);
  761. /* VLAN aware Switching */
  762. gswip_switch_mask(priv, 0, GSWIP_PCE_GCTRL_0_VLAN, GSWIP_PCE_GCTRL_0);
  763. /* Flush MAC Table */
  764. gswip_switch_mask(priv, 0, GSWIP_PCE_GCTRL_0_MTFL, GSWIP_PCE_GCTRL_0);
  765. err = gswip_switch_r_timeout(priv, GSWIP_PCE_GCTRL_0,
  766. GSWIP_PCE_GCTRL_0_MTFL);
  767. if (err) {
  768. dev_err(priv->dev, "MAC flushing didn't finish\n");
  769. return err;
  770. }
  771. ds->mtu_enforcement_ingress = true;
  772. ds->configure_vlan_while_not_filtering = false;
  773. return 0;
  774. }
  775. static enum dsa_tag_protocol gswip_get_tag_protocol(struct dsa_switch *ds,
  776. int port,
  777. enum dsa_tag_protocol mp)
  778. {
  779. return DSA_TAG_PROTO_GSWIP;
  780. }
  781. static int gswip_vlan_active_create(struct gswip_priv *priv,
  782. struct net_device *bridge,
  783. int fid, u16 vid)
  784. {
  785. struct gswip_pce_table_entry vlan_active = {0,};
  786. unsigned int max_ports = priv->hw_info->max_ports;
  787. int idx = -1;
  788. int err;
  789. int i;
  790. /* Look for a free slot */
  791. for (i = max_ports; i < ARRAY_SIZE(priv->vlans); i++) {
  792. if (!priv->vlans[i].bridge) {
  793. idx = i;
  794. break;
  795. }
  796. }
  797. if (idx == -1)
  798. return -ENOSPC;
  799. if (fid == -1)
  800. fid = idx;
  801. vlan_active.index = idx;
  802. vlan_active.table = GSWIP_TABLE_ACTIVE_VLAN;
  803. vlan_active.key[0] = vid;
  804. vlan_active.val[0] = fid;
  805. vlan_active.valid = true;
  806. err = gswip_pce_table_entry_write(priv, &vlan_active);
  807. if (err) {
  808. dev_err(priv->dev, "failed to write active VLAN: %d\n", err);
  809. return err;
  810. }
  811. priv->vlans[idx].bridge = bridge;
  812. priv->vlans[idx].vid = vid;
  813. priv->vlans[idx].fid = fid;
  814. return idx;
  815. }
  816. static int gswip_vlan_active_remove(struct gswip_priv *priv, int idx)
  817. {
  818. struct gswip_pce_table_entry vlan_active = {0,};
  819. int err;
  820. vlan_active.index = idx;
  821. vlan_active.table = GSWIP_TABLE_ACTIVE_VLAN;
  822. vlan_active.valid = false;
  823. err = gswip_pce_table_entry_write(priv, &vlan_active);
  824. if (err)
  825. dev_err(priv->dev, "failed to delete active VLAN: %d\n", err);
  826. priv->vlans[idx].bridge = NULL;
  827. return err;
  828. }
  829. static int gswip_vlan_add_unaware(struct gswip_priv *priv,
  830. struct net_device *bridge, int port)
  831. {
  832. struct gswip_pce_table_entry vlan_mapping = {0,};
  833. unsigned int max_ports = priv->hw_info->max_ports;
  834. unsigned int cpu_port = priv->hw_info->cpu_port;
  835. bool active_vlan_created = false;
  836. int idx = -1;
  837. int i;
  838. int err;
  839. /* Check if there is already a page for this bridge */
  840. for (i = max_ports; i < ARRAY_SIZE(priv->vlans); i++) {
  841. if (priv->vlans[i].bridge == bridge) {
  842. idx = i;
  843. break;
  844. }
  845. }
  846. /* If this bridge is not programmed yet, add a Active VLAN table
  847. * entry in a free slot and prepare the VLAN mapping table entry.
  848. */
  849. if (idx == -1) {
  850. idx = gswip_vlan_active_create(priv, bridge, -1, 0);
  851. if (idx < 0)
  852. return idx;
  853. active_vlan_created = true;
  854. vlan_mapping.index = idx;
  855. vlan_mapping.table = GSWIP_TABLE_VLAN_MAPPING;
  856. /* VLAN ID byte, maps to the VLAN ID of vlan active table */
  857. vlan_mapping.val[0] = 0;
  858. } else {
  859. /* Read the existing VLAN mapping entry from the switch */
  860. vlan_mapping.index = idx;
  861. vlan_mapping.table = GSWIP_TABLE_VLAN_MAPPING;
  862. err = gswip_pce_table_entry_read(priv, &vlan_mapping);
  863. if (err) {
  864. dev_err(priv->dev, "failed to read VLAN mapping: %d\n",
  865. err);
  866. return err;
  867. }
  868. }
  869. /* Update the VLAN mapping entry and write it to the switch */
  870. vlan_mapping.val[1] |= BIT(cpu_port);
  871. vlan_mapping.val[1] |= BIT(port);
  872. err = gswip_pce_table_entry_write(priv, &vlan_mapping);
  873. if (err) {
  874. dev_err(priv->dev, "failed to write VLAN mapping: %d\n", err);
  875. /* In case an Active VLAN was creaetd delete it again */
  876. if (active_vlan_created)
  877. gswip_vlan_active_remove(priv, idx);
  878. return err;
  879. }
  880. gswip_switch_w(priv, 0, GSWIP_PCE_DEFPVID(port));
  881. return 0;
  882. }
  883. static int gswip_vlan_add_aware(struct gswip_priv *priv,
  884. struct net_device *bridge, int port,
  885. u16 vid, bool untagged,
  886. bool pvid)
  887. {
  888. struct gswip_pce_table_entry vlan_mapping = {0,};
  889. unsigned int max_ports = priv->hw_info->max_ports;
  890. unsigned int cpu_port = priv->hw_info->cpu_port;
  891. bool active_vlan_created = false;
  892. int idx = -1;
  893. int fid = -1;
  894. int i;
  895. int err;
  896. /* Check if there is already a page for this bridge */
  897. for (i = max_ports; i < ARRAY_SIZE(priv->vlans); i++) {
  898. if (priv->vlans[i].bridge == bridge) {
  899. if (fid != -1 && fid != priv->vlans[i].fid)
  900. dev_err(priv->dev, "one bridge with multiple flow ids\n");
  901. fid = priv->vlans[i].fid;
  902. if (priv->vlans[i].vid == vid) {
  903. idx = i;
  904. break;
  905. }
  906. }
  907. }
  908. /* If this bridge is not programmed yet, add a Active VLAN table
  909. * entry in a free slot and prepare the VLAN mapping table entry.
  910. */
  911. if (idx == -1) {
  912. idx = gswip_vlan_active_create(priv, bridge, fid, vid);
  913. if (idx < 0)
  914. return idx;
  915. active_vlan_created = true;
  916. vlan_mapping.index = idx;
  917. vlan_mapping.table = GSWIP_TABLE_VLAN_MAPPING;
  918. /* VLAN ID byte, maps to the VLAN ID of vlan active table */
  919. vlan_mapping.val[0] = vid;
  920. } else {
  921. /* Read the existing VLAN mapping entry from the switch */
  922. vlan_mapping.index = idx;
  923. vlan_mapping.table = GSWIP_TABLE_VLAN_MAPPING;
  924. err = gswip_pce_table_entry_read(priv, &vlan_mapping);
  925. if (err) {
  926. dev_err(priv->dev, "failed to read VLAN mapping: %d\n",
  927. err);
  928. return err;
  929. }
  930. }
  931. vlan_mapping.val[0] = vid;
  932. /* Update the VLAN mapping entry and write it to the switch */
  933. vlan_mapping.val[1] |= BIT(cpu_port);
  934. vlan_mapping.val[2] |= BIT(cpu_port);
  935. vlan_mapping.val[1] |= BIT(port);
  936. if (untagged)
  937. vlan_mapping.val[2] &= ~BIT(port);
  938. else
  939. vlan_mapping.val[2] |= BIT(port);
  940. err = gswip_pce_table_entry_write(priv, &vlan_mapping);
  941. if (err) {
  942. dev_err(priv->dev, "failed to write VLAN mapping: %d\n", err);
  943. /* In case an Active VLAN was creaetd delete it again */
  944. if (active_vlan_created)
  945. gswip_vlan_active_remove(priv, idx);
  946. return err;
  947. }
  948. if (pvid)
  949. gswip_switch_w(priv, idx, GSWIP_PCE_DEFPVID(port));
  950. return 0;
  951. }
  952. static int gswip_vlan_remove(struct gswip_priv *priv,
  953. struct net_device *bridge, int port,
  954. u16 vid, bool pvid, bool vlan_aware)
  955. {
  956. struct gswip_pce_table_entry vlan_mapping = {0,};
  957. unsigned int max_ports = priv->hw_info->max_ports;
  958. unsigned int cpu_port = priv->hw_info->cpu_port;
  959. int idx = -1;
  960. int i;
  961. int err;
  962. /* Check if there is already a page for this bridge */
  963. for (i = max_ports; i < ARRAY_SIZE(priv->vlans); i++) {
  964. if (priv->vlans[i].bridge == bridge &&
  965. (!vlan_aware || priv->vlans[i].vid == vid)) {
  966. idx = i;
  967. break;
  968. }
  969. }
  970. if (idx == -1) {
  971. dev_err(priv->dev, "bridge to leave does not exists\n");
  972. return -ENOENT;
  973. }
  974. vlan_mapping.index = idx;
  975. vlan_mapping.table = GSWIP_TABLE_VLAN_MAPPING;
  976. err = gswip_pce_table_entry_read(priv, &vlan_mapping);
  977. if (err) {
  978. dev_err(priv->dev, "failed to read VLAN mapping: %d\n", err);
  979. return err;
  980. }
  981. vlan_mapping.val[1] &= ~BIT(port);
  982. vlan_mapping.val[2] &= ~BIT(port);
  983. err = gswip_pce_table_entry_write(priv, &vlan_mapping);
  984. if (err) {
  985. dev_err(priv->dev, "failed to write VLAN mapping: %d\n", err);
  986. return err;
  987. }
  988. /* In case all ports are removed from the bridge, remove the VLAN */
  989. if ((vlan_mapping.val[1] & ~BIT(cpu_port)) == 0) {
  990. err = gswip_vlan_active_remove(priv, idx);
  991. if (err) {
  992. dev_err(priv->dev, "failed to write active VLAN: %d\n",
  993. err);
  994. return err;
  995. }
  996. }
  997. /* GSWIP 2.2 (GRX300) and later program here the VID directly. */
  998. if (pvid)
  999. gswip_switch_w(priv, 0, GSWIP_PCE_DEFPVID(port));
  1000. return 0;
  1001. }
  1002. static int gswip_port_bridge_join(struct dsa_switch *ds, int port,
  1003. struct dsa_bridge bridge,
  1004. bool *tx_fwd_offload,
  1005. struct netlink_ext_ack *extack)
  1006. {
  1007. struct net_device *br = bridge.dev;
  1008. struct gswip_priv *priv = ds->priv;
  1009. int err;
  1010. /* When the bridge uses VLAN filtering we have to configure VLAN
  1011. * specific bridges. No bridge is configured here.
  1012. */
  1013. if (!br_vlan_enabled(br)) {
  1014. err = gswip_vlan_add_unaware(priv, br, port);
  1015. if (err)
  1016. return err;
  1017. priv->port_vlan_filter &= ~BIT(port);
  1018. } else {
  1019. priv->port_vlan_filter |= BIT(port);
  1020. }
  1021. return gswip_add_single_port_br(priv, port, false);
  1022. }
  1023. static void gswip_port_bridge_leave(struct dsa_switch *ds, int port,
  1024. struct dsa_bridge bridge)
  1025. {
  1026. struct net_device *br = bridge.dev;
  1027. struct gswip_priv *priv = ds->priv;
  1028. gswip_add_single_port_br(priv, port, true);
  1029. /* When the bridge uses VLAN filtering we have to configure VLAN
  1030. * specific bridges. No bridge is configured here.
  1031. */
  1032. if (!br_vlan_enabled(br))
  1033. gswip_vlan_remove(priv, br, port, 0, true, false);
  1034. }
  1035. static int gswip_port_vlan_prepare(struct dsa_switch *ds, int port,
  1036. const struct switchdev_obj_port_vlan *vlan,
  1037. struct netlink_ext_ack *extack)
  1038. {
  1039. struct net_device *bridge = dsa_port_bridge_dev_get(dsa_to_port(ds, port));
  1040. struct gswip_priv *priv = ds->priv;
  1041. unsigned int max_ports = priv->hw_info->max_ports;
  1042. int pos = max_ports;
  1043. int i, idx = -1;
  1044. /* We only support VLAN filtering on bridges */
  1045. if (!dsa_is_cpu_port(ds, port) && !bridge)
  1046. return -EOPNOTSUPP;
  1047. /* Check if there is already a page for this VLAN */
  1048. for (i = max_ports; i < ARRAY_SIZE(priv->vlans); i++) {
  1049. if (priv->vlans[i].bridge == bridge &&
  1050. priv->vlans[i].vid == vlan->vid) {
  1051. idx = i;
  1052. break;
  1053. }
  1054. }
  1055. /* If this VLAN is not programmed yet, we have to reserve
  1056. * one entry in the VLAN table. Make sure we start at the
  1057. * next position round.
  1058. */
  1059. if (idx == -1) {
  1060. /* Look for a free slot */
  1061. for (; pos < ARRAY_SIZE(priv->vlans); pos++) {
  1062. if (!priv->vlans[pos].bridge) {
  1063. idx = pos;
  1064. pos++;
  1065. break;
  1066. }
  1067. }
  1068. if (idx == -1) {
  1069. NL_SET_ERR_MSG_MOD(extack, "No slot in VLAN table");
  1070. return -ENOSPC;
  1071. }
  1072. }
  1073. return 0;
  1074. }
  1075. static int gswip_port_vlan_add(struct dsa_switch *ds, int port,
  1076. const struct switchdev_obj_port_vlan *vlan,
  1077. struct netlink_ext_ack *extack)
  1078. {
  1079. struct net_device *bridge = dsa_port_bridge_dev_get(dsa_to_port(ds, port));
  1080. struct gswip_priv *priv = ds->priv;
  1081. bool untagged = vlan->flags & BRIDGE_VLAN_INFO_UNTAGGED;
  1082. bool pvid = vlan->flags & BRIDGE_VLAN_INFO_PVID;
  1083. int err;
  1084. err = gswip_port_vlan_prepare(ds, port, vlan, extack);
  1085. if (err)
  1086. return err;
  1087. /* We have to receive all packets on the CPU port and should not
  1088. * do any VLAN filtering here. This is also called with bridge
  1089. * NULL and then we do not know for which bridge to configure
  1090. * this.
  1091. */
  1092. if (dsa_is_cpu_port(ds, port))
  1093. return 0;
  1094. return gswip_vlan_add_aware(priv, bridge, port, vlan->vid,
  1095. untagged, pvid);
  1096. }
  1097. static int gswip_port_vlan_del(struct dsa_switch *ds, int port,
  1098. const struct switchdev_obj_port_vlan *vlan)
  1099. {
  1100. struct net_device *bridge = dsa_port_bridge_dev_get(dsa_to_port(ds, port));
  1101. struct gswip_priv *priv = ds->priv;
  1102. bool pvid = vlan->flags & BRIDGE_VLAN_INFO_PVID;
  1103. /* We have to receive all packets on the CPU port and should not
  1104. * do any VLAN filtering here. This is also called with bridge
  1105. * NULL and then we do not know for which bridge to configure
  1106. * this.
  1107. */
  1108. if (dsa_is_cpu_port(ds, port))
  1109. return 0;
  1110. return gswip_vlan_remove(priv, bridge, port, vlan->vid, pvid, true);
  1111. }
  1112. static void gswip_port_fast_age(struct dsa_switch *ds, int port)
  1113. {
  1114. struct gswip_priv *priv = ds->priv;
  1115. struct gswip_pce_table_entry mac_bridge = {0,};
  1116. int i;
  1117. int err;
  1118. for (i = 0; i < 2048; i++) {
  1119. mac_bridge.table = GSWIP_TABLE_MAC_BRIDGE;
  1120. mac_bridge.index = i;
  1121. err = gswip_pce_table_entry_read(priv, &mac_bridge);
  1122. if (err) {
  1123. dev_err(priv->dev, "failed to read mac bridge: %d\n",
  1124. err);
  1125. return;
  1126. }
  1127. if (!mac_bridge.valid)
  1128. continue;
  1129. if (mac_bridge.val[1] & GSWIP_TABLE_MAC_BRIDGE_VAL1_STATIC)
  1130. continue;
  1131. if (port != FIELD_GET(GSWIP_TABLE_MAC_BRIDGE_VAL0_PORT,
  1132. mac_bridge.val[0]))
  1133. continue;
  1134. mac_bridge.valid = false;
  1135. err = gswip_pce_table_entry_write(priv, &mac_bridge);
  1136. if (err) {
  1137. dev_err(priv->dev, "failed to write mac bridge: %d\n",
  1138. err);
  1139. return;
  1140. }
  1141. }
  1142. }
  1143. static void gswip_port_stp_state_set(struct dsa_switch *ds, int port, u8 state)
  1144. {
  1145. struct gswip_priv *priv = ds->priv;
  1146. u32 stp_state;
  1147. switch (state) {
  1148. case BR_STATE_DISABLED:
  1149. gswip_switch_mask(priv, GSWIP_SDMA_PCTRL_EN, 0,
  1150. GSWIP_SDMA_PCTRLp(port));
  1151. return;
  1152. case BR_STATE_BLOCKING:
  1153. case BR_STATE_LISTENING:
  1154. stp_state = GSWIP_PCE_PCTRL_0_PSTATE_LISTEN;
  1155. break;
  1156. case BR_STATE_LEARNING:
  1157. stp_state = GSWIP_PCE_PCTRL_0_PSTATE_LEARNING;
  1158. break;
  1159. case BR_STATE_FORWARDING:
  1160. stp_state = GSWIP_PCE_PCTRL_0_PSTATE_FORWARDING;
  1161. break;
  1162. default:
  1163. dev_err(priv->dev, "invalid STP state: %d\n", state);
  1164. return;
  1165. }
  1166. gswip_switch_mask(priv, 0, GSWIP_SDMA_PCTRL_EN,
  1167. GSWIP_SDMA_PCTRLp(port));
  1168. gswip_switch_mask(priv, GSWIP_PCE_PCTRL_0_PSTATE_MASK, stp_state,
  1169. GSWIP_PCE_PCTRL_0p(port));
  1170. }
  1171. static int gswip_port_fdb(struct dsa_switch *ds, int port,
  1172. const unsigned char *addr, u16 vid, bool add)
  1173. {
  1174. struct net_device *bridge = dsa_port_bridge_dev_get(dsa_to_port(ds, port));
  1175. struct gswip_priv *priv = ds->priv;
  1176. struct gswip_pce_table_entry mac_bridge = {0,};
  1177. unsigned int max_ports = priv->hw_info->max_ports;
  1178. int fid = -1;
  1179. int i;
  1180. int err;
  1181. /* Operation not supported on the CPU port, don't throw errors */
  1182. if (!bridge)
  1183. return 0;
  1184. for (i = max_ports; i < ARRAY_SIZE(priv->vlans); i++) {
  1185. if (priv->vlans[i].bridge == bridge) {
  1186. fid = priv->vlans[i].fid;
  1187. break;
  1188. }
  1189. }
  1190. if (fid == -1) {
  1191. dev_err(priv->dev, "no FID found for bridge %s\n",
  1192. bridge->name);
  1193. return -EINVAL;
  1194. }
  1195. mac_bridge.table = GSWIP_TABLE_MAC_BRIDGE;
  1196. mac_bridge.key_mode = true;
  1197. mac_bridge.key[0] = addr[5] | (addr[4] << 8);
  1198. mac_bridge.key[1] = addr[3] | (addr[2] << 8);
  1199. mac_bridge.key[2] = addr[1] | (addr[0] << 8);
  1200. mac_bridge.key[3] = FIELD_PREP(GSWIP_TABLE_MAC_BRIDGE_KEY3_FID, fid);
  1201. mac_bridge.val[0] = add ? BIT(port) : 0; /* port map */
  1202. mac_bridge.val[1] = GSWIP_TABLE_MAC_BRIDGE_VAL1_STATIC;
  1203. mac_bridge.valid = add;
  1204. err = gswip_pce_table_entry_write(priv, &mac_bridge);
  1205. if (err)
  1206. dev_err(priv->dev, "failed to write mac bridge: %d\n", err);
  1207. return err;
  1208. }
  1209. static int gswip_port_fdb_add(struct dsa_switch *ds, int port,
  1210. const unsigned char *addr, u16 vid,
  1211. struct dsa_db db)
  1212. {
  1213. return gswip_port_fdb(ds, port, addr, vid, true);
  1214. }
  1215. static int gswip_port_fdb_del(struct dsa_switch *ds, int port,
  1216. const unsigned char *addr, u16 vid,
  1217. struct dsa_db db)
  1218. {
  1219. return gswip_port_fdb(ds, port, addr, vid, false);
  1220. }
  1221. static int gswip_port_fdb_dump(struct dsa_switch *ds, int port,
  1222. dsa_fdb_dump_cb_t *cb, void *data)
  1223. {
  1224. struct gswip_priv *priv = ds->priv;
  1225. struct gswip_pce_table_entry mac_bridge = {0,};
  1226. unsigned char addr[ETH_ALEN];
  1227. int i;
  1228. int err;
  1229. for (i = 0; i < 2048; i++) {
  1230. mac_bridge.table = GSWIP_TABLE_MAC_BRIDGE;
  1231. mac_bridge.index = i;
  1232. err = gswip_pce_table_entry_read(priv, &mac_bridge);
  1233. if (err) {
  1234. dev_err(priv->dev,
  1235. "failed to read mac bridge entry %d: %d\n",
  1236. i, err);
  1237. return err;
  1238. }
  1239. if (!mac_bridge.valid)
  1240. continue;
  1241. addr[5] = mac_bridge.key[0] & 0xff;
  1242. addr[4] = (mac_bridge.key[0] >> 8) & 0xff;
  1243. addr[3] = mac_bridge.key[1] & 0xff;
  1244. addr[2] = (mac_bridge.key[1] >> 8) & 0xff;
  1245. addr[1] = mac_bridge.key[2] & 0xff;
  1246. addr[0] = (mac_bridge.key[2] >> 8) & 0xff;
  1247. if (mac_bridge.val[1] & GSWIP_TABLE_MAC_BRIDGE_VAL1_STATIC) {
  1248. if (mac_bridge.val[0] & BIT(port)) {
  1249. err = cb(addr, 0, true, data);
  1250. if (err)
  1251. return err;
  1252. }
  1253. } else {
  1254. if (port == FIELD_GET(GSWIP_TABLE_MAC_BRIDGE_VAL0_PORT,
  1255. mac_bridge.val[0])) {
  1256. err = cb(addr, 0, false, data);
  1257. if (err)
  1258. return err;
  1259. }
  1260. }
  1261. }
  1262. return 0;
  1263. }
  1264. static int gswip_port_max_mtu(struct dsa_switch *ds, int port)
  1265. {
  1266. /* Includes 8 bytes for special header. */
  1267. return GSWIP_MAX_PACKET_LENGTH - VLAN_ETH_HLEN - ETH_FCS_LEN;
  1268. }
  1269. static int gswip_port_change_mtu(struct dsa_switch *ds, int port, int new_mtu)
  1270. {
  1271. struct gswip_priv *priv = ds->priv;
  1272. /* CPU port always has maximum mtu of user ports, so use it to set
  1273. * switch frame size, including 8 byte special header.
  1274. */
  1275. if (dsa_is_cpu_port(ds, port)) {
  1276. new_mtu += 8;
  1277. gswip_switch_w(priv, VLAN_ETH_HLEN + new_mtu + ETH_FCS_LEN,
  1278. GSWIP_MAC_FLEN);
  1279. }
  1280. /* Enable MLEN for ports with non-standard MTUs, including the special
  1281. * header on the CPU port added above.
  1282. */
  1283. if (new_mtu != ETH_DATA_LEN)
  1284. gswip_switch_mask(priv, 0, GSWIP_MAC_CTRL_2_MLEN,
  1285. GSWIP_MAC_CTRL_2p(port));
  1286. else
  1287. gswip_switch_mask(priv, GSWIP_MAC_CTRL_2_MLEN, 0,
  1288. GSWIP_MAC_CTRL_2p(port));
  1289. return 0;
  1290. }
  1291. static void gswip_xrx200_phylink_get_caps(struct dsa_switch *ds, int port,
  1292. struct phylink_config *config)
  1293. {
  1294. switch (port) {
  1295. case 0:
  1296. case 1:
  1297. phy_interface_set_rgmii(config->supported_interfaces);
  1298. __set_bit(PHY_INTERFACE_MODE_MII,
  1299. config->supported_interfaces);
  1300. __set_bit(PHY_INTERFACE_MODE_REVMII,
  1301. config->supported_interfaces);
  1302. __set_bit(PHY_INTERFACE_MODE_RMII,
  1303. config->supported_interfaces);
  1304. break;
  1305. case 2:
  1306. case 3:
  1307. case 4:
  1308. case 6:
  1309. __set_bit(PHY_INTERFACE_MODE_INTERNAL,
  1310. config->supported_interfaces);
  1311. break;
  1312. case 5:
  1313. phy_interface_set_rgmii(config->supported_interfaces);
  1314. __set_bit(PHY_INTERFACE_MODE_INTERNAL,
  1315. config->supported_interfaces);
  1316. break;
  1317. }
  1318. config->mac_capabilities = MAC_ASYM_PAUSE | MAC_SYM_PAUSE |
  1319. MAC_10 | MAC_100 | MAC_1000;
  1320. }
  1321. static void gswip_xrx300_phylink_get_caps(struct dsa_switch *ds, int port,
  1322. struct phylink_config *config)
  1323. {
  1324. switch (port) {
  1325. case 0:
  1326. phy_interface_set_rgmii(config->supported_interfaces);
  1327. __set_bit(PHY_INTERFACE_MODE_GMII,
  1328. config->supported_interfaces);
  1329. __set_bit(PHY_INTERFACE_MODE_RMII,
  1330. config->supported_interfaces);
  1331. break;
  1332. case 1:
  1333. case 2:
  1334. case 3:
  1335. case 4:
  1336. case 6:
  1337. __set_bit(PHY_INTERFACE_MODE_INTERNAL,
  1338. config->supported_interfaces);
  1339. break;
  1340. case 5:
  1341. phy_interface_set_rgmii(config->supported_interfaces);
  1342. __set_bit(PHY_INTERFACE_MODE_INTERNAL,
  1343. config->supported_interfaces);
  1344. __set_bit(PHY_INTERFACE_MODE_RMII,
  1345. config->supported_interfaces);
  1346. break;
  1347. }
  1348. config->mac_capabilities = MAC_ASYM_PAUSE | MAC_SYM_PAUSE |
  1349. MAC_10 | MAC_100 | MAC_1000;
  1350. }
  1351. static void gswip_port_set_link(struct gswip_priv *priv, int port, bool link)
  1352. {
  1353. u32 mdio_phy;
  1354. if (link)
  1355. mdio_phy = GSWIP_MDIO_PHY_LINK_UP;
  1356. else
  1357. mdio_phy = GSWIP_MDIO_PHY_LINK_DOWN;
  1358. gswip_mdio_mask(priv, GSWIP_MDIO_PHY_LINK_MASK, mdio_phy,
  1359. GSWIP_MDIO_PHYp(port));
  1360. }
  1361. static void gswip_port_set_speed(struct gswip_priv *priv, int port, int speed,
  1362. phy_interface_t interface)
  1363. {
  1364. u32 mdio_phy = 0, mii_cfg = 0, mac_ctrl_0 = 0;
  1365. switch (speed) {
  1366. case SPEED_10:
  1367. mdio_phy = GSWIP_MDIO_PHY_SPEED_M10;
  1368. if (interface == PHY_INTERFACE_MODE_RMII)
  1369. mii_cfg = GSWIP_MII_CFG_RATE_M50;
  1370. else
  1371. mii_cfg = GSWIP_MII_CFG_RATE_M2P5;
  1372. mac_ctrl_0 = GSWIP_MAC_CTRL_0_GMII_MII;
  1373. break;
  1374. case SPEED_100:
  1375. mdio_phy = GSWIP_MDIO_PHY_SPEED_M100;
  1376. if (interface == PHY_INTERFACE_MODE_RMII)
  1377. mii_cfg = GSWIP_MII_CFG_RATE_M50;
  1378. else
  1379. mii_cfg = GSWIP_MII_CFG_RATE_M25;
  1380. mac_ctrl_0 = GSWIP_MAC_CTRL_0_GMII_MII;
  1381. break;
  1382. case SPEED_1000:
  1383. mdio_phy = GSWIP_MDIO_PHY_SPEED_G1;
  1384. mii_cfg = GSWIP_MII_CFG_RATE_M125;
  1385. mac_ctrl_0 = GSWIP_MAC_CTRL_0_GMII_RGMII;
  1386. break;
  1387. }
  1388. gswip_mdio_mask(priv, GSWIP_MDIO_PHY_SPEED_MASK, mdio_phy,
  1389. GSWIP_MDIO_PHYp(port));
  1390. gswip_mii_mask_cfg(priv, GSWIP_MII_CFG_RATE_MASK, mii_cfg, port);
  1391. gswip_switch_mask(priv, GSWIP_MAC_CTRL_0_GMII_MASK, mac_ctrl_0,
  1392. GSWIP_MAC_CTRL_0p(port));
  1393. }
  1394. static void gswip_port_set_duplex(struct gswip_priv *priv, int port, int duplex)
  1395. {
  1396. u32 mac_ctrl_0, mdio_phy;
  1397. if (duplex == DUPLEX_FULL) {
  1398. mac_ctrl_0 = GSWIP_MAC_CTRL_0_FDUP_EN;
  1399. mdio_phy = GSWIP_MDIO_PHY_FDUP_EN;
  1400. } else {
  1401. mac_ctrl_0 = GSWIP_MAC_CTRL_0_FDUP_DIS;
  1402. mdio_phy = GSWIP_MDIO_PHY_FDUP_DIS;
  1403. }
  1404. gswip_switch_mask(priv, GSWIP_MAC_CTRL_0_FDUP_MASK, mac_ctrl_0,
  1405. GSWIP_MAC_CTRL_0p(port));
  1406. gswip_mdio_mask(priv, GSWIP_MDIO_PHY_FDUP_MASK, mdio_phy,
  1407. GSWIP_MDIO_PHYp(port));
  1408. }
  1409. static void gswip_port_set_pause(struct gswip_priv *priv, int port,
  1410. bool tx_pause, bool rx_pause)
  1411. {
  1412. u32 mac_ctrl_0, mdio_phy;
  1413. if (tx_pause && rx_pause) {
  1414. mac_ctrl_0 = GSWIP_MAC_CTRL_0_FCON_RXTX;
  1415. mdio_phy = GSWIP_MDIO_PHY_FCONTX_EN |
  1416. GSWIP_MDIO_PHY_FCONRX_EN;
  1417. } else if (tx_pause) {
  1418. mac_ctrl_0 = GSWIP_MAC_CTRL_0_FCON_TX;
  1419. mdio_phy = GSWIP_MDIO_PHY_FCONTX_EN |
  1420. GSWIP_MDIO_PHY_FCONRX_DIS;
  1421. } else if (rx_pause) {
  1422. mac_ctrl_0 = GSWIP_MAC_CTRL_0_FCON_RX;
  1423. mdio_phy = GSWIP_MDIO_PHY_FCONTX_DIS |
  1424. GSWIP_MDIO_PHY_FCONRX_EN;
  1425. } else {
  1426. mac_ctrl_0 = GSWIP_MAC_CTRL_0_FCON_NONE;
  1427. mdio_phy = GSWIP_MDIO_PHY_FCONTX_DIS |
  1428. GSWIP_MDIO_PHY_FCONRX_DIS;
  1429. }
  1430. gswip_switch_mask(priv, GSWIP_MAC_CTRL_0_FCON_MASK,
  1431. mac_ctrl_0, GSWIP_MAC_CTRL_0p(port));
  1432. gswip_mdio_mask(priv,
  1433. GSWIP_MDIO_PHY_FCONTX_MASK |
  1434. GSWIP_MDIO_PHY_FCONRX_MASK,
  1435. mdio_phy, GSWIP_MDIO_PHYp(port));
  1436. }
  1437. static void gswip_phylink_mac_config(struct phylink_config *config,
  1438. unsigned int mode,
  1439. const struct phylink_link_state *state)
  1440. {
  1441. struct dsa_port *dp = dsa_phylink_to_port(config);
  1442. struct gswip_priv *priv = dp->ds->priv;
  1443. int port = dp->index;
  1444. u32 miicfg = 0;
  1445. miicfg |= GSWIP_MII_CFG_LDCLKDIS;
  1446. switch (state->interface) {
  1447. case PHY_INTERFACE_MODE_MII:
  1448. case PHY_INTERFACE_MODE_INTERNAL:
  1449. miicfg |= GSWIP_MII_CFG_MODE_MIIM;
  1450. break;
  1451. case PHY_INTERFACE_MODE_REVMII:
  1452. miicfg |= GSWIP_MII_CFG_MODE_MIIP;
  1453. break;
  1454. case PHY_INTERFACE_MODE_RMII:
  1455. miicfg |= GSWIP_MII_CFG_MODE_RMIIM;
  1456. break;
  1457. case PHY_INTERFACE_MODE_RGMII:
  1458. case PHY_INTERFACE_MODE_RGMII_ID:
  1459. case PHY_INTERFACE_MODE_RGMII_RXID:
  1460. case PHY_INTERFACE_MODE_RGMII_TXID:
  1461. miicfg |= GSWIP_MII_CFG_MODE_RGMII;
  1462. break;
  1463. case PHY_INTERFACE_MODE_GMII:
  1464. miicfg |= GSWIP_MII_CFG_MODE_GMII;
  1465. break;
  1466. default:
  1467. dev_err(dp->ds->dev,
  1468. "Unsupported interface: %d\n", state->interface);
  1469. return;
  1470. }
  1471. gswip_mii_mask_cfg(priv,
  1472. GSWIP_MII_CFG_MODE_MASK | GSWIP_MII_CFG_RMII_CLK |
  1473. GSWIP_MII_CFG_RGMII_IBS | GSWIP_MII_CFG_LDCLKDIS,
  1474. miicfg, port);
  1475. switch (state->interface) {
  1476. case PHY_INTERFACE_MODE_RGMII_ID:
  1477. gswip_mii_mask_pcdu(priv, GSWIP_MII_PCDU_TXDLY_MASK |
  1478. GSWIP_MII_PCDU_RXDLY_MASK, 0, port);
  1479. break;
  1480. case PHY_INTERFACE_MODE_RGMII_RXID:
  1481. gswip_mii_mask_pcdu(priv, GSWIP_MII_PCDU_RXDLY_MASK, 0, port);
  1482. break;
  1483. case PHY_INTERFACE_MODE_RGMII_TXID:
  1484. gswip_mii_mask_pcdu(priv, GSWIP_MII_PCDU_TXDLY_MASK, 0, port);
  1485. break;
  1486. default:
  1487. break;
  1488. }
  1489. }
  1490. static void gswip_phylink_mac_link_down(struct phylink_config *config,
  1491. unsigned int mode,
  1492. phy_interface_t interface)
  1493. {
  1494. struct dsa_port *dp = dsa_phylink_to_port(config);
  1495. struct gswip_priv *priv = dp->ds->priv;
  1496. int port = dp->index;
  1497. gswip_mii_mask_cfg(priv, GSWIP_MII_CFG_EN, 0, port);
  1498. if (!dsa_port_is_cpu(dp))
  1499. gswip_port_set_link(priv, port, false);
  1500. }
  1501. static void gswip_phylink_mac_link_up(struct phylink_config *config,
  1502. struct phy_device *phydev,
  1503. unsigned int mode,
  1504. phy_interface_t interface,
  1505. int speed, int duplex,
  1506. bool tx_pause, bool rx_pause)
  1507. {
  1508. struct dsa_port *dp = dsa_phylink_to_port(config);
  1509. struct gswip_priv *priv = dp->ds->priv;
  1510. int port = dp->index;
  1511. if (!dsa_port_is_cpu(dp)) {
  1512. gswip_port_set_link(priv, port, true);
  1513. gswip_port_set_speed(priv, port, speed, interface);
  1514. gswip_port_set_duplex(priv, port, duplex);
  1515. gswip_port_set_pause(priv, port, tx_pause, rx_pause);
  1516. }
  1517. gswip_mii_mask_cfg(priv, 0, GSWIP_MII_CFG_EN, port);
  1518. }
  1519. static void gswip_get_strings(struct dsa_switch *ds, int port, u32 stringset,
  1520. uint8_t *data)
  1521. {
  1522. int i;
  1523. if (stringset != ETH_SS_STATS)
  1524. return;
  1525. for (i = 0; i < ARRAY_SIZE(gswip_rmon_cnt); i++)
  1526. ethtool_puts(&data, gswip_rmon_cnt[i].name);
  1527. }
  1528. static u32 gswip_bcm_ram_entry_read(struct gswip_priv *priv, u32 table,
  1529. u32 index)
  1530. {
  1531. u32 result;
  1532. int err;
  1533. gswip_switch_w(priv, index, GSWIP_BM_RAM_ADDR);
  1534. gswip_switch_mask(priv, GSWIP_BM_RAM_CTRL_ADDR_MASK |
  1535. GSWIP_BM_RAM_CTRL_OPMOD,
  1536. table | GSWIP_BM_RAM_CTRL_BAS,
  1537. GSWIP_BM_RAM_CTRL);
  1538. err = gswip_switch_r_timeout(priv, GSWIP_BM_RAM_CTRL,
  1539. GSWIP_BM_RAM_CTRL_BAS);
  1540. if (err) {
  1541. dev_err(priv->dev, "timeout while reading table: %u, index: %u\n",
  1542. table, index);
  1543. return 0;
  1544. }
  1545. result = gswip_switch_r(priv, GSWIP_BM_RAM_VAL(0));
  1546. result |= gswip_switch_r(priv, GSWIP_BM_RAM_VAL(1)) << 16;
  1547. return result;
  1548. }
  1549. static void gswip_get_ethtool_stats(struct dsa_switch *ds, int port,
  1550. uint64_t *data)
  1551. {
  1552. struct gswip_priv *priv = ds->priv;
  1553. const struct gswip_rmon_cnt_desc *rmon_cnt;
  1554. int i;
  1555. u64 high;
  1556. for (i = 0; i < ARRAY_SIZE(gswip_rmon_cnt); i++) {
  1557. rmon_cnt = &gswip_rmon_cnt[i];
  1558. data[i] = gswip_bcm_ram_entry_read(priv, port,
  1559. rmon_cnt->offset);
  1560. if (rmon_cnt->size == 2) {
  1561. high = gswip_bcm_ram_entry_read(priv, port,
  1562. rmon_cnt->offset + 1);
  1563. data[i] |= high << 32;
  1564. }
  1565. }
  1566. }
  1567. static int gswip_get_sset_count(struct dsa_switch *ds, int port, int sset)
  1568. {
  1569. if (sset != ETH_SS_STATS)
  1570. return 0;
  1571. return ARRAY_SIZE(gswip_rmon_cnt);
  1572. }
  1573. static const struct phylink_mac_ops gswip_phylink_mac_ops = {
  1574. .mac_config = gswip_phylink_mac_config,
  1575. .mac_link_down = gswip_phylink_mac_link_down,
  1576. .mac_link_up = gswip_phylink_mac_link_up,
  1577. };
  1578. static const struct dsa_switch_ops gswip_xrx200_switch_ops = {
  1579. .get_tag_protocol = gswip_get_tag_protocol,
  1580. .setup = gswip_setup,
  1581. .port_setup = gswip_port_setup,
  1582. .port_enable = gswip_port_enable,
  1583. .port_disable = gswip_port_disable,
  1584. .port_bridge_join = gswip_port_bridge_join,
  1585. .port_bridge_leave = gswip_port_bridge_leave,
  1586. .port_fast_age = gswip_port_fast_age,
  1587. .port_vlan_filtering = gswip_port_vlan_filtering,
  1588. .port_vlan_add = gswip_port_vlan_add,
  1589. .port_vlan_del = gswip_port_vlan_del,
  1590. .port_stp_state_set = gswip_port_stp_state_set,
  1591. .port_fdb_add = gswip_port_fdb_add,
  1592. .port_fdb_del = gswip_port_fdb_del,
  1593. .port_fdb_dump = gswip_port_fdb_dump,
  1594. .port_change_mtu = gswip_port_change_mtu,
  1595. .port_max_mtu = gswip_port_max_mtu,
  1596. .phylink_get_caps = gswip_xrx200_phylink_get_caps,
  1597. .get_strings = gswip_get_strings,
  1598. .get_ethtool_stats = gswip_get_ethtool_stats,
  1599. .get_sset_count = gswip_get_sset_count,
  1600. };
  1601. static const struct dsa_switch_ops gswip_xrx300_switch_ops = {
  1602. .get_tag_protocol = gswip_get_tag_protocol,
  1603. .setup = gswip_setup,
  1604. .port_enable = gswip_port_enable,
  1605. .port_disable = gswip_port_disable,
  1606. .port_bridge_join = gswip_port_bridge_join,
  1607. .port_bridge_leave = gswip_port_bridge_leave,
  1608. .port_fast_age = gswip_port_fast_age,
  1609. .port_vlan_filtering = gswip_port_vlan_filtering,
  1610. .port_vlan_add = gswip_port_vlan_add,
  1611. .port_vlan_del = gswip_port_vlan_del,
  1612. .port_stp_state_set = gswip_port_stp_state_set,
  1613. .port_fdb_add = gswip_port_fdb_add,
  1614. .port_fdb_del = gswip_port_fdb_del,
  1615. .port_fdb_dump = gswip_port_fdb_dump,
  1616. .port_change_mtu = gswip_port_change_mtu,
  1617. .port_max_mtu = gswip_port_max_mtu,
  1618. .phylink_get_caps = gswip_xrx300_phylink_get_caps,
  1619. .get_strings = gswip_get_strings,
  1620. .get_ethtool_stats = gswip_get_ethtool_stats,
  1621. .get_sset_count = gswip_get_sset_count,
  1622. };
  1623. static const struct xway_gphy_match_data xrx200a1x_gphy_data = {
  1624. .fe_firmware_name = "lantiq/xrx200_phy22f_a14.bin",
  1625. .ge_firmware_name = "lantiq/xrx200_phy11g_a14.bin",
  1626. };
  1627. static const struct xway_gphy_match_data xrx200a2x_gphy_data = {
  1628. .fe_firmware_name = "lantiq/xrx200_phy22f_a22.bin",
  1629. .ge_firmware_name = "lantiq/xrx200_phy11g_a22.bin",
  1630. };
  1631. static const struct xway_gphy_match_data xrx300_gphy_data = {
  1632. .fe_firmware_name = "lantiq/xrx300_phy22f_a21.bin",
  1633. .ge_firmware_name = "lantiq/xrx300_phy11g_a21.bin",
  1634. };
  1635. static const struct of_device_id xway_gphy_match[] __maybe_unused = {
  1636. { .compatible = "lantiq,xrx200-gphy-fw", .data = NULL },
  1637. { .compatible = "lantiq,xrx200a1x-gphy-fw", .data = &xrx200a1x_gphy_data },
  1638. { .compatible = "lantiq,xrx200a2x-gphy-fw", .data = &xrx200a2x_gphy_data },
  1639. { .compatible = "lantiq,xrx300-gphy-fw", .data = &xrx300_gphy_data },
  1640. { .compatible = "lantiq,xrx330-gphy-fw", .data = &xrx300_gphy_data },
  1641. {},
  1642. };
  1643. static int gswip_gphy_fw_load(struct gswip_priv *priv, struct gswip_gphy_fw *gphy_fw)
  1644. {
  1645. struct device *dev = priv->dev;
  1646. const struct firmware *fw;
  1647. void *fw_addr;
  1648. dma_addr_t dma_addr;
  1649. dma_addr_t dev_addr;
  1650. size_t size;
  1651. int ret;
  1652. ret = clk_prepare_enable(gphy_fw->clk_gate);
  1653. if (ret)
  1654. return ret;
  1655. reset_control_assert(gphy_fw->reset);
  1656. /* The vendor BSP uses a 200ms delay after asserting the reset line.
  1657. * Without this some users are observing that the PHY is not coming up
  1658. * on the MDIO bus.
  1659. */
  1660. msleep(200);
  1661. ret = request_firmware(&fw, gphy_fw->fw_name, dev);
  1662. if (ret)
  1663. return dev_err_probe(dev, ret, "failed to load firmware: %s\n",
  1664. gphy_fw->fw_name);
  1665. /* GPHY cores need the firmware code in a persistent and contiguous
  1666. * memory area with a 16 kB boundary aligned start address.
  1667. */
  1668. size = fw->size + XRX200_GPHY_FW_ALIGN;
  1669. fw_addr = dmam_alloc_coherent(dev, size, &dma_addr, GFP_KERNEL);
  1670. if (fw_addr) {
  1671. fw_addr = PTR_ALIGN(fw_addr, XRX200_GPHY_FW_ALIGN);
  1672. dev_addr = ALIGN(dma_addr, XRX200_GPHY_FW_ALIGN);
  1673. memcpy(fw_addr, fw->data, fw->size);
  1674. } else {
  1675. release_firmware(fw);
  1676. return dev_err_probe(dev, -ENOMEM,
  1677. "failed to alloc firmware memory\n");
  1678. }
  1679. release_firmware(fw);
  1680. ret = regmap_write(priv->rcu_regmap, gphy_fw->fw_addr_offset, dev_addr);
  1681. if (ret)
  1682. return ret;
  1683. reset_control_deassert(gphy_fw->reset);
  1684. return ret;
  1685. }
  1686. static int gswip_gphy_fw_probe(struct gswip_priv *priv,
  1687. struct gswip_gphy_fw *gphy_fw,
  1688. struct device_node *gphy_fw_np, int i)
  1689. {
  1690. struct device *dev = priv->dev;
  1691. u32 gphy_mode;
  1692. int ret;
  1693. char gphyname[10];
  1694. snprintf(gphyname, sizeof(gphyname), "gphy%d", i);
  1695. gphy_fw->clk_gate = devm_clk_get(dev, gphyname);
  1696. if (IS_ERR(gphy_fw->clk_gate)) {
  1697. return dev_err_probe(dev, PTR_ERR(gphy_fw->clk_gate),
  1698. "Failed to lookup gate clock\n");
  1699. }
  1700. ret = of_property_read_u32(gphy_fw_np, "reg", &gphy_fw->fw_addr_offset);
  1701. if (ret)
  1702. return ret;
  1703. ret = of_property_read_u32(gphy_fw_np, "lantiq,gphy-mode", &gphy_mode);
  1704. /* Default to GE mode */
  1705. if (ret)
  1706. gphy_mode = GPHY_MODE_GE;
  1707. switch (gphy_mode) {
  1708. case GPHY_MODE_FE:
  1709. gphy_fw->fw_name = priv->gphy_fw_name_cfg->fe_firmware_name;
  1710. break;
  1711. case GPHY_MODE_GE:
  1712. gphy_fw->fw_name = priv->gphy_fw_name_cfg->ge_firmware_name;
  1713. break;
  1714. default:
  1715. return dev_err_probe(dev, -EINVAL, "Unknown GPHY mode %d\n",
  1716. gphy_mode);
  1717. }
  1718. gphy_fw->reset = of_reset_control_array_get_exclusive(gphy_fw_np);
  1719. if (IS_ERR(gphy_fw->reset))
  1720. return dev_err_probe(dev, PTR_ERR(gphy_fw->reset),
  1721. "Failed to lookup gphy reset\n");
  1722. return gswip_gphy_fw_load(priv, gphy_fw);
  1723. }
  1724. static void gswip_gphy_fw_remove(struct gswip_priv *priv,
  1725. struct gswip_gphy_fw *gphy_fw)
  1726. {
  1727. int ret;
  1728. /* check if the device was fully probed */
  1729. if (!gphy_fw->fw_name)
  1730. return;
  1731. ret = regmap_write(priv->rcu_regmap, gphy_fw->fw_addr_offset, 0);
  1732. if (ret)
  1733. dev_err(priv->dev, "can not reset GPHY FW pointer\n");
  1734. clk_disable_unprepare(gphy_fw->clk_gate);
  1735. reset_control_put(gphy_fw->reset);
  1736. }
  1737. static int gswip_gphy_fw_list(struct gswip_priv *priv,
  1738. struct device_node *gphy_fw_list_np, u32 version)
  1739. {
  1740. struct device *dev = priv->dev;
  1741. struct device_node *gphy_fw_np;
  1742. const struct of_device_id *match;
  1743. int err;
  1744. int i = 0;
  1745. /* The VRX200 rev 1.1 uses the GSWIP 2.0 and needs the older
  1746. * GPHY firmware. The VRX200 rev 1.2 uses the GSWIP 2.1 and also
  1747. * needs a different GPHY firmware.
  1748. */
  1749. if (of_device_is_compatible(gphy_fw_list_np, "lantiq,xrx200-gphy-fw")) {
  1750. switch (version) {
  1751. case GSWIP_VERSION_2_0:
  1752. priv->gphy_fw_name_cfg = &xrx200a1x_gphy_data;
  1753. break;
  1754. case GSWIP_VERSION_2_1:
  1755. priv->gphy_fw_name_cfg = &xrx200a2x_gphy_data;
  1756. break;
  1757. default:
  1758. return dev_err_probe(dev, -ENOENT,
  1759. "unknown GSWIP version: 0x%x\n",
  1760. version);
  1761. }
  1762. }
  1763. match = of_match_node(xway_gphy_match, gphy_fw_list_np);
  1764. if (match && match->data)
  1765. priv->gphy_fw_name_cfg = match->data;
  1766. if (!priv->gphy_fw_name_cfg)
  1767. return dev_err_probe(dev, -ENOENT,
  1768. "GPHY compatible type not supported\n");
  1769. priv->num_gphy_fw = of_get_available_child_count(gphy_fw_list_np);
  1770. if (!priv->num_gphy_fw)
  1771. return -ENOENT;
  1772. priv->rcu_regmap = syscon_regmap_lookup_by_phandle(gphy_fw_list_np,
  1773. "lantiq,rcu");
  1774. if (IS_ERR(priv->rcu_regmap))
  1775. return PTR_ERR(priv->rcu_regmap);
  1776. priv->gphy_fw = devm_kmalloc_array(dev, priv->num_gphy_fw,
  1777. sizeof(*priv->gphy_fw),
  1778. GFP_KERNEL | __GFP_ZERO);
  1779. if (!priv->gphy_fw)
  1780. return -ENOMEM;
  1781. for_each_available_child_of_node(gphy_fw_list_np, gphy_fw_np) {
  1782. err = gswip_gphy_fw_probe(priv, &priv->gphy_fw[i],
  1783. gphy_fw_np, i);
  1784. if (err) {
  1785. of_node_put(gphy_fw_np);
  1786. goto remove_gphy;
  1787. }
  1788. i++;
  1789. }
  1790. /* The standalone PHY11G requires 300ms to be fully
  1791. * initialized and ready for any MDIO communication after being
  1792. * taken out of reset. For the SoC-internal GPHY variant there
  1793. * is no (known) documentation for the minimum time after a
  1794. * reset. Use the same value as for the standalone variant as
  1795. * some users have reported internal PHYs not being detected
  1796. * without any delay.
  1797. */
  1798. msleep(300);
  1799. return 0;
  1800. remove_gphy:
  1801. for (i = 0; i < priv->num_gphy_fw; i++)
  1802. gswip_gphy_fw_remove(priv, &priv->gphy_fw[i]);
  1803. return err;
  1804. }
  1805. static int gswip_probe(struct platform_device *pdev)
  1806. {
  1807. struct device_node *np, *gphy_fw_np;
  1808. struct device *dev = &pdev->dev;
  1809. struct gswip_priv *priv;
  1810. int err;
  1811. int i;
  1812. u32 version;
  1813. priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
  1814. if (!priv)
  1815. return -ENOMEM;
  1816. priv->gswip = devm_platform_ioremap_resource(pdev, 0);
  1817. if (IS_ERR(priv->gswip))
  1818. return PTR_ERR(priv->gswip);
  1819. priv->mdio = devm_platform_ioremap_resource(pdev, 1);
  1820. if (IS_ERR(priv->mdio))
  1821. return PTR_ERR(priv->mdio);
  1822. priv->mii = devm_platform_ioremap_resource(pdev, 2);
  1823. if (IS_ERR(priv->mii))
  1824. return PTR_ERR(priv->mii);
  1825. priv->hw_info = of_device_get_match_data(dev);
  1826. if (!priv->hw_info)
  1827. return -EINVAL;
  1828. priv->ds = devm_kzalloc(dev, sizeof(*priv->ds), GFP_KERNEL);
  1829. if (!priv->ds)
  1830. return -ENOMEM;
  1831. priv->ds->dev = dev;
  1832. priv->ds->num_ports = priv->hw_info->max_ports;
  1833. priv->ds->priv = priv;
  1834. priv->ds->ops = priv->hw_info->ops;
  1835. priv->ds->phylink_mac_ops = &gswip_phylink_mac_ops;
  1836. priv->dev = dev;
  1837. mutex_init(&priv->pce_table_lock);
  1838. version = gswip_switch_r(priv, GSWIP_VERSION);
  1839. np = dev->of_node;
  1840. switch (version) {
  1841. case GSWIP_VERSION_2_0:
  1842. case GSWIP_VERSION_2_1:
  1843. if (!of_device_is_compatible(np, "lantiq,xrx200-gswip"))
  1844. return -EINVAL;
  1845. break;
  1846. case GSWIP_VERSION_2_2:
  1847. case GSWIP_VERSION_2_2_ETC:
  1848. if (!of_device_is_compatible(np, "lantiq,xrx300-gswip") &&
  1849. !of_device_is_compatible(np, "lantiq,xrx330-gswip"))
  1850. return -EINVAL;
  1851. break;
  1852. default:
  1853. return dev_err_probe(dev, -ENOENT,
  1854. "unknown GSWIP version: 0x%x\n", version);
  1855. }
  1856. /* bring up the mdio bus */
  1857. gphy_fw_np = of_get_compatible_child(dev->of_node, "lantiq,gphy-fw");
  1858. if (gphy_fw_np) {
  1859. err = gswip_gphy_fw_list(priv, gphy_fw_np, version);
  1860. of_node_put(gphy_fw_np);
  1861. if (err)
  1862. return dev_err_probe(dev, err,
  1863. "gphy fw probe failed\n");
  1864. }
  1865. /* bring up the mdio bus */
  1866. err = gswip_mdio(priv);
  1867. if (err) {
  1868. dev_err_probe(dev, err, "mdio probe failed\n");
  1869. goto gphy_fw_remove;
  1870. }
  1871. err = dsa_register_switch(priv->ds);
  1872. if (err) {
  1873. dev_err_probe(dev, err, "dsa switch registration failed\n");
  1874. goto gphy_fw_remove;
  1875. }
  1876. if (!dsa_is_cpu_port(priv->ds, priv->hw_info->cpu_port)) {
  1877. err = dev_err_probe(dev, -EINVAL,
  1878. "wrong CPU port defined, HW only supports port: %i\n",
  1879. priv->hw_info->cpu_port);
  1880. goto disable_switch;
  1881. }
  1882. platform_set_drvdata(pdev, priv);
  1883. dev_info(dev, "probed GSWIP version %lx mod %lx\n",
  1884. (version & GSWIP_VERSION_REV_MASK) >> GSWIP_VERSION_REV_SHIFT,
  1885. (version & GSWIP_VERSION_MOD_MASK) >> GSWIP_VERSION_MOD_SHIFT);
  1886. return 0;
  1887. disable_switch:
  1888. gswip_mdio_mask(priv, GSWIP_MDIO_GLOB_ENABLE, 0, GSWIP_MDIO_GLOB);
  1889. dsa_unregister_switch(priv->ds);
  1890. gphy_fw_remove:
  1891. for (i = 0; i < priv->num_gphy_fw; i++)
  1892. gswip_gphy_fw_remove(priv, &priv->gphy_fw[i]);
  1893. return err;
  1894. }
  1895. static void gswip_remove(struct platform_device *pdev)
  1896. {
  1897. struct gswip_priv *priv = platform_get_drvdata(pdev);
  1898. int i;
  1899. if (!priv)
  1900. return;
  1901. /* disable the switch */
  1902. gswip_mdio_mask(priv, GSWIP_MDIO_GLOB_ENABLE, 0, GSWIP_MDIO_GLOB);
  1903. dsa_unregister_switch(priv->ds);
  1904. for (i = 0; i < priv->num_gphy_fw; i++)
  1905. gswip_gphy_fw_remove(priv, &priv->gphy_fw[i]);
  1906. }
  1907. static void gswip_shutdown(struct platform_device *pdev)
  1908. {
  1909. struct gswip_priv *priv = platform_get_drvdata(pdev);
  1910. if (!priv)
  1911. return;
  1912. dsa_switch_shutdown(priv->ds);
  1913. platform_set_drvdata(pdev, NULL);
  1914. }
  1915. static const struct gswip_hw_info gswip_xrx200 = {
  1916. .max_ports = 7,
  1917. .cpu_port = 6,
  1918. .ops = &gswip_xrx200_switch_ops,
  1919. };
  1920. static const struct gswip_hw_info gswip_xrx300 = {
  1921. .max_ports = 7,
  1922. .cpu_port = 6,
  1923. .ops = &gswip_xrx300_switch_ops,
  1924. };
  1925. static const struct of_device_id gswip_of_match[] = {
  1926. { .compatible = "lantiq,xrx200-gswip", .data = &gswip_xrx200 },
  1927. { .compatible = "lantiq,xrx300-gswip", .data = &gswip_xrx300 },
  1928. { .compatible = "lantiq,xrx330-gswip", .data = &gswip_xrx300 },
  1929. {},
  1930. };
  1931. MODULE_DEVICE_TABLE(of, gswip_of_match);
  1932. static struct platform_driver gswip_driver = {
  1933. .probe = gswip_probe,
  1934. .remove_new = gswip_remove,
  1935. .shutdown = gswip_shutdown,
  1936. .driver = {
  1937. .name = "gswip",
  1938. .of_match_table = gswip_of_match,
  1939. },
  1940. };
  1941. module_platform_driver(gswip_driver);
  1942. MODULE_FIRMWARE("lantiq/xrx300_phy11g_a21.bin");
  1943. MODULE_FIRMWARE("lantiq/xrx300_phy22f_a21.bin");
  1944. MODULE_FIRMWARE("lantiq/xrx200_phy11g_a14.bin");
  1945. MODULE_FIRMWARE("lantiq/xrx200_phy11g_a22.bin");
  1946. MODULE_FIRMWARE("lantiq/xrx200_phy22f_a14.bin");
  1947. MODULE_FIRMWARE("lantiq/xrx200_phy22f_a22.bin");
  1948. MODULE_AUTHOR("Hauke Mehrtens <hauke@hauke-m.de>");
  1949. MODULE_DESCRIPTION("Lantiq / Intel GSWIP driver");
  1950. MODULE_LICENSE("GPL v2");