wdenk | 945af8d | 2003-07-16 21:53:01 +0000 | [diff] [blame] | 1 | /* |
| 2 | * (C) Copyright 2003 |
| 3 | * Wolfgang Denk, DENX Software Engineering, wd@denx.de. |
| 4 | * |
| 5 | * See file CREDITS for list of people who contributed to this |
| 6 | * project. |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or |
| 9 | * modify it under the terms of the GNU General Public License as |
| 10 | * published by the Free Software Foundation; either version 2 of |
| 11 | * the License, or (at your option) any later version. |
| 12 | * |
| 13 | * This program is distributed in the hope that it will be useful, |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | * GNU General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU General Public License |
| 19 | * along with this program; if not, write to the Free Software |
| 20 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, |
| 21 | * MA 02111-1307 USA |
| 22 | */ |
| 23 | |
| 24 | #include <common.h> |
| 25 | #include <mpc5xxx.h> |
wdenk | 96e48cf | 2003-08-05 18:22:44 +0000 | [diff] [blame] | 26 | #include <pci.h> |
wdenk | 945af8d | 2003-07-16 21:53:01 +0000 | [diff] [blame] | 27 | |
wdenk | d94f92c | 2003-08-28 09:41:22 +0000 | [diff] [blame] | 28 | #ifndef CFG_RAMBOOT |
wdenk | e0ac62d | 2003-08-17 18:55:18 +0000 | [diff] [blame] | 29 | static long int dram_size(long int *base, long int maxsize) |
| 30 | { |
| 31 | volatile long int *addr; |
| 32 | ulong cnt, val; |
| 33 | ulong save[32]; /* to make test non-destructive */ |
| 34 | unsigned char i = 0; |
| 35 | |
| 36 | for (cnt = (maxsize / sizeof (long)) >> 1; cnt > 0; cnt >>= 1) { |
| 37 | addr = base + cnt; /* pointer arith! */ |
| 38 | |
| 39 | save[i++] = *addr; |
| 40 | *addr = ~cnt; |
| 41 | } |
| 42 | |
| 43 | /* write 0 to base address */ |
| 44 | addr = base; |
| 45 | save[i] = *addr; |
| 46 | *addr = 0; |
| 47 | |
| 48 | /* check at base address */ |
| 49 | if ((val = *addr) != 0) { |
| 50 | *addr = save[i]; |
| 51 | return (0); |
| 52 | } |
| 53 | |
| 54 | for (cnt = 1; cnt < maxsize / sizeof (long); cnt <<= 1) { |
| 55 | addr = base + cnt; /* pointer arith! */ |
| 56 | |
| 57 | val = *addr; |
| 58 | *addr = save[--i]; |
| 59 | |
| 60 | if (val != (~cnt)) { |
| 61 | return (cnt * sizeof (long)); |
| 62 | } |
| 63 | } |
| 64 | return (maxsize); |
| 65 | } |
| 66 | |
| 67 | static void sdram_start (int hi_addr) |
| 68 | { |
| 69 | long hi_addr_bit = hi_addr ? 0x01000000 : 0; |
| 70 | |
wdenk | b2001f2 | 2003-12-20 22:45:10 +0000 | [diff] [blame] | 71 | #ifdef CONFIG_MPC5200_DDR |
| 72 | /* unlock mode register */ |
| 73 | *(vu_long *)MPC5XXX_SDRAM_CTRL = 0xf05f0f00 | hi_addr_bit; |
| 74 | /* precharge all banks */ |
| 75 | *(vu_long *)MPC5XXX_SDRAM_CTRL = 0xf05f0f02 | hi_addr_bit; |
| 76 | /* set mode register: extended mode */ |
| 77 | *(vu_long *)MPC5XXX_SDRAM_MODE = 0x40090000; |
| 78 | /* set mode register: reset DLL */ |
| 79 | *(vu_long *)MPC5XXX_SDRAM_MODE = 0x058d0000; |
| 80 | /* precharge all banks */ |
| 81 | *(vu_long *)MPC5XXX_SDRAM_CTRL = 0xf05f0f02 | hi_addr_bit; |
| 82 | /* auto refresh */ |
| 83 | *(vu_long *)MPC5XXX_SDRAM_CTRL = 0xf05f0f04 | hi_addr_bit; |
| 84 | /* set mode register */ |
| 85 | *(vu_long *)MPC5XXX_SDRAM_MODE = 0x018d0000; |
| 86 | /* normal operation */ |
| 87 | *(vu_long *)MPC5XXX_SDRAM_CTRL = 0x705f0f00 | hi_addr_bit; |
| 88 | #else |
wdenk | e0ac62d | 2003-08-17 18:55:18 +0000 | [diff] [blame] | 89 | /* unlock mode register */ |
| 90 | *(vu_long *)MPC5XXX_SDRAM_CTRL = 0xd04f0000 | hi_addr_bit; |
| 91 | /* precharge all banks */ |
| 92 | *(vu_long *)MPC5XXX_SDRAM_CTRL = 0xd04f0002 | hi_addr_bit; |
| 93 | /* set mode register */ |
| 94 | #if defined(CONFIG_MPC5200) |
| 95 | *(vu_long *)MPC5XXX_SDRAM_MODE = 0x408d0000; |
| 96 | #elif defined(CONFIG_MGT5100) |
| 97 | *(vu_long *)MPC5XXX_SDRAM_MODE = 0x008d0000; |
| 98 | #endif |
| 99 | /* precharge all banks */ |
| 100 | *(vu_long *)MPC5XXX_SDRAM_CTRL = 0xd04f0002 | hi_addr_bit; |
| 101 | /* auto refresh */ |
| 102 | *(vu_long *)MPC5XXX_SDRAM_CTRL = 0xd04f0004 | hi_addr_bit; |
| 103 | /* set mode register */ |
| 104 | *(vu_long *)MPC5XXX_SDRAM_MODE = 0x008d0000; |
| 105 | /* normal operation */ |
| 106 | *(vu_long *)MPC5XXX_SDRAM_CTRL = 0x504f0000 | hi_addr_bit; |
wdenk | b2001f2 | 2003-12-20 22:45:10 +0000 | [diff] [blame] | 107 | #endif |
wdenk | e0ac62d | 2003-08-17 18:55:18 +0000 | [diff] [blame] | 108 | } |
wdenk | d94f92c | 2003-08-28 09:41:22 +0000 | [diff] [blame] | 109 | #endif |
wdenk | e0ac62d | 2003-08-17 18:55:18 +0000 | [diff] [blame] | 110 | |
wdenk | 945af8d | 2003-07-16 21:53:01 +0000 | [diff] [blame] | 111 | long int initdram (int board_type) |
| 112 | { |
wdenk | d94f92c | 2003-08-28 09:41:22 +0000 | [diff] [blame] | 113 | ulong dramsize = 0; |
wdenk | b2001f2 | 2003-12-20 22:45:10 +0000 | [diff] [blame] | 114 | #ifdef CONFIG_MPC5200_DDR |
| 115 | ulong dramsize2 = 0; |
| 116 | #endif |
wdenk | 945af8d | 2003-07-16 21:53:01 +0000 | [diff] [blame] | 117 | #ifndef CFG_RAMBOOT |
wdenk | d94f92c | 2003-08-28 09:41:22 +0000 | [diff] [blame] | 118 | ulong test1, test2; |
| 119 | |
wdenk | 945af8d | 2003-07-16 21:53:01 +0000 | [diff] [blame] | 120 | /* configure SDRAM start/end */ |
| 121 | #if defined(CONFIG_MPC5200) |
wdenk | e0ac62d | 2003-08-17 18:55:18 +0000 | [diff] [blame] | 122 | *(vu_long *)MPC5XXX_SDRAM_CS0CFG = 0x0000001e;/* 2G at 0x0 */ |
| 123 | *(vu_long *)MPC5XXX_SDRAM_CS1CFG = 0x80000000;/* disabled */ |
wdenk | 945af8d | 2003-07-16 21:53:01 +0000 | [diff] [blame] | 124 | |
wdenk | b2001f2 | 2003-12-20 22:45:10 +0000 | [diff] [blame] | 125 | #ifdef CONFIG_MPC5200_DDR |
| 126 | /* setup config registers */ |
| 127 | *(vu_long *)MPC5XXX_SDRAM_CONFIG1 = 0x73722930; |
| 128 | *(vu_long *)MPC5XXX_SDRAM_CONFIG2 = 0x47770000; |
wdenk | d4ca31c | 2004-01-02 14:00:00 +0000 | [diff] [blame] | 129 | |
wdenk | b2001f2 | 2003-12-20 22:45:10 +0000 | [diff] [blame] | 130 | /* set tap delay to 0x10 */ |
| 131 | *(vu_long *)MPC5XXX_CDM_PORCFG = 0x10000000; |
| 132 | #else |
wdenk | 945af8d | 2003-07-16 21:53:01 +0000 | [diff] [blame] | 133 | /* setup config registers */ |
| 134 | *(vu_long *)MPC5XXX_SDRAM_CONFIG1 = 0xc2233a00; |
| 135 | *(vu_long *)MPC5XXX_SDRAM_CONFIG2 = 0x88b70004; |
wdenk | b2001f2 | 2003-12-20 22:45:10 +0000 | [diff] [blame] | 136 | #endif |
wdenk | 945af8d | 2003-07-16 21:53:01 +0000 | [diff] [blame] | 137 | |
wdenk | 945af8d | 2003-07-16 21:53:01 +0000 | [diff] [blame] | 138 | #elif defined(CONFIG_MGT5100) |
| 139 | *(vu_long *)MPC5XXX_SDRAM_START = 0x00000000; |
wdenk | e0ac62d | 2003-08-17 18:55:18 +0000 | [diff] [blame] | 140 | *(vu_long *)MPC5XXX_SDRAM_STOP = 0x0000ffff;/* 2G */ |
wdenk | 945af8d | 2003-07-16 21:53:01 +0000 | [diff] [blame] | 141 | *(vu_long *)MPC5XXX_ADDECR |= (1 << 22); /* Enable SDRAM */ |
| 142 | |
| 143 | /* setup config registers */ |
| 144 | *(vu_long *)MPC5XXX_SDRAM_CONFIG1 = 0xc2222600; |
| 145 | *(vu_long *)MPC5XXX_SDRAM_CONFIG2 = 0x88b70004; |
| 146 | |
| 147 | /* address select register */ |
| 148 | *(vu_long *)MPC5XXX_SDRAM_XLBSEL = 0x03000000; |
wdenk | 945af8d | 2003-07-16 21:53:01 +0000 | [diff] [blame] | 149 | #endif |
wdenk | e0ac62d | 2003-08-17 18:55:18 +0000 | [diff] [blame] | 150 | sdram_start(0); |
| 151 | test1 = dram_size((ulong *)CFG_SDRAM_BASE, 0x80000000); |
| 152 | sdram_start(1); |
| 153 | test2 = dram_size((ulong *)CFG_SDRAM_BASE, 0x80000000); |
| 154 | if (test1 > test2) { |
| 155 | sdram_start(0); |
| 156 | dramsize = test1; |
| 157 | } else { |
| 158 | dramsize = test2; |
| 159 | } |
| 160 | #if defined(CONFIG_MPC5200) |
| 161 | *(vu_long *)MPC5XXX_SDRAM_CS0CFG = |
| 162 | (0x13 + __builtin_ffs(dramsize >> 20) - 1); |
wdenk | b2001f2 | 2003-12-20 22:45:10 +0000 | [diff] [blame] | 163 | #ifdef CONFIG_MPC5200_DDR |
| 164 | *(vu_long *)MPC5XXX_SDRAM_CS1CFG = dramsize + 0x0000001e;/* 2G */ |
| 165 | sdram_start(0); |
| 166 | test1 = dram_size((ulong *)(CFG_SDRAM_BASE + dramsize), 0x80000000); |
| 167 | sdram_start(1); |
| 168 | test2 = dram_size((ulong *)(CFG_SDRAM_BASE + dramsize), 0x80000000); |
| 169 | if (test1 > test2) { |
| 170 | sdram_start(0); |
| 171 | dramsize2 = test1; |
| 172 | } else { |
| 173 | dramsize2 = test2; |
| 174 | } |
| 175 | *(vu_long *)MPC5XXX_SDRAM_CS1CFG = |
| 176 | dramsize + (0x13 + __builtin_ffs(dramsize2 >> 20) - 1); |
| 177 | #else |
wdenk | e0ac62d | 2003-08-17 18:55:18 +0000 | [diff] [blame] | 178 | *(vu_long *)MPC5XXX_SDRAM_CS1CFG = dramsize; /* disabled */ |
wdenk | b2001f2 | 2003-12-20 22:45:10 +0000 | [diff] [blame] | 179 | #endif |
wdenk | e0ac62d | 2003-08-17 18:55:18 +0000 | [diff] [blame] | 180 | #elif defined(CONFIG_MGT5100) |
| 181 | *(vu_long *)MPC5XXX_SDRAM_STOP = ((dramsize - 1) >> 15); |
| 182 | #endif |
| 183 | |
wdenk | 5cf9da4 | 2003-11-07 13:42:26 +0000 | [diff] [blame] | 184 | #else /* CFG_RAMBOOT */ |
wdenk | 945af8d | 2003-07-16 21:53:01 +0000 | [diff] [blame] | 185 | #ifdef CONFIG_MGT5100 |
| 186 | *(vu_long *)MPC5XXX_ADDECR |= (1 << 22); /* Enable SDRAM */ |
wdenk | d94f92c | 2003-08-28 09:41:22 +0000 | [diff] [blame] | 187 | dramsize = ((*(vu_long *)MPC5XXX_SDRAM_STOP + 1) << 15); |
| 188 | #else |
| 189 | dramsize = ((1 << (*(vu_long *)MPC5XXX_SDRAM_CS0CFG - 0x13)) << 20); |
wdenk | b2001f2 | 2003-12-20 22:45:10 +0000 | [diff] [blame] | 190 | #ifdef CONFIG_MPC5200_DDR |
| 191 | dramsize2 = ((1 << (*(vu_long *)MPC5XXX_SDRAM_CS1CFG - 0x13)) << 20); |
| 192 | #endif |
wdenk | 945af8d | 2003-07-16 21:53:01 +0000 | [diff] [blame] | 193 | #endif |
wdenk | d94f92c | 2003-08-28 09:41:22 +0000 | [diff] [blame] | 194 | #endif /* CFG_RAMBOOT */ |
wdenk | b2001f2 | 2003-12-20 22:45:10 +0000 | [diff] [blame] | 195 | |
| 196 | #ifdef CONFIG_MPC5200_DDR |
| 197 | dramsize += dramsize2; |
| 198 | #endif |
wdenk | 945af8d | 2003-07-16 21:53:01 +0000 | [diff] [blame] | 199 | /* return total ram size */ |
wdenk | e0ac62d | 2003-08-17 18:55:18 +0000 | [diff] [blame] | 200 | return dramsize; |
wdenk | 945af8d | 2003-07-16 21:53:01 +0000 | [diff] [blame] | 201 | } |
| 202 | |
| 203 | int checkboard (void) |
| 204 | { |
| 205 | #if defined(CONFIG_MPC5200) |
| 206 | puts ("Board: Motorola MPC5200 (IceCube)\n"); |
| 207 | #elif defined(CONFIG_MGT5100) |
| 208 | puts ("Board: Motorola MGT5100 (IceCube)\n"); |
| 209 | #endif |
| 210 | return 0; |
| 211 | } |
| 212 | |
| 213 | void flash_preinit(void) |
| 214 | { |
| 215 | /* |
| 216 | * Now, when we are in RAM, enable flash write |
| 217 | * access for detection process. |
| 218 | * Note that CS_BOOT cannot be cleared when |
| 219 | * executing in flash. |
| 220 | */ |
| 221 | #if defined(CONFIG_MGT5100) |
| 222 | *(vu_long *)MPC5XXX_ADDECR &= ~(1 << 25); /* disable CS_BOOT */ |
| 223 | *(vu_long *)MPC5XXX_ADDECR |= (1 << 16); /* enable CS0 */ |
| 224 | #endif |
| 225 | *(vu_long *)MPC5XXX_BOOTCS_CFG &= ~0x1; /* clear RO */ |
| 226 | } |
wdenk | 96e48cf | 2003-08-05 18:22:44 +0000 | [diff] [blame] | 227 | |
wdenk | 7152b1d | 2003-09-05 23:19:14 +0000 | [diff] [blame] | 228 | void flash_afterinit(ulong size) |
| 229 | { |
| 230 | if (size == 0x800000) { /* adjust mapping */ |
wdenk | 42d1f03 | 2003-10-15 23:53:47 +0000 | [diff] [blame] | 231 | *(vu_long *)MPC5XXX_BOOTCS_START = *(vu_long *)MPC5XXX_CS0_START = |
wdenk | 7152b1d | 2003-09-05 23:19:14 +0000 | [diff] [blame] | 232 | START_REG(CFG_BOOTCS_START | size); |
wdenk | 42d1f03 | 2003-10-15 23:53:47 +0000 | [diff] [blame] | 233 | *(vu_long *)MPC5XXX_BOOTCS_STOP = *(vu_long *)MPC5XXX_CS0_STOP = |
wdenk | 7152b1d | 2003-09-05 23:19:14 +0000 | [diff] [blame] | 234 | STOP_REG(CFG_BOOTCS_START | size, size); |
| 235 | } |
| 236 | } |
| 237 | |
wdenk | 96e48cf | 2003-08-05 18:22:44 +0000 | [diff] [blame] | 238 | #ifdef CONFIG_PCI |
| 239 | static struct pci_controller hose; |
| 240 | |
| 241 | extern void pci_mpc5xxx_init(struct pci_controller *); |
| 242 | |
| 243 | void pci_init_board(void) |
| 244 | { |
| 245 | pci_mpc5xxx_init(&hose); |
| 246 | } |
| 247 | #endif |