ctrl_regs.c 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351
  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. {
  528. unsigned short esdmode2 = 0; /* Extended SDRAM mode 2 */
  529. unsigned short esdmode3 = 0; /* Extended SDRAM mode 3 */
  530. #if defined(CONFIG_FSL_DDR3)
  531. unsigned int rtt_wr = 2; /* 120 ohm Rtt_WR */
  532. unsigned int srt = 0; /* self-refresh temerature, normal range */
  533. unsigned int asr = 0; /* auto self-refresh disable */
  534. unsigned int cwl = compute_cas_write_latency() - 5;
  535. unsigned int pasr = 0; /* partial array self refresh disable */
  536. esdmode2 = (0
  537. | ((rtt_wr & 0x3) << 9)
  538. | ((srt & 0x1) << 7)
  539. | ((asr & 0x1) << 6)
  540. | ((cwl & 0x7) << 3)
  541. | ((pasr & 0x7) << 0));
  542. #endif
  543. ddr->ddr_sdram_mode_2 = (0
  544. | ((esdmode2 & 0xFFFF) << 16)
  545. | ((esdmode3 & 0xFFFF) << 0)
  546. );
  547. debug("FSLDDR: ddr_sdram_mode_2 = 0x%08x\n", ddr->ddr_sdram_mode_2);
  548. }
  549. /* DDR SDRAM Interval Configuration (DDR_SDRAM_INTERVAL) */
  550. static void set_ddr_sdram_interval(fsl_ddr_cfg_regs_t *ddr,
  551. const memctl_options_t *popts,
  552. const common_timing_params_t *common_dimm)
  553. {
  554. unsigned int refint; /* Refresh interval */
  555. unsigned int bstopre; /* Precharge interval */
  556. refint = picos_to_mclk(common_dimm->refresh_rate_ps);
  557. bstopre = popts->bstopre;
  558. /* refint field used 0x3FFF in earlier controllers */
  559. ddr->ddr_sdram_interval = (0
  560. | ((refint & 0xFFFF) << 16)
  561. | ((bstopre & 0x3FFF) << 0)
  562. );
  563. debug("FSLDDR: ddr_sdram_interval = 0x%08x\n", ddr->ddr_sdram_interval);
  564. }
  565. #if defined(CONFIG_FSL_DDR3)
  566. /* DDR SDRAM Mode configuration set (DDR_SDRAM_MODE) */
  567. static void set_ddr_sdram_mode(fsl_ddr_cfg_regs_t *ddr,
  568. const memctl_options_t *popts,
  569. const common_timing_params_t *common_dimm,
  570. unsigned int cas_latency,
  571. unsigned int additive_latency)
  572. {
  573. unsigned short esdmode; /* Extended SDRAM mode */
  574. unsigned short sdmode; /* SDRAM mode */
  575. /* Mode Register - MR1 */
  576. unsigned int qoff = 0; /* Output buffer enable 0=yes, 1=no */
  577. unsigned int tdqs_en = 0; /* TDQS Enable: 0=no, 1=yes */
  578. unsigned int rtt;
  579. unsigned int wrlvl_en = 0; /* Write level enable: 0=no, 1=yes */
  580. unsigned int al = 0; /* Posted CAS# additive latency (AL) */
  581. unsigned int dic = 1; /* Output driver impedance, 34ohm */
  582. unsigned int dll_en = 0; /* DLL Enable 0=Enable (Normal),
  583. 1=Disable (Test/Debug) */
  584. /* Mode Register - MR0 */
  585. unsigned int dll_on; /* DLL control for precharge PD, 0=off, 1=on */
  586. unsigned int wr; /* Write Recovery */
  587. unsigned int dll_rst; /* DLL Reset */
  588. unsigned int mode; /* Normal=0 or Test=1 */
  589. unsigned int caslat = 4;/* CAS# latency, default set as 6 cycles */
  590. /* BT: Burst Type (0=Nibble Sequential, 1=Interleaved) */
  591. unsigned int bt;
  592. unsigned int bl; /* BL: Burst Length */
  593. unsigned int wr_mclk;
  594. const unsigned int mclk_ps = get_memory_clk_period_ps();
  595. rtt = fsl_ddr_get_rtt();
  596. if (popts->rtt_override)
  597. rtt = popts->rtt_override_value;
  598. if (additive_latency == (cas_latency - 1))
  599. al = 1;
  600. if (additive_latency == (cas_latency - 2))
  601. al = 2;
  602. /*
  603. * The esdmode value will also be used for writing
  604. * MR1 during write leveling for DDR3, although the
  605. * bits specifically related to the write leveling
  606. * scheme will be handled automatically by the DDR
  607. * controller. so we set the wrlvl_en = 0 here.
  608. */
  609. esdmode = (0
  610. | ((qoff & 0x1) << 12)
  611. | ((tdqs_en & 0x1) << 11)
  612. | ((rtt & 0x4) << 7) /* rtt field is split */
  613. | ((wrlvl_en & 0x1) << 7)
  614. | ((rtt & 0x2) << 5) /* rtt field is split */
  615. | ((dic & 0x2) << 4) /* DIC field is split */
  616. | ((al & 0x3) << 3)
  617. | ((rtt & 0x1) << 2) /* rtt field is split */
  618. | ((dic & 0x1) << 1) /* DIC field is split */
  619. | ((dll_en & 0x1) << 0)
  620. );
  621. /*
  622. * DLL control for precharge PD
  623. * 0=slow exit DLL off (tXPDLL)
  624. * 1=fast exit DLL on (tXP)
  625. */
  626. dll_on = 1;
  627. wr_mclk = (common_dimm->tWR_ps + mclk_ps - 1) / mclk_ps;
  628. if (wr_mclk >= 12)
  629. wr = 6;
  630. else if (wr_mclk >= 9)
  631. wr = 5;
  632. else
  633. wr = wr_mclk - 4;
  634. dll_rst = 0; /* dll no reset */
  635. mode = 0; /* normal mode */
  636. /* look up table to get the cas latency bits */
  637. if (cas_latency >= 5 && cas_latency <= 11) {
  638. unsigned char cas_latency_table[7] = {
  639. 0x2, /* 5 clocks */
  640. 0x4, /* 6 clocks */
  641. 0x6, /* 7 clocks */
  642. 0x8, /* 8 clocks */
  643. 0xa, /* 9 clocks */
  644. 0xc, /* 10 clocks */
  645. 0xe /* 11 clocks */
  646. };
  647. caslat = cas_latency_table[cas_latency - 5];
  648. }
  649. bt = 0; /* Nibble sequential */
  650. switch (popts->burst_length) {
  651. case DDR_BL8:
  652. bl = 0;
  653. break;
  654. case DDR_OTF:
  655. bl = 1;
  656. break;
  657. case DDR_BC4:
  658. bl = 2;
  659. break;
  660. default:
  661. printf("Error: invalid burst length of %u specified. "
  662. " Defaulting to on-the-fly BC4 or BL8 beats.\n",
  663. popts->burst_length);
  664. bl = 1;
  665. break;
  666. }
  667. sdmode = (0
  668. | ((dll_on & 0x1) << 12)
  669. | ((wr & 0x7) << 9)
  670. | ((dll_rst & 0x1) << 8)
  671. | ((mode & 0x1) << 7)
  672. | (((caslat >> 1) & 0x7) << 4)
  673. | ((bt & 0x1) << 3)
  674. | ((bl & 0x3) << 0)
  675. );
  676. ddr->ddr_sdram_mode = (0
  677. | ((esdmode & 0xFFFF) << 16)
  678. | ((sdmode & 0xFFFF) << 0)
  679. );
  680. debug("FSLDDR: ddr_sdram_mode = 0x%08x\n", ddr->ddr_sdram_mode);
  681. }
  682. #else /* !CONFIG_FSL_DDR3 */
  683. /* DDR SDRAM Mode configuration set (DDR_SDRAM_MODE) */
  684. static void set_ddr_sdram_mode(fsl_ddr_cfg_regs_t *ddr,
  685. const memctl_options_t *popts,
  686. const common_timing_params_t *common_dimm,
  687. unsigned int cas_latency,
  688. unsigned int additive_latency)
  689. {
  690. unsigned short esdmode; /* Extended SDRAM mode */
  691. unsigned short sdmode; /* SDRAM mode */
  692. /*
  693. * FIXME: This ought to be pre-calculated in a
  694. * technology-specific routine,
  695. * e.g. compute_DDR2_mode_register(), and then the
  696. * sdmode and esdmode passed in as part of common_dimm.
  697. */
  698. /* Extended Mode Register */
  699. unsigned int mrs = 0; /* Mode Register Set */
  700. unsigned int outputs = 0; /* 0=Enabled, 1=Disabled */
  701. unsigned int rdqs_en = 0; /* RDQS Enable: 0=no, 1=yes */
  702. unsigned int dqs_en = 0; /* DQS# Enable: 0=enable, 1=disable */
  703. unsigned int ocd = 0; /* 0x0=OCD not supported,
  704. 0x7=OCD default state */
  705. unsigned int rtt;
  706. unsigned int al; /* Posted CAS# additive latency (AL) */
  707. unsigned int ods = 0; /* Output Drive Strength:
  708. 0 = Full strength (18ohm)
  709. 1 = Reduced strength (4ohm) */
  710. unsigned int dll_en = 0; /* DLL Enable 0=Enable (Normal),
  711. 1=Disable (Test/Debug) */
  712. /* Mode Register (MR) */
  713. unsigned int mr; /* Mode Register Definition */
  714. unsigned int pd; /* Power-Down Mode */
  715. unsigned int wr; /* Write Recovery */
  716. unsigned int dll_res; /* DLL Reset */
  717. unsigned int mode; /* Normal=0 or Test=1 */
  718. unsigned int caslat = 0;/* CAS# latency */
  719. /* BT: Burst Type (0=Sequential, 1=Interleaved) */
  720. unsigned int bt;
  721. unsigned int bl; /* BL: Burst Length */
  722. #if defined(CONFIG_FSL_DDR2)
  723. const unsigned int mclk_ps = get_memory_clk_period_ps();
  724. #endif
  725. rtt = fsl_ddr_get_rtt();
  726. al = additive_latency;
  727. esdmode = (0
  728. | ((mrs & 0x3) << 14)
  729. | ((outputs & 0x1) << 12)
  730. | ((rdqs_en & 0x1) << 11)
  731. | ((dqs_en & 0x1) << 10)
  732. | ((ocd & 0x7) << 7)
  733. | ((rtt & 0x2) << 5) /* rtt field is split */
  734. | ((al & 0x7) << 3)
  735. | ((rtt & 0x1) << 2) /* rtt field is split */
  736. | ((ods & 0x1) << 1)
  737. | ((dll_en & 0x1) << 0)
  738. );
  739. mr = 0; /* FIXME: CHECKME */
  740. /*
  741. * 0 = Fast Exit (Normal)
  742. * 1 = Slow Exit (Low Power)
  743. */
  744. pd = 0;
  745. #if defined(CONFIG_FSL_DDR1)
  746. wr = 0; /* Historical */
  747. #elif defined(CONFIG_FSL_DDR2)
  748. wr = (common_dimm->tWR_ps + mclk_ps - 1) / mclk_ps - 1;
  749. #endif
  750. dll_res = 0;
  751. mode = 0;
  752. #if defined(CONFIG_FSL_DDR1)
  753. if (1 <= cas_latency && cas_latency <= 4) {
  754. unsigned char mode_caslat_table[4] = {
  755. 0x5, /* 1.5 clocks */
  756. 0x2, /* 2.0 clocks */
  757. 0x6, /* 2.5 clocks */
  758. 0x3 /* 3.0 clocks */
  759. };
  760. caslat = mode_caslat_table[cas_latency - 1];
  761. } else {
  762. printf("Warning: unknown cas_latency %d\n", cas_latency);
  763. }
  764. #elif defined(CONFIG_FSL_DDR2)
  765. caslat = cas_latency;
  766. #endif
  767. bt = 0;
  768. switch (popts->burst_length) {
  769. case DDR_BL4:
  770. bl = 2;
  771. break;
  772. case DDR_BL8:
  773. bl = 3;
  774. break;
  775. default:
  776. printf("Error: invalid burst length of %u specified. "
  777. " Defaulting to 4 beats.\n",
  778. popts->burst_length);
  779. bl = 2;
  780. break;
  781. }
  782. sdmode = (0
  783. | ((mr & 0x3) << 14)
  784. | ((pd & 0x1) << 12)
  785. | ((wr & 0x7) << 9)
  786. | ((dll_res & 0x1) << 8)
  787. | ((mode & 0x1) << 7)
  788. | ((caslat & 0x7) << 4)
  789. | ((bt & 0x1) << 3)
  790. | ((bl & 0x7) << 0)
  791. );
  792. ddr->ddr_sdram_mode = (0
  793. | ((esdmode & 0xFFFF) << 16)
  794. | ((sdmode & 0xFFFF) << 0)
  795. );
  796. debug("FSLDDR: ddr_sdram_mode = 0x%08x\n", ddr->ddr_sdram_mode);
  797. }
  798. #endif
  799. /* DDR SDRAM Data Initialization (DDR_DATA_INIT) */
  800. static void set_ddr_data_init(fsl_ddr_cfg_regs_t *ddr)
  801. {
  802. unsigned int init_value; /* Initialization value */
  803. init_value = 0xDEADBEEF;
  804. ddr->ddr_data_init = init_value;
  805. }
  806. /*
  807. * DDR SDRAM Clock Control (DDR_SDRAM_CLK_CNTL)
  808. * The old controller on the 8540/60 doesn't have this register.
  809. * Hope it's OK to set it (to 0) anyway.
  810. */
  811. static void set_ddr_sdram_clk_cntl(fsl_ddr_cfg_regs_t *ddr,
  812. const memctl_options_t *popts)
  813. {
  814. unsigned int clk_adjust; /* Clock adjust */
  815. clk_adjust = popts->clk_adjust;
  816. ddr->ddr_sdram_clk_cntl = (clk_adjust & 0xF) << 23;
  817. }
  818. /* DDR Initialization Address (DDR_INIT_ADDR) */
  819. static void set_ddr_init_addr(fsl_ddr_cfg_regs_t *ddr)
  820. {
  821. unsigned int init_addr = 0; /* Initialization address */
  822. ddr->ddr_init_addr = init_addr;
  823. }
  824. /* DDR Initialization Address (DDR_INIT_EXT_ADDR) */
  825. static void set_ddr_init_ext_addr(fsl_ddr_cfg_regs_t *ddr)
  826. {
  827. unsigned int uia = 0; /* Use initialization address */
  828. unsigned int init_ext_addr = 0; /* Initialization address */
  829. ddr->ddr_init_ext_addr = (0
  830. | ((uia & 0x1) << 31)
  831. | (init_ext_addr & 0xF)
  832. );
  833. }
  834. /* DDR SDRAM Timing Configuration 4 (TIMING_CFG_4) */
  835. static void set_timing_cfg_4(fsl_ddr_cfg_regs_t *ddr)
  836. {
  837. unsigned int rwt = 0; /* Read-to-write turnaround for same CS */
  838. unsigned int wrt = 0; /* Write-to-read turnaround for same CS */
  839. unsigned int rrt = 0; /* Read-to-read turnaround for same CS */
  840. unsigned int wwt = 0; /* Write-to-write turnaround for same CS */
  841. unsigned int dll_lock = 0; /* DDR SDRAM DLL Lock Time */
  842. #if defined(CONFIG_FSL_DDR3)
  843. /* We need set BL/2 + 4 for BC4 or OTF */
  844. rrt = 4; /* BL/2 + 4 clocks */
  845. wwt = 4; /* BL/2 + 4 clocks */
  846. dll_lock = 1; /* tDLLK = 512 clocks from spec */
  847. #endif
  848. ddr->timing_cfg_4 = (0
  849. | ((rwt & 0xf) << 28)
  850. | ((wrt & 0xf) << 24)
  851. | ((rrt & 0xf) << 20)
  852. | ((wwt & 0xf) << 16)
  853. | (dll_lock & 0x3)
  854. );
  855. debug("FSLDDR: timing_cfg_4 = 0x%08x\n", ddr->timing_cfg_4);
  856. }
  857. /* DDR SDRAM Timing Configuration 5 (TIMING_CFG_5) */
  858. static void set_timing_cfg_5(fsl_ddr_cfg_regs_t *ddr)
  859. {
  860. unsigned int rodt_on = 0; /* Read to ODT on */
  861. unsigned int rodt_off = 0; /* Read to ODT off */
  862. unsigned int wodt_on = 0; /* Write to ODT on */
  863. unsigned int wodt_off = 0; /* Write to ODT off */
  864. #if defined(CONFIG_FSL_DDR3)
  865. rodt_on = 3; /* 2 clocks */
  866. rodt_off = 4; /* 4 clocks */
  867. wodt_on = 2; /* 1 clocks */
  868. wodt_off = 4; /* 4 clocks */
  869. #endif
  870. ddr->timing_cfg_5 = (0
  871. | ((rodt_on & 0x1f) << 24)
  872. | ((rodt_off & 0x7) << 20)
  873. | ((wodt_on & 0x1f) << 12)
  874. | ((wodt_off & 0x7) << 8)
  875. );
  876. debug("FSLDDR: timing_cfg_5 = 0x%08x\n", ddr->timing_cfg_5);
  877. }
  878. /* DDR ZQ Calibration Control (DDR_ZQ_CNTL) */
  879. static void set_ddr_zq_cntl(fsl_ddr_cfg_regs_t *ddr, unsigned int zq_en)
  880. {
  881. unsigned int zqinit = 0;/* POR ZQ Calibration Time (tZQinit) */
  882. /* Normal Operation Full Calibration Time (tZQoper) */
  883. unsigned int zqoper = 0;
  884. /* Normal Operation Short Calibration Time (tZQCS) */
  885. unsigned int zqcs = 0;
  886. if (zq_en) {
  887. zqinit = 9; /* 512 clocks */
  888. zqoper = 8; /* 256 clocks */
  889. zqcs = 6; /* 64 clocks */
  890. }
  891. ddr->ddr_zq_cntl = (0
  892. | ((zq_en & 0x1) << 31)
  893. | ((zqinit & 0xF) << 24)
  894. | ((zqoper & 0xF) << 16)
  895. | ((zqcs & 0xF) << 8)
  896. );
  897. }
  898. /* DDR Write Leveling Control (DDR_WRLVL_CNTL) */
  899. static void set_ddr_wrlvl_cntl(fsl_ddr_cfg_regs_t *ddr, unsigned int wrlvl_en,
  900. const memctl_options_t *popts)
  901. {
  902. /*
  903. * First DQS pulse rising edge after margining mode
  904. * is programmed (tWL_MRD)
  905. */
  906. unsigned int wrlvl_mrd = 0;
  907. /* ODT delay after margining mode is programmed (tWL_ODTEN) */
  908. unsigned int wrlvl_odten = 0;
  909. /* DQS/DQS_ delay after margining mode is programmed (tWL_DQSEN) */
  910. unsigned int wrlvl_dqsen = 0;
  911. /* WRLVL_SMPL: Write leveling sample time */
  912. unsigned int wrlvl_smpl = 0;
  913. /* WRLVL_WLR: Write leveling repeition time */
  914. unsigned int wrlvl_wlr = 0;
  915. /* WRLVL_START: Write leveling start time */
  916. unsigned int wrlvl_start = 0;
  917. /* suggest enable write leveling for DDR3 due to fly-by topology */
  918. if (wrlvl_en) {
  919. /* tWL_MRD min = 40 nCK, we set it 64 */
  920. wrlvl_mrd = 0x6;
  921. /* tWL_ODTEN 128 */
  922. wrlvl_odten = 0x7;
  923. /* tWL_DQSEN min = 25 nCK, we set it 32 */
  924. wrlvl_dqsen = 0x5;
  925. /*
  926. * Write leveling sample time at least need 6 clocks
  927. * higher than tWLO to allow enough time for progagation
  928. * delay and sampling the prime data bits.
  929. */
  930. wrlvl_smpl = 0xf;
  931. /*
  932. * Write leveling repetition time
  933. * at least tWLO + 6 clocks clocks
  934. * we set it 32
  935. */
  936. wrlvl_wlr = 0x5;
  937. /*
  938. * Write leveling start time
  939. * The value use for the DQS_ADJUST for the first sample
  940. * when write leveling is enabled.
  941. */
  942. wrlvl_start = 0x8;
  943. /*
  944. * Override the write leveling sample and start time
  945. * according to specific board
  946. */
  947. if (popts->wrlvl_override) {
  948. wrlvl_smpl = popts->wrlvl_sample;
  949. wrlvl_start = popts->wrlvl_start;
  950. }
  951. }
  952. ddr->ddr_wrlvl_cntl = (0
  953. | ((wrlvl_en & 0x1) << 31)
  954. | ((wrlvl_mrd & 0x7) << 24)
  955. | ((wrlvl_odten & 0x7) << 20)
  956. | ((wrlvl_dqsen & 0x7) << 16)
  957. | ((wrlvl_smpl & 0xf) << 12)
  958. | ((wrlvl_wlr & 0x7) << 8)
  959. | ((wrlvl_start & 0x1F) << 0)
  960. );
  961. }
  962. /* DDR Self Refresh Counter (DDR_SR_CNTR) */
  963. static void set_ddr_sr_cntr(fsl_ddr_cfg_regs_t *ddr, unsigned int sr_it)
  964. {
  965. /* Self Refresh Idle Threshold */
  966. ddr->ddr_sr_cntr = (sr_it & 0xF) << 16;
  967. }
  968. /* DDR SDRAM Register Control Word 1 (DDR_SDRAM_RCW_1) */
  969. static void set_ddr_sdram_rcw_1(fsl_ddr_cfg_regs_t *ddr)
  970. {
  971. unsigned int rcw0 = 0; /* RCW0: Register Control Word 0 */
  972. unsigned int rcw1 = 0; /* RCW1: Register Control Word 1 */
  973. unsigned int rcw2 = 0; /* RCW2: Register Control Word 2 */
  974. unsigned int rcw3 = 0; /* RCW3: Register Control Word 3 */
  975. unsigned int rcw4 = 0; /* RCW4: Register Control Word 4 */
  976. unsigned int rcw5 = 0; /* RCW5: Register Control Word 5 */
  977. unsigned int rcw6 = 0; /* RCW6: Register Control Word 6 */
  978. unsigned int rcw7 = 0; /* RCW7: Register Control Word 7 */
  979. ddr->ddr_sdram_rcw_1 = (0
  980. | ((rcw0 & 0xF) << 28)
  981. | ((rcw1 & 0xF) << 24)
  982. | ((rcw2 & 0xF) << 20)
  983. | ((rcw3 & 0xF) << 16)
  984. | ((rcw4 & 0xF) << 12)
  985. | ((rcw5 & 0xF) << 8)
  986. | ((rcw6 & 0xF) << 4)
  987. | ((rcw7 & 0xF) << 0)
  988. );
  989. }
  990. /* DDR SDRAM Register Control Word 2 (DDR_SDRAM_RCW_2) */
  991. static void set_ddr_sdram_rcw_2(fsl_ddr_cfg_regs_t *ddr)
  992. {
  993. unsigned int rcw8 = 0; /* RCW0: Register Control Word 8 */
  994. unsigned int rcw9 = 0; /* RCW1: Register Control Word 9 */
  995. unsigned int rcw10 = 0; /* RCW2: Register Control Word 10 */
  996. unsigned int rcw11 = 0; /* RCW3: Register Control Word 11 */
  997. unsigned int rcw12 = 0; /* RCW4: Register Control Word 12 */
  998. unsigned int rcw13 = 0; /* RCW5: Register Control Word 13 */
  999. unsigned int rcw14 = 0; /* RCW6: Register Control Word 14 */
  1000. unsigned int rcw15 = 0; /* RCW7: Register Control Word 15 */
  1001. ddr->ddr_sdram_rcw_2 = (0
  1002. | ((rcw8 & 0xF) << 28)
  1003. | ((rcw9 & 0xF) << 24)
  1004. | ((rcw10 & 0xF) << 20)
  1005. | ((rcw11 & 0xF) << 16)
  1006. | ((rcw12 & 0xF) << 12)
  1007. | ((rcw13 & 0xF) << 8)
  1008. | ((rcw14 & 0xF) << 4)
  1009. | ((rcw15 & 0xF) << 0)
  1010. );
  1011. }
  1012. unsigned int
  1013. check_fsl_memctl_config_regs(const fsl_ddr_cfg_regs_t *ddr)
  1014. {
  1015. unsigned int res = 0;
  1016. /*
  1017. * Check that DDR_SDRAM_CFG[RD_EN] and DDR_SDRAM_CFG[2T_EN] are
  1018. * not set at the same time.
  1019. */
  1020. if (ddr->ddr_sdram_cfg & 0x10000000
  1021. && ddr->ddr_sdram_cfg & 0x00008000) {
  1022. printf("Error: DDR_SDRAM_CFG[RD_EN] and DDR_SDRAM_CFG[2T_EN] "
  1023. " should not be set at the same time.\n");
  1024. res++;
  1025. }
  1026. return res;
  1027. }
  1028. unsigned int
  1029. compute_fsl_memctl_config_regs(const memctl_options_t *popts,
  1030. fsl_ddr_cfg_regs_t *ddr,
  1031. const common_timing_params_t *common_dimm,
  1032. const dimm_params_t *dimm_params,
  1033. unsigned int dbw_cap_adj)
  1034. {
  1035. unsigned int i;
  1036. unsigned int cas_latency;
  1037. unsigned int additive_latency;
  1038. unsigned int sr_it;
  1039. unsigned int zq_en;
  1040. unsigned int wrlvl_en;
  1041. memset(ddr, 0, sizeof(fsl_ddr_cfg_regs_t));
  1042. if (common_dimm == NULL) {
  1043. printf("Error: subset DIMM params struct null pointer\n");
  1044. return 1;
  1045. }
  1046. /*
  1047. * Process overrides first.
  1048. *
  1049. * FIXME: somehow add dereated caslat to this
  1050. */
  1051. cas_latency = (popts->cas_latency_override)
  1052. ? popts->cas_latency_override_value
  1053. : common_dimm->lowest_common_SPD_caslat;
  1054. additive_latency = (popts->additive_latency_override)
  1055. ? popts->additive_latency_override_value
  1056. : common_dimm->additive_latency;
  1057. sr_it = (popts->auto_self_refresh_en)
  1058. ? popts->sr_it
  1059. : 0;
  1060. /* ZQ calibration */
  1061. zq_en = (popts->zq_en) ? 1 : 0;
  1062. /* write leveling */
  1063. wrlvl_en = (popts->wrlvl_en) ? 1 : 0;
  1064. /* Chip Select Memory Bounds (CSn_BNDS) */
  1065. for (i = 0; i < CONFIG_CHIP_SELECTS_PER_CTRL; i++) {
  1066. unsigned long long ea = 0, sa = 0;
  1067. if (popts->ba_intlv_ctl && (i > 0) &&
  1068. ((popts->ba_intlv_ctl & 0x60) != FSL_DDR_CS2_CS3 )) {
  1069. /* Don't set up boundaries for other CS
  1070. * other than CS0, if bank interleaving
  1071. * is enabled and not CS2+CS3 interleaved.
  1072. */
  1073. break;
  1074. }
  1075. if (dimm_params[i/2].n_ranks == 0) {
  1076. debug("Skipping setup of CS%u "
  1077. "because n_ranks on DIMM %u is 0\n", i, i/2);
  1078. continue;
  1079. }
  1080. if (popts->memctl_interleaving && popts->ba_intlv_ctl) {
  1081. /*
  1082. * This works superbank 2CS
  1083. * There are 2 memory controllers configured
  1084. * identically, memory is interleaved between them,
  1085. * and each controller uses rank interleaving within
  1086. * itself. Therefore the starting and ending address
  1087. * on each controller is twice the amount present on
  1088. * each controller.
  1089. */
  1090. unsigned long long rank_density
  1091. = dimm_params[0].capacity;
  1092. ea = (2 * (rank_density >> dbw_cap_adj)) - 1;
  1093. }
  1094. else if (!popts->memctl_interleaving && popts->ba_intlv_ctl) {
  1095. /*
  1096. * If memory interleaving between controllers is NOT
  1097. * enabled, the starting address for each memory
  1098. * controller is distinct. However, because rank
  1099. * interleaving is enabled, the starting and ending
  1100. * addresses of the total memory on that memory
  1101. * controller needs to be programmed into its
  1102. * respective CS0_BNDS.
  1103. */
  1104. unsigned long long rank_density
  1105. = dimm_params[i/2].rank_density;
  1106. switch (popts->ba_intlv_ctl & FSL_DDR_CS0_CS1_CS2_CS3) {
  1107. case FSL_DDR_CS0_CS1_CS2_CS3:
  1108. /* CS0+CS1+CS2+CS3 interleaving, only CS0_CNDS
  1109. * needs to be set.
  1110. */
  1111. sa = common_dimm->base_address;
  1112. ea = sa + (4 * (rank_density >> dbw_cap_adj))-1;
  1113. break;
  1114. case FSL_DDR_CS0_CS1_AND_CS2_CS3:
  1115. /* CS0+CS1 and CS2+CS3 interleaving, CS0_CNDS
  1116. * and CS2_CNDS need to be set.
  1117. */
  1118. if (!(i&1)) {
  1119. sa = dimm_params[i/2].base_address;
  1120. ea = sa + (i * (rank_density >>
  1121. dbw_cap_adj)) - 1;
  1122. }
  1123. break;
  1124. case FSL_DDR_CS0_CS1:
  1125. /* CS0+CS1 interleaving, CS0_CNDS needs
  1126. * to be set
  1127. */
  1128. sa = common_dimm->base_address;
  1129. ea = sa + (2 * (rank_density >> dbw_cap_adj))-1;
  1130. break;
  1131. case FSL_DDR_CS2_CS3:
  1132. /* CS2+CS3 interleaving*/
  1133. if (i == 2) {
  1134. sa = dimm_params[i/2].base_address;
  1135. ea = sa + (2 * (rank_density >>
  1136. dbw_cap_adj)) - 1;
  1137. }
  1138. break;
  1139. default: /* No bank(chip-select) interleaving */
  1140. break;
  1141. }
  1142. }
  1143. else if (popts->memctl_interleaving && !popts->ba_intlv_ctl) {
  1144. /*
  1145. * Only the rank on CS0 of each memory controller may
  1146. * be used if memory controller interleaving is used
  1147. * without rank interleaving within each memory
  1148. * controller. However, the ending address programmed
  1149. * into each CS0 must be the sum of the amount of
  1150. * memory in the two CS0 ranks.
  1151. */
  1152. if (i == 0) {
  1153. unsigned long long rank_density
  1154. = dimm_params[0].rank_density;
  1155. ea = (2 * (rank_density >> dbw_cap_adj)) - 1;
  1156. }
  1157. }
  1158. else if (!popts->memctl_interleaving && !popts->ba_intlv_ctl) {
  1159. /*
  1160. * No rank interleaving and no memory controller
  1161. * interleaving.
  1162. */
  1163. unsigned long long rank_density
  1164. = dimm_params[i/2].rank_density;
  1165. sa = dimm_params[i/2].base_address;
  1166. ea = sa + (rank_density >> dbw_cap_adj) - 1;
  1167. if (i&1) {
  1168. if ((dimm_params[i/2].n_ranks == 1)) {
  1169. /* Odd chip select, single-rank dimm */
  1170. sa = 0;
  1171. ea = 0;
  1172. } else {
  1173. /* Odd chip select, dual-rank DIMM */
  1174. sa += rank_density >> dbw_cap_adj;
  1175. ea += rank_density >> dbw_cap_adj;
  1176. }
  1177. }
  1178. }
  1179. sa >>= 24;
  1180. ea >>= 24;
  1181. ddr->cs[i].bnds = (0
  1182. | ((sa & 0xFFF) << 16) /* starting address MSB */
  1183. | ((ea & 0xFFF) << 0) /* ending address MSB */
  1184. );
  1185. debug("FSLDDR: cs[%d]_bnds = 0x%08x\n", i, ddr->cs[i].bnds);
  1186. set_csn_config(i, ddr, popts, dimm_params);
  1187. set_csn_config_2(i, ddr);
  1188. }
  1189. #if !defined(CONFIG_FSL_DDR1)
  1190. set_timing_cfg_0(ddr);
  1191. #endif
  1192. set_timing_cfg_3(ddr, common_dimm, cas_latency);
  1193. set_timing_cfg_1(ddr, popts, common_dimm, cas_latency);
  1194. set_timing_cfg_2(ddr, popts, common_dimm,
  1195. cas_latency, additive_latency);
  1196. set_ddr_sdram_cfg(ddr, popts, common_dimm);
  1197. set_ddr_sdram_cfg_2(ddr, popts);
  1198. set_ddr_sdram_mode(ddr, popts, common_dimm,
  1199. cas_latency, additive_latency);
  1200. set_ddr_sdram_mode_2(ddr);
  1201. set_ddr_sdram_interval(ddr, popts, common_dimm);
  1202. set_ddr_data_init(ddr);
  1203. set_ddr_sdram_clk_cntl(ddr, popts);
  1204. set_ddr_init_addr(ddr);
  1205. set_ddr_init_ext_addr(ddr);
  1206. set_timing_cfg_4(ddr);
  1207. set_timing_cfg_5(ddr);
  1208. set_ddr_zq_cntl(ddr, zq_en);
  1209. set_ddr_wrlvl_cntl(ddr, wrlvl_en, popts);
  1210. set_ddr_sr_cntr(ddr, sr_it);
  1211. set_ddr_sdram_rcw_1(ddr);
  1212. set_ddr_sdram_rcw_2(ddr);
  1213. return check_fsl_memctl_config_regs(ddr);
  1214. }