rtl931x.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  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. inline void rtl931x_exec_tbl0_cmd(u32 cmd)
  7. {
  8. sw_w32(cmd, RTL931X_TBL_ACCESS_CTRL_0);
  9. do { } while (sw_r32(RTL931X_TBL_ACCESS_CTRL_0) & (1 << 20));
  10. }
  11. inline void rtl931x_exec_tbl1_cmd(u32 cmd)
  12. {
  13. sw_w32(cmd, RTL931X_TBL_ACCESS_CTRL_1);
  14. do { } while (sw_r32(RTL931X_TBL_ACCESS_CTRL_1) & (1 << 17));
  15. }
  16. inline int rtl931x_tbl_access_data_0(int i)
  17. {
  18. return RTL931X_TBL_ACCESS_DATA_0(i);
  19. }
  20. void rtl931x_vlan_profile_dump(int index)
  21. {
  22. u64 profile[4];
  23. if (index < 0 || index > 15)
  24. return;
  25. profile[0] = sw_r32(RTL931X_VLAN_PROFILE_SET(index));
  26. profile[1] = (sw_r32(RTL931X_VLAN_PROFILE_SET(index) + 4) & 0x1FFFFFFFULL) << 32
  27. | (sw_r32(RTL931X_VLAN_PROFILE_SET(index) + 8) & 0xFFFFFFFF);
  28. profile[2] = (sw_r32(RTL931X_VLAN_PROFILE_SET(index) + 16) & 0xFFFFFFFFULL) << 32
  29. | (sw_r32(RTL931X_VLAN_PROFILE_SET(index) + 12) & 0x1FFFFFFULL);
  30. profile[3] = (sw_r32(RTL931X_VLAN_PROFILE_SET(index) + 20) & 0x1FFFFFFFULL) << 32
  31. | (sw_r32(RTL931X_VLAN_PROFILE_SET(index) + 24) & 0xFFFFFFFF);
  32. pr_info("VLAN %d: L2 learning: %d, L2 Unknown MultiCast Field %llx, \
  33. IPv4 Unknown MultiCast Field %llx, IPv6 Unknown MultiCast Field: %llx",
  34. index, (u32) (profile[0] & (3 << 14)), profile[1], profile[2], profile[3]);
  35. }
  36. static void rtl931x_stp_get(struct rtl838x_switch_priv *priv, u16 msti, u32 port_state[])
  37. {
  38. int i;
  39. u32 cmd = 1 << 20 /* Execute cmd */
  40. | 0 << 19 /* Read */
  41. | 2 << 15 /* Table type 0b10 */
  42. | (msti & 0x3fff);
  43. priv->r->exec_tbl0_cmd(cmd);
  44. for (i = 0; i < 4; i++)
  45. port_state[i] = sw_r32(priv->r->tbl_access_data_0(i));
  46. }
  47. static void rtl931x_stp_set(struct rtl838x_switch_priv *priv, u16 msti, u32 port_state[])
  48. {
  49. int i;
  50. u32 cmd = 1 << 20 /* Execute cmd */
  51. | 1 << 19 /* Write */
  52. | 5 << 15 /* Table type 0b101 */
  53. | (msti & 0x3fff);
  54. for (i = 0; i < 4; i++)
  55. sw_w32(port_state[i], priv->r->tbl_access_data_0(i));
  56. priv->r->exec_tbl0_cmd(cmd);
  57. }
  58. inline static int rtl931x_trk_mbr_ctr(int group)
  59. {
  60. return RTL931X_TRK_MBR_CTRL + (group << 2);
  61. }
  62. static void rtl931x_vlan_tables_read(u32 vlan, struct rtl838x_vlan_info *info)
  63. {
  64. u32 v, w, x, y;
  65. // Read VLAN table (3) via register 0
  66. struct table_reg *r = rtl_table_get(RTL9310_TBL_0, 3);
  67. rtl_table_read(r, vlan);
  68. v = sw_r32(rtl_table_data(r, 0));
  69. w = sw_r32(rtl_table_data(r, 1));
  70. x = sw_r32(rtl_table_data(r, 2));
  71. y = sw_r32(rtl_table_data(r, 3));
  72. pr_debug("VLAN_READ %d: %08x %08x\n", vlan, v, w);
  73. rtl_table_release(r);
  74. info->tagged_ports = ((u64) v) << 25 | (w >> 7);
  75. info->profile_id = (x >> 16) & 0xf;
  76. info->hash_mc_fid = !!(x & BIT(30));
  77. info->hash_uc_fid = !!(x & BIT(31));
  78. info->fid = w & 0x7f;
  79. // TODO: use also info in 4th register
  80. // Read UNTAG table via table register 3
  81. r = rtl_table_get(RTL9310_TBL_3, 0);
  82. rtl_table_read(r, vlan);
  83. v = ((u64)sw_r32(rtl_table_data(r, 0))) << 25;
  84. v |= sw_r32(rtl_table_data(r, 1)) >> 7;
  85. rtl_table_release(r);
  86. info->untagged_ports = v;
  87. }
  88. static void rtl931x_vlan_set_tagged(u32 vlan, struct rtl838x_vlan_info *info)
  89. {
  90. u32 v, w, x;
  91. // Access VLAN table (1) via register 0
  92. struct table_reg *r = rtl_table_get(RTL9310_TBL_0, 3);
  93. v = info->tagged_ports << 7;
  94. w = (info->tagged_ports & 0x7f000000) << 25;
  95. w |= (u32)info->fid;
  96. x = info->profile_id << 16;
  97. w |= info->hash_mc_fid ? BIT(30) : 0;
  98. w |= info->hash_uc_fid ? BIT(31) : 0;
  99. // TODO: use also info in 4th register
  100. sw_w32(v, rtl_table_data(r, 0));
  101. sw_w32(w, rtl_table_data(r, 1));
  102. sw_w32(x, rtl_table_data(r, 2));
  103. rtl_table_write(r, vlan);
  104. rtl_table_release(r);
  105. }
  106. static void rtl931x_vlan_set_untagged(u32 vlan, u64 portmask)
  107. {
  108. struct table_reg *r = rtl_table_get(RTL9310_TBL_3, 0);
  109. rtl839x_set_port_reg_be(portmask << 7, rtl_table_data(r, 0));
  110. rtl_table_write(r, vlan);
  111. rtl_table_release(r);
  112. }
  113. static inline int rtl931x_mac_force_mode_ctrl(int p)
  114. {
  115. return RTL931X_MAC_FORCE_MODE_CTRL + (p << 2);
  116. }
  117. static inline int rtl931x_mac_link_spd_sts(int p)
  118. {
  119. return RTL931X_MAC_LINK_SPD_STS(p);
  120. }
  121. static inline int rtl931x_mac_port_ctrl(int p)
  122. {
  123. return RTL931X_MAC_PORT_CTRL(p);
  124. }
  125. static inline int rtl931x_l2_port_new_salrn(int p)
  126. {
  127. return RTL931X_L2_PORT_NEW_SALRN(p);
  128. }
  129. static inline int rtl931x_l2_port_new_sa_fwd(int p)
  130. {
  131. return RTL931X_L2_PORT_NEW_SA_FWD(p);
  132. }
  133. static u64 rtl931x_read_l2_entry_using_hash(u32 hash, u32 position, struct rtl838x_l2_entry *e)
  134. {
  135. u64 entry = 0;
  136. // TODO: Implement
  137. return entry;
  138. }
  139. static u64 rtl931x_read_cam(int idx, struct rtl838x_l2_entry *e)
  140. {
  141. u64 entry = 0;
  142. // TODO: Implement
  143. return entry;
  144. }
  145. irqreturn_t rtl931x_switch_irq(int irq, void *dev_id)
  146. {
  147. struct dsa_switch *ds = dev_id;
  148. u32 status = sw_r32(RTL931X_ISR_GLB_SRC);
  149. u64 ports = rtl839x_get_port_reg_le(RTL931X_ISR_PORT_LINK_STS_CHG);
  150. u64 link;
  151. int i;
  152. /* Clear status */
  153. rtl839x_set_port_reg_le(ports, RTL931X_ISR_PORT_LINK_STS_CHG);
  154. pr_info("RTL9310 Link change: status: %x, ports %llx\n", status, ports);
  155. for (i = 0; i < 56; i++) {
  156. if (ports & BIT_ULL(i)) {
  157. link = rtl839x_get_port_reg_le(RTL931X_MAC_LINK_STS);
  158. if (link & BIT_ULL(i))
  159. dsa_port_phylink_mac_change(ds, i, true);
  160. else
  161. dsa_port_phylink_mac_change(ds, i, false);
  162. }
  163. }
  164. return IRQ_HANDLED;
  165. }
  166. int rtl931x_write_phy(u32 port, u32 page, u32 reg, u32 val)
  167. {
  168. u32 v;
  169. int err = 0;
  170. val &= 0xffff;
  171. if (port > 63 || page > 4095 || reg > 31)
  172. return -ENOTSUPP;
  173. mutex_lock(&smi_lock);
  174. /* Clear both port registers */
  175. sw_w32(0, RTL931X_SMI_INDRT_ACCESS_CTRL_2);
  176. sw_w32(0, RTL931X_SMI_INDRT_ACCESS_CTRL_2 + 4);
  177. sw_w32_mask(0, BIT(port), RTL931X_SMI_INDRT_ACCESS_CTRL_2+ (port % 32) * 4);
  178. sw_w32_mask(0xffff0000, val << 16, RTL931X_SMI_INDRT_ACCESS_CTRL_3);
  179. v = reg << 6 | page << 11 ;
  180. sw_w32(v, RTL931X_SMI_INDRT_ACCESS_CTRL_0);
  181. sw_w32(0x1ff, RTL931X_SMI_INDRT_ACCESS_CTRL_1);
  182. v |= 1 << 3 | 1; /* Write operation and execute */
  183. sw_w32(v, RTL931X_SMI_INDRT_ACCESS_CTRL_0);
  184. do {
  185. } while (sw_r32(RTL931X_SMI_INDRT_ACCESS_CTRL_0) & 0x1);
  186. if (sw_r32(RTL931X_SMI_INDRT_ACCESS_CTRL_0) & 0x2)
  187. err = -EIO;
  188. mutex_unlock(&smi_lock);
  189. return err;
  190. }
  191. int rtl931x_read_phy(u32 port, u32 page, u32 reg, u32 *val)
  192. {
  193. u32 v;
  194. if (port > 63 || page > 4095 || reg > 31)
  195. return -ENOTSUPP;
  196. mutex_lock(&smi_lock);
  197. sw_w32_mask(0xffff, port, RTL931X_SMI_INDRT_ACCESS_CTRL_3);
  198. v = reg << 6 | page << 11; // TODO: ACCESS Offset? Park page
  199. sw_w32(v, RTL931X_SMI_INDRT_ACCESS_CTRL_0);
  200. sw_w32(0x1ff, RTL931X_SMI_INDRT_ACCESS_CTRL_1);
  201. v |= 1;
  202. sw_w32(v, RTL931X_SMI_INDRT_ACCESS_CTRL_0);
  203. do {
  204. } while (sw_r32(RTL931X_SMI_INDRT_ACCESS_CTRL_0) & 0x1);
  205. *val = (sw_r32(RTL931X_SMI_INDRT_ACCESS_CTRL_3) & 0xffff0000) >> 16;
  206. pr_info("%s: port %d, page: %d, reg: %x, val: %x\n", __func__, port, page, reg, *val);
  207. mutex_unlock(&smi_lock);
  208. return 0;
  209. }
  210. /*
  211. * Read an mmd register of the PHY
  212. */
  213. int rtl931x_read_mmd_phy(u32 port, u32 devnum, u32 regnum, u32 *val)
  214. {
  215. int err = 0;
  216. u32 v;
  217. int type = 1; // TODO: For C45 PHYs need to set to 2
  218. mutex_lock(&smi_lock);
  219. // Set PHY to access via port-number
  220. sw_w32(port << 5, RTL931X_SMI_INDRT_ACCESS_BC_PHYID_CTRL);
  221. // Set MMD device number and register to write to
  222. sw_w32(devnum << 16 | (regnum & 0xffff), RTL931X_SMI_INDRT_ACCESS_MMD_CTRL);
  223. v = type << 2 | BIT(0); // MMD-access-type | EXEC
  224. sw_w32(v, RTL931X_SMI_INDRT_ACCESS_CTRL_0);
  225. do {
  226. v = sw_r32(RTL931X_SMI_INDRT_ACCESS_CTRL_0);
  227. } while (v & BIT(0));
  228. // There is no error-checking via BIT 1 of v, as it does not seem to be set correctly
  229. *val = (sw_r32(RTL931X_SMI_INDRT_ACCESS_CTRL_3) & 0xffff);
  230. pr_debug("%s: port %d, regnum: %x, val: %x (err %d)\n", __func__, port, regnum, *val, err);
  231. mutex_unlock(&smi_lock);
  232. return err;
  233. }
  234. /*
  235. * Write to an mmd register of the PHY
  236. */
  237. int rtl931x_write_mmd_phy(u32 port, u32 devnum, u32 regnum, u32 val)
  238. {
  239. int err = 0;
  240. u32 v;
  241. int type = 1; // TODO: For C45 PHYs need to set to 2
  242. mutex_lock(&smi_lock);
  243. // Set PHY to access via port-number
  244. sw_w32(port << 5, RTL931X_SMI_INDRT_ACCESS_BC_PHYID_CTRL);
  245. // Set data to write
  246. sw_w32_mask(0xffff << 16, val << 16, RTL931X_SMI_INDRT_ACCESS_CTRL_3);
  247. // Set MMD device number and register to write to
  248. sw_w32(devnum << 16 | (regnum & 0xffff), RTL931X_SMI_INDRT_ACCESS_MMD_CTRL);
  249. v = BIT(4) | type << 2 | BIT(0); // WRITE | MMD-access-type | EXEC
  250. sw_w32(v, RTL931X_SMI_INDRT_ACCESS_CTRL_0);
  251. do {
  252. v = sw_r32(RTL931X_SMI_INDRT_ACCESS_CTRL_0);
  253. } while (v & BIT(0));
  254. pr_debug("%s: port %d, regnum: %x, val: %x (err %d)\n", __func__, port, regnum, val, err);
  255. mutex_unlock(&smi_lock);
  256. return err;
  257. }
  258. void rtl931x_print_matrix(void)
  259. {
  260. volatile u64 *ptr = RTL838X_SW_BASE + RTL839X_PORT_ISO_CTRL(0);
  261. int i;
  262. for (i = 0; i < 52; i += 4)
  263. pr_info("> %16llx %16llx %16llx %16llx\n",
  264. ptr[i + 0], ptr[i + 1], ptr[i + 2], ptr[i + 3]);
  265. pr_info("CPU_PORT> %16llx\n", ptr[52]);
  266. }
  267. const struct rtl838x_reg rtl931x_reg = {
  268. .mask_port_reg_be = rtl839x_mask_port_reg_be,
  269. .set_port_reg_be = rtl839x_set_port_reg_be,
  270. .get_port_reg_be = rtl839x_get_port_reg_be,
  271. .mask_port_reg_le = rtl839x_mask_port_reg_le,
  272. .set_port_reg_le = rtl839x_set_port_reg_le,
  273. .get_port_reg_le = rtl839x_get_port_reg_le,
  274. .stat_port_rst = RTL931X_STAT_PORT_RST,
  275. .stat_rst = RTL931X_STAT_RST,
  276. .stat_port_std_mib = 0, // Not defined
  277. .l2_ctrl_0 = RTL931X_L2_CTRL,
  278. .l2_ctrl_1 = RTL931X_L2_AGE_CTRL,
  279. .l2_port_aging_out = RTL931X_L2_PORT_AGE_CTRL,
  280. // .smi_poll_ctrl does not exist
  281. .l2_tbl_flush_ctrl = RTL931X_L2_TBL_FLUSH_CTRL,
  282. .exec_tbl0_cmd = rtl931x_exec_tbl0_cmd,
  283. .exec_tbl1_cmd = rtl931x_exec_tbl1_cmd,
  284. .tbl_access_data_0 = rtl931x_tbl_access_data_0,
  285. .isr_glb_src = RTL931X_ISR_GLB_SRC,
  286. .isr_port_link_sts_chg = RTL931X_ISR_PORT_LINK_STS_CHG,
  287. .imr_port_link_sts_chg = RTL931X_IMR_PORT_LINK_STS_CHG,
  288. // imr_glb does not exist on RTL931X
  289. .vlan_tables_read = rtl931x_vlan_tables_read,
  290. .vlan_set_tagged = rtl931x_vlan_set_tagged,
  291. .vlan_set_untagged = rtl931x_vlan_set_untagged,
  292. .vlan_profile_dump = rtl931x_vlan_profile_dump,
  293. .stp_get = rtl931x_stp_get,
  294. .stp_set = rtl931x_stp_set,
  295. .mac_force_mode_ctrl = rtl931x_mac_force_mode_ctrl,
  296. .mac_port_ctrl = rtl931x_mac_port_ctrl,
  297. .l2_port_new_salrn = rtl931x_l2_port_new_salrn,
  298. .l2_port_new_sa_fwd = rtl931x_l2_port_new_sa_fwd,
  299. .mir_ctrl = RTL931X_MIR_CTRL,
  300. .mir_dpm = RTL931X_MIR_DPM_CTRL,
  301. .mir_spm = RTL931X_MIR_SPM_CTRL,
  302. .mac_link_sts = RTL931X_MAC_LINK_STS,
  303. .mac_link_dup_sts = RTL931X_MAC_LINK_DUP_STS,
  304. .mac_link_spd_sts = rtl931x_mac_link_spd_sts,
  305. .mac_rx_pause_sts = RTL931X_MAC_RX_PAUSE_STS,
  306. .mac_tx_pause_sts = RTL931X_MAC_TX_PAUSE_STS,
  307. .read_l2_entry_using_hash = rtl931x_read_l2_entry_using_hash,
  308. .read_cam = rtl931x_read_cam,
  309. .vlan_port_egr_filter = RTL931X_VLAN_PORT_EGR_FLTR(0),
  310. .vlan_port_igr_filter = RTL931X_VLAN_PORT_IGR_FLTR(0),
  311. // .vlan_port_pb = does not exist
  312. .vlan_port_tag_sts_ctrl = RTL931X_VLAN_PORT_TAG_CTRL,
  313. .trk_mbr_ctr = rtl931x_trk_mbr_ctr,
  314. };