2 * Copyright (C) 2014 Gateworks Corporation
3 * Author: Tim Harvey <tharvey@gateworks.com>
5 * SPDX-License-Identifier: GPL-2.0+
9 #include <linux/types.h>
10 #include <asm/arch/clock.h>
11 #include <asm/arch/mx6-ddr.h>
12 #include <asm/arch/sys_proto.h>
14 #include <asm/types.h>
17 #if defined(CONFIG_MX6_DDRCAL)
18 static void reset_read_data_fifos(void)
20 struct mmdc_p_regs *mmdc0 = (struct mmdc_p_regs *)MMDC_P0_BASE_ADDR;
22 /* Reset data FIFOs twice. */
23 setbits_le32(&mmdc0->mpdgctrl0, 1 << 31);
24 wait_for_bit_le32(&mmdc0->mpdgctrl0, 1 << 31, 0, 100, 0);
26 setbits_le32(&mmdc0->mpdgctrl0, 1 << 31);
27 wait_for_bit_le32(&mmdc0->mpdgctrl0, 1 << 31, 0, 100, 0);
30 static void precharge_all(const bool cs0_enable, const bool cs1_enable)
32 struct mmdc_p_regs *mmdc0 = (struct mmdc_p_regs *)MMDC_P0_BASE_ADDR;
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
40 if (cs0_enable) { /* CS0 */
41 writel(0x04008050, &mmdc0->mdscr);
42 wait_for_bit_le32(&mmdc0->mdscr, 1 << 14, 1, 100, 0);
45 if (cs1_enable) { /* CS1 */
46 writel(0x04008058, &mmdc0->mdscr);
47 wait_for_bit_le32(&mmdc0->mdscr, 1 << 14, 1, 100, 0);
51 static void force_delay_measurement(int bus_size)
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;
56 writel(0x800, &mmdc0->mpmur0);
58 writel(0x800, &mmdc1->mpmur0);
61 static void modify_dg_result(u32 *reg_st0, u32 *reg_st1, u32 *reg_ctrl)
63 u32 dg_tmp_val, dg_dl_abs_offset, dg_hc_del, val_ctrl;
66 * DQS gating absolute offset should be modified from reflecting
67 * (HW_DG_LOWx + HW_DG_UPx)/2 to reflecting (HW_DG_UPx - 0x80)
70 val_ctrl = readl(reg_ctrl);
71 val_ctrl &= 0xf0000000;
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;
77 val_ctrl |= dg_dl_abs_offset + dg_hc_del;
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;
83 val_ctrl |= (dg_dl_abs_offset + dg_hc_del) << 16;
85 writel(val_ctrl, reg_ctrl);
88 static void correct_mpwldectr_result(void *reg)
90 /* Limit is 200/256 of CK, which is WL_HC_DELx | 0x48. */
91 const unsigned int limit = 0x148;
95 if ((val & 0x17f) > limit)
98 if (((val >> 16) & 0x17f) > limit)
105 int mmdc_do_write_level_calibration(struct mx6_ddr_sysinfo const *sysinfo)
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;
111 u32 ldectrl[4] = {0};
116 * Stash old values in case calibration fails,
117 * we need to restore them
119 ldectrl[0] = readl(&mmdc0->mpwldectrl0);
120 ldectrl[1] = readl(&mmdc0->mpwldectrl1);
121 if (sysinfo->dsize == 2) {
122 ldectrl[2] = readl(&mmdc1->mpwldectrl0);
123 ldectrl[3] = readl(&mmdc1->mpwldectrl1);
126 /* disable DDR logic power down timer */
127 clrbits_le32(&mmdc0->mdpdc, 0xff00);
129 /* disable Adopt power down timer */
130 setbits_le32(&mmdc0->mapsr, 0x1);
132 debug("Starting write leveling calibration.\n");
135 * 2. disable auto refresh and ZQ calibration
136 * before proceeding with Write Leveling calibration
138 esdmisc_val = readl(&mmdc0->mdref);
139 writel(0x0000C000, &mmdc0->mdref);
140 zq_val = readl(&mmdc0->mpzqhwctrl);
141 writel(zq_val & ~0x3, &mmdc0->mpzqhwctrl);
143 /* 3. increase walat and ralat to maximum */
144 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);
149 * 4 & 5. Configure the external DDR device to enter write-leveling
150 * mode through Load Mode Register command.
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
157 writel(0x00808231, &mmdc0->mdscr);
159 /* 6. Activate automatic calibration by setting MPWLGCR[HW_WL_EN] */
160 writel(0x00000001, &mmdc0->mpwlgcr);
163 * 7. Upon completion of this process the MMDC de-asserts
164 * the MPWLGCR[HW_WL_EN]
166 wait_for_bit_le32(&mmdc0->mpwlgcr, 1 << 0, 0, 100, 0);
169 * 8. check for any errors: check both PHYs for x64 configuration,
170 * if x32, check only PHY0
172 if (readl(&mmdc0->mpwlgcr) & 0x00000F00)
174 if (sysinfo->dsize == 2)
175 if (readl(&mmdc1->mpwlgcr) & 0x00000F00)
178 debug("Ending write leveling calibration. Error mask: 0x%x\n", errors);
180 /* check to see if cal failed */
181 if ((readl(&mmdc0->mpwldectrl0) == 0x001F001F) &&
182 (readl(&mmdc0->mpwldectrl1) == 0x001F001F) &&
183 ((sysinfo->dsize < 2) ||
184 ((readl(&mmdc1->mpwldectrl0) == 0x001F001F) &&
185 (readl(&mmdc1->mpwldectrl1) == 0x001F001F)))) {
186 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);
189 if (sysinfo->dsize == 2) {
190 writel(ldectrl[2], &mmdc1->mpwldectrl0);
191 writel(ldectrl[3], &mmdc1->mpwldectrl1);
196 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);
204 * User should issue MRS command to exit write leveling mode
205 * through Load Mode Register command
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
212 writel((ddr_mr1 << 16) + 0x8031, &mmdc0->mdscr);
214 /* re-enable auto refresh and zq cal */
215 writel(esdmisc_val, &mmdc0->mdref);
216 writel(zq_val, &mmdc0->mpzqhwctrl);
218 debug("\tMMDC_MPWLDECTRL0 after write level cal: 0x%08X\n",
219 readl(&mmdc0->mpwldectrl0));
220 debug("\tMMDC_MPWLDECTRL1 after write level cal: 0x%08X\n",
221 readl(&mmdc0->mpwldectrl1));
222 if (sysinfo->dsize == 2) {
223 debug("\tMMDC_MPWLDECTRL0 after write level cal: 0x%08X\n",
224 readl(&mmdc1->mpwldectrl0));
225 debug("\tMMDC_MPWLDECTRL1 after write level cal: 0x%08X\n",
226 readl(&mmdc1->mpwldectrl1));
229 /* We must force a readback of these values, to get them to stick */
230 readl(&mmdc0->mpwldectrl0);
231 readl(&mmdc0->mpwldectrl1);
232 if (sysinfo->dsize == 2) {
233 readl(&mmdc1->mpwldectrl0);
234 readl(&mmdc1->mpwldectrl1);
237 /* enable DDR logic power down timer: */
238 setbits_le32(&mmdc0->mdpdc, 0x00005500);
240 /* Enable Adopt power down timer: */
241 clrbits_le32(&mmdc0->mapsr, 0x1);
244 writel(0, &mmdc0->mdscr);
249 int mmdc_do_dqs_calibration(struct mx6_ddr_sysinfo const *sysinfo)
251 struct mmdc_p_regs *mmdc0 = (struct mmdc_p_regs *)MMDC_P0_BASE_ADDR;
252 struct mmdc_p_regs *mmdc1 = (struct mmdc_p_regs *)MMDC_P1_BASE_ADDR;
253 struct mx6dq_iomux_ddr_regs *mx6_ddr_iomux =
254 (struct mx6dq_iomux_ddr_regs *)MX6DQ_IOM_DDR_BASE;
257 bool cs0_enable_initial;
258 bool cs1_enable_initial;
261 u32 pddword = 0x00ffff00; /* best so far, place into MPPDCMPR1 */
263 u32 initdelay = 0x40404040;
265 /* check to see which chip selects are enabled */
266 cs0_enable_initial = readl(&mmdc0->mdctl) & 0x80000000;
267 cs1_enable_initial = readl(&mmdc0->mdctl) & 0x40000000;
269 /* disable DDR logic power down timer: */
270 clrbits_le32(&mmdc0->mdpdc, 0xff00);
272 /* disable Adopt power down timer: */
273 setbits_le32(&mmdc0->mapsr, 0x1);
275 /* set DQS pull ups */
276 setbits_le32(&mx6_ddr_iomux->dram_sdqs0, 0x7000);
277 setbits_le32(&mx6_ddr_iomux->dram_sdqs1, 0x7000);
278 setbits_le32(&mx6_ddr_iomux->dram_sdqs2, 0x7000);
279 setbits_le32(&mx6_ddr_iomux->dram_sdqs3, 0x7000);
280 setbits_le32(&mx6_ddr_iomux->dram_sdqs4, 0x7000);
281 setbits_le32(&mx6_ddr_iomux->dram_sdqs5, 0x7000);
282 setbits_le32(&mx6_ddr_iomux->dram_sdqs6, 0x7000);
283 setbits_le32(&mx6_ddr_iomux->dram_sdqs7, 0x7000);
285 /* Save old RALAT and WALAT values */
286 esdmisc_val = readl(&mmdc0->mdmisc);
288 setbits_le32(&mmdc0->mdmisc,
289 (1 << 6) | (1 << 7) | (1 << 8) | (1 << 16) | (1 << 17));
291 /* Disable auto refresh before proceeding with calibration */
292 temp_ref = readl(&mmdc0->mdref);
293 writel(0x0000c000, &mmdc0->mdref);
296 * Per the ref manual, issue one refresh cycle MDSCR[CMD]= 0x2,
297 * this also sets the CON_REQ bit.
299 if (cs0_enable_initial)
300 writel(0x00008020, &mmdc0->mdscr);
301 if (cs1_enable_initial)
302 writel(0x00008028, &mmdc0->mdscr);
304 /* poll to make sure the con_ack bit was asserted */
305 wait_for_bit_le32(&mmdc0->mdscr, 1 << 14, 1, 100, 0);
308 * Check MDMISC register CALIB_PER_CS to see which CS calibration
309 * is targeted to (under normal cases, it should be cleared
310 * as this is the default value, indicating calibration is directed
312 * Disable the other chip select not being target for calibration
313 * to avoid any potential issues. This will get re-enabled at end
316 if ((readl(&mmdc0->mdmisc) & 0x00100000) == 0)
317 clrbits_le32(&mmdc0->mdctl, 1 << 30); /* clear SDE_1 */
319 clrbits_le32(&mmdc0->mdctl, 1 << 31); /* clear SDE_0 */
322 * Check to see which chip selects are now enabled for
323 * the remainder of the calibration.
325 cs0_enable = readl(&mmdc0->mdctl) & 0x80000000;
326 cs1_enable = readl(&mmdc0->mdctl) & 0x40000000;
328 precharge_all(cs0_enable, cs1_enable);
330 /* Write the pre-defined value into MPPDCMPR1 */
331 writel(pddword, &mmdc0->mppdcmpr1);
334 * Issue a write access to the external DDR device by setting
335 * the bit SW_DUMMY_WR (bit 0) in the MPSWDAR0 and then poll
336 * this bit until it clears to indicate completion of the write access.
338 setbits_le32(&mmdc0->mpswdar0, 1);
339 wait_for_bit_le32(&mmdc0->mpswdar0, 1 << 0, 0, 100, 0);
341 /* Set the RD_DL_ABS# bits to their default values
342 * (will be calibrated later in the read delay-line calibration).
343 * Both PHYs for x64 configuration, if x32, do only PHY0.
345 writel(initdelay, &mmdc0->mprddlctl);
346 if (sysinfo->dsize == 0x2)
347 writel(initdelay, &mmdc1->mprddlctl);
349 /* Force a measurment, for previous delay setup to take effect. */
350 force_delay_measurement(sysinfo->dsize);
353 * ***************************
354 * Read DQS Gating calibration
355 * ***************************
357 debug("Starting Read DQS Gating calibration.\n");
360 * Reset the read data FIFOs (two resets); only need to issue reset
361 * to PHY0 since in x64 mode, the reset will also go to PHY1.
363 reset_read_data_fifos();
366 * Start the automatic read DQS gating calibration process by
367 * asserting MPDGCTRL0[HW_DG_EN] and MPDGCTRL0[DG_CMP_CYC]
368 * and then poll MPDGCTRL0[HW_DG_EN]] until this bit clears
369 * to indicate completion.
370 * Also, ensure that MPDGCTRL0[HW_DG_ERR] is clear to indicate
371 * no errors were seen during calibration.
375 * Set bit 30: chooses option to wait 32 cycles instead of
376 * 16 before comparing read data.
378 setbits_le32(&mmdc0->mpdgctrl0, 1 << 30);
379 if (sysinfo->dsize == 2)
380 setbits_le32(&mmdc1->mpdgctrl0, 1 << 30);
382 /* Set bit 28 to start automatic read DQS gating calibration */
383 setbits_le32(&mmdc0->mpdgctrl0, 5 << 28);
385 /* Poll for completion. MPDGCTRL0[HW_DG_EN] should be 0 */
386 wait_for_bit_le32(&mmdc0->mpdgctrl0, 1 << 28, 0, 100, 0);
389 * Check to see if any errors were encountered during calibration
390 * (check MPDGCTRL0[HW_DG_ERR]).
391 * Check both PHYs for x64 configuration, if x32, check only PHY0.
393 if (readl(&mmdc0->mpdgctrl0) & 0x00001000)
396 if ((sysinfo->dsize == 0x2) && (readl(&mmdc1->mpdgctrl0) & 0x00001000))
399 /* now disable mpdgctrl0[DG_CMP_CYC] */
400 clrbits_le32(&mmdc0->mpdgctrl0, 1 << 30);
401 if (sysinfo->dsize == 2)
402 clrbits_le32(&mmdc1->mpdgctrl0, 1 << 30);
405 * DQS gating absolute offset should be modified from
406 * reflecting (HW_DG_LOWx + HW_DG_UPx)/2 to
407 * reflecting (HW_DG_UPx - 0x80)
409 modify_dg_result(&mmdc0->mpdghwst0, &mmdc0->mpdghwst1,
411 modify_dg_result(&mmdc0->mpdghwst2, &mmdc0->mpdghwst3,
413 if (sysinfo->dsize == 0x2) {
414 modify_dg_result(&mmdc1->mpdghwst0, &mmdc1->mpdghwst1,
416 modify_dg_result(&mmdc1->mpdghwst2, &mmdc1->mpdghwst3,
419 debug("Ending Read DQS Gating calibration. Error mask: 0x%x\n", errors);
422 * **********************
423 * Read Delay calibration
424 * **********************
426 debug("Starting Read Delay calibration.\n");
428 reset_read_data_fifos();
431 * 4. Issue the Precharge-All command to the DDR device for both
432 * chip selects. If only using one chip select, then precharge
433 * only the desired chip select.
435 precharge_all(cs0_enable, cs1_enable);
438 * 9. Read delay-line calibration
439 * Start the automatic read calibration process by asserting
440 * MPRDDLHWCTL[HW_RD_DL_EN].
442 writel(0x00000030, &mmdc0->mprddlhwctl);
445 * 10. poll for completion
446 * MMDC indicates that the write data calibration had finished by
447 * setting MPRDDLHWCTL[HW_RD_DL_EN] = 0. Also, ensure that
448 * no error bits were set.
450 wait_for_bit_le32(&mmdc0->mprddlhwctl, 1 << 4, 0, 100, 0);
452 /* check both PHYs for x64 configuration, if x32, check only PHY0 */
453 if (readl(&mmdc0->mprddlhwctl) & 0x0000000f)
456 if ((sysinfo->dsize == 0x2) &&
457 (readl(&mmdc1->mprddlhwctl) & 0x0000000f))
460 debug("Ending Read Delay calibration. Error mask: 0x%x\n", errors);
463 * ***********************
464 * Write Delay Calibration
465 * ***********************
467 debug("Starting Write Delay calibration.\n");
469 reset_read_data_fifos();
472 * 4. Issue the Precharge-All command to the DDR device for both
473 * chip selects. If only using one chip select, then precharge
474 * only the desired chip select.
476 precharge_all(cs0_enable, cs1_enable);
479 * 8. Set the WR_DL_ABS# bits to their default values.
480 * Both PHYs for x64 configuration, if x32, do only PHY0.
482 writel(initdelay, &mmdc0->mpwrdlctl);
483 if (sysinfo->dsize == 0x2)
484 writel(initdelay, &mmdc1->mpwrdlctl);
487 * XXX This isn't in the manual. Force a measurement,
488 * for previous delay setup to effect.
490 force_delay_measurement(sysinfo->dsize);
493 * 9. 10. Start the automatic write calibration process
494 * by asserting MPWRDLHWCTL0[HW_WR_DL_EN].
496 writel(0x00000030, &mmdc0->mpwrdlhwctl);
499 * Poll for completion.
500 * MMDC indicates that the write data calibration had finished
501 * by setting MPWRDLHWCTL[HW_WR_DL_EN] = 0.
502 * Also, ensure that no error bits were set.
504 wait_for_bit_le32(&mmdc0->mpwrdlhwctl, 1 << 4, 0, 100, 0);
506 /* Check both PHYs for x64 configuration, if x32, check only PHY0 */
507 if (readl(&mmdc0->mpwrdlhwctl) & 0x0000000f)
510 if ((sysinfo->dsize == 0x2) &&
511 (readl(&mmdc1->mpwrdlhwctl) & 0x0000000f))
514 debug("Ending Write Delay calibration. Error mask: 0x%x\n", errors);
516 reset_read_data_fifos();
518 /* Enable DDR logic power down timer */
519 setbits_le32(&mmdc0->mdpdc, 0x00005500);
521 /* Enable Adopt power down timer */
522 clrbits_le32(&mmdc0->mapsr, 0x1);
524 /* Restore MDMISC value (RALAT, WALAT) to MMDCP1 */
525 writel(esdmisc_val, &mmdc0->mdmisc);
527 /* Clear DQS pull ups */
528 clrbits_le32(&mx6_ddr_iomux->dram_sdqs0, 0x7000);
529 clrbits_le32(&mx6_ddr_iomux->dram_sdqs1, 0x7000);
530 clrbits_le32(&mx6_ddr_iomux->dram_sdqs2, 0x7000);
531 clrbits_le32(&mx6_ddr_iomux->dram_sdqs3, 0x7000);
532 clrbits_le32(&mx6_ddr_iomux->dram_sdqs4, 0x7000);
533 clrbits_le32(&mx6_ddr_iomux->dram_sdqs5, 0x7000);
534 clrbits_le32(&mx6_ddr_iomux->dram_sdqs6, 0x7000);
535 clrbits_le32(&mx6_ddr_iomux->dram_sdqs7, 0x7000);
537 /* Re-enable SDE (chip selects) if they were set initially */
538 if (cs1_enable_initial)
540 setbits_le32(&mmdc0->mdctl, 1 << 30);
542 if (cs0_enable_initial)
544 setbits_le32(&mmdc0->mdctl, 1 << 31);
546 /* Re-enable to auto refresh */
547 writel(temp_ref, &mmdc0->mdref);
549 /* Clear the MDSCR (including the con_req bit) */
550 writel(0x0, &mmdc0->mdscr); /* CS0 */
552 /* Poll to make sure the con_ack bit is clear */
553 wait_for_bit_le32(&mmdc0->mdscr, 1 << 14, 0, 100, 0);
556 * Print out the registers that were updated as a result
557 * of the calibration process.
559 debug("MMDC registers updated from calibration\n");
560 debug("Read DQS gating calibration:\n");
561 debug("\tMPDGCTRL0 PHY0 = 0x%08X\n", readl(&mmdc0->mpdgctrl0));
562 debug("\tMPDGCTRL1 PHY0 = 0x%08X\n", readl(&mmdc0->mpdgctrl1));
563 if (sysinfo->dsize == 2) {
564 debug("\tMPDGCTRL0 PHY1 = 0x%08X\n", readl(&mmdc1->mpdgctrl0));
565 debug("\tMPDGCTRL1 PHY1 = 0x%08X\n", readl(&mmdc1->mpdgctrl1));
567 debug("Read calibration:\n");
568 debug("\tMPRDDLCTL PHY0 = 0x%08X\n", readl(&mmdc0->mprddlctl));
569 if (sysinfo->dsize == 2)
570 debug("\tMPRDDLCTL PHY1 = 0x%08X\n", readl(&mmdc1->mprddlctl));
571 debug("Write calibration:\n");
572 debug("\tMPWRDLCTL PHY0 = 0x%08X\n", readl(&mmdc0->mpwrdlctl));
573 if (sysinfo->dsize == 2)
574 debug("\tMPWRDLCTL PHY1 = 0x%08X\n", readl(&mmdc1->mpwrdlctl));
577 * Registers below are for debugging purposes. These print out
578 * the upper and lower boundaries captured during
579 * read DQS gating calibration.
581 debug("Status registers bounds for read DQS gating:\n");
582 debug("\tMPDGHWST0 PHY0 = 0x%08x\n", readl(&mmdc0->mpdghwst0));
583 debug("\tMPDGHWST1 PHY0 = 0x%08x\n", readl(&mmdc0->mpdghwst1));
584 debug("\tMPDGHWST2 PHY0 = 0x%08x\n", readl(&mmdc0->mpdghwst2));
585 debug("\tMPDGHWST3 PHY0 = 0x%08x\n", readl(&mmdc0->mpdghwst3));
586 if (sysinfo->dsize == 2) {
587 debug("\tMPDGHWST0 PHY1 = 0x%08x\n", readl(&mmdc1->mpdghwst0));
588 debug("\tMPDGHWST1 PHY1 = 0x%08x\n", readl(&mmdc1->mpdghwst1));
589 debug("\tMPDGHWST2 PHY1 = 0x%08x\n", readl(&mmdc1->mpdghwst2));
590 debug("\tMPDGHWST3 PHY1 = 0x%08x\n", readl(&mmdc1->mpdghwst3));
593 debug("Final do_dqs_calibration error mask: 0x%x\n", errors);
599 #if defined(CONFIG_MX6SX)
600 /* Configure MX6SX mmdc iomux */
601 void mx6sx_dram_iocfg(unsigned width,
602 const struct mx6sx_iomux_ddr_regs *ddr,
603 const struct mx6sx_iomux_grp_regs *grp)
605 struct mx6sx_iomux_ddr_regs *mx6_ddr_iomux;
606 struct mx6sx_iomux_grp_regs *mx6_grp_iomux;
608 mx6_ddr_iomux = (struct mx6sx_iomux_ddr_regs *)MX6SX_IOM_DDR_BASE;
609 mx6_grp_iomux = (struct mx6sx_iomux_grp_regs *)MX6SX_IOM_GRP_BASE;
612 writel(grp->grp_ddr_type, &mx6_grp_iomux->grp_ddr_type);
613 writel(grp->grp_ddrpke, &mx6_grp_iomux->grp_ddrpke);
616 writel(ddr->dram_sdclk_0, &mx6_ddr_iomux->dram_sdclk_0);
619 writel(ddr->dram_cas, &mx6_ddr_iomux->dram_cas);
620 writel(ddr->dram_ras, &mx6_ddr_iomux->dram_ras);
621 writel(grp->grp_addds, &mx6_grp_iomux->grp_addds);
624 writel(ddr->dram_reset, &mx6_ddr_iomux->dram_reset);
625 writel(ddr->dram_sdba2, &mx6_ddr_iomux->dram_sdba2);
626 writel(ddr->dram_sdcke0, &mx6_ddr_iomux->dram_sdcke0);
627 writel(ddr->dram_sdcke1, &mx6_ddr_iomux->dram_sdcke1);
628 writel(ddr->dram_odt0, &mx6_ddr_iomux->dram_odt0);
629 writel(ddr->dram_odt1, &mx6_ddr_iomux->dram_odt1);
630 writel(grp->grp_ctlds, &mx6_grp_iomux->grp_ctlds);
633 writel(grp->grp_ddrmode_ctl, &mx6_grp_iomux->grp_ddrmode_ctl);
634 writel(ddr->dram_sdqs0, &mx6_ddr_iomux->dram_sdqs0);
635 writel(ddr->dram_sdqs1, &mx6_ddr_iomux->dram_sdqs1);
637 writel(ddr->dram_sdqs2, &mx6_ddr_iomux->dram_sdqs2);
638 writel(ddr->dram_sdqs3, &mx6_ddr_iomux->dram_sdqs3);
642 writel(grp->grp_ddrmode, &mx6_grp_iomux->grp_ddrmode);
643 writel(grp->grp_b0ds, &mx6_grp_iomux->grp_b0ds);
644 writel(grp->grp_b1ds, &mx6_grp_iomux->grp_b1ds);
646 writel(grp->grp_b2ds, &mx6_grp_iomux->grp_b2ds);
647 writel(grp->grp_b3ds, &mx6_grp_iomux->grp_b3ds);
649 writel(ddr->dram_dqm0, &mx6_ddr_iomux->dram_dqm0);
650 writel(ddr->dram_dqm1, &mx6_ddr_iomux->dram_dqm1);
652 writel(ddr->dram_dqm2, &mx6_ddr_iomux->dram_dqm2);
653 writel(ddr->dram_dqm3, &mx6_ddr_iomux->dram_dqm3);
658 #if defined(CONFIG_MX6UL) || defined(CONFIG_MX6ULL)
659 void mx6ul_dram_iocfg(unsigned width,
660 const struct mx6ul_iomux_ddr_regs *ddr,
661 const struct mx6ul_iomux_grp_regs *grp)
663 struct mx6ul_iomux_ddr_regs *mx6_ddr_iomux;
664 struct mx6ul_iomux_grp_regs *mx6_grp_iomux;
666 mx6_ddr_iomux = (struct mx6ul_iomux_ddr_regs *)MX6UL_IOM_DDR_BASE;
667 mx6_grp_iomux = (struct mx6ul_iomux_grp_regs *)MX6UL_IOM_GRP_BASE;
670 writel(grp->grp_ddr_type, &mx6_grp_iomux->grp_ddr_type);
671 writel(grp->grp_ddrpke, &mx6_grp_iomux->grp_ddrpke);
674 writel(ddr->dram_sdclk_0, &mx6_ddr_iomux->dram_sdclk_0);
677 writel(ddr->dram_cas, &mx6_ddr_iomux->dram_cas);
678 writel(ddr->dram_ras, &mx6_ddr_iomux->dram_ras);
679 writel(grp->grp_addds, &mx6_grp_iomux->grp_addds);
682 writel(ddr->dram_reset, &mx6_ddr_iomux->dram_reset);
683 writel(ddr->dram_sdba2, &mx6_ddr_iomux->dram_sdba2);
684 writel(ddr->dram_odt0, &mx6_ddr_iomux->dram_odt0);
685 writel(ddr->dram_odt1, &mx6_ddr_iomux->dram_odt1);
686 writel(grp->grp_ctlds, &mx6_grp_iomux->grp_ctlds);
689 writel(grp->grp_ddrmode_ctl, &mx6_grp_iomux->grp_ddrmode_ctl);
690 writel(ddr->dram_sdqs0, &mx6_ddr_iomux->dram_sdqs0);
691 writel(ddr->dram_sdqs1, &mx6_ddr_iomux->dram_sdqs1);
694 writel(grp->grp_ddrmode, &mx6_grp_iomux->grp_ddrmode);
695 writel(grp->grp_b0ds, &mx6_grp_iomux->grp_b0ds);
696 writel(grp->grp_b1ds, &mx6_grp_iomux->grp_b1ds);
697 writel(ddr->dram_dqm0, &mx6_ddr_iomux->dram_dqm0);
698 writel(ddr->dram_dqm1, &mx6_ddr_iomux->dram_dqm1);
702 #if defined(CONFIG_MX6SL)
703 void mx6sl_dram_iocfg(unsigned width,
704 const struct mx6sl_iomux_ddr_regs *ddr,
705 const struct mx6sl_iomux_grp_regs *grp)
707 struct mx6sl_iomux_ddr_regs *mx6_ddr_iomux;
708 struct mx6sl_iomux_grp_regs *mx6_grp_iomux;
710 mx6_ddr_iomux = (struct mx6sl_iomux_ddr_regs *)MX6SL_IOM_DDR_BASE;
711 mx6_grp_iomux = (struct mx6sl_iomux_grp_regs *)MX6SL_IOM_GRP_BASE;
714 mx6_grp_iomux->grp_ddr_type = grp->grp_ddr_type;
715 mx6_grp_iomux->grp_ddrpke = grp->grp_ddrpke;
718 mx6_ddr_iomux->dram_sdclk_0 = ddr->dram_sdclk_0;
721 mx6_ddr_iomux->dram_cas = ddr->dram_cas;
722 mx6_ddr_iomux->dram_ras = ddr->dram_ras;
723 mx6_grp_iomux->grp_addds = grp->grp_addds;
726 mx6_ddr_iomux->dram_reset = ddr->dram_reset;
727 mx6_ddr_iomux->dram_sdba2 = ddr->dram_sdba2;
728 mx6_grp_iomux->grp_ctlds = grp->grp_ctlds;
731 mx6_grp_iomux->grp_ddrmode_ctl = grp->grp_ddrmode_ctl;
732 mx6_ddr_iomux->dram_sdqs0 = ddr->dram_sdqs0;
733 mx6_ddr_iomux->dram_sdqs1 = ddr->dram_sdqs1;
735 mx6_ddr_iomux->dram_sdqs2 = ddr->dram_sdqs2;
736 mx6_ddr_iomux->dram_sdqs3 = ddr->dram_sdqs3;
740 mx6_grp_iomux->grp_ddrmode = grp->grp_ddrmode;
741 mx6_grp_iomux->grp_b0ds = grp->grp_b0ds;
742 mx6_grp_iomux->grp_b1ds = grp->grp_b1ds;
744 mx6_grp_iomux->grp_b2ds = grp->grp_b2ds;
745 mx6_grp_iomux->grp_b3ds = grp->grp_b3ds;
748 mx6_ddr_iomux->dram_dqm0 = ddr->dram_dqm0;
749 mx6_ddr_iomux->dram_dqm1 = ddr->dram_dqm1;
751 mx6_ddr_iomux->dram_dqm2 = ddr->dram_dqm2;
752 mx6_ddr_iomux->dram_dqm3 = ddr->dram_dqm3;
757 #if defined(CONFIG_MX6QDL) || defined(CONFIG_MX6Q) || defined(CONFIG_MX6D)
758 /* Configure MX6DQ mmdc iomux */
759 void mx6dq_dram_iocfg(unsigned width,
760 const struct mx6dq_iomux_ddr_regs *ddr,
761 const struct mx6dq_iomux_grp_regs *grp)
763 volatile struct mx6dq_iomux_ddr_regs *mx6_ddr_iomux;
764 volatile struct mx6dq_iomux_grp_regs *mx6_grp_iomux;
766 mx6_ddr_iomux = (struct mx6dq_iomux_ddr_regs *)MX6DQ_IOM_DDR_BASE;
767 mx6_grp_iomux = (struct mx6dq_iomux_grp_regs *)MX6DQ_IOM_GRP_BASE;
770 mx6_grp_iomux->grp_ddr_type = grp->grp_ddr_type;
771 mx6_grp_iomux->grp_ddrpke = grp->grp_ddrpke;
774 mx6_ddr_iomux->dram_sdclk_0 = ddr->dram_sdclk_0;
775 mx6_ddr_iomux->dram_sdclk_1 = ddr->dram_sdclk_1;
778 mx6_ddr_iomux->dram_cas = ddr->dram_cas;
779 mx6_ddr_iomux->dram_ras = ddr->dram_ras;
780 mx6_grp_iomux->grp_addds = grp->grp_addds;
783 mx6_ddr_iomux->dram_reset = ddr->dram_reset;
784 mx6_ddr_iomux->dram_sdcke0 = ddr->dram_sdcke0;
785 mx6_ddr_iomux->dram_sdcke1 = ddr->dram_sdcke1;
786 mx6_ddr_iomux->dram_sdba2 = ddr->dram_sdba2;
787 mx6_ddr_iomux->dram_sdodt0 = ddr->dram_sdodt0;
788 mx6_ddr_iomux->dram_sdodt1 = ddr->dram_sdodt1;
789 mx6_grp_iomux->grp_ctlds = grp->grp_ctlds;
792 mx6_grp_iomux->grp_ddrmode_ctl = grp->grp_ddrmode_ctl;
793 mx6_ddr_iomux->dram_sdqs0 = ddr->dram_sdqs0;
794 mx6_ddr_iomux->dram_sdqs1 = ddr->dram_sdqs1;
796 mx6_ddr_iomux->dram_sdqs2 = ddr->dram_sdqs2;
797 mx6_ddr_iomux->dram_sdqs3 = ddr->dram_sdqs3;
800 mx6_ddr_iomux->dram_sdqs4 = ddr->dram_sdqs4;
801 mx6_ddr_iomux->dram_sdqs5 = ddr->dram_sdqs5;
802 mx6_ddr_iomux->dram_sdqs6 = ddr->dram_sdqs6;
803 mx6_ddr_iomux->dram_sdqs7 = ddr->dram_sdqs7;
807 mx6_grp_iomux->grp_ddrmode = grp->grp_ddrmode;
808 mx6_grp_iomux->grp_b0ds = grp->grp_b0ds;
809 mx6_grp_iomux->grp_b1ds = grp->grp_b1ds;
811 mx6_grp_iomux->grp_b2ds = grp->grp_b2ds;
812 mx6_grp_iomux->grp_b3ds = grp->grp_b3ds;
815 mx6_grp_iomux->grp_b4ds = grp->grp_b4ds;
816 mx6_grp_iomux->grp_b5ds = grp->grp_b5ds;
817 mx6_grp_iomux->grp_b6ds = grp->grp_b6ds;
818 mx6_grp_iomux->grp_b7ds = grp->grp_b7ds;
820 mx6_ddr_iomux->dram_dqm0 = ddr->dram_dqm0;
821 mx6_ddr_iomux->dram_dqm1 = ddr->dram_dqm1;
823 mx6_ddr_iomux->dram_dqm2 = ddr->dram_dqm2;
824 mx6_ddr_iomux->dram_dqm3 = ddr->dram_dqm3;
827 mx6_ddr_iomux->dram_dqm4 = ddr->dram_dqm4;
828 mx6_ddr_iomux->dram_dqm5 = ddr->dram_dqm5;
829 mx6_ddr_iomux->dram_dqm6 = ddr->dram_dqm6;
830 mx6_ddr_iomux->dram_dqm7 = ddr->dram_dqm7;
835 #if defined(CONFIG_MX6QDL) || defined(CONFIG_MX6DL) || defined(CONFIG_MX6S)
836 /* Configure MX6SDL mmdc iomux */
837 void mx6sdl_dram_iocfg(unsigned width,
838 const struct mx6sdl_iomux_ddr_regs *ddr,
839 const struct mx6sdl_iomux_grp_regs *grp)
841 volatile struct mx6sdl_iomux_ddr_regs *mx6_ddr_iomux;
842 volatile struct mx6sdl_iomux_grp_regs *mx6_grp_iomux;
844 mx6_ddr_iomux = (struct mx6sdl_iomux_ddr_regs *)MX6SDL_IOM_DDR_BASE;
845 mx6_grp_iomux = (struct mx6sdl_iomux_grp_regs *)MX6SDL_IOM_GRP_BASE;
848 mx6_grp_iomux->grp_ddr_type = grp->grp_ddr_type;
849 mx6_grp_iomux->grp_ddrpke = grp->grp_ddrpke;
852 mx6_ddr_iomux->dram_sdclk_0 = ddr->dram_sdclk_0;
853 mx6_ddr_iomux->dram_sdclk_1 = ddr->dram_sdclk_1;
856 mx6_ddr_iomux->dram_cas = ddr->dram_cas;
857 mx6_ddr_iomux->dram_ras = ddr->dram_ras;
858 mx6_grp_iomux->grp_addds = grp->grp_addds;
861 mx6_ddr_iomux->dram_reset = ddr->dram_reset;
862 mx6_ddr_iomux->dram_sdcke0 = ddr->dram_sdcke0;
863 mx6_ddr_iomux->dram_sdcke1 = ddr->dram_sdcke1;
864 mx6_ddr_iomux->dram_sdba2 = ddr->dram_sdba2;
865 mx6_ddr_iomux->dram_sdodt0 = ddr->dram_sdodt0;
866 mx6_ddr_iomux->dram_sdodt1 = ddr->dram_sdodt1;
867 mx6_grp_iomux->grp_ctlds = grp->grp_ctlds;
870 mx6_grp_iomux->grp_ddrmode_ctl = grp->grp_ddrmode_ctl;
871 mx6_ddr_iomux->dram_sdqs0 = ddr->dram_sdqs0;
872 mx6_ddr_iomux->dram_sdqs1 = ddr->dram_sdqs1;
874 mx6_ddr_iomux->dram_sdqs2 = ddr->dram_sdqs2;
875 mx6_ddr_iomux->dram_sdqs3 = ddr->dram_sdqs3;
878 mx6_ddr_iomux->dram_sdqs4 = ddr->dram_sdqs4;
879 mx6_ddr_iomux->dram_sdqs5 = ddr->dram_sdqs5;
880 mx6_ddr_iomux->dram_sdqs6 = ddr->dram_sdqs6;
881 mx6_ddr_iomux->dram_sdqs7 = ddr->dram_sdqs7;
885 mx6_grp_iomux->grp_ddrmode = grp->grp_ddrmode;
886 mx6_grp_iomux->grp_b0ds = grp->grp_b0ds;
887 mx6_grp_iomux->grp_b1ds = grp->grp_b1ds;
889 mx6_grp_iomux->grp_b2ds = grp->grp_b2ds;
890 mx6_grp_iomux->grp_b3ds = grp->grp_b3ds;
893 mx6_grp_iomux->grp_b4ds = grp->grp_b4ds;
894 mx6_grp_iomux->grp_b5ds = grp->grp_b5ds;
895 mx6_grp_iomux->grp_b6ds = grp->grp_b6ds;
896 mx6_grp_iomux->grp_b7ds = grp->grp_b7ds;
898 mx6_ddr_iomux->dram_dqm0 = ddr->dram_dqm0;
899 mx6_ddr_iomux->dram_dqm1 = ddr->dram_dqm1;
901 mx6_ddr_iomux->dram_dqm2 = ddr->dram_dqm2;
902 mx6_ddr_iomux->dram_dqm3 = ddr->dram_dqm3;
905 mx6_ddr_iomux->dram_dqm4 = ddr->dram_dqm4;
906 mx6_ddr_iomux->dram_dqm5 = ddr->dram_dqm5;
907 mx6_ddr_iomux->dram_dqm6 = ddr->dram_dqm6;
908 mx6_ddr_iomux->dram_dqm7 = ddr->dram_dqm7;
914 * Configure mx6 mmdc registers based on:
915 * - board-specific memory configuration
916 * - board-specific calibration data
917 * - ddr3/lpddr2 chip details
919 * The various calculations here are derived from the Freescale
920 * 1. i.Mx6DQSDL DDR3 Script Aid spreadsheet (DOC-94917) designed to generate
921 * MMDC configuration registers based on memory system and memory chip
924 * 2. i.Mx6SL LPDDR2 Script Aid spreadsheet V0.04 designed to generate MMDC
925 * configuration registers based on memory system and memory chip
928 * The defaults here are those which were specified in the spreadsheet.
929 * For details on each register, refer to the IMX6DQRM and/or IMX6SDLRM
930 * and/or IMX6SLRM section titled MMDC initialization.
932 #define MR(val, ba, cmd, cs1) \
933 ((val << 16) | (1 << 15) | (cmd << 4) | (cs1 << 3) | ba)
934 #define MMDC1(entry, value) do { \
935 if (!is_mx6sx() && !is_mx6ul() && !is_mx6ull() && !is_mx6sl()) \
936 mmdc1->entry = value; \
940 * According JESD209-2B-LPDDR2: Table 103
943 static int lpddr2_wl(uint32_t mem_speed)
958 puts("invalid memory speed\n");
966 * According JESD209-2B-LPDDR2: Table 103
969 static int lpddr2_rl(uint32_t mem_speed)
986 puts("invalid memory speed\n");
993 void mx6_lpddr2_cfg(const struct mx6_ddr_sysinfo *sysinfo,
994 const struct mx6_mmdc_calibration *calib,
995 const struct mx6_lpddr2_cfg *lpddr2_cfg)
997 volatile struct mmdc_p_regs *mmdc0;
999 u8 tcke, tcksrx, tcksre, trrd;
1000 u8 twl, txp, tfaw, tcl;
1001 u16 tras, twr, tmrd, trtp, twtr, trfc, txsr;
1002 u16 trcd_lp, trppb_lp, trpab_lp, trc_lp;
1005 int clkper; /* clock period in picoseconds */
1006 int clock; /* clock freq in mHz */
1009 /* only support 16/32 bits */
1010 if (sysinfo->dsize > 1)
1013 mmdc0 = (struct mmdc_p_regs *)MMDC_P0_BASE_ADDR;
1015 clock = mxc_get_clock(MXC_DDR_CLK) / 1000000U;
1016 clkper = (1000 * 1000) / clock; /* pico seconds */
1018 twl = lpddr2_wl(lpddr2_cfg->mem_speed) - 1;
1020 /* LPDDR2-S2 and LPDDR2-S4 have the same tRFC value. */
1021 switch (lpddr2_cfg->density) {
1025 trfc = DIV_ROUND_UP(130000, clkper) - 1;
1026 txsr = DIV_ROUND_UP(140000, clkper) - 1;
1029 trfc = DIV_ROUND_UP(210000, clkper) - 1;
1030 txsr = DIV_ROUND_UP(220000, clkper) - 1;
1034 * 64Mb, 128Mb, 256Mb, 512Mb are not supported currently.
1040 * txpdll, txpr, taonpd and taofpd are not relevant in LPDDR2 mode,
1042 txp = DIV_ROUND_UP(7500, clkper) - 1;
1044 if (lpddr2_cfg->mem_speed == 333)
1045 tfaw = DIV_ROUND_UP(60000, clkper) - 1;
1047 tfaw = DIV_ROUND_UP(50000, clkper) - 1;
1048 trrd = DIV_ROUND_UP(10000, clkper) - 1;
1050 /* tckesr for LPDDR2 */
1051 tcksre = DIV_ROUND_UP(15000, clkper);
1053 twr = DIV_ROUND_UP(15000, clkper) - 1;
1056 * tMRD should be set to max(tMRR, tMRW)
1059 tras = DIV_ROUND_UP(lpddr2_cfg->trasmin, clkper / 10) - 1;
1060 /* LPDDR2 mode use tRCD_LP filed in MDCFG3. */
1061 trcd_lp = DIV_ROUND_UP(lpddr2_cfg->trcd_lp, clkper / 10) - 1;
1062 trc_lp = DIV_ROUND_UP(lpddr2_cfg->trasmin + lpddr2_cfg->trppb_lp,
1064 trppb_lp = DIV_ROUND_UP(lpddr2_cfg->trppb_lp, clkper / 10) - 1;
1065 trpab_lp = DIV_ROUND_UP(lpddr2_cfg->trpab_lp, clkper / 10) - 1;
1066 /* To LPDDR2, CL in MDCFG0 refers to RL */
1067 tcl = lpddr2_rl(lpddr2_cfg->mem_speed) - 3;
1068 twtr = DIV_ROUND_UP(7500, clkper) - 1;
1069 trtp = DIV_ROUND_UP(7500, clkper) - 1;
1071 cs0_end = 4 * sysinfo->cs_density - 1;
1073 debug("density:%d Gb (%d Gb per chip)\n",
1074 sysinfo->cs_density, lpddr2_cfg->density);
1075 debug("clock: %dMHz (%d ps)\n", clock, clkper);
1076 debug("memspd:%d\n", lpddr2_cfg->mem_speed);
1077 debug("trcd_lp=%d\n", trcd_lp);
1078 debug("trppb_lp=%d\n", trppb_lp);
1079 debug("trpab_lp=%d\n", trpab_lp);
1080 debug("trc_lp=%d\n", trc_lp);
1081 debug("tcke=%d\n", tcke);
1082 debug("tcksrx=%d\n", tcksrx);
1083 debug("tcksre=%d\n", tcksre);
1084 debug("trfc=%d\n", trfc);
1085 debug("txsr=%d\n", txsr);
1086 debug("txp=%d\n", txp);
1087 debug("tfaw=%d\n", tfaw);
1088 debug("tcl=%d\n", tcl);
1089 debug("tras=%d\n", tras);
1090 debug("twr=%d\n", twr);
1091 debug("tmrd=%d\n", tmrd);
1092 debug("twl=%d\n", twl);
1093 debug("trtp=%d\n", trtp);
1094 debug("twtr=%d\n", twtr);
1095 debug("trrd=%d\n", trrd);
1096 debug("cs0_end=%d\n", cs0_end);
1097 debug("ncs=%d\n", sysinfo->ncs);
1100 * board-specific configuration:
1101 * These values are determined empirically and vary per board layout
1103 mmdc0->mpwldectrl0 = calib->p0_mpwldectrl0;
1104 mmdc0->mpwldectrl1 = calib->p0_mpwldectrl1;
1105 mmdc0->mpdgctrl0 = calib->p0_mpdgctrl0;
1106 mmdc0->mpdgctrl1 = calib->p0_mpdgctrl1;
1107 mmdc0->mprddlctl = calib->p0_mprddlctl;
1108 mmdc0->mpwrdlctl = calib->p0_mpwrdlctl;
1109 mmdc0->mpzqlp2ctl = calib->mpzqlp2ctl;
1111 /* Read data DQ Byte0-3 delay */
1112 mmdc0->mprddqby0dl = 0x33333333;
1113 mmdc0->mprddqby1dl = 0x33333333;
1114 if (sysinfo->dsize > 0) {
1115 mmdc0->mprddqby2dl = 0x33333333;
1116 mmdc0->mprddqby3dl = 0x33333333;
1119 /* Write data DQ Byte0-3 delay */
1120 mmdc0->mpwrdqby0dl = 0xf3333333;
1121 mmdc0->mpwrdqby1dl = 0xf3333333;
1122 if (sysinfo->dsize > 0) {
1123 mmdc0->mpwrdqby2dl = 0xf3333333;
1124 mmdc0->mpwrdqby3dl = 0xf3333333;
1128 * In LPDDR2 mode this register should be cleared,
1129 * so no termination will be activated.
1131 mmdc0->mpodtctrl = 0;
1133 /* complete calibration */
1134 val = (1 << 11); /* Force measurement on delay-lines */
1135 mmdc0->mpmur0 = val;
1137 /* Step 1: configuration request */
1138 mmdc0->mdscr = (u32)(1 << 15); /* config request */
1140 /* Step 2: Timing configuration */
1141 mmdc0->mdcfg0 = (trfc << 24) | (txsr << 16) | (txp << 13) |
1143 mmdc0->mdcfg1 = (tras << 16) | (twr << 9) | (tmrd << 5) | twl;
1144 mmdc0->mdcfg2 = (trtp << 6) | (twtr << 3) | trrd;
1145 mmdc0->mdcfg3lp = (trc_lp << 16) | (trcd_lp << 8) |
1146 (trppb_lp << 4) | trpab_lp;
1149 mmdc0->mdasp = cs0_end; /* CS addressing */
1151 /* Step 3: Configure DDR type */
1152 mmdc0->mdmisc = (sysinfo->cs1_mirror << 19) | (sysinfo->walat << 16) |
1153 (sysinfo->bi_on << 12) | (sysinfo->mif3_mode << 9) |
1154 (sysinfo->ralat << 6) | (1 << 3);
1156 /* Step 4: Configure delay while leaving reset */
1157 mmdc0->mdor = (sysinfo->sde_to_rst << 8) |
1158 (sysinfo->rst_to_cke << 0);
1160 /* Step 5: Configure DDR physical parameters (density and burst len) */
1161 coladdr = lpddr2_cfg->coladdr;
1162 if (lpddr2_cfg->coladdr == 8) /* 8-bit COL is 0x3 */
1164 else if (lpddr2_cfg->coladdr == 12) /* 12-bit COL is 0x4 */
1166 mmdc0->mdctl = (lpddr2_cfg->rowaddr - 11) << 24 | /* ROW */
1167 (coladdr - 9) << 20 | /* COL */
1168 (0 << 19) | /* Burst Length = 4 for LPDDR2 */
1169 (sysinfo->dsize << 16); /* DDR data bus size */
1171 /* Step 6: Perform ZQ calibration */
1172 val = 0xa1390003; /* one-time HW ZQ calib */
1173 mmdc0->mpzqhwctrl = val;
1175 /* Step 7: Enable MMDC with desired chip select */
1176 mmdc0->mdctl |= (1 << 31) | /* SDE_0 for CS0 */
1177 ((sysinfo->ncs == 2) ? 1 : 0) << 30; /* SDE_1 for CS1 */
1179 /* Step 8: Write Mode Registers to Init LPDDR2 devices */
1180 for (cs = 0; cs < sysinfo->ncs; cs++) {
1182 mmdc0->mdscr = MR(63, 0, 3, cs);
1183 /* MR10: calibration,
1184 * 0xff is calibration command after intilization.
1186 val = 0xA | (0xff << 8);
1187 mmdc0->mdscr = MR(val, 0, 3, cs);
1189 val = 0x1 | (0x82 << 8);
1190 mmdc0->mdscr = MR(val, 0, 3, cs);
1192 val = 0x2 | (0x04 << 8);
1193 mmdc0->mdscr = MR(val, 0, 3, cs);
1195 val = 0x3 | (0x02 << 8);
1196 mmdc0->mdscr = MR(val, 0, 3, cs);
1199 /* Step 10: Power down control and self-refresh */
1200 mmdc0->mdpdc = (tcke & 0x7) << 16 |
1201 5 << 12 | /* PWDT_1: 256 cycles */
1202 5 << 8 | /* PWDT_0: 256 cycles */
1203 1 << 6 | /* BOTH_CS_PD */
1204 (tcksrx & 0x7) << 3 |
1206 mmdc0->mapsr = 0x00001006; /* ADOPT power down enabled */
1208 /* Step 11: Configure ZQ calibration: one-time and periodic 1ms */
1210 mmdc0->mpzqhwctrl = val;
1212 /* Step 12: Configure and activate periodic refresh */
1213 mmdc0->mdref = (sysinfo->refsel << 14) | (sysinfo->refr << 11);
1215 /* Step 13: Deassert config request - init complete */
1216 mmdc0->mdscr = 0x00000000;
1218 /* wait for auto-ZQ calibration to complete */
1222 void mx6_ddr3_cfg(const struct mx6_ddr_sysinfo *sysinfo,
1223 const struct mx6_mmdc_calibration *calib,
1224 const struct mx6_ddr3_cfg *ddr3_cfg)
1226 volatile struct mmdc_p_regs *mmdc0;
1227 volatile struct mmdc_p_regs *mmdc1;
1229 u8 tcke, tcksrx, tcksre, txpdll, taofpd, taonpd, trrd;
1230 u8 todtlon, taxpd, tanpd, tcwl, txp, tfaw, tcl;
1231 u8 todt_idle_off = 0x4; /* from DDR3 Script Aid spreadsheet */
1232 u16 trcd, trc, tras, twr, tmrd, trtp, trp, twtr, trfc, txs, txpr;
1234 u16 tdllk = 0x1ff; /* DLL locking time: 512 cycles (JEDEC DDR3) */
1236 int clkper; /* clock period in picoseconds */
1237 int clock; /* clock freq in MHz */
1239 u16 mem_speed = ddr3_cfg->mem_speed;
1241 mmdc0 = (struct mmdc_p_regs *)MMDC_P0_BASE_ADDR;
1242 if (!is_mx6sx() && !is_mx6ul() && !is_mx6ull() && !is_mx6sl())
1243 mmdc1 = (struct mmdc_p_regs *)MMDC_P1_BASE_ADDR;
1245 /* Limit mem_speed for MX6D/MX6Q */
1246 if (is_mx6dq() || is_mx6dqp()) {
1247 if (mem_speed > 1066)
1248 mem_speed = 1066; /* 1066 MT/s */
1252 /* Limit mem_speed for MX6S/MX6DL */
1254 if (mem_speed > 800)
1255 mem_speed = 800; /* 800 MT/s */
1260 clock = mem_speed / 2;
1262 * Data rate of 1066 MT/s requires 533 MHz DDR3 clock, but MX6D/Q supports
1263 * up to 528 MHz, so reduce the clock to fit chip specs
1265 if (is_mx6dq() || is_mx6dqp()) {
1267 clock = 528; /* 528 MHz */
1270 clkper = (1000 * 1000) / clock; /* pico seconds */
1275 switch (ddr3_cfg->density) {
1276 case 1: /* 1Gb per chip */
1277 trfc = DIV_ROUND_UP(110000, clkper) - 1;
1278 txs = DIV_ROUND_UP(120000, clkper) - 1;
1280 case 2: /* 2Gb per chip */
1281 trfc = DIV_ROUND_UP(160000, clkper) - 1;
1282 txs = DIV_ROUND_UP(170000, clkper) - 1;
1284 case 4: /* 4Gb per chip */
1285 trfc = DIV_ROUND_UP(260000, clkper) - 1;
1286 txs = DIV_ROUND_UP(270000, clkper) - 1;
1288 case 8: /* 8Gb per chip */
1289 trfc = DIV_ROUND_UP(350000, clkper) - 1;
1290 txs = DIV_ROUND_UP(360000, clkper) - 1;
1293 /* invalid density */
1294 puts("invalid chip density\n");
1300 switch (mem_speed) {
1302 txp = DIV_ROUND_UP(max(3 * clkper, 7500), clkper) - 1;
1303 tcke = DIV_ROUND_UP(max(3 * clkper, 7500), clkper) - 1;
1304 if (ddr3_cfg->pagesz == 1) {
1305 tfaw = DIV_ROUND_UP(40000, clkper) - 1;
1306 trrd = DIV_ROUND_UP(max(4 * clkper, 10000), clkper) - 1;
1308 tfaw = DIV_ROUND_UP(50000, clkper) - 1;
1309 trrd = DIV_ROUND_UP(max(4 * clkper, 10000), clkper) - 1;
1313 txp = DIV_ROUND_UP(max(3 * clkper, 7500), clkper) - 1;
1314 tcke = DIV_ROUND_UP(max(3 * clkper, 5625), clkper) - 1;
1315 if (ddr3_cfg->pagesz == 1) {
1316 tfaw = DIV_ROUND_UP(37500, clkper) - 1;
1317 trrd = DIV_ROUND_UP(max(4 * clkper, 7500), clkper) - 1;
1319 tfaw = DIV_ROUND_UP(50000, clkper) - 1;
1320 trrd = DIV_ROUND_UP(max(4 * clkper, 10000), clkper) - 1;
1324 puts("invalid memory speed\n");
1328 txpdll = DIV_ROUND_UP(max(10 * clkper, 24000), clkper) - 1;
1329 tcksre = DIV_ROUND_UP(max(5 * clkper, 10000), clkper);
1330 taonpd = DIV_ROUND_UP(2000, clkper) - 1;
1333 twr = DIV_ROUND_UP(15000, clkper) - 1;
1334 tmrd = DIV_ROUND_UP(max(12 * clkper, 15000), clkper) - 1;
1335 trc = DIV_ROUND_UP(ddr3_cfg->trcmin, clkper / 10) - 1;
1336 tras = DIV_ROUND_UP(ddr3_cfg->trasmin, clkper / 10) - 1;
1337 tcl = DIV_ROUND_UP(ddr3_cfg->trcd, clkper / 10) - 3;
1338 trp = DIV_ROUND_UP(ddr3_cfg->trcd, clkper / 10) - 1;
1339 twtr = ROUND(max(4 * clkper, 7500) / clkper, 1) - 1;
1342 cs0_end = 4 * sysinfo->cs_density - 1;
1344 debug("density:%d Gb (%d Gb per chip)\n",
1345 sysinfo->cs_density, ddr3_cfg->density);
1346 debug("clock: %dMHz (%d ps)\n", clock, clkper);
1347 debug("memspd:%d\n", mem_speed);
1348 debug("tcke=%d\n", tcke);
1349 debug("tcksrx=%d\n", tcksrx);
1350 debug("tcksre=%d\n", tcksre);
1351 debug("taofpd=%d\n", taofpd);
1352 debug("taonpd=%d\n", taonpd);
1353 debug("todtlon=%d\n", todtlon);
1354 debug("tanpd=%d\n", tanpd);
1355 debug("taxpd=%d\n", taxpd);
1356 debug("trfc=%d\n", trfc);
1357 debug("txs=%d\n", txs);
1358 debug("txp=%d\n", txp);
1359 debug("txpdll=%d\n", txpdll);
1360 debug("tfaw=%d\n", tfaw);
1361 debug("tcl=%d\n", tcl);
1362 debug("trcd=%d\n", trcd);
1363 debug("trp=%d\n", trp);
1364 debug("trc=%d\n", trc);
1365 debug("tras=%d\n", tras);
1366 debug("twr=%d\n", twr);
1367 debug("tmrd=%d\n", tmrd);
1368 debug("tcwl=%d\n", tcwl);
1369 debug("tdllk=%d\n", tdllk);
1370 debug("trtp=%d\n", trtp);
1371 debug("twtr=%d\n", twtr);
1372 debug("trrd=%d\n", trrd);
1373 debug("txpr=%d\n", txpr);
1374 debug("cs0_end=%d\n", cs0_end);
1375 debug("ncs=%d\n", sysinfo->ncs);
1376 debug("Rtt_wr=%d\n", sysinfo->rtt_wr);
1377 debug("Rtt_nom=%d\n", sysinfo->rtt_nom);
1378 debug("SRT=%d\n", ddr3_cfg->SRT);
1379 debug("twr=%d\n", twr);
1382 * board-specific configuration:
1383 * These values are determined empirically and vary per board layout
1385 * appnote, ddr3 spreadsheet
1387 mmdc0->mpwldectrl0 = calib->p0_mpwldectrl0;
1388 mmdc0->mpwldectrl1 = calib->p0_mpwldectrl1;
1389 mmdc0->mpdgctrl0 = calib->p0_mpdgctrl0;
1390 mmdc0->mpdgctrl1 = calib->p0_mpdgctrl1;
1391 mmdc0->mprddlctl = calib->p0_mprddlctl;
1392 mmdc0->mpwrdlctl = calib->p0_mpwrdlctl;
1393 if (sysinfo->dsize > 1) {
1394 MMDC1(mpwldectrl0, calib->p1_mpwldectrl0);
1395 MMDC1(mpwldectrl1, calib->p1_mpwldectrl1);
1396 MMDC1(mpdgctrl0, calib->p1_mpdgctrl0);
1397 MMDC1(mpdgctrl1, calib->p1_mpdgctrl1);
1398 MMDC1(mprddlctl, calib->p1_mprddlctl);
1399 MMDC1(mpwrdlctl, calib->p1_mpwrdlctl);
1402 /* Read data DQ Byte0-3 delay */
1403 mmdc0->mprddqby0dl = 0x33333333;
1404 mmdc0->mprddqby1dl = 0x33333333;
1405 if (sysinfo->dsize > 0) {
1406 mmdc0->mprddqby2dl = 0x33333333;
1407 mmdc0->mprddqby3dl = 0x33333333;
1410 if (sysinfo->dsize > 1) {
1411 MMDC1(mprddqby0dl, 0x33333333);
1412 MMDC1(mprddqby1dl, 0x33333333);
1413 MMDC1(mprddqby2dl, 0x33333333);
1414 MMDC1(mprddqby3dl, 0x33333333);
1417 /* MMDC Termination: rtt_nom:2 RZQ/2(120ohm), rtt_nom:1 RZQ/4(60ohm) */
1418 val = (sysinfo->rtt_nom == 2) ? 0x00011117 : 0x00022227;
1419 mmdc0->mpodtctrl = val;
1420 if (sysinfo->dsize > 1)
1421 MMDC1(mpodtctrl, val);
1423 /* complete calibration */
1424 val = (1 << 11); /* Force measurement on delay-lines */
1425 mmdc0->mpmur0 = val;
1426 if (sysinfo->dsize > 1)
1429 /* Step 1: configuration request */
1430 mmdc0->mdscr = (u32)(1 << 15); /* config request */
1432 /* Step 2: Timing configuration */
1433 mmdc0->mdcfg0 = (trfc << 24) | (txs << 16) | (txp << 13) |
1434 (txpdll << 9) | (tfaw << 4) | tcl;
1435 mmdc0->mdcfg1 = (trcd << 29) | (trp << 26) | (trc << 21) |
1436 (tras << 16) | (1 << 15) /* trpa */ |
1437 (twr << 9) | (tmrd << 5) | tcwl;
1438 mmdc0->mdcfg2 = (tdllk << 16) | (trtp << 6) | (twtr << 3) | trrd;
1439 mmdc0->mdotc = (taofpd << 27) | (taonpd << 24) | (tanpd << 20) |
1440 (taxpd << 16) | (todtlon << 12) | (todt_idle_off << 4);
1441 mmdc0->mdasp = cs0_end; /* CS addressing */
1443 /* Step 3: Configure DDR type */
1444 mmdc0->mdmisc = (sysinfo->cs1_mirror << 19) | (sysinfo->walat << 16) |
1445 (sysinfo->bi_on << 12) | (sysinfo->mif3_mode << 9) |
1446 (sysinfo->ralat << 6);
1448 /* Step 4: Configure delay while leaving reset */
1449 mmdc0->mdor = (txpr << 16) | (sysinfo->sde_to_rst << 8) |
1450 (sysinfo->rst_to_cke << 0);
1452 /* Step 5: Configure DDR physical parameters (density and burst len) */
1453 coladdr = ddr3_cfg->coladdr;
1454 if (ddr3_cfg->coladdr == 8) /* 8-bit COL is 0x3 */
1456 else if (ddr3_cfg->coladdr == 12) /* 12-bit COL is 0x4 */
1458 mmdc0->mdctl = (ddr3_cfg->rowaddr - 11) << 24 | /* ROW */
1459 (coladdr - 9) << 20 | /* COL */
1460 (1 << 19) | /* Burst Length = 8 for DDR3 */
1461 (sysinfo->dsize << 16); /* DDR data bus size */
1463 /* Step 6: Perform ZQ calibration */
1464 val = 0xa1390001; /* one-time HW ZQ calib */
1465 mmdc0->mpzqhwctrl = val;
1466 if (sysinfo->dsize > 1)
1467 MMDC1(mpzqhwctrl, val);
1469 /* Step 7: Enable MMDC with desired chip select */
1470 mmdc0->mdctl |= (1 << 31) | /* SDE_0 for CS0 */
1471 ((sysinfo->ncs == 2) ? 1 : 0) << 30; /* SDE_1 for CS1 */
1473 /* Step 8: Write Mode Registers to Init DDR3 devices */
1474 for (cs = 0; cs < sysinfo->ncs; cs++) {
1476 val = (sysinfo->rtt_wr & 3) << 9 | (ddr3_cfg->SRT & 1) << 7 |
1477 ((tcwl - 3) & 3) << 3;
1478 debug("MR2 CS%d: 0x%08x\n", cs, (u32)MR(val, 2, 3, cs));
1479 mmdc0->mdscr = MR(val, 2, 3, cs);
1481 debug("MR3 CS%d: 0x%08x\n", cs, (u32)MR(0, 3, 3, cs));
1482 mmdc0->mdscr = MR(0, 3, 3, cs);
1484 val = ((sysinfo->rtt_nom & 1) ? 1 : 0) << 2 |
1485 ((sysinfo->rtt_nom & 2) ? 1 : 0) << 6;
1486 debug("MR1 CS%d: 0x%08x\n", cs, (u32)MR(val, 1, 3, cs));
1487 mmdc0->mdscr = MR(val, 1, 3, cs);
1489 val = ((tcl - 1) << 4) | /* CAS */
1490 (1 << 8) | /* DLL Reset */
1491 ((twr - 3) << 9) | /* Write Recovery */
1492 (sysinfo->pd_fast_exit << 12); /* Precharge PD PLL on */
1493 debug("MR0 CS%d: 0x%08x\n", cs, (u32)MR(val, 0, 3, cs));
1494 mmdc0->mdscr = MR(val, 0, 3, cs);
1495 /* ZQ calibration */
1497 mmdc0->mdscr = MR(val, 0, 4, cs);
1500 /* Step 10: Power down control and self-refresh */
1501 mmdc0->mdpdc = (tcke & 0x7) << 16 |
1502 5 << 12 | /* PWDT_1: 256 cycles */
1503 5 << 8 | /* PWDT_0: 256 cycles */
1504 1 << 6 | /* BOTH_CS_PD */
1505 (tcksrx & 0x7) << 3 |
1507 if (!sysinfo->pd_fast_exit)
1508 mmdc0->mdpdc |= (1 << 7); /* SLOW_PD */
1509 mmdc0->mapsr = 0x00001006; /* ADOPT power down enabled */
1511 /* Step 11: Configure ZQ calibration: one-time and periodic 1ms */
1513 mmdc0->mpzqhwctrl = val;
1514 if (sysinfo->dsize > 1)
1515 MMDC1(mpzqhwctrl, val);
1517 /* Step 12: Configure and activate periodic refresh */
1518 mmdc0->mdref = (sysinfo->refsel << 14) | (sysinfo->refr << 11);
1520 /* Step 13: Deassert config request - init complete */
1521 mmdc0->mdscr = 0x00000000;
1523 /* wait for auto-ZQ calibration to complete */
1527 void mmdc_read_calibration(struct mx6_ddr_sysinfo const *sysinfo,
1528 struct mx6_mmdc_calibration *calib)
1530 struct mmdc_p_regs *mmdc0 = (struct mmdc_p_regs *)MMDC_P0_BASE_ADDR;
1531 struct mmdc_p_regs *mmdc1 = (struct mmdc_p_regs *)MMDC_P1_BASE_ADDR;
1533 calib->p0_mpwldectrl0 = readl(&mmdc0->mpwldectrl0);
1534 calib->p0_mpwldectrl1 = readl(&mmdc0->mpwldectrl1);
1535 calib->p0_mpdgctrl0 = readl(&mmdc0->mpdgctrl0);
1536 calib->p0_mpdgctrl1 = readl(&mmdc0->mpdgctrl1);
1537 calib->p0_mprddlctl = readl(&mmdc0->mprddlctl);
1538 calib->p0_mpwrdlctl = readl(&mmdc0->mpwrdlctl);
1540 if (sysinfo->dsize == 2) {
1541 calib->p1_mpwldectrl0 = readl(&mmdc1->mpwldectrl0);
1542 calib->p1_mpwldectrl1 = readl(&mmdc1->mpwldectrl1);
1543 calib->p1_mpdgctrl0 = readl(&mmdc1->mpdgctrl0);
1544 calib->p1_mpdgctrl1 = readl(&mmdc1->mpdgctrl1);
1545 calib->p1_mprddlctl = readl(&mmdc1->mprddlctl);
1546 calib->p1_mpwrdlctl = readl(&mmdc1->mpwrdlctl);
1550 void mx6_dram_cfg(const struct mx6_ddr_sysinfo *sysinfo,
1551 const struct mx6_mmdc_calibration *calib,
1552 const void *ddr_cfg)
1554 if (sysinfo->ddr_type == DDR_TYPE_DDR3) {
1555 mx6_ddr3_cfg(sysinfo, calib, ddr_cfg);
1556 } else if (sysinfo->ddr_type == DDR_TYPE_LPDDR2) {
1557 mx6_lpddr2_cfg(sysinfo, calib, ddr_cfg);
1559 puts("Unsupported ddr type\n");