bcm_sf2.c 33 KB

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