bcm_sf2.c 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420
  1. /*
  2. * Broadcom Starfighter 2 DSA switch driver
  3. *
  4. * Copyright (C) 2014, Broadcom Corporation
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. */
  11. #include <linux/list.h>
  12. #include <linux/module.h>
  13. #include <linux/netdevice.h>
  14. #include <linux/interrupt.h>
  15. #include <linux/platform_device.h>
  16. #include <linux/of.h>
  17. #include <linux/phy.h>
  18. #include <linux/phy_fixed.h>
  19. #include <linux/mii.h>
  20. #include <linux/of.h>
  21. #include <linux/of_irq.h>
  22. #include <linux/of_address.h>
  23. #include <linux/of_net.h>
  24. #include <net/dsa.h>
  25. #include <linux/ethtool.h>
  26. #include <linux/if_bridge.h>
  27. #include <linux/brcmphy.h>
  28. #include <linux/etherdevice.h>
  29. #include <net/switchdev.h>
  30. #include "bcm_sf2.h"
  31. #include "bcm_sf2_regs.h"
  32. /* String, offset, and register size in bytes if different from 4 bytes */
  33. static const struct bcm_sf2_hw_stats bcm_sf2_mib[] = {
  34. { "TxOctets", 0x000, 8 },
  35. { "TxDropPkts", 0x020 },
  36. { "TxQPKTQ0", 0x030 },
  37. { "TxBroadcastPkts", 0x040 },
  38. { "TxMulticastPkts", 0x050 },
  39. { "TxUnicastPKts", 0x060 },
  40. { "TxCollisions", 0x070 },
  41. { "TxSingleCollision", 0x080 },
  42. { "TxMultipleCollision", 0x090 },
  43. { "TxDeferredCollision", 0x0a0 },
  44. { "TxLateCollision", 0x0b0 },
  45. { "TxExcessiveCollision", 0x0c0 },
  46. { "TxFrameInDisc", 0x0d0 },
  47. { "TxPausePkts", 0x0e0 },
  48. { "TxQPKTQ1", 0x0f0 },
  49. { "TxQPKTQ2", 0x100 },
  50. { "TxQPKTQ3", 0x110 },
  51. { "TxQPKTQ4", 0x120 },
  52. { "TxQPKTQ5", 0x130 },
  53. { "RxOctets", 0x140, 8 },
  54. { "RxUndersizePkts", 0x160 },
  55. { "RxPausePkts", 0x170 },
  56. { "RxPkts64Octets", 0x180 },
  57. { "RxPkts65to127Octets", 0x190 },
  58. { "RxPkts128to255Octets", 0x1a0 },
  59. { "RxPkts256to511Octets", 0x1b0 },
  60. { "RxPkts512to1023Octets", 0x1c0 },
  61. { "RxPkts1024toMaxPktsOctets", 0x1d0 },
  62. { "RxOversizePkts", 0x1e0 },
  63. { "RxJabbers", 0x1f0 },
  64. { "RxAlignmentErrors", 0x200 },
  65. { "RxFCSErrors", 0x210 },
  66. { "RxGoodOctets", 0x220, 8 },
  67. { "RxDropPkts", 0x240 },
  68. { "RxUnicastPkts", 0x250 },
  69. { "RxMulticastPkts", 0x260 },
  70. { "RxBroadcastPkts", 0x270 },
  71. { "RxSAChanges", 0x280 },
  72. { "RxFragments", 0x290 },
  73. { "RxJumboPkt", 0x2a0 },
  74. { "RxSymblErr", 0x2b0 },
  75. { "InRangeErrCount", 0x2c0 },
  76. { "OutRangeErrCount", 0x2d0 },
  77. { "EEELpiEvent", 0x2e0 },
  78. { "EEELpiDuration", 0x2f0 },
  79. { "RxDiscard", 0x300, 8 },
  80. { "TxQPKTQ6", 0x320 },
  81. { "TxQPKTQ7", 0x330 },
  82. { "TxPkts64Octets", 0x340 },
  83. { "TxPkts65to127Octets", 0x350 },
  84. { "TxPkts128to255Octets", 0x360 },
  85. { "TxPkts256to511Ocets", 0x370 },
  86. { "TxPkts512to1023Ocets", 0x380 },
  87. { "TxPkts1024toMaxPktOcets", 0x390 },
  88. };
  89. #define BCM_SF2_STATS_SIZE ARRAY_SIZE(bcm_sf2_mib)
  90. static void bcm_sf2_sw_get_strings(struct dsa_switch *ds,
  91. int port, uint8_t *data)
  92. {
  93. unsigned int i;
  94. for (i = 0; i < BCM_SF2_STATS_SIZE; i++)
  95. memcpy(data + i * ETH_GSTRING_LEN,
  96. bcm_sf2_mib[i].string, ETH_GSTRING_LEN);
  97. }
  98. static void bcm_sf2_sw_get_ethtool_stats(struct dsa_switch *ds,
  99. int port, uint64_t *data)
  100. {
  101. struct bcm_sf2_priv *priv = ds_to_priv(ds);
  102. const struct bcm_sf2_hw_stats *s;
  103. unsigned int i;
  104. u64 val = 0;
  105. u32 offset;
  106. mutex_lock(&priv->stats_mutex);
  107. /* Now fetch the per-port counters */
  108. for (i = 0; i < BCM_SF2_STATS_SIZE; i++) {
  109. s = &bcm_sf2_mib[i];
  110. /* Do a latched 64-bit read if needed */
  111. offset = s->reg + CORE_P_MIB_OFFSET(port);
  112. if (s->sizeof_stat == 8)
  113. val = core_readq(priv, offset);
  114. else
  115. val = core_readl(priv, offset);
  116. data[i] = (u64)val;
  117. }
  118. mutex_unlock(&priv->stats_mutex);
  119. }
  120. static int bcm_sf2_sw_get_sset_count(struct dsa_switch *ds)
  121. {
  122. return BCM_SF2_STATS_SIZE;
  123. }
  124. static const char *bcm_sf2_sw_drv_probe(struct device *dsa_dev,
  125. struct device *host_dev, int sw_addr,
  126. void **_priv)
  127. {
  128. struct bcm_sf2_priv *priv;
  129. priv = devm_kzalloc(dsa_dev, sizeof(*priv), GFP_KERNEL);
  130. if (!priv)
  131. return NULL;
  132. *_priv = priv;
  133. return "Broadcom Starfighter 2";
  134. }
  135. static void bcm_sf2_imp_vlan_setup(struct dsa_switch *ds, int cpu_port)
  136. {
  137. struct bcm_sf2_priv *priv = ds_to_priv(ds);
  138. unsigned int i;
  139. u32 reg;
  140. /* Enable the IMP Port to be in the same VLAN as the other ports
  141. * on a per-port basis such that we only have Port i and IMP in
  142. * the same VLAN.
  143. */
  144. for (i = 0; i < priv->hw_params.num_ports; i++) {
  145. if (!((1 << i) & ds->enabled_port_mask))
  146. continue;
  147. reg = core_readl(priv, CORE_PORT_VLAN_CTL_PORT(i));
  148. reg |= (1 << cpu_port);
  149. core_writel(priv, reg, CORE_PORT_VLAN_CTL_PORT(i));
  150. }
  151. }
  152. static void bcm_sf2_imp_setup(struct dsa_switch *ds, int port)
  153. {
  154. struct bcm_sf2_priv *priv = ds_to_priv(ds);
  155. u32 reg, val;
  156. /* Enable the port memories */
  157. reg = core_readl(priv, CORE_MEM_PSM_VDD_CTRL);
  158. reg &= ~P_TXQ_PSM_VDD(port);
  159. core_writel(priv, reg, CORE_MEM_PSM_VDD_CTRL);
  160. /* Enable Broadcast, Multicast, Unicast forwarding to IMP port */
  161. reg = core_readl(priv, CORE_IMP_CTL);
  162. reg |= (RX_BCST_EN | RX_MCST_EN | RX_UCST_EN);
  163. reg &= ~(RX_DIS | TX_DIS);
  164. core_writel(priv, reg, CORE_IMP_CTL);
  165. /* Enable forwarding */
  166. core_writel(priv, SW_FWDG_EN, CORE_SWMODE);
  167. /* Enable IMP port in dumb mode */
  168. reg = core_readl(priv, CORE_SWITCH_CTRL);
  169. reg |= MII_DUMB_FWDG_EN;
  170. core_writel(priv, reg, CORE_SWITCH_CTRL);
  171. /* Resolve which bit controls the Broadcom tag */
  172. switch (port) {
  173. case 8:
  174. val = BRCM_HDR_EN_P8;
  175. break;
  176. case 7:
  177. val = BRCM_HDR_EN_P7;
  178. break;
  179. case 5:
  180. val = BRCM_HDR_EN_P5;
  181. break;
  182. default:
  183. val = 0;
  184. break;
  185. }
  186. /* Enable Broadcom tags for IMP port */
  187. reg = core_readl(priv, CORE_BRCM_HDR_CTRL);
  188. reg |= val;
  189. core_writel(priv, reg, CORE_BRCM_HDR_CTRL);
  190. /* Enable reception Broadcom tag for CPU TX (switch RX) to
  191. * allow us to tag outgoing frames
  192. */
  193. reg = core_readl(priv, CORE_BRCM_HDR_RX_DIS);
  194. reg &= ~(1 << port);
  195. core_writel(priv, reg, CORE_BRCM_HDR_RX_DIS);
  196. /* Enable transmission of Broadcom tags from the switch (CPU RX) to
  197. * allow delivering frames to the per-port net_devices
  198. */
  199. reg = core_readl(priv, CORE_BRCM_HDR_TX_DIS);
  200. reg &= ~(1 << port);
  201. core_writel(priv, reg, CORE_BRCM_HDR_TX_DIS);
  202. /* Force link status for IMP port */
  203. reg = core_readl(priv, CORE_STS_OVERRIDE_IMP);
  204. reg |= (MII_SW_OR | LINK_STS);
  205. core_writel(priv, reg, CORE_STS_OVERRIDE_IMP);
  206. }
  207. static void bcm_sf2_eee_enable_set(struct dsa_switch *ds, int port, bool enable)
  208. {
  209. struct bcm_sf2_priv *priv = ds_to_priv(ds);
  210. u32 reg;
  211. reg = core_readl(priv, CORE_EEE_EN_CTRL);
  212. if (enable)
  213. reg |= 1 << port;
  214. else
  215. reg &= ~(1 << port);
  216. core_writel(priv, reg, CORE_EEE_EN_CTRL);
  217. }
  218. static void bcm_sf2_gphy_enable_set(struct dsa_switch *ds, bool enable)
  219. {
  220. struct bcm_sf2_priv *priv = ds_to_priv(ds);
  221. u32 reg;
  222. reg = reg_readl(priv, REG_SPHY_CNTRL);
  223. if (enable) {
  224. reg |= PHY_RESET;
  225. reg &= ~(EXT_PWR_DOWN | IDDQ_BIAS | CK25_DIS);
  226. reg_writel(priv, reg, REG_SPHY_CNTRL);
  227. udelay(21);
  228. reg = reg_readl(priv, REG_SPHY_CNTRL);
  229. reg &= ~PHY_RESET;
  230. } else {
  231. reg |= EXT_PWR_DOWN | IDDQ_BIAS | PHY_RESET;
  232. reg_writel(priv, reg, REG_SPHY_CNTRL);
  233. mdelay(1);
  234. reg |= CK25_DIS;
  235. }
  236. reg_writel(priv, reg, REG_SPHY_CNTRL);
  237. /* Use PHY-driven LED signaling */
  238. if (!enable) {
  239. reg = reg_readl(priv, REG_LED_CNTRL(0));
  240. reg |= SPDLNK_SRC_SEL;
  241. reg_writel(priv, reg, REG_LED_CNTRL(0));
  242. }
  243. }
  244. static inline void bcm_sf2_port_intr_enable(struct bcm_sf2_priv *priv,
  245. int port)
  246. {
  247. unsigned int off;
  248. switch (port) {
  249. case 7:
  250. off = P7_IRQ_OFF;
  251. break;
  252. case 0:
  253. /* Port 0 interrupts are located on the first bank */
  254. intrl2_0_mask_clear(priv, P_IRQ_MASK(P0_IRQ_OFF));
  255. return;
  256. default:
  257. off = P_IRQ_OFF(port);
  258. break;
  259. }
  260. intrl2_1_mask_clear(priv, P_IRQ_MASK(off));
  261. }
  262. static inline void bcm_sf2_port_intr_disable(struct bcm_sf2_priv *priv,
  263. int port)
  264. {
  265. unsigned int off;
  266. switch (port) {
  267. case 7:
  268. off = P7_IRQ_OFF;
  269. break;
  270. case 0:
  271. /* Port 0 interrupts are located on the first bank */
  272. intrl2_0_mask_set(priv, P_IRQ_MASK(P0_IRQ_OFF));
  273. intrl2_0_writel(priv, P_IRQ_MASK(P0_IRQ_OFF), INTRL2_CPU_CLEAR);
  274. return;
  275. default:
  276. off = P_IRQ_OFF(port);
  277. break;
  278. }
  279. intrl2_1_mask_set(priv, P_IRQ_MASK(off));
  280. intrl2_1_writel(priv, P_IRQ_MASK(off), INTRL2_CPU_CLEAR);
  281. }
  282. static int bcm_sf2_port_setup(struct dsa_switch *ds, int port,
  283. struct phy_device *phy)
  284. {
  285. struct bcm_sf2_priv *priv = ds_to_priv(ds);
  286. s8 cpu_port = ds->dst[ds->index].cpu_port;
  287. u32 reg;
  288. /* Clear the memory power down */
  289. reg = core_readl(priv, CORE_MEM_PSM_VDD_CTRL);
  290. reg &= ~P_TXQ_PSM_VDD(port);
  291. core_writel(priv, reg, CORE_MEM_PSM_VDD_CTRL);
  292. /* Clear the Rx and Tx disable bits and set to no spanning tree */
  293. core_writel(priv, 0, CORE_G_PCTL_PORT(port));
  294. /* Re-enable the GPHY and re-apply workarounds */
  295. if (priv->int_phy_mask & 1 << port && priv->hw_params.num_gphy == 1) {
  296. bcm_sf2_gphy_enable_set(ds, true);
  297. if (phy) {
  298. /* if phy_stop() has been called before, phy
  299. * will be in halted state, and phy_start()
  300. * will call resume.
  301. *
  302. * the resume path does not configure back
  303. * autoneg settings, and since we hard reset
  304. * the phy manually here, we need to reset the
  305. * state machine also.
  306. */
  307. phy->state = PHY_READY;
  308. phy_init_hw(phy);
  309. }
  310. }
  311. /* Enable MoCA port interrupts to get notified */
  312. if (port == priv->moca_port)
  313. bcm_sf2_port_intr_enable(priv, port);
  314. /* Set this port, and only this one to be in the default VLAN,
  315. * if member of a bridge, restore its membership prior to
  316. * bringing down this port.
  317. */
  318. reg = core_readl(priv, CORE_PORT_VLAN_CTL_PORT(port));
  319. reg &= ~PORT_VLAN_CTRL_MASK;
  320. reg |= (1 << port);
  321. reg |= priv->port_sts[port].vlan_ctl_mask;
  322. core_writel(priv, reg, CORE_PORT_VLAN_CTL_PORT(port));
  323. bcm_sf2_imp_vlan_setup(ds, cpu_port);
  324. /* If EEE was enabled, restore it */
  325. if (priv->port_sts[port].eee.eee_enabled)
  326. bcm_sf2_eee_enable_set(ds, port, true);
  327. return 0;
  328. }
  329. static void bcm_sf2_port_disable(struct dsa_switch *ds, int port,
  330. struct phy_device *phy)
  331. {
  332. struct bcm_sf2_priv *priv = ds_to_priv(ds);
  333. u32 off, reg;
  334. if (priv->wol_ports_mask & (1 << port))
  335. return;
  336. if (port == priv->moca_port)
  337. bcm_sf2_port_intr_disable(priv, port);
  338. if (priv->int_phy_mask & 1 << port && priv->hw_params.num_gphy == 1)
  339. bcm_sf2_gphy_enable_set(ds, false);
  340. if (dsa_is_cpu_port(ds, port))
  341. off = CORE_IMP_CTL;
  342. else
  343. off = CORE_G_PCTL_PORT(port);
  344. reg = core_readl(priv, off);
  345. reg |= RX_DIS | TX_DIS;
  346. core_writel(priv, reg, off);
  347. /* Power down the port memory */
  348. reg = core_readl(priv, CORE_MEM_PSM_VDD_CTRL);
  349. reg |= P_TXQ_PSM_VDD(port);
  350. core_writel(priv, reg, CORE_MEM_PSM_VDD_CTRL);
  351. }
  352. /* Returns 0 if EEE was not enabled, or 1 otherwise
  353. */
  354. static int bcm_sf2_eee_init(struct dsa_switch *ds, int port,
  355. struct phy_device *phy)
  356. {
  357. struct bcm_sf2_priv *priv = ds_to_priv(ds);
  358. struct ethtool_eee *p = &priv->port_sts[port].eee;
  359. int ret;
  360. p->supported = (SUPPORTED_1000baseT_Full | SUPPORTED_100baseT_Full);
  361. ret = phy_init_eee(phy, 0);
  362. if (ret)
  363. return 0;
  364. bcm_sf2_eee_enable_set(ds, port, true);
  365. return 1;
  366. }
  367. static int bcm_sf2_sw_get_eee(struct dsa_switch *ds, int port,
  368. struct ethtool_eee *e)
  369. {
  370. struct bcm_sf2_priv *priv = ds_to_priv(ds);
  371. struct ethtool_eee *p = &priv->port_sts[port].eee;
  372. u32 reg;
  373. reg = core_readl(priv, CORE_EEE_LPI_INDICATE);
  374. e->eee_enabled = p->eee_enabled;
  375. e->eee_active = !!(reg & (1 << port));
  376. return 0;
  377. }
  378. static int bcm_sf2_sw_set_eee(struct dsa_switch *ds, int port,
  379. struct phy_device *phydev,
  380. struct ethtool_eee *e)
  381. {
  382. struct bcm_sf2_priv *priv = ds_to_priv(ds);
  383. struct ethtool_eee *p = &priv->port_sts[port].eee;
  384. p->eee_enabled = e->eee_enabled;
  385. if (!p->eee_enabled) {
  386. bcm_sf2_eee_enable_set(ds, port, false);
  387. } else {
  388. p->eee_enabled = bcm_sf2_eee_init(ds, port, phydev);
  389. if (!p->eee_enabled)
  390. return -EOPNOTSUPP;
  391. }
  392. return 0;
  393. }
  394. /* Fast-ageing of ARL entries for a given port, equivalent to an ARL
  395. * flush for that port.
  396. */
  397. static int bcm_sf2_sw_fast_age_port(struct dsa_switch *ds, int port)
  398. {
  399. struct bcm_sf2_priv *priv = ds_to_priv(ds);
  400. unsigned int timeout = 1000;
  401. u32 reg;
  402. core_writel(priv, port, CORE_FAST_AGE_PORT);
  403. reg = core_readl(priv, CORE_FAST_AGE_CTRL);
  404. reg |= EN_AGE_PORT | EN_AGE_DYNAMIC | FAST_AGE_STR_DONE;
  405. core_writel(priv, reg, CORE_FAST_AGE_CTRL);
  406. do {
  407. reg = core_readl(priv, CORE_FAST_AGE_CTRL);
  408. if (!(reg & FAST_AGE_STR_DONE))
  409. break;
  410. cpu_relax();
  411. } while (timeout--);
  412. if (!timeout)
  413. return -ETIMEDOUT;
  414. core_writel(priv, 0, CORE_FAST_AGE_CTRL);
  415. return 0;
  416. }
  417. static int bcm_sf2_sw_br_join(struct dsa_switch *ds, int port,
  418. struct net_device *bridge)
  419. {
  420. struct bcm_sf2_priv *priv = ds_to_priv(ds);
  421. unsigned int i;
  422. u32 reg, p_ctl;
  423. priv->port_sts[port].bridge_dev = bridge;
  424. p_ctl = core_readl(priv, CORE_PORT_VLAN_CTL_PORT(port));
  425. for (i = 0; i < priv->hw_params.num_ports; i++) {
  426. if (priv->port_sts[i].bridge_dev != bridge)
  427. continue;
  428. /* Add this local port to the remote port VLAN control
  429. * membership and update the remote port bitmask
  430. */
  431. reg = core_readl(priv, CORE_PORT_VLAN_CTL_PORT(i));
  432. reg |= 1 << port;
  433. core_writel(priv, reg, CORE_PORT_VLAN_CTL_PORT(i));
  434. priv->port_sts[i].vlan_ctl_mask = reg;
  435. p_ctl |= 1 << i;
  436. }
  437. /* Configure the local port VLAN control membership to include
  438. * remote ports and update the local port bitmask
  439. */
  440. core_writel(priv, p_ctl, CORE_PORT_VLAN_CTL_PORT(port));
  441. priv->port_sts[port].vlan_ctl_mask = p_ctl;
  442. return 0;
  443. }
  444. static void bcm_sf2_sw_br_leave(struct dsa_switch *ds, int port)
  445. {
  446. struct bcm_sf2_priv *priv = ds_to_priv(ds);
  447. struct net_device *bridge = priv->port_sts[port].bridge_dev;
  448. unsigned int i;
  449. u32 reg, p_ctl;
  450. p_ctl = core_readl(priv, CORE_PORT_VLAN_CTL_PORT(port));
  451. for (i = 0; i < priv->hw_params.num_ports; i++) {
  452. /* Don't touch the remaining ports */
  453. if (priv->port_sts[i].bridge_dev != bridge)
  454. continue;
  455. reg = core_readl(priv, CORE_PORT_VLAN_CTL_PORT(i));
  456. reg &= ~(1 << port);
  457. core_writel(priv, reg, CORE_PORT_VLAN_CTL_PORT(i));
  458. priv->port_sts[port].vlan_ctl_mask = reg;
  459. /* Prevent self removal to preserve isolation */
  460. if (port != i)
  461. p_ctl &= ~(1 << i);
  462. }
  463. core_writel(priv, p_ctl, CORE_PORT_VLAN_CTL_PORT(port));
  464. priv->port_sts[port].vlan_ctl_mask = p_ctl;
  465. priv->port_sts[port].bridge_dev = NULL;
  466. }
  467. static void bcm_sf2_sw_br_set_stp_state(struct dsa_switch *ds, int port,
  468. u8 state)
  469. {
  470. struct bcm_sf2_priv *priv = ds_to_priv(ds);
  471. u8 hw_state, cur_hw_state;
  472. u32 reg;
  473. reg = core_readl(priv, CORE_G_PCTL_PORT(port));
  474. cur_hw_state = reg & (G_MISTP_STATE_MASK << G_MISTP_STATE_SHIFT);
  475. switch (state) {
  476. case BR_STATE_DISABLED:
  477. hw_state = G_MISTP_DIS_STATE;
  478. break;
  479. case BR_STATE_LISTENING:
  480. hw_state = G_MISTP_LISTEN_STATE;
  481. break;
  482. case BR_STATE_LEARNING:
  483. hw_state = G_MISTP_LEARN_STATE;
  484. break;
  485. case BR_STATE_FORWARDING:
  486. hw_state = G_MISTP_FWD_STATE;
  487. break;
  488. case BR_STATE_BLOCKING:
  489. hw_state = G_MISTP_BLOCK_STATE;
  490. break;
  491. default:
  492. pr_err("%s: invalid STP state: %d\n", __func__, state);
  493. return;
  494. }
  495. /* Fast-age ARL entries if we are moving a port from Learning or
  496. * Forwarding (cur_hw_state) state to Disabled, Blocking or Listening
  497. * state (hw_state)
  498. */
  499. if (cur_hw_state != hw_state) {
  500. if (cur_hw_state >= G_MISTP_LEARN_STATE &&
  501. hw_state <= G_MISTP_LISTEN_STATE) {
  502. if (bcm_sf2_sw_fast_age_port(ds, port)) {
  503. pr_err("%s: fast-ageing failed\n", __func__);
  504. return;
  505. }
  506. }
  507. }
  508. reg = core_readl(priv, CORE_G_PCTL_PORT(port));
  509. reg &= ~(G_MISTP_STATE_MASK << G_MISTP_STATE_SHIFT);
  510. reg |= hw_state;
  511. core_writel(priv, reg, CORE_G_PCTL_PORT(port));
  512. }
  513. /* Address Resolution Logic routines */
  514. static int bcm_sf2_arl_op_wait(struct bcm_sf2_priv *priv)
  515. {
  516. unsigned int timeout = 10;
  517. u32 reg;
  518. do {
  519. reg = core_readl(priv, CORE_ARLA_RWCTL);
  520. if (!(reg & ARL_STRTDN))
  521. return 0;
  522. usleep_range(1000, 2000);
  523. } while (timeout--);
  524. return -ETIMEDOUT;
  525. }
  526. static int bcm_sf2_arl_rw_op(struct bcm_sf2_priv *priv, unsigned int op)
  527. {
  528. u32 cmd;
  529. if (op > ARL_RW)
  530. return -EINVAL;
  531. cmd = core_readl(priv, CORE_ARLA_RWCTL);
  532. cmd &= ~IVL_SVL_SELECT;
  533. cmd |= ARL_STRTDN;
  534. if (op)
  535. cmd |= ARL_RW;
  536. else
  537. cmd &= ~ARL_RW;
  538. core_writel(priv, cmd, CORE_ARLA_RWCTL);
  539. return bcm_sf2_arl_op_wait(priv);
  540. }
  541. static int bcm_sf2_arl_read(struct bcm_sf2_priv *priv, u64 mac,
  542. u16 vid, struct bcm_sf2_arl_entry *ent, u8 *idx,
  543. bool is_valid)
  544. {
  545. unsigned int i;
  546. int ret;
  547. ret = bcm_sf2_arl_op_wait(priv);
  548. if (ret)
  549. return ret;
  550. /* Read the 4 bins */
  551. for (i = 0; i < 4; i++) {
  552. u64 mac_vid;
  553. u32 fwd_entry;
  554. mac_vid = core_readq(priv, CORE_ARLA_MACVID_ENTRY(i));
  555. fwd_entry = core_readl(priv, CORE_ARLA_FWD_ENTRY(i));
  556. bcm_sf2_arl_to_entry(ent, mac_vid, fwd_entry);
  557. if (ent->is_valid && is_valid) {
  558. *idx = i;
  559. return 0;
  560. }
  561. /* This is the MAC we just deleted */
  562. if (!is_valid && (mac_vid & mac))
  563. return 0;
  564. }
  565. return -ENOENT;
  566. }
  567. static int bcm_sf2_arl_op(struct bcm_sf2_priv *priv, int op, int port,
  568. const unsigned char *addr, u16 vid, bool is_valid)
  569. {
  570. struct bcm_sf2_arl_entry ent;
  571. u32 fwd_entry;
  572. u64 mac, mac_vid = 0;
  573. u8 idx = 0;
  574. int ret;
  575. /* Convert the array into a 64-bit MAC */
  576. mac = bcm_sf2_mac_to_u64(addr);
  577. /* Perform a read for the given MAC and VID */
  578. core_writeq(priv, mac, CORE_ARLA_MAC);
  579. core_writel(priv, vid, CORE_ARLA_VID);
  580. /* Issue a read operation for this MAC */
  581. ret = bcm_sf2_arl_rw_op(priv, 1);
  582. if (ret)
  583. return ret;
  584. ret = bcm_sf2_arl_read(priv, mac, vid, &ent, &idx, is_valid);
  585. /* If this is a read, just finish now */
  586. if (op)
  587. return ret;
  588. /* We could not find a matching MAC, so reset to a new entry */
  589. if (ret) {
  590. fwd_entry = 0;
  591. idx = 0;
  592. }
  593. memset(&ent, 0, sizeof(ent));
  594. ent.port = port;
  595. ent.is_valid = is_valid;
  596. ent.vid = vid;
  597. ent.is_static = true;
  598. memcpy(ent.mac, addr, ETH_ALEN);
  599. bcm_sf2_arl_from_entry(&mac_vid, &fwd_entry, &ent);
  600. core_writeq(priv, mac_vid, CORE_ARLA_MACVID_ENTRY(idx));
  601. core_writel(priv, fwd_entry, CORE_ARLA_FWD_ENTRY(idx));
  602. ret = bcm_sf2_arl_rw_op(priv, 0);
  603. if (ret)
  604. return ret;
  605. /* Re-read the entry to check */
  606. return bcm_sf2_arl_read(priv, mac, vid, &ent, &idx, is_valid);
  607. }
  608. static int bcm_sf2_sw_fdb_prepare(struct dsa_switch *ds, int port,
  609. const struct switchdev_obj_port_fdb *fdb,
  610. struct switchdev_trans *trans)
  611. {
  612. /* We do not need to do anything specific here yet */
  613. return 0;
  614. }
  615. static void bcm_sf2_sw_fdb_add(struct dsa_switch *ds, int port,
  616. const struct switchdev_obj_port_fdb *fdb,
  617. struct switchdev_trans *trans)
  618. {
  619. struct bcm_sf2_priv *priv = ds_to_priv(ds);
  620. if (bcm_sf2_arl_op(priv, 0, port, fdb->addr, fdb->vid, true))
  621. pr_err("%s: failed to add MAC address\n", __func__);
  622. }
  623. static int bcm_sf2_sw_fdb_del(struct dsa_switch *ds, int port,
  624. const struct switchdev_obj_port_fdb *fdb)
  625. {
  626. struct bcm_sf2_priv *priv = ds_to_priv(ds);
  627. return bcm_sf2_arl_op(priv, 0, port, fdb->addr, fdb->vid, false);
  628. }
  629. static int bcm_sf2_arl_search_wait(struct bcm_sf2_priv *priv)
  630. {
  631. unsigned timeout = 1000;
  632. u32 reg;
  633. do {
  634. reg = core_readl(priv, CORE_ARLA_SRCH_CTL);
  635. if (!(reg & ARLA_SRCH_STDN))
  636. return 0;
  637. if (reg & ARLA_SRCH_VLID)
  638. return 0;
  639. usleep_range(1000, 2000);
  640. } while (timeout--);
  641. return -ETIMEDOUT;
  642. }
  643. static void bcm_sf2_arl_search_rd(struct bcm_sf2_priv *priv, u8 idx,
  644. struct bcm_sf2_arl_entry *ent)
  645. {
  646. u64 mac_vid;
  647. u32 fwd_entry;
  648. mac_vid = core_readq(priv, CORE_ARLA_SRCH_RSLT_MACVID(idx));
  649. fwd_entry = core_readl(priv, CORE_ARLA_SRCH_RSLT(idx));
  650. bcm_sf2_arl_to_entry(ent, mac_vid, fwd_entry);
  651. }
  652. static int bcm_sf2_sw_fdb_copy(struct net_device *dev, int port,
  653. const struct bcm_sf2_arl_entry *ent,
  654. struct switchdev_obj_port_fdb *fdb,
  655. int (*cb)(struct switchdev_obj *obj))
  656. {
  657. if (!ent->is_valid)
  658. return 0;
  659. if (port != ent->port)
  660. return 0;
  661. ether_addr_copy(fdb->addr, ent->mac);
  662. fdb->vid = ent->vid;
  663. fdb->ndm_state = ent->is_static ? NUD_NOARP : NUD_REACHABLE;
  664. return cb(&fdb->obj);
  665. }
  666. static int bcm_sf2_sw_fdb_dump(struct dsa_switch *ds, int port,
  667. struct switchdev_obj_port_fdb *fdb,
  668. int (*cb)(struct switchdev_obj *obj))
  669. {
  670. struct bcm_sf2_priv *priv = ds_to_priv(ds);
  671. struct net_device *dev = ds->ports[port];
  672. struct bcm_sf2_arl_entry results[2];
  673. unsigned int count = 0;
  674. int ret;
  675. /* Start search operation */
  676. core_writel(priv, ARLA_SRCH_STDN, CORE_ARLA_SRCH_CTL);
  677. do {
  678. ret = bcm_sf2_arl_search_wait(priv);
  679. if (ret)
  680. return ret;
  681. /* Read both entries, then return their values back */
  682. bcm_sf2_arl_search_rd(priv, 0, &results[0]);
  683. ret = bcm_sf2_sw_fdb_copy(dev, port, &results[0], fdb, cb);
  684. if (ret)
  685. return ret;
  686. bcm_sf2_arl_search_rd(priv, 1, &results[1]);
  687. ret = bcm_sf2_sw_fdb_copy(dev, port, &results[1], fdb, cb);
  688. if (ret)
  689. return ret;
  690. if (!results[0].is_valid && !results[1].is_valid)
  691. break;
  692. } while (count++ < CORE_ARLA_NUM_ENTRIES);
  693. return 0;
  694. }
  695. static irqreturn_t bcm_sf2_switch_0_isr(int irq, void *dev_id)
  696. {
  697. struct bcm_sf2_priv *priv = dev_id;
  698. priv->irq0_stat = intrl2_0_readl(priv, INTRL2_CPU_STATUS) &
  699. ~priv->irq0_mask;
  700. intrl2_0_writel(priv, priv->irq0_stat, INTRL2_CPU_CLEAR);
  701. return IRQ_HANDLED;
  702. }
  703. static irqreturn_t bcm_sf2_switch_1_isr(int irq, void *dev_id)
  704. {
  705. struct bcm_sf2_priv *priv = dev_id;
  706. priv->irq1_stat = intrl2_1_readl(priv, INTRL2_CPU_STATUS) &
  707. ~priv->irq1_mask;
  708. intrl2_1_writel(priv, priv->irq1_stat, INTRL2_CPU_CLEAR);
  709. if (priv->irq1_stat & P_LINK_UP_IRQ(P7_IRQ_OFF))
  710. priv->port_sts[7].link = 1;
  711. if (priv->irq1_stat & P_LINK_DOWN_IRQ(P7_IRQ_OFF))
  712. priv->port_sts[7].link = 0;
  713. return IRQ_HANDLED;
  714. }
  715. static int bcm_sf2_sw_rst(struct bcm_sf2_priv *priv)
  716. {
  717. unsigned int timeout = 1000;
  718. u32 reg;
  719. reg = core_readl(priv, CORE_WATCHDOG_CTRL);
  720. reg |= SOFTWARE_RESET | EN_CHIP_RST | EN_SW_RESET;
  721. core_writel(priv, reg, CORE_WATCHDOG_CTRL);
  722. do {
  723. reg = core_readl(priv, CORE_WATCHDOG_CTRL);
  724. if (!(reg & SOFTWARE_RESET))
  725. break;
  726. usleep_range(1000, 2000);
  727. } while (timeout-- > 0);
  728. if (timeout == 0)
  729. return -ETIMEDOUT;
  730. return 0;
  731. }
  732. static void bcm_sf2_intr_disable(struct bcm_sf2_priv *priv)
  733. {
  734. intrl2_0_writel(priv, 0xffffffff, INTRL2_CPU_MASK_SET);
  735. intrl2_0_writel(priv, 0xffffffff, INTRL2_CPU_CLEAR);
  736. intrl2_0_writel(priv, 0, INTRL2_CPU_MASK_CLEAR);
  737. intrl2_1_writel(priv, 0xffffffff, INTRL2_CPU_MASK_SET);
  738. intrl2_1_writel(priv, 0xffffffff, INTRL2_CPU_CLEAR);
  739. intrl2_1_writel(priv, 0, INTRL2_CPU_MASK_CLEAR);
  740. }
  741. static void bcm_sf2_identify_ports(struct bcm_sf2_priv *priv,
  742. struct device_node *dn)
  743. {
  744. struct device_node *port;
  745. const char *phy_mode_str;
  746. int mode;
  747. unsigned int port_num;
  748. int ret;
  749. priv->moca_port = -1;
  750. for_each_available_child_of_node(dn, port) {
  751. if (of_property_read_u32(port, "reg", &port_num))
  752. continue;
  753. /* Internal PHYs get assigned a specific 'phy-mode' property
  754. * value: "internal" to help flag them before MDIO probing
  755. * has completed, since they might be turned off at that
  756. * time
  757. */
  758. mode = of_get_phy_mode(port);
  759. if (mode < 0) {
  760. ret = of_property_read_string(port, "phy-mode",
  761. &phy_mode_str);
  762. if (ret < 0)
  763. continue;
  764. if (!strcasecmp(phy_mode_str, "internal"))
  765. priv->int_phy_mask |= 1 << port_num;
  766. }
  767. if (mode == PHY_INTERFACE_MODE_MOCA)
  768. priv->moca_port = port_num;
  769. }
  770. }
  771. static int bcm_sf2_sw_setup(struct dsa_switch *ds)
  772. {
  773. const char *reg_names[BCM_SF2_REGS_NUM] = BCM_SF2_REGS_NAME;
  774. struct bcm_sf2_priv *priv = ds_to_priv(ds);
  775. struct device_node *dn;
  776. void __iomem **base;
  777. unsigned int port;
  778. unsigned int i;
  779. u32 reg, rev;
  780. int ret;
  781. spin_lock_init(&priv->indir_lock);
  782. mutex_init(&priv->stats_mutex);
  783. /* All the interesting properties are at the parent device_node
  784. * level
  785. */
  786. dn = ds->pd->of_node->parent;
  787. bcm_sf2_identify_ports(priv, ds->pd->of_node);
  788. priv->irq0 = irq_of_parse_and_map(dn, 0);
  789. priv->irq1 = irq_of_parse_and_map(dn, 1);
  790. base = &priv->core;
  791. for (i = 0; i < BCM_SF2_REGS_NUM; i++) {
  792. *base = of_iomap(dn, i);
  793. if (*base == NULL) {
  794. pr_err("unable to find register: %s\n", reg_names[i]);
  795. ret = -ENOMEM;
  796. goto out_unmap;
  797. }
  798. base++;
  799. }
  800. ret = bcm_sf2_sw_rst(priv);
  801. if (ret) {
  802. pr_err("unable to software reset switch: %d\n", ret);
  803. goto out_unmap;
  804. }
  805. /* Disable all interrupts and request them */
  806. bcm_sf2_intr_disable(priv);
  807. ret = request_irq(priv->irq0, bcm_sf2_switch_0_isr, 0,
  808. "switch_0", priv);
  809. if (ret < 0) {
  810. pr_err("failed to request switch_0 IRQ\n");
  811. goto out_unmap;
  812. }
  813. ret = request_irq(priv->irq1, bcm_sf2_switch_1_isr, 0,
  814. "switch_1", priv);
  815. if (ret < 0) {
  816. pr_err("failed to request switch_1 IRQ\n");
  817. goto out_free_irq0;
  818. }
  819. /* Reset the MIB counters */
  820. reg = core_readl(priv, CORE_GMNCFGCFG);
  821. reg |= RST_MIB_CNT;
  822. core_writel(priv, reg, CORE_GMNCFGCFG);
  823. reg &= ~RST_MIB_CNT;
  824. core_writel(priv, reg, CORE_GMNCFGCFG);
  825. /* Get the maximum number of ports for this switch */
  826. priv->hw_params.num_ports = core_readl(priv, CORE_IMP0_PRT_ID) + 1;
  827. if (priv->hw_params.num_ports > DSA_MAX_PORTS)
  828. priv->hw_params.num_ports = DSA_MAX_PORTS;
  829. /* Assume a single GPHY setup if we can't read that property */
  830. if (of_property_read_u32(dn, "brcm,num-gphy",
  831. &priv->hw_params.num_gphy))
  832. priv->hw_params.num_gphy = 1;
  833. /* Enable all valid ports and disable those unused */
  834. for (port = 0; port < priv->hw_params.num_ports; port++) {
  835. /* IMP port receives special treatment */
  836. if ((1 << port) & ds->enabled_port_mask)
  837. bcm_sf2_port_setup(ds, port, NULL);
  838. else if (dsa_is_cpu_port(ds, port))
  839. bcm_sf2_imp_setup(ds, port);
  840. else
  841. bcm_sf2_port_disable(ds, port, NULL);
  842. }
  843. /* Include the pseudo-PHY address and the broadcast PHY address to
  844. * divert reads towards our workaround. This is only required for
  845. * 7445D0, since 7445E0 disconnects the internal switch pseudo-PHY such
  846. * that we can use the regular SWITCH_MDIO master controller instead.
  847. *
  848. * By default, DSA initializes ds->phys_mii_mask to
  849. * ds->enabled_port_mask to have a 1:1 mapping between Port address
  850. * and PHY address in order to utilize the slave_mii_bus instance to
  851. * read from Port PHYs. This is not what we want here, so we
  852. * initialize phys_mii_mask 0 to always utilize the "master" MDIO
  853. * bus backed by the "mdio-unimac" driver.
  854. */
  855. if (of_machine_is_compatible("brcm,bcm7445d0"))
  856. ds->phys_mii_mask |= ((1 << BRCM_PSEUDO_PHY_ADDR) | (1 << 0));
  857. else
  858. ds->phys_mii_mask = 0;
  859. rev = reg_readl(priv, REG_SWITCH_REVISION);
  860. priv->hw_params.top_rev = (rev >> SWITCH_TOP_REV_SHIFT) &
  861. SWITCH_TOP_REV_MASK;
  862. priv->hw_params.core_rev = (rev & SF2_REV_MASK);
  863. rev = reg_readl(priv, REG_PHY_REVISION);
  864. priv->hw_params.gphy_rev = rev & PHY_REVISION_MASK;
  865. pr_info("Starfighter 2 top: %x.%02x, core: %x.%02x base: 0x%p, IRQs: %d, %d\n",
  866. priv->hw_params.top_rev >> 8, priv->hw_params.top_rev & 0xff,
  867. priv->hw_params.core_rev >> 8, priv->hw_params.core_rev & 0xff,
  868. priv->core, priv->irq0, priv->irq1);
  869. return 0;
  870. out_free_irq0:
  871. free_irq(priv->irq0, priv);
  872. out_unmap:
  873. base = &priv->core;
  874. for (i = 0; i < BCM_SF2_REGS_NUM; i++) {
  875. if (*base)
  876. iounmap(*base);
  877. base++;
  878. }
  879. return ret;
  880. }
  881. static int bcm_sf2_sw_set_addr(struct dsa_switch *ds, u8 *addr)
  882. {
  883. return 0;
  884. }
  885. static u32 bcm_sf2_sw_get_phy_flags(struct dsa_switch *ds, int port)
  886. {
  887. struct bcm_sf2_priv *priv = ds_to_priv(ds);
  888. /* The BCM7xxx PHY driver expects to find the integrated PHY revision
  889. * in bits 15:8 and the patch level in bits 7:0 which is exactly what
  890. * the REG_PHY_REVISION register layout is.
  891. */
  892. return priv->hw_params.gphy_rev;
  893. }
  894. static int bcm_sf2_sw_indir_rw(struct dsa_switch *ds, int op, int addr,
  895. int regnum, u16 val)
  896. {
  897. struct bcm_sf2_priv *priv = ds_to_priv(ds);
  898. int ret = 0;
  899. u32 reg;
  900. reg = reg_readl(priv, REG_SWITCH_CNTRL);
  901. reg |= MDIO_MASTER_SEL;
  902. reg_writel(priv, reg, REG_SWITCH_CNTRL);
  903. /* Page << 8 | offset */
  904. reg = 0x70;
  905. reg <<= 2;
  906. core_writel(priv, addr, reg);
  907. /* Page << 8 | offset */
  908. reg = 0x80 << 8 | regnum << 1;
  909. reg <<= 2;
  910. if (op)
  911. ret = core_readl(priv, reg);
  912. else
  913. core_writel(priv, val, reg);
  914. reg = reg_readl(priv, REG_SWITCH_CNTRL);
  915. reg &= ~MDIO_MASTER_SEL;
  916. reg_writel(priv, reg, REG_SWITCH_CNTRL);
  917. return ret & 0xffff;
  918. }
  919. static int bcm_sf2_sw_phy_read(struct dsa_switch *ds, int addr, int regnum)
  920. {
  921. /* Intercept reads from the MDIO broadcast address or Broadcom
  922. * pseudo-PHY address
  923. */
  924. switch (addr) {
  925. case 0:
  926. case BRCM_PSEUDO_PHY_ADDR:
  927. return bcm_sf2_sw_indir_rw(ds, 1, addr, regnum, 0);
  928. default:
  929. return 0xffff;
  930. }
  931. }
  932. static int bcm_sf2_sw_phy_write(struct dsa_switch *ds, int addr, int regnum,
  933. u16 val)
  934. {
  935. /* Intercept writes to the MDIO broadcast address or Broadcom
  936. * pseudo-PHY address
  937. */
  938. switch (addr) {
  939. case 0:
  940. case BRCM_PSEUDO_PHY_ADDR:
  941. bcm_sf2_sw_indir_rw(ds, 0, addr, regnum, val);
  942. break;
  943. }
  944. return 0;
  945. }
  946. static void bcm_sf2_sw_adjust_link(struct dsa_switch *ds, int port,
  947. struct phy_device *phydev)
  948. {
  949. struct bcm_sf2_priv *priv = ds_to_priv(ds);
  950. u32 id_mode_dis = 0, port_mode;
  951. const char *str = NULL;
  952. u32 reg;
  953. switch (phydev->interface) {
  954. case PHY_INTERFACE_MODE_RGMII:
  955. str = "RGMII (no delay)";
  956. id_mode_dis = 1;
  957. case PHY_INTERFACE_MODE_RGMII_TXID:
  958. if (!str)
  959. str = "RGMII (TX delay)";
  960. port_mode = EXT_GPHY;
  961. break;
  962. case PHY_INTERFACE_MODE_MII:
  963. str = "MII";
  964. port_mode = EXT_EPHY;
  965. break;
  966. case PHY_INTERFACE_MODE_REVMII:
  967. str = "Reverse MII";
  968. port_mode = EXT_REVMII;
  969. break;
  970. default:
  971. /* All other PHYs: internal and MoCA */
  972. goto force_link;
  973. }
  974. /* If the link is down, just disable the interface to conserve power */
  975. if (!phydev->link) {
  976. reg = reg_readl(priv, REG_RGMII_CNTRL_P(port));
  977. reg &= ~RGMII_MODE_EN;
  978. reg_writel(priv, reg, REG_RGMII_CNTRL_P(port));
  979. goto force_link;
  980. }
  981. /* Clear id_mode_dis bit, and the existing port mode, but
  982. * make sure we enable the RGMII block for data to pass
  983. */
  984. reg = reg_readl(priv, REG_RGMII_CNTRL_P(port));
  985. reg &= ~ID_MODE_DIS;
  986. reg &= ~(PORT_MODE_MASK << PORT_MODE_SHIFT);
  987. reg &= ~(RX_PAUSE_EN | TX_PAUSE_EN);
  988. reg |= port_mode | RGMII_MODE_EN;
  989. if (id_mode_dis)
  990. reg |= ID_MODE_DIS;
  991. if (phydev->pause) {
  992. if (phydev->asym_pause)
  993. reg |= TX_PAUSE_EN;
  994. reg |= RX_PAUSE_EN;
  995. }
  996. reg_writel(priv, reg, REG_RGMII_CNTRL_P(port));
  997. pr_info("Port %d configured for %s\n", port, str);
  998. force_link:
  999. /* Force link settings detected from the PHY */
  1000. reg = SW_OVERRIDE;
  1001. switch (phydev->speed) {
  1002. case SPEED_1000:
  1003. reg |= SPDSTS_1000 << SPEED_SHIFT;
  1004. break;
  1005. case SPEED_100:
  1006. reg |= SPDSTS_100 << SPEED_SHIFT;
  1007. break;
  1008. }
  1009. if (phydev->link)
  1010. reg |= LINK_STS;
  1011. if (phydev->duplex == DUPLEX_FULL)
  1012. reg |= DUPLX_MODE;
  1013. core_writel(priv, reg, CORE_STS_OVERRIDE_GMIIP_PORT(port));
  1014. }
  1015. static void bcm_sf2_sw_fixed_link_update(struct dsa_switch *ds, int port,
  1016. struct fixed_phy_status *status)
  1017. {
  1018. struct bcm_sf2_priv *priv = ds_to_priv(ds);
  1019. u32 duplex, pause;
  1020. u32 reg;
  1021. duplex = core_readl(priv, CORE_DUPSTS);
  1022. pause = core_readl(priv, CORE_PAUSESTS);
  1023. status->link = 0;
  1024. /* MoCA port is special as we do not get link status from CORE_LNKSTS,
  1025. * which means that we need to force the link at the port override
  1026. * level to get the data to flow. We do use what the interrupt handler
  1027. * did determine before.
  1028. *
  1029. * For the other ports, we just force the link status, since this is
  1030. * a fixed PHY device.
  1031. */
  1032. if (port == priv->moca_port) {
  1033. status->link = priv->port_sts[port].link;
  1034. /* For MoCA interfaces, also force a link down notification
  1035. * since some version of the user-space daemon (mocad) use
  1036. * cmd->autoneg to force the link, which messes up the PHY
  1037. * state machine and make it go in PHY_FORCING state instead.
  1038. */
  1039. if (!status->link)
  1040. netif_carrier_off(ds->ports[port]);
  1041. status->duplex = 1;
  1042. } else {
  1043. status->link = 1;
  1044. status->duplex = !!(duplex & (1 << port));
  1045. }
  1046. reg = core_readl(priv, CORE_STS_OVERRIDE_GMIIP_PORT(port));
  1047. reg |= SW_OVERRIDE;
  1048. if (status->link)
  1049. reg |= LINK_STS;
  1050. else
  1051. reg &= ~LINK_STS;
  1052. core_writel(priv, reg, CORE_STS_OVERRIDE_GMIIP_PORT(port));
  1053. if ((pause & (1 << port)) &&
  1054. (pause & (1 << (port + PAUSESTS_TX_PAUSE_SHIFT)))) {
  1055. status->asym_pause = 1;
  1056. status->pause = 1;
  1057. }
  1058. if (pause & (1 << port))
  1059. status->pause = 1;
  1060. }
  1061. static int bcm_sf2_sw_suspend(struct dsa_switch *ds)
  1062. {
  1063. struct bcm_sf2_priv *priv = ds_to_priv(ds);
  1064. unsigned int port;
  1065. bcm_sf2_intr_disable(priv);
  1066. /* Disable all ports physically present including the IMP
  1067. * port, the other ones have already been disabled during
  1068. * bcm_sf2_sw_setup
  1069. */
  1070. for (port = 0; port < DSA_MAX_PORTS; port++) {
  1071. if ((1 << port) & ds->enabled_port_mask ||
  1072. dsa_is_cpu_port(ds, port))
  1073. bcm_sf2_port_disable(ds, port, NULL);
  1074. }
  1075. return 0;
  1076. }
  1077. static int bcm_sf2_sw_resume(struct dsa_switch *ds)
  1078. {
  1079. struct bcm_sf2_priv *priv = ds_to_priv(ds);
  1080. unsigned int port;
  1081. int ret;
  1082. ret = bcm_sf2_sw_rst(priv);
  1083. if (ret) {
  1084. pr_err("%s: failed to software reset switch\n", __func__);
  1085. return ret;
  1086. }
  1087. if (priv->hw_params.num_gphy == 1)
  1088. bcm_sf2_gphy_enable_set(ds, true);
  1089. for (port = 0; port < DSA_MAX_PORTS; port++) {
  1090. if ((1 << port) & ds->enabled_port_mask)
  1091. bcm_sf2_port_setup(ds, port, NULL);
  1092. else if (dsa_is_cpu_port(ds, port))
  1093. bcm_sf2_imp_setup(ds, port);
  1094. }
  1095. return 0;
  1096. }
  1097. static void bcm_sf2_sw_get_wol(struct dsa_switch *ds, int port,
  1098. struct ethtool_wolinfo *wol)
  1099. {
  1100. struct net_device *p = ds->dst[ds->index].master_netdev;
  1101. struct bcm_sf2_priv *priv = ds_to_priv(ds);
  1102. struct ethtool_wolinfo pwol;
  1103. /* Get the parent device WoL settings */
  1104. p->ethtool_ops->get_wol(p, &pwol);
  1105. /* Advertise the parent device supported settings */
  1106. wol->supported = pwol.supported;
  1107. memset(&wol->sopass, 0, sizeof(wol->sopass));
  1108. if (pwol.wolopts & WAKE_MAGICSECURE)
  1109. memcpy(&wol->sopass, pwol.sopass, sizeof(wol->sopass));
  1110. if (priv->wol_ports_mask & (1 << port))
  1111. wol->wolopts = pwol.wolopts;
  1112. else
  1113. wol->wolopts = 0;
  1114. }
  1115. static int bcm_sf2_sw_set_wol(struct dsa_switch *ds, int port,
  1116. struct ethtool_wolinfo *wol)
  1117. {
  1118. struct net_device *p = ds->dst[ds->index].master_netdev;
  1119. struct bcm_sf2_priv *priv = ds_to_priv(ds);
  1120. s8 cpu_port = ds->dst[ds->index].cpu_port;
  1121. struct ethtool_wolinfo pwol;
  1122. p->ethtool_ops->get_wol(p, &pwol);
  1123. if (wol->wolopts & ~pwol.supported)
  1124. return -EINVAL;
  1125. if (wol->wolopts)
  1126. priv->wol_ports_mask |= (1 << port);
  1127. else
  1128. priv->wol_ports_mask &= ~(1 << port);
  1129. /* If we have at least one port enabled, make sure the CPU port
  1130. * is also enabled. If the CPU port is the last one enabled, we disable
  1131. * it since this configuration does not make sense.
  1132. */
  1133. if (priv->wol_ports_mask && priv->wol_ports_mask != (1 << cpu_port))
  1134. priv->wol_ports_mask |= (1 << cpu_port);
  1135. else
  1136. priv->wol_ports_mask &= ~(1 << cpu_port);
  1137. return p->ethtool_ops->set_wol(p, wol);
  1138. }
  1139. static struct dsa_switch_driver bcm_sf2_switch_driver = {
  1140. .tag_protocol = DSA_TAG_PROTO_BRCM,
  1141. .probe = bcm_sf2_sw_drv_probe,
  1142. .setup = bcm_sf2_sw_setup,
  1143. .set_addr = bcm_sf2_sw_set_addr,
  1144. .get_phy_flags = bcm_sf2_sw_get_phy_flags,
  1145. .phy_read = bcm_sf2_sw_phy_read,
  1146. .phy_write = bcm_sf2_sw_phy_write,
  1147. .get_strings = bcm_sf2_sw_get_strings,
  1148. .get_ethtool_stats = bcm_sf2_sw_get_ethtool_stats,
  1149. .get_sset_count = bcm_sf2_sw_get_sset_count,
  1150. .adjust_link = bcm_sf2_sw_adjust_link,
  1151. .fixed_link_update = bcm_sf2_sw_fixed_link_update,
  1152. .suspend = bcm_sf2_sw_suspend,
  1153. .resume = bcm_sf2_sw_resume,
  1154. .get_wol = bcm_sf2_sw_get_wol,
  1155. .set_wol = bcm_sf2_sw_set_wol,
  1156. .port_enable = bcm_sf2_port_setup,
  1157. .port_disable = bcm_sf2_port_disable,
  1158. .get_eee = bcm_sf2_sw_get_eee,
  1159. .set_eee = bcm_sf2_sw_set_eee,
  1160. .port_bridge_join = bcm_sf2_sw_br_join,
  1161. .port_bridge_leave = bcm_sf2_sw_br_leave,
  1162. .port_stp_state_set = bcm_sf2_sw_br_set_stp_state,
  1163. .port_fdb_prepare = bcm_sf2_sw_fdb_prepare,
  1164. .port_fdb_add = bcm_sf2_sw_fdb_add,
  1165. .port_fdb_del = bcm_sf2_sw_fdb_del,
  1166. .port_fdb_dump = bcm_sf2_sw_fdb_dump,
  1167. };
  1168. static int __init bcm_sf2_init(void)
  1169. {
  1170. register_switch_driver(&bcm_sf2_switch_driver);
  1171. return 0;
  1172. }
  1173. module_init(bcm_sf2_init);
  1174. static void __exit bcm_sf2_exit(void)
  1175. {
  1176. unregister_switch_driver(&bcm_sf2_switch_driver);
  1177. }
  1178. module_exit(bcm_sf2_exit);
  1179. MODULE_AUTHOR("Broadcom Corporation");
  1180. MODULE_DESCRIPTION("Driver for Broadcom Starfighter 2 ethernet switch chip");
  1181. MODULE_LICENSE("GPL");
  1182. MODULE_ALIAS("platform:brcm-sf2");