1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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 u8 rows;
33 u8 cols;
34 u8 banks2;
35 u8 org;
36} cpc710_mem_org_t;
37
38static int cpc710_compute_mcer (u32 * mcer,
39 unsigned long *size, unsigned int sdram);
40static int cpc710_eeprom_checksum (unsigned int sdram);
41static u8 cpc710_eeprom_read (unsigned int sdram, unsigned int offset);
42
43static u32 cpc710_mcer_mem[] = {
44 0x000003f3,
45 0x000003e3,
46 0x000003c3,
47 0x00000383,
48 0x00000303,
49 0x00000203,
50 0x00000003,
51 0x00000002,
52 0x00000001
53};
54static cpc710_mem_org_t cpc710_mem_org[] = {
55 {0x0c, 0x09, 0x02, 0x00},
56 {0x0d, 0x09, 0x02, 0x00},
57 {0x0d, 0x0a, 0x02, 0x00},
58 {0x0d, 0x0b, 0x02, 0x00},
59 {0x0d, 0x0c, 0x02, 0x00},
60 {0x0e, 0x0c, 0x02, 0x00},
61 {0x0b, 0x08, 0x02, 0x01},
62 {0x0b, 0x09, 0x01, 0x02},
63 {0x0b, 0x0a, 0x01, 0x03},
64 {0x0c, 0x08, 0x02, 0x04},
65 {0x0c, 0x0a, 0x02, 0x05},
66 {0x0d, 0x08, 0x01, 0x06},
67 {0x0d, 0x08, 0x02, 0x07},
68 {0x0d, 0x09, 0x01, 0x08},
69 {0x0d, 0x0a, 0x01, 0x09},
70 {0x0b, 0x08, 0x01, 0x0a},
71 {0x0c, 0x08, 0x01, 0x0b},
72 {0x0c, 0x09, 0x01, 0x0c},
73 {0x0e, 0x09, 0x02, 0x0d},
74 {0x0e, 0x0a, 0x02, 0x0e},
75 {0x0e, 0x0b, 0x02, 0x0f}
76};
77
78unsigned long cpc710_ram_init (void)
79{
80 unsigned long memsize = 0;
81 unsigned long bank_size;
82 u32 mcer;
83
84#ifndef CONFIG_SYS_RAMBOOT
85
86
87 out32 (REG (SDRAM0, MCER0), 0);
88 out32 (REG (SDRAM0, MCER1), 0);
89 out32 (REG (SDRAM0, MCER2), 0);
90 out32 (REG (SDRAM0, MCER3), 0);
91 out32 (REG (SDRAM0, MCER4), 0);
92 out32 (REG (SDRAM0, MCER5), 0);
93 out32 (REG (SDRAM0, MCER6), 0);
94 out32 (REG (SDRAM0, MCER7), 0);
95 iobarrier_rw ();
96
97
98
99 out32 (REG (SDRAM0, MCCR), 0x13b06000);
100 iobarrier_rw ();
101#endif
102
103
104
105 if (!cpc710_compute_mcer (&mcer, &bank_size, 0)) {
106 puts ("Unsupported SDRAM type !\n");
107 hang ();
108 }
109 memsize += bank_size;
110#ifndef CONFIG_SYS_RAMBOOT
111
112
113 out32 (REG (SDRAM0, MCER0), mcer | 0x80000000);
114 iobarrier_rw ();
115#endif
116
117#ifndef CONFIG_SYS_RAMBOOT
118
119
120 out32 (REG (SDRAM0, MCCR), in32 (REG (SDRAM0, MCCR)) | 0x80000000);
121
122
123
124 while (!(in32 (REG (SDRAM0, MCCR)) & 0x20000000)) {
125 iobarrier_rw ();
126 }
127
128
129
130 out32 (REG (SDRAM0, MESR), 0);
131 out32 (REG (SDRAM0, MEAR), 0);
132 iobarrier_rw ();
133
134
135
136#endif
137
138
139
140 out32 (REG (CPC0, RGBAN1), memsize);
141
142 return memsize;
143}
144
145static int cpc710_compute_mcer (u32 * mcer, unsigned long *size, unsigned int sdram)
146{
147 u8 rows;
148 u8 cols;
149 u8 banks2;
150 unsigned int lines;
151 u32 mc = 0;
152 unsigned int i;
153 cpc710_mem_org_t *org = 0;
154
155 if (!i2c_reset ()) {
156 puts ("Can't reset I2C!\n");
157 hang ();
158 }
159
160 if (!cpc710_eeprom_checksum (sdram)) {
161 puts ("Invalid EEPROM checksum !\n");
162 hang ();
163 }
164
165 rows = cpc710_eeprom_read (sdram, 3);
166 cols = cpc710_eeprom_read (sdram, 4);
167
168
169 banks2 = cpc710_eeprom_read (sdram, 17) / 2;
170
171 lines = rows + cols + banks2;
172
173 if (lines < 18 || lines > 26) {
174
175
176 return 0;
177 }
178
179 mc |= cpc710_mcer_mem[lines - 18] << 6;
180
181 for (i = 0; i < sizeof (cpc710_mem_org) / sizeof (cpc710_mem_org_t);
182 i++) {
183 cpc710_mem_org_t *corg = cpc710_mem_org + i;
184
185 if (corg->rows == rows && corg->cols == cols
186 && corg->banks2 == banks2) {
187 org = corg;
188
189 break;
190 }
191 }
192
193 if (!org) {
194
195
196 return 0;
197 }
198
199 mc |= (u32) org->org << 2;
200
201
202
203 *mcer = mc;
204 *size = 1l << (lines + 4);
205
206 return 1;
207}
208
209static int cpc710_eeprom_checksum (unsigned int sdram)
210{
211 u8 sum = 0;
212 unsigned int i;
213
214 for (i = 0; i < 63; i++) {
215 sum += cpc710_eeprom_read (sdram, i);
216 }
217
218 return sum == cpc710_eeprom_read (sdram, 63);
219}
220
221static u8 cpc710_eeprom_read (unsigned int sdram, unsigned int offset)
222{
223 u8 dev = (sdram << 1) | 0xa0;
224 u8 data;
225
226 if (!i2c_read_byte (&data, dev, offset)) {
227 puts ("I2C error !\n");
228 hang ();
229 }
230
231 return data;
232}
233