summaryrefslogtreecommitdiffstats
path: root/arch/arm/mach-at91/ddramc_ll.c
blob: 001d3d7a22cb5549ee7e10fb982007a40618ce5f (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
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
// SPDX-License-Identifier: BSD-1-Clause
/*
 * Copyright (c) 2007, Stelian Pop <stelian.pop@leadtechdesign.com>
 * Copyright (c) 2007 Lead Tech Design <www.leadtechdesign.com>
 */

#include <linux/kconfig.h>
#include <asm/system.h>
#include <mach/at91/at91_ddrsdrc.h>
#include <mach/at91/ddramc.h>
#include <mach/at91/early_udelay.h>

void at91_ddram_initialize(void __iomem *base_address,
			   void __iomem *ram_address,
			   const struct at91_ddramc_register *ddramc_config)
{
	unsigned long ba_offset;
	unsigned long cr = 0;

	/* compute BA[] offset according to CR configuration */
	ba_offset = (ddramc_config->cr & AT91_DDRC2_NC) + 9;
	if ((ddramc_config->cr & AT91_DDRC2_DECOD) == AT91_DDRC2_DECOD_SEQUENTIAL)
		ba_offset += ((ddramc_config->cr & AT91_DDRC2_NR) >> 2) + 11;

	ba_offset += (ddramc_config->mdr & AT91_DDRC2_DBW) ? 1 : 2;

	/*
	 * Step 1: Program the memory device type into the Memory Device Register
	 */
	writel(ddramc_config->mdr, base_address + AT91_HDDRSDRC2_MDR);

	/*
	 * Step 2: Program the feature of DDR2-SDRAM device into
	 * the Timing Register, and into the Configuration Register
	 */
	writel(ddramc_config->cr, base_address + AT91_HDDRSDRC2_CR);

	writel(ddramc_config->t0pr, base_address + AT91_HDDRSDRC2_T0PR);
	writel(ddramc_config->t1pr, base_address + AT91_HDDRSDRC2_T1PR);
	writel(ddramc_config->t2pr, base_address + AT91_HDDRSDRC2_T2PR);

	/*
	 * Step 3: An NOP command is issued to the DDR2-SDRAM
	 */
	writel(AT91_DDRC2_MODE_NOP_CMD, base_address + AT91_HDDRSDRC2_MR);
	writel(0, ram_address);
	/* Now, clocks which drive the DDR2-SDRAM device are enabled */

	/* A minimum pause wait 200 us is provided to precede any signal toggle.
	   (6 core cycles per iteration, core is at 396MHz: min 13340 loops) */
	early_udelay(200);

	/*
	 * Step 4:  An NOP command is issued to the DDR2-SDRAM
	 */
	writel(AT91_DDRC2_MODE_NOP_CMD, base_address + AT91_HDDRSDRC2_MR);
	writel(0, ram_address);
	/* Now, CKE is driven high */
	/* wait 400 ns min */
	early_udelay(1);

	/*
	 * Step 5: An all banks precharge command is issued to the DDR2-SDRAM.
	 */
	writel(AT91_DDRC2_MODE_PRCGALL_CMD, base_address + AT91_HDDRSDRC2_MR);
	writel(0, ram_address);

	/* wait 2 cycles min (of tCK) = 15 ns min */
	early_udelay(1);

	/*
	 * Step 6: An Extended Mode Register set(EMRS2) cycle is issued to chose between commercial or high
	 * temperature operations.
	 * Perform a write access to DDR2-SDRAM to acknowledge this command.
	 * The write address must be chosen so that BA[1] is set to 1 and BA[0] is set to 0.
	 */
	writel(AT91_DDRC2_MODE_EXT_LMR_CMD, base_address + AT91_HDDRSDRC2_MR);
	writel(0, ram_address + (0x2 << ba_offset));

	/* wait 2 cycles min (of tCK) = 15 ns min */
	early_udelay(1);

	/*
	 * Step 7: An Extended Mode Register set(EMRS3) cycle is issued
	 * to set the Extended Mode Register to "0".
	 * Perform a write access to DDR2-SDRAM to acknowledge this command.
	 * The write address must be chosen so that BA[1] is set to 1 and BA[0] is set to 1.
	 */
	writel(AT91_DDRC2_MODE_EXT_LMR_CMD, base_address + AT91_HDDRSDRC2_MR);
	writel(0, ram_address + (0x3 << ba_offset));

	/* wait 2 cycles min (of tCK) = 15 ns min */
	early_udelay(1);

	/*
	 * Step 8: An Extened Mode Register set(EMRS1) cycle is issued to enable DLL,
	 * and to program D.I.C(Output Driver Impedance Control)
	 * Perform a write access to DDR2-SDRAM to acknowledge this command.
	 * The write address must be chosen so that BA[1] is set to 0 and BA[0] is set to 1.
	 */
	writel(AT91_DDRC2_MODE_EXT_LMR_CMD, base_address + AT91_HDDRSDRC2_MR);
	writel(0, ram_address + (0x1 << ba_offset));

	/* An additional 200 cycles of clock are required for locking DLL */
	early_udelay(1);

	/*
	 * Step 9: Program DLL field into the Configuration Register to high(Enable DLL reset)
	 */
	cr = readl(base_address + AT91_HDDRSDRC2_CR);
	writel(cr | AT91_DDRC2_ENABLE_RESET_DLL, base_address + AT91_HDDRSDRC2_CR);

	/*
	 * Step 10: A Mode Register set(MRS) cycle is issied to reset DLL.
	 * Perform a write access to DDR2-SDRAM to acknowledge this command.
	 * The write address must be chosen so that BA[1:0] bits are set to 0.
	 */
	writel(AT91_DDRC2_MODE_LMR_CMD, base_address + AT91_HDDRSDRC2_MR);
	writel(0, ram_address + (0x0 << ba_offset));

	/* wait 2 cycles min (of tCK) = 15 ns min */
	early_udelay(1);

	/*
	 * Step 11: An all banks precharge command is issued to the DDR2-SDRAM.
	 */
	writel(AT91_DDRC2_MODE_PRCGALL_CMD, base_address + AT91_HDDRSDRC2_MR);
	writel(0, ram_address);

	/* wait 400 ns min (not needed on certain DDR2 devices) */
	early_udelay(1);

	/*
	 * Step 12: Two auto-refresh (CBR) cycles are provided.
	 * Program the auto refresh command (CBR) into the Mode Register.
	 */
	writel(AT91_DDRC2_MODE_RFSH_CMD, base_address + AT91_HDDRSDRC2_MR);
	writel(0, ram_address);

	/* wait TRFC cycles min (135 ns min) extended to 400 ns */
	early_udelay(1);

	/* Set 2nd CBR */
	writel(AT91_DDRC2_MODE_RFSH_CMD, base_address + AT91_HDDRSDRC2_MR);
	writel(0, ram_address);

	/* wait TRFC cycles min (135 ns min) extended to 400 ns */
	early_udelay(1);

	/*
	 * Step 13: Program DLL field into the Configuration Register to low(Disable DLL reset).
	 */
	cr = readl(base_address + AT91_HDDRSDRC2_CR);
	writel(cr & ~AT91_DDRC2_ENABLE_RESET_DLL, base_address + AT91_HDDRSDRC2_CR);

	/*
	 * Step 14: A Mode Register set (MRS) cycle is issued to program
	 * the parameters of the DDR2-SDRAM devices, in particular CAS latency,
	 * burst length and to disable DDL reset.
	 * Perform a write access to DDR2-SDRAM to acknowledge this command.
	 * The write address must be chosen so that BA[1:0] bits are set to 0.
	 */
	writel(AT91_DDRC2_MODE_LMR_CMD, base_address + AT91_HDDRSDRC2_MR);
	writel(0, ram_address + (0x0 << ba_offset));

	/* wait 2 cycles min (of tCK) = 15 ns min */
	early_udelay(1);

	/*
	 * Step 15: Program OCD field into the Configuration Register
	 * to high (OCD calibration default).
	 */
	cr = readl(base_address + AT91_HDDRSDRC2_CR);
	writel(cr | AT91_DDRC2_OCD_DEFAULT, base_address + AT91_HDDRSDRC2_CR);

	/* wait 2 cycles min (of tCK) = 15 ns min */
	early_udelay(1);

	/*
	 * Step 16: An Extended Mode Register set (EMRS1) cycle is issued to OCD default value.
	 * Perform a write access to DDR2-SDRAM to acknowledge this command.
	 * The write address must be chosen so that BA[1] is set to 0 and BA[0] is set to 1.
	 */
	writel(AT91_DDRC2_MODE_EXT_LMR_CMD, base_address + AT91_HDDRSDRC2_MR);
	writel(0, ram_address + (0x1 << ba_offset));

	/* wait 2 cycles min (of tCK) = 15 ns min */
	early_udelay(1);

	/*
	 * Step 17: Program OCD field into the Configuration Register
	 * to low (OCD calibration mode exit).
	 */
	cr = readl(base_address + AT91_HDDRSDRC2_CR);
	writel(cr & ~AT91_DDRC2_OCD_DEFAULT, base_address + AT91_HDDRSDRC2_CR);

	/* wait 2 cycles min (of tCK) = 15 ns min */
	early_udelay(1);

	/*
	 * Step 18: An Extended Mode Register set (EMRS1) cycle is issued to enable OCD exit.
	 * Perform a write access to DDR2-SDRAM to acknowledge this command.
	 * The write address must be chosen so that BA[1] is set to 0 and BA[0] is set to 1.
	 */
	writel(AT91_DDRC2_MODE_EXT_LMR_CMD, base_address + AT91_HDDRSDRC2_MR);
	writel(0, ram_address + (0x1 << ba_offset));

	/* wait 2 cycles min (of tCK) = 15 ns min */
	early_udelay(1);

	/*
	 * Step 19: A Nornal mode command is provided.
	 */
	writel(AT91_DDRC2_MODE_NORMAL_CMD, base_address + AT91_HDDRSDRC2_MR);
	writel(0, ram_address);

	/*
	 * Step 20: Perform a write access to any DDR2-SDRAM address
	 */
	writel(0, ram_address);

	/*
	 * Step 21: Write the refresh rate into the count field in the Refresh Timer register.
	 */
	writel(ddramc_config->rtr, base_address + AT91_HDDRSDRC2_RTR);

	/*
	 * Now we are ready to work on the DDRSDR
	 *  wait for end of calibration
	 */
	early_udelay(10);
}

/* This initialization sequence is sama5d3 and sama5d4 LP-DDR2 specific */

void at91_lpddr2_sdram_initialize(void __iomem *base_address,
				  void __iomem *ram_address,
				  const struct at91_ddramc_register *ddramc_config)
{
	unsigned long reg;

	writel(ddramc_config->lpddr2_lpr, base_address + AT91_MPDDRC_LPDDR2_LPR);

	writel(ddramc_config->tim_calr, base_address + AT91_MPDDRC_LPDDR2_TIM_CAL);

	/*
	 * Step 1: Program the memory device type.
	 */
	writel(ddramc_config->mdr, base_address + AT91_HDDRSDRC2_MDR);

	/*
	 * Step 2: Program the feature of the low-power DDR2-SDRAM device.
	 */
	writel(ddramc_config->cr, base_address + AT91_HDDRSDRC2_CR);

	writel(ddramc_config->t0pr, base_address + AT91_HDDRSDRC2_T0PR);
	writel(ddramc_config->t1pr, base_address + AT91_HDDRSDRC2_T1PR);
	writel(ddramc_config->t2pr, base_address + AT91_HDDRSDRC2_T2PR);

	/*
	 * Step 3: A NOP command is issued to the low-power DDR2-SDRAM.
	 */
	writel(AT91_DDRC2_MODE_NOP_CMD, base_address + AT91_HDDRSDRC2_MR);

	/*
	 * Step 3bis: Add memory barrier then Perform a write access to
	 * any low-power DDR2-SDRAM address to acknowledge the command.
	 */
	dmb();
	writel(0, ram_address);

	/*
	 * Step 4: A pause of at least 100 ns must be observed before
	 * a single toggle.
	 */
	early_udelay(1);

	/*
	 * Step 5: A NOP command is issued to the low-power DDR2-SDRAM.
	 */
	writel(AT91_DDRC2_MODE_NOP_CMD, base_address + AT91_HDDRSDRC2_MR);
	dmb();
	writel(0, ram_address);

	/*
	 * Step 6: A pause of at least 200 us must be observed before a Reset
	 * Command.
	 */
	early_udelay(200);

	/*
	 * Step 7: A Reset command is issued to the low-power DDR2-SDRAM.
	 */
	writel(AT91_DDRC2_MRS(63) | AT91_DDRC2_MODE_LPDDR2_CMD,
		     base_address + AT91_HDDRSDRC2_MR);
	dmb();
	writel(0, ram_address);

	/*
	 * Step 8: A pause of at least tINIT5 must be observed before issuing
	 * any commands.
	 */
	early_udelay(1);

	/*
	 * Step 9: A Calibration command is issued to the low-power DDR2-SDRAM.
	 */
	reg = readl(base_address + AT91_HDDRSDRC2_CR);
	reg &= ~AT91_DDRC2_ZQ;
	reg |= AT91_DDRC2_ZQ_RESET;
	writel(reg, base_address + AT91_HDDRSDRC2_CR);

	writel(AT91_DDRC2_MRS(10) | AT91_DDRC2_MODE_LPDDR2_CMD,
		     base_address + AT91_HDDRSDRC2_MR);
	dmb();
	writel(0, ram_address);

	/*
	 * Step 9bis: The ZQ Calibration command is now issued.
	 * Program the type of calibration in the MPDDRC_CR: set the
	 * ZQ field to the SHORT value.
	 */
	reg = readl(base_address + AT91_HDDRSDRC2_CR);
	reg &= ~AT91_DDRC2_ZQ;
	reg |= AT91_DDRC2_ZQ_SHORT;
	writel(reg, base_address + AT91_HDDRSDRC2_CR);

	/*
	 * Step 10: A Mode Register Write command with 1 to the MRS field
	 * is issued to the low-power DDR2-SDRAM.
	 */
	writel(AT91_DDRC2_MRS(1) | AT91_DDRC2_MODE_LPDDR2_CMD,
		     base_address + AT91_HDDRSDRC2_MR);
	dmb();
	writel(0, ram_address);

	/*
	 * Step 11: A Mode Register Write command with 2 to the MRS field
	 * is issued to the low-power DDR2-SDRAM.
	 */
	writel(AT91_DDRC2_MRS(2) | AT91_DDRC2_MODE_LPDDR2_CMD,
		     base_address + AT91_HDDRSDRC2_MR);
	dmb();
	writel(0, ram_address);

	/*
	 * Step 12: A Mode Register Write command with 3 to the MRS field
	 * is issued to the low-power DDR2-SDRAM.
	 */
	writel(AT91_DDRC2_MRS(3) | AT91_DDRC2_MODE_LPDDR2_CMD,
		     base_address + AT91_HDDRSDRC2_MR);
	dmb();
	writel(0, ram_address);

	/*
	 * Step 13: A Mode Register Write command with 16 to the MRS field
	 * is issued to the low-power DDR2-SDRAM.
	 */
	writel(AT91_DDRC2_MRS(16) | AT91_DDRC2_MODE_LPDDR2_CMD,
		     base_address + AT91_HDDRSDRC2_MR);
	dmb();
	writel(0, ram_address);

	/*
	 * Step 14: A Normal Mode command is provided.
	 */
	writel(AT91_DDRC2_MODE_NORMAL_CMD, base_address + AT91_HDDRSDRC2_MR);
	dmb();
	writel(0, ram_address);

	/*
	 * Step 15: close the input buffers: error in documentation: no need.
	 */

	/*
	 * Step 16: Write the refresh rate into the COUNT field in the MPDDRC
	 * Refresh Timer Register.
	 */
	writel(ddramc_config->rtr, base_address + AT91_HDDRSDRC2_RTR);

	/*
	 * Now configure the CAL MR4 register.
	 */
	writel(ddramc_config->cal_mr4r, base_address + AT91_MPDDRC_LPDDR2_CAL_MR4);
}

void at91_lpddr1_sdram_initialize(void __iomem *base_address,
				  void __iomem *ram_address,
				  const struct at91_ddramc_register *ddramc_config)
{
	unsigned long ba_offset;

	/* Compute BA[] offset according to CR configuration */
	ba_offset = (ddramc_config->cr & AT91_DDRC2_NC) + 8;
	if (!(ddramc_config->cr & AT91_DDRC2_DECOD_INTERLEAVED))
		ba_offset += ((ddramc_config->cr & AT91_DDRC2_NR) >> 2) + 11;

	ba_offset += (ddramc_config->mdr & AT91_DDRC2_DBW) ? 1 : 2;

	/*
	 * Step 1: Program the memory device type in the MPDDRC Memory Device Register
	 */
	writel(ddramc_config->mdr, base_address + AT91_HDDRSDRC2_MDR);

	/*
	 * Step 2: Program the features of the low-power DDR1-SDRAM device
	 * in the MPDDRC Configuration Register and in the MPDDRC Timing
	 * Parameter 0 Register/MPDDRC Timing Parameter 1 Register.
	 */
	writel(ddramc_config->cr, base_address + AT91_HDDRSDRC2_CR);

	writel(ddramc_config->t0pr, base_address + AT91_HDDRSDRC2_T0PR);
	writel(ddramc_config->t1pr, base_address + AT91_HDDRSDRC2_T1PR);
	writel(ddramc_config->t2pr, base_address + AT91_HDDRSDRC2_T2PR);

	/*
	 * Step 3: Program Temperature Compensated Self-refresh (TCR),
	 * Partial Array Self-refresh (PASR) and Drive Strength (DS) parameters
	 * in the MPDDRC Low-power Register.
	 */
	writel(ddramc_config->lpr, base_address + AT91_HDDRSDRC2_LPR);

	/*
	 * Step 4: A NOP command is issued to the low-power DDR1-SDRAM.
	 * Program the NOP command in the MPDDRC Mode Register (MPDDRC_MR).
	 * The clocks which drive the low-power DDR1-SDRAM device
	 * are now enabled.
	 */
	writel(AT91_DDRC2_MODE_NOP_CMD, base_address + AT91_HDDRSDRC2_MR);
	writel(0, ram_address);

	/*
	 * Step 5: A pause of at least 200 us must be observed before
	 * a signal toggle.
	 */
	early_udelay(200);

	/*
	 * Step 6: A NOP command is issued to the low-power DDR1-SDRAM.
	 * Program the NOP command in the MPDDRC_MR. calibration request is
	 * now made to the I/O pad.
	 */
	writel(AT91_DDRC2_MODE_NOP_CMD, base_address + AT91_HDDRSDRC2_MR);
	writel(0, ram_address);

	/*
	 * Step 7: An All Banks Precharge command is issued
	 * to the low-power DDR1-SDRAM.
	 * Program All Banks Precharge command in the MPDDRC_MR.
	 */
	writel(AT91_DDRC2_MODE_PRCGALL_CMD, base_address + AT91_HDDRSDRC2_MR);
	writel(0, ram_address);

	/*
	 * Step 8: Two auto-refresh (CBR) cycles are provided.
	 * Program the Auto Refresh command (CBR) in the MPDDRC_MR.
	 * The application must write a four to the MODE field
	 * in the MPDDRC_MR. Perform a write access to any low-power
	 * DDR1-SDRAM location twice to acknowledge these commands.
	 */
	writel(AT91_DDRC2_MODE_RFSH_CMD, base_address + AT91_HDDRSDRC2_MR);
	writel(0, ram_address);

	writel(AT91_DDRC2_MODE_RFSH_CMD, base_address + AT91_HDDRSDRC2_MR);
	writel(0, ram_address);

	/*
	 * Step 9: An Extended Mode Register Set (EMRS) cycle is issued to
	 * program the low-power DDR1-SDRAM parameters (TCSR, PASR, DS).
	 * The application must write a five to the MODE field in the MPDDRC_MR
	 * and perform a write access to the SDRAM to acknowledge this command.
	 * The write address must be chosen so that signal BA[1] is set to 1
	 * and BA[0] is set to 0.
	 */
	writel(AT91_DDRC2_MODE_EXT_LMR_CMD, base_address + AT91_HDDRSDRC2_MR);
	writel(0, ram_address + (0x2 << ba_offset));

	/*
	 * Step 10: A Mode Register Set (MRS) cycle is issued to program
	 * parameters of the low-power DDR1-SDRAM devices, in particular
	 * CAS latency.
	 * The application must write a three to the MODE field in the MPDDRC_MR
	 * and perform a write access to the SDRAM to acknowledge this command.
	 * The write address must be chosen so that signals BA[1:0] are set to 0.
	 */
	writel(AT91_DDRC2_MODE_LMR_CMD, base_address + AT91_HDDRSDRC2_MR);
	writel(0, ram_address + (0x0 << ba_offset));

	/*
	 * Step 11: The application must enter Normal mode, write a zero
	 * to the MODE field in the MPDDRC_MR and perform a write access
	 * at any location in the SDRAM to acknowledge this command.
	 */
	writel(AT91_DDRC2_MODE_NORMAL_CMD, base_address + AT91_HDDRSDRC2_MR);
	writel(0, ram_address);

	/*
	 * Step 12: Perform a write access to any low-power DDR1-SDRAM address.
	 */
	writel(0, ram_address);

	/*
	 * Step 14: Write the refresh rate into the COUNT field in the MPDDRC
	 * Refresh Timer Register (MPDDRC_RTR):
	 */
	writel(ddramc_config->rtr, base_address + AT91_HDDRSDRC2_RTR);
}