bcm_sf2.c 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Broadcom Starfighter 2 DSA switch driver
  4. *
  5. * Copyright (C) 2014, Broadcom Corporation
  6. */
  7. #include <linux/list.h>
  8. #include <linux/module.h>
  9. #include <linux/netdevice.h>
  10. #include <linux/interrupt.h>
  11. #include <linux/platform_device.h>
  12. #include <linux/phy.h>
  13. #include <linux/phy_fixed.h>
  14. #include <linux/phylink.h>
  15. #include <linux/mii.h>
  16. #include <linux/of.h>
  17. #include <linux/of_irq.h>
  18. #include <linux/of_address.h>
  19. #include <linux/of_net.h>
  20. #include <linux/of_mdio.h>
  21. #include <net/dsa.h>
  22. #include <linux/ethtool.h>
  23. #include <linux/if_bridge.h>
  24. #include <linux/brcmphy.h>
  25. #include <linux/etherdevice.h>
  26. #include <linux/platform_data/b53.h>
  27. #include "bcm_sf2.h"
  28. #include "bcm_sf2_regs.h"
  29. #include "b53/b53_priv.h"
  30. #include "b53/b53_regs.h"
  31. static void bcm_sf2_imp_setup(struct dsa_switch *ds, int port)
  32. {
  33. struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
  34. unsigned int i;
  35. u32 reg, offset;
  36. /* Enable the port memories */
  37. reg = core_readl(priv, CORE_MEM_PSM_VDD_CTRL);
  38. reg &= ~P_TXQ_PSM_VDD(port);
  39. core_writel(priv, reg, CORE_MEM_PSM_VDD_CTRL);
  40. /* Enable forwarding */
  41. core_writel(priv, SW_FWDG_EN, CORE_SWMODE);
  42. /* Enable IMP port in dumb mode */
  43. reg = core_readl(priv, CORE_SWITCH_CTRL);
  44. reg |= MII_DUMB_FWDG_EN;
  45. core_writel(priv, reg, CORE_SWITCH_CTRL);
  46. /* Configure Traffic Class to QoS mapping, allow each priority to map
  47. * to a different queue number
  48. */
  49. reg = core_readl(priv, CORE_PORT_TC2_QOS_MAP_PORT(port));
  50. for (i = 0; i < SF2_NUM_EGRESS_QUEUES; i++)
  51. reg |= i << (PRT_TO_QID_SHIFT * i);
  52. core_writel(priv, reg, CORE_PORT_TC2_QOS_MAP_PORT(port));
  53. b53_brcm_hdr_setup(ds, port);
  54. if (port == 8) {
  55. if (priv->type == BCM7445_DEVICE_ID)
  56. offset = CORE_STS_OVERRIDE_IMP;
  57. else
  58. offset = CORE_STS_OVERRIDE_IMP2;
  59. /* Force link status for IMP port */
  60. reg = core_readl(priv, offset);
  61. reg |= (MII_SW_OR | LINK_STS);
  62. reg &= ~GMII_SPEED_UP_2G;
  63. core_writel(priv, reg, offset);
  64. /* Enable Broadcast, Multicast, Unicast forwarding to IMP port */
  65. reg = core_readl(priv, CORE_IMP_CTL);
  66. reg |= (RX_BCST_EN | RX_MCST_EN | RX_UCST_EN);
  67. reg &= ~(RX_DIS | TX_DIS);
  68. core_writel(priv, reg, CORE_IMP_CTL);
  69. } else {
  70. reg = core_readl(priv, CORE_G_PCTL_PORT(port));
  71. reg &= ~(RX_DIS | TX_DIS);
  72. core_writel(priv, reg, CORE_G_PCTL_PORT(port));
  73. }
  74. }
  75. static void bcm_sf2_gphy_enable_set(struct dsa_switch *ds, bool enable)
  76. {
  77. struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
  78. u32 reg;
  79. reg = reg_readl(priv, REG_SPHY_CNTRL);
  80. if (enable) {
  81. reg |= PHY_RESET;
  82. reg &= ~(EXT_PWR_DOWN | IDDQ_BIAS | IDDQ_GLOBAL_PWR | CK25_DIS);
  83. reg_writel(priv, reg, REG_SPHY_CNTRL);
  84. udelay(21);
  85. reg = reg_readl(priv, REG_SPHY_CNTRL);
  86. reg &= ~PHY_RESET;
  87. } else {
  88. reg |= EXT_PWR_DOWN | IDDQ_BIAS | PHY_RESET;
  89. reg_writel(priv, reg, REG_SPHY_CNTRL);
  90. mdelay(1);
  91. reg |= CK25_DIS;
  92. }
  93. reg_writel(priv, reg, REG_SPHY_CNTRL);
  94. /* Use PHY-driven LED signaling */
  95. if (!enable) {
  96. reg = reg_readl(priv, REG_LED_CNTRL(0));
  97. reg |= SPDLNK_SRC_SEL;
  98. reg_writel(priv, reg, REG_LED_CNTRL(0));
  99. }
  100. }
  101. static inline void bcm_sf2_port_intr_enable(struct bcm_sf2_priv *priv,
  102. int port)
  103. {
  104. unsigned int off;
  105. switch (port) {
  106. case 7:
  107. off = P7_IRQ_OFF;
  108. break;
  109. case 0:
  110. /* Port 0 interrupts are located on the first bank */
  111. intrl2_0_mask_clear(priv, P_IRQ_MASK(P0_IRQ_OFF));
  112. return;
  113. default:
  114. off = P_IRQ_OFF(port);
  115. break;
  116. }
  117. intrl2_1_mask_clear(priv, P_IRQ_MASK(off));
  118. }
  119. static inline void bcm_sf2_port_intr_disable(struct bcm_sf2_priv *priv,
  120. int port)
  121. {
  122. unsigned int off;
  123. switch (port) {
  124. case 7:
  125. off = P7_IRQ_OFF;
  126. break;
  127. case 0:
  128. /* Port 0 interrupts are located on the first bank */
  129. intrl2_0_mask_set(priv, P_IRQ_MASK(P0_IRQ_OFF));
  130. intrl2_0_writel(priv, P_IRQ_MASK(P0_IRQ_OFF), INTRL2_CPU_CLEAR);
  131. return;
  132. default:
  133. off = P_IRQ_OFF(port);
  134. break;
  135. }
  136. intrl2_1_mask_set(priv, P_IRQ_MASK(off));
  137. intrl2_1_writel(priv, P_IRQ_MASK(off), INTRL2_CPU_CLEAR);
  138. }
  139. static int bcm_sf2_port_setup(struct dsa_switch *ds, int port,
  140. struct phy_device *phy)
  141. {
  142. struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
  143. unsigned int i;
  144. u32 reg;
  145. if (!dsa_is_user_port(ds, port))
  146. return 0;
  147. /* Clear the memory power down */
  148. reg = core_readl(priv, CORE_MEM_PSM_VDD_CTRL);
  149. reg &= ~P_TXQ_PSM_VDD(port);
  150. core_writel(priv, reg, CORE_MEM_PSM_VDD_CTRL);
  151. /* Enable Broadcom tags for that port if requested */
  152. if (priv->brcm_tag_mask & BIT(port))
  153. b53_brcm_hdr_setup(ds, port);
  154. /* Configure Traffic Class to QoS mapping, allow each priority to map
  155. * to a different queue number
  156. */
  157. reg = core_readl(priv, CORE_PORT_TC2_QOS_MAP_PORT(port));
  158. for (i = 0; i < SF2_NUM_EGRESS_QUEUES; i++)
  159. reg |= i << (PRT_TO_QID_SHIFT * i);
  160. core_writel(priv, reg, CORE_PORT_TC2_QOS_MAP_PORT(port));
  161. /* Re-enable the GPHY and re-apply workarounds */
  162. if (priv->int_phy_mask & 1 << port && priv->hw_params.num_gphy == 1) {
  163. bcm_sf2_gphy_enable_set(ds, true);
  164. if (phy) {
  165. /* if phy_stop() has been called before, phy
  166. * will be in halted state, and phy_start()
  167. * will call resume.
  168. *
  169. * the resume path does not configure back
  170. * autoneg settings, and since we hard reset
  171. * the phy manually here, we need to reset the
  172. * state machine also.
  173. */
  174. phy->state = PHY_READY;
  175. phy_init_hw(phy);
  176. }
  177. }
  178. /* Enable MoCA port interrupts to get notified */
  179. if (port == priv->moca_port)
  180. bcm_sf2_port_intr_enable(priv, port);
  181. /* Set per-queue pause threshold to 32 */
  182. core_writel(priv, 32, CORE_TXQ_THD_PAUSE_QN_PORT(port));
  183. /* Set ACB threshold to 24 */
  184. for (i = 0; i < SF2_NUM_EGRESS_QUEUES; i++) {
  185. reg = acb_readl(priv, ACB_QUEUE_CFG(port *
  186. SF2_NUM_EGRESS_QUEUES + i));
  187. reg &= ~XOFF_THRESHOLD_MASK;
  188. reg |= 24;
  189. acb_writel(priv, reg, ACB_QUEUE_CFG(port *
  190. SF2_NUM_EGRESS_QUEUES + i));
  191. }
  192. return b53_enable_port(ds, port, phy);
  193. }
  194. static void bcm_sf2_port_disable(struct dsa_switch *ds, int port)
  195. {
  196. struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
  197. u32 reg;
  198. /* Disable learning while in WoL mode */
  199. if (priv->wol_ports_mask & (1 << port)) {
  200. reg = core_readl(priv, CORE_DIS_LEARN);
  201. reg |= BIT(port);
  202. core_writel(priv, reg, CORE_DIS_LEARN);
  203. return;
  204. }
  205. if (port == priv->moca_port)
  206. bcm_sf2_port_intr_disable(priv, port);
  207. if (priv->int_phy_mask & 1 << port && priv->hw_params.num_gphy == 1)
  208. bcm_sf2_gphy_enable_set(ds, false);
  209. b53_disable_port(ds, port);
  210. /* Power down the port memory */
  211. reg = core_readl(priv, CORE_MEM_PSM_VDD_CTRL);
  212. reg |= P_TXQ_PSM_VDD(port);
  213. core_writel(priv, reg, CORE_MEM_PSM_VDD_CTRL);
  214. }
  215. static int bcm_sf2_sw_indir_rw(struct bcm_sf2_priv *priv, int op, int addr,
  216. int regnum, u16 val)
  217. {
  218. int ret = 0;
  219. u32 reg;
  220. reg = reg_readl(priv, REG_SWITCH_CNTRL);
  221. reg |= MDIO_MASTER_SEL;
  222. reg_writel(priv, reg, REG_SWITCH_CNTRL);
  223. /* Page << 8 | offset */
  224. reg = 0x70;
  225. reg <<= 2;
  226. core_writel(priv, addr, reg);
  227. /* Page << 8 | offset */
  228. reg = 0x80 << 8 | regnum << 1;
  229. reg <<= 2;
  230. if (op)
  231. ret = core_readl(priv, reg);
  232. else
  233. core_writel(priv, val, reg);
  234. reg = reg_readl(priv, REG_SWITCH_CNTRL);
  235. reg &= ~MDIO_MASTER_SEL;
  236. reg_writel(priv, reg, REG_SWITCH_CNTRL);
  237. return ret & 0xffff;
  238. }
  239. static int bcm_sf2_sw_mdio_read(struct mii_bus *bus, int addr, int regnum)
  240. {
  241. struct bcm_sf2_priv *priv = bus->priv;
  242. /* Intercept reads from Broadcom pseudo-PHY address, else, send
  243. * them to our master MDIO bus controller
  244. */
  245. if (addr == BRCM_PSEUDO_PHY_ADDR && priv->indir_phy_mask & BIT(addr))
  246. return bcm_sf2_sw_indir_rw(priv, 1, addr, regnum, 0);
  247. else
  248. return mdiobus_read_nested(priv->master_mii_bus, addr, regnum);
  249. }
  250. static int bcm_sf2_sw_mdio_write(struct mii_bus *bus, int addr, int regnum,
  251. u16 val)
  252. {
  253. struct bcm_sf2_priv *priv = bus->priv;
  254. /* Intercept writes to the Broadcom pseudo-PHY address, else,
  255. * send them to our master MDIO bus controller
  256. */
  257. if (addr == BRCM_PSEUDO_PHY_ADDR && priv->indir_phy_mask & BIT(addr))
  258. return bcm_sf2_sw_indir_rw(priv, 0, addr, regnum, val);
  259. else
  260. return mdiobus_write_nested(priv->master_mii_bus, addr,
  261. regnum, val);
  262. }
  263. static irqreturn_t bcm_sf2_switch_0_isr(int irq, void *dev_id)
  264. {
  265. struct dsa_switch *ds = dev_id;
  266. struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
  267. priv->irq0_stat = intrl2_0_readl(priv, INTRL2_CPU_STATUS) &
  268. ~priv->irq0_mask;
  269. intrl2_0_writel(priv, priv->irq0_stat, INTRL2_CPU_CLEAR);
  270. return IRQ_HANDLED;
  271. }
  272. static irqreturn_t bcm_sf2_switch_1_isr(int irq, void *dev_id)
  273. {
  274. struct dsa_switch *ds = dev_id;
  275. struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
  276. priv->irq1_stat = intrl2_1_readl(priv, INTRL2_CPU_STATUS) &
  277. ~priv->irq1_mask;
  278. intrl2_1_writel(priv, priv->irq1_stat, INTRL2_CPU_CLEAR);
  279. if (priv->irq1_stat & P_LINK_UP_IRQ(P7_IRQ_OFF)) {
  280. priv->port_sts[7].link = true;
  281. dsa_port_phylink_mac_change(ds, 7, true);
  282. }
  283. if (priv->irq1_stat & P_LINK_DOWN_IRQ(P7_IRQ_OFF)) {
  284. priv->port_sts[7].link = false;
  285. dsa_port_phylink_mac_change(ds, 7, false);
  286. }
  287. return IRQ_HANDLED;
  288. }
  289. static int bcm_sf2_sw_rst(struct bcm_sf2_priv *priv)
  290. {
  291. unsigned int timeout = 1000;
  292. u32 reg;
  293. reg = core_readl(priv, CORE_WATCHDOG_CTRL);
  294. reg |= SOFTWARE_RESET | EN_CHIP_RST | EN_SW_RESET;
  295. core_writel(priv, reg, CORE_WATCHDOG_CTRL);
  296. do {
  297. reg = core_readl(priv, CORE_WATCHDOG_CTRL);
  298. if (!(reg & SOFTWARE_RESET))
  299. break;
  300. usleep_range(1000, 2000);
  301. } while (timeout-- > 0);
  302. if (timeout == 0)
  303. return -ETIMEDOUT;
  304. return 0;
  305. }
  306. static void bcm_sf2_intr_disable(struct bcm_sf2_priv *priv)
  307. {
  308. intrl2_0_mask_set(priv, 0xffffffff);
  309. intrl2_0_writel(priv, 0xffffffff, INTRL2_CPU_CLEAR);
  310. intrl2_1_mask_set(priv, 0xffffffff);
  311. intrl2_1_writel(priv, 0xffffffff, INTRL2_CPU_CLEAR);
  312. }
  313. static void bcm_sf2_identify_ports(struct bcm_sf2_priv *priv,
  314. struct device_node *dn)
  315. {
  316. struct device_node *port;
  317. int mode;
  318. unsigned int port_num;
  319. priv->moca_port = -1;
  320. for_each_available_child_of_node(dn, port) {
  321. if (of_property_read_u32(port, "reg", &port_num))
  322. continue;
  323. /* Internal PHYs get assigned a specific 'phy-mode' property
  324. * value: "internal" to help flag them before MDIO probing
  325. * has completed, since they might be turned off at that
  326. * time
  327. */
  328. mode = of_get_phy_mode(port);
  329. if (mode < 0)
  330. continue;
  331. if (mode == PHY_INTERFACE_MODE_INTERNAL)
  332. priv->int_phy_mask |= 1 << port_num;
  333. if (mode == PHY_INTERFACE_MODE_MOCA)
  334. priv->moca_port = port_num;
  335. if (of_property_read_bool(port, "brcm,use-bcm-hdr"))
  336. priv->brcm_tag_mask |= 1 << port_num;
  337. }
  338. }
  339. static int bcm_sf2_mdio_register(struct dsa_switch *ds)
  340. {
  341. struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
  342. struct device_node *dn;
  343. static int index;
  344. int err;
  345. /* Find our integrated MDIO bus node */
  346. dn = of_find_compatible_node(NULL, NULL, "brcm,unimac-mdio");
  347. priv->master_mii_bus = of_mdio_find_bus(dn);
  348. if (!priv->master_mii_bus) {
  349. of_node_put(dn);
  350. return -EPROBE_DEFER;
  351. }
  352. get_device(&priv->master_mii_bus->dev);
  353. priv->master_mii_dn = dn;
  354. priv->slave_mii_bus = devm_mdiobus_alloc(ds->dev);
  355. if (!priv->slave_mii_bus) {
  356. of_node_put(dn);
  357. return -ENOMEM;
  358. }
  359. priv->slave_mii_bus->priv = priv;
  360. priv->slave_mii_bus->name = "sf2 slave mii";
  361. priv->slave_mii_bus->read = bcm_sf2_sw_mdio_read;
  362. priv->slave_mii_bus->write = bcm_sf2_sw_mdio_write;
  363. snprintf(priv->slave_mii_bus->id, MII_BUS_ID_SIZE, "sf2-%d",
  364. index++);
  365. priv->slave_mii_bus->dev.of_node = dn;
  366. /* Include the pseudo-PHY address to divert reads towards our
  367. * workaround. This is only required for 7445D0, since 7445E0
  368. * disconnects the internal switch pseudo-PHY such that we can use the
  369. * regular SWITCH_MDIO master controller instead.
  370. *
  371. * Here we flag the pseudo PHY as needing special treatment and would
  372. * otherwise make all other PHY read/writes go to the master MDIO bus
  373. * controller that comes with this switch backed by the "mdio-unimac"
  374. * driver.
  375. */
  376. if (of_machine_is_compatible("brcm,bcm7445d0"))
  377. priv->indir_phy_mask |= (1 << BRCM_PSEUDO_PHY_ADDR);
  378. else
  379. priv->indir_phy_mask = 0;
  380. ds->phys_mii_mask = priv->indir_phy_mask;
  381. ds->slave_mii_bus = priv->slave_mii_bus;
  382. priv->slave_mii_bus->parent = ds->dev->parent;
  383. priv->slave_mii_bus->phy_mask = ~priv->indir_phy_mask;
  384. err = mdiobus_register(priv->slave_mii_bus);
  385. if (err && dn)
  386. of_node_put(dn);
  387. return err;
  388. }
  389. static void bcm_sf2_mdio_unregister(struct bcm_sf2_priv *priv)
  390. {
  391. mdiobus_unregister(priv->slave_mii_bus);
  392. of_node_put(priv->master_mii_dn);
  393. }
  394. static u32 bcm_sf2_sw_get_phy_flags(struct dsa_switch *ds, int port)
  395. {
  396. struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
  397. /* The BCM7xxx PHY driver expects to find the integrated PHY revision
  398. * in bits 15:8 and the patch level in bits 7:0 which is exactly what
  399. * the REG_PHY_REVISION register layout is.
  400. */
  401. if (priv->int_phy_mask & BIT(port))
  402. return priv->hw_params.gphy_rev;
  403. else
  404. return 0;
  405. }
  406. static void bcm_sf2_sw_validate(struct dsa_switch *ds, int port,
  407. unsigned long *supported,
  408. struct phylink_link_state *state)
  409. {
  410. struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
  411. __ETHTOOL_DECLARE_LINK_MODE_MASK(mask) = { 0, };
  412. if (!phy_interface_mode_is_rgmii(state->interface) &&
  413. state->interface != PHY_INTERFACE_MODE_MII &&
  414. state->interface != PHY_INTERFACE_MODE_REVMII &&
  415. state->interface != PHY_INTERFACE_MODE_GMII &&
  416. state->interface != PHY_INTERFACE_MODE_INTERNAL &&
  417. state->interface != PHY_INTERFACE_MODE_MOCA) {
  418. bitmap_zero(supported, __ETHTOOL_LINK_MODE_MASK_NBITS);
  419. if (port != core_readl(priv, CORE_IMP0_PRT_ID))
  420. dev_err(ds->dev,
  421. "Unsupported interface: %d for port %d\n",
  422. state->interface, port);
  423. return;
  424. }
  425. /* Allow all the expected bits */
  426. phylink_set(mask, Autoneg);
  427. phylink_set_port_modes(mask);
  428. phylink_set(mask, Pause);
  429. phylink_set(mask, Asym_Pause);
  430. /* With the exclusion of MII and Reverse MII, we support Gigabit,
  431. * including Half duplex
  432. */
  433. if (state->interface != PHY_INTERFACE_MODE_MII &&
  434. state->interface != PHY_INTERFACE_MODE_REVMII) {
  435. phylink_set(mask, 1000baseT_Full);
  436. phylink_set(mask, 1000baseT_Half);
  437. }
  438. phylink_set(mask, 10baseT_Half);
  439. phylink_set(mask, 10baseT_Full);
  440. phylink_set(mask, 100baseT_Half);
  441. phylink_set(mask, 100baseT_Full);
  442. bitmap_and(supported, supported, mask,
  443. __ETHTOOL_LINK_MODE_MASK_NBITS);
  444. bitmap_and(state->advertising, state->advertising, mask,
  445. __ETHTOOL_LINK_MODE_MASK_NBITS);
  446. }
  447. static void bcm_sf2_sw_mac_config(struct dsa_switch *ds, int port,
  448. unsigned int mode,
  449. const struct phylink_link_state *state)
  450. {
  451. struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
  452. u32 id_mode_dis = 0, port_mode;
  453. u32 reg, offset;
  454. if (port == core_readl(priv, CORE_IMP0_PRT_ID))
  455. return;
  456. if (priv->type == BCM7445_DEVICE_ID)
  457. offset = CORE_STS_OVERRIDE_GMIIP_PORT(port);
  458. else
  459. offset = CORE_STS_OVERRIDE_GMIIP2_PORT(port);
  460. switch (state->interface) {
  461. case PHY_INTERFACE_MODE_RGMII:
  462. id_mode_dis = 1;
  463. /* fallthrough */
  464. case PHY_INTERFACE_MODE_RGMII_TXID:
  465. port_mode = EXT_GPHY;
  466. break;
  467. case PHY_INTERFACE_MODE_MII:
  468. port_mode = EXT_EPHY;
  469. break;
  470. case PHY_INTERFACE_MODE_REVMII:
  471. port_mode = EXT_REVMII;
  472. break;
  473. default:
  474. /* all other PHYs: internal and MoCA */
  475. goto force_link;
  476. }
  477. /* Clear id_mode_dis bit, and the existing port mode, let
  478. * RGMII_MODE_EN bet set by mac_link_{up,down}
  479. */
  480. reg = reg_readl(priv, REG_RGMII_CNTRL_P(port));
  481. reg &= ~ID_MODE_DIS;
  482. reg &= ~(PORT_MODE_MASK << PORT_MODE_SHIFT);
  483. reg &= ~(RX_PAUSE_EN | TX_PAUSE_EN);
  484. reg |= port_mode;
  485. if (id_mode_dis)
  486. reg |= ID_MODE_DIS;
  487. if (state->pause & MLO_PAUSE_TXRX_MASK) {
  488. if (state->pause & MLO_PAUSE_TX)
  489. reg |= TX_PAUSE_EN;
  490. reg |= RX_PAUSE_EN;
  491. }
  492. reg_writel(priv, reg, REG_RGMII_CNTRL_P(port));
  493. force_link:
  494. /* Force link settings detected from the PHY */
  495. reg = SW_OVERRIDE;
  496. switch (state->speed) {
  497. case SPEED_1000:
  498. reg |= SPDSTS_1000 << SPEED_SHIFT;
  499. break;
  500. case SPEED_100:
  501. reg |= SPDSTS_100 << SPEED_SHIFT;
  502. break;
  503. }
  504. if (state->link)
  505. reg |= LINK_STS;
  506. if (state->duplex == DUPLEX_FULL)
  507. reg |= DUPLX_MODE;
  508. core_writel(priv, reg, offset);
  509. }
  510. static void bcm_sf2_sw_mac_link_set(struct dsa_switch *ds, int port,
  511. phy_interface_t interface, bool link)
  512. {
  513. struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
  514. u32 reg;
  515. if (!phy_interface_mode_is_rgmii(interface) &&
  516. interface != PHY_INTERFACE_MODE_MII &&
  517. interface != PHY_INTERFACE_MODE_REVMII)
  518. return;
  519. /* If the link is down, just disable the interface to conserve power */
  520. reg = reg_readl(priv, REG_RGMII_CNTRL_P(port));
  521. if (link)
  522. reg |= RGMII_MODE_EN;
  523. else
  524. reg &= ~RGMII_MODE_EN;
  525. reg_writel(priv, reg, REG_RGMII_CNTRL_P(port));
  526. }
  527. static void bcm_sf2_sw_mac_link_down(struct dsa_switch *ds, int port,
  528. unsigned int mode,
  529. phy_interface_t interface)
  530. {
  531. bcm_sf2_sw_mac_link_set(ds, port, interface, false);
  532. }
  533. static void bcm_sf2_sw_mac_link_up(struct dsa_switch *ds, int port,
  534. unsigned int mode,
  535. phy_interface_t interface,
  536. struct phy_device *phydev)
  537. {
  538. struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
  539. struct ethtool_eee *p = &priv->dev->ports[port].eee;
  540. bcm_sf2_sw_mac_link_set(ds, port, interface, true);
  541. if (mode == MLO_AN_PHY && phydev)
  542. p->eee_enabled = b53_eee_init(ds, port, phydev);
  543. }
  544. static void bcm_sf2_sw_fixed_state(struct dsa_switch *ds, int port,
  545. struct phylink_link_state *status)
  546. {
  547. struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
  548. status->link = false;
  549. /* MoCA port is special as we do not get link status from CORE_LNKSTS,
  550. * which means that we need to force the link at the port override
  551. * level to get the data to flow. We do use what the interrupt handler
  552. * did determine before.
  553. *
  554. * For the other ports, we just force the link status, since this is
  555. * a fixed PHY device.
  556. */
  557. if (port == priv->moca_port) {
  558. status->link = priv->port_sts[port].link;
  559. /* For MoCA interfaces, also force a link down notification
  560. * since some version of the user-space daemon (mocad) use
  561. * cmd->autoneg to force the link, which messes up the PHY
  562. * state machine and make it go in PHY_FORCING state instead.
  563. */
  564. if (!status->link)
  565. netif_carrier_off(ds->ports[port].slave);
  566. status->duplex = DUPLEX_FULL;
  567. } else {
  568. status->link = true;
  569. }
  570. }
  571. static void bcm_sf2_enable_acb(struct dsa_switch *ds)
  572. {
  573. struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
  574. u32 reg;
  575. /* Enable ACB globally */
  576. reg = acb_readl(priv, ACB_CONTROL);
  577. reg |= (ACB_FLUSH_MASK << ACB_FLUSH_SHIFT);
  578. acb_writel(priv, reg, ACB_CONTROL);
  579. reg &= ~(ACB_FLUSH_MASK << ACB_FLUSH_SHIFT);
  580. reg |= ACB_EN | ACB_ALGORITHM;
  581. acb_writel(priv, reg, ACB_CONTROL);
  582. }
  583. static int bcm_sf2_sw_suspend(struct dsa_switch *ds)
  584. {
  585. struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
  586. unsigned int port;
  587. bcm_sf2_intr_disable(priv);
  588. /* Disable all ports physically present including the IMP
  589. * port, the other ones have already been disabled during
  590. * bcm_sf2_sw_setup
  591. */
  592. for (port = 0; port < ds->num_ports; port++) {
  593. if (dsa_is_user_port(ds, port) || dsa_is_cpu_port(ds, port))
  594. bcm_sf2_port_disable(ds, port);
  595. }
  596. return 0;
  597. }
  598. static int bcm_sf2_sw_resume(struct dsa_switch *ds)
  599. {
  600. struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
  601. int ret;
  602. ret = bcm_sf2_sw_rst(priv);
  603. if (ret) {
  604. pr_err("%s: failed to software reset switch\n", __func__);
  605. return ret;
  606. }
  607. ret = bcm_sf2_cfp_resume(ds);
  608. if (ret)
  609. return ret;
  610. if (priv->hw_params.num_gphy == 1)
  611. bcm_sf2_gphy_enable_set(ds, true);
  612. ds->ops->setup(ds);
  613. return 0;
  614. }
  615. static void bcm_sf2_sw_get_wol(struct dsa_switch *ds, int port,
  616. struct ethtool_wolinfo *wol)
  617. {
  618. struct net_device *p = ds->ports[port].cpu_dp->master;
  619. struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
  620. struct ethtool_wolinfo pwol = { };
  621. /* Get the parent device WoL settings */
  622. if (p->ethtool_ops->get_wol)
  623. p->ethtool_ops->get_wol(p, &pwol);
  624. /* Advertise the parent device supported settings */
  625. wol->supported = pwol.supported;
  626. memset(&wol->sopass, 0, sizeof(wol->sopass));
  627. if (pwol.wolopts & WAKE_MAGICSECURE)
  628. memcpy(&wol->sopass, pwol.sopass, sizeof(wol->sopass));
  629. if (priv->wol_ports_mask & (1 << port))
  630. wol->wolopts = pwol.wolopts;
  631. else
  632. wol->wolopts = 0;
  633. }
  634. static int bcm_sf2_sw_set_wol(struct dsa_switch *ds, int port,
  635. struct ethtool_wolinfo *wol)
  636. {
  637. struct net_device *p = ds->ports[port].cpu_dp->master;
  638. struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
  639. s8 cpu_port = ds->ports[port].cpu_dp->index;
  640. struct ethtool_wolinfo pwol = { };
  641. if (p->ethtool_ops->get_wol)
  642. p->ethtool_ops->get_wol(p, &pwol);
  643. if (wol->wolopts & ~pwol.supported)
  644. return -EINVAL;
  645. if (wol->wolopts)
  646. priv->wol_ports_mask |= (1 << port);
  647. else
  648. priv->wol_ports_mask &= ~(1 << port);
  649. /* If we have at least one port enabled, make sure the CPU port
  650. * is also enabled. If the CPU port is the last one enabled, we disable
  651. * it since this configuration does not make sense.
  652. */
  653. if (priv->wol_ports_mask && priv->wol_ports_mask != (1 << cpu_port))
  654. priv->wol_ports_mask |= (1 << cpu_port);
  655. else
  656. priv->wol_ports_mask &= ~(1 << cpu_port);
  657. return p->ethtool_ops->set_wol(p, wol);
  658. }
  659. static int bcm_sf2_sw_setup(struct dsa_switch *ds)
  660. {
  661. struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
  662. unsigned int port;
  663. /* Enable all valid ports and disable those unused */
  664. for (port = 0; port < priv->hw_params.num_ports; port++) {
  665. /* IMP port receives special treatment */
  666. if (dsa_is_user_port(ds, port))
  667. bcm_sf2_port_setup(ds, port, NULL);
  668. else if (dsa_is_cpu_port(ds, port))
  669. bcm_sf2_imp_setup(ds, port);
  670. else
  671. bcm_sf2_port_disable(ds, port);
  672. }
  673. b53_configure_vlan(ds);
  674. bcm_sf2_enable_acb(ds);
  675. return 0;
  676. }
  677. /* The SWITCH_CORE register space is managed by b53 but operates on a page +
  678. * register basis so we need to translate that into an address that the
  679. * bus-glue understands.
  680. */
  681. #define SF2_PAGE_REG_MKADDR(page, reg) ((page) << 10 | (reg) << 2)
  682. static int bcm_sf2_core_read8(struct b53_device *dev, u8 page, u8 reg,
  683. u8 *val)
  684. {
  685. struct bcm_sf2_priv *priv = dev->priv;
  686. *val = core_readl(priv, SF2_PAGE_REG_MKADDR(page, reg));
  687. return 0;
  688. }
  689. static int bcm_sf2_core_read16(struct b53_device *dev, u8 page, u8 reg,
  690. u16 *val)
  691. {
  692. struct bcm_sf2_priv *priv = dev->priv;
  693. *val = core_readl(priv, SF2_PAGE_REG_MKADDR(page, reg));
  694. return 0;
  695. }
  696. static int bcm_sf2_core_read32(struct b53_device *dev, u8 page, u8 reg,
  697. u32 *val)
  698. {
  699. struct bcm_sf2_priv *priv = dev->priv;
  700. *val = core_readl(priv, SF2_PAGE_REG_MKADDR(page, reg));
  701. return 0;
  702. }
  703. static int bcm_sf2_core_read64(struct b53_device *dev, u8 page, u8 reg,
  704. u64 *val)
  705. {
  706. struct bcm_sf2_priv *priv = dev->priv;
  707. *val = core_readq(priv, SF2_PAGE_REG_MKADDR(page, reg));
  708. return 0;
  709. }
  710. static int bcm_sf2_core_write8(struct b53_device *dev, u8 page, u8 reg,
  711. u8 value)
  712. {
  713. struct bcm_sf2_priv *priv = dev->priv;
  714. core_writel(priv, value, SF2_PAGE_REG_MKADDR(page, reg));
  715. return 0;
  716. }
  717. static int bcm_sf2_core_write16(struct b53_device *dev, u8 page, u8 reg,
  718. u16 value)
  719. {
  720. struct bcm_sf2_priv *priv = dev->priv;
  721. core_writel(priv, value, SF2_PAGE_REG_MKADDR(page, reg));
  722. return 0;
  723. }
  724. static int bcm_sf2_core_write32(struct b53_device *dev, u8 page, u8 reg,
  725. u32 value)
  726. {
  727. struct bcm_sf2_priv *priv = dev->priv;
  728. core_writel(priv, value, SF2_PAGE_REG_MKADDR(page, reg));
  729. return 0;
  730. }
  731. static int bcm_sf2_core_write64(struct b53_device *dev, u8 page, u8 reg,
  732. u64 value)
  733. {
  734. struct bcm_sf2_priv *priv = dev->priv;
  735. core_writeq(priv, value, SF2_PAGE_REG_MKADDR(page, reg));
  736. return 0;
  737. }
  738. static const struct b53_io_ops bcm_sf2_io_ops = {
  739. .read8 = bcm_sf2_core_read8,
  740. .read16 = bcm_sf2_core_read16,
  741. .read32 = bcm_sf2_core_read32,
  742. .read48 = bcm_sf2_core_read64,
  743. .read64 = bcm_sf2_core_read64,
  744. .write8 = bcm_sf2_core_write8,
  745. .write16 = bcm_sf2_core_write16,
  746. .write32 = bcm_sf2_core_write32,
  747. .write48 = bcm_sf2_core_write64,
  748. .write64 = bcm_sf2_core_write64,
  749. };
  750. static void bcm_sf2_sw_get_strings(struct dsa_switch *ds, int port,
  751. u32 stringset, uint8_t *data)
  752. {
  753. int cnt = b53_get_sset_count(ds, port, stringset);
  754. b53_get_strings(ds, port, stringset, data);
  755. bcm_sf2_cfp_get_strings(ds, port, stringset,
  756. data + cnt * ETH_GSTRING_LEN);
  757. }
  758. static void bcm_sf2_sw_get_ethtool_stats(struct dsa_switch *ds, int port,
  759. uint64_t *data)
  760. {
  761. int cnt = b53_get_sset_count(ds, port, ETH_SS_STATS);
  762. b53_get_ethtool_stats(ds, port, data);
  763. bcm_sf2_cfp_get_ethtool_stats(ds, port, data + cnt);
  764. }
  765. static int bcm_sf2_sw_get_sset_count(struct dsa_switch *ds, int port,
  766. int sset)
  767. {
  768. int cnt = b53_get_sset_count(ds, port, sset);
  769. if (cnt < 0)
  770. return cnt;
  771. cnt += bcm_sf2_cfp_get_sset_count(ds, port, sset);
  772. return cnt;
  773. }
  774. static const struct dsa_switch_ops bcm_sf2_ops = {
  775. .get_tag_protocol = b53_get_tag_protocol,
  776. .setup = bcm_sf2_sw_setup,
  777. .get_strings = bcm_sf2_sw_get_strings,
  778. .get_ethtool_stats = bcm_sf2_sw_get_ethtool_stats,
  779. .get_sset_count = bcm_sf2_sw_get_sset_count,
  780. .get_ethtool_phy_stats = b53_get_ethtool_phy_stats,
  781. .get_phy_flags = bcm_sf2_sw_get_phy_flags,
  782. .phylink_validate = bcm_sf2_sw_validate,
  783. .phylink_mac_config = bcm_sf2_sw_mac_config,
  784. .phylink_mac_link_down = bcm_sf2_sw_mac_link_down,
  785. .phylink_mac_link_up = bcm_sf2_sw_mac_link_up,
  786. .phylink_fixed_state = bcm_sf2_sw_fixed_state,
  787. .suspend = bcm_sf2_sw_suspend,
  788. .resume = bcm_sf2_sw_resume,
  789. .get_wol = bcm_sf2_sw_get_wol,
  790. .set_wol = bcm_sf2_sw_set_wol,
  791. .port_enable = bcm_sf2_port_setup,
  792. .port_disable = bcm_sf2_port_disable,
  793. .get_mac_eee = b53_get_mac_eee,
  794. .set_mac_eee = b53_set_mac_eee,
  795. .port_bridge_join = b53_br_join,
  796. .port_bridge_leave = b53_br_leave,
  797. .port_stp_state_set = b53_br_set_stp_state,
  798. .port_fast_age = b53_br_fast_age,
  799. .port_vlan_filtering = b53_vlan_filtering,
  800. .port_vlan_prepare = b53_vlan_prepare,
  801. .port_vlan_add = b53_vlan_add,
  802. .port_vlan_del = b53_vlan_del,
  803. .port_fdb_dump = b53_fdb_dump,
  804. .port_fdb_add = b53_fdb_add,
  805. .port_fdb_del = b53_fdb_del,
  806. .get_rxnfc = bcm_sf2_get_rxnfc,
  807. .set_rxnfc = bcm_sf2_set_rxnfc,
  808. .port_mirror_add = b53_mirror_add,
  809. .port_mirror_del = b53_mirror_del,
  810. };
  811. struct bcm_sf2_of_data {
  812. u32 type;
  813. const u16 *reg_offsets;
  814. unsigned int core_reg_align;
  815. unsigned int num_cfp_rules;
  816. };
  817. /* Register offsets for the SWITCH_REG_* block */
  818. static const u16 bcm_sf2_7445_reg_offsets[] = {
  819. [REG_SWITCH_CNTRL] = 0x00,
  820. [REG_SWITCH_STATUS] = 0x04,
  821. [REG_DIR_DATA_WRITE] = 0x08,
  822. [REG_DIR_DATA_READ] = 0x0C,
  823. [REG_SWITCH_REVISION] = 0x18,
  824. [REG_PHY_REVISION] = 0x1C,
  825. [REG_SPHY_CNTRL] = 0x2C,
  826. [REG_RGMII_0_CNTRL] = 0x34,
  827. [REG_RGMII_1_CNTRL] = 0x40,
  828. [REG_RGMII_2_CNTRL] = 0x4c,
  829. [REG_LED_0_CNTRL] = 0x90,
  830. [REG_LED_1_CNTRL] = 0x94,
  831. [REG_LED_2_CNTRL] = 0x98,
  832. };
  833. static const struct bcm_sf2_of_data bcm_sf2_7445_data = {
  834. .type = BCM7445_DEVICE_ID,
  835. .core_reg_align = 0,
  836. .reg_offsets = bcm_sf2_7445_reg_offsets,
  837. .num_cfp_rules = 256,
  838. };
  839. static const u16 bcm_sf2_7278_reg_offsets[] = {
  840. [REG_SWITCH_CNTRL] = 0x00,
  841. [REG_SWITCH_STATUS] = 0x04,
  842. [REG_DIR_DATA_WRITE] = 0x08,
  843. [REG_DIR_DATA_READ] = 0x0c,
  844. [REG_SWITCH_REVISION] = 0x10,
  845. [REG_PHY_REVISION] = 0x14,
  846. [REG_SPHY_CNTRL] = 0x24,
  847. [REG_RGMII_0_CNTRL] = 0xe0,
  848. [REG_RGMII_1_CNTRL] = 0xec,
  849. [REG_RGMII_2_CNTRL] = 0xf8,
  850. [REG_LED_0_CNTRL] = 0x40,
  851. [REG_LED_1_CNTRL] = 0x4c,
  852. [REG_LED_2_CNTRL] = 0x58,
  853. };
  854. static const struct bcm_sf2_of_data bcm_sf2_7278_data = {
  855. .type = BCM7278_DEVICE_ID,
  856. .core_reg_align = 1,
  857. .reg_offsets = bcm_sf2_7278_reg_offsets,
  858. .num_cfp_rules = 128,
  859. };
  860. static const struct of_device_id bcm_sf2_of_match[] = {
  861. { .compatible = "brcm,bcm7445-switch-v4.0",
  862. .data = &bcm_sf2_7445_data
  863. },
  864. { .compatible = "brcm,bcm7278-switch-v4.0",
  865. .data = &bcm_sf2_7278_data
  866. },
  867. { .compatible = "brcm,bcm7278-switch-v4.8",
  868. .data = &bcm_sf2_7278_data
  869. },
  870. { /* sentinel */ },
  871. };
  872. MODULE_DEVICE_TABLE(of, bcm_sf2_of_match);
  873. static int bcm_sf2_sw_probe(struct platform_device *pdev)
  874. {
  875. const char *reg_names[BCM_SF2_REGS_NUM] = BCM_SF2_REGS_NAME;
  876. struct device_node *dn = pdev->dev.of_node;
  877. const struct of_device_id *of_id = NULL;
  878. const struct bcm_sf2_of_data *data;
  879. struct b53_platform_data *pdata;
  880. struct dsa_switch_ops *ops;
  881. struct device_node *ports;
  882. struct bcm_sf2_priv *priv;
  883. struct b53_device *dev;
  884. struct dsa_switch *ds;
  885. void __iomem **base;
  886. unsigned int i;
  887. u32 reg, rev;
  888. int ret;
  889. priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
  890. if (!priv)
  891. return -ENOMEM;
  892. ops = devm_kzalloc(&pdev->dev, sizeof(*ops), GFP_KERNEL);
  893. if (!ops)
  894. return -ENOMEM;
  895. dev = b53_switch_alloc(&pdev->dev, &bcm_sf2_io_ops, priv);
  896. if (!dev)
  897. return -ENOMEM;
  898. pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
  899. if (!pdata)
  900. return -ENOMEM;
  901. of_id = of_match_node(bcm_sf2_of_match, dn);
  902. if (!of_id || !of_id->data)
  903. return -EINVAL;
  904. data = of_id->data;
  905. /* Set SWITCH_REG register offsets and SWITCH_CORE align factor */
  906. priv->type = data->type;
  907. priv->reg_offsets = data->reg_offsets;
  908. priv->core_reg_align = data->core_reg_align;
  909. priv->num_cfp_rules = data->num_cfp_rules;
  910. /* Auto-detection using standard registers will not work, so
  911. * provide an indication of what kind of device we are for
  912. * b53_common to work with
  913. */
  914. pdata->chip_id = priv->type;
  915. dev->pdata = pdata;
  916. priv->dev = dev;
  917. ds = dev->ds;
  918. ds->ops = &bcm_sf2_ops;
  919. /* Advertise the 8 egress queues */
  920. ds->num_tx_queues = SF2_NUM_EGRESS_QUEUES;
  921. dev_set_drvdata(&pdev->dev, priv);
  922. spin_lock_init(&priv->indir_lock);
  923. mutex_init(&priv->cfp.lock);
  924. INIT_LIST_HEAD(&priv->cfp.rules_list);
  925. /* CFP rule #0 cannot be used for specific classifications, flag it as
  926. * permanently used
  927. */
  928. set_bit(0, priv->cfp.used);
  929. set_bit(0, priv->cfp.unique);
  930. /* Balance of_node_put() done by of_find_node_by_name() */
  931. of_node_get(dn);
  932. ports = of_find_node_by_name(dn, "ports");
  933. if (ports) {
  934. bcm_sf2_identify_ports(priv, ports);
  935. of_node_put(ports);
  936. }
  937. priv->irq0 = irq_of_parse_and_map(dn, 0);
  938. priv->irq1 = irq_of_parse_and_map(dn, 1);
  939. base = &priv->core;
  940. for (i = 0; i < BCM_SF2_REGS_NUM; i++) {
  941. *base = devm_platform_ioremap_resource(pdev, i);
  942. if (IS_ERR(*base)) {
  943. pr_err("unable to find register: %s\n", reg_names[i]);
  944. return PTR_ERR(*base);
  945. }
  946. base++;
  947. }
  948. ret = bcm_sf2_sw_rst(priv);
  949. if (ret) {
  950. pr_err("unable to software reset switch: %d\n", ret);
  951. return ret;
  952. }
  953. bcm_sf2_gphy_enable_set(priv->dev->ds, true);
  954. ret = bcm_sf2_mdio_register(ds);
  955. if (ret) {
  956. pr_err("failed to register MDIO bus\n");
  957. return ret;
  958. }
  959. bcm_sf2_gphy_enable_set(priv->dev->ds, false);
  960. ret = bcm_sf2_cfp_rst(priv);
  961. if (ret) {
  962. pr_err("failed to reset CFP\n");
  963. goto out_mdio;
  964. }
  965. /* Disable all interrupts and request them */
  966. bcm_sf2_intr_disable(priv);
  967. ret = devm_request_irq(&pdev->dev, priv->irq0, bcm_sf2_switch_0_isr, 0,
  968. "switch_0", ds);
  969. if (ret < 0) {
  970. pr_err("failed to request switch_0 IRQ\n");
  971. goto out_mdio;
  972. }
  973. ret = devm_request_irq(&pdev->dev, priv->irq1, bcm_sf2_switch_1_isr, 0,
  974. "switch_1", ds);
  975. if (ret < 0) {
  976. pr_err("failed to request switch_1 IRQ\n");
  977. goto out_mdio;
  978. }
  979. /* Reset the MIB counters */
  980. reg = core_readl(priv, CORE_GMNCFGCFG);
  981. reg |= RST_MIB_CNT;
  982. core_writel(priv, reg, CORE_GMNCFGCFG);
  983. reg &= ~RST_MIB_CNT;
  984. core_writel(priv, reg, CORE_GMNCFGCFG);
  985. /* Get the maximum number of ports for this switch */
  986. priv->hw_params.num_ports = core_readl(priv, CORE_IMP0_PRT_ID) + 1;
  987. if (priv->hw_params.num_ports > DSA_MAX_PORTS)
  988. priv->hw_params.num_ports = DSA_MAX_PORTS;
  989. /* Assume a single GPHY setup if we can't read that property */
  990. if (of_property_read_u32(dn, "brcm,num-gphy",
  991. &priv->hw_params.num_gphy))
  992. priv->hw_params.num_gphy = 1;
  993. rev = reg_readl(priv, REG_SWITCH_REVISION);
  994. priv->hw_params.top_rev = (rev >> SWITCH_TOP_REV_SHIFT) &
  995. SWITCH_TOP_REV_MASK;
  996. priv->hw_params.core_rev = (rev & SF2_REV_MASK);
  997. rev = reg_readl(priv, REG_PHY_REVISION);
  998. priv->hw_params.gphy_rev = rev & PHY_REVISION_MASK;
  999. ret = b53_switch_register(dev);
  1000. if (ret)
  1001. goto out_mdio;
  1002. dev_info(&pdev->dev,
  1003. "Starfighter 2 top: %x.%02x, core: %x.%02x, IRQs: %d, %d\n",
  1004. priv->hw_params.top_rev >> 8, priv->hw_params.top_rev & 0xff,
  1005. priv->hw_params.core_rev >> 8, priv->hw_params.core_rev & 0xff,
  1006. priv->irq0, priv->irq1);
  1007. return 0;
  1008. out_mdio:
  1009. bcm_sf2_mdio_unregister(priv);
  1010. return ret;
  1011. }
  1012. static int bcm_sf2_sw_remove(struct platform_device *pdev)
  1013. {
  1014. struct bcm_sf2_priv *priv = platform_get_drvdata(pdev);
  1015. priv->wol_ports_mask = 0;
  1016. /* Disable interrupts */
  1017. bcm_sf2_intr_disable(priv);
  1018. dsa_unregister_switch(priv->dev->ds);
  1019. bcm_sf2_cfp_exit(priv->dev->ds);
  1020. bcm_sf2_mdio_unregister(priv);
  1021. return 0;
  1022. }
  1023. static void bcm_sf2_sw_shutdown(struct platform_device *pdev)
  1024. {
  1025. struct bcm_sf2_priv *priv = platform_get_drvdata(pdev);
  1026. /* For a kernel about to be kexec'd we want to keep the GPHY on for a
  1027. * successful MDIO bus scan to occur. If we did turn off the GPHY
  1028. * before (e.g: port_disable), this will also power it back on.
  1029. *
  1030. * Do not rely on kexec_in_progress, just power the PHY on.
  1031. */
  1032. if (priv->hw_params.num_gphy == 1)
  1033. bcm_sf2_gphy_enable_set(priv->dev->ds, true);
  1034. }
  1035. #ifdef CONFIG_PM_SLEEP
  1036. static int bcm_sf2_suspend(struct device *dev)
  1037. {
  1038. struct bcm_sf2_priv *priv = dev_get_drvdata(dev);
  1039. return dsa_switch_suspend(priv->dev->ds);
  1040. }
  1041. static int bcm_sf2_resume(struct device *dev)
  1042. {
  1043. struct bcm_sf2_priv *priv = dev_get_drvdata(dev);
  1044. return dsa_switch_resume(priv->dev->ds);
  1045. }
  1046. #endif /* CONFIG_PM_SLEEP */
  1047. static SIMPLE_DEV_PM_OPS(bcm_sf2_pm_ops,
  1048. bcm_sf2_suspend, bcm_sf2_resume);
  1049. static struct platform_driver bcm_sf2_driver = {
  1050. .probe = bcm_sf2_sw_probe,
  1051. .remove = bcm_sf2_sw_remove,
  1052. .shutdown = bcm_sf2_sw_shutdown,
  1053. .driver = {
  1054. .name = "brcm-sf2",
  1055. .of_match_table = bcm_sf2_of_match,
  1056. .pm = &bcm_sf2_pm_ops,
  1057. },
  1058. };
  1059. module_platform_driver(bcm_sf2_driver);
  1060. MODULE_AUTHOR("Broadcom Corporation");
  1061. MODULE_DESCRIPTION("Driver for Broadcom Starfighter 2 ethernet switch chip");
  1062. MODULE_LICENSE("GPL");
  1063. MODULE_ALIAS("platform:brcm-sf2");