bcm_sf2.c 37 KB

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