blob: 0658261d8dac86321416821d0d0fd034a316d528 [file] [log] [blame]
Kumar Gala58e5e9a2008-08-26 15:01:29 -05001/*
York Suna4c66502012-08-17 08:22:39 +00002 * Copyright 2008-2012 Freescale Semiconductor, Inc.
Kumar Gala58e5e9a2008-08-26 15:01:29 -05003 *
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.
7 */
8
9#include <common.h>
York Sun9ac4ffb2013-09-30 14:20:51 -070010#ifdef CONFIG_PPC
Kumar Gala58e5e9a2008-08-26 15:01:29 -050011#include <asm/fsl_law.h>
York Sun9ac4ffb2013-09-30 14:20:51 -070012#endif
Kyle Moffette820a132011-03-15 11:23:47 -040013#include <div64.h>
Kumar Gala58e5e9a2008-08-26 15:01:29 -050014
York Sun5614e712013-09-30 09:22:09 -070015#include <fsl_ddr.h>
York Sun9a17eb52013-11-18 10:29:32 -080016#include <fsl_immap.h>
York Sun5614e712013-09-30 09:22:09 -070017#include <asm/io.h>
Kumar Gala58e5e9a2008-08-26 15:01:29 -050018
Kyle Moffette820a132011-03-15 11:23:47 -040019/* To avoid 64-bit full-divides, we factor this here */
Kyle Moffetta2879632011-04-14 13:39:30 -040020#define ULL_2E12 2000000000000ULL
21#define UL_5POW12 244140625UL
22#define UL_2POW13 (1UL << 13)
Kyle Moffette820a132011-03-15 11:23:47 -040023
Kyle Moffetta2879632011-04-14 13:39:30 -040024#define ULL_8FS 0xFFFFFFFFULL
Kyle Moffette820a132011-03-15 11:23:47 -040025
Kumar Gala58e5e9a2008-08-26 15:01:29 -050026/*
York Sun905acde2011-08-26 11:32:42 -070027 * Round up mclk_ps to nearest 1 ps in memory controller code
28 * if the error is 0.5ps or more.
Kumar Gala58e5e9a2008-08-26 15:01:29 -050029 *
30 * If an imprecise data rate is too high due to rounding error
31 * propagation, compute a suitably rounded mclk_ps to compute
32 * a working memory controller configuration.
33 */
34unsigned int get_memory_clk_period_ps(void)
35{
Kyle Moffette820a132011-03-15 11:23:47 -040036 unsigned int data_rate = get_ddr_freq(0);
37 unsigned int result;
Kumar Gala58e5e9a2008-08-26 15:01:29 -050038
Kyle Moffette820a132011-03-15 11:23:47 -040039 /* Round to nearest 10ps, being careful about 64-bit multiply/divide */
York Sun905acde2011-08-26 11:32:42 -070040 unsigned long long rem, mclk_ps = ULL_2E12;
Kyle Moffette820a132011-03-15 11:23:47 -040041
42 /* Now perform the big divide, the result fits in 32-bits */
York Sun905acde2011-08-26 11:32:42 -070043 rem = do_div(mclk_ps, data_rate);
44 result = (rem >= (data_rate >> 1)) ? mclk_ps + 1 : mclk_ps;
Kyle Moffette820a132011-03-15 11:23:47 -040045
York Sun905acde2011-08-26 11:32:42 -070046 return result;
Kumar Gala58e5e9a2008-08-26 15:01:29 -050047}
48
49/* Convert picoseconds into DRAM clock cycles (rounding up if needed). */
50unsigned int picos_to_mclk(unsigned int picos)
51{
Kyle Moffette820a132011-03-15 11:23:47 -040052 unsigned long long clks, clks_rem;
York Sun905acde2011-08-26 11:32:42 -070053 unsigned long data_rate = get_ddr_freq(0);
Kumar Gala58e5e9a2008-08-26 15:01:29 -050054
Kyle Moffette820a132011-03-15 11:23:47 -040055 /* Short circuit for zero picos */
Kumar Gala58e5e9a2008-08-26 15:01:29 -050056 if (!picos)
57 return 0;
58
Kyle Moffette820a132011-03-15 11:23:47 -040059 /* First multiply the time by the data rate (32x32 => 64) */
York Sun905acde2011-08-26 11:32:42 -070060 clks = picos * (unsigned long long)data_rate;
Kyle Moffette820a132011-03-15 11:23:47 -040061 /*
62 * Now divide by 5^12 and track the 32-bit remainder, then divide
63 * by 2*(2^12) using shifts (and updating the remainder).
64 */
Kyle Moffetta2879632011-04-14 13:39:30 -040065 clks_rem = do_div(clks, UL_5POW12);
York Sun905acde2011-08-26 11:32:42 -070066 clks_rem += (clks & (UL_2POW13-1)) * UL_5POW12;
Kyle Moffette820a132011-03-15 11:23:47 -040067 clks >>= 13;
68
York Sun905acde2011-08-26 11:32:42 -070069 /* If we had a remainder greater than the 1ps error, then round up */
70 if (clks_rem > data_rate)
Kumar Gala58e5e9a2008-08-26 15:01:29 -050071 clks++;
Kumar Gala58e5e9a2008-08-26 15:01:29 -050072
Kyle Moffette820a132011-03-15 11:23:47 -040073 /* Clamp to the maximum representable value */
Kyle Moffetta2879632011-04-14 13:39:30 -040074 if (clks > ULL_8FS)
75 clks = ULL_8FS;
Kumar Gala58e5e9a2008-08-26 15:01:29 -050076 return (unsigned int) clks;
77}
78
79unsigned int mclk_to_picos(unsigned int mclk)
80{
81 return get_memory_clk_period_ps() * mclk;
82}
83
York Sun9ac4ffb2013-09-30 14:20:51 -070084#ifdef CONFIG_PPC
Kumar Gala58e5e9a2008-08-26 15:01:29 -050085void
86__fsl_ddr_set_lawbar(const common_timing_params_t *memctl_common_params,
York Suna4c66502012-08-17 08:22:39 +000087 unsigned int law_memctl,
Kumar Gala58e5e9a2008-08-26 15:01:29 -050088 unsigned int ctrl_num)
89{
Kumar Galae7563af2009-06-11 23:42:35 -050090 unsigned long long base = memctl_common_params->base_address;
91 unsigned long long size = memctl_common_params->total_mem;
92
Kumar Gala58e5e9a2008-08-26 15:01:29 -050093 /*
94 * If no DIMMs on this controller, do not proceed any further.
95 */
96 if (!memctl_common_params->ndimms_present) {
97 return;
98 }
99
Kumar Galae7563af2009-06-11 23:42:35 -0500100#if !defined(CONFIG_PHYS_64BIT)
101 if (base >= CONFIG_MAX_MEM_MAPPED)
102 return;
103 if ((base + size) >= CONFIG_MAX_MEM_MAPPED)
104 size = CONFIG_MAX_MEM_MAPPED - base;
105#endif
York Suna4c66502012-08-17 08:22:39 +0000106 if (set_ddr_laws(base, size, law_memctl) < 0) {
107 printf("%s: ERROR (ctrl #%d, TRGT ID=%x)\n", __func__, ctrl_num,
108 law_memctl);
109 return ;
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500110 }
York Suna4c66502012-08-17 08:22:39 +0000111 debug("setup ddr law base = 0x%llx, size 0x%llx, TRGT_ID 0x%x\n",
112 base, size, law_memctl);
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500113}
114
115__attribute__((weak, alias("__fsl_ddr_set_lawbar"))) void
116fsl_ddr_set_lawbar(const common_timing_params_t *memctl_common_params,
117 unsigned int memctl_interleaved,
118 unsigned int ctrl_num);
York Sun9ac4ffb2013-09-30 14:20:51 -0700119#endif
Peter Tyserd9c147f2009-07-17 10:14:48 -0500120
York Suna4c66502012-08-17 08:22:39 +0000121void fsl_ddr_set_intl3r(const unsigned int granule_size)
122{
123#ifdef CONFIG_E6500
124 u32 *mcintl3r = (void *) (CONFIG_SYS_IMMR + 0x18004);
125 *mcintl3r = 0x80000000 | (granule_size & 0x1f);
126 debug("Enable MCINTL3R with granule size 0x%x\n", granule_size);
127#endif
128}
129
York Suneb539412012-10-08 07:44:25 +0000130u32 fsl_ddr_get_intl3r(void)
131{
132 u32 val = 0;
133#ifdef CONFIG_E6500
134 u32 *mcintl3r = (void *) (CONFIG_SYS_IMMR + 0x18004);
135 val = *mcintl3r;
136#endif
137 return val;
138}
139
Peter Tyserd9c147f2009-07-17 10:14:48 -0500140void board_add_ram_info(int use_default)
141{
York Sun9a17eb52013-11-18 10:29:32 -0800142 struct ccsr_ddr __iomem *ddr =
143 (struct ccsr_ddr __iomem *)(CONFIG_SYS_FSL_DDR_ADDR);
Andy Fleminge76cd5d2012-10-23 19:03:46 -0500144
York Suna4c66502012-08-17 08:22:39 +0000145#if defined(CONFIG_E6500) && (CONFIG_NUM_DDR_CONTROLLERS == 3)
146 u32 *mcintl3r = (void *) (CONFIG_SYS_IMMR + 0x18004);
147#endif
Peter Tyserd9c147f2009-07-17 10:14:48 -0500148#if (CONFIG_NUM_DDR_CONTROLLERS > 1)
149 uint32_t cs0_config = in_be32(&ddr->cs0_config);
150#endif
151 uint32_t sdram_cfg = in_be32(&ddr->sdram_cfg);
152 int cas_lat;
153
York Sun123922b2012-10-08 07:44:23 +0000154#if CONFIG_NUM_DDR_CONTROLLERS >= 2
155 if (!(sdram_cfg & SDRAM_CFG_MEM_EN)) {
York Sun5614e712013-09-30 09:22:09 -0700156 ddr = (void __iomem *)CONFIG_SYS_FSL_DDR2_ADDR;
York Sun123922b2012-10-08 07:44:23 +0000157 sdram_cfg = in_be32(&ddr->sdram_cfg);
158 }
159#endif
160#if CONFIG_NUM_DDR_CONTROLLERS >= 3
161 if (!(sdram_cfg & SDRAM_CFG_MEM_EN)) {
York Sun5614e712013-09-30 09:22:09 -0700162 ddr = (void __iomem *)CONFIG_SYS_FSL_DDR3_ADDR;
York Sun123922b2012-10-08 07:44:23 +0000163 sdram_cfg = in_be32(&ddr->sdram_cfg);
164 }
165#endif
Peter Tyserd9c147f2009-07-17 10:14:48 -0500166 puts(" (DDR");
167 switch ((sdram_cfg & SDRAM_CFG_SDRAM_TYPE_MASK) >>
168 SDRAM_CFG_SDRAM_TYPE_SHIFT) {
169 case SDRAM_TYPE_DDR1:
170 puts("1");
171 break;
172 case SDRAM_TYPE_DDR2:
173 puts("2");
174 break;
175 case SDRAM_TYPE_DDR3:
176 puts("3");
177 break;
178 default:
179 puts("?");
180 break;
181 }
182
183 if (sdram_cfg & SDRAM_CFG_32_BE)
184 puts(", 32-bit");
Poonam Aggrwal0b3b1762011-02-07 15:09:51 +0530185 else if (sdram_cfg & SDRAM_CFG_16_BE)
186 puts(", 16-bit");
Peter Tyserd9c147f2009-07-17 10:14:48 -0500187 else
188 puts(", 64-bit");
189
190 /* Calculate CAS latency based on timing cfg values */
191 cas_lat = ((in_be32(&ddr->timing_cfg_1) >> 16) & 0xf) + 1;
192 if ((in_be32(&ddr->timing_cfg_3) >> 12) & 1)
193 cas_lat += (8 << 1);
194 printf(", CL=%d", cas_lat >> 1);
195 if (cas_lat & 0x1)
196 puts(".5");
197
198 if (sdram_cfg & SDRAM_CFG_ECC_EN)
199 puts(", ECC on)");
200 else
201 puts(", ECC off)");
202
York Suna4c66502012-08-17 08:22:39 +0000203#if (CONFIG_NUM_DDR_CONTROLLERS == 3)
204#ifdef CONFIG_E6500
205 if (*mcintl3r & 0x80000000) {
206 puts("\n");
207 puts(" DDR Controller Interleaving Mode: ");
208 switch (*mcintl3r & 0x1f) {
209 case FSL_DDR_3WAY_1KB_INTERLEAVING:
210 puts("3-way 1KB");
211 break;
212 case FSL_DDR_3WAY_4KB_INTERLEAVING:
213 puts("3-way 4KB");
214 break;
215 case FSL_DDR_3WAY_8KB_INTERLEAVING:
216 puts("3-way 8KB");
217 break;
218 default:
219 puts("3-way UNKNOWN");
220 break;
221 }
222 }
223#endif
224#endif
225#if (CONFIG_NUM_DDR_CONTROLLERS >= 2)
Peter Tyserd9c147f2009-07-17 10:14:48 -0500226 if (cs0_config & 0x20000000) {
227 puts("\n");
228 puts(" DDR Controller Interleaving Mode: ");
229
230 switch ((cs0_config >> 24) & 0xf) {
231 case FSL_DDR_CACHE_LINE_INTERLEAVING:
232 puts("cache line");
233 break;
234 case FSL_DDR_PAGE_INTERLEAVING:
235 puts("page");
236 break;
237 case FSL_DDR_BANK_INTERLEAVING:
238 puts("bank");
239 break;
240 case FSL_DDR_SUPERBANK_INTERLEAVING:
241 puts("super-bank");
242 break;
243 default:
244 puts("invalid");
245 break;
246 }
247 }
248#endif
249
250 if ((sdram_cfg >> 8) & 0x7f) {
251 puts("\n");
252 puts(" DDR Chip-Select Interleaving Mode: ");
253 switch(sdram_cfg >> 8 & 0x7f) {
254 case FSL_DDR_CS0_CS1_CS2_CS3:
255 puts("CS0+CS1+CS2+CS3");
256 break;
257 case FSL_DDR_CS0_CS1:
258 puts("CS0+CS1");
259 break;
260 case FSL_DDR_CS2_CS3:
261 puts("CS2+CS3");
262 break;
263 case FSL_DDR_CS0_CS1_AND_CS2_CS3:
264 puts("CS0+CS1 and CS2+CS3");
265 break;
266 default:
267 puts("invalid");
268 break;
269 }
270 }
271}