summaryrefslogtreecommitdiff
path: root/board/prodrive/p3mx/sdram_init.c
blob: 4a42fa35462ee622e0ae6d2f533c6d4d7d5a25f6 (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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
/*
 * (C) Copyright 2001
 * Josh Huber <huber@mclx.com>, Mission Critical Linux, Inc.
 *
 * SPDX-License-Identifier:	GPL-2.0+ 
 */

/*************************************************************************
 * adaption for the Marvell DB64460 Board
 * Ingo Assmus (ingo.assmus@keymile.com)
 *************************************************************************/

/* sdram_init.c - automatic memory sizing */

#include <common.h>
#include <74xx_7xx.h>
#include "../../Marvell/include/memory.h"
#include "../../Marvell/include/pci.h"
#include "../../Marvell/include/mv_gen_reg.h"
#include <net.h>

#include "eth.h"
#include "mpsc.h"
#include "../../Marvell/common/i2c.h"
#include "64460.h"
#include "mv_regs.h"

DECLARE_GLOBAL_DATA_PTR;

#undef	DEBUG
#define MAP_PCI

#ifdef DEBUG
#define DP(x) x
#else
#define DP(x)
#endif

int set_dfcdlInit (void);	/* setup delay line of Mv64460 */
int mvDmaIsChannelActive (int);
int mvDmaSetMemorySpace (ulong, ulong, ulong, ulong, ulong);
int mvDmaTransfer (int, ulong, ulong, ulong, ulong);

#define D_CACHE_FLUSH_LINE(addr, offset)				\
	{								\
		__asm__ __volatile__ ("dcbf %0,%1" : : "r" (addr), "r" (offset)); \
	}

int memory_map_bank (unsigned int bankNo,
		     unsigned int bankBase, unsigned int bankLength)
{
#if defined (MAP_PCI) && defined (CONFIG_PCI)
	PCI_HOST host;
#endif

#ifdef DEBUG
	if (bankLength > 0) {
		printf ("mapping bank %d at %08x - %08x\n",
			bankNo, bankBase, bankBase + bankLength - 1);
	} else {
		printf ("unmapping bank %d\n", bankNo);
	}
#endif

	memoryMapBank (bankNo, bankBase, bankLength);

#if defined (MAP_PCI) && defined (CONFIG_PCI)
	for (host = PCI_HOST0; host <= PCI_HOST1; host++) {
		const int features =
			PREFETCH_ENABLE |
			DELAYED_READ_ENABLE |
			AGGRESSIVE_PREFETCH |
			READ_LINE_AGGRESSIVE_PREFETCH |
			READ_MULTI_AGGRESSIVE_PREFETCH |
			MAX_BURST_4 | PCI_NO_SWAP;

		pciMapMemoryBank (host, bankNo, bankBase, bankLength);

		pciSetRegionSnoopMode (host, bankNo, PCI_SNOOP_WB, bankBase,
				       bankLength);

		pciSetRegionFeatures (host, bankNo, features, bankBase,
				      bankLength);
	}
#endif

	return 0;
}

/*
 * Check memory range for valid RAM. A simple memory test determines
 * the actually available RAM size between addresses `base' and
 * `base + maxsize'. Some (not all) hardware errors are detected:
 * - short between address lines
 * - short between data lines
 */
long int dram_size (long int *base, long int maxsize)
{
	volatile long int *addr, *b = base;
	long int cnt, val, save1, save2;

#define STARTVAL (1<<20)	/* start test at 1M */
	for (cnt = STARTVAL / sizeof (long); cnt < maxsize / sizeof (long);
	     cnt <<= 1) {
		addr = base + cnt;	/* pointer arith! */

		save1 = *addr;	/* save contents of addr */
		save2 = *b;	/* save contents of base */

		*addr = cnt;	/* write cnt to addr */
		*b = 0;		/* put null at base */

		/* check at base address */
		if ((*b) != 0) {
			*addr = save1;	/* restore *addr */
			*b = save2;	/* restore *b */
			return (0);
		}
		val = *addr;	/* read *addr */
		val = *addr;	/* read *addr */

		*addr = save1;
		*b = save2;

		if (val != cnt) {
			DP (printf
			    ("Found %08x  at Address %08x (failure)\n",
			     (unsigned int) val, (unsigned int) addr));
			/* fix boundary condition.. STARTVAL means zero */
			if (cnt == STARTVAL / sizeof (long))
				cnt = 0;
			return (cnt * sizeof (long));
		}
	}

	return maxsize;
}

#define SDRAM_NORMAL			0x0
#define SDRAM_PRECHARGE_ALL		0x1
#define SDRAM_REFRESH_ALL		0x2
#define SDRAM_MODE_REG_SETUP		0x3
#define SDRAM_XTEN_MODE_REG_SETUP	0x4
#define SDRAM_NOP			0x5
#define SDRAM_SELF_REFRESH		0x7

phys_size_t initdram (int board_type)
{
	int tmp;
	int start;
	ulong size;
	ulong memSpaceAttr;
	ulong dest;

	/* first disable all banks */
	memory_map_bank(0, 0, 0);
	memory_map_bank(1, 0, 0);
	memory_map_bank(2, 0, 0);
	memory_map_bank(3, 0, 0);

	/* calibrate delay lines */
	set_dfcdlInit();

	GT_REG_WRITE(MV64460_SDRAM_OPERATION, SDRAM_NOP);		/* 0x1418 */
	do {
		tmp = GTREGREAD(MV64460_SDRAM_OPERATION);
	} while(tmp != 0x0);

	/* SDRAM controller configuration */
#ifdef CONFIG_MV64460_ECC
	GT_REG_WRITE(MV64460_SDRAM_CONFIG,		0x58201400);	/* 0x1400 */
#else
	GT_REG_WRITE(MV64460_SDRAM_CONFIG,		0x58200400);	/* 0x1400 */
#endif
	GT_REG_WRITE(MV64460_D_UNIT_CONTROL_LOW,	0xC3000540);	/* 0x1404  */
	GT_REG_WRITE(MV64460_D_UNIT_CONTROL_HIGH,	0x0300F777);	/* 0x1424 */
	GT_REG_WRITE(MV64460_SDRAM_TIMING_CONTROL_LOW,	0x01712220);	/* 0x1408 */
	GT_REG_WRITE(MV64460_SDRAM_TIMING_CONTROL_HIGH, 0x0000005D);	/* 0x140C */
	GT_REG_WRITE(MV64460_SDRAM_ADDR_CONTROL,	0x00000012);	/* 0x1410 */
	GT_REG_WRITE(MV64460_SDRAM_OPEN_PAGES_CONTROL,	0x00000001);	/* 0x1414 */

	/* SDRAM drive strength */
	GT_REG_WRITE(MV64460_SDRAM_ADDR_CTRL_PADS_CALIBRATION, 0x80000000); /* 0x14C0 */
	GT_REG_WRITE(MV64460_SDRAM_ADDR_CTRL_PADS_CALIBRATION, 0x80000008); /* 0x14C0 */
	GT_REG_WRITE(MV64460_SDRAM_DATA_PADS_CALIBRATION, 0x80000000);	    /* 0x14C4 */
	GT_REG_WRITE(MV64460_SDRAM_DATA_PADS_CALIBRATION, 0x80000008);	    /* 0x14C4 */

	/* setup SDRAM device registers */

	/* precharge all */
	GT_REG_WRITE(MV64460_SDRAM_OPERATION, SDRAM_PRECHARGE_ALL);	/* 0x1418 */
	do {
		tmp = GTREGREAD(MV64460_SDRAM_OPERATION);
	} while(tmp != 0x0);

	/* enable DLL */
	GT_REG_WRITE(MV64460_EXTENDED_DRAM_MODE, 0x00000000);			/* 0x1420 */
	GT_REG_WRITE(MV64460_SDRAM_OPERATION, SDRAM_XTEN_MODE_REG_SETUP);	/* 0x1418 */
	do {
		tmp = GTREGREAD(MV64460_SDRAM_OPERATION);
	} while(tmp != 0x0);

	/* reset DLL */
	GT_REG_WRITE(MV64460_SDRAM_MODE, 0x00000132);	/* 0x141C */
	GT_REG_WRITE(MV64460_SDRAM_OPERATION, SDRAM_MODE_REG_SETUP);	/* 0x1418 */
	do {
		tmp = GTREGREAD(MV64460_SDRAM_OPERATION);
	} while(tmp != 0x0);

	/* precharge all */
	GT_REG_WRITE(MV64460_SDRAM_OPERATION, SDRAM_PRECHARGE_ALL);	/* 0x1418 */
	do {
		tmp = GTREGREAD(MV64460_SDRAM_OPERATION);
	} while(tmp != 0x0);

	/* wait for 2 auto refresh commands */
	udelay(20);

	/* un-reset DLL */
	GT_REG_WRITE(MV64460_SDRAM_MODE, 0x00000032);	/* 0x141C */
	GT_REG_WRITE(MV64460_SDRAM_OPERATION, SDRAM_MODE_REG_SETUP);	/* 0x1418 */
	do {
		tmp = GTREGREAD(MV64460_SDRAM_OPERATION);
	} while(tmp != 0x0);

	/* wait 200 cycles */
	udelay(2);  /* FIXME  make this dynamic for the system clock */

	/* SDRAM init done */
	memory_map_bank(0, CONFIG_SYS_SDRAM_BASE,  (256 << 20));
#ifdef CONFIG_SYS_SDRAM1_BASE
	memory_map_bank(1, CONFIG_SYS_SDRAM1_BASE, (256 << 20));
#endif

	/* DUNIT_MMASK: enable SnoopHitEn bit to avoid errata CPU-#4
	 */
	tmp = GTREGREAD(MV64460_D_UNIT_MMASK);				/* 0x14B0 */
	GT_REG_WRITE(MV64460_D_UNIT_MMASK, tmp | 0x2);

	start = (0 << 20);
#ifdef CONFIG_P3M750
	size = (512 << 20);
#elif defined (CONFIG_P3M7448)
	size = (128 << 20);
#endif

#ifdef CONFIG_MV64460_ECC
	memSpaceAttr = ((~(BIT0 << 0)) & 0xf) << 8;
	mvDmaSetMemorySpace (0, 0, memSpaceAttr, start, size);
	for (dest = start; dest < start + size; dest += _8M) {
		mvDmaTransfer (0, start, dest, _8M,
			       BIT8 /*DMA_DTL_128BYTES */  |
			       BIT3 /*DMA_HOLD_SOURCE_ADDR */ |
			       BIT11 /*DMA_BLOCK_TRANSFER_MODE */ );
		while (mvDmaIsChannelActive (0));
	}
#endif

	return (size);
}

void board_add_ram_info(int use_default)
{
	u32 val;

	puts(" (CL=");
	switch ((GTREGREAD(MV64460_SDRAM_MODE) >> 4) & 0x7) {
	case 0x2:
		puts("2");
		break;
	case 0x3:
		puts("3");
		break;
	case 0x5:
		puts("1.5");
		break;
	case 0x6:
		puts("2.5");
		break;
	}

	val = GTREGREAD(MV64460_SDRAM_CONFIG);

	puts(", ECC ");
	if (val & 0x00001000)
		puts("enabled)");
	else
		puts("not enabled)");
}

/*
 * mvDmaIsChannelActive - Check if IDMA channel is active
 *
 * channel	= IDMA channel number from 0 to 7
 */
int mvDmaIsChannelActive (int channel)
{
	ulong data;

	data = GTREGREAD (MV64460_DMA_CHANNEL0_CONTROL + 4 * channel);
	if (data & BIT14)	/* activity status */
		return 1;

	return 0;
}

/*
 * mvDmaSetMemorySpace - Set a DMA memory window for the DMA's address decoding
 *			 map.
 *
 * memSpace	= IDMA memory window number from 0 to 7
 * trg_if	= Target interface:
 *		  0x0 DRAM
 *		  0x1 Device Bus
 *		  0x2 Integrated SDRAM (or CPU bus 60x only)
 *		  0x3 PCI0
 *		  0x4 PCI1
 * attr		= IDMA attributes (see MV datasheet)
 * base_addr	= Sets up memory window for transfers
 *
 */
int mvDmaSetMemorySpace (ulong memSpace,
			 ulong trg_if,
			 ulong attr, ulong base_addr, ulong size)
{
	ulong temp;

	/* The base address must be aligned to the size.  */
	if (base_addr % size != 0)
		return 0;

	if (size >= 0x10000) {	 /* 64K */
		size &= 0xffff0000;
		base_addr = (base_addr & 0xffff0000);
		/* Set the new attributes */
		GT_REG_WRITE (MV64460_DMA_BASE_ADDR_REG0 + memSpace * 8,
			      (base_addr | trg_if | attr));
		GT_REG_WRITE ((MV64460_DMA_SIZE_REG0 + memSpace * 8),
			      (size - 1) & 0xffff0000);
		temp = GTREGREAD (MV64460_DMA_BASE_ADDR_ENABLE_REG);
		GT_REG_WRITE (DMA_BASE_ADDR_ENABLE_REG,
			      (temp & ~(BIT0 << memSpace)));
		return 1;
	}

	return 0;
}

/*
 * mvDmaTransfer - Transfer data from src_addr to dst_addr on one of the 4
 *		   DMA channels.
 *
 * channel	= IDMA channel number from 0 to 3
 * destAddr	= Destination address
 * sourceAddr	= Source address
 * size		= Size in bytes
 * command	= See MV datasheet
 *
 */
int mvDmaTransfer (int channel, ulong sourceAddr,
		   ulong destAddr, ulong size, ulong command)
{
	ulong engOffReg = 0;	/* Engine Offset Register */

	if (size > 0xffff)
		command = command | BIT31;	/* DMA_16M_DESCRIPTOR_MODE */
	command = command | ((command >> 6) & 0x7);
	engOffReg = channel * 4;
	GT_REG_WRITE (MV64460_DMA_CHANNEL0_BYTE_COUNT + engOffReg, size);
	GT_REG_WRITE (MV64460_DMA_CHANNEL0_SOURCE_ADDR + engOffReg, sourceAddr);
	GT_REG_WRITE (MV64460_DMA_CHANNEL0_DESTINATION_ADDR + engOffReg, destAddr);
	command = command |
		BIT12	|			/* DMA_CHANNEL_ENABLE */
		BIT9;				/* DMA_NON_CHAIN_MODE */
	/* Activate DMA channel By writting to mvDmaControlRegister */
	GT_REG_WRITE (MV64460_DMA_CHANNEL0_CONTROL + engOffReg, command);
	return 1;
}

/****************************************************************************************
 *			       SDRAM INIT						*
 *  This procedure detect all Sdram types: 64, 128, 256, 512 Mbit, 1Gbit and 2Gb	*
 *		 This procedure fits only the Atlantis					*
 *											*
 ***************************************************************************************/

/****************************************************************************************
 *			       DFCDL initialize MV643xx Design Considerations		*
 *											*
 ***************************************************************************************/
int set_dfcdlInit (void)
{
	int i;

	/* Values from MV64460 User Manual */
	unsigned int dfcdl_tbl[] = { 0x00000000, 0x00000001, 0x00000042, 0x00000083,
				     0x000000c4, 0x00000105, 0x00000146, 0x00000187,
				     0x000001c8, 0x00000209, 0x0000024a, 0x0000028b,
				     0x000002cc, 0x0000030d, 0x0000034e, 0x0000038f,
				     0x000003d0, 0x00000411, 0x00000452, 0x00000493,
				     0x000004d4, 0x00000515, 0x00000556, 0x00000597,
				     0x000005d8, 0x00000619, 0x0000065a, 0x0000069b,
				     0x000006dc, 0x0000071d, 0x0000075e, 0x0000079f,
				     0x000007e0, 0x00000821, 0x00000862, 0x000008a3,
				     0x000008e4, 0x00000925, 0x00000966, 0x000009a7,
				     0x000009e8, 0x00000a29, 0x00000a6a, 0x00000aab,
				     0x00000aec, 0x00000b2d, 0x00000b6e, 0x00000baf,
				     0x00000bf0, 0x00000c31, 0x00000c72, 0x00000cb3,
				     0x00000cf4, 0x00000d35, 0x00000d76, 0x00000db7,
				     0x00000df8, 0x00000e39, 0x00000e7a, 0x00000ebb,
				     0x00000efc, 0x00000f3d, 0x00000f7e, 0x00000fbf };

	for (i = 0; i < 64; i++)
		GT_REG_WRITE (SRAM_DATA0, dfcdl_tbl[i]);
	GT_REG_WRITE (DFCDL_CONFIG0, 0x00300000);	/* enable dynamic delay line updating */

	return (0);
}