blob: 5639beddce9f6299b68a3ae4c67a692fa64d35ba [file] [log] [blame]
wdenkdb01a2e2004-04-15 23:14:49 +00001/*
2 * Copyright (c) 2004 Picture Elements, Inc.
3 * Stephen Williams (steve@icarus.com)
4 *
Wolfgang Denk1a459662013-07-08 09:37:19 +02005 * SPDX-License-Identifier: GPL-2.0+
wdenkdb01a2e2004-04-15 23:14:49 +00006 */
7
8#include <common.h>
Stefan Roeseb36df562010-09-09 19:18:00 +02009#include <asm/ppc4xx.h>
wdenkdb01a2e2004-04-15 23:14:49 +000010#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 Roesea47a12b2010-04-15 16:07:28 +020017 * in arch/powerpc/lib/board.c to initialize the memory and return what I
wdenkdb01a2e2004-04-15 23:14:49 +000018 * found.
19 */
Becky Bruce9973e3c2008-06-09 16:03:40 -050020phys_size_t initdram (int board_type)
wdenkdb01a2e2004-04-15 23:14:49 +000021{
22 /* Configure the SDRAMS */
23
24 /* disable memory controller */
Stefan Roese95b602b2009-09-24 13:59:57 +020025 mtdcr (SDRAM0_CFGADDR, SDRAM0_CFG);
Stefan Roesed1c3b272009-09-09 16:25:29 +020026 mtdcr (SDRAM0_CFGDATA, 0x00000000);
wdenkdb01a2e2004-04-15 23:14:49 +000027
28 udelay (500);
29
30 /* Clear SDRAM0_BESR0 (Bus Error Syndrome Register) */
Stefan Roese95b602b2009-09-24 13:59:57 +020031 mtdcr (SDRAM0_CFGADDR, SDRAM0_BESR0);
Stefan Roesed1c3b272009-09-09 16:25:29 +020032 mtdcr (SDRAM0_CFGDATA, 0xffffffff);
wdenkdb01a2e2004-04-15 23:14:49 +000033
34 /* Clear SDRAM0_BESR1 (Bus Error Syndrome Register) */
Stefan Roese95b602b2009-09-24 13:59:57 +020035 mtdcr (SDRAM0_CFGADDR, SDRAM0_BESR1);
Stefan Roesed1c3b272009-09-09 16:25:29 +020036 mtdcr (SDRAM0_CFGDATA, 0xffffffff);
wdenkdb01a2e2004-04-15 23:14:49 +000037
38 /* Clear SDRAM0_ECCCFG (disable ECC) */
Stefan Roese95b602b2009-09-24 13:59:57 +020039 mtdcr (SDRAM0_CFGADDR, SDRAM0_ECCCFG);
Stefan Roesed1c3b272009-09-09 16:25:29 +020040 mtdcr (SDRAM0_CFGDATA, 0x00000000);
wdenkdb01a2e2004-04-15 23:14:49 +000041
42 /* Clear SDRAM0_ECCESR (ECC Error Syndrome Register) */
Stefan Roese95b602b2009-09-24 13:59:57 +020043 mtdcr (SDRAM0_CFGADDR, SDRAM0_ECCESR);
Stefan Roesed1c3b272009-09-09 16:25:29 +020044 mtdcr (SDRAM0_CFGDATA, 0xffffffff);
wdenkdb01a2e2004-04-15 23:14:49 +000045
46 /* Timing register: CASL=2, PTA=2, CTP=2, LDF=1, RFTA=5, RCD=2 */
Stefan Roese95b602b2009-09-24 13:59:57 +020047 mtdcr (SDRAM0_CFGADDR, SDRAM0_TR);
Stefan Roesed1c3b272009-09-09 16:25:29 +020048 mtdcr (SDRAM0_CFGDATA, 0x010a4016);
wdenkdb01a2e2004-04-15 23:14:49 +000049
50 /* Memory Bank 0 Config == BA=0x00000000, SZ=64M, AM=3, BE=1 */
Stefan Roese95b602b2009-09-24 13:59:57 +020051 mtdcr (SDRAM0_CFGADDR, SDRAM0_B0CR);
Stefan Roesed1c3b272009-09-09 16:25:29 +020052 mtdcr (SDRAM0_CFGDATA, 0x00084001);
wdenkdb01a2e2004-04-15 23:14:49 +000053
54 /* Memory Bank 1 Config == BA=0x04000000, SZ=64M, AM=3, BE=1 */
Stefan Roese95b602b2009-09-24 13:59:57 +020055 mtdcr (SDRAM0_CFGADDR, SDRAM0_B1CR);
Stefan Roesed1c3b272009-09-09 16:25:29 +020056 mtdcr (SDRAM0_CFGDATA, 0x04084001);
wdenkdb01a2e2004-04-15 23:14:49 +000057
58 /* Memory Bank 2 Config == BE=0 */
Stefan Roese95b602b2009-09-24 13:59:57 +020059 mtdcr (SDRAM0_CFGADDR, SDRAM0_B2CR);
Stefan Roesed1c3b272009-09-09 16:25:29 +020060 mtdcr (SDRAM0_CFGDATA, 0x00000000);
wdenkdb01a2e2004-04-15 23:14:49 +000061
62 /* Memory Bank 3 Config == BE=0 */
Stefan Roese95b602b2009-09-24 13:59:57 +020063 mtdcr (SDRAM0_CFGADDR, SDRAM0_B3CR);
Stefan Roesed1c3b272009-09-09 16:25:29 +020064 mtdcr (SDRAM0_CFGDATA, 0x00000000);
wdenkdb01a2e2004-04-15 23:14:49 +000065
66 /* refresh timer = 0x400 */
Stefan Roese95b602b2009-09-24 13:59:57 +020067 mtdcr (SDRAM0_CFGADDR, SDRAM0_RTR);
Stefan Roesed1c3b272009-09-09 16:25:29 +020068 mtdcr (SDRAM0_CFGDATA, 0x04000000);
wdenkdb01a2e2004-04-15 23:14:49 +000069
70 /* Power management idle timer set to the default. */
Stefan Roese95b602b2009-09-24 13:59:57 +020071 mtdcr (SDRAM0_CFGADDR, SDRAM0_PMIT);
Stefan Roesed1c3b272009-09-09 16:25:29 +020072 mtdcr (SDRAM0_CFGDATA, 0x07c00000);
wdenkdb01a2e2004-04-15 23:14:49 +000073
74 udelay (500);
75
76 /* Enable banks (DCE=1, BPRF=1, ECCDD=1, EMDUL=1) */
Stefan Roese95b602b2009-09-24 13:59:57 +020077 mtdcr (SDRAM0_CFGADDR, SDRAM0_CFG);
Stefan Roesed1c3b272009-09-09 16:25:29 +020078 mtdcr (SDRAM0_CFGDATA, 0x80e00000);
wdenkdb01a2e2004-04-15 23:14:49 +000079
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 */
88int 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 Roese95b602b2009-09-24 13:59:57 +020098 mtdcr (SDRAM0_CFGADDR, SDRAM0_CFG);
Stefan Roesed1c3b272009-09-09 16:25:29 +020099 val = mfdcr (SDRAM0_CFGDATA);
wdenkdb01a2e2004-04-15 23:14:49 +0000100 printf (" SDRAM0_CFG : 0x%08x\n", val);
101
Stefan Roesed1c3b272009-09-09 16:25:29 +0200102 mtdcr (SDRAM0_CFGADDR, 0x24);
103 val = mfdcr (SDRAM0_CFGDATA);
wdenkdb01a2e2004-04-15 23:14:49 +0000104 printf (" SDRAM0_STATUS: 0x%08x\n", val);
105
Stefan Roese95b602b2009-09-24 13:59:57 +0200106 mtdcr (SDRAM0_CFGADDR, SDRAM0_B0CR);
Stefan Roesed1c3b272009-09-09 16:25:29 +0200107 val = mfdcr (SDRAM0_CFGDATA);
wdenkdb01a2e2004-04-15 23:14:49 +0000108 printf (" SDRAM0_B0CR : 0x%08x\n", val);
109
Stefan Roese95b602b2009-09-24 13:59:57 +0200110 mtdcr (SDRAM0_CFGADDR, SDRAM0_B1CR);
Stefan Roesed1c3b272009-09-09 16:25:29 +0200111 val = mfdcr (SDRAM0_CFGDATA);
wdenkdb01a2e2004-04-15 23:14:49 +0000112 printf (" SDRAM0_B1CR : 0x%08x\n", val);
113
Stefan Roese95b602b2009-09-24 13:59:57 +0200114 mtdcr (SDRAM0_CFGADDR, SDRAM0_TR);
Stefan Roesed1c3b272009-09-09 16:25:29 +0200115 val = mfdcr (SDRAM0_CFGDATA);
wdenkdb01a2e2004-04-15 23:14:49 +0000116 printf (" SDRAM0_TR : 0x%08x\n", val);
117
Stefan Roese95b602b2009-09-24 13:59:57 +0200118 mtdcr (SDRAM0_CFGADDR, SDRAM0_RTR);
Stefan Roesed1c3b272009-09-09 16:25:29 +0200119 val = mfdcr (SDRAM0_CFGDATA);
wdenkdb01a2e2004-04-15 23:14:49 +0000120 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 Roesed1c3b272009-09-09 16:25:29 +0200127 mtdcr (SDRAM0_CFGADDR, 0x24);
128 val = mfdcr (SDRAM0_CFGDATA);
wdenkdb01a2e2004-04-15 23:14:49 +0000129 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-VILLARD6d0f6bc2008-10-16 15:01:15 +0200141 sdram = (unsigned long *) CONFIG_SYS_SDRAM_BASE;
wdenkdb01a2e2004-04-15 23:14:49 +0000142
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}