rtl839x.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514
  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. static inline void rtl839x_mask_port_reg_be(u64 clear, u64 set, int reg)
  6. {
  7. sw_w32_mask((u32)(clear >> 32), (u32)(set >> 32), reg);
  8. sw_w32_mask((u32)(clear & 0xffffffff), (u32)(set & 0xffffffff), reg + 4);
  9. }
  10. static inline u64 rtl839x_get_port_reg_be(int reg)
  11. {
  12. u64 v = sw_r32(reg);
  13. v <<= 32;
  14. v |= sw_r32(reg + 4);
  15. return v;
  16. }
  17. static inline void rtl839x_set_port_reg_be(u64 set, int reg)
  18. {
  19. sw_w32(set >> 32, reg);
  20. sw_w32(set & 0xffffffff, reg + 4);
  21. }
  22. static inline void rtl839x_mask_port_reg_le(u64 clear, u64 set, int reg)
  23. {
  24. sw_w32_mask((u32)clear, (u32)set, reg);
  25. sw_w32_mask((u32)(clear >> 32), (u32)(set >> 32), reg + 4);
  26. }
  27. static inline void rtl839x_set_port_reg_le(u64 set, int reg)
  28. {
  29. sw_w32(set, reg);
  30. sw_w32(set >> 32, reg + 4);
  31. }
  32. static inline u64 rtl839x_get_port_reg_le(int reg)
  33. {
  34. u64 v = sw_r32(reg + 4);
  35. v <<= 32;
  36. v |= sw_r32(reg);
  37. return v;
  38. }
  39. static inline int rtl839x_stat_port_std_mib(int p)
  40. {
  41. return RTL839X_STAT_PORT_STD_MIB + (p << 8);
  42. }
  43. static inline int rtl839x_port_iso_ctrl(int p)
  44. {
  45. return RTL839X_PORT_ISO_CTRL(p);
  46. }
  47. static inline void rtl839x_exec_tbl0_cmd(u32 cmd)
  48. {
  49. sw_w32(cmd, RTL839X_TBL_ACCESS_CTRL_0);
  50. do { } while (sw_r32(RTL839X_TBL_ACCESS_CTRL_0) & BIT(16));
  51. }
  52. static inline void rtl839x_exec_tbl1_cmd(u32 cmd)
  53. {
  54. sw_w32(cmd, RTL839X_TBL_ACCESS_CTRL_1);
  55. do { } while (sw_r32(RTL839X_TBL_ACCESS_CTRL_1) & BIT(16));
  56. }
  57. static inline int rtl839x_tbl_access_data_0(int i)
  58. {
  59. return RTL839X_TBL_ACCESS_DATA_0(i);
  60. }
  61. static void rtl839x_vlan_tables_read(u32 vlan, struct rtl838x_vlan_info *info)
  62. {
  63. u32 cmd;
  64. u64 v;
  65. u32 u, w;
  66. cmd = BIT(16) /* Execute cmd */
  67. | 0 << 15 /* Read */
  68. | 0 << 12 /* Table type 0b000 */
  69. | (vlan & 0xfff);
  70. rtl839x_exec_tbl0_cmd(cmd);
  71. v = sw_r32(RTL838X_TBL_ACCESS_DATA_0(0));
  72. v <<= 32;
  73. u = sw_r32(RTL838X_TBL_ACCESS_DATA_0(1));
  74. v |= u;
  75. info->tagged_ports = v >> 11;
  76. w = sw_r32(RTL838X_TBL_ACCESS_DATA_0(2));
  77. info->profile_id = w >> 30 | ((u & 1) << 2);
  78. info->hash_mc_fid = !!(u & 2);
  79. info->hash_uc_fid = !!(u & 4);
  80. info->fid = (u >> 3) & 0xff;
  81. cmd = BIT(16) /* Execute cmd */
  82. | 0 << 15 /* Read */
  83. | 0 << 12 /* Table type 0b000 */
  84. | (vlan & 0xfff);
  85. rtl839x_exec_tbl1_cmd(cmd);
  86. v = sw_r32(RTL838X_TBL_ACCESS_DATA_1(0));
  87. v <<= 32;
  88. v |= sw_r32(RTL838X_TBL_ACCESS_DATA_1(1));
  89. info->untagged_ports = v >> 11;
  90. }
  91. static void rtl839x_vlan_set_tagged(u32 vlan, struct rtl838x_vlan_info *info)
  92. {
  93. u32 cmd = BIT(16) /* Execute cmd */
  94. | BIT(15) /* Write */
  95. | 0 << 12 /* Table type 0b00 */
  96. | (vlan & 0xfff);
  97. u32 w;
  98. u64 v = info->tagged_ports << 11;
  99. v |= info->profile_id >> 2;
  100. v |= info->hash_mc_fid ? 2 : 0;
  101. v |= info->hash_uc_fid ? 4 : 0;
  102. v |= ((u32)info->fid) << 3;
  103. rtl839x_set_port_reg_be(v, RTL838X_TBL_ACCESS_DATA_0(0));
  104. w = info->profile_id;
  105. sw_w32(w << 30, RTL838X_TBL_ACCESS_DATA_0(2));
  106. rtl839x_exec_tbl0_cmd(cmd);
  107. }
  108. static void rtl839x_vlan_set_untagged(u32 vlan, u64 portmask)
  109. {
  110. u32 cmd = BIT(16) /* Execute cmd */
  111. | BIT(15) /* Write */
  112. | 0 << 12 /* Table type 0b00 */
  113. | (vlan & 0xfff);
  114. rtl839x_set_port_reg_be(portmask << 11, RTL838X_TBL_ACCESS_DATA_1(0));
  115. rtl839x_exec_tbl1_cmd(cmd);
  116. }
  117. static inline int rtl839x_mac_force_mode_ctrl(int p)
  118. {
  119. return RTL839X_MAC_FORCE_MODE_CTRL + (p << 2);
  120. }
  121. static inline int rtl839x_mac_port_ctrl(int p)
  122. {
  123. return RTL839X_MAC_PORT_CTRL(p);
  124. }
  125. static inline int rtl839x_l2_port_new_salrn(int p)
  126. {
  127. return RTL839X_L2_PORT_NEW_SALRN(p);
  128. }
  129. static inline int rtl839x_l2_port_new_sa_fwd(int p)
  130. {
  131. return RTL839X_L2_PORT_NEW_SA_FWD(p);
  132. }
  133. static inline int rtl839x_mir_ctrl(int group)
  134. {
  135. return RTL839X_MIR_CTRL(group);
  136. }
  137. static inline int rtl839x_mir_dpm(int group)
  138. {
  139. return RTL839X_MIR_DPM_CTRL(group);
  140. }
  141. static inline int rtl839x_mir_spm(int group)
  142. {
  143. return RTL839X_MIR_SPM_CTRL(group);
  144. }
  145. static inline int rtl839x_mac_link_spd_sts(int p)
  146. {
  147. return RTL839X_MAC_LINK_SPD_STS(p);
  148. }
  149. static u64 rtl839x_read_l2_entry_using_hash(u32 hash, u32 position, struct rtl838x_l2_entry *e)
  150. {
  151. u64 entry;
  152. u32 r[3];
  153. /* Search in SRAM, with hash and at position in hash bucket (0-3) */
  154. u32 idx = (0 << 14) | (hash << 2) | position;
  155. u32 cmd = BIT(17) /* Execute cmd */
  156. | 0 << 16 /* Read */
  157. | 0 << 14 /* Table type 0b00 */
  158. | (idx & 0x3fff);
  159. sw_w32(cmd, RTL839X_TBL_ACCESS_L2_CTRL);
  160. do { } while (sw_r32(RTL839X_TBL_ACCESS_L2_CTRL) & BIT(17));
  161. r[0] = sw_r32(RTL839X_TBL_ACCESS_L2_DATA(0));
  162. r[1] = sw_r32(RTL839X_TBL_ACCESS_L2_DATA(1));
  163. r[2] = sw_r32(RTL839X_TBL_ACCESS_L2_DATA(2));
  164. /* Table contains different entry types, we need to identify the right one:
  165. * Check for MC entries, first
  166. */
  167. e->is_ip_mc = !!(r[2] & BIT(31));
  168. e->is_ipv6_mc = !!(r[2] & BIT(30));
  169. e->type = L2_INVALID;
  170. if (!e->is_ip_mc) {
  171. e->mac[0] = (r[0] >> 12);
  172. e->mac[1] = (r[0] >> 4);
  173. e->mac[2] = ((r[1] >> 28) | (r[0] << 4));
  174. e->mac[3] = (r[1] >> 20);
  175. e->mac[4] = (r[1] >> 12);
  176. e->mac[5] = (r[1] >> 4);
  177. /* Is it a unicast entry? check multicast bit */
  178. if (!(e->mac[0] & 1)) {
  179. e->is_static = !!((r[2] >> 18) & 1);
  180. e->vid = (r[2] >> 4) & 0xfff;
  181. e->rvid = (r[0] >> 20) & 0xfff;
  182. e->port = (r[2] >> 24) & 0x3f;
  183. e->block_da = !!(r[2] & BIT(19));
  184. e->block_sa = !!(r[2] & BIT(20));
  185. e->suspended = !!(r[2] & BIT(17));
  186. e->next_hop = !!(r[2] & BIT(16));
  187. if (e->next_hop)
  188. pr_debug("Found next hop entry, need to read data\n");
  189. e->age = (r[2] >> 21) & 3;
  190. e->valid = true;
  191. if (!(r[2] & 0xc0fd0000)) /* Check for valid entry */
  192. e->valid = false;
  193. else
  194. e->type = L2_UNICAST;
  195. } else {
  196. e->valid = true;
  197. e->type = L2_MULTICAST;
  198. e->mc_portmask_index = (r[2]>>6) & 0xfff;
  199. }
  200. }
  201. if (e->is_ip_mc) {
  202. e->valid = true;
  203. e->type = IP4_MULTICAST;
  204. }
  205. if (e->is_ipv6_mc) {
  206. e->valid = true;
  207. e->type = IP6_MULTICAST;
  208. }
  209. entry = (((u64) r[0]) << 12) | ((r[1] & 0xfffffff0) << 12) | ((r[2] >> 4) & 0xfff);
  210. return entry;
  211. }
  212. static u64 rtl839x_read_cam(int idx, struct rtl838x_l2_entry *e)
  213. {
  214. u64 entry;
  215. u32 r[3];
  216. u32 cmd = BIT(17) /* Execute cmd */
  217. | 0 << 16 /* Read */
  218. | BIT(14) /* Table type 0b01 */
  219. | (idx & 0x3f);
  220. sw_w32(cmd, RTL839X_TBL_ACCESS_L2_CTRL);
  221. do { } while (sw_r32(RTL839X_TBL_ACCESS_L2_CTRL) & BIT(17));
  222. r[0] = sw_r32(RTL839X_TBL_ACCESS_L2_DATA(0));
  223. r[1] = sw_r32(RTL839X_TBL_ACCESS_L2_DATA(1));
  224. r[2] = sw_r32(RTL839X_TBL_ACCESS_L2_DATA(2));
  225. e->mac[0] = (r[0] >> 12);
  226. e->mac[1] = (r[0] >> 4);
  227. e->mac[2] = ((r[1] >> 28) | (r[0] << 4));
  228. e->mac[3] = (r[1] >> 20);
  229. e->mac[4] = (r[1] >> 12);
  230. e->mac[5] = (r[1] >> 4);
  231. e->is_static = !!((r[2] >> 18) & 1);
  232. e->vid = (r[2] >> 4) & 0xfff;
  233. e->rvid = (r[0] >> 20) & 0xfff;
  234. e->port = (r[2] >> 24) & 0x3f;
  235. e->valid = true;
  236. if (!(r[2] & 0x10fd0000)) /* Check for invalid entry */
  237. e->valid = false;
  238. if (e->valid)
  239. pr_debug("Found in CAM: R1 %x R2 %x R3 %x\n", r[0], r[1], r[2]);
  240. entry = (((u64) r[0]) << 12) | ((r[1] & 0xfffffff0) << 12) | ((r[2] >> 4) & 0xfff);
  241. return entry;
  242. }
  243. static inline int rtl839x_vlan_profile(int profile)
  244. {
  245. return RTL839X_VLAN_PROFILE(profile);
  246. }
  247. static inline int rtl839x_vlan_port_egr_filter(int port)
  248. {
  249. return RTL839X_VLAN_PORT_EGR_FLTR(port);
  250. }
  251. static inline int rtl839x_vlan_port_igr_filter(int port)
  252. {
  253. return RTL839X_VLAN_PORT_IGR_FLTR(port);
  254. }
  255. static inline int rtl839x_vlan_port_pb(int port)
  256. {
  257. return RTL839X_VLAN_PORT_PB_VLAN(port);
  258. }
  259. static inline int rtl839x_vlan_port_tag_sts_ctrl(int port)
  260. {
  261. return RTL839X_VLAN_PORT_TAG_STS_CTRL(port);
  262. }
  263. const struct rtl838x_reg rtl839x_reg = {
  264. .mask_port_reg_be = rtl839x_mask_port_reg_be,
  265. .set_port_reg_be = rtl839x_set_port_reg_be,
  266. .get_port_reg_be = rtl839x_get_port_reg_be,
  267. .mask_port_reg_le = rtl839x_mask_port_reg_le,
  268. .set_port_reg_le = rtl839x_set_port_reg_le,
  269. .get_port_reg_le = rtl839x_get_port_reg_le,
  270. .stat_port_rst = RTL839X_STAT_PORT_RST,
  271. .stat_rst = RTL839X_STAT_RST,
  272. .stat_port_std_mib = rtl839x_stat_port_std_mib,
  273. .port_iso_ctrl = rtl839x_port_iso_ctrl,
  274. .l2_ctrl_0 = RTL839X_L2_CTRL_0,
  275. .l2_ctrl_1 = RTL839X_L2_CTRL_1,
  276. .l2_port_aging_out = RTL839X_L2_PORT_AGING_OUT,
  277. .smi_poll_ctrl = RTL839X_SMI_PORT_POLLING_CTRL,
  278. .l2_tbl_flush_ctrl = RTL839X_L2_TBL_FLUSH_CTRL,
  279. .exec_tbl0_cmd = rtl839x_exec_tbl0_cmd,
  280. .exec_tbl1_cmd = rtl839x_exec_tbl1_cmd,
  281. .tbl_access_data_0 = rtl839x_tbl_access_data_0,
  282. .isr_glb_src = RTL839X_ISR_GLB_SRC,
  283. .isr_port_link_sts_chg = RTL839X_ISR_PORT_LINK_STS_CHG,
  284. .imr_port_link_sts_chg = RTL839X_IMR_PORT_LINK_STS_CHG,
  285. .imr_glb = RTL839X_IMR_GLB,
  286. .vlan_tables_read = rtl839x_vlan_tables_read,
  287. .vlan_set_tagged = rtl839x_vlan_set_tagged,
  288. .vlan_set_untagged = rtl839x_vlan_set_untagged,
  289. .mac_force_mode_ctrl = rtl839x_mac_force_mode_ctrl,
  290. .mac_port_ctrl = rtl839x_mac_port_ctrl,
  291. .l2_port_new_salrn = rtl839x_l2_port_new_salrn,
  292. .l2_port_new_sa_fwd = rtl839x_l2_port_new_sa_fwd,
  293. .mir_ctrl = rtl839x_mir_ctrl,
  294. .mir_dpm = rtl839x_mir_dpm,
  295. .mir_spm = rtl839x_mir_spm,
  296. .mac_link_sts = RTL839X_MAC_LINK_STS,
  297. .mac_link_dup_sts = RTL839X_MAC_LINK_DUP_STS,
  298. .mac_link_spd_sts = rtl839x_mac_link_spd_sts,
  299. .mac_rx_pause_sts = RTL839X_MAC_RX_PAUSE_STS,
  300. .mac_tx_pause_sts = RTL839X_MAC_TX_PAUSE_STS,
  301. .read_l2_entry_using_hash = rtl839x_read_l2_entry_using_hash,
  302. .read_cam = rtl839x_read_cam,
  303. .vlan_profile = rtl839x_vlan_profile,
  304. .vlan_port_egr_filter = rtl839x_vlan_port_egr_filter,
  305. .vlan_port_igr_filter = rtl839x_vlan_port_igr_filter,
  306. .vlan_port_pb = rtl839x_vlan_port_pb,
  307. .vlan_port_tag_sts_ctrl = rtl839x_vlan_port_tag_sts_ctrl,
  308. };
  309. irqreturn_t rtl839x_switch_irq(int irq, void *dev_id)
  310. {
  311. struct dsa_switch *ds = dev_id;
  312. u32 status = sw_r32(RTL839X_ISR_GLB_SRC);
  313. u64 ports = rtl839x_get_port_reg_le(RTL839X_ISR_PORT_LINK_STS_CHG);
  314. u64 link;
  315. int i;
  316. /* Clear status */
  317. rtl839x_set_port_reg_le(ports, RTL839X_ISR_PORT_LINK_STS_CHG);
  318. pr_debug("RTL8390 Link change: status: %x, ports %llx\n", status, ports);
  319. for (i = 0; i < 52; i++) {
  320. if (ports & (1ULL << i)) {
  321. link = rtl839x_get_port_reg_le(RTL839X_MAC_LINK_STS);
  322. if (link & (1ULL << i))
  323. dsa_port_phylink_mac_change(ds, i, true);
  324. else
  325. dsa_port_phylink_mac_change(ds, i, false);
  326. }
  327. }
  328. return IRQ_HANDLED;
  329. }
  330. // TODO: unused
  331. int rtl8390_sds_power(int mac, int val)
  332. {
  333. u32 offset = (mac == 48) ? 0x0 : 0x100;
  334. u32 mode = val ? 0 : 1;
  335. pr_debug("In %s: mac %d, set %d\n", __func__, mac, val);
  336. if ((mac != 48) && (mac != 49)) {
  337. pr_err("%s: not an SFP port: %d\n", __func__, mac);
  338. return -1;
  339. }
  340. // Set bit 1003. 1000 starts at 7c
  341. sw_w32_mask(BIT(11), mode << 11, RTL839X_SDS12_13_PWR0 + offset);
  342. return 0;
  343. }
  344. int rtl839x_read_phy(u32 port, u32 page, u32 reg, u32 *val)
  345. {
  346. u32 v;
  347. if (port > 63 || page > 4095 || reg > 31)
  348. return -ENOTSUPP;
  349. mutex_lock(&smi_lock);
  350. sw_w32_mask(0xffff0000, port << 16, RTL839X_PHYREG_DATA_CTRL);
  351. v = reg << 5 | page << 10 | ((page == 0x1fff) ? 0x1f : 0) << 23;
  352. sw_w32(v, RTL839X_PHYREG_ACCESS_CTRL);
  353. sw_w32(0x1ff, RTL839X_PHYREG_CTRL);
  354. v |= 1;
  355. sw_w32(v, RTL839X_PHYREG_ACCESS_CTRL);
  356. do {
  357. } while (sw_r32(RTL839X_PHYREG_ACCESS_CTRL) & 0x1);
  358. *val = sw_r32(RTL839X_PHYREG_DATA_CTRL) & 0xffff;
  359. mutex_unlock(&smi_lock);
  360. return 0;
  361. }
  362. int rtl839x_write_phy(u32 port, u32 page, u32 reg, u32 val)
  363. {
  364. u32 v;
  365. int err = 0;
  366. val &= 0xffff;
  367. if (port > 63 || page > 4095 || reg > 31)
  368. return -ENOTSUPP;
  369. mutex_lock(&smi_lock);
  370. /* Clear both port registers */
  371. sw_w32(0, RTL839X_PHYREG_PORT_CTRL(0));
  372. sw_w32(0, RTL839X_PHYREG_PORT_CTRL(0) + 4);
  373. sw_w32_mask(0, BIT(port), RTL839X_PHYREG_PORT_CTRL(port));
  374. sw_w32_mask(0xffff0000, val << 16, RTL839X_PHYREG_DATA_CTRL);
  375. v = reg << 5 | page << 10 | ((page == 0x1fff) ? 0x1f : 0) << 23;
  376. sw_w32(v, RTL839X_PHYREG_ACCESS_CTRL);
  377. sw_w32(0x1ff, RTL839X_PHYREG_CTRL);
  378. v |= BIT(3) | 1; /* Write operation and execute */
  379. sw_w32(v, RTL839X_PHYREG_ACCESS_CTRL);
  380. do {
  381. } while (sw_r32(RTL839X_PHYREG_ACCESS_CTRL) & 0x1);
  382. if (sw_r32(RTL839X_PHYREG_ACCESS_CTRL) & 0x2)
  383. err = -EIO;
  384. mutex_unlock(&smi_lock);
  385. return err;
  386. }
  387. void rtl8390_get_version(struct rtl838x_switch_priv *priv)
  388. {
  389. u32 info;
  390. sw_w32_mask(0xf << 28, 0xa << 28, RTL839X_CHIP_INFO);
  391. info = sw_r32(RTL839X_CHIP_INFO);
  392. pr_debug("Chip-Info: %x\n", info);
  393. priv->version = RTL8390_VERSION_A;
  394. }
  395. u32 rtl839x_hash(struct rtl838x_switch_priv *priv, u64 seed)
  396. {
  397. u32 h1, h2, h;
  398. if (sw_r32(priv->r->l2_ctrl_0) & 1) {
  399. h1 = (u32) (((seed >> 60) & 0x3f) ^ ((seed >> 54) & 0x3f)
  400. ^ ((seed >> 36) & 0x3f) ^ ((seed >> 30) & 0x3f)
  401. ^ ((seed >> 12) & 0x3f) ^ ((seed >> 6) & 0x3f));
  402. h2 = (u32) (((seed >> 48) & 0x3f) ^ ((seed >> 42) & 0x3f)
  403. ^ ((seed >> 24) & 0x3f) ^ ((seed >> 18) & 0x3f)
  404. ^ (seed & 0x3f));
  405. h = (h1 << 6) | h2;
  406. } else {
  407. h = (seed >> 60)
  408. ^ ((((seed >> 48) & 0x3f) << 6) | ((seed >> 54) & 0x3f))
  409. ^ ((seed >> 36) & 0xfff) ^ ((seed >> 24) & 0xfff)
  410. ^ ((seed >> 12) & 0xfff) ^ (seed & 0xfff);
  411. }
  412. return h;
  413. }
  414. void rtl839x_vlan_profile_dump(int index)
  415. {
  416. u32 profile, profile1;
  417. if (index < 0 || index > 7)
  418. return;
  419. profile1 = sw_r32(RTL839X_VLAN_PROFILE(index) + 4);
  420. profile = sw_r32(RTL839X_VLAN_PROFILE(index));
  421. pr_debug("VLAN %d: L2 learning: %d, L2 Unknown MultiCast Field %x, \
  422. IPv4 Unknown MultiCast Field %x, IPv6 Unknown MultiCast Field: %x",
  423. index, profile & 1, (profile >> 1) & 0xfff, (profile >> 13) & 0xfff,
  424. (profile1) & 0xfff);
  425. }