rtl838x.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. #include <asm/mach-rtl838x/mach-rtl83xx.h>
  3. #include "rtl83xx.h"
  4. extern struct mutex smi_lock;
  5. void rtl838x_print_matrix(void)
  6. {
  7. unsigned volatile int *ptr8;
  8. int i;
  9. ptr8 = RTL838X_SW_BASE + RTL838X_PORT_ISO_CTRL(0);
  10. for (i = 0; i < 28; i += 8)
  11. pr_debug("> %8x %8x %8x %8x %8x %8x %8x %8x\n",
  12. ptr8[i + 0], ptr8[i + 1], ptr8[i + 2], ptr8[i + 3],
  13. ptr8[i + 4], ptr8[i + 5], ptr8[i + 6], ptr8[i + 7]);
  14. pr_debug("CPU_PORT> %8x\n", ptr8[28]);
  15. }
  16. static inline int rtl838x_port_iso_ctrl(int p)
  17. {
  18. return RTL838X_PORT_ISO_CTRL(p);
  19. }
  20. static inline void rtl838x_exec_tbl0_cmd(u32 cmd)
  21. {
  22. sw_w32(cmd, RTL838X_TBL_ACCESS_CTRL_0);
  23. do { } while (sw_r32(RTL838X_TBL_ACCESS_CTRL_0) & BIT(15));
  24. }
  25. static inline void rtl838x_exec_tbl1_cmd(u32 cmd)
  26. {
  27. sw_w32(cmd, RTL838X_TBL_ACCESS_CTRL_1);
  28. do { } while (sw_r32(RTL838X_TBL_ACCESS_CTRL_1) & BIT(15));
  29. }
  30. static inline int rtl838x_tbl_access_data_0(int i)
  31. {
  32. return RTL838X_TBL_ACCESS_DATA_0(i);
  33. }
  34. static void rtl838x_vlan_tables_read(u32 vlan, struct rtl838x_vlan_info *info)
  35. {
  36. u32 v;
  37. // Read VLAN table (0) via register 0
  38. struct table_reg *r = rtl_table_get(RTL8380_TBL_0, 0);
  39. rtl_table_read(r, vlan);
  40. info->tagged_ports = sw_r32(rtl_table_data(r, 0));
  41. v = sw_r32(rtl_table_data(r, 1));
  42. pr_debug("VLAN_READ %d: %016llx %08x\n", vlan, info->tagged_ports, v);
  43. rtl_table_release(r);
  44. info->profile_id = v & 0x7;
  45. info->hash_mc_fid = !!(v & 0x8);
  46. info->hash_uc_fid = !!(v & 0x10);
  47. info->fid = (v >> 5) & 0x3f;
  48. // Read UNTAG table (0) via table register 1
  49. r = rtl_table_get(RTL8380_TBL_1, 0);
  50. rtl_table_read(r, vlan);
  51. info->untagged_ports = sw_r32(rtl_table_data(r, 0));
  52. rtl_table_release(r);
  53. }
  54. static void rtl838x_vlan_set_tagged(u32 vlan, struct rtl838x_vlan_info *info)
  55. {
  56. u32 v;
  57. // Access VLAN table (0) via register 0
  58. struct table_reg *r = rtl_table_get(RTL8380_TBL_0, 0);
  59. sw_w32(info->tagged_ports, rtl_table_data(r, 0));
  60. v = info->profile_id;
  61. v |= info->hash_mc_fid ? 0x8 : 0;
  62. v |= info->hash_uc_fid ? 0x10 : 0;
  63. v |= ((u32)info->fid) << 5;
  64. sw_w32(v, rtl_table_data(r, 1));
  65. rtl_table_write(r, vlan);
  66. rtl_table_release(r);
  67. }
  68. static void rtl838x_vlan_set_untagged(u32 vlan, u64 portmask)
  69. {
  70. // Access UNTAG table (0) via register 1
  71. struct table_reg *r = rtl_table_get(RTL8380_TBL_1, 0);
  72. sw_w32(portmask & 0x1fffffff, rtl_table_data(r, 0));
  73. rtl_table_write(r, vlan);
  74. rtl_table_release(r);
  75. }
  76. /* Sets the L2 forwarding to be based on either the inner VLAN tag or the outer
  77. */
  78. static void rtl838x_vlan_fwd_on_inner(int port, bool is_set)
  79. {
  80. if (is_set)
  81. sw_w32_mask(BIT(port), 0, RTL838X_VLAN_PORT_FWD);
  82. else
  83. sw_w32_mask(0, BIT(port), RTL838X_VLAN_PORT_FWD);
  84. }
  85. static u64 rtl838x_l2_hash_seed(u64 mac, u32 vid)
  86. {
  87. return mac << 12 | vid;
  88. }
  89. /*
  90. * Applies the same hash algorithm as the one used currently by the ASIC to the seed
  91. * and returns a key into the L2 hash table
  92. */
  93. static u32 rtl838x_l2_hash_key(struct rtl838x_switch_priv *priv, u64 seed)
  94. {
  95. u32 h1, h2, h3, h;
  96. if (sw_r32(priv->r->l2_ctrl_0) & 1) {
  97. h1 = (seed >> 11) & 0x7ff;
  98. h1 = ((h1 & 0x1f) << 6) | ((h1 >> 5) & 0x3f);
  99. h2 = (seed >> 33) & 0x7ff;
  100. h2 = ((h2 & 0x3f) << 5) | ((h2 >> 6) & 0x1f);
  101. h3 = (seed >> 44) & 0x7ff;
  102. h3 = ((h3 & 0x7f) << 4) | ((h3 >> 7) & 0xf);
  103. h = h1 ^ h2 ^ h3 ^ ((seed >> 55) & 0x1ff);
  104. h ^= ((seed >> 22) & 0x7ff) ^ (seed & 0x7ff);
  105. } else {
  106. h = ((seed >> 55) & 0x1ff) ^ ((seed >> 44) & 0x7ff)
  107. ^ ((seed >> 33) & 0x7ff) ^ ((seed >> 22) & 0x7ff)
  108. ^ ((seed >> 11) & 0x7ff) ^ (seed & 0x7ff);
  109. }
  110. return h;
  111. }
  112. static inline int rtl838x_mac_force_mode_ctrl(int p)
  113. {
  114. return RTL838X_MAC_FORCE_MODE_CTRL + (p << 2);
  115. }
  116. static inline int rtl838x_mac_port_ctrl(int p)
  117. {
  118. return RTL838X_MAC_PORT_CTRL(p);
  119. }
  120. static inline int rtl838x_l2_port_new_salrn(int p)
  121. {
  122. return RTL838X_L2_PORT_NEW_SALRN(p);
  123. }
  124. static inline int rtl838x_l2_port_new_sa_fwd(int p)
  125. {
  126. return RTL838X_L2_PORT_NEW_SA_FWD(p);
  127. }
  128. static inline int rtl838x_mac_link_spd_sts(int p)
  129. {
  130. return RTL838X_MAC_LINK_SPD_STS(p);
  131. }
  132. inline static int rtl838x_trk_mbr_ctr(int group)
  133. {
  134. return RTL838X_TRK_MBR_CTR + (group << 2);
  135. }
  136. /*
  137. * Fills an L2 entry structure from the SoC registers
  138. */
  139. static void rtl838x_fill_l2_entry(u32 r[], struct rtl838x_l2_entry *e)
  140. {
  141. /* Table contains different entry types, we need to identify the right one:
  142. * Check for MC entries, first
  143. * In contrast to the RTL93xx SoCs, there is no valid bit, use heuristics to
  144. * identify valid entries
  145. */
  146. e->is_ip_mc = !!(r[0] & BIT(22));
  147. e->is_ipv6_mc = !!(r[0] & BIT(21));
  148. e->type = L2_INVALID;
  149. if (!e->is_ip_mc && !e->is_ipv6_mc) {
  150. e->mac[0] = (r[1] >> 20);
  151. e->mac[1] = (r[1] >> 12);
  152. e->mac[2] = (r[1] >> 4);
  153. e->mac[3] = (r[1] & 0xf) << 4 | (r[2] >> 28);
  154. e->mac[4] = (r[2] >> 20);
  155. e->mac[5] = (r[2] >> 12);
  156. e->rvid = r[2] & 0xfff;
  157. e->vid = r[0] & 0xfff;
  158. /* Is it a unicast entry? check multicast bit */
  159. if (!(e->mac[0] & 1)) {
  160. e->is_static = !!((r[0] >> 19) & 1);
  161. e->port = (r[0] >> 12) & 0x1f;
  162. e->block_da = !!(r[1] & BIT(30));
  163. e->block_sa = !!(r[1] & BIT(31));
  164. e->suspended = !!(r[1] & BIT(29));
  165. e->next_hop = !!(r[1] & BIT(28));
  166. if (e->next_hop) {
  167. pr_info("Found next hop entry, need to read extra data\n");
  168. e->nh_vlan_target = !!(r[0] & BIT(9));
  169. e->nh_route_id = r[0] & 0x1ff;
  170. }
  171. e->age = (r[0] >> 17) & 0x3;
  172. e->valid = true;
  173. /* A valid entry has one of mutli-cast, aging, sa/da-blocking,
  174. * next-hop or static entry bit set */
  175. if (!(r[0] & 0x007c0000) && !(r[1] & 0xd0000000))
  176. e->valid = false;
  177. else
  178. e->type = L2_UNICAST;
  179. } else { // L2 multicast
  180. pr_info("Got L2 MC entry: %08x %08x %08x\n", r[0], r[1], r[2]);
  181. e->valid = true;
  182. e->type = L2_MULTICAST;
  183. e->mc_portmask_index = (r[0] >> 12) & 0x1ff;
  184. }
  185. } else { // IPv4 and IPv6 multicast
  186. e->valid = true;
  187. e->mc_portmask_index = (r[0] >> 12) & 0x1ff;
  188. e->mc_gip = r[1];
  189. e->mc_sip = r[2];
  190. e->rvid = r[0] & 0xfff;
  191. }
  192. if (e->is_ip_mc)
  193. e->type = IP4_MULTICAST;
  194. if (e->is_ipv6_mc)
  195. e->type = IP6_MULTICAST;
  196. }
  197. /*
  198. * Fills the 3 SoC table registers r[] with the information of in the rtl838x_l2_entry
  199. */
  200. static void rtl838x_fill_l2_row(u32 r[], struct rtl838x_l2_entry *e)
  201. {
  202. u64 mac = ether_addr_to_u64(e->mac);
  203. if (!e->valid) {
  204. r[0] = r[1] = r[2] = 0;
  205. return;
  206. }
  207. r[0] = e->is_ip_mc ? BIT(22) : 0;
  208. r[0] |= e->is_ipv6_mc ? BIT(21) : 0;
  209. if (!e->is_ip_mc && !e->is_ipv6_mc) {
  210. r[1] = mac >> 20;
  211. r[2] = (mac & 0xfffff) << 12;
  212. /* Is it a unicast entry? check multicast bit */
  213. if (!(e->mac[0] & 1)) {
  214. r[0] |= e->is_static ? BIT(19) : 0;
  215. r[0] |= (e->port & 0x3f) << 12;
  216. r[0] |= e->vid;
  217. r[1] |= e->block_da ? BIT(30) : 0;
  218. r[1] |= e->block_sa ? BIT(31) : 0;
  219. r[1] |= e->suspended ? BIT(29) : 0;
  220. r[2] |= e->rvid & 0xfff;
  221. if (e->next_hop) {
  222. r[1] |= BIT(28);
  223. r[0] |= e->nh_vlan_target ? BIT(9) : 0;
  224. r[0] |= e->nh_route_id &0x1ff;
  225. }
  226. r[0] |= (e->age & 0x3) << 17;
  227. } else { // L2 Multicast
  228. r[0] |= (e->mc_portmask_index & 0x1ff) << 12;
  229. r[2] |= e->rvid & 0xfff;
  230. r[0] |= e->vid & 0xfff;
  231. pr_info("FILL MC: %08x %08x %08x\n", r[0], r[1], r[2]);
  232. }
  233. } else { // IPv4 and IPv6 multicast
  234. r[1] = e->mc_gip;
  235. r[2] = e->mc_sip;
  236. r[0] |= e->rvid;
  237. }
  238. }
  239. /*
  240. * Read an L2 UC or MC entry out of a hash bucket of the L2 forwarding table
  241. * hash is the id of the bucket and pos is the position of the entry in that bucket
  242. * The data read from the SoC is filled into rtl838x_l2_entry
  243. */
  244. static u64 rtl838x_read_l2_entry_using_hash(u32 hash, u32 pos, struct rtl838x_l2_entry *e)
  245. {
  246. u64 entry;
  247. u32 r[3];
  248. struct table_reg *q = rtl_table_get(RTL8380_TBL_L2, 0); // Access L2 Table 0
  249. u32 idx = (0 << 14) | (hash << 2) | pos; // Search SRAM, with hash and at pos in bucket
  250. int i;
  251. rtl_table_read(q, idx);
  252. for (i= 0; i < 3; i++)
  253. r[i] = sw_r32(rtl_table_data(q, i));
  254. rtl_table_release(q);
  255. rtl838x_fill_l2_entry(r, e);
  256. if (!e->valid)
  257. return 0;
  258. entry = (((u64) r[1]) << 32) | (r[2] & 0xfffff000) | (r[0] & 0xfff);
  259. return entry;
  260. }
  261. static void rtl838x_write_l2_entry_using_hash(u32 hash, u32 pos, struct rtl838x_l2_entry *e)
  262. {
  263. u32 r[3];
  264. struct table_reg *q = rtl_table_get(RTL8380_TBL_L2, 0);
  265. int i;
  266. u32 idx = (0 << 14) | (hash << 2) | pos; // Access SRAM, with hash and at pos in bucket
  267. rtl838x_fill_l2_row(r, e);
  268. for (i= 0; i < 3; i++)
  269. sw_w32(r[i], rtl_table_data(q, i));
  270. rtl_table_write(q, idx);
  271. rtl_table_release(q);
  272. }
  273. static u64 rtl838x_read_cam(int idx, struct rtl838x_l2_entry *e)
  274. {
  275. u64 entry;
  276. u32 r[3];
  277. struct table_reg *q = rtl_table_get(RTL8380_TBL_L2, 1); // Access L2 Table 1
  278. int i;
  279. rtl_table_read(q, idx);
  280. for (i= 0; i < 3; i++)
  281. r[i] = sw_r32(rtl_table_data(q, i));
  282. rtl_table_release(q);
  283. rtl838x_fill_l2_entry(r, e);
  284. if (!e->valid)
  285. return 0;
  286. pr_debug("Found in CAM: R1 %x R2 %x R3 %x\n", r[0], r[1], r[2]);
  287. // Return MAC with concatenated VID ac concatenated ID
  288. entry = (((u64) r[1]) << 32) | (r[2] & 0xfffff000) | (r[0] & 0xfff);
  289. return entry;
  290. }
  291. static void rtl838x_write_cam(int idx, struct rtl838x_l2_entry *e)
  292. {
  293. u32 r[3];
  294. struct table_reg *q = rtl_table_get(RTL8380_TBL_L2, 1); // Access L2 Table 1
  295. int i;
  296. rtl838x_fill_l2_row(r, e);
  297. for (i= 0; i < 3; i++)
  298. sw_w32(r[i], rtl_table_data(q, i));
  299. rtl_table_write(q, idx);
  300. rtl_table_release(q);
  301. }
  302. static u64 rtl838x_read_mcast_pmask(int idx)
  303. {
  304. u32 portmask;
  305. // Read MC_PMSK (2) via register RTL8380_TBL_L2
  306. struct table_reg *q = rtl_table_get(RTL8380_TBL_L2, 2);
  307. rtl_table_read(q, idx);
  308. portmask = sw_r32(rtl_table_data(q, 0));
  309. rtl_table_release(q);
  310. return portmask;
  311. }
  312. static void rtl838x_write_mcast_pmask(int idx, u64 portmask)
  313. {
  314. // Access MC_PMSK (2) via register RTL8380_TBL_L2
  315. struct table_reg *q = rtl_table_get(RTL8380_TBL_L2, 2);
  316. sw_w32(((u32)portmask) & 0x1fffffff, rtl_table_data(q, 0));
  317. rtl_table_write(q, idx);
  318. rtl_table_release(q);
  319. }
  320. static void rtl838x_vlan_profile_setup(int profile)
  321. {
  322. u32 pmask_id = UNKNOWN_MC_PMASK;
  323. // Enable L2 Learning BIT 0, portmask UNKNOWN_MC_PMASK for unknown MC traffic flooding
  324. u32 p = 1 | pmask_id << 1 | pmask_id << 10 | pmask_id << 19;
  325. sw_w32(p, RTL838X_VLAN_PROFILE(profile));
  326. /* RTL8380 and RTL8390 use an index into the portmask table to set the
  327. * unknown multicast portmask, setup a default at a safe location
  328. * On RTL93XX, the portmask is directly set in the profile,
  329. * see e.g. rtl9300_vlan_profile_setup
  330. */
  331. rtl838x_write_mcast_pmask(UNKNOWN_MC_PMASK, 0x1fffffff);
  332. }
  333. static inline int rtl838x_vlan_port_egr_filter(int port)
  334. {
  335. return RTL838X_VLAN_PORT_EGR_FLTR;
  336. }
  337. static inline int rtl838x_vlan_port_igr_filter(int port)
  338. {
  339. return RTL838X_VLAN_PORT_IGR_FLTR(port);
  340. }
  341. static void rtl838x_stp_get(struct rtl838x_switch_priv *priv, u16 msti, u32 port_state[])
  342. {
  343. int i;
  344. u32 cmd = 1 << 15 /* Execute cmd */
  345. | 1 << 14 /* Read */
  346. | 2 << 12 /* Table type 0b10 */
  347. | (msti & 0xfff);
  348. priv->r->exec_tbl0_cmd(cmd);
  349. for (i = 0; i < 2; i++)
  350. port_state[i] = sw_r32(priv->r->tbl_access_data_0(i));
  351. }
  352. static void rtl838x_stp_set(struct rtl838x_switch_priv *priv, u16 msti, u32 port_state[])
  353. {
  354. int i;
  355. u32 cmd = 1 << 15 /* Execute cmd */
  356. | 0 << 14 /* Write */
  357. | 2 << 12 /* Table type 0b10 */
  358. | (msti & 0xfff);
  359. for (i = 0; i < 2; i++)
  360. sw_w32(port_state[i], priv->r->tbl_access_data_0(i));
  361. priv->r->exec_tbl0_cmd(cmd);
  362. }
  363. u64 rtl838x_traffic_get(int source)
  364. {
  365. return rtl838x_get_port_reg(rtl838x_port_iso_ctrl(source));
  366. }
  367. void rtl838x_traffic_set(int source, u64 dest_matrix)
  368. {
  369. rtl838x_set_port_reg(dest_matrix, rtl838x_port_iso_ctrl(source));
  370. }
  371. void rtl838x_traffic_enable(int source, int dest)
  372. {
  373. rtl838x_mask_port_reg(0, BIT(dest), rtl838x_port_iso_ctrl(source));
  374. }
  375. void rtl838x_traffic_disable(int source, int dest)
  376. {
  377. rtl838x_mask_port_reg(BIT(dest), 0, rtl838x_port_iso_ctrl(source));
  378. }
  379. /*
  380. * Enables or disables the EEE/EEEP capability of a port
  381. */
  382. static void rtl838x_port_eee_set(struct rtl838x_switch_priv *priv, int port, bool enable)
  383. {
  384. u32 v;
  385. // This works only for Ethernet ports, and on the RTL838X, ports from 24 are SFP
  386. if (port >= 24)
  387. return;
  388. pr_debug("In %s: setting port %d to %d\n", __func__, port, enable);
  389. v = enable ? 0x3 : 0x0;
  390. // Set EEE state for 100 (bit 9) & 1000MBit (bit 10)
  391. sw_w32_mask(0x3 << 9, v << 9, priv->r->mac_force_mode_ctrl(port));
  392. // Set TX/RX EEE state
  393. if (enable) {
  394. sw_w32_mask(0, BIT(port), RTL838X_EEE_PORT_TX_EN);
  395. sw_w32_mask(0, BIT(port), RTL838X_EEE_PORT_RX_EN);
  396. } else {
  397. sw_w32_mask(BIT(port), 0, RTL838X_EEE_PORT_TX_EN);
  398. sw_w32_mask(BIT(port), 0, RTL838X_EEE_PORT_RX_EN);
  399. }
  400. priv->ports[port].eee_enabled = enable;
  401. }
  402. /*
  403. * Get EEE own capabilities and negotiation result
  404. */
  405. static int rtl838x_eee_port_ability(struct rtl838x_switch_priv *priv,
  406. struct ethtool_eee *e, int port)
  407. {
  408. u64 link;
  409. if (port >= 24)
  410. return 0;
  411. link = rtl839x_get_port_reg_le(RTL838X_MAC_LINK_STS);
  412. if (!(link & BIT(port)))
  413. return 0;
  414. if (sw_r32(rtl838x_mac_force_mode_ctrl(port)) & BIT(9))
  415. e->advertised |= ADVERTISED_100baseT_Full;
  416. if (sw_r32(rtl838x_mac_force_mode_ctrl(port)) & BIT(10))
  417. e->advertised |= ADVERTISED_1000baseT_Full;
  418. if (sw_r32(RTL838X_MAC_EEE_ABLTY) & BIT(port)) {
  419. e->lp_advertised = ADVERTISED_100baseT_Full;
  420. e->lp_advertised |= ADVERTISED_1000baseT_Full;
  421. return 1;
  422. }
  423. return 0;
  424. }
  425. static void rtl838x_init_eee(struct rtl838x_switch_priv *priv, bool enable)
  426. {
  427. int i;
  428. pr_info("Setting up EEE, state: %d\n", enable);
  429. sw_w32_mask(0x4, 0, RTL838X_SMI_GLB_CTRL);
  430. /* Set timers for EEE */
  431. sw_w32(0x5001411, RTL838X_EEE_TX_TIMER_GIGA_CTRL);
  432. sw_w32(0x5001417, RTL838X_EEE_TX_TIMER_GELITE_CTRL);
  433. // Enable EEE MAC support on ports
  434. for (i = 0; i < priv->cpu_port; i++) {
  435. if (priv->ports[i].phy)
  436. rtl838x_port_eee_set(priv, i, enable);
  437. }
  438. priv->eee_enabled = enable;
  439. }
  440. const struct rtl838x_reg rtl838x_reg = {
  441. .mask_port_reg_be = rtl838x_mask_port_reg,
  442. .set_port_reg_be = rtl838x_set_port_reg,
  443. .get_port_reg_be = rtl838x_get_port_reg,
  444. .mask_port_reg_le = rtl838x_mask_port_reg,
  445. .set_port_reg_le = rtl838x_set_port_reg,
  446. .get_port_reg_le = rtl838x_get_port_reg,
  447. .stat_port_rst = RTL838X_STAT_PORT_RST,
  448. .stat_rst = RTL838X_STAT_RST,
  449. .stat_port_std_mib = RTL838X_STAT_PORT_STD_MIB,
  450. .port_iso_ctrl = rtl838x_port_iso_ctrl,
  451. .traffic_enable = rtl838x_traffic_enable,
  452. .traffic_disable = rtl838x_traffic_disable,
  453. .traffic_get = rtl838x_traffic_get,
  454. .traffic_set = rtl838x_traffic_set,
  455. .l2_ctrl_0 = RTL838X_L2_CTRL_0,
  456. .l2_ctrl_1 = RTL838X_L2_CTRL_1,
  457. .l2_port_aging_out = RTL838X_L2_PORT_AGING_OUT,
  458. .smi_poll_ctrl = RTL838X_SMI_POLL_CTRL,
  459. .l2_tbl_flush_ctrl = RTL838X_L2_TBL_FLUSH_CTRL,
  460. .exec_tbl0_cmd = rtl838x_exec_tbl0_cmd,
  461. .exec_tbl1_cmd = rtl838x_exec_tbl1_cmd,
  462. .tbl_access_data_0 = rtl838x_tbl_access_data_0,
  463. .isr_glb_src = RTL838X_ISR_GLB_SRC,
  464. .isr_port_link_sts_chg = RTL838X_ISR_PORT_LINK_STS_CHG,
  465. .imr_port_link_sts_chg = RTL838X_IMR_PORT_LINK_STS_CHG,
  466. .imr_glb = RTL838X_IMR_GLB,
  467. .vlan_tables_read = rtl838x_vlan_tables_read,
  468. .vlan_set_tagged = rtl838x_vlan_set_tagged,
  469. .vlan_set_untagged = rtl838x_vlan_set_untagged,
  470. .mac_force_mode_ctrl = rtl838x_mac_force_mode_ctrl,
  471. .vlan_profile_dump = rtl838x_vlan_profile_dump,
  472. .vlan_profile_setup = rtl838x_vlan_profile_setup,
  473. .vlan_fwd_on_inner = rtl838x_vlan_fwd_on_inner,
  474. .stp_get = rtl838x_stp_get,
  475. .stp_set = rtl838x_stp_set,
  476. .mac_port_ctrl = rtl838x_mac_port_ctrl,
  477. .l2_port_new_salrn = rtl838x_l2_port_new_salrn,
  478. .l2_port_new_sa_fwd = rtl838x_l2_port_new_sa_fwd,
  479. .mir_ctrl = RTL838X_MIR_CTRL,
  480. .mir_dpm = RTL838X_MIR_DPM_CTRL,
  481. .mir_spm = RTL838X_MIR_SPM_CTRL,
  482. .mac_link_sts = RTL838X_MAC_LINK_STS,
  483. .mac_link_dup_sts = RTL838X_MAC_LINK_DUP_STS,
  484. .mac_link_spd_sts = rtl838x_mac_link_spd_sts,
  485. .mac_rx_pause_sts = RTL838X_MAC_RX_PAUSE_STS,
  486. .mac_tx_pause_sts = RTL838X_MAC_TX_PAUSE_STS,
  487. .read_l2_entry_using_hash = rtl838x_read_l2_entry_using_hash,
  488. .write_l2_entry_using_hash = rtl838x_write_l2_entry_using_hash,
  489. .read_cam = rtl838x_read_cam,
  490. .write_cam = rtl838x_write_cam,
  491. .vlan_port_egr_filter = RTL838X_VLAN_PORT_EGR_FLTR,
  492. .vlan_port_igr_filter = RTL838X_VLAN_PORT_IGR_FLTR(0),
  493. .vlan_port_pb = RTL838X_VLAN_PORT_PB_VLAN,
  494. .vlan_port_tag_sts_ctrl = RTL838X_VLAN_PORT_TAG_STS_CTRL,
  495. .trk_mbr_ctr = rtl838x_trk_mbr_ctr,
  496. .rma_bpdu_fld_pmask = RTL838X_RMA_BPDU_FLD_PMSK,
  497. .spcl_trap_eapol_ctrl = RTL838X_SPCL_TRAP_EAPOL_CTRL,
  498. .init_eee = rtl838x_init_eee,
  499. .port_eee_set = rtl838x_port_eee_set,
  500. .eee_port_ability = rtl838x_eee_port_ability,
  501. .l2_hash_seed = rtl838x_l2_hash_seed,
  502. .l2_hash_key = rtl838x_l2_hash_key,
  503. .read_mcast_pmask = rtl838x_read_mcast_pmask,
  504. .write_mcast_pmask = rtl838x_write_mcast_pmask,
  505. };
  506. irqreturn_t rtl838x_switch_irq(int irq, void *dev_id)
  507. {
  508. struct dsa_switch *ds = dev_id;
  509. u32 status = sw_r32(RTL838X_ISR_GLB_SRC);
  510. u32 ports = sw_r32(RTL838X_ISR_PORT_LINK_STS_CHG);
  511. u32 link;
  512. int i;
  513. /* Clear status */
  514. sw_w32(ports, RTL838X_ISR_PORT_LINK_STS_CHG);
  515. pr_info("RTL8380 Link change: status: %x, ports %x\n", status, ports);
  516. for (i = 0; i < 28; i++) {
  517. if (ports & BIT(i)) {
  518. link = sw_r32(RTL838X_MAC_LINK_STS);
  519. if (link & BIT(i))
  520. dsa_port_phylink_mac_change(ds, i, true);
  521. else
  522. dsa_port_phylink_mac_change(ds, i, false);
  523. }
  524. }
  525. return IRQ_HANDLED;
  526. }
  527. int rtl838x_smi_wait_op(int timeout)
  528. {
  529. do {
  530. timeout--;
  531. udelay(10);
  532. } while ((sw_r32(RTL838X_SMI_ACCESS_PHY_CTRL_1) & 0x1) && (timeout >= 0));
  533. if (timeout <= 0)
  534. return -1;
  535. return 0;
  536. }
  537. /*
  538. * Reads a register in a page from the PHY
  539. */
  540. int rtl838x_read_phy(u32 port, u32 page, u32 reg, u32 *val)
  541. {
  542. u32 v;
  543. u32 park_page;
  544. if (port > 31) {
  545. *val = 0xffff;
  546. return 0;
  547. }
  548. if (page > 4095 || reg > 31)
  549. return -ENOTSUPP;
  550. mutex_lock(&smi_lock);
  551. if (rtl838x_smi_wait_op(10000))
  552. goto timeout;
  553. sw_w32_mask(0xffff0000, port << 16, RTL838X_SMI_ACCESS_PHY_CTRL_2);
  554. park_page = sw_r32(RTL838X_SMI_ACCESS_PHY_CTRL_1) & ((0x1f << 15) | 0x2);
  555. v = reg << 20 | page << 3;
  556. sw_w32(v | park_page, RTL838X_SMI_ACCESS_PHY_CTRL_1);
  557. sw_w32_mask(0, 1, RTL838X_SMI_ACCESS_PHY_CTRL_1);
  558. if (rtl838x_smi_wait_op(10000))
  559. goto timeout;
  560. *val = sw_r32(RTL838X_SMI_ACCESS_PHY_CTRL_2) & 0xffff;
  561. mutex_unlock(&smi_lock);
  562. return 0;
  563. timeout:
  564. mutex_unlock(&smi_lock);
  565. return -ETIMEDOUT;
  566. }
  567. /*
  568. * Write to a register in a page of the PHY
  569. */
  570. int rtl838x_write_phy(u32 port, u32 page, u32 reg, u32 val)
  571. {
  572. u32 v;
  573. u32 park_page;
  574. val &= 0xffff;
  575. if (port > 31 || page > 4095 || reg > 31)
  576. return -ENOTSUPP;
  577. mutex_lock(&smi_lock);
  578. if (rtl838x_smi_wait_op(10000))
  579. goto timeout;
  580. sw_w32(BIT(port), RTL838X_SMI_ACCESS_PHY_CTRL_0);
  581. mdelay(10);
  582. sw_w32_mask(0xffff0000, val << 16, RTL838X_SMI_ACCESS_PHY_CTRL_2);
  583. park_page = sw_r32(RTL838X_SMI_ACCESS_PHY_CTRL_1) & ((0x1f << 15) | 0x2);
  584. v = reg << 20 | page << 3 | 0x4;
  585. sw_w32(v | park_page, RTL838X_SMI_ACCESS_PHY_CTRL_1);
  586. sw_w32_mask(0, 1, RTL838X_SMI_ACCESS_PHY_CTRL_1);
  587. if (rtl838x_smi_wait_op(10000))
  588. goto timeout;
  589. mutex_unlock(&smi_lock);
  590. return 0;
  591. timeout:
  592. mutex_unlock(&smi_lock);
  593. return -ETIMEDOUT;
  594. }
  595. /*
  596. * Read an mmd register of a PHY
  597. */
  598. int rtl838x_read_mmd_phy(u32 port, u32 addr, u32 reg, u32 *val)
  599. {
  600. u32 v;
  601. mutex_lock(&smi_lock);
  602. if (rtl838x_smi_wait_op(10000))
  603. goto timeout;
  604. sw_w32(1 << port, RTL838X_SMI_ACCESS_PHY_CTRL_0);
  605. mdelay(10);
  606. sw_w32_mask(0xffff0000, port << 16, RTL838X_SMI_ACCESS_PHY_CTRL_2);
  607. v = addr << 16 | reg;
  608. sw_w32(v, RTL838X_SMI_ACCESS_PHY_CTRL_3);
  609. /* mmd-access | read | cmd-start */
  610. v = 1 << 1 | 0 << 2 | 1;
  611. sw_w32(v, RTL838X_SMI_ACCESS_PHY_CTRL_1);
  612. if (rtl838x_smi_wait_op(10000))
  613. goto timeout;
  614. *val = sw_r32(RTL838X_SMI_ACCESS_PHY_CTRL_2) & 0xffff;
  615. mutex_unlock(&smi_lock);
  616. return 0;
  617. timeout:
  618. mutex_unlock(&smi_lock);
  619. return -ETIMEDOUT;
  620. }
  621. /*
  622. * Write to an mmd register of a PHY
  623. */
  624. int rtl838x_write_mmd_phy(u32 port, u32 addr, u32 reg, u32 val)
  625. {
  626. u32 v;
  627. pr_debug("MMD write: port %d, dev %d, reg %d, val %x\n", port, addr, reg, val);
  628. val &= 0xffff;
  629. mutex_lock(&smi_lock);
  630. if (rtl838x_smi_wait_op(10000))
  631. goto timeout;
  632. sw_w32(1 << port, RTL838X_SMI_ACCESS_PHY_CTRL_0);
  633. mdelay(10);
  634. sw_w32_mask(0xffff0000, val << 16, RTL838X_SMI_ACCESS_PHY_CTRL_2);
  635. sw_w32_mask(0x1f << 16, addr << 16, RTL838X_SMI_ACCESS_PHY_CTRL_3);
  636. sw_w32_mask(0xffff, reg, RTL838X_SMI_ACCESS_PHY_CTRL_3);
  637. /* mmd-access | write | cmd-start */
  638. v = 1 << 1 | 1 << 2 | 1;
  639. sw_w32(v, RTL838X_SMI_ACCESS_PHY_CTRL_1);
  640. if (rtl838x_smi_wait_op(10000))
  641. goto timeout;
  642. mutex_unlock(&smi_lock);
  643. return 0;
  644. timeout:
  645. mutex_unlock(&smi_lock);
  646. return -ETIMEDOUT;
  647. }
  648. void rtl8380_get_version(struct rtl838x_switch_priv *priv)
  649. {
  650. u32 rw_save, info_save;
  651. u32 info;
  652. rw_save = sw_r32(RTL838X_INT_RW_CTRL);
  653. sw_w32(rw_save | 0x3, RTL838X_INT_RW_CTRL);
  654. info_save = sw_r32(RTL838X_CHIP_INFO);
  655. sw_w32(info_save | 0xA0000000, RTL838X_CHIP_INFO);
  656. info = sw_r32(RTL838X_CHIP_INFO);
  657. sw_w32(info_save, RTL838X_CHIP_INFO);
  658. sw_w32(rw_save, RTL838X_INT_RW_CTRL);
  659. if ((info & 0xFFFF) == 0x6275) {
  660. if (((info >> 16) & 0x1F) == 0x1)
  661. priv->version = RTL8380_VERSION_A;
  662. else if (((info >> 16) & 0x1F) == 0x2)
  663. priv->version = RTL8380_VERSION_B;
  664. else
  665. priv->version = RTL8380_VERSION_B;
  666. } else {
  667. priv->version = '-';
  668. }
  669. }
  670. void rtl838x_vlan_profile_dump(int profile)
  671. {
  672. u32 p;
  673. if (profile < 0 || profile > 7)
  674. return;
  675. p = sw_r32(RTL838X_VLAN_PROFILE(profile));
  676. pr_info("VLAN profile %d: L2 learning: %d, UNKN L2MC FLD PMSK %d, \
  677. UNKN IPMC FLD PMSK %d, UNKN IPv6MC FLD PMSK: %d",
  678. profile, p & 1, (p >> 1) & 0x1ff, (p >> 10) & 0x1ff, (p >> 19) & 0x1ff);
  679. }
  680. void rtl8380_sds_rst(int mac)
  681. {
  682. u32 offset = (mac == 24) ? 0 : 0x100;
  683. sw_w32_mask(1 << 11, 0, RTL838X_SDS4_FIB_REG0 + offset);
  684. sw_w32_mask(0x3, 0, RTL838X_SDS4_REG28 + offset);
  685. sw_w32_mask(0x3, 0x3, RTL838X_SDS4_REG28 + offset);
  686. sw_w32_mask(0, 0x1 << 6, RTL838X_SDS4_DUMMY0 + offset);
  687. sw_w32_mask(0x1 << 6, 0, RTL838X_SDS4_DUMMY0 + offset);
  688. pr_debug("SERDES reset: %d\n", mac);
  689. }
  690. int rtl8380_sds_power(int mac, int val)
  691. {
  692. u32 mode = (val == 1) ? 0x4 : 0x9;
  693. u32 offset = (mac == 24) ? 5 : 0;
  694. if ((mac != 24) && (mac != 26)) {
  695. pr_err("%s: not a fibre port: %d\n", __func__, mac);
  696. return -1;
  697. }
  698. sw_w32_mask(0x1f << offset, mode << offset, RTL838X_SDS_MODE_SEL);
  699. rtl8380_sds_rst(mac);
  700. return 0;
  701. }