mt7530.c 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Mediatek MT7530 DSA Switch driver
  4. * Copyright (C) 2017 Sean Wang <sean.wang@mediatek.com>
  5. */
  6. #include <linux/etherdevice.h>
  7. #include <linux/if_bridge.h>
  8. #include <linux/iopoll.h>
  9. #include <linux/mdio.h>
  10. #include <linux/mfd/syscon.h>
  11. #include <linux/module.h>
  12. #include <linux/netdevice.h>
  13. #include <linux/of_mdio.h>
  14. #include <linux/of_net.h>
  15. #include <linux/of_platform.h>
  16. #include <linux/phylink.h>
  17. #include <linux/regmap.h>
  18. #include <linux/regulator/consumer.h>
  19. #include <linux/reset.h>
  20. #include <linux/gpio/consumer.h>
  21. #include <net/dsa.h>
  22. #include "mt7530.h"
  23. /* String, offset, and register size in bytes if different from 4 bytes */
  24. static const struct mt7530_mib_desc mt7530_mib[] = {
  25. MIB_DESC(1, 0x00, "TxDrop"),
  26. MIB_DESC(1, 0x04, "TxCrcErr"),
  27. MIB_DESC(1, 0x08, "TxUnicast"),
  28. MIB_DESC(1, 0x0c, "TxMulticast"),
  29. MIB_DESC(1, 0x10, "TxBroadcast"),
  30. MIB_DESC(1, 0x14, "TxCollision"),
  31. MIB_DESC(1, 0x18, "TxSingleCollision"),
  32. MIB_DESC(1, 0x1c, "TxMultipleCollision"),
  33. MIB_DESC(1, 0x20, "TxDeferred"),
  34. MIB_DESC(1, 0x24, "TxLateCollision"),
  35. MIB_DESC(1, 0x28, "TxExcessiveCollistion"),
  36. MIB_DESC(1, 0x2c, "TxPause"),
  37. MIB_DESC(1, 0x30, "TxPktSz64"),
  38. MIB_DESC(1, 0x34, "TxPktSz65To127"),
  39. MIB_DESC(1, 0x38, "TxPktSz128To255"),
  40. MIB_DESC(1, 0x3c, "TxPktSz256To511"),
  41. MIB_DESC(1, 0x40, "TxPktSz512To1023"),
  42. MIB_DESC(1, 0x44, "Tx1024ToMax"),
  43. MIB_DESC(2, 0x48, "TxBytes"),
  44. MIB_DESC(1, 0x60, "RxDrop"),
  45. MIB_DESC(1, 0x64, "RxFiltering"),
  46. MIB_DESC(1, 0x68, "RxUnicast"),
  47. MIB_DESC(1, 0x6c, "RxMulticast"),
  48. MIB_DESC(1, 0x70, "RxBroadcast"),
  49. MIB_DESC(1, 0x74, "RxAlignErr"),
  50. MIB_DESC(1, 0x78, "RxCrcErr"),
  51. MIB_DESC(1, 0x7c, "RxUnderSizeErr"),
  52. MIB_DESC(1, 0x80, "RxFragErr"),
  53. MIB_DESC(1, 0x84, "RxOverSzErr"),
  54. MIB_DESC(1, 0x88, "RxJabberErr"),
  55. MIB_DESC(1, 0x8c, "RxPause"),
  56. MIB_DESC(1, 0x90, "RxPktSz64"),
  57. MIB_DESC(1, 0x94, "RxPktSz65To127"),
  58. MIB_DESC(1, 0x98, "RxPktSz128To255"),
  59. MIB_DESC(1, 0x9c, "RxPktSz256To511"),
  60. MIB_DESC(1, 0xa0, "RxPktSz512To1023"),
  61. MIB_DESC(1, 0xa4, "RxPktSz1024ToMax"),
  62. MIB_DESC(2, 0xa8, "RxBytes"),
  63. MIB_DESC(1, 0xb0, "RxCtrlDrop"),
  64. MIB_DESC(1, 0xb4, "RxIngressDrop"),
  65. MIB_DESC(1, 0xb8, "RxArlDrop"),
  66. };
  67. static int
  68. core_read_mmd_indirect(struct mt7530_priv *priv, int prtad, int devad)
  69. {
  70. struct mii_bus *bus = priv->bus;
  71. int value, ret;
  72. /* Write the desired MMD Devad */
  73. ret = bus->write(bus, 0, MII_MMD_CTRL, devad);
  74. if (ret < 0)
  75. goto err;
  76. /* Write the desired MMD register address */
  77. ret = bus->write(bus, 0, MII_MMD_DATA, prtad);
  78. if (ret < 0)
  79. goto err;
  80. /* Select the Function : DATA with no post increment */
  81. ret = bus->write(bus, 0, MII_MMD_CTRL, (devad | MII_MMD_CTRL_NOINCR));
  82. if (ret < 0)
  83. goto err;
  84. /* Read the content of the MMD's selected register */
  85. value = bus->read(bus, 0, MII_MMD_DATA);
  86. return value;
  87. err:
  88. dev_err(&bus->dev, "failed to read mmd register\n");
  89. return ret;
  90. }
  91. static int
  92. core_write_mmd_indirect(struct mt7530_priv *priv, int prtad,
  93. int devad, u32 data)
  94. {
  95. struct mii_bus *bus = priv->bus;
  96. int ret;
  97. /* Write the desired MMD Devad */
  98. ret = bus->write(bus, 0, MII_MMD_CTRL, devad);
  99. if (ret < 0)
  100. goto err;
  101. /* Write the desired MMD register address */
  102. ret = bus->write(bus, 0, MII_MMD_DATA, prtad);
  103. if (ret < 0)
  104. goto err;
  105. /* Select the Function : DATA with no post increment */
  106. ret = bus->write(bus, 0, MII_MMD_CTRL, (devad | MII_MMD_CTRL_NOINCR));
  107. if (ret < 0)
  108. goto err;
  109. /* Write the data into MMD's selected register */
  110. ret = bus->write(bus, 0, MII_MMD_DATA, data);
  111. err:
  112. if (ret < 0)
  113. dev_err(&bus->dev,
  114. "failed to write mmd register\n");
  115. return ret;
  116. }
  117. static void
  118. core_write(struct mt7530_priv *priv, u32 reg, u32 val)
  119. {
  120. struct mii_bus *bus = priv->bus;
  121. mutex_lock_nested(&bus->mdio_lock, MDIO_MUTEX_NESTED);
  122. core_write_mmd_indirect(priv, reg, MDIO_MMD_VEND2, val);
  123. mutex_unlock(&bus->mdio_lock);
  124. }
  125. static void
  126. core_rmw(struct mt7530_priv *priv, u32 reg, u32 mask, u32 set)
  127. {
  128. struct mii_bus *bus = priv->bus;
  129. u32 val;
  130. mutex_lock_nested(&bus->mdio_lock, MDIO_MUTEX_NESTED);
  131. val = core_read_mmd_indirect(priv, reg, MDIO_MMD_VEND2);
  132. val &= ~mask;
  133. val |= set;
  134. core_write_mmd_indirect(priv, reg, MDIO_MMD_VEND2, val);
  135. mutex_unlock(&bus->mdio_lock);
  136. }
  137. static void
  138. core_set(struct mt7530_priv *priv, u32 reg, u32 val)
  139. {
  140. core_rmw(priv, reg, 0, val);
  141. }
  142. static void
  143. core_clear(struct mt7530_priv *priv, u32 reg, u32 val)
  144. {
  145. core_rmw(priv, reg, val, 0);
  146. }
  147. static int
  148. mt7530_mii_write(struct mt7530_priv *priv, u32 reg, u32 val)
  149. {
  150. struct mii_bus *bus = priv->bus;
  151. u16 page, r, lo, hi;
  152. int ret;
  153. page = (reg >> 6) & 0x3ff;
  154. r = (reg >> 2) & 0xf;
  155. lo = val & 0xffff;
  156. hi = val >> 16;
  157. /* MT7530 uses 31 as the pseudo port */
  158. ret = bus->write(bus, 0x1f, 0x1f, page);
  159. if (ret < 0)
  160. goto err;
  161. ret = bus->write(bus, 0x1f, r, lo);
  162. if (ret < 0)
  163. goto err;
  164. ret = bus->write(bus, 0x1f, 0x10, hi);
  165. err:
  166. if (ret < 0)
  167. dev_err(&bus->dev,
  168. "failed to write mt7530 register\n");
  169. return ret;
  170. }
  171. static u32
  172. mt7530_mii_read(struct mt7530_priv *priv, u32 reg)
  173. {
  174. struct mii_bus *bus = priv->bus;
  175. u16 page, r, lo, hi;
  176. int ret;
  177. page = (reg >> 6) & 0x3ff;
  178. r = (reg >> 2) & 0xf;
  179. /* MT7530 uses 31 as the pseudo port */
  180. ret = bus->write(bus, 0x1f, 0x1f, page);
  181. if (ret < 0) {
  182. dev_err(&bus->dev,
  183. "failed to read mt7530 register\n");
  184. return ret;
  185. }
  186. lo = bus->read(bus, 0x1f, r);
  187. hi = bus->read(bus, 0x1f, 0x10);
  188. return (hi << 16) | (lo & 0xffff);
  189. }
  190. static void
  191. mt7530_write(struct mt7530_priv *priv, u32 reg, u32 val)
  192. {
  193. struct mii_bus *bus = priv->bus;
  194. mutex_lock_nested(&bus->mdio_lock, MDIO_MUTEX_NESTED);
  195. mt7530_mii_write(priv, reg, val);
  196. mutex_unlock(&bus->mdio_lock);
  197. }
  198. static u32
  199. _mt7530_read(struct mt7530_dummy_poll *p)
  200. {
  201. struct mii_bus *bus = p->priv->bus;
  202. u32 val;
  203. mutex_lock_nested(&bus->mdio_lock, MDIO_MUTEX_NESTED);
  204. val = mt7530_mii_read(p->priv, p->reg);
  205. mutex_unlock(&bus->mdio_lock);
  206. return val;
  207. }
  208. static u32
  209. mt7530_read(struct mt7530_priv *priv, u32 reg)
  210. {
  211. struct mt7530_dummy_poll p;
  212. INIT_MT7530_DUMMY_POLL(&p, priv, reg);
  213. return _mt7530_read(&p);
  214. }
  215. static void
  216. mt7530_rmw(struct mt7530_priv *priv, u32 reg,
  217. u32 mask, u32 set)
  218. {
  219. struct mii_bus *bus = priv->bus;
  220. u32 val;
  221. mutex_lock_nested(&bus->mdio_lock, MDIO_MUTEX_NESTED);
  222. val = mt7530_mii_read(priv, reg);
  223. val &= ~mask;
  224. val |= set;
  225. mt7530_mii_write(priv, reg, val);
  226. mutex_unlock(&bus->mdio_lock);
  227. }
  228. static void
  229. mt7530_set(struct mt7530_priv *priv, u32 reg, u32 val)
  230. {
  231. mt7530_rmw(priv, reg, 0, val);
  232. }
  233. static void
  234. mt7530_clear(struct mt7530_priv *priv, u32 reg, u32 val)
  235. {
  236. mt7530_rmw(priv, reg, val, 0);
  237. }
  238. static int
  239. mt7530_fdb_cmd(struct mt7530_priv *priv, enum mt7530_fdb_cmd cmd, u32 *rsp)
  240. {
  241. u32 val;
  242. int ret;
  243. struct mt7530_dummy_poll p;
  244. /* Set the command operating upon the MAC address entries */
  245. val = ATC_BUSY | ATC_MAT(0) | cmd;
  246. mt7530_write(priv, MT7530_ATC, val);
  247. INIT_MT7530_DUMMY_POLL(&p, priv, MT7530_ATC);
  248. ret = readx_poll_timeout(_mt7530_read, &p, val,
  249. !(val & ATC_BUSY), 20, 20000);
  250. if (ret < 0) {
  251. dev_err(priv->dev, "reset timeout\n");
  252. return ret;
  253. }
  254. /* Additional sanity for read command if the specified
  255. * entry is invalid
  256. */
  257. val = mt7530_read(priv, MT7530_ATC);
  258. if ((cmd == MT7530_FDB_READ) && (val & ATC_INVALID))
  259. return -EINVAL;
  260. if (rsp)
  261. *rsp = val;
  262. return 0;
  263. }
  264. static void
  265. mt7530_fdb_read(struct mt7530_priv *priv, struct mt7530_fdb *fdb)
  266. {
  267. u32 reg[3];
  268. int i;
  269. /* Read from ARL table into an array */
  270. for (i = 0; i < 3; i++) {
  271. reg[i] = mt7530_read(priv, MT7530_TSRA1 + (i * 4));
  272. dev_dbg(priv->dev, "%s(%d) reg[%d]=0x%x\n",
  273. __func__, __LINE__, i, reg[i]);
  274. }
  275. fdb->vid = (reg[1] >> CVID) & CVID_MASK;
  276. fdb->aging = (reg[2] >> AGE_TIMER) & AGE_TIMER_MASK;
  277. fdb->port_mask = (reg[2] >> PORT_MAP) & PORT_MAP_MASK;
  278. fdb->mac[0] = (reg[0] >> MAC_BYTE_0) & MAC_BYTE_MASK;
  279. fdb->mac[1] = (reg[0] >> MAC_BYTE_1) & MAC_BYTE_MASK;
  280. fdb->mac[2] = (reg[0] >> MAC_BYTE_2) & MAC_BYTE_MASK;
  281. fdb->mac[3] = (reg[0] >> MAC_BYTE_3) & MAC_BYTE_MASK;
  282. fdb->mac[4] = (reg[1] >> MAC_BYTE_4) & MAC_BYTE_MASK;
  283. fdb->mac[5] = (reg[1] >> MAC_BYTE_5) & MAC_BYTE_MASK;
  284. fdb->noarp = ((reg[2] >> ENT_STATUS) & ENT_STATUS_MASK) == STATIC_ENT;
  285. }
  286. static void
  287. mt7530_fdb_write(struct mt7530_priv *priv, u16 vid,
  288. u8 port_mask, const u8 *mac,
  289. u8 aging, u8 type)
  290. {
  291. u32 reg[3] = { 0 };
  292. int i;
  293. reg[1] |= vid & CVID_MASK;
  294. reg[2] |= (aging & AGE_TIMER_MASK) << AGE_TIMER;
  295. reg[2] |= (port_mask & PORT_MAP_MASK) << PORT_MAP;
  296. /* STATIC_ENT indicate that entry is static wouldn't
  297. * be aged out and STATIC_EMP specified as erasing an
  298. * entry
  299. */
  300. reg[2] |= (type & ENT_STATUS_MASK) << ENT_STATUS;
  301. reg[1] |= mac[5] << MAC_BYTE_5;
  302. reg[1] |= mac[4] << MAC_BYTE_4;
  303. reg[0] |= mac[3] << MAC_BYTE_3;
  304. reg[0] |= mac[2] << MAC_BYTE_2;
  305. reg[0] |= mac[1] << MAC_BYTE_1;
  306. reg[0] |= mac[0] << MAC_BYTE_0;
  307. /* Write array into the ARL table */
  308. for (i = 0; i < 3; i++)
  309. mt7530_write(priv, MT7530_ATA1 + (i * 4), reg[i]);
  310. }
  311. static int
  312. mt7530_pad_clk_setup(struct dsa_switch *ds, int mode)
  313. {
  314. struct mt7530_priv *priv = ds->priv;
  315. u32 ncpo1, ssc_delta, trgint, i, xtal;
  316. xtal = mt7530_read(priv, MT7530_MHWTRAP) & HWTRAP_XTAL_MASK;
  317. if (xtal == HWTRAP_XTAL_20MHZ) {
  318. dev_err(priv->dev,
  319. "%s: MT7530 with a 20MHz XTAL is not supported!\n",
  320. __func__);
  321. return -EINVAL;
  322. }
  323. switch (mode) {
  324. case PHY_INTERFACE_MODE_RGMII:
  325. trgint = 0;
  326. /* PLL frequency: 125MHz */
  327. ncpo1 = 0x0c80;
  328. break;
  329. case PHY_INTERFACE_MODE_TRGMII:
  330. trgint = 1;
  331. if (priv->id == ID_MT7621) {
  332. /* PLL frequency: 150MHz: 1.2GBit */
  333. if (xtal == HWTRAP_XTAL_40MHZ)
  334. ncpo1 = 0x0780;
  335. if (xtal == HWTRAP_XTAL_25MHZ)
  336. ncpo1 = 0x0a00;
  337. } else { /* PLL frequency: 250MHz: 2.0Gbit */
  338. if (xtal == HWTRAP_XTAL_40MHZ)
  339. ncpo1 = 0x0c80;
  340. if (xtal == HWTRAP_XTAL_25MHZ)
  341. ncpo1 = 0x1400;
  342. }
  343. break;
  344. default:
  345. dev_err(priv->dev, "xMII mode %d not supported\n", mode);
  346. return -EINVAL;
  347. }
  348. if (xtal == HWTRAP_XTAL_25MHZ)
  349. ssc_delta = 0x57;
  350. else
  351. ssc_delta = 0x87;
  352. mt7530_rmw(priv, MT7530_P6ECR, P6_INTF_MODE_MASK,
  353. P6_INTF_MODE(trgint));
  354. /* Lower Tx Driving for TRGMII path */
  355. for (i = 0 ; i < NUM_TRGMII_CTRL ; i++)
  356. mt7530_write(priv, MT7530_TRGMII_TD_ODT(i),
  357. TD_DM_DRVP(8) | TD_DM_DRVN(8));
  358. /* Setup core clock for MT7530 */
  359. if (!trgint) {
  360. /* Disable MT7530 core clock */
  361. core_clear(priv, CORE_TRGMII_GSW_CLK_CG, REG_GSWCK_EN);
  362. /* Disable PLL, since phy_device has not yet been created
  363. * provided for phy_[read,write]_mmd_indirect is called, we
  364. * provide our own core_write_mmd_indirect to complete this
  365. * function.
  366. */
  367. core_write_mmd_indirect(priv,
  368. CORE_GSWPLL_GRP1,
  369. MDIO_MMD_VEND2,
  370. 0);
  371. /* Set core clock into 500Mhz */
  372. core_write(priv, CORE_GSWPLL_GRP2,
  373. RG_GSWPLL_POSDIV_500M(1) |
  374. RG_GSWPLL_FBKDIV_500M(25));
  375. /* Enable PLL */
  376. core_write(priv, CORE_GSWPLL_GRP1,
  377. RG_GSWPLL_EN_PRE |
  378. RG_GSWPLL_POSDIV_200M(2) |
  379. RG_GSWPLL_FBKDIV_200M(32));
  380. /* Enable MT7530 core clock */
  381. core_set(priv, CORE_TRGMII_GSW_CLK_CG, REG_GSWCK_EN);
  382. }
  383. /* Setup the MT7530 TRGMII Tx Clock */
  384. core_set(priv, CORE_TRGMII_GSW_CLK_CG, REG_GSWCK_EN);
  385. core_write(priv, CORE_PLL_GROUP5, RG_LCDDS_PCW_NCPO1(ncpo1));
  386. core_write(priv, CORE_PLL_GROUP6, RG_LCDDS_PCW_NCPO0(0));
  387. core_write(priv, CORE_PLL_GROUP10, RG_LCDDS_SSC_DELTA(ssc_delta));
  388. core_write(priv, CORE_PLL_GROUP11, RG_LCDDS_SSC_DELTA1(ssc_delta));
  389. core_write(priv, CORE_PLL_GROUP4,
  390. RG_SYSPLL_DDSFBK_EN | RG_SYSPLL_BIAS_EN |
  391. RG_SYSPLL_BIAS_LPF_EN);
  392. core_write(priv, CORE_PLL_GROUP2,
  393. RG_SYSPLL_EN_NORMAL | RG_SYSPLL_VODEN |
  394. RG_SYSPLL_POSDIV(1));
  395. core_write(priv, CORE_PLL_GROUP7,
  396. RG_LCDDS_PCW_NCPO_CHG | RG_LCCDS_C(3) |
  397. RG_LCDDS_PWDB | RG_LCDDS_ISO_EN);
  398. core_set(priv, CORE_TRGMII_GSW_CLK_CG,
  399. REG_GSWCK_EN | REG_TRGMIICK_EN);
  400. if (!trgint)
  401. for (i = 0 ; i < NUM_TRGMII_CTRL; i++)
  402. mt7530_rmw(priv, MT7530_TRGMII_RD(i),
  403. RD_TAP_MASK, RD_TAP(16));
  404. return 0;
  405. }
  406. static void
  407. mt7530_mib_reset(struct dsa_switch *ds)
  408. {
  409. struct mt7530_priv *priv = ds->priv;
  410. mt7530_write(priv, MT7530_MIB_CCR, CCR_MIB_FLUSH);
  411. mt7530_write(priv, MT7530_MIB_CCR, CCR_MIB_ACTIVATE);
  412. }
  413. static void
  414. mt7530_port_set_status(struct mt7530_priv *priv, int port, int enable)
  415. {
  416. u32 mask = PMCR_TX_EN | PMCR_RX_EN | PMCR_FORCE_LNK;
  417. if (enable)
  418. mt7530_set(priv, MT7530_PMCR_P(port), mask);
  419. else
  420. mt7530_clear(priv, MT7530_PMCR_P(port), mask);
  421. }
  422. static int mt7530_phy_read(struct dsa_switch *ds, int port, int regnum)
  423. {
  424. struct mt7530_priv *priv = ds->priv;
  425. return mdiobus_read_nested(priv->bus, port, regnum);
  426. }
  427. static int mt7530_phy_write(struct dsa_switch *ds, int port, int regnum,
  428. u16 val)
  429. {
  430. struct mt7530_priv *priv = ds->priv;
  431. return mdiobus_write_nested(priv->bus, port, regnum, val);
  432. }
  433. static void
  434. mt7530_get_strings(struct dsa_switch *ds, int port, u32 stringset,
  435. uint8_t *data)
  436. {
  437. int i;
  438. if (stringset != ETH_SS_STATS)
  439. return;
  440. for (i = 0; i < ARRAY_SIZE(mt7530_mib); i++)
  441. strncpy(data + i * ETH_GSTRING_LEN, mt7530_mib[i].name,
  442. ETH_GSTRING_LEN);
  443. }
  444. static void
  445. mt7530_get_ethtool_stats(struct dsa_switch *ds, int port,
  446. uint64_t *data)
  447. {
  448. struct mt7530_priv *priv = ds->priv;
  449. const struct mt7530_mib_desc *mib;
  450. u32 reg, i;
  451. u64 hi;
  452. for (i = 0; i < ARRAY_SIZE(mt7530_mib); i++) {
  453. mib = &mt7530_mib[i];
  454. reg = MT7530_PORT_MIB_COUNTER(port) + mib->offset;
  455. data[i] = mt7530_read(priv, reg);
  456. if (mib->size == 2) {
  457. hi = mt7530_read(priv, reg + 4);
  458. data[i] |= hi << 32;
  459. }
  460. }
  461. }
  462. static int
  463. mt7530_get_sset_count(struct dsa_switch *ds, int port, int sset)
  464. {
  465. if (sset != ETH_SS_STATS)
  466. return 0;
  467. return ARRAY_SIZE(mt7530_mib);
  468. }
  469. static void mt7530_setup_port5(struct dsa_switch *ds, phy_interface_t interface)
  470. {
  471. struct mt7530_priv *priv = ds->priv;
  472. u8 tx_delay = 0;
  473. int val;
  474. mutex_lock(&priv->reg_mutex);
  475. val = mt7530_read(priv, MT7530_MHWTRAP);
  476. val |= MHWTRAP_MANUAL | MHWTRAP_P5_MAC_SEL | MHWTRAP_P5_DIS;
  477. val &= ~MHWTRAP_P5_RGMII_MODE & ~MHWTRAP_PHY0_SEL;
  478. switch (priv->p5_intf_sel) {
  479. case P5_INTF_SEL_PHY_P0:
  480. /* MT7530_P5_MODE_GPHY_P0: 2nd GMAC -> P5 -> P0 */
  481. val |= MHWTRAP_PHY0_SEL;
  482. /* fall through */
  483. case P5_INTF_SEL_PHY_P4:
  484. /* MT7530_P5_MODE_GPHY_P4: 2nd GMAC -> P5 -> P4 */
  485. val &= ~MHWTRAP_P5_MAC_SEL & ~MHWTRAP_P5_DIS;
  486. /* Setup the MAC by default for the cpu port */
  487. mt7530_write(priv, MT7530_PMCR_P(5), 0x56300);
  488. break;
  489. case P5_INTF_SEL_GMAC5:
  490. /* MT7530_P5_MODE_GMAC: P5 -> External phy or 2nd GMAC */
  491. val &= ~MHWTRAP_P5_DIS;
  492. break;
  493. case P5_DISABLED:
  494. interface = PHY_INTERFACE_MODE_NA;
  495. break;
  496. default:
  497. dev_err(ds->dev, "Unsupported p5_intf_sel %d\n",
  498. priv->p5_intf_sel);
  499. goto unlock_exit;
  500. }
  501. /* Setup RGMII settings */
  502. if (phy_interface_mode_is_rgmii(interface)) {
  503. val |= MHWTRAP_P5_RGMII_MODE;
  504. /* P5 RGMII RX Clock Control: delay setting for 1000M */
  505. mt7530_write(priv, MT7530_P5RGMIIRXCR, CSR_RGMII_EDGE_ALIGN);
  506. /* Don't set delay in DSA mode */
  507. if (!dsa_is_dsa_port(priv->ds, 5) &&
  508. (interface == PHY_INTERFACE_MODE_RGMII_TXID ||
  509. interface == PHY_INTERFACE_MODE_RGMII_ID))
  510. tx_delay = 4; /* n * 0.5 ns */
  511. /* P5 RGMII TX Clock Control: delay x */
  512. mt7530_write(priv, MT7530_P5RGMIITXCR,
  513. CSR_RGMII_TXC_CFG(0x10 + tx_delay));
  514. /* reduce P5 RGMII Tx driving, 8mA */
  515. mt7530_write(priv, MT7530_IO_DRV_CR,
  516. P5_IO_CLK_DRV(1) | P5_IO_DATA_DRV(1));
  517. }
  518. mt7530_write(priv, MT7530_MHWTRAP, val);
  519. dev_dbg(ds->dev, "Setup P5, HWTRAP=0x%x, intf_sel=%s, phy-mode=%s\n",
  520. val, p5_intf_modes(priv->p5_intf_sel), phy_modes(interface));
  521. priv->p5_interface = interface;
  522. unlock_exit:
  523. mutex_unlock(&priv->reg_mutex);
  524. }
  525. static int
  526. mt7530_cpu_port_enable(struct mt7530_priv *priv,
  527. int port)
  528. {
  529. /* Enable Mediatek header mode on the cpu port */
  530. mt7530_write(priv, MT7530_PVC_P(port),
  531. PORT_SPEC_TAG);
  532. /* Unknown multicast frame forwarding to the cpu port */
  533. mt7530_rmw(priv, MT7530_MFC, UNM_FFP_MASK, UNM_FFP(BIT(port)));
  534. /* Set CPU port number */
  535. if (priv->id == ID_MT7621)
  536. mt7530_rmw(priv, MT7530_MFC, CPU_MASK, CPU_EN | CPU_PORT(port));
  537. /* CPU port gets connected to all user ports of
  538. * the switch
  539. */
  540. mt7530_write(priv, MT7530_PCR_P(port),
  541. PCR_MATRIX(dsa_user_ports(priv->ds)));
  542. return 0;
  543. }
  544. static int
  545. mt7530_port_enable(struct dsa_switch *ds, int port,
  546. struct phy_device *phy)
  547. {
  548. struct mt7530_priv *priv = ds->priv;
  549. if (!dsa_is_user_port(ds, port))
  550. return 0;
  551. mutex_lock(&priv->reg_mutex);
  552. /* Allow the user port gets connected to the cpu port and also
  553. * restore the port matrix if the port is the member of a certain
  554. * bridge.
  555. */
  556. priv->ports[port].pm |= PCR_MATRIX(BIT(MT7530_CPU_PORT));
  557. priv->ports[port].enable = true;
  558. mt7530_rmw(priv, MT7530_PCR_P(port), PCR_MATRIX_MASK,
  559. priv->ports[port].pm);
  560. mt7530_port_set_status(priv, port, 0);
  561. mutex_unlock(&priv->reg_mutex);
  562. return 0;
  563. }
  564. static void
  565. mt7530_port_disable(struct dsa_switch *ds, int port)
  566. {
  567. struct mt7530_priv *priv = ds->priv;
  568. if (!dsa_is_user_port(ds, port))
  569. return;
  570. mutex_lock(&priv->reg_mutex);
  571. /* Clear up all port matrix which could be restored in the next
  572. * enablement for the port.
  573. */
  574. priv->ports[port].enable = false;
  575. mt7530_rmw(priv, MT7530_PCR_P(port), PCR_MATRIX_MASK,
  576. PCR_MATRIX_CLR);
  577. mt7530_port_set_status(priv, port, 0);
  578. mutex_unlock(&priv->reg_mutex);
  579. }
  580. static void
  581. mt7530_stp_state_set(struct dsa_switch *ds, int port, u8 state)
  582. {
  583. struct mt7530_priv *priv = ds->priv;
  584. u32 stp_state;
  585. switch (state) {
  586. case BR_STATE_DISABLED:
  587. stp_state = MT7530_STP_DISABLED;
  588. break;
  589. case BR_STATE_BLOCKING:
  590. stp_state = MT7530_STP_BLOCKING;
  591. break;
  592. case BR_STATE_LISTENING:
  593. stp_state = MT7530_STP_LISTENING;
  594. break;
  595. case BR_STATE_LEARNING:
  596. stp_state = MT7530_STP_LEARNING;
  597. break;
  598. case BR_STATE_FORWARDING:
  599. default:
  600. stp_state = MT7530_STP_FORWARDING;
  601. break;
  602. }
  603. mt7530_rmw(priv, MT7530_SSP_P(port), FID_PST_MASK, stp_state);
  604. }
  605. static int
  606. mt7530_port_bridge_join(struct dsa_switch *ds, int port,
  607. struct net_device *bridge)
  608. {
  609. struct mt7530_priv *priv = ds->priv;
  610. u32 port_bitmap = BIT(MT7530_CPU_PORT);
  611. int i;
  612. mutex_lock(&priv->reg_mutex);
  613. for (i = 0; i < MT7530_NUM_PORTS; i++) {
  614. /* Add this port to the port matrix of the other ports in the
  615. * same bridge. If the port is disabled, port matrix is kept
  616. * and not being setup until the port becomes enabled.
  617. */
  618. if (dsa_is_user_port(ds, i) && i != port) {
  619. if (dsa_to_port(ds, i)->bridge_dev != bridge)
  620. continue;
  621. if (priv->ports[i].enable)
  622. mt7530_set(priv, MT7530_PCR_P(i),
  623. PCR_MATRIX(BIT(port)));
  624. priv->ports[i].pm |= PCR_MATRIX(BIT(port));
  625. port_bitmap |= BIT(i);
  626. }
  627. }
  628. /* Add the all other ports to this port matrix. */
  629. if (priv->ports[port].enable)
  630. mt7530_rmw(priv, MT7530_PCR_P(port),
  631. PCR_MATRIX_MASK, PCR_MATRIX(port_bitmap));
  632. priv->ports[port].pm |= PCR_MATRIX(port_bitmap);
  633. mutex_unlock(&priv->reg_mutex);
  634. return 0;
  635. }
  636. static void
  637. mt7530_port_set_vlan_unaware(struct dsa_switch *ds, int port)
  638. {
  639. struct mt7530_priv *priv = ds->priv;
  640. bool all_user_ports_removed = true;
  641. int i;
  642. /* When a port is removed from the bridge, the port would be set up
  643. * back to the default as is at initial boot which is a VLAN-unaware
  644. * port.
  645. */
  646. mt7530_rmw(priv, MT7530_PCR_P(port), PCR_PORT_VLAN_MASK,
  647. MT7530_PORT_MATRIX_MODE);
  648. mt7530_rmw(priv, MT7530_PVC_P(port), VLAN_ATTR_MASK | PVC_EG_TAG_MASK,
  649. VLAN_ATTR(MT7530_VLAN_TRANSPARENT) |
  650. PVC_EG_TAG(MT7530_VLAN_EG_CONSISTENT));
  651. for (i = 0; i < MT7530_NUM_PORTS; i++) {
  652. if (dsa_is_user_port(ds, i) &&
  653. dsa_port_is_vlan_filtering(&ds->ports[i])) {
  654. all_user_ports_removed = false;
  655. break;
  656. }
  657. }
  658. /* CPU port also does the same thing until all user ports belonging to
  659. * the CPU port get out of VLAN filtering mode.
  660. */
  661. if (all_user_ports_removed) {
  662. mt7530_write(priv, MT7530_PCR_P(MT7530_CPU_PORT),
  663. PCR_MATRIX(dsa_user_ports(priv->ds)));
  664. mt7530_write(priv, MT7530_PVC_P(MT7530_CPU_PORT), PORT_SPEC_TAG
  665. | PVC_EG_TAG(MT7530_VLAN_EG_CONSISTENT));
  666. }
  667. }
  668. static void
  669. mt7530_port_set_vlan_aware(struct dsa_switch *ds, int port)
  670. {
  671. struct mt7530_priv *priv = ds->priv;
  672. /* Trapped into security mode allows packet forwarding through VLAN
  673. * table lookup. CPU port is set to fallback mode to let untagged
  674. * frames pass through.
  675. */
  676. if (dsa_is_cpu_port(ds, port))
  677. mt7530_rmw(priv, MT7530_PCR_P(port), PCR_PORT_VLAN_MASK,
  678. MT7530_PORT_FALLBACK_MODE);
  679. else
  680. mt7530_rmw(priv, MT7530_PCR_P(port), PCR_PORT_VLAN_MASK,
  681. MT7530_PORT_SECURITY_MODE);
  682. /* Set the port as a user port which is to be able to recognize VID
  683. * from incoming packets before fetching entry within the VLAN table.
  684. */
  685. mt7530_rmw(priv, MT7530_PVC_P(port), VLAN_ATTR_MASK | PVC_EG_TAG_MASK,
  686. VLAN_ATTR(MT7530_VLAN_USER) |
  687. PVC_EG_TAG(MT7530_VLAN_EG_DISABLED));
  688. }
  689. static void
  690. mt7530_port_bridge_leave(struct dsa_switch *ds, int port,
  691. struct net_device *bridge)
  692. {
  693. struct mt7530_priv *priv = ds->priv;
  694. int i;
  695. mutex_lock(&priv->reg_mutex);
  696. for (i = 0; i < MT7530_NUM_PORTS; i++) {
  697. /* Remove this port from the port matrix of the other ports
  698. * in the same bridge. If the port is disabled, port matrix
  699. * is kept and not being setup until the port becomes enabled.
  700. */
  701. if (dsa_is_user_port(ds, i) && i != port) {
  702. if (dsa_to_port(ds, i)->bridge_dev != bridge)
  703. continue;
  704. if (priv->ports[i].enable)
  705. mt7530_clear(priv, MT7530_PCR_P(i),
  706. PCR_MATRIX(BIT(port)));
  707. priv->ports[i].pm &= ~PCR_MATRIX(BIT(port));
  708. }
  709. }
  710. /* Set the cpu port to be the only one in the port matrix of
  711. * this port.
  712. */
  713. if (priv->ports[port].enable)
  714. mt7530_rmw(priv, MT7530_PCR_P(port), PCR_MATRIX_MASK,
  715. PCR_MATRIX(BIT(MT7530_CPU_PORT)));
  716. priv->ports[port].pm = PCR_MATRIX(BIT(MT7530_CPU_PORT));
  717. mutex_unlock(&priv->reg_mutex);
  718. }
  719. static int
  720. mt7530_port_fdb_add(struct dsa_switch *ds, int port,
  721. const unsigned char *addr, u16 vid)
  722. {
  723. struct mt7530_priv *priv = ds->priv;
  724. int ret;
  725. u8 port_mask = BIT(port);
  726. mutex_lock(&priv->reg_mutex);
  727. mt7530_fdb_write(priv, vid, port_mask, addr, -1, STATIC_ENT);
  728. ret = mt7530_fdb_cmd(priv, MT7530_FDB_WRITE, NULL);
  729. mutex_unlock(&priv->reg_mutex);
  730. return ret;
  731. }
  732. static int
  733. mt7530_port_fdb_del(struct dsa_switch *ds, int port,
  734. const unsigned char *addr, u16 vid)
  735. {
  736. struct mt7530_priv *priv = ds->priv;
  737. int ret;
  738. u8 port_mask = BIT(port);
  739. mutex_lock(&priv->reg_mutex);
  740. mt7530_fdb_write(priv, vid, port_mask, addr, -1, STATIC_EMP);
  741. ret = mt7530_fdb_cmd(priv, MT7530_FDB_WRITE, NULL);
  742. mutex_unlock(&priv->reg_mutex);
  743. return ret;
  744. }
  745. static int
  746. mt7530_port_fdb_dump(struct dsa_switch *ds, int port,
  747. dsa_fdb_dump_cb_t *cb, void *data)
  748. {
  749. struct mt7530_priv *priv = ds->priv;
  750. struct mt7530_fdb _fdb = { 0 };
  751. int cnt = MT7530_NUM_FDB_RECORDS;
  752. int ret = 0;
  753. u32 rsp = 0;
  754. mutex_lock(&priv->reg_mutex);
  755. ret = mt7530_fdb_cmd(priv, MT7530_FDB_START, &rsp);
  756. if (ret < 0)
  757. goto err;
  758. do {
  759. if (rsp & ATC_SRCH_HIT) {
  760. mt7530_fdb_read(priv, &_fdb);
  761. if (_fdb.port_mask & BIT(port)) {
  762. ret = cb(_fdb.mac, _fdb.vid, _fdb.noarp,
  763. data);
  764. if (ret < 0)
  765. break;
  766. }
  767. }
  768. } while (--cnt &&
  769. !(rsp & ATC_SRCH_END) &&
  770. !mt7530_fdb_cmd(priv, MT7530_FDB_NEXT, &rsp));
  771. err:
  772. mutex_unlock(&priv->reg_mutex);
  773. return 0;
  774. }
  775. static int
  776. mt7530_vlan_cmd(struct mt7530_priv *priv, enum mt7530_vlan_cmd cmd, u16 vid)
  777. {
  778. struct mt7530_dummy_poll p;
  779. u32 val;
  780. int ret;
  781. val = VTCR_BUSY | VTCR_FUNC(cmd) | vid;
  782. mt7530_write(priv, MT7530_VTCR, val);
  783. INIT_MT7530_DUMMY_POLL(&p, priv, MT7530_VTCR);
  784. ret = readx_poll_timeout(_mt7530_read, &p, val,
  785. !(val & VTCR_BUSY), 20, 20000);
  786. if (ret < 0) {
  787. dev_err(priv->dev, "poll timeout\n");
  788. return ret;
  789. }
  790. val = mt7530_read(priv, MT7530_VTCR);
  791. if (val & VTCR_INVALID) {
  792. dev_err(priv->dev, "read VTCR invalid\n");
  793. return -EINVAL;
  794. }
  795. return 0;
  796. }
  797. static int
  798. mt7530_port_vlan_filtering(struct dsa_switch *ds, int port,
  799. bool vlan_filtering)
  800. {
  801. if (vlan_filtering) {
  802. /* The port is being kept as VLAN-unaware port when bridge is
  803. * set up with vlan_filtering not being set, Otherwise, the
  804. * port and the corresponding CPU port is required the setup
  805. * for becoming a VLAN-aware port.
  806. */
  807. mt7530_port_set_vlan_aware(ds, port);
  808. mt7530_port_set_vlan_aware(ds, MT7530_CPU_PORT);
  809. } else {
  810. mt7530_port_set_vlan_unaware(ds, port);
  811. }
  812. return 0;
  813. }
  814. static int
  815. mt7530_port_vlan_prepare(struct dsa_switch *ds, int port,
  816. const struct switchdev_obj_port_vlan *vlan)
  817. {
  818. /* nothing needed */
  819. return 0;
  820. }
  821. static void
  822. mt7530_hw_vlan_add(struct mt7530_priv *priv,
  823. struct mt7530_hw_vlan_entry *entry)
  824. {
  825. u8 new_members;
  826. u32 val;
  827. new_members = entry->old_members | BIT(entry->port) |
  828. BIT(MT7530_CPU_PORT);
  829. /* Validate the entry with independent learning, create egress tag per
  830. * VLAN and joining the port as one of the port members.
  831. */
  832. val = IVL_MAC | VTAG_EN | PORT_MEM(new_members) | VLAN_VALID;
  833. mt7530_write(priv, MT7530_VAWD1, val);
  834. /* Decide whether adding tag or not for those outgoing packets from the
  835. * port inside the VLAN.
  836. */
  837. val = entry->untagged ? MT7530_VLAN_EGRESS_UNTAG :
  838. MT7530_VLAN_EGRESS_TAG;
  839. mt7530_rmw(priv, MT7530_VAWD2,
  840. ETAG_CTRL_P_MASK(entry->port),
  841. ETAG_CTRL_P(entry->port, val));
  842. /* CPU port is always taken as a tagged port for serving more than one
  843. * VLANs across and also being applied with egress type stack mode for
  844. * that VLAN tags would be appended after hardware special tag used as
  845. * DSA tag.
  846. */
  847. mt7530_rmw(priv, MT7530_VAWD2,
  848. ETAG_CTRL_P_MASK(MT7530_CPU_PORT),
  849. ETAG_CTRL_P(MT7530_CPU_PORT,
  850. MT7530_VLAN_EGRESS_STACK));
  851. }
  852. static void
  853. mt7530_hw_vlan_del(struct mt7530_priv *priv,
  854. struct mt7530_hw_vlan_entry *entry)
  855. {
  856. u8 new_members;
  857. u32 val;
  858. new_members = entry->old_members & ~BIT(entry->port);
  859. val = mt7530_read(priv, MT7530_VAWD1);
  860. if (!(val & VLAN_VALID)) {
  861. dev_err(priv->dev,
  862. "Cannot be deleted due to invalid entry\n");
  863. return;
  864. }
  865. /* If certain member apart from CPU port is still alive in the VLAN,
  866. * the entry would be kept valid. Otherwise, the entry is got to be
  867. * disabled.
  868. */
  869. if (new_members && new_members != BIT(MT7530_CPU_PORT)) {
  870. val = IVL_MAC | VTAG_EN | PORT_MEM(new_members) |
  871. VLAN_VALID;
  872. mt7530_write(priv, MT7530_VAWD1, val);
  873. } else {
  874. mt7530_write(priv, MT7530_VAWD1, 0);
  875. mt7530_write(priv, MT7530_VAWD2, 0);
  876. }
  877. }
  878. static void
  879. mt7530_hw_vlan_update(struct mt7530_priv *priv, u16 vid,
  880. struct mt7530_hw_vlan_entry *entry,
  881. mt7530_vlan_op vlan_op)
  882. {
  883. u32 val;
  884. /* Fetch entry */
  885. mt7530_vlan_cmd(priv, MT7530_VTCR_RD_VID, vid);
  886. val = mt7530_read(priv, MT7530_VAWD1);
  887. entry->old_members = (val >> PORT_MEM_SHFT) & PORT_MEM_MASK;
  888. /* Manipulate entry */
  889. vlan_op(priv, entry);
  890. /* Flush result to hardware */
  891. mt7530_vlan_cmd(priv, MT7530_VTCR_WR_VID, vid);
  892. }
  893. static void
  894. mt7530_port_vlan_add(struct dsa_switch *ds, int port,
  895. const struct switchdev_obj_port_vlan *vlan)
  896. {
  897. bool untagged = vlan->flags & BRIDGE_VLAN_INFO_UNTAGGED;
  898. bool pvid = vlan->flags & BRIDGE_VLAN_INFO_PVID;
  899. struct mt7530_hw_vlan_entry new_entry;
  900. struct mt7530_priv *priv = ds->priv;
  901. u16 vid;
  902. /* The port is kept as VLAN-unaware if bridge with vlan_filtering not
  903. * being set.
  904. */
  905. if (!dsa_port_is_vlan_filtering(&ds->ports[port]))
  906. return;
  907. mutex_lock(&priv->reg_mutex);
  908. for (vid = vlan->vid_begin; vid <= vlan->vid_end; ++vid) {
  909. mt7530_hw_vlan_entry_init(&new_entry, port, untagged);
  910. mt7530_hw_vlan_update(priv, vid, &new_entry,
  911. mt7530_hw_vlan_add);
  912. }
  913. if (pvid) {
  914. mt7530_rmw(priv, MT7530_PPBV1_P(port), G0_PORT_VID_MASK,
  915. G0_PORT_VID(vlan->vid_end));
  916. priv->ports[port].pvid = vlan->vid_end;
  917. }
  918. mutex_unlock(&priv->reg_mutex);
  919. }
  920. static int
  921. mt7530_port_vlan_del(struct dsa_switch *ds, int port,
  922. const struct switchdev_obj_port_vlan *vlan)
  923. {
  924. struct mt7530_hw_vlan_entry target_entry;
  925. struct mt7530_priv *priv = ds->priv;
  926. u16 vid, pvid;
  927. /* The port is kept as VLAN-unaware if bridge with vlan_filtering not
  928. * being set.
  929. */
  930. if (!dsa_port_is_vlan_filtering(&ds->ports[port]))
  931. return 0;
  932. mutex_lock(&priv->reg_mutex);
  933. pvid = priv->ports[port].pvid;
  934. for (vid = vlan->vid_begin; vid <= vlan->vid_end; ++vid) {
  935. mt7530_hw_vlan_entry_init(&target_entry, port, 0);
  936. mt7530_hw_vlan_update(priv, vid, &target_entry,
  937. mt7530_hw_vlan_del);
  938. /* PVID is being restored to the default whenever the PVID port
  939. * is being removed from the VLAN.
  940. */
  941. if (pvid == vid)
  942. pvid = G0_PORT_VID_DEF;
  943. }
  944. mt7530_rmw(priv, MT7530_PPBV1_P(port), G0_PORT_VID_MASK, pvid);
  945. priv->ports[port].pvid = pvid;
  946. mutex_unlock(&priv->reg_mutex);
  947. return 0;
  948. }
  949. static enum dsa_tag_protocol
  950. mtk_get_tag_protocol(struct dsa_switch *ds, int port)
  951. {
  952. struct mt7530_priv *priv = ds->priv;
  953. if (port != MT7530_CPU_PORT) {
  954. dev_warn(priv->dev,
  955. "port not matched with tagging CPU port\n");
  956. return DSA_TAG_PROTO_NONE;
  957. } else {
  958. return DSA_TAG_PROTO_MTK;
  959. }
  960. }
  961. static int
  962. mt7530_setup(struct dsa_switch *ds)
  963. {
  964. struct mt7530_priv *priv = ds->priv;
  965. struct device_node *phy_node;
  966. struct device_node *mac_np;
  967. struct mt7530_dummy_poll p;
  968. phy_interface_t interface;
  969. struct device_node *dn;
  970. u32 id, val;
  971. int ret, i;
  972. /* The parent node of master netdev which holds the common system
  973. * controller also is the container for two GMACs nodes representing
  974. * as two netdev instances.
  975. */
  976. dn = ds->ports[MT7530_CPU_PORT].master->dev.of_node->parent;
  977. if (priv->id == ID_MT7530) {
  978. regulator_set_voltage(priv->core_pwr, 1000000, 1000000);
  979. ret = regulator_enable(priv->core_pwr);
  980. if (ret < 0) {
  981. dev_err(priv->dev,
  982. "Failed to enable core power: %d\n", ret);
  983. return ret;
  984. }
  985. regulator_set_voltage(priv->io_pwr, 3300000, 3300000);
  986. ret = regulator_enable(priv->io_pwr);
  987. if (ret < 0) {
  988. dev_err(priv->dev, "Failed to enable io pwr: %d\n",
  989. ret);
  990. return ret;
  991. }
  992. }
  993. /* Reset whole chip through gpio pin or memory-mapped registers for
  994. * different type of hardware
  995. */
  996. if (priv->mcm) {
  997. reset_control_assert(priv->rstc);
  998. usleep_range(1000, 1100);
  999. reset_control_deassert(priv->rstc);
  1000. } else {
  1001. gpiod_set_value_cansleep(priv->reset, 0);
  1002. usleep_range(1000, 1100);
  1003. gpiod_set_value_cansleep(priv->reset, 1);
  1004. }
  1005. /* Waiting for MT7530 got to stable */
  1006. INIT_MT7530_DUMMY_POLL(&p, priv, MT7530_HWTRAP);
  1007. ret = readx_poll_timeout(_mt7530_read, &p, val, val != 0,
  1008. 20, 1000000);
  1009. if (ret < 0) {
  1010. dev_err(priv->dev, "reset timeout\n");
  1011. return ret;
  1012. }
  1013. id = mt7530_read(priv, MT7530_CREV);
  1014. id >>= CHIP_NAME_SHIFT;
  1015. if (id != MT7530_ID) {
  1016. dev_err(priv->dev, "chip %x can't be supported\n", id);
  1017. return -ENODEV;
  1018. }
  1019. /* Reset the switch through internal reset */
  1020. mt7530_write(priv, MT7530_SYS_CTRL,
  1021. SYS_CTRL_PHY_RST | SYS_CTRL_SW_RST |
  1022. SYS_CTRL_REG_RST);
  1023. /* Enable Port 6 only; P5 as GMAC5 which currently is not supported */
  1024. val = mt7530_read(priv, MT7530_MHWTRAP);
  1025. val &= ~MHWTRAP_P6_DIS & ~MHWTRAP_PHY_ACCESS;
  1026. val |= MHWTRAP_MANUAL;
  1027. mt7530_write(priv, MT7530_MHWTRAP, val);
  1028. priv->p6_interface = PHY_INTERFACE_MODE_NA;
  1029. /* Enable and reset MIB counters */
  1030. mt7530_mib_reset(ds);
  1031. for (i = 0; i < MT7530_NUM_PORTS; i++) {
  1032. /* Disable forwarding by default on all ports */
  1033. mt7530_rmw(priv, MT7530_PCR_P(i), PCR_MATRIX_MASK,
  1034. PCR_MATRIX_CLR);
  1035. if (dsa_is_cpu_port(ds, i))
  1036. mt7530_cpu_port_enable(priv, i);
  1037. else
  1038. mt7530_port_disable(ds, i);
  1039. /* Enable consistent egress tag */
  1040. mt7530_rmw(priv, MT7530_PVC_P(i), PVC_EG_TAG_MASK,
  1041. PVC_EG_TAG(MT7530_VLAN_EG_CONSISTENT));
  1042. }
  1043. /* Setup port 5 */
  1044. priv->p5_intf_sel = P5_DISABLED;
  1045. interface = PHY_INTERFACE_MODE_NA;
  1046. if (!dsa_is_unused_port(ds, 5)) {
  1047. priv->p5_intf_sel = P5_INTF_SEL_GMAC5;
  1048. interface = of_get_phy_mode(ds->ports[5].dn);
  1049. } else {
  1050. /* Scan the ethernet nodes. look for GMAC1, lookup used phy */
  1051. for_each_child_of_node(dn, mac_np) {
  1052. if (!of_device_is_compatible(mac_np,
  1053. "mediatek,eth-mac"))
  1054. continue;
  1055. ret = of_property_read_u32(mac_np, "reg", &id);
  1056. if (ret < 0 || id != 1)
  1057. continue;
  1058. phy_node = of_parse_phandle(mac_np, "phy-handle", 0);
  1059. if (!phy_node)
  1060. continue;
  1061. if (phy_node->parent == priv->dev->of_node->parent) {
  1062. interface = of_get_phy_mode(mac_np);
  1063. id = of_mdio_parse_addr(ds->dev, phy_node);
  1064. if (id == 0)
  1065. priv->p5_intf_sel = P5_INTF_SEL_PHY_P0;
  1066. if (id == 4)
  1067. priv->p5_intf_sel = P5_INTF_SEL_PHY_P4;
  1068. }
  1069. of_node_put(phy_node);
  1070. break;
  1071. }
  1072. }
  1073. mt7530_setup_port5(ds, interface);
  1074. /* Flush the FDB table */
  1075. ret = mt7530_fdb_cmd(priv, MT7530_FDB_FLUSH, NULL);
  1076. if (ret < 0)
  1077. return ret;
  1078. return 0;
  1079. }
  1080. static void mt7530_phylink_mac_config(struct dsa_switch *ds, int port,
  1081. unsigned int mode,
  1082. const struct phylink_link_state *state)
  1083. {
  1084. struct mt7530_priv *priv = ds->priv;
  1085. u32 mcr_cur, mcr_new;
  1086. switch (port) {
  1087. case 0: /* Internal phy */
  1088. case 1:
  1089. case 2:
  1090. case 3:
  1091. case 4:
  1092. if (state->interface != PHY_INTERFACE_MODE_GMII)
  1093. return;
  1094. break;
  1095. case 5: /* 2nd cpu port with phy of port 0 or 4 / external phy */
  1096. if (priv->p5_interface == state->interface)
  1097. break;
  1098. if (!phy_interface_mode_is_rgmii(state->interface) &&
  1099. state->interface != PHY_INTERFACE_MODE_MII &&
  1100. state->interface != PHY_INTERFACE_MODE_GMII)
  1101. return;
  1102. mt7530_setup_port5(ds, state->interface);
  1103. break;
  1104. case 6: /* 1st cpu port */
  1105. if (priv->p6_interface == state->interface)
  1106. break;
  1107. if (state->interface != PHY_INTERFACE_MODE_RGMII &&
  1108. state->interface != PHY_INTERFACE_MODE_TRGMII)
  1109. return;
  1110. /* Setup TX circuit incluing relevant PAD and driving */
  1111. mt7530_pad_clk_setup(ds, state->interface);
  1112. priv->p6_interface = state->interface;
  1113. break;
  1114. default:
  1115. dev_err(ds->dev, "%s: unsupported port: %i\n", __func__, port);
  1116. return;
  1117. }
  1118. if (phylink_autoneg_inband(mode)) {
  1119. dev_err(ds->dev, "%s: in-band negotiation unsupported\n",
  1120. __func__);
  1121. return;
  1122. }
  1123. mcr_cur = mt7530_read(priv, MT7530_PMCR_P(port));
  1124. mcr_new = mcr_cur;
  1125. mcr_new &= ~(PMCR_FORCE_SPEED_1000 | PMCR_FORCE_SPEED_100 |
  1126. PMCR_FORCE_FDX | PMCR_TX_FC_EN | PMCR_RX_FC_EN);
  1127. mcr_new |= PMCR_IFG_XMIT(1) | PMCR_MAC_MODE | PMCR_BACKOFF_EN |
  1128. PMCR_BACKPR_EN | PMCR_FORCE_MODE;
  1129. /* Are we connected to external phy */
  1130. if (port == 5 && dsa_is_user_port(ds, 5))
  1131. mcr_new |= PMCR_EXT_PHY;
  1132. switch (state->speed) {
  1133. case SPEED_1000:
  1134. mcr_new |= PMCR_FORCE_SPEED_1000;
  1135. break;
  1136. case SPEED_100:
  1137. mcr_new |= PMCR_FORCE_SPEED_100;
  1138. break;
  1139. }
  1140. if (state->duplex == DUPLEX_FULL) {
  1141. mcr_new |= PMCR_FORCE_FDX;
  1142. if (state->pause & MLO_PAUSE_TX)
  1143. mcr_new |= PMCR_TX_FC_EN;
  1144. if (state->pause & MLO_PAUSE_RX)
  1145. mcr_new |= PMCR_RX_FC_EN;
  1146. }
  1147. if (mcr_new != mcr_cur)
  1148. mt7530_write(priv, MT7530_PMCR_P(port), mcr_new);
  1149. }
  1150. static void mt7530_phylink_mac_link_down(struct dsa_switch *ds, int port,
  1151. unsigned int mode,
  1152. phy_interface_t interface)
  1153. {
  1154. struct mt7530_priv *priv = ds->priv;
  1155. mt7530_port_set_status(priv, port, 0);
  1156. }
  1157. static void mt7530_phylink_mac_link_up(struct dsa_switch *ds, int port,
  1158. unsigned int mode,
  1159. phy_interface_t interface,
  1160. struct phy_device *phydev)
  1161. {
  1162. struct mt7530_priv *priv = ds->priv;
  1163. mt7530_port_set_status(priv, port, 1);
  1164. }
  1165. static void mt7530_phylink_validate(struct dsa_switch *ds, int port,
  1166. unsigned long *supported,
  1167. struct phylink_link_state *state)
  1168. {
  1169. __ETHTOOL_DECLARE_LINK_MODE_MASK(mask) = { 0, };
  1170. switch (port) {
  1171. case 0: /* Internal phy */
  1172. case 1:
  1173. case 2:
  1174. case 3:
  1175. case 4:
  1176. if (state->interface != PHY_INTERFACE_MODE_NA &&
  1177. state->interface != PHY_INTERFACE_MODE_GMII)
  1178. goto unsupported;
  1179. break;
  1180. case 5: /* 2nd cpu port with phy of port 0 or 4 / external phy */
  1181. if (state->interface != PHY_INTERFACE_MODE_NA &&
  1182. !phy_interface_mode_is_rgmii(state->interface) &&
  1183. state->interface != PHY_INTERFACE_MODE_MII &&
  1184. state->interface != PHY_INTERFACE_MODE_GMII)
  1185. goto unsupported;
  1186. break;
  1187. case 6: /* 1st cpu port */
  1188. if (state->interface != PHY_INTERFACE_MODE_NA &&
  1189. state->interface != PHY_INTERFACE_MODE_RGMII &&
  1190. state->interface != PHY_INTERFACE_MODE_TRGMII)
  1191. goto unsupported;
  1192. break;
  1193. default:
  1194. dev_err(ds->dev, "%s: unsupported port: %i\n", __func__, port);
  1195. unsupported:
  1196. linkmode_zero(supported);
  1197. return;
  1198. }
  1199. phylink_set_port_modes(mask);
  1200. phylink_set(mask, Autoneg);
  1201. if (state->interface == PHY_INTERFACE_MODE_TRGMII) {
  1202. phylink_set(mask, 1000baseT_Full);
  1203. } else {
  1204. phylink_set(mask, 10baseT_Half);
  1205. phylink_set(mask, 10baseT_Full);
  1206. phylink_set(mask, 100baseT_Half);
  1207. phylink_set(mask, 100baseT_Full);
  1208. if (state->interface != PHY_INTERFACE_MODE_MII) {
  1209. /* This switch only supports 1G full-duplex. */
  1210. phylink_set(mask, 1000baseT_Full);
  1211. if (port == 5)
  1212. phylink_set(mask, 1000baseX_Full);
  1213. }
  1214. }
  1215. phylink_set(mask, Pause);
  1216. phylink_set(mask, Asym_Pause);
  1217. linkmode_and(supported, supported, mask);
  1218. linkmode_and(state->advertising, state->advertising, mask);
  1219. }
  1220. static int
  1221. mt7530_phylink_mac_link_state(struct dsa_switch *ds, int port,
  1222. struct phylink_link_state *state)
  1223. {
  1224. struct mt7530_priv *priv = ds->priv;
  1225. u32 pmsr;
  1226. if (port < 0 || port >= MT7530_NUM_PORTS)
  1227. return -EINVAL;
  1228. pmsr = mt7530_read(priv, MT7530_PMSR_P(port));
  1229. state->link = (pmsr & PMSR_LINK);
  1230. state->an_complete = state->link;
  1231. state->duplex = !!(pmsr & PMSR_DPX);
  1232. switch (pmsr & PMSR_SPEED_MASK) {
  1233. case PMSR_SPEED_10:
  1234. state->speed = SPEED_10;
  1235. break;
  1236. case PMSR_SPEED_100:
  1237. state->speed = SPEED_100;
  1238. break;
  1239. case PMSR_SPEED_1000:
  1240. state->speed = SPEED_1000;
  1241. break;
  1242. default:
  1243. state->speed = SPEED_UNKNOWN;
  1244. break;
  1245. }
  1246. state->pause &= ~(MLO_PAUSE_RX | MLO_PAUSE_TX);
  1247. if (pmsr & PMSR_RX_FC)
  1248. state->pause |= MLO_PAUSE_RX;
  1249. if (pmsr & PMSR_TX_FC)
  1250. state->pause |= MLO_PAUSE_TX;
  1251. return 1;
  1252. }
  1253. static const struct dsa_switch_ops mt7530_switch_ops = {
  1254. .get_tag_protocol = mtk_get_tag_protocol,
  1255. .setup = mt7530_setup,
  1256. .get_strings = mt7530_get_strings,
  1257. .phy_read = mt7530_phy_read,
  1258. .phy_write = mt7530_phy_write,
  1259. .get_ethtool_stats = mt7530_get_ethtool_stats,
  1260. .get_sset_count = mt7530_get_sset_count,
  1261. .port_enable = mt7530_port_enable,
  1262. .port_disable = mt7530_port_disable,
  1263. .port_stp_state_set = mt7530_stp_state_set,
  1264. .port_bridge_join = mt7530_port_bridge_join,
  1265. .port_bridge_leave = mt7530_port_bridge_leave,
  1266. .port_fdb_add = mt7530_port_fdb_add,
  1267. .port_fdb_del = mt7530_port_fdb_del,
  1268. .port_fdb_dump = mt7530_port_fdb_dump,
  1269. .port_vlan_filtering = mt7530_port_vlan_filtering,
  1270. .port_vlan_prepare = mt7530_port_vlan_prepare,
  1271. .port_vlan_add = mt7530_port_vlan_add,
  1272. .port_vlan_del = mt7530_port_vlan_del,
  1273. .phylink_validate = mt7530_phylink_validate,
  1274. .phylink_mac_link_state = mt7530_phylink_mac_link_state,
  1275. .phylink_mac_config = mt7530_phylink_mac_config,
  1276. .phylink_mac_link_down = mt7530_phylink_mac_link_down,
  1277. .phylink_mac_link_up = mt7530_phylink_mac_link_up,
  1278. };
  1279. static const struct of_device_id mt7530_of_match[] = {
  1280. { .compatible = "mediatek,mt7621", .data = (void *)ID_MT7621, },
  1281. { .compatible = "mediatek,mt7530", .data = (void *)ID_MT7530, },
  1282. { /* sentinel */ },
  1283. };
  1284. MODULE_DEVICE_TABLE(of, mt7530_of_match);
  1285. static int
  1286. mt7530_probe(struct mdio_device *mdiodev)
  1287. {
  1288. struct mt7530_priv *priv;
  1289. struct device_node *dn;
  1290. dn = mdiodev->dev.of_node;
  1291. priv = devm_kzalloc(&mdiodev->dev, sizeof(*priv), GFP_KERNEL);
  1292. if (!priv)
  1293. return -ENOMEM;
  1294. priv->ds = dsa_switch_alloc(&mdiodev->dev, DSA_MAX_PORTS);
  1295. if (!priv->ds)
  1296. return -ENOMEM;
  1297. /* Use medatek,mcm property to distinguish hardware type that would
  1298. * casues a little bit differences on power-on sequence.
  1299. */
  1300. priv->mcm = of_property_read_bool(dn, "mediatek,mcm");
  1301. if (priv->mcm) {
  1302. dev_info(&mdiodev->dev, "MT7530 adapts as multi-chip module\n");
  1303. priv->rstc = devm_reset_control_get(&mdiodev->dev, "mcm");
  1304. if (IS_ERR(priv->rstc)) {
  1305. dev_err(&mdiodev->dev, "Couldn't get our reset line\n");
  1306. return PTR_ERR(priv->rstc);
  1307. }
  1308. }
  1309. /* Get the hardware identifier from the devicetree node.
  1310. * We will need it for some of the clock and regulator setup.
  1311. */
  1312. priv->id = (unsigned int)(unsigned long)
  1313. of_device_get_match_data(&mdiodev->dev);
  1314. if (priv->id == ID_MT7530) {
  1315. priv->core_pwr = devm_regulator_get(&mdiodev->dev, "core");
  1316. if (IS_ERR(priv->core_pwr))
  1317. return PTR_ERR(priv->core_pwr);
  1318. priv->io_pwr = devm_regulator_get(&mdiodev->dev, "io");
  1319. if (IS_ERR(priv->io_pwr))
  1320. return PTR_ERR(priv->io_pwr);
  1321. }
  1322. /* Not MCM that indicates switch works as the remote standalone
  1323. * integrated circuit so the GPIO pin would be used to complete
  1324. * the reset, otherwise memory-mapped register accessing used
  1325. * through syscon provides in the case of MCM.
  1326. */
  1327. if (!priv->mcm) {
  1328. priv->reset = devm_gpiod_get_optional(&mdiodev->dev, "reset",
  1329. GPIOD_OUT_LOW);
  1330. if (IS_ERR(priv->reset)) {
  1331. dev_err(&mdiodev->dev, "Couldn't get our reset line\n");
  1332. return PTR_ERR(priv->reset);
  1333. }
  1334. }
  1335. priv->bus = mdiodev->bus;
  1336. priv->dev = &mdiodev->dev;
  1337. priv->ds->priv = priv;
  1338. priv->ds->ops = &mt7530_switch_ops;
  1339. mutex_init(&priv->reg_mutex);
  1340. dev_set_drvdata(&mdiodev->dev, priv);
  1341. return dsa_register_switch(priv->ds);
  1342. }
  1343. static void
  1344. mt7530_remove(struct mdio_device *mdiodev)
  1345. {
  1346. struct mt7530_priv *priv = dev_get_drvdata(&mdiodev->dev);
  1347. int ret = 0;
  1348. ret = regulator_disable(priv->core_pwr);
  1349. if (ret < 0)
  1350. dev_err(priv->dev,
  1351. "Failed to disable core power: %d\n", ret);
  1352. ret = regulator_disable(priv->io_pwr);
  1353. if (ret < 0)
  1354. dev_err(priv->dev, "Failed to disable io pwr: %d\n",
  1355. ret);
  1356. dsa_unregister_switch(priv->ds);
  1357. mutex_destroy(&priv->reg_mutex);
  1358. }
  1359. static struct mdio_driver mt7530_mdio_driver = {
  1360. .probe = mt7530_probe,
  1361. .remove = mt7530_remove,
  1362. .mdiodrv.driver = {
  1363. .name = "mt7530",
  1364. .of_match_table = mt7530_of_match,
  1365. },
  1366. };
  1367. mdio_module_driver(mt7530_mdio_driver);
  1368. MODULE_AUTHOR("Sean Wang <sean.wang@mediatek.com>");
  1369. MODULE_DESCRIPTION("Driver for Mediatek MT7530 Switch");
  1370. MODULE_LICENSE("GPL");