4xx_ibm_ddr2_autocalib.c 34 KB

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