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