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