blob: 3ec3e791c94d2fbb1012f771bd709970a1010ecf [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>
10#include <asm/arch/mx6-ddr.h>
11#include <asm/arch/sys_proto.h>
12#include <asm/io.h>
13#include <asm/types.h>
14
Peng Fand9efd472014-12-30 17:24:01 +080015#if defined(CONFIG_MX6SX)
16/* Configure MX6SX mmdc iomux */
17void mx6sx_dram_iocfg(unsigned width,
18 const struct mx6sx_iomux_ddr_regs *ddr,
19 const struct mx6sx_iomux_grp_regs *grp)
20{
21 struct mx6sx_iomux_ddr_regs *mx6_ddr_iomux;
22 struct mx6sx_iomux_grp_regs *mx6_grp_iomux;
23
24 mx6_ddr_iomux = (struct mx6sx_iomux_ddr_regs *)MX6SX_IOM_DDR_BASE;
25 mx6_grp_iomux = (struct mx6sx_iomux_grp_regs *)MX6SX_IOM_GRP_BASE;
26
27 /* DDR IO TYPE */
28 writel(grp->grp_ddr_type, &mx6_grp_iomux->grp_ddr_type);
29 writel(grp->grp_ddrpke, &mx6_grp_iomux->grp_ddrpke);
30
31 /* CLOCK */
32 writel(ddr->dram_sdclk_0, &mx6_ddr_iomux->dram_sdclk_0);
33
34 /* ADDRESS */
35 writel(ddr->dram_cas, &mx6_ddr_iomux->dram_cas);
36 writel(ddr->dram_ras, &mx6_ddr_iomux->dram_ras);
37 writel(grp->grp_addds, &mx6_grp_iomux->grp_addds);
38
39 /* Control */
40 writel(ddr->dram_reset, &mx6_ddr_iomux->dram_reset);
41 writel(ddr->dram_sdba2, &mx6_ddr_iomux->dram_sdba2);
42 writel(ddr->dram_sdcke0, &mx6_ddr_iomux->dram_sdcke0);
43 writel(ddr->dram_sdcke1, &mx6_ddr_iomux->dram_sdcke1);
44 writel(ddr->dram_odt0, &mx6_ddr_iomux->dram_odt0);
45 writel(ddr->dram_odt1, &mx6_ddr_iomux->dram_odt1);
46 writel(grp->grp_ctlds, &mx6_grp_iomux->grp_ctlds);
47
48 /* Data Strobes */
49 writel(grp->grp_ddrmode_ctl, &mx6_grp_iomux->grp_ddrmode_ctl);
50 writel(ddr->dram_sdqs0, &mx6_ddr_iomux->dram_sdqs0);
51 writel(ddr->dram_sdqs1, &mx6_ddr_iomux->dram_sdqs1);
52 if (width >= 32) {
53 writel(ddr->dram_sdqs2, &mx6_ddr_iomux->dram_sdqs2);
54 writel(ddr->dram_sdqs3, &mx6_ddr_iomux->dram_sdqs3);
55 }
56
57 /* Data */
58 writel(grp->grp_ddrmode, &mx6_grp_iomux->grp_ddrmode);
59 writel(grp->grp_b0ds, &mx6_grp_iomux->grp_b0ds);
60 writel(grp->grp_b1ds, &mx6_grp_iomux->grp_b1ds);
61 if (width >= 32) {
62 writel(grp->grp_b2ds, &mx6_grp_iomux->grp_b2ds);
63 writel(grp->grp_b3ds, &mx6_grp_iomux->grp_b3ds);
64 }
65 writel(ddr->dram_dqm0, &mx6_ddr_iomux->dram_dqm0);
66 writel(ddr->dram_dqm1, &mx6_ddr_iomux->dram_dqm1);
67 if (width >= 32) {
68 writel(ddr->dram_dqm2, &mx6_ddr_iomux->dram_dqm2);
69 writel(ddr->dram_dqm3, &mx6_ddr_iomux->dram_dqm3);
70 }
71}
72#endif
73
Peng Fana462c342015-07-20 19:28:33 +080074#ifdef CONFIG_MX6UL
75void mx6ul_dram_iocfg(unsigned width,
76 const struct mx6ul_iomux_ddr_regs *ddr,
77 const struct mx6ul_iomux_grp_regs *grp)
78{
79 struct mx6ul_iomux_ddr_regs *mx6_ddr_iomux;
80 struct mx6ul_iomux_grp_regs *mx6_grp_iomux;
81
82 mx6_ddr_iomux = (struct mx6ul_iomux_ddr_regs *)MX6UL_IOM_DDR_BASE;
83 mx6_grp_iomux = (struct mx6ul_iomux_grp_regs *)MX6UL_IOM_GRP_BASE;
84
85 /* DDR IO TYPE */
86 writel(grp->grp_ddr_type, &mx6_grp_iomux->grp_ddr_type);
87 writel(grp->grp_ddrpke, &mx6_grp_iomux->grp_ddrpke);
88
89 /* CLOCK */
90 writel(ddr->dram_sdclk_0, &mx6_ddr_iomux->dram_sdclk_0);
91
92 /* ADDRESS */
93 writel(ddr->dram_cas, &mx6_ddr_iomux->dram_cas);
94 writel(ddr->dram_ras, &mx6_ddr_iomux->dram_ras);
95 writel(grp->grp_addds, &mx6_grp_iomux->grp_addds);
96
97 /* Control */
98 writel(ddr->dram_reset, &mx6_ddr_iomux->dram_reset);
99 writel(ddr->dram_sdba2, &mx6_ddr_iomux->dram_sdba2);
100 writel(ddr->dram_odt0, &mx6_ddr_iomux->dram_odt0);
101 writel(ddr->dram_odt1, &mx6_ddr_iomux->dram_odt1);
102 writel(grp->grp_ctlds, &mx6_grp_iomux->grp_ctlds);
103
104 /* Data Strobes */
105 writel(grp->grp_ddrmode_ctl, &mx6_grp_iomux->grp_ddrmode_ctl);
106 writel(ddr->dram_sdqs0, &mx6_ddr_iomux->dram_sdqs0);
107 writel(ddr->dram_sdqs1, &mx6_ddr_iomux->dram_sdqs1);
108
109 /* Data */
110 writel(grp->grp_ddrmode, &mx6_grp_iomux->grp_ddrmode);
111 writel(grp->grp_b0ds, &mx6_grp_iomux->grp_b0ds);
112 writel(grp->grp_b1ds, &mx6_grp_iomux->grp_b1ds);
113 writel(ddr->dram_dqm0, &mx6_ddr_iomux->dram_dqm0);
114 writel(ddr->dram_dqm1, &mx6_ddr_iomux->dram_dqm1);
115}
116#endif
117
Tim Harveyfe0f7f72014-06-02 16:13:23 -0700118#if defined(CONFIG_MX6QDL) || defined(CONFIG_MX6Q) || defined(CONFIG_MX6D)
119/* Configure MX6DQ mmdc iomux */
120void mx6dq_dram_iocfg(unsigned width,
121 const struct mx6dq_iomux_ddr_regs *ddr,
122 const struct mx6dq_iomux_grp_regs *grp)
123{
124 volatile struct mx6dq_iomux_ddr_regs *mx6_ddr_iomux;
125 volatile struct mx6dq_iomux_grp_regs *mx6_grp_iomux;
126
127 mx6_ddr_iomux = (struct mx6dq_iomux_ddr_regs *)MX6DQ_IOM_DDR_BASE;
128 mx6_grp_iomux = (struct mx6dq_iomux_grp_regs *)MX6DQ_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 mx6_ddr_iomux->dram_sdclk_1 = ddr->dram_sdclk_1;
137
138 /* Address */
139 mx6_ddr_iomux->dram_cas = ddr->dram_cas;
140 mx6_ddr_iomux->dram_ras = ddr->dram_ras;
141 mx6_grp_iomux->grp_addds = grp->grp_addds;
142
143 /* Control */
144 mx6_ddr_iomux->dram_reset = ddr->dram_reset;
145 mx6_ddr_iomux->dram_sdcke0 = ddr->dram_sdcke0;
146 mx6_ddr_iomux->dram_sdcke1 = ddr->dram_sdcke1;
147 mx6_ddr_iomux->dram_sdba2 = ddr->dram_sdba2;
148 mx6_ddr_iomux->dram_sdodt0 = ddr->dram_sdodt0;
149 mx6_ddr_iomux->dram_sdodt1 = ddr->dram_sdodt1;
150 mx6_grp_iomux->grp_ctlds = grp->grp_ctlds;
151
152 /* Data Strobes */
153 mx6_grp_iomux->grp_ddrmode_ctl = grp->grp_ddrmode_ctl;
154 mx6_ddr_iomux->dram_sdqs0 = ddr->dram_sdqs0;
155 mx6_ddr_iomux->dram_sdqs1 = ddr->dram_sdqs1;
156 if (width >= 32) {
157 mx6_ddr_iomux->dram_sdqs2 = ddr->dram_sdqs2;
158 mx6_ddr_iomux->dram_sdqs3 = ddr->dram_sdqs3;
159 }
160 if (width >= 64) {
161 mx6_ddr_iomux->dram_sdqs4 = ddr->dram_sdqs4;
162 mx6_ddr_iomux->dram_sdqs5 = ddr->dram_sdqs5;
163 mx6_ddr_iomux->dram_sdqs6 = ddr->dram_sdqs6;
164 mx6_ddr_iomux->dram_sdqs7 = ddr->dram_sdqs7;
165 }
166
167 /* Data */
168 mx6_grp_iomux->grp_ddrmode = grp->grp_ddrmode;
169 mx6_grp_iomux->grp_b0ds = grp->grp_b0ds;
170 mx6_grp_iomux->grp_b1ds = grp->grp_b1ds;
171 if (width >= 32) {
172 mx6_grp_iomux->grp_b2ds = grp->grp_b2ds;
173 mx6_grp_iomux->grp_b3ds = grp->grp_b3ds;
174 }
175 if (width >= 64) {
176 mx6_grp_iomux->grp_b4ds = grp->grp_b4ds;
177 mx6_grp_iomux->grp_b5ds = grp->grp_b5ds;
178 mx6_grp_iomux->grp_b6ds = grp->grp_b6ds;
179 mx6_grp_iomux->grp_b7ds = grp->grp_b7ds;
180 }
181 mx6_ddr_iomux->dram_dqm0 = ddr->dram_dqm0;
182 mx6_ddr_iomux->dram_dqm1 = ddr->dram_dqm1;
183 if (width >= 32) {
184 mx6_ddr_iomux->dram_dqm2 = ddr->dram_dqm2;
185 mx6_ddr_iomux->dram_dqm3 = ddr->dram_dqm3;
186 }
187 if (width >= 64) {
188 mx6_ddr_iomux->dram_dqm4 = ddr->dram_dqm4;
189 mx6_ddr_iomux->dram_dqm5 = ddr->dram_dqm5;
190 mx6_ddr_iomux->dram_dqm6 = ddr->dram_dqm6;
191 mx6_ddr_iomux->dram_dqm7 = ddr->dram_dqm7;
192 }
193}
194#endif
195
196#if defined(CONFIG_MX6QDL) || defined(CONFIG_MX6DL) || defined(CONFIG_MX6S)
197/* Configure MX6SDL mmdc iomux */
198void mx6sdl_dram_iocfg(unsigned width,
199 const struct mx6sdl_iomux_ddr_regs *ddr,
200 const struct mx6sdl_iomux_grp_regs *grp)
201{
202 volatile struct mx6sdl_iomux_ddr_regs *mx6_ddr_iomux;
203 volatile struct mx6sdl_iomux_grp_regs *mx6_grp_iomux;
204
205 mx6_ddr_iomux = (struct mx6sdl_iomux_ddr_regs *)MX6SDL_IOM_DDR_BASE;
206 mx6_grp_iomux = (struct mx6sdl_iomux_grp_regs *)MX6SDL_IOM_GRP_BASE;
207
208 /* DDR IO Type */
209 mx6_grp_iomux->grp_ddr_type = grp->grp_ddr_type;
210 mx6_grp_iomux->grp_ddrpke = grp->grp_ddrpke;
211
212 /* Clock */
213 mx6_ddr_iomux->dram_sdclk_0 = ddr->dram_sdclk_0;
214 mx6_ddr_iomux->dram_sdclk_1 = ddr->dram_sdclk_1;
215
216 /* Address */
217 mx6_ddr_iomux->dram_cas = ddr->dram_cas;
218 mx6_ddr_iomux->dram_ras = ddr->dram_ras;
219 mx6_grp_iomux->grp_addds = grp->grp_addds;
220
221 /* Control */
222 mx6_ddr_iomux->dram_reset = ddr->dram_reset;
223 mx6_ddr_iomux->dram_sdcke0 = ddr->dram_sdcke0;
224 mx6_ddr_iomux->dram_sdcke1 = ddr->dram_sdcke1;
225 mx6_ddr_iomux->dram_sdba2 = ddr->dram_sdba2;
226 mx6_ddr_iomux->dram_sdodt0 = ddr->dram_sdodt0;
227 mx6_ddr_iomux->dram_sdodt1 = ddr->dram_sdodt1;
228 mx6_grp_iomux->grp_ctlds = grp->grp_ctlds;
229
230 /* Data Strobes */
231 mx6_grp_iomux->grp_ddrmode_ctl = grp->grp_ddrmode_ctl;
232 mx6_ddr_iomux->dram_sdqs0 = ddr->dram_sdqs0;
233 mx6_ddr_iomux->dram_sdqs1 = ddr->dram_sdqs1;
234 if (width >= 32) {
235 mx6_ddr_iomux->dram_sdqs2 = ddr->dram_sdqs2;
236 mx6_ddr_iomux->dram_sdqs3 = ddr->dram_sdqs3;
237 }
238 if (width >= 64) {
239 mx6_ddr_iomux->dram_sdqs4 = ddr->dram_sdqs4;
240 mx6_ddr_iomux->dram_sdqs5 = ddr->dram_sdqs5;
241 mx6_ddr_iomux->dram_sdqs6 = ddr->dram_sdqs6;
242 mx6_ddr_iomux->dram_sdqs7 = ddr->dram_sdqs7;
243 }
244
245 /* Data */
246 mx6_grp_iomux->grp_ddrmode = grp->grp_ddrmode;
247 mx6_grp_iomux->grp_b0ds = grp->grp_b0ds;
248 mx6_grp_iomux->grp_b1ds = grp->grp_b1ds;
249 if (width >= 32) {
250 mx6_grp_iomux->grp_b2ds = grp->grp_b2ds;
251 mx6_grp_iomux->grp_b3ds = grp->grp_b3ds;
252 }
253 if (width >= 64) {
254 mx6_grp_iomux->grp_b4ds = grp->grp_b4ds;
255 mx6_grp_iomux->grp_b5ds = grp->grp_b5ds;
256 mx6_grp_iomux->grp_b6ds = grp->grp_b6ds;
257 mx6_grp_iomux->grp_b7ds = grp->grp_b7ds;
258 }
259 mx6_ddr_iomux->dram_dqm0 = ddr->dram_dqm0;
260 mx6_ddr_iomux->dram_dqm1 = ddr->dram_dqm1;
261 if (width >= 32) {
262 mx6_ddr_iomux->dram_dqm2 = ddr->dram_dqm2;
263 mx6_ddr_iomux->dram_dqm3 = ddr->dram_dqm3;
264 }
265 if (width >= 64) {
266 mx6_ddr_iomux->dram_dqm4 = ddr->dram_dqm4;
267 mx6_ddr_iomux->dram_dqm5 = ddr->dram_dqm5;
268 mx6_ddr_iomux->dram_dqm6 = ddr->dram_dqm6;
269 mx6_ddr_iomux->dram_dqm7 = ddr->dram_dqm7;
270 }
271}
272#endif
273
274/*
275 * Configure mx6 mmdc registers based on:
276 * - board-specific memory configuration
277 * - board-specific calibration data
278 * - ddr3 chip details
279 *
280 * The various calculations here are derived from the Freescale
281 * i.Mx6DQSDL DDR3 Script Aid spreadsheet (DOC-94917) designed to generate MMDC
282 * configuration registers based on memory system and memory chip parameters.
283 *
284 * The defaults here are those which were specified in the spreadsheet.
285 * For details on each register, refer to the IMX6DQRM and/or IMX6SDLRM
286 * section titled MMDC initialization
287 */
288#define MR(val, ba, cmd, cs1) \
289 ((val << 16) | (1 << 15) | (cmd << 4) | (cs1 << 3) | ba)
Peng Fana462c342015-07-20 19:28:33 +0800290#define MMDC1(entry, value) do { \
Peng Fan24139752015-08-17 16:10:58 +0800291 if (!is_cpu_type(MXC_CPU_MX6SX) && !is_cpu_type(MXC_CPU_MX6UL) && \
292 !is_cpu_type(MXC_CPU_MX6SL)) \
Peng Fana462c342015-07-20 19:28:33 +0800293 mmdc1->entry = value; \
294 } while (0)
295
Nikita Kiryanov33689182014-09-07 18:58:11 +0300296void mx6_dram_cfg(const struct mx6_ddr_sysinfo *sysinfo,
297 const struct mx6_mmdc_calibration *calib,
298 const struct mx6_ddr3_cfg *ddr3_cfg)
Tim Harveyfe0f7f72014-06-02 16:13:23 -0700299{
300 volatile struct mmdc_p_regs *mmdc0;
301 volatile struct mmdc_p_regs *mmdc1;
Nikita Kiryanov33689182014-09-07 18:58:11 +0300302 u32 val;
Tim Harveyfe0f7f72014-06-02 16:13:23 -0700303 u8 tcke, tcksrx, tcksre, txpdll, taofpd, taonpd, trrd;
304 u8 todtlon, taxpd, tanpd, tcwl, txp, tfaw, tcl;
305 u8 todt_idle_off = 0x4; /* from DDR3 Script Aid spreadsheet */
306 u16 trcd, trc, tras, twr, tmrd, trtp, trp, twtr, trfc, txs, txpr;
Nikita Kiryanov33689182014-09-07 18:58:11 +0300307 u16 cs0_end;
Tim Harveyfe0f7f72014-06-02 16:13:23 -0700308 u16 tdllk = 0x1ff; /* DLL locking time: 512 cycles (JEDEC DDR3) */
Marek Vasutb299ab72014-08-04 01:47:10 +0200309 u8 coladdr;
Tim Harveyfe0f7f72014-06-02 16:13:23 -0700310 int clkper; /* clock period in picoseconds */
Nikolay Dimitrov8a2bd212015-04-22 18:37:31 +0300311 int clock; /* clock freq in MHz */
Tim Harveyfe0f7f72014-06-02 16:13:23 -0700312 int cs;
Nikolay Dimitrov8a2bd212015-04-22 18:37:31 +0300313 u16 mem_speed = ddr3_cfg->mem_speed;
Tim Harveyfe0f7f72014-06-02 16:13:23 -0700314
315 mmdc0 = (struct mmdc_p_regs *)MMDC_P0_BASE_ADDR;
Peng Fan24139752015-08-17 16:10:58 +0800316 if (!is_cpu_type(MXC_CPU_MX6SX) && !is_cpu_type(MXC_CPU_MX6UL) &&
317 !is_cpu_type(MXC_CPU_MX6SL))
Peng Fana462c342015-07-20 19:28:33 +0800318 mmdc1 = (struct mmdc_p_regs *)MMDC_P1_BASE_ADDR;
Tim Harveyfe0f7f72014-06-02 16:13:23 -0700319
Nikolay Dimitrov8a2bd212015-04-22 18:37:31 +0300320 /* Limit mem_speed for MX6D/MX6Q */
Tim Harveyfe0f7f72014-06-02 16:13:23 -0700321 if (is_cpu_type(MXC_CPU_MX6Q) || is_cpu_type(MXC_CPU_MX6D)) {
Nikolay Dimitrov8a2bd212015-04-22 18:37:31 +0300322 if (mem_speed > 1066)
323 mem_speed = 1066; /* 1066 MT/s */
324
Tim Harveyfe0f7f72014-06-02 16:13:23 -0700325 tcwl = 4;
326 }
Nikolay Dimitrov8a2bd212015-04-22 18:37:31 +0300327 /* Limit mem_speed for MX6S/MX6DL */
Tim Harveyfe0f7f72014-06-02 16:13:23 -0700328 else {
Nikolay Dimitrov8a2bd212015-04-22 18:37:31 +0300329 if (mem_speed > 800)
330 mem_speed = 800; /* 800 MT/s */
331
Tim Harveyfe0f7f72014-06-02 16:13:23 -0700332 tcwl = 3;
333 }
Nikolay Dimitrov8a2bd212015-04-22 18:37:31 +0300334
335 clock = mem_speed / 2;
336 /*
337 * Data rate of 1066 MT/s requires 533 MHz DDR3 clock, but MX6D/Q supports
338 * up to 528 MHz, so reduce the clock to fit chip specs
339 */
340 if (is_cpu_type(MXC_CPU_MX6Q) || is_cpu_type(MXC_CPU_MX6D)) {
341 if (clock > 528)
342 clock = 528; /* 528 MHz */
343 }
344
Nikita Kiryanov33689182014-09-07 18:58:11 +0300345 clkper = (1000 * 1000) / clock; /* pico seconds */
Tim Harveyfe0f7f72014-06-02 16:13:23 -0700346 todtlon = tcwl;
347 taxpd = tcwl;
348 tanpd = tcwl;
Tim Harveyfe0f7f72014-06-02 16:13:23 -0700349
Nikita Kiryanov33689182014-09-07 18:58:11 +0300350 switch (ddr3_cfg->density) {
Tim Harveyfe0f7f72014-06-02 16:13:23 -0700351 case 1: /* 1Gb per chip */
352 trfc = DIV_ROUND_UP(110000, clkper) - 1;
353 txs = DIV_ROUND_UP(120000, clkper) - 1;
354 break;
355 case 2: /* 2Gb per chip */
356 trfc = DIV_ROUND_UP(160000, clkper) - 1;
357 txs = DIV_ROUND_UP(170000, clkper) - 1;
358 break;
359 case 4: /* 4Gb per chip */
Peng Fan2cd8cd02015-08-17 16:10:59 +0800360 trfc = DIV_ROUND_UP(300000, clkper) - 1;
361 txs = DIV_ROUND_UP(310000, clkper) - 1;
Tim Harveyfe0f7f72014-06-02 16:13:23 -0700362 break;
363 case 8: /* 8Gb per chip */
364 trfc = DIV_ROUND_UP(350000, clkper) - 1;
365 txs = DIV_ROUND_UP(360000, clkper) - 1;
366 break;
367 default:
368 /* invalid density */
Nikita Kiryanov33689182014-09-07 18:58:11 +0300369 puts("invalid chip density\n");
Tim Harveyfe0f7f72014-06-02 16:13:23 -0700370 hang();
371 break;
372 }
373 txpr = txs;
374
Nikolay Dimitrov8a2bd212015-04-22 18:37:31 +0300375 switch (mem_speed) {
Tim Harveyfe0f7f72014-06-02 16:13:23 -0700376 case 800:
Masahiro Yamadac79cba32014-09-18 13:28:06 +0900377 txp = DIV_ROUND_UP(max(3 * clkper, 7500), clkper) - 1;
378 tcke = DIV_ROUND_UP(max(3 * clkper, 7500), clkper) - 1;
Nikita Kiryanov33689182014-09-07 18:58:11 +0300379 if (ddr3_cfg->pagesz == 1) {
Tim Harveyfe0f7f72014-06-02 16:13:23 -0700380 tfaw = DIV_ROUND_UP(40000, clkper) - 1;
Masahiro Yamadac79cba32014-09-18 13:28:06 +0900381 trrd = DIV_ROUND_UP(max(4 * clkper, 10000), clkper) - 1;
Tim Harveyfe0f7f72014-06-02 16:13:23 -0700382 } else {
383 tfaw = DIV_ROUND_UP(50000, clkper) - 1;
Masahiro Yamadac79cba32014-09-18 13:28:06 +0900384 trrd = DIV_ROUND_UP(max(4 * clkper, 10000), clkper) - 1;
Tim Harveyfe0f7f72014-06-02 16:13:23 -0700385 }
386 break;
387 case 1066:
Masahiro Yamadac79cba32014-09-18 13:28:06 +0900388 txp = DIV_ROUND_UP(max(3 * clkper, 7500), clkper) - 1;
389 tcke = DIV_ROUND_UP(max(3 * clkper, 5625), clkper) - 1;
Nikita Kiryanov33689182014-09-07 18:58:11 +0300390 if (ddr3_cfg->pagesz == 1) {
Tim Harveyfe0f7f72014-06-02 16:13:23 -0700391 tfaw = DIV_ROUND_UP(37500, clkper) - 1;
Masahiro Yamadac79cba32014-09-18 13:28:06 +0900392 trrd = DIV_ROUND_UP(max(4 * clkper, 7500), clkper) - 1;
Tim Harveyfe0f7f72014-06-02 16:13:23 -0700393 } else {
394 tfaw = DIV_ROUND_UP(50000, clkper) - 1;
Masahiro Yamadac79cba32014-09-18 13:28:06 +0900395 trrd = DIV_ROUND_UP(max(4 * clkper, 10000), clkper) - 1;
Tim Harveyfe0f7f72014-06-02 16:13:23 -0700396 }
397 break;
Tim Harveyfe0f7f72014-06-02 16:13:23 -0700398 default:
Nikita Kiryanov33689182014-09-07 18:58:11 +0300399 puts("invalid memory speed\n");
Tim Harveyfe0f7f72014-06-02 16:13:23 -0700400 hang();
401 break;
402 }
Masahiro Yamadac79cba32014-09-18 13:28:06 +0900403 txpdll = DIV_ROUND_UP(max(10 * clkper, 24000), clkper) - 1;
404 tcksre = DIV_ROUND_UP(max(5 * clkper, 10000), clkper);
Tim Harveyfe0f7f72014-06-02 16:13:23 -0700405 taonpd = DIV_ROUND_UP(2000, clkper) - 1;
Nikita Kiryanov33689182014-09-07 18:58:11 +0300406 tcksrx = tcksre;
Tim Harveyfe0f7f72014-06-02 16:13:23 -0700407 taofpd = taonpd;
Nikita Kiryanov33689182014-09-07 18:58:11 +0300408 twr = DIV_ROUND_UP(15000, clkper) - 1;
Masahiro Yamadac79cba32014-09-18 13:28:06 +0900409 tmrd = DIV_ROUND_UP(max(12 * clkper, 15000), clkper) - 1;
Nikita Kiryanov33689182014-09-07 18:58:11 +0300410 trc = DIV_ROUND_UP(ddr3_cfg->trcmin, clkper / 10) - 1;
411 tras = DIV_ROUND_UP(ddr3_cfg->trasmin, clkper / 10) - 1;
412 tcl = DIV_ROUND_UP(ddr3_cfg->trcd, clkper / 10) - 3;
413 trp = DIV_ROUND_UP(ddr3_cfg->trcd, clkper / 10) - 1;
Masahiro Yamadac79cba32014-09-18 13:28:06 +0900414 twtr = ROUND(max(4 * clkper, 7500) / clkper, 1) - 1;
Tim Harveyfe0f7f72014-06-02 16:13:23 -0700415 trcd = trp;
Tim Harveyfe0f7f72014-06-02 16:13:23 -0700416 trtp = twtr;
Nikita Kiryanov07ee9272014-08-20 15:08:58 +0300417 cs0_end = 4 * sysinfo->cs_density - 1;
Nikita Kiryanov33689182014-09-07 18:58:11 +0300418
419 debug("density:%d Gb (%d Gb per chip)\n",
420 sysinfo->cs_density, ddr3_cfg->density);
Tim Harveyfe0f7f72014-06-02 16:13:23 -0700421 debug("clock: %dMHz (%d ps)\n", clock, clkper);
Nikolay Dimitrov8a2bd212015-04-22 18:37:31 +0300422 debug("memspd:%d\n", mem_speed);
Tim Harveyfe0f7f72014-06-02 16:13:23 -0700423 debug("tcke=%d\n", tcke);
424 debug("tcksrx=%d\n", tcksrx);
425 debug("tcksre=%d\n", tcksre);
426 debug("taofpd=%d\n", taofpd);
427 debug("taonpd=%d\n", taonpd);
428 debug("todtlon=%d\n", todtlon);
429 debug("tanpd=%d\n", tanpd);
430 debug("taxpd=%d\n", taxpd);
431 debug("trfc=%d\n", trfc);
432 debug("txs=%d\n", txs);
433 debug("txp=%d\n", txp);
434 debug("txpdll=%d\n", txpdll);
435 debug("tfaw=%d\n", tfaw);
436 debug("tcl=%d\n", tcl);
437 debug("trcd=%d\n", trcd);
438 debug("trp=%d\n", trp);
439 debug("trc=%d\n", trc);
440 debug("tras=%d\n", tras);
441 debug("twr=%d\n", twr);
442 debug("tmrd=%d\n", tmrd);
443 debug("tcwl=%d\n", tcwl);
444 debug("tdllk=%d\n", tdllk);
445 debug("trtp=%d\n", trtp);
446 debug("twtr=%d\n", twtr);
447 debug("trrd=%d\n", trrd);
448 debug("txpr=%d\n", txpr);
Nikita Kiryanov33689182014-09-07 18:58:11 +0300449 debug("cs0_end=%d\n", cs0_end);
450 debug("ncs=%d\n", sysinfo->ncs);
451 debug("Rtt_wr=%d\n", sysinfo->rtt_wr);
452 debug("Rtt_nom=%d\n", sysinfo->rtt_nom);
453 debug("SRT=%d\n", ddr3_cfg->SRT);
Tim Harveyfe0f7f72014-06-02 16:13:23 -0700454 debug("tcl=%d\n", tcl);
455 debug("twr=%d\n", twr);
456
457 /*
458 * board-specific configuration:
459 * These values are determined empirically and vary per board layout
460 * see:
461 * appnote, ddr3 spreadsheet
462 */
Nikita Kiryanov33689182014-09-07 18:58:11 +0300463 mmdc0->mpwldectrl0 = calib->p0_mpwldectrl0;
464 mmdc0->mpwldectrl1 = calib->p0_mpwldectrl1;
465 mmdc0->mpdgctrl0 = calib->p0_mpdgctrl0;
466 mmdc0->mpdgctrl1 = calib->p0_mpdgctrl1;
467 mmdc0->mprddlctl = calib->p0_mprddlctl;
468 mmdc0->mpwrdlctl = calib->p0_mpwrdlctl;
469 if (sysinfo->dsize > 1) {
Peng Fand9efd472014-12-30 17:24:01 +0800470 MMDC1(mpwldectrl0, calib->p1_mpwldectrl0);
471 MMDC1(mpwldectrl1, calib->p1_mpwldectrl1);
472 MMDC1(mpdgctrl0, calib->p1_mpdgctrl0);
473 MMDC1(mpdgctrl1, calib->p1_mpdgctrl1);
474 MMDC1(mprddlctl, calib->p1_mprddlctl);
475 MMDC1(mpwrdlctl, calib->p1_mpwrdlctl);
Tim Harveyfe0f7f72014-06-02 16:13:23 -0700476 }
477
478 /* Read data DQ Byte0-3 delay */
Nikita Kiryanov33689182014-09-07 18:58:11 +0300479 mmdc0->mprddqby0dl = 0x33333333;
480 mmdc0->mprddqby1dl = 0x33333333;
481 if (sysinfo->dsize > 0) {
482 mmdc0->mprddqby2dl = 0x33333333;
483 mmdc0->mprddqby3dl = 0x33333333;
Tim Harveyfe0f7f72014-06-02 16:13:23 -0700484 }
Nikita Kiryanov33689182014-09-07 18:58:11 +0300485
486 if (sysinfo->dsize > 1) {
Peng Fand9efd472014-12-30 17:24:01 +0800487 MMDC1(mprddqby0dl, 0x33333333);
488 MMDC1(mprddqby1dl, 0x33333333);
489 MMDC1(mprddqby2dl, 0x33333333);
490 MMDC1(mprddqby3dl, 0x33333333);
Tim Harveyfe0f7f72014-06-02 16:13:23 -0700491 }
492
493 /* MMDC Termination: rtt_nom:2 RZQ/2(120ohm), rtt_nom:1 RZQ/4(60ohm) */
Nikita Kiryanov33689182014-09-07 18:58:11 +0300494 val = (sysinfo->rtt_nom == 2) ? 0x00011117 : 0x00022227;
495 mmdc0->mpodtctrl = val;
496 if (sysinfo->dsize > 1)
Peng Fand9efd472014-12-30 17:24:01 +0800497 MMDC1(mpodtctrl, val);
Tim Harveyfe0f7f72014-06-02 16:13:23 -0700498
499 /* complete calibration */
Nikita Kiryanov33689182014-09-07 18:58:11 +0300500 val = (1 << 11); /* Force measurement on delay-lines */
501 mmdc0->mpmur0 = val;
502 if (sysinfo->dsize > 1)
Peng Fand9efd472014-12-30 17:24:01 +0800503 MMDC1(mpmur0, val);
Tim Harveyfe0f7f72014-06-02 16:13:23 -0700504
505 /* Step 1: configuration request */
506 mmdc0->mdscr = (u32)(1 << 15); /* config request */
507
508 /* Step 2: Timing configuration */
Nikita Kiryanov33689182014-09-07 18:58:11 +0300509 mmdc0->mdcfg0 = (trfc << 24) | (txs << 16) | (txp << 13) |
510 (txpdll << 9) | (tfaw << 4) | tcl;
511 mmdc0->mdcfg1 = (trcd << 29) | (trp << 26) | (trc << 21) |
512 (tras << 16) | (1 << 15) /* trpa */ |
513 (twr << 9) | (tmrd << 5) | tcwl;
514 mmdc0->mdcfg2 = (tdllk << 16) | (trtp << 6) | (twtr << 3) | trrd;
515 mmdc0->mdotc = (taofpd << 27) | (taonpd << 24) | (tanpd << 20) |
516 (taxpd << 16) | (todtlon << 12) | (todt_idle_off << 4);
517 mmdc0->mdasp = cs0_end; /* CS addressing */
Tim Harveyfe0f7f72014-06-02 16:13:23 -0700518
519 /* Step 3: Configure DDR type */
Nikita Kiryanov33689182014-09-07 18:58:11 +0300520 mmdc0->mdmisc = (sysinfo->cs1_mirror << 19) | (sysinfo->walat << 16) |
521 (sysinfo->bi_on << 12) | (sysinfo->mif3_mode << 9) |
522 (sysinfo->ralat << 6);
Tim Harveyfe0f7f72014-06-02 16:13:23 -0700523
524 /* Step 4: Configure delay while leaving reset */
Nikita Kiryanov33689182014-09-07 18:58:11 +0300525 mmdc0->mdor = (txpr << 16) | (sysinfo->sde_to_rst << 8) |
526 (sysinfo->rst_to_cke << 0);
Tim Harveyfe0f7f72014-06-02 16:13:23 -0700527
528 /* Step 5: Configure DDR physical parameters (density and burst len) */
Nikita Kiryanov33689182014-09-07 18:58:11 +0300529 coladdr = ddr3_cfg->coladdr;
530 if (ddr3_cfg->coladdr == 8) /* 8-bit COL is 0x3 */
Marek Vasutb299ab72014-08-04 01:47:10 +0200531 coladdr += 4;
Nikita Kiryanov33689182014-09-07 18:58:11 +0300532 else if (ddr3_cfg->coladdr == 12) /* 12-bit COL is 0x4 */
Marek Vasutb299ab72014-08-04 01:47:10 +0200533 coladdr += 1;
Nikita Kiryanov33689182014-09-07 18:58:11 +0300534 mmdc0->mdctl = (ddr3_cfg->rowaddr - 11) << 24 | /* ROW */
535 (coladdr - 9) << 20 | /* COL */
536 (1 << 19) | /* Burst Length = 8 for DDR3 */
537 (sysinfo->dsize << 16); /* DDR data bus size */
Tim Harveyfe0f7f72014-06-02 16:13:23 -0700538
539 /* Step 6: Perform ZQ calibration */
Nikita Kiryanov33689182014-09-07 18:58:11 +0300540 val = 0xa1390001; /* one-time HW ZQ calib */
541 mmdc0->mpzqhwctrl = val;
542 if (sysinfo->dsize > 1)
Peng Fand9efd472014-12-30 17:24:01 +0800543 MMDC1(mpzqhwctrl, val);
Tim Harveyfe0f7f72014-06-02 16:13:23 -0700544
545 /* Step 7: Enable MMDC with desired chip select */
Nikita Kiryanov33689182014-09-07 18:58:11 +0300546 mmdc0->mdctl |= (1 << 31) | /* SDE_0 for CS0 */
547 ((sysinfo->ncs == 2) ? 1 : 0) << 30; /* SDE_1 for CS1 */
Tim Harveyfe0f7f72014-06-02 16:13:23 -0700548
549 /* Step 8: Write Mode Registers to Init DDR3 devices */
Nikita Kiryanov33689182014-09-07 18:58:11 +0300550 for (cs = 0; cs < sysinfo->ncs; cs++) {
Tim Harveyfe0f7f72014-06-02 16:13:23 -0700551 /* MR2 */
Nikita Kiryanov33689182014-09-07 18:58:11 +0300552 val = (sysinfo->rtt_wr & 3) << 9 | (ddr3_cfg->SRT & 1) << 7 |
Tim Harveyfe0f7f72014-06-02 16:13:23 -0700553 ((tcwl - 3) & 3) << 3;
Tim Harvey78c5a182015-04-03 16:52:52 -0700554 debug("MR2 CS%d: 0x%08x\n", cs, (u32)MR(val, 2, 3, cs));
Nikita Kiryanov33689182014-09-07 18:58:11 +0300555 mmdc0->mdscr = MR(val, 2, 3, cs);
Tim Harveyfe0f7f72014-06-02 16:13:23 -0700556 /* MR3 */
Tim Harvey78c5a182015-04-03 16:52:52 -0700557 debug("MR3 CS%d: 0x%08x\n", cs, (u32)MR(0, 3, 3, cs));
Nikita Kiryanov33689182014-09-07 18:58:11 +0300558 mmdc0->mdscr = MR(0, 3, 3, cs);
Tim Harveyfe0f7f72014-06-02 16:13:23 -0700559 /* MR1 */
Nikita Kiryanov33689182014-09-07 18:58:11 +0300560 val = ((sysinfo->rtt_nom & 1) ? 1 : 0) << 2 |
561 ((sysinfo->rtt_nom & 2) ? 1 : 0) << 6;
Tim Harvey78c5a182015-04-03 16:52:52 -0700562 debug("MR1 CS%d: 0x%08x\n", cs, (u32)MR(val, 1, 3, cs));
Nikita Kiryanov33689182014-09-07 18:58:11 +0300563 mmdc0->mdscr = MR(val, 1, 3, cs);
564 /* MR0 */
565 val = ((tcl - 1) << 4) | /* CAS */
Tim Harveyfe0f7f72014-06-02 16:13:23 -0700566 (1 << 8) | /* DLL Reset */
Tim Harvey3625fd62015-05-18 07:07:02 -0700567 ((twr - 3) << 9) | /* Write Recovery */
568 (sysinfo->pd_fast_exit << 12); /* Precharge PD PLL on */
Tim Harvey78c5a182015-04-03 16:52:52 -0700569 debug("MR0 CS%d: 0x%08x\n", cs, (u32)MR(val, 0, 3, cs));
Nikita Kiryanov33689182014-09-07 18:58:11 +0300570 mmdc0->mdscr = MR(val, 0, 3, cs);
Tim Harveyfe0f7f72014-06-02 16:13:23 -0700571 /* ZQ calibration */
Nikita Kiryanov33689182014-09-07 18:58:11 +0300572 val = (1 << 10);
573 mmdc0->mdscr = MR(val, 0, 4, cs);
Tim Harveyfe0f7f72014-06-02 16:13:23 -0700574 }
575
576 /* Step 10: Power down control and self-refresh */
Nikita Kiryanov33689182014-09-07 18:58:11 +0300577 mmdc0->mdpdc = (tcke & 0x7) << 16 |
578 5 << 12 | /* PWDT_1: 256 cycles */
579 5 << 8 | /* PWDT_0: 256 cycles */
580 1 << 6 | /* BOTH_CS_PD */
581 (tcksrx & 0x7) << 3 |
582 (tcksre & 0x7);
Tim Harvey78c5a182015-04-03 16:52:52 -0700583 if (!sysinfo->pd_fast_exit)
584 mmdc0->mdpdc |= (1 << 7); /* SLOW_PD */
Nikita Kiryanov06a51b82014-08-20 15:08:56 +0300585 mmdc0->mapsr = 0x00001006; /* ADOPT power down enabled */
Tim Harveyfe0f7f72014-06-02 16:13:23 -0700586
587 /* Step 11: Configure ZQ calibration: one-time and periodic 1ms */
Nikita Kiryanov33689182014-09-07 18:58:11 +0300588 val = 0xa1390003;
589 mmdc0->mpzqhwctrl = val;
590 if (sysinfo->dsize > 1)
Peng Fand9efd472014-12-30 17:24:01 +0800591 MMDC1(mpzqhwctrl, val);
Tim Harveyfe0f7f72014-06-02 16:13:23 -0700592
593 /* Step 12: Configure and activate periodic refresh */
Nikita Kiryanov33689182014-09-07 18:58:11 +0300594 mmdc0->mdref = (1 << 14) | /* REF_SEL: Periodic refresh cycle: 32kHz */
595 (7 << 11); /* REFR: Refresh Rate - 8 refreshes */
Tim Harveyfe0f7f72014-06-02 16:13:23 -0700596
597 /* Step 13: Deassert config request - init complete */
Nikita Kiryanov33689182014-09-07 18:58:11 +0300598 mmdc0->mdscr = 0x00000000;
Tim Harveyfe0f7f72014-06-02 16:13:23 -0700599
600 /* wait for auto-ZQ calibration to complete */
601 mdelay(1);
602}