blob: 65afefe53e18a584873c84fae02e8ed7722465ba [file] [log] [blame]
Paul Burton96c68472018-12-16 19:25:22 -03001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * JZ4780 DDR initialization
4 *
5 * Copyright (c) 2013 Imagination Technologies
6 * Author: Paul Burton <paul.burton@imgtec.com>
7 *
8 * Based on spl/common/{jz4780_ddr,jz_ddr3_init}.c from X-Boot
9 * Copyright (c) 2006-2013 Ingenic Semiconductor
10 */
11
12#include <common.h>
Simon Glassf11478f2019-12-28 10:45:07 -070013#include <hang.h>
Paul Burton96c68472018-12-16 19:25:22 -030014#include <asm/io.h>
15#include <mach/jz4780.h>
16#include <mach/jz4780_dram.h>
17
18static const u32 get_mem_clk(void)
19{
20 const u32 mpll_out = ((u64)JZ4780_SYS_EXTAL * JZ4780_MPLL_M) /
21 (JZ4780_MPLL_N * JZ4780_MPLL_OD);
22 return mpll_out / JZ4780_SYS_MEM_DIV;
23}
24
25u32 sdram_size(int cs)
26{
27 u32 dw = DDR_DW32 ? 4 : 2;
28 u32 banks = DDR_BANK8 ? 8 : 4;
29 u32 size = 0;
30
31 if ((cs == 0) && DDR_CS0EN) {
32 size = (1 << (DDR_ROW + DDR_COL)) * dw * banks;
33 if (DDR_CS1EN && (size > 0x20000000))
34 size = 0x20000000;
35 } else if ((cs == 1) && DDR_CS1EN) {
36 size = (1 << (DDR_ROW + DDR_COL)) * dw * banks;
37 }
38
39 return size;
40}
41
42static void ddr_cfg_init(void)
43{
44 void __iomem *ddr_ctl_regs = (void __iomem *)DDRC_BASE;
45 u32 ddrc_cfg, tmp;
46
47 tmp = DDR_CL;
48 if (tmp)
49 tmp--;
50 if (tmp > 4)
51 tmp = 4;
52
53 ddrc_cfg = DDRC_CFG_TYPE_DDR3 | DDRC_CFG_IMBA |
54 DDR_DW32 | DDRC_CFG_MPRT | ((tmp | 0x8) << 2) |
55 ((DDR_ROW - 12) << 11) | ((DDR_COL - 8) << 8) |
56 (DDR_CS0EN << 6) | (DDR_BANK8 << 1) |
57 ((DDR_ROW - 12) << 27) | ((DDR_COL - 8) << 24) |
58 (DDR_CS1EN << 7) | (DDR_BANK8 << 23);
59
60 if (DDR_BL > 4)
61 ddrc_cfg |= BIT(21);
62
63 writel(ddrc_cfg, ddr_ctl_regs + DDRC_CFG);
64}
65
66static void ddr_phy_init(const struct jz4780_ddr_config *ddr_config)
67{
68 void __iomem *ddr_ctl_regs = (void __iomem *)DDRC_BASE;
69 void __iomem *ddr_phy_regs = ddr_ctl_regs + DDR_PHY_OFFSET;
70 unsigned int count = 0, i;
71 u32 reg, mask;
72
73 writel(DDRP_DCR_TYPE_DDR3 | (DDR_BANK8 << 3), ddr_phy_regs + DDRP_DCR);
74
75 writel(ddr_config->mr0, ddr_phy_regs + DDRP_MR0);
76 writel(ddr_config->mr1, ddr_phy_regs + DDRP_MR1);
77 writel(0, ddr_phy_regs + DDRP_ODTCR);
78 writel(0, ddr_phy_regs + DDRP_MR2);
79
80 writel(ddr_config->ptr0, ddr_phy_regs + DDRP_PTR0);
81 writel(ddr_config->ptr1, ddr_phy_regs + DDRP_PTR1);
82 writel(ddr_config->ptr2, ddr_phy_regs + DDRP_PTR2);
83
84 writel(ddr_config->dtpr0, ddr_phy_regs + DDRP_DTPR0);
85 writel(ddr_config->dtpr1, ddr_phy_regs + DDRP_DTPR1);
86 writel(ddr_config->dtpr2, ddr_phy_regs + DDRP_DTPR2);
87
88 writel(DDRP_PGCR_DQSCFG | (7 << DDRP_PGCR_CKEN_BIT) |
89 (2 << DDRP_PGCR_CKDV_BIT) |
90 (DDR_CS0EN | (DDR_CS1EN << 1)) << DDRP_PGCR_RANKEN_BIT |
91 DDRP_PGCR_ZCKSEL_32 | DDRP_PGCR_PDDISDX,
92 ddr_phy_regs + DDRP_PGCR);
93
94 for (i = 0; i < 8; i++)
95 clrbits_le32(ddr_phy_regs + DDRP_DXGCR(i), 0x3 << 9);
96
97 count = 0;
98 mask = DDRP_PGSR_IDONE | DDRP_PGSR_DLDONE | DDRP_PGSR_ZCDONE;
99 for (;;) {
100 reg = readl(ddr_phy_regs + DDRP_PGSR);
101 if ((reg == mask) || (reg == 0x1f))
102 break;
103 if (count++ == 10000)
104 hang();
105 }
106
107 /* DQS extension and early set to 1 */
108 clrsetbits_le32(ddr_phy_regs + DDRP_DSGCR, 0x7E << 4, 0x12 << 4);
109
110 /* 500 pull up and 500 pull down */
111 clrsetbits_le32(ddr_phy_regs + DDRP_DXCCR, 0xFF << 4, 0xC4 << 4);
112
113 /* Initialise phy */
114 writel(DDRP_PIR_INIT | DDRP_PIR_DRAMINT | DDRP_PIR_DRAMRST,
115 ddr_phy_regs + DDRP_PIR);
116
117 count = 0;
118 mask |= DDRP_PGSR_DIDONE;
119 for (;;) {
120 reg = readl(ddr_phy_regs + DDRP_PGSR);
121 if ((reg == mask) || (reg == 0x1f))
122 break;
123 if (count++ == 20000)
124 hang();
125 }
126
127 writel(DDRP_PIR_INIT | DDRP_PIR_QSTRN, ddr_phy_regs + DDRP_PIR);
128
129 count = 0;
130 mask |= DDRP_PGSR_DTDONE;
131 for (;;) {
132 reg = readl(ddr_phy_regs + DDRP_PGSR);
133 if (reg == mask)
134 break;
135 if (count++ != 50000)
136 continue;
137 reg &= DDRP_PGSR_DTDONE | DDRP_PGSR_DTERR | DDRP_PGSR_DTIERR;
138 if (reg)
139 hang();
140 count = 0;
141 }
142
143 /* Override impedance */
144 clrsetbits_le32(ddr_phy_regs + DDRP_ZQXCR0(0), 0x3ff,
145 ((ddr_config->pullup & 0x1f) << DDRP_ZQXCR_PULLUP_IMPE_BIT) |
146 ((ddr_config->pulldn & 0x1f) << DDRP_ZQXCR_PULLDOWN_IMPE_BIT) |
147 DDRP_ZQXCR_ZDEN);
148}
149
150#define JZBIT(bit) ((bit % 4) * 8)
151#define JZMASK(bit) (0x1f << JZBIT(bit))
152
153static void remap_swap(int a, int b)
154{
155 void __iomem *ddr_ctl_regs = (void __iomem *)DDRC_BASE;
156 u32 remmap[2], tmp[2];
157
158 remmap[0] = readl(ddr_ctl_regs + DDRC_REMMAP(a / 4));
159 remmap[1] = readl(ddr_ctl_regs + DDRC_REMMAP(b / 4));
160
161 tmp[0] = (remmap[0] & JZMASK(a)) >> JZBIT(a);
162 tmp[1] = (remmap[1] & JZMASK(b)) >> JZBIT(b);
163
164 remmap[0] &= ~JZMASK(a);
165 remmap[1] &= ~JZMASK(b);
166
167 writel(remmap[0] | (tmp[1] << JZBIT(a)),
168 ddr_ctl_regs + DDRC_REMMAP(a / 4));
169 writel(remmap[1] | (tmp[0] << JZBIT(b)),
170 ddr_ctl_regs + DDRC_REMMAP(b / 4));
171}
172
173static void mem_remap(void)
174{
175 u32 start = (DDR_ROW + DDR_COL + (DDR_DW32 ? 4 : 2) / 2) - 12;
176 u32 num = DDR_BANK8 ? 3 : 2;
177
178 if (DDR_CS0EN && DDR_CS1EN)
179 num++;
180
181 for (; num > 0; num--)
182 remap_swap(0 + num - 1, start + num - 1);
183}
184
185/* Fetch DRAM config from board file */
186__weak const struct jz4780_ddr_config *jz4780_get_ddr_config(void)
187{
188 return NULL;
189}
190
191void sdram_init(void)
192{
193 const struct jz4780_ddr_config *ddr_config = jz4780_get_ddr_config();
194 void __iomem *ddr_ctl_regs = (void __iomem *)DDRC_BASE;
195 void __iomem *ddr_phy_regs = ddr_ctl_regs + DDR_PHY_OFFSET;
196 void __iomem *cpm_regs = (void __iomem *)CPM_BASE;
197 u32 mem_clk, tmp, i;
198 u32 mem_base0, mem_base1;
199 u32 mem_mask0, mem_mask1;
200 u32 mem_size0, mem_size1;
201
202 if (!ddr_config)
203 hang();
204
205 /* Reset DLL in DDR PHY */
206 writel(0x3, cpm_regs + 0xd0);
207 mdelay(400);
208 writel(0x1, cpm_regs + 0xd0);
209 mdelay(400);
210
211 /* Enter reset */
212 writel(0xf << 20, ddr_ctl_regs + DDRC_CTRL);
213
214 mem_clk = get_mem_clk();
215
216 tmp = 1000000000 / mem_clk;
217 if (1000000000 % mem_clk)
218 tmp++;
219 tmp = DDR_tREFI / tmp;
220 tmp = tmp / (16 * (1 << DDR_CLK_DIV)) - 1;
221 if (tmp > 0xff)
222 tmp = 0xff;
223 if (tmp < 1)
224 tmp = 1;
225
226 writel(0x0, ddr_ctl_regs + DDRC_CTRL);
227
228 writel(0x150000, ddr_phy_regs + DDRP_DTAR);
229 ddr_phy_init(ddr_config);
230
231 writel(DDRC_CTRL_CKE | DDRC_CTRL_ALH, ddr_ctl_regs + DDRC_CTRL);
232 writel(0x0, ddr_ctl_regs + DDRC_CTRL);
233
234 ddr_cfg_init();
235
236 for (i = 0; i < 6; i++)
237 writel(ddr_config->timing[i], ddr_ctl_regs + DDRC_TIMING(i));
238
239 mem_size0 = sdram_size(0);
240 mem_size1 = sdram_size(1);
241
242 if (!mem_size1 && mem_size0 > 0x20000000) {
243 mem_base0 = 0x0;
244 mem_mask0 = ~(((mem_size0 * 2) >> 24) - 1) & DDRC_MMAP_MASK_MASK;
245 } else {
246 mem_base0 = (DDR_MEM_PHY_BASE >> 24) & 0xff;
247 mem_mask0 = ~((mem_size0 >> 24) - 1) & DDRC_MMAP_MASK_MASK;
248 }
249
250 if (mem_size1) {
251 mem_mask1 = ~((mem_size1 >> 24) - 1) & DDRC_MMAP_MASK_MASK;
252 mem_base1 = ((DDR_MEM_PHY_BASE + mem_size0) >> 24) & 0xff;
253 } else {
254 mem_mask1 = 0;
255 mem_base1 = 0xff;
256 }
257
258 writel(mem_base0 << DDRC_MMAP_BASE_BIT | mem_mask0,
259 ddr_ctl_regs + DDRC_MMAP0);
260 writel(mem_base1 << DDRC_MMAP_BASE_BIT | mem_mask1,
261 ddr_ctl_regs + DDRC_MMAP1);
262 writel(DDRC_CTRL_CKE | DDRC_CTRL_ALH, ddr_ctl_regs + DDRC_CTRL);
263 writel((DDR_CLK_DIV << 1) | DDRC_REFCNT_REF_EN |
264 (tmp << DDRC_REFCNT_CON_BIT),
265 ddr_ctl_regs + DDRC_REFCNT);
266 writel((1 << 15) | (4 << 12) | (1 << 11) | (1 << 8) | (0 << 6) |
267 (1 << 4) | (1 << 3) | (1 << 2) | (1 << 1),
268 ddr_ctl_regs + DDRC_CTRL);
269 mem_remap();
270 clrbits_le32(ddr_ctl_regs + DDRC_ST, 0x40);
271}