ctrl_regs.c 40 KB

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