1
2
3
4
5
6
7
8#include <common.h>
9#include <linux/types.h>
10#include <asm/arch/clock.h>
11#include <asm/arch/mx6-ddr.h>
12#include <asm/arch/sys_proto.h>
13#include <asm/io.h>
14#include <asm/types.h>
15#include <wait_bit.h>
16
17#if defined(CONFIG_MX6_DDRCAL)
18static void reset_read_data_fifos(void)
19{
20 struct mmdc_p_regs *mmdc0 = (struct mmdc_p_regs *)MMDC_P0_BASE_ADDR;
21
22
23 setbits_le32(&mmdc0->mpdgctrl0, 1 << 31);
24 wait_for_bit("MMDC", &mmdc0->mpdgctrl0, 1 << 31, 0, 100, 0);
25
26 setbits_le32(&mmdc0->mpdgctrl0, 1 << 31);
27 wait_for_bit("MMDC", &mmdc0->mpdgctrl0, 1 << 31, 0, 100, 0);
28}
29
30static 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
36
37
38
39
40 if (cs0_enable) {
41 writel(0x04008050, &mmdc0->mdscr);
42 wait_for_bit("MMDC", &mmdc0->mdscr, 1 << 14, 1, 100, 0);
43 }
44
45 if (cs1_enable) {
46 writel(0x04008058, &mmdc0->mdscr);
47 wait_for_bit("MMDC", &mmdc0->mdscr, 1 << 14, 1, 100, 0);
48 }
49}
50
51static 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
61static 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
67
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
88int mmdc_do_write_level_calibration(struct mx6_ddr_sysinfo const *sysinfo)
89{
90 struct mmdc_p_regs *mmdc0 = (struct mmdc_p_regs *)MMDC_P0_BASE_ADDR;
91 struct mmdc_p_regs *mmdc1 = (struct mmdc_p_regs *)MMDC_P1_BASE_ADDR;
92 u32 esdmisc_val, zq_val;
93 u32 errors = 0;
94 u32 ldectrl[4] = {0};
95 u32 ddr_mr1 = 0x4;
96 u32 rwalat_max;
97
98
99
100
101
102 ldectrl[0] = readl(&mmdc0->mpwldectrl0);
103 ldectrl[1] = readl(&mmdc0->mpwldectrl1);
104 if (sysinfo->dsize == 2) {
105 ldectrl[2] = readl(&mmdc1->mpwldectrl0);
106 ldectrl[3] = readl(&mmdc1->mpwldectrl1);
107 }
108
109
110 clrbits_le32(&mmdc0->mdpdc, 0xff00);
111
112
113 setbits_le32(&mmdc0->mapsr, 0x1);
114
115 debug("Starting write leveling calibration.\n");
116
117
118
119
120
121 esdmisc_val = readl(&mmdc0->mdref);
122 writel(0x0000C000, &mmdc0->mdref);
123 zq_val = readl(&mmdc0->mpzqhwctrl);
124 writel(zq_val & ~0x3, &mmdc0->mpzqhwctrl);
125
126
127 rwalat_max = (1 << 6) | (1 << 7) | (1 << 8) | (1 << 16) | (1 << 17);
128 setbits_le32(&mmdc0->mdmisc, rwalat_max);
129 if (sysinfo->dsize == 2)
130 setbits_le32(&mmdc1->mdmisc, rwalat_max);
131
132
133
134
135
136
137
138
139
140 writel(0x00808231, &mmdc0->mdscr);
141
142
143 writel(0x00000001, &mmdc0->mpwlgcr);
144
145
146
147
148
149 wait_for_bit("MMDC", &mmdc0->mpwlgcr, 1 << 0, 0, 100, 0);
150
151
152
153
154
155 if (readl(&mmdc0->mpwlgcr) & 0x00000F00)
156 errors |= 1;
157 if (sysinfo->dsize == 2)
158 if (readl(&mmdc1->mpwlgcr) & 0x00000F00)
159 errors |= 2;
160
161 debug("Ending write leveling calibration. Error mask: 0x%x\n", errors);
162
163
164 if ((readl(&mmdc0->mpwldectrl0) == 0x001F001F) &&
165 (readl(&mmdc0->mpwldectrl1) == 0x001F001F) &&
166 ((sysinfo->dsize < 2) ||
167 ((readl(&mmdc1->mpwldectrl0) == 0x001F001F) &&
168 (readl(&mmdc1->mpwldectrl1) == 0x001F001F)))) {
169 debug("Cal seems to have soft-failed due to memory not supporting write leveling on all channels. Restoring original write leveling values.\n");
170 writel(ldectrl[0], &mmdc0->mpwldectrl0);
171 writel(ldectrl[1], &mmdc0->mpwldectrl1);
172 if (sysinfo->dsize == 2) {
173 writel(ldectrl[2], &mmdc1->mpwldectrl0);
174 writel(ldectrl[3], &mmdc1->mpwldectrl1);
175 }
176 errors |= 4;
177 }
178
179
180
181
182
183
184
185
186
187
188 writel((ddr_mr1 << 16) + 0x8031, &mmdc0->mdscr);
189
190
191 writel(esdmisc_val, &mmdc0->mdref);
192 writel(zq_val, &mmdc0->mpzqhwctrl);
193
194 debug("\tMMDC_MPWLDECTRL0 after write level cal: 0x%08X\n",
195 readl(&mmdc0->mpwldectrl0));
196 debug("\tMMDC_MPWLDECTRL1 after write level cal: 0x%08X\n",
197 readl(&mmdc0->mpwldectrl1));
198 if (sysinfo->dsize == 2) {
199 debug("\tMMDC_MPWLDECTRL0 after write level cal: 0x%08X\n",
200 readl(&mmdc1->mpwldectrl0));
201 debug("\tMMDC_MPWLDECTRL1 after write level cal: 0x%08X\n",
202 readl(&mmdc1->mpwldectrl1));
203 }
204
205
206 readl(&mmdc0->mpwldectrl0);
207 readl(&mmdc0->mpwldectrl1);
208 if (sysinfo->dsize == 2) {
209 readl(&mmdc1->mpwldectrl0);
210 readl(&mmdc1->mpwldectrl1);
211 }
212
213
214 setbits_le32(&mmdc0->mdpdc, 0x00005500);
215
216
217 clrbits_le32(&mmdc0->mapsr, 0x1);
218
219
220 writel(0, &mmdc0->mdscr);
221
222 return errors;
223}
224
225int mmdc_do_dqs_calibration(struct mx6_ddr_sysinfo const *sysinfo)
226{
227 struct mmdc_p_regs *mmdc0 = (struct mmdc_p_regs *)MMDC_P0_BASE_ADDR;
228 struct mmdc_p_regs *mmdc1 = (struct mmdc_p_regs *)MMDC_P1_BASE_ADDR;
229 struct mx6dq_iomux_ddr_regs *mx6_ddr_iomux =
230 (struct mx6dq_iomux_ddr_regs *)MX6DQ_IOM_DDR_BASE;
231 bool cs0_enable;
232 bool cs1_enable;
233 bool cs0_enable_initial;
234 bool cs1_enable_initial;
235 u32 esdmisc_val;
236 u32 temp_ref;
237 u32 pddword = 0x00ffff00;
238 u32 errors = 0;
239 u32 initdelay = 0x40404040;
240
241
242 cs0_enable_initial = readl(&mmdc0->mdctl) & 0x80000000;
243 cs1_enable_initial = readl(&mmdc0->mdctl) & 0x40000000;
244
245
246 clrbits_le32(&mmdc0->mdpdc, 0xff00);
247
248
249 setbits_le32(&mmdc0->mapsr, 0x1);
250
251
252 setbits_le32(&mx6_ddr_iomux->dram_sdqs0, 0x7000);
253 setbits_le32(&mx6_ddr_iomux->dram_sdqs1, 0x7000);
254 setbits_le32(&mx6_ddr_iomux->dram_sdqs2, 0x7000);
255 setbits_le32(&mx6_ddr_iomux->dram_sdqs3, 0x7000);
256 setbits_le32(&mx6_ddr_iomux->dram_sdqs4, 0x7000);
257 setbits_le32(&mx6_ddr_iomux->dram_sdqs5, 0x7000);
258 setbits_le32(&mx6_ddr_iomux->dram_sdqs6, 0x7000);
259 setbits_le32(&mx6_ddr_iomux->dram_sdqs7, 0x7000);
260
261
262 esdmisc_val = readl(&mmdc0->mdmisc);
263
264 setbits_le32(&mmdc0->mdmisc,
265 (1 << 6) | (1 << 7) | (1 << 8) | (1 << 16) | (1 << 17));
266
267
268 temp_ref = readl(&mmdc0->mdref);
269 writel(0x0000c000, &mmdc0->mdref);
270
271
272
273
274
275 if (cs0_enable_initial)
276 writel(0x00008020, &mmdc0->mdscr);
277 if (cs1_enable_initial)
278 writel(0x00008028, &mmdc0->mdscr);
279
280
281 wait_for_bit("MMDC", &mmdc0->mdscr, 1 << 14, 1, 100, 0);
282
283
284
285
286
287
288
289
290
291
292 if ((readl(&mmdc0->mdmisc) & 0x00100000) == 0)
293 clrbits_le32(&mmdc0->mdctl, 1 << 30);
294 else
295 clrbits_le32(&mmdc0->mdctl, 1 << 31);
296
297
298
299
300
301 cs0_enable = readl(&mmdc0->mdctl) & 0x80000000;
302 cs1_enable = readl(&mmdc0->mdctl) & 0x40000000;
303
304 precharge_all(cs0_enable, cs1_enable);
305
306
307 writel(pddword, &mmdc0->mppdcmpr1);
308
309
310
311
312
313
314 setbits_le32(&mmdc0->mpswdar0, 1);
315 wait_for_bit("MMDC", &mmdc0->mpswdar0, 1 << 0, 0, 100, 0);
316
317
318
319
320
321 writel(initdelay, &mmdc0->mprddlctl);
322 if (sysinfo->dsize == 0x2)
323 writel(initdelay, &mmdc1->mprddlctl);
324
325
326 force_delay_measurement(sysinfo->dsize);
327
328
329
330
331
332
333 debug("Starting Read DQS Gating calibration.\n");
334
335
336
337
338
339 reset_read_data_fifos();
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354 setbits_le32(&mmdc0->mpdgctrl0, 1 << 30);
355 if (sysinfo->dsize == 2)
356 setbits_le32(&mmdc1->mpdgctrl0, 1 << 30);
357
358
359 setbits_le32(&mmdc0->mpdgctrl0, 5 << 28);
360
361
362 wait_for_bit("MMDC", &mmdc0->mpdgctrl0, 1 << 28, 0, 100, 0);
363
364
365
366
367
368
369 if (readl(&mmdc0->mpdgctrl0) & 0x00001000)
370 errors |= 1;
371
372 if ((sysinfo->dsize == 0x2) && (readl(&mmdc1->mpdgctrl0) & 0x00001000))
373 errors |= 2;
374
375
376 clrbits_le32(&mmdc0->mpdgctrl0, 1 << 30);
377 if (sysinfo->dsize == 2)
378 clrbits_le32(&mmdc1->mpdgctrl0, 1 << 30);
379
380
381
382
383
384
385 modify_dg_result(&mmdc0->mpdghwst0, &mmdc0->mpdghwst1,
386 &mmdc0->mpdgctrl0);
387 modify_dg_result(&mmdc0->mpdghwst2, &mmdc0->mpdghwst3,
388 &mmdc0->mpdgctrl1);
389 if (sysinfo->dsize == 0x2) {
390 modify_dg_result(&mmdc1->mpdghwst0, &mmdc1->mpdghwst1,
391 &mmdc1->mpdgctrl0);
392 modify_dg_result(&mmdc1->mpdghwst2, &mmdc1->mpdghwst3,
393 &mmdc1->mpdgctrl1);
394 }
395 debug("Ending Read DQS Gating calibration. Error mask: 0x%x\n", errors);
396
397
398
399
400
401
402 debug("Starting Read Delay calibration.\n");
403
404 reset_read_data_fifos();
405
406
407
408
409
410
411 precharge_all(cs0_enable, cs1_enable);
412
413
414
415
416
417
418 writel(0x00000030, &mmdc0->mprddlhwctl);
419
420
421
422
423
424
425
426 wait_for_bit("MMDC", &mmdc0->mprddlhwctl, 1 << 4, 0, 100, 0);
427
428
429 if (readl(&mmdc0->mprddlhwctl) & 0x0000000f)
430 errors |= 4;
431
432 if ((sysinfo->dsize == 0x2) &&
433 (readl(&mmdc1->mprddlhwctl) & 0x0000000f))
434 errors |= 8;
435
436 debug("Ending Read Delay calibration. Error mask: 0x%x\n", errors);
437
438
439
440
441
442
443 debug("Starting Write Delay calibration.\n");
444
445 reset_read_data_fifos();
446
447
448
449
450
451
452 precharge_all(cs0_enable, cs1_enable);
453
454
455
456
457
458 writel(initdelay, &mmdc0->mpwrdlctl);
459 if (sysinfo->dsize == 0x2)
460 writel(initdelay, &mmdc1->mpwrdlctl);
461
462
463
464
465
466 force_delay_measurement(sysinfo->dsize);
467
468
469
470
471
472 writel(0x00000030, &mmdc0->mpwrdlhwctl);
473
474
475
476
477
478
479
480 wait_for_bit("MMDC", &mmdc0->mpwrdlhwctl, 1 << 4, 0, 100, 0);
481
482
483 if (readl(&mmdc0->mpwrdlhwctl) & 0x0000000f)
484 errors |= 16;
485
486 if ((sysinfo->dsize == 0x2) &&
487 (readl(&mmdc1->mpwrdlhwctl) & 0x0000000f))
488 errors |= 32;
489
490 debug("Ending Write Delay calibration. Error mask: 0x%x\n", errors);
491
492 reset_read_data_fifos();
493
494
495 setbits_le32(&mmdc0->mdpdc, 0x00005500);
496
497
498 clrbits_le32(&mmdc0->mapsr, 0x1);
499
500
501 writel(esdmisc_val, &mmdc0->mdmisc);
502
503
504 clrbits_le32(&mx6_ddr_iomux->dram_sdqs0, 0x7000);
505 clrbits_le32(&mx6_ddr_iomux->dram_sdqs1, 0x7000);
506 clrbits_le32(&mx6_ddr_iomux->dram_sdqs2, 0x7000);
507 clrbits_le32(&mx6_ddr_iomux->dram_sdqs3, 0x7000);
508 clrbits_le32(&mx6_ddr_iomux->dram_sdqs4, 0x7000);
509 clrbits_le32(&mx6_ddr_iomux->dram_sdqs5, 0x7000);
510 clrbits_le32(&mx6_ddr_iomux->dram_sdqs6, 0x7000);
511 clrbits_le32(&mx6_ddr_iomux->dram_sdqs7, 0x7000);
512
513
514 if (cs1_enable_initial)
515
516 setbits_le32(&mmdc0->mdctl, 1 << 30);
517
518 if (cs0_enable_initial)
519
520 setbits_le32(&mmdc0->mdctl, 1 << 31);
521
522
523 writel(temp_ref, &mmdc0->mdref);
524
525
526 writel(0x0, &mmdc0->mdscr);
527
528
529 wait_for_bit("MMDC", &mmdc0->mdscr, 1 << 14, 0, 100, 0);
530
531
532
533
534
535 debug("MMDC registers updated from calibration\n");
536 debug("Read DQS gating calibration:\n");
537 debug("\tMPDGCTRL0 PHY0 = 0x%08X\n", readl(&mmdc0->mpdgctrl0));
538 debug("\tMPDGCTRL1 PHY0 = 0x%08X\n", readl(&mmdc0->mpdgctrl1));
539 if (sysinfo->dsize == 2) {
540 debug("\tMPDGCTRL0 PHY1 = 0x%08X\n", readl(&mmdc1->mpdgctrl0));
541 debug("\tMPDGCTRL1 PHY1 = 0x%08X\n", readl(&mmdc1->mpdgctrl1));
542 }
543 debug("Read calibration:\n");
544 debug("\tMPRDDLCTL PHY0 = 0x%08X\n", readl(&mmdc0->mprddlctl));
545 if (sysinfo->dsize == 2)
546 debug("\tMPRDDLCTL PHY1 = 0x%08X\n", readl(&mmdc1->mprddlctl));
547 debug("Write calibration:\n");
548 debug("\tMPWRDLCTL PHY0 = 0x%08X\n", readl(&mmdc0->mpwrdlctl));
549 if (sysinfo->dsize == 2)
550 debug("\tMPWRDLCTL PHY1 = 0x%08X\n", readl(&mmdc1->mpwrdlctl));
551
552
553
554
555
556
557 debug("Status registers bounds for read DQS gating:\n");
558 debug("\tMPDGHWST0 PHY0 = 0x%08x\n", readl(&mmdc0->mpdghwst0));
559 debug("\tMPDGHWST1 PHY0 = 0x%08x\n", readl(&mmdc0->mpdghwst1));
560 debug("\tMPDGHWST2 PHY0 = 0x%08x\n", readl(&mmdc0->mpdghwst2));
561 debug("\tMPDGHWST3 PHY0 = 0x%08x\n", readl(&mmdc0->mpdghwst3));
562 if (sysinfo->dsize == 2) {
563 debug("\tMPDGHWST0 PHY1 = 0x%08x\n", readl(&mmdc1->mpdghwst0));
564 debug("\tMPDGHWST1 PHY1 = 0x%08x\n", readl(&mmdc1->mpdghwst1));
565 debug("\tMPDGHWST2 PHY1 = 0x%08x\n", readl(&mmdc1->mpdghwst2));
566 debug("\tMPDGHWST3 PHY1 = 0x%08x\n", readl(&mmdc1->mpdghwst3));
567 }
568
569 debug("Final do_dqs_calibration error mask: 0x%x\n", errors);
570
571 return errors;
572}
573#endif
574
575#if defined(CONFIG_MX6SX)
576
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
588 writel(grp->grp_ddr_type, &mx6_grp_iomux->grp_ddr_type);
589 writel(grp->grp_ddrpke, &mx6_grp_iomux->grp_ddrpke);
590
591
592 writel(ddr->dram_sdclk_0, &mx6_ddr_iomux->dram_sdclk_0);
593
594
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
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
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
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
634#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
646 writel(grp->grp_ddr_type, &mx6_grp_iomux->grp_ddr_type);
647 writel(grp->grp_ddrpke, &mx6_grp_iomux->grp_ddrpke);
648
649
650 writel(ddr->dram_sdclk_0, &mx6_ddr_iomux->dram_sdclk_0);
651
652
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
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
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
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
678#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
690 mx6_grp_iomux->grp_ddr_type = grp->grp_ddr_type;
691 mx6_grp_iomux->grp_ddrpke = grp->grp_ddrpke;
692
693
694 mx6_ddr_iomux->dram_sdclk_0 = ddr->dram_sdclk_0;
695
696
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
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
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
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
733#if defined(CONFIG_MX6QDL) || defined(CONFIG_MX6Q) || defined(CONFIG_MX6D)
734
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
746 mx6_grp_iomux->grp_ddr_type = grp->grp_ddr_type;
747 mx6_grp_iomux->grp_ddrpke = grp->grp_ddrpke;
748
749
750 mx6_ddr_iomux->dram_sdclk_0 = ddr->dram_sdclk_0;
751 mx6_ddr_iomux->dram_sdclk_1 = ddr->dram_sdclk_1;
752
753
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
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
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
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
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
824 mx6_grp_iomux->grp_ddr_type = grp->grp_ddr_type;
825 mx6_grp_iomux->grp_ddrpke = grp->grp_ddrpke;
826
827
828 mx6_ddr_iomux->dram_sdclk_0 = ddr->dram_sdclk_0;
829 mx6_ddr_iomux->dram_sdclk_1 = ddr->dram_sdclk_1;
830
831
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
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
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
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
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908#define MR(val, ba, cmd, cs1) \
909 ((val << 16) | (1 << 15) | (cmd << 4) | (cs1 << 3) | ba)
910#define MMDC1(entry, value) do { \
911 if (!is_mx6sx() && !is_mx6ul() && !is_mx6sl()) \
912 mmdc1->entry = value; \
913 } while (0)
914
915
916
917
918
919static int lpddr2_wl(uint32_t mem_speed)
920{
921 switch (mem_speed) {
922 case 1066:
923 case 933:
924 return 4;
925 case 800:
926 return 3;
927 case 677:
928 case 533:
929 return 2;
930 case 400:
931 case 333:
932 return 1;
933 default:
934 puts("invalid memory speed\n");
935 hang();
936 }
937
938 return 0;
939}
940
941
942
943
944
945static int lpddr2_rl(uint32_t mem_speed)
946{
947 switch (mem_speed) {
948 case 1066:
949 return 8;
950 case 933:
951 return 7;
952 case 800:
953 return 6;
954 case 677:
955 return 5;
956 case 533:
957 return 4;
958 case 400:
959 case 333:
960 return 3;
961 default:
962 puts("invalid memory speed\n");
963 hang();
964 }
965
966 return 0;
967}
968
969void mx6_lpddr2_cfg(const struct mx6_ddr_sysinfo *sysinfo,
970 const struct mx6_mmdc_calibration *calib,
971 const struct mx6_lpddr2_cfg *lpddr2_cfg)
972{
973 volatile struct mmdc_p_regs *mmdc0;
974 u32 val;
975 u8 tcke, tcksrx, tcksre, trrd;
976 u8 twl, txp, tfaw, tcl;
977 u16 tras, twr, tmrd, trtp, twtr, trfc, txsr;
978 u16 trcd_lp, trppb_lp, trpab_lp, trc_lp;
979 u16 cs0_end;
980 u8 coladdr;
981 int clkper;
982 int clock;
983 int cs;
984
985
986 if (sysinfo->dsize > 1)
987 hang();
988
989 mmdc0 = (struct mmdc_p_regs *)MMDC_P0_BASE_ADDR;
990
991 clock = mxc_get_clock(MXC_DDR_CLK) / 1000000U;
992 clkper = (1000 * 1000) / clock;
993
994 twl = lpddr2_wl(lpddr2_cfg->mem_speed) - 1;
995
996
997 switch (lpddr2_cfg->density) {
998 case 1:
999 case 2:
1000 case 4:
1001 trfc = DIV_ROUND_UP(130000, clkper) - 1;
1002 txsr = DIV_ROUND_UP(140000, clkper) - 1;
1003 break;
1004 case 8:
1005 trfc = DIV_ROUND_UP(210000, clkper) - 1;
1006 txsr = DIV_ROUND_UP(220000, clkper) - 1;
1007 break;
1008 default:
1009
1010
1011
1012 hang();
1013 break;
1014 }
1015
1016
1017
1018 txp = DIV_ROUND_UP(7500, clkper) - 1;
1019 tcke = 3;
1020 if (lpddr2_cfg->mem_speed == 333)
1021 tfaw = DIV_ROUND_UP(60000, clkper) - 1;
1022 else
1023 tfaw = DIV_ROUND_UP(50000, clkper) - 1;
1024 trrd = DIV_ROUND_UP(10000, clkper) - 1;
1025
1026
1027 tcksre = DIV_ROUND_UP(15000, clkper);
1028 tcksrx = tcksre;
1029 twr = DIV_ROUND_UP(15000, clkper) - 1;
1030
1031
1032
1033
1034 tmrd = 5;
1035 tras = DIV_ROUND_UP(lpddr2_cfg->trasmin, clkper / 10) - 1;
1036
1037 trcd_lp = DIV_ROUND_UP(lpddr2_cfg->trcd_lp, clkper / 10) - 1;
1038 trc_lp = DIV_ROUND_UP(lpddr2_cfg->trasmin + lpddr2_cfg->trppb_lp,
1039 clkper / 10) - 1;
1040 trppb_lp = DIV_ROUND_UP(lpddr2_cfg->trppb_lp, clkper / 10) - 1;
1041 trpab_lp = DIV_ROUND_UP(lpddr2_cfg->trpab_lp, clkper / 10) - 1;
1042
1043 tcl = lpddr2_rl(lpddr2_cfg->mem_speed) - 3;
1044 twtr = DIV_ROUND_UP(7500, clkper) - 1;
1045 trtp = DIV_ROUND_UP(7500, clkper) - 1;
1046
1047 cs0_end = 4 * sysinfo->cs_density - 1;
1048
1049 debug("density:%d Gb (%d Gb per chip)\n",
1050 sysinfo->cs_density, lpddr2_cfg->density);
1051 debug("clock: %dMHz (%d ps)\n", clock, clkper);
1052 debug("memspd:%d\n", lpddr2_cfg->mem_speed);
1053 debug("trcd_lp=%d\n", trcd_lp);
1054 debug("trppb_lp=%d\n", trppb_lp);
1055 debug("trpab_lp=%d\n", trpab_lp);
1056 debug("trc_lp=%d\n", trc_lp);
1057 debug("tcke=%d\n", tcke);
1058 debug("tcksrx=%d\n", tcksrx);
1059 debug("tcksre=%d\n", tcksre);
1060 debug("trfc=%d\n", trfc);
1061 debug("txsr=%d\n", txsr);
1062 debug("txp=%d\n", txp);
1063 debug("tfaw=%d\n", tfaw);
1064 debug("tcl=%d\n", tcl);
1065 debug("tras=%d\n", tras);
1066 debug("twr=%d\n", twr);
1067 debug("tmrd=%d\n", tmrd);
1068 debug("twl=%d\n", twl);
1069 debug("trtp=%d\n", trtp);
1070 debug("twtr=%d\n", twtr);
1071 debug("trrd=%d\n", trrd);
1072 debug("cs0_end=%d\n", cs0_end);
1073 debug("ncs=%d\n", sysinfo->ncs);
1074
1075
1076
1077
1078
1079 mmdc0->mpwldectrl0 = calib->p0_mpwldectrl0;
1080 mmdc0->mpwldectrl1 = calib->p0_mpwldectrl1;
1081 mmdc0->mpdgctrl0 = calib->p0_mpdgctrl0;
1082 mmdc0->mpdgctrl1 = calib->p0_mpdgctrl1;
1083 mmdc0->mprddlctl = calib->p0_mprddlctl;
1084 mmdc0->mpwrdlctl = calib->p0_mpwrdlctl;
1085 mmdc0->mpzqlp2ctl = calib->mpzqlp2ctl;
1086
1087
1088 mmdc0->mprddqby0dl = 0x33333333;
1089 mmdc0->mprddqby1dl = 0x33333333;
1090 if (sysinfo->dsize > 0) {
1091 mmdc0->mprddqby2dl = 0x33333333;
1092 mmdc0->mprddqby3dl = 0x33333333;
1093 }
1094
1095
1096 mmdc0->mpwrdqby0dl = 0xf3333333;
1097 mmdc0->mpwrdqby1dl = 0xf3333333;
1098 if (sysinfo->dsize > 0) {
1099 mmdc0->mpwrdqby2dl = 0xf3333333;
1100 mmdc0->mpwrdqby3dl = 0xf3333333;
1101 }
1102
1103
1104
1105
1106
1107 mmdc0->mpodtctrl = 0;
1108
1109
1110 val = (1 << 11);
1111 mmdc0->mpmur0 = val;
1112
1113
1114 mmdc0->mdscr = (u32)(1 << 15);
1115
1116
1117 mmdc0->mdcfg0 = (trfc << 24) | (txsr << 16) | (txp << 13) |
1118 (tfaw << 4) | tcl;
1119 mmdc0->mdcfg1 = (tras << 16) | (twr << 9) | (tmrd << 5) | twl;
1120 mmdc0->mdcfg2 = (trtp << 6) | (twtr << 3) | trrd;
1121 mmdc0->mdcfg3lp = (trc_lp << 16) | (trcd_lp << 8) |
1122 (trppb_lp << 4) | trpab_lp;
1123 mmdc0->mdotc = 0;
1124
1125 mmdc0->mdasp = cs0_end;
1126
1127
1128 mmdc0->mdmisc = (sysinfo->cs1_mirror << 19) | (sysinfo->walat << 16) |
1129 (sysinfo->bi_on << 12) | (sysinfo->mif3_mode << 9) |
1130 (sysinfo->ralat << 6) | (1 << 3);
1131
1132
1133 mmdc0->mdor = (sysinfo->sde_to_rst << 8) |
1134 (sysinfo->rst_to_cke << 0);
1135
1136
1137 coladdr = lpddr2_cfg->coladdr;
1138 if (lpddr2_cfg->coladdr == 8)
1139 coladdr += 4;
1140 else if (lpddr2_cfg->coladdr == 12)
1141 coladdr += 1;
1142 mmdc0->mdctl = (lpddr2_cfg->rowaddr - 11) << 24 |
1143 (coladdr - 9) << 20 |
1144 (0 << 19) |
1145 (sysinfo->dsize << 16);
1146
1147
1148 val = 0xa1390003;
1149 mmdc0->mpzqhwctrl = val;
1150
1151
1152 mmdc0->mdctl |= (1 << 31) |
1153 ((sysinfo->ncs == 2) ? 1 : 0) << 30;
1154
1155
1156 for (cs = 0; cs < sysinfo->ncs; cs++) {
1157
1158 mmdc0->mdscr = MR(63, 0, 3, cs);
1159
1160
1161
1162 val = 0xA | (0xff << 8);
1163 mmdc0->mdscr = MR(val, 0, 3, cs);
1164
1165 val = 0x1 | (0x82 << 8);
1166 mmdc0->mdscr = MR(val, 0, 3, cs);
1167
1168 val = 0x2 | (0x04 << 8);
1169 mmdc0->mdscr = MR(val, 0, 3, cs);
1170
1171 val = 0x3 | (0x02 << 8);
1172 mmdc0->mdscr = MR(val, 0, 3, cs);
1173 }
1174
1175
1176 mmdc0->mdpdc = (tcke & 0x7) << 16 |
1177 5 << 12 |
1178 5 << 8 |
1179 1 << 6 |
1180 (tcksrx & 0x7) << 3 |
1181 (tcksre & 0x7);
1182 mmdc0->mapsr = 0x00001006;
1183
1184
1185 val = 0xa1310003;
1186 mmdc0->mpzqhwctrl = val;
1187
1188
1189 mmdc0->mdref = (sysinfo->refsel << 14) | (sysinfo->refr << 11);
1190
1191
1192 mmdc0->mdscr = 0x00000000;
1193
1194
1195 mdelay(1);
1196}
1197
1198void mx6_ddr3_cfg(const struct mx6_ddr_sysinfo *sysinfo,
1199 const struct mx6_mmdc_calibration *calib,
1200 const struct mx6_ddr3_cfg *ddr3_cfg)
1201{
1202 volatile struct mmdc_p_regs *mmdc0;
1203 volatile struct mmdc_p_regs *mmdc1;
1204 u32 val;
1205 u8 tcke, tcksrx, tcksre, txpdll, taofpd, taonpd, trrd;
1206 u8 todtlon, taxpd, tanpd, tcwl, txp, tfaw, tcl;
1207 u8 todt_idle_off = 0x4;
1208 u16 trcd, trc, tras, twr, tmrd, trtp, trp, twtr, trfc, txs, txpr;
1209 u16 cs0_end;
1210 u16 tdllk = 0x1ff;
1211 u8 coladdr;
1212 int clkper;
1213 int clock;
1214 int cs;
1215 u16 mem_speed = ddr3_cfg->mem_speed;
1216
1217 mmdc0 = (struct mmdc_p_regs *)MMDC_P0_BASE_ADDR;
1218 if (!is_mx6sx() && !is_mx6ul() && !is_mx6sl())
1219 mmdc1 = (struct mmdc_p_regs *)MMDC_P1_BASE_ADDR;
1220
1221
1222 if (is_mx6dq() || is_mx6dqp()) {
1223 if (mem_speed > 1066)
1224 mem_speed = 1066;
1225
1226 tcwl = 4;
1227 }
1228
1229 else {
1230 if (mem_speed > 800)
1231 mem_speed = 800;
1232
1233 tcwl = 3;
1234 }
1235
1236 clock = mem_speed / 2;
1237
1238
1239
1240
1241 if (is_mx6dq() || is_mx6dqp()) {
1242 if (clock > 528)
1243 clock = 528;
1244 }
1245
1246 clkper = (1000 * 1000) / clock;
1247 todtlon = tcwl;
1248 taxpd = tcwl;
1249 tanpd = tcwl;
1250
1251 switch (ddr3_cfg->density) {
1252 case 1:
1253 trfc = DIV_ROUND_UP(110000, clkper) - 1;
1254 txs = DIV_ROUND_UP(120000, clkper) - 1;
1255 break;
1256 case 2:
1257 trfc = DIV_ROUND_UP(160000, clkper) - 1;
1258 txs = DIV_ROUND_UP(170000, clkper) - 1;
1259 break;
1260 case 4:
1261 trfc = DIV_ROUND_UP(260000, clkper) - 1;
1262 txs = DIV_ROUND_UP(270000, clkper) - 1;
1263 break;
1264 case 8:
1265 trfc = DIV_ROUND_UP(350000, clkper) - 1;
1266 txs = DIV_ROUND_UP(360000, clkper) - 1;
1267 break;
1268 default:
1269
1270 puts("invalid chip density\n");
1271 hang();
1272 break;
1273 }
1274 txpr = txs;
1275
1276 switch (mem_speed) {
1277 case 800:
1278 txp = DIV_ROUND_UP(max(3 * clkper, 7500), clkper) - 1;
1279 tcke = DIV_ROUND_UP(max(3 * clkper, 7500), clkper) - 1;
1280 if (ddr3_cfg->pagesz == 1) {
1281 tfaw = DIV_ROUND_UP(40000, clkper) - 1;
1282 trrd = DIV_ROUND_UP(max(4 * clkper, 10000), clkper) - 1;
1283 } else {
1284 tfaw = DIV_ROUND_UP(50000, clkper) - 1;
1285 trrd = DIV_ROUND_UP(max(4 * clkper, 10000), clkper) - 1;
1286 }
1287 break;
1288 case 1066:
1289 txp = DIV_ROUND_UP(max(3 * clkper, 7500), clkper) - 1;
1290 tcke = DIV_ROUND_UP(max(3 * clkper, 5625), clkper) - 1;
1291 if (ddr3_cfg->pagesz == 1) {
1292 tfaw = DIV_ROUND_UP(37500, clkper) - 1;
1293 trrd = DIV_ROUND_UP(max(4 * clkper, 7500), clkper) - 1;
1294 } else {
1295 tfaw = DIV_ROUND_UP(50000, clkper) - 1;
1296 trrd = DIV_ROUND_UP(max(4 * clkper, 10000), clkper) - 1;
1297 }
1298 break;
1299 default:
1300 puts("invalid memory speed\n");
1301 hang();
1302 break;
1303 }
1304 txpdll = DIV_ROUND_UP(max(10 * clkper, 24000), clkper) - 1;
1305 tcksre = DIV_ROUND_UP(max(5 * clkper, 10000), clkper);
1306 taonpd = DIV_ROUND_UP(2000, clkper) - 1;
1307 tcksrx = tcksre;
1308 taofpd = taonpd;
1309 twr = DIV_ROUND_UP(15000, clkper) - 1;
1310 tmrd = DIV_ROUND_UP(max(12 * clkper, 15000), clkper) - 1;
1311 trc = DIV_ROUND_UP(ddr3_cfg->trcmin, clkper / 10) - 1;
1312 tras = DIV_ROUND_UP(ddr3_cfg->trasmin, clkper / 10) - 1;
1313 tcl = DIV_ROUND_UP(ddr3_cfg->trcd, clkper / 10) - 3;
1314 trp = DIV_ROUND_UP(ddr3_cfg->trcd, clkper / 10) - 1;
1315 twtr = ROUND(max(4 * clkper, 7500) / clkper, 1) - 1;
1316 trcd = trp;
1317 trtp = twtr;
1318 cs0_end = 4 * sysinfo->cs_density - 1;
1319
1320 debug("density:%d Gb (%d Gb per chip)\n",
1321 sysinfo->cs_density, ddr3_cfg->density);
1322 debug("clock: %dMHz (%d ps)\n", clock, clkper);
1323 debug("memspd:%d\n", mem_speed);
1324 debug("tcke=%d\n", tcke);
1325 debug("tcksrx=%d\n", tcksrx);
1326 debug("tcksre=%d\n", tcksre);
1327 debug("taofpd=%d\n", taofpd);
1328 debug("taonpd=%d\n", taonpd);
1329 debug("todtlon=%d\n", todtlon);
1330 debug("tanpd=%d\n", tanpd);
1331 debug("taxpd=%d\n", taxpd);
1332 debug("trfc=%d\n", trfc);
1333 debug("txs=%d\n", txs);
1334 debug("txp=%d\n", txp);
1335 debug("txpdll=%d\n", txpdll);
1336 debug("tfaw=%d\n", tfaw);
1337 debug("tcl=%d\n", tcl);
1338 debug("trcd=%d\n", trcd);
1339 debug("trp=%d\n", trp);
1340 debug("trc=%d\n", trc);
1341 debug("tras=%d\n", tras);
1342 debug("twr=%d\n", twr);
1343 debug("tmrd=%d\n", tmrd);
1344 debug("tcwl=%d\n", tcwl);
1345 debug("tdllk=%d\n", tdllk);
1346 debug("trtp=%d\n", trtp);
1347 debug("twtr=%d\n", twtr);
1348 debug("trrd=%d\n", trrd);
1349 debug("txpr=%d\n", txpr);
1350 debug("cs0_end=%d\n", cs0_end);
1351 debug("ncs=%d\n", sysinfo->ncs);
1352 debug("Rtt_wr=%d\n", sysinfo->rtt_wr);
1353 debug("Rtt_nom=%d\n", sysinfo->rtt_nom);
1354 debug("SRT=%d\n", ddr3_cfg->SRT);
1355 debug("twr=%d\n", twr);
1356
1357
1358
1359
1360
1361
1362
1363 mmdc0->mpwldectrl0 = calib->p0_mpwldectrl0;
1364 mmdc0->mpwldectrl1 = calib->p0_mpwldectrl1;
1365 mmdc0->mpdgctrl0 = calib->p0_mpdgctrl0;
1366 mmdc0->mpdgctrl1 = calib->p0_mpdgctrl1;
1367 mmdc0->mprddlctl = calib->p0_mprddlctl;
1368 mmdc0->mpwrdlctl = calib->p0_mpwrdlctl;
1369 if (sysinfo->dsize > 1) {
1370 MMDC1(mpwldectrl0, calib->p1_mpwldectrl0);
1371 MMDC1(mpwldectrl1, calib->p1_mpwldectrl1);
1372 MMDC1(mpdgctrl0, calib->p1_mpdgctrl0);
1373 MMDC1(mpdgctrl1, calib->p1_mpdgctrl1);
1374 MMDC1(mprddlctl, calib->p1_mprddlctl);
1375 MMDC1(mpwrdlctl, calib->p1_mpwrdlctl);
1376 }
1377
1378
1379 mmdc0->mprddqby0dl = 0x33333333;
1380 mmdc0->mprddqby1dl = 0x33333333;
1381 if (sysinfo->dsize > 0) {
1382 mmdc0->mprddqby2dl = 0x33333333;
1383 mmdc0->mprddqby3dl = 0x33333333;
1384 }
1385
1386 if (sysinfo->dsize > 1) {
1387 MMDC1(mprddqby0dl, 0x33333333);
1388 MMDC1(mprddqby1dl, 0x33333333);
1389 MMDC1(mprddqby2dl, 0x33333333);
1390 MMDC1(mprddqby3dl, 0x33333333);
1391 }
1392
1393
1394 val = (sysinfo->rtt_nom == 2) ? 0x00011117 : 0x00022227;
1395 mmdc0->mpodtctrl = val;
1396 if (sysinfo->dsize > 1)
1397 MMDC1(mpodtctrl, val);
1398
1399
1400 val = (1 << 11);
1401 mmdc0->mpmur0 = val;
1402 if (sysinfo->dsize > 1)
1403 MMDC1(mpmur0, val);
1404
1405
1406 mmdc0->mdscr = (u32)(1 << 15);
1407
1408
1409 mmdc0->mdcfg0 = (trfc << 24) | (txs << 16) | (txp << 13) |
1410 (txpdll << 9) | (tfaw << 4) | tcl;
1411 mmdc0->mdcfg1 = (trcd << 29) | (trp << 26) | (trc << 21) |
1412 (tras << 16) | (1 << 15) |
1413 (twr << 9) | (tmrd << 5) | tcwl;
1414 mmdc0->mdcfg2 = (tdllk << 16) | (trtp << 6) | (twtr << 3) | trrd;
1415 mmdc0->mdotc = (taofpd << 27) | (taonpd << 24) | (tanpd << 20) |
1416 (taxpd << 16) | (todtlon << 12) | (todt_idle_off << 4);
1417 mmdc0->mdasp = cs0_end;
1418
1419
1420 mmdc0->mdmisc = (sysinfo->cs1_mirror << 19) | (sysinfo->walat << 16) |
1421 (sysinfo->bi_on << 12) | (sysinfo->mif3_mode << 9) |
1422 (sysinfo->ralat << 6);
1423
1424
1425 mmdc0->mdor = (txpr << 16) | (sysinfo->sde_to_rst << 8) |
1426 (sysinfo->rst_to_cke << 0);
1427
1428
1429 coladdr = ddr3_cfg->coladdr;
1430 if (ddr3_cfg->coladdr == 8)
1431 coladdr += 4;
1432 else if (ddr3_cfg->coladdr == 12)
1433 coladdr += 1;
1434 mmdc0->mdctl = (ddr3_cfg->rowaddr - 11) << 24 |
1435 (coladdr - 9) << 20 |
1436 (1 << 19) |
1437 (sysinfo->dsize << 16);
1438
1439
1440 val = 0xa1390001;
1441 mmdc0->mpzqhwctrl = val;
1442 if (sysinfo->dsize > 1)
1443 MMDC1(mpzqhwctrl, val);
1444
1445
1446 mmdc0->mdctl |= (1 << 31) |
1447 ((sysinfo->ncs == 2) ? 1 : 0) << 30;
1448
1449
1450 for (cs = 0; cs < sysinfo->ncs; cs++) {
1451
1452 val = (sysinfo->rtt_wr & 3) << 9 | (ddr3_cfg->SRT & 1) << 7 |
1453 ((tcwl - 3) & 3) << 3;
1454 debug("MR2 CS%d: 0x%08x\n", cs, (u32)MR(val, 2, 3, cs));
1455 mmdc0->mdscr = MR(val, 2, 3, cs);
1456
1457 debug("MR3 CS%d: 0x%08x\n", cs, (u32)MR(0, 3, 3, cs));
1458 mmdc0->mdscr = MR(0, 3, 3, cs);
1459
1460 val = ((sysinfo->rtt_nom & 1) ? 1 : 0) << 2 |
1461 ((sysinfo->rtt_nom & 2) ? 1 : 0) << 6;
1462 debug("MR1 CS%d: 0x%08x\n", cs, (u32)MR(val, 1, 3, cs));
1463 mmdc0->mdscr = MR(val, 1, 3, cs);
1464
1465 val = ((tcl - 1) << 4) |
1466 (1 << 8) |
1467 ((twr - 3) << 9) |
1468 (sysinfo->pd_fast_exit << 12);
1469 debug("MR0 CS%d: 0x%08x\n", cs, (u32)MR(val, 0, 3, cs));
1470 mmdc0->mdscr = MR(val, 0, 3, cs);
1471
1472 val = (1 << 10);
1473 mmdc0->mdscr = MR(val, 0, 4, cs);
1474 }
1475
1476
1477 mmdc0->mdpdc = (tcke & 0x7) << 16 |
1478 5 << 12 |
1479 5 << 8 |
1480 1 << 6 |
1481 (tcksrx & 0x7) << 3 |
1482 (tcksre & 0x7);
1483 if (!sysinfo->pd_fast_exit)
1484 mmdc0->mdpdc |= (1 << 7);
1485 mmdc0->mapsr = 0x00001006;
1486
1487
1488 val = 0xa1390003;
1489 mmdc0->mpzqhwctrl = val;
1490 if (sysinfo->dsize > 1)
1491 MMDC1(mpzqhwctrl, val);
1492
1493
1494 mmdc0->mdref = (sysinfo->refsel << 14) | (sysinfo->refr << 11);
1495
1496
1497 mmdc0->mdscr = 0x00000000;
1498
1499
1500 mdelay(1);
1501}
1502
1503void mmdc_read_calibration(struct mx6_ddr_sysinfo const *sysinfo,
1504 struct mx6_mmdc_calibration *calib)
1505{
1506 struct mmdc_p_regs *mmdc0 = (struct mmdc_p_regs *)MMDC_P0_BASE_ADDR;
1507 struct mmdc_p_regs *mmdc1 = (struct mmdc_p_regs *)MMDC_P1_BASE_ADDR;
1508
1509 calib->p0_mpwldectrl0 = readl(&mmdc0->mpwldectrl0);
1510 calib->p0_mpwldectrl1 = readl(&mmdc0->mpwldectrl1);
1511 calib->p0_mpdgctrl0 = readl(&mmdc0->mpdgctrl0);
1512 calib->p0_mpdgctrl1 = readl(&mmdc0->mpdgctrl1);
1513 calib->p0_mprddlctl = readl(&mmdc0->mprddlctl);
1514 calib->p0_mpwrdlctl = readl(&mmdc0->mpwrdlctl);
1515
1516 if (sysinfo->dsize == 2) {
1517 calib->p1_mpwldectrl0 = readl(&mmdc1->mpwldectrl0);
1518 calib->p1_mpwldectrl1 = readl(&mmdc1->mpwldectrl1);
1519 calib->p1_mpdgctrl0 = readl(&mmdc1->mpdgctrl0);
1520 calib->p1_mpdgctrl1 = readl(&mmdc1->mpdgctrl1);
1521 calib->p1_mprddlctl = readl(&mmdc1->mprddlctl);
1522 calib->p1_mpwrdlctl = readl(&mmdc1->mpwrdlctl);
1523 }
1524}
1525
1526void mx6_dram_cfg(const struct mx6_ddr_sysinfo *sysinfo,
1527 const struct mx6_mmdc_calibration *calib,
1528 const void *ddr_cfg)
1529{
1530 if (sysinfo->ddr_type == DDR_TYPE_DDR3) {
1531 mx6_ddr3_cfg(sysinfo, calib, ddr_cfg);
1532 } else if (sysinfo->ddr_type == DDR_TYPE_LPDDR2) {
1533 mx6_lpddr2_cfg(sysinfo, calib, ddr_cfg);
1534 } else {
1535 puts("Unsupported ddr type\n");
1536 hang();
1537 }
1538}
1539