ddr3_read_leveling.c 34 KB

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