mt7530.c 85 KB

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