mt7530.c 95 KB

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