fsl_lsch3_serdes.c 11 KB

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