ctrl_regs.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995
  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 = 0;/* 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. } else {
  511. printf("Warning: unknown cas_latency %d\n", cas_latency);
  512. }
  513. #elif defined(CONFIG_FSL_DDR2)
  514. caslat = cas_latency;
  515. #else
  516. #error "Fix the mode CAS Latency for DDR3"
  517. #endif
  518. bt = 0;
  519. switch (popts->burst_length) {
  520. case 4:
  521. bl = 2;
  522. break;
  523. case 8:
  524. bl = 3;
  525. break;
  526. default:
  527. printf("Error: invalid burst length of %u specified. "
  528. " Defaulting to 4 beats.\n",
  529. popts->burst_length);
  530. bl = 2;
  531. break;
  532. }
  533. sdmode = (0
  534. | ((mr & 0x3) << 14)
  535. | ((pd & 0x1) << 12)
  536. | ((wr & 0x7) << 9)
  537. | ((dll_res & 0x1) << 8)
  538. | ((mode & 0x1) << 7)
  539. | ((caslat & 0x7) << 4)
  540. | ((bt & 0x1) << 3)
  541. | ((bl & 0x7) << 0)
  542. );
  543. ddr->ddr_sdram_mode = (0
  544. | ((esdmode & 0xFFFF) << 16)
  545. | ((sdmode & 0xFFFF) << 0)
  546. );
  547. }
  548. /* DDR SDRAM Data Initialization (DDR_DATA_INIT) */
  549. static void set_ddr_data_init(fsl_ddr_cfg_regs_t *ddr)
  550. {
  551. unsigned int init_value; /* Initialization value */
  552. init_value = 0xDEADBEEF;
  553. ddr->ddr_data_init = init_value;
  554. }
  555. /*
  556. * DDR SDRAM Clock Control (DDR_SDRAM_CLK_CNTL)
  557. * The old controller on the 8540/60 doesn't have this register.
  558. * Hope it's OK to set it (to 0) anyway.
  559. */
  560. static void set_ddr_sdram_clk_cntl(fsl_ddr_cfg_regs_t *ddr,
  561. const memctl_options_t *popts)
  562. {
  563. unsigned int clk_adjust; /* Clock adjust */
  564. clk_adjust = popts->clk_adjust;
  565. ddr->ddr_sdram_clk_cntl = (clk_adjust & 0xF) << 23;
  566. }
  567. /* DDR Initialization Address (DDR_INIT_ADDR) */
  568. static void set_ddr_init_addr(fsl_ddr_cfg_regs_t *ddr)
  569. {
  570. unsigned int init_addr = 0; /* Initialization address */
  571. ddr->ddr_init_addr = init_addr;
  572. }
  573. /* DDR Initialization Address (DDR_INIT_EXT_ADDR) */
  574. static void set_ddr_init_ext_addr(fsl_ddr_cfg_regs_t *ddr)
  575. {
  576. unsigned int uia = 0; /* Use initialization address */
  577. unsigned int init_ext_addr = 0; /* Initialization address */
  578. ddr->ddr_init_ext_addr = (0
  579. | ((uia & 0x1) << 31)
  580. | (init_ext_addr & 0xF)
  581. );
  582. }
  583. /* DDR SDRAM Timing Configuration 4 (TIMING_CFG_4) */
  584. static void set_timing_cfg_4(fsl_ddr_cfg_regs_t *ddr)
  585. {
  586. unsigned int rwt = 0; /* Read-to-write turnaround for same CS */
  587. unsigned int wrt = 0; /* Write-to-read turnaround for same CS */
  588. unsigned int rrt = 0; /* Read-to-read turnaround for same CS */
  589. unsigned int wwt = 0; /* Write-to-write turnaround for same CS */
  590. unsigned int dll_lock = 0; /* DDR SDRAM DLL Lock Time */
  591. ddr->timing_cfg_4 = (0
  592. | ((rwt & 0xf) << 28)
  593. | ((wrt & 0xf) << 24)
  594. | ((rrt & 0xf) << 20)
  595. | ((wwt & 0xf) << 16)
  596. | (dll_lock & 0x3)
  597. );
  598. }
  599. /* DDR SDRAM Timing Configuration 5 (TIMING_CFG_5) */
  600. static void set_timing_cfg_5(fsl_ddr_cfg_regs_t *ddr)
  601. {
  602. unsigned int rodt_on = 0; /* Read to ODT on */
  603. unsigned int rodt_off = 0; /* Read to ODT off */
  604. unsigned int wodt_on = 0; /* Write to ODT on */
  605. unsigned int wodt_off = 0; /* Write to ODT off */
  606. ddr->timing_cfg_5 = (0
  607. | ((rodt_on & 0xf) << 24)
  608. | ((rodt_off & 0xf) << 20)
  609. | ((wodt_on & 0xf) << 12)
  610. | ((wodt_off & 0xf) << 8)
  611. );
  612. }
  613. /* DDR ZQ Calibration Control (DDR_ZQ_CNTL) */
  614. static void set_ddr_zq_cntl(fsl_ddr_cfg_regs_t *ddr)
  615. {
  616. unsigned int zq_en = 0; /* ZQ Calibration Enable */
  617. unsigned int zqinit = 0;/* POR ZQ Calibration Time (tZQinit) */
  618. /* Normal Operation Full Calibration Time (tZQoper) */
  619. unsigned int zqoper = 0;
  620. /* Normal Operation Short Calibration Time (tZQCS) */
  621. unsigned int zqcs = 0;
  622. ddr->ddr_zq_cntl = (0
  623. | ((zq_en & 0x1) << 31)
  624. | ((zqinit & 0xF) << 24)
  625. | ((zqoper & 0xF) << 16)
  626. | ((zqcs & 0xF) << 8)
  627. );
  628. }
  629. /* DDR Write Leveling Control (DDR_WRLVL_CNTL) */
  630. static void set_ddr_wrlvl_cntl(fsl_ddr_cfg_regs_t *ddr)
  631. {
  632. unsigned int wrlvl_en = 0; /* Write Leveling Enable */
  633. /*
  634. * First DQS pulse rising edge after margining mode
  635. * is programmed (tWL_MRD)
  636. */
  637. unsigned int wrlvl_mrd = 0;
  638. /* ODT delay after margining mode is programmed (tWL_ODTEN) */
  639. unsigned int wrlvl_odten = 0;
  640. /* DQS/DQS_ delay after margining mode is programmed (tWL_DQSEN) */
  641. unsigned int wrlvl_dqsen = 0;
  642. /* WRLVL_SMPL: Write leveling sample time */
  643. unsigned int wrlvl_smpl = 0;
  644. /* WRLVL_WLR: Write leveling repeition time */
  645. unsigned int wrlvl_wlr = 0;
  646. /* WRLVL_START: Write leveling start time */
  647. unsigned int wrlvl_start = 0;
  648. ddr->ddr_wrlvl_cntl = (0
  649. | ((wrlvl_en & 0x1) << 31)
  650. | ((wrlvl_mrd & 0x7) << 24)
  651. | ((wrlvl_odten & 0x7) << 20)
  652. | ((wrlvl_dqsen & 0x7) << 16)
  653. | ((wrlvl_smpl & 0xf) << 12)
  654. | ((wrlvl_wlr & 0x7) << 8)
  655. | ((wrlvl_start & 0xF) << 0)
  656. );
  657. }
  658. /* DDR Self Refresh Counter (DDR_SR_CNTR) */
  659. static void set_ddr_sr_cntr(fsl_ddr_cfg_regs_t *ddr)
  660. {
  661. unsigned int sr_it = 0; /* Self Refresh Idle Threshold */
  662. ddr->ddr_sr_cntr = (sr_it & 0xF) << 16;
  663. }
  664. /* DDR Pre-Drive Conditioning Control (DDR_PD_CNTL) */
  665. static void set_ddr_pd_cntl(fsl_ddr_cfg_regs_t *ddr)
  666. {
  667. /* Termination value during pre-drive conditioning */
  668. unsigned int tvpd = 0;
  669. unsigned int pd_en = 0; /* Pre-Drive Conditioning Enable */
  670. unsigned int pdar = 0; /* Pre-Drive After Read */
  671. unsigned int pdaw = 0; /* Pre-Drive After Write */
  672. unsigned int pd_on = 0; /* Pre-Drive Conditioning On */
  673. unsigned int pd_off = 0; /* Pre-Drive Conditioning Off */
  674. ddr->ddr_pd_cntl = (0
  675. | ((pd_en & 0x1) << 31)
  676. | ((tvpd & 0x7) << 28)
  677. | ((pdar & 0x7F) << 20)
  678. | ((pdaw & 0x7F) << 12)
  679. | ((pd_on & 0x1F) << 6)
  680. | ((pd_off & 0x1F) << 0)
  681. );
  682. }
  683. /* DDR SDRAM Register Control Word 1 (DDR_SDRAM_RCW_1) */
  684. static void set_ddr_sdram_rcw_1(fsl_ddr_cfg_regs_t *ddr)
  685. {
  686. unsigned int rcw0 = 0; /* RCW0: Register Control Word 0 */
  687. unsigned int rcw1 = 0; /* RCW1: Register Control Word 1 */
  688. unsigned int rcw2 = 0; /* RCW2: Register Control Word 2 */
  689. unsigned int rcw3 = 0; /* RCW3: Register Control Word 3 */
  690. unsigned int rcw4 = 0; /* RCW4: Register Control Word 4 */
  691. unsigned int rcw5 = 0; /* RCW5: Register Control Word 5 */
  692. unsigned int rcw6 = 0; /* RCW6: Register Control Word 6 */
  693. unsigned int rcw7 = 0; /* RCW7: Register Control Word 7 */
  694. ddr->ddr_sdram_rcw_1 = (0
  695. | ((rcw0 & 0xF) << 28)
  696. | ((rcw1 & 0xF) << 24)
  697. | ((rcw2 & 0xF) << 20)
  698. | ((rcw3 & 0xF) << 16)
  699. | ((rcw4 & 0xF) << 12)
  700. | ((rcw5 & 0xF) << 8)
  701. | ((rcw6 & 0xF) << 4)
  702. | ((rcw7 & 0xF) << 0)
  703. );
  704. }
  705. /* DDR SDRAM Register Control Word 2 (DDR_SDRAM_RCW_2) */
  706. static void set_ddr_sdram_rcw_2(fsl_ddr_cfg_regs_t *ddr)
  707. {
  708. unsigned int rcw8 = 0; /* RCW0: Register Control Word 8 */
  709. unsigned int rcw9 = 0; /* RCW1: Register Control Word 9 */
  710. unsigned int rcw10 = 0; /* RCW2: Register Control Word 10 */
  711. unsigned int rcw11 = 0; /* RCW3: Register Control Word 11 */
  712. unsigned int rcw12 = 0; /* RCW4: Register Control Word 12 */
  713. unsigned int rcw13 = 0; /* RCW5: Register Control Word 13 */
  714. unsigned int rcw14 = 0; /* RCW6: Register Control Word 14 */
  715. unsigned int rcw15 = 0; /* RCW7: Register Control Word 15 */
  716. ddr->ddr_sdram_rcw_2 = (0
  717. | ((rcw8 & 0xF) << 28)
  718. | ((rcw9 & 0xF) << 24)
  719. | ((rcw10 & 0xF) << 20)
  720. | ((rcw11 & 0xF) << 16)
  721. | ((rcw12 & 0xF) << 12)
  722. | ((rcw13 & 0xF) << 8)
  723. | ((rcw14 & 0xF) << 4)
  724. | ((rcw15 & 0xF) << 0)
  725. );
  726. }
  727. unsigned int
  728. check_fsl_memctl_config_regs(const fsl_ddr_cfg_regs_t *ddr)
  729. {
  730. unsigned int res = 0;
  731. /*
  732. * Check that DDR_SDRAM_CFG[RD_EN] and DDR_SDRAM_CFG[2T_EN] are
  733. * not set at the same time.
  734. */
  735. if (ddr->ddr_sdram_cfg & 0x10000000
  736. && ddr->ddr_sdram_cfg & 0x00008000) {
  737. printf("Error: DDR_SDRAM_CFG[RD_EN] and DDR_SDRAM_CFG[2T_EN] "
  738. " should not be set at the same time.\n");
  739. res++;
  740. }
  741. return res;
  742. }
  743. unsigned int
  744. compute_fsl_memctl_config_regs(const memctl_options_t *popts,
  745. fsl_ddr_cfg_regs_t *ddr,
  746. const common_timing_params_t *common_dimm,
  747. const dimm_params_t *dimm_params,
  748. unsigned int dbw_cap_adj)
  749. {
  750. unsigned int i;
  751. unsigned int cas_latency;
  752. unsigned int additive_latency;
  753. memset(ddr, 0, sizeof(fsl_ddr_cfg_regs_t));
  754. if (common_dimm == NULL) {
  755. printf("Error: subset DIMM params struct null pointer\n");
  756. return 1;
  757. }
  758. /*
  759. * Process overrides first.
  760. *
  761. * FIXME: somehow add dereated caslat to this
  762. */
  763. cas_latency = (popts->cas_latency_override)
  764. ? popts->cas_latency_override_value
  765. : common_dimm->lowest_common_SPD_caslat;
  766. additive_latency = (popts->additive_latency_override)
  767. ? popts->additive_latency_override_value
  768. : common_dimm->additive_latency;
  769. /* Chip Select Memory Bounds (CSn_BNDS) */
  770. for (i = 0; i < CONFIG_CHIP_SELECTS_PER_CTRL; i++) {
  771. phys_size_t sa = 0;
  772. phys_size_t ea = 0;
  773. if (popts->ba_intlv_ctl && i > 0) {
  774. /* Don't set up boundaries if bank interleaving */
  775. break;
  776. }
  777. if (dimm_params[i/2].n_ranks == 0) {
  778. debug("Skipping setup of CS%u "
  779. "because n_ranks on DIMM %u is 0\n", i, i/2);
  780. continue;
  781. }
  782. if (popts->memctl_interleaving && popts->ba_intlv_ctl) {
  783. /*
  784. * This works superbank 2CS
  785. * There are 2 memory controllers configured
  786. * identically, memory is interleaved between them,
  787. * and each controller uses rank interleaving within
  788. * itself. Therefore the starting and ending address
  789. * on each controller is twice the amount present on
  790. * each controller.
  791. */
  792. ea = (2 * common_dimm->total_mem >> dbw_cap_adj) - 1;
  793. }
  794. else if (!popts->memctl_interleaving && popts->ba_intlv_ctl) {
  795. /*
  796. * If memory interleaving between controllers is NOT
  797. * enabled, the starting address for each memory
  798. * controller is distinct. However, because rank
  799. * interleaving is enabled, the starting and ending
  800. * addresses of the total memory on that memory
  801. * controller needs to be programmed into its
  802. * respective CS0_BNDS.
  803. */
  804. sa = common_dimm->base_address;
  805. ea = sa + (common_dimm->total_mem >> dbw_cap_adj) - 1;
  806. }
  807. else if (popts->memctl_interleaving && !popts->ba_intlv_ctl) {
  808. /*
  809. * Only the rank on CS0 of each memory controller may
  810. * be used if memory controller interleaving is used
  811. * without rank interleaving within each memory
  812. * controller. However, the ending address programmed
  813. * into each CS0 must be the sum of the amount of
  814. * memory in the two CS0 ranks.
  815. */
  816. if (i == 0) {
  817. unsigned long long rank_density
  818. = dimm_params[0].rank_density;
  819. ea = (2 * (rank_density >> dbw_cap_adj)) - 1;
  820. }
  821. }
  822. else if (!popts->memctl_interleaving && !popts->ba_intlv_ctl) {
  823. /*
  824. * No rank interleaving and no memory controller
  825. * interleaving.
  826. */
  827. unsigned long long rank_density
  828. = dimm_params[i/2].rank_density;
  829. sa = dimm_params[i/2].base_address;
  830. ea = sa + (rank_density >> dbw_cap_adj) - 1;
  831. if (i&1) {
  832. if ((dimm_params[i/2].n_ranks == 1)) {
  833. /* Odd chip select, single-rank dimm */
  834. sa = 0;
  835. ea = 0;
  836. } else {
  837. /* Odd chip select, dual-rank DIMM */
  838. sa += rank_density >> dbw_cap_adj;
  839. ea += rank_density >> dbw_cap_adj;
  840. }
  841. }
  842. }
  843. sa >>= 24;
  844. ea >>= 24;
  845. ddr->cs[i].bnds = (0
  846. | ((sa & 0xFFF) << 16) /* starting address MSB */
  847. | ((ea & 0xFFF) << 0) /* ending address MSB */
  848. );
  849. set_csn_config(i, ddr, popts, dimm_params);
  850. set_csn_config_2(i, ddr);
  851. }
  852. #if defined(CONFIG_FSL_DDR2)
  853. set_timing_cfg_0(ddr);
  854. #endif
  855. set_timing_cfg_3(ddr, common_dimm);
  856. set_timing_cfg_1(ddr, common_dimm, cas_latency);
  857. set_timing_cfg_2(ddr, popts, common_dimm,
  858. cas_latency, additive_latency);
  859. set_ddr_sdram_cfg(ddr, popts, common_dimm);
  860. set_ddr_sdram_cfg_2(ddr, popts);
  861. set_ddr_sdram_mode(ddr, popts, common_dimm,
  862. cas_latency, additive_latency);
  863. set_ddr_sdram_mode_2(ddr);
  864. set_ddr_sdram_interval(ddr, popts, common_dimm);
  865. set_ddr_data_init(ddr);
  866. set_ddr_sdram_clk_cntl(ddr, popts);
  867. set_ddr_init_addr(ddr);
  868. set_ddr_init_ext_addr(ddr);
  869. set_timing_cfg_4(ddr);
  870. set_timing_cfg_5(ddr);
  871. set_ddr_zq_cntl(ddr);
  872. set_ddr_wrlvl_cntl(ddr);
  873. set_ddr_pd_cntl(ddr);
  874. set_ddr_sr_cntr(ddr);
  875. set_ddr_sdram_rcw_1(ddr);
  876. set_ddr_sdram_rcw_2(ddr);
  877. return check_fsl_memctl_config_regs(ddr);
  878. }