ctrl_regs.c 30 KB

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