4xx_ibm_ddr2_autocalib.c 35 KB

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