-
Notifications
You must be signed in to change notification settings - Fork 110
/
Copy pathsdram.c
executable file
·360 lines (273 loc) · 10.6 KB
/
sdram.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
/*=============================================================================
Copyright (C) 2016 Kristina Brooks
All rights reserved.
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
FILE DESCRIPTION
VideoCoreIV SDRAM initialization code.
=============================================================================*/
#include "lib/common.h"
#include "hardware.h"
extern uint32_t g_CPUID;
#define MR_REQUEST_SUCCESS(x) ((SD_MR_TIMEOUT_SET & x) != SD_MR_TIMEOUT_SET)
#define MR_GET_RDATA(x) ((x & SD_MR_RDATA_SET) >> SD_MR_RDATA_LSB)
#define RAM_TEST_ADDR 0xC0000000
#define RAM_TEST_PATTERN 0xAAAAAAAA
#define SIP_DEBUG(x) x
#define SCLKU_DEBUG(x) //SIP_DEBUG(x)
ALWAYS_INLINE inline void sdram_clkman_update_begin() {
CM_SDCCTL |= CM_PASSWORD | CM_SDCCTL_UPDATE_SET;
SCLKU_DEBUG(printf("%s: waiting for ACCPT (%X) ...\n", __FUNCTION__, CM_SDCCTL));
for (;;) if (CM_SDCCTL & CM_SDCCTL_ACCPT_SET) break;
SCLKU_DEBUG(printf("%s: ACCPT received! (%X)\n", __FUNCTION__, CM_SDCCTL));
}
ALWAYS_INLINE inline void sdram_clkman_update_end() {
CM_SDCCTL = CM_PASSWORD | (CM_SDCCTL & CM_SDCCTL_UPDATE_CLR);
SCLKU_DEBUG(printf("%s: waiting for ACCPT clear (%X) ...\n", __FUNCTION__, CM_SDCCTL));
for (;;) if ((CM_SDCCTL & CM_SDCCTL_ACCPT_SET) == 0) break;
SCLKU_DEBUG(printf("%s: ACCPT cleared! (%X)\n", __FUNCTION__, CM_SDCCTL));
}
ALWAYS_INLINE void sdram_reset_phy_lines() {
SIP_DEBUG(printf("%s: resetting APHY/DPHY lines ...\n", __FUNCTION__));
/* politely tell sdc that we'll be messing with address lines */
APHY_CSR_PHY_BIST_CNTRL_SPR = 0x30;
DPHY_CSR_GLBL_DQ_DLL_RESET = 0x1;
APHY_CSR_GLBL_ADDR_DLL_RESET = 0x1;
/* stall ... */
SD_CS;
SD_CS;
SD_CS;
SD_CS;
DPHY_CSR_GLBL_DQ_DLL_RESET = 0x0;
APHY_CSR_GLBL_ADDR_DLL_RESET = 0x0;
SIP_DEBUG(printf("%s: waiting for DPHY master PLL to lock ...\n", __FUNCTION__));
for (;;) if ((DPHY_CSR_GLBL_MSTR_DLL_LOCK_STAT & 0xFFFF) == 0xFFFF) break;
SIP_DEBUG(printf("%s: DPHY master PLL locked!\n", __FUNCTION__));
}
void sdram_init_late() {
uint32_t ctrl = 0x4;
SD_CS = (SD_CS & ~(SD_CS_DEL_KEEP_SET|SD_CS_DPD_SET|SD_CS_RESTRT_SET)) | SD_CS_STBY_SET;
/* wait for SDRAM controller to go down */
SIP_DEBUG(printf("%s: waiting for SDRAM controller to go down (%X) ...\n", __FUNCTION__, SD_CS));
for (;;) if ((SD_CS & SD_CS_SDUP_SET) == 0) break;
SIP_DEBUG(printf("%s: SDRAM controller down!\n", __FUNCTION__));
/* disable SDRAM clock */
sdram_clkman_update_begin();
CM_SDCCTL = (CM_SDCCTL & ~(CM_SDCCTL_ENAB_SET|CM_SDCCTL_CTRL_SET)) | CM_PASSWORD;
sdram_clkman_update_end();
SIP_DEBUG(printf("%s: SDRAM clock disabled!\n", __FUNCTION__));
/* left */
APHY_CSR_DDR_PLL_PWRDWN = 0;
APHY_CSR_DDR_PLL_GLOBAL_RESET = 0;
APHY_CSR_DDR_PLL_POST_DIV_RESET = 0;
APHY_CSR_DDR_PLL_VCO_FREQ_CNTRL0 = (1 << 16) | 0x53 /* magic */;
APHY_CSR_DDR_PLL_VCO_FREQ_CNTRL1 = 0;
APHY_CSR_DDR_PLL_MDIV_VALUE = 0;
APHY_CSR_DDR_PLL_GLOBAL_RESET = 1;
SIP_DEBUG(printf("%s: waiting for APHY DDR PLL to lock ...\n", __FUNCTION__));
for (;;) if (APHY_CSR_DDR_PLL_LOCK_STATUS & (1 << 16)) break;
SIP_DEBUG(printf("%s: APHY DDR PLL locked!\n", __FUNCTION__));
APHY_CSR_DDR_PLL_POST_DIV_RESET = 1;
sdram_clkman_update_begin();
CM_SDCCTL = CM_PASSWORD | (ctrl << CM_SDCCTL_CTRL_LSB) | (CM_SDCCTL & CM_SDCCTL_CTRL_CLR);
sdram_clkman_update_end();
SIP_DEBUG(printf("%s: CM_SDCCTL = 0x%X\n", __FUNCTION__, CM_SDCCTL));
/* second stage magic values */
SD_SA = 0x0C293395;
SD_SB = 0x0F9;
SD_SC = 0x32200743;
SD_SD = 0x71810F66;
SD_SE = 0x10412136;
SD_PT1 = 0x137B828;
SD_PT2 = 0x0F96;
SD_MRT = 0x3;
sdram_reset_phy_lines();
/* wait for address line pll to come back */
SIP_DEBUG(printf("%s: waiting for APHY global PLL to lock ...\n", __FUNCTION__));
for (;;) if (APHY_CSR_GLBL_ADR_DLL_LOCK_STAT == 3) break;
SIP_DEBUG(printf("%s: APHY global PLL locked!\n", __FUNCTION__));
/* tell sdc we're done messing with address lines */
APHY_CSR_PHY_BIST_CNTRL_SPR = 0x0;
/* woo, turn on sdram! */
SD_CS = (0x200042 & ~(SD_CS_STOP_SET|SD_CS_STBY_SET)) | SD_CS_RESTRT_SET;
}
unsigned int sdram_read_mr(unsigned int addr) {
while ((SD_MR & SD_MR_DONE_SET) != SD_MR_DONE_SET) {}
SD_MR = addr & 0xFF;
unsigned int mrr;
while (((mrr = SD_MR) & SD_MR_DONE_SET) != SD_MR_DONE_SET) {}
return mrr;
}
unsigned int sdram_write_mr(unsigned int addr, unsigned int data, bool wait) {
while ((SD_MR & SD_MR_DONE_SET) != SD_MR_DONE_SET) {}
SD_MR = (addr & 0xFF) | ((data & 0xFF) << 8) | SD_MR_RW_SET;
if (wait) {
unsigned int mrr;
while (((mrr = SD_MR) & SD_MR_DONE_SET) != SD_MR_DONE_SET) {}
if (mrr & SD_MR_TIMEOUT_SET)
panic("MR write timed out (addr=%d data=0x%X)", addr, data);
return mrr;
}
else {
return 0;
}
}
void sdram_reset_phy() {
printf("%s: resetting SDRAM PHY ...\n", __FUNCTION__);
/* reset PHYC */
SD_PHYC = SD_PHYC_PHYRST_SET;
udelay(64);
SD_PHYC = 0;
printf("%s: resetting DPHY CTRL ...\n", __FUNCTION__);
DPHY_CSR_DQ_PHY_MISC_CTRL = 0x7;
DPHY_CSR_DQ_PAD_MISC_CTRL = 0x0;
DPHY_CSR_BOOT_READ_DQS_GATE_CTRL = 0x11;
sdram_reset_phy_lines();
APHY_CSR_PHY_BIST_CNTRL_SPR = 0x0;
}
static void sdram_set_clock_source(unsigned int source, unsigned int div_) {
CM_SDCDIV = CM_PASSWORD | (div_ << CM_SDCDIV_DIV_LSB);
CM_SDCCTL = CM_PASSWORD | (CM_SDCCTL & CM_SDCCTL_SRC_CLR) | source;
CM_SDCCTL |= CM_PASSWORD | CM_SDCCTL_ENAB_SET;
printf("%s: source set to %d, div to %d, waiting for BUSY set (%X) ... \n", __FUNCTION__, source, div_, CM_SDCCTL);
for (;;) if (CM_SDCCTL & CM_SDCCTL_BUSY_SET) break;
printf("%s: BUSY set! (%X)\n", __FUNCTION__, CM_SDCCTL);
}
static void sdram_init_clkman()
{
uint32_t ctrl = 0;
sdram_clkman_update_begin();
CM_SDCCTL = CM_PASSWORD | (ctrl << CM_SDCCTL_CTRL_LSB) | (CM_SDCCTL & CM_SDCCTL_CTRL_CLR);
sdram_clkman_update_end();
}
static const char* lpddr2_manufacturer_name(uint32_t mr) {
switch (mr) {
case 1: return "Samsung";
case 2: return "Qimonda";
case 3: return "Elpida";
case 4: return "Etron";
case 5: return "Nanya";
case 6: return "Hynix";
default: return "Unknown";
}
}
static const char* lpddr2_density(uint32_t mr) {
/*
* i'm so stupid, why did i not notice that LPDDR2
* spec listed those as bits, not bytes *sigh*
*/
switch ((mr & 0x33) >> 3) {
case 1: return "128MB";
case 2: return "256MB";
case 3: return "512MB";
case 4: return "1GB";
case 5: return "2GB";
case 6: return "4GB";
default: return "Unknown";
}
}
static void sdram_calibrate() {
/* some hw revisions require different slews */
bool st = ((g_CPUID >> 4) & 0xFFF) == 0x14;
uint32_t dq_slew = (st ? 2 : 3);
/* i don't get it, the spec says do not use this register */
sdram_write_mr(0xFF, 0, true);
/* RL = 6 / WL = 3 */
sdram_write_mr(LPDDR2_MR_DEVICE_FEATURE_2, 4, true);
APHY_CSR_ADDR_PAD_DRV_SLEW_CTRL = 0x333;
DPHY_CSR_DQ_PAD_DRV_SLEW_CTRL = (dq_slew << 8) | (dq_slew << 4) | 3;
printf("%s: DPHY_CSR_DQ_PAD_DRV_SLEW_CTRL = 0x%X\n", __FUNCTION__, DPHY_CSR_DQ_PAD_DRV_SLEW_CTRL);
/* tell sdc we want to calibrate */
APHY_CSR_PHY_BIST_CNTRL_SPR = 0x20;
APHY_CSR_ADDR_PVT_COMP_CTRL = 0x1;
printf("%s: waiting for address PVT calibration ...\n", __FUNCTION__);
for (;;) if (APHY_CSR_ADDR_PVT_COMP_STATUS & 2) break;
DPHY_CSR_DQ_PVT_COMP_CTRL = 0x1;
printf("%s: waiting for data PVT calibration ...\n", __FUNCTION__);
for (;;) if (DPHY_CSR_DQ_PVT_COMP_STATUS & 2) break;
/* tell sdc we're done calibrating */
APHY_CSR_PHY_BIST_CNTRL_SPR = 0x0;
/* send calibration command */
uint32_t old_mrt = SD_MRT;
SD_MRT = 20;
printf("%s: waiting for SDRAM calibration command ...\n", __FUNCTION__);
SD_MR = LPDDR2_MR_CALIBRATION | (0xFF << 8) | SD_MR_RW_SET | SD_MR_HI_Z_SET;
while ((SD_MR & SD_MR_DONE_SET) != SD_MR_DONE_SET) {}
SD_MRT = old_mrt;
sdram_write_mr(LPDDR2_MR_IO_CONFIG, is_thing ? 3 : 2, false);
}
static void sdram_selftest()
{
volatile uint32_t* p = (volatile uint32_t*)RAM_TEST_ADDR;
printf("Testing SDRAM ...\n");
for (int i = 0; i < 0x100000; i++) {
p[i] = RAM_TEST_PATTERN;
if (p[i] != RAM_TEST_PATTERN)
panic("sdram initialization failed (idx=%d exptected=0x%x got=0x%x)", i, RAM_TEST_PATTERN, p[i]);
}
printf("SDRAM test successful!\n");
}
void sdram_init() {
uint32_t vendor_id, bc;
printf("%s: (0) SD_CS = 0x%X\n", __FUNCTION__, SD_CS);
PM_SMPS = PM_PASSWORD | 0x1;
A2W_SMPS_LDO1 = A2W_PASSWORD | 0x40000;
A2W_SMPS_LDO0 = A2W_PASSWORD | 0x0;
A2W_XOSC_CTRL |= A2W_PASSWORD | A2W_XOSC_CTRL_DDREN_SET;
/*
* STEP 1:
* configure the low-frequency PLL and enable SDC and perform
* the calibration sequence.
*/
sdram_set_clock_source(CM_SRC_OSC, 1);
sdram_init_clkman();
sdram_reset_phy();
/* magic values */
SD_SA = 0x6E3395;
SD_SB = 0x0F9;
SD_SC = 0x6000431;
SD_SD = 0x10000011;
SD_SE = 0x10106000;
SD_PT1 = 0x0AF002;
SD_PT2 = 0x8C;
SD_MRT = 0x3;
SD_CS = 0x200042;
/* wait for SDRAM controller */
printf("%s: waiting for SDUP (%X) ...\n", __FUNCTION__, SD_CS);
for (;;) if (SD_CS & SD_CS_SDUP_SET) break;
printf("%s: SDRAM controller has arrived! (%X)\n", __FUNCTION__, SD_CS);
/* RL = 6 / WL = 3 */
sdram_write_mr(LPDDR2_MR_DEVICE_FEATURE_2, 4, false);
sdram_calibrate();
/* identify installed memory */
vendor_id = sdram_read_mr(LPDDR2_MR_MANUFACTURER_ID);
if (!MR_REQUEST_SUCCESS(vendor_id)) {
panic("vendor id memory register read timed out");
}
vendor_id = MR_GET_RDATA(vendor_id);
bc = sdram_read_mr(LPDDR2_MR_METRICS);
if (!MR_REQUEST_SUCCESS(bc)) {
panic("basic configuration memory register read timed out");
}
bc = MR_GET_RDATA(bc);
printf("SDRAM Type: %s %s LPDDR2 (BC=0x%X)\n",
lpddr2_manufacturer_name(vendor_id),
lpddr2_density(bc),
bc);
/*
* STEP 2:
* after calibration, enable high-freq SDRAM PLL. because we're
* running from cache, we can freely mess with SDRAM clock without
* any issues, removing the need to copy the SDRAM late init stuff
* to bootrom ram. if later code that's running from SDRAM wants to
* mess with SDRAM clock it would need to do that.
*/
sdram_init_late();
sdram_selftest();
}