ddrc.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594
  1. /*
  2. * Copyright 2021 NXP
  3. *
  4. * SPDX-License-Identifier: BSD-3-Clause
  5. *
  6. */
  7. #include <errno.h>
  8. #include <stdbool.h>
  9. #include <stdint.h>
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12. #include <common/debug.h>
  13. #include <ddr.h>
  14. #include <drivers/delay_timer.h>
  15. #include <immap.h>
  16. #define BIST_CR 0x80060000
  17. #define BIST_CR_EN 0x80000000
  18. #define BIST_CR_STAT 0x00000001
  19. #define CTLR_INTLV_MASK 0x20000000
  20. #pragma weak run_bist
  21. bool run_bist(void)
  22. {
  23. #ifdef BIST_EN
  24. return true;
  25. #else
  26. return false;
  27. #endif
  28. }
  29. /*
  30. * Perform build-in test on memory
  31. * timeout value in 10ms
  32. */
  33. int bist(const struct ccsr_ddr *ddr, int timeout)
  34. {
  35. const unsigned int test_pattern[10] = {
  36. 0xffffffff,
  37. 0x00000000,
  38. 0xaaaaaaaa,
  39. 0x55555555,
  40. 0xcccccccc,
  41. 0x33333333,
  42. 0x12345678,
  43. 0xabcdef01,
  44. 0xaa55aa55,
  45. 0x55aa55aa
  46. };
  47. unsigned int mtcr, err_detect, err_sbe;
  48. unsigned int cs0_config;
  49. unsigned int csn_bnds[4];
  50. int ret = 0;
  51. uint32_t i;
  52. #ifdef CONFIG_DDR_ADDR_DEC
  53. uint32_t dec_9 = ddr_in32(&ddr->dec[9]);
  54. uint32_t pos = 0U;
  55. uint32_t map_save = 0U;
  56. uint32_t temp32 = 0U;
  57. uint32_t map, shift, highest;
  58. #endif
  59. cs0_config = ddr_in32(&ddr->csn_cfg[0]);
  60. if ((cs0_config & CTLR_INTLV_MASK) != 0U) {
  61. /* set bnds to non-interleaving */
  62. for (i = 0U; i < 4U; i++) {
  63. csn_bnds[i] = ddr_in32(&ddr->bnds[i].a);
  64. ddr_out32(&ddr->bnds[i].a,
  65. (csn_bnds[i] & U(0xfffefffe)) >> 1U);
  66. }
  67. ddr_out32(&ddr->csn_cfg[0], cs0_config & ~CTLR_INTLV_MASK);
  68. #ifdef CONFIG_DDR_ADDR_DEC
  69. if ((dec_9 & 0x1U) != 0U) {
  70. highest = (dec_9 >> 26U) == U(0x3F) ? 0U : dec_9 >> 26U;
  71. pos = 37U;
  72. for (i = 0U; i < 36U; i++) { /* Go through all 37 */
  73. if ((i % 4U) == 0U) {
  74. temp32 = ddr_in32(&ddr->dec[i >> 2U]);
  75. }
  76. shift = (3U - i % 4U) * 8U + 2U;
  77. map = (temp32 >> shift) & U(0x3F);
  78. if (map > highest && map != U(0x3F)) {
  79. highest = map;
  80. pos = i;
  81. }
  82. }
  83. debug("\nFound highest position %d, mapping to %d, ",
  84. pos, highest);
  85. map_save = ddr_in32(&ddr->dec[pos >> 2]);
  86. shift = (3U - pos % 4U) * 8U + 2U;
  87. debug("in dec[%d], bit %d (0x%x)\n",
  88. pos >> 2U, shift, map_save);
  89. temp32 = map_save & ~(U(0x3F) << shift);
  90. temp32 |= 8U << shift;
  91. ddr_out32(&ddr->dec[pos >> 2U], temp32);
  92. timeout <<= 2U;
  93. debug("Increase wait time to %d ms\n", timeout * 10);
  94. }
  95. #endif
  96. }
  97. for (i = 0U; i < 10U; i++) {
  98. ddr_out32(&ddr->mtp[i], test_pattern[i]);
  99. }
  100. mtcr = BIST_CR;
  101. ddr_out32(&ddr->mtcr, mtcr);
  102. do {
  103. mdelay(10);
  104. mtcr = ddr_in32(&ddr->mtcr);
  105. } while (timeout-- > 0 && ((mtcr & BIST_CR_EN) != 0));
  106. if (timeout <= 0) {
  107. ERROR("Timeout\n");
  108. } else {
  109. debug("Timer remains %d\n", timeout);
  110. }
  111. err_detect = ddr_in32(&ddr->err_detect);
  112. err_sbe = ddr_in32(&ddr->err_sbe);
  113. if (err_detect != 0U || ((err_sbe & U(0xffff)) != 0U)) {
  114. ERROR("ECC error detected\n");
  115. ret = -EIO;
  116. }
  117. if ((cs0_config & CTLR_INTLV_MASK) != 0) {
  118. for (i = 0U; i < 4U; i++) {
  119. ddr_out32(&ddr->bnds[i].a, csn_bnds[i]);
  120. }
  121. ddr_out32(&ddr->csn_cfg[0], cs0_config);
  122. #ifdef CONFIG_DDR_ADDR_DEC
  123. if ((dec_9 & U(0x1)) != 0U) {
  124. ddr_out32(&ddr->dec[pos >> 2], map_save);
  125. }
  126. #endif
  127. }
  128. if ((mtcr & BIST_CR_STAT) != 0) {
  129. ERROR("Built-in self test failed\n");
  130. ret = -EIO;
  131. } else {
  132. NOTICE("Build-in self test passed\n");
  133. }
  134. return ret;
  135. }
  136. void dump_ddrc(unsigned int *ddr)
  137. {
  138. #ifdef DDR_DEBUG
  139. uint32_t i;
  140. unsigned long val;
  141. for (i = 0U; i < U(0x400); i++, ddr++) {
  142. val = ddr_in32(ddr);
  143. if (val != 0U) { /* skip zeros */
  144. debug("*0x%lx = 0x%lx\n", (unsigned long)ddr, val);
  145. }
  146. }
  147. #endif
  148. }
  149. #ifdef ERRATA_DDR_A009803
  150. static void set_wait_for_bits_clear(const void *ptr,
  151. unsigned int value,
  152. unsigned int bits)
  153. {
  154. int timeout = 1000;
  155. ddr_out32(ptr, value);
  156. do {
  157. udelay(100);
  158. } while (timeout-- > 0 && ((ddr_in32(ptr) & bits) != 0));
  159. if (timeout <= 0) {
  160. ERROR("wait for clear timeout.\n");
  161. }
  162. }
  163. #endif
  164. #if (DDRC_NUM_CS > 4)
  165. #error Invalid setting for DDRC_NUM_CS
  166. #endif
  167. /*
  168. * If supported by the platform, writing to DDR controller takes two
  169. * passes to deassert DDR reset to comply with JEDEC specs for RDIMMs.
  170. */
  171. int ddrc_set_regs(const unsigned long clk,
  172. const struct ddr_cfg_regs *regs,
  173. const struct ccsr_ddr *ddr,
  174. int twopass)
  175. {
  176. unsigned int i, bus_width;
  177. unsigned int temp_sdram_cfg;
  178. unsigned int total_mem_per_ctrl, total_mem_per_ctrl_adj;
  179. const int mod_bnds = regs->cs[0].config & CTLR_INTLV_MASK;
  180. int timeout;
  181. int ret = 0;
  182. #if defined(ERRATA_DDR_A009942) || defined(ERRATA_DDR_A010165)
  183. unsigned long ddr_freq;
  184. unsigned int tmp;
  185. #ifdef ERRATA_DDR_A009942
  186. unsigned int check;
  187. unsigned int cpo_min = U(0xff);
  188. unsigned int cpo_max = 0U;
  189. #endif
  190. #endif
  191. if (twopass == 2U) {
  192. goto after_reset;
  193. }
  194. /* Set cdr1 first in case 0.9v VDD is enabled for some SoCs*/
  195. ddr_out32(&ddr->ddr_cdr1, regs->cdr[0]);
  196. ddr_out32(&ddr->sdram_clk_cntl, regs->clk_cntl);
  197. for (i = 0U; i < DDRC_NUM_CS; i++) {
  198. if (mod_bnds != 0U) {
  199. ddr_out32(&ddr->bnds[i].a,
  200. (regs->cs[i].bnds & U(0xfffefffe)) >> 1U);
  201. } else {
  202. ddr_out32(&ddr->bnds[i].a, regs->cs[i].bnds);
  203. }
  204. ddr_out32(&ddr->csn_cfg_2[i], regs->cs[i].config_2);
  205. }
  206. ddr_out32(&ddr->timing_cfg_0, regs->timing_cfg[0]);
  207. ddr_out32(&ddr->timing_cfg_1, regs->timing_cfg[1]);
  208. ddr_out32(&ddr->timing_cfg_2, regs->timing_cfg[2]);
  209. ddr_out32(&ddr->timing_cfg_3, regs->timing_cfg[3]);
  210. ddr_out32(&ddr->timing_cfg_4, regs->timing_cfg[4]);
  211. ddr_out32(&ddr->timing_cfg_5, regs->timing_cfg[5]);
  212. ddr_out32(&ddr->timing_cfg_6, regs->timing_cfg[6]);
  213. ddr_out32(&ddr->timing_cfg_7, regs->timing_cfg[7]);
  214. ddr_out32(&ddr->timing_cfg_8, regs->timing_cfg[8]);
  215. ddr_out32(&ddr->timing_cfg_9, regs->timing_cfg[9]);
  216. ddr_out32(&ddr->zq_cntl, regs->zq_cntl);
  217. for (i = 0U; i < 4U; i++) {
  218. ddr_out32(&ddr->dq_map[i], regs->dq_map[i]);
  219. }
  220. ddr_out32(&ddr->sdram_cfg_3, regs->sdram_cfg[2]);
  221. ddr_out32(&ddr->sdram_mode, regs->sdram_mode[0]);
  222. ddr_out32(&ddr->sdram_mode_2, regs->sdram_mode[1]);
  223. ddr_out32(&ddr->sdram_mode_3, regs->sdram_mode[2]);
  224. ddr_out32(&ddr->sdram_mode_4, regs->sdram_mode[3]);
  225. ddr_out32(&ddr->sdram_mode_5, regs->sdram_mode[4]);
  226. ddr_out32(&ddr->sdram_mode_6, regs->sdram_mode[5]);
  227. ddr_out32(&ddr->sdram_mode_7, regs->sdram_mode[6]);
  228. ddr_out32(&ddr->sdram_mode_8, regs->sdram_mode[7]);
  229. ddr_out32(&ddr->sdram_mode_9, regs->sdram_mode[8]);
  230. ddr_out32(&ddr->sdram_mode_10, regs->sdram_mode[9]);
  231. ddr_out32(&ddr->sdram_mode_11, regs->sdram_mode[10]);
  232. ddr_out32(&ddr->sdram_mode_12, regs->sdram_mode[11]);
  233. ddr_out32(&ddr->sdram_mode_13, regs->sdram_mode[12]);
  234. ddr_out32(&ddr->sdram_mode_14, regs->sdram_mode[13]);
  235. ddr_out32(&ddr->sdram_mode_15, regs->sdram_mode[14]);
  236. ddr_out32(&ddr->sdram_mode_16, regs->sdram_mode[15]);
  237. ddr_out32(&ddr->sdram_md_cntl, regs->md_cntl);
  238. #ifdef ERRATA_DDR_A009663
  239. ddr_out32(&ddr->sdram_interval,
  240. regs->interval & ~SDRAM_INTERVAL_BSTOPRE);
  241. #else
  242. ddr_out32(&ddr->sdram_interval, regs->interval);
  243. #endif
  244. ddr_out32(&ddr->sdram_data_init, regs->data_init);
  245. if (regs->eor != 0) {
  246. ddr_out32(&ddr->eor, regs->eor);
  247. }
  248. ddr_out32(&ddr->wrlvl_cntl, regs->wrlvl_cntl[0]);
  249. #ifndef NXP_DDR_EMU
  250. /*
  251. * Skip these two registers if running on emulator
  252. * because emulator doesn't have skew between bytes.
  253. */
  254. if (regs->wrlvl_cntl[1] != 0) {
  255. ddr_out32(&ddr->ddr_wrlvl_cntl_2, regs->wrlvl_cntl[1]);
  256. }
  257. if (regs->wrlvl_cntl[2] != 0) {
  258. ddr_out32(&ddr->ddr_wrlvl_cntl_3, regs->wrlvl_cntl[2]);
  259. }
  260. #endif
  261. ddr_out32(&ddr->ddr_sr_cntr, regs->ddr_sr_cntr);
  262. ddr_out32(&ddr->ddr_sdram_rcw_1, regs->sdram_rcw[0]);
  263. ddr_out32(&ddr->ddr_sdram_rcw_2, regs->sdram_rcw[1]);
  264. ddr_out32(&ddr->ddr_sdram_rcw_3, regs->sdram_rcw[2]);
  265. ddr_out32(&ddr->ddr_sdram_rcw_4, regs->sdram_rcw[3]);
  266. ddr_out32(&ddr->ddr_sdram_rcw_5, regs->sdram_rcw[4]);
  267. ddr_out32(&ddr->ddr_sdram_rcw_6, regs->sdram_rcw[5]);
  268. ddr_out32(&ddr->ddr_cdr2, regs->cdr[1]);
  269. ddr_out32(&ddr->sdram_cfg_2, regs->sdram_cfg[1]);
  270. ddr_out32(&ddr->init_addr, regs->init_addr);
  271. ddr_out32(&ddr->init_ext_addr, regs->init_ext_addr);
  272. #ifdef ERRATA_DDR_A009803
  273. /* part 1 of 2 */
  274. if ((regs->sdram_cfg[1] & SDRAM_CFG2_AP_EN) != 0) {
  275. if ((regs->sdram_cfg[0] & SDRAM_CFG_RD_EN) != 0) {
  276. ddr_out32(&ddr->ddr_sdram_rcw_2,
  277. regs->sdram_rcw[1] & ~0xf0);
  278. }
  279. ddr_out32(&ddr->err_disable,
  280. regs->err_disable | DDR_ERR_DISABLE_APED);
  281. }
  282. #else
  283. ddr_out32(&ddr->err_disable, regs->err_disable);
  284. #endif
  285. ddr_out32(&ddr->err_int_en, regs->err_int_en);
  286. /* For DDRC 5.05 only */
  287. if (get_ddrc_version(ddr) == 0x50500) {
  288. ddr_out32(&ddr->tx_cfg[1], 0x1f1f1f1f);
  289. ddr_out32(&ddr->debug[3], 0x124a02c0);
  290. }
  291. for (i = 0U; i < 4U; i++) {
  292. if (regs->tx_cfg[i] != 0) {
  293. ddr_out32(&ddr->tx_cfg[i], regs->tx_cfg[i]);
  294. }
  295. }
  296. for (i = 0U; i < 64U; i++) {
  297. if (regs->debug[i] != 0) {
  298. #ifdef ERRATA_DDR_A009942
  299. if (i == 28U) {
  300. continue;
  301. }
  302. #endif
  303. ddr_out32(&ddr->debug[i], regs->debug[i]);
  304. }
  305. }
  306. #ifdef CONFIG_DDR_ADDR_DEC
  307. if ((regs->dec[9] & 1) != 0U) {
  308. for (i = 0U; i < 10U; i++) {
  309. ddr_out32(&ddr->dec[i], regs->dec[i]);
  310. }
  311. if (mod_bnds != 0) {
  312. debug("Disable address decoding\n");
  313. ddr_out32(&ddr->dec[9], 0);
  314. }
  315. }
  316. #endif
  317. #ifdef ERRATA_DDR_A008511
  318. /* Part 1 of 2 */
  319. /* This erraum only applies to version 5.2.1 */
  320. if (get_ddrc_version(ddr) == 0x50200) {
  321. ERROR("Unsupported SoC.\n");
  322. } else if (get_ddrc_version(ddr) == 0x50201) {
  323. ddr_out32(&ddr->debug[37], (U(1) << 31));
  324. ddr_out32(&ddr->ddr_cdr2,
  325. regs->cdr[1] | DDR_CDR2_VREF_TRAIN_EN);
  326. } else {
  327. debug("Erratum A008511 doesn't apply.\n");
  328. }
  329. #endif
  330. #ifdef ERRATA_DDR_A009942
  331. ddr_freq = clk / 1000000U;
  332. tmp = ddr_in32(&ddr->debug[28]);
  333. tmp &= U(0xff0fff00);
  334. tmp |= ddr_freq <= 1333U ? U(0x0080006a) :
  335. (ddr_freq <= 1600U ? U(0x0070006f) :
  336. (ddr_freq <= 1867U ? U(0x00700076) : U(0x0060007b)));
  337. if (regs->debug[28] != 0) {
  338. tmp &= ~0xff;
  339. tmp |= regs->debug[28] & 0xff;
  340. } else {
  341. WARN("Warning: Optimal CPO value not set.\n");
  342. }
  343. ddr_out32(&ddr->debug[28], tmp);
  344. #endif
  345. #ifdef ERRATA_DDR_A010165
  346. ddr_freq = clk / 1000000U;
  347. if ((ddr_freq > 1900) && (ddr_freq < 2300)) {
  348. tmp = ddr_in32(&ddr->debug[28]);
  349. ddr_out32(&ddr->debug[28], tmp | 0x000a0000);
  350. }
  351. #endif
  352. /*
  353. * For RDIMMs, JEDEC spec requires clocks to be stable before reset is
  354. * deasserted. Clocks start when any chip select is enabled and clock
  355. * control register is set. Because all DDR components are connected to
  356. * one reset signal, this needs to be done in two steps. Step 1 is to
  357. * get the clocks started. Step 2 resumes after reset signal is
  358. * deasserted.
  359. */
  360. if (twopass == 1) {
  361. udelay(200);
  362. return 0;
  363. }
  364. /* As per new sequence flow shall be write CSn_CONFIG registers needs to
  365. * be set after all the other DDR controller registers are set, then poll
  366. * for PHY_INIT_CMPLT = 1 , then wait at least 100us (micro seconds),
  367. * then set the MEM_EN = 1
  368. */
  369. for (i = 0U; i < DDRC_NUM_CS; i++) {
  370. if (mod_bnds != 0U && i == 0U) {
  371. ddr_out32(&ddr->csn_cfg[i],
  372. (regs->cs[i].config & ~CTLR_INTLV_MASK));
  373. } else {
  374. ddr_out32(&ddr->csn_cfg[i], regs->cs[i].config);
  375. }
  376. }
  377. after_reset:
  378. /* Set, but do not enable the memory */
  379. temp_sdram_cfg = regs->sdram_cfg[0];
  380. temp_sdram_cfg &= ~(SDRAM_CFG_MEM_EN);
  381. ddr_out32(&ddr->sdram_cfg, temp_sdram_cfg);
  382. if (get_ddrc_version(ddr) < U(0x50500)) {
  383. /*
  384. * 500 painful micro-seconds must elapse between
  385. * the DDR clock setup and the DDR config enable.
  386. * DDR2 need 200 us, and DDR3 need 500 us from spec,
  387. * we choose the max, that is 500 us for all of case.
  388. */
  389. udelay(500);
  390. /* applied memory barrier */
  391. mb();
  392. isb();
  393. } else {
  394. /* wait for PHY complete */
  395. timeout = 40;
  396. while (((ddr_in32(&ddr->ddr_dsr2) & 0x4) != 0) &&
  397. (timeout > 0)) {
  398. udelay(500);
  399. timeout--;
  400. }
  401. if (timeout <= 0) {
  402. printf("PHY handshake timeout, ddr_dsr2 = %x\n",
  403. ddr_in32(&ddr->ddr_dsr2));
  404. } else {
  405. debug("PHY handshake completed, timer remains %d\n",
  406. timeout);
  407. }
  408. }
  409. temp_sdram_cfg = ddr_in32(&ddr->sdram_cfg);
  410. /* Let the controller go */
  411. udelay(100);
  412. ddr_out32(&ddr->sdram_cfg, temp_sdram_cfg | SDRAM_CFG_MEM_EN);
  413. /* applied memory barrier */
  414. mb();
  415. isb();
  416. total_mem_per_ctrl = 0;
  417. for (i = 0; i < DDRC_NUM_CS; i++) {
  418. if ((regs->cs[i].config & 0x80000000) == 0) {
  419. continue;
  420. }
  421. total_mem_per_ctrl += 1 << (
  422. ((regs->cs[i].config >> 14) & 0x3) + 2 +
  423. ((regs->cs[i].config >> 8) & 0x7) + 12 +
  424. ((regs->cs[i].config >> 4) & 0x3) + 0 +
  425. ((regs->cs[i].config >> 0) & 0x7) + 8 +
  426. ((regs->sdram_cfg[2] >> 4) & 0x3) +
  427. 3 - ((regs->sdram_cfg[0] >> 19) & 0x3) -
  428. 26); /* minus 26 (count of 64M) */
  429. }
  430. total_mem_per_ctrl_adj = total_mem_per_ctrl;
  431. /*
  432. * total memory / bus width = transactions needed
  433. * transactions needed / data rate = seconds
  434. * to add plenty of buffer, double the time
  435. * For example, 2GB on 666MT/s 64-bit bus takes about 402ms
  436. * Let's wait for 800ms
  437. */
  438. bus_width = 3 - ((ddr_in32(&ddr->sdram_cfg) & SDRAM_CFG_DBW_MASK)
  439. >> SDRAM_CFG_DBW_SHIFT);
  440. timeout = ((total_mem_per_ctrl_adj << (6 - bus_width)) * 100 /
  441. (clk >> 20)) << 2;
  442. total_mem_per_ctrl_adj >>= 4; /* shift down to gb size */
  443. if ((ddr_in32(&ddr->sdram_cfg_2) & SDRAM_CFG2_D_INIT) != 0) {
  444. debug("total size %d GB\n", total_mem_per_ctrl_adj);
  445. debug("Need to wait up to %d ms\n", timeout * 10);
  446. do {
  447. mdelay(10);
  448. } while (timeout-- > 0 &&
  449. ((ddr_in32(&ddr->sdram_cfg_2) & SDRAM_CFG2_D_INIT)) != 0);
  450. if (timeout <= 0) {
  451. if (ddr_in32(&ddr->debug[1]) & 0x3d00) {
  452. ERROR("Found training error(s): 0x%x\n",
  453. ddr_in32(&ddr->debug[1]));
  454. }
  455. ERROR("Error: Waiting for D_INIT timeout.\n");
  456. return -EIO;
  457. }
  458. }
  459. if (mod_bnds != 0U) {
  460. debug("Restore original bnds\n");
  461. for (i = 0U; i < DDRC_NUM_CS; i++) {
  462. ddr_out32(&ddr->bnds[i].a, regs->cs[i].bnds);
  463. }
  464. ddr_out32(&ddr->csn_cfg[0], regs->cs[0].config);
  465. #ifdef CONFIG_DDR_ADDR_DEC
  466. if ((regs->dec[9] & U(0x1)) != 0U) {
  467. debug("Restore address decoding\n");
  468. ddr_out32(&ddr->dec[9], regs->dec[9]);
  469. }
  470. #endif
  471. }
  472. #ifdef ERRATA_DDR_A009803
  473. /* Part 2 of 2 */
  474. if ((regs->sdram_cfg[1] & SDRAM_CFG2_AP_EN) != 0) {
  475. timeout = 400;
  476. do {
  477. mdelay(1);
  478. } while (timeout-- > 0 && ((ddr_in32(&ddr->debug[1]) & 0x2) == 0));
  479. if ((regs->sdram_cfg[0] & SDRAM_CFG_RD_EN) != 0) {
  480. for (i = 0U; i < DDRC_NUM_CS; i++) {
  481. if ((regs->cs[i].config & SDRAM_CS_CONFIG_EN) == 0) {
  482. continue;
  483. }
  484. set_wait_for_bits_clear(&ddr->sdram_md_cntl,
  485. MD_CNTL_MD_EN |
  486. MD_CNTL_CS_SEL(i) |
  487. 0x070000ed,
  488. MD_CNTL_MD_EN);
  489. udelay(1);
  490. }
  491. }
  492. ddr_out32(&ddr->err_disable,
  493. regs->err_disable & ~DDR_ERR_DISABLE_APED);
  494. }
  495. #endif
  496. #ifdef ERRATA_DDR_A009663
  497. ddr_out32(&ddr->sdram_interval, regs->interval);
  498. #endif
  499. #ifdef ERRATA_DDR_A009942
  500. timeout = 400;
  501. do {
  502. mdelay(1);
  503. } while (timeout-- > 0 && ((ddr_in32(&ddr->debug[1]) & 0x2) == 0));
  504. tmp = (regs->sdram_cfg[0] >> 19) & 0x3;
  505. check = (tmp == DDR_DBUS_64) ? 4 : ((tmp == DDR_DBUS_32) ? 2 : 1);
  506. for (i = 0; i < check; i++) {
  507. tmp = ddr_in32(&ddr->debug[9 + i]);
  508. debug("Reading debug[%d] as 0x%x\n", i + 9, tmp);
  509. cpo_min = min(cpo_min,
  510. min((tmp >> 24) & 0xff, (tmp >> 8) & 0xff));
  511. cpo_max = max(cpo_max,
  512. max((tmp >> 24) & 0xff, (tmp >> 8) & 0xff));
  513. }
  514. if ((regs->sdram_cfg[0] & SDRAM_CFG_ECC_EN) != 0) {
  515. tmp = ddr_in32(&ddr->debug[13]);
  516. cpo_min = min(cpo_min, (tmp >> 24) & 0xff);
  517. cpo_max = max(cpo_max, (tmp >> 24) & 0xff);
  518. }
  519. debug("cpo_min 0x%x\n", cpo_min);
  520. debug("cpo_max 0x%x\n", cpo_max);
  521. tmp = ddr_in32(&ddr->debug[28]);
  522. debug("debug[28] 0x%x\n", tmp);
  523. if ((cpo_min + 0x3B) < (tmp & 0xff)) {
  524. WARN("Warning: A009942 requires setting cpo_sample to 0x%x\n",
  525. (cpo_min + cpo_max) / 2 + 0x27);
  526. } else {
  527. debug("Optimal cpo_sample 0x%x\n",
  528. (cpo_min + cpo_max) / 2 + 0x27);
  529. }
  530. #endif
  531. if (run_bist() != 0) {
  532. if ((ddr_in32(&ddr->debug[1]) &
  533. ((get_ddrc_version(ddr) == 0x50500) ? 0x3c00 : 0x3d00)) != 0) {
  534. ERROR("Found training error(s): 0x%x\n",
  535. ddr_in32(&ddr->debug[1]));
  536. return -EIO;
  537. }
  538. INFO("Running built-in self test ...\n");
  539. /* give it 10x time to cover whole memory */
  540. timeout = ((total_mem_per_ctrl << (6 - bus_width)) *
  541. 100 / (clk >> 20)) * 10;
  542. INFO("\tWait up to %d ms\n", timeout * 10);
  543. ret = bist(ddr, timeout);
  544. }
  545. dump_ddrc((void *)ddr);
  546. return ret;
  547. }