mt7530.c 28 KB

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