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