fsl_lsch3_serdes.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright 2014-2015 Freescale Semiconductor, Inc.
  4. */
  5. #include <common.h>
  6. #include <asm/io.h>
  7. #include <linux/errno.h>
  8. #include <asm/arch/fsl_serdes.h>
  9. #include <asm/arch/soc.h>
  10. #include <fsl-mc/ldpaa_wriop.h>
  11. #ifdef CONFIG_SYS_FSL_SRDS_1
  12. static u8 serdes1_prtcl_map[SERDES_PRCTL_COUNT];
  13. #endif
  14. #ifdef CONFIG_SYS_FSL_SRDS_2
  15. static u8 serdes2_prtcl_map[SERDES_PRCTL_COUNT];
  16. #endif
  17. #if defined(CONFIG_FSL_MC_ENET) && !defined(CONFIG_SPL_BUILD)
  18. int xfi_dpmac[XFI8 + 1];
  19. int sgmii_dpmac[SGMII16 + 1];
  20. #endif
  21. __weak void wriop_init_dpmac_qsgmii(int sd, int lane_prtcl)
  22. {
  23. return;
  24. }
  25. /*
  26. *The return value of this func is the serdes protocol used.
  27. *Typically this function is called number of times depending
  28. *upon the number of serdes blocks in the Silicon.
  29. *Zero is used to denote that no serdes was enabled,
  30. *this is the case when golden RCW was used where DPAA2 bring was
  31. *intentionally removed to achieve boot to prompt
  32. */
  33. __weak int serdes_get_number(int serdes, int cfg)
  34. {
  35. return cfg;
  36. }
  37. int is_serdes_configured(enum srds_prtcl device)
  38. {
  39. int ret = 0;
  40. #ifdef CONFIG_SYS_FSL_SRDS_1
  41. if (!serdes1_prtcl_map[NONE])
  42. fsl_serdes_init();
  43. ret |= serdes1_prtcl_map[device];
  44. #endif
  45. #ifdef CONFIG_SYS_FSL_SRDS_2
  46. if (!serdes2_prtcl_map[NONE])
  47. fsl_serdes_init();
  48. ret |= serdes2_prtcl_map[device];
  49. #endif
  50. return !!ret;
  51. }
  52. int serdes_get_first_lane(u32 sd, enum srds_prtcl device)
  53. {
  54. struct ccsr_gur __iomem *gur = (void *)(CONFIG_SYS_FSL_GUTS_ADDR);
  55. u32 cfg = 0;
  56. int i;
  57. switch (sd) {
  58. #ifdef CONFIG_SYS_FSL_SRDS_1
  59. case FSL_SRDS_1:
  60. cfg = gur_in32(&gur->rcwsr[FSL_CHASSIS3_SRDS1_REGSR - 1]);
  61. cfg &= FSL_CHASSIS3_SRDS1_PRTCL_MASK;
  62. cfg >>= FSL_CHASSIS3_SRDS1_PRTCL_SHIFT;
  63. break;
  64. #endif
  65. #ifdef CONFIG_SYS_FSL_SRDS_2
  66. case FSL_SRDS_2:
  67. cfg = gur_in32(&gur->rcwsr[FSL_CHASSIS3_SRDS2_REGSR - 1]);
  68. cfg &= FSL_CHASSIS3_SRDS2_PRTCL_MASK;
  69. cfg >>= FSL_CHASSIS3_SRDS2_PRTCL_SHIFT;
  70. break;
  71. #endif
  72. default:
  73. printf("invalid SerDes%d\n", sd);
  74. break;
  75. }
  76. cfg = serdes_get_number(sd, cfg);
  77. /* Is serdes enabled at all? */
  78. if (cfg == 0)
  79. return -ENODEV;
  80. for (i = 0; i < SRDS_MAX_LANES; i++) {
  81. if (serdes_get_prtcl(sd, cfg, i) == device)
  82. return i;
  83. }
  84. return -ENODEV;
  85. }
  86. void serdes_init(u32 sd, u32 sd_addr, u32 rcwsr, u32 sd_prctl_mask,
  87. u32 sd_prctl_shift, u8 serdes_prtcl_map[SERDES_PRCTL_COUNT])
  88. {
  89. struct ccsr_gur __iomem *gur = (void *)(CONFIG_SYS_FSL_GUTS_ADDR);
  90. u32 cfg;
  91. int lane;
  92. if (serdes_prtcl_map[NONE])
  93. return;
  94. memset(serdes_prtcl_map, 0, sizeof(u8) * SERDES_PRCTL_COUNT);
  95. cfg = gur_in32(&gur->rcwsr[rcwsr - 1]) & sd_prctl_mask;
  96. cfg >>= sd_prctl_shift;
  97. cfg = serdes_get_number(sd, cfg);
  98. printf("Using SERDES%d Protocol: %d (0x%x)\n", sd + 1, cfg, cfg);
  99. if (!is_serdes_prtcl_valid(sd, cfg))
  100. printf("SERDES%d[PRTCL] = 0x%x is not valid\n", sd + 1, cfg);
  101. for (lane = 0; lane < SRDS_MAX_LANES; lane++) {
  102. enum srds_prtcl lane_prtcl = serdes_get_prtcl(sd, cfg, lane);
  103. if (unlikely(lane_prtcl >= SERDES_PRCTL_COUNT))
  104. debug("Unknown SerDes lane protocol %d\n", lane_prtcl);
  105. else {
  106. serdes_prtcl_map[lane_prtcl] = 1;
  107. #if defined(CONFIG_FSL_MC_ENET) && !defined(CONFIG_SPL_BUILD)
  108. switch (lane_prtcl) {
  109. case QSGMII_A:
  110. case QSGMII_B:
  111. case QSGMII_C:
  112. case QSGMII_D:
  113. wriop_init_dpmac_qsgmii(sd, (int)lane_prtcl);
  114. break;
  115. default:
  116. if (lane_prtcl >= XFI1 && lane_prtcl <= XFI8)
  117. wriop_init_dpmac(sd,
  118. xfi_dpmac[lane_prtcl],
  119. (int)lane_prtcl);
  120. if (lane_prtcl >= SGMII1 &&
  121. lane_prtcl <= SGMII16)
  122. wriop_init_dpmac(sd, sgmii_dpmac[
  123. lane_prtcl],
  124. (int)lane_prtcl);
  125. break;
  126. }
  127. #endif
  128. }
  129. }
  130. /* Set the first element to indicate serdes has been initialized */
  131. serdes_prtcl_map[NONE] = 1;
  132. }
  133. __weak int get_serdes_volt(void)
  134. {
  135. return -1;
  136. }
  137. __weak int set_serdes_volt(int svdd)
  138. {
  139. return -1;
  140. }
  141. #define LNAGCR0_RT_RSTB 0x00600000
  142. #define RSTCTL_RESET_MASK 0x000000E0
  143. #define RSTCTL_RSTREQ 0x80000000
  144. #define RSTCTL_RST_DONE 0x40000000
  145. #define RSTCTL_RSTERR 0x20000000
  146. #define RSTCTL_SDEN 0x00000020
  147. #define RSTCTL_SDRST_B 0x00000040
  148. #define RSTCTL_PLLRST_B 0x00000080
  149. #define TCALCR_CALRST_B 0x08000000
  150. struct serdes_prctl_info {
  151. u32 id;
  152. u32 mask;
  153. u32 shift;
  154. };
  155. struct serdes_prctl_info srds_prctl_info[] = {
  156. #ifdef CONFIG_SYS_FSL_SRDS_1
  157. {.id = 1,
  158. .mask = FSL_CHASSIS3_SRDS1_PRTCL_MASK,
  159. .shift = FSL_CHASSIS3_SRDS1_PRTCL_SHIFT
  160. },
  161. #endif
  162. #ifdef CONFIG_SYS_FSL_SRDS_2
  163. {.id = 2,
  164. .mask = FSL_CHASSIS3_SRDS2_PRTCL_MASK,
  165. .shift = FSL_CHASSIS3_SRDS2_PRTCL_SHIFT
  166. },
  167. #endif
  168. {} /* NULL ENTRY */
  169. };
  170. static int get_serdes_prctl_info_idx(u32 serdes_id)
  171. {
  172. int pos = 0;
  173. struct serdes_prctl_info *srds_info;
  174. /* loop until NULL ENTRY defined by .id=0 */
  175. for (srds_info = srds_prctl_info; srds_info->id != 0;
  176. srds_info++, pos++) {
  177. if (srds_info->id == serdes_id)
  178. return pos;
  179. }
  180. return -1;
  181. }
  182. static void do_enabled_lanes_reset(u32 serdes_id, u32 cfg,
  183. struct ccsr_serdes __iomem *serdes_base,
  184. bool cmplt)
  185. {
  186. int i, pos;
  187. u32 cfg_tmp;
  188. pos = get_serdes_prctl_info_idx(serdes_id);
  189. if (pos == -1) {
  190. printf("invalid serdes_id %d\n", serdes_id);
  191. return;
  192. }
  193. cfg_tmp = cfg & srds_prctl_info[pos].mask;
  194. cfg_tmp >>= srds_prctl_info[pos].shift;
  195. for (i = 0; i < 4 && cfg_tmp & (0xf << (3 - i)); i++) {
  196. if (cmplt)
  197. setbits_le32(&serdes_base->lane[i].gcr0,
  198. LNAGCR0_RT_RSTB);
  199. else
  200. clrbits_le32(&serdes_base->lane[i].gcr0,
  201. LNAGCR0_RT_RSTB);
  202. }
  203. }
  204. static void do_pll_reset(u32 cfg,
  205. struct ccsr_serdes __iomem *serdes_base)
  206. {
  207. int i;
  208. for (i = 0; i < 2 && !(cfg & (0x1 << (1 - i))); i++) {
  209. clrbits_le32(&serdes_base->bank[i].rstctl,
  210. RSTCTL_RESET_MASK);
  211. udelay(1);
  212. setbits_le32(&serdes_base->bank[i].rstctl,
  213. RSTCTL_RSTREQ);
  214. }
  215. udelay(1);
  216. }
  217. static void do_rx_tx_cal_reset(struct ccsr_serdes __iomem *serdes_base)
  218. {
  219. clrbits_le32(&serdes_base->srdstcalcr, TCALCR_CALRST_B);
  220. clrbits_le32(&serdes_base->srdstcalcr, TCALCR_CALRST_B);
  221. }
  222. static void do_rx_tx_cal_reset_comp(u32 cfg, int i,
  223. struct ccsr_serdes __iomem *serdes_base)
  224. {
  225. if (!(cfg == 0x3 && i == 1)) {
  226. udelay(1);
  227. setbits_le32(&serdes_base->srdstcalcr, TCALCR_CALRST_B);
  228. setbits_le32(&serdes_base->srdstcalcr, TCALCR_CALRST_B);
  229. }
  230. udelay(1);
  231. }
  232. static void do_pll_reset_done(u32 cfg,
  233. struct ccsr_serdes __iomem *serdes_base)
  234. {
  235. int i;
  236. u32 reg = 0;
  237. for (i = 0; i < 2; i++) {
  238. reg = in_le32(&serdes_base->bank[i].pllcr0);
  239. if (!(cfg & (0x1 << (1 - i))) && ((reg >> 23) & 0x1)) {
  240. setbits_le32(&serdes_base->bank[i].rstctl,
  241. RSTCTL_RST_DONE);
  242. }
  243. }
  244. }
  245. static void do_serdes_enable(u32 cfg,
  246. struct ccsr_serdes __iomem *serdes_base)
  247. {
  248. int i;
  249. for (i = 0; i < 2 && !(cfg & (0x1 << (1 - i))); i++) {
  250. setbits_le32(&serdes_base->bank[i].rstctl, RSTCTL_SDEN);
  251. udelay(1);
  252. setbits_le32(&serdes_base->bank[i].rstctl, RSTCTL_PLLRST_B);
  253. udelay(1);
  254. /* Take the Rx/Tx calibration out of reset */
  255. do_rx_tx_cal_reset_comp(cfg, i, serdes_base);
  256. }
  257. }
  258. static void do_pll_lock(u32 cfg,
  259. struct ccsr_serdes __iomem *serdes_base)
  260. {
  261. int i;
  262. u32 reg = 0;
  263. for (i = 0; i < 2 && !(cfg & (0x1 << (1 - i))); i++) {
  264. /* if the PLL is not locked, set RST_ERR */
  265. reg = in_le32(&serdes_base->bank[i].pllcr0);
  266. if (!((reg >> 23) & 0x1)) {
  267. setbits_le32(&serdes_base->bank[i].rstctl,
  268. RSTCTL_RSTERR);
  269. } else {
  270. udelay(1);
  271. setbits_le32(&serdes_base->bank[i].rstctl,
  272. RSTCTL_SDRST_B);
  273. udelay(1);
  274. }
  275. }
  276. }
  277. int setup_serdes_volt(u32 svdd)
  278. {
  279. struct ccsr_gur __iomem *gur = (void *)(CONFIG_SYS_FSL_GUTS_ADDR);
  280. struct ccsr_serdes __iomem *serdes1_base =
  281. (void *)CONFIG_SYS_FSL_LSCH3_SERDES_ADDR;
  282. u32 cfg_rcwsrds1 = gur_in32(&gur->rcwsr[FSL_CHASSIS3_SRDS1_REGSR - 1]);
  283. #ifdef CONFIG_SYS_FSL_SRDS_2
  284. struct ccsr_serdes __iomem *serdes2_base =
  285. (void *)(CONFIG_SYS_FSL_LSCH3_SERDES_ADDR + 0x10000);
  286. u32 cfg_rcwsrds2 = gur_in32(&gur->rcwsr[FSL_CHASSIS3_SRDS2_REGSR - 1]);
  287. #endif
  288. u32 cfg_tmp;
  289. int svdd_cur, svdd_tar;
  290. int ret = 1;
  291. /* Only support switch SVDD to 900mV */
  292. if (svdd != 900)
  293. return -EINVAL;
  294. /* Scale up to the LTC resolution is 1/4096V */
  295. svdd = (svdd * 4096) / 1000;
  296. svdd_tar = svdd;
  297. svdd_cur = get_serdes_volt();
  298. if (svdd_cur < 0)
  299. return -EINVAL;
  300. debug("%s: current SVDD: %x; target SVDD: %x\n",
  301. __func__, svdd_cur, svdd_tar);
  302. if (svdd_cur == svdd_tar)
  303. return 0;
  304. /* Put the all enabled lanes in reset */
  305. #ifdef CONFIG_SYS_FSL_SRDS_1
  306. do_enabled_lanes_reset(1, cfg_rcwsrds1, serdes1_base, false);
  307. #endif
  308. #ifdef CONFIG_SYS_FSL_SRDS_2
  309. do_enabled_lanes_reset(2, cfg_rcwsrds2, serdes2_base, false);
  310. #endif
  311. /* Put the all enabled PLL in reset */
  312. #ifdef CONFIG_SYS_FSL_SRDS_1
  313. cfg_tmp = cfg_rcwsrds1 & 0x3;
  314. do_pll_reset(cfg_tmp, serdes1_base);
  315. #endif
  316. #ifdef CONFIG_SYS_FSL_SRDS_2
  317. cfg_tmp = cfg_rcwsrds1 & 0xC;
  318. cfg_tmp >>= 2;
  319. do_pll_reset(cfg_tmp, serdes2_base);
  320. #endif
  321. /* Put the Rx/Tx calibration into reset */
  322. #ifdef CONFIG_SYS_FSL_SRDS_1
  323. do_rx_tx_cal_reset(serdes1_base);
  324. #endif
  325. #ifdef CONFIG_SYS_FSL_SRDS_2
  326. do_rx_tx_cal_reset(serdes2_base);
  327. #endif
  328. ret = set_serdes_volt(svdd);
  329. if (ret < 0) {
  330. printf("could not change SVDD\n");
  331. ret = -1;
  332. }
  333. /* For each PLL that’s not disabled via RCW enable the SERDES */
  334. #ifdef CONFIG_SYS_FSL_SRDS_1
  335. cfg_tmp = cfg_rcwsrds1 & 0x3;
  336. do_serdes_enable(cfg_tmp, serdes1_base);
  337. #endif
  338. #ifdef CONFIG_SYS_FSL_SRDS_2
  339. cfg_tmp = cfg_rcwsrds1 & 0xC;
  340. cfg_tmp >>= 2;
  341. do_serdes_enable(cfg_tmp, serdes2_base);
  342. #endif
  343. /* Wait for at at least 625us, ensure the PLLs being reset are locked */
  344. udelay(800);
  345. #ifdef CONFIG_SYS_FSL_SRDS_1
  346. cfg_tmp = cfg_rcwsrds1 & 0x3;
  347. do_pll_lock(cfg_tmp, serdes1_base);
  348. #endif
  349. #ifdef CONFIG_SYS_FSL_SRDS_2
  350. cfg_tmp = cfg_rcwsrds1 & 0xC;
  351. cfg_tmp >>= 2;
  352. do_pll_lock(cfg_tmp, serdes2_base);
  353. #endif
  354. /* Take the all enabled lanes out of reset */
  355. #ifdef CONFIG_SYS_FSL_SRDS_1
  356. do_enabled_lanes_reset(1, cfg_rcwsrds1, serdes1_base, true);
  357. #endif
  358. #ifdef CONFIG_SYS_FSL_SRDS_2
  359. do_enabled_lanes_reset(2, cfg_rcwsrds2, serdes2_base, true);
  360. #endif
  361. /* For each PLL being reset, and achieved PLL lock set RST_DONE */
  362. #ifdef CONFIG_SYS_FSL_SRDS_1
  363. cfg_tmp = cfg_rcwsrds1 & 0x3;
  364. do_pll_reset_done(cfg_tmp, serdes1_base);
  365. #endif
  366. #ifdef CONFIG_SYS_FSL_SRDS_2
  367. cfg_tmp = cfg_rcwsrds1 & 0xC;
  368. cfg_tmp >>= 2;
  369. do_pll_reset_done(cfg_tmp, serdes2_base);
  370. #endif
  371. return ret;
  372. }
  373. void fsl_serdes_init(void)
  374. {
  375. #if defined(CONFIG_FSL_MC_ENET) && !defined(CONFIG_SPL_BUILD)
  376. int i , j;
  377. for (i = XFI1, j = 1; i <= XFI8; i++, j++)
  378. xfi_dpmac[i] = j;
  379. for (i = SGMII1, j = 1; i <= SGMII16; i++, j++)
  380. sgmii_dpmac[i] = j;
  381. #endif
  382. #ifdef CONFIG_SYS_FSL_SRDS_1
  383. serdes_init(FSL_SRDS_1,
  384. CONFIG_SYS_FSL_LSCH3_SERDES_ADDR,
  385. FSL_CHASSIS3_SRDS1_REGSR,
  386. FSL_CHASSIS3_SRDS1_PRTCL_MASK,
  387. FSL_CHASSIS3_SRDS1_PRTCL_SHIFT,
  388. serdes1_prtcl_map);
  389. #endif
  390. #ifdef CONFIG_SYS_FSL_SRDS_2
  391. serdes_init(FSL_SRDS_2,
  392. CONFIG_SYS_FSL_LSCH3_SERDES_ADDR + FSL_SRDS_2 * 0x10000,
  393. FSL_CHASSIS3_SRDS2_REGSR,
  394. FSL_CHASSIS3_SRDS2_PRTCL_MASK,
  395. FSL_CHASSIS3_SRDS2_PRTCL_SHIFT,
  396. serdes2_prtcl_map);
  397. #endif
  398. }