ctrl_regs.c 40 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403
  1. /*
  2. * Copyright 2008-2010 Freescale Semiconductor, Inc.
  3. *
  4. * This program is free software; you can redistribute it and/or modify it
  5. * under the terms of the GNU General Public License as published by the Free
  6. * Software Foundation; either version 2 of the License, or (at your option)
  7. * any later version.
  8. */
  9. /*
  10. * Generic driver for Freescale DDR/DDR2/DDR3 memory controller.
  11. * Based on code from spd_sdram.c
  12. * Author: James Yang [at freescale.com]
  13. */
  14. #include <common.h>
  15. #include <asm/fsl_ddr_sdram.h>
  16. #include "ddr.h"
  17. extern unsigned int picos_to_mclk(unsigned int picos);
  18. /*
  19. * Determine Rtt value.
  20. *
  21. * This should likely be either board or controller specific.
  22. *
  23. * Rtt(nominal) - DDR2:
  24. * 0 = Rtt disabled
  25. * 1 = 75 ohm
  26. * 2 = 150 ohm
  27. * 3 = 50 ohm
  28. * Rtt(nominal) - DDR3:
  29. * 0 = Rtt disabled
  30. * 1 = 60 ohm
  31. * 2 = 120 ohm
  32. * 3 = 40 ohm
  33. * 4 = 20 ohm
  34. * 5 = 30 ohm
  35. *
  36. * FIXME: Apparently 8641 needs a value of 2
  37. * FIXME: Old code seys if 667 MHz or higher, use 3 on 8572
  38. *
  39. * FIXME: There was some effort down this line earlier:
  40. *
  41. * unsigned int i;
  42. * for (i = 0; i < CONFIG_CHIP_SELECTS_PER_CTRL/2; i++) {
  43. * if (popts->dimmslot[i].num_valid_cs
  44. * && (popts->cs_local_opts[2*i].odt_rd_cfg
  45. * || popts->cs_local_opts[2*i].odt_wr_cfg)) {
  46. * rtt = 2;
  47. * break;
  48. * }
  49. * }
  50. */
  51. static inline int fsl_ddr_get_rtt(void)
  52. {
  53. int rtt;
  54. #if defined(CONFIG_FSL_DDR1)
  55. rtt = 0;
  56. #elif defined(CONFIG_FSL_DDR2)
  57. rtt = 3;
  58. #else
  59. rtt = 0;
  60. #endif
  61. return rtt;
  62. }
  63. /*
  64. * compute the CAS write latency according to DDR3 spec
  65. * CWL = 5 if tCK >= 2.5ns
  66. * 6 if 2.5ns > tCK >= 1.875ns
  67. * 7 if 1.875ns > tCK >= 1.5ns
  68. * 8 if 1.5ns > tCK >= 1.25ns
  69. */
  70. static inline unsigned int compute_cas_write_latency(void)
  71. {
  72. unsigned int cwl;
  73. const unsigned int mclk_ps = get_memory_clk_period_ps();
  74. if (mclk_ps >= 2500)
  75. cwl = 5;
  76. else if (mclk_ps >= 1875)
  77. cwl = 6;
  78. else if (mclk_ps >= 1500)
  79. cwl = 7;
  80. else if (mclk_ps >= 1250)
  81. cwl = 8;
  82. else
  83. cwl = 8;
  84. return cwl;
  85. }
  86. /* Chip Select Configuration (CSn_CONFIG) */
  87. static void set_csn_config(int i, fsl_ddr_cfg_regs_t *ddr,
  88. const memctl_options_t *popts,
  89. const dimm_params_t *dimm_params)
  90. {
  91. unsigned int cs_n_en = 0; /* Chip Select enable */
  92. unsigned int intlv_en = 0; /* Memory controller interleave enable */
  93. unsigned int intlv_ctl = 0; /* Interleaving control */
  94. unsigned int ap_n_en = 0; /* Chip select n auto-precharge enable */
  95. unsigned int odt_rd_cfg = 0; /* ODT for reads configuration */
  96. unsigned int odt_wr_cfg = 0; /* ODT for writes configuration */
  97. unsigned int ba_bits_cs_n = 0; /* Num of bank bits for SDRAM on CSn */
  98. unsigned int row_bits_cs_n = 0; /* Num of row bits for SDRAM on CSn */
  99. unsigned int col_bits_cs_n = 0; /* Num of ocl bits for SDRAM on CSn */
  100. /* Compute CS_CONFIG only for existing ranks of each DIMM. */
  101. if ((((i&1) == 0)
  102. && (dimm_params[i/2].n_ranks == 1))
  103. || (dimm_params[i/2].n_ranks == 2)) {
  104. unsigned int n_banks_per_sdram_device;
  105. cs_n_en = 1;
  106. if (i == 0) {
  107. /* These fields only available in CS0_CONFIG */
  108. intlv_en = popts->memctl_interleaving;
  109. intlv_ctl = popts->memctl_interleaving_mode;
  110. }
  111. ap_n_en = popts->cs_local_opts[i].auto_precharge;
  112. odt_rd_cfg = popts->cs_local_opts[i].odt_rd_cfg;
  113. odt_wr_cfg = popts->cs_local_opts[i].odt_wr_cfg;
  114. n_banks_per_sdram_device
  115. = dimm_params[i/2].n_banks_per_sdram_device;
  116. ba_bits_cs_n = __ilog2(n_banks_per_sdram_device) - 2;
  117. row_bits_cs_n = dimm_params[i/2].n_row_addr - 12;
  118. col_bits_cs_n = dimm_params[i/2].n_col_addr - 8;
  119. }
  120. ddr->cs[i].config = (0
  121. | ((cs_n_en & 0x1) << 31)
  122. | ((intlv_en & 0x3) << 29)
  123. | ((intlv_ctl & 0xf) << 24)
  124. | ((ap_n_en & 0x1) << 23)
  125. /* XXX: some implementation only have 1 bit starting at left */
  126. | ((odt_rd_cfg & 0x7) << 20)
  127. /* XXX: Some implementation only have 1 bit starting at left */
  128. | ((odt_wr_cfg & 0x7) << 16)
  129. | ((ba_bits_cs_n & 0x3) << 14)
  130. | ((row_bits_cs_n & 0x7) << 8)
  131. | ((col_bits_cs_n & 0x7) << 0)
  132. );
  133. debug("FSLDDR: cs[%d]_config = 0x%08x\n", i,ddr->cs[i].config);
  134. }
  135. /* Chip Select Configuration 2 (CSn_CONFIG_2) */
  136. /* FIXME: 8572 */
  137. static void set_csn_config_2(int i, fsl_ddr_cfg_regs_t *ddr)
  138. {
  139. unsigned int pasr_cfg = 0; /* Partial array self refresh config */
  140. ddr->cs[i].config_2 = ((pasr_cfg & 7) << 24);
  141. debug("FSLDDR: cs[%d]_config_2 = 0x%08x\n", i, ddr->cs[i].config_2);
  142. }
  143. /* -3E = 667 CL5, -25 = CL6 800, -25E = CL5 800 */
  144. #if !defined(CONFIG_FSL_DDR1)
  145. /*
  146. * DDR SDRAM Timing Configuration 0 (TIMING_CFG_0)
  147. *
  148. * Avoid writing for DDR I. The new PQ38 DDR controller
  149. * dreams up non-zero default values to be backwards compatible.
  150. */
  151. static void set_timing_cfg_0(fsl_ddr_cfg_regs_t *ddr)
  152. {
  153. unsigned char trwt_mclk = 0; /* Read-to-write turnaround */
  154. unsigned char twrt_mclk = 0; /* Write-to-read turnaround */
  155. /* 7.5 ns on -3E; 0 means WL - CL + BL/2 + 1 */
  156. unsigned char trrt_mclk = 0; /* Read-to-read turnaround */
  157. unsigned char twwt_mclk = 0; /* Write-to-write turnaround */
  158. /* Active powerdown exit timing (tXARD and tXARDS). */
  159. unsigned char act_pd_exit_mclk;
  160. /* Precharge powerdown exit timing (tXP). */
  161. unsigned char pre_pd_exit_mclk;
  162. /* Precharge powerdown exit timing (tAXPD). */
  163. unsigned char taxpd_mclk;
  164. /* Mode register set cycle time (tMRD). */
  165. unsigned char tmrd_mclk;
  166. #if defined(CONFIG_FSL_DDR3)
  167. /*
  168. * (tXARD and tXARDS). Empirical?
  169. * The DDR3 spec has not tXARD,
  170. * we use the tXP instead of it.
  171. * tXP=max(3nCK, 7.5ns) for DDR3.
  172. * spec has not the tAXPD, we use
  173. * tAXPD=8, need design to confirm.
  174. */
  175. int tXP = max((get_memory_clk_period_ps() * 3), 7500); /* unit=ps */
  176. act_pd_exit_mclk = picos_to_mclk(tXP);
  177. /* Mode register MR0[A12] is '1' - fast exit */
  178. pre_pd_exit_mclk = act_pd_exit_mclk;
  179. taxpd_mclk = 8;
  180. tmrd_mclk = 4;
  181. /* set the turnaround time */
  182. trwt_mclk = 1;
  183. #else /* CONFIG_FSL_DDR2 */
  184. /*
  185. * (tXARD and tXARDS). Empirical?
  186. * tXARD = 2 for DDR2
  187. * tXP=2
  188. * tAXPD=8
  189. */
  190. act_pd_exit_mclk = 2;
  191. pre_pd_exit_mclk = 2;
  192. taxpd_mclk = 8;
  193. tmrd_mclk = 2;
  194. #endif
  195. ddr->timing_cfg_0 = (0
  196. | ((trwt_mclk & 0x3) << 30) /* RWT */
  197. | ((twrt_mclk & 0x3) << 28) /* WRT */
  198. | ((trrt_mclk & 0x3) << 26) /* RRT */
  199. | ((twwt_mclk & 0x3) << 24) /* WWT */
  200. | ((act_pd_exit_mclk & 0x7) << 20) /* ACT_PD_EXIT */
  201. | ((pre_pd_exit_mclk & 0xF) << 16) /* PRE_PD_EXIT */
  202. | ((taxpd_mclk & 0xf) << 8) /* ODT_PD_EXIT */
  203. | ((tmrd_mclk & 0xf) << 0) /* MRS_CYC */
  204. );
  205. debug("FSLDDR: timing_cfg_0 = 0x%08x\n", ddr->timing_cfg_0);
  206. }
  207. #endif /* defined(CONFIG_FSL_DDR2) */
  208. /* DDR SDRAM Timing Configuration 3 (TIMING_CFG_3) */
  209. static void set_timing_cfg_3(fsl_ddr_cfg_regs_t *ddr,
  210. const common_timing_params_t *common_dimm,
  211. unsigned int cas_latency)
  212. {
  213. /* Extended Activate to precharge interval (tRAS) */
  214. unsigned int ext_acttopre = 0;
  215. unsigned int ext_refrec; /* Extended refresh recovery time (tRFC) */
  216. unsigned int ext_caslat = 0; /* Extended MCAS latency from READ cmd */
  217. unsigned int cntl_adj = 0; /* Control Adjust */
  218. /* If the tRAS > 19 MCLK, we use the ext mode */
  219. if (picos_to_mclk(common_dimm->tRAS_ps) > 0x13)
  220. ext_acttopre = 1;
  221. ext_refrec = (picos_to_mclk(common_dimm->tRFC_ps) - 8) >> 4;
  222. /* If the CAS latency more than 8, use the ext mode */
  223. if (cas_latency > 8)
  224. ext_caslat = 1;
  225. ddr->timing_cfg_3 = (0
  226. | ((ext_acttopre & 0x1) << 24)
  227. | ((ext_refrec & 0xF) << 16)
  228. | ((ext_caslat & 0x1) << 12)
  229. | ((cntl_adj & 0x7) << 0)
  230. );
  231. debug("FSLDDR: timing_cfg_3 = 0x%08x\n", ddr->timing_cfg_3);
  232. }
  233. /* DDR SDRAM Timing Configuration 1 (TIMING_CFG_1) */
  234. static void set_timing_cfg_1(fsl_ddr_cfg_regs_t *ddr,
  235. const memctl_options_t *popts,
  236. const common_timing_params_t *common_dimm,
  237. unsigned int cas_latency)
  238. {
  239. /* Precharge-to-activate interval (tRP) */
  240. unsigned char pretoact_mclk;
  241. /* Activate to precharge interval (tRAS) */
  242. unsigned char acttopre_mclk;
  243. /* Activate to read/write interval (tRCD) */
  244. unsigned char acttorw_mclk;
  245. /* CASLAT */
  246. unsigned char caslat_ctrl;
  247. /* Refresh recovery time (tRFC) ; trfc_low */
  248. unsigned char refrec_ctrl;
  249. /* Last data to precharge minimum interval (tWR) */
  250. unsigned char wrrec_mclk;
  251. /* Activate-to-activate interval (tRRD) */
  252. unsigned char acttoact_mclk;
  253. /* Last write data pair to read command issue interval (tWTR) */
  254. unsigned char wrtord_mclk;
  255. pretoact_mclk = picos_to_mclk(common_dimm->tRP_ps);
  256. acttopre_mclk = picos_to_mclk(common_dimm->tRAS_ps);
  257. acttorw_mclk = picos_to_mclk(common_dimm->tRCD_ps);
  258. /*
  259. * Translate CAS Latency to a DDR controller field value:
  260. *
  261. * CAS Lat DDR I DDR II Ctrl
  262. * Clocks SPD Bit SPD Bit Value
  263. * ------- ------- ------- -----
  264. * 1.0 0 0001
  265. * 1.5 1 0010
  266. * 2.0 2 2 0011
  267. * 2.5 3 0100
  268. * 3.0 4 3 0101
  269. * 3.5 5 0110
  270. * 4.0 4 0111
  271. * 4.5 1000
  272. * 5.0 5 1001
  273. */
  274. #if defined(CONFIG_FSL_DDR1)
  275. caslat_ctrl = (cas_latency + 1) & 0x07;
  276. #elif defined(CONFIG_FSL_DDR2)
  277. caslat_ctrl = 2 * cas_latency - 1;
  278. #else
  279. /*
  280. * if the CAS latency more than 8 cycle,
  281. * we need set extend bit for it at
  282. * TIMING_CFG_3[EXT_CASLAT]
  283. */
  284. if (cas_latency > 8)
  285. cas_latency -= 8;
  286. caslat_ctrl = 2 * cas_latency - 1;
  287. #endif
  288. refrec_ctrl = picos_to_mclk(common_dimm->tRFC_ps) - 8;
  289. wrrec_mclk = picos_to_mclk(common_dimm->tWR_ps);
  290. if (popts->OTF_burst_chop_en)
  291. wrrec_mclk += 2;
  292. acttoact_mclk = picos_to_mclk(common_dimm->tRRD_ps);
  293. /*
  294. * JEDEC has min requirement for tRRD
  295. */
  296. #if defined(CONFIG_FSL_DDR3)
  297. if (acttoact_mclk < 4)
  298. acttoact_mclk = 4;
  299. #endif
  300. wrtord_mclk = picos_to_mclk(common_dimm->tWTR_ps);
  301. /*
  302. * JEDEC has some min requirements for tWTR
  303. */
  304. #if defined(CONFIG_FSL_DDR2)
  305. if (wrtord_mclk < 2)
  306. wrtord_mclk = 2;
  307. #elif defined(CONFIG_FSL_DDR3)
  308. if (wrtord_mclk < 4)
  309. wrtord_mclk = 4;
  310. #endif
  311. if (popts->OTF_burst_chop_en)
  312. wrtord_mclk += 2;
  313. ddr->timing_cfg_1 = (0
  314. | ((pretoact_mclk & 0x0F) << 28)
  315. | ((acttopre_mclk & 0x0F) << 24)
  316. | ((acttorw_mclk & 0xF) << 20)
  317. | ((caslat_ctrl & 0xF) << 16)
  318. | ((refrec_ctrl & 0xF) << 12)
  319. | ((wrrec_mclk & 0x0F) << 8)
  320. | ((acttoact_mclk & 0x07) << 4)
  321. | ((wrtord_mclk & 0x07) << 0)
  322. );
  323. debug("FSLDDR: timing_cfg_1 = 0x%08x\n", ddr->timing_cfg_1);
  324. }
  325. /* DDR SDRAM Timing Configuration 2 (TIMING_CFG_2) */
  326. static void set_timing_cfg_2(fsl_ddr_cfg_regs_t *ddr,
  327. const memctl_options_t *popts,
  328. const common_timing_params_t *common_dimm,
  329. unsigned int cas_latency,
  330. unsigned int additive_latency)
  331. {
  332. /* Additive latency */
  333. unsigned char add_lat_mclk;
  334. /* CAS-to-preamble override */
  335. unsigned short cpo;
  336. /* Write latency */
  337. unsigned char wr_lat;
  338. /* Read to precharge (tRTP) */
  339. unsigned char rd_to_pre;
  340. /* Write command to write data strobe timing adjustment */
  341. unsigned char wr_data_delay;
  342. /* Minimum CKE pulse width (tCKE) */
  343. unsigned char cke_pls;
  344. /* Window for four activates (tFAW) */
  345. unsigned short four_act;
  346. /* FIXME add check that this must be less than acttorw_mclk */
  347. add_lat_mclk = additive_latency;
  348. cpo = popts->cpo_override;
  349. #if defined(CONFIG_FSL_DDR1)
  350. /*
  351. * This is a lie. It should really be 1, but if it is
  352. * set to 1, bits overlap into the old controller's
  353. * otherwise unused ACSM field. If we leave it 0, then
  354. * the HW will magically treat it as 1 for DDR 1. Oh Yea.
  355. */
  356. wr_lat = 0;
  357. #elif defined(CONFIG_FSL_DDR2)
  358. wr_lat = cas_latency - 1;
  359. #else
  360. wr_lat = compute_cas_write_latency();
  361. #endif
  362. rd_to_pre = picos_to_mclk(common_dimm->tRTP_ps);
  363. /*
  364. * JEDEC has some min requirements for tRTP
  365. */
  366. #if defined(CONFIG_FSL_DDR2)
  367. if (rd_to_pre < 2)
  368. rd_to_pre = 2;
  369. #elif defined(CONFIG_FSL_DDR3)
  370. if (rd_to_pre < 4)
  371. rd_to_pre = 4;
  372. #endif
  373. if (additive_latency)
  374. rd_to_pre += additive_latency;
  375. if (popts->OTF_burst_chop_en)
  376. rd_to_pre += 2; /* according to UM */
  377. wr_data_delay = popts->write_data_delay;
  378. cke_pls = picos_to_mclk(popts->tCKE_clock_pulse_width_ps);
  379. four_act = picos_to_mclk(popts->tFAW_window_four_activates_ps);
  380. ddr->timing_cfg_2 = (0
  381. | ((add_lat_mclk & 0xf) << 28)
  382. | ((cpo & 0x1f) << 23)
  383. | ((wr_lat & 0xf) << 19)
  384. | ((rd_to_pre & RD_TO_PRE_MASK) << RD_TO_PRE_SHIFT)
  385. | ((wr_data_delay & WR_DATA_DELAY_MASK) << WR_DATA_DELAY_SHIFT)
  386. | ((cke_pls & 0x7) << 6)
  387. | ((four_act & 0x3f) << 0)
  388. );
  389. debug("FSLDDR: timing_cfg_2 = 0x%08x\n", ddr->timing_cfg_2);
  390. }
  391. /* DDR SDRAM control configuration (DDR_SDRAM_CFG) */
  392. static void set_ddr_sdram_cfg(fsl_ddr_cfg_regs_t *ddr,
  393. const memctl_options_t *popts,
  394. const common_timing_params_t *common_dimm)
  395. {
  396. unsigned int mem_en; /* DDR SDRAM interface logic enable */
  397. unsigned int sren; /* Self refresh enable (during sleep) */
  398. unsigned int ecc_en; /* ECC enable. */
  399. unsigned int rd_en; /* Registered DIMM enable */
  400. unsigned int sdram_type; /* Type of SDRAM */
  401. unsigned int dyn_pwr; /* Dynamic power management mode */
  402. unsigned int dbw; /* DRAM dta bus width */
  403. unsigned int eight_be = 0; /* 8-beat burst enable, DDR2 is zero */
  404. unsigned int ncap = 0; /* Non-concurrent auto-precharge */
  405. unsigned int threeT_en; /* Enable 3T timing */
  406. unsigned int twoT_en; /* Enable 2T timing */
  407. unsigned int ba_intlv_ctl; /* Bank (CS) interleaving control */
  408. unsigned int x32_en = 0; /* x32 enable */
  409. unsigned int pchb8 = 0; /* precharge bit 8 enable */
  410. unsigned int hse; /* Global half strength override */
  411. unsigned int mem_halt = 0; /* memory controller halt */
  412. unsigned int bi = 0; /* Bypass initialization */
  413. mem_en = 1;
  414. sren = popts->self_refresh_in_sleep;
  415. if (common_dimm->all_DIMMs_ECC_capable) {
  416. /* Allow setting of ECC only if all DIMMs are ECC. */
  417. ecc_en = popts->ECC_mode;
  418. } else {
  419. ecc_en = 0;
  420. }
  421. rd_en = (common_dimm->all_DIMMs_registered
  422. && !common_dimm->all_DIMMs_unbuffered);
  423. sdram_type = CONFIG_FSL_SDRAM_TYPE;
  424. dyn_pwr = popts->dynamic_power;
  425. dbw = popts->data_bus_width;
  426. /* 8-beat burst enable DDR-III case
  427. * we must clear it when use the on-the-fly mode,
  428. * must set it when use the 32-bits bus mode.
  429. */
  430. if (sdram_type == SDRAM_TYPE_DDR3) {
  431. if (popts->burst_length == DDR_BL8)
  432. eight_be = 1;
  433. if (popts->burst_length == DDR_OTF)
  434. eight_be = 0;
  435. if (dbw == 0x1)
  436. eight_be = 1;
  437. }
  438. threeT_en = popts->threeT_en;
  439. twoT_en = popts->twoT_en;
  440. ba_intlv_ctl = popts->ba_intlv_ctl;
  441. hse = popts->half_strength_driver_enable;
  442. ddr->ddr_sdram_cfg = (0
  443. | ((mem_en & 0x1) << 31)
  444. | ((sren & 0x1) << 30)
  445. | ((ecc_en & 0x1) << 29)
  446. | ((rd_en & 0x1) << 28)
  447. | ((sdram_type & 0x7) << 24)
  448. | ((dyn_pwr & 0x1) << 21)
  449. | ((dbw & 0x3) << 19)
  450. | ((eight_be & 0x1) << 18)
  451. | ((ncap & 0x1) << 17)
  452. | ((threeT_en & 0x1) << 16)
  453. | ((twoT_en & 0x1) << 15)
  454. | ((ba_intlv_ctl & 0x7F) << 8)
  455. | ((x32_en & 0x1) << 5)
  456. | ((pchb8 & 0x1) << 4)
  457. | ((hse & 0x1) << 3)
  458. | ((mem_halt & 0x1) << 1)
  459. | ((bi & 0x1) << 0)
  460. );
  461. debug("FSLDDR: ddr_sdram_cfg = 0x%08x\n", ddr->ddr_sdram_cfg);
  462. }
  463. /* DDR SDRAM control configuration 2 (DDR_SDRAM_CFG_2) */
  464. static void set_ddr_sdram_cfg_2(fsl_ddr_cfg_regs_t *ddr,
  465. const memctl_options_t *popts)
  466. {
  467. unsigned int frc_sr = 0; /* Force self refresh */
  468. unsigned int sr_ie = 0; /* Self-refresh interrupt enable */
  469. unsigned int dll_rst_dis; /* DLL reset disable */
  470. unsigned int dqs_cfg; /* DQS configuration */
  471. unsigned int odt_cfg; /* ODT configuration */
  472. unsigned int num_pr; /* Number of posted refreshes */
  473. unsigned int obc_cfg; /* On-The-Fly Burst Chop Cfg */
  474. unsigned int ap_en; /* Address Parity Enable */
  475. unsigned int d_init; /* DRAM data initialization */
  476. unsigned int rcw_en = 0; /* Register Control Word Enable */
  477. unsigned int md_en = 0; /* Mirrored DIMM Enable */
  478. dll_rst_dis = 1; /* Make this configurable */
  479. dqs_cfg = popts->DQS_config;
  480. if (popts->cs_local_opts[0].odt_rd_cfg
  481. || popts->cs_local_opts[0].odt_wr_cfg) {
  482. /* FIXME */
  483. odt_cfg = 2;
  484. } else {
  485. odt_cfg = 0;
  486. }
  487. num_pr = 1; /* Make this configurable */
  488. /*
  489. * 8572 manual says
  490. * {TIMING_CFG_1[PRETOACT]
  491. * + [DDR_SDRAM_CFG_2[NUM_PR]
  492. * * ({EXT_REFREC || REFREC} + 8 + 2)]}
  493. * << DDR_SDRAM_INTERVAL[REFINT]
  494. */
  495. #if defined(CONFIG_FSL_DDR3)
  496. obc_cfg = popts->OTF_burst_chop_en;
  497. #else
  498. obc_cfg = 0;
  499. #endif
  500. ap_en = 0; /* Make this configurable? */
  501. #if defined(CONFIG_ECC_INIT_VIA_DDRCONTROLLER)
  502. /* Use the DDR controller to auto initialize memory. */
  503. d_init = 1;
  504. ddr->ddr_data_init = CONFIG_MEM_INIT_VALUE;
  505. debug("DDR: ddr_data_init = 0x%08x\n", ddr->ddr_data_init);
  506. #else
  507. /* Memory will be initialized via DMA, or not at all. */
  508. d_init = 0;
  509. #endif
  510. #if defined(CONFIG_FSL_DDR3)
  511. md_en = popts->mirrored_dimm;
  512. #endif
  513. ddr->ddr_sdram_cfg_2 = (0
  514. | ((frc_sr & 0x1) << 31)
  515. | ((sr_ie & 0x1) << 30)
  516. | ((dll_rst_dis & 0x1) << 29)
  517. | ((dqs_cfg & 0x3) << 26)
  518. | ((odt_cfg & 0x3) << 21)
  519. | ((num_pr & 0xf) << 12)
  520. | ((obc_cfg & 0x1) << 6)
  521. | ((ap_en & 0x1) << 5)
  522. | ((d_init & 0x1) << 4)
  523. | ((rcw_en & 0x1) << 2)
  524. | ((md_en & 0x1) << 0)
  525. );
  526. debug("FSLDDR: ddr_sdram_cfg_2 = 0x%08x\n", ddr->ddr_sdram_cfg_2);
  527. }
  528. /* DDR SDRAM Mode configuration 2 (DDR_SDRAM_MODE_2) */
  529. static void set_ddr_sdram_mode_2(fsl_ddr_cfg_regs_t *ddr,
  530. const memctl_options_t *popts)
  531. {
  532. unsigned short esdmode2 = 0; /* Extended SDRAM mode 2 */
  533. unsigned short esdmode3 = 0; /* Extended SDRAM mode 3 */
  534. #if defined(CONFIG_FSL_DDR3)
  535. unsigned int rtt_wr = 0; /* Rtt_WR - dynamic ODT off */
  536. unsigned int srt = 0; /* self-refresh temerature, normal range */
  537. unsigned int asr = 0; /* auto self-refresh disable */
  538. unsigned int cwl = compute_cas_write_latency() - 5;
  539. unsigned int pasr = 0; /* partial array self refresh disable */
  540. if (popts->rtt_override)
  541. rtt_wr = popts->rtt_wr_override_value;
  542. esdmode2 = (0
  543. | ((rtt_wr & 0x3) << 9)
  544. | ((srt & 0x1) << 7)
  545. | ((asr & 0x1) << 6)
  546. | ((cwl & 0x7) << 3)
  547. | ((pasr & 0x7) << 0));
  548. #endif
  549. ddr->ddr_sdram_mode_2 = (0
  550. | ((esdmode2 & 0xFFFF) << 16)
  551. | ((esdmode3 & 0xFFFF) << 0)
  552. );
  553. debug("FSLDDR: ddr_sdram_mode_2 = 0x%08x\n", ddr->ddr_sdram_mode_2);
  554. }
  555. /* DDR SDRAM Interval Configuration (DDR_SDRAM_INTERVAL) */
  556. static void set_ddr_sdram_interval(fsl_ddr_cfg_regs_t *ddr,
  557. const memctl_options_t *popts,
  558. const common_timing_params_t *common_dimm)
  559. {
  560. unsigned int refint; /* Refresh interval */
  561. unsigned int bstopre; /* Precharge interval */
  562. refint = picos_to_mclk(common_dimm->refresh_rate_ps);
  563. bstopre = popts->bstopre;
  564. /* refint field used 0x3FFF in earlier controllers */
  565. ddr->ddr_sdram_interval = (0
  566. | ((refint & 0xFFFF) << 16)
  567. | ((bstopre & 0x3FFF) << 0)
  568. );
  569. debug("FSLDDR: ddr_sdram_interval = 0x%08x\n", ddr->ddr_sdram_interval);
  570. }
  571. #if defined(CONFIG_FSL_DDR3)
  572. /* DDR SDRAM Mode configuration set (DDR_SDRAM_MODE) */
  573. static void set_ddr_sdram_mode(fsl_ddr_cfg_regs_t *ddr,
  574. const memctl_options_t *popts,
  575. const common_timing_params_t *common_dimm,
  576. unsigned int cas_latency,
  577. unsigned int additive_latency)
  578. {
  579. unsigned short esdmode; /* Extended SDRAM mode */
  580. unsigned short sdmode; /* SDRAM mode */
  581. /* Mode Register - MR1 */
  582. unsigned int qoff = 0; /* Output buffer enable 0=yes, 1=no */
  583. unsigned int tdqs_en = 0; /* TDQS Enable: 0=no, 1=yes */
  584. unsigned int rtt;
  585. unsigned int wrlvl_en = 0; /* Write level enable: 0=no, 1=yes */
  586. unsigned int al = 0; /* Posted CAS# additive latency (AL) */
  587. unsigned int dic = 1; /* Output driver impedance, 34ohm */
  588. unsigned int dll_en = 0; /* DLL Enable 0=Enable (Normal),
  589. 1=Disable (Test/Debug) */
  590. /* Mode Register - MR0 */
  591. unsigned int dll_on; /* DLL control for precharge PD, 0=off, 1=on */
  592. unsigned int wr; /* Write Recovery */
  593. unsigned int dll_rst; /* DLL Reset */
  594. unsigned int mode; /* Normal=0 or Test=1 */
  595. unsigned int caslat = 4;/* CAS# latency, default set as 6 cycles */
  596. /* BT: Burst Type (0=Nibble Sequential, 1=Interleaved) */
  597. unsigned int bt;
  598. unsigned int bl; /* BL: Burst Length */
  599. unsigned int wr_mclk;
  600. const unsigned int mclk_ps = get_memory_clk_period_ps();
  601. rtt = fsl_ddr_get_rtt();
  602. if (popts->rtt_override)
  603. rtt = popts->rtt_override_value;
  604. if (additive_latency == (cas_latency - 1))
  605. al = 1;
  606. if (additive_latency == (cas_latency - 2))
  607. al = 2;
  608. /*
  609. * The esdmode value will also be used for writing
  610. * MR1 during write leveling for DDR3, although the
  611. * bits specifically related to the write leveling
  612. * scheme will be handled automatically by the DDR
  613. * controller. so we set the wrlvl_en = 0 here.
  614. */
  615. esdmode = (0
  616. | ((qoff & 0x1) << 12)
  617. | ((tdqs_en & 0x1) << 11)
  618. | ((rtt & 0x4) << 7) /* rtt field is split */
  619. | ((wrlvl_en & 0x1) << 7)
  620. | ((rtt & 0x2) << 5) /* rtt field is split */
  621. | ((dic & 0x2) << 4) /* DIC field is split */
  622. | ((al & 0x3) << 3)
  623. | ((rtt & 0x1) << 2) /* rtt field is split */
  624. | ((dic & 0x1) << 1) /* DIC field is split */
  625. | ((dll_en & 0x1) << 0)
  626. );
  627. /*
  628. * DLL control for precharge PD
  629. * 0=slow exit DLL off (tXPDLL)
  630. * 1=fast exit DLL on (tXP)
  631. */
  632. dll_on = 1;
  633. wr_mclk = (common_dimm->tWR_ps + mclk_ps - 1) / mclk_ps;
  634. if (wr_mclk >= 12)
  635. wr = 6;
  636. else if (wr_mclk >= 9)
  637. wr = 5;
  638. else
  639. wr = wr_mclk - 4;
  640. dll_rst = 0; /* dll no reset */
  641. mode = 0; /* normal mode */
  642. /* look up table to get the cas latency bits */
  643. if (cas_latency >= 5 && cas_latency <= 11) {
  644. unsigned char cas_latency_table[7] = {
  645. 0x2, /* 5 clocks */
  646. 0x4, /* 6 clocks */
  647. 0x6, /* 7 clocks */
  648. 0x8, /* 8 clocks */
  649. 0xa, /* 9 clocks */
  650. 0xc, /* 10 clocks */
  651. 0xe /* 11 clocks */
  652. };
  653. caslat = cas_latency_table[cas_latency - 5];
  654. }
  655. bt = 0; /* Nibble sequential */
  656. switch (popts->burst_length) {
  657. case DDR_BL8:
  658. bl = 0;
  659. break;
  660. case DDR_OTF:
  661. bl = 1;
  662. break;
  663. case DDR_BC4:
  664. bl = 2;
  665. break;
  666. default:
  667. printf("Error: invalid burst length of %u specified. "
  668. " Defaulting to on-the-fly BC4 or BL8 beats.\n",
  669. popts->burst_length);
  670. bl = 1;
  671. break;
  672. }
  673. sdmode = (0
  674. | ((dll_on & 0x1) << 12)
  675. | ((wr & 0x7) << 9)
  676. | ((dll_rst & 0x1) << 8)
  677. | ((mode & 0x1) << 7)
  678. | (((caslat >> 1) & 0x7) << 4)
  679. | ((bt & 0x1) << 3)
  680. | ((bl & 0x3) << 0)
  681. );
  682. ddr->ddr_sdram_mode = (0
  683. | ((esdmode & 0xFFFF) << 16)
  684. | ((sdmode & 0xFFFF) << 0)
  685. );
  686. debug("FSLDDR: ddr_sdram_mode = 0x%08x\n", ddr->ddr_sdram_mode);
  687. }
  688. #else /* !CONFIG_FSL_DDR3 */
  689. /* DDR SDRAM Mode configuration set (DDR_SDRAM_MODE) */
  690. static void set_ddr_sdram_mode(fsl_ddr_cfg_regs_t *ddr,
  691. const memctl_options_t *popts,
  692. const common_timing_params_t *common_dimm,
  693. unsigned int cas_latency,
  694. unsigned int additive_latency)
  695. {
  696. unsigned short esdmode; /* Extended SDRAM mode */
  697. unsigned short sdmode; /* SDRAM mode */
  698. /*
  699. * FIXME: This ought to be pre-calculated in a
  700. * technology-specific routine,
  701. * e.g. compute_DDR2_mode_register(), and then the
  702. * sdmode and esdmode passed in as part of common_dimm.
  703. */
  704. /* Extended Mode Register */
  705. unsigned int mrs = 0; /* Mode Register Set */
  706. unsigned int outputs = 0; /* 0=Enabled, 1=Disabled */
  707. unsigned int rdqs_en = 0; /* RDQS Enable: 0=no, 1=yes */
  708. unsigned int dqs_en = 0; /* DQS# Enable: 0=enable, 1=disable */
  709. unsigned int ocd = 0; /* 0x0=OCD not supported,
  710. 0x7=OCD default state */
  711. unsigned int rtt;
  712. unsigned int al; /* Posted CAS# additive latency (AL) */
  713. unsigned int ods = 0; /* Output Drive Strength:
  714. 0 = Full strength (18ohm)
  715. 1 = Reduced strength (4ohm) */
  716. unsigned int dll_en = 0; /* DLL Enable 0=Enable (Normal),
  717. 1=Disable (Test/Debug) */
  718. /* Mode Register (MR) */
  719. unsigned int mr; /* Mode Register Definition */
  720. unsigned int pd; /* Power-Down Mode */
  721. unsigned int wr; /* Write Recovery */
  722. unsigned int dll_res; /* DLL Reset */
  723. unsigned int mode; /* Normal=0 or Test=1 */
  724. unsigned int caslat = 0;/* CAS# latency */
  725. /* BT: Burst Type (0=Sequential, 1=Interleaved) */
  726. unsigned int bt;
  727. unsigned int bl; /* BL: Burst Length */
  728. #if defined(CONFIG_FSL_DDR2)
  729. const unsigned int mclk_ps = get_memory_clk_period_ps();
  730. #endif
  731. rtt = fsl_ddr_get_rtt();
  732. al = additive_latency;
  733. esdmode = (0
  734. | ((mrs & 0x3) << 14)
  735. | ((outputs & 0x1) << 12)
  736. | ((rdqs_en & 0x1) << 11)
  737. | ((dqs_en & 0x1) << 10)
  738. | ((ocd & 0x7) << 7)
  739. | ((rtt & 0x2) << 5) /* rtt field is split */
  740. | ((al & 0x7) << 3)
  741. | ((rtt & 0x1) << 2) /* rtt field is split */
  742. | ((ods & 0x1) << 1)
  743. | ((dll_en & 0x1) << 0)
  744. );
  745. mr = 0; /* FIXME: CHECKME */
  746. /*
  747. * 0 = Fast Exit (Normal)
  748. * 1 = Slow Exit (Low Power)
  749. */
  750. pd = 0;
  751. #if defined(CONFIG_FSL_DDR1)
  752. wr = 0; /* Historical */
  753. #elif defined(CONFIG_FSL_DDR2)
  754. wr = (common_dimm->tWR_ps + mclk_ps - 1) / mclk_ps - 1;
  755. #endif
  756. dll_res = 0;
  757. mode = 0;
  758. #if defined(CONFIG_FSL_DDR1)
  759. if (1 <= cas_latency && cas_latency <= 4) {
  760. unsigned char mode_caslat_table[4] = {
  761. 0x5, /* 1.5 clocks */
  762. 0x2, /* 2.0 clocks */
  763. 0x6, /* 2.5 clocks */
  764. 0x3 /* 3.0 clocks */
  765. };
  766. caslat = mode_caslat_table[cas_latency - 1];
  767. } else {
  768. printf("Warning: unknown cas_latency %d\n", cas_latency);
  769. }
  770. #elif defined(CONFIG_FSL_DDR2)
  771. caslat = cas_latency;
  772. #endif
  773. bt = 0;
  774. switch (popts->burst_length) {
  775. case DDR_BL4:
  776. bl = 2;
  777. break;
  778. case DDR_BL8:
  779. bl = 3;
  780. break;
  781. default:
  782. printf("Error: invalid burst length of %u specified. "
  783. " Defaulting to 4 beats.\n",
  784. popts->burst_length);
  785. bl = 2;
  786. break;
  787. }
  788. sdmode = (0
  789. | ((mr & 0x3) << 14)
  790. | ((pd & 0x1) << 12)
  791. | ((wr & 0x7) << 9)
  792. | ((dll_res & 0x1) << 8)
  793. | ((mode & 0x1) << 7)
  794. | ((caslat & 0x7) << 4)
  795. | ((bt & 0x1) << 3)
  796. | ((bl & 0x7) << 0)
  797. );
  798. ddr->ddr_sdram_mode = (0
  799. | ((esdmode & 0xFFFF) << 16)
  800. | ((sdmode & 0xFFFF) << 0)
  801. );
  802. debug("FSLDDR: ddr_sdram_mode = 0x%08x\n", ddr->ddr_sdram_mode);
  803. }
  804. #endif
  805. /* DDR SDRAM Data Initialization (DDR_DATA_INIT) */
  806. static void set_ddr_data_init(fsl_ddr_cfg_regs_t *ddr)
  807. {
  808. unsigned int init_value; /* Initialization value */
  809. init_value = 0xDEADBEEF;
  810. ddr->ddr_data_init = init_value;
  811. }
  812. /*
  813. * DDR SDRAM Clock Control (DDR_SDRAM_CLK_CNTL)
  814. * The old controller on the 8540/60 doesn't have this register.
  815. * Hope it's OK to set it (to 0) anyway.
  816. */
  817. static void set_ddr_sdram_clk_cntl(fsl_ddr_cfg_regs_t *ddr,
  818. const memctl_options_t *popts)
  819. {
  820. unsigned int clk_adjust; /* Clock adjust */
  821. clk_adjust = popts->clk_adjust;
  822. ddr->ddr_sdram_clk_cntl = (clk_adjust & 0xF) << 23;
  823. }
  824. /* DDR Initialization Address (DDR_INIT_ADDR) */
  825. static void set_ddr_init_addr(fsl_ddr_cfg_regs_t *ddr)
  826. {
  827. unsigned int init_addr = 0; /* Initialization address */
  828. ddr->ddr_init_addr = init_addr;
  829. }
  830. /* DDR Initialization Address (DDR_INIT_EXT_ADDR) */
  831. static void set_ddr_init_ext_addr(fsl_ddr_cfg_regs_t *ddr)
  832. {
  833. unsigned int uia = 0; /* Use initialization address */
  834. unsigned int init_ext_addr = 0; /* Initialization address */
  835. ddr->ddr_init_ext_addr = (0
  836. | ((uia & 0x1) << 31)
  837. | (init_ext_addr & 0xF)
  838. );
  839. }
  840. /* DDR SDRAM Timing Configuration 4 (TIMING_CFG_4) */
  841. static void set_timing_cfg_4(fsl_ddr_cfg_regs_t *ddr,
  842. const memctl_options_t *popts)
  843. {
  844. unsigned int rwt = 0; /* Read-to-write turnaround for same CS */
  845. unsigned int wrt = 0; /* Write-to-read turnaround for same CS */
  846. unsigned int rrt = 0; /* Read-to-read turnaround for same CS */
  847. unsigned int wwt = 0; /* Write-to-write turnaround for same CS */
  848. unsigned int dll_lock = 0; /* DDR SDRAM DLL Lock Time */
  849. #if defined(CONFIG_FSL_DDR3)
  850. if (popts->burst_length == DDR_BL8) {
  851. /* We set BL/2 for fixed BL8 */
  852. rrt = 0; /* BL/2 clocks */
  853. wwt = 0; /* BL/2 clocks */
  854. } else {
  855. /* We need to set BL/2 + 2 to BC4 and OTF */
  856. rrt = 2; /* BL/2 + 2 clocks */
  857. wwt = 2; /* BL/2 + 2 clocks */
  858. }
  859. dll_lock = 1; /* tDLLK = 512 clocks from spec */
  860. #endif
  861. ddr->timing_cfg_4 = (0
  862. | ((rwt & 0xf) << 28)
  863. | ((wrt & 0xf) << 24)
  864. | ((rrt & 0xf) << 20)
  865. | ((wwt & 0xf) << 16)
  866. | (dll_lock & 0x3)
  867. );
  868. debug("FSLDDR: timing_cfg_4 = 0x%08x\n", ddr->timing_cfg_4);
  869. }
  870. /* DDR SDRAM Timing Configuration 5 (TIMING_CFG_5) */
  871. static void set_timing_cfg_5(fsl_ddr_cfg_regs_t *ddr)
  872. {
  873. unsigned int rodt_on = 0; /* Read to ODT on */
  874. unsigned int rodt_off = 0; /* Read to ODT off */
  875. unsigned int wodt_on = 0; /* Write to ODT on */
  876. unsigned int wodt_off = 0; /* Write to ODT off */
  877. #if defined(CONFIG_FSL_DDR3)
  878. rodt_on = 3; /* 2 clocks */
  879. rodt_off = 4; /* 4 clocks */
  880. wodt_on = 2; /* 1 clocks */
  881. wodt_off = 4; /* 4 clocks */
  882. #endif
  883. ddr->timing_cfg_5 = (0
  884. | ((rodt_on & 0x1f) << 24)
  885. | ((rodt_off & 0x7) << 20)
  886. | ((wodt_on & 0x1f) << 12)
  887. | ((wodt_off & 0x7) << 8)
  888. );
  889. debug("FSLDDR: timing_cfg_5 = 0x%08x\n", ddr->timing_cfg_5);
  890. }
  891. /* DDR ZQ Calibration Control (DDR_ZQ_CNTL) */
  892. static void set_ddr_zq_cntl(fsl_ddr_cfg_regs_t *ddr, unsigned int zq_en)
  893. {
  894. unsigned int zqinit = 0;/* POR ZQ Calibration Time (tZQinit) */
  895. /* Normal Operation Full Calibration Time (tZQoper) */
  896. unsigned int zqoper = 0;
  897. /* Normal Operation Short Calibration Time (tZQCS) */
  898. unsigned int zqcs = 0;
  899. if (zq_en) {
  900. zqinit = 9; /* 512 clocks */
  901. zqoper = 8; /* 256 clocks */
  902. zqcs = 6; /* 64 clocks */
  903. }
  904. ddr->ddr_zq_cntl = (0
  905. | ((zq_en & 0x1) << 31)
  906. | ((zqinit & 0xF) << 24)
  907. | ((zqoper & 0xF) << 16)
  908. | ((zqcs & 0xF) << 8)
  909. );
  910. }
  911. /* DDR Write Leveling Control (DDR_WRLVL_CNTL) */
  912. static void set_ddr_wrlvl_cntl(fsl_ddr_cfg_regs_t *ddr, unsigned int wrlvl_en,
  913. const memctl_options_t *popts)
  914. {
  915. /*
  916. * First DQS pulse rising edge after margining mode
  917. * is programmed (tWL_MRD)
  918. */
  919. unsigned int wrlvl_mrd = 0;
  920. /* ODT delay after margining mode is programmed (tWL_ODTEN) */
  921. unsigned int wrlvl_odten = 0;
  922. /* DQS/DQS_ delay after margining mode is programmed (tWL_DQSEN) */
  923. unsigned int wrlvl_dqsen = 0;
  924. /* WRLVL_SMPL: Write leveling sample time */
  925. unsigned int wrlvl_smpl = 0;
  926. /* WRLVL_WLR: Write leveling repeition time */
  927. unsigned int wrlvl_wlr = 0;
  928. /* WRLVL_START: Write leveling start time */
  929. unsigned int wrlvl_start = 0;
  930. /* suggest enable write leveling for DDR3 due to fly-by topology */
  931. if (wrlvl_en) {
  932. /* tWL_MRD min = 40 nCK, we set it 64 */
  933. wrlvl_mrd = 0x6;
  934. /* tWL_ODTEN 128 */
  935. wrlvl_odten = 0x7;
  936. /* tWL_DQSEN min = 25 nCK, we set it 32 */
  937. wrlvl_dqsen = 0x5;
  938. /*
  939. * Write leveling sample time at least need 6 clocks
  940. * higher than tWLO to allow enough time for progagation
  941. * delay and sampling the prime data bits.
  942. */
  943. wrlvl_smpl = 0xf;
  944. /*
  945. * Write leveling repetition time
  946. * at least tWLO + 6 clocks clocks
  947. * we set it 32
  948. */
  949. wrlvl_wlr = 0x5;
  950. /*
  951. * Write leveling start time
  952. * The value use for the DQS_ADJUST for the first sample
  953. * when write leveling is enabled.
  954. */
  955. wrlvl_start = 0x8;
  956. /*
  957. * Override the write leveling sample and start time
  958. * according to specific board
  959. */
  960. if (popts->wrlvl_override) {
  961. wrlvl_smpl = popts->wrlvl_sample;
  962. wrlvl_start = popts->wrlvl_start;
  963. }
  964. }
  965. ddr->ddr_wrlvl_cntl = (0
  966. | ((wrlvl_en & 0x1) << 31)
  967. | ((wrlvl_mrd & 0x7) << 24)
  968. | ((wrlvl_odten & 0x7) << 20)
  969. | ((wrlvl_dqsen & 0x7) << 16)
  970. | ((wrlvl_smpl & 0xf) << 12)
  971. | ((wrlvl_wlr & 0x7) << 8)
  972. | ((wrlvl_start & 0x1F) << 0)
  973. );
  974. }
  975. /* DDR Self Refresh Counter (DDR_SR_CNTR) */
  976. static void set_ddr_sr_cntr(fsl_ddr_cfg_regs_t *ddr, unsigned int sr_it)
  977. {
  978. /* Self Refresh Idle Threshold */
  979. ddr->ddr_sr_cntr = (sr_it & 0xF) << 16;
  980. }
  981. /* DDR SDRAM Register Control Word 1 (DDR_SDRAM_RCW_1) */
  982. static void set_ddr_sdram_rcw_1(fsl_ddr_cfg_regs_t *ddr)
  983. {
  984. unsigned int rcw0 = 0; /* RCW0: Register Control Word 0 */
  985. unsigned int rcw1 = 0; /* RCW1: Register Control Word 1 */
  986. unsigned int rcw2 = 0; /* RCW2: Register Control Word 2 */
  987. unsigned int rcw3 = 0; /* RCW3: Register Control Word 3 */
  988. unsigned int rcw4 = 0; /* RCW4: Register Control Word 4 */
  989. unsigned int rcw5 = 0; /* RCW5: Register Control Word 5 */
  990. unsigned int rcw6 = 0; /* RCW6: Register Control Word 6 */
  991. unsigned int rcw7 = 0; /* RCW7: Register Control Word 7 */
  992. ddr->ddr_sdram_rcw_1 = (0
  993. | ((rcw0 & 0xF) << 28)
  994. | ((rcw1 & 0xF) << 24)
  995. | ((rcw2 & 0xF) << 20)
  996. | ((rcw3 & 0xF) << 16)
  997. | ((rcw4 & 0xF) << 12)
  998. | ((rcw5 & 0xF) << 8)
  999. | ((rcw6 & 0xF) << 4)
  1000. | ((rcw7 & 0xF) << 0)
  1001. );
  1002. }
  1003. /* DDR SDRAM Register Control Word 2 (DDR_SDRAM_RCW_2) */
  1004. static void set_ddr_sdram_rcw_2(fsl_ddr_cfg_regs_t *ddr)
  1005. {
  1006. unsigned int rcw8 = 0; /* RCW0: Register Control Word 8 */
  1007. unsigned int rcw9 = 0; /* RCW1: Register Control Word 9 */
  1008. unsigned int rcw10 = 0; /* RCW2: Register Control Word 10 */
  1009. unsigned int rcw11 = 0; /* RCW3: Register Control Word 11 */
  1010. unsigned int rcw12 = 0; /* RCW4: Register Control Word 12 */
  1011. unsigned int rcw13 = 0; /* RCW5: Register Control Word 13 */
  1012. unsigned int rcw14 = 0; /* RCW6: Register Control Word 14 */
  1013. unsigned int rcw15 = 0; /* RCW7: Register Control Word 15 */
  1014. ddr->ddr_sdram_rcw_2 = (0
  1015. | ((rcw8 & 0xF) << 28)
  1016. | ((rcw9 & 0xF) << 24)
  1017. | ((rcw10 & 0xF) << 20)
  1018. | ((rcw11 & 0xF) << 16)
  1019. | ((rcw12 & 0xF) << 12)
  1020. | ((rcw13 & 0xF) << 8)
  1021. | ((rcw14 & 0xF) << 4)
  1022. | ((rcw15 & 0xF) << 0)
  1023. );
  1024. }
  1025. unsigned int
  1026. check_fsl_memctl_config_regs(const fsl_ddr_cfg_regs_t *ddr)
  1027. {
  1028. unsigned int res = 0;
  1029. /*
  1030. * Check that DDR_SDRAM_CFG[RD_EN] and DDR_SDRAM_CFG[2T_EN] are
  1031. * not set at the same time.
  1032. */
  1033. if (ddr->ddr_sdram_cfg & 0x10000000
  1034. && ddr->ddr_sdram_cfg & 0x00008000) {
  1035. printf("Error: DDR_SDRAM_CFG[RD_EN] and DDR_SDRAM_CFG[2T_EN] "
  1036. " should not be set at the same time.\n");
  1037. res++;
  1038. }
  1039. return res;
  1040. }
  1041. unsigned int
  1042. compute_fsl_memctl_config_regs(const memctl_options_t *popts,
  1043. fsl_ddr_cfg_regs_t *ddr,
  1044. const common_timing_params_t *common_dimm,
  1045. const dimm_params_t *dimm_params,
  1046. unsigned int dbw_cap_adj)
  1047. {
  1048. unsigned int i;
  1049. unsigned int cas_latency;
  1050. unsigned int additive_latency;
  1051. unsigned int sr_it;
  1052. unsigned int zq_en;
  1053. unsigned int wrlvl_en;
  1054. memset(ddr, 0, sizeof(fsl_ddr_cfg_regs_t));
  1055. if (common_dimm == NULL) {
  1056. printf("Error: subset DIMM params struct null pointer\n");
  1057. return 1;
  1058. }
  1059. /*
  1060. * Process overrides first.
  1061. *
  1062. * FIXME: somehow add dereated caslat to this
  1063. */
  1064. cas_latency = (popts->cas_latency_override)
  1065. ? popts->cas_latency_override_value
  1066. : common_dimm->lowest_common_SPD_caslat;
  1067. additive_latency = (popts->additive_latency_override)
  1068. ? popts->additive_latency_override_value
  1069. : common_dimm->additive_latency;
  1070. sr_it = (popts->auto_self_refresh_en)
  1071. ? popts->sr_it
  1072. : 0;
  1073. /* ZQ calibration */
  1074. zq_en = (popts->zq_en) ? 1 : 0;
  1075. /* write leveling */
  1076. wrlvl_en = (popts->wrlvl_en) ? 1 : 0;
  1077. /* Chip Select Memory Bounds (CSn_BNDS) */
  1078. for (i = 0; i < CONFIG_CHIP_SELECTS_PER_CTRL; i++) {
  1079. unsigned long long ea = 0, sa = 0;
  1080. unsigned int cs_per_dimm
  1081. = CONFIG_CHIP_SELECTS_PER_CTRL / CONFIG_DIMM_SLOTS_PER_CTLR;
  1082. unsigned int dimm_number
  1083. = i / cs_per_dimm;
  1084. unsigned long long rank_density
  1085. = dimm_params[dimm_number].rank_density;
  1086. if (((i == 1) && (popts->ba_intlv_ctl & FSL_DDR_CS0_CS1)) ||
  1087. ((i == 2) && (popts->ba_intlv_ctl & 0x04)) ||
  1088. ((i == 3) && (popts->ba_intlv_ctl & FSL_DDR_CS2_CS3))) {
  1089. /*
  1090. * Don't set up boundaries for unused CS
  1091. * cs1 for cs0_cs1, cs0_cs1_and_cs2_cs3, cs0_cs1_cs2_cs3
  1092. * cs2 for cs0_cs1_cs2_cs3
  1093. * cs3 for cs2_cs3, cs0_cs1_and_cs2_cs3, cs0_cs1_cs2_cs3
  1094. * But we need to set the ODT_RD_CFG and
  1095. * ODT_WR_CFG for CS1_CONFIG here.
  1096. */
  1097. set_csn_config(i, ddr, popts, dimm_params);
  1098. continue;
  1099. }
  1100. if (dimm_params[dimm_number].n_ranks == 0) {
  1101. debug("Skipping setup of CS%u "
  1102. "because n_ranks on DIMM %u is 0\n", i, i/2);
  1103. continue;
  1104. }
  1105. if (popts->memctl_interleaving && popts->ba_intlv_ctl) {
  1106. /*
  1107. * This works superbank 2CS
  1108. * There are 2 or more memory controllers configured
  1109. * identically, memory is interleaved between them,
  1110. * and each controller uses rank interleaving within
  1111. * itself. Therefore the starting and ending address
  1112. * on each controller is twice the amount present on
  1113. * each controller.
  1114. */
  1115. unsigned long long ctlr_density = 0;
  1116. switch (popts->ba_intlv_ctl & FSL_DDR_CS0_CS1_CS2_CS3) {
  1117. case FSL_DDR_CS0_CS1:
  1118. case FSL_DDR_CS0_CS1_AND_CS2_CS3:
  1119. ctlr_density = dimm_params[0].rank_density * 2;
  1120. break;
  1121. case FSL_DDR_CS2_CS3:
  1122. ctlr_density = dimm_params[0].rank_density;
  1123. break;
  1124. case FSL_DDR_CS0_CS1_CS2_CS3:
  1125. /*
  1126. * The four CS interleaving should have been verified by
  1127. * populate_memctl_options()
  1128. */
  1129. ctlr_density = dimm_params[0].rank_density * 4;
  1130. break;
  1131. default:
  1132. break;
  1133. }
  1134. ea = (CONFIG_NUM_DDR_CONTROLLERS *
  1135. (ctlr_density >> dbw_cap_adj)) - 1;
  1136. }
  1137. else if (!popts->memctl_interleaving && popts->ba_intlv_ctl) {
  1138. /*
  1139. * If memory interleaving between controllers is NOT
  1140. * enabled, the starting address for each memory
  1141. * controller is distinct. However, because rank
  1142. * interleaving is enabled, the starting and ending
  1143. * addresses of the total memory on that memory
  1144. * controller needs to be programmed into its
  1145. * respective CS0_BNDS.
  1146. */
  1147. switch (popts->ba_intlv_ctl & FSL_DDR_CS0_CS1_CS2_CS3) {
  1148. case FSL_DDR_CS0_CS1_CS2_CS3:
  1149. /* CS0+CS1+CS2+CS3 interleaving, only CS0_CNDS
  1150. * needs to be set.
  1151. */
  1152. sa = common_dimm->base_address;
  1153. ea = sa + (4 * (rank_density >> dbw_cap_adj))-1;
  1154. break;
  1155. case FSL_DDR_CS0_CS1_AND_CS2_CS3:
  1156. /* CS0+CS1 and CS2+CS3 interleaving, CS0_CNDS
  1157. * and CS2_CNDS need to be set.
  1158. */
  1159. if ((i == 2) && (dimm_number == 0)) {
  1160. sa = dimm_params[dimm_number].base_address +
  1161. 2 * (rank_density >> dbw_cap_adj);
  1162. ea = sa + 2 * (rank_density >> dbw_cap_adj) - 1;
  1163. } else {
  1164. sa = dimm_params[dimm_number].base_address;
  1165. ea = sa + (2 * (rank_density >>
  1166. dbw_cap_adj)) - 1;
  1167. }
  1168. break;
  1169. case FSL_DDR_CS0_CS1:
  1170. /* CS0+CS1 interleaving, CS0_CNDS needs
  1171. * to be set
  1172. */
  1173. if (dimm_params[dimm_number].n_ranks > (i % cs_per_dimm)) {
  1174. sa = dimm_params[dimm_number].base_address;
  1175. ea = sa + (rank_density >> dbw_cap_adj) - 1;
  1176. sa += (i % cs_per_dimm) * (rank_density >> dbw_cap_adj);
  1177. ea += (i % cs_per_dimm) * (rank_density >> dbw_cap_adj);
  1178. } else {
  1179. sa = 0;
  1180. ea = 0;
  1181. }
  1182. if (i == 0)
  1183. ea += (rank_density >> dbw_cap_adj);
  1184. break;
  1185. case FSL_DDR_CS2_CS3:
  1186. /* CS2+CS3 interleaving*/
  1187. if (dimm_params[dimm_number].n_ranks > (i % cs_per_dimm)) {
  1188. sa = dimm_params[dimm_number].base_address;
  1189. ea = sa + (rank_density >> dbw_cap_adj) - 1;
  1190. sa += (i % cs_per_dimm) * (rank_density >> dbw_cap_adj);
  1191. ea += (i % cs_per_dimm) * (rank_density >> dbw_cap_adj);
  1192. } else {
  1193. sa = 0;
  1194. ea = 0;
  1195. }
  1196. if (i == 2)
  1197. ea += (rank_density >> dbw_cap_adj);
  1198. break;
  1199. default: /* No bank(chip-select) interleaving */
  1200. break;
  1201. }
  1202. }
  1203. else if (popts->memctl_interleaving && !popts->ba_intlv_ctl) {
  1204. /*
  1205. * Only the rank on CS0 of each memory controller may
  1206. * be used if memory controller interleaving is used
  1207. * without rank interleaving within each memory
  1208. * controller. However, the ending address programmed
  1209. * into each CS0 must be the sum of the amount of
  1210. * memory in the two CS0 ranks.
  1211. */
  1212. if (i == 0) {
  1213. ea = (2 * (rank_density >> dbw_cap_adj)) - 1;
  1214. }
  1215. }
  1216. else if (!popts->memctl_interleaving && !popts->ba_intlv_ctl) {
  1217. /*
  1218. * No rank interleaving and no memory controller
  1219. * interleaving.
  1220. */
  1221. sa = dimm_params[dimm_number].base_address;
  1222. ea = sa + (rank_density >> dbw_cap_adj) - 1;
  1223. if (dimm_params[dimm_number].n_ranks > (i % cs_per_dimm)) {
  1224. sa += (i % cs_per_dimm) * (rank_density >> dbw_cap_adj);
  1225. ea += (i % cs_per_dimm) * (rank_density >> dbw_cap_adj);
  1226. } else {
  1227. sa = 0;
  1228. ea = 0;
  1229. }
  1230. }
  1231. sa >>= 24;
  1232. ea >>= 24;
  1233. ddr->cs[i].bnds = (0
  1234. | ((sa & 0xFFF) << 16) /* starting address MSB */
  1235. | ((ea & 0xFFF) << 0) /* ending address MSB */
  1236. );
  1237. debug("FSLDDR: cs[%d]_bnds = 0x%08x\n", i, ddr->cs[i].bnds);
  1238. set_csn_config(i, ddr, popts, dimm_params);
  1239. set_csn_config_2(i, ddr);
  1240. }
  1241. #if !defined(CONFIG_FSL_DDR1)
  1242. set_timing_cfg_0(ddr);
  1243. #endif
  1244. set_timing_cfg_3(ddr, common_dimm, cas_latency);
  1245. set_timing_cfg_1(ddr, popts, common_dimm, cas_latency);
  1246. set_timing_cfg_2(ddr, popts, common_dimm,
  1247. cas_latency, additive_latency);
  1248. set_ddr_sdram_cfg(ddr, popts, common_dimm);
  1249. set_ddr_sdram_cfg_2(ddr, popts);
  1250. set_ddr_sdram_mode(ddr, popts, common_dimm,
  1251. cas_latency, additive_latency);
  1252. set_ddr_sdram_mode_2(ddr, popts);
  1253. set_ddr_sdram_interval(ddr, popts, common_dimm);
  1254. set_ddr_data_init(ddr);
  1255. set_ddr_sdram_clk_cntl(ddr, popts);
  1256. set_ddr_init_addr(ddr);
  1257. set_ddr_init_ext_addr(ddr);
  1258. set_timing_cfg_4(ddr, popts);
  1259. set_timing_cfg_5(ddr);
  1260. set_ddr_zq_cntl(ddr, zq_en);
  1261. set_ddr_wrlvl_cntl(ddr, wrlvl_en, popts);
  1262. set_ddr_sr_cntr(ddr, sr_it);
  1263. set_ddr_sdram_rcw_1(ddr);
  1264. set_ddr_sdram_rcw_2(ddr);
  1265. return check_fsl_memctl_config_regs(ddr);
  1266. }