1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (C) 2014 Gateworks Corporation
4 * Author: Tim Harvey <tharvey@gateworks.com>
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 static void mmdc_set_sdqs(bool set)
251 struct mx6dq_iomux_ddr_regs *mx6dq_ddr_iomux =
252 (struct mx6dq_iomux_ddr_regs *)MX6DQ_IOM_DDR_BASE;
253 struct mx6sx_iomux_ddr_regs *mx6sx_ddr_iomux =
254 (struct mx6sx_iomux_ddr_regs *)MX6SX_IOM_DDR_BASE;
259 sdqs = (u32)(&mx6sx_ddr_iomux->dram_sdqs0);
262 sdqs = (u32)(&mx6dq_ddr_iomux->dram_sdqs0);
266 for (i = 0; i < sdqs_cnt; i++) {
268 setbits_le32(sdqs + (4 * i), 0x7000);
270 clrbits_le32(sdqs + (4 * i), 0x7000);
274 int mmdc_do_dqs_calibration(struct mx6_ddr_sysinfo const *sysinfo)
276 struct mmdc_p_regs *mmdc0 = (struct mmdc_p_regs *)MMDC_P0_BASE_ADDR;
277 struct mmdc_p_regs *mmdc1 = (struct mmdc_p_regs *)MMDC_P1_BASE_ADDR;
280 bool cs0_enable_initial;
281 bool cs1_enable_initial;
284 u32 pddword = 0x00ffff00; /* best so far, place into MPPDCMPR1 */
286 u32 initdelay = 0x40404040;
288 /* check to see which chip selects are enabled */
289 cs0_enable_initial = readl(&mmdc0->mdctl) & 0x80000000;
290 cs1_enable_initial = readl(&mmdc0->mdctl) & 0x40000000;
292 /* disable DDR logic power down timer: */
293 clrbits_le32(&mmdc0->mdpdc, 0xff00);
295 /* disable Adopt power down timer: */
296 setbits_le32(&mmdc0->mapsr, 0x1);
298 /* set DQS pull ups */
301 /* Save old RALAT and WALAT values */
302 esdmisc_val = readl(&mmdc0->mdmisc);
304 setbits_le32(&mmdc0->mdmisc,
305 (1 << 6) | (1 << 7) | (1 << 8) | (1 << 16) | (1 << 17));
307 /* Disable auto refresh before proceeding with calibration */
308 temp_ref = readl(&mmdc0->mdref);
309 writel(0x0000c000, &mmdc0->mdref);
312 * Per the ref manual, issue one refresh cycle MDSCR[CMD]= 0x2,
313 * this also sets the CON_REQ bit.
315 if (cs0_enable_initial)
316 writel(0x00008020, &mmdc0->mdscr);
317 if (cs1_enable_initial)
318 writel(0x00008028, &mmdc0->mdscr);
320 /* poll to make sure the con_ack bit was asserted */
321 wait_for_bit_le32(&mmdc0->mdscr, 1 << 14, 1, 100, 0);
324 * Check MDMISC register CALIB_PER_CS to see which CS calibration
325 * is targeted to (under normal cases, it should be cleared
326 * as this is the default value, indicating calibration is directed
328 * Disable the other chip select not being target for calibration
329 * to avoid any potential issues. This will get re-enabled at end
332 if ((readl(&mmdc0->mdmisc) & 0x00100000) == 0)
333 clrbits_le32(&mmdc0->mdctl, 1 << 30); /* clear SDE_1 */
335 clrbits_le32(&mmdc0->mdctl, 1 << 31); /* clear SDE_0 */
338 * Check to see which chip selects are now enabled for
339 * the remainder of the calibration.
341 cs0_enable = readl(&mmdc0->mdctl) & 0x80000000;
342 cs1_enable = readl(&mmdc0->mdctl) & 0x40000000;
344 precharge_all(cs0_enable, cs1_enable);
346 /* Write the pre-defined value into MPPDCMPR1 */
347 writel(pddword, &mmdc0->mppdcmpr1);
350 * Issue a write access to the external DDR device by setting
351 * the bit SW_DUMMY_WR (bit 0) in the MPSWDAR0 and then poll
352 * this bit until it clears to indicate completion of the write access.
354 setbits_le32(&mmdc0->mpswdar0, 1);
355 wait_for_bit_le32(&mmdc0->mpswdar0, 1 << 0, 0, 100, 0);
357 /* Set the RD_DL_ABS# bits to their default values
358 * (will be calibrated later in the read delay-line calibration).
359 * Both PHYs for x64 configuration, if x32, do only PHY0.
361 writel(initdelay, &mmdc0->mprddlctl);
362 if (sysinfo->dsize == 0x2)
363 writel(initdelay, &mmdc1->mprddlctl);
365 /* Force a measurment, for previous delay setup to take effect. */
366 force_delay_measurement(sysinfo->dsize);
369 * ***************************
370 * Read DQS Gating calibration
371 * ***************************
373 debug("Starting Read DQS Gating calibration.\n");
376 * Reset the read data FIFOs (two resets); only need to issue reset
377 * to PHY0 since in x64 mode, the reset will also go to PHY1.
379 reset_read_data_fifos();
382 * Start the automatic read DQS gating calibration process by
383 * asserting MPDGCTRL0[HW_DG_EN] and MPDGCTRL0[DG_CMP_CYC]
384 * and then poll MPDGCTRL0[HW_DG_EN]] until this bit clears
385 * to indicate completion.
386 * Also, ensure that MPDGCTRL0[HW_DG_ERR] is clear to indicate
387 * no errors were seen during calibration.
391 * Set bit 30: chooses option to wait 32 cycles instead of
392 * 16 before comparing read data.
394 setbits_le32(&mmdc0->mpdgctrl0, 1 << 30);
395 if (sysinfo->dsize == 2)
396 setbits_le32(&mmdc1->mpdgctrl0, 1 << 30);
398 /* Set bit 28 to start automatic read DQS gating calibration */
399 setbits_le32(&mmdc0->mpdgctrl0, 5 << 28);
401 /* Poll for completion. MPDGCTRL0[HW_DG_EN] should be 0 */
402 wait_for_bit_le32(&mmdc0->mpdgctrl0, 1 << 28, 0, 100, 0);
405 * Check to see if any errors were encountered during calibration
406 * (check MPDGCTRL0[HW_DG_ERR]).
407 * Check both PHYs for x64 configuration, if x32, check only PHY0.
409 if (readl(&mmdc0->mpdgctrl0) & 0x00001000)
412 if ((sysinfo->dsize == 0x2) && (readl(&mmdc1->mpdgctrl0) & 0x00001000))
415 /* now disable mpdgctrl0[DG_CMP_CYC] */
416 clrbits_le32(&mmdc0->mpdgctrl0, 1 << 30);
417 if (sysinfo->dsize == 2)
418 clrbits_le32(&mmdc1->mpdgctrl0, 1 << 30);
421 * DQS gating absolute offset should be modified from
422 * reflecting (HW_DG_LOWx + HW_DG_UPx)/2 to
423 * reflecting (HW_DG_UPx - 0x80)
425 modify_dg_result(&mmdc0->mpdghwst0, &mmdc0->mpdghwst1,
427 modify_dg_result(&mmdc0->mpdghwst2, &mmdc0->mpdghwst3,
429 if (sysinfo->dsize == 0x2) {
430 modify_dg_result(&mmdc1->mpdghwst0, &mmdc1->mpdghwst1,
432 modify_dg_result(&mmdc1->mpdghwst2, &mmdc1->mpdghwst3,
435 debug("Ending Read DQS Gating calibration. Error mask: 0x%x\n", errors);
438 * **********************
439 * Read Delay calibration
440 * **********************
442 debug("Starting Read Delay calibration.\n");
444 reset_read_data_fifos();
447 * 4. Issue the Precharge-All command to the DDR device for both
448 * chip selects. If only using one chip select, then precharge
449 * only the desired chip select.
451 precharge_all(cs0_enable, cs1_enable);
454 * 9. Read delay-line calibration
455 * Start the automatic read calibration process by asserting
456 * MPRDDLHWCTL[HW_RD_DL_EN].
458 writel(0x00000030, &mmdc0->mprddlhwctl);
461 * 10. poll for completion
462 * MMDC indicates that the write data calibration had finished by
463 * setting MPRDDLHWCTL[HW_RD_DL_EN] = 0. Also, ensure that
464 * no error bits were set.
466 wait_for_bit_le32(&mmdc0->mprddlhwctl, 1 << 4, 0, 100, 0);
468 /* check both PHYs for x64 configuration, if x32, check only PHY0 */
469 if (readl(&mmdc0->mprddlhwctl) & 0x0000000f)
472 if ((sysinfo->dsize == 0x2) &&
473 (readl(&mmdc1->mprddlhwctl) & 0x0000000f))
476 debug("Ending Read Delay calibration. Error mask: 0x%x\n", errors);
479 * ***********************
480 * Write Delay Calibration
481 * ***********************
483 debug("Starting Write Delay calibration.\n");
485 reset_read_data_fifos();
488 * 4. Issue the Precharge-All command to the DDR device for both
489 * chip selects. If only using one chip select, then precharge
490 * only the desired chip select.
492 precharge_all(cs0_enable, cs1_enable);
495 * 8. Set the WR_DL_ABS# bits to their default values.
496 * Both PHYs for x64 configuration, if x32, do only PHY0.
498 writel(initdelay, &mmdc0->mpwrdlctl);
499 if (sysinfo->dsize == 0x2)
500 writel(initdelay, &mmdc1->mpwrdlctl);
503 * XXX This isn't in the manual. Force a measurement,
504 * for previous delay setup to effect.
506 force_delay_measurement(sysinfo->dsize);
509 * 9. 10. Start the automatic write calibration process
510 * by asserting MPWRDLHWCTL0[HW_WR_DL_EN].
512 writel(0x00000030, &mmdc0->mpwrdlhwctl);
515 * Poll for completion.
516 * MMDC indicates that the write data calibration had finished
517 * by setting MPWRDLHWCTL[HW_WR_DL_EN] = 0.
518 * Also, ensure that no error bits were set.
520 wait_for_bit_le32(&mmdc0->mpwrdlhwctl, 1 << 4, 0, 100, 0);
522 /* Check both PHYs for x64 configuration, if x32, check only PHY0 */
523 if (readl(&mmdc0->mpwrdlhwctl) & 0x0000000f)
526 if ((sysinfo->dsize == 0x2) &&
527 (readl(&mmdc1->mpwrdlhwctl) & 0x0000000f))
530 debug("Ending Write Delay calibration. Error mask: 0x%x\n", errors);
532 reset_read_data_fifos();
534 /* Enable DDR logic power down timer */
535 setbits_le32(&mmdc0->mdpdc, 0x00005500);
537 /* Enable Adopt power down timer */
538 clrbits_le32(&mmdc0->mapsr, 0x1);
540 /* Restore MDMISC value (RALAT, WALAT) to MMDCP1 */
541 writel(esdmisc_val, &mmdc0->mdmisc);
543 /* Clear DQS pull ups */
544 mmdc_set_sdqs(false);
546 /* Re-enable SDE (chip selects) if they were set initially */
547 if (cs1_enable_initial)
549 setbits_le32(&mmdc0->mdctl, 1 << 30);
551 if (cs0_enable_initial)
553 setbits_le32(&mmdc0->mdctl, 1 << 31);
555 /* Re-enable to auto refresh */
556 writel(temp_ref, &mmdc0->mdref);
558 /* Clear the MDSCR (including the con_req bit) */
559 writel(0x0, &mmdc0->mdscr); /* CS0 */
561 /* Poll to make sure the con_ack bit is clear */
562 wait_for_bit_le32(&mmdc0->mdscr, 1 << 14, 0, 100, 0);
565 * Print out the registers that were updated as a result
566 * of the calibration process.
568 debug("MMDC registers updated from calibration\n");
569 debug("Read DQS gating calibration:\n");
570 debug("\tMPDGCTRL0 PHY0 = 0x%08x\n", readl(&mmdc0->mpdgctrl0));
571 debug("\tMPDGCTRL1 PHY0 = 0x%08x\n", readl(&mmdc0->mpdgctrl1));
572 if (sysinfo->dsize == 2) {
573 debug("\tMPDGCTRL0 PHY1 = 0x%08x\n", readl(&mmdc1->mpdgctrl0));
574 debug("\tMPDGCTRL1 PHY1 = 0x%08x\n", readl(&mmdc1->mpdgctrl1));
576 debug("Read calibration:\n");
577 debug("\tMPRDDLCTL PHY0 = 0x%08x\n", readl(&mmdc0->mprddlctl));
578 if (sysinfo->dsize == 2)
579 debug("\tMPRDDLCTL PHY1 = 0x%08x\n", readl(&mmdc1->mprddlctl));
580 debug("Write calibration:\n");
581 debug("\tMPWRDLCTL PHY0 = 0x%08x\n", readl(&mmdc0->mpwrdlctl));
582 if (sysinfo->dsize == 2)
583 debug("\tMPWRDLCTL PHY1 = 0x%08x\n", readl(&mmdc1->mpwrdlctl));
586 * Registers below are for debugging purposes. These print out
587 * the upper and lower boundaries captured during
588 * read DQS gating calibration.
590 debug("Status registers bounds for read DQS gating:\n");
591 debug("\tMPDGHWST0 PHY0 = 0x%08x\n", readl(&mmdc0->mpdghwst0));
592 debug("\tMPDGHWST1 PHY0 = 0x%08x\n", readl(&mmdc0->mpdghwst1));
593 debug("\tMPDGHWST2 PHY0 = 0x%08x\n", readl(&mmdc0->mpdghwst2));
594 debug("\tMPDGHWST3 PHY0 = 0x%08x\n", readl(&mmdc0->mpdghwst3));
595 if (sysinfo->dsize == 2) {
596 debug("\tMPDGHWST0 PHY1 = 0x%08x\n", readl(&mmdc1->mpdghwst0));
597 debug("\tMPDGHWST1 PHY1 = 0x%08x\n", readl(&mmdc1->mpdghwst1));
598 debug("\tMPDGHWST2 PHY1 = 0x%08x\n", readl(&mmdc1->mpdghwst2));
599 debug("\tMPDGHWST3 PHY1 = 0x%08x\n", readl(&mmdc1->mpdghwst3));
602 debug("Final do_dqs_calibration error mask: 0x%x\n", errors);
608 #if defined(CONFIG_MX6SX)
609 /* Configure MX6SX mmdc iomux */
610 void mx6sx_dram_iocfg(unsigned width,
611 const struct mx6sx_iomux_ddr_regs *ddr,
612 const struct mx6sx_iomux_grp_regs *grp)
614 struct mx6sx_iomux_ddr_regs *mx6_ddr_iomux;
615 struct mx6sx_iomux_grp_regs *mx6_grp_iomux;
617 mx6_ddr_iomux = (struct mx6sx_iomux_ddr_regs *)MX6SX_IOM_DDR_BASE;
618 mx6_grp_iomux = (struct mx6sx_iomux_grp_regs *)MX6SX_IOM_GRP_BASE;
621 writel(grp->grp_ddr_type, &mx6_grp_iomux->grp_ddr_type);
622 writel(grp->grp_ddrpke, &mx6_grp_iomux->grp_ddrpke);
625 writel(ddr->dram_sdclk_0, &mx6_ddr_iomux->dram_sdclk_0);
628 writel(ddr->dram_cas, &mx6_ddr_iomux->dram_cas);
629 writel(ddr->dram_ras, &mx6_ddr_iomux->dram_ras);
630 writel(grp->grp_addds, &mx6_grp_iomux->grp_addds);
633 writel(ddr->dram_reset, &mx6_ddr_iomux->dram_reset);
634 writel(ddr->dram_sdba2, &mx6_ddr_iomux->dram_sdba2);
635 writel(ddr->dram_sdcke0, &mx6_ddr_iomux->dram_sdcke0);
636 writel(ddr->dram_sdcke1, &mx6_ddr_iomux->dram_sdcke1);
637 writel(ddr->dram_odt0, &mx6_ddr_iomux->dram_odt0);
638 writel(ddr->dram_odt1, &mx6_ddr_iomux->dram_odt1);
639 writel(grp->grp_ctlds, &mx6_grp_iomux->grp_ctlds);
642 writel(grp->grp_ddrmode_ctl, &mx6_grp_iomux->grp_ddrmode_ctl);
643 writel(ddr->dram_sdqs0, &mx6_ddr_iomux->dram_sdqs0);
644 writel(ddr->dram_sdqs1, &mx6_ddr_iomux->dram_sdqs1);
646 writel(ddr->dram_sdqs2, &mx6_ddr_iomux->dram_sdqs2);
647 writel(ddr->dram_sdqs3, &mx6_ddr_iomux->dram_sdqs3);
651 writel(grp->grp_ddrmode, &mx6_grp_iomux->grp_ddrmode);
652 writel(grp->grp_b0ds, &mx6_grp_iomux->grp_b0ds);
653 writel(grp->grp_b1ds, &mx6_grp_iomux->grp_b1ds);
655 writel(grp->grp_b2ds, &mx6_grp_iomux->grp_b2ds);
656 writel(grp->grp_b3ds, &mx6_grp_iomux->grp_b3ds);
658 writel(ddr->dram_dqm0, &mx6_ddr_iomux->dram_dqm0);
659 writel(ddr->dram_dqm1, &mx6_ddr_iomux->dram_dqm1);
661 writel(ddr->dram_dqm2, &mx6_ddr_iomux->dram_dqm2);
662 writel(ddr->dram_dqm3, &mx6_ddr_iomux->dram_dqm3);
667 #if defined(CONFIG_MX6UL) || defined(CONFIG_MX6ULL)
668 void mx6ul_dram_iocfg(unsigned width,
669 const struct mx6ul_iomux_ddr_regs *ddr,
670 const struct mx6ul_iomux_grp_regs *grp)
672 struct mx6ul_iomux_ddr_regs *mx6_ddr_iomux;
673 struct mx6ul_iomux_grp_regs *mx6_grp_iomux;
675 mx6_ddr_iomux = (struct mx6ul_iomux_ddr_regs *)MX6UL_IOM_DDR_BASE;
676 mx6_grp_iomux = (struct mx6ul_iomux_grp_regs *)MX6UL_IOM_GRP_BASE;
679 writel(grp->grp_ddr_type, &mx6_grp_iomux->grp_ddr_type);
680 writel(grp->grp_ddrpke, &mx6_grp_iomux->grp_ddrpke);
683 writel(ddr->dram_sdclk_0, &mx6_ddr_iomux->dram_sdclk_0);
686 writel(ddr->dram_cas, &mx6_ddr_iomux->dram_cas);
687 writel(ddr->dram_ras, &mx6_ddr_iomux->dram_ras);
688 writel(grp->grp_addds, &mx6_grp_iomux->grp_addds);
691 writel(ddr->dram_reset, &mx6_ddr_iomux->dram_reset);
692 writel(ddr->dram_sdba2, &mx6_ddr_iomux->dram_sdba2);
693 writel(ddr->dram_odt0, &mx6_ddr_iomux->dram_odt0);
694 writel(ddr->dram_odt1, &mx6_ddr_iomux->dram_odt1);
695 writel(grp->grp_ctlds, &mx6_grp_iomux->grp_ctlds);
698 writel(grp->grp_ddrmode_ctl, &mx6_grp_iomux->grp_ddrmode_ctl);
699 writel(ddr->dram_sdqs0, &mx6_ddr_iomux->dram_sdqs0);
700 writel(ddr->dram_sdqs1, &mx6_ddr_iomux->dram_sdqs1);
703 writel(grp->grp_ddrmode, &mx6_grp_iomux->grp_ddrmode);
704 writel(grp->grp_b0ds, &mx6_grp_iomux->grp_b0ds);
705 writel(grp->grp_b1ds, &mx6_grp_iomux->grp_b1ds);
706 writel(ddr->dram_dqm0, &mx6_ddr_iomux->dram_dqm0);
707 writel(ddr->dram_dqm1, &mx6_ddr_iomux->dram_dqm1);
711 #if defined(CONFIG_MX6SL)
712 void mx6sl_dram_iocfg(unsigned width,
713 const struct mx6sl_iomux_ddr_regs *ddr,
714 const struct mx6sl_iomux_grp_regs *grp)
716 struct mx6sl_iomux_ddr_regs *mx6_ddr_iomux;
717 struct mx6sl_iomux_grp_regs *mx6_grp_iomux;
719 mx6_ddr_iomux = (struct mx6sl_iomux_ddr_regs *)MX6SL_IOM_DDR_BASE;
720 mx6_grp_iomux = (struct mx6sl_iomux_grp_regs *)MX6SL_IOM_GRP_BASE;
723 mx6_grp_iomux->grp_ddr_type = grp->grp_ddr_type;
724 mx6_grp_iomux->grp_ddrpke = grp->grp_ddrpke;
727 mx6_ddr_iomux->dram_sdclk_0 = ddr->dram_sdclk_0;
730 mx6_ddr_iomux->dram_cas = ddr->dram_cas;
731 mx6_ddr_iomux->dram_ras = ddr->dram_ras;
732 mx6_grp_iomux->grp_addds = grp->grp_addds;
735 mx6_ddr_iomux->dram_reset = ddr->dram_reset;
736 mx6_ddr_iomux->dram_sdba2 = ddr->dram_sdba2;
737 mx6_grp_iomux->grp_ctlds = grp->grp_ctlds;
740 mx6_grp_iomux->grp_ddrmode_ctl = grp->grp_ddrmode_ctl;
741 mx6_ddr_iomux->dram_sdqs0 = ddr->dram_sdqs0;
742 mx6_ddr_iomux->dram_sdqs1 = ddr->dram_sdqs1;
744 mx6_ddr_iomux->dram_sdqs2 = ddr->dram_sdqs2;
745 mx6_ddr_iomux->dram_sdqs3 = ddr->dram_sdqs3;
749 mx6_grp_iomux->grp_ddrmode = grp->grp_ddrmode;
750 mx6_grp_iomux->grp_b0ds = grp->grp_b0ds;
751 mx6_grp_iomux->grp_b1ds = grp->grp_b1ds;
753 mx6_grp_iomux->grp_b2ds = grp->grp_b2ds;
754 mx6_grp_iomux->grp_b3ds = grp->grp_b3ds;
757 mx6_ddr_iomux->dram_dqm0 = ddr->dram_dqm0;
758 mx6_ddr_iomux->dram_dqm1 = ddr->dram_dqm1;
760 mx6_ddr_iomux->dram_dqm2 = ddr->dram_dqm2;
761 mx6_ddr_iomux->dram_dqm3 = ddr->dram_dqm3;
766 #if defined(CONFIG_MX6QDL) || defined(CONFIG_MX6Q) || defined(CONFIG_MX6D)
767 /* Configure MX6DQ mmdc iomux */
768 void mx6dq_dram_iocfg(unsigned width,
769 const struct mx6dq_iomux_ddr_regs *ddr,
770 const struct mx6dq_iomux_grp_regs *grp)
772 volatile struct mx6dq_iomux_ddr_regs *mx6_ddr_iomux;
773 volatile struct mx6dq_iomux_grp_regs *mx6_grp_iomux;
775 mx6_ddr_iomux = (struct mx6dq_iomux_ddr_regs *)MX6DQ_IOM_DDR_BASE;
776 mx6_grp_iomux = (struct mx6dq_iomux_grp_regs *)MX6DQ_IOM_GRP_BASE;
779 mx6_grp_iomux->grp_ddr_type = grp->grp_ddr_type;
780 mx6_grp_iomux->grp_ddrpke = grp->grp_ddrpke;
783 mx6_ddr_iomux->dram_sdclk_0 = ddr->dram_sdclk_0;
784 mx6_ddr_iomux->dram_sdclk_1 = ddr->dram_sdclk_1;
787 mx6_ddr_iomux->dram_cas = ddr->dram_cas;
788 mx6_ddr_iomux->dram_ras = ddr->dram_ras;
789 mx6_grp_iomux->grp_addds = grp->grp_addds;
792 mx6_ddr_iomux->dram_reset = ddr->dram_reset;
793 mx6_ddr_iomux->dram_sdcke0 = ddr->dram_sdcke0;
794 mx6_ddr_iomux->dram_sdcke1 = ddr->dram_sdcke1;
795 mx6_ddr_iomux->dram_sdba2 = ddr->dram_sdba2;
796 mx6_ddr_iomux->dram_sdodt0 = ddr->dram_sdodt0;
797 mx6_ddr_iomux->dram_sdodt1 = ddr->dram_sdodt1;
798 mx6_grp_iomux->grp_ctlds = grp->grp_ctlds;
801 mx6_grp_iomux->grp_ddrmode_ctl = grp->grp_ddrmode_ctl;
802 mx6_ddr_iomux->dram_sdqs0 = ddr->dram_sdqs0;
803 mx6_ddr_iomux->dram_sdqs1 = ddr->dram_sdqs1;
805 mx6_ddr_iomux->dram_sdqs2 = ddr->dram_sdqs2;
806 mx6_ddr_iomux->dram_sdqs3 = ddr->dram_sdqs3;
809 mx6_ddr_iomux->dram_sdqs4 = ddr->dram_sdqs4;
810 mx6_ddr_iomux->dram_sdqs5 = ddr->dram_sdqs5;
811 mx6_ddr_iomux->dram_sdqs6 = ddr->dram_sdqs6;
812 mx6_ddr_iomux->dram_sdqs7 = ddr->dram_sdqs7;
816 mx6_grp_iomux->grp_ddrmode = grp->grp_ddrmode;
817 mx6_grp_iomux->grp_b0ds = grp->grp_b0ds;
818 mx6_grp_iomux->grp_b1ds = grp->grp_b1ds;
820 mx6_grp_iomux->grp_b2ds = grp->grp_b2ds;
821 mx6_grp_iomux->grp_b3ds = grp->grp_b3ds;
824 mx6_grp_iomux->grp_b4ds = grp->grp_b4ds;
825 mx6_grp_iomux->grp_b5ds = grp->grp_b5ds;
826 mx6_grp_iomux->grp_b6ds = grp->grp_b6ds;
827 mx6_grp_iomux->grp_b7ds = grp->grp_b7ds;
829 mx6_ddr_iomux->dram_dqm0 = ddr->dram_dqm0;
830 mx6_ddr_iomux->dram_dqm1 = ddr->dram_dqm1;
832 mx6_ddr_iomux->dram_dqm2 = ddr->dram_dqm2;
833 mx6_ddr_iomux->dram_dqm3 = ddr->dram_dqm3;
836 mx6_ddr_iomux->dram_dqm4 = ddr->dram_dqm4;
837 mx6_ddr_iomux->dram_dqm5 = ddr->dram_dqm5;
838 mx6_ddr_iomux->dram_dqm6 = ddr->dram_dqm6;
839 mx6_ddr_iomux->dram_dqm7 = ddr->dram_dqm7;
844 #if defined(CONFIG_MX6QDL) || defined(CONFIG_MX6DL) || defined(CONFIG_MX6S)
845 /* Configure MX6SDL mmdc iomux */
846 void mx6sdl_dram_iocfg(unsigned width,
847 const struct mx6sdl_iomux_ddr_regs *ddr,
848 const struct mx6sdl_iomux_grp_regs *grp)
850 volatile struct mx6sdl_iomux_ddr_regs *mx6_ddr_iomux;
851 volatile struct mx6sdl_iomux_grp_regs *mx6_grp_iomux;
853 mx6_ddr_iomux = (struct mx6sdl_iomux_ddr_regs *)MX6SDL_IOM_DDR_BASE;
854 mx6_grp_iomux = (struct mx6sdl_iomux_grp_regs *)MX6SDL_IOM_GRP_BASE;
857 mx6_grp_iomux->grp_ddr_type = grp->grp_ddr_type;
858 mx6_grp_iomux->grp_ddrpke = grp->grp_ddrpke;
861 mx6_ddr_iomux->dram_sdclk_0 = ddr->dram_sdclk_0;
862 mx6_ddr_iomux->dram_sdclk_1 = ddr->dram_sdclk_1;
865 mx6_ddr_iomux->dram_cas = ddr->dram_cas;
866 mx6_ddr_iomux->dram_ras = ddr->dram_ras;
867 mx6_grp_iomux->grp_addds = grp->grp_addds;
870 mx6_ddr_iomux->dram_reset = ddr->dram_reset;
871 mx6_ddr_iomux->dram_sdcke0 = ddr->dram_sdcke0;
872 mx6_ddr_iomux->dram_sdcke1 = ddr->dram_sdcke1;
873 mx6_ddr_iomux->dram_sdba2 = ddr->dram_sdba2;
874 mx6_ddr_iomux->dram_sdodt0 = ddr->dram_sdodt0;
875 mx6_ddr_iomux->dram_sdodt1 = ddr->dram_sdodt1;
876 mx6_grp_iomux->grp_ctlds = grp->grp_ctlds;
879 mx6_grp_iomux->grp_ddrmode_ctl = grp->grp_ddrmode_ctl;
880 mx6_ddr_iomux->dram_sdqs0 = ddr->dram_sdqs0;
881 mx6_ddr_iomux->dram_sdqs1 = ddr->dram_sdqs1;
883 mx6_ddr_iomux->dram_sdqs2 = ddr->dram_sdqs2;
884 mx6_ddr_iomux->dram_sdqs3 = ddr->dram_sdqs3;
887 mx6_ddr_iomux->dram_sdqs4 = ddr->dram_sdqs4;
888 mx6_ddr_iomux->dram_sdqs5 = ddr->dram_sdqs5;
889 mx6_ddr_iomux->dram_sdqs6 = ddr->dram_sdqs6;
890 mx6_ddr_iomux->dram_sdqs7 = ddr->dram_sdqs7;
894 mx6_grp_iomux->grp_ddrmode = grp->grp_ddrmode;
895 mx6_grp_iomux->grp_b0ds = grp->grp_b0ds;
896 mx6_grp_iomux->grp_b1ds = grp->grp_b1ds;
898 mx6_grp_iomux->grp_b2ds = grp->grp_b2ds;
899 mx6_grp_iomux->grp_b3ds = grp->grp_b3ds;
902 mx6_grp_iomux->grp_b4ds = grp->grp_b4ds;
903 mx6_grp_iomux->grp_b5ds = grp->grp_b5ds;
904 mx6_grp_iomux->grp_b6ds = grp->grp_b6ds;
905 mx6_grp_iomux->grp_b7ds = grp->grp_b7ds;
907 mx6_ddr_iomux->dram_dqm0 = ddr->dram_dqm0;
908 mx6_ddr_iomux->dram_dqm1 = ddr->dram_dqm1;
910 mx6_ddr_iomux->dram_dqm2 = ddr->dram_dqm2;
911 mx6_ddr_iomux->dram_dqm3 = ddr->dram_dqm3;
914 mx6_ddr_iomux->dram_dqm4 = ddr->dram_dqm4;
915 mx6_ddr_iomux->dram_dqm5 = ddr->dram_dqm5;
916 mx6_ddr_iomux->dram_dqm6 = ddr->dram_dqm6;
917 mx6_ddr_iomux->dram_dqm7 = ddr->dram_dqm7;
923 * Configure mx6 mmdc registers based on:
924 * - board-specific memory configuration
925 * - board-specific calibration data
926 * - ddr3/lpddr2 chip details
928 * The various calculations here are derived from the Freescale
929 * 1. i.Mx6DQSDL DDR3 Script Aid spreadsheet (DOC-94917) designed to generate
930 * MMDC configuration registers based on memory system and memory chip
933 * 2. i.Mx6SL LPDDR2 Script Aid spreadsheet V0.04 designed to generate MMDC
934 * configuration registers based on memory system and memory chip
937 * The defaults here are those which were specified in the spreadsheet.
938 * For details on each register, refer to the IMX6DQRM and/or IMX6SDLRM
939 * and/or IMX6SLRM section titled MMDC initialization.
941 #define MR(val, ba, cmd, cs1) \
942 ((val << 16) | (1 << 15) | (cmd << 4) | (cs1 << 3) | ba)
943 #define MMDC1(entry, value) do { \
944 if (!is_mx6sx() && !is_mx6ul() && !is_mx6ull() && !is_mx6sl()) \
945 mmdc1->entry = value; \
948 /* see BOOT_CFG3 description Table 5-4. EIM Boot Fusemap */
949 #define BOOT_CFG3_DDR_MASK 0x30
950 #define BOOT_CFG3_EXT_DDR_MASK 0x33
952 #define DDR_MMAP_NOC_SINGLE 0
953 #define DDR_MMAP_NOC_DUAL 0x31
955 /* NoC ACTIVATE shifts */
956 #define NOC_RD_SHIFT 0
957 #define NOC_FAW_PERIOD_SHIFT 4
958 #define NOC_FAW_BANKS_SHIFT 10
960 /* NoC DdrTiming shifts */
961 #define NOC_ACT_TO_ACT_SHIFT 0
962 #define NOC_RD_TO_MISS_SHIFT 6
963 #define NOC_WR_TO_MISS_SHIFT 12
964 #define NOC_BURST_LEN_SHIFT 18
965 #define NOC_RD_TO_WR_SHIFT 21
966 #define NOC_WR_TO_RD_SHIFT 26
967 #define NOC_BW_RATIO_SHIFT 31
970 * According JESD209-2B-LPDDR2: Table 103
973 static int lpddr2_wl(uint32_t mem_speed)
988 puts("invalid memory speed\n");
996 * According JESD209-2B-LPDDR2: Table 103
999 static int lpddr2_rl(uint32_t mem_speed)
1001 switch (mem_speed) {
1016 puts("invalid memory speed\n");
1023 void mx6_lpddr2_cfg(const struct mx6_ddr_sysinfo *sysinfo,
1024 const struct mx6_mmdc_calibration *calib,
1025 const struct mx6_lpddr2_cfg *lpddr2_cfg)
1027 volatile struct mmdc_p_regs *mmdc0;
1029 u8 tcke, tcksrx, tcksre, trrd;
1030 u8 twl, txp, tfaw, tcl;
1031 u16 tras, twr, tmrd, trtp, twtr, trfc, txsr;
1032 u16 trcd_lp, trppb_lp, trpab_lp, trc_lp;
1035 int clkper; /* clock period in picoseconds */
1036 int clock; /* clock freq in mHz */
1039 /* only support 16/32 bits */
1040 if (sysinfo->dsize > 1)
1043 mmdc0 = (struct mmdc_p_regs *)MMDC_P0_BASE_ADDR;
1045 clock = mxc_get_clock(MXC_DDR_CLK) / 1000000U;
1046 clkper = (1000 * 1000) / clock; /* pico seconds */
1048 twl = lpddr2_wl(lpddr2_cfg->mem_speed) - 1;
1050 /* LPDDR2-S2 and LPDDR2-S4 have the same tRFC value. */
1051 switch (lpddr2_cfg->density) {
1055 trfc = DIV_ROUND_UP(130000, clkper) - 1;
1056 txsr = DIV_ROUND_UP(140000, clkper) - 1;
1059 trfc = DIV_ROUND_UP(210000, clkper) - 1;
1060 txsr = DIV_ROUND_UP(220000, clkper) - 1;
1064 * 64Mb, 128Mb, 256Mb, 512Mb are not supported currently.
1070 * txpdll, txpr, taonpd and taofpd are not relevant in LPDDR2 mode,
1072 txp = DIV_ROUND_UP(7500, clkper) - 1;
1074 if (lpddr2_cfg->mem_speed == 333)
1075 tfaw = DIV_ROUND_UP(60000, clkper) - 1;
1077 tfaw = DIV_ROUND_UP(50000, clkper) - 1;
1078 trrd = DIV_ROUND_UP(10000, clkper) - 1;
1080 /* tckesr for LPDDR2 */
1081 tcksre = DIV_ROUND_UP(15000, clkper);
1083 twr = DIV_ROUND_UP(15000, clkper) - 1;
1086 * tMRD should be set to max(tMRR, tMRW)
1089 tras = DIV_ROUND_UP(lpddr2_cfg->trasmin, clkper / 10) - 1;
1090 /* LPDDR2 mode use tRCD_LP filed in MDCFG3. */
1091 trcd_lp = DIV_ROUND_UP(lpddr2_cfg->trcd_lp, clkper / 10) - 1;
1092 trc_lp = DIV_ROUND_UP(lpddr2_cfg->trasmin + lpddr2_cfg->trppb_lp,
1094 trppb_lp = DIV_ROUND_UP(lpddr2_cfg->trppb_lp, clkper / 10) - 1;
1095 trpab_lp = DIV_ROUND_UP(lpddr2_cfg->trpab_lp, clkper / 10) - 1;
1096 /* To LPDDR2, CL in MDCFG0 refers to RL */
1097 tcl = lpddr2_rl(lpddr2_cfg->mem_speed) - 3;
1098 twtr = DIV_ROUND_UP(7500, clkper) - 1;
1099 trtp = DIV_ROUND_UP(7500, clkper) - 1;
1101 cs0_end = 4 * sysinfo->cs_density - 1;
1103 debug("density:%d Gb (%d Gb per chip)\n",
1104 sysinfo->cs_density, lpddr2_cfg->density);
1105 debug("clock: %dMHz (%d ps)\n", clock, clkper);
1106 debug("memspd:%d\n", lpddr2_cfg->mem_speed);
1107 debug("trcd_lp=%d\n", trcd_lp);
1108 debug("trppb_lp=%d\n", trppb_lp);
1109 debug("trpab_lp=%d\n", trpab_lp);
1110 debug("trc_lp=%d\n", trc_lp);
1111 debug("tcke=%d\n", tcke);
1112 debug("tcksrx=%d\n", tcksrx);
1113 debug("tcksre=%d\n", tcksre);
1114 debug("trfc=%d\n", trfc);
1115 debug("txsr=%d\n", txsr);
1116 debug("txp=%d\n", txp);
1117 debug("tfaw=%d\n", tfaw);
1118 debug("tcl=%d\n", tcl);
1119 debug("tras=%d\n", tras);
1120 debug("twr=%d\n", twr);
1121 debug("tmrd=%d\n", tmrd);
1122 debug("twl=%d\n", twl);
1123 debug("trtp=%d\n", trtp);
1124 debug("twtr=%d\n", twtr);
1125 debug("trrd=%d\n", trrd);
1126 debug("cs0_end=%d\n", cs0_end);
1127 debug("ncs=%d\n", sysinfo->ncs);
1130 * board-specific configuration:
1131 * These values are determined empirically and vary per board layout
1133 mmdc0->mpwldectrl0 = calib->p0_mpwldectrl0;
1134 mmdc0->mpwldectrl1 = calib->p0_mpwldectrl1;
1135 mmdc0->mpdgctrl0 = calib->p0_mpdgctrl0;
1136 mmdc0->mpdgctrl1 = calib->p0_mpdgctrl1;
1137 mmdc0->mprddlctl = calib->p0_mprddlctl;
1138 mmdc0->mpwrdlctl = calib->p0_mpwrdlctl;
1139 mmdc0->mpzqlp2ctl = calib->mpzqlp2ctl;
1141 /* Read data DQ Byte0-3 delay */
1142 mmdc0->mprddqby0dl = 0x33333333;
1143 mmdc0->mprddqby1dl = 0x33333333;
1144 if (sysinfo->dsize > 0) {
1145 mmdc0->mprddqby2dl = 0x33333333;
1146 mmdc0->mprddqby3dl = 0x33333333;
1149 /* Write data DQ Byte0-3 delay */
1150 mmdc0->mpwrdqby0dl = 0xf3333333;
1151 mmdc0->mpwrdqby1dl = 0xf3333333;
1152 if (sysinfo->dsize > 0) {
1153 mmdc0->mpwrdqby2dl = 0xf3333333;
1154 mmdc0->mpwrdqby3dl = 0xf3333333;
1158 * In LPDDR2 mode this register should be cleared,
1159 * so no termination will be activated.
1161 mmdc0->mpodtctrl = 0;
1163 /* complete calibration */
1164 val = (1 << 11); /* Force measurement on delay-lines */
1165 mmdc0->mpmur0 = val;
1167 /* Step 1: configuration request */
1168 mmdc0->mdscr = (u32)(1 << 15); /* config request */
1170 /* Step 2: Timing configuration */
1171 mmdc0->mdcfg0 = (trfc << 24) | (txsr << 16) | (txp << 13) |
1173 mmdc0->mdcfg1 = (tras << 16) | (twr << 9) | (tmrd << 5) | twl;
1174 mmdc0->mdcfg2 = (trtp << 6) | (twtr << 3) | trrd;
1175 mmdc0->mdcfg3lp = (trc_lp << 16) | (trcd_lp << 8) |
1176 (trppb_lp << 4) | trpab_lp;
1179 mmdc0->mdasp = cs0_end; /* CS addressing */
1181 /* Step 3: Configure DDR type */
1182 mmdc0->mdmisc = (sysinfo->cs1_mirror << 19) | (sysinfo->walat << 16) |
1183 (sysinfo->bi_on << 12) | (sysinfo->mif3_mode << 9) |
1184 (sysinfo->ralat << 6) | (1 << 3);
1186 /* Step 4: Configure delay while leaving reset */
1187 mmdc0->mdor = (sysinfo->sde_to_rst << 8) |
1188 (sysinfo->rst_to_cke << 0);
1190 /* Step 5: Configure DDR physical parameters (density and burst len) */
1191 coladdr = lpddr2_cfg->coladdr;
1192 if (lpddr2_cfg->coladdr == 8) /* 8-bit COL is 0x3 */
1194 else if (lpddr2_cfg->coladdr == 12) /* 12-bit COL is 0x4 */
1196 mmdc0->mdctl = (lpddr2_cfg->rowaddr - 11) << 24 | /* ROW */
1197 (coladdr - 9) << 20 | /* COL */
1198 (0 << 19) | /* Burst Length = 4 for LPDDR2 */
1199 (sysinfo->dsize << 16); /* DDR data bus size */
1201 /* Step 6: Perform ZQ calibration */
1202 val = 0xa1390003; /* one-time HW ZQ calib */
1203 mmdc0->mpzqhwctrl = val;
1205 /* Step 7: Enable MMDC with desired chip select */
1206 mmdc0->mdctl |= (1 << 31) | /* SDE_0 for CS0 */
1207 ((sysinfo->ncs == 2) ? 1 : 0) << 30; /* SDE_1 for CS1 */
1209 /* Step 8: Write Mode Registers to Init LPDDR2 devices */
1210 for (cs = 0; cs < sysinfo->ncs; cs++) {
1212 mmdc0->mdscr = MR(63, 0, 3, cs);
1213 /* MR10: calibration,
1214 * 0xff is calibration command after intilization.
1216 val = 0xA | (0xff << 8);
1217 mmdc0->mdscr = MR(val, 0, 3, cs);
1219 val = 0x1 | (0x82 << 8);
1220 mmdc0->mdscr = MR(val, 0, 3, cs);
1222 val = 0x2 | (0x04 << 8);
1223 mmdc0->mdscr = MR(val, 0, 3, cs);
1225 val = 0x3 | (0x02 << 8);
1226 mmdc0->mdscr = MR(val, 0, 3, cs);
1229 /* Step 10: Power down control and self-refresh */
1230 mmdc0->mdpdc = (tcke & 0x7) << 16 |
1231 5 << 12 | /* PWDT_1: 256 cycles */
1232 5 << 8 | /* PWDT_0: 256 cycles */
1233 1 << 6 | /* BOTH_CS_PD */
1234 (tcksrx & 0x7) << 3 |
1236 mmdc0->mapsr = 0x00001006; /* ADOPT power down enabled */
1238 /* Step 11: Configure ZQ calibration: one-time and periodic 1ms */
1240 mmdc0->mpzqhwctrl = val;
1242 /* Step 12: Configure and activate periodic refresh */
1243 mmdc0->mdref = (sysinfo->refsel << 14) | (sysinfo->refr << 11);
1245 /* Step 13: Deassert config request - init complete */
1246 mmdc0->mdscr = 0x00000000;
1248 /* wait for auto-ZQ calibration to complete */
1252 void mx6_ddr3_cfg(const struct mx6_ddr_sysinfo *sysinfo,
1253 const struct mx6_mmdc_calibration *calib,
1254 const struct mx6_ddr3_cfg *ddr3_cfg)
1256 volatile struct mmdc_p_regs *mmdc0;
1257 volatile struct mmdc_p_regs *mmdc1;
1258 struct src *src_regs = (struct src *)SRC_BASE_ADDR;
1259 u8 soc_boot_cfg3 = (readl(&src_regs->sbmr1) >> 16) & 0xff;
1261 u8 tcke, tcksrx, tcksre, txpdll, taofpd, taonpd, trrd;
1262 u8 todtlon, taxpd, tanpd, tcwl, txp, tfaw, tcl;
1263 u8 todt_idle_off = 0x4; /* from DDR3 Script Aid spreadsheet */
1264 u16 trcd, trc, tras, twr, tmrd, trtp, trp, twtr, trfc, txs, txpr;
1266 u16 tdllk = 0x1ff; /* DLL locking time: 512 cycles (JEDEC DDR3) */
1268 int clkper; /* clock period in picoseconds */
1269 int clock; /* clock freq in MHz */
1271 u16 mem_speed = ddr3_cfg->mem_speed;
1273 mmdc0 = (struct mmdc_p_regs *)MMDC_P0_BASE_ADDR;
1274 if (!is_mx6sx() && !is_mx6ul() && !is_mx6ull() && !is_mx6sl())
1275 mmdc1 = (struct mmdc_p_regs *)MMDC_P1_BASE_ADDR;
1277 /* Limit mem_speed for MX6D/MX6Q */
1278 if (is_mx6dq() || is_mx6dqp()) {
1279 if (mem_speed > 1066)
1280 mem_speed = 1066; /* 1066 MT/s */
1284 /* Limit mem_speed for MX6S/MX6DL */
1286 if (mem_speed > 800)
1287 mem_speed = 800; /* 800 MT/s */
1292 clock = mem_speed / 2;
1294 * Data rate of 1066 MT/s requires 533 MHz DDR3 clock, but MX6D/Q supports
1295 * up to 528 MHz, so reduce the clock to fit chip specs
1297 if (is_mx6dq() || is_mx6dqp()) {
1299 clock = 528; /* 528 MHz */
1302 clkper = (1000 * 1000) / clock; /* pico seconds */
1307 switch (ddr3_cfg->density) {
1308 case 1: /* 1Gb per chip */
1309 trfc = DIV_ROUND_UP(110000, clkper) - 1;
1310 txs = DIV_ROUND_UP(120000, clkper) - 1;
1312 case 2: /* 2Gb per chip */
1313 trfc = DIV_ROUND_UP(160000, clkper) - 1;
1314 txs = DIV_ROUND_UP(170000, clkper) - 1;
1316 case 4: /* 4Gb per chip */
1317 trfc = DIV_ROUND_UP(260000, clkper) - 1;
1318 txs = DIV_ROUND_UP(270000, clkper) - 1;
1320 case 8: /* 8Gb per chip */
1321 trfc = DIV_ROUND_UP(350000, clkper) - 1;
1322 txs = DIV_ROUND_UP(360000, clkper) - 1;
1325 /* invalid density */
1326 puts("invalid chip density\n");
1332 switch (mem_speed) {
1334 txp = DIV_ROUND_UP(max(3 * clkper, 7500), clkper) - 1;
1335 tcke = DIV_ROUND_UP(max(3 * clkper, 7500), clkper) - 1;
1336 if (ddr3_cfg->pagesz == 1) {
1337 tfaw = DIV_ROUND_UP(40000, clkper) - 1;
1338 trrd = DIV_ROUND_UP(max(4 * clkper, 10000), clkper) - 1;
1340 tfaw = DIV_ROUND_UP(50000, clkper) - 1;
1341 trrd = DIV_ROUND_UP(max(4 * clkper, 10000), clkper) - 1;
1345 txp = DIV_ROUND_UP(max(3 * clkper, 7500), clkper) - 1;
1346 tcke = DIV_ROUND_UP(max(3 * clkper, 5625), clkper) - 1;
1347 if (ddr3_cfg->pagesz == 1) {
1348 tfaw = DIV_ROUND_UP(37500, clkper) - 1;
1349 trrd = DIV_ROUND_UP(max(4 * clkper, 7500), clkper) - 1;
1351 tfaw = DIV_ROUND_UP(50000, clkper) - 1;
1352 trrd = DIV_ROUND_UP(max(4 * clkper, 10000), clkper) - 1;
1356 puts("invalid memory speed\n");
1360 txpdll = DIV_ROUND_UP(max(10 * clkper, 24000), clkper) - 1;
1361 tcksre = DIV_ROUND_UP(max(5 * clkper, 10000), clkper);
1362 taonpd = DIV_ROUND_UP(2000, clkper) - 1;
1365 twr = DIV_ROUND_UP(15000, clkper) - 1;
1366 tmrd = DIV_ROUND_UP(max(12 * clkper, 15000), clkper) - 1;
1367 trc = DIV_ROUND_UP(ddr3_cfg->trcmin, clkper / 10) - 1;
1368 tras = DIV_ROUND_UP(ddr3_cfg->trasmin, clkper / 10) - 1;
1369 tcl = DIV_ROUND_UP(ddr3_cfg->trcd, clkper / 10) - 3;
1370 trp = DIV_ROUND_UP(ddr3_cfg->trcd, clkper / 10) - 1;
1371 twtr = ROUND(max(4 * clkper, 7500) / clkper, 1) - 1;
1374 cs0_end = 4 * sysinfo->cs_density - 1;
1376 debug("density:%d Gb (%d Gb per chip)\n",
1377 sysinfo->cs_density, ddr3_cfg->density);
1378 debug("clock: %dMHz (%d ps)\n", clock, clkper);
1379 debug("memspd:%d\n", mem_speed);
1380 debug("tcke=%d\n", tcke);
1381 debug("tcksrx=%d\n", tcksrx);
1382 debug("tcksre=%d\n", tcksre);
1383 debug("taofpd=%d\n", taofpd);
1384 debug("taonpd=%d\n", taonpd);
1385 debug("todtlon=%d\n", todtlon);
1386 debug("tanpd=%d\n", tanpd);
1387 debug("taxpd=%d\n", taxpd);
1388 debug("trfc=%d\n", trfc);
1389 debug("txs=%d\n", txs);
1390 debug("txp=%d\n", txp);
1391 debug("txpdll=%d\n", txpdll);
1392 debug("tfaw=%d\n", tfaw);
1393 debug("tcl=%d\n", tcl);
1394 debug("trcd=%d\n", trcd);
1395 debug("trp=%d\n", trp);
1396 debug("trc=%d\n", trc);
1397 debug("tras=%d\n", tras);
1398 debug("twr=%d\n", twr);
1399 debug("tmrd=%d\n", tmrd);
1400 debug("tcwl=%d\n", tcwl);
1401 debug("tdllk=%d\n", tdllk);
1402 debug("trtp=%d\n", trtp);
1403 debug("twtr=%d\n", twtr);
1404 debug("trrd=%d\n", trrd);
1405 debug("txpr=%d\n", txpr);
1406 debug("cs0_end=%d\n", cs0_end);
1407 debug("ncs=%d\n", sysinfo->ncs);
1408 debug("Rtt_wr=%d\n", sysinfo->rtt_wr);
1409 debug("Rtt_nom=%d\n", sysinfo->rtt_nom);
1410 debug("SRT=%d\n", ddr3_cfg->SRT);
1411 debug("twr=%d\n", twr);
1414 * board-specific configuration:
1415 * These values are determined empirically and vary per board layout
1417 * appnote, ddr3 spreadsheet
1419 mmdc0->mpwldectrl0 = calib->p0_mpwldectrl0;
1420 mmdc0->mpwldectrl1 = calib->p0_mpwldectrl1;
1421 mmdc0->mpdgctrl0 = calib->p0_mpdgctrl0;
1422 mmdc0->mpdgctrl1 = calib->p0_mpdgctrl1;
1423 mmdc0->mprddlctl = calib->p0_mprddlctl;
1424 mmdc0->mpwrdlctl = calib->p0_mpwrdlctl;
1425 if (sysinfo->dsize > 1) {
1426 MMDC1(mpwldectrl0, calib->p1_mpwldectrl0);
1427 MMDC1(mpwldectrl1, calib->p1_mpwldectrl1);
1428 MMDC1(mpdgctrl0, calib->p1_mpdgctrl0);
1429 MMDC1(mpdgctrl1, calib->p1_mpdgctrl1);
1430 MMDC1(mprddlctl, calib->p1_mprddlctl);
1431 MMDC1(mpwrdlctl, calib->p1_mpwrdlctl);
1434 /* Read data DQ Byte0-3 delay */
1435 mmdc0->mprddqby0dl = 0x33333333;
1436 mmdc0->mprddqby1dl = 0x33333333;
1437 if (sysinfo->dsize > 0) {
1438 mmdc0->mprddqby2dl = 0x33333333;
1439 mmdc0->mprddqby3dl = 0x33333333;
1442 if (sysinfo->dsize > 1) {
1443 MMDC1(mprddqby0dl, 0x33333333);
1444 MMDC1(mprddqby1dl, 0x33333333);
1445 MMDC1(mprddqby2dl, 0x33333333);
1446 MMDC1(mprddqby3dl, 0x33333333);
1449 /* MMDC Termination: rtt_nom:2 RZQ/2(120ohm), rtt_nom:1 RZQ/4(60ohm) */
1450 val = (sysinfo->rtt_nom == 2) ? 0x00011117 : 0x00022227;
1451 mmdc0->mpodtctrl = val;
1452 if (sysinfo->dsize > 1)
1453 MMDC1(mpodtctrl, val);
1455 /* complete calibration */
1456 val = (1 << 11); /* Force measurement on delay-lines */
1457 mmdc0->mpmur0 = val;
1458 if (sysinfo->dsize > 1)
1461 /* Step 1: configuration request */
1462 mmdc0->mdscr = (u32)(1 << 15); /* config request */
1464 /* Step 2: Timing configuration */
1465 mmdc0->mdcfg0 = (trfc << 24) | (txs << 16) | (txp << 13) |
1466 (txpdll << 9) | (tfaw << 4) | tcl;
1467 mmdc0->mdcfg1 = (trcd << 29) | (trp << 26) | (trc << 21) |
1468 (tras << 16) | (1 << 15) /* trpa */ |
1469 (twr << 9) | (tmrd << 5) | tcwl;
1470 mmdc0->mdcfg2 = (tdllk << 16) | (trtp << 6) | (twtr << 3) | trrd;
1471 mmdc0->mdotc = (taofpd << 27) | (taonpd << 24) | (tanpd << 20) |
1472 (taxpd << 16) | (todtlon << 12) | (todt_idle_off << 4);
1473 mmdc0->mdasp = cs0_end; /* CS addressing */
1475 /* Step 3: Configure DDR type */
1476 mmdc0->mdmisc = (sysinfo->cs1_mirror << 19) | (sysinfo->walat << 16) |
1477 (sysinfo->bi_on << 12) | (sysinfo->mif3_mode << 9) |
1478 (sysinfo->ralat << 6);
1480 /* Step 4: Configure delay while leaving reset */
1481 mmdc0->mdor = (txpr << 16) | (sysinfo->sde_to_rst << 8) |
1482 (sysinfo->rst_to_cke << 0);
1484 /* Step 5: Configure DDR physical parameters (density and burst len) */
1485 coladdr = ddr3_cfg->coladdr;
1486 if (ddr3_cfg->coladdr == 8) /* 8-bit COL is 0x3 */
1488 else if (ddr3_cfg->coladdr == 12) /* 12-bit COL is 0x4 */
1490 mmdc0->mdctl = (ddr3_cfg->rowaddr - 11) << 24 | /* ROW */
1491 (coladdr - 9) << 20 | /* COL */
1492 (1 << 19) | /* Burst Length = 8 for DDR3 */
1493 (sysinfo->dsize << 16); /* DDR data bus size */
1495 /* Step 6: Perform ZQ calibration */
1496 val = 0xa1390001; /* one-time HW ZQ calib */
1497 mmdc0->mpzqhwctrl = val;
1498 if (sysinfo->dsize > 1)
1499 MMDC1(mpzqhwctrl, val);
1501 /* Step 7: Enable MMDC with desired chip select */
1502 mmdc0->mdctl |= (1 << 31) | /* SDE_0 for CS0 */
1503 ((sysinfo->ncs == 2) ? 1 : 0) << 30; /* SDE_1 for CS1 */
1505 /* Step 8: Write Mode Registers to Init DDR3 devices */
1506 for (cs = 0; cs < sysinfo->ncs; cs++) {
1508 val = (sysinfo->rtt_wr & 3) << 9 | (ddr3_cfg->SRT & 1) << 7 |
1509 ((tcwl - 3) & 3) << 3;
1510 debug("MR2 CS%d: 0x%08x\n", cs, (u32)MR(val, 2, 3, cs));
1511 mmdc0->mdscr = MR(val, 2, 3, cs);
1513 debug("MR3 CS%d: 0x%08x\n", cs, (u32)MR(0, 3, 3, cs));
1514 mmdc0->mdscr = MR(0, 3, 3, cs);
1516 val = ((sysinfo->rtt_nom & 1) ? 1 : 0) << 2 |
1517 ((sysinfo->rtt_nom & 2) ? 1 : 0) << 6;
1518 debug("MR1 CS%d: 0x%08x\n", cs, (u32)MR(val, 1, 3, cs));
1519 mmdc0->mdscr = MR(val, 1, 3, cs);
1521 val = ((tcl - 1) << 4) | /* CAS */
1522 (1 << 8) | /* DLL Reset */
1523 ((twr - 3) << 9) | /* Write Recovery */
1524 (sysinfo->pd_fast_exit << 12); /* Precharge PD PLL on */
1525 debug("MR0 CS%d: 0x%08x\n", cs, (u32)MR(val, 0, 3, cs));
1526 mmdc0->mdscr = MR(val, 0, 3, cs);
1527 /* ZQ calibration */
1529 mmdc0->mdscr = MR(val, 0, 4, cs);
1532 /* Step 10: Power down control and self-refresh */
1533 mmdc0->mdpdc = (tcke & 0x7) << 16 |
1534 5 << 12 | /* PWDT_1: 256 cycles */
1535 5 << 8 | /* PWDT_0: 256 cycles */
1536 1 << 6 | /* BOTH_CS_PD */
1537 (tcksrx & 0x7) << 3 |
1539 if (!sysinfo->pd_fast_exit)
1540 mmdc0->mdpdc |= (1 << 7); /* SLOW_PD */
1541 mmdc0->mapsr = 0x00001006; /* ADOPT power down enabled */
1543 /* Step 11: Configure ZQ calibration: one-time and periodic 1ms */
1545 mmdc0->mpzqhwctrl = val;
1546 if (sysinfo->dsize > 1)
1547 MMDC1(mpzqhwctrl, val);
1549 /* Step 12: Configure and activate periodic refresh */
1550 mmdc0->mdref = (sysinfo->refsel << 14) | (sysinfo->refr << 11);
1553 * Step 13: i.MX6DQP only: If the NoC scheduler is enabled,
1554 * configure it and disable MMDC arbitration/reordering (see EB828)
1557 ((soc_boot_cfg3 & BOOT_CFG3_DDR_MASK) == DDR_MMAP_NOC_SINGLE ||
1558 (soc_boot_cfg3 & BOOT_CFG3_EXT_DDR_MASK) == DDR_MMAP_NOC_DUAL)) {
1559 struct mx6dqp_noc_sched_regs *noc_sched =
1560 (struct mx6dqp_noc_sched_regs *)MX6DQP_NOC_SCHED_BASE;
1563 * These values are fixed based on integration parameters and
1564 * should not be modified
1566 noc_sched->rlat = 0x00000040;
1567 noc_sched->ipu1 = 0x00000020;
1568 noc_sched->ipu2 = 0x00000020;
1570 noc_sched->activate = (1 << NOC_FAW_BANKS_SHIFT) |
1571 (tfaw << NOC_FAW_PERIOD_SHIFT) |
1572 (trrd << NOC_RD_SHIFT);
1573 noc_sched->ddrtiming = (((sysinfo->dsize == 1) ? 1 : 0)
1574 << NOC_BW_RATIO_SHIFT) |
1575 ((tcwl + twtr) << NOC_WR_TO_RD_SHIFT) |
1576 ((tcl - tcwl + 2) << NOC_RD_TO_WR_SHIFT) |
1577 (4 << NOC_BURST_LEN_SHIFT) | /* BL8 */
1578 ((tcwl + twr + trp + trcd)
1579 << NOC_WR_TO_MISS_SHIFT) |
1580 ((trtp + trp + trcd - 4)
1581 << NOC_RD_TO_MISS_SHIFT) |
1582 (trc << NOC_ACT_TO_ACT_SHIFT);
1584 if (sysinfo->dsize == 2) {
1585 if (ddr3_cfg->coladdr == 10) {
1586 if (ddr3_cfg->rowaddr == 15 &&
1588 noc_sched->ddrconf = 4;
1590 noc_sched->ddrconf = 0;
1591 } else if (ddr3_cfg->coladdr == 11) {
1592 noc_sched->ddrconf = 1;
1595 if (ddr3_cfg->coladdr == 9) {
1596 if (ddr3_cfg->rowaddr == 13)
1597 noc_sched->ddrconf = 2;
1598 else if (ddr3_cfg->rowaddr == 14)
1599 noc_sched->ddrconf = 15;
1600 } else if (ddr3_cfg->coladdr == 10) {
1601 if (ddr3_cfg->rowaddr == 14 &&
1603 noc_sched->ddrconf = 14;
1604 else if (ddr3_cfg->rowaddr == 15 &&
1606 noc_sched->ddrconf = 9;
1608 noc_sched->ddrconf = 3;
1609 } else if (ddr3_cfg->coladdr == 11) {
1610 if (ddr3_cfg->rowaddr == 15 &&
1612 noc_sched->ddrconf = 4;
1614 noc_sched->ddrconf = 0;
1615 } else if (ddr3_cfg->coladdr == 12) {
1616 if (ddr3_cfg->rowaddr == 14)
1617 noc_sched->ddrconf = 1;
1621 /* Disable MMDC arbitration/reordering */
1622 mmdc0->maarcr = 0x14420000;
1625 /* Step 13: Deassert config request - init complete */
1626 mmdc0->mdscr = 0x00000000;
1628 /* wait for auto-ZQ calibration to complete */
1632 void mmdc_read_calibration(struct mx6_ddr_sysinfo const *sysinfo,
1633 struct mx6_mmdc_calibration *calib)
1635 struct mmdc_p_regs *mmdc0 = (struct mmdc_p_regs *)MMDC_P0_BASE_ADDR;
1636 struct mmdc_p_regs *mmdc1 = (struct mmdc_p_regs *)MMDC_P1_BASE_ADDR;
1638 calib->p0_mpwldectrl0 = readl(&mmdc0->mpwldectrl0);
1639 calib->p0_mpwldectrl1 = readl(&mmdc0->mpwldectrl1);
1640 calib->p0_mpdgctrl0 = readl(&mmdc0->mpdgctrl0);
1641 calib->p0_mpdgctrl1 = readl(&mmdc0->mpdgctrl1);
1642 calib->p0_mprddlctl = readl(&mmdc0->mprddlctl);
1643 calib->p0_mpwrdlctl = readl(&mmdc0->mpwrdlctl);
1645 if (sysinfo->dsize == 2) {
1646 calib->p1_mpwldectrl0 = readl(&mmdc1->mpwldectrl0);
1647 calib->p1_mpwldectrl1 = readl(&mmdc1->mpwldectrl1);
1648 calib->p1_mpdgctrl0 = readl(&mmdc1->mpdgctrl0);
1649 calib->p1_mpdgctrl1 = readl(&mmdc1->mpdgctrl1);
1650 calib->p1_mprddlctl = readl(&mmdc1->mprddlctl);
1651 calib->p1_mpwrdlctl = readl(&mmdc1->mpwrdlctl);
1655 void mx6_dram_cfg(const struct mx6_ddr_sysinfo *sysinfo,
1656 const struct mx6_mmdc_calibration *calib,
1657 const void *ddr_cfg)
1659 if (sysinfo->ddr_type == DDR_TYPE_DDR3) {
1660 mx6_ddr3_cfg(sysinfo, calib, ddr_cfg);
1661 } else if (sysinfo->ddr_type == DDR_TYPE_LPDDR2) {
1662 mx6_lpddr2_cfg(sysinfo, calib, ddr_cfg);
1664 puts("Unsupported ddr type\n");