ddr3_pbs.c 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592
  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_PBS_FULL_C(s, d, l) \
  17. DEBUG_PBS_FULL_S(s); DEBUG_PBS_FULL_D(d, l); DEBUG_PBS_FULL_S("\n")
  18. #define DEBUG_PBS_C(s, d, l) \
  19. DEBUG_PBS_S(s); DEBUG_PBS_D(d, l); DEBUG_PBS_S("\n")
  20. #ifdef MV_DEBUG_PBS
  21. #define DEBUG_PBS_S(s) puts(s)
  22. #define DEBUG_PBS_D(d, l) printf("%x", d)
  23. #else
  24. #define DEBUG_PBS_S(s)
  25. #define DEBUG_PBS_D(d, l)
  26. #endif
  27. #ifdef MV_DEBUG_FULL_PBS
  28. #define DEBUG_PBS_FULL_S(s) puts(s)
  29. #define DEBUG_PBS_FULL_D(d, l) printf("%x", d)
  30. #else
  31. #define DEBUG_PBS_FULL_S(s)
  32. #define DEBUG_PBS_FULL_D(d, l)
  33. #endif
  34. #if defined(MV88F78X60) || defined(MV88F672X)
  35. /* Temp array for skew data storage */
  36. static u32 skew_array[(MAX_PUP_NUM) * DQ_NUM] = { 0 };
  37. /* PBS locked dq (per pup) */
  38. extern u32 pbs_locked_dq[MAX_PUP_NUM][DQ_NUM];
  39. extern u32 pbs_locked_dm[MAX_PUP_NUM];
  40. extern u32 pbs_locked_value[MAX_PUP_NUM][DQ_NUM];
  41. #if defined(MV88F672X)
  42. extern u32 pbs_pattern[2][LEN_16BIT_PBS_PATTERN];
  43. extern u32 pbs_pattern_32b[2][LEN_PBS_PATTERN];
  44. #else
  45. extern u32 pbs_pattern_32b[2][LEN_PBS_PATTERN];
  46. extern u32 pbs_pattern_64b[2][LEN_PBS_PATTERN];
  47. #endif
  48. extern u32 pbs_dq_mapping[PUP_NUM_64BIT + 1][DQ_NUM];
  49. static int ddr3_tx_shift_dqs_adll_step_before_fail(MV_DRAM_INFO *dram_info,
  50. u32 cur_pup, u32 pbs_pattern_idx, u32 ecc);
  51. static int ddr3_rx_shift_dqs_to_first_fail(MV_DRAM_INFO *dram_info, u32 cur_pup,
  52. u32 pbs_pattern_idx, u32 ecc);
  53. static int ddr3_pbs_per_bit(MV_DRAM_INFO *dram_info, int *start_over, int is_tx,
  54. u32 *pcur_pup, u32 pbs_pattern_idx, u32 ecc);
  55. static int ddr3_set_pbs_results(MV_DRAM_INFO *dram_info, int is_tx);
  56. static void ddr3_pbs_write_pup_dqs_reg(u32 cs, u32 pup, u32 dqs_delay);
  57. /*
  58. * Name: ddr3_pbs_tx
  59. * Desc: Execute the PBS TX phase.
  60. * Args: dram_info ddr3 training information struct
  61. * Notes:
  62. * Returns: MV_OK if success, other error code if fail.
  63. */
  64. int ddr3_pbs_tx(MV_DRAM_INFO *dram_info)
  65. {
  66. /* Array of Deskew results */
  67. /*
  68. * Array to hold the total sum of skew from all iterations
  69. * (for average purpose)
  70. */
  71. u32 skew_sum_array[MAX_PUP_NUM][DQ_NUM] = { {0} };
  72. /*
  73. * Array to hold the total average skew from both patterns
  74. * (for average purpose)
  75. */
  76. u32 pattern_skew_array[MAX_PUP_NUM][DQ_NUM] = { {0} };
  77. u32 pbs_rep_time = 0; /* counts number of loop in case of fail */
  78. /* bit array for unlock pups - used to repeat on the RX operation */
  79. u32 cur_pup;
  80. u32 max_pup;
  81. u32 pbs_retry;
  82. u32 pup, dq, pups, cur_max_pup, valid_pup, reg;
  83. u32 pattern_idx;
  84. u32 ecc;
  85. /* indicates whether we need to start the loop again */
  86. int start_over;
  87. DEBUG_PBS_S("DDR3 - PBS TX - Starting PBS TX procedure\n");
  88. pups = dram_info->num_of_total_pups;
  89. max_pup = dram_info->num_of_total_pups;
  90. /* Enable SW override */
  91. reg = reg_read(REG_DRAM_TRAINING_2_ADDR) |
  92. (1 << REG_DRAM_TRAINING_2_SW_OVRD_OFFS);
  93. /* [0] = 1 - Enable SW override */
  94. /* 0x15B8 - Training SW 2 Register */
  95. reg_write(REG_DRAM_TRAINING_2_ADDR, reg);
  96. DEBUG_PBS_S("DDR3 - PBS RX - SW Override Enabled\n");
  97. reg = 1 << REG_DRAM_TRAINING_AUTO_OFFS;
  98. reg_write(REG_DRAM_TRAINING_ADDR, reg); /* 0x15B0 - Training Register */
  99. /* Running twice for 2 different patterns. each patterns - 3 times */
  100. for (pattern_idx = 0; pattern_idx < COUNT_PBS_PATTERN; pattern_idx++) {
  101. DEBUG_PBS_C("DDR3 - PBS TX - Working with pattern - ",
  102. pattern_idx, 1);
  103. /* Reset sum array */
  104. for (pup = 0; pup < pups; pup++) {
  105. for (dq = 0; dq < DQ_NUM; dq++)
  106. skew_sum_array[pup][dq] = 0;
  107. }
  108. /*
  109. * Perform PBS several of times (3 for each pattern).
  110. * At the end, we'll use the average
  111. */
  112. /* If there is ECC, do each PBS again with mux change */
  113. for (pbs_retry = 0; pbs_retry < COUNT_PBS_REPEAT; pbs_retry++) {
  114. for (ecc = 0; ecc < (dram_info->ecc_ena + 1); ecc++) {
  115. /*
  116. * This parameter stores the current PUP
  117. * num - ecc mode dependent - 4-8 / 1 pups
  118. */
  119. cur_max_pup = (1 - ecc) *
  120. dram_info->num_of_std_pups + ecc;
  121. if (ecc) {
  122. /* Only 1 pup in this case */
  123. valid_pup = 0x1;
  124. } else if (cur_max_pup > 4) {
  125. /* 64 bit - 8 pups */
  126. valid_pup = 0xFF;
  127. } else if (cur_max_pup == 4) {
  128. /* 32 bit - 4 pups */
  129. valid_pup = 0xF;
  130. } else {
  131. /* 16 bit - 2 pups */
  132. valid_pup = 0x3;
  133. }
  134. /* ECC Support - Switch ECC Mux on ecc=1 */
  135. reg = reg_read(REG_DRAM_TRAINING_2_ADDR) &
  136. ~(1 << REG_DRAM_TRAINING_2_ECC_MUX_OFFS);
  137. reg |= (dram_info->ecc_ena * ecc <<
  138. REG_DRAM_TRAINING_2_ECC_MUX_OFFS);
  139. reg_write(REG_DRAM_TRAINING_2_ADDR, reg);
  140. if (ecc)
  141. DEBUG_PBS_S("DDR3 - PBS Tx - ECC Mux Enabled\n");
  142. else
  143. DEBUG_PBS_S("DDR3 - PBS Tx - ECC Mux Disabled\n");
  144. /* Init iteration values */
  145. /* Clear the locked DQs */
  146. for (pup = 0; pup < cur_max_pup; pup++) {
  147. for (dq = 0; dq < DQ_NUM; dq++) {
  148. pbs_locked_dq[
  149. pup + ecc *
  150. (max_pup - 1)][dq] =
  151. 0;
  152. }
  153. }
  154. pbs_rep_time = 0;
  155. cur_pup = valid_pup;
  156. start_over = 0;
  157. /*
  158. * Run loop On current Pattern and current
  159. * pattern iteration (just to cover the false
  160. * fail problem)
  161. */
  162. do {
  163. DEBUG_PBS_S("DDR3 - PBS Tx - Pbs Rep Loop is ");
  164. DEBUG_PBS_D(pbs_rep_time, 1);
  165. DEBUG_PBS_S(", for Retry No.");
  166. DEBUG_PBS_D(pbs_retry, 1);
  167. DEBUG_PBS_S("\n");
  168. /* Set all PBS values to MIN (0) */
  169. DEBUG_PBS_S("DDR3 - PBS Tx - Set all PBS values to MIN\n");
  170. for (dq = 0; dq < DQ_NUM; dq++) {
  171. ddr3_write_pup_reg(
  172. PUP_PBS_TX +
  173. pbs_dq_mapping[pup *
  174. (1 - ecc) +
  175. ecc * ECC_PUP]
  176. [dq], CS0, (1 - ecc) *
  177. PUP_BC + ecc * ECC_PUP, 0,
  178. 0);
  179. }
  180. /*
  181. * Shift DQ ADLL right, One step before
  182. * fail
  183. */
  184. DEBUG_PBS_S("DDR3 - PBS Tx - ADLL shift right one phase before fail\n");
  185. if (MV_OK != ddr3_tx_shift_dqs_adll_step_before_fail
  186. (dram_info, cur_pup, pattern_idx,
  187. ecc))
  188. return MV_DDR3_TRAINING_ERR_PBS_ADLL_SHR_1PHASE;
  189. /* PBS For each bit */
  190. DEBUG_PBS_S("DDR3 - PBS Tx - perform PBS for each bit\n");
  191. /*
  192. * In this stage - start_over = 0
  193. */
  194. if (MV_OK != ddr3_pbs_per_bit(
  195. dram_info, &start_over, 1,
  196. &cur_pup, pattern_idx, ecc))
  197. return MV_DDR3_TRAINING_ERR_PBS_TX_PER_BIT;
  198. } while ((start_over == 1) &&
  199. (++pbs_rep_time < COUNT_PBS_STARTOVER));
  200. if (pbs_rep_time == COUNT_PBS_STARTOVER &&
  201. start_over == 1) {
  202. DEBUG_PBS_S("DDR3 - PBS Tx - FAIL - Adll reach max value\n");
  203. return MV_DDR3_TRAINING_ERR_PBS_TX_MAX_VAL;
  204. }
  205. DEBUG_PBS_FULL_C("DDR3 - PBS TX - values for iteration - ",
  206. pbs_retry, 1);
  207. for (pup = 0; pup < cur_max_pup; pup++) {
  208. /*
  209. * To minimize delay elements, inc
  210. * from pbs value the min pbs val
  211. */
  212. DEBUG_PBS_S("DDR3 - PBS - PUP");
  213. DEBUG_PBS_D((pup + (ecc * ECC_PUP)), 1);
  214. DEBUG_PBS_S(": ");
  215. for (dq = 0; dq < DQ_NUM; dq++) {
  216. /* Set skew value for all dq */
  217. /*
  218. * Bit# Deskew <- Bit# Deskew -
  219. * last / first failing bit
  220. * Deskew For all bits (per PUP)
  221. * (minimize delay elements)
  222. */
  223. DEBUG_PBS_S("DQ");
  224. DEBUG_PBS_D(dq, 1);
  225. DEBUG_PBS_S("-");
  226. DEBUG_PBS_D(skew_array
  227. [((pup) * DQ_NUM) +
  228. dq], 2);
  229. DEBUG_PBS_S(", ");
  230. }
  231. DEBUG_PBS_S("\n");
  232. }
  233. /*
  234. * Collect the results we got on this trial
  235. * of PBS
  236. */
  237. for (pup = 0; pup < cur_max_pup; pup++) {
  238. for (dq = 0; dq < DQ_NUM; dq++) {
  239. skew_sum_array[pup + (ecc * (max_pup - 1))]
  240. [dq] += skew_array
  241. [((pup) * DQ_NUM) + dq];
  242. }
  243. }
  244. /* ECC Support - Disable ECC MUX */
  245. reg = reg_read(REG_DRAM_TRAINING_2_ADDR) &
  246. ~(1 << REG_DRAM_TRAINING_2_ECC_MUX_OFFS);
  247. reg_write(REG_DRAM_TRAINING_2_ADDR, reg);
  248. }
  249. }
  250. DEBUG_PBS_C("DDR3 - PBS TX - values for current pattern - ",
  251. pattern_idx, 1);
  252. for (pup = 0; pup < max_pup; pup++) {
  253. /*
  254. * To minimize delay elements, inc from pbs value the
  255. * min pbs val
  256. */
  257. DEBUG_PBS_S("DDR3 - PBS - PUP");
  258. DEBUG_PBS_D(pup, 1);
  259. DEBUG_PBS_S(": ");
  260. for (dq = 0; dq < DQ_NUM; dq++) {
  261. /* set skew value for all dq */
  262. /* Bit# Deskew <- Bit# Deskew - last / first failing bit Deskew For all bits (per PUP) (minimize delay elements) */
  263. DEBUG_PBS_S("DQ");
  264. DEBUG_PBS_D(dq, 1);
  265. DEBUG_PBS_S("-");
  266. DEBUG_PBS_D(skew_sum_array[pup][dq] /
  267. COUNT_PBS_REPEAT, 2);
  268. DEBUG_PBS_S(", ");
  269. }
  270. DEBUG_PBS_S("\n");
  271. }
  272. /*
  273. * Calculate the average skew for current pattern for each
  274. * pup and each bit
  275. */
  276. DEBUG_PBS_C("DDR3 - PBS TX - Average for pattern - ",
  277. pattern_idx, 1);
  278. for (pup = 0; pup < max_pup; pup++) {
  279. /*
  280. * FOR ECC only :: found min and max value for current
  281. * pattern skew array
  282. */
  283. /* Loop for all dqs */
  284. for (dq = 0; dq < DQ_NUM; dq++) {
  285. pattern_skew_array[pup][dq] +=
  286. (skew_sum_array[pup][dq] /
  287. COUNT_PBS_REPEAT);
  288. }
  289. }
  290. }
  291. /* Calculate the average skew */
  292. for (pup = 0; pup < max_pup; pup++) {
  293. for (dq = 0; dq < DQ_NUM; dq++)
  294. skew_array[((pup) * DQ_NUM) + dq] =
  295. pattern_skew_array[pup][dq] / COUNT_PBS_PATTERN;
  296. }
  297. DEBUG_PBS_S("DDR3 - PBS TX - Average for all patterns:\n");
  298. for (pup = 0; pup < max_pup; pup++) {
  299. /*
  300. * To minimize delay elements, inc from pbs value the min
  301. * pbs val
  302. */
  303. DEBUG_PBS_S("DDR3 - PBS - PUP");
  304. DEBUG_PBS_D(pup, 1);
  305. DEBUG_PBS_S(": ");
  306. for (dq = 0; dq < DQ_NUM; dq++) {
  307. /* Set skew value for all dq */
  308. /*
  309. * Bit# Deskew <- Bit# Deskew - last / first
  310. * failing bit Deskew For all bits (per PUP)
  311. * (minimize delay elements)
  312. */
  313. DEBUG_PBS_S("DQ");
  314. DEBUG_PBS_D(dq, 1);
  315. DEBUG_PBS_S("-");
  316. DEBUG_PBS_D(skew_array[(pup * DQ_NUM) + dq], 2);
  317. DEBUG_PBS_S(", ");
  318. }
  319. DEBUG_PBS_S("\n");
  320. }
  321. /* Return ADLL to default value */
  322. for (pup = 0; pup < max_pup; pup++) {
  323. if (pup == (max_pup - 1) && dram_info->ecc_ena)
  324. pup = ECC_PUP;
  325. ddr3_pbs_write_pup_dqs_reg(CS0, pup, INIT_WL_DELAY);
  326. }
  327. /* Set averaged PBS results */
  328. ddr3_set_pbs_results(dram_info, 1);
  329. /* Disable SW override - Must be in a different stage */
  330. /* [0]=0 - Enable SW override */
  331. reg = reg_read(REG_DRAM_TRAINING_2_ADDR);
  332. reg &= ~(1 << REG_DRAM_TRAINING_2_SW_OVRD_OFFS);
  333. /* 0x15B8 - Training SW 2 Register */
  334. reg_write(REG_DRAM_TRAINING_2_ADDR, reg);
  335. reg = reg_read(REG_DRAM_TRAINING_1_ADDR) |
  336. (1 << REG_DRAM_TRAINING_1_TRNBPOINT_OFFS);
  337. reg_write(REG_DRAM_TRAINING_1_ADDR, reg);
  338. DEBUG_PBS_S("DDR3 - PBS Tx - PBS TX ended successfuly\n");
  339. return MV_OK;
  340. }
  341. /*
  342. * Name: ddr3_tx_shift_dqs_adll_step_before_fail
  343. * Desc: Execute the Tx shift DQ phase.
  344. * Args: dram_info ddr3 training information struct
  345. * cur_pup bit array of the function active pups.
  346. * pbs_pattern_idx Index of PBS pattern
  347. * Notes:
  348. * Returns: MV_OK if success, other error code if fail.
  349. */
  350. static int ddr3_tx_shift_dqs_adll_step_before_fail(MV_DRAM_INFO *dram_info,
  351. u32 cur_pup,
  352. u32 pbs_pattern_idx, u32 ecc)
  353. {
  354. u32 unlock_pup; /* bit array of unlock pups */
  355. u32 new_lockup_pup; /* bit array of compare failed pups */
  356. u32 adll_val = 4; /* INIT_WL_DELAY */
  357. u32 cur_max_pup, pup;
  358. u32 dqs_dly_set[MAX_PUP_NUM] = { 0 };
  359. u32 *pattern_ptr;
  360. /* Choose pattern */
  361. switch (dram_info->ddr_width) {
  362. #if defined(MV88F672X)
  363. case 16:
  364. pattern_ptr = (u32 *)&pbs_pattern[pbs_pattern_idx];
  365. break;
  366. #endif
  367. case 32:
  368. pattern_ptr = (u32 *)&pbs_pattern_32b[pbs_pattern_idx];
  369. break;
  370. #if defined(MV88F78X60)
  371. case 64:
  372. pattern_ptr = (u32 *)&pbs_pattern_64b[pbs_pattern_idx];
  373. break;
  374. #endif
  375. default:
  376. return MV_FAIL;
  377. }
  378. /* Set current pup number */
  379. if (cur_pup == 0x1) /* Ecc mode */
  380. cur_max_pup = 1;
  381. else
  382. cur_max_pup = dram_info->num_of_std_pups;
  383. unlock_pup = cur_pup; /* '1' for each unlocked pup */
  384. /* Loop on all ADLL Vaules */
  385. do {
  386. /* Loop until found first fail */
  387. adll_val++;
  388. /*
  389. * Increment (Move to right - ADLL) DQ TX delay
  390. * (broadcast to all Data PUPs)
  391. */
  392. for (pup = 0; pup < cur_max_pup; pup++)
  393. ddr3_pbs_write_pup_dqs_reg(CS0,
  394. pup * (1 - ecc) +
  395. ECC_PUP * ecc, adll_val);
  396. /*
  397. * Write and Read, compare results (read was already verified)
  398. */
  399. /* 0 - all locked */
  400. new_lockup_pup = 0;
  401. if (MV_OK != ddr3_sdram_compare(dram_info, unlock_pup,
  402. &new_lockup_pup,
  403. pattern_ptr, LEN_PBS_PATTERN,
  404. SDRAM_PBS_TX_OFFS, 1, 0,
  405. NULL,
  406. 0))
  407. return MV_FAIL;
  408. unlock_pup &= ~new_lockup_pup;
  409. DEBUG_PBS_FULL_S("Shift DQS by 2 steps for PUPs: ");
  410. DEBUG_PBS_FULL_D(unlock_pup, 2);
  411. DEBUG_PBS_FULL_C(", Set ADLL value = ", adll_val, 2);
  412. /* If any PUP failed there is '1' to mark the PUP */
  413. if (new_lockup_pup != 0) {
  414. /*
  415. * Decrement (Move Back to Left two steps - ADLL)
  416. * DQ TX delay for current failed pups and save
  417. */
  418. for (pup = 0; pup < cur_max_pup; pup++) {
  419. if (((new_lockup_pup >> pup) & 0x1) &&
  420. dqs_dly_set[pup] == 0)
  421. dqs_dly_set[pup] = adll_val - 1;
  422. }
  423. }
  424. } while ((unlock_pup != 0) && (adll_val != ADLL_MAX));
  425. if (unlock_pup != 0) {
  426. DEBUG_PBS_FULL_S("DDR3 - PBS Tx - Shift DQ - Adll value reached maximum\n");
  427. for (pup = 0; pup < cur_max_pup; pup++) {
  428. if (((unlock_pup >> pup) & 0x1) &&
  429. dqs_dly_set[pup] == 0)
  430. dqs_dly_set[pup] = adll_val - 1;
  431. }
  432. }
  433. DEBUG_PBS_FULL_C("PBS TX one step before fail last pups locked Adll ",
  434. adll_val - 2, 2);
  435. /* Set the PUP DQS DLY Values */
  436. for (pup = 0; pup < cur_max_pup; pup++)
  437. ddr3_pbs_write_pup_dqs_reg(CS0, pup * (1 - ecc) + ECC_PUP * ecc,
  438. dqs_dly_set[pup]);
  439. /* Found one phase before fail */
  440. return MV_OK;
  441. }
  442. /*
  443. * Name: ddr3_pbs_rx
  444. * Desc: Execute the PBS RX phase.
  445. * Args: dram_info ddr3 training information struct
  446. * Notes:
  447. * Returns: MV_OK if success, other error code if fail.
  448. */
  449. int ddr3_pbs_rx(MV_DRAM_INFO *dram_info)
  450. {
  451. /*
  452. * Array to hold the total sum of skew from all iterations
  453. * (for average purpose)
  454. */
  455. u32 skew_sum_array[MAX_PUP_NUM][DQ_NUM] = { {0} };
  456. /*
  457. * Array to hold the total average skew from both patterns
  458. * (for average purpose)
  459. */
  460. u32 pattern_skew_array[MAX_PUP_NUM][DQ_NUM] = { {0} };
  461. u32 pbs_rep_time = 0; /* counts number of loop in case of fail */
  462. /* bit array for unlock pups - used to repeat on the RX operation */
  463. u32 cur_pup;
  464. u32 max_pup;
  465. u32 pbs_retry;
  466. u32 pup, dq, pups, cur_max_pup, valid_pup, reg;
  467. u32 pattern_idx;
  468. u32 ecc;
  469. /* indicates whether we need to start the loop again */
  470. int start_over;
  471. int status;
  472. DEBUG_PBS_S("DDR3 - PBS RX - Starting PBS RX procedure\n");
  473. pups = dram_info->num_of_total_pups;
  474. max_pup = dram_info->num_of_total_pups;
  475. /* Enable SW override */
  476. reg = reg_read(REG_DRAM_TRAINING_2_ADDR) |
  477. (1 << REG_DRAM_TRAINING_2_SW_OVRD_OFFS);
  478. /* [0] = 1 - Enable SW override */
  479. /* 0x15B8 - Training SW 2 Register */
  480. reg_write(REG_DRAM_TRAINING_2_ADDR, reg);
  481. DEBUG_PBS_FULL_S("DDR3 - PBS RX - SW Override Enabled\n");
  482. reg = 1 << REG_DRAM_TRAINING_AUTO_OFFS;
  483. reg_write(REG_DRAM_TRAINING_ADDR, reg); /* 0x15B0 - Training Register */
  484. /* Running twice for 2 different patterns. each patterns - 3 times */
  485. for (pattern_idx = 0; pattern_idx < COUNT_PBS_PATTERN; pattern_idx++) {
  486. DEBUG_PBS_FULL_C("DDR3 - PBS RX - Working with pattern - ",
  487. pattern_idx, 1);
  488. /* Reset sum array */
  489. for (pup = 0; pup < pups; pup++) {
  490. for (dq = 0; dq < DQ_NUM; dq++)
  491. skew_sum_array[pup][dq] = 0;
  492. }
  493. /*
  494. * Perform PBS several of times (3 for each pattern).
  495. * At the end, we'll use the average
  496. */
  497. /* If there is ECC, do each PBS again with mux change */
  498. for (pbs_retry = 0; pbs_retry < COUNT_PBS_REPEAT; pbs_retry++) {
  499. for (ecc = 0; ecc < (dram_info->ecc_ena + 1); ecc++) {
  500. /*
  501. * This parameter stores the current PUP
  502. * num - ecc mode dependent - 4-8 / 1 pups
  503. */
  504. cur_max_pup = (1 - ecc) *
  505. dram_info->num_of_std_pups + ecc;
  506. if (ecc) {
  507. /* Only 1 pup in this case */
  508. valid_pup = 0x1;
  509. } else if (cur_max_pup > 4) {
  510. /* 64 bit - 8 pups */
  511. valid_pup = 0xFF;
  512. } else if (cur_max_pup == 4) {
  513. /* 32 bit - 4 pups */
  514. valid_pup = 0xF;
  515. } else {
  516. /* 16 bit - 2 pups */
  517. valid_pup = 0x3;
  518. }
  519. /* ECC Support - Switch ECC Mux on ecc=1 */
  520. reg = reg_read(REG_DRAM_TRAINING_2_ADDR) &
  521. ~(1 << REG_DRAM_TRAINING_2_ECC_MUX_OFFS);
  522. reg |= (dram_info->ecc_ena * ecc <<
  523. REG_DRAM_TRAINING_2_ECC_MUX_OFFS);
  524. reg_write(REG_DRAM_TRAINING_2_ADDR, reg);
  525. if (ecc)
  526. DEBUG_PBS_FULL_S("DDR3 - PBS Rx - ECC Mux Enabled\n");
  527. else
  528. DEBUG_PBS_FULL_S("DDR3 - PBS Rx - ECC Mux Disabled\n");
  529. /* Init iteration values */
  530. /* Clear the locked DQs */
  531. for (pup = 0; pup < cur_max_pup; pup++) {
  532. for (dq = 0; dq < DQ_NUM; dq++) {
  533. pbs_locked_dq[
  534. pup + ecc * (max_pup - 1)][dq] =
  535. 0;
  536. }
  537. }
  538. pbs_rep_time = 0;
  539. cur_pup = valid_pup;
  540. start_over = 0;
  541. /*
  542. * Run loop On current Pattern and current
  543. * pattern iteration (just to cover the false
  544. * fail problem
  545. */
  546. do {
  547. DEBUG_PBS_FULL_S("DDR3 - PBS Rx - Pbs Rep Loop is ");
  548. DEBUG_PBS_FULL_D(pbs_rep_time, 1);
  549. DEBUG_PBS_FULL_S(", for Retry No.");
  550. DEBUG_PBS_FULL_D(pbs_retry, 1);
  551. DEBUG_PBS_FULL_S("\n");
  552. /* Set all PBS values to MAX (31) */
  553. for (pup = 0; pup < cur_max_pup; pup++) {
  554. for (dq = 0; dq < DQ_NUM; dq++)
  555. ddr3_write_pup_reg(
  556. PUP_PBS_RX +
  557. pbs_dq_mapping[
  558. pup * (1 - ecc)
  559. + ecc * ECC_PUP]
  560. [dq], CS0,
  561. pup + ecc * ECC_PUP,
  562. 0, MAX_PBS);
  563. }
  564. /* Set all DQS PBS values to MIN (0) */
  565. for (pup = 0; pup < cur_max_pup; pup++) {
  566. ddr3_write_pup_reg(PUP_PBS_RX +
  567. DQ_NUM, CS0,
  568. pup +
  569. ecc *
  570. ECC_PUP, 0,
  571. 0);
  572. }
  573. /* Shift DQS, To first Fail */
  574. DEBUG_PBS_FULL_S("DDR3 - PBS Rx - Shift RX DQS to first fail\n");
  575. status = ddr3_rx_shift_dqs_to_first_fail
  576. (dram_info, cur_pup,
  577. pattern_idx, ecc);
  578. if (MV_OK != status) {
  579. DEBUG_PBS_S("DDR3 - PBS Rx - ddr3_rx_shift_dqs_to_first_fail failed.\n");
  580. DEBUG_PBS_D(status, 8);
  581. DEBUG_PBS_S("\nDDR3 - PBS Rx - SKIP.\n");
  582. /* Reset read FIFO */
  583. reg = reg_read(REG_DRAM_TRAINING_ADDR);
  584. /* Start Auto Read Leveling procedure */
  585. reg |= (1 << REG_DRAM_TRAINING_RL_OFFS);
  586. /* 0x15B0 - Training Register */
  587. reg_write(REG_DRAM_TRAINING_ADDR, reg);
  588. reg = reg_read(REG_DRAM_TRAINING_2_ADDR);
  589. reg |= ((1 << REG_DRAM_TRAINING_2_FIFO_RST_OFFS)
  590. + (1 << REG_DRAM_TRAINING_2_SW_OVRD_OFFS));
  591. /* [0] = 1 - Enable SW override, [4] = 1 - FIFO reset */
  592. /* 0x15B8 - Training SW 2 Register */
  593. reg_write(REG_DRAM_TRAINING_2_ADDR, reg);
  594. do {
  595. reg = (reg_read(REG_DRAM_TRAINING_2_ADDR))
  596. & (1 << REG_DRAM_TRAINING_2_FIFO_RST_OFFS);
  597. } while (reg); /* Wait for '0' */
  598. reg = reg_read(REG_DRAM_TRAINING_ADDR);
  599. /* Clear Auto Read Leveling procedure */
  600. reg &= ~(1 << REG_DRAM_TRAINING_RL_OFFS);
  601. /* 0x15B0 - Training Register */
  602. reg_write(REG_DRAM_TRAINING_ADDR, reg);
  603. /* Set ADLL to 15 */
  604. for (pup = 0; pup < max_pup;
  605. pup++) {
  606. ddr3_write_pup_reg
  607. (PUP_DQS_RD, CS0,
  608. pup +
  609. (ecc * ECC_PUP), 0,
  610. 15);
  611. }
  612. /* Set all PBS values to MIN (0) */
  613. for (pup = 0; pup < cur_max_pup;
  614. pup++) {
  615. for (dq = 0;
  616. dq < DQ_NUM; dq++)
  617. ddr3_write_pup_reg
  618. (PUP_PBS_RX +
  619. pbs_dq_mapping
  620. [pup * (1 - ecc) +
  621. ecc * ECC_PUP]
  622. [dq], CS0,
  623. pup + ecc * ECC_PUP,
  624. 0, MIN_PBS);
  625. }
  626. return MV_OK;
  627. }
  628. /* PBS For each bit */
  629. DEBUG_PBS_FULL_S("DDR3 - PBS Rx - perform PBS for each bit\n");
  630. /* in this stage - start_over = 0; */
  631. if (MV_OK != ddr3_pbs_per_bit(
  632. dram_info, &start_over,
  633. 0, &cur_pup,
  634. pattern_idx, ecc)) {
  635. DEBUG_PBS_S("DDR3 - PBS Rx - ddr3_pbs_per_bit failed.");
  636. return MV_DDR3_TRAINING_ERR_PBS_RX_PER_BIT;
  637. }
  638. } while ((start_over == 1) &&
  639. (++pbs_rep_time < COUNT_PBS_STARTOVER));
  640. if (pbs_rep_time == COUNT_PBS_STARTOVER &&
  641. start_over == 1) {
  642. DEBUG_PBS_FULL_S("DDR3 - PBS Rx - FAIL - Algorithm failed doing RX PBS\n");
  643. return MV_DDR3_TRAINING_ERR_PBS_RX_MAX_VAL;
  644. }
  645. /* Return DQS ADLL to default value - 15 */
  646. /* Set all DQS PBS values to MIN (0) */
  647. for (pup = 0; pup < cur_max_pup; pup++)
  648. ddr3_write_pup_reg(PUP_DQS_RD, CS0,
  649. pup + ecc * ECC_PUP,
  650. 0, INIT_RL_DELAY);
  651. DEBUG_PBS_FULL_C("DDR3 - PBS RX - values for iteration - ",
  652. pbs_retry, 1);
  653. for (pup = 0; pup < cur_max_pup; pup++) {
  654. /*
  655. * To minimize delay elements, inc from
  656. * pbs value the min pbs val
  657. */
  658. DEBUG_PBS_FULL_S("DDR3 - PBS - PUP");
  659. DEBUG_PBS_FULL_D((pup +
  660. (ecc * ECC_PUP)), 1);
  661. DEBUG_PBS_FULL_S(": ");
  662. for (dq = 0; dq < DQ_NUM; dq++) {
  663. /* Set skew value for all dq */
  664. /*
  665. * Bit# Deskew <- Bit# Deskew -
  666. * last / first failing bit
  667. * Deskew For all bits (per PUP)
  668. * (minimize delay elements)
  669. */
  670. DEBUG_PBS_FULL_S("DQ");
  671. DEBUG_PBS_FULL_D(dq, 1);
  672. DEBUG_PBS_FULL_S("-");
  673. DEBUG_PBS_FULL_D(skew_array
  674. [((pup) *
  675. DQ_NUM) +
  676. dq], 2);
  677. DEBUG_PBS_FULL_S(", ");
  678. }
  679. DEBUG_PBS_FULL_S("\n");
  680. }
  681. /*
  682. * Collect the results we got on this trial
  683. * of PBS
  684. */
  685. for (pup = 0; pup < cur_max_pup; pup++) {
  686. for (dq = 0; dq < DQ_NUM; dq++) {
  687. skew_sum_array
  688. [pup + (ecc * (max_pup - 1))]
  689. [dq] +=
  690. skew_array[((pup) * DQ_NUM) + dq];
  691. }
  692. }
  693. /* ECC Support - Disable ECC MUX */
  694. reg = reg_read(REG_DRAM_TRAINING_2_ADDR) &
  695. ~(1 << REG_DRAM_TRAINING_2_ECC_MUX_OFFS);
  696. reg_write(REG_DRAM_TRAINING_2_ADDR, reg);
  697. }
  698. }
  699. /*
  700. * Calculate the average skew for current pattern for each
  701. * pup and each bit
  702. */
  703. DEBUG_PBS_FULL_C("DDR3 - PBS RX - Average for pattern - ",
  704. pattern_idx, 1);
  705. for (pup = 0; pup < max_pup; pup++) {
  706. /*
  707. * FOR ECC only :: found min and max value for
  708. * current pattern skew array
  709. */
  710. /* Loop for all dqs */
  711. for (dq = 0; dq < DQ_NUM; dq++) {
  712. pattern_skew_array[pup][dq] +=
  713. (skew_sum_array[pup][dq] /
  714. COUNT_PBS_REPEAT);
  715. }
  716. }
  717. DEBUG_PBS_C("DDR3 - PBS RX - values for current pattern - ",
  718. pattern_idx, 1);
  719. for (pup = 0; pup < max_pup; pup++) {
  720. /*
  721. * To minimize delay elements, inc from pbs value the
  722. * min pbs val
  723. */
  724. DEBUG_PBS_S("DDR3 - PBS RX - PUP");
  725. DEBUG_PBS_D(pup, 1);
  726. DEBUG_PBS_S(": ");
  727. for (dq = 0; dq < DQ_NUM; dq++) {
  728. /* Set skew value for all dq */
  729. /*
  730. * Bit# Deskew <- Bit# Deskew - last / first
  731. * failing bit Deskew For all bits (per PUP)
  732. * (minimize delay elements)
  733. */
  734. DEBUG_PBS_S("DQ");
  735. DEBUG_PBS_D(dq, 1);
  736. DEBUG_PBS_S("-");
  737. DEBUG_PBS_D(skew_sum_array[pup][dq] /
  738. COUNT_PBS_REPEAT, 2);
  739. DEBUG_PBS_S(", ");
  740. }
  741. DEBUG_PBS_S("\n");
  742. }
  743. }
  744. /* Calculate the average skew */
  745. for (pup = 0; pup < max_pup; pup++) {
  746. for (dq = 0; dq < DQ_NUM; dq++)
  747. skew_array[((pup) * DQ_NUM) + dq] =
  748. pattern_skew_array[pup][dq] / COUNT_PBS_PATTERN;
  749. }
  750. DEBUG_PBS_S("DDR3 - PBS RX - Average for all patterns:\n");
  751. for (pup = 0; pup < max_pup; pup++) {
  752. /*
  753. * To minimize delay elements, inc from pbs value the
  754. * min pbs val
  755. */
  756. DEBUG_PBS_S("DDR3 - PBS - PUP");
  757. DEBUG_PBS_D(pup, 1);
  758. DEBUG_PBS_S(": ");
  759. for (dq = 0; dq < DQ_NUM; dq++) {
  760. /* Set skew value for all dq */
  761. /*
  762. * Bit# Deskew <- Bit# Deskew - last / first
  763. * failing bit Deskew For all bits (per PUP)
  764. * (minimize delay elements)
  765. */
  766. DEBUG_PBS_S("DQ");
  767. DEBUG_PBS_D(dq, 1);
  768. DEBUG_PBS_S("-");
  769. DEBUG_PBS_D(skew_array[(pup * DQ_NUM) + dq], 2);
  770. DEBUG_PBS_S(", ");
  771. }
  772. DEBUG_PBS_S("\n");
  773. }
  774. /* Return ADLL to default value */
  775. ddr3_write_pup_reg(PUP_DQS_RD, CS0, PUP_BC, 0, INIT_RL_DELAY);
  776. /* Set averaged PBS results */
  777. ddr3_set_pbs_results(dram_info, 0);
  778. /* Disable SW override - Must be in a different stage */
  779. /* [0]=0 - Enable SW override */
  780. reg = reg_read(REG_DRAM_TRAINING_2_ADDR);
  781. reg &= ~(1 << REG_DRAM_TRAINING_2_SW_OVRD_OFFS);
  782. /* 0x15B8 - Training SW 2 Register */
  783. reg_write(REG_DRAM_TRAINING_2_ADDR, reg);
  784. reg = reg_read(REG_DRAM_TRAINING_1_ADDR) |
  785. (1 << REG_DRAM_TRAINING_1_TRNBPOINT_OFFS);
  786. reg_write(REG_DRAM_TRAINING_1_ADDR, reg);
  787. DEBUG_PBS_FULL_S("DDR3 - PBS RX - ended successfuly\n");
  788. return MV_OK;
  789. }
  790. /*
  791. * Name: ddr3_rx_shift_dqs_to_first_fail
  792. * Desc: Execute the Rx shift DQ phase.
  793. * Args: dram_info ddr3 training information struct
  794. * cur_pup bit array of the function active pups.
  795. * pbs_pattern_idx Index of PBS pattern
  796. * Notes:
  797. * Returns: MV_OK if success, other error code if fail.
  798. */
  799. static int ddr3_rx_shift_dqs_to_first_fail(MV_DRAM_INFO *dram_info, u32 cur_pup,
  800. u32 pbs_pattern_idx, u32 ecc)
  801. {
  802. u32 unlock_pup; /* bit array of unlock pups */
  803. u32 new_lockup_pup; /* bit array of compare failed pups */
  804. u32 adll_val = MAX_DELAY;
  805. u32 dqs_deskew_val = 0; /* current value of DQS PBS deskew */
  806. u32 cur_max_pup, pup, pass_pup;
  807. u32 *pattern_ptr;
  808. /* Choose pattern */
  809. switch (dram_info->ddr_width) {
  810. #if defined(MV88F672X)
  811. case 16:
  812. pattern_ptr = (u32 *)&pbs_pattern[pbs_pattern_idx];
  813. break;
  814. #endif
  815. case 32:
  816. pattern_ptr = (u32 *)&pbs_pattern_32b[pbs_pattern_idx];
  817. break;
  818. #if defined(MV88F78X60)
  819. case 64:
  820. pattern_ptr = (u32 *)&pbs_pattern_64b[pbs_pattern_idx];
  821. break;
  822. #endif
  823. default:
  824. return MV_FAIL;
  825. }
  826. /* Set current pup number */
  827. if (cur_pup == 0x1) /* Ecc mode */
  828. cur_max_pup = 1;
  829. else
  830. cur_max_pup = dram_info->num_of_std_pups;
  831. unlock_pup = cur_pup; /* '1' for each unlocked pup */
  832. DEBUG_PBS_FULL_S("DDR3 - PBS RX - Shift DQS - Starting...\n");
  833. /* Set DQS ADLL to MAX */
  834. DEBUG_PBS_FULL_S("DDR3 - PBS RX - Shift DQS - Set DQS ADLL to Max for all PUPs\n");
  835. for (pup = 0; pup < cur_max_pup; pup++)
  836. ddr3_write_pup_reg(PUP_DQS_RD, CS0, pup + ecc * ECC_PUP, 0,
  837. MAX_DELAY);
  838. /* Loop on all ADLL Vaules */
  839. do {
  840. /* Loop until found fail for all pups */
  841. new_lockup_pup = 0;
  842. if (MV_OK != ddr3_sdram_compare(dram_info, unlock_pup,
  843. &new_lockup_pup,
  844. pattern_ptr, LEN_PBS_PATTERN,
  845. SDRAM_PBS_I_OFFS +
  846. pbs_pattern_idx * SDRAM_PBS_NEXT_OFFS,
  847. 0, 0, NULL, 0)) {
  848. DEBUG_PBS_S("DDR3 - PBS Rx - Shift DQS - MV_DDR3_TRAINING_ERR_PBS_SHIFT_QDS_SRAM_CMP(ddr3_sdram_compare)\n");
  849. return MV_DDR3_TRAINING_ERR_PBS_SHIFT_QDS_SRAM_CMP;
  850. }
  851. if ((new_lockup_pup != 0) && (dqs_deskew_val <= 1)) {
  852. /* Fail on start with first deskew value */
  853. /* Decrement DQS ADLL */
  854. --adll_val;
  855. if (adll_val == ADLL_MIN) {
  856. DEBUG_PBS_S("DDR3 - PBS Rx - Shift DQS - fail on start with first deskew value\n");
  857. return MV_DDR3_TRAINING_ERR_PBS_SHIFT_QDS_SRAM_CMP;
  858. }
  859. ddr3_write_pup_reg(PUP_DQS_RD, CS0, pup + ecc * ECC_PUP,
  860. 0, adll_val);
  861. continue;
  862. }
  863. /* Update all new locked pups */
  864. unlock_pup &= ~new_lockup_pup;
  865. if ((unlock_pup == 0) || (dqs_deskew_val == MAX_PBS)) {
  866. if (dqs_deskew_val == MAX_PBS) {
  867. /*
  868. * Reach max value of dqs deskew or get fail
  869. * for all pups
  870. */
  871. DEBUG_PBS_FULL_S("DDR3 - PBS RX - Shift DQS - DQS deskew reached maximum value\n");
  872. }
  873. break;
  874. }
  875. DEBUG_PBS_FULL_S("DDR3 - PBS RX - Shift DQS - Inc DQS deskew for PUPs: ");
  876. DEBUG_PBS_FULL_D(unlock_pup, 2);
  877. DEBUG_PBS_FULL_C(", deskew = ", dqs_deskew_val, 2);
  878. /* Increment DQS deskew elements - Only for unlocked pups */
  879. dqs_deskew_val++;
  880. for (pup = 0; pup < cur_max_pup; pup++) {
  881. if (IS_PUP_ACTIVE(unlock_pup, pup) == 1) {
  882. ddr3_write_pup_reg(PUP_PBS_RX + DQS_DQ_NUM, CS0,
  883. pup + ecc * ECC_PUP, 0,
  884. dqs_deskew_val);
  885. }
  886. }
  887. } while (1);
  888. DEBUG_PBS_FULL_S("DDR3 - PBS RX - Shift DQS - ADLL shift one step before fail\n");
  889. /* Continue to ADLL shift one step before fail */
  890. unlock_pup = cur_pup;
  891. do {
  892. /* Loop until pass compare for all pups */
  893. new_lockup_pup = 0;
  894. /* Read and compare results */
  895. if (MV_OK != ddr3_sdram_compare(dram_info, unlock_pup, &new_lockup_pup,
  896. pattern_ptr, LEN_PBS_PATTERN,
  897. SDRAM_PBS_I_OFFS +
  898. pbs_pattern_idx * SDRAM_PBS_NEXT_OFFS,
  899. 1, 0, NULL, 0)) {
  900. DEBUG_PBS_S("DDR3 - PBS Rx - Shift DQS - MV_DDR3_TRAINING_ERR_PBS_SHIFT_QDS_SRAM_CMP(ddr3_sdram_compare)\n");
  901. return MV_DDR3_TRAINING_ERR_PBS_SHIFT_QDS_SRAM_CMP;
  902. }
  903. /*
  904. * Get mask for pup which passed so their adll will be
  905. * changed to 2 steps before fails
  906. */
  907. pass_pup = unlock_pup & ~new_lockup_pup;
  908. DEBUG_PBS_FULL_S("Shift DQS by 2 steps for PUPs: ");
  909. DEBUG_PBS_FULL_D(pass_pup, 2);
  910. DEBUG_PBS_FULL_C(", Set ADLL value = ", (adll_val - 2), 2);
  911. /* Only for pass pups */
  912. for (pup = 0; pup < cur_max_pup; pup++) {
  913. if (IS_PUP_ACTIVE(pass_pup, pup) == 1) {
  914. ddr3_write_pup_reg(PUP_DQS_RD, CS0,
  915. pup + ecc * ECC_PUP, 0,
  916. (adll_val - 2));
  917. }
  918. }
  919. /* Locked pups that compare success */
  920. unlock_pup &= new_lockup_pup;
  921. if (unlock_pup == 0) {
  922. /* All pups locked */
  923. break;
  924. }
  925. /* Found error */
  926. if (adll_val == 0) {
  927. DEBUG_PBS_FULL_S("DDR3 - PBS Rx - Shift DQS - Adll reach min value\n");
  928. return MV_DDR3_TRAINING_ERR_PBS_SHIFT_QDS_MAX_VAL;
  929. }
  930. /*
  931. * Decrement (Move Back to Left one phase - ADLL) dqs RX delay
  932. */
  933. adll_val--;
  934. for (pup = 0; pup < cur_max_pup; pup++) {
  935. if (IS_PUP_ACTIVE(unlock_pup, pup) == 1) {
  936. ddr3_write_pup_reg(PUP_DQS_RD, CS0,
  937. pup + ecc * ECC_PUP, 0,
  938. adll_val);
  939. }
  940. }
  941. } while (1);
  942. return MV_OK;
  943. }
  944. /*
  945. * lock_pups() extracted from ddr3_pbs_per_bit(). This just got too
  946. * much indented making it hard to read / edit.
  947. */
  948. static void lock_pups(u32 pup, u32 *pup_locked, u8 *unlock_pup_dq_array,
  949. u32 pbs_curr_val, u32 start_pbs, u32 ecc, int is_tx)
  950. {
  951. u32 dq;
  952. int idx;
  953. /* Lock PBS value for all remaining PUPs bits */
  954. DEBUG_PBS_FULL_S("DDR3 - PBS Per bit - Lock PBS value for all remaining PUPs bits, pup ");
  955. DEBUG_PBS_FULL_D(pup, 1);
  956. DEBUG_PBS_FULL_C(" pbs value ", pbs_curr_val, 2);
  957. idx = pup * (1 - ecc) + ecc * ECC_PUP;
  958. *pup_locked &= ~(1 << pup);
  959. for (dq = 0; dq < DQ_NUM; dq++) {
  960. if (IS_PUP_ACTIVE(unlock_pup_dq_array[dq], pup) == 1) {
  961. int offs;
  962. /* Lock current dq */
  963. unlock_pup_dq_array[dq] &= ~(1 << pup);
  964. skew_array[(pup * DQ_NUM) + dq] = pbs_curr_val;
  965. if (is_tx == 1)
  966. offs = PUP_PBS_TX;
  967. else
  968. offs = PUP_PBS_RX;
  969. ddr3_write_pup_reg(offs +
  970. pbs_dq_mapping[idx][dq], CS0,
  971. idx, 0, start_pbs);
  972. }
  973. }
  974. }
  975. /*
  976. * Name: ddr3_pbs_per_bit
  977. * Desc: Execute the Per Bit Skew phase.
  978. * Args: start_over Return whether need to start over the algorithm
  979. * is_tx Indicate whether Rx or Tx
  980. * pcur_pup bit array of the function active pups. return the
  981. * pups that need to repeat on the PBS
  982. * pbs_pattern_idx Index of PBS pattern
  983. *
  984. * Notes: Current implementation supports double activation of this function.
  985. * i.e. in order to activate this function (using start_over) more than
  986. * twice, the implementation should change.
  987. * imlementation limitation are marked using
  988. * ' CHIP-ONLY! - Implementation Limitation '
  989. * Returns: MV_OK if success, other error code if fail.
  990. */
  991. static int ddr3_pbs_per_bit(MV_DRAM_INFO *dram_info, int *start_over, int is_tx,
  992. u32 *pcur_pup, u32 pbs_pattern_idx, u32 ecc)
  993. {
  994. /*
  995. * Bit array to indicate if we already get fail on bit per pup & dq bit
  996. */
  997. u8 unlock_pup_dq_array[DQ_NUM] = {
  998. *pcur_pup, *pcur_pup, *pcur_pup, *pcur_pup, *pcur_pup,
  999. *pcur_pup, *pcur_pup, *pcur_pup
  1000. };
  1001. u8 cmp_unlock_pup_dq_array[COUNT_PBS_COMP_RETRY_NUM][DQ_NUM];
  1002. u32 pup, dq;
  1003. /* value of pbs is according to RX or TX */
  1004. u32 start_pbs, last_pbs;
  1005. u32 pbs_curr_val;
  1006. /* bit array that indicates all dq of the pup locked */
  1007. u32 pup_locked;
  1008. u32 first_fail[MAX_PUP_NUM] = { 0 }; /* count first fail per pup */
  1009. /* indicates whether we get first fail per pup */
  1010. int first_failed[MAX_PUP_NUM] = { 0 };
  1011. /* bit array that indicates pup already get fail */
  1012. u32 sum_pup_fail;
  1013. /* use to calculate diff between curr pbs to first fail pbs */
  1014. u32 calc_pbs_diff;
  1015. u32 pbs_cmp_retry;
  1016. u32 max_pup;
  1017. /* Set init values for retry array - 8 retry */
  1018. for (pbs_cmp_retry = 0; pbs_cmp_retry < COUNT_PBS_COMP_RETRY_NUM;
  1019. pbs_cmp_retry++) {
  1020. for (dq = 0; dq < DQ_NUM; dq++)
  1021. cmp_unlock_pup_dq_array[pbs_cmp_retry][dq] = *pcur_pup;
  1022. }
  1023. memset(&skew_array, 0, MAX_PUP_NUM * DQ_NUM * sizeof(u32));
  1024. DEBUG_PBS_FULL_S("DDR3 - PBS Per bit - Started\n");
  1025. /* The pbs value depends if rx or tx */
  1026. if (is_tx == 1) {
  1027. start_pbs = MIN_PBS;
  1028. last_pbs = MAX_PBS;
  1029. } else {
  1030. start_pbs = MAX_PBS;
  1031. last_pbs = MIN_PBS;
  1032. }
  1033. pbs_curr_val = start_pbs;
  1034. pup_locked = *pcur_pup;
  1035. /* Set current pup number */
  1036. if (pup_locked == 0x1) /* Ecc mode */
  1037. max_pup = 1;
  1038. else
  1039. max_pup = dram_info->num_of_std_pups;
  1040. do {
  1041. /* Increment/ decrement PBS for un-lock bits only */
  1042. if (is_tx == 1)
  1043. pbs_curr_val++;
  1044. else
  1045. pbs_curr_val--;
  1046. /* Set Current PBS delay */
  1047. for (dq = 0; dq < DQ_NUM; dq++) {
  1048. /* Check DQ bits to see if locked in all pups */
  1049. if (unlock_pup_dq_array[dq] == 0) {
  1050. DEBUG_PBS_FULL_S("DDR3 - PBS Per bit - All pups are locked for DQ ");
  1051. DEBUG_PBS_FULL_D(dq, 1);
  1052. DEBUG_PBS_FULL_S("\n");
  1053. continue;
  1054. }
  1055. for (pup = 0; pup < max_pup; pup++) {
  1056. int idx;
  1057. idx = pup * (1 - ecc) + ecc * ECC_PUP;
  1058. if (IS_PUP_ACTIVE(unlock_pup_dq_array[dq], pup)
  1059. == 0)
  1060. continue;
  1061. if (is_tx == 1)
  1062. ddr3_write_pup_reg(
  1063. PUP_PBS_TX + pbs_dq_mapping[idx][dq],
  1064. CS0, idx, 0, pbs_curr_val);
  1065. else
  1066. ddr3_write_pup_reg(
  1067. PUP_PBS_RX + pbs_dq_mapping[idx][dq],
  1068. CS0, idx, 0, pbs_curr_val);
  1069. }
  1070. }
  1071. /*
  1072. * Write Read and compare results - run the test
  1073. * DDR_PBS_COMP_RETRY_NUM times
  1074. */
  1075. /* Run number of read and write to verify */
  1076. for (pbs_cmp_retry = 0;
  1077. pbs_cmp_retry < COUNT_PBS_COMP_RETRY_NUM;
  1078. pbs_cmp_retry++) {
  1079. if (MV_OK !=
  1080. ddr3_sdram_pbs_compare(dram_info, pup_locked, is_tx,
  1081. pbs_pattern_idx,
  1082. pbs_curr_val, start_pbs,
  1083. skew_array,
  1084. cmp_unlock_pup_dq_array
  1085. [pbs_cmp_retry], ecc))
  1086. return MV_FAIL;
  1087. for (pup = 0; pup < max_pup; pup++) {
  1088. for (dq = 0; dq < DQ_NUM; dq++) {
  1089. if ((IS_PUP_ACTIVE(unlock_pup_dq_array[dq],
  1090. pup) == 1)
  1091. && (IS_PUP_ACTIVE(cmp_unlock_pup_dq_array
  1092. [pbs_cmp_retry][dq],
  1093. pup) == 0)) {
  1094. DEBUG_PBS_FULL_S("DDR3 - PBS Per bit - PbsCurrVal: ");
  1095. DEBUG_PBS_FULL_D(pbs_curr_val, 2);
  1096. DEBUG_PBS_FULL_S(" PUP: ");
  1097. DEBUG_PBS_FULL_D(pup, 1);
  1098. DEBUG_PBS_FULL_S(" DQ: ");
  1099. DEBUG_PBS_FULL_D(dq, 1);
  1100. DEBUG_PBS_FULL_S(" - failed\n");
  1101. }
  1102. }
  1103. }
  1104. for (dq = 0; dq < DQ_NUM; dq++) {
  1105. unlock_pup_dq_array[dq] &=
  1106. cmp_unlock_pup_dq_array[pbs_cmp_retry][dq];
  1107. }
  1108. }
  1109. pup_locked = 0;
  1110. sum_pup_fail = *pcur_pup;
  1111. /* Check which DQ is failed */
  1112. for (dq = 0; dq < DQ_NUM; dq++) {
  1113. /* Summarize the locked pup */
  1114. pup_locked |= unlock_pup_dq_array[dq];
  1115. /* Check if get fail */
  1116. sum_pup_fail &= unlock_pup_dq_array[dq];
  1117. }
  1118. /* If all PUPS are locked in all DQ - Break */
  1119. if (pup_locked == 0) {
  1120. /* All pups are locked */
  1121. *start_over = 0;
  1122. DEBUG_PBS_FULL_S("DDR3 - PBS Per bit - All bit in all pups are successfully locked\n");
  1123. break;
  1124. }
  1125. /* PBS deskew elements reach max ? */
  1126. if (pbs_curr_val == last_pbs) {
  1127. DEBUG_PBS_FULL_S("DDR3 - PBS Per bit - PBS deskew elements reach max\n");
  1128. /* CHIP-ONLY! - Implementation Limitation */
  1129. *start_over = (sum_pup_fail != 0) && (!(*start_over));
  1130. *pcur_pup = pup_locked;
  1131. DEBUG_PBS_FULL_S("DDR3 - PBS Per bit - StartOver: ");
  1132. DEBUG_PBS_FULL_D(*start_over, 1);
  1133. DEBUG_PBS_FULL_S(" pup_locked: ");
  1134. DEBUG_PBS_FULL_D(pup_locked, 2);
  1135. DEBUG_PBS_FULL_S(" sum_pup_fail: ");
  1136. DEBUG_PBS_FULL_D(sum_pup_fail, 2);
  1137. DEBUG_PBS_FULL_S("\n");
  1138. /* Lock PBS value for all remaining bits */
  1139. for (pup = 0; pup < max_pup; pup++) {
  1140. /* Check if current pup already received error */
  1141. if (IS_PUP_ACTIVE(pup_locked, pup) == 1) {
  1142. /* Valid pup for current function */
  1143. if (IS_PUP_ACTIVE(sum_pup_fail, pup) ==
  1144. 1 && (*start_over == 1)) {
  1145. DEBUG_PBS_FULL_C("DDR3 - PBS Per bit - skipping lock of pup (first loop of pbs)",
  1146. pup, 1);
  1147. continue;
  1148. } else
  1149. if (IS_PUP_ACTIVE(sum_pup_fail, pup)
  1150. == 1) {
  1151. DEBUG_PBS_FULL_C("DDR3 - PBS Per bit - Locking pup %d (even though it wasn't supposed to be locked)",
  1152. pup, 1);
  1153. }
  1154. /* Already got fail on the PUP */
  1155. /* Lock PBS value for all remaining bits */
  1156. DEBUG_PBS_FULL_S("DDR3 - PBS Per bit - Locking remaning DQs for pup - ");
  1157. DEBUG_PBS_FULL_D(pup, 1);
  1158. DEBUG_PBS_FULL_S(": ");
  1159. for (dq = 0; dq < DQ_NUM; dq++) {
  1160. if (IS_PUP_ACTIVE
  1161. (unlock_pup_dq_array[dq],
  1162. pup) == 1) {
  1163. DEBUG_PBS_FULL_D(dq, 1);
  1164. DEBUG_PBS_FULL_S(",");
  1165. /* set current PBS */
  1166. skew_array[((pup) *
  1167. DQ_NUM) +
  1168. dq] =
  1169. pbs_curr_val;
  1170. }
  1171. }
  1172. if (*start_over == 1) {
  1173. /*
  1174. * Reset this pup bit - when
  1175. * restart the PBS, ignore this
  1176. * pup
  1177. */
  1178. *pcur_pup &= ~(1 << pup);
  1179. }
  1180. DEBUG_PBS_FULL_S("\n");
  1181. } else {
  1182. DEBUG_PBS_FULL_S("DDR3 - PBS Per bit - Pup ");
  1183. DEBUG_PBS_FULL_D(pup, 1);
  1184. DEBUG_PBS_FULL_C(" is not set in puplocked - ",
  1185. pup_locked, 1);
  1186. }
  1187. }
  1188. /* Need to start the PBS again */
  1189. if (*start_over == 1) {
  1190. DEBUG_PBS_FULL_S("DDR3 - PBS Per bit - false fail - returning to start\n");
  1191. return MV_OK;
  1192. }
  1193. break;
  1194. }
  1195. /* Diff Check */
  1196. for (pup = 0; pup < max_pup; pup++) {
  1197. if (IS_PUP_ACTIVE(pup_locked, pup) == 1) {
  1198. /* pup is not locked */
  1199. if (first_failed[pup] == 0) {
  1200. /* No first fail until now */
  1201. if (IS_PUP_ACTIVE(sum_pup_fail, pup) ==
  1202. 0) {
  1203. /* Get first fail */
  1204. DEBUG_PBS_FULL_C("DDR3 - PBS Per bit - First fail in pup ",
  1205. pup, 1);
  1206. first_failed[pup] = 1;
  1207. first_fail[pup] = pbs_curr_val;
  1208. }
  1209. } else {
  1210. /* Already got first fail */
  1211. if (is_tx == 1) {
  1212. /* TX - inc pbs */
  1213. calc_pbs_diff = pbs_curr_val -
  1214. first_fail[pup];
  1215. } else {
  1216. /* RX - dec pbs */
  1217. calc_pbs_diff = first_fail[pup] -
  1218. pbs_curr_val;
  1219. }
  1220. if (calc_pbs_diff >= PBS_DIFF_LIMIT) {
  1221. lock_pups(pup, &pup_locked,
  1222. unlock_pup_dq_array,
  1223. pbs_curr_val,
  1224. start_pbs, ecc, is_tx);
  1225. }
  1226. }
  1227. }
  1228. }
  1229. } while (1);
  1230. return MV_OK;
  1231. }
  1232. /*
  1233. * Name: ddr3_set_pbs_results
  1234. * Desc: Set to HW the PBS phase results.
  1235. * Args: is_tx Indicates whether to set Tx or RX results
  1236. * Notes:
  1237. * Returns: MV_OK if success, other error code if fail.
  1238. */
  1239. static int ddr3_set_pbs_results(MV_DRAM_INFO *dram_info, int is_tx)
  1240. {
  1241. u32 pup, phys_pup, dq;
  1242. u32 max_pup; /* number of valid pups */
  1243. u32 pbs_min; /* minimal pbs val per pup */
  1244. u32 pbs_max; /* maximum pbs val per pup */
  1245. u32 val[9];
  1246. max_pup = dram_info->num_of_total_pups;
  1247. DEBUG_PBS_FULL_S("DDR3 - PBS - ddr3_set_pbs_results:\n");
  1248. /* Loop for all dqs & pups */
  1249. for (pup = 0; pup < max_pup; pup++) {
  1250. if (pup == (max_pup - 1) && dram_info->ecc_ena)
  1251. phys_pup = ECC_PUP;
  1252. else
  1253. phys_pup = pup;
  1254. /*
  1255. * To minimize delay elements, inc from pbs value the min
  1256. * pbs val
  1257. */
  1258. pbs_min = MAX_PBS;
  1259. pbs_max = 0;
  1260. for (dq = 0; dq < DQ_NUM; dq++) {
  1261. if (pbs_min > skew_array[(pup * DQ_NUM) + dq])
  1262. pbs_min = skew_array[(pup * DQ_NUM) + dq];
  1263. if (pbs_max < skew_array[(pup * DQ_NUM) + dq])
  1264. pbs_max = skew_array[(pup * DQ_NUM) + dq];
  1265. }
  1266. pbs_max -= pbs_min;
  1267. DEBUG_PBS_FULL_S("DDR3 - PBS - PUP");
  1268. DEBUG_PBS_FULL_D(phys_pup, 1);
  1269. DEBUG_PBS_FULL_S(": Min Val = ");
  1270. DEBUG_PBS_FULL_D(pbs_min, 2);
  1271. DEBUG_PBS_FULL_C(", Max Val = ", pbs_max, 2);
  1272. val[pup] = 0;
  1273. for (dq = 0; dq < DQ_NUM; dq++) {
  1274. int idx;
  1275. int offs;
  1276. /* Set skew value for all dq */
  1277. /*
  1278. * Bit# Deskew <- Bit# Deskew - last / first
  1279. * failing bit Deskew For all bits (per PUP)
  1280. * (minimize delay elements)
  1281. */
  1282. DEBUG_PBS_FULL_S("DQ");
  1283. DEBUG_PBS_FULL_D(dq, 1);
  1284. DEBUG_PBS_FULL_S("-");
  1285. DEBUG_PBS_FULL_D((skew_array[(pup * DQ_NUM) + dq] -
  1286. pbs_min), 2);
  1287. DEBUG_PBS_FULL_S(", ");
  1288. idx = (pup * DQ_NUM) + dq;
  1289. if (is_tx == 1)
  1290. offs = PUP_PBS_TX;
  1291. else
  1292. offs = PUP_PBS_RX;
  1293. ddr3_write_pup_reg(offs + pbs_dq_mapping[phys_pup][dq],
  1294. CS0, phys_pup, 0,
  1295. skew_array[idx] - pbs_min);
  1296. if (is_tx == 1)
  1297. val[pup] += skew_array[idx] - pbs_min;
  1298. }
  1299. DEBUG_PBS_FULL_S("\n");
  1300. /* Set the DQS the half of the Max PBS of the DQs */
  1301. if (is_tx == 1) {
  1302. ddr3_write_pup_reg(PUP_PBS_TX + 8, CS0, phys_pup, 0,
  1303. pbs_max / 2);
  1304. ddr3_write_pup_reg(PUP_PBS_TX + 0xa, CS0, phys_pup, 0,
  1305. val[pup] / 8);
  1306. } else
  1307. ddr3_write_pup_reg(PUP_PBS_RX + 8, CS0, phys_pup, 0,
  1308. pbs_max / 2);
  1309. }
  1310. return MV_OK;
  1311. }
  1312. static void ddr3_pbs_write_pup_dqs_reg(u32 cs, u32 pup, u32 dqs_delay)
  1313. {
  1314. u32 reg, delay;
  1315. reg = (ddr3_read_pup_reg(PUP_WL_MODE, cs, pup) & 0x3FF);
  1316. delay = reg & PUP_DELAY_MASK;
  1317. reg |= ((dqs_delay + delay) << REG_PHY_DQS_REF_DLY_OFFS);
  1318. reg |= REG_PHY_REGISTRY_FILE_ACCESS_OP_WR;
  1319. reg |= (pup << REG_PHY_PUP_OFFS);
  1320. reg |= ((0x4 * cs + PUP_WL_MODE) << REG_PHY_CS_OFFS);
  1321. reg_write(REG_PHY_REGISTRY_FILE_ACCESS_ADDR, reg); /* 0x16A0 */
  1322. do {
  1323. reg = reg_read(REG_PHY_REGISTRY_FILE_ACCESS_ADDR) &
  1324. REG_PHY_REGISTRY_FILE_ACCESS_OP_DONE;
  1325. } while (reg); /* Wait for '0' to mark the end of the transaction */
  1326. udelay(10);
  1327. }
  1328. /*
  1329. * Set training patterns
  1330. */
  1331. int ddr3_load_pbs_patterns(MV_DRAM_INFO *dram_info)
  1332. {
  1333. u32 cs, cs_count, cs_tmp;
  1334. u32 sdram_addr;
  1335. u32 *pattern_ptr0, *pattern_ptr1;
  1336. /* Choose pattern */
  1337. switch (dram_info->ddr_width) {
  1338. #if defined(MV88F672X)
  1339. case 16:
  1340. pattern_ptr0 = (u32 *)&pbs_pattern[0];
  1341. pattern_ptr1 = (u32 *)&pbs_pattern[1];
  1342. break;
  1343. #endif
  1344. case 32:
  1345. pattern_ptr0 = (u32 *)&pbs_pattern_32b[0];
  1346. pattern_ptr1 = (u32 *)&pbs_pattern_32b[1];
  1347. break;
  1348. #if defined(MV88F78X60)
  1349. case 64:
  1350. pattern_ptr0 = (u32 *)&pbs_pattern_64b[0];
  1351. pattern_ptr1 = (u32 *)&pbs_pattern_64b[1];
  1352. break;
  1353. #endif
  1354. default:
  1355. return MV_FAIL;
  1356. }
  1357. /* Loop for each CS */
  1358. for (cs = 0; cs < MAX_CS; cs++) {
  1359. if (dram_info->cs_ena & (1 << cs)) {
  1360. cs_count = 0;
  1361. for (cs_tmp = 0; cs_tmp < cs; cs_tmp++) {
  1362. if (dram_info->cs_ena & (1 << cs_tmp))
  1363. cs_count++;
  1364. }
  1365. /* Init PBS I pattern */
  1366. sdram_addr = (cs_count * (SDRAM_CS_SIZE + 1) +
  1367. SDRAM_PBS_I_OFFS);
  1368. if (MV_OK !=
  1369. ddr3_sdram_compare(dram_info, (u32) NULL, NULL,
  1370. pattern_ptr0, LEN_STD_PATTERN,
  1371. sdram_addr, 1, 0, NULL,
  1372. 0))
  1373. return MV_FAIL;
  1374. /* Init PBS II pattern */
  1375. sdram_addr = (cs_count * (SDRAM_CS_SIZE + 1) +
  1376. SDRAM_PBS_II_OFFS);
  1377. if (MV_OK !=
  1378. ddr3_sdram_compare(dram_info, (u32) NULL, NULL,
  1379. pattern_ptr1, LEN_STD_PATTERN,
  1380. sdram_addr, 1, 0, NULL,
  1381. 0))
  1382. return MV_FAIL;
  1383. }
  1384. }
  1385. return MV_OK;
  1386. }
  1387. #endif