blob: 4396880b7482d87c39702c23952a626a61d961ba [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Tim Harvey8ab871b2014-06-02 16:13:23 -07002/*
3 * Copyright (C) 2014 Gateworks Corporation
4 * Author: Tim Harvey <tharvey@gateworks.com>
Tim Harvey8ab871b2014-06-02 16:13:23 -07005 */
6
7#include <common.h>
Simon Glassf11478f2019-12-28 10:45:07 -07008#include <hang.h>
Tim Harvey8ab871b2014-06-02 16:13:23 -07009#include <linux/types.h>
Peng Fanda7ada02015-08-17 16:11:04 +080010#include <asm/arch/clock.h>
Tim Harvey8ab871b2014-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>
Marek Vasut23023572016-03-02 14:49:51 +010015#include <wait_bit.h>
Tim Harvey8ab871b2014-06-02 16:13:23 -070016
Eric Nelsonc448df72016-10-30 16:33:50 -070017#if defined(CONFIG_MX6_DDRCAL)
Marek Vasutab257ed2015-12-16 15:40:06 +010018static void reset_read_data_fifos(void)
19{
20 struct mmdc_p_regs *mmdc0 = (struct mmdc_p_regs *)MMDC_P0_BASE_ADDR;
21
22 /* Reset data FIFOs twice. */
23 setbits_le32(&mmdc0->mpdgctrl0, 1 << 31);
Álvaro Fernández Rojas918de032018-01-23 17:14:55 +010024 wait_for_bit_le32(&mmdc0->mpdgctrl0, 1 << 31, 0, 100, 0);
Marek Vasutab257ed2015-12-16 15:40:06 +010025
26 setbits_le32(&mmdc0->mpdgctrl0, 1 << 31);
Álvaro Fernández Rojas918de032018-01-23 17:14:55 +010027 wait_for_bit_le32(&mmdc0->mpdgctrl0, 1 << 31, 0, 100, 0);
Marek Vasutab257ed2015-12-16 15:40:06 +010028}
29
30static void precharge_all(const bool cs0_enable, const bool cs1_enable)
31{
32 struct mmdc_p_regs *mmdc0 = (struct mmdc_p_regs *)MMDC_P0_BASE_ADDR;
33
34 /*
35 * Issue the Precharge-All command to the DDR device for both
36 * chip selects. Note, CON_REQ bit should also remain set. If
37 * only using one chip select, then precharge only the desired
38 * chip select.
39 */
40 if (cs0_enable) { /* CS0 */
41 writel(0x04008050, &mmdc0->mdscr);
Álvaro Fernández Rojas918de032018-01-23 17:14:55 +010042 wait_for_bit_le32(&mmdc0->mdscr, 1 << 14, 1, 100, 0);
Marek Vasutab257ed2015-12-16 15:40:06 +010043 }
44
45 if (cs1_enable) { /* CS1 */
46 writel(0x04008058, &mmdc0->mdscr);
Álvaro Fernández Rojas918de032018-01-23 17:14:55 +010047 wait_for_bit_le32(&mmdc0->mdscr, 1 << 14, 1, 100, 0);
Marek Vasutab257ed2015-12-16 15:40:06 +010048 }
49}
50
51static void force_delay_measurement(int bus_size)
52{
53 struct mmdc_p_regs *mmdc0 = (struct mmdc_p_regs *)MMDC_P0_BASE_ADDR;
54 struct mmdc_p_regs *mmdc1 = (struct mmdc_p_regs *)MMDC_P1_BASE_ADDR;
55
56 writel(0x800, &mmdc0->mpmur0);
57 if (bus_size == 0x2)
58 writel(0x800, &mmdc1->mpmur0);
59}
60
61static void modify_dg_result(u32 *reg_st0, u32 *reg_st1, u32 *reg_ctrl)
62{
63 u32 dg_tmp_val, dg_dl_abs_offset, dg_hc_del, val_ctrl;
64
65 /*
66 * DQS gating absolute offset should be modified from reflecting
67 * (HW_DG_LOWx + HW_DG_UPx)/2 to reflecting (HW_DG_UPx - 0x80)
68 */
69
70 val_ctrl = readl(reg_ctrl);
71 val_ctrl &= 0xf0000000;
72
73 dg_tmp_val = ((readl(reg_st0) & 0x07ff0000) >> 16) - 0xc0;
74 dg_dl_abs_offset = dg_tmp_val & 0x7f;
75 dg_hc_del = (dg_tmp_val & 0x780) << 1;
76
77 val_ctrl |= dg_dl_abs_offset + dg_hc_del;
78
79 dg_tmp_val = ((readl(reg_st1) & 0x07ff0000) >> 16) - 0xc0;
80 dg_dl_abs_offset = dg_tmp_val & 0x7f;
81 dg_hc_del = (dg_tmp_val & 0x780) << 1;
82
83 val_ctrl |= (dg_dl_abs_offset + dg_hc_del) << 16;
84
85 writel(val_ctrl, reg_ctrl);
86}
87
Marek Vasuta694dac2018-03-30 03:04:43 +020088static void correct_mpwldectr_result(void *reg)
89{
90 /* Limit is 200/256 of CK, which is WL_HC_DELx | 0x48. */
91 const unsigned int limit = 0x148;
92 u32 val = readl(reg);
93 u32 old = val;
94
95 if ((val & 0x17f) > limit)
96 val &= 0xffff << 16;
97
98 if (((val >> 16) & 0x17f) > limit)
99 val &= 0xffff;
100
101 if (old != val)
102 writel(val, reg);
103}
104
Eric Nelsona09d68a2016-10-30 16:33:48 -0700105int mmdc_do_write_level_calibration(struct mx6_ddr_sysinfo const *sysinfo)
Marek Vasutab257ed2015-12-16 15:40:06 +0100106{
107 struct mmdc_p_regs *mmdc0 = (struct mmdc_p_regs *)MMDC_P0_BASE_ADDR;
108 struct mmdc_p_regs *mmdc1 = (struct mmdc_p_regs *)MMDC_P1_BASE_ADDR;
109 u32 esdmisc_val, zq_val;
110 u32 errors = 0;
Eric Nelsona09d68a2016-10-30 16:33:48 -0700111 u32 ldectrl[4] = {0};
Marek Vasutab257ed2015-12-16 15:40:06 +0100112 u32 ddr_mr1 = 0x4;
Eric Nelsona09d68a2016-10-30 16:33:48 -0700113 u32 rwalat_max;
Marek Vasutab257ed2015-12-16 15:40:06 +0100114
115 /*
116 * Stash old values in case calibration fails,
117 * we need to restore them
118 */
119 ldectrl[0] = readl(&mmdc0->mpwldectrl0);
120 ldectrl[1] = readl(&mmdc0->mpwldectrl1);
Eric Nelsona09d68a2016-10-30 16:33:48 -0700121 if (sysinfo->dsize == 2) {
122 ldectrl[2] = readl(&mmdc1->mpwldectrl0);
123 ldectrl[3] = readl(&mmdc1->mpwldectrl1);
124 }
Marek Vasutab257ed2015-12-16 15:40:06 +0100125
126 /* disable DDR logic power down timer */
127 clrbits_le32(&mmdc0->mdpdc, 0xff00);
128
129 /* disable Adopt power down timer */
130 setbits_le32(&mmdc0->mapsr, 0x1);
131
132 debug("Starting write leveling calibration.\n");
133
134 /*
135 * 2. disable auto refresh and ZQ calibration
136 * before proceeding with Write Leveling calibration
137 */
138 esdmisc_val = readl(&mmdc0->mdref);
139 writel(0x0000C000, &mmdc0->mdref);
140 zq_val = readl(&mmdc0->mpzqhwctrl);
141 writel(zq_val & ~0x3, &mmdc0->mpzqhwctrl);
142
143 /* 3. increase walat and ralat to maximum */
Eric Nelsona09d68a2016-10-30 16:33:48 -0700144 rwalat_max = (1 << 6) | (1 << 7) | (1 << 8) | (1 << 16) | (1 << 17);
145 setbits_le32(&mmdc0->mdmisc, rwalat_max);
146 if (sysinfo->dsize == 2)
147 setbits_le32(&mmdc1->mdmisc, rwalat_max);
Marek Vasutab257ed2015-12-16 15:40:06 +0100148 /*
149 * 4 & 5. Configure the external DDR device to enter write-leveling
150 * mode through Load Mode Register command.
151 * Register setting:
152 * Bits[31:16] MR1 value (0x0080 write leveling enable)
153 * Bit[9] set WL_EN to enable MMDC DQS output
154 * Bits[6:4] set CMD bits for Load Mode Register programming
155 * Bits[2:0] set CMD_BA to 0x1 for DDR MR1 programming
156 */
157 writel(0x00808231, &mmdc0->mdscr);
158
159 /* 6. Activate automatic calibration by setting MPWLGCR[HW_WL_EN] */
160 writel(0x00000001, &mmdc0->mpwlgcr);
161
162 /*
163 * 7. Upon completion of this process the MMDC de-asserts
164 * the MPWLGCR[HW_WL_EN]
165 */
Álvaro Fernández Rojas918de032018-01-23 17:14:55 +0100166 wait_for_bit_le32(&mmdc0->mpwlgcr, 1 << 0, 0, 100, 0);
Marek Vasutab257ed2015-12-16 15:40:06 +0100167
168 /*
169 * 8. check for any errors: check both PHYs for x64 configuration,
170 * if x32, check only PHY0
171 */
172 if (readl(&mmdc0->mpwlgcr) & 0x00000F00)
173 errors |= 1;
Eric Nelsona09d68a2016-10-30 16:33:48 -0700174 if (sysinfo->dsize == 2)
175 if (readl(&mmdc1->mpwlgcr) & 0x00000F00)
176 errors |= 2;
Marek Vasutab257ed2015-12-16 15:40:06 +0100177
178 debug("Ending write leveling calibration. Error mask: 0x%x\n", errors);
179
180 /* check to see if cal failed */
181 if ((readl(&mmdc0->mpwldectrl0) == 0x001F001F) &&
182 (readl(&mmdc0->mpwldectrl1) == 0x001F001F) &&
Eric Nelsona09d68a2016-10-30 16:33:48 -0700183 ((sysinfo->dsize < 2) ||
184 ((readl(&mmdc1->mpwldectrl0) == 0x001F001F) &&
185 (readl(&mmdc1->mpwldectrl1) == 0x001F001F)))) {
Marek Vasutab257ed2015-12-16 15:40:06 +0100186 debug("Cal seems to have soft-failed due to memory not supporting write leveling on all channels. Restoring original write leveling values.\n");
187 writel(ldectrl[0], &mmdc0->mpwldectrl0);
188 writel(ldectrl[1], &mmdc0->mpwldectrl1);
Eric Nelsona09d68a2016-10-30 16:33:48 -0700189 if (sysinfo->dsize == 2) {
190 writel(ldectrl[2], &mmdc1->mpwldectrl0);
191 writel(ldectrl[3], &mmdc1->mpwldectrl1);
192 }
Marek Vasutab257ed2015-12-16 15:40:06 +0100193 errors |= 4;
194 }
195
Marek Vasuta694dac2018-03-30 03:04:43 +0200196 correct_mpwldectr_result(&mmdc0->mpwldectrl0);
197 correct_mpwldectr_result(&mmdc0->mpwldectrl1);
198 if (sysinfo->dsize == 2) {
199 correct_mpwldectr_result(&mmdc1->mpwldectrl0);
200 correct_mpwldectr_result(&mmdc1->mpwldectrl1);
201 }
202
Marek Vasutab257ed2015-12-16 15:40:06 +0100203 /*
204 * User should issue MRS command to exit write leveling mode
205 * through Load Mode Register command
206 * Register setting:
207 * Bits[31:16] MR1 value "ddr_mr1" value from initialization
208 * Bit[9] clear WL_EN to disable MMDC DQS output
209 * Bits[6:4] set CMD bits for Load Mode Register programming
210 * Bits[2:0] set CMD_BA to 0x1 for DDR MR1 programming
211 */
212 writel((ddr_mr1 << 16) + 0x8031, &mmdc0->mdscr);
213
214 /* re-enable auto refresh and zq cal */
215 writel(esdmisc_val, &mmdc0->mdref);
216 writel(zq_val, &mmdc0->mpzqhwctrl);
217
Marek Vasut1b8e5dc2019-11-26 09:34:49 +0100218 debug("\tMMDC_MPWLDECTRL0 after write level cal: 0x%08x\n",
Marek Vasutab257ed2015-12-16 15:40:06 +0100219 readl(&mmdc0->mpwldectrl0));
Marek Vasut1b8e5dc2019-11-26 09:34:49 +0100220 debug("\tMMDC_MPWLDECTRL1 after write level cal: 0x%08x\n",
Marek Vasutab257ed2015-12-16 15:40:06 +0100221 readl(&mmdc0->mpwldectrl1));
Eric Nelsona09d68a2016-10-30 16:33:48 -0700222 if (sysinfo->dsize == 2) {
Marek Vasut1b8e5dc2019-11-26 09:34:49 +0100223 debug("\tMMDC_MPWLDECTRL0 after write level cal: 0x%08x\n",
Eric Nelsona09d68a2016-10-30 16:33:48 -0700224 readl(&mmdc1->mpwldectrl0));
Marek Vasut1b8e5dc2019-11-26 09:34:49 +0100225 debug("\tMMDC_MPWLDECTRL1 after write level cal: 0x%08x\n",
Eric Nelsona09d68a2016-10-30 16:33:48 -0700226 readl(&mmdc1->mpwldectrl1));
227 }
Marek Vasutab257ed2015-12-16 15:40:06 +0100228
229 /* We must force a readback of these values, to get them to stick */
230 readl(&mmdc0->mpwldectrl0);
231 readl(&mmdc0->mpwldectrl1);
Eric Nelsona09d68a2016-10-30 16:33:48 -0700232 if (sysinfo->dsize == 2) {
233 readl(&mmdc1->mpwldectrl0);
234 readl(&mmdc1->mpwldectrl1);
235 }
Marek Vasutab257ed2015-12-16 15:40:06 +0100236
237 /* enable DDR logic power down timer: */
238 setbits_le32(&mmdc0->mdpdc, 0x00005500);
239
240 /* Enable Adopt power down timer: */
241 clrbits_le32(&mmdc0->mapsr, 0x1);
242
243 /* Clear CON_REQ */
244 writel(0, &mmdc0->mdscr);
245
246 return errors;
247}
248
Marek Vasut49fad0e2019-11-26 09:34:50 +0100249static void mmdc_set_sdqs(bool set)
250{
Marek Vasut04ab1612019-11-26 09:34:52 +0100251 struct mx6dq_iomux_ddr_regs *mx6dq_ddr_iomux =
Marek Vasut49fad0e2019-11-26 09:34:50 +0100252 (struct mx6dq_iomux_ddr_regs *)MX6DQ_IOM_DDR_BASE;
Marek Vasut04ab1612019-11-26 09:34:52 +0100253 struct mx6sx_iomux_ddr_regs *mx6sx_ddr_iomux =
254 (struct mx6sx_iomux_ddr_regs *)MX6SX_IOM_DDR_BASE;
255 int i, sdqs_cnt;
256 u32 sdqs;
257
258 if (is_mx6sx()) {
259 sdqs = (u32)(&mx6sx_ddr_iomux->dram_sdqs0);
260 sdqs_cnt = 2;
261 } else { /* MX6DQ */
262 sdqs = (u32)(&mx6dq_ddr_iomux->dram_sdqs0);
263 sdqs_cnt = 8;
264 }
Marek Vasut49fad0e2019-11-26 09:34:50 +0100265
Marek Vasut04ab1612019-11-26 09:34:52 +0100266 for (i = 0; i < sdqs_cnt; i++) {
Marek Vasut85edd7f2019-11-26 09:34:51 +0100267 if (set)
268 setbits_le32(sdqs + (4 * i), 0x7000);
269 else
270 clrbits_le32(sdqs + (4 * i), 0x7000);
Marek Vasut49fad0e2019-11-26 09:34:50 +0100271 }
272}
273
Eric Nelsona09d68a2016-10-30 16:33:48 -0700274int mmdc_do_dqs_calibration(struct mx6_ddr_sysinfo const *sysinfo)
Marek Vasutab257ed2015-12-16 15:40:06 +0100275{
276 struct mmdc_p_regs *mmdc0 = (struct mmdc_p_regs *)MMDC_P0_BASE_ADDR;
277 struct mmdc_p_regs *mmdc1 = (struct mmdc_p_regs *)MMDC_P1_BASE_ADDR;
Marek Vasutab257ed2015-12-16 15:40:06 +0100278 bool cs0_enable;
279 bool cs1_enable;
280 bool cs0_enable_initial;
281 bool cs1_enable_initial;
282 u32 esdmisc_val;
Marek Vasutab257ed2015-12-16 15:40:06 +0100283 u32 temp_ref;
284 u32 pddword = 0x00ffff00; /* best so far, place into MPPDCMPR1 */
285 u32 errors = 0;
286 u32 initdelay = 0x40404040;
287
288 /* check to see which chip selects are enabled */
289 cs0_enable_initial = readl(&mmdc0->mdctl) & 0x80000000;
290 cs1_enable_initial = readl(&mmdc0->mdctl) & 0x40000000;
291
292 /* disable DDR logic power down timer: */
293 clrbits_le32(&mmdc0->mdpdc, 0xff00);
294
295 /* disable Adopt power down timer: */
296 setbits_le32(&mmdc0->mapsr, 0x1);
297
298 /* set DQS pull ups */
Marek Vasut49fad0e2019-11-26 09:34:50 +0100299 mmdc_set_sdqs(true);
Marek Vasutab257ed2015-12-16 15:40:06 +0100300
301 /* Save old RALAT and WALAT values */
302 esdmisc_val = readl(&mmdc0->mdmisc);
303
304 setbits_le32(&mmdc0->mdmisc,
305 (1 << 6) | (1 << 7) | (1 << 8) | (1 << 16) | (1 << 17));
306
307 /* Disable auto refresh before proceeding with calibration */
308 temp_ref = readl(&mmdc0->mdref);
309 writel(0x0000c000, &mmdc0->mdref);
310
311 /*
312 * Per the ref manual, issue one refresh cycle MDSCR[CMD]= 0x2,
313 * this also sets the CON_REQ bit.
314 */
315 if (cs0_enable_initial)
316 writel(0x00008020, &mmdc0->mdscr);
317 if (cs1_enable_initial)
318 writel(0x00008028, &mmdc0->mdscr);
319
320 /* poll to make sure the con_ack bit was asserted */
Álvaro Fernández Rojas918de032018-01-23 17:14:55 +0100321 wait_for_bit_le32(&mmdc0->mdscr, 1 << 14, 1, 100, 0);
Marek Vasutab257ed2015-12-16 15:40:06 +0100322
323 /*
324 * Check MDMISC register CALIB_PER_CS to see which CS calibration
325 * is targeted to (under normal cases, it should be cleared
326 * as this is the default value, indicating calibration is directed
327 * to CS0).
328 * Disable the other chip select not being target for calibration
329 * to avoid any potential issues. This will get re-enabled at end
330 * of calibration.
331 */
332 if ((readl(&mmdc0->mdmisc) & 0x00100000) == 0)
333 clrbits_le32(&mmdc0->mdctl, 1 << 30); /* clear SDE_1 */
334 else
335 clrbits_le32(&mmdc0->mdctl, 1 << 31); /* clear SDE_0 */
336
337 /*
338 * Check to see which chip selects are now enabled for
339 * the remainder of the calibration.
340 */
341 cs0_enable = readl(&mmdc0->mdctl) & 0x80000000;
342 cs1_enable = readl(&mmdc0->mdctl) & 0x40000000;
343
Marek Vasutab257ed2015-12-16 15:40:06 +0100344 precharge_all(cs0_enable, cs1_enable);
345
346 /* Write the pre-defined value into MPPDCMPR1 */
347 writel(pddword, &mmdc0->mppdcmpr1);
348
349 /*
350 * Issue a write access to the external DDR device by setting
351 * the bit SW_DUMMY_WR (bit 0) in the MPSWDAR0 and then poll
352 * this bit until it clears to indicate completion of the write access.
353 */
354 setbits_le32(&mmdc0->mpswdar0, 1);
Álvaro Fernández Rojas918de032018-01-23 17:14:55 +0100355 wait_for_bit_le32(&mmdc0->mpswdar0, 1 << 0, 0, 100, 0);
Marek Vasutab257ed2015-12-16 15:40:06 +0100356
357 /* Set the RD_DL_ABS# bits to their default values
358 * (will be calibrated later in the read delay-line calibration).
359 * Both PHYs for x64 configuration, if x32, do only PHY0.
360 */
361 writel(initdelay, &mmdc0->mprddlctl);
Eric Nelsona09d68a2016-10-30 16:33:48 -0700362 if (sysinfo->dsize == 0x2)
Marek Vasutab257ed2015-12-16 15:40:06 +0100363 writel(initdelay, &mmdc1->mprddlctl);
364
365 /* Force a measurment, for previous delay setup to take effect. */
Eric Nelsona09d68a2016-10-30 16:33:48 -0700366 force_delay_measurement(sysinfo->dsize);
Marek Vasutab257ed2015-12-16 15:40:06 +0100367
368 /*
369 * ***************************
370 * Read DQS Gating calibration
371 * ***************************
372 */
373 debug("Starting Read DQS Gating calibration.\n");
374
375 /*
376 * Reset the read data FIFOs (two resets); only need to issue reset
377 * to PHY0 since in x64 mode, the reset will also go to PHY1.
378 */
379 reset_read_data_fifos();
380
381 /*
382 * Start the automatic read DQS gating calibration process by
383 * asserting MPDGCTRL0[HW_DG_EN] and MPDGCTRL0[DG_CMP_CYC]
384 * and then poll MPDGCTRL0[HW_DG_EN]] until this bit clears
385 * to indicate completion.
386 * Also, ensure that MPDGCTRL0[HW_DG_ERR] is clear to indicate
387 * no errors were seen during calibration.
388 */
389
390 /*
391 * Set bit 30: chooses option to wait 32 cycles instead of
392 * 16 before comparing read data.
393 */
394 setbits_le32(&mmdc0->mpdgctrl0, 1 << 30);
Eric Nelson4285a532016-10-30 16:33:47 -0700395 if (sysinfo->dsize == 2)
396 setbits_le32(&mmdc1->mpdgctrl0, 1 << 30);
Marek Vasutab257ed2015-12-16 15:40:06 +0100397
398 /* Set bit 28 to start automatic read DQS gating calibration */
399 setbits_le32(&mmdc0->mpdgctrl0, 5 << 28);
400
401 /* Poll for completion. MPDGCTRL0[HW_DG_EN] should be 0 */
Álvaro Fernández Rojas918de032018-01-23 17:14:55 +0100402 wait_for_bit_le32(&mmdc0->mpdgctrl0, 1 << 28, 0, 100, 0);
Marek Vasutab257ed2015-12-16 15:40:06 +0100403
404 /*
405 * Check to see if any errors were encountered during calibration
406 * (check MPDGCTRL0[HW_DG_ERR]).
407 * Check both PHYs for x64 configuration, if x32, check only PHY0.
408 */
409 if (readl(&mmdc0->mpdgctrl0) & 0x00001000)
410 errors |= 1;
411
Eric Nelsona09d68a2016-10-30 16:33:48 -0700412 if ((sysinfo->dsize == 0x2) && (readl(&mmdc1->mpdgctrl0) & 0x00001000))
Marek Vasutab257ed2015-12-16 15:40:06 +0100413 errors |= 2;
414
Eric Nelson4285a532016-10-30 16:33:47 -0700415 /* now disable mpdgctrl0[DG_CMP_CYC] */
416 clrbits_le32(&mmdc0->mpdgctrl0, 1 << 30);
417 if (sysinfo->dsize == 2)
418 clrbits_le32(&mmdc1->mpdgctrl0, 1 << 30);
419
Marek Vasutab257ed2015-12-16 15:40:06 +0100420 /*
421 * DQS gating absolute offset should be modified from
422 * reflecting (HW_DG_LOWx + HW_DG_UPx)/2 to
423 * reflecting (HW_DG_UPx - 0x80)
424 */
425 modify_dg_result(&mmdc0->mpdghwst0, &mmdc0->mpdghwst1,
426 &mmdc0->mpdgctrl0);
427 modify_dg_result(&mmdc0->mpdghwst2, &mmdc0->mpdghwst3,
428 &mmdc0->mpdgctrl1);
Eric Nelsona09d68a2016-10-30 16:33:48 -0700429 if (sysinfo->dsize == 0x2) {
Marek Vasutab257ed2015-12-16 15:40:06 +0100430 modify_dg_result(&mmdc1->mpdghwst0, &mmdc1->mpdghwst1,
431 &mmdc1->mpdgctrl0);
432 modify_dg_result(&mmdc1->mpdghwst2, &mmdc1->mpdghwst3,
433 &mmdc1->mpdgctrl1);
434 }
435 debug("Ending Read DQS Gating calibration. Error mask: 0x%x\n", errors);
436
437 /*
438 * **********************
439 * Read Delay calibration
440 * **********************
441 */
442 debug("Starting Read Delay calibration.\n");
443
444 reset_read_data_fifos();
445
446 /*
447 * 4. Issue the Precharge-All command to the DDR device for both
448 * chip selects. If only using one chip select, then precharge
449 * only the desired chip select.
450 */
451 precharge_all(cs0_enable, cs1_enable);
452
453 /*
454 * 9. Read delay-line calibration
455 * Start the automatic read calibration process by asserting
456 * MPRDDLHWCTL[HW_RD_DL_EN].
457 */
458 writel(0x00000030, &mmdc0->mprddlhwctl);
459
460 /*
461 * 10. poll for completion
462 * MMDC indicates that the write data calibration had finished by
463 * setting MPRDDLHWCTL[HW_RD_DL_EN] = 0. Also, ensure that
464 * no error bits were set.
465 */
Álvaro Fernández Rojas918de032018-01-23 17:14:55 +0100466 wait_for_bit_le32(&mmdc0->mprddlhwctl, 1 << 4, 0, 100, 0);
Marek Vasutab257ed2015-12-16 15:40:06 +0100467
468 /* check both PHYs for x64 configuration, if x32, check only PHY0 */
469 if (readl(&mmdc0->mprddlhwctl) & 0x0000000f)
470 errors |= 4;
471
Eric Nelsona09d68a2016-10-30 16:33:48 -0700472 if ((sysinfo->dsize == 0x2) &&
473 (readl(&mmdc1->mprddlhwctl) & 0x0000000f))
Marek Vasutab257ed2015-12-16 15:40:06 +0100474 errors |= 8;
475
476 debug("Ending Read Delay calibration. Error mask: 0x%x\n", errors);
477
478 /*
479 * ***********************
480 * Write Delay Calibration
481 * ***********************
482 */
483 debug("Starting Write Delay calibration.\n");
484
485 reset_read_data_fifos();
486
487 /*
488 * 4. Issue the Precharge-All command to the DDR device for both
489 * chip selects. If only using one chip select, then precharge
490 * only the desired chip select.
491 */
492 precharge_all(cs0_enable, cs1_enable);
493
494 /*
495 * 8. Set the WR_DL_ABS# bits to their default values.
496 * Both PHYs for x64 configuration, if x32, do only PHY0.
497 */
498 writel(initdelay, &mmdc0->mpwrdlctl);
Eric Nelsona09d68a2016-10-30 16:33:48 -0700499 if (sysinfo->dsize == 0x2)
Marek Vasutab257ed2015-12-16 15:40:06 +0100500 writel(initdelay, &mmdc1->mpwrdlctl);
501
502 /*
503 * XXX This isn't in the manual. Force a measurement,
504 * for previous delay setup to effect.
505 */
Eric Nelsona09d68a2016-10-30 16:33:48 -0700506 force_delay_measurement(sysinfo->dsize);
Marek Vasutab257ed2015-12-16 15:40:06 +0100507
508 /*
509 * 9. 10. Start the automatic write calibration process
510 * by asserting MPWRDLHWCTL0[HW_WR_DL_EN].
511 */
512 writel(0x00000030, &mmdc0->mpwrdlhwctl);
513
514 /*
515 * Poll for completion.
516 * MMDC indicates that the write data calibration had finished
517 * by setting MPWRDLHWCTL[HW_WR_DL_EN] = 0.
518 * Also, ensure that no error bits were set.
519 */
Álvaro Fernández Rojas918de032018-01-23 17:14:55 +0100520 wait_for_bit_le32(&mmdc0->mpwrdlhwctl, 1 << 4, 0, 100, 0);
Marek Vasutab257ed2015-12-16 15:40:06 +0100521
522 /* Check both PHYs for x64 configuration, if x32, check only PHY0 */
523 if (readl(&mmdc0->mpwrdlhwctl) & 0x0000000f)
524 errors |= 16;
525
Eric Nelsona09d68a2016-10-30 16:33:48 -0700526 if ((sysinfo->dsize == 0x2) &&
527 (readl(&mmdc1->mpwrdlhwctl) & 0x0000000f))
Marek Vasutab257ed2015-12-16 15:40:06 +0100528 errors |= 32;
529
530 debug("Ending Write Delay calibration. Error mask: 0x%x\n", errors);
531
532 reset_read_data_fifos();
533
534 /* Enable DDR logic power down timer */
535 setbits_le32(&mmdc0->mdpdc, 0x00005500);
536
537 /* Enable Adopt power down timer */
538 clrbits_le32(&mmdc0->mapsr, 0x1);
539
540 /* Restore MDMISC value (RALAT, WALAT) to MMDCP1 */
541 writel(esdmisc_val, &mmdc0->mdmisc);
542
543 /* Clear DQS pull ups */
Marek Vasut49fad0e2019-11-26 09:34:50 +0100544 mmdc_set_sdqs(false);
Marek Vasutab257ed2015-12-16 15:40:06 +0100545
546 /* Re-enable SDE (chip selects) if they were set initially */
547 if (cs1_enable_initial)
548 /* Set SDE_1 */
549 setbits_le32(&mmdc0->mdctl, 1 << 30);
550
551 if (cs0_enable_initial)
552 /* Set SDE_0 */
553 setbits_le32(&mmdc0->mdctl, 1 << 31);
554
555 /* Re-enable to auto refresh */
556 writel(temp_ref, &mmdc0->mdref);
557
558 /* Clear the MDSCR (including the con_req bit) */
559 writel(0x0, &mmdc0->mdscr); /* CS0 */
560
561 /* Poll to make sure the con_ack bit is clear */
Álvaro Fernández Rojas918de032018-01-23 17:14:55 +0100562 wait_for_bit_le32(&mmdc0->mdscr, 1 << 14, 0, 100, 0);
Marek Vasutab257ed2015-12-16 15:40:06 +0100563
564 /*
565 * Print out the registers that were updated as a result
566 * of the calibration process.
567 */
568 debug("MMDC registers updated from calibration\n");
569 debug("Read DQS gating calibration:\n");
Marek Vasut1b8e5dc2019-11-26 09:34:49 +0100570 debug("\tMPDGCTRL0 PHY0 = 0x%08x\n", readl(&mmdc0->mpdgctrl0));
571 debug("\tMPDGCTRL1 PHY0 = 0x%08x\n", readl(&mmdc0->mpdgctrl1));
Eric Nelsona09d68a2016-10-30 16:33:48 -0700572 if (sysinfo->dsize == 2) {
Marek Vasut1b8e5dc2019-11-26 09:34:49 +0100573 debug("\tMPDGCTRL0 PHY1 = 0x%08x\n", readl(&mmdc1->mpdgctrl0));
574 debug("\tMPDGCTRL1 PHY1 = 0x%08x\n", readl(&mmdc1->mpdgctrl1));
Eric Nelsona09d68a2016-10-30 16:33:48 -0700575 }
Marek Vasutab257ed2015-12-16 15:40:06 +0100576 debug("Read calibration:\n");
Marek Vasut1b8e5dc2019-11-26 09:34:49 +0100577 debug("\tMPRDDLCTL PHY0 = 0x%08x\n", readl(&mmdc0->mprddlctl));
Eric Nelsona09d68a2016-10-30 16:33:48 -0700578 if (sysinfo->dsize == 2)
Marek Vasut1b8e5dc2019-11-26 09:34:49 +0100579 debug("\tMPRDDLCTL PHY1 = 0x%08x\n", readl(&mmdc1->mprddlctl));
Marek Vasutab257ed2015-12-16 15:40:06 +0100580 debug("Write calibration:\n");
Marek Vasut1b8e5dc2019-11-26 09:34:49 +0100581 debug("\tMPWRDLCTL PHY0 = 0x%08x\n", readl(&mmdc0->mpwrdlctl));
Eric Nelsona09d68a2016-10-30 16:33:48 -0700582 if (sysinfo->dsize == 2)
Marek Vasut1b8e5dc2019-11-26 09:34:49 +0100583 debug("\tMPWRDLCTL PHY1 = 0x%08x\n", readl(&mmdc1->mpwrdlctl));
Marek Vasutab257ed2015-12-16 15:40:06 +0100584
585 /*
586 * Registers below are for debugging purposes. These print out
587 * the upper and lower boundaries captured during
588 * read DQS gating calibration.
589 */
590 debug("Status registers bounds for read DQS gating:\n");
591 debug("\tMPDGHWST0 PHY0 = 0x%08x\n", readl(&mmdc0->mpdghwst0));
592 debug("\tMPDGHWST1 PHY0 = 0x%08x\n", readl(&mmdc0->mpdghwst1));
593 debug("\tMPDGHWST2 PHY0 = 0x%08x\n", readl(&mmdc0->mpdghwst2));
594 debug("\tMPDGHWST3 PHY0 = 0x%08x\n", readl(&mmdc0->mpdghwst3));
Eric Nelsona09d68a2016-10-30 16:33:48 -0700595 if (sysinfo->dsize == 2) {
596 debug("\tMPDGHWST0 PHY1 = 0x%08x\n", readl(&mmdc1->mpdghwst0));
597 debug("\tMPDGHWST1 PHY1 = 0x%08x\n", readl(&mmdc1->mpdghwst1));
598 debug("\tMPDGHWST2 PHY1 = 0x%08x\n", readl(&mmdc1->mpdghwst2));
599 debug("\tMPDGHWST3 PHY1 = 0x%08x\n", readl(&mmdc1->mpdghwst3));
600 }
Marek Vasutab257ed2015-12-16 15:40:06 +0100601
602 debug("Final do_dqs_calibration error mask: 0x%x\n", errors);
603
604 return errors;
605}
606#endif
607
Peng Fan2ecdd022014-12-30 17:24:01 +0800608#if defined(CONFIG_MX6SX)
609/* Configure MX6SX mmdc iomux */
610void mx6sx_dram_iocfg(unsigned width,
611 const struct mx6sx_iomux_ddr_regs *ddr,
612 const struct mx6sx_iomux_grp_regs *grp)
613{
614 struct mx6sx_iomux_ddr_regs *mx6_ddr_iomux;
615 struct mx6sx_iomux_grp_regs *mx6_grp_iomux;
616
617 mx6_ddr_iomux = (struct mx6sx_iomux_ddr_regs *)MX6SX_IOM_DDR_BASE;
618 mx6_grp_iomux = (struct mx6sx_iomux_grp_regs *)MX6SX_IOM_GRP_BASE;
619
620 /* DDR IO TYPE */
621 writel(grp->grp_ddr_type, &mx6_grp_iomux->grp_ddr_type);
622 writel(grp->grp_ddrpke, &mx6_grp_iomux->grp_ddrpke);
623
624 /* CLOCK */
625 writel(ddr->dram_sdclk_0, &mx6_ddr_iomux->dram_sdclk_0);
626
627 /* ADDRESS */
628 writel(ddr->dram_cas, &mx6_ddr_iomux->dram_cas);
629 writel(ddr->dram_ras, &mx6_ddr_iomux->dram_ras);
630 writel(grp->grp_addds, &mx6_grp_iomux->grp_addds);
631
632 /* Control */
633 writel(ddr->dram_reset, &mx6_ddr_iomux->dram_reset);
634 writel(ddr->dram_sdba2, &mx6_ddr_iomux->dram_sdba2);
635 writel(ddr->dram_sdcke0, &mx6_ddr_iomux->dram_sdcke0);
636 writel(ddr->dram_sdcke1, &mx6_ddr_iomux->dram_sdcke1);
637 writel(ddr->dram_odt0, &mx6_ddr_iomux->dram_odt0);
638 writel(ddr->dram_odt1, &mx6_ddr_iomux->dram_odt1);
639 writel(grp->grp_ctlds, &mx6_grp_iomux->grp_ctlds);
640
641 /* Data Strobes */
642 writel(grp->grp_ddrmode_ctl, &mx6_grp_iomux->grp_ddrmode_ctl);
643 writel(ddr->dram_sdqs0, &mx6_ddr_iomux->dram_sdqs0);
644 writel(ddr->dram_sdqs1, &mx6_ddr_iomux->dram_sdqs1);
645 if (width >= 32) {
646 writel(ddr->dram_sdqs2, &mx6_ddr_iomux->dram_sdqs2);
647 writel(ddr->dram_sdqs3, &mx6_ddr_iomux->dram_sdqs3);
648 }
649
650 /* Data */
651 writel(grp->grp_ddrmode, &mx6_grp_iomux->grp_ddrmode);
652 writel(grp->grp_b0ds, &mx6_grp_iomux->grp_b0ds);
653 writel(grp->grp_b1ds, &mx6_grp_iomux->grp_b1ds);
654 if (width >= 32) {
655 writel(grp->grp_b2ds, &mx6_grp_iomux->grp_b2ds);
656 writel(grp->grp_b3ds, &mx6_grp_iomux->grp_b3ds);
657 }
658 writel(ddr->dram_dqm0, &mx6_ddr_iomux->dram_dqm0);
659 writel(ddr->dram_dqm1, &mx6_ddr_iomux->dram_dqm1);
660 if (width >= 32) {
661 writel(ddr->dram_dqm2, &mx6_ddr_iomux->dram_dqm2);
662 writel(ddr->dram_dqm3, &mx6_ddr_iomux->dram_dqm3);
663 }
664}
665#endif
666
Fabio Estevam1b691df2018-01-03 12:33:05 -0200667#if defined(CONFIG_MX6UL) || defined(CONFIG_MX6ULL)
Peng Fan98f11a12015-07-20 19:28:33 +0800668void mx6ul_dram_iocfg(unsigned width,
669 const struct mx6ul_iomux_ddr_regs *ddr,
670 const struct mx6ul_iomux_grp_regs *grp)
671{
672 struct mx6ul_iomux_ddr_regs *mx6_ddr_iomux;
673 struct mx6ul_iomux_grp_regs *mx6_grp_iomux;
674
675 mx6_ddr_iomux = (struct mx6ul_iomux_ddr_regs *)MX6UL_IOM_DDR_BASE;
676 mx6_grp_iomux = (struct mx6ul_iomux_grp_regs *)MX6UL_IOM_GRP_BASE;
677
678 /* DDR IO TYPE */
679 writel(grp->grp_ddr_type, &mx6_grp_iomux->grp_ddr_type);
680 writel(grp->grp_ddrpke, &mx6_grp_iomux->grp_ddrpke);
681
682 /* CLOCK */
683 writel(ddr->dram_sdclk_0, &mx6_ddr_iomux->dram_sdclk_0);
684
685 /* ADDRESS */
686 writel(ddr->dram_cas, &mx6_ddr_iomux->dram_cas);
687 writel(ddr->dram_ras, &mx6_ddr_iomux->dram_ras);
688 writel(grp->grp_addds, &mx6_grp_iomux->grp_addds);
689
690 /* Control */
691 writel(ddr->dram_reset, &mx6_ddr_iomux->dram_reset);
692 writel(ddr->dram_sdba2, &mx6_ddr_iomux->dram_sdba2);
693 writel(ddr->dram_odt0, &mx6_ddr_iomux->dram_odt0);
694 writel(ddr->dram_odt1, &mx6_ddr_iomux->dram_odt1);
695 writel(grp->grp_ctlds, &mx6_grp_iomux->grp_ctlds);
696
697 /* Data Strobes */
698 writel(grp->grp_ddrmode_ctl, &mx6_grp_iomux->grp_ddrmode_ctl);
699 writel(ddr->dram_sdqs0, &mx6_ddr_iomux->dram_sdqs0);
700 writel(ddr->dram_sdqs1, &mx6_ddr_iomux->dram_sdqs1);
701
702 /* Data */
703 writel(grp->grp_ddrmode, &mx6_grp_iomux->grp_ddrmode);
704 writel(grp->grp_b0ds, &mx6_grp_iomux->grp_b0ds);
705 writel(grp->grp_b1ds, &mx6_grp_iomux->grp_b1ds);
706 writel(ddr->dram_dqm0, &mx6_ddr_iomux->dram_dqm0);
707 writel(ddr->dram_dqm1, &mx6_ddr_iomux->dram_dqm1);
708}
709#endif
710
Peng Fand226fac2015-08-17 16:11:00 +0800711#if defined(CONFIG_MX6SL)
712void mx6sl_dram_iocfg(unsigned width,
713 const struct mx6sl_iomux_ddr_regs *ddr,
714 const struct mx6sl_iomux_grp_regs *grp)
715{
716 struct mx6sl_iomux_ddr_regs *mx6_ddr_iomux;
717 struct mx6sl_iomux_grp_regs *mx6_grp_iomux;
718
719 mx6_ddr_iomux = (struct mx6sl_iomux_ddr_regs *)MX6SL_IOM_DDR_BASE;
720 mx6_grp_iomux = (struct mx6sl_iomux_grp_regs *)MX6SL_IOM_GRP_BASE;
721
722 /* DDR IO TYPE */
723 mx6_grp_iomux->grp_ddr_type = grp->grp_ddr_type;
724 mx6_grp_iomux->grp_ddrpke = grp->grp_ddrpke;
725
726 /* CLOCK */
727 mx6_ddr_iomux->dram_sdclk_0 = ddr->dram_sdclk_0;
728
729 /* ADDRESS */
730 mx6_ddr_iomux->dram_cas = ddr->dram_cas;
731 mx6_ddr_iomux->dram_ras = ddr->dram_ras;
732 mx6_grp_iomux->grp_addds = grp->grp_addds;
733
734 /* Control */
735 mx6_ddr_iomux->dram_reset = ddr->dram_reset;
736 mx6_ddr_iomux->dram_sdba2 = ddr->dram_sdba2;
737 mx6_grp_iomux->grp_ctlds = grp->grp_ctlds;
738
739 /* Data Strobes */
740 mx6_grp_iomux->grp_ddrmode_ctl = grp->grp_ddrmode_ctl;
741 mx6_ddr_iomux->dram_sdqs0 = ddr->dram_sdqs0;
742 mx6_ddr_iomux->dram_sdqs1 = ddr->dram_sdqs1;
743 if (width >= 32) {
744 mx6_ddr_iomux->dram_sdqs2 = ddr->dram_sdqs2;
745 mx6_ddr_iomux->dram_sdqs3 = ddr->dram_sdqs3;
746 }
747
748 /* Data */
749 mx6_grp_iomux->grp_ddrmode = grp->grp_ddrmode;
750 mx6_grp_iomux->grp_b0ds = grp->grp_b0ds;
751 mx6_grp_iomux->grp_b1ds = grp->grp_b1ds;
752 if (width >= 32) {
753 mx6_grp_iomux->grp_b2ds = grp->grp_b2ds;
754 mx6_grp_iomux->grp_b3ds = grp->grp_b3ds;
755 }
756
757 mx6_ddr_iomux->dram_dqm0 = ddr->dram_dqm0;
758 mx6_ddr_iomux->dram_dqm1 = ddr->dram_dqm1;
759 if (width >= 32) {
760 mx6_ddr_iomux->dram_dqm2 = ddr->dram_dqm2;
761 mx6_ddr_iomux->dram_dqm3 = ddr->dram_dqm3;
762 }
763}
764#endif
765
Tim Harvey8ab871b2014-06-02 16:13:23 -0700766#if defined(CONFIG_MX6QDL) || defined(CONFIG_MX6Q) || defined(CONFIG_MX6D)
767/* Configure MX6DQ mmdc iomux */
768void mx6dq_dram_iocfg(unsigned width,
769 const struct mx6dq_iomux_ddr_regs *ddr,
770 const struct mx6dq_iomux_grp_regs *grp)
771{
772 volatile struct mx6dq_iomux_ddr_regs *mx6_ddr_iomux;
773 volatile struct mx6dq_iomux_grp_regs *mx6_grp_iomux;
774
775 mx6_ddr_iomux = (struct mx6dq_iomux_ddr_regs *)MX6DQ_IOM_DDR_BASE;
776 mx6_grp_iomux = (struct mx6dq_iomux_grp_regs *)MX6DQ_IOM_GRP_BASE;
777
778 /* DDR IO Type */
779 mx6_grp_iomux->grp_ddr_type = grp->grp_ddr_type;
780 mx6_grp_iomux->grp_ddrpke = grp->grp_ddrpke;
781
782 /* Clock */
783 mx6_ddr_iomux->dram_sdclk_0 = ddr->dram_sdclk_0;
784 mx6_ddr_iomux->dram_sdclk_1 = ddr->dram_sdclk_1;
785
786 /* Address */
787 mx6_ddr_iomux->dram_cas = ddr->dram_cas;
788 mx6_ddr_iomux->dram_ras = ddr->dram_ras;
789 mx6_grp_iomux->grp_addds = grp->grp_addds;
790
791 /* Control */
792 mx6_ddr_iomux->dram_reset = ddr->dram_reset;
793 mx6_ddr_iomux->dram_sdcke0 = ddr->dram_sdcke0;
794 mx6_ddr_iomux->dram_sdcke1 = ddr->dram_sdcke1;
795 mx6_ddr_iomux->dram_sdba2 = ddr->dram_sdba2;
796 mx6_ddr_iomux->dram_sdodt0 = ddr->dram_sdodt0;
797 mx6_ddr_iomux->dram_sdodt1 = ddr->dram_sdodt1;
798 mx6_grp_iomux->grp_ctlds = grp->grp_ctlds;
799
800 /* Data Strobes */
801 mx6_grp_iomux->grp_ddrmode_ctl = grp->grp_ddrmode_ctl;
802 mx6_ddr_iomux->dram_sdqs0 = ddr->dram_sdqs0;
803 mx6_ddr_iomux->dram_sdqs1 = ddr->dram_sdqs1;
804 if (width >= 32) {
805 mx6_ddr_iomux->dram_sdqs2 = ddr->dram_sdqs2;
806 mx6_ddr_iomux->dram_sdqs3 = ddr->dram_sdqs3;
807 }
808 if (width >= 64) {
809 mx6_ddr_iomux->dram_sdqs4 = ddr->dram_sdqs4;
810 mx6_ddr_iomux->dram_sdqs5 = ddr->dram_sdqs5;
811 mx6_ddr_iomux->dram_sdqs6 = ddr->dram_sdqs6;
812 mx6_ddr_iomux->dram_sdqs7 = ddr->dram_sdqs7;
813 }
814
815 /* Data */
816 mx6_grp_iomux->grp_ddrmode = grp->grp_ddrmode;
817 mx6_grp_iomux->grp_b0ds = grp->grp_b0ds;
818 mx6_grp_iomux->grp_b1ds = grp->grp_b1ds;
819 if (width >= 32) {
820 mx6_grp_iomux->grp_b2ds = grp->grp_b2ds;
821 mx6_grp_iomux->grp_b3ds = grp->grp_b3ds;
822 }
823 if (width >= 64) {
824 mx6_grp_iomux->grp_b4ds = grp->grp_b4ds;
825 mx6_grp_iomux->grp_b5ds = grp->grp_b5ds;
826 mx6_grp_iomux->grp_b6ds = grp->grp_b6ds;
827 mx6_grp_iomux->grp_b7ds = grp->grp_b7ds;
828 }
829 mx6_ddr_iomux->dram_dqm0 = ddr->dram_dqm0;
830 mx6_ddr_iomux->dram_dqm1 = ddr->dram_dqm1;
831 if (width >= 32) {
832 mx6_ddr_iomux->dram_dqm2 = ddr->dram_dqm2;
833 mx6_ddr_iomux->dram_dqm3 = ddr->dram_dqm3;
834 }
835 if (width >= 64) {
836 mx6_ddr_iomux->dram_dqm4 = ddr->dram_dqm4;
837 mx6_ddr_iomux->dram_dqm5 = ddr->dram_dqm5;
838 mx6_ddr_iomux->dram_dqm6 = ddr->dram_dqm6;
839 mx6_ddr_iomux->dram_dqm7 = ddr->dram_dqm7;
840 }
841}
842#endif
843
844#if defined(CONFIG_MX6QDL) || defined(CONFIG_MX6DL) || defined(CONFIG_MX6S)
845/* Configure MX6SDL mmdc iomux */
846void mx6sdl_dram_iocfg(unsigned width,
847 const struct mx6sdl_iomux_ddr_regs *ddr,
848 const struct mx6sdl_iomux_grp_regs *grp)
849{
850 volatile struct mx6sdl_iomux_ddr_regs *mx6_ddr_iomux;
851 volatile struct mx6sdl_iomux_grp_regs *mx6_grp_iomux;
852
853 mx6_ddr_iomux = (struct mx6sdl_iomux_ddr_regs *)MX6SDL_IOM_DDR_BASE;
854 mx6_grp_iomux = (struct mx6sdl_iomux_grp_regs *)MX6SDL_IOM_GRP_BASE;
855
856 /* DDR IO Type */
857 mx6_grp_iomux->grp_ddr_type = grp->grp_ddr_type;
858 mx6_grp_iomux->grp_ddrpke = grp->grp_ddrpke;
859
860 /* Clock */
861 mx6_ddr_iomux->dram_sdclk_0 = ddr->dram_sdclk_0;
862 mx6_ddr_iomux->dram_sdclk_1 = ddr->dram_sdclk_1;
863
864 /* Address */
865 mx6_ddr_iomux->dram_cas = ddr->dram_cas;
866 mx6_ddr_iomux->dram_ras = ddr->dram_ras;
867 mx6_grp_iomux->grp_addds = grp->grp_addds;
868
869 /* Control */
870 mx6_ddr_iomux->dram_reset = ddr->dram_reset;
871 mx6_ddr_iomux->dram_sdcke0 = ddr->dram_sdcke0;
872 mx6_ddr_iomux->dram_sdcke1 = ddr->dram_sdcke1;
873 mx6_ddr_iomux->dram_sdba2 = ddr->dram_sdba2;
874 mx6_ddr_iomux->dram_sdodt0 = ddr->dram_sdodt0;
875 mx6_ddr_iomux->dram_sdodt1 = ddr->dram_sdodt1;
876 mx6_grp_iomux->grp_ctlds = grp->grp_ctlds;
877
878 /* Data Strobes */
879 mx6_grp_iomux->grp_ddrmode_ctl = grp->grp_ddrmode_ctl;
880 mx6_ddr_iomux->dram_sdqs0 = ddr->dram_sdqs0;
881 mx6_ddr_iomux->dram_sdqs1 = ddr->dram_sdqs1;
882 if (width >= 32) {
883 mx6_ddr_iomux->dram_sdqs2 = ddr->dram_sdqs2;
884 mx6_ddr_iomux->dram_sdqs3 = ddr->dram_sdqs3;
885 }
886 if (width >= 64) {
887 mx6_ddr_iomux->dram_sdqs4 = ddr->dram_sdqs4;
888 mx6_ddr_iomux->dram_sdqs5 = ddr->dram_sdqs5;
889 mx6_ddr_iomux->dram_sdqs6 = ddr->dram_sdqs6;
890 mx6_ddr_iomux->dram_sdqs7 = ddr->dram_sdqs7;
891 }
892
893 /* Data */
894 mx6_grp_iomux->grp_ddrmode = grp->grp_ddrmode;
895 mx6_grp_iomux->grp_b0ds = grp->grp_b0ds;
896 mx6_grp_iomux->grp_b1ds = grp->grp_b1ds;
897 if (width >= 32) {
898 mx6_grp_iomux->grp_b2ds = grp->grp_b2ds;
899 mx6_grp_iomux->grp_b3ds = grp->grp_b3ds;
900 }
901 if (width >= 64) {
902 mx6_grp_iomux->grp_b4ds = grp->grp_b4ds;
903 mx6_grp_iomux->grp_b5ds = grp->grp_b5ds;
904 mx6_grp_iomux->grp_b6ds = grp->grp_b6ds;
905 mx6_grp_iomux->grp_b7ds = grp->grp_b7ds;
906 }
907 mx6_ddr_iomux->dram_dqm0 = ddr->dram_dqm0;
908 mx6_ddr_iomux->dram_dqm1 = ddr->dram_dqm1;
909 if (width >= 32) {
910 mx6_ddr_iomux->dram_dqm2 = ddr->dram_dqm2;
911 mx6_ddr_iomux->dram_dqm3 = ddr->dram_dqm3;
912 }
913 if (width >= 64) {
914 mx6_ddr_iomux->dram_dqm4 = ddr->dram_dqm4;
915 mx6_ddr_iomux->dram_dqm5 = ddr->dram_dqm5;
916 mx6_ddr_iomux->dram_dqm6 = ddr->dram_dqm6;
917 mx6_ddr_iomux->dram_dqm7 = ddr->dram_dqm7;
918 }
919}
920#endif
921
922/*
923 * Configure mx6 mmdc registers based on:
924 * - board-specific memory configuration
925 * - board-specific calibration data
Peng Fanda7ada02015-08-17 16:11:04 +0800926 * - ddr3/lpddr2 chip details
Tim Harvey8ab871b2014-06-02 16:13:23 -0700927 *
928 * The various calculations here are derived from the Freescale
Peng Fanda7ada02015-08-17 16:11:04 +0800929 * 1. i.Mx6DQSDL DDR3 Script Aid spreadsheet (DOC-94917) designed to generate
930 * MMDC configuration registers based on memory system and memory chip
931 * parameters.
932 *
933 * 2. i.Mx6SL LPDDR2 Script Aid spreadsheet V0.04 designed to generate MMDC
934 * configuration registers based on memory system and memory chip
935 * parameters.
Tim Harvey8ab871b2014-06-02 16:13:23 -0700936 *
937 * The defaults here are those which were specified in the spreadsheet.
938 * For details on each register, refer to the IMX6DQRM and/or IMX6SDLRM
Peng Fanda7ada02015-08-17 16:11:04 +0800939 * and/or IMX6SLRM section titled MMDC initialization.
Tim Harvey8ab871b2014-06-02 16:13:23 -0700940 */
941#define MR(val, ba, cmd, cs1) \
942 ((val << 16) | (1 << 15) | (cmd << 4) | (cs1 << 3) | ba)
Peng Fan98f11a12015-07-20 19:28:33 +0800943#define MMDC1(entry, value) do { \
Fabio Estevam8548f972018-01-01 22:51:45 -0200944 if (!is_mx6sx() && !is_mx6ul() && !is_mx6ull() && !is_mx6sl()) \
Peng Fan98f11a12015-07-20 19:28:33 +0800945 mmdc1->entry = value; \
946 } while (0)
947
Peng Fanda7ada02015-08-17 16:11:04 +0800948/*
949 * According JESD209-2B-LPDDR2: Table 103
950 * WL: write latency
951 */
952static int lpddr2_wl(uint32_t mem_speed)
953{
954 switch (mem_speed) {
955 case 1066:
956 case 933:
957 return 4;
958 case 800:
959 return 3;
960 case 677:
961 case 533:
962 return 2;
963 case 400:
964 case 333:
965 return 1;
966 default:
967 puts("invalid memory speed\n");
968 hang();
969 }
970
971 return 0;
972}
973
974/*
975 * According JESD209-2B-LPDDR2: Table 103
976 * RL: read latency
977 */
978static int lpddr2_rl(uint32_t mem_speed)
979{
980 switch (mem_speed) {
981 case 1066:
982 return 8;
983 case 933:
984 return 7;
985 case 800:
986 return 6;
987 case 677:
988 return 5;
989 case 533:
990 return 4;
991 case 400:
992 case 333:
993 return 3;
994 default:
995 puts("invalid memory speed\n");
996 hang();
997 }
998
999 return 0;
1000}
1001
1002void mx6_lpddr2_cfg(const struct mx6_ddr_sysinfo *sysinfo,
1003 const struct mx6_mmdc_calibration *calib,
1004 const struct mx6_lpddr2_cfg *lpddr2_cfg)
1005{
1006 volatile struct mmdc_p_regs *mmdc0;
1007 u32 val;
1008 u8 tcke, tcksrx, tcksre, trrd;
1009 u8 twl, txp, tfaw, tcl;
1010 u16 tras, twr, tmrd, trtp, twtr, trfc, txsr;
1011 u16 trcd_lp, trppb_lp, trpab_lp, trc_lp;
1012 u16 cs0_end;
1013 u8 coladdr;
1014 int clkper; /* clock period in picoseconds */
1015 int clock; /* clock freq in mHz */
1016 int cs;
1017
1018 /* only support 16/32 bits */
1019 if (sysinfo->dsize > 1)
1020 hang();
1021
1022 mmdc0 = (struct mmdc_p_regs *)MMDC_P0_BASE_ADDR;
1023
1024 clock = mxc_get_clock(MXC_DDR_CLK) / 1000000U;
1025 clkper = (1000 * 1000) / clock; /* pico seconds */
1026
1027 twl = lpddr2_wl(lpddr2_cfg->mem_speed) - 1;
1028
1029 /* LPDDR2-S2 and LPDDR2-S4 have the same tRFC value. */
1030 switch (lpddr2_cfg->density) {
1031 case 1:
1032 case 2:
1033 case 4:
1034 trfc = DIV_ROUND_UP(130000, clkper) - 1;
1035 txsr = DIV_ROUND_UP(140000, clkper) - 1;
1036 break;
1037 case 8:
1038 trfc = DIV_ROUND_UP(210000, clkper) - 1;
1039 txsr = DIV_ROUND_UP(220000, clkper) - 1;
1040 break;
1041 default:
1042 /*
1043 * 64Mb, 128Mb, 256Mb, 512Mb are not supported currently.
1044 */
1045 hang();
1046 break;
1047 }
1048 /*
1049 * txpdll, txpr, taonpd and taofpd are not relevant in LPDDR2 mode,
1050 * set them to 0. */
1051 txp = DIV_ROUND_UP(7500, clkper) - 1;
1052 tcke = 3;
1053 if (lpddr2_cfg->mem_speed == 333)
1054 tfaw = DIV_ROUND_UP(60000, clkper) - 1;
1055 else
1056 tfaw = DIV_ROUND_UP(50000, clkper) - 1;
1057 trrd = DIV_ROUND_UP(10000, clkper) - 1;
1058
1059 /* tckesr for LPDDR2 */
1060 tcksre = DIV_ROUND_UP(15000, clkper);
1061 tcksrx = tcksre;
1062 twr = DIV_ROUND_UP(15000, clkper) - 1;
1063 /*
1064 * tMRR: 2, tMRW: 5
1065 * tMRD should be set to max(tMRR, tMRW)
1066 */
1067 tmrd = 5;
1068 tras = DIV_ROUND_UP(lpddr2_cfg->trasmin, clkper / 10) - 1;
1069 /* LPDDR2 mode use tRCD_LP filed in MDCFG3. */
1070 trcd_lp = DIV_ROUND_UP(lpddr2_cfg->trcd_lp, clkper / 10) - 1;
1071 trc_lp = DIV_ROUND_UP(lpddr2_cfg->trasmin + lpddr2_cfg->trppb_lp,
1072 clkper / 10) - 1;
1073 trppb_lp = DIV_ROUND_UP(lpddr2_cfg->trppb_lp, clkper / 10) - 1;
1074 trpab_lp = DIV_ROUND_UP(lpddr2_cfg->trpab_lp, clkper / 10) - 1;
1075 /* To LPDDR2, CL in MDCFG0 refers to RL */
1076 tcl = lpddr2_rl(lpddr2_cfg->mem_speed) - 3;
1077 twtr = DIV_ROUND_UP(7500, clkper) - 1;
1078 trtp = DIV_ROUND_UP(7500, clkper) - 1;
1079
1080 cs0_end = 4 * sysinfo->cs_density - 1;
1081
1082 debug("density:%d Gb (%d Gb per chip)\n",
1083 sysinfo->cs_density, lpddr2_cfg->density);
1084 debug("clock: %dMHz (%d ps)\n", clock, clkper);
1085 debug("memspd:%d\n", lpddr2_cfg->mem_speed);
1086 debug("trcd_lp=%d\n", trcd_lp);
1087 debug("trppb_lp=%d\n", trppb_lp);
1088 debug("trpab_lp=%d\n", trpab_lp);
1089 debug("trc_lp=%d\n", trc_lp);
1090 debug("tcke=%d\n", tcke);
1091 debug("tcksrx=%d\n", tcksrx);
1092 debug("tcksre=%d\n", tcksre);
1093 debug("trfc=%d\n", trfc);
1094 debug("txsr=%d\n", txsr);
1095 debug("txp=%d\n", txp);
1096 debug("tfaw=%d\n", tfaw);
1097 debug("tcl=%d\n", tcl);
1098 debug("tras=%d\n", tras);
1099 debug("twr=%d\n", twr);
1100 debug("tmrd=%d\n", tmrd);
1101 debug("twl=%d\n", twl);
1102 debug("trtp=%d\n", trtp);
1103 debug("twtr=%d\n", twtr);
1104 debug("trrd=%d\n", trrd);
1105 debug("cs0_end=%d\n", cs0_end);
1106 debug("ncs=%d\n", sysinfo->ncs);
1107
1108 /*
1109 * board-specific configuration:
1110 * These values are determined empirically and vary per board layout
1111 */
1112 mmdc0->mpwldectrl0 = calib->p0_mpwldectrl0;
1113 mmdc0->mpwldectrl1 = calib->p0_mpwldectrl1;
1114 mmdc0->mpdgctrl0 = calib->p0_mpdgctrl0;
1115 mmdc0->mpdgctrl1 = calib->p0_mpdgctrl1;
1116 mmdc0->mprddlctl = calib->p0_mprddlctl;
1117 mmdc0->mpwrdlctl = calib->p0_mpwrdlctl;
1118 mmdc0->mpzqlp2ctl = calib->mpzqlp2ctl;
1119
1120 /* Read data DQ Byte0-3 delay */
1121 mmdc0->mprddqby0dl = 0x33333333;
1122 mmdc0->mprddqby1dl = 0x33333333;
1123 if (sysinfo->dsize > 0) {
1124 mmdc0->mprddqby2dl = 0x33333333;
1125 mmdc0->mprddqby3dl = 0x33333333;
1126 }
1127
1128 /* Write data DQ Byte0-3 delay */
1129 mmdc0->mpwrdqby0dl = 0xf3333333;
1130 mmdc0->mpwrdqby1dl = 0xf3333333;
1131 if (sysinfo->dsize > 0) {
1132 mmdc0->mpwrdqby2dl = 0xf3333333;
1133 mmdc0->mpwrdqby3dl = 0xf3333333;
1134 }
1135
1136 /*
1137 * In LPDDR2 mode this register should be cleared,
1138 * so no termination will be activated.
1139 */
1140 mmdc0->mpodtctrl = 0;
1141
1142 /* complete calibration */
1143 val = (1 << 11); /* Force measurement on delay-lines */
1144 mmdc0->mpmur0 = val;
1145
1146 /* Step 1: configuration request */
1147 mmdc0->mdscr = (u32)(1 << 15); /* config request */
1148
1149 /* Step 2: Timing configuration */
1150 mmdc0->mdcfg0 = (trfc << 24) | (txsr << 16) | (txp << 13) |
1151 (tfaw << 4) | tcl;
1152 mmdc0->mdcfg1 = (tras << 16) | (twr << 9) | (tmrd << 5) | twl;
1153 mmdc0->mdcfg2 = (trtp << 6) | (twtr << 3) | trrd;
1154 mmdc0->mdcfg3lp = (trc_lp << 16) | (trcd_lp << 8) |
1155 (trppb_lp << 4) | trpab_lp;
1156 mmdc0->mdotc = 0;
1157
1158 mmdc0->mdasp = cs0_end; /* CS addressing */
1159
1160 /* Step 3: Configure DDR type */
1161 mmdc0->mdmisc = (sysinfo->cs1_mirror << 19) | (sysinfo->walat << 16) |
1162 (sysinfo->bi_on << 12) | (sysinfo->mif3_mode << 9) |
1163 (sysinfo->ralat << 6) | (1 << 3);
1164
1165 /* Step 4: Configure delay while leaving reset */
1166 mmdc0->mdor = (sysinfo->sde_to_rst << 8) |
1167 (sysinfo->rst_to_cke << 0);
1168
1169 /* Step 5: Configure DDR physical parameters (density and burst len) */
1170 coladdr = lpddr2_cfg->coladdr;
1171 if (lpddr2_cfg->coladdr == 8) /* 8-bit COL is 0x3 */
1172 coladdr += 4;
1173 else if (lpddr2_cfg->coladdr == 12) /* 12-bit COL is 0x4 */
1174 coladdr += 1;
1175 mmdc0->mdctl = (lpddr2_cfg->rowaddr - 11) << 24 | /* ROW */
1176 (coladdr - 9) << 20 | /* COL */
1177 (0 << 19) | /* Burst Length = 4 for LPDDR2 */
1178 (sysinfo->dsize << 16); /* DDR data bus size */
1179
1180 /* Step 6: Perform ZQ calibration */
1181 val = 0xa1390003; /* one-time HW ZQ calib */
1182 mmdc0->mpzqhwctrl = val;
1183
1184 /* Step 7: Enable MMDC with desired chip select */
1185 mmdc0->mdctl |= (1 << 31) | /* SDE_0 for CS0 */
1186 ((sysinfo->ncs == 2) ? 1 : 0) << 30; /* SDE_1 for CS1 */
1187
1188 /* Step 8: Write Mode Registers to Init LPDDR2 devices */
1189 for (cs = 0; cs < sysinfo->ncs; cs++) {
1190 /* MR63: reset */
1191 mmdc0->mdscr = MR(63, 0, 3, cs);
1192 /* MR10: calibration,
1193 * 0xff is calibration command after intilization.
1194 */
1195 val = 0xA | (0xff << 8);
1196 mmdc0->mdscr = MR(val, 0, 3, cs);
1197 /* MR1 */
1198 val = 0x1 | (0x82 << 8);
1199 mmdc0->mdscr = MR(val, 0, 3, cs);
1200 /* MR2 */
1201 val = 0x2 | (0x04 << 8);
1202 mmdc0->mdscr = MR(val, 0, 3, cs);
1203 /* MR3 */
1204 val = 0x3 | (0x02 << 8);
1205 mmdc0->mdscr = MR(val, 0, 3, cs);
1206 }
1207
1208 /* Step 10: Power down control and self-refresh */
1209 mmdc0->mdpdc = (tcke & 0x7) << 16 |
1210 5 << 12 | /* PWDT_1: 256 cycles */
1211 5 << 8 | /* PWDT_0: 256 cycles */
1212 1 << 6 | /* BOTH_CS_PD */
1213 (tcksrx & 0x7) << 3 |
1214 (tcksre & 0x7);
1215 mmdc0->mapsr = 0x00001006; /* ADOPT power down enabled */
1216
1217 /* Step 11: Configure ZQ calibration: one-time and periodic 1ms */
1218 val = 0xa1310003;
1219 mmdc0->mpzqhwctrl = val;
1220
1221 /* Step 12: Configure and activate periodic refresh */
Fabio Estevamcb3c1212016-08-29 20:37:15 -03001222 mmdc0->mdref = (sysinfo->refsel << 14) | (sysinfo->refr << 11);
Peng Fanda7ada02015-08-17 16:11:04 +08001223
1224 /* Step 13: Deassert config request - init complete */
1225 mmdc0->mdscr = 0x00000000;
1226
1227 /* wait for auto-ZQ calibration to complete */
1228 mdelay(1);
1229}
1230
Peng Fan77e86952015-08-17 16:11:03 +08001231void mx6_ddr3_cfg(const struct mx6_ddr_sysinfo *sysinfo,
Nikita Kiryanovc4753462014-09-07 18:58:11 +03001232 const struct mx6_mmdc_calibration *calib,
1233 const struct mx6_ddr3_cfg *ddr3_cfg)
Tim Harvey8ab871b2014-06-02 16:13:23 -07001234{
1235 volatile struct mmdc_p_regs *mmdc0;
1236 volatile struct mmdc_p_regs *mmdc1;
Nikita Kiryanovc4753462014-09-07 18:58:11 +03001237 u32 val;
Tim Harvey8ab871b2014-06-02 16:13:23 -07001238 u8 tcke, tcksrx, tcksre, txpdll, taofpd, taonpd, trrd;
1239 u8 todtlon, taxpd, tanpd, tcwl, txp, tfaw, tcl;
1240 u8 todt_idle_off = 0x4; /* from DDR3 Script Aid spreadsheet */
1241 u16 trcd, trc, tras, twr, tmrd, trtp, trp, twtr, trfc, txs, txpr;
Nikita Kiryanovc4753462014-09-07 18:58:11 +03001242 u16 cs0_end;
Tim Harvey8ab871b2014-06-02 16:13:23 -07001243 u16 tdllk = 0x1ff; /* DLL locking time: 512 cycles (JEDEC DDR3) */
Marek Vasut4a463602014-08-04 01:47:10 +02001244 u8 coladdr;
Tim Harvey8ab871b2014-06-02 16:13:23 -07001245 int clkper; /* clock period in picoseconds */
Nikolay Dimitrov99c25ff2015-04-22 18:37:31 +03001246 int clock; /* clock freq in MHz */
Tim Harvey8ab871b2014-06-02 16:13:23 -07001247 int cs;
Nikolay Dimitrov99c25ff2015-04-22 18:37:31 +03001248 u16 mem_speed = ddr3_cfg->mem_speed;
Tim Harvey8ab871b2014-06-02 16:13:23 -07001249
1250 mmdc0 = (struct mmdc_p_regs *)MMDC_P0_BASE_ADDR;
Fabio Estevam8548f972018-01-01 22:51:45 -02001251 if (!is_mx6sx() && !is_mx6ul() && !is_mx6ull() && !is_mx6sl())
Peng Fan98f11a12015-07-20 19:28:33 +08001252 mmdc1 = (struct mmdc_p_regs *)MMDC_P1_BASE_ADDR;
Tim Harvey8ab871b2014-06-02 16:13:23 -07001253
Nikolay Dimitrov99c25ff2015-04-22 18:37:31 +03001254 /* Limit mem_speed for MX6D/MX6Q */
Peng Fan9dba13b2016-05-23 18:35:57 +08001255 if (is_mx6dq() || is_mx6dqp()) {
Nikolay Dimitrov99c25ff2015-04-22 18:37:31 +03001256 if (mem_speed > 1066)
1257 mem_speed = 1066; /* 1066 MT/s */
1258
Tim Harvey8ab871b2014-06-02 16:13:23 -07001259 tcwl = 4;
1260 }
Nikolay Dimitrov99c25ff2015-04-22 18:37:31 +03001261 /* Limit mem_speed for MX6S/MX6DL */
Tim Harvey8ab871b2014-06-02 16:13:23 -07001262 else {
Nikolay Dimitrov99c25ff2015-04-22 18:37:31 +03001263 if (mem_speed > 800)
1264 mem_speed = 800; /* 800 MT/s */
1265
Tim Harvey8ab871b2014-06-02 16:13:23 -07001266 tcwl = 3;
1267 }
Nikolay Dimitrov99c25ff2015-04-22 18:37:31 +03001268
1269 clock = mem_speed / 2;
1270 /*
1271 * Data rate of 1066 MT/s requires 533 MHz DDR3 clock, but MX6D/Q supports
1272 * up to 528 MHz, so reduce the clock to fit chip specs
1273 */
Peng Fan9dba13b2016-05-23 18:35:57 +08001274 if (is_mx6dq() || is_mx6dqp()) {
Nikolay Dimitrov99c25ff2015-04-22 18:37:31 +03001275 if (clock > 528)
1276 clock = 528; /* 528 MHz */
1277 }
1278
Nikita Kiryanovc4753462014-09-07 18:58:11 +03001279 clkper = (1000 * 1000) / clock; /* pico seconds */
Tim Harvey8ab871b2014-06-02 16:13:23 -07001280 todtlon = tcwl;
1281 taxpd = tcwl;
1282 tanpd = tcwl;
Tim Harvey8ab871b2014-06-02 16:13:23 -07001283
Nikita Kiryanovc4753462014-09-07 18:58:11 +03001284 switch (ddr3_cfg->density) {
Tim Harvey8ab871b2014-06-02 16:13:23 -07001285 case 1: /* 1Gb per chip */
1286 trfc = DIV_ROUND_UP(110000, clkper) - 1;
1287 txs = DIV_ROUND_UP(120000, clkper) - 1;
1288 break;
1289 case 2: /* 2Gb per chip */
1290 trfc = DIV_ROUND_UP(160000, clkper) - 1;
1291 txs = DIV_ROUND_UP(170000, clkper) - 1;
1292 break;
1293 case 4: /* 4Gb per chip */
Peng Fanb96b74c2015-09-01 11:03:14 +08001294 trfc = DIV_ROUND_UP(260000, clkper) - 1;
1295 txs = DIV_ROUND_UP(270000, clkper) - 1;
Tim Harvey8ab871b2014-06-02 16:13:23 -07001296 break;
1297 case 8: /* 8Gb per chip */
1298 trfc = DIV_ROUND_UP(350000, clkper) - 1;
1299 txs = DIV_ROUND_UP(360000, clkper) - 1;
1300 break;
1301 default:
1302 /* invalid density */
Nikita Kiryanovc4753462014-09-07 18:58:11 +03001303 puts("invalid chip density\n");
Tim Harvey8ab871b2014-06-02 16:13:23 -07001304 hang();
1305 break;
1306 }
1307 txpr = txs;
1308
Nikolay Dimitrov99c25ff2015-04-22 18:37:31 +03001309 switch (mem_speed) {
Tim Harvey8ab871b2014-06-02 16:13:23 -07001310 case 800:
Masahiro Yamadab62b39b2014-09-18 13:28:06 +09001311 txp = DIV_ROUND_UP(max(3 * clkper, 7500), clkper) - 1;
1312 tcke = DIV_ROUND_UP(max(3 * clkper, 7500), clkper) - 1;
Nikita Kiryanovc4753462014-09-07 18:58:11 +03001313 if (ddr3_cfg->pagesz == 1) {
Tim Harvey8ab871b2014-06-02 16:13:23 -07001314 tfaw = DIV_ROUND_UP(40000, clkper) - 1;
Masahiro Yamadab62b39b2014-09-18 13:28:06 +09001315 trrd = DIV_ROUND_UP(max(4 * clkper, 10000), clkper) - 1;
Tim Harvey8ab871b2014-06-02 16:13:23 -07001316 } else {
1317 tfaw = DIV_ROUND_UP(50000, clkper) - 1;
Masahiro Yamadab62b39b2014-09-18 13:28:06 +09001318 trrd = DIV_ROUND_UP(max(4 * clkper, 10000), clkper) - 1;
Tim Harvey8ab871b2014-06-02 16:13:23 -07001319 }
1320 break;
1321 case 1066:
Masahiro Yamadab62b39b2014-09-18 13:28:06 +09001322 txp = DIV_ROUND_UP(max(3 * clkper, 7500), clkper) - 1;
1323 tcke = DIV_ROUND_UP(max(3 * clkper, 5625), clkper) - 1;
Nikita Kiryanovc4753462014-09-07 18:58:11 +03001324 if (ddr3_cfg->pagesz == 1) {
Tim Harvey8ab871b2014-06-02 16:13:23 -07001325 tfaw = DIV_ROUND_UP(37500, clkper) - 1;
Masahiro Yamadab62b39b2014-09-18 13:28:06 +09001326 trrd = DIV_ROUND_UP(max(4 * clkper, 7500), clkper) - 1;
Tim Harvey8ab871b2014-06-02 16:13:23 -07001327 } else {
1328 tfaw = DIV_ROUND_UP(50000, clkper) - 1;
Masahiro Yamadab62b39b2014-09-18 13:28:06 +09001329 trrd = DIV_ROUND_UP(max(4 * clkper, 10000), clkper) - 1;
Tim Harvey8ab871b2014-06-02 16:13:23 -07001330 }
1331 break;
Tim Harvey8ab871b2014-06-02 16:13:23 -07001332 default:
Nikita Kiryanovc4753462014-09-07 18:58:11 +03001333 puts("invalid memory speed\n");
Tim Harvey8ab871b2014-06-02 16:13:23 -07001334 hang();
1335 break;
1336 }
Masahiro Yamadab62b39b2014-09-18 13:28:06 +09001337 txpdll = DIV_ROUND_UP(max(10 * clkper, 24000), clkper) - 1;
1338 tcksre = DIV_ROUND_UP(max(5 * clkper, 10000), clkper);
Tim Harvey8ab871b2014-06-02 16:13:23 -07001339 taonpd = DIV_ROUND_UP(2000, clkper) - 1;
Nikita Kiryanovc4753462014-09-07 18:58:11 +03001340 tcksrx = tcksre;
Tim Harvey8ab871b2014-06-02 16:13:23 -07001341 taofpd = taonpd;
Nikita Kiryanovc4753462014-09-07 18:58:11 +03001342 twr = DIV_ROUND_UP(15000, clkper) - 1;
Masahiro Yamadab62b39b2014-09-18 13:28:06 +09001343 tmrd = DIV_ROUND_UP(max(12 * clkper, 15000), clkper) - 1;
Nikita Kiryanovc4753462014-09-07 18:58:11 +03001344 trc = DIV_ROUND_UP(ddr3_cfg->trcmin, clkper / 10) - 1;
1345 tras = DIV_ROUND_UP(ddr3_cfg->trasmin, clkper / 10) - 1;
1346 tcl = DIV_ROUND_UP(ddr3_cfg->trcd, clkper / 10) - 3;
1347 trp = DIV_ROUND_UP(ddr3_cfg->trcd, clkper / 10) - 1;
Masahiro Yamadab62b39b2014-09-18 13:28:06 +09001348 twtr = ROUND(max(4 * clkper, 7500) / clkper, 1) - 1;
Tim Harvey8ab871b2014-06-02 16:13:23 -07001349 trcd = trp;
Tim Harvey8ab871b2014-06-02 16:13:23 -07001350 trtp = twtr;
Nikita Kiryanov4a50ec22014-08-20 15:08:58 +03001351 cs0_end = 4 * sysinfo->cs_density - 1;
Nikita Kiryanovc4753462014-09-07 18:58:11 +03001352
1353 debug("density:%d Gb (%d Gb per chip)\n",
1354 sysinfo->cs_density, ddr3_cfg->density);
Tim Harvey8ab871b2014-06-02 16:13:23 -07001355 debug("clock: %dMHz (%d ps)\n", clock, clkper);
Nikolay Dimitrov99c25ff2015-04-22 18:37:31 +03001356 debug("memspd:%d\n", mem_speed);
Tim Harvey8ab871b2014-06-02 16:13:23 -07001357 debug("tcke=%d\n", tcke);
1358 debug("tcksrx=%d\n", tcksrx);
1359 debug("tcksre=%d\n", tcksre);
1360 debug("taofpd=%d\n", taofpd);
1361 debug("taonpd=%d\n", taonpd);
1362 debug("todtlon=%d\n", todtlon);
1363 debug("tanpd=%d\n", tanpd);
1364 debug("taxpd=%d\n", taxpd);
1365 debug("trfc=%d\n", trfc);
1366 debug("txs=%d\n", txs);
1367 debug("txp=%d\n", txp);
1368 debug("txpdll=%d\n", txpdll);
1369 debug("tfaw=%d\n", tfaw);
1370 debug("tcl=%d\n", tcl);
1371 debug("trcd=%d\n", trcd);
1372 debug("trp=%d\n", trp);
1373 debug("trc=%d\n", trc);
1374 debug("tras=%d\n", tras);
1375 debug("twr=%d\n", twr);
1376 debug("tmrd=%d\n", tmrd);
1377 debug("tcwl=%d\n", tcwl);
1378 debug("tdllk=%d\n", tdllk);
1379 debug("trtp=%d\n", trtp);
1380 debug("twtr=%d\n", twtr);
1381 debug("trrd=%d\n", trrd);
1382 debug("txpr=%d\n", txpr);
Nikita Kiryanovc4753462014-09-07 18:58:11 +03001383 debug("cs0_end=%d\n", cs0_end);
1384 debug("ncs=%d\n", sysinfo->ncs);
1385 debug("Rtt_wr=%d\n", sysinfo->rtt_wr);
1386 debug("Rtt_nom=%d\n", sysinfo->rtt_nom);
1387 debug("SRT=%d\n", ddr3_cfg->SRT);
Tim Harvey8ab871b2014-06-02 16:13:23 -07001388 debug("twr=%d\n", twr);
1389
1390 /*
1391 * board-specific configuration:
1392 * These values are determined empirically and vary per board layout
1393 * see:
1394 * appnote, ddr3 spreadsheet
1395 */
Nikita Kiryanovc4753462014-09-07 18:58:11 +03001396 mmdc0->mpwldectrl0 = calib->p0_mpwldectrl0;
1397 mmdc0->mpwldectrl1 = calib->p0_mpwldectrl1;
1398 mmdc0->mpdgctrl0 = calib->p0_mpdgctrl0;
1399 mmdc0->mpdgctrl1 = calib->p0_mpdgctrl1;
1400 mmdc0->mprddlctl = calib->p0_mprddlctl;
1401 mmdc0->mpwrdlctl = calib->p0_mpwrdlctl;
1402 if (sysinfo->dsize > 1) {
Peng Fan2ecdd022014-12-30 17:24:01 +08001403 MMDC1(mpwldectrl0, calib->p1_mpwldectrl0);
1404 MMDC1(mpwldectrl1, calib->p1_mpwldectrl1);
1405 MMDC1(mpdgctrl0, calib->p1_mpdgctrl0);
1406 MMDC1(mpdgctrl1, calib->p1_mpdgctrl1);
1407 MMDC1(mprddlctl, calib->p1_mprddlctl);
1408 MMDC1(mpwrdlctl, calib->p1_mpwrdlctl);
Tim Harvey8ab871b2014-06-02 16:13:23 -07001409 }
1410
1411 /* Read data DQ Byte0-3 delay */
Nikita Kiryanovc4753462014-09-07 18:58:11 +03001412 mmdc0->mprddqby0dl = 0x33333333;
1413 mmdc0->mprddqby1dl = 0x33333333;
1414 if (sysinfo->dsize > 0) {
1415 mmdc0->mprddqby2dl = 0x33333333;
1416 mmdc0->mprddqby3dl = 0x33333333;
Tim Harvey8ab871b2014-06-02 16:13:23 -07001417 }
Nikita Kiryanovc4753462014-09-07 18:58:11 +03001418
1419 if (sysinfo->dsize > 1) {
Peng Fan2ecdd022014-12-30 17:24:01 +08001420 MMDC1(mprddqby0dl, 0x33333333);
1421 MMDC1(mprddqby1dl, 0x33333333);
1422 MMDC1(mprddqby2dl, 0x33333333);
1423 MMDC1(mprddqby3dl, 0x33333333);
Tim Harvey8ab871b2014-06-02 16:13:23 -07001424 }
1425
1426 /* MMDC Termination: rtt_nom:2 RZQ/2(120ohm), rtt_nom:1 RZQ/4(60ohm) */
Nikita Kiryanovc4753462014-09-07 18:58:11 +03001427 val = (sysinfo->rtt_nom == 2) ? 0x00011117 : 0x00022227;
1428 mmdc0->mpodtctrl = val;
1429 if (sysinfo->dsize > 1)
Peng Fan2ecdd022014-12-30 17:24:01 +08001430 MMDC1(mpodtctrl, val);
Tim Harvey8ab871b2014-06-02 16:13:23 -07001431
1432 /* complete calibration */
Nikita Kiryanovc4753462014-09-07 18:58:11 +03001433 val = (1 << 11); /* Force measurement on delay-lines */
1434 mmdc0->mpmur0 = val;
1435 if (sysinfo->dsize > 1)
Peng Fan2ecdd022014-12-30 17:24:01 +08001436 MMDC1(mpmur0, val);
Tim Harvey8ab871b2014-06-02 16:13:23 -07001437
1438 /* Step 1: configuration request */
1439 mmdc0->mdscr = (u32)(1 << 15); /* config request */
1440
1441 /* Step 2: Timing configuration */
Nikita Kiryanovc4753462014-09-07 18:58:11 +03001442 mmdc0->mdcfg0 = (trfc << 24) | (txs << 16) | (txp << 13) |
1443 (txpdll << 9) | (tfaw << 4) | tcl;
1444 mmdc0->mdcfg1 = (trcd << 29) | (trp << 26) | (trc << 21) |
1445 (tras << 16) | (1 << 15) /* trpa */ |
1446 (twr << 9) | (tmrd << 5) | tcwl;
1447 mmdc0->mdcfg2 = (tdllk << 16) | (trtp << 6) | (twtr << 3) | trrd;
1448 mmdc0->mdotc = (taofpd << 27) | (taonpd << 24) | (tanpd << 20) |
1449 (taxpd << 16) | (todtlon << 12) | (todt_idle_off << 4);
1450 mmdc0->mdasp = cs0_end; /* CS addressing */
Tim Harvey8ab871b2014-06-02 16:13:23 -07001451
1452 /* Step 3: Configure DDR type */
Nikita Kiryanovc4753462014-09-07 18:58:11 +03001453 mmdc0->mdmisc = (sysinfo->cs1_mirror << 19) | (sysinfo->walat << 16) |
1454 (sysinfo->bi_on << 12) | (sysinfo->mif3_mode << 9) |
1455 (sysinfo->ralat << 6);
Tim Harvey8ab871b2014-06-02 16:13:23 -07001456
1457 /* Step 4: Configure delay while leaving reset */
Nikita Kiryanovc4753462014-09-07 18:58:11 +03001458 mmdc0->mdor = (txpr << 16) | (sysinfo->sde_to_rst << 8) |
1459 (sysinfo->rst_to_cke << 0);
Tim Harvey8ab871b2014-06-02 16:13:23 -07001460
1461 /* Step 5: Configure DDR physical parameters (density and burst len) */
Nikita Kiryanovc4753462014-09-07 18:58:11 +03001462 coladdr = ddr3_cfg->coladdr;
1463 if (ddr3_cfg->coladdr == 8) /* 8-bit COL is 0x3 */
Marek Vasut4a463602014-08-04 01:47:10 +02001464 coladdr += 4;
Nikita Kiryanovc4753462014-09-07 18:58:11 +03001465 else if (ddr3_cfg->coladdr == 12) /* 12-bit COL is 0x4 */
Marek Vasut4a463602014-08-04 01:47:10 +02001466 coladdr += 1;
Nikita Kiryanovc4753462014-09-07 18:58:11 +03001467 mmdc0->mdctl = (ddr3_cfg->rowaddr - 11) << 24 | /* ROW */
1468 (coladdr - 9) << 20 | /* COL */
1469 (1 << 19) | /* Burst Length = 8 for DDR3 */
1470 (sysinfo->dsize << 16); /* DDR data bus size */
Tim Harvey8ab871b2014-06-02 16:13:23 -07001471
1472 /* Step 6: Perform ZQ calibration */
Nikita Kiryanovc4753462014-09-07 18:58:11 +03001473 val = 0xa1390001; /* one-time HW ZQ calib */
1474 mmdc0->mpzqhwctrl = val;
1475 if (sysinfo->dsize > 1)
Peng Fan2ecdd022014-12-30 17:24:01 +08001476 MMDC1(mpzqhwctrl, val);
Tim Harvey8ab871b2014-06-02 16:13:23 -07001477
1478 /* Step 7: Enable MMDC with desired chip select */
Nikita Kiryanovc4753462014-09-07 18:58:11 +03001479 mmdc0->mdctl |= (1 << 31) | /* SDE_0 for CS0 */
1480 ((sysinfo->ncs == 2) ? 1 : 0) << 30; /* SDE_1 for CS1 */
Tim Harvey8ab871b2014-06-02 16:13:23 -07001481
1482 /* Step 8: Write Mode Registers to Init DDR3 devices */
Nikita Kiryanovc4753462014-09-07 18:58:11 +03001483 for (cs = 0; cs < sysinfo->ncs; cs++) {
Tim Harvey8ab871b2014-06-02 16:13:23 -07001484 /* MR2 */
Nikita Kiryanovc4753462014-09-07 18:58:11 +03001485 val = (sysinfo->rtt_wr & 3) << 9 | (ddr3_cfg->SRT & 1) << 7 |
Tim Harvey8ab871b2014-06-02 16:13:23 -07001486 ((tcwl - 3) & 3) << 3;
Tim Harveyfe1723f2015-04-03 16:52:52 -07001487 debug("MR2 CS%d: 0x%08x\n", cs, (u32)MR(val, 2, 3, cs));
Nikita Kiryanovc4753462014-09-07 18:58:11 +03001488 mmdc0->mdscr = MR(val, 2, 3, cs);
Tim Harvey8ab871b2014-06-02 16:13:23 -07001489 /* MR3 */
Tim Harveyfe1723f2015-04-03 16:52:52 -07001490 debug("MR3 CS%d: 0x%08x\n", cs, (u32)MR(0, 3, 3, cs));
Nikita Kiryanovc4753462014-09-07 18:58:11 +03001491 mmdc0->mdscr = MR(0, 3, 3, cs);
Tim Harvey8ab871b2014-06-02 16:13:23 -07001492 /* MR1 */
Nikita Kiryanovc4753462014-09-07 18:58:11 +03001493 val = ((sysinfo->rtt_nom & 1) ? 1 : 0) << 2 |
1494 ((sysinfo->rtt_nom & 2) ? 1 : 0) << 6;
Tim Harveyfe1723f2015-04-03 16:52:52 -07001495 debug("MR1 CS%d: 0x%08x\n", cs, (u32)MR(val, 1, 3, cs));
Nikita Kiryanovc4753462014-09-07 18:58:11 +03001496 mmdc0->mdscr = MR(val, 1, 3, cs);
1497 /* MR0 */
1498 val = ((tcl - 1) << 4) | /* CAS */
Tim Harvey8ab871b2014-06-02 16:13:23 -07001499 (1 << 8) | /* DLL Reset */
Tim Harvey591fe972015-05-18 07:07:02 -07001500 ((twr - 3) << 9) | /* Write Recovery */
1501 (sysinfo->pd_fast_exit << 12); /* Precharge PD PLL on */
Tim Harveyfe1723f2015-04-03 16:52:52 -07001502 debug("MR0 CS%d: 0x%08x\n", cs, (u32)MR(val, 0, 3, cs));
Nikita Kiryanovc4753462014-09-07 18:58:11 +03001503 mmdc0->mdscr = MR(val, 0, 3, cs);
Tim Harvey8ab871b2014-06-02 16:13:23 -07001504 /* ZQ calibration */
Nikita Kiryanovc4753462014-09-07 18:58:11 +03001505 val = (1 << 10);
1506 mmdc0->mdscr = MR(val, 0, 4, cs);
Tim Harvey8ab871b2014-06-02 16:13:23 -07001507 }
1508
1509 /* Step 10: Power down control and self-refresh */
Nikita Kiryanovc4753462014-09-07 18:58:11 +03001510 mmdc0->mdpdc = (tcke & 0x7) << 16 |
1511 5 << 12 | /* PWDT_1: 256 cycles */
1512 5 << 8 | /* PWDT_0: 256 cycles */
1513 1 << 6 | /* BOTH_CS_PD */
1514 (tcksrx & 0x7) << 3 |
1515 (tcksre & 0x7);
Tim Harveyfe1723f2015-04-03 16:52:52 -07001516 if (!sysinfo->pd_fast_exit)
1517 mmdc0->mdpdc |= (1 << 7); /* SLOW_PD */
Nikita Kiryanov6816f712014-08-20 15:08:56 +03001518 mmdc0->mapsr = 0x00001006; /* ADOPT power down enabled */
Tim Harvey8ab871b2014-06-02 16:13:23 -07001519
1520 /* Step 11: Configure ZQ calibration: one-time and periodic 1ms */
Nikita Kiryanovc4753462014-09-07 18:58:11 +03001521 val = 0xa1390003;
1522 mmdc0->mpzqhwctrl = val;
1523 if (sysinfo->dsize > 1)
Peng Fan2ecdd022014-12-30 17:24:01 +08001524 MMDC1(mpzqhwctrl, val);
Tim Harvey8ab871b2014-06-02 16:13:23 -07001525
1526 /* Step 12: Configure and activate periodic refresh */
Fabio Estevamcb3c1212016-08-29 20:37:15 -03001527 mmdc0->mdref = (sysinfo->refsel << 14) | (sysinfo->refr << 11);
Tim Harvey8ab871b2014-06-02 16:13:23 -07001528
1529 /* Step 13: Deassert config request - init complete */
Nikita Kiryanovc4753462014-09-07 18:58:11 +03001530 mmdc0->mdscr = 0x00000000;
Tim Harvey8ab871b2014-06-02 16:13:23 -07001531
1532 /* wait for auto-ZQ calibration to complete */
1533 mdelay(1);
1534}
Peng Fan77e86952015-08-17 16:11:03 +08001535
Eric Nelsonec4fe262016-10-30 16:33:49 -07001536void mmdc_read_calibration(struct mx6_ddr_sysinfo const *sysinfo,
1537 struct mx6_mmdc_calibration *calib)
1538{
1539 struct mmdc_p_regs *mmdc0 = (struct mmdc_p_regs *)MMDC_P0_BASE_ADDR;
1540 struct mmdc_p_regs *mmdc1 = (struct mmdc_p_regs *)MMDC_P1_BASE_ADDR;
1541
1542 calib->p0_mpwldectrl0 = readl(&mmdc0->mpwldectrl0);
1543 calib->p0_mpwldectrl1 = readl(&mmdc0->mpwldectrl1);
1544 calib->p0_mpdgctrl0 = readl(&mmdc0->mpdgctrl0);
1545 calib->p0_mpdgctrl1 = readl(&mmdc0->mpdgctrl1);
1546 calib->p0_mprddlctl = readl(&mmdc0->mprddlctl);
1547 calib->p0_mpwrdlctl = readl(&mmdc0->mpwrdlctl);
1548
1549 if (sysinfo->dsize == 2) {
1550 calib->p1_mpwldectrl0 = readl(&mmdc1->mpwldectrl0);
1551 calib->p1_mpwldectrl1 = readl(&mmdc1->mpwldectrl1);
1552 calib->p1_mpdgctrl0 = readl(&mmdc1->mpdgctrl0);
1553 calib->p1_mpdgctrl1 = readl(&mmdc1->mpdgctrl1);
1554 calib->p1_mprddlctl = readl(&mmdc1->mprddlctl);
1555 calib->p1_mpwrdlctl = readl(&mmdc1->mpwrdlctl);
1556 }
1557}
1558
Peng Fan77e86952015-08-17 16:11:03 +08001559void mx6_dram_cfg(const struct mx6_ddr_sysinfo *sysinfo,
1560 const struct mx6_mmdc_calibration *calib,
1561 const void *ddr_cfg)
1562{
1563 if (sysinfo->ddr_type == DDR_TYPE_DDR3) {
1564 mx6_ddr3_cfg(sysinfo, calib, ddr_cfg);
Peng Fanda7ada02015-08-17 16:11:04 +08001565 } else if (sysinfo->ddr_type == DDR_TYPE_LPDDR2) {
1566 mx6_lpddr2_cfg(sysinfo, calib, ddr_cfg);
Peng Fan77e86952015-08-17 16:11:03 +08001567 } else {
1568 puts("Unsupported ddr type\n");
1569 hang();
1570 }
1571}