4xx_ibm_ddr2_autocalib.c 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249
  1. /*
  2. * cpu/ppc4xx/4xx_ibm_ddr2_autocalib.c
  3. * This SPD SDRAM detection code supports AMCC PPC44x cpu's with a
  4. * DDR2 controller (non Denali Core). Those currently are:
  5. *
  6. * 405: 405EX
  7. * 440/460: 440SP/440SPe/460EX/460GT/460SX
  8. *
  9. * (C) Copyright 2008 Applied Micro Circuits Corporation
  10. * Adam Graham <agraham@amcc.com>
  11. *
  12. * (C) Copyright 2007-2008
  13. * Stefan Roese, DENX Software Engineering, sr@denx.de.
  14. *
  15. * COPYRIGHT AMCC CORPORATION 2004
  16. *
  17. * See file CREDITS for list of people who contributed to this
  18. * project.
  19. *
  20. * This program is free software; you can redistribute it and/or
  21. * modify it under the terms of the GNU General Public License as
  22. * published by the Free Software Foundation; either version 2 of
  23. * the License, or (at your option) any later version.
  24. *
  25. * This program is distributed in the hope that it will be useful,
  26. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  27. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  28. * GNU General Public License for more details.
  29. *
  30. * You should have received a copy of the GNU General Public License
  31. * along with this program; if not, write to the Free Software
  32. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  33. * MA 02111-1307 USA
  34. *
  35. */
  36. /* define DEBUG for debugging output (obviously ;-)) */
  37. #undef DEBUG
  38. #include <common.h>
  39. #include <ppc4xx.h>
  40. #include <asm/io.h>
  41. #include <asm/processor.h>
  42. #if defined(CONFIG_PPC4xx_DDR_AUTOCALIBRATION)
  43. /*
  44. * Only compile the DDR auto-calibration code for NOR boot and
  45. * not for NAND boot (NAND SPL and NAND U-Boot - NUB)
  46. */
  47. #if !defined(CONFIG_NAND_U_BOOT) && !defined(CONFIG_NAND_SPL)
  48. #define MAXBXCF 4
  49. #define SDRAM_RXBAS_SHIFT_1M 20
  50. #if defined(CONFIG_SYS_DECREMENT_PATTERNS)
  51. #define NUMMEMTESTS 24
  52. #else
  53. #define NUMMEMTESTS 8
  54. #endif /* CONFIG_SYS_DECREMENT_PATTERNS */
  55. #define NUMLOOPS 1 /* configure as you deem approporiate */
  56. #define NUMMEMWORDS 16
  57. #define SDRAM_RDCC_RDSS_VAL(n) SDRAM_RDCC_RDSS_DECODE(ddr_rdss_opt(n))
  58. /* Private Structure Definitions */
  59. struct autocal_regs {
  60. u32 rffd;
  61. u32 rqfd;
  62. };
  63. struct ddrautocal {
  64. u32 rffd;
  65. u32 rffd_min;
  66. u32 rffd_max;
  67. u32 rffd_size;
  68. u32 rqfd;
  69. u32 rqfd_size;
  70. u32 rdcc;
  71. u32 flags;
  72. };
  73. struct sdram_timing {
  74. u32 wrdtr;
  75. u32 clktr;
  76. };
  77. struct sdram_timing_clks {
  78. u32 wrdtr;
  79. u32 clktr;
  80. u32 rdcc;
  81. u32 flags;
  82. };
  83. struct autocal_clks {
  84. struct sdram_timing_clks clocks;
  85. struct ddrautocal autocal;
  86. };
  87. /*--------------------------------------------------------------------------+
  88. * Prototypes
  89. *--------------------------------------------------------------------------*/
  90. #if defined(CONFIG_PPC4xx_DDR_METHOD_A)
  91. static u32 DQS_calibration_methodA(struct ddrautocal *);
  92. static u32 program_DQS_calibration_methodA(struct ddrautocal *);
  93. #else
  94. static u32 DQS_calibration_methodB(struct ddrautocal *);
  95. static u32 program_DQS_calibration_methodB(struct ddrautocal *);
  96. #endif
  97. static int short_mem_test(u32 *);
  98. /*
  99. * To provide an interface for board specific config values in this common
  100. * DDR setup code, we implement he "weak" default functions here. They return
  101. * the default value back to the caller.
  102. *
  103. * Please see include/configs/yucca.h for an example fora board specific
  104. * implementation.
  105. */
  106. #if !defined(CONFIG_SPD_EEPROM)
  107. u32 __ddr_wrdtr(u32 default_val)
  108. {
  109. return default_val;
  110. }
  111. u32 ddr_wrdtr(u32) __attribute__((weak, alias("__ddr_wrdtr")));
  112. u32 __ddr_clktr(u32 default_val)
  113. {
  114. return default_val;
  115. }
  116. u32 ddr_clktr(u32) __attribute__((weak, alias("__ddr_clktr")));
  117. /*
  118. * Board-specific Platform code can reimplement spd_ddr_init_hang () if needed
  119. */
  120. void __spd_ddr_init_hang(void)
  121. {
  122. hang();
  123. }
  124. void
  125. spd_ddr_init_hang(void) __attribute__((weak, alias("__spd_ddr_init_hang")));
  126. #endif /* defined(CONFIG_SPD_EEPROM) */
  127. ulong __ddr_scan_option(ulong default_val)
  128. {
  129. return default_val;
  130. }
  131. ulong ddr_scan_option(ulong) __attribute__((weak, alias("__ddr_scan_option")));
  132. u32 __ddr_rdss_opt(u32 default_val)
  133. {
  134. return default_val;
  135. }
  136. u32 ddr_rdss_opt(ulong) __attribute__((weak, alias("__ddr_rdss_opt")));
  137. static u32 *get_membase(int bxcr_num)
  138. {
  139. ulong bxcf;
  140. u32 *membase;
  141. #if defined(SDRAM_R0BAS)
  142. /* BAS from Memory Queue rank reg. */
  143. membase =
  144. (u32 *)(SDRAM_RXBAS_SDBA_DECODE(mfdcr_any(SDRAM_R0BAS+bxcr_num)));
  145. bxcf = 0; /* just to satisfy the compiler */
  146. #else
  147. /* BAS from SDRAM_MBxCF mem rank reg. */
  148. mfsdram(SDRAM_MB0CF + (bxcr_num<<2), bxcf);
  149. membase = (u32 *)((bxcf & 0xfff80000) << 3);
  150. #endif
  151. return membase;
  152. }
  153. static inline void ecc_clear_status_reg(void)
  154. {
  155. mtsdram(SDRAM_ECCCR, 0xffffffff);
  156. #if defined(SDRAM_R0BAS)
  157. mtdcr(SDRAM_ERRSTATLL, 0xffffffff);
  158. #endif
  159. }
  160. /*
  161. * Reset and relock memory DLL after SDRAM_CLKTR change
  162. */
  163. static inline void relock_memory_DLL(void)
  164. {
  165. u32 reg;
  166. mtsdram(SDRAM_MCOPT2, SDRAM_MCOPT2_IPTR_EXECUTE);
  167. do {
  168. mfsdram(SDRAM_MCSTAT, reg);
  169. } while (!(reg & SDRAM_MCSTAT_MIC_COMP));
  170. mfsdram(SDRAM_MCOPT2, reg);
  171. mtsdram(SDRAM_MCOPT2, reg | SDRAM_MCOPT2_DCEN_ENABLE);
  172. }
  173. static int ecc_check_status_reg(void)
  174. {
  175. u32 ecc_status;
  176. /*
  177. * Compare suceeded, now check
  178. * if got ecc error. If got an
  179. * ecc error, then don't count
  180. * this as a passing value
  181. */
  182. mfsdram(SDRAM_ECCCR, ecc_status);
  183. if (ecc_status != 0x00000000) {
  184. /* clear on error */
  185. ecc_clear_status_reg();
  186. /* ecc check failure */
  187. return 0;
  188. }
  189. ecc_clear_status_reg();
  190. sync();
  191. return 1;
  192. }
  193. /* return 1 if passes, 0 if fail */
  194. static int short_mem_test(u32 *base_address)
  195. {
  196. int i, j, l;
  197. u32 ecc_mode = 0;
  198. ulong test[NUMMEMTESTS][NUMMEMWORDS] = {
  199. /* 0 */ {0x00000000, 0x00000000, 0xFFFFFFFF, 0xFFFFFFFF,
  200. 0x00000000, 0x00000000, 0xFFFFFFFF, 0xFFFFFFFF,
  201. 0x00000000, 0x00000000, 0xFFFFFFFF, 0xFFFFFFFF,
  202. 0x00000000, 0x00000000, 0xFFFFFFFF, 0xFFFFFFFF},
  203. /* 1 */ {0xFFFFFFFF, 0xFFFFFFFF, 0x00000000, 0x00000000,
  204. 0xFFFFFFFF, 0xFFFFFFFF, 0x00000000, 0x00000000,
  205. 0xFFFFFFFF, 0xFFFFFFFF, 0x00000000, 0x00000000,
  206. 0xFFFFFFFF, 0xFFFFFFFF, 0x00000000, 0x00000000},
  207. /* 2 */ {0xAAAAAAAA, 0xAAAAAAAA, 0x55555555, 0x55555555,
  208. 0xAAAAAAAA, 0xAAAAAAAA, 0x55555555, 0x55555555,
  209. 0xAAAAAAAA, 0xAAAAAAAA, 0x55555555, 0x55555555,
  210. 0xAAAAAAAA, 0xAAAAAAAA, 0x55555555, 0x55555555},
  211. /* 3 */ {0x55555555, 0x55555555, 0xAAAAAAAA, 0xAAAAAAAA,
  212. 0x55555555, 0x55555555, 0xAAAAAAAA, 0xAAAAAAAA,
  213. 0x55555555, 0x55555555, 0xAAAAAAAA, 0xAAAAAAAA,
  214. 0x55555555, 0x55555555, 0xAAAAAAAA, 0xAAAAAAAA},
  215. /* 4 */ {0xA5A5A5A5, 0xA5A5A5A5, 0x5A5A5A5A, 0x5A5A5A5A,
  216. 0xA5A5A5A5, 0xA5A5A5A5, 0x5A5A5A5A, 0x5A5A5A5A,
  217. 0xA5A5A5A5, 0xA5A5A5A5, 0x5A5A5A5A, 0x5A5A5A5A,
  218. 0xA5A5A5A5, 0xA5A5A5A5, 0x5A5A5A5A, 0x5A5A5A5A},
  219. /* 5 */ {0x5A5A5A5A, 0x5A5A5A5A, 0xA5A5A5A5, 0xA5A5A5A5,
  220. 0x5A5A5A5A, 0x5A5A5A5A, 0xA5A5A5A5, 0xA5A5A5A5,
  221. 0x5A5A5A5A, 0x5A5A5A5A, 0xA5A5A5A5, 0xA5A5A5A5,
  222. 0x5A5A5A5A, 0x5A5A5A5A, 0xA5A5A5A5, 0xA5A5A5A5},
  223. /* 6 */ {0xAA55AA55, 0xAA55AA55, 0x55AA55AA, 0x55AA55AA,
  224. 0xAA55AA55, 0xAA55AA55, 0x55AA55AA, 0x55AA55AA,
  225. 0xAA55AA55, 0xAA55AA55, 0x55AA55AA, 0x55AA55AA,
  226. 0xAA55AA55, 0xAA55AA55, 0x55AA55AA, 0x55AA55AA},
  227. /* 7 */ {0x55AA55AA, 0x55AA55AA, 0xAA55AA55, 0xAA55AA55,
  228. 0x55AA55AA, 0x55AA55AA, 0xAA55AA55, 0xAA55AA55,
  229. 0x55AA55AA, 0x55AA55AA, 0xAA55AA55, 0xAA55AA55,
  230. 0x55AA55AA, 0x55AA55AA, 0xAA55AA55, 0xAA55AA55},
  231. #if defined(CONFIG_SYS_DECREMENT_PATTERNS)
  232. /* 8 */ {0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff,
  233. 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff,
  234. 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff,
  235. 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff},
  236. /* 9 */ {0xfffefffe, 0xfffefffe, 0xfffefffe, 0xfffefffe,
  237. 0xfffefffe, 0xfffefffe, 0xfffefffe, 0xfffefffe,
  238. 0xfffefffe, 0xfffefffe, 0xfffefffe, 0xfffefffe,
  239. 0xfffefffe, 0xfffefffe, 0xfffefffe, 0xfffefffe},
  240. /* 10 */{0xfffdfffd, 0xfffdfffd, 0xfffdffff, 0xfffdfffd,
  241. 0xfffdfffd, 0xfffdfffd, 0xfffdffff, 0xfffdfffd,
  242. 0xfffdfffd, 0xfffdfffd, 0xfffdffff, 0xfffdfffd,
  243. 0xfffdfffd, 0xfffdfffd, 0xfffdffff, 0xfffdfffd},
  244. /* 11 */{0xfffcfffc, 0xfffcfffc, 0xfffcfffc, 0xfffcfffc,
  245. 0xfffcfffc, 0xfffcfffc, 0xfffcfffc, 0xfffcfffc,
  246. 0xfffcfffc, 0xfffcfffc, 0xfffcfffc, 0xfffcfffc,
  247. 0xfffcfffc, 0xfffcfffc, 0xfffcfffc, 0xfffcfffc},
  248. /* 12 */{0xfffbfffb, 0xfffffffb, 0xfffffffb, 0xfffffffb,
  249. 0xfffbfffb, 0xfffffffb, 0xfffffffb, 0xfffffffb,
  250. 0xfffbfffb, 0xfffffffb, 0xfffffffb, 0xfffffffb,
  251. 0xfffbfffb, 0xfffffffb, 0xfffffffb, 0xfffffffb},
  252. /* 13 */{0xfffafffa, 0xfffafffa, 0xfffffffa, 0xfffafffa,
  253. 0xfffafffa, 0xfffafffa, 0xfffafffa, 0xfffafffa,
  254. 0xfffafffa, 0xfffafffa, 0xfffafffa, 0xfffafffa,
  255. 0xfffafffa, 0xfffafffa, 0xfffafffa, 0xfffafffa},
  256. /* 14 */{0xfff9fff9, 0xfff9fff9, 0xfff9fff9, 0xfff9fff9,
  257. 0xfff9fff9, 0xfff9fff9, 0xfff9fff9, 0xfff9fff9,
  258. 0xfff9fff9, 0xfff9fff9, 0xfff9fff9, 0xfff9fff9,
  259. 0xfff9fff9, 0xfff9fff9, 0xfff9fff9, 0xfff9fff9},
  260. /* 15 */{0xfff8fff8, 0xfff8fff8, 0xfff8fff8, 0xfff8fff8,
  261. 0xfff8fff8, 0xfff8fff8, 0xfff8fff8, 0xfff8fff8,
  262. 0xfff8fff8, 0xfff8fff8, 0xfff8fff8, 0xfff8fff8,
  263. 0xfff8fff8, 0xfff8fff8, 0xfff8fff8, 0xfff8fff8},
  264. /* 16 */{0xfff7fff7, 0xfff7ffff, 0xfff7fff7, 0xfff7fff7,
  265. 0xfff7fff7, 0xfff7ffff, 0xfff7fff7, 0xfff7fff7,
  266. 0xfff7fff7, 0xfff7ffff, 0xfff7fff7, 0xfff7fff7,
  267. 0xfff7ffff, 0xfff7ffff, 0xfff7fff7, 0xfff7fff7},
  268. /* 17 */{0xfff6fff5, 0xfff6ffff, 0xfff6fff6, 0xfff6fff7,
  269. 0xfff6fff5, 0xfff6ffff, 0xfff6fff6, 0xfff6fff7,
  270. 0xfff6fff5, 0xfff6ffff, 0xfff6fff6, 0xfff6fff7,
  271. 0xfff6fff5, 0xfff6ffff, 0xfff6fff6, 0xfff6fff7},
  272. /* 18 */{0xfff5fff4, 0xfff5ffff, 0xfff5fff5, 0xfff5fff5,
  273. 0xfff5fff4, 0xfff5ffff, 0xfff5fff5, 0xfff5fff5,
  274. 0xfff5fff4, 0xfff5ffff, 0xfff5fff5, 0xfff5fff5,
  275. 0xfff5fff4, 0xfff5ffff, 0xfff5fff5, 0xfff5fff5},
  276. /* 19 */{0xfff4fff3, 0xfff4ffff, 0xfff4fff4, 0xfff4fff4,
  277. 0xfff4fff3, 0xfff4ffff, 0xfff4fff4, 0xfff4fff4,
  278. 0xfff4fff3, 0xfff4ffff, 0xfff4fff4, 0xfff4fff4,
  279. 0xfff4fff3, 0xfff4ffff, 0xfff4fff4, 0xfff4fff4},
  280. /* 20 */{0xfff3fff2, 0xfff3ffff, 0xfff3fff3, 0xfff3fff3,
  281. 0xfff3fff2, 0xfff3ffff, 0xfff3fff3, 0xfff3fff3,
  282. 0xfff3fff2, 0xfff3ffff, 0xfff3fff3, 0xfff3fff3,
  283. 0xfff3fff2, 0xfff3ffff, 0xfff3fff3, 0xfff3fff3},
  284. /* 21 */{0xfff2ffff, 0xfff2ffff, 0xfff2fff2, 0xfff2fff2,
  285. 0xfff2ffff, 0xfff2ffff, 0xfff2fff2, 0xfff2fff2,
  286. 0xfff2ffff, 0xfff2ffff, 0xfff2fff2, 0xfff2fff2,
  287. 0xfff2ffff, 0xfff2ffff, 0xfff2fff2, 0xfff2fff2},
  288. /* 22 */{0xfff1ffff, 0xfff1ffff, 0xfff1fff1, 0xfff1fff1,
  289. 0xfff1ffff, 0xfff1ffff, 0xfff1fff1, 0xfff1fff1,
  290. 0xfff1ffff, 0xfff1ffff, 0xfff1fff1, 0xfff1fff1,
  291. 0xfff1ffff, 0xfff1ffff, 0xfff1fff1, 0xfff1fff1},
  292. /* 23 */{0xfff0fff0, 0xfff0fff0, 0xfff0fff0, 0xfff0fff0,
  293. 0xfff0fff0, 0xfff0fff0, 0xfff0fff0, 0xfff0fff0,
  294. 0xfff0fff0, 0xfff0fff0, 0xfff0fff0, 0xfff0fff0,
  295. 0xfff0fff0, 0xfff0fffe, 0xfff0fff0, 0xfff0fff0},
  296. #endif /* CONFIG_SYS_DECREMENT_PATTERNS */
  297. };
  298. mfsdram(SDRAM_MCOPT1, ecc_mode);
  299. if ((ecc_mode & SDRAM_MCOPT1_MCHK_CHK_REP) ==
  300. SDRAM_MCOPT1_MCHK_CHK_REP) {
  301. ecc_clear_status_reg();
  302. sync();
  303. ecc_mode = 1;
  304. } else {
  305. ecc_mode = 0;
  306. }
  307. /*
  308. * Run the short memory test.
  309. */
  310. for (i = 0; i < NUMMEMTESTS; i++) {
  311. for (j = 0; j < NUMMEMWORDS; j++) {
  312. base_address[j] = test[i][j];
  313. ppcDcbf((ulong)&(base_address[j]));
  314. }
  315. sync();
  316. iobarrier_rw();
  317. for (l = 0; l < NUMLOOPS; l++) {
  318. for (j = 0; j < NUMMEMWORDS; j++) {
  319. if (base_address[j] != test[i][j]) {
  320. ppcDcbf((u32)&(base_address[j]));
  321. return 0;
  322. } else {
  323. if (ecc_mode) {
  324. if (!ecc_check_status_reg())
  325. return 0;
  326. }
  327. }
  328. ppcDcbf((u32)&(base_address[j]));
  329. } /* for (j = 0; j < NUMMEMWORDS; j++) */
  330. sync();
  331. iobarrier_rw();
  332. } /* for (l=0; l<NUMLOOPS; l++) */
  333. }
  334. return 1;
  335. }
  336. #if defined(CONFIG_PPC4xx_DDR_METHOD_A)
  337. /*-----------------------------------------------------------------------------+
  338. | program_DQS_calibration_methodA.
  339. +-----------------------------------------------------------------------------*/
  340. static u32 program_DQS_calibration_methodA(struct ddrautocal *ddrcal)
  341. {
  342. u32 pass_result = 0;
  343. #ifdef DEBUG
  344. ulong temp;
  345. mfsdram(SDRAM_RDCC, temp);
  346. debug("<%s>SDRAM_RDCC=0x%08x\n", __func__, temp);
  347. #endif
  348. pass_result = DQS_calibration_methodA(ddrcal);
  349. return pass_result;
  350. }
  351. /*
  352. * DQS_calibration_methodA()
  353. *
  354. * Autocalibration Method A
  355. *
  356. * ARRAY [Entire DQS Range] DQS_Valid_Window ; initialized to all zeros
  357. * ARRAY [Entire FDBK Range] FDBK_Valid_Window; initialized to all zeros
  358. * MEMWRITE(addr, expected_data);
  359. * for (i = 0; i < Entire DQS Range; i++) { RQDC.RQFD
  360. * for (j = 0; j < Entire FDBK Range; j++) { RFDC.RFFD
  361. * MEMREAD(addr, actual_data);
  362. * if (actual_data == expected_data) {
  363. * DQS_Valid_Window[i] = 1; RQDC.RQFD
  364. * FDBK_Valid_Window[i][j] = 1; RFDC.RFFD
  365. * }
  366. * }
  367. * }
  368. */
  369. static u32 DQS_calibration_methodA(struct ddrautocal *cal)
  370. {
  371. ulong rfdc_reg;
  372. ulong rffd;
  373. ulong rqdc_reg;
  374. ulong rqfd;
  375. u32 *membase;
  376. ulong bxcf;
  377. int rqfd_average;
  378. int bxcr_num;
  379. int rffd_average;
  380. int pass;
  381. u32 passed = 0;
  382. int in_window;
  383. struct autocal_regs curr_win_min;
  384. struct autocal_regs curr_win_max;
  385. struct autocal_regs best_win_min;
  386. struct autocal_regs best_win_max;
  387. struct autocal_regs loop_win_min;
  388. struct autocal_regs loop_win_max;
  389. #ifdef DEBUG
  390. ulong temp;
  391. #endif
  392. ulong rdcc;
  393. char slash[] = "\\|/-\\|/-";
  394. int loopi = 0;
  395. /* start */
  396. in_window = 0;
  397. memset(&curr_win_min, 0, sizeof(curr_win_min));
  398. memset(&curr_win_max, 0, sizeof(curr_win_max));
  399. memset(&best_win_min, 0, sizeof(best_win_min));
  400. memset(&best_win_max, 0, sizeof(best_win_max));
  401. memset(&loop_win_min, 0, sizeof(loop_win_min));
  402. memset(&loop_win_max, 0, sizeof(loop_win_max));
  403. rdcc = 0;
  404. /*
  405. * Program RDCC register
  406. * Read sample cycle auto-update enable
  407. */
  408. mtsdram(SDRAM_RDCC,
  409. ddr_rdss_opt(SDRAM_RDCC_RDSS_T2) | SDRAM_RDCC_RSAE_ENABLE);
  410. #ifdef DEBUG
  411. mfsdram(SDRAM_RDCC, temp);
  412. debug("<%s>SDRAM_RDCC=0x%x\n", __func__, temp);
  413. mfsdram(SDRAM_RTSR, temp);
  414. debug("<%s>SDRAM_RTSR=0x%x\n", __func__, temp);
  415. mfsdram(SDRAM_FCSR, temp);
  416. debug("<%s>SDRAM_FCSR=0x%x\n", __func__, temp);
  417. #endif
  418. /*
  419. * Program RQDC register
  420. * Internal DQS delay mechanism enable
  421. */
  422. mtsdram(SDRAM_RQDC,
  423. SDRAM_RQDC_RQDE_ENABLE | SDRAM_RQDC_RQFD_ENCODE(0x00));
  424. #ifdef DEBUG
  425. mfsdram(SDRAM_RQDC, temp);
  426. debug("<%s>SDRAM_RQDC=0x%x\n", __func__, temp);
  427. #endif
  428. /*
  429. * Program RFDC register
  430. * Set Feedback Fractional Oversample
  431. * Auto-detect read sample cycle enable
  432. */
  433. mtsdram(SDRAM_RFDC, SDRAM_RFDC_ARSE_ENABLE |
  434. SDRAM_RFDC_RFOS_ENCODE(0) | SDRAM_RFDC_RFFD_ENCODE(0));
  435. #ifdef DEBUG
  436. mfsdram(SDRAM_RFDC, temp);
  437. debug("<%s>SDRAM_RFDC=0x%x\n", __func__, temp);
  438. #endif
  439. putc(' ');
  440. for (rqfd = 0; rqfd <= SDRAM_RQDC_RQFD_MAX; rqfd++) {
  441. mfsdram(SDRAM_RQDC, rqdc_reg);
  442. rqdc_reg &= ~(SDRAM_RQDC_RQFD_MASK);
  443. mtsdram(SDRAM_RQDC, rqdc_reg | SDRAM_RQDC_RQFD_ENCODE(rqfd));
  444. putc('\b');
  445. putc(slash[loopi++ % 8]);
  446. curr_win_min.rffd = 0;
  447. curr_win_max.rffd = 0;
  448. in_window = 0;
  449. for (rffd = 0, pass = 0; rffd <= SDRAM_RFDC_RFFD_MAX; rffd++) {
  450. mfsdram(SDRAM_RFDC, rfdc_reg);
  451. rfdc_reg &= ~(SDRAM_RFDC_RFFD_MASK);
  452. mtsdram(SDRAM_RFDC,
  453. rfdc_reg | SDRAM_RFDC_RFFD_ENCODE(rffd));
  454. for (bxcr_num = 0; bxcr_num < MAXBXCF; bxcr_num++) {
  455. mfsdram(SDRAM_MB0CF + (bxcr_num<<2), bxcf);
  456. /* Banks enabled */
  457. if (bxcf & SDRAM_BXCF_M_BE_MASK) {
  458. /* Bank is enabled */
  459. membase = get_membase(bxcr_num);
  460. pass = short_mem_test(membase);
  461. } /* if bank enabled */
  462. } /* for bxcr_num */
  463. /* If this value passed update RFFD windows */
  464. if (pass && !in_window) { /* at the start of window */
  465. in_window = 1;
  466. curr_win_min.rffd = curr_win_max.rffd = rffd;
  467. curr_win_min.rqfd = curr_win_max.rqfd = rqfd;
  468. mfsdram(SDRAM_RDCC, rdcc); /*record this value*/
  469. } else if (!pass && in_window) { /* at end of window */
  470. in_window = 0;
  471. } else if (pass && in_window) { /* within the window */
  472. curr_win_max.rffd = rffd;
  473. curr_win_max.rqfd = rqfd;
  474. }
  475. /* else if (!pass && !in_window)
  476. skip - no pass, not currently in a window */
  477. if (in_window) {
  478. if ((curr_win_max.rffd - curr_win_min.rffd) >
  479. (best_win_max.rffd - best_win_min.rffd)) {
  480. best_win_min.rffd = curr_win_min.rffd;
  481. best_win_max.rffd = curr_win_max.rffd;
  482. best_win_min.rqfd = curr_win_min.rqfd;
  483. best_win_max.rqfd = curr_win_max.rqfd;
  484. cal->rdcc = rdcc;
  485. }
  486. passed = 1;
  487. }
  488. } /* RFDC.RFFD */
  489. /*
  490. * save-off the best window results of the RFDC.RFFD
  491. * for this RQDC.RQFD setting
  492. */
  493. /*
  494. * if (just ended RFDC.RFDC loop pass window) >
  495. * (prior RFDC.RFFD loop pass window)
  496. */
  497. if ((best_win_max.rffd - best_win_min.rffd) >
  498. (loop_win_max.rffd - loop_win_min.rffd)) {
  499. loop_win_min.rffd = best_win_min.rffd;
  500. loop_win_max.rffd = best_win_max.rffd;
  501. loop_win_min.rqfd = rqfd;
  502. loop_win_max.rqfd = rqfd;
  503. debug("RQFD.min 0x%08x, RQFD.max 0x%08x, "
  504. "RFFD.min 0x%08x, RFFD.max 0x%08x\n",
  505. loop_win_min.rqfd, loop_win_max.rqfd,
  506. loop_win_min.rffd, loop_win_max.rffd);
  507. }
  508. } /* RQDC.RQFD */
  509. putc('\b');
  510. debug("\n");
  511. if ((loop_win_min.rffd == 0) && (loop_win_max.rffd == 0) &&
  512. (best_win_min.rffd == 0) && (best_win_max.rffd == 0) &&
  513. (best_win_min.rqfd == 0) && (best_win_max.rqfd == 0)) {
  514. passed = 0;
  515. }
  516. /*
  517. * Need to program RQDC before RFDC.
  518. */
  519. debug("<%s> RQFD Min: 0x%x\n", __func__, loop_win_min.rqfd);
  520. debug("<%s> RQFD Max: 0x%x\n", __func__, loop_win_max.rqfd);
  521. rqfd_average = loop_win_max.rqfd;
  522. if (rqfd_average < 0)
  523. rqfd_average = 0;
  524. if (rqfd_average > SDRAM_RQDC_RQFD_MAX)
  525. rqfd_average = SDRAM_RQDC_RQFD_MAX;
  526. debug("<%s> RFFD average: 0x%08x\n", __func__, rqfd_average);
  527. mtsdram(SDRAM_RQDC, (rqdc_reg & ~SDRAM_RQDC_RQFD_MASK) |
  528. SDRAM_RQDC_RQFD_ENCODE(rqfd_average));
  529. debug("<%s> RFFD Min: 0x%08x\n", __func__, loop_win_min.rffd);
  530. debug("<%s> RFFD Max: 0x%08x\n", __func__, loop_win_max.rffd);
  531. rffd_average = ((loop_win_min.rffd + loop_win_max.rffd) / 2);
  532. if (rffd_average < 0)
  533. rffd_average = 0;
  534. if (rffd_average > SDRAM_RFDC_RFFD_MAX)
  535. rffd_average = SDRAM_RFDC_RFFD_MAX;
  536. debug("<%s> RFFD average: 0x%08x\n", __func__, rffd_average);
  537. mtsdram(SDRAM_RFDC, rfdc_reg | SDRAM_RFDC_RFFD_ENCODE(rffd_average));
  538. /* if something passed, then return the size of the largest window */
  539. if (passed != 0) {
  540. passed = loop_win_max.rffd - loop_win_min.rffd;
  541. cal->rqfd = rqfd_average;
  542. cal->rffd = rffd_average;
  543. cal->rffd_min = loop_win_min.rffd;
  544. cal->rffd_max = loop_win_max.rffd;
  545. }
  546. return (u32)passed;
  547. }
  548. #else /* !defined(CONFIG_PPC4xx_DDR_METHOD_A) */
  549. /*-----------------------------------------------------------------------------+
  550. | program_DQS_calibration_methodB.
  551. +-----------------------------------------------------------------------------*/
  552. static u32 program_DQS_calibration_methodB(struct ddrautocal *ddrcal)
  553. {
  554. u32 pass_result = 0;
  555. #ifdef DEBUG
  556. ulong temp;
  557. #endif
  558. /*
  559. * Program RDCC register
  560. * Read sample cycle auto-update enable
  561. */
  562. mtsdram(SDRAM_RDCC,
  563. ddr_rdss_opt(SDRAM_RDCC_RDSS_T2) | SDRAM_RDCC_RSAE_ENABLE);
  564. #ifdef DEBUG
  565. mfsdram(SDRAM_RDCC, temp);
  566. debug("<%s>SDRAM_RDCC=0x%08x\n", __func__, temp);
  567. #endif
  568. /*
  569. * Program RQDC register
  570. * Internal DQS delay mechanism enable
  571. */
  572. mtsdram(SDRAM_RQDC,
  573. #if defined(CONFIG_DDR_RQDC_START_VAL)
  574. SDRAM_RQDC_RQDE_ENABLE |
  575. SDRAM_RQDC_RQFD_ENCODE(CONFIG_DDR_RQDC_START_VAL));
  576. #else
  577. SDRAM_RQDC_RQDE_ENABLE | SDRAM_RQDC_RQFD_ENCODE(0x38));
  578. #endif
  579. #ifdef DEBUG
  580. mfsdram(SDRAM_RQDC, temp);
  581. debug("<%s>SDRAM_RQDC=0x%08x\n", __func__, temp);
  582. #endif
  583. /*
  584. * Program RFDC register
  585. * Set Feedback Fractional Oversample
  586. * Auto-detect read sample cycle enable
  587. */
  588. mtsdram(SDRAM_RFDC, SDRAM_RFDC_ARSE_ENABLE |
  589. SDRAM_RFDC_RFOS_ENCODE(0) |
  590. SDRAM_RFDC_RFFD_ENCODE(0));
  591. #ifdef DEBUG
  592. mfsdram(SDRAM_RFDC, temp);
  593. debug("<%s>SDRAM_RFDC=0x%08x\n", __func__, temp);
  594. #endif
  595. pass_result = DQS_calibration_methodB(ddrcal);
  596. return pass_result;
  597. }
  598. /*
  599. * DQS_calibration_methodB()
  600. *
  601. * Autocalibration Method B
  602. *
  603. * ARRAY [Entire DQS Range] DQS_Valid_Window ; initialized to all zeros
  604. * ARRAY [Entire Feedback Range] FDBK_Valid_Window; initialized to all zeros
  605. * MEMWRITE(addr, expected_data);
  606. * Initialialize the DQS delay to 80 degrees (MCIF0_RRQDC[RQFD]=0x38).
  607. *
  608. * for (j = 0; j < Entire Feedback Range; j++) {
  609. * MEMREAD(addr, actual_data);
  610. * if (actual_data == expected_data) {
  611. * FDBK_Valid_Window[j] = 1;
  612. * }
  613. * }
  614. *
  615. * Set MCIF0_RFDC[RFFD] to the middle of the FDBK_Valid_Window.
  616. *
  617. * for (i = 0; i < Entire DQS Range; i++) {
  618. * MEMREAD(addr, actual_data);
  619. * if (actual_data == expected_data) {
  620. * DQS_Valid_Window[i] = 1;
  621. * }
  622. * }
  623. *
  624. * Set MCIF0_RRQDC[RQFD] to the middle of the DQS_Valid_Window.
  625. */
  626. /*-----------------------------------------------------------------------------+
  627. | DQS_calibration_methodB.
  628. +-----------------------------------------------------------------------------*/
  629. static u32 DQS_calibration_methodB(struct ddrautocal *cal)
  630. {
  631. ulong rfdc_reg;
  632. ulong rffd;
  633. ulong rqdc_reg;
  634. ulong rqfd;
  635. ulong rdcc;
  636. u32 *membase;
  637. ulong bxcf;
  638. int rqfd_average;
  639. int bxcr_num;
  640. int rffd_average;
  641. int pass;
  642. uint passed = 0;
  643. int in_window;
  644. u32 curr_win_min, curr_win_max;
  645. u32 best_win_min, best_win_max;
  646. u32 size = 0;
  647. /*------------------------------------------------------------------
  648. | Test to determine the best read clock delay tuning bits.
  649. |
  650. | Before the DDR controller can be used, the read clock delay needs to
  651. | be set. This is SDRAM_RQDC[RQFD] and SDRAM_RFDC[RFFD].
  652. | This value cannot be hardcoded into the program because it changes
  653. | depending on the board's setup and environment.
  654. | To do this, all delay values are tested to see if they
  655. | work or not. By doing this, you get groups of fails with groups of
  656. | passing values. The idea is to find the start and end of a passing
  657. | window and take the center of it to use as the read clock delay.
  658. |
  659. | A failure has to be seen first so that when we hit a pass, we know
  660. | that it is truely the start of the window. If we get passing values
  661. | to start off with, we don't know if we are at the start of the window
  662. |
  663. | The code assumes that a failure will always be found.
  664. | If a failure is not found, there is no easy way to get the middle
  665. | of the passing window. I guess we can pretty much pick any value
  666. | but some values will be better than others. Since the lowest speed
  667. | we can clock the DDR interface at is 200 MHz (2x 100 MHz PLB speed),
  668. | from experimentation it is safe to say you will always have a failure
  669. +-----------------------------------------------------------------*/
  670. debug("\n\n");
  671. in_window = 0;
  672. rdcc = 0;
  673. curr_win_min = curr_win_max = 0;
  674. best_win_min = best_win_max = 0;
  675. for (rffd = 0; rffd <= SDRAM_RFDC_RFFD_MAX; rffd++) {
  676. mfsdram(SDRAM_RFDC, rfdc_reg);
  677. rfdc_reg &= ~(SDRAM_RFDC_RFFD_MASK);
  678. mtsdram(SDRAM_RFDC, rfdc_reg | SDRAM_RFDC_RFFD_ENCODE(rffd));
  679. pass = 1;
  680. for (bxcr_num = 0; bxcr_num < MAXBXCF; bxcr_num++) {
  681. mfsdram(SDRAM_MB0CF + (bxcr_num<<2), bxcf);
  682. /* Banks enabled */
  683. if (bxcf & SDRAM_BXCF_M_BE_MASK) {
  684. /* Bank is enabled */
  685. membase = get_membase(bxcr_num);
  686. pass &= short_mem_test(membase);
  687. } /* if bank enabled */
  688. } /* for bxcf_num */
  689. /* If this value passed */
  690. if (pass && !in_window) { /* start of passing window */
  691. in_window = 1;
  692. curr_win_min = curr_win_max = rffd;
  693. mfsdram(SDRAM_RDCC, rdcc); /* record this value */
  694. } else if (!pass && in_window) { /* end passing window */
  695. in_window = 0;
  696. } else if (pass && in_window) { /* within the passing window */
  697. curr_win_max = rffd;
  698. }
  699. if (in_window) {
  700. if ((curr_win_max - curr_win_min) >
  701. (best_win_max - best_win_min)) {
  702. best_win_min = curr_win_min;
  703. best_win_max = curr_win_max;
  704. cal->rdcc = rdcc;
  705. }
  706. passed = 1;
  707. }
  708. } /* for rffd */
  709. if ((best_win_min == 0) && (best_win_max == 0))
  710. passed = 0;
  711. else
  712. size = best_win_max - best_win_min;
  713. debug("RFFD Min: 0x%x\n", best_win_min);
  714. debug("RFFD Max: 0x%x\n", best_win_max);
  715. rffd_average = ((best_win_min + best_win_max) / 2);
  716. cal->rffd_min = best_win_min;
  717. cal->rffd_max = best_win_max;
  718. if (rffd_average < 0)
  719. rffd_average = 0;
  720. if (rffd_average > SDRAM_RFDC_RFFD_MAX)
  721. rffd_average = SDRAM_RFDC_RFFD_MAX;
  722. mtsdram(SDRAM_RFDC, rfdc_reg | SDRAM_RFDC_RFFD_ENCODE(rffd_average));
  723. rffd = rffd_average;
  724. in_window = 0;
  725. curr_win_min = curr_win_max = 0;
  726. best_win_min = best_win_max = 0;
  727. for (rqfd = 0; rqfd <= SDRAM_RQDC_RQFD_MAX; rqfd++) {
  728. mfsdram(SDRAM_RQDC, rqdc_reg);
  729. rqdc_reg &= ~(SDRAM_RQDC_RQFD_MASK);
  730. mtsdram(SDRAM_RQDC, rqdc_reg | SDRAM_RQDC_RQFD_ENCODE(rqfd));
  731. pass = 1;
  732. for (bxcr_num = 0; bxcr_num < MAXBXCF; bxcr_num++) {
  733. mfsdram(SDRAM_MB0CF + (bxcr_num<<2), bxcf);
  734. /* Banks enabled */
  735. if (bxcf & SDRAM_BXCF_M_BE_MASK) {
  736. /* Bank is enabled */
  737. membase = get_membase(bxcr_num);
  738. pass &= short_mem_test(membase);
  739. } /* if bank enabled */
  740. } /* for bxcf_num */
  741. /* If this value passed */
  742. if (pass && !in_window) {
  743. in_window = 1;
  744. curr_win_min = curr_win_max = rqfd;
  745. } else if (!pass && in_window) {
  746. in_window = 0;
  747. } else if (pass && in_window) {
  748. curr_win_max = rqfd;
  749. }
  750. if (in_window) {
  751. if ((curr_win_max - curr_win_min) >
  752. (best_win_max - best_win_min)) {
  753. best_win_min = curr_win_min;
  754. best_win_max = curr_win_max;
  755. }
  756. passed = 1;
  757. }
  758. } /* for rqfd */
  759. if ((best_win_min == 0) && (best_win_max == 0))
  760. passed = 0;
  761. debug("RQFD Min: 0x%x\n", best_win_min);
  762. debug("RQFD Max: 0x%x\n", best_win_max);
  763. rqfd_average = ((best_win_min + best_win_max) / 2);
  764. if (rqfd_average < 0)
  765. rqfd_average = 0;
  766. if (rqfd_average > SDRAM_RQDC_RQFD_MAX)
  767. rqfd_average = SDRAM_RQDC_RQFD_MAX;
  768. mtsdram(SDRAM_RQDC, (rqdc_reg & ~SDRAM_RQDC_RQFD_MASK) |
  769. SDRAM_RQDC_RQFD_ENCODE(rqfd_average));
  770. mfsdram(SDRAM_RQDC, rqdc_reg);
  771. mfsdram(SDRAM_RFDC, rfdc_reg);
  772. /*
  773. * Need to program RQDC before RFDC. The value is read above.
  774. * That is the reason why auto cal not work.
  775. * See, comments below.
  776. */
  777. mtsdram(SDRAM_RQDC, rqdc_reg);
  778. mtsdram(SDRAM_RFDC, rfdc_reg);
  779. debug("RQDC: 0x%08X\n", rqdc_reg);
  780. debug("RFDC: 0x%08X\n", rfdc_reg);
  781. /* if something passed, then return the size of the largest window */
  782. if (passed != 0) {
  783. passed = size;
  784. cal->rqfd = rqfd_average;
  785. cal->rffd = rffd_average;
  786. }
  787. return (uint)passed;
  788. }
  789. #endif /* defined(CONFIG_PPC4xx_DDR_METHOD_A) */
  790. /*
  791. * Default table for DDR auto-calibration of all
  792. * possible WRDTR and CLKTR values.
  793. * Table format is:
  794. * {SDRAM_WRDTR.[WDTR], SDRAM_CLKTR.[CKTR]}
  795. *
  796. * Table is terminated with {-1, -1} value pair.
  797. *
  798. * Board vendors can specify their own board specific subset of
  799. * known working {SDRAM_WRDTR.[WDTR], SDRAM_CLKTR.[CKTR]} value
  800. * pairs via a board defined ddr_scan_option() function.
  801. */
  802. struct sdram_timing full_scan_options[] = {
  803. {0, 0}, {0, 1}, {0, 2}, {0, 3},
  804. {1, 0}, {1, 1}, {1, 2}, {1, 3},
  805. {2, 0}, {2, 1}, {2, 2}, {2, 3},
  806. {3, 0}, {3, 1}, {3, 2}, {3, 3},
  807. {4, 0}, {4, 1}, {4, 2}, {4, 3},
  808. {5, 0}, {5, 1}, {5, 2}, {5, 3},
  809. {6, 0}, {6, 1}, {6, 2}, {6, 3},
  810. {-1, -1}
  811. };
  812. /*---------------------------------------------------------------------------+
  813. | DQS_calibration.
  814. +----------------------------------------------------------------------------*/
  815. u32 DQS_autocalibration(void)
  816. {
  817. u32 wdtr;
  818. u32 clkp;
  819. u32 result = 0;
  820. u32 best_result = 0;
  821. u32 best_rdcc;
  822. struct ddrautocal ddrcal;
  823. struct autocal_clks tcal;
  824. ulong rfdc_reg;
  825. ulong rqdc_reg;
  826. u32 val;
  827. int verbose_lvl = 0;
  828. char *str;
  829. char slash[] = "\\|/-\\|/-";
  830. int loopi = 0;
  831. struct sdram_timing *scan_list;
  832. #if defined(DEBUG_PPC4xx_DDR_AUTOCALIBRATION)
  833. int i;
  834. char tmp[64]; /* long enough for environment variables */
  835. #endif
  836. memset(&tcal, 0, sizeof(tcal));
  837. ddr_scan_option((ulong)full_scan_options);
  838. scan_list =
  839. (struct sdram_timing *)ddr_scan_option((ulong)full_scan_options);
  840. mfsdram(SDRAM_MCOPT1, val);
  841. if ((val & SDRAM_MCOPT1_MCHK_CHK_REP) == SDRAM_MCOPT1_MCHK_CHK_REP)
  842. str = "ECC Auto calibration -";
  843. else
  844. str = "Auto calibration -";
  845. puts(str);
  846. #if defined(DEBUG_PPC4xx_DDR_AUTOCALIBRATION)
  847. i = getenv_r("autocalib", tmp, sizeof(tmp));
  848. if (i < 0)
  849. strcpy(tmp, CONFIG_AUTOCALIB);
  850. if (strcmp(tmp, "final") == 0) {
  851. /* display the final autocalibration results only */
  852. verbose_lvl = 1;
  853. } else if (strcmp(tmp, "loop") == 0) {
  854. /* display summary autocalibration info per iteration */
  855. verbose_lvl = 2;
  856. } else if (strcmp(tmp, "display") == 0) {
  857. /* display full debug autocalibration window info. */
  858. verbose_lvl = 3;
  859. }
  860. #endif /* (DEBUG_PPC4xx_DDR_AUTOCALIBRATION) */
  861. best_rdcc = (SDRAM_RDCC_RDSS_T4 >> 30);
  862. while ((scan_list->wrdtr != -1) && (scan_list->clktr != -1)) {
  863. wdtr = scan_list->wrdtr;
  864. clkp = scan_list->clktr;
  865. mfsdram(SDRAM_WRDTR, val);
  866. val &= ~(SDRAM_WRDTR_LLWP_MASK | SDRAM_WRDTR_WTR_MASK);
  867. mtsdram(SDRAM_WRDTR, (val |
  868. ddr_wrdtr(SDRAM_WRDTR_LLWP_1_CYC | (wdtr << 25))));
  869. mtsdram(SDRAM_CLKTR, clkp << 30);
  870. relock_memory_DLL();
  871. putc('\b');
  872. putc(slash[loopi++ % 8]);
  873. #ifdef DEBUG
  874. debug("\n");
  875. debug("*** --------------\n");
  876. mfsdram(SDRAM_WRDTR, val);
  877. debug("*** SDRAM_WRDTR set to 0x%08x\n", val);
  878. mfsdram(SDRAM_CLKTR, val);
  879. debug("*** SDRAM_CLKTR set to 0x%08x\n", val);
  880. #endif
  881. debug("\n");
  882. if (verbose_lvl > 2) {
  883. printf("*** SDRAM_WRDTR (wdtr) set to %d\n", wdtr);
  884. printf("*** SDRAM_CLKTR (clkp) set to %d\n", clkp);
  885. }
  886. memset(&ddrcal, 0, sizeof(ddrcal));
  887. /*
  888. * DQS calibration.
  889. */
  890. /*
  891. * program_DQS_calibration_method[A|B]() returns 0 if no
  892. * passing RFDC.[RFFD] window is found or returns the size
  893. * of the best passing window; in the case of a found passing
  894. * window, the ddrcal will contain the values of the best
  895. * window RQDC.[RQFD] and RFDC.[RFFD].
  896. */
  897. /*
  898. * Call PPC4xx SDRAM DDR autocalibration methodA or methodB.
  899. * Default is methodB.
  900. * Defined the autocalibration method in the board specific
  901. * header file.
  902. * Please see include/configs/kilauea.h for an example for
  903. * a board specific implementation.
  904. */
  905. #if defined(CONFIG_PPC4xx_DDR_METHOD_A)
  906. result = program_DQS_calibration_methodA(&ddrcal);
  907. #else
  908. result = program_DQS_calibration_methodB(&ddrcal);
  909. #endif
  910. sync();
  911. /*
  912. * Clear potential errors resulting from auto-calibration.
  913. * If not done, then we could get an interrupt later on when
  914. * exceptions are enabled.
  915. */
  916. set_mcsr(get_mcsr());
  917. val = ddrcal.rdcc; /* RDCC from the best passing window */
  918. udelay(100);
  919. if (verbose_lvl > 1) {
  920. char *tstr;
  921. switch ((val >> 30)) {
  922. case 0:
  923. if (result != 0)
  924. tstr = "T1";
  925. else
  926. tstr = "N/A";
  927. break;
  928. case 1:
  929. tstr = "T2";
  930. break;
  931. case 2:
  932. tstr = "T3";
  933. break;
  934. case 3:
  935. tstr = "T4";
  936. break;
  937. default:
  938. tstr = "unknown";
  939. break;
  940. }
  941. printf("** WRDTR(%d) CLKTR(%d), Wind (%d), best (%d), "
  942. "max-min(0x%04x)(0x%04x), RDCC: %s\n",
  943. wdtr, clkp, result, best_result,
  944. ddrcal.rffd_min, ddrcal.rffd_max, tstr);
  945. }
  946. /*
  947. * The DQS calibration "result" is either "0"
  948. * if no passing window was found, or is the
  949. * size of the RFFD passing window.
  950. */
  951. /*
  952. * want the lowest Read Sample Cycle Select
  953. */
  954. val = SDRAM_RDCC_RDSS_DECODE(val);
  955. debug("*** (%d) (%d) current_rdcc, best_rdcc\n",
  956. val, best_rdcc);
  957. if ((result != 0) &&
  958. (val >= SDRAM_RDCC_RDSS_VAL(SDRAM_RDCC_RDSS_T2))) {
  959. if (((result == best_result) && (val < best_rdcc)) ||
  960. ((result > best_result) && (val <= best_rdcc))) {
  961. tcal.autocal.flags = 1;
  962. debug("*** (%d)(%d) result passed window "
  963. "size: 0x%08x, rqfd = 0x%08x, "
  964. "rffd = 0x%08x, rdcc = 0x%08x\n",
  965. wdtr, clkp, result, ddrcal.rqfd,
  966. ddrcal.rffd, ddrcal.rdcc);
  967. /*
  968. * Save the SDRAM_WRDTR and SDRAM_CLKTR
  969. * settings for the largest returned
  970. * RFFD passing window size.
  971. */
  972. best_rdcc = val;
  973. tcal.clocks.wrdtr = wdtr;
  974. tcal.clocks.clktr = clkp;
  975. tcal.clocks.rdcc = SDRAM_RDCC_RDSS_ENCODE(val);
  976. tcal.autocal.rqfd = ddrcal.rqfd;
  977. tcal.autocal.rffd = ddrcal.rffd;
  978. best_result = result;
  979. if (verbose_lvl > 2) {
  980. printf("** (%d)(%d) "
  981. "best result: 0x%04x\n",
  982. wdtr, clkp,
  983. best_result);
  984. printf("** (%d)(%d) "
  985. "best WRDTR: 0x%04x\n",
  986. wdtr, clkp,
  987. tcal.clocks.wrdtr);
  988. printf("** (%d)(%d) "
  989. "best CLKTR: 0x%04x\n",
  990. wdtr, clkp,
  991. tcal.clocks.clktr);
  992. printf("** (%d)(%d) "
  993. "best RQDC: 0x%04x\n",
  994. wdtr, clkp,
  995. tcal.autocal.rqfd);
  996. printf("** (%d)(%d) "
  997. "best RFDC: 0x%04x\n",
  998. wdtr, clkp,
  999. tcal.autocal.rffd);
  1000. printf("** (%d)(%d) "
  1001. "best RDCC: 0x%08x\n",
  1002. wdtr, clkp,
  1003. (u32)tcal.clocks.rdcc);
  1004. mfsdram(SDRAM_RTSR, val);
  1005. printf("** (%d)(%d) best "
  1006. "loop RTSR: 0x%08x\n",
  1007. wdtr, clkp, val);
  1008. mfsdram(SDRAM_FCSR, val);
  1009. printf("** (%d)(%d) best "
  1010. "loop FCSR: 0x%08x\n",
  1011. wdtr, clkp, val);
  1012. }
  1013. }
  1014. } /* if ((result != 0) && (val >= (ddr_rdss_opt()))) */
  1015. scan_list++;
  1016. } /* while ((scan_list->wrdtr != -1) && (scan_list->clktr != -1)) */
  1017. if (tcal.autocal.flags == 1) {
  1018. if (verbose_lvl > 0) {
  1019. printf("*** --------------\n");
  1020. printf("*** best_result window size: %d\n",
  1021. best_result);
  1022. printf("*** best_result WRDTR: 0x%04x\n",
  1023. tcal.clocks.wrdtr);
  1024. printf("*** best_result CLKTR: 0x%04x\n",
  1025. tcal.clocks.clktr);
  1026. printf("*** best_result RQFD: 0x%04x\n",
  1027. tcal.autocal.rqfd);
  1028. printf("*** best_result RFFD: 0x%04x\n",
  1029. tcal.autocal.rffd);
  1030. printf("*** best_result RDCC: 0x%04x\n",
  1031. tcal.clocks.rdcc);
  1032. printf("*** --------------\n");
  1033. printf("\n");
  1034. }
  1035. /*
  1036. * if got best passing result window, then lock in the
  1037. * best CLKTR, WRDTR, RQFD, and RFFD values
  1038. */
  1039. mfsdram(SDRAM_WRDTR, val);
  1040. mtsdram(SDRAM_WRDTR, (val &
  1041. ~(SDRAM_WRDTR_LLWP_MASK | SDRAM_WRDTR_WTR_MASK)) |
  1042. ddr_wrdtr(SDRAM_WRDTR_LLWP_1_CYC |
  1043. (tcal.clocks.wrdtr << 25)));
  1044. mtsdram(SDRAM_CLKTR, tcal.clocks.clktr << 30);
  1045. relock_memory_DLL();
  1046. mfsdram(SDRAM_RQDC, rqdc_reg);
  1047. rqdc_reg &= ~(SDRAM_RQDC_RQFD_MASK);
  1048. mtsdram(SDRAM_RQDC, rqdc_reg |
  1049. SDRAM_RQDC_RQFD_ENCODE(tcal.autocal.rqfd));
  1050. mfsdram(SDRAM_RQDC, rqdc_reg);
  1051. debug("*** best_result: read value SDRAM_RQDC 0x%08x\n",
  1052. rqdc_reg);
  1053. mfsdram(SDRAM_RFDC, rfdc_reg);
  1054. rfdc_reg &= ~(SDRAM_RFDC_RFFD_MASK);
  1055. mtsdram(SDRAM_RFDC, rfdc_reg |
  1056. SDRAM_RFDC_RFFD_ENCODE(tcal.autocal.rffd));
  1057. mfsdram(SDRAM_RFDC, rfdc_reg);
  1058. debug("*** best_result: read value SDRAM_RFDC 0x%08x\n",
  1059. rfdc_reg);
  1060. mfsdram(SDRAM_RDCC, val);
  1061. debug("*** SDRAM_RDCC 0x%08x\n", val);
  1062. } else {
  1063. /*
  1064. * no valid windows were found
  1065. */
  1066. printf("DQS memory calibration window can not be determined, "
  1067. "terminating u-boot.\n");
  1068. ppc4xx_ibm_ddr2_register_dump();
  1069. spd_ddr_init_hang();
  1070. }
  1071. blank_string(strlen(str));
  1072. return 0;
  1073. }
  1074. #else /* defined(CONFIG_NAND_U_BOOT) || defined(CONFIG_NAND_SPL) */
  1075. u32 DQS_autocalibration(void)
  1076. {
  1077. return 0;
  1078. }
  1079. #endif /* !defined(CONFIG_NAND_U_BOOT) && !defined(CONFIG_NAND_SPL) */
  1080. #endif /* defined(CONFIG_PPC4xx_DDR_AUTOCALIBRATION) */