4xx_ibm_ddr2_autocalib.c 34 KB

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