blob: 69fe756b0b9f1f745cbf62cdce007db6b66ce286 [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
Bernhard Messerklinger9de2cb82020-03-09 10:55:34 +0100948/* see BOOT_CFG3 description Table 5-4. EIM Boot Fusemap */
949#define BOOT_CFG3_DDR_MASK 0x30
950#define BOOT_CFG3_EXT_DDR_MASK 0x33
951
952#define DDR_MMAP_NOC_SINGLE 0
953#define DDR_MMAP_NOC_DUAL 0x31
954
955/* NoC ACTIVATE shifts */
956#define NOC_RD_SHIFT 0
957#define NOC_FAW_PERIOD_SHIFT 4
958#define NOC_FAW_BANKS_SHIFT 10
959
960/* NoC DdrTiming shifts */
961#define NOC_ACT_TO_ACT_SHIFT 0
962#define NOC_RD_TO_MISS_SHIFT 6
963#define NOC_WR_TO_MISS_SHIFT 12
964#define NOC_BURST_LEN_SHIFT 18
965#define NOC_RD_TO_WR_SHIFT 21
966#define NOC_WR_TO_RD_SHIFT 26
967#define NOC_BW_RATIO_SHIFT 31
968
Peng Fanda7ada02015-08-17 16:11:04 +0800969/*
970 * According JESD209-2B-LPDDR2: Table 103
971 * WL: write latency
972 */
973static int lpddr2_wl(uint32_t mem_speed)
974{
975 switch (mem_speed) {
976 case 1066:
977 case 933:
978 return 4;
979 case 800:
980 return 3;
981 case 677:
982 case 533:
983 return 2;
984 case 400:
985 case 333:
986 return 1;
987 default:
988 puts("invalid memory speed\n");
989 hang();
990 }
991
992 return 0;
993}
994
995/*
996 * According JESD209-2B-LPDDR2: Table 103
997 * RL: read latency
998 */
999static int lpddr2_rl(uint32_t mem_speed)
1000{
1001 switch (mem_speed) {
1002 case 1066:
1003 return 8;
1004 case 933:
1005 return 7;
1006 case 800:
1007 return 6;
1008 case 677:
1009 return 5;
1010 case 533:
1011 return 4;
1012 case 400:
1013 case 333:
1014 return 3;
1015 default:
1016 puts("invalid memory speed\n");
1017 hang();
1018 }
1019
1020 return 0;
1021}
1022
1023void mx6_lpddr2_cfg(const struct mx6_ddr_sysinfo *sysinfo,
1024 const struct mx6_mmdc_calibration *calib,
1025 const struct mx6_lpddr2_cfg *lpddr2_cfg)
1026{
1027 volatile struct mmdc_p_regs *mmdc0;
1028 u32 val;
1029 u8 tcke, tcksrx, tcksre, trrd;
1030 u8 twl, txp, tfaw, tcl;
1031 u16 tras, twr, tmrd, trtp, twtr, trfc, txsr;
1032 u16 trcd_lp, trppb_lp, trpab_lp, trc_lp;
1033 u16 cs0_end;
1034 u8 coladdr;
1035 int clkper; /* clock period in picoseconds */
1036 int clock; /* clock freq in mHz */
1037 int cs;
1038
1039 /* only support 16/32 bits */
1040 if (sysinfo->dsize > 1)
1041 hang();
1042
1043 mmdc0 = (struct mmdc_p_regs *)MMDC_P0_BASE_ADDR;
1044
1045 clock = mxc_get_clock(MXC_DDR_CLK) / 1000000U;
1046 clkper = (1000 * 1000) / clock; /* pico seconds */
1047
1048 twl = lpddr2_wl(lpddr2_cfg->mem_speed) - 1;
1049
1050 /* LPDDR2-S2 and LPDDR2-S4 have the same tRFC value. */
1051 switch (lpddr2_cfg->density) {
1052 case 1:
1053 case 2:
1054 case 4:
1055 trfc = DIV_ROUND_UP(130000, clkper) - 1;
1056 txsr = DIV_ROUND_UP(140000, clkper) - 1;
1057 break;
1058 case 8:
1059 trfc = DIV_ROUND_UP(210000, clkper) - 1;
1060 txsr = DIV_ROUND_UP(220000, clkper) - 1;
1061 break;
1062 default:
1063 /*
1064 * 64Mb, 128Mb, 256Mb, 512Mb are not supported currently.
1065 */
1066 hang();
1067 break;
1068 }
1069 /*
1070 * txpdll, txpr, taonpd and taofpd are not relevant in LPDDR2 mode,
1071 * set them to 0. */
1072 txp = DIV_ROUND_UP(7500, clkper) - 1;
1073 tcke = 3;
1074 if (lpddr2_cfg->mem_speed == 333)
1075 tfaw = DIV_ROUND_UP(60000, clkper) - 1;
1076 else
1077 tfaw = DIV_ROUND_UP(50000, clkper) - 1;
1078 trrd = DIV_ROUND_UP(10000, clkper) - 1;
1079
1080 /* tckesr for LPDDR2 */
1081 tcksre = DIV_ROUND_UP(15000, clkper);
1082 tcksrx = tcksre;
1083 twr = DIV_ROUND_UP(15000, clkper) - 1;
1084 /*
1085 * tMRR: 2, tMRW: 5
1086 * tMRD should be set to max(tMRR, tMRW)
1087 */
1088 tmrd = 5;
1089 tras = DIV_ROUND_UP(lpddr2_cfg->trasmin, clkper / 10) - 1;
1090 /* LPDDR2 mode use tRCD_LP filed in MDCFG3. */
1091 trcd_lp = DIV_ROUND_UP(lpddr2_cfg->trcd_lp, clkper / 10) - 1;
1092 trc_lp = DIV_ROUND_UP(lpddr2_cfg->trasmin + lpddr2_cfg->trppb_lp,
1093 clkper / 10) - 1;
1094 trppb_lp = DIV_ROUND_UP(lpddr2_cfg->trppb_lp, clkper / 10) - 1;
1095 trpab_lp = DIV_ROUND_UP(lpddr2_cfg->trpab_lp, clkper / 10) - 1;
1096 /* To LPDDR2, CL in MDCFG0 refers to RL */
1097 tcl = lpddr2_rl(lpddr2_cfg->mem_speed) - 3;
1098 twtr = DIV_ROUND_UP(7500, clkper) - 1;
1099 trtp = DIV_ROUND_UP(7500, clkper) - 1;
1100
1101 cs0_end = 4 * sysinfo->cs_density - 1;
1102
1103 debug("density:%d Gb (%d Gb per chip)\n",
1104 sysinfo->cs_density, lpddr2_cfg->density);
1105 debug("clock: %dMHz (%d ps)\n", clock, clkper);
1106 debug("memspd:%d\n", lpddr2_cfg->mem_speed);
1107 debug("trcd_lp=%d\n", trcd_lp);
1108 debug("trppb_lp=%d\n", trppb_lp);
1109 debug("trpab_lp=%d\n", trpab_lp);
1110 debug("trc_lp=%d\n", trc_lp);
1111 debug("tcke=%d\n", tcke);
1112 debug("tcksrx=%d\n", tcksrx);
1113 debug("tcksre=%d\n", tcksre);
1114 debug("trfc=%d\n", trfc);
1115 debug("txsr=%d\n", txsr);
1116 debug("txp=%d\n", txp);
1117 debug("tfaw=%d\n", tfaw);
1118 debug("tcl=%d\n", tcl);
1119 debug("tras=%d\n", tras);
1120 debug("twr=%d\n", twr);
1121 debug("tmrd=%d\n", tmrd);
1122 debug("twl=%d\n", twl);
1123 debug("trtp=%d\n", trtp);
1124 debug("twtr=%d\n", twtr);
1125 debug("trrd=%d\n", trrd);
1126 debug("cs0_end=%d\n", cs0_end);
1127 debug("ncs=%d\n", sysinfo->ncs);
1128
1129 /*
1130 * board-specific configuration:
1131 * These values are determined empirically and vary per board layout
1132 */
1133 mmdc0->mpwldectrl0 = calib->p0_mpwldectrl0;
1134 mmdc0->mpwldectrl1 = calib->p0_mpwldectrl1;
1135 mmdc0->mpdgctrl0 = calib->p0_mpdgctrl0;
1136 mmdc0->mpdgctrl1 = calib->p0_mpdgctrl1;
1137 mmdc0->mprddlctl = calib->p0_mprddlctl;
1138 mmdc0->mpwrdlctl = calib->p0_mpwrdlctl;
1139 mmdc0->mpzqlp2ctl = calib->mpzqlp2ctl;
1140
1141 /* Read data DQ Byte0-3 delay */
1142 mmdc0->mprddqby0dl = 0x33333333;
1143 mmdc0->mprddqby1dl = 0x33333333;
1144 if (sysinfo->dsize > 0) {
1145 mmdc0->mprddqby2dl = 0x33333333;
1146 mmdc0->mprddqby3dl = 0x33333333;
1147 }
1148
1149 /* Write data DQ Byte0-3 delay */
1150 mmdc0->mpwrdqby0dl = 0xf3333333;
1151 mmdc0->mpwrdqby1dl = 0xf3333333;
1152 if (sysinfo->dsize > 0) {
1153 mmdc0->mpwrdqby2dl = 0xf3333333;
1154 mmdc0->mpwrdqby3dl = 0xf3333333;
1155 }
1156
1157 /*
1158 * In LPDDR2 mode this register should be cleared,
1159 * so no termination will be activated.
1160 */
1161 mmdc0->mpodtctrl = 0;
1162
1163 /* complete calibration */
1164 val = (1 << 11); /* Force measurement on delay-lines */
1165 mmdc0->mpmur0 = val;
1166
1167 /* Step 1: configuration request */
1168 mmdc0->mdscr = (u32)(1 << 15); /* config request */
1169
1170 /* Step 2: Timing configuration */
1171 mmdc0->mdcfg0 = (trfc << 24) | (txsr << 16) | (txp << 13) |
1172 (tfaw << 4) | tcl;
1173 mmdc0->mdcfg1 = (tras << 16) | (twr << 9) | (tmrd << 5) | twl;
1174 mmdc0->mdcfg2 = (trtp << 6) | (twtr << 3) | trrd;
1175 mmdc0->mdcfg3lp = (trc_lp << 16) | (trcd_lp << 8) |
1176 (trppb_lp << 4) | trpab_lp;
1177 mmdc0->mdotc = 0;
1178
1179 mmdc0->mdasp = cs0_end; /* CS addressing */
1180
1181 /* Step 3: Configure DDR type */
1182 mmdc0->mdmisc = (sysinfo->cs1_mirror << 19) | (sysinfo->walat << 16) |
1183 (sysinfo->bi_on << 12) | (sysinfo->mif3_mode << 9) |
1184 (sysinfo->ralat << 6) | (1 << 3);
1185
1186 /* Step 4: Configure delay while leaving reset */
1187 mmdc0->mdor = (sysinfo->sde_to_rst << 8) |
1188 (sysinfo->rst_to_cke << 0);
1189
1190 /* Step 5: Configure DDR physical parameters (density and burst len) */
1191 coladdr = lpddr2_cfg->coladdr;
1192 if (lpddr2_cfg->coladdr == 8) /* 8-bit COL is 0x3 */
1193 coladdr += 4;
1194 else if (lpddr2_cfg->coladdr == 12) /* 12-bit COL is 0x4 */
1195 coladdr += 1;
1196 mmdc0->mdctl = (lpddr2_cfg->rowaddr - 11) << 24 | /* ROW */
1197 (coladdr - 9) << 20 | /* COL */
1198 (0 << 19) | /* Burst Length = 4 for LPDDR2 */
1199 (sysinfo->dsize << 16); /* DDR data bus size */
1200
1201 /* Step 6: Perform ZQ calibration */
1202 val = 0xa1390003; /* one-time HW ZQ calib */
1203 mmdc0->mpzqhwctrl = val;
1204
1205 /* Step 7: Enable MMDC with desired chip select */
1206 mmdc0->mdctl |= (1 << 31) | /* SDE_0 for CS0 */
1207 ((sysinfo->ncs == 2) ? 1 : 0) << 30; /* SDE_1 for CS1 */
1208
1209 /* Step 8: Write Mode Registers to Init LPDDR2 devices */
1210 for (cs = 0; cs < sysinfo->ncs; cs++) {
1211 /* MR63: reset */
1212 mmdc0->mdscr = MR(63, 0, 3, cs);
1213 /* MR10: calibration,
1214 * 0xff is calibration command after intilization.
1215 */
1216 val = 0xA | (0xff << 8);
1217 mmdc0->mdscr = MR(val, 0, 3, cs);
1218 /* MR1 */
1219 val = 0x1 | (0x82 << 8);
1220 mmdc0->mdscr = MR(val, 0, 3, cs);
1221 /* MR2 */
1222 val = 0x2 | (0x04 << 8);
1223 mmdc0->mdscr = MR(val, 0, 3, cs);
1224 /* MR3 */
1225 val = 0x3 | (0x02 << 8);
1226 mmdc0->mdscr = MR(val, 0, 3, cs);
1227 }
1228
1229 /* Step 10: Power down control and self-refresh */
1230 mmdc0->mdpdc = (tcke & 0x7) << 16 |
1231 5 << 12 | /* PWDT_1: 256 cycles */
1232 5 << 8 | /* PWDT_0: 256 cycles */
1233 1 << 6 | /* BOTH_CS_PD */
1234 (tcksrx & 0x7) << 3 |
1235 (tcksre & 0x7);
1236 mmdc0->mapsr = 0x00001006; /* ADOPT power down enabled */
1237
1238 /* Step 11: Configure ZQ calibration: one-time and periodic 1ms */
1239 val = 0xa1310003;
1240 mmdc0->mpzqhwctrl = val;
1241
1242 /* Step 12: Configure and activate periodic refresh */
Fabio Estevamcb3c1212016-08-29 20:37:15 -03001243 mmdc0->mdref = (sysinfo->refsel << 14) | (sysinfo->refr << 11);
Peng Fanda7ada02015-08-17 16:11:04 +08001244
1245 /* Step 13: Deassert config request - init complete */
1246 mmdc0->mdscr = 0x00000000;
1247
1248 /* wait for auto-ZQ calibration to complete */
1249 mdelay(1);
1250}
1251
Peng Fan77e86952015-08-17 16:11:03 +08001252void mx6_ddr3_cfg(const struct mx6_ddr_sysinfo *sysinfo,
Nikita Kiryanovc4753462014-09-07 18:58:11 +03001253 const struct mx6_mmdc_calibration *calib,
1254 const struct mx6_ddr3_cfg *ddr3_cfg)
Tim Harvey8ab871b2014-06-02 16:13:23 -07001255{
1256 volatile struct mmdc_p_regs *mmdc0;
1257 volatile struct mmdc_p_regs *mmdc1;
Bernhard Messerklinger9de2cb82020-03-09 10:55:34 +01001258 struct src *src_regs = (struct src *)SRC_BASE_ADDR;
1259 u8 soc_boot_cfg3 = (readl(&src_regs->sbmr1) >> 16) & 0xff;
Nikita Kiryanovc4753462014-09-07 18:58:11 +03001260 u32 val;
Tim Harvey8ab871b2014-06-02 16:13:23 -07001261 u8 tcke, tcksrx, tcksre, txpdll, taofpd, taonpd, trrd;
1262 u8 todtlon, taxpd, tanpd, tcwl, txp, tfaw, tcl;
1263 u8 todt_idle_off = 0x4; /* from DDR3 Script Aid spreadsheet */
1264 u16 trcd, trc, tras, twr, tmrd, trtp, trp, twtr, trfc, txs, txpr;
Nikita Kiryanovc4753462014-09-07 18:58:11 +03001265 u16 cs0_end;
Tim Harvey8ab871b2014-06-02 16:13:23 -07001266 u16 tdllk = 0x1ff; /* DLL locking time: 512 cycles (JEDEC DDR3) */
Marek Vasut4a463602014-08-04 01:47:10 +02001267 u8 coladdr;
Tim Harvey8ab871b2014-06-02 16:13:23 -07001268 int clkper; /* clock period in picoseconds */
Nikolay Dimitrov99c25ff2015-04-22 18:37:31 +03001269 int clock; /* clock freq in MHz */
Tim Harvey8ab871b2014-06-02 16:13:23 -07001270 int cs;
Nikolay Dimitrov99c25ff2015-04-22 18:37:31 +03001271 u16 mem_speed = ddr3_cfg->mem_speed;
Tim Harvey8ab871b2014-06-02 16:13:23 -07001272
1273 mmdc0 = (struct mmdc_p_regs *)MMDC_P0_BASE_ADDR;
Fabio Estevam8548f972018-01-01 22:51:45 -02001274 if (!is_mx6sx() && !is_mx6ul() && !is_mx6ull() && !is_mx6sl())
Peng Fan98f11a12015-07-20 19:28:33 +08001275 mmdc1 = (struct mmdc_p_regs *)MMDC_P1_BASE_ADDR;
Tim Harvey8ab871b2014-06-02 16:13:23 -07001276
Nikolay Dimitrov99c25ff2015-04-22 18:37:31 +03001277 /* Limit mem_speed for MX6D/MX6Q */
Peng Fan9dba13b2016-05-23 18:35:57 +08001278 if (is_mx6dq() || is_mx6dqp()) {
Nikolay Dimitrov99c25ff2015-04-22 18:37:31 +03001279 if (mem_speed > 1066)
1280 mem_speed = 1066; /* 1066 MT/s */
1281
Tim Harvey8ab871b2014-06-02 16:13:23 -07001282 tcwl = 4;
1283 }
Nikolay Dimitrov99c25ff2015-04-22 18:37:31 +03001284 /* Limit mem_speed for MX6S/MX6DL */
Tim Harvey8ab871b2014-06-02 16:13:23 -07001285 else {
Nikolay Dimitrov99c25ff2015-04-22 18:37:31 +03001286 if (mem_speed > 800)
1287 mem_speed = 800; /* 800 MT/s */
1288
Tim Harvey8ab871b2014-06-02 16:13:23 -07001289 tcwl = 3;
1290 }
Nikolay Dimitrov99c25ff2015-04-22 18:37:31 +03001291
1292 clock = mem_speed / 2;
1293 /*
1294 * Data rate of 1066 MT/s requires 533 MHz DDR3 clock, but MX6D/Q supports
1295 * up to 528 MHz, so reduce the clock to fit chip specs
1296 */
Peng Fan9dba13b2016-05-23 18:35:57 +08001297 if (is_mx6dq() || is_mx6dqp()) {
Nikolay Dimitrov99c25ff2015-04-22 18:37:31 +03001298 if (clock > 528)
1299 clock = 528; /* 528 MHz */
1300 }
1301
Nikita Kiryanovc4753462014-09-07 18:58:11 +03001302 clkper = (1000 * 1000) / clock; /* pico seconds */
Tim Harvey8ab871b2014-06-02 16:13:23 -07001303 todtlon = tcwl;
1304 taxpd = tcwl;
1305 tanpd = tcwl;
Tim Harvey8ab871b2014-06-02 16:13:23 -07001306
Nikita Kiryanovc4753462014-09-07 18:58:11 +03001307 switch (ddr3_cfg->density) {
Tim Harvey8ab871b2014-06-02 16:13:23 -07001308 case 1: /* 1Gb per chip */
1309 trfc = DIV_ROUND_UP(110000, clkper) - 1;
1310 txs = DIV_ROUND_UP(120000, clkper) - 1;
1311 break;
1312 case 2: /* 2Gb per chip */
1313 trfc = DIV_ROUND_UP(160000, clkper) - 1;
1314 txs = DIV_ROUND_UP(170000, clkper) - 1;
1315 break;
1316 case 4: /* 4Gb per chip */
Peng Fanb96b74c2015-09-01 11:03:14 +08001317 trfc = DIV_ROUND_UP(260000, clkper) - 1;
1318 txs = DIV_ROUND_UP(270000, clkper) - 1;
Tim Harvey8ab871b2014-06-02 16:13:23 -07001319 break;
1320 case 8: /* 8Gb per chip */
1321 trfc = DIV_ROUND_UP(350000, clkper) - 1;
1322 txs = DIV_ROUND_UP(360000, clkper) - 1;
1323 break;
1324 default:
1325 /* invalid density */
Nikita Kiryanovc4753462014-09-07 18:58:11 +03001326 puts("invalid chip density\n");
Tim Harvey8ab871b2014-06-02 16:13:23 -07001327 hang();
1328 break;
1329 }
1330 txpr = txs;
1331
Nikolay Dimitrov99c25ff2015-04-22 18:37:31 +03001332 switch (mem_speed) {
Tim Harvey8ab871b2014-06-02 16:13:23 -07001333 case 800:
Masahiro Yamadab62b39b2014-09-18 13:28:06 +09001334 txp = DIV_ROUND_UP(max(3 * clkper, 7500), clkper) - 1;
1335 tcke = DIV_ROUND_UP(max(3 * clkper, 7500), clkper) - 1;
Nikita Kiryanovc4753462014-09-07 18:58:11 +03001336 if (ddr3_cfg->pagesz == 1) {
Tim Harvey8ab871b2014-06-02 16:13:23 -07001337 tfaw = DIV_ROUND_UP(40000, clkper) - 1;
Masahiro Yamadab62b39b2014-09-18 13:28:06 +09001338 trrd = DIV_ROUND_UP(max(4 * clkper, 10000), clkper) - 1;
Tim Harvey8ab871b2014-06-02 16:13:23 -07001339 } else {
1340 tfaw = DIV_ROUND_UP(50000, clkper) - 1;
Masahiro Yamadab62b39b2014-09-18 13:28:06 +09001341 trrd = DIV_ROUND_UP(max(4 * clkper, 10000), clkper) - 1;
Tim Harvey8ab871b2014-06-02 16:13:23 -07001342 }
1343 break;
1344 case 1066:
Masahiro Yamadab62b39b2014-09-18 13:28:06 +09001345 txp = DIV_ROUND_UP(max(3 * clkper, 7500), clkper) - 1;
1346 tcke = DIV_ROUND_UP(max(3 * clkper, 5625), clkper) - 1;
Nikita Kiryanovc4753462014-09-07 18:58:11 +03001347 if (ddr3_cfg->pagesz == 1) {
Tim Harvey8ab871b2014-06-02 16:13:23 -07001348 tfaw = DIV_ROUND_UP(37500, clkper) - 1;
Masahiro Yamadab62b39b2014-09-18 13:28:06 +09001349 trrd = DIV_ROUND_UP(max(4 * clkper, 7500), clkper) - 1;
Tim Harvey8ab871b2014-06-02 16:13:23 -07001350 } else {
1351 tfaw = DIV_ROUND_UP(50000, clkper) - 1;
Masahiro Yamadab62b39b2014-09-18 13:28:06 +09001352 trrd = DIV_ROUND_UP(max(4 * clkper, 10000), clkper) - 1;
Tim Harvey8ab871b2014-06-02 16:13:23 -07001353 }
1354 break;
Tim Harvey8ab871b2014-06-02 16:13:23 -07001355 default:
Nikita Kiryanovc4753462014-09-07 18:58:11 +03001356 puts("invalid memory speed\n");
Tim Harvey8ab871b2014-06-02 16:13:23 -07001357 hang();
1358 break;
1359 }
Masahiro Yamadab62b39b2014-09-18 13:28:06 +09001360 txpdll = DIV_ROUND_UP(max(10 * clkper, 24000), clkper) - 1;
1361 tcksre = DIV_ROUND_UP(max(5 * clkper, 10000), clkper);
Tim Harvey8ab871b2014-06-02 16:13:23 -07001362 taonpd = DIV_ROUND_UP(2000, clkper) - 1;
Nikita Kiryanovc4753462014-09-07 18:58:11 +03001363 tcksrx = tcksre;
Tim Harvey8ab871b2014-06-02 16:13:23 -07001364 taofpd = taonpd;
Nikita Kiryanovc4753462014-09-07 18:58:11 +03001365 twr = DIV_ROUND_UP(15000, clkper) - 1;
Masahiro Yamadab62b39b2014-09-18 13:28:06 +09001366 tmrd = DIV_ROUND_UP(max(12 * clkper, 15000), clkper) - 1;
Nikita Kiryanovc4753462014-09-07 18:58:11 +03001367 trc = DIV_ROUND_UP(ddr3_cfg->trcmin, clkper / 10) - 1;
1368 tras = DIV_ROUND_UP(ddr3_cfg->trasmin, clkper / 10) - 1;
1369 tcl = DIV_ROUND_UP(ddr3_cfg->trcd, clkper / 10) - 3;
1370 trp = DIV_ROUND_UP(ddr3_cfg->trcd, clkper / 10) - 1;
Masahiro Yamadab62b39b2014-09-18 13:28:06 +09001371 twtr = ROUND(max(4 * clkper, 7500) / clkper, 1) - 1;
Tim Harvey8ab871b2014-06-02 16:13:23 -07001372 trcd = trp;
Tim Harvey8ab871b2014-06-02 16:13:23 -07001373 trtp = twtr;
Nikita Kiryanov4a50ec22014-08-20 15:08:58 +03001374 cs0_end = 4 * sysinfo->cs_density - 1;
Nikita Kiryanovc4753462014-09-07 18:58:11 +03001375
1376 debug("density:%d Gb (%d Gb per chip)\n",
1377 sysinfo->cs_density, ddr3_cfg->density);
Tim Harvey8ab871b2014-06-02 16:13:23 -07001378 debug("clock: %dMHz (%d ps)\n", clock, clkper);
Nikolay Dimitrov99c25ff2015-04-22 18:37:31 +03001379 debug("memspd:%d\n", mem_speed);
Tim Harvey8ab871b2014-06-02 16:13:23 -07001380 debug("tcke=%d\n", tcke);
1381 debug("tcksrx=%d\n", tcksrx);
1382 debug("tcksre=%d\n", tcksre);
1383 debug("taofpd=%d\n", taofpd);
1384 debug("taonpd=%d\n", taonpd);
1385 debug("todtlon=%d\n", todtlon);
1386 debug("tanpd=%d\n", tanpd);
1387 debug("taxpd=%d\n", taxpd);
1388 debug("trfc=%d\n", trfc);
1389 debug("txs=%d\n", txs);
1390 debug("txp=%d\n", txp);
1391 debug("txpdll=%d\n", txpdll);
1392 debug("tfaw=%d\n", tfaw);
1393 debug("tcl=%d\n", tcl);
1394 debug("trcd=%d\n", trcd);
1395 debug("trp=%d\n", trp);
1396 debug("trc=%d\n", trc);
1397 debug("tras=%d\n", tras);
1398 debug("twr=%d\n", twr);
1399 debug("tmrd=%d\n", tmrd);
1400 debug("tcwl=%d\n", tcwl);
1401 debug("tdllk=%d\n", tdllk);
1402 debug("trtp=%d\n", trtp);
1403 debug("twtr=%d\n", twtr);
1404 debug("trrd=%d\n", trrd);
1405 debug("txpr=%d\n", txpr);
Nikita Kiryanovc4753462014-09-07 18:58:11 +03001406 debug("cs0_end=%d\n", cs0_end);
1407 debug("ncs=%d\n", sysinfo->ncs);
1408 debug("Rtt_wr=%d\n", sysinfo->rtt_wr);
1409 debug("Rtt_nom=%d\n", sysinfo->rtt_nom);
1410 debug("SRT=%d\n", ddr3_cfg->SRT);
Tim Harvey8ab871b2014-06-02 16:13:23 -07001411 debug("twr=%d\n", twr);
1412
1413 /*
1414 * board-specific configuration:
1415 * These values are determined empirically and vary per board layout
1416 * see:
1417 * appnote, ddr3 spreadsheet
1418 */
Nikita Kiryanovc4753462014-09-07 18:58:11 +03001419 mmdc0->mpwldectrl0 = calib->p0_mpwldectrl0;
1420 mmdc0->mpwldectrl1 = calib->p0_mpwldectrl1;
1421 mmdc0->mpdgctrl0 = calib->p0_mpdgctrl0;
1422 mmdc0->mpdgctrl1 = calib->p0_mpdgctrl1;
1423 mmdc0->mprddlctl = calib->p0_mprddlctl;
1424 mmdc0->mpwrdlctl = calib->p0_mpwrdlctl;
1425 if (sysinfo->dsize > 1) {
Peng Fan2ecdd022014-12-30 17:24:01 +08001426 MMDC1(mpwldectrl0, calib->p1_mpwldectrl0);
1427 MMDC1(mpwldectrl1, calib->p1_mpwldectrl1);
1428 MMDC1(mpdgctrl0, calib->p1_mpdgctrl0);
1429 MMDC1(mpdgctrl1, calib->p1_mpdgctrl1);
1430 MMDC1(mprddlctl, calib->p1_mprddlctl);
1431 MMDC1(mpwrdlctl, calib->p1_mpwrdlctl);
Tim Harvey8ab871b2014-06-02 16:13:23 -07001432 }
1433
1434 /* Read data DQ Byte0-3 delay */
Nikita Kiryanovc4753462014-09-07 18:58:11 +03001435 mmdc0->mprddqby0dl = 0x33333333;
1436 mmdc0->mprddqby1dl = 0x33333333;
1437 if (sysinfo->dsize > 0) {
1438 mmdc0->mprddqby2dl = 0x33333333;
1439 mmdc0->mprddqby3dl = 0x33333333;
Tim Harvey8ab871b2014-06-02 16:13:23 -07001440 }
Nikita Kiryanovc4753462014-09-07 18:58:11 +03001441
1442 if (sysinfo->dsize > 1) {
Peng Fan2ecdd022014-12-30 17:24:01 +08001443 MMDC1(mprddqby0dl, 0x33333333);
1444 MMDC1(mprddqby1dl, 0x33333333);
1445 MMDC1(mprddqby2dl, 0x33333333);
1446 MMDC1(mprddqby3dl, 0x33333333);
Tim Harvey8ab871b2014-06-02 16:13:23 -07001447 }
1448
1449 /* MMDC Termination: rtt_nom:2 RZQ/2(120ohm), rtt_nom:1 RZQ/4(60ohm) */
Nikita Kiryanovc4753462014-09-07 18:58:11 +03001450 val = (sysinfo->rtt_nom == 2) ? 0x00011117 : 0x00022227;
1451 mmdc0->mpodtctrl = val;
1452 if (sysinfo->dsize > 1)
Peng Fan2ecdd022014-12-30 17:24:01 +08001453 MMDC1(mpodtctrl, val);
Tim Harvey8ab871b2014-06-02 16:13:23 -07001454
1455 /* complete calibration */
Nikita Kiryanovc4753462014-09-07 18:58:11 +03001456 val = (1 << 11); /* Force measurement on delay-lines */
1457 mmdc0->mpmur0 = val;
1458 if (sysinfo->dsize > 1)
Peng Fan2ecdd022014-12-30 17:24:01 +08001459 MMDC1(mpmur0, val);
Tim Harvey8ab871b2014-06-02 16:13:23 -07001460
1461 /* Step 1: configuration request */
1462 mmdc0->mdscr = (u32)(1 << 15); /* config request */
1463
1464 /* Step 2: Timing configuration */
Nikita Kiryanovc4753462014-09-07 18:58:11 +03001465 mmdc0->mdcfg0 = (trfc << 24) | (txs << 16) | (txp << 13) |
1466 (txpdll << 9) | (tfaw << 4) | tcl;
1467 mmdc0->mdcfg1 = (trcd << 29) | (trp << 26) | (trc << 21) |
1468 (tras << 16) | (1 << 15) /* trpa */ |
1469 (twr << 9) | (tmrd << 5) | tcwl;
1470 mmdc0->mdcfg2 = (tdllk << 16) | (trtp << 6) | (twtr << 3) | trrd;
1471 mmdc0->mdotc = (taofpd << 27) | (taonpd << 24) | (tanpd << 20) |
1472 (taxpd << 16) | (todtlon << 12) | (todt_idle_off << 4);
1473 mmdc0->mdasp = cs0_end; /* CS addressing */
Tim Harvey8ab871b2014-06-02 16:13:23 -07001474
1475 /* Step 3: Configure DDR type */
Nikita Kiryanovc4753462014-09-07 18:58:11 +03001476 mmdc0->mdmisc = (sysinfo->cs1_mirror << 19) | (sysinfo->walat << 16) |
1477 (sysinfo->bi_on << 12) | (sysinfo->mif3_mode << 9) |
1478 (sysinfo->ralat << 6);
Tim Harvey8ab871b2014-06-02 16:13:23 -07001479
1480 /* Step 4: Configure delay while leaving reset */
Nikita Kiryanovc4753462014-09-07 18:58:11 +03001481 mmdc0->mdor = (txpr << 16) | (sysinfo->sde_to_rst << 8) |
1482 (sysinfo->rst_to_cke << 0);
Tim Harvey8ab871b2014-06-02 16:13:23 -07001483
1484 /* Step 5: Configure DDR physical parameters (density and burst len) */
Nikita Kiryanovc4753462014-09-07 18:58:11 +03001485 coladdr = ddr3_cfg->coladdr;
1486 if (ddr3_cfg->coladdr == 8) /* 8-bit COL is 0x3 */
Marek Vasut4a463602014-08-04 01:47:10 +02001487 coladdr += 4;
Nikita Kiryanovc4753462014-09-07 18:58:11 +03001488 else if (ddr3_cfg->coladdr == 12) /* 12-bit COL is 0x4 */
Marek Vasut4a463602014-08-04 01:47:10 +02001489 coladdr += 1;
Nikita Kiryanovc4753462014-09-07 18:58:11 +03001490 mmdc0->mdctl = (ddr3_cfg->rowaddr - 11) << 24 | /* ROW */
1491 (coladdr - 9) << 20 | /* COL */
1492 (1 << 19) | /* Burst Length = 8 for DDR3 */
1493 (sysinfo->dsize << 16); /* DDR data bus size */
Tim Harvey8ab871b2014-06-02 16:13:23 -07001494
1495 /* Step 6: Perform ZQ calibration */
Nikita Kiryanovc4753462014-09-07 18:58:11 +03001496 val = 0xa1390001; /* one-time HW ZQ calib */
1497 mmdc0->mpzqhwctrl = val;
1498 if (sysinfo->dsize > 1)
Peng Fan2ecdd022014-12-30 17:24:01 +08001499 MMDC1(mpzqhwctrl, val);
Tim Harvey8ab871b2014-06-02 16:13:23 -07001500
1501 /* Step 7: Enable MMDC with desired chip select */
Nikita Kiryanovc4753462014-09-07 18:58:11 +03001502 mmdc0->mdctl |= (1 << 31) | /* SDE_0 for CS0 */
1503 ((sysinfo->ncs == 2) ? 1 : 0) << 30; /* SDE_1 for CS1 */
Tim Harvey8ab871b2014-06-02 16:13:23 -07001504
1505 /* Step 8: Write Mode Registers to Init DDR3 devices */
Nikita Kiryanovc4753462014-09-07 18:58:11 +03001506 for (cs = 0; cs < sysinfo->ncs; cs++) {
Tim Harvey8ab871b2014-06-02 16:13:23 -07001507 /* MR2 */
Nikita Kiryanovc4753462014-09-07 18:58:11 +03001508 val = (sysinfo->rtt_wr & 3) << 9 | (ddr3_cfg->SRT & 1) << 7 |
Tim Harvey8ab871b2014-06-02 16:13:23 -07001509 ((tcwl - 3) & 3) << 3;
Tim Harveyfe1723f2015-04-03 16:52:52 -07001510 debug("MR2 CS%d: 0x%08x\n", cs, (u32)MR(val, 2, 3, cs));
Nikita Kiryanovc4753462014-09-07 18:58:11 +03001511 mmdc0->mdscr = MR(val, 2, 3, cs);
Tim Harvey8ab871b2014-06-02 16:13:23 -07001512 /* MR3 */
Tim Harveyfe1723f2015-04-03 16:52:52 -07001513 debug("MR3 CS%d: 0x%08x\n", cs, (u32)MR(0, 3, 3, cs));
Nikita Kiryanovc4753462014-09-07 18:58:11 +03001514 mmdc0->mdscr = MR(0, 3, 3, cs);
Tim Harvey8ab871b2014-06-02 16:13:23 -07001515 /* MR1 */
Nikita Kiryanovc4753462014-09-07 18:58:11 +03001516 val = ((sysinfo->rtt_nom & 1) ? 1 : 0) << 2 |
1517 ((sysinfo->rtt_nom & 2) ? 1 : 0) << 6;
Tim Harveyfe1723f2015-04-03 16:52:52 -07001518 debug("MR1 CS%d: 0x%08x\n", cs, (u32)MR(val, 1, 3, cs));
Nikita Kiryanovc4753462014-09-07 18:58:11 +03001519 mmdc0->mdscr = MR(val, 1, 3, cs);
1520 /* MR0 */
1521 val = ((tcl - 1) << 4) | /* CAS */
Tim Harvey8ab871b2014-06-02 16:13:23 -07001522 (1 << 8) | /* DLL Reset */
Tim Harvey591fe972015-05-18 07:07:02 -07001523 ((twr - 3) << 9) | /* Write Recovery */
1524 (sysinfo->pd_fast_exit << 12); /* Precharge PD PLL on */
Tim Harveyfe1723f2015-04-03 16:52:52 -07001525 debug("MR0 CS%d: 0x%08x\n", cs, (u32)MR(val, 0, 3, cs));
Nikita Kiryanovc4753462014-09-07 18:58:11 +03001526 mmdc0->mdscr = MR(val, 0, 3, cs);
Tim Harvey8ab871b2014-06-02 16:13:23 -07001527 /* ZQ calibration */
Nikita Kiryanovc4753462014-09-07 18:58:11 +03001528 val = (1 << 10);
1529 mmdc0->mdscr = MR(val, 0, 4, cs);
Tim Harvey8ab871b2014-06-02 16:13:23 -07001530 }
1531
1532 /* Step 10: Power down control and self-refresh */
Nikita Kiryanovc4753462014-09-07 18:58:11 +03001533 mmdc0->mdpdc = (tcke & 0x7) << 16 |
1534 5 << 12 | /* PWDT_1: 256 cycles */
1535 5 << 8 | /* PWDT_0: 256 cycles */
1536 1 << 6 | /* BOTH_CS_PD */
1537 (tcksrx & 0x7) << 3 |
1538 (tcksre & 0x7);
Tim Harveyfe1723f2015-04-03 16:52:52 -07001539 if (!sysinfo->pd_fast_exit)
1540 mmdc0->mdpdc |= (1 << 7); /* SLOW_PD */
Nikita Kiryanov6816f712014-08-20 15:08:56 +03001541 mmdc0->mapsr = 0x00001006; /* ADOPT power down enabled */
Tim Harvey8ab871b2014-06-02 16:13:23 -07001542
1543 /* Step 11: Configure ZQ calibration: one-time and periodic 1ms */
Nikita Kiryanovc4753462014-09-07 18:58:11 +03001544 val = 0xa1390003;
1545 mmdc0->mpzqhwctrl = val;
1546 if (sysinfo->dsize > 1)
Peng Fan2ecdd022014-12-30 17:24:01 +08001547 MMDC1(mpzqhwctrl, val);
Tim Harvey8ab871b2014-06-02 16:13:23 -07001548
1549 /* Step 12: Configure and activate periodic refresh */
Fabio Estevamcb3c1212016-08-29 20:37:15 -03001550 mmdc0->mdref = (sysinfo->refsel << 14) | (sysinfo->refr << 11);
Tim Harvey8ab871b2014-06-02 16:13:23 -07001551
Bernhard Messerklinger9de2cb82020-03-09 10:55:34 +01001552 /*
1553 * Step 13: i.MX6DQP only: If the NoC scheduler is enabled,
1554 * configure it and disable MMDC arbitration/reordering (see EB828)
1555 */
1556 if (is_mx6dqp() &&
1557 ((soc_boot_cfg3 & BOOT_CFG3_DDR_MASK) == DDR_MMAP_NOC_SINGLE ||
1558 (soc_boot_cfg3 & BOOT_CFG3_EXT_DDR_MASK) == DDR_MMAP_NOC_DUAL)) {
1559 struct mx6dqp_noc_sched_regs *noc_sched =
1560 (struct mx6dqp_noc_sched_regs *)MX6DQP_NOC_SCHED_BASE;
1561
1562 /*
1563 * These values are fixed based on integration parameters and
1564 * should not be modified
1565 */
1566 noc_sched->rlat = 0x00000040;
1567 noc_sched->ipu1 = 0x00000020;
1568 noc_sched->ipu2 = 0x00000020;
1569
1570 noc_sched->activate = (1 << NOC_FAW_BANKS_SHIFT) |
1571 (tfaw << NOC_FAW_PERIOD_SHIFT) |
1572 (trrd << NOC_RD_SHIFT);
1573 noc_sched->ddrtiming = (((sysinfo->dsize == 1) ? 1 : 0)
1574 << NOC_BW_RATIO_SHIFT) |
1575 ((tcwl + twtr) << NOC_WR_TO_RD_SHIFT) |
1576 ((tcl - tcwl + 2) << NOC_RD_TO_WR_SHIFT) |
1577 (4 << NOC_BURST_LEN_SHIFT) | /* BL8 */
1578 ((tcwl + twr + trp + trcd)
1579 << NOC_WR_TO_MISS_SHIFT) |
1580 ((trtp + trp + trcd - 4)
1581 << NOC_RD_TO_MISS_SHIFT) |
1582 (trc << NOC_ACT_TO_ACT_SHIFT);
1583
1584 if (sysinfo->dsize == 2) {
1585 if (ddr3_cfg->coladdr == 10) {
1586 if (ddr3_cfg->rowaddr == 15 &&
1587 sysinfo->ncs == 2)
1588 noc_sched->ddrconf = 4;
1589 else
1590 noc_sched->ddrconf = 0;
1591 } else if (ddr3_cfg->coladdr == 11) {
1592 noc_sched->ddrconf = 1;
1593 }
1594 } else {
1595 if (ddr3_cfg->coladdr == 9) {
1596 if (ddr3_cfg->rowaddr == 13)
1597 noc_sched->ddrconf = 2;
1598 else if (ddr3_cfg->rowaddr == 14)
1599 noc_sched->ddrconf = 15;
1600 } else if (ddr3_cfg->coladdr == 10) {
1601 if (ddr3_cfg->rowaddr == 14 &&
1602 sysinfo->ncs == 2)
1603 noc_sched->ddrconf = 14;
1604 else if (ddr3_cfg->rowaddr == 15 &&
1605 sysinfo->ncs == 2)
1606 noc_sched->ddrconf = 9;
1607 else
1608 noc_sched->ddrconf = 3;
1609 } else if (ddr3_cfg->coladdr == 11) {
1610 if (ddr3_cfg->rowaddr == 15 &&
1611 sysinfo->ncs == 2)
1612 noc_sched->ddrconf = 4;
1613 else
1614 noc_sched->ddrconf = 0;
1615 } else if (ddr3_cfg->coladdr == 12) {
1616 if (ddr3_cfg->rowaddr == 14)
1617 noc_sched->ddrconf = 1;
1618 }
1619 }
1620
1621 /* Disable MMDC arbitration/reordering */
1622 mmdc0->maarcr = 0x14420000;
1623 }
1624
Tim Harvey8ab871b2014-06-02 16:13:23 -07001625 /* Step 13: Deassert config request - init complete */
Nikita Kiryanovc4753462014-09-07 18:58:11 +03001626 mmdc0->mdscr = 0x00000000;
Tim Harvey8ab871b2014-06-02 16:13:23 -07001627
1628 /* wait for auto-ZQ calibration to complete */
1629 mdelay(1);
1630}
Peng Fan77e86952015-08-17 16:11:03 +08001631
Eric Nelsonec4fe262016-10-30 16:33:49 -07001632void mmdc_read_calibration(struct mx6_ddr_sysinfo const *sysinfo,
1633 struct mx6_mmdc_calibration *calib)
1634{
1635 struct mmdc_p_regs *mmdc0 = (struct mmdc_p_regs *)MMDC_P0_BASE_ADDR;
1636 struct mmdc_p_regs *mmdc1 = (struct mmdc_p_regs *)MMDC_P1_BASE_ADDR;
1637
1638 calib->p0_mpwldectrl0 = readl(&mmdc0->mpwldectrl0);
1639 calib->p0_mpwldectrl1 = readl(&mmdc0->mpwldectrl1);
1640 calib->p0_mpdgctrl0 = readl(&mmdc0->mpdgctrl0);
1641 calib->p0_mpdgctrl1 = readl(&mmdc0->mpdgctrl1);
1642 calib->p0_mprddlctl = readl(&mmdc0->mprddlctl);
1643 calib->p0_mpwrdlctl = readl(&mmdc0->mpwrdlctl);
1644
1645 if (sysinfo->dsize == 2) {
1646 calib->p1_mpwldectrl0 = readl(&mmdc1->mpwldectrl0);
1647 calib->p1_mpwldectrl1 = readl(&mmdc1->mpwldectrl1);
1648 calib->p1_mpdgctrl0 = readl(&mmdc1->mpdgctrl0);
1649 calib->p1_mpdgctrl1 = readl(&mmdc1->mpdgctrl1);
1650 calib->p1_mprddlctl = readl(&mmdc1->mprddlctl);
1651 calib->p1_mpwrdlctl = readl(&mmdc1->mpwrdlctl);
1652 }
1653}
1654
Peng Fan77e86952015-08-17 16:11:03 +08001655void mx6_dram_cfg(const struct mx6_ddr_sysinfo *sysinfo,
1656 const struct mx6_mmdc_calibration *calib,
1657 const void *ddr_cfg)
1658{
1659 if (sysinfo->ddr_type == DDR_TYPE_DDR3) {
1660 mx6_ddr3_cfg(sysinfo, calib, ddr_cfg);
Peng Fanda7ada02015-08-17 16:11:04 +08001661 } else if (sysinfo->ddr_type == DDR_TYPE_LPDDR2) {
1662 mx6_lpddr2_cfg(sysinfo, calib, ddr_cfg);
Peng Fan77e86952015-08-17 16:11:03 +08001663 } else {
1664 puts("Unsupported ddr type\n");
1665 hang();
1666 }
1667}