bcm_sf2.c 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100
  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 <net/dsa.h>
  24. #include <linux/ethtool.h>
  25. #include <linux/if_bridge.h>
  26. #include <linux/brcmphy.h>
  27. #include "bcm_sf2.h"
  28. #include "bcm_sf2_regs.h"
  29. /* String, offset, and register size in bytes if different from 4 bytes */
  30. static const struct bcm_sf2_hw_stats bcm_sf2_mib[] = {
  31. { "TxOctets", 0x000, 8 },
  32. { "TxDropPkts", 0x020 },
  33. { "TxQPKTQ0", 0x030 },
  34. { "TxBroadcastPkts", 0x040 },
  35. { "TxMulticastPkts", 0x050 },
  36. { "TxUnicastPKts", 0x060 },
  37. { "TxCollisions", 0x070 },
  38. { "TxSingleCollision", 0x080 },
  39. { "TxMultipleCollision", 0x090 },
  40. { "TxDeferredCollision", 0x0a0 },
  41. { "TxLateCollision", 0x0b0 },
  42. { "TxExcessiveCollision", 0x0c0 },
  43. { "TxFrameInDisc", 0x0d0 },
  44. { "TxPausePkts", 0x0e0 },
  45. { "TxQPKTQ1", 0x0f0 },
  46. { "TxQPKTQ2", 0x100 },
  47. { "TxQPKTQ3", 0x110 },
  48. { "TxQPKTQ4", 0x120 },
  49. { "TxQPKTQ5", 0x130 },
  50. { "RxOctets", 0x140, 8 },
  51. { "RxUndersizePkts", 0x160 },
  52. { "RxPausePkts", 0x170 },
  53. { "RxPkts64Octets", 0x180 },
  54. { "RxPkts65to127Octets", 0x190 },
  55. { "RxPkts128to255Octets", 0x1a0 },
  56. { "RxPkts256to511Octets", 0x1b0 },
  57. { "RxPkts512to1023Octets", 0x1c0 },
  58. { "RxPkts1024toMaxPktsOctets", 0x1d0 },
  59. { "RxOversizePkts", 0x1e0 },
  60. { "RxJabbers", 0x1f0 },
  61. { "RxAlignmentErrors", 0x200 },
  62. { "RxFCSErrors", 0x210 },
  63. { "RxGoodOctets", 0x220, 8 },
  64. { "RxDropPkts", 0x240 },
  65. { "RxUnicastPkts", 0x250 },
  66. { "RxMulticastPkts", 0x260 },
  67. { "RxBroadcastPkts", 0x270 },
  68. { "RxSAChanges", 0x280 },
  69. { "RxFragments", 0x290 },
  70. { "RxJumboPkt", 0x2a0 },
  71. { "RxSymblErr", 0x2b0 },
  72. { "InRangeErrCount", 0x2c0 },
  73. { "OutRangeErrCount", 0x2d0 },
  74. { "EEELpiEvent", 0x2e0 },
  75. { "EEELpiDuration", 0x2f0 },
  76. { "RxDiscard", 0x300, 8 },
  77. { "TxQPKTQ6", 0x320 },
  78. { "TxQPKTQ7", 0x330 },
  79. { "TxPkts64Octets", 0x340 },
  80. { "TxPkts65to127Octets", 0x350 },
  81. { "TxPkts128to255Octets", 0x360 },
  82. { "TxPkts256to511Ocets", 0x370 },
  83. { "TxPkts512to1023Ocets", 0x380 },
  84. { "TxPkts1024toMaxPktOcets", 0x390 },
  85. };
  86. #define BCM_SF2_STATS_SIZE ARRAY_SIZE(bcm_sf2_mib)
  87. static void bcm_sf2_sw_get_strings(struct dsa_switch *ds,
  88. int port, uint8_t *data)
  89. {
  90. unsigned int i;
  91. for (i = 0; i < BCM_SF2_STATS_SIZE; i++)
  92. memcpy(data + i * ETH_GSTRING_LEN,
  93. bcm_sf2_mib[i].string, ETH_GSTRING_LEN);
  94. }
  95. static void bcm_sf2_sw_get_ethtool_stats(struct dsa_switch *ds,
  96. int port, uint64_t *data)
  97. {
  98. struct bcm_sf2_priv *priv = ds_to_priv(ds);
  99. const struct bcm_sf2_hw_stats *s;
  100. unsigned int i;
  101. u64 val = 0;
  102. u32 offset;
  103. mutex_lock(&priv->stats_mutex);
  104. /* Now fetch the per-port counters */
  105. for (i = 0; i < BCM_SF2_STATS_SIZE; i++) {
  106. s = &bcm_sf2_mib[i];
  107. /* Do a latched 64-bit read if needed */
  108. offset = s->reg + CORE_P_MIB_OFFSET(port);
  109. if (s->sizeof_stat == 8)
  110. val = core_readq(priv, offset);
  111. else
  112. val = core_readl(priv, offset);
  113. data[i] = (u64)val;
  114. }
  115. mutex_unlock(&priv->stats_mutex);
  116. }
  117. static int bcm_sf2_sw_get_sset_count(struct dsa_switch *ds)
  118. {
  119. return BCM_SF2_STATS_SIZE;
  120. }
  121. static char *bcm_sf2_sw_probe(struct device *host_dev, int sw_addr)
  122. {
  123. return "Broadcom Starfighter 2";
  124. }
  125. static void bcm_sf2_imp_vlan_setup(struct dsa_switch *ds, int cpu_port)
  126. {
  127. struct bcm_sf2_priv *priv = ds_to_priv(ds);
  128. unsigned int i;
  129. u32 reg;
  130. /* Enable the IMP Port to be in the same VLAN as the other ports
  131. * on a per-port basis such that we only have Port i and IMP in
  132. * the same VLAN.
  133. */
  134. for (i = 0; i < priv->hw_params.num_ports; i++) {
  135. if (!((1 << i) & ds->phys_port_mask))
  136. continue;
  137. reg = core_readl(priv, CORE_PORT_VLAN_CTL_PORT(i));
  138. reg |= (1 << cpu_port);
  139. core_writel(priv, reg, CORE_PORT_VLAN_CTL_PORT(i));
  140. }
  141. }
  142. static void bcm_sf2_imp_setup(struct dsa_switch *ds, int port)
  143. {
  144. struct bcm_sf2_priv *priv = ds_to_priv(ds);
  145. u32 reg, val;
  146. /* Enable the port memories */
  147. reg = core_readl(priv, CORE_MEM_PSM_VDD_CTRL);
  148. reg &= ~P_TXQ_PSM_VDD(port);
  149. core_writel(priv, reg, CORE_MEM_PSM_VDD_CTRL);
  150. /* Enable Broadcast, Multicast, Unicast forwarding to IMP port */
  151. reg = core_readl(priv, CORE_IMP_CTL);
  152. reg |= (RX_BCST_EN | RX_MCST_EN | RX_UCST_EN);
  153. reg &= ~(RX_DIS | TX_DIS);
  154. core_writel(priv, reg, CORE_IMP_CTL);
  155. /* Enable forwarding */
  156. core_writel(priv, SW_FWDG_EN, CORE_SWMODE);
  157. /* Enable IMP port in dumb mode */
  158. reg = core_readl(priv, CORE_SWITCH_CTRL);
  159. reg |= MII_DUMB_FWDG_EN;
  160. core_writel(priv, reg, CORE_SWITCH_CTRL);
  161. /* Resolve which bit controls the Broadcom tag */
  162. switch (port) {
  163. case 8:
  164. val = BRCM_HDR_EN_P8;
  165. break;
  166. case 7:
  167. val = BRCM_HDR_EN_P7;
  168. break;
  169. case 5:
  170. val = BRCM_HDR_EN_P5;
  171. break;
  172. default:
  173. val = 0;
  174. break;
  175. }
  176. /* Enable Broadcom tags for IMP port */
  177. reg = core_readl(priv, CORE_BRCM_HDR_CTRL);
  178. reg |= val;
  179. core_writel(priv, reg, CORE_BRCM_HDR_CTRL);
  180. /* Enable reception Broadcom tag for CPU TX (switch RX) to
  181. * allow us to tag outgoing frames
  182. */
  183. reg = core_readl(priv, CORE_BRCM_HDR_RX_DIS);
  184. reg &= ~(1 << port);
  185. core_writel(priv, reg, CORE_BRCM_HDR_RX_DIS);
  186. /* Enable transmission of Broadcom tags from the switch (CPU RX) to
  187. * allow delivering frames to the per-port net_devices
  188. */
  189. reg = core_readl(priv, CORE_BRCM_HDR_TX_DIS);
  190. reg &= ~(1 << port);
  191. core_writel(priv, reg, CORE_BRCM_HDR_TX_DIS);
  192. /* Force link status for IMP port */
  193. reg = core_readl(priv, CORE_STS_OVERRIDE_IMP);
  194. reg |= (MII_SW_OR | LINK_STS);
  195. core_writel(priv, reg, CORE_STS_OVERRIDE_IMP);
  196. }
  197. static void bcm_sf2_eee_enable_set(struct dsa_switch *ds, int port, bool enable)
  198. {
  199. struct bcm_sf2_priv *priv = ds_to_priv(ds);
  200. u32 reg;
  201. reg = core_readl(priv, CORE_EEE_EN_CTRL);
  202. if (enable)
  203. reg |= 1 << port;
  204. else
  205. reg &= ~(1 << port);
  206. core_writel(priv, reg, CORE_EEE_EN_CTRL);
  207. }
  208. static void bcm_sf2_gphy_enable_set(struct dsa_switch *ds, bool enable)
  209. {
  210. struct bcm_sf2_priv *priv = ds_to_priv(ds);
  211. u32 reg;
  212. reg = reg_readl(priv, REG_SPHY_CNTRL);
  213. if (enable) {
  214. reg |= PHY_RESET;
  215. reg &= ~(EXT_PWR_DOWN | IDDQ_BIAS | CK25_DIS);
  216. reg_writel(priv, reg, REG_SPHY_CNTRL);
  217. udelay(21);
  218. reg = reg_readl(priv, REG_SPHY_CNTRL);
  219. reg &= ~PHY_RESET;
  220. } else {
  221. reg |= EXT_PWR_DOWN | IDDQ_BIAS | PHY_RESET;
  222. reg_writel(priv, reg, REG_SPHY_CNTRL);
  223. mdelay(1);
  224. reg |= CK25_DIS;
  225. }
  226. reg_writel(priv, reg, REG_SPHY_CNTRL);
  227. /* Use PHY-driven LED signaling */
  228. if (!enable) {
  229. reg = reg_readl(priv, REG_LED_CNTRL(0));
  230. reg |= SPDLNK_SRC_SEL;
  231. reg_writel(priv, reg, REG_LED_CNTRL(0));
  232. }
  233. }
  234. static int bcm_sf2_port_setup(struct dsa_switch *ds, int port,
  235. struct phy_device *phy)
  236. {
  237. struct bcm_sf2_priv *priv = ds_to_priv(ds);
  238. s8 cpu_port = ds->dst[ds->index].cpu_port;
  239. u32 reg;
  240. /* Clear the memory power down */
  241. reg = core_readl(priv, CORE_MEM_PSM_VDD_CTRL);
  242. reg &= ~P_TXQ_PSM_VDD(port);
  243. core_writel(priv, reg, CORE_MEM_PSM_VDD_CTRL);
  244. /* Clear the Rx and Tx disable bits and set to no spanning tree */
  245. core_writel(priv, 0, CORE_G_PCTL_PORT(port));
  246. /* Re-enable the GPHY and re-apply workarounds */
  247. if (port == 0 && priv->hw_params.num_gphy == 1) {
  248. bcm_sf2_gphy_enable_set(ds, true);
  249. if (phy) {
  250. /* if phy_stop() has been called before, phy
  251. * will be in halted state, and phy_start()
  252. * will call resume.
  253. *
  254. * the resume path does not configure back
  255. * autoneg settings, and since we hard reset
  256. * the phy manually here, we need to reset the
  257. * state machine also.
  258. */
  259. phy->state = PHY_READY;
  260. phy_init_hw(phy);
  261. }
  262. }
  263. /* Enable port 7 interrupts to get notified */
  264. if (port == 7)
  265. intrl2_1_mask_clear(priv, P_IRQ_MASK(P7_IRQ_OFF));
  266. /* Set this port, and only this one to be in the default VLAN,
  267. * if member of a bridge, restore its membership prior to
  268. * bringing down this port.
  269. */
  270. reg = core_readl(priv, CORE_PORT_VLAN_CTL_PORT(port));
  271. reg &= ~PORT_VLAN_CTRL_MASK;
  272. reg |= (1 << port);
  273. reg |= priv->port_sts[port].vlan_ctl_mask;
  274. core_writel(priv, reg, CORE_PORT_VLAN_CTL_PORT(port));
  275. bcm_sf2_imp_vlan_setup(ds, cpu_port);
  276. /* If EEE was enabled, restore it */
  277. if (priv->port_sts[port].eee.eee_enabled)
  278. bcm_sf2_eee_enable_set(ds, port, true);
  279. return 0;
  280. }
  281. static void bcm_sf2_port_disable(struct dsa_switch *ds, int port,
  282. struct phy_device *phy)
  283. {
  284. struct bcm_sf2_priv *priv = ds_to_priv(ds);
  285. u32 off, reg;
  286. if (priv->wol_ports_mask & (1 << port))
  287. return;
  288. if (port == 7) {
  289. intrl2_1_mask_set(priv, P_IRQ_MASK(P7_IRQ_OFF));
  290. intrl2_1_writel(priv, P_IRQ_MASK(P7_IRQ_OFF), INTRL2_CPU_CLEAR);
  291. }
  292. if (port == 0 && priv->hw_params.num_gphy == 1)
  293. bcm_sf2_gphy_enable_set(ds, false);
  294. if (dsa_is_cpu_port(ds, port))
  295. off = CORE_IMP_CTL;
  296. else
  297. off = CORE_G_PCTL_PORT(port);
  298. reg = core_readl(priv, off);
  299. reg |= RX_DIS | TX_DIS;
  300. core_writel(priv, reg, off);
  301. /* Power down the port memory */
  302. reg = core_readl(priv, CORE_MEM_PSM_VDD_CTRL);
  303. reg |= P_TXQ_PSM_VDD(port);
  304. core_writel(priv, reg, CORE_MEM_PSM_VDD_CTRL);
  305. }
  306. /* Returns 0 if EEE was not enabled, or 1 otherwise
  307. */
  308. static int bcm_sf2_eee_init(struct dsa_switch *ds, int port,
  309. struct phy_device *phy)
  310. {
  311. struct bcm_sf2_priv *priv = ds_to_priv(ds);
  312. struct ethtool_eee *p = &priv->port_sts[port].eee;
  313. int ret;
  314. p->supported = (SUPPORTED_1000baseT_Full | SUPPORTED_100baseT_Full);
  315. ret = phy_init_eee(phy, 0);
  316. if (ret)
  317. return 0;
  318. bcm_sf2_eee_enable_set(ds, port, true);
  319. return 1;
  320. }
  321. static int bcm_sf2_sw_get_eee(struct dsa_switch *ds, int port,
  322. struct ethtool_eee *e)
  323. {
  324. struct bcm_sf2_priv *priv = ds_to_priv(ds);
  325. struct ethtool_eee *p = &priv->port_sts[port].eee;
  326. u32 reg;
  327. reg = core_readl(priv, CORE_EEE_LPI_INDICATE);
  328. e->eee_enabled = p->eee_enabled;
  329. e->eee_active = !!(reg & (1 << port));
  330. return 0;
  331. }
  332. static int bcm_sf2_sw_set_eee(struct dsa_switch *ds, int port,
  333. struct phy_device *phydev,
  334. struct ethtool_eee *e)
  335. {
  336. struct bcm_sf2_priv *priv = ds_to_priv(ds);
  337. struct ethtool_eee *p = &priv->port_sts[port].eee;
  338. p->eee_enabled = e->eee_enabled;
  339. if (!p->eee_enabled) {
  340. bcm_sf2_eee_enable_set(ds, port, false);
  341. } else {
  342. p->eee_enabled = bcm_sf2_eee_init(ds, port, phydev);
  343. if (!p->eee_enabled)
  344. return -EOPNOTSUPP;
  345. }
  346. return 0;
  347. }
  348. /* Fast-ageing of ARL entries for a given port, equivalent to an ARL
  349. * flush for that port.
  350. */
  351. static int bcm_sf2_sw_fast_age_port(struct dsa_switch *ds, int port)
  352. {
  353. struct bcm_sf2_priv *priv = ds_to_priv(ds);
  354. unsigned int timeout = 1000;
  355. u32 reg;
  356. core_writel(priv, port, CORE_FAST_AGE_PORT);
  357. reg = core_readl(priv, CORE_FAST_AGE_CTRL);
  358. reg |= EN_AGE_PORT | FAST_AGE_STR_DONE;
  359. core_writel(priv, reg, CORE_FAST_AGE_CTRL);
  360. do {
  361. reg = core_readl(priv, CORE_FAST_AGE_CTRL);
  362. if (!(reg & FAST_AGE_STR_DONE))
  363. break;
  364. cpu_relax();
  365. } while (timeout--);
  366. if (!timeout)
  367. return -ETIMEDOUT;
  368. return 0;
  369. }
  370. static int bcm_sf2_sw_br_join(struct dsa_switch *ds, int port,
  371. u32 br_port_mask)
  372. {
  373. struct bcm_sf2_priv *priv = ds_to_priv(ds);
  374. unsigned int i;
  375. u32 reg, p_ctl;
  376. p_ctl = core_readl(priv, CORE_PORT_VLAN_CTL_PORT(port));
  377. for (i = 0; i < priv->hw_params.num_ports; i++) {
  378. if (!((1 << i) & br_port_mask))
  379. continue;
  380. /* Add this local port to the remote port VLAN control
  381. * membership and update the remote port bitmask
  382. */
  383. reg = core_readl(priv, CORE_PORT_VLAN_CTL_PORT(i));
  384. reg |= 1 << port;
  385. core_writel(priv, reg, CORE_PORT_VLAN_CTL_PORT(i));
  386. priv->port_sts[i].vlan_ctl_mask = reg;
  387. p_ctl |= 1 << i;
  388. }
  389. /* Configure the local port VLAN control membership to include
  390. * remote ports and update the local port bitmask
  391. */
  392. core_writel(priv, p_ctl, CORE_PORT_VLAN_CTL_PORT(port));
  393. priv->port_sts[port].vlan_ctl_mask = p_ctl;
  394. return 0;
  395. }
  396. static int bcm_sf2_sw_br_leave(struct dsa_switch *ds, int port,
  397. u32 br_port_mask)
  398. {
  399. struct bcm_sf2_priv *priv = ds_to_priv(ds);
  400. unsigned int i;
  401. u32 reg, p_ctl;
  402. p_ctl = core_readl(priv, CORE_PORT_VLAN_CTL_PORT(port));
  403. for (i = 0; i < priv->hw_params.num_ports; i++) {
  404. /* Don't touch the remaining ports */
  405. if (!((1 << i) & br_port_mask))
  406. continue;
  407. reg = core_readl(priv, CORE_PORT_VLAN_CTL_PORT(i));
  408. reg &= ~(1 << port);
  409. core_writel(priv, reg, CORE_PORT_VLAN_CTL_PORT(i));
  410. priv->port_sts[port].vlan_ctl_mask = reg;
  411. /* Prevent self removal to preserve isolation */
  412. if (port != i)
  413. p_ctl &= ~(1 << i);
  414. }
  415. core_writel(priv, p_ctl, CORE_PORT_VLAN_CTL_PORT(port));
  416. priv->port_sts[port].vlan_ctl_mask = p_ctl;
  417. return 0;
  418. }
  419. static int bcm_sf2_sw_br_set_stp_state(struct dsa_switch *ds, int port,
  420. u8 state)
  421. {
  422. struct bcm_sf2_priv *priv = ds_to_priv(ds);
  423. u8 hw_state, cur_hw_state;
  424. int ret = 0;
  425. u32 reg;
  426. reg = core_readl(priv, CORE_G_PCTL_PORT(port));
  427. cur_hw_state = reg >> G_MISTP_STATE_SHIFT;
  428. switch (state) {
  429. case BR_STATE_DISABLED:
  430. hw_state = G_MISTP_DIS_STATE;
  431. break;
  432. case BR_STATE_LISTENING:
  433. hw_state = G_MISTP_LISTEN_STATE;
  434. break;
  435. case BR_STATE_LEARNING:
  436. hw_state = G_MISTP_LEARN_STATE;
  437. break;
  438. case BR_STATE_FORWARDING:
  439. hw_state = G_MISTP_FWD_STATE;
  440. break;
  441. case BR_STATE_BLOCKING:
  442. hw_state = G_MISTP_BLOCK_STATE;
  443. break;
  444. default:
  445. pr_err("%s: invalid STP state: %d\n", __func__, state);
  446. return -EINVAL;
  447. }
  448. /* Fast-age ARL entries if we are moving a port from Learning or
  449. * Forwarding state to Disabled, Blocking or Listening state
  450. */
  451. if (cur_hw_state != hw_state) {
  452. if (cur_hw_state & 4 && !(hw_state & 4)) {
  453. ret = bcm_sf2_sw_fast_age_port(ds, port);
  454. if (ret) {
  455. pr_err("%s: fast-ageing failed\n", __func__);
  456. return ret;
  457. }
  458. }
  459. }
  460. reg = core_readl(priv, CORE_G_PCTL_PORT(port));
  461. reg &= ~(G_MISTP_STATE_MASK << G_MISTP_STATE_SHIFT);
  462. reg |= hw_state;
  463. core_writel(priv, reg, CORE_G_PCTL_PORT(port));
  464. return 0;
  465. }
  466. static irqreturn_t bcm_sf2_switch_0_isr(int irq, void *dev_id)
  467. {
  468. struct bcm_sf2_priv *priv = dev_id;
  469. priv->irq0_stat = intrl2_0_readl(priv, INTRL2_CPU_STATUS) &
  470. ~priv->irq0_mask;
  471. intrl2_0_writel(priv, priv->irq0_stat, INTRL2_CPU_CLEAR);
  472. return IRQ_HANDLED;
  473. }
  474. static irqreturn_t bcm_sf2_switch_1_isr(int irq, void *dev_id)
  475. {
  476. struct bcm_sf2_priv *priv = dev_id;
  477. priv->irq1_stat = intrl2_1_readl(priv, INTRL2_CPU_STATUS) &
  478. ~priv->irq1_mask;
  479. intrl2_1_writel(priv, priv->irq1_stat, INTRL2_CPU_CLEAR);
  480. if (priv->irq1_stat & P_LINK_UP_IRQ(P7_IRQ_OFF))
  481. priv->port_sts[7].link = 1;
  482. if (priv->irq1_stat & P_LINK_DOWN_IRQ(P7_IRQ_OFF))
  483. priv->port_sts[7].link = 0;
  484. return IRQ_HANDLED;
  485. }
  486. static int bcm_sf2_sw_rst(struct bcm_sf2_priv *priv)
  487. {
  488. unsigned int timeout = 1000;
  489. u32 reg;
  490. reg = core_readl(priv, CORE_WATCHDOG_CTRL);
  491. reg |= SOFTWARE_RESET | EN_CHIP_RST | EN_SW_RESET;
  492. core_writel(priv, reg, CORE_WATCHDOG_CTRL);
  493. do {
  494. reg = core_readl(priv, CORE_WATCHDOG_CTRL);
  495. if (!(reg & SOFTWARE_RESET))
  496. break;
  497. usleep_range(1000, 2000);
  498. } while (timeout-- > 0);
  499. if (timeout == 0)
  500. return -ETIMEDOUT;
  501. return 0;
  502. }
  503. static void bcm_sf2_intr_disable(struct bcm_sf2_priv *priv)
  504. {
  505. intrl2_0_writel(priv, 0xffffffff, INTRL2_CPU_MASK_SET);
  506. intrl2_0_writel(priv, 0xffffffff, INTRL2_CPU_CLEAR);
  507. intrl2_0_writel(priv, 0, INTRL2_CPU_MASK_CLEAR);
  508. intrl2_1_writel(priv, 0xffffffff, INTRL2_CPU_MASK_SET);
  509. intrl2_1_writel(priv, 0xffffffff, INTRL2_CPU_CLEAR);
  510. intrl2_1_writel(priv, 0, INTRL2_CPU_MASK_CLEAR);
  511. }
  512. static int bcm_sf2_sw_setup(struct dsa_switch *ds)
  513. {
  514. const char *reg_names[BCM_SF2_REGS_NUM] = BCM_SF2_REGS_NAME;
  515. struct bcm_sf2_priv *priv = ds_to_priv(ds);
  516. struct device_node *dn;
  517. void __iomem **base;
  518. unsigned int port;
  519. unsigned int i;
  520. u32 reg, rev;
  521. int ret;
  522. spin_lock_init(&priv->indir_lock);
  523. mutex_init(&priv->stats_mutex);
  524. /* All the interesting properties are at the parent device_node
  525. * level
  526. */
  527. dn = ds->pd->of_node->parent;
  528. priv->irq0 = irq_of_parse_and_map(dn, 0);
  529. priv->irq1 = irq_of_parse_and_map(dn, 1);
  530. base = &priv->core;
  531. for (i = 0; i < BCM_SF2_REGS_NUM; i++) {
  532. *base = of_iomap(dn, i);
  533. if (*base == NULL) {
  534. pr_err("unable to find register: %s\n", reg_names[i]);
  535. ret = -ENOMEM;
  536. goto out_unmap;
  537. }
  538. base++;
  539. }
  540. ret = bcm_sf2_sw_rst(priv);
  541. if (ret) {
  542. pr_err("unable to software reset switch: %d\n", ret);
  543. goto out_unmap;
  544. }
  545. /* Disable all interrupts and request them */
  546. bcm_sf2_intr_disable(priv);
  547. ret = request_irq(priv->irq0, bcm_sf2_switch_0_isr, 0,
  548. "switch_0", priv);
  549. if (ret < 0) {
  550. pr_err("failed to request switch_0 IRQ\n");
  551. goto out_unmap;
  552. }
  553. ret = request_irq(priv->irq1, bcm_sf2_switch_1_isr, 0,
  554. "switch_1", priv);
  555. if (ret < 0) {
  556. pr_err("failed to request switch_1 IRQ\n");
  557. goto out_free_irq0;
  558. }
  559. /* Reset the MIB counters */
  560. reg = core_readl(priv, CORE_GMNCFGCFG);
  561. reg |= RST_MIB_CNT;
  562. core_writel(priv, reg, CORE_GMNCFGCFG);
  563. reg &= ~RST_MIB_CNT;
  564. core_writel(priv, reg, CORE_GMNCFGCFG);
  565. /* Get the maximum number of ports for this switch */
  566. priv->hw_params.num_ports = core_readl(priv, CORE_IMP0_PRT_ID) + 1;
  567. if (priv->hw_params.num_ports > DSA_MAX_PORTS)
  568. priv->hw_params.num_ports = DSA_MAX_PORTS;
  569. /* Assume a single GPHY setup if we can't read that property */
  570. if (of_property_read_u32(dn, "brcm,num-gphy",
  571. &priv->hw_params.num_gphy))
  572. priv->hw_params.num_gphy = 1;
  573. /* Enable all valid ports and disable those unused */
  574. for (port = 0; port < priv->hw_params.num_ports; port++) {
  575. /* IMP port receives special treatment */
  576. if ((1 << port) & ds->phys_port_mask)
  577. bcm_sf2_port_setup(ds, port, NULL);
  578. else if (dsa_is_cpu_port(ds, port))
  579. bcm_sf2_imp_setup(ds, port);
  580. else
  581. bcm_sf2_port_disable(ds, port, NULL);
  582. }
  583. /* Include the pseudo-PHY address and the broadcast PHY address to
  584. * divert reads towards our workaround
  585. */
  586. ds->phys_mii_mask |= ((1 << BRCM_PSEUDO_PHY_ADDR) | (1 << 0));
  587. rev = reg_readl(priv, REG_SWITCH_REVISION);
  588. priv->hw_params.top_rev = (rev >> SWITCH_TOP_REV_SHIFT) &
  589. SWITCH_TOP_REV_MASK;
  590. priv->hw_params.core_rev = (rev & SF2_REV_MASK);
  591. rev = reg_readl(priv, REG_PHY_REVISION);
  592. priv->hw_params.gphy_rev = rev & PHY_REVISION_MASK;
  593. pr_info("Starfighter 2 top: %x.%02x, core: %x.%02x base: 0x%p, IRQs: %d, %d\n",
  594. priv->hw_params.top_rev >> 8, priv->hw_params.top_rev & 0xff,
  595. priv->hw_params.core_rev >> 8, priv->hw_params.core_rev & 0xff,
  596. priv->core, priv->irq0, priv->irq1);
  597. return 0;
  598. out_free_irq0:
  599. free_irq(priv->irq0, priv);
  600. out_unmap:
  601. base = &priv->core;
  602. for (i = 0; i < BCM_SF2_REGS_NUM; i++) {
  603. if (*base)
  604. iounmap(*base);
  605. base++;
  606. }
  607. return ret;
  608. }
  609. static int bcm_sf2_sw_set_addr(struct dsa_switch *ds, u8 *addr)
  610. {
  611. return 0;
  612. }
  613. static u32 bcm_sf2_sw_get_phy_flags(struct dsa_switch *ds, int port)
  614. {
  615. struct bcm_sf2_priv *priv = ds_to_priv(ds);
  616. /* The BCM7xxx PHY driver expects to find the integrated PHY revision
  617. * in bits 15:8 and the patch level in bits 7:0 which is exactly what
  618. * the REG_PHY_REVISION register layout is.
  619. */
  620. return priv->hw_params.gphy_rev;
  621. }
  622. static int bcm_sf2_sw_indir_rw(struct dsa_switch *ds, int op, int addr,
  623. int regnum, u16 val)
  624. {
  625. struct bcm_sf2_priv *priv = ds_to_priv(ds);
  626. int ret = 0;
  627. u32 reg;
  628. reg = reg_readl(priv, REG_SWITCH_CNTRL);
  629. reg |= MDIO_MASTER_SEL;
  630. reg_writel(priv, reg, REG_SWITCH_CNTRL);
  631. /* Page << 8 | offset */
  632. reg = 0x70;
  633. reg <<= 2;
  634. core_writel(priv, addr, reg);
  635. /* Page << 8 | offset */
  636. reg = 0x80 << 8 | regnum << 1;
  637. reg <<= 2;
  638. if (op)
  639. ret = core_readl(priv, reg);
  640. else
  641. core_writel(priv, val, reg);
  642. reg = reg_readl(priv, REG_SWITCH_CNTRL);
  643. reg &= ~MDIO_MASTER_SEL;
  644. reg_writel(priv, reg, REG_SWITCH_CNTRL);
  645. return ret & 0xffff;
  646. }
  647. static int bcm_sf2_sw_phy_read(struct dsa_switch *ds, int addr, int regnum)
  648. {
  649. /* Intercept reads from the MDIO broadcast address or Broadcom
  650. * pseudo-PHY address
  651. */
  652. switch (addr) {
  653. case 0:
  654. case BRCM_PSEUDO_PHY_ADDR:
  655. return bcm_sf2_sw_indir_rw(ds, 1, addr, regnum, 0);
  656. default:
  657. return 0xffff;
  658. }
  659. }
  660. static int bcm_sf2_sw_phy_write(struct dsa_switch *ds, int addr, int regnum,
  661. u16 val)
  662. {
  663. /* Intercept writes to the MDIO broadcast address or Broadcom
  664. * pseudo-PHY address
  665. */
  666. switch (addr) {
  667. case 0:
  668. case BRCM_PSEUDO_PHY_ADDR:
  669. bcm_sf2_sw_indir_rw(ds, 0, addr, regnum, val);
  670. break;
  671. }
  672. return 0;
  673. }
  674. static void bcm_sf2_sw_adjust_link(struct dsa_switch *ds, int port,
  675. struct phy_device *phydev)
  676. {
  677. struct bcm_sf2_priv *priv = ds_to_priv(ds);
  678. u32 id_mode_dis = 0, port_mode;
  679. const char *str = NULL;
  680. u32 reg;
  681. switch (phydev->interface) {
  682. case PHY_INTERFACE_MODE_RGMII:
  683. str = "RGMII (no delay)";
  684. id_mode_dis = 1;
  685. case PHY_INTERFACE_MODE_RGMII_TXID:
  686. if (!str)
  687. str = "RGMII (TX delay)";
  688. port_mode = EXT_GPHY;
  689. break;
  690. case PHY_INTERFACE_MODE_MII:
  691. str = "MII";
  692. port_mode = EXT_EPHY;
  693. break;
  694. case PHY_INTERFACE_MODE_REVMII:
  695. str = "Reverse MII";
  696. port_mode = EXT_REVMII;
  697. break;
  698. default:
  699. /* All other PHYs: internal and MoCA */
  700. goto force_link;
  701. }
  702. /* If the link is down, just disable the interface to conserve power */
  703. if (!phydev->link) {
  704. reg = reg_readl(priv, REG_RGMII_CNTRL_P(port));
  705. reg &= ~RGMII_MODE_EN;
  706. reg_writel(priv, reg, REG_RGMII_CNTRL_P(port));
  707. goto force_link;
  708. }
  709. /* Clear id_mode_dis bit, and the existing port mode, but
  710. * make sure we enable the RGMII block for data to pass
  711. */
  712. reg = reg_readl(priv, REG_RGMII_CNTRL_P(port));
  713. reg &= ~ID_MODE_DIS;
  714. reg &= ~(PORT_MODE_MASK << PORT_MODE_SHIFT);
  715. reg &= ~(RX_PAUSE_EN | TX_PAUSE_EN);
  716. reg |= port_mode | RGMII_MODE_EN;
  717. if (id_mode_dis)
  718. reg |= ID_MODE_DIS;
  719. if (phydev->pause) {
  720. if (phydev->asym_pause)
  721. reg |= TX_PAUSE_EN;
  722. reg |= RX_PAUSE_EN;
  723. }
  724. reg_writel(priv, reg, REG_RGMII_CNTRL_P(port));
  725. pr_info("Port %d configured for %s\n", port, str);
  726. force_link:
  727. /* Force link settings detected from the PHY */
  728. reg = SW_OVERRIDE;
  729. switch (phydev->speed) {
  730. case SPEED_1000:
  731. reg |= SPDSTS_1000 << SPEED_SHIFT;
  732. break;
  733. case SPEED_100:
  734. reg |= SPDSTS_100 << SPEED_SHIFT;
  735. break;
  736. }
  737. if (phydev->link)
  738. reg |= LINK_STS;
  739. if (phydev->duplex == DUPLEX_FULL)
  740. reg |= DUPLX_MODE;
  741. core_writel(priv, reg, CORE_STS_OVERRIDE_GMIIP_PORT(port));
  742. }
  743. static void bcm_sf2_sw_fixed_link_update(struct dsa_switch *ds, int port,
  744. struct fixed_phy_status *status)
  745. {
  746. struct bcm_sf2_priv *priv = ds_to_priv(ds);
  747. u32 duplex, pause, speed;
  748. u32 reg;
  749. duplex = core_readl(priv, CORE_DUPSTS);
  750. pause = core_readl(priv, CORE_PAUSESTS);
  751. speed = core_readl(priv, CORE_SPDSTS);
  752. speed >>= (port * SPDSTS_SHIFT);
  753. speed &= SPDSTS_MASK;
  754. status->link = 0;
  755. /* Port 7 is special as we do not get link status from CORE_LNKSTS,
  756. * which means that we need to force the link at the port override
  757. * level to get the data to flow. We do use what the interrupt handler
  758. * did determine before.
  759. *
  760. * For the other ports, we just force the link status, since this is
  761. * a fixed PHY device.
  762. */
  763. if (port == 7) {
  764. status->link = priv->port_sts[port].link;
  765. /* For MoCA interfaces, also force a link down notification
  766. * since some version of the user-space daemon (mocad) use
  767. * cmd->autoneg to force the link, which messes up the PHY
  768. * state machine and make it go in PHY_FORCING state instead.
  769. */
  770. if (!status->link)
  771. netif_carrier_off(ds->ports[port]);
  772. status->duplex = 1;
  773. } else {
  774. status->link = 1;
  775. status->duplex = !!(duplex & (1 << port));
  776. }
  777. reg = core_readl(priv, CORE_STS_OVERRIDE_GMIIP_PORT(port));
  778. reg |= SW_OVERRIDE;
  779. if (status->link)
  780. reg |= LINK_STS;
  781. else
  782. reg &= ~LINK_STS;
  783. core_writel(priv, reg, CORE_STS_OVERRIDE_GMIIP_PORT(port));
  784. switch (speed) {
  785. case SPDSTS_10:
  786. status->speed = SPEED_10;
  787. break;
  788. case SPDSTS_100:
  789. status->speed = SPEED_100;
  790. break;
  791. case SPDSTS_1000:
  792. status->speed = SPEED_1000;
  793. break;
  794. }
  795. if ((pause & (1 << port)) &&
  796. (pause & (1 << (port + PAUSESTS_TX_PAUSE_SHIFT)))) {
  797. status->asym_pause = 1;
  798. status->pause = 1;
  799. }
  800. if (pause & (1 << port))
  801. status->pause = 1;
  802. }
  803. static int bcm_sf2_sw_suspend(struct dsa_switch *ds)
  804. {
  805. struct bcm_sf2_priv *priv = ds_to_priv(ds);
  806. unsigned int port;
  807. bcm_sf2_intr_disable(priv);
  808. /* Disable all ports physically present including the IMP
  809. * port, the other ones have already been disabled during
  810. * bcm_sf2_sw_setup
  811. */
  812. for (port = 0; port < DSA_MAX_PORTS; port++) {
  813. if ((1 << port) & ds->phys_port_mask ||
  814. dsa_is_cpu_port(ds, port))
  815. bcm_sf2_port_disable(ds, port, NULL);
  816. }
  817. return 0;
  818. }
  819. static int bcm_sf2_sw_resume(struct dsa_switch *ds)
  820. {
  821. struct bcm_sf2_priv *priv = ds_to_priv(ds);
  822. unsigned int port;
  823. int ret;
  824. ret = bcm_sf2_sw_rst(priv);
  825. if (ret) {
  826. pr_err("%s: failed to software reset switch\n", __func__);
  827. return ret;
  828. }
  829. if (priv->hw_params.num_gphy == 1)
  830. bcm_sf2_gphy_enable_set(ds, true);
  831. for (port = 0; port < DSA_MAX_PORTS; port++) {
  832. if ((1 << port) & ds->phys_port_mask)
  833. bcm_sf2_port_setup(ds, port, NULL);
  834. else if (dsa_is_cpu_port(ds, port))
  835. bcm_sf2_imp_setup(ds, port);
  836. }
  837. return 0;
  838. }
  839. static void bcm_sf2_sw_get_wol(struct dsa_switch *ds, int port,
  840. struct ethtool_wolinfo *wol)
  841. {
  842. struct net_device *p = ds->dst[ds->index].master_netdev;
  843. struct bcm_sf2_priv *priv = ds_to_priv(ds);
  844. struct ethtool_wolinfo pwol;
  845. /* Get the parent device WoL settings */
  846. p->ethtool_ops->get_wol(p, &pwol);
  847. /* Advertise the parent device supported settings */
  848. wol->supported = pwol.supported;
  849. memset(&wol->sopass, 0, sizeof(wol->sopass));
  850. if (pwol.wolopts & WAKE_MAGICSECURE)
  851. memcpy(&wol->sopass, pwol.sopass, sizeof(wol->sopass));
  852. if (priv->wol_ports_mask & (1 << port))
  853. wol->wolopts = pwol.wolopts;
  854. else
  855. wol->wolopts = 0;
  856. }
  857. static int bcm_sf2_sw_set_wol(struct dsa_switch *ds, int port,
  858. struct ethtool_wolinfo *wol)
  859. {
  860. struct net_device *p = ds->dst[ds->index].master_netdev;
  861. struct bcm_sf2_priv *priv = ds_to_priv(ds);
  862. s8 cpu_port = ds->dst[ds->index].cpu_port;
  863. struct ethtool_wolinfo pwol;
  864. p->ethtool_ops->get_wol(p, &pwol);
  865. if (wol->wolopts & ~pwol.supported)
  866. return -EINVAL;
  867. if (wol->wolopts)
  868. priv->wol_ports_mask |= (1 << port);
  869. else
  870. priv->wol_ports_mask &= ~(1 << port);
  871. /* If we have at least one port enabled, make sure the CPU port
  872. * is also enabled. If the CPU port is the last one enabled, we disable
  873. * it since this configuration does not make sense.
  874. */
  875. if (priv->wol_ports_mask && priv->wol_ports_mask != (1 << cpu_port))
  876. priv->wol_ports_mask |= (1 << cpu_port);
  877. else
  878. priv->wol_ports_mask &= ~(1 << cpu_port);
  879. return p->ethtool_ops->set_wol(p, wol);
  880. }
  881. static struct dsa_switch_driver bcm_sf2_switch_driver = {
  882. .tag_protocol = DSA_TAG_PROTO_BRCM,
  883. .priv_size = sizeof(struct bcm_sf2_priv),
  884. .probe = bcm_sf2_sw_probe,
  885. .setup = bcm_sf2_sw_setup,
  886. .set_addr = bcm_sf2_sw_set_addr,
  887. .get_phy_flags = bcm_sf2_sw_get_phy_flags,
  888. .phy_read = bcm_sf2_sw_phy_read,
  889. .phy_write = bcm_sf2_sw_phy_write,
  890. .get_strings = bcm_sf2_sw_get_strings,
  891. .get_ethtool_stats = bcm_sf2_sw_get_ethtool_stats,
  892. .get_sset_count = bcm_sf2_sw_get_sset_count,
  893. .adjust_link = bcm_sf2_sw_adjust_link,
  894. .fixed_link_update = bcm_sf2_sw_fixed_link_update,
  895. .suspend = bcm_sf2_sw_suspend,
  896. .resume = bcm_sf2_sw_resume,
  897. .get_wol = bcm_sf2_sw_get_wol,
  898. .set_wol = bcm_sf2_sw_set_wol,
  899. .port_enable = bcm_sf2_port_setup,
  900. .port_disable = bcm_sf2_port_disable,
  901. .get_eee = bcm_sf2_sw_get_eee,
  902. .set_eee = bcm_sf2_sw_set_eee,
  903. .port_join_bridge = bcm_sf2_sw_br_join,
  904. .port_leave_bridge = bcm_sf2_sw_br_leave,
  905. .port_stp_update = bcm_sf2_sw_br_set_stp_state,
  906. };
  907. static int __init bcm_sf2_init(void)
  908. {
  909. register_switch_driver(&bcm_sf2_switch_driver);
  910. return 0;
  911. }
  912. module_init(bcm_sf2_init);
  913. static void __exit bcm_sf2_exit(void)
  914. {
  915. unregister_switch_driver(&bcm_sf2_switch_driver);
  916. }
  917. module_exit(bcm_sf2_exit);
  918. MODULE_AUTHOR("Broadcom Corporation");
  919. MODULE_DESCRIPTION("Driver for Broadcom Starfighter 2 ethernet switch chip");
  920. MODULE_LICENSE("GPL");
  921. MODULE_ALIAS("platform:brcm-sf2");