xref: /openbmc/u-boot/arch/powerpc/cpu/mpc83xx/spd_sdram.c (revision a47a12becf66f02a56da91c161e2edb625e9f20c)
1*a47a12beSStefan Roese /*
2*a47a12beSStefan Roese  * (C) Copyright 2006-2007 Freescale Semiconductor, Inc.
3*a47a12beSStefan Roese  *
4*a47a12beSStefan Roese  * (C) Copyright 2006
5*a47a12beSStefan Roese  * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
6*a47a12beSStefan Roese  *
7*a47a12beSStefan Roese  * Copyright (C) 2004-2006 Freescale Semiconductor, Inc.
8*a47a12beSStefan Roese  * (C) Copyright 2003 Motorola Inc.
9*a47a12beSStefan Roese  * Xianghua Xiao (X.Xiao@motorola.com)
10*a47a12beSStefan Roese  *
11*a47a12beSStefan Roese  * See file CREDITS for list of people who contributed to this
12*a47a12beSStefan Roese  * project.
13*a47a12beSStefan Roese  *
14*a47a12beSStefan Roese  * This program is free software; you can redistribute it and/or
15*a47a12beSStefan Roese  * modify it under the terms of the GNU General Public License as
16*a47a12beSStefan Roese  * published by the Free Software Foundation; either version 2 of
17*a47a12beSStefan Roese  * the License, or (at your option) any later version.
18*a47a12beSStefan Roese  *
19*a47a12beSStefan Roese  * This program is distributed in the hope that it will be useful,
20*a47a12beSStefan Roese  * but WITHOUT ANY WARRANTY; without even the implied warranty of
21*a47a12beSStefan Roese  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.	 See the
22*a47a12beSStefan Roese  * GNU General Public License for more details.
23*a47a12beSStefan Roese  *
24*a47a12beSStefan Roese  * You should have received a copy of the GNU General Public License
25*a47a12beSStefan Roese  * along with this program; if not, write to the Free Software
26*a47a12beSStefan Roese  * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
27*a47a12beSStefan Roese  * MA 02111-1307 USA
28*a47a12beSStefan Roese  */
29*a47a12beSStefan Roese 
30*a47a12beSStefan Roese #include <common.h>
31*a47a12beSStefan Roese #include <asm/processor.h>
32*a47a12beSStefan Roese #include <asm/io.h>
33*a47a12beSStefan Roese #include <i2c.h>
34*a47a12beSStefan Roese #include <spd.h>
35*a47a12beSStefan Roese #include <asm/mmu.h>
36*a47a12beSStefan Roese #include <spd_sdram.h>
37*a47a12beSStefan Roese 
38*a47a12beSStefan Roese DECLARE_GLOBAL_DATA_PTR;
39*a47a12beSStefan Roese 
40*a47a12beSStefan Roese void board_add_ram_info(int use_default)
41*a47a12beSStefan Roese {
42*a47a12beSStefan Roese 	volatile immap_t *immap = (immap_t *) CONFIG_SYS_IMMR;
43*a47a12beSStefan Roese 	volatile ddr83xx_t *ddr = &immap->ddr;
44*a47a12beSStefan Roese 	char buf[32];
45*a47a12beSStefan Roese 
46*a47a12beSStefan Roese 	printf(" (DDR%d", ((ddr->sdram_cfg & SDRAM_CFG_SDRAM_TYPE_MASK)
47*a47a12beSStefan Roese 			   >> SDRAM_CFG_SDRAM_TYPE_SHIFT) - 1);
48*a47a12beSStefan Roese 
49*a47a12beSStefan Roese 	if (ddr->sdram_cfg & SDRAM_CFG_32_BE)
50*a47a12beSStefan Roese 		puts(", 32-bit");
51*a47a12beSStefan Roese 	else
52*a47a12beSStefan Roese 		puts(", 64-bit");
53*a47a12beSStefan Roese 
54*a47a12beSStefan Roese 	if (ddr->sdram_cfg & SDRAM_CFG_ECC_EN)
55*a47a12beSStefan Roese 		puts(", ECC on");
56*a47a12beSStefan Roese 	else
57*a47a12beSStefan Roese 		puts(", ECC off");
58*a47a12beSStefan Roese 
59*a47a12beSStefan Roese 	printf(", %s MHz)", strmhz(buf, gd->mem_clk));
60*a47a12beSStefan Roese 
61*a47a12beSStefan Roese #if defined(CONFIG_SYS_LB_SDRAM) && defined(CONFIG_SYS_LBC_SDRAM_SIZE)
62*a47a12beSStefan Roese 	puts("\nSDRAM: ");
63*a47a12beSStefan Roese 	print_size (CONFIG_SYS_LBC_SDRAM_SIZE * 1024 * 1024, " (local bus)");
64*a47a12beSStefan Roese #endif
65*a47a12beSStefan Roese }
66*a47a12beSStefan Roese 
67*a47a12beSStefan Roese #ifdef CONFIG_SPD_EEPROM
68*a47a12beSStefan Roese #ifndef	CONFIG_SYS_READ_SPD
69*a47a12beSStefan Roese #define CONFIG_SYS_READ_SPD	i2c_read
70*a47a12beSStefan Roese #endif
71*a47a12beSStefan Roese 
72*a47a12beSStefan Roese /*
73*a47a12beSStefan Roese  * Convert picoseconds into clock cycles (rounding up if needed).
74*a47a12beSStefan Roese  */
75*a47a12beSStefan Roese int
76*a47a12beSStefan Roese picos_to_clk(int picos)
77*a47a12beSStefan Roese {
78*a47a12beSStefan Roese 	unsigned int mem_bus_clk;
79*a47a12beSStefan Roese 	int clks;
80*a47a12beSStefan Roese 
81*a47a12beSStefan Roese 	mem_bus_clk = gd->mem_clk >> 1;
82*a47a12beSStefan Roese 	clks = picos / (1000000000 / (mem_bus_clk / 1000));
83*a47a12beSStefan Roese 	if (picos % (1000000000 / (mem_bus_clk / 1000)) != 0)
84*a47a12beSStefan Roese 		clks++;
85*a47a12beSStefan Roese 
86*a47a12beSStefan Roese 	return clks;
87*a47a12beSStefan Roese }
88*a47a12beSStefan Roese 
89*a47a12beSStefan Roese unsigned int banksize(unsigned char row_dens)
90*a47a12beSStefan Roese {
91*a47a12beSStefan Roese 	return ((row_dens >> 2) | ((row_dens & 3) << 6)) << 24;
92*a47a12beSStefan Roese }
93*a47a12beSStefan Roese 
94*a47a12beSStefan Roese int read_spd(uint addr)
95*a47a12beSStefan Roese {
96*a47a12beSStefan Roese 	return ((int) addr);
97*a47a12beSStefan Roese }
98*a47a12beSStefan Roese 
99*a47a12beSStefan Roese #undef SPD_DEBUG
100*a47a12beSStefan Roese #ifdef SPD_DEBUG
101*a47a12beSStefan Roese static void spd_debug(spd_eeprom_t *spd)
102*a47a12beSStefan Roese {
103*a47a12beSStefan Roese 	printf ("\nDIMM type:       %-18.18s\n", spd->mpart);
104*a47a12beSStefan Roese 	printf ("SPD size:        %d\n", spd->info_size);
105*a47a12beSStefan Roese 	printf ("EEPROM size:     %d\n", 1 << spd->chip_size);
106*a47a12beSStefan Roese 	printf ("Memory type:     %d\n", spd->mem_type);
107*a47a12beSStefan Roese 	printf ("Row addr:        %d\n", spd->nrow_addr);
108*a47a12beSStefan Roese 	printf ("Column addr:     %d\n", spd->ncol_addr);
109*a47a12beSStefan Roese 	printf ("# of rows:       %d\n", spd->nrows);
110*a47a12beSStefan Roese 	printf ("Row density:     %d\n", spd->row_dens);
111*a47a12beSStefan Roese 	printf ("# of banks:      %d\n", spd->nbanks);
112*a47a12beSStefan Roese 	printf ("Data width:      %d\n",
113*a47a12beSStefan Roese 			256 * spd->dataw_msb + spd->dataw_lsb);
114*a47a12beSStefan Roese 	printf ("Chip width:      %d\n", spd->primw);
115*a47a12beSStefan Roese 	printf ("Refresh rate:    %02X\n", spd->refresh);
116*a47a12beSStefan Roese 	printf ("CAS latencies:   %02X\n", spd->cas_lat);
117*a47a12beSStefan Roese 	printf ("Write latencies: %02X\n", spd->write_lat);
118*a47a12beSStefan Roese 	printf ("tRP:             %d\n", spd->trp);
119*a47a12beSStefan Roese 	printf ("tRCD:            %d\n", spd->trcd);
120*a47a12beSStefan Roese 	printf ("\n");
121*a47a12beSStefan Roese }
122*a47a12beSStefan Roese #endif /* SPD_DEBUG */
123*a47a12beSStefan Roese 
124*a47a12beSStefan Roese long int spd_sdram()
125*a47a12beSStefan Roese {
126*a47a12beSStefan Roese 	volatile immap_t *immap = (immap_t *)CONFIG_SYS_IMMR;
127*a47a12beSStefan Roese 	volatile ddr83xx_t *ddr = &immap->ddr;
128*a47a12beSStefan Roese 	volatile law83xx_t *ecm = &immap->sysconf.ddrlaw[0];
129*a47a12beSStefan Roese 	spd_eeprom_t spd;
130*a47a12beSStefan Roese 	unsigned int n_ranks;
131*a47a12beSStefan Roese 	unsigned int odt_rd_cfg, odt_wr_cfg;
132*a47a12beSStefan Roese 	unsigned char twr_clk, twtr_clk;
133*a47a12beSStefan Roese 	unsigned int sdram_type;
134*a47a12beSStefan Roese 	unsigned int memsize;
135*a47a12beSStefan Roese 	unsigned int law_size;
136*a47a12beSStefan Roese 	unsigned char caslat, caslat_ctrl;
137*a47a12beSStefan Roese 	unsigned int trfc, trfc_clk, trfc_low, trfc_high;
138*a47a12beSStefan Roese 	unsigned int trcd_clk, trtp_clk;
139*a47a12beSStefan Roese 	unsigned char cke_min_clk;
140*a47a12beSStefan Roese 	unsigned char add_lat, wr_lat;
141*a47a12beSStefan Roese 	unsigned char wr_data_delay;
142*a47a12beSStefan Roese 	unsigned char four_act;
143*a47a12beSStefan Roese 	unsigned char cpo;
144*a47a12beSStefan Roese 	unsigned char burstlen;
145*a47a12beSStefan Roese 	unsigned char odt_cfg, mode_odt_enable;
146*a47a12beSStefan Roese 	unsigned int max_bus_clk;
147*a47a12beSStefan Roese 	unsigned int max_data_rate, effective_data_rate;
148*a47a12beSStefan Roese 	unsigned int ddrc_clk;
149*a47a12beSStefan Roese 	unsigned int refresh_clk;
150*a47a12beSStefan Roese 	unsigned int sdram_cfg;
151*a47a12beSStefan Roese 	unsigned int ddrc_ecc_enable;
152*a47a12beSStefan Roese 	unsigned int pvr = get_pvr();
153*a47a12beSStefan Roese 
154*a47a12beSStefan Roese 	/*
155*a47a12beSStefan Roese 	 * First disable the memory controller (could be enabled
156*a47a12beSStefan Roese 	 * by the debugger)
157*a47a12beSStefan Roese 	 */
158*a47a12beSStefan Roese 	clrsetbits_be32(&ddr->sdram_cfg, SDRAM_CFG_MEM_EN, 0);
159*a47a12beSStefan Roese 	sync();
160*a47a12beSStefan Roese 	isync();
161*a47a12beSStefan Roese 
162*a47a12beSStefan Roese 	/* Read SPD parameters with I2C */
163*a47a12beSStefan Roese 	CONFIG_SYS_READ_SPD(SPD_EEPROM_ADDRESS, 0, 1, (uchar *) & spd, sizeof (spd));
164*a47a12beSStefan Roese #ifdef SPD_DEBUG
165*a47a12beSStefan Roese 	spd_debug(&spd);
166*a47a12beSStefan Roese #endif
167*a47a12beSStefan Roese 	/* Check the memory type */
168*a47a12beSStefan Roese 	if (spd.mem_type != SPD_MEMTYPE_DDR && spd.mem_type != SPD_MEMTYPE_DDR2) {
169*a47a12beSStefan Roese 		debug("DDR: Module mem type is %02X\n", spd.mem_type);
170*a47a12beSStefan Roese 		return 0;
171*a47a12beSStefan Roese 	}
172*a47a12beSStefan Roese 
173*a47a12beSStefan Roese 	/* Check the number of physical bank */
174*a47a12beSStefan Roese 	if (spd.mem_type == SPD_MEMTYPE_DDR) {
175*a47a12beSStefan Roese 		n_ranks = spd.nrows;
176*a47a12beSStefan Roese 	} else {
177*a47a12beSStefan Roese 		n_ranks = (spd.nrows & 0x7) + 1;
178*a47a12beSStefan Roese 	}
179*a47a12beSStefan Roese 
180*a47a12beSStefan Roese 	if (n_ranks > 2) {
181*a47a12beSStefan Roese 		printf("DDR: The number of physical bank is %02X\n", n_ranks);
182*a47a12beSStefan Roese 		return 0;
183*a47a12beSStefan Roese 	}
184*a47a12beSStefan Roese 
185*a47a12beSStefan Roese 	/* Check if the number of row of the module is in the range of DDRC */
186*a47a12beSStefan Roese 	if (spd.nrow_addr < 12 || spd.nrow_addr > 15) {
187*a47a12beSStefan Roese 		printf("DDR: Row number is out of range of DDRC, row=%02X\n",
188*a47a12beSStefan Roese 							 spd.nrow_addr);
189*a47a12beSStefan Roese 		return 0;
190*a47a12beSStefan Roese 	}
191*a47a12beSStefan Roese 
192*a47a12beSStefan Roese 	/* Check if the number of col of the module is in the range of DDRC */
193*a47a12beSStefan Roese 	if (spd.ncol_addr < 8 || spd.ncol_addr > 11) {
194*a47a12beSStefan Roese 		printf("DDR: Col number is out of range of DDRC, col=%02X\n",
195*a47a12beSStefan Roese 							 spd.ncol_addr);
196*a47a12beSStefan Roese 		return 0;
197*a47a12beSStefan Roese 	}
198*a47a12beSStefan Roese 
199*a47a12beSStefan Roese #ifdef CONFIG_SYS_DDRCDR_VALUE
200*a47a12beSStefan Roese 	/*
201*a47a12beSStefan Roese 	 * Adjust DDR II IO voltage biasing.  It just makes it work.
202*a47a12beSStefan Roese 	 */
203*a47a12beSStefan Roese 	if(spd.mem_type == SPD_MEMTYPE_DDR2) {
204*a47a12beSStefan Roese 		immap->sysconf.ddrcdr = CONFIG_SYS_DDRCDR_VALUE;
205*a47a12beSStefan Roese 	}
206*a47a12beSStefan Roese 	udelay(50000);
207*a47a12beSStefan Roese #endif
208*a47a12beSStefan Roese 
209*a47a12beSStefan Roese 	/*
210*a47a12beSStefan Roese 	 * ODT configuration recommendation from DDR Controller Chapter.
211*a47a12beSStefan Roese 	 */
212*a47a12beSStefan Roese 	odt_rd_cfg = 0;			/* Never assert ODT */
213*a47a12beSStefan Roese 	odt_wr_cfg = 0;			/* Never assert ODT */
214*a47a12beSStefan Roese 	if (spd.mem_type == SPD_MEMTYPE_DDR2) {
215*a47a12beSStefan Roese 		odt_wr_cfg = 1;		/* Assert ODT on writes to CSn */
216*a47a12beSStefan Roese 	}
217*a47a12beSStefan Roese 
218*a47a12beSStefan Roese 	/* Setup DDR chip select register */
219*a47a12beSStefan Roese #ifdef CONFIG_SYS_83XX_DDR_USES_CS0
220*a47a12beSStefan Roese 	ddr->csbnds[0].csbnds = (banksize(spd.row_dens) >> 24) - 1;
221*a47a12beSStefan Roese 	ddr->cs_config[0] = ( 1 << 31
222*a47a12beSStefan Roese 			    | (odt_rd_cfg << 20)
223*a47a12beSStefan Roese 			    | (odt_wr_cfg << 16)
224*a47a12beSStefan Roese 			    | ((spd.nbanks == 8 ? 1 : 0) << 14)
225*a47a12beSStefan Roese 			    | ((spd.nrow_addr - 12) << 8)
226*a47a12beSStefan Roese 			    | (spd.ncol_addr - 8) );
227*a47a12beSStefan Roese 	debug("\n");
228*a47a12beSStefan Roese 	debug("cs0_bnds = 0x%08x\n",ddr->csbnds[0].csbnds);
229*a47a12beSStefan Roese 	debug("cs0_config = 0x%08x\n",ddr->cs_config[0]);
230*a47a12beSStefan Roese 
231*a47a12beSStefan Roese 	if (n_ranks == 2) {
232*a47a12beSStefan Roese 		ddr->csbnds[1].csbnds = ( (banksize(spd.row_dens) >> 8)
233*a47a12beSStefan Roese 				  | ((banksize(spd.row_dens) >> 23) - 1) );
234*a47a12beSStefan Roese 		ddr->cs_config[1] = ( 1<<31
235*a47a12beSStefan Roese 				    | (odt_rd_cfg << 20)
236*a47a12beSStefan Roese 				    | (odt_wr_cfg << 16)
237*a47a12beSStefan Roese 				    | ((spd.nbanks == 8 ? 1 : 0) << 14)
238*a47a12beSStefan Roese 				    | ((spd.nrow_addr - 12) << 8)
239*a47a12beSStefan Roese 				    | (spd.ncol_addr - 8) );
240*a47a12beSStefan Roese 		debug("cs1_bnds = 0x%08x\n",ddr->csbnds[1].csbnds);
241*a47a12beSStefan Roese 		debug("cs1_config = 0x%08x\n",ddr->cs_config[1]);
242*a47a12beSStefan Roese 	}
243*a47a12beSStefan Roese 
244*a47a12beSStefan Roese #else
245*a47a12beSStefan Roese 	ddr->csbnds[2].csbnds = (banksize(spd.row_dens) >> 24) - 1;
246*a47a12beSStefan Roese 	ddr->cs_config[2] = ( 1 << 31
247*a47a12beSStefan Roese 			    | (odt_rd_cfg << 20)
248*a47a12beSStefan Roese 			    | (odt_wr_cfg << 16)
249*a47a12beSStefan Roese 			    | ((spd.nbanks == 8 ? 1 : 0) << 14)
250*a47a12beSStefan Roese 			    | ((spd.nrow_addr - 12) << 8)
251*a47a12beSStefan Roese 			    | (spd.ncol_addr - 8) );
252*a47a12beSStefan Roese 	debug("\n");
253*a47a12beSStefan Roese 	debug("cs2_bnds = 0x%08x\n",ddr->csbnds[2].csbnds);
254*a47a12beSStefan Roese 	debug("cs2_config = 0x%08x\n",ddr->cs_config[2]);
255*a47a12beSStefan Roese 
256*a47a12beSStefan Roese 	if (n_ranks == 2) {
257*a47a12beSStefan Roese 		ddr->csbnds[3].csbnds = ( (banksize(spd.row_dens) >> 8)
258*a47a12beSStefan Roese 				  | ((banksize(spd.row_dens) >> 23) - 1) );
259*a47a12beSStefan Roese 		ddr->cs_config[3] = ( 1<<31
260*a47a12beSStefan Roese 				    | (odt_rd_cfg << 20)
261*a47a12beSStefan Roese 				    | (odt_wr_cfg << 16)
262*a47a12beSStefan Roese 				    | ((spd.nbanks == 8 ? 1 : 0) << 14)
263*a47a12beSStefan Roese 				    | ((spd.nrow_addr - 12) << 8)
264*a47a12beSStefan Roese 				    | (spd.ncol_addr - 8) );
265*a47a12beSStefan Roese 		debug("cs3_bnds = 0x%08x\n",ddr->csbnds[3].csbnds);
266*a47a12beSStefan Roese 		debug("cs3_config = 0x%08x\n",ddr->cs_config[3]);
267*a47a12beSStefan Roese 	}
268*a47a12beSStefan Roese #endif
269*a47a12beSStefan Roese 
270*a47a12beSStefan Roese 	/*
271*a47a12beSStefan Roese 	 * Figure out memory size in Megabytes.
272*a47a12beSStefan Roese 	 */
273*a47a12beSStefan Roese 	memsize = n_ranks * banksize(spd.row_dens) / 0x100000;
274*a47a12beSStefan Roese 
275*a47a12beSStefan Roese 	/*
276*a47a12beSStefan Roese 	 * First supported LAW size is 16M, at LAWAR_SIZE_16M == 23.
277*a47a12beSStefan Roese 	 */
278*a47a12beSStefan Roese 	law_size = 19 + __ilog2(memsize);
279*a47a12beSStefan Roese 
280*a47a12beSStefan Roese 	/*
281*a47a12beSStefan Roese 	 * Set up LAWBAR for all of DDR.
282*a47a12beSStefan Roese 	 */
283*a47a12beSStefan Roese 	ecm->bar = CONFIG_SYS_DDR_SDRAM_BASE & 0xfffff000;
284*a47a12beSStefan Roese 	ecm->ar  = (LAWAR_EN | LAWAR_TRGT_IF_DDR | (LAWAR_SIZE & law_size));
285*a47a12beSStefan Roese 	debug("DDR:bar=0x%08x\n", ecm->bar);
286*a47a12beSStefan Roese 	debug("DDR:ar=0x%08x\n", ecm->ar);
287*a47a12beSStefan Roese 
288*a47a12beSStefan Roese 	/*
289*a47a12beSStefan Roese 	 * Find the largest CAS by locating the highest 1 bit
290*a47a12beSStefan Roese 	 * in the spd.cas_lat field.  Translate it to a DDR
291*a47a12beSStefan Roese 	 * controller field value:
292*a47a12beSStefan Roese 	 *
293*a47a12beSStefan Roese 	 *	CAS Lat	DDR I	DDR II	Ctrl
294*a47a12beSStefan Roese 	 *	Clocks	SPD Bit	SPD Bit	Value
295*a47a12beSStefan Roese 	 *	-------	-------	-------	-----
296*a47a12beSStefan Roese 	 *	1.0	0		0001
297*a47a12beSStefan Roese 	 *	1.5	1		0010
298*a47a12beSStefan Roese 	 *	2.0	2	2	0011
299*a47a12beSStefan Roese 	 *	2.5	3		0100
300*a47a12beSStefan Roese 	 *	3.0	4	3	0101
301*a47a12beSStefan Roese 	 *	3.5	5		0110
302*a47a12beSStefan Roese 	 *	4.0	6	4	0111
303*a47a12beSStefan Roese 	 *	4.5			1000
304*a47a12beSStefan Roese 	 *	5.0		5	1001
305*a47a12beSStefan Roese 	 */
306*a47a12beSStefan Roese 	caslat = __ilog2(spd.cas_lat);
307*a47a12beSStefan Roese 	if ((spd.mem_type == SPD_MEMTYPE_DDR)
308*a47a12beSStefan Roese 	    && (caslat > 6)) {
309*a47a12beSStefan Roese 		printf("DDR I: Invalid SPD CAS Latency: 0x%x.\n", spd.cas_lat);
310*a47a12beSStefan Roese 		return 0;
311*a47a12beSStefan Roese 	} else if (spd.mem_type == SPD_MEMTYPE_DDR2
312*a47a12beSStefan Roese 		   && (caslat < 2 || caslat > 5)) {
313*a47a12beSStefan Roese 		printf("DDR II: Invalid SPD CAS Latency: 0x%x.\n",
314*a47a12beSStefan Roese 		       spd.cas_lat);
315*a47a12beSStefan Roese 		return 0;
316*a47a12beSStefan Roese 	}
317*a47a12beSStefan Roese 	debug("DDR: caslat SPD bit is %d\n", caslat);
318*a47a12beSStefan Roese 
319*a47a12beSStefan Roese 	max_bus_clk = 1000 *10 / (((spd.clk_cycle & 0xF0) >> 4) * 10
320*a47a12beSStefan Roese 			+ (spd.clk_cycle & 0x0f));
321*a47a12beSStefan Roese 	max_data_rate = max_bus_clk * 2;
322*a47a12beSStefan Roese 
323*a47a12beSStefan Roese 	debug("DDR:Module maximum data rate is: %d MHz\n", max_data_rate);
324*a47a12beSStefan Roese 
325*a47a12beSStefan Roese 	ddrc_clk = gd->mem_clk / 1000000;
326*a47a12beSStefan Roese 	effective_data_rate = 0;
327*a47a12beSStefan Roese 
328*a47a12beSStefan Roese 	if (max_data_rate >= 460) { /* it is DDR2-800, 667, 533 */
329*a47a12beSStefan Roese 		if (spd.cas_lat & 0x08)
330*a47a12beSStefan Roese 			caslat = 3;
331*a47a12beSStefan Roese 		else
332*a47a12beSStefan Roese 			caslat = 4;
333*a47a12beSStefan Roese 		if (ddrc_clk <= 460 && ddrc_clk > 350)
334*a47a12beSStefan Roese 			effective_data_rate = 400;
335*a47a12beSStefan Roese 		else if (ddrc_clk <=350 && ddrc_clk > 280)
336*a47a12beSStefan Roese 			effective_data_rate = 333;
337*a47a12beSStefan Roese 		else if (ddrc_clk <= 280 && ddrc_clk > 230)
338*a47a12beSStefan Roese 			effective_data_rate = 266;
339*a47a12beSStefan Roese 		else
340*a47a12beSStefan Roese 			effective_data_rate = 200;
341*a47a12beSStefan Roese 	} else if (max_data_rate >= 390 && max_data_rate < 460) { /* it is DDR 400 */
342*a47a12beSStefan Roese 		if (ddrc_clk <= 460 && ddrc_clk > 350) {
343*a47a12beSStefan Roese 			/* DDR controller clk at 350~460 */
344*a47a12beSStefan Roese 			effective_data_rate = 400; /* 5ns */
345*a47a12beSStefan Roese 			caslat = caslat;
346*a47a12beSStefan Roese 		} else if (ddrc_clk <= 350 && ddrc_clk > 280) {
347*a47a12beSStefan Roese 			/* DDR controller clk at 280~350 */
348*a47a12beSStefan Roese 			effective_data_rate = 333; /* 6ns */
349*a47a12beSStefan Roese 			if (spd.clk_cycle2 == 0x60)
350*a47a12beSStefan Roese 				caslat = caslat - 1;
351*a47a12beSStefan Roese 			else
352*a47a12beSStefan Roese 				caslat = caslat;
353*a47a12beSStefan Roese 		} else if (ddrc_clk <= 280 && ddrc_clk > 230) {
354*a47a12beSStefan Roese 			/* DDR controller clk at 230~280 */
355*a47a12beSStefan Roese 			effective_data_rate = 266; /* 7.5ns */
356*a47a12beSStefan Roese 			if (spd.clk_cycle3 == 0x75)
357*a47a12beSStefan Roese 				caslat = caslat - 2;
358*a47a12beSStefan Roese 			else if (spd.clk_cycle2 == 0x75)
359*a47a12beSStefan Roese 				caslat = caslat - 1;
360*a47a12beSStefan Roese 			else
361*a47a12beSStefan Roese 				caslat = caslat;
362*a47a12beSStefan Roese 		} else if (ddrc_clk <= 230 && ddrc_clk > 90) {
363*a47a12beSStefan Roese 			/* DDR controller clk at 90~230 */
364*a47a12beSStefan Roese 			effective_data_rate = 200; /* 10ns */
365*a47a12beSStefan Roese 			if (spd.clk_cycle3 == 0xa0)
366*a47a12beSStefan Roese 				caslat = caslat - 2;
367*a47a12beSStefan Roese 			else if (spd.clk_cycle2 == 0xa0)
368*a47a12beSStefan Roese 				caslat = caslat - 1;
369*a47a12beSStefan Roese 			else
370*a47a12beSStefan Roese 				caslat = caslat;
371*a47a12beSStefan Roese 		}
372*a47a12beSStefan Roese 	} else if (max_data_rate >= 323) { /* it is DDR 333 */
373*a47a12beSStefan Roese 		if (ddrc_clk <= 350 && ddrc_clk > 280) {
374*a47a12beSStefan Roese 			/* DDR controller clk at 280~350 */
375*a47a12beSStefan Roese 			effective_data_rate = 333; /* 6ns */
376*a47a12beSStefan Roese 			caslat = caslat;
377*a47a12beSStefan Roese 		} else if (ddrc_clk <= 280 && ddrc_clk > 230) {
378*a47a12beSStefan Roese 			/* DDR controller clk at 230~280 */
379*a47a12beSStefan Roese 			effective_data_rate = 266; /* 7.5ns */
380*a47a12beSStefan Roese 			if (spd.clk_cycle2 == 0x75)
381*a47a12beSStefan Roese 				caslat = caslat - 1;
382*a47a12beSStefan Roese 			else
383*a47a12beSStefan Roese 				caslat = caslat;
384*a47a12beSStefan Roese 		} else if (ddrc_clk <= 230 && ddrc_clk > 90) {
385*a47a12beSStefan Roese 			/* DDR controller clk at 90~230 */
386*a47a12beSStefan Roese 			effective_data_rate = 200; /* 10ns */
387*a47a12beSStefan Roese 			if (spd.clk_cycle3 == 0xa0)
388*a47a12beSStefan Roese 				caslat = caslat - 2;
389*a47a12beSStefan Roese 			else if (spd.clk_cycle2 == 0xa0)
390*a47a12beSStefan Roese 				caslat = caslat - 1;
391*a47a12beSStefan Roese 			else
392*a47a12beSStefan Roese 				caslat = caslat;
393*a47a12beSStefan Roese 		}
394*a47a12beSStefan Roese 	} else if (max_data_rate >= 256) { /* it is DDR 266 */
395*a47a12beSStefan Roese 		if (ddrc_clk <= 350 && ddrc_clk > 280) {
396*a47a12beSStefan Roese 			/* DDR controller clk at 280~350 */
397*a47a12beSStefan Roese 			printf("DDR: DDR controller freq is more than "
398*a47a12beSStefan Roese 				"max data rate of the module\n");
399*a47a12beSStefan Roese 			return 0;
400*a47a12beSStefan Roese 		} else if (ddrc_clk <= 280 && ddrc_clk > 230) {
401*a47a12beSStefan Roese 			/* DDR controller clk at 230~280 */
402*a47a12beSStefan Roese 			effective_data_rate = 266; /* 7.5ns */
403*a47a12beSStefan Roese 			caslat = caslat;
404*a47a12beSStefan Roese 		} else if (ddrc_clk <= 230 && ddrc_clk > 90) {
405*a47a12beSStefan Roese 			/* DDR controller clk at 90~230 */
406*a47a12beSStefan Roese 			effective_data_rate = 200; /* 10ns */
407*a47a12beSStefan Roese 			if (spd.clk_cycle2 == 0xa0)
408*a47a12beSStefan Roese 				caslat = caslat - 1;
409*a47a12beSStefan Roese 		}
410*a47a12beSStefan Roese 	} else if (max_data_rate >= 190) { /* it is DDR 200 */
411*a47a12beSStefan Roese 		if (ddrc_clk <= 350 && ddrc_clk > 230) {
412*a47a12beSStefan Roese 			/* DDR controller clk at 230~350 */
413*a47a12beSStefan Roese 			printf("DDR: DDR controller freq is more than "
414*a47a12beSStefan Roese 				"max data rate of the module\n");
415*a47a12beSStefan Roese 			return 0;
416*a47a12beSStefan Roese 		} else if (ddrc_clk <= 230 && ddrc_clk > 90) {
417*a47a12beSStefan Roese 			/* DDR controller clk at 90~230 */
418*a47a12beSStefan Roese 			effective_data_rate = 200; /* 10ns */
419*a47a12beSStefan Roese 			caslat = caslat;
420*a47a12beSStefan Roese 		}
421*a47a12beSStefan Roese 	}
422*a47a12beSStefan Roese 
423*a47a12beSStefan Roese 	debug("DDR:Effective data rate is: %dMHz\n", effective_data_rate);
424*a47a12beSStefan Roese 	debug("DDR:The MSB 1 of CAS Latency is: %d\n", caslat);
425*a47a12beSStefan Roese 
426*a47a12beSStefan Roese 	/*
427*a47a12beSStefan Roese 	 * Errata DDR6 work around: input enable 2 cycles earlier.
428*a47a12beSStefan Roese 	 * including MPC834x Rev1.0/1.1 and MPC8360 Rev1.1/1.2.
429*a47a12beSStefan Roese 	 */
430*a47a12beSStefan Roese 	if(PVR_MAJ(pvr) <= 1 && spd.mem_type == SPD_MEMTYPE_DDR){
431*a47a12beSStefan Roese 		if (caslat == 2)
432*a47a12beSStefan Roese 			ddr->debug_reg = 0x201c0000; /* CL=2 */
433*a47a12beSStefan Roese 		else if (caslat == 3)
434*a47a12beSStefan Roese 			ddr->debug_reg = 0x202c0000; /* CL=2.5 */
435*a47a12beSStefan Roese 		else if (caslat == 4)
436*a47a12beSStefan Roese 			ddr->debug_reg = 0x202c0000; /* CL=3.0 */
437*a47a12beSStefan Roese 
438*a47a12beSStefan Roese 		__asm__ __volatile__ ("sync");
439*a47a12beSStefan Roese 
440*a47a12beSStefan Roese 		debug("Errata DDR6 (debug_reg=0x%08x)\n", ddr->debug_reg);
441*a47a12beSStefan Roese 	}
442*a47a12beSStefan Roese 
443*a47a12beSStefan Roese 	/*
444*a47a12beSStefan Roese 	 * Convert caslat clocks to DDR controller value.
445*a47a12beSStefan Roese 	 * Force caslat_ctrl to be DDR Controller field-sized.
446*a47a12beSStefan Roese 	 */
447*a47a12beSStefan Roese 	if (spd.mem_type == SPD_MEMTYPE_DDR) {
448*a47a12beSStefan Roese 		caslat_ctrl = (caslat + 1) & 0x07;
449*a47a12beSStefan Roese 	} else {
450*a47a12beSStefan Roese 		caslat_ctrl =  (2 * caslat - 1) & 0x0f;
451*a47a12beSStefan Roese 	}
452*a47a12beSStefan Roese 
453*a47a12beSStefan Roese 	debug("DDR: effective data rate is %d MHz\n", effective_data_rate);
454*a47a12beSStefan Roese 	debug("DDR: caslat SPD bit is %d, controller field is 0x%x\n",
455*a47a12beSStefan Roese 	      caslat, caslat_ctrl);
456*a47a12beSStefan Roese 
457*a47a12beSStefan Roese 	/*
458*a47a12beSStefan Roese 	 * Timing Config 0.
459*a47a12beSStefan Roese 	 * Avoid writing for DDR I.
460*a47a12beSStefan Roese 	 */
461*a47a12beSStefan Roese 	if (spd.mem_type == SPD_MEMTYPE_DDR2) {
462*a47a12beSStefan Roese 		unsigned char taxpd_clk = 8;		/* By the book. */
463*a47a12beSStefan Roese 		unsigned char tmrd_clk = 2;		/* By the book. */
464*a47a12beSStefan Roese 		unsigned char act_pd_exit = 2;		/* Empirical? */
465*a47a12beSStefan Roese 		unsigned char pre_pd_exit = 6;		/* Empirical? */
466*a47a12beSStefan Roese 
467*a47a12beSStefan Roese 		ddr->timing_cfg_0 = (0
468*a47a12beSStefan Roese 			| ((act_pd_exit & 0x7) << 20)	/* ACT_PD_EXIT */
469*a47a12beSStefan Roese 			| ((pre_pd_exit & 0x7) << 16)	/* PRE_PD_EXIT */
470*a47a12beSStefan Roese 			| ((taxpd_clk & 0xf) << 8)	/* ODT_PD_EXIT */
471*a47a12beSStefan Roese 			| ((tmrd_clk & 0xf) << 0)	/* MRS_CYC */
472*a47a12beSStefan Roese 			);
473*a47a12beSStefan Roese 		debug("DDR: timing_cfg_0 = 0x%08x\n", ddr->timing_cfg_0);
474*a47a12beSStefan Roese 	}
475*a47a12beSStefan Roese 
476*a47a12beSStefan Roese 	/*
477*a47a12beSStefan Roese 	 * For DDR I, WRREC(Twr) and WRTORD(Twtr) are not in SPD,
478*a47a12beSStefan Roese 	 * use conservative value.
479*a47a12beSStefan Roese 	 * For DDR II, they are bytes 36 and 37, in quarter nanos.
480*a47a12beSStefan Roese 	 */
481*a47a12beSStefan Roese 
482*a47a12beSStefan Roese 	if (spd.mem_type == SPD_MEMTYPE_DDR) {
483*a47a12beSStefan Roese 		twr_clk = 3;	/* Clocks */
484*a47a12beSStefan Roese 		twtr_clk = 1;	/* Clocks */
485*a47a12beSStefan Roese 	} else {
486*a47a12beSStefan Roese 		twr_clk = picos_to_clk(spd.twr * 250);
487*a47a12beSStefan Roese 		twtr_clk = picos_to_clk(spd.twtr * 250);
488*a47a12beSStefan Roese 		if (twtr_clk < 2)
489*a47a12beSStefan Roese 			twtr_clk = 2;
490*a47a12beSStefan Roese 	}
491*a47a12beSStefan Roese 
492*a47a12beSStefan Roese 	/*
493*a47a12beSStefan Roese 	 * Calculate Trfc, in picos.
494*a47a12beSStefan Roese 	 * DDR I:  Byte 42 straight up in ns.
495*a47a12beSStefan Roese 	 * DDR II: Byte 40 and 42 swizzled some, in ns.
496*a47a12beSStefan Roese 	 */
497*a47a12beSStefan Roese 	if (spd.mem_type == SPD_MEMTYPE_DDR) {
498*a47a12beSStefan Roese 		trfc = spd.trfc * 1000;		/* up to ps */
499*a47a12beSStefan Roese 	} else {
500*a47a12beSStefan Roese 		unsigned int byte40_table_ps[8] = {
501*a47a12beSStefan Roese 			0,
502*a47a12beSStefan Roese 			250,
503*a47a12beSStefan Roese 			330,
504*a47a12beSStefan Roese 			500,
505*a47a12beSStefan Roese 			660,
506*a47a12beSStefan Roese 			750,
507*a47a12beSStefan Roese 			0,
508*a47a12beSStefan Roese 			0
509*a47a12beSStefan Roese 		};
510*a47a12beSStefan Roese 
511*a47a12beSStefan Roese 		trfc = (((spd.trctrfc_ext & 0x1) * 256) + spd.trfc) * 1000
512*a47a12beSStefan Roese 			+ byte40_table_ps[(spd.trctrfc_ext >> 1) & 0x7];
513*a47a12beSStefan Roese 	}
514*a47a12beSStefan Roese 	trfc_clk = picos_to_clk(trfc);
515*a47a12beSStefan Roese 
516*a47a12beSStefan Roese 	/*
517*a47a12beSStefan Roese 	 * Trcd, Byte 29, from quarter nanos to ps and clocks.
518*a47a12beSStefan Roese 	 */
519*a47a12beSStefan Roese 	trcd_clk = picos_to_clk(spd.trcd * 250) & 0x7;
520*a47a12beSStefan Roese 
521*a47a12beSStefan Roese 	/*
522*a47a12beSStefan Roese 	 * Convert trfc_clk to DDR controller fields.  DDR I should
523*a47a12beSStefan Roese 	 * fit in the REFREC field (16-19) of TIMING_CFG_1, but the
524*a47a12beSStefan Roese 	 * 83xx controller has an extended REFREC field of three bits.
525*a47a12beSStefan Roese 	 * The controller automatically adds 8 clocks to this value,
526*a47a12beSStefan Roese 	 * so preadjust it down 8 first before splitting it up.
527*a47a12beSStefan Roese 	 */
528*a47a12beSStefan Roese 	trfc_low = (trfc_clk - 8) & 0xf;
529*a47a12beSStefan Roese 	trfc_high = ((trfc_clk - 8) >> 4) & 0x3;
530*a47a12beSStefan Roese 
531*a47a12beSStefan Roese 	ddr->timing_cfg_1 =
532*a47a12beSStefan Roese 	    (((picos_to_clk(spd.trp * 250) & 0x07) << 28 ) |	/* PRETOACT */
533*a47a12beSStefan Roese 	     ((picos_to_clk(spd.tras * 1000) & 0x0f ) << 24 ) | /* ACTTOPRE */
534*a47a12beSStefan Roese 	     (trcd_clk << 20 ) |				/* ACTTORW */
535*a47a12beSStefan Roese 	     (caslat_ctrl << 16 ) |				/* CASLAT */
536*a47a12beSStefan Roese 	     (trfc_low << 12 ) |				/* REFEC */
537*a47a12beSStefan Roese 	     ((twr_clk & 0x07) << 8) |				/* WRRREC */
538*a47a12beSStefan Roese 	     ((picos_to_clk(spd.trrd * 250) & 0x07) << 4) |	/* ACTTOACT */
539*a47a12beSStefan Roese 	     ((twtr_clk & 0x07) << 0)				/* WRTORD */
540*a47a12beSStefan Roese 	    );
541*a47a12beSStefan Roese 
542*a47a12beSStefan Roese 	/*
543*a47a12beSStefan Roese 	 * Additive Latency
544*a47a12beSStefan Roese 	 * For DDR I, 0.
545*a47a12beSStefan Roese 	 * For DDR II, with ODT enabled, use "a value" less than ACTTORW,
546*a47a12beSStefan Roese 	 * which comes from Trcd, and also note that:
547*a47a12beSStefan Roese 	 *	add_lat + caslat must be >= 4
548*a47a12beSStefan Roese 	 */
549*a47a12beSStefan Roese 	add_lat = 0;
550*a47a12beSStefan Roese 	if (spd.mem_type == SPD_MEMTYPE_DDR2
551*a47a12beSStefan Roese 	    && (odt_wr_cfg || odt_rd_cfg)
552*a47a12beSStefan Roese 	    && (caslat < 4)) {
553*a47a12beSStefan Roese 		add_lat = 4 - caslat;
554*a47a12beSStefan Roese 		if ((add_lat + caslat) < 4) {
555*a47a12beSStefan Roese 			add_lat = 0;
556*a47a12beSStefan Roese 		}
557*a47a12beSStefan Roese 	}
558*a47a12beSStefan Roese 
559*a47a12beSStefan Roese 	/*
560*a47a12beSStefan Roese 	 * Write Data Delay
561*a47a12beSStefan Roese 	 * Historically 0x2 == 4/8 clock delay.
562*a47a12beSStefan Roese 	 * Empirically, 0x3 == 6/8 clock delay is suggested for DDR I 266.
563*a47a12beSStefan Roese 	 */
564*a47a12beSStefan Roese 	wr_data_delay = 2;
565*a47a12beSStefan Roese 
566*a47a12beSStefan Roese 	/*
567*a47a12beSStefan Roese 	 * Write Latency
568*a47a12beSStefan Roese 	 * Read to Precharge
569*a47a12beSStefan Roese 	 * Minimum CKE Pulse Width.
570*a47a12beSStefan Roese 	 * Four Activate Window
571*a47a12beSStefan Roese 	 */
572*a47a12beSStefan Roese 	if (spd.mem_type == SPD_MEMTYPE_DDR) {
573*a47a12beSStefan Roese 		/*
574*a47a12beSStefan Roese 		 * This is a lie.  It should really be 1, but if it is
575*a47a12beSStefan Roese 		 * set to 1, bits overlap into the old controller's
576*a47a12beSStefan Roese 		 * otherwise unused ACSM field.  If we leave it 0, then
577*a47a12beSStefan Roese 		 * the HW will magically treat it as 1 for DDR 1.  Oh Yea.
578*a47a12beSStefan Roese 		 */
579*a47a12beSStefan Roese 		wr_lat = 0;
580*a47a12beSStefan Roese 
581*a47a12beSStefan Roese 		trtp_clk = 2;		/* By the book. */
582*a47a12beSStefan Roese 		cke_min_clk = 1;	/* By the book. */
583*a47a12beSStefan Roese 		four_act = 1;		/* By the book. */
584*a47a12beSStefan Roese 
585*a47a12beSStefan Roese 	} else {
586*a47a12beSStefan Roese 		wr_lat = caslat - 1;
587*a47a12beSStefan Roese 
588*a47a12beSStefan Roese 		/* Convert SPD value from quarter nanos to picos. */
589*a47a12beSStefan Roese 		trtp_clk = picos_to_clk(spd.trtp * 250);
590*a47a12beSStefan Roese 		if (trtp_clk < 2)
591*a47a12beSStefan Roese 			trtp_clk = 2;
592*a47a12beSStefan Roese 		trtp_clk += add_lat;
593*a47a12beSStefan Roese 
594*a47a12beSStefan Roese 		cke_min_clk = 3;	/* By the book. */
595*a47a12beSStefan Roese 		four_act = picos_to_clk(37500);	/* By the book. 1k pages? */
596*a47a12beSStefan Roese 	}
597*a47a12beSStefan Roese 
598*a47a12beSStefan Roese 	/*
599*a47a12beSStefan Roese 	 * Empirically set ~MCAS-to-preamble override for DDR 2.
600*a47a12beSStefan Roese 	 * Your milage will vary.
601*a47a12beSStefan Roese 	 */
602*a47a12beSStefan Roese 	cpo = 0;
603*a47a12beSStefan Roese 	if (spd.mem_type == SPD_MEMTYPE_DDR2) {
604*a47a12beSStefan Roese 		if (effective_data_rate == 266) {
605*a47a12beSStefan Roese 			cpo = 0x4;		/* READ_LAT + 1/2 */
606*a47a12beSStefan Roese 		} else if (effective_data_rate == 333) {
607*a47a12beSStefan Roese 			cpo = 0x6;		/* READ_LAT + 1 */
608*a47a12beSStefan Roese 		} else if (effective_data_rate == 400) {
609*a47a12beSStefan Roese 			cpo = 0x7;		/* READ_LAT + 5/4 */
610*a47a12beSStefan Roese 		} else {
611*a47a12beSStefan Roese 			/* Automatic calibration */
612*a47a12beSStefan Roese 			cpo = 0x1f;
613*a47a12beSStefan Roese 		}
614*a47a12beSStefan Roese 	}
615*a47a12beSStefan Roese 
616*a47a12beSStefan Roese 	ddr->timing_cfg_2 = (0
617*a47a12beSStefan Roese 		| ((add_lat & 0x7) << 28)		/* ADD_LAT */
618*a47a12beSStefan Roese 		| ((cpo & 0x1f) << 23)			/* CPO */
619*a47a12beSStefan Roese 		| ((wr_lat & 0x7) << 19)		/* WR_LAT */
620*a47a12beSStefan Roese 		| ((trtp_clk & 0x7) << 13)		/* RD_TO_PRE */
621*a47a12beSStefan Roese 		| ((wr_data_delay & 0x7) << 10)		/* WR_DATA_DELAY */
622*a47a12beSStefan Roese 		| ((cke_min_clk & 0x7) << 6)		/* CKE_PLS */
623*a47a12beSStefan Roese 		| ((four_act & 0x1f) << 0)		/* FOUR_ACT */
624*a47a12beSStefan Roese 		);
625*a47a12beSStefan Roese 
626*a47a12beSStefan Roese 	debug("DDR:timing_cfg_1=0x%08x\n", ddr->timing_cfg_1);
627*a47a12beSStefan Roese 	debug("DDR:timing_cfg_2=0x%08x\n", ddr->timing_cfg_2);
628*a47a12beSStefan Roese 
629*a47a12beSStefan Roese 	/* Check DIMM data bus width */
630*a47a12beSStefan Roese 	if (spd.dataw_lsb < 64) {
631*a47a12beSStefan Roese 		if (spd.mem_type == SPD_MEMTYPE_DDR)
632*a47a12beSStefan Roese 			burstlen = 0x03; /* 32 bit data bus, burst len is 8 */
633*a47a12beSStefan Roese 		else
634*a47a12beSStefan Roese 			burstlen = 0x02; /* 32 bit data bus, burst len is 4 */
635*a47a12beSStefan Roese 		debug("\n   DDR DIMM: data bus width is 32 bit");
636*a47a12beSStefan Roese 	} else {
637*a47a12beSStefan Roese 		burstlen = 0x02; /* Others act as 64 bit bus, burst len is 4 */
638*a47a12beSStefan Roese 		debug("\n   DDR DIMM: data bus width is 64 bit");
639*a47a12beSStefan Roese 	}
640*a47a12beSStefan Roese 
641*a47a12beSStefan Roese 	/* Is this an ECC DDR chip? */
642*a47a12beSStefan Roese 	if (spd.config == 0x02)
643*a47a12beSStefan Roese 		debug(" with ECC\n");
644*a47a12beSStefan Roese 	else
645*a47a12beSStefan Roese 		debug(" without ECC\n");
646*a47a12beSStefan Roese 
647*a47a12beSStefan Roese 	/* Burst length is always 4 for 64 bit data bus, 8 for 32 bit data bus,
648*a47a12beSStefan Roese 	   Burst type is sequential
649*a47a12beSStefan Roese 	 */
650*a47a12beSStefan Roese 	if (spd.mem_type == SPD_MEMTYPE_DDR) {
651*a47a12beSStefan Roese 		switch (caslat) {
652*a47a12beSStefan Roese 		case 1:
653*a47a12beSStefan Roese 			ddr->sdram_mode = 0x50 | burstlen; /* CL=1.5 */
654*a47a12beSStefan Roese 			break;
655*a47a12beSStefan Roese 		case 2:
656*a47a12beSStefan Roese 			ddr->sdram_mode = 0x20 | burstlen; /* CL=2.0 */
657*a47a12beSStefan Roese 			break;
658*a47a12beSStefan Roese 		case 3:
659*a47a12beSStefan Roese 			ddr->sdram_mode = 0x60 | burstlen; /* CL=2.5 */
660*a47a12beSStefan Roese 			break;
661*a47a12beSStefan Roese 		case 4:
662*a47a12beSStefan Roese 			ddr->sdram_mode = 0x30 | burstlen; /* CL=3.0 */
663*a47a12beSStefan Roese 			break;
664*a47a12beSStefan Roese 		default:
665*a47a12beSStefan Roese 			printf("DDR:only CL 1.5, 2.0, 2.5, 3.0 is supported\n");
666*a47a12beSStefan Roese 			return 0;
667*a47a12beSStefan Roese 		}
668*a47a12beSStefan Roese 	} else {
669*a47a12beSStefan Roese 		mode_odt_enable = 0x0;                  /* Default disabled */
670*a47a12beSStefan Roese 		if (odt_wr_cfg || odt_rd_cfg) {
671*a47a12beSStefan Roese 			/*
672*a47a12beSStefan Roese 			 * Bits 6 and 2 in Extended MRS(1)
673*a47a12beSStefan Roese 			 * Bit 2 == 0x04 == 75 Ohm, with 2 DIMM modules.
674*a47a12beSStefan Roese 			 * Bit 6 == 0x40 == 150 Ohm, with 1 DIMM module.
675*a47a12beSStefan Roese 			 */
676*a47a12beSStefan Roese 			mode_odt_enable = 0x40;         /* 150 Ohm */
677*a47a12beSStefan Roese 		}
678*a47a12beSStefan Roese 
679*a47a12beSStefan Roese 		ddr->sdram_mode =
680*a47a12beSStefan Roese 			(0
681*a47a12beSStefan Roese 			 | (1 << (16 + 10))             /* DQS Differential disable */
682*a47a12beSStefan Roese 			 | (add_lat << (16 + 3))        /* Additive Latency in EMRS1 */
683*a47a12beSStefan Roese 			 | (mode_odt_enable << 16)      /* ODT Enable in EMRS1 */
684*a47a12beSStefan Roese 			 | ((twr_clk - 1) << 9)         /* Write Recovery Autopre */
685*a47a12beSStefan Roese 			 | (caslat << 4)                /* caslat */
686*a47a12beSStefan Roese 			 | (burstlen << 0)              /* Burst length */
687*a47a12beSStefan Roese 			);
688*a47a12beSStefan Roese 	}
689*a47a12beSStefan Roese 	debug("DDR:sdram_mode=0x%08x\n", ddr->sdram_mode);
690*a47a12beSStefan Roese 
691*a47a12beSStefan Roese 	/*
692*a47a12beSStefan Roese 	 * Clear EMRS2 and EMRS3.
693*a47a12beSStefan Roese 	 */
694*a47a12beSStefan Roese 	ddr->sdram_mode2 = 0;
695*a47a12beSStefan Roese 	debug("DDR: sdram_mode2 = 0x%08x\n", ddr->sdram_mode2);
696*a47a12beSStefan Roese 
697*a47a12beSStefan Roese 	switch (spd.refresh) {
698*a47a12beSStefan Roese 		case 0x00:
699*a47a12beSStefan Roese 		case 0x80:
700*a47a12beSStefan Roese 			refresh_clk = picos_to_clk(15625000);
701*a47a12beSStefan Roese 			break;
702*a47a12beSStefan Roese 		case 0x01:
703*a47a12beSStefan Roese 		case 0x81:
704*a47a12beSStefan Roese 			refresh_clk = picos_to_clk(3900000);
705*a47a12beSStefan Roese 			break;
706*a47a12beSStefan Roese 		case 0x02:
707*a47a12beSStefan Roese 		case 0x82:
708*a47a12beSStefan Roese 			refresh_clk = picos_to_clk(7800000);
709*a47a12beSStefan Roese 			break;
710*a47a12beSStefan Roese 		case 0x03:
711*a47a12beSStefan Roese 		case 0x83:
712*a47a12beSStefan Roese 			refresh_clk = picos_to_clk(31300000);
713*a47a12beSStefan Roese 			break;
714*a47a12beSStefan Roese 		case 0x04:
715*a47a12beSStefan Roese 		case 0x84:
716*a47a12beSStefan Roese 			refresh_clk = picos_to_clk(62500000);
717*a47a12beSStefan Roese 			break;
718*a47a12beSStefan Roese 		case 0x05:
719*a47a12beSStefan Roese 		case 0x85:
720*a47a12beSStefan Roese 			refresh_clk = picos_to_clk(125000000);
721*a47a12beSStefan Roese 			break;
722*a47a12beSStefan Roese 		default:
723*a47a12beSStefan Roese 			refresh_clk = 0x512;
724*a47a12beSStefan Roese 			break;
725*a47a12beSStefan Roese 	}
726*a47a12beSStefan Roese 
727*a47a12beSStefan Roese 	/*
728*a47a12beSStefan Roese 	 * Set BSTOPRE to 0x100 for page mode
729*a47a12beSStefan Roese 	 * If auto-charge is used, set BSTOPRE = 0
730*a47a12beSStefan Roese 	 */
731*a47a12beSStefan Roese 	ddr->sdram_interval = ((refresh_clk & 0x3fff) << 16) | 0x100;
732*a47a12beSStefan Roese 	debug("DDR:sdram_interval=0x%08x\n", ddr->sdram_interval);
733*a47a12beSStefan Roese 
734*a47a12beSStefan Roese 	/*
735*a47a12beSStefan Roese 	 * SDRAM Cfg 2
736*a47a12beSStefan Roese 	 */
737*a47a12beSStefan Roese 	odt_cfg = 0;
738*a47a12beSStefan Roese #ifndef CONFIG_NEVER_ASSERT_ODT_TO_CPU
739*a47a12beSStefan Roese 	if (odt_rd_cfg | odt_wr_cfg) {
740*a47a12beSStefan Roese 		odt_cfg = 0x2;		/* ODT to IOs during reads */
741*a47a12beSStefan Roese 	}
742*a47a12beSStefan Roese #endif
743*a47a12beSStefan Roese 	if (spd.mem_type == SPD_MEMTYPE_DDR2) {
744*a47a12beSStefan Roese 		ddr->sdram_cfg2 = (0
745*a47a12beSStefan Roese 			    | (0 << 26)	/* True DQS */
746*a47a12beSStefan Roese 			    | (odt_cfg << 21)	/* ODT only read */
747*a47a12beSStefan Roese 			    | (1 << 12)	/* 1 refresh at a time */
748*a47a12beSStefan Roese 			    );
749*a47a12beSStefan Roese 
750*a47a12beSStefan Roese 		debug("DDR: sdram_cfg2  = 0x%08x\n", ddr->sdram_cfg2);
751*a47a12beSStefan Roese 	}
752*a47a12beSStefan Roese 
753*a47a12beSStefan Roese #ifdef CONFIG_SYS_DDR_SDRAM_CLK_CNTL	/* Optional platform specific value */
754*a47a12beSStefan Roese 	ddr->sdram_clk_cntl = CONFIG_SYS_DDR_SDRAM_CLK_CNTL;
755*a47a12beSStefan Roese #endif
756*a47a12beSStefan Roese 	debug("DDR:sdram_clk_cntl=0x%08x\n", ddr->sdram_clk_cntl);
757*a47a12beSStefan Roese 
758*a47a12beSStefan Roese 	asm("sync;isync");
759*a47a12beSStefan Roese 
760*a47a12beSStefan Roese 	udelay(600);
761*a47a12beSStefan Roese 
762*a47a12beSStefan Roese 	/*
763*a47a12beSStefan Roese 	 * Figure out the settings for the sdram_cfg register. Build up
764*a47a12beSStefan Roese 	 * the value in 'sdram_cfg' before writing since the write into
765*a47a12beSStefan Roese 	 * the register will actually enable the memory controller, and all
766*a47a12beSStefan Roese 	 * settings must be done before enabling.
767*a47a12beSStefan Roese 	 *
768*a47a12beSStefan Roese 	 * sdram_cfg[0]   = 1 (ddr sdram logic enable)
769*a47a12beSStefan Roese 	 * sdram_cfg[1]   = 1 (self-refresh-enable)
770*a47a12beSStefan Roese 	 * sdram_cfg[5:7] = (SDRAM type = DDR SDRAM)
771*a47a12beSStefan Roese 	 *			010 DDR 1 SDRAM
772*a47a12beSStefan Roese 	 *			011 DDR 2 SDRAM
773*a47a12beSStefan Roese 	 * sdram_cfg[12] = 0 (32_BE =0 , 64 bit bus mode)
774*a47a12beSStefan Roese 	 * sdram_cfg[13] = 0 (8_BE =0, 4-beat bursts)
775*a47a12beSStefan Roese 	 */
776*a47a12beSStefan Roese 	if (spd.mem_type == SPD_MEMTYPE_DDR)
777*a47a12beSStefan Roese 		sdram_type = SDRAM_CFG_SDRAM_TYPE_DDR1;
778*a47a12beSStefan Roese 	else
779*a47a12beSStefan Roese 		sdram_type = SDRAM_CFG_SDRAM_TYPE_DDR2;
780*a47a12beSStefan Roese 
781*a47a12beSStefan Roese 	sdram_cfg = (0
782*a47a12beSStefan Roese 		     | SDRAM_CFG_MEM_EN		/* DDR enable */
783*a47a12beSStefan Roese 		     | SDRAM_CFG_SREN		/* Self refresh */
784*a47a12beSStefan Roese 		     | sdram_type		/* SDRAM type */
785*a47a12beSStefan Roese 		     );
786*a47a12beSStefan Roese 
787*a47a12beSStefan Roese 	/* sdram_cfg[3] = RD_EN - registered DIMM enable */
788*a47a12beSStefan Roese 	if (spd.mod_attr & 0x02)
789*a47a12beSStefan Roese 		sdram_cfg |= SDRAM_CFG_RD_EN;
790*a47a12beSStefan Roese 
791*a47a12beSStefan Roese 	/* The DIMM is 32bit width */
792*a47a12beSStefan Roese 	if (spd.dataw_lsb < 64) {
793*a47a12beSStefan Roese 		if (spd.mem_type == SPD_MEMTYPE_DDR)
794*a47a12beSStefan Roese 			sdram_cfg |= SDRAM_CFG_32_BE | SDRAM_CFG_8_BE;
795*a47a12beSStefan Roese 		if (spd.mem_type == SPD_MEMTYPE_DDR2)
796*a47a12beSStefan Roese 			sdram_cfg |= SDRAM_CFG_32_BE;
797*a47a12beSStefan Roese 	}
798*a47a12beSStefan Roese 
799*a47a12beSStefan Roese 	ddrc_ecc_enable = 0;
800*a47a12beSStefan Roese 
801*a47a12beSStefan Roese #if defined(CONFIG_DDR_ECC)
802*a47a12beSStefan Roese 	/* Enable ECC with sdram_cfg[2] */
803*a47a12beSStefan Roese 	if (spd.config == 0x02) {
804*a47a12beSStefan Roese 		sdram_cfg |= 0x20000000;
805*a47a12beSStefan Roese 		ddrc_ecc_enable = 1;
806*a47a12beSStefan Roese 		/* disable error detection */
807*a47a12beSStefan Roese 		ddr->err_disable = ~ECC_ERROR_ENABLE;
808*a47a12beSStefan Roese 		/* set single bit error threshold to maximum value,
809*a47a12beSStefan Roese 		 * reset counter to zero */
810*a47a12beSStefan Roese 		ddr->err_sbe = (255 << ECC_ERROR_MAN_SBET_SHIFT) |
811*a47a12beSStefan Roese 				(0 << ECC_ERROR_MAN_SBEC_SHIFT);
812*a47a12beSStefan Roese 	}
813*a47a12beSStefan Roese 
814*a47a12beSStefan Roese 	debug("DDR:err_disable=0x%08x\n", ddr->err_disable);
815*a47a12beSStefan Roese 	debug("DDR:err_sbe=0x%08x\n", ddr->err_sbe);
816*a47a12beSStefan Roese #endif
817*a47a12beSStefan Roese 	debug("   DDRC ECC mode: %s\n", ddrc_ecc_enable ? "ON":"OFF");
818*a47a12beSStefan Roese 
819*a47a12beSStefan Roese #if defined(CONFIG_DDR_2T_TIMING)
820*a47a12beSStefan Roese 	/*
821*a47a12beSStefan Roese 	 * Enable 2T timing by setting sdram_cfg[16].
822*a47a12beSStefan Roese 	 */
823*a47a12beSStefan Roese 	sdram_cfg |= SDRAM_CFG_2T_EN;
824*a47a12beSStefan Roese #endif
825*a47a12beSStefan Roese 	/* Enable controller, and GO! */
826*a47a12beSStefan Roese 	ddr->sdram_cfg = sdram_cfg;
827*a47a12beSStefan Roese 	asm("sync;isync");
828*a47a12beSStefan Roese 	udelay(500);
829*a47a12beSStefan Roese 
830*a47a12beSStefan Roese 	debug("DDR:sdram_cfg=0x%08x\n", ddr->sdram_cfg);
831*a47a12beSStefan Roese 	return memsize; /*in MBytes*/
832*a47a12beSStefan Roese }
833*a47a12beSStefan Roese #endif /* CONFIG_SPD_EEPROM */
834*a47a12beSStefan Roese 
835*a47a12beSStefan Roese #if defined(CONFIG_DDR_ECC) && !defined(CONFIG_ECC_INIT_VIA_DDRCONTROLLER)
836*a47a12beSStefan Roese /*
837*a47a12beSStefan Roese  * Use timebase counter, get_timer() is not availabe
838*a47a12beSStefan Roese  * at this point of initialization yet.
839*a47a12beSStefan Roese  */
840*a47a12beSStefan Roese static __inline__ unsigned long get_tbms (void)
841*a47a12beSStefan Roese {
842*a47a12beSStefan Roese 	unsigned long tbl;
843*a47a12beSStefan Roese 	unsigned long tbu1, tbu2;
844*a47a12beSStefan Roese 	unsigned long ms;
845*a47a12beSStefan Roese 	unsigned long long tmp;
846*a47a12beSStefan Roese 
847*a47a12beSStefan Roese 	ulong tbclk = get_tbclk();
848*a47a12beSStefan Roese 
849*a47a12beSStefan Roese 	/* get the timebase ticks */
850*a47a12beSStefan Roese 	do {
851*a47a12beSStefan Roese 		asm volatile ("mftbu %0":"=r" (tbu1):);
852*a47a12beSStefan Roese 		asm volatile ("mftb %0":"=r" (tbl):);
853*a47a12beSStefan Roese 		asm volatile ("mftbu %0":"=r" (tbu2):);
854*a47a12beSStefan Roese 	} while (tbu1 != tbu2);
855*a47a12beSStefan Roese 
856*a47a12beSStefan Roese 	/* convert ticks to ms */
857*a47a12beSStefan Roese 	tmp = (unsigned long long)(tbu1);
858*a47a12beSStefan Roese 	tmp = (tmp << 32);
859*a47a12beSStefan Roese 	tmp += (unsigned long long)(tbl);
860*a47a12beSStefan Roese 	ms = tmp/(tbclk/1000);
861*a47a12beSStefan Roese 
862*a47a12beSStefan Roese 	return ms;
863*a47a12beSStefan Roese }
864*a47a12beSStefan Roese 
865*a47a12beSStefan Roese /*
866*a47a12beSStefan Roese  * Initialize all of memory for ECC, then enable errors.
867*a47a12beSStefan Roese  */
868*a47a12beSStefan Roese void ddr_enable_ecc(unsigned int dram_size)
869*a47a12beSStefan Roese {
870*a47a12beSStefan Roese 	volatile immap_t *immap = (immap_t *)CONFIG_SYS_IMMR;
871*a47a12beSStefan Roese 	volatile ddr83xx_t *ddr= &immap->ddr;
872*a47a12beSStefan Roese 	unsigned long t_start, t_end;
873*a47a12beSStefan Roese 	register u64 *p;
874*a47a12beSStefan Roese 	register uint size;
875*a47a12beSStefan Roese 	unsigned int pattern[2];
876*a47a12beSStefan Roese 
877*a47a12beSStefan Roese 	icache_enable();
878*a47a12beSStefan Roese 	t_start = get_tbms();
879*a47a12beSStefan Roese 	pattern[0] = 0xdeadbeef;
880*a47a12beSStefan Roese 	pattern[1] = 0xdeadbeef;
881*a47a12beSStefan Roese 
882*a47a12beSStefan Roese #if defined(CONFIG_DDR_ECC_INIT_VIA_DMA)
883*a47a12beSStefan Roese 	dma_meminit(pattern[0], dram_size);
884*a47a12beSStefan Roese #else
885*a47a12beSStefan Roese 	debug("ddr init: CPU FP write method\n");
886*a47a12beSStefan Roese 	size = dram_size;
887*a47a12beSStefan Roese 	for (p = 0; p < (u64*)(size); p++) {
888*a47a12beSStefan Roese 		ppcDWstore((u32*)p, pattern);
889*a47a12beSStefan Roese 	}
890*a47a12beSStefan Roese 	__asm__ __volatile__ ("sync");
891*a47a12beSStefan Roese #endif
892*a47a12beSStefan Roese 
893*a47a12beSStefan Roese 	t_end = get_tbms();
894*a47a12beSStefan Roese 	icache_disable();
895*a47a12beSStefan Roese 
896*a47a12beSStefan Roese 	debug("\nREADY!!\n");
897*a47a12beSStefan Roese 	debug("ddr init duration: %ld ms\n", t_end - t_start);
898*a47a12beSStefan Roese 
899*a47a12beSStefan Roese 	/* Clear All ECC Errors */
900*a47a12beSStefan Roese 	if ((ddr->err_detect & ECC_ERROR_DETECT_MME) == ECC_ERROR_DETECT_MME)
901*a47a12beSStefan Roese 		ddr->err_detect |= ECC_ERROR_DETECT_MME;
902*a47a12beSStefan Roese 	if ((ddr->err_detect & ECC_ERROR_DETECT_MBE) == ECC_ERROR_DETECT_MBE)
903*a47a12beSStefan Roese 		ddr->err_detect |= ECC_ERROR_DETECT_MBE;
904*a47a12beSStefan Roese 	if ((ddr->err_detect & ECC_ERROR_DETECT_SBE) == ECC_ERROR_DETECT_SBE)
905*a47a12beSStefan Roese 		ddr->err_detect |= ECC_ERROR_DETECT_SBE;
906*a47a12beSStefan Roese 	if ((ddr->err_detect & ECC_ERROR_DETECT_MSE) == ECC_ERROR_DETECT_MSE)
907*a47a12beSStefan Roese 		ddr->err_detect |= ECC_ERROR_DETECT_MSE;
908*a47a12beSStefan Roese 
909*a47a12beSStefan Roese 	/* Disable ECC-Interrupts */
910*a47a12beSStefan Roese 	ddr->err_int_en &= ECC_ERR_INT_DISABLE;
911*a47a12beSStefan Roese 
912*a47a12beSStefan Roese 	/* Enable errors for ECC */
913*a47a12beSStefan Roese 	ddr->err_disable &= ECC_ERROR_ENABLE;
914*a47a12beSStefan Roese 
915*a47a12beSStefan Roese 	__asm__ __volatile__ ("sync");
916*a47a12beSStefan Roese 	__asm__ __volatile__ ("isync");
917*a47a12beSStefan Roese }
918*a47a12beSStefan Roese #endif	/* CONFIG_DDR_ECC */
919