2 * Copyright 2008-2011 Freescale Semiconductor, Inc.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * Version 2 as published by the Free Software Foundation.
11 #include <asm/fsl_ddr_sdram.h>
12 #include <asm/processor.h>
14 #if (CONFIG_CHIP_SELECTS_PER_CTRL > 4)
15 #error Invalid setting for CONFIG_CHIP_SELECTS_PER_CTRL
18 void fsl_ddr_set_memctl_regs(const fsl_ddr_cfg_regs_t *regs,
19 unsigned int ctrl_num)
22 volatile ccsr_ddr_t *ddr;
27 ddr = (void *)CONFIG_SYS_MPC85xx_DDR_ADDR;
30 ddr = (void *)CONFIG_SYS_MPC85xx_DDR2_ADDR;
33 printf("%s unexpected ctrl_num = %u\n", __FUNCTION__, ctrl_num);
37 out_be32(&ddr->eor, regs->ddr_eor);
39 for (i = 0; i < CONFIG_CHIP_SELECTS_PER_CTRL; i++) {
41 out_be32(&ddr->cs0_bnds, regs->cs[i].bnds);
42 out_be32(&ddr->cs0_config, regs->cs[i].config);
43 out_be32(&ddr->cs0_config_2, regs->cs[i].config_2);
46 out_be32(&ddr->cs1_bnds, regs->cs[i].bnds);
47 out_be32(&ddr->cs1_config, regs->cs[i].config);
48 out_be32(&ddr->cs1_config_2, regs->cs[i].config_2);
51 out_be32(&ddr->cs2_bnds, regs->cs[i].bnds);
52 out_be32(&ddr->cs2_config, regs->cs[i].config);
53 out_be32(&ddr->cs2_config_2, regs->cs[i].config_2);
56 out_be32(&ddr->cs3_bnds, regs->cs[i].bnds);
57 out_be32(&ddr->cs3_config, regs->cs[i].config);
58 out_be32(&ddr->cs3_config_2, regs->cs[i].config_2);
62 out_be32(&ddr->timing_cfg_3, regs->timing_cfg_3);
63 out_be32(&ddr->timing_cfg_0, regs->timing_cfg_0);
64 out_be32(&ddr->timing_cfg_1, regs->timing_cfg_1);
65 out_be32(&ddr->timing_cfg_2, regs->timing_cfg_2);
66 out_be32(&ddr->sdram_cfg_2, regs->ddr_sdram_cfg_2);
67 out_be32(&ddr->sdram_mode, regs->ddr_sdram_mode);
68 out_be32(&ddr->sdram_mode_2, regs->ddr_sdram_mode_2);
69 out_be32(&ddr->sdram_mode_3, regs->ddr_sdram_mode_3);
70 out_be32(&ddr->sdram_mode_4, regs->ddr_sdram_mode_4);
71 out_be32(&ddr->sdram_mode_5, regs->ddr_sdram_mode_5);
72 out_be32(&ddr->sdram_mode_6, regs->ddr_sdram_mode_6);
73 out_be32(&ddr->sdram_mode_7, regs->ddr_sdram_mode_7);
74 out_be32(&ddr->sdram_mode_8, regs->ddr_sdram_mode_8);
75 out_be32(&ddr->sdram_md_cntl, regs->ddr_sdram_md_cntl);
76 out_be32(&ddr->sdram_interval, regs->ddr_sdram_interval);
77 out_be32(&ddr->sdram_data_init, regs->ddr_data_init);
78 out_be32(&ddr->sdram_clk_cntl, regs->ddr_sdram_clk_cntl);
79 out_be32(&ddr->init_addr, regs->ddr_init_addr);
80 out_be32(&ddr->init_ext_addr, regs->ddr_init_ext_addr);
82 out_be32(&ddr->timing_cfg_4, regs->timing_cfg_4);
83 out_be32(&ddr->timing_cfg_5, regs->timing_cfg_5);
84 out_be32(&ddr->ddr_zq_cntl, regs->ddr_zq_cntl);
85 out_be32(&ddr->ddr_wrlvl_cntl, regs->ddr_wrlvl_cntl);
86 out_be32(&ddr->ddr_sr_cntr, regs->ddr_sr_cntr);
87 out_be32(&ddr->ddr_sdram_rcw_1, regs->ddr_sdram_rcw_1);
88 out_be32(&ddr->ddr_sdram_rcw_2, regs->ddr_sdram_rcw_2);
89 out_be32(&ddr->ddr_cdr1, regs->ddr_cdr1);
90 out_be32(&ddr->ddr_cdr2, regs->ddr_cdr2);
91 out_be32(&ddr->err_disable, regs->err_disable);
92 out_be32(&ddr->err_int_en, regs->err_int_en);
93 for (i = 0; i < 32; i++)
94 out_be32(&ddr->debug[i], regs->debug[i]);
96 /* Set, but do not enable the memory */
97 temp_sdram_cfg = regs->ddr_sdram_cfg;
98 temp_sdram_cfg &= ~(SDRAM_CFG_MEM_EN);
99 out_be32(&ddr->sdram_cfg, temp_sdram_cfg);
100 #ifdef CONFIG_SYS_FSL_ERRATUM_DDR_A003
101 if (regs->ddr_sdram_rcw_2 & 0x00f00000) {
102 out_be32(&ddr->timing_cfg_2, regs->timing_cfg_2 & 0xf07fffff);
103 out_be32(&ddr->debug[2], 0x00000400);
104 out_be32(&ddr->ddr_zq_cntl, regs->ddr_zq_cntl & 0x7fffffff);
105 out_be32(&ddr->ddr_wrlvl_cntl, regs->ddr_wrlvl_cntl & 0x7fffffff);
106 out_be32(&ddr->sdram_cfg_2, regs->ddr_sdram_cfg_2 & 0xffffffeb);
107 out_be32(&ddr->mtcr, 0);
108 out_be32(&ddr->debug[12], 0x00000015);
109 out_be32(&ddr->debug[21], 0x24000000);
110 out_be32(&ddr->sdram_interval, regs->ddr_sdram_interval & 0xffff);
111 out_be32(&ddr->sdram_cfg, temp_sdram_cfg | SDRAM_CFG_BI | SDRAM_CFG_MEM_EN);
113 asm volatile("sync;isync");
114 while (!(in_be32(&ddr->debug[1]) & 0x2))
117 switch (regs->ddr_sdram_rcw_2 & 0x00f00000) {
119 out_be32(&ddr->sdram_md_cntl,
121 MD_CNTL_CS_SEL_CS0_CS1 |
124 MD_CNTL_MD_VALUE(0x02));
127 out_be32(&ddr->sdram_md_cntl,
129 MD_CNTL_CS_SEL_CS0_CS1 |
132 MD_CNTL_MD_VALUE(0x0a));
135 out_be32(&ddr->sdram_md_cntl,
137 MD_CNTL_CS_SEL_CS0_CS1 |
140 MD_CNTL_MD_VALUE(0x12));
143 out_be32(&ddr->sdram_md_cntl,
145 MD_CNTL_CS_SEL_CS0_CS1 |
148 MD_CNTL_MD_VALUE(0x1a));
151 out_be32(&ddr->sdram_md_cntl,
153 MD_CNTL_CS_SEL_CS0_CS1 |
156 MD_CNTL_MD_VALUE(0x02));
157 printf("Unsupported RC10\n");
161 while (in_be32(&ddr->sdram_md_cntl) & 0x80000000)
164 out_be32(&ddr->sdram_cfg, temp_sdram_cfg);
165 out_be32(&ddr->timing_cfg_2, regs->timing_cfg_2);
166 out_be32(&ddr->debug[2], 0x0);
167 out_be32(&ddr->ddr_zq_cntl, regs->ddr_zq_cntl);
168 out_be32(&ddr->ddr_wrlvl_cntl, regs->ddr_wrlvl_cntl);
169 out_be32(&ddr->sdram_cfg_2, regs->ddr_sdram_cfg_2);
170 out_be32(&ddr->debug[12], 0x0);
171 out_be32(&ddr->debug[21], 0x0);
172 out_be32(&ddr->sdram_interval, regs->ddr_sdram_interval);
177 * For 8572 DDR1 erratum - DDR controller may enter illegal state
178 * when operatiing in 32-bit bus mode with 4-beat bursts,
179 * This erratum does not affect DDR3 mode, only for DDR2 mode.
181 #ifdef CONFIG_SYS_FSL_ERRATUM_DDR_115
182 if ((((in_be32(&ddr->sdram_cfg) >> 24) & 0x7) == SDRAM_TYPE_DDR2)
183 && in_be32(&ddr->sdram_cfg) & 0x80000) {
184 /* set DEBUG_1[31] */
185 setbits_be32(&ddr->debug[0], 1);
190 * 500 painful micro-seconds must elapse between
191 * the DDR clock setup and the DDR config enable.
192 * DDR2 need 200 us, and DDR3 need 500 us from spec,
193 * we choose the max, that is 500 us for all of case.
196 asm volatile("sync;isync");
198 /* Let the controller go */
199 temp_sdram_cfg = in_be32(&ddr->sdram_cfg) & ~SDRAM_CFG_BI;
200 out_be32(&ddr->sdram_cfg, temp_sdram_cfg | SDRAM_CFG_MEM_EN);
201 asm volatile("sync;isync");
203 /* Poll DDR_SDRAM_CFG_2[D_INIT] bit until auto-data init is done. */
204 while (in_be32(&ddr->sdram_cfg_2) & 0x10) {
205 udelay(10000); /* throttle polling rate */