blob: b15f376da43587f5508d19e2587a07d39694f729 [file] [log] [blame]
Tim Harveyfe0f7f72014-06-02 16:13:23 -07001/*
2 * Copyright (C) 2014 Gateworks Corporation
3 * Author: Tim Harvey <tharvey@gateworks.com>
4 *
5 * SPDX-License-Identifier: GPL-2.0+
6 */
7
8#include <common.h>
9#include <linux/types.h>
Peng Faneb796cb2015-08-17 16:11:04 +080010#include <asm/arch/clock.h>
Tim Harveyfe0f7f72014-06-02 16:13:23 -070011#include <asm/arch/mx6-ddr.h>
12#include <asm/arch/sys_proto.h>
13#include <asm/io.h>
14#include <asm/types.h>
Marek Vasutb10d93e2016-03-02 14:49:51 +010015#include <wait_bit.h>
Tim Harveyfe0f7f72014-06-02 16:13:23 -070016
Marek Vasutd339f162015-12-16 15:40:06 +010017#if defined(CONFIG_MX6QDL) || defined(CONFIG_MX6Q) || defined(CONFIG_MX6D)
18
Marek Vasutd339f162015-12-16 15:40:06 +010019static void reset_read_data_fifos(void)
20{
21 struct mmdc_p_regs *mmdc0 = (struct mmdc_p_regs *)MMDC_P0_BASE_ADDR;
22
23 /* Reset data FIFOs twice. */
24 setbits_le32(&mmdc0->mpdgctrl0, 1 << 31);
Marek Vasutb10d93e2016-03-02 14:49:51 +010025 wait_for_bit("MMDC", &mmdc0->mpdgctrl0, 1 << 31, 0, 100, 0);
Marek Vasutd339f162015-12-16 15:40:06 +010026
27 setbits_le32(&mmdc0->mpdgctrl0, 1 << 31);
Marek Vasutb10d93e2016-03-02 14:49:51 +010028 wait_for_bit("MMDC", &mmdc0->mpdgctrl0, 1 << 31, 0, 100, 0);
Marek Vasutd339f162015-12-16 15:40:06 +010029}
30
31static void precharge_all(const bool cs0_enable, const bool cs1_enable)
32{
33 struct mmdc_p_regs *mmdc0 = (struct mmdc_p_regs *)MMDC_P0_BASE_ADDR;
34
35 /*
36 * Issue the Precharge-All command to the DDR device for both
37 * chip selects. Note, CON_REQ bit should also remain set. If
38 * only using one chip select, then precharge only the desired
39 * chip select.
40 */
41 if (cs0_enable) { /* CS0 */
42 writel(0x04008050, &mmdc0->mdscr);
Marek Vasutb10d93e2016-03-02 14:49:51 +010043 wait_for_bit("MMDC", &mmdc0->mdscr, 1 << 14, 1, 100, 0);
Marek Vasutd339f162015-12-16 15:40:06 +010044 }
45
46 if (cs1_enable) { /* CS1 */
47 writel(0x04008058, &mmdc0->mdscr);
Marek Vasutb10d93e2016-03-02 14:49:51 +010048 wait_for_bit("MMDC", &mmdc0->mdscr, 1 << 14, 1, 100, 0);
Marek Vasutd339f162015-12-16 15:40:06 +010049 }
50}
51
52static void force_delay_measurement(int bus_size)
53{
54 struct mmdc_p_regs *mmdc0 = (struct mmdc_p_regs *)MMDC_P0_BASE_ADDR;
55 struct mmdc_p_regs *mmdc1 = (struct mmdc_p_regs *)MMDC_P1_BASE_ADDR;
56
57 writel(0x800, &mmdc0->mpmur0);
58 if (bus_size == 0x2)
59 writel(0x800, &mmdc1->mpmur0);
60}
61
62static void modify_dg_result(u32 *reg_st0, u32 *reg_st1, u32 *reg_ctrl)
63{
64 u32 dg_tmp_val, dg_dl_abs_offset, dg_hc_del, val_ctrl;
65
66 /*
67 * DQS gating absolute offset should be modified from reflecting
68 * (HW_DG_LOWx + HW_DG_UPx)/2 to reflecting (HW_DG_UPx - 0x80)
69 */
70
71 val_ctrl = readl(reg_ctrl);
72 val_ctrl &= 0xf0000000;
73
74 dg_tmp_val = ((readl(reg_st0) & 0x07ff0000) >> 16) - 0xc0;
75 dg_dl_abs_offset = dg_tmp_val & 0x7f;
76 dg_hc_del = (dg_tmp_val & 0x780) << 1;
77
78 val_ctrl |= dg_dl_abs_offset + dg_hc_del;
79
80 dg_tmp_val = ((readl(reg_st1) & 0x07ff0000) >> 16) - 0xc0;
81 dg_dl_abs_offset = dg_tmp_val & 0x7f;
82 dg_hc_del = (dg_tmp_val & 0x780) << 1;
83
84 val_ctrl |= (dg_dl_abs_offset + dg_hc_del) << 16;
85
86 writel(val_ctrl, reg_ctrl);
87}
88
89int mmdc_do_write_level_calibration(void)
90{
91 struct mmdc_p_regs *mmdc0 = (struct mmdc_p_regs *)MMDC_P0_BASE_ADDR;
92 struct mmdc_p_regs *mmdc1 = (struct mmdc_p_regs *)MMDC_P1_BASE_ADDR;
93 u32 esdmisc_val, zq_val;
94 u32 errors = 0;
95 u32 ldectrl[4];
96 u32 ddr_mr1 = 0x4;
97
98 /*
99 * Stash old values in case calibration fails,
100 * we need to restore them
101 */
102 ldectrl[0] = readl(&mmdc0->mpwldectrl0);
103 ldectrl[1] = readl(&mmdc0->mpwldectrl1);
104 ldectrl[2] = readl(&mmdc1->mpwldectrl0);
105 ldectrl[3] = readl(&mmdc1->mpwldectrl1);
106
107 /* disable DDR logic power down timer */
108 clrbits_le32(&mmdc0->mdpdc, 0xff00);
109
110 /* disable Adopt power down timer */
111 setbits_le32(&mmdc0->mapsr, 0x1);
112
113 debug("Starting write leveling calibration.\n");
114
115 /*
116 * 2. disable auto refresh and ZQ calibration
117 * before proceeding with Write Leveling calibration
118 */
119 esdmisc_val = readl(&mmdc0->mdref);
120 writel(0x0000C000, &mmdc0->mdref);
121 zq_val = readl(&mmdc0->mpzqhwctrl);
122 writel(zq_val & ~0x3, &mmdc0->mpzqhwctrl);
123
124 /* 3. increase walat and ralat to maximum */
125 setbits_le32(&mmdc0->mdmisc,
126 (1 << 6) | (1 << 7) | (1 << 8) | (1 << 16) | (1 << 17));
127 setbits_le32(&mmdc1->mdmisc,
128 (1 << 6) | (1 << 7) | (1 << 8) | (1 << 16) | (1 << 17));
129 /*
130 * 4 & 5. Configure the external DDR device to enter write-leveling
131 * mode through Load Mode Register command.
132 * Register setting:
133 * Bits[31:16] MR1 value (0x0080 write leveling enable)
134 * Bit[9] set WL_EN to enable MMDC DQS output
135 * Bits[6:4] set CMD bits for Load Mode Register programming
136 * Bits[2:0] set CMD_BA to 0x1 for DDR MR1 programming
137 */
138 writel(0x00808231, &mmdc0->mdscr);
139
140 /* 6. Activate automatic calibration by setting MPWLGCR[HW_WL_EN] */
141 writel(0x00000001, &mmdc0->mpwlgcr);
142
143 /*
144 * 7. Upon completion of this process the MMDC de-asserts
145 * the MPWLGCR[HW_WL_EN]
146 */
Marek Vasutb10d93e2016-03-02 14:49:51 +0100147 wait_for_bit("MMDC", &mmdc0->mpwlgcr, 1 << 0, 0, 100, 0);
Marek Vasutd339f162015-12-16 15:40:06 +0100148
149 /*
150 * 8. check for any errors: check both PHYs for x64 configuration,
151 * if x32, check only PHY0
152 */
153 if (readl(&mmdc0->mpwlgcr) & 0x00000F00)
154 errors |= 1;
155 if (readl(&mmdc1->mpwlgcr) & 0x00000F00)
156 errors |= 2;
157
158 debug("Ending write leveling calibration. Error mask: 0x%x\n", errors);
159
160 /* check to see if cal failed */
161 if ((readl(&mmdc0->mpwldectrl0) == 0x001F001F) &&
162 (readl(&mmdc0->mpwldectrl1) == 0x001F001F) &&
163 (readl(&mmdc1->mpwldectrl0) == 0x001F001F) &&
164 (readl(&mmdc1->mpwldectrl1) == 0x001F001F)) {
165 debug("Cal seems to have soft-failed due to memory not supporting write leveling on all channels. Restoring original write leveling values.\n");
166 writel(ldectrl[0], &mmdc0->mpwldectrl0);
167 writel(ldectrl[1], &mmdc0->mpwldectrl1);
168 writel(ldectrl[2], &mmdc1->mpwldectrl0);
169 writel(ldectrl[3], &mmdc1->mpwldectrl1);
170 errors |= 4;
171 }
172
173 /*
174 * User should issue MRS command to exit write leveling mode
175 * through Load Mode Register command
176 * Register setting:
177 * Bits[31:16] MR1 value "ddr_mr1" value from initialization
178 * Bit[9] clear WL_EN to disable MMDC DQS output
179 * Bits[6:4] set CMD bits for Load Mode Register programming
180 * Bits[2:0] set CMD_BA to 0x1 for DDR MR1 programming
181 */
182 writel((ddr_mr1 << 16) + 0x8031, &mmdc0->mdscr);
183
184 /* re-enable auto refresh and zq cal */
185 writel(esdmisc_val, &mmdc0->mdref);
186 writel(zq_val, &mmdc0->mpzqhwctrl);
187
188 debug("\tMMDC_MPWLDECTRL0 after write level cal: 0x%08X\n",
189 readl(&mmdc0->mpwldectrl0));
190 debug("\tMMDC_MPWLDECTRL1 after write level cal: 0x%08X\n",
191 readl(&mmdc0->mpwldectrl1));
192 debug("\tMMDC_MPWLDECTRL0 after write level cal: 0x%08X\n",
193 readl(&mmdc1->mpwldectrl0));
194 debug("\tMMDC_MPWLDECTRL1 after write level cal: 0x%08X\n",
195 readl(&mmdc1->mpwldectrl1));
196
197 /* We must force a readback of these values, to get them to stick */
198 readl(&mmdc0->mpwldectrl0);
199 readl(&mmdc0->mpwldectrl1);
200 readl(&mmdc1->mpwldectrl0);
201 readl(&mmdc1->mpwldectrl1);
202
203 /* enable DDR logic power down timer: */
204 setbits_le32(&mmdc0->mdpdc, 0x00005500);
205
206 /* Enable Adopt power down timer: */
207 clrbits_le32(&mmdc0->mapsr, 0x1);
208
209 /* Clear CON_REQ */
210 writel(0, &mmdc0->mdscr);
211
212 return errors;
213}
214
215int mmdc_do_dqs_calibration(void)
216{
217 struct mmdc_p_regs *mmdc0 = (struct mmdc_p_regs *)MMDC_P0_BASE_ADDR;
218 struct mmdc_p_regs *mmdc1 = (struct mmdc_p_regs *)MMDC_P1_BASE_ADDR;
219 struct mx6dq_iomux_ddr_regs *mx6_ddr_iomux =
220 (struct mx6dq_iomux_ddr_regs *)MX6DQ_IOM_DDR_BASE;
221 bool cs0_enable;
222 bool cs1_enable;
223 bool cs0_enable_initial;
224 bool cs1_enable_initial;
225 u32 esdmisc_val;
226 u32 bus_size;
227 u32 temp_ref;
228 u32 pddword = 0x00ffff00; /* best so far, place into MPPDCMPR1 */
229 u32 errors = 0;
230 u32 initdelay = 0x40404040;
231
232 /* check to see which chip selects are enabled */
233 cs0_enable_initial = readl(&mmdc0->mdctl) & 0x80000000;
234 cs1_enable_initial = readl(&mmdc0->mdctl) & 0x40000000;
235
236 /* disable DDR logic power down timer: */
237 clrbits_le32(&mmdc0->mdpdc, 0xff00);
238
239 /* disable Adopt power down timer: */
240 setbits_le32(&mmdc0->mapsr, 0x1);
241
242 /* set DQS pull ups */
243 setbits_le32(&mx6_ddr_iomux->dram_sdqs0, 0x7000);
244 setbits_le32(&mx6_ddr_iomux->dram_sdqs1, 0x7000);
245 setbits_le32(&mx6_ddr_iomux->dram_sdqs2, 0x7000);
246 setbits_le32(&mx6_ddr_iomux->dram_sdqs3, 0x7000);
247 setbits_le32(&mx6_ddr_iomux->dram_sdqs4, 0x7000);
248 setbits_le32(&mx6_ddr_iomux->dram_sdqs5, 0x7000);
249 setbits_le32(&mx6_ddr_iomux->dram_sdqs6, 0x7000);
250 setbits_le32(&mx6_ddr_iomux->dram_sdqs7, 0x7000);
251
252 /* Save old RALAT and WALAT values */
253 esdmisc_val = readl(&mmdc0->mdmisc);
254
255 setbits_le32(&mmdc0->mdmisc,
256 (1 << 6) | (1 << 7) | (1 << 8) | (1 << 16) | (1 << 17));
257
258 /* Disable auto refresh before proceeding with calibration */
259 temp_ref = readl(&mmdc0->mdref);
260 writel(0x0000c000, &mmdc0->mdref);
261
262 /*
263 * Per the ref manual, issue one refresh cycle MDSCR[CMD]= 0x2,
264 * this also sets the CON_REQ bit.
265 */
266 if (cs0_enable_initial)
267 writel(0x00008020, &mmdc0->mdscr);
268 if (cs1_enable_initial)
269 writel(0x00008028, &mmdc0->mdscr);
270
271 /* poll to make sure the con_ack bit was asserted */
Marek Vasutb10d93e2016-03-02 14:49:51 +0100272 wait_for_bit("MMDC", &mmdc0->mdscr, 1 << 14, 1, 100, 0);
Marek Vasutd339f162015-12-16 15:40:06 +0100273
274 /*
275 * Check MDMISC register CALIB_PER_CS to see which CS calibration
276 * is targeted to (under normal cases, it should be cleared
277 * as this is the default value, indicating calibration is directed
278 * to CS0).
279 * Disable the other chip select not being target for calibration
280 * to avoid any potential issues. This will get re-enabled at end
281 * of calibration.
282 */
283 if ((readl(&mmdc0->mdmisc) & 0x00100000) == 0)
284 clrbits_le32(&mmdc0->mdctl, 1 << 30); /* clear SDE_1 */
285 else
286 clrbits_le32(&mmdc0->mdctl, 1 << 31); /* clear SDE_0 */
287
288 /*
289 * Check to see which chip selects are now enabled for
290 * the remainder of the calibration.
291 */
292 cs0_enable = readl(&mmdc0->mdctl) & 0x80000000;
293 cs1_enable = readl(&mmdc0->mdctl) & 0x40000000;
294
295 /* Check to see what the data bus size is */
296 bus_size = (readl(&mmdc0->mdctl) & 0x30000) >> 16;
297 debug("Data bus size: %d (%d bits)\n", bus_size, 1 << (bus_size + 4));
298
299 precharge_all(cs0_enable, cs1_enable);
300
301 /* Write the pre-defined value into MPPDCMPR1 */
302 writel(pddword, &mmdc0->mppdcmpr1);
303
304 /*
305 * Issue a write access to the external DDR device by setting
306 * the bit SW_DUMMY_WR (bit 0) in the MPSWDAR0 and then poll
307 * this bit until it clears to indicate completion of the write access.
308 */
309 setbits_le32(&mmdc0->mpswdar0, 1);
Marek Vasutb10d93e2016-03-02 14:49:51 +0100310 wait_for_bit("MMDC", &mmdc0->mpswdar0, 1 << 0, 0, 100, 0);
Marek Vasutd339f162015-12-16 15:40:06 +0100311
312 /* Set the RD_DL_ABS# bits to their default values
313 * (will be calibrated later in the read delay-line calibration).
314 * Both PHYs for x64 configuration, if x32, do only PHY0.
315 */
316 writel(initdelay, &mmdc0->mprddlctl);
317 if (bus_size == 0x2)
318 writel(initdelay, &mmdc1->mprddlctl);
319
320 /* Force a measurment, for previous delay setup to take effect. */
321 force_delay_measurement(bus_size);
322
323 /*
324 * ***************************
325 * Read DQS Gating calibration
326 * ***************************
327 */
328 debug("Starting Read DQS Gating calibration.\n");
329
330 /*
331 * Reset the read data FIFOs (two resets); only need to issue reset
332 * to PHY0 since in x64 mode, the reset will also go to PHY1.
333 */
334 reset_read_data_fifos();
335
336 /*
337 * Start the automatic read DQS gating calibration process by
338 * asserting MPDGCTRL0[HW_DG_EN] and MPDGCTRL0[DG_CMP_CYC]
339 * and then poll MPDGCTRL0[HW_DG_EN]] until this bit clears
340 * to indicate completion.
341 * Also, ensure that MPDGCTRL0[HW_DG_ERR] is clear to indicate
342 * no errors were seen during calibration.
343 */
344
345 /*
346 * Set bit 30: chooses option to wait 32 cycles instead of
347 * 16 before comparing read data.
348 */
349 setbits_le32(&mmdc0->mpdgctrl0, 1 << 30);
Eric Nelsonb33f74e2016-10-30 16:33:47 -0700350 if (sysinfo->dsize == 2)
351 setbits_le32(&mmdc1->mpdgctrl0, 1 << 30);
Marek Vasutd339f162015-12-16 15:40:06 +0100352
353 /* Set bit 28 to start automatic read DQS gating calibration */
354 setbits_le32(&mmdc0->mpdgctrl0, 5 << 28);
355
356 /* Poll for completion. MPDGCTRL0[HW_DG_EN] should be 0 */
Marek Vasutb10d93e2016-03-02 14:49:51 +0100357 wait_for_bit("MMDC", &mmdc0->mpdgctrl0, 1 << 28, 0, 100, 0);
Marek Vasutd339f162015-12-16 15:40:06 +0100358
359 /*
360 * Check to see if any errors were encountered during calibration
361 * (check MPDGCTRL0[HW_DG_ERR]).
362 * Check both PHYs for x64 configuration, if x32, check only PHY0.
363 */
364 if (readl(&mmdc0->mpdgctrl0) & 0x00001000)
365 errors |= 1;
366
367 if ((bus_size == 0x2) && (readl(&mmdc1->mpdgctrl0) & 0x00001000))
368 errors |= 2;
369
Eric Nelsonb33f74e2016-10-30 16:33:47 -0700370 /* now disable mpdgctrl0[DG_CMP_CYC] */
371 clrbits_le32(&mmdc0->mpdgctrl0, 1 << 30);
372 if (sysinfo->dsize == 2)
373 clrbits_le32(&mmdc1->mpdgctrl0, 1 << 30);
374
Marek Vasutd339f162015-12-16 15:40:06 +0100375 /*
376 * DQS gating absolute offset should be modified from
377 * reflecting (HW_DG_LOWx + HW_DG_UPx)/2 to
378 * reflecting (HW_DG_UPx - 0x80)
379 */
380 modify_dg_result(&mmdc0->mpdghwst0, &mmdc0->mpdghwst1,
381 &mmdc0->mpdgctrl0);
382 modify_dg_result(&mmdc0->mpdghwst2, &mmdc0->mpdghwst3,
383 &mmdc0->mpdgctrl1);
384 if (bus_size == 0x2) {
385 modify_dg_result(&mmdc1->mpdghwst0, &mmdc1->mpdghwst1,
386 &mmdc1->mpdgctrl0);
387 modify_dg_result(&mmdc1->mpdghwst2, &mmdc1->mpdghwst3,
388 &mmdc1->mpdgctrl1);
389 }
390 debug("Ending Read DQS Gating calibration. Error mask: 0x%x\n", errors);
391
392 /*
393 * **********************
394 * Read Delay calibration
395 * **********************
396 */
397 debug("Starting Read Delay calibration.\n");
398
399 reset_read_data_fifos();
400
401 /*
402 * 4. Issue the Precharge-All command to the DDR device for both
403 * chip selects. If only using one chip select, then precharge
404 * only the desired chip select.
405 */
406 precharge_all(cs0_enable, cs1_enable);
407
408 /*
409 * 9. Read delay-line calibration
410 * Start the automatic read calibration process by asserting
411 * MPRDDLHWCTL[HW_RD_DL_EN].
412 */
413 writel(0x00000030, &mmdc0->mprddlhwctl);
414
415 /*
416 * 10. poll for completion
417 * MMDC indicates that the write data calibration had finished by
418 * setting MPRDDLHWCTL[HW_RD_DL_EN] = 0. Also, ensure that
419 * no error bits were set.
420 */
Marek Vasutb10d93e2016-03-02 14:49:51 +0100421 wait_for_bit("MMDC", &mmdc0->mprddlhwctl, 1 << 4, 0, 100, 0);
Marek Vasutd339f162015-12-16 15:40:06 +0100422
423 /* check both PHYs for x64 configuration, if x32, check only PHY0 */
424 if (readl(&mmdc0->mprddlhwctl) & 0x0000000f)
425 errors |= 4;
426
427 if ((bus_size == 0x2) && (readl(&mmdc1->mprddlhwctl) & 0x0000000f))
428 errors |= 8;
429
430 debug("Ending Read Delay calibration. Error mask: 0x%x\n", errors);
431
432 /*
433 * ***********************
434 * Write Delay Calibration
435 * ***********************
436 */
437 debug("Starting Write Delay calibration.\n");
438
439 reset_read_data_fifos();
440
441 /*
442 * 4. Issue the Precharge-All command to the DDR device for both
443 * chip selects. If only using one chip select, then precharge
444 * only the desired chip select.
445 */
446 precharge_all(cs0_enable, cs1_enable);
447
448 /*
449 * 8. Set the WR_DL_ABS# bits to their default values.
450 * Both PHYs for x64 configuration, if x32, do only PHY0.
451 */
452 writel(initdelay, &mmdc0->mpwrdlctl);
453 if (bus_size == 0x2)
454 writel(initdelay, &mmdc1->mpwrdlctl);
455
456 /*
457 * XXX This isn't in the manual. Force a measurement,
458 * for previous delay setup to effect.
459 */
460 force_delay_measurement(bus_size);
461
462 /*
463 * 9. 10. Start the automatic write calibration process
464 * by asserting MPWRDLHWCTL0[HW_WR_DL_EN].
465 */
466 writel(0x00000030, &mmdc0->mpwrdlhwctl);
467
468 /*
469 * Poll for completion.
470 * MMDC indicates that the write data calibration had finished
471 * by setting MPWRDLHWCTL[HW_WR_DL_EN] = 0.
472 * Also, ensure that no error bits were set.
473 */
Marek Vasutb10d93e2016-03-02 14:49:51 +0100474 wait_for_bit("MMDC", &mmdc0->mpwrdlhwctl, 1 << 4, 0, 100, 0);
Marek Vasutd339f162015-12-16 15:40:06 +0100475
476 /* Check both PHYs for x64 configuration, if x32, check only PHY0 */
477 if (readl(&mmdc0->mpwrdlhwctl) & 0x0000000f)
478 errors |= 16;
479
480 if ((bus_size == 0x2) && (readl(&mmdc1->mpwrdlhwctl) & 0x0000000f))
481 errors |= 32;
482
483 debug("Ending Write Delay calibration. Error mask: 0x%x\n", errors);
484
485 reset_read_data_fifos();
486
487 /* Enable DDR logic power down timer */
488 setbits_le32(&mmdc0->mdpdc, 0x00005500);
489
490 /* Enable Adopt power down timer */
491 clrbits_le32(&mmdc0->mapsr, 0x1);
492
493 /* Restore MDMISC value (RALAT, WALAT) to MMDCP1 */
494 writel(esdmisc_val, &mmdc0->mdmisc);
495
496 /* Clear DQS pull ups */
497 clrbits_le32(&mx6_ddr_iomux->dram_sdqs0, 0x7000);
498 clrbits_le32(&mx6_ddr_iomux->dram_sdqs1, 0x7000);
499 clrbits_le32(&mx6_ddr_iomux->dram_sdqs2, 0x7000);
500 clrbits_le32(&mx6_ddr_iomux->dram_sdqs3, 0x7000);
501 clrbits_le32(&mx6_ddr_iomux->dram_sdqs4, 0x7000);
502 clrbits_le32(&mx6_ddr_iomux->dram_sdqs5, 0x7000);
503 clrbits_le32(&mx6_ddr_iomux->dram_sdqs6, 0x7000);
504 clrbits_le32(&mx6_ddr_iomux->dram_sdqs7, 0x7000);
505
506 /* Re-enable SDE (chip selects) if they were set initially */
507 if (cs1_enable_initial)
508 /* Set SDE_1 */
509 setbits_le32(&mmdc0->mdctl, 1 << 30);
510
511 if (cs0_enable_initial)
512 /* Set SDE_0 */
513 setbits_le32(&mmdc0->mdctl, 1 << 31);
514
515 /* Re-enable to auto refresh */
516 writel(temp_ref, &mmdc0->mdref);
517
518 /* Clear the MDSCR (including the con_req bit) */
519 writel(0x0, &mmdc0->mdscr); /* CS0 */
520
521 /* Poll to make sure the con_ack bit is clear */
Marek Vasutb10d93e2016-03-02 14:49:51 +0100522 wait_for_bit("MMDC", &mmdc0->mdscr, 1 << 14, 0, 100, 0);
Marek Vasutd339f162015-12-16 15:40:06 +0100523
524 /*
525 * Print out the registers that were updated as a result
526 * of the calibration process.
527 */
528 debug("MMDC registers updated from calibration\n");
529 debug("Read DQS gating calibration:\n");
530 debug("\tMPDGCTRL0 PHY0 = 0x%08X\n", readl(&mmdc0->mpdgctrl0));
531 debug("\tMPDGCTRL1 PHY0 = 0x%08X\n", readl(&mmdc0->mpdgctrl1));
532 debug("\tMPDGCTRL0 PHY1 = 0x%08X\n", readl(&mmdc1->mpdgctrl0));
533 debug("\tMPDGCTRL1 PHY1 = 0x%08X\n", readl(&mmdc1->mpdgctrl1));
534 debug("Read calibration:\n");
535 debug("\tMPRDDLCTL PHY0 = 0x%08X\n", readl(&mmdc0->mprddlctl));
536 debug("\tMPRDDLCTL PHY1 = 0x%08X\n", readl(&mmdc1->mprddlctl));
537 debug("Write calibration:\n");
538 debug("\tMPWRDLCTL PHY0 = 0x%08X\n", readl(&mmdc0->mpwrdlctl));
539 debug("\tMPWRDLCTL PHY1 = 0x%08X\n", readl(&mmdc1->mpwrdlctl));
540
541 /*
542 * Registers below are for debugging purposes. These print out
543 * the upper and lower boundaries captured during
544 * read DQS gating calibration.
545 */
546 debug("Status registers bounds for read DQS gating:\n");
547 debug("\tMPDGHWST0 PHY0 = 0x%08x\n", readl(&mmdc0->mpdghwst0));
548 debug("\tMPDGHWST1 PHY0 = 0x%08x\n", readl(&mmdc0->mpdghwst1));
549 debug("\tMPDGHWST2 PHY0 = 0x%08x\n", readl(&mmdc0->mpdghwst2));
550 debug("\tMPDGHWST3 PHY0 = 0x%08x\n", readl(&mmdc0->mpdghwst3));
551 debug("\tMPDGHWST0 PHY1 = 0x%08x\n", readl(&mmdc1->mpdghwst0));
552 debug("\tMPDGHWST1 PHY1 = 0x%08x\n", readl(&mmdc1->mpdghwst1));
553 debug("\tMPDGHWST2 PHY1 = 0x%08x\n", readl(&mmdc1->mpdghwst2));
554 debug("\tMPDGHWST3 PHY1 = 0x%08x\n", readl(&mmdc1->mpdghwst3));
555
556 debug("Final do_dqs_calibration error mask: 0x%x\n", errors);
557
558 return errors;
559}
560#endif
561
Peng Fand9efd472014-12-30 17:24:01 +0800562#if defined(CONFIG_MX6SX)
563/* Configure MX6SX mmdc iomux */
564void mx6sx_dram_iocfg(unsigned width,
565 const struct mx6sx_iomux_ddr_regs *ddr,
566 const struct mx6sx_iomux_grp_regs *grp)
567{
568 struct mx6sx_iomux_ddr_regs *mx6_ddr_iomux;
569 struct mx6sx_iomux_grp_regs *mx6_grp_iomux;
570
571 mx6_ddr_iomux = (struct mx6sx_iomux_ddr_regs *)MX6SX_IOM_DDR_BASE;
572 mx6_grp_iomux = (struct mx6sx_iomux_grp_regs *)MX6SX_IOM_GRP_BASE;
573
574 /* DDR IO TYPE */
575 writel(grp->grp_ddr_type, &mx6_grp_iomux->grp_ddr_type);
576 writel(grp->grp_ddrpke, &mx6_grp_iomux->grp_ddrpke);
577
578 /* CLOCK */
579 writel(ddr->dram_sdclk_0, &mx6_ddr_iomux->dram_sdclk_0);
580
581 /* ADDRESS */
582 writel(ddr->dram_cas, &mx6_ddr_iomux->dram_cas);
583 writel(ddr->dram_ras, &mx6_ddr_iomux->dram_ras);
584 writel(grp->grp_addds, &mx6_grp_iomux->grp_addds);
585
586 /* Control */
587 writel(ddr->dram_reset, &mx6_ddr_iomux->dram_reset);
588 writel(ddr->dram_sdba2, &mx6_ddr_iomux->dram_sdba2);
589 writel(ddr->dram_sdcke0, &mx6_ddr_iomux->dram_sdcke0);
590 writel(ddr->dram_sdcke1, &mx6_ddr_iomux->dram_sdcke1);
591 writel(ddr->dram_odt0, &mx6_ddr_iomux->dram_odt0);
592 writel(ddr->dram_odt1, &mx6_ddr_iomux->dram_odt1);
593 writel(grp->grp_ctlds, &mx6_grp_iomux->grp_ctlds);
594
595 /* Data Strobes */
596 writel(grp->grp_ddrmode_ctl, &mx6_grp_iomux->grp_ddrmode_ctl);
597 writel(ddr->dram_sdqs0, &mx6_ddr_iomux->dram_sdqs0);
598 writel(ddr->dram_sdqs1, &mx6_ddr_iomux->dram_sdqs1);
599 if (width >= 32) {
600 writel(ddr->dram_sdqs2, &mx6_ddr_iomux->dram_sdqs2);
601 writel(ddr->dram_sdqs3, &mx6_ddr_iomux->dram_sdqs3);
602 }
603
604 /* Data */
605 writel(grp->grp_ddrmode, &mx6_grp_iomux->grp_ddrmode);
606 writel(grp->grp_b0ds, &mx6_grp_iomux->grp_b0ds);
607 writel(grp->grp_b1ds, &mx6_grp_iomux->grp_b1ds);
608 if (width >= 32) {
609 writel(grp->grp_b2ds, &mx6_grp_iomux->grp_b2ds);
610 writel(grp->grp_b3ds, &mx6_grp_iomux->grp_b3ds);
611 }
612 writel(ddr->dram_dqm0, &mx6_ddr_iomux->dram_dqm0);
613 writel(ddr->dram_dqm1, &mx6_ddr_iomux->dram_dqm1);
614 if (width >= 32) {
615 writel(ddr->dram_dqm2, &mx6_ddr_iomux->dram_dqm2);
616 writel(ddr->dram_dqm3, &mx6_ddr_iomux->dram_dqm3);
617 }
618}
619#endif
620
Peng Fana462c342015-07-20 19:28:33 +0800621#ifdef CONFIG_MX6UL
622void mx6ul_dram_iocfg(unsigned width,
623 const struct mx6ul_iomux_ddr_regs *ddr,
624 const struct mx6ul_iomux_grp_regs *grp)
625{
626 struct mx6ul_iomux_ddr_regs *mx6_ddr_iomux;
627 struct mx6ul_iomux_grp_regs *mx6_grp_iomux;
628
629 mx6_ddr_iomux = (struct mx6ul_iomux_ddr_regs *)MX6UL_IOM_DDR_BASE;
630 mx6_grp_iomux = (struct mx6ul_iomux_grp_regs *)MX6UL_IOM_GRP_BASE;
631
632 /* DDR IO TYPE */
633 writel(grp->grp_ddr_type, &mx6_grp_iomux->grp_ddr_type);
634 writel(grp->grp_ddrpke, &mx6_grp_iomux->grp_ddrpke);
635
636 /* CLOCK */
637 writel(ddr->dram_sdclk_0, &mx6_ddr_iomux->dram_sdclk_0);
638
639 /* ADDRESS */
640 writel(ddr->dram_cas, &mx6_ddr_iomux->dram_cas);
641 writel(ddr->dram_ras, &mx6_ddr_iomux->dram_ras);
642 writel(grp->grp_addds, &mx6_grp_iomux->grp_addds);
643
644 /* Control */
645 writel(ddr->dram_reset, &mx6_ddr_iomux->dram_reset);
646 writel(ddr->dram_sdba2, &mx6_ddr_iomux->dram_sdba2);
647 writel(ddr->dram_odt0, &mx6_ddr_iomux->dram_odt0);
648 writel(ddr->dram_odt1, &mx6_ddr_iomux->dram_odt1);
649 writel(grp->grp_ctlds, &mx6_grp_iomux->grp_ctlds);
650
651 /* Data Strobes */
652 writel(grp->grp_ddrmode_ctl, &mx6_grp_iomux->grp_ddrmode_ctl);
653 writel(ddr->dram_sdqs0, &mx6_ddr_iomux->dram_sdqs0);
654 writel(ddr->dram_sdqs1, &mx6_ddr_iomux->dram_sdqs1);
655
656 /* Data */
657 writel(grp->grp_ddrmode, &mx6_grp_iomux->grp_ddrmode);
658 writel(grp->grp_b0ds, &mx6_grp_iomux->grp_b0ds);
659 writel(grp->grp_b1ds, &mx6_grp_iomux->grp_b1ds);
660 writel(ddr->dram_dqm0, &mx6_ddr_iomux->dram_dqm0);
661 writel(ddr->dram_dqm1, &mx6_ddr_iomux->dram_dqm1);
662}
663#endif
664
Peng Fan1b811e22015-08-17 16:11:00 +0800665#if defined(CONFIG_MX6SL)
666void mx6sl_dram_iocfg(unsigned width,
667 const struct mx6sl_iomux_ddr_regs *ddr,
668 const struct mx6sl_iomux_grp_regs *grp)
669{
670 struct mx6sl_iomux_ddr_regs *mx6_ddr_iomux;
671 struct mx6sl_iomux_grp_regs *mx6_grp_iomux;
672
673 mx6_ddr_iomux = (struct mx6sl_iomux_ddr_regs *)MX6SL_IOM_DDR_BASE;
674 mx6_grp_iomux = (struct mx6sl_iomux_grp_regs *)MX6SL_IOM_GRP_BASE;
675
676 /* DDR IO TYPE */
677 mx6_grp_iomux->grp_ddr_type = grp->grp_ddr_type;
678 mx6_grp_iomux->grp_ddrpke = grp->grp_ddrpke;
679
680 /* CLOCK */
681 mx6_ddr_iomux->dram_sdclk_0 = ddr->dram_sdclk_0;
682
683 /* ADDRESS */
684 mx6_ddr_iomux->dram_cas = ddr->dram_cas;
685 mx6_ddr_iomux->dram_ras = ddr->dram_ras;
686 mx6_grp_iomux->grp_addds = grp->grp_addds;
687
688 /* Control */
689 mx6_ddr_iomux->dram_reset = ddr->dram_reset;
690 mx6_ddr_iomux->dram_sdba2 = ddr->dram_sdba2;
691 mx6_grp_iomux->grp_ctlds = grp->grp_ctlds;
692
693 /* Data Strobes */
694 mx6_grp_iomux->grp_ddrmode_ctl = grp->grp_ddrmode_ctl;
695 mx6_ddr_iomux->dram_sdqs0 = ddr->dram_sdqs0;
696 mx6_ddr_iomux->dram_sdqs1 = ddr->dram_sdqs1;
697 if (width >= 32) {
698 mx6_ddr_iomux->dram_sdqs2 = ddr->dram_sdqs2;
699 mx6_ddr_iomux->dram_sdqs3 = ddr->dram_sdqs3;
700 }
701
702 /* Data */
703 mx6_grp_iomux->grp_ddrmode = grp->grp_ddrmode;
704 mx6_grp_iomux->grp_b0ds = grp->grp_b0ds;
705 mx6_grp_iomux->grp_b1ds = grp->grp_b1ds;
706 if (width >= 32) {
707 mx6_grp_iomux->grp_b2ds = grp->grp_b2ds;
708 mx6_grp_iomux->grp_b3ds = grp->grp_b3ds;
709 }
710
711 mx6_ddr_iomux->dram_dqm0 = ddr->dram_dqm0;
712 mx6_ddr_iomux->dram_dqm1 = ddr->dram_dqm1;
713 if (width >= 32) {
714 mx6_ddr_iomux->dram_dqm2 = ddr->dram_dqm2;
715 mx6_ddr_iomux->dram_dqm3 = ddr->dram_dqm3;
716 }
717}
718#endif
719
Tim Harveyfe0f7f72014-06-02 16:13:23 -0700720#if defined(CONFIG_MX6QDL) || defined(CONFIG_MX6Q) || defined(CONFIG_MX6D)
721/* Configure MX6DQ mmdc iomux */
722void mx6dq_dram_iocfg(unsigned width,
723 const struct mx6dq_iomux_ddr_regs *ddr,
724 const struct mx6dq_iomux_grp_regs *grp)
725{
726 volatile struct mx6dq_iomux_ddr_regs *mx6_ddr_iomux;
727 volatile struct mx6dq_iomux_grp_regs *mx6_grp_iomux;
728
729 mx6_ddr_iomux = (struct mx6dq_iomux_ddr_regs *)MX6DQ_IOM_DDR_BASE;
730 mx6_grp_iomux = (struct mx6dq_iomux_grp_regs *)MX6DQ_IOM_GRP_BASE;
731
732 /* DDR IO Type */
733 mx6_grp_iomux->grp_ddr_type = grp->grp_ddr_type;
734 mx6_grp_iomux->grp_ddrpke = grp->grp_ddrpke;
735
736 /* Clock */
737 mx6_ddr_iomux->dram_sdclk_0 = ddr->dram_sdclk_0;
738 mx6_ddr_iomux->dram_sdclk_1 = ddr->dram_sdclk_1;
739
740 /* Address */
741 mx6_ddr_iomux->dram_cas = ddr->dram_cas;
742 mx6_ddr_iomux->dram_ras = ddr->dram_ras;
743 mx6_grp_iomux->grp_addds = grp->grp_addds;
744
745 /* Control */
746 mx6_ddr_iomux->dram_reset = ddr->dram_reset;
747 mx6_ddr_iomux->dram_sdcke0 = ddr->dram_sdcke0;
748 mx6_ddr_iomux->dram_sdcke1 = ddr->dram_sdcke1;
749 mx6_ddr_iomux->dram_sdba2 = ddr->dram_sdba2;
750 mx6_ddr_iomux->dram_sdodt0 = ddr->dram_sdodt0;
751 mx6_ddr_iomux->dram_sdodt1 = ddr->dram_sdodt1;
752 mx6_grp_iomux->grp_ctlds = grp->grp_ctlds;
753
754 /* Data Strobes */
755 mx6_grp_iomux->grp_ddrmode_ctl = grp->grp_ddrmode_ctl;
756 mx6_ddr_iomux->dram_sdqs0 = ddr->dram_sdqs0;
757 mx6_ddr_iomux->dram_sdqs1 = ddr->dram_sdqs1;
758 if (width >= 32) {
759 mx6_ddr_iomux->dram_sdqs2 = ddr->dram_sdqs2;
760 mx6_ddr_iomux->dram_sdqs3 = ddr->dram_sdqs3;
761 }
762 if (width >= 64) {
763 mx6_ddr_iomux->dram_sdqs4 = ddr->dram_sdqs4;
764 mx6_ddr_iomux->dram_sdqs5 = ddr->dram_sdqs5;
765 mx6_ddr_iomux->dram_sdqs6 = ddr->dram_sdqs6;
766 mx6_ddr_iomux->dram_sdqs7 = ddr->dram_sdqs7;
767 }
768
769 /* Data */
770 mx6_grp_iomux->grp_ddrmode = grp->grp_ddrmode;
771 mx6_grp_iomux->grp_b0ds = grp->grp_b0ds;
772 mx6_grp_iomux->grp_b1ds = grp->grp_b1ds;
773 if (width >= 32) {
774 mx6_grp_iomux->grp_b2ds = grp->grp_b2ds;
775 mx6_grp_iomux->grp_b3ds = grp->grp_b3ds;
776 }
777 if (width >= 64) {
778 mx6_grp_iomux->grp_b4ds = grp->grp_b4ds;
779 mx6_grp_iomux->grp_b5ds = grp->grp_b5ds;
780 mx6_grp_iomux->grp_b6ds = grp->grp_b6ds;
781 mx6_grp_iomux->grp_b7ds = grp->grp_b7ds;
782 }
783 mx6_ddr_iomux->dram_dqm0 = ddr->dram_dqm0;
784 mx6_ddr_iomux->dram_dqm1 = ddr->dram_dqm1;
785 if (width >= 32) {
786 mx6_ddr_iomux->dram_dqm2 = ddr->dram_dqm2;
787 mx6_ddr_iomux->dram_dqm3 = ddr->dram_dqm3;
788 }
789 if (width >= 64) {
790 mx6_ddr_iomux->dram_dqm4 = ddr->dram_dqm4;
791 mx6_ddr_iomux->dram_dqm5 = ddr->dram_dqm5;
792 mx6_ddr_iomux->dram_dqm6 = ddr->dram_dqm6;
793 mx6_ddr_iomux->dram_dqm7 = ddr->dram_dqm7;
794 }
795}
796#endif
797
798#if defined(CONFIG_MX6QDL) || defined(CONFIG_MX6DL) || defined(CONFIG_MX6S)
799/* Configure MX6SDL mmdc iomux */
800void mx6sdl_dram_iocfg(unsigned width,
801 const struct mx6sdl_iomux_ddr_regs *ddr,
802 const struct mx6sdl_iomux_grp_regs *grp)
803{
804 volatile struct mx6sdl_iomux_ddr_regs *mx6_ddr_iomux;
805 volatile struct mx6sdl_iomux_grp_regs *mx6_grp_iomux;
806
807 mx6_ddr_iomux = (struct mx6sdl_iomux_ddr_regs *)MX6SDL_IOM_DDR_BASE;
808 mx6_grp_iomux = (struct mx6sdl_iomux_grp_regs *)MX6SDL_IOM_GRP_BASE;
809
810 /* DDR IO Type */
811 mx6_grp_iomux->grp_ddr_type = grp->grp_ddr_type;
812 mx6_grp_iomux->grp_ddrpke = grp->grp_ddrpke;
813
814 /* Clock */
815 mx6_ddr_iomux->dram_sdclk_0 = ddr->dram_sdclk_0;
816 mx6_ddr_iomux->dram_sdclk_1 = ddr->dram_sdclk_1;
817
818 /* Address */
819 mx6_ddr_iomux->dram_cas = ddr->dram_cas;
820 mx6_ddr_iomux->dram_ras = ddr->dram_ras;
821 mx6_grp_iomux->grp_addds = grp->grp_addds;
822
823 /* Control */
824 mx6_ddr_iomux->dram_reset = ddr->dram_reset;
825 mx6_ddr_iomux->dram_sdcke0 = ddr->dram_sdcke0;
826 mx6_ddr_iomux->dram_sdcke1 = ddr->dram_sdcke1;
827 mx6_ddr_iomux->dram_sdba2 = ddr->dram_sdba2;
828 mx6_ddr_iomux->dram_sdodt0 = ddr->dram_sdodt0;
829 mx6_ddr_iomux->dram_sdodt1 = ddr->dram_sdodt1;
830 mx6_grp_iomux->grp_ctlds = grp->grp_ctlds;
831
832 /* Data Strobes */
833 mx6_grp_iomux->grp_ddrmode_ctl = grp->grp_ddrmode_ctl;
834 mx6_ddr_iomux->dram_sdqs0 = ddr->dram_sdqs0;
835 mx6_ddr_iomux->dram_sdqs1 = ddr->dram_sdqs1;
836 if (width >= 32) {
837 mx6_ddr_iomux->dram_sdqs2 = ddr->dram_sdqs2;
838 mx6_ddr_iomux->dram_sdqs3 = ddr->dram_sdqs3;
839 }
840 if (width >= 64) {
841 mx6_ddr_iomux->dram_sdqs4 = ddr->dram_sdqs4;
842 mx6_ddr_iomux->dram_sdqs5 = ddr->dram_sdqs5;
843 mx6_ddr_iomux->dram_sdqs6 = ddr->dram_sdqs6;
844 mx6_ddr_iomux->dram_sdqs7 = ddr->dram_sdqs7;
845 }
846
847 /* Data */
848 mx6_grp_iomux->grp_ddrmode = grp->grp_ddrmode;
849 mx6_grp_iomux->grp_b0ds = grp->grp_b0ds;
850 mx6_grp_iomux->grp_b1ds = grp->grp_b1ds;
851 if (width >= 32) {
852 mx6_grp_iomux->grp_b2ds = grp->grp_b2ds;
853 mx6_grp_iomux->grp_b3ds = grp->grp_b3ds;
854 }
855 if (width >= 64) {
856 mx6_grp_iomux->grp_b4ds = grp->grp_b4ds;
857 mx6_grp_iomux->grp_b5ds = grp->grp_b5ds;
858 mx6_grp_iomux->grp_b6ds = grp->grp_b6ds;
859 mx6_grp_iomux->grp_b7ds = grp->grp_b7ds;
860 }
861 mx6_ddr_iomux->dram_dqm0 = ddr->dram_dqm0;
862 mx6_ddr_iomux->dram_dqm1 = ddr->dram_dqm1;
863 if (width >= 32) {
864 mx6_ddr_iomux->dram_dqm2 = ddr->dram_dqm2;
865 mx6_ddr_iomux->dram_dqm3 = ddr->dram_dqm3;
866 }
867 if (width >= 64) {
868 mx6_ddr_iomux->dram_dqm4 = ddr->dram_dqm4;
869 mx6_ddr_iomux->dram_dqm5 = ddr->dram_dqm5;
870 mx6_ddr_iomux->dram_dqm6 = ddr->dram_dqm6;
871 mx6_ddr_iomux->dram_dqm7 = ddr->dram_dqm7;
872 }
873}
874#endif
875
876/*
877 * Configure mx6 mmdc registers based on:
878 * - board-specific memory configuration
879 * - board-specific calibration data
Peng Faneb796cb2015-08-17 16:11:04 +0800880 * - ddr3/lpddr2 chip details
Tim Harveyfe0f7f72014-06-02 16:13:23 -0700881 *
882 * The various calculations here are derived from the Freescale
Peng Faneb796cb2015-08-17 16:11:04 +0800883 * 1. i.Mx6DQSDL DDR3 Script Aid spreadsheet (DOC-94917) designed to generate
884 * MMDC configuration registers based on memory system and memory chip
885 * parameters.
886 *
887 * 2. i.Mx6SL LPDDR2 Script Aid spreadsheet V0.04 designed to generate MMDC
888 * configuration registers based on memory system and memory chip
889 * parameters.
Tim Harveyfe0f7f72014-06-02 16:13:23 -0700890 *
891 * The defaults here are those which were specified in the spreadsheet.
892 * For details on each register, refer to the IMX6DQRM and/or IMX6SDLRM
Peng Faneb796cb2015-08-17 16:11:04 +0800893 * and/or IMX6SLRM section titled MMDC initialization.
Tim Harveyfe0f7f72014-06-02 16:13:23 -0700894 */
895#define MR(val, ba, cmd, cs1) \
896 ((val << 16) | (1 << 15) | (cmd << 4) | (cs1 << 3) | ba)
Peng Fana462c342015-07-20 19:28:33 +0800897#define MMDC1(entry, value) do { \
Peng Fanb949fd22016-05-23 18:35:54 +0800898 if (!is_mx6sx() && !is_mx6ul() && !is_mx6sl()) \
Peng Fana462c342015-07-20 19:28:33 +0800899 mmdc1->entry = value; \
900 } while (0)
901
Peng Faneb796cb2015-08-17 16:11:04 +0800902/*
903 * According JESD209-2B-LPDDR2: Table 103
904 * WL: write latency
905 */
906static int lpddr2_wl(uint32_t mem_speed)
907{
908 switch (mem_speed) {
909 case 1066:
910 case 933:
911 return 4;
912 case 800:
913 return 3;
914 case 677:
915 case 533:
916 return 2;
917 case 400:
918 case 333:
919 return 1;
920 default:
921 puts("invalid memory speed\n");
922 hang();
923 }
924
925 return 0;
926}
927
928/*
929 * According JESD209-2B-LPDDR2: Table 103
930 * RL: read latency
931 */
932static int lpddr2_rl(uint32_t mem_speed)
933{
934 switch (mem_speed) {
935 case 1066:
936 return 8;
937 case 933:
938 return 7;
939 case 800:
940 return 6;
941 case 677:
942 return 5;
943 case 533:
944 return 4;
945 case 400:
946 case 333:
947 return 3;
948 default:
949 puts("invalid memory speed\n");
950 hang();
951 }
952
953 return 0;
954}
955
956void mx6_lpddr2_cfg(const struct mx6_ddr_sysinfo *sysinfo,
957 const struct mx6_mmdc_calibration *calib,
958 const struct mx6_lpddr2_cfg *lpddr2_cfg)
959{
960 volatile struct mmdc_p_regs *mmdc0;
961 u32 val;
962 u8 tcke, tcksrx, tcksre, trrd;
963 u8 twl, txp, tfaw, tcl;
964 u16 tras, twr, tmrd, trtp, twtr, trfc, txsr;
965 u16 trcd_lp, trppb_lp, trpab_lp, trc_lp;
966 u16 cs0_end;
967 u8 coladdr;
968 int clkper; /* clock period in picoseconds */
969 int clock; /* clock freq in mHz */
970 int cs;
971
972 /* only support 16/32 bits */
973 if (sysinfo->dsize > 1)
974 hang();
975
976 mmdc0 = (struct mmdc_p_regs *)MMDC_P0_BASE_ADDR;
977
978 clock = mxc_get_clock(MXC_DDR_CLK) / 1000000U;
979 clkper = (1000 * 1000) / clock; /* pico seconds */
980
981 twl = lpddr2_wl(lpddr2_cfg->mem_speed) - 1;
982
983 /* LPDDR2-S2 and LPDDR2-S4 have the same tRFC value. */
984 switch (lpddr2_cfg->density) {
985 case 1:
986 case 2:
987 case 4:
988 trfc = DIV_ROUND_UP(130000, clkper) - 1;
989 txsr = DIV_ROUND_UP(140000, clkper) - 1;
990 break;
991 case 8:
992 trfc = DIV_ROUND_UP(210000, clkper) - 1;
993 txsr = DIV_ROUND_UP(220000, clkper) - 1;
994 break;
995 default:
996 /*
997 * 64Mb, 128Mb, 256Mb, 512Mb are not supported currently.
998 */
999 hang();
1000 break;
1001 }
1002 /*
1003 * txpdll, txpr, taonpd and taofpd are not relevant in LPDDR2 mode,
1004 * set them to 0. */
1005 txp = DIV_ROUND_UP(7500, clkper) - 1;
1006 tcke = 3;
1007 if (lpddr2_cfg->mem_speed == 333)
1008 tfaw = DIV_ROUND_UP(60000, clkper) - 1;
1009 else
1010 tfaw = DIV_ROUND_UP(50000, clkper) - 1;
1011 trrd = DIV_ROUND_UP(10000, clkper) - 1;
1012
1013 /* tckesr for LPDDR2 */
1014 tcksre = DIV_ROUND_UP(15000, clkper);
1015 tcksrx = tcksre;
1016 twr = DIV_ROUND_UP(15000, clkper) - 1;
1017 /*
1018 * tMRR: 2, tMRW: 5
1019 * tMRD should be set to max(tMRR, tMRW)
1020 */
1021 tmrd = 5;
1022 tras = DIV_ROUND_UP(lpddr2_cfg->trasmin, clkper / 10) - 1;
1023 /* LPDDR2 mode use tRCD_LP filed in MDCFG3. */
1024 trcd_lp = DIV_ROUND_UP(lpddr2_cfg->trcd_lp, clkper / 10) - 1;
1025 trc_lp = DIV_ROUND_UP(lpddr2_cfg->trasmin + lpddr2_cfg->trppb_lp,
1026 clkper / 10) - 1;
1027 trppb_lp = DIV_ROUND_UP(lpddr2_cfg->trppb_lp, clkper / 10) - 1;
1028 trpab_lp = DIV_ROUND_UP(lpddr2_cfg->trpab_lp, clkper / 10) - 1;
1029 /* To LPDDR2, CL in MDCFG0 refers to RL */
1030 tcl = lpddr2_rl(lpddr2_cfg->mem_speed) - 3;
1031 twtr = DIV_ROUND_UP(7500, clkper) - 1;
1032 trtp = DIV_ROUND_UP(7500, clkper) - 1;
1033
1034 cs0_end = 4 * sysinfo->cs_density - 1;
1035
1036 debug("density:%d Gb (%d Gb per chip)\n",
1037 sysinfo->cs_density, lpddr2_cfg->density);
1038 debug("clock: %dMHz (%d ps)\n", clock, clkper);
1039 debug("memspd:%d\n", lpddr2_cfg->mem_speed);
1040 debug("trcd_lp=%d\n", trcd_lp);
1041 debug("trppb_lp=%d\n", trppb_lp);
1042 debug("trpab_lp=%d\n", trpab_lp);
1043 debug("trc_lp=%d\n", trc_lp);
1044 debug("tcke=%d\n", tcke);
1045 debug("tcksrx=%d\n", tcksrx);
1046 debug("tcksre=%d\n", tcksre);
1047 debug("trfc=%d\n", trfc);
1048 debug("txsr=%d\n", txsr);
1049 debug("txp=%d\n", txp);
1050 debug("tfaw=%d\n", tfaw);
1051 debug("tcl=%d\n", tcl);
1052 debug("tras=%d\n", tras);
1053 debug("twr=%d\n", twr);
1054 debug("tmrd=%d\n", tmrd);
1055 debug("twl=%d\n", twl);
1056 debug("trtp=%d\n", trtp);
1057 debug("twtr=%d\n", twtr);
1058 debug("trrd=%d\n", trrd);
1059 debug("cs0_end=%d\n", cs0_end);
1060 debug("ncs=%d\n", sysinfo->ncs);
1061
1062 /*
1063 * board-specific configuration:
1064 * These values are determined empirically and vary per board layout
1065 */
1066 mmdc0->mpwldectrl0 = calib->p0_mpwldectrl0;
1067 mmdc0->mpwldectrl1 = calib->p0_mpwldectrl1;
1068 mmdc0->mpdgctrl0 = calib->p0_mpdgctrl0;
1069 mmdc0->mpdgctrl1 = calib->p0_mpdgctrl1;
1070 mmdc0->mprddlctl = calib->p0_mprddlctl;
1071 mmdc0->mpwrdlctl = calib->p0_mpwrdlctl;
1072 mmdc0->mpzqlp2ctl = calib->mpzqlp2ctl;
1073
1074 /* Read data DQ Byte0-3 delay */
1075 mmdc0->mprddqby0dl = 0x33333333;
1076 mmdc0->mprddqby1dl = 0x33333333;
1077 if (sysinfo->dsize > 0) {
1078 mmdc0->mprddqby2dl = 0x33333333;
1079 mmdc0->mprddqby3dl = 0x33333333;
1080 }
1081
1082 /* Write data DQ Byte0-3 delay */
1083 mmdc0->mpwrdqby0dl = 0xf3333333;
1084 mmdc0->mpwrdqby1dl = 0xf3333333;
1085 if (sysinfo->dsize > 0) {
1086 mmdc0->mpwrdqby2dl = 0xf3333333;
1087 mmdc0->mpwrdqby3dl = 0xf3333333;
1088 }
1089
1090 /*
1091 * In LPDDR2 mode this register should be cleared,
1092 * so no termination will be activated.
1093 */
1094 mmdc0->mpodtctrl = 0;
1095
1096 /* complete calibration */
1097 val = (1 << 11); /* Force measurement on delay-lines */
1098 mmdc0->mpmur0 = val;
1099
1100 /* Step 1: configuration request */
1101 mmdc0->mdscr = (u32)(1 << 15); /* config request */
1102
1103 /* Step 2: Timing configuration */
1104 mmdc0->mdcfg0 = (trfc << 24) | (txsr << 16) | (txp << 13) |
1105 (tfaw << 4) | tcl;
1106 mmdc0->mdcfg1 = (tras << 16) | (twr << 9) | (tmrd << 5) | twl;
1107 mmdc0->mdcfg2 = (trtp << 6) | (twtr << 3) | trrd;
1108 mmdc0->mdcfg3lp = (trc_lp << 16) | (trcd_lp << 8) |
1109 (trppb_lp << 4) | trpab_lp;
1110 mmdc0->mdotc = 0;
1111
1112 mmdc0->mdasp = cs0_end; /* CS addressing */
1113
1114 /* Step 3: Configure DDR type */
1115 mmdc0->mdmisc = (sysinfo->cs1_mirror << 19) | (sysinfo->walat << 16) |
1116 (sysinfo->bi_on << 12) | (sysinfo->mif3_mode << 9) |
1117 (sysinfo->ralat << 6) | (1 << 3);
1118
1119 /* Step 4: Configure delay while leaving reset */
1120 mmdc0->mdor = (sysinfo->sde_to_rst << 8) |
1121 (sysinfo->rst_to_cke << 0);
1122
1123 /* Step 5: Configure DDR physical parameters (density and burst len) */
1124 coladdr = lpddr2_cfg->coladdr;
1125 if (lpddr2_cfg->coladdr == 8) /* 8-bit COL is 0x3 */
1126 coladdr += 4;
1127 else if (lpddr2_cfg->coladdr == 12) /* 12-bit COL is 0x4 */
1128 coladdr += 1;
1129 mmdc0->mdctl = (lpddr2_cfg->rowaddr - 11) << 24 | /* ROW */
1130 (coladdr - 9) << 20 | /* COL */
1131 (0 << 19) | /* Burst Length = 4 for LPDDR2 */
1132 (sysinfo->dsize << 16); /* DDR data bus size */
1133
1134 /* Step 6: Perform ZQ calibration */
1135 val = 0xa1390003; /* one-time HW ZQ calib */
1136 mmdc0->mpzqhwctrl = val;
1137
1138 /* Step 7: Enable MMDC with desired chip select */
1139 mmdc0->mdctl |= (1 << 31) | /* SDE_0 for CS0 */
1140 ((sysinfo->ncs == 2) ? 1 : 0) << 30; /* SDE_1 for CS1 */
1141
1142 /* Step 8: Write Mode Registers to Init LPDDR2 devices */
1143 for (cs = 0; cs < sysinfo->ncs; cs++) {
1144 /* MR63: reset */
1145 mmdc0->mdscr = MR(63, 0, 3, cs);
1146 /* MR10: calibration,
1147 * 0xff is calibration command after intilization.
1148 */
1149 val = 0xA | (0xff << 8);
1150 mmdc0->mdscr = MR(val, 0, 3, cs);
1151 /* MR1 */
1152 val = 0x1 | (0x82 << 8);
1153 mmdc0->mdscr = MR(val, 0, 3, cs);
1154 /* MR2 */
1155 val = 0x2 | (0x04 << 8);
1156 mmdc0->mdscr = MR(val, 0, 3, cs);
1157 /* MR3 */
1158 val = 0x3 | (0x02 << 8);
1159 mmdc0->mdscr = MR(val, 0, 3, cs);
1160 }
1161
1162 /* Step 10: Power down control and self-refresh */
1163 mmdc0->mdpdc = (tcke & 0x7) << 16 |
1164 5 << 12 | /* PWDT_1: 256 cycles */
1165 5 << 8 | /* PWDT_0: 256 cycles */
1166 1 << 6 | /* BOTH_CS_PD */
1167 (tcksrx & 0x7) << 3 |
1168 (tcksre & 0x7);
1169 mmdc0->mapsr = 0x00001006; /* ADOPT power down enabled */
1170
1171 /* Step 11: Configure ZQ calibration: one-time and periodic 1ms */
1172 val = 0xa1310003;
1173 mmdc0->mpzqhwctrl = val;
1174
1175 /* Step 12: Configure and activate periodic refresh */
Fabio Estevamedf00932016-08-29 20:37:15 -03001176 mmdc0->mdref = (sysinfo->refsel << 14) | (sysinfo->refr << 11);
Peng Faneb796cb2015-08-17 16:11:04 +08001177
1178 /* Step 13: Deassert config request - init complete */
1179 mmdc0->mdscr = 0x00000000;
1180
1181 /* wait for auto-ZQ calibration to complete */
1182 mdelay(1);
1183}
1184
Peng Fanf2ff8342015-08-17 16:11:03 +08001185void mx6_ddr3_cfg(const struct mx6_ddr_sysinfo *sysinfo,
Nikita Kiryanov33689182014-09-07 18:58:11 +03001186 const struct mx6_mmdc_calibration *calib,
1187 const struct mx6_ddr3_cfg *ddr3_cfg)
Tim Harveyfe0f7f72014-06-02 16:13:23 -07001188{
1189 volatile struct mmdc_p_regs *mmdc0;
1190 volatile struct mmdc_p_regs *mmdc1;
Nikita Kiryanov33689182014-09-07 18:58:11 +03001191 u32 val;
Tim Harveyfe0f7f72014-06-02 16:13:23 -07001192 u8 tcke, tcksrx, tcksre, txpdll, taofpd, taonpd, trrd;
1193 u8 todtlon, taxpd, tanpd, tcwl, txp, tfaw, tcl;
1194 u8 todt_idle_off = 0x4; /* from DDR3 Script Aid spreadsheet */
1195 u16 trcd, trc, tras, twr, tmrd, trtp, trp, twtr, trfc, txs, txpr;
Nikita Kiryanov33689182014-09-07 18:58:11 +03001196 u16 cs0_end;
Tim Harveyfe0f7f72014-06-02 16:13:23 -07001197 u16 tdllk = 0x1ff; /* DLL locking time: 512 cycles (JEDEC DDR3) */
Marek Vasutb299ab72014-08-04 01:47:10 +02001198 u8 coladdr;
Tim Harveyfe0f7f72014-06-02 16:13:23 -07001199 int clkper; /* clock period in picoseconds */
Nikolay Dimitrov8a2bd212015-04-22 18:37:31 +03001200 int clock; /* clock freq in MHz */
Tim Harveyfe0f7f72014-06-02 16:13:23 -07001201 int cs;
Nikolay Dimitrov8a2bd212015-04-22 18:37:31 +03001202 u16 mem_speed = ddr3_cfg->mem_speed;
Tim Harveyfe0f7f72014-06-02 16:13:23 -07001203
1204 mmdc0 = (struct mmdc_p_regs *)MMDC_P0_BASE_ADDR;
Peng Fanb949fd22016-05-23 18:35:54 +08001205 if (!is_mx6sx() && !is_mx6ul() && !is_mx6sl())
Peng Fana462c342015-07-20 19:28:33 +08001206 mmdc1 = (struct mmdc_p_regs *)MMDC_P1_BASE_ADDR;
Tim Harveyfe0f7f72014-06-02 16:13:23 -07001207
Nikolay Dimitrov8a2bd212015-04-22 18:37:31 +03001208 /* Limit mem_speed for MX6D/MX6Q */
Peng Fane4d79dc2016-05-23 18:35:57 +08001209 if (is_mx6dq() || is_mx6dqp()) {
Nikolay Dimitrov8a2bd212015-04-22 18:37:31 +03001210 if (mem_speed > 1066)
1211 mem_speed = 1066; /* 1066 MT/s */
1212
Tim Harveyfe0f7f72014-06-02 16:13:23 -07001213 tcwl = 4;
1214 }
Nikolay Dimitrov8a2bd212015-04-22 18:37:31 +03001215 /* Limit mem_speed for MX6S/MX6DL */
Tim Harveyfe0f7f72014-06-02 16:13:23 -07001216 else {
Nikolay Dimitrov8a2bd212015-04-22 18:37:31 +03001217 if (mem_speed > 800)
1218 mem_speed = 800; /* 800 MT/s */
1219
Tim Harveyfe0f7f72014-06-02 16:13:23 -07001220 tcwl = 3;
1221 }
Nikolay Dimitrov8a2bd212015-04-22 18:37:31 +03001222
1223 clock = mem_speed / 2;
1224 /*
1225 * Data rate of 1066 MT/s requires 533 MHz DDR3 clock, but MX6D/Q supports
1226 * up to 528 MHz, so reduce the clock to fit chip specs
1227 */
Peng Fane4d79dc2016-05-23 18:35:57 +08001228 if (is_mx6dq() || is_mx6dqp()) {
Nikolay Dimitrov8a2bd212015-04-22 18:37:31 +03001229 if (clock > 528)
1230 clock = 528; /* 528 MHz */
1231 }
1232
Nikita Kiryanov33689182014-09-07 18:58:11 +03001233 clkper = (1000 * 1000) / clock; /* pico seconds */
Tim Harveyfe0f7f72014-06-02 16:13:23 -07001234 todtlon = tcwl;
1235 taxpd = tcwl;
1236 tanpd = tcwl;
Tim Harveyfe0f7f72014-06-02 16:13:23 -07001237
Nikita Kiryanov33689182014-09-07 18:58:11 +03001238 switch (ddr3_cfg->density) {
Tim Harveyfe0f7f72014-06-02 16:13:23 -07001239 case 1: /* 1Gb per chip */
1240 trfc = DIV_ROUND_UP(110000, clkper) - 1;
1241 txs = DIV_ROUND_UP(120000, clkper) - 1;
1242 break;
1243 case 2: /* 2Gb per chip */
1244 trfc = DIV_ROUND_UP(160000, clkper) - 1;
1245 txs = DIV_ROUND_UP(170000, clkper) - 1;
1246 break;
1247 case 4: /* 4Gb per chip */
Peng Fan0eca9f62015-09-01 11:03:14 +08001248 trfc = DIV_ROUND_UP(260000, clkper) - 1;
1249 txs = DIV_ROUND_UP(270000, clkper) - 1;
Tim Harveyfe0f7f72014-06-02 16:13:23 -07001250 break;
1251 case 8: /* 8Gb per chip */
1252 trfc = DIV_ROUND_UP(350000, clkper) - 1;
1253 txs = DIV_ROUND_UP(360000, clkper) - 1;
1254 break;
1255 default:
1256 /* invalid density */
Nikita Kiryanov33689182014-09-07 18:58:11 +03001257 puts("invalid chip density\n");
Tim Harveyfe0f7f72014-06-02 16:13:23 -07001258 hang();
1259 break;
1260 }
1261 txpr = txs;
1262
Nikolay Dimitrov8a2bd212015-04-22 18:37:31 +03001263 switch (mem_speed) {
Tim Harveyfe0f7f72014-06-02 16:13:23 -07001264 case 800:
Masahiro Yamadac79cba32014-09-18 13:28:06 +09001265 txp = DIV_ROUND_UP(max(3 * clkper, 7500), clkper) - 1;
1266 tcke = DIV_ROUND_UP(max(3 * clkper, 7500), clkper) - 1;
Nikita Kiryanov33689182014-09-07 18:58:11 +03001267 if (ddr3_cfg->pagesz == 1) {
Tim Harveyfe0f7f72014-06-02 16:13:23 -07001268 tfaw = DIV_ROUND_UP(40000, clkper) - 1;
Masahiro Yamadac79cba32014-09-18 13:28:06 +09001269 trrd = DIV_ROUND_UP(max(4 * clkper, 10000), clkper) - 1;
Tim Harveyfe0f7f72014-06-02 16:13:23 -07001270 } else {
1271 tfaw = DIV_ROUND_UP(50000, clkper) - 1;
Masahiro Yamadac79cba32014-09-18 13:28:06 +09001272 trrd = DIV_ROUND_UP(max(4 * clkper, 10000), clkper) - 1;
Tim Harveyfe0f7f72014-06-02 16:13:23 -07001273 }
1274 break;
1275 case 1066:
Masahiro Yamadac79cba32014-09-18 13:28:06 +09001276 txp = DIV_ROUND_UP(max(3 * clkper, 7500), clkper) - 1;
1277 tcke = DIV_ROUND_UP(max(3 * clkper, 5625), clkper) - 1;
Nikita Kiryanov33689182014-09-07 18:58:11 +03001278 if (ddr3_cfg->pagesz == 1) {
Tim Harveyfe0f7f72014-06-02 16:13:23 -07001279 tfaw = DIV_ROUND_UP(37500, clkper) - 1;
Masahiro Yamadac79cba32014-09-18 13:28:06 +09001280 trrd = DIV_ROUND_UP(max(4 * clkper, 7500), clkper) - 1;
Tim Harveyfe0f7f72014-06-02 16:13:23 -07001281 } else {
1282 tfaw = DIV_ROUND_UP(50000, clkper) - 1;
Masahiro Yamadac79cba32014-09-18 13:28:06 +09001283 trrd = DIV_ROUND_UP(max(4 * clkper, 10000), clkper) - 1;
Tim Harveyfe0f7f72014-06-02 16:13:23 -07001284 }
1285 break;
Tim Harveyfe0f7f72014-06-02 16:13:23 -07001286 default:
Nikita Kiryanov33689182014-09-07 18:58:11 +03001287 puts("invalid memory speed\n");
Tim Harveyfe0f7f72014-06-02 16:13:23 -07001288 hang();
1289 break;
1290 }
Masahiro Yamadac79cba32014-09-18 13:28:06 +09001291 txpdll = DIV_ROUND_UP(max(10 * clkper, 24000), clkper) - 1;
1292 tcksre = DIV_ROUND_UP(max(5 * clkper, 10000), clkper);
Tim Harveyfe0f7f72014-06-02 16:13:23 -07001293 taonpd = DIV_ROUND_UP(2000, clkper) - 1;
Nikita Kiryanov33689182014-09-07 18:58:11 +03001294 tcksrx = tcksre;
Tim Harveyfe0f7f72014-06-02 16:13:23 -07001295 taofpd = taonpd;
Nikita Kiryanov33689182014-09-07 18:58:11 +03001296 twr = DIV_ROUND_UP(15000, clkper) - 1;
Masahiro Yamadac79cba32014-09-18 13:28:06 +09001297 tmrd = DIV_ROUND_UP(max(12 * clkper, 15000), clkper) - 1;
Nikita Kiryanov33689182014-09-07 18:58:11 +03001298 trc = DIV_ROUND_UP(ddr3_cfg->trcmin, clkper / 10) - 1;
1299 tras = DIV_ROUND_UP(ddr3_cfg->trasmin, clkper / 10) - 1;
1300 tcl = DIV_ROUND_UP(ddr3_cfg->trcd, clkper / 10) - 3;
1301 trp = DIV_ROUND_UP(ddr3_cfg->trcd, clkper / 10) - 1;
Masahiro Yamadac79cba32014-09-18 13:28:06 +09001302 twtr = ROUND(max(4 * clkper, 7500) / clkper, 1) - 1;
Tim Harveyfe0f7f72014-06-02 16:13:23 -07001303 trcd = trp;
Tim Harveyfe0f7f72014-06-02 16:13:23 -07001304 trtp = twtr;
Nikita Kiryanov07ee9272014-08-20 15:08:58 +03001305 cs0_end = 4 * sysinfo->cs_density - 1;
Nikita Kiryanov33689182014-09-07 18:58:11 +03001306
1307 debug("density:%d Gb (%d Gb per chip)\n",
1308 sysinfo->cs_density, ddr3_cfg->density);
Tim Harveyfe0f7f72014-06-02 16:13:23 -07001309 debug("clock: %dMHz (%d ps)\n", clock, clkper);
Nikolay Dimitrov8a2bd212015-04-22 18:37:31 +03001310 debug("memspd:%d\n", mem_speed);
Tim Harveyfe0f7f72014-06-02 16:13:23 -07001311 debug("tcke=%d\n", tcke);
1312 debug("tcksrx=%d\n", tcksrx);
1313 debug("tcksre=%d\n", tcksre);
1314 debug("taofpd=%d\n", taofpd);
1315 debug("taonpd=%d\n", taonpd);
1316 debug("todtlon=%d\n", todtlon);
1317 debug("tanpd=%d\n", tanpd);
1318 debug("taxpd=%d\n", taxpd);
1319 debug("trfc=%d\n", trfc);
1320 debug("txs=%d\n", txs);
1321 debug("txp=%d\n", txp);
1322 debug("txpdll=%d\n", txpdll);
1323 debug("tfaw=%d\n", tfaw);
1324 debug("tcl=%d\n", tcl);
1325 debug("trcd=%d\n", trcd);
1326 debug("trp=%d\n", trp);
1327 debug("trc=%d\n", trc);
1328 debug("tras=%d\n", tras);
1329 debug("twr=%d\n", twr);
1330 debug("tmrd=%d\n", tmrd);
1331 debug("tcwl=%d\n", tcwl);
1332 debug("tdllk=%d\n", tdllk);
1333 debug("trtp=%d\n", trtp);
1334 debug("twtr=%d\n", twtr);
1335 debug("trrd=%d\n", trrd);
1336 debug("txpr=%d\n", txpr);
Nikita Kiryanov33689182014-09-07 18:58:11 +03001337 debug("cs0_end=%d\n", cs0_end);
1338 debug("ncs=%d\n", sysinfo->ncs);
1339 debug("Rtt_wr=%d\n", sysinfo->rtt_wr);
1340 debug("Rtt_nom=%d\n", sysinfo->rtt_nom);
1341 debug("SRT=%d\n", ddr3_cfg->SRT);
Tim Harveyfe0f7f72014-06-02 16:13:23 -07001342 debug("twr=%d\n", twr);
1343
1344 /*
1345 * board-specific configuration:
1346 * These values are determined empirically and vary per board layout
1347 * see:
1348 * appnote, ddr3 spreadsheet
1349 */
Nikita Kiryanov33689182014-09-07 18:58:11 +03001350 mmdc0->mpwldectrl0 = calib->p0_mpwldectrl0;
1351 mmdc0->mpwldectrl1 = calib->p0_mpwldectrl1;
1352 mmdc0->mpdgctrl0 = calib->p0_mpdgctrl0;
1353 mmdc0->mpdgctrl1 = calib->p0_mpdgctrl1;
1354 mmdc0->mprddlctl = calib->p0_mprddlctl;
1355 mmdc0->mpwrdlctl = calib->p0_mpwrdlctl;
1356 if (sysinfo->dsize > 1) {
Peng Fand9efd472014-12-30 17:24:01 +08001357 MMDC1(mpwldectrl0, calib->p1_mpwldectrl0);
1358 MMDC1(mpwldectrl1, calib->p1_mpwldectrl1);
1359 MMDC1(mpdgctrl0, calib->p1_mpdgctrl0);
1360 MMDC1(mpdgctrl1, calib->p1_mpdgctrl1);
1361 MMDC1(mprddlctl, calib->p1_mprddlctl);
1362 MMDC1(mpwrdlctl, calib->p1_mpwrdlctl);
Tim Harveyfe0f7f72014-06-02 16:13:23 -07001363 }
1364
1365 /* Read data DQ Byte0-3 delay */
Nikita Kiryanov33689182014-09-07 18:58:11 +03001366 mmdc0->mprddqby0dl = 0x33333333;
1367 mmdc0->mprddqby1dl = 0x33333333;
1368 if (sysinfo->dsize > 0) {
1369 mmdc0->mprddqby2dl = 0x33333333;
1370 mmdc0->mprddqby3dl = 0x33333333;
Tim Harveyfe0f7f72014-06-02 16:13:23 -07001371 }
Nikita Kiryanov33689182014-09-07 18:58:11 +03001372
1373 if (sysinfo->dsize > 1) {
Peng Fand9efd472014-12-30 17:24:01 +08001374 MMDC1(mprddqby0dl, 0x33333333);
1375 MMDC1(mprddqby1dl, 0x33333333);
1376 MMDC1(mprddqby2dl, 0x33333333);
1377 MMDC1(mprddqby3dl, 0x33333333);
Tim Harveyfe0f7f72014-06-02 16:13:23 -07001378 }
1379
1380 /* MMDC Termination: rtt_nom:2 RZQ/2(120ohm), rtt_nom:1 RZQ/4(60ohm) */
Nikita Kiryanov33689182014-09-07 18:58:11 +03001381 val = (sysinfo->rtt_nom == 2) ? 0x00011117 : 0x00022227;
1382 mmdc0->mpodtctrl = val;
1383 if (sysinfo->dsize > 1)
Peng Fand9efd472014-12-30 17:24:01 +08001384 MMDC1(mpodtctrl, val);
Tim Harveyfe0f7f72014-06-02 16:13:23 -07001385
1386 /* complete calibration */
Nikita Kiryanov33689182014-09-07 18:58:11 +03001387 val = (1 << 11); /* Force measurement on delay-lines */
1388 mmdc0->mpmur0 = val;
1389 if (sysinfo->dsize > 1)
Peng Fand9efd472014-12-30 17:24:01 +08001390 MMDC1(mpmur0, val);
Tim Harveyfe0f7f72014-06-02 16:13:23 -07001391
1392 /* Step 1: configuration request */
1393 mmdc0->mdscr = (u32)(1 << 15); /* config request */
1394
1395 /* Step 2: Timing configuration */
Nikita Kiryanov33689182014-09-07 18:58:11 +03001396 mmdc0->mdcfg0 = (trfc << 24) | (txs << 16) | (txp << 13) |
1397 (txpdll << 9) | (tfaw << 4) | tcl;
1398 mmdc0->mdcfg1 = (trcd << 29) | (trp << 26) | (trc << 21) |
1399 (tras << 16) | (1 << 15) /* trpa */ |
1400 (twr << 9) | (tmrd << 5) | tcwl;
1401 mmdc0->mdcfg2 = (tdllk << 16) | (trtp << 6) | (twtr << 3) | trrd;
1402 mmdc0->mdotc = (taofpd << 27) | (taonpd << 24) | (tanpd << 20) |
1403 (taxpd << 16) | (todtlon << 12) | (todt_idle_off << 4);
1404 mmdc0->mdasp = cs0_end; /* CS addressing */
Tim Harveyfe0f7f72014-06-02 16:13:23 -07001405
1406 /* Step 3: Configure DDR type */
Nikita Kiryanov33689182014-09-07 18:58:11 +03001407 mmdc0->mdmisc = (sysinfo->cs1_mirror << 19) | (sysinfo->walat << 16) |
1408 (sysinfo->bi_on << 12) | (sysinfo->mif3_mode << 9) |
1409 (sysinfo->ralat << 6);
Tim Harveyfe0f7f72014-06-02 16:13:23 -07001410
1411 /* Step 4: Configure delay while leaving reset */
Nikita Kiryanov33689182014-09-07 18:58:11 +03001412 mmdc0->mdor = (txpr << 16) | (sysinfo->sde_to_rst << 8) |
1413 (sysinfo->rst_to_cke << 0);
Tim Harveyfe0f7f72014-06-02 16:13:23 -07001414
1415 /* Step 5: Configure DDR physical parameters (density and burst len) */
Nikita Kiryanov33689182014-09-07 18:58:11 +03001416 coladdr = ddr3_cfg->coladdr;
1417 if (ddr3_cfg->coladdr == 8) /* 8-bit COL is 0x3 */
Marek Vasutb299ab72014-08-04 01:47:10 +02001418 coladdr += 4;
Nikita Kiryanov33689182014-09-07 18:58:11 +03001419 else if (ddr3_cfg->coladdr == 12) /* 12-bit COL is 0x4 */
Marek Vasutb299ab72014-08-04 01:47:10 +02001420 coladdr += 1;
Nikita Kiryanov33689182014-09-07 18:58:11 +03001421 mmdc0->mdctl = (ddr3_cfg->rowaddr - 11) << 24 | /* ROW */
1422 (coladdr - 9) << 20 | /* COL */
1423 (1 << 19) | /* Burst Length = 8 for DDR3 */
1424 (sysinfo->dsize << 16); /* DDR data bus size */
Tim Harveyfe0f7f72014-06-02 16:13:23 -07001425
1426 /* Step 6: Perform ZQ calibration */
Nikita Kiryanov33689182014-09-07 18:58:11 +03001427 val = 0xa1390001; /* one-time HW ZQ calib */
1428 mmdc0->mpzqhwctrl = val;
1429 if (sysinfo->dsize > 1)
Peng Fand9efd472014-12-30 17:24:01 +08001430 MMDC1(mpzqhwctrl, val);
Tim Harveyfe0f7f72014-06-02 16:13:23 -07001431
1432 /* Step 7: Enable MMDC with desired chip select */
Nikita Kiryanov33689182014-09-07 18:58:11 +03001433 mmdc0->mdctl |= (1 << 31) | /* SDE_0 for CS0 */
1434 ((sysinfo->ncs == 2) ? 1 : 0) << 30; /* SDE_1 for CS1 */
Tim Harveyfe0f7f72014-06-02 16:13:23 -07001435
1436 /* Step 8: Write Mode Registers to Init DDR3 devices */
Nikita Kiryanov33689182014-09-07 18:58:11 +03001437 for (cs = 0; cs < sysinfo->ncs; cs++) {
Tim Harveyfe0f7f72014-06-02 16:13:23 -07001438 /* MR2 */
Nikita Kiryanov33689182014-09-07 18:58:11 +03001439 val = (sysinfo->rtt_wr & 3) << 9 | (ddr3_cfg->SRT & 1) << 7 |
Tim Harveyfe0f7f72014-06-02 16:13:23 -07001440 ((tcwl - 3) & 3) << 3;
Tim Harvey78c5a182015-04-03 16:52:52 -07001441 debug("MR2 CS%d: 0x%08x\n", cs, (u32)MR(val, 2, 3, cs));
Nikita Kiryanov33689182014-09-07 18:58:11 +03001442 mmdc0->mdscr = MR(val, 2, 3, cs);
Tim Harveyfe0f7f72014-06-02 16:13:23 -07001443 /* MR3 */
Tim Harvey78c5a182015-04-03 16:52:52 -07001444 debug("MR3 CS%d: 0x%08x\n", cs, (u32)MR(0, 3, 3, cs));
Nikita Kiryanov33689182014-09-07 18:58:11 +03001445 mmdc0->mdscr = MR(0, 3, 3, cs);
Tim Harveyfe0f7f72014-06-02 16:13:23 -07001446 /* MR1 */
Nikita Kiryanov33689182014-09-07 18:58:11 +03001447 val = ((sysinfo->rtt_nom & 1) ? 1 : 0) << 2 |
1448 ((sysinfo->rtt_nom & 2) ? 1 : 0) << 6;
Tim Harvey78c5a182015-04-03 16:52:52 -07001449 debug("MR1 CS%d: 0x%08x\n", cs, (u32)MR(val, 1, 3, cs));
Nikita Kiryanov33689182014-09-07 18:58:11 +03001450 mmdc0->mdscr = MR(val, 1, 3, cs);
1451 /* MR0 */
1452 val = ((tcl - 1) << 4) | /* CAS */
Tim Harveyfe0f7f72014-06-02 16:13:23 -07001453 (1 << 8) | /* DLL Reset */
Tim Harvey3625fd62015-05-18 07:07:02 -07001454 ((twr - 3) << 9) | /* Write Recovery */
1455 (sysinfo->pd_fast_exit << 12); /* Precharge PD PLL on */
Tim Harvey78c5a182015-04-03 16:52:52 -07001456 debug("MR0 CS%d: 0x%08x\n", cs, (u32)MR(val, 0, 3, cs));
Nikita Kiryanov33689182014-09-07 18:58:11 +03001457 mmdc0->mdscr = MR(val, 0, 3, cs);
Tim Harveyfe0f7f72014-06-02 16:13:23 -07001458 /* ZQ calibration */
Nikita Kiryanov33689182014-09-07 18:58:11 +03001459 val = (1 << 10);
1460 mmdc0->mdscr = MR(val, 0, 4, cs);
Tim Harveyfe0f7f72014-06-02 16:13:23 -07001461 }
1462
1463 /* Step 10: Power down control and self-refresh */
Nikita Kiryanov33689182014-09-07 18:58:11 +03001464 mmdc0->mdpdc = (tcke & 0x7) << 16 |
1465 5 << 12 | /* PWDT_1: 256 cycles */
1466 5 << 8 | /* PWDT_0: 256 cycles */
1467 1 << 6 | /* BOTH_CS_PD */
1468 (tcksrx & 0x7) << 3 |
1469 (tcksre & 0x7);
Tim Harvey78c5a182015-04-03 16:52:52 -07001470 if (!sysinfo->pd_fast_exit)
1471 mmdc0->mdpdc |= (1 << 7); /* SLOW_PD */
Nikita Kiryanov06a51b82014-08-20 15:08:56 +03001472 mmdc0->mapsr = 0x00001006; /* ADOPT power down enabled */
Tim Harveyfe0f7f72014-06-02 16:13:23 -07001473
1474 /* Step 11: Configure ZQ calibration: one-time and periodic 1ms */
Nikita Kiryanov33689182014-09-07 18:58:11 +03001475 val = 0xa1390003;
1476 mmdc0->mpzqhwctrl = val;
1477 if (sysinfo->dsize > 1)
Peng Fand9efd472014-12-30 17:24:01 +08001478 MMDC1(mpzqhwctrl, val);
Tim Harveyfe0f7f72014-06-02 16:13:23 -07001479
1480 /* Step 12: Configure and activate periodic refresh */
Fabio Estevamedf00932016-08-29 20:37:15 -03001481 mmdc0->mdref = (sysinfo->refsel << 14) | (sysinfo->refr << 11);
Tim Harveyfe0f7f72014-06-02 16:13:23 -07001482
1483 /* Step 13: Deassert config request - init complete */
Nikita Kiryanov33689182014-09-07 18:58:11 +03001484 mmdc0->mdscr = 0x00000000;
Tim Harveyfe0f7f72014-06-02 16:13:23 -07001485
1486 /* wait for auto-ZQ calibration to complete */
1487 mdelay(1);
1488}
Peng Fanf2ff8342015-08-17 16:11:03 +08001489
1490void mx6_dram_cfg(const struct mx6_ddr_sysinfo *sysinfo,
1491 const struct mx6_mmdc_calibration *calib,
1492 const void *ddr_cfg)
1493{
1494 if (sysinfo->ddr_type == DDR_TYPE_DDR3) {
1495 mx6_ddr3_cfg(sysinfo, calib, ddr_cfg);
Peng Faneb796cb2015-08-17 16:11:04 +08001496 } else if (sysinfo->ddr_type == DDR_TYPE_LPDDR2) {
1497 mx6_lpddr2_cfg(sysinfo, calib, ddr_cfg);
Peng Fanf2ff8342015-08-17 16:11:03 +08001498 } else {
1499 puts("Unsupported ddr type\n");
1500 hang();
1501 }
1502}