mt7530.c 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427
  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, int port)
  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_set_vlan_unaware(struct dsa_switch *ds, int port)
  668. {
  669. struct mt7530_priv *priv = ds->priv;
  670. bool all_user_ports_removed = true;
  671. int i;
  672. /* When a port is removed from the bridge, the port would be set up
  673. * back to the default as is at initial boot which is a VLAN-unaware
  674. * port.
  675. */
  676. mt7530_rmw(priv, MT7530_PCR_P(port), PCR_PORT_VLAN_MASK,
  677. MT7530_PORT_MATRIX_MODE);
  678. mt7530_rmw(priv, MT7530_PVC_P(port), VLAN_ATTR_MASK,
  679. VLAN_ATTR(MT7530_VLAN_TRANSPARENT));
  680. priv->ports[port].vlan_filtering = false;
  681. for (i = 0; i < MT7530_NUM_PORTS; i++) {
  682. if (dsa_is_user_port(ds, i) &&
  683. priv->ports[i].vlan_filtering) {
  684. all_user_ports_removed = false;
  685. break;
  686. }
  687. }
  688. /* CPU port also does the same thing until all user ports belonging to
  689. * the CPU port get out of VLAN filtering mode.
  690. */
  691. if (all_user_ports_removed) {
  692. mt7530_write(priv, MT7530_PCR_P(MT7530_CPU_PORT),
  693. PCR_MATRIX(dsa_user_ports(priv->ds)));
  694. mt7530_write(priv, MT7530_PVC_P(MT7530_CPU_PORT),
  695. PORT_SPEC_TAG);
  696. }
  697. }
  698. static void
  699. mt7530_port_set_vlan_aware(struct dsa_switch *ds, int port)
  700. {
  701. struct mt7530_priv *priv = ds->priv;
  702. /* The real fabric path would be decided on the membership in the
  703. * entry of VLAN table. PCR_MATRIX set up here with ALL_MEMBERS
  704. * means potential VLAN can be consisting of certain subset of all
  705. * ports.
  706. */
  707. mt7530_rmw(priv, MT7530_PCR_P(port),
  708. PCR_MATRIX_MASK, PCR_MATRIX(MT7530_ALL_MEMBERS));
  709. /* Trapped into security mode allows packet forwarding through VLAN
  710. * table lookup.
  711. */
  712. mt7530_rmw(priv, MT7530_PCR_P(port), PCR_PORT_VLAN_MASK,
  713. MT7530_PORT_SECURITY_MODE);
  714. /* Set the port as a user port which is to be able to recognize VID
  715. * from incoming packets before fetching entry within the VLAN table.
  716. */
  717. mt7530_rmw(priv, MT7530_PVC_P(port), VLAN_ATTR_MASK,
  718. VLAN_ATTR(MT7530_VLAN_USER));
  719. }
  720. static void
  721. mt7530_port_bridge_leave(struct dsa_switch *ds, int port,
  722. struct net_device *bridge)
  723. {
  724. struct mt7530_priv *priv = ds->priv;
  725. int i;
  726. mutex_lock(&priv->reg_mutex);
  727. for (i = 0; i < MT7530_NUM_PORTS; i++) {
  728. /* Remove this port from the port matrix of the other ports
  729. * in the same bridge. If the port is disabled, port matrix
  730. * is kept and not being setup until the port becomes enabled.
  731. * And the other port's port matrix cannot be broken when the
  732. * other port is still a VLAN-aware port.
  733. */
  734. if (!priv->ports[i].vlan_filtering &&
  735. dsa_is_user_port(ds, i) && i != port) {
  736. if (dsa_to_port(ds, i)->bridge_dev != bridge)
  737. continue;
  738. if (priv->ports[i].enable)
  739. mt7530_clear(priv, MT7530_PCR_P(i),
  740. PCR_MATRIX(BIT(port)));
  741. priv->ports[i].pm &= ~PCR_MATRIX(BIT(port));
  742. }
  743. }
  744. /* Set the cpu port to be the only one in the port matrix of
  745. * this port.
  746. */
  747. if (priv->ports[port].enable)
  748. mt7530_rmw(priv, MT7530_PCR_P(port), PCR_MATRIX_MASK,
  749. PCR_MATRIX(BIT(MT7530_CPU_PORT)));
  750. priv->ports[port].pm = PCR_MATRIX(BIT(MT7530_CPU_PORT));
  751. mt7530_port_set_vlan_unaware(ds, port);
  752. mutex_unlock(&priv->reg_mutex);
  753. }
  754. static int
  755. mt7530_port_fdb_add(struct dsa_switch *ds, int port,
  756. const unsigned char *addr, u16 vid)
  757. {
  758. struct mt7530_priv *priv = ds->priv;
  759. int ret;
  760. u8 port_mask = BIT(port);
  761. mutex_lock(&priv->reg_mutex);
  762. mt7530_fdb_write(priv, vid, port_mask, addr, -1, STATIC_ENT);
  763. ret = mt7530_fdb_cmd(priv, MT7530_FDB_WRITE, NULL);
  764. mutex_unlock(&priv->reg_mutex);
  765. return ret;
  766. }
  767. static int
  768. mt7530_port_fdb_del(struct dsa_switch *ds, int port,
  769. const unsigned char *addr, u16 vid)
  770. {
  771. struct mt7530_priv *priv = ds->priv;
  772. int ret;
  773. u8 port_mask = BIT(port);
  774. mutex_lock(&priv->reg_mutex);
  775. mt7530_fdb_write(priv, vid, port_mask, addr, -1, STATIC_EMP);
  776. ret = mt7530_fdb_cmd(priv, MT7530_FDB_WRITE, NULL);
  777. mutex_unlock(&priv->reg_mutex);
  778. return ret;
  779. }
  780. static int
  781. mt7530_port_fdb_dump(struct dsa_switch *ds, int port,
  782. dsa_fdb_dump_cb_t *cb, void *data)
  783. {
  784. struct mt7530_priv *priv = ds->priv;
  785. struct mt7530_fdb _fdb = { 0 };
  786. int cnt = MT7530_NUM_FDB_RECORDS;
  787. int ret = 0;
  788. u32 rsp = 0;
  789. mutex_lock(&priv->reg_mutex);
  790. ret = mt7530_fdb_cmd(priv, MT7530_FDB_START, &rsp);
  791. if (ret < 0)
  792. goto err;
  793. do {
  794. if (rsp & ATC_SRCH_HIT) {
  795. mt7530_fdb_read(priv, &_fdb);
  796. if (_fdb.port_mask & BIT(port)) {
  797. ret = cb(_fdb.mac, _fdb.vid, _fdb.noarp,
  798. data);
  799. if (ret < 0)
  800. break;
  801. }
  802. }
  803. } while (--cnt &&
  804. !(rsp & ATC_SRCH_END) &&
  805. !mt7530_fdb_cmd(priv, MT7530_FDB_NEXT, &rsp));
  806. err:
  807. mutex_unlock(&priv->reg_mutex);
  808. return 0;
  809. }
  810. static int
  811. mt7530_vlan_cmd(struct mt7530_priv *priv, enum mt7530_vlan_cmd cmd, u16 vid)
  812. {
  813. struct mt7530_dummy_poll p;
  814. u32 val;
  815. int ret;
  816. val = VTCR_BUSY | VTCR_FUNC(cmd) | vid;
  817. mt7530_write(priv, MT7530_VTCR, val);
  818. INIT_MT7530_DUMMY_POLL(&p, priv, MT7530_VTCR);
  819. ret = readx_poll_timeout(_mt7530_read, &p, val,
  820. !(val & VTCR_BUSY), 20, 20000);
  821. if (ret < 0) {
  822. dev_err(priv->dev, "poll timeout\n");
  823. return ret;
  824. }
  825. val = mt7530_read(priv, MT7530_VTCR);
  826. if (val & VTCR_INVALID) {
  827. dev_err(priv->dev, "read VTCR invalid\n");
  828. return -EINVAL;
  829. }
  830. return 0;
  831. }
  832. static int
  833. mt7530_port_vlan_filtering(struct dsa_switch *ds, int port,
  834. bool vlan_filtering)
  835. {
  836. struct mt7530_priv *priv = ds->priv;
  837. priv->ports[port].vlan_filtering = vlan_filtering;
  838. if (vlan_filtering) {
  839. /* The port is being kept as VLAN-unaware port when bridge is
  840. * set up with vlan_filtering not being set, Otherwise, the
  841. * port and the corresponding CPU port is required the setup
  842. * for becoming a VLAN-aware port.
  843. */
  844. mt7530_port_set_vlan_aware(ds, port);
  845. mt7530_port_set_vlan_aware(ds, MT7530_CPU_PORT);
  846. }
  847. return 0;
  848. }
  849. static int
  850. mt7530_port_vlan_prepare(struct dsa_switch *ds, int port,
  851. const struct switchdev_obj_port_vlan *vlan)
  852. {
  853. /* nothing needed */
  854. return 0;
  855. }
  856. static void
  857. mt7530_hw_vlan_add(struct mt7530_priv *priv,
  858. struct mt7530_hw_vlan_entry *entry)
  859. {
  860. u8 new_members;
  861. u32 val;
  862. new_members = entry->old_members | BIT(entry->port) |
  863. BIT(MT7530_CPU_PORT);
  864. /* Validate the entry with independent learning, create egress tag per
  865. * VLAN and joining the port as one of the port members.
  866. */
  867. val = IVL_MAC | VTAG_EN | PORT_MEM(new_members) | VLAN_VALID;
  868. mt7530_write(priv, MT7530_VAWD1, val);
  869. /* Decide whether adding tag or not for those outgoing packets from the
  870. * port inside the VLAN.
  871. */
  872. val = entry->untagged ? MT7530_VLAN_EGRESS_UNTAG :
  873. MT7530_VLAN_EGRESS_TAG;
  874. mt7530_rmw(priv, MT7530_VAWD2,
  875. ETAG_CTRL_P_MASK(entry->port),
  876. ETAG_CTRL_P(entry->port, val));
  877. /* CPU port is always taken as a tagged port for serving more than one
  878. * VLANs across and also being applied with egress type stack mode for
  879. * that VLAN tags would be appended after hardware special tag used as
  880. * DSA tag.
  881. */
  882. mt7530_rmw(priv, MT7530_VAWD2,
  883. ETAG_CTRL_P_MASK(MT7530_CPU_PORT),
  884. ETAG_CTRL_P(MT7530_CPU_PORT,
  885. MT7530_VLAN_EGRESS_STACK));
  886. }
  887. static void
  888. mt7530_hw_vlan_del(struct mt7530_priv *priv,
  889. struct mt7530_hw_vlan_entry *entry)
  890. {
  891. u8 new_members;
  892. u32 val;
  893. new_members = entry->old_members & ~BIT(entry->port);
  894. val = mt7530_read(priv, MT7530_VAWD1);
  895. if (!(val & VLAN_VALID)) {
  896. dev_err(priv->dev,
  897. "Cannot be deleted due to invalid entry\n");
  898. return;
  899. }
  900. /* If certain member apart from CPU port is still alive in the VLAN,
  901. * the entry would be kept valid. Otherwise, the entry is got to be
  902. * disabled.
  903. */
  904. if (new_members && new_members != BIT(MT7530_CPU_PORT)) {
  905. val = IVL_MAC | VTAG_EN | PORT_MEM(new_members) |
  906. VLAN_VALID;
  907. mt7530_write(priv, MT7530_VAWD1, val);
  908. } else {
  909. mt7530_write(priv, MT7530_VAWD1, 0);
  910. mt7530_write(priv, MT7530_VAWD2, 0);
  911. }
  912. }
  913. static void
  914. mt7530_hw_vlan_update(struct mt7530_priv *priv, u16 vid,
  915. struct mt7530_hw_vlan_entry *entry,
  916. mt7530_vlan_op vlan_op)
  917. {
  918. u32 val;
  919. /* Fetch entry */
  920. mt7530_vlan_cmd(priv, MT7530_VTCR_RD_VID, vid);
  921. val = mt7530_read(priv, MT7530_VAWD1);
  922. entry->old_members = (val >> PORT_MEM_SHFT) & PORT_MEM_MASK;
  923. /* Manipulate entry */
  924. vlan_op(priv, entry);
  925. /* Flush result to hardware */
  926. mt7530_vlan_cmd(priv, MT7530_VTCR_WR_VID, vid);
  927. }
  928. static void
  929. mt7530_port_vlan_add(struct dsa_switch *ds, int port,
  930. const struct switchdev_obj_port_vlan *vlan)
  931. {
  932. bool untagged = vlan->flags & BRIDGE_VLAN_INFO_UNTAGGED;
  933. bool pvid = vlan->flags & BRIDGE_VLAN_INFO_PVID;
  934. struct mt7530_hw_vlan_entry new_entry;
  935. struct mt7530_priv *priv = ds->priv;
  936. u16 vid;
  937. /* The port is kept as VLAN-unaware if bridge with vlan_filtering not
  938. * being set.
  939. */
  940. if (!priv->ports[port].vlan_filtering)
  941. return;
  942. mutex_lock(&priv->reg_mutex);
  943. for (vid = vlan->vid_begin; vid <= vlan->vid_end; ++vid) {
  944. mt7530_hw_vlan_entry_init(&new_entry, port, untagged);
  945. mt7530_hw_vlan_update(priv, vid, &new_entry,
  946. mt7530_hw_vlan_add);
  947. }
  948. if (pvid) {
  949. mt7530_rmw(priv, MT7530_PPBV1_P(port), G0_PORT_VID_MASK,
  950. G0_PORT_VID(vlan->vid_end));
  951. priv->ports[port].pvid = vlan->vid_end;
  952. }
  953. mutex_unlock(&priv->reg_mutex);
  954. }
  955. static int
  956. mt7530_port_vlan_del(struct dsa_switch *ds, int port,
  957. const struct switchdev_obj_port_vlan *vlan)
  958. {
  959. struct mt7530_hw_vlan_entry target_entry;
  960. struct mt7530_priv *priv = ds->priv;
  961. u16 vid, pvid;
  962. /* The port is kept as VLAN-unaware if bridge with vlan_filtering not
  963. * being set.
  964. */
  965. if (!priv->ports[port].vlan_filtering)
  966. return 0;
  967. mutex_lock(&priv->reg_mutex);
  968. pvid = priv->ports[port].pvid;
  969. for (vid = vlan->vid_begin; vid <= vlan->vid_end; ++vid) {
  970. mt7530_hw_vlan_entry_init(&target_entry, port, 0);
  971. mt7530_hw_vlan_update(priv, vid, &target_entry,
  972. mt7530_hw_vlan_del);
  973. /* PVID is being restored to the default whenever the PVID port
  974. * is being removed from the VLAN.
  975. */
  976. if (pvid == vid)
  977. pvid = G0_PORT_VID_DEF;
  978. }
  979. mt7530_rmw(priv, MT7530_PPBV1_P(port), G0_PORT_VID_MASK, pvid);
  980. priv->ports[port].pvid = pvid;
  981. mutex_unlock(&priv->reg_mutex);
  982. return 0;
  983. }
  984. static enum dsa_tag_protocol
  985. mtk_get_tag_protocol(struct dsa_switch *ds, int port)
  986. {
  987. struct mt7530_priv *priv = ds->priv;
  988. if (port != MT7530_CPU_PORT) {
  989. dev_warn(priv->dev,
  990. "port not matched with tagging CPU port\n");
  991. return DSA_TAG_PROTO_NONE;
  992. } else {
  993. return DSA_TAG_PROTO_MTK;
  994. }
  995. }
  996. static int
  997. mt7530_setup(struct dsa_switch *ds)
  998. {
  999. struct mt7530_priv *priv = ds->priv;
  1000. int ret, i;
  1001. u32 id, val;
  1002. struct device_node *dn;
  1003. struct mt7530_dummy_poll p;
  1004. /* The parent node of master netdev which holds the common system
  1005. * controller also is the container for two GMACs nodes representing
  1006. * as two netdev instances.
  1007. */
  1008. dn = ds->ports[MT7530_CPU_PORT].master->dev.of_node->parent;
  1009. priv->ethernet = syscon_node_to_regmap(dn);
  1010. if (IS_ERR(priv->ethernet))
  1011. return PTR_ERR(priv->ethernet);
  1012. regulator_set_voltage(priv->core_pwr, 1000000, 1000000);
  1013. ret = regulator_enable(priv->core_pwr);
  1014. if (ret < 0) {
  1015. dev_err(priv->dev,
  1016. "Failed to enable core power: %d\n", ret);
  1017. return ret;
  1018. }
  1019. regulator_set_voltage(priv->io_pwr, 3300000, 3300000);
  1020. ret = regulator_enable(priv->io_pwr);
  1021. if (ret < 0) {
  1022. dev_err(priv->dev, "Failed to enable io pwr: %d\n",
  1023. ret);
  1024. return ret;
  1025. }
  1026. /* Reset whole chip through gpio pin or memory-mapped registers for
  1027. * different type of hardware
  1028. */
  1029. if (priv->mcm) {
  1030. reset_control_assert(priv->rstc);
  1031. usleep_range(1000, 1100);
  1032. reset_control_deassert(priv->rstc);
  1033. } else {
  1034. gpiod_set_value_cansleep(priv->reset, 0);
  1035. usleep_range(1000, 1100);
  1036. gpiod_set_value_cansleep(priv->reset, 1);
  1037. }
  1038. /* Waiting for MT7530 got to stable */
  1039. INIT_MT7530_DUMMY_POLL(&p, priv, MT7530_HWTRAP);
  1040. ret = readx_poll_timeout(_mt7530_read, &p, val, val != 0,
  1041. 20, 1000000);
  1042. if (ret < 0) {
  1043. dev_err(priv->dev, "reset timeout\n");
  1044. return ret;
  1045. }
  1046. id = mt7530_read(priv, MT7530_CREV);
  1047. id >>= CHIP_NAME_SHIFT;
  1048. if (id != MT7530_ID) {
  1049. dev_err(priv->dev, "chip %x can't be supported\n", id);
  1050. return -ENODEV;
  1051. }
  1052. /* Reset the switch through internal reset */
  1053. mt7530_write(priv, MT7530_SYS_CTRL,
  1054. SYS_CTRL_PHY_RST | SYS_CTRL_SW_RST |
  1055. SYS_CTRL_REG_RST);
  1056. /* Enable Port 6 only; P5 as GMAC5 which currently is not supported */
  1057. val = mt7530_read(priv, MT7530_MHWTRAP);
  1058. val &= ~MHWTRAP_P6_DIS & ~MHWTRAP_PHY_ACCESS;
  1059. val |= MHWTRAP_MANUAL;
  1060. mt7530_write(priv, MT7530_MHWTRAP, val);
  1061. /* Enable and reset MIB counters */
  1062. mt7530_mib_reset(ds);
  1063. mt7530_clear(priv, MT7530_MFC, UNU_FFP_MASK);
  1064. for (i = 0; i < MT7530_NUM_PORTS; i++) {
  1065. /* Disable forwarding by default on all ports */
  1066. mt7530_rmw(priv, MT7530_PCR_P(i), PCR_MATRIX_MASK,
  1067. PCR_MATRIX_CLR);
  1068. if (dsa_is_cpu_port(ds, i))
  1069. mt7530_cpu_port_enable(priv, i);
  1070. else
  1071. mt7530_port_disable(ds, i, NULL);
  1072. }
  1073. /* Flush the FDB table */
  1074. ret = mt7530_fdb_cmd(priv, MT7530_FDB_FLUSH, NULL);
  1075. if (ret < 0)
  1076. return ret;
  1077. return 0;
  1078. }
  1079. static const struct dsa_switch_ops mt7530_switch_ops = {
  1080. .get_tag_protocol = mtk_get_tag_protocol,
  1081. .setup = mt7530_setup,
  1082. .get_strings = mt7530_get_strings,
  1083. .phy_read = mt7530_phy_read,
  1084. .phy_write = mt7530_phy_write,
  1085. .get_ethtool_stats = mt7530_get_ethtool_stats,
  1086. .get_sset_count = mt7530_get_sset_count,
  1087. .adjust_link = mt7530_adjust_link,
  1088. .port_enable = mt7530_port_enable,
  1089. .port_disable = mt7530_port_disable,
  1090. .port_stp_state_set = mt7530_stp_state_set,
  1091. .port_bridge_join = mt7530_port_bridge_join,
  1092. .port_bridge_leave = mt7530_port_bridge_leave,
  1093. .port_fdb_add = mt7530_port_fdb_add,
  1094. .port_fdb_del = mt7530_port_fdb_del,
  1095. .port_fdb_dump = mt7530_port_fdb_dump,
  1096. .port_vlan_filtering = mt7530_port_vlan_filtering,
  1097. .port_vlan_prepare = mt7530_port_vlan_prepare,
  1098. .port_vlan_add = mt7530_port_vlan_add,
  1099. .port_vlan_del = mt7530_port_vlan_del,
  1100. };
  1101. static int
  1102. mt7530_probe(struct mdio_device *mdiodev)
  1103. {
  1104. struct mt7530_priv *priv;
  1105. struct device_node *dn;
  1106. dn = mdiodev->dev.of_node;
  1107. priv = devm_kzalloc(&mdiodev->dev, sizeof(*priv), GFP_KERNEL);
  1108. if (!priv)
  1109. return -ENOMEM;
  1110. priv->ds = dsa_switch_alloc(&mdiodev->dev, DSA_MAX_PORTS);
  1111. if (!priv->ds)
  1112. return -ENOMEM;
  1113. /* Use medatek,mcm property to distinguish hardware type that would
  1114. * casues a little bit differences on power-on sequence.
  1115. */
  1116. priv->mcm = of_property_read_bool(dn, "mediatek,mcm");
  1117. if (priv->mcm) {
  1118. dev_info(&mdiodev->dev, "MT7530 adapts as multi-chip module\n");
  1119. priv->rstc = devm_reset_control_get(&mdiodev->dev, "mcm");
  1120. if (IS_ERR(priv->rstc)) {
  1121. dev_err(&mdiodev->dev, "Couldn't get our reset line\n");
  1122. return PTR_ERR(priv->rstc);
  1123. }
  1124. }
  1125. priv->core_pwr = devm_regulator_get(&mdiodev->dev, "core");
  1126. if (IS_ERR(priv->core_pwr))
  1127. return PTR_ERR(priv->core_pwr);
  1128. priv->io_pwr = devm_regulator_get(&mdiodev->dev, "io");
  1129. if (IS_ERR(priv->io_pwr))
  1130. return PTR_ERR(priv->io_pwr);
  1131. /* Not MCM that indicates switch works as the remote standalone
  1132. * integrated circuit so the GPIO pin would be used to complete
  1133. * the reset, otherwise memory-mapped register accessing used
  1134. * through syscon provides in the case of MCM.
  1135. */
  1136. if (!priv->mcm) {
  1137. priv->reset = devm_gpiod_get_optional(&mdiodev->dev, "reset",
  1138. GPIOD_OUT_LOW);
  1139. if (IS_ERR(priv->reset)) {
  1140. dev_err(&mdiodev->dev, "Couldn't get our reset line\n");
  1141. return PTR_ERR(priv->reset);
  1142. }
  1143. }
  1144. priv->bus = mdiodev->bus;
  1145. priv->dev = &mdiodev->dev;
  1146. priv->ds->priv = priv;
  1147. priv->ds->ops = &mt7530_switch_ops;
  1148. mutex_init(&priv->reg_mutex);
  1149. dev_set_drvdata(&mdiodev->dev, priv);
  1150. return dsa_register_switch(priv->ds);
  1151. }
  1152. static void
  1153. mt7530_remove(struct mdio_device *mdiodev)
  1154. {
  1155. struct mt7530_priv *priv = dev_get_drvdata(&mdiodev->dev);
  1156. int ret = 0;
  1157. ret = regulator_disable(priv->core_pwr);
  1158. if (ret < 0)
  1159. dev_err(priv->dev,
  1160. "Failed to disable core power: %d\n", ret);
  1161. ret = regulator_disable(priv->io_pwr);
  1162. if (ret < 0)
  1163. dev_err(priv->dev, "Failed to disable io pwr: %d\n",
  1164. ret);
  1165. dsa_unregister_switch(priv->ds);
  1166. mutex_destroy(&priv->reg_mutex);
  1167. }
  1168. static const struct of_device_id mt7530_of_match[] = {
  1169. { .compatible = "mediatek,mt7530" },
  1170. { /* sentinel */ },
  1171. };
  1172. MODULE_DEVICE_TABLE(of, mt7530_of_match);
  1173. static struct mdio_driver mt7530_mdio_driver = {
  1174. .probe = mt7530_probe,
  1175. .remove = mt7530_remove,
  1176. .mdiodrv.driver = {
  1177. .name = "mt7530",
  1178. .of_match_table = mt7530_of_match,
  1179. },
  1180. };
  1181. mdio_module_driver(mt7530_mdio_driver);
  1182. MODULE_AUTHOR("Sean Wang <sean.wang@mediatek.com>");
  1183. MODULE_DESCRIPTION("Driver for Mediatek MT7530 Switch");
  1184. MODULE_LICENSE("GPL");