2 * Copyright 2008-2012 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.
10 #include <asm/fsl_law.h>
14 #include <fsl_immap.h>
17 /* To avoid 64-bit full-divides, we factor this here */
18 #define ULL_2E12 2000000000000ULL
19 #define UL_5POW12 244140625UL
20 #define UL_2POW13 (1UL << 13)
22 #define ULL_8FS 0xFFFFFFFFULL
25 * Round up mclk_ps to nearest 1 ps in memory controller code
26 * if the error is 0.5ps or more.
28 * If an imprecise data rate is too high due to rounding error
29 * propagation, compute a suitably rounded mclk_ps to compute
30 * a working memory controller configuration.
32 unsigned int get_memory_clk_period_ps(void)
34 unsigned int data_rate = get_ddr_freq(0);
37 /* Round to nearest 10ps, being careful about 64-bit multiply/divide */
38 unsigned long long rem, mclk_ps = ULL_2E12;
40 /* Now perform the big divide, the result fits in 32-bits */
41 rem = do_div(mclk_ps, data_rate);
42 result = (rem >= (data_rate >> 1)) ? mclk_ps + 1 : mclk_ps;
47 /* Convert picoseconds into DRAM clock cycles (rounding up if needed). */
48 unsigned int picos_to_mclk(unsigned int picos)
50 unsigned long long clks, clks_rem;
51 unsigned long data_rate = get_ddr_freq(0);
53 /* Short circuit for zero picos */
57 /* First multiply the time by the data rate (32x32 => 64) */
58 clks = picos * (unsigned long long)data_rate;
60 * Now divide by 5^12 and track the 32-bit remainder, then divide
61 * by 2*(2^12) using shifts (and updating the remainder).
63 clks_rem = do_div(clks, UL_5POW12);
64 clks_rem += (clks & (UL_2POW13-1)) * UL_5POW12;
67 /* If we had a remainder greater than the 1ps error, then round up */
68 if (clks_rem > data_rate)
71 /* Clamp to the maximum representable value */
74 return (unsigned int) clks;
77 unsigned int mclk_to_picos(unsigned int mclk)
79 return get_memory_clk_period_ps() * mclk;
83 __fsl_ddr_set_lawbar(const common_timing_params_t *memctl_common_params,
84 unsigned int law_memctl,
85 unsigned int ctrl_num)
87 unsigned long long base = memctl_common_params->base_address;
88 unsigned long long size = memctl_common_params->total_mem;
91 * If no DIMMs on this controller, do not proceed any further.
93 if (!memctl_common_params->ndimms_present) {
97 #if !defined(CONFIG_PHYS_64BIT)
98 if (base >= CONFIG_MAX_MEM_MAPPED)
100 if ((base + size) >= CONFIG_MAX_MEM_MAPPED)
101 size = CONFIG_MAX_MEM_MAPPED - base;
103 if (set_ddr_laws(base, size, law_memctl) < 0) {
104 printf("%s: ERROR (ctrl #%d, TRGT ID=%x)\n", __func__, ctrl_num,
108 debug("setup ddr law base = 0x%llx, size 0x%llx, TRGT_ID 0x%x\n",
109 base, size, law_memctl);
112 __attribute__((weak, alias("__fsl_ddr_set_lawbar"))) void
113 fsl_ddr_set_lawbar(const common_timing_params_t *memctl_common_params,
114 unsigned int memctl_interleaved,
115 unsigned int ctrl_num);
117 void fsl_ddr_set_intl3r(const unsigned int granule_size)
120 u32 *mcintl3r = (void *) (CONFIG_SYS_IMMR + 0x18004);
121 *mcintl3r = 0x80000000 | (granule_size & 0x1f);
122 debug("Enable MCINTL3R with granule size 0x%x\n", granule_size);
126 u32 fsl_ddr_get_intl3r(void)
130 u32 *mcintl3r = (void *) (CONFIG_SYS_IMMR + 0x18004);
136 void board_add_ram_info(int use_default)
138 struct ccsr_ddr __iomem *ddr =
139 (struct ccsr_ddr __iomem *)(CONFIG_SYS_FSL_DDR_ADDR);
141 #if defined(CONFIG_E6500) && (CONFIG_NUM_DDR_CONTROLLERS == 3)
142 u32 *mcintl3r = (void *) (CONFIG_SYS_IMMR + 0x18004);
144 #if (CONFIG_NUM_DDR_CONTROLLERS > 1)
145 uint32_t cs0_config = in_be32(&ddr->cs0_config);
147 uint32_t sdram_cfg = in_be32(&ddr->sdram_cfg);
150 #if CONFIG_NUM_DDR_CONTROLLERS >= 2
151 if (!(sdram_cfg & SDRAM_CFG_MEM_EN)) {
152 ddr = (void __iomem *)CONFIG_SYS_FSL_DDR2_ADDR;
153 sdram_cfg = in_be32(&ddr->sdram_cfg);
156 #if CONFIG_NUM_DDR_CONTROLLERS >= 3
157 if (!(sdram_cfg & SDRAM_CFG_MEM_EN)) {
158 ddr = (void __iomem *)CONFIG_SYS_FSL_DDR3_ADDR;
159 sdram_cfg = in_be32(&ddr->sdram_cfg);
163 switch ((sdram_cfg & SDRAM_CFG_SDRAM_TYPE_MASK) >>
164 SDRAM_CFG_SDRAM_TYPE_SHIFT) {
165 case SDRAM_TYPE_DDR1:
168 case SDRAM_TYPE_DDR2:
171 case SDRAM_TYPE_DDR3:
179 if (sdram_cfg & SDRAM_CFG_32_BE)
181 else if (sdram_cfg & SDRAM_CFG_16_BE)
186 /* Calculate CAS latency based on timing cfg values */
187 cas_lat = ((in_be32(&ddr->timing_cfg_1) >> 16) & 0xf) + 1;
188 if ((in_be32(&ddr->timing_cfg_3) >> 12) & 1)
190 printf(", CL=%d", cas_lat >> 1);
194 if (sdram_cfg & SDRAM_CFG_ECC_EN)
199 #if (CONFIG_NUM_DDR_CONTROLLERS == 3)
201 if (*mcintl3r & 0x80000000) {
203 puts(" DDR Controller Interleaving Mode: ");
204 switch (*mcintl3r & 0x1f) {
205 case FSL_DDR_3WAY_1KB_INTERLEAVING:
208 case FSL_DDR_3WAY_4KB_INTERLEAVING:
211 case FSL_DDR_3WAY_8KB_INTERLEAVING:
215 puts("3-way UNKNOWN");
221 #if (CONFIG_NUM_DDR_CONTROLLERS >= 2)
222 if (cs0_config & 0x20000000) {
224 puts(" DDR Controller Interleaving Mode: ");
226 switch ((cs0_config >> 24) & 0xf) {
227 case FSL_DDR_CACHE_LINE_INTERLEAVING:
230 case FSL_DDR_PAGE_INTERLEAVING:
233 case FSL_DDR_BANK_INTERLEAVING:
236 case FSL_DDR_SUPERBANK_INTERLEAVING:
246 if ((sdram_cfg >> 8) & 0x7f) {
248 puts(" DDR Chip-Select Interleaving Mode: ");
249 switch(sdram_cfg >> 8 & 0x7f) {
250 case FSL_DDR_CS0_CS1_CS2_CS3:
251 puts("CS0+CS1+CS2+CS3");
253 case FSL_DDR_CS0_CS1:
256 case FSL_DDR_CS2_CS3:
259 case FSL_DDR_CS0_CS1_AND_CS2_CS3:
260 puts("CS0+CS1 and CS2+CS3");