mt7530.c 36 KB

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