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_MX6QDL) || defined(CONFIG_MX6Q) || defined(CONFIG_MX6D)
18
19static void reset_read_data_fifos(void)
20{
21 struct mmdc_p_regs *mmdc0 = (struct mmdc_p_regs *)MMDC_P0_BASE_ADDR;
22
23
24 setbits_le32(&mmdc0->mpdgctrl0, 1 << 31);
25 wait_for_bit("MMDC", &mmdc0->mpdgctrl0, 1 << 31, 0, 100, 0);
26
27 setbits_le32(&mmdc0->mpdgctrl0, 1 << 31);
28 wait_for_bit("MMDC", &mmdc0->mpdgctrl0, 1 << 31, 0, 100, 0);
29}
30
31static void precharge_all(const bool cs0_enable, const bool cs1_enable)
32{
33 struct mmdc_p_regs *mmdc0 = (struct mmdc_p_regs *)MMDC_P0_BASE_ADDR;
34
35
36
37
38
39
40
41 if (cs0_enable) {
42 writel(0x04008050, &mmdc0->mdscr);
43 wait_for_bit("MMDC", &mmdc0->mdscr, 1 << 14, 1, 100, 0);
44 }
45
46 if (cs1_enable) {
47 writel(0x04008058, &mmdc0->mdscr);
48 wait_for_bit("MMDC", &mmdc0->mdscr, 1 << 14, 1, 100, 0);
49 }
50}
51
52static void force_delay_measurement(int bus_size)
53{
54 struct mmdc_p_regs *mmdc0 = (struct mmdc_p_regs *)MMDC_P0_BASE_ADDR;
55 struct mmdc_p_regs *mmdc1 = (struct mmdc_p_regs *)MMDC_P1_BASE_ADDR;
56
57 writel(0x800, &mmdc0->mpmur0);
58 if (bus_size == 0x2)
59 writel(0x800, &mmdc1->mpmur0);
60}
61
62static void modify_dg_result(u32 *reg_st0, u32 *reg_st1, u32 *reg_ctrl)
63{
64 u32 dg_tmp_val, dg_dl_abs_offset, dg_hc_del, val_ctrl;
65
66
67
68
69
70
71 val_ctrl = readl(reg_ctrl);
72 val_ctrl &= 0xf0000000;
73
74 dg_tmp_val = ((readl(reg_st0) & 0x07ff0000) >> 16) - 0xc0;
75 dg_dl_abs_offset = dg_tmp_val & 0x7f;
76 dg_hc_del = (dg_tmp_val & 0x780) << 1;
77
78 val_ctrl |= dg_dl_abs_offset + dg_hc_del;
79
80 dg_tmp_val = ((readl(reg_st1) & 0x07ff0000) >> 16) - 0xc0;
81 dg_dl_abs_offset = dg_tmp_val & 0x7f;
82 dg_hc_del = (dg_tmp_val & 0x780) << 1;
83
84 val_ctrl |= (dg_dl_abs_offset + dg_hc_del) << 16;
85
86 writel(val_ctrl, reg_ctrl);
87}
88
89int mmdc_do_write_level_calibration(void)
90{
91 struct mmdc_p_regs *mmdc0 = (struct mmdc_p_regs *)MMDC_P0_BASE_ADDR;
92 struct mmdc_p_regs *mmdc1 = (struct mmdc_p_regs *)MMDC_P1_BASE_ADDR;
93 u32 esdmisc_val, zq_val;
94 u32 errors = 0;
95 u32 ldectrl[4];
96 u32 ddr_mr1 = 0x4;
97
98
99
100
101
102 ldectrl[0] = readl(&mmdc0->mpwldectrl0);
103 ldectrl[1] = readl(&mmdc0->mpwldectrl1);
104 ldectrl[2] = readl(&mmdc1->mpwldectrl0);
105 ldectrl[3] = readl(&mmdc1->mpwldectrl1);
106
107
108 clrbits_le32(&mmdc0->mdpdc, 0xff00);
109
110
111 setbits_le32(&mmdc0->mapsr, 0x1);
112
113 debug("Starting write leveling calibration.\n");
114
115
116
117
118
119 esdmisc_val = readl(&mmdc0->mdref);
120 writel(0x0000C000, &mmdc0->mdref);
121 zq_val = readl(&mmdc0->mpzqhwctrl);
122 writel(zq_val & ~0x3, &mmdc0->mpzqhwctrl);
123
124
125 setbits_le32(&mmdc0->mdmisc,
126 (1 << 6) | (1 << 7) | (1 << 8) | (1 << 16) | (1 << 17));
127 setbits_le32(&mmdc1->mdmisc,
128 (1 << 6) | (1 << 7) | (1 << 8) | (1 << 16) | (1 << 17));
129
130
131
132
133
134
135
136
137
138 writel(0x00808231, &mmdc0->mdscr);
139
140
141 writel(0x00000001, &mmdc0->mpwlgcr);
142
143
144
145
146
147 wait_for_bit("MMDC", &mmdc0->mpwlgcr, 1 << 0, 0, 100, 0);
148
149
150
151
152
153 if (readl(&mmdc0->mpwlgcr) & 0x00000F00)
154 errors |= 1;
155 if (readl(&mmdc1->mpwlgcr) & 0x00000F00)
156 errors |= 2;
157
158 debug("Ending write leveling calibration. Error mask: 0x%x\n", errors);
159
160
161 if ((readl(&mmdc0->mpwldectrl0) == 0x001F001F) &&
162 (readl(&mmdc0->mpwldectrl1) == 0x001F001F) &&
163 (readl(&mmdc1->mpwldectrl0) == 0x001F001F) &&
164 (readl(&mmdc1->mpwldectrl1) == 0x001F001F)) {
165 debug("Cal seems to have soft-failed due to memory not supporting write leveling on all channels. Restoring original write leveling values.\n");
166 writel(ldectrl[0], &mmdc0->mpwldectrl0);
167 writel(ldectrl[1], &mmdc0->mpwldectrl1);
168 writel(ldectrl[2], &mmdc1->mpwldectrl0);
169 writel(ldectrl[3], &mmdc1->mpwldectrl1);
170 errors |= 4;
171 }
172
173
174
175
176
177
178
179
180
181
182 writel((ddr_mr1 << 16) + 0x8031, &mmdc0->mdscr);
183
184
185 writel(esdmisc_val, &mmdc0->mdref);
186 writel(zq_val, &mmdc0->mpzqhwctrl);
187
188 debug("\tMMDC_MPWLDECTRL0 after write level cal: 0x%08X\n",
189 readl(&mmdc0->mpwldectrl0));
190 debug("\tMMDC_MPWLDECTRL1 after write level cal: 0x%08X\n",
191 readl(&mmdc0->mpwldectrl1));
192 debug("\tMMDC_MPWLDECTRL0 after write level cal: 0x%08X\n",
193 readl(&mmdc1->mpwldectrl0));
194 debug("\tMMDC_MPWLDECTRL1 after write level cal: 0x%08X\n",
195 readl(&mmdc1->mpwldectrl1));
196
197
198 readl(&mmdc0->mpwldectrl0);
199 readl(&mmdc0->mpwldectrl1);
200 readl(&mmdc1->mpwldectrl0);
201 readl(&mmdc1->mpwldectrl1);
202
203
204 setbits_le32(&mmdc0->mdpdc, 0x00005500);
205
206
207 clrbits_le32(&mmdc0->mapsr, 0x1);
208
209
210 writel(0, &mmdc0->mdscr);
211
212 return errors;
213}
214
215int mmdc_do_dqs_calibration(void)
216{
217 struct mmdc_p_regs *mmdc0 = (struct mmdc_p_regs *)MMDC_P0_BASE_ADDR;
218 struct mmdc_p_regs *mmdc1 = (struct mmdc_p_regs *)MMDC_P1_BASE_ADDR;
219 struct mx6dq_iomux_ddr_regs *mx6_ddr_iomux =
220 (struct mx6dq_iomux_ddr_regs *)MX6DQ_IOM_DDR_BASE;
221 bool cs0_enable;
222 bool cs1_enable;
223 bool cs0_enable_initial;
224 bool cs1_enable_initial;
225 u32 esdmisc_val;
226 u32 bus_size;
227 u32 temp_ref;
228 u32 pddword = 0x00ffff00;
229 u32 errors = 0;
230 u32 initdelay = 0x40404040;
231
232
233 cs0_enable_initial = readl(&mmdc0->mdctl) & 0x80000000;
234 cs1_enable_initial = readl(&mmdc0->mdctl) & 0x40000000;
235
236
237 clrbits_le32(&mmdc0->mdpdc, 0xff00);
238
239
240 setbits_le32(&mmdc0->mapsr, 0x1);
241
242
243 setbits_le32(&mx6_ddr_iomux->dram_sdqs0, 0x7000);
244 setbits_le32(&mx6_ddr_iomux->dram_sdqs1, 0x7000);
245 setbits_le32(&mx6_ddr_iomux->dram_sdqs2, 0x7000);
246 setbits_le32(&mx6_ddr_iomux->dram_sdqs3, 0x7000);
247 setbits_le32(&mx6_ddr_iomux->dram_sdqs4, 0x7000);
248 setbits_le32(&mx6_ddr_iomux->dram_sdqs5, 0x7000);
249 setbits_le32(&mx6_ddr_iomux->dram_sdqs6, 0x7000);
250 setbits_le32(&mx6_ddr_iomux->dram_sdqs7, 0x7000);
251
252
253 esdmisc_val = readl(&mmdc0->mdmisc);
254
255 setbits_le32(&mmdc0->mdmisc,
256 (1 << 6) | (1 << 7) | (1 << 8) | (1 << 16) | (1 << 17));
257
258
259 temp_ref = readl(&mmdc0->mdref);
260 writel(0x0000c000, &mmdc0->mdref);
261
262
263
264
265
266 if (cs0_enable_initial)
267 writel(0x00008020, &mmdc0->mdscr);
268 if (cs1_enable_initial)
269 writel(0x00008028, &mmdc0->mdscr);
270
271
272 wait_for_bit("MMDC", &mmdc0->mdscr, 1 << 14, 1, 100, 0);
273
274
275
276
277
278
279
280
281
282
283 if ((readl(&mmdc0->mdmisc) & 0x00100000) == 0)
284 clrbits_le32(&mmdc0->mdctl, 1 << 30);
285 else
286 clrbits_le32(&mmdc0->mdctl, 1 << 31);
287
288
289
290
291
292 cs0_enable = readl(&mmdc0->mdctl) & 0x80000000;
293 cs1_enable = readl(&mmdc0->mdctl) & 0x40000000;
294
295
296 bus_size = (readl(&mmdc0->mdctl) & 0x30000) >> 16;
297 debug("Data bus size: %d (%d bits)\n", bus_size, 1 << (bus_size + 4));
298
299 precharge_all(cs0_enable, cs1_enable);
300
301
302 writel(pddword, &mmdc0->mppdcmpr1);
303
304
305
306
307
308
309 setbits_le32(&mmdc0->mpswdar0, 1);
310 wait_for_bit("MMDC", &mmdc0->mpswdar0, 1 << 0, 0, 100, 0);
311
312
313
314
315
316 writel(initdelay, &mmdc0->mprddlctl);
317 if (bus_size == 0x2)
318 writel(initdelay, &mmdc1->mprddlctl);
319
320
321 force_delay_measurement(bus_size);
322
323
324
325
326
327
328 debug("Starting Read DQS Gating calibration.\n");
329
330
331
332
333
334 reset_read_data_fifos();
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349 setbits_le32(&mmdc0->mpdgctrl0, 1 << 30);
350
351
352 setbits_le32(&mmdc0->mpdgctrl0, 5 << 28);
353
354
355 wait_for_bit("MMDC", &mmdc0->mpdgctrl0, 1 << 28, 0, 100, 0);
356
357
358
359
360
361
362 if (readl(&mmdc0->mpdgctrl0) & 0x00001000)
363 errors |= 1;
364
365 if ((bus_size == 0x2) && (readl(&mmdc1->mpdgctrl0) & 0x00001000))
366 errors |= 2;
367
368
369
370
371
372
373 modify_dg_result(&mmdc0->mpdghwst0, &mmdc0->mpdghwst1,
374 &mmdc0->mpdgctrl0);
375 modify_dg_result(&mmdc0->mpdghwst2, &mmdc0->mpdghwst3,
376 &mmdc0->mpdgctrl1);
377 if (bus_size == 0x2) {
378 modify_dg_result(&mmdc1->mpdghwst0, &mmdc1->mpdghwst1,
379 &mmdc1->mpdgctrl0);
380 modify_dg_result(&mmdc1->mpdghwst2, &mmdc1->mpdghwst3,
381 &mmdc1->mpdgctrl1);
382 }
383 debug("Ending Read DQS Gating calibration. Error mask: 0x%x\n", errors);
384
385
386
387
388
389
390 debug("Starting Read Delay calibration.\n");
391
392 reset_read_data_fifos();
393
394
395
396
397
398
399 precharge_all(cs0_enable, cs1_enable);
400
401
402
403
404
405
406 writel(0x00000030, &mmdc0->mprddlhwctl);
407
408
409
410
411
412
413
414 wait_for_bit("MMDC", &mmdc0->mprddlhwctl, 1 << 4, 0, 100, 0);
415
416
417 if (readl(&mmdc0->mprddlhwctl) & 0x0000000f)
418 errors |= 4;
419
420 if ((bus_size == 0x2) && (readl(&mmdc1->mprddlhwctl) & 0x0000000f))
421 errors |= 8;
422
423 debug("Ending Read Delay calibration. Error mask: 0x%x\n", errors);
424
425
426
427
428
429
430 debug("Starting Write Delay calibration.\n");
431
432 reset_read_data_fifos();
433
434
435
436
437
438
439 precharge_all(cs0_enable, cs1_enable);
440
441
442
443
444
445 writel(initdelay, &mmdc0->mpwrdlctl);
446 if (bus_size == 0x2)
447 writel(initdelay, &mmdc1->mpwrdlctl);
448
449
450
451
452
453 force_delay_measurement(bus_size);
454
455
456
457
458
459 writel(0x00000030, &mmdc0->mpwrdlhwctl);
460
461
462
463
464
465
466
467 wait_for_bit("MMDC", &mmdc0->mpwrdlhwctl, 1 << 4, 0, 100, 0);
468
469
470 if (readl(&mmdc0->mpwrdlhwctl) & 0x0000000f)
471 errors |= 16;
472
473 if ((bus_size == 0x2) && (readl(&mmdc1->mpwrdlhwctl) & 0x0000000f))
474 errors |= 32;
475
476 debug("Ending Write Delay calibration. Error mask: 0x%x\n", errors);
477
478 reset_read_data_fifos();
479
480
481 setbits_le32(&mmdc0->mdpdc, 0x00005500);
482
483
484 clrbits_le32(&mmdc0->mapsr, 0x1);
485
486
487 writel(esdmisc_val, &mmdc0->mdmisc);
488
489
490 clrbits_le32(&mx6_ddr_iomux->dram_sdqs0, 0x7000);
491 clrbits_le32(&mx6_ddr_iomux->dram_sdqs1, 0x7000);
492 clrbits_le32(&mx6_ddr_iomux->dram_sdqs2, 0x7000);
493 clrbits_le32(&mx6_ddr_iomux->dram_sdqs3, 0x7000);
494 clrbits_le32(&mx6_ddr_iomux->dram_sdqs4, 0x7000);
495 clrbits_le32(&mx6_ddr_iomux->dram_sdqs5, 0x7000);
496 clrbits_le32(&mx6_ddr_iomux->dram_sdqs6, 0x7000);
497 clrbits_le32(&mx6_ddr_iomux->dram_sdqs7, 0x7000);
498
499
500 if (cs1_enable_initial)
501
502 setbits_le32(&mmdc0->mdctl, 1 << 30);
503
504 if (cs0_enable_initial)
505
506 setbits_le32(&mmdc0->mdctl, 1 << 31);
507
508
509 writel(temp_ref, &mmdc0->mdref);
510
511
512 writel(0x0, &mmdc0->mdscr);
513
514
515 wait_for_bit("MMDC", &mmdc0->mdscr, 1 << 14, 0, 100, 0);
516
517
518
519
520
521 debug("MMDC registers updated from calibration\n");
522 debug("Read DQS gating calibration:\n");
523 debug("\tMPDGCTRL0 PHY0 = 0x%08X\n", readl(&mmdc0->mpdgctrl0));
524 debug("\tMPDGCTRL1 PHY0 = 0x%08X\n", readl(&mmdc0->mpdgctrl1));
525 debug("\tMPDGCTRL0 PHY1 = 0x%08X\n", readl(&mmdc1->mpdgctrl0));
526 debug("\tMPDGCTRL1 PHY1 = 0x%08X\n", readl(&mmdc1->mpdgctrl1));
527 debug("Read calibration:\n");
528 debug("\tMPRDDLCTL PHY0 = 0x%08X\n", readl(&mmdc0->mprddlctl));
529 debug("\tMPRDDLCTL PHY1 = 0x%08X\n", readl(&mmdc1->mprddlctl));
530 debug("Write calibration:\n");
531 debug("\tMPWRDLCTL PHY0 = 0x%08X\n", readl(&mmdc0->mpwrdlctl));
532 debug("\tMPWRDLCTL PHY1 = 0x%08X\n", readl(&mmdc1->mpwrdlctl));
533
534
535
536
537
538
539 debug("Status registers bounds for read DQS gating:\n");
540 debug("\tMPDGHWST0 PHY0 = 0x%08x\n", readl(&mmdc0->mpdghwst0));
541 debug("\tMPDGHWST1 PHY0 = 0x%08x\n", readl(&mmdc0->mpdghwst1));
542 debug("\tMPDGHWST2 PHY0 = 0x%08x\n", readl(&mmdc0->mpdghwst2));
543 debug("\tMPDGHWST3 PHY0 = 0x%08x\n", readl(&mmdc0->mpdghwst3));
544 debug("\tMPDGHWST0 PHY1 = 0x%08x\n", readl(&mmdc1->mpdghwst0));
545 debug("\tMPDGHWST1 PHY1 = 0x%08x\n", readl(&mmdc1->mpdghwst1));
546 debug("\tMPDGHWST2 PHY1 = 0x%08x\n", readl(&mmdc1->mpdghwst2));
547 debug("\tMPDGHWST3 PHY1 = 0x%08x\n", readl(&mmdc1->mpdghwst3));
548
549 debug("Final do_dqs_calibration error mask: 0x%x\n", errors);
550
551 return errors;
552}
553#endif
554
555#if defined(CONFIG_MX6SX)
556
557void mx6sx_dram_iocfg(unsigned width,
558 const struct mx6sx_iomux_ddr_regs *ddr,
559 const struct mx6sx_iomux_grp_regs *grp)
560{
561 struct mx6sx_iomux_ddr_regs *mx6_ddr_iomux;
562 struct mx6sx_iomux_grp_regs *mx6_grp_iomux;
563
564 mx6_ddr_iomux = (struct mx6sx_iomux_ddr_regs *)MX6SX_IOM_DDR_BASE;
565 mx6_grp_iomux = (struct mx6sx_iomux_grp_regs *)MX6SX_IOM_GRP_BASE;
566
567
568 writel(grp->grp_ddr_type, &mx6_grp_iomux->grp_ddr_type);
569 writel(grp->grp_ddrpke, &mx6_grp_iomux->grp_ddrpke);
570
571
572 writel(ddr->dram_sdclk_0, &mx6_ddr_iomux->dram_sdclk_0);
573
574
575 writel(ddr->dram_cas, &mx6_ddr_iomux->dram_cas);
576 writel(ddr->dram_ras, &mx6_ddr_iomux->dram_ras);
577 writel(grp->grp_addds, &mx6_grp_iomux->grp_addds);
578
579
580 writel(ddr->dram_reset, &mx6_ddr_iomux->dram_reset);
581 writel(ddr->dram_sdba2, &mx6_ddr_iomux->dram_sdba2);
582 writel(ddr->dram_sdcke0, &mx6_ddr_iomux->dram_sdcke0);
583 writel(ddr->dram_sdcke1, &mx6_ddr_iomux->dram_sdcke1);
584 writel(ddr->dram_odt0, &mx6_ddr_iomux->dram_odt0);
585 writel(ddr->dram_odt1, &mx6_ddr_iomux->dram_odt1);
586 writel(grp->grp_ctlds, &mx6_grp_iomux->grp_ctlds);
587
588
589 writel(grp->grp_ddrmode_ctl, &mx6_grp_iomux->grp_ddrmode_ctl);
590 writel(ddr->dram_sdqs0, &mx6_ddr_iomux->dram_sdqs0);
591 writel(ddr->dram_sdqs1, &mx6_ddr_iomux->dram_sdqs1);
592 if (width >= 32) {
593 writel(ddr->dram_sdqs2, &mx6_ddr_iomux->dram_sdqs2);
594 writel(ddr->dram_sdqs3, &mx6_ddr_iomux->dram_sdqs3);
595 }
596
597
598 writel(grp->grp_ddrmode, &mx6_grp_iomux->grp_ddrmode);
599 writel(grp->grp_b0ds, &mx6_grp_iomux->grp_b0ds);
600 writel(grp->grp_b1ds, &mx6_grp_iomux->grp_b1ds);
601 if (width >= 32) {
602 writel(grp->grp_b2ds, &mx6_grp_iomux->grp_b2ds);
603 writel(grp->grp_b3ds, &mx6_grp_iomux->grp_b3ds);
604 }
605 writel(ddr->dram_dqm0, &mx6_ddr_iomux->dram_dqm0);
606 writel(ddr->dram_dqm1, &mx6_ddr_iomux->dram_dqm1);
607 if (width >= 32) {
608 writel(ddr->dram_dqm2, &mx6_ddr_iomux->dram_dqm2);
609 writel(ddr->dram_dqm3, &mx6_ddr_iomux->dram_dqm3);
610 }
611}
612#endif
613
614#ifdef CONFIG_MX6UL
615void mx6ul_dram_iocfg(unsigned width,
616 const struct mx6ul_iomux_ddr_regs *ddr,
617 const struct mx6ul_iomux_grp_regs *grp)
618{
619 struct mx6ul_iomux_ddr_regs *mx6_ddr_iomux;
620 struct mx6ul_iomux_grp_regs *mx6_grp_iomux;
621
622 mx6_ddr_iomux = (struct mx6ul_iomux_ddr_regs *)MX6UL_IOM_DDR_BASE;
623 mx6_grp_iomux = (struct mx6ul_iomux_grp_regs *)MX6UL_IOM_GRP_BASE;
624
625
626 writel(grp->grp_ddr_type, &mx6_grp_iomux->grp_ddr_type);
627 writel(grp->grp_ddrpke, &mx6_grp_iomux->grp_ddrpke);
628
629
630 writel(ddr->dram_sdclk_0, &mx6_ddr_iomux->dram_sdclk_0);
631
632
633 writel(ddr->dram_cas, &mx6_ddr_iomux->dram_cas);
634 writel(ddr->dram_ras, &mx6_ddr_iomux->dram_ras);
635 writel(grp->grp_addds, &mx6_grp_iomux->grp_addds);
636
637
638 writel(ddr->dram_reset, &mx6_ddr_iomux->dram_reset);
639 writel(ddr->dram_sdba2, &mx6_ddr_iomux->dram_sdba2);
640 writel(ddr->dram_odt0, &mx6_ddr_iomux->dram_odt0);
641 writel(ddr->dram_odt1, &mx6_ddr_iomux->dram_odt1);
642 writel(grp->grp_ctlds, &mx6_grp_iomux->grp_ctlds);
643
644
645 writel(grp->grp_ddrmode_ctl, &mx6_grp_iomux->grp_ddrmode_ctl);
646 writel(ddr->dram_sdqs0, &mx6_ddr_iomux->dram_sdqs0);
647 writel(ddr->dram_sdqs1, &mx6_ddr_iomux->dram_sdqs1);
648
649
650 writel(grp->grp_ddrmode, &mx6_grp_iomux->grp_ddrmode);
651 writel(grp->grp_b0ds, &mx6_grp_iomux->grp_b0ds);
652 writel(grp->grp_b1ds, &mx6_grp_iomux->grp_b1ds);
653 writel(ddr->dram_dqm0, &mx6_ddr_iomux->dram_dqm0);
654 writel(ddr->dram_dqm1, &mx6_ddr_iomux->dram_dqm1);
655}
656#endif
657
658#if defined(CONFIG_MX6SL)
659void mx6sl_dram_iocfg(unsigned width,
660 const struct mx6sl_iomux_ddr_regs *ddr,
661 const struct mx6sl_iomux_grp_regs *grp)
662{
663 struct mx6sl_iomux_ddr_regs *mx6_ddr_iomux;
664 struct mx6sl_iomux_grp_regs *mx6_grp_iomux;
665
666 mx6_ddr_iomux = (struct mx6sl_iomux_ddr_regs *)MX6SL_IOM_DDR_BASE;
667 mx6_grp_iomux = (struct mx6sl_iomux_grp_regs *)MX6SL_IOM_GRP_BASE;
668
669
670 mx6_grp_iomux->grp_ddr_type = grp->grp_ddr_type;
671 mx6_grp_iomux->grp_ddrpke = grp->grp_ddrpke;
672
673
674 mx6_ddr_iomux->dram_sdclk_0 = ddr->dram_sdclk_0;
675
676
677 mx6_ddr_iomux->dram_cas = ddr->dram_cas;
678 mx6_ddr_iomux->dram_ras = ddr->dram_ras;
679 mx6_grp_iomux->grp_addds = grp->grp_addds;
680
681
682 mx6_ddr_iomux->dram_reset = ddr->dram_reset;
683 mx6_ddr_iomux->dram_sdba2 = ddr->dram_sdba2;
684 mx6_grp_iomux->grp_ctlds = grp->grp_ctlds;
685
686
687 mx6_grp_iomux->grp_ddrmode_ctl = grp->grp_ddrmode_ctl;
688 mx6_ddr_iomux->dram_sdqs0 = ddr->dram_sdqs0;
689 mx6_ddr_iomux->dram_sdqs1 = ddr->dram_sdqs1;
690 if (width >= 32) {
691 mx6_ddr_iomux->dram_sdqs2 = ddr->dram_sdqs2;
692 mx6_ddr_iomux->dram_sdqs3 = ddr->dram_sdqs3;
693 }
694
695
696 mx6_grp_iomux->grp_ddrmode = grp->grp_ddrmode;
697 mx6_grp_iomux->grp_b0ds = grp->grp_b0ds;
698 mx6_grp_iomux->grp_b1ds = grp->grp_b1ds;
699 if (width >= 32) {
700 mx6_grp_iomux->grp_b2ds = grp->grp_b2ds;
701 mx6_grp_iomux->grp_b3ds = grp->grp_b3ds;
702 }
703
704 mx6_ddr_iomux->dram_dqm0 = ddr->dram_dqm0;
705 mx6_ddr_iomux->dram_dqm1 = ddr->dram_dqm1;
706 if (width >= 32) {
707 mx6_ddr_iomux->dram_dqm2 = ddr->dram_dqm2;
708 mx6_ddr_iomux->dram_dqm3 = ddr->dram_dqm3;
709 }
710}
711#endif
712
713#if defined(CONFIG_MX6QDL) || defined(CONFIG_MX6Q) || defined(CONFIG_MX6D)
714
715void mx6dq_dram_iocfg(unsigned width,
716 const struct mx6dq_iomux_ddr_regs *ddr,
717 const struct mx6dq_iomux_grp_regs *grp)
718{
719 volatile struct mx6dq_iomux_ddr_regs *mx6_ddr_iomux;
720 volatile struct mx6dq_iomux_grp_regs *mx6_grp_iomux;
721
722 mx6_ddr_iomux = (struct mx6dq_iomux_ddr_regs *)MX6DQ_IOM_DDR_BASE;
723 mx6_grp_iomux = (struct mx6dq_iomux_grp_regs *)MX6DQ_IOM_GRP_BASE;
724
725
726 mx6_grp_iomux->grp_ddr_type = grp->grp_ddr_type;
727 mx6_grp_iomux->grp_ddrpke = grp->grp_ddrpke;
728
729
730 mx6_ddr_iomux->dram_sdclk_0 = ddr->dram_sdclk_0;
731 mx6_ddr_iomux->dram_sdclk_1 = ddr->dram_sdclk_1;
732
733
734 mx6_ddr_iomux->dram_cas = ddr->dram_cas;
735 mx6_ddr_iomux->dram_ras = ddr->dram_ras;
736 mx6_grp_iomux->grp_addds = grp->grp_addds;
737
738
739 mx6_ddr_iomux->dram_reset = ddr->dram_reset;
740 mx6_ddr_iomux->dram_sdcke0 = ddr->dram_sdcke0;
741 mx6_ddr_iomux->dram_sdcke1 = ddr->dram_sdcke1;
742 mx6_ddr_iomux->dram_sdba2 = ddr->dram_sdba2;
743 mx6_ddr_iomux->dram_sdodt0 = ddr->dram_sdodt0;
744 mx6_ddr_iomux->dram_sdodt1 = ddr->dram_sdodt1;
745 mx6_grp_iomux->grp_ctlds = grp->grp_ctlds;
746
747
748 mx6_grp_iomux->grp_ddrmode_ctl = grp->grp_ddrmode_ctl;
749 mx6_ddr_iomux->dram_sdqs0 = ddr->dram_sdqs0;
750 mx6_ddr_iomux->dram_sdqs1 = ddr->dram_sdqs1;
751 if (width >= 32) {
752 mx6_ddr_iomux->dram_sdqs2 = ddr->dram_sdqs2;
753 mx6_ddr_iomux->dram_sdqs3 = ddr->dram_sdqs3;
754 }
755 if (width >= 64) {
756 mx6_ddr_iomux->dram_sdqs4 = ddr->dram_sdqs4;
757 mx6_ddr_iomux->dram_sdqs5 = ddr->dram_sdqs5;
758 mx6_ddr_iomux->dram_sdqs6 = ddr->dram_sdqs6;
759 mx6_ddr_iomux->dram_sdqs7 = ddr->dram_sdqs7;
760 }
761
762
763 mx6_grp_iomux->grp_ddrmode = grp->grp_ddrmode;
764 mx6_grp_iomux->grp_b0ds = grp->grp_b0ds;
765 mx6_grp_iomux->grp_b1ds = grp->grp_b1ds;
766 if (width >= 32) {
767 mx6_grp_iomux->grp_b2ds = grp->grp_b2ds;
768 mx6_grp_iomux->grp_b3ds = grp->grp_b3ds;
769 }
770 if (width >= 64) {
771 mx6_grp_iomux->grp_b4ds = grp->grp_b4ds;
772 mx6_grp_iomux->grp_b5ds = grp->grp_b5ds;
773 mx6_grp_iomux->grp_b6ds = grp->grp_b6ds;
774 mx6_grp_iomux->grp_b7ds = grp->grp_b7ds;
775 }
776 mx6_ddr_iomux->dram_dqm0 = ddr->dram_dqm0;
777 mx6_ddr_iomux->dram_dqm1 = ddr->dram_dqm1;
778 if (width >= 32) {
779 mx6_ddr_iomux->dram_dqm2 = ddr->dram_dqm2;
780 mx6_ddr_iomux->dram_dqm3 = ddr->dram_dqm3;
781 }
782 if (width >= 64) {
783 mx6_ddr_iomux->dram_dqm4 = ddr->dram_dqm4;
784 mx6_ddr_iomux->dram_dqm5 = ddr->dram_dqm5;
785 mx6_ddr_iomux->dram_dqm6 = ddr->dram_dqm6;
786 mx6_ddr_iomux->dram_dqm7 = ddr->dram_dqm7;
787 }
788}
789#endif
790
791#if defined(CONFIG_MX6QDL) || defined(CONFIG_MX6DL) || defined(CONFIG_MX6S)
792
793void mx6sdl_dram_iocfg(unsigned width,
794 const struct mx6sdl_iomux_ddr_regs *ddr,
795 const struct mx6sdl_iomux_grp_regs *grp)
796{
797 volatile struct mx6sdl_iomux_ddr_regs *mx6_ddr_iomux;
798 volatile struct mx6sdl_iomux_grp_regs *mx6_grp_iomux;
799
800 mx6_ddr_iomux = (struct mx6sdl_iomux_ddr_regs *)MX6SDL_IOM_DDR_BASE;
801 mx6_grp_iomux = (struct mx6sdl_iomux_grp_regs *)MX6SDL_IOM_GRP_BASE;
802
803
804 mx6_grp_iomux->grp_ddr_type = grp->grp_ddr_type;
805 mx6_grp_iomux->grp_ddrpke = grp->grp_ddrpke;
806
807
808 mx6_ddr_iomux->dram_sdclk_0 = ddr->dram_sdclk_0;
809 mx6_ddr_iomux->dram_sdclk_1 = ddr->dram_sdclk_1;
810
811
812 mx6_ddr_iomux->dram_cas = ddr->dram_cas;
813 mx6_ddr_iomux->dram_ras = ddr->dram_ras;
814 mx6_grp_iomux->grp_addds = grp->grp_addds;
815
816
817 mx6_ddr_iomux->dram_reset = ddr->dram_reset;
818 mx6_ddr_iomux->dram_sdcke0 = ddr->dram_sdcke0;
819 mx6_ddr_iomux->dram_sdcke1 = ddr->dram_sdcke1;
820 mx6_ddr_iomux->dram_sdba2 = ddr->dram_sdba2;
821 mx6_ddr_iomux->dram_sdodt0 = ddr->dram_sdodt0;
822 mx6_ddr_iomux->dram_sdodt1 = ddr->dram_sdodt1;
823 mx6_grp_iomux->grp_ctlds = grp->grp_ctlds;
824
825
826 mx6_grp_iomux->grp_ddrmode_ctl = grp->grp_ddrmode_ctl;
827 mx6_ddr_iomux->dram_sdqs0 = ddr->dram_sdqs0;
828 mx6_ddr_iomux->dram_sdqs1 = ddr->dram_sdqs1;
829 if (width >= 32) {
830 mx6_ddr_iomux->dram_sdqs2 = ddr->dram_sdqs2;
831 mx6_ddr_iomux->dram_sdqs3 = ddr->dram_sdqs3;
832 }
833 if (width >= 64) {
834 mx6_ddr_iomux->dram_sdqs4 = ddr->dram_sdqs4;
835 mx6_ddr_iomux->dram_sdqs5 = ddr->dram_sdqs5;
836 mx6_ddr_iomux->dram_sdqs6 = ddr->dram_sdqs6;
837 mx6_ddr_iomux->dram_sdqs7 = ddr->dram_sdqs7;
838 }
839
840
841 mx6_grp_iomux->grp_ddrmode = grp->grp_ddrmode;
842 mx6_grp_iomux->grp_b0ds = grp->grp_b0ds;
843 mx6_grp_iomux->grp_b1ds = grp->grp_b1ds;
844 if (width >= 32) {
845 mx6_grp_iomux->grp_b2ds = grp->grp_b2ds;
846 mx6_grp_iomux->grp_b3ds = grp->grp_b3ds;
847 }
848 if (width >= 64) {
849 mx6_grp_iomux->grp_b4ds = grp->grp_b4ds;
850 mx6_grp_iomux->grp_b5ds = grp->grp_b5ds;
851 mx6_grp_iomux->grp_b6ds = grp->grp_b6ds;
852 mx6_grp_iomux->grp_b7ds = grp->grp_b7ds;
853 }
854 mx6_ddr_iomux->dram_dqm0 = ddr->dram_dqm0;
855 mx6_ddr_iomux->dram_dqm1 = ddr->dram_dqm1;
856 if (width >= 32) {
857 mx6_ddr_iomux->dram_dqm2 = ddr->dram_dqm2;
858 mx6_ddr_iomux->dram_dqm3 = ddr->dram_dqm3;
859 }
860 if (width >= 64) {
861 mx6_ddr_iomux->dram_dqm4 = ddr->dram_dqm4;
862 mx6_ddr_iomux->dram_dqm5 = ddr->dram_dqm5;
863 mx6_ddr_iomux->dram_dqm6 = ddr->dram_dqm6;
864 mx6_ddr_iomux->dram_dqm7 = ddr->dram_dqm7;
865 }
866}
867#endif
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888#define MR(val, ba, cmd, cs1) \
889 ((val << 16) | (1 << 15) | (cmd << 4) | (cs1 << 3) | ba)
890#define MMDC1(entry, value) do { \
891 if (!is_mx6sx() && !is_mx6ul() && !is_mx6sl()) \
892 mmdc1->entry = value; \
893 } while (0)
894
895
896
897
898
899static int lpddr2_wl(uint32_t mem_speed)
900{
901 switch (mem_speed) {
902 case 1066:
903 case 933:
904 return 4;
905 case 800:
906 return 3;
907 case 677:
908 case 533:
909 return 2;
910 case 400:
911 case 333:
912 return 1;
913 default:
914 puts("invalid memory speed\n");
915 hang();
916 }
917
918 return 0;
919}
920
921
922
923
924
925static int lpddr2_rl(uint32_t mem_speed)
926{
927 switch (mem_speed) {
928 case 1066:
929 return 8;
930 case 933:
931 return 7;
932 case 800:
933 return 6;
934 case 677:
935 return 5;
936 case 533:
937 return 4;
938 case 400:
939 case 333:
940 return 3;
941 default:
942 puts("invalid memory speed\n");
943 hang();
944 }
945
946 return 0;
947}
948
949void mx6_lpddr2_cfg(const struct mx6_ddr_sysinfo *sysinfo,
950 const struct mx6_mmdc_calibration *calib,
951 const struct mx6_lpddr2_cfg *lpddr2_cfg)
952{
953 volatile struct mmdc_p_regs *mmdc0;
954 u32 val;
955 u8 tcke, tcksrx, tcksre, trrd;
956 u8 twl, txp, tfaw, tcl;
957 u16 tras, twr, tmrd, trtp, twtr, trfc, txsr;
958 u16 trcd_lp, trppb_lp, trpab_lp, trc_lp;
959 u16 cs0_end;
960 u8 coladdr;
961 int clkper;
962 int clock;
963 int cs;
964
965
966 if (sysinfo->dsize > 1)
967 hang();
968
969 mmdc0 = (struct mmdc_p_regs *)MMDC_P0_BASE_ADDR;
970
971 clock = mxc_get_clock(MXC_DDR_CLK) / 1000000U;
972 clkper = (1000 * 1000) / clock;
973
974 twl = lpddr2_wl(lpddr2_cfg->mem_speed) - 1;
975
976
977 switch (lpddr2_cfg->density) {
978 case 1:
979 case 2:
980 case 4:
981 trfc = DIV_ROUND_UP(130000, clkper) - 1;
982 txsr = DIV_ROUND_UP(140000, clkper) - 1;
983 break;
984 case 8:
985 trfc = DIV_ROUND_UP(210000, clkper) - 1;
986 txsr = DIV_ROUND_UP(220000, clkper) - 1;
987 break;
988 default:
989
990
991
992 hang();
993 break;
994 }
995
996
997
998 txp = DIV_ROUND_UP(7500, clkper) - 1;
999 tcke = 3;
1000 if (lpddr2_cfg->mem_speed == 333)
1001 tfaw = DIV_ROUND_UP(60000, clkper) - 1;
1002 else
1003 tfaw = DIV_ROUND_UP(50000, clkper) - 1;
1004 trrd = DIV_ROUND_UP(10000, clkper) - 1;
1005
1006
1007 tcksre = DIV_ROUND_UP(15000, clkper);
1008 tcksrx = tcksre;
1009 twr = DIV_ROUND_UP(15000, clkper) - 1;
1010
1011
1012
1013
1014 tmrd = 5;
1015 tras = DIV_ROUND_UP(lpddr2_cfg->trasmin, clkper / 10) - 1;
1016
1017 trcd_lp = DIV_ROUND_UP(lpddr2_cfg->trcd_lp, clkper / 10) - 1;
1018 trc_lp = DIV_ROUND_UP(lpddr2_cfg->trasmin + lpddr2_cfg->trppb_lp,
1019 clkper / 10) - 1;
1020 trppb_lp = DIV_ROUND_UP(lpddr2_cfg->trppb_lp, clkper / 10) - 1;
1021 trpab_lp = DIV_ROUND_UP(lpddr2_cfg->trpab_lp, clkper / 10) - 1;
1022
1023 tcl = lpddr2_rl(lpddr2_cfg->mem_speed) - 3;
1024 twtr = DIV_ROUND_UP(7500, clkper) - 1;
1025 trtp = DIV_ROUND_UP(7500, clkper) - 1;
1026
1027 cs0_end = 4 * sysinfo->cs_density - 1;
1028
1029 debug("density:%d Gb (%d Gb per chip)\n",
1030 sysinfo->cs_density, lpddr2_cfg->density);
1031 debug("clock: %dMHz (%d ps)\n", clock, clkper);
1032 debug("memspd:%d\n", lpddr2_cfg->mem_speed);
1033 debug("trcd_lp=%d\n", trcd_lp);
1034 debug("trppb_lp=%d\n", trppb_lp);
1035 debug("trpab_lp=%d\n", trpab_lp);
1036 debug("trc_lp=%d\n", trc_lp);
1037 debug("tcke=%d\n", tcke);
1038 debug("tcksrx=%d\n", tcksrx);
1039 debug("tcksre=%d\n", tcksre);
1040 debug("trfc=%d\n", trfc);
1041 debug("txsr=%d\n", txsr);
1042 debug("txp=%d\n", txp);
1043 debug("tfaw=%d\n", tfaw);
1044 debug("tcl=%d\n", tcl);
1045 debug("tras=%d\n", tras);
1046 debug("twr=%d\n", twr);
1047 debug("tmrd=%d\n", tmrd);
1048 debug("twl=%d\n", twl);
1049 debug("trtp=%d\n", trtp);
1050 debug("twtr=%d\n", twtr);
1051 debug("trrd=%d\n", trrd);
1052 debug("cs0_end=%d\n", cs0_end);
1053 debug("ncs=%d\n", sysinfo->ncs);
1054
1055
1056
1057
1058
1059 mmdc0->mpwldectrl0 = calib->p0_mpwldectrl0;
1060 mmdc0->mpwldectrl1 = calib->p0_mpwldectrl1;
1061 mmdc0->mpdgctrl0 = calib->p0_mpdgctrl0;
1062 mmdc0->mpdgctrl1 = calib->p0_mpdgctrl1;
1063 mmdc0->mprddlctl = calib->p0_mprddlctl;
1064 mmdc0->mpwrdlctl = calib->p0_mpwrdlctl;
1065 mmdc0->mpzqlp2ctl = calib->mpzqlp2ctl;
1066
1067
1068 mmdc0->mprddqby0dl = 0x33333333;
1069 mmdc0->mprddqby1dl = 0x33333333;
1070 if (sysinfo->dsize > 0) {
1071 mmdc0->mprddqby2dl = 0x33333333;
1072 mmdc0->mprddqby3dl = 0x33333333;
1073 }
1074
1075
1076 mmdc0->mpwrdqby0dl = 0xf3333333;
1077 mmdc0->mpwrdqby1dl = 0xf3333333;
1078 if (sysinfo->dsize > 0) {
1079 mmdc0->mpwrdqby2dl = 0xf3333333;
1080 mmdc0->mpwrdqby3dl = 0xf3333333;
1081 }
1082
1083
1084
1085
1086
1087 mmdc0->mpodtctrl = 0;
1088
1089
1090 val = (1 << 11);
1091 mmdc0->mpmur0 = val;
1092
1093
1094 mmdc0->mdscr = (u32)(1 << 15);
1095
1096
1097 mmdc0->mdcfg0 = (trfc << 24) | (txsr << 16) | (txp << 13) |
1098 (tfaw << 4) | tcl;
1099 mmdc0->mdcfg1 = (tras << 16) | (twr << 9) | (tmrd << 5) | twl;
1100 mmdc0->mdcfg2 = (trtp << 6) | (twtr << 3) | trrd;
1101 mmdc0->mdcfg3lp = (trc_lp << 16) | (trcd_lp << 8) |
1102 (trppb_lp << 4) | trpab_lp;
1103 mmdc0->mdotc = 0;
1104
1105 mmdc0->mdasp = cs0_end;
1106
1107
1108 mmdc0->mdmisc = (sysinfo->cs1_mirror << 19) | (sysinfo->walat << 16) |
1109 (sysinfo->bi_on << 12) | (sysinfo->mif3_mode << 9) |
1110 (sysinfo->ralat << 6) | (1 << 3);
1111
1112
1113 mmdc0->mdor = (sysinfo->sde_to_rst << 8) |
1114 (sysinfo->rst_to_cke << 0);
1115
1116
1117 coladdr = lpddr2_cfg->coladdr;
1118 if (lpddr2_cfg->coladdr == 8)
1119 coladdr += 4;
1120 else if (lpddr2_cfg->coladdr == 12)
1121 coladdr += 1;
1122 mmdc0->mdctl = (lpddr2_cfg->rowaddr - 11) << 24 |
1123 (coladdr - 9) << 20 |
1124 (0 << 19) |
1125 (sysinfo->dsize << 16);
1126
1127
1128 val = 0xa1390003;
1129 mmdc0->mpzqhwctrl = val;
1130
1131
1132 mmdc0->mdctl |= (1 << 31) |
1133 ((sysinfo->ncs == 2) ? 1 : 0) << 30;
1134
1135
1136 for (cs = 0; cs < sysinfo->ncs; cs++) {
1137
1138 mmdc0->mdscr = MR(63, 0, 3, cs);
1139
1140
1141
1142 val = 0xA | (0xff << 8);
1143 mmdc0->mdscr = MR(val, 0, 3, cs);
1144
1145 val = 0x1 | (0x82 << 8);
1146 mmdc0->mdscr = MR(val, 0, 3, cs);
1147
1148 val = 0x2 | (0x04 << 8);
1149 mmdc0->mdscr = MR(val, 0, 3, cs);
1150
1151 val = 0x3 | (0x02 << 8);
1152 mmdc0->mdscr = MR(val, 0, 3, cs);
1153 }
1154
1155
1156 mmdc0->mdpdc = (tcke & 0x7) << 16 |
1157 5 << 12 |
1158 5 << 8 |
1159 1 << 6 |
1160 (tcksrx & 0x7) << 3 |
1161 (tcksre & 0x7);
1162 mmdc0->mapsr = 0x00001006;
1163
1164
1165 val = 0xa1310003;
1166 mmdc0->mpzqhwctrl = val;
1167
1168
1169 mmdc0->mdref = (0 << 14) |
1170 (3 << 11);
1171
1172
1173 mmdc0->mdscr = 0x00000000;
1174
1175
1176 mdelay(1);
1177}
1178
1179void mx6_ddr3_cfg(const struct mx6_ddr_sysinfo *sysinfo,
1180 const struct mx6_mmdc_calibration *calib,
1181 const struct mx6_ddr3_cfg *ddr3_cfg)
1182{
1183 volatile struct mmdc_p_regs *mmdc0;
1184 volatile struct mmdc_p_regs *mmdc1;
1185 u32 val;
1186 u8 tcke, tcksrx, tcksre, txpdll, taofpd, taonpd, trrd;
1187 u8 todtlon, taxpd, tanpd, tcwl, txp, tfaw, tcl;
1188 u8 todt_idle_off = 0x4;
1189 u16 trcd, trc, tras, twr, tmrd, trtp, trp, twtr, trfc, txs, txpr;
1190 u16 cs0_end;
1191 u16 tdllk = 0x1ff;
1192 u8 coladdr;
1193 int clkper;
1194 int clock;
1195 int cs;
1196 u16 mem_speed = ddr3_cfg->mem_speed;
1197
1198 mmdc0 = (struct mmdc_p_regs *)MMDC_P0_BASE_ADDR;
1199 if (!is_mx6sx() && !is_mx6ul() && !is_mx6sl())
1200 mmdc1 = (struct mmdc_p_regs *)MMDC_P1_BASE_ADDR;
1201
1202
1203 if (is_mx6dq() || is_mx6dqp()) {
1204 if (mem_speed > 1066)
1205 mem_speed = 1066;
1206
1207 tcwl = 4;
1208 }
1209
1210 else {
1211 if (mem_speed > 800)
1212 mem_speed = 800;
1213
1214 tcwl = 3;
1215 }
1216
1217 clock = mem_speed / 2;
1218
1219
1220
1221
1222 if (is_mx6dq() || is_mx6dqp()) {
1223 if (clock > 528)
1224 clock = 528;
1225 }
1226
1227 clkper = (1000 * 1000) / clock;
1228 todtlon = tcwl;
1229 taxpd = tcwl;
1230 tanpd = tcwl;
1231
1232 switch (ddr3_cfg->density) {
1233 case 1:
1234 trfc = DIV_ROUND_UP(110000, clkper) - 1;
1235 txs = DIV_ROUND_UP(120000, clkper) - 1;
1236 break;
1237 case 2:
1238 trfc = DIV_ROUND_UP(160000, clkper) - 1;
1239 txs = DIV_ROUND_UP(170000, clkper) - 1;
1240 break;
1241 case 4:
1242 trfc = DIV_ROUND_UP(260000, clkper) - 1;
1243 txs = DIV_ROUND_UP(270000, clkper) - 1;
1244 break;
1245 case 8:
1246 trfc = DIV_ROUND_UP(350000, clkper) - 1;
1247 txs = DIV_ROUND_UP(360000, clkper) - 1;
1248 break;
1249 default:
1250
1251 puts("invalid chip density\n");
1252 hang();
1253 break;
1254 }
1255 txpr = txs;
1256
1257 switch (mem_speed) {
1258 case 800:
1259 txp = DIV_ROUND_UP(max(3 * clkper, 7500), clkper) - 1;
1260 tcke = DIV_ROUND_UP(max(3 * clkper, 7500), clkper) - 1;
1261 if (ddr3_cfg->pagesz == 1) {
1262 tfaw = DIV_ROUND_UP(40000, clkper) - 1;
1263 trrd = DIV_ROUND_UP(max(4 * clkper, 10000), clkper) - 1;
1264 } else {
1265 tfaw = DIV_ROUND_UP(50000, clkper) - 1;
1266 trrd = DIV_ROUND_UP(max(4 * clkper, 10000), clkper) - 1;
1267 }
1268 break;
1269 case 1066:
1270 txp = DIV_ROUND_UP(max(3 * clkper, 7500), clkper) - 1;
1271 tcke = DIV_ROUND_UP(max(3 * clkper, 5625), clkper) - 1;
1272 if (ddr3_cfg->pagesz == 1) {
1273 tfaw = DIV_ROUND_UP(37500, clkper) - 1;
1274 trrd = DIV_ROUND_UP(max(4 * clkper, 7500), clkper) - 1;
1275 } else {
1276 tfaw = DIV_ROUND_UP(50000, clkper) - 1;
1277 trrd = DIV_ROUND_UP(max(4 * clkper, 10000), clkper) - 1;
1278 }
1279 break;
1280 default:
1281 puts("invalid memory speed\n");
1282 hang();
1283 break;
1284 }
1285 txpdll = DIV_ROUND_UP(max(10 * clkper, 24000), clkper) - 1;
1286 tcksre = DIV_ROUND_UP(max(5 * clkper, 10000), clkper);
1287 taonpd = DIV_ROUND_UP(2000, clkper) - 1;
1288 tcksrx = tcksre;
1289 taofpd = taonpd;
1290 twr = DIV_ROUND_UP(15000, clkper) - 1;
1291 tmrd = DIV_ROUND_UP(max(12 * clkper, 15000), clkper) - 1;
1292 trc = DIV_ROUND_UP(ddr3_cfg->trcmin, clkper / 10) - 1;
1293 tras = DIV_ROUND_UP(ddr3_cfg->trasmin, clkper / 10) - 1;
1294 tcl = DIV_ROUND_UP(ddr3_cfg->trcd, clkper / 10) - 3;
1295 trp = DIV_ROUND_UP(ddr3_cfg->trcd, clkper / 10) - 1;
1296 twtr = ROUND(max(4 * clkper, 7500) / clkper, 1) - 1;
1297 trcd = trp;
1298 trtp = twtr;
1299 cs0_end = 4 * sysinfo->cs_density - 1;
1300
1301 debug("density:%d Gb (%d Gb per chip)\n",
1302 sysinfo->cs_density, ddr3_cfg->density);
1303 debug("clock: %dMHz (%d ps)\n", clock, clkper);
1304 debug("memspd:%d\n", mem_speed);
1305 debug("tcke=%d\n", tcke);
1306 debug("tcksrx=%d\n", tcksrx);
1307 debug("tcksre=%d\n", tcksre);
1308 debug("taofpd=%d\n", taofpd);
1309 debug("taonpd=%d\n", taonpd);
1310 debug("todtlon=%d\n", todtlon);
1311 debug("tanpd=%d\n", tanpd);
1312 debug("taxpd=%d\n", taxpd);
1313 debug("trfc=%d\n", trfc);
1314 debug("txs=%d\n", txs);
1315 debug("txp=%d\n", txp);
1316 debug("txpdll=%d\n", txpdll);
1317 debug("tfaw=%d\n", tfaw);
1318 debug("tcl=%d\n", tcl);
1319 debug("trcd=%d\n", trcd);
1320 debug("trp=%d\n", trp);
1321 debug("trc=%d\n", trc);
1322 debug("tras=%d\n", tras);
1323 debug("twr=%d\n", twr);
1324 debug("tmrd=%d\n", tmrd);
1325 debug("tcwl=%d\n", tcwl);
1326 debug("tdllk=%d\n", tdllk);
1327 debug("trtp=%d\n", trtp);
1328 debug("twtr=%d\n", twtr);
1329 debug("trrd=%d\n", trrd);
1330 debug("txpr=%d\n", txpr);
1331 debug("cs0_end=%d\n", cs0_end);
1332 debug("ncs=%d\n", sysinfo->ncs);
1333 debug("Rtt_wr=%d\n", sysinfo->rtt_wr);
1334 debug("Rtt_nom=%d\n", sysinfo->rtt_nom);
1335 debug("SRT=%d\n", ddr3_cfg->SRT);
1336 debug("twr=%d\n", twr);
1337
1338
1339
1340
1341
1342
1343
1344 mmdc0->mpwldectrl0 = calib->p0_mpwldectrl0;
1345 mmdc0->mpwldectrl1 = calib->p0_mpwldectrl1;
1346 mmdc0->mpdgctrl0 = calib->p0_mpdgctrl0;
1347 mmdc0->mpdgctrl1 = calib->p0_mpdgctrl1;
1348 mmdc0->mprddlctl = calib->p0_mprddlctl;
1349 mmdc0->mpwrdlctl = calib->p0_mpwrdlctl;
1350 if (sysinfo->dsize > 1) {
1351 MMDC1(mpwldectrl0, calib->p1_mpwldectrl0);
1352 MMDC1(mpwldectrl1, calib->p1_mpwldectrl1);
1353 MMDC1(mpdgctrl0, calib->p1_mpdgctrl0);
1354 MMDC1(mpdgctrl1, calib->p1_mpdgctrl1);
1355 MMDC1(mprddlctl, calib->p1_mprddlctl);
1356 MMDC1(mpwrdlctl, calib->p1_mpwrdlctl);
1357 }
1358
1359
1360 mmdc0->mprddqby0dl = 0x33333333;
1361 mmdc0->mprddqby1dl = 0x33333333;
1362 if (sysinfo->dsize > 0) {
1363 mmdc0->mprddqby2dl = 0x33333333;
1364 mmdc0->mprddqby3dl = 0x33333333;
1365 }
1366
1367 if (sysinfo->dsize > 1) {
1368 MMDC1(mprddqby0dl, 0x33333333);
1369 MMDC1(mprddqby1dl, 0x33333333);
1370 MMDC1(mprddqby2dl, 0x33333333);
1371 MMDC1(mprddqby3dl, 0x33333333);
1372 }
1373
1374
1375 val = (sysinfo->rtt_nom == 2) ? 0x00011117 : 0x00022227;
1376 mmdc0->mpodtctrl = val;
1377 if (sysinfo->dsize > 1)
1378 MMDC1(mpodtctrl, val);
1379
1380
1381 val = (1 << 11);
1382 mmdc0->mpmur0 = val;
1383 if (sysinfo->dsize > 1)
1384 MMDC1(mpmur0, val);
1385
1386
1387 mmdc0->mdscr = (u32)(1 << 15);
1388
1389
1390 mmdc0->mdcfg0 = (trfc << 24) | (txs << 16) | (txp << 13) |
1391 (txpdll << 9) | (tfaw << 4) | tcl;
1392 mmdc0->mdcfg1 = (trcd << 29) | (trp << 26) | (trc << 21) |
1393 (tras << 16) | (1 << 15) |
1394 (twr << 9) | (tmrd << 5) | tcwl;
1395 mmdc0->mdcfg2 = (tdllk << 16) | (trtp << 6) | (twtr << 3) | trrd;
1396 mmdc0->mdotc = (taofpd << 27) | (taonpd << 24) | (tanpd << 20) |
1397 (taxpd << 16) | (todtlon << 12) | (todt_idle_off << 4);
1398 mmdc0->mdasp = cs0_end;
1399
1400
1401 mmdc0->mdmisc = (sysinfo->cs1_mirror << 19) | (sysinfo->walat << 16) |
1402 (sysinfo->bi_on << 12) | (sysinfo->mif3_mode << 9) |
1403 (sysinfo->ralat << 6);
1404
1405
1406 mmdc0->mdor = (txpr << 16) | (sysinfo->sde_to_rst << 8) |
1407 (sysinfo->rst_to_cke << 0);
1408
1409
1410 coladdr = ddr3_cfg->coladdr;
1411 if (ddr3_cfg->coladdr == 8)
1412 coladdr += 4;
1413 else if (ddr3_cfg->coladdr == 12)
1414 coladdr += 1;
1415 mmdc0->mdctl = (ddr3_cfg->rowaddr - 11) << 24 |
1416 (coladdr - 9) << 20 |
1417 (1 << 19) |
1418 (sysinfo->dsize << 16);
1419
1420
1421 val = 0xa1390001;
1422 mmdc0->mpzqhwctrl = val;
1423 if (sysinfo->dsize > 1)
1424 MMDC1(mpzqhwctrl, val);
1425
1426
1427 mmdc0->mdctl |= (1 << 31) |
1428 ((sysinfo->ncs == 2) ? 1 : 0) << 30;
1429
1430
1431 for (cs = 0; cs < sysinfo->ncs; cs++) {
1432
1433 val = (sysinfo->rtt_wr & 3) << 9 | (ddr3_cfg->SRT & 1) << 7 |
1434 ((tcwl - 3) & 3) << 3;
1435 debug("MR2 CS%d: 0x%08x\n", cs, (u32)MR(val, 2, 3, cs));
1436 mmdc0->mdscr = MR(val, 2, 3, cs);
1437
1438 debug("MR3 CS%d: 0x%08x\n", cs, (u32)MR(0, 3, 3, cs));
1439 mmdc0->mdscr = MR(0, 3, 3, cs);
1440
1441 val = ((sysinfo->rtt_nom & 1) ? 1 : 0) << 2 |
1442 ((sysinfo->rtt_nom & 2) ? 1 : 0) << 6;
1443 debug("MR1 CS%d: 0x%08x\n", cs, (u32)MR(val, 1, 3, cs));
1444 mmdc0->mdscr = MR(val, 1, 3, cs);
1445
1446 val = ((tcl - 1) << 4) |
1447 (1 << 8) |
1448 ((twr - 3) << 9) |
1449 (sysinfo->pd_fast_exit << 12);
1450 debug("MR0 CS%d: 0x%08x\n", cs, (u32)MR(val, 0, 3, cs));
1451 mmdc0->mdscr = MR(val, 0, 3, cs);
1452
1453 val = (1 << 10);
1454 mmdc0->mdscr = MR(val, 0, 4, cs);
1455 }
1456
1457
1458 mmdc0->mdpdc = (tcke & 0x7) << 16 |
1459 5 << 12 |
1460 5 << 8 |
1461 1 << 6 |
1462 (tcksrx & 0x7) << 3 |
1463 (tcksre & 0x7);
1464 if (!sysinfo->pd_fast_exit)
1465 mmdc0->mdpdc |= (1 << 7);
1466 mmdc0->mapsr = 0x00001006;
1467
1468
1469 val = 0xa1390003;
1470 mmdc0->mpzqhwctrl = val;
1471 if (sysinfo->dsize > 1)
1472 MMDC1(mpzqhwctrl, val);
1473
1474
1475 mmdc0->mdref = (1 << 14) |
1476 (7 << 11);
1477
1478
1479 mmdc0->mdscr = 0x00000000;
1480
1481
1482 mdelay(1);
1483}
1484
1485void mx6_dram_cfg(const struct mx6_ddr_sysinfo *sysinfo,
1486 const struct mx6_mmdc_calibration *calib,
1487 const void *ddr_cfg)
1488{
1489 if (sysinfo->ddr_type == DDR_TYPE_DDR3) {
1490 mx6_ddr3_cfg(sysinfo, calib, ddr_cfg);
1491 } else if (sysinfo->ddr_type == DDR_TYPE_LPDDR2) {
1492 mx6_lpddr2_cfg(sysinfo, calib, ddr_cfg);
1493 } else {
1494 puts("Unsupported ddr type\n");
1495 hang();
1496 }
1497}
1498