common.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. #include <linux/of_mdio.h>
  3. #include <linux/of_platform.h>
  4. #include <asm/mach-rtl838x/mach-rtl83xx.h>
  5. #include "rtl83xx.h"
  6. extern struct rtl83xx_soc_info soc_info;
  7. extern const struct rtl838x_reg rtl838x_reg;
  8. extern const struct rtl838x_reg rtl839x_reg;
  9. extern const struct dsa_switch_ops rtl83xx_switch_ops;
  10. DEFINE_MUTEX(smi_lock);
  11. // TODO: unused
  12. static void dump_fdb(struct rtl838x_switch_priv *priv)
  13. {
  14. struct rtl838x_l2_entry e;
  15. int i;
  16. mutex_lock(&priv->reg_mutex);
  17. for (i = 0; i < priv->fib_entries; i++) {
  18. priv->r->read_l2_entry_using_hash(i >> 2, i & 0x3, &e);
  19. if (!e.valid) /* Check for invalid entry */
  20. continue;
  21. pr_debug("-> port %02d: mac %pM, vid: %d, rvid: %d, MC: %d, %d\n",
  22. e.port, &e.mac[0], e.vid, e.rvid, e.is_ip_mc, e.is_ipv6_mc);
  23. }
  24. mutex_unlock(&priv->reg_mutex);
  25. }
  26. // TODO: unused
  27. static void rtl83xx_port_get_stp_state(struct rtl838x_switch_priv *priv, int port)
  28. {
  29. u32 cmd, msti = 0;
  30. u32 port_state[4];
  31. int index, bit, i;
  32. int pos = port;
  33. int n = priv->family_id == RTL8380_FAMILY_ID ? 2 : 4;
  34. /* CPU PORT can only be configured on RTL838x */
  35. if (port >= priv->cpu_port || port > 51)
  36. return;
  37. mutex_lock(&priv->reg_mutex);
  38. /* For the RTL839x, the bits are left-aligned in the 128 bit field */
  39. if (priv->family_id == RTL8390_FAMILY_ID)
  40. pos += 12;
  41. index = n - (pos >> 4) - 1;
  42. bit = (pos << 1) % 32;
  43. if (priv->family_id == RTL8380_FAMILY_ID) {
  44. cmd = BIT(15) /* Execute cmd */
  45. | BIT(14) /* Read */
  46. | 2 << 12 /* Table type 0b10 */
  47. | (msti & 0xfff);
  48. } else {
  49. cmd = BIT(16) /* Execute cmd */
  50. | 0 << 15 /* Read */
  51. | 5 << 12 /* Table type 0b101 */
  52. | (msti & 0xfff);
  53. }
  54. priv->r->exec_tbl0_cmd(cmd);
  55. for (i = 0; i < n; i++)
  56. port_state[i] = sw_r32(priv->r->tbl_access_data_0(i));
  57. mutex_unlock(&priv->reg_mutex);
  58. }
  59. int rtl83xx_dsa_phy_read(struct dsa_switch *ds, int phy_addr, int phy_reg)
  60. {
  61. u32 val;
  62. u32 offset = 0;
  63. struct rtl838x_switch_priv *priv = ds->priv;
  64. if (phy_addr >= 24 && phy_addr <= 27
  65. && priv->ports[24].phy == PHY_RTL838X_SDS) {
  66. if (phy_addr == 26)
  67. offset = 0x100;
  68. val = sw_r32(MAPLE_SDS4_FIB_REG0r + offset + (phy_reg << 2)) & 0xffff;
  69. return val;
  70. }
  71. if (soc_info.family == RTL8390_FAMILY_ID)
  72. rtl839x_read_phy(phy_addr, 0, phy_reg, &val);
  73. else
  74. rtl838x_read_phy(phy_addr, 0, phy_reg, &val);
  75. return val;
  76. }
  77. int rtl83xx_dsa_phy_write(struct dsa_switch *ds, int phy_addr, int phy_reg, u16 val)
  78. {
  79. u32 offset = 0;
  80. struct rtl838x_switch_priv *priv = ds->priv;
  81. if (phy_addr >= 24 && phy_addr <= 27
  82. && priv->ports[24].phy == PHY_RTL838X_SDS) {
  83. if (phy_addr == 26)
  84. offset = 0x100;
  85. sw_w32(val, MAPLE_SDS4_FIB_REG0r + offset + (phy_reg << 2));
  86. return 0;
  87. }
  88. if (soc_info.family == RTL8390_FAMILY_ID)
  89. return rtl839x_write_phy(phy_addr, 0, phy_reg, val);
  90. else
  91. return rtl838x_write_phy(phy_addr, 0, phy_reg, val);
  92. }
  93. static int rtl83xx_mdio_read(struct mii_bus *bus, int addr, int regnum)
  94. {
  95. int ret;
  96. struct rtl838x_switch_priv *priv = bus->priv;
  97. ret = rtl83xx_dsa_phy_read(priv->ds, addr, regnum);
  98. return ret;
  99. }
  100. static int rtl83xx_mdio_write(struct mii_bus *bus, int addr, int regnum,
  101. u16 val)
  102. {
  103. struct rtl838x_switch_priv *priv = bus->priv;
  104. return rtl83xx_dsa_phy_write(priv->ds, addr, regnum, val);
  105. }
  106. static void rtl8380_sds_rst(int mac)
  107. {
  108. u32 offset = (mac == 24) ? 0 : 0x100;
  109. sw_w32_mask(1 << 11, 0, RTL8380_SDS4_FIB_REG0 + offset);
  110. sw_w32_mask(0x3, 0, RTL838X_SDS4_REG28 + offset);
  111. sw_w32_mask(0x3, 0x3, RTL838X_SDS4_REG28 + offset);
  112. sw_w32_mask(0, 0x1 << 6, RTL838X_SDS4_DUMMY0 + offset);
  113. sw_w32_mask(0x1 << 6, 0, RTL838X_SDS4_DUMMY0 + offset);
  114. pr_debug("SERDES reset: %d\n", mac);
  115. }
  116. static int __init rtl8380_sds_power(int mac, int val)
  117. {
  118. u32 mode = (val == 1) ? 0x4 : 0x9;
  119. u32 offset = (mac == 24) ? 5 : 0;
  120. if ((mac != 24) && (mac != 26)) {
  121. pr_err("%s: not a fibre port: %d\n", __func__, mac);
  122. return -1;
  123. }
  124. sw_w32_mask(0x1f << offset, mode << offset, RTL838X_SDS_MODE_SEL);
  125. rtl8380_sds_rst(mac);
  126. return 0;
  127. }
  128. static int __init rtl83xx_mdio_probe(struct rtl838x_switch_priv *priv)
  129. {
  130. struct device *dev = priv->dev;
  131. struct device_node *dn, *mii_np = dev->of_node;
  132. struct mii_bus *bus;
  133. int ret;
  134. u32 pn;
  135. pr_debug("In %s\n", __func__);
  136. mii_np = of_find_compatible_node(NULL, NULL, "realtek,rtl838x-mdio");
  137. if (mii_np) {
  138. pr_debug("Found compatible MDIO node!\n");
  139. } else {
  140. dev_err(priv->dev, "no %s child node found", "mdio-bus");
  141. return -ENODEV;
  142. }
  143. priv->mii_bus = of_mdio_find_bus(mii_np);
  144. if (!priv->mii_bus) {
  145. pr_debug("Deferring probe of mdio bus\n");
  146. return -EPROBE_DEFER;
  147. }
  148. if (!of_device_is_available(mii_np))
  149. ret = -ENODEV;
  150. bus = devm_mdiobus_alloc(priv->ds->dev);
  151. if (!bus)
  152. return -ENOMEM;
  153. bus->name = "rtl838x slave mii";
  154. bus->read = &rtl83xx_mdio_read;
  155. bus->write = &rtl83xx_mdio_write;
  156. snprintf(bus->id, MII_BUS_ID_SIZE, "%s-%d", bus->name, dev->id);
  157. bus->parent = dev;
  158. priv->ds->slave_mii_bus = bus;
  159. priv->ds->slave_mii_bus->priv = priv;
  160. ret = mdiobus_register(priv->ds->slave_mii_bus);
  161. if (ret && mii_np) {
  162. of_node_put(dn);
  163. return ret;
  164. }
  165. dn = mii_np;
  166. for_each_node_by_name(dn, "ethernet-phy") {
  167. if (of_property_read_u32(dn, "reg", &pn))
  168. continue;
  169. priv->ports[pn].dp = dsa_to_port(priv->ds, pn);
  170. // Check for the integrated SerDes of the RTL8380M first
  171. if (of_property_read_bool(dn, "phy-is-integrated")
  172. && priv->id == 0x8380 && pn >= 24) {
  173. pr_debug("----> FÓUND A SERDES\n");
  174. priv->ports[pn].phy = PHY_RTL838X_SDS;
  175. continue;
  176. }
  177. if (of_property_read_bool(dn, "phy-is-integrated")
  178. && !of_property_read_bool(dn, "sfp")) {
  179. priv->ports[pn].phy = PHY_RTL8218B_INT;
  180. continue;
  181. }
  182. if (!of_property_read_bool(dn, "phy-is-integrated")
  183. && of_property_read_bool(dn, "sfp")) {
  184. priv->ports[pn].phy = PHY_RTL8214FC;
  185. continue;
  186. }
  187. if (!of_property_read_bool(dn, "phy-is-integrated")
  188. && !of_property_read_bool(dn, "sfp")) {
  189. priv->ports[pn].phy = PHY_RTL8218B_EXT;
  190. continue;
  191. }
  192. }
  193. /* Disable MAC polling the PHY so that we can start configuration */
  194. priv->r->set_port_reg_le(0ULL, priv->r->smi_poll_ctrl);
  195. /* Enable PHY control via SoC */
  196. if (priv->family_id == RTL8380_FAMILY_ID) {
  197. /* Enable PHY control via SoC */
  198. sw_w32_mask(0, BIT(15), RTL838X_SMI_GLB_CTRL);
  199. } else {
  200. /* Disable PHY polling via SoC */
  201. sw_w32_mask(BIT(7), 0, RTL839X_SMI_GLB_CTRL);
  202. }
  203. /* Power on fibre ports and reset them if necessary */
  204. if (priv->ports[24].phy == PHY_RTL838X_SDS) {
  205. pr_debug("Powering on fibre ports & reset\n");
  206. rtl8380_sds_power(24, 1);
  207. rtl8380_sds_power(26, 1);
  208. }
  209. pr_debug("%s done\n", __func__);
  210. return 0;
  211. }
  212. static int __init rtl83xx_get_l2aging(struct rtl838x_switch_priv *priv)
  213. {
  214. int t = sw_r32(priv->r->l2_ctrl_1);
  215. t &= priv->family_id == RTL8380_FAMILY_ID ? 0x7fffff : 0x1FFFFF;
  216. if (priv->family_id == RTL8380_FAMILY_ID)
  217. t = t * 128 / 625; /* Aging time in seconds. 0: L2 aging disabled */
  218. else
  219. t = (t * 3) / 5;
  220. pr_debug("L2 AGING time: %d sec\n", t);
  221. pr_debug("Dynamic aging for ports: %x\n", sw_r32(priv->r->l2_port_aging_out));
  222. return t;
  223. }
  224. static int __init rtl83xx_sw_probe(struct platform_device *pdev)
  225. {
  226. int err = 0, i;
  227. struct rtl838x_switch_priv *priv;
  228. struct device *dev = &pdev->dev;
  229. u64 irq_mask;
  230. pr_debug("Probing RTL838X switch device\n");
  231. if (!pdev->dev.of_node) {
  232. dev_err(dev, "No DT found\n");
  233. return -EINVAL;
  234. }
  235. priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
  236. if (!priv)
  237. return -ENOMEM;
  238. priv->ds = dsa_switch_alloc(dev, DSA_MAX_PORTS);
  239. if (!priv->ds)
  240. return -ENOMEM;
  241. priv->ds->dev = dev;
  242. priv->ds->priv = priv;
  243. priv->ds->ops = &rtl83xx_switch_ops;
  244. priv->dev = dev;
  245. priv->family_id = soc_info.family;
  246. priv->id = soc_info.id;
  247. if (soc_info.family == RTL8380_FAMILY_ID) {
  248. priv->cpu_port = RTL838X_CPU_PORT;
  249. priv->port_mask = 0x1f;
  250. priv->r = &rtl838x_reg;
  251. priv->ds->num_ports = 30;
  252. priv->fib_entries = 8192;
  253. rtl8380_get_version(priv);
  254. } else {
  255. priv->cpu_port = RTL839X_CPU_PORT;
  256. priv->port_mask = 0x3f;
  257. priv->r = &rtl839x_reg;
  258. priv->ds->num_ports = 53;
  259. priv->fib_entries = 16384;
  260. rtl8390_get_version(priv);
  261. }
  262. pr_debug("Chip version %c\n", priv->version);
  263. err = rtl83xx_mdio_probe(priv);
  264. if (err) {
  265. /* Probing fails the 1st time because of missing ethernet driver
  266. * initialization. Use this to disable traffic in case the bootloader left if on
  267. */
  268. return err;
  269. }
  270. err = dsa_register_switch(priv->ds);
  271. if (err) {
  272. dev_err(dev, "Error registering switch: %d\n", err);
  273. return err;
  274. }
  275. /* Enable link and media change interrupts. Are the SERDES masks needed? */
  276. sw_w32_mask(0, 3, priv->r->isr_glb_src);
  277. /* ... for all ports */
  278. irq_mask = soc_info.family == RTL8380_FAMILY_ID ? 0x0FFFFFFF : 0xFFFFFFFFFFFFFULL;
  279. priv->r->set_port_reg_le(irq_mask, priv->r->isr_port_link_sts_chg);
  280. priv->r->set_port_reg_le(irq_mask, priv->r->imr_port_link_sts_chg);
  281. priv->link_state_irq = platform_get_irq(pdev, 0);;
  282. if (priv->family_id == RTL8380_FAMILY_ID) {
  283. err = request_irq(priv->link_state_irq, rtl838x_switch_irq,
  284. IRQF_SHARED, "rtl838x-link-state", priv->ds);
  285. } else {
  286. err = request_irq(priv->link_state_irq, rtl839x_switch_irq,
  287. IRQF_SHARED, "rtl839x-link-state", priv->ds);
  288. }
  289. if (err) {
  290. dev_err(dev, "Error setting up switch interrupt.\n");
  291. /* Need to free allocated switch here */
  292. }
  293. /* Enable interrupts for switch */
  294. sw_w32(0x1, priv->r->imr_glb);
  295. rtl83xx_get_l2aging(priv);
  296. /*
  297. if (priv->family_id == RTL8380_FAMILY_ID)
  298. rtl83xx_storm_control_init(priv);
  299. */
  300. /* Clear all destination ports for mirror groups */
  301. for (i = 0; i < 4; i++)
  302. priv->mirror_group_ports[i] = -1;
  303. rtl838x_dbgfs_init(priv);
  304. return err;
  305. }
  306. static int rtl83xx_sw_remove(struct platform_device *pdev)
  307. {
  308. // TODO:
  309. pr_debug("Removing platform driver for rtl83xx-sw\n");
  310. return 0;
  311. }
  312. static const struct of_device_id rtl83xx_switch_of_ids[] = {
  313. { .compatible = "realtek,rtl83xx-switch"},
  314. { /* sentinel */ }
  315. };
  316. MODULE_DEVICE_TABLE(of, rtl83xx_switch_of_ids);
  317. static struct platform_driver rtl83xx_switch_driver = {
  318. .probe = rtl83xx_sw_probe,
  319. .remove = rtl83xx_sw_remove,
  320. .driver = {
  321. .name = "rtl83xx-switch",
  322. .pm = NULL,
  323. .of_match_table = rtl83xx_switch_of_ids,
  324. },
  325. };
  326. module_platform_driver(rtl83xx_switch_driver);
  327. MODULE_AUTHOR("B. Koblitz");
  328. MODULE_DESCRIPTION("RTL83XX SoC Switch Driver");
  329. MODULE_LICENSE("GPL");