149249e13SPoonam Aggrwal /*
249249e13SPoonam Aggrwal * Copyright 2010-2011 Freescale Semiconductor, Inc.
349249e13SPoonam Aggrwal *
41a459660SWolfgang Denk * SPDX-License-Identifier: GPL-2.0+
549249e13SPoonam Aggrwal */
649249e13SPoonam Aggrwal
749249e13SPoonam Aggrwal #include <common.h>
849249e13SPoonam Aggrwal #include <asm/mmu.h>
949249e13SPoonam Aggrwal #include <asm/immap_85xx.h>
1049249e13SPoonam Aggrwal #include <asm/processor.h>
11*5614e71bSYork Sun #include <fsl_ddr_sdram.h>
12*5614e71bSYork Sun #include <fsl_ddr_dimm_params.h>
1349249e13SPoonam Aggrwal #include <asm/io.h>
1449249e13SPoonam Aggrwal #include <asm/fsl_law.h>
1549249e13SPoonam Aggrwal
1649249e13SPoonam Aggrwal DECLARE_GLOBAL_DATA_PTR;
1749249e13SPoonam Aggrwal
181ba62f10SYork Sun #ifndef CONFIG_SYS_DDR_RAW_TIMING
1949249e13SPoonam Aggrwal #define CONFIG_SYS_DRAM_SIZE 1024
2049249e13SPoonam Aggrwal
2149249e13SPoonam Aggrwal fsl_ddr_cfg_regs_t ddr_cfg_regs_800 = {
2249249e13SPoonam Aggrwal .cs[0].bnds = CONFIG_SYS_DDR_CS0_BNDS,
2349249e13SPoonam Aggrwal .cs[0].config = CONFIG_SYS_DDR_CS0_CONFIG,
2449249e13SPoonam Aggrwal .cs[0].config_2 = CONFIG_SYS_DDR_CS0_CONFIG_2,
2549249e13SPoonam Aggrwal .timing_cfg_3 = CONFIG_SYS_DDR_TIMING_3_800,
2649249e13SPoonam Aggrwal .timing_cfg_0 = CONFIG_SYS_DDR_TIMING_0_800,
2749249e13SPoonam Aggrwal .timing_cfg_1 = CONFIG_SYS_DDR_TIMING_1_800,
2849249e13SPoonam Aggrwal .timing_cfg_2 = CONFIG_SYS_DDR_TIMING_2_800,
2949249e13SPoonam Aggrwal .ddr_sdram_cfg = CONFIG_SYS_DDR_CONTROL,
3049249e13SPoonam Aggrwal .ddr_sdram_cfg_2 = CONFIG_SYS_DDR_CONTROL_2,
3149249e13SPoonam Aggrwal .ddr_sdram_mode = CONFIG_SYS_DDR_MODE_1_800,
3249249e13SPoonam Aggrwal .ddr_sdram_mode_2 = CONFIG_SYS_DDR_MODE_2_800,
3349249e13SPoonam Aggrwal .ddr_sdram_md_cntl = CONFIG_SYS_DDR_MODE_CONTROL,
3449249e13SPoonam Aggrwal .ddr_sdram_interval = CONFIG_SYS_DDR_INTERVAL_800,
3549249e13SPoonam Aggrwal .ddr_data_init = CONFIG_MEM_INIT_VALUE,
3649249e13SPoonam Aggrwal .ddr_sdram_clk_cntl = CONFIG_SYS_DDR_CLK_CTRL_800,
3749249e13SPoonam Aggrwal .ddr_init_addr = CONFIG_SYS_DDR_INIT_ADDR,
3849249e13SPoonam Aggrwal .ddr_init_ext_addr = CONFIG_SYS_DDR_INIT_EXT_ADDR,
3949249e13SPoonam Aggrwal .timing_cfg_4 = CONFIG_SYS_DDR_TIMING_4,
4049249e13SPoonam Aggrwal .timing_cfg_5 = CONFIG_SYS_DDR_TIMING_5,
4149249e13SPoonam Aggrwal .ddr_zq_cntl = CONFIG_SYS_DDR_ZQ_CONTROL,
4249249e13SPoonam Aggrwal .ddr_wrlvl_cntl = CONFIG_SYS_DDR_WRLVL_CONTROL_800,
4349249e13SPoonam Aggrwal .ddr_sr_cntr = CONFIG_SYS_DDR_SR_CNTR,
4449249e13SPoonam Aggrwal .ddr_sdram_rcw_1 = CONFIG_SYS_DDR_RCW_1,
4549249e13SPoonam Aggrwal .ddr_sdram_rcw_2 = CONFIG_SYS_DDR_RCW_2
4649249e13SPoonam Aggrwal };
4749249e13SPoonam Aggrwal
4849249e13SPoonam Aggrwal fsl_ddr_cfg_regs_t ddr_cfg_regs_667 = {
4949249e13SPoonam Aggrwal .cs[0].bnds = CONFIG_SYS_DDR_CS0_BNDS,
5049249e13SPoonam Aggrwal .cs[0].config = CONFIG_SYS_DDR_CS0_CONFIG,
5149249e13SPoonam Aggrwal .cs[0].config_2 = CONFIG_SYS_DDR_CS0_CONFIG_2,
5249249e13SPoonam Aggrwal .timing_cfg_3 = CONFIG_SYS_DDR_TIMING_3_667,
5349249e13SPoonam Aggrwal .timing_cfg_0 = CONFIG_SYS_DDR_TIMING_0_667,
5449249e13SPoonam Aggrwal .timing_cfg_1 = CONFIG_SYS_DDR_TIMING_1_667,
5549249e13SPoonam Aggrwal .timing_cfg_2 = CONFIG_SYS_DDR_TIMING_2_667,
5649249e13SPoonam Aggrwal .ddr_sdram_cfg = CONFIG_SYS_DDR_CONTROL,
5749249e13SPoonam Aggrwal .ddr_sdram_cfg_2 = CONFIG_SYS_DDR_CONTROL_2,
5849249e13SPoonam Aggrwal .ddr_sdram_mode = CONFIG_SYS_DDR_MODE_1_667,
5949249e13SPoonam Aggrwal .ddr_sdram_mode_2 = CONFIG_SYS_DDR_MODE_2_667,
6049249e13SPoonam Aggrwal .ddr_sdram_md_cntl = CONFIG_SYS_DDR_MODE_CONTROL,
6149249e13SPoonam Aggrwal .ddr_sdram_interval = CONFIG_SYS_DDR_INTERVAL_667,
6249249e13SPoonam Aggrwal .ddr_data_init = CONFIG_MEM_INIT_VALUE,
6349249e13SPoonam Aggrwal .ddr_sdram_clk_cntl = CONFIG_SYS_DDR_CLK_CTRL_667,
6449249e13SPoonam Aggrwal .ddr_init_addr = CONFIG_SYS_DDR_INIT_ADDR,
6549249e13SPoonam Aggrwal .ddr_init_ext_addr = CONFIG_SYS_DDR_INIT_EXT_ADDR,
6649249e13SPoonam Aggrwal .timing_cfg_4 = CONFIG_SYS_DDR_TIMING_4,
6749249e13SPoonam Aggrwal .timing_cfg_5 = CONFIG_SYS_DDR_TIMING_5,
6849249e13SPoonam Aggrwal .ddr_zq_cntl = CONFIG_SYS_DDR_ZQ_CONTROL,
6949249e13SPoonam Aggrwal .ddr_wrlvl_cntl = CONFIG_SYS_DDR_WRLVL_CONTROL_667,
7049249e13SPoonam Aggrwal .ddr_sr_cntr = CONFIG_SYS_DDR_SR_CNTR,
7149249e13SPoonam Aggrwal .ddr_sdram_rcw_1 = CONFIG_SYS_DDR_RCW_1,
7249249e13SPoonam Aggrwal .ddr_sdram_rcw_2 = CONFIG_SYS_DDR_RCW_2
7349249e13SPoonam Aggrwal };
7449249e13SPoonam Aggrwal
7549249e13SPoonam Aggrwal fixed_ddr_parm_t fixed_ddr_parm_0[] = {
7649249e13SPoonam Aggrwal {750, 850, &ddr_cfg_regs_800},
7749249e13SPoonam Aggrwal {607, 749, &ddr_cfg_regs_667},
7849249e13SPoonam Aggrwal {0, 0, NULL}
7949249e13SPoonam Aggrwal };
8049249e13SPoonam Aggrwal
get_sdram_size(void)8149249e13SPoonam Aggrwal unsigned long get_sdram_size(void)
8249249e13SPoonam Aggrwal {
8349249e13SPoonam Aggrwal struct cpu_type *cpu;
8449249e13SPoonam Aggrwal phys_size_t ddr_size;
8549249e13SPoonam Aggrwal
8667ac13b1SSimon Glass cpu = gd->arch.cpu;
8749249e13SPoonam Aggrwal /* P1014 and it's derivatives support max 16it DDR width */
8848f6a5c3SYork Sun if (cpu->soc_ver == SVR_P1014)
8949249e13SPoonam Aggrwal ddr_size = (CONFIG_SYS_DRAM_SIZE / 2);
9049249e13SPoonam Aggrwal else
9149249e13SPoonam Aggrwal ddr_size = CONFIG_SYS_DRAM_SIZE;
9249249e13SPoonam Aggrwal
9349249e13SPoonam Aggrwal return ddr_size;
9449249e13SPoonam Aggrwal }
9549249e13SPoonam Aggrwal
9649249e13SPoonam Aggrwal /*
9749249e13SPoonam Aggrwal * Fixed sdram init -- doesn't use serial presence detect.
9849249e13SPoonam Aggrwal */
fixed_sdram(void)9949249e13SPoonam Aggrwal phys_size_t fixed_sdram(void)
10049249e13SPoonam Aggrwal {
10149249e13SPoonam Aggrwal int i;
10249249e13SPoonam Aggrwal char buf[32];
10349249e13SPoonam Aggrwal fsl_ddr_cfg_regs_t ddr_cfg_regs;
10449249e13SPoonam Aggrwal phys_size_t ddr_size;
10549249e13SPoonam Aggrwal ulong ddr_freq, ddr_freq_mhz;
10649249e13SPoonam Aggrwal struct cpu_type *cpu;
10749249e13SPoonam Aggrwal
10849249e13SPoonam Aggrwal #if defined(CONFIG_SYS_RAMBOOT)
10949249e13SPoonam Aggrwal return CONFIG_SYS_SDRAM_SIZE * 1024 * 1024;
11049249e13SPoonam Aggrwal #endif
11149249e13SPoonam Aggrwal
11249249e13SPoonam Aggrwal ddr_freq = get_ddr_freq(0);
11349249e13SPoonam Aggrwal ddr_freq_mhz = ddr_freq / 1000000;
11449249e13SPoonam Aggrwal
11549249e13SPoonam Aggrwal printf("Configuring DDR for %s MT/s data rate\n",
11649249e13SPoonam Aggrwal strmhz(buf, ddr_freq));
11749249e13SPoonam Aggrwal
11849249e13SPoonam Aggrwal for (i = 0; fixed_ddr_parm_0[i].max_freq > 0; i++) {
11949249e13SPoonam Aggrwal if ((ddr_freq_mhz > fixed_ddr_parm_0[i].min_freq) &&
12049249e13SPoonam Aggrwal (ddr_freq_mhz <= fixed_ddr_parm_0[i].max_freq)) {
12149249e13SPoonam Aggrwal memcpy(&ddr_cfg_regs, fixed_ddr_parm_0[i].ddr_settings,
12249249e13SPoonam Aggrwal sizeof(ddr_cfg_regs));
12349249e13SPoonam Aggrwal break;
12449249e13SPoonam Aggrwal }
12549249e13SPoonam Aggrwal }
12649249e13SPoonam Aggrwal
12749249e13SPoonam Aggrwal if (fixed_ddr_parm_0[i].max_freq == 0)
12849249e13SPoonam Aggrwal panic("Unsupported DDR data rate %s MT/s data rate\n",
12949249e13SPoonam Aggrwal strmhz(buf, ddr_freq));
13049249e13SPoonam Aggrwal
13167ac13b1SSimon Glass cpu = gd->arch.cpu;
13249249e13SPoonam Aggrwal /* P1014 and it's derivatives support max 16bit DDR width */
13348f6a5c3SYork Sun if (cpu->soc_ver == SVR_P1014) {
134c8f9802aSMatthew McClintock ddr_cfg_regs.ddr_sdram_cfg &= ~SDRAM_CFG_DBW_MASK;
13549249e13SPoonam Aggrwal ddr_cfg_regs.ddr_sdram_cfg |= SDRAM_CFG_16_BE;
136c8f9802aSMatthew McClintock /* divide SA and EA by two and then mask the rest so we don't
137c8f9802aSMatthew McClintock * write to reserved fields */
138c8f9802aSMatthew McClintock ddr_cfg_regs.cs[0].bnds = (CONFIG_SYS_DDR_CS0_BNDS >> 1) & 0x0fff0fff;
13949249e13SPoonam Aggrwal }
14049249e13SPoonam Aggrwal
14149249e13SPoonam Aggrwal ddr_size = (phys_size_t) CONFIG_SYS_SDRAM_SIZE * 1024 * 1024;
142c63e1370SYork Sun fsl_ddr_set_memctl_regs(&ddr_cfg_regs, 0, 0);
14349249e13SPoonam Aggrwal
14449249e13SPoonam Aggrwal if (set_ddr_laws(CONFIG_SYS_DDR_SDRAM_BASE, ddr_size,
14549249e13SPoonam Aggrwal LAW_TRGT_IF_DDR_1) < 0) {
14649249e13SPoonam Aggrwal printf("ERROR setting Local Access Windows for DDR\n");
14749249e13SPoonam Aggrwal return 0;
14849249e13SPoonam Aggrwal }
14949249e13SPoonam Aggrwal
15049249e13SPoonam Aggrwal return ddr_size;
15149249e13SPoonam Aggrwal }
15249249e13SPoonam Aggrwal
1531ba62f10SYork Sun #else /* CONFIG_SYS_DDR_RAW_TIMING */
15449249e13SPoonam Aggrwal /*
15549249e13SPoonam Aggrwal * Samsung K4B2G0846C-HCF8
15649249e13SPoonam Aggrwal * The following timing are for "downshift"
15749249e13SPoonam Aggrwal * i.e. to use CL9 part as CL7
15849249e13SPoonam Aggrwal * otherwise, tAA, tRCD, tRP will be 13500ps
15949249e13SPoonam Aggrwal * and tRC will be 49500ps
16049249e13SPoonam Aggrwal */
16149249e13SPoonam Aggrwal dimm_params_t ddr_raw_timing = {
16249249e13SPoonam Aggrwal .n_ranks = 1,
16349249e13SPoonam Aggrwal .rank_density = 1073741824u,
16449249e13SPoonam Aggrwal .capacity = 1073741824u,
16549249e13SPoonam Aggrwal .primary_sdram_width = 32,
16649249e13SPoonam Aggrwal .ec_sdram_width = 0,
16749249e13SPoonam Aggrwal .registered_dimm = 0,
16849249e13SPoonam Aggrwal .mirrored_dimm = 0,
16949249e13SPoonam Aggrwal .n_row_addr = 15,
17049249e13SPoonam Aggrwal .n_col_addr = 10,
17149249e13SPoonam Aggrwal .n_banks_per_sdram_device = 8,
17249249e13SPoonam Aggrwal .edc_config = 0,
17349249e13SPoonam Aggrwal .burst_lengths_bitmask = 0x0c,
17449249e13SPoonam Aggrwal
1750dd38a35SPriyanka Jain .tckmin_x_ps = 1875,
1760dd38a35SPriyanka Jain .caslat_x = 0x1e << 4, /* 5,6,7,8 */
1770dd38a35SPriyanka Jain .taa_ps = 13125,
1780dd38a35SPriyanka Jain .twr_ps = 15000,
1790dd38a35SPriyanka Jain .trcd_ps = 13125,
1800dd38a35SPriyanka Jain .trrd_ps = 7500,
1810dd38a35SPriyanka Jain .trp_ps = 13125,
1820dd38a35SPriyanka Jain .tras_ps = 37500,
1830dd38a35SPriyanka Jain .trc_ps = 50625,
1840dd38a35SPriyanka Jain .trfc_ps = 160000,
1850dd38a35SPriyanka Jain .twtr_ps = 7500,
1860dd38a35SPriyanka Jain .trtp_ps = 7500,
18749249e13SPoonam Aggrwal .refresh_rate_ps = 7800000,
1880dd38a35SPriyanka Jain .tfaw_ps = 37500,
18949249e13SPoonam Aggrwal };
19049249e13SPoonam Aggrwal
fsl_ddr_get_dimm_params(dimm_params_t * pdimm,unsigned int controller_number,unsigned int dimm_number)19149249e13SPoonam Aggrwal int fsl_ddr_get_dimm_params(dimm_params_t *pdimm,
19249249e13SPoonam Aggrwal unsigned int controller_number,
19349249e13SPoonam Aggrwal unsigned int dimm_number)
19449249e13SPoonam Aggrwal {
19549249e13SPoonam Aggrwal const char dimm_model[] = "Fixed DDR on board";
19649249e13SPoonam Aggrwal
19749249e13SPoonam Aggrwal if ((controller_number == 0) && (dimm_number == 0)) {
19849249e13SPoonam Aggrwal memcpy(pdimm, &ddr_raw_timing, sizeof(dimm_params_t));
19949249e13SPoonam Aggrwal memset(pdimm->mpart, 0, sizeof(pdimm->mpart));
20049249e13SPoonam Aggrwal memcpy(pdimm->mpart, dimm_model, sizeof(dimm_model) - 1);
20149249e13SPoonam Aggrwal }
20249249e13SPoonam Aggrwal
20349249e13SPoonam Aggrwal return 0;
20449249e13SPoonam Aggrwal }
20549249e13SPoonam Aggrwal
fsl_ddr_board_options(memctl_options_t * popts,dimm_params_t * pdimm,unsigned int ctrl_num)20649249e13SPoonam Aggrwal void fsl_ddr_board_options(memctl_options_t *popts,
20749249e13SPoonam Aggrwal dimm_params_t *pdimm,
20849249e13SPoonam Aggrwal unsigned int ctrl_num)
20949249e13SPoonam Aggrwal {
21049249e13SPoonam Aggrwal struct cpu_type *cpu;
21149249e13SPoonam Aggrwal int i;
21249249e13SPoonam Aggrwal popts->clk_adjust = 6;
21349249e13SPoonam Aggrwal popts->cpo_override = 0x1f;
21449249e13SPoonam Aggrwal popts->write_data_delay = 2;
21549249e13SPoonam Aggrwal popts->half_strength_driver_enable = 1;
21649249e13SPoonam Aggrwal /* Write leveling override */
21749249e13SPoonam Aggrwal popts->wrlvl_en = 1;
21849249e13SPoonam Aggrwal popts->wrlvl_override = 1;
21949249e13SPoonam Aggrwal popts->wrlvl_sample = 0xf;
22049249e13SPoonam Aggrwal popts->wrlvl_start = 0x8;
22149249e13SPoonam Aggrwal popts->trwt_override = 1;
22249249e13SPoonam Aggrwal popts->trwt = 0;
22349249e13SPoonam Aggrwal
22467ac13b1SSimon Glass cpu = gd->arch.cpu;
22549249e13SPoonam Aggrwal /* P1014 and it's derivatives support max 16it DDR width */
22648f6a5c3SYork Sun if (cpu->soc_ver == SVR_P1014)
22749249e13SPoonam Aggrwal popts->data_bus_width = DDR_DATA_BUS_WIDTH_16;
22849249e13SPoonam Aggrwal
22949249e13SPoonam Aggrwal for (i = 0; i < CONFIG_CHIP_SELECTS_PER_CTRL; i++) {
23049249e13SPoonam Aggrwal popts->cs_local_opts[i].odt_rd_cfg = FSL_DDR_ODT_NEVER;
23149249e13SPoonam Aggrwal popts->cs_local_opts[i].odt_wr_cfg = FSL_DDR_ODT_CS;
23249249e13SPoonam Aggrwal }
23349249e13SPoonam Aggrwal }
23449249e13SPoonam Aggrwal
2351ba62f10SYork Sun #endif /* CONFIG_SYS_DDR_RAW_TIMING */
236