bcm_sf2.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176
  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 <linux/platform_data/b53.h>
  32. #include "bcm_sf2.h"
  33. #include "bcm_sf2_regs.h"
  34. #include "b53/b53_priv.h"
  35. #include "b53/b53_regs.h"
  36. static enum dsa_tag_protocol bcm_sf2_sw_get_tag_protocol(struct dsa_switch *ds)
  37. {
  38. return DSA_TAG_PROTO_BRCM;
  39. }
  40. static void bcm_sf2_imp_vlan_setup(struct dsa_switch *ds, int cpu_port)
  41. {
  42. struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
  43. unsigned int i;
  44. u32 reg;
  45. /* Enable the IMP Port to be in the same VLAN as the other ports
  46. * on a per-port basis such that we only have Port i and IMP in
  47. * the same VLAN.
  48. */
  49. for (i = 0; i < priv->hw_params.num_ports; i++) {
  50. if (!((1 << i) & ds->enabled_port_mask))
  51. continue;
  52. reg = core_readl(priv, CORE_PORT_VLAN_CTL_PORT(i));
  53. reg |= (1 << cpu_port);
  54. core_writel(priv, reg, CORE_PORT_VLAN_CTL_PORT(i));
  55. }
  56. }
  57. static void bcm_sf2_imp_setup(struct dsa_switch *ds, int port)
  58. {
  59. struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
  60. u32 reg, val;
  61. /* Enable the port memories */
  62. reg = core_readl(priv, CORE_MEM_PSM_VDD_CTRL);
  63. reg &= ~P_TXQ_PSM_VDD(port);
  64. core_writel(priv, reg, CORE_MEM_PSM_VDD_CTRL);
  65. /* Enable Broadcast, Multicast, Unicast forwarding to IMP port */
  66. reg = core_readl(priv, CORE_IMP_CTL);
  67. reg |= (RX_BCST_EN | RX_MCST_EN | RX_UCST_EN);
  68. reg &= ~(RX_DIS | TX_DIS);
  69. core_writel(priv, reg, CORE_IMP_CTL);
  70. /* Enable forwarding */
  71. core_writel(priv, SW_FWDG_EN, CORE_SWMODE);
  72. /* Enable IMP port in dumb mode */
  73. reg = core_readl(priv, CORE_SWITCH_CTRL);
  74. reg |= MII_DUMB_FWDG_EN;
  75. core_writel(priv, reg, CORE_SWITCH_CTRL);
  76. /* Resolve which bit controls the Broadcom tag */
  77. switch (port) {
  78. case 8:
  79. val = BRCM_HDR_EN_P8;
  80. break;
  81. case 7:
  82. val = BRCM_HDR_EN_P7;
  83. break;
  84. case 5:
  85. val = BRCM_HDR_EN_P5;
  86. break;
  87. default:
  88. val = 0;
  89. break;
  90. }
  91. /* Enable Broadcom tags for IMP port */
  92. reg = core_readl(priv, CORE_BRCM_HDR_CTRL);
  93. reg |= val;
  94. core_writel(priv, reg, CORE_BRCM_HDR_CTRL);
  95. /* Enable reception Broadcom tag for CPU TX (switch RX) to
  96. * allow us to tag outgoing frames
  97. */
  98. reg = core_readl(priv, CORE_BRCM_HDR_RX_DIS);
  99. reg &= ~(1 << port);
  100. core_writel(priv, reg, CORE_BRCM_HDR_RX_DIS);
  101. /* Enable transmission of Broadcom tags from the switch (CPU RX) to
  102. * allow delivering frames to the per-port net_devices
  103. */
  104. reg = core_readl(priv, CORE_BRCM_HDR_TX_DIS);
  105. reg &= ~(1 << port);
  106. core_writel(priv, reg, CORE_BRCM_HDR_TX_DIS);
  107. /* Force link status for IMP port */
  108. reg = core_readl(priv, CORE_STS_OVERRIDE_IMP);
  109. reg |= (MII_SW_OR | LINK_STS);
  110. core_writel(priv, reg, CORE_STS_OVERRIDE_IMP);
  111. }
  112. static void bcm_sf2_eee_enable_set(struct dsa_switch *ds, int port, bool enable)
  113. {
  114. struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
  115. u32 reg;
  116. reg = core_readl(priv, CORE_EEE_EN_CTRL);
  117. if (enable)
  118. reg |= 1 << port;
  119. else
  120. reg &= ~(1 << port);
  121. core_writel(priv, reg, CORE_EEE_EN_CTRL);
  122. }
  123. static void bcm_sf2_gphy_enable_set(struct dsa_switch *ds, bool enable)
  124. {
  125. struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
  126. u32 reg;
  127. reg = reg_readl(priv, REG_SPHY_CNTRL);
  128. if (enable) {
  129. reg |= PHY_RESET;
  130. reg &= ~(EXT_PWR_DOWN | IDDQ_BIAS | CK25_DIS);
  131. reg_writel(priv, reg, REG_SPHY_CNTRL);
  132. udelay(21);
  133. reg = reg_readl(priv, REG_SPHY_CNTRL);
  134. reg &= ~PHY_RESET;
  135. } else {
  136. reg |= EXT_PWR_DOWN | IDDQ_BIAS | PHY_RESET;
  137. reg_writel(priv, reg, REG_SPHY_CNTRL);
  138. mdelay(1);
  139. reg |= CK25_DIS;
  140. }
  141. reg_writel(priv, reg, REG_SPHY_CNTRL);
  142. /* Use PHY-driven LED signaling */
  143. if (!enable) {
  144. reg = reg_readl(priv, REG_LED_CNTRL(0));
  145. reg |= SPDLNK_SRC_SEL;
  146. reg_writel(priv, reg, REG_LED_CNTRL(0));
  147. }
  148. }
  149. static inline void bcm_sf2_port_intr_enable(struct bcm_sf2_priv *priv,
  150. int port)
  151. {
  152. unsigned int off;
  153. switch (port) {
  154. case 7:
  155. off = P7_IRQ_OFF;
  156. break;
  157. case 0:
  158. /* Port 0 interrupts are located on the first bank */
  159. intrl2_0_mask_clear(priv, P_IRQ_MASK(P0_IRQ_OFF));
  160. return;
  161. default:
  162. off = P_IRQ_OFF(port);
  163. break;
  164. }
  165. intrl2_1_mask_clear(priv, P_IRQ_MASK(off));
  166. }
  167. static inline void bcm_sf2_port_intr_disable(struct bcm_sf2_priv *priv,
  168. int port)
  169. {
  170. unsigned int off;
  171. switch (port) {
  172. case 7:
  173. off = P7_IRQ_OFF;
  174. break;
  175. case 0:
  176. /* Port 0 interrupts are located on the first bank */
  177. intrl2_0_mask_set(priv, P_IRQ_MASK(P0_IRQ_OFF));
  178. intrl2_0_writel(priv, P_IRQ_MASK(P0_IRQ_OFF), INTRL2_CPU_CLEAR);
  179. return;
  180. default:
  181. off = P_IRQ_OFF(port);
  182. break;
  183. }
  184. intrl2_1_mask_set(priv, P_IRQ_MASK(off));
  185. intrl2_1_writel(priv, P_IRQ_MASK(off), INTRL2_CPU_CLEAR);
  186. }
  187. static int bcm_sf2_port_setup(struct dsa_switch *ds, int port,
  188. struct phy_device *phy)
  189. {
  190. struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
  191. s8 cpu_port = ds->dst[ds->index].cpu_port;
  192. u32 reg;
  193. /* Clear the memory power down */
  194. reg = core_readl(priv, CORE_MEM_PSM_VDD_CTRL);
  195. reg &= ~P_TXQ_PSM_VDD(port);
  196. core_writel(priv, reg, CORE_MEM_PSM_VDD_CTRL);
  197. /* Clear the Rx and Tx disable bits and set to no spanning tree */
  198. core_writel(priv, 0, CORE_G_PCTL_PORT(port));
  199. /* Re-enable the GPHY and re-apply workarounds */
  200. if (priv->int_phy_mask & 1 << port && priv->hw_params.num_gphy == 1) {
  201. bcm_sf2_gphy_enable_set(ds, true);
  202. if (phy) {
  203. /* if phy_stop() has been called before, phy
  204. * will be in halted state, and phy_start()
  205. * will call resume.
  206. *
  207. * the resume path does not configure back
  208. * autoneg settings, and since we hard reset
  209. * the phy manually here, we need to reset the
  210. * state machine also.
  211. */
  212. phy->state = PHY_READY;
  213. phy_init_hw(phy);
  214. }
  215. }
  216. /* Enable MoCA port interrupts to get notified */
  217. if (port == priv->moca_port)
  218. bcm_sf2_port_intr_enable(priv, port);
  219. /* Set this port, and only this one to be in the default VLAN,
  220. * if member of a bridge, restore its membership prior to
  221. * bringing down this port.
  222. */
  223. reg = core_readl(priv, CORE_PORT_VLAN_CTL_PORT(port));
  224. reg &= ~PORT_VLAN_CTRL_MASK;
  225. reg |= (1 << port);
  226. reg |= priv->dev->ports[port].vlan_ctl_mask;
  227. core_writel(priv, reg, CORE_PORT_VLAN_CTL_PORT(port));
  228. bcm_sf2_imp_vlan_setup(ds, cpu_port);
  229. /* If EEE was enabled, restore it */
  230. if (priv->port_sts[port].eee.eee_enabled)
  231. bcm_sf2_eee_enable_set(ds, port, true);
  232. return 0;
  233. }
  234. static void bcm_sf2_port_disable(struct dsa_switch *ds, int port,
  235. struct phy_device *phy)
  236. {
  237. struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
  238. u32 off, reg;
  239. if (priv->wol_ports_mask & (1 << port))
  240. return;
  241. if (port == priv->moca_port)
  242. bcm_sf2_port_intr_disable(priv, port);
  243. if (priv->int_phy_mask & 1 << port && priv->hw_params.num_gphy == 1)
  244. bcm_sf2_gphy_enable_set(ds, false);
  245. if (dsa_is_cpu_port(ds, port))
  246. off = CORE_IMP_CTL;
  247. else
  248. off = CORE_G_PCTL_PORT(port);
  249. reg = core_readl(priv, off);
  250. reg |= RX_DIS | TX_DIS;
  251. core_writel(priv, reg, off);
  252. /* Power down the port memory */
  253. reg = core_readl(priv, CORE_MEM_PSM_VDD_CTRL);
  254. reg |= P_TXQ_PSM_VDD(port);
  255. core_writel(priv, reg, CORE_MEM_PSM_VDD_CTRL);
  256. }
  257. /* Returns 0 if EEE was not enabled, or 1 otherwise
  258. */
  259. static int bcm_sf2_eee_init(struct dsa_switch *ds, int port,
  260. struct phy_device *phy)
  261. {
  262. struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
  263. struct ethtool_eee *p = &priv->port_sts[port].eee;
  264. int ret;
  265. p->supported = (SUPPORTED_1000baseT_Full | SUPPORTED_100baseT_Full);
  266. ret = phy_init_eee(phy, 0);
  267. if (ret)
  268. return 0;
  269. bcm_sf2_eee_enable_set(ds, port, true);
  270. return 1;
  271. }
  272. static int bcm_sf2_sw_get_eee(struct dsa_switch *ds, int port,
  273. struct ethtool_eee *e)
  274. {
  275. struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
  276. struct ethtool_eee *p = &priv->port_sts[port].eee;
  277. u32 reg;
  278. reg = core_readl(priv, CORE_EEE_LPI_INDICATE);
  279. e->eee_enabled = p->eee_enabled;
  280. e->eee_active = !!(reg & (1 << port));
  281. return 0;
  282. }
  283. static int bcm_sf2_sw_set_eee(struct dsa_switch *ds, int port,
  284. struct phy_device *phydev,
  285. struct ethtool_eee *e)
  286. {
  287. struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
  288. struct ethtool_eee *p = &priv->port_sts[port].eee;
  289. p->eee_enabled = e->eee_enabled;
  290. if (!p->eee_enabled) {
  291. bcm_sf2_eee_enable_set(ds, port, false);
  292. } else {
  293. p->eee_enabled = bcm_sf2_eee_init(ds, port, phydev);
  294. if (!p->eee_enabled)
  295. return -EOPNOTSUPP;
  296. }
  297. return 0;
  298. }
  299. static int bcm_sf2_sw_indir_rw(struct bcm_sf2_priv *priv, int op, int addr,
  300. int regnum, u16 val)
  301. {
  302. int ret = 0;
  303. u32 reg;
  304. reg = reg_readl(priv, REG_SWITCH_CNTRL);
  305. reg |= MDIO_MASTER_SEL;
  306. reg_writel(priv, reg, REG_SWITCH_CNTRL);
  307. /* Page << 8 | offset */
  308. reg = 0x70;
  309. reg <<= 2;
  310. core_writel(priv, addr, reg);
  311. /* Page << 8 | offset */
  312. reg = 0x80 << 8 | regnum << 1;
  313. reg <<= 2;
  314. if (op)
  315. ret = core_readl(priv, reg);
  316. else
  317. core_writel(priv, val, reg);
  318. reg = reg_readl(priv, REG_SWITCH_CNTRL);
  319. reg &= ~MDIO_MASTER_SEL;
  320. reg_writel(priv, reg, REG_SWITCH_CNTRL);
  321. return ret & 0xffff;
  322. }
  323. static int bcm_sf2_sw_mdio_read(struct mii_bus *bus, int addr, int regnum)
  324. {
  325. struct bcm_sf2_priv *priv = bus->priv;
  326. /* Intercept reads from Broadcom pseudo-PHY address, else, send
  327. * them to our master MDIO bus controller
  328. */
  329. if (addr == BRCM_PSEUDO_PHY_ADDR && priv->indir_phy_mask & BIT(addr))
  330. return bcm_sf2_sw_indir_rw(priv, 1, addr, regnum, 0);
  331. else
  332. return mdiobus_read(priv->master_mii_bus, addr, regnum);
  333. }
  334. static int bcm_sf2_sw_mdio_write(struct mii_bus *bus, int addr, int regnum,
  335. u16 val)
  336. {
  337. struct bcm_sf2_priv *priv = bus->priv;
  338. /* Intercept writes to the Broadcom pseudo-PHY address, else,
  339. * send them to our master MDIO bus controller
  340. */
  341. if (addr == BRCM_PSEUDO_PHY_ADDR && priv->indir_phy_mask & BIT(addr))
  342. bcm_sf2_sw_indir_rw(priv, 0, addr, regnum, val);
  343. else
  344. mdiobus_write(priv->master_mii_bus, addr, regnum, val);
  345. return 0;
  346. }
  347. static irqreturn_t bcm_sf2_switch_0_isr(int irq, void *dev_id)
  348. {
  349. struct bcm_sf2_priv *priv = dev_id;
  350. priv->irq0_stat = intrl2_0_readl(priv, INTRL2_CPU_STATUS) &
  351. ~priv->irq0_mask;
  352. intrl2_0_writel(priv, priv->irq0_stat, INTRL2_CPU_CLEAR);
  353. return IRQ_HANDLED;
  354. }
  355. static irqreturn_t bcm_sf2_switch_1_isr(int irq, void *dev_id)
  356. {
  357. struct bcm_sf2_priv *priv = dev_id;
  358. priv->irq1_stat = intrl2_1_readl(priv, INTRL2_CPU_STATUS) &
  359. ~priv->irq1_mask;
  360. intrl2_1_writel(priv, priv->irq1_stat, INTRL2_CPU_CLEAR);
  361. if (priv->irq1_stat & P_LINK_UP_IRQ(P7_IRQ_OFF))
  362. priv->port_sts[7].link = 1;
  363. if (priv->irq1_stat & P_LINK_DOWN_IRQ(P7_IRQ_OFF))
  364. priv->port_sts[7].link = 0;
  365. return IRQ_HANDLED;
  366. }
  367. static int bcm_sf2_sw_rst(struct bcm_sf2_priv *priv)
  368. {
  369. unsigned int timeout = 1000;
  370. u32 reg;
  371. reg = core_readl(priv, CORE_WATCHDOG_CTRL);
  372. reg |= SOFTWARE_RESET | EN_CHIP_RST | EN_SW_RESET;
  373. core_writel(priv, reg, CORE_WATCHDOG_CTRL);
  374. do {
  375. reg = core_readl(priv, CORE_WATCHDOG_CTRL);
  376. if (!(reg & SOFTWARE_RESET))
  377. break;
  378. usleep_range(1000, 2000);
  379. } while (timeout-- > 0);
  380. if (timeout == 0)
  381. return -ETIMEDOUT;
  382. return 0;
  383. }
  384. static void bcm_sf2_intr_disable(struct bcm_sf2_priv *priv)
  385. {
  386. intrl2_0_mask_set(priv, 0xffffffff);
  387. intrl2_0_writel(priv, 0xffffffff, INTRL2_CPU_CLEAR);
  388. intrl2_1_mask_set(priv, 0xffffffff);
  389. intrl2_1_writel(priv, 0xffffffff, INTRL2_CPU_CLEAR);
  390. }
  391. static void bcm_sf2_identify_ports(struct bcm_sf2_priv *priv,
  392. struct device_node *dn)
  393. {
  394. struct device_node *port;
  395. const char *phy_mode_str;
  396. int mode;
  397. unsigned int port_num;
  398. int ret;
  399. priv->moca_port = -1;
  400. for_each_available_child_of_node(dn, port) {
  401. if (of_property_read_u32(port, "reg", &port_num))
  402. continue;
  403. /* Internal PHYs get assigned a specific 'phy-mode' property
  404. * value: "internal" to help flag them before MDIO probing
  405. * has completed, since they might be turned off at that
  406. * time
  407. */
  408. mode = of_get_phy_mode(port);
  409. if (mode < 0) {
  410. ret = of_property_read_string(port, "phy-mode",
  411. &phy_mode_str);
  412. if (ret < 0)
  413. continue;
  414. if (!strcasecmp(phy_mode_str, "internal"))
  415. priv->int_phy_mask |= 1 << port_num;
  416. }
  417. if (mode == PHY_INTERFACE_MODE_MOCA)
  418. priv->moca_port = port_num;
  419. }
  420. }
  421. static int bcm_sf2_mdio_register(struct dsa_switch *ds)
  422. {
  423. struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
  424. struct device_node *dn;
  425. static int index;
  426. int err;
  427. /* Find our integrated MDIO bus node */
  428. dn = of_find_compatible_node(NULL, NULL, "brcm,unimac-mdio");
  429. priv->master_mii_bus = of_mdio_find_bus(dn);
  430. if (!priv->master_mii_bus)
  431. return -EPROBE_DEFER;
  432. get_device(&priv->master_mii_bus->dev);
  433. priv->master_mii_dn = dn;
  434. priv->slave_mii_bus = devm_mdiobus_alloc(ds->dev);
  435. if (!priv->slave_mii_bus)
  436. return -ENOMEM;
  437. priv->slave_mii_bus->priv = priv;
  438. priv->slave_mii_bus->name = "sf2 slave mii";
  439. priv->slave_mii_bus->read = bcm_sf2_sw_mdio_read;
  440. priv->slave_mii_bus->write = bcm_sf2_sw_mdio_write;
  441. snprintf(priv->slave_mii_bus->id, MII_BUS_ID_SIZE, "sf2-%d",
  442. index++);
  443. priv->slave_mii_bus->dev.of_node = dn;
  444. /* Include the pseudo-PHY address to divert reads towards our
  445. * workaround. This is only required for 7445D0, since 7445E0
  446. * disconnects the internal switch pseudo-PHY such that we can use the
  447. * regular SWITCH_MDIO master controller instead.
  448. *
  449. * Here we flag the pseudo PHY as needing special treatment and would
  450. * otherwise make all other PHY read/writes go to the master MDIO bus
  451. * controller that comes with this switch backed by the "mdio-unimac"
  452. * driver.
  453. */
  454. if (of_machine_is_compatible("brcm,bcm7445d0"))
  455. priv->indir_phy_mask |= (1 << BRCM_PSEUDO_PHY_ADDR);
  456. else
  457. priv->indir_phy_mask = 0;
  458. ds->phys_mii_mask = priv->indir_phy_mask;
  459. ds->slave_mii_bus = priv->slave_mii_bus;
  460. priv->slave_mii_bus->parent = ds->dev->parent;
  461. priv->slave_mii_bus->phy_mask = ~priv->indir_phy_mask;
  462. if (dn)
  463. err = of_mdiobus_register(priv->slave_mii_bus, dn);
  464. else
  465. err = mdiobus_register(priv->slave_mii_bus);
  466. if (err)
  467. of_node_put(dn);
  468. return err;
  469. }
  470. static void bcm_sf2_mdio_unregister(struct bcm_sf2_priv *priv)
  471. {
  472. mdiobus_unregister(priv->slave_mii_bus);
  473. if (priv->master_mii_dn)
  474. of_node_put(priv->master_mii_dn);
  475. }
  476. static u32 bcm_sf2_sw_get_phy_flags(struct dsa_switch *ds, int port)
  477. {
  478. struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
  479. /* The BCM7xxx PHY driver expects to find the integrated PHY revision
  480. * in bits 15:8 and the patch level in bits 7:0 which is exactly what
  481. * the REG_PHY_REVISION register layout is.
  482. */
  483. return priv->hw_params.gphy_rev;
  484. }
  485. static void bcm_sf2_sw_adjust_link(struct dsa_switch *ds, int port,
  486. struct phy_device *phydev)
  487. {
  488. struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
  489. u32 id_mode_dis = 0, port_mode;
  490. const char *str = NULL;
  491. u32 reg;
  492. switch (phydev->interface) {
  493. case PHY_INTERFACE_MODE_RGMII:
  494. str = "RGMII (no delay)";
  495. id_mode_dis = 1;
  496. case PHY_INTERFACE_MODE_RGMII_TXID:
  497. if (!str)
  498. str = "RGMII (TX delay)";
  499. port_mode = EXT_GPHY;
  500. break;
  501. case PHY_INTERFACE_MODE_MII:
  502. str = "MII";
  503. port_mode = EXT_EPHY;
  504. break;
  505. case PHY_INTERFACE_MODE_REVMII:
  506. str = "Reverse MII";
  507. port_mode = EXT_REVMII;
  508. break;
  509. default:
  510. /* All other PHYs: internal and MoCA */
  511. goto force_link;
  512. }
  513. /* If the link is down, just disable the interface to conserve power */
  514. if (!phydev->link) {
  515. reg = reg_readl(priv, REG_RGMII_CNTRL_P(port));
  516. reg &= ~RGMII_MODE_EN;
  517. reg_writel(priv, reg, REG_RGMII_CNTRL_P(port));
  518. goto force_link;
  519. }
  520. /* Clear id_mode_dis bit, and the existing port mode, but
  521. * make sure we enable the RGMII block for data to pass
  522. */
  523. reg = reg_readl(priv, REG_RGMII_CNTRL_P(port));
  524. reg &= ~ID_MODE_DIS;
  525. reg &= ~(PORT_MODE_MASK << PORT_MODE_SHIFT);
  526. reg &= ~(RX_PAUSE_EN | TX_PAUSE_EN);
  527. reg |= port_mode | RGMII_MODE_EN;
  528. if (id_mode_dis)
  529. reg |= ID_MODE_DIS;
  530. if (phydev->pause) {
  531. if (phydev->asym_pause)
  532. reg |= TX_PAUSE_EN;
  533. reg |= RX_PAUSE_EN;
  534. }
  535. reg_writel(priv, reg, REG_RGMII_CNTRL_P(port));
  536. pr_info("Port %d configured for %s\n", port, str);
  537. force_link:
  538. /* Force link settings detected from the PHY */
  539. reg = SW_OVERRIDE;
  540. switch (phydev->speed) {
  541. case SPEED_1000:
  542. reg |= SPDSTS_1000 << SPEED_SHIFT;
  543. break;
  544. case SPEED_100:
  545. reg |= SPDSTS_100 << SPEED_SHIFT;
  546. break;
  547. }
  548. if (phydev->link)
  549. reg |= LINK_STS;
  550. if (phydev->duplex == DUPLEX_FULL)
  551. reg |= DUPLX_MODE;
  552. core_writel(priv, reg, CORE_STS_OVERRIDE_GMIIP_PORT(port));
  553. }
  554. static void bcm_sf2_sw_fixed_link_update(struct dsa_switch *ds, int port,
  555. struct fixed_phy_status *status)
  556. {
  557. struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
  558. u32 duplex, pause;
  559. u32 reg;
  560. duplex = core_readl(priv, CORE_DUPSTS);
  561. pause = core_readl(priv, CORE_PAUSESTS);
  562. status->link = 0;
  563. /* MoCA port is special as we do not get link status from CORE_LNKSTS,
  564. * which means that we need to force the link at the port override
  565. * level to get the data to flow. We do use what the interrupt handler
  566. * did determine before.
  567. *
  568. * For the other ports, we just force the link status, since this is
  569. * a fixed PHY device.
  570. */
  571. if (port == priv->moca_port) {
  572. status->link = priv->port_sts[port].link;
  573. /* For MoCA interfaces, also force a link down notification
  574. * since some version of the user-space daemon (mocad) use
  575. * cmd->autoneg to force the link, which messes up the PHY
  576. * state machine and make it go in PHY_FORCING state instead.
  577. */
  578. if (!status->link)
  579. netif_carrier_off(ds->ports[port].netdev);
  580. status->duplex = 1;
  581. } else {
  582. status->link = 1;
  583. status->duplex = !!(duplex & (1 << port));
  584. }
  585. reg = core_readl(priv, CORE_STS_OVERRIDE_GMIIP_PORT(port));
  586. reg |= SW_OVERRIDE;
  587. if (status->link)
  588. reg |= LINK_STS;
  589. else
  590. reg &= ~LINK_STS;
  591. core_writel(priv, reg, CORE_STS_OVERRIDE_GMIIP_PORT(port));
  592. if ((pause & (1 << port)) &&
  593. (pause & (1 << (port + PAUSESTS_TX_PAUSE_SHIFT)))) {
  594. status->asym_pause = 1;
  595. status->pause = 1;
  596. }
  597. if (pause & (1 << port))
  598. status->pause = 1;
  599. }
  600. static int bcm_sf2_sw_suspend(struct dsa_switch *ds)
  601. {
  602. struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
  603. unsigned int port;
  604. bcm_sf2_intr_disable(priv);
  605. /* Disable all ports physically present including the IMP
  606. * port, the other ones have already been disabled during
  607. * bcm_sf2_sw_setup
  608. */
  609. for (port = 0; port < DSA_MAX_PORTS; port++) {
  610. if ((1 << port) & ds->enabled_port_mask ||
  611. dsa_is_cpu_port(ds, port))
  612. bcm_sf2_port_disable(ds, port, NULL);
  613. }
  614. return 0;
  615. }
  616. static int bcm_sf2_sw_resume(struct dsa_switch *ds)
  617. {
  618. struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
  619. unsigned int port;
  620. int ret;
  621. ret = bcm_sf2_sw_rst(priv);
  622. if (ret) {
  623. pr_err("%s: failed to software reset switch\n", __func__);
  624. return ret;
  625. }
  626. if (priv->hw_params.num_gphy == 1)
  627. bcm_sf2_gphy_enable_set(ds, true);
  628. for (port = 0; port < DSA_MAX_PORTS; port++) {
  629. if ((1 << port) & ds->enabled_port_mask)
  630. bcm_sf2_port_setup(ds, port, NULL);
  631. else if (dsa_is_cpu_port(ds, port))
  632. bcm_sf2_imp_setup(ds, port);
  633. }
  634. return 0;
  635. }
  636. static void bcm_sf2_sw_get_wol(struct dsa_switch *ds, int port,
  637. struct ethtool_wolinfo *wol)
  638. {
  639. struct net_device *p = ds->dst[ds->index].master_netdev;
  640. struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
  641. struct ethtool_wolinfo pwol;
  642. /* Get the parent device WoL settings */
  643. p->ethtool_ops->get_wol(p, &pwol);
  644. /* Advertise the parent device supported settings */
  645. wol->supported = pwol.supported;
  646. memset(&wol->sopass, 0, sizeof(wol->sopass));
  647. if (pwol.wolopts & WAKE_MAGICSECURE)
  648. memcpy(&wol->sopass, pwol.sopass, sizeof(wol->sopass));
  649. if (priv->wol_ports_mask & (1 << port))
  650. wol->wolopts = pwol.wolopts;
  651. else
  652. wol->wolopts = 0;
  653. }
  654. static int bcm_sf2_sw_set_wol(struct dsa_switch *ds, int port,
  655. struct ethtool_wolinfo *wol)
  656. {
  657. struct net_device *p = ds->dst[ds->index].master_netdev;
  658. struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
  659. s8 cpu_port = ds->dst[ds->index].cpu_port;
  660. struct ethtool_wolinfo pwol;
  661. p->ethtool_ops->get_wol(p, &pwol);
  662. if (wol->wolopts & ~pwol.supported)
  663. return -EINVAL;
  664. if (wol->wolopts)
  665. priv->wol_ports_mask |= (1 << port);
  666. else
  667. priv->wol_ports_mask &= ~(1 << port);
  668. /* If we have at least one port enabled, make sure the CPU port
  669. * is also enabled. If the CPU port is the last one enabled, we disable
  670. * it since this configuration does not make sense.
  671. */
  672. if (priv->wol_ports_mask && priv->wol_ports_mask != (1 << cpu_port))
  673. priv->wol_ports_mask |= (1 << cpu_port);
  674. else
  675. priv->wol_ports_mask &= ~(1 << cpu_port);
  676. return p->ethtool_ops->set_wol(p, wol);
  677. }
  678. static int bcm_sf2_vlan_op_wait(struct bcm_sf2_priv *priv)
  679. {
  680. unsigned int timeout = 10;
  681. u32 reg;
  682. do {
  683. reg = core_readl(priv, CORE_ARLA_VTBL_RWCTRL);
  684. if (!(reg & ARLA_VTBL_STDN))
  685. return 0;
  686. usleep_range(1000, 2000);
  687. } while (timeout--);
  688. return -ETIMEDOUT;
  689. }
  690. static int bcm_sf2_vlan_op(struct bcm_sf2_priv *priv, u8 op)
  691. {
  692. core_writel(priv, ARLA_VTBL_STDN | op, CORE_ARLA_VTBL_RWCTRL);
  693. return bcm_sf2_vlan_op_wait(priv);
  694. }
  695. static void bcm_sf2_sw_configure_vlan(struct dsa_switch *ds)
  696. {
  697. struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
  698. unsigned int port;
  699. /* Clear all VLANs */
  700. bcm_sf2_vlan_op(priv, ARLA_VTBL_CMD_CLEAR);
  701. for (port = 0; port < priv->hw_params.num_ports; port++) {
  702. if (!((1 << port) & ds->enabled_port_mask))
  703. continue;
  704. core_writel(priv, 1, CORE_DEFAULT_1Q_TAG_P(port));
  705. }
  706. }
  707. static int bcm_sf2_sw_setup(struct dsa_switch *ds)
  708. {
  709. struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
  710. unsigned int port;
  711. /* Enable all valid ports and disable those unused */
  712. for (port = 0; port < priv->hw_params.num_ports; port++) {
  713. /* IMP port receives special treatment */
  714. if ((1 << port) & ds->enabled_port_mask)
  715. bcm_sf2_port_setup(ds, port, NULL);
  716. else if (dsa_is_cpu_port(ds, port))
  717. bcm_sf2_imp_setup(ds, port);
  718. else
  719. bcm_sf2_port_disable(ds, port, NULL);
  720. }
  721. bcm_sf2_sw_configure_vlan(ds);
  722. return 0;
  723. }
  724. /* The SWITCH_CORE register space is managed by b53 but operates on a page +
  725. * register basis so we need to translate that into an address that the
  726. * bus-glue understands.
  727. */
  728. #define SF2_PAGE_REG_MKADDR(page, reg) ((page) << 10 | (reg) << 2)
  729. static int bcm_sf2_core_read8(struct b53_device *dev, u8 page, u8 reg,
  730. u8 *val)
  731. {
  732. struct bcm_sf2_priv *priv = dev->priv;
  733. *val = core_readl(priv, SF2_PAGE_REG_MKADDR(page, reg));
  734. return 0;
  735. }
  736. static int bcm_sf2_core_read16(struct b53_device *dev, u8 page, u8 reg,
  737. u16 *val)
  738. {
  739. struct bcm_sf2_priv *priv = dev->priv;
  740. *val = core_readl(priv, SF2_PAGE_REG_MKADDR(page, reg));
  741. return 0;
  742. }
  743. static int bcm_sf2_core_read32(struct b53_device *dev, u8 page, u8 reg,
  744. u32 *val)
  745. {
  746. struct bcm_sf2_priv *priv = dev->priv;
  747. *val = core_readl(priv, SF2_PAGE_REG_MKADDR(page, reg));
  748. return 0;
  749. }
  750. static int bcm_sf2_core_read64(struct b53_device *dev, u8 page, u8 reg,
  751. u64 *val)
  752. {
  753. struct bcm_sf2_priv *priv = dev->priv;
  754. *val = core_readq(priv, SF2_PAGE_REG_MKADDR(page, reg));
  755. return 0;
  756. }
  757. static int bcm_sf2_core_write8(struct b53_device *dev, u8 page, u8 reg,
  758. u8 value)
  759. {
  760. struct bcm_sf2_priv *priv = dev->priv;
  761. core_writel(priv, value, SF2_PAGE_REG_MKADDR(page, reg));
  762. return 0;
  763. }
  764. static int bcm_sf2_core_write16(struct b53_device *dev, u8 page, u8 reg,
  765. u16 value)
  766. {
  767. struct bcm_sf2_priv *priv = dev->priv;
  768. core_writel(priv, value, SF2_PAGE_REG_MKADDR(page, reg));
  769. return 0;
  770. }
  771. static int bcm_sf2_core_write32(struct b53_device *dev, u8 page, u8 reg,
  772. u32 value)
  773. {
  774. struct bcm_sf2_priv *priv = dev->priv;
  775. core_writel(priv, value, SF2_PAGE_REG_MKADDR(page, reg));
  776. return 0;
  777. }
  778. static int bcm_sf2_core_write64(struct b53_device *dev, u8 page, u8 reg,
  779. u64 value)
  780. {
  781. struct bcm_sf2_priv *priv = dev->priv;
  782. core_writeq(priv, value, SF2_PAGE_REG_MKADDR(page, reg));
  783. return 0;
  784. }
  785. static struct b53_io_ops bcm_sf2_io_ops = {
  786. .read8 = bcm_sf2_core_read8,
  787. .read16 = bcm_sf2_core_read16,
  788. .read32 = bcm_sf2_core_read32,
  789. .read48 = bcm_sf2_core_read64,
  790. .read64 = bcm_sf2_core_read64,
  791. .write8 = bcm_sf2_core_write8,
  792. .write16 = bcm_sf2_core_write16,
  793. .write32 = bcm_sf2_core_write32,
  794. .write48 = bcm_sf2_core_write64,
  795. .write64 = bcm_sf2_core_write64,
  796. };
  797. static int bcm_sf2_sw_probe(struct platform_device *pdev)
  798. {
  799. const char *reg_names[BCM_SF2_REGS_NUM] = BCM_SF2_REGS_NAME;
  800. struct device_node *dn = pdev->dev.of_node;
  801. struct b53_platform_data *pdata;
  802. struct bcm_sf2_priv *priv;
  803. struct b53_device *dev;
  804. struct dsa_switch *ds;
  805. void __iomem **base;
  806. struct resource *r;
  807. unsigned int i;
  808. u32 reg, rev;
  809. int ret;
  810. priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
  811. if (!priv)
  812. return -ENOMEM;
  813. dev = b53_switch_alloc(&pdev->dev, &bcm_sf2_io_ops, priv);
  814. if (!dev)
  815. return -ENOMEM;
  816. pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
  817. if (!pdata)
  818. return -ENOMEM;
  819. /* Auto-detection using standard registers will not work, so
  820. * provide an indication of what kind of device we are for
  821. * b53_common to work with
  822. */
  823. pdata->chip_id = BCM7445_DEVICE_ID;
  824. dev->pdata = pdata;
  825. priv->dev = dev;
  826. ds = dev->ds;
  827. /* Override the parts that are non-standard wrt. normal b53 devices */
  828. ds->ops->get_tag_protocol = bcm_sf2_sw_get_tag_protocol;
  829. ds->ops->setup = bcm_sf2_sw_setup;
  830. ds->ops->get_phy_flags = bcm_sf2_sw_get_phy_flags;
  831. ds->ops->adjust_link = bcm_sf2_sw_adjust_link;
  832. ds->ops->fixed_link_update = bcm_sf2_sw_fixed_link_update;
  833. ds->ops->suspend = bcm_sf2_sw_suspend;
  834. ds->ops->resume = bcm_sf2_sw_resume;
  835. ds->ops->get_wol = bcm_sf2_sw_get_wol;
  836. ds->ops->set_wol = bcm_sf2_sw_set_wol;
  837. ds->ops->port_enable = bcm_sf2_port_setup;
  838. ds->ops->port_disable = bcm_sf2_port_disable;
  839. ds->ops->get_eee = bcm_sf2_sw_get_eee;
  840. ds->ops->set_eee = bcm_sf2_sw_set_eee;
  841. /* Avoid having DSA free our slave MDIO bus (checking for
  842. * ds->slave_mii_bus and ds->ops->phy_read being non-NULL)
  843. */
  844. ds->ops->phy_read = NULL;
  845. dev_set_drvdata(&pdev->dev, priv);
  846. spin_lock_init(&priv->indir_lock);
  847. mutex_init(&priv->stats_mutex);
  848. bcm_sf2_identify_ports(priv, dn->child);
  849. priv->irq0 = irq_of_parse_and_map(dn, 0);
  850. priv->irq1 = irq_of_parse_and_map(dn, 1);
  851. base = &priv->core;
  852. for (i = 0; i < BCM_SF2_REGS_NUM; i++) {
  853. r = platform_get_resource(pdev, IORESOURCE_MEM, i);
  854. *base = devm_ioremap_resource(&pdev->dev, r);
  855. if (IS_ERR(*base)) {
  856. pr_err("unable to find register: %s\n", reg_names[i]);
  857. return PTR_ERR(*base);
  858. }
  859. base++;
  860. }
  861. ret = bcm_sf2_sw_rst(priv);
  862. if (ret) {
  863. pr_err("unable to software reset switch: %d\n", ret);
  864. return ret;
  865. }
  866. ret = bcm_sf2_mdio_register(ds);
  867. if (ret) {
  868. pr_err("failed to register MDIO bus\n");
  869. return ret;
  870. }
  871. /* Disable all interrupts and request them */
  872. bcm_sf2_intr_disable(priv);
  873. ret = devm_request_irq(&pdev->dev, priv->irq0, bcm_sf2_switch_0_isr, 0,
  874. "switch_0", priv);
  875. if (ret < 0) {
  876. pr_err("failed to request switch_0 IRQ\n");
  877. goto out_mdio;
  878. }
  879. ret = devm_request_irq(&pdev->dev, priv->irq1, bcm_sf2_switch_1_isr, 0,
  880. "switch_1", priv);
  881. if (ret < 0) {
  882. pr_err("failed to request switch_1 IRQ\n");
  883. goto out_mdio;
  884. }
  885. /* Reset the MIB counters */
  886. reg = core_readl(priv, CORE_GMNCFGCFG);
  887. reg |= RST_MIB_CNT;
  888. core_writel(priv, reg, CORE_GMNCFGCFG);
  889. reg &= ~RST_MIB_CNT;
  890. core_writel(priv, reg, CORE_GMNCFGCFG);
  891. /* Get the maximum number of ports for this switch */
  892. priv->hw_params.num_ports = core_readl(priv, CORE_IMP0_PRT_ID) + 1;
  893. if (priv->hw_params.num_ports > DSA_MAX_PORTS)
  894. priv->hw_params.num_ports = DSA_MAX_PORTS;
  895. /* Assume a single GPHY setup if we can't read that property */
  896. if (of_property_read_u32(dn, "brcm,num-gphy",
  897. &priv->hw_params.num_gphy))
  898. priv->hw_params.num_gphy = 1;
  899. rev = reg_readl(priv, REG_SWITCH_REVISION);
  900. priv->hw_params.top_rev = (rev >> SWITCH_TOP_REV_SHIFT) &
  901. SWITCH_TOP_REV_MASK;
  902. priv->hw_params.core_rev = (rev & SF2_REV_MASK);
  903. rev = reg_readl(priv, REG_PHY_REVISION);
  904. priv->hw_params.gphy_rev = rev & PHY_REVISION_MASK;
  905. ret = b53_switch_register(dev);
  906. if (ret)
  907. goto out_mdio;
  908. pr_info("Starfighter 2 top: %x.%02x, core: %x.%02x base: 0x%p, IRQs: %d, %d\n",
  909. priv->hw_params.top_rev >> 8, priv->hw_params.top_rev & 0xff,
  910. priv->hw_params.core_rev >> 8, priv->hw_params.core_rev & 0xff,
  911. priv->core, priv->irq0, priv->irq1);
  912. return 0;
  913. out_mdio:
  914. bcm_sf2_mdio_unregister(priv);
  915. return ret;
  916. }
  917. static int bcm_sf2_sw_remove(struct platform_device *pdev)
  918. {
  919. struct bcm_sf2_priv *priv = platform_get_drvdata(pdev);
  920. /* Disable all ports and interrupts */
  921. priv->wol_ports_mask = 0;
  922. bcm_sf2_sw_suspend(priv->dev->ds);
  923. dsa_unregister_switch(priv->dev->ds);
  924. bcm_sf2_mdio_unregister(priv);
  925. return 0;
  926. }
  927. #ifdef CONFIG_PM_SLEEP
  928. static int bcm_sf2_suspend(struct device *dev)
  929. {
  930. struct platform_device *pdev = to_platform_device(dev);
  931. struct bcm_sf2_priv *priv = platform_get_drvdata(pdev);
  932. return dsa_switch_suspend(priv->dev->ds);
  933. }
  934. static int bcm_sf2_resume(struct device *dev)
  935. {
  936. struct platform_device *pdev = to_platform_device(dev);
  937. struct bcm_sf2_priv *priv = platform_get_drvdata(pdev);
  938. return dsa_switch_resume(priv->dev->ds);
  939. }
  940. #endif /* CONFIG_PM_SLEEP */
  941. static SIMPLE_DEV_PM_OPS(bcm_sf2_pm_ops,
  942. bcm_sf2_suspend, bcm_sf2_resume);
  943. static const struct of_device_id bcm_sf2_of_match[] = {
  944. { .compatible = "brcm,bcm7445-switch-v4.0" },
  945. { /* sentinel */ },
  946. };
  947. static struct platform_driver bcm_sf2_driver = {
  948. .probe = bcm_sf2_sw_probe,
  949. .remove = bcm_sf2_sw_remove,
  950. .driver = {
  951. .name = "brcm-sf2",
  952. .of_match_table = bcm_sf2_of_match,
  953. .pm = &bcm_sf2_pm_ops,
  954. },
  955. };
  956. module_platform_driver(bcm_sf2_driver);
  957. MODULE_AUTHOR("Broadcom Corporation");
  958. MODULE_DESCRIPTION("Driver for Broadcom Starfighter 2 ethernet switch chip");
  959. MODULE_LICENSE("GPL");
  960. MODULE_ALIAS("platform:brcm-sf2");