ddr3_read_leveling.c 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) Marvell International Ltd. and its affiliates
  4. */
  5. #include <common.h>
  6. #include <i2c.h>
  7. #include <spl.h>
  8. #include <asm/io.h>
  9. #include <asm/arch/cpu.h>
  10. #include <asm/arch/soc.h>
  11. #include "ddr3_hw_training.h"
  12. /*
  13. * Debug
  14. */
  15. #define DEBUG_RL_C(s, d, l) \
  16. DEBUG_RL_S(s); DEBUG_RL_D(d, l); DEBUG_RL_S("\n")
  17. #define DEBUG_RL_FULL_C(s, d, l) \
  18. DEBUG_RL_FULL_S(s); DEBUG_RL_FULL_D(d, l); DEBUG_RL_FULL_S("\n")
  19. #ifdef MV_DEBUG_RL
  20. #define DEBUG_RL_S(s) \
  21. debug_cond(ddr3_get_log_level() >= MV_LOG_LEVEL_2, "%s", s)
  22. #define DEBUG_RL_D(d, l) \
  23. debug_cond(ddr3_get_log_level() >= MV_LOG_LEVEL_2, "%x", d)
  24. #else
  25. #define DEBUG_RL_S(s)
  26. #define DEBUG_RL_D(d, l)
  27. #endif
  28. #ifdef MV_DEBUG_RL_FULL
  29. #define DEBUG_RL_FULL_S(s) puts(s)
  30. #define DEBUG_RL_FULL_D(d, l) printf("%x", d)
  31. #else
  32. #define DEBUG_RL_FULL_S(s)
  33. #define DEBUG_RL_FULL_D(d, l)
  34. #endif
  35. extern u32 rl_pattern[LEN_STD_PATTERN];
  36. #ifdef RL_MODE
  37. static int ddr3_read_leveling_single_cs_rl_mode(u32 cs, u32 freq,
  38. int ratio_2to1, u32 ecc,
  39. MV_DRAM_INFO *dram_info);
  40. #else
  41. static int ddr3_read_leveling_single_cs_window_mode(u32 cs, u32 freq,
  42. int ratio_2to1, u32 ecc,
  43. MV_DRAM_INFO *dram_info);
  44. #endif
  45. /*
  46. * Name: ddr3_read_leveling_hw
  47. * Desc: Execute the Read leveling phase by HW
  48. * Args: dram_info - main struct
  49. * freq - current sequence frequency
  50. * Notes:
  51. * Returns: MV_OK if success, MV_FAIL if fail.
  52. */
  53. int ddr3_read_leveling_hw(u32 freq, MV_DRAM_INFO *dram_info)
  54. {
  55. u32 reg;
  56. /* Debug message - Start Read leveling procedure */
  57. DEBUG_RL_S("DDR3 - Read Leveling - Starting HW RL procedure\n");
  58. /* Start Auto Read Leveling procedure */
  59. reg = 1 << REG_DRAM_TRAINING_RL_OFFS;
  60. /* Config the retest number */
  61. reg |= (COUNT_HW_RL << REG_DRAM_TRAINING_RETEST_OFFS);
  62. /* Enable CS in the automatic process */
  63. reg |= (dram_info->cs_ena << REG_DRAM_TRAINING_CS_OFFS);
  64. reg_write(REG_DRAM_TRAINING_ADDR, reg); /* 0x15B0 - Training Register */
  65. reg = reg_read(REG_DRAM_TRAINING_SHADOW_ADDR) |
  66. (1 << REG_DRAM_TRAINING_AUTO_OFFS);
  67. reg_write(REG_DRAM_TRAINING_SHADOW_ADDR, reg);
  68. /* Wait */
  69. do {
  70. reg = reg_read(REG_DRAM_TRAINING_SHADOW_ADDR) &
  71. (1 << REG_DRAM_TRAINING_AUTO_OFFS);
  72. } while (reg); /* Wait for '0' */
  73. /* Check if Successful */
  74. if (reg_read(REG_DRAM_TRAINING_SHADOW_ADDR) &
  75. (1 << REG_DRAM_TRAINING_ERROR_OFFS)) {
  76. u32 delay, phase, pup, cs;
  77. dram_info->rl_max_phase = 0;
  78. dram_info->rl_min_phase = 10;
  79. /* Read results to arrays */
  80. for (cs = 0; cs < MAX_CS; cs++) {
  81. if (dram_info->cs_ena & (1 << cs)) {
  82. for (pup = 0;
  83. pup < dram_info->num_of_total_pups;
  84. pup++) {
  85. if (pup == dram_info->num_of_std_pups
  86. && dram_info->ecc_ena)
  87. pup = ECC_PUP;
  88. reg =
  89. ddr3_read_pup_reg(PUP_RL_MODE, cs,
  90. pup);
  91. phase = (reg >> REG_PHY_PHASE_OFFS) &
  92. PUP_PHASE_MASK;
  93. delay = reg & PUP_DELAY_MASK;
  94. dram_info->rl_val[cs][pup][P] = phase;
  95. if (phase > dram_info->rl_max_phase)
  96. dram_info->rl_max_phase = phase;
  97. if (phase < dram_info->rl_min_phase)
  98. dram_info->rl_min_phase = phase;
  99. dram_info->rl_val[cs][pup][D] = delay;
  100. dram_info->rl_val[cs][pup][S] =
  101. RL_FINAL_STATE;
  102. reg =
  103. ddr3_read_pup_reg(PUP_RL_MODE + 0x1,
  104. cs, pup);
  105. dram_info->rl_val[cs][pup][DQS] =
  106. (reg & 0x3F);
  107. }
  108. #ifdef MV_DEBUG_RL
  109. /* Print results */
  110. DEBUG_RL_C("DDR3 - Read Leveling - Results for CS - ",
  111. (u32) cs, 1);
  112. for (pup = 0;
  113. pup < (dram_info->num_of_total_pups);
  114. pup++) {
  115. if (pup == dram_info->num_of_std_pups
  116. && dram_info->ecc_ena)
  117. pup = ECC_PUP;
  118. DEBUG_RL_S("DDR3 - Read Leveling - PUP: ");
  119. DEBUG_RL_D((u32) pup, 1);
  120. DEBUG_RL_S(", Phase: ");
  121. DEBUG_RL_D((u32) dram_info->
  122. rl_val[cs][pup][P], 1);
  123. DEBUG_RL_S(", Delay: ");
  124. DEBUG_RL_D((u32) dram_info->
  125. rl_val[cs][pup][D], 2);
  126. DEBUG_RL_S("\n");
  127. }
  128. #endif
  129. }
  130. }
  131. dram_info->rd_rdy_dly =
  132. reg_read(REG_READ_DATA_READY_DELAYS_ADDR) &
  133. REG_READ_DATA_SAMPLE_DELAYS_MASK;
  134. dram_info->rd_smpl_dly =
  135. reg_read(REG_READ_DATA_SAMPLE_DELAYS_ADDR) &
  136. REG_READ_DATA_READY_DELAYS_MASK;
  137. #ifdef MV_DEBUG_RL
  138. DEBUG_RL_C("DDR3 - Read Leveling - Read Sample Delay: ",
  139. dram_info->rd_smpl_dly, 2);
  140. DEBUG_RL_C("DDR3 - Read Leveling - Read Ready Delay: ",
  141. dram_info->rd_rdy_dly, 2);
  142. DEBUG_RL_S("DDR3 - Read Leveling - HW RL Ended Successfully\n");
  143. #endif
  144. return MV_OK;
  145. } else {
  146. DEBUG_RL_S("DDR3 - Read Leveling - HW RL Error\n");
  147. return MV_FAIL;
  148. }
  149. }
  150. /*
  151. * Name: ddr3_read_leveling_sw
  152. * Desc: Execute the Read leveling phase by SW
  153. * Args: dram_info - main struct
  154. * freq - current sequence frequency
  155. * Notes:
  156. * Returns: MV_OK if success, MV_FAIL if fail.
  157. */
  158. int ddr3_read_leveling_sw(u32 freq, int ratio_2to1, MV_DRAM_INFO *dram_info)
  159. {
  160. u32 reg, cs, ecc, pup_num, phase, delay, pup;
  161. int status;
  162. /* Debug message - Start Read leveling procedure */
  163. DEBUG_RL_S("DDR3 - Read Leveling - Starting SW RL procedure\n");
  164. /* Enable SW Read Leveling */
  165. reg = reg_read(REG_DRAM_TRAINING_2_ADDR) |
  166. (1 << REG_DRAM_TRAINING_2_SW_OVRD_OFFS);
  167. reg &= ~(1 << REG_DRAM_TRAINING_2_RL_MODE_OFFS);
  168. /* [0]=1 - Enable SW override */
  169. /* 0x15B8 - Training SW 2 Register */
  170. reg_write(REG_DRAM_TRAINING_2_ADDR, reg);
  171. #ifdef RL_MODE
  172. reg = (dram_info->cs_ena << REG_DRAM_TRAINING_CS_OFFS) |
  173. (1 << REG_DRAM_TRAINING_AUTO_OFFS);
  174. reg_write(REG_DRAM_TRAINING_ADDR, reg); /* 0x15B0 - Training Register */
  175. #endif
  176. /* Loop for each CS */
  177. for (cs = 0; cs < dram_info->num_cs; cs++) {
  178. DEBUG_RL_C("DDR3 - Read Leveling - CS - ", (u32) cs, 1);
  179. for (ecc = 0; ecc <= (dram_info->ecc_ena); ecc++) {
  180. /* ECC Support - Switch ECC Mux on ecc=1 */
  181. reg = reg_read(REG_DRAM_TRAINING_2_ADDR) &
  182. ~(1 << REG_DRAM_TRAINING_2_ECC_MUX_OFFS);
  183. reg |= (dram_info->ecc_ena *
  184. ecc << REG_DRAM_TRAINING_2_ECC_MUX_OFFS);
  185. reg_write(REG_DRAM_TRAINING_2_ADDR, reg);
  186. if (ecc)
  187. DEBUG_RL_S("DDR3 - Read Leveling - ECC Mux Enabled\n");
  188. else
  189. DEBUG_RL_S("DDR3 - Read Leveling - ECC Mux Disabled\n");
  190. /* Set current sample delays */
  191. reg = reg_read(REG_READ_DATA_SAMPLE_DELAYS_ADDR);
  192. reg &= ~(REG_READ_DATA_SAMPLE_DELAYS_MASK <<
  193. (REG_READ_DATA_SAMPLE_DELAYS_OFFS * cs));
  194. reg |= (dram_info->cl <<
  195. (REG_READ_DATA_SAMPLE_DELAYS_OFFS * cs));
  196. reg_write(REG_READ_DATA_SAMPLE_DELAYS_ADDR, reg);
  197. /* Set current Ready delay */
  198. reg = reg_read(REG_READ_DATA_READY_DELAYS_ADDR);
  199. reg &= ~(REG_READ_DATA_READY_DELAYS_MASK <<
  200. (REG_READ_DATA_READY_DELAYS_OFFS * cs));
  201. if (!ratio_2to1) {
  202. /* 1:1 mode */
  203. reg |= ((dram_info->cl + 1) <<
  204. (REG_READ_DATA_READY_DELAYS_OFFS * cs));
  205. } else {
  206. /* 2:1 mode */
  207. reg |= ((dram_info->cl + 2) <<
  208. (REG_READ_DATA_READY_DELAYS_OFFS * cs));
  209. }
  210. reg_write(REG_READ_DATA_READY_DELAYS_ADDR, reg);
  211. /* Read leveling Single CS[cs] */
  212. #ifdef RL_MODE
  213. status =
  214. ddr3_read_leveling_single_cs_rl_mode(cs, freq,
  215. ratio_2to1,
  216. ecc,
  217. dram_info);
  218. if (MV_OK != status)
  219. return status;
  220. #else
  221. status =
  222. ddr3_read_leveling_single_cs_window_mode(cs, freq,
  223. ratio_2to1,
  224. ecc,
  225. dram_info)
  226. if (MV_OK != status)
  227. return status;
  228. #endif
  229. }
  230. /* Print results */
  231. DEBUG_RL_C("DDR3 - Read Leveling - Results for CS - ", (u32) cs,
  232. 1);
  233. for (pup = 0;
  234. pup < (dram_info->num_of_std_pups + dram_info->ecc_ena);
  235. pup++) {
  236. DEBUG_RL_S("DDR3 - Read Leveling - PUP: ");
  237. DEBUG_RL_D((u32) pup, 1);
  238. DEBUG_RL_S(", Phase: ");
  239. DEBUG_RL_D((u32) dram_info->rl_val[cs][pup][P], 1);
  240. DEBUG_RL_S(", Delay: ");
  241. DEBUG_RL_D((u32) dram_info->rl_val[cs][pup][D], 2);
  242. DEBUG_RL_S("\n");
  243. }
  244. DEBUG_RL_C("DDR3 - Read Leveling - Read Sample Delay: ",
  245. dram_info->rd_smpl_dly, 2);
  246. DEBUG_RL_C("DDR3 - Read Leveling - Read Ready Delay: ",
  247. dram_info->rd_rdy_dly, 2);
  248. /* Configure PHY with average of 3 locked leveling settings */
  249. for (pup = 0;
  250. pup < (dram_info->num_of_std_pups + dram_info->ecc_ena);
  251. pup++) {
  252. /* ECC support - bit 8 */
  253. pup_num = (pup == dram_info->num_of_std_pups) ? ECC_BIT : pup;
  254. /* For now, set last cnt result */
  255. phase = dram_info->rl_val[cs][pup][P];
  256. delay = dram_info->rl_val[cs][pup][D];
  257. ddr3_write_pup_reg(PUP_RL_MODE, cs, pup_num, phase,
  258. delay);
  259. }
  260. }
  261. /* Reset PHY read FIFO */
  262. reg = reg_read(REG_DRAM_TRAINING_2_ADDR) |
  263. (1 << REG_DRAM_TRAINING_2_FIFO_RST_OFFS);
  264. /* 0x15B8 - Training SW 2 Register */
  265. reg_write(REG_DRAM_TRAINING_2_ADDR, reg);
  266. do {
  267. reg = (reg_read(REG_DRAM_TRAINING_2_ADDR)) &
  268. (1 << REG_DRAM_TRAINING_2_FIFO_RST_OFFS);
  269. } while (reg); /* Wait for '0' */
  270. /* ECC Support - Switch ECC Mux off ecc=0 */
  271. reg = reg_read(REG_DRAM_TRAINING_2_ADDR) &
  272. ~(1 << REG_DRAM_TRAINING_2_ECC_MUX_OFFS);
  273. reg_write(REG_DRAM_TRAINING_2_ADDR, reg);
  274. #ifdef RL_MODE
  275. reg_write(REG_DRAM_TRAINING_ADDR, 0); /* 0x15B0 - Training Register */
  276. #endif
  277. /* Disable SW Read Leveling */
  278. reg = reg_read(REG_DRAM_TRAINING_2_ADDR) &
  279. ~(1 << REG_DRAM_TRAINING_2_SW_OVRD_OFFS);
  280. /* [0] = 0 - Disable SW override */
  281. reg = (reg | (0x1 << REG_DRAM_TRAINING_2_RL_MODE_OFFS));
  282. /* [3] = 1 - Disable RL MODE */
  283. /* 0x15B8 - Training SW 2 Register */
  284. reg_write(REG_DRAM_TRAINING_2_ADDR, reg);
  285. DEBUG_RL_S("DDR3 - Read Leveling - Finished RL procedure for all CS\n");
  286. return MV_OK;
  287. }
  288. #ifdef RL_MODE
  289. /*
  290. * overrun() extracted from ddr3_read_leveling_single_cs_rl_mode().
  291. * This just got too much indented making it hard to read / edit.
  292. */
  293. static void overrun(u32 cs, MV_DRAM_INFO *info, u32 pup, u32 locked_pups,
  294. u32 *locked_sum, u32 ecc, int *first_octet_locked,
  295. int *counter_in_progress, int final_delay, u32 delay,
  296. u32 phase)
  297. {
  298. /* If no OverRun */
  299. if (((~locked_pups >> pup) & 0x1) && (final_delay == 0)) {
  300. int idx;
  301. idx = pup + ecc * ECC_BIT;
  302. /* PUP passed, start examining */
  303. if (info->rl_val[cs][idx][S] == RL_UNLOCK_STATE) {
  304. /* Must be RL_UNLOCK_STATE */
  305. /* Match expected value ? - Update State Machine */
  306. if (info->rl_val[cs][idx][C] < RL_RETRY_COUNT) {
  307. DEBUG_RL_FULL_C("DDR3 - Read Leveling - We have no overrun and a match on pup: ",
  308. (u32)pup, 1);
  309. info->rl_val[cs][idx][C]++;
  310. /* If pup got to last state - lock the delays */
  311. if (info->rl_val[cs][idx][C] == RL_RETRY_COUNT) {
  312. info->rl_val[cs][idx][C] = 0;
  313. info->rl_val[cs][idx][DS] = delay;
  314. info->rl_val[cs][idx][PS] = phase;
  315. /* Go to Final State */
  316. info->rl_val[cs][idx][S] = RL_FINAL_STATE;
  317. *locked_sum = *locked_sum + 1;
  318. DEBUG_RL_FULL_C("DDR3 - Read Leveling - We have locked pup: ",
  319. (u32)pup, 1);
  320. /*
  321. * If first lock - need to lock delays
  322. */
  323. if (*first_octet_locked == 0) {
  324. DEBUG_RL_FULL_C("DDR3 - Read Leveling - We got first lock on pup: ",
  325. (u32)pup, 1);
  326. *first_octet_locked = 1;
  327. }
  328. /*
  329. * If pup is in not in final state but
  330. * there was match - dont increment
  331. * counter
  332. */
  333. } else {
  334. *counter_in_progress = 1;
  335. }
  336. }
  337. }
  338. }
  339. }
  340. /*
  341. * Name: ddr3_read_leveling_single_cs_rl_mode
  342. * Desc: Execute Read leveling for single Chip select
  343. * Args: cs - current chip select
  344. * freq - current sequence frequency
  345. * ecc - ecc iteration indication
  346. * dram_info - main struct
  347. * Notes:
  348. * Returns: MV_OK if success, MV_FAIL if fail.
  349. */
  350. static int ddr3_read_leveling_single_cs_rl_mode(u32 cs, u32 freq,
  351. int ratio_2to1, u32 ecc,
  352. MV_DRAM_INFO *dram_info)
  353. {
  354. u32 reg, delay, phase, pup, rd_sample_delay, add, locked_pups,
  355. repeat_max_cnt, sdram_offset, locked_sum;
  356. u32 phase_min, ui_max_delay;
  357. int all_locked, first_octet_locked, counter_in_progress;
  358. int final_delay = 0;
  359. DEBUG_RL_FULL_C("DDR3 - Read Leveling - Single CS - ", (u32) cs, 1);
  360. /* Init values */
  361. phase = 0;
  362. delay = 0;
  363. rd_sample_delay = dram_info->cl;
  364. all_locked = 0;
  365. first_octet_locked = 0;
  366. repeat_max_cnt = 0;
  367. locked_sum = 0;
  368. for (pup = 0; pup < (dram_info->num_of_std_pups * (1 - ecc) + ecc);
  369. pup++)
  370. dram_info->rl_val[cs][pup + ecc * ECC_BIT][S] = 0;
  371. /* Main loop */
  372. while (!all_locked) {
  373. counter_in_progress = 0;
  374. DEBUG_RL_FULL_S("DDR3 - Read Leveling - RdSmplDly = ");
  375. DEBUG_RL_FULL_D(rd_sample_delay, 2);
  376. DEBUG_RL_FULL_S(", RdRdyDly = ");
  377. DEBUG_RL_FULL_D(dram_info->rd_rdy_dly, 2);
  378. DEBUG_RL_FULL_S(", Phase = ");
  379. DEBUG_RL_FULL_D(phase, 1);
  380. DEBUG_RL_FULL_S(", Delay = ");
  381. DEBUG_RL_FULL_D(delay, 2);
  382. DEBUG_RL_FULL_S("\n");
  383. /*
  384. * Broadcast to all PUPs current RL delays: DQS phase,
  385. * leveling delay
  386. */
  387. ddr3_write_pup_reg(PUP_RL_MODE, cs, PUP_BC, phase, delay);
  388. /* Reset PHY read FIFO */
  389. reg = reg_read(REG_DRAM_TRAINING_2_ADDR) |
  390. (1 << REG_DRAM_TRAINING_2_FIFO_RST_OFFS);
  391. /* 0x15B8 - Training SW 2 Register */
  392. reg_write(REG_DRAM_TRAINING_2_ADDR, reg);
  393. do {
  394. reg = (reg_read(REG_DRAM_TRAINING_2_ADDR)) &
  395. (1 << REG_DRAM_TRAINING_2_FIFO_RST_OFFS);
  396. } while (reg); /* Wait for '0' */
  397. /* Read pattern from SDRAM */
  398. sdram_offset = cs * (SDRAM_CS_SIZE + 1) + SDRAM_RL_OFFS;
  399. locked_pups = 0;
  400. if (MV_OK !=
  401. ddr3_sdram_compare(dram_info, 0xFF, &locked_pups,
  402. rl_pattern, LEN_STD_PATTERN,
  403. sdram_offset, 0, 0, NULL, 0))
  404. return MV_DDR3_TRAINING_ERR_RD_LVL_RL_PATTERN;
  405. /* Octet evaluation */
  406. /* pup_num = Q or 1 for ECC */
  407. for (pup = 0; pup < (dram_info->num_of_std_pups * (1 - ecc) + ecc); pup++) {
  408. /* Check Overrun */
  409. if (!((reg_read(REG_DRAM_TRAINING_2_ADDR) >>
  410. (REG_DRAM_TRAINING_2_OVERRUN_OFFS + pup)) & 0x1)) {
  411. overrun(cs, dram_info, pup, locked_pups,
  412. &locked_sum, ecc, &first_octet_locked,
  413. &counter_in_progress, final_delay,
  414. delay, phase);
  415. } else {
  416. DEBUG_RL_FULL_C("DDR3 - Read Leveling - We got overrun on pup: ",
  417. (u32)pup, 1);
  418. }
  419. }
  420. if (locked_sum == (dram_info->num_of_std_pups *
  421. (1 - ecc) + ecc)) {
  422. all_locked = 1;
  423. DEBUG_RL_FULL_S("DDR3 - Read Leveling - Single Cs - All pups locked\n");
  424. }
  425. /*
  426. * This is a fix for unstable condition where pups are
  427. * toggling between match and no match
  428. */
  429. /*
  430. * If some of the pups is >1 <3, check if we did it too
  431. * many times
  432. */
  433. if (counter_in_progress == 1) {
  434. /* Notify at least one Counter is >=1 and < 3 */
  435. if (repeat_max_cnt < RL_RETRY_COUNT) {
  436. repeat_max_cnt++;
  437. counter_in_progress = 1;
  438. DEBUG_RL_FULL_S("DDR3 - Read Leveling - Counter is >=1 and <3\n");
  439. DEBUG_RL_FULL_S("DDR3 - Read Leveling - So we will not increment the delay to see if locked again\n");
  440. } else {
  441. DEBUG_RL_FULL_S("DDR3 - Read Leveling - repeat_max_cnt reached max so now we will increment the delay\n");
  442. counter_in_progress = 0;
  443. }
  444. }
  445. /*
  446. * Check some of the pups are in the middle of state machine
  447. * and don't increment the delays
  448. */
  449. if (!counter_in_progress && !all_locked) {
  450. int idx;
  451. idx = pup + ecc * ECC_BIT;
  452. repeat_max_cnt = 0;
  453. /* if 1:1 mode */
  454. if ((!ratio_2to1) && ((phase == 0) || (phase == 4)))
  455. ui_max_delay = MAX_DELAY_INV;
  456. else
  457. ui_max_delay = MAX_DELAY;
  458. /* Increment Delay */
  459. if (delay < ui_max_delay) {
  460. delay++;
  461. /*
  462. * Mark the last delay/pahse place for
  463. * window final place
  464. */
  465. if (delay == ui_max_delay) {
  466. if ((!ratio_2to1 && phase ==
  467. MAX_PHASE_RL_L_1TO1)
  468. || (ratio_2to1 && phase ==
  469. MAX_PHASE_RL_L_2TO1))
  470. final_delay = 1;
  471. }
  472. } else {
  473. /* Phase+CL Incrementation */
  474. delay = 0;
  475. if (!ratio_2to1) {
  476. /* 1:1 mode */
  477. if (first_octet_locked) {
  478. /* some Pup was Locked */
  479. if (phase < MAX_PHASE_RL_L_1TO1) {
  480. if (phase == 1) {
  481. phase = 4;
  482. } else {
  483. phase++;
  484. delay = MIN_DELAY_PHASE_1_LIMIT;
  485. }
  486. } else {
  487. DEBUG_RL_FULL_S("DDR3 - Read Leveling - ERROR - NOT all PUPs Locked\n");
  488. DEBUG_RL_S("1)DDR3 - Read Leveling - ERROR - NOT all PUPs Locked n");
  489. return MV_DDR3_TRAINING_ERR_RD_LVL_RL_PUP_UNLOCK;
  490. }
  491. } else {
  492. /* NO Pup was Locked */
  493. if (phase < MAX_PHASE_RL_UL_1TO1) {
  494. phase++;
  495. delay =
  496. MIN_DELAY_PHASE_1_LIMIT;
  497. } else {
  498. phase = 0;
  499. }
  500. }
  501. } else {
  502. /* 2:1 mode */
  503. if (first_octet_locked) {
  504. /* some Pup was Locked */
  505. if (phase < MAX_PHASE_RL_L_2TO1) {
  506. phase++;
  507. } else {
  508. DEBUG_RL_FULL_S("DDR3 - Read Leveling - ERROR - NOT all PUPs Locked\n");
  509. DEBUG_RL_S("2)DDR3 - Read Leveling - ERROR - NOT all PUPs Locked\n");
  510. for (pup = 0; pup < (dram_info->num_of_std_pups * (1 - ecc) + ecc); pup++) {
  511. /* pup_num = Q or 1 for ECC */
  512. if (dram_info->rl_val[cs][idx][S]
  513. == 0) {
  514. DEBUG_RL_C("Failed byte is = ",
  515. pup, 1);
  516. }
  517. }
  518. return MV_DDR3_TRAINING_ERR_RD_LVL_RL_PUP_UNLOCK;
  519. }
  520. } else {
  521. /* No Pup was Locked */
  522. if (phase < MAX_PHASE_RL_UL_2TO1)
  523. phase++;
  524. else
  525. phase = 0;
  526. }
  527. }
  528. /*
  529. * If we finished a full Phases cycle (so now
  530. * phase = 0, need to increment rd_sample_dly
  531. */
  532. if (phase == 0 && first_octet_locked == 0) {
  533. rd_sample_delay++;
  534. if (rd_sample_delay == 0x10) {
  535. DEBUG_RL_FULL_S("DDR3 - Read Leveling - ERROR - NOT all PUPs Locked\n");
  536. DEBUG_RL_S("3)DDR3 - Read Leveling - ERROR - NOT all PUPs Locked\n");
  537. for (pup = 0; pup < (dram_info->num_of_std_pups * (1 - ecc) + ecc); pup++) {
  538. /* pup_num = Q or 1 for ECC */
  539. if (dram_info->
  540. rl_val[cs][idx][S] == 0) {
  541. DEBUG_RL_C("Failed byte is = ",
  542. pup, 1);
  543. }
  544. }
  545. return MV_DDR3_TRAINING_ERR_RD_LVL_PUP_UNLOCK;
  546. }
  547. /* Set current rd_sample_delay */
  548. reg = reg_read(REG_READ_DATA_SAMPLE_DELAYS_ADDR);
  549. reg &= ~(REG_READ_DATA_SAMPLE_DELAYS_MASK
  550. << (REG_READ_DATA_SAMPLE_DELAYS_OFFS
  551. * cs));
  552. reg |= (rd_sample_delay <<
  553. (REG_READ_DATA_SAMPLE_DELAYS_OFFS *
  554. cs));
  555. reg_write(REG_READ_DATA_SAMPLE_DELAYS_ADDR,
  556. reg);
  557. }
  558. /*
  559. * Set current rdReadyDelay according to the
  560. * hash table (Need to do this in every phase
  561. * change)
  562. */
  563. if (!ratio_2to1) {
  564. /* 1:1 mode */
  565. add = reg_read(REG_TRAINING_DEBUG_2_ADDR);
  566. switch (phase) {
  567. case 0:
  568. add = (add >>
  569. REG_TRAINING_DEBUG_2_OFFS);
  570. break;
  571. case 1:
  572. add = (add >>
  573. (REG_TRAINING_DEBUG_2_OFFS
  574. + 3));
  575. break;
  576. case 4:
  577. add = (add >>
  578. (REG_TRAINING_DEBUG_2_OFFS
  579. + 6));
  580. break;
  581. case 5:
  582. add = (add >>
  583. (REG_TRAINING_DEBUG_2_OFFS
  584. + 9));
  585. break;
  586. }
  587. add &= REG_TRAINING_DEBUG_2_MASK;
  588. } else {
  589. /* 2:1 mode */
  590. add = reg_read(REG_TRAINING_DEBUG_3_ADDR);
  591. add = (add >>
  592. (phase *
  593. REG_TRAINING_DEBUG_3_OFFS));
  594. add &= REG_TRAINING_DEBUG_3_MASK;
  595. }
  596. reg = reg_read(REG_READ_DATA_READY_DELAYS_ADDR);
  597. reg &= ~(REG_READ_DATA_READY_DELAYS_MASK <<
  598. (REG_READ_DATA_READY_DELAYS_OFFS * cs));
  599. reg |= ((rd_sample_delay + add) <<
  600. (REG_READ_DATA_READY_DELAYS_OFFS * cs));
  601. reg_write(REG_READ_DATA_READY_DELAYS_ADDR, reg);
  602. dram_info->rd_smpl_dly = rd_sample_delay;
  603. dram_info->rd_rdy_dly = rd_sample_delay + add;
  604. }
  605. /* Reset counters for pups with states<RD_STATE_COUNT */
  606. for (pup = 0; pup <
  607. (dram_info->num_of_std_pups * (1 - ecc) + ecc);
  608. pup++) {
  609. if (dram_info->rl_val[cs][idx][C] < RL_RETRY_COUNT)
  610. dram_info->rl_val[cs][idx][C] = 0;
  611. }
  612. }
  613. }
  614. phase_min = 10;
  615. for (pup = 0; pup < (dram_info->num_of_std_pups); pup++) {
  616. if (dram_info->rl_val[cs][pup][PS] < phase_min)
  617. phase_min = dram_info->rl_val[cs][pup][PS];
  618. }
  619. /*
  620. * Set current rdReadyDelay according to the hash table (Need to
  621. * do this in every phase change)
  622. */
  623. if (!ratio_2to1) {
  624. /* 1:1 mode */
  625. add = reg_read(REG_TRAINING_DEBUG_2_ADDR);
  626. switch (phase_min) {
  627. case 0:
  628. add = (add >> REG_TRAINING_DEBUG_2_OFFS);
  629. break;
  630. case 1:
  631. add = (add >> (REG_TRAINING_DEBUG_2_OFFS + 3));
  632. break;
  633. case 4:
  634. add = (add >> (REG_TRAINING_DEBUG_2_OFFS + 6));
  635. break;
  636. case 5:
  637. add = (add >> (REG_TRAINING_DEBUG_2_OFFS + 9));
  638. break;
  639. }
  640. add &= REG_TRAINING_DEBUG_2_MASK;
  641. } else {
  642. /* 2:1 mode */
  643. add = reg_read(REG_TRAINING_DEBUG_3_ADDR);
  644. add = (add >> (phase_min * REG_TRAINING_DEBUG_3_OFFS));
  645. add &= REG_TRAINING_DEBUG_3_MASK;
  646. }
  647. reg = reg_read(REG_READ_DATA_READY_DELAYS_ADDR);
  648. reg &= ~(REG_READ_DATA_READY_DELAYS_MASK <<
  649. (REG_READ_DATA_READY_DELAYS_OFFS * cs));
  650. reg |= ((rd_sample_delay + add) << (REG_READ_DATA_READY_DELAYS_OFFS * cs));
  651. reg_write(REG_READ_DATA_READY_DELAYS_ADDR, reg);
  652. dram_info->rd_rdy_dly = rd_sample_delay + add;
  653. for (cs = 0; cs < dram_info->num_cs; cs++) {
  654. for (pup = 0; pup < dram_info->num_of_total_pups; pup++) {
  655. reg = ddr3_read_pup_reg(PUP_RL_MODE + 0x1, cs, pup);
  656. dram_info->rl_val[cs][pup][DQS] = (reg & 0x3F);
  657. }
  658. }
  659. return MV_OK;
  660. }
  661. #else
  662. /*
  663. * Name: ddr3_read_leveling_single_cs_window_mode
  664. * Desc: Execute Read leveling for single Chip select
  665. * Args: cs - current chip select
  666. * freq - current sequence frequency
  667. * ecc - ecc iteration indication
  668. * dram_info - main struct
  669. * Notes:
  670. * Returns: MV_OK if success, MV_FAIL if fail.
  671. */
  672. static int ddr3_read_leveling_single_cs_window_mode(u32 cs, u32 freq,
  673. int ratio_2to1, u32 ecc,
  674. MV_DRAM_INFO *dram_info)
  675. {
  676. u32 reg, delay, phase, sum, pup, rd_sample_delay, add, locked_pups,
  677. repeat_max_cnt, sdram_offset, final_sum, locked_sum;
  678. u32 delay_s, delay_e, tmp, phase_min, ui_max_delay;
  679. int all_locked, first_octet_locked, counter_in_progress;
  680. int final_delay = 0;
  681. DEBUG_RL_FULL_C("DDR3 - Read Leveling - Single CS - ", (u32) cs, 1);
  682. /* Init values */
  683. phase = 0;
  684. delay = 0;
  685. rd_sample_delay = dram_info->cl;
  686. all_locked = 0;
  687. first_octet_locked = 0;
  688. repeat_max_cnt = 0;
  689. sum = 0;
  690. final_sum = 0;
  691. locked_sum = 0;
  692. for (pup = 0; pup < (dram_info->num_of_std_pups * (1 - ecc) + ecc);
  693. pup++)
  694. dram_info->rl_val[cs][pup + ecc * ECC_BIT][S] = 0;
  695. /* Main loop */
  696. while (!all_locked) {
  697. counter_in_progress = 0;
  698. DEBUG_RL_FULL_S("DDR3 - Read Leveling - RdSmplDly = ");
  699. DEBUG_RL_FULL_D(rd_sample_delay, 2);
  700. DEBUG_RL_FULL_S(", RdRdyDly = ");
  701. DEBUG_RL_FULL_D(dram_info->rd_rdy_dly, 2);
  702. DEBUG_RL_FULL_S(", Phase = ");
  703. DEBUG_RL_FULL_D(phase, 1);
  704. DEBUG_RL_FULL_S(", Delay = ");
  705. DEBUG_RL_FULL_D(delay, 2);
  706. DEBUG_RL_FULL_S("\n");
  707. /*
  708. * Broadcast to all PUPs current RL delays: DQS phase,leveling
  709. * delay
  710. */
  711. ddr3_write_pup_reg(PUP_RL_MODE, cs, PUP_BC, phase, delay);
  712. /* Reset PHY read FIFO */
  713. reg = reg_read(REG_DRAM_TRAINING_2_ADDR) |
  714. (1 << REG_DRAM_TRAINING_2_FIFO_RST_OFFS);
  715. /* 0x15B8 - Training SW 2 Register */
  716. reg_write(REG_DRAM_TRAINING_2_ADDR, reg);
  717. do {
  718. reg = (reg_read(REG_DRAM_TRAINING_2_ADDR)) &
  719. (1 << REG_DRAM_TRAINING_2_FIFO_RST_OFFS);
  720. } while (reg); /* Wait for '0' */
  721. /* Read pattern from SDRAM */
  722. sdram_offset = cs * (SDRAM_CS_SIZE + 1) + SDRAM_RL_OFFS;
  723. locked_pups = 0;
  724. if (MV_OK !=
  725. ddr3_sdram_compare(dram_info, 0xFF, &locked_pups,
  726. rl_pattern, LEN_STD_PATTERN,
  727. sdram_offset, 0, 0, NULL, 0))
  728. return MV_DDR3_TRAINING_ERR_RD_LVL_WIN_PATTERN;
  729. /* Octet evaluation */
  730. for (pup = 0; pup < (dram_info->num_of_std_pups *
  731. (1 - ecc) + ecc); pup++) {
  732. /* pup_num = Q or 1 for ECC */
  733. int idx;
  734. idx = pup + ecc * ECC_BIT;
  735. /* Check Overrun */
  736. if (!((reg_read(REG_DRAM_TRAINING_2_ADDR) >>
  737. (REG_DRAM_TRAINING_2_OVERRUN_OFFS +
  738. pup)) & 0x1)) {
  739. /* If no OverRun */
  740. /* Inside the window */
  741. if (dram_info->rl_val[cs][idx][S] == RL_WINDOW_STATE) {
  742. /*
  743. * Match expected value ? - Update
  744. * State Machine
  745. */
  746. if (((~locked_pups >> pup) & 0x1)
  747. && (final_delay == 0)) {
  748. /* Match - Still inside the Window */
  749. DEBUG_RL_FULL_C("DDR3 - Read Leveling - We got another match inside the window for pup: ",
  750. (u32)pup, 1);
  751. } else {
  752. /* We got fail -> this is the end of the window */
  753. dram_info->rl_val[cs][idx][DE] = delay;
  754. dram_info->rl_val[cs][idx][PE] = phase;
  755. /* Go to Final State */
  756. dram_info->rl_val[cs][idx][S]++;
  757. final_sum++;
  758. DEBUG_RL_FULL_C("DDR3 - Read Leveling - We finished the window for pup: ",
  759. (u32)pup, 1);
  760. }
  761. /* Before the start of the window */
  762. } else if (dram_info->rl_val[cs][idx][S] ==
  763. RL_UNLOCK_STATE) {
  764. /* Must be RL_UNLOCK_STATE */
  765. /*
  766. * Match expected value ? - Update
  767. * State Machine
  768. */
  769. if (dram_info->rl_val[cs][idx][C] <
  770. RL_RETRY_COUNT) {
  771. if (((~locked_pups >> pup) & 0x1)) {
  772. /* Match */
  773. DEBUG_RL_FULL_C("DDR3 - Read Leveling - We have no overrun and a match on pup: ",
  774. (u32)pup, 1);
  775. dram_info->rl_val[cs][idx][C]++;
  776. /* If pup got to last state - lock the delays */
  777. if (dram_info->rl_val[cs][idx][C] ==
  778. RL_RETRY_COUNT) {
  779. dram_info->rl_val[cs][idx][C] = 0;
  780. dram_info->rl_val[cs][idx][DS] =
  781. delay;
  782. dram_info->rl_val[cs][idx][PS] =
  783. phase;
  784. dram_info->rl_val[cs][idx][S]++; /* Go to Window State */
  785. locked_sum++;
  786. /* Will count the pups that got locked */
  787. /* IF First lock - need to lock delays */
  788. if (first_octet_locked == 0) {
  789. DEBUG_RL_FULL_C("DDR3 - Read Leveling - We got first lock on pup: ",
  790. (u32)pup, 1);
  791. first_octet_locked
  792. =
  793. 1;
  794. }
  795. }
  796. /* if pup is in not in final state but there was match - dont increment counter */
  797. else {
  798. counter_in_progress
  799. = 1;
  800. }
  801. }
  802. }
  803. }
  804. } else {
  805. DEBUG_RL_FULL_C("DDR3 - Read Leveling - We got overrun on pup: ",
  806. (u32)pup, 1);
  807. counter_in_progress = 1;
  808. }
  809. }
  810. if (final_sum == (dram_info->num_of_std_pups * (1 - ecc) + ecc)) {
  811. all_locked = 1;
  812. DEBUG_RL_FULL_S("DDR3 - Read Leveling - Single Cs - All pups locked\n");
  813. }
  814. /*
  815. * This is a fix for unstable condition where pups are
  816. * toggling between match and no match
  817. */
  818. /*
  819. * If some of the pups is >1 <3, check if we did it too many
  820. * times
  821. */
  822. if (counter_in_progress == 1) {
  823. if (repeat_max_cnt < RL_RETRY_COUNT) {
  824. /* Notify at least one Counter is >=1 and < 3 */
  825. repeat_max_cnt++;
  826. counter_in_progress = 1;
  827. DEBUG_RL_FULL_S("DDR3 - Read Leveling - Counter is >=1 and <3\n");
  828. DEBUG_RL_FULL_S("DDR3 - Read Leveling - So we will not increment the delay to see if locked again\n");
  829. } else {
  830. DEBUG_RL_FULL_S("DDR3 - Read Leveling - repeat_max_cnt reached max so now we will increment the delay\n");
  831. counter_in_progress = 0;
  832. }
  833. }
  834. /*
  835. * Check some of the pups are in the middle of state machine
  836. * and don't increment the delays
  837. */
  838. if (!counter_in_progress && !all_locked) {
  839. repeat_max_cnt = 0;
  840. if (!ratio_2to1)
  841. ui_max_delay = MAX_DELAY_INV;
  842. else
  843. ui_max_delay = MAX_DELAY;
  844. /* Increment Delay */
  845. if (delay < ui_max_delay) {
  846. /* Delay Incrementation */
  847. delay++;
  848. if (delay == ui_max_delay) {
  849. /*
  850. * Mark the last delay/pahse place
  851. * for window final place
  852. */
  853. if ((!ratio_2to1
  854. && phase == MAX_PHASE_RL_L_1TO1)
  855. || (ratio_2to1
  856. && phase ==
  857. MAX_PHASE_RL_L_2TO1))
  858. final_delay = 1;
  859. }
  860. } else {
  861. /* Phase+CL Incrementation */
  862. delay = 0;
  863. if (!ratio_2to1) {
  864. /* 1:1 mode */
  865. if (first_octet_locked) {
  866. /* some pupet was Locked */
  867. if (phase < MAX_PHASE_RL_L_1TO1) {
  868. #ifdef RL_WINDOW_WA
  869. if (phase == 0)
  870. #else
  871. if (phase == 1)
  872. #endif
  873. phase = 4;
  874. else
  875. phase++;
  876. } else {
  877. DEBUG_RL_FULL_S("DDR3 - Read Leveling - ERROR - NOT all PUPs Locked\n");
  878. return MV_DDR3_TRAINING_ERR_RD_LVL_WIN_PUP_UNLOCK;
  879. }
  880. } else {
  881. /* No Pup was Locked */
  882. if (phase < MAX_PHASE_RL_UL_1TO1) {
  883. #ifdef RL_WINDOW_WA
  884. if (phase == 0)
  885. phase = 4;
  886. #else
  887. phase++;
  888. #endif
  889. } else
  890. phase = 0;
  891. }
  892. } else {
  893. /* 2:1 mode */
  894. if (first_octet_locked) {
  895. /* Some Pup was Locked */
  896. if (phase < MAX_PHASE_RL_L_2TO1) {
  897. phase++;
  898. } else {
  899. DEBUG_RL_FULL_S("DDR3 - Read Leveling - ERROR - NOT all PUPs Locked\n");
  900. return MV_DDR3_TRAINING_ERR_RD_LVL_WIN_PUP_UNLOCK;
  901. }
  902. } else {
  903. /* No Pup was Locked */
  904. if (phase < MAX_PHASE_RL_UL_2TO1)
  905. phase++;
  906. else
  907. phase = 0;
  908. }
  909. }
  910. /*
  911. * If we finished a full Phases cycle (so
  912. * now phase = 0, need to increment
  913. * rd_sample_dly
  914. */
  915. if (phase == 0 && first_octet_locked == 0) {
  916. rd_sample_delay++;
  917. /* Set current rd_sample_delay */
  918. reg = reg_read(REG_READ_DATA_SAMPLE_DELAYS_ADDR);
  919. reg &= ~(REG_READ_DATA_SAMPLE_DELAYS_MASK <<
  920. (REG_READ_DATA_SAMPLE_DELAYS_OFFS
  921. * cs));
  922. reg |= (rd_sample_delay <<
  923. (REG_READ_DATA_SAMPLE_DELAYS_OFFS *
  924. cs));
  925. reg_write(REG_READ_DATA_SAMPLE_DELAYS_ADDR,
  926. reg);
  927. }
  928. /*
  929. * Set current rdReadyDelay according to the
  930. * hash table (Need to do this in every phase
  931. * change)
  932. */
  933. if (!ratio_2to1) {
  934. /* 1:1 mode */
  935. add = reg_read(REG_TRAINING_DEBUG_2_ADDR);
  936. switch (phase) {
  937. case 0:
  938. add = add >>
  939. REG_TRAINING_DEBUG_2_OFFS;
  940. break;
  941. case 1:
  942. add = add >>
  943. (REG_TRAINING_DEBUG_2_OFFS
  944. + 3);
  945. break;
  946. case 4:
  947. add = add >>
  948. (REG_TRAINING_DEBUG_2_OFFS
  949. + 6);
  950. break;
  951. case 5:
  952. add = add >>
  953. (REG_TRAINING_DEBUG_2_OFFS
  954. + 9);
  955. break;
  956. }
  957. } else {
  958. /* 2:1 mode */
  959. add = reg_read(REG_TRAINING_DEBUG_3_ADDR);
  960. add = (add >> phase *
  961. REG_TRAINING_DEBUG_3_OFFS);
  962. }
  963. add &= REG_TRAINING_DEBUG_2_MASK;
  964. reg = reg_read(REG_READ_DATA_READY_DELAYS_ADDR);
  965. reg &= ~(REG_READ_DATA_READY_DELAYS_MASK <<
  966. (REG_READ_DATA_READY_DELAYS_OFFS * cs));
  967. reg |= ((rd_sample_delay + add) <<
  968. (REG_READ_DATA_READY_DELAYS_OFFS * cs));
  969. reg_write(REG_READ_DATA_READY_DELAYS_ADDR, reg);
  970. dram_info->rd_smpl_dly = rd_sample_delay;
  971. dram_info->rd_rdy_dly = rd_sample_delay + add;
  972. }
  973. /* Reset counters for pups with states<RD_STATE_COUNT */
  974. for (pup = 0;
  975. pup <
  976. (dram_info->num_of_std_pups * (1 - ecc) + ecc);
  977. pup++) {
  978. if (dram_info->rl_val[cs][idx][C] < RL_RETRY_COUNT)
  979. dram_info->rl_val[cs][idx][C] = 0;
  980. }
  981. }
  982. }
  983. phase_min = 10;
  984. for (pup = 0; pup < (dram_info->num_of_std_pups); pup++) {
  985. DEBUG_RL_S("DDR3 - Read Leveling - Window info - PUP: ");
  986. DEBUG_RL_D((u32) pup, 1);
  987. DEBUG_RL_S(", PS: ");
  988. DEBUG_RL_D((u32) dram_info->rl_val[cs][pup][PS], 1);
  989. DEBUG_RL_S(", DS: ");
  990. DEBUG_RL_D((u32) dram_info->rl_val[cs][pup][DS], 2);
  991. DEBUG_RL_S(", PE: ");
  992. DEBUG_RL_D((u32) dram_info->rl_val[cs][pup][PE], 1);
  993. DEBUG_RL_S(", DE: ");
  994. DEBUG_RL_D((u32) dram_info->rl_val[cs][pup][DE], 2);
  995. DEBUG_RL_S("\n");
  996. }
  997. /* Find center of the window procedure */
  998. for (pup = 0; pup < (dram_info->num_of_std_pups * (1 - ecc) + ecc);
  999. pup++) {
  1000. #ifdef RL_WINDOW_WA
  1001. if (!ratio_2to1) { /* 1:1 mode */
  1002. if (dram_info->rl_val[cs][idx][PS] == 4)
  1003. dram_info->rl_val[cs][idx][PS] = 1;
  1004. if (dram_info->rl_val[cs][idx][PE] == 4)
  1005. dram_info->rl_val[cs][idx][PE] = 1;
  1006. delay_s = dram_info->rl_val[cs][idx][PS] *
  1007. MAX_DELAY_INV + dram_info->rl_val[cs][idx][DS];
  1008. delay_e = dram_info->rl_val[cs][idx][PE] *
  1009. MAX_DELAY_INV + dram_info->rl_val[cs][idx][DE];
  1010. tmp = (delay_e - delay_s) / 2 + delay_s;
  1011. phase = tmp / MAX_DELAY_INV;
  1012. if (phase == 1) /* 1:1 mode */
  1013. phase = 4;
  1014. if (phase < phase_min) /* for the read ready delay */
  1015. phase_min = phase;
  1016. dram_info->rl_val[cs][idx][P] = phase;
  1017. dram_info->rl_val[cs][idx][D] = tmp % MAX_DELAY_INV;
  1018. } else {
  1019. delay_s = dram_info->rl_val[cs][idx][PS] *
  1020. MAX_DELAY + dram_info->rl_val[cs][idx][DS];
  1021. delay_e = dram_info->rl_val[cs][idx][PE] *
  1022. MAX_DELAY + dram_info->rl_val[cs][idx][DE];
  1023. tmp = (delay_e - delay_s) / 2 + delay_s;
  1024. phase = tmp / MAX_DELAY;
  1025. if (phase < phase_min) /* for the read ready delay */
  1026. phase_min = phase;
  1027. dram_info->rl_val[cs][idx][P] = phase;
  1028. dram_info->rl_val[cs][idx][D] = tmp % MAX_DELAY;
  1029. }
  1030. #else
  1031. if (!ratio_2to1) { /* 1:1 mode */
  1032. if (dram_info->rl_val[cs][idx][PS] > 1)
  1033. dram_info->rl_val[cs][idx][PS] -= 2;
  1034. if (dram_info->rl_val[cs][idx][PE] > 1)
  1035. dram_info->rl_val[cs][idx][PE] -= 2;
  1036. }
  1037. delay_s = dram_info->rl_val[cs][idx][PS] * MAX_DELAY +
  1038. dram_info->rl_val[cs][idx][DS];
  1039. delay_e = dram_info->rl_val[cs][idx][PE] * MAX_DELAY +
  1040. dram_info->rl_val[cs][idx][DE];
  1041. tmp = (delay_e - delay_s) / 2 + delay_s;
  1042. phase = tmp / MAX_DELAY;
  1043. if (!ratio_2to1 && phase > 1) /* 1:1 mode */
  1044. phase += 2;
  1045. if (phase < phase_min) /* for the read ready delay */
  1046. phase_min = phase;
  1047. dram_info->rl_val[cs][idx][P] = phase;
  1048. dram_info->rl_val[cs][idx][D] = tmp % MAX_DELAY;
  1049. #endif
  1050. }
  1051. /* Set current rdReadyDelay according to the hash table (Need to do this in every phase change) */
  1052. if (!ratio_2to1) { /* 1:1 mode */
  1053. add = reg_read(REG_TRAINING_DEBUG_2_ADDR);
  1054. switch (phase_min) {
  1055. case 0:
  1056. add = (add >> REG_TRAINING_DEBUG_2_OFFS);
  1057. break;
  1058. case 1:
  1059. add = (add >> (REG_TRAINING_DEBUG_2_OFFS + 3));
  1060. break;
  1061. case 4:
  1062. add = (add >> (REG_TRAINING_DEBUG_2_OFFS + 6));
  1063. break;
  1064. case 5:
  1065. add = (add >> (REG_TRAINING_DEBUG_2_OFFS + 9));
  1066. break;
  1067. }
  1068. } else { /* 2:1 mode */
  1069. add = reg_read(REG_TRAINING_DEBUG_3_ADDR);
  1070. add = (add >> phase_min * REG_TRAINING_DEBUG_3_OFFS);
  1071. }
  1072. add &= REG_TRAINING_DEBUG_2_MASK;
  1073. reg = reg_read(REG_READ_DATA_READY_DELAYS_ADDR);
  1074. reg &=
  1075. ~(REG_READ_DATA_READY_DELAYS_MASK <<
  1076. (REG_READ_DATA_READY_DELAYS_OFFS * cs));
  1077. reg |=
  1078. ((rd_sample_delay + add) << (REG_READ_DATA_READY_DELAYS_OFFS * cs));
  1079. reg_write(REG_READ_DATA_READY_DELAYS_ADDR, reg);
  1080. dram_info->rd_rdy_dly = rd_sample_delay + add;
  1081. for (cs = 0; cs < dram_info->num_cs; cs++) {
  1082. for (pup = 0; pup < dram_info->num_of_total_pups; pup++) {
  1083. reg = ddr3_read_pup_reg(PUP_RL_MODE + 0x1, cs, pup);
  1084. dram_info->rl_val[cs][pup][DQS] = (reg & 0x3F);
  1085. }
  1086. }
  1087. return MV_OK;
  1088. }
  1089. #endif