Skip to content
Snippets Groups Projects
ctrl_regs.c 69.1 KiB
Newer Older
  • Learn to ignore specific revisions
  •  * Copyright 2008-2014 Freescale Semiconductor, Inc.
    
     * SPDX-License-Identifier:	GPL-2.0+
    
     */
    
    /*
     * Generic driver for Freescale DDR/DDR2/DDR3 memory controller.
     * Based on code from spd_sdram.c
     * Author: James Yang [at freescale.com]
     */
    
    #include <common.h>
    
    #include <fsl_ddr_sdram.h>
    
    #include <fsl_immap.h>
    
    
    /*
     * Determine Rtt value.
     *
     * This should likely be either board or controller specific.
     *
    
     * Rtt(nominal) - DDR2:
    
     *	0 = Rtt disabled
     *	1 = 75 ohm
     *	2 = 150 ohm
     *	3 = 50 ohm
    
     * Rtt(nominal) - DDR3:
     *	0 = Rtt disabled
     *	1 = 60 ohm
     *	2 = 120 ohm
     *	3 = 40 ohm
     *	4 = 20 ohm
     *	5 = 30 ohm
    
     *
     * FIXME: Apparently 8641 needs a value of 2
     * FIXME: Old code seys if 667 MHz or higher, use 3 on 8572
     *
     * FIXME: There was some effort down this line earlier:
     *
     *	unsigned int i;
     *	for (i = 0; i < CONFIG_CHIP_SELECTS_PER_CTRL/2; i++) {
     *		if (popts->dimmslot[i].num_valid_cs
     *		    && (popts->cs_local_opts[2*i].odt_rd_cfg
     *			|| popts->cs_local_opts[2*i].odt_wr_cfg)) {
     *			rtt = 2;
     *			break;
     *		}
     *	}
     */
    static inline int fsl_ddr_get_rtt(void)
    {
    	int rtt;
    
    
    #if defined(CONFIG_SYS_FSL_DDR1)
    
    #elif defined(CONFIG_SYS_FSL_DDR2)
    
    #ifdef CONFIG_SYS_FSL_DDR4
    /*
     * compute CAS write latency according to DDR4 spec
     * CWL = 9 for <= 1600MT/s
     *       10 for <= 1866MT/s
     *       11 for <= 2133MT/s
     *       12 for <= 2400MT/s
     *       14 for <= 2667MT/s
     *       16 for <= 2933MT/s
     *       18 for higher
     */
    
    static inline unsigned int compute_cas_write_latency(
    				const unsigned int ctrl_num)
    
    	const unsigned int mclk_ps = get_memory_clk_period_ps(ctrl_num);
    
    	if (mclk_ps >= 1250)
    		cwl = 9;
    	else if (mclk_ps >= 1070)
    		cwl = 10;
    	else if (mclk_ps >= 935)
    		cwl = 11;
    	else if (mclk_ps >= 833)
    		cwl = 12;
    	else if (mclk_ps >= 750)
    		cwl = 14;
    	else if (mclk_ps >= 681)
    		cwl = 16;
    	else
    		cwl = 18;
    
    	return cwl;
    }
    #else
    
    /*
     * compute the CAS write latency according to DDR3 spec
     * CWL = 5 if tCK >= 2.5ns
     *       6 if 2.5ns > tCK >= 1.875ns
     *       7 if 1.875ns > tCK >= 1.5ns
     *       8 if 1.5ns > tCK >= 1.25ns
    
     *       9 if 1.25ns > tCK >= 1.07ns
     *       10 if 1.07ns > tCK >= 0.935ns
     *       11 if 0.935ns > tCK >= 0.833ns
     *       12 if 0.833ns > tCK >= 0.75ns
    
    static inline unsigned int compute_cas_write_latency(
    				const unsigned int ctrl_num)
    
    {
    	unsigned int cwl;
    
    	const unsigned int mclk_ps = get_memory_clk_period_ps(ctrl_num);
    
    
    	if (mclk_ps >= 2500)
    		cwl = 5;
    	else if (mclk_ps >= 1875)
    		cwl = 6;
    	else if (mclk_ps >= 1500)
    		cwl = 7;
    	else if (mclk_ps >= 1250)
    		cwl = 8;
    
    	else if (mclk_ps >= 1070)
    		cwl = 9;
    	else if (mclk_ps >= 935)
    		cwl = 10;
    	else if (mclk_ps >= 833)
    		cwl = 11;
    	else if (mclk_ps >= 750)
    		cwl = 12;
    	else {
    		cwl = 12;
    		printf("Warning: CWL is out of range\n");
    	}
    
    /* Chip Select Configuration (CSn_CONFIG) */
    
    static void set_csn_config(int dimm_number, int i, fsl_ddr_cfg_regs_t *ddr,
    
    			       const memctl_options_t *popts,
    			       const dimm_params_t *dimm_params)
    {
    	unsigned int cs_n_en = 0; /* Chip Select enable */
    	unsigned int intlv_en = 0; /* Memory controller interleave enable */
    	unsigned int intlv_ctl = 0; /* Interleaving control */
    	unsigned int ap_n_en = 0; /* Chip select n auto-precharge enable */
    	unsigned int odt_rd_cfg = 0; /* ODT for reads configuration */
    	unsigned int odt_wr_cfg = 0; /* ODT for writes configuration */
    	unsigned int ba_bits_cs_n = 0; /* Num of bank bits for SDRAM on CSn */
    	unsigned int row_bits_cs_n = 0; /* Num of row bits for SDRAM on CSn */
    	unsigned int col_bits_cs_n = 0; /* Num of ocl bits for SDRAM on CSn */
    
    	int go_config = 0;
    
    #ifdef CONFIG_SYS_FSL_DDR4
    	unsigned int bg_bits_cs_n = 0; /* Num of bank group bits */
    #else
    	unsigned int n_banks_per_sdram_device;
    #endif
    
    
    	/* Compute CS_CONFIG only for existing ranks of each DIMM.  */
    
    	switch (i) {
    	case 0:
    		if (dimm_params[dimm_number].n_ranks > 0) {
    			go_config = 1;
    
    			/* These fields only available in CS0_CONFIG */
    
    			if (!popts->memctl_interleaving)
    				break;
    			switch (popts->memctl_interleaving_mode) {
    
    			case FSL_DDR_256B_INTERLEAVING:
    
    			case FSL_DDR_CACHE_LINE_INTERLEAVING:
    			case FSL_DDR_PAGE_INTERLEAVING:
    			case FSL_DDR_BANK_INTERLEAVING:
    			case FSL_DDR_SUPERBANK_INTERLEAVING:
    				intlv_en = popts->memctl_interleaving;
    				intlv_ctl = popts->memctl_interleaving_mode;
    				break;
    			default:
    				break;
    			}
    
    		break;
    	case 1:
    		if ((dimm_number == 0 && dimm_params[0].n_ranks > 1) || \
    		    (dimm_number == 1 && dimm_params[1].n_ranks > 0))
    			go_config = 1;
    		break;
    	case 2:
    		if ((dimm_number == 0 && dimm_params[0].n_ranks > 2) || \
    
    		   (dimm_number >= 1 && dimm_params[dimm_number].n_ranks > 0))
    
    			go_config = 1;
    		break;
    	case 3:
    		if ((dimm_number == 0 && dimm_params[0].n_ranks > 3) || \
    		    (dimm_number == 1 && dimm_params[1].n_ranks > 1) || \
    		    (dimm_number == 3 && dimm_params[3].n_ranks > 0))
    			go_config = 1;
    		break;
    	default:
    		break;
    	}
    	if (go_config) {
    		cs_n_en = 1;
    
    		ap_n_en = popts->cs_local_opts[i].auto_precharge;
    		odt_rd_cfg = popts->cs_local_opts[i].odt_rd_cfg;
    		odt_wr_cfg = popts->cs_local_opts[i].odt_wr_cfg;
    
    #ifdef CONFIG_SYS_FSL_DDR4
    		ba_bits_cs_n = dimm_params[dimm_number].bank_addr_bits;
    		bg_bits_cs_n = dimm_params[dimm_number].bank_group_bits;
    #else
    
    		n_banks_per_sdram_device
    
    			= dimm_params[dimm_number].n_banks_per_sdram_device;
    
    		ba_bits_cs_n = __ilog2(n_banks_per_sdram_device) - 2;
    
    		row_bits_cs_n = dimm_params[dimm_number].n_row_addr - 12;
    		col_bits_cs_n = dimm_params[dimm_number].n_col_addr - 8;
    
    	}
    	ddr->cs[i].config = (0
    		| ((cs_n_en & 0x1) << 31)
    		| ((intlv_en & 0x3) << 29)
    
    		| ((intlv_ctl & 0xf) << 24)
    
    		| ((ap_n_en & 0x1) << 23)
    
    		/* XXX: some implementation only have 1 bit starting at left */
    		| ((odt_rd_cfg & 0x7) << 20)
    
    		/* XXX: Some implementation only have 1 bit starting at left */
    		| ((odt_wr_cfg & 0x7) << 16)
    
    		| ((ba_bits_cs_n & 0x3) << 14)
    		| ((row_bits_cs_n & 0x7) << 8)
    
    #ifdef CONFIG_SYS_FSL_DDR4
    		| ((bg_bits_cs_n & 0x3) << 4)
    #endif
    
    		| ((col_bits_cs_n & 0x7) << 0)
    		);
    
    	debug("FSLDDR: cs[%d]_config = 0x%08x\n", i,ddr->cs[i].config);
    
    }
    
    /* Chip Select Configuration 2 (CSn_CONFIG_2) */
    /* FIXME: 8572 */
    static void set_csn_config_2(int i, fsl_ddr_cfg_regs_t *ddr)
    {
    	unsigned int pasr_cfg = 0;	/* Partial array self refresh config */
    
    	ddr->cs[i].config_2 = ((pasr_cfg & 7) << 24);
    
    	debug("FSLDDR: cs[%d]_config_2 = 0x%08x\n", i, ddr->cs[i].config_2);
    
    }
    
    /* -3E = 667 CL5, -25 = CL6 800, -25E = CL5 800 */
    
    
    #if !defined(CONFIG_SYS_FSL_DDR1)
    
    /*
     * Check DIMM configuration, return 2 if quad-rank or two dual-rank
     * Return 1 if other two slots configuration. Return 0 if single slot.
     */
    
    static inline int avoid_odt_overlap(const dimm_params_t *dimm_params)
    {
    #if CONFIG_DIMM_SLOTS_PER_CTLR == 1
    	if (dimm_params[0].n_ranks == 4)
    
    #endif
    
    #if CONFIG_DIMM_SLOTS_PER_CTLR == 2
    	if ((dimm_params[0].n_ranks == 2) &&
    		(dimm_params[1].n_ranks == 2))
    
    
    #ifdef CONFIG_FSL_DDR_FIRST_SLOT_QUAD_CAPABLE
    	if (dimm_params[0].n_ranks == 4)
    
    
    	if ((dimm_params[0].n_ranks != 0) &&
    	    (dimm_params[2].n_ranks != 0))
    		return 1;
    
    /*
     * DDR SDRAM Timing Configuration 0 (TIMING_CFG_0)
     *
     * Avoid writing for DDR I.  The new PQ38 DDR controller
     * dreams up non-zero default values to be backwards compatible.
     */
    
    static void set_timing_cfg_0(const unsigned int ctrl_num,
    				fsl_ddr_cfg_regs_t *ddr,
    
    				const memctl_options_t *popts,
    				const dimm_params_t *dimm_params)
    
    {
    	unsigned char trwt_mclk = 0;   /* Read-to-write turnaround */
    	unsigned char twrt_mclk = 0;   /* Write-to-read turnaround */
    	/* 7.5 ns on -3E; 0 means WL - CL + BL/2 + 1 */
    	unsigned char trrt_mclk = 0;   /* Read-to-read turnaround */
    	unsigned char twwt_mclk = 0;   /* Write-to-write turnaround */
    
    	/* Active powerdown exit timing (tXARD and tXARDS). */
    	unsigned char act_pd_exit_mclk;
    	/* Precharge powerdown exit timing (tXP). */
    	unsigned char pre_pd_exit_mclk;
    
    	/* ODT powerdown exit timing (tAXPD). */
    
    	unsigned char taxpd_mclk = 0;
    
    	/* Mode register set cycle time (tMRD). */
    	unsigned char tmrd_mclk;
    
    #if defined(CONFIG_SYS_FSL_DDR4) || defined(CONFIG_SYS_FSL_DDR3)
    
    	const unsigned int mclk_ps = get_memory_clk_period_ps(ctrl_num);
    
    #endif
    
    #ifdef CONFIG_SYS_FSL_DDR4
    	/* tXP=max(4nCK, 6ns) */
    
    	int txp = max((int)mclk_ps * 4, 6000); /* unit=ps */
    
    	unsigned int data_rate = get_ddr_freq(ctrl_num);
    
    	/* for faster clock, need more time for data setup */
    	trwt_mclk = (data_rate/1000000 > 1900) ? 3 : 2;
    
    
    	/*
    	 * for single quad-rank DIMM and two-slot DIMMs
    	 * to avoid ODT overlap
    	 */
    	switch (avoid_odt_overlap(dimm_params)) {
    	case 2:
    		twrt_mclk = 2;
    		twwt_mclk = 2;
    		trrt_mclk = 2;
    		break;
    	default:
    		twrt_mclk = 1;
    		twwt_mclk = 1;
    		trrt_mclk = 0;
    		break;
    	}
    
    
    	act_pd_exit_mclk = picos_to_mclk(ctrl_num, txp);
    
    	pre_pd_exit_mclk = act_pd_exit_mclk;
    	/*
    	 * MRS_CYC = max(tMRD, tMOD)
    	 * tMRD = 8nCK, tMOD = max(24nCK, 15ns)
    	 */
    
    	tmrd_mclk = max(24U, picos_to_mclk(ctrl_num, 15000));
    
    #elif defined(CONFIG_SYS_FSL_DDR3)
    
    	unsigned int data_rate = get_ddr_freq(ctrl_num);
    
    	int txp;
    
    	unsigned int ip_rev;
    
    	/*
    	 * (tXARD and tXARDS). Empirical?
    	 * The DDR3 spec has not tXARD,
    	 * we use the tXP instead of it.
    
    	 * tXP=max(3nCK, 7.5ns) for DDR3-800, 1066
    	 *     max(3nCK, 6ns) for DDR3-1333, 1600, 1866, 2133
    
    	 * spec has not the tAXPD, we use
    
    	 * tAXPD=1, need design to confirm.
    
    	txp = max((int)mclk_ps * 3, (mclk_ps > 1540 ? 7500 : 6000));
    
    	ip_rev = fsl_ddr_get_version(ctrl_num);
    
    	if (ip_rev >= 0x40700) {
    		/*
    		 * MRS_CYC = max(tMRD, tMOD)
    		 * tMRD = 4nCK (8nCK for RDIMM)
    		 * tMOD = max(12nCK, 15ns)
    		 */
    
    		tmrd_mclk = max((unsigned int)12,
    				picos_to_mclk(ctrl_num, 15000));
    
    	} else {
    		/*
    		 * MRS_CYC = tMRD
    		 * tMRD = 4nCK (8nCK for RDIMM)
    		 */
    		if (popts->registered_dimm_en)
    			tmrd_mclk = 8;
    		else
    			tmrd_mclk = 4;
    	}
    
    
    	/* set the turnaround time */
    
    	 * for single quad-rank DIMM and two-slot DIMMs
    
    	odt_overlap = avoid_odt_overlap(dimm_params);
    	switch (odt_overlap) {
    	case 2:
    
    		break;
    	case 1:
    		twwt_mclk = 1;
    		trrt_mclk = 0;
    		break;
    	default:
    		break;
    
    	/* for faster clock, need more time for data setup */
    	trwt_mclk = (data_rate/1000000 > 1800) ? 2 : 1;
    
    
    	if ((data_rate/1000000 > 1150) || (popts->memctl_interleaving))
    		twrt_mclk = 1;
    
    
    	if (popts->dynamic_power == 0) {	/* powerdown is not used */
    		act_pd_exit_mclk = 1;
    		pre_pd_exit_mclk = 1;
    		taxpd_mclk = 1;
    	} else {
    		/* act_pd_exit_mclk = tXARD, see above */
    
    		act_pd_exit_mclk = picos_to_mclk(ctrl_num, txp);
    
    		/* Mode register MR0[A12] is '1' - fast exit */
    		pre_pd_exit_mclk = act_pd_exit_mclk;
    		taxpd_mclk = 1;
    	}
    
    #else /* CONFIG_SYS_FSL_DDR2 */
    
    	/*
    	 * (tXARD and tXARDS). Empirical?
    	 * tXARD = 2 for DDR2
    	 * tXP=2
    	 * tAXPD=8
    	 */
    	act_pd_exit_mclk = 2;
    	pre_pd_exit_mclk = 2;
    	taxpd_mclk = 8;
    
    	if (popts->trwt_override)
    		trwt_mclk = popts->trwt;
    
    
    	ddr->timing_cfg_0 = (0
    		| ((trwt_mclk & 0x3) << 30)	/* RWT */
    		| ((twrt_mclk & 0x3) << 28)	/* WRT */
    		| ((trrt_mclk & 0x3) << 26)	/* RRT */
    		| ((twwt_mclk & 0x3) << 24)	/* WWT */
    
    		| ((act_pd_exit_mclk & 0xf) << 20)  /* ACT_PD_EXIT */
    
    		| ((pre_pd_exit_mclk & 0xF) << 16)  /* PRE_PD_EXIT */
    
    		| ((taxpd_mclk & 0xf) << 8)	/* ODT_PD_EXIT */
    
    		| ((tmrd_mclk & 0x1f) << 0)	/* MRS_CYC */
    
    		);
    	debug("FSLDDR: timing_cfg_0 = 0x%08x\n", ddr->timing_cfg_0);
    }
    
    #endif	/* !defined(CONFIG_SYS_FSL_DDR1) */
    
    
    /* DDR SDRAM Timing Configuration 3 (TIMING_CFG_3) */
    
    static void set_timing_cfg_3(const unsigned int ctrl_num,
    			     fsl_ddr_cfg_regs_t *ddr,
    			     const memctl_options_t *popts,
    			     const common_timing_params_t *common_dimm,
    			     unsigned int cas_latency,
    			     unsigned int additive_latency)
    
    	/* Extended precharge to activate interval (tRP) */
    	unsigned int ext_pretoact = 0;
    
    	/* Extended Activate to precharge interval (tRAS) */
    	unsigned int ext_acttopre = 0;
    
    	/* Extended activate to read/write interval (tRCD) */
    	unsigned int ext_acttorw = 0;
    	/* Extended refresh recovery time (tRFC) */
    	unsigned int ext_refrec;
    	/* Extended MCAS latency from READ cmd */
    	unsigned int ext_caslat = 0;
    
    	/* Extended additive latency */
    	unsigned int ext_add_lat = 0;
    
    	/* Extended last data to precharge interval (tWR) */
    	unsigned int ext_wrrec = 0;
    	/* Control Adjust */
    	unsigned int cntl_adj = 0;
    
    
    	ext_pretoact = picos_to_mclk(ctrl_num, common_dimm->trp_ps) >> 4;
    	ext_acttopre = picos_to_mclk(ctrl_num, common_dimm->tras_ps) >> 4;
    	ext_acttorw = picos_to_mclk(ctrl_num, common_dimm->trcd_ps) >> 4;
    
    	ext_caslat = (2 * cas_latency - 1) >> 4;
    
    	ext_add_lat = additive_latency >> 4;
    
    #ifdef CONFIG_SYS_FSL_DDR4
    
    	ext_refrec = (picos_to_mclk(ctrl_num, common_dimm->trfc1_ps) - 8) >> 4;
    
    	ext_refrec = (picos_to_mclk(ctrl_num, common_dimm->trfc_ps) - 8) >> 4;
    
    	/* ext_wrrec only deals with 16 clock and above, or 14 with OTF */
    
    	ext_wrrec = (picos_to_mclk(ctrl_num, common_dimm->twr_ps) +
    
    		(popts->otf_burst_chop_en ? 2 : 0)) >> 4;
    
    		| ((ext_pretoact & 0x1) << 28)
    
    		| ((ext_acttopre & 0x3) << 24)
    
    		| ((ext_acttorw & 0x1) << 22)
    		| ((ext_refrec & 0x1F) << 16)
    		| ((ext_caslat & 0x3) << 12)
    
    		| ((ext_add_lat & 0x1) << 10)
    
    		| ((ext_wrrec & 0x1) << 8)
    
    		| ((cntl_adj & 0x7) << 0)
    		);
    
    	debug("FSLDDR: timing_cfg_3 = 0x%08x\n", ddr->timing_cfg_3);
    
    }
    
    /* DDR SDRAM Timing Configuration 1 (TIMING_CFG_1) */
    
    static void set_timing_cfg_1(const unsigned int ctrl_num,
    			     fsl_ddr_cfg_regs_t *ddr,
    			     const memctl_options_t *popts,
    			     const common_timing_params_t *common_dimm,
    			     unsigned int cas_latency)
    
    {
    	/* Precharge-to-activate interval (tRP) */
    	unsigned char pretoact_mclk;
    	/* Activate to precharge interval (tRAS) */
    	unsigned char acttopre_mclk;
    	/*  Activate to read/write interval (tRCD) */
    	unsigned char acttorw_mclk;
    	/* CASLAT */
    	unsigned char caslat_ctrl;
    	/*  Refresh recovery time (tRFC) ; trfc_low */
    	unsigned char refrec_ctrl;
    	/* Last data to precharge minimum interval (tWR) */
    	unsigned char wrrec_mclk;
    	/* Activate-to-activate interval (tRRD) */
    	unsigned char acttoact_mclk;
    	/* Last write data pair to read command issue interval (tWTR) */
    	unsigned char wrtord_mclk;
    
    #ifdef CONFIG_SYS_FSL_DDR4
    	/* DDR4 supports 10, 12, 14, 16, 18, 20, 24 */
    	static const u8 wrrec_table[] = {
    		10, 10, 10, 10, 10,
    		10, 10, 10, 10, 10,
    		12, 12, 14, 14, 16,
    		16, 18, 18, 20, 20,
    		24, 24, 24, 24};
    #else
    
    	/* DDR_SDRAM_MODE doesn't support 9,11,13,15 */
    	static const u8 wrrec_table[] = {
    		1, 2, 3, 4, 5, 6, 7, 8, 10, 10, 12, 12, 14, 14, 0, 0};
    
    	pretoact_mclk = picos_to_mclk(ctrl_num, common_dimm->trp_ps);
    	acttopre_mclk = picos_to_mclk(ctrl_num, common_dimm->tras_ps);
    	acttorw_mclk = picos_to_mclk(ctrl_num, common_dimm->trcd_ps);
    
    
    	/*
    	 * Translate CAS Latency to a DDR controller field value:
    	 *
    	 *      CAS Lat DDR I   DDR II  Ctrl
    	 *      Clocks  SPD Bit SPD Bit Value
    	 *      ------- ------- ------- -----
    	 *      1.0     0               0001
    	 *      1.5     1               0010
    	 *      2.0     2       2       0011
    	 *      2.5     3               0100
    	 *      3.0     4       3       0101
    	 *      3.5     5               0110
    	 *      4.0             4       0111
    	 *      4.5                     1000
    	 *      5.0             5       1001
    	 */
    
    #if defined(CONFIG_SYS_FSL_DDR1)
    
    	caslat_ctrl = (cas_latency + 1) & 0x07;
    
    #elif defined(CONFIG_SYS_FSL_DDR2)
    
    	caslat_ctrl = 2 * cas_latency - 1;
    #else
    
    	/*
    	 * if the CAS latency more than 8 cycle,
    	 * we need set extend bit for it at
    	 * TIMING_CFG_3[EXT_CASLAT]
    	 */
    
    	if (fsl_ddr_get_version(ctrl_num) <= 0x40400)
    
    		caslat_ctrl = 2 * cas_latency - 1;
    	else
    		caslat_ctrl = (cas_latency - 1) << 1;
    
    #ifdef CONFIG_SYS_FSL_DDR4
    
    	refrec_ctrl = picos_to_mclk(ctrl_num, common_dimm->trfc1_ps) - 8;
    	wrrec_mclk = picos_to_mclk(ctrl_num, common_dimm->twr_ps);
    	acttoact_mclk = max(picos_to_mclk(ctrl_num, common_dimm->trrds_ps), 4U);
    	wrtord_mclk = max(2U, picos_to_mclk(ctrl_num, 2500));
    
    	if ((wrrec_mclk < 1) || (wrrec_mclk > 24))
    		printf("Error: WRREC doesn't support %d clocks\n", wrrec_mclk);
    
    	else
    		wrrec_mclk = wrrec_table[wrrec_mclk - 1];
    #else
    
    	refrec_ctrl = picos_to_mclk(ctrl_num, common_dimm->trfc_ps) - 8;
    	wrrec_mclk = picos_to_mclk(ctrl_num, common_dimm->twr_ps);
    	acttoact_mclk = picos_to_mclk(ctrl_num, common_dimm->trrd_ps);
    	wrtord_mclk = picos_to_mclk(ctrl_num, common_dimm->twtr_ps);
    
    	if ((wrrec_mclk < 1) || (wrrec_mclk > 16))
    		printf("Error: WRREC doesn't support %d clocks\n", wrrec_mclk);
    
    	else
    		wrrec_mclk = wrrec_table[wrrec_mclk - 1];
    
    	if (popts->otf_burst_chop_en)
    
    		wrrec_mclk += 2;
    
    	/*
    	 * JEDEC has min requirement for tRRD
    	 */
    
    #if defined(CONFIG_SYS_FSL_DDR3)
    
    	if (acttoact_mclk < 4)
    		acttoact_mclk = 4;
    #endif
    	/*
    	 * JEDEC has some min requirements for tWTR
    	 */
    
    #if defined(CONFIG_SYS_FSL_DDR2)
    
    	if (wrtord_mclk < 2)
    		wrtord_mclk = 2;
    
    #elif defined(CONFIG_SYS_FSL_DDR3)
    
    	if (wrtord_mclk < 4)
    		wrtord_mclk = 4;
    #endif
    
    	if (popts->otf_burst_chop_en)
    
    		wrtord_mclk += 2;
    
    		| ((pretoact_mclk & 0x0F) << 28)
    
    		| ((acttopre_mclk & 0x0F) << 24)
    
    		| ((acttorw_mclk & 0xF) << 20)
    
    		| ((caslat_ctrl & 0xF) << 16)
    		| ((refrec_ctrl & 0xF) << 12)
    
    		| ((wrrec_mclk & 0x0F) << 8)
    
    		| ((acttoact_mclk & 0x0F) << 4)
    		| ((wrtord_mclk & 0x0F) << 0)
    
    	debug("FSLDDR: timing_cfg_1 = 0x%08x\n", ddr->timing_cfg_1);
    
    }
    
    /* DDR SDRAM Timing Configuration 2 (TIMING_CFG_2) */
    
    static void set_timing_cfg_2(const unsigned int ctrl_num,
    			     fsl_ddr_cfg_regs_t *ddr,
    			     const memctl_options_t *popts,
    			     const common_timing_params_t *common_dimm,
    			     unsigned int cas_latency,
    			     unsigned int additive_latency)
    
    {
    	/* Additive latency */
    	unsigned char add_lat_mclk;
    	/* CAS-to-preamble override */
    	unsigned short cpo;
    	/* Write latency */
    	unsigned char wr_lat;
    	/*  Read to precharge (tRTP) */
    	unsigned char rd_to_pre;
    	/* Write command to write data strobe timing adjustment */
    	unsigned char wr_data_delay;
    	/* Minimum CKE pulse width (tCKE) */
    	unsigned char cke_pls;
    	/* Window for four activates (tFAW) */
    	unsigned short four_act;
    
    #ifdef CONFIG_SYS_FSL_DDR3
    
    	const unsigned int mclk_ps = get_memory_clk_period_ps(ctrl_num);
    
    #endif
    
    
    	/* FIXME add check that this must be less than acttorw_mclk */
    	add_lat_mclk = additive_latency;
    	cpo = popts->cpo_override;
    
    
    #if defined(CONFIG_SYS_FSL_DDR1)
    
    	/*
    	 * This is a lie.  It should really be 1, but if it is
    	 * set to 1, bits overlap into the old controller's
    	 * otherwise unused ACSM field.  If we leave it 0, then
    	 * the HW will magically treat it as 1 for DDR 1.  Oh Yea.
    	 */
    	wr_lat = 0;
    
    #elif defined(CONFIG_SYS_FSL_DDR2)
    
    	wr_lat = cas_latency - 1;
    
    	wr_lat = compute_cas_write_latency(ctrl_num);
    
    #ifdef CONFIG_SYS_FSL_DDR4
    
    	rd_to_pre = picos_to_mclk(ctrl_num, 7500);
    
    	rd_to_pre = picos_to_mclk(ctrl_num, common_dimm->trtp_ps);
    
    	/*
    	 * JEDEC has some min requirements for tRTP
    	 */
    
    #if defined(CONFIG_SYS_FSL_DDR2)
    
    	if (rd_to_pre  < 2)
    		rd_to_pre  = 2;
    
    #elif defined(CONFIG_SYS_FSL_DDR3) || defined(CONFIG_SYS_FSL_DDR4)
    
    	if (rd_to_pre < 4)
    		rd_to_pre = 4;
    
    	if (popts->otf_burst_chop_en)
    
    		rd_to_pre += 2; /* according to UM */
    
    
    	wr_data_delay = popts->write_data_delay;
    
    #ifdef CONFIG_SYS_FSL_DDR4
    	cpo = 0;
    
    	cke_pls = max(3U, picos_to_mclk(ctrl_num, 5000));
    
    #elif defined(CONFIG_SYS_FSL_DDR3)
    	/*
    	 * cke pulse = max(3nCK, 7.5ns) for DDR3-800
    	 *             max(3nCK, 5.625ns) for DDR3-1066, 1333
    	 *             max(3nCK, 5ns) for DDR3-1600, 1866, 2133
    	 */
    
    	cke_pls = max(3U, picos_to_mclk(ctrl_num, mclk_ps > 1870 ? 7500 :
    					(mclk_ps > 1245 ? 5625 : 5000)));
    
    	cke_pls = FSL_DDR_MIN_TCKE_PULSE_WIDTH_DDR;
    
    	four_act = picos_to_mclk(ctrl_num,
    				 popts->tfaw_window_four_activates_ps);
    
    		| ((add_lat_mclk & 0xf) << 28)
    
    		| ((wr_lat & 0xf) << 19)
    
    		| ((wr_lat & 0x10) << 14)
    
    		| ((rd_to_pre & RD_TO_PRE_MASK) << RD_TO_PRE_SHIFT)
    		| ((wr_data_delay & WR_DATA_DELAY_MASK) << WR_DATA_DELAY_SHIFT)
    
    		| ((cke_pls & 0x7) << 6)
    
    		| ((four_act & 0x3f) << 0)
    
    	debug("FSLDDR: timing_cfg_2 = 0x%08x\n", ddr->timing_cfg_2);
    
    /* DDR SDRAM Register Control Word */
    static void set_ddr_sdram_rcw(fsl_ddr_cfg_regs_t *ddr,
    
    			       const memctl_options_t *popts,
    
    			       const common_timing_params_t *common_dimm)
    {
    
    	if (common_dimm->all_dimms_registered &&
    	    !common_dimm->all_dimms_unbuffered)	{
    
    		if (popts->rcw_override) {
    			ddr->ddr_sdram_rcw_1 = popts->rcw_1;
    			ddr->ddr_sdram_rcw_2 = popts->rcw_2;
    		} else {
    			ddr->ddr_sdram_rcw_1 =
    				common_dimm->rcw[0] << 28 | \
    				common_dimm->rcw[1] << 24 | \
    				common_dimm->rcw[2] << 20 | \
    				common_dimm->rcw[3] << 16 | \
    				common_dimm->rcw[4] << 12 | \
    				common_dimm->rcw[5] << 8 | \
    				common_dimm->rcw[6] << 4 | \
    				common_dimm->rcw[7];
    			ddr->ddr_sdram_rcw_2 =
    				common_dimm->rcw[8] << 28 | \
    				common_dimm->rcw[9] << 24 | \
    				common_dimm->rcw[10] << 20 | \
    				common_dimm->rcw[11] << 16 | \
    				common_dimm->rcw[12] << 12 | \
    				common_dimm->rcw[13] << 8 | \
    				common_dimm->rcw[14] << 4 | \
    				common_dimm->rcw[15];
    		}
    
    		debug("FSLDDR: ddr_sdram_rcw_1 = 0x%08x\n", ddr->ddr_sdram_rcw_1);
    		debug("FSLDDR: ddr_sdram_rcw_2 = 0x%08x\n", ddr->ddr_sdram_rcw_2);
    	}
    }
    
    
    /* DDR SDRAM control configuration (DDR_SDRAM_CFG) */
    static void set_ddr_sdram_cfg(fsl_ddr_cfg_regs_t *ddr,
    			       const memctl_options_t *popts,
    			       const common_timing_params_t *common_dimm)
    {
    	unsigned int mem_en;		/* DDR SDRAM interface logic enable */
    	unsigned int sren;		/* Self refresh enable (during sleep) */
    	unsigned int ecc_en;		/* ECC enable. */
    	unsigned int rd_en;		/* Registered DIMM enable */
    	unsigned int sdram_type;	/* Type of SDRAM */
    	unsigned int dyn_pwr;		/* Dynamic power management mode */
    	unsigned int dbw;		/* DRAM dta bus width */
    
    	unsigned int eight_be = 0;	/* 8-beat burst enable, DDR2 is zero */
    
    	unsigned int ncap = 0;		/* Non-concurrent auto-precharge */
    
    	unsigned int threet_en;		/* Enable 3T timing */
    	unsigned int twot_en;		/* Enable 2T timing */
    
    	unsigned int ba_intlv_ctl;	/* Bank (CS) interleaving control */
    	unsigned int x32_en = 0;	/* x32 enable */
    	unsigned int pchb8 = 0;		/* precharge bit 8 enable */
    	unsigned int hse;		/* Global half strength override */
    
    	unsigned int acc_ecc_en = 0;	/* Accumulated ECC enable */
    
    	unsigned int mem_halt = 0;	/* memory controller halt */
    	unsigned int bi = 0;		/* Bypass initialization */
    
    	mem_en = 1;
    	sren = popts->self_refresh_in_sleep;
    
    	if (common_dimm->all_dimms_ecc_capable) {
    
    		/* Allow setting of ECC only if all DIMMs are ECC. */
    
    		ecc_en = popts->ecc_mode;
    
    	if (common_dimm->all_dimms_registered &&
    	    !common_dimm->all_dimms_unbuffered)	{
    
    		twot_en = popts->twot_en;
    
    
    	sdram_type = CONFIG_FSL_SDRAM_TYPE;
    
    	dyn_pwr = popts->dynamic_power;
    	dbw = popts->data_bus_width;
    
    	/* 8-beat burst enable DDR-III case
    	 * we must clear it when use the on-the-fly mode,
    	 * must set it when use the 32-bits bus mode.
    	 */
    
    	if ((sdram_type == SDRAM_TYPE_DDR3) ||
    	    (sdram_type == SDRAM_TYPE_DDR4)) {
    
    		if (popts->burst_length == DDR_BL8)
    			eight_be = 1;
    		if (popts->burst_length == DDR_OTF)
    			eight_be = 0;
    		if (dbw == 0x1)
    			eight_be = 1;
    	}
    
    
    	threet_en = popts->threet_en;
    
    	ba_intlv_ctl = popts->ba_intlv_ctl;
    	hse = popts->half_strength_driver_enable;
    
    
    	/* set when ddr bus width < 64 */
    	acc_ecc_en = (dbw != 0 && ecc_en == 1) ? 1 : 0;
    
    
    	ddr->ddr_sdram_cfg = (0
    			| ((mem_en & 0x1) << 31)
    			| ((sren & 0x1) << 30)
    			| ((ecc_en & 0x1) << 29)
    			| ((rd_en & 0x1) << 28)
    			| ((sdram_type & 0x7) << 24)
    			| ((dyn_pwr & 0x1) << 21)
    			| ((dbw & 0x3) << 19)
    			| ((eight_be & 0x1) << 18)
    			| ((ncap & 0x1) << 17)
    
    			| ((threet_en & 0x1) << 16)
    			| ((twot_en & 0x1) << 15)
    
    			| ((ba_intlv_ctl & 0x7F) << 8)
    			| ((x32_en & 0x1) << 5)
    			| ((pchb8 & 0x1) << 4)
    			| ((hse & 0x1) << 3)
    
    			| ((acc_ecc_en & 0x1) << 2)
    
    			| ((mem_halt & 0x1) << 1)
    			| ((bi & 0x1) << 0)
    			);
    
    	debug("FSLDDR: ddr_sdram_cfg = 0x%08x\n", ddr->ddr_sdram_cfg);
    
    }
    
    /* DDR SDRAM control configuration 2 (DDR_SDRAM_CFG_2) */
    
    static void set_ddr_sdram_cfg_2(const unsigned int ctrl_num,
    			       fsl_ddr_cfg_regs_t *ddr,
    
    			       const memctl_options_t *popts,
    			       const unsigned int unq_mrs_en)
    
    {
    	unsigned int frc_sr = 0;	/* Force self refresh */
    	unsigned int sr_ie = 0;		/* Self-refresh interrupt enable */
    
    	unsigned int odt_cfg = 0;	/* ODT configuration */
    
    	unsigned int num_pr;		/* Number of posted refreshes */
    
    	unsigned int slow = 0;		/* DDR will be run less than 1250 */
    
    	unsigned int x4_en = 0;		/* x4 DRAM enable */
    
    	unsigned int obc_cfg;		/* On-The-Fly Burst Chop Cfg */
    	unsigned int ap_en;		/* Address Parity Enable */
    	unsigned int d_init;		/* DRAM data initialization */
    	unsigned int rcw_en = 0;	/* Register Control Word Enable */
    	unsigned int md_en = 0;		/* Mirrored DIMM Enable */
    
    	unsigned int qd_en = 0;		/* quad-rank DIMM Enable */
    
    #ifndef CONFIG_SYS_FSL_DDR4
    	unsigned int dll_rst_dis = 1;	/* DLL reset disable */
    	unsigned int dqs_cfg;		/* DQS configuration */
    
    	dqs_cfg = popts->dqs_config;
    
    	for (i = 0; i < CONFIG_CHIP_SELECTS_PER_CTRL; i++) {
    		if (popts->cs_local_opts[i].odt_rd_cfg
    			|| popts->cs_local_opts[i].odt_wr_cfg) {
    			odt_cfg = SDRAM_CFG2_ODT_ONLY_READ;
    			break;
    		}
    
    	sr_ie = popts->self_refresh_interrupt_en;
    
    	num_pr = 1;	/* Make this configurable */
    
    	/*
    	 * 8572 manual says
    	 *     {TIMING_CFG_1[PRETOACT]
    	 *      + [DDR_SDRAM_CFG_2[NUM_PR]
    	 *        * ({EXT_REFREC || REFREC} + 8 + 2)]}
    	 *      << DDR_SDRAM_INTERVAL[REFINT]
    	 */
    
    #if defined(CONFIG_SYS_FSL_DDR3) || defined(CONFIG_SYS_FSL_DDR4)
    
    	obc_cfg = popts->otf_burst_chop_en;
    
    #else
    	obc_cfg = 0;
    #endif
    
    #if (CONFIG_SYS_FSL_DDR_VER >= FSL_DDR_VER_4_7)
    
    	slow = get_ddr_freq(ctrl_num) < 1249000000;
    
    	if (popts->registered_dimm_en) {
    		rcw_en = 1;
    		ap_en = popts->ap_en;
    	} else {
    		ap_en = 0;
    	}
    
    	x4_en = popts->x4_en ? 1 : 0;
    
    
    #if defined(CONFIG_ECC_INIT_VIA_DDRCONTROLLER)
    	/* Use the DDR controller to auto initialize memory. */
    
    	d_init = popts->ecc_init_using_memctl;
    
    	ddr->ddr_data_init = CONFIG_MEM_INIT_VALUE;
    	debug("DDR: ddr_data_init = 0x%08x\n", ddr->ddr_data_init);
    #else
    	/* Memory will be initialized via DMA, or not at all. */
    	d_init = 0;
    #endif
    
    
    #if defined(CONFIG_SYS_FSL_DDR3) || defined(CONFIG_SYS_FSL_DDR4)
    
    	md_en = popts->mirrored_dimm;
    #endif
    
    	qd_en = popts->quad_rank_present ? 1 : 0;
    
    	ddr->ddr_sdram_cfg_2 = (0
    		| ((frc_sr & 0x1) << 31)
    		| ((sr_ie & 0x1) << 30)
    
    #ifndef CONFIG_SYS_FSL_DDR4
    
    		| ((dll_rst_dis & 0x1) << 29)
    		| ((dqs_cfg & 0x3) << 26)
    
    		| ((odt_cfg & 0x3) << 21)
    		| ((num_pr & 0xf) << 12)
    
    		| ((slow & 1) << 11)
    
    		| (x4_en << 10)
    
    		| (qd_en << 9)
    
    		| ((obc_cfg & 0x1) << 6)
    		| ((ap_en & 0x1) << 5)
    		| ((d_init & 0x1) << 4)
    		| ((rcw_en & 0x1) << 2)
    		| ((md_en & 0x1) << 0)
    		);
    
    	debug("FSLDDR: ddr_sdram_cfg_2 = 0x%08x\n", ddr->ddr_sdram_cfg_2);
    
    #ifdef CONFIG_SYS_FSL_DDR4
    
    /* DDR SDRAM Mode configuration 2 (DDR_SDRAM_MODE_2) */
    
    static void set_ddr_sdram_mode_2(const unsigned int ctrl_num,
    				fsl_ddr_cfg_regs_t *ddr,
    
    				const memctl_options_t *popts,
    
    				const common_timing_params_t *common_dimm,
    
    				const unsigned int unq_mrs_en)
    
    {
    	unsigned short esdmode2 = 0;	/* Extended SDRAM mode 2 */
    	unsigned short esdmode3 = 0;	/* Extended SDRAM mode 3 */
    
    	int i;
    	unsigned int wr_crc = 0;	/* Disable */
    	unsigned int rtt_wr = 0;	/* Rtt_WR - dynamic ODT off */
    	unsigned int srt = 0;	/* self-refresh temerature, normal range */
    
    	unsigned int cwl = compute_cas_write_latency(ctrl_num) - 9;
    
    	unsigned int mpr = 0;	/* serial */
    	unsigned int wc_lat;
    
    	const unsigned int mclk_ps = get_memory_clk_period_ps(ctrl_num);
    
    	if (popts->rtt_override)
    		rtt_wr = popts->rtt_wr_override_value;
    	else
    		rtt_wr = popts->cs_local_opts[0].odt_rtt_wr;
    
    	if (common_dimm->extended_op_srt)
    		srt = common_dimm->extended_op_srt;
    
    	esdmode2 = (0
    		| ((wr_crc & 0x1) << 12)
    		| ((rtt_wr & 0x3) << 9)
    		| ((srt & 0x3) << 6)
    		| ((cwl & 0x7) << 3));
    
    	if (mclk_ps >= 1250)
    		wc_lat = 0;
    	else if (mclk_ps >= 833)
    		wc_lat = 1;
    	else
    		wc_lat = 2;
    
    	esdmode3 = (0
    		| ((mpr & 0x3) << 11)
    		| ((wc_lat & 0x3) << 9));
    
    	ddr->ddr_sdram_mode_2 = (0
    				 | ((esdmode2 & 0xFFFF) << 16)
    				 | ((esdmode3 & 0xFFFF) << 0)
    				 );
    	debug("FSLDDR: ddr_sdram_mode_2 = 0x%08x\n", ddr->ddr_sdram_mode_2);
    
    	if (unq_mrs_en) {	/* unique mode registers are supported */
    		for (i = 1; i < CONFIG_CHIP_SELECTS_PER_CTRL; i++) {
    			if (popts->rtt_override)
    				rtt_wr = popts->rtt_wr_override_value;
    			else
    				rtt_wr = popts->cs_local_opts[i].odt_rtt_wr;
    
    			esdmode2 &= 0xF9FF;	/* clear bit 10, 9 */