1
2
3
4
5
6
7
8
9#include <common.h>
10#include <config.h>
11#include <asm/io.h>
12#include <asm/arch/clock.h>
13#include <asm/arch/cpu.h>
14#include <asm/arch/dmc.h>
15#include <asm/arch/power.h>
16#include "common_setup.h"
17#include "exynos5_setup.h"
18#include "clock_init.h"
19
20#define TIMEOUT_US 10000
21#define NUM_BYTE_LANES 4
22#define DEFAULT_DQS 8
23#define DEFAULT_DQS_X4 (DEFAULT_DQS << 24) || (DEFAULT_DQS << 16) \
24 || (DEFAULT_DQS << 8) || (DEFAULT_DQS << 0)
25
26#ifdef CONFIG_EXYNOS5250
27static void reset_phy_ctrl(void)
28{
29 struct exynos5_clock *clk =
30 (struct exynos5_clock *)samsung_get_base_clock();
31
32 writel(DDR3PHY_CTRL_PHY_RESET_OFF, &clk->lpddr3phy_ctrl);
33 writel(DDR3PHY_CTRL_PHY_RESET, &clk->lpddr3phy_ctrl);
34}
35
36int ddr3_mem_ctrl_init(struct mem_timings *mem, int reset)
37{
38 unsigned int val;
39 struct exynos5_phy_control *phy0_ctrl, *phy1_ctrl;
40 struct exynos5_dmc *dmc;
41 int i;
42
43 phy0_ctrl = (struct exynos5_phy_control *)samsung_get_base_dmc_phy();
44 phy1_ctrl = (struct exynos5_phy_control *)(samsung_get_base_dmc_phy()
45 + DMC_OFFSET);
46 dmc = (struct exynos5_dmc *)samsung_get_base_dmc_ctrl();
47
48 if (reset)
49 reset_phy_ctrl();
50
51
52 val = (mem->impedance << CA_CK_DRVR_DS_OFFSET) |
53 (mem->impedance << CA_CKE_DRVR_DS_OFFSET) |
54 (mem->impedance << CA_CS_DRVR_DS_OFFSET) |
55 (mem->impedance << CA_ADR_DRVR_DS_OFFSET);
56 writel(val, &phy0_ctrl->phy_con39);
57 writel(val, &phy1_ctrl->phy_con39);
58
59
60 val = (mem->ctrl_bstlen << PHY_CON42_CTRL_BSTLEN_SHIFT) |
61 (mem->ctrl_rdlat << PHY_CON42_CTRL_RDLAT_SHIFT);
62 writel(val, &phy0_ctrl->phy_con42);
63 writel(val, &phy1_ctrl->phy_con42);
64
65
66 if (dmc_config_zq(mem, &phy0_ctrl->phy_con16, &phy1_ctrl->phy_con16,
67 &phy0_ctrl->phy_con17, &phy1_ctrl->phy_con17))
68 return SETUP_ERR_ZQ_CALIBRATION_FAILURE;
69
70
71 writel(mem->phy0_pulld_dqs, &phy0_ctrl->phy_con14);
72 writel(mem->phy1_pulld_dqs, &phy1_ctrl->phy_con14);
73
74 writel(mem->concontrol | (mem->rd_fetch << CONCONTROL_RD_FETCH_SHIFT)
75 | (mem->dfi_init_start << CONCONTROL_DFI_INIT_START_SHIFT),
76 &dmc->concontrol);
77
78 update_reset_dll(&dmc->phycontrol0, DDR_MODE_DDR3);
79
80
81 writel(mem->phy0_dqs, &phy0_ctrl->phy_con4);
82 writel(mem->phy1_dqs, &phy1_ctrl->phy_con4);
83
84 writel(mem->phy0_dq, &phy0_ctrl->phy_con6);
85 writel(mem->phy1_dq, &phy1_ctrl->phy_con6);
86
87 writel(mem->phy0_tFS, &phy0_ctrl->phy_con10);
88 writel(mem->phy1_tFS, &phy1_ctrl->phy_con10);
89
90 val = (mem->ctrl_start_point << PHY_CON12_CTRL_START_POINT_SHIFT) |
91 (mem->ctrl_inc << PHY_CON12_CTRL_INC_SHIFT) |
92 (mem->ctrl_dll_on << PHY_CON12_CTRL_DLL_ON_SHIFT) |
93 (mem->ctrl_ref << PHY_CON12_CTRL_REF_SHIFT);
94 writel(val, &phy0_ctrl->phy_con12);
95 writel(val, &phy1_ctrl->phy_con12);
96
97
98 writel(val | (mem->ctrl_start << PHY_CON12_CTRL_START_SHIFT),
99 &phy0_ctrl->phy_con12);
100 writel(val | (mem->ctrl_start << PHY_CON12_CTRL_START_SHIFT),
101 &phy1_ctrl->phy_con12);
102
103 update_reset_dll(&dmc->phycontrol0, DDR_MODE_DDR3);
104
105 writel(mem->concontrol | (mem->rd_fetch << CONCONTROL_RD_FETCH_SHIFT),
106 &dmc->concontrol);
107
108
109 writel(mem->iv_size, &dmc->ivcontrol);
110
111 writel(mem->memconfig, &dmc->memconfig0);
112 writel(mem->memconfig, &dmc->memconfig1);
113 writel(mem->membaseconfig0, &dmc->membaseconfig0);
114 writel(mem->membaseconfig1, &dmc->membaseconfig1);
115
116
117 writel(mem->prechconfig_tp_cnt << PRECHCONFIG_TP_CNT_SHIFT,
118 &dmc->prechconfig);
119
120
121 writel(mem->dpwrdn_cyc << PWRDNCONFIG_DPWRDN_CYC_SHIFT |
122 mem->dsref_cyc << PWRDNCONFIG_DSREF_CYC_SHIFT,
123 &dmc->pwrdnconfig);
124
125
126
127
128 writel(mem->timing_ref, &dmc->timingref);
129 writel(mem->timing_row, &dmc->timingrow);
130 writel(mem->timing_data, &dmc->timingdata);
131 writel(mem->timing_power, &dmc->timingpower);
132
133
134 dmc_config_prech(mem, &dmc->directcmd);
135
136
137 dmc_config_mrs(mem, &dmc->directcmd);
138
139 if (mem->gate_leveling_enable) {
140 val = PHY_CON0_RESET_VAL;
141 val |= P0_CMD_EN;
142 writel(val, &phy0_ctrl->phy_con0);
143 writel(val, &phy1_ctrl->phy_con0);
144
145 val = PHY_CON2_RESET_VAL;
146 val |= INIT_DESKEW_EN;
147 writel(val, &phy0_ctrl->phy_con2);
148 writel(val, &phy1_ctrl->phy_con2);
149
150 val = PHY_CON0_RESET_VAL;
151 val |= P0_CMD_EN;
152 val |= BYTE_RDLVL_EN;
153 writel(val, &phy0_ctrl->phy_con0);
154 writel(val, &phy1_ctrl->phy_con0);
155
156 val = (mem->ctrl_start_point <<
157 PHY_CON12_CTRL_START_POINT_SHIFT) |
158 (mem->ctrl_inc << PHY_CON12_CTRL_INC_SHIFT) |
159 (mem->ctrl_force << PHY_CON12_CTRL_FORCE_SHIFT) |
160 (mem->ctrl_start << PHY_CON12_CTRL_START_SHIFT) |
161 (mem->ctrl_ref << PHY_CON12_CTRL_REF_SHIFT);
162 writel(val, &phy0_ctrl->phy_con12);
163 writel(val, &phy1_ctrl->phy_con12);
164
165 val = PHY_CON2_RESET_VAL;
166 val |= INIT_DESKEW_EN;
167 val |= RDLVL_GATE_EN;
168 writel(val, &phy0_ctrl->phy_con2);
169 writel(val, &phy1_ctrl->phy_con2);
170
171 val = PHY_CON0_RESET_VAL;
172 val |= P0_CMD_EN;
173 val |= BYTE_RDLVL_EN;
174 val |= CTRL_SHGATE;
175 writel(val, &phy0_ctrl->phy_con0);
176 writel(val, &phy1_ctrl->phy_con0);
177
178 val = PHY_CON1_RESET_VAL;
179 val &= ~(CTRL_GATEDURADJ_MASK);
180 writel(val, &phy0_ctrl->phy_con1);
181 writel(val, &phy1_ctrl->phy_con1);
182
183 writel(CTRL_RDLVL_GATE_ENABLE, &dmc->rdlvl_config);
184 i = TIMEOUT_US;
185 while ((readl(&dmc->phystatus) &
186 (RDLVL_COMPLETE_CHO | RDLVL_COMPLETE_CH1)) !=
187 (RDLVL_COMPLETE_CHO | RDLVL_COMPLETE_CH1) && i > 0) {
188
189
190
191
192 sdelay(100);
193 i--;
194 }
195 if (!i)
196 return SETUP_ERR_RDLV_COMPLETE_TIMEOUT;
197 writel(CTRL_RDLVL_GATE_DISABLE, &dmc->rdlvl_config);
198
199 writel(0, &phy0_ctrl->phy_con14);
200 writel(0, &phy1_ctrl->phy_con14);
201
202 val = (mem->ctrl_start_point <<
203 PHY_CON12_CTRL_START_POINT_SHIFT) |
204 (mem->ctrl_inc << PHY_CON12_CTRL_INC_SHIFT) |
205 (mem->ctrl_force << PHY_CON12_CTRL_FORCE_SHIFT) |
206 (mem->ctrl_start << PHY_CON12_CTRL_START_SHIFT) |
207 (mem->ctrl_dll_on << PHY_CON12_CTRL_DLL_ON_SHIFT) |
208 (mem->ctrl_ref << PHY_CON12_CTRL_REF_SHIFT);
209 writel(val, &phy0_ctrl->phy_con12);
210 writel(val, &phy1_ctrl->phy_con12);
211
212 update_reset_dll(&dmc->phycontrol0, DDR_MODE_DDR3);
213 }
214
215
216 dmc_config_prech(mem, &dmc->directcmd);
217
218 writel(mem->memcontrol, &dmc->memcontrol);
219
220
221 writel(mem->concontrol | (mem->rd_fetch << CONCONTROL_RD_FETCH_SHIFT)
222 | (mem->aref_en << CONCONTROL_AREF_EN_SHIFT), &dmc->concontrol);
223 return 0;
224}
225#endif
226
227#ifdef CONFIG_EXYNOS5420
228
229
230
231
232
233
234
235
236
237
238
239
240const unsigned int test_addr = CONFIG_SYS_SDRAM_BASE;
241
242
243static const unsigned int test_pattern[] = {
244 0x5a5a5a5a,
245 0xa5a5a5a5,
246 0xf0f0f0f0,
247 0x0f0f0f0f,
248};
249
250
251
252
253
254
255
256
257
258
259static bool dmc_valid_window_test_vector(int ch, int byte_lane)
260{
261 unsigned int read_data;
262 unsigned int mask;
263 int i;
264
265 mask = 0xFF << (8 * byte_lane);
266
267 for (i = 0; i < ARRAY_SIZE(test_pattern); i++) {
268 read_data = readl(test_addr + i * 4 + ch * 0x80);
269 if ((read_data & mask) != (test_pattern[i] & mask))
270 return false;
271 }
272
273 return true;
274}
275
276
277
278
279
280
281static unsigned int dmc_get_read_offset_value(struct exynos5420_phy_control
282 *phy_ctrl)
283{
284 return readl(&phy_ctrl->phy_con4);
285}
286
287
288
289
290
291
292static void ddr_phy_set_do_resync(struct exynos5420_phy_control *phy_ctrl)
293{
294 setbits_le32(&phy_ctrl->phy_con10, PHY_CON10_CTRL_OFFSETR3);
295 clrbits_le32(&phy_ctrl->phy_con10, PHY_CON10_CTRL_OFFSETR3);
296}
297
298
299
300
301
302
303
304
305
306static void dmc_set_read_offset_value(struct exynos5420_phy_control *phy_ctrl,
307 unsigned int offset)
308{
309 writel(offset, &phy_ctrl->phy_con4);
310 ddr_phy_set_do_resync(phy_ctrl);
311}
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330unsigned char make_signed_byte(signed char b)
331{
332 if (b < 0)
333 return 0x80 | -b;
334 else
335 return b;
336}
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352void test_shifts(struct exynos5420_phy_control *phy_ctrl, int ch,
353 int start, int end, int results[NUM_BYTE_LANES])
354{
355 int incr = (start < end) ? 1 : -1;
356 int byte_lane;
357
358 for (byte_lane = 0; byte_lane < NUM_BYTE_LANES; byte_lane++) {
359 int shift;
360
361 dmc_set_read_offset_value(phy_ctrl, DEFAULT_DQS_X4);
362 results[byte_lane] = DEFAULT_DQS;
363
364 for (shift = start; shift != (end + incr); shift += incr) {
365 unsigned int byte_offsetr;
366 unsigned int offsetr;
367
368 byte_offsetr = make_signed_byte(shift);
369
370 offsetr = dmc_get_read_offset_value(phy_ctrl);
371 offsetr &= ~(0xFF << (8 * byte_lane));
372 offsetr |= (byte_offsetr << (8 * byte_lane));
373 dmc_set_read_offset_value(phy_ctrl, offsetr);
374
375 if (dmc_valid_window_test_vector(ch, byte_lane)) {
376 results[byte_lane] = shift;
377 break;
378 }
379 }
380 }
381}
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403static void software_find_read_offset(struct exynos5420_phy_control *phy_ctrl,
404 int ch, unsigned int coarse_lock_val)
405{
406 unsigned int offsetr_cent;
407 int byte_lane;
408 int left_limit;
409 int right_limit;
410 int left[NUM_BYTE_LANES];
411 int right[NUM_BYTE_LANES];
412 int i;
413
414
415 for (i = 0; i < ARRAY_SIZE(test_pattern); i++)
416 writel(test_pattern[i], test_addr + i * 4 + ch * 0x80);
417
418
419 left_limit = DEFAULT_DQS - coarse_lock_val;
420 right_limit = DEFAULT_DQS + coarse_lock_val;
421 if (right_limit > 127)
422 right_limit = 127;
423
424
425 test_shifts(phy_ctrl, ch, left_limit, right_limit, left);
426 test_shifts(phy_ctrl, ch, right_limit, left_limit, right);
427
428
429 offsetr_cent = 0;
430 for (byte_lane = 0; byte_lane < NUM_BYTE_LANES; byte_lane++) {
431 int temp_center;
432 unsigned int vmwc;
433
434 temp_center = (left[byte_lane] + right[byte_lane]) / 2;
435 vmwc = make_signed_byte(temp_center);
436 offsetr_cent |= vmwc << (8 * byte_lane);
437 }
438 dmc_set_read_offset_value(phy_ctrl, offsetr_cent);
439}
440
441int ddr3_mem_ctrl_init(struct mem_timings *mem, int reset)
442{
443 struct exynos5420_clock *clk =
444 (struct exynos5420_clock *)samsung_get_base_clock();
445 struct exynos5420_power *power =
446 (struct exynos5420_power *)samsung_get_base_power();
447 struct exynos5420_phy_control *phy0_ctrl, *phy1_ctrl;
448 struct exynos5420_dmc *drex0, *drex1;
449 struct exynos5420_tzasc *tzasc0, *tzasc1;
450 struct exynos5_power *pmu;
451 uint32_t val, n_lock_r, n_lock_w_phy0, n_lock_w_phy1;
452 uint32_t lock0_info, lock1_info;
453 int chip;
454 int i;
455
456 phy0_ctrl = (struct exynos5420_phy_control *)samsung_get_base_dmc_phy();
457 phy1_ctrl = (struct exynos5420_phy_control *)(samsung_get_base_dmc_phy()
458 + DMC_OFFSET);
459 drex0 = (struct exynos5420_dmc *)samsung_get_base_dmc_ctrl();
460 drex1 = (struct exynos5420_dmc *)(samsung_get_base_dmc_ctrl()
461 + DMC_OFFSET);
462 tzasc0 = (struct exynos5420_tzasc *)samsung_get_base_dmc_tzasc();
463 tzasc1 = (struct exynos5420_tzasc *)(samsung_get_base_dmc_tzasc()
464 + DMC_OFFSET);
465 pmu = (struct exynos5_power *)EXYNOS5420_POWER_BASE;
466
467
468 setbits_le32(&clk->pause, ENABLE_BIT);
469
470
471 setbits_le32(&clk->bpll_con1, BYPASS_EN);
472
473 writel(MUX_BPLL_SEL_FOUTBPLL, &clk->src_cdrex);
474 do {
475 val = readl(&clk->mux_stat_cdrex);
476 val &= BPLL_SEL_MASK;
477 } while (val != FOUTBPLL);
478
479 clrbits_le32(&clk->bpll_con1, BYPASS_EN);
480
481
482 val = readl(&phy0_ctrl->phy_con0);
483 val &= ~(PHY_CON0_CTRL_DDR_MODE_MASK << PHY_CON0_CTRL_DDR_MODE_SHIFT);
484 val |= (DDR_MODE_DDR3 << PHY_CON0_CTRL_DDR_MODE_SHIFT);
485 writel(val, &phy0_ctrl->phy_con0);
486
487 val = readl(&phy1_ctrl->phy_con0);
488 val &= ~(PHY_CON0_CTRL_DDR_MODE_MASK << PHY_CON0_CTRL_DDR_MODE_SHIFT);
489 val |= (DDR_MODE_DDR3 << PHY_CON0_CTRL_DDR_MODE_SHIFT);
490 writel(val, &phy1_ctrl->phy_con0);
491
492
493 val = (mem->ctrl_bstlen << PHY_CON42_CTRL_BSTLEN_SHIFT) |
494 (mem->ctrl_rdlat << PHY_CON42_CTRL_RDLAT_SHIFT);
495 writel(val, &phy0_ctrl->phy_con42);
496 writel(val, &phy1_ctrl->phy_con42);
497
498 val = readl(&phy0_ctrl->phy_con26);
499 val &= ~(T_WRDATA_EN_MASK << T_WRDATA_EN_OFFSET);
500 val |= (T_WRDATA_EN_DDR3 << T_WRDATA_EN_OFFSET);
501 writel(val, &phy0_ctrl->phy_con26);
502
503 val = readl(&phy1_ctrl->phy_con26);
504 val &= ~(T_WRDATA_EN_MASK << T_WRDATA_EN_OFFSET);
505 val |= (T_WRDATA_EN_DDR3 << T_WRDATA_EN_OFFSET);
506 writel(val, &phy1_ctrl->phy_con26);
507
508
509
510
511
512 val = (0x7 << CA_CK_DRVR_DS_OFFSET) | (0x7 << CA_CKE_DRVR_DS_OFFSET) |
513 (0x7 << CA_CS_DRVR_DS_OFFSET) | (0x7 << CA_ADR_DRVR_DS_OFFSET);
514 val |= (0x7 << DA_3_DS_OFFSET) | (0x7 << DA_2_DS_OFFSET) |
515 (0x7 << DA_1_DS_OFFSET) | (0x7 << DA_0_DS_OFFSET);
516 writel(val, &phy0_ctrl->phy_con39);
517 writel(val, &phy1_ctrl->phy_con39);
518
519
520 if (dmc_config_zq(mem, &phy0_ctrl->phy_con16, &phy1_ctrl->phy_con16,
521 &phy0_ctrl->phy_con17, &phy1_ctrl->phy_con17))
522 return SETUP_ERR_ZQ_CALIBRATION_FAILURE;
523
524 clrbits_le32(&phy0_ctrl->phy_con16, ZQ_CLK_DIV_EN);
525 clrbits_le32(&phy1_ctrl->phy_con16, ZQ_CLK_DIV_EN);
526
527
528 val = readl(&phy0_ctrl->phy_con14);
529 val |= mem->phy0_pulld_dqs;
530 writel(val, &phy0_ctrl->phy_con14);
531 val = readl(&phy1_ctrl->phy_con14);
532 val |= mem->phy1_pulld_dqs;
533 writel(val, &phy1_ctrl->phy_con14);
534
535 val = MEM_TERM_EN | PHY_TERM_EN;
536 writel(val, &drex0->phycontrol0);
537 writel(val, &drex1->phycontrol0);
538
539 writel(mem->concontrol |
540 (mem->dfi_init_start << CONCONTROL_DFI_INIT_START_SHIFT) |
541 (mem->rd_fetch << CONCONTROL_RD_FETCH_SHIFT),
542 &drex0->concontrol);
543 writel(mem->concontrol |
544 (mem->dfi_init_start << CONCONTROL_DFI_INIT_START_SHIFT) |
545 (mem->rd_fetch << CONCONTROL_RD_FETCH_SHIFT),
546 &drex1->concontrol);
547
548 do {
549 val = readl(&drex0->phystatus);
550 } while ((val & DFI_INIT_COMPLETE) != DFI_INIT_COMPLETE);
551 do {
552 val = readl(&drex1->phystatus);
553 } while ((val & DFI_INIT_COMPLETE) != DFI_INIT_COMPLETE);
554
555 clrbits_le32(&drex0->concontrol, DFI_INIT_START);
556 clrbits_le32(&drex1->concontrol, DFI_INIT_START);
557
558 update_reset_dll(&drex0->phycontrol0, DDR_MODE_DDR3);
559 update_reset_dll(&drex1->phycontrol0, DDR_MODE_DDR3);
560
561
562
563
564
565
566
567 val = DMC_MEMBASECONFIGX_CHIP_BASE(DMC_CHIP_BASE_0) |
568 DMC_MEMBASECONFIGX_CHIP_MASK(DMC_CHIP_MASK);
569 writel(val, &tzasc0->membaseconfig0);
570 writel(val, &tzasc1->membaseconfig0);
571
572
573 val = DMC_MEMBASECONFIGX_CHIP_BASE(DMC_CHIP_BASE_1) |
574 DMC_MEMBASECONFIGX_CHIP_MASK(DMC_CHIP_MASK);
575 writel(val, &tzasc0->membaseconfig1);
576 writel(val, &tzasc1->membaseconfig1);
577
578
579
580
581
582
583 writel(mem->memconfig, &tzasc0->memconfig0);
584 writel(mem->memconfig, &tzasc1->memconfig0);
585 writel(mem->memconfig, &tzasc0->memconfig1);
586 writel(mem->memconfig, &tzasc1->memconfig1);
587
588
589 writel(mem->prechconfig_tp_cnt << PRECHCONFIG_TP_CNT_SHIFT,
590 &drex0->prechconfig0);
591 writel(mem->prechconfig_tp_cnt << PRECHCONFIG_TP_CNT_SHIFT,
592 &drex1->prechconfig0);
593
594
595
596
597
598 writel(mem->timing_ref, &drex0->timingref);
599 writel(mem->timing_ref, &drex1->timingref);
600 writel(mem->timing_row, &drex0->timingrow0);
601 writel(mem->timing_row, &drex1->timingrow0);
602 writel(mem->timing_data, &drex0->timingdata0);
603 writel(mem->timing_data, &drex1->timingdata0);
604 writel(mem->timing_power, &drex0->timingpower0);
605 writel(mem->timing_power, &drex1->timingpower0);
606
607 if (reset) {
608
609
610
611
612
613
614 dmc_config_mrs(mem, &drex0->directcmd);
615 dmc_config_mrs(mem, &drex1->directcmd);
616 }
617
618
619
620
621
622
623
624
625
626
627
628 val = readl(&clk->gate_bus_cdrex);
629 while (true) {
630 writel(val & ~0x1, &clk->gate_bus_cdrex);
631 lock0_info = readl(&phy0_ctrl->phy_con13);
632 writel(val, &clk->gate_bus_cdrex);
633
634 if ((lock0_info & CTRL_FINE_LOCKED) == CTRL_FINE_LOCKED)
635 break;
636
637 sdelay(10);
638 }
639 while (true) {
640 writel(val & ~0x2, &clk->gate_bus_cdrex);
641 lock1_info = readl(&phy1_ctrl->phy_con13);
642 writel(val, &clk->gate_bus_cdrex);
643
644 if ((lock1_info & CTRL_FINE_LOCKED) == CTRL_FINE_LOCKED)
645 break;
646
647 sdelay(10);
648 }
649
650 if (!reset) {
651
652
653
654
655
656
657
658
659 writel(PAD_RETENTION_DRAM_COREBLK_VAL,
660 &power->pad_retention_dram_coreblk_option);
661 do {
662 val = readl(&power->pad_retention_dram_status);
663 } while (val != 0x1);
664
665
666
667
668
669 for (i = 0; i < 128; i++) {
670 for (chip = 0; chip < mem->chips_to_configure; chip++) {
671 writel(DIRECT_CMD_REFA |
672 (chip << DIRECT_CMD_CHIP_SHIFT),
673 &drex0->directcmd);
674 writel(DIRECT_CMD_REFA |
675 (chip << DIRECT_CMD_CHIP_SHIFT),
676 &drex1->directcmd);
677 }
678 }
679 }
680
681 if (mem->gate_leveling_enable) {
682 writel(PHY_CON0_RESET_VAL, &phy0_ctrl->phy_con0);
683 writel(PHY_CON0_RESET_VAL, &phy1_ctrl->phy_con0);
684
685 setbits_le32(&phy0_ctrl->phy_con0, P0_CMD_EN);
686 setbits_le32(&phy1_ctrl->phy_con0, P0_CMD_EN);
687
688 val = PHY_CON2_RESET_VAL;
689 val |= INIT_DESKEW_EN;
690 writel(val, &phy0_ctrl->phy_con2);
691 writel(val, &phy1_ctrl->phy_con2);
692
693 val = readl(&phy0_ctrl->phy_con1);
694 val |= (RDLVL_PASS_ADJ_VAL << RDLVL_PASS_ADJ_OFFSET);
695 writel(val, &phy0_ctrl->phy_con1);
696
697 val = readl(&phy1_ctrl->phy_con1);
698 val |= (RDLVL_PASS_ADJ_VAL << RDLVL_PASS_ADJ_OFFSET);
699 writel(val, &phy1_ctrl->phy_con1);
700
701 n_lock_w_phy0 = (lock0_info & CTRL_LOCK_COARSE_MASK) >> 2;
702 n_lock_r = readl(&phy0_ctrl->phy_con12);
703 n_lock_r &= ~CTRL_DLL_ON;
704 n_lock_r |= n_lock_w_phy0;
705 writel(n_lock_r, &phy0_ctrl->phy_con12);
706
707 n_lock_w_phy1 = (lock1_info & CTRL_LOCK_COARSE_MASK) >> 2;
708 n_lock_r = readl(&phy1_ctrl->phy_con12);
709 n_lock_r &= ~CTRL_DLL_ON;
710 n_lock_r |= n_lock_w_phy1;
711 writel(n_lock_r, &phy1_ctrl->phy_con12);
712
713 val = (0x3 << DIRECT_CMD_BANK_SHIFT) | 0x4;
714 for (chip = 0; chip < mem->chips_to_configure; chip++) {
715 writel(val | (chip << DIRECT_CMD_CHIP_SHIFT),
716 &drex0->directcmd);
717 writel(val | (chip << DIRECT_CMD_CHIP_SHIFT),
718 &drex1->directcmd);
719 }
720
721 setbits_le32(&phy0_ctrl->phy_con2, RDLVL_GATE_EN);
722 setbits_le32(&phy1_ctrl->phy_con2, RDLVL_GATE_EN);
723
724 setbits_le32(&phy0_ctrl->phy_con0, CTRL_SHGATE);
725 setbits_le32(&phy1_ctrl->phy_con0, CTRL_SHGATE);
726
727 val = readl(&phy0_ctrl->phy_con1);
728 val &= ~(CTRL_GATEDURADJ_MASK);
729 writel(val, &phy0_ctrl->phy_con1);
730
731 val = readl(&phy1_ctrl->phy_con1);
732 val &= ~(CTRL_GATEDURADJ_MASK);
733 writel(val, &phy1_ctrl->phy_con1);
734
735 writel(CTRL_RDLVL_GATE_ENABLE, &drex0->rdlvl_config);
736 i = TIMEOUT_US;
737 while (((readl(&drex0->phystatus) & RDLVL_COMPLETE_CHO) !=
738 RDLVL_COMPLETE_CHO) && (i > 0)) {
739
740
741
742
743 sdelay(100);
744 i--;
745 }
746 if (!i)
747 return SETUP_ERR_RDLV_COMPLETE_TIMEOUT;
748 writel(CTRL_RDLVL_GATE_DISABLE, &drex0->rdlvl_config);
749
750 writel(CTRL_RDLVL_GATE_ENABLE, &drex1->rdlvl_config);
751 i = TIMEOUT_US;
752 while (((readl(&drex1->phystatus) & RDLVL_COMPLETE_CHO) !=
753 RDLVL_COMPLETE_CHO) && (i > 0)) {
754
755
756
757
758 sdelay(100);
759 i--;
760 }
761 if (!i)
762 return SETUP_ERR_RDLV_COMPLETE_TIMEOUT;
763 writel(CTRL_RDLVL_GATE_DISABLE, &drex1->rdlvl_config);
764
765 writel(0, &phy0_ctrl->phy_con14);
766 writel(0, &phy1_ctrl->phy_con14);
767
768 val = (0x3 << DIRECT_CMD_BANK_SHIFT);
769 for (chip = 0; chip < mem->chips_to_configure; chip++) {
770 writel(val | (chip << DIRECT_CMD_CHIP_SHIFT),
771 &drex0->directcmd);
772 writel(val | (chip << DIRECT_CMD_CHIP_SHIFT),
773 &drex1->directcmd);
774 }
775
776
777 val = PHY_CON12_RESET_VAL;
778 writel((val + n_lock_w_phy0), &phy0_ctrl->phy_con12);
779 writel((val + n_lock_w_phy1), &phy1_ctrl->phy_con12);
780
781 setbits_le32(&phy0_ctrl->phy_con2, DLL_DESKEW_EN);
782 setbits_le32(&phy1_ctrl->phy_con2, DLL_DESKEW_EN);
783 }
784
785
786
787
788
789
790
791
792 if (!reset) {
793
794 dmc_set_read_offset_value(phy0_ctrl, readl(&pmu->pmu_spare1));
795 dmc_set_read_offset_value(phy1_ctrl, readl(&pmu->pmu_spare2));
796 } else {
797 software_find_read_offset(phy0_ctrl, 0,
798 CTRL_LOCK_COARSE(lock0_info));
799 software_find_read_offset(phy1_ctrl, 1,
800 CTRL_LOCK_COARSE(lock1_info));
801
802 writel(dmc_get_read_offset_value(phy0_ctrl), &pmu->pmu_spare1);
803 writel(dmc_get_read_offset_value(phy1_ctrl), &pmu->pmu_spare2);
804 }
805
806
807 dmc_config_prech(mem, &drex0->directcmd);
808 dmc_config_prech(mem, &drex1->directcmd);
809
810 writel(mem->memcontrol, &drex0->memcontrol);
811 writel(mem->memcontrol, &drex1->memcontrol);
812
813
814
815
816
817
818 writel(mem->concontrol | (mem->aref_en << CONCONTROL_AREF_EN_SHIFT) |
819 (mem->rd_fetch << CONCONTROL_RD_FETCH_SHIFT)|
820 DMC_CONCONTROL_IO_PD_CON(0x2),
821 &drex0->concontrol);
822 writel(mem->concontrol | (mem->aref_en << CONCONTROL_AREF_EN_SHIFT) |
823 (mem->rd_fetch << CONCONTROL_RD_FETCH_SHIFT)|
824 DMC_CONCONTROL_IO_PD_CON(0x2),
825 &drex1->concontrol);
826
827
828
829
830
831
832 setbits_le32(&drex0->cgcontrol, DMC_INTERNAL_CG);
833 setbits_le32(&drex1->cgcontrol, DMC_INTERNAL_CG);
834
835 return 0;
836}
837#endif
838