common.c 20 KB

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