mt7530.c 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126
  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 <net/switchdev.h>
  32. #include "mt7530.h"
  33. /* String, offset, and register size in bytes if different from 4 bytes */
  34. static const struct mt7530_mib_desc mt7530_mib[] = {
  35. MIB_DESC(1, 0x00, "TxDrop"),
  36. MIB_DESC(1, 0x04, "TxCrcErr"),
  37. MIB_DESC(1, 0x08, "TxUnicast"),
  38. MIB_DESC(1, 0x0c, "TxMulticast"),
  39. MIB_DESC(1, 0x10, "TxBroadcast"),
  40. MIB_DESC(1, 0x14, "TxCollision"),
  41. MIB_DESC(1, 0x18, "TxSingleCollision"),
  42. MIB_DESC(1, 0x1c, "TxMultipleCollision"),
  43. MIB_DESC(1, 0x20, "TxDeferred"),
  44. MIB_DESC(1, 0x24, "TxLateCollision"),
  45. MIB_DESC(1, 0x28, "TxExcessiveCollistion"),
  46. MIB_DESC(1, 0x2c, "TxPause"),
  47. MIB_DESC(1, 0x30, "TxPktSz64"),
  48. MIB_DESC(1, 0x34, "TxPktSz65To127"),
  49. MIB_DESC(1, 0x38, "TxPktSz128To255"),
  50. MIB_DESC(1, 0x3c, "TxPktSz256To511"),
  51. MIB_DESC(1, 0x40, "TxPktSz512To1023"),
  52. MIB_DESC(1, 0x44, "Tx1024ToMax"),
  53. MIB_DESC(2, 0x48, "TxBytes"),
  54. MIB_DESC(1, 0x60, "RxDrop"),
  55. MIB_DESC(1, 0x64, "RxFiltering"),
  56. MIB_DESC(1, 0x6c, "RxMulticast"),
  57. MIB_DESC(1, 0x70, "RxBroadcast"),
  58. MIB_DESC(1, 0x74, "RxAlignErr"),
  59. MIB_DESC(1, 0x78, "RxCrcErr"),
  60. MIB_DESC(1, 0x7c, "RxUnderSizeErr"),
  61. MIB_DESC(1, 0x80, "RxFragErr"),
  62. MIB_DESC(1, 0x84, "RxOverSzErr"),
  63. MIB_DESC(1, 0x88, "RxJabberErr"),
  64. MIB_DESC(1, 0x8c, "RxPause"),
  65. MIB_DESC(1, 0x90, "RxPktSz64"),
  66. MIB_DESC(1, 0x94, "RxPktSz65To127"),
  67. MIB_DESC(1, 0x98, "RxPktSz128To255"),
  68. MIB_DESC(1, 0x9c, "RxPktSz256To511"),
  69. MIB_DESC(1, 0xa0, "RxPktSz512To1023"),
  70. MIB_DESC(1, 0xa4, "RxPktSz1024ToMax"),
  71. MIB_DESC(2, 0xa8, "RxBytes"),
  72. MIB_DESC(1, 0xb0, "RxCtrlDrop"),
  73. MIB_DESC(1, 0xb4, "RxIngressDrop"),
  74. MIB_DESC(1, 0xb8, "RxArlDrop"),
  75. };
  76. static int
  77. mt7623_trgmii_write(struct mt7530_priv *priv, u32 reg, u32 val)
  78. {
  79. int ret;
  80. ret = regmap_write(priv->ethernet, TRGMII_BASE(reg), val);
  81. if (ret < 0)
  82. dev_err(priv->dev,
  83. "failed to priv write register\n");
  84. return ret;
  85. }
  86. static u32
  87. mt7623_trgmii_read(struct mt7530_priv *priv, u32 reg)
  88. {
  89. int ret;
  90. u32 val;
  91. ret = regmap_read(priv->ethernet, TRGMII_BASE(reg), &val);
  92. if (ret < 0) {
  93. dev_err(priv->dev,
  94. "failed to priv read register\n");
  95. return ret;
  96. }
  97. return val;
  98. }
  99. static void
  100. mt7623_trgmii_rmw(struct mt7530_priv *priv, u32 reg,
  101. u32 mask, u32 set)
  102. {
  103. u32 val;
  104. val = mt7623_trgmii_read(priv, reg);
  105. val &= ~mask;
  106. val |= set;
  107. mt7623_trgmii_write(priv, reg, val);
  108. }
  109. static void
  110. mt7623_trgmii_set(struct mt7530_priv *priv, u32 reg, u32 val)
  111. {
  112. mt7623_trgmii_rmw(priv, reg, 0, val);
  113. }
  114. static void
  115. mt7623_trgmii_clear(struct mt7530_priv *priv, u32 reg, u32 val)
  116. {
  117. mt7623_trgmii_rmw(priv, reg, val, 0);
  118. }
  119. static int
  120. core_read_mmd_indirect(struct mt7530_priv *priv, int prtad, int devad)
  121. {
  122. struct mii_bus *bus = priv->bus;
  123. int value, ret;
  124. /* Write the desired MMD Devad */
  125. ret = bus->write(bus, 0, MII_MMD_CTRL, devad);
  126. if (ret < 0)
  127. goto err;
  128. /* Write the desired MMD register address */
  129. ret = bus->write(bus, 0, MII_MMD_DATA, prtad);
  130. if (ret < 0)
  131. goto err;
  132. /* Select the Function : DATA with no post increment */
  133. ret = bus->write(bus, 0, MII_MMD_CTRL, (devad | MII_MMD_CTRL_NOINCR));
  134. if (ret < 0)
  135. goto err;
  136. /* Read the content of the MMD's selected register */
  137. value = bus->read(bus, 0, MII_MMD_DATA);
  138. return value;
  139. err:
  140. dev_err(&bus->dev, "failed to read mmd register\n");
  141. return ret;
  142. }
  143. static int
  144. core_write_mmd_indirect(struct mt7530_priv *priv, int prtad,
  145. int devad, u32 data)
  146. {
  147. struct mii_bus *bus = priv->bus;
  148. int ret;
  149. /* Write the desired MMD Devad */
  150. ret = bus->write(bus, 0, MII_MMD_CTRL, devad);
  151. if (ret < 0)
  152. goto err;
  153. /* Write the desired MMD register address */
  154. ret = bus->write(bus, 0, MII_MMD_DATA, prtad);
  155. if (ret < 0)
  156. goto err;
  157. /* Select the Function : DATA with no post increment */
  158. ret = bus->write(bus, 0, MII_MMD_CTRL, (devad | MII_MMD_CTRL_NOINCR));
  159. if (ret < 0)
  160. goto err;
  161. /* Write the data into MMD's selected register */
  162. ret = bus->write(bus, 0, MII_MMD_DATA, data);
  163. err:
  164. if (ret < 0)
  165. dev_err(&bus->dev,
  166. "failed to write mmd register\n");
  167. return ret;
  168. }
  169. static void
  170. core_write(struct mt7530_priv *priv, u32 reg, u32 val)
  171. {
  172. struct mii_bus *bus = priv->bus;
  173. mutex_lock_nested(&bus->mdio_lock, MDIO_MUTEX_NESTED);
  174. core_write_mmd_indirect(priv, reg, MDIO_MMD_VEND2, val);
  175. mutex_unlock(&bus->mdio_lock);
  176. }
  177. static void
  178. core_rmw(struct mt7530_priv *priv, u32 reg, u32 mask, u32 set)
  179. {
  180. struct mii_bus *bus = priv->bus;
  181. u32 val;
  182. mutex_lock_nested(&bus->mdio_lock, MDIO_MUTEX_NESTED);
  183. val = core_read_mmd_indirect(priv, reg, MDIO_MMD_VEND2);
  184. val &= ~mask;
  185. val |= set;
  186. core_write_mmd_indirect(priv, reg, MDIO_MMD_VEND2, val);
  187. mutex_unlock(&bus->mdio_lock);
  188. }
  189. static void
  190. core_set(struct mt7530_priv *priv, u32 reg, u32 val)
  191. {
  192. core_rmw(priv, reg, 0, val);
  193. }
  194. static void
  195. core_clear(struct mt7530_priv *priv, u32 reg, u32 val)
  196. {
  197. core_rmw(priv, reg, val, 0);
  198. }
  199. static int
  200. mt7530_mii_write(struct mt7530_priv *priv, u32 reg, u32 val)
  201. {
  202. struct mii_bus *bus = priv->bus;
  203. u16 page, r, lo, hi;
  204. int ret;
  205. page = (reg >> 6) & 0x3ff;
  206. r = (reg >> 2) & 0xf;
  207. lo = val & 0xffff;
  208. hi = val >> 16;
  209. /* MT7530 uses 31 as the pseudo port */
  210. ret = bus->write(bus, 0x1f, 0x1f, page);
  211. if (ret < 0)
  212. goto err;
  213. ret = bus->write(bus, 0x1f, r, lo);
  214. if (ret < 0)
  215. goto err;
  216. ret = bus->write(bus, 0x1f, 0x10, hi);
  217. err:
  218. if (ret < 0)
  219. dev_err(&bus->dev,
  220. "failed to write mt7530 register\n");
  221. return ret;
  222. }
  223. static u32
  224. mt7530_mii_read(struct mt7530_priv *priv, u32 reg)
  225. {
  226. struct mii_bus *bus = priv->bus;
  227. u16 page, r, lo, hi;
  228. int ret;
  229. page = (reg >> 6) & 0x3ff;
  230. r = (reg >> 2) & 0xf;
  231. /* MT7530 uses 31 as the pseudo port */
  232. ret = bus->write(bus, 0x1f, 0x1f, page);
  233. if (ret < 0) {
  234. dev_err(&bus->dev,
  235. "failed to read mt7530 register\n");
  236. return ret;
  237. }
  238. lo = bus->read(bus, 0x1f, r);
  239. hi = bus->read(bus, 0x1f, 0x10);
  240. return (hi << 16) | (lo & 0xffff);
  241. }
  242. static void
  243. mt7530_write(struct mt7530_priv *priv, u32 reg, u32 val)
  244. {
  245. struct mii_bus *bus = priv->bus;
  246. mutex_lock_nested(&bus->mdio_lock, MDIO_MUTEX_NESTED);
  247. mt7530_mii_write(priv, reg, val);
  248. mutex_unlock(&bus->mdio_lock);
  249. }
  250. static u32
  251. _mt7530_read(struct mt7530_dummy_poll *p)
  252. {
  253. struct mii_bus *bus = p->priv->bus;
  254. u32 val;
  255. mutex_lock_nested(&bus->mdio_lock, MDIO_MUTEX_NESTED);
  256. val = mt7530_mii_read(p->priv, p->reg);
  257. mutex_unlock(&bus->mdio_lock);
  258. return val;
  259. }
  260. static u32
  261. mt7530_read(struct mt7530_priv *priv, u32 reg)
  262. {
  263. struct mt7530_dummy_poll p;
  264. INIT_MT7530_DUMMY_POLL(&p, priv, reg);
  265. return _mt7530_read(&p);
  266. }
  267. static void
  268. mt7530_rmw(struct mt7530_priv *priv, u32 reg,
  269. u32 mask, u32 set)
  270. {
  271. struct mii_bus *bus = priv->bus;
  272. u32 val;
  273. mutex_lock_nested(&bus->mdio_lock, MDIO_MUTEX_NESTED);
  274. val = mt7530_mii_read(priv, reg);
  275. val &= ~mask;
  276. val |= set;
  277. mt7530_mii_write(priv, reg, val);
  278. mutex_unlock(&bus->mdio_lock);
  279. }
  280. static void
  281. mt7530_set(struct mt7530_priv *priv, u32 reg, u32 val)
  282. {
  283. mt7530_rmw(priv, reg, 0, val);
  284. }
  285. static void
  286. mt7530_clear(struct mt7530_priv *priv, u32 reg, u32 val)
  287. {
  288. mt7530_rmw(priv, reg, val, 0);
  289. }
  290. static int
  291. mt7530_fdb_cmd(struct mt7530_priv *priv, enum mt7530_fdb_cmd cmd, u32 *rsp)
  292. {
  293. u32 val;
  294. int ret;
  295. struct mt7530_dummy_poll p;
  296. /* Set the command operating upon the MAC address entries */
  297. val = ATC_BUSY | ATC_MAT(0) | cmd;
  298. mt7530_write(priv, MT7530_ATC, val);
  299. INIT_MT7530_DUMMY_POLL(&p, priv, MT7530_ATC);
  300. ret = readx_poll_timeout(_mt7530_read, &p, val,
  301. !(val & ATC_BUSY), 20, 20000);
  302. if (ret < 0) {
  303. dev_err(priv->dev, "reset timeout\n");
  304. return ret;
  305. }
  306. /* Additional sanity for read command if the specified
  307. * entry is invalid
  308. */
  309. val = mt7530_read(priv, MT7530_ATC);
  310. if ((cmd == MT7530_FDB_READ) && (val & ATC_INVALID))
  311. return -EINVAL;
  312. if (rsp)
  313. *rsp = val;
  314. return 0;
  315. }
  316. static void
  317. mt7530_fdb_read(struct mt7530_priv *priv, struct mt7530_fdb *fdb)
  318. {
  319. u32 reg[3];
  320. int i;
  321. /* Read from ARL table into an array */
  322. for (i = 0; i < 3; i++) {
  323. reg[i] = mt7530_read(priv, MT7530_TSRA1 + (i * 4));
  324. dev_dbg(priv->dev, "%s(%d) reg[%d]=0x%x\n",
  325. __func__, __LINE__, i, reg[i]);
  326. }
  327. fdb->vid = (reg[1] >> CVID) & CVID_MASK;
  328. fdb->aging = (reg[2] >> AGE_TIMER) & AGE_TIMER_MASK;
  329. fdb->port_mask = (reg[2] >> PORT_MAP) & PORT_MAP_MASK;
  330. fdb->mac[0] = (reg[0] >> MAC_BYTE_0) & MAC_BYTE_MASK;
  331. fdb->mac[1] = (reg[0] >> MAC_BYTE_1) & MAC_BYTE_MASK;
  332. fdb->mac[2] = (reg[0] >> MAC_BYTE_2) & MAC_BYTE_MASK;
  333. fdb->mac[3] = (reg[0] >> MAC_BYTE_3) & MAC_BYTE_MASK;
  334. fdb->mac[4] = (reg[1] >> MAC_BYTE_4) & MAC_BYTE_MASK;
  335. fdb->mac[5] = (reg[1] >> MAC_BYTE_5) & MAC_BYTE_MASK;
  336. fdb->noarp = ((reg[2] >> ENT_STATUS) & ENT_STATUS_MASK) == STATIC_ENT;
  337. }
  338. static void
  339. mt7530_fdb_write(struct mt7530_priv *priv, u16 vid,
  340. u8 port_mask, const u8 *mac,
  341. u8 aging, u8 type)
  342. {
  343. u32 reg[3] = { 0 };
  344. int i;
  345. reg[1] |= vid & CVID_MASK;
  346. reg[2] |= (aging & AGE_TIMER_MASK) << AGE_TIMER;
  347. reg[2] |= (port_mask & PORT_MAP_MASK) << PORT_MAP;
  348. /* STATIC_ENT indicate that entry is static wouldn't
  349. * be aged out and STATIC_EMP specified as erasing an
  350. * entry
  351. */
  352. reg[2] |= (type & ENT_STATUS_MASK) << ENT_STATUS;
  353. reg[1] |= mac[5] << MAC_BYTE_5;
  354. reg[1] |= mac[4] << MAC_BYTE_4;
  355. reg[0] |= mac[3] << MAC_BYTE_3;
  356. reg[0] |= mac[2] << MAC_BYTE_2;
  357. reg[0] |= mac[1] << MAC_BYTE_1;
  358. reg[0] |= mac[0] << MAC_BYTE_0;
  359. /* Write array into the ARL table */
  360. for (i = 0; i < 3; i++)
  361. mt7530_write(priv, MT7530_ATA1 + (i * 4), reg[i]);
  362. }
  363. static int
  364. mt7530_pad_clk_setup(struct dsa_switch *ds, int mode)
  365. {
  366. struct mt7530_priv *priv = ds->priv;
  367. u32 ncpo1, ssc_delta, trgint, i;
  368. switch (mode) {
  369. case PHY_INTERFACE_MODE_RGMII:
  370. trgint = 0;
  371. ncpo1 = 0x0c80;
  372. ssc_delta = 0x87;
  373. break;
  374. case PHY_INTERFACE_MODE_TRGMII:
  375. trgint = 1;
  376. ncpo1 = 0x1400;
  377. ssc_delta = 0x57;
  378. break;
  379. default:
  380. dev_err(priv->dev, "xMII mode %d not supported\n", mode);
  381. return -EINVAL;
  382. }
  383. mt7530_rmw(priv, MT7530_P6ECR, P6_INTF_MODE_MASK,
  384. P6_INTF_MODE(trgint));
  385. /* Lower Tx Driving for TRGMII path */
  386. for (i = 0 ; i < NUM_TRGMII_CTRL ; i++)
  387. mt7530_write(priv, MT7530_TRGMII_TD_ODT(i),
  388. TD_DM_DRVP(8) | TD_DM_DRVN(8));
  389. /* Setup core clock for MT7530 */
  390. if (!trgint) {
  391. /* Disable MT7530 core clock */
  392. core_clear(priv, CORE_TRGMII_GSW_CLK_CG, REG_GSWCK_EN);
  393. /* Disable PLL, since phy_device has not yet been created
  394. * provided for phy_[read,write]_mmd_indirect is called, we
  395. * provide our own core_write_mmd_indirect to complete this
  396. * function.
  397. */
  398. core_write_mmd_indirect(priv,
  399. CORE_GSWPLL_GRP1,
  400. MDIO_MMD_VEND2,
  401. 0);
  402. /* Set core clock into 500Mhz */
  403. core_write(priv, CORE_GSWPLL_GRP2,
  404. RG_GSWPLL_POSDIV_500M(1) |
  405. RG_GSWPLL_FBKDIV_500M(25));
  406. /* Enable PLL */
  407. core_write(priv, CORE_GSWPLL_GRP1,
  408. RG_GSWPLL_EN_PRE |
  409. RG_GSWPLL_POSDIV_200M(2) |
  410. RG_GSWPLL_FBKDIV_200M(32));
  411. /* Enable MT7530 core clock */
  412. core_set(priv, CORE_TRGMII_GSW_CLK_CG, REG_GSWCK_EN);
  413. }
  414. /* Setup the MT7530 TRGMII Tx Clock */
  415. core_set(priv, CORE_TRGMII_GSW_CLK_CG, REG_GSWCK_EN);
  416. core_write(priv, CORE_PLL_GROUP5, RG_LCDDS_PCW_NCPO1(ncpo1));
  417. core_write(priv, CORE_PLL_GROUP6, RG_LCDDS_PCW_NCPO0(0));
  418. core_write(priv, CORE_PLL_GROUP10, RG_LCDDS_SSC_DELTA(ssc_delta));
  419. core_write(priv, CORE_PLL_GROUP11, RG_LCDDS_SSC_DELTA1(ssc_delta));
  420. core_write(priv, CORE_PLL_GROUP4,
  421. RG_SYSPLL_DDSFBK_EN | RG_SYSPLL_BIAS_EN |
  422. RG_SYSPLL_BIAS_LPF_EN);
  423. core_write(priv, CORE_PLL_GROUP2,
  424. RG_SYSPLL_EN_NORMAL | RG_SYSPLL_VODEN |
  425. RG_SYSPLL_POSDIV(1));
  426. core_write(priv, CORE_PLL_GROUP7,
  427. RG_LCDDS_PCW_NCPO_CHG | RG_LCCDS_C(3) |
  428. RG_LCDDS_PWDB | RG_LCDDS_ISO_EN);
  429. core_set(priv, CORE_TRGMII_GSW_CLK_CG,
  430. REG_GSWCK_EN | REG_TRGMIICK_EN);
  431. if (!trgint)
  432. for (i = 0 ; i < NUM_TRGMII_CTRL; i++)
  433. mt7530_rmw(priv, MT7530_TRGMII_RD(i),
  434. RD_TAP_MASK, RD_TAP(16));
  435. else
  436. mt7623_trgmii_set(priv, GSW_INTF_MODE, INTF_MODE_TRGMII);
  437. return 0;
  438. }
  439. static int
  440. mt7623_pad_clk_setup(struct dsa_switch *ds)
  441. {
  442. struct mt7530_priv *priv = ds->priv;
  443. int i;
  444. for (i = 0 ; i < NUM_TRGMII_CTRL; i++)
  445. mt7623_trgmii_write(priv, GSW_TRGMII_TD_ODT(i),
  446. TD_DM_DRVP(8) | TD_DM_DRVN(8));
  447. mt7623_trgmii_set(priv, GSW_TRGMII_RCK_CTRL, RX_RST | RXC_DQSISEL);
  448. mt7623_trgmii_clear(priv, GSW_TRGMII_RCK_CTRL, RX_RST);
  449. return 0;
  450. }
  451. static void
  452. mt7530_mib_reset(struct dsa_switch *ds)
  453. {
  454. struct mt7530_priv *priv = ds->priv;
  455. mt7530_write(priv, MT7530_MIB_CCR, CCR_MIB_FLUSH);
  456. mt7530_write(priv, MT7530_MIB_CCR, CCR_MIB_ACTIVATE);
  457. }
  458. static void
  459. mt7530_port_set_status(struct mt7530_priv *priv, int port, int enable)
  460. {
  461. u32 mask = PMCR_TX_EN | PMCR_RX_EN;
  462. if (enable)
  463. mt7530_set(priv, MT7530_PMCR_P(port), mask);
  464. else
  465. mt7530_clear(priv, MT7530_PMCR_P(port), mask);
  466. }
  467. static int mt7530_phy_read(struct dsa_switch *ds, int port, int regnum)
  468. {
  469. struct mt7530_priv *priv = ds->priv;
  470. return mdiobus_read_nested(priv->bus, port, regnum);
  471. }
  472. int mt7530_phy_write(struct dsa_switch *ds, int port, int regnum, 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, uint8_t *data)
  479. {
  480. int i;
  481. for (i = 0; i < ARRAY_SIZE(mt7530_mib); i++)
  482. strncpy(data + i * ETH_GSTRING_LEN, mt7530_mib[i].name,
  483. ETH_GSTRING_LEN);
  484. }
  485. static void
  486. mt7530_get_ethtool_stats(struct dsa_switch *ds, int port,
  487. uint64_t *data)
  488. {
  489. struct mt7530_priv *priv = ds->priv;
  490. const struct mt7530_mib_desc *mib;
  491. u32 reg, i;
  492. u64 hi;
  493. for (i = 0; i < ARRAY_SIZE(mt7530_mib); i++) {
  494. mib = &mt7530_mib[i];
  495. reg = MT7530_PORT_MIB_COUNTER(port) + mib->offset;
  496. data[i] = mt7530_read(priv, reg);
  497. if (mib->size == 2) {
  498. hi = mt7530_read(priv, reg + 4);
  499. data[i] |= hi << 32;
  500. }
  501. }
  502. }
  503. static int
  504. mt7530_get_sset_count(struct dsa_switch *ds)
  505. {
  506. return ARRAY_SIZE(mt7530_mib);
  507. }
  508. static void mt7530_adjust_link(struct dsa_switch *ds, int port,
  509. struct phy_device *phydev)
  510. {
  511. struct mt7530_priv *priv = ds->priv;
  512. if (phy_is_pseudo_fixed_link(phydev)) {
  513. dev_dbg(priv->dev, "phy-mode for master device = %x\n",
  514. phydev->interface);
  515. /* Setup TX circuit incluing relevant PAD and driving */
  516. mt7530_pad_clk_setup(ds, phydev->interface);
  517. /* Setup RX circuit, relevant PAD and driving on the host
  518. * which must be placed after the setup on the device side is
  519. * all finished.
  520. */
  521. mt7623_pad_clk_setup(ds);
  522. }
  523. }
  524. static int
  525. mt7530_cpu_port_enable(struct mt7530_priv *priv,
  526. int port)
  527. {
  528. /* Enable Mediatek header mode on the cpu port */
  529. mt7530_write(priv, MT7530_PVC_P(port),
  530. PORT_SPEC_TAG);
  531. /* Setup the MAC by default for the cpu port */
  532. mt7530_write(priv, MT7530_PMCR_P(port), PMCR_CPUP_LINK);
  533. /* Disable auto learning on the cpu port */
  534. mt7530_set(priv, MT7530_PSC_P(port), SA_DIS);
  535. /* Unknown unicast frame fordwarding to the cpu port */
  536. mt7530_set(priv, MT7530_MFC, UNU_FFP(BIT(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(priv->ds->enabled_port_mask));
  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. mutex_lock(&priv->reg_mutex);
  550. /* Setup the MAC for the user port */
  551. mt7530_write(priv, MT7530_PMCR_P(port), PMCR_USERP_LINK);
  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, 1);
  561. mutex_unlock(&priv->reg_mutex);
  562. return 0;
  563. }
  564. static void
  565. mt7530_port_disable(struct dsa_switch *ds, int port,
  566. struct phy_device *phy)
  567. {
  568. struct mt7530_priv *priv = ds->priv;
  569. mutex_lock(&priv->reg_mutex);
  570. /* Clear up all port matrix which could be restored in the next
  571. * enablement for the port.
  572. */
  573. priv->ports[port].enable = false;
  574. mt7530_rmw(priv, MT7530_PCR_P(port), PCR_MATRIX_MASK,
  575. PCR_MATRIX_CLR);
  576. mt7530_port_set_status(priv, port, 0);
  577. mutex_unlock(&priv->reg_mutex);
  578. }
  579. static void
  580. mt7530_stp_state_set(struct dsa_switch *ds, int port, u8 state)
  581. {
  582. struct mt7530_priv *priv = ds->priv;
  583. u32 stp_state;
  584. switch (state) {
  585. case BR_STATE_DISABLED:
  586. stp_state = MT7530_STP_DISABLED;
  587. break;
  588. case BR_STATE_BLOCKING:
  589. stp_state = MT7530_STP_BLOCKING;
  590. break;
  591. case BR_STATE_LISTENING:
  592. stp_state = MT7530_STP_LISTENING;
  593. break;
  594. case BR_STATE_LEARNING:
  595. stp_state = MT7530_STP_LEARNING;
  596. break;
  597. case BR_STATE_FORWARDING:
  598. default:
  599. stp_state = MT7530_STP_FORWARDING;
  600. break;
  601. }
  602. mt7530_rmw(priv, MT7530_SSP_P(port), FID_PST_MASK, stp_state);
  603. }
  604. static int
  605. mt7530_port_bridge_join(struct dsa_switch *ds, int port,
  606. struct net_device *bridge)
  607. {
  608. struct mt7530_priv *priv = ds->priv;
  609. u32 port_bitmap = BIT(MT7530_CPU_PORT);
  610. int i;
  611. mutex_lock(&priv->reg_mutex);
  612. for (i = 0; i < MT7530_NUM_PORTS; i++) {
  613. /* Add this port to the port matrix of the other ports in the
  614. * same bridge. If the port is disabled, port matrix is kept
  615. * and not being setup until the port becomes enabled.
  616. */
  617. if (ds->enabled_port_mask & BIT(i) && i != port) {
  618. if (ds->ports[i].bridge_dev != bridge)
  619. continue;
  620. if (priv->ports[i].enable)
  621. mt7530_set(priv, MT7530_PCR_P(i),
  622. PCR_MATRIX(BIT(port)));
  623. priv->ports[i].pm |= PCR_MATRIX(BIT(port));
  624. port_bitmap |= BIT(i);
  625. }
  626. }
  627. /* Add the all other ports to this port matrix. */
  628. if (priv->ports[port].enable)
  629. mt7530_rmw(priv, MT7530_PCR_P(port),
  630. PCR_MATRIX_MASK, PCR_MATRIX(port_bitmap));
  631. priv->ports[port].pm |= PCR_MATRIX(port_bitmap);
  632. mutex_unlock(&priv->reg_mutex);
  633. return 0;
  634. }
  635. static void
  636. mt7530_port_bridge_leave(struct dsa_switch *ds, int port,
  637. struct net_device *bridge)
  638. {
  639. struct mt7530_priv *priv = ds->priv;
  640. int i;
  641. mutex_lock(&priv->reg_mutex);
  642. for (i = 0; i < MT7530_NUM_PORTS; i++) {
  643. /* Remove this port from the port matrix of the other ports
  644. * in the same bridge. If the port is disabled, port matrix
  645. * is kept and not being setup until the port becomes enabled.
  646. */
  647. if (ds->enabled_port_mask & BIT(i) && i != port) {
  648. if (ds->ports[i].bridge_dev != bridge)
  649. continue;
  650. if (priv->ports[i].enable)
  651. mt7530_clear(priv, MT7530_PCR_P(i),
  652. PCR_MATRIX(BIT(port)));
  653. priv->ports[i].pm &= ~PCR_MATRIX(BIT(port));
  654. }
  655. }
  656. /* Set the cpu port to be the only one in the port matrix of
  657. * this port.
  658. */
  659. if (priv->ports[port].enable)
  660. mt7530_rmw(priv, MT7530_PCR_P(port), PCR_MATRIX_MASK,
  661. PCR_MATRIX(BIT(MT7530_CPU_PORT)));
  662. priv->ports[port].pm = PCR_MATRIX(BIT(MT7530_CPU_PORT));
  663. mutex_unlock(&priv->reg_mutex);
  664. }
  665. static int
  666. mt7530_port_fdb_prepare(struct dsa_switch *ds, int port,
  667. const struct switchdev_obj_port_fdb *fdb,
  668. struct switchdev_trans *trans)
  669. {
  670. struct mt7530_priv *priv = ds->priv;
  671. int ret;
  672. /* Because auto-learned entrie shares the same FDB table.
  673. * an entry is reserved with no port_mask to make sure fdb_add
  674. * is called while the entry is still available.
  675. */
  676. mutex_lock(&priv->reg_mutex);
  677. mt7530_fdb_write(priv, fdb->vid, 0, fdb->addr, -1, STATIC_ENT);
  678. ret = mt7530_fdb_cmd(priv, MT7530_FDB_WRITE, 0);
  679. mutex_unlock(&priv->reg_mutex);
  680. return ret;
  681. }
  682. static void
  683. mt7530_port_fdb_add(struct dsa_switch *ds, int port,
  684. const struct switchdev_obj_port_fdb *fdb,
  685. struct switchdev_trans *trans)
  686. {
  687. struct mt7530_priv *priv = ds->priv;
  688. u8 port_mask = BIT(port);
  689. mutex_lock(&priv->reg_mutex);
  690. mt7530_fdb_write(priv, fdb->vid, port_mask, fdb->addr, -1, STATIC_ENT);
  691. mt7530_fdb_cmd(priv, MT7530_FDB_WRITE, 0);
  692. mutex_unlock(&priv->reg_mutex);
  693. }
  694. static int
  695. mt7530_port_fdb_del(struct dsa_switch *ds, int port,
  696. const struct switchdev_obj_port_fdb *fdb)
  697. {
  698. struct mt7530_priv *priv = ds->priv;
  699. int ret;
  700. u8 port_mask = BIT(port);
  701. mutex_lock(&priv->reg_mutex);
  702. mt7530_fdb_write(priv, fdb->vid, port_mask, fdb->addr, -1, STATIC_EMP);
  703. ret = mt7530_fdb_cmd(priv, MT7530_FDB_WRITE, 0);
  704. mutex_unlock(&priv->reg_mutex);
  705. return ret;
  706. }
  707. static int
  708. mt7530_port_fdb_dump(struct dsa_switch *ds, int port,
  709. struct switchdev_obj_port_fdb *fdb,
  710. int (*cb)(struct switchdev_obj *obj))
  711. {
  712. struct mt7530_priv *priv = ds->priv;
  713. struct mt7530_fdb _fdb = { 0 };
  714. int cnt = MT7530_NUM_FDB_RECORDS;
  715. int ret = 0;
  716. u32 rsp = 0;
  717. mutex_lock(&priv->reg_mutex);
  718. ret = mt7530_fdb_cmd(priv, MT7530_FDB_START, &rsp);
  719. if (ret < 0)
  720. goto err;
  721. do {
  722. if (rsp & ATC_SRCH_HIT) {
  723. mt7530_fdb_read(priv, &_fdb);
  724. if (_fdb.port_mask & BIT(port)) {
  725. ether_addr_copy(fdb->addr, _fdb.mac);
  726. fdb->vid = _fdb.vid;
  727. fdb->ndm_state = _fdb.noarp ?
  728. NUD_NOARP : NUD_REACHABLE;
  729. ret = cb(&fdb->obj);
  730. if (ret < 0)
  731. break;
  732. }
  733. }
  734. } while (--cnt &&
  735. !(rsp & ATC_SRCH_END) &&
  736. !mt7530_fdb_cmd(priv, MT7530_FDB_NEXT, &rsp));
  737. err:
  738. mutex_unlock(&priv->reg_mutex);
  739. return 0;
  740. }
  741. static enum dsa_tag_protocol
  742. mtk_get_tag_protocol(struct dsa_switch *ds)
  743. {
  744. struct mt7530_priv *priv = ds->priv;
  745. if (!dsa_is_cpu_port(ds, MT7530_CPU_PORT)) {
  746. dev_warn(priv->dev,
  747. "port not matched with tagging CPU port\n");
  748. return DSA_TAG_PROTO_NONE;
  749. } else {
  750. return DSA_TAG_PROTO_MTK;
  751. }
  752. }
  753. static int
  754. mt7530_setup(struct dsa_switch *ds)
  755. {
  756. struct mt7530_priv *priv = ds->priv;
  757. int ret, i;
  758. u32 id, val;
  759. struct device_node *dn;
  760. struct mt7530_dummy_poll p;
  761. /* The parent node of master_netdev which holds the common system
  762. * controller also is the container for two GMACs nodes representing
  763. * as two netdev instances.
  764. */
  765. dn = ds->master_netdev->dev.of_node->parent;
  766. priv->ethernet = syscon_node_to_regmap(dn);
  767. if (IS_ERR(priv->ethernet))
  768. return PTR_ERR(priv->ethernet);
  769. regulator_set_voltage(priv->core_pwr, 1000000, 1000000);
  770. ret = regulator_enable(priv->core_pwr);
  771. if (ret < 0) {
  772. dev_err(priv->dev,
  773. "Failed to enable core power: %d\n", ret);
  774. return ret;
  775. }
  776. regulator_set_voltage(priv->io_pwr, 3300000, 3300000);
  777. ret = regulator_enable(priv->io_pwr);
  778. if (ret < 0) {
  779. dev_err(priv->dev, "Failed to enable io pwr: %d\n",
  780. ret);
  781. return ret;
  782. }
  783. /* Reset whole chip through gpio pin or memory-mapped registers for
  784. * different type of hardware
  785. */
  786. if (priv->mcm) {
  787. reset_control_assert(priv->rstc);
  788. usleep_range(1000, 1100);
  789. reset_control_deassert(priv->rstc);
  790. } else {
  791. gpiod_set_value_cansleep(priv->reset, 0);
  792. usleep_range(1000, 1100);
  793. gpiod_set_value_cansleep(priv->reset, 1);
  794. }
  795. /* Waiting for MT7530 got to stable */
  796. INIT_MT7530_DUMMY_POLL(&p, priv, MT7530_HWTRAP);
  797. ret = readx_poll_timeout(_mt7530_read, &p, val, val != 0,
  798. 20, 1000000);
  799. if (ret < 0) {
  800. dev_err(priv->dev, "reset timeout\n");
  801. return ret;
  802. }
  803. id = mt7530_read(priv, MT7530_CREV);
  804. id >>= CHIP_NAME_SHIFT;
  805. if (id != MT7530_ID) {
  806. dev_err(priv->dev, "chip %x can't be supported\n", id);
  807. return -ENODEV;
  808. }
  809. /* Reset the switch through internal reset */
  810. mt7530_write(priv, MT7530_SYS_CTRL,
  811. SYS_CTRL_PHY_RST | SYS_CTRL_SW_RST |
  812. SYS_CTRL_REG_RST);
  813. /* Enable Port 6 only; P5 as GMAC5 which currently is not supported */
  814. val = mt7530_read(priv, MT7530_MHWTRAP);
  815. val &= ~MHWTRAP_P6_DIS & ~MHWTRAP_PHY_ACCESS;
  816. val |= MHWTRAP_MANUAL;
  817. mt7530_write(priv, MT7530_MHWTRAP, val);
  818. /* Enable and reset MIB counters */
  819. mt7530_mib_reset(ds);
  820. mt7530_clear(priv, MT7530_MFC, UNU_FFP_MASK);
  821. for (i = 0; i < MT7530_NUM_PORTS; i++) {
  822. /* Disable forwarding by default on all ports */
  823. mt7530_rmw(priv, MT7530_PCR_P(i), PCR_MATRIX_MASK,
  824. PCR_MATRIX_CLR);
  825. if (dsa_is_cpu_port(ds, i))
  826. mt7530_cpu_port_enable(priv, i);
  827. else
  828. mt7530_port_disable(ds, i, NULL);
  829. }
  830. /* Flush the FDB table */
  831. ret = mt7530_fdb_cmd(priv, MT7530_FDB_FLUSH, 0);
  832. if (ret < 0)
  833. return ret;
  834. return 0;
  835. }
  836. static struct dsa_switch_ops mt7530_switch_ops = {
  837. .get_tag_protocol = mtk_get_tag_protocol,
  838. .setup = mt7530_setup,
  839. .get_strings = mt7530_get_strings,
  840. .phy_read = mt7530_phy_read,
  841. .phy_write = mt7530_phy_write,
  842. .get_ethtool_stats = mt7530_get_ethtool_stats,
  843. .get_sset_count = mt7530_get_sset_count,
  844. .adjust_link = mt7530_adjust_link,
  845. .port_enable = mt7530_port_enable,
  846. .port_disable = mt7530_port_disable,
  847. .port_stp_state_set = mt7530_stp_state_set,
  848. .port_bridge_join = mt7530_port_bridge_join,
  849. .port_bridge_leave = mt7530_port_bridge_leave,
  850. .port_fdb_prepare = mt7530_port_fdb_prepare,
  851. .port_fdb_add = mt7530_port_fdb_add,
  852. .port_fdb_del = mt7530_port_fdb_del,
  853. .port_fdb_dump = mt7530_port_fdb_dump,
  854. };
  855. static int
  856. mt7530_probe(struct mdio_device *mdiodev)
  857. {
  858. struct mt7530_priv *priv;
  859. struct device_node *dn;
  860. dn = mdiodev->dev.of_node;
  861. priv = devm_kzalloc(&mdiodev->dev, sizeof(*priv), GFP_KERNEL);
  862. if (!priv)
  863. return -ENOMEM;
  864. priv->ds = dsa_switch_alloc(&mdiodev->dev, DSA_MAX_PORTS);
  865. if (!priv->ds)
  866. return -ENOMEM;
  867. /* Use medatek,mcm property to distinguish hardware type that would
  868. * casues a little bit differences on power-on sequence.
  869. */
  870. priv->mcm = of_property_read_bool(dn, "mediatek,mcm");
  871. if (priv->mcm) {
  872. dev_info(&mdiodev->dev, "MT7530 adapts as multi-chip module\n");
  873. priv->rstc = devm_reset_control_get(&mdiodev->dev, "mcm");
  874. if (IS_ERR(priv->rstc)) {
  875. dev_err(&mdiodev->dev, "Couldn't get our reset line\n");
  876. return PTR_ERR(priv->rstc);
  877. }
  878. }
  879. priv->core_pwr = devm_regulator_get(&mdiodev->dev, "core");
  880. if (IS_ERR(priv->core_pwr))
  881. return PTR_ERR(priv->core_pwr);
  882. priv->io_pwr = devm_regulator_get(&mdiodev->dev, "io");
  883. if (IS_ERR(priv->io_pwr))
  884. return PTR_ERR(priv->io_pwr);
  885. /* Not MCM that indicates switch works as the remote standalone
  886. * integrated circuit so the GPIO pin would be used to complete
  887. * the reset, otherwise memory-mapped register accessing used
  888. * through syscon provides in the case of MCM.
  889. */
  890. if (!priv->mcm) {
  891. priv->reset = devm_gpiod_get_optional(&mdiodev->dev, "reset",
  892. GPIOD_OUT_LOW);
  893. if (IS_ERR(priv->reset)) {
  894. dev_err(&mdiodev->dev, "Couldn't get our reset line\n");
  895. return PTR_ERR(priv->reset);
  896. }
  897. }
  898. priv->bus = mdiodev->bus;
  899. priv->dev = &mdiodev->dev;
  900. priv->ds->priv = priv;
  901. priv->ds->ops = &mt7530_switch_ops;
  902. mutex_init(&priv->reg_mutex);
  903. dev_set_drvdata(&mdiodev->dev, priv);
  904. return dsa_register_switch(priv->ds, &mdiodev->dev);
  905. }
  906. static void
  907. mt7530_remove(struct mdio_device *mdiodev)
  908. {
  909. struct mt7530_priv *priv = dev_get_drvdata(&mdiodev->dev);
  910. int ret = 0;
  911. ret = regulator_disable(priv->core_pwr);
  912. if (ret < 0)
  913. dev_err(priv->dev,
  914. "Failed to disable core power: %d\n", ret);
  915. ret = regulator_disable(priv->io_pwr);
  916. if (ret < 0)
  917. dev_err(priv->dev, "Failed to disable io pwr: %d\n",
  918. ret);
  919. dsa_unregister_switch(priv->ds);
  920. mutex_destroy(&priv->reg_mutex);
  921. }
  922. static const struct of_device_id mt7530_of_match[] = {
  923. { .compatible = "mediatek,mt7530" },
  924. { /* sentinel */ },
  925. };
  926. static struct mdio_driver mt7530_mdio_driver = {
  927. .probe = mt7530_probe,
  928. .remove = mt7530_remove,
  929. .mdiodrv.driver = {
  930. .name = "mt7530",
  931. .of_match_table = mt7530_of_match,
  932. },
  933. };
  934. mdio_module_driver(mt7530_mdio_driver);
  935. MODULE_AUTHOR("Sean Wang <sean.wang@mediatek.com>");
  936. MODULE_DESCRIPTION("Driver for Mediatek MT7530 Switch");
  937. MODULE_LICENSE("GPL");
  938. MODULE_ALIAS("platform:mediatek-mt7530");