blob: e457febf678b102ef29e90182ed8db54a942a5be [file] [log] [blame]
Tim Harvey8ab871b2014-06-02 16:13:23 -07001/*
2 * Copyright (C) 2014 Gateworks Corporation
3 * Author: Tim Harvey <tharvey@gateworks.com>
4 *
5 * SPDX-License-Identifier: GPL-2.0+
6 */
7
8#include <common.h>
9#include <linux/types.h>
Peng Fanda7ada02015-08-17 16:11:04 +080010#include <asm/arch/clock.h>
Tim Harvey8ab871b2014-06-02 16:13:23 -070011#include <asm/arch/mx6-ddr.h>
12#include <asm/arch/sys_proto.h>
13#include <asm/io.h>
14#include <asm/types.h>
15
Marek Vasutab257ed2015-12-16 15:40:06 +010016#if defined(CONFIG_MX6QDL) || defined(CONFIG_MX6Q) || defined(CONFIG_MX6D)
17
18static int wait_for_bit(void *reg, const uint32_t mask, bool set)
19{
20 unsigned int timeout = 1000;
21 u32 val;
22
23 while (--timeout) {
24 val = readl(reg);
25 if (!set)
26 val = ~val;
27
28 if ((val & mask) == mask)
29 return 0;
30
31 udelay(1);
32 }
33
34 printf("%s: Timeout (reg=%p mask=%08x wait_set=%i)\n",
35 __func__, reg, mask, set);
36 hang(); /* DRAM couldn't be calibrated, game over :-( */
37}
38
39static void reset_read_data_fifos(void)
40{
41 struct mmdc_p_regs *mmdc0 = (struct mmdc_p_regs *)MMDC_P0_BASE_ADDR;
42
43 /* Reset data FIFOs twice. */
44 setbits_le32(&mmdc0->mpdgctrl0, 1 << 31);
45 wait_for_bit(&mmdc0->mpdgctrl0, 1 << 31, 0);
46
47 setbits_le32(&mmdc0->mpdgctrl0, 1 << 31);
48 wait_for_bit(&mmdc0->mpdgctrl0, 1 << 31, 0);
49}
50
51static void precharge_all(const bool cs0_enable, const bool cs1_enable)
52{
53 struct mmdc_p_regs *mmdc0 = (struct mmdc_p_regs *)MMDC_P0_BASE_ADDR;
54
55 /*
56 * Issue the Precharge-All command to the DDR device for both
57 * chip selects. Note, CON_REQ bit should also remain set. If
58 * only using one chip select, then precharge only the desired
59 * chip select.
60 */
61 if (cs0_enable) { /* CS0 */
62 writel(0x04008050, &mmdc0->mdscr);
63 wait_for_bit(&mmdc0->mdscr, 1 << 14, 1);
64 }
65
66 if (cs1_enable) { /* CS1 */
67 writel(0x04008058, &mmdc0->mdscr);
68 wait_for_bit(&mmdc0->mdscr, 1 << 14, 1);
69 }
70}
71
72static void force_delay_measurement(int bus_size)
73{
74 struct mmdc_p_regs *mmdc0 = (struct mmdc_p_regs *)MMDC_P0_BASE_ADDR;
75 struct mmdc_p_regs *mmdc1 = (struct mmdc_p_regs *)MMDC_P1_BASE_ADDR;
76
77 writel(0x800, &mmdc0->mpmur0);
78 if (bus_size == 0x2)
79 writel(0x800, &mmdc1->mpmur0);
80}
81
82static void modify_dg_result(u32 *reg_st0, u32 *reg_st1, u32 *reg_ctrl)
83{
84 u32 dg_tmp_val, dg_dl_abs_offset, dg_hc_del, val_ctrl;
85
86 /*
87 * DQS gating absolute offset should be modified from reflecting
88 * (HW_DG_LOWx + HW_DG_UPx)/2 to reflecting (HW_DG_UPx - 0x80)
89 */
90
91 val_ctrl = readl(reg_ctrl);
92 val_ctrl &= 0xf0000000;
93
94 dg_tmp_val = ((readl(reg_st0) & 0x07ff0000) >> 16) - 0xc0;
95 dg_dl_abs_offset = dg_tmp_val & 0x7f;
96 dg_hc_del = (dg_tmp_val & 0x780) << 1;
97
98 val_ctrl |= dg_dl_abs_offset + dg_hc_del;
99
100 dg_tmp_val = ((readl(reg_st1) & 0x07ff0000) >> 16) - 0xc0;
101 dg_dl_abs_offset = dg_tmp_val & 0x7f;
102 dg_hc_del = (dg_tmp_val & 0x780) << 1;
103
104 val_ctrl |= (dg_dl_abs_offset + dg_hc_del) << 16;
105
106 writel(val_ctrl, reg_ctrl);
107}
108
109int mmdc_do_write_level_calibration(void)
110{
111 struct mmdc_p_regs *mmdc0 = (struct mmdc_p_regs *)MMDC_P0_BASE_ADDR;
112 struct mmdc_p_regs *mmdc1 = (struct mmdc_p_regs *)MMDC_P1_BASE_ADDR;
113 u32 esdmisc_val, zq_val;
114 u32 errors = 0;
115 u32 ldectrl[4];
116 u32 ddr_mr1 = 0x4;
117
118 /*
119 * Stash old values in case calibration fails,
120 * we need to restore them
121 */
122 ldectrl[0] = readl(&mmdc0->mpwldectrl0);
123 ldectrl[1] = readl(&mmdc0->mpwldectrl1);
124 ldectrl[2] = readl(&mmdc1->mpwldectrl0);
125 ldectrl[3] = readl(&mmdc1->mpwldectrl1);
126
127 /* disable DDR logic power down timer */
128 clrbits_le32(&mmdc0->mdpdc, 0xff00);
129
130 /* disable Adopt power down timer */
131 setbits_le32(&mmdc0->mapsr, 0x1);
132
133 debug("Starting write leveling calibration.\n");
134
135 /*
136 * 2. disable auto refresh and ZQ calibration
137 * before proceeding with Write Leveling calibration
138 */
139 esdmisc_val = readl(&mmdc0->mdref);
140 writel(0x0000C000, &mmdc0->mdref);
141 zq_val = readl(&mmdc0->mpzqhwctrl);
142 writel(zq_val & ~0x3, &mmdc0->mpzqhwctrl);
143
144 /* 3. increase walat and ralat to maximum */
145 setbits_le32(&mmdc0->mdmisc,
146 (1 << 6) | (1 << 7) | (1 << 8) | (1 << 16) | (1 << 17));
147 setbits_le32(&mmdc1->mdmisc,
148 (1 << 6) | (1 << 7) | (1 << 8) | (1 << 16) | (1 << 17));
149 /*
150 * 4 & 5. Configure the external DDR device to enter write-leveling
151 * mode through Load Mode Register command.
152 * Register setting:
153 * Bits[31:16] MR1 value (0x0080 write leveling enable)
154 * Bit[9] set WL_EN to enable MMDC DQS output
155 * Bits[6:4] set CMD bits for Load Mode Register programming
156 * Bits[2:0] set CMD_BA to 0x1 for DDR MR1 programming
157 */
158 writel(0x00808231, &mmdc0->mdscr);
159
160 /* 6. Activate automatic calibration by setting MPWLGCR[HW_WL_EN] */
161 writel(0x00000001, &mmdc0->mpwlgcr);
162
163 /*
164 * 7. Upon completion of this process the MMDC de-asserts
165 * the MPWLGCR[HW_WL_EN]
166 */
167 wait_for_bit(&mmdc0->mpwlgcr, 1 << 0, 0);
168
169 /*
170 * 8. check for any errors: check both PHYs for x64 configuration,
171 * if x32, check only PHY0
172 */
173 if (readl(&mmdc0->mpwlgcr) & 0x00000F00)
174 errors |= 1;
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 (readl(&mmdc1->mpwldectrl0) == 0x001F001F) &&
184 (readl(&mmdc1->mpwldectrl1) == 0x001F001F)) {
185 debug("Cal seems to have soft-failed due to memory not supporting write leveling on all channels. Restoring original write leveling values.\n");
186 writel(ldectrl[0], &mmdc0->mpwldectrl0);
187 writel(ldectrl[1], &mmdc0->mpwldectrl1);
188 writel(ldectrl[2], &mmdc1->mpwldectrl0);
189 writel(ldectrl[3], &mmdc1->mpwldectrl1);
190 errors |= 4;
191 }
192
193 /*
194 * User should issue MRS command to exit write leveling mode
195 * through Load Mode Register command
196 * Register setting:
197 * Bits[31:16] MR1 value "ddr_mr1" value from initialization
198 * Bit[9] clear WL_EN to disable MMDC DQS output
199 * Bits[6:4] set CMD bits for Load Mode Register programming
200 * Bits[2:0] set CMD_BA to 0x1 for DDR MR1 programming
201 */
202 writel((ddr_mr1 << 16) + 0x8031, &mmdc0->mdscr);
203
204 /* re-enable auto refresh and zq cal */
205 writel(esdmisc_val, &mmdc0->mdref);
206 writel(zq_val, &mmdc0->mpzqhwctrl);
207
208 debug("\tMMDC_MPWLDECTRL0 after write level cal: 0x%08X\n",
209 readl(&mmdc0->mpwldectrl0));
210 debug("\tMMDC_MPWLDECTRL1 after write level cal: 0x%08X\n",
211 readl(&mmdc0->mpwldectrl1));
212 debug("\tMMDC_MPWLDECTRL0 after write level cal: 0x%08X\n",
213 readl(&mmdc1->mpwldectrl0));
214 debug("\tMMDC_MPWLDECTRL1 after write level cal: 0x%08X\n",
215 readl(&mmdc1->mpwldectrl1));
216
217 /* We must force a readback of these values, to get them to stick */
218 readl(&mmdc0->mpwldectrl0);
219 readl(&mmdc0->mpwldectrl1);
220 readl(&mmdc1->mpwldectrl0);
221 readl(&mmdc1->mpwldectrl1);
222
223 /* enable DDR logic power down timer: */
224 setbits_le32(&mmdc0->mdpdc, 0x00005500);
225
226 /* Enable Adopt power down timer: */
227 clrbits_le32(&mmdc0->mapsr, 0x1);
228
229 /* Clear CON_REQ */
230 writel(0, &mmdc0->mdscr);
231
232 return errors;
233}
234
235int mmdc_do_dqs_calibration(void)
236{
237 struct mmdc_p_regs *mmdc0 = (struct mmdc_p_regs *)MMDC_P0_BASE_ADDR;
238 struct mmdc_p_regs *mmdc1 = (struct mmdc_p_regs *)MMDC_P1_BASE_ADDR;
239 struct mx6dq_iomux_ddr_regs *mx6_ddr_iomux =
240 (struct mx6dq_iomux_ddr_regs *)MX6DQ_IOM_DDR_BASE;
241 bool cs0_enable;
242 bool cs1_enable;
243 bool cs0_enable_initial;
244 bool cs1_enable_initial;
245 u32 esdmisc_val;
246 u32 bus_size;
247 u32 temp_ref;
248 u32 pddword = 0x00ffff00; /* best so far, place into MPPDCMPR1 */
249 u32 errors = 0;
250 u32 initdelay = 0x40404040;
251
252 /* check to see which chip selects are enabled */
253 cs0_enable_initial = readl(&mmdc0->mdctl) & 0x80000000;
254 cs1_enable_initial = readl(&mmdc0->mdctl) & 0x40000000;
255
256 /* disable DDR logic power down timer: */
257 clrbits_le32(&mmdc0->mdpdc, 0xff00);
258
259 /* disable Adopt power down timer: */
260 setbits_le32(&mmdc0->mapsr, 0x1);
261
262 /* set DQS pull ups */
263 setbits_le32(&mx6_ddr_iomux->dram_sdqs0, 0x7000);
264 setbits_le32(&mx6_ddr_iomux->dram_sdqs1, 0x7000);
265 setbits_le32(&mx6_ddr_iomux->dram_sdqs2, 0x7000);
266 setbits_le32(&mx6_ddr_iomux->dram_sdqs3, 0x7000);
267 setbits_le32(&mx6_ddr_iomux->dram_sdqs4, 0x7000);
268 setbits_le32(&mx6_ddr_iomux->dram_sdqs5, 0x7000);
269 setbits_le32(&mx6_ddr_iomux->dram_sdqs6, 0x7000);
270 setbits_le32(&mx6_ddr_iomux->dram_sdqs7, 0x7000);
271
272 /* Save old RALAT and WALAT values */
273 esdmisc_val = readl(&mmdc0->mdmisc);
274
275 setbits_le32(&mmdc0->mdmisc,
276 (1 << 6) | (1 << 7) | (1 << 8) | (1 << 16) | (1 << 17));
277
278 /* Disable auto refresh before proceeding with calibration */
279 temp_ref = readl(&mmdc0->mdref);
280 writel(0x0000c000, &mmdc0->mdref);
281
282 /*
283 * Per the ref manual, issue one refresh cycle MDSCR[CMD]= 0x2,
284 * this also sets the CON_REQ bit.
285 */
286 if (cs0_enable_initial)
287 writel(0x00008020, &mmdc0->mdscr);
288 if (cs1_enable_initial)
289 writel(0x00008028, &mmdc0->mdscr);
290
291 /* poll to make sure the con_ack bit was asserted */
292 wait_for_bit(&mmdc0->mdscr, 1 << 14, 1);
293
294 /*
295 * Check MDMISC register CALIB_PER_CS to see which CS calibration
296 * is targeted to (under normal cases, it should be cleared
297 * as this is the default value, indicating calibration is directed
298 * to CS0).
299 * Disable the other chip select not being target for calibration
300 * to avoid any potential issues. This will get re-enabled at end
301 * of calibration.
302 */
303 if ((readl(&mmdc0->mdmisc) & 0x00100000) == 0)
304 clrbits_le32(&mmdc0->mdctl, 1 << 30); /* clear SDE_1 */
305 else
306 clrbits_le32(&mmdc0->mdctl, 1 << 31); /* clear SDE_0 */
307
308 /*
309 * Check to see which chip selects are now enabled for
310 * the remainder of the calibration.
311 */
312 cs0_enable = readl(&mmdc0->mdctl) & 0x80000000;
313 cs1_enable = readl(&mmdc0->mdctl) & 0x40000000;
314
315 /* Check to see what the data bus size is */
316 bus_size = (readl(&mmdc0->mdctl) & 0x30000) >> 16;
317 debug("Data bus size: %d (%d bits)\n", bus_size, 1 << (bus_size + 4));
318
319 precharge_all(cs0_enable, cs1_enable);
320
321 /* Write the pre-defined value into MPPDCMPR1 */
322 writel(pddword, &mmdc0->mppdcmpr1);
323
324 /*
325 * Issue a write access to the external DDR device by setting
326 * the bit SW_DUMMY_WR (bit 0) in the MPSWDAR0 and then poll
327 * this bit until it clears to indicate completion of the write access.
328 */
329 setbits_le32(&mmdc0->mpswdar0, 1);
330 wait_for_bit(&mmdc0->mpswdar0, 1 << 0, 0);
331
332 /* Set the RD_DL_ABS# bits to their default values
333 * (will be calibrated later in the read delay-line calibration).
334 * Both PHYs for x64 configuration, if x32, do only PHY0.
335 */
336 writel(initdelay, &mmdc0->mprddlctl);
337 if (bus_size == 0x2)
338 writel(initdelay, &mmdc1->mprddlctl);
339
340 /* Force a measurment, for previous delay setup to take effect. */
341 force_delay_measurement(bus_size);
342
343 /*
344 * ***************************
345 * Read DQS Gating calibration
346 * ***************************
347 */
348 debug("Starting Read DQS Gating calibration.\n");
349
350 /*
351 * Reset the read data FIFOs (two resets); only need to issue reset
352 * to PHY0 since in x64 mode, the reset will also go to PHY1.
353 */
354 reset_read_data_fifos();
355
356 /*
357 * Start the automatic read DQS gating calibration process by
358 * asserting MPDGCTRL0[HW_DG_EN] and MPDGCTRL0[DG_CMP_CYC]
359 * and then poll MPDGCTRL0[HW_DG_EN]] until this bit clears
360 * to indicate completion.
361 * Also, ensure that MPDGCTRL0[HW_DG_ERR] is clear to indicate
362 * no errors were seen during calibration.
363 */
364
365 /*
366 * Set bit 30: chooses option to wait 32 cycles instead of
367 * 16 before comparing read data.
368 */
369 setbits_le32(&mmdc0->mpdgctrl0, 1 << 30);
370
371 /* Set bit 28 to start automatic read DQS gating calibration */
372 setbits_le32(&mmdc0->mpdgctrl0, 5 << 28);
373
374 /* Poll for completion. MPDGCTRL0[HW_DG_EN] should be 0 */
375 wait_for_bit(&mmdc0->mpdgctrl0, 1 << 28, 0);
376
377 /*
378 * Check to see if any errors were encountered during calibration
379 * (check MPDGCTRL0[HW_DG_ERR]).
380 * Check both PHYs for x64 configuration, if x32, check only PHY0.
381 */
382 if (readl(&mmdc0->mpdgctrl0) & 0x00001000)
383 errors |= 1;
384
385 if ((bus_size == 0x2) && (readl(&mmdc1->mpdgctrl0) & 0x00001000))
386 errors |= 2;
387
388 /*
389 * DQS gating absolute offset should be modified from
390 * reflecting (HW_DG_LOWx + HW_DG_UPx)/2 to
391 * reflecting (HW_DG_UPx - 0x80)
392 */
393 modify_dg_result(&mmdc0->mpdghwst0, &mmdc0->mpdghwst1,
394 &mmdc0->mpdgctrl0);
395 modify_dg_result(&mmdc0->mpdghwst2, &mmdc0->mpdghwst3,
396 &mmdc0->mpdgctrl1);
397 if (bus_size == 0x2) {
398 modify_dg_result(&mmdc1->mpdghwst0, &mmdc1->mpdghwst1,
399 &mmdc1->mpdgctrl0);
400 modify_dg_result(&mmdc1->mpdghwst2, &mmdc1->mpdghwst3,
401 &mmdc1->mpdgctrl1);
402 }
403 debug("Ending Read DQS Gating calibration. Error mask: 0x%x\n", errors);
404
405 /*
406 * **********************
407 * Read Delay calibration
408 * **********************
409 */
410 debug("Starting Read Delay calibration.\n");
411
412 reset_read_data_fifos();
413
414 /*
415 * 4. Issue the Precharge-All command to the DDR device for both
416 * chip selects. If only using one chip select, then precharge
417 * only the desired chip select.
418 */
419 precharge_all(cs0_enable, cs1_enable);
420
421 /*
422 * 9. Read delay-line calibration
423 * Start the automatic read calibration process by asserting
424 * MPRDDLHWCTL[HW_RD_DL_EN].
425 */
426 writel(0x00000030, &mmdc0->mprddlhwctl);
427
428 /*
429 * 10. poll for completion
430 * MMDC indicates that the write data calibration had finished by
431 * setting MPRDDLHWCTL[HW_RD_DL_EN] = 0. Also, ensure that
432 * no error bits were set.
433 */
434 wait_for_bit(&mmdc0->mprddlhwctl, 1 << 4, 0);
435
436 /* check both PHYs for x64 configuration, if x32, check only PHY0 */
437 if (readl(&mmdc0->mprddlhwctl) & 0x0000000f)
438 errors |= 4;
439
440 if ((bus_size == 0x2) && (readl(&mmdc1->mprddlhwctl) & 0x0000000f))
441 errors |= 8;
442
443 debug("Ending Read Delay calibration. Error mask: 0x%x\n", errors);
444
445 /*
446 * ***********************
447 * Write Delay Calibration
448 * ***********************
449 */
450 debug("Starting Write Delay calibration.\n");
451
452 reset_read_data_fifos();
453
454 /*
455 * 4. Issue the Precharge-All command to the DDR device for both
456 * chip selects. If only using one chip select, then precharge
457 * only the desired chip select.
458 */
459 precharge_all(cs0_enable, cs1_enable);
460
461 /*
462 * 8. Set the WR_DL_ABS# bits to their default values.
463 * Both PHYs for x64 configuration, if x32, do only PHY0.
464 */
465 writel(initdelay, &mmdc0->mpwrdlctl);
466 if (bus_size == 0x2)
467 writel(initdelay, &mmdc1->mpwrdlctl);
468
469 /*
470 * XXX This isn't in the manual. Force a measurement,
471 * for previous delay setup to effect.
472 */
473 force_delay_measurement(bus_size);
474
475 /*
476 * 9. 10. Start the automatic write calibration process
477 * by asserting MPWRDLHWCTL0[HW_WR_DL_EN].
478 */
479 writel(0x00000030, &mmdc0->mpwrdlhwctl);
480
481 /*
482 * Poll for completion.
483 * MMDC indicates that the write data calibration had finished
484 * by setting MPWRDLHWCTL[HW_WR_DL_EN] = 0.
485 * Also, ensure that no error bits were set.
486 */
487 wait_for_bit(&mmdc0->mpwrdlhwctl, 1 << 4, 0);
488
489 /* Check both PHYs for x64 configuration, if x32, check only PHY0 */
490 if (readl(&mmdc0->mpwrdlhwctl) & 0x0000000f)
491 errors |= 16;
492
493 if ((bus_size == 0x2) && (readl(&mmdc1->mpwrdlhwctl) & 0x0000000f))
494 errors |= 32;
495
496 debug("Ending Write Delay calibration. Error mask: 0x%x\n", errors);
497
498 reset_read_data_fifos();
499
500 /* Enable DDR logic power down timer */
501 setbits_le32(&mmdc0->mdpdc, 0x00005500);
502
503 /* Enable Adopt power down timer */
504 clrbits_le32(&mmdc0->mapsr, 0x1);
505
506 /* Restore MDMISC value (RALAT, WALAT) to MMDCP1 */
507 writel(esdmisc_val, &mmdc0->mdmisc);
508
509 /* Clear DQS pull ups */
510 clrbits_le32(&mx6_ddr_iomux->dram_sdqs0, 0x7000);
511 clrbits_le32(&mx6_ddr_iomux->dram_sdqs1, 0x7000);
512 clrbits_le32(&mx6_ddr_iomux->dram_sdqs2, 0x7000);
513 clrbits_le32(&mx6_ddr_iomux->dram_sdqs3, 0x7000);
514 clrbits_le32(&mx6_ddr_iomux->dram_sdqs4, 0x7000);
515 clrbits_le32(&mx6_ddr_iomux->dram_sdqs5, 0x7000);
516 clrbits_le32(&mx6_ddr_iomux->dram_sdqs6, 0x7000);
517 clrbits_le32(&mx6_ddr_iomux->dram_sdqs7, 0x7000);
518
519 /* Re-enable SDE (chip selects) if they were set initially */
520 if (cs1_enable_initial)
521 /* Set SDE_1 */
522 setbits_le32(&mmdc0->mdctl, 1 << 30);
523
524 if (cs0_enable_initial)
525 /* Set SDE_0 */
526 setbits_le32(&mmdc0->mdctl, 1 << 31);
527
528 /* Re-enable to auto refresh */
529 writel(temp_ref, &mmdc0->mdref);
530
531 /* Clear the MDSCR (including the con_req bit) */
532 writel(0x0, &mmdc0->mdscr); /* CS0 */
533
534 /* Poll to make sure the con_ack bit is clear */
535 wait_for_bit(&mmdc0->mdscr, 1 << 14, 0);
536
537 /*
538 * Print out the registers that were updated as a result
539 * of the calibration process.
540 */
541 debug("MMDC registers updated from calibration\n");
542 debug("Read DQS gating calibration:\n");
543 debug("\tMPDGCTRL0 PHY0 = 0x%08X\n", readl(&mmdc0->mpdgctrl0));
544 debug("\tMPDGCTRL1 PHY0 = 0x%08X\n", readl(&mmdc0->mpdgctrl1));
545 debug("\tMPDGCTRL0 PHY1 = 0x%08X\n", readl(&mmdc1->mpdgctrl0));
546 debug("\tMPDGCTRL1 PHY1 = 0x%08X\n", readl(&mmdc1->mpdgctrl1));
547 debug("Read calibration:\n");
548 debug("\tMPRDDLCTL PHY0 = 0x%08X\n", readl(&mmdc0->mprddlctl));
549 debug("\tMPRDDLCTL PHY1 = 0x%08X\n", readl(&mmdc1->mprddlctl));
550 debug("Write calibration:\n");
551 debug("\tMPWRDLCTL PHY0 = 0x%08X\n", readl(&mmdc0->mpwrdlctl));
552 debug("\tMPWRDLCTL PHY1 = 0x%08X\n", readl(&mmdc1->mpwrdlctl));
553
554 /*
555 * Registers below are for debugging purposes. These print out
556 * the upper and lower boundaries captured during
557 * read DQS gating calibration.
558 */
559 debug("Status registers bounds for read DQS gating:\n");
560 debug("\tMPDGHWST0 PHY0 = 0x%08x\n", readl(&mmdc0->mpdghwst0));
561 debug("\tMPDGHWST1 PHY0 = 0x%08x\n", readl(&mmdc0->mpdghwst1));
562 debug("\tMPDGHWST2 PHY0 = 0x%08x\n", readl(&mmdc0->mpdghwst2));
563 debug("\tMPDGHWST3 PHY0 = 0x%08x\n", readl(&mmdc0->mpdghwst3));
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 debug("Final do_dqs_calibration error mask: 0x%x\n", errors);
570
571 return errors;
572}
573#endif
574
Peng Fan2ecdd022014-12-30 17:24:01 +0800575#if defined(CONFIG_MX6SX)
576/* Configure MX6SX mmdc iomux */
577void mx6sx_dram_iocfg(unsigned width,
578 const struct mx6sx_iomux_ddr_regs *ddr,
579 const struct mx6sx_iomux_grp_regs *grp)
580{
581 struct mx6sx_iomux_ddr_regs *mx6_ddr_iomux;
582 struct mx6sx_iomux_grp_regs *mx6_grp_iomux;
583
584 mx6_ddr_iomux = (struct mx6sx_iomux_ddr_regs *)MX6SX_IOM_DDR_BASE;
585 mx6_grp_iomux = (struct mx6sx_iomux_grp_regs *)MX6SX_IOM_GRP_BASE;
586
587 /* DDR IO TYPE */
588 writel(grp->grp_ddr_type, &mx6_grp_iomux->grp_ddr_type);
589 writel(grp->grp_ddrpke, &mx6_grp_iomux->grp_ddrpke);
590
591 /* CLOCK */
592 writel(ddr->dram_sdclk_0, &mx6_ddr_iomux->dram_sdclk_0);
593
594 /* ADDRESS */
595 writel(ddr->dram_cas, &mx6_ddr_iomux->dram_cas);
596 writel(ddr->dram_ras, &mx6_ddr_iomux->dram_ras);
597 writel(grp->grp_addds, &mx6_grp_iomux->grp_addds);
598
599 /* Control */
600 writel(ddr->dram_reset, &mx6_ddr_iomux->dram_reset);
601 writel(ddr->dram_sdba2, &mx6_ddr_iomux->dram_sdba2);
602 writel(ddr->dram_sdcke0, &mx6_ddr_iomux->dram_sdcke0);
603 writel(ddr->dram_sdcke1, &mx6_ddr_iomux->dram_sdcke1);
604 writel(ddr->dram_odt0, &mx6_ddr_iomux->dram_odt0);
605 writel(ddr->dram_odt1, &mx6_ddr_iomux->dram_odt1);
606 writel(grp->grp_ctlds, &mx6_grp_iomux->grp_ctlds);
607
608 /* Data Strobes */
609 writel(grp->grp_ddrmode_ctl, &mx6_grp_iomux->grp_ddrmode_ctl);
610 writel(ddr->dram_sdqs0, &mx6_ddr_iomux->dram_sdqs0);
611 writel(ddr->dram_sdqs1, &mx6_ddr_iomux->dram_sdqs1);
612 if (width >= 32) {
613 writel(ddr->dram_sdqs2, &mx6_ddr_iomux->dram_sdqs2);
614 writel(ddr->dram_sdqs3, &mx6_ddr_iomux->dram_sdqs3);
615 }
616
617 /* Data */
618 writel(grp->grp_ddrmode, &mx6_grp_iomux->grp_ddrmode);
619 writel(grp->grp_b0ds, &mx6_grp_iomux->grp_b0ds);
620 writel(grp->grp_b1ds, &mx6_grp_iomux->grp_b1ds);
621 if (width >= 32) {
622 writel(grp->grp_b2ds, &mx6_grp_iomux->grp_b2ds);
623 writel(grp->grp_b3ds, &mx6_grp_iomux->grp_b3ds);
624 }
625 writel(ddr->dram_dqm0, &mx6_ddr_iomux->dram_dqm0);
626 writel(ddr->dram_dqm1, &mx6_ddr_iomux->dram_dqm1);
627 if (width >= 32) {
628 writel(ddr->dram_dqm2, &mx6_ddr_iomux->dram_dqm2);
629 writel(ddr->dram_dqm3, &mx6_ddr_iomux->dram_dqm3);
630 }
631}
632#endif
633
Peng Fan98f11a12015-07-20 19:28:33 +0800634#ifdef CONFIG_MX6UL
635void mx6ul_dram_iocfg(unsigned width,
636 const struct mx6ul_iomux_ddr_regs *ddr,
637 const struct mx6ul_iomux_grp_regs *grp)
638{
639 struct mx6ul_iomux_ddr_regs *mx6_ddr_iomux;
640 struct mx6ul_iomux_grp_regs *mx6_grp_iomux;
641
642 mx6_ddr_iomux = (struct mx6ul_iomux_ddr_regs *)MX6UL_IOM_DDR_BASE;
643 mx6_grp_iomux = (struct mx6ul_iomux_grp_regs *)MX6UL_IOM_GRP_BASE;
644
645 /* DDR IO TYPE */
646 writel(grp->grp_ddr_type, &mx6_grp_iomux->grp_ddr_type);
647 writel(grp->grp_ddrpke, &mx6_grp_iomux->grp_ddrpke);
648
649 /* CLOCK */
650 writel(ddr->dram_sdclk_0, &mx6_ddr_iomux->dram_sdclk_0);
651
652 /* ADDRESS */
653 writel(ddr->dram_cas, &mx6_ddr_iomux->dram_cas);
654 writel(ddr->dram_ras, &mx6_ddr_iomux->dram_ras);
655 writel(grp->grp_addds, &mx6_grp_iomux->grp_addds);
656
657 /* Control */
658 writel(ddr->dram_reset, &mx6_ddr_iomux->dram_reset);
659 writel(ddr->dram_sdba2, &mx6_ddr_iomux->dram_sdba2);
660 writel(ddr->dram_odt0, &mx6_ddr_iomux->dram_odt0);
661 writel(ddr->dram_odt1, &mx6_ddr_iomux->dram_odt1);
662 writel(grp->grp_ctlds, &mx6_grp_iomux->grp_ctlds);
663
664 /* Data Strobes */
665 writel(grp->grp_ddrmode_ctl, &mx6_grp_iomux->grp_ddrmode_ctl);
666 writel(ddr->dram_sdqs0, &mx6_ddr_iomux->dram_sdqs0);
667 writel(ddr->dram_sdqs1, &mx6_ddr_iomux->dram_sdqs1);
668
669 /* Data */
670 writel(grp->grp_ddrmode, &mx6_grp_iomux->grp_ddrmode);
671 writel(grp->grp_b0ds, &mx6_grp_iomux->grp_b0ds);
672 writel(grp->grp_b1ds, &mx6_grp_iomux->grp_b1ds);
673 writel(ddr->dram_dqm0, &mx6_ddr_iomux->dram_dqm0);
674 writel(ddr->dram_dqm1, &mx6_ddr_iomux->dram_dqm1);
675}
676#endif
677
Peng Fand226fac2015-08-17 16:11:00 +0800678#if defined(CONFIG_MX6SL)
679void mx6sl_dram_iocfg(unsigned width,
680 const struct mx6sl_iomux_ddr_regs *ddr,
681 const struct mx6sl_iomux_grp_regs *grp)
682{
683 struct mx6sl_iomux_ddr_regs *mx6_ddr_iomux;
684 struct mx6sl_iomux_grp_regs *mx6_grp_iomux;
685
686 mx6_ddr_iomux = (struct mx6sl_iomux_ddr_regs *)MX6SL_IOM_DDR_BASE;
687 mx6_grp_iomux = (struct mx6sl_iomux_grp_regs *)MX6SL_IOM_GRP_BASE;
688
689 /* DDR IO TYPE */
690 mx6_grp_iomux->grp_ddr_type = grp->grp_ddr_type;
691 mx6_grp_iomux->grp_ddrpke = grp->grp_ddrpke;
692
693 /* CLOCK */
694 mx6_ddr_iomux->dram_sdclk_0 = ddr->dram_sdclk_0;
695
696 /* ADDRESS */
697 mx6_ddr_iomux->dram_cas = ddr->dram_cas;
698 mx6_ddr_iomux->dram_ras = ddr->dram_ras;
699 mx6_grp_iomux->grp_addds = grp->grp_addds;
700
701 /* Control */
702 mx6_ddr_iomux->dram_reset = ddr->dram_reset;
703 mx6_ddr_iomux->dram_sdba2 = ddr->dram_sdba2;
704 mx6_grp_iomux->grp_ctlds = grp->grp_ctlds;
705
706 /* Data Strobes */
707 mx6_grp_iomux->grp_ddrmode_ctl = grp->grp_ddrmode_ctl;
708 mx6_ddr_iomux->dram_sdqs0 = ddr->dram_sdqs0;
709 mx6_ddr_iomux->dram_sdqs1 = ddr->dram_sdqs1;
710 if (width >= 32) {
711 mx6_ddr_iomux->dram_sdqs2 = ddr->dram_sdqs2;
712 mx6_ddr_iomux->dram_sdqs3 = ddr->dram_sdqs3;
713 }
714
715 /* Data */
716 mx6_grp_iomux->grp_ddrmode = grp->grp_ddrmode;
717 mx6_grp_iomux->grp_b0ds = grp->grp_b0ds;
718 mx6_grp_iomux->grp_b1ds = grp->grp_b1ds;
719 if (width >= 32) {
720 mx6_grp_iomux->grp_b2ds = grp->grp_b2ds;
721 mx6_grp_iomux->grp_b3ds = grp->grp_b3ds;
722 }
723
724 mx6_ddr_iomux->dram_dqm0 = ddr->dram_dqm0;
725 mx6_ddr_iomux->dram_dqm1 = ddr->dram_dqm1;
726 if (width >= 32) {
727 mx6_ddr_iomux->dram_dqm2 = ddr->dram_dqm2;
728 mx6_ddr_iomux->dram_dqm3 = ddr->dram_dqm3;
729 }
730}
731#endif
732
Tim Harvey8ab871b2014-06-02 16:13:23 -0700733#if defined(CONFIG_MX6QDL) || defined(CONFIG_MX6Q) || defined(CONFIG_MX6D)
734/* Configure MX6DQ mmdc iomux */
735void mx6dq_dram_iocfg(unsigned width,
736 const struct mx6dq_iomux_ddr_regs *ddr,
737 const struct mx6dq_iomux_grp_regs *grp)
738{
739 volatile struct mx6dq_iomux_ddr_regs *mx6_ddr_iomux;
740 volatile struct mx6dq_iomux_grp_regs *mx6_grp_iomux;
741
742 mx6_ddr_iomux = (struct mx6dq_iomux_ddr_regs *)MX6DQ_IOM_DDR_BASE;
743 mx6_grp_iomux = (struct mx6dq_iomux_grp_regs *)MX6DQ_IOM_GRP_BASE;
744
745 /* DDR IO Type */
746 mx6_grp_iomux->grp_ddr_type = grp->grp_ddr_type;
747 mx6_grp_iomux->grp_ddrpke = grp->grp_ddrpke;
748
749 /* Clock */
750 mx6_ddr_iomux->dram_sdclk_0 = ddr->dram_sdclk_0;
751 mx6_ddr_iomux->dram_sdclk_1 = ddr->dram_sdclk_1;
752
753 /* Address */
754 mx6_ddr_iomux->dram_cas = ddr->dram_cas;
755 mx6_ddr_iomux->dram_ras = ddr->dram_ras;
756 mx6_grp_iomux->grp_addds = grp->grp_addds;
757
758 /* Control */
759 mx6_ddr_iomux->dram_reset = ddr->dram_reset;
760 mx6_ddr_iomux->dram_sdcke0 = ddr->dram_sdcke0;
761 mx6_ddr_iomux->dram_sdcke1 = ddr->dram_sdcke1;
762 mx6_ddr_iomux->dram_sdba2 = ddr->dram_sdba2;
763 mx6_ddr_iomux->dram_sdodt0 = ddr->dram_sdodt0;
764 mx6_ddr_iomux->dram_sdodt1 = ddr->dram_sdodt1;
765 mx6_grp_iomux->grp_ctlds = grp->grp_ctlds;
766
767 /* Data Strobes */
768 mx6_grp_iomux->grp_ddrmode_ctl = grp->grp_ddrmode_ctl;
769 mx6_ddr_iomux->dram_sdqs0 = ddr->dram_sdqs0;
770 mx6_ddr_iomux->dram_sdqs1 = ddr->dram_sdqs1;
771 if (width >= 32) {
772 mx6_ddr_iomux->dram_sdqs2 = ddr->dram_sdqs2;
773 mx6_ddr_iomux->dram_sdqs3 = ddr->dram_sdqs3;
774 }
775 if (width >= 64) {
776 mx6_ddr_iomux->dram_sdqs4 = ddr->dram_sdqs4;
777 mx6_ddr_iomux->dram_sdqs5 = ddr->dram_sdqs5;
778 mx6_ddr_iomux->dram_sdqs6 = ddr->dram_sdqs6;
779 mx6_ddr_iomux->dram_sdqs7 = ddr->dram_sdqs7;
780 }
781
782 /* Data */
783 mx6_grp_iomux->grp_ddrmode = grp->grp_ddrmode;
784 mx6_grp_iomux->grp_b0ds = grp->grp_b0ds;
785 mx6_grp_iomux->grp_b1ds = grp->grp_b1ds;
786 if (width >= 32) {
787 mx6_grp_iomux->grp_b2ds = grp->grp_b2ds;
788 mx6_grp_iomux->grp_b3ds = grp->grp_b3ds;
789 }
790 if (width >= 64) {
791 mx6_grp_iomux->grp_b4ds = grp->grp_b4ds;
792 mx6_grp_iomux->grp_b5ds = grp->grp_b5ds;
793 mx6_grp_iomux->grp_b6ds = grp->grp_b6ds;
794 mx6_grp_iomux->grp_b7ds = grp->grp_b7ds;
795 }
796 mx6_ddr_iomux->dram_dqm0 = ddr->dram_dqm0;
797 mx6_ddr_iomux->dram_dqm1 = ddr->dram_dqm1;
798 if (width >= 32) {
799 mx6_ddr_iomux->dram_dqm2 = ddr->dram_dqm2;
800 mx6_ddr_iomux->dram_dqm3 = ddr->dram_dqm3;
801 }
802 if (width >= 64) {
803 mx6_ddr_iomux->dram_dqm4 = ddr->dram_dqm4;
804 mx6_ddr_iomux->dram_dqm5 = ddr->dram_dqm5;
805 mx6_ddr_iomux->dram_dqm6 = ddr->dram_dqm6;
806 mx6_ddr_iomux->dram_dqm7 = ddr->dram_dqm7;
807 }
808}
809#endif
810
811#if defined(CONFIG_MX6QDL) || defined(CONFIG_MX6DL) || defined(CONFIG_MX6S)
812/* Configure MX6SDL mmdc iomux */
813void mx6sdl_dram_iocfg(unsigned width,
814 const struct mx6sdl_iomux_ddr_regs *ddr,
815 const struct mx6sdl_iomux_grp_regs *grp)
816{
817 volatile struct mx6sdl_iomux_ddr_regs *mx6_ddr_iomux;
818 volatile struct mx6sdl_iomux_grp_regs *mx6_grp_iomux;
819
820 mx6_ddr_iomux = (struct mx6sdl_iomux_ddr_regs *)MX6SDL_IOM_DDR_BASE;
821 mx6_grp_iomux = (struct mx6sdl_iomux_grp_regs *)MX6SDL_IOM_GRP_BASE;
822
823 /* DDR IO Type */
824 mx6_grp_iomux->grp_ddr_type = grp->grp_ddr_type;
825 mx6_grp_iomux->grp_ddrpke = grp->grp_ddrpke;
826
827 /* Clock */
828 mx6_ddr_iomux->dram_sdclk_0 = ddr->dram_sdclk_0;
829 mx6_ddr_iomux->dram_sdclk_1 = ddr->dram_sdclk_1;
830
831 /* Address */
832 mx6_ddr_iomux->dram_cas = ddr->dram_cas;
833 mx6_ddr_iomux->dram_ras = ddr->dram_ras;
834 mx6_grp_iomux->grp_addds = grp->grp_addds;
835
836 /* Control */
837 mx6_ddr_iomux->dram_reset = ddr->dram_reset;
838 mx6_ddr_iomux->dram_sdcke0 = ddr->dram_sdcke0;
839 mx6_ddr_iomux->dram_sdcke1 = ddr->dram_sdcke1;
840 mx6_ddr_iomux->dram_sdba2 = ddr->dram_sdba2;
841 mx6_ddr_iomux->dram_sdodt0 = ddr->dram_sdodt0;
842 mx6_ddr_iomux->dram_sdodt1 = ddr->dram_sdodt1;
843 mx6_grp_iomux->grp_ctlds = grp->grp_ctlds;
844
845 /* Data Strobes */
846 mx6_grp_iomux->grp_ddrmode_ctl = grp->grp_ddrmode_ctl;
847 mx6_ddr_iomux->dram_sdqs0 = ddr->dram_sdqs0;
848 mx6_ddr_iomux->dram_sdqs1 = ddr->dram_sdqs1;
849 if (width >= 32) {
850 mx6_ddr_iomux->dram_sdqs2 = ddr->dram_sdqs2;
851 mx6_ddr_iomux->dram_sdqs3 = ddr->dram_sdqs3;
852 }
853 if (width >= 64) {
854 mx6_ddr_iomux->dram_sdqs4 = ddr->dram_sdqs4;
855 mx6_ddr_iomux->dram_sdqs5 = ddr->dram_sdqs5;
856 mx6_ddr_iomux->dram_sdqs6 = ddr->dram_sdqs6;
857 mx6_ddr_iomux->dram_sdqs7 = ddr->dram_sdqs7;
858 }
859
860 /* Data */
861 mx6_grp_iomux->grp_ddrmode = grp->grp_ddrmode;
862 mx6_grp_iomux->grp_b0ds = grp->grp_b0ds;
863 mx6_grp_iomux->grp_b1ds = grp->grp_b1ds;
864 if (width >= 32) {
865 mx6_grp_iomux->grp_b2ds = grp->grp_b2ds;
866 mx6_grp_iomux->grp_b3ds = grp->grp_b3ds;
867 }
868 if (width >= 64) {
869 mx6_grp_iomux->grp_b4ds = grp->grp_b4ds;
870 mx6_grp_iomux->grp_b5ds = grp->grp_b5ds;
871 mx6_grp_iomux->grp_b6ds = grp->grp_b6ds;
872 mx6_grp_iomux->grp_b7ds = grp->grp_b7ds;
873 }
874 mx6_ddr_iomux->dram_dqm0 = ddr->dram_dqm0;
875 mx6_ddr_iomux->dram_dqm1 = ddr->dram_dqm1;
876 if (width >= 32) {
877 mx6_ddr_iomux->dram_dqm2 = ddr->dram_dqm2;
878 mx6_ddr_iomux->dram_dqm3 = ddr->dram_dqm3;
879 }
880 if (width >= 64) {
881 mx6_ddr_iomux->dram_dqm4 = ddr->dram_dqm4;
882 mx6_ddr_iomux->dram_dqm5 = ddr->dram_dqm5;
883 mx6_ddr_iomux->dram_dqm6 = ddr->dram_dqm6;
884 mx6_ddr_iomux->dram_dqm7 = ddr->dram_dqm7;
885 }
886}
887#endif
888
889/*
890 * Configure mx6 mmdc registers based on:
891 * - board-specific memory configuration
892 * - board-specific calibration data
Peng Fanda7ada02015-08-17 16:11:04 +0800893 * - ddr3/lpddr2 chip details
Tim Harvey8ab871b2014-06-02 16:13:23 -0700894 *
895 * The various calculations here are derived from the Freescale
Peng Fanda7ada02015-08-17 16:11:04 +0800896 * 1. i.Mx6DQSDL DDR3 Script Aid spreadsheet (DOC-94917) designed to generate
897 * MMDC configuration registers based on memory system and memory chip
898 * parameters.
899 *
900 * 2. i.Mx6SL LPDDR2 Script Aid spreadsheet V0.04 designed to generate MMDC
901 * configuration registers based on memory system and memory chip
902 * parameters.
Tim Harvey8ab871b2014-06-02 16:13:23 -0700903 *
904 * The defaults here are those which were specified in the spreadsheet.
905 * For details on each register, refer to the IMX6DQRM and/or IMX6SDLRM
Peng Fanda7ada02015-08-17 16:11:04 +0800906 * and/or IMX6SLRM section titled MMDC initialization.
Tim Harvey8ab871b2014-06-02 16:13:23 -0700907 */
908#define MR(val, ba, cmd, cs1) \
909 ((val << 16) | (1 << 15) | (cmd << 4) | (cs1 << 3) | ba)
Peng Fan98f11a12015-07-20 19:28:33 +0800910#define MMDC1(entry, value) do { \
Peng Fan1b81b062015-08-17 16:10:58 +0800911 if (!is_cpu_type(MXC_CPU_MX6SX) && !is_cpu_type(MXC_CPU_MX6UL) && \
912 !is_cpu_type(MXC_CPU_MX6SL)) \
Peng Fan98f11a12015-07-20 19:28:33 +0800913 mmdc1->entry = value; \
914 } while (0)
915
Peng Fanda7ada02015-08-17 16:11:04 +0800916/*
917 * According JESD209-2B-LPDDR2: Table 103
918 * WL: write latency
919 */
920static 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 */
946static 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
970void 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 = (0 << 14) | /* REF_SEL: Periodic refresh cycle: 64kHz */
1191 (3 << 11); /* REFR: Refresh Rate - 4 refreshes */
1192
1193 /* Step 13: Deassert config request - init complete */
1194 mmdc0->mdscr = 0x00000000;
1195
1196 /* wait for auto-ZQ calibration to complete */
1197 mdelay(1);
1198}
1199
Peng Fan77e86952015-08-17 16:11:03 +08001200void mx6_ddr3_cfg(const struct mx6_ddr_sysinfo *sysinfo,
Nikita Kiryanovc4753462014-09-07 18:58:11 +03001201 const struct mx6_mmdc_calibration *calib,
1202 const struct mx6_ddr3_cfg *ddr3_cfg)
Tim Harvey8ab871b2014-06-02 16:13:23 -07001203{
1204 volatile struct mmdc_p_regs *mmdc0;
1205 volatile struct mmdc_p_regs *mmdc1;
Nikita Kiryanovc4753462014-09-07 18:58:11 +03001206 u32 val;
Tim Harvey8ab871b2014-06-02 16:13:23 -07001207 u8 tcke, tcksrx, tcksre, txpdll, taofpd, taonpd, trrd;
1208 u8 todtlon, taxpd, tanpd, tcwl, txp, tfaw, tcl;
1209 u8 todt_idle_off = 0x4; /* from DDR3 Script Aid spreadsheet */
1210 u16 trcd, trc, tras, twr, tmrd, trtp, trp, twtr, trfc, txs, txpr;
Nikita Kiryanovc4753462014-09-07 18:58:11 +03001211 u16 cs0_end;
Tim Harvey8ab871b2014-06-02 16:13:23 -07001212 u16 tdllk = 0x1ff; /* DLL locking time: 512 cycles (JEDEC DDR3) */
Marek Vasut4a463602014-08-04 01:47:10 +02001213 u8 coladdr;
Tim Harvey8ab871b2014-06-02 16:13:23 -07001214 int clkper; /* clock period in picoseconds */
Nikolay Dimitrov99c25ff2015-04-22 18:37:31 +03001215 int clock; /* clock freq in MHz */
Tim Harvey8ab871b2014-06-02 16:13:23 -07001216 int cs;
Nikolay Dimitrov99c25ff2015-04-22 18:37:31 +03001217 u16 mem_speed = ddr3_cfg->mem_speed;
Tim Harvey8ab871b2014-06-02 16:13:23 -07001218
1219 mmdc0 = (struct mmdc_p_regs *)MMDC_P0_BASE_ADDR;
Peng Fan1b81b062015-08-17 16:10:58 +08001220 if (!is_cpu_type(MXC_CPU_MX6SX) && !is_cpu_type(MXC_CPU_MX6UL) &&
1221 !is_cpu_type(MXC_CPU_MX6SL))
Peng Fan98f11a12015-07-20 19:28:33 +08001222 mmdc1 = (struct mmdc_p_regs *)MMDC_P1_BASE_ADDR;
Tim Harvey8ab871b2014-06-02 16:13:23 -07001223
Nikolay Dimitrov99c25ff2015-04-22 18:37:31 +03001224 /* Limit mem_speed for MX6D/MX6Q */
Tim Harvey8ab871b2014-06-02 16:13:23 -07001225 if (is_cpu_type(MXC_CPU_MX6Q) || is_cpu_type(MXC_CPU_MX6D)) {
Nikolay Dimitrov99c25ff2015-04-22 18:37:31 +03001226 if (mem_speed > 1066)
1227 mem_speed = 1066; /* 1066 MT/s */
1228
Tim Harvey8ab871b2014-06-02 16:13:23 -07001229 tcwl = 4;
1230 }
Nikolay Dimitrov99c25ff2015-04-22 18:37:31 +03001231 /* Limit mem_speed for MX6S/MX6DL */
Tim Harvey8ab871b2014-06-02 16:13:23 -07001232 else {
Nikolay Dimitrov99c25ff2015-04-22 18:37:31 +03001233 if (mem_speed > 800)
1234 mem_speed = 800; /* 800 MT/s */
1235
Tim Harvey8ab871b2014-06-02 16:13:23 -07001236 tcwl = 3;
1237 }
Nikolay Dimitrov99c25ff2015-04-22 18:37:31 +03001238
1239 clock = mem_speed / 2;
1240 /*
1241 * Data rate of 1066 MT/s requires 533 MHz DDR3 clock, but MX6D/Q supports
1242 * up to 528 MHz, so reduce the clock to fit chip specs
1243 */
1244 if (is_cpu_type(MXC_CPU_MX6Q) || is_cpu_type(MXC_CPU_MX6D)) {
1245 if (clock > 528)
1246 clock = 528; /* 528 MHz */
1247 }
1248
Nikita Kiryanovc4753462014-09-07 18:58:11 +03001249 clkper = (1000 * 1000) / clock; /* pico seconds */
Tim Harvey8ab871b2014-06-02 16:13:23 -07001250 todtlon = tcwl;
1251 taxpd = tcwl;
1252 tanpd = tcwl;
Tim Harvey8ab871b2014-06-02 16:13:23 -07001253
Nikita Kiryanovc4753462014-09-07 18:58:11 +03001254 switch (ddr3_cfg->density) {
Tim Harvey8ab871b2014-06-02 16:13:23 -07001255 case 1: /* 1Gb per chip */
1256 trfc = DIV_ROUND_UP(110000, clkper) - 1;
1257 txs = DIV_ROUND_UP(120000, clkper) - 1;
1258 break;
1259 case 2: /* 2Gb per chip */
1260 trfc = DIV_ROUND_UP(160000, clkper) - 1;
1261 txs = DIV_ROUND_UP(170000, clkper) - 1;
1262 break;
1263 case 4: /* 4Gb per chip */
Peng Fanb96b74c2015-09-01 11:03:14 +08001264 trfc = DIV_ROUND_UP(260000, clkper) - 1;
1265 txs = DIV_ROUND_UP(270000, clkper) - 1;
Tim Harvey8ab871b2014-06-02 16:13:23 -07001266 break;
1267 case 8: /* 8Gb per chip */
1268 trfc = DIV_ROUND_UP(350000, clkper) - 1;
1269 txs = DIV_ROUND_UP(360000, clkper) - 1;
1270 break;
1271 default:
1272 /* invalid density */
Nikita Kiryanovc4753462014-09-07 18:58:11 +03001273 puts("invalid chip density\n");
Tim Harvey8ab871b2014-06-02 16:13:23 -07001274 hang();
1275 break;
1276 }
1277 txpr = txs;
1278
Nikolay Dimitrov99c25ff2015-04-22 18:37:31 +03001279 switch (mem_speed) {
Tim Harvey8ab871b2014-06-02 16:13:23 -07001280 case 800:
Masahiro Yamadab62b39b2014-09-18 13:28:06 +09001281 txp = DIV_ROUND_UP(max(3 * clkper, 7500), clkper) - 1;
1282 tcke = DIV_ROUND_UP(max(3 * clkper, 7500), clkper) - 1;
Nikita Kiryanovc4753462014-09-07 18:58:11 +03001283 if (ddr3_cfg->pagesz == 1) {
Tim Harvey8ab871b2014-06-02 16:13:23 -07001284 tfaw = DIV_ROUND_UP(40000, clkper) - 1;
Masahiro Yamadab62b39b2014-09-18 13:28:06 +09001285 trrd = DIV_ROUND_UP(max(4 * clkper, 10000), clkper) - 1;
Tim Harvey8ab871b2014-06-02 16:13:23 -07001286 } else {
1287 tfaw = DIV_ROUND_UP(50000, clkper) - 1;
Masahiro Yamadab62b39b2014-09-18 13:28:06 +09001288 trrd = DIV_ROUND_UP(max(4 * clkper, 10000), clkper) - 1;
Tim Harvey8ab871b2014-06-02 16:13:23 -07001289 }
1290 break;
1291 case 1066:
Masahiro Yamadab62b39b2014-09-18 13:28:06 +09001292 txp = DIV_ROUND_UP(max(3 * clkper, 7500), clkper) - 1;
1293 tcke = DIV_ROUND_UP(max(3 * clkper, 5625), clkper) - 1;
Nikita Kiryanovc4753462014-09-07 18:58:11 +03001294 if (ddr3_cfg->pagesz == 1) {
Tim Harvey8ab871b2014-06-02 16:13:23 -07001295 tfaw = DIV_ROUND_UP(37500, clkper) - 1;
Masahiro Yamadab62b39b2014-09-18 13:28:06 +09001296 trrd = DIV_ROUND_UP(max(4 * clkper, 7500), clkper) - 1;
Tim Harvey8ab871b2014-06-02 16:13:23 -07001297 } else {
1298 tfaw = DIV_ROUND_UP(50000, clkper) - 1;
Masahiro Yamadab62b39b2014-09-18 13:28:06 +09001299 trrd = DIV_ROUND_UP(max(4 * clkper, 10000), clkper) - 1;
Tim Harvey8ab871b2014-06-02 16:13:23 -07001300 }
1301 break;
Tim Harvey8ab871b2014-06-02 16:13:23 -07001302 default:
Nikita Kiryanovc4753462014-09-07 18:58:11 +03001303 puts("invalid memory speed\n");
Tim Harvey8ab871b2014-06-02 16:13:23 -07001304 hang();
1305 break;
1306 }
Masahiro Yamadab62b39b2014-09-18 13:28:06 +09001307 txpdll = DIV_ROUND_UP(max(10 * clkper, 24000), clkper) - 1;
1308 tcksre = DIV_ROUND_UP(max(5 * clkper, 10000), clkper);
Tim Harvey8ab871b2014-06-02 16:13:23 -07001309 taonpd = DIV_ROUND_UP(2000, clkper) - 1;
Nikita Kiryanovc4753462014-09-07 18:58:11 +03001310 tcksrx = tcksre;
Tim Harvey8ab871b2014-06-02 16:13:23 -07001311 taofpd = taonpd;
Nikita Kiryanovc4753462014-09-07 18:58:11 +03001312 twr = DIV_ROUND_UP(15000, clkper) - 1;
Masahiro Yamadab62b39b2014-09-18 13:28:06 +09001313 tmrd = DIV_ROUND_UP(max(12 * clkper, 15000), clkper) - 1;
Nikita Kiryanovc4753462014-09-07 18:58:11 +03001314 trc = DIV_ROUND_UP(ddr3_cfg->trcmin, clkper / 10) - 1;
1315 tras = DIV_ROUND_UP(ddr3_cfg->trasmin, clkper / 10) - 1;
1316 tcl = DIV_ROUND_UP(ddr3_cfg->trcd, clkper / 10) - 3;
1317 trp = DIV_ROUND_UP(ddr3_cfg->trcd, clkper / 10) - 1;
Masahiro Yamadab62b39b2014-09-18 13:28:06 +09001318 twtr = ROUND(max(4 * clkper, 7500) / clkper, 1) - 1;
Tim Harvey8ab871b2014-06-02 16:13:23 -07001319 trcd = trp;
Tim Harvey8ab871b2014-06-02 16:13:23 -07001320 trtp = twtr;
Nikita Kiryanov4a50ec22014-08-20 15:08:58 +03001321 cs0_end = 4 * sysinfo->cs_density - 1;
Nikita Kiryanovc4753462014-09-07 18:58:11 +03001322
1323 debug("density:%d Gb (%d Gb per chip)\n",
1324 sysinfo->cs_density, ddr3_cfg->density);
Tim Harvey8ab871b2014-06-02 16:13:23 -07001325 debug("clock: %dMHz (%d ps)\n", clock, clkper);
Nikolay Dimitrov99c25ff2015-04-22 18:37:31 +03001326 debug("memspd:%d\n", mem_speed);
Tim Harvey8ab871b2014-06-02 16:13:23 -07001327 debug("tcke=%d\n", tcke);
1328 debug("tcksrx=%d\n", tcksrx);
1329 debug("tcksre=%d\n", tcksre);
1330 debug("taofpd=%d\n", taofpd);
1331 debug("taonpd=%d\n", taonpd);
1332 debug("todtlon=%d\n", todtlon);
1333 debug("tanpd=%d\n", tanpd);
1334 debug("taxpd=%d\n", taxpd);
1335 debug("trfc=%d\n", trfc);
1336 debug("txs=%d\n", txs);
1337 debug("txp=%d\n", txp);
1338 debug("txpdll=%d\n", txpdll);
1339 debug("tfaw=%d\n", tfaw);
1340 debug("tcl=%d\n", tcl);
1341 debug("trcd=%d\n", trcd);
1342 debug("trp=%d\n", trp);
1343 debug("trc=%d\n", trc);
1344 debug("tras=%d\n", tras);
1345 debug("twr=%d\n", twr);
1346 debug("tmrd=%d\n", tmrd);
1347 debug("tcwl=%d\n", tcwl);
1348 debug("tdllk=%d\n", tdllk);
1349 debug("trtp=%d\n", trtp);
1350 debug("twtr=%d\n", twtr);
1351 debug("trrd=%d\n", trrd);
1352 debug("txpr=%d\n", txpr);
Nikita Kiryanovc4753462014-09-07 18:58:11 +03001353 debug("cs0_end=%d\n", cs0_end);
1354 debug("ncs=%d\n", sysinfo->ncs);
1355 debug("Rtt_wr=%d\n", sysinfo->rtt_wr);
1356 debug("Rtt_nom=%d\n", sysinfo->rtt_nom);
1357 debug("SRT=%d\n", ddr3_cfg->SRT);
Tim Harvey8ab871b2014-06-02 16:13:23 -07001358 debug("twr=%d\n", twr);
1359
1360 /*
1361 * board-specific configuration:
1362 * These values are determined empirically and vary per board layout
1363 * see:
1364 * appnote, ddr3 spreadsheet
1365 */
Nikita Kiryanovc4753462014-09-07 18:58:11 +03001366 mmdc0->mpwldectrl0 = calib->p0_mpwldectrl0;
1367 mmdc0->mpwldectrl1 = calib->p0_mpwldectrl1;
1368 mmdc0->mpdgctrl0 = calib->p0_mpdgctrl0;
1369 mmdc0->mpdgctrl1 = calib->p0_mpdgctrl1;
1370 mmdc0->mprddlctl = calib->p0_mprddlctl;
1371 mmdc0->mpwrdlctl = calib->p0_mpwrdlctl;
1372 if (sysinfo->dsize > 1) {
Peng Fan2ecdd022014-12-30 17:24:01 +08001373 MMDC1(mpwldectrl0, calib->p1_mpwldectrl0);
1374 MMDC1(mpwldectrl1, calib->p1_mpwldectrl1);
1375 MMDC1(mpdgctrl0, calib->p1_mpdgctrl0);
1376 MMDC1(mpdgctrl1, calib->p1_mpdgctrl1);
1377 MMDC1(mprddlctl, calib->p1_mprddlctl);
1378 MMDC1(mpwrdlctl, calib->p1_mpwrdlctl);
Tim Harvey8ab871b2014-06-02 16:13:23 -07001379 }
1380
1381 /* Read data DQ Byte0-3 delay */
Nikita Kiryanovc4753462014-09-07 18:58:11 +03001382 mmdc0->mprddqby0dl = 0x33333333;
1383 mmdc0->mprddqby1dl = 0x33333333;
1384 if (sysinfo->dsize > 0) {
1385 mmdc0->mprddqby2dl = 0x33333333;
1386 mmdc0->mprddqby3dl = 0x33333333;
Tim Harvey8ab871b2014-06-02 16:13:23 -07001387 }
Nikita Kiryanovc4753462014-09-07 18:58:11 +03001388
1389 if (sysinfo->dsize > 1) {
Peng Fan2ecdd022014-12-30 17:24:01 +08001390 MMDC1(mprddqby0dl, 0x33333333);
1391 MMDC1(mprddqby1dl, 0x33333333);
1392 MMDC1(mprddqby2dl, 0x33333333);
1393 MMDC1(mprddqby3dl, 0x33333333);
Tim Harvey8ab871b2014-06-02 16:13:23 -07001394 }
1395
1396 /* MMDC Termination: rtt_nom:2 RZQ/2(120ohm), rtt_nom:1 RZQ/4(60ohm) */
Nikita Kiryanovc4753462014-09-07 18:58:11 +03001397 val = (sysinfo->rtt_nom == 2) ? 0x00011117 : 0x00022227;
1398 mmdc0->mpodtctrl = val;
1399 if (sysinfo->dsize > 1)
Peng Fan2ecdd022014-12-30 17:24:01 +08001400 MMDC1(mpodtctrl, val);
Tim Harvey8ab871b2014-06-02 16:13:23 -07001401
1402 /* complete calibration */
Nikita Kiryanovc4753462014-09-07 18:58:11 +03001403 val = (1 << 11); /* Force measurement on delay-lines */
1404 mmdc0->mpmur0 = val;
1405 if (sysinfo->dsize > 1)
Peng Fan2ecdd022014-12-30 17:24:01 +08001406 MMDC1(mpmur0, val);
Tim Harvey8ab871b2014-06-02 16:13:23 -07001407
1408 /* Step 1: configuration request */
1409 mmdc0->mdscr = (u32)(1 << 15); /* config request */
1410
1411 /* Step 2: Timing configuration */
Nikita Kiryanovc4753462014-09-07 18:58:11 +03001412 mmdc0->mdcfg0 = (trfc << 24) | (txs << 16) | (txp << 13) |
1413 (txpdll << 9) | (tfaw << 4) | tcl;
1414 mmdc0->mdcfg1 = (trcd << 29) | (trp << 26) | (trc << 21) |
1415 (tras << 16) | (1 << 15) /* trpa */ |
1416 (twr << 9) | (tmrd << 5) | tcwl;
1417 mmdc0->mdcfg2 = (tdllk << 16) | (trtp << 6) | (twtr << 3) | trrd;
1418 mmdc0->mdotc = (taofpd << 27) | (taonpd << 24) | (tanpd << 20) |
1419 (taxpd << 16) | (todtlon << 12) | (todt_idle_off << 4);
1420 mmdc0->mdasp = cs0_end; /* CS addressing */
Tim Harvey8ab871b2014-06-02 16:13:23 -07001421
1422 /* Step 3: Configure DDR type */
Nikita Kiryanovc4753462014-09-07 18:58:11 +03001423 mmdc0->mdmisc = (sysinfo->cs1_mirror << 19) | (sysinfo->walat << 16) |
1424 (sysinfo->bi_on << 12) | (sysinfo->mif3_mode << 9) |
1425 (sysinfo->ralat << 6);
Tim Harvey8ab871b2014-06-02 16:13:23 -07001426
1427 /* Step 4: Configure delay while leaving reset */
Nikita Kiryanovc4753462014-09-07 18:58:11 +03001428 mmdc0->mdor = (txpr << 16) | (sysinfo->sde_to_rst << 8) |
1429 (sysinfo->rst_to_cke << 0);
Tim Harvey8ab871b2014-06-02 16:13:23 -07001430
1431 /* Step 5: Configure DDR physical parameters (density and burst len) */
Nikita Kiryanovc4753462014-09-07 18:58:11 +03001432 coladdr = ddr3_cfg->coladdr;
1433 if (ddr3_cfg->coladdr == 8) /* 8-bit COL is 0x3 */
Marek Vasut4a463602014-08-04 01:47:10 +02001434 coladdr += 4;
Nikita Kiryanovc4753462014-09-07 18:58:11 +03001435 else if (ddr3_cfg->coladdr == 12) /* 12-bit COL is 0x4 */
Marek Vasut4a463602014-08-04 01:47:10 +02001436 coladdr += 1;
Nikita Kiryanovc4753462014-09-07 18:58:11 +03001437 mmdc0->mdctl = (ddr3_cfg->rowaddr - 11) << 24 | /* ROW */
1438 (coladdr - 9) << 20 | /* COL */
1439 (1 << 19) | /* Burst Length = 8 for DDR3 */
1440 (sysinfo->dsize << 16); /* DDR data bus size */
Tim Harvey8ab871b2014-06-02 16:13:23 -07001441
1442 /* Step 6: Perform ZQ calibration */
Nikita Kiryanovc4753462014-09-07 18:58:11 +03001443 val = 0xa1390001; /* one-time HW ZQ calib */
1444 mmdc0->mpzqhwctrl = val;
1445 if (sysinfo->dsize > 1)
Peng Fan2ecdd022014-12-30 17:24:01 +08001446 MMDC1(mpzqhwctrl, val);
Tim Harvey8ab871b2014-06-02 16:13:23 -07001447
1448 /* Step 7: Enable MMDC with desired chip select */
Nikita Kiryanovc4753462014-09-07 18:58:11 +03001449 mmdc0->mdctl |= (1 << 31) | /* SDE_0 for CS0 */
1450 ((sysinfo->ncs == 2) ? 1 : 0) << 30; /* SDE_1 for CS1 */
Tim Harvey8ab871b2014-06-02 16:13:23 -07001451
1452 /* Step 8: Write Mode Registers to Init DDR3 devices */
Nikita Kiryanovc4753462014-09-07 18:58:11 +03001453 for (cs = 0; cs < sysinfo->ncs; cs++) {
Tim Harvey8ab871b2014-06-02 16:13:23 -07001454 /* MR2 */
Nikita Kiryanovc4753462014-09-07 18:58:11 +03001455 val = (sysinfo->rtt_wr & 3) << 9 | (ddr3_cfg->SRT & 1) << 7 |
Tim Harvey8ab871b2014-06-02 16:13:23 -07001456 ((tcwl - 3) & 3) << 3;
Tim Harveyfe1723f2015-04-03 16:52:52 -07001457 debug("MR2 CS%d: 0x%08x\n", cs, (u32)MR(val, 2, 3, cs));
Nikita Kiryanovc4753462014-09-07 18:58:11 +03001458 mmdc0->mdscr = MR(val, 2, 3, cs);
Tim Harvey8ab871b2014-06-02 16:13:23 -07001459 /* MR3 */
Tim Harveyfe1723f2015-04-03 16:52:52 -07001460 debug("MR3 CS%d: 0x%08x\n", cs, (u32)MR(0, 3, 3, cs));
Nikita Kiryanovc4753462014-09-07 18:58:11 +03001461 mmdc0->mdscr = MR(0, 3, 3, cs);
Tim Harvey8ab871b2014-06-02 16:13:23 -07001462 /* MR1 */
Nikita Kiryanovc4753462014-09-07 18:58:11 +03001463 val = ((sysinfo->rtt_nom & 1) ? 1 : 0) << 2 |
1464 ((sysinfo->rtt_nom & 2) ? 1 : 0) << 6;
Tim Harveyfe1723f2015-04-03 16:52:52 -07001465 debug("MR1 CS%d: 0x%08x\n", cs, (u32)MR(val, 1, 3, cs));
Nikita Kiryanovc4753462014-09-07 18:58:11 +03001466 mmdc0->mdscr = MR(val, 1, 3, cs);
1467 /* MR0 */
1468 val = ((tcl - 1) << 4) | /* CAS */
Tim Harvey8ab871b2014-06-02 16:13:23 -07001469 (1 << 8) | /* DLL Reset */
Tim Harvey591fe972015-05-18 07:07:02 -07001470 ((twr - 3) << 9) | /* Write Recovery */
1471 (sysinfo->pd_fast_exit << 12); /* Precharge PD PLL on */
Tim Harveyfe1723f2015-04-03 16:52:52 -07001472 debug("MR0 CS%d: 0x%08x\n", cs, (u32)MR(val, 0, 3, cs));
Nikita Kiryanovc4753462014-09-07 18:58:11 +03001473 mmdc0->mdscr = MR(val, 0, 3, cs);
Tim Harvey8ab871b2014-06-02 16:13:23 -07001474 /* ZQ calibration */
Nikita Kiryanovc4753462014-09-07 18:58:11 +03001475 val = (1 << 10);
1476 mmdc0->mdscr = MR(val, 0, 4, cs);
Tim Harvey8ab871b2014-06-02 16:13:23 -07001477 }
1478
1479 /* Step 10: Power down control and self-refresh */
Nikita Kiryanovc4753462014-09-07 18:58:11 +03001480 mmdc0->mdpdc = (tcke & 0x7) << 16 |
1481 5 << 12 | /* PWDT_1: 256 cycles */
1482 5 << 8 | /* PWDT_0: 256 cycles */
1483 1 << 6 | /* BOTH_CS_PD */
1484 (tcksrx & 0x7) << 3 |
1485 (tcksre & 0x7);
Tim Harveyfe1723f2015-04-03 16:52:52 -07001486 if (!sysinfo->pd_fast_exit)
1487 mmdc0->mdpdc |= (1 << 7); /* SLOW_PD */
Nikita Kiryanov6816f712014-08-20 15:08:56 +03001488 mmdc0->mapsr = 0x00001006; /* ADOPT power down enabled */
Tim Harvey8ab871b2014-06-02 16:13:23 -07001489
1490 /* Step 11: Configure ZQ calibration: one-time and periodic 1ms */
Nikita Kiryanovc4753462014-09-07 18:58:11 +03001491 val = 0xa1390003;
1492 mmdc0->mpzqhwctrl = val;
1493 if (sysinfo->dsize > 1)
Peng Fan2ecdd022014-12-30 17:24:01 +08001494 MMDC1(mpzqhwctrl, val);
Tim Harvey8ab871b2014-06-02 16:13:23 -07001495
1496 /* Step 12: Configure and activate periodic refresh */
Nikita Kiryanovc4753462014-09-07 18:58:11 +03001497 mmdc0->mdref = (1 << 14) | /* REF_SEL: Periodic refresh cycle: 32kHz */
1498 (7 << 11); /* REFR: Refresh Rate - 8 refreshes */
Tim Harvey8ab871b2014-06-02 16:13:23 -07001499
1500 /* Step 13: Deassert config request - init complete */
Nikita Kiryanovc4753462014-09-07 18:58:11 +03001501 mmdc0->mdscr = 0x00000000;
Tim Harvey8ab871b2014-06-02 16:13:23 -07001502
1503 /* wait for auto-ZQ calibration to complete */
1504 mdelay(1);
1505}
Peng Fan77e86952015-08-17 16:11:03 +08001506
1507void mx6_dram_cfg(const struct mx6_ddr_sysinfo *sysinfo,
1508 const struct mx6_mmdc_calibration *calib,
1509 const void *ddr_cfg)
1510{
1511 if (sysinfo->ddr_type == DDR_TYPE_DDR3) {
1512 mx6_ddr3_cfg(sysinfo, calib, ddr_cfg);
Peng Fanda7ada02015-08-17 16:11:04 +08001513 } else if (sysinfo->ddr_type == DDR_TYPE_LPDDR2) {
1514 mx6_lpddr2_cfg(sysinfo, calib, ddr_cfg);
Peng Fan77e86952015-08-17 16:11:03 +08001515 } else {
1516 puts("Unsupported ddr type\n");
1517 hang();
1518 }
1519}