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