netcp_xgbepcsr.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502
  1. /*
  2. * XGE PCSR module initialisation
  3. *
  4. * Copyright (C) 2014 Texas Instruments Incorporated
  5. * Authors: Sandeep Nair <sandeep_n@ti.com>
  6. * WingMan Kwok <w-kwok2@ti.com>
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License as
  10. * published by the Free Software Foundation version 2.
  11. *
  12. * This program is distributed "as is" WITHOUT ANY WARRANTY of any
  13. * kind, whether express or implied; without even the implied warranty
  14. * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. */
  17. #include "netcp.h"
  18. /* XGBE registers */
  19. #define XGBE_CTRL_OFFSET 0x0c
  20. #define XGBE_SGMII_1_OFFSET 0x0114
  21. #define XGBE_SGMII_2_OFFSET 0x0214
  22. /* PCS-R registers */
  23. #define PCSR_CPU_CTRL_OFFSET 0x1fd0
  24. #define POR_EN BIT(29)
  25. #define reg_rmw(addr, value, mask) \
  26. writel(((readl(addr) & (~(mask))) | \
  27. (value & (mask))), (addr))
  28. /* bit mask of width w at offset s */
  29. #define MASK_WID_SH(w, s) (((1 << w) - 1) << s)
  30. /* shift value v to offset s */
  31. #define VAL_SH(v, s) (v << s)
  32. #define PHY_A(serdes) 0
  33. struct serdes_cfg {
  34. u32 ofs;
  35. u32 val;
  36. u32 mask;
  37. };
  38. static struct serdes_cfg cfg_phyb_1p25g_156p25mhz_cmu0[] = {
  39. {0x0000, 0x00800002, 0x00ff00ff},
  40. {0x0014, 0x00003838, 0x0000ffff},
  41. {0x0060, 0x1c44e438, 0xffffffff},
  42. {0x0064, 0x00c18400, 0x00ffffff},
  43. {0x0068, 0x17078200, 0xffffff00},
  44. {0x006c, 0x00000014, 0x000000ff},
  45. {0x0078, 0x0000c000, 0x0000ff00},
  46. {0x0000, 0x00000003, 0x000000ff},
  47. };
  48. static struct serdes_cfg cfg_phyb_10p3125g_156p25mhz_cmu1[] = {
  49. {0x0c00, 0x00030002, 0x00ff00ff},
  50. {0x0c14, 0x00005252, 0x0000ffff},
  51. {0x0c28, 0x80000000, 0xff000000},
  52. {0x0c2c, 0x000000f6, 0x000000ff},
  53. {0x0c3c, 0x04000405, 0xff00ffff},
  54. {0x0c40, 0xc0800000, 0xffff0000},
  55. {0x0c44, 0x5a202062, 0xffffffff},
  56. {0x0c48, 0x40040424, 0xffffffff},
  57. {0x0c4c, 0x00004002, 0x0000ffff},
  58. {0x0c50, 0x19001c00, 0xff00ff00},
  59. {0x0c54, 0x00002100, 0x0000ff00},
  60. {0x0c58, 0x00000060, 0x000000ff},
  61. {0x0c60, 0x80131e7c, 0xffffffff},
  62. {0x0c64, 0x8400cb02, 0xff00ffff},
  63. {0x0c68, 0x17078200, 0xffffff00},
  64. {0x0c6c, 0x00000016, 0x000000ff},
  65. {0x0c74, 0x00000400, 0x0000ff00},
  66. {0x0c78, 0x0000c000, 0x0000ff00},
  67. {0x0c00, 0x00000003, 0x000000ff},
  68. };
  69. static struct serdes_cfg cfg_phyb_10p3125g_16bit_lane[] = {
  70. {0x0204, 0x00000080, 0x000000ff},
  71. {0x0208, 0x0000920d, 0x0000ffff},
  72. {0x0204, 0xfc000000, 0xff000000},
  73. {0x0208, 0x00009104, 0x0000ffff},
  74. {0x0210, 0x1a000000, 0xff000000},
  75. {0x0214, 0x00006b58, 0x00ffffff},
  76. {0x0218, 0x75800084, 0xffff00ff},
  77. {0x022c, 0x00300000, 0x00ff0000},
  78. {0x0230, 0x00003800, 0x0000ff00},
  79. {0x024c, 0x008f0000, 0x00ff0000},
  80. {0x0250, 0x30000000, 0xff000000},
  81. {0x0260, 0x00000002, 0x000000ff},
  82. {0x0264, 0x00000057, 0x000000ff},
  83. {0x0268, 0x00575700, 0x00ffff00},
  84. {0x0278, 0xff000000, 0xff000000},
  85. {0x0280, 0x00500050, 0x00ff00ff},
  86. {0x0284, 0x00001f15, 0x0000ffff},
  87. {0x028c, 0x00006f00, 0x0000ff00},
  88. {0x0294, 0x00000000, 0xffffff00},
  89. {0x0298, 0x00002640, 0xff00ffff},
  90. {0x029c, 0x00000003, 0x000000ff},
  91. {0x02a4, 0x00000f13, 0x0000ffff},
  92. {0x02a8, 0x0001b600, 0x00ffff00},
  93. {0x0380, 0x00000030, 0x000000ff},
  94. {0x03c0, 0x00000200, 0x0000ff00},
  95. {0x03cc, 0x00000018, 0x000000ff},
  96. {0x03cc, 0x00000000, 0x000000ff},
  97. };
  98. static struct serdes_cfg cfg_phyb_10p3125g_comlane[] = {
  99. {0x0a00, 0x00000800, 0x0000ff00},
  100. {0x0a84, 0x00000000, 0x000000ff},
  101. {0x0a8c, 0x00130000, 0x00ff0000},
  102. {0x0a90, 0x77a00000, 0xffff0000},
  103. {0x0a94, 0x00007777, 0x0000ffff},
  104. {0x0b08, 0x000f0000, 0xffff0000},
  105. {0x0b0c, 0x000f0000, 0x00ffffff},
  106. {0x0b10, 0xbe000000, 0xff000000},
  107. {0x0b14, 0x000000ff, 0x000000ff},
  108. {0x0b18, 0x00000014, 0x000000ff},
  109. {0x0b5c, 0x981b0000, 0xffff0000},
  110. {0x0b64, 0x00001100, 0x0000ff00},
  111. {0x0b78, 0x00000c00, 0x0000ff00},
  112. {0x0abc, 0xff000000, 0xff000000},
  113. {0x0ac0, 0x0000008b, 0x000000ff},
  114. };
  115. static struct serdes_cfg cfg_cm_c1_c2[] = {
  116. {0x0208, 0x00000000, 0x00000f00},
  117. {0x0208, 0x00000000, 0x0000001f},
  118. {0x0204, 0x00000000, 0x00040000},
  119. {0x0208, 0x000000a0, 0x000000e0},
  120. };
  121. static void netcp_xgbe_serdes_cmu_init(void __iomem *serdes_regs)
  122. {
  123. int i;
  124. /* cmu0 setup */
  125. for (i = 0; i < ARRAY_SIZE(cfg_phyb_1p25g_156p25mhz_cmu0); i++) {
  126. reg_rmw(serdes_regs + cfg_phyb_1p25g_156p25mhz_cmu0[i].ofs,
  127. cfg_phyb_1p25g_156p25mhz_cmu0[i].val,
  128. cfg_phyb_1p25g_156p25mhz_cmu0[i].mask);
  129. }
  130. /* cmu1 setup */
  131. for (i = 0; i < ARRAY_SIZE(cfg_phyb_10p3125g_156p25mhz_cmu1); i++) {
  132. reg_rmw(serdes_regs + cfg_phyb_10p3125g_156p25mhz_cmu1[i].ofs,
  133. cfg_phyb_10p3125g_156p25mhz_cmu1[i].val,
  134. cfg_phyb_10p3125g_156p25mhz_cmu1[i].mask);
  135. }
  136. }
  137. /* lane is 0 based */
  138. static void netcp_xgbe_serdes_lane_config(
  139. void __iomem *serdes_regs, int lane)
  140. {
  141. int i;
  142. /* lane setup */
  143. for (i = 0; i < ARRAY_SIZE(cfg_phyb_10p3125g_16bit_lane); i++) {
  144. reg_rmw(serdes_regs +
  145. cfg_phyb_10p3125g_16bit_lane[i].ofs +
  146. (0x200 * lane),
  147. cfg_phyb_10p3125g_16bit_lane[i].val,
  148. cfg_phyb_10p3125g_16bit_lane[i].mask);
  149. }
  150. /* disable auto negotiation*/
  151. reg_rmw(serdes_regs + (0x200 * lane) + 0x0380,
  152. 0x00000000, 0x00000010);
  153. /* disable link training */
  154. reg_rmw(serdes_regs + (0x200 * lane) + 0x03c0,
  155. 0x00000000, 0x00000200);
  156. }
  157. static void netcp_xgbe_serdes_com_enable(void __iomem *serdes_regs)
  158. {
  159. int i;
  160. for (i = 0; i < ARRAY_SIZE(cfg_phyb_10p3125g_comlane); i++) {
  161. reg_rmw(serdes_regs + cfg_phyb_10p3125g_comlane[i].ofs,
  162. cfg_phyb_10p3125g_comlane[i].val,
  163. cfg_phyb_10p3125g_comlane[i].mask);
  164. }
  165. }
  166. static void netcp_xgbe_serdes_lane_enable(
  167. void __iomem *serdes_regs, int lane)
  168. {
  169. /* Set Lane Control Rate */
  170. writel(0xe0e9e038, serdes_regs + 0x1fe0 + (4 * lane));
  171. }
  172. static void netcp_xgbe_serdes_phyb_rst_clr(void __iomem *serdes_regs)
  173. {
  174. reg_rmw(serdes_regs + 0x0a00, 0x0000001f, 0x000000ff);
  175. }
  176. static void netcp_xgbe_serdes_pll_disable(void __iomem *serdes_regs)
  177. {
  178. writel(0x88000000, serdes_regs + 0x1ff4);
  179. }
  180. static void netcp_xgbe_serdes_pll_enable(void __iomem *serdes_regs)
  181. {
  182. netcp_xgbe_serdes_phyb_rst_clr(serdes_regs);
  183. writel(0xee000000, serdes_regs + 0x1ff4);
  184. }
  185. static int netcp_xgbe_wait_pll_locked(void __iomem *sw_regs)
  186. {
  187. unsigned long timeout;
  188. int ret = 0;
  189. u32 val_1, val_0;
  190. timeout = jiffies + msecs_to_jiffies(500);
  191. do {
  192. val_0 = (readl(sw_regs + XGBE_SGMII_1_OFFSET) & BIT(4));
  193. val_1 = (readl(sw_regs + XGBE_SGMII_2_OFFSET) & BIT(4));
  194. if (val_1 && val_0)
  195. return 0;
  196. if (time_after(jiffies, timeout)) {
  197. ret = -ETIMEDOUT;
  198. break;
  199. }
  200. cpu_relax();
  201. } while (true);
  202. pr_err("XGBE serdes not locked: time out.\n");
  203. return ret;
  204. }
  205. static void netcp_xgbe_serdes_enable_xgmii_port(void __iomem *sw_regs)
  206. {
  207. writel(0x03, sw_regs + XGBE_CTRL_OFFSET);
  208. }
  209. static u32 netcp_xgbe_serdes_read_tbus_val(void __iomem *serdes_regs)
  210. {
  211. u32 tmp;
  212. if (PHY_A(serdes_regs)) {
  213. tmp = (readl(serdes_regs + 0x0ec) >> 24) & 0x0ff;
  214. tmp |= ((readl(serdes_regs + 0x0fc) >> 16) & 0x00f00);
  215. } else {
  216. tmp = (readl(serdes_regs + 0x0f8) >> 16) & 0x0fff;
  217. }
  218. return tmp;
  219. }
  220. static void netcp_xgbe_serdes_write_tbus_addr(void __iomem *serdes_regs,
  221. int select, int ofs)
  222. {
  223. if (PHY_A(serdes_regs)) {
  224. reg_rmw(serdes_regs + 0x0008, ((select << 5) + ofs) << 24,
  225. ~0x00ffffff);
  226. return;
  227. }
  228. /* For 2 lane Phy-B, lane0 is actually lane1 */
  229. switch (select) {
  230. case 1:
  231. select = 2;
  232. break;
  233. case 2:
  234. select = 3;
  235. break;
  236. default:
  237. return;
  238. }
  239. reg_rmw(serdes_regs + 0x00fc, ((select << 8) + ofs) << 16, ~0xf800ffff);
  240. }
  241. static u32 netcp_xgbe_serdes_read_select_tbus(void __iomem *serdes_regs,
  242. int select, int ofs)
  243. {
  244. /* Set tbus address */
  245. netcp_xgbe_serdes_write_tbus_addr(serdes_regs, select, ofs);
  246. /* Get TBUS Value */
  247. return netcp_xgbe_serdes_read_tbus_val(serdes_regs);
  248. }
  249. static void netcp_xgbe_serdes_reset_cdr(void __iomem *serdes_regs,
  250. void __iomem *sig_detect_reg, int lane)
  251. {
  252. u32 tmp, dlpf, tbus;
  253. /*Get the DLPF values */
  254. tmp = netcp_xgbe_serdes_read_select_tbus(
  255. serdes_regs, lane + 1, 5);
  256. dlpf = tmp >> 2;
  257. if (dlpf < 400 || dlpf > 700) {
  258. reg_rmw(sig_detect_reg, VAL_SH(2, 1), MASK_WID_SH(2, 1));
  259. mdelay(1);
  260. reg_rmw(sig_detect_reg, VAL_SH(0, 1), MASK_WID_SH(2, 1));
  261. } else {
  262. tbus = netcp_xgbe_serdes_read_select_tbus(serdes_regs, lane +
  263. 1, 0xe);
  264. pr_debug("XGBE: CDR centered, DLPF: %4d,%d,%d.\n",
  265. tmp >> 2, tmp & 3, (tbus >> 2) & 3);
  266. }
  267. }
  268. /* Call every 100 ms */
  269. static int netcp_xgbe_check_link_status(void __iomem *serdes_regs,
  270. void __iomem *sw_regs, u32 lanes,
  271. u32 *current_state, u32 *lane_down)
  272. {
  273. void __iomem *pcsr_base = sw_regs + 0x0600;
  274. void __iomem *sig_detect_reg;
  275. u32 pcsr_rx_stat, blk_lock, blk_errs;
  276. int loss, i, status = 1;
  277. for (i = 0; i < lanes; i++) {
  278. /* Get the Loss bit */
  279. loss = readl(serdes_regs + 0x1fc0 + 0x20 + (i * 0x04)) & 0x1;
  280. /* Get Block Errors and Block Lock bits */
  281. pcsr_rx_stat = readl(pcsr_base + 0x0c + (i * 0x80));
  282. blk_lock = (pcsr_rx_stat >> 30) & 0x1;
  283. blk_errs = (pcsr_rx_stat >> 16) & 0x0ff;
  284. /* Get Signal Detect Overlay Address */
  285. sig_detect_reg = serdes_regs + (i * 0x200) + 0x200 + 0x04;
  286. /* If Block errors maxed out, attempt recovery! */
  287. if (blk_errs == 0x0ff)
  288. blk_lock = 0;
  289. switch (current_state[i]) {
  290. case 0:
  291. /* if good link lock the signal detect ON! */
  292. if (!loss && blk_lock) {
  293. pr_debug("XGBE PCSR Linked Lane: %d\n", i);
  294. reg_rmw(sig_detect_reg, VAL_SH(3, 1),
  295. MASK_WID_SH(2, 1));
  296. current_state[i] = 1;
  297. } else if (!blk_lock) {
  298. /* if no lock, then reset CDR */
  299. pr_debug("XGBE PCSR Recover Lane: %d\n", i);
  300. netcp_xgbe_serdes_reset_cdr(serdes_regs,
  301. sig_detect_reg, i);
  302. }
  303. break;
  304. case 1:
  305. if (!blk_lock) {
  306. /* Link Lost? */
  307. lane_down[i] = 1;
  308. current_state[i] = 2;
  309. }
  310. break;
  311. case 2:
  312. if (blk_lock)
  313. /* Nope just noise */
  314. current_state[i] = 1;
  315. else {
  316. /* Lost the block lock, reset CDR if it is
  317. * not centered and go back to sync state
  318. */
  319. netcp_xgbe_serdes_reset_cdr(serdes_regs,
  320. sig_detect_reg, i);
  321. current_state[i] = 0;
  322. }
  323. break;
  324. default:
  325. pr_err("XGBE: unknown current_state[%d] %d\n",
  326. i, current_state[i]);
  327. break;
  328. }
  329. if (blk_errs > 0) {
  330. /* Reset the Error counts! */
  331. reg_rmw(pcsr_base + 0x08 + (i * 0x80), VAL_SH(0x19, 0),
  332. MASK_WID_SH(8, 0));
  333. reg_rmw(pcsr_base + 0x08 + (i * 0x80), VAL_SH(0x00, 0),
  334. MASK_WID_SH(8, 0));
  335. }
  336. status &= (current_state[i] == 1);
  337. }
  338. return status;
  339. }
  340. static int netcp_xgbe_serdes_check_lane(void __iomem *serdes_regs,
  341. void __iomem *sw_regs)
  342. {
  343. u32 current_state[2] = {0, 0};
  344. int retries = 0, link_up;
  345. u32 lane_down[2];
  346. do {
  347. lane_down[0] = 0;
  348. lane_down[1] = 0;
  349. link_up = netcp_xgbe_check_link_status(serdes_regs, sw_regs, 2,
  350. current_state,
  351. lane_down);
  352. /* if we did not get link up then wait 100ms before calling
  353. * it again
  354. */
  355. if (link_up)
  356. break;
  357. if (lane_down[0])
  358. pr_debug("XGBE: detected link down on lane 0\n");
  359. if (lane_down[1])
  360. pr_debug("XGBE: detected link down on lane 1\n");
  361. if (++retries > 1) {
  362. pr_debug("XGBE: timeout waiting for serdes link up\n");
  363. return -ETIMEDOUT;
  364. }
  365. mdelay(100);
  366. } while (!link_up);
  367. pr_debug("XGBE: PCSR link is up\n");
  368. return 0;
  369. }
  370. static void netcp_xgbe_serdes_setup_cm_c1_c2(void __iomem *serdes_regs,
  371. int lane, int cm, int c1, int c2)
  372. {
  373. int i;
  374. for (i = 0; i < ARRAY_SIZE(cfg_cm_c1_c2); i++) {
  375. reg_rmw(serdes_regs + cfg_cm_c1_c2[i].ofs + (0x200 * lane),
  376. cfg_cm_c1_c2[i].val,
  377. cfg_cm_c1_c2[i].mask);
  378. }
  379. }
  380. static void netcp_xgbe_reset_serdes(void __iomem *serdes_regs)
  381. {
  382. /* Toggle the POR_EN bit in CONFIG.CPU_CTRL */
  383. /* enable POR_EN bit */
  384. reg_rmw(serdes_regs + PCSR_CPU_CTRL_OFFSET, POR_EN, POR_EN);
  385. usleep_range(10, 100);
  386. /* disable POR_EN bit */
  387. reg_rmw(serdes_regs + PCSR_CPU_CTRL_OFFSET, 0, POR_EN);
  388. usleep_range(10, 100);
  389. }
  390. static int netcp_xgbe_serdes_config(void __iomem *serdes_regs,
  391. void __iomem *sw_regs)
  392. {
  393. u32 ret, i;
  394. netcp_xgbe_serdes_pll_disable(serdes_regs);
  395. netcp_xgbe_serdes_cmu_init(serdes_regs);
  396. for (i = 0; i < 2; i++)
  397. netcp_xgbe_serdes_lane_config(serdes_regs, i);
  398. netcp_xgbe_serdes_com_enable(serdes_regs);
  399. /* This is EVM + RTM-BOC specific */
  400. for (i = 0; i < 2; i++)
  401. netcp_xgbe_serdes_setup_cm_c1_c2(serdes_regs, i, 0, 0, 5);
  402. netcp_xgbe_serdes_pll_enable(serdes_regs);
  403. for (i = 0; i < 2; i++)
  404. netcp_xgbe_serdes_lane_enable(serdes_regs, i);
  405. /* SB PLL Status Poll */
  406. ret = netcp_xgbe_wait_pll_locked(sw_regs);
  407. if (ret)
  408. return ret;
  409. netcp_xgbe_serdes_enable_xgmii_port(sw_regs);
  410. netcp_xgbe_serdes_check_lane(serdes_regs, sw_regs);
  411. return ret;
  412. }
  413. int netcp_xgbe_serdes_init(void __iomem *serdes_regs, void __iomem *xgbe_regs)
  414. {
  415. u32 val;
  416. /* read COMLANE bits 4:0 */
  417. val = readl(serdes_regs + 0xa00);
  418. if (val & 0x1f) {
  419. pr_debug("XGBE: serdes already in operation - reset\n");
  420. netcp_xgbe_reset_serdes(serdes_regs);
  421. }
  422. return netcp_xgbe_serdes_config(serdes_regs, xgbe_regs);
  423. }