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 <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. u32 *membase;
  137. #if defined(SDRAM_R0BAS)
  138. /* BAS from Memory Queue rank reg. */
  139. membase =
  140. (u32 *)(SDRAM_RXBAS_SDBA_DECODE(mfdcr_any(SDRAM_R0BAS+bxcr_num)));
  141. #else
  142. {
  143. ulong bxcf;
  144. /* BAS from SDRAM_MBxCF mem rank reg. */
  145. mfsdram(SDRAM_MB0CF + (bxcr_num<<2), bxcf);
  146. membase = (u32 *)((bxcf & 0xfff80000) << 3);
  147. }
  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. #ifndef CONFIG_DDR_RFDC_FIXED
  631. ulong rffd;
  632. #endif
  633. ulong rqdc_reg;
  634. ulong rqfd;
  635. ulong rdcc;
  636. u32 *membase;
  637. ulong bxcf;
  638. int rqfd_average;
  639. int bxcr_num;
  640. int rffd_average;
  641. int pass;
  642. uint passed = 0;
  643. int in_window;
  644. u32 curr_win_min, curr_win_max;
  645. u32 best_win_min, best_win_max;
  646. u32 size = 0;
  647. /*------------------------------------------------------------------
  648. | Test to determine the best read clock delay tuning bits.
  649. |
  650. | Before the DDR controller can be used, the read clock delay needs to
  651. | be set. This is SDRAM_RQDC[RQFD] and SDRAM_RFDC[RFFD].
  652. | This value cannot be hardcoded into the program because it changes
  653. | depending on the board's setup and environment.
  654. | To do this, all delay values are tested to see if they
  655. | work or not. By doing this, you get groups of fails with groups of
  656. | passing values. The idea is to find the start and end of a passing
  657. | window and take the center of it to use as the read clock delay.
  658. |
  659. | A failure has to be seen first so that when we hit a pass, we know
  660. | that it is truely the start of the window. If we get passing values
  661. | to start off with, we don't know if we are at the start of the window
  662. |
  663. | The code assumes that a failure will always be found.
  664. | If a failure is not found, there is no easy way to get the middle
  665. | of the passing window. I guess we can pretty much pick any value
  666. | but some values will be better than others. Since the lowest speed
  667. | we can clock the DDR interface at is 200 MHz (2x 100 MHz PLB speed),
  668. | from experimentation it is safe to say you will always have a failure
  669. +-----------------------------------------------------------------*/
  670. debug("\n\n");
  671. #if defined(CONFIG_DDR_RFDC_FIXED)
  672. mtsdram(SDRAM_RFDC, CONFIG_DDR_RFDC_FIXED);
  673. size = 512;
  674. rffd_average = CONFIG_DDR_RFDC_FIXED & SDRAM_RFDC_RFFD_MASK;
  675. mfsdram(SDRAM_RDCC, rdcc); /* record this value */
  676. cal->rdcc = rdcc;
  677. #else /* CONFIG_DDR_RFDC_FIXED */
  678. in_window = 0;
  679. rdcc = 0;
  680. curr_win_min = curr_win_max = 0;
  681. best_win_min = best_win_max = 0;
  682. for (rffd = 0; rffd <= SDRAM_RFDC_RFFD_MAX; rffd++) {
  683. mfsdram(SDRAM_RFDC, rfdc_reg);
  684. rfdc_reg &= ~(SDRAM_RFDC_RFFD_MASK);
  685. mtsdram(SDRAM_RFDC, rfdc_reg | SDRAM_RFDC_RFFD_ENCODE(rffd));
  686. pass = 1;
  687. for (bxcr_num = 0; bxcr_num < MAXBXCF; bxcr_num++) {
  688. mfsdram(SDRAM_MB0CF + (bxcr_num<<2), bxcf);
  689. /* Banks enabled */
  690. if (bxcf & SDRAM_BXCF_M_BE_MASK) {
  691. /* Bank is enabled */
  692. membase = get_membase(bxcr_num);
  693. pass &= short_mem_test(membase);
  694. } /* if bank enabled */
  695. } /* for bxcf_num */
  696. /* If this value passed */
  697. if (pass && !in_window) { /* start of passing window */
  698. in_window = 1;
  699. curr_win_min = curr_win_max = rffd;
  700. mfsdram(SDRAM_RDCC, rdcc); /* record this value */
  701. } else if (!pass && in_window) { /* end passing window */
  702. in_window = 0;
  703. } else if (pass && in_window) { /* within the passing window */
  704. curr_win_max = rffd;
  705. }
  706. if (in_window) {
  707. if ((curr_win_max - curr_win_min) >
  708. (best_win_max - best_win_min)) {
  709. best_win_min = curr_win_min;
  710. best_win_max = curr_win_max;
  711. cal->rdcc = rdcc;
  712. }
  713. passed = 1;
  714. }
  715. } /* for rffd */
  716. if ((best_win_min == 0) && (best_win_max == 0))
  717. passed = 0;
  718. else
  719. size = best_win_max - best_win_min;
  720. debug("RFFD Min: 0x%x\n", best_win_min);
  721. debug("RFFD Max: 0x%x\n", best_win_max);
  722. rffd_average = ((best_win_min + best_win_max) / 2);
  723. cal->rffd_min = best_win_min;
  724. cal->rffd_max = best_win_max;
  725. if (rffd_average < 0)
  726. rffd_average = 0;
  727. if (rffd_average > SDRAM_RFDC_RFFD_MAX)
  728. rffd_average = SDRAM_RFDC_RFFD_MAX;
  729. mtsdram(SDRAM_RFDC, rfdc_reg | SDRAM_RFDC_RFFD_ENCODE(rffd_average));
  730. #endif /* CONFIG_DDR_RFDC_FIXED */
  731. in_window = 0;
  732. curr_win_min = curr_win_max = 0;
  733. best_win_min = best_win_max = 0;
  734. for (rqfd = 0; rqfd <= SDRAM_RQDC_RQFD_MAX; rqfd++) {
  735. mfsdram(SDRAM_RQDC, rqdc_reg);
  736. rqdc_reg &= ~(SDRAM_RQDC_RQFD_MASK);
  737. mtsdram(SDRAM_RQDC, rqdc_reg | SDRAM_RQDC_RQFD_ENCODE(rqfd));
  738. pass = 1;
  739. for (bxcr_num = 0; bxcr_num < MAXBXCF; bxcr_num++) {
  740. mfsdram(SDRAM_MB0CF + (bxcr_num<<2), bxcf);
  741. /* Banks enabled */
  742. if (bxcf & SDRAM_BXCF_M_BE_MASK) {
  743. /* Bank is enabled */
  744. membase = get_membase(bxcr_num);
  745. pass &= short_mem_test(membase);
  746. } /* if bank enabled */
  747. } /* for bxcf_num */
  748. /* If this value passed */
  749. if (pass && !in_window) {
  750. in_window = 1;
  751. curr_win_min = curr_win_max = rqfd;
  752. } else if (!pass && in_window) {
  753. in_window = 0;
  754. } else if (pass && in_window) {
  755. curr_win_max = rqfd;
  756. }
  757. if (in_window) {
  758. if ((curr_win_max - curr_win_min) >
  759. (best_win_max - best_win_min)) {
  760. best_win_min = curr_win_min;
  761. best_win_max = curr_win_max;
  762. }
  763. passed = 1;
  764. }
  765. } /* for rqfd */
  766. if ((best_win_min == 0) && (best_win_max == 0))
  767. passed = 0;
  768. debug("RQFD Min: 0x%x\n", best_win_min);
  769. debug("RQFD Max: 0x%x\n", best_win_max);
  770. rqfd_average = ((best_win_min + best_win_max) / 2);
  771. if (rqfd_average < 0)
  772. rqfd_average = 0;
  773. if (rqfd_average > SDRAM_RQDC_RQFD_MAX)
  774. rqfd_average = SDRAM_RQDC_RQFD_MAX;
  775. mtsdram(SDRAM_RQDC, (rqdc_reg & ~SDRAM_RQDC_RQFD_MASK) |
  776. SDRAM_RQDC_RQFD_ENCODE(rqfd_average));
  777. mfsdram(SDRAM_RQDC, rqdc_reg);
  778. mfsdram(SDRAM_RFDC, rfdc_reg);
  779. /*
  780. * Need to program RQDC before RFDC. The value is read above.
  781. * That is the reason why auto cal not work.
  782. * See, comments below.
  783. */
  784. mtsdram(SDRAM_RQDC, rqdc_reg);
  785. mtsdram(SDRAM_RFDC, rfdc_reg);
  786. debug("RQDC: 0x%08lX\n", rqdc_reg);
  787. debug("RFDC: 0x%08lX\n", rfdc_reg);
  788. /* if something passed, then return the size of the largest window */
  789. if (passed != 0) {
  790. passed = size;
  791. cal->rqfd = rqfd_average;
  792. cal->rffd = rffd_average;
  793. }
  794. return (uint)passed;
  795. }
  796. #endif /* defined(CONFIG_PPC4xx_DDR_METHOD_A) */
  797. /*
  798. * Default table for DDR auto-calibration of all
  799. * possible WRDTR and CLKTR values.
  800. * Table format is:
  801. * {SDRAM_WRDTR.[WDTR], SDRAM_CLKTR.[CKTR]}
  802. *
  803. * Table is terminated with {-1, -1} value pair.
  804. *
  805. * Board vendors can specify their own board specific subset of
  806. * known working {SDRAM_WRDTR.[WDTR], SDRAM_CLKTR.[CKTR]} value
  807. * pairs via a board defined ddr_scan_option() function.
  808. */
  809. static struct sdram_timing full_scan_options[] = {
  810. {0, 0}, {0, 1}, {0, 2}, {0, 3},
  811. {1, 0}, {1, 1}, {1, 2}, {1, 3},
  812. {2, 0}, {2, 1}, {2, 2}, {2, 3},
  813. {3, 0}, {3, 1}, {3, 2}, {3, 3},
  814. {4, 0}, {4, 1}, {4, 2}, {4, 3},
  815. {5, 0}, {5, 1}, {5, 2}, {5, 3},
  816. {6, 0}, {6, 1}, {6, 2}, {6, 3},
  817. {-1, -1}
  818. };
  819. /*---------------------------------------------------------------------------+
  820. | DQS_calibration.
  821. +----------------------------------------------------------------------------*/
  822. u32 DQS_autocalibration(void)
  823. {
  824. u32 wdtr;
  825. u32 clkp;
  826. u32 result = 0;
  827. u32 best_result = 0;
  828. u32 best_rdcc;
  829. struct ddrautocal ddrcal;
  830. struct autocal_clks tcal;
  831. ulong rfdc_reg;
  832. ulong rqdc_reg;
  833. u32 val;
  834. int verbose_lvl = 0;
  835. char *str;
  836. char slash[] = "\\|/-\\|/-";
  837. int loopi = 0;
  838. struct sdram_timing *scan_list;
  839. #if defined(DEBUG_PPC4xx_DDR_AUTOCALIBRATION)
  840. int i;
  841. char tmp[64]; /* long enough for environment variables */
  842. #endif
  843. memset(&tcal, 0, sizeof(tcal));
  844. scan_list = ddr_scan_option(full_scan_options);
  845. mfsdram(SDRAM_MCOPT1, val);
  846. if ((val & SDRAM_MCOPT1_MCHK_CHK_REP) == SDRAM_MCOPT1_MCHK_CHK_REP)
  847. str = "ECC Auto calibration -";
  848. else
  849. str = "Auto calibration -";
  850. puts(str);
  851. #if defined(DEBUG_PPC4xx_DDR_AUTOCALIBRATION)
  852. i = getenv_f("autocalib", tmp, sizeof(tmp));
  853. if (i < 0)
  854. strcpy(tmp, CONFIG_AUTOCALIB);
  855. if (strcmp(tmp, "final") == 0) {
  856. /* display the final autocalibration results only */
  857. verbose_lvl = 1;
  858. } else if (strcmp(tmp, "loop") == 0) {
  859. /* display summary autocalibration info per iteration */
  860. verbose_lvl = 2;
  861. } else if (strcmp(tmp, "display") == 0) {
  862. /* display full debug autocalibration window info. */
  863. verbose_lvl = 3;
  864. }
  865. #endif /* (DEBUG_PPC4xx_DDR_AUTOCALIBRATION) */
  866. best_rdcc = (SDRAM_RDCC_RDSS_T4 >> 30);
  867. while ((scan_list->wrdtr != -1) && (scan_list->clktr != -1)) {
  868. wdtr = scan_list->wrdtr;
  869. clkp = scan_list->clktr;
  870. mfsdram(SDRAM_WRDTR, val);
  871. val &= ~(SDRAM_WRDTR_LLWP_MASK | SDRAM_WRDTR_WTR_MASK);
  872. mtsdram(SDRAM_WRDTR, (val |
  873. ddr_wrdtr(SDRAM_WRDTR_LLWP_1_CYC | (wdtr << 25))));
  874. mtsdram(SDRAM_CLKTR, clkp << 30);
  875. relock_memory_DLL();
  876. putc('\b');
  877. putc(slash[loopi++ % 8]);
  878. #ifdef DEBUG
  879. debug("\n");
  880. debug("*** --------------\n");
  881. mfsdram(SDRAM_WRDTR, val);
  882. debug("*** SDRAM_WRDTR set to 0x%08x\n", val);
  883. mfsdram(SDRAM_CLKTR, val);
  884. debug("*** SDRAM_CLKTR set to 0x%08x\n", val);
  885. #endif
  886. debug("\n");
  887. if (verbose_lvl > 2) {
  888. printf("*** SDRAM_WRDTR (wdtr) set to %d\n", wdtr);
  889. printf("*** SDRAM_CLKTR (clkp) set to %d\n", clkp);
  890. }
  891. memset(&ddrcal, 0, sizeof(ddrcal));
  892. /*
  893. * DQS calibration.
  894. */
  895. /*
  896. * program_DQS_calibration_method[A|B]() returns 0 if no
  897. * passing RFDC.[RFFD] window is found or returns the size
  898. * of the best passing window; in the case of a found passing
  899. * window, the ddrcal will contain the values of the best
  900. * window RQDC.[RQFD] and RFDC.[RFFD].
  901. */
  902. /*
  903. * Call PPC4xx SDRAM DDR autocalibration methodA or methodB.
  904. * Default is methodB.
  905. * Defined the autocalibration method in the board specific
  906. * header file.
  907. * Please see include/configs/kilauea.h for an example for
  908. * a board specific implementation.
  909. */
  910. #if defined(CONFIG_PPC4xx_DDR_METHOD_A)
  911. result = program_DQS_calibration_methodA(&ddrcal);
  912. #else
  913. result = program_DQS_calibration_methodB(&ddrcal);
  914. #endif
  915. sync();
  916. /*
  917. * Clear potential errors resulting from auto-calibration.
  918. * If not done, then we could get an interrupt later on when
  919. * exceptions are enabled.
  920. */
  921. set_mcsr(get_mcsr());
  922. val = ddrcal.rdcc; /* RDCC from the best passing window */
  923. udelay(100);
  924. if (verbose_lvl > 1) {
  925. char *tstr;
  926. switch ((val >> 30)) {
  927. case 0:
  928. if (result != 0)
  929. tstr = "T1";
  930. else
  931. tstr = "N/A";
  932. break;
  933. case 1:
  934. tstr = "T2";
  935. break;
  936. case 2:
  937. tstr = "T3";
  938. break;
  939. case 3:
  940. tstr = "T4";
  941. break;
  942. default:
  943. tstr = "unknown";
  944. break;
  945. }
  946. printf("** WRDTR(%d) CLKTR(%d), Wind (%d), best (%d), "
  947. "max-min(0x%04x)(0x%04x), RDCC: %s\n",
  948. wdtr, clkp, result, best_result,
  949. ddrcal.rffd_min, ddrcal.rffd_max, tstr);
  950. }
  951. /*
  952. * The DQS calibration "result" is either "0"
  953. * if no passing window was found, or is the
  954. * size of the RFFD passing window.
  955. */
  956. /*
  957. * want the lowest Read Sample Cycle Select
  958. */
  959. val = SDRAM_RDCC_RDSS_DECODE(val);
  960. debug("*** (%d) (%d) current_rdcc, best_rdcc\n",
  961. val, best_rdcc);
  962. if ((result != 0) &&
  963. (val >= SDRAM_RDCC_RDSS_VAL(SDRAM_RDCC_RDSS_T2))) {
  964. if (((result == best_result) && (val < best_rdcc)) ||
  965. ((result > best_result) && (val <= best_rdcc))) {
  966. tcal.autocal.flags = 1;
  967. debug("*** (%d)(%d) result passed window "
  968. "size: 0x%08x, rqfd = 0x%08x, "
  969. "rffd = 0x%08x, rdcc = 0x%08x\n",
  970. wdtr, clkp, result, ddrcal.rqfd,
  971. ddrcal.rffd, ddrcal.rdcc);
  972. /*
  973. * Save the SDRAM_WRDTR and SDRAM_CLKTR
  974. * settings for the largest returned
  975. * RFFD passing window size.
  976. */
  977. best_rdcc = val;
  978. tcal.clocks.wrdtr = wdtr;
  979. tcal.clocks.clktr = clkp;
  980. tcal.clocks.rdcc = SDRAM_RDCC_RDSS_ENCODE(val);
  981. tcal.autocal.rqfd = ddrcal.rqfd;
  982. tcal.autocal.rffd = ddrcal.rffd;
  983. best_result = result;
  984. if (verbose_lvl > 2) {
  985. printf("** (%d)(%d) "
  986. "best result: 0x%04x\n",
  987. wdtr, clkp,
  988. best_result);
  989. printf("** (%d)(%d) "
  990. "best WRDTR: 0x%04x\n",
  991. wdtr, clkp,
  992. tcal.clocks.wrdtr);
  993. printf("** (%d)(%d) "
  994. "best CLKTR: 0x%04x\n",
  995. wdtr, clkp,
  996. tcal.clocks.clktr);
  997. printf("** (%d)(%d) "
  998. "best RQDC: 0x%04x\n",
  999. wdtr, clkp,
  1000. tcal.autocal.rqfd);
  1001. printf("** (%d)(%d) "
  1002. "best RFDC: 0x%04x\n",
  1003. wdtr, clkp,
  1004. tcal.autocal.rffd);
  1005. printf("** (%d)(%d) "
  1006. "best RDCC: 0x%08x\n",
  1007. wdtr, clkp,
  1008. (u32)tcal.clocks.rdcc);
  1009. mfsdram(SDRAM_RTSR, val);
  1010. printf("** (%d)(%d) best "
  1011. "loop RTSR: 0x%08x\n",
  1012. wdtr, clkp, val);
  1013. mfsdram(SDRAM_FCSR, val);
  1014. printf("** (%d)(%d) best "
  1015. "loop FCSR: 0x%08x\n",
  1016. wdtr, clkp, val);
  1017. }
  1018. }
  1019. } /* if ((result != 0) && (val >= (ddr_rdss_opt()))) */
  1020. scan_list++;
  1021. } /* while ((scan_list->wrdtr != -1) && (scan_list->clktr != -1)) */
  1022. if (tcal.autocal.flags == 1) {
  1023. if (verbose_lvl > 0) {
  1024. printf("*** --------------\n");
  1025. printf("*** best_result window size: %d\n",
  1026. best_result);
  1027. printf("*** best_result WRDTR: 0x%04x\n",
  1028. tcal.clocks.wrdtr);
  1029. printf("*** best_result CLKTR: 0x%04x\n",
  1030. tcal.clocks.clktr);
  1031. printf("*** best_result RQFD: 0x%04x\n",
  1032. tcal.autocal.rqfd);
  1033. printf("*** best_result RFFD: 0x%04x\n",
  1034. tcal.autocal.rffd);
  1035. printf("*** best_result RDCC: 0x%04x\n",
  1036. tcal.clocks.rdcc);
  1037. printf("*** --------------\n");
  1038. printf("\n");
  1039. }
  1040. /*
  1041. * if got best passing result window, then lock in the
  1042. * best CLKTR, WRDTR, RQFD, and RFFD values
  1043. */
  1044. mfsdram(SDRAM_WRDTR, val);
  1045. mtsdram(SDRAM_WRDTR, (val &
  1046. ~(SDRAM_WRDTR_LLWP_MASK | SDRAM_WRDTR_WTR_MASK)) |
  1047. ddr_wrdtr(SDRAM_WRDTR_LLWP_1_CYC |
  1048. (tcal.clocks.wrdtr << 25)));
  1049. mtsdram(SDRAM_CLKTR, tcal.clocks.clktr << 30);
  1050. relock_memory_DLL();
  1051. mfsdram(SDRAM_RQDC, rqdc_reg);
  1052. rqdc_reg &= ~(SDRAM_RQDC_RQFD_MASK);
  1053. mtsdram(SDRAM_RQDC, rqdc_reg |
  1054. SDRAM_RQDC_RQFD_ENCODE(tcal.autocal.rqfd));
  1055. mfsdram(SDRAM_RQDC, rqdc_reg);
  1056. debug("*** best_result: read value SDRAM_RQDC 0x%08lx\n",
  1057. rqdc_reg);
  1058. #if defined(CONFIG_DDR_RFDC_FIXED)
  1059. mtsdram(SDRAM_RFDC, CONFIG_DDR_RFDC_FIXED);
  1060. #else /* CONFIG_DDR_RFDC_FIXED */
  1061. mfsdram(SDRAM_RFDC, rfdc_reg);
  1062. rfdc_reg &= ~(SDRAM_RFDC_RFFD_MASK);
  1063. mtsdram(SDRAM_RFDC, rfdc_reg |
  1064. SDRAM_RFDC_RFFD_ENCODE(tcal.autocal.rffd));
  1065. #endif /* CONFIG_DDR_RFDC_FIXED */
  1066. mfsdram(SDRAM_RFDC, rfdc_reg);
  1067. debug("*** best_result: read value SDRAM_RFDC 0x%08lx\n",
  1068. rfdc_reg);
  1069. mfsdram(SDRAM_RDCC, val);
  1070. debug("*** SDRAM_RDCC 0x%08x\n", val);
  1071. } else {
  1072. /*
  1073. * no valid windows were found
  1074. */
  1075. printf("DQS memory calibration window can not be determined, "
  1076. "terminating u-boot.\n");
  1077. ppc4xx_ibm_ddr2_register_dump();
  1078. spd_ddr_init_hang();
  1079. }
  1080. blank_string(strlen(str));
  1081. return 0;
  1082. }
  1083. #else /* defined(CONFIG_NAND_U_BOOT) || defined(CONFIG_NAND_SPL) */
  1084. u32 DQS_autocalibration(void)
  1085. {
  1086. return 0;
  1087. }
  1088. #endif /* !defined(CONFIG_NAND_U_BOOT) && !defined(CONFIG_NAND_SPL) */