arm: imx6: configure NoC on i.MX6DQP
[oweals/u-boot.git] / arch / arm / mach-imx / mx6 / ddr.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright (C) 2014 Gateworks Corporation
4  * Author: Tim Harvey <tharvey@gateworks.com>
5  */
6
7 #include <common.h>
8 #include <hang.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_MX6_DDRCAL)
18 static void reset_read_data_fifos(void)
19 {
20         struct mmdc_p_regs *mmdc0 = (struct mmdc_p_regs *)MMDC_P0_BASE_ADDR;
21
22         /* Reset data FIFOs twice. */
23         setbits_le32(&mmdc0->mpdgctrl0, 1 << 31);
24         wait_for_bit_le32(&mmdc0->mpdgctrl0, 1 << 31, 0, 100, 0);
25
26         setbits_le32(&mmdc0->mpdgctrl0, 1 << 31);
27         wait_for_bit_le32(&mmdc0->mpdgctrl0, 1 << 31, 0, 100, 0);
28 }
29
30 static void precharge_all(const bool cs0_enable, const bool cs1_enable)
31 {
32         struct mmdc_p_regs *mmdc0 = (struct mmdc_p_regs *)MMDC_P0_BASE_ADDR;
33
34         /*
35          * Issue the Precharge-All command to the DDR device for both
36          * chip selects. Note, CON_REQ bit should also remain set. If
37          * only using one chip select, then precharge only the desired
38          * chip select.
39          */
40         if (cs0_enable) { /* CS0 */
41                 writel(0x04008050, &mmdc0->mdscr);
42                 wait_for_bit_le32(&mmdc0->mdscr, 1 << 14, 1, 100, 0);
43         }
44
45         if (cs1_enable) { /* CS1 */
46                 writel(0x04008058, &mmdc0->mdscr);
47                 wait_for_bit_le32(&mmdc0->mdscr, 1 << 14, 1, 100, 0);
48         }
49 }
50
51 static void force_delay_measurement(int bus_size)
52 {
53         struct mmdc_p_regs *mmdc0 = (struct mmdc_p_regs *)MMDC_P0_BASE_ADDR;
54         struct mmdc_p_regs *mmdc1 = (struct mmdc_p_regs *)MMDC_P1_BASE_ADDR;
55
56         writel(0x800, &mmdc0->mpmur0);
57         if (bus_size == 0x2)
58                 writel(0x800, &mmdc1->mpmur0);
59 }
60
61 static void modify_dg_result(u32 *reg_st0, u32 *reg_st1, u32 *reg_ctrl)
62 {
63         u32 dg_tmp_val, dg_dl_abs_offset, dg_hc_del, val_ctrl;
64
65         /*
66          * DQS gating absolute offset should be modified from reflecting
67          * (HW_DG_LOWx + HW_DG_UPx)/2 to reflecting (HW_DG_UPx - 0x80)
68          */
69
70         val_ctrl = readl(reg_ctrl);
71         val_ctrl &= 0xf0000000;
72
73         dg_tmp_val = ((readl(reg_st0) & 0x07ff0000) >> 16) - 0xc0;
74         dg_dl_abs_offset = dg_tmp_val & 0x7f;
75         dg_hc_del = (dg_tmp_val & 0x780) << 1;
76
77         val_ctrl |= dg_dl_abs_offset + dg_hc_del;
78
79         dg_tmp_val = ((readl(reg_st1) & 0x07ff0000) >> 16) - 0xc0;
80         dg_dl_abs_offset = dg_tmp_val & 0x7f;
81         dg_hc_del = (dg_tmp_val & 0x780) << 1;
82
83         val_ctrl |= (dg_dl_abs_offset + dg_hc_del) << 16;
84
85         writel(val_ctrl, reg_ctrl);
86 }
87
88 static void correct_mpwldectr_result(void *reg)
89 {
90         /* Limit is 200/256 of CK, which is WL_HC_DELx | 0x48. */
91         const unsigned int limit = 0x148;
92         u32 val = readl(reg);
93         u32 old = val;
94
95         if ((val & 0x17f) > limit)
96                 val &= 0xffff << 16;
97
98         if (((val >> 16) & 0x17f) > limit)
99                 val &= 0xffff;
100
101         if (old != val)
102                 writel(val, reg);
103 }
104
105 int mmdc_do_write_level_calibration(struct mx6_ddr_sysinfo const *sysinfo)
106 {
107         struct mmdc_p_regs *mmdc0 = (struct mmdc_p_regs *)MMDC_P0_BASE_ADDR;
108         struct mmdc_p_regs *mmdc1 = (struct mmdc_p_regs *)MMDC_P1_BASE_ADDR;
109         u32 esdmisc_val, zq_val;
110         u32 errors = 0;
111         u32 ldectrl[4] = {0};
112         u32 ddr_mr1 = 0x4;
113         u32 rwalat_max;
114
115         /*
116          * Stash old values in case calibration fails,
117          * we need to restore them
118          */
119         ldectrl[0] = readl(&mmdc0->mpwldectrl0);
120         ldectrl[1] = readl(&mmdc0->mpwldectrl1);
121         if (sysinfo->dsize == 2) {
122                 ldectrl[2] = readl(&mmdc1->mpwldectrl0);
123                 ldectrl[3] = readl(&mmdc1->mpwldectrl1);
124         }
125
126         /* disable DDR logic power down timer */
127         clrbits_le32(&mmdc0->mdpdc, 0xff00);
128
129         /* disable Adopt power down timer */
130         setbits_le32(&mmdc0->mapsr, 0x1);
131
132         debug("Starting write leveling calibration.\n");
133
134         /*
135          * 2. disable auto refresh and ZQ calibration
136          * before proceeding with Write Leveling calibration
137          */
138         esdmisc_val = readl(&mmdc0->mdref);
139         writel(0x0000C000, &mmdc0->mdref);
140         zq_val = readl(&mmdc0->mpzqhwctrl);
141         writel(zq_val & ~0x3, &mmdc0->mpzqhwctrl);
142
143         /* 3. increase walat and ralat to maximum */
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);
148         /*
149          * 4 & 5. Configure the external DDR device to enter write-leveling
150          * mode through Load Mode Register command.
151          * Register setting:
152          * Bits[31:16] MR1 value (0x0080 write leveling enable)
153          * Bit[9] set WL_EN to enable MMDC DQS output
154          * Bits[6:4] set CMD bits for Load Mode Register programming
155          * Bits[2:0] set CMD_BA to 0x1 for DDR MR1 programming
156          */
157         writel(0x00808231, &mmdc0->mdscr);
158
159         /* 6. Activate automatic calibration by setting MPWLGCR[HW_WL_EN] */
160         writel(0x00000001, &mmdc0->mpwlgcr);
161
162         /*
163          * 7. Upon completion of this process the MMDC de-asserts
164          * the MPWLGCR[HW_WL_EN]
165          */
166         wait_for_bit_le32(&mmdc0->mpwlgcr, 1 << 0, 0, 100, 0);
167
168         /*
169          * 8. check for any errors: check both PHYs for x64 configuration,
170          * if x32, check only PHY0
171          */
172         if (readl(&mmdc0->mpwlgcr) & 0x00000F00)
173                 errors |= 1;
174         if (sysinfo->dsize == 2)
175                 if (readl(&mmdc1->mpwlgcr) & 0x00000F00)
176                         errors |= 2;
177
178         debug("Ending write leveling calibration. Error mask: 0x%x\n", errors);
179
180         /* check to see if cal failed */
181         if ((readl(&mmdc0->mpwldectrl0) == 0x001F001F) &&
182             (readl(&mmdc0->mpwldectrl1) == 0x001F001F) &&
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);
192                 }
193                 errors |= 4;
194         }
195
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);
201         }
202
203         /*
204          * User should issue MRS command to exit write leveling mode
205          * through Load Mode Register command
206          * Register setting:
207          * Bits[31:16] MR1 value "ddr_mr1" value from initialization
208          * Bit[9] clear WL_EN to disable MMDC DQS output
209          * Bits[6:4] set CMD bits for Load Mode Register programming
210          * Bits[2:0] set CMD_BA to 0x1 for DDR MR1 programming
211          */
212         writel((ddr_mr1 << 16) + 0x8031, &mmdc0->mdscr);
213
214         /* re-enable auto refresh and zq cal */
215         writel(esdmisc_val, &mmdc0->mdref);
216         writel(zq_val, &mmdc0->mpzqhwctrl);
217
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));
227         }
228
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);
235         }
236
237         /* enable DDR logic power down timer: */
238         setbits_le32(&mmdc0->mdpdc, 0x00005500);
239
240         /* Enable Adopt power down timer: */
241         clrbits_le32(&mmdc0->mapsr, 0x1);
242
243         /* Clear CON_REQ */
244         writel(0, &mmdc0->mdscr);
245
246         return errors;
247 }
248
249 static void mmdc_set_sdqs(bool set)
250 {
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;
255         int i, sdqs_cnt;
256         u32 sdqs;
257
258         if (is_mx6sx()) {
259                 sdqs = (u32)(&mx6sx_ddr_iomux->dram_sdqs0);
260                 sdqs_cnt = 2;
261         } else {        /* MX6DQ */
262                 sdqs = (u32)(&mx6dq_ddr_iomux->dram_sdqs0);
263                 sdqs_cnt = 8;
264         }
265
266         for (i = 0; i < sdqs_cnt; i++) {
267                 if (set)
268                         setbits_le32(sdqs + (4 * i), 0x7000);
269                 else
270                         clrbits_le32(sdqs + (4 * i), 0x7000);
271         }
272 }
273
274 int mmdc_do_dqs_calibration(struct mx6_ddr_sysinfo const *sysinfo)
275 {
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;
278         bool cs0_enable;
279         bool cs1_enable;
280         bool cs0_enable_initial;
281         bool cs1_enable_initial;
282         u32 esdmisc_val;
283         u32 temp_ref;
284         u32 pddword = 0x00ffff00; /* best so far, place into MPPDCMPR1 */
285         u32 errors = 0;
286         u32 initdelay = 0x40404040;
287
288         /* check to see which chip selects are enabled */
289         cs0_enable_initial = readl(&mmdc0->mdctl) & 0x80000000;
290         cs1_enable_initial = readl(&mmdc0->mdctl) & 0x40000000;
291
292         /* disable DDR logic power down timer: */
293         clrbits_le32(&mmdc0->mdpdc, 0xff00);
294
295         /* disable Adopt power down timer: */
296         setbits_le32(&mmdc0->mapsr, 0x1);
297
298         /* set DQS pull ups */
299         mmdc_set_sdqs(true);
300
301         /* Save old RALAT and WALAT values */
302         esdmisc_val = readl(&mmdc0->mdmisc);
303
304         setbits_le32(&mmdc0->mdmisc,
305                      (1 << 6) | (1 << 7) | (1 << 8) | (1 << 16) | (1 << 17));
306
307         /* Disable auto refresh before proceeding with calibration */
308         temp_ref = readl(&mmdc0->mdref);
309         writel(0x0000c000, &mmdc0->mdref);
310
311         /*
312          * Per the ref manual, issue one refresh cycle MDSCR[CMD]= 0x2,
313          * this also sets the CON_REQ bit.
314          */
315         if (cs0_enable_initial)
316                 writel(0x00008020, &mmdc0->mdscr);
317         if (cs1_enable_initial)
318                 writel(0x00008028, &mmdc0->mdscr);
319
320         /* poll to make sure the con_ack bit was asserted */
321         wait_for_bit_le32(&mmdc0->mdscr, 1 << 14, 1, 100, 0);
322
323         /*
324          * Check MDMISC register CALIB_PER_CS to see which CS calibration
325          * is targeted to (under normal cases, it should be cleared
326          * as this is the default value, indicating calibration is directed
327          * to CS0).
328          * Disable the other chip select not being target for calibration
329          * to avoid any potential issues.  This will get re-enabled at end
330          * of calibration.
331          */
332         if ((readl(&mmdc0->mdmisc) & 0x00100000) == 0)
333                 clrbits_le32(&mmdc0->mdctl, 1 << 30);   /* clear SDE_1 */
334         else
335                 clrbits_le32(&mmdc0->mdctl, 1 << 31);   /* clear SDE_0 */
336
337         /*
338          * Check to see which chip selects are now enabled for
339          * the remainder of the calibration.
340          */
341         cs0_enable = readl(&mmdc0->mdctl) & 0x80000000;
342         cs1_enable = readl(&mmdc0->mdctl) & 0x40000000;
343
344         precharge_all(cs0_enable, cs1_enable);
345
346         /* Write the pre-defined value into MPPDCMPR1 */
347         writel(pddword, &mmdc0->mppdcmpr1);
348
349         /*
350          * Issue a write access to the external DDR device by setting
351          * the bit SW_DUMMY_WR (bit 0) in the MPSWDAR0 and then poll
352          * this bit until it clears to indicate completion of the write access.
353          */
354         setbits_le32(&mmdc0->mpswdar0, 1);
355         wait_for_bit_le32(&mmdc0->mpswdar0, 1 << 0, 0, 100, 0);
356
357         /* Set the RD_DL_ABS# bits to their default values
358          * (will be calibrated later in the read delay-line calibration).
359          * Both PHYs for x64 configuration, if x32, do only PHY0.
360          */
361         writel(initdelay, &mmdc0->mprddlctl);
362         if (sysinfo->dsize == 0x2)
363                 writel(initdelay, &mmdc1->mprddlctl);
364
365         /* Force a measurment, for previous delay setup to take effect. */
366         force_delay_measurement(sysinfo->dsize);
367
368         /*
369          * ***************************
370          * Read DQS Gating calibration
371          * ***************************
372          */
373         debug("Starting Read DQS Gating calibration.\n");
374
375         /*
376          * Reset the read data FIFOs (two resets); only need to issue reset
377          * to PHY0 since in x64 mode, the reset will also go to PHY1.
378          */
379         reset_read_data_fifos();
380
381         /*
382          * Start the automatic read DQS gating calibration process by
383          * asserting MPDGCTRL0[HW_DG_EN] and MPDGCTRL0[DG_CMP_CYC]
384          * and then poll MPDGCTRL0[HW_DG_EN]] until this bit clears
385          * to indicate completion.
386          * Also, ensure that MPDGCTRL0[HW_DG_ERR] is clear to indicate
387          * no errors were seen during calibration.
388          */
389
390         /*
391          * Set bit 30: chooses option to wait 32 cycles instead of
392          * 16 before comparing read data.
393          */
394         setbits_le32(&mmdc0->mpdgctrl0, 1 << 30);
395         if (sysinfo->dsize == 2)
396                 setbits_le32(&mmdc1->mpdgctrl0, 1 << 30);
397
398         /* Set bit 28 to start automatic read DQS gating calibration */
399         setbits_le32(&mmdc0->mpdgctrl0, 5 << 28);
400
401         /* Poll for completion.  MPDGCTRL0[HW_DG_EN] should be 0 */
402         wait_for_bit_le32(&mmdc0->mpdgctrl0, 1 << 28, 0, 100, 0);
403
404         /*
405          * Check to see if any errors were encountered during calibration
406          * (check MPDGCTRL0[HW_DG_ERR]).
407          * Check both PHYs for x64 configuration, if x32, check only PHY0.
408          */
409         if (readl(&mmdc0->mpdgctrl0) & 0x00001000)
410                 errors |= 1;
411
412         if ((sysinfo->dsize == 0x2) && (readl(&mmdc1->mpdgctrl0) & 0x00001000))
413                 errors |= 2;
414
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);
419
420         /*
421          * DQS gating absolute offset should be modified from
422          * reflecting (HW_DG_LOWx + HW_DG_UPx)/2 to
423          * reflecting (HW_DG_UPx - 0x80)
424          */
425         modify_dg_result(&mmdc0->mpdghwst0, &mmdc0->mpdghwst1,
426                          &mmdc0->mpdgctrl0);
427         modify_dg_result(&mmdc0->mpdghwst2, &mmdc0->mpdghwst3,
428                          &mmdc0->mpdgctrl1);
429         if (sysinfo->dsize == 0x2) {
430                 modify_dg_result(&mmdc1->mpdghwst0, &mmdc1->mpdghwst1,
431                                  &mmdc1->mpdgctrl0);
432                 modify_dg_result(&mmdc1->mpdghwst2, &mmdc1->mpdghwst3,
433                                  &mmdc1->mpdgctrl1);
434         }
435         debug("Ending Read DQS Gating calibration. Error mask: 0x%x\n", errors);
436
437         /*
438          * **********************
439          * Read Delay calibration
440          * **********************
441          */
442         debug("Starting Read Delay calibration.\n");
443
444         reset_read_data_fifos();
445
446         /*
447          * 4. Issue the Precharge-All command to the DDR device for both
448          * chip selects.  If only using one chip select, then precharge
449          * only the desired chip select.
450          */
451         precharge_all(cs0_enable, cs1_enable);
452
453         /*
454          * 9. Read delay-line calibration
455          * Start the automatic read calibration process by asserting
456          * MPRDDLHWCTL[HW_RD_DL_EN].
457          */
458         writel(0x00000030, &mmdc0->mprddlhwctl);
459
460         /*
461          * 10. poll for completion
462          * MMDC indicates that the write data calibration had finished by
463          * setting MPRDDLHWCTL[HW_RD_DL_EN] = 0.   Also, ensure that
464          * no error bits were set.
465          */
466         wait_for_bit_le32(&mmdc0->mprddlhwctl, 1 << 4, 0, 100, 0);
467
468         /* check both PHYs for x64 configuration, if x32, check only PHY0 */
469         if (readl(&mmdc0->mprddlhwctl) & 0x0000000f)
470                 errors |= 4;
471
472         if ((sysinfo->dsize == 0x2) &&
473             (readl(&mmdc1->mprddlhwctl) & 0x0000000f))
474                 errors |= 8;
475
476         debug("Ending Read Delay calibration. Error mask: 0x%x\n", errors);
477
478         /*
479          * ***********************
480          * Write Delay Calibration
481          * ***********************
482          */
483         debug("Starting Write Delay calibration.\n");
484
485         reset_read_data_fifos();
486
487         /*
488          * 4. Issue the Precharge-All command to the DDR device for both
489          * chip selects. If only using one chip select, then precharge
490          * only the desired chip select.
491          */
492         precharge_all(cs0_enable, cs1_enable);
493
494         /*
495          * 8. Set the WR_DL_ABS# bits to their default values.
496          * Both PHYs for x64 configuration, if x32, do only PHY0.
497          */
498         writel(initdelay, &mmdc0->mpwrdlctl);
499         if (sysinfo->dsize == 0x2)
500                 writel(initdelay, &mmdc1->mpwrdlctl);
501
502         /*
503          * XXX This isn't in the manual. Force a measurement,
504          * for previous delay setup to effect.
505          */
506         force_delay_measurement(sysinfo->dsize);
507
508         /*
509          * 9. 10. Start the automatic write calibration process
510          * by asserting MPWRDLHWCTL0[HW_WR_DL_EN].
511          */
512         writel(0x00000030, &mmdc0->mpwrdlhwctl);
513
514         /*
515          * Poll for completion.
516          * MMDC indicates that the write data calibration had finished
517          * by setting MPWRDLHWCTL[HW_WR_DL_EN] = 0.
518          * Also, ensure that no error bits were set.
519          */
520         wait_for_bit_le32(&mmdc0->mpwrdlhwctl, 1 << 4, 0, 100, 0);
521
522         /* Check both PHYs for x64 configuration, if x32, check only PHY0 */
523         if (readl(&mmdc0->mpwrdlhwctl) & 0x0000000f)
524                 errors |= 16;
525
526         if ((sysinfo->dsize == 0x2) &&
527             (readl(&mmdc1->mpwrdlhwctl) & 0x0000000f))
528                 errors |= 32;
529
530         debug("Ending Write Delay calibration. Error mask: 0x%x\n", errors);
531
532         reset_read_data_fifos();
533
534         /* Enable DDR logic power down timer */
535         setbits_le32(&mmdc0->mdpdc, 0x00005500);
536
537         /* Enable Adopt power down timer */
538         clrbits_le32(&mmdc0->mapsr, 0x1);
539
540         /* Restore MDMISC value (RALAT, WALAT) to MMDCP1 */
541         writel(esdmisc_val, &mmdc0->mdmisc);
542
543         /* Clear DQS pull ups */
544         mmdc_set_sdqs(false);
545
546         /* Re-enable SDE (chip selects) if they were set initially */
547         if (cs1_enable_initial)
548                 /* Set SDE_1 */
549                 setbits_le32(&mmdc0->mdctl, 1 << 30);
550
551         if (cs0_enable_initial)
552                 /* Set SDE_0 */
553                 setbits_le32(&mmdc0->mdctl, 1 << 31);
554
555         /* Re-enable to auto refresh */
556         writel(temp_ref, &mmdc0->mdref);
557
558         /* Clear the MDSCR (including the con_req bit) */
559         writel(0x0, &mmdc0->mdscr);     /* CS0 */
560
561         /* Poll to make sure the con_ack bit is clear */
562         wait_for_bit_le32(&mmdc0->mdscr, 1 << 14, 0, 100, 0);
563
564         /*
565          * Print out the registers that were updated as a result
566          * of the calibration process.
567          */
568         debug("MMDC registers updated from calibration\n");
569         debug("Read DQS gating calibration:\n");
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));
575         }
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));
584
585         /*
586          * Registers below are for debugging purposes.  These print out
587          * the upper and lower boundaries captured during
588          * read DQS gating calibration.
589          */
590         debug("Status registers bounds for read DQS gating:\n");
591         debug("\tMPDGHWST0 PHY0 = 0x%08x\n", readl(&mmdc0->mpdghwst0));
592         debug("\tMPDGHWST1 PHY0 = 0x%08x\n", readl(&mmdc0->mpdghwst1));
593         debug("\tMPDGHWST2 PHY0 = 0x%08x\n", readl(&mmdc0->mpdghwst2));
594         debug("\tMPDGHWST3 PHY0 = 0x%08x\n", readl(&mmdc0->mpdghwst3));
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));
600         }
601
602         debug("Final do_dqs_calibration error mask: 0x%x\n", errors);
603
604         return errors;
605 }
606 #endif
607
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)
613 {
614         struct mx6sx_iomux_ddr_regs *mx6_ddr_iomux;
615         struct mx6sx_iomux_grp_regs *mx6_grp_iomux;
616
617         mx6_ddr_iomux = (struct mx6sx_iomux_ddr_regs *)MX6SX_IOM_DDR_BASE;
618         mx6_grp_iomux = (struct mx6sx_iomux_grp_regs *)MX6SX_IOM_GRP_BASE;
619
620         /* DDR IO TYPE */
621         writel(grp->grp_ddr_type, &mx6_grp_iomux->grp_ddr_type);
622         writel(grp->grp_ddrpke, &mx6_grp_iomux->grp_ddrpke);
623
624         /* CLOCK */
625         writel(ddr->dram_sdclk_0, &mx6_ddr_iomux->dram_sdclk_0);
626
627         /* ADDRESS */
628         writel(ddr->dram_cas, &mx6_ddr_iomux->dram_cas);
629         writel(ddr->dram_ras, &mx6_ddr_iomux->dram_ras);
630         writel(grp->grp_addds, &mx6_grp_iomux->grp_addds);
631
632         /* Control */
633         writel(ddr->dram_reset, &mx6_ddr_iomux->dram_reset);
634         writel(ddr->dram_sdba2, &mx6_ddr_iomux->dram_sdba2);
635         writel(ddr->dram_sdcke0, &mx6_ddr_iomux->dram_sdcke0);
636         writel(ddr->dram_sdcke1, &mx6_ddr_iomux->dram_sdcke1);
637         writel(ddr->dram_odt0, &mx6_ddr_iomux->dram_odt0);
638         writel(ddr->dram_odt1, &mx6_ddr_iomux->dram_odt1);
639         writel(grp->grp_ctlds, &mx6_grp_iomux->grp_ctlds);
640
641         /* Data Strobes */
642         writel(grp->grp_ddrmode_ctl, &mx6_grp_iomux->grp_ddrmode_ctl);
643         writel(ddr->dram_sdqs0, &mx6_ddr_iomux->dram_sdqs0);
644         writel(ddr->dram_sdqs1, &mx6_ddr_iomux->dram_sdqs1);
645         if (width >= 32) {
646                 writel(ddr->dram_sdqs2, &mx6_ddr_iomux->dram_sdqs2);
647                 writel(ddr->dram_sdqs3, &mx6_ddr_iomux->dram_sdqs3);
648         }
649
650         /* Data */
651         writel(grp->grp_ddrmode, &mx6_grp_iomux->grp_ddrmode);
652         writel(grp->grp_b0ds, &mx6_grp_iomux->grp_b0ds);
653         writel(grp->grp_b1ds, &mx6_grp_iomux->grp_b1ds);
654         if (width >= 32) {
655                 writel(grp->grp_b2ds, &mx6_grp_iomux->grp_b2ds);
656                 writel(grp->grp_b3ds, &mx6_grp_iomux->grp_b3ds);
657         }
658         writel(ddr->dram_dqm0, &mx6_ddr_iomux->dram_dqm0);
659         writel(ddr->dram_dqm1, &mx6_ddr_iomux->dram_dqm1);
660         if (width >= 32) {
661                 writel(ddr->dram_dqm2, &mx6_ddr_iomux->dram_dqm2);
662                 writel(ddr->dram_dqm3, &mx6_ddr_iomux->dram_dqm3);
663         }
664 }
665 #endif
666
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)
671 {
672         struct mx6ul_iomux_ddr_regs *mx6_ddr_iomux;
673         struct mx6ul_iomux_grp_regs *mx6_grp_iomux;
674
675         mx6_ddr_iomux = (struct mx6ul_iomux_ddr_regs *)MX6UL_IOM_DDR_BASE;
676         mx6_grp_iomux = (struct mx6ul_iomux_grp_regs *)MX6UL_IOM_GRP_BASE;
677
678         /* DDR IO TYPE */
679         writel(grp->grp_ddr_type, &mx6_grp_iomux->grp_ddr_type);
680         writel(grp->grp_ddrpke, &mx6_grp_iomux->grp_ddrpke);
681
682         /* CLOCK */
683         writel(ddr->dram_sdclk_0, &mx6_ddr_iomux->dram_sdclk_0);
684
685         /* ADDRESS */
686         writel(ddr->dram_cas, &mx6_ddr_iomux->dram_cas);
687         writel(ddr->dram_ras, &mx6_ddr_iomux->dram_ras);
688         writel(grp->grp_addds, &mx6_grp_iomux->grp_addds);
689
690         /* Control */
691         writel(ddr->dram_reset, &mx6_ddr_iomux->dram_reset);
692         writel(ddr->dram_sdba2, &mx6_ddr_iomux->dram_sdba2);
693         writel(ddr->dram_odt0, &mx6_ddr_iomux->dram_odt0);
694         writel(ddr->dram_odt1, &mx6_ddr_iomux->dram_odt1);
695         writel(grp->grp_ctlds, &mx6_grp_iomux->grp_ctlds);
696
697         /* Data Strobes */
698         writel(grp->grp_ddrmode_ctl, &mx6_grp_iomux->grp_ddrmode_ctl);
699         writel(ddr->dram_sdqs0, &mx6_ddr_iomux->dram_sdqs0);
700         writel(ddr->dram_sdqs1, &mx6_ddr_iomux->dram_sdqs1);
701
702         /* Data */
703         writel(grp->grp_ddrmode, &mx6_grp_iomux->grp_ddrmode);
704         writel(grp->grp_b0ds, &mx6_grp_iomux->grp_b0ds);
705         writel(grp->grp_b1ds, &mx6_grp_iomux->grp_b1ds);
706         writel(ddr->dram_dqm0, &mx6_ddr_iomux->dram_dqm0);
707         writel(ddr->dram_dqm1, &mx6_ddr_iomux->dram_dqm1);
708 }
709 #endif
710
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)
715 {
716         struct mx6sl_iomux_ddr_regs *mx6_ddr_iomux;
717         struct mx6sl_iomux_grp_regs *mx6_grp_iomux;
718
719         mx6_ddr_iomux = (struct mx6sl_iomux_ddr_regs *)MX6SL_IOM_DDR_BASE;
720         mx6_grp_iomux = (struct mx6sl_iomux_grp_regs *)MX6SL_IOM_GRP_BASE;
721
722         /* DDR IO TYPE */
723         mx6_grp_iomux->grp_ddr_type = grp->grp_ddr_type;
724         mx6_grp_iomux->grp_ddrpke = grp->grp_ddrpke;
725
726         /* CLOCK */
727         mx6_ddr_iomux->dram_sdclk_0 = ddr->dram_sdclk_0;
728
729         /* ADDRESS */
730         mx6_ddr_iomux->dram_cas = ddr->dram_cas;
731         mx6_ddr_iomux->dram_ras = ddr->dram_ras;
732         mx6_grp_iomux->grp_addds = grp->grp_addds;
733
734         /* Control */
735         mx6_ddr_iomux->dram_reset = ddr->dram_reset;
736         mx6_ddr_iomux->dram_sdba2 = ddr->dram_sdba2;
737         mx6_grp_iomux->grp_ctlds = grp->grp_ctlds;
738
739         /* Data Strobes */
740         mx6_grp_iomux->grp_ddrmode_ctl = grp->grp_ddrmode_ctl;
741         mx6_ddr_iomux->dram_sdqs0 = ddr->dram_sdqs0;
742         mx6_ddr_iomux->dram_sdqs1 = ddr->dram_sdqs1;
743         if (width >= 32) {
744                 mx6_ddr_iomux->dram_sdqs2 = ddr->dram_sdqs2;
745                 mx6_ddr_iomux->dram_sdqs3 = ddr->dram_sdqs3;
746         }
747
748         /* Data */
749         mx6_grp_iomux->grp_ddrmode = grp->grp_ddrmode;
750         mx6_grp_iomux->grp_b0ds = grp->grp_b0ds;
751         mx6_grp_iomux->grp_b1ds = grp->grp_b1ds;
752         if (width >= 32) {
753                 mx6_grp_iomux->grp_b2ds = grp->grp_b2ds;
754                 mx6_grp_iomux->grp_b3ds = grp->grp_b3ds;
755         }
756
757         mx6_ddr_iomux->dram_dqm0 = ddr->dram_dqm0;
758         mx6_ddr_iomux->dram_dqm1 = ddr->dram_dqm1;
759         if (width >= 32) {
760                 mx6_ddr_iomux->dram_dqm2 = ddr->dram_dqm2;
761                 mx6_ddr_iomux->dram_dqm3 = ddr->dram_dqm3;
762         }
763 }
764 #endif
765
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)
771 {
772         volatile struct mx6dq_iomux_ddr_regs *mx6_ddr_iomux;
773         volatile struct mx6dq_iomux_grp_regs *mx6_grp_iomux;
774
775         mx6_ddr_iomux = (struct mx6dq_iomux_ddr_regs *)MX6DQ_IOM_DDR_BASE;
776         mx6_grp_iomux = (struct mx6dq_iomux_grp_regs *)MX6DQ_IOM_GRP_BASE;
777
778         /* DDR IO Type */
779         mx6_grp_iomux->grp_ddr_type = grp->grp_ddr_type;
780         mx6_grp_iomux->grp_ddrpke = grp->grp_ddrpke;
781
782         /* Clock */
783         mx6_ddr_iomux->dram_sdclk_0 = ddr->dram_sdclk_0;
784         mx6_ddr_iomux->dram_sdclk_1 = ddr->dram_sdclk_1;
785
786         /* Address */
787         mx6_ddr_iomux->dram_cas = ddr->dram_cas;
788         mx6_ddr_iomux->dram_ras = ddr->dram_ras;
789         mx6_grp_iomux->grp_addds = grp->grp_addds;
790
791         /* Control */
792         mx6_ddr_iomux->dram_reset = ddr->dram_reset;
793         mx6_ddr_iomux->dram_sdcke0 = ddr->dram_sdcke0;
794         mx6_ddr_iomux->dram_sdcke1 = ddr->dram_sdcke1;
795         mx6_ddr_iomux->dram_sdba2 = ddr->dram_sdba2;
796         mx6_ddr_iomux->dram_sdodt0 = ddr->dram_sdodt0;
797         mx6_ddr_iomux->dram_sdodt1 = ddr->dram_sdodt1;
798         mx6_grp_iomux->grp_ctlds = grp->grp_ctlds;
799
800         /* Data Strobes */
801         mx6_grp_iomux->grp_ddrmode_ctl = grp->grp_ddrmode_ctl;
802         mx6_ddr_iomux->dram_sdqs0 = ddr->dram_sdqs0;
803         mx6_ddr_iomux->dram_sdqs1 = ddr->dram_sdqs1;
804         if (width >= 32) {
805                 mx6_ddr_iomux->dram_sdqs2 = ddr->dram_sdqs2;
806                 mx6_ddr_iomux->dram_sdqs3 = ddr->dram_sdqs3;
807         }
808         if (width >= 64) {
809                 mx6_ddr_iomux->dram_sdqs4 = ddr->dram_sdqs4;
810                 mx6_ddr_iomux->dram_sdqs5 = ddr->dram_sdqs5;
811                 mx6_ddr_iomux->dram_sdqs6 = ddr->dram_sdqs6;
812                 mx6_ddr_iomux->dram_sdqs7 = ddr->dram_sdqs7;
813         }
814
815         /* Data */
816         mx6_grp_iomux->grp_ddrmode = grp->grp_ddrmode;
817         mx6_grp_iomux->grp_b0ds = grp->grp_b0ds;
818         mx6_grp_iomux->grp_b1ds = grp->grp_b1ds;
819         if (width >= 32) {
820                 mx6_grp_iomux->grp_b2ds = grp->grp_b2ds;
821                 mx6_grp_iomux->grp_b3ds = grp->grp_b3ds;
822         }
823         if (width >= 64) {
824                 mx6_grp_iomux->grp_b4ds = grp->grp_b4ds;
825                 mx6_grp_iomux->grp_b5ds = grp->grp_b5ds;
826                 mx6_grp_iomux->grp_b6ds = grp->grp_b6ds;
827                 mx6_grp_iomux->grp_b7ds = grp->grp_b7ds;
828         }
829         mx6_ddr_iomux->dram_dqm0 = ddr->dram_dqm0;
830         mx6_ddr_iomux->dram_dqm1 = ddr->dram_dqm1;
831         if (width >= 32) {
832                 mx6_ddr_iomux->dram_dqm2 = ddr->dram_dqm2;
833                 mx6_ddr_iomux->dram_dqm3 = ddr->dram_dqm3;
834         }
835         if (width >= 64) {
836                 mx6_ddr_iomux->dram_dqm4 = ddr->dram_dqm4;
837                 mx6_ddr_iomux->dram_dqm5 = ddr->dram_dqm5;
838                 mx6_ddr_iomux->dram_dqm6 = ddr->dram_dqm6;
839                 mx6_ddr_iomux->dram_dqm7 = ddr->dram_dqm7;
840         }
841 }
842 #endif
843
844 #if defined(CONFIG_MX6QDL) || defined(CONFIG_MX6DL) || defined(CONFIG_MX6S)
845 /* Configure MX6SDL mmdc iomux */
846 void mx6sdl_dram_iocfg(unsigned width,
847                        const struct mx6sdl_iomux_ddr_regs *ddr,
848                        const struct mx6sdl_iomux_grp_regs *grp)
849 {
850         volatile struct mx6sdl_iomux_ddr_regs *mx6_ddr_iomux;
851         volatile struct mx6sdl_iomux_grp_regs *mx6_grp_iomux;
852
853         mx6_ddr_iomux = (struct mx6sdl_iomux_ddr_regs *)MX6SDL_IOM_DDR_BASE;
854         mx6_grp_iomux = (struct mx6sdl_iomux_grp_regs *)MX6SDL_IOM_GRP_BASE;
855
856         /* DDR IO Type */
857         mx6_grp_iomux->grp_ddr_type = grp->grp_ddr_type;
858         mx6_grp_iomux->grp_ddrpke = grp->grp_ddrpke;
859
860         /* Clock */
861         mx6_ddr_iomux->dram_sdclk_0 = ddr->dram_sdclk_0;
862         mx6_ddr_iomux->dram_sdclk_1 = ddr->dram_sdclk_1;
863
864         /* Address */
865         mx6_ddr_iomux->dram_cas = ddr->dram_cas;
866         mx6_ddr_iomux->dram_ras = ddr->dram_ras;
867         mx6_grp_iomux->grp_addds = grp->grp_addds;
868
869         /* Control */
870         mx6_ddr_iomux->dram_reset = ddr->dram_reset;
871         mx6_ddr_iomux->dram_sdcke0 = ddr->dram_sdcke0;
872         mx6_ddr_iomux->dram_sdcke1 = ddr->dram_sdcke1;
873         mx6_ddr_iomux->dram_sdba2 = ddr->dram_sdba2;
874         mx6_ddr_iomux->dram_sdodt0 = ddr->dram_sdodt0;
875         mx6_ddr_iomux->dram_sdodt1 = ddr->dram_sdodt1;
876         mx6_grp_iomux->grp_ctlds = grp->grp_ctlds;
877
878         /* Data Strobes */
879         mx6_grp_iomux->grp_ddrmode_ctl = grp->grp_ddrmode_ctl;
880         mx6_ddr_iomux->dram_sdqs0 = ddr->dram_sdqs0;
881         mx6_ddr_iomux->dram_sdqs1 = ddr->dram_sdqs1;
882         if (width >= 32) {
883                 mx6_ddr_iomux->dram_sdqs2 = ddr->dram_sdqs2;
884                 mx6_ddr_iomux->dram_sdqs3 = ddr->dram_sdqs3;
885         }
886         if (width >= 64) {
887                 mx6_ddr_iomux->dram_sdqs4 = ddr->dram_sdqs4;
888                 mx6_ddr_iomux->dram_sdqs5 = ddr->dram_sdqs5;
889                 mx6_ddr_iomux->dram_sdqs6 = ddr->dram_sdqs6;
890                 mx6_ddr_iomux->dram_sdqs7 = ddr->dram_sdqs7;
891         }
892
893         /* Data */
894         mx6_grp_iomux->grp_ddrmode = grp->grp_ddrmode;
895         mx6_grp_iomux->grp_b0ds = grp->grp_b0ds;
896         mx6_grp_iomux->grp_b1ds = grp->grp_b1ds;
897         if (width >= 32) {
898                 mx6_grp_iomux->grp_b2ds = grp->grp_b2ds;
899                 mx6_grp_iomux->grp_b3ds = grp->grp_b3ds;
900         }
901         if (width >= 64) {
902                 mx6_grp_iomux->grp_b4ds = grp->grp_b4ds;
903                 mx6_grp_iomux->grp_b5ds = grp->grp_b5ds;
904                 mx6_grp_iomux->grp_b6ds = grp->grp_b6ds;
905                 mx6_grp_iomux->grp_b7ds = grp->grp_b7ds;
906         }
907         mx6_ddr_iomux->dram_dqm0 = ddr->dram_dqm0;
908         mx6_ddr_iomux->dram_dqm1 = ddr->dram_dqm1;
909         if (width >= 32) {
910                 mx6_ddr_iomux->dram_dqm2 = ddr->dram_dqm2;
911                 mx6_ddr_iomux->dram_dqm3 = ddr->dram_dqm3;
912         }
913         if (width >= 64) {
914                 mx6_ddr_iomux->dram_dqm4 = ddr->dram_dqm4;
915                 mx6_ddr_iomux->dram_dqm5 = ddr->dram_dqm5;
916                 mx6_ddr_iomux->dram_dqm6 = ddr->dram_dqm6;
917                 mx6_ddr_iomux->dram_dqm7 = ddr->dram_dqm7;
918         }
919 }
920 #endif
921
922 /*
923  * Configure mx6 mmdc registers based on:
924  *  - board-specific memory configuration
925  *  - board-specific calibration data
926  *  - ddr3/lpddr2 chip details
927  *
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
931  *    parameters.
932  *
933  * 2. i.Mx6SL LPDDR2 Script Aid spreadsheet V0.04 designed to generate MMDC
934  *    configuration registers based on memory system and memory chip
935  *    parameters.
936  *
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.
940  */
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;                                     \
946         } while (0)
947
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
951
952 #define DDR_MMAP_NOC_SINGLE     0
953 #define DDR_MMAP_NOC_DUAL       0x31
954
955 /* NoC ACTIVATE shifts */
956 #define NOC_RD_SHIFT            0
957 #define NOC_FAW_PERIOD_SHIFT    4
958 #define NOC_FAW_BANKS_SHIFT     10
959
960 /* NoC DdrTiming shifts */
961 #define NOC_ACT_TO_ACT_SHIFT    0
962 #define NOC_RD_TO_MISS_SHIFT    6
963 #define NOC_WR_TO_MISS_SHIFT    12
964 #define NOC_BURST_LEN_SHIFT     18
965 #define NOC_RD_TO_WR_SHIFT      21
966 #define NOC_WR_TO_RD_SHIFT      26
967 #define NOC_BW_RATIO_SHIFT      31
968
969 /*
970  * According JESD209-2B-LPDDR2: Table 103
971  * WL: write latency
972  */
973 static int lpddr2_wl(uint32_t mem_speed)
974 {
975         switch (mem_speed) {
976         case 1066:
977         case 933:
978                 return 4;
979         case 800:
980                 return 3;
981         case 677:
982         case 533:
983                 return 2;
984         case 400:
985         case 333:
986                 return 1;
987         default:
988                 puts("invalid memory speed\n");
989                 hang();
990         }
991
992         return 0;
993 }
994
995 /*
996  * According JESD209-2B-LPDDR2: Table 103
997  * RL: read latency
998  */
999 static int lpddr2_rl(uint32_t mem_speed)
1000 {
1001         switch (mem_speed) {
1002         case 1066:
1003                 return 8;
1004         case 933:
1005                 return 7;
1006         case 800:
1007                 return 6;
1008         case 677:
1009                 return 5;
1010         case 533:
1011                 return 4;
1012         case 400:
1013         case 333:
1014                 return 3;
1015         default:
1016                 puts("invalid memory speed\n");
1017                 hang();
1018         }
1019
1020         return 0;
1021 }
1022
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)
1026 {
1027         volatile struct mmdc_p_regs *mmdc0;
1028         u32 val;
1029         u8 tcke, tcksrx, tcksre, trrd;
1030         u8 twl, txp, tfaw, tcl;
1031         u16 tras, twr, tmrd, trtp, twtr, trfc, txsr;
1032         u16 trcd_lp, trppb_lp, trpab_lp, trc_lp;
1033         u16 cs0_end;
1034         u8 coladdr;
1035         int clkper; /* clock period in picoseconds */
1036         int clock;  /* clock freq in mHz */
1037         int cs;
1038
1039         /* only support 16/32 bits */
1040         if (sysinfo->dsize > 1)
1041                 hang();
1042
1043         mmdc0 = (struct mmdc_p_regs *)MMDC_P0_BASE_ADDR;
1044
1045         clock = mxc_get_clock(MXC_DDR_CLK) / 1000000U;
1046         clkper = (1000 * 1000) / clock; /* pico seconds */
1047
1048         twl = lpddr2_wl(lpddr2_cfg->mem_speed) - 1;
1049
1050         /* LPDDR2-S2 and LPDDR2-S4 have the same tRFC value. */
1051         switch (lpddr2_cfg->density) {
1052         case 1:
1053         case 2:
1054         case 4:
1055                 trfc = DIV_ROUND_UP(130000, clkper) - 1;
1056                 txsr = DIV_ROUND_UP(140000, clkper) - 1;
1057                 break;
1058         case 8:
1059                 trfc = DIV_ROUND_UP(210000, clkper) - 1;
1060                 txsr = DIV_ROUND_UP(220000, clkper) - 1;
1061                 break;
1062         default:
1063                 /*
1064                  * 64Mb, 128Mb, 256Mb, 512Mb are not supported currently.
1065                  */
1066                 hang();
1067                 break;
1068         }
1069         /*
1070          * txpdll, txpr, taonpd and taofpd are not relevant in LPDDR2 mode,
1071          * set them to 0. */
1072         txp = DIV_ROUND_UP(7500, clkper) - 1;
1073         tcke = 3;
1074         if (lpddr2_cfg->mem_speed == 333)
1075                 tfaw = DIV_ROUND_UP(60000, clkper) - 1;
1076         else
1077                 tfaw = DIV_ROUND_UP(50000, clkper) - 1;
1078         trrd = DIV_ROUND_UP(10000, clkper) - 1;
1079
1080         /* tckesr for LPDDR2 */
1081         tcksre = DIV_ROUND_UP(15000, clkper);
1082         tcksrx = tcksre;
1083         twr  = DIV_ROUND_UP(15000, clkper) - 1;
1084         /*
1085          * tMRR: 2, tMRW: 5
1086          * tMRD should be set to max(tMRR, tMRW)
1087          */
1088         tmrd = 5;
1089         tras = DIV_ROUND_UP(lpddr2_cfg->trasmin, clkper / 10) - 1;
1090         /* LPDDR2 mode use tRCD_LP filed in MDCFG3. */
1091         trcd_lp = DIV_ROUND_UP(lpddr2_cfg->trcd_lp, clkper / 10) - 1;
1092         trc_lp = DIV_ROUND_UP(lpddr2_cfg->trasmin + lpddr2_cfg->trppb_lp,
1093                               clkper / 10) - 1;
1094         trppb_lp = DIV_ROUND_UP(lpddr2_cfg->trppb_lp, clkper / 10) - 1;
1095         trpab_lp = DIV_ROUND_UP(lpddr2_cfg->trpab_lp, clkper / 10) - 1;
1096         /* To LPDDR2, CL in MDCFG0 refers to RL */
1097         tcl = lpddr2_rl(lpddr2_cfg->mem_speed) - 3;
1098         twtr = DIV_ROUND_UP(7500, clkper) - 1;
1099         trtp = DIV_ROUND_UP(7500, clkper) - 1;
1100
1101         cs0_end = 4 * sysinfo->cs_density - 1;
1102
1103         debug("density:%d Gb (%d Gb per chip)\n",
1104               sysinfo->cs_density, lpddr2_cfg->density);
1105         debug("clock: %dMHz (%d ps)\n", clock, clkper);
1106         debug("memspd:%d\n", lpddr2_cfg->mem_speed);
1107         debug("trcd_lp=%d\n", trcd_lp);
1108         debug("trppb_lp=%d\n", trppb_lp);
1109         debug("trpab_lp=%d\n", trpab_lp);
1110         debug("trc_lp=%d\n", trc_lp);
1111         debug("tcke=%d\n", tcke);
1112         debug("tcksrx=%d\n", tcksrx);
1113         debug("tcksre=%d\n", tcksre);
1114         debug("trfc=%d\n", trfc);
1115         debug("txsr=%d\n", txsr);
1116         debug("txp=%d\n", txp);
1117         debug("tfaw=%d\n", tfaw);
1118         debug("tcl=%d\n", tcl);
1119         debug("tras=%d\n", tras);
1120         debug("twr=%d\n", twr);
1121         debug("tmrd=%d\n", tmrd);
1122         debug("twl=%d\n", twl);
1123         debug("trtp=%d\n", trtp);
1124         debug("twtr=%d\n", twtr);
1125         debug("trrd=%d\n", trrd);
1126         debug("cs0_end=%d\n", cs0_end);
1127         debug("ncs=%d\n", sysinfo->ncs);
1128
1129         /*
1130          * board-specific configuration:
1131          *  These values are determined empirically and vary per board layout
1132          */
1133         mmdc0->mpwldectrl0 = calib->p0_mpwldectrl0;
1134         mmdc0->mpwldectrl1 = calib->p0_mpwldectrl1;
1135         mmdc0->mpdgctrl0 = calib->p0_mpdgctrl0;
1136         mmdc0->mpdgctrl1 = calib->p0_mpdgctrl1;
1137         mmdc0->mprddlctl = calib->p0_mprddlctl;
1138         mmdc0->mpwrdlctl = calib->p0_mpwrdlctl;
1139         mmdc0->mpzqlp2ctl = calib->mpzqlp2ctl;
1140
1141         /* Read data DQ Byte0-3 delay */
1142         mmdc0->mprddqby0dl = 0x33333333;
1143         mmdc0->mprddqby1dl = 0x33333333;
1144         if (sysinfo->dsize > 0) {
1145                 mmdc0->mprddqby2dl = 0x33333333;
1146                 mmdc0->mprddqby3dl = 0x33333333;
1147         }
1148
1149         /* Write data DQ Byte0-3 delay */
1150         mmdc0->mpwrdqby0dl = 0xf3333333;
1151         mmdc0->mpwrdqby1dl = 0xf3333333;
1152         if (sysinfo->dsize > 0) {
1153                 mmdc0->mpwrdqby2dl = 0xf3333333;
1154                 mmdc0->mpwrdqby3dl = 0xf3333333;
1155         }
1156
1157         /*
1158          * In LPDDR2 mode this register should be cleared,
1159          * so no termination will be activated.
1160          */
1161         mmdc0->mpodtctrl = 0;
1162
1163         /* complete calibration */
1164         val = (1 << 11); /* Force measurement on delay-lines */
1165         mmdc0->mpmur0 = val;
1166
1167         /* Step 1: configuration request */
1168         mmdc0->mdscr = (u32)(1 << 15); /* config request */
1169
1170         /* Step 2: Timing configuration */
1171         mmdc0->mdcfg0 = (trfc << 24) | (txsr << 16) | (txp << 13) |
1172                         (tfaw << 4) | tcl;
1173         mmdc0->mdcfg1 = (tras << 16) | (twr << 9) | (tmrd << 5) | twl;
1174         mmdc0->mdcfg2 = (trtp << 6) | (twtr << 3) | trrd;
1175         mmdc0->mdcfg3lp = (trc_lp << 16) | (trcd_lp << 8) |
1176                           (trppb_lp << 4) | trpab_lp;
1177         mmdc0->mdotc = 0;
1178
1179         mmdc0->mdasp = cs0_end; /* CS addressing */
1180
1181         /* Step 3: Configure DDR type */
1182         mmdc0->mdmisc = (sysinfo->cs1_mirror << 19) | (sysinfo->walat << 16) |
1183                         (sysinfo->bi_on << 12) | (sysinfo->mif3_mode << 9) |
1184                         (sysinfo->ralat << 6) | (1 << 3);
1185
1186         /* Step 4: Configure delay while leaving reset */
1187         mmdc0->mdor = (sysinfo->sde_to_rst << 8) |
1188                       (sysinfo->rst_to_cke << 0);
1189
1190         /* Step 5: Configure DDR physical parameters (density and burst len) */
1191         coladdr = lpddr2_cfg->coladdr;
1192         if (lpddr2_cfg->coladdr == 8)           /* 8-bit COL is 0x3 */
1193                 coladdr += 4;
1194         else if (lpddr2_cfg->coladdr == 12)     /* 12-bit COL is 0x4 */
1195                 coladdr += 1;
1196         mmdc0->mdctl =  (lpddr2_cfg->rowaddr - 11) << 24 |      /* ROW */
1197                         (coladdr - 9) << 20 |                   /* COL */
1198                         (0 << 19) |     /* Burst Length = 4 for LPDDR2 */
1199                         (sysinfo->dsize << 16); /* DDR data bus size */
1200
1201         /* Step 6: Perform ZQ calibration */
1202         val = 0xa1390003; /* one-time HW ZQ calib */
1203         mmdc0->mpzqhwctrl = val;
1204
1205         /* Step 7: Enable MMDC with desired chip select */
1206         mmdc0->mdctl |= (1 << 31) |                          /* SDE_0 for CS0 */
1207                         ((sysinfo->ncs == 2) ? 1 : 0) << 30; /* SDE_1 for CS1 */
1208
1209         /* Step 8: Write Mode Registers to Init LPDDR2 devices */
1210         for (cs = 0; cs < sysinfo->ncs; cs++) {
1211                 /* MR63: reset */
1212                 mmdc0->mdscr = MR(63, 0, 3, cs);
1213                 /* MR10: calibration,
1214                  * 0xff is calibration command after intilization.
1215                  */
1216                 val = 0xA | (0xff << 8);
1217                 mmdc0->mdscr = MR(val, 0, 3, cs);
1218                 /* MR1 */
1219                 val = 0x1 | (0x82 << 8);
1220                 mmdc0->mdscr = MR(val, 0, 3, cs);
1221                 /* MR2 */
1222                 val = 0x2 | (0x04 << 8);
1223                 mmdc0->mdscr = MR(val, 0, 3, cs);
1224                 /* MR3 */
1225                 val = 0x3 | (0x02 << 8);
1226                 mmdc0->mdscr = MR(val, 0, 3, cs);
1227         }
1228
1229         /* Step 10: Power down control and self-refresh */
1230         mmdc0->mdpdc = (tcke & 0x7) << 16 |
1231                         5            << 12 |  /* PWDT_1: 256 cycles */
1232                         5            <<  8 |  /* PWDT_0: 256 cycles */
1233                         1            <<  6 |  /* BOTH_CS_PD */
1234                         (tcksrx & 0x7) << 3 |
1235                         (tcksre & 0x7);
1236         mmdc0->mapsr = 0x00001006; /* ADOPT power down enabled */
1237
1238         /* Step 11: Configure ZQ calibration: one-time and periodic 1ms */
1239         val = 0xa1310003;
1240         mmdc0->mpzqhwctrl = val;
1241
1242         /* Step 12: Configure and activate periodic refresh */
1243         mmdc0->mdref = (sysinfo->refsel << 14) | (sysinfo->refr << 11);
1244
1245         /* Step 13: Deassert config request - init complete */
1246         mmdc0->mdscr = 0x00000000;
1247
1248         /* wait for auto-ZQ calibration to complete */
1249         mdelay(1);
1250 }
1251
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)
1255 {
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;
1260         u32 val;
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;
1265         u16 cs0_end;
1266         u16 tdllk = 0x1ff; /* DLL locking time: 512 cycles (JEDEC DDR3) */
1267         u8 coladdr;
1268         int clkper; /* clock period in picoseconds */
1269         int clock; /* clock freq in MHz */
1270         int cs;
1271         u16 mem_speed = ddr3_cfg->mem_speed;
1272
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;
1276
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 */
1281
1282                 tcwl = 4;
1283         }
1284         /* Limit mem_speed for MX6S/MX6DL */
1285         else {
1286                 if (mem_speed > 800)
1287                         mem_speed = 800;  /* 800 MT/s */
1288
1289                 tcwl = 3;
1290         }
1291
1292         clock = mem_speed / 2;
1293         /*
1294          * Data rate of 1066 MT/s requires 533 MHz DDR3 clock, but MX6D/Q supports
1295          * up to 528 MHz, so reduce the clock to fit chip specs
1296          */
1297         if (is_mx6dq() || is_mx6dqp()) {
1298                 if (clock > 528)
1299                         clock = 528; /* 528 MHz */
1300         }
1301
1302         clkper = (1000 * 1000) / clock; /* pico seconds */
1303         todtlon = tcwl;
1304         taxpd = tcwl;
1305         tanpd = tcwl;
1306
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;
1311                 break;
1312         case 2: /* 2Gb per chip */
1313                 trfc = DIV_ROUND_UP(160000, clkper) - 1;
1314                 txs = DIV_ROUND_UP(170000, clkper) - 1;
1315                 break;
1316         case 4: /* 4Gb per chip */
1317                 trfc = DIV_ROUND_UP(260000, clkper) - 1;
1318                 txs = DIV_ROUND_UP(270000, clkper) - 1;
1319                 break;
1320         case 8: /* 8Gb per chip */
1321                 trfc = DIV_ROUND_UP(350000, clkper) - 1;
1322                 txs = DIV_ROUND_UP(360000, clkper) - 1;
1323                 break;
1324         default:
1325                 /* invalid density */
1326                 puts("invalid chip density\n");
1327                 hang();
1328                 break;
1329         }
1330         txpr = txs;
1331
1332         switch (mem_speed) {
1333         case 800:
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;
1339                 } else {
1340                         tfaw = DIV_ROUND_UP(50000, clkper) - 1;
1341                         trrd = DIV_ROUND_UP(max(4 * clkper, 10000), clkper) - 1;
1342                 }
1343                 break;
1344         case 1066:
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;
1350                 } else {
1351                         tfaw = DIV_ROUND_UP(50000, clkper) - 1;
1352                         trrd = DIV_ROUND_UP(max(4 * clkper, 10000), clkper) - 1;
1353                 }
1354                 break;
1355         default:
1356                 puts("invalid memory speed\n");
1357                 hang();
1358                 break;
1359         }
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;
1363         tcksrx = tcksre;
1364         taofpd = taonpd;
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;
1372         trcd = trp;
1373         trtp = twtr;
1374         cs0_end = 4 * sysinfo->cs_density - 1;
1375
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);
1412
1413         /*
1414          * board-specific configuration:
1415          *  These values are determined empirically and vary per board layout
1416          *  see:
1417          *   appnote, ddr3 spreadsheet
1418          */
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);
1432         }
1433
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;
1440         }
1441
1442         if (sysinfo->dsize > 1) {
1443                 MMDC1(mprddqby0dl, 0x33333333);
1444                 MMDC1(mprddqby1dl, 0x33333333);
1445                 MMDC1(mprddqby2dl, 0x33333333);
1446                 MMDC1(mprddqby3dl, 0x33333333);
1447         }
1448
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);
1454
1455         /* complete calibration */
1456         val = (1 << 11); /* Force measurement on delay-lines */
1457         mmdc0->mpmur0 = val;
1458         if (sysinfo->dsize > 1)
1459                 MMDC1(mpmur0, val);
1460
1461         /* Step 1: configuration request */
1462         mmdc0->mdscr = (u32)(1 << 15); /* config request */
1463
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 */
1474
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);
1479
1480         /* Step 4: Configure delay while leaving reset */
1481         mmdc0->mdor = (txpr << 16) | (sysinfo->sde_to_rst << 8) |
1482                       (sysinfo->rst_to_cke << 0);
1483
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 */
1487                 coladdr += 4;
1488         else if (ddr3_cfg->coladdr == 12)       /* 12-bit COL is 0x4 */
1489                 coladdr += 1;
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 */
1494
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);
1500
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 */
1504
1505         /* Step 8: Write Mode Registers to Init DDR3 devices */
1506         for (cs = 0; cs < sysinfo->ncs; cs++) {
1507                 /* MR2 */
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);
1512                 /* MR3 */
1513                 debug("MR3 CS%d: 0x%08x\n", cs, (u32)MR(0, 3, 3, cs));
1514                 mmdc0->mdscr = MR(0, 3, 3, cs);
1515                 /* MR1 */
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);
1520                 /* MR0 */
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 */
1528                 val = (1 << 10);
1529                 mmdc0->mdscr = MR(val, 0, 4, cs);
1530         }
1531
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 |
1538                         (tcksre & 0x7);
1539         if (!sysinfo->pd_fast_exit)
1540                 mmdc0->mdpdc |= (1 << 7); /* SLOW_PD */
1541         mmdc0->mapsr = 0x00001006; /* ADOPT power down enabled */
1542
1543         /* Step 11: Configure ZQ calibration: one-time and periodic 1ms */
1544         val = 0xa1390003;
1545         mmdc0->mpzqhwctrl = val;
1546         if (sysinfo->dsize > 1)
1547                 MMDC1(mpzqhwctrl, val);
1548
1549         /* Step 12: Configure and activate periodic refresh */
1550         mmdc0->mdref = (sysinfo->refsel << 14) | (sysinfo->refr << 11);
1551
1552         /*
1553          * Step 13: i.MX6DQP only: If the NoC scheduler is enabled,
1554          * configure it and disable MMDC arbitration/reordering (see EB828)
1555          */
1556         if (is_mx6dqp() &&
1557             ((soc_boot_cfg3 & BOOT_CFG3_DDR_MASK) == DDR_MMAP_NOC_SINGLE ||
1558             (soc_boot_cfg3 & BOOT_CFG3_EXT_DDR_MASK) == DDR_MMAP_NOC_DUAL)) {
1559                 struct mx6dqp_noc_sched_regs *noc_sched =
1560                         (struct mx6dqp_noc_sched_regs *)MX6DQP_NOC_SCHED_BASE;
1561
1562                 /*
1563                  * These values are fixed based on integration parameters and
1564                  * should not be modified
1565                  */
1566                 noc_sched->rlat = 0x00000040;
1567                 noc_sched->ipu1 = 0x00000020;
1568                 noc_sched->ipu2 = 0x00000020;
1569
1570                 noc_sched->activate = (1 << NOC_FAW_BANKS_SHIFT) |
1571                                       (tfaw << NOC_FAW_PERIOD_SHIFT) |
1572                                       (trrd << NOC_RD_SHIFT);
1573                 noc_sched->ddrtiming = (((sysinfo->dsize == 1) ? 1 : 0)
1574                                          << NOC_BW_RATIO_SHIFT) |
1575                                        ((tcwl + twtr) << NOC_WR_TO_RD_SHIFT) |
1576                                        ((tcl - tcwl + 2) << NOC_RD_TO_WR_SHIFT) |
1577                                        (4 << NOC_BURST_LEN_SHIFT) | /* BL8 */
1578                                        ((tcwl + twr + trp + trcd)
1579                                          << NOC_WR_TO_MISS_SHIFT) |
1580                                        ((trtp + trp + trcd - 4)
1581                                          << NOC_RD_TO_MISS_SHIFT) |
1582                                        (trc << NOC_ACT_TO_ACT_SHIFT);
1583
1584                 if (sysinfo->dsize == 2) {
1585                         if (ddr3_cfg->coladdr == 10) {
1586                                 if (ddr3_cfg->rowaddr == 15 &&
1587                                     sysinfo->ncs == 2)
1588                                         noc_sched->ddrconf = 4;
1589                                 else
1590                                         noc_sched->ddrconf = 0;
1591                         } else if (ddr3_cfg->coladdr == 11) {
1592                                 noc_sched->ddrconf = 1;
1593                         }
1594                 } else {
1595                         if (ddr3_cfg->coladdr == 9) {
1596                                 if (ddr3_cfg->rowaddr == 13)
1597                                         noc_sched->ddrconf = 2;
1598                                 else if (ddr3_cfg->rowaddr == 14)
1599                                         noc_sched->ddrconf = 15;
1600                         } else if (ddr3_cfg->coladdr == 10) {
1601                                 if (ddr3_cfg->rowaddr == 14 &&
1602                                     sysinfo->ncs == 2)
1603                                         noc_sched->ddrconf = 14;
1604                                 else if (ddr3_cfg->rowaddr == 15 &&
1605                                          sysinfo->ncs == 2)
1606                                         noc_sched->ddrconf = 9;
1607                                 else
1608                                         noc_sched->ddrconf = 3;
1609                         } else if (ddr3_cfg->coladdr == 11) {
1610                                 if (ddr3_cfg->rowaddr == 15 &&
1611                                     sysinfo->ncs == 2)
1612                                         noc_sched->ddrconf = 4;
1613                                 else
1614                                         noc_sched->ddrconf = 0;
1615                         } else if (ddr3_cfg->coladdr == 12) {
1616                                 if (ddr3_cfg->rowaddr == 14)
1617                                         noc_sched->ddrconf = 1;
1618                         }
1619                 }
1620
1621                 /* Disable MMDC arbitration/reordering */
1622                 mmdc0->maarcr = 0x14420000;
1623         }
1624
1625         /* Step 13: Deassert config request - init complete */
1626         mmdc0->mdscr = 0x00000000;
1627
1628         /* wait for auto-ZQ calibration to complete */
1629         mdelay(1);
1630 }
1631
1632 void mmdc_read_calibration(struct mx6_ddr_sysinfo const *sysinfo,
1633                            struct mx6_mmdc_calibration *calib)
1634 {
1635         struct mmdc_p_regs *mmdc0 = (struct mmdc_p_regs *)MMDC_P0_BASE_ADDR;
1636         struct mmdc_p_regs *mmdc1 = (struct mmdc_p_regs *)MMDC_P1_BASE_ADDR;
1637
1638         calib->p0_mpwldectrl0 = readl(&mmdc0->mpwldectrl0);
1639         calib->p0_mpwldectrl1 = readl(&mmdc0->mpwldectrl1);
1640         calib->p0_mpdgctrl0 = readl(&mmdc0->mpdgctrl0);
1641         calib->p0_mpdgctrl1 = readl(&mmdc0->mpdgctrl1);
1642         calib->p0_mprddlctl = readl(&mmdc0->mprddlctl);
1643         calib->p0_mpwrdlctl = readl(&mmdc0->mpwrdlctl);
1644
1645         if (sysinfo->dsize == 2) {
1646                 calib->p1_mpwldectrl0 = readl(&mmdc1->mpwldectrl0);
1647                 calib->p1_mpwldectrl1 = readl(&mmdc1->mpwldectrl1);
1648                 calib->p1_mpdgctrl0 = readl(&mmdc1->mpdgctrl0);
1649                 calib->p1_mpdgctrl1 = readl(&mmdc1->mpdgctrl1);
1650                 calib->p1_mprddlctl = readl(&mmdc1->mprddlctl);
1651                 calib->p1_mpwrdlctl = readl(&mmdc1->mpwrdlctl);
1652         }
1653 }
1654
1655 void mx6_dram_cfg(const struct mx6_ddr_sysinfo *sysinfo,
1656                   const struct mx6_mmdc_calibration *calib,
1657                   const void *ddr_cfg)
1658 {
1659         if (sysinfo->ddr_type == DDR_TYPE_DDR3) {
1660                 mx6_ddr3_cfg(sysinfo, calib, ddr_cfg);
1661         } else if (sysinfo->ddr_type == DDR_TYPE_LPDDR2) {
1662                 mx6_lpddr2_cfg(sysinfo, calib, ddr_cfg);
1663         } else {
1664                 puts("Unsupported ddr type\n");
1665                 hang();
1666         }
1667 }