fsl_corenet_serdes.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530
  1. /*
  2. * Copyright 2009-2011 Freescale Semiconductor, Inc.
  3. *
  4. * See file CREDITS for list of people who contributed to this
  5. * project.
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License as
  9. * published by the Free Software Foundation; either version 2 of
  10. * the License, or (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  20. * MA 02111-1307 USA
  21. */
  22. #include <common.h>
  23. #ifdef CONFIG_SYS_P4080_ERRATUM_SERDES8
  24. #include <hwconfig.h>
  25. #endif
  26. #include <asm/fsl_serdes.h>
  27. #include <asm/immap_85xx.h>
  28. #include <asm/io.h>
  29. #include <asm/processor.h>
  30. #include <asm/fsl_law.h>
  31. #include <asm/errno.h>
  32. #include "fsl_corenet_serdes.h"
  33. static u32 serdes_prtcl_map;
  34. #define HWCONFIG_BUFFER_SIZE 128
  35. #ifdef DEBUG
  36. static const char *serdes_prtcl_str[] = {
  37. [NONE] = "NA",
  38. [PCIE1] = "PCIE1",
  39. [PCIE2] = "PCIE2",
  40. [PCIE3] = "PCIE3",
  41. [PCIE4] = "PCIE4",
  42. [SATA1] = "SATA1",
  43. [SATA2] = "SATA2",
  44. [SRIO1] = "SRIO1",
  45. [SRIO2] = "SRIO2",
  46. [SGMII_FM1_DTSEC1] = "SGMII_FM1_DTSEC1",
  47. [SGMII_FM1_DTSEC2] = "SGMII_FM1_DTSEC2",
  48. [SGMII_FM1_DTSEC3] = "SGMII_FM1_DTSEC3",
  49. [SGMII_FM1_DTSEC4] = "SGMII_FM1_DTSEC4",
  50. [SGMII_FM1_DTSEC5] = "SGMII_FM1_DTSEC5",
  51. [SGMII_FM2_DTSEC1] = "SGMII_FM2_DTSEC1",
  52. [SGMII_FM2_DTSEC2] = "SGMII_FM2_DTSEC2",
  53. [SGMII_FM2_DTSEC3] = "SGMII_FM2_DTSEC3",
  54. [SGMII_FM2_DTSEC4] = "SGMII_FM2_DTSEC4",
  55. [XAUI_FM1] = "XAUI_FM1",
  56. [XAUI_FM2] = "XAUI_FM2",
  57. [AURORA] = "DEBUG",
  58. };
  59. #endif
  60. static const struct {
  61. int idx;
  62. unsigned int lpd; /* RCW lane powerdown bit */
  63. int bank;
  64. } lanes[SRDS_MAX_LANES] = {
  65. { 0, 152, FSL_SRDS_BANK_1 },
  66. { 1, 153, FSL_SRDS_BANK_1 },
  67. { 2, 154, FSL_SRDS_BANK_1 },
  68. { 3, 155, FSL_SRDS_BANK_1 },
  69. { 4, 156, FSL_SRDS_BANK_1 },
  70. { 5, 157, FSL_SRDS_BANK_1 },
  71. { 6, 158, FSL_SRDS_BANK_1 },
  72. { 7, 159, FSL_SRDS_BANK_1 },
  73. { 8, 160, FSL_SRDS_BANK_1 },
  74. { 9, 161, FSL_SRDS_BANK_1 },
  75. { 16, 162, FSL_SRDS_BANK_2 },
  76. { 17, 163, FSL_SRDS_BANK_2 },
  77. { 18, 164, FSL_SRDS_BANK_2 },
  78. { 19, 165, FSL_SRDS_BANK_2 },
  79. { 20, 170, FSL_SRDS_BANK_3 },
  80. { 21, 171, FSL_SRDS_BANK_3 },
  81. { 22, 172, FSL_SRDS_BANK_3 },
  82. { 23, 173, FSL_SRDS_BANK_3 },
  83. };
  84. int serdes_get_lane_idx(int lane)
  85. {
  86. return lanes[lane].idx;
  87. }
  88. int serdes_get_bank_by_lane(int lane)
  89. {
  90. return lanes[lane].bank;
  91. }
  92. int serdes_lane_enabled(int lane)
  93. {
  94. ccsr_gur_t *gur = (void *)(CONFIG_SYS_MPC85xx_GUTS_ADDR);
  95. serdes_corenet_t *regs = (void *)CONFIG_SYS_FSL_CORENET_SERDES_ADDR;
  96. int bank = lanes[lane].bank;
  97. int word = lanes[lane].lpd / 32;
  98. int bit = lanes[lane].lpd % 32;
  99. if (in_be32(&regs->bank[bank].rstctl) & SRDS_RSTCTL_SDPD)
  100. return 0;
  101. #ifdef CONFIG_SYS_P4080_ERRATUM_SERDES8
  102. /*
  103. * For banks two and three, use the srds_lpd_b[] array instead of the
  104. * RCW, because this array contains the real values of SRDS_LPD_B2 and
  105. * SRDS_LPD_B3.
  106. */
  107. if (bank > 0)
  108. return !(srds_lpd_b[bank] & (8 >> (lane - (6 + 4 * bank))));
  109. #endif
  110. return !(in_be32(&gur->rcwsr[word]) & (0x80000000 >> bit));
  111. }
  112. int is_serdes_configured(enum srds_prtcl device)
  113. {
  114. ccsr_gur_t *gur = (void *)(CONFIG_SYS_MPC85xx_GUTS_ADDR);
  115. /* Is serdes enabled at all? */
  116. if (!(in_be32(&gur->rcwsr[5]) & FSL_CORENET_RCWSR5_SRDS_EN))
  117. return 0;
  118. return (1 << device) & serdes_prtcl_map;
  119. }
  120. static int __serdes_get_first_lane(uint32_t prtcl, enum srds_prtcl device)
  121. {
  122. int i;
  123. for (i = 0; i < SRDS_MAX_LANES; i++) {
  124. if (serdes_get_prtcl(prtcl, i) == device)
  125. return i;
  126. }
  127. return -ENODEV;
  128. }
  129. /*
  130. * Returns the SERDES lane (0..SRDS_MAX_LANES-1) that routes to the given
  131. * device. This depends on the current SERDES protocol, as defined in the RCW.
  132. *
  133. * Returns a negative error code if SERDES is disabled or the given device is
  134. * not supported in the current SERDES protocol.
  135. */
  136. int serdes_get_first_lane(enum srds_prtcl device)
  137. {
  138. u32 prtcl;
  139. const ccsr_gur_t *gur;
  140. gur = (typeof(gur))CONFIG_SYS_MPC85xx_GUTS_ADDR;
  141. /* Is serdes enabled at all? */
  142. if (unlikely((in_be32(&gur->rcwsr[5]) & 0x2000) == 0))
  143. return -ENODEV;
  144. prtcl = (in_be32(&gur->rcwsr[4]) & FSL_CORENET_RCWSR4_SRDS_PRTCL) >> 26;
  145. return __serdes_get_first_lane(prtcl, device);
  146. }
  147. #ifndef CONFIG_SYS_DCSRBAR_PHYS
  148. #define CONFIG_SYS_DCSRBAR_PHYS 0x80000000 /* Must be 1GB-aligned for rev1.0 */
  149. #define CONFIG_SYS_DCSRBAR 0x80000000
  150. #define __DCSR_NOT_DEFINED_BY_CONFIG
  151. #endif
  152. #ifdef CONFIG_SYS_P4080_ERRATUM_SERDES8
  153. static void enable_bank(ccsr_gur_t *gur, int bank)
  154. {
  155. u32 rcw5;
  156. /*
  157. * Enable the lanes SRDS_LPD_Bn. The RCW bits are read-only in
  158. * CCSR, and read/write in DSCR.
  159. */
  160. rcw5 = in_be32(gur->rcwsr + 5);
  161. if (bank == FSL_SRDS_BANK_2) {
  162. rcw5 &= ~FSL_CORENET_RCWSRn_SRDS_LPD_B2;
  163. rcw5 |= srds_lpd_b[bank] << 26;
  164. } else if (bank == FSL_SRDS_BANK_3) {
  165. rcw5 &= ~FSL_CORENET_RCWSRn_SRDS_LPD_B3;
  166. rcw5 |= srds_lpd_b[bank] << 18;
  167. } else {
  168. printf("SERDES: enable_bank: bad bank %d\n", bank + 1);
  169. return;
  170. }
  171. /* See similar code in cpu/mpc85xx/cpu_init.c for an explanation
  172. * of the DCSR mapping.
  173. */
  174. {
  175. #ifdef __DCSR_NOT_DEFINED_BY_CONFIG
  176. struct law_entry law = find_law(CONFIG_SYS_DCSRBAR_PHYS);
  177. int law_index;
  178. if (law.index == -1)
  179. law_index = set_next_law(CONFIG_SYS_DCSRBAR_PHYS,
  180. LAW_SIZE_1M, LAW_TRGT_IF_DCSR);
  181. else
  182. set_law(law.index, CONFIG_SYS_DCSRBAR_PHYS, LAW_SIZE_1M,
  183. LAW_TRGT_IF_DCSR);
  184. #endif
  185. u32 *p = (void *)CONFIG_SYS_DCSRBAR + 0x20114;
  186. out_be32(p, rcw5);
  187. #ifdef __DCSR_NOT_DEFINED_BY_CONFIG
  188. if (law.index == -1)
  189. disable_law(law_index);
  190. else
  191. set_law(law.index, law.addr, law.size, law.trgt_id);
  192. #endif
  193. }
  194. }
  195. /*
  196. * To avoid problems with clock jitter, rev 2 p4080 uses the pll from
  197. * bank 3 to clock banks 2 and 3, as well as a limited selection of
  198. * protocol configurations. This requires that banks 2 and 3's lanes be
  199. * disabled in the RCW, and enabled with some fixup here to re-enable
  200. * them, and to configure bank 2's clock parameters in bank 3's pll in
  201. * cases where they differ.
  202. */
  203. static void p4080_erratum_serdes8(serdes_corenet_t *regs, ccsr_gur_t *gur,
  204. u32 devdisr, u32 devdisr2, int cfg)
  205. {
  206. int srds_ratio_b2;
  207. int rfck_sel;
  208. /*
  209. * The disabled lanes of bank 2 will cause the associated
  210. * logic blocks to be disabled in DEVDISR. We reverse that here.
  211. *
  212. * Note that normally it is not permitted to clear DEVDISR bits
  213. * once the device has been disabled, but the hardware people
  214. * say that this special case is OK.
  215. */
  216. clrbits_be32(&gur->devdisr, devdisr);
  217. clrbits_be32(&gur->devdisr2, devdisr2);
  218. /*
  219. * Some protocols require special handling. There are a few
  220. * additional protocol configurations that can be used, which are
  221. * not listed here. See app note 4065 for supported protocol
  222. * configurations.
  223. */
  224. switch (cfg) {
  225. case 0x19:
  226. /*
  227. * Bank 2 has PCIe which wants BWSEL -- tell bank 3's PLL.
  228. * SGMII on bank 3 should still be usable.
  229. */
  230. setbits_be32(&regs->bank[FSL_SRDS_BANK_3].pllcr1,
  231. SRDS_PLLCR1_PLL_BWSEL);
  232. enable_bank(gur, FSL_SRDS_BANK_3);
  233. break;
  234. case 0x0f:
  235. case 0x10:
  236. /*
  237. * Banks 2 (XAUI) and 3 (SGMII) have different clocking
  238. * requirements in these configurations. Bank 3 cannot
  239. * be used and should have its lanes (but not the bank
  240. * itself) disabled in the RCW. We set up bank 3's pll
  241. * for bank 2's needs here.
  242. */
  243. srds_ratio_b2 = (in_be32(&gur->rcwsr[4]) >> 13) & 7;
  244. /* Determine refclock from XAUI ratio */
  245. switch (srds_ratio_b2) {
  246. case 1: /* 20:1 */
  247. rfck_sel = SRDS_PLLCR0_RFCK_SEL_156_25;
  248. break;
  249. case 2: /* 25:1 */
  250. rfck_sel = SRDS_PLLCR0_RFCK_SEL_125;
  251. break;
  252. default:
  253. printf("SERDES: bad SRDS_RATIO_B2 %d\n",
  254. srds_ratio_b2);
  255. return;
  256. }
  257. clrsetbits_be32(&regs->bank[FSL_SRDS_BANK_3].pllcr0,
  258. SRDS_PLLCR0_RFCK_SEL_MASK, rfck_sel);
  259. clrsetbits_be32(&regs->bank[FSL_SRDS_BANK_3].pllcr0,
  260. SRDS_PLLCR0_FRATE_SEL_MASK,
  261. SRDS_PLLCR0_FRATE_SEL_6_25);
  262. break;
  263. default:
  264. enable_bank(gur, FSL_SRDS_BANK_3);
  265. }
  266. }
  267. #endif
  268. void fsl_serdes_init(void)
  269. {
  270. ccsr_gur_t *gur = (void *)(CONFIG_SYS_MPC85xx_GUTS_ADDR);
  271. int cfg;
  272. serdes_corenet_t *srds_regs;
  273. int lane, bank, idx;
  274. enum srds_prtcl lane_prtcl;
  275. long long end_tick;
  276. int have_bank[SRDS_MAX_BANK] = {};
  277. #ifdef CONFIG_SYS_P4080_ERRATUM_SERDES8
  278. u32 serdes8_devdisr = 0;
  279. u32 serdes8_devdisr2 = 0;
  280. char srds_lpd_opt[16];
  281. const char *srds_lpd_arg;
  282. size_t arglen;
  283. #endif
  284. char buffer[HWCONFIG_BUFFER_SIZE];
  285. char *buf = NULL;
  286. /*
  287. * Extract hwconfig from environment since we have not properly setup
  288. * the environment but need it for ddr config params
  289. */
  290. if (getenv_f("hwconfig", buffer, sizeof(buffer)) > 0)
  291. buf = buffer;
  292. /* Is serdes enabled at all? */
  293. if (!(in_be32(&gur->rcwsr[5]) & FSL_CORENET_RCWSR5_SRDS_EN))
  294. return;
  295. srds_regs = (void *)(CONFIG_SYS_FSL_CORENET_SERDES_ADDR);
  296. cfg = (in_be32(&gur->rcwsr[4]) & FSL_CORENET_RCWSR4_SRDS_PRTCL) >> 26;
  297. debug("Using SERDES configuration 0x%x, lane settings:\n", cfg);
  298. if (!is_serdes_prtcl_valid(cfg)) {
  299. printf("SERDES[PRTCL] = 0x%x is not valid\n", cfg);
  300. return;
  301. }
  302. #ifdef CONFIG_SYS_P4080_ERRATUM_SERDES8
  303. /*
  304. * Store the values of the fsl_srds_lpd_b2 and fsl_srds_lpd_b3
  305. * hwconfig options into the srds_lpd_b[] array. See README.p4080ds
  306. * for a description of these options.
  307. */
  308. for (bank = 1; bank < ARRAY_SIZE(srds_lpd_b); bank++) {
  309. sprintf(srds_lpd_opt, "fsl_srds_lpd_b%u", bank + 1);
  310. srds_lpd_arg =
  311. hwconfig_subarg_f("serdes", srds_lpd_opt, &arglen, buf);
  312. if (srds_lpd_arg)
  313. srds_lpd_b[bank] =
  314. simple_strtoul(srds_lpd_arg, NULL, 0) & 0xf;
  315. }
  316. #endif
  317. /* Look for banks with all lanes disabled, and power down the bank. */
  318. for (lane = 0; lane < SRDS_MAX_LANES; lane++) {
  319. enum srds_prtcl lane_prtcl = serdes_get_prtcl(cfg, lane);
  320. if (serdes_lane_enabled(lane)) {
  321. have_bank[serdes_get_bank_by_lane(lane)] = 1;
  322. serdes_prtcl_map |= (1 << lane_prtcl);
  323. }
  324. }
  325. #ifdef CONFIG_SYS_P4080_ERRATUM_SERDES8
  326. /*
  327. * Bank two uses the clock from bank three, so if bank two is enabled,
  328. * then bank three must also be enabled.
  329. */
  330. if (have_bank[FSL_SRDS_BANK_2])
  331. have_bank[FSL_SRDS_BANK_3] = 1;
  332. #endif
  333. for (bank = 0; bank < SRDS_MAX_BANK; bank++) {
  334. if (!have_bank[bank]) {
  335. printf("SERDES: bank %d disabled\n", bank + 1);
  336. setbits_be32(&srds_regs->bank[bank].rstctl,
  337. SRDS_RSTCTL_SDPD);
  338. }
  339. }
  340. for (lane = 0; lane < SRDS_MAX_LANES; lane++) {
  341. idx = serdes_get_lane_idx(lane);
  342. lane_prtcl = serdes_get_prtcl(cfg, lane);
  343. #ifdef DEBUG
  344. switch (lane) {
  345. case 0:
  346. puts("Bank1: ");
  347. break;
  348. case 10:
  349. puts("\nBank2: ");
  350. break;
  351. case 14:
  352. puts("\nBank3: ");
  353. break;
  354. default:
  355. break;
  356. }
  357. printf("%s ", serdes_prtcl_str[lane_prtcl]);
  358. #endif
  359. #ifdef CONFIG_SYS_P4080_ERRATUM_SERDES8
  360. switch (lane_prtcl) {
  361. case PCIE1:
  362. case PCIE2:
  363. case PCIE3:
  364. serdes8_devdisr |= FSL_CORENET_DEVDISR_PCIE1 >>
  365. (lane_prtcl - PCIE1);
  366. break;
  367. case SRIO1:
  368. case SRIO2:
  369. serdes8_devdisr |= FSL_CORENET_DEVDISR_SRIO1 >>
  370. (lane_prtcl - SRIO1);
  371. break;
  372. case SGMII_FM1_DTSEC1:
  373. serdes8_devdisr2 |= FSL_CORENET_DEVDISR2_FM1 |
  374. FSL_CORENET_DEVDISR2_DTSEC1_1;
  375. break;
  376. case SGMII_FM1_DTSEC2:
  377. serdes8_devdisr2 |= FSL_CORENET_DEVDISR2_FM1 |
  378. FSL_CORENET_DEVDISR2_DTSEC1_2;
  379. break;
  380. case SGMII_FM1_DTSEC3:
  381. serdes8_devdisr2 |= FSL_CORENET_DEVDISR2_FM1 |
  382. FSL_CORENET_DEVDISR2_DTSEC1_3;
  383. break;
  384. case SGMII_FM1_DTSEC4:
  385. serdes8_devdisr2 |= FSL_CORENET_DEVDISR2_FM1 |
  386. FSL_CORENET_DEVDISR2_DTSEC1_4;
  387. break;
  388. case SGMII_FM2_DTSEC1:
  389. serdes8_devdisr2 |= FSL_CORENET_DEVDISR2_FM2 |
  390. FSL_CORENET_DEVDISR2_DTSEC2_1;
  391. break;
  392. case SGMII_FM2_DTSEC2:
  393. serdes8_devdisr2 |= FSL_CORENET_DEVDISR2_FM2 |
  394. FSL_CORENET_DEVDISR2_DTSEC2_2;
  395. break;
  396. case SGMII_FM2_DTSEC3:
  397. serdes8_devdisr2 |= FSL_CORENET_DEVDISR2_FM2 |
  398. FSL_CORENET_DEVDISR2_DTSEC2_3;
  399. break;
  400. case SGMII_FM2_DTSEC4:
  401. serdes8_devdisr2 |= FSL_CORENET_DEVDISR2_FM2 |
  402. FSL_CORENET_DEVDISR2_DTSEC2_4;
  403. break;
  404. case XAUI_FM1:
  405. case XAUI_FM2:
  406. if (lane_prtcl == XAUI_FM1)
  407. serdes8_devdisr2 |= FSL_CORENET_DEVDISR2_FM1 |
  408. FSL_CORENET_DEVDISR2_10GEC1;
  409. else
  410. serdes8_devdisr2 |= FSL_CORENET_DEVDISR2_FM2 |
  411. FSL_CORENET_DEVDISR2_10GEC2;
  412. break;
  413. case AURORA:
  414. break;
  415. default:
  416. break;
  417. }
  418. #endif
  419. }
  420. #ifdef DEBUG
  421. puts("\n");
  422. #endif
  423. for (idx = 0; idx < SRDS_MAX_BANK; idx++) {
  424. u32 rstctl;
  425. bank = idx;
  426. #ifdef CONFIG_SYS_P4080_ERRATUM_SERDES8
  427. /*
  428. * Change bank init order to 0, 2, 1, so that the third bank's
  429. * PLL is established before we start the second bank. The
  430. * second bank uses the third bank's PLL.
  431. */
  432. if (idx == 1)
  433. bank = FSL_SRDS_BANK_3;
  434. else if (idx == 2)
  435. bank = FSL_SRDS_BANK_2;
  436. #endif
  437. /* Skip disabled banks */
  438. if (!have_bank[bank])
  439. continue;
  440. #ifdef CONFIG_SYS_P4080_ERRATUM_SERDES8
  441. if (idx == 1) {
  442. /*
  443. * Re-enable devices on banks two and three that were
  444. * disabled by the RCW, and then enable bank three. The
  445. * devices need to be enabled before either bank is
  446. * powered up.
  447. */
  448. p4080_erratum_serdes8(srds_regs, gur, serdes8_devdisr,
  449. serdes8_devdisr2, cfg);
  450. } else if (idx == 2) {
  451. /* Eable bank two now that bank three is enabled. */
  452. enable_bank(gur, FSL_SRDS_BANK_2);
  453. }
  454. #endif
  455. /* reset banks for errata */
  456. setbits_be32(&srds_regs->bank[bank].rstctl, SRDS_RSTCTL_RST);
  457. /* wait for reset complete or 1-second timeout */
  458. end_tick = usec2ticks(1000000) + get_ticks();
  459. do {
  460. rstctl = in_be32(&srds_regs->bank[bank].rstctl);
  461. if (rstctl & SRDS_RSTCTL_RSTDONE)
  462. break;
  463. } while (end_tick > get_ticks());
  464. if (!(rstctl & SRDS_RSTCTL_RSTDONE)) {
  465. printf("SERDES: timeout resetting bank %d\n",
  466. bank + 1);
  467. continue;
  468. }
  469. }
  470. }