bcm_sf2.c 27 KB

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