4xx_ibm_ddr2_autocalib.c 34 KB

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