ctrl_regs.c 40 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418
  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. qd_en = popts->quad_rank_present ? 1 : 0;
  565. ddr->ddr_sdram_cfg_2 = (0
  566. | ((frc_sr & 0x1) << 31)
  567. | ((sr_ie & 0x1) << 30)
  568. | ((dll_rst_dis & 0x1) << 29)
  569. | ((dqs_cfg & 0x3) << 26)
  570. | ((odt_cfg & 0x3) << 21)
  571. | ((num_pr & 0xf) << 12)
  572. | (qd_en << 9)
  573. | ((obc_cfg & 0x1) << 6)
  574. | ((ap_en & 0x1) << 5)
  575. | ((d_init & 0x1) << 4)
  576. | ((rcw_en & 0x1) << 2)
  577. | ((md_en & 0x1) << 0)
  578. );
  579. debug("FSLDDR: ddr_sdram_cfg_2 = 0x%08x\n", ddr->ddr_sdram_cfg_2);
  580. }
  581. /* DDR SDRAM Mode configuration 2 (DDR_SDRAM_MODE_2) */
  582. static void set_ddr_sdram_mode_2(fsl_ddr_cfg_regs_t *ddr,
  583. const memctl_options_t *popts)
  584. {
  585. unsigned short esdmode2 = 0; /* Extended SDRAM mode 2 */
  586. unsigned short esdmode3 = 0; /* Extended SDRAM mode 3 */
  587. #if defined(CONFIG_FSL_DDR3)
  588. unsigned int rtt_wr = 0; /* Rtt_WR - dynamic ODT off */
  589. unsigned int srt = 0; /* self-refresh temerature, normal range */
  590. unsigned int asr = 0; /* auto self-refresh disable */
  591. unsigned int cwl = compute_cas_write_latency() - 5;
  592. unsigned int pasr = 0; /* partial array self refresh disable */
  593. if (popts->rtt_override)
  594. rtt_wr = popts->rtt_wr_override_value;
  595. esdmode2 = (0
  596. | ((rtt_wr & 0x3) << 9)
  597. | ((srt & 0x1) << 7)
  598. | ((asr & 0x1) << 6)
  599. | ((cwl & 0x7) << 3)
  600. | ((pasr & 0x7) << 0));
  601. #endif
  602. ddr->ddr_sdram_mode_2 = (0
  603. | ((esdmode2 & 0xFFFF) << 16)
  604. | ((esdmode3 & 0xFFFF) << 0)
  605. );
  606. debug("FSLDDR: ddr_sdram_mode_2 = 0x%08x\n", ddr->ddr_sdram_mode_2);
  607. }
  608. /* DDR SDRAM Interval Configuration (DDR_SDRAM_INTERVAL) */
  609. static void set_ddr_sdram_interval(fsl_ddr_cfg_regs_t *ddr,
  610. const memctl_options_t *popts,
  611. const common_timing_params_t *common_dimm)
  612. {
  613. unsigned int refint; /* Refresh interval */
  614. unsigned int bstopre; /* Precharge interval */
  615. refint = picos_to_mclk(common_dimm->refresh_rate_ps);
  616. bstopre = popts->bstopre;
  617. /* refint field used 0x3FFF in earlier controllers */
  618. ddr->ddr_sdram_interval = (0
  619. | ((refint & 0xFFFF) << 16)
  620. | ((bstopre & 0x3FFF) << 0)
  621. );
  622. debug("FSLDDR: ddr_sdram_interval = 0x%08x\n", ddr->ddr_sdram_interval);
  623. }
  624. #if defined(CONFIG_FSL_DDR3)
  625. /* DDR SDRAM Mode configuration set (DDR_SDRAM_MODE) */
  626. static void set_ddr_sdram_mode(fsl_ddr_cfg_regs_t *ddr,
  627. const memctl_options_t *popts,
  628. const common_timing_params_t *common_dimm,
  629. unsigned int cas_latency,
  630. unsigned int additive_latency)
  631. {
  632. unsigned short esdmode; /* Extended SDRAM mode */
  633. unsigned short sdmode; /* SDRAM mode */
  634. /* Mode Register - MR1 */
  635. unsigned int qoff = 0; /* Output buffer enable 0=yes, 1=no */
  636. unsigned int tdqs_en = 0; /* TDQS Enable: 0=no, 1=yes */
  637. unsigned int rtt;
  638. unsigned int wrlvl_en = 0; /* Write level enable: 0=no, 1=yes */
  639. unsigned int al = 0; /* Posted CAS# additive latency (AL) */
  640. unsigned int dic = 1; /* Output driver impedance, 34ohm */
  641. unsigned int dll_en = 0; /* DLL Enable 0=Enable (Normal),
  642. 1=Disable (Test/Debug) */
  643. /* Mode Register - MR0 */
  644. unsigned int dll_on; /* DLL control for precharge PD, 0=off, 1=on */
  645. unsigned int wr; /* Write Recovery */
  646. unsigned int dll_rst; /* DLL Reset */
  647. unsigned int mode; /* Normal=0 or Test=1 */
  648. unsigned int caslat = 4;/* CAS# latency, default set as 6 cycles */
  649. /* BT: Burst Type (0=Nibble Sequential, 1=Interleaved) */
  650. unsigned int bt;
  651. unsigned int bl; /* BL: Burst Length */
  652. unsigned int wr_mclk;
  653. const unsigned int mclk_ps = get_memory_clk_period_ps();
  654. rtt = fsl_ddr_get_rtt();
  655. if (popts->rtt_override)
  656. rtt = popts->rtt_override_value;
  657. if (additive_latency == (cas_latency - 1))
  658. al = 1;
  659. if (additive_latency == (cas_latency - 2))
  660. al = 2;
  661. /*
  662. * The esdmode value will also be used for writing
  663. * MR1 during write leveling for DDR3, although the
  664. * bits specifically related to the write leveling
  665. * scheme will be handled automatically by the DDR
  666. * controller. so we set the wrlvl_en = 0 here.
  667. */
  668. esdmode = (0
  669. | ((qoff & 0x1) << 12)
  670. | ((tdqs_en & 0x1) << 11)
  671. | ((rtt & 0x4) << 7) /* rtt field is split */
  672. | ((wrlvl_en & 0x1) << 7)
  673. | ((rtt & 0x2) << 5) /* rtt field is split */
  674. | ((dic & 0x2) << 4) /* DIC field is split */
  675. | ((al & 0x3) << 3)
  676. | ((rtt & 0x1) << 2) /* rtt field is split */
  677. | ((dic & 0x1) << 1) /* DIC field is split */
  678. | ((dll_en & 0x1) << 0)
  679. );
  680. /*
  681. * DLL control for precharge PD
  682. * 0=slow exit DLL off (tXPDLL)
  683. * 1=fast exit DLL on (tXP)
  684. */
  685. dll_on = 1;
  686. wr_mclk = (common_dimm->tWR_ps + mclk_ps - 1) / mclk_ps;
  687. if (wr_mclk >= 12)
  688. wr = 6;
  689. else if (wr_mclk >= 9)
  690. wr = 5;
  691. else
  692. wr = wr_mclk - 4;
  693. dll_rst = 0; /* dll no reset */
  694. mode = 0; /* normal mode */
  695. /* look up table to get the cas latency bits */
  696. if (cas_latency >= 5 && cas_latency <= 11) {
  697. unsigned char cas_latency_table[7] = {
  698. 0x2, /* 5 clocks */
  699. 0x4, /* 6 clocks */
  700. 0x6, /* 7 clocks */
  701. 0x8, /* 8 clocks */
  702. 0xa, /* 9 clocks */
  703. 0xc, /* 10 clocks */
  704. 0xe /* 11 clocks */
  705. };
  706. caslat = cas_latency_table[cas_latency - 5];
  707. }
  708. bt = 0; /* Nibble sequential */
  709. switch (popts->burst_length) {
  710. case DDR_BL8:
  711. bl = 0;
  712. break;
  713. case DDR_OTF:
  714. bl = 1;
  715. break;
  716. case DDR_BC4:
  717. bl = 2;
  718. break;
  719. default:
  720. printf("Error: invalid burst length of %u specified. "
  721. " Defaulting to on-the-fly BC4 or BL8 beats.\n",
  722. popts->burst_length);
  723. bl = 1;
  724. break;
  725. }
  726. sdmode = (0
  727. | ((dll_on & 0x1) << 12)
  728. | ((wr & 0x7) << 9)
  729. | ((dll_rst & 0x1) << 8)
  730. | ((mode & 0x1) << 7)
  731. | (((caslat >> 1) & 0x7) << 4)
  732. | ((bt & 0x1) << 3)
  733. | ((bl & 0x3) << 0)
  734. );
  735. ddr->ddr_sdram_mode = (0
  736. | ((esdmode & 0xFFFF) << 16)
  737. | ((sdmode & 0xFFFF) << 0)
  738. );
  739. debug("FSLDDR: ddr_sdram_mode = 0x%08x\n", ddr->ddr_sdram_mode);
  740. }
  741. #else /* !CONFIG_FSL_DDR3 */
  742. /* DDR SDRAM Mode configuration set (DDR_SDRAM_MODE) */
  743. static void set_ddr_sdram_mode(fsl_ddr_cfg_regs_t *ddr,
  744. const memctl_options_t *popts,
  745. const common_timing_params_t *common_dimm,
  746. unsigned int cas_latency,
  747. unsigned int additive_latency)
  748. {
  749. unsigned short esdmode; /* Extended SDRAM mode */
  750. unsigned short sdmode; /* SDRAM mode */
  751. /*
  752. * FIXME: This ought to be pre-calculated in a
  753. * technology-specific routine,
  754. * e.g. compute_DDR2_mode_register(), and then the
  755. * sdmode and esdmode passed in as part of common_dimm.
  756. */
  757. /* Extended Mode Register */
  758. unsigned int mrs = 0; /* Mode Register Set */
  759. unsigned int outputs = 0; /* 0=Enabled, 1=Disabled */
  760. unsigned int rdqs_en = 0; /* RDQS Enable: 0=no, 1=yes */
  761. unsigned int dqs_en = 0; /* DQS# Enable: 0=enable, 1=disable */
  762. unsigned int ocd = 0; /* 0x0=OCD not supported,
  763. 0x7=OCD default state */
  764. unsigned int rtt;
  765. unsigned int al; /* Posted CAS# additive latency (AL) */
  766. unsigned int ods = 0; /* Output Drive Strength:
  767. 0 = Full strength (18ohm)
  768. 1 = Reduced strength (4ohm) */
  769. unsigned int dll_en = 0; /* DLL Enable 0=Enable (Normal),
  770. 1=Disable (Test/Debug) */
  771. /* Mode Register (MR) */
  772. unsigned int mr; /* Mode Register Definition */
  773. unsigned int pd; /* Power-Down Mode */
  774. unsigned int wr; /* Write Recovery */
  775. unsigned int dll_res; /* DLL Reset */
  776. unsigned int mode; /* Normal=0 or Test=1 */
  777. unsigned int caslat = 0;/* CAS# latency */
  778. /* BT: Burst Type (0=Sequential, 1=Interleaved) */
  779. unsigned int bt;
  780. unsigned int bl; /* BL: Burst Length */
  781. #if defined(CONFIG_FSL_DDR2)
  782. const unsigned int mclk_ps = get_memory_clk_period_ps();
  783. #endif
  784. rtt = fsl_ddr_get_rtt();
  785. al = additive_latency;
  786. esdmode = (0
  787. | ((mrs & 0x3) << 14)
  788. | ((outputs & 0x1) << 12)
  789. | ((rdqs_en & 0x1) << 11)
  790. | ((dqs_en & 0x1) << 10)
  791. | ((ocd & 0x7) << 7)
  792. | ((rtt & 0x2) << 5) /* rtt field is split */
  793. | ((al & 0x7) << 3)
  794. | ((rtt & 0x1) << 2) /* rtt field is split */
  795. | ((ods & 0x1) << 1)
  796. | ((dll_en & 0x1) << 0)
  797. );
  798. mr = 0; /* FIXME: CHECKME */
  799. /*
  800. * 0 = Fast Exit (Normal)
  801. * 1 = Slow Exit (Low Power)
  802. */
  803. pd = 0;
  804. #if defined(CONFIG_FSL_DDR1)
  805. wr = 0; /* Historical */
  806. #elif defined(CONFIG_FSL_DDR2)
  807. wr = (common_dimm->tWR_ps + mclk_ps - 1) / mclk_ps - 1;
  808. #endif
  809. dll_res = 0;
  810. mode = 0;
  811. #if defined(CONFIG_FSL_DDR1)
  812. if (1 <= cas_latency && cas_latency <= 4) {
  813. unsigned char mode_caslat_table[4] = {
  814. 0x5, /* 1.5 clocks */
  815. 0x2, /* 2.0 clocks */
  816. 0x6, /* 2.5 clocks */
  817. 0x3 /* 3.0 clocks */
  818. };
  819. caslat = mode_caslat_table[cas_latency - 1];
  820. } else {
  821. printf("Warning: unknown cas_latency %d\n", cas_latency);
  822. }
  823. #elif defined(CONFIG_FSL_DDR2)
  824. caslat = cas_latency;
  825. #endif
  826. bt = 0;
  827. switch (popts->burst_length) {
  828. case DDR_BL4:
  829. bl = 2;
  830. break;
  831. case DDR_BL8:
  832. bl = 3;
  833. break;
  834. default:
  835. printf("Error: invalid burst length of %u specified. "
  836. " Defaulting to 4 beats.\n",
  837. popts->burst_length);
  838. bl = 2;
  839. break;
  840. }
  841. sdmode = (0
  842. | ((mr & 0x3) << 14)
  843. | ((pd & 0x1) << 12)
  844. | ((wr & 0x7) << 9)
  845. | ((dll_res & 0x1) << 8)
  846. | ((mode & 0x1) << 7)
  847. | ((caslat & 0x7) << 4)
  848. | ((bt & 0x1) << 3)
  849. | ((bl & 0x7) << 0)
  850. );
  851. ddr->ddr_sdram_mode = (0
  852. | ((esdmode & 0xFFFF) << 16)
  853. | ((sdmode & 0xFFFF) << 0)
  854. );
  855. debug("FSLDDR: ddr_sdram_mode = 0x%08x\n", ddr->ddr_sdram_mode);
  856. }
  857. #endif
  858. /* DDR SDRAM Data Initialization (DDR_DATA_INIT) */
  859. static void set_ddr_data_init(fsl_ddr_cfg_regs_t *ddr)
  860. {
  861. unsigned int init_value; /* Initialization value */
  862. init_value = 0xDEADBEEF;
  863. ddr->ddr_data_init = init_value;
  864. }
  865. /*
  866. * DDR SDRAM Clock Control (DDR_SDRAM_CLK_CNTL)
  867. * The old controller on the 8540/60 doesn't have this register.
  868. * Hope it's OK to set it (to 0) anyway.
  869. */
  870. static void set_ddr_sdram_clk_cntl(fsl_ddr_cfg_regs_t *ddr,
  871. const memctl_options_t *popts)
  872. {
  873. unsigned int clk_adjust; /* Clock adjust */
  874. clk_adjust = popts->clk_adjust;
  875. ddr->ddr_sdram_clk_cntl = (clk_adjust & 0xF) << 23;
  876. debug("FSLDDR: clk_cntl = 0x%08x\n", ddr->ddr_sdram_clk_cntl);
  877. }
  878. /* DDR Initialization Address (DDR_INIT_ADDR) */
  879. static void set_ddr_init_addr(fsl_ddr_cfg_regs_t *ddr)
  880. {
  881. unsigned int init_addr = 0; /* Initialization address */
  882. ddr->ddr_init_addr = init_addr;
  883. }
  884. /* DDR Initialization Address (DDR_INIT_EXT_ADDR) */
  885. static void set_ddr_init_ext_addr(fsl_ddr_cfg_regs_t *ddr)
  886. {
  887. unsigned int uia = 0; /* Use initialization address */
  888. unsigned int init_ext_addr = 0; /* Initialization address */
  889. ddr->ddr_init_ext_addr = (0
  890. | ((uia & 0x1) << 31)
  891. | (init_ext_addr & 0xF)
  892. );
  893. }
  894. /* DDR SDRAM Timing Configuration 4 (TIMING_CFG_4) */
  895. static void set_timing_cfg_4(fsl_ddr_cfg_regs_t *ddr,
  896. const memctl_options_t *popts)
  897. {
  898. unsigned int rwt = 0; /* Read-to-write turnaround for same CS */
  899. unsigned int wrt = 0; /* Write-to-read turnaround for same CS */
  900. unsigned int rrt = 0; /* Read-to-read turnaround for same CS */
  901. unsigned int wwt = 0; /* Write-to-write turnaround for same CS */
  902. unsigned int dll_lock = 0; /* DDR SDRAM DLL Lock Time */
  903. #if defined(CONFIG_FSL_DDR3)
  904. if (popts->burst_length == DDR_BL8) {
  905. /* We set BL/2 for fixed BL8 */
  906. rrt = 0; /* BL/2 clocks */
  907. wwt = 0; /* BL/2 clocks */
  908. } else {
  909. /* We need to set BL/2 + 2 to BC4 and OTF */
  910. rrt = 2; /* BL/2 + 2 clocks */
  911. wwt = 2; /* BL/2 + 2 clocks */
  912. }
  913. dll_lock = 1; /* tDLLK = 512 clocks from spec */
  914. #endif
  915. ddr->timing_cfg_4 = (0
  916. | ((rwt & 0xf) << 28)
  917. | ((wrt & 0xf) << 24)
  918. | ((rrt & 0xf) << 20)
  919. | ((wwt & 0xf) << 16)
  920. | (dll_lock & 0x3)
  921. );
  922. debug("FSLDDR: timing_cfg_4 = 0x%08x\n", ddr->timing_cfg_4);
  923. }
  924. /* DDR SDRAM Timing Configuration 5 (TIMING_CFG_5) */
  925. static void set_timing_cfg_5(fsl_ddr_cfg_regs_t *ddr)
  926. {
  927. unsigned int rodt_on = 0; /* Read to ODT on */
  928. unsigned int rodt_off = 0; /* Read to ODT off */
  929. unsigned int wodt_on = 0; /* Write to ODT on */
  930. unsigned int wodt_off = 0; /* Write to ODT off */
  931. #if defined(CONFIG_FSL_DDR3)
  932. rodt_on = 2; /* 2 clocks */
  933. rodt_off = 4; /* 4 clocks */
  934. wodt_on = 1; /* 1 clocks */
  935. wodt_off = 4; /* 4 clocks */
  936. #endif
  937. ddr->timing_cfg_5 = (0
  938. | ((rodt_on & 0x1f) << 24)
  939. | ((rodt_off & 0x7) << 20)
  940. | ((wodt_on & 0x1f) << 12)
  941. | ((wodt_off & 0x7) << 8)
  942. );
  943. debug("FSLDDR: timing_cfg_5 = 0x%08x\n", ddr->timing_cfg_5);
  944. }
  945. /* DDR ZQ Calibration Control (DDR_ZQ_CNTL) */
  946. static void set_ddr_zq_cntl(fsl_ddr_cfg_regs_t *ddr, unsigned int zq_en)
  947. {
  948. unsigned int zqinit = 0;/* POR ZQ Calibration Time (tZQinit) */
  949. /* Normal Operation Full Calibration Time (tZQoper) */
  950. unsigned int zqoper = 0;
  951. /* Normal Operation Short Calibration Time (tZQCS) */
  952. unsigned int zqcs = 0;
  953. if (zq_en) {
  954. zqinit = 9; /* 512 clocks */
  955. zqoper = 8; /* 256 clocks */
  956. zqcs = 6; /* 64 clocks */
  957. }
  958. ddr->ddr_zq_cntl = (0
  959. | ((zq_en & 0x1) << 31)
  960. | ((zqinit & 0xF) << 24)
  961. | ((zqoper & 0xF) << 16)
  962. | ((zqcs & 0xF) << 8)
  963. );
  964. }
  965. /* DDR Write Leveling Control (DDR_WRLVL_CNTL) */
  966. static void set_ddr_wrlvl_cntl(fsl_ddr_cfg_regs_t *ddr, unsigned int wrlvl_en,
  967. const memctl_options_t *popts)
  968. {
  969. /*
  970. * First DQS pulse rising edge after margining mode
  971. * is programmed (tWL_MRD)
  972. */
  973. unsigned int wrlvl_mrd = 0;
  974. /* ODT delay after margining mode is programmed (tWL_ODTEN) */
  975. unsigned int wrlvl_odten = 0;
  976. /* DQS/DQS_ delay after margining mode is programmed (tWL_DQSEN) */
  977. unsigned int wrlvl_dqsen = 0;
  978. /* WRLVL_SMPL: Write leveling sample time */
  979. unsigned int wrlvl_smpl = 0;
  980. /* WRLVL_WLR: Write leveling repeition time */
  981. unsigned int wrlvl_wlr = 0;
  982. /* WRLVL_START: Write leveling start time */
  983. unsigned int wrlvl_start = 0;
  984. /* suggest enable write leveling for DDR3 due to fly-by topology */
  985. if (wrlvl_en) {
  986. /* tWL_MRD min = 40 nCK, we set it 64 */
  987. wrlvl_mrd = 0x6;
  988. /* tWL_ODTEN 128 */
  989. wrlvl_odten = 0x7;
  990. /* tWL_DQSEN min = 25 nCK, we set it 32 */
  991. wrlvl_dqsen = 0x5;
  992. /*
  993. * Write leveling sample time at least need 6 clocks
  994. * higher than tWLO to allow enough time for progagation
  995. * delay and sampling the prime data bits.
  996. */
  997. wrlvl_smpl = 0xf;
  998. /*
  999. * Write leveling repetition time
  1000. * at least tWLO + 6 clocks clocks
  1001. * we set it 64
  1002. */
  1003. wrlvl_wlr = 0x6;
  1004. /*
  1005. * Write leveling start time
  1006. * The value use for the DQS_ADJUST for the first sample
  1007. * when write leveling is enabled.
  1008. */
  1009. wrlvl_start = 0x8;
  1010. /*
  1011. * Override the write leveling sample and start time
  1012. * according to specific board
  1013. */
  1014. if (popts->wrlvl_override) {
  1015. wrlvl_smpl = popts->wrlvl_sample;
  1016. wrlvl_start = popts->wrlvl_start;
  1017. }
  1018. }
  1019. ddr->ddr_wrlvl_cntl = (0
  1020. | ((wrlvl_en & 0x1) << 31)
  1021. | ((wrlvl_mrd & 0x7) << 24)
  1022. | ((wrlvl_odten & 0x7) << 20)
  1023. | ((wrlvl_dqsen & 0x7) << 16)
  1024. | ((wrlvl_smpl & 0xf) << 12)
  1025. | ((wrlvl_wlr & 0x7) << 8)
  1026. | ((wrlvl_start & 0x1F) << 0)
  1027. );
  1028. }
  1029. /* DDR Self Refresh Counter (DDR_SR_CNTR) */
  1030. static void set_ddr_sr_cntr(fsl_ddr_cfg_regs_t *ddr, unsigned int sr_it)
  1031. {
  1032. /* Self Refresh Idle Threshold */
  1033. ddr->ddr_sr_cntr = (sr_it & 0xF) << 16;
  1034. }
  1035. static void set_ddr_eor(fsl_ddr_cfg_regs_t *ddr, const memctl_options_t *popts)
  1036. {
  1037. if (popts->addr_hash) {
  1038. ddr->ddr_eor = 0x40000000; /* address hash enable */
  1039. puts("Addess hashing enabled.\n");
  1040. }
  1041. }
  1042. unsigned int
  1043. check_fsl_memctl_config_regs(const fsl_ddr_cfg_regs_t *ddr)
  1044. {
  1045. unsigned int res = 0;
  1046. /*
  1047. * Check that DDR_SDRAM_CFG[RD_EN] and DDR_SDRAM_CFG[2T_EN] are
  1048. * not set at the same time.
  1049. */
  1050. if (ddr->ddr_sdram_cfg & 0x10000000
  1051. && ddr->ddr_sdram_cfg & 0x00008000) {
  1052. printf("Error: DDR_SDRAM_CFG[RD_EN] and DDR_SDRAM_CFG[2T_EN] "
  1053. " should not be set at the same time.\n");
  1054. res++;
  1055. }
  1056. return res;
  1057. }
  1058. unsigned int
  1059. compute_fsl_memctl_config_regs(const memctl_options_t *popts,
  1060. fsl_ddr_cfg_regs_t *ddr,
  1061. const common_timing_params_t *common_dimm,
  1062. const dimm_params_t *dimm_params,
  1063. unsigned int dbw_cap_adj)
  1064. {
  1065. unsigned int i;
  1066. unsigned int cas_latency;
  1067. unsigned int additive_latency;
  1068. unsigned int sr_it;
  1069. unsigned int zq_en;
  1070. unsigned int wrlvl_en;
  1071. memset(ddr, 0, sizeof(fsl_ddr_cfg_regs_t));
  1072. if (common_dimm == NULL) {
  1073. printf("Error: subset DIMM params struct null pointer\n");
  1074. return 1;
  1075. }
  1076. /*
  1077. * Process overrides first.
  1078. *
  1079. * FIXME: somehow add dereated caslat to this
  1080. */
  1081. cas_latency = (popts->cas_latency_override)
  1082. ? popts->cas_latency_override_value
  1083. : common_dimm->lowest_common_SPD_caslat;
  1084. additive_latency = (popts->additive_latency_override)
  1085. ? popts->additive_latency_override_value
  1086. : common_dimm->additive_latency;
  1087. sr_it = (popts->auto_self_refresh_en)
  1088. ? popts->sr_it
  1089. : 0;
  1090. /* ZQ calibration */
  1091. zq_en = (popts->zq_en) ? 1 : 0;
  1092. /* write leveling */
  1093. wrlvl_en = (popts->wrlvl_en) ? 1 : 0;
  1094. /* Chip Select Memory Bounds (CSn_BNDS) */
  1095. for (i = 0; i < CONFIG_CHIP_SELECTS_PER_CTRL; i++) {
  1096. unsigned long long ea = 0, sa = 0;
  1097. unsigned int cs_per_dimm
  1098. = CONFIG_CHIP_SELECTS_PER_CTRL / CONFIG_DIMM_SLOTS_PER_CTLR;
  1099. unsigned int dimm_number
  1100. = i / cs_per_dimm;
  1101. unsigned long long rank_density
  1102. = dimm_params[dimm_number].rank_density;
  1103. if (((i == 1) && (popts->ba_intlv_ctl & FSL_DDR_CS0_CS1)) ||
  1104. ((i == 2) && (popts->ba_intlv_ctl & 0x04)) ||
  1105. ((i == 3) && (popts->ba_intlv_ctl & FSL_DDR_CS2_CS3))) {
  1106. /*
  1107. * Don't set up boundaries for unused CS
  1108. * cs1 for cs0_cs1, cs0_cs1_and_cs2_cs3, cs0_cs1_cs2_cs3
  1109. * cs2 for cs0_cs1_cs2_cs3
  1110. * cs3 for cs2_cs3, cs0_cs1_and_cs2_cs3, cs0_cs1_cs2_cs3
  1111. * But we need to set the ODT_RD_CFG and
  1112. * ODT_WR_CFG for CS1_CONFIG here.
  1113. */
  1114. set_csn_config(dimm_number, i, ddr, popts, dimm_params);
  1115. continue;
  1116. }
  1117. if (dimm_params[dimm_number].n_ranks == 0) {
  1118. debug("Skipping setup of CS%u "
  1119. "because n_ranks on DIMM %u is 0\n", i, dimm_number);
  1120. continue;
  1121. }
  1122. if (popts->memctl_interleaving && popts->ba_intlv_ctl) {
  1123. /*
  1124. * This works superbank 2CS
  1125. * There are 2 or more memory controllers configured
  1126. * identically, memory is interleaved between them,
  1127. * and each controller uses rank interleaving within
  1128. * itself. Therefore the starting and ending address
  1129. * on each controller is twice the amount present on
  1130. * each controller.
  1131. */
  1132. unsigned long long ctlr_density = 0;
  1133. switch (popts->ba_intlv_ctl & FSL_DDR_CS0_CS1_CS2_CS3) {
  1134. case FSL_DDR_CS0_CS1:
  1135. case FSL_DDR_CS0_CS1_AND_CS2_CS3:
  1136. ctlr_density = dimm_params[0].rank_density * 2;
  1137. break;
  1138. case FSL_DDR_CS2_CS3:
  1139. ctlr_density = dimm_params[0].rank_density;
  1140. break;
  1141. case FSL_DDR_CS0_CS1_CS2_CS3:
  1142. /*
  1143. * The four CS interleaving should have been verified by
  1144. * populate_memctl_options()
  1145. */
  1146. ctlr_density = dimm_params[0].rank_density * 4;
  1147. break;
  1148. default:
  1149. break;
  1150. }
  1151. ea = (CONFIG_NUM_DDR_CONTROLLERS *
  1152. (ctlr_density >> dbw_cap_adj)) - 1;
  1153. }
  1154. else if (!popts->memctl_interleaving && popts->ba_intlv_ctl) {
  1155. /*
  1156. * If memory interleaving between controllers is NOT
  1157. * enabled, the starting address for each memory
  1158. * controller is distinct. However, because rank
  1159. * interleaving is enabled, the starting and ending
  1160. * addresses of the total memory on that memory
  1161. * controller needs to be programmed into its
  1162. * respective CS0_BNDS.
  1163. */
  1164. switch (popts->ba_intlv_ctl & FSL_DDR_CS0_CS1_CS2_CS3) {
  1165. case FSL_DDR_CS0_CS1_CS2_CS3:
  1166. /* CS0+CS1+CS2+CS3 interleaving, only CS0_CNDS
  1167. * needs to be set.
  1168. */
  1169. sa = common_dimm->base_address;
  1170. ea = sa + (4 * (rank_density >> dbw_cap_adj))-1;
  1171. break;
  1172. case FSL_DDR_CS0_CS1_AND_CS2_CS3:
  1173. /* CS0+CS1 and CS2+CS3 interleaving, CS0_CNDS
  1174. * and CS2_CNDS need to be set.
  1175. */
  1176. if ((i == 2) && (dimm_number == 0)) {
  1177. sa = dimm_params[dimm_number].base_address +
  1178. 2 * (rank_density >> dbw_cap_adj);
  1179. ea = sa + 2 * (rank_density >> dbw_cap_adj) - 1;
  1180. } else {
  1181. sa = dimm_params[dimm_number].base_address;
  1182. ea = sa + (2 * (rank_density >>
  1183. dbw_cap_adj)) - 1;
  1184. }
  1185. break;
  1186. case FSL_DDR_CS0_CS1:
  1187. /* CS0+CS1 interleaving, CS0_CNDS needs
  1188. * to be set
  1189. */
  1190. if (dimm_params[dimm_number].n_ranks > (i % cs_per_dimm)) {
  1191. sa = dimm_params[dimm_number].base_address;
  1192. ea = sa + (rank_density >> dbw_cap_adj) - 1;
  1193. sa += (i % cs_per_dimm) * (rank_density >> dbw_cap_adj);
  1194. ea += (i % cs_per_dimm) * (rank_density >> dbw_cap_adj);
  1195. } else {
  1196. sa = 0;
  1197. ea = 0;
  1198. }
  1199. if (i == 0)
  1200. ea += (rank_density >> dbw_cap_adj);
  1201. break;
  1202. case FSL_DDR_CS2_CS3:
  1203. /* CS2+CS3 interleaving*/
  1204. if (dimm_params[dimm_number].n_ranks > (i % cs_per_dimm)) {
  1205. sa = dimm_params[dimm_number].base_address;
  1206. ea = sa + (rank_density >> dbw_cap_adj) - 1;
  1207. sa += (i % cs_per_dimm) * (rank_density >> dbw_cap_adj);
  1208. ea += (i % cs_per_dimm) * (rank_density >> dbw_cap_adj);
  1209. } else {
  1210. sa = 0;
  1211. ea = 0;
  1212. }
  1213. if (i == 2)
  1214. ea += (rank_density >> dbw_cap_adj);
  1215. break;
  1216. default: /* No bank(chip-select) interleaving */
  1217. break;
  1218. }
  1219. }
  1220. else if (popts->memctl_interleaving && !popts->ba_intlv_ctl) {
  1221. /*
  1222. * Only the rank on CS0 of each memory controller may
  1223. * be used if memory controller interleaving is used
  1224. * without rank interleaving within each memory
  1225. * controller. However, the ending address programmed
  1226. * into each CS0 must be the sum of the amount of
  1227. * memory in the two CS0 ranks.
  1228. */
  1229. if (i == 0) {
  1230. ea = (2 * (rank_density >> dbw_cap_adj)) - 1;
  1231. }
  1232. }
  1233. else if (!popts->memctl_interleaving && !popts->ba_intlv_ctl) {
  1234. /*
  1235. * No rank interleaving and no memory controller
  1236. * interleaving.
  1237. */
  1238. sa = dimm_params[dimm_number].base_address;
  1239. ea = sa + (rank_density >> dbw_cap_adj) - 1;
  1240. if (dimm_params[dimm_number].n_ranks > (i % cs_per_dimm)) {
  1241. sa += (i % cs_per_dimm) * (rank_density >> dbw_cap_adj);
  1242. ea += (i % cs_per_dimm) * (rank_density >> dbw_cap_adj);
  1243. } else {
  1244. sa = 0;
  1245. ea = 0;
  1246. }
  1247. }
  1248. sa >>= 24;
  1249. ea >>= 24;
  1250. ddr->cs[i].bnds = (0
  1251. | ((sa & 0xFFF) << 16) /* starting address MSB */
  1252. | ((ea & 0xFFF) << 0) /* ending address MSB */
  1253. );
  1254. debug("FSLDDR: cs[%d]_bnds = 0x%08x\n", i, ddr->cs[i].bnds);
  1255. set_csn_config(dimm_number, i, ddr, popts, dimm_params);
  1256. set_csn_config_2(i, ddr);
  1257. }
  1258. set_ddr_eor(ddr, popts);
  1259. #if !defined(CONFIG_FSL_DDR1)
  1260. set_timing_cfg_0(ddr);
  1261. #endif
  1262. set_timing_cfg_3(ddr, common_dimm, cas_latency);
  1263. set_timing_cfg_1(ddr, popts, common_dimm, cas_latency);
  1264. set_timing_cfg_2(ddr, popts, common_dimm,
  1265. cas_latency, additive_latency);
  1266. set_ddr_sdram_cfg(ddr, popts, common_dimm);
  1267. set_ddr_sdram_cfg_2(ddr, popts);
  1268. set_ddr_sdram_mode(ddr, popts, common_dimm,
  1269. cas_latency, additive_latency);
  1270. set_ddr_sdram_mode_2(ddr, popts);
  1271. set_ddr_sdram_interval(ddr, popts, common_dimm);
  1272. set_ddr_data_init(ddr);
  1273. set_ddr_sdram_clk_cntl(ddr, popts);
  1274. set_ddr_init_addr(ddr);
  1275. set_ddr_init_ext_addr(ddr);
  1276. set_timing_cfg_4(ddr, popts);
  1277. set_timing_cfg_5(ddr);
  1278. set_ddr_zq_cntl(ddr, zq_en);
  1279. set_ddr_wrlvl_cntl(ddr, wrlvl_en, popts);
  1280. set_ddr_sr_cntr(ddr, sr_it);
  1281. set_ddr_sdram_rcw(ddr, common_dimm);
  1282. return check_fsl_memctl_config_regs(ddr);
  1283. }