ctrl_regs.c 39 KB

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