Eran Liberty | f046ccd | 2005-07-28 10:08:46 -0500 | [diff] [blame] | 1 | /* |
Dave Liu | f6eda7f | 2006-10-25 14:41:21 -0500 | [diff] [blame] | 2 | * (C) Copyright 2006 Freescale Semiconductor, Inc. |
| 3 | * |
Rafal Jaworowski | dc9e499 | 2006-03-16 17:46:46 +0100 | [diff] [blame] | 4 | * (C) Copyright 2006 |
| 5 | * Wolfgang Denk, DENX Software Engineering, wd@denx.de. |
Wolfgang Denk | cf48eb9 | 2006-04-16 10:51:58 +0200 | [diff] [blame] | 6 | * |
Eran Liberty | f046ccd | 2005-07-28 10:08:46 -0500 | [diff] [blame] | 7 | * Copyright 2004 Freescale Semiconductor. |
| 8 | * (C) Copyright 2003 Motorola Inc. |
| 9 | * Xianghua Xiao (X.Xiao@motorola.com) |
| 10 | * |
| 11 | * See file CREDITS for list of people who contributed to this |
| 12 | * project. |
| 13 | * |
| 14 | * This program is free software; you can redistribute it and/or |
| 15 | * modify it under the terms of the GNU General Public License as |
| 16 | * published by the Free Software Foundation; either version 2 of |
| 17 | * the License, or (at your option) any later version. |
| 18 | * |
| 19 | * This program is distributed in the hope that it will be useful, |
| 20 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 21 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 22 | * GNU General Public License for more details. |
| 23 | * |
| 24 | * You should have received a copy of the GNU General Public License |
| 25 | * along with this program; if not, write to the Free Software |
| 26 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, |
| 27 | * MA 02111-1307 USA |
| 28 | * |
| 29 | * Change log: |
| 30 | * |
| 31 | * 20050101: Eran Liberty (liberty@freescale.com) |
| 32 | * Initial file creating (porting from 85XX & 8260) |
Dave Liu | f6eda7f | 2006-10-25 14:41:21 -0500 | [diff] [blame] | 33 | * 20060601: Dave Liu (daveliu@freescale.com) |
| 34 | * DDR ECC support |
| 35 | * unify variable names for 83xx |
| 36 | * code cleanup |
Eran Liberty | f046ccd | 2005-07-28 10:08:46 -0500 | [diff] [blame] | 37 | */ |
| 38 | |
| 39 | #include <common.h> |
| 40 | #include <asm/processor.h> |
| 41 | #include <i2c.h> |
| 42 | #include <spd.h> |
| 43 | #include <asm/mmu.h> |
| 44 | #include <spd_sdram.h> |
| 45 | |
| 46 | #ifdef CONFIG_SPD_EEPROM |
| 47 | |
Dave Liu | f6eda7f | 2006-10-25 14:41:21 -0500 | [diff] [blame] | 48 | #if defined(CONFIG_DDR_ECC) && !defined(CONFIG_ECC_INIT_VIA_DDRC) |
Eran Liberty | f046ccd | 2005-07-28 10:08:46 -0500 | [diff] [blame] | 49 | extern void dma_init(void); |
| 50 | extern uint dma_check(void); |
| 51 | extern int dma_xfer(void *dest, uint count, void *src); |
| 52 | #endif |
| 53 | |
Eran Liberty | f046ccd | 2005-07-28 10:08:46 -0500 | [diff] [blame] | 54 | #ifndef CFG_READ_SPD |
| 55 | #define CFG_READ_SPD i2c_read |
| 56 | #endif |
| 57 | |
Eran Liberty | f046ccd | 2005-07-28 10:08:46 -0500 | [diff] [blame] | 58 | /* |
| 59 | * Convert picoseconds into clock cycles (rounding up if needed). |
| 60 | */ |
Dave Liu | f6eda7f | 2006-10-25 14:41:21 -0500 | [diff] [blame] | 61 | extern ulong get_ddr_clk(ulong dummy); |
Eran Liberty | f046ccd | 2005-07-28 10:08:46 -0500 | [diff] [blame] | 62 | |
| 63 | int |
| 64 | picos_to_clk(int picos) |
| 65 | { |
Dave Liu | f6eda7f | 2006-10-25 14:41:21 -0500 | [diff] [blame] | 66 | unsigned int ddr_bus_clk; |
Eran Liberty | f046ccd | 2005-07-28 10:08:46 -0500 | [diff] [blame] | 67 | int clks; |
| 68 | |
Dave Liu | f6eda7f | 2006-10-25 14:41:21 -0500 | [diff] [blame] | 69 | ddr_bus_clk = get_ddr_clk(0) >> 1; |
| 70 | clks = picos / ((1000000000 / ddr_bus_clk) * 1000); |
| 71 | if (picos % ((1000000000 / ddr_bus_clk) * 1000) !=0) { |
| 72 | clks++; |
Eran Liberty | f046ccd | 2005-07-28 10:08:46 -0500 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | return clks; |
| 76 | } |
| 77 | |
Marian Balakowicz | 4c8d1ec | 2006-03-14 16:23:35 +0100 | [diff] [blame] | 78 | unsigned int banksize(unsigned char row_dens) |
Eran Liberty | f046ccd | 2005-07-28 10:08:46 -0500 | [diff] [blame] | 79 | { |
| 80 | return ((row_dens >> 2) | ((row_dens & 3) << 6)) << 24; |
| 81 | } |
| 82 | |
Marian Balakowicz | 4c8d1ec | 2006-03-14 16:23:35 +0100 | [diff] [blame] | 83 | int read_spd(uint addr) |
| 84 | { |
| 85 | return ((int) addr); |
| 86 | } |
| 87 | |
Rafal Jaworowski | dc9e499 | 2006-03-16 17:46:46 +0100 | [diff] [blame] | 88 | #undef SPD_DEBUG |
| 89 | #ifdef SPD_DEBUG |
| 90 | static void spd_debug(spd_eeprom_t *spd) |
| 91 | { |
| 92 | printf ("\nDIMM type: %-18.18s\n", spd->mpart); |
| 93 | printf ("SPD size: %d\n", spd->info_size); |
| 94 | printf ("EEPROM size: %d\n", 1 << spd->chip_size); |
| 95 | printf ("Memory type: %d\n", spd->mem_type); |
| 96 | printf ("Row addr: %d\n", spd->nrow_addr); |
| 97 | printf ("Column addr: %d\n", spd->ncol_addr); |
| 98 | printf ("# of rows: %d\n", spd->nrows); |
| 99 | printf ("Row density: %d\n", spd->row_dens); |
| 100 | printf ("# of banks: %d\n", spd->nbanks); |
| 101 | printf ("Data width: %d\n", |
| 102 | 256 * spd->dataw_msb + spd->dataw_lsb); |
| 103 | printf ("Chip width: %d\n", spd->primw); |
| 104 | printf ("Refresh rate: %02X\n", spd->refresh); |
| 105 | printf ("CAS latencies: %02X\n", spd->cas_lat); |
| 106 | printf ("Write latencies: %02X\n", spd->write_lat); |
| 107 | printf ("tRP: %d\n", spd->trp); |
| 108 | printf ("tRCD: %d\n", spd->trcd); |
| 109 | printf ("\n"); |
| 110 | } |
| 111 | #endif /* SPD_DEBUG */ |
| 112 | |
| 113 | long int spd_sdram() |
Eran Liberty | f046ccd | 2005-07-28 10:08:46 -0500 | [diff] [blame] | 114 | { |
Timur Tabi | bed85ca | 2006-10-31 18:13:36 -0600 | [diff] [blame^] | 115 | #ifdef CONFIG_MPC834X |
| 116 | int caslat_83xx; /* For Errata DDR6 */ |
| 117 | #endif |
Eran Liberty | f046ccd | 2005-07-28 10:08:46 -0500 | [diff] [blame] | 118 | volatile immap_t *immap = (immap_t *)CFG_IMMRBAR; |
Dave Liu | f6eda7f | 2006-10-25 14:41:21 -0500 | [diff] [blame] | 119 | volatile ddr83xx_t *ddr = &immap->ddr; |
| 120 | volatile law83xx_t *ecm = &immap->sysconf.ddrlaw[0]; |
Eran Liberty | f046ccd | 2005-07-28 10:08:46 -0500 | [diff] [blame] | 121 | spd_eeprom_t spd; |
Timur Tabi | bed85ca | 2006-10-31 18:13:36 -0600 | [diff] [blame^] | 122 | unsigned int tmp, tmp1; |
Eran Liberty | f046ccd | 2005-07-28 10:08:46 -0500 | [diff] [blame] | 123 | unsigned int memsize; |
| 124 | unsigned int law_size; |
Dave Liu | f6eda7f | 2006-10-25 14:41:21 -0500 | [diff] [blame] | 125 | unsigned char caslat, caslat_ctrl; |
| 126 | unsigned char burstlen; |
| 127 | unsigned int max_bus_clk; |
| 128 | unsigned int max_data_rate, effective_data_rate; |
| 129 | unsigned int ddrc_clk; |
| 130 | unsigned int refresh_clk; |
| 131 | unsigned sdram_cfg; |
| 132 | unsigned int ddrc_ecc_enable; |
Jon Loeliger | de1d0a6 | 2005-08-01 13:20:47 -0500 | [diff] [blame] | 133 | |
Dave Liu | f6eda7f | 2006-10-25 14:41:21 -0500 | [diff] [blame] | 134 | |
| 135 | /* Read SPD parameters with I2C */ |
Eran Liberty | f046ccd | 2005-07-28 10:08:46 -0500 | [diff] [blame] | 136 | CFG_READ_SPD(SPD_EEPROM_ADDRESS, 0, 1, (uchar *) & spd, sizeof (spd)); |
Rafal Jaworowski | dc9e499 | 2006-03-16 17:46:46 +0100 | [diff] [blame] | 137 | #ifdef SPD_DEBUG |
| 138 | spd_debug(&spd); |
| 139 | #endif |
Dave Liu | f6eda7f | 2006-10-25 14:41:21 -0500 | [diff] [blame] | 140 | /* Check the memory type */ |
| 141 | if (spd.mem_type != SPD_MEMTYPE_DDR) { |
| 142 | printf("DDR: Module mem type is %02X\n", spd.mem_type); |
| 143 | return 0; |
| 144 | } |
| 145 | |
| 146 | /* Check the number of physical bank */ |
Eran Liberty | f046ccd | 2005-07-28 10:08:46 -0500 | [diff] [blame] | 147 | if (spd.nrows > 2) { |
Dave Liu | f6eda7f | 2006-10-25 14:41:21 -0500 | [diff] [blame] | 148 | printf("DDR: The number of physical bank is %02X\n", spd.nrows); |
Eran Liberty | f046ccd | 2005-07-28 10:08:46 -0500 | [diff] [blame] | 149 | return 0; |
| 150 | } |
| 151 | |
Dave Liu | f6eda7f | 2006-10-25 14:41:21 -0500 | [diff] [blame] | 152 | /* Check if the number of row of the module is in the range of DDRC */ |
| 153 | if (spd.nrow_addr < 12 || spd.nrow_addr > 14) { |
| 154 | printf("DDR: Row number is out of range of DDRC, row=%02X\n", |
| 155 | spd.nrow_addr); |
Eran Liberty | f046ccd | 2005-07-28 10:08:46 -0500 | [diff] [blame] | 156 | return 0; |
| 157 | } |
| 158 | |
Dave Liu | f6eda7f | 2006-10-25 14:41:21 -0500 | [diff] [blame] | 159 | /* Check if the number of col of the module is in the range of DDRC */ |
| 160 | if (spd.ncol_addr < 8 || spd.ncol_addr > 11) { |
| 161 | printf("DDR: Col number is out of range of DDRC, col=%02X\n", |
| 162 | spd.ncol_addr); |
| 163 | return 0; |
| 164 | } |
| 165 | /* Setup DDR chip select register */ |
Eran Liberty | f046ccd | 2005-07-28 10:08:46 -0500 | [diff] [blame] | 166 | ddr->csbnds[2].csbnds = (banksize(spd.row_dens) >> 24) - 1; |
| 167 | ddr->cs_config[2] = ( 1 << 31 |
| 168 | | (spd.nrow_addr - 12) << 8 |
| 169 | | (spd.ncol_addr - 8) ); |
| 170 | debug("\n"); |
| 171 | debug("cs2_bnds = 0x%08x\n",ddr->csbnds[2].csbnds); |
| 172 | debug("cs2_config = 0x%08x\n",ddr->cs_config[2]); |
Jon Loeliger | de1d0a6 | 2005-08-01 13:20:47 -0500 | [diff] [blame] | 173 | |
Eran Liberty | f046ccd | 2005-07-28 10:08:46 -0500 | [diff] [blame] | 174 | if (spd.nrows == 2) { |
| 175 | ddr->csbnds[3].csbnds = ( (banksize(spd.row_dens) >> 8) |
| 176 | | ((banksize(spd.row_dens) >> 23) - 1) ); |
| 177 | ddr->cs_config[3] = ( 1<<31 |
| 178 | | (spd.nrow_addr-12) << 8 |
| 179 | | (spd.ncol_addr-8) ); |
| 180 | debug("cs3_bnds = 0x%08x\n",ddr->csbnds[3].csbnds); |
| 181 | debug("cs3_config = 0x%08x\n",ddr->cs_config[3]); |
| 182 | } |
| 183 | |
| 184 | if (spd.mem_type != 0x07) { |
| 185 | puts("No DDR module found!\n"); |
| 186 | return 0; |
| 187 | } |
| 188 | |
| 189 | /* |
| 190 | * Figure out memory size in Megabytes. |
| 191 | */ |
| 192 | memsize = spd.nrows * banksize(spd.row_dens) / 0x100000; |
| 193 | |
| 194 | /* |
| 195 | * First supported LAW size is 16M, at LAWAR_SIZE_16M == 23. |
| 196 | */ |
| 197 | law_size = 19 + __ilog2(memsize); |
| 198 | |
| 199 | /* |
| 200 | * Set up LAWBAR for all of DDR. |
| 201 | */ |
| 202 | ecm->bar = ((CFG_DDR_SDRAM_BASE>>12) & 0xfffff); |
| 203 | ecm->ar = (LAWAR_EN | LAWAR_TRGT_IF_DDR | (LAWAR_SIZE & law_size)); |
| 204 | debug("DDR:bar=0x%08x\n", ecm->bar); |
| 205 | debug("DDR:ar=0x%08x\n", ecm->ar); |
| 206 | |
| 207 | /* |
Dave Liu | f6eda7f | 2006-10-25 14:41:21 -0500 | [diff] [blame] | 208 | * Find the largest CAS by locating the highest 1 bit |
| 209 | * in the spd.cas_lat field. Translate it to a DDR |
| 210 | * controller field value: |
| 211 | * |
| 212 | * CAS Lat DDR I Ctrl |
| 213 | * Clocks SPD Bit Value |
| 214 | * -------+--------+--------- |
| 215 | * 1.0 0 001 |
| 216 | * 1.5 1 010 |
| 217 | * 2.0 2 011 |
| 218 | * 2.5 3 100 |
| 219 | * 3.0 4 101 |
| 220 | * 3.5 5 110 |
| 221 | * 4.0 6 111 |
Eran Liberty | f046ccd | 2005-07-28 10:08:46 -0500 | [diff] [blame] | 222 | */ |
Dave Liu | f6eda7f | 2006-10-25 14:41:21 -0500 | [diff] [blame] | 223 | caslat = __ilog2(spd.cas_lat); |
| 224 | |
| 225 | if (caslat > 4 ) { |
| 226 | printf("DDR: Invalid SPD CAS Latency, caslat=%02X\n", caslat); |
Eran Liberty | f046ccd | 2005-07-28 10:08:46 -0500 | [diff] [blame] | 227 | return 0; |
| 228 | } |
Dave Liu | f6eda7f | 2006-10-25 14:41:21 -0500 | [diff] [blame] | 229 | max_bus_clk = 1000 *10 / (((spd.clk_cycle & 0xF0) >> 4) * 10 |
| 230 | + (spd.clk_cycle & 0x0f)); |
| 231 | max_data_rate = max_bus_clk * 2; |
Eran Liberty | f046ccd | 2005-07-28 10:08:46 -0500 | [diff] [blame] | 232 | |
Dave Liu | f6eda7f | 2006-10-25 14:41:21 -0500 | [diff] [blame] | 233 | debug("DDR:Module maximum data rate is: %dMhz\n", max_data_rate); |
Eran Liberty | f046ccd | 2005-07-28 10:08:46 -0500 | [diff] [blame] | 234 | |
Dave Liu | f6eda7f | 2006-10-25 14:41:21 -0500 | [diff] [blame] | 235 | ddrc_clk = get_ddr_clk(0) / 1000000; |
| 236 | |
| 237 | if (max_data_rate >= 390) { /* it is DDR 400 */ |
| 238 | printf("DDR: platform not support DDR 400\n"); |
Eran Liberty | f046ccd | 2005-07-28 10:08:46 -0500 | [diff] [blame] | 239 | return 0; |
Dave Liu | f6eda7f | 2006-10-25 14:41:21 -0500 | [diff] [blame] | 240 | } else if (max_data_rate >= 323) { /* it is DDR 333 */ |
| 241 | if (ddrc_clk <= 350 && ddrc_clk > 280) { |
| 242 | /* DDRC clk at 280~350 */ |
| 243 | effective_data_rate = 333; /* 6ns */ |
| 244 | caslat = caslat; |
| 245 | } else if (ddrc_clk <= 280 && ddrc_clk > 230) { |
| 246 | /* DDRC clk at 230~280 */ |
| 247 | if (spd.clk_cycle2 == 0x75) { |
| 248 | effective_data_rate = 266; /* 7.5ns */ |
| 249 | caslat = caslat - 1; |
| 250 | } |
| 251 | } else if (ddrc_clk <= 230 && ddrc_clk > 90) { |
| 252 | /* DDRC clk at 90~230 */ |
| 253 | if (spd.clk_cycle3 == 0xa0) { |
| 254 | effective_data_rate = 200; /* 10ns */ |
| 255 | caslat = caslat - 2; |
| 256 | } |
| 257 | } |
| 258 | } else if (max_data_rate >= 256) { /* it is DDR 266 */ |
| 259 | if (ddrc_clk <= 350 && ddrc_clk > 280) { |
| 260 | /* DDRC clk at 280~350 */ |
| 261 | printf("DDR: DDR controller freq is more than " |
| 262 | "max data rate of the module\n"); |
| 263 | return 0; |
| 264 | } else if (ddrc_clk <= 280 && ddrc_clk > 230) { |
| 265 | /* DDRC clk at 230~280 */ |
| 266 | effective_data_rate = 266; /* 7.5ns */ |
| 267 | caslat = caslat; |
| 268 | } else if (ddrc_clk <= 230 && ddrc_clk > 90) { |
| 269 | /* DDRC clk at 90~230 */ |
| 270 | if (spd.clk_cycle2 == 0xa0) { |
| 271 | effective_data_rate = 200; /* 10ns */ |
| 272 | caslat = caslat - 1; |
| 273 | } |
| 274 | } |
| 275 | } else if (max_data_rate >= 190) { /* it is DDR 200 */ |
| 276 | if (ddrc_clk <= 350 && ddrc_clk > 230) { |
| 277 | /* DDRC clk at 230~350 */ |
| 278 | printf("DDR: DDR controller freq is more than " |
| 279 | "max data rate of the module\n"); |
| 280 | return 0; |
| 281 | } else if (ddrc_clk <= 230 && ddrc_clk > 90) { |
| 282 | /* DDRC clk at 90~230 */ |
| 283 | effective_data_rate = 200; /* 10ns */ |
| 284 | caslat = caslat; |
| 285 | } |
Eran Liberty | f046ccd | 2005-07-28 10:08:46 -0500 | [diff] [blame] | 286 | } |
| 287 | |
Timur Tabi | bed85ca | 2006-10-31 18:13:36 -0600 | [diff] [blame^] | 288 | #ifdef CONFIG_MPC834X |
| 289 | /* Errata DDR6 |
| 290 | This errata affects all MPC8349E, MPC8343E and MPC8347E processors. |
| 291 | */ |
| 292 | if ((tmp1 >= 280) && (tmp1 < 350)) /* CSB=333 */ |
| 293 | { |
| 294 | if (spd.mid[0] == 0x2c) { |
| 295 | /* Micron memory running at 333 MHz */ |
| 296 | /* Chances are, U-Boot will crash before we get here, |
| 297 | but just in case, display a message and return error. */ |
| 298 | printf("Micron DDR not supported at 333MHz CSB\n"); |
| 299 | return 0; |
| 300 | } else if (spd.mid[0] == 0xad) { |
| 301 | printf("Hynix DDR does not require Errata DDR6\n"); |
| 302 | } else { |
| 303 | /* enable 2 cycle Earlier for CL=2.5 or 3 */ |
| 304 | ddr->debug_reg = 0x202c0000; |
| 305 | printf("Errata DDR6 (debug_reg=0x%x)\n", ddr->debug_reg); |
| 306 | } |
| 307 | caslat_83xx = caslat; |
| 308 | } |
| 309 | |
| 310 | if ((tmp1 >= 230) && (tmp1 < 280)) { /* CSB=266 */ |
| 311 | if (spd.mid[0] != 0x2c) /* non-Micron */ |
| 312 | caslat_83xx = caslat - 1; |
| 313 | |
| 314 | } |
| 315 | |
| 316 | if ((tmp1 >= 90) && (tmp1 < 230)) { /* CSB=200 */ |
| 317 | caslat = 3; |
| 318 | caslat_83xx = 2; |
| 319 | } |
| 320 | #endif |
| 321 | |
Eran Liberty | f046ccd | 2005-07-28 10:08:46 -0500 | [diff] [blame] | 322 | /* |
| 323 | * note: caslat must also be programmed into ddr->sdram_mode |
| 324 | * register. |
| 325 | * |
| 326 | * note: WRREC(Twr) and WRTORD(Twtr) are not in SPD, |
| 327 | * use conservative value here. |
| 328 | */ |
Dave Liu | f6eda7f | 2006-10-25 14:41:21 -0500 | [diff] [blame] | 329 | caslat_ctrl = (caslat + 1) & 0x07; /* see as above */ |
Eran Liberty | f046ccd | 2005-07-28 10:08:46 -0500 | [diff] [blame] | 330 | |
| 331 | ddr->timing_cfg_1 = |
| 332 | (((picos_to_clk(spd.trp * 250) & 0x07) << 28 ) | |
| 333 | ((picos_to_clk(spd.tras * 1000) & 0x0f ) << 24 ) | |
| 334 | ((picos_to_clk(spd.trcd * 250) & 0x07) << 20 ) | |
Timur Tabi | bed85ca | 2006-10-31 18:13:36 -0600 | [diff] [blame^] | 335 | #ifdef CONFIG_MPC834x |
| 336 | ((caslat_83xx & 0x07) << 16 ) | |
| 337 | #else |
Dave Liu | f6eda7f | 2006-10-25 14:41:21 -0500 | [diff] [blame] | 338 | ((caslat_ctrl & 0x07) << 16 ) | |
Timur Tabi | bed85ca | 2006-10-31 18:13:36 -0600 | [diff] [blame^] | 339 | #endif |
Dave Liu | f6eda7f | 2006-10-25 14:41:21 -0500 | [diff] [blame] | 340 | (((picos_to_clk(spd.trfc * 1000) - 8) & 0x0f) << 12 ) | |
Eran Liberty | f046ccd | 2005-07-28 10:08:46 -0500 | [diff] [blame] | 341 | ( 0x300 ) | |
| 342 | ((picos_to_clk(spd.trrd * 250) & 0x07) << 4) | 1); |
| 343 | |
| 344 | ddr->timing_cfg_2 = 0x00000800; |
| 345 | |
| 346 | debug("DDR:timing_cfg_1=0x%08x\n", ddr->timing_cfg_1); |
| 347 | debug("DDR:timing_cfg_2=0x%08x\n", ddr->timing_cfg_2); |
Dave Liu | f6eda7f | 2006-10-25 14:41:21 -0500 | [diff] [blame] | 348 | /* Setup init value, but not enable */ |
| 349 | ddr->sdram_cfg = 0x42000000; |
Eran Liberty | f046ccd | 2005-07-28 10:08:46 -0500 | [diff] [blame] | 350 | |
Dave Liu | f6eda7f | 2006-10-25 14:41:21 -0500 | [diff] [blame] | 351 | /* Check DIMM data bus width */ |
| 352 | if (spd.dataw_lsb == 0x20) |
| 353 | { |
| 354 | burstlen = 0x03; /* 32 bit data bus, burst len is 8 */ |
| 355 | printf("\n DDR DIMM: data bus width is 32 bit"); |
| 356 | } |
| 357 | else |
| 358 | { |
| 359 | burstlen = 0x02; /* Others act as 64 bit bus, burst len is 4 */ |
| 360 | printf("\n DDR DIMM: data bus width is 64 bit"); |
| 361 | } |
| 362 | |
| 363 | /* Is this an ECC DDR chip? */ |
| 364 | if (spd.config == 0x02) { |
| 365 | printf(" with ECC\n"); |
| 366 | } |
| 367 | else |
| 368 | printf(" without ECC\n"); |
| 369 | |
| 370 | /* Burst length is always 4 for 64 bit data bus, 8 for 32 bit data bus, |
| 371 | Burst type is sequential |
Eran Liberty | f046ccd | 2005-07-28 10:08:46 -0500 | [diff] [blame] | 372 | */ |
Eran Liberty | f046ccd | 2005-07-28 10:08:46 -0500 | [diff] [blame] | 373 | switch(caslat) { |
Dave Liu | f6eda7f | 2006-10-25 14:41:21 -0500 | [diff] [blame] | 374 | case 1: |
| 375 | ddr->sdram_mode = 0x50 | burstlen; /* CL=1.5 */ |
| 376 | break; |
Eran Liberty | f046ccd | 2005-07-28 10:08:46 -0500 | [diff] [blame] | 377 | case 2: |
Dave Liu | f6eda7f | 2006-10-25 14:41:21 -0500 | [diff] [blame] | 378 | ddr->sdram_mode = 0x20 | burstlen; /* CL=2.0 */ |
Eran Liberty | f046ccd | 2005-07-28 10:08:46 -0500 | [diff] [blame] | 379 | break; |
| 380 | case 3: |
Dave Liu | f6eda7f | 2006-10-25 14:41:21 -0500 | [diff] [blame] | 381 | ddr->sdram_mode = 0x60 | burstlen; /* CL=2.5 */ |
Eran Liberty | f046ccd | 2005-07-28 10:08:46 -0500 | [diff] [blame] | 382 | break; |
| 383 | case 4: |
Dave Liu | f6eda7f | 2006-10-25 14:41:21 -0500 | [diff] [blame] | 384 | ddr->sdram_mode = 0x30 | burstlen; /* CL=3.0 */ |
Eran Liberty | f046ccd | 2005-07-28 10:08:46 -0500 | [diff] [blame] | 385 | break; |
| 386 | default: |
Dave Liu | f6eda7f | 2006-10-25 14:41:21 -0500 | [diff] [blame] | 387 | printf("DDR:only CAS Latency 1.5, 2.0, 2.5, 3.0 " |
| 388 | "is supported.\n"); |
Eran Liberty | f046ccd | 2005-07-28 10:08:46 -0500 | [diff] [blame] | 389 | return 0; |
| 390 | } |
| 391 | debug("DDR:sdram_mode=0x%08x\n", ddr->sdram_mode); |
| 392 | |
| 393 | switch(spd.refresh) { |
| 394 | case 0x00: |
| 395 | case 0x80: |
| 396 | tmp = picos_to_clk(15625000); |
| 397 | break; |
| 398 | case 0x01: |
| 399 | case 0x81: |
| 400 | tmp = picos_to_clk(3900000); |
| 401 | break; |
| 402 | case 0x02: |
| 403 | case 0x82: |
| 404 | tmp = picos_to_clk(7800000); |
| 405 | break; |
| 406 | case 0x03: |
| 407 | case 0x83: |
| 408 | tmp = picos_to_clk(31300000); |
| 409 | break; |
| 410 | case 0x04: |
| 411 | case 0x84: |
| 412 | tmp = picos_to_clk(62500000); |
| 413 | break; |
| 414 | case 0x05: |
| 415 | case 0x85: |
| 416 | tmp = picos_to_clk(125000000); |
| 417 | break; |
| 418 | default: |
| 419 | tmp = 0x512; |
| 420 | break; |
| 421 | } |
| 422 | |
| 423 | /* |
| 424 | * Set BSTOPRE to 0x100 for page mode |
| 425 | * If auto-charge is used, set BSTOPRE = 0 |
| 426 | */ |
| 427 | ddr->sdram_interval = ((tmp & 0x3fff) << 16) | 0x100; |
| 428 | debug("DDR:sdram_interval=0x%08x\n", ddr->sdram_interval); |
| 429 | |
Dave Liu | f6eda7f | 2006-10-25 14:41:21 -0500 | [diff] [blame] | 430 | /* SS_EN = 0, source synchronous disable |
| 431 | * CLK_ADJST = 0, MCK/MCK# is launched aligned with addr/cmd |
| 432 | */ |
| 433 | ddr->sdram_clk_cntl = 0x00000000; |
| 434 | debug("DDR:sdram_clk_cntl=0x%08x\n", ddr->sdram_clk_cntl); |
Marian Balakowicz | 4c8d1ec | 2006-03-14 16:23:35 +0100 | [diff] [blame] | 435 | |
Eran Liberty | f046ccd | 2005-07-28 10:08:46 -0500 | [diff] [blame] | 436 | asm("sync;isync"); |
| 437 | |
Dave Liu | f6eda7f | 2006-10-25 14:41:21 -0500 | [diff] [blame] | 438 | udelay(600); |
Eran Liberty | f046ccd | 2005-07-28 10:08:46 -0500 | [diff] [blame] | 439 | |
| 440 | /* |
| 441 | * Figure out the settings for the sdram_cfg register. Build up |
| 442 | * the entire register in 'tmp' before writing since the write into |
| 443 | * the register will actually enable the memory controller, and all |
| 444 | * settings must be done before enabling. |
| 445 | * |
| 446 | * sdram_cfg[0] = 1 (ddr sdram logic enable) |
| 447 | * sdram_cfg[1] = 1 (self-refresh-enable) |
| 448 | * sdram_cfg[6:7] = 2 (SDRAM type = DDR SDRAM) |
Dave Liu | f6eda7f | 2006-10-25 14:41:21 -0500 | [diff] [blame] | 449 | * sdram_cfg[12] = 0 (32_BE =0 , 64 bit bus mode) |
| 450 | * sdram_cfg[13] = 0 (8_BE =0, 4-beat bursts) |
Eran Liberty | f046ccd | 2005-07-28 10:08:46 -0500 | [diff] [blame] | 451 | */ |
Dave Liu | f6eda7f | 2006-10-25 14:41:21 -0500 | [diff] [blame] | 452 | sdram_cfg = 0xC2000000; |
Eran Liberty | f046ccd | 2005-07-28 10:08:46 -0500 | [diff] [blame] | 453 | |
Dave Liu | f6eda7f | 2006-10-25 14:41:21 -0500 | [diff] [blame] | 454 | /* sdram_cfg[3] = RD_EN - registered DIMM enable */ |
| 455 | if (spd.mod_attr & 0x02) { |
| 456 | sdram_cfg |= 0x10000000; |
Eran Liberty | f046ccd | 2005-07-28 10:08:46 -0500 | [diff] [blame] | 457 | } |
| 458 | |
Dave Liu | f6eda7f | 2006-10-25 14:41:21 -0500 | [diff] [blame] | 459 | /* The DIMM is 32bit width */ |
| 460 | if (spd.dataw_lsb == 0x20) { |
| 461 | sdram_cfg |= 0x000C0000; |
| 462 | } |
| 463 | ddrc_ecc_enable = 0; |
| 464 | |
Eran Liberty | f046ccd | 2005-07-28 10:08:46 -0500 | [diff] [blame] | 465 | #if defined(CONFIG_DDR_ECC) |
Dave Liu | f6eda7f | 2006-10-25 14:41:21 -0500 | [diff] [blame] | 466 | /* Enable ECC with sdram_cfg[2] */ |
Eran Liberty | f046ccd | 2005-07-28 10:08:46 -0500 | [diff] [blame] | 467 | if (spd.config == 0x02) { |
Dave Liu | f6eda7f | 2006-10-25 14:41:21 -0500 | [diff] [blame] | 468 | sdram_cfg |= 0x20000000; |
| 469 | ddrc_ecc_enable = 1; |
| 470 | /* disable error detection */ |
| 471 | ddr->err_disable = ~ECC_ERROR_ENABLE; |
| 472 | /* set single bit error threshold to maximum value, |
| 473 | * reset counter to zero */ |
| 474 | ddr->err_sbe = (255 << ECC_ERROR_MAN_SBET_SHIFT) | |
| 475 | (0 << ECC_ERROR_MAN_SBEC_SHIFT); |
Eran Liberty | f046ccd | 2005-07-28 10:08:46 -0500 | [diff] [blame] | 476 | } |
Dave Liu | f6eda7f | 2006-10-25 14:41:21 -0500 | [diff] [blame] | 477 | |
| 478 | debug("DDR:err_disable=0x%08x\n", ddr->err_disable); |
| 479 | debug("DDR:err_sbe=0x%08x\n", ddr->err_sbe); |
Eran Liberty | f046ccd | 2005-07-28 10:08:46 -0500 | [diff] [blame] | 480 | #endif |
Dave Liu | f6eda7f | 2006-10-25 14:41:21 -0500 | [diff] [blame] | 481 | printf(" DDRC ECC mode: %s", ddrc_ecc_enable ? "ON":"OFF"); |
Eran Liberty | f046ccd | 2005-07-28 10:08:46 -0500 | [diff] [blame] | 482 | |
| 483 | #if defined(CONFIG_DDR_2T_TIMING) |
| 484 | /* |
| 485 | * Enable 2T timing by setting sdram_cfg[16]. |
| 486 | */ |
Dave Liu | f6eda7f | 2006-10-25 14:41:21 -0500 | [diff] [blame] | 487 | sdram_cfg |= SDRAM_CFG_2T_EN; |
Eran Liberty | f046ccd | 2005-07-28 10:08:46 -0500 | [diff] [blame] | 488 | #endif |
Dave Liu | f6eda7f | 2006-10-25 14:41:21 -0500 | [diff] [blame] | 489 | /* Enable controller, and GO! */ |
| 490 | ddr->sdram_cfg = sdram_cfg; |
Eran Liberty | f046ccd | 2005-07-28 10:08:46 -0500 | [diff] [blame] | 491 | asm("sync;isync"); |
| 492 | udelay(500); |
| 493 | |
| 494 | debug("DDR:sdram_cfg=0x%08x\n", ddr->sdram_cfg); |
Rafal Jaworowski | dc9e499 | 2006-03-16 17:46:46 +0100 | [diff] [blame] | 495 | return memsize; /*in MBytes*/ |
Eran Liberty | f046ccd | 2005-07-28 10:08:46 -0500 | [diff] [blame] | 496 | } |
Eran Liberty | f046ccd | 2005-07-28 10:08:46 -0500 | [diff] [blame] | 497 | #endif /* CONFIG_SPD_EEPROM */ |
| 498 | |
| 499 | |
Dave Liu | f6eda7f | 2006-10-25 14:41:21 -0500 | [diff] [blame] | 500 | #if defined(CONFIG_DDR_ECC) && !defined(CONFIG_ECC_INIT_VIA_DDRC) |
Eran Liberty | f046ccd | 2005-07-28 10:08:46 -0500 | [diff] [blame] | 501 | /* |
Marian Balakowicz | 4c8d1ec | 2006-03-14 16:23:35 +0100 | [diff] [blame] | 502 | * Use timebase counter, get_timer() is not availabe |
| 503 | * at this point of initialization yet. |
| 504 | */ |
| 505 | static __inline__ unsigned long get_tbms (void) |
| 506 | { |
| 507 | unsigned long tbl; |
| 508 | unsigned long tbu1, tbu2; |
| 509 | unsigned long ms; |
| 510 | unsigned long long tmp; |
| 511 | |
| 512 | ulong tbclk = get_tbclk(); |
| 513 | |
| 514 | /* get the timebase ticks */ |
| 515 | do { |
| 516 | asm volatile ("mftbu %0":"=r" (tbu1):); |
| 517 | asm volatile ("mftb %0":"=r" (tbl):); |
| 518 | asm volatile ("mftbu %0":"=r" (tbu2):); |
| 519 | } while (tbu1 != tbu2); |
| 520 | |
| 521 | /* convert ticks to ms */ |
| 522 | tmp = (unsigned long long)(tbu1); |
| 523 | tmp = (tmp << 32); |
| 524 | tmp += (unsigned long long)(tbl); |
| 525 | ms = tmp/(tbclk/1000); |
| 526 | |
| 527 | return ms; |
| 528 | } |
| 529 | |
| 530 | /* |
Eran Liberty | f046ccd | 2005-07-28 10:08:46 -0500 | [diff] [blame] | 531 | * Initialize all of memory for ECC, then enable errors. |
| 532 | */ |
Wolfgang Denk | cf48eb9 | 2006-04-16 10:51:58 +0200 | [diff] [blame] | 533 | /* #define CONFIG_DDR_ECC_INIT_VIA_DMA */ |
Marian Balakowicz | 4c8d1ec | 2006-03-14 16:23:35 +0100 | [diff] [blame] | 534 | void ddr_enable_ecc(unsigned int dram_size) |
Eran Liberty | f046ccd | 2005-07-28 10:08:46 -0500 | [diff] [blame] | 535 | { |
Marian Balakowicz | 4c8d1ec | 2006-03-14 16:23:35 +0100 | [diff] [blame] | 536 | uint *p; |
Eran Liberty | f046ccd | 2005-07-28 10:08:46 -0500 | [diff] [blame] | 537 | volatile immap_t *immap = (immap_t *)CFG_IMMRBAR; |
Dave Liu | f6eda7f | 2006-10-25 14:41:21 -0500 | [diff] [blame] | 538 | volatile ddr83xx_t *ddr= &immap->ddr; |
Marian Balakowicz | 4c8d1ec | 2006-03-14 16:23:35 +0100 | [diff] [blame] | 539 | unsigned long t_start, t_end; |
| 540 | #if defined(CONFIG_DDR_ECC_INIT_VIA_DMA) |
| 541 | uint i; |
| 542 | #endif |
Eran Liberty | f046ccd | 2005-07-28 10:08:46 -0500 | [diff] [blame] | 543 | |
Marian Balakowicz | 4c8d1ec | 2006-03-14 16:23:35 +0100 | [diff] [blame] | 544 | debug("Initialize a Cachline in DRAM\n"); |
| 545 | icache_enable(); |
| 546 | |
| 547 | #if defined(CONFIG_DDR_ECC_INIT_VIA_DMA) |
| 548 | /* Initialise DMA for direct Transfers */ |
Eran Liberty | f046ccd | 2005-07-28 10:08:46 -0500 | [diff] [blame] | 549 | dma_init(); |
Marian Balakowicz | 4c8d1ec | 2006-03-14 16:23:35 +0100 | [diff] [blame] | 550 | #endif |
Eran Liberty | f046ccd | 2005-07-28 10:08:46 -0500 | [diff] [blame] | 551 | |
Marian Balakowicz | 4c8d1ec | 2006-03-14 16:23:35 +0100 | [diff] [blame] | 552 | t_start = get_tbms(); |
| 553 | |
| 554 | #if !defined(CONFIG_DDR_ECC_INIT_VIA_DMA) |
| 555 | debug("DDR init: Cache flush method\n"); |
| 556 | for (p = 0; p < (uint *)(dram_size); p++) { |
Eran Liberty | f046ccd | 2005-07-28 10:08:46 -0500 | [diff] [blame] | 557 | if (((unsigned int)p & 0x1f) == 0) { |
| 558 | ppcDcbz((unsigned long) p); |
| 559 | } |
Marian Balakowicz | 4c8d1ec | 2006-03-14 16:23:35 +0100 | [diff] [blame] | 560 | |
| 561 | /* write pattern to cache and flush */ |
Eran Liberty | f046ccd | 2005-07-28 10:08:46 -0500 | [diff] [blame] | 562 | *p = (unsigned int)0xdeadbeef; |
Marian Balakowicz | 4c8d1ec | 2006-03-14 16:23:35 +0100 | [diff] [blame] | 563 | |
Eran Liberty | f046ccd | 2005-07-28 10:08:46 -0500 | [diff] [blame] | 564 | if (((unsigned int)p & 0x1c) == 0x1c) { |
| 565 | ppcDcbf((unsigned long) p); |
| 566 | } |
| 567 | } |
Marian Balakowicz | 4c8d1ec | 2006-03-14 16:23:35 +0100 | [diff] [blame] | 568 | #else |
| 569 | printf("DDR init: DMA method\n"); |
| 570 | for (p = 0; p < (uint *)(8 * 1024); p++) { |
| 571 | /* zero one data cache line */ |
| 572 | if (((unsigned int)p & 0x1f) == 0) { |
| 573 | ppcDcbz((unsigned long)p); |
| 574 | } |
| 575 | |
| 576 | /* write pattern to it and flush */ |
| 577 | *p = (unsigned int)0xdeadbeef; |
| 578 | |
| 579 | if (((unsigned int)p & 0x1c) == 0x1c) { |
| 580 | ppcDcbf((unsigned long)p); |
| 581 | } |
| 582 | } |
Eran Liberty | f046ccd | 2005-07-28 10:08:46 -0500 | [diff] [blame] | 583 | |
| 584 | /* 8K */ |
| 585 | dma_xfer((uint *)0x2000, 0x2000, (uint *)0); |
| 586 | /* 16K */ |
| 587 | dma_xfer((uint *)0x4000, 0x4000, (uint *)0); |
| 588 | /* 32K */ |
| 589 | dma_xfer((uint *)0x8000, 0x8000, (uint *)0); |
| 590 | /* 64K */ |
| 591 | dma_xfer((uint *)0x10000, 0x10000, (uint *)0); |
| 592 | /* 128k */ |
| 593 | dma_xfer((uint *)0x20000, 0x20000, (uint *)0); |
| 594 | /* 256k */ |
| 595 | dma_xfer((uint *)0x40000, 0x40000, (uint *)0); |
| 596 | /* 512k */ |
| 597 | dma_xfer((uint *)0x80000, 0x80000, (uint *)0); |
| 598 | /* 1M */ |
| 599 | dma_xfer((uint *)0x100000, 0x100000, (uint *)0); |
| 600 | /* 2M */ |
| 601 | dma_xfer((uint *)0x200000, 0x200000, (uint *)0); |
| 602 | /* 4M */ |
| 603 | dma_xfer((uint *)0x400000, 0x400000, (uint *)0); |
| 604 | |
| 605 | for (i = 1; i < dram_size / 0x800000; i++) { |
| 606 | dma_xfer((uint *)(0x800000*i), 0x800000, (uint *)0); |
| 607 | } |
Jon Loeliger | de1d0a6 | 2005-08-01 13:20:47 -0500 | [diff] [blame] | 608 | #endif |
Eran Liberty | f046ccd | 2005-07-28 10:08:46 -0500 | [diff] [blame] | 609 | |
Marian Balakowicz | 4c8d1ec | 2006-03-14 16:23:35 +0100 | [diff] [blame] | 610 | t_end = get_tbms(); |
| 611 | icache_disable(); |
| 612 | |
| 613 | debug("\nREADY!!\n"); |
| 614 | debug("ddr init duration: %ld ms\n", t_end - t_start); |
| 615 | |
| 616 | /* Clear All ECC Errors */ |
| 617 | if ((ddr->err_detect & ECC_ERROR_DETECT_MME) == ECC_ERROR_DETECT_MME) |
| 618 | ddr->err_detect |= ECC_ERROR_DETECT_MME; |
| 619 | if ((ddr->err_detect & ECC_ERROR_DETECT_MBE) == ECC_ERROR_DETECT_MBE) |
| 620 | ddr->err_detect |= ECC_ERROR_DETECT_MBE; |
| 621 | if ((ddr->err_detect & ECC_ERROR_DETECT_SBE) == ECC_ERROR_DETECT_SBE) |
| 622 | ddr->err_detect |= ECC_ERROR_DETECT_SBE; |
| 623 | if ((ddr->err_detect & ECC_ERROR_DETECT_MSE) == ECC_ERROR_DETECT_MSE) |
| 624 | ddr->err_detect |= ECC_ERROR_DETECT_MSE; |
| 625 | |
| 626 | /* Disable ECC-Interrupts */ |
| 627 | ddr->err_int_en &= ECC_ERR_INT_DISABLE; |
| 628 | |
| 629 | /* Enable errors for ECC */ |
| 630 | ddr->err_disable &= ECC_ERROR_ENABLE; |
| 631 | |
| 632 | __asm__ __volatile__ ("sync"); |
| 633 | __asm__ __volatile__ ("isync"); |
| 634 | } |
Eran Liberty | f046ccd | 2005-07-28 10:08:46 -0500 | [diff] [blame] | 635 | #endif /* CONFIG_DDR_ECC */ |