1*4882a593Smuzhiyun #include <common.h>
2*4882a593Smuzhiyun #include <asm/arch/dram.h>
3*4882a593Smuzhiyun #include <asm/arch/cpu.h>
4*4882a593Smuzhiyun
mctl_set_timing_params(uint16_t socid,struct dram_para * para)5*4882a593Smuzhiyun void mctl_set_timing_params(uint16_t socid, struct dram_para *para)
6*4882a593Smuzhiyun {
7*4882a593Smuzhiyun struct sunxi_mctl_ctl_reg * const mctl_ctl =
8*4882a593Smuzhiyun (struct sunxi_mctl_ctl_reg *)SUNXI_DRAM_CTL0_BASE;
9*4882a593Smuzhiyun
10*4882a593Smuzhiyun u8 tccd = 2;
11*4882a593Smuzhiyun u8 tfaw = max(ns_to_t(50), 4);
12*4882a593Smuzhiyun u8 trrd = max(ns_to_t(10), 2);
13*4882a593Smuzhiyun u8 trcd = max(ns_to_t(24), 2);
14*4882a593Smuzhiyun u8 trc = ns_to_t(70);
15*4882a593Smuzhiyun u8 txp = max(ns_to_t(8), 2);
16*4882a593Smuzhiyun u8 twtr = max(ns_to_t(8), 2);
17*4882a593Smuzhiyun u8 trtp = max(ns_to_t(8), 2);
18*4882a593Smuzhiyun u8 twr = max(ns_to_t(15), 3);
19*4882a593Smuzhiyun u8 trp = max(ns_to_t(27), 2);
20*4882a593Smuzhiyun u8 tras = ns_to_t(42);
21*4882a593Smuzhiyun u16 trefi = ns_to_t(3900) / 32;
22*4882a593Smuzhiyun u16 trfc = ns_to_t(210);
23*4882a593Smuzhiyun
24*4882a593Smuzhiyun u8 tmrw = 5;
25*4882a593Smuzhiyun u8 tmrd = 5;
26*4882a593Smuzhiyun u8 tmod = 12;
27*4882a593Smuzhiyun u8 tcke = 3;
28*4882a593Smuzhiyun u8 tcksrx = 5;
29*4882a593Smuzhiyun u8 tcksre = 5;
30*4882a593Smuzhiyun u8 tckesr = 5;
31*4882a593Smuzhiyun u8 trasmax = 24;
32*4882a593Smuzhiyun
33*4882a593Smuzhiyun u8 tcl = 6; /* CL 12 */
34*4882a593Smuzhiyun u8 tcwl = 3; /* CWL 6 */
35*4882a593Smuzhiyun u8 t_rdata_en = 5;
36*4882a593Smuzhiyun u8 wr_latency = 2;
37*4882a593Smuzhiyun
38*4882a593Smuzhiyun u32 tdinit0 = (200 * CONFIG_DRAM_CLK) + 1; /* 200us */
39*4882a593Smuzhiyun u32 tdinit1 = (100 * CONFIG_DRAM_CLK) / 1000 + 1; /* 100ns */
40*4882a593Smuzhiyun u32 tdinit2 = (11 * CONFIG_DRAM_CLK) + 1; /* 11us */
41*4882a593Smuzhiyun u32 tdinit3 = (1 * CONFIG_DRAM_CLK) + 1; /* 1us */
42*4882a593Smuzhiyun
43*4882a593Smuzhiyun u8 twtp = tcwl + 4 + twr + 1;
44*4882a593Smuzhiyun u8 twr2rd = tcwl + 4 + 1 + twtr;
45*4882a593Smuzhiyun u8 trd2wr = tcl + 4 + 5 - tcwl + 1;
46*4882a593Smuzhiyun
47*4882a593Smuzhiyun /* set mode register */
48*4882a593Smuzhiyun writel(0xc3, &mctl_ctl->mr[1]); /* nWR=8, BL8 */
49*4882a593Smuzhiyun writel(0xa, &mctl_ctl->mr[2]); /* RL=12, WL=6 */
50*4882a593Smuzhiyun writel(0x2, &mctl_ctl->mr[3]); /* 40 0hms PD/PU */
51*4882a593Smuzhiyun
52*4882a593Smuzhiyun /* set DRAM timing */
53*4882a593Smuzhiyun writel(DRAMTMG0_TWTP(twtp) | DRAMTMG0_TFAW(tfaw) |
54*4882a593Smuzhiyun DRAMTMG0_TRAS_MAX(trasmax) | DRAMTMG0_TRAS(tras),
55*4882a593Smuzhiyun &mctl_ctl->dramtmg[0]);
56*4882a593Smuzhiyun writel(DRAMTMG1_TXP(txp) | DRAMTMG1_TRTP(trtp) | DRAMTMG1_TRC(trc),
57*4882a593Smuzhiyun &mctl_ctl->dramtmg[1]);
58*4882a593Smuzhiyun writel(DRAMTMG2_TCWL(tcwl) | DRAMTMG2_TCL(tcl) |
59*4882a593Smuzhiyun DRAMTMG2_TRD2WR(trd2wr) | DRAMTMG2_TWR2RD(twr2rd),
60*4882a593Smuzhiyun &mctl_ctl->dramtmg[2]);
61*4882a593Smuzhiyun writel(DRAMTMG3_TMRW(tmrw) | DRAMTMG3_TMRD(tmrd) | DRAMTMG3_TMOD(tmod),
62*4882a593Smuzhiyun &mctl_ctl->dramtmg[3]);
63*4882a593Smuzhiyun writel(DRAMTMG4_TRCD(trcd) | DRAMTMG4_TCCD(tccd) | DRAMTMG4_TRRD(trrd) |
64*4882a593Smuzhiyun DRAMTMG4_TRP(trp), &mctl_ctl->dramtmg[4]);
65*4882a593Smuzhiyun writel(DRAMTMG5_TCKSRX(tcksrx) | DRAMTMG5_TCKSRE(tcksre) |
66*4882a593Smuzhiyun DRAMTMG5_TCKESR(tckesr) | DRAMTMG5_TCKE(tcke),
67*4882a593Smuzhiyun &mctl_ctl->dramtmg[5]);
68*4882a593Smuzhiyun
69*4882a593Smuzhiyun /* set two rank timing */
70*4882a593Smuzhiyun clrsetbits_le32(&mctl_ctl->dramtmg[8], (0xff << 8) | (0xff << 0),
71*4882a593Smuzhiyun (0x66 << 8) | (0x10 << 0));
72*4882a593Smuzhiyun
73*4882a593Smuzhiyun /* set PHY interface timing, write latency and read latency configure */
74*4882a593Smuzhiyun writel((0x2 << 24) | (t_rdata_en << 16) | (0x1 << 8) |
75*4882a593Smuzhiyun (wr_latency << 0), &mctl_ctl->pitmg[0]);
76*4882a593Smuzhiyun
77*4882a593Smuzhiyun /* set PHY timing, PTR0-2 use default */
78*4882a593Smuzhiyun writel(PTR3_TDINIT0(tdinit0) | PTR3_TDINIT1(tdinit1), &mctl_ctl->ptr[3]);
79*4882a593Smuzhiyun writel(PTR4_TDINIT2(tdinit2) | PTR4_TDINIT3(tdinit3), &mctl_ctl->ptr[4]);
80*4882a593Smuzhiyun
81*4882a593Smuzhiyun /* set refresh timing */
82*4882a593Smuzhiyun writel(RFSHTMG_TREFI(trefi) | RFSHTMG_TRFC(trfc), &mctl_ctl->rfshtmg);
83*4882a593Smuzhiyun }
84