mt7530.c 88 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Mediatek MT7530 DSA Switch driver
  4. * Copyright (C) 2017 Sean Wang <sean.wang@mediatek.com>
  5. */
  6. #include <linux/etherdevice.h>
  7. #include <linux/if_bridge.h>
  8. #include <linux/iopoll.h>
  9. #include <linux/mdio.h>
  10. #include <linux/mfd/syscon.h>
  11. #include <linux/module.h>
  12. #include <linux/netdevice.h>
  13. #include <linux/of_irq.h>
  14. #include <linux/of_mdio.h>
  15. #include <linux/of_net.h>
  16. #include <linux/of_platform.h>
  17. #include <linux/phylink.h>
  18. #include <linux/regmap.h>
  19. #include <linux/regulator/consumer.h>
  20. #include <linux/reset.h>
  21. #include <linux/gpio/consumer.h>
  22. #include <linux/gpio/driver.h>
  23. #include <net/dsa.h>
  24. #include "mt7530.h"
  25. static struct mt753x_pcs *pcs_to_mt753x_pcs(struct phylink_pcs *pcs)
  26. {
  27. return container_of(pcs, struct mt753x_pcs, pcs);
  28. }
  29. /* String, offset, and register size in bytes if different from 4 bytes */
  30. static const struct mt7530_mib_desc mt7530_mib[] = {
  31. MIB_DESC(1, 0x00, "TxDrop"),
  32. MIB_DESC(1, 0x04, "TxCrcErr"),
  33. MIB_DESC(1, 0x08, "TxUnicast"),
  34. MIB_DESC(1, 0x0c, "TxMulticast"),
  35. MIB_DESC(1, 0x10, "TxBroadcast"),
  36. MIB_DESC(1, 0x14, "TxCollision"),
  37. MIB_DESC(1, 0x18, "TxSingleCollision"),
  38. MIB_DESC(1, 0x1c, "TxMultipleCollision"),
  39. MIB_DESC(1, 0x20, "TxDeferred"),
  40. MIB_DESC(1, 0x24, "TxLateCollision"),
  41. MIB_DESC(1, 0x28, "TxExcessiveCollistion"),
  42. MIB_DESC(1, 0x2c, "TxPause"),
  43. MIB_DESC(1, 0x30, "TxPktSz64"),
  44. MIB_DESC(1, 0x34, "TxPktSz65To127"),
  45. MIB_DESC(1, 0x38, "TxPktSz128To255"),
  46. MIB_DESC(1, 0x3c, "TxPktSz256To511"),
  47. MIB_DESC(1, 0x40, "TxPktSz512To1023"),
  48. MIB_DESC(1, 0x44, "Tx1024ToMax"),
  49. MIB_DESC(2, 0x48, "TxBytes"),
  50. MIB_DESC(1, 0x60, "RxDrop"),
  51. MIB_DESC(1, 0x64, "RxFiltering"),
  52. MIB_DESC(1, 0x68, "RxUnicast"),
  53. MIB_DESC(1, 0x6c, "RxMulticast"),
  54. MIB_DESC(1, 0x70, "RxBroadcast"),
  55. MIB_DESC(1, 0x74, "RxAlignErr"),
  56. MIB_DESC(1, 0x78, "RxCrcErr"),
  57. MIB_DESC(1, 0x7c, "RxUnderSizeErr"),
  58. MIB_DESC(1, 0x80, "RxFragErr"),
  59. MIB_DESC(1, 0x84, "RxOverSzErr"),
  60. MIB_DESC(1, 0x88, "RxJabberErr"),
  61. MIB_DESC(1, 0x8c, "RxPause"),
  62. MIB_DESC(1, 0x90, "RxPktSz64"),
  63. MIB_DESC(1, 0x94, "RxPktSz65To127"),
  64. MIB_DESC(1, 0x98, "RxPktSz128To255"),
  65. MIB_DESC(1, 0x9c, "RxPktSz256To511"),
  66. MIB_DESC(1, 0xa0, "RxPktSz512To1023"),
  67. MIB_DESC(1, 0xa4, "RxPktSz1024ToMax"),
  68. MIB_DESC(2, 0xa8, "RxBytes"),
  69. MIB_DESC(1, 0xb0, "RxCtrlDrop"),
  70. MIB_DESC(1, 0xb4, "RxIngressDrop"),
  71. MIB_DESC(1, 0xb8, "RxArlDrop"),
  72. };
  73. static void
  74. mt7530_mutex_lock(struct mt7530_priv *priv)
  75. {
  76. if (priv->bus)
  77. mutex_lock_nested(&priv->bus->mdio_lock, MDIO_MUTEX_NESTED);
  78. }
  79. static void
  80. mt7530_mutex_unlock(struct mt7530_priv *priv)
  81. {
  82. if (priv->bus)
  83. mutex_unlock(&priv->bus->mdio_lock);
  84. }
  85. static void
  86. core_write(struct mt7530_priv *priv, u32 reg, u32 val)
  87. {
  88. struct mii_bus *bus = priv->bus;
  89. int ret;
  90. mt7530_mutex_lock(priv);
  91. /* Write the desired MMD Devad */
  92. ret = bus->write(bus, MT753X_CTRL_PHY_ADDR(priv->mdiodev->addr),
  93. MII_MMD_CTRL, MDIO_MMD_VEND2);
  94. if (ret < 0)
  95. goto err;
  96. /* Write the desired MMD register address */
  97. ret = bus->write(bus, MT753X_CTRL_PHY_ADDR(priv->mdiodev->addr),
  98. MII_MMD_DATA, reg);
  99. if (ret < 0)
  100. goto err;
  101. /* Select the Function : DATA with no post increment */
  102. ret = bus->write(bus, MT753X_CTRL_PHY_ADDR(priv->mdiodev->addr),
  103. MII_MMD_CTRL, MDIO_MMD_VEND2 | MII_MMD_CTRL_NOINCR);
  104. if (ret < 0)
  105. goto err;
  106. /* Write the data into MMD's selected register */
  107. ret = bus->write(bus, MT753X_CTRL_PHY_ADDR(priv->mdiodev->addr),
  108. MII_MMD_DATA, val);
  109. err:
  110. if (ret < 0)
  111. dev_err(&bus->dev, "failed to write mmd register\n");
  112. mt7530_mutex_unlock(priv);
  113. }
  114. static void
  115. core_rmw(struct mt7530_priv *priv, u32 reg, u32 mask, u32 set)
  116. {
  117. struct mii_bus *bus = priv->bus;
  118. u32 val;
  119. int ret;
  120. mt7530_mutex_lock(priv);
  121. /* Write the desired MMD Devad */
  122. ret = bus->write(bus, MT753X_CTRL_PHY_ADDR(priv->mdiodev->addr),
  123. MII_MMD_CTRL, MDIO_MMD_VEND2);
  124. if (ret < 0)
  125. goto err;
  126. /* Write the desired MMD register address */
  127. ret = bus->write(bus, MT753X_CTRL_PHY_ADDR(priv->mdiodev->addr),
  128. MII_MMD_DATA, reg);
  129. if (ret < 0)
  130. goto err;
  131. /* Select the Function : DATA with no post increment */
  132. ret = bus->write(bus, MT753X_CTRL_PHY_ADDR(priv->mdiodev->addr),
  133. MII_MMD_CTRL, MDIO_MMD_VEND2 | MII_MMD_CTRL_NOINCR);
  134. if (ret < 0)
  135. goto err;
  136. /* Read the content of the MMD's selected register */
  137. val = bus->read(bus, MT753X_CTRL_PHY_ADDR(priv->mdiodev->addr),
  138. MII_MMD_DATA);
  139. val &= ~mask;
  140. val |= set;
  141. /* Write the data into MMD's selected register */
  142. ret = bus->write(bus, MT753X_CTRL_PHY_ADDR(priv->mdiodev->addr),
  143. MII_MMD_DATA, val);
  144. err:
  145. if (ret < 0)
  146. dev_err(&bus->dev, "failed to write mmd register\n");
  147. mt7530_mutex_unlock(priv);
  148. }
  149. static void
  150. core_set(struct mt7530_priv *priv, u32 reg, u32 val)
  151. {
  152. core_rmw(priv, reg, 0, val);
  153. }
  154. static void
  155. core_clear(struct mt7530_priv *priv, u32 reg, u32 val)
  156. {
  157. core_rmw(priv, reg, val, 0);
  158. }
  159. static int
  160. mt7530_mii_write(struct mt7530_priv *priv, u32 reg, u32 val)
  161. {
  162. int ret;
  163. ret = regmap_write(priv->regmap, reg, val);
  164. if (ret < 0)
  165. dev_err(priv->dev,
  166. "failed to write mt7530 register\n");
  167. return ret;
  168. }
  169. static u32
  170. mt7530_mii_read(struct mt7530_priv *priv, u32 reg)
  171. {
  172. int ret;
  173. u32 val;
  174. ret = regmap_read(priv->regmap, reg, &val);
  175. if (ret) {
  176. WARN_ON_ONCE(1);
  177. dev_err(priv->dev,
  178. "failed to read mt7530 register\n");
  179. return 0;
  180. }
  181. return val;
  182. }
  183. static void
  184. mt7530_write(struct mt7530_priv *priv, u32 reg, u32 val)
  185. {
  186. mt7530_mutex_lock(priv);
  187. mt7530_mii_write(priv, reg, val);
  188. mt7530_mutex_unlock(priv);
  189. }
  190. static u32
  191. _mt7530_unlocked_read(struct mt7530_dummy_poll *p)
  192. {
  193. return mt7530_mii_read(p->priv, p->reg);
  194. }
  195. static u32
  196. _mt7530_read(struct mt7530_dummy_poll *p)
  197. {
  198. u32 val;
  199. mt7530_mutex_lock(p->priv);
  200. val = mt7530_mii_read(p->priv, p->reg);
  201. mt7530_mutex_unlock(p->priv);
  202. return val;
  203. }
  204. static u32
  205. mt7530_read(struct mt7530_priv *priv, u32 reg)
  206. {
  207. struct mt7530_dummy_poll p;
  208. INIT_MT7530_DUMMY_POLL(&p, priv, reg);
  209. return _mt7530_read(&p);
  210. }
  211. static void
  212. mt7530_rmw(struct mt7530_priv *priv, u32 reg,
  213. u32 mask, u32 set)
  214. {
  215. mt7530_mutex_lock(priv);
  216. regmap_update_bits(priv->regmap, reg, mask, set);
  217. mt7530_mutex_unlock(priv);
  218. }
  219. static void
  220. mt7530_set(struct mt7530_priv *priv, u32 reg, u32 val)
  221. {
  222. mt7530_rmw(priv, reg, val, val);
  223. }
  224. static void
  225. mt7530_clear(struct mt7530_priv *priv, u32 reg, u32 val)
  226. {
  227. mt7530_rmw(priv, reg, val, 0);
  228. }
  229. static int
  230. mt7530_fdb_cmd(struct mt7530_priv *priv, enum mt7530_fdb_cmd cmd, u32 *rsp)
  231. {
  232. u32 val;
  233. int ret;
  234. struct mt7530_dummy_poll p;
  235. /* Set the command operating upon the MAC address entries */
  236. val = ATC_BUSY | ATC_MAT(0) | cmd;
  237. mt7530_write(priv, MT7530_ATC, val);
  238. INIT_MT7530_DUMMY_POLL(&p, priv, MT7530_ATC);
  239. ret = readx_poll_timeout(_mt7530_read, &p, val,
  240. !(val & ATC_BUSY), 20, 20000);
  241. if (ret < 0) {
  242. dev_err(priv->dev, "reset timeout\n");
  243. return ret;
  244. }
  245. /* Additional sanity for read command if the specified
  246. * entry is invalid
  247. */
  248. val = mt7530_read(priv, MT7530_ATC);
  249. if ((cmd == MT7530_FDB_READ) && (val & ATC_INVALID))
  250. return -EINVAL;
  251. if (rsp)
  252. *rsp = val;
  253. return 0;
  254. }
  255. static void
  256. mt7530_fdb_read(struct mt7530_priv *priv, struct mt7530_fdb *fdb)
  257. {
  258. u32 reg[3];
  259. int i;
  260. /* Read from ARL table into an array */
  261. for (i = 0; i < 3; i++) {
  262. reg[i] = mt7530_read(priv, MT7530_TSRA1 + (i * 4));
  263. dev_dbg(priv->dev, "%s(%d) reg[%d]=0x%x\n",
  264. __func__, __LINE__, i, reg[i]);
  265. }
  266. fdb->vid = (reg[1] >> CVID) & CVID_MASK;
  267. fdb->aging = (reg[2] >> AGE_TIMER) & AGE_TIMER_MASK;
  268. fdb->port_mask = (reg[2] >> PORT_MAP) & PORT_MAP_MASK;
  269. fdb->mac[0] = (reg[0] >> MAC_BYTE_0) & MAC_BYTE_MASK;
  270. fdb->mac[1] = (reg[0] >> MAC_BYTE_1) & MAC_BYTE_MASK;
  271. fdb->mac[2] = (reg[0] >> MAC_BYTE_2) & MAC_BYTE_MASK;
  272. fdb->mac[3] = (reg[0] >> MAC_BYTE_3) & MAC_BYTE_MASK;
  273. fdb->mac[4] = (reg[1] >> MAC_BYTE_4) & MAC_BYTE_MASK;
  274. fdb->mac[5] = (reg[1] >> MAC_BYTE_5) & MAC_BYTE_MASK;
  275. fdb->noarp = ((reg[2] >> ENT_STATUS) & ENT_STATUS_MASK) == STATIC_ENT;
  276. }
  277. static void
  278. mt7530_fdb_write(struct mt7530_priv *priv, u16 vid,
  279. u8 port_mask, const u8 *mac,
  280. u8 aging, u8 type)
  281. {
  282. u32 reg[3] = { 0 };
  283. int i;
  284. reg[1] |= vid & CVID_MASK;
  285. reg[1] |= ATA2_IVL;
  286. reg[1] |= ATA2_FID(FID_BRIDGED);
  287. reg[2] |= (aging & AGE_TIMER_MASK) << AGE_TIMER;
  288. reg[2] |= (port_mask & PORT_MAP_MASK) << PORT_MAP;
  289. /* STATIC_ENT indicate that entry is static wouldn't
  290. * be aged out and STATIC_EMP specified as erasing an
  291. * entry
  292. */
  293. reg[2] |= (type & ENT_STATUS_MASK) << ENT_STATUS;
  294. reg[1] |= mac[5] << MAC_BYTE_5;
  295. reg[1] |= mac[4] << MAC_BYTE_4;
  296. reg[0] |= mac[3] << MAC_BYTE_3;
  297. reg[0] |= mac[2] << MAC_BYTE_2;
  298. reg[0] |= mac[1] << MAC_BYTE_1;
  299. reg[0] |= mac[0] << MAC_BYTE_0;
  300. /* Write array into the ARL table */
  301. for (i = 0; i < 3; i++)
  302. mt7530_write(priv, MT7530_ATA1 + (i * 4), reg[i]);
  303. }
  304. /* Set up switch core clock for MT7530 */
  305. static void mt7530_pll_setup(struct mt7530_priv *priv)
  306. {
  307. /* Disable core clock */
  308. core_clear(priv, CORE_TRGMII_GSW_CLK_CG, REG_GSWCK_EN);
  309. /* Disable PLL */
  310. core_write(priv, CORE_GSWPLL_GRP1, 0);
  311. /* Set core clock into 500Mhz */
  312. core_write(priv, CORE_GSWPLL_GRP2,
  313. RG_GSWPLL_POSDIV_500M(1) |
  314. RG_GSWPLL_FBKDIV_500M(25));
  315. /* Enable PLL */
  316. core_write(priv, CORE_GSWPLL_GRP1,
  317. RG_GSWPLL_EN_PRE |
  318. RG_GSWPLL_POSDIV_200M(2) |
  319. RG_GSWPLL_FBKDIV_200M(32));
  320. udelay(20);
  321. /* Enable core clock */
  322. core_set(priv, CORE_TRGMII_GSW_CLK_CG, REG_GSWCK_EN);
  323. }
  324. /* If port 6 is available as a CPU port, always prefer that as the default,
  325. * otherwise don't care.
  326. */
  327. static struct dsa_port *
  328. mt753x_preferred_default_local_cpu_port(struct dsa_switch *ds)
  329. {
  330. struct dsa_port *cpu_dp = dsa_to_port(ds, 6);
  331. if (dsa_port_is_cpu(cpu_dp))
  332. return cpu_dp;
  333. return NULL;
  334. }
  335. /* Setup port 6 interface mode and TRGMII TX circuit */
  336. static void
  337. mt7530_setup_port6(struct dsa_switch *ds, phy_interface_t interface)
  338. {
  339. struct mt7530_priv *priv = ds->priv;
  340. u32 ncpo1, ssc_delta, xtal;
  341. /* Disable the MT7530 TRGMII clocks */
  342. core_clear(priv, CORE_TRGMII_GSW_CLK_CG, REG_TRGMIICK_EN);
  343. if (interface == PHY_INTERFACE_MODE_RGMII) {
  344. mt7530_rmw(priv, MT7530_P6ECR, P6_INTF_MODE_MASK,
  345. P6_INTF_MODE(0));
  346. return;
  347. }
  348. mt7530_rmw(priv, MT7530_P6ECR, P6_INTF_MODE_MASK, P6_INTF_MODE(1));
  349. xtal = mt7530_read(priv, MT753X_MTRAP) & MT7530_XTAL_MASK;
  350. if (xtal == MT7530_XTAL_25MHZ)
  351. ssc_delta = 0x57;
  352. else
  353. ssc_delta = 0x87;
  354. if (priv->id == ID_MT7621) {
  355. /* PLL frequency: 125MHz: 1.0GBit */
  356. if (xtal == MT7530_XTAL_40MHZ)
  357. ncpo1 = 0x0640;
  358. if (xtal == MT7530_XTAL_25MHZ)
  359. ncpo1 = 0x0a00;
  360. } else { /* PLL frequency: 250MHz: 2.0Gbit */
  361. if (xtal == MT7530_XTAL_40MHZ)
  362. ncpo1 = 0x0c80;
  363. if (xtal == MT7530_XTAL_25MHZ)
  364. ncpo1 = 0x1400;
  365. }
  366. /* Setup the MT7530 TRGMII Tx Clock */
  367. core_write(priv, CORE_PLL_GROUP5, RG_LCDDS_PCW_NCPO1(ncpo1));
  368. core_write(priv, CORE_PLL_GROUP6, RG_LCDDS_PCW_NCPO0(0));
  369. core_write(priv, CORE_PLL_GROUP10, RG_LCDDS_SSC_DELTA(ssc_delta));
  370. core_write(priv, CORE_PLL_GROUP11, RG_LCDDS_SSC_DELTA1(ssc_delta));
  371. core_write(priv, CORE_PLL_GROUP4, RG_SYSPLL_DDSFBK_EN |
  372. RG_SYSPLL_BIAS_EN | RG_SYSPLL_BIAS_LPF_EN);
  373. core_write(priv, CORE_PLL_GROUP2, RG_SYSPLL_EN_NORMAL |
  374. RG_SYSPLL_VODEN | RG_SYSPLL_POSDIV(1));
  375. core_write(priv, CORE_PLL_GROUP7, RG_LCDDS_PCW_NCPO_CHG |
  376. RG_LCCDS_C(3) | RG_LCDDS_PWDB | RG_LCDDS_ISO_EN);
  377. /* Enable the MT7530 TRGMII clocks */
  378. core_set(priv, CORE_TRGMII_GSW_CLK_CG, REG_TRGMIICK_EN);
  379. }
  380. static void
  381. mt7531_pll_setup(struct mt7530_priv *priv)
  382. {
  383. enum mt7531_xtal_fsel xtal;
  384. u32 top_sig;
  385. u32 hwstrap;
  386. u32 val;
  387. val = mt7530_read(priv, MT7531_CREV);
  388. top_sig = mt7530_read(priv, MT7531_TOP_SIG_SR);
  389. hwstrap = mt7530_read(priv, MT753X_TRAP);
  390. if ((val & CHIP_REV_M) > 0)
  391. xtal = (top_sig & PAD_MCM_SMI_EN) ? MT7531_XTAL_FSEL_40MHZ :
  392. MT7531_XTAL_FSEL_25MHZ;
  393. else
  394. xtal = (hwstrap & MT7531_XTAL25) ? MT7531_XTAL_FSEL_25MHZ :
  395. MT7531_XTAL_FSEL_40MHZ;
  396. /* Step 1 : Disable MT7531 COREPLL */
  397. val = mt7530_read(priv, MT7531_PLLGP_EN);
  398. val &= ~EN_COREPLL;
  399. mt7530_write(priv, MT7531_PLLGP_EN, val);
  400. /* Step 2: switch to XTAL output */
  401. val = mt7530_read(priv, MT7531_PLLGP_EN);
  402. val |= SW_CLKSW;
  403. mt7530_write(priv, MT7531_PLLGP_EN, val);
  404. val = mt7530_read(priv, MT7531_PLLGP_CR0);
  405. val &= ~RG_COREPLL_EN;
  406. mt7530_write(priv, MT7531_PLLGP_CR0, val);
  407. /* Step 3: disable PLLGP and enable program PLLGP */
  408. val = mt7530_read(priv, MT7531_PLLGP_EN);
  409. val |= SW_PLLGP;
  410. mt7530_write(priv, MT7531_PLLGP_EN, val);
  411. /* Step 4: program COREPLL output frequency to 500MHz */
  412. val = mt7530_read(priv, MT7531_PLLGP_CR0);
  413. val &= ~RG_COREPLL_POSDIV_M;
  414. val |= 2 << RG_COREPLL_POSDIV_S;
  415. mt7530_write(priv, MT7531_PLLGP_CR0, val);
  416. usleep_range(25, 35);
  417. switch (xtal) {
  418. case MT7531_XTAL_FSEL_25MHZ:
  419. val = mt7530_read(priv, MT7531_PLLGP_CR0);
  420. val &= ~RG_COREPLL_SDM_PCW_M;
  421. val |= 0x140000 << RG_COREPLL_SDM_PCW_S;
  422. mt7530_write(priv, MT7531_PLLGP_CR0, val);
  423. break;
  424. case MT7531_XTAL_FSEL_40MHZ:
  425. val = mt7530_read(priv, MT7531_PLLGP_CR0);
  426. val &= ~RG_COREPLL_SDM_PCW_M;
  427. val |= 0x190000 << RG_COREPLL_SDM_PCW_S;
  428. mt7530_write(priv, MT7531_PLLGP_CR0, val);
  429. break;
  430. }
  431. /* Set feedback divide ratio update signal to high */
  432. val = mt7530_read(priv, MT7531_PLLGP_CR0);
  433. val |= RG_COREPLL_SDM_PCW_CHG;
  434. mt7530_write(priv, MT7531_PLLGP_CR0, val);
  435. /* Wait for at least 16 XTAL clocks */
  436. usleep_range(10, 20);
  437. /* Step 5: set feedback divide ratio update signal to low */
  438. val = mt7530_read(priv, MT7531_PLLGP_CR0);
  439. val &= ~RG_COREPLL_SDM_PCW_CHG;
  440. mt7530_write(priv, MT7531_PLLGP_CR0, val);
  441. /* Enable 325M clock for SGMII */
  442. mt7530_write(priv, MT7531_ANA_PLLGP_CR5, 0xad0000);
  443. /* Enable 250SSC clock for RGMII */
  444. mt7530_write(priv, MT7531_ANA_PLLGP_CR2, 0x4f40000);
  445. /* Step 6: Enable MT7531 PLL */
  446. val = mt7530_read(priv, MT7531_PLLGP_CR0);
  447. val |= RG_COREPLL_EN;
  448. mt7530_write(priv, MT7531_PLLGP_CR0, val);
  449. val = mt7530_read(priv, MT7531_PLLGP_EN);
  450. val |= EN_COREPLL;
  451. mt7530_write(priv, MT7531_PLLGP_EN, val);
  452. usleep_range(25, 35);
  453. }
  454. static void
  455. mt7530_mib_reset(struct dsa_switch *ds)
  456. {
  457. struct mt7530_priv *priv = ds->priv;
  458. mt7530_write(priv, MT7530_MIB_CCR, CCR_MIB_FLUSH);
  459. mt7530_write(priv, MT7530_MIB_CCR, CCR_MIB_ACTIVATE);
  460. }
  461. static int mt7530_phy_read_c22(struct mt7530_priv *priv, int port, int regnum)
  462. {
  463. return mdiobus_read_nested(priv->bus, port, regnum);
  464. }
  465. static int mt7530_phy_write_c22(struct mt7530_priv *priv, int port, int regnum,
  466. u16 val)
  467. {
  468. return mdiobus_write_nested(priv->bus, port, regnum, val);
  469. }
  470. static int mt7530_phy_read_c45(struct mt7530_priv *priv, int port,
  471. int devad, int regnum)
  472. {
  473. return mdiobus_c45_read_nested(priv->bus, port, devad, regnum);
  474. }
  475. static int mt7530_phy_write_c45(struct mt7530_priv *priv, int port, int devad,
  476. int regnum, u16 val)
  477. {
  478. return mdiobus_c45_write_nested(priv->bus, port, devad, regnum, val);
  479. }
  480. static int
  481. mt7531_ind_c45_phy_read(struct mt7530_priv *priv, int port, int devad,
  482. int regnum)
  483. {
  484. struct mt7530_dummy_poll p;
  485. u32 reg, val;
  486. int ret;
  487. INIT_MT7530_DUMMY_POLL(&p, priv, MT7531_PHY_IAC);
  488. mt7530_mutex_lock(priv);
  489. ret = readx_poll_timeout(_mt7530_unlocked_read, &p, val,
  490. !(val & MT7531_PHY_ACS_ST), 20, 100000);
  491. if (ret < 0) {
  492. dev_err(priv->dev, "poll timeout\n");
  493. goto out;
  494. }
  495. reg = MT7531_MDIO_CL45_ADDR | MT7531_MDIO_PHY_ADDR(port) |
  496. MT7531_MDIO_DEV_ADDR(devad) | regnum;
  497. mt7530_mii_write(priv, MT7531_PHY_IAC, reg | MT7531_PHY_ACS_ST);
  498. ret = readx_poll_timeout(_mt7530_unlocked_read, &p, val,
  499. !(val & MT7531_PHY_ACS_ST), 20, 100000);
  500. if (ret < 0) {
  501. dev_err(priv->dev, "poll timeout\n");
  502. goto out;
  503. }
  504. reg = MT7531_MDIO_CL45_READ | MT7531_MDIO_PHY_ADDR(port) |
  505. MT7531_MDIO_DEV_ADDR(devad);
  506. mt7530_mii_write(priv, MT7531_PHY_IAC, reg | MT7531_PHY_ACS_ST);
  507. ret = readx_poll_timeout(_mt7530_unlocked_read, &p, val,
  508. !(val & MT7531_PHY_ACS_ST), 20, 100000);
  509. if (ret < 0) {
  510. dev_err(priv->dev, "poll timeout\n");
  511. goto out;
  512. }
  513. ret = val & MT7531_MDIO_RW_DATA_MASK;
  514. out:
  515. mt7530_mutex_unlock(priv);
  516. return ret;
  517. }
  518. static int
  519. mt7531_ind_c45_phy_write(struct mt7530_priv *priv, int port, int devad,
  520. int regnum, u16 data)
  521. {
  522. struct mt7530_dummy_poll p;
  523. u32 val, reg;
  524. int ret;
  525. INIT_MT7530_DUMMY_POLL(&p, priv, MT7531_PHY_IAC);
  526. mt7530_mutex_lock(priv);
  527. ret = readx_poll_timeout(_mt7530_unlocked_read, &p, val,
  528. !(val & MT7531_PHY_ACS_ST), 20, 100000);
  529. if (ret < 0) {
  530. dev_err(priv->dev, "poll timeout\n");
  531. goto out;
  532. }
  533. reg = MT7531_MDIO_CL45_ADDR | MT7531_MDIO_PHY_ADDR(port) |
  534. MT7531_MDIO_DEV_ADDR(devad) | regnum;
  535. mt7530_mii_write(priv, MT7531_PHY_IAC, reg | MT7531_PHY_ACS_ST);
  536. ret = readx_poll_timeout(_mt7530_unlocked_read, &p, val,
  537. !(val & MT7531_PHY_ACS_ST), 20, 100000);
  538. if (ret < 0) {
  539. dev_err(priv->dev, "poll timeout\n");
  540. goto out;
  541. }
  542. reg = MT7531_MDIO_CL45_WRITE | MT7531_MDIO_PHY_ADDR(port) |
  543. MT7531_MDIO_DEV_ADDR(devad) | data;
  544. mt7530_mii_write(priv, MT7531_PHY_IAC, reg | MT7531_PHY_ACS_ST);
  545. ret = readx_poll_timeout(_mt7530_unlocked_read, &p, val,
  546. !(val & MT7531_PHY_ACS_ST), 20, 100000);
  547. if (ret < 0) {
  548. dev_err(priv->dev, "poll timeout\n");
  549. goto out;
  550. }
  551. out:
  552. mt7530_mutex_unlock(priv);
  553. return ret;
  554. }
  555. static int
  556. mt7531_ind_c22_phy_read(struct mt7530_priv *priv, int port, int regnum)
  557. {
  558. struct mt7530_dummy_poll p;
  559. int ret;
  560. u32 val;
  561. INIT_MT7530_DUMMY_POLL(&p, priv, MT7531_PHY_IAC);
  562. mt7530_mutex_lock(priv);
  563. ret = readx_poll_timeout(_mt7530_unlocked_read, &p, val,
  564. !(val & MT7531_PHY_ACS_ST), 20, 100000);
  565. if (ret < 0) {
  566. dev_err(priv->dev, "poll timeout\n");
  567. goto out;
  568. }
  569. val = MT7531_MDIO_CL22_READ | MT7531_MDIO_PHY_ADDR(port) |
  570. MT7531_MDIO_REG_ADDR(regnum);
  571. mt7530_mii_write(priv, MT7531_PHY_IAC, val | MT7531_PHY_ACS_ST);
  572. ret = readx_poll_timeout(_mt7530_unlocked_read, &p, val,
  573. !(val & MT7531_PHY_ACS_ST), 20, 100000);
  574. if (ret < 0) {
  575. dev_err(priv->dev, "poll timeout\n");
  576. goto out;
  577. }
  578. ret = val & MT7531_MDIO_RW_DATA_MASK;
  579. out:
  580. mt7530_mutex_unlock(priv);
  581. return ret;
  582. }
  583. static int
  584. mt7531_ind_c22_phy_write(struct mt7530_priv *priv, int port, int regnum,
  585. u16 data)
  586. {
  587. struct mt7530_dummy_poll p;
  588. int ret;
  589. u32 reg;
  590. INIT_MT7530_DUMMY_POLL(&p, priv, MT7531_PHY_IAC);
  591. mt7530_mutex_lock(priv);
  592. ret = readx_poll_timeout(_mt7530_unlocked_read, &p, reg,
  593. !(reg & MT7531_PHY_ACS_ST), 20, 100000);
  594. if (ret < 0) {
  595. dev_err(priv->dev, "poll timeout\n");
  596. goto out;
  597. }
  598. reg = MT7531_MDIO_CL22_WRITE | MT7531_MDIO_PHY_ADDR(port) |
  599. MT7531_MDIO_REG_ADDR(regnum) | data;
  600. mt7530_mii_write(priv, MT7531_PHY_IAC, reg | MT7531_PHY_ACS_ST);
  601. ret = readx_poll_timeout(_mt7530_unlocked_read, &p, reg,
  602. !(reg & MT7531_PHY_ACS_ST), 20, 100000);
  603. if (ret < 0) {
  604. dev_err(priv->dev, "poll timeout\n");
  605. goto out;
  606. }
  607. out:
  608. mt7530_mutex_unlock(priv);
  609. return ret;
  610. }
  611. static int
  612. mt753x_phy_read_c22(struct mii_bus *bus, int port, int regnum)
  613. {
  614. struct mt7530_priv *priv = bus->priv;
  615. return priv->info->phy_read_c22(priv, port, regnum);
  616. }
  617. static int
  618. mt753x_phy_read_c45(struct mii_bus *bus, int port, int devad, int regnum)
  619. {
  620. struct mt7530_priv *priv = bus->priv;
  621. return priv->info->phy_read_c45(priv, port, devad, regnum);
  622. }
  623. static int
  624. mt753x_phy_write_c22(struct mii_bus *bus, int port, int regnum, u16 val)
  625. {
  626. struct mt7530_priv *priv = bus->priv;
  627. return priv->info->phy_write_c22(priv, port, regnum, val);
  628. }
  629. static int
  630. mt753x_phy_write_c45(struct mii_bus *bus, int port, int devad, int regnum,
  631. u16 val)
  632. {
  633. struct mt7530_priv *priv = bus->priv;
  634. return priv->info->phy_write_c45(priv, port, devad, regnum, val);
  635. }
  636. static void
  637. mt7530_get_strings(struct dsa_switch *ds, int port, u32 stringset,
  638. uint8_t *data)
  639. {
  640. int i;
  641. if (stringset != ETH_SS_STATS)
  642. return;
  643. for (i = 0; i < ARRAY_SIZE(mt7530_mib); i++)
  644. ethtool_puts(&data, mt7530_mib[i].name);
  645. }
  646. static void
  647. mt7530_get_ethtool_stats(struct dsa_switch *ds, int port,
  648. uint64_t *data)
  649. {
  650. struct mt7530_priv *priv = ds->priv;
  651. const struct mt7530_mib_desc *mib;
  652. u32 reg, i;
  653. u64 hi;
  654. for (i = 0; i < ARRAY_SIZE(mt7530_mib); i++) {
  655. mib = &mt7530_mib[i];
  656. reg = MT7530_PORT_MIB_COUNTER(port) + mib->offset;
  657. data[i] = mt7530_read(priv, reg);
  658. if (mib->size == 2) {
  659. hi = mt7530_read(priv, reg + 4);
  660. data[i] |= hi << 32;
  661. }
  662. }
  663. }
  664. static int
  665. mt7530_get_sset_count(struct dsa_switch *ds, int port, int sset)
  666. {
  667. if (sset != ETH_SS_STATS)
  668. return 0;
  669. return ARRAY_SIZE(mt7530_mib);
  670. }
  671. static int
  672. mt7530_set_ageing_time(struct dsa_switch *ds, unsigned int msecs)
  673. {
  674. struct mt7530_priv *priv = ds->priv;
  675. unsigned int secs = msecs / 1000;
  676. unsigned int tmp_age_count;
  677. unsigned int error = -1;
  678. unsigned int age_count;
  679. unsigned int age_unit;
  680. /* Applied timer is (AGE_CNT + 1) * (AGE_UNIT + 1) seconds */
  681. if (secs < 1 || secs > (AGE_CNT_MAX + 1) * (AGE_UNIT_MAX + 1))
  682. return -ERANGE;
  683. /* iterate through all possible age_count to find the closest pair */
  684. for (tmp_age_count = 0; tmp_age_count <= AGE_CNT_MAX; ++tmp_age_count) {
  685. unsigned int tmp_age_unit = secs / (tmp_age_count + 1) - 1;
  686. if (tmp_age_unit <= AGE_UNIT_MAX) {
  687. unsigned int tmp_error = secs -
  688. (tmp_age_count + 1) * (tmp_age_unit + 1);
  689. /* found a closer pair */
  690. if (error > tmp_error) {
  691. error = tmp_error;
  692. age_count = tmp_age_count;
  693. age_unit = tmp_age_unit;
  694. }
  695. /* found the exact match, so break the loop */
  696. if (!error)
  697. break;
  698. }
  699. }
  700. mt7530_write(priv, MT7530_AAC, AGE_CNT(age_count) | AGE_UNIT(age_unit));
  701. return 0;
  702. }
  703. static const char *mt7530_p5_mode_str(unsigned int mode)
  704. {
  705. switch (mode) {
  706. case MUX_PHY_P0:
  707. return "MUX PHY P0";
  708. case MUX_PHY_P4:
  709. return "MUX PHY P4";
  710. default:
  711. return "GMAC5";
  712. }
  713. }
  714. static void mt7530_setup_port5(struct dsa_switch *ds, phy_interface_t interface)
  715. {
  716. struct mt7530_priv *priv = ds->priv;
  717. u8 tx_delay = 0;
  718. int val;
  719. mutex_lock(&priv->reg_mutex);
  720. val = mt7530_read(priv, MT753X_MTRAP);
  721. val &= ~MT7530_P5_PHY0_SEL & ~MT7530_P5_MAC_SEL & ~MT7530_P5_RGMII_MODE;
  722. switch (priv->p5_mode) {
  723. /* MUX_PHY_P0: P0 -> P5 -> SoC MAC */
  724. case MUX_PHY_P0:
  725. val |= MT7530_P5_PHY0_SEL;
  726. fallthrough;
  727. /* MUX_PHY_P4: P4 -> P5 -> SoC MAC */
  728. case MUX_PHY_P4:
  729. /* Setup the MAC by default for the cpu port */
  730. mt7530_write(priv, MT753X_PMCR_P(5), 0x56300);
  731. break;
  732. /* GMAC5: P5 -> SoC MAC or external PHY */
  733. default:
  734. val |= MT7530_P5_MAC_SEL;
  735. break;
  736. }
  737. /* Setup RGMII settings */
  738. if (phy_interface_mode_is_rgmii(interface)) {
  739. val |= MT7530_P5_RGMII_MODE;
  740. /* P5 RGMII RX Clock Control: delay setting for 1000M */
  741. mt7530_write(priv, MT7530_P5RGMIIRXCR, CSR_RGMII_EDGE_ALIGN);
  742. /* Don't set delay in DSA mode */
  743. if (!dsa_is_dsa_port(priv->ds, 5) &&
  744. (interface == PHY_INTERFACE_MODE_RGMII_TXID ||
  745. interface == PHY_INTERFACE_MODE_RGMII_ID))
  746. tx_delay = 4; /* n * 0.5 ns */
  747. /* P5 RGMII TX Clock Control: delay x */
  748. mt7530_write(priv, MT7530_P5RGMIITXCR,
  749. CSR_RGMII_TXC_CFG(0x10 + tx_delay));
  750. /* reduce P5 RGMII Tx driving, 8mA */
  751. mt7530_write(priv, MT7530_IO_DRV_CR,
  752. P5_IO_CLK_DRV(1) | P5_IO_DATA_DRV(1));
  753. }
  754. mt7530_write(priv, MT753X_MTRAP, val);
  755. dev_dbg(ds->dev, "Setup P5, HWTRAP=0x%x, mode=%s, phy-mode=%s\n", val,
  756. mt7530_p5_mode_str(priv->p5_mode), phy_modes(interface));
  757. mutex_unlock(&priv->reg_mutex);
  758. }
  759. /* In Clause 5 of IEEE Std 802-2014, two sublayers of the data link layer (DLL)
  760. * of the Open Systems Interconnection basic reference model (OSI/RM) are
  761. * described; the medium access control (MAC) and logical link control (LLC)
  762. * sublayers. The MAC sublayer is the one facing the physical layer.
  763. *
  764. * In 8.2 of IEEE Std 802.1Q-2022, the Bridge architecture is described. A
  765. * Bridge component comprises a MAC Relay Entity for interconnecting the Ports
  766. * of the Bridge, at least two Ports, and higher layer entities with at least a
  767. * Spanning Tree Protocol Entity included.
  768. *
  769. * Each Bridge Port also functions as an end station and shall provide the MAC
  770. * Service to an LLC Entity. Each instance of the MAC Service is provided to a
  771. * distinct LLC Entity that supports protocol identification, multiplexing, and
  772. * demultiplexing, for protocol data unit (PDU) transmission and reception by
  773. * one or more higher layer entities.
  774. *
  775. * It is described in 8.13.9 of IEEE Std 802.1Q-2022 that in a Bridge, the LLC
  776. * Entity associated with each Bridge Port is modeled as being directly
  777. * connected to the attached Local Area Network (LAN).
  778. *
  779. * On the switch with CPU port architecture, CPU port functions as Management
  780. * Port, and the Management Port functionality is provided by software which
  781. * functions as an end station. Software is connected to an IEEE 802 LAN that is
  782. * wholly contained within the system that incorporates the Bridge. Software
  783. * provides access to the LLC Entity associated with each Bridge Port by the
  784. * value of the source port field on the special tag on the frame received by
  785. * software.
  786. *
  787. * We call frames that carry control information to determine the active
  788. * topology and current extent of each Virtual Local Area Network (VLAN), i.e.,
  789. * spanning tree or Shortest Path Bridging (SPB) and Multiple VLAN Registration
  790. * Protocol Data Units (MVRPDUs), and frames from other link constrained
  791. * protocols, such as Extensible Authentication Protocol over LAN (EAPOL) and
  792. * Link Layer Discovery Protocol (LLDP), link-local frames. They are not
  793. * forwarded by a Bridge. Permanently configured entries in the filtering
  794. * database (FDB) ensure that such frames are discarded by the Forwarding
  795. * Process. In 8.6.3 of IEEE Std 802.1Q-2022, this is described in detail:
  796. *
  797. * Each of the reserved MAC addresses specified in Table 8-1
  798. * (01-80-C2-00-00-[00,01,02,03,04,05,06,07,08,09,0A,0B,0C,0D,0E,0F]) shall be
  799. * permanently configured in the FDB in C-VLAN components and ERs.
  800. *
  801. * Each of the reserved MAC addresses specified in Table 8-2
  802. * (01-80-C2-00-00-[01,02,03,04,05,06,07,08,09,0A,0E]) shall be permanently
  803. * configured in the FDB in S-VLAN components.
  804. *
  805. * Each of the reserved MAC addresses specified in Table 8-3
  806. * (01-80-C2-00-00-[01,02,04,0E]) shall be permanently configured in the FDB in
  807. * TPMR components.
  808. *
  809. * The FDB entries for reserved MAC addresses shall specify filtering for all
  810. * Bridge Ports and all VIDs. Management shall not provide the capability to
  811. * modify or remove entries for reserved MAC addresses.
  812. *
  813. * The addresses in Table 8-1, Table 8-2, and Table 8-3 determine the scope of
  814. * propagation of PDUs within a Bridged Network, as follows:
  815. *
  816. * The Nearest Bridge group address (01-80-C2-00-00-0E) is an address that no
  817. * conformant Two-Port MAC Relay (TPMR) component, Service VLAN (S-VLAN)
  818. * component, Customer VLAN (C-VLAN) component, or MAC Bridge can forward.
  819. * PDUs transmitted using this destination address, or any other addresses
  820. * that appear in Table 8-1, Table 8-2, and Table 8-3
  821. * (01-80-C2-00-00-[00,01,02,03,04,05,06,07,08,09,0A,0B,0C,0D,0E,0F]), can
  822. * therefore travel no further than those stations that can be reached via a
  823. * single individual LAN from the originating station.
  824. *
  825. * The Nearest non-TPMR Bridge group address (01-80-C2-00-00-03), is an
  826. * address that no conformant S-VLAN component, C-VLAN component, or MAC
  827. * Bridge can forward; however, this address is relayed by a TPMR component.
  828. * PDUs using this destination address, or any of the other addresses that
  829. * appear in both Table 8-1 and Table 8-2 but not in Table 8-3
  830. * (01-80-C2-00-00-[00,03,05,06,07,08,09,0A,0B,0C,0D,0F]), will be relayed by
  831. * any TPMRs but will propagate no further than the nearest S-VLAN component,
  832. * C-VLAN component, or MAC Bridge.
  833. *
  834. * The Nearest Customer Bridge group address (01-80-C2-00-00-00) is an address
  835. * that no conformant C-VLAN component, MAC Bridge can forward; however, it is
  836. * relayed by TPMR components and S-VLAN components. PDUs using this
  837. * destination address, or any of the other addresses that appear in Table 8-1
  838. * but not in either Table 8-2 or Table 8-3 (01-80-C2-00-00-[00,0B,0C,0D,0F]),
  839. * will be relayed by TPMR components and S-VLAN components but will propagate
  840. * no further than the nearest C-VLAN component or MAC Bridge.
  841. *
  842. * Because the LLC Entity associated with each Bridge Port is provided via CPU
  843. * port, we must not filter these frames but forward them to CPU port.
  844. *
  845. * In a Bridge, the transmission Port is majorly decided by ingress and egress
  846. * rules, FDB, and spanning tree Port State functions of the Forwarding Process.
  847. * For link-local frames, only CPU port should be designated as destination port
  848. * in the FDB, and the other functions of the Forwarding Process must not
  849. * interfere with the decision of the transmission Port. We call this process
  850. * trapping frames to CPU port.
  851. *
  852. * Therefore, on the switch with CPU port architecture, link-local frames must
  853. * be trapped to CPU port, and certain link-local frames received by a Port of a
  854. * Bridge comprising a TPMR component or an S-VLAN component must be excluded
  855. * from it.
  856. *
  857. * A Bridge of the switch with CPU port architecture cannot comprise a Two-Port
  858. * MAC Relay (TPMR) component as a TPMR component supports only a subset of the
  859. * functionality of a MAC Bridge. A Bridge comprising two Ports (Management Port
  860. * doesn't count) of this architecture will either function as a standard MAC
  861. * Bridge or a standard VLAN Bridge.
  862. *
  863. * Therefore, a Bridge of this architecture can only comprise S-VLAN components,
  864. * C-VLAN components, or MAC Bridge components. Since there's no TPMR component,
  865. * we don't need to relay PDUs using the destination addresses specified on the
  866. * Nearest non-TPMR section, and the proportion of the Nearest Customer Bridge
  867. * section where they must be relayed by TPMR components.
  868. *
  869. * One option to trap link-local frames to CPU port is to add static FDB entries
  870. * with CPU port designated as destination port. However, because that
  871. * Independent VLAN Learning (IVL) is being used on every VID, each entry only
  872. * applies to a single VLAN Identifier (VID). For a Bridge comprising a MAC
  873. * Bridge component or a C-VLAN component, there would have to be 16 times 4096
  874. * entries. This switch intellectual property can only hold a maximum of 2048
  875. * entries. Using this option, there also isn't a mechanism to prevent
  876. * link-local frames from being discarded when the spanning tree Port State of
  877. * the reception Port is discarding.
  878. *
  879. * The remaining option is to utilise the BPC, RGAC1, RGAC2, RGAC3, and RGAC4
  880. * registers. Whilst this applies to every VID, it doesn't contain all of the
  881. * reserved MAC addresses without affecting the remaining Standard Group MAC
  882. * Addresses. The REV_UN frame tag utilised using the RGAC4 register covers the
  883. * remaining 01-80-C2-00-00-[04,05,06,07,08,09,0A,0B,0C,0D,0F] destination
  884. * addresses. It also includes the 01-80-C2-00-00-22 to 01-80-C2-00-00-FF
  885. * destination addresses which may be relayed by MAC Bridges or VLAN Bridges.
  886. * The latter option provides better but not complete conformance.
  887. *
  888. * This switch intellectual property also does not provide a mechanism to trap
  889. * link-local frames with specific destination addresses to CPU port by Bridge,
  890. * to conform to the filtering rules for the distinct Bridge components.
  891. *
  892. * Therefore, regardless of the type of the Bridge component, link-local frames
  893. * with these destination addresses will be trapped to CPU port:
  894. *
  895. * 01-80-C2-00-00-[00,01,02,03,0E]
  896. *
  897. * In a Bridge comprising a MAC Bridge component or a C-VLAN component:
  898. *
  899. * Link-local frames with these destination addresses won't be trapped to CPU
  900. * port which won't conform to IEEE Std 802.1Q-2022:
  901. *
  902. * 01-80-C2-00-00-[04,05,06,07,08,09,0A,0B,0C,0D,0F]
  903. *
  904. * In a Bridge comprising an S-VLAN component:
  905. *
  906. * Link-local frames with these destination addresses will be trapped to CPU
  907. * port which won't conform to IEEE Std 802.1Q-2022:
  908. *
  909. * 01-80-C2-00-00-00
  910. *
  911. * Link-local frames with these destination addresses won't be trapped to CPU
  912. * port which won't conform to IEEE Std 802.1Q-2022:
  913. *
  914. * 01-80-C2-00-00-[04,05,06,07,08,09,0A]
  915. *
  916. * To trap link-local frames to CPU port as conformant as this switch
  917. * intellectual property can allow, link-local frames are made to be regarded as
  918. * Bridge Protocol Data Units (BPDUs). This is because this switch intellectual
  919. * property only lets the frames regarded as BPDUs bypass the spanning tree Port
  920. * State function of the Forwarding Process.
  921. *
  922. * The only remaining interference is the ingress rules. When the reception Port
  923. * has no PVID assigned on software, VLAN-untagged frames won't be allowed in.
  924. * There doesn't seem to be a mechanism on the switch intellectual property to
  925. * have link-local frames bypass this function of the Forwarding Process.
  926. */
  927. static void
  928. mt753x_trap_frames(struct mt7530_priv *priv)
  929. {
  930. /* Trap 802.1X PAE frames and BPDUs to the CPU port(s) and egress them
  931. * VLAN-untagged.
  932. */
  933. mt7530_rmw(priv, MT753X_BPC,
  934. PAE_BPDU_FR | PAE_EG_TAG_MASK | PAE_PORT_FW_MASK |
  935. BPDU_EG_TAG_MASK | BPDU_PORT_FW_MASK,
  936. PAE_BPDU_FR | PAE_EG_TAG(MT7530_VLAN_EG_UNTAGGED) |
  937. PAE_PORT_FW(TO_CPU_FW_CPU_ONLY) |
  938. BPDU_EG_TAG(MT7530_VLAN_EG_UNTAGGED) |
  939. TO_CPU_FW_CPU_ONLY);
  940. /* Trap frames with :01 and :02 MAC DAs to the CPU port(s) and egress
  941. * them VLAN-untagged.
  942. */
  943. mt7530_rmw(priv, MT753X_RGAC1,
  944. R02_BPDU_FR | R02_EG_TAG_MASK | R02_PORT_FW_MASK |
  945. R01_BPDU_FR | R01_EG_TAG_MASK | R01_PORT_FW_MASK,
  946. R02_BPDU_FR | R02_EG_TAG(MT7530_VLAN_EG_UNTAGGED) |
  947. R02_PORT_FW(TO_CPU_FW_CPU_ONLY) | R01_BPDU_FR |
  948. R01_EG_TAG(MT7530_VLAN_EG_UNTAGGED) |
  949. TO_CPU_FW_CPU_ONLY);
  950. /* Trap frames with :03 and :0E MAC DAs to the CPU port(s) and egress
  951. * them VLAN-untagged.
  952. */
  953. mt7530_rmw(priv, MT753X_RGAC2,
  954. R0E_BPDU_FR | R0E_EG_TAG_MASK | R0E_PORT_FW_MASK |
  955. R03_BPDU_FR | R03_EG_TAG_MASK | R03_PORT_FW_MASK,
  956. R0E_BPDU_FR | R0E_EG_TAG(MT7530_VLAN_EG_UNTAGGED) |
  957. R0E_PORT_FW(TO_CPU_FW_CPU_ONLY) | R03_BPDU_FR |
  958. R03_EG_TAG(MT7530_VLAN_EG_UNTAGGED) |
  959. TO_CPU_FW_CPU_ONLY);
  960. }
  961. static void
  962. mt753x_cpu_port_enable(struct dsa_switch *ds, int port)
  963. {
  964. struct mt7530_priv *priv = ds->priv;
  965. /* Enable Mediatek header mode on the cpu port */
  966. mt7530_write(priv, MT7530_PVC_P(port),
  967. PORT_SPEC_TAG);
  968. /* Enable flooding on the CPU port */
  969. mt7530_set(priv, MT753X_MFC, BC_FFP(BIT(port)) | UNM_FFP(BIT(port)) |
  970. UNU_FFP(BIT(port)));
  971. /* Add the CPU port to the CPU port bitmap for MT7531 and the switch on
  972. * the MT7988 SoC. Trapped frames will be forwarded to the CPU port that
  973. * is affine to the inbound user port.
  974. */
  975. if (priv->id == ID_MT7531 || priv->id == ID_MT7988 ||
  976. priv->id == ID_EN7581)
  977. mt7530_set(priv, MT7531_CFC, MT7531_CPU_PMAP(BIT(port)));
  978. /* CPU port gets connected to all user ports of
  979. * the switch.
  980. */
  981. mt7530_write(priv, MT7530_PCR_P(port),
  982. PCR_MATRIX(dsa_user_ports(priv->ds)));
  983. /* Set to fallback mode for independent VLAN learning */
  984. mt7530_rmw(priv, MT7530_PCR_P(port), PCR_PORT_VLAN_MASK,
  985. MT7530_PORT_FALLBACK_MODE);
  986. }
  987. static int
  988. mt7530_port_enable(struct dsa_switch *ds, int port,
  989. struct phy_device *phy)
  990. {
  991. struct dsa_port *dp = dsa_to_port(ds, port);
  992. struct mt7530_priv *priv = ds->priv;
  993. mutex_lock(&priv->reg_mutex);
  994. /* Allow the user port gets connected to the cpu port and also
  995. * restore the port matrix if the port is the member of a certain
  996. * bridge.
  997. */
  998. if (dsa_port_is_user(dp)) {
  999. struct dsa_port *cpu_dp = dp->cpu_dp;
  1000. priv->ports[port].pm |= PCR_MATRIX(BIT(cpu_dp->index));
  1001. }
  1002. priv->ports[port].enable = true;
  1003. mt7530_rmw(priv, MT7530_PCR_P(port), PCR_MATRIX_MASK,
  1004. priv->ports[port].pm);
  1005. mutex_unlock(&priv->reg_mutex);
  1006. if (priv->id != ID_MT7530 && priv->id != ID_MT7621)
  1007. return 0;
  1008. if (port == 5)
  1009. mt7530_clear(priv, MT753X_MTRAP, MT7530_P5_DIS);
  1010. else if (port == 6)
  1011. mt7530_clear(priv, MT753X_MTRAP, MT7530_P6_DIS);
  1012. return 0;
  1013. }
  1014. static void
  1015. mt7530_port_disable(struct dsa_switch *ds, int port)
  1016. {
  1017. struct mt7530_priv *priv = ds->priv;
  1018. mutex_lock(&priv->reg_mutex);
  1019. /* Clear up all port matrix which could be restored in the next
  1020. * enablement for the port.
  1021. */
  1022. priv->ports[port].enable = false;
  1023. mt7530_rmw(priv, MT7530_PCR_P(port), PCR_MATRIX_MASK,
  1024. PCR_MATRIX_CLR);
  1025. mutex_unlock(&priv->reg_mutex);
  1026. if (priv->id != ID_MT7530 && priv->id != ID_MT7621)
  1027. return;
  1028. /* Do not set MT7530_P5_DIS when port 5 is being used for PHY muxing. */
  1029. if (port == 5 && priv->p5_mode == GMAC5)
  1030. mt7530_set(priv, MT753X_MTRAP, MT7530_P5_DIS);
  1031. else if (port == 6)
  1032. mt7530_set(priv, MT753X_MTRAP, MT7530_P6_DIS);
  1033. }
  1034. static int
  1035. mt7530_port_change_mtu(struct dsa_switch *ds, int port, int new_mtu)
  1036. {
  1037. struct mt7530_priv *priv = ds->priv;
  1038. int length;
  1039. u32 val;
  1040. /* When a new MTU is set, DSA always set the CPU port's MTU to the
  1041. * largest MTU of the user ports. Because the switch only has a global
  1042. * RX length register, only allowing CPU port here is enough.
  1043. */
  1044. if (!dsa_is_cpu_port(ds, port))
  1045. return 0;
  1046. mt7530_mutex_lock(priv);
  1047. val = mt7530_mii_read(priv, MT7530_GMACCR);
  1048. val &= ~MAX_RX_PKT_LEN_MASK;
  1049. /* RX length also includes Ethernet header, MTK tag, and FCS length */
  1050. length = new_mtu + ETH_HLEN + MTK_HDR_LEN + ETH_FCS_LEN;
  1051. if (length <= 1522) {
  1052. val |= MAX_RX_PKT_LEN_1522;
  1053. } else if (length <= 1536) {
  1054. val |= MAX_RX_PKT_LEN_1536;
  1055. } else if (length <= 1552) {
  1056. val |= MAX_RX_PKT_LEN_1552;
  1057. } else {
  1058. val &= ~MAX_RX_JUMBO_MASK;
  1059. val |= MAX_RX_JUMBO(DIV_ROUND_UP(length, 1024));
  1060. val |= MAX_RX_PKT_LEN_JUMBO;
  1061. }
  1062. mt7530_mii_write(priv, MT7530_GMACCR, val);
  1063. mt7530_mutex_unlock(priv);
  1064. return 0;
  1065. }
  1066. static int
  1067. mt7530_port_max_mtu(struct dsa_switch *ds, int port)
  1068. {
  1069. return MT7530_MAX_MTU;
  1070. }
  1071. static void
  1072. mt7530_stp_state_set(struct dsa_switch *ds, int port, u8 state)
  1073. {
  1074. struct mt7530_priv *priv = ds->priv;
  1075. u32 stp_state;
  1076. switch (state) {
  1077. case BR_STATE_DISABLED:
  1078. stp_state = MT7530_STP_DISABLED;
  1079. break;
  1080. case BR_STATE_BLOCKING:
  1081. stp_state = MT7530_STP_BLOCKING;
  1082. break;
  1083. case BR_STATE_LISTENING:
  1084. stp_state = MT7530_STP_LISTENING;
  1085. break;
  1086. case BR_STATE_LEARNING:
  1087. stp_state = MT7530_STP_LEARNING;
  1088. break;
  1089. case BR_STATE_FORWARDING:
  1090. default:
  1091. stp_state = MT7530_STP_FORWARDING;
  1092. break;
  1093. }
  1094. mt7530_rmw(priv, MT7530_SSP_P(port), FID_PST_MASK(FID_BRIDGED),
  1095. FID_PST(FID_BRIDGED, stp_state));
  1096. }
  1097. static void mt7530_update_port_member(struct mt7530_priv *priv, int port,
  1098. const struct net_device *bridge_dev,
  1099. bool join) __must_hold(&priv->reg_mutex)
  1100. {
  1101. struct dsa_port *dp = dsa_to_port(priv->ds, port), *other_dp;
  1102. struct mt7530_port *p = &priv->ports[port], *other_p;
  1103. struct dsa_port *cpu_dp = dp->cpu_dp;
  1104. u32 port_bitmap = BIT(cpu_dp->index);
  1105. int other_port;
  1106. bool isolated;
  1107. dsa_switch_for_each_user_port(other_dp, priv->ds) {
  1108. other_port = other_dp->index;
  1109. other_p = &priv->ports[other_port];
  1110. if (dp == other_dp)
  1111. continue;
  1112. /* Add/remove this port to/from the port matrix of the other
  1113. * ports in the same bridge. If the port is disabled, port
  1114. * matrix is kept and not being setup until the port becomes
  1115. * enabled.
  1116. */
  1117. if (!dsa_port_offloads_bridge_dev(other_dp, bridge_dev))
  1118. continue;
  1119. isolated = p->isolated && other_p->isolated;
  1120. if (join && !isolated) {
  1121. other_p->pm |= PCR_MATRIX(BIT(port));
  1122. port_bitmap |= BIT(other_port);
  1123. } else {
  1124. other_p->pm &= ~PCR_MATRIX(BIT(port));
  1125. }
  1126. if (other_p->enable)
  1127. mt7530_rmw(priv, MT7530_PCR_P(other_port),
  1128. PCR_MATRIX_MASK, other_p->pm);
  1129. }
  1130. /* Add/remove the all other ports to this port matrix. For !join
  1131. * (leaving the bridge), only the CPU port will remain in the port matrix
  1132. * of this port.
  1133. */
  1134. p->pm = PCR_MATRIX(port_bitmap);
  1135. if (priv->ports[port].enable)
  1136. mt7530_rmw(priv, MT7530_PCR_P(port), PCR_MATRIX_MASK, p->pm);
  1137. }
  1138. static int
  1139. mt7530_port_pre_bridge_flags(struct dsa_switch *ds, int port,
  1140. struct switchdev_brport_flags flags,
  1141. struct netlink_ext_ack *extack)
  1142. {
  1143. if (flags.mask & ~(BR_LEARNING | BR_FLOOD | BR_MCAST_FLOOD |
  1144. BR_BCAST_FLOOD | BR_ISOLATED))
  1145. return -EINVAL;
  1146. return 0;
  1147. }
  1148. static int
  1149. mt7530_port_bridge_flags(struct dsa_switch *ds, int port,
  1150. struct switchdev_brport_flags flags,
  1151. struct netlink_ext_ack *extack)
  1152. {
  1153. struct mt7530_priv *priv = ds->priv;
  1154. if (flags.mask & BR_LEARNING)
  1155. mt7530_rmw(priv, MT7530_PSC_P(port), SA_DIS,
  1156. flags.val & BR_LEARNING ? 0 : SA_DIS);
  1157. if (flags.mask & BR_FLOOD)
  1158. mt7530_rmw(priv, MT753X_MFC, UNU_FFP(BIT(port)),
  1159. flags.val & BR_FLOOD ? UNU_FFP(BIT(port)) : 0);
  1160. if (flags.mask & BR_MCAST_FLOOD)
  1161. mt7530_rmw(priv, MT753X_MFC, UNM_FFP(BIT(port)),
  1162. flags.val & BR_MCAST_FLOOD ? UNM_FFP(BIT(port)) : 0);
  1163. if (flags.mask & BR_BCAST_FLOOD)
  1164. mt7530_rmw(priv, MT753X_MFC, BC_FFP(BIT(port)),
  1165. flags.val & BR_BCAST_FLOOD ? BC_FFP(BIT(port)) : 0);
  1166. if (flags.mask & BR_ISOLATED) {
  1167. struct dsa_port *dp = dsa_to_port(ds, port);
  1168. struct net_device *bridge_dev = dsa_port_bridge_dev_get(dp);
  1169. priv->ports[port].isolated = !!(flags.val & BR_ISOLATED);
  1170. mutex_lock(&priv->reg_mutex);
  1171. mt7530_update_port_member(priv, port, bridge_dev, true);
  1172. mutex_unlock(&priv->reg_mutex);
  1173. }
  1174. return 0;
  1175. }
  1176. static int
  1177. mt7530_port_bridge_join(struct dsa_switch *ds, int port,
  1178. struct dsa_bridge bridge, bool *tx_fwd_offload,
  1179. struct netlink_ext_ack *extack)
  1180. {
  1181. struct mt7530_priv *priv = ds->priv;
  1182. mutex_lock(&priv->reg_mutex);
  1183. mt7530_update_port_member(priv, port, bridge.dev, true);
  1184. /* Set to fallback mode for independent VLAN learning */
  1185. mt7530_rmw(priv, MT7530_PCR_P(port), PCR_PORT_VLAN_MASK,
  1186. MT7530_PORT_FALLBACK_MODE);
  1187. mutex_unlock(&priv->reg_mutex);
  1188. return 0;
  1189. }
  1190. static void
  1191. mt7530_port_set_vlan_unaware(struct dsa_switch *ds, int port)
  1192. {
  1193. struct mt7530_priv *priv = ds->priv;
  1194. bool all_user_ports_removed = true;
  1195. int i;
  1196. /* This is called after .port_bridge_leave when leaving a VLAN-aware
  1197. * bridge. Don't set standalone ports to fallback mode.
  1198. */
  1199. if (dsa_port_bridge_dev_get(dsa_to_port(ds, port)))
  1200. mt7530_rmw(priv, MT7530_PCR_P(port), PCR_PORT_VLAN_MASK,
  1201. MT7530_PORT_FALLBACK_MODE);
  1202. mt7530_rmw(priv, MT7530_PVC_P(port),
  1203. VLAN_ATTR_MASK | PVC_EG_TAG_MASK | ACC_FRM_MASK,
  1204. VLAN_ATTR(MT7530_VLAN_TRANSPARENT) |
  1205. PVC_EG_TAG(MT7530_VLAN_EG_CONSISTENT) |
  1206. MT7530_VLAN_ACC_ALL);
  1207. /* Set PVID to 0 */
  1208. mt7530_rmw(priv, MT7530_PPBV1_P(port), G0_PORT_VID_MASK,
  1209. G0_PORT_VID_DEF);
  1210. for (i = 0; i < priv->ds->num_ports; i++) {
  1211. if (dsa_is_user_port(ds, i) &&
  1212. dsa_port_is_vlan_filtering(dsa_to_port(ds, i))) {
  1213. all_user_ports_removed = false;
  1214. break;
  1215. }
  1216. }
  1217. /* CPU port also does the same thing until all user ports belonging to
  1218. * the CPU port get out of VLAN filtering mode.
  1219. */
  1220. if (all_user_ports_removed) {
  1221. struct dsa_port *dp = dsa_to_port(ds, port);
  1222. struct dsa_port *cpu_dp = dp->cpu_dp;
  1223. mt7530_write(priv, MT7530_PCR_P(cpu_dp->index),
  1224. PCR_MATRIX(dsa_user_ports(priv->ds)));
  1225. mt7530_write(priv, MT7530_PVC_P(cpu_dp->index), PORT_SPEC_TAG
  1226. | PVC_EG_TAG(MT7530_VLAN_EG_CONSISTENT));
  1227. }
  1228. }
  1229. static void
  1230. mt7530_port_set_vlan_aware(struct dsa_switch *ds, int port)
  1231. {
  1232. struct mt7530_priv *priv = ds->priv;
  1233. /* Trapped into security mode allows packet forwarding through VLAN
  1234. * table lookup.
  1235. */
  1236. if (dsa_is_user_port(ds, port)) {
  1237. mt7530_rmw(priv, MT7530_PCR_P(port), PCR_PORT_VLAN_MASK,
  1238. MT7530_PORT_SECURITY_MODE);
  1239. mt7530_rmw(priv, MT7530_PPBV1_P(port), G0_PORT_VID_MASK,
  1240. G0_PORT_VID(priv->ports[port].pvid));
  1241. /* Only accept tagged frames if PVID is not set */
  1242. if (!priv->ports[port].pvid)
  1243. mt7530_rmw(priv, MT7530_PVC_P(port), ACC_FRM_MASK,
  1244. MT7530_VLAN_ACC_TAGGED);
  1245. /* Set the port as a user port which is to be able to recognize
  1246. * VID from incoming packets before fetching entry within the
  1247. * VLAN table.
  1248. */
  1249. mt7530_rmw(priv, MT7530_PVC_P(port),
  1250. VLAN_ATTR_MASK | PVC_EG_TAG_MASK,
  1251. VLAN_ATTR(MT7530_VLAN_USER) |
  1252. PVC_EG_TAG(MT7530_VLAN_EG_DISABLED));
  1253. } else {
  1254. /* Also set CPU ports to the "user" VLAN port attribute, to
  1255. * allow VLAN classification, but keep the EG_TAG attribute as
  1256. * "consistent" (i.o.w. don't change its value) for packets
  1257. * received by the switch from the CPU, so that tagged packets
  1258. * are forwarded to user ports as tagged, and untagged as
  1259. * untagged.
  1260. */
  1261. mt7530_rmw(priv, MT7530_PVC_P(port), VLAN_ATTR_MASK,
  1262. VLAN_ATTR(MT7530_VLAN_USER));
  1263. }
  1264. }
  1265. static void
  1266. mt7530_port_bridge_leave(struct dsa_switch *ds, int port,
  1267. struct dsa_bridge bridge)
  1268. {
  1269. struct mt7530_priv *priv = ds->priv;
  1270. mutex_lock(&priv->reg_mutex);
  1271. mt7530_update_port_member(priv, port, bridge.dev, false);
  1272. /* When a port is removed from the bridge, the port would be set up
  1273. * back to the default as is at initial boot which is a VLAN-unaware
  1274. * port.
  1275. */
  1276. mt7530_rmw(priv, MT7530_PCR_P(port), PCR_PORT_VLAN_MASK,
  1277. MT7530_PORT_MATRIX_MODE);
  1278. mutex_unlock(&priv->reg_mutex);
  1279. }
  1280. static int
  1281. mt7530_port_fdb_add(struct dsa_switch *ds, int port,
  1282. const unsigned char *addr, u16 vid,
  1283. struct dsa_db db)
  1284. {
  1285. struct mt7530_priv *priv = ds->priv;
  1286. int ret;
  1287. u8 port_mask = BIT(port);
  1288. mutex_lock(&priv->reg_mutex);
  1289. mt7530_fdb_write(priv, vid, port_mask, addr, -1, STATIC_ENT);
  1290. ret = mt7530_fdb_cmd(priv, MT7530_FDB_WRITE, NULL);
  1291. mutex_unlock(&priv->reg_mutex);
  1292. return ret;
  1293. }
  1294. static int
  1295. mt7530_port_fdb_del(struct dsa_switch *ds, int port,
  1296. const unsigned char *addr, u16 vid,
  1297. struct dsa_db db)
  1298. {
  1299. struct mt7530_priv *priv = ds->priv;
  1300. int ret;
  1301. u8 port_mask = BIT(port);
  1302. mutex_lock(&priv->reg_mutex);
  1303. mt7530_fdb_write(priv, vid, port_mask, addr, -1, STATIC_EMP);
  1304. ret = mt7530_fdb_cmd(priv, MT7530_FDB_WRITE, NULL);
  1305. mutex_unlock(&priv->reg_mutex);
  1306. return ret;
  1307. }
  1308. static int
  1309. mt7530_port_fdb_dump(struct dsa_switch *ds, int port,
  1310. dsa_fdb_dump_cb_t *cb, void *data)
  1311. {
  1312. struct mt7530_priv *priv = ds->priv;
  1313. struct mt7530_fdb _fdb = { 0 };
  1314. int cnt = MT7530_NUM_FDB_RECORDS;
  1315. int ret = 0;
  1316. u32 rsp = 0;
  1317. mutex_lock(&priv->reg_mutex);
  1318. ret = mt7530_fdb_cmd(priv, MT7530_FDB_START, &rsp);
  1319. if (ret < 0)
  1320. goto err;
  1321. do {
  1322. if (rsp & ATC_SRCH_HIT) {
  1323. mt7530_fdb_read(priv, &_fdb);
  1324. if (_fdb.port_mask & BIT(port)) {
  1325. ret = cb(_fdb.mac, _fdb.vid, _fdb.noarp,
  1326. data);
  1327. if (ret < 0)
  1328. break;
  1329. }
  1330. }
  1331. } while (--cnt &&
  1332. !(rsp & ATC_SRCH_END) &&
  1333. !mt7530_fdb_cmd(priv, MT7530_FDB_NEXT, &rsp));
  1334. err:
  1335. mutex_unlock(&priv->reg_mutex);
  1336. return 0;
  1337. }
  1338. static int
  1339. mt7530_port_mdb_add(struct dsa_switch *ds, int port,
  1340. const struct switchdev_obj_port_mdb *mdb,
  1341. struct dsa_db db)
  1342. {
  1343. struct mt7530_priv *priv = ds->priv;
  1344. const u8 *addr = mdb->addr;
  1345. u16 vid = mdb->vid;
  1346. u8 port_mask = 0;
  1347. int ret;
  1348. mutex_lock(&priv->reg_mutex);
  1349. mt7530_fdb_write(priv, vid, 0, addr, 0, STATIC_EMP);
  1350. if (!mt7530_fdb_cmd(priv, MT7530_FDB_READ, NULL))
  1351. port_mask = (mt7530_read(priv, MT7530_ATRD) >> PORT_MAP)
  1352. & PORT_MAP_MASK;
  1353. port_mask |= BIT(port);
  1354. mt7530_fdb_write(priv, vid, port_mask, addr, -1, STATIC_ENT);
  1355. ret = mt7530_fdb_cmd(priv, MT7530_FDB_WRITE, NULL);
  1356. mutex_unlock(&priv->reg_mutex);
  1357. return ret;
  1358. }
  1359. static int
  1360. mt7530_port_mdb_del(struct dsa_switch *ds, int port,
  1361. const struct switchdev_obj_port_mdb *mdb,
  1362. struct dsa_db db)
  1363. {
  1364. struct mt7530_priv *priv = ds->priv;
  1365. const u8 *addr = mdb->addr;
  1366. u16 vid = mdb->vid;
  1367. u8 port_mask = 0;
  1368. int ret;
  1369. mutex_lock(&priv->reg_mutex);
  1370. mt7530_fdb_write(priv, vid, 0, addr, 0, STATIC_EMP);
  1371. if (!mt7530_fdb_cmd(priv, MT7530_FDB_READ, NULL))
  1372. port_mask = (mt7530_read(priv, MT7530_ATRD) >> PORT_MAP)
  1373. & PORT_MAP_MASK;
  1374. port_mask &= ~BIT(port);
  1375. mt7530_fdb_write(priv, vid, port_mask, addr, -1,
  1376. port_mask ? STATIC_ENT : STATIC_EMP);
  1377. ret = mt7530_fdb_cmd(priv, MT7530_FDB_WRITE, NULL);
  1378. mutex_unlock(&priv->reg_mutex);
  1379. return ret;
  1380. }
  1381. static int
  1382. mt7530_vlan_cmd(struct mt7530_priv *priv, enum mt7530_vlan_cmd cmd, u16 vid)
  1383. {
  1384. struct mt7530_dummy_poll p;
  1385. u32 val;
  1386. int ret;
  1387. val = VTCR_BUSY | VTCR_FUNC(cmd) | vid;
  1388. mt7530_write(priv, MT7530_VTCR, val);
  1389. INIT_MT7530_DUMMY_POLL(&p, priv, MT7530_VTCR);
  1390. ret = readx_poll_timeout(_mt7530_read, &p, val,
  1391. !(val & VTCR_BUSY), 20, 20000);
  1392. if (ret < 0) {
  1393. dev_err(priv->dev, "poll timeout\n");
  1394. return ret;
  1395. }
  1396. val = mt7530_read(priv, MT7530_VTCR);
  1397. if (val & VTCR_INVALID) {
  1398. dev_err(priv->dev, "read VTCR invalid\n");
  1399. return -EINVAL;
  1400. }
  1401. return 0;
  1402. }
  1403. static int
  1404. mt7530_port_vlan_filtering(struct dsa_switch *ds, int port, bool vlan_filtering,
  1405. struct netlink_ext_ack *extack)
  1406. {
  1407. struct dsa_port *dp = dsa_to_port(ds, port);
  1408. struct dsa_port *cpu_dp = dp->cpu_dp;
  1409. if (vlan_filtering) {
  1410. /* The port is being kept as VLAN-unaware port when bridge is
  1411. * set up with vlan_filtering not being set, Otherwise, the
  1412. * port and the corresponding CPU port is required the setup
  1413. * for becoming a VLAN-aware port.
  1414. */
  1415. mt7530_port_set_vlan_aware(ds, port);
  1416. mt7530_port_set_vlan_aware(ds, cpu_dp->index);
  1417. } else {
  1418. mt7530_port_set_vlan_unaware(ds, port);
  1419. }
  1420. return 0;
  1421. }
  1422. static void
  1423. mt7530_hw_vlan_add(struct mt7530_priv *priv,
  1424. struct mt7530_hw_vlan_entry *entry)
  1425. {
  1426. struct dsa_port *dp = dsa_to_port(priv->ds, entry->port);
  1427. u8 new_members;
  1428. u32 val;
  1429. new_members = entry->old_members | BIT(entry->port);
  1430. /* Validate the entry with independent learning, create egress tag per
  1431. * VLAN and joining the port as one of the port members.
  1432. */
  1433. val = IVL_MAC | VTAG_EN | PORT_MEM(new_members) | FID(FID_BRIDGED) |
  1434. VLAN_VALID;
  1435. mt7530_write(priv, MT7530_VAWD1, val);
  1436. /* Decide whether adding tag or not for those outgoing packets from the
  1437. * port inside the VLAN.
  1438. * CPU port is always taken as a tagged port for serving more than one
  1439. * VLANs across and also being applied with egress type stack mode for
  1440. * that VLAN tags would be appended after hardware special tag used as
  1441. * DSA tag.
  1442. */
  1443. if (dsa_port_is_cpu(dp))
  1444. val = MT7530_VLAN_EGRESS_STACK;
  1445. else if (entry->untagged)
  1446. val = MT7530_VLAN_EGRESS_UNTAG;
  1447. else
  1448. val = MT7530_VLAN_EGRESS_TAG;
  1449. mt7530_rmw(priv, MT7530_VAWD2,
  1450. ETAG_CTRL_P_MASK(entry->port),
  1451. ETAG_CTRL_P(entry->port, val));
  1452. }
  1453. static void
  1454. mt7530_hw_vlan_del(struct mt7530_priv *priv,
  1455. struct mt7530_hw_vlan_entry *entry)
  1456. {
  1457. u8 new_members;
  1458. u32 val;
  1459. new_members = entry->old_members & ~BIT(entry->port);
  1460. val = mt7530_read(priv, MT7530_VAWD1);
  1461. if (!(val & VLAN_VALID)) {
  1462. dev_err(priv->dev,
  1463. "Cannot be deleted due to invalid entry\n");
  1464. return;
  1465. }
  1466. if (new_members) {
  1467. val = IVL_MAC | VTAG_EN | PORT_MEM(new_members) |
  1468. VLAN_VALID;
  1469. mt7530_write(priv, MT7530_VAWD1, val);
  1470. } else {
  1471. mt7530_write(priv, MT7530_VAWD1, 0);
  1472. mt7530_write(priv, MT7530_VAWD2, 0);
  1473. }
  1474. }
  1475. static void
  1476. mt7530_hw_vlan_update(struct mt7530_priv *priv, u16 vid,
  1477. struct mt7530_hw_vlan_entry *entry,
  1478. mt7530_vlan_op vlan_op)
  1479. {
  1480. u32 val;
  1481. /* Fetch entry */
  1482. mt7530_vlan_cmd(priv, MT7530_VTCR_RD_VID, vid);
  1483. val = mt7530_read(priv, MT7530_VAWD1);
  1484. entry->old_members = (val >> PORT_MEM_SHFT) & PORT_MEM_MASK;
  1485. /* Manipulate entry */
  1486. vlan_op(priv, entry);
  1487. /* Flush result to hardware */
  1488. mt7530_vlan_cmd(priv, MT7530_VTCR_WR_VID, vid);
  1489. }
  1490. static int
  1491. mt7530_setup_vlan0(struct mt7530_priv *priv)
  1492. {
  1493. u32 val;
  1494. /* Validate the entry with independent learning, keep the original
  1495. * ingress tag attribute.
  1496. */
  1497. val = IVL_MAC | EG_CON | PORT_MEM(MT7530_ALL_MEMBERS) | FID(FID_BRIDGED) |
  1498. VLAN_VALID;
  1499. mt7530_write(priv, MT7530_VAWD1, val);
  1500. return mt7530_vlan_cmd(priv, MT7530_VTCR_WR_VID, 0);
  1501. }
  1502. static int
  1503. mt7530_port_vlan_add(struct dsa_switch *ds, int port,
  1504. const struct switchdev_obj_port_vlan *vlan,
  1505. struct netlink_ext_ack *extack)
  1506. {
  1507. bool untagged = vlan->flags & BRIDGE_VLAN_INFO_UNTAGGED;
  1508. bool pvid = vlan->flags & BRIDGE_VLAN_INFO_PVID;
  1509. struct mt7530_hw_vlan_entry new_entry;
  1510. struct mt7530_priv *priv = ds->priv;
  1511. mutex_lock(&priv->reg_mutex);
  1512. mt7530_hw_vlan_entry_init(&new_entry, port, untagged);
  1513. mt7530_hw_vlan_update(priv, vlan->vid, &new_entry, mt7530_hw_vlan_add);
  1514. if (pvid) {
  1515. priv->ports[port].pvid = vlan->vid;
  1516. /* Accept all frames if PVID is set */
  1517. mt7530_rmw(priv, MT7530_PVC_P(port), ACC_FRM_MASK,
  1518. MT7530_VLAN_ACC_ALL);
  1519. /* Only configure PVID if VLAN filtering is enabled */
  1520. if (dsa_port_is_vlan_filtering(dsa_to_port(ds, port)))
  1521. mt7530_rmw(priv, MT7530_PPBV1_P(port),
  1522. G0_PORT_VID_MASK,
  1523. G0_PORT_VID(vlan->vid));
  1524. } else if (vlan->vid && priv->ports[port].pvid == vlan->vid) {
  1525. /* This VLAN is overwritten without PVID, so unset it */
  1526. priv->ports[port].pvid = G0_PORT_VID_DEF;
  1527. /* Only accept tagged frames if the port is VLAN-aware */
  1528. if (dsa_port_is_vlan_filtering(dsa_to_port(ds, port)))
  1529. mt7530_rmw(priv, MT7530_PVC_P(port), ACC_FRM_MASK,
  1530. MT7530_VLAN_ACC_TAGGED);
  1531. mt7530_rmw(priv, MT7530_PPBV1_P(port), G0_PORT_VID_MASK,
  1532. G0_PORT_VID_DEF);
  1533. }
  1534. mutex_unlock(&priv->reg_mutex);
  1535. return 0;
  1536. }
  1537. static int
  1538. mt7530_port_vlan_del(struct dsa_switch *ds, int port,
  1539. const struct switchdev_obj_port_vlan *vlan)
  1540. {
  1541. struct mt7530_hw_vlan_entry target_entry;
  1542. struct mt7530_priv *priv = ds->priv;
  1543. mutex_lock(&priv->reg_mutex);
  1544. mt7530_hw_vlan_entry_init(&target_entry, port, 0);
  1545. mt7530_hw_vlan_update(priv, vlan->vid, &target_entry,
  1546. mt7530_hw_vlan_del);
  1547. /* PVID is being restored to the default whenever the PVID port
  1548. * is being removed from the VLAN.
  1549. */
  1550. if (priv->ports[port].pvid == vlan->vid) {
  1551. priv->ports[port].pvid = G0_PORT_VID_DEF;
  1552. /* Only accept tagged frames if the port is VLAN-aware */
  1553. if (dsa_port_is_vlan_filtering(dsa_to_port(ds, port)))
  1554. mt7530_rmw(priv, MT7530_PVC_P(port), ACC_FRM_MASK,
  1555. MT7530_VLAN_ACC_TAGGED);
  1556. mt7530_rmw(priv, MT7530_PPBV1_P(port), G0_PORT_VID_MASK,
  1557. G0_PORT_VID_DEF);
  1558. }
  1559. mutex_unlock(&priv->reg_mutex);
  1560. return 0;
  1561. }
  1562. static int mt753x_port_mirror_add(struct dsa_switch *ds, int port,
  1563. struct dsa_mall_mirror_tc_entry *mirror,
  1564. bool ingress, struct netlink_ext_ack *extack)
  1565. {
  1566. struct mt7530_priv *priv = ds->priv;
  1567. int monitor_port;
  1568. u32 val;
  1569. /* Check for existent entry */
  1570. if ((ingress ? priv->mirror_rx : priv->mirror_tx) & BIT(port))
  1571. return -EEXIST;
  1572. val = mt7530_read(priv, MT753X_MIRROR_REG(priv->id));
  1573. /* MT7530 only supports one monitor port */
  1574. monitor_port = MT753X_MIRROR_PORT_GET(priv->id, val);
  1575. if (val & MT753X_MIRROR_EN(priv->id) &&
  1576. monitor_port != mirror->to_local_port)
  1577. return -EEXIST;
  1578. val |= MT753X_MIRROR_EN(priv->id);
  1579. val &= ~MT753X_MIRROR_PORT_MASK(priv->id);
  1580. val |= MT753X_MIRROR_PORT_SET(priv->id, mirror->to_local_port);
  1581. mt7530_write(priv, MT753X_MIRROR_REG(priv->id), val);
  1582. val = mt7530_read(priv, MT7530_PCR_P(port));
  1583. if (ingress) {
  1584. val |= PORT_RX_MIR;
  1585. priv->mirror_rx |= BIT(port);
  1586. } else {
  1587. val |= PORT_TX_MIR;
  1588. priv->mirror_tx |= BIT(port);
  1589. }
  1590. mt7530_write(priv, MT7530_PCR_P(port), val);
  1591. return 0;
  1592. }
  1593. static void mt753x_port_mirror_del(struct dsa_switch *ds, int port,
  1594. struct dsa_mall_mirror_tc_entry *mirror)
  1595. {
  1596. struct mt7530_priv *priv = ds->priv;
  1597. u32 val;
  1598. val = mt7530_read(priv, MT7530_PCR_P(port));
  1599. if (mirror->ingress) {
  1600. val &= ~PORT_RX_MIR;
  1601. priv->mirror_rx &= ~BIT(port);
  1602. } else {
  1603. val &= ~PORT_TX_MIR;
  1604. priv->mirror_tx &= ~BIT(port);
  1605. }
  1606. mt7530_write(priv, MT7530_PCR_P(port), val);
  1607. if (!priv->mirror_rx && !priv->mirror_tx) {
  1608. val = mt7530_read(priv, MT753X_MIRROR_REG(priv->id));
  1609. val &= ~MT753X_MIRROR_EN(priv->id);
  1610. mt7530_write(priv, MT753X_MIRROR_REG(priv->id), val);
  1611. }
  1612. }
  1613. static enum dsa_tag_protocol
  1614. mtk_get_tag_protocol(struct dsa_switch *ds, int port,
  1615. enum dsa_tag_protocol mp)
  1616. {
  1617. return DSA_TAG_PROTO_MTK;
  1618. }
  1619. #ifdef CONFIG_GPIOLIB
  1620. static inline u32
  1621. mt7530_gpio_to_bit(unsigned int offset)
  1622. {
  1623. /* Map GPIO offset to register bit
  1624. * [ 2: 0] port 0 LED 0..2 as GPIO 0..2
  1625. * [ 6: 4] port 1 LED 0..2 as GPIO 3..5
  1626. * [10: 8] port 2 LED 0..2 as GPIO 6..8
  1627. * [14:12] port 3 LED 0..2 as GPIO 9..11
  1628. * [18:16] port 4 LED 0..2 as GPIO 12..14
  1629. */
  1630. return BIT(offset + offset / 3);
  1631. }
  1632. static int
  1633. mt7530_gpio_get(struct gpio_chip *gc, unsigned int offset)
  1634. {
  1635. struct mt7530_priv *priv = gpiochip_get_data(gc);
  1636. u32 bit = mt7530_gpio_to_bit(offset);
  1637. return !!(mt7530_read(priv, MT7530_LED_GPIO_DATA) & bit);
  1638. }
  1639. static void
  1640. mt7530_gpio_set(struct gpio_chip *gc, unsigned int offset, int value)
  1641. {
  1642. struct mt7530_priv *priv = gpiochip_get_data(gc);
  1643. u32 bit = mt7530_gpio_to_bit(offset);
  1644. if (value)
  1645. mt7530_set(priv, MT7530_LED_GPIO_DATA, bit);
  1646. else
  1647. mt7530_clear(priv, MT7530_LED_GPIO_DATA, bit);
  1648. }
  1649. static int
  1650. mt7530_gpio_get_direction(struct gpio_chip *gc, unsigned int offset)
  1651. {
  1652. struct mt7530_priv *priv = gpiochip_get_data(gc);
  1653. u32 bit = mt7530_gpio_to_bit(offset);
  1654. return (mt7530_read(priv, MT7530_LED_GPIO_DIR) & bit) ?
  1655. GPIO_LINE_DIRECTION_OUT : GPIO_LINE_DIRECTION_IN;
  1656. }
  1657. static int
  1658. mt7530_gpio_direction_input(struct gpio_chip *gc, unsigned int offset)
  1659. {
  1660. struct mt7530_priv *priv = gpiochip_get_data(gc);
  1661. u32 bit = mt7530_gpio_to_bit(offset);
  1662. mt7530_clear(priv, MT7530_LED_GPIO_OE, bit);
  1663. mt7530_clear(priv, MT7530_LED_GPIO_DIR, bit);
  1664. return 0;
  1665. }
  1666. static int
  1667. mt7530_gpio_direction_output(struct gpio_chip *gc, unsigned int offset, int value)
  1668. {
  1669. struct mt7530_priv *priv = gpiochip_get_data(gc);
  1670. u32 bit = mt7530_gpio_to_bit(offset);
  1671. mt7530_set(priv, MT7530_LED_GPIO_DIR, bit);
  1672. if (value)
  1673. mt7530_set(priv, MT7530_LED_GPIO_DATA, bit);
  1674. else
  1675. mt7530_clear(priv, MT7530_LED_GPIO_DATA, bit);
  1676. mt7530_set(priv, MT7530_LED_GPIO_OE, bit);
  1677. return 0;
  1678. }
  1679. static int
  1680. mt7530_setup_gpio(struct mt7530_priv *priv)
  1681. {
  1682. struct device *dev = priv->dev;
  1683. struct gpio_chip *gc;
  1684. gc = devm_kzalloc(dev, sizeof(*gc), GFP_KERNEL);
  1685. if (!gc)
  1686. return -ENOMEM;
  1687. mt7530_write(priv, MT7530_LED_GPIO_OE, 0);
  1688. mt7530_write(priv, MT7530_LED_GPIO_DIR, 0);
  1689. mt7530_write(priv, MT7530_LED_IO_MODE, 0);
  1690. gc->label = "mt7530";
  1691. gc->parent = dev;
  1692. gc->owner = THIS_MODULE;
  1693. gc->get_direction = mt7530_gpio_get_direction;
  1694. gc->direction_input = mt7530_gpio_direction_input;
  1695. gc->direction_output = mt7530_gpio_direction_output;
  1696. gc->get = mt7530_gpio_get;
  1697. gc->set = mt7530_gpio_set;
  1698. gc->base = -1;
  1699. gc->ngpio = 15;
  1700. gc->can_sleep = true;
  1701. return devm_gpiochip_add_data(dev, gc, priv);
  1702. }
  1703. #endif /* CONFIG_GPIOLIB */
  1704. static irqreturn_t
  1705. mt7530_irq_thread_fn(int irq, void *dev_id)
  1706. {
  1707. struct mt7530_priv *priv = dev_id;
  1708. bool handled = false;
  1709. u32 val;
  1710. int p;
  1711. mt7530_mutex_lock(priv);
  1712. val = mt7530_mii_read(priv, MT7530_SYS_INT_STS);
  1713. mt7530_mii_write(priv, MT7530_SYS_INT_STS, val);
  1714. mt7530_mutex_unlock(priv);
  1715. for (p = 0; p < MT7530_NUM_PHYS; p++) {
  1716. if (BIT(p) & val) {
  1717. unsigned int irq;
  1718. irq = irq_find_mapping(priv->irq_domain, p);
  1719. handle_nested_irq(irq);
  1720. handled = true;
  1721. }
  1722. }
  1723. return IRQ_RETVAL(handled);
  1724. }
  1725. static void
  1726. mt7530_irq_mask(struct irq_data *d)
  1727. {
  1728. struct mt7530_priv *priv = irq_data_get_irq_chip_data(d);
  1729. priv->irq_enable &= ~BIT(d->hwirq);
  1730. }
  1731. static void
  1732. mt7530_irq_unmask(struct irq_data *d)
  1733. {
  1734. struct mt7530_priv *priv = irq_data_get_irq_chip_data(d);
  1735. priv->irq_enable |= BIT(d->hwirq);
  1736. }
  1737. static void
  1738. mt7530_irq_bus_lock(struct irq_data *d)
  1739. {
  1740. struct mt7530_priv *priv = irq_data_get_irq_chip_data(d);
  1741. mt7530_mutex_lock(priv);
  1742. }
  1743. static void
  1744. mt7530_irq_bus_sync_unlock(struct irq_data *d)
  1745. {
  1746. struct mt7530_priv *priv = irq_data_get_irq_chip_data(d);
  1747. mt7530_mii_write(priv, MT7530_SYS_INT_EN, priv->irq_enable);
  1748. mt7530_mutex_unlock(priv);
  1749. }
  1750. static struct irq_chip mt7530_irq_chip = {
  1751. .name = KBUILD_MODNAME,
  1752. .irq_mask = mt7530_irq_mask,
  1753. .irq_unmask = mt7530_irq_unmask,
  1754. .irq_bus_lock = mt7530_irq_bus_lock,
  1755. .irq_bus_sync_unlock = mt7530_irq_bus_sync_unlock,
  1756. };
  1757. static int
  1758. mt7530_irq_map(struct irq_domain *domain, unsigned int irq,
  1759. irq_hw_number_t hwirq)
  1760. {
  1761. irq_set_chip_data(irq, domain->host_data);
  1762. irq_set_chip_and_handler(irq, &mt7530_irq_chip, handle_simple_irq);
  1763. irq_set_nested_thread(irq, true);
  1764. irq_set_noprobe(irq);
  1765. return 0;
  1766. }
  1767. static const struct irq_domain_ops mt7530_irq_domain_ops = {
  1768. .map = mt7530_irq_map,
  1769. .xlate = irq_domain_xlate_onecell,
  1770. };
  1771. static void
  1772. mt7988_irq_mask(struct irq_data *d)
  1773. {
  1774. struct mt7530_priv *priv = irq_data_get_irq_chip_data(d);
  1775. priv->irq_enable &= ~BIT(d->hwirq);
  1776. mt7530_mii_write(priv, MT7530_SYS_INT_EN, priv->irq_enable);
  1777. }
  1778. static void
  1779. mt7988_irq_unmask(struct irq_data *d)
  1780. {
  1781. struct mt7530_priv *priv = irq_data_get_irq_chip_data(d);
  1782. priv->irq_enable |= BIT(d->hwirq);
  1783. mt7530_mii_write(priv, MT7530_SYS_INT_EN, priv->irq_enable);
  1784. }
  1785. static struct irq_chip mt7988_irq_chip = {
  1786. .name = KBUILD_MODNAME,
  1787. .irq_mask = mt7988_irq_mask,
  1788. .irq_unmask = mt7988_irq_unmask,
  1789. };
  1790. static int
  1791. mt7988_irq_map(struct irq_domain *domain, unsigned int irq,
  1792. irq_hw_number_t hwirq)
  1793. {
  1794. irq_set_chip_data(irq, domain->host_data);
  1795. irq_set_chip_and_handler(irq, &mt7988_irq_chip, handle_simple_irq);
  1796. irq_set_nested_thread(irq, true);
  1797. irq_set_noprobe(irq);
  1798. return 0;
  1799. }
  1800. static const struct irq_domain_ops mt7988_irq_domain_ops = {
  1801. .map = mt7988_irq_map,
  1802. .xlate = irq_domain_xlate_onecell,
  1803. };
  1804. static void
  1805. mt7530_setup_mdio_irq(struct mt7530_priv *priv)
  1806. {
  1807. struct dsa_switch *ds = priv->ds;
  1808. int p;
  1809. for (p = 0; p < MT7530_NUM_PHYS; p++) {
  1810. if (BIT(p) & ds->phys_mii_mask) {
  1811. unsigned int irq;
  1812. irq = irq_create_mapping(priv->irq_domain, p);
  1813. ds->user_mii_bus->irq[p] = irq;
  1814. }
  1815. }
  1816. }
  1817. static int
  1818. mt7530_setup_irq(struct mt7530_priv *priv)
  1819. {
  1820. struct device *dev = priv->dev;
  1821. struct device_node *np = dev->of_node;
  1822. int ret;
  1823. if (!of_property_read_bool(np, "interrupt-controller")) {
  1824. dev_info(dev, "no interrupt support\n");
  1825. return 0;
  1826. }
  1827. priv->irq = of_irq_get(np, 0);
  1828. if (priv->irq <= 0) {
  1829. dev_err(dev, "failed to get parent IRQ: %d\n", priv->irq);
  1830. return priv->irq ? : -EINVAL;
  1831. }
  1832. if (priv->id == ID_MT7988 || priv->id == ID_EN7581)
  1833. priv->irq_domain = irq_domain_add_linear(np, MT7530_NUM_PHYS,
  1834. &mt7988_irq_domain_ops,
  1835. priv);
  1836. else
  1837. priv->irq_domain = irq_domain_add_linear(np, MT7530_NUM_PHYS,
  1838. &mt7530_irq_domain_ops,
  1839. priv);
  1840. if (!priv->irq_domain) {
  1841. dev_err(dev, "failed to create IRQ domain\n");
  1842. return -ENOMEM;
  1843. }
  1844. /* This register must be set for MT7530 to properly fire interrupts */
  1845. if (priv->id == ID_MT7530 || priv->id == ID_MT7621)
  1846. mt7530_set(priv, MT7530_TOP_SIG_CTRL, TOP_SIG_CTRL_NORMAL);
  1847. ret = request_threaded_irq(priv->irq, NULL, mt7530_irq_thread_fn,
  1848. IRQF_ONESHOT, KBUILD_MODNAME, priv);
  1849. if (ret) {
  1850. irq_domain_remove(priv->irq_domain);
  1851. dev_err(dev, "failed to request IRQ: %d\n", ret);
  1852. return ret;
  1853. }
  1854. return 0;
  1855. }
  1856. static void
  1857. mt7530_free_mdio_irq(struct mt7530_priv *priv)
  1858. {
  1859. int p;
  1860. for (p = 0; p < MT7530_NUM_PHYS; p++) {
  1861. if (BIT(p) & priv->ds->phys_mii_mask) {
  1862. unsigned int irq;
  1863. irq = irq_find_mapping(priv->irq_domain, p);
  1864. irq_dispose_mapping(irq);
  1865. }
  1866. }
  1867. }
  1868. static void
  1869. mt7530_free_irq_common(struct mt7530_priv *priv)
  1870. {
  1871. free_irq(priv->irq, priv);
  1872. irq_domain_remove(priv->irq_domain);
  1873. }
  1874. static void
  1875. mt7530_free_irq(struct mt7530_priv *priv)
  1876. {
  1877. struct device_node *mnp, *np = priv->dev->of_node;
  1878. mnp = of_get_child_by_name(np, "mdio");
  1879. if (!mnp)
  1880. mt7530_free_mdio_irq(priv);
  1881. of_node_put(mnp);
  1882. mt7530_free_irq_common(priv);
  1883. }
  1884. static int
  1885. mt7530_setup_mdio(struct mt7530_priv *priv)
  1886. {
  1887. struct device_node *mnp, *np = priv->dev->of_node;
  1888. struct dsa_switch *ds = priv->ds;
  1889. struct device *dev = priv->dev;
  1890. struct mii_bus *bus;
  1891. static int idx;
  1892. int ret = 0;
  1893. mnp = of_get_child_by_name(np, "mdio");
  1894. if (mnp && !of_device_is_available(mnp))
  1895. goto out;
  1896. bus = devm_mdiobus_alloc(dev);
  1897. if (!bus) {
  1898. ret = -ENOMEM;
  1899. goto out;
  1900. }
  1901. if (!mnp)
  1902. ds->user_mii_bus = bus;
  1903. bus->priv = priv;
  1904. bus->name = KBUILD_MODNAME "-mii";
  1905. snprintf(bus->id, MII_BUS_ID_SIZE, KBUILD_MODNAME "-%d", idx++);
  1906. bus->read = mt753x_phy_read_c22;
  1907. bus->write = mt753x_phy_write_c22;
  1908. bus->read_c45 = mt753x_phy_read_c45;
  1909. bus->write_c45 = mt753x_phy_write_c45;
  1910. bus->parent = dev;
  1911. bus->phy_mask = ~ds->phys_mii_mask;
  1912. if (priv->irq && !mnp)
  1913. mt7530_setup_mdio_irq(priv);
  1914. ret = devm_of_mdiobus_register(dev, bus, mnp);
  1915. if (ret) {
  1916. dev_err(dev, "failed to register MDIO bus: %d\n", ret);
  1917. if (priv->irq && !mnp)
  1918. mt7530_free_mdio_irq(priv);
  1919. }
  1920. out:
  1921. of_node_put(mnp);
  1922. return ret;
  1923. }
  1924. static int
  1925. mt7530_setup(struct dsa_switch *ds)
  1926. {
  1927. struct mt7530_priv *priv = ds->priv;
  1928. struct device_node *dn = NULL;
  1929. struct device_node *phy_node;
  1930. struct device_node *mac_np;
  1931. struct mt7530_dummy_poll p;
  1932. phy_interface_t interface;
  1933. struct dsa_port *cpu_dp;
  1934. u32 id, val;
  1935. int ret, i;
  1936. /* The parent node of conduit netdev which holds the common system
  1937. * controller also is the container for two GMACs nodes representing
  1938. * as two netdev instances.
  1939. */
  1940. dsa_switch_for_each_cpu_port(cpu_dp, ds) {
  1941. dn = cpu_dp->conduit->dev.of_node->parent;
  1942. /* It doesn't matter which CPU port is found first,
  1943. * their conduits should share the same parent OF node
  1944. */
  1945. break;
  1946. }
  1947. if (!dn) {
  1948. dev_err(ds->dev, "parent OF node of DSA conduit not found");
  1949. return -EINVAL;
  1950. }
  1951. ds->assisted_learning_on_cpu_port = true;
  1952. ds->mtu_enforcement_ingress = true;
  1953. if (priv->id == ID_MT7530) {
  1954. regulator_set_voltage(priv->core_pwr, 1000000, 1000000);
  1955. ret = regulator_enable(priv->core_pwr);
  1956. if (ret < 0) {
  1957. dev_err(priv->dev,
  1958. "Failed to enable core power: %d\n", ret);
  1959. return ret;
  1960. }
  1961. regulator_set_voltage(priv->io_pwr, 3300000, 3300000);
  1962. ret = regulator_enable(priv->io_pwr);
  1963. if (ret < 0) {
  1964. dev_err(priv->dev, "Failed to enable io pwr: %d\n",
  1965. ret);
  1966. return ret;
  1967. }
  1968. }
  1969. /* Reset whole chip through gpio pin or memory-mapped registers for
  1970. * different type of hardware
  1971. */
  1972. if (priv->mcm) {
  1973. reset_control_assert(priv->rstc);
  1974. usleep_range(5000, 5100);
  1975. reset_control_deassert(priv->rstc);
  1976. } else {
  1977. gpiod_set_value_cansleep(priv->reset, 0);
  1978. usleep_range(5000, 5100);
  1979. gpiod_set_value_cansleep(priv->reset, 1);
  1980. }
  1981. /* Waiting for MT7530 got to stable */
  1982. INIT_MT7530_DUMMY_POLL(&p, priv, MT753X_TRAP);
  1983. ret = readx_poll_timeout(_mt7530_read, &p, val, val != 0,
  1984. 20, 1000000);
  1985. if (ret < 0) {
  1986. dev_err(priv->dev, "reset timeout\n");
  1987. return ret;
  1988. }
  1989. id = mt7530_read(priv, MT7530_CREV);
  1990. id >>= CHIP_NAME_SHIFT;
  1991. if (id != MT7530_ID) {
  1992. dev_err(priv->dev, "chip %x can't be supported\n", id);
  1993. return -ENODEV;
  1994. }
  1995. if ((val & MT7530_XTAL_MASK) == MT7530_XTAL_20MHZ) {
  1996. dev_err(priv->dev,
  1997. "MT7530 with a 20MHz XTAL is not supported!\n");
  1998. return -EINVAL;
  1999. }
  2000. /* Reset the switch through internal reset */
  2001. mt7530_write(priv, MT7530_SYS_CTRL,
  2002. SYS_CTRL_PHY_RST | SYS_CTRL_SW_RST |
  2003. SYS_CTRL_REG_RST);
  2004. /* Lower Tx driving for TRGMII path */
  2005. for (i = 0; i < NUM_TRGMII_CTRL; i++)
  2006. mt7530_write(priv, MT7530_TRGMII_TD_ODT(i),
  2007. TD_DM_DRVP(8) | TD_DM_DRVN(8));
  2008. for (i = 0; i < NUM_TRGMII_CTRL; i++)
  2009. mt7530_rmw(priv, MT7530_TRGMII_RD(i),
  2010. RD_TAP_MASK, RD_TAP(16));
  2011. /* Allow modifying the trap and directly access PHY registers via the
  2012. * MDIO bus the switch is on.
  2013. */
  2014. mt7530_rmw(priv, MT753X_MTRAP, MT7530_CHG_TRAP |
  2015. MT7530_PHY_INDIRECT_ACCESS, MT7530_CHG_TRAP);
  2016. if ((val & MT7530_XTAL_MASK) == MT7530_XTAL_40MHZ)
  2017. mt7530_pll_setup(priv);
  2018. mt753x_trap_frames(priv);
  2019. /* Enable and reset MIB counters */
  2020. mt7530_mib_reset(ds);
  2021. for (i = 0; i < priv->ds->num_ports; i++) {
  2022. /* Clear link settings and enable force mode to force link down
  2023. * on all ports until they're enabled later.
  2024. */
  2025. mt7530_rmw(priv, MT753X_PMCR_P(i),
  2026. PMCR_LINK_SETTINGS_MASK |
  2027. MT753X_FORCE_MODE(priv->id),
  2028. MT753X_FORCE_MODE(priv->id));
  2029. /* Disable forwarding by default on all ports */
  2030. mt7530_rmw(priv, MT7530_PCR_P(i), PCR_MATRIX_MASK,
  2031. PCR_MATRIX_CLR);
  2032. /* Disable learning by default on all ports */
  2033. mt7530_set(priv, MT7530_PSC_P(i), SA_DIS);
  2034. if (dsa_is_cpu_port(ds, i)) {
  2035. mt753x_cpu_port_enable(ds, i);
  2036. } else {
  2037. mt7530_port_disable(ds, i);
  2038. /* Set default PVID to 0 on all user ports */
  2039. mt7530_rmw(priv, MT7530_PPBV1_P(i), G0_PORT_VID_MASK,
  2040. G0_PORT_VID_DEF);
  2041. }
  2042. /* Enable consistent egress tag */
  2043. mt7530_rmw(priv, MT7530_PVC_P(i), PVC_EG_TAG_MASK,
  2044. PVC_EG_TAG(MT7530_VLAN_EG_CONSISTENT));
  2045. }
  2046. /* Allow mirroring frames received on the local port (monitor port). */
  2047. mt7530_set(priv, MT753X_AGC, LOCAL_EN);
  2048. /* Setup VLAN ID 0 for VLAN-unaware bridges */
  2049. ret = mt7530_setup_vlan0(priv);
  2050. if (ret)
  2051. return ret;
  2052. /* Check for PHY muxing on port 5 */
  2053. if (dsa_is_unused_port(ds, 5)) {
  2054. /* Scan the ethernet nodes. Look for GMAC1, lookup the used PHY.
  2055. * Set priv->p5_mode to the appropriate value if PHY muxing is
  2056. * detected.
  2057. */
  2058. for_each_child_of_node(dn, mac_np) {
  2059. if (!of_device_is_compatible(mac_np,
  2060. "mediatek,eth-mac"))
  2061. continue;
  2062. ret = of_property_read_u32(mac_np, "reg", &id);
  2063. if (ret < 0 || id != 1)
  2064. continue;
  2065. phy_node = of_parse_phandle(mac_np, "phy-handle", 0);
  2066. if (!phy_node)
  2067. continue;
  2068. if (phy_node->parent == priv->dev->of_node->parent ||
  2069. phy_node->parent->parent == priv->dev->of_node) {
  2070. ret = of_get_phy_mode(mac_np, &interface);
  2071. if (ret && ret != -ENODEV) {
  2072. of_node_put(mac_np);
  2073. of_node_put(phy_node);
  2074. return ret;
  2075. }
  2076. id = of_mdio_parse_addr(ds->dev, phy_node);
  2077. if (id == 0)
  2078. priv->p5_mode = MUX_PHY_P0;
  2079. if (id == 4)
  2080. priv->p5_mode = MUX_PHY_P4;
  2081. }
  2082. of_node_put(mac_np);
  2083. of_node_put(phy_node);
  2084. break;
  2085. }
  2086. if (priv->p5_mode == MUX_PHY_P0 ||
  2087. priv->p5_mode == MUX_PHY_P4) {
  2088. mt7530_clear(priv, MT753X_MTRAP, MT7530_P5_DIS);
  2089. mt7530_setup_port5(ds, interface);
  2090. }
  2091. }
  2092. #ifdef CONFIG_GPIOLIB
  2093. if (of_property_read_bool(priv->dev->of_node, "gpio-controller")) {
  2094. ret = mt7530_setup_gpio(priv);
  2095. if (ret)
  2096. return ret;
  2097. }
  2098. #endif /* CONFIG_GPIOLIB */
  2099. /* Flush the FDB table */
  2100. ret = mt7530_fdb_cmd(priv, MT7530_FDB_FLUSH, NULL);
  2101. if (ret < 0)
  2102. return ret;
  2103. return 0;
  2104. }
  2105. static int
  2106. mt7531_setup_common(struct dsa_switch *ds)
  2107. {
  2108. struct mt7530_priv *priv = ds->priv;
  2109. int ret, i;
  2110. ds->assisted_learning_on_cpu_port = true;
  2111. ds->mtu_enforcement_ingress = true;
  2112. mt753x_trap_frames(priv);
  2113. /* Enable and reset MIB counters */
  2114. mt7530_mib_reset(ds);
  2115. /* Disable flooding on all ports */
  2116. mt7530_clear(priv, MT753X_MFC, BC_FFP_MASK | UNM_FFP_MASK |
  2117. UNU_FFP_MASK);
  2118. for (i = 0; i < priv->ds->num_ports; i++) {
  2119. /* Clear link settings and enable force mode to force link down
  2120. * on all ports until they're enabled later.
  2121. */
  2122. mt7530_rmw(priv, MT753X_PMCR_P(i),
  2123. PMCR_LINK_SETTINGS_MASK |
  2124. MT753X_FORCE_MODE(priv->id),
  2125. MT753X_FORCE_MODE(priv->id));
  2126. /* Disable forwarding by default on all ports */
  2127. mt7530_rmw(priv, MT7530_PCR_P(i), PCR_MATRIX_MASK,
  2128. PCR_MATRIX_CLR);
  2129. /* Disable learning by default on all ports */
  2130. mt7530_set(priv, MT7530_PSC_P(i), SA_DIS);
  2131. mt7530_set(priv, MT7531_DBG_CNT(i), MT7531_DIS_CLR);
  2132. if (dsa_is_cpu_port(ds, i)) {
  2133. mt753x_cpu_port_enable(ds, i);
  2134. } else {
  2135. mt7530_port_disable(ds, i);
  2136. /* Set default PVID to 0 on all user ports */
  2137. mt7530_rmw(priv, MT7530_PPBV1_P(i), G0_PORT_VID_MASK,
  2138. G0_PORT_VID_DEF);
  2139. }
  2140. /* Enable consistent egress tag */
  2141. mt7530_rmw(priv, MT7530_PVC_P(i), PVC_EG_TAG_MASK,
  2142. PVC_EG_TAG(MT7530_VLAN_EG_CONSISTENT));
  2143. }
  2144. /* Allow mirroring frames received on the local port (monitor port). */
  2145. mt7530_set(priv, MT753X_AGC, LOCAL_EN);
  2146. /* Flush the FDB table */
  2147. ret = mt7530_fdb_cmd(priv, MT7530_FDB_FLUSH, NULL);
  2148. if (ret < 0)
  2149. return ret;
  2150. /* Setup VLAN ID 0 for VLAN-unaware bridges */
  2151. return mt7530_setup_vlan0(priv);
  2152. }
  2153. static int
  2154. mt7531_setup(struct dsa_switch *ds)
  2155. {
  2156. struct mt7530_priv *priv = ds->priv;
  2157. struct mt7530_dummy_poll p;
  2158. u32 val, id;
  2159. int ret, i;
  2160. /* Reset whole chip through gpio pin or memory-mapped registers for
  2161. * different type of hardware
  2162. */
  2163. if (priv->mcm) {
  2164. reset_control_assert(priv->rstc);
  2165. usleep_range(5000, 5100);
  2166. reset_control_deassert(priv->rstc);
  2167. } else {
  2168. gpiod_set_value_cansleep(priv->reset, 0);
  2169. usleep_range(5000, 5100);
  2170. gpiod_set_value_cansleep(priv->reset, 1);
  2171. }
  2172. /* Waiting for MT7530 got to stable */
  2173. INIT_MT7530_DUMMY_POLL(&p, priv, MT753X_TRAP);
  2174. ret = readx_poll_timeout(_mt7530_read, &p, val, val != 0,
  2175. 20, 1000000);
  2176. if (ret < 0) {
  2177. dev_err(priv->dev, "reset timeout\n");
  2178. return ret;
  2179. }
  2180. id = mt7530_read(priv, MT7531_CREV);
  2181. id >>= CHIP_NAME_SHIFT;
  2182. if (id != MT7531_ID) {
  2183. dev_err(priv->dev, "chip %x can't be supported\n", id);
  2184. return -ENODEV;
  2185. }
  2186. /* MT7531AE has got two SGMII units. One for port 5, one for port 6.
  2187. * MT7531BE has got only one SGMII unit which is for port 6.
  2188. */
  2189. val = mt7530_read(priv, MT7531_TOP_SIG_SR);
  2190. priv->p5_sgmii = !!(val & PAD_DUAL_SGMII_EN);
  2191. /* Force link down on all ports before internal reset */
  2192. for (i = 0; i < priv->ds->num_ports; i++)
  2193. mt7530_write(priv, MT753X_PMCR_P(i), MT7531_FORCE_MODE_LNK);
  2194. /* Reset the switch through internal reset */
  2195. mt7530_write(priv, MT7530_SYS_CTRL, SYS_CTRL_SW_RST | SYS_CTRL_REG_RST);
  2196. if (!priv->p5_sgmii) {
  2197. mt7531_pll_setup(priv);
  2198. } else {
  2199. /* Unlike MT7531BE, the GPIO 6-12 pins are not used for RGMII on
  2200. * MT7531AE. Set the GPIO 11-12 pins to function as MDC and MDIO
  2201. * to expose the MDIO bus of the switch.
  2202. */
  2203. mt7530_rmw(priv, MT7531_GPIO_MODE1, MT7531_GPIO11_RG_RXD2_MASK,
  2204. MT7531_EXT_P_MDC_11);
  2205. mt7530_rmw(priv, MT7531_GPIO_MODE1, MT7531_GPIO12_RG_RXD3_MASK,
  2206. MT7531_EXT_P_MDIO_12);
  2207. }
  2208. mt7530_rmw(priv, MT7531_GPIO_MODE0, MT7531_GPIO0_MASK,
  2209. MT7531_GPIO0_INTERRUPT);
  2210. /* Enable Energy-Efficient Ethernet (EEE) and PHY core PLL, since
  2211. * phy_device has not yet been created provided for
  2212. * phy_[read,write]_mmd_indirect is called, we provide our own
  2213. * mt7531_ind_mmd_phy_[read,write] to complete this function.
  2214. */
  2215. val = mt7531_ind_c45_phy_read(priv,
  2216. MT753X_CTRL_PHY_ADDR(priv->mdiodev->addr),
  2217. MDIO_MMD_VEND2, CORE_PLL_GROUP4);
  2218. val |= MT7531_RG_SYSPLL_DMY2 | MT7531_PHY_PLL_BYPASS_MODE;
  2219. val &= ~MT7531_PHY_PLL_OFF;
  2220. mt7531_ind_c45_phy_write(priv,
  2221. MT753X_CTRL_PHY_ADDR(priv->mdiodev->addr),
  2222. MDIO_MMD_VEND2, CORE_PLL_GROUP4, val);
  2223. /* Disable EEE advertisement on the switch PHYs. */
  2224. for (i = MT753X_CTRL_PHY_ADDR(priv->mdiodev->addr);
  2225. i < MT753X_CTRL_PHY_ADDR(priv->mdiodev->addr) + MT7530_NUM_PHYS;
  2226. i++) {
  2227. mt7531_ind_c45_phy_write(priv, i, MDIO_MMD_AN, MDIO_AN_EEE_ADV,
  2228. 0);
  2229. }
  2230. ret = mt7531_setup_common(ds);
  2231. if (ret)
  2232. return ret;
  2233. return 0;
  2234. }
  2235. static void mt7530_mac_port_get_caps(struct dsa_switch *ds, int port,
  2236. struct phylink_config *config)
  2237. {
  2238. config->mac_capabilities |= MAC_10 | MAC_100 | MAC_1000FD;
  2239. switch (port) {
  2240. /* Ports which are connected to switch PHYs. There is no MII pinout. */
  2241. case 0 ... 4:
  2242. __set_bit(PHY_INTERFACE_MODE_GMII,
  2243. config->supported_interfaces);
  2244. break;
  2245. /* Port 5 supports rgmii with delays, mii, and gmii. */
  2246. case 5:
  2247. phy_interface_set_rgmii(config->supported_interfaces);
  2248. __set_bit(PHY_INTERFACE_MODE_MII,
  2249. config->supported_interfaces);
  2250. __set_bit(PHY_INTERFACE_MODE_GMII,
  2251. config->supported_interfaces);
  2252. break;
  2253. /* Port 6 supports rgmii and trgmii. */
  2254. case 6:
  2255. __set_bit(PHY_INTERFACE_MODE_RGMII,
  2256. config->supported_interfaces);
  2257. __set_bit(PHY_INTERFACE_MODE_TRGMII,
  2258. config->supported_interfaces);
  2259. break;
  2260. }
  2261. }
  2262. static void mt7531_mac_port_get_caps(struct dsa_switch *ds, int port,
  2263. struct phylink_config *config)
  2264. {
  2265. struct mt7530_priv *priv = ds->priv;
  2266. config->mac_capabilities |= MAC_10 | MAC_100 | MAC_1000FD;
  2267. switch (port) {
  2268. /* Ports which are connected to switch PHYs. There is no MII pinout. */
  2269. case 0 ... 4:
  2270. __set_bit(PHY_INTERFACE_MODE_GMII,
  2271. config->supported_interfaces);
  2272. break;
  2273. /* Port 5 supports rgmii with delays on MT7531BE, sgmii/802.3z on
  2274. * MT7531AE.
  2275. */
  2276. case 5:
  2277. if (!priv->p5_sgmii) {
  2278. phy_interface_set_rgmii(config->supported_interfaces);
  2279. break;
  2280. }
  2281. fallthrough;
  2282. /* Port 6 supports sgmii/802.3z. */
  2283. case 6:
  2284. __set_bit(PHY_INTERFACE_MODE_SGMII,
  2285. config->supported_interfaces);
  2286. __set_bit(PHY_INTERFACE_MODE_1000BASEX,
  2287. config->supported_interfaces);
  2288. __set_bit(PHY_INTERFACE_MODE_2500BASEX,
  2289. config->supported_interfaces);
  2290. config->mac_capabilities |= MAC_2500FD;
  2291. break;
  2292. }
  2293. }
  2294. static void mt7988_mac_port_get_caps(struct dsa_switch *ds, int port,
  2295. struct phylink_config *config)
  2296. {
  2297. switch (port) {
  2298. /* Ports which are connected to switch PHYs. There is no MII pinout. */
  2299. case 0 ... 3:
  2300. __set_bit(PHY_INTERFACE_MODE_INTERNAL,
  2301. config->supported_interfaces);
  2302. config->mac_capabilities |= MAC_10 | MAC_100 | MAC_1000FD;
  2303. break;
  2304. /* Port 6 is connected to SoC's XGMII MAC. There is no MII pinout. */
  2305. case 6:
  2306. __set_bit(PHY_INTERFACE_MODE_INTERNAL,
  2307. config->supported_interfaces);
  2308. config->mac_capabilities |= MAC_10000FD;
  2309. break;
  2310. }
  2311. }
  2312. static void en7581_mac_port_get_caps(struct dsa_switch *ds, int port,
  2313. struct phylink_config *config)
  2314. {
  2315. switch (port) {
  2316. /* Ports which are connected to switch PHYs. There is no MII pinout. */
  2317. case 0 ... 4:
  2318. __set_bit(PHY_INTERFACE_MODE_INTERNAL,
  2319. config->supported_interfaces);
  2320. config->mac_capabilities |= MAC_10 | MAC_100 | MAC_1000FD;
  2321. break;
  2322. /* Port 6 is connected to SoC's XGMII MAC. There is no MII pinout. */
  2323. case 6:
  2324. __set_bit(PHY_INTERFACE_MODE_INTERNAL,
  2325. config->supported_interfaces);
  2326. config->mac_capabilities |= MAC_10000FD;
  2327. break;
  2328. }
  2329. }
  2330. static void
  2331. mt7530_mac_config(struct dsa_switch *ds, int port, unsigned int mode,
  2332. phy_interface_t interface)
  2333. {
  2334. struct mt7530_priv *priv = ds->priv;
  2335. if (port == 5)
  2336. mt7530_setup_port5(priv->ds, interface);
  2337. else if (port == 6)
  2338. mt7530_setup_port6(priv->ds, interface);
  2339. }
  2340. static void mt7531_rgmii_setup(struct mt7530_priv *priv,
  2341. phy_interface_t interface,
  2342. struct phy_device *phydev)
  2343. {
  2344. u32 val;
  2345. val = mt7530_read(priv, MT7531_CLKGEN_CTRL);
  2346. val |= GP_CLK_EN;
  2347. val &= ~GP_MODE_MASK;
  2348. val |= GP_MODE(MT7531_GP_MODE_RGMII);
  2349. val &= ~CLK_SKEW_IN_MASK;
  2350. val |= CLK_SKEW_IN(MT7531_CLK_SKEW_NO_CHG);
  2351. val &= ~CLK_SKEW_OUT_MASK;
  2352. val |= CLK_SKEW_OUT(MT7531_CLK_SKEW_NO_CHG);
  2353. val |= TXCLK_NO_REVERSE | RXCLK_NO_DELAY;
  2354. /* Do not adjust rgmii delay when vendor phy driver presents. */
  2355. if (!phydev || phy_driver_is_genphy(phydev)) {
  2356. val &= ~(TXCLK_NO_REVERSE | RXCLK_NO_DELAY);
  2357. switch (interface) {
  2358. case PHY_INTERFACE_MODE_RGMII:
  2359. val |= TXCLK_NO_REVERSE;
  2360. val |= RXCLK_NO_DELAY;
  2361. break;
  2362. case PHY_INTERFACE_MODE_RGMII_RXID:
  2363. val |= TXCLK_NO_REVERSE;
  2364. break;
  2365. case PHY_INTERFACE_MODE_RGMII_TXID:
  2366. val |= RXCLK_NO_DELAY;
  2367. break;
  2368. case PHY_INTERFACE_MODE_RGMII_ID:
  2369. break;
  2370. default:
  2371. break;
  2372. }
  2373. }
  2374. mt7530_write(priv, MT7531_CLKGEN_CTRL, val);
  2375. }
  2376. static void
  2377. mt7531_mac_config(struct dsa_switch *ds, int port, unsigned int mode,
  2378. phy_interface_t interface)
  2379. {
  2380. struct mt7530_priv *priv = ds->priv;
  2381. struct phy_device *phydev;
  2382. struct dsa_port *dp;
  2383. if (phy_interface_mode_is_rgmii(interface)) {
  2384. dp = dsa_to_port(ds, port);
  2385. phydev = dp->user->phydev;
  2386. mt7531_rgmii_setup(priv, interface, phydev);
  2387. }
  2388. }
  2389. static struct phylink_pcs *
  2390. mt753x_phylink_mac_select_pcs(struct phylink_config *config,
  2391. phy_interface_t interface)
  2392. {
  2393. struct dsa_port *dp = dsa_phylink_to_port(config);
  2394. struct mt7530_priv *priv = dp->ds->priv;
  2395. switch (interface) {
  2396. case PHY_INTERFACE_MODE_TRGMII:
  2397. return &priv->pcs[dp->index].pcs;
  2398. case PHY_INTERFACE_MODE_SGMII:
  2399. case PHY_INTERFACE_MODE_1000BASEX:
  2400. case PHY_INTERFACE_MODE_2500BASEX:
  2401. return priv->ports[dp->index].sgmii_pcs;
  2402. default:
  2403. return NULL;
  2404. }
  2405. }
  2406. static void
  2407. mt753x_phylink_mac_config(struct phylink_config *config, unsigned int mode,
  2408. const struct phylink_link_state *state)
  2409. {
  2410. struct dsa_port *dp = dsa_phylink_to_port(config);
  2411. struct dsa_switch *ds = dp->ds;
  2412. struct mt7530_priv *priv;
  2413. int port = dp->index;
  2414. priv = ds->priv;
  2415. if ((port == 5 || port == 6) && priv->info->mac_port_config)
  2416. priv->info->mac_port_config(ds, port, mode, state->interface);
  2417. /* Are we connected to external phy */
  2418. if (port == 5 && dsa_is_user_port(ds, 5))
  2419. mt7530_set(priv, MT753X_PMCR_P(port), PMCR_EXT_PHY);
  2420. }
  2421. static void mt753x_phylink_mac_link_down(struct phylink_config *config,
  2422. unsigned int mode,
  2423. phy_interface_t interface)
  2424. {
  2425. struct dsa_port *dp = dsa_phylink_to_port(config);
  2426. struct mt7530_priv *priv = dp->ds->priv;
  2427. mt7530_clear(priv, MT753X_PMCR_P(dp->index), PMCR_LINK_SETTINGS_MASK);
  2428. }
  2429. static void mt753x_phylink_mac_link_up(struct phylink_config *config,
  2430. struct phy_device *phydev,
  2431. unsigned int mode,
  2432. phy_interface_t interface,
  2433. int speed, int duplex,
  2434. bool tx_pause, bool rx_pause)
  2435. {
  2436. struct dsa_port *dp = dsa_phylink_to_port(config);
  2437. struct mt7530_priv *priv = dp->ds->priv;
  2438. u32 mcr;
  2439. mcr = PMCR_MAC_RX_EN | PMCR_MAC_TX_EN | PMCR_FORCE_LNK;
  2440. switch (speed) {
  2441. case SPEED_1000:
  2442. case SPEED_2500:
  2443. case SPEED_10000:
  2444. mcr |= PMCR_FORCE_SPEED_1000;
  2445. break;
  2446. case SPEED_100:
  2447. mcr |= PMCR_FORCE_SPEED_100;
  2448. break;
  2449. }
  2450. if (duplex == DUPLEX_FULL) {
  2451. mcr |= PMCR_FORCE_FDX;
  2452. if (tx_pause)
  2453. mcr |= PMCR_FORCE_TX_FC_EN;
  2454. if (rx_pause)
  2455. mcr |= PMCR_FORCE_RX_FC_EN;
  2456. }
  2457. if (mode == MLO_AN_PHY && phydev && phy_init_eee(phydev, false) >= 0) {
  2458. switch (speed) {
  2459. case SPEED_1000:
  2460. case SPEED_2500:
  2461. mcr |= PMCR_FORCE_EEE1G;
  2462. break;
  2463. case SPEED_100:
  2464. mcr |= PMCR_FORCE_EEE100;
  2465. break;
  2466. }
  2467. }
  2468. mt7530_set(priv, MT753X_PMCR_P(dp->index), mcr);
  2469. }
  2470. static void mt753x_phylink_get_caps(struct dsa_switch *ds, int port,
  2471. struct phylink_config *config)
  2472. {
  2473. struct mt7530_priv *priv = ds->priv;
  2474. config->mac_capabilities = MAC_ASYM_PAUSE | MAC_SYM_PAUSE;
  2475. priv->info->mac_port_get_caps(ds, port, config);
  2476. }
  2477. static int mt753x_pcs_validate(struct phylink_pcs *pcs,
  2478. unsigned long *supported,
  2479. const struct phylink_link_state *state)
  2480. {
  2481. /* Autonegotiation is not supported in TRGMII nor 802.3z modes */
  2482. if (state->interface == PHY_INTERFACE_MODE_TRGMII ||
  2483. phy_interface_mode_is_8023z(state->interface))
  2484. phylink_clear(supported, Autoneg);
  2485. return 0;
  2486. }
  2487. static void mt7530_pcs_get_state(struct phylink_pcs *pcs,
  2488. struct phylink_link_state *state)
  2489. {
  2490. struct mt7530_priv *priv = pcs_to_mt753x_pcs(pcs)->priv;
  2491. int port = pcs_to_mt753x_pcs(pcs)->port;
  2492. u32 pmsr;
  2493. pmsr = mt7530_read(priv, MT7530_PMSR_P(port));
  2494. state->link = (pmsr & PMSR_LINK);
  2495. state->an_complete = state->link;
  2496. state->duplex = !!(pmsr & PMSR_DPX);
  2497. switch (pmsr & PMSR_SPEED_MASK) {
  2498. case PMSR_SPEED_10:
  2499. state->speed = SPEED_10;
  2500. break;
  2501. case PMSR_SPEED_100:
  2502. state->speed = SPEED_100;
  2503. break;
  2504. case PMSR_SPEED_1000:
  2505. state->speed = SPEED_1000;
  2506. break;
  2507. default:
  2508. state->speed = SPEED_UNKNOWN;
  2509. break;
  2510. }
  2511. state->pause &= ~(MLO_PAUSE_RX | MLO_PAUSE_TX);
  2512. if (pmsr & PMSR_RX_FC)
  2513. state->pause |= MLO_PAUSE_RX;
  2514. if (pmsr & PMSR_TX_FC)
  2515. state->pause |= MLO_PAUSE_TX;
  2516. }
  2517. static int mt753x_pcs_config(struct phylink_pcs *pcs, unsigned int neg_mode,
  2518. phy_interface_t interface,
  2519. const unsigned long *advertising,
  2520. bool permit_pause_to_mac)
  2521. {
  2522. return 0;
  2523. }
  2524. static void mt7530_pcs_an_restart(struct phylink_pcs *pcs)
  2525. {
  2526. }
  2527. static const struct phylink_pcs_ops mt7530_pcs_ops = {
  2528. .pcs_validate = mt753x_pcs_validate,
  2529. .pcs_get_state = mt7530_pcs_get_state,
  2530. .pcs_config = mt753x_pcs_config,
  2531. .pcs_an_restart = mt7530_pcs_an_restart,
  2532. };
  2533. static int
  2534. mt753x_setup(struct dsa_switch *ds)
  2535. {
  2536. struct mt7530_priv *priv = ds->priv;
  2537. int ret = priv->info->sw_setup(ds);
  2538. int i;
  2539. if (ret)
  2540. return ret;
  2541. ret = mt7530_setup_irq(priv);
  2542. if (ret)
  2543. return ret;
  2544. ret = mt7530_setup_mdio(priv);
  2545. if (ret && priv->irq)
  2546. mt7530_free_irq_common(priv);
  2547. if (ret)
  2548. return ret;
  2549. /* Initialise the PCS devices */
  2550. for (i = 0; i < priv->ds->num_ports; i++) {
  2551. priv->pcs[i].pcs.ops = priv->info->pcs_ops;
  2552. priv->pcs[i].pcs.neg_mode = true;
  2553. priv->pcs[i].priv = priv;
  2554. priv->pcs[i].port = i;
  2555. }
  2556. if (priv->create_sgmii) {
  2557. ret = priv->create_sgmii(priv);
  2558. if (ret && priv->irq)
  2559. mt7530_free_irq(priv);
  2560. }
  2561. return ret;
  2562. }
  2563. static int mt753x_get_mac_eee(struct dsa_switch *ds, int port,
  2564. struct ethtool_keee *e)
  2565. {
  2566. struct mt7530_priv *priv = ds->priv;
  2567. u32 eeecr = mt7530_read(priv, MT753X_PMEEECR_P(port));
  2568. e->tx_lpi_enabled = !(eeecr & LPI_MODE_EN);
  2569. e->tx_lpi_timer = LPI_THRESH_GET(eeecr);
  2570. return 0;
  2571. }
  2572. static int mt753x_set_mac_eee(struct dsa_switch *ds, int port,
  2573. struct ethtool_keee *e)
  2574. {
  2575. struct mt7530_priv *priv = ds->priv;
  2576. u32 set, mask = LPI_THRESH_MASK | LPI_MODE_EN;
  2577. if (e->tx_lpi_timer > 0xFFF)
  2578. return -EINVAL;
  2579. set = LPI_THRESH_SET(e->tx_lpi_timer);
  2580. if (!e->tx_lpi_enabled)
  2581. /* Force LPI Mode without a delay */
  2582. set |= LPI_MODE_EN;
  2583. mt7530_rmw(priv, MT753X_PMEEECR_P(port), mask, set);
  2584. return 0;
  2585. }
  2586. static void
  2587. mt753x_conduit_state_change(struct dsa_switch *ds,
  2588. const struct net_device *conduit,
  2589. bool operational)
  2590. {
  2591. struct dsa_port *cpu_dp = conduit->dsa_ptr;
  2592. struct mt7530_priv *priv = ds->priv;
  2593. int val = 0;
  2594. u8 mask;
  2595. /* Set the CPU port to trap frames to for MT7530. Trapped frames will be
  2596. * forwarded to the numerically smallest CPU port whose conduit
  2597. * interface is up.
  2598. */
  2599. if (priv->id != ID_MT7530 && priv->id != ID_MT7621)
  2600. return;
  2601. mask = BIT(cpu_dp->index);
  2602. if (operational)
  2603. priv->active_cpu_ports |= mask;
  2604. else
  2605. priv->active_cpu_ports &= ~mask;
  2606. if (priv->active_cpu_ports) {
  2607. val = MT7530_CPU_EN |
  2608. MT7530_CPU_PORT(__ffs(priv->active_cpu_ports));
  2609. }
  2610. mt7530_rmw(priv, MT753X_MFC, MT7530_CPU_EN | MT7530_CPU_PORT_MASK, val);
  2611. }
  2612. static int mt7988_setup(struct dsa_switch *ds)
  2613. {
  2614. struct mt7530_priv *priv = ds->priv;
  2615. /* Reset the switch */
  2616. reset_control_assert(priv->rstc);
  2617. usleep_range(20, 50);
  2618. reset_control_deassert(priv->rstc);
  2619. usleep_range(20, 50);
  2620. /* Reset the switch PHYs */
  2621. mt7530_write(priv, MT7530_SYS_CTRL, SYS_CTRL_PHY_RST);
  2622. return mt7531_setup_common(ds);
  2623. }
  2624. const struct dsa_switch_ops mt7530_switch_ops = {
  2625. .get_tag_protocol = mtk_get_tag_protocol,
  2626. .setup = mt753x_setup,
  2627. .preferred_default_local_cpu_port = mt753x_preferred_default_local_cpu_port,
  2628. .get_strings = mt7530_get_strings,
  2629. .get_ethtool_stats = mt7530_get_ethtool_stats,
  2630. .get_sset_count = mt7530_get_sset_count,
  2631. .set_ageing_time = mt7530_set_ageing_time,
  2632. .port_enable = mt7530_port_enable,
  2633. .port_disable = mt7530_port_disable,
  2634. .port_change_mtu = mt7530_port_change_mtu,
  2635. .port_max_mtu = mt7530_port_max_mtu,
  2636. .port_stp_state_set = mt7530_stp_state_set,
  2637. .port_pre_bridge_flags = mt7530_port_pre_bridge_flags,
  2638. .port_bridge_flags = mt7530_port_bridge_flags,
  2639. .port_bridge_join = mt7530_port_bridge_join,
  2640. .port_bridge_leave = mt7530_port_bridge_leave,
  2641. .port_fdb_add = mt7530_port_fdb_add,
  2642. .port_fdb_del = mt7530_port_fdb_del,
  2643. .port_fdb_dump = mt7530_port_fdb_dump,
  2644. .port_mdb_add = mt7530_port_mdb_add,
  2645. .port_mdb_del = mt7530_port_mdb_del,
  2646. .port_vlan_filtering = mt7530_port_vlan_filtering,
  2647. .port_vlan_add = mt7530_port_vlan_add,
  2648. .port_vlan_del = mt7530_port_vlan_del,
  2649. .port_mirror_add = mt753x_port_mirror_add,
  2650. .port_mirror_del = mt753x_port_mirror_del,
  2651. .phylink_get_caps = mt753x_phylink_get_caps,
  2652. .get_mac_eee = mt753x_get_mac_eee,
  2653. .set_mac_eee = mt753x_set_mac_eee,
  2654. .conduit_state_change = mt753x_conduit_state_change,
  2655. };
  2656. EXPORT_SYMBOL_GPL(mt7530_switch_ops);
  2657. static const struct phylink_mac_ops mt753x_phylink_mac_ops = {
  2658. .mac_select_pcs = mt753x_phylink_mac_select_pcs,
  2659. .mac_config = mt753x_phylink_mac_config,
  2660. .mac_link_down = mt753x_phylink_mac_link_down,
  2661. .mac_link_up = mt753x_phylink_mac_link_up,
  2662. };
  2663. const struct mt753x_info mt753x_table[] = {
  2664. [ID_MT7621] = {
  2665. .id = ID_MT7621,
  2666. .pcs_ops = &mt7530_pcs_ops,
  2667. .sw_setup = mt7530_setup,
  2668. .phy_read_c22 = mt7530_phy_read_c22,
  2669. .phy_write_c22 = mt7530_phy_write_c22,
  2670. .phy_read_c45 = mt7530_phy_read_c45,
  2671. .phy_write_c45 = mt7530_phy_write_c45,
  2672. .mac_port_get_caps = mt7530_mac_port_get_caps,
  2673. .mac_port_config = mt7530_mac_config,
  2674. },
  2675. [ID_MT7530] = {
  2676. .id = ID_MT7530,
  2677. .pcs_ops = &mt7530_pcs_ops,
  2678. .sw_setup = mt7530_setup,
  2679. .phy_read_c22 = mt7530_phy_read_c22,
  2680. .phy_write_c22 = mt7530_phy_write_c22,
  2681. .phy_read_c45 = mt7530_phy_read_c45,
  2682. .phy_write_c45 = mt7530_phy_write_c45,
  2683. .mac_port_get_caps = mt7530_mac_port_get_caps,
  2684. .mac_port_config = mt7530_mac_config,
  2685. },
  2686. [ID_MT7531] = {
  2687. .id = ID_MT7531,
  2688. .pcs_ops = &mt7530_pcs_ops,
  2689. .sw_setup = mt7531_setup,
  2690. .phy_read_c22 = mt7531_ind_c22_phy_read,
  2691. .phy_write_c22 = mt7531_ind_c22_phy_write,
  2692. .phy_read_c45 = mt7531_ind_c45_phy_read,
  2693. .phy_write_c45 = mt7531_ind_c45_phy_write,
  2694. .mac_port_get_caps = mt7531_mac_port_get_caps,
  2695. .mac_port_config = mt7531_mac_config,
  2696. },
  2697. [ID_MT7988] = {
  2698. .id = ID_MT7988,
  2699. .pcs_ops = &mt7530_pcs_ops,
  2700. .sw_setup = mt7988_setup,
  2701. .phy_read_c22 = mt7531_ind_c22_phy_read,
  2702. .phy_write_c22 = mt7531_ind_c22_phy_write,
  2703. .phy_read_c45 = mt7531_ind_c45_phy_read,
  2704. .phy_write_c45 = mt7531_ind_c45_phy_write,
  2705. .mac_port_get_caps = mt7988_mac_port_get_caps,
  2706. },
  2707. [ID_EN7581] = {
  2708. .id = ID_EN7581,
  2709. .pcs_ops = &mt7530_pcs_ops,
  2710. .sw_setup = mt7988_setup,
  2711. .phy_read_c22 = mt7531_ind_c22_phy_read,
  2712. .phy_write_c22 = mt7531_ind_c22_phy_write,
  2713. .phy_read_c45 = mt7531_ind_c45_phy_read,
  2714. .phy_write_c45 = mt7531_ind_c45_phy_write,
  2715. .mac_port_get_caps = en7581_mac_port_get_caps,
  2716. },
  2717. };
  2718. EXPORT_SYMBOL_GPL(mt753x_table);
  2719. int
  2720. mt7530_probe_common(struct mt7530_priv *priv)
  2721. {
  2722. struct device *dev = priv->dev;
  2723. priv->ds = devm_kzalloc(dev, sizeof(*priv->ds), GFP_KERNEL);
  2724. if (!priv->ds)
  2725. return -ENOMEM;
  2726. priv->ds->dev = dev;
  2727. priv->ds->num_ports = MT7530_NUM_PORTS;
  2728. /* Get the hardware identifier from the devicetree node.
  2729. * We will need it for some of the clock and regulator setup.
  2730. */
  2731. priv->info = of_device_get_match_data(dev);
  2732. if (!priv->info)
  2733. return -EINVAL;
  2734. priv->id = priv->info->id;
  2735. priv->dev = dev;
  2736. priv->ds->priv = priv;
  2737. priv->ds->ops = &mt7530_switch_ops;
  2738. priv->ds->phylink_mac_ops = &mt753x_phylink_mac_ops;
  2739. mutex_init(&priv->reg_mutex);
  2740. dev_set_drvdata(dev, priv);
  2741. return 0;
  2742. }
  2743. EXPORT_SYMBOL_GPL(mt7530_probe_common);
  2744. void
  2745. mt7530_remove_common(struct mt7530_priv *priv)
  2746. {
  2747. if (priv->irq)
  2748. mt7530_free_irq(priv);
  2749. dsa_unregister_switch(priv->ds);
  2750. mutex_destroy(&priv->reg_mutex);
  2751. }
  2752. EXPORT_SYMBOL_GPL(mt7530_remove_common);
  2753. MODULE_AUTHOR("Sean Wang <sean.wang@mediatek.com>");
  2754. MODULE_DESCRIPTION("Driver for Mediatek MT7530 Switch");
  2755. MODULE_LICENSE("GPL");