xref: /openbmc/u-boot/drivers/ddr/fsl/ctrl_regs.c (revision d4263b8adb5bd940c95cbaebaa0da9eaf759bfed)
15614e71bSYork Sun /*
25614e71bSYork Sun  * Copyright 2008-2012 Freescale Semiconductor, Inc.
35614e71bSYork Sun  *
45614e71bSYork Sun  * SPDX-License-Identifier:	GPL-2.0+
55614e71bSYork Sun  */
65614e71bSYork Sun 
75614e71bSYork Sun /*
85614e71bSYork Sun  * Generic driver for Freescale DDR/DDR2/DDR3 memory controller.
95614e71bSYork Sun  * Based on code from spd_sdram.c
105614e71bSYork Sun  * Author: James Yang [at freescale.com]
115614e71bSYork Sun  */
125614e71bSYork Sun 
135614e71bSYork Sun #include <common.h>
145614e71bSYork Sun #include <fsl_ddr_sdram.h>
155614e71bSYork Sun 
165614e71bSYork Sun #include <fsl_ddr.h>
179a17eb5bSYork Sun #include <fsl_immap.h>
185614e71bSYork Sun #include <asm/io.h>
195614e71bSYork Sun 
205614e71bSYork Sun #define _DDR_ADDR CONFIG_SYS_FSL_DDR_ADDR
215614e71bSYork Sun 
225614e71bSYork Sun static u32 fsl_ddr_get_version(void)
235614e71bSYork Sun {
249a17eb5bSYork Sun 	struct ccsr_ddr __iomem *ddr;
255614e71bSYork Sun 	u32 ver_major_minor_errata;
265614e71bSYork Sun 
275614e71bSYork Sun 	ddr = (void *)_DDR_ADDR;
285614e71bSYork Sun 	ver_major_minor_errata = (in_be32(&ddr->ip_rev1) & 0xFFFF) << 8;
295614e71bSYork Sun 	ver_major_minor_errata |= (in_be32(&ddr->ip_rev2) & 0xFF00) >> 8;
305614e71bSYork Sun 
315614e71bSYork Sun 	return ver_major_minor_errata;
325614e71bSYork Sun }
335614e71bSYork Sun 
345614e71bSYork Sun unsigned int picos_to_mclk(unsigned int picos);
355614e71bSYork Sun 
365614e71bSYork Sun /*
375614e71bSYork Sun  * Determine Rtt value.
385614e71bSYork Sun  *
395614e71bSYork Sun  * This should likely be either board or controller specific.
405614e71bSYork Sun  *
415614e71bSYork Sun  * Rtt(nominal) - DDR2:
425614e71bSYork Sun  *	0 = Rtt disabled
435614e71bSYork Sun  *	1 = 75 ohm
445614e71bSYork Sun  *	2 = 150 ohm
455614e71bSYork Sun  *	3 = 50 ohm
465614e71bSYork Sun  * Rtt(nominal) - DDR3:
475614e71bSYork Sun  *	0 = Rtt disabled
485614e71bSYork Sun  *	1 = 60 ohm
495614e71bSYork Sun  *	2 = 120 ohm
505614e71bSYork Sun  *	3 = 40 ohm
515614e71bSYork Sun  *	4 = 20 ohm
525614e71bSYork Sun  *	5 = 30 ohm
535614e71bSYork Sun  *
545614e71bSYork Sun  * FIXME: Apparently 8641 needs a value of 2
555614e71bSYork Sun  * FIXME: Old code seys if 667 MHz or higher, use 3 on 8572
565614e71bSYork Sun  *
575614e71bSYork Sun  * FIXME: There was some effort down this line earlier:
585614e71bSYork Sun  *
595614e71bSYork Sun  *	unsigned int i;
605614e71bSYork Sun  *	for (i = 0; i < CONFIG_CHIP_SELECTS_PER_CTRL/2; i++) {
615614e71bSYork Sun  *		if (popts->dimmslot[i].num_valid_cs
625614e71bSYork Sun  *		    && (popts->cs_local_opts[2*i].odt_rd_cfg
635614e71bSYork Sun  *			|| popts->cs_local_opts[2*i].odt_wr_cfg)) {
645614e71bSYork Sun  *			rtt = 2;
655614e71bSYork Sun  *			break;
665614e71bSYork Sun  *		}
675614e71bSYork Sun  *	}
685614e71bSYork Sun  */
695614e71bSYork Sun static inline int fsl_ddr_get_rtt(void)
705614e71bSYork Sun {
715614e71bSYork Sun 	int rtt;
725614e71bSYork Sun 
735614e71bSYork Sun #if defined(CONFIG_SYS_FSL_DDR1)
745614e71bSYork Sun 	rtt = 0;
755614e71bSYork Sun #elif defined(CONFIG_SYS_FSL_DDR2)
765614e71bSYork Sun 	rtt = 3;
775614e71bSYork Sun #else
785614e71bSYork Sun 	rtt = 0;
795614e71bSYork Sun #endif
805614e71bSYork Sun 
815614e71bSYork Sun 	return rtt;
825614e71bSYork Sun }
835614e71bSYork Sun 
845614e71bSYork Sun /*
855614e71bSYork Sun  * compute the CAS write latency according to DDR3 spec
865614e71bSYork Sun  * CWL = 5 if tCK >= 2.5ns
875614e71bSYork Sun  *       6 if 2.5ns > tCK >= 1.875ns
885614e71bSYork Sun  *       7 if 1.875ns > tCK >= 1.5ns
895614e71bSYork Sun  *       8 if 1.5ns > tCK >= 1.25ns
905614e71bSYork Sun  *       9 if 1.25ns > tCK >= 1.07ns
915614e71bSYork Sun  *       10 if 1.07ns > tCK >= 0.935ns
925614e71bSYork Sun  *       11 if 0.935ns > tCK >= 0.833ns
935614e71bSYork Sun  *       12 if 0.833ns > tCK >= 0.75ns
945614e71bSYork Sun  */
955614e71bSYork Sun static inline unsigned int compute_cas_write_latency(void)
965614e71bSYork Sun {
975614e71bSYork Sun 	unsigned int cwl;
985614e71bSYork Sun 	const unsigned int mclk_ps = get_memory_clk_period_ps();
995614e71bSYork Sun 
1005614e71bSYork Sun 	if (mclk_ps >= 2500)
1015614e71bSYork Sun 		cwl = 5;
1025614e71bSYork Sun 	else if (mclk_ps >= 1875)
1035614e71bSYork Sun 		cwl = 6;
1045614e71bSYork Sun 	else if (mclk_ps >= 1500)
1055614e71bSYork Sun 		cwl = 7;
1065614e71bSYork Sun 	else if (mclk_ps >= 1250)
1075614e71bSYork Sun 		cwl = 8;
1085614e71bSYork Sun 	else if (mclk_ps >= 1070)
1095614e71bSYork Sun 		cwl = 9;
1105614e71bSYork Sun 	else if (mclk_ps >= 935)
1115614e71bSYork Sun 		cwl = 10;
1125614e71bSYork Sun 	else if (mclk_ps >= 833)
1135614e71bSYork Sun 		cwl = 11;
1145614e71bSYork Sun 	else if (mclk_ps >= 750)
1155614e71bSYork Sun 		cwl = 12;
1165614e71bSYork Sun 	else {
1175614e71bSYork Sun 		cwl = 12;
1185614e71bSYork Sun 		printf("Warning: CWL is out of range\n");
1195614e71bSYork Sun 	}
1205614e71bSYork Sun 	return cwl;
1215614e71bSYork Sun }
1225614e71bSYork Sun 
1235614e71bSYork Sun /* Chip Select Configuration (CSn_CONFIG) */
1245614e71bSYork Sun static void set_csn_config(int dimm_number, int i, fsl_ddr_cfg_regs_t *ddr,
1255614e71bSYork Sun 			       const memctl_options_t *popts,
1265614e71bSYork Sun 			       const dimm_params_t *dimm_params)
1275614e71bSYork Sun {
1285614e71bSYork Sun 	unsigned int cs_n_en = 0; /* Chip Select enable */
1295614e71bSYork Sun 	unsigned int intlv_en = 0; /* Memory controller interleave enable */
1305614e71bSYork Sun 	unsigned int intlv_ctl = 0; /* Interleaving control */
1315614e71bSYork Sun 	unsigned int ap_n_en = 0; /* Chip select n auto-precharge enable */
1325614e71bSYork Sun 	unsigned int odt_rd_cfg = 0; /* ODT for reads configuration */
1335614e71bSYork Sun 	unsigned int odt_wr_cfg = 0; /* ODT for writes configuration */
1345614e71bSYork Sun 	unsigned int ba_bits_cs_n = 0; /* Num of bank bits for SDRAM on CSn */
1355614e71bSYork Sun 	unsigned int row_bits_cs_n = 0; /* Num of row bits for SDRAM on CSn */
1365614e71bSYork Sun 	unsigned int col_bits_cs_n = 0; /* Num of ocl bits for SDRAM on CSn */
1375614e71bSYork Sun 	int go_config = 0;
1385614e71bSYork Sun 
1395614e71bSYork Sun 	/* Compute CS_CONFIG only for existing ranks of each DIMM.  */
1405614e71bSYork Sun 	switch (i) {
1415614e71bSYork Sun 	case 0:
1425614e71bSYork Sun 		if (dimm_params[dimm_number].n_ranks > 0) {
1435614e71bSYork Sun 			go_config = 1;
1445614e71bSYork Sun 			/* These fields only available in CS0_CONFIG */
1455614e71bSYork Sun 			if (!popts->memctl_interleaving)
1465614e71bSYork Sun 				break;
1475614e71bSYork Sun 			switch (popts->memctl_interleaving_mode) {
1485614e71bSYork Sun 			case FSL_DDR_CACHE_LINE_INTERLEAVING:
1495614e71bSYork Sun 			case FSL_DDR_PAGE_INTERLEAVING:
1505614e71bSYork Sun 			case FSL_DDR_BANK_INTERLEAVING:
1515614e71bSYork Sun 			case FSL_DDR_SUPERBANK_INTERLEAVING:
1525614e71bSYork Sun 				intlv_en = popts->memctl_interleaving;
1535614e71bSYork Sun 				intlv_ctl = popts->memctl_interleaving_mode;
1545614e71bSYork Sun 				break;
1555614e71bSYork Sun 			default:
1565614e71bSYork Sun 				break;
1575614e71bSYork Sun 			}
1585614e71bSYork Sun 		}
1595614e71bSYork Sun 		break;
1605614e71bSYork Sun 	case 1:
1615614e71bSYork Sun 		if ((dimm_number == 0 && dimm_params[0].n_ranks > 1) || \
1625614e71bSYork Sun 		    (dimm_number == 1 && dimm_params[1].n_ranks > 0))
1635614e71bSYork Sun 			go_config = 1;
1645614e71bSYork Sun 		break;
1655614e71bSYork Sun 	case 2:
1665614e71bSYork Sun 		if ((dimm_number == 0 && dimm_params[0].n_ranks > 2) || \
1675614e71bSYork Sun 		   (dimm_number >= 1 && dimm_params[dimm_number].n_ranks > 0))
1685614e71bSYork Sun 			go_config = 1;
1695614e71bSYork Sun 		break;
1705614e71bSYork Sun 	case 3:
1715614e71bSYork Sun 		if ((dimm_number == 0 && dimm_params[0].n_ranks > 3) || \
1725614e71bSYork Sun 		    (dimm_number == 1 && dimm_params[1].n_ranks > 1) || \
1735614e71bSYork Sun 		    (dimm_number == 3 && dimm_params[3].n_ranks > 0))
1745614e71bSYork Sun 			go_config = 1;
1755614e71bSYork Sun 		break;
1765614e71bSYork Sun 	default:
1775614e71bSYork Sun 		break;
1785614e71bSYork Sun 	}
1795614e71bSYork Sun 	if (go_config) {
1805614e71bSYork Sun 		unsigned int n_banks_per_sdram_device;
1815614e71bSYork Sun 		cs_n_en = 1;
1825614e71bSYork Sun 		ap_n_en = popts->cs_local_opts[i].auto_precharge;
1835614e71bSYork Sun 		odt_rd_cfg = popts->cs_local_opts[i].odt_rd_cfg;
1845614e71bSYork Sun 		odt_wr_cfg = popts->cs_local_opts[i].odt_wr_cfg;
1855614e71bSYork Sun 		n_banks_per_sdram_device
1865614e71bSYork Sun 			= dimm_params[dimm_number].n_banks_per_sdram_device;
1875614e71bSYork Sun 		ba_bits_cs_n = __ilog2(n_banks_per_sdram_device) - 2;
1885614e71bSYork Sun 		row_bits_cs_n = dimm_params[dimm_number].n_row_addr - 12;
1895614e71bSYork Sun 		col_bits_cs_n = dimm_params[dimm_number].n_col_addr - 8;
1905614e71bSYork Sun 	}
1915614e71bSYork Sun 	ddr->cs[i].config = (0
1925614e71bSYork Sun 		| ((cs_n_en & 0x1) << 31)
1935614e71bSYork Sun 		| ((intlv_en & 0x3) << 29)
1945614e71bSYork Sun 		| ((intlv_ctl & 0xf) << 24)
1955614e71bSYork Sun 		| ((ap_n_en & 0x1) << 23)
1965614e71bSYork Sun 
1975614e71bSYork Sun 		/* XXX: some implementation only have 1 bit starting at left */
1985614e71bSYork Sun 		| ((odt_rd_cfg & 0x7) << 20)
1995614e71bSYork Sun 
2005614e71bSYork Sun 		/* XXX: Some implementation only have 1 bit starting at left */
2015614e71bSYork Sun 		| ((odt_wr_cfg & 0x7) << 16)
2025614e71bSYork Sun 
2035614e71bSYork Sun 		| ((ba_bits_cs_n & 0x3) << 14)
2045614e71bSYork Sun 		| ((row_bits_cs_n & 0x7) << 8)
2055614e71bSYork Sun 		| ((col_bits_cs_n & 0x7) << 0)
2065614e71bSYork Sun 		);
2075614e71bSYork Sun 	debug("FSLDDR: cs[%d]_config = 0x%08x\n", i,ddr->cs[i].config);
2085614e71bSYork Sun }
2095614e71bSYork Sun 
2105614e71bSYork Sun /* Chip Select Configuration 2 (CSn_CONFIG_2) */
2115614e71bSYork Sun /* FIXME: 8572 */
2125614e71bSYork Sun static void set_csn_config_2(int i, fsl_ddr_cfg_regs_t *ddr)
2135614e71bSYork Sun {
2145614e71bSYork Sun 	unsigned int pasr_cfg = 0;	/* Partial array self refresh config */
2155614e71bSYork Sun 
2165614e71bSYork Sun 	ddr->cs[i].config_2 = ((pasr_cfg & 7) << 24);
2175614e71bSYork Sun 	debug("FSLDDR: cs[%d]_config_2 = 0x%08x\n", i, ddr->cs[i].config_2);
2185614e71bSYork Sun }
2195614e71bSYork Sun 
2205614e71bSYork Sun /* -3E = 667 CL5, -25 = CL6 800, -25E = CL5 800 */
2215614e71bSYork Sun 
2225614e71bSYork Sun #if !defined(CONFIG_SYS_FSL_DDR1)
2235614e71bSYork Sun static inline int avoid_odt_overlap(const dimm_params_t *dimm_params)
2245614e71bSYork Sun {
2255614e71bSYork Sun #if CONFIG_DIMM_SLOTS_PER_CTLR == 1
2265614e71bSYork Sun 	if (dimm_params[0].n_ranks == 4)
2275614e71bSYork Sun 		return 1;
2285614e71bSYork Sun #endif
2295614e71bSYork Sun 
2305614e71bSYork Sun #if CONFIG_DIMM_SLOTS_PER_CTLR == 2
2315614e71bSYork Sun 	if ((dimm_params[0].n_ranks == 2) &&
2325614e71bSYork Sun 		(dimm_params[1].n_ranks == 2))
2335614e71bSYork Sun 		return 1;
2345614e71bSYork Sun 
2355614e71bSYork Sun #ifdef CONFIG_FSL_DDR_FIRST_SLOT_QUAD_CAPABLE
2365614e71bSYork Sun 	if (dimm_params[0].n_ranks == 4)
2375614e71bSYork Sun 		return 1;
2385614e71bSYork Sun #endif
2395614e71bSYork Sun #endif
2405614e71bSYork Sun 	return 0;
2415614e71bSYork Sun }
2425614e71bSYork Sun 
2435614e71bSYork Sun /*
2445614e71bSYork Sun  * DDR SDRAM Timing Configuration 0 (TIMING_CFG_0)
2455614e71bSYork Sun  *
2465614e71bSYork Sun  * Avoid writing for DDR I.  The new PQ38 DDR controller
2475614e71bSYork Sun  * dreams up non-zero default values to be backwards compatible.
2485614e71bSYork Sun  */
2495614e71bSYork Sun static void set_timing_cfg_0(fsl_ddr_cfg_regs_t *ddr,
2505614e71bSYork Sun 				const memctl_options_t *popts,
2515614e71bSYork Sun 				const dimm_params_t *dimm_params)
2525614e71bSYork Sun {
2535614e71bSYork Sun 	unsigned char trwt_mclk = 0;   /* Read-to-write turnaround */
2545614e71bSYork Sun 	unsigned char twrt_mclk = 0;   /* Write-to-read turnaround */
2555614e71bSYork Sun 	/* 7.5 ns on -3E; 0 means WL - CL + BL/2 + 1 */
2565614e71bSYork Sun 	unsigned char trrt_mclk = 0;   /* Read-to-read turnaround */
2575614e71bSYork Sun 	unsigned char twwt_mclk = 0;   /* Write-to-write turnaround */
2585614e71bSYork Sun 
2595614e71bSYork Sun 	/* Active powerdown exit timing (tXARD and tXARDS). */
2605614e71bSYork Sun 	unsigned char act_pd_exit_mclk;
2615614e71bSYork Sun 	/* Precharge powerdown exit timing (tXP). */
2625614e71bSYork Sun 	unsigned char pre_pd_exit_mclk;
2635614e71bSYork Sun 	/* ODT powerdown exit timing (tAXPD). */
2645614e71bSYork Sun 	unsigned char taxpd_mclk;
2655614e71bSYork Sun 	/* Mode register set cycle time (tMRD). */
2665614e71bSYork Sun 	unsigned char tmrd_mclk;
2675614e71bSYork Sun 
2685614e71bSYork Sun #ifdef CONFIG_SYS_FSL_DDR3
2695614e71bSYork Sun 	/*
2705614e71bSYork Sun 	 * (tXARD and tXARDS). Empirical?
2715614e71bSYork Sun 	 * The DDR3 spec has not tXARD,
2725614e71bSYork Sun 	 * we use the tXP instead of it.
2735614e71bSYork Sun 	 * tXP=max(3nCK, 7.5ns) for DDR3.
2745614e71bSYork Sun 	 * spec has not the tAXPD, we use
2755614e71bSYork Sun 	 * tAXPD=1, need design to confirm.
2765614e71bSYork Sun 	 */
2775614e71bSYork Sun 	int tXP = max((get_memory_clk_period_ps() * 3), 7500); /* unit=ps */
2785614e71bSYork Sun 	unsigned int data_rate = get_ddr_freq(0);
2795614e71bSYork Sun 	tmrd_mclk = 4;
2805614e71bSYork Sun 	/* set the turnaround time */
2815614e71bSYork Sun 
2825614e71bSYork Sun 	/*
2835614e71bSYork Sun 	 * for single quad-rank DIMM and two dual-rank DIMMs
2845614e71bSYork Sun 	 * to avoid ODT overlap
2855614e71bSYork Sun 	 */
2865614e71bSYork Sun 	if (avoid_odt_overlap(dimm_params)) {
2875614e71bSYork Sun 		twwt_mclk = 2;
2885614e71bSYork Sun 		trrt_mclk = 1;
2895614e71bSYork Sun 	}
2905614e71bSYork Sun 	/* for faster clock, need more time for data setup */
2915614e71bSYork Sun 	trwt_mclk = (data_rate/1000000 > 1800) ? 2 : 1;
2925614e71bSYork Sun 
2935614e71bSYork Sun 	if ((data_rate/1000000 > 1150) || (popts->memctl_interleaving))
2945614e71bSYork Sun 		twrt_mclk = 1;
2955614e71bSYork Sun 
2965614e71bSYork Sun 	if (popts->dynamic_power == 0) {	/* powerdown is not used */
2975614e71bSYork Sun 		act_pd_exit_mclk = 1;
2985614e71bSYork Sun 		pre_pd_exit_mclk = 1;
2995614e71bSYork Sun 		taxpd_mclk = 1;
3005614e71bSYork Sun 	} else {
3015614e71bSYork Sun 		/* act_pd_exit_mclk = tXARD, see above */
3025614e71bSYork Sun 		act_pd_exit_mclk = picos_to_mclk(tXP);
3035614e71bSYork Sun 		/* Mode register MR0[A12] is '1' - fast exit */
3045614e71bSYork Sun 		pre_pd_exit_mclk = act_pd_exit_mclk;
3055614e71bSYork Sun 		taxpd_mclk = 1;
3065614e71bSYork Sun 	}
3075614e71bSYork Sun #else /* CONFIG_SYS_FSL_DDR2 */
3085614e71bSYork Sun 	/*
3095614e71bSYork Sun 	 * (tXARD and tXARDS). Empirical?
3105614e71bSYork Sun 	 * tXARD = 2 for DDR2
3115614e71bSYork Sun 	 * tXP=2
3125614e71bSYork Sun 	 * tAXPD=8
3135614e71bSYork Sun 	 */
3145614e71bSYork Sun 	act_pd_exit_mclk = 2;
3155614e71bSYork Sun 	pre_pd_exit_mclk = 2;
3165614e71bSYork Sun 	taxpd_mclk = 8;
3175614e71bSYork Sun 	tmrd_mclk = 2;
3185614e71bSYork Sun #endif
3195614e71bSYork Sun 
3205614e71bSYork Sun 	if (popts->trwt_override)
3215614e71bSYork Sun 		trwt_mclk = popts->trwt;
3225614e71bSYork Sun 
3235614e71bSYork Sun 	ddr->timing_cfg_0 = (0
3245614e71bSYork Sun 		| ((trwt_mclk & 0x3) << 30)	/* RWT */
3255614e71bSYork Sun 		| ((twrt_mclk & 0x3) << 28)	/* WRT */
3265614e71bSYork Sun 		| ((trrt_mclk & 0x3) << 26)	/* RRT */
3275614e71bSYork Sun 		| ((twwt_mclk & 0x3) << 24)	/* WWT */
328*d4263b8aSYork Sun 		| ((act_pd_exit_mclk & 0xf) << 20)  /* ACT_PD_EXIT */
3295614e71bSYork Sun 		| ((pre_pd_exit_mclk & 0xF) << 16)  /* PRE_PD_EXIT */
3305614e71bSYork Sun 		| ((taxpd_mclk & 0xf) << 8)	/* ODT_PD_EXIT */
331*d4263b8aSYork Sun 		| ((tmrd_mclk & 0x1f) << 0)	/* MRS_CYC */
3325614e71bSYork Sun 		);
3335614e71bSYork Sun 	debug("FSLDDR: timing_cfg_0 = 0x%08x\n", ddr->timing_cfg_0);
3345614e71bSYork Sun }
3355614e71bSYork Sun #endif	/* defined(CONFIG_SYS_FSL_DDR2) */
3365614e71bSYork Sun 
3375614e71bSYork Sun /* DDR SDRAM Timing Configuration 3 (TIMING_CFG_3) */
3385614e71bSYork Sun static void set_timing_cfg_3(fsl_ddr_cfg_regs_t *ddr,
3395614e71bSYork Sun 			       const memctl_options_t *popts,
3405614e71bSYork Sun 			       const common_timing_params_t *common_dimm,
341*d4263b8aSYork Sun 			       unsigned int cas_latency,
342*d4263b8aSYork Sun 			       unsigned int additive_latency)
3435614e71bSYork Sun {
3445614e71bSYork Sun 	/* Extended precharge to activate interval (tRP) */
3455614e71bSYork Sun 	unsigned int ext_pretoact = 0;
3465614e71bSYork Sun 	/* Extended Activate to precharge interval (tRAS) */
3475614e71bSYork Sun 	unsigned int ext_acttopre = 0;
3485614e71bSYork Sun 	/* Extended activate to read/write interval (tRCD) */
3495614e71bSYork Sun 	unsigned int ext_acttorw = 0;
3505614e71bSYork Sun 	/* Extended refresh recovery time (tRFC) */
3515614e71bSYork Sun 	unsigned int ext_refrec;
3525614e71bSYork Sun 	/* Extended MCAS latency from READ cmd */
3535614e71bSYork Sun 	unsigned int ext_caslat = 0;
354*d4263b8aSYork Sun 	/* Extended additive latency */
355*d4263b8aSYork Sun 	unsigned int ext_add_lat = 0;
3565614e71bSYork Sun 	/* Extended last data to precharge interval (tWR) */
3575614e71bSYork Sun 	unsigned int ext_wrrec = 0;
3585614e71bSYork Sun 	/* Control Adjust */
3595614e71bSYork Sun 	unsigned int cntl_adj = 0;
3605614e71bSYork Sun 
3615614e71bSYork Sun 	ext_pretoact = picos_to_mclk(common_dimm->trp_ps) >> 4;
3625614e71bSYork Sun 	ext_acttopre = picos_to_mclk(common_dimm->tras_ps) >> 4;
3635614e71bSYork Sun 	ext_acttorw = picos_to_mclk(common_dimm->trcd_ps) >> 4;
3645614e71bSYork Sun 	ext_caslat = (2 * cas_latency - 1) >> 4;
365*d4263b8aSYork Sun 	ext_add_lat = additive_latency >> 4;
3665614e71bSYork Sun 	ext_refrec = (picos_to_mclk(common_dimm->trfc_ps) - 8) >> 4;
3675614e71bSYork Sun 	/* ext_wrrec only deals with 16 clock and above, or 14 with OTF */
3685614e71bSYork Sun 	ext_wrrec = (picos_to_mclk(common_dimm->twr_ps) +
3695614e71bSYork Sun 		(popts->otf_burst_chop_en ? 2 : 0)) >> 4;
3705614e71bSYork Sun 
3715614e71bSYork Sun 	ddr->timing_cfg_3 = (0
3725614e71bSYork Sun 		| ((ext_pretoact & 0x1) << 28)
3735614e71bSYork Sun 		| ((ext_acttopre & 0x3) << 24)
3745614e71bSYork Sun 		| ((ext_acttorw & 0x1) << 22)
3755614e71bSYork Sun 		| ((ext_refrec & 0x1F) << 16)
3765614e71bSYork Sun 		| ((ext_caslat & 0x3) << 12)
377*d4263b8aSYork Sun 		| ((ext_add_lat & 0x1) << 10)
3785614e71bSYork Sun 		| ((ext_wrrec & 0x1) << 8)
3795614e71bSYork Sun 		| ((cntl_adj & 0x7) << 0)
3805614e71bSYork Sun 		);
3815614e71bSYork Sun 	debug("FSLDDR: timing_cfg_3 = 0x%08x\n", ddr->timing_cfg_3);
3825614e71bSYork Sun }
3835614e71bSYork Sun 
3845614e71bSYork Sun /* DDR SDRAM Timing Configuration 1 (TIMING_CFG_1) */
3855614e71bSYork Sun static void set_timing_cfg_1(fsl_ddr_cfg_regs_t *ddr,
3865614e71bSYork Sun 			       const memctl_options_t *popts,
3875614e71bSYork Sun 			       const common_timing_params_t *common_dimm,
3885614e71bSYork Sun 			       unsigned int cas_latency)
3895614e71bSYork Sun {
3905614e71bSYork Sun 	/* Precharge-to-activate interval (tRP) */
3915614e71bSYork Sun 	unsigned char pretoact_mclk;
3925614e71bSYork Sun 	/* Activate to precharge interval (tRAS) */
3935614e71bSYork Sun 	unsigned char acttopre_mclk;
3945614e71bSYork Sun 	/*  Activate to read/write interval (tRCD) */
3955614e71bSYork Sun 	unsigned char acttorw_mclk;
3965614e71bSYork Sun 	/* CASLAT */
3975614e71bSYork Sun 	unsigned char caslat_ctrl;
3985614e71bSYork Sun 	/*  Refresh recovery time (tRFC) ; trfc_low */
3995614e71bSYork Sun 	unsigned char refrec_ctrl;
4005614e71bSYork Sun 	/* Last data to precharge minimum interval (tWR) */
4015614e71bSYork Sun 	unsigned char wrrec_mclk;
4025614e71bSYork Sun 	/* Activate-to-activate interval (tRRD) */
4035614e71bSYork Sun 	unsigned char acttoact_mclk;
4045614e71bSYork Sun 	/* Last write data pair to read command issue interval (tWTR) */
4055614e71bSYork Sun 	unsigned char wrtord_mclk;
4065614e71bSYork Sun 	/* DDR_SDRAM_MODE doesn't support 9,11,13,15 */
4075614e71bSYork Sun 	static const u8 wrrec_table[] = {
4085614e71bSYork Sun 		1, 2, 3, 4, 5, 6, 7, 8, 10, 10, 12, 12, 14, 14, 0, 0};
4095614e71bSYork Sun 
4105614e71bSYork Sun 	pretoact_mclk = picos_to_mclk(common_dimm->trp_ps);
4115614e71bSYork Sun 	acttopre_mclk = picos_to_mclk(common_dimm->tras_ps);
4125614e71bSYork Sun 	acttorw_mclk = picos_to_mclk(common_dimm->trcd_ps);
4135614e71bSYork Sun 
4145614e71bSYork Sun 	/*
4155614e71bSYork Sun 	 * Translate CAS Latency to a DDR controller field value:
4165614e71bSYork Sun 	 *
4175614e71bSYork Sun 	 *      CAS Lat DDR I   DDR II  Ctrl
4185614e71bSYork Sun 	 *      Clocks  SPD Bit SPD Bit Value
4195614e71bSYork Sun 	 *      ------- ------- ------- -----
4205614e71bSYork Sun 	 *      1.0     0               0001
4215614e71bSYork Sun 	 *      1.5     1               0010
4225614e71bSYork Sun 	 *      2.0     2       2       0011
4235614e71bSYork Sun 	 *      2.5     3               0100
4245614e71bSYork Sun 	 *      3.0     4       3       0101
4255614e71bSYork Sun 	 *      3.5     5               0110
4265614e71bSYork Sun 	 *      4.0             4       0111
4275614e71bSYork Sun 	 *      4.5                     1000
4285614e71bSYork Sun 	 *      5.0             5       1001
4295614e71bSYork Sun 	 */
4305614e71bSYork Sun #if defined(CONFIG_SYS_FSL_DDR1)
4315614e71bSYork Sun 	caslat_ctrl = (cas_latency + 1) & 0x07;
4325614e71bSYork Sun #elif defined(CONFIG_SYS_FSL_DDR2)
4335614e71bSYork Sun 	caslat_ctrl = 2 * cas_latency - 1;
4345614e71bSYork Sun #else
4355614e71bSYork Sun 	/*
4365614e71bSYork Sun 	 * if the CAS latency more than 8 cycle,
4375614e71bSYork Sun 	 * we need set extend bit for it at
4385614e71bSYork Sun 	 * TIMING_CFG_3[EXT_CASLAT]
4395614e71bSYork Sun 	 */
4405614e71bSYork Sun 	caslat_ctrl = 2 * cas_latency - 1;
4415614e71bSYork Sun #endif
4425614e71bSYork Sun 
4435614e71bSYork Sun 	refrec_ctrl = picos_to_mclk(common_dimm->trfc_ps) - 8;
4445614e71bSYork Sun 	wrrec_mclk = picos_to_mclk(common_dimm->twr_ps);
4455614e71bSYork Sun 
4465614e71bSYork Sun 	if (wrrec_mclk > 16)
4475614e71bSYork Sun 		printf("Error: WRREC doesn't support more than 16 clocks\n");
4485614e71bSYork Sun 	else
4495614e71bSYork Sun 		wrrec_mclk = wrrec_table[wrrec_mclk - 1];
4505614e71bSYork Sun 	if (popts->otf_burst_chop_en)
4515614e71bSYork Sun 		wrrec_mclk += 2;
4525614e71bSYork Sun 
4535614e71bSYork Sun 	acttoact_mclk = picos_to_mclk(common_dimm->trrd_ps);
4545614e71bSYork Sun 	/*
4555614e71bSYork Sun 	 * JEDEC has min requirement for tRRD
4565614e71bSYork Sun 	 */
4575614e71bSYork Sun #if defined(CONFIG_SYS_FSL_DDR3)
4585614e71bSYork Sun 	if (acttoact_mclk < 4)
4595614e71bSYork Sun 		acttoact_mclk = 4;
4605614e71bSYork Sun #endif
4615614e71bSYork Sun 	wrtord_mclk = picos_to_mclk(common_dimm->twtr_ps);
4625614e71bSYork Sun 	/*
4635614e71bSYork Sun 	 * JEDEC has some min requirements for tWTR
4645614e71bSYork Sun 	 */
4655614e71bSYork Sun #if defined(CONFIG_SYS_FSL_DDR2)
4665614e71bSYork Sun 	if (wrtord_mclk < 2)
4675614e71bSYork Sun 		wrtord_mclk = 2;
4685614e71bSYork Sun #elif defined(CONFIG_SYS_FSL_DDR3)
4695614e71bSYork Sun 	if (wrtord_mclk < 4)
4705614e71bSYork Sun 		wrtord_mclk = 4;
4715614e71bSYork Sun #endif
4725614e71bSYork Sun 	if (popts->otf_burst_chop_en)
4735614e71bSYork Sun 		wrtord_mclk += 2;
4745614e71bSYork Sun 
4755614e71bSYork Sun 	ddr->timing_cfg_1 = (0
4765614e71bSYork Sun 		| ((pretoact_mclk & 0x0F) << 28)
4775614e71bSYork Sun 		| ((acttopre_mclk & 0x0F) << 24)
4785614e71bSYork Sun 		| ((acttorw_mclk & 0xF) << 20)
4795614e71bSYork Sun 		| ((caslat_ctrl & 0xF) << 16)
4805614e71bSYork Sun 		| ((refrec_ctrl & 0xF) << 12)
4815614e71bSYork Sun 		| ((wrrec_mclk & 0x0F) << 8)
4825614e71bSYork Sun 		| ((acttoact_mclk & 0x0F) << 4)
4835614e71bSYork Sun 		| ((wrtord_mclk & 0x0F) << 0)
4845614e71bSYork Sun 		);
4855614e71bSYork Sun 	debug("FSLDDR: timing_cfg_1 = 0x%08x\n", ddr->timing_cfg_1);
4865614e71bSYork Sun }
4875614e71bSYork Sun 
4885614e71bSYork Sun /* DDR SDRAM Timing Configuration 2 (TIMING_CFG_2) */
4895614e71bSYork Sun static void set_timing_cfg_2(fsl_ddr_cfg_regs_t *ddr,
4905614e71bSYork Sun 			       const memctl_options_t *popts,
4915614e71bSYork Sun 			       const common_timing_params_t *common_dimm,
4925614e71bSYork Sun 			       unsigned int cas_latency,
4935614e71bSYork Sun 			       unsigned int additive_latency)
4945614e71bSYork Sun {
4955614e71bSYork Sun 	/* Additive latency */
4965614e71bSYork Sun 	unsigned char add_lat_mclk;
4975614e71bSYork Sun 	/* CAS-to-preamble override */
4985614e71bSYork Sun 	unsigned short cpo;
4995614e71bSYork Sun 	/* Write latency */
5005614e71bSYork Sun 	unsigned char wr_lat;
5015614e71bSYork Sun 	/*  Read to precharge (tRTP) */
5025614e71bSYork Sun 	unsigned char rd_to_pre;
5035614e71bSYork Sun 	/* Write command to write data strobe timing adjustment */
5045614e71bSYork Sun 	unsigned char wr_data_delay;
5055614e71bSYork Sun 	/* Minimum CKE pulse width (tCKE) */
5065614e71bSYork Sun 	unsigned char cke_pls;
5075614e71bSYork Sun 	/* Window for four activates (tFAW) */
5085614e71bSYork Sun 	unsigned short four_act;
5095614e71bSYork Sun 
5105614e71bSYork Sun 	/* FIXME add check that this must be less than acttorw_mclk */
5115614e71bSYork Sun 	add_lat_mclk = additive_latency;
5125614e71bSYork Sun 	cpo = popts->cpo_override;
5135614e71bSYork Sun 
5145614e71bSYork Sun #if defined(CONFIG_SYS_FSL_DDR1)
5155614e71bSYork Sun 	/*
5165614e71bSYork Sun 	 * This is a lie.  It should really be 1, but if it is
5175614e71bSYork Sun 	 * set to 1, bits overlap into the old controller's
5185614e71bSYork Sun 	 * otherwise unused ACSM field.  If we leave it 0, then
5195614e71bSYork Sun 	 * the HW will magically treat it as 1 for DDR 1.  Oh Yea.
5205614e71bSYork Sun 	 */
5215614e71bSYork Sun 	wr_lat = 0;
5225614e71bSYork Sun #elif defined(CONFIG_SYS_FSL_DDR2)
5235614e71bSYork Sun 	wr_lat = cas_latency - 1;
5245614e71bSYork Sun #else
5255614e71bSYork Sun 	wr_lat = compute_cas_write_latency();
5265614e71bSYork Sun #endif
5275614e71bSYork Sun 
5285614e71bSYork Sun 	rd_to_pre = picos_to_mclk(common_dimm->trtp_ps);
5295614e71bSYork Sun 	/*
5305614e71bSYork Sun 	 * JEDEC has some min requirements for tRTP
5315614e71bSYork Sun 	 */
5325614e71bSYork Sun #if defined(CONFIG_SYS_FSL_DDR2)
5335614e71bSYork Sun 	if (rd_to_pre  < 2)
5345614e71bSYork Sun 		rd_to_pre  = 2;
5355614e71bSYork Sun #elif defined(CONFIG_SYS_FSL_DDR3)
5365614e71bSYork Sun 	if (rd_to_pre < 4)
5375614e71bSYork Sun 		rd_to_pre = 4;
5385614e71bSYork Sun #endif
5395614e71bSYork Sun 	if (popts->otf_burst_chop_en)
5405614e71bSYork Sun 		rd_to_pre += 2; /* according to UM */
5415614e71bSYork Sun 
5425614e71bSYork Sun 	wr_data_delay = popts->write_data_delay;
5435614e71bSYork Sun 	cke_pls = picos_to_mclk(popts->tcke_clock_pulse_width_ps);
5445614e71bSYork Sun 	four_act = picos_to_mclk(popts->tfaw_window_four_activates_ps);
5455614e71bSYork Sun 
5465614e71bSYork Sun 	ddr->timing_cfg_2 = (0
5475614e71bSYork Sun 		| ((add_lat_mclk & 0xf) << 28)
5485614e71bSYork Sun 		| ((cpo & 0x1f) << 23)
5495614e71bSYork Sun 		| ((wr_lat & 0xf) << 19)
5505614e71bSYork Sun 		| ((rd_to_pre & RD_TO_PRE_MASK) << RD_TO_PRE_SHIFT)
5515614e71bSYork Sun 		| ((wr_data_delay & WR_DATA_DELAY_MASK) << WR_DATA_DELAY_SHIFT)
5525614e71bSYork Sun 		| ((cke_pls & 0x7) << 6)
5535614e71bSYork Sun 		| ((four_act & 0x3f) << 0)
5545614e71bSYork Sun 		);
5555614e71bSYork Sun 	debug("FSLDDR: timing_cfg_2 = 0x%08x\n", ddr->timing_cfg_2);
5565614e71bSYork Sun }
5575614e71bSYork Sun 
5585614e71bSYork Sun /* DDR SDRAM Register Control Word */
5595614e71bSYork Sun static void set_ddr_sdram_rcw(fsl_ddr_cfg_regs_t *ddr,
5605614e71bSYork Sun 			       const memctl_options_t *popts,
5615614e71bSYork Sun 			       const common_timing_params_t *common_dimm)
5625614e71bSYork Sun {
5635614e71bSYork Sun 	if (common_dimm->all_dimms_registered &&
5645614e71bSYork Sun 	    !common_dimm->all_dimms_unbuffered)	{
5655614e71bSYork Sun 		if (popts->rcw_override) {
5665614e71bSYork Sun 			ddr->ddr_sdram_rcw_1 = popts->rcw_1;
5675614e71bSYork Sun 			ddr->ddr_sdram_rcw_2 = popts->rcw_2;
5685614e71bSYork Sun 		} else {
5695614e71bSYork Sun 			ddr->ddr_sdram_rcw_1 =
5705614e71bSYork Sun 				common_dimm->rcw[0] << 28 | \
5715614e71bSYork Sun 				common_dimm->rcw[1] << 24 | \
5725614e71bSYork Sun 				common_dimm->rcw[2] << 20 | \
5735614e71bSYork Sun 				common_dimm->rcw[3] << 16 | \
5745614e71bSYork Sun 				common_dimm->rcw[4] << 12 | \
5755614e71bSYork Sun 				common_dimm->rcw[5] << 8 | \
5765614e71bSYork Sun 				common_dimm->rcw[6] << 4 | \
5775614e71bSYork Sun 				common_dimm->rcw[7];
5785614e71bSYork Sun 			ddr->ddr_sdram_rcw_2 =
5795614e71bSYork Sun 				common_dimm->rcw[8] << 28 | \
5805614e71bSYork Sun 				common_dimm->rcw[9] << 24 | \
5815614e71bSYork Sun 				common_dimm->rcw[10] << 20 | \
5825614e71bSYork Sun 				common_dimm->rcw[11] << 16 | \
5835614e71bSYork Sun 				common_dimm->rcw[12] << 12 | \
5845614e71bSYork Sun 				common_dimm->rcw[13] << 8 | \
5855614e71bSYork Sun 				common_dimm->rcw[14] << 4 | \
5865614e71bSYork Sun 				common_dimm->rcw[15];
5875614e71bSYork Sun 		}
5885614e71bSYork Sun 		debug("FSLDDR: ddr_sdram_rcw_1 = 0x%08x\n", ddr->ddr_sdram_rcw_1);
5895614e71bSYork Sun 		debug("FSLDDR: ddr_sdram_rcw_2 = 0x%08x\n", ddr->ddr_sdram_rcw_2);
5905614e71bSYork Sun 	}
5915614e71bSYork Sun }
5925614e71bSYork Sun 
5935614e71bSYork Sun /* DDR SDRAM control configuration (DDR_SDRAM_CFG) */
5945614e71bSYork Sun static void set_ddr_sdram_cfg(fsl_ddr_cfg_regs_t *ddr,
5955614e71bSYork Sun 			       const memctl_options_t *popts,
5965614e71bSYork Sun 			       const common_timing_params_t *common_dimm)
5975614e71bSYork Sun {
5985614e71bSYork Sun 	unsigned int mem_en;		/* DDR SDRAM interface logic enable */
5995614e71bSYork Sun 	unsigned int sren;		/* Self refresh enable (during sleep) */
6005614e71bSYork Sun 	unsigned int ecc_en;		/* ECC enable. */
6015614e71bSYork Sun 	unsigned int rd_en;		/* Registered DIMM enable */
6025614e71bSYork Sun 	unsigned int sdram_type;	/* Type of SDRAM */
6035614e71bSYork Sun 	unsigned int dyn_pwr;		/* Dynamic power management mode */
6045614e71bSYork Sun 	unsigned int dbw;		/* DRAM dta bus width */
6055614e71bSYork Sun 	unsigned int eight_be = 0;	/* 8-beat burst enable, DDR2 is zero */
6065614e71bSYork Sun 	unsigned int ncap = 0;		/* Non-concurrent auto-precharge */
6075614e71bSYork Sun 	unsigned int threet_en;		/* Enable 3T timing */
6085614e71bSYork Sun 	unsigned int twot_en;		/* Enable 2T timing */
6095614e71bSYork Sun 	unsigned int ba_intlv_ctl;	/* Bank (CS) interleaving control */
6105614e71bSYork Sun 	unsigned int x32_en = 0;	/* x32 enable */
6115614e71bSYork Sun 	unsigned int pchb8 = 0;		/* precharge bit 8 enable */
6125614e71bSYork Sun 	unsigned int hse;		/* Global half strength override */
6135614e71bSYork Sun 	unsigned int mem_halt = 0;	/* memory controller halt */
6145614e71bSYork Sun 	unsigned int bi = 0;		/* Bypass initialization */
6155614e71bSYork Sun 
6165614e71bSYork Sun 	mem_en = 1;
6175614e71bSYork Sun 	sren = popts->self_refresh_in_sleep;
6185614e71bSYork Sun 	if (common_dimm->all_dimms_ecc_capable) {
6195614e71bSYork Sun 		/* Allow setting of ECC only if all DIMMs are ECC. */
6205614e71bSYork Sun 		ecc_en = popts->ecc_mode;
6215614e71bSYork Sun 	} else {
6225614e71bSYork Sun 		ecc_en = 0;
6235614e71bSYork Sun 	}
6245614e71bSYork Sun 
6255614e71bSYork Sun 	if (common_dimm->all_dimms_registered &&
6265614e71bSYork Sun 	    !common_dimm->all_dimms_unbuffered)	{
6275614e71bSYork Sun 		rd_en = 1;
6285614e71bSYork Sun 		twot_en = 0;
6295614e71bSYork Sun 	} else {
6305614e71bSYork Sun 		rd_en = 0;
6315614e71bSYork Sun 		twot_en = popts->twot_en;
6325614e71bSYork Sun 	}
6335614e71bSYork Sun 
6345614e71bSYork Sun 	sdram_type = CONFIG_FSL_SDRAM_TYPE;
6355614e71bSYork Sun 
6365614e71bSYork Sun 	dyn_pwr = popts->dynamic_power;
6375614e71bSYork Sun 	dbw = popts->data_bus_width;
6385614e71bSYork Sun 	/* 8-beat burst enable DDR-III case
6395614e71bSYork Sun 	 * we must clear it when use the on-the-fly mode,
6405614e71bSYork Sun 	 * must set it when use the 32-bits bus mode.
6415614e71bSYork Sun 	 */
6425614e71bSYork Sun 	if (sdram_type == SDRAM_TYPE_DDR3) {
6435614e71bSYork Sun 		if (popts->burst_length == DDR_BL8)
6445614e71bSYork Sun 			eight_be = 1;
6455614e71bSYork Sun 		if (popts->burst_length == DDR_OTF)
6465614e71bSYork Sun 			eight_be = 0;
6475614e71bSYork Sun 		if (dbw == 0x1)
6485614e71bSYork Sun 			eight_be = 1;
6495614e71bSYork Sun 	}
6505614e71bSYork Sun 
6515614e71bSYork Sun 	threet_en = popts->threet_en;
6525614e71bSYork Sun 	ba_intlv_ctl = popts->ba_intlv_ctl;
6535614e71bSYork Sun 	hse = popts->half_strength_driver_enable;
6545614e71bSYork Sun 
6555614e71bSYork Sun 	ddr->ddr_sdram_cfg = (0
6565614e71bSYork Sun 			| ((mem_en & 0x1) << 31)
6575614e71bSYork Sun 			| ((sren & 0x1) << 30)
6585614e71bSYork Sun 			| ((ecc_en & 0x1) << 29)
6595614e71bSYork Sun 			| ((rd_en & 0x1) << 28)
6605614e71bSYork Sun 			| ((sdram_type & 0x7) << 24)
6615614e71bSYork Sun 			| ((dyn_pwr & 0x1) << 21)
6625614e71bSYork Sun 			| ((dbw & 0x3) << 19)
6635614e71bSYork Sun 			| ((eight_be & 0x1) << 18)
6645614e71bSYork Sun 			| ((ncap & 0x1) << 17)
6655614e71bSYork Sun 			| ((threet_en & 0x1) << 16)
6665614e71bSYork Sun 			| ((twot_en & 0x1) << 15)
6675614e71bSYork Sun 			| ((ba_intlv_ctl & 0x7F) << 8)
6685614e71bSYork Sun 			| ((x32_en & 0x1) << 5)
6695614e71bSYork Sun 			| ((pchb8 & 0x1) << 4)
6705614e71bSYork Sun 			| ((hse & 0x1) << 3)
6715614e71bSYork Sun 			| ((mem_halt & 0x1) << 1)
6725614e71bSYork Sun 			| ((bi & 0x1) << 0)
6735614e71bSYork Sun 			);
6745614e71bSYork Sun 	debug("FSLDDR: ddr_sdram_cfg = 0x%08x\n", ddr->ddr_sdram_cfg);
6755614e71bSYork Sun }
6765614e71bSYork Sun 
6775614e71bSYork Sun /* DDR SDRAM control configuration 2 (DDR_SDRAM_CFG_2) */
6785614e71bSYork Sun static void set_ddr_sdram_cfg_2(fsl_ddr_cfg_regs_t *ddr,
6795614e71bSYork Sun 			       const memctl_options_t *popts,
6805614e71bSYork Sun 			       const unsigned int unq_mrs_en)
6815614e71bSYork Sun {
6825614e71bSYork Sun 	unsigned int frc_sr = 0;	/* Force self refresh */
6835614e71bSYork Sun 	unsigned int sr_ie = 0;		/* Self-refresh interrupt enable */
6845614e71bSYork Sun 	unsigned int dll_rst_dis;	/* DLL reset disable */
6855614e71bSYork Sun 	unsigned int dqs_cfg;		/* DQS configuration */
6865614e71bSYork Sun 	unsigned int odt_cfg = 0;	/* ODT configuration */
6875614e71bSYork Sun 	unsigned int num_pr;		/* Number of posted refreshes */
6885614e71bSYork Sun 	unsigned int slow = 0;		/* DDR will be run less than 1250 */
6895614e71bSYork Sun 	unsigned int x4_en = 0;		/* x4 DRAM enable */
6905614e71bSYork Sun 	unsigned int obc_cfg;		/* On-The-Fly Burst Chop Cfg */
6915614e71bSYork Sun 	unsigned int ap_en;		/* Address Parity Enable */
6925614e71bSYork Sun 	unsigned int d_init;		/* DRAM data initialization */
6935614e71bSYork Sun 	unsigned int rcw_en = 0;	/* Register Control Word Enable */
6945614e71bSYork Sun 	unsigned int md_en = 0;		/* Mirrored DIMM Enable */
6955614e71bSYork Sun 	unsigned int qd_en = 0;		/* quad-rank DIMM Enable */
6965614e71bSYork Sun 	int i;
6975614e71bSYork Sun 
6985614e71bSYork Sun 	dll_rst_dis = 1;	/* Make this configurable */
6995614e71bSYork Sun 	dqs_cfg = popts->dqs_config;
7005614e71bSYork Sun 	for (i = 0; i < CONFIG_CHIP_SELECTS_PER_CTRL; i++) {
7015614e71bSYork Sun 		if (popts->cs_local_opts[i].odt_rd_cfg
7025614e71bSYork Sun 			|| popts->cs_local_opts[i].odt_wr_cfg) {
7035614e71bSYork Sun 			odt_cfg = SDRAM_CFG2_ODT_ONLY_READ;
7045614e71bSYork Sun 			break;
7055614e71bSYork Sun 		}
7065614e71bSYork Sun 	}
7075614e71bSYork Sun 
7085614e71bSYork Sun 	num_pr = 1;	/* Make this configurable */
7095614e71bSYork Sun 
7105614e71bSYork Sun 	/*
7115614e71bSYork Sun 	 * 8572 manual says
7125614e71bSYork Sun 	 *     {TIMING_CFG_1[PRETOACT]
7135614e71bSYork Sun 	 *      + [DDR_SDRAM_CFG_2[NUM_PR]
7145614e71bSYork Sun 	 *        * ({EXT_REFREC || REFREC} + 8 + 2)]}
7155614e71bSYork Sun 	 *      << DDR_SDRAM_INTERVAL[REFINT]
7165614e71bSYork Sun 	 */
7175614e71bSYork Sun #if defined(CONFIG_SYS_FSL_DDR3)
7185614e71bSYork Sun 	obc_cfg = popts->otf_burst_chop_en;
7195614e71bSYork Sun #else
7205614e71bSYork Sun 	obc_cfg = 0;
7215614e71bSYork Sun #endif
7225614e71bSYork Sun 
7235614e71bSYork Sun #if (CONFIG_SYS_FSL_DDR_VER >= FSL_DDR_VER_4_7)
7245614e71bSYork Sun 	slow = get_ddr_freq(0) < 1249000000;
7255614e71bSYork Sun #endif
7265614e71bSYork Sun 
7275614e71bSYork Sun 	if (popts->registered_dimm_en) {
7285614e71bSYork Sun 		rcw_en = 1;
7295614e71bSYork Sun 		ap_en = popts->ap_en;
7305614e71bSYork Sun 	} else {
7315614e71bSYork Sun 		ap_en = 0;
7325614e71bSYork Sun 	}
7335614e71bSYork Sun 
7345614e71bSYork Sun 	x4_en = popts->x4_en ? 1 : 0;
7355614e71bSYork Sun 
7365614e71bSYork Sun #if defined(CONFIG_ECC_INIT_VIA_DDRCONTROLLER)
7375614e71bSYork Sun 	/* Use the DDR controller to auto initialize memory. */
7385614e71bSYork Sun 	d_init = popts->ecc_init_using_memctl;
7395614e71bSYork Sun 	ddr->ddr_data_init = CONFIG_MEM_INIT_VALUE;
7405614e71bSYork Sun 	debug("DDR: ddr_data_init = 0x%08x\n", ddr->ddr_data_init);
7415614e71bSYork Sun #else
7425614e71bSYork Sun 	/* Memory will be initialized via DMA, or not at all. */
7435614e71bSYork Sun 	d_init = 0;
7445614e71bSYork Sun #endif
7455614e71bSYork Sun 
7465614e71bSYork Sun #if defined(CONFIG_SYS_FSL_DDR3)
7475614e71bSYork Sun 	md_en = popts->mirrored_dimm;
7485614e71bSYork Sun #endif
7495614e71bSYork Sun 	qd_en = popts->quad_rank_present ? 1 : 0;
7505614e71bSYork Sun 	ddr->ddr_sdram_cfg_2 = (0
7515614e71bSYork Sun 		| ((frc_sr & 0x1) << 31)
7525614e71bSYork Sun 		| ((sr_ie & 0x1) << 30)
7535614e71bSYork Sun 		| ((dll_rst_dis & 0x1) << 29)
7545614e71bSYork Sun 		| ((dqs_cfg & 0x3) << 26)
7555614e71bSYork Sun 		| ((odt_cfg & 0x3) << 21)
7565614e71bSYork Sun 		| ((num_pr & 0xf) << 12)
7575614e71bSYork Sun 		| ((slow & 1) << 11)
7585614e71bSYork Sun 		| (x4_en << 10)
7595614e71bSYork Sun 		| (qd_en << 9)
7605614e71bSYork Sun 		| (unq_mrs_en << 8)
7615614e71bSYork Sun 		| ((obc_cfg & 0x1) << 6)
7625614e71bSYork Sun 		| ((ap_en & 0x1) << 5)
7635614e71bSYork Sun 		| ((d_init & 0x1) << 4)
7645614e71bSYork Sun 		| ((rcw_en & 0x1) << 2)
7655614e71bSYork Sun 		| ((md_en & 0x1) << 0)
7665614e71bSYork Sun 		);
7675614e71bSYork Sun 	debug("FSLDDR: ddr_sdram_cfg_2 = 0x%08x\n", ddr->ddr_sdram_cfg_2);
7685614e71bSYork Sun }
7695614e71bSYork Sun 
7705614e71bSYork Sun /* DDR SDRAM Mode configuration 2 (DDR_SDRAM_MODE_2) */
7715614e71bSYork Sun static void set_ddr_sdram_mode_2(fsl_ddr_cfg_regs_t *ddr,
7725614e71bSYork Sun 				const memctl_options_t *popts,
7735614e71bSYork Sun 				const common_timing_params_t *common_dimm,
7745614e71bSYork Sun 				const unsigned int unq_mrs_en)
7755614e71bSYork Sun {
7765614e71bSYork Sun 	unsigned short esdmode2 = 0;	/* Extended SDRAM mode 2 */
7775614e71bSYork Sun 	unsigned short esdmode3 = 0;	/* Extended SDRAM mode 3 */
7785614e71bSYork Sun 
7795614e71bSYork Sun #if defined(CONFIG_SYS_FSL_DDR3)
7805614e71bSYork Sun 	int i;
7815614e71bSYork Sun 	unsigned int rtt_wr = 0;	/* Rtt_WR - dynamic ODT off */
7825614e71bSYork Sun 	unsigned int srt = 0;	/* self-refresh temerature, normal range */
7835614e71bSYork Sun 	unsigned int asr = 0;	/* auto self-refresh disable */
7845614e71bSYork Sun 	unsigned int cwl = compute_cas_write_latency() - 5;
7855614e71bSYork Sun 	unsigned int pasr = 0;	/* partial array self refresh disable */
7865614e71bSYork Sun 
7875614e71bSYork Sun 	if (popts->rtt_override)
7885614e71bSYork Sun 		rtt_wr = popts->rtt_wr_override_value;
7895614e71bSYork Sun 	else
7905614e71bSYork Sun 		rtt_wr = popts->cs_local_opts[0].odt_rtt_wr;
7915614e71bSYork Sun 
7925614e71bSYork Sun 	if (common_dimm->extended_op_srt)
7935614e71bSYork Sun 		srt = common_dimm->extended_op_srt;
7945614e71bSYork Sun 
7955614e71bSYork Sun 	esdmode2 = (0
7965614e71bSYork Sun 		| ((rtt_wr & 0x3) << 9)
7975614e71bSYork Sun 		| ((srt & 0x1) << 7)
7985614e71bSYork Sun 		| ((asr & 0x1) << 6)
7995614e71bSYork Sun 		| ((cwl & 0x7) << 3)
8005614e71bSYork Sun 		| ((pasr & 0x7) << 0));
8015614e71bSYork Sun #endif
8025614e71bSYork Sun 	ddr->ddr_sdram_mode_2 = (0
8035614e71bSYork Sun 				 | ((esdmode2 & 0xFFFF) << 16)
8045614e71bSYork Sun 				 | ((esdmode3 & 0xFFFF) << 0)
8055614e71bSYork Sun 				 );
8065614e71bSYork Sun 	debug("FSLDDR: ddr_sdram_mode_2 = 0x%08x\n", ddr->ddr_sdram_mode_2);
8075614e71bSYork Sun 
8085614e71bSYork Sun #ifdef CONFIG_SYS_FSL_DDR3
8095614e71bSYork Sun 	if (unq_mrs_en) {	/* unique mode registers are supported */
8105614e71bSYork Sun 		for (i = 1; i < CONFIG_CHIP_SELECTS_PER_CTRL; i++) {
8115614e71bSYork Sun 			if (popts->rtt_override)
8125614e71bSYork Sun 				rtt_wr = popts->rtt_wr_override_value;
8135614e71bSYork Sun 			else
8145614e71bSYork Sun 				rtt_wr = popts->cs_local_opts[i].odt_rtt_wr;
8155614e71bSYork Sun 
8165614e71bSYork Sun 			esdmode2 &= 0xF9FF;	/* clear bit 10, 9 */
8175614e71bSYork Sun 			esdmode2 |= (rtt_wr & 0x3) << 9;
8185614e71bSYork Sun 			switch (i) {
8195614e71bSYork Sun 			case 1:
8205614e71bSYork Sun 				ddr->ddr_sdram_mode_4 = (0
8215614e71bSYork Sun 					| ((esdmode2 & 0xFFFF) << 16)
8225614e71bSYork Sun 					| ((esdmode3 & 0xFFFF) << 0)
8235614e71bSYork Sun 					);
8245614e71bSYork Sun 				break;
8255614e71bSYork Sun 			case 2:
8265614e71bSYork Sun 				ddr->ddr_sdram_mode_6 = (0
8275614e71bSYork Sun 					| ((esdmode2 & 0xFFFF) << 16)
8285614e71bSYork Sun 					| ((esdmode3 & 0xFFFF) << 0)
8295614e71bSYork Sun 					);
8305614e71bSYork Sun 				break;
8315614e71bSYork Sun 			case 3:
8325614e71bSYork Sun 				ddr->ddr_sdram_mode_8 = (0
8335614e71bSYork Sun 					| ((esdmode2 & 0xFFFF) << 16)
8345614e71bSYork Sun 					| ((esdmode3 & 0xFFFF) << 0)
8355614e71bSYork Sun 					);
8365614e71bSYork Sun 				break;
8375614e71bSYork Sun 			}
8385614e71bSYork Sun 		}
8395614e71bSYork Sun 		debug("FSLDDR: ddr_sdram_mode_4 = 0x%08x\n",
8405614e71bSYork Sun 			ddr->ddr_sdram_mode_4);
8415614e71bSYork Sun 		debug("FSLDDR: ddr_sdram_mode_6 = 0x%08x\n",
8425614e71bSYork Sun 			ddr->ddr_sdram_mode_6);
8435614e71bSYork Sun 		debug("FSLDDR: ddr_sdram_mode_8 = 0x%08x\n",
8445614e71bSYork Sun 			ddr->ddr_sdram_mode_8);
8455614e71bSYork Sun 	}
8465614e71bSYork Sun #endif
8475614e71bSYork Sun }
8485614e71bSYork Sun 
8495614e71bSYork Sun /* DDR SDRAM Interval Configuration (DDR_SDRAM_INTERVAL) */
8505614e71bSYork Sun static void set_ddr_sdram_interval(fsl_ddr_cfg_regs_t *ddr,
8515614e71bSYork Sun 			       const memctl_options_t *popts,
8525614e71bSYork Sun 			       const common_timing_params_t *common_dimm)
8535614e71bSYork Sun {
8545614e71bSYork Sun 	unsigned int refint;	/* Refresh interval */
8555614e71bSYork Sun 	unsigned int bstopre;	/* Precharge interval */
8565614e71bSYork Sun 
8575614e71bSYork Sun 	refint = picos_to_mclk(common_dimm->refresh_rate_ps);
8585614e71bSYork Sun 
8595614e71bSYork Sun 	bstopre = popts->bstopre;
8605614e71bSYork Sun 
8615614e71bSYork Sun 	/* refint field used 0x3FFF in earlier controllers */
8625614e71bSYork Sun 	ddr->ddr_sdram_interval = (0
8635614e71bSYork Sun 				   | ((refint & 0xFFFF) << 16)
8645614e71bSYork Sun 				   | ((bstopre & 0x3FFF) << 0)
8655614e71bSYork Sun 				   );
8665614e71bSYork Sun 	debug("FSLDDR: ddr_sdram_interval = 0x%08x\n", ddr->ddr_sdram_interval);
8675614e71bSYork Sun }
8685614e71bSYork Sun 
8695614e71bSYork Sun #if defined(CONFIG_SYS_FSL_DDR3)
8705614e71bSYork Sun /* DDR SDRAM Mode configuration set (DDR_SDRAM_MODE) */
8715614e71bSYork Sun static void set_ddr_sdram_mode(fsl_ddr_cfg_regs_t *ddr,
8725614e71bSYork Sun 			       const memctl_options_t *popts,
8735614e71bSYork Sun 			       const common_timing_params_t *common_dimm,
8745614e71bSYork Sun 			       unsigned int cas_latency,
8755614e71bSYork Sun 			       unsigned int additive_latency,
8765614e71bSYork Sun 			       const unsigned int unq_mrs_en)
8775614e71bSYork Sun {
8785614e71bSYork Sun 	unsigned short esdmode;		/* Extended SDRAM mode */
8795614e71bSYork Sun 	unsigned short sdmode;		/* SDRAM mode */
8805614e71bSYork Sun 
8815614e71bSYork Sun 	/* Mode Register - MR1 */
8825614e71bSYork Sun 	unsigned int qoff = 0;		/* Output buffer enable 0=yes, 1=no */
8835614e71bSYork Sun 	unsigned int tdqs_en = 0;	/* TDQS Enable: 0=no, 1=yes */
8845614e71bSYork Sun 	unsigned int rtt;
8855614e71bSYork Sun 	unsigned int wrlvl_en = 0;	/* Write level enable: 0=no, 1=yes */
8865614e71bSYork Sun 	unsigned int al = 0;		/* Posted CAS# additive latency (AL) */
8875614e71bSYork Sun 	unsigned int dic = 0;		/* Output driver impedance, 40ohm */
8885614e71bSYork Sun 	unsigned int dll_en = 0;	/* DLL Enable  0=Enable (Normal),
8895614e71bSYork Sun 						       1=Disable (Test/Debug) */
8905614e71bSYork Sun 
8915614e71bSYork Sun 	/* Mode Register - MR0 */
8925614e71bSYork Sun 	unsigned int dll_on;	/* DLL control for precharge PD, 0=off, 1=on */
8935614e71bSYork Sun 	unsigned int wr = 0;	/* Write Recovery */
8945614e71bSYork Sun 	unsigned int dll_rst;	/* DLL Reset */
8955614e71bSYork Sun 	unsigned int mode;	/* Normal=0 or Test=1 */
8965614e71bSYork Sun 	unsigned int caslat = 4;/* CAS# latency, default set as 6 cycles */
8975614e71bSYork Sun 	/* BT: Burst Type (0=Nibble Sequential, 1=Interleaved) */
8985614e71bSYork Sun 	unsigned int bt;
8995614e71bSYork Sun 	unsigned int bl;	/* BL: Burst Length */
9005614e71bSYork Sun 
9015614e71bSYork Sun 	unsigned int wr_mclk;
9025614e71bSYork Sun 	/*
9035614e71bSYork Sun 	 * DDR_SDRAM_MODE doesn't support 9,11,13,15
9045614e71bSYork Sun 	 * Please refer JEDEC Standard No. 79-3E for Mode Register MR0
9055614e71bSYork Sun 	 * for this table
9065614e71bSYork Sun 	 */
9075614e71bSYork Sun 	static const u8 wr_table[] = {1, 2, 3, 4, 5, 5, 6, 6, 7, 7, 0, 0};
9085614e71bSYork Sun 
9095614e71bSYork Sun 	const unsigned int mclk_ps = get_memory_clk_period_ps();
9105614e71bSYork Sun 	int i;
9115614e71bSYork Sun 
9125614e71bSYork Sun 	if (popts->rtt_override)
9135614e71bSYork Sun 		rtt = popts->rtt_override_value;
9145614e71bSYork Sun 	else
9155614e71bSYork Sun 		rtt = popts->cs_local_opts[0].odt_rtt_norm;
9165614e71bSYork Sun 
9175614e71bSYork Sun 	if (additive_latency == (cas_latency - 1))
9185614e71bSYork Sun 		al = 1;
9195614e71bSYork Sun 	if (additive_latency == (cas_latency - 2))
9205614e71bSYork Sun 		al = 2;
9215614e71bSYork Sun 
9225614e71bSYork Sun 	if (popts->quad_rank_present)
9235614e71bSYork Sun 		dic = 1;	/* output driver impedance 240/7 ohm */
9245614e71bSYork Sun 
9255614e71bSYork Sun 	/*
9265614e71bSYork Sun 	 * The esdmode value will also be used for writing
9275614e71bSYork Sun 	 * MR1 during write leveling for DDR3, although the
9285614e71bSYork Sun 	 * bits specifically related to the write leveling
9295614e71bSYork Sun 	 * scheme will be handled automatically by the DDR
9305614e71bSYork Sun 	 * controller. so we set the wrlvl_en = 0 here.
9315614e71bSYork Sun 	 */
9325614e71bSYork Sun 	esdmode = (0
9335614e71bSYork Sun 		| ((qoff & 0x1) << 12)
9345614e71bSYork Sun 		| ((tdqs_en & 0x1) << 11)
9355614e71bSYork Sun 		| ((rtt & 0x4) << 7)   /* rtt field is split */
9365614e71bSYork Sun 		| ((wrlvl_en & 0x1) << 7)
9375614e71bSYork Sun 		| ((rtt & 0x2) << 5)   /* rtt field is split */
9385614e71bSYork Sun 		| ((dic & 0x2) << 4)   /* DIC field is split */
9395614e71bSYork Sun 		| ((al & 0x3) << 3)
9405614e71bSYork Sun 		| ((rtt & 0x1) << 2)  /* rtt field is split */
9415614e71bSYork Sun 		| ((dic & 0x1) << 1)   /* DIC field is split */
9425614e71bSYork Sun 		| ((dll_en & 0x1) << 0)
9435614e71bSYork Sun 		);
9445614e71bSYork Sun 
9455614e71bSYork Sun 	/*
9465614e71bSYork Sun 	 * DLL control for precharge PD
9475614e71bSYork Sun 	 * 0=slow exit DLL off (tXPDLL)
9485614e71bSYork Sun 	 * 1=fast exit DLL on (tXP)
9495614e71bSYork Sun 	 */
9505614e71bSYork Sun 	dll_on = 1;
9515614e71bSYork Sun 
9525614e71bSYork Sun 	wr_mclk = (common_dimm->twr_ps + mclk_ps - 1) / mclk_ps;
9535614e71bSYork Sun 	if (wr_mclk <= 16) {
9545614e71bSYork Sun 		wr = wr_table[wr_mclk - 5];
9555614e71bSYork Sun 	} else {
9565614e71bSYork Sun 		printf("Error: unsupported write recovery for mode register "
9575614e71bSYork Sun 		       "wr_mclk = %d\n", wr_mclk);
9585614e71bSYork Sun 	}
9595614e71bSYork Sun 
9605614e71bSYork Sun 	dll_rst = 0;	/* dll no reset */
9615614e71bSYork Sun 	mode = 0;	/* normal mode */
9625614e71bSYork Sun 
9635614e71bSYork Sun 	/* look up table to get the cas latency bits */
9645614e71bSYork Sun 	if (cas_latency >= 5 && cas_latency <= 16) {
9655614e71bSYork Sun 		unsigned char cas_latency_table[] = {
9665614e71bSYork Sun 			0x2,	/* 5 clocks */
9675614e71bSYork Sun 			0x4,	/* 6 clocks */
9685614e71bSYork Sun 			0x6,	/* 7 clocks */
9695614e71bSYork Sun 			0x8,	/* 8 clocks */
9705614e71bSYork Sun 			0xa,	/* 9 clocks */
9715614e71bSYork Sun 			0xc,	/* 10 clocks */
9725614e71bSYork Sun 			0xe,	/* 11 clocks */
9735614e71bSYork Sun 			0x1,	/* 12 clocks */
9745614e71bSYork Sun 			0x3,	/* 13 clocks */
9755614e71bSYork Sun 			0x5,	/* 14 clocks */
9765614e71bSYork Sun 			0x7,	/* 15 clocks */
9775614e71bSYork Sun 			0x9,	/* 16 clocks */
9785614e71bSYork Sun 		};
9795614e71bSYork Sun 		caslat = cas_latency_table[cas_latency - 5];
9805614e71bSYork Sun 	} else {
9815614e71bSYork Sun 		printf("Error: unsupported cas latency for mode register\n");
9825614e71bSYork Sun 	}
9835614e71bSYork Sun 
9845614e71bSYork Sun 	bt = 0;	/* Nibble sequential */
9855614e71bSYork Sun 
9865614e71bSYork Sun 	switch (popts->burst_length) {
9875614e71bSYork Sun 	case DDR_BL8:
9885614e71bSYork Sun 		bl = 0;
9895614e71bSYork Sun 		break;
9905614e71bSYork Sun 	case DDR_OTF:
9915614e71bSYork Sun 		bl = 1;
9925614e71bSYork Sun 		break;
9935614e71bSYork Sun 	case DDR_BC4:
9945614e71bSYork Sun 		bl = 2;
9955614e71bSYork Sun 		break;
9965614e71bSYork Sun 	default:
9975614e71bSYork Sun 		printf("Error: invalid burst length of %u specified. "
9985614e71bSYork Sun 			" Defaulting to on-the-fly BC4 or BL8 beats.\n",
9995614e71bSYork Sun 			popts->burst_length);
10005614e71bSYork Sun 		bl = 1;
10015614e71bSYork Sun 		break;
10025614e71bSYork Sun 	}
10035614e71bSYork Sun 
10045614e71bSYork Sun 	sdmode = (0
10055614e71bSYork Sun 		  | ((dll_on & 0x1) << 12)
10065614e71bSYork Sun 		  | ((wr & 0x7) << 9)
10075614e71bSYork Sun 		  | ((dll_rst & 0x1) << 8)
10085614e71bSYork Sun 		  | ((mode & 0x1) << 7)
10095614e71bSYork Sun 		  | (((caslat >> 1) & 0x7) << 4)
10105614e71bSYork Sun 		  | ((bt & 0x1) << 3)
10115614e71bSYork Sun 		  | ((caslat & 1) << 2)
10125614e71bSYork Sun 		  | ((bl & 0x3) << 0)
10135614e71bSYork Sun 		  );
10145614e71bSYork Sun 
10155614e71bSYork Sun 	ddr->ddr_sdram_mode = (0
10165614e71bSYork Sun 			       | ((esdmode & 0xFFFF) << 16)
10175614e71bSYork Sun 			       | ((sdmode & 0xFFFF) << 0)
10185614e71bSYork Sun 			       );
10195614e71bSYork Sun 
10205614e71bSYork Sun 	debug("FSLDDR: ddr_sdram_mode = 0x%08x\n", ddr->ddr_sdram_mode);
10215614e71bSYork Sun 
10225614e71bSYork Sun 	if (unq_mrs_en) {	/* unique mode registers are supported */
10235614e71bSYork Sun 		for (i = 1; i < CONFIG_CHIP_SELECTS_PER_CTRL; i++) {
10245614e71bSYork Sun 			if (popts->rtt_override)
10255614e71bSYork Sun 				rtt = popts->rtt_override_value;
10265614e71bSYork Sun 			else
10275614e71bSYork Sun 				rtt = popts->cs_local_opts[i].odt_rtt_norm;
10285614e71bSYork Sun 
10295614e71bSYork Sun 			esdmode &= 0xFDBB;	/* clear bit 9,6,2 */
10305614e71bSYork Sun 			esdmode |= (0
10315614e71bSYork Sun 				| ((rtt & 0x4) << 7)   /* rtt field is split */
10325614e71bSYork Sun 				| ((rtt & 0x2) << 5)   /* rtt field is split */
10335614e71bSYork Sun 				| ((rtt & 0x1) << 2)  /* rtt field is split */
10345614e71bSYork Sun 				);
10355614e71bSYork Sun 			switch (i) {
10365614e71bSYork Sun 			case 1:
10375614e71bSYork Sun 				ddr->ddr_sdram_mode_3 = (0
10385614e71bSYork Sun 				       | ((esdmode & 0xFFFF) << 16)
10395614e71bSYork Sun 				       | ((sdmode & 0xFFFF) << 0)
10405614e71bSYork Sun 				       );
10415614e71bSYork Sun 				break;
10425614e71bSYork Sun 			case 2:
10435614e71bSYork Sun 				ddr->ddr_sdram_mode_5 = (0
10445614e71bSYork Sun 				       | ((esdmode & 0xFFFF) << 16)
10455614e71bSYork Sun 				       | ((sdmode & 0xFFFF) << 0)
10465614e71bSYork Sun 				       );
10475614e71bSYork Sun 				break;
10485614e71bSYork Sun 			case 3:
10495614e71bSYork Sun 				ddr->ddr_sdram_mode_7 = (0
10505614e71bSYork Sun 				       | ((esdmode & 0xFFFF) << 16)
10515614e71bSYork Sun 				       | ((sdmode & 0xFFFF) << 0)
10525614e71bSYork Sun 				       );
10535614e71bSYork Sun 				break;
10545614e71bSYork Sun 			}
10555614e71bSYork Sun 		}
10565614e71bSYork Sun 		debug("FSLDDR: ddr_sdram_mode_3 = 0x%08x\n",
10575614e71bSYork Sun 			ddr->ddr_sdram_mode_3);
10585614e71bSYork Sun 		debug("FSLDDR: ddr_sdram_mode_5 = 0x%08x\n",
10595614e71bSYork Sun 			ddr->ddr_sdram_mode_5);
10605614e71bSYork Sun 		debug("FSLDDR: ddr_sdram_mode_5 = 0x%08x\n",
10615614e71bSYork Sun 			ddr->ddr_sdram_mode_5);
10625614e71bSYork Sun 	}
10635614e71bSYork Sun }
10645614e71bSYork Sun 
10655614e71bSYork Sun #else /* !CONFIG_SYS_FSL_DDR3 */
10665614e71bSYork Sun 
10675614e71bSYork Sun /* DDR SDRAM Mode configuration set (DDR_SDRAM_MODE) */
10685614e71bSYork Sun static void set_ddr_sdram_mode(fsl_ddr_cfg_regs_t *ddr,
10695614e71bSYork Sun 			       const memctl_options_t *popts,
10705614e71bSYork Sun 			       const common_timing_params_t *common_dimm,
10715614e71bSYork Sun 			       unsigned int cas_latency,
10725614e71bSYork Sun 			       unsigned int additive_latency,
10735614e71bSYork Sun 			       const unsigned int unq_mrs_en)
10745614e71bSYork Sun {
10755614e71bSYork Sun 	unsigned short esdmode;		/* Extended SDRAM mode */
10765614e71bSYork Sun 	unsigned short sdmode;		/* SDRAM mode */
10775614e71bSYork Sun 
10785614e71bSYork Sun 	/*
10795614e71bSYork Sun 	 * FIXME: This ought to be pre-calculated in a
10805614e71bSYork Sun 	 * technology-specific routine,
10815614e71bSYork Sun 	 * e.g. compute_DDR2_mode_register(), and then the
10825614e71bSYork Sun 	 * sdmode and esdmode passed in as part of common_dimm.
10835614e71bSYork Sun 	 */
10845614e71bSYork Sun 
10855614e71bSYork Sun 	/* Extended Mode Register */
10865614e71bSYork Sun 	unsigned int mrs = 0;		/* Mode Register Set */
10875614e71bSYork Sun 	unsigned int outputs = 0;	/* 0=Enabled, 1=Disabled */
10885614e71bSYork Sun 	unsigned int rdqs_en = 0;	/* RDQS Enable: 0=no, 1=yes */
10895614e71bSYork Sun 	unsigned int dqs_en = 0;	/* DQS# Enable: 0=enable, 1=disable */
10905614e71bSYork Sun 	unsigned int ocd = 0;		/* 0x0=OCD not supported,
10915614e71bSYork Sun 					   0x7=OCD default state */
10925614e71bSYork Sun 	unsigned int rtt;
10935614e71bSYork Sun 	unsigned int al;		/* Posted CAS# additive latency (AL) */
10945614e71bSYork Sun 	unsigned int ods = 0;		/* Output Drive Strength:
10955614e71bSYork Sun 						0 = Full strength (18ohm)
10965614e71bSYork Sun 						1 = Reduced strength (4ohm) */
10975614e71bSYork Sun 	unsigned int dll_en = 0;	/* DLL Enable  0=Enable (Normal),
10985614e71bSYork Sun 						       1=Disable (Test/Debug) */
10995614e71bSYork Sun 
11005614e71bSYork Sun 	/* Mode Register (MR) */
11015614e71bSYork Sun 	unsigned int mr;	/* Mode Register Definition */
11025614e71bSYork Sun 	unsigned int pd;	/* Power-Down Mode */
11035614e71bSYork Sun 	unsigned int wr;	/* Write Recovery */
11045614e71bSYork Sun 	unsigned int dll_res;	/* DLL Reset */
11055614e71bSYork Sun 	unsigned int mode;	/* Normal=0 or Test=1 */
11065614e71bSYork Sun 	unsigned int caslat = 0;/* CAS# latency */
11075614e71bSYork Sun 	/* BT: Burst Type (0=Sequential, 1=Interleaved) */
11085614e71bSYork Sun 	unsigned int bt;
11095614e71bSYork Sun 	unsigned int bl;	/* BL: Burst Length */
11105614e71bSYork Sun 
11115614e71bSYork Sun #if defined(CONFIG_SYS_FSL_DDR2)
11125614e71bSYork Sun 	const unsigned int mclk_ps = get_memory_clk_period_ps();
11135614e71bSYork Sun #endif
11145614e71bSYork Sun 	dqs_en = !popts->dqs_config;
11155614e71bSYork Sun 	rtt = fsl_ddr_get_rtt();
11165614e71bSYork Sun 
11175614e71bSYork Sun 	al = additive_latency;
11185614e71bSYork Sun 
11195614e71bSYork Sun 	esdmode = (0
11205614e71bSYork Sun 		| ((mrs & 0x3) << 14)
11215614e71bSYork Sun 		| ((outputs & 0x1) << 12)
11225614e71bSYork Sun 		| ((rdqs_en & 0x1) << 11)
11235614e71bSYork Sun 		| ((dqs_en & 0x1) << 10)
11245614e71bSYork Sun 		| ((ocd & 0x7) << 7)
11255614e71bSYork Sun 		| ((rtt & 0x2) << 5)   /* rtt field is split */
11265614e71bSYork Sun 		| ((al & 0x7) << 3)
11275614e71bSYork Sun 		| ((rtt & 0x1) << 2)   /* rtt field is split */
11285614e71bSYork Sun 		| ((ods & 0x1) << 1)
11295614e71bSYork Sun 		| ((dll_en & 0x1) << 0)
11305614e71bSYork Sun 		);
11315614e71bSYork Sun 
11325614e71bSYork Sun 	mr = 0;		 /* FIXME: CHECKME */
11335614e71bSYork Sun 
11345614e71bSYork Sun 	/*
11355614e71bSYork Sun 	 * 0 = Fast Exit (Normal)
11365614e71bSYork Sun 	 * 1 = Slow Exit (Low Power)
11375614e71bSYork Sun 	 */
11385614e71bSYork Sun 	pd = 0;
11395614e71bSYork Sun 
11405614e71bSYork Sun #if defined(CONFIG_SYS_FSL_DDR1)
11415614e71bSYork Sun 	wr = 0;       /* Historical */
11425614e71bSYork Sun #elif defined(CONFIG_SYS_FSL_DDR2)
11435614e71bSYork Sun 	wr = (common_dimm->twr_ps + mclk_ps - 1) / mclk_ps - 1;
11445614e71bSYork Sun #endif
11455614e71bSYork Sun 	dll_res = 0;
11465614e71bSYork Sun 	mode = 0;
11475614e71bSYork Sun 
11485614e71bSYork Sun #if defined(CONFIG_SYS_FSL_DDR1)
11495614e71bSYork Sun 	if (1 <= cas_latency && cas_latency <= 4) {
11505614e71bSYork Sun 		unsigned char mode_caslat_table[4] = {
11515614e71bSYork Sun 			0x5,	/* 1.5 clocks */
11525614e71bSYork Sun 			0x2,	/* 2.0 clocks */
11535614e71bSYork Sun 			0x6,	/* 2.5 clocks */
11545614e71bSYork Sun 			0x3	/* 3.0 clocks */
11555614e71bSYork Sun 		};
11565614e71bSYork Sun 		caslat = mode_caslat_table[cas_latency - 1];
11575614e71bSYork Sun 	} else {
11585614e71bSYork Sun 		printf("Warning: unknown cas_latency %d\n", cas_latency);
11595614e71bSYork Sun 	}
11605614e71bSYork Sun #elif defined(CONFIG_SYS_FSL_DDR2)
11615614e71bSYork Sun 	caslat = cas_latency;
11625614e71bSYork Sun #endif
11635614e71bSYork Sun 	bt = 0;
11645614e71bSYork Sun 
11655614e71bSYork Sun 	switch (popts->burst_length) {
11665614e71bSYork Sun 	case DDR_BL4:
11675614e71bSYork Sun 		bl = 2;
11685614e71bSYork Sun 		break;
11695614e71bSYork Sun 	case DDR_BL8:
11705614e71bSYork Sun 		bl = 3;
11715614e71bSYork Sun 		break;
11725614e71bSYork Sun 	default:
11735614e71bSYork Sun 		printf("Error: invalid burst length of %u specified. "
11745614e71bSYork Sun 			" Defaulting to 4 beats.\n",
11755614e71bSYork Sun 			popts->burst_length);
11765614e71bSYork Sun 		bl = 2;
11775614e71bSYork Sun 		break;
11785614e71bSYork Sun 	}
11795614e71bSYork Sun 
11805614e71bSYork Sun 	sdmode = (0
11815614e71bSYork Sun 		  | ((mr & 0x3) << 14)
11825614e71bSYork Sun 		  | ((pd & 0x1) << 12)
11835614e71bSYork Sun 		  | ((wr & 0x7) << 9)
11845614e71bSYork Sun 		  | ((dll_res & 0x1) << 8)
11855614e71bSYork Sun 		  | ((mode & 0x1) << 7)
11865614e71bSYork Sun 		  | ((caslat & 0x7) << 4)
11875614e71bSYork Sun 		  | ((bt & 0x1) << 3)
11885614e71bSYork Sun 		  | ((bl & 0x7) << 0)
11895614e71bSYork Sun 		  );
11905614e71bSYork Sun 
11915614e71bSYork Sun 	ddr->ddr_sdram_mode = (0
11925614e71bSYork Sun 			       | ((esdmode & 0xFFFF) << 16)
11935614e71bSYork Sun 			       | ((sdmode & 0xFFFF) << 0)
11945614e71bSYork Sun 			       );
11955614e71bSYork Sun 	debug("FSLDDR: ddr_sdram_mode = 0x%08x\n", ddr->ddr_sdram_mode);
11965614e71bSYork Sun }
11975614e71bSYork Sun #endif
11985614e71bSYork Sun 
11995614e71bSYork Sun /* DDR SDRAM Data Initialization (DDR_DATA_INIT) */
12005614e71bSYork Sun static void set_ddr_data_init(fsl_ddr_cfg_regs_t *ddr)
12015614e71bSYork Sun {
12025614e71bSYork Sun 	unsigned int init_value;	/* Initialization value */
12035614e71bSYork Sun 
12045614e71bSYork Sun #ifdef CONFIG_MEM_INIT_VALUE
12055614e71bSYork Sun 	init_value = CONFIG_MEM_INIT_VALUE;
12065614e71bSYork Sun #else
12075614e71bSYork Sun 	init_value = 0xDEADBEEF;
12085614e71bSYork Sun #endif
12095614e71bSYork Sun 	ddr->ddr_data_init = init_value;
12105614e71bSYork Sun }
12115614e71bSYork Sun 
12125614e71bSYork Sun /*
12135614e71bSYork Sun  * DDR SDRAM Clock Control (DDR_SDRAM_CLK_CNTL)
12145614e71bSYork Sun  * The old controller on the 8540/60 doesn't have this register.
12155614e71bSYork Sun  * Hope it's OK to set it (to 0) anyway.
12165614e71bSYork Sun  */
12175614e71bSYork Sun static void set_ddr_sdram_clk_cntl(fsl_ddr_cfg_regs_t *ddr,
12185614e71bSYork Sun 					 const memctl_options_t *popts)
12195614e71bSYork Sun {
12205614e71bSYork Sun 	unsigned int clk_adjust;	/* Clock adjust */
12215614e71bSYork Sun 
12225614e71bSYork Sun 	clk_adjust = popts->clk_adjust;
12235614e71bSYork Sun 	ddr->ddr_sdram_clk_cntl = (clk_adjust & 0xF) << 23;
12245614e71bSYork Sun 	debug("FSLDDR: clk_cntl = 0x%08x\n", ddr->ddr_sdram_clk_cntl);
12255614e71bSYork Sun }
12265614e71bSYork Sun 
12275614e71bSYork Sun /* DDR Initialization Address (DDR_INIT_ADDR) */
12285614e71bSYork Sun static void set_ddr_init_addr(fsl_ddr_cfg_regs_t *ddr)
12295614e71bSYork Sun {
12305614e71bSYork Sun 	unsigned int init_addr = 0;	/* Initialization address */
12315614e71bSYork Sun 
12325614e71bSYork Sun 	ddr->ddr_init_addr = init_addr;
12335614e71bSYork Sun }
12345614e71bSYork Sun 
12355614e71bSYork Sun /* DDR Initialization Address (DDR_INIT_EXT_ADDR) */
12365614e71bSYork Sun static void set_ddr_init_ext_addr(fsl_ddr_cfg_regs_t *ddr)
12375614e71bSYork Sun {
12385614e71bSYork Sun 	unsigned int uia = 0;	/* Use initialization address */
12395614e71bSYork Sun 	unsigned int init_ext_addr = 0;	/* Initialization address */
12405614e71bSYork Sun 
12415614e71bSYork Sun 	ddr->ddr_init_ext_addr = (0
12425614e71bSYork Sun 				  | ((uia & 0x1) << 31)
12435614e71bSYork Sun 				  | (init_ext_addr & 0xF)
12445614e71bSYork Sun 				  );
12455614e71bSYork Sun }
12465614e71bSYork Sun 
12475614e71bSYork Sun /* DDR SDRAM Timing Configuration 4 (TIMING_CFG_4) */
12485614e71bSYork Sun static void set_timing_cfg_4(fsl_ddr_cfg_regs_t *ddr,
12495614e71bSYork Sun 				const memctl_options_t *popts)
12505614e71bSYork Sun {
12515614e71bSYork Sun 	unsigned int rwt = 0; /* Read-to-write turnaround for same CS */
12525614e71bSYork Sun 	unsigned int wrt = 0; /* Write-to-read turnaround for same CS */
12535614e71bSYork Sun 	unsigned int rrt = 0; /* Read-to-read turnaround for same CS */
12545614e71bSYork Sun 	unsigned int wwt = 0; /* Write-to-write turnaround for same CS */
12555614e71bSYork Sun 	unsigned int dll_lock = 0; /* DDR SDRAM DLL Lock Time */
12565614e71bSYork Sun 
12575614e71bSYork Sun #if defined(CONFIG_SYS_FSL_DDR3)
12585614e71bSYork Sun 	if (popts->burst_length == DDR_BL8) {
12595614e71bSYork Sun 		/* We set BL/2 for fixed BL8 */
12605614e71bSYork Sun 		rrt = 0;	/* BL/2 clocks */
12615614e71bSYork Sun 		wwt = 0;	/* BL/2 clocks */
12625614e71bSYork Sun 	} else {
12635614e71bSYork Sun 		/* We need to set BL/2 + 2 to BC4 and OTF */
12645614e71bSYork Sun 		rrt = 2;	/* BL/2 + 2 clocks */
12655614e71bSYork Sun 		wwt = 2;	/* BL/2 + 2 clocks */
12665614e71bSYork Sun 	}
12675614e71bSYork Sun 	dll_lock = 1;	/* tDLLK = 512 clocks from spec */
12685614e71bSYork Sun #endif
12695614e71bSYork Sun 	ddr->timing_cfg_4 = (0
12705614e71bSYork Sun 			     | ((rwt & 0xf) << 28)
12715614e71bSYork Sun 			     | ((wrt & 0xf) << 24)
12725614e71bSYork Sun 			     | ((rrt & 0xf) << 20)
12735614e71bSYork Sun 			     | ((wwt & 0xf) << 16)
12745614e71bSYork Sun 			     | (dll_lock & 0x3)
12755614e71bSYork Sun 			     );
12765614e71bSYork Sun 	debug("FSLDDR: timing_cfg_4 = 0x%08x\n", ddr->timing_cfg_4);
12775614e71bSYork Sun }
12785614e71bSYork Sun 
12795614e71bSYork Sun /* DDR SDRAM Timing Configuration 5 (TIMING_CFG_5) */
12805614e71bSYork Sun static void set_timing_cfg_5(fsl_ddr_cfg_regs_t *ddr, unsigned int cas_latency)
12815614e71bSYork Sun {
12825614e71bSYork Sun 	unsigned int rodt_on = 0;	/* Read to ODT on */
12835614e71bSYork Sun 	unsigned int rodt_off = 0;	/* Read to ODT off */
12845614e71bSYork Sun 	unsigned int wodt_on = 0;	/* Write to ODT on */
12855614e71bSYork Sun 	unsigned int wodt_off = 0;	/* Write to ODT off */
12865614e71bSYork Sun 
12875614e71bSYork Sun #if defined(CONFIG_SYS_FSL_DDR3)
12885614e71bSYork Sun 	/* rodt_on = timing_cfg_1[caslat] - timing_cfg_2[wrlat] + 1 */
12895614e71bSYork Sun 	rodt_on = cas_latency - ((ddr->timing_cfg_2 & 0x00780000) >> 19) + 1;
12905614e71bSYork Sun 	rodt_off = 4;	/*  4 clocks */
12915614e71bSYork Sun 	wodt_on = 1;	/*  1 clocks */
12925614e71bSYork Sun 	wodt_off = 4;	/*  4 clocks */
12935614e71bSYork Sun #endif
12945614e71bSYork Sun 
12955614e71bSYork Sun 	ddr->timing_cfg_5 = (0
12965614e71bSYork Sun 			     | ((rodt_on & 0x1f) << 24)
12975614e71bSYork Sun 			     | ((rodt_off & 0x7) << 20)
12985614e71bSYork Sun 			     | ((wodt_on & 0x1f) << 12)
12995614e71bSYork Sun 			     | ((wodt_off & 0x7) << 8)
13005614e71bSYork Sun 			     );
13015614e71bSYork Sun 	debug("FSLDDR: timing_cfg_5 = 0x%08x\n", ddr->timing_cfg_5);
13025614e71bSYork Sun }
13035614e71bSYork Sun 
13045614e71bSYork Sun /* DDR ZQ Calibration Control (DDR_ZQ_CNTL) */
13055614e71bSYork Sun static void set_ddr_zq_cntl(fsl_ddr_cfg_regs_t *ddr, unsigned int zq_en)
13065614e71bSYork Sun {
13075614e71bSYork Sun 	unsigned int zqinit = 0;/* POR ZQ Calibration Time (tZQinit) */
13085614e71bSYork Sun 	/* Normal Operation Full Calibration Time (tZQoper) */
13095614e71bSYork Sun 	unsigned int zqoper = 0;
13105614e71bSYork Sun 	/* Normal Operation Short Calibration Time (tZQCS) */
13115614e71bSYork Sun 	unsigned int zqcs = 0;
13125614e71bSYork Sun 
13135614e71bSYork Sun 	if (zq_en) {
13145614e71bSYork Sun 		zqinit = 9;	/* 512 clocks */
13155614e71bSYork Sun 		zqoper = 8;	/* 256 clocks */
13165614e71bSYork Sun 		zqcs = 6;	/* 64 clocks */
13175614e71bSYork Sun 	}
13185614e71bSYork Sun 
13195614e71bSYork Sun 	ddr->ddr_zq_cntl = (0
13205614e71bSYork Sun 			    | ((zq_en & 0x1) << 31)
13215614e71bSYork Sun 			    | ((zqinit & 0xF) << 24)
13225614e71bSYork Sun 			    | ((zqoper & 0xF) << 16)
13235614e71bSYork Sun 			    | ((zqcs & 0xF) << 8)
13245614e71bSYork Sun 			    );
13255614e71bSYork Sun 	debug("FSLDDR: zq_cntl = 0x%08x\n", ddr->ddr_zq_cntl);
13265614e71bSYork Sun }
13275614e71bSYork Sun 
13285614e71bSYork Sun /* DDR Write Leveling Control (DDR_WRLVL_CNTL) */
13295614e71bSYork Sun static void set_ddr_wrlvl_cntl(fsl_ddr_cfg_regs_t *ddr, unsigned int wrlvl_en,
13305614e71bSYork Sun 				const memctl_options_t *popts)
13315614e71bSYork Sun {
13325614e71bSYork Sun 	/*
13335614e71bSYork Sun 	 * First DQS pulse rising edge after margining mode
13345614e71bSYork Sun 	 * is programmed (tWL_MRD)
13355614e71bSYork Sun 	 */
13365614e71bSYork Sun 	unsigned int wrlvl_mrd = 0;
13375614e71bSYork Sun 	/* ODT delay after margining mode is programmed (tWL_ODTEN) */
13385614e71bSYork Sun 	unsigned int wrlvl_odten = 0;
13395614e71bSYork Sun 	/* DQS/DQS_ delay after margining mode is programmed (tWL_DQSEN) */
13405614e71bSYork Sun 	unsigned int wrlvl_dqsen = 0;
13415614e71bSYork Sun 	/* WRLVL_SMPL: Write leveling sample time */
13425614e71bSYork Sun 	unsigned int wrlvl_smpl = 0;
13435614e71bSYork Sun 	/* WRLVL_WLR: Write leveling repeition time */
13445614e71bSYork Sun 	unsigned int wrlvl_wlr = 0;
13455614e71bSYork Sun 	/* WRLVL_START: Write leveling start time */
13465614e71bSYork Sun 	unsigned int wrlvl_start = 0;
13475614e71bSYork Sun 
13485614e71bSYork Sun 	/* suggest enable write leveling for DDR3 due to fly-by topology */
13495614e71bSYork Sun 	if (wrlvl_en) {
13505614e71bSYork Sun 		/* tWL_MRD min = 40 nCK, we set it 64 */
13515614e71bSYork Sun 		wrlvl_mrd = 0x6;
13525614e71bSYork Sun 		/* tWL_ODTEN 128 */
13535614e71bSYork Sun 		wrlvl_odten = 0x7;
13545614e71bSYork Sun 		/* tWL_DQSEN min = 25 nCK, we set it 32 */
13555614e71bSYork Sun 		wrlvl_dqsen = 0x5;
13565614e71bSYork Sun 		/*
13575614e71bSYork Sun 		 * Write leveling sample time at least need 6 clocks
13585614e71bSYork Sun 		 * higher than tWLO to allow enough time for progagation
13595614e71bSYork Sun 		 * delay and sampling the prime data bits.
13605614e71bSYork Sun 		 */
13615614e71bSYork Sun 		wrlvl_smpl = 0xf;
13625614e71bSYork Sun 		/*
13635614e71bSYork Sun 		 * Write leveling repetition time
13645614e71bSYork Sun 		 * at least tWLO + 6 clocks clocks
13655614e71bSYork Sun 		 * we set it 64
13665614e71bSYork Sun 		 */
13675614e71bSYork Sun 		wrlvl_wlr = 0x6;
13685614e71bSYork Sun 		/*
13695614e71bSYork Sun 		 * Write leveling start time
13705614e71bSYork Sun 		 * The value use for the DQS_ADJUST for the first sample
13715614e71bSYork Sun 		 * when write leveling is enabled. It probably needs to be
13725614e71bSYork Sun 		 * overriden per platform.
13735614e71bSYork Sun 		 */
13745614e71bSYork Sun 		wrlvl_start = 0x8;
13755614e71bSYork Sun 		/*
13765614e71bSYork Sun 		 * Override the write leveling sample and start time
13775614e71bSYork Sun 		 * according to specific board
13785614e71bSYork Sun 		 */
13795614e71bSYork Sun 		if (popts->wrlvl_override) {
13805614e71bSYork Sun 			wrlvl_smpl = popts->wrlvl_sample;
13815614e71bSYork Sun 			wrlvl_start = popts->wrlvl_start;
13825614e71bSYork Sun 		}
13835614e71bSYork Sun 	}
13845614e71bSYork Sun 
13855614e71bSYork Sun 	ddr->ddr_wrlvl_cntl = (0
13865614e71bSYork Sun 			       | ((wrlvl_en & 0x1) << 31)
13875614e71bSYork Sun 			       | ((wrlvl_mrd & 0x7) << 24)
13885614e71bSYork Sun 			       | ((wrlvl_odten & 0x7) << 20)
13895614e71bSYork Sun 			       | ((wrlvl_dqsen & 0x7) << 16)
13905614e71bSYork Sun 			       | ((wrlvl_smpl & 0xf) << 12)
13915614e71bSYork Sun 			       | ((wrlvl_wlr & 0x7) << 8)
13925614e71bSYork Sun 			       | ((wrlvl_start & 0x1F) << 0)
13935614e71bSYork Sun 			       );
13945614e71bSYork Sun 	debug("FSLDDR: wrlvl_cntl = 0x%08x\n", ddr->ddr_wrlvl_cntl);
13955614e71bSYork Sun 	ddr->ddr_wrlvl_cntl_2 = popts->wrlvl_ctl_2;
13965614e71bSYork Sun 	debug("FSLDDR: wrlvl_cntl_2 = 0x%08x\n", ddr->ddr_wrlvl_cntl_2);
13975614e71bSYork Sun 	ddr->ddr_wrlvl_cntl_3 = popts->wrlvl_ctl_3;
13985614e71bSYork Sun 	debug("FSLDDR: wrlvl_cntl_3 = 0x%08x\n", ddr->ddr_wrlvl_cntl_3);
13995614e71bSYork Sun 
14005614e71bSYork Sun }
14015614e71bSYork Sun 
14025614e71bSYork Sun /* DDR Self Refresh Counter (DDR_SR_CNTR) */
14035614e71bSYork Sun static void set_ddr_sr_cntr(fsl_ddr_cfg_regs_t *ddr, unsigned int sr_it)
14045614e71bSYork Sun {
14055614e71bSYork Sun 	/* Self Refresh Idle Threshold */
14065614e71bSYork Sun 	ddr->ddr_sr_cntr = (sr_it & 0xF) << 16;
14075614e71bSYork Sun }
14085614e71bSYork Sun 
14095614e71bSYork Sun static void set_ddr_eor(fsl_ddr_cfg_regs_t *ddr, const memctl_options_t *popts)
14105614e71bSYork Sun {
14115614e71bSYork Sun 	if (popts->addr_hash) {
14125614e71bSYork Sun 		ddr->ddr_eor = 0x40000000;	/* address hash enable */
14135614e71bSYork Sun 		puts("Address hashing enabled.\n");
14145614e71bSYork Sun 	}
14155614e71bSYork Sun }
14165614e71bSYork Sun 
14175614e71bSYork Sun static void set_ddr_cdr1(fsl_ddr_cfg_regs_t *ddr, const memctl_options_t *popts)
14185614e71bSYork Sun {
14195614e71bSYork Sun 	ddr->ddr_cdr1 = popts->ddr_cdr1;
14205614e71bSYork Sun 	debug("FSLDDR: ddr_cdr1 = 0x%08x\n", ddr->ddr_cdr1);
14215614e71bSYork Sun }
14225614e71bSYork Sun 
14235614e71bSYork Sun static void set_ddr_cdr2(fsl_ddr_cfg_regs_t *ddr, const memctl_options_t *popts)
14245614e71bSYork Sun {
14255614e71bSYork Sun 	ddr->ddr_cdr2 = popts->ddr_cdr2;
14265614e71bSYork Sun 	debug("FSLDDR: ddr_cdr2 = 0x%08x\n", ddr->ddr_cdr2);
14275614e71bSYork Sun }
14285614e71bSYork Sun 
14295614e71bSYork Sun unsigned int
14305614e71bSYork Sun check_fsl_memctl_config_regs(const fsl_ddr_cfg_regs_t *ddr)
14315614e71bSYork Sun {
14325614e71bSYork Sun 	unsigned int res = 0;
14335614e71bSYork Sun 
14345614e71bSYork Sun 	/*
14355614e71bSYork Sun 	 * Check that DDR_SDRAM_CFG[RD_EN] and DDR_SDRAM_CFG[2T_EN] are
14365614e71bSYork Sun 	 * not set at the same time.
14375614e71bSYork Sun 	 */
14385614e71bSYork Sun 	if (ddr->ddr_sdram_cfg & 0x10000000
14395614e71bSYork Sun 	    && ddr->ddr_sdram_cfg & 0x00008000) {
14405614e71bSYork Sun 		printf("Error: DDR_SDRAM_CFG[RD_EN] and DDR_SDRAM_CFG[2T_EN] "
14415614e71bSYork Sun 				" should not be set at the same time.\n");
14425614e71bSYork Sun 		res++;
14435614e71bSYork Sun 	}
14445614e71bSYork Sun 
14455614e71bSYork Sun 	return res;
14465614e71bSYork Sun }
14475614e71bSYork Sun 
14485614e71bSYork Sun unsigned int
14495614e71bSYork Sun compute_fsl_memctl_config_regs(const memctl_options_t *popts,
14505614e71bSYork Sun 			       fsl_ddr_cfg_regs_t *ddr,
14515614e71bSYork Sun 			       const common_timing_params_t *common_dimm,
14525614e71bSYork Sun 			       const dimm_params_t *dimm_params,
14535614e71bSYork Sun 			       unsigned int dbw_cap_adj,
14545614e71bSYork Sun 			       unsigned int size_only)
14555614e71bSYork Sun {
14565614e71bSYork Sun 	unsigned int i;
14575614e71bSYork Sun 	unsigned int cas_latency;
14585614e71bSYork Sun 	unsigned int additive_latency;
14595614e71bSYork Sun 	unsigned int sr_it;
14605614e71bSYork Sun 	unsigned int zq_en;
14615614e71bSYork Sun 	unsigned int wrlvl_en;
14625614e71bSYork Sun 	unsigned int ip_rev = 0;
14635614e71bSYork Sun 	unsigned int unq_mrs_en = 0;
14645614e71bSYork Sun 	int cs_en = 1;
14655614e71bSYork Sun 
14665614e71bSYork Sun 	memset(ddr, 0, sizeof(fsl_ddr_cfg_regs_t));
14675614e71bSYork Sun 
14685614e71bSYork Sun 	if (common_dimm == NULL) {
14695614e71bSYork Sun 		printf("Error: subset DIMM params struct null pointer\n");
14705614e71bSYork Sun 		return 1;
14715614e71bSYork Sun 	}
14725614e71bSYork Sun 
14735614e71bSYork Sun 	/*
14745614e71bSYork Sun 	 * Process overrides first.
14755614e71bSYork Sun 	 *
14765614e71bSYork Sun 	 * FIXME: somehow add dereated caslat to this
14775614e71bSYork Sun 	 */
14785614e71bSYork Sun 	cas_latency = (popts->cas_latency_override)
14795614e71bSYork Sun 		? popts->cas_latency_override_value
14805614e71bSYork Sun 		: common_dimm->lowest_common_SPD_caslat;
14815614e71bSYork Sun 
14825614e71bSYork Sun 	additive_latency = (popts->additive_latency_override)
14835614e71bSYork Sun 		? popts->additive_latency_override_value
14845614e71bSYork Sun 		: common_dimm->additive_latency;
14855614e71bSYork Sun 
14865614e71bSYork Sun 	sr_it = (popts->auto_self_refresh_en)
14875614e71bSYork Sun 		? popts->sr_it
14885614e71bSYork Sun 		: 0;
14895614e71bSYork Sun 	/* ZQ calibration */
14905614e71bSYork Sun 	zq_en = (popts->zq_en) ? 1 : 0;
14915614e71bSYork Sun 	/* write leveling */
14925614e71bSYork Sun 	wrlvl_en = (popts->wrlvl_en) ? 1 : 0;
14935614e71bSYork Sun 
14945614e71bSYork Sun 	/* Chip Select Memory Bounds (CSn_BNDS) */
14955614e71bSYork Sun 	for (i = 0; i < CONFIG_CHIP_SELECTS_PER_CTRL; i++) {
14965614e71bSYork Sun 		unsigned long long ea, sa;
14975614e71bSYork Sun 		unsigned int cs_per_dimm
14985614e71bSYork Sun 			= CONFIG_CHIP_SELECTS_PER_CTRL / CONFIG_DIMM_SLOTS_PER_CTLR;
14995614e71bSYork Sun 		unsigned int dimm_number
15005614e71bSYork Sun 			= i / cs_per_dimm;
15015614e71bSYork Sun 		unsigned long long rank_density
15025614e71bSYork Sun 			= dimm_params[dimm_number].rank_density >> dbw_cap_adj;
15035614e71bSYork Sun 
15045614e71bSYork Sun 		if (dimm_params[dimm_number].n_ranks == 0) {
15055614e71bSYork Sun 			debug("Skipping setup of CS%u "
15065614e71bSYork Sun 				"because n_ranks on DIMM %u is 0\n", i, dimm_number);
15075614e71bSYork Sun 			continue;
15085614e71bSYork Sun 		}
15095614e71bSYork Sun 		if (popts->memctl_interleaving) {
15105614e71bSYork Sun 			switch (popts->ba_intlv_ctl & FSL_DDR_CS0_CS1_CS2_CS3) {
15115614e71bSYork Sun 			case FSL_DDR_CS0_CS1_CS2_CS3:
15125614e71bSYork Sun 				break;
15135614e71bSYork Sun 			case FSL_DDR_CS0_CS1:
15145614e71bSYork Sun 			case FSL_DDR_CS0_CS1_AND_CS2_CS3:
15155614e71bSYork Sun 				if (i > 1)
15165614e71bSYork Sun 					cs_en = 0;
15175614e71bSYork Sun 				break;
15185614e71bSYork Sun 			case FSL_DDR_CS2_CS3:
15195614e71bSYork Sun 			default:
15205614e71bSYork Sun 				if (i > 0)
15215614e71bSYork Sun 					cs_en = 0;
15225614e71bSYork Sun 				break;
15235614e71bSYork Sun 			}
15245614e71bSYork Sun 			sa = common_dimm->base_address;
15255614e71bSYork Sun 			ea = sa + common_dimm->total_mem - 1;
15265614e71bSYork Sun 		} else if (!popts->memctl_interleaving) {
15275614e71bSYork Sun 			/*
15285614e71bSYork Sun 			 * If memory interleaving between controllers is NOT
15295614e71bSYork Sun 			 * enabled, the starting address for each memory
15305614e71bSYork Sun 			 * controller is distinct.  However, because rank
15315614e71bSYork Sun 			 * interleaving is enabled, the starting and ending
15325614e71bSYork Sun 			 * addresses of the total memory on that memory
15335614e71bSYork Sun 			 * controller needs to be programmed into its
15345614e71bSYork Sun 			 * respective CS0_BNDS.
15355614e71bSYork Sun 			 */
15365614e71bSYork Sun 			switch (popts->ba_intlv_ctl & FSL_DDR_CS0_CS1_CS2_CS3) {
15375614e71bSYork Sun 			case FSL_DDR_CS0_CS1_CS2_CS3:
15385614e71bSYork Sun 				sa = common_dimm->base_address;
15395614e71bSYork Sun 				ea = sa + common_dimm->total_mem - 1;
15405614e71bSYork Sun 				break;
15415614e71bSYork Sun 			case FSL_DDR_CS0_CS1_AND_CS2_CS3:
15425614e71bSYork Sun 				if ((i >= 2) && (dimm_number == 0)) {
15435614e71bSYork Sun 					sa = dimm_params[dimm_number].base_address +
15445614e71bSYork Sun 					      2 * rank_density;
15455614e71bSYork Sun 					ea = sa + 2 * rank_density - 1;
15465614e71bSYork Sun 				} else {
15475614e71bSYork Sun 					sa = dimm_params[dimm_number].base_address;
15485614e71bSYork Sun 					ea = sa + 2 * rank_density - 1;
15495614e71bSYork Sun 				}
15505614e71bSYork Sun 				break;
15515614e71bSYork Sun 			case FSL_DDR_CS0_CS1:
15525614e71bSYork Sun 				if (dimm_params[dimm_number].n_ranks > (i % cs_per_dimm)) {
15535614e71bSYork Sun 					sa = dimm_params[dimm_number].base_address;
15545614e71bSYork Sun 					ea = sa + rank_density - 1;
15555614e71bSYork Sun 					if (i != 1)
15565614e71bSYork Sun 						sa += (i % cs_per_dimm) * rank_density;
15575614e71bSYork Sun 					ea += (i % cs_per_dimm) * rank_density;
15585614e71bSYork Sun 				} else {
15595614e71bSYork Sun 					sa = 0;
15605614e71bSYork Sun 					ea = 0;
15615614e71bSYork Sun 				}
15625614e71bSYork Sun 				if (i == 0)
15635614e71bSYork Sun 					ea += rank_density;
15645614e71bSYork Sun 				break;
15655614e71bSYork Sun 			case FSL_DDR_CS2_CS3:
15665614e71bSYork Sun 				if (dimm_params[dimm_number].n_ranks > (i % cs_per_dimm)) {
15675614e71bSYork Sun 					sa = dimm_params[dimm_number].base_address;
15685614e71bSYork Sun 					ea = sa + rank_density - 1;
15695614e71bSYork Sun 					if (i != 3)
15705614e71bSYork Sun 						sa += (i % cs_per_dimm) * rank_density;
15715614e71bSYork Sun 					ea += (i % cs_per_dimm) * rank_density;
15725614e71bSYork Sun 				} else {
15735614e71bSYork Sun 					sa = 0;
15745614e71bSYork Sun 					ea = 0;
15755614e71bSYork Sun 				}
15765614e71bSYork Sun 				if (i == 2)
15775614e71bSYork Sun 					ea += (rank_density >> dbw_cap_adj);
15785614e71bSYork Sun 				break;
15795614e71bSYork Sun 			default:  /* No bank(chip-select) interleaving */
15805614e71bSYork Sun 				sa = dimm_params[dimm_number].base_address;
15815614e71bSYork Sun 				ea = sa + rank_density - 1;
15825614e71bSYork Sun 				if (dimm_params[dimm_number].n_ranks > (i % cs_per_dimm)) {
15835614e71bSYork Sun 					sa += (i % cs_per_dimm) * rank_density;
15845614e71bSYork Sun 					ea += (i % cs_per_dimm) * rank_density;
15855614e71bSYork Sun 				} else {
15865614e71bSYork Sun 					sa = 0;
15875614e71bSYork Sun 					ea = 0;
15885614e71bSYork Sun 				}
15895614e71bSYork Sun 				break;
15905614e71bSYork Sun 			}
15915614e71bSYork Sun 		}
15925614e71bSYork Sun 
15935614e71bSYork Sun 		sa >>= 24;
15945614e71bSYork Sun 		ea >>= 24;
15955614e71bSYork Sun 
15965614e71bSYork Sun 		if (cs_en) {
15975614e71bSYork Sun 			ddr->cs[i].bnds = (0
1598*d4263b8aSYork Sun 				| ((sa & 0xffff) << 16) /* starting address */
1599*d4263b8aSYork Sun 				| ((ea & 0xffff) << 0)	/* ending address */
16005614e71bSYork Sun 				);
16015614e71bSYork Sun 		} else {
16025614e71bSYork Sun 			/* setting bnds to 0xffffffff for inactive CS */
16035614e71bSYork Sun 			ddr->cs[i].bnds = 0xffffffff;
16045614e71bSYork Sun 		}
16055614e71bSYork Sun 
16065614e71bSYork Sun 		debug("FSLDDR: cs[%d]_bnds = 0x%08x\n", i, ddr->cs[i].bnds);
16075614e71bSYork Sun 		set_csn_config(dimm_number, i, ddr, popts, dimm_params);
16085614e71bSYork Sun 		set_csn_config_2(i, ddr);
16095614e71bSYork Sun 	}
16105614e71bSYork Sun 
16115614e71bSYork Sun 	/*
16125614e71bSYork Sun 	 * In the case we only need to compute the ddr sdram size, we only need
16135614e71bSYork Sun 	 * to set csn registers, so return from here.
16145614e71bSYork Sun 	 */
16155614e71bSYork Sun 	if (size_only)
16165614e71bSYork Sun 		return 0;
16175614e71bSYork Sun 
16185614e71bSYork Sun 	set_ddr_eor(ddr, popts);
16195614e71bSYork Sun 
16205614e71bSYork Sun #if !defined(CONFIG_SYS_FSL_DDR1)
16215614e71bSYork Sun 	set_timing_cfg_0(ddr, popts, dimm_params);
16225614e71bSYork Sun #endif
16235614e71bSYork Sun 
1624*d4263b8aSYork Sun 	set_timing_cfg_3(ddr, popts, common_dimm, cas_latency,
1625*d4263b8aSYork Sun 			 additive_latency);
16265614e71bSYork Sun 	set_timing_cfg_1(ddr, popts, common_dimm, cas_latency);
16275614e71bSYork Sun 	set_timing_cfg_2(ddr, popts, common_dimm,
16285614e71bSYork Sun 				cas_latency, additive_latency);
16295614e71bSYork Sun 
16305614e71bSYork Sun 	set_ddr_cdr1(ddr, popts);
16315614e71bSYork Sun 	set_ddr_cdr2(ddr, popts);
16325614e71bSYork Sun 	set_ddr_sdram_cfg(ddr, popts, common_dimm);
16335614e71bSYork Sun 	ip_rev = fsl_ddr_get_version();
16345614e71bSYork Sun 	if (ip_rev > 0x40400)
16355614e71bSYork Sun 		unq_mrs_en = 1;
16365614e71bSYork Sun 
16375614e71bSYork Sun 	set_ddr_sdram_cfg_2(ddr, popts, unq_mrs_en);
16385614e71bSYork Sun 	set_ddr_sdram_mode(ddr, popts, common_dimm,
16395614e71bSYork Sun 				cas_latency, additive_latency, unq_mrs_en);
16405614e71bSYork Sun 	set_ddr_sdram_mode_2(ddr, popts, common_dimm, unq_mrs_en);
16415614e71bSYork Sun 	set_ddr_sdram_interval(ddr, popts, common_dimm);
16425614e71bSYork Sun 	set_ddr_data_init(ddr);
16435614e71bSYork Sun 	set_ddr_sdram_clk_cntl(ddr, popts);
16445614e71bSYork Sun 	set_ddr_init_addr(ddr);
16455614e71bSYork Sun 	set_ddr_init_ext_addr(ddr);
16465614e71bSYork Sun 	set_timing_cfg_4(ddr, popts);
16475614e71bSYork Sun 	set_timing_cfg_5(ddr, cas_latency);
16485614e71bSYork Sun 
16495614e71bSYork Sun 	set_ddr_zq_cntl(ddr, zq_en);
16505614e71bSYork Sun 	set_ddr_wrlvl_cntl(ddr, wrlvl_en, popts);
16515614e71bSYork Sun 
16525614e71bSYork Sun 	set_ddr_sr_cntr(ddr, sr_it);
16535614e71bSYork Sun 
16545614e71bSYork Sun 	set_ddr_sdram_rcw(ddr, popts, common_dimm);
16555614e71bSYork Sun 
16565614e71bSYork Sun #ifdef CONFIG_SYS_FSL_DDR_EMU
16575614e71bSYork Sun 	/* disble DDR training for emulator */
16585614e71bSYork Sun 	ddr->debug[2] = 0x00000400;
16595614e71bSYork Sun 	ddr->debug[4] = 0xff800000;
16605614e71bSYork Sun #endif
16615614e71bSYork Sun 	return check_fsl_memctl_config_regs(ddr);
16625614e71bSYork Sun }
1663