1
2
3
4
5
6
7
8
9
10
11
12
13
14#include <common.h>
15#include <dm.h>
16#include <errno.h>
17#include <ram.h>
18#include <asm/io.h>
19#include <asm/arch/clock.h>
20#include <asm/arch/dram.h>
21#include <asm/arch/sys_proto.h>
22
23DECLARE_GLOBAL_DATA_PTR;
24
25#define DRAM_CLK (CONFIG_DRAM_CLK * 1000000)
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
85struct dram_sun9i_timing {
86 u32 ck;
87 u32 ps;
88};
89
90
91struct dram_sun9i_cl_cwl_timing {
92 u32 CL;
93 u32 CWL;
94 u32 tCKmin;
95 u32 tCKmax;
96};
97
98struct dram_sun9i_para {
99 u32 dram_type;
100
101 u8 bus_width;
102 u8 chan;
103 u8 rank;
104 u8 rows;
105 u16 page_size;
106
107
108 struct dram_sun9i_cl_cwl_timing *cl_cwl_table;
109 u32 cl_cwl_numentries;
110
111
112
113
114
115
116
117 u32 tREFI;
118 u32 tRFC;
119
120 u32 tRAS;
121
122
123 u32 tDLLK;
124 struct dram_sun9i_timing tRTP;
125 struct dram_sun9i_timing tWTR;
126 u32 tWR;
127 u32 tMRD;
128 struct dram_sun9i_timing tMOD;
129 u32 tRCD;
130 u32 tRP;
131 u32 tRC;
132 u32 tCCD;
133 struct dram_sun9i_timing tRRD;
134 u32 tFAW;
135
136
137
138 struct dram_sun9i_timing tZQoper;
139 struct dram_sun9i_timing tZQCS;
140
141
142
143
144
145 struct dram_sun9i_timing tXS;
146 u32 tXSDLL;
147
148 struct dram_sun9i_timing tCKSRE;
149 struct dram_sun9i_timing tCKSRX;
150
151
152 struct dram_sun9i_timing tXP;
153 struct dram_sun9i_timing tXPDLL;
154 struct dram_sun9i_timing tCKE;
155
156
157 u32 tWLMRD;
158
159 u32 tWLO;
160
161
162
163
164};
165
166static void mctl_sys_init(void);
167
168#define SCHED_RDWR_IDLE_GAP(n) ((n & 0xff) << 24)
169#define SCHED_GO2CRITICAL_HYSTERESIS(n) ((n & 0xff) << 16)
170#define SCHED_LPR_NUM_ENTRIES(n) ((n & 0xff) << 8)
171#define SCHED_PAGECLOSE (1 << 2)
172#define SCHED_PREFER_WRITE (1 << 1)
173#define SCHED_FORCE_LOW_PRI_N (1 << 0)
174
175#define SCHED_CONFIG (SCHED_RDWR_IDLE_GAP(0xf) | \
176 SCHED_GO2CRITICAL_HYSTERESIS(0x80) | \
177 SCHED_LPR_NUM_ENTRIES(0x20) | \
178 SCHED_FORCE_LOW_PRI_N)
179#define PERFHPR0_CONFIG 0x0000001f
180#define PERFHPR1_CONFIG 0x1f00001f
181#define PERFLPR0_CONFIG 0x000000ff
182#define PERFLPR1_CONFIG 0x0f0000ff
183#define PERFWR0_CONFIG 0x000000ff
184#define PERFWR1_CONFIG 0x0f0001ff
185
186static void mctl_ctl_sched_init(unsigned long base)
187{
188 struct sunxi_mctl_ctl_reg *mctl_ctl =
189 (struct sunxi_mctl_ctl_reg *)base;
190
191
192 writel(SCHED_CONFIG, &mctl_ctl->sched);
193 writel(PERFHPR0_CONFIG, &mctl_ctl->perfhpr0);
194 writel(PERFHPR1_CONFIG, &mctl_ctl->perfhpr1);
195 writel(PERFLPR0_CONFIG, &mctl_ctl->perflpr0);
196 writel(PERFLPR1_CONFIG, &mctl_ctl->perflpr1);
197 writel(PERFWR0_CONFIG, &mctl_ctl->perfwr0);
198 writel(PERFWR1_CONFIG, &mctl_ctl->perfwr1);
199}
200
201static void mctl_sys_init(void)
202{
203 struct sunxi_ccm_reg * const ccm =
204 (struct sunxi_ccm_reg *)SUNXI_CCM_BASE;
205 struct sunxi_mctl_com_reg * const mctl_com =
206 (struct sunxi_mctl_com_reg *)SUNXI_DRAM_COM_BASE;
207
208 debug("Setting PLL6 to %d\n", DRAM_CLK * 2);
209 clock_set_pll6(DRAM_CLK * 2);
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 clrbits_le32(&ccm->ahb_reset0_cfg, 1 << AHB_RESET_OFFSET_MCTL);
277
278 clrbits_le32(&ccm->ahb_gate0, 1 << AHB_GATE_OFFSET_MCTL);
279
280 sdelay(2000);
281
282
283 setbits_le32(&ccm->ahb_reset0_cfg, 1 << AHB_RESET_OFFSET_MCTL);
284
285 setbits_le32(&ccm->ahb_gate0, 1 << AHB_GATE_OFFSET_MCTL);
286
287
288 mctl_ctl_sched_init(SUNXI_DRAM_CTL0_BASE);
289 mctl_ctl_sched_init(SUNXI_DRAM_CTL1_BASE);
290 sdelay(1000);
291
292 debug("2\n");
293
294
295 writel((3 << 12) | (1 << 16), &ccm->dram_clk_cfg);
296 do {
297 debug("Waiting for DRAM_CLK_CFG\n");
298 sdelay(10000);
299 } while (readl(&ccm->dram_clk_cfg) & (1 << 16));
300 setbits_le32(&ccm->dram_clk_cfg, (1 << 31));
301
302
303 setbits_le32(&mctl_com->ccr, (1 << 14) | (1 << 30));
304 writel(2, &mctl_com->rmcr);
305
306 sdelay(2000);
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 clrbits_le32(&mctl_com->ccr, MCTL_CCR_CH0_CLK_EN | MCTL_CCR_CH1_CLK_EN);
334 sdelay(1000);
335
336 setbits_le32(&mctl_com->ccr, MCTL_CCR_CH0_CLK_EN);
337
338 setbits_le32(&mctl_com->ccr, MCTL_CCR_CH1_CLK_EN);
339}
340
341static void mctl_com_init(struct dram_sun9i_para *para)
342{
343 struct sunxi_mctl_com_reg * const mctl_com =
344 (struct sunxi_mctl_com_reg *)SUNXI_DRAM_COM_BASE;
345
346
347 writel(((para->chan == 2) ? MCTL_CR_CHANNEL_DUAL :
348 MCTL_CR_CHANNEL_SINGLE)
349 | MCTL_CR_DRAMTYPE_DDR3 | MCTL_CR_BANK(1)
350 | MCTL_CR_ROW(para->rows)
351 | ((para->bus_width == 32) ? MCTL_CR_BUSW32 : MCTL_CR_BUSW16)
352 | MCTL_CR_PAGE_SIZE(para->page_size) | MCTL_CR_RANK(para->rank),
353 &mctl_com->cr);
354
355 debug("CR: %d\n", readl(&mctl_com->cr));
356}
357
358static u32 mctl_channel_init(u32 ch_index, struct dram_sun9i_para *para)
359{
360 struct sunxi_mctl_ctl_reg *mctl_ctl;
361 struct sunxi_mctl_phy_reg *mctl_phy;
362
363 u32 CL = 0;
364 u32 CWL = 0;
365 u16 mr[4] = { 0, };
366
367#define PS2CYCLES_FLOOR(n) ((n * CONFIG_DRAM_CLK) / 1000000)
368#define PS2CYCLES_ROUNDUP(n) ((n * CONFIG_DRAM_CLK + 999999) / 1000000)
369#define NS2CYCLES_FLOOR(n) ((n * CONFIG_DRAM_CLK) / 1000)
370#define NS2CYCLES_ROUNDUP(n) ((n * CONFIG_DRAM_CLK + 999) / 1000)
371#define MAX(a, b) ((a) > (b) ? (a) : (b))
372
373
374
375
376
377
378 const u32 tREFI = NS2CYCLES_FLOOR(para->tREFI);
379 const u32 tRFC = NS2CYCLES_ROUNDUP(para->tRFC);
380 const u32 tRCD = PS2CYCLES_ROUNDUP(para->tRCD);
381 const u32 tRP = PS2CYCLES_ROUNDUP(para->tRP);
382 const u32 tRC = PS2CYCLES_ROUNDUP(para->tRC);
383 const u32 tRAS = PS2CYCLES_ROUNDUP(para->tRAS);
384
385
386 const u32 tDLLK = para->tDLLK;
387 const u32 tRTP = MAX(para->tRTP.ck, PS2CYCLES_ROUNDUP(para->tRTP.ps));
388 const u32 tWTR = MAX(para->tWTR.ck, PS2CYCLES_ROUNDUP(para->tWTR.ps));
389 const u32 tWR = NS2CYCLES_FLOOR(para->tWR);
390 const u32 tMRD = para->tMRD;
391 const u32 tMOD = MAX(para->tMOD.ck, PS2CYCLES_ROUNDUP(para->tMOD.ps));
392 const u32 tCCD = para->tCCD;
393 const u32 tRRD = MAX(para->tRRD.ck, PS2CYCLES_ROUNDUP(para->tRRD.ps));
394 const u32 tFAW = PS2CYCLES_ROUNDUP(para->tFAW);
395
396
397
398
399 const u32 tZQoper = MAX(para->tZQoper.ck,
400 PS2CYCLES_ROUNDUP(para->tZQoper.ps));
401 const u32 tZQCS = MAX(para->tZQCS.ck,
402 PS2CYCLES_ROUNDUP(para->tZQCS.ps));
403
404
405
406
407
408
409 const u32 tXP = MAX(para->tXP.ck, PS2CYCLES_ROUNDUP(para->tXP.ps));
410 const u32 tXPDLL = MAX(para->tXPDLL.ck,
411 PS2CYCLES_ROUNDUP(para->tXPDLL.ps));
412 const u32 tCKE = MAX(para->tCKE.ck, PS2CYCLES_ROUNDUP(para->tCKE.ps));
413
414
415
416
417
418 const u32 tXS = MAX(para->tXS.ck, PS2CYCLES_ROUNDUP(para->tXS.ps));
419 const u32 tXSDLL = para->tXSDLL;
420 const u32 tCKSRE = MAX(para->tCKSRE.ck,
421 PS2CYCLES_ROUNDUP(para->tCKSRE.ps));
422 const u32 tCKESR = tCKE + 1;
423 const u32 tCKSRX = MAX(para->tCKSRX.ck,
424 PS2CYCLES_ROUNDUP(para->tCKSRX.ps));
425
426
427 const u32 tWLMRD = para->tWLMRD;
428
429 const u32 tWLO = PS2CYCLES_FLOOR(para->tWLO);
430
431
432 const u32 tRASmax = tREFI * 9;
433 int i;
434
435 for (i = 0; i < para->cl_cwl_numentries; ++i) {
436 const u32 tCK = 1000000 / CONFIG_DRAM_CLK;
437
438 if ((para->cl_cwl_table[i].tCKmin <= tCK) &&
439 (tCK < para->cl_cwl_table[i].tCKmax)) {
440 CL = para->cl_cwl_table[i].CL;
441 CWL = para->cl_cwl_table[i].CWL;
442
443 debug("found CL/CWL: CL = %d, CWL = %d\n", CL, CWL);
444 break;
445 }
446 }
447
448 if ((CL == 0) && (CWL == 0)) {
449 printf("failed to find valid CL/CWL for operating point %d MHz\n",
450 CONFIG_DRAM_CLK);
451 return 0;
452 }
453
454 if (ch_index == 0) {
455 mctl_ctl = (struct sunxi_mctl_ctl_reg *)SUNXI_DRAM_CTL0_BASE;
456 mctl_phy = (struct sunxi_mctl_phy_reg *)SUNXI_DRAM_PHY0_BASE;
457 } else {
458 mctl_ctl = (struct sunxi_mctl_ctl_reg *)SUNXI_DRAM_CTL1_BASE;
459 mctl_phy = (struct sunxi_mctl_phy_reg *)SUNXI_DRAM_PHY1_BASE;
460 }
461
462 if (para->dram_type == DRAM_TYPE_DDR3) {
463 mr[0] = DDR3_MR0_PPD_FAST_EXIT | DDR3_MR0_WR(tWR) |
464 DDR3_MR0_CL(CL);
465 mr[1] = DDR3_MR1_RTT120OHM;
466 mr[2] = DDR3_MR2_TWL(CWL);
467 mr[3] = 0;
468
469
470
471
472
473
474
475
476
477
478
479 writel(MCTL_INIT0_POST_CKE_x1024(1) |
480 MCTL_INIT0_PRE_CKE_x1024(
481 (500 * CONFIG_DRAM_CLK + 1023) / 1024),
482 &mctl_ctl->init[0]);
483 writel(MCTL_INIT1_DRAM_RSTN_x1024(1),
484 &mctl_ctl->init[1]);
485
486 writel(MCTL_INIT3_MR(mr[0]) | MCTL_INIT3_EMR(mr[1]),
487 &mctl_ctl->init[3]);
488 writel(MCTL_INIT4_EMR2(mr[2]) | MCTL_INIT4_EMR3(mr[3]),
489 &mctl_ctl->init[4]);
490 writel(MCTL_INIT5_DEV_ZQINIT_x32(512 / 32),
491 &mctl_ctl->init[5]);
492 } else {
493
494
495
496
497
498 writel(MCTL_INIT0_POST_CKE_x1024(
499 (200 * CONFIG_DRAM_CLK + 1023) / 1024),
500 &mctl_ctl->init[0]);
501 writel(MCTL_INIT1_DRAM_RSTN_x1024(1),
502 &mctl_ctl->init[1]);
503 writel(MCTL_INIT2_IDLE_AFTER_RESET_x32(
504 (CONFIG_DRAM_CLK + 31) / 32)
505 | MCTL_INIT2_MIN_STABLE_CLOCK_x1(5),
506 &mctl_ctl->init[2]);
507 writel(MCTL_INIT3_MR(mr[1]) | MCTL_INIT3_EMR(mr[2]),
508 &mctl_ctl->init[3]);
509 writel(MCTL_INIT4_EMR2(mr[3]),
510 &mctl_ctl->init[4]);
511 writel(MCTL_INIT5_DEV_ZQINIT_x32(
512 (CONFIG_DRAM_CLK + 31) / 32)
513 | MCTL_INIT5_MAX_AUTO_INIT_x1024(
514 (10 * CONFIG_DRAM_CLK + 1023) / 1024),
515 &mctl_ctl->init[5]);
516 }
517
518
519#define MCTL_BL 8
520
521#define WR2PRE (MCTL_BL/2 + CWL + tWTR)
522
523#define WR2RD (MCTL_BL/2 + CWL + tWTR)
524
525
526
527
528#define RD2WR (CL + MCTL_BL/2 + 2 - CWL)
529#define MCTL_PHY_TRTW 0
530#define MCTL_PHY_TRTODT 0
531
532#define MCTL_DIV2(n) ((n + 1)/2)
533#define MCTL_DIV32(n) (n/32)
534#define MCTL_DIV1024(n) (n/1024)
535
536 writel((MCTL_DIV2(WR2PRE) << 24) | (MCTL_DIV2(tFAW) << 16) |
537 (MCTL_DIV1024(tRASmax) << 8) | (MCTL_DIV2(tRAS) << 0),
538 &mctl_ctl->dramtmg[0]);
539 writel((MCTL_DIV2(tXP) << 16) | (MCTL_DIV2(tRTP) << 8) |
540 (MCTL_DIV2(tRC) << 0),
541 &mctl_ctl->dramtmg[1]);
542 writel((MCTL_DIV2(CWL) << 24) | (MCTL_DIV2(CL) << 16) |
543 (MCTL_DIV2(RD2WR) << 8) | (MCTL_DIV2(WR2RD) << 0),
544 &mctl_ctl->dramtmg[2]);
545
546
547
548
549 writel((MCTL_DIV2(tMRD) << 12) | (MCTL_DIV2(tMOD) << 0),
550 &mctl_ctl->dramtmg[3]);
551 writel((MCTL_DIV2(tRCD) << 24) | (MCTL_DIV2(tCCD) << 16) |
552 (MCTL_DIV2(tRRD) << 8) | (MCTL_DIV2(tRP) << 0),
553 &mctl_ctl->dramtmg[4]);
554 writel((MCTL_DIV2(tCKSRX) << 24) | (MCTL_DIV2(tCKSRE) << 16) |
555 (MCTL_DIV2(tCKESR) << 8) | (MCTL_DIV2(tCKE) << 0),
556 &mctl_ctl->dramtmg[5]);
557
558
559
560
561
562
563
564
565
566
567
568
569
570 writel((MCTL_DIV32(tXSDLL) << 0), &mctl_ctl->dramtmg[8]);
571
572 writel((MCTL_DIV32(tREFI) << 16) | (MCTL_DIV2(tRFC) << 0),
573 &mctl_ctl->rfshtmg);
574
575 if (para->dram_type == DRAM_TYPE_DDR3) {
576 writel((2 << 24) | ((MCTL_DIV2(CL) - 2) << 16) |
577 (1 << 8) | ((MCTL_DIV2(CWL) - 2) << 0),
578 &mctl_ctl->dfitmg[0]);
579 } else {
580
581 }
582
583
584
585
586
587
588
589 clrbits_le32(&mctl_ctl->dfimisc, MCTL_DFIMISC_DFI_INIT_COMPLETE_EN);
590
591 setbits_le32(&mctl_ctl->dfiupd[0], MCTL_DFIUPD0_DIS_AUTO_CTRLUPD);
592
593
594
595 writel(MCTL_MSTR_DEVICETYPE(para->dram_type) |
596 MCTL_MSTR_BURSTLENGTH(para->dram_type) |
597 MCTL_MSTR_ACTIVERANKS(para->rank) |
598 MCTL_MSTR_2TMODE | MCTL_MSTR_BUSWIDTH32,
599 &mctl_ctl->mstr);
600
601 if (para->dram_type == DRAM_TYPE_DDR3) {
602 writel(MCTL_ZQCTRL0_TZQCL(MCTL_DIV2(tZQoper)) |
603 (MCTL_DIV2(tZQCS)), &mctl_ctl->zqctrl[0]);
604
605
606
607
608
609 writel(MCTL_ZQCTRL1_TZQSI_x1024(0x100),
610 &mctl_ctl->zqctrl[1]);
611 } else {
612 writel(MCTL_ZQCTRL0_TZQCL(0x200) | MCTL_ZQCTRL0_TZQCS(0x40),
613 &mctl_ctl->zqctrl[0]);
614 writel(MCTL_ZQCTRL1_TZQRESET(0x28) |
615 MCTL_ZQCTRL1_TZQSI_x1024(0x100),
616 &mctl_ctl->zqctrl[1]);
617 }
618
619
620 setbits_le32(&mctl_ctl->dfimisc, MCTL_DFIMISC_DFI_INIT_COMPLETE_EN);
621
622 setbits_le32(&mctl_ctl->rfshctl3, MCTL_RFSHCTL3_DIS_AUTO_REFRESH);
623
624
625
626
627 writel(MCTL_PHY_DCR_BYTEMASK | MCTL_PHY_DCR_2TMODE |
628 MCTL_PHY_DCR_DDR8BNK | MCTL_PHY_DRAMMODE_DDR3,
629 &mctl_phy->dcr);
630
631
632 if (para->dram_type != DRAM_TYPE_DDR3)
633 clrbits_le32(&mctl_phy->dsgcr, (3 << 6));
634
635 writel(mr[0], &mctl_phy->mr0);
636 writel(mr[1], &mctl_phy->mr1);
637 writel(mr[2], &mctl_phy->mr2);
638 writel(mr[3], &mctl_phy->mr3);
639
640
641
642
643
644 writel((tRC << 26) | (tRRD << 22) | (tRAS << 16) |
645 (tRCD << 12) | (tRP << 8) | (tWTR << 4) | (tRTP << 0),
646 &mctl_phy->dtpr[0]);
647 writel((tMRD << 0) | ((tMOD - 12) << 2) | (tFAW << 5) |
648 (tRFC << 11) | (tWLMRD << 20) | (tWLO << 26),
649 &mctl_phy->dtpr[1]);
650 writel((tXS << 0) | (MAX(tXP, tXPDLL) << 10) |
651 (tCKE << 15) | (tDLLK << 19) |
652 (MCTL_PHY_TRTODT << 29) | (MCTL_PHY_TRTW << 30) |
653 (((tCCD - 4) & 0x1) << 31),
654 &mctl_phy->dtpr[2]);
655
656
657
658
659
660
661
662
663
664 writel(0x42C21590, &mctl_phy->ptr[0]);
665 writel(0xD05612C0, &mctl_phy->ptr[1]);
666 if (para->dram_type == DRAM_TYPE_DDR3) {
667 const unsigned int tdinit0 = 500 * CONFIG_DRAM_CLK;
668 const unsigned int tdinit1 = (360 * CONFIG_DRAM_CLK + 999) /
669 1000;
670 const unsigned int tdinit2 = 200 * CONFIG_DRAM_CLK;
671 const unsigned int tdinit3 = CONFIG_DRAM_CLK;
672
673 writel((tdinit1 << 20) | tdinit0, &mctl_phy->ptr[3]);
674 writel((tdinit3 << 18) | tdinit2, &mctl_phy->ptr[4]);
675 } else {
676
677 const unsigned int tdinit0 = (100 * CONFIG_DRAM_CLK + 999) /
678 1000;
679 const unsigned int tdinit1 = 200 * CONFIG_DRAM_CLK;
680 const unsigned int tdinit2 = 22 * CONFIG_DRAM_CLK;
681 const unsigned int tdinit3 = 2 * CONFIG_DRAM_CLK;
682
683 writel((tdinit1 << 20) | tdinit0, &mctl_phy->ptr[3]);
684 writel((tdinit3 << 18) | tdinit2, &mctl_phy->ptr[4]);
685 }
686
687
688 writel(0x00203131, &mctl_phy->acmdlr);
689
690
691 writel(MCTL_DTCR_DEFAULT | MCTL_DTCR_RANKEN(para->rank),
692 &mctl_phy->dtcr);
693
694
695 debug("DX2GCR0 reset: 0x%x\n", readl(&mctl_phy->dx[2].gcr[0]));
696 writel(0x7C000285, &mctl_phy->dx[2].gcr[0]);
697 writel(0x7C000285, &mctl_phy->dx[3].gcr[0]);
698
699 clrsetbits_le32(&mctl_phy->zq[0].pr, 0xff,
700 (CONFIG_DRAM_ZQ >> 0) & 0xff);
701 clrsetbits_le32(&mctl_phy->zq[1].pr, 0xff,
702 (CONFIG_DRAM_ZQ >> 8) & 0xff);
703 clrsetbits_le32(&mctl_phy->zq[2].pr, 0xff,
704 (CONFIG_DRAM_ZQ >> 16) & 0xff);
705
706
707 if (1) {
708 int lane;
709 for (lane = 0; lane < 4; ++lane) {
710 clrbits_le32(&mctl_phy->dx[lane].gcr[2], 0xffff);
711 clrbits_le32(&mctl_phy->dx[lane].gcr[3],
712 (0x3<<12) | (0x3<<4));
713 }
714 } else {
715
716 int lane;
717 for (lane = 0; lane < 4; ++lane) {
718 clrsetbits_le32(&mctl_phy->dx[lane].gcr[2], 0xffff,
719 0xaaaa);
720 if (para->dram_type == DRAM_TYPE_DDR3)
721 setbits_le32(&mctl_phy->dx[lane].gcr[3],
722 (0x3<<12) | (0x3<<4));
723 else
724 setbits_le32(&mctl_phy->dx[lane].gcr[3],
725 0x00000012);
726 }
727 }
728
729 writel(0x04058D02, &mctl_phy->zq[0].cr);
730 writel(0x04058D02, &mctl_phy->zq[1].cr);
731 writel(0x04058D02, &mctl_phy->zq[2].cr);
732
733
734 setbits_le32(&mctl_ctl->rfshctl3, MCTL_RFSHCTL3_DIS_AUTO_REFRESH);
735
736 setbits_le32(&mctl_phy->dsgcr, 0xf << 24);
737
738 clrsetbits_le32(&mctl_phy->pgcr[1],
739 MCTL_PGCR1_ZCKSEL_MASK,
740 MCTL_PGCR1_IODDRM_DDR3 | MCTL_PGCR1_INHVT_EN);
741
742 setbits_le32(&mctl_phy->pllcr, 0x3 << 19);
743
744 setbits_le32(&mctl_phy->pllcr,
745 MCTL_PLLGCR_PLL_BYPASS | MCTL_PLLGCR_PLL_POWERDOWN);
746
747
748
749 clrbits_le32(&mctl_phy->pgcr[0], 0x3f);
750
751
752 if (para->dram_type == DRAM_TYPE_DDR3)
753 clrsetbits_le32(&mctl_phy->pir, MCTL_PIR_MASK, 0x20df3);
754 else
755 clrsetbits_le32(&mctl_phy->pir, MCTL_PIR_MASK, 0x2c573);
756
757 sdelay(10000);
758
759
760 while ((readl(&mctl_phy->pir) & MCTL_PIR_INIT) != MCTL_PIR_INIT) {
761
762 debug("MCTL_PIR_INIT not set\n");
763 sdelay(1000);
764
765 }
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795 if (readl(&mctl_phy->pgsr[0]) & MCTL_PGSR0_ERRORS) {
796 debug("Channel %d unavailable!\n", ch_index);
797 return 0;
798 } else{
799
800 debug("Channel %d OK!\n", ch_index);
801
802 }
803
804 while ((readl(&mctl_ctl->stat) & 0x1) != 0x1) {
805 debug("Waiting for INIT to be done (controller to come up into 'normal operating' mode\n");
806 sdelay(100000);
807
808
809 }
810 debug("done\n");
811
812
813 clrbits_le32(&mctl_phy->pgcr[3], (1 << 25));
814
815
816 debug("DFIMISC before writing 0: 0x%x\n", readl(&mctl_ctl->dfimisc));
817 writel(0, &mctl_ctl->dfimisc);
818
819
820 clrbits_le32(&mctl_ctl->rfshctl3, MCTL_RFSHCTL3_DIS_AUTO_REFRESH);
821
822 debug("channel_init complete\n");
823 return 1;
824}
825
826signed int DRAMC_get_dram_size(void)
827{
828 struct sunxi_mctl_com_reg * const mctl_com =
829 (struct sunxi_mctl_com_reg *)SUNXI_DRAM_COM_BASE;
830
831 unsigned int reg_val;
832 unsigned int dram_size;
833 unsigned int temp;
834
835 reg_val = readl(&mctl_com->cr);
836
837 temp = (reg_val >> 8) & 0xf;
838 dram_size = (temp - 6);
839
840 temp = (reg_val >> 4) & 0xf;
841 dram_size += (temp + 1);
842
843 temp = (reg_val >> 2) & 0x3;
844 dram_size += (temp + 2);
845
846 temp = reg_val & 0x3;
847 dram_size += temp;
848
849 temp = (reg_val >> 19) & 0x1;
850 dram_size += temp;
851
852 dram_size = dram_size - 11;
853
854 return 1 << dram_size;
855}
856
857unsigned long sunxi_dram_init(void)
858{
859 struct sunxi_mctl_com_reg * const mctl_com =
860 (struct sunxi_mctl_com_reg *)SUNXI_DRAM_COM_BASE;
861
862 struct dram_sun9i_cl_cwl_timing cl_cwl[] = {
863 { .CL = 5, .CWL = 5, .tCKmin = 3000, .tCKmax = 3300 },
864 { .CL = 6, .CWL = 5, .tCKmin = 2500, .tCKmax = 3300 },
865 { .CL = 8, .CWL = 6, .tCKmin = 1875, .tCKmax = 2500 },
866 { .CL = 10, .CWL = 7, .tCKmin = 1500, .tCKmax = 1875 },
867 { .CL = 11, .CWL = 8, .tCKmin = 1250, .tCKmax = 1500 }
868 };
869
870
871 struct dram_sun9i_para para = {
872 .dram_type = DRAM_TYPE_DDR3,
873 .bus_width = 32,
874 .chan = 2,
875 .rank = 1,
876
877 .page_size = 4096,
878
879 .rows = 15,
880
881
882 .cl_cwl_table = cl_cwl,
883 .cl_cwl_numentries = sizeof(cl_cwl) /
884 sizeof(struct dram_sun9i_cl_cwl_timing),
885
886
887 .tREFI = 7800,
888 .tRFC = 260,
889
890
891 .tRCD = 13750,
892 .tRP = 13750,
893 .tRC = 48750,
894 .tRAS = 35000,
895
896 .tDLLK = 512,
897 .tRTP = { .ck = 4, .ps = 7500 },
898 .tWTR = { .ck = 4, .ps = 7500 },
899 .tWR = 15,
900 .tMRD = 4,
901 .tMOD = { .ck = 12, .ps = 15000 },
902 .tCCD = 4,
903 .tRRD = { .ck = 4, .ps = 7500 },
904 .tFAW = 40,
905
906
907
908 .tZQoper = { .ck = 256, .ps = 320000 },
909 .tZQCS = { .ck = 64, .ps = 80000 },
910
911
912
913
914
915 .tXS = { .ck = 5, .ps = 10000 },
916 .tXSDLL = 512,
917 .tCKSRE = { .ck = 5, .ps = 10000 },
918 .tCKSRX = { .ck = 5, .ps = 10000 },
919
920
921 .tXP = { .ck = 3, .ps = 6000 },
922 .tXPDLL = { .ck = 10, .ps = 24000 },
923 .tCKE = { .ck = 3, .ps = 5000 },
924
925
926 .tWLMRD = 40,
927
928 .tWLO = 7500,
929
930 };
931
932
933
934
935
936
937
938
939
940 setbits_le32(SUNXI_PRCM_BASE + 0x1e0, (0x3 << 8));
941 writel(0, SUNXI_PRCM_BASE + 0x1e8);
942
943 mctl_sys_init();
944
945 if (!mctl_channel_init(0, ¶))
946 return 0;
947
948
949 if (!mctl_channel_init(1, ¶)) {
950
951 clrsetbits_le32(&mctl_com->cr, MCTL_CR_CHANNEL_MASK,
952 MCTL_CR_CHANNEL_SINGLE);
953
954 clrbits_le32(&mctl_com->cr, MCTL_CCR_CH1_CLK_EN);
955 }
956
957 mctl_com_init(¶);
958
959
960 return DRAMC_get_dram_size() << 20;
961}
962