common.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722
  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 rtl838x_reg rtl930x_reg;
  10. extern const struct rtl838x_reg rtl931x_reg;
  11. extern const struct dsa_switch_ops rtl83xx_switch_ops;
  12. extern const struct dsa_switch_ops rtl930x_switch_ops;
  13. DEFINE_MUTEX(smi_lock);
  14. int rtl83xx_port_get_stp_state(struct rtl838x_switch_priv *priv, int port)
  15. {
  16. u32 msti = 0;
  17. u32 port_state[4];
  18. int index, bit;
  19. int pos = port;
  20. int n = priv->port_width << 1;
  21. /* Ports above or equal CPU port can never be configured */
  22. if (port >= priv->cpu_port)
  23. return -1;
  24. mutex_lock(&priv->reg_mutex);
  25. /* For the RTL839x and following, the bits are left-aligned in the 64/128 bit field */
  26. if (priv->family_id == RTL8390_FAMILY_ID)
  27. pos += 12;
  28. if (priv->family_id == RTL9300_FAMILY_ID)
  29. pos += 3;
  30. if (priv->family_id == RTL9310_FAMILY_ID)
  31. pos += 8;
  32. index = n - (pos >> 4) - 1;
  33. bit = (pos << 1) % 32;
  34. priv->r->stp_get(priv, msti, port_state);
  35. mutex_unlock(&priv->reg_mutex);
  36. return (port_state[index] >> bit) & 3;
  37. }
  38. static struct table_reg rtl838x_tbl_regs[] = {
  39. TBL_DESC(0x6900, 0x6908, 3, 15, 13, 1), // RTL8380_TBL_L2
  40. TBL_DESC(0x6914, 0x6918, 18, 14, 12, 1), // RTL8380_TBL_0
  41. TBL_DESC(0xA4C8, 0xA4CC, 6, 14, 12, 1), // RTL8380_TBL_1
  42. TBL_DESC(0x1180, 0x1184, 3, 16, 14, 0), // RTL8390_TBL_L2
  43. TBL_DESC(0x1190, 0x1194, 17, 15, 12, 0), // RTL8390_TBL_0
  44. TBL_DESC(0x6B80, 0x6B84, 4, 14, 12, 0), // RTL8390_TBL_1
  45. TBL_DESC(0x611C, 0x6120, 9, 8, 6, 0), // RTL8390_TBL_2
  46. TBL_DESC(0xB320, 0xB334, 3, 18, 16, 0), // RTL9300_TBL_L2
  47. TBL_DESC(0xB340, 0xB344, 19, 16, 12, 0), // RTL9300_TBL_0
  48. TBL_DESC(0xB3A0, 0xB3A4, 20, 16, 13, 0), // RTL9300_TBL_1
  49. TBL_DESC(0xCE04, 0xCE08, 6, 14, 12, 0), // RTL9300_TBL_2
  50. TBL_DESC(0xD600, 0xD604, 30, 7, 6, 0), // RTL9300_TBL_HSB
  51. TBL_DESC(0x7880, 0x7884, 22, 9, 8, 0), // RTL9300_TBL_HSA
  52. TBL_DESC(0x8500, 0x8508, 8, 19, 15, 0), // RTL9310_TBL_0
  53. TBL_DESC(0x40C0, 0x40C4, 22, 16, 14, 0), // RTL9310_TBL_1
  54. TBL_DESC(0x8528, 0x852C, 6, 18, 14, 0), // RTL9310_TBL_2
  55. TBL_DESC(0x0200, 0x0204, 9, 15, 12, 0), // RTL9310_TBL_3
  56. TBL_DESC(0x20dc, 0x20e0, 29, 7, 6, 0), // RTL9310_TBL_4
  57. TBL_DESC(0x7e1c, 0x7e20, 53, 8, 6, 0), // RTL9310_TBL_5
  58. };
  59. void rtl_table_init(void)
  60. {
  61. int i;
  62. for (i = 0; i < RTL_TBL_END; i++)
  63. mutex_init(&rtl838x_tbl_regs[i].lock);
  64. }
  65. /*
  66. * Request access to table t in table access register r
  67. * Returns a handle to a lock for that table
  68. */
  69. struct table_reg *rtl_table_get(rtl838x_tbl_reg_t r, int t)
  70. {
  71. if (r >= RTL_TBL_END)
  72. return NULL;
  73. if (t >= BIT(rtl838x_tbl_regs[r].c_bit-rtl838x_tbl_regs[r].t_bit))
  74. return NULL;
  75. mutex_lock(&rtl838x_tbl_regs[r].lock);
  76. rtl838x_tbl_regs[r].tbl = t;
  77. return &rtl838x_tbl_regs[r];
  78. }
  79. /*
  80. * Release a table r, unlock the corresponding lock
  81. */
  82. void rtl_table_release(struct table_reg *r)
  83. {
  84. if (!r)
  85. return;
  86. // pr_info("Unlocking %08x\n", (u32)r);
  87. mutex_unlock(&r->lock);
  88. // pr_info("Unlock done\n");
  89. }
  90. /*
  91. * Reads table index idx into the data registers of the table
  92. */
  93. void rtl_table_read(struct table_reg *r, int idx)
  94. {
  95. u32 cmd = r->rmode ? BIT(r->c_bit) : 0;
  96. cmd |= BIT(r->c_bit + 1) | (r->tbl << r->t_bit) | (idx & (BIT(r->t_bit) - 1));
  97. sw_w32(cmd, r->addr);
  98. do { } while (sw_r32(r->addr) & BIT(r->c_bit + 1));
  99. }
  100. /*
  101. * Writes the content of the table data registers into the table at index idx
  102. */
  103. void rtl_table_write(struct table_reg *r, int idx)
  104. {
  105. u32 cmd = r->rmode ? 0 : BIT(r->c_bit);
  106. cmd |= BIT(r->c_bit + 1) | (r->tbl << r->t_bit) | (idx & (BIT(r->t_bit) - 1));
  107. sw_w32(cmd, r->addr);
  108. do { } while (sw_r32(r->addr) & BIT(r->c_bit + 1));
  109. }
  110. /*
  111. * Returns the address of the ith data register of table register r
  112. * the address is relative to the beginning of the Switch-IO block at 0xbb000000
  113. */
  114. inline u16 rtl_table_data(struct table_reg *r, int i)
  115. {
  116. if (i >= r->max_data)
  117. i = r->max_data - 1;
  118. return r->data + i * 4;
  119. }
  120. inline u32 rtl_table_data_r(struct table_reg *r, int i)
  121. {
  122. return sw_r32(rtl_table_data(r, i));
  123. }
  124. inline void rtl_table_data_w(struct table_reg *r, u32 v, int i)
  125. {
  126. sw_w32(v, rtl_table_data(r, i));
  127. }
  128. /* Port register accessor functions for the RTL838x and RTL930X SoCs */
  129. void rtl838x_mask_port_reg(u64 clear, u64 set, int reg)
  130. {
  131. sw_w32_mask((u32)clear, (u32)set, reg);
  132. }
  133. void rtl838x_set_port_reg(u64 set, int reg)
  134. {
  135. sw_w32((u32)set, reg);
  136. }
  137. u64 rtl838x_get_port_reg(int reg)
  138. {
  139. return ((u64) sw_r32(reg));
  140. }
  141. /* Port register accessor functions for the RTL839x and RTL931X SoCs */
  142. void rtl839x_mask_port_reg_be(u64 clear, u64 set, int reg)
  143. {
  144. sw_w32_mask((u32)(clear >> 32), (u32)(set >> 32), reg);
  145. sw_w32_mask((u32)(clear & 0xffffffff), (u32)(set & 0xffffffff), reg + 4);
  146. }
  147. u64 rtl839x_get_port_reg_be(int reg)
  148. {
  149. u64 v = sw_r32(reg);
  150. v <<= 32;
  151. v |= sw_r32(reg + 4);
  152. return v;
  153. }
  154. void rtl839x_set_port_reg_be(u64 set, int reg)
  155. {
  156. sw_w32(set >> 32, reg);
  157. sw_w32(set & 0xffffffff, reg + 4);
  158. }
  159. void rtl839x_mask_port_reg_le(u64 clear, u64 set, int reg)
  160. {
  161. sw_w32_mask((u32)clear, (u32)set, reg);
  162. sw_w32_mask((u32)(clear >> 32), (u32)(set >> 32), reg + 4);
  163. }
  164. void rtl839x_set_port_reg_le(u64 set, int reg)
  165. {
  166. sw_w32(set, reg);
  167. sw_w32(set >> 32, reg + 4);
  168. }
  169. u64 rtl839x_get_port_reg_le(int reg)
  170. {
  171. u64 v = sw_r32(reg + 4);
  172. v <<= 32;
  173. v |= sw_r32(reg);
  174. return v;
  175. }
  176. int read_phy(u32 port, u32 page, u32 reg, u32 *val)
  177. {
  178. switch (soc_info.family) {
  179. case RTL8380_FAMILY_ID:
  180. return rtl838x_read_phy(port, page, reg, val);
  181. case RTL8390_FAMILY_ID:
  182. return rtl839x_read_phy(port, page, reg, val);
  183. case RTL9300_FAMILY_ID:
  184. return rtl930x_read_phy(port, page, reg, val);
  185. case RTL9310_FAMILY_ID:
  186. return rtl931x_read_phy(port, page, reg, val);
  187. }
  188. return -1;
  189. }
  190. int write_phy(u32 port, u32 page, u32 reg, u32 val)
  191. {
  192. switch (soc_info.family) {
  193. case RTL8380_FAMILY_ID:
  194. return rtl838x_write_phy(port, page, reg, val);
  195. case RTL8390_FAMILY_ID:
  196. return rtl839x_write_phy(port, page, reg, val);
  197. case RTL9300_FAMILY_ID:
  198. return rtl930x_write_phy(port, page, reg, val);
  199. case RTL9310_FAMILY_ID:
  200. return rtl931x_write_phy(port, page, reg, val);
  201. }
  202. return -1;
  203. }
  204. static int __init rtl83xx_mdio_probe(struct rtl838x_switch_priv *priv)
  205. {
  206. struct device *dev = priv->dev;
  207. struct device_node *dn, *mii_np = dev->of_node;
  208. struct mii_bus *bus;
  209. int ret;
  210. u32 pn;
  211. pr_debug("In %s\n", __func__);
  212. mii_np = of_find_compatible_node(NULL, NULL, "realtek,rtl838x-mdio");
  213. if (mii_np) {
  214. pr_debug("Found compatible MDIO node!\n");
  215. } else {
  216. dev_err(priv->dev, "no %s child node found", "mdio-bus");
  217. return -ENODEV;
  218. }
  219. priv->mii_bus = of_mdio_find_bus(mii_np);
  220. if (!priv->mii_bus) {
  221. pr_debug("Deferring probe of mdio bus\n");
  222. return -EPROBE_DEFER;
  223. }
  224. if (!of_device_is_available(mii_np))
  225. ret = -ENODEV;
  226. bus = devm_mdiobus_alloc(priv->ds->dev);
  227. if (!bus)
  228. return -ENOMEM;
  229. bus->name = "rtl838x slave mii";
  230. /*
  231. * Since the NIC driver is loaded first, we can use the mdio rw functions
  232. * assigned there.
  233. */
  234. bus->read = priv->mii_bus->read;
  235. bus->write = priv->mii_bus->write;
  236. snprintf(bus->id, MII_BUS_ID_SIZE, "%s-%d", bus->name, dev->id);
  237. bus->parent = dev;
  238. priv->ds->slave_mii_bus = bus;
  239. priv->ds->slave_mii_bus->priv = priv;
  240. ret = mdiobus_register(priv->ds->slave_mii_bus);
  241. if (ret && mii_np) {
  242. of_node_put(dn);
  243. return ret;
  244. }
  245. dn = mii_np;
  246. for_each_node_by_name(dn, "ethernet-phy") {
  247. if (of_property_read_u32(dn, "reg", &pn))
  248. continue;
  249. priv->ports[pn].dp = dsa_to_port(priv->ds, pn);
  250. // Check for the integrated SerDes of the RTL8380M first
  251. if (of_property_read_bool(dn, "phy-is-integrated")
  252. && priv->id == 0x8380 && pn >= 24) {
  253. pr_debug("----> FÓUND A SERDES\n");
  254. priv->ports[pn].phy = PHY_RTL838X_SDS;
  255. continue;
  256. }
  257. if (of_property_read_bool(dn, "phy-is-integrated")
  258. && !of_property_read_bool(dn, "sfp")) {
  259. priv->ports[pn].phy = PHY_RTL8218B_INT;
  260. continue;
  261. }
  262. if (!of_property_read_bool(dn, "phy-is-integrated")
  263. && of_property_read_bool(dn, "sfp")) {
  264. priv->ports[pn].phy = PHY_RTL8214FC;
  265. continue;
  266. }
  267. if (!of_property_read_bool(dn, "phy-is-integrated")
  268. && !of_property_read_bool(dn, "sfp")) {
  269. priv->ports[pn].phy = PHY_RTL8218B_EXT;
  270. continue;
  271. }
  272. }
  273. // TODO: Do this needs to come from the .dts, at least the SerDes number
  274. if (priv->family_id == RTL9300_FAMILY_ID) {
  275. priv->ports[24].is2G5 = true;
  276. priv->ports[25].is2G5 = true;
  277. priv->ports[24].sds_num = 1;
  278. priv->ports[24].sds_num = 2;
  279. }
  280. /* Disable MAC polling the PHY so that we can start configuration */
  281. priv->r->set_port_reg_le(0ULL, priv->r->smi_poll_ctrl);
  282. /* Enable PHY control via SoC */
  283. if (priv->family_id == RTL8380_FAMILY_ID) {
  284. /* Enable SerDes NWAY and PHY control via SoC */
  285. sw_w32_mask(BIT(7), BIT(15), RTL838X_SMI_GLB_CTRL);
  286. } else {
  287. /* Disable PHY polling via SoC */
  288. sw_w32_mask(BIT(7), 0, RTL839X_SMI_GLB_CTRL);
  289. }
  290. /* Power on fibre ports and reset them if necessary */
  291. if (priv->ports[24].phy == PHY_RTL838X_SDS) {
  292. pr_debug("Powering on fibre ports & reset\n");
  293. rtl8380_sds_power(24, 1);
  294. rtl8380_sds_power(26, 1);
  295. }
  296. // TODO: Only power on SerDes with external PHYs connected
  297. if (priv->family_id == RTL9300_FAMILY_ID) {
  298. pr_info("RTL9300 Powering on SerDes ports\n");
  299. rtl9300_sds_power(24, 1);
  300. rtl9300_sds_power(25, 1);
  301. rtl9300_sds_power(26, 1);
  302. rtl9300_sds_power(27, 1);
  303. }
  304. pr_debug("%s done\n", __func__);
  305. return 0;
  306. }
  307. static int __init rtl83xx_get_l2aging(struct rtl838x_switch_priv *priv)
  308. {
  309. int t = sw_r32(priv->r->l2_ctrl_1);
  310. t &= priv->family_id == RTL8380_FAMILY_ID ? 0x7fffff : 0x1FFFFF;
  311. if (priv->family_id == RTL8380_FAMILY_ID)
  312. t = t * 128 / 625; /* Aging time in seconds. 0: L2 aging disabled */
  313. else
  314. t = (t * 3) / 5;
  315. pr_debug("L2 AGING time: %d sec\n", t);
  316. pr_debug("Dynamic aging for ports: %x\n", sw_r32(priv->r->l2_port_aging_out));
  317. return t;
  318. }
  319. /* Caller must hold priv->reg_mutex */
  320. int rtl83xx_lag_add(struct dsa_switch *ds, int group, int port)
  321. {
  322. struct rtl838x_switch_priv *priv = ds->priv;
  323. int i;
  324. pr_info("%s: Adding port %d to LA-group %d\n", __func__, port, group);
  325. if (group >= priv->n_lags) {
  326. pr_err("Link Agrregation group too large.\n");
  327. return -EINVAL;
  328. }
  329. if (port >= priv->cpu_port) {
  330. pr_err("Invalid port number.\n");
  331. return -EINVAL;
  332. }
  333. for (i = 0; i < priv->n_lags; i++) {
  334. if (priv->lags_port_members[i] & BIT_ULL(i))
  335. break;
  336. }
  337. if (i != priv->n_lags) {
  338. pr_err("%s: Port already member of LAG: %d\n", __func__, i);
  339. return -ENOSPC;
  340. }
  341. priv->r->mask_port_reg_be(0, BIT_ULL(port), priv->r->trk_mbr_ctr(group));
  342. priv->lags_port_members[group] |= BIT_ULL(port);
  343. pr_info("lags_port_members %d now %016llx\n", group, priv->lags_port_members[group]);
  344. return 0;
  345. }
  346. /* Caller must hold priv->reg_mutex */
  347. int rtl83xx_lag_del(struct dsa_switch *ds, int group, int port)
  348. {
  349. struct rtl838x_switch_priv *priv = ds->priv;
  350. pr_info("%s: Removing port %d from LA-group %d\n", __func__, port, group);
  351. if (group >= priv->n_lags) {
  352. pr_err("Link Agrregation group too large.\n");
  353. return -EINVAL;
  354. }
  355. if (port >= priv->cpu_port) {
  356. pr_err("Invalid port number.\n");
  357. return -EINVAL;
  358. }
  359. if (!(priv->lags_port_members[group] & BIT_ULL(port))) {
  360. pr_err("%s: Port not member of LAG: %d\n", __func__, group
  361. );
  362. return -ENOSPC;
  363. }
  364. priv->r->mask_port_reg_be(BIT_ULL(port), 0, priv->r->trk_mbr_ctr(group));
  365. priv->lags_port_members[group] &= ~BIT_ULL(port);
  366. pr_info("lags_port_members %d now %016llx\n", group, priv->lags_port_members[group]);
  367. return 0;
  368. }
  369. static int rtl83xx_handle_changeupper(struct rtl838x_switch_priv *priv,
  370. struct net_device *ndev,
  371. struct netdev_notifier_changeupper_info *info)
  372. {
  373. struct net_device *upper = info->upper_dev;
  374. int i, j, err;
  375. if (!netif_is_lag_master(upper))
  376. return 0;
  377. mutex_lock(&priv->reg_mutex);
  378. for (i = 0; i < priv->n_lags; i++) {
  379. if ((!priv->lag_devs[i]) || (priv->lag_devs[i] == upper))
  380. break;
  381. }
  382. for (j = 0; j < priv->cpu_port; j++) {
  383. if (priv->ports[j].dp->slave == ndev)
  384. break;
  385. }
  386. if (j >= priv->cpu_port) {
  387. err = -EINVAL;
  388. goto out;
  389. }
  390. if (info->linking) {
  391. if (!priv->lag_devs[i])
  392. priv->lag_devs[i] = upper;
  393. err = rtl83xx_lag_add(priv->ds, i, priv->ports[j].dp->index);
  394. if (err) {
  395. err = -EINVAL;
  396. goto out;
  397. }
  398. } else {
  399. if (!priv->lag_devs[i])
  400. err = -EINVAL;
  401. err = rtl83xx_lag_del(priv->ds, i, priv->ports[j].dp->index);
  402. if (err) {
  403. err = -EINVAL;
  404. goto out;
  405. }
  406. if (!priv->lags_port_members[i])
  407. priv->lag_devs[i] = NULL;
  408. }
  409. out:
  410. mutex_unlock(&priv->reg_mutex);
  411. return 0;
  412. }
  413. static int rtl83xx_netdevice_event(struct notifier_block *this,
  414. unsigned long event, void *ptr)
  415. {
  416. struct net_device *ndev = netdev_notifier_info_to_dev(ptr);
  417. struct rtl838x_switch_priv *priv;
  418. int err;
  419. pr_debug("In: %s, event: %lu\n", __func__, event);
  420. if ((event != NETDEV_CHANGEUPPER) && (event != NETDEV_CHANGELOWERSTATE))
  421. return NOTIFY_DONE;
  422. priv = container_of(this, struct rtl838x_switch_priv, nb);
  423. switch (event) {
  424. case NETDEV_CHANGEUPPER:
  425. err = rtl83xx_handle_changeupper(priv, ndev, ptr);
  426. break;
  427. }
  428. if (err)
  429. return err;
  430. return NOTIFY_DONE;
  431. }
  432. static int __init rtl83xx_sw_probe(struct platform_device *pdev)
  433. {
  434. int err = 0, i;
  435. struct rtl838x_switch_priv *priv;
  436. struct device *dev = &pdev->dev;
  437. u64 bpdu_mask;
  438. pr_debug("Probing RTL838X switch device\n");
  439. if (!pdev->dev.of_node) {
  440. dev_err(dev, "No DT found\n");
  441. return -EINVAL;
  442. }
  443. // Initialize access to RTL switch tables
  444. rtl_table_init();
  445. priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
  446. if (!priv)
  447. return -ENOMEM;
  448. priv->ds = dsa_switch_alloc(dev, DSA_MAX_PORTS);
  449. if (!priv->ds)
  450. return -ENOMEM;
  451. priv->ds->dev = dev;
  452. priv->ds->priv = priv;
  453. priv->ds->ops = &rtl83xx_switch_ops;
  454. priv->dev = dev;
  455. priv->family_id = soc_info.family;
  456. priv->id = soc_info.id;
  457. switch(soc_info.family) {
  458. case RTL8380_FAMILY_ID:
  459. priv->ds->ops = &rtl83xx_switch_ops;
  460. priv->cpu_port = RTL838X_CPU_PORT;
  461. priv->port_mask = 0x1f;
  462. priv->port_width = 1;
  463. priv->irq_mask = 0x0FFFFFFF;
  464. priv->r = &rtl838x_reg;
  465. priv->ds->num_ports = 29;
  466. priv->fib_entries = 8192;
  467. rtl8380_get_version(priv);
  468. priv->n_lags = 8;
  469. priv->l2_bucket_size = 4;
  470. break;
  471. case RTL8390_FAMILY_ID:
  472. priv->ds->ops = &rtl83xx_switch_ops;
  473. priv->cpu_port = RTL839X_CPU_PORT;
  474. priv->port_mask = 0x3f;
  475. priv->port_width = 2;
  476. priv->irq_mask = 0xFFFFFFFFFFFFFULL;
  477. priv->r = &rtl839x_reg;
  478. priv->ds->num_ports = 53;
  479. priv->fib_entries = 16384;
  480. rtl8390_get_version(priv);
  481. priv->n_lags = 16;
  482. priv->l2_bucket_size = 4;
  483. break;
  484. case RTL9300_FAMILY_ID:
  485. priv->ds->ops = &rtl930x_switch_ops;
  486. priv->cpu_port = RTL930X_CPU_PORT;
  487. priv->port_mask = 0x1f;
  488. priv->port_width = 1;
  489. priv->irq_mask = 0x0FFFFFFF;
  490. priv->r = &rtl930x_reg;
  491. priv->ds->num_ports = 29;
  492. priv->fib_entries = 16384;
  493. priv->version = RTL8390_VERSION_A;
  494. priv->n_lags = 16;
  495. sw_w32(1, RTL930X_ST_CTRL);
  496. priv->l2_bucket_size = 8;
  497. break;
  498. case RTL9310_FAMILY_ID:
  499. priv->ds->ops = &rtl930x_switch_ops;
  500. priv->cpu_port = RTL931X_CPU_PORT;
  501. priv->port_mask = 0x3f;
  502. priv->port_width = 2;
  503. priv->irq_mask = 0xFFFFFFFFFFFFFULL;
  504. priv->r = &rtl931x_reg;
  505. priv->ds->num_ports = 57;
  506. priv->fib_entries = 16384;
  507. priv->version = RTL8390_VERSION_A;
  508. priv->n_lags = 16;
  509. priv->l2_bucket_size = 8;
  510. break;
  511. }
  512. pr_debug("Chip version %c\n", priv->version);
  513. err = rtl83xx_mdio_probe(priv);
  514. if (err) {
  515. /* Probing fails the 1st time because of missing ethernet driver
  516. * initialization. Use this to disable traffic in case the bootloader left if on
  517. */
  518. return err;
  519. }
  520. err = dsa_register_switch(priv->ds);
  521. if (err) {
  522. dev_err(dev, "Error registering switch: %d\n", err);
  523. return err;
  524. }
  525. /* Enable link and media change interrupts. Are the SERDES masks needed? */
  526. sw_w32_mask(0, 3, priv->r->isr_glb_src);
  527. priv->r->set_port_reg_le(priv->irq_mask, priv->r->isr_port_link_sts_chg);
  528. priv->r->set_port_reg_le(priv->irq_mask, priv->r->imr_port_link_sts_chg);
  529. priv->link_state_irq = platform_get_irq(pdev, 0);
  530. pr_info("LINK state irq: %d\n", priv->link_state_irq);
  531. switch (priv->family_id) {
  532. case RTL8380_FAMILY_ID:
  533. err = request_irq(priv->link_state_irq, rtl838x_switch_irq,
  534. IRQF_SHARED, "rtl838x-link-state", priv->ds);
  535. break;
  536. case RTL8390_FAMILY_ID:
  537. err = request_irq(priv->link_state_irq, rtl839x_switch_irq,
  538. IRQF_SHARED, "rtl839x-link-state", priv->ds);
  539. break;
  540. case RTL9300_FAMILY_ID:
  541. err = request_irq(priv->link_state_irq, rtl930x_switch_irq,
  542. IRQF_SHARED, "rtl930x-link-state", priv->ds);
  543. break;
  544. case RTL9310_FAMILY_ID:
  545. err = request_irq(priv->link_state_irq, rtl931x_switch_irq,
  546. IRQF_SHARED, "rtl931x-link-state", priv->ds);
  547. break;
  548. }
  549. if (err) {
  550. dev_err(dev, "Error setting up switch interrupt.\n");
  551. /* Need to free allocated switch here */
  552. }
  553. /* Enable interrupts for switch, on RTL931x, the IRQ is always on globally */
  554. if (soc_info.family != RTL9310_FAMILY_ID)
  555. sw_w32(0x1, priv->r->imr_glb);
  556. rtl83xx_get_l2aging(priv);
  557. rtl83xx_setup_qos(priv);
  558. /* Clear all destination ports for mirror groups */
  559. for (i = 0; i < 4; i++)
  560. priv->mirror_group_ports[i] = -1;
  561. priv->nb.notifier_call = rtl83xx_netdevice_event;
  562. if (register_netdevice_notifier(&priv->nb)) {
  563. priv->nb.notifier_call = NULL;
  564. dev_err(dev, "Failed to register LAG netdev notifier\n");
  565. }
  566. // Flood BPDUs to all ports including cpu-port
  567. if (soc_info.family != RTL9300_FAMILY_ID) { // TODO: Port this functionality
  568. bpdu_mask = soc_info.family == RTL8380_FAMILY_ID ? 0x1FFFFFFF : 0x1FFFFFFFFFFFFF;
  569. priv->r->set_port_reg_be(bpdu_mask, priv->r->rma_bpdu_fld_pmask);
  570. // TRAP 802.1X frames (EAPOL) to the CPU-Port, bypass STP and VLANs
  571. sw_w32(7, priv->r->spcl_trap_eapol_ctrl);
  572. rtl838x_dbgfs_init(priv);
  573. }
  574. return err;
  575. }
  576. static int rtl83xx_sw_remove(struct platform_device *pdev)
  577. {
  578. // TODO:
  579. pr_debug("Removing platform driver for rtl83xx-sw\n");
  580. return 0;
  581. }
  582. static const struct of_device_id rtl83xx_switch_of_ids[] = {
  583. { .compatible = "realtek,rtl83xx-switch"},
  584. { /* sentinel */ }
  585. };
  586. MODULE_DEVICE_TABLE(of, rtl83xx_switch_of_ids);
  587. static struct platform_driver rtl83xx_switch_driver = {
  588. .probe = rtl83xx_sw_probe,
  589. .remove = rtl83xx_sw_remove,
  590. .driver = {
  591. .name = "rtl83xx-switch",
  592. .pm = NULL,
  593. .of_match_table = rtl83xx_switch_of_ids,
  594. },
  595. };
  596. module_platform_driver(rtl83xx_switch_driver);
  597. MODULE_AUTHOR("B. Koblitz");
  598. MODULE_DESCRIPTION("RTL83XX SoC Switch Driver");
  599. MODULE_LICENSE("GPL");