2 * Copyright 2004 Freescale Semiconductor.
3 * (C) Copyright 2003 Motorola Inc.
4 * Xianghua Xiao (X.Xiao@motorola.com)
6 * See file CREDITS for list of people who contributed to this
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License as
11 * published by the Free Software Foundation; either version 2 of
12 * the License, or (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
26 * 20050101: Eran Liberty (liberty@freescale.com)
27 * Initial file creating (porting from 85XX & 8260)
31 #include <asm/processor.h>
35 #include <spd_sdram.h>
37 #ifdef CONFIG_SPD_EEPROM
40 #if defined(CONFIG_DDR_ECC)
41 extern void dma_init(void);
42 extern uint dma_check(void);
43 extern int dma_xfer(void *dest, uint count, void *src);
48 #define CFG_READ_SPD i2c_read
54 * Convert picoseconds into clock cycles (rounding up if needed).
58 picos_to_clk(int picos)
62 clks = picos / (2000000000 / (get_bus_freq(0) / 1000));
63 if (picos % (2000000000 / (get_bus_freq(0) / 1000)) != 0) {
72 banksize(unsigned char row_dens)
74 return ((row_dens >> 2) | ((row_dens & 3) << 6)) << 24;
78 long int spd_sdram(int(read_spd)(uint addr))
80 volatile immap_t *immap = (immap_t *)CFG_IMMRBAR;
81 volatile ddr8349_t *ddr = &immap->ddr;
82 volatile law8349_t *ecm = &immap->sysconf.ddrlaw[0];
86 unsigned int law_size;
88 unsigned int trfc, trfc_clk, trfc_low;
90 #warning Current spd_sdram does not fit its usage... adjust implementation or API...
92 CFG_READ_SPD(SPD_EEPROM_ADDRESS, 0, 1, (uchar *) & spd, sizeof (spd));
95 puts("DDR:Only two chip selects are supported on ADS.\n");
99 if (spd.nrow_addr < 12
100 || spd.nrow_addr > 14
102 || spd.ncol_addr > 11) {
103 puts("DDR:Row or Col number unsupported.\n");
107 ddr->csbnds[2].csbnds = (banksize(spd.row_dens) >> 24) - 1;
108 ddr->cs_config[2] = ( 1 << 31
109 | (spd.nrow_addr - 12) << 8
110 | (spd.ncol_addr - 8) );
112 debug("cs2_bnds = 0x%08x\n",ddr->csbnds[2].csbnds);
113 debug("cs2_config = 0x%08x\n",ddr->cs_config[2]);
115 if (spd.nrows == 2) {
116 ddr->csbnds[3].csbnds = ( (banksize(spd.row_dens) >> 8)
117 | ((banksize(spd.row_dens) >> 23) - 1) );
118 ddr->cs_config[3] = ( 1<<31
119 | (spd.nrow_addr-12) << 8
120 | (spd.ncol_addr-8) );
121 debug("cs3_bnds = 0x%08x\n",ddr->csbnds[3].csbnds);
122 debug("cs3_config = 0x%08x\n",ddr->cs_config[3]);
125 if (spd.mem_type != 0x07) {
126 puts("No DDR module found!\n");
131 * Figure out memory size in Megabytes.
133 memsize = spd.nrows * banksize(spd.row_dens) / 0x100000;
136 * First supported LAW size is 16M, at LAWAR_SIZE_16M == 23.
138 law_size = 19 + __ilog2(memsize);
141 * Set up LAWBAR for all of DDR.
143 ecm->bar = ((CFG_DDR_SDRAM_BASE>>12) & 0xfffff);
144 ecm->ar = (LAWAR_EN | LAWAR_TRGT_IF_DDR | (LAWAR_SIZE & law_size));
145 debug("DDR:bar=0x%08x\n", ecm->bar);
146 debug("DDR:ar=0x%08x\n", ecm->ar);
149 * find the largest CAS
151 if(spd.cas_lat & 0x40) {
153 } else if (spd.cas_lat & 0x20) {
155 } else if (spd.cas_lat & 0x10) {
157 } else if (spd.cas_lat & 0x08) {
159 } else if (spd.cas_lat & 0x04) {
161 } else if (spd.cas_lat & 0x02) {
163 } else if (spd.cas_lat & 0x01) {
166 puts("DDR:no valid CAS Latency information.\n");
170 tmp = 20000 / (((spd.clk_cycle & 0xF0) >> 4) * 10
171 + (spd.clk_cycle & 0x0f));
172 debug("DDR:Module maximum data rate is: %dMhz\n", tmp);
174 tmp1 = get_bus_freq(0) / 1000000;
175 if (tmp1 < 230 && tmp1 >= 90 && tmp >= 230) {
176 /* 90~230 range, treated as DDR 200 */
177 if (spd.clk_cycle3 == 0xa0)
179 else if(spd.clk_cycle2 == 0xa0)
181 } else if (tmp1 < 280 && tmp1 >= 230 && tmp >= 280) {
182 /* 230-280 range, treated as DDR 266 */
183 if (spd.clk_cycle3 == 0x75)
185 else if (spd.clk_cycle2 == 0x75)
187 } else if (tmp1 < 350 && tmp1 >= 280 && tmp >= 350) {
188 /* 280~350 range, treated as DDR 333 */
189 if (spd.clk_cycle3 == 0x60)
191 else if (spd.clk_cycle2 == 0x60)
193 } else if (tmp1 < 90 || tmp1 >= 350) {
194 /* DDR rate out-of-range */
195 puts("DDR:platform frequency is not fit for DDR rate\n");
200 * note: caslat must also be programmed into ddr->sdram_mode
203 * note: WRREC(Twr) and WRTORD(Twtr) are not in SPD,
204 * use conservative value here.
206 trfc = spd.trfc * 1000; /* up to ps */
207 trfc_clk = picos_to_clk(trfc);
208 trfc_low = (trfc_clk - 8) & 0xf;
211 (((picos_to_clk(spd.trp * 250) & 0x07) << 28 ) |
212 ((picos_to_clk(spd.tras * 1000) & 0x0f ) << 24 ) |
213 ((picos_to_clk(spd.trcd * 250) & 0x07) << 20 ) |
214 ((caslat & 0x07) << 16 ) |
217 ((picos_to_clk(spd.trrd * 250) & 0x07) << 4) | 1);
219 ddr->timing_cfg_2 = 0x00000800;
221 debug("DDR:timing_cfg_1=0x%08x\n", ddr->timing_cfg_1);
222 debug("DDR:timing_cfg_2=0x%08x\n", ddr->timing_cfg_2);
225 * Only DDR I is supported
226 * DDR I and II have different mode-register-set definition
229 /* burst length is always 4 */
232 ddr->sdram_mode = 0x52; /* 1.5 */
235 ddr->sdram_mode = 0x22; /* 2.0 */
238 ddr->sdram_mode = 0x62; /* 2.5 */
241 ddr->sdram_mode = 0x32; /* 3.0 */
244 puts("DDR:only CAS Latency 1.5, 2.0, 2.5, 3.0 is supported.\n");
247 debug("DDR:sdram_mode=0x%08x\n", ddr->sdram_mode);
249 switch(spd.refresh) {
252 tmp = picos_to_clk(15625000);
256 tmp = picos_to_clk(3900000);
260 tmp = picos_to_clk(7800000);
264 tmp = picos_to_clk(31300000);
268 tmp = picos_to_clk(62500000);
272 tmp = picos_to_clk(125000000);
280 * Set BSTOPRE to 0x100 for page mode
281 * If auto-charge is used, set BSTOPRE = 0
283 ddr->sdram_interval = ((tmp & 0x3fff) << 16) | 0x100;
284 debug("DDR:sdram_interval=0x%08x\n", ddr->sdram_interval);
287 * Is this an ECC DDR chip?
289 #if defined(CONFIG_DDR_ECC)
290 if (spd.config == 0x02) {
291 ddr->err_disable = 0x0000000d;
292 ddr->err_sbe = 0x00ff0000;
294 debug("DDR:err_disable=0x%08x\n", ddr->err_disable);
295 debug("DDR:err_sbe=0x%08x\n", ddr->err_sbe);
302 ddr->sdram_clk_cntl = 0x82000000;/*SS_EN=1, CLK_ADJST = 2-MCK/MCK_B, is lauched 1/2 of one SDRAM clock cycle after address/command*/
306 * Figure out the settings for the sdram_cfg register. Build up
307 * the entire register in 'tmp' before writing since the write into
308 * the register will actually enable the memory controller, and all
309 * settings must be done before enabling.
311 * sdram_cfg[0] = 1 (ddr sdram logic enable)
312 * sdram_cfg[1] = 1 (self-refresh-enable)
313 * sdram_cfg[6:7] = 2 (SDRAM type = DDR SDRAM)
318 * sdram_cfg[3] = RD_EN - registered DIMM enable
319 * A value of 0x26 indicates micron registered DIMMS (micron.com)
321 if (spd.mod_attr == 0x26) {
325 #if defined(CONFIG_DDR_ECC)
327 * If the user wanted ECC (enabled via sdram_cfg[2])
329 if (spd.config == 0x02) {
334 #if defined(CONFIG_DDR_2T_TIMING)
336 * Enable 2T timing by setting sdram_cfg[16].
338 tmp |= SDRAM_CFG_2T_EN;
341 ddr->sdram_cfg = tmp;
346 debug("DDR:sdram_cfg=0x%08x\n", ddr->sdram_cfg);
348 return memsize;/*in MBytes*/
351 #endif /* CONFIG_SPD_EEPROM */
354 #if defined(CONFIG_DDR_ECC)
356 * Initialize all of memory for ECC, then enable errors.
360 ddr_enable_ecc(unsigned int dram_size)
365 volatile immap_t *immap = (immap_t *)CFG_IMMRBAR;
366 volatile ccsr_ddr_t *ddr= &immap->im_ddr;
370 for (*p = 0; p < (uint *)(8 * 1024); p++) {
371 if (((unsigned int)p & 0x1f) == 0) {
372 ppcDcbz((unsigned long) p);
374 *p = (unsigned int)0xdeadbeef;
375 if (((unsigned int)p & 0x1c) == 0x1c) {
376 ppcDcbf((unsigned long) p);
381 dma_xfer((uint *)0x2000, 0x2000, (uint *)0);
383 dma_xfer((uint *)0x4000, 0x4000, (uint *)0);
385 dma_xfer((uint *)0x8000, 0x8000, (uint *)0);
387 dma_xfer((uint *)0x10000, 0x10000, (uint *)0);
389 dma_xfer((uint *)0x20000, 0x20000, (uint *)0);
391 dma_xfer((uint *)0x40000, 0x40000, (uint *)0);
393 dma_xfer((uint *)0x80000, 0x80000, (uint *)0);
395 dma_xfer((uint *)0x100000, 0x100000, (uint *)0);
397 dma_xfer((uint *)0x200000, 0x200000, (uint *)0);
399 dma_xfer((uint *)0x400000, 0x400000, (uint *)0);
401 for (i = 1; i < dram_size / 0x800000; i++) {
402 dma_xfer((uint *)(0x800000*i), 0x800000, (uint *)0);
406 * Enable errors for ECC.
408 ddr->err_disable = 0x00000000;
413 #endif /* CONFIG_DDR_ECC */