blob: cf5587be54b5a23078a3aa9c4ec517859b31c2a6 [file] [log] [blame]
Tim Harveyfe0f7f72014-06-02 16:13:23 -07001/*
2 * Copyright (C) 2014 Gateworks Corporation
3 * Author: Tim Harvey <tharvey@gateworks.com>
4 *
5 * SPDX-License-Identifier: GPL-2.0+
6 */
7
8#include <common.h>
9#include <linux/types.h>
Peng Faneb796cb2015-08-17 16:11:04 +080010#include <asm/arch/clock.h>
Tim Harveyfe0f7f72014-06-02 16:13:23 -070011#include <asm/arch/mx6-ddr.h>
12#include <asm/arch/sys_proto.h>
13#include <asm/io.h>
14#include <asm/types.h>
15
Peng Fand9efd472014-12-30 17:24:01 +080016#if defined(CONFIG_MX6SX)
17/* Configure MX6SX mmdc iomux */
18void mx6sx_dram_iocfg(unsigned width,
19 const struct mx6sx_iomux_ddr_regs *ddr,
20 const struct mx6sx_iomux_grp_regs *grp)
21{
22 struct mx6sx_iomux_ddr_regs *mx6_ddr_iomux;
23 struct mx6sx_iomux_grp_regs *mx6_grp_iomux;
24
25 mx6_ddr_iomux = (struct mx6sx_iomux_ddr_regs *)MX6SX_IOM_DDR_BASE;
26 mx6_grp_iomux = (struct mx6sx_iomux_grp_regs *)MX6SX_IOM_GRP_BASE;
27
28 /* DDR IO TYPE */
29 writel(grp->grp_ddr_type, &mx6_grp_iomux->grp_ddr_type);
30 writel(grp->grp_ddrpke, &mx6_grp_iomux->grp_ddrpke);
31
32 /* CLOCK */
33 writel(ddr->dram_sdclk_0, &mx6_ddr_iomux->dram_sdclk_0);
34
35 /* ADDRESS */
36 writel(ddr->dram_cas, &mx6_ddr_iomux->dram_cas);
37 writel(ddr->dram_ras, &mx6_ddr_iomux->dram_ras);
38 writel(grp->grp_addds, &mx6_grp_iomux->grp_addds);
39
40 /* Control */
41 writel(ddr->dram_reset, &mx6_ddr_iomux->dram_reset);
42 writel(ddr->dram_sdba2, &mx6_ddr_iomux->dram_sdba2);
43 writel(ddr->dram_sdcke0, &mx6_ddr_iomux->dram_sdcke0);
44 writel(ddr->dram_sdcke1, &mx6_ddr_iomux->dram_sdcke1);
45 writel(ddr->dram_odt0, &mx6_ddr_iomux->dram_odt0);
46 writel(ddr->dram_odt1, &mx6_ddr_iomux->dram_odt1);
47 writel(grp->grp_ctlds, &mx6_grp_iomux->grp_ctlds);
48
49 /* Data Strobes */
50 writel(grp->grp_ddrmode_ctl, &mx6_grp_iomux->grp_ddrmode_ctl);
51 writel(ddr->dram_sdqs0, &mx6_ddr_iomux->dram_sdqs0);
52 writel(ddr->dram_sdqs1, &mx6_ddr_iomux->dram_sdqs1);
53 if (width >= 32) {
54 writel(ddr->dram_sdqs2, &mx6_ddr_iomux->dram_sdqs2);
55 writel(ddr->dram_sdqs3, &mx6_ddr_iomux->dram_sdqs3);
56 }
57
58 /* Data */
59 writel(grp->grp_ddrmode, &mx6_grp_iomux->grp_ddrmode);
60 writel(grp->grp_b0ds, &mx6_grp_iomux->grp_b0ds);
61 writel(grp->grp_b1ds, &mx6_grp_iomux->grp_b1ds);
62 if (width >= 32) {
63 writel(grp->grp_b2ds, &mx6_grp_iomux->grp_b2ds);
64 writel(grp->grp_b3ds, &mx6_grp_iomux->grp_b3ds);
65 }
66 writel(ddr->dram_dqm0, &mx6_ddr_iomux->dram_dqm0);
67 writel(ddr->dram_dqm1, &mx6_ddr_iomux->dram_dqm1);
68 if (width >= 32) {
69 writel(ddr->dram_dqm2, &mx6_ddr_iomux->dram_dqm2);
70 writel(ddr->dram_dqm3, &mx6_ddr_iomux->dram_dqm3);
71 }
72}
73#endif
74
Peng Fana462c342015-07-20 19:28:33 +080075#ifdef CONFIG_MX6UL
76void mx6ul_dram_iocfg(unsigned width,
77 const struct mx6ul_iomux_ddr_regs *ddr,
78 const struct mx6ul_iomux_grp_regs *grp)
79{
80 struct mx6ul_iomux_ddr_regs *mx6_ddr_iomux;
81 struct mx6ul_iomux_grp_regs *mx6_grp_iomux;
82
83 mx6_ddr_iomux = (struct mx6ul_iomux_ddr_regs *)MX6UL_IOM_DDR_BASE;
84 mx6_grp_iomux = (struct mx6ul_iomux_grp_regs *)MX6UL_IOM_GRP_BASE;
85
86 /* DDR IO TYPE */
87 writel(grp->grp_ddr_type, &mx6_grp_iomux->grp_ddr_type);
88 writel(grp->grp_ddrpke, &mx6_grp_iomux->grp_ddrpke);
89
90 /* CLOCK */
91 writel(ddr->dram_sdclk_0, &mx6_ddr_iomux->dram_sdclk_0);
92
93 /* ADDRESS */
94 writel(ddr->dram_cas, &mx6_ddr_iomux->dram_cas);
95 writel(ddr->dram_ras, &mx6_ddr_iomux->dram_ras);
96 writel(grp->grp_addds, &mx6_grp_iomux->grp_addds);
97
98 /* Control */
99 writel(ddr->dram_reset, &mx6_ddr_iomux->dram_reset);
100 writel(ddr->dram_sdba2, &mx6_ddr_iomux->dram_sdba2);
101 writel(ddr->dram_odt0, &mx6_ddr_iomux->dram_odt0);
102 writel(ddr->dram_odt1, &mx6_ddr_iomux->dram_odt1);
103 writel(grp->grp_ctlds, &mx6_grp_iomux->grp_ctlds);
104
105 /* Data Strobes */
106 writel(grp->grp_ddrmode_ctl, &mx6_grp_iomux->grp_ddrmode_ctl);
107 writel(ddr->dram_sdqs0, &mx6_ddr_iomux->dram_sdqs0);
108 writel(ddr->dram_sdqs1, &mx6_ddr_iomux->dram_sdqs1);
109
110 /* Data */
111 writel(grp->grp_ddrmode, &mx6_grp_iomux->grp_ddrmode);
112 writel(grp->grp_b0ds, &mx6_grp_iomux->grp_b0ds);
113 writel(grp->grp_b1ds, &mx6_grp_iomux->grp_b1ds);
114 writel(ddr->dram_dqm0, &mx6_ddr_iomux->dram_dqm0);
115 writel(ddr->dram_dqm1, &mx6_ddr_iomux->dram_dqm1);
116}
117#endif
118
Peng Fan1b811e22015-08-17 16:11:00 +0800119#if defined(CONFIG_MX6SL)
120void mx6sl_dram_iocfg(unsigned width,
121 const struct mx6sl_iomux_ddr_regs *ddr,
122 const struct mx6sl_iomux_grp_regs *grp)
123{
124 struct mx6sl_iomux_ddr_regs *mx6_ddr_iomux;
125 struct mx6sl_iomux_grp_regs *mx6_grp_iomux;
126
127 mx6_ddr_iomux = (struct mx6sl_iomux_ddr_regs *)MX6SL_IOM_DDR_BASE;
128 mx6_grp_iomux = (struct mx6sl_iomux_grp_regs *)MX6SL_IOM_GRP_BASE;
129
130 /* DDR IO TYPE */
131 mx6_grp_iomux->grp_ddr_type = grp->grp_ddr_type;
132 mx6_grp_iomux->grp_ddrpke = grp->grp_ddrpke;
133
134 /* CLOCK */
135 mx6_ddr_iomux->dram_sdclk_0 = ddr->dram_sdclk_0;
136
137 /* ADDRESS */
138 mx6_ddr_iomux->dram_cas = ddr->dram_cas;
139 mx6_ddr_iomux->dram_ras = ddr->dram_ras;
140 mx6_grp_iomux->grp_addds = grp->grp_addds;
141
142 /* Control */
143 mx6_ddr_iomux->dram_reset = ddr->dram_reset;
144 mx6_ddr_iomux->dram_sdba2 = ddr->dram_sdba2;
145 mx6_grp_iomux->grp_ctlds = grp->grp_ctlds;
146
147 /* Data Strobes */
148 mx6_grp_iomux->grp_ddrmode_ctl = grp->grp_ddrmode_ctl;
149 mx6_ddr_iomux->dram_sdqs0 = ddr->dram_sdqs0;
150 mx6_ddr_iomux->dram_sdqs1 = ddr->dram_sdqs1;
151 if (width >= 32) {
152 mx6_ddr_iomux->dram_sdqs2 = ddr->dram_sdqs2;
153 mx6_ddr_iomux->dram_sdqs3 = ddr->dram_sdqs3;
154 }
155
156 /* Data */
157 mx6_grp_iomux->grp_ddrmode = grp->grp_ddrmode;
158 mx6_grp_iomux->grp_b0ds = grp->grp_b0ds;
159 mx6_grp_iomux->grp_b1ds = grp->grp_b1ds;
160 if (width >= 32) {
161 mx6_grp_iomux->grp_b2ds = grp->grp_b2ds;
162 mx6_grp_iomux->grp_b3ds = grp->grp_b3ds;
163 }
164
165 mx6_ddr_iomux->dram_dqm0 = ddr->dram_dqm0;
166 mx6_ddr_iomux->dram_dqm1 = ddr->dram_dqm1;
167 if (width >= 32) {
168 mx6_ddr_iomux->dram_dqm2 = ddr->dram_dqm2;
169 mx6_ddr_iomux->dram_dqm3 = ddr->dram_dqm3;
170 }
171}
172#endif
173
Tim Harveyfe0f7f72014-06-02 16:13:23 -0700174#if defined(CONFIG_MX6QDL) || defined(CONFIG_MX6Q) || defined(CONFIG_MX6D)
175/* Configure MX6DQ mmdc iomux */
176void mx6dq_dram_iocfg(unsigned width,
177 const struct mx6dq_iomux_ddr_regs *ddr,
178 const struct mx6dq_iomux_grp_regs *grp)
179{
180 volatile struct mx6dq_iomux_ddr_regs *mx6_ddr_iomux;
181 volatile struct mx6dq_iomux_grp_regs *mx6_grp_iomux;
182
183 mx6_ddr_iomux = (struct mx6dq_iomux_ddr_regs *)MX6DQ_IOM_DDR_BASE;
184 mx6_grp_iomux = (struct mx6dq_iomux_grp_regs *)MX6DQ_IOM_GRP_BASE;
185
186 /* DDR IO Type */
187 mx6_grp_iomux->grp_ddr_type = grp->grp_ddr_type;
188 mx6_grp_iomux->grp_ddrpke = grp->grp_ddrpke;
189
190 /* Clock */
191 mx6_ddr_iomux->dram_sdclk_0 = ddr->dram_sdclk_0;
192 mx6_ddr_iomux->dram_sdclk_1 = ddr->dram_sdclk_1;
193
194 /* Address */
195 mx6_ddr_iomux->dram_cas = ddr->dram_cas;
196 mx6_ddr_iomux->dram_ras = ddr->dram_ras;
197 mx6_grp_iomux->grp_addds = grp->grp_addds;
198
199 /* Control */
200 mx6_ddr_iomux->dram_reset = ddr->dram_reset;
201 mx6_ddr_iomux->dram_sdcke0 = ddr->dram_sdcke0;
202 mx6_ddr_iomux->dram_sdcke1 = ddr->dram_sdcke1;
203 mx6_ddr_iomux->dram_sdba2 = ddr->dram_sdba2;
204 mx6_ddr_iomux->dram_sdodt0 = ddr->dram_sdodt0;
205 mx6_ddr_iomux->dram_sdodt1 = ddr->dram_sdodt1;
206 mx6_grp_iomux->grp_ctlds = grp->grp_ctlds;
207
208 /* Data Strobes */
209 mx6_grp_iomux->grp_ddrmode_ctl = grp->grp_ddrmode_ctl;
210 mx6_ddr_iomux->dram_sdqs0 = ddr->dram_sdqs0;
211 mx6_ddr_iomux->dram_sdqs1 = ddr->dram_sdqs1;
212 if (width >= 32) {
213 mx6_ddr_iomux->dram_sdqs2 = ddr->dram_sdqs2;
214 mx6_ddr_iomux->dram_sdqs3 = ddr->dram_sdqs3;
215 }
216 if (width >= 64) {
217 mx6_ddr_iomux->dram_sdqs4 = ddr->dram_sdqs4;
218 mx6_ddr_iomux->dram_sdqs5 = ddr->dram_sdqs5;
219 mx6_ddr_iomux->dram_sdqs6 = ddr->dram_sdqs6;
220 mx6_ddr_iomux->dram_sdqs7 = ddr->dram_sdqs7;
221 }
222
223 /* Data */
224 mx6_grp_iomux->grp_ddrmode = grp->grp_ddrmode;
225 mx6_grp_iomux->grp_b0ds = grp->grp_b0ds;
226 mx6_grp_iomux->grp_b1ds = grp->grp_b1ds;
227 if (width >= 32) {
228 mx6_grp_iomux->grp_b2ds = grp->grp_b2ds;
229 mx6_grp_iomux->grp_b3ds = grp->grp_b3ds;
230 }
231 if (width >= 64) {
232 mx6_grp_iomux->grp_b4ds = grp->grp_b4ds;
233 mx6_grp_iomux->grp_b5ds = grp->grp_b5ds;
234 mx6_grp_iomux->grp_b6ds = grp->grp_b6ds;
235 mx6_grp_iomux->grp_b7ds = grp->grp_b7ds;
236 }
237 mx6_ddr_iomux->dram_dqm0 = ddr->dram_dqm0;
238 mx6_ddr_iomux->dram_dqm1 = ddr->dram_dqm1;
239 if (width >= 32) {
240 mx6_ddr_iomux->dram_dqm2 = ddr->dram_dqm2;
241 mx6_ddr_iomux->dram_dqm3 = ddr->dram_dqm3;
242 }
243 if (width >= 64) {
244 mx6_ddr_iomux->dram_dqm4 = ddr->dram_dqm4;
245 mx6_ddr_iomux->dram_dqm5 = ddr->dram_dqm5;
246 mx6_ddr_iomux->dram_dqm6 = ddr->dram_dqm6;
247 mx6_ddr_iomux->dram_dqm7 = ddr->dram_dqm7;
248 }
249}
250#endif
251
252#if defined(CONFIG_MX6QDL) || defined(CONFIG_MX6DL) || defined(CONFIG_MX6S)
253/* Configure MX6SDL mmdc iomux */
254void mx6sdl_dram_iocfg(unsigned width,
255 const struct mx6sdl_iomux_ddr_regs *ddr,
256 const struct mx6sdl_iomux_grp_regs *grp)
257{
258 volatile struct mx6sdl_iomux_ddr_regs *mx6_ddr_iomux;
259 volatile struct mx6sdl_iomux_grp_regs *mx6_grp_iomux;
260
261 mx6_ddr_iomux = (struct mx6sdl_iomux_ddr_regs *)MX6SDL_IOM_DDR_BASE;
262 mx6_grp_iomux = (struct mx6sdl_iomux_grp_regs *)MX6SDL_IOM_GRP_BASE;
263
264 /* DDR IO Type */
265 mx6_grp_iomux->grp_ddr_type = grp->grp_ddr_type;
266 mx6_grp_iomux->grp_ddrpke = grp->grp_ddrpke;
267
268 /* Clock */
269 mx6_ddr_iomux->dram_sdclk_0 = ddr->dram_sdclk_0;
270 mx6_ddr_iomux->dram_sdclk_1 = ddr->dram_sdclk_1;
271
272 /* Address */
273 mx6_ddr_iomux->dram_cas = ddr->dram_cas;
274 mx6_ddr_iomux->dram_ras = ddr->dram_ras;
275 mx6_grp_iomux->grp_addds = grp->grp_addds;
276
277 /* Control */
278 mx6_ddr_iomux->dram_reset = ddr->dram_reset;
279 mx6_ddr_iomux->dram_sdcke0 = ddr->dram_sdcke0;
280 mx6_ddr_iomux->dram_sdcke1 = ddr->dram_sdcke1;
281 mx6_ddr_iomux->dram_sdba2 = ddr->dram_sdba2;
282 mx6_ddr_iomux->dram_sdodt0 = ddr->dram_sdodt0;
283 mx6_ddr_iomux->dram_sdodt1 = ddr->dram_sdodt1;
284 mx6_grp_iomux->grp_ctlds = grp->grp_ctlds;
285
286 /* Data Strobes */
287 mx6_grp_iomux->grp_ddrmode_ctl = grp->grp_ddrmode_ctl;
288 mx6_ddr_iomux->dram_sdqs0 = ddr->dram_sdqs0;
289 mx6_ddr_iomux->dram_sdqs1 = ddr->dram_sdqs1;
290 if (width >= 32) {
291 mx6_ddr_iomux->dram_sdqs2 = ddr->dram_sdqs2;
292 mx6_ddr_iomux->dram_sdqs3 = ddr->dram_sdqs3;
293 }
294 if (width >= 64) {
295 mx6_ddr_iomux->dram_sdqs4 = ddr->dram_sdqs4;
296 mx6_ddr_iomux->dram_sdqs5 = ddr->dram_sdqs5;
297 mx6_ddr_iomux->dram_sdqs6 = ddr->dram_sdqs6;
298 mx6_ddr_iomux->dram_sdqs7 = ddr->dram_sdqs7;
299 }
300
301 /* Data */
302 mx6_grp_iomux->grp_ddrmode = grp->grp_ddrmode;
303 mx6_grp_iomux->grp_b0ds = grp->grp_b0ds;
304 mx6_grp_iomux->grp_b1ds = grp->grp_b1ds;
305 if (width >= 32) {
306 mx6_grp_iomux->grp_b2ds = grp->grp_b2ds;
307 mx6_grp_iomux->grp_b3ds = grp->grp_b3ds;
308 }
309 if (width >= 64) {
310 mx6_grp_iomux->grp_b4ds = grp->grp_b4ds;
311 mx6_grp_iomux->grp_b5ds = grp->grp_b5ds;
312 mx6_grp_iomux->grp_b6ds = grp->grp_b6ds;
313 mx6_grp_iomux->grp_b7ds = grp->grp_b7ds;
314 }
315 mx6_ddr_iomux->dram_dqm0 = ddr->dram_dqm0;
316 mx6_ddr_iomux->dram_dqm1 = ddr->dram_dqm1;
317 if (width >= 32) {
318 mx6_ddr_iomux->dram_dqm2 = ddr->dram_dqm2;
319 mx6_ddr_iomux->dram_dqm3 = ddr->dram_dqm3;
320 }
321 if (width >= 64) {
322 mx6_ddr_iomux->dram_dqm4 = ddr->dram_dqm4;
323 mx6_ddr_iomux->dram_dqm5 = ddr->dram_dqm5;
324 mx6_ddr_iomux->dram_dqm6 = ddr->dram_dqm6;
325 mx6_ddr_iomux->dram_dqm7 = ddr->dram_dqm7;
326 }
327}
328#endif
329
330/*
331 * Configure mx6 mmdc registers based on:
332 * - board-specific memory configuration
333 * - board-specific calibration data
Peng Faneb796cb2015-08-17 16:11:04 +0800334 * - ddr3/lpddr2 chip details
Tim Harveyfe0f7f72014-06-02 16:13:23 -0700335 *
336 * The various calculations here are derived from the Freescale
Peng Faneb796cb2015-08-17 16:11:04 +0800337 * 1. i.Mx6DQSDL DDR3 Script Aid spreadsheet (DOC-94917) designed to generate
338 * MMDC configuration registers based on memory system and memory chip
339 * parameters.
340 *
341 * 2. i.Mx6SL LPDDR2 Script Aid spreadsheet V0.04 designed to generate MMDC
342 * configuration registers based on memory system and memory chip
343 * parameters.
Tim Harveyfe0f7f72014-06-02 16:13:23 -0700344 *
345 * The defaults here are those which were specified in the spreadsheet.
346 * For details on each register, refer to the IMX6DQRM and/or IMX6SDLRM
Peng Faneb796cb2015-08-17 16:11:04 +0800347 * and/or IMX6SLRM section titled MMDC initialization.
Tim Harveyfe0f7f72014-06-02 16:13:23 -0700348 */
349#define MR(val, ba, cmd, cs1) \
350 ((val << 16) | (1 << 15) | (cmd << 4) | (cs1 << 3) | ba)
Peng Fana462c342015-07-20 19:28:33 +0800351#define MMDC1(entry, value) do { \
Peng Fan24139752015-08-17 16:10:58 +0800352 if (!is_cpu_type(MXC_CPU_MX6SX) && !is_cpu_type(MXC_CPU_MX6UL) && \
353 !is_cpu_type(MXC_CPU_MX6SL)) \
Peng Fana462c342015-07-20 19:28:33 +0800354 mmdc1->entry = value; \
355 } while (0)
356
Peng Faneb796cb2015-08-17 16:11:04 +0800357/*
358 * According JESD209-2B-LPDDR2: Table 103
359 * WL: write latency
360 */
361static int lpddr2_wl(uint32_t mem_speed)
362{
363 switch (mem_speed) {
364 case 1066:
365 case 933:
366 return 4;
367 case 800:
368 return 3;
369 case 677:
370 case 533:
371 return 2;
372 case 400:
373 case 333:
374 return 1;
375 default:
376 puts("invalid memory speed\n");
377 hang();
378 }
379
380 return 0;
381}
382
383/*
384 * According JESD209-2B-LPDDR2: Table 103
385 * RL: read latency
386 */
387static int lpddr2_rl(uint32_t mem_speed)
388{
389 switch (mem_speed) {
390 case 1066:
391 return 8;
392 case 933:
393 return 7;
394 case 800:
395 return 6;
396 case 677:
397 return 5;
398 case 533:
399 return 4;
400 case 400:
401 case 333:
402 return 3;
403 default:
404 puts("invalid memory speed\n");
405 hang();
406 }
407
408 return 0;
409}
410
411void mx6_lpddr2_cfg(const struct mx6_ddr_sysinfo *sysinfo,
412 const struct mx6_mmdc_calibration *calib,
413 const struct mx6_lpddr2_cfg *lpddr2_cfg)
414{
415 volatile struct mmdc_p_regs *mmdc0;
416 u32 val;
417 u8 tcke, tcksrx, tcksre, trrd;
418 u8 twl, txp, tfaw, tcl;
419 u16 tras, twr, tmrd, trtp, twtr, trfc, txsr;
420 u16 trcd_lp, trppb_lp, trpab_lp, trc_lp;
421 u16 cs0_end;
422 u8 coladdr;
423 int clkper; /* clock period in picoseconds */
424 int clock; /* clock freq in mHz */
425 int cs;
426
427 /* only support 16/32 bits */
428 if (sysinfo->dsize > 1)
429 hang();
430
431 mmdc0 = (struct mmdc_p_regs *)MMDC_P0_BASE_ADDR;
432
433 clock = mxc_get_clock(MXC_DDR_CLK) / 1000000U;
434 clkper = (1000 * 1000) / clock; /* pico seconds */
435
436 twl = lpddr2_wl(lpddr2_cfg->mem_speed) - 1;
437
438 /* LPDDR2-S2 and LPDDR2-S4 have the same tRFC value. */
439 switch (lpddr2_cfg->density) {
440 case 1:
441 case 2:
442 case 4:
443 trfc = DIV_ROUND_UP(130000, clkper) - 1;
444 txsr = DIV_ROUND_UP(140000, clkper) - 1;
445 break;
446 case 8:
447 trfc = DIV_ROUND_UP(210000, clkper) - 1;
448 txsr = DIV_ROUND_UP(220000, clkper) - 1;
449 break;
450 default:
451 /*
452 * 64Mb, 128Mb, 256Mb, 512Mb are not supported currently.
453 */
454 hang();
455 break;
456 }
457 /*
458 * txpdll, txpr, taonpd and taofpd are not relevant in LPDDR2 mode,
459 * set them to 0. */
460 txp = DIV_ROUND_UP(7500, clkper) - 1;
461 tcke = 3;
462 if (lpddr2_cfg->mem_speed == 333)
463 tfaw = DIV_ROUND_UP(60000, clkper) - 1;
464 else
465 tfaw = DIV_ROUND_UP(50000, clkper) - 1;
466 trrd = DIV_ROUND_UP(10000, clkper) - 1;
467
468 /* tckesr for LPDDR2 */
469 tcksre = DIV_ROUND_UP(15000, clkper);
470 tcksrx = tcksre;
471 twr = DIV_ROUND_UP(15000, clkper) - 1;
472 /*
473 * tMRR: 2, tMRW: 5
474 * tMRD should be set to max(tMRR, tMRW)
475 */
476 tmrd = 5;
477 tras = DIV_ROUND_UP(lpddr2_cfg->trasmin, clkper / 10) - 1;
478 /* LPDDR2 mode use tRCD_LP filed in MDCFG3. */
479 trcd_lp = DIV_ROUND_UP(lpddr2_cfg->trcd_lp, clkper / 10) - 1;
480 trc_lp = DIV_ROUND_UP(lpddr2_cfg->trasmin + lpddr2_cfg->trppb_lp,
481 clkper / 10) - 1;
482 trppb_lp = DIV_ROUND_UP(lpddr2_cfg->trppb_lp, clkper / 10) - 1;
483 trpab_lp = DIV_ROUND_UP(lpddr2_cfg->trpab_lp, clkper / 10) - 1;
484 /* To LPDDR2, CL in MDCFG0 refers to RL */
485 tcl = lpddr2_rl(lpddr2_cfg->mem_speed) - 3;
486 twtr = DIV_ROUND_UP(7500, clkper) - 1;
487 trtp = DIV_ROUND_UP(7500, clkper) - 1;
488
489 cs0_end = 4 * sysinfo->cs_density - 1;
490
491 debug("density:%d Gb (%d Gb per chip)\n",
492 sysinfo->cs_density, lpddr2_cfg->density);
493 debug("clock: %dMHz (%d ps)\n", clock, clkper);
494 debug("memspd:%d\n", lpddr2_cfg->mem_speed);
495 debug("trcd_lp=%d\n", trcd_lp);
496 debug("trppb_lp=%d\n", trppb_lp);
497 debug("trpab_lp=%d\n", trpab_lp);
498 debug("trc_lp=%d\n", trc_lp);
499 debug("tcke=%d\n", tcke);
500 debug("tcksrx=%d\n", tcksrx);
501 debug("tcksre=%d\n", tcksre);
502 debug("trfc=%d\n", trfc);
503 debug("txsr=%d\n", txsr);
504 debug("txp=%d\n", txp);
505 debug("tfaw=%d\n", tfaw);
506 debug("tcl=%d\n", tcl);
507 debug("tras=%d\n", tras);
508 debug("twr=%d\n", twr);
509 debug("tmrd=%d\n", tmrd);
510 debug("twl=%d\n", twl);
511 debug("trtp=%d\n", trtp);
512 debug("twtr=%d\n", twtr);
513 debug("trrd=%d\n", trrd);
514 debug("cs0_end=%d\n", cs0_end);
515 debug("ncs=%d\n", sysinfo->ncs);
516
517 /*
518 * board-specific configuration:
519 * These values are determined empirically and vary per board layout
520 */
521 mmdc0->mpwldectrl0 = calib->p0_mpwldectrl0;
522 mmdc0->mpwldectrl1 = calib->p0_mpwldectrl1;
523 mmdc0->mpdgctrl0 = calib->p0_mpdgctrl0;
524 mmdc0->mpdgctrl1 = calib->p0_mpdgctrl1;
525 mmdc0->mprddlctl = calib->p0_mprddlctl;
526 mmdc0->mpwrdlctl = calib->p0_mpwrdlctl;
527 mmdc0->mpzqlp2ctl = calib->mpzqlp2ctl;
528
529 /* Read data DQ Byte0-3 delay */
530 mmdc0->mprddqby0dl = 0x33333333;
531 mmdc0->mprddqby1dl = 0x33333333;
532 if (sysinfo->dsize > 0) {
533 mmdc0->mprddqby2dl = 0x33333333;
534 mmdc0->mprddqby3dl = 0x33333333;
535 }
536
537 /* Write data DQ Byte0-3 delay */
538 mmdc0->mpwrdqby0dl = 0xf3333333;
539 mmdc0->mpwrdqby1dl = 0xf3333333;
540 if (sysinfo->dsize > 0) {
541 mmdc0->mpwrdqby2dl = 0xf3333333;
542 mmdc0->mpwrdqby3dl = 0xf3333333;
543 }
544
545 /*
546 * In LPDDR2 mode this register should be cleared,
547 * so no termination will be activated.
548 */
549 mmdc0->mpodtctrl = 0;
550
551 /* complete calibration */
552 val = (1 << 11); /* Force measurement on delay-lines */
553 mmdc0->mpmur0 = val;
554
555 /* Step 1: configuration request */
556 mmdc0->mdscr = (u32)(1 << 15); /* config request */
557
558 /* Step 2: Timing configuration */
559 mmdc0->mdcfg0 = (trfc << 24) | (txsr << 16) | (txp << 13) |
560 (tfaw << 4) | tcl;
561 mmdc0->mdcfg1 = (tras << 16) | (twr << 9) | (tmrd << 5) | twl;
562 mmdc0->mdcfg2 = (trtp << 6) | (twtr << 3) | trrd;
563 mmdc0->mdcfg3lp = (trc_lp << 16) | (trcd_lp << 8) |
564 (trppb_lp << 4) | trpab_lp;
565 mmdc0->mdotc = 0;
566
567 mmdc0->mdasp = cs0_end; /* CS addressing */
568
569 /* Step 3: Configure DDR type */
570 mmdc0->mdmisc = (sysinfo->cs1_mirror << 19) | (sysinfo->walat << 16) |
571 (sysinfo->bi_on << 12) | (sysinfo->mif3_mode << 9) |
572 (sysinfo->ralat << 6) | (1 << 3);
573
574 /* Step 4: Configure delay while leaving reset */
575 mmdc0->mdor = (sysinfo->sde_to_rst << 8) |
576 (sysinfo->rst_to_cke << 0);
577
578 /* Step 5: Configure DDR physical parameters (density and burst len) */
579 coladdr = lpddr2_cfg->coladdr;
580 if (lpddr2_cfg->coladdr == 8) /* 8-bit COL is 0x3 */
581 coladdr += 4;
582 else if (lpddr2_cfg->coladdr == 12) /* 12-bit COL is 0x4 */
583 coladdr += 1;
584 mmdc0->mdctl = (lpddr2_cfg->rowaddr - 11) << 24 | /* ROW */
585 (coladdr - 9) << 20 | /* COL */
586 (0 << 19) | /* Burst Length = 4 for LPDDR2 */
587 (sysinfo->dsize << 16); /* DDR data bus size */
588
589 /* Step 6: Perform ZQ calibration */
590 val = 0xa1390003; /* one-time HW ZQ calib */
591 mmdc0->mpzqhwctrl = val;
592
593 /* Step 7: Enable MMDC with desired chip select */
594 mmdc0->mdctl |= (1 << 31) | /* SDE_0 for CS0 */
595 ((sysinfo->ncs == 2) ? 1 : 0) << 30; /* SDE_1 for CS1 */
596
597 /* Step 8: Write Mode Registers to Init LPDDR2 devices */
598 for (cs = 0; cs < sysinfo->ncs; cs++) {
599 /* MR63: reset */
600 mmdc0->mdscr = MR(63, 0, 3, cs);
601 /* MR10: calibration,
602 * 0xff is calibration command after intilization.
603 */
604 val = 0xA | (0xff << 8);
605 mmdc0->mdscr = MR(val, 0, 3, cs);
606 /* MR1 */
607 val = 0x1 | (0x82 << 8);
608 mmdc0->mdscr = MR(val, 0, 3, cs);
609 /* MR2 */
610 val = 0x2 | (0x04 << 8);
611 mmdc0->mdscr = MR(val, 0, 3, cs);
612 /* MR3 */
613 val = 0x3 | (0x02 << 8);
614 mmdc0->mdscr = MR(val, 0, 3, cs);
615 }
616
617 /* Step 10: Power down control and self-refresh */
618 mmdc0->mdpdc = (tcke & 0x7) << 16 |
619 5 << 12 | /* PWDT_1: 256 cycles */
620 5 << 8 | /* PWDT_0: 256 cycles */
621 1 << 6 | /* BOTH_CS_PD */
622 (tcksrx & 0x7) << 3 |
623 (tcksre & 0x7);
624 mmdc0->mapsr = 0x00001006; /* ADOPT power down enabled */
625
626 /* Step 11: Configure ZQ calibration: one-time and periodic 1ms */
627 val = 0xa1310003;
628 mmdc0->mpzqhwctrl = val;
629
630 /* Step 12: Configure and activate periodic refresh */
631 mmdc0->mdref = (0 << 14) | /* REF_SEL: Periodic refresh cycle: 64kHz */
632 (3 << 11); /* REFR: Refresh Rate - 4 refreshes */
633
634 /* Step 13: Deassert config request - init complete */
635 mmdc0->mdscr = 0x00000000;
636
637 /* wait for auto-ZQ calibration to complete */
638 mdelay(1);
639}
640
Peng Fanf2ff8342015-08-17 16:11:03 +0800641void mx6_ddr3_cfg(const struct mx6_ddr_sysinfo *sysinfo,
Nikita Kiryanov33689182014-09-07 18:58:11 +0300642 const struct mx6_mmdc_calibration *calib,
643 const struct mx6_ddr3_cfg *ddr3_cfg)
Tim Harveyfe0f7f72014-06-02 16:13:23 -0700644{
645 volatile struct mmdc_p_regs *mmdc0;
646 volatile struct mmdc_p_regs *mmdc1;
Nikita Kiryanov33689182014-09-07 18:58:11 +0300647 u32 val;
Tim Harveyfe0f7f72014-06-02 16:13:23 -0700648 u8 tcke, tcksrx, tcksre, txpdll, taofpd, taonpd, trrd;
649 u8 todtlon, taxpd, tanpd, tcwl, txp, tfaw, tcl;
650 u8 todt_idle_off = 0x4; /* from DDR3 Script Aid spreadsheet */
651 u16 trcd, trc, tras, twr, tmrd, trtp, trp, twtr, trfc, txs, txpr;
Nikita Kiryanov33689182014-09-07 18:58:11 +0300652 u16 cs0_end;
Tim Harveyfe0f7f72014-06-02 16:13:23 -0700653 u16 tdllk = 0x1ff; /* DLL locking time: 512 cycles (JEDEC DDR3) */
Marek Vasutb299ab72014-08-04 01:47:10 +0200654 u8 coladdr;
Tim Harveyfe0f7f72014-06-02 16:13:23 -0700655 int clkper; /* clock period in picoseconds */
Nikolay Dimitrov8a2bd212015-04-22 18:37:31 +0300656 int clock; /* clock freq in MHz */
Tim Harveyfe0f7f72014-06-02 16:13:23 -0700657 int cs;
Nikolay Dimitrov8a2bd212015-04-22 18:37:31 +0300658 u16 mem_speed = ddr3_cfg->mem_speed;
Tim Harveyfe0f7f72014-06-02 16:13:23 -0700659
660 mmdc0 = (struct mmdc_p_regs *)MMDC_P0_BASE_ADDR;
Peng Fan24139752015-08-17 16:10:58 +0800661 if (!is_cpu_type(MXC_CPU_MX6SX) && !is_cpu_type(MXC_CPU_MX6UL) &&
662 !is_cpu_type(MXC_CPU_MX6SL))
Peng Fana462c342015-07-20 19:28:33 +0800663 mmdc1 = (struct mmdc_p_regs *)MMDC_P1_BASE_ADDR;
Tim Harveyfe0f7f72014-06-02 16:13:23 -0700664
Nikolay Dimitrov8a2bd212015-04-22 18:37:31 +0300665 /* Limit mem_speed for MX6D/MX6Q */
Tim Harveyfe0f7f72014-06-02 16:13:23 -0700666 if (is_cpu_type(MXC_CPU_MX6Q) || is_cpu_type(MXC_CPU_MX6D)) {
Nikolay Dimitrov8a2bd212015-04-22 18:37:31 +0300667 if (mem_speed > 1066)
668 mem_speed = 1066; /* 1066 MT/s */
669
Tim Harveyfe0f7f72014-06-02 16:13:23 -0700670 tcwl = 4;
671 }
Nikolay Dimitrov8a2bd212015-04-22 18:37:31 +0300672 /* Limit mem_speed for MX6S/MX6DL */
Tim Harveyfe0f7f72014-06-02 16:13:23 -0700673 else {
Nikolay Dimitrov8a2bd212015-04-22 18:37:31 +0300674 if (mem_speed > 800)
675 mem_speed = 800; /* 800 MT/s */
676
Tim Harveyfe0f7f72014-06-02 16:13:23 -0700677 tcwl = 3;
678 }
Nikolay Dimitrov8a2bd212015-04-22 18:37:31 +0300679
680 clock = mem_speed / 2;
681 /*
682 * Data rate of 1066 MT/s requires 533 MHz DDR3 clock, but MX6D/Q supports
683 * up to 528 MHz, so reduce the clock to fit chip specs
684 */
685 if (is_cpu_type(MXC_CPU_MX6Q) || is_cpu_type(MXC_CPU_MX6D)) {
686 if (clock > 528)
687 clock = 528; /* 528 MHz */
688 }
689
Nikita Kiryanov33689182014-09-07 18:58:11 +0300690 clkper = (1000 * 1000) / clock; /* pico seconds */
Tim Harveyfe0f7f72014-06-02 16:13:23 -0700691 todtlon = tcwl;
692 taxpd = tcwl;
693 tanpd = tcwl;
Tim Harveyfe0f7f72014-06-02 16:13:23 -0700694
Nikita Kiryanov33689182014-09-07 18:58:11 +0300695 switch (ddr3_cfg->density) {
Tim Harveyfe0f7f72014-06-02 16:13:23 -0700696 case 1: /* 1Gb per chip */
697 trfc = DIV_ROUND_UP(110000, clkper) - 1;
698 txs = DIV_ROUND_UP(120000, clkper) - 1;
699 break;
700 case 2: /* 2Gb per chip */
701 trfc = DIV_ROUND_UP(160000, clkper) - 1;
702 txs = DIV_ROUND_UP(170000, clkper) - 1;
703 break;
704 case 4: /* 4Gb per chip */
Peng Fan2cd8cd02015-08-17 16:10:59 +0800705 trfc = DIV_ROUND_UP(300000, clkper) - 1;
706 txs = DIV_ROUND_UP(310000, clkper) - 1;
Tim Harveyfe0f7f72014-06-02 16:13:23 -0700707 break;
708 case 8: /* 8Gb per chip */
709 trfc = DIV_ROUND_UP(350000, clkper) - 1;
710 txs = DIV_ROUND_UP(360000, clkper) - 1;
711 break;
712 default:
713 /* invalid density */
Nikita Kiryanov33689182014-09-07 18:58:11 +0300714 puts("invalid chip density\n");
Tim Harveyfe0f7f72014-06-02 16:13:23 -0700715 hang();
716 break;
717 }
718 txpr = txs;
719
Nikolay Dimitrov8a2bd212015-04-22 18:37:31 +0300720 switch (mem_speed) {
Tim Harveyfe0f7f72014-06-02 16:13:23 -0700721 case 800:
Masahiro Yamadac79cba32014-09-18 13:28:06 +0900722 txp = DIV_ROUND_UP(max(3 * clkper, 7500), clkper) - 1;
723 tcke = DIV_ROUND_UP(max(3 * clkper, 7500), clkper) - 1;
Nikita Kiryanov33689182014-09-07 18:58:11 +0300724 if (ddr3_cfg->pagesz == 1) {
Tim Harveyfe0f7f72014-06-02 16:13:23 -0700725 tfaw = DIV_ROUND_UP(40000, clkper) - 1;
Masahiro Yamadac79cba32014-09-18 13:28:06 +0900726 trrd = DIV_ROUND_UP(max(4 * clkper, 10000), clkper) - 1;
Tim Harveyfe0f7f72014-06-02 16:13:23 -0700727 } else {
728 tfaw = DIV_ROUND_UP(50000, clkper) - 1;
Masahiro Yamadac79cba32014-09-18 13:28:06 +0900729 trrd = DIV_ROUND_UP(max(4 * clkper, 10000), clkper) - 1;
Tim Harveyfe0f7f72014-06-02 16:13:23 -0700730 }
731 break;
732 case 1066:
Masahiro Yamadac79cba32014-09-18 13:28:06 +0900733 txp = DIV_ROUND_UP(max(3 * clkper, 7500), clkper) - 1;
734 tcke = DIV_ROUND_UP(max(3 * clkper, 5625), clkper) - 1;
Nikita Kiryanov33689182014-09-07 18:58:11 +0300735 if (ddr3_cfg->pagesz == 1) {
Tim Harveyfe0f7f72014-06-02 16:13:23 -0700736 tfaw = DIV_ROUND_UP(37500, clkper) - 1;
Masahiro Yamadac79cba32014-09-18 13:28:06 +0900737 trrd = DIV_ROUND_UP(max(4 * clkper, 7500), clkper) - 1;
Tim Harveyfe0f7f72014-06-02 16:13:23 -0700738 } else {
739 tfaw = DIV_ROUND_UP(50000, clkper) - 1;
Masahiro Yamadac79cba32014-09-18 13:28:06 +0900740 trrd = DIV_ROUND_UP(max(4 * clkper, 10000), clkper) - 1;
Tim Harveyfe0f7f72014-06-02 16:13:23 -0700741 }
742 break;
Tim Harveyfe0f7f72014-06-02 16:13:23 -0700743 default:
Nikita Kiryanov33689182014-09-07 18:58:11 +0300744 puts("invalid memory speed\n");
Tim Harveyfe0f7f72014-06-02 16:13:23 -0700745 hang();
746 break;
747 }
Masahiro Yamadac79cba32014-09-18 13:28:06 +0900748 txpdll = DIV_ROUND_UP(max(10 * clkper, 24000), clkper) - 1;
749 tcksre = DIV_ROUND_UP(max(5 * clkper, 10000), clkper);
Tim Harveyfe0f7f72014-06-02 16:13:23 -0700750 taonpd = DIV_ROUND_UP(2000, clkper) - 1;
Nikita Kiryanov33689182014-09-07 18:58:11 +0300751 tcksrx = tcksre;
Tim Harveyfe0f7f72014-06-02 16:13:23 -0700752 taofpd = taonpd;
Nikita Kiryanov33689182014-09-07 18:58:11 +0300753 twr = DIV_ROUND_UP(15000, clkper) - 1;
Masahiro Yamadac79cba32014-09-18 13:28:06 +0900754 tmrd = DIV_ROUND_UP(max(12 * clkper, 15000), clkper) - 1;
Nikita Kiryanov33689182014-09-07 18:58:11 +0300755 trc = DIV_ROUND_UP(ddr3_cfg->trcmin, clkper / 10) - 1;
756 tras = DIV_ROUND_UP(ddr3_cfg->trasmin, clkper / 10) - 1;
757 tcl = DIV_ROUND_UP(ddr3_cfg->trcd, clkper / 10) - 3;
758 trp = DIV_ROUND_UP(ddr3_cfg->trcd, clkper / 10) - 1;
Masahiro Yamadac79cba32014-09-18 13:28:06 +0900759 twtr = ROUND(max(4 * clkper, 7500) / clkper, 1) - 1;
Tim Harveyfe0f7f72014-06-02 16:13:23 -0700760 trcd = trp;
Tim Harveyfe0f7f72014-06-02 16:13:23 -0700761 trtp = twtr;
Nikita Kiryanov07ee9272014-08-20 15:08:58 +0300762 cs0_end = 4 * sysinfo->cs_density - 1;
Nikita Kiryanov33689182014-09-07 18:58:11 +0300763
764 debug("density:%d Gb (%d Gb per chip)\n",
765 sysinfo->cs_density, ddr3_cfg->density);
Tim Harveyfe0f7f72014-06-02 16:13:23 -0700766 debug("clock: %dMHz (%d ps)\n", clock, clkper);
Nikolay Dimitrov8a2bd212015-04-22 18:37:31 +0300767 debug("memspd:%d\n", mem_speed);
Tim Harveyfe0f7f72014-06-02 16:13:23 -0700768 debug("tcke=%d\n", tcke);
769 debug("tcksrx=%d\n", tcksrx);
770 debug("tcksre=%d\n", tcksre);
771 debug("taofpd=%d\n", taofpd);
772 debug("taonpd=%d\n", taonpd);
773 debug("todtlon=%d\n", todtlon);
774 debug("tanpd=%d\n", tanpd);
775 debug("taxpd=%d\n", taxpd);
776 debug("trfc=%d\n", trfc);
777 debug("txs=%d\n", txs);
778 debug("txp=%d\n", txp);
779 debug("txpdll=%d\n", txpdll);
780 debug("tfaw=%d\n", tfaw);
781 debug("tcl=%d\n", tcl);
782 debug("trcd=%d\n", trcd);
783 debug("trp=%d\n", trp);
784 debug("trc=%d\n", trc);
785 debug("tras=%d\n", tras);
786 debug("twr=%d\n", twr);
787 debug("tmrd=%d\n", tmrd);
788 debug("tcwl=%d\n", tcwl);
789 debug("tdllk=%d\n", tdllk);
790 debug("trtp=%d\n", trtp);
791 debug("twtr=%d\n", twtr);
792 debug("trrd=%d\n", trrd);
793 debug("txpr=%d\n", txpr);
Nikita Kiryanov33689182014-09-07 18:58:11 +0300794 debug("cs0_end=%d\n", cs0_end);
795 debug("ncs=%d\n", sysinfo->ncs);
796 debug("Rtt_wr=%d\n", sysinfo->rtt_wr);
797 debug("Rtt_nom=%d\n", sysinfo->rtt_nom);
798 debug("SRT=%d\n", ddr3_cfg->SRT);
Tim Harveyfe0f7f72014-06-02 16:13:23 -0700799 debug("tcl=%d\n", tcl);
800 debug("twr=%d\n", twr);
801
802 /*
803 * board-specific configuration:
804 * These values are determined empirically and vary per board layout
805 * see:
806 * appnote, ddr3 spreadsheet
807 */
Nikita Kiryanov33689182014-09-07 18:58:11 +0300808 mmdc0->mpwldectrl0 = calib->p0_mpwldectrl0;
809 mmdc0->mpwldectrl1 = calib->p0_mpwldectrl1;
810 mmdc0->mpdgctrl0 = calib->p0_mpdgctrl0;
811 mmdc0->mpdgctrl1 = calib->p0_mpdgctrl1;
812 mmdc0->mprddlctl = calib->p0_mprddlctl;
813 mmdc0->mpwrdlctl = calib->p0_mpwrdlctl;
814 if (sysinfo->dsize > 1) {
Peng Fand9efd472014-12-30 17:24:01 +0800815 MMDC1(mpwldectrl0, calib->p1_mpwldectrl0);
816 MMDC1(mpwldectrl1, calib->p1_mpwldectrl1);
817 MMDC1(mpdgctrl0, calib->p1_mpdgctrl0);
818 MMDC1(mpdgctrl1, calib->p1_mpdgctrl1);
819 MMDC1(mprddlctl, calib->p1_mprddlctl);
820 MMDC1(mpwrdlctl, calib->p1_mpwrdlctl);
Tim Harveyfe0f7f72014-06-02 16:13:23 -0700821 }
822
823 /* Read data DQ Byte0-3 delay */
Nikita Kiryanov33689182014-09-07 18:58:11 +0300824 mmdc0->mprddqby0dl = 0x33333333;
825 mmdc0->mprddqby1dl = 0x33333333;
826 if (sysinfo->dsize > 0) {
827 mmdc0->mprddqby2dl = 0x33333333;
828 mmdc0->mprddqby3dl = 0x33333333;
Tim Harveyfe0f7f72014-06-02 16:13:23 -0700829 }
Nikita Kiryanov33689182014-09-07 18:58:11 +0300830
831 if (sysinfo->dsize > 1) {
Peng Fand9efd472014-12-30 17:24:01 +0800832 MMDC1(mprddqby0dl, 0x33333333);
833 MMDC1(mprddqby1dl, 0x33333333);
834 MMDC1(mprddqby2dl, 0x33333333);
835 MMDC1(mprddqby3dl, 0x33333333);
Tim Harveyfe0f7f72014-06-02 16:13:23 -0700836 }
837
838 /* MMDC Termination: rtt_nom:2 RZQ/2(120ohm), rtt_nom:1 RZQ/4(60ohm) */
Nikita Kiryanov33689182014-09-07 18:58:11 +0300839 val = (sysinfo->rtt_nom == 2) ? 0x00011117 : 0x00022227;
840 mmdc0->mpodtctrl = val;
841 if (sysinfo->dsize > 1)
Peng Fand9efd472014-12-30 17:24:01 +0800842 MMDC1(mpodtctrl, val);
Tim Harveyfe0f7f72014-06-02 16:13:23 -0700843
844 /* complete calibration */
Nikita Kiryanov33689182014-09-07 18:58:11 +0300845 val = (1 << 11); /* Force measurement on delay-lines */
846 mmdc0->mpmur0 = val;
847 if (sysinfo->dsize > 1)
Peng Fand9efd472014-12-30 17:24:01 +0800848 MMDC1(mpmur0, val);
Tim Harveyfe0f7f72014-06-02 16:13:23 -0700849
850 /* Step 1: configuration request */
851 mmdc0->mdscr = (u32)(1 << 15); /* config request */
852
853 /* Step 2: Timing configuration */
Nikita Kiryanov33689182014-09-07 18:58:11 +0300854 mmdc0->mdcfg0 = (trfc << 24) | (txs << 16) | (txp << 13) |
855 (txpdll << 9) | (tfaw << 4) | tcl;
856 mmdc0->mdcfg1 = (trcd << 29) | (trp << 26) | (trc << 21) |
857 (tras << 16) | (1 << 15) /* trpa */ |
858 (twr << 9) | (tmrd << 5) | tcwl;
859 mmdc0->mdcfg2 = (tdllk << 16) | (trtp << 6) | (twtr << 3) | trrd;
860 mmdc0->mdotc = (taofpd << 27) | (taonpd << 24) | (tanpd << 20) |
861 (taxpd << 16) | (todtlon << 12) | (todt_idle_off << 4);
862 mmdc0->mdasp = cs0_end; /* CS addressing */
Tim Harveyfe0f7f72014-06-02 16:13:23 -0700863
864 /* Step 3: Configure DDR type */
Nikita Kiryanov33689182014-09-07 18:58:11 +0300865 mmdc0->mdmisc = (sysinfo->cs1_mirror << 19) | (sysinfo->walat << 16) |
866 (sysinfo->bi_on << 12) | (sysinfo->mif3_mode << 9) |
867 (sysinfo->ralat << 6);
Tim Harveyfe0f7f72014-06-02 16:13:23 -0700868
869 /* Step 4: Configure delay while leaving reset */
Nikita Kiryanov33689182014-09-07 18:58:11 +0300870 mmdc0->mdor = (txpr << 16) | (sysinfo->sde_to_rst << 8) |
871 (sysinfo->rst_to_cke << 0);
Tim Harveyfe0f7f72014-06-02 16:13:23 -0700872
873 /* Step 5: Configure DDR physical parameters (density and burst len) */
Nikita Kiryanov33689182014-09-07 18:58:11 +0300874 coladdr = ddr3_cfg->coladdr;
875 if (ddr3_cfg->coladdr == 8) /* 8-bit COL is 0x3 */
Marek Vasutb299ab72014-08-04 01:47:10 +0200876 coladdr += 4;
Nikita Kiryanov33689182014-09-07 18:58:11 +0300877 else if (ddr3_cfg->coladdr == 12) /* 12-bit COL is 0x4 */
Marek Vasutb299ab72014-08-04 01:47:10 +0200878 coladdr += 1;
Nikita Kiryanov33689182014-09-07 18:58:11 +0300879 mmdc0->mdctl = (ddr3_cfg->rowaddr - 11) << 24 | /* ROW */
880 (coladdr - 9) << 20 | /* COL */
881 (1 << 19) | /* Burst Length = 8 for DDR3 */
882 (sysinfo->dsize << 16); /* DDR data bus size */
Tim Harveyfe0f7f72014-06-02 16:13:23 -0700883
884 /* Step 6: Perform ZQ calibration */
Nikita Kiryanov33689182014-09-07 18:58:11 +0300885 val = 0xa1390001; /* one-time HW ZQ calib */
886 mmdc0->mpzqhwctrl = val;
887 if (sysinfo->dsize > 1)
Peng Fand9efd472014-12-30 17:24:01 +0800888 MMDC1(mpzqhwctrl, val);
Tim Harveyfe0f7f72014-06-02 16:13:23 -0700889
890 /* Step 7: Enable MMDC with desired chip select */
Nikita Kiryanov33689182014-09-07 18:58:11 +0300891 mmdc0->mdctl |= (1 << 31) | /* SDE_0 for CS0 */
892 ((sysinfo->ncs == 2) ? 1 : 0) << 30; /* SDE_1 for CS1 */
Tim Harveyfe0f7f72014-06-02 16:13:23 -0700893
894 /* Step 8: Write Mode Registers to Init DDR3 devices */
Nikita Kiryanov33689182014-09-07 18:58:11 +0300895 for (cs = 0; cs < sysinfo->ncs; cs++) {
Tim Harveyfe0f7f72014-06-02 16:13:23 -0700896 /* MR2 */
Nikita Kiryanov33689182014-09-07 18:58:11 +0300897 val = (sysinfo->rtt_wr & 3) << 9 | (ddr3_cfg->SRT & 1) << 7 |
Tim Harveyfe0f7f72014-06-02 16:13:23 -0700898 ((tcwl - 3) & 3) << 3;
Tim Harvey78c5a182015-04-03 16:52:52 -0700899 debug("MR2 CS%d: 0x%08x\n", cs, (u32)MR(val, 2, 3, cs));
Nikita Kiryanov33689182014-09-07 18:58:11 +0300900 mmdc0->mdscr = MR(val, 2, 3, cs);
Tim Harveyfe0f7f72014-06-02 16:13:23 -0700901 /* MR3 */
Tim Harvey78c5a182015-04-03 16:52:52 -0700902 debug("MR3 CS%d: 0x%08x\n", cs, (u32)MR(0, 3, 3, cs));
Nikita Kiryanov33689182014-09-07 18:58:11 +0300903 mmdc0->mdscr = MR(0, 3, 3, cs);
Tim Harveyfe0f7f72014-06-02 16:13:23 -0700904 /* MR1 */
Nikita Kiryanov33689182014-09-07 18:58:11 +0300905 val = ((sysinfo->rtt_nom & 1) ? 1 : 0) << 2 |
906 ((sysinfo->rtt_nom & 2) ? 1 : 0) << 6;
Tim Harvey78c5a182015-04-03 16:52:52 -0700907 debug("MR1 CS%d: 0x%08x\n", cs, (u32)MR(val, 1, 3, cs));
Nikita Kiryanov33689182014-09-07 18:58:11 +0300908 mmdc0->mdscr = MR(val, 1, 3, cs);
909 /* MR0 */
910 val = ((tcl - 1) << 4) | /* CAS */
Tim Harveyfe0f7f72014-06-02 16:13:23 -0700911 (1 << 8) | /* DLL Reset */
Tim Harvey3625fd62015-05-18 07:07:02 -0700912 ((twr - 3) << 9) | /* Write Recovery */
913 (sysinfo->pd_fast_exit << 12); /* Precharge PD PLL on */
Tim Harvey78c5a182015-04-03 16:52:52 -0700914 debug("MR0 CS%d: 0x%08x\n", cs, (u32)MR(val, 0, 3, cs));
Nikita Kiryanov33689182014-09-07 18:58:11 +0300915 mmdc0->mdscr = MR(val, 0, 3, cs);
Tim Harveyfe0f7f72014-06-02 16:13:23 -0700916 /* ZQ calibration */
Nikita Kiryanov33689182014-09-07 18:58:11 +0300917 val = (1 << 10);
918 mmdc0->mdscr = MR(val, 0, 4, cs);
Tim Harveyfe0f7f72014-06-02 16:13:23 -0700919 }
920
921 /* Step 10: Power down control and self-refresh */
Nikita Kiryanov33689182014-09-07 18:58:11 +0300922 mmdc0->mdpdc = (tcke & 0x7) << 16 |
923 5 << 12 | /* PWDT_1: 256 cycles */
924 5 << 8 | /* PWDT_0: 256 cycles */
925 1 << 6 | /* BOTH_CS_PD */
926 (tcksrx & 0x7) << 3 |
927 (tcksre & 0x7);
Tim Harvey78c5a182015-04-03 16:52:52 -0700928 if (!sysinfo->pd_fast_exit)
929 mmdc0->mdpdc |= (1 << 7); /* SLOW_PD */
Nikita Kiryanov06a51b82014-08-20 15:08:56 +0300930 mmdc0->mapsr = 0x00001006; /* ADOPT power down enabled */
Tim Harveyfe0f7f72014-06-02 16:13:23 -0700931
932 /* Step 11: Configure ZQ calibration: one-time and periodic 1ms */
Nikita Kiryanov33689182014-09-07 18:58:11 +0300933 val = 0xa1390003;
934 mmdc0->mpzqhwctrl = val;
935 if (sysinfo->dsize > 1)
Peng Fand9efd472014-12-30 17:24:01 +0800936 MMDC1(mpzqhwctrl, val);
Tim Harveyfe0f7f72014-06-02 16:13:23 -0700937
938 /* Step 12: Configure and activate periodic refresh */
Nikita Kiryanov33689182014-09-07 18:58:11 +0300939 mmdc0->mdref = (1 << 14) | /* REF_SEL: Periodic refresh cycle: 32kHz */
940 (7 << 11); /* REFR: Refresh Rate - 8 refreshes */
Tim Harveyfe0f7f72014-06-02 16:13:23 -0700941
942 /* Step 13: Deassert config request - init complete */
Nikita Kiryanov33689182014-09-07 18:58:11 +0300943 mmdc0->mdscr = 0x00000000;
Tim Harveyfe0f7f72014-06-02 16:13:23 -0700944
945 /* wait for auto-ZQ calibration to complete */
946 mdelay(1);
947}
Peng Fanf2ff8342015-08-17 16:11:03 +0800948
949void mx6_dram_cfg(const struct mx6_ddr_sysinfo *sysinfo,
950 const struct mx6_mmdc_calibration *calib,
951 const void *ddr_cfg)
952{
953 if (sysinfo->ddr_type == DDR_TYPE_DDR3) {
954 mx6_ddr3_cfg(sysinfo, calib, ddr_cfg);
Peng Faneb796cb2015-08-17 16:11:04 +0800955 } else if (sysinfo->ddr_type == DDR_TYPE_LPDDR2) {
956 mx6_lpddr2_cfg(sysinfo, calib, ddr_cfg);
Peng Fanf2ff8342015-08-17 16:11:03 +0800957 } else {
958 puts("Unsupported ddr type\n");
959 hang();
960 }
961}