Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Kumar Gala | d5a1fb9 | 2008-08-26 21:34:55 -0500 | [diff] [blame] | 2 | /* |
York Sun | b513d9d | 2012-08-17 08:22:36 +0000 | [diff] [blame] | 3 | * Copyright 2008-2012 Freescale Semiconductor, Inc. |
Kumar Gala | d5a1fb9 | 2008-08-26 21:34:55 -0500 | [diff] [blame] | 4 | */ |
| 5 | |
| 6 | #include <common.h> |
| 7 | #include <asm/io.h> |
York Sun | f062659 | 2013-09-30 09:22:09 -0700 | [diff] [blame] | 8 | #include <fsl_ddr_sdram.h> |
York Sun | 7dda847 | 2011-01-10 12:02:59 +0000 | [diff] [blame] | 9 | #include <asm/processor.h> |
Kumar Gala | d5a1fb9 | 2008-08-26 21:34:55 -0500 | [diff] [blame] | 10 | |
| 11 | #if (CONFIG_CHIP_SELECTS_PER_CTRL > 4) |
| 12 | #error Invalid setting for CONFIG_CHIP_SELECTS_PER_CTRL |
| 13 | #endif |
| 14 | |
York Sun | 5e15555 | 2013-06-25 11:37:48 -0700 | [diff] [blame] | 15 | /* |
| 16 | * regs has the to-be-set values for DDR controller registers |
| 17 | * ctrl_num is the DDR controller number |
| 18 | * step: 0 goes through the initialization in one pass |
| 19 | * 1 sets registers and returns before enabling controller |
| 20 | * 2 resumes from step 1 and continues to initialize |
| 21 | * Dividing the initialization to two steps to deassert DDR reset signal |
| 22 | * to comply with JEDEC specs for RDIMMs. |
| 23 | */ |
Kumar Gala | d5a1fb9 | 2008-08-26 21:34:55 -0500 | [diff] [blame] | 24 | void fsl_ddr_set_memctl_regs(const fsl_ddr_cfg_regs_t *regs, |
York Sun | 5e15555 | 2013-06-25 11:37:48 -0700 | [diff] [blame] | 25 | unsigned int ctrl_num, int step) |
Kumar Gala | d5a1fb9 | 2008-08-26 21:34:55 -0500 | [diff] [blame] | 26 | { |
York Sun | 016095d | 2012-10-08 07:44:24 +0000 | [diff] [blame] | 27 | unsigned int i, bus_width; |
York Sun | a21803d | 2013-11-18 10:29:32 -0800 | [diff] [blame] | 28 | struct ccsr_ddr __iomem *ddr; |
Poonam_Aggrwal-b10812 | 30cb145 | 2009-01-04 08:46:38 +0530 | [diff] [blame] | 29 | u32 temp_sdram_cfg; |
York Sun | 016095d | 2012-10-08 07:44:24 +0000 | [diff] [blame] | 30 | u32 total_gb_size_per_controller; |
Andy Fleming | 1bc8b04 | 2012-10-22 17:28:18 -0500 | [diff] [blame] | 31 | int timeout; |
York Sun | c8fc959 | 2011-01-25 22:05:49 -0800 | [diff] [blame] | 32 | #ifdef CONFIG_SYS_FSL_ERRATUM_DDR111_DDR134 |
Andy Fleming | 1bc8b04 | 2012-10-22 17:28:18 -0500 | [diff] [blame] | 33 | int timeout_save; |
York Sun | c8fc959 | 2011-01-25 22:05:49 -0800 | [diff] [blame] | 34 | volatile ccsr_local_ecm_t *ecm = (void *)CONFIG_SYS_MPC85xx_ECM_ADDR; |
York Sun | 7d9781b | 2011-03-17 11:18:13 -0700 | [diff] [blame] | 35 | unsigned int csn_bnds_backup = 0, cs_sa, cs_ea, *csn_bnds_t; |
| 36 | int csn = -1; |
York Sun | c8fc959 | 2011-01-25 22:05:49 -0800 | [diff] [blame] | 37 | #endif |
York Sun | 0c88c81 | 2014-01-08 13:00:42 -0800 | [diff] [blame] | 38 | #ifdef CONFIG_SYS_FSL_ERRATUM_DDR_A003 |
| 39 | u32 save1, save2; |
| 40 | #endif |
Kumar Gala | d5a1fb9 | 2008-08-26 21:34:55 -0500 | [diff] [blame] | 41 | |
| 42 | switch (ctrl_num) { |
| 43 | case 0: |
York Sun | f062659 | 2013-09-30 09:22:09 -0700 | [diff] [blame] | 44 | ddr = (void *)CONFIG_SYS_FSL_DDR_ADDR; |
Kumar Gala | d5a1fb9 | 2008-08-26 21:34:55 -0500 | [diff] [blame] | 45 | break; |
York Sun | fe84507 | 2016-12-28 08:43:45 -0800 | [diff] [blame] | 46 | #if defined(CONFIG_SYS_FSL_DDR2_ADDR) && (CONFIG_SYS_NUM_DDR_CTLRS > 1) |
Kumar Gala | d5a1fb9 | 2008-08-26 21:34:55 -0500 | [diff] [blame] | 47 | case 1: |
York Sun | f062659 | 2013-09-30 09:22:09 -0700 | [diff] [blame] | 48 | ddr = (void *)CONFIG_SYS_FSL_DDR2_ADDR; |
Kumar Gala | d5a1fb9 | 2008-08-26 21:34:55 -0500 | [diff] [blame] | 49 | break; |
York Sun | e8dc17b | 2012-08-17 08:22:39 +0000 | [diff] [blame] | 50 | #endif |
York Sun | fe84507 | 2016-12-28 08:43:45 -0800 | [diff] [blame] | 51 | #if defined(CONFIG_SYS_FSL_DDR3_ADDR) && (CONFIG_SYS_NUM_DDR_CTLRS > 2) |
York Sun | e8dc17b | 2012-08-17 08:22:39 +0000 | [diff] [blame] | 52 | case 2: |
York Sun | f062659 | 2013-09-30 09:22:09 -0700 | [diff] [blame] | 53 | ddr = (void *)CONFIG_SYS_FSL_DDR3_ADDR; |
York Sun | e8dc17b | 2012-08-17 08:22:39 +0000 | [diff] [blame] | 54 | break; |
| 55 | #endif |
York Sun | fe84507 | 2016-12-28 08:43:45 -0800 | [diff] [blame] | 56 | #if defined(CONFIG_SYS_FSL_DDR4_ADDR) && (CONFIG_SYS_NUM_DDR_CTLRS > 3) |
York Sun | e8dc17b | 2012-08-17 08:22:39 +0000 | [diff] [blame] | 57 | case 3: |
York Sun | f062659 | 2013-09-30 09:22:09 -0700 | [diff] [blame] | 58 | ddr = (void *)CONFIG_SYS_FSL_DDR4_ADDR; |
York Sun | e8dc17b | 2012-08-17 08:22:39 +0000 | [diff] [blame] | 59 | break; |
| 60 | #endif |
Kumar Gala | d5a1fb9 | 2008-08-26 21:34:55 -0500 | [diff] [blame] | 61 | default: |
| 62 | printf("%s unexpected ctrl_num = %u\n", __FUNCTION__, ctrl_num); |
| 63 | return; |
| 64 | } |
| 65 | |
York Sun | 5e15555 | 2013-06-25 11:37:48 -0700 | [diff] [blame] | 66 | if (step == 2) |
| 67 | goto step2; |
| 68 | |
York Sun | 016095d | 2012-10-08 07:44:24 +0000 | [diff] [blame] | 69 | if (regs->ddr_eor) |
| 70 | out_be32(&ddr->eor, regs->ddr_eor); |
York Sun | 7d9781b | 2011-03-17 11:18:13 -0700 | [diff] [blame] | 71 | #ifdef CONFIG_SYS_FSL_ERRATUM_DDR111_DDR134 |
York Sun | b513d9d | 2012-08-17 08:22:36 +0000 | [diff] [blame] | 72 | debug("Workaround for ERRATUM_DDR111_DDR134\n"); |
York Sun | 7d9781b | 2011-03-17 11:18:13 -0700 | [diff] [blame] | 73 | for (i = 0; i < CONFIG_CHIP_SELECTS_PER_CTRL; i++) { |
| 74 | cs_sa = (regs->cs[i].bnds >> 16) & 0xfff; |
| 75 | cs_ea = regs->cs[i].bnds & 0xfff; |
| 76 | if ((cs_sa <= 0xff) && (cs_ea >= 0xff)) { |
| 77 | csn = i; |
| 78 | csn_bnds_backup = regs->cs[i].bnds; |
| 79 | csn_bnds_t = (unsigned int *) ®s->cs[i].bnds; |
York Sun | 4b736b8 | 2012-05-21 08:43:11 +0000 | [diff] [blame] | 80 | if (cs_ea > 0xeff) |
| 81 | *csn_bnds_t = regs->cs[i].bnds + 0x01000000; |
| 82 | else |
| 83 | *csn_bnds_t = regs->cs[i].bnds + 0x01000100; |
York Sun | 7d9781b | 2011-03-17 11:18:13 -0700 | [diff] [blame] | 84 | debug("Found cs%d_bns (0x%08x) covering 0xff000000, " |
| 85 | "change it to 0x%x\n", |
| 86 | csn, csn_bnds_backup, regs->cs[i].bnds); |
| 87 | break; |
| 88 | } |
| 89 | } |
| 90 | #endif |
Kumar Gala | d5a1fb9 | 2008-08-26 21:34:55 -0500 | [diff] [blame] | 91 | for (i = 0; i < CONFIG_CHIP_SELECTS_PER_CTRL; i++) { |
| 92 | if (i == 0) { |
| 93 | out_be32(&ddr->cs0_bnds, regs->cs[i].bnds); |
| 94 | out_be32(&ddr->cs0_config, regs->cs[i].config); |
| 95 | out_be32(&ddr->cs0_config_2, regs->cs[i].config_2); |
| 96 | |
| 97 | } else if (i == 1) { |
| 98 | out_be32(&ddr->cs1_bnds, regs->cs[i].bnds); |
| 99 | out_be32(&ddr->cs1_config, regs->cs[i].config); |
| 100 | out_be32(&ddr->cs1_config_2, regs->cs[i].config_2); |
| 101 | |
| 102 | } else if (i == 2) { |
| 103 | out_be32(&ddr->cs2_bnds, regs->cs[i].bnds); |
| 104 | out_be32(&ddr->cs2_config, regs->cs[i].config); |
| 105 | out_be32(&ddr->cs2_config_2, regs->cs[i].config_2); |
| 106 | |
| 107 | } else if (i == 3) { |
| 108 | out_be32(&ddr->cs3_bnds, regs->cs[i].bnds); |
| 109 | out_be32(&ddr->cs3_config, regs->cs[i].config); |
| 110 | out_be32(&ddr->cs3_config_2, regs->cs[i].config_2); |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | out_be32(&ddr->timing_cfg_3, regs->timing_cfg_3); |
| 115 | out_be32(&ddr->timing_cfg_0, regs->timing_cfg_0); |
| 116 | out_be32(&ddr->timing_cfg_1, regs->timing_cfg_1); |
| 117 | out_be32(&ddr->timing_cfg_2, regs->timing_cfg_2); |
Kumar Gala | d5a1fb9 | 2008-08-26 21:34:55 -0500 | [diff] [blame] | 118 | out_be32(&ddr->sdram_mode, regs->ddr_sdram_mode); |
| 119 | out_be32(&ddr->sdram_mode_2, regs->ddr_sdram_mode_2); |
York Sun | ba0c2eb | 2011-01-10 12:03:00 +0000 | [diff] [blame] | 120 | out_be32(&ddr->sdram_mode_3, regs->ddr_sdram_mode_3); |
| 121 | out_be32(&ddr->sdram_mode_4, regs->ddr_sdram_mode_4); |
| 122 | out_be32(&ddr->sdram_mode_5, regs->ddr_sdram_mode_5); |
| 123 | out_be32(&ddr->sdram_mode_6, regs->ddr_sdram_mode_6); |
| 124 | out_be32(&ddr->sdram_mode_7, regs->ddr_sdram_mode_7); |
| 125 | out_be32(&ddr->sdram_mode_8, regs->ddr_sdram_mode_8); |
Kumar Gala | d5a1fb9 | 2008-08-26 21:34:55 -0500 | [diff] [blame] | 126 | out_be32(&ddr->sdram_md_cntl, regs->ddr_sdram_md_cntl); |
| 127 | out_be32(&ddr->sdram_interval, regs->ddr_sdram_interval); |
| 128 | out_be32(&ddr->sdram_data_init, regs->ddr_data_init); |
| 129 | out_be32(&ddr->sdram_clk_cntl, regs->ddr_sdram_clk_cntl); |
Kumar Gala | d5a1fb9 | 2008-08-26 21:34:55 -0500 | [diff] [blame] | 130 | out_be32(&ddr->timing_cfg_4, regs->timing_cfg_4); |
| 131 | out_be32(&ddr->timing_cfg_5, regs->timing_cfg_5); |
| 132 | out_be32(&ddr->ddr_zq_cntl, regs->ddr_zq_cntl); |
| 133 | out_be32(&ddr->ddr_wrlvl_cntl, regs->ddr_wrlvl_cntl); |
York Sun | 972cc40 | 2013-06-25 11:37:41 -0700 | [diff] [blame] | 134 | #ifndef CONFIG_SYS_FSL_DDR_EMU |
| 135 | /* |
| 136 | * Skip these two registers if running on emulator |
| 137 | * because emulator doesn't have skew between bytes. |
| 138 | */ |
| 139 | |
York Sun | 7d69ea3 | 2012-10-08 07:44:22 +0000 | [diff] [blame] | 140 | if (regs->ddr_wrlvl_cntl_2) |
| 141 | out_be32(&ddr->ddr_wrlvl_cntl_2, regs->ddr_wrlvl_cntl_2); |
| 142 | if (regs->ddr_wrlvl_cntl_3) |
| 143 | out_be32(&ddr->ddr_wrlvl_cntl_3, regs->ddr_wrlvl_cntl_3); |
York Sun | 972cc40 | 2013-06-25 11:37:41 -0700 | [diff] [blame] | 144 | #endif |
York Sun | 7d69ea3 | 2012-10-08 07:44:22 +0000 | [diff] [blame] | 145 | |
Kumar Gala | d5a1fb9 | 2008-08-26 21:34:55 -0500 | [diff] [blame] | 146 | out_be32(&ddr->ddr_sr_cntr, regs->ddr_sr_cntr); |
| 147 | out_be32(&ddr->ddr_sdram_rcw_1, regs->ddr_sdram_rcw_1); |
| 148 | out_be32(&ddr->ddr_sdram_rcw_2, regs->ddr_sdram_rcw_2); |
York Sun | 7dda847 | 2011-01-10 12:02:59 +0000 | [diff] [blame] | 149 | out_be32(&ddr->ddr_cdr1, regs->ddr_cdr1); |
Tang Yuantian | 064f126 | 2014-11-21 11:17:15 +0800 | [diff] [blame] | 150 | #ifdef CONFIG_DEEP_SLEEP |
| 151 | if (is_warm_boot()) { |
| 152 | out_be32(&ddr->sdram_cfg_2, |
| 153 | regs->ddr_sdram_cfg_2 & ~SDRAM_CFG2_D_INIT); |
| 154 | out_be32(&ddr->init_addr, CONFIG_SYS_SDRAM_BASE); |
| 155 | out_be32(&ddr->init_ext_addr, DDR_INIT_ADDR_EXT_UIA); |
| 156 | |
| 157 | /* DRAM VRef will not be trained */ |
| 158 | out_be32(&ddr->ddr_cdr2, |
| 159 | regs->ddr_cdr2 & ~DDR_CDR2_VREF_TRAIN_EN); |
| 160 | } else |
| 161 | #endif |
| 162 | { |
| 163 | out_be32(&ddr->sdram_cfg_2, regs->ddr_sdram_cfg_2); |
| 164 | out_be32(&ddr->init_addr, regs->ddr_init_addr); |
| 165 | out_be32(&ddr->init_ext_addr, regs->ddr_init_ext_addr); |
| 166 | out_be32(&ddr->ddr_cdr2, regs->ddr_cdr2); |
| 167 | } |
York Sun | 7dda847 | 2011-01-10 12:02:59 +0000 | [diff] [blame] | 168 | out_be32(&ddr->err_disable, regs->err_disable); |
| 169 | out_be32(&ddr->err_int_en, regs->err_int_en); |
York Sun | b513d9d | 2012-08-17 08:22:36 +0000 | [diff] [blame] | 170 | for (i = 0; i < 32; i++) { |
| 171 | if (regs->debug[i]) { |
| 172 | debug("Write to debug_%d as %08x\n", i+1, regs->debug[i]); |
| 173 | out_be32(&ddr->debug[i], regs->debug[i]); |
| 174 | } |
| 175 | } |
Kumar Gala | d5a1fb9 | 2008-08-26 21:34:55 -0500 | [diff] [blame] | 176 | |
York Sun | df2be19 | 2011-11-20 10:01:35 -0800 | [diff] [blame] | 177 | #ifdef CONFIG_SYS_FSL_ERRATUM_DDR_A003474 |
| 178 | out_be32(&ddr->debug[12], 0x00000015); |
| 179 | out_be32(&ddr->debug[21], 0x24000000); |
| 180 | #endif /* CONFIG_SYS_FSL_ERRATUM_DDR_A003474 */ |
| 181 | |
York Sun | 5e15555 | 2013-06-25 11:37:48 -0700 | [diff] [blame] | 182 | /* |
| 183 | * For RDIMMs, JEDEC spec requires clocks to be stable before reset is |
| 184 | * deasserted. Clocks start when any chip select is enabled and clock |
| 185 | * control register is set. Because all DDR components are connected to |
| 186 | * one reset signal, this needs to be done in two steps. Step 1 is to |
| 187 | * get the clocks started. Step 2 resumes after reset signal is |
| 188 | * deasserted. |
| 189 | */ |
| 190 | if (step == 1) { |
| 191 | udelay(200); |
| 192 | return; |
| 193 | } |
| 194 | |
| 195 | step2: |
Ed Swarthout | e674b83 | 2009-02-24 02:37:59 -0600 | [diff] [blame] | 196 | /* Set, but do not enable the memory */ |
| 197 | temp_sdram_cfg = regs->ddr_sdram_cfg; |
Poonam_Aggrwal-b10812 | 30cb145 | 2009-01-04 08:46:38 +0530 | [diff] [blame] | 198 | temp_sdram_cfg &= ~(SDRAM_CFG_MEM_EN); |
| 199 | out_be32(&ddr->sdram_cfg, temp_sdram_cfg); |
York Sun | 922f40f | 2011-01-10 12:03:01 +0000 | [diff] [blame] | 200 | #ifdef CONFIG_SYS_FSL_ERRATUM_DDR_A003 |
York Sun | b513d9d | 2012-08-17 08:22:36 +0000 | [diff] [blame] | 201 | debug("Workaround for ERRATUM_DDR_A003\n"); |
York Sun | 922f40f | 2011-01-10 12:03:01 +0000 | [diff] [blame] | 202 | if (regs->ddr_sdram_rcw_2 & 0x00f00000) { |
| 203 | out_be32(&ddr->timing_cfg_2, regs->timing_cfg_2 & 0xf07fffff); |
| 204 | out_be32(&ddr->debug[2], 0x00000400); |
| 205 | out_be32(&ddr->ddr_zq_cntl, regs->ddr_zq_cntl & 0x7fffffff); |
| 206 | out_be32(&ddr->ddr_wrlvl_cntl, regs->ddr_wrlvl_cntl & 0x7fffffff); |
| 207 | out_be32(&ddr->sdram_cfg_2, regs->ddr_sdram_cfg_2 & 0xffffffeb); |
| 208 | out_be32(&ddr->mtcr, 0); |
York Sun | 0c88c81 | 2014-01-08 13:00:42 -0800 | [diff] [blame] | 209 | save1 = in_be32(&ddr->debug[12]); |
| 210 | save2 = in_be32(&ddr->debug[21]); |
York Sun | 922f40f | 2011-01-10 12:03:01 +0000 | [diff] [blame] | 211 | out_be32(&ddr->debug[12], 0x00000015); |
| 212 | out_be32(&ddr->debug[21], 0x24000000); |
| 213 | out_be32(&ddr->sdram_interval, regs->ddr_sdram_interval & 0xffff); |
| 214 | out_be32(&ddr->sdram_cfg, temp_sdram_cfg | SDRAM_CFG_BI | SDRAM_CFG_MEM_EN); |
| 215 | |
| 216 | asm volatile("sync;isync"); |
| 217 | while (!(in_be32(&ddr->debug[1]) & 0x2)) |
| 218 | ; |
| 219 | |
| 220 | switch (regs->ddr_sdram_rcw_2 & 0x00f00000) { |
| 221 | case 0x00000000: |
| 222 | out_be32(&ddr->sdram_md_cntl, |
| 223 | MD_CNTL_MD_EN | |
| 224 | MD_CNTL_CS_SEL_CS0_CS1 | |
| 225 | 0x04000000 | |
| 226 | MD_CNTL_WRCW | |
| 227 | MD_CNTL_MD_VALUE(0x02)); |
York Sun | 0c88c81 | 2014-01-08 13:00:42 -0800 | [diff] [blame] | 228 | #if (CONFIG_DIMM_SLOTS_PER_CTLR == 2) |
| 229 | if (!(regs->cs[2].config & SDRAM_CS_CONFIG_EN)) |
| 230 | break; |
| 231 | while (in_be32(&ddr->sdram_md_cntl) & MD_CNTL_MD_EN) |
| 232 | ; |
| 233 | out_be32(&ddr->sdram_md_cntl, |
| 234 | MD_CNTL_MD_EN | |
| 235 | MD_CNTL_CS_SEL_CS2_CS3 | |
| 236 | 0x04000000 | |
| 237 | MD_CNTL_WRCW | |
| 238 | MD_CNTL_MD_VALUE(0x02)); |
| 239 | #endif |
York Sun | 922f40f | 2011-01-10 12:03:01 +0000 | [diff] [blame] | 240 | break; |
| 241 | case 0x00100000: |
| 242 | out_be32(&ddr->sdram_md_cntl, |
| 243 | MD_CNTL_MD_EN | |
| 244 | MD_CNTL_CS_SEL_CS0_CS1 | |
| 245 | 0x04000000 | |
| 246 | MD_CNTL_WRCW | |
| 247 | MD_CNTL_MD_VALUE(0x0a)); |
York Sun | 0c88c81 | 2014-01-08 13:00:42 -0800 | [diff] [blame] | 248 | #if (CONFIG_DIMM_SLOTS_PER_CTLR == 2) |
| 249 | if (!(regs->cs[2].config & SDRAM_CS_CONFIG_EN)) |
| 250 | break; |
| 251 | while (in_be32(&ddr->sdram_md_cntl) & MD_CNTL_MD_EN) |
| 252 | ; |
| 253 | out_be32(&ddr->sdram_md_cntl, |
| 254 | MD_CNTL_MD_EN | |
| 255 | MD_CNTL_CS_SEL_CS2_CS3 | |
| 256 | 0x04000000 | |
| 257 | MD_CNTL_WRCW | |
| 258 | MD_CNTL_MD_VALUE(0x0a)); |
| 259 | #endif |
York Sun | 922f40f | 2011-01-10 12:03:01 +0000 | [diff] [blame] | 260 | break; |
| 261 | case 0x00200000: |
| 262 | out_be32(&ddr->sdram_md_cntl, |
| 263 | MD_CNTL_MD_EN | |
| 264 | MD_CNTL_CS_SEL_CS0_CS1 | |
| 265 | 0x04000000 | |
| 266 | MD_CNTL_WRCW | |
| 267 | MD_CNTL_MD_VALUE(0x12)); |
York Sun | 0c88c81 | 2014-01-08 13:00:42 -0800 | [diff] [blame] | 268 | #if (CONFIG_DIMM_SLOTS_PER_CTLR == 2) |
| 269 | if (!(regs->cs[2].config & SDRAM_CS_CONFIG_EN)) |
| 270 | break; |
| 271 | while (in_be32(&ddr->sdram_md_cntl) & MD_CNTL_MD_EN) |
| 272 | ; |
| 273 | out_be32(&ddr->sdram_md_cntl, |
| 274 | MD_CNTL_MD_EN | |
| 275 | MD_CNTL_CS_SEL_CS2_CS3 | |
| 276 | 0x04000000 | |
| 277 | MD_CNTL_WRCW | |
| 278 | MD_CNTL_MD_VALUE(0x12)); |
| 279 | #endif |
York Sun | 922f40f | 2011-01-10 12:03:01 +0000 | [diff] [blame] | 280 | break; |
| 281 | case 0x00300000: |
| 282 | out_be32(&ddr->sdram_md_cntl, |
| 283 | MD_CNTL_MD_EN | |
| 284 | MD_CNTL_CS_SEL_CS0_CS1 | |
| 285 | 0x04000000 | |
| 286 | MD_CNTL_WRCW | |
| 287 | MD_CNTL_MD_VALUE(0x1a)); |
York Sun | 0c88c81 | 2014-01-08 13:00:42 -0800 | [diff] [blame] | 288 | #if (CONFIG_DIMM_SLOTS_PER_CTLR == 2) |
| 289 | if (!(regs->cs[2].config & SDRAM_CS_CONFIG_EN)) |
| 290 | break; |
| 291 | while (in_be32(&ddr->sdram_md_cntl) & MD_CNTL_MD_EN) |
| 292 | ; |
| 293 | out_be32(&ddr->sdram_md_cntl, |
| 294 | MD_CNTL_MD_EN | |
| 295 | MD_CNTL_CS_SEL_CS2_CS3 | |
| 296 | 0x04000000 | |
| 297 | MD_CNTL_WRCW | |
| 298 | MD_CNTL_MD_VALUE(0x1a)); |
| 299 | #endif |
York Sun | 922f40f | 2011-01-10 12:03:01 +0000 | [diff] [blame] | 300 | break; |
| 301 | default: |
| 302 | out_be32(&ddr->sdram_md_cntl, |
| 303 | MD_CNTL_MD_EN | |
| 304 | MD_CNTL_CS_SEL_CS0_CS1 | |
| 305 | 0x04000000 | |
| 306 | MD_CNTL_WRCW | |
| 307 | MD_CNTL_MD_VALUE(0x02)); |
York Sun | 0c88c81 | 2014-01-08 13:00:42 -0800 | [diff] [blame] | 308 | #if (CONFIG_DIMM_SLOTS_PER_CTLR == 2) |
| 309 | if (!(regs->cs[2].config & SDRAM_CS_CONFIG_EN)) |
| 310 | break; |
| 311 | while (in_be32(&ddr->sdram_md_cntl) & MD_CNTL_MD_EN) |
| 312 | ; |
| 313 | out_be32(&ddr->sdram_md_cntl, |
| 314 | MD_CNTL_MD_EN | |
| 315 | MD_CNTL_CS_SEL_CS2_CS3 | |
| 316 | 0x04000000 | |
| 317 | MD_CNTL_WRCW | |
| 318 | MD_CNTL_MD_VALUE(0x02)); |
| 319 | #endif |
York Sun | 922f40f | 2011-01-10 12:03:01 +0000 | [diff] [blame] | 320 | printf("Unsupported RC10\n"); |
| 321 | break; |
| 322 | } |
| 323 | |
| 324 | while (in_be32(&ddr->sdram_md_cntl) & 0x80000000) |
| 325 | ; |
| 326 | udelay(6); |
| 327 | out_be32(&ddr->sdram_cfg, temp_sdram_cfg); |
| 328 | out_be32(&ddr->timing_cfg_2, regs->timing_cfg_2); |
| 329 | out_be32(&ddr->debug[2], 0x0); |
| 330 | out_be32(&ddr->ddr_zq_cntl, regs->ddr_zq_cntl); |
| 331 | out_be32(&ddr->ddr_wrlvl_cntl, regs->ddr_wrlvl_cntl); |
| 332 | out_be32(&ddr->sdram_cfg_2, regs->ddr_sdram_cfg_2); |
York Sun | 0c88c81 | 2014-01-08 13:00:42 -0800 | [diff] [blame] | 333 | out_be32(&ddr->debug[12], save1); |
| 334 | out_be32(&ddr->debug[21], save2); |
York Sun | 922f40f | 2011-01-10 12:03:01 +0000 | [diff] [blame] | 335 | out_be32(&ddr->sdram_interval, regs->ddr_sdram_interval); |
| 336 | |
| 337 | } |
| 338 | #endif |
Kumar Gala | d5a1fb9 | 2008-08-26 21:34:55 -0500 | [diff] [blame] | 339 | /* |
Dave Liu | 7dc79f7 | 2008-10-23 21:18:53 +0800 | [diff] [blame] | 340 | * For 8572 DDR1 erratum - DDR controller may enter illegal state |
| 341 | * when operatiing in 32-bit bus mode with 4-beat bursts, |
| 342 | * This erratum does not affect DDR3 mode, only for DDR2 mode. |
Kumar Gala | d5a1fb9 | 2008-08-26 21:34:55 -0500 | [diff] [blame] | 343 | */ |
York Sun | 9aa857b | 2011-01-25 21:51:27 -0800 | [diff] [blame] | 344 | #ifdef CONFIG_SYS_FSL_ERRATUM_DDR_115 |
York Sun | b513d9d | 2012-08-17 08:22:36 +0000 | [diff] [blame] | 345 | debug("Workaround for ERRATUM_DDR_115\n"); |
Kumar Gala | d5a1fb9 | 2008-08-26 21:34:55 -0500 | [diff] [blame] | 346 | if ((((in_be32(&ddr->sdram_cfg) >> 24) & 0x7) == SDRAM_TYPE_DDR2) |
Dave Liu | 7dc79f7 | 2008-10-23 21:18:53 +0800 | [diff] [blame] | 347 | && in_be32(&ddr->sdram_cfg) & 0x80000) { |
Kumar Gala | d5a1fb9 | 2008-08-26 21:34:55 -0500 | [diff] [blame] | 348 | /* set DEBUG_1[31] */ |
York Sun | 7dda847 | 2011-01-10 12:02:59 +0000 | [diff] [blame] | 349 | setbits_be32(&ddr->debug[0], 1); |
Kumar Gala | d5a1fb9 | 2008-08-26 21:34:55 -0500 | [diff] [blame] | 350 | } |
Dave Liu | 7dc79f7 | 2008-10-23 21:18:53 +0800 | [diff] [blame] | 351 | #endif |
York Sun | c8fc959 | 2011-01-25 22:05:49 -0800 | [diff] [blame] | 352 | #ifdef CONFIG_SYS_FSL_ERRATUM_DDR111_DDR134 |
York Sun | b513d9d | 2012-08-17 08:22:36 +0000 | [diff] [blame] | 353 | debug("Workaround for ERRATUM_DDR111_DDR134\n"); |
York Sun | c8fc959 | 2011-01-25 22:05:49 -0800 | [diff] [blame] | 354 | /* |
| 355 | * This is the combined workaround for DDR111 and DDR134 |
| 356 | * following the published errata for MPC8572 |
| 357 | */ |
| 358 | |
| 359 | /* 1. Set EEBACR[3] */ |
| 360 | setbits_be32(&ecm->eebacr, 0x10000000); |
| 361 | debug("Setting EEBACR[3] to 0x%08x\n", in_be32(&ecm->eebacr)); |
| 362 | |
| 363 | /* 2. Set DINIT in SDRAM_CFG_2*/ |
| 364 | setbits_be32(&ddr->sdram_cfg_2, SDRAM_CFG2_D_INIT); |
| 365 | debug("Setting sdram_cfg_2[D_INIT] to 0x%08x\n", |
| 366 | in_be32(&ddr->sdram_cfg_2)); |
| 367 | |
| 368 | /* 3. Set DEBUG_3[21] */ |
| 369 | setbits_be32(&ddr->debug[2], 0x400); |
| 370 | debug("Setting DEBUG_3[21] to 0x%08x\n", in_be32(&ddr->debug[2])); |
| 371 | |
| 372 | #endif /* part 1 of the workaound */ |
Kumar Gala | d5a1fb9 | 2008-08-26 21:34:55 -0500 | [diff] [blame] | 373 | |
| 374 | /* |
Dave Liu | 4be87b2 | 2009-03-14 12:48:30 +0800 | [diff] [blame] | 375 | * 500 painful micro-seconds must elapse between |
Kumar Gala | d5a1fb9 | 2008-08-26 21:34:55 -0500 | [diff] [blame] | 376 | * the DDR clock setup and the DDR config enable. |
Dave Liu | 4be87b2 | 2009-03-14 12:48:30 +0800 | [diff] [blame] | 377 | * DDR2 need 200 us, and DDR3 need 500 us from spec, |
| 378 | * we choose the max, that is 500 us for all of case. |
Kumar Gala | d5a1fb9 | 2008-08-26 21:34:55 -0500 | [diff] [blame] | 379 | */ |
Dave Liu | 4be87b2 | 2009-03-14 12:48:30 +0800 | [diff] [blame] | 380 | udelay(500); |
Kumar Gala | d5a1fb9 | 2008-08-26 21:34:55 -0500 | [diff] [blame] | 381 | asm volatile("sync;isync"); |
| 382 | |
Tang Yuantian | a7364af | 2014-04-17 15:33:46 +0800 | [diff] [blame] | 383 | #ifdef CONFIG_DEEP_SLEEP |
Tang Yuantian | 064f126 | 2014-11-21 11:17:15 +0800 | [diff] [blame] | 384 | if (is_warm_boot()) { |
Biwen Li | 4d5cda6 | 2020-04-09 20:44:48 +0800 | [diff] [blame] | 385 | /* enter self-refresh */ |
| 386 | setbits_be32(&ddr->sdram_cfg_2, SDRAM_CFG2_FRC_SR); |
Tang Yuantian | a7364af | 2014-04-17 15:33:46 +0800 | [diff] [blame] | 387 | /* do board specific memory setup */ |
| 388 | board_mem_sleep_setup(); |
Tang Yuantian | a7364af | 2014-04-17 15:33:46 +0800 | [diff] [blame] | 389 | temp_sdram_cfg = (in_be32(&ddr->sdram_cfg) | SDRAM_CFG_BI); |
Tang Yuantian | 064f126 | 2014-11-21 11:17:15 +0800 | [diff] [blame] | 390 | } else |
Tang Yuantian | a7364af | 2014-04-17 15:33:46 +0800 | [diff] [blame] | 391 | #endif |
| 392 | temp_sdram_cfg = (in_be32(&ddr->sdram_cfg) & ~SDRAM_CFG_BI); |
Tang Yuantian | 064f126 | 2014-11-21 11:17:15 +0800 | [diff] [blame] | 393 | |
| 394 | /* Let the controller go */ |
Poonam_Aggrwal-b10812 | 30cb145 | 2009-01-04 08:46:38 +0530 | [diff] [blame] | 395 | out_be32(&ddr->sdram_cfg, temp_sdram_cfg | SDRAM_CFG_MEM_EN); |
York Sun | 922f40f | 2011-01-10 12:03:01 +0000 | [diff] [blame] | 396 | asm volatile("sync;isync"); |
Kumar Gala | d5a1fb9 | 2008-08-26 21:34:55 -0500 | [diff] [blame] | 397 | |
York Sun | 016095d | 2012-10-08 07:44:24 +0000 | [diff] [blame] | 398 | total_gb_size_per_controller = 0; |
| 399 | for (i = 0; i < CONFIG_CHIP_SELECTS_PER_CTRL; i++) { |
| 400 | if (!(regs->cs[i].config & 0x80000000)) |
| 401 | continue; |
| 402 | total_gb_size_per_controller += 1 << ( |
| 403 | ((regs->cs[i].config >> 14) & 0x3) + 2 + |
| 404 | ((regs->cs[i].config >> 8) & 0x7) + 12 + |
| 405 | ((regs->cs[i].config >> 0) & 0x7) + 8 + |
| 406 | 3 - ((regs->ddr_sdram_cfg >> 19) & 0x3) - |
| 407 | 26); /* minus 26 (count of 64M) */ |
| 408 | } |
| 409 | if (fsl_ddr_get_intl3r() & 0x80000000) /* 3-way interleaving */ |
| 410 | total_gb_size_per_controller *= 3; |
| 411 | else if (regs->cs[0].config & 0x20000000) /* 2-way interleaving */ |
| 412 | total_gb_size_per_controller <<= 1; |
| 413 | /* |
| 414 | * total memory / bus width = transactions needed |
| 415 | * transactions needed / data rate = seconds |
| 416 | * to add plenty of buffer, double the time |
| 417 | * For example, 2GB on 666MT/s 64-bit bus takes about 402ms |
| 418 | * Let's wait for 800ms |
| 419 | */ |
| 420 | bus_width = 3 - ((ddr->sdram_cfg & SDRAM_CFG_DBW_MASK) |
| 421 | >> SDRAM_CFG_DBW_SHIFT); |
| 422 | timeout = ((total_gb_size_per_controller << (6 - bus_width)) * 100 / |
York Sun | 2c0b62d | 2015-01-06 13:18:50 -0800 | [diff] [blame] | 423 | (get_ddr_freq(ctrl_num) >> 20)) << 1; |
Andy Fleming | 1bc8b04 | 2012-10-22 17:28:18 -0500 | [diff] [blame] | 424 | #ifdef CONFIG_SYS_FSL_ERRATUM_DDR111_DDR134 |
York Sun | 016095d | 2012-10-08 07:44:24 +0000 | [diff] [blame] | 425 | timeout_save = timeout; |
Andy Fleming | 1bc8b04 | 2012-10-22 17:28:18 -0500 | [diff] [blame] | 426 | #endif |
York Sun | 016095d | 2012-10-08 07:44:24 +0000 | [diff] [blame] | 427 | total_gb_size_per_controller >>= 4; /* shift down to gb size */ |
| 428 | debug("total %d GB\n", total_gb_size_per_controller); |
| 429 | debug("Need to wait up to %d * 10ms\n", timeout); |
| 430 | |
Kumar Gala | d5a1fb9 | 2008-08-26 21:34:55 -0500 | [diff] [blame] | 431 | /* Poll DDR_SDRAM_CFG_2[D_INIT] bit until auto-data init is done. */ |
York Sun | 016095d | 2012-10-08 07:44:24 +0000 | [diff] [blame] | 432 | while ((in_be32(&ddr->sdram_cfg_2) & SDRAM_CFG2_D_INIT) && |
| 433 | (timeout >= 0)) { |
Kumar Gala | d5a1fb9 | 2008-08-26 21:34:55 -0500 | [diff] [blame] | 434 | udelay(10000); /* throttle polling rate */ |
York Sun | 016095d | 2012-10-08 07:44:24 +0000 | [diff] [blame] | 435 | timeout--; |
| 436 | } |
| 437 | |
| 438 | if (timeout <= 0) |
| 439 | printf("Waiting for D_INIT timeout. Memory may not work.\n"); |
York Sun | c8fc959 | 2011-01-25 22:05:49 -0800 | [diff] [blame] | 440 | |
| 441 | #ifdef CONFIG_SYS_FSL_ERRATUM_DDR111_DDR134 |
| 442 | /* continue this workaround */ |
| 443 | |
| 444 | /* 4. Clear DEBUG3[21] */ |
| 445 | clrbits_be32(&ddr->debug[2], 0x400); |
| 446 | debug("Clearing D3[21] to 0x%08x\n", in_be32(&ddr->debug[2])); |
| 447 | |
| 448 | /* DDR134 workaround starts */ |
| 449 | /* A: Clear sdram_cfg_2[odt_cfg] */ |
| 450 | clrbits_be32(&ddr->sdram_cfg_2, SDRAM_CFG2_ODT_CFG_MASK); |
| 451 | debug("Clearing SDRAM_CFG2[ODT_CFG] to 0x%08x\n", |
| 452 | in_be32(&ddr->sdram_cfg_2)); |
| 453 | |
| 454 | /* B: Set DEBUG1[15] */ |
| 455 | setbits_be32(&ddr->debug[0], 0x10000); |
| 456 | debug("Setting D1[15] to 0x%08x\n", in_be32(&ddr->debug[0])); |
| 457 | |
| 458 | /* C: Set timing_cfg_2[cpo] to 0b11111 */ |
| 459 | setbits_be32(&ddr->timing_cfg_2, TIMING_CFG_2_CPO_MASK); |
| 460 | debug("Setting TMING_CFG_2[CPO] to 0x%08x\n", |
| 461 | in_be32(&ddr->timing_cfg_2)); |
| 462 | |
| 463 | /* D: Set D6 to 0x9f9f9f9f */ |
| 464 | out_be32(&ddr->debug[5], 0x9f9f9f9f); |
| 465 | debug("Setting D6 to 0x%08x\n", in_be32(&ddr->debug[5])); |
| 466 | |
| 467 | /* E: Set D7 to 0x9f9f9f9f */ |
| 468 | out_be32(&ddr->debug[6], 0x9f9f9f9f); |
| 469 | debug("Setting D7 to 0x%08x\n", in_be32(&ddr->debug[6])); |
| 470 | |
| 471 | /* F: Set D2[20] */ |
| 472 | setbits_be32(&ddr->debug[1], 0x800); |
| 473 | debug("Setting D2[20] to 0x%08x\n", in_be32(&ddr->debug[1])); |
| 474 | |
| 475 | /* G: Poll on D2[20] until cleared */ |
| 476 | while (in_be32(&ddr->debug[1]) & 0x800) |
| 477 | udelay(10000); /* throttle polling rate */ |
| 478 | |
| 479 | /* H: Clear D1[15] */ |
| 480 | clrbits_be32(&ddr->debug[0], 0x10000); |
| 481 | debug("Setting D1[15] to 0x%08x\n", in_be32(&ddr->debug[0])); |
| 482 | |
| 483 | /* I: Set sdram_cfg_2[odt_cfg] */ |
| 484 | setbits_be32(&ddr->sdram_cfg_2, |
| 485 | regs->ddr_sdram_cfg_2 & SDRAM_CFG2_ODT_CFG_MASK); |
| 486 | debug("Setting sdram_cfg_2 to 0x%08x\n", in_be32(&ddr->sdram_cfg_2)); |
| 487 | |
| 488 | /* Continuing with the DDR111 workaround */ |
| 489 | /* 5. Set D2[21] */ |
| 490 | setbits_be32(&ddr->debug[1], 0x400); |
| 491 | debug("Setting D2[21] to 0x%08x\n", in_be32(&ddr->debug[1])); |
| 492 | |
| 493 | /* 6. Poll D2[21] until its cleared */ |
| 494 | while (in_be32(&ddr->debug[1]) & 0x400) |
| 495 | udelay(10000); /* throttle polling rate */ |
| 496 | |
York Sun | 016095d | 2012-10-08 07:44:24 +0000 | [diff] [blame] | 497 | /* 7. Wait for state machine 2nd run, roughly 400ms/GB */ |
| 498 | debug("Wait for %d * 10ms\n", timeout_save); |
| 499 | udelay(timeout_save * 10000); |
York Sun | c8fc959 | 2011-01-25 22:05:49 -0800 | [diff] [blame] | 500 | |
| 501 | /* 8. Set sdram_cfg_2[dinit] if options requires */ |
| 502 | setbits_be32(&ddr->sdram_cfg_2, |
| 503 | regs->ddr_sdram_cfg_2 & SDRAM_CFG2_D_INIT); |
| 504 | debug("Setting sdram_cfg_2 to 0x%08x\n", in_be32(&ddr->sdram_cfg_2)); |
| 505 | |
| 506 | /* 9. Poll until dinit is cleared */ |
York Sun | 016095d | 2012-10-08 07:44:24 +0000 | [diff] [blame] | 507 | timeout = timeout_save; |
| 508 | debug("Need to wait up to %d * 10ms\n", timeout); |
| 509 | while ((in_be32(&ddr->sdram_cfg_2) & SDRAM_CFG2_D_INIT) && |
| 510 | (timeout >= 0)) { |
| 511 | udelay(10000); /* throttle polling rate */ |
| 512 | timeout--; |
| 513 | } |
| 514 | |
| 515 | if (timeout <= 0) |
| 516 | printf("Waiting for D_INIT timeout. Memory may not work.\n"); |
York Sun | c8fc959 | 2011-01-25 22:05:49 -0800 | [diff] [blame] | 517 | |
| 518 | /* 10. Clear EEBACR[3] */ |
| 519 | clrbits_be32(&ecm->eebacr, 10000000); |
| 520 | debug("Clearing EEBACR[3] to 0x%08x\n", in_be32(&ecm->eebacr)); |
York Sun | 7d9781b | 2011-03-17 11:18:13 -0700 | [diff] [blame] | 521 | |
| 522 | if (csn != -1) { |
| 523 | csn_bnds_t = (unsigned int *) ®s->cs[csn].bnds; |
| 524 | *csn_bnds_t = csn_bnds_backup; |
| 525 | debug("Change cs%d_bnds back to 0x%08x\n", |
| 526 | csn, regs->cs[csn].bnds); |
| 527 | setbits_be32(&ddr->sdram_cfg, 0x2); /* MEM_HALT */ |
| 528 | switch (csn) { |
| 529 | case 0: |
| 530 | out_be32(&ddr->cs0_bnds, regs->cs[csn].bnds); |
| 531 | break; |
| 532 | case 1: |
| 533 | out_be32(&ddr->cs1_bnds, regs->cs[csn].bnds); |
| 534 | break; |
York Sun | 2c0b62d | 2015-01-06 13:18:50 -0800 | [diff] [blame] | 535 | #if CONFIG_CHIP_SELECTS_PER_CTRL > 2 |
York Sun | 7d9781b | 2011-03-17 11:18:13 -0700 | [diff] [blame] | 536 | case 2: |
| 537 | out_be32(&ddr->cs2_bnds, regs->cs[csn].bnds); |
| 538 | break; |
| 539 | case 3: |
| 540 | out_be32(&ddr->cs3_bnds, regs->cs[csn].bnds); |
| 541 | break; |
York Sun | 2c0b62d | 2015-01-06 13:18:50 -0800 | [diff] [blame] | 542 | #endif |
York Sun | 7d9781b | 2011-03-17 11:18:13 -0700 | [diff] [blame] | 543 | } |
| 544 | clrbits_be32(&ddr->sdram_cfg, 0x2); |
| 545 | } |
York Sun | c8fc959 | 2011-01-25 22:05:49 -0800 | [diff] [blame] | 546 | #endif /* CONFIG_SYS_FSL_ERRATUM_DDR111_DDR134 */ |
Biwen Li | 4d5cda6 | 2020-04-09 20:44:48 +0800 | [diff] [blame] | 547 | #ifdef CONFIG_DEEP_SLEEP |
| 548 | if (is_warm_boot()) |
| 549 | /* exit self-refresh */ |
| 550 | clrbits_be32(&ddr->sdram_cfg_2, SDRAM_CFG2_FRC_SR); |
| 551 | #endif |
Kumar Gala | d5a1fb9 | 2008-08-26 21:34:55 -0500 | [diff] [blame] | 552 | } |