wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1 | /* |
Stefan Roese | 8d98230 | 2007-01-18 10:25:34 +0100 | [diff] [blame] | 2 | * (C) Copyright 2005-2007 |
Stefan Roese | c443fe9 | 2005-11-22 13:20:42 +0100 | [diff] [blame] | 3 | * Stefan Roese, DENX Software Engineering, sr@denx.de. |
| 4 | * |
Stefan Roese | fd63793 | 2006-03-17 10:28:24 +0100 | [diff] [blame] | 5 | * (C) Copyright 2006 |
| 6 | * DAVE Srl <www.dave-tech.it> |
| 7 | * |
stroese | 55ca749 | 2004-07-15 14:41:13 +0000 | [diff] [blame] | 8 | * (C) Copyright 2002-2004 |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 9 | * Stefan Roese, esd gmbh germany, stefan.roese@esd-electronics.com |
| 10 | * |
Wolfgang Denk | bd8ec7e | 2013-10-07 13:07:26 +0200 | [diff] [blame] | 11 | * SPDX-License-Identifier: GPL-2.0+ |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 12 | */ |
| 13 | |
| 14 | #include <common.h> |
Stefan Roese | 247e9d7 | 2010-09-09 19:18:00 +0200 | [diff] [blame] | 15 | #include <asm/ppc4xx.h> |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 16 | #include <asm/processor.h> |
Stefan Roese | fd63793 | 2006-03-17 10:28:24 +0100 | [diff] [blame] | 17 | #include "sdram.h" |
Grant Erickson | b693341 | 2008-05-22 14:44:14 -0700 | [diff] [blame] | 18 | #include "ecc.h" |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 19 | |
Simon Glass | 39f90ba | 2017-03-31 08:40:25 -0600 | [diff] [blame] | 20 | DECLARE_GLOBAL_DATA_PTR; |
| 21 | |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 22 | #ifdef CONFIG_SDRAM_BANK0 |
| 23 | |
Stefan Roese | 8d98230 | 2007-01-18 10:25:34 +0100 | [diff] [blame] | 24 | #ifndef CONFIG_440 |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 25 | |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 26 | #ifndef CONFIG_SYS_SDRAM_TABLE |
stroese | 55ca749 | 2004-07-15 14:41:13 +0000 | [diff] [blame] | 27 | sdram_conf_t mb0cf[] = { |
Wolfgang Denk | ebd3deb | 2006-04-16 10:51:58 +0200 | [diff] [blame] | 28 | {(128 << 20), 13, 0x000A4001}, /* (0-128MB) Address Mode 3, 13x10(4) */ |
| 29 | {(64 << 20), 13, 0x00084001}, /* (0-64MB) Address Mode 3, 13x9(4) */ |
| 30 | {(32 << 20), 12, 0x00062001}, /* (0-32MB) Address Mode 2, 12x9(4) */ |
| 31 | {(16 << 20), 12, 0x00046001}, /* (0-16MB) Address Mode 4, 12x8(4) */ |
| 32 | {(4 << 20), 11, 0x00008001}, /* (0-4MB) Address Mode 5, 11x8(2) */ |
stroese | 55ca749 | 2004-07-15 14:41:13 +0000 | [diff] [blame] | 33 | }; |
Stefan Roese | c443fe9 | 2005-11-22 13:20:42 +0100 | [diff] [blame] | 34 | #else |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 35 | sdram_conf_t mb0cf[] = CONFIG_SYS_SDRAM_TABLE; |
Stefan Roese | c443fe9 | 2005-11-22 13:20:42 +0100 | [diff] [blame] | 36 | #endif |
| 37 | |
Robert P. J. Day | 0c91159 | 2016-05-23 06:49:21 -0400 | [diff] [blame] | 38 | #define N_MB0CF (ARRAY_SIZE(mb0cf)) |
stroese | 55ca749 | 2004-07-15 14:41:13 +0000 | [diff] [blame] | 39 | |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 40 | #ifdef CONFIG_SYS_SDRAM_CASL |
Stefan Roese | fd63793 | 2006-03-17 10:28:24 +0100 | [diff] [blame] | 41 | static ulong ns2clks(ulong ns) |
| 42 | { |
| 43 | ulong bus_period_x_10 = ONE_BILLION / (get_bus_freq(0) / 10); |
| 44 | |
| 45 | return ((ns * 10) + bus_period_x_10) / bus_period_x_10; |
| 46 | } |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 47 | #endif /* CONFIG_SYS_SDRAM_CASL */ |
Stefan Roese | fd63793 | 2006-03-17 10:28:24 +0100 | [diff] [blame] | 48 | |
| 49 | static ulong compute_sdtr1(ulong speed) |
| 50 | { |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 51 | #ifdef CONFIG_SYS_SDRAM_CASL |
Wolfgang Denk | ebd3deb | 2006-04-16 10:51:58 +0200 | [diff] [blame] | 52 | ulong tmp; |
| 53 | ulong sdtr1 = 0; |
Stefan Roese | fd63793 | 2006-03-17 10:28:24 +0100 | [diff] [blame] | 54 | |
Wolfgang Denk | ebd3deb | 2006-04-16 10:51:58 +0200 | [diff] [blame] | 55 | /* CASL */ |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 56 | if (CONFIG_SYS_SDRAM_CASL < 2) |
Wolfgang Denk | ebd3deb | 2006-04-16 10:51:58 +0200 | [diff] [blame] | 57 | sdtr1 |= (1 << SDRAM0_TR_CASL); |
| 58 | else |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 59 | if (CONFIG_SYS_SDRAM_CASL > 4) |
Wolfgang Denk | ebd3deb | 2006-04-16 10:51:58 +0200 | [diff] [blame] | 60 | sdtr1 |= (3 << SDRAM0_TR_CASL); |
| 61 | else |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 62 | sdtr1 |= ((CONFIG_SYS_SDRAM_CASL-1) << SDRAM0_TR_CASL); |
Stefan Roese | fd63793 | 2006-03-17 10:28:24 +0100 | [diff] [blame] | 63 | |
Wolfgang Denk | ebd3deb | 2006-04-16 10:51:58 +0200 | [diff] [blame] | 64 | /* PTA */ |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 65 | tmp = ns2clks(CONFIG_SYS_SDRAM_PTA); |
Wolfgang Denk | ebd3deb | 2006-04-16 10:51:58 +0200 | [diff] [blame] | 66 | if ((tmp >= 2) && (tmp <= 4)) |
| 67 | sdtr1 |= ((tmp-1) << SDRAM0_TR_PTA); |
| 68 | else |
| 69 | sdtr1 |= ((4-1) << SDRAM0_TR_PTA); |
Stefan Roese | fd63793 | 2006-03-17 10:28:24 +0100 | [diff] [blame] | 70 | |
Wolfgang Denk | ebd3deb | 2006-04-16 10:51:58 +0200 | [diff] [blame] | 71 | /* CTP */ |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 72 | tmp = ns2clks(CONFIG_SYS_SDRAM_CTP); |
Wolfgang Denk | ebd3deb | 2006-04-16 10:51:58 +0200 | [diff] [blame] | 73 | if ((tmp >= 2) && (tmp <= 4)) |
| 74 | sdtr1 |= ((tmp-1) << SDRAM0_TR_CTP); |
| 75 | else |
| 76 | sdtr1 |= ((4-1) << SDRAM0_TR_CTP); |
Stefan Roese | fd63793 | 2006-03-17 10:28:24 +0100 | [diff] [blame] | 77 | |
Wolfgang Denk | ebd3deb | 2006-04-16 10:51:58 +0200 | [diff] [blame] | 78 | /* LDF */ |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 79 | tmp = ns2clks(CONFIG_SYS_SDRAM_LDF); |
Wolfgang Denk | ebd3deb | 2006-04-16 10:51:58 +0200 | [diff] [blame] | 80 | if ((tmp >= 2) && (tmp <= 4)) |
| 81 | sdtr1 |= ((tmp-1) << SDRAM0_TR_LDF); |
| 82 | else |
| 83 | sdtr1 |= ((2-1) << SDRAM0_TR_LDF); |
Stefan Roese | fd63793 | 2006-03-17 10:28:24 +0100 | [diff] [blame] | 84 | |
Wolfgang Denk | ebd3deb | 2006-04-16 10:51:58 +0200 | [diff] [blame] | 85 | /* RFTA */ |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 86 | tmp = ns2clks(CONFIG_SYS_SDRAM_RFTA); |
Wolfgang Denk | ebd3deb | 2006-04-16 10:51:58 +0200 | [diff] [blame] | 87 | if ((tmp >= 4) && (tmp <= 10)) |
| 88 | sdtr1 |= ((tmp-4) << SDRAM0_TR_RFTA); |
| 89 | else |
| 90 | sdtr1 |= ((10-4) << SDRAM0_TR_RFTA); |
Stefan Roese | fd63793 | 2006-03-17 10:28:24 +0100 | [diff] [blame] | 91 | |
Wolfgang Denk | ebd3deb | 2006-04-16 10:51:58 +0200 | [diff] [blame] | 92 | /* RCD */ |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 93 | tmp = ns2clks(CONFIG_SYS_SDRAM_RCD); |
Wolfgang Denk | ebd3deb | 2006-04-16 10:51:58 +0200 | [diff] [blame] | 94 | if ((tmp >= 2) && (tmp <= 4)) |
| 95 | sdtr1 |= ((tmp-1) << SDRAM0_TR_RCD); |
| 96 | else |
| 97 | sdtr1 |= ((4-1) << SDRAM0_TR_RCD); |
Stefan Roese | fd63793 | 2006-03-17 10:28:24 +0100 | [diff] [blame] | 98 | |
Wolfgang Denk | ebd3deb | 2006-04-16 10:51:58 +0200 | [diff] [blame] | 99 | return sdtr1; |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 100 | #else /* CONFIG_SYS_SDRAM_CASL */ |
Wolfgang Denk | ebd3deb | 2006-04-16 10:51:58 +0200 | [diff] [blame] | 101 | /* |
| 102 | * If no values are configured in the board config file |
| 103 | * use the default values, which seem to be ok for most |
| 104 | * boards. |
| 105 | * |
| 106 | * REMARK: |
| 107 | * For new board ports we strongly recommend to define the |
| 108 | * correct values for the used SDRAM chips in your board |
| 109 | * config file (see PPChameleonEVB.h) |
| 110 | */ |
| 111 | if (speed > 100000000) { |
| 112 | /* |
| 113 | * 133 MHz SDRAM |
| 114 | */ |
| 115 | return 0x01074015; |
| 116 | } else { |
| 117 | /* |
| 118 | * default: 100 MHz SDRAM |
| 119 | */ |
| 120 | return 0x0086400d; |
| 121 | } |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 122 | #endif /* CONFIG_SYS_SDRAM_CASL */ |
Stefan Roese | fd63793 | 2006-03-17 10:28:24 +0100 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | /* refresh is expressed in ms */ |
| 126 | static ulong compute_rtr(ulong speed, ulong rows, ulong refresh) |
| 127 | { |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 128 | #ifdef CONFIG_SYS_SDRAM_CASL |
Wolfgang Denk | ebd3deb | 2006-04-16 10:51:58 +0200 | [diff] [blame] | 129 | ulong tmp; |
Stefan Roese | fd63793 | 2006-03-17 10:28:24 +0100 | [diff] [blame] | 130 | |
Wolfgang Denk | ebd3deb | 2006-04-16 10:51:58 +0200 | [diff] [blame] | 131 | tmp = ((refresh*1000*1000) / (1 << rows)) * (speed / 1000); |
| 132 | tmp /= 1000000; |
Stefan Roese | fd63793 | 2006-03-17 10:28:24 +0100 | [diff] [blame] | 133 | |
Wolfgang Denk | ebd3deb | 2006-04-16 10:51:58 +0200 | [diff] [blame] | 134 | return ((tmp & 0x00003FF8) << 16); |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 135 | #else /* CONFIG_SYS_SDRAM_CASL */ |
Wolfgang Denk | ebd3deb | 2006-04-16 10:51:58 +0200 | [diff] [blame] | 136 | if (speed > 100000000) { |
| 137 | /* |
| 138 | * 133 MHz SDRAM |
| 139 | */ |
Stefan Roese | fd63793 | 2006-03-17 10:28:24 +0100 | [diff] [blame] | 140 | return 0x07f00000; |
Wolfgang Denk | ebd3deb | 2006-04-16 10:51:58 +0200 | [diff] [blame] | 141 | } else { |
| 142 | /* |
| 143 | * default: 100 MHz SDRAM |
| 144 | */ |
Stefan Roese | fd63793 | 2006-03-17 10:28:24 +0100 | [diff] [blame] | 145 | return 0x05f00000; |
Wolfgang Denk | ebd3deb | 2006-04-16 10:51:58 +0200 | [diff] [blame] | 146 | } |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 147 | #endif /* CONFIG_SYS_SDRAM_CASL */ |
Stefan Roese | fd63793 | 2006-03-17 10:28:24 +0100 | [diff] [blame] | 148 | } |
| 149 | |
Stefan Roese | c443fe9 | 2005-11-22 13:20:42 +0100 | [diff] [blame] | 150 | /* |
| 151 | * Autodetect onboard SDRAM on 405 platforms |
| 152 | */ |
Simon Glass | 39f90ba | 2017-03-31 08:40:25 -0600 | [diff] [blame] | 153 | int initdram(void) |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 154 | { |
Stefan Roese | fd63793 | 2006-03-17 10:28:24 +0100 | [diff] [blame] | 155 | ulong speed; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 156 | ulong sdtr1; |
stroese | 55ca749 | 2004-07-15 14:41:13 +0000 | [diff] [blame] | 157 | int i; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 158 | |
Wolfgang Denk | ebd3deb | 2006-04-16 10:51:58 +0200 | [diff] [blame] | 159 | /* |
| 160 | * Determine SDRAM speed |
| 161 | */ |
| 162 | speed = get_bus_freq(0); /* parameter not used on ppc4xx */ |
Stefan Roese | fd63793 | 2006-03-17 10:28:24 +0100 | [diff] [blame] | 163 | |
Wolfgang Denk | ebd3deb | 2006-04-16 10:51:58 +0200 | [diff] [blame] | 164 | /* |
| 165 | * sdtr1 (register SDRAM0_TR) must take into account timings listed |
| 166 | * in SDRAM chip datasheet. rtr (register SDRAM0_RTR) must take into |
| 167 | * account actual SDRAM size. So we can set up sdtr1 according to what |
| 168 | * is specified in board configuration file while rtr dependds on SDRAM |
| 169 | * size we are assuming before detection. |
| 170 | */ |
| 171 | sdtr1 = compute_sdtr1(speed); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 172 | |
stroese | 55ca749 | 2004-07-15 14:41:13 +0000 | [diff] [blame] | 173 | for (i=0; i<N_MB0CF; i++) { |
stroese | 51c57b9 | 2003-02-10 16:26:37 +0000 | [diff] [blame] | 174 | /* |
stroese | 55ca749 | 2004-07-15 14:41:13 +0000 | [diff] [blame] | 175 | * Disable memory controller. |
stroese | 51c57b9 | 2003-02-10 16:26:37 +0000 | [diff] [blame] | 176 | */ |
Stefan Roese | 6987e65 | 2009-09-24 13:59:57 +0200 | [diff] [blame] | 177 | mtsdram(SDRAM0_CFG, 0x00000000); |
wdenk | 41e2e05 | 2003-02-11 01:49:43 +0000 | [diff] [blame] | 178 | |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 179 | /* |
stroese | 55ca749 | 2004-07-15 14:41:13 +0000 | [diff] [blame] | 180 | * Set MB0CF for bank 0. |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 181 | */ |
Stefan Roese | 6987e65 | 2009-09-24 13:59:57 +0200 | [diff] [blame] | 182 | mtsdram(SDRAM0_B0CR, mb0cf[i].reg); |
| 183 | mtsdram(SDRAM0_TR, sdtr1); |
| 184 | mtsdram(SDRAM0_RTR, compute_rtr(speed, mb0cf[i].rows, 64)); |
wdenk | 41e2e05 | 2003-02-11 01:49:43 +0000 | [diff] [blame] | 185 | |
stroese | 55ca749 | 2004-07-15 14:41:13 +0000 | [diff] [blame] | 186 | udelay(200); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 187 | |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 188 | /* |
stroese | 55ca749 | 2004-07-15 14:41:13 +0000 | [diff] [blame] | 189 | * Set memory controller options reg, MCOPT1. |
| 190 | * Set DC_EN to '1' and BRD_PRF to '01' for 16 byte PLB Burst |
| 191 | * read/prefetch. |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 192 | */ |
Stefan Roese | 6987e65 | 2009-09-24 13:59:57 +0200 | [diff] [blame] | 193 | mtsdram(SDRAM0_CFG, 0x80800000); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 194 | |
stroese | 55ca749 | 2004-07-15 14:41:13 +0000 | [diff] [blame] | 195 | udelay(10000); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 196 | |
stroese | 55ca749 | 2004-07-15 14:41:13 +0000 | [diff] [blame] | 197 | if (get_ram_size(0, mb0cf[i].size) == mb0cf[i].size) { |
Stefan Roese | 58296a1 | 2008-09-10 16:53:47 +0200 | [diff] [blame] | 198 | phys_size_t size = mb0cf[i].size; |
| 199 | |
stroese | 55ca749 | 2004-07-15 14:41:13 +0000 | [diff] [blame] | 200 | /* |
John Otken | 9aa3677 | 2007-07-26 17:49:11 +0200 | [diff] [blame] | 201 | * OK, size detected. Enable second bank if |
| 202 | * defined (assumes same type as bank 0) |
stroese | 55ca749 | 2004-07-15 14:41:13 +0000 | [diff] [blame] | 203 | */ |
John Otken | 9aa3677 | 2007-07-26 17:49:11 +0200 | [diff] [blame] | 204 | #ifdef CONFIG_SDRAM_BANK1 |
Stefan Roese | 6987e65 | 2009-09-24 13:59:57 +0200 | [diff] [blame] | 205 | mtsdram(SDRAM0_CFG, 0x00000000); |
| 206 | mtsdram(SDRAM0_B1CR, mb0cf[i].size | mb0cf[i].reg); |
| 207 | mtsdram(SDRAM0_CFG, 0x80800000); |
John Otken | 9aa3677 | 2007-07-26 17:49:11 +0200 | [diff] [blame] | 208 | udelay(10000); |
Stefan Roese | a5d182e | 2007-08-14 14:44:41 +0200 | [diff] [blame] | 209 | |
| 210 | /* |
| 211 | * Check if 2nd bank is really available. |
| 212 | * If the size not equal to the size of the first |
| 213 | * bank, then disable the 2nd bank completely. |
| 214 | */ |
| 215 | if (get_ram_size((long *)mb0cf[i].size, mb0cf[i].size) != |
| 216 | mb0cf[i].size) { |
Stefan Roese | 6987e65 | 2009-09-24 13:59:57 +0200 | [diff] [blame] | 217 | mtsdram(SDRAM0_B1CR, 0); |
| 218 | mtsdram(SDRAM0_CFG, 0); |
Stefan Roese | 58296a1 | 2008-09-10 16:53:47 +0200 | [diff] [blame] | 219 | } else { |
| 220 | /* |
| 221 | * We have two identical banks, so the size |
| 222 | * is twice the bank size |
| 223 | */ |
| 224 | size = 2 * size; |
Stefan Roese | a5d182e | 2007-08-14 14:44:41 +0200 | [diff] [blame] | 225 | } |
John Otken | 9aa3677 | 2007-07-26 17:49:11 +0200 | [diff] [blame] | 226 | #endif |
Stefan Roese | cdb0470 | 2008-06-02 17:37:28 +0200 | [diff] [blame] | 227 | |
| 228 | /* |
| 229 | * OK, size detected -> all done |
| 230 | */ |
Simon Glass | 39f90ba | 2017-03-31 08:40:25 -0600 | [diff] [blame] | 231 | gd->ram_size = size; |
| 232 | |
| 233 | return 0; |
stroese | 55ca749 | 2004-07-15 14:41:13 +0000 | [diff] [blame] | 234 | } |
stroese | 51c57b9 | 2003-02-10 16:26:37 +0000 | [diff] [blame] | 235 | } |
Stefan Roese | cdb0470 | 2008-06-02 17:37:28 +0200 | [diff] [blame] | 236 | |
Simon Glass | 39f90ba | 2017-03-31 08:40:25 -0600 | [diff] [blame] | 237 | return -ENXIO; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 238 | } |
| 239 | |
Stefan Roese | c443fe9 | 2005-11-22 13:20:42 +0100 | [diff] [blame] | 240 | #else /* CONFIG_440 */ |
| 241 | |
Stefan Roese | 8d98230 | 2007-01-18 10:25:34 +0100 | [diff] [blame] | 242 | /* |
| 243 | * Define some default values. Those can be overwritten in the |
| 244 | * board config file. |
| 245 | */ |
| 246 | |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 247 | #ifndef CONFIG_SYS_SDRAM_TABLE |
Stefan Roese | 8d98230 | 2007-01-18 10:25:34 +0100 | [diff] [blame] | 248 | sdram_conf_t mb0cf[] = { |
| 249 | {(256 << 20), 13, 0x000C4001}, /* 256MB mode 3, 13x10(4) */ |
Dirk Eibach | 09fe652 | 2008-12-09 11:00:07 +0100 | [diff] [blame] | 250 | {(128 << 20), 13, 0x000A4001}, /* 128MB mode 3, 13x10(4) */ |
Stefan Roese | 8d98230 | 2007-01-18 10:25:34 +0100 | [diff] [blame] | 251 | {(64 << 20), 12, 0x00082001} /* 64MB mode 2, 12x9(4) */ |
| 252 | }; |
| 253 | #else |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 254 | sdram_conf_t mb0cf[] = CONFIG_SYS_SDRAM_TABLE; |
Stefan Roese | 8d98230 | 2007-01-18 10:25:34 +0100 | [diff] [blame] | 255 | #endif |
| 256 | |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 257 | #ifndef CONFIG_SYS_SDRAM0_TR0 |
| 258 | #define CONFIG_SYS_SDRAM0_TR0 0x41094012 |
Stefan Roese | 8d98230 | 2007-01-18 10:25:34 +0100 | [diff] [blame] | 259 | #endif |
| 260 | |
Dirk Eibach | 09fe652 | 2008-12-09 11:00:07 +0100 | [diff] [blame] | 261 | #ifndef CONFIG_SYS_SDRAM0_WDDCTR |
| 262 | #define CONFIG_SYS_SDRAM0_WDDCTR 0x00000000 /* wrcp=0 dcd=0 */ |
| 263 | #endif |
| 264 | |
| 265 | #ifndef CONFIG_SYS_SDRAM0_RTR |
| 266 | #define CONFIG_SYS_SDRAM0_RTR 0x04100000 /* 7.8us @ 133MHz PLB */ |
| 267 | #endif |
| 268 | |
| 269 | #ifndef CONFIG_SYS_SDRAM0_CFG0 |
| 270 | #define CONFIG_SYS_SDRAM0_CFG0 0x82000000 /* DCEN=1, PMUD=0, 64-bit */ |
| 271 | #endif |
| 272 | |
Robert P. J. Day | 0c91159 | 2016-05-23 06:49:21 -0400 | [diff] [blame] | 273 | #define N_MB0CF (ARRAY_SIZE(mb0cf)) |
Stefan Roese | 8d98230 | 2007-01-18 10:25:34 +0100 | [diff] [blame] | 274 | |
Stefan Roese | fd63793 | 2006-03-17 10:28:24 +0100 | [diff] [blame] | 275 | #define NUM_TRIES 64 |
| 276 | #define NUM_READS 10 |
| 277 | |
| 278 | static void sdram_tr1_set(int ram_address, int* tr1_value) |
| 279 | { |
| 280 | int i; |
| 281 | int j, k; |
| 282 | volatile unsigned int* ram_pointer = (unsigned int *)ram_address; |
| 283 | int first_good = -1, last_bad = 0x1ff; |
| 284 | |
| 285 | unsigned long test[NUM_TRIES] = { |
| 286 | 0x00000000, 0x00000000, 0xFFFFFFFF, 0xFFFFFFFF, |
| 287 | 0x00000000, 0x00000000, 0xFFFFFFFF, 0xFFFFFFFF, |
| 288 | 0xFFFFFFFF, 0xFFFFFFFF, 0x00000000, 0x00000000, |
| 289 | 0xFFFFFFFF, 0xFFFFFFFF, 0x00000000, 0x00000000, |
| 290 | 0xAAAAAAAA, 0xAAAAAAAA, 0x55555555, 0x55555555, |
| 291 | 0xAAAAAAAA, 0xAAAAAAAA, 0x55555555, 0x55555555, |
| 292 | 0x55555555, 0x55555555, 0xAAAAAAAA, 0xAAAAAAAA, |
| 293 | 0x55555555, 0x55555555, 0xAAAAAAAA, 0xAAAAAAAA, |
| 294 | 0xA5A5A5A5, 0xA5A5A5A5, 0x5A5A5A5A, 0x5A5A5A5A, |
| 295 | 0xA5A5A5A5, 0xA5A5A5A5, 0x5A5A5A5A, 0x5A5A5A5A, |
| 296 | 0x5A5A5A5A, 0x5A5A5A5A, 0xA5A5A5A5, 0xA5A5A5A5, |
| 297 | 0x5A5A5A5A, 0x5A5A5A5A, 0xA5A5A5A5, 0xA5A5A5A5, |
| 298 | 0xAA55AA55, 0xAA55AA55, 0x55AA55AA, 0x55AA55AA, |
| 299 | 0xAA55AA55, 0xAA55AA55, 0x55AA55AA, 0x55AA55AA, |
| 300 | 0x55AA55AA, 0x55AA55AA, 0xAA55AA55, 0xAA55AA55, |
| 301 | 0x55AA55AA, 0x55AA55AA, 0xAA55AA55, 0xAA55AA55 }; |
| 302 | |
| 303 | /* go through all possible SDRAM0_TR1[RDCT] values */ |
| 304 | for (i=0; i<=0x1ff; i++) { |
| 305 | /* set the current value for TR1 */ |
Stefan Roese | 6987e65 | 2009-09-24 13:59:57 +0200 | [diff] [blame] | 306 | mtsdram(SDRAM0_TR1, (0x80800800 | i)); |
Stefan Roese | fd63793 | 2006-03-17 10:28:24 +0100 | [diff] [blame] | 307 | |
| 308 | /* write values */ |
| 309 | for (j=0; j<NUM_TRIES; j++) { |
| 310 | ram_pointer[j] = test[j]; |
| 311 | |
| 312 | /* clear any cache at ram location */ |
| 313 | __asm__("dcbf 0,%0": :"r" (&ram_pointer[j])); |
| 314 | } |
| 315 | |
| 316 | /* read values back */ |
| 317 | for (j=0; j<NUM_TRIES; j++) { |
| 318 | for (k=0; k<NUM_READS; k++) { |
| 319 | /* clear any cache at ram location */ |
| 320 | __asm__("dcbf 0,%0": :"r" (&ram_pointer[j])); |
| 321 | |
| 322 | if (ram_pointer[j] != test[j]) |
| 323 | break; |
| 324 | } |
| 325 | |
| 326 | /* read error */ |
| 327 | if (k != NUM_READS) |
| 328 | break; |
| 329 | } |
| 330 | |
| 331 | /* we have a SDRAM0_TR1[RDCT] that is part of the window */ |
| 332 | if (j == NUM_TRIES) { |
| 333 | if (first_good == -1) |
| 334 | first_good = i; /* found beginning of window */ |
| 335 | } else { /* bad read */ |
| 336 | /* if we have not had a good read then don't care */ |
| 337 | if (first_good != -1) { |
| 338 | /* first failure after a good read */ |
| 339 | last_bad = i-1; |
| 340 | break; |
| 341 | } |
| 342 | } |
| 343 | } |
| 344 | |
| 345 | /* return the current value for TR1 */ |
| 346 | *tr1_value = (first_good + last_bad) / 2; |
| 347 | } |
| 348 | |
Stefan Roese | c443fe9 | 2005-11-22 13:20:42 +0100 | [diff] [blame] | 349 | /* |
| 350 | * Autodetect onboard DDR SDRAM on 440 platforms |
| 351 | * |
| 352 | * NOTE: Some of the hardcoded values are hardware dependant, |
Wolfgang Denk | ebd3deb | 2006-04-16 10:51:58 +0200 | [diff] [blame] | 353 | * so this should be extended for other future boards |
| 354 | * using this routine! |
Stefan Roese | c443fe9 | 2005-11-22 13:20:42 +0100 | [diff] [blame] | 355 | */ |
Simon Glass | 39f90ba | 2017-03-31 08:40:25 -0600 | [diff] [blame] | 356 | int initdram(void) |
Stefan Roese | c443fe9 | 2005-11-22 13:20:42 +0100 | [diff] [blame] | 357 | { |
| 358 | int i; |
Stefan Roese | fd63793 | 2006-03-17 10:28:24 +0100 | [diff] [blame] | 359 | int tr1_bank1; |
Stefan Roese | c443fe9 | 2005-11-22 13:20:42 +0100 | [diff] [blame] | 360 | |
Stefan Roese | 8d98230 | 2007-01-18 10:25:34 +0100 | [diff] [blame] | 361 | #if defined(CONFIG_440GX) || defined(CONFIG_440EP) || \ |
| 362 | defined(CONFIG_440GR) || defined(CONFIG_440SP) |
Stefan Roese | 4963968 | 2006-08-15 14:22:35 +0200 | [diff] [blame] | 363 | /* |
| 364 | * Soft-reset SDRAM controller. |
| 365 | */ |
Stefan Roese | 918010a | 2009-09-09 16:25:29 +0200 | [diff] [blame] | 366 | mtsdr(SDR0_SRST, SDR0_SRST_DMC); |
| 367 | mtsdr(SDR0_SRST, 0x00000000); |
Stefan Roese | 4963968 | 2006-08-15 14:22:35 +0200 | [diff] [blame] | 368 | #endif |
| 369 | |
Stefan Roese | c443fe9 | 2005-11-22 13:20:42 +0100 | [diff] [blame] | 370 | for (i=0; i<N_MB0CF; i++) { |
| 371 | /* |
| 372 | * Disable memory controller. |
| 373 | */ |
Stefan Roese | 6987e65 | 2009-09-24 13:59:57 +0200 | [diff] [blame] | 374 | mtsdram(SDRAM0_CFG0, 0x00000000); |
Stefan Roese | c443fe9 | 2005-11-22 13:20:42 +0100 | [diff] [blame] | 375 | |
| 376 | /* |
| 377 | * Setup some default |
| 378 | */ |
Stefan Roese | 6987e65 | 2009-09-24 13:59:57 +0200 | [diff] [blame] | 379 | mtsdram(SDRAM0_UABBA, 0x00000000); /* ubba=0 (default) */ |
| 380 | mtsdram(SDRAM0_SLIO, 0x00000000); /* rdre=0 wrre=0 rarw=0 */ |
| 381 | mtsdram(SDRAM0_DEVOPT, 0x00000000); /* dll=0 ds=0 (normal) */ |
| 382 | mtsdram(SDRAM0_WDDCTR, CONFIG_SYS_SDRAM0_WDDCTR); |
| 383 | mtsdram(SDRAM0_CLKTR, 0x40000000); /* clkp=1 (90 deg wr) dcdt=0 */ |
Stefan Roese | c443fe9 | 2005-11-22 13:20:42 +0100 | [diff] [blame] | 384 | |
| 385 | /* |
| 386 | * Following for CAS Latency = 2.5 @ 133 MHz PLB |
| 387 | */ |
Stefan Roese | 6987e65 | 2009-09-24 13:59:57 +0200 | [diff] [blame] | 388 | mtsdram(SDRAM0_B0CR, mb0cf[i].reg); |
| 389 | mtsdram(SDRAM0_TR0, CONFIG_SYS_SDRAM0_TR0); |
| 390 | mtsdram(SDRAM0_TR1, 0x80800800); /* SS=T2 SL=STAGE 3 CD=1 CT=0x00*/ |
| 391 | mtsdram(SDRAM0_RTR, CONFIG_SYS_SDRAM0_RTR); |
| 392 | mtsdram(SDRAM0_CFG1, 0x00000000); /* Self-refresh exit, disable PM*/ |
Stefan Roese | c443fe9 | 2005-11-22 13:20:42 +0100 | [diff] [blame] | 393 | udelay(400); /* Delay 200 usecs (min) */ |
| 394 | |
| 395 | /* |
| 396 | * Enable the controller, then wait for DCEN to complete |
| 397 | */ |
Stefan Roese | 6987e65 | 2009-09-24 13:59:57 +0200 | [diff] [blame] | 398 | mtsdram(SDRAM0_CFG0, CONFIG_SYS_SDRAM0_CFG0); |
Stefan Roese | c443fe9 | 2005-11-22 13:20:42 +0100 | [diff] [blame] | 399 | udelay(10000); |
| 400 | |
| 401 | if (get_ram_size(0, mb0cf[i].size) == mb0cf[i].size) { |
Dirk Eibach | 09fe652 | 2008-12-09 11:00:07 +0100 | [diff] [blame] | 402 | phys_size_t size = mb0cf[i].size; |
Stefan Roese | c443fe9 | 2005-11-22 13:20:42 +0100 | [diff] [blame] | 403 | /* |
Stefan Roese | fd63793 | 2006-03-17 10:28:24 +0100 | [diff] [blame] | 404 | * Optimize TR1 to current hardware environment |
| 405 | */ |
| 406 | sdram_tr1_set(0x00000000, &tr1_bank1); |
Stefan Roese | 6987e65 | 2009-09-24 13:59:57 +0200 | [diff] [blame] | 407 | mtsdram(SDRAM0_TR1, (tr1_bank1 | 0x80800800)); |
Stefan Roese | fd63793 | 2006-03-17 10:28:24 +0100 | [diff] [blame] | 408 | |
Dirk Eibach | 09fe652 | 2008-12-09 11:00:07 +0100 | [diff] [blame] | 409 | |
| 410 | /* |
| 411 | * OK, size detected. Enable second bank if |
| 412 | * defined (assumes same type as bank 0) |
| 413 | */ |
| 414 | #ifdef CONFIG_SDRAM_BANK1 |
Stefan Roese | 6987e65 | 2009-09-24 13:59:57 +0200 | [diff] [blame] | 415 | mtsdram(SDRAM0_CFG0, 0); |
| 416 | mtsdram(SDRAM0_B1CR, mb0cf[i].size | mb0cf[i].reg); |
| 417 | mtsdram(SDRAM0_CFG0, CONFIG_SYS_SDRAM0_CFG0); |
Dirk Eibach | 09fe652 | 2008-12-09 11:00:07 +0100 | [diff] [blame] | 418 | udelay(10000); |
| 419 | |
| 420 | /* |
| 421 | * Check if 2nd bank is really available. |
| 422 | * If the size not equal to the size of the first |
| 423 | * bank, then disable the 2nd bank completely. |
| 424 | */ |
| 425 | if (get_ram_size((long *)mb0cf[i].size, mb0cf[i].size) |
| 426 | != mb0cf[i].size) { |
Stefan Roese | 6987e65 | 2009-09-24 13:59:57 +0200 | [diff] [blame] | 427 | mtsdram(SDRAM0_CFG0, 0); |
| 428 | mtsdram(SDRAM0_B1CR, 0); |
| 429 | mtsdram(SDRAM0_CFG0, CONFIG_SYS_SDRAM0_CFG0); |
Dirk Eibach | 09fe652 | 2008-12-09 11:00:07 +0100 | [diff] [blame] | 430 | udelay(10000); |
| 431 | } else { |
| 432 | /* |
| 433 | * We have two identical banks, so the size |
| 434 | * is twice the bank size |
| 435 | */ |
| 436 | size = 2 * size; |
| 437 | } |
| 438 | #endif |
| 439 | |
Stefan Roese | fd63793 | 2006-03-17 10:28:24 +0100 | [diff] [blame] | 440 | #ifdef CONFIG_SDRAM_ECC |
Dirk Eibach | 09fe652 | 2008-12-09 11:00:07 +0100 | [diff] [blame] | 441 | ecc_init(0, size); |
Stefan Roese | fd63793 | 2006-03-17 10:28:24 +0100 | [diff] [blame] | 442 | #endif |
| 443 | |
| 444 | /* |
Stefan Roese | c443fe9 | 2005-11-22 13:20:42 +0100 | [diff] [blame] | 445 | * OK, size detected -> all done |
| 446 | */ |
Simon Glass | 39f90ba | 2017-03-31 08:40:25 -0600 | [diff] [blame] | 447 | gd->ram_size = size; |
| 448 | |
| 449 | return 0; |
Stefan Roese | c443fe9 | 2005-11-22 13:20:42 +0100 | [diff] [blame] | 450 | } |
| 451 | } |
| 452 | |
Simon Glass | 39f90ba | 2017-03-31 08:40:25 -0600 | [diff] [blame] | 453 | return -ENXIO; /* nothing found ! */ |
Stefan Roese | c443fe9 | 2005-11-22 13:20:42 +0100 | [diff] [blame] | 454 | } |
| 455 | |
| 456 | #endif /* CONFIG_440 */ |
| 457 | |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 458 | #endif /* CONFIG_SDRAM_BANK0 */ |