ctrl_regs.c 38 KB

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