ctrl_regs.c 39 KB

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