mt7530.c 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141
  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;
  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, 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. } else {
  523. u16 lcl_adv = 0, rmt_adv = 0;
  524. u8 flowctrl;
  525. u32 mcr = PMCR_USERP_LINK | PMCR_FORCE_MODE;
  526. switch (phydev->speed) {
  527. case SPEED_1000:
  528. mcr |= PMCR_FORCE_SPEED_1000;
  529. break;
  530. case SPEED_100:
  531. mcr |= PMCR_FORCE_SPEED_100;
  532. break;
  533. };
  534. if (phydev->link)
  535. mcr |= PMCR_FORCE_LNK;
  536. if (phydev->duplex) {
  537. mcr |= PMCR_FORCE_FDX;
  538. if (phydev->pause)
  539. rmt_adv = LPA_PAUSE_CAP;
  540. if (phydev->asym_pause)
  541. rmt_adv |= LPA_PAUSE_ASYM;
  542. if (phydev->advertising & ADVERTISED_Pause)
  543. lcl_adv |= ADVERTISE_PAUSE_CAP;
  544. if (phydev->advertising & ADVERTISED_Asym_Pause)
  545. lcl_adv |= ADVERTISE_PAUSE_ASYM;
  546. flowctrl = mii_resolve_flowctrl_fdx(lcl_adv, rmt_adv);
  547. if (flowctrl & FLOW_CTRL_TX)
  548. mcr |= PMCR_TX_FC_EN;
  549. if (flowctrl & FLOW_CTRL_RX)
  550. mcr |= PMCR_RX_FC_EN;
  551. }
  552. mt7530_write(priv, MT7530_PMCR_P(port), mcr);
  553. }
  554. }
  555. static int
  556. mt7530_cpu_port_enable(struct mt7530_priv *priv,
  557. int port)
  558. {
  559. /* Enable Mediatek header mode on the cpu port */
  560. mt7530_write(priv, MT7530_PVC_P(port),
  561. PORT_SPEC_TAG);
  562. /* Setup the MAC by default for the cpu port */
  563. mt7530_write(priv, MT7530_PMCR_P(port), PMCR_CPUP_LINK);
  564. /* Disable auto learning on the cpu port */
  565. mt7530_set(priv, MT7530_PSC_P(port), SA_DIS);
  566. /* Unknown unicast frame fordwarding to the cpu port */
  567. mt7530_set(priv, MT7530_MFC, UNU_FFP(BIT(port)));
  568. /* CPU port gets connected to all user ports of
  569. * the switch
  570. */
  571. mt7530_write(priv, MT7530_PCR_P(port),
  572. PCR_MATRIX(dsa_user_ports(priv->ds)));
  573. return 0;
  574. }
  575. static int
  576. mt7530_port_enable(struct dsa_switch *ds, int port,
  577. struct phy_device *phy)
  578. {
  579. struct mt7530_priv *priv = ds->priv;
  580. mutex_lock(&priv->reg_mutex);
  581. /* Setup the MAC for the user port */
  582. mt7530_write(priv, MT7530_PMCR_P(port), PMCR_USERP_LINK);
  583. /* Allow the user port gets connected to the cpu port and also
  584. * restore the port matrix if the port is the member of a certain
  585. * bridge.
  586. */
  587. priv->ports[port].pm |= PCR_MATRIX(BIT(MT7530_CPU_PORT));
  588. priv->ports[port].enable = true;
  589. mt7530_rmw(priv, MT7530_PCR_P(port), PCR_MATRIX_MASK,
  590. priv->ports[port].pm);
  591. mt7530_port_set_status(priv, port, 1);
  592. mutex_unlock(&priv->reg_mutex);
  593. return 0;
  594. }
  595. static void
  596. mt7530_port_disable(struct dsa_switch *ds, int port,
  597. struct phy_device *phy)
  598. {
  599. struct mt7530_priv *priv = ds->priv;
  600. mutex_lock(&priv->reg_mutex);
  601. /* Clear up all port matrix which could be restored in the next
  602. * enablement for the port.
  603. */
  604. priv->ports[port].enable = false;
  605. mt7530_rmw(priv, MT7530_PCR_P(port), PCR_MATRIX_MASK,
  606. PCR_MATRIX_CLR);
  607. mt7530_port_set_status(priv, port, 0);
  608. mutex_unlock(&priv->reg_mutex);
  609. }
  610. static void
  611. mt7530_stp_state_set(struct dsa_switch *ds, int port, u8 state)
  612. {
  613. struct mt7530_priv *priv = ds->priv;
  614. u32 stp_state;
  615. switch (state) {
  616. case BR_STATE_DISABLED:
  617. stp_state = MT7530_STP_DISABLED;
  618. break;
  619. case BR_STATE_BLOCKING:
  620. stp_state = MT7530_STP_BLOCKING;
  621. break;
  622. case BR_STATE_LISTENING:
  623. stp_state = MT7530_STP_LISTENING;
  624. break;
  625. case BR_STATE_LEARNING:
  626. stp_state = MT7530_STP_LEARNING;
  627. break;
  628. case BR_STATE_FORWARDING:
  629. default:
  630. stp_state = MT7530_STP_FORWARDING;
  631. break;
  632. }
  633. mt7530_rmw(priv, MT7530_SSP_P(port), FID_PST_MASK, stp_state);
  634. }
  635. static int
  636. mt7530_port_bridge_join(struct dsa_switch *ds, int port,
  637. struct net_device *bridge)
  638. {
  639. struct mt7530_priv *priv = ds->priv;
  640. u32 port_bitmap = BIT(MT7530_CPU_PORT);
  641. int i;
  642. mutex_lock(&priv->reg_mutex);
  643. for (i = 0; i < MT7530_NUM_PORTS; i++) {
  644. /* Add this port to the port matrix of the other ports in the
  645. * same bridge. If the port is disabled, port matrix is kept
  646. * and not being setup until the port becomes enabled.
  647. */
  648. if (dsa_is_user_port(ds, i) && i != port) {
  649. if (dsa_to_port(ds, i)->bridge_dev != bridge)
  650. continue;
  651. if (priv->ports[i].enable)
  652. mt7530_set(priv, MT7530_PCR_P(i),
  653. PCR_MATRIX(BIT(port)));
  654. priv->ports[i].pm |= PCR_MATRIX(BIT(port));
  655. port_bitmap |= BIT(i);
  656. }
  657. }
  658. /* Add the all other ports to this port matrix. */
  659. if (priv->ports[port].enable)
  660. mt7530_rmw(priv, MT7530_PCR_P(port),
  661. PCR_MATRIX_MASK, PCR_MATRIX(port_bitmap));
  662. priv->ports[port].pm |= PCR_MATRIX(port_bitmap);
  663. mutex_unlock(&priv->reg_mutex);
  664. return 0;
  665. }
  666. static void
  667. mt7530_port_bridge_leave(struct dsa_switch *ds, int port,
  668. struct net_device *bridge)
  669. {
  670. struct mt7530_priv *priv = ds->priv;
  671. int i;
  672. mutex_lock(&priv->reg_mutex);
  673. for (i = 0; i < MT7530_NUM_PORTS; i++) {
  674. /* Remove this port from the port matrix of the other ports
  675. * in the same bridge. If the port is disabled, port matrix
  676. * is kept and not being setup until the port becomes enabled.
  677. */
  678. if (dsa_is_user_port(ds, i) && i != port) {
  679. if (dsa_to_port(ds, i)->bridge_dev != bridge)
  680. continue;
  681. if (priv->ports[i].enable)
  682. mt7530_clear(priv, MT7530_PCR_P(i),
  683. PCR_MATRIX(BIT(port)));
  684. priv->ports[i].pm &= ~PCR_MATRIX(BIT(port));
  685. }
  686. }
  687. /* Set the cpu port to be the only one in the port matrix of
  688. * this port.
  689. */
  690. if (priv->ports[port].enable)
  691. mt7530_rmw(priv, MT7530_PCR_P(port), PCR_MATRIX_MASK,
  692. PCR_MATRIX(BIT(MT7530_CPU_PORT)));
  693. priv->ports[port].pm = PCR_MATRIX(BIT(MT7530_CPU_PORT));
  694. mutex_unlock(&priv->reg_mutex);
  695. }
  696. static int
  697. mt7530_port_fdb_add(struct dsa_switch *ds, int port,
  698. const unsigned char *addr, u16 vid)
  699. {
  700. struct mt7530_priv *priv = ds->priv;
  701. int ret;
  702. u8 port_mask = BIT(port);
  703. mutex_lock(&priv->reg_mutex);
  704. mt7530_fdb_write(priv, vid, port_mask, addr, -1, STATIC_ENT);
  705. ret = mt7530_fdb_cmd(priv, MT7530_FDB_WRITE, 0);
  706. mutex_unlock(&priv->reg_mutex);
  707. return ret;
  708. }
  709. static int
  710. mt7530_port_fdb_del(struct dsa_switch *ds, int port,
  711. const unsigned char *addr, u16 vid)
  712. {
  713. struct mt7530_priv *priv = ds->priv;
  714. int ret;
  715. u8 port_mask = BIT(port);
  716. mutex_lock(&priv->reg_mutex);
  717. mt7530_fdb_write(priv, vid, port_mask, addr, -1, STATIC_EMP);
  718. ret = mt7530_fdb_cmd(priv, MT7530_FDB_WRITE, 0);
  719. mutex_unlock(&priv->reg_mutex);
  720. return ret;
  721. }
  722. static int
  723. mt7530_port_fdb_dump(struct dsa_switch *ds, int port,
  724. dsa_fdb_dump_cb_t *cb, void *data)
  725. {
  726. struct mt7530_priv *priv = ds->priv;
  727. struct mt7530_fdb _fdb = { 0 };
  728. int cnt = MT7530_NUM_FDB_RECORDS;
  729. int ret = 0;
  730. u32 rsp = 0;
  731. mutex_lock(&priv->reg_mutex);
  732. ret = mt7530_fdb_cmd(priv, MT7530_FDB_START, &rsp);
  733. if (ret < 0)
  734. goto err;
  735. do {
  736. if (rsp & ATC_SRCH_HIT) {
  737. mt7530_fdb_read(priv, &_fdb);
  738. if (_fdb.port_mask & BIT(port)) {
  739. ret = cb(_fdb.mac, _fdb.vid, _fdb.noarp,
  740. data);
  741. if (ret < 0)
  742. break;
  743. }
  744. }
  745. } while (--cnt &&
  746. !(rsp & ATC_SRCH_END) &&
  747. !mt7530_fdb_cmd(priv, MT7530_FDB_NEXT, &rsp));
  748. err:
  749. mutex_unlock(&priv->reg_mutex);
  750. return 0;
  751. }
  752. static enum dsa_tag_protocol
  753. mtk_get_tag_protocol(struct dsa_switch *ds)
  754. {
  755. struct mt7530_priv *priv = ds->priv;
  756. if (!dsa_is_cpu_port(ds, MT7530_CPU_PORT)) {
  757. dev_warn(priv->dev,
  758. "port not matched with tagging CPU port\n");
  759. return DSA_TAG_PROTO_NONE;
  760. } else {
  761. return DSA_TAG_PROTO_MTK;
  762. }
  763. }
  764. static int
  765. mt7530_setup(struct dsa_switch *ds)
  766. {
  767. struct mt7530_priv *priv = ds->priv;
  768. int ret, i;
  769. u32 id, val;
  770. struct device_node *dn;
  771. struct mt7530_dummy_poll p;
  772. /* The parent node of master netdev which holds the common system
  773. * controller also is the container for two GMACs nodes representing
  774. * as two netdev instances.
  775. */
  776. dn = ds->ports[MT7530_CPU_PORT].master->dev.of_node->parent;
  777. priv->ethernet = syscon_node_to_regmap(dn);
  778. if (IS_ERR(priv->ethernet))
  779. return PTR_ERR(priv->ethernet);
  780. regulator_set_voltage(priv->core_pwr, 1000000, 1000000);
  781. ret = regulator_enable(priv->core_pwr);
  782. if (ret < 0) {
  783. dev_err(priv->dev,
  784. "Failed to enable core power: %d\n", ret);
  785. return ret;
  786. }
  787. regulator_set_voltage(priv->io_pwr, 3300000, 3300000);
  788. ret = regulator_enable(priv->io_pwr);
  789. if (ret < 0) {
  790. dev_err(priv->dev, "Failed to enable io pwr: %d\n",
  791. ret);
  792. return ret;
  793. }
  794. /* Reset whole chip through gpio pin or memory-mapped registers for
  795. * different type of hardware
  796. */
  797. if (priv->mcm) {
  798. reset_control_assert(priv->rstc);
  799. usleep_range(1000, 1100);
  800. reset_control_deassert(priv->rstc);
  801. } else {
  802. gpiod_set_value_cansleep(priv->reset, 0);
  803. usleep_range(1000, 1100);
  804. gpiod_set_value_cansleep(priv->reset, 1);
  805. }
  806. /* Waiting for MT7530 got to stable */
  807. INIT_MT7530_DUMMY_POLL(&p, priv, MT7530_HWTRAP);
  808. ret = readx_poll_timeout(_mt7530_read, &p, val, val != 0,
  809. 20, 1000000);
  810. if (ret < 0) {
  811. dev_err(priv->dev, "reset timeout\n");
  812. return ret;
  813. }
  814. id = mt7530_read(priv, MT7530_CREV);
  815. id >>= CHIP_NAME_SHIFT;
  816. if (id != MT7530_ID) {
  817. dev_err(priv->dev, "chip %x can't be supported\n", id);
  818. return -ENODEV;
  819. }
  820. /* Reset the switch through internal reset */
  821. mt7530_write(priv, MT7530_SYS_CTRL,
  822. SYS_CTRL_PHY_RST | SYS_CTRL_SW_RST |
  823. SYS_CTRL_REG_RST);
  824. /* Enable Port 6 only; P5 as GMAC5 which currently is not supported */
  825. val = mt7530_read(priv, MT7530_MHWTRAP);
  826. val &= ~MHWTRAP_P6_DIS & ~MHWTRAP_PHY_ACCESS;
  827. val |= MHWTRAP_MANUAL;
  828. mt7530_write(priv, MT7530_MHWTRAP, val);
  829. /* Enable and reset MIB counters */
  830. mt7530_mib_reset(ds);
  831. mt7530_clear(priv, MT7530_MFC, UNU_FFP_MASK);
  832. for (i = 0; i < MT7530_NUM_PORTS; i++) {
  833. /* Disable forwarding by default on all ports */
  834. mt7530_rmw(priv, MT7530_PCR_P(i), PCR_MATRIX_MASK,
  835. PCR_MATRIX_CLR);
  836. if (dsa_is_cpu_port(ds, i))
  837. mt7530_cpu_port_enable(priv, i);
  838. else
  839. mt7530_port_disable(ds, i, NULL);
  840. }
  841. /* Flush the FDB table */
  842. ret = mt7530_fdb_cmd(priv, MT7530_FDB_FLUSH, 0);
  843. if (ret < 0)
  844. return ret;
  845. return 0;
  846. }
  847. static const struct dsa_switch_ops mt7530_switch_ops = {
  848. .get_tag_protocol = mtk_get_tag_protocol,
  849. .setup = mt7530_setup,
  850. .get_strings = mt7530_get_strings,
  851. .phy_read = mt7530_phy_read,
  852. .phy_write = mt7530_phy_write,
  853. .get_ethtool_stats = mt7530_get_ethtool_stats,
  854. .get_sset_count = mt7530_get_sset_count,
  855. .adjust_link = mt7530_adjust_link,
  856. .port_enable = mt7530_port_enable,
  857. .port_disable = mt7530_port_disable,
  858. .port_stp_state_set = mt7530_stp_state_set,
  859. .port_bridge_join = mt7530_port_bridge_join,
  860. .port_bridge_leave = mt7530_port_bridge_leave,
  861. .port_fdb_add = mt7530_port_fdb_add,
  862. .port_fdb_del = mt7530_port_fdb_del,
  863. .port_fdb_dump = mt7530_port_fdb_dump,
  864. };
  865. static int
  866. mt7530_probe(struct mdio_device *mdiodev)
  867. {
  868. struct mt7530_priv *priv;
  869. struct device_node *dn;
  870. dn = mdiodev->dev.of_node;
  871. priv = devm_kzalloc(&mdiodev->dev, sizeof(*priv), GFP_KERNEL);
  872. if (!priv)
  873. return -ENOMEM;
  874. priv->ds = dsa_switch_alloc(&mdiodev->dev, DSA_MAX_PORTS);
  875. if (!priv->ds)
  876. return -ENOMEM;
  877. /* Use medatek,mcm property to distinguish hardware type that would
  878. * casues a little bit differences on power-on sequence.
  879. */
  880. priv->mcm = of_property_read_bool(dn, "mediatek,mcm");
  881. if (priv->mcm) {
  882. dev_info(&mdiodev->dev, "MT7530 adapts as multi-chip module\n");
  883. priv->rstc = devm_reset_control_get(&mdiodev->dev, "mcm");
  884. if (IS_ERR(priv->rstc)) {
  885. dev_err(&mdiodev->dev, "Couldn't get our reset line\n");
  886. return PTR_ERR(priv->rstc);
  887. }
  888. }
  889. priv->core_pwr = devm_regulator_get(&mdiodev->dev, "core");
  890. if (IS_ERR(priv->core_pwr))
  891. return PTR_ERR(priv->core_pwr);
  892. priv->io_pwr = devm_regulator_get(&mdiodev->dev, "io");
  893. if (IS_ERR(priv->io_pwr))
  894. return PTR_ERR(priv->io_pwr);
  895. /* Not MCM that indicates switch works as the remote standalone
  896. * integrated circuit so the GPIO pin would be used to complete
  897. * the reset, otherwise memory-mapped register accessing used
  898. * through syscon provides in the case of MCM.
  899. */
  900. if (!priv->mcm) {
  901. priv->reset = devm_gpiod_get_optional(&mdiodev->dev, "reset",
  902. GPIOD_OUT_LOW);
  903. if (IS_ERR(priv->reset)) {
  904. dev_err(&mdiodev->dev, "Couldn't get our reset line\n");
  905. return PTR_ERR(priv->reset);
  906. }
  907. }
  908. priv->bus = mdiodev->bus;
  909. priv->dev = &mdiodev->dev;
  910. priv->ds->priv = priv;
  911. priv->ds->ops = &mt7530_switch_ops;
  912. mutex_init(&priv->reg_mutex);
  913. dev_set_drvdata(&mdiodev->dev, priv);
  914. return dsa_register_switch(priv->ds);
  915. }
  916. static void
  917. mt7530_remove(struct mdio_device *mdiodev)
  918. {
  919. struct mt7530_priv *priv = dev_get_drvdata(&mdiodev->dev);
  920. int ret = 0;
  921. ret = regulator_disable(priv->core_pwr);
  922. if (ret < 0)
  923. dev_err(priv->dev,
  924. "Failed to disable core power: %d\n", ret);
  925. ret = regulator_disable(priv->io_pwr);
  926. if (ret < 0)
  927. dev_err(priv->dev, "Failed to disable io pwr: %d\n",
  928. ret);
  929. dsa_unregister_switch(priv->ds);
  930. mutex_destroy(&priv->reg_mutex);
  931. }
  932. static const struct of_device_id mt7530_of_match[] = {
  933. { .compatible = "mediatek,mt7530" },
  934. { /* sentinel */ },
  935. };
  936. static struct mdio_driver mt7530_mdio_driver = {
  937. .probe = mt7530_probe,
  938. .remove = mt7530_remove,
  939. .mdiodrv.driver = {
  940. .name = "mt7530",
  941. .of_match_table = mt7530_of_match,
  942. },
  943. };
  944. mdio_module_driver(mt7530_mdio_driver);
  945. MODULE_AUTHOR("Sean Wang <sean.wang@mediatek.com>");
  946. MODULE_DESCRIPTION("Driver for Mediatek MT7530 Switch");
  947. MODULE_LICENSE("GPL");
  948. MODULE_ALIAS("platform:mediatek-mt7530");