ddr3_pbs.c 43 KB

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