4xx_ibm_ddr2_autocalib.c 35 KB

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