rtl839x.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807
  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. extern struct rtl83xx_soc_info soc_info;
  6. void rtl839x_print_matrix(void)
  7. {
  8. volatile u64 *ptr9;
  9. int i;
  10. ptr9 = RTL838X_SW_BASE + RTL839X_PORT_ISO_CTRL(0);
  11. for (i = 0; i < 52; i += 4)
  12. pr_debug("> %16llx %16llx %16llx %16llx\n",
  13. ptr9[i + 0], ptr9[i + 1], ptr9[i + 2], ptr9[i + 3]);
  14. pr_debug("CPU_PORT> %16llx\n", ptr9[52]);
  15. }
  16. static inline int rtl839x_port_iso_ctrl(int p)
  17. {
  18. return RTL839X_PORT_ISO_CTRL(p);
  19. }
  20. static inline void rtl839x_exec_tbl0_cmd(u32 cmd)
  21. {
  22. sw_w32(cmd, RTL839X_TBL_ACCESS_CTRL_0);
  23. do { } while (sw_r32(RTL839X_TBL_ACCESS_CTRL_0) & BIT(16));
  24. }
  25. static inline void rtl839x_exec_tbl1_cmd(u32 cmd)
  26. {
  27. sw_w32(cmd, RTL839X_TBL_ACCESS_CTRL_1);
  28. do { } while (sw_r32(RTL839X_TBL_ACCESS_CTRL_1) & BIT(16));
  29. }
  30. inline void rtl839x_exec_tbl2_cmd(u32 cmd)
  31. {
  32. sw_w32(cmd, RTL839X_TBL_ACCESS_CTRL_2);
  33. do { } while (sw_r32(RTL839X_TBL_ACCESS_CTRL_2) & (1 << 9));
  34. }
  35. static inline int rtl839x_tbl_access_data_0(int i)
  36. {
  37. return RTL839X_TBL_ACCESS_DATA_0(i);
  38. }
  39. static void rtl839x_vlan_tables_read(u32 vlan, struct rtl838x_vlan_info *info)
  40. {
  41. u32 u, v, w;
  42. // Read VLAN table (0) via register 0
  43. struct table_reg *r = rtl_table_get(RTL8390_TBL_0, 0);
  44. rtl_table_read(r, vlan);
  45. u = sw_r32(rtl_table_data(r, 0));
  46. v = sw_r32(rtl_table_data(r, 1));
  47. w = sw_r32(rtl_table_data(r, 2));
  48. rtl_table_release(r);
  49. info->tagged_ports = u;
  50. info->tagged_ports = (info->tagged_ports << 21) | ((v >> 11) & 0x1fffff);
  51. info->profile_id = w >> 30 | ((v & 1) << 2);
  52. info->hash_mc_fid = !!(w & BIT(2));
  53. info->hash_uc_fid = !!(w & BIT(3));
  54. info->fid = (v >> 3) & 0xff;
  55. // Read UNTAG table (0) via table register 1
  56. r = rtl_table_get(RTL8390_TBL_1, 0);
  57. rtl_table_read(r, vlan);
  58. u = sw_r32(rtl_table_data(r, 0));
  59. v = sw_r32(rtl_table_data(r, 1));
  60. rtl_table_release(r);
  61. info->untagged_ports = u;
  62. info->untagged_ports = (info->untagged_ports << 21) | ((v >> 11) & 0x1fffff);
  63. }
  64. static void rtl839x_vlan_set_tagged(u32 vlan, struct rtl838x_vlan_info *info)
  65. {
  66. u32 u, v, w;
  67. // Access VLAN table (0) via register 0
  68. struct table_reg *r = rtl_table_get(RTL8390_TBL_0, 0);
  69. u = info->tagged_ports >> 21;
  70. v = info->tagged_ports << 11;
  71. v |= ((u32)info->fid) << 3;
  72. v |= info->hash_uc_fid ? BIT(2) : 0;
  73. v |= info->hash_mc_fid ? BIT(1) : 0;
  74. v |= (info->profile_id & 0x4) ? 1 : 0;
  75. w = ((u32)(info->profile_id & 3)) << 30;
  76. sw_w32(u, rtl_table_data(r, 0));
  77. sw_w32(v, rtl_table_data(r, 1));
  78. sw_w32(w, rtl_table_data(r, 2));
  79. rtl_table_write(r, vlan);
  80. rtl_table_release(r);
  81. }
  82. static void rtl839x_vlan_set_untagged(u32 vlan, u64 portmask)
  83. {
  84. u32 u, v;
  85. // Access UNTAG table (0) via table register 1
  86. struct table_reg *r = rtl_table_get(RTL8390_TBL_1, 0);
  87. u = portmask >> 21;
  88. v = portmask << 11;
  89. sw_w32(u, rtl_table_data(r, 0));
  90. sw_w32(v, rtl_table_data(r, 1));
  91. rtl_table_write(r, vlan);
  92. rtl_table_release(r);
  93. }
  94. /* Sets the L2 forwarding to be based on either the inner VLAN tag or the outer
  95. */
  96. static void rtl839x_vlan_fwd_on_inner(int port, bool is_set)
  97. {
  98. if (is_set)
  99. rtl839x_mask_port_reg_be(BIT_ULL(port), 0ULL, RTL839X_VLAN_PORT_FWD);
  100. else
  101. rtl839x_mask_port_reg_be(0ULL, BIT_ULL(port), RTL839X_VLAN_PORT_FWD);
  102. }
  103. /*
  104. * Hash seed is vid (actually rvid) concatenated with the MAC address
  105. */
  106. static u64 rtl839x_l2_hash_seed(u64 mac, u32 vid)
  107. {
  108. u64 v = vid;
  109. v <<= 48;
  110. v |= mac;
  111. return v;
  112. }
  113. /*
  114. * Applies the same hash algorithm as the one used currently by the ASIC to the seed
  115. * and returns a key into the L2 hash table
  116. */
  117. static u32 rtl839x_l2_hash_key(struct rtl838x_switch_priv *priv, u64 seed)
  118. {
  119. u32 h1, h2, h;
  120. if (sw_r32(priv->r->l2_ctrl_0) & 1) {
  121. h1 = (u32) (((seed >> 60) & 0x3f) ^ ((seed >> 54) & 0x3f)
  122. ^ ((seed >> 36) & 0x3f) ^ ((seed >> 30) & 0x3f)
  123. ^ ((seed >> 12) & 0x3f) ^ ((seed >> 6) & 0x3f));
  124. h2 = (u32) (((seed >> 48) & 0x3f) ^ ((seed >> 42) & 0x3f)
  125. ^ ((seed >> 24) & 0x3f) ^ ((seed >> 18) & 0x3f)
  126. ^ (seed & 0x3f));
  127. h = (h1 << 6) | h2;
  128. } else {
  129. h = (seed >> 60)
  130. ^ ((((seed >> 48) & 0x3f) << 6) | ((seed >> 54) & 0x3f))
  131. ^ ((seed >> 36) & 0xfff) ^ ((seed >> 24) & 0xfff)
  132. ^ ((seed >> 12) & 0xfff) ^ (seed & 0xfff);
  133. }
  134. return h;
  135. }
  136. static inline int rtl839x_mac_force_mode_ctrl(int p)
  137. {
  138. return RTL839X_MAC_FORCE_MODE_CTRL + (p << 2);
  139. }
  140. static inline int rtl839x_mac_port_ctrl(int p)
  141. {
  142. return RTL839X_MAC_PORT_CTRL(p);
  143. }
  144. static inline int rtl839x_l2_port_new_salrn(int p)
  145. {
  146. return RTL839X_L2_PORT_NEW_SALRN(p);
  147. }
  148. static inline int rtl839x_l2_port_new_sa_fwd(int p)
  149. {
  150. return RTL839X_L2_PORT_NEW_SA_FWD(p);
  151. }
  152. static inline int rtl839x_mac_link_spd_sts(int p)
  153. {
  154. return RTL839X_MAC_LINK_SPD_STS(p);
  155. }
  156. static inline int rtl839x_trk_mbr_ctr(int group)
  157. {
  158. return RTL839X_TRK_MBR_CTR + (group << 3);
  159. }
  160. static void rtl839x_fill_l2_entry(u32 r[], struct rtl838x_l2_entry *e)
  161. {
  162. /* Table contains different entry types, we need to identify the right one:
  163. * Check for MC entries, first
  164. */
  165. e->is_ip_mc = !!(r[2] & BIT(31));
  166. e->is_ipv6_mc = !!(r[2] & BIT(30));
  167. e->type = L2_INVALID;
  168. if (!e->is_ip_mc) {
  169. e->mac[0] = (r[0] >> 12);
  170. e->mac[1] = (r[0] >> 4);
  171. e->mac[2] = ((r[1] >> 28) | (r[0] << 4));
  172. e->mac[3] = (r[1] >> 20);
  173. e->mac[4] = (r[1] >> 12);
  174. e->mac[5] = (r[1] >> 4);
  175. /* Is it a unicast entry? check multicast bit */
  176. if (!(e->mac[0] & 1)) {
  177. e->is_static = !!((r[2] >> 18) & 1);
  178. e->vid = (r[2] >> 4) & 0xfff;
  179. e->rvid = (r[0] >> 20) & 0xfff;
  180. e->port = (r[2] >> 24) & 0x3f;
  181. e->block_da = !!(r[2] & (1 << 19));
  182. e->block_sa = !!(r[2] & (1 << 20));
  183. e->suspended = !!(r[2] & (1 << 17));
  184. e->next_hop = !!(r[2] & (1 << 16));
  185. if (e->next_hop)
  186. pr_info("Found next hop entry, need to read data\n");
  187. e->age = (r[2] >> 21) & 3;
  188. e->valid = true;
  189. if (!(r[2] & 0xc0fd0000)) /* Check for valid entry */
  190. e->valid = false;
  191. else
  192. e->type = L2_UNICAST;
  193. } else {
  194. e->valid = true;
  195. e->type = L2_MULTICAST;
  196. e->mc_portmask_index = (r[2]>>6) & 0xfff;
  197. }
  198. }
  199. if (e->is_ip_mc) {
  200. e->valid = true;
  201. e->type = IP4_MULTICAST;
  202. }
  203. if (e->is_ipv6_mc) {
  204. e->valid = true;
  205. e->type = IP6_MULTICAST;
  206. }
  207. }
  208. /*
  209. * Fills the 3 SoC table registers r[] with the information of in the rtl838x_l2_entry
  210. */
  211. static void rtl839x_fill_l2_row(u32 r[], struct rtl838x_l2_entry *e)
  212. {
  213. if (!e->valid) {
  214. r[0] = r[1] = r[2] = 0;
  215. return;
  216. }
  217. r[2] = e->is_ip_mc ? BIT(31) : 0;
  218. r[2] |= e->is_ipv6_mc ? BIT(30) : 0;
  219. if (!e->is_ip_mc && !e->is_ipv6_mc) {
  220. r[0] = ((u32)e->mac[0]) << 12;
  221. r[0] |= ((u32)e->mac[1]) << 4;
  222. r[0] |= ((u32)e->mac[2]) >> 4;
  223. r[1] = ((u32)e->mac[2]) << 28;
  224. r[1] |= ((u32)e->mac[3]) << 20;
  225. r[1] |= ((u32)e->mac[4]) << 12;
  226. r[1] |= ((u32)e->mac[5]) << 4;
  227. if (!(e->mac[0] & 1)) { // Not multicast
  228. r[2] |= e->is_static ? BIT(18) : 0;
  229. r[2] |= e->vid << 4;
  230. r[0] |= ((u32)e->rvid) << 20;
  231. r[2] |= e->port << 24;
  232. r[2] |= e->block_da ? BIT(19) : 0;
  233. r[2] |= e->block_sa ? BIT(20) : 0;
  234. r[2] |= e->suspended ? BIT(17) : 0;
  235. if (e->next_hop) {
  236. r[2] |= BIT(16);
  237. r[2] |= e->nh_vlan_target ? BIT(15) : 0;
  238. r[2] |= (e->nh_route_id & 0x7ff) << 4;
  239. }
  240. r[2] |= ((u32)e->age) << 21;
  241. } else { // L2 Multicast
  242. r[0] |= ((u32)e->rvid) << 20;
  243. r[2] |= ((u32)e->mc_portmask_index) << 6;
  244. pr_debug("Write L2 MC entry: %08x %08x %08x\n", r[0], r[1], r[2]);
  245. }
  246. } else { // IPv4 or IPv6 MC entry
  247. r[0] = ((u32)e->rvid) << 20;
  248. r[2] |= ((u32)e->mc_portmask_index) << 6;
  249. r[1] = e->mc_gip;
  250. }
  251. }
  252. /*
  253. * Read an L2 UC or MC entry out of a hash bucket of the L2 forwarding table
  254. * hash is the id of the bucket and pos is the position of the entry in that bucket
  255. * The data read from the SoC is filled into rtl838x_l2_entry
  256. */
  257. static u64 rtl839x_read_l2_entry_using_hash(u32 hash, u32 pos, struct rtl838x_l2_entry *e)
  258. {
  259. u32 r[3];
  260. struct table_reg *q = rtl_table_get(RTL8390_TBL_L2, 0);
  261. u32 idx = (0 << 14) | (hash << 2) | pos; // Search SRAM, with hash and at pos in bucket
  262. int i;
  263. rtl_table_read(q, idx);
  264. for (i= 0; i < 3; i++)
  265. r[i] = sw_r32(rtl_table_data(q, i));
  266. rtl_table_release(q);
  267. rtl839x_fill_l2_entry(r, e);
  268. if (!e->valid)
  269. return 0;
  270. return rtl839x_l2_hash_seed(ether_addr_to_u64(&e->mac[0]), e->rvid);
  271. }
  272. static void rtl839x_write_l2_entry_using_hash(u32 hash, u32 pos, struct rtl838x_l2_entry *e)
  273. {
  274. u32 r[3];
  275. struct table_reg *q = rtl_table_get(RTL8390_TBL_L2, 0);
  276. int i;
  277. u32 idx = (0 << 14) | (hash << 2) | pos; // Access SRAM, with hash and at pos in bucket
  278. rtl839x_fill_l2_row(r, e);
  279. for (i= 0; i < 3; i++)
  280. sw_w32(r[i], rtl_table_data(q, i));
  281. rtl_table_write(q, idx);
  282. rtl_table_release(q);
  283. }
  284. static u64 rtl839x_read_cam(int idx, struct rtl838x_l2_entry *e)
  285. {
  286. u32 r[3];
  287. struct table_reg *q = rtl_table_get(RTL8390_TBL_L2, 1); // Access L2 Table 1
  288. int i;
  289. rtl_table_read(q, idx);
  290. for (i= 0; i < 3; i++)
  291. r[i] = sw_r32(rtl_table_data(q, i));
  292. rtl_table_release(q);
  293. rtl839x_fill_l2_entry(r, e);
  294. if (!e->valid)
  295. return 0;
  296. pr_debug("Found in CAM: R1 %x R2 %x R3 %x\n", r[0], r[1], r[2]);
  297. // Return MAC with concatenated VID ac concatenated ID
  298. return rtl839x_l2_hash_seed(ether_addr_to_u64(&e->mac[0]), e->rvid);
  299. }
  300. static void rtl839x_write_cam(int idx, struct rtl838x_l2_entry *e)
  301. {
  302. u32 r[3];
  303. struct table_reg *q = rtl_table_get(RTL8390_TBL_L2, 1); // Access L2 Table 1
  304. int i;
  305. rtl839x_fill_l2_row(r, e);
  306. for (i= 0; i < 3; i++)
  307. sw_w32(r[i], rtl_table_data(q, i));
  308. rtl_table_write(q, idx);
  309. rtl_table_release(q);
  310. }
  311. static u64 rtl839x_read_mcast_pmask(int idx)
  312. {
  313. u64 portmask;
  314. // Read MC_PMSK (2) via register RTL8390_TBL_L2
  315. struct table_reg *q = rtl_table_get(RTL8390_TBL_L2, 2);
  316. rtl_table_read(q, idx);
  317. portmask = sw_r32(rtl_table_data(q, 0));
  318. portmask <<= 32;
  319. portmask |= sw_r32(rtl_table_data(q, 1));
  320. portmask >>= 11; // LSB is bit 11 in data registers
  321. rtl_table_release(q);
  322. return portmask;
  323. }
  324. static void rtl839x_write_mcast_pmask(int idx, u64 portmask)
  325. {
  326. // Access MC_PMSK (2) via register RTL8380_TBL_L2
  327. struct table_reg *q = rtl_table_get(RTL8390_TBL_L2, 2);
  328. portmask <<= 11; // LSB is bit 11 in data registers
  329. sw_w32((u32)(portmask >> 32), rtl_table_data(q, 0));
  330. sw_w32((u32)((portmask & 0xfffff800)), rtl_table_data(q, 1));
  331. rtl_table_write(q, idx);
  332. rtl_table_release(q);
  333. }
  334. static void rtl839x_vlan_profile_setup(int profile)
  335. {
  336. u32 p[2];
  337. u32 pmask_id = UNKNOWN_MC_PMASK;
  338. p[0] = pmask_id; // Use portmaks 0xfff for unknown IPv6 MC flooding
  339. // Enable L2 Learning BIT 0, portmask UNKNOWN_MC_PMASK for IP/L2-MC traffic flooding
  340. p[1] = 1 | pmask_id << 1 | pmask_id << 13;
  341. sw_w32(p[0], RTL839X_VLAN_PROFILE(profile));
  342. sw_w32(p[1], RTL839X_VLAN_PROFILE(profile) + 4);
  343. rtl839x_write_mcast_pmask(UNKNOWN_MC_PMASK, 0x001fffffffffffff);
  344. }
  345. static inline int rtl839x_vlan_port_egr_filter(int port)
  346. {
  347. return RTL839X_VLAN_PORT_EGR_FLTR(port);
  348. }
  349. static inline int rtl839x_vlan_port_igr_filter(int port)
  350. {
  351. return RTL839X_VLAN_PORT_IGR_FLTR(port);
  352. }
  353. u64 rtl839x_traffic_get(int source)
  354. {
  355. return rtl839x_get_port_reg_be(rtl839x_port_iso_ctrl(source));
  356. }
  357. void rtl839x_traffic_set(int source, u64 dest_matrix)
  358. {
  359. rtl839x_set_port_reg_be(dest_matrix, rtl839x_port_iso_ctrl(source));
  360. }
  361. void rtl839x_traffic_enable(int source, int dest)
  362. {
  363. rtl839x_mask_port_reg_be(0, BIT_ULL(dest), rtl839x_port_iso_ctrl(source));
  364. }
  365. void rtl839x_traffic_disable(int source, int dest)
  366. {
  367. rtl839x_mask_port_reg_be(BIT_ULL(dest), 0, rtl839x_port_iso_ctrl(source));
  368. }
  369. irqreturn_t rtl839x_switch_irq(int irq, void *dev_id)
  370. {
  371. struct dsa_switch *ds = dev_id;
  372. u32 status = sw_r32(RTL839X_ISR_GLB_SRC);
  373. u64 ports = rtl839x_get_port_reg_le(RTL839X_ISR_PORT_LINK_STS_CHG);
  374. u64 link;
  375. int i;
  376. /* Clear status */
  377. rtl839x_set_port_reg_le(ports, RTL839X_ISR_PORT_LINK_STS_CHG);
  378. pr_debug("RTL8390 Link change: status: %x, ports %llx\n", status, ports);
  379. for (i = 0; i < RTL839X_CPU_PORT; i++) {
  380. if (ports & BIT_ULL(i)) {
  381. link = rtl839x_get_port_reg_le(RTL839X_MAC_LINK_STS);
  382. if (link & BIT_ULL(i))
  383. dsa_port_phylink_mac_change(ds, i, true);
  384. else
  385. dsa_port_phylink_mac_change(ds, i, false);
  386. }
  387. }
  388. return IRQ_HANDLED;
  389. }
  390. // TODO: unused
  391. int rtl8390_sds_power(int mac, int val)
  392. {
  393. u32 offset = (mac == 48) ? 0x0 : 0x100;
  394. u32 mode = val ? 0 : 1;
  395. pr_debug("In %s: mac %d, set %d\n", __func__, mac, val);
  396. if ((mac != 48) && (mac != 49)) {
  397. pr_err("%s: not an SFP port: %d\n", __func__, mac);
  398. return -1;
  399. }
  400. // Set bit 1003. 1000 starts at 7c
  401. sw_w32_mask(BIT(11), mode << 11, RTL839X_SDS12_13_PWR0 + offset);
  402. return 0;
  403. }
  404. int rtl839x_read_phy(u32 port, u32 page, u32 reg, u32 *val)
  405. {
  406. u32 v;
  407. if (port > 63 || page > 4095 || reg > 31)
  408. return -ENOTSUPP;
  409. mutex_lock(&smi_lock);
  410. sw_w32_mask(0xffff0000, port << 16, RTL839X_PHYREG_DATA_CTRL);
  411. v = reg << 5 | page << 10 | ((page == 0x1fff) ? 0x1f : 0) << 23;
  412. sw_w32(v, RTL839X_PHYREG_ACCESS_CTRL);
  413. sw_w32(0x1ff, RTL839X_PHYREG_CTRL);
  414. v |= 1;
  415. sw_w32(v, RTL839X_PHYREG_ACCESS_CTRL);
  416. do {
  417. } while (sw_r32(RTL839X_PHYREG_ACCESS_CTRL) & 0x1);
  418. *val = sw_r32(RTL839X_PHYREG_DATA_CTRL) & 0xffff;
  419. mutex_unlock(&smi_lock);
  420. return 0;
  421. }
  422. int rtl839x_write_phy(u32 port, u32 page, u32 reg, u32 val)
  423. {
  424. u32 v;
  425. int err = 0;
  426. val &= 0xffff;
  427. if (port > 63 || page > 4095 || reg > 31)
  428. return -ENOTSUPP;
  429. mutex_lock(&smi_lock);
  430. // Set PHY to access
  431. rtl839x_set_port_reg_le(BIT_ULL(port), RTL839X_PHYREG_PORT_CTRL);
  432. sw_w32_mask(0xffff0000, val << 16, RTL839X_PHYREG_DATA_CTRL);
  433. v = reg << 5 | page << 10 | ((page == 0x1fff) ? 0x1f : 0) << 23;
  434. sw_w32(v, RTL839X_PHYREG_ACCESS_CTRL);
  435. sw_w32(0x1ff, RTL839X_PHYREG_CTRL);
  436. v |= BIT(3) | 1; /* Write operation and execute */
  437. sw_w32(v, RTL839X_PHYREG_ACCESS_CTRL);
  438. do {
  439. } while (sw_r32(RTL839X_PHYREG_ACCESS_CTRL) & 0x1);
  440. if (sw_r32(RTL839X_PHYREG_ACCESS_CTRL) & 0x2)
  441. err = -EIO;
  442. mutex_unlock(&smi_lock);
  443. return err;
  444. }
  445. /*
  446. * Read an mmd register of the PHY
  447. */
  448. int rtl839x_read_mmd_phy(u32 port, u32 devnum, u32 regnum, u32 *val)
  449. {
  450. int err = 0;
  451. u32 v;
  452. mutex_lock(&smi_lock);
  453. // Set PHY to access
  454. sw_w32_mask(0xffff << 16, port << 16, RTL839X_PHYREG_DATA_CTRL);
  455. // Set MMD device number and register to write to
  456. sw_w32(devnum << 16 | (regnum & 0xffff), RTL839X_PHYREG_MMD_CTRL);
  457. v = BIT(2) | BIT(0); // MMD-access | EXEC
  458. sw_w32(v, RTL839X_PHYREG_ACCESS_CTRL);
  459. do {
  460. v = sw_r32(RTL839X_PHYREG_ACCESS_CTRL);
  461. } while (v & BIT(0));
  462. // There is no error-checking via BIT 1 of v, as it does not seem to be set correctly
  463. *val = (sw_r32(RTL839X_PHYREG_DATA_CTRL) & 0xffff);
  464. pr_debug("%s: port %d, regnum: %x, val: %x (err %d)\n", __func__, port, regnum, *val, err);
  465. mutex_unlock(&smi_lock);
  466. return err;
  467. }
  468. /*
  469. * Write to an mmd register of the PHY
  470. */
  471. int rtl839x_write_mmd_phy(u32 port, u32 devnum, u32 regnum, u32 val)
  472. {
  473. int err = 0;
  474. u32 v;
  475. mutex_lock(&smi_lock);
  476. // Set PHY to access
  477. rtl839x_set_port_reg_le(BIT_ULL(port), RTL839X_PHYREG_PORT_CTRL);
  478. // Set data to write
  479. sw_w32_mask(0xffff << 16, val << 16, RTL839X_PHYREG_DATA_CTRL);
  480. // Set MMD device number and register to write to
  481. sw_w32(devnum << 16 | (regnum & 0xffff), RTL839X_PHYREG_MMD_CTRL);
  482. v = BIT(3) | BIT(2) | BIT(0); // WRITE | MMD-access | EXEC
  483. sw_w32(v, RTL839X_PHYREG_ACCESS_CTRL);
  484. do {
  485. v = sw_r32(RTL839X_PHYREG_ACCESS_CTRL);
  486. } while (v & BIT(0));
  487. pr_debug("%s: port %d, regnum: %x, val: %x (err %d)\n", __func__, port, regnum, val, err);
  488. mutex_unlock(&smi_lock);
  489. return err;
  490. }
  491. void rtl8390_get_version(struct rtl838x_switch_priv *priv)
  492. {
  493. u32 info;
  494. sw_w32_mask(0xf << 28, 0xa << 28, RTL839X_CHIP_INFO);
  495. info = sw_r32(RTL839X_CHIP_INFO);
  496. pr_debug("Chip-Info: %x\n", info);
  497. priv->version = RTL8390_VERSION_A;
  498. }
  499. void rtl839x_vlan_profile_dump(int profile)
  500. {
  501. u32 p[2];
  502. if (profile < 0 || profile > 7)
  503. return;
  504. p[0] = sw_r32(RTL839X_VLAN_PROFILE(profile));
  505. p[1] = sw_r32(RTL839X_VLAN_PROFILE(profile) + 4);
  506. pr_info("VLAN profile %d: L2 learning: %d, UNKN L2MC FLD PMSK %d, \
  507. UNKN IPMC FLD PMSK %d, UNKN IPv6MC FLD PMSK: %d",
  508. profile, p[1] & 1, (p[1] >> 1) & 0xfff, (p[1] >> 13) & 0xfff,
  509. (p[0]) & 0xfff);
  510. pr_info("VLAN profile %d: raw %08x, %08x\n", profile, p[0], p[1]);
  511. }
  512. static void rtl839x_stp_get(struct rtl838x_switch_priv *priv, u16 msti, u32 port_state[])
  513. {
  514. int i;
  515. u32 cmd = 1 << 16 /* Execute cmd */
  516. | 0 << 15 /* Read */
  517. | 5 << 12 /* Table type 0b101 */
  518. | (msti & 0xfff);
  519. priv->r->exec_tbl0_cmd(cmd);
  520. for (i = 0; i < 4; i++)
  521. port_state[i] = sw_r32(priv->r->tbl_access_data_0(i));
  522. }
  523. static void rtl839x_stp_set(struct rtl838x_switch_priv *priv, u16 msti, u32 port_state[])
  524. {
  525. int i;
  526. u32 cmd = 1 << 16 /* Execute cmd */
  527. | 1 << 15 /* Write */
  528. | 5 << 12 /* Table type 0b101 */
  529. | (msti & 0xfff);
  530. for (i = 0; i < 4; i++)
  531. sw_w32(port_state[i], priv->r->tbl_access_data_0(i));
  532. priv->r->exec_tbl0_cmd(cmd);
  533. }
  534. /*
  535. * Enables or disables the EEE/EEEP capability of a port
  536. */
  537. void rtl839x_port_eee_set(struct rtl838x_switch_priv *priv, int port, bool enable)
  538. {
  539. u32 v;
  540. // This works only for Ethernet ports, and on the RTL839X, ports above 47 are SFP
  541. if (port >= 48)
  542. return;
  543. enable = true;
  544. pr_debug("In %s: setting port %d to %d\n", __func__, port, enable);
  545. v = enable ? 0xf : 0x0;
  546. // Set EEE for 100, 500, 1000MBit and 10GBit
  547. sw_w32_mask(0xf << 8, v << 8, rtl839x_mac_force_mode_ctrl(port));
  548. // Set TX/RX EEE state
  549. v = enable ? 0x3 : 0x0;
  550. sw_w32(v, RTL839X_EEE_CTRL(port));
  551. priv->ports[port].eee_enabled = enable;
  552. }
  553. /*
  554. * Get EEE own capabilities and negotiation result
  555. */
  556. int rtl839x_eee_port_ability(struct rtl838x_switch_priv *priv, struct ethtool_eee *e, int port)
  557. {
  558. u64 link, a;
  559. if (port >= 48)
  560. return 0;
  561. link = rtl839x_get_port_reg_le(RTL839X_MAC_LINK_STS);
  562. if (!(link & BIT_ULL(port)))
  563. return 0;
  564. if (sw_r32(rtl839x_mac_force_mode_ctrl(port)) & BIT(8))
  565. e->advertised |= ADVERTISED_100baseT_Full;
  566. if (sw_r32(rtl839x_mac_force_mode_ctrl(port)) & BIT(10))
  567. e->advertised |= ADVERTISED_1000baseT_Full;
  568. a = rtl839x_get_port_reg_le(RTL839X_MAC_EEE_ABLTY);
  569. pr_info("Link partner: %016llx\n", a);
  570. if (rtl839x_get_port_reg_le(RTL839X_MAC_EEE_ABLTY) & BIT_ULL(port)) {
  571. e->lp_advertised = ADVERTISED_100baseT_Full;
  572. e->lp_advertised |= ADVERTISED_1000baseT_Full;
  573. return 1;
  574. }
  575. return 0;
  576. }
  577. static void rtl839x_init_eee(struct rtl838x_switch_priv *priv, bool enable)
  578. {
  579. int i;
  580. pr_info("Setting up EEE, state: %d\n", enable);
  581. // Set wake timer for TX and pause timer both to 0x21
  582. sw_w32_mask(0xff << 20| 0xff, 0x21 << 20| 0x21, RTL839X_EEE_TX_TIMER_GELITE_CTRL);
  583. // Set pause wake timer for GIGA-EEE to 0x11
  584. sw_w32_mask(0xff << 20, 0x11 << 20, RTL839X_EEE_TX_TIMER_GIGA_CTRL);
  585. // Set pause wake timer for 10GBit ports to 0x11
  586. sw_w32_mask(0xff << 20, 0x11 << 20, RTL839X_EEE_TX_TIMER_10G_CTRL);
  587. // Setup EEE on all ports
  588. for (i = 0; i < priv->cpu_port; i++) {
  589. if (priv->ports[i].phy)
  590. rtl839x_port_eee_set(priv, i, enable);
  591. }
  592. priv->eee_enabled = enable;
  593. }
  594. const struct rtl838x_reg rtl839x_reg = {
  595. .mask_port_reg_be = rtl839x_mask_port_reg_be,
  596. .set_port_reg_be = rtl839x_set_port_reg_be,
  597. .get_port_reg_be = rtl839x_get_port_reg_be,
  598. .mask_port_reg_le = rtl839x_mask_port_reg_le,
  599. .set_port_reg_le = rtl839x_set_port_reg_le,
  600. .get_port_reg_le = rtl839x_get_port_reg_le,
  601. .stat_port_rst = RTL839X_STAT_PORT_RST,
  602. .stat_rst = RTL839X_STAT_RST,
  603. .stat_port_std_mib = RTL839X_STAT_PORT_STD_MIB,
  604. .traffic_enable = rtl839x_traffic_enable,
  605. .traffic_disable = rtl839x_traffic_disable,
  606. .traffic_get = rtl839x_traffic_get,
  607. .traffic_set = rtl839x_traffic_set,
  608. .port_iso_ctrl = rtl839x_port_iso_ctrl,
  609. .l2_ctrl_0 = RTL839X_L2_CTRL_0,
  610. .l2_ctrl_1 = RTL839X_L2_CTRL_1,
  611. .l2_port_aging_out = RTL839X_L2_PORT_AGING_OUT,
  612. .smi_poll_ctrl = RTL839X_SMI_PORT_POLLING_CTRL,
  613. .l2_tbl_flush_ctrl = RTL839X_L2_TBL_FLUSH_CTRL,
  614. .exec_tbl0_cmd = rtl839x_exec_tbl0_cmd,
  615. .exec_tbl1_cmd = rtl839x_exec_tbl1_cmd,
  616. .tbl_access_data_0 = rtl839x_tbl_access_data_0,
  617. .isr_glb_src = RTL839X_ISR_GLB_SRC,
  618. .isr_port_link_sts_chg = RTL839X_ISR_PORT_LINK_STS_CHG,
  619. .imr_port_link_sts_chg = RTL839X_IMR_PORT_LINK_STS_CHG,
  620. .imr_glb = RTL839X_IMR_GLB,
  621. .vlan_tables_read = rtl839x_vlan_tables_read,
  622. .vlan_set_tagged = rtl839x_vlan_set_tagged,
  623. .vlan_set_untagged = rtl839x_vlan_set_untagged,
  624. .vlan_profile_dump = rtl839x_vlan_profile_dump,
  625. .vlan_profile_setup = rtl839x_vlan_profile_setup,
  626. .vlan_fwd_on_inner = rtl839x_vlan_fwd_on_inner,
  627. .stp_get = rtl839x_stp_get,
  628. .stp_set = rtl839x_stp_set,
  629. .mac_force_mode_ctrl = rtl839x_mac_force_mode_ctrl,
  630. .mac_port_ctrl = rtl839x_mac_port_ctrl,
  631. .l2_port_new_salrn = rtl839x_l2_port_new_salrn,
  632. .l2_port_new_sa_fwd = rtl839x_l2_port_new_sa_fwd,
  633. .mir_ctrl = RTL839X_MIR_CTRL,
  634. .mir_dpm = RTL839X_MIR_DPM_CTRL,
  635. .mir_spm = RTL839X_MIR_SPM_CTRL,
  636. .mac_link_sts = RTL839X_MAC_LINK_STS,
  637. .mac_link_dup_sts = RTL839X_MAC_LINK_DUP_STS,
  638. .mac_link_spd_sts = rtl839x_mac_link_spd_sts,
  639. .mac_rx_pause_sts = RTL839X_MAC_RX_PAUSE_STS,
  640. .mac_tx_pause_sts = RTL839X_MAC_TX_PAUSE_STS,
  641. .read_l2_entry_using_hash = rtl839x_read_l2_entry_using_hash,
  642. .write_l2_entry_using_hash = rtl839x_write_l2_entry_using_hash,
  643. .read_cam = rtl839x_read_cam,
  644. .write_cam = rtl839x_write_cam,
  645. .vlan_port_egr_filter = RTL839X_VLAN_PORT_EGR_FLTR(0),
  646. .vlan_port_igr_filter = RTL839X_VLAN_PORT_IGR_FLTR(0),
  647. .vlan_port_pb = RTL839X_VLAN_PORT_PB_VLAN,
  648. .vlan_port_tag_sts_ctrl = RTL839X_VLAN_PORT_TAG_STS_CTRL,
  649. .trk_mbr_ctr = rtl839x_trk_mbr_ctr,
  650. .rma_bpdu_fld_pmask = RTL839X_RMA_BPDU_FLD_PMSK,
  651. .spcl_trap_eapol_ctrl = RTL839X_SPCL_TRAP_EAPOL_CTRL,
  652. .init_eee = rtl839x_init_eee,
  653. .port_eee_set = rtl839x_port_eee_set,
  654. .eee_port_ability = rtl839x_eee_port_ability,
  655. .l2_hash_seed = rtl839x_l2_hash_seed,
  656. .l2_hash_key = rtl839x_l2_hash_key,
  657. .read_mcast_pmask = rtl839x_read_mcast_pmask,
  658. .write_mcast_pmask = rtl839x_write_mcast_pmask,
  659. };