summaryrefslogtreecommitdiffstats
path: root/arch/arm/boards/guf-cupid/lowlevel.c
blob: 0f7d7533edce7ffb246011b63f6b921f83baa3d2 (plain)
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
/*
 *
 * (c) 2007 Pengutronix, Sascha Hauer <s.hauer@pengutronix.de>
 *
 * See file CREDITS for list of people who contributed to this
 * project.
 *
 * 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.
 *
 */
#include <common.h>
#include <init.h>
#include <mach/imx-regs.h>
#include <mach/imx-pll.h>
#include <mach/esdctl.h>
#include <asm/cache-l2x0.h>
#include <io.h>
#include <mach/imx-nand.h>
#include <asm/barebox-arm.h>
#include <asm-generic/sections.h>
#include <asm-generic/memory_layout.h>
#include <asm/system.h>

/* Assuming 24MHz input clock */
#define MPCTL_PARAM_399     (IMX_PLL_PD(0) | IMX_PLL_MFD(15) | IMX_PLL_MFI(8) | IMX_PLL_MFN(5))
#define MPCTL_PARAM_532     ((1 << 31) | IMX_PLL_PD(0) | IMX_PLL_MFD(11) | IMX_PLL_MFI(11) | IMX_PLL_MFN(1))
#define PPCTL_PARAM_300     (IMX_PLL_PD(0) | IMX_PLL_MFD(3) | IMX_PLL_MFI(6) | IMX_PLL_MFN(1))

#define SDRAM_MODE_BL_8	0x0003
#define SDRAM_MODE_BSEQ	0x0000
#define SDRAM_MODE_CL_3	0x0030
#define MDDR_DS_HALF	0x20
#define SDRAM_COMPARE_CONST1	0x55555555
#define SDRAM_COMPARE_CONST2	0xaaaaaaaa

#ifdef CONFIG_NAND_IMX_BOOT
static void __bare_init __naked insdram(void)
{
	uint32_t r;

	/* Speed up NAND controller by adjusting the NFC divider */
	r = readl(IMX_CCM_BASE + CCM_PDR4);
	r &= ~(0xf << 28);
	r |= 0x1 << 28;
	writel(r, IMX_CCM_BASE + CCM_PDR4);

	/* setup a stack to be able to call imx_nand_load_image() */
	r = STACK_BASE + STACK_SIZE - 12;
	__asm__ __volatile__("mov sp, %0" : : "r"(r));

	imx_nand_load_image(_text, barebox_image_size);

	board_init_lowlevel_return();
}
#endif

static void __bare_init noinline setup_sdram(u32 memsize, u32 mode, u32 sdram_addr)
{
	volatile int loop;
	void *r9 = (void *)IMX_SDRAM_CS0;
	u32 r11 = 0xda; /* dummy constant */
	u32 r1, r0;

	/* disable second SDRAM region to save power */
	r1 = readl(ESDCTL1);
	r1 &= ~ESDCTL0_SDE;
	writel(r1, ESDCTL1);

	mode |= ESDMISC_RST | ESDMISC_MDDR_DL_RST;
	writel(mode, ESDMISC);

	mode &= ~(ESDMISC_RST | ESDMISC_MDDR_DL_RST);
	writel(mode, ESDMISC);

	/* wait for esdctl reset */
	for (loop = 0; loop < 0x20000; loop++);

	r1 = ESDCFGx_tXP_4 | ESDCFGx_tWTR_1 |
		ESDCFGx_tRP_3 | ESDCFGx_tMRD_2 |
		ESDCFGx_tWR_1_2 | ESDCFGx_tRAS_6 |
		ESDCFGx_tRRD_2 | ESDCFGx_tCAS_3 |
		ESDCFGx_tRCD_3 | ESDCFGx_tRC_20;

	writel(r1, ESDCFG0);

	/* enable SDRAM controller */
	writel(memsize | ESDCTL0_SMODE_NORMAL, ESDCTL0);

	/* Micron Datasheet Initialization Step 3: Wait 200us before first command */
	for (loop = 0; loop < 1000; loop++);

	/* Micron Datasheet Initialization Step 4: PRE CHARGE ALL */
	writel(memsize | ESDCTL0_SMODE_PRECHARGE, ESDCTL0);
	writeb(r11, sdram_addr);

	/* Micron Datasheet Initialization Step 5: NOP for tRP (at least 22.5ns)
	 * The CPU is not fast enough to cause a problem here
	 */

	/* Micron Datasheet Initialization Step 6: 2 AUTO REFRESH and tRFC NOP
	 * (at least 140ns)
	 */
	writel(memsize | ESDCTL0_SMODE_AUTO_REFRESH, ESDCTL0);
	writeb(r11, r9); /* AUTO REFRESH #1 */

	for (loop = 0; loop < 3; loop++); /* ~140ns delay at 532MHz */

	writeb(r11, r9); /* AUTO REFRESH #2 */

	for (loop = 0; loop < 3; loop++); /* ~140ns delay at 532MHz */

	/* Micron Datasheet Initialization Step 7: LOAD MODE REGISTER */
	writel(memsize | ESDCTL0_SMODE_LOAD_MODE, ESDCTL0);
	writeb(r11, r9 + (SDRAM_MODE_BL_8 | SDRAM_MODE_BSEQ | SDRAM_MODE_CL_3));

	/* Micron Datasheet Initialization Step 8: tMRD = 2 tCK NOP
	 * (The memory controller will take care of this delay)
	 */

	/* Micron Datasheet Initialization Step 9: LOAD MODE REGISTER EXTENDED */
	writeb(r11, 0x84000000 | MDDR_DS_HALF);  /*we assume 14 Rows / 10 Cols here */

	/* Micron Datasheet Initialization Step 9: tMRD = 2 tCK NOP
	 * (The memory controller will take care of this delay)
	 */

	/* Now configure SDRAM-Controller and check that it works */
	writel(memsize | ESDCTL0_BL | ESDCTL0_REF4, ESDCTL0);

	/* Freescale asks for first access to be a write to properly
	 * initialize DQS pin-state and keepers
	 */
	writel(0xdeadbeef, r9);

	/* test that the RAM is in fact working */
	writel(SDRAM_COMPARE_CONST1, r9);
	writel(SDRAM_COMPARE_CONST2, r9 + 0x4);

	if (readl(r9) != SDRAM_COMPARE_CONST1)
		while (1);

	/* Verify that the correct row and coloumn is selected */

	/* So far we asssumed that we have 14 rows, verify this */
	writel(SDRAM_COMPARE_CONST1, r9);
	writel(SDRAM_COMPARE_CONST2, r9 + (1 << 25));

	/* if both value are identical, we don't have 14 rows. assume 13 instead */
	if (readl(r9) == readl(r9 + (1 << 25))) {
		r0 = readl(ESDCTL0);
		r0 &= ~ESDCTL0_ROW_MASK;
		r0 |= ESDCTL0_ROW13;
		writel(r0, ESDCTL0);
	}

	/* So far we asssumed that we have 10 columns, verify this */
	writel(SDRAM_COMPARE_CONST1, r9);
	writel(SDRAM_COMPARE_CONST2, r9 + (1 << 11));

	/* if both value are identical, we don't have 10 cols. assume 9 instead */
	if (readl(r9) == readl(r9 + (1 << 11))) {
		r0 = readl(ESDCTL0);
		r0 &= ~ESDCTL0_COL_MASK;
		r0 |= ESDCTL0_COL9;
		writel(r0, ESDCTL0);
	}
}

#define BRANCH_PREDICTION_ENABLE
#define UNALIGNED_ACCESS_ENABLE
#define LOW_INT_LATENCY_ENABLE

void __bare_init __naked board_init_lowlevel(void)
{
	u32 r0, r1;
	void *iomuxc_base = (void *)IMX_IOMUXC_BASE;
	int i;
#ifdef CONFIG_NAND_IMX_BOOT
	unsigned int *trg, *src;
#endif

	r0 = 0x10000000 + 128 * 1024 - 16;
        __asm__ __volatile__("mov sp, %0" : : "r"(r0));

	/*
	 *       ARM1136 init
	 *       - invalidate I/D cache/TLB and drain write buffer;
	 *       - invalidate L2 cache
	 *       - unaligned access
	 *       - branch predictions
	 */
#ifdef TURN_OFF_IMPRECISE_ABORT
	__asm__ __volatile__("mrs %0, cpsr":"=r"(r0));
	r0 &= ~0x100;
	__asm__ __volatile__("msr cpsr, %0" : : "r"(r0));
#endif
	/* ensure L1 caches and MMU are turned-off for now */
	r1 = get_cr();
	r1 &= ~(CR_I | CR_M | CR_C);

	/* setup core features */
	__asm__ __volatile__("mrc p15, 0, r0, c1, c0, 1":"=r"(r0));
#ifdef BRANCH_PREDICTION_ENABLE
	r0 |= 7;
	r1 |= CR_Z;
#else
	r0 &= ~7;
	r1 &= ~CR_Z;
#endif
	__asm__ __volatile__("mcr p15, 0, r0, c1, c0, 1" : : "r"(r0));

#ifdef UNALIGNED_ACCESS_ENABLE
	r1 |= CR_U;
#else
	r1 &= ~CR_U;
#endif

#ifdef LOW_INT_LATENCY_ENABLE
	r1 |= CR_FI;
#else
	r1 &= ~CR_FI;
#endif
	set_cr(r1);

	r0 = 0;
	__asm__ __volatile__("mcr p15, 0, %0, c7, c5, 6" : : "r"(r0));

	/* invalidate I cache and D cache */
	__asm__ __volatile__("mcr p15, 0, r0, c7, c7, 0" : : "r"(r0));
	/* invalidate TLBs */
	__asm__ __volatile__("mcr p15, 0, r0, c8, c7, 0" : : "r"(r0));
	/* Drain the write buffer */
	__asm__ __volatile__("mcr p15, 0, r0, c7, c10, 4" : : "r"(r0));

	/* Also setup the Peripheral Port Remap register inside the core */
	r0 = 0x40000015; /* start from AIPS 2GB region */
	__asm__ __volatile__("mcr p15, 0, r0, c15, c2, 4" : : "r"(r0));

#define	WDOG_WMCR 0x8
	/* silence reset WDOG */
	writew(0, IMX_WDOG_BASE + WDOG_WMCR);

	/* Skip SDRAM initialization if we run from RAM */
	r0 = get_pc();
	if (r0 > 0x80000000 && r0 < 0x90000000)
		board_init_lowlevel_return();

	/* Configure drive strength */

	/* Configure DDR-pins to correct mode */
	r0 = 0x00001800;
	writel(r0, iomuxc_base + 0x794);
	writel(r0, iomuxc_base + 0x798);
	writel(r0, iomuxc_base + 0x79c);
	writel(r0, iomuxc_base + 0x7a0);
	writel(r0, iomuxc_base + 0x7a4);

	/* Set drive strength for DDR-pins */
	for (i = 0x368; i <= 0x4c8; i += 4) {
		r0 = readl(iomuxc_base + i);
		r0 &= ~0x6;
		r0 |= 0x2;
		writel(r0, iomuxc_base + i);
		if (i == 0x468)
			i = 0x4a4;
	}

	r0 = readl(iomuxc_base + 0x480);
	r0 &= ~0x6;
	r0 |= 0x2;
	writel(r0, iomuxc_base + 0x480);

	r0 = readl(iomuxc_base + 0x4b8);
	r0 &= ~0x6;
	r0 |= 0x2;
	writel(r0, iomuxc_base + 0x4b8);

	/* Configure static chip-selects */
	r0 = readl(iomuxc_base + 0x000);
	r0 &= ~1; /* configure CS2/CSD0 for SDRAM */
	writel(r0, iomuxc_base + 0x000);

	/* start-up code doesn't need any static chip-select.
	 * Leave their initialization to high-level code that
	 * can initialize them depending on the baseboard.
	 */

	/* Configure clocks */

	/* setup cpu/bus clocks */
	writel(0x003f4208, IMX_CCM_BASE + CCM_CCMR);

	/* configure MPLL */
	writel(MPCTL_PARAM_532, IMX_CCM_BASE + CCM_MPCTL);

	/* configure PPLL */
	writel(PPCTL_PARAM_300, IMX_CCM_BASE + CCM_PPCTL);

	/* configure core dividers */
	r0 = PDR0_CCM_PER_AHB(1) | PDR0_HSP_PODF(2);

	writel(r0, IMX_CCM_BASE + CCM_PDR0);

	/* configure clock-gates */
	r0 = readl(IMX_CCM_BASE + CCM_CGR0);
	r0 |= 0x00300000;
	writel(r0, IMX_CCM_BASE + CCM_CGR0);

	r0 = readl(IMX_CCM_BASE + CCM_CGR1);
	r0 |= 0x00000c03;
	writel(r0, IMX_CCM_BASE + CCM_CGR1);

	/* Configure SDRAM */
	/* Try 32-Bit 256 MB DDR memory */
	r0 = ESDCTL0_SDE | ESDCTL0_ROW14 | ESDCTL0_COL10 | ESDCTL0_DSIZ_31_0; /* 1024 MBit DDR-SDRAM */
	setup_sdram(r0, ESDMISC_MDDR_EN, 0x80000f00);

#ifdef CONFIG_NAND_IMX_BOOT
	/* skip NAND boot if not running from NFC space */
	r0 = get_pc();
	if (r0 < IMX_NFC_BASE || r0 > IMX_NFC_BASE + 0x800)
		board_init_lowlevel_return();

	src = (unsigned int *)IMX_NFC_BASE;
	trg = (unsigned int *)_text;

	/* Move ourselves out of NFC SRAM */
	for (i = 0; i < 0x800 / sizeof(int); i++)
		*trg++ = *src++;

	/* Jump to SDRAM */
	r0 = (unsigned int)&insdram;
	__asm__ __volatile__("mov pc, %0" : : "r"(r0));
#else
	board_init_lowlevel_return();
#endif
}