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