ctrl_regs.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993
  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):
  23. * 0 = Rtt disabled
  24. * 1 = 75 ohm
  25. * 2 = 150 ohm
  26. * 3 = 50 ohm
  27. *
  28. * FIXME: Apparently 8641 needs a value of 2
  29. * FIXME: Old code seys if 667 MHz or higher, use 3 on 8572
  30. *
  31. * FIXME: There was some effort down this line earlier:
  32. *
  33. * unsigned int i;
  34. * for (i = 0; i < CONFIG_CHIP_SELECTS_PER_CTRL/2; i++) {
  35. * if (popts->dimmslot[i].num_valid_cs
  36. * && (popts->cs_local_opts[2*i].odt_rd_cfg
  37. * || popts->cs_local_opts[2*i].odt_wr_cfg)) {
  38. * rtt = 2;
  39. * break;
  40. * }
  41. * }
  42. */
  43. static inline int fsl_ddr_get_rtt(void)
  44. {
  45. int rtt;
  46. #if defined(CONFIG_FSL_DDR1)
  47. rtt = 0;
  48. #elif defined(CONFIG_FSL_DDR2)
  49. rtt = 3;
  50. #else
  51. #error "Need Rtt value for DDR3"
  52. #endif
  53. return rtt;
  54. }
  55. /* Chip Select Configuration (CSn_CONFIG) */
  56. static void set_csn_config(int i, fsl_ddr_cfg_regs_t *ddr,
  57. const memctl_options_t *popts,
  58. const dimm_params_t *dimm_params)
  59. {
  60. unsigned int cs_n_en = 0; /* Chip Select enable */
  61. unsigned int intlv_en = 0; /* Memory controller interleave enable */
  62. unsigned int intlv_ctl = 0; /* Interleaving control */
  63. unsigned int ap_n_en = 0; /* Chip select n auto-precharge enable */
  64. unsigned int odt_rd_cfg = 0; /* ODT for reads configuration */
  65. unsigned int odt_wr_cfg = 0; /* ODT for writes configuration */
  66. unsigned int ba_bits_cs_n = 0; /* Num of bank bits for SDRAM on CSn */
  67. unsigned int row_bits_cs_n = 0; /* Num of row bits for SDRAM on CSn */
  68. unsigned int col_bits_cs_n = 0; /* Num of ocl bits for SDRAM on CSn */
  69. /* Compute CS_CONFIG only for existing ranks of each DIMM. */
  70. if ((((i&1) == 0)
  71. && (dimm_params[i/2].n_ranks == 1))
  72. || (dimm_params[i/2].n_ranks == 2)) {
  73. unsigned int n_banks_per_sdram_device;
  74. cs_n_en = 1;
  75. if (i == 0) {
  76. /* These fields only available in CS0_CONFIG */
  77. intlv_en = popts->memctl_interleaving;
  78. intlv_ctl = popts->memctl_interleaving_mode;
  79. }
  80. ap_n_en = popts->cs_local_opts[i].auto_precharge;
  81. odt_rd_cfg = popts->cs_local_opts[i].odt_rd_cfg;
  82. odt_wr_cfg = popts->cs_local_opts[i].odt_wr_cfg;
  83. n_banks_per_sdram_device
  84. = dimm_params[i/2].n_banks_per_sdram_device;
  85. ba_bits_cs_n = __ilog2(n_banks_per_sdram_device) - 2;
  86. row_bits_cs_n = dimm_params[i/2].n_row_addr - 12;
  87. col_bits_cs_n = dimm_params[i/2].n_col_addr - 8;
  88. }
  89. /* FIXME: intlv_en, intlv_ctl only on CS0_CONFIG */
  90. if (i != 0) {
  91. intlv_en = 0;
  92. intlv_ctl = 0;
  93. }
  94. ddr->cs[i].config = (0
  95. | ((cs_n_en & 0x1) << 31)
  96. | ((intlv_en & 0x3) << 29)
  97. | ((intlv_en & 0xf) << 24)
  98. | ((ap_n_en & 0x1) << 23)
  99. /* XXX: some implementation only have 1 bit starting at left */
  100. | ((odt_rd_cfg & 0x7) << 20)
  101. /* XXX: Some implementation only have 1 bit starting at left */
  102. | ((odt_wr_cfg & 0x7) << 16)
  103. | ((ba_bits_cs_n & 0x3) << 14)
  104. | ((row_bits_cs_n & 0x7) << 8)
  105. | ((col_bits_cs_n & 0x7) << 0)
  106. );
  107. }
  108. /* Chip Select Configuration 2 (CSn_CONFIG_2) */
  109. /* FIXME: 8572 */
  110. static void set_csn_config_2(int i, fsl_ddr_cfg_regs_t *ddr)
  111. {
  112. unsigned int pasr_cfg = 0; /* Partial array self refresh config */
  113. ddr->cs[i].config_2 = ((pasr_cfg & 7) << 24);
  114. }
  115. /* -3E = 667 CL5, -25 = CL6 800, -25E = CL5 800 */
  116. #if defined(CONFIG_FSL_DDR2)
  117. /*
  118. * DDR SDRAM Timing Configuration 0 (TIMING_CFG_0)
  119. *
  120. * Avoid writing for DDR I. The new PQ38 DDR controller
  121. * dreams up non-zero default values to be backwards compatible.
  122. */
  123. static void set_timing_cfg_0(fsl_ddr_cfg_regs_t *ddr)
  124. {
  125. unsigned char trwt_mclk = 0; /* Read-to-write turnaround */
  126. unsigned char twrt_mclk = 0; /* Write-to-read turnaround */
  127. /* 7.5 ns on -3E; 0 means WL - CL + BL/2 + 1 */
  128. unsigned char trrt_mclk = 0; /* Read-to-read turnaround */
  129. unsigned char twwt_mclk = 0; /* Write-to-write turnaround */
  130. /* Active powerdown exit timing (tXARD and tXARDS). */
  131. unsigned char act_pd_exit_mclk;
  132. /* Precharge powerdown exit timing (tXP). */
  133. unsigned char pre_pd_exit_mclk;
  134. /* Precharge powerdown exit timing (tAXPD). */
  135. unsigned char taxpd_mclk;
  136. /* Mode register set cycle time (tMRD). */
  137. unsigned char tmrd_mclk;
  138. /* (tXARD and tXARDS). Empirical? */
  139. act_pd_exit_mclk = 2;
  140. /* XXX: tXARD = 2, tXARDS = 7 - AL. * Empirical? */
  141. pre_pd_exit_mclk = 6;
  142. /* FIXME: tXP = 2 on Micron 667 MHz DIMM */
  143. taxpd_mclk = 8;
  144. tmrd_mclk = 2;
  145. ddr->timing_cfg_0 = (0
  146. | ((trwt_mclk & 0x3) << 30) /* RWT */
  147. | ((twrt_mclk & 0x3) << 28) /* WRT */
  148. | ((trrt_mclk & 0x3) << 26) /* RRT */
  149. | ((twwt_mclk & 0x3) << 24) /* WWT */
  150. | ((act_pd_exit_mclk & 0x7) << 20) /* ACT_PD_EXIT */
  151. | ((pre_pd_exit_mclk & 0x7) << 16) /* PRE_PD_EXIT */
  152. | ((taxpd_mclk & 0xf) << 8) /* ODT_PD_EXIT */
  153. | ((tmrd_mclk & 0xf) << 0) /* MRS_CYC */
  154. );
  155. debug("FSLDDR: timing_cfg_0 = 0x%08x\n", ddr->timing_cfg_0);
  156. }
  157. #endif /* defined(CONFIG_FSL_DDR2) */
  158. /* DDR SDRAM Timing Configuration 3 (TIMING_CFG_3) */
  159. static void set_timing_cfg_3(fsl_ddr_cfg_regs_t *ddr,
  160. const common_timing_params_t *common_dimm)
  161. {
  162. /* Extended Activate to precharge interval (tRAS) */
  163. unsigned int ext_acttopre = 0;
  164. unsigned int ext_refrec; /* Extended refresh recovery time (tRFC) */
  165. unsigned int ext_caslat = 0; /* Extended MCAS latency from READ cmd */
  166. unsigned int cntl_adj = 0; /* Control Adjust */
  167. ext_refrec = (picos_to_mclk(common_dimm->tRFC_ps) - 8) >> 4;
  168. ddr->timing_cfg_3 = (0
  169. | ((ext_acttopre & 0x1) << 24)
  170. | ((ext_refrec & 0x7) << 16)
  171. | ((ext_caslat & 0x1) << 12)
  172. | ((cntl_adj & 0x7) << 0)
  173. );
  174. }
  175. /* DDR SDRAM Timing Configuration 1 (TIMING_CFG_1) */
  176. static void set_timing_cfg_1(fsl_ddr_cfg_regs_t *ddr,
  177. const common_timing_params_t *common_dimm,
  178. unsigned int cas_latency)
  179. {
  180. /* Precharge-to-activate interval (tRP) */
  181. unsigned char pretoact_mclk;
  182. /* Activate to precharge interval (tRAS) */
  183. unsigned char acttopre_mclk;
  184. /* Activate to read/write interval (tRCD) */
  185. unsigned char acttorw_mclk;
  186. /* CASLAT */
  187. unsigned char caslat_ctrl;
  188. /* Refresh recovery time (tRFC) ; trfc_low */
  189. unsigned char refrec_ctrl;
  190. /* Last data to precharge minimum interval (tWR) */
  191. unsigned char wrrec_mclk;
  192. /* Activate-to-activate interval (tRRD) */
  193. unsigned char acttoact_mclk;
  194. /* Last write data pair to read command issue interval (tWTR) */
  195. unsigned char wrtord_mclk;
  196. pretoact_mclk = picos_to_mclk(common_dimm->tRP_ps);
  197. acttopre_mclk = picos_to_mclk(common_dimm->tRAS_ps);
  198. acttorw_mclk = picos_to_mclk(common_dimm->tRCD_ps);
  199. /*
  200. * Translate CAS Latency to a DDR controller field value:
  201. *
  202. * CAS Lat DDR I DDR II Ctrl
  203. * Clocks SPD Bit SPD Bit Value
  204. * ------- ------- ------- -----
  205. * 1.0 0 0001
  206. * 1.5 1 0010
  207. * 2.0 2 2 0011
  208. * 2.5 3 0100
  209. * 3.0 4 3 0101
  210. * 3.5 5 0110
  211. * 4.0 4 0111
  212. * 4.5 1000
  213. * 5.0 5 1001
  214. */
  215. #if defined(CONFIG_FSL_DDR1)
  216. caslat_ctrl = (cas_latency + 1) & 0x07;
  217. #elif defined(CONFIG_FSL_DDR2)
  218. caslat_ctrl = 2 * cas_latency - 1;
  219. #else
  220. #error "Need CAS Latency help for DDR3 in fsl_ddr_sdram.c"
  221. #endif
  222. refrec_ctrl = picos_to_mclk(common_dimm->tRFC_ps) - 8;
  223. wrrec_mclk = picos_to_mclk(common_dimm->tWR_ps);
  224. acttoact_mclk = picos_to_mclk(common_dimm->tRRD_ps);
  225. wrtord_mclk = picos_to_mclk(common_dimm->tWTR_ps);
  226. ddr->timing_cfg_1 = (0
  227. | ((pretoact_mclk & 0x07) << 28)
  228. | ((acttopre_mclk & 0x0F) << 24)
  229. | ((acttorw_mclk & 0x7) << 20)
  230. | ((caslat_ctrl & 0xF) << 16)
  231. | ((refrec_ctrl & 0xF) << 12)
  232. | ((wrrec_mclk & 0x07) << 8)
  233. | ((acttoact_mclk & 0x07) << 4)
  234. | ((wrtord_mclk & 0x07) << 0)
  235. );
  236. }
  237. /* DDR SDRAM Timing Configuration 2 (TIMING_CFG_2) */
  238. static void set_timing_cfg_2(fsl_ddr_cfg_regs_t *ddr,
  239. const memctl_options_t *popts,
  240. const common_timing_params_t *common_dimm,
  241. unsigned int cas_latency,
  242. unsigned int additive_latency)
  243. {
  244. /* Additive latency */
  245. unsigned char add_lat_mclk;
  246. /* CAS-to-preamble override */
  247. unsigned short cpo;
  248. /* Write latency */
  249. unsigned char wr_lat;
  250. /* Read to precharge (tRTP) */
  251. unsigned char rd_to_pre;
  252. /* Write command to write data strobe timing adjustment */
  253. unsigned char wr_data_delay;
  254. /* Minimum CKE pulse width (tCKE) */
  255. unsigned char cke_pls;
  256. /* Window for four activates (tFAW) */
  257. unsigned short four_act;
  258. /* FIXME add check that this must be less than acttorw_mclk */
  259. add_lat_mclk = additive_latency;
  260. cpo = popts->cpo_override;
  261. #if defined(CONFIG_FSL_DDR1)
  262. /*
  263. * This is a lie. It should really be 1, but if it is
  264. * set to 1, bits overlap into the old controller's
  265. * otherwise unused ACSM field. If we leave it 0, then
  266. * the HW will magically treat it as 1 for DDR 1. Oh Yea.
  267. */
  268. wr_lat = 0;
  269. #elif defined(CONFIG_FSL_DDR2)
  270. wr_lat = cas_latency + additive_latency - 1;
  271. #else
  272. #error "Fix WR_LAT for DDR3"
  273. #endif
  274. rd_to_pre = picos_to_mclk(common_dimm->tRTP_ps);
  275. wr_data_delay = popts->write_data_delay;
  276. cke_pls = picos_to_mclk(popts->tCKE_clock_pulse_width_ps);
  277. four_act = picos_to_mclk(popts->tFAW_window_four_activates_ps);
  278. ddr->timing_cfg_2 = (0
  279. | ((add_lat_mclk & 0x7) << 28)
  280. | ((cpo & 0x1f) << 23)
  281. | ((wr_lat & 0x7) << 19)
  282. | ((rd_to_pre & 0x7) << 13)
  283. | ((wr_data_delay & 0x7) << 10)
  284. | ((cke_pls & 0x7) << 6)
  285. | ((four_act & 0x1f) << 0)
  286. );
  287. }
  288. /* DDR SDRAM control configuration (DDR_SDRAM_CFG) */
  289. static void set_ddr_sdram_cfg(fsl_ddr_cfg_regs_t *ddr,
  290. const memctl_options_t *popts,
  291. const common_timing_params_t *common_dimm)
  292. {
  293. unsigned int mem_en; /* DDR SDRAM interface logic enable */
  294. unsigned int sren; /* Self refresh enable (during sleep) */
  295. unsigned int ecc_en; /* ECC enable. */
  296. unsigned int rd_en; /* Registered DIMM enable */
  297. unsigned int sdram_type; /* Type of SDRAM */
  298. unsigned int dyn_pwr; /* Dynamic power management mode */
  299. unsigned int dbw; /* DRAM dta bus width */
  300. unsigned int eight_be; /* 8-beat burst enable */
  301. unsigned int ncap = 0; /* Non-concurrent auto-precharge */
  302. unsigned int threeT_en; /* Enable 3T timing */
  303. unsigned int twoT_en; /* Enable 2T timing */
  304. unsigned int ba_intlv_ctl; /* Bank (CS) interleaving control */
  305. unsigned int x32_en = 0; /* x32 enable */
  306. unsigned int pchb8 = 0; /* precharge bit 8 enable */
  307. unsigned int hse; /* Global half strength override */
  308. unsigned int mem_halt = 0; /* memory controller halt */
  309. unsigned int bi = 0; /* Bypass initialization */
  310. mem_en = 1;
  311. sren = popts->self_refresh_in_sleep;
  312. if (common_dimm->all_DIMMs_ECC_capable) {
  313. /* Allow setting of ECC only if all DIMMs are ECC. */
  314. ecc_en = popts->ECC_mode;
  315. } else {
  316. ecc_en = 0;
  317. }
  318. rd_en = (common_dimm->all_DIMMs_registered
  319. && !common_dimm->all_DIMMs_unbuffered);
  320. sdram_type = CONFIG_FSL_SDRAM_TYPE;
  321. dyn_pwr = popts->dynamic_power;
  322. dbw = popts->data_bus_width;
  323. eight_be = 0; /* always 0 for DDR2 */
  324. threeT_en = popts->threeT_en;
  325. twoT_en = popts->twoT_en;
  326. ba_intlv_ctl = popts->ba_intlv_ctl;
  327. hse = popts->half_strength_driver_enable;
  328. ddr->ddr_sdram_cfg = (0
  329. | ((mem_en & 0x1) << 31)
  330. | ((sren & 0x1) << 30)
  331. | ((ecc_en & 0x1) << 29)
  332. | ((rd_en & 0x1) << 28)
  333. | ((sdram_type & 0x7) << 24)
  334. | ((dyn_pwr & 0x1) << 21)
  335. | ((dbw & 0x3) << 19)
  336. | ((eight_be & 0x1) << 18)
  337. | ((ncap & 0x1) << 17)
  338. | ((threeT_en & 0x1) << 16)
  339. | ((twoT_en & 0x1) << 15)
  340. | ((ba_intlv_ctl & 0x7F) << 8)
  341. | ((x32_en & 0x1) << 5)
  342. | ((pchb8 & 0x1) << 4)
  343. | ((hse & 0x1) << 3)
  344. | ((mem_halt & 0x1) << 1)
  345. | ((bi & 0x1) << 0)
  346. );
  347. }
  348. /* DDR SDRAM control configuration 2 (DDR_SDRAM_CFG_2) */
  349. static void set_ddr_sdram_cfg_2(fsl_ddr_cfg_regs_t *ddr,
  350. const memctl_options_t *popts)
  351. {
  352. unsigned int frc_sr = 0; /* Force self refresh */
  353. unsigned int sr_ie = 0; /* Self-refresh interrupt enable */
  354. unsigned int dll_rst_dis; /* DLL reset disable */
  355. unsigned int dqs_cfg; /* DQS configuration */
  356. unsigned int odt_cfg; /* ODT configuration */
  357. unsigned int num_pr; /* Number of posted refreshes */
  358. unsigned int obc_cfg; /* On-The-Fly Burst Chop Cfg */
  359. unsigned int ap_en; /* Address Parity Enable */
  360. unsigned int d_init; /* DRAM data initialization */
  361. unsigned int rcw_en = 0; /* Register Control Word Enable */
  362. unsigned int md_en = 0; /* Mirrored DIMM Enable */
  363. dll_rst_dis = 1; /* Make this configurable */
  364. dqs_cfg = popts->DQS_config;
  365. if (popts->cs_local_opts[0].odt_rd_cfg
  366. || popts->cs_local_opts[0].odt_wr_cfg) {
  367. /* FIXME */
  368. odt_cfg = 2;
  369. } else {
  370. odt_cfg = 0;
  371. }
  372. num_pr = 1; /* Make this configurable */
  373. /*
  374. * 8572 manual says
  375. * {TIMING_CFG_1[PRETOACT]
  376. * + [DDR_SDRAM_CFG_2[NUM_PR]
  377. * * ({EXT_REFREC || REFREC} + 8 + 2)]}
  378. * << DDR_SDRAM_INTERVAL[REFINT]
  379. */
  380. obc_cfg = 0; /* Make this configurable? */
  381. ap_en = 0; /* Make this configurable? */
  382. #if defined(CONFIG_ECC_INIT_VIA_DDRCONTROLLER)
  383. /* Use the DDR controller to auto initialize memory. */
  384. d_init = 1;
  385. ddr->ddr_data_init = CONFIG_MEM_INIT_VALUE;
  386. debug("DDR: ddr_data_init = 0x%08x\n", ddr->ddr_data_init);
  387. #else
  388. /* Memory will be initialized via DMA, or not at all. */
  389. d_init = 0;
  390. #endif
  391. ddr->ddr_sdram_cfg_2 = (0
  392. | ((frc_sr & 0x1) << 31)
  393. | ((sr_ie & 0x1) << 30)
  394. | ((dll_rst_dis & 0x1) << 29)
  395. | ((dqs_cfg & 0x3) << 26)
  396. | ((odt_cfg & 0x3) << 21)
  397. | ((num_pr & 0xf) << 12)
  398. | ((obc_cfg & 0x1) << 6)
  399. | ((ap_en & 0x1) << 5)
  400. | ((d_init & 0x1) << 4)
  401. | ((rcw_en & 0x1) << 2)
  402. | ((md_en & 0x1) << 0)
  403. );
  404. }
  405. /* DDR SDRAM Mode configuration 2 (DDR_SDRAM_MODE_2) */
  406. static void set_ddr_sdram_mode_2(fsl_ddr_cfg_regs_t *ddr)
  407. {
  408. unsigned short esdmode2 = 0; /* Extended SDRAM mode 2 */
  409. unsigned short esdmode3 = 0; /* Extended SDRAM mode 3 */
  410. ddr->ddr_sdram_mode_2 = (0
  411. | ((esdmode2 & 0xFFFF) << 16)
  412. | ((esdmode3 & 0xFFFF) << 0)
  413. );
  414. }
  415. /* DDR SDRAM Interval Configuration (DDR_SDRAM_INTERVAL) */
  416. static void set_ddr_sdram_interval(fsl_ddr_cfg_regs_t *ddr,
  417. const memctl_options_t *popts,
  418. const common_timing_params_t *common_dimm)
  419. {
  420. unsigned int refint; /* Refresh interval */
  421. unsigned int bstopre; /* Precharge interval */
  422. refint = picos_to_mclk(common_dimm->refresh_rate_ps);
  423. bstopre = popts->bstopre;
  424. /* refint field used 0x3FFF in earlier controllers */
  425. ddr->ddr_sdram_interval = (0
  426. | ((refint & 0xFFFF) << 16)
  427. | ((bstopre & 0x3FFF) << 0)
  428. );
  429. }
  430. /* DDR SDRAM Mode configuration set (DDR_SDRAM_MODE) */
  431. static void set_ddr_sdram_mode(fsl_ddr_cfg_regs_t *ddr,
  432. const memctl_options_t *popts,
  433. const common_timing_params_t *common_dimm,
  434. unsigned int cas_latency,
  435. unsigned int additive_latency)
  436. {
  437. unsigned short esdmode; /* Extended SDRAM mode */
  438. unsigned short sdmode; /* SDRAM mode */
  439. /*
  440. * FIXME: This ought to be pre-calculated in a
  441. * technology-specific routine,
  442. * e.g. compute_DDR2_mode_register(), and then the
  443. * sdmode and esdmode passed in as part of common_dimm.
  444. */
  445. /* Extended Mode Register */
  446. unsigned int mrs = 0; /* Mode Register Set */
  447. unsigned int outputs = 0; /* 0=Enabled, 1=Disabled */
  448. unsigned int rdqs_en = 0; /* RDQS Enable: 0=no, 1=yes */
  449. unsigned int dqs_en = 0; /* DQS# Enable: 0=enable, 1=disable */
  450. unsigned int ocd = 0; /* 0x0=OCD not supported,
  451. 0x7=OCD default state */
  452. unsigned int rtt;
  453. unsigned int al; /* Posted CAS# additive latency (AL) */
  454. unsigned int ods = 0; /* Output Drive Strength:
  455. 0 = Full strength (18ohm)
  456. 1 = Reduced strength (4ohm) */
  457. unsigned int dll_en = 0; /* DLL Enable 0=Enable (Normal),
  458. 1=Disable (Test/Debug) */
  459. /* Mode Register (MR) */
  460. unsigned int mr; /* Mode Register Definition */
  461. unsigned int pd; /* Power-Down Mode */
  462. unsigned int wr; /* Write Recovery */
  463. unsigned int dll_res; /* DLL Reset */
  464. unsigned int mode; /* Normal=0 or Test=1 */
  465. unsigned int caslat; /* CAS# latency */
  466. /* BT: Burst Type (0=Sequential, 1=Interleaved) */
  467. unsigned int bt;
  468. unsigned int bl; /* BL: Burst Length */
  469. #if defined(CONFIG_FSL_DDR2)
  470. const unsigned int mclk_ps = get_memory_clk_period_ps();
  471. #endif
  472. rtt = fsl_ddr_get_rtt();
  473. al = additive_latency;
  474. esdmode = (0
  475. | ((mrs & 0x3) << 14)
  476. | ((outputs & 0x1) << 12)
  477. | ((rdqs_en & 0x1) << 11)
  478. | ((dqs_en & 0x1) << 10)
  479. | ((ocd & 0x7) << 7)
  480. | ((rtt & 0x2) << 5) /* rtt field is split */
  481. | ((al & 0x7) << 3)
  482. | ((rtt & 0x1) << 2) /* rtt field is split */
  483. | ((ods & 0x1) << 1)
  484. | ((dll_en & 0x1) << 0)
  485. );
  486. mr = 0; /* FIXME: CHECKME */
  487. /*
  488. * 0 = Fast Exit (Normal)
  489. * 1 = Slow Exit (Low Power)
  490. */
  491. pd = 0;
  492. #if defined(CONFIG_FSL_DDR1)
  493. wr = 0; /* Historical */
  494. #elif defined(CONFIG_FSL_DDR2)
  495. wr = (common_dimm->tWR_ps + mclk_ps - 1) / mclk_ps - 1;
  496. #else
  497. #error "Write tWR_auto for DDR3"
  498. #endif
  499. dll_res = 0;
  500. mode = 0;
  501. #if defined(CONFIG_FSL_DDR1)
  502. if (1 <= cas_latency && cas_latency <= 4) {
  503. unsigned char mode_caslat_table[4] = {
  504. 0x5, /* 1.5 clocks */
  505. 0x2, /* 2.0 clocks */
  506. 0x6, /* 2.5 clocks */
  507. 0x3 /* 3.0 clocks */
  508. };
  509. caslat = mode_caslat_table[cas_latency - 1];
  510. }
  511. #elif defined(CONFIG_FSL_DDR2)
  512. caslat = cas_latency;
  513. #else
  514. #error "Fix the mode CAS Latency for DDR3"
  515. #endif
  516. bt = 0;
  517. switch (popts->burst_length) {
  518. case 4:
  519. bl = 2;
  520. break;
  521. case 8:
  522. bl = 3;
  523. break;
  524. default:
  525. printf("Error: invalid burst length of %u specified. "
  526. " Defaulting to 4 beats.\n",
  527. popts->burst_length);
  528. bl = 2;
  529. break;
  530. }
  531. sdmode = (0
  532. | ((mr & 0x3) << 14)
  533. | ((pd & 0x1) << 12)
  534. | ((wr & 0x7) << 9)
  535. | ((dll_res & 0x1) << 8)
  536. | ((mode & 0x1) << 7)
  537. | ((caslat & 0x7) << 4)
  538. | ((bt & 0x1) << 3)
  539. | ((bl & 0x7) << 0)
  540. );
  541. ddr->ddr_sdram_mode = (0
  542. | ((esdmode & 0xFFFF) << 16)
  543. | ((sdmode & 0xFFFF) << 0)
  544. );
  545. }
  546. /* DDR SDRAM Data Initialization (DDR_DATA_INIT) */
  547. static void set_ddr_data_init(fsl_ddr_cfg_regs_t *ddr)
  548. {
  549. unsigned int init_value; /* Initialization value */
  550. init_value = 0xDEADBEEF;
  551. ddr->ddr_data_init = init_value;
  552. }
  553. /*
  554. * DDR SDRAM Clock Control (DDR_SDRAM_CLK_CNTL)
  555. * The old controller on the 8540/60 doesn't have this register.
  556. * Hope it's OK to set it (to 0) anyway.
  557. */
  558. static void set_ddr_sdram_clk_cntl(fsl_ddr_cfg_regs_t *ddr,
  559. const memctl_options_t *popts)
  560. {
  561. unsigned int clk_adjust; /* Clock adjust */
  562. clk_adjust = popts->clk_adjust;
  563. ddr->ddr_sdram_clk_cntl = (clk_adjust & 0xF) << 23;
  564. }
  565. /* DDR Initialization Address (DDR_INIT_ADDR) */
  566. static void set_ddr_init_addr(fsl_ddr_cfg_regs_t *ddr)
  567. {
  568. unsigned int init_addr = 0; /* Initialization address */
  569. ddr->ddr_init_addr = init_addr;
  570. }
  571. /* DDR Initialization Address (DDR_INIT_EXT_ADDR) */
  572. static void set_ddr_init_ext_addr(fsl_ddr_cfg_regs_t *ddr)
  573. {
  574. unsigned int uia = 0; /* Use initialization address */
  575. unsigned int init_ext_addr = 0; /* Initialization address */
  576. ddr->ddr_init_ext_addr = (0
  577. | ((uia & 0x1) << 31)
  578. | (init_ext_addr & 0xF)
  579. );
  580. }
  581. /* DDR SDRAM Timing Configuration 4 (TIMING_CFG_4) */
  582. static void set_timing_cfg_4(fsl_ddr_cfg_regs_t *ddr)
  583. {
  584. unsigned int rwt = 0; /* Read-to-write turnaround for same CS */
  585. unsigned int wrt = 0; /* Write-to-read turnaround for same CS */
  586. unsigned int rrt = 0; /* Read-to-read turnaround for same CS */
  587. unsigned int wwt = 0; /* Write-to-write turnaround for same CS */
  588. unsigned int dll_lock = 0; /* DDR SDRAM DLL Lock Time */
  589. ddr->timing_cfg_4 = (0
  590. | ((rwt & 0xf) << 28)
  591. | ((wrt & 0xf) << 24)
  592. | ((rrt & 0xf) << 20)
  593. | ((wwt & 0xf) << 16)
  594. | (dll_lock & 0x3)
  595. );
  596. }
  597. /* DDR SDRAM Timing Configuration 5 (TIMING_CFG_5) */
  598. static void set_timing_cfg_5(fsl_ddr_cfg_regs_t *ddr)
  599. {
  600. unsigned int rodt_on = 0; /* Read to ODT on */
  601. unsigned int rodt_off = 0; /* Read to ODT off */
  602. unsigned int wodt_on = 0; /* Write to ODT on */
  603. unsigned int wodt_off = 0; /* Write to ODT off */
  604. ddr->timing_cfg_5 = (0
  605. | ((rodt_on & 0xf) << 24)
  606. | ((rodt_off & 0xf) << 20)
  607. | ((wodt_on & 0xf) << 12)
  608. | ((wodt_off & 0xf) << 8)
  609. );
  610. }
  611. /* DDR ZQ Calibration Control (DDR_ZQ_CNTL) */
  612. static void set_ddr_zq_cntl(fsl_ddr_cfg_regs_t *ddr)
  613. {
  614. unsigned int zq_en = 0; /* ZQ Calibration Enable */
  615. unsigned int zqinit = 0;/* POR ZQ Calibration Time (tZQinit) */
  616. /* Normal Operation Full Calibration Time (tZQoper) */
  617. unsigned int zqoper = 0;
  618. /* Normal Operation Short Calibration Time (tZQCS) */
  619. unsigned int zqcs = 0;
  620. ddr->ddr_zq_cntl = (0
  621. | ((zq_en & 0x1) << 31)
  622. | ((zqinit & 0xF) << 24)
  623. | ((zqoper & 0xF) << 16)
  624. | ((zqcs & 0xF) << 8)
  625. );
  626. }
  627. /* DDR Write Leveling Control (DDR_WRLVL_CNTL) */
  628. static void set_ddr_wrlvl_cntl(fsl_ddr_cfg_regs_t *ddr)
  629. {
  630. unsigned int wrlvl_en = 0; /* Write Leveling Enable */
  631. /*
  632. * First DQS pulse rising edge after margining mode
  633. * is programmed (tWL_MRD)
  634. */
  635. unsigned int wrlvl_mrd = 0;
  636. /* ODT delay after margining mode is programmed (tWL_ODTEN) */
  637. unsigned int wrlvl_odten = 0;
  638. /* DQS/DQS_ delay after margining mode is programmed (tWL_DQSEN) */
  639. unsigned int wrlvl_dqsen = 0;
  640. /* WRLVL_SMPL: Write leveling sample time */
  641. unsigned int wrlvl_smpl = 0;
  642. /* WRLVL_WLR: Write leveling repeition time */
  643. unsigned int wrlvl_wlr = 0;
  644. /* WRLVL_START: Write leveling start time */
  645. unsigned int wrlvl_start = 0;
  646. ddr->ddr_wrlvl_cntl = (0
  647. | ((wrlvl_en & 0x1) << 31)
  648. | ((wrlvl_mrd & 0x7) << 24)
  649. | ((wrlvl_odten & 0x7) << 20)
  650. | ((wrlvl_dqsen & 0x7) << 16)
  651. | ((wrlvl_smpl & 0xf) << 12)
  652. | ((wrlvl_wlr & 0x7) << 8)
  653. | ((wrlvl_start & 0xF) << 0)
  654. );
  655. }
  656. /* DDR Self Refresh Counter (DDR_SR_CNTR) */
  657. static void set_ddr_sr_cntr(fsl_ddr_cfg_regs_t *ddr)
  658. {
  659. unsigned int sr_it = 0; /* Self Refresh Idle Threshold */
  660. ddr->ddr_sr_cntr = (sr_it & 0xF) << 16;
  661. }
  662. /* DDR Pre-Drive Conditioning Control (DDR_PD_CNTL) */
  663. static void set_ddr_pd_cntl(fsl_ddr_cfg_regs_t *ddr)
  664. {
  665. /* Termination value during pre-drive conditioning */
  666. unsigned int tvpd = 0;
  667. unsigned int pd_en = 0; /* Pre-Drive Conditioning Enable */
  668. unsigned int pdar = 0; /* Pre-Drive After Read */
  669. unsigned int pdaw = 0; /* Pre-Drive After Write */
  670. unsigned int pd_on = 0; /* Pre-Drive Conditioning On */
  671. unsigned int pd_off = 0; /* Pre-Drive Conditioning Off */
  672. ddr->ddr_pd_cntl = (0
  673. | ((pd_en & 0x1) << 31)
  674. | ((tvpd & 0x7) << 28)
  675. | ((pdar & 0x7F) << 20)
  676. | ((pdaw & 0x7F) << 12)
  677. | ((pd_on & 0x1F) << 6)
  678. | ((pd_off & 0x1F) << 0)
  679. );
  680. }
  681. /* DDR SDRAM Register Control Word 1 (DDR_SDRAM_RCW_1) */
  682. static void set_ddr_sdram_rcw_1(fsl_ddr_cfg_regs_t *ddr)
  683. {
  684. unsigned int rcw0 = 0; /* RCW0: Register Control Word 0 */
  685. unsigned int rcw1 = 0; /* RCW1: Register Control Word 1 */
  686. unsigned int rcw2 = 0; /* RCW2: Register Control Word 2 */
  687. unsigned int rcw3 = 0; /* RCW3: Register Control Word 3 */
  688. unsigned int rcw4 = 0; /* RCW4: Register Control Word 4 */
  689. unsigned int rcw5 = 0; /* RCW5: Register Control Word 5 */
  690. unsigned int rcw6 = 0; /* RCW6: Register Control Word 6 */
  691. unsigned int rcw7 = 0; /* RCW7: Register Control Word 7 */
  692. ddr->ddr_sdram_rcw_1 = (0
  693. | ((rcw0 & 0xF) << 28)
  694. | ((rcw1 & 0xF) << 24)
  695. | ((rcw2 & 0xF) << 20)
  696. | ((rcw3 & 0xF) << 16)
  697. | ((rcw4 & 0xF) << 12)
  698. | ((rcw5 & 0xF) << 8)
  699. | ((rcw6 & 0xF) << 4)
  700. | ((rcw7 & 0xF) << 0)
  701. );
  702. }
  703. /* DDR SDRAM Register Control Word 2 (DDR_SDRAM_RCW_2) */
  704. static void set_ddr_sdram_rcw_2(fsl_ddr_cfg_regs_t *ddr)
  705. {
  706. unsigned int rcw8 = 0; /* RCW0: Register Control Word 8 */
  707. unsigned int rcw9 = 0; /* RCW1: Register Control Word 9 */
  708. unsigned int rcw10 = 0; /* RCW2: Register Control Word 10 */
  709. unsigned int rcw11 = 0; /* RCW3: Register Control Word 11 */
  710. unsigned int rcw12 = 0; /* RCW4: Register Control Word 12 */
  711. unsigned int rcw13 = 0; /* RCW5: Register Control Word 13 */
  712. unsigned int rcw14 = 0; /* RCW6: Register Control Word 14 */
  713. unsigned int rcw15 = 0; /* RCW7: Register Control Word 15 */
  714. ddr->ddr_sdram_rcw_2 = (0
  715. | ((rcw8 & 0xF) << 28)
  716. | ((rcw9 & 0xF) << 24)
  717. | ((rcw10 & 0xF) << 20)
  718. | ((rcw11 & 0xF) << 16)
  719. | ((rcw12 & 0xF) << 12)
  720. | ((rcw13 & 0xF) << 8)
  721. | ((rcw14 & 0xF) << 4)
  722. | ((rcw15 & 0xF) << 0)
  723. );
  724. }
  725. unsigned int
  726. check_fsl_memctl_config_regs(const fsl_ddr_cfg_regs_t *ddr)
  727. {
  728. unsigned int res = 0;
  729. /*
  730. * Check that DDR_SDRAM_CFG[RD_EN] and DDR_SDRAM_CFG[2T_EN] are
  731. * not set at the same time.
  732. */
  733. if (ddr->ddr_sdram_cfg & 0x10000000
  734. && ddr->ddr_sdram_cfg & 0x00008000) {
  735. printf("Error: DDR_SDRAM_CFG[RD_EN] and DDR_SDRAM_CFG[2T_EN] "
  736. " should not be set at the same time.\n");
  737. res++;
  738. }
  739. return res;
  740. }
  741. unsigned int
  742. compute_fsl_memctl_config_regs(const memctl_options_t *popts,
  743. fsl_ddr_cfg_regs_t *ddr,
  744. const common_timing_params_t *common_dimm,
  745. const dimm_params_t *dimm_params,
  746. unsigned int dbw_cap_adj)
  747. {
  748. unsigned int i;
  749. unsigned int cas_latency;
  750. unsigned int additive_latency;
  751. memset(ddr, 0, sizeof(fsl_ddr_cfg_regs_t));
  752. if (common_dimm == NULL) {
  753. printf("Error: subset DIMM params struct null pointer\n");
  754. return 1;
  755. }
  756. /*
  757. * Process overrides first.
  758. *
  759. * FIXME: somehow add dereated caslat to this
  760. */
  761. cas_latency = (popts->cas_latency_override)
  762. ? popts->cas_latency_override_value
  763. : common_dimm->lowest_common_SPD_caslat;
  764. additive_latency = (popts->additive_latency_override)
  765. ? popts->additive_latency_override_value
  766. : common_dimm->additive_latency;
  767. /* Chip Select Memory Bounds (CSn_BNDS) */
  768. for (i = 0; i < CONFIG_CHIP_SELECTS_PER_CTRL; i++) {
  769. phys_size_t sa = 0;
  770. phys_size_t ea = 0;
  771. if (popts->ba_intlv_ctl && i > 0) {
  772. /* Don't set up boundaries if bank interleaving */
  773. break;
  774. }
  775. if (dimm_params[i/2].n_ranks == 0) {
  776. debug("Skipping setup of CS%u "
  777. "because n_ranks on DIMM %u is 0\n", i, i/2);
  778. continue;
  779. }
  780. if (popts->memctl_interleaving && popts->ba_intlv_ctl) {
  781. /*
  782. * This works superbank 2CS
  783. * There are 2 memory controllers configured
  784. * identically, memory is interleaved between them,
  785. * and each controller uses rank interleaving within
  786. * itself. Therefore the starting and ending address
  787. * on each controller is twice the amount present on
  788. * each controller.
  789. */
  790. ea = (2 * common_dimm->total_mem >> dbw_cap_adj) - 1;
  791. }
  792. else if (!popts->memctl_interleaving && popts->ba_intlv_ctl) {
  793. /*
  794. * If memory interleaving between controllers is NOT
  795. * enabled, the starting address for each memory
  796. * controller is distinct. However, because rank
  797. * interleaving is enabled, the starting and ending
  798. * addresses of the total memory on that memory
  799. * controller needs to be programmed into its
  800. * respective CS0_BNDS.
  801. */
  802. sa = common_dimm->base_address;
  803. ea = sa + (common_dimm->total_mem >> dbw_cap_adj) - 1;
  804. }
  805. else if (popts->memctl_interleaving && !popts->ba_intlv_ctl) {
  806. /*
  807. * Only the rank on CS0 of each memory controller may
  808. * be used if memory controller interleaving is used
  809. * without rank interleaving within each memory
  810. * controller. However, the ending address programmed
  811. * into each CS0 must be the sum of the amount of
  812. * memory in the two CS0 ranks.
  813. */
  814. if (i == 0) {
  815. unsigned long long rank_density
  816. = dimm_params[0].rank_density;
  817. ea = (2 * (rank_density >> dbw_cap_adj)) - 1;
  818. }
  819. }
  820. else if (!popts->memctl_interleaving && !popts->ba_intlv_ctl) {
  821. /*
  822. * No rank interleaving and no memory controller
  823. * interleaving.
  824. */
  825. unsigned long long rank_density
  826. = dimm_params[i/2].rank_density;
  827. sa = dimm_params[i/2].base_address;
  828. ea = sa + (rank_density >> dbw_cap_adj) - 1;
  829. if (i&1) {
  830. if ((dimm_params[i/2].n_ranks == 1)) {
  831. /* Odd chip select, single-rank dimm */
  832. sa = 0;
  833. ea = 0;
  834. } else {
  835. /* Odd chip select, dual-rank DIMM */
  836. sa += rank_density >> dbw_cap_adj;
  837. ea += rank_density >> dbw_cap_adj;
  838. }
  839. }
  840. }
  841. sa >>= 24;
  842. ea >>= 24;
  843. ddr->cs[i].bnds = (0
  844. | ((sa & 0xFFF) << 16) /* starting address MSB */
  845. | ((ea & 0xFFF) << 0) /* ending address MSB */
  846. );
  847. set_csn_config(i, ddr, popts, dimm_params);
  848. set_csn_config_2(i, ddr);
  849. }
  850. #if defined(CONFIG_FSL_DDR2)
  851. set_timing_cfg_0(ddr);
  852. #endif
  853. set_timing_cfg_3(ddr, common_dimm);
  854. set_timing_cfg_1(ddr, common_dimm, cas_latency);
  855. set_timing_cfg_2(ddr, popts, common_dimm,
  856. cas_latency, additive_latency);
  857. set_ddr_sdram_cfg(ddr, popts, common_dimm);
  858. set_ddr_sdram_cfg_2(ddr, popts);
  859. set_ddr_sdram_mode(ddr, popts, common_dimm,
  860. cas_latency, additive_latency);
  861. set_ddr_sdram_mode_2(ddr);
  862. set_ddr_sdram_interval(ddr, popts, common_dimm);
  863. set_ddr_data_init(ddr);
  864. set_ddr_sdram_clk_cntl(ddr, popts);
  865. set_ddr_init_addr(ddr);
  866. set_ddr_init_ext_addr(ddr);
  867. set_timing_cfg_4(ddr);
  868. set_timing_cfg_5(ddr);
  869. set_ddr_zq_cntl(ddr);
  870. set_ddr_wrlvl_cntl(ddr);
  871. set_ddr_pd_cntl(ddr);
  872. set_ddr_sr_cntr(ddr);
  873. set_ddr_sdram_rcw_1(ddr);
  874. set_ddr_sdram_rcw_2(ddr);
  875. return check_fsl_memctl_config_regs(ddr);
  876. }