blob: 57ed8f087fbf87ab9085b306390ddac571763b59 [file] [log] [blame]
wdenkc6097192002-11-03 00:24:07 +00001/*
2 * (C) Copyright 2002
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 <config.h>
25#include <common.h>
26#include <asm/io.h>
27
28#include "pcippc2.h"
29#include "i2c.h"
30
31typedef struct cpc710_mem_org_s
32{
33 u8 rows;
34 u8 cols;
35 u8 banks2;
36 u8 org;
37} cpc710_mem_org_t;
38
39static int cpc710_compute_mcer (u32 * mcer,
40 unsigned long *
41 size,
42 unsigned int sdram);
43static int cpc710_eeprom_checksum (unsigned int sdram);
44static u8 cpc710_eeprom_read (unsigned int sdram,
45 unsigned int offset);
46
47static u32 cpc710_mcer_mem [] =
48{
49 0x000003f3, /* 18 lines, 4 Mb */
50 0x000003e3, /* 19 lines, 8 Mb */
51 0x000003c3, /* 20 lines, 16 Mb */
52 0x00000383, /* 21 lines, 32 Mb */
53 0x00000303, /* 22 lines, 64 Mb */
54 0x00000203, /* 23 lines, 128 Mb */
55 0x00000003, /* 24 lines, 256 Mb */
56 0x00000002, /* 25 lines, 512 Mb */
57 0x00000001 /* 26 lines, 1024 Mb */
58};
59static cpc710_mem_org_t cpc710_mem_org [] =
60{
61 { 0x0c, 0x09, 0x02, 0x00 }, /* 0000: 12/ 9/2 */
62 { 0x0d, 0x09, 0x02, 0x00 }, /* 0000: 13/ 9/2 */
63 { 0x0d, 0x0a, 0x02, 0x00 }, /* 0000: 13/10/2 */
64 { 0x0d, 0x0b, 0x02, 0x00 }, /* 0000: 13/11/2 */
65 { 0x0d, 0x0c, 0x02, 0x00 }, /* 0000: 13/12/2 */
66 { 0x0e, 0x0c, 0x02, 0x00 }, /* 0000: 14/12/2 */
67 { 0x0b, 0x08, 0x02, 0x01 }, /* 0001: 11/ 8/2 */
68 { 0x0b, 0x09, 0x01, 0x02 }, /* 0010: 11/ 9/1 */
69 { 0x0b, 0x0a, 0x01, 0x03 }, /* 0011: 11/10/1 */
70 { 0x0c, 0x08, 0x02, 0x04 }, /* 0100: 12/ 8/2 */
71 { 0x0c, 0x0a, 0x02, 0x05 }, /* 0101: 12/10/2 */
72 { 0x0d, 0x08, 0x01, 0x06 }, /* 0110: 13/ 8/1 */
73 { 0x0d, 0x08, 0x02, 0x07 }, /* 0111: 13/ 8/2 */
74 { 0x0d, 0x09, 0x01, 0x08 }, /* 1000: 13/ 9/1 */
75 { 0x0d, 0x0a, 0x01, 0x09 }, /* 1001: 13/10/1 */
76 { 0x0b, 0x08, 0x01, 0x0a }, /* 1010: 11/ 8/1 */
77 { 0x0c, 0x08, 0x01, 0x0b }, /* 1011: 12/ 8/1 */
78 { 0x0c, 0x09, 0x01, 0x0c }, /* 1100: 12/ 9/1 */
79 { 0x0e, 0x09, 0x02, 0x0d }, /* 1101: 14/ 9/2 */
80 { 0x0e, 0x0a, 0x02, 0x0e }, /* 1110: 14/10/2 */
81 { 0x0e, 0x0b, 0x02, 0x0f } /* 1111: 14/11/2 */
82};
83
84unsigned long cpc710_ram_init (void)
85{
86 unsigned long memsize = 0;
87 unsigned long bank_size;
88 u32 mcer;
89
90#ifndef CFG_RAMBOOT
91 /* Clear memory banks
92 */
93 out32(REG(SDRAM0, MCER0), 0);
94 out32(REG(SDRAM0, MCER1), 0);
95 out32(REG(SDRAM0, MCER2), 0);
96 out32(REG(SDRAM0, MCER3), 0);
97 out32(REG(SDRAM0, MCER4), 0);
98 out32(REG(SDRAM0, MCER5), 0);
99 out32(REG(SDRAM0, MCER6), 0);
100 out32(REG(SDRAM0, MCER7), 0);
101 iobarrier_rw();
102
103 /* Disable memory
104 */
105 out32(REG(SDRAM0,MCCR), 0x13b06000);
106 iobarrier_rw();
107#endif
108
109 /* Only the first memory bank is initialised now
110 */
111 if (! cpc710_compute_mcer(& mcer, & bank_size, 0))
112 {
113 puts("Unsupported SDRAM type !\n");
114 hang();
115 }
116 memsize += bank_size;
117#ifndef CFG_RAMBOOT
118 /* Enable bank, zero start
119 */
120 out32(REG(SDRAM0, MCER0), mcer | 0x80000000);
121 iobarrier_rw();
122#endif
123
124#ifndef CFG_RAMBOOT
125 /* Enable memory
126 */
127 out32(REG(SDRAM0, MCCR), in32(REG(SDRAM0, MCCR)) | 0x80000000);
128
129 /* Wait until initialisation finished
130 */
131 while (! (in32 (REG(SDRAM0, MCCR)) & 0x20000000))
132 {
133 iobarrier_rw();
134 }
135
136 /* Clear Memory Error Status and Address registers
137 */
138 out32(REG(SDRAM0, MESR), 0);
139 out32(REG(SDRAM0, MEAR), 0);
140 iobarrier_rw();
141
142 /* ECC is not configured now
143 */
144#endif
145
146 /* Memory size counter
147 */
148 out32(REG(CPC0, RGBAN1), memsize);
149
150 return memsize;
151}
152
153static int cpc710_compute_mcer (
154 u32 * mcer,
155 unsigned long * size,
156 unsigned int sdram)
157{
158 u8 rows;
159 u8 cols;
160 u8 banks2;
161 unsigned int lines;
162 u32 mc = 0;
163 unsigned int i;
164 cpc710_mem_org_t * org = 0;
165
166
167 if (! i2c_reset())
168 {
169 puts("Can't reset I2C!\n");
170 hang();
171 }
172
173 if (! cpc710_eeprom_checksum(sdram))
174 {
175 puts("Invalid EEPROM checksum !\n");
176 hang();
177 }
178
179 rows = cpc710_eeprom_read(sdram, 3);
180 cols = cpc710_eeprom_read(sdram, 4);
181 /* Can be 2 or 4 banks; divide by 2
182 */
183 banks2 = cpc710_eeprom_read(sdram, 17) / 2;
184
185 lines = rows + cols + banks2;
186
187 if (lines < 18 || lines > 26)
188 {
189 /* Unsupported configuration
190 */
191 return 0;
192 }
193
194
195 mc |= cpc710_mcer_mem [lines - 18] << 6;
196
197 for (i = 0; i < sizeof(cpc710_mem_org) / sizeof(cpc710_mem_org_t); i++)
198 {
199 cpc710_mem_org_t * corg = cpc710_mem_org + i;
200
201 if (corg->rows == rows && corg->cols == cols && corg->banks2 == banks2)
202 {
203 org = corg;
204
205 break;
206 }
207 }
208
209 if (! org)
210 {
211 /* Unsupported configuration
212 */
213 return 0;
214 }
215
216 mc |= (u32) org->org << 2;
217
218 /* Supported configuration
219 */
220 *mcer = mc;
221 *size = 1l << (lines + 4);
222
223 return 1;
224}
225
226static int cpc710_eeprom_checksum (
227 unsigned int sdram)
228{
229 u8 sum = 0;
230 unsigned int i;
231
232 for (i = 0; i < 63; i++)
233 {
234 sum += cpc710_eeprom_read(sdram, i);
235 }
236
237 return sum == cpc710_eeprom_read(sdram, 63);
238}
239
240static u8 cpc710_eeprom_read (
241 unsigned int sdram,
242 unsigned int offset)
243{
244 u8 dev = (sdram << 1) | 0xa0;
245 u8 data;
246
247 if (! i2c_read_byte(& data, dev,offset))
248 {
249 puts("I2C error !\n");
250 hang();
251 }
252
253 return data;
254}