wdenk | db01a2e | 2004-04-15 23:14:49 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2004 Picture Elements, Inc. |
| 3 | * Stephen Williams (steve@icarus.com) |
| 4 | * |
Wolfgang Denk | 1a45966 | 2013-07-08 09:37:19 +0200 | [diff] [blame^] | 5 | * SPDX-License-Identifier: GPL-2.0+ |
wdenk | db01a2e | 2004-04-15 23:14:49 +0000 | [diff] [blame] | 6 | */ |
| 7 | |
| 8 | #include <common.h> |
Stefan Roese | b36df56 | 2010-09-09 19:18:00 +0200 | [diff] [blame] | 9 | #include <asm/ppc4xx.h> |
wdenk | db01a2e | 2004-04-15 23:14:49 +0000 | [diff] [blame] | 10 | #include <asm/processor.h> |
| 11 | |
| 12 | # define SDRAM_LEN 0x08000000 |
| 13 | |
| 14 | /* |
| 15 | * this is even after checkboard. It returns the size of the SDRAM |
| 16 | * that we have installed. This function is called by board_init_f |
Stefan Roese | a47a12b | 2010-04-15 16:07:28 +0200 | [diff] [blame] | 17 | * in arch/powerpc/lib/board.c to initialize the memory and return what I |
wdenk | db01a2e | 2004-04-15 23:14:49 +0000 | [diff] [blame] | 18 | * found. |
| 19 | */ |
Becky Bruce | 9973e3c | 2008-06-09 16:03:40 -0500 | [diff] [blame] | 20 | phys_size_t initdram (int board_type) |
wdenk | db01a2e | 2004-04-15 23:14:49 +0000 | [diff] [blame] | 21 | { |
| 22 | /* Configure the SDRAMS */ |
| 23 | |
| 24 | /* disable memory controller */ |
Stefan Roese | 95b602b | 2009-09-24 13:59:57 +0200 | [diff] [blame] | 25 | mtdcr (SDRAM0_CFGADDR, SDRAM0_CFG); |
Stefan Roese | d1c3b27 | 2009-09-09 16:25:29 +0200 | [diff] [blame] | 26 | mtdcr (SDRAM0_CFGDATA, 0x00000000); |
wdenk | db01a2e | 2004-04-15 23:14:49 +0000 | [diff] [blame] | 27 | |
| 28 | udelay (500); |
| 29 | |
| 30 | /* Clear SDRAM0_BESR0 (Bus Error Syndrome Register) */ |
Stefan Roese | 95b602b | 2009-09-24 13:59:57 +0200 | [diff] [blame] | 31 | mtdcr (SDRAM0_CFGADDR, SDRAM0_BESR0); |
Stefan Roese | d1c3b27 | 2009-09-09 16:25:29 +0200 | [diff] [blame] | 32 | mtdcr (SDRAM0_CFGDATA, 0xffffffff); |
wdenk | db01a2e | 2004-04-15 23:14:49 +0000 | [diff] [blame] | 33 | |
| 34 | /* Clear SDRAM0_BESR1 (Bus Error Syndrome Register) */ |
Stefan Roese | 95b602b | 2009-09-24 13:59:57 +0200 | [diff] [blame] | 35 | mtdcr (SDRAM0_CFGADDR, SDRAM0_BESR1); |
Stefan Roese | d1c3b27 | 2009-09-09 16:25:29 +0200 | [diff] [blame] | 36 | mtdcr (SDRAM0_CFGDATA, 0xffffffff); |
wdenk | db01a2e | 2004-04-15 23:14:49 +0000 | [diff] [blame] | 37 | |
| 38 | /* Clear SDRAM0_ECCCFG (disable ECC) */ |
Stefan Roese | 95b602b | 2009-09-24 13:59:57 +0200 | [diff] [blame] | 39 | mtdcr (SDRAM0_CFGADDR, SDRAM0_ECCCFG); |
Stefan Roese | d1c3b27 | 2009-09-09 16:25:29 +0200 | [diff] [blame] | 40 | mtdcr (SDRAM0_CFGDATA, 0x00000000); |
wdenk | db01a2e | 2004-04-15 23:14:49 +0000 | [diff] [blame] | 41 | |
| 42 | /* Clear SDRAM0_ECCESR (ECC Error Syndrome Register) */ |
Stefan Roese | 95b602b | 2009-09-24 13:59:57 +0200 | [diff] [blame] | 43 | mtdcr (SDRAM0_CFGADDR, SDRAM0_ECCESR); |
Stefan Roese | d1c3b27 | 2009-09-09 16:25:29 +0200 | [diff] [blame] | 44 | mtdcr (SDRAM0_CFGDATA, 0xffffffff); |
wdenk | db01a2e | 2004-04-15 23:14:49 +0000 | [diff] [blame] | 45 | |
| 46 | /* Timing register: CASL=2, PTA=2, CTP=2, LDF=1, RFTA=5, RCD=2 */ |
Stefan Roese | 95b602b | 2009-09-24 13:59:57 +0200 | [diff] [blame] | 47 | mtdcr (SDRAM0_CFGADDR, SDRAM0_TR); |
Stefan Roese | d1c3b27 | 2009-09-09 16:25:29 +0200 | [diff] [blame] | 48 | mtdcr (SDRAM0_CFGDATA, 0x010a4016); |
wdenk | db01a2e | 2004-04-15 23:14:49 +0000 | [diff] [blame] | 49 | |
| 50 | /* Memory Bank 0 Config == BA=0x00000000, SZ=64M, AM=3, BE=1 */ |
Stefan Roese | 95b602b | 2009-09-24 13:59:57 +0200 | [diff] [blame] | 51 | mtdcr (SDRAM0_CFGADDR, SDRAM0_B0CR); |
Stefan Roese | d1c3b27 | 2009-09-09 16:25:29 +0200 | [diff] [blame] | 52 | mtdcr (SDRAM0_CFGDATA, 0x00084001); |
wdenk | db01a2e | 2004-04-15 23:14:49 +0000 | [diff] [blame] | 53 | |
| 54 | /* Memory Bank 1 Config == BA=0x04000000, SZ=64M, AM=3, BE=1 */ |
Stefan Roese | 95b602b | 2009-09-24 13:59:57 +0200 | [diff] [blame] | 55 | mtdcr (SDRAM0_CFGADDR, SDRAM0_B1CR); |
Stefan Roese | d1c3b27 | 2009-09-09 16:25:29 +0200 | [diff] [blame] | 56 | mtdcr (SDRAM0_CFGDATA, 0x04084001); |
wdenk | db01a2e | 2004-04-15 23:14:49 +0000 | [diff] [blame] | 57 | |
| 58 | /* Memory Bank 2 Config == BE=0 */ |
Stefan Roese | 95b602b | 2009-09-24 13:59:57 +0200 | [diff] [blame] | 59 | mtdcr (SDRAM0_CFGADDR, SDRAM0_B2CR); |
Stefan Roese | d1c3b27 | 2009-09-09 16:25:29 +0200 | [diff] [blame] | 60 | mtdcr (SDRAM0_CFGDATA, 0x00000000); |
wdenk | db01a2e | 2004-04-15 23:14:49 +0000 | [diff] [blame] | 61 | |
| 62 | /* Memory Bank 3 Config == BE=0 */ |
Stefan Roese | 95b602b | 2009-09-24 13:59:57 +0200 | [diff] [blame] | 63 | mtdcr (SDRAM0_CFGADDR, SDRAM0_B3CR); |
Stefan Roese | d1c3b27 | 2009-09-09 16:25:29 +0200 | [diff] [blame] | 64 | mtdcr (SDRAM0_CFGDATA, 0x00000000); |
wdenk | db01a2e | 2004-04-15 23:14:49 +0000 | [diff] [blame] | 65 | |
| 66 | /* refresh timer = 0x400 */ |
Stefan Roese | 95b602b | 2009-09-24 13:59:57 +0200 | [diff] [blame] | 67 | mtdcr (SDRAM0_CFGADDR, SDRAM0_RTR); |
Stefan Roese | d1c3b27 | 2009-09-09 16:25:29 +0200 | [diff] [blame] | 68 | mtdcr (SDRAM0_CFGDATA, 0x04000000); |
wdenk | db01a2e | 2004-04-15 23:14:49 +0000 | [diff] [blame] | 69 | |
| 70 | /* Power management idle timer set to the default. */ |
Stefan Roese | 95b602b | 2009-09-24 13:59:57 +0200 | [diff] [blame] | 71 | mtdcr (SDRAM0_CFGADDR, SDRAM0_PMIT); |
Stefan Roese | d1c3b27 | 2009-09-09 16:25:29 +0200 | [diff] [blame] | 72 | mtdcr (SDRAM0_CFGDATA, 0x07c00000); |
wdenk | db01a2e | 2004-04-15 23:14:49 +0000 | [diff] [blame] | 73 | |
| 74 | udelay (500); |
| 75 | |
| 76 | /* Enable banks (DCE=1, BPRF=1, ECCDD=1, EMDUL=1) */ |
Stefan Roese | 95b602b | 2009-09-24 13:59:57 +0200 | [diff] [blame] | 77 | mtdcr (SDRAM0_CFGADDR, SDRAM0_CFG); |
Stefan Roese | d1c3b27 | 2009-09-09 16:25:29 +0200 | [diff] [blame] | 78 | mtdcr (SDRAM0_CFGDATA, 0x80e00000); |
wdenk | db01a2e | 2004-04-15 23:14:49 +0000 | [diff] [blame] | 79 | |
| 80 | return SDRAM_LEN; |
| 81 | } |
| 82 | |
| 83 | /* |
| 84 | * The U-Boot core, as part of the initialization to prepare for |
| 85 | * loading the monitor into SDRAM, requests of this function that the |
| 86 | * memory be tested. Return 0 if the memory tests OK. |
| 87 | */ |
| 88 | int testdram (void) |
| 89 | { |
| 90 | unsigned long idx; |
| 91 | unsigned val; |
| 92 | unsigned errors; |
| 93 | volatile unsigned long *sdram; |
| 94 | |
| 95 | #ifdef DEBUG |
| 96 | printf ("SDRAM Controller Registers --\n"); |
| 97 | |
Stefan Roese | 95b602b | 2009-09-24 13:59:57 +0200 | [diff] [blame] | 98 | mtdcr (SDRAM0_CFGADDR, SDRAM0_CFG); |
Stefan Roese | d1c3b27 | 2009-09-09 16:25:29 +0200 | [diff] [blame] | 99 | val = mfdcr (SDRAM0_CFGDATA); |
wdenk | db01a2e | 2004-04-15 23:14:49 +0000 | [diff] [blame] | 100 | printf (" SDRAM0_CFG : 0x%08x\n", val); |
| 101 | |
Stefan Roese | d1c3b27 | 2009-09-09 16:25:29 +0200 | [diff] [blame] | 102 | mtdcr (SDRAM0_CFGADDR, 0x24); |
| 103 | val = mfdcr (SDRAM0_CFGDATA); |
wdenk | db01a2e | 2004-04-15 23:14:49 +0000 | [diff] [blame] | 104 | printf (" SDRAM0_STATUS: 0x%08x\n", val); |
| 105 | |
Stefan Roese | 95b602b | 2009-09-24 13:59:57 +0200 | [diff] [blame] | 106 | mtdcr (SDRAM0_CFGADDR, SDRAM0_B0CR); |
Stefan Roese | d1c3b27 | 2009-09-09 16:25:29 +0200 | [diff] [blame] | 107 | val = mfdcr (SDRAM0_CFGDATA); |
wdenk | db01a2e | 2004-04-15 23:14:49 +0000 | [diff] [blame] | 108 | printf (" SDRAM0_B0CR : 0x%08x\n", val); |
| 109 | |
Stefan Roese | 95b602b | 2009-09-24 13:59:57 +0200 | [diff] [blame] | 110 | mtdcr (SDRAM0_CFGADDR, SDRAM0_B1CR); |
Stefan Roese | d1c3b27 | 2009-09-09 16:25:29 +0200 | [diff] [blame] | 111 | val = mfdcr (SDRAM0_CFGDATA); |
wdenk | db01a2e | 2004-04-15 23:14:49 +0000 | [diff] [blame] | 112 | printf (" SDRAM0_B1CR : 0x%08x\n", val); |
| 113 | |
Stefan Roese | 95b602b | 2009-09-24 13:59:57 +0200 | [diff] [blame] | 114 | mtdcr (SDRAM0_CFGADDR, SDRAM0_TR); |
Stefan Roese | d1c3b27 | 2009-09-09 16:25:29 +0200 | [diff] [blame] | 115 | val = mfdcr (SDRAM0_CFGDATA); |
wdenk | db01a2e | 2004-04-15 23:14:49 +0000 | [diff] [blame] | 116 | printf (" SDRAM0_TR : 0x%08x\n", val); |
| 117 | |
Stefan Roese | 95b602b | 2009-09-24 13:59:57 +0200 | [diff] [blame] | 118 | mtdcr (SDRAM0_CFGADDR, SDRAM0_RTR); |
Stefan Roese | d1c3b27 | 2009-09-09 16:25:29 +0200 | [diff] [blame] | 119 | val = mfdcr (SDRAM0_CFGDATA); |
wdenk | db01a2e | 2004-04-15 23:14:49 +0000 | [diff] [blame] | 120 | printf (" SDRAM0_RTR : 0x%08x\n", val); |
| 121 | #endif |
| 122 | |
| 123 | /* Wait for memory to be ready by testing MRSCMPbit |
| 124 | bit. Really, there should already have been plenty of time, |
| 125 | given it was started long ago. But, best to check. */ |
| 126 | for (idx = 0; idx < 1000000; idx += 1) { |
Stefan Roese | d1c3b27 | 2009-09-09 16:25:29 +0200 | [diff] [blame] | 127 | mtdcr (SDRAM0_CFGADDR, 0x24); |
| 128 | val = mfdcr (SDRAM0_CFGDATA); |
wdenk | db01a2e | 2004-04-15 23:14:49 +0000 | [diff] [blame] | 129 | if (val & 0x80000000) |
| 130 | break; |
| 131 | } |
| 132 | |
| 133 | if (!(val & 0x80000000)) { |
| 134 | printf ("SDRAM ERROR: SDRAM0_STATUS never set!\n"); |
| 135 | return 1; |
| 136 | } |
| 137 | |
| 138 | /* Start memory test. */ |
| 139 | printf ("test: %u MB - ", SDRAM_LEN / 1048576); |
| 140 | |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 141 | sdram = (unsigned long *) CONFIG_SYS_SDRAM_BASE; |
wdenk | db01a2e | 2004-04-15 23:14:49 +0000 | [diff] [blame] | 142 | |
| 143 | printf ("write - "); |
| 144 | for (idx = 2; idx < SDRAM_LEN / 4; idx += 2) { |
| 145 | sdram[idx + 0] = idx; |
| 146 | sdram[idx + 1] = ~idx; |
| 147 | } |
| 148 | |
| 149 | printf ("read - "); |
| 150 | errors = 0; |
| 151 | for (idx = 2; idx < SDRAM_LEN / 4; idx += 2) { |
| 152 | if (sdram[idx + 0] != idx) |
| 153 | errors += 1; |
| 154 | if (sdram[idx + 1] != ~idx) |
| 155 | errors += 1; |
| 156 | if (errors > 0) |
| 157 | break; |
| 158 | } |
| 159 | |
| 160 | if (errors > 0) { |
| 161 | printf ("NOT OK\n"); |
| 162 | printf ("FIRST ERROR at %p: 0x%08lx:0x%08lx != 0x%08lx:0x%08lx\n", |
| 163 | sdram + idx, sdram[idx + 0], sdram[idx + 1], idx, ~idx); |
| 164 | return 1; |
| 165 | } |
| 166 | |
| 167 | printf ("ok\n"); |
| 168 | return 0; |
| 169 | } |