970cbec389341d7fb0c715165877a2d811f8b69e
[firefly-linux-kernel-4.4.55.git] / arch / arm / mach-rk30 / ddr.c
1 /*
2  * arch/arm/mach-rk30/ddr.c
3  *
4  * Function Driver for DDR controller
5  *
6  * Copyright (C) 2011 Fuzhou Rockchip Electronics Co.,Ltd
7  * Author: 
8  * hcy@rock-chips.com
9  * yk@rock-chips.com
10  * 
11  * v1.00 
12  */
13  
14 #include <linux/kernel.h>
15 #include <linux/io.h>
16 #include <linux/module.h>
17 #include <linux/clk.h>
18
19 #include <asm/cacheflush.h>
20 #include <asm/tlbflush.h>
21
22 #include <mach/sram.h>
23 #include <mach/ddr.h>
24
25 typedef uint32_t uint32;
26
27 #define DDR3_DDR2_DLL_DISABLE_FREQ    (125)
28 #define DDR3_DDR2_ODT_DISABLE_FREQ    (333)
29 #define SR_IDLE                       (0x1)   //unit:32*DDR clk cycle, and 0 for disable auto self-refresh
30 #define PD_IDLE                       (0X40)  //unit:DDR clk cycle, and 0 for disable auto power-down
31
32 #define PMU_BASE_ADDR           RK30_PMU_BASE
33 #define SDRAMC_BASE_ADDR        RK30_DDR_PCTL_BASE
34 #define DDR_PUBL_BASE           RK30_DDR_PUBL_BASE
35 #define CRU_BASE_ADDR           RK30_CRU_BASE
36 #define REG_FILE_BASE_ADDR      RK30_GRF_BASE
37 #define SysSrv_DdrConf          (RK30_CPU_AXI_BUS_BASE+0x08)
38 #define SysSrv_DdrTiming        (RK30_CPU_AXI_BUS_BASE+0x0c)
39 #define SysSrv_DdrMode          (RK30_CPU_AXI_BUS_BASE+0x10)
40 #define SysSrv_ReadLatency      (RK30_CPU_AXI_BUS_BASE+0x14)
41
42 #define ddr_print(x...) printk( "DDR DEBUG: " x )
43
44 /***********************************
45  * LPDDR2 define
46  ***********************************/
47 //MR0 (Device Information)
48 #define  LPDDR2_DAI    (0x1)        // 0:DAI complete, 1:DAI still in progress
49 #define  LPDDR2_DI     (0x1<<1)     // 0:S2 or S4 SDRAM, 1:NVM
50 #define  LPDDR2_DNVI   (0x1<<2)     // 0:DNV not supported, 1:DNV supported
51 #define  LPDDR2_RZQI   (0x3<<3)     // 00:RZQ self test not supported, 01:ZQ-pin may connect to VDDCA or float
52                                     // 10:ZQ-pin may short to GND.     11:ZQ-pin self test completed, no error condition detected.
53
54 //MR1 (Device Feature)
55 #define LPDDR2_BL4     (0x2)
56 #define LPDDR2_BL8     (0x3)
57 #define LPDDR2_BL16    (0x4)
58 #define LPDDR2_nWR(n)  (((n)-2)<<5)
59
60 //MR2 (Device Feature 2)
61 #define LPDDR2_RL3_WL1  (0x1)
62 #define LPDDR2_RL4_WL2  (0x2)
63 #define LPDDR2_RL5_WL2  (0x3)
64 #define LPDDR2_RL6_WL3  (0x4)
65 #define LPDDR2_RL7_WL4  (0x5)
66 #define LPDDR2_RL8_WL4  (0x6)
67
68 //MR3 (IO Configuration 1)
69 #define LPDDR2_DS_34    (0x1)
70 #define LPDDR2_DS_40    (0x2)
71 #define LPDDR2_DS_48    (0x3)
72 #define LPDDR2_DS_60    (0x4)
73 #define LPDDR2_DS_80    (0x6)
74 #define LPDDR2_DS_120   (0x7)   //optional
75
76 //MR4 (Device Temperature)
77 #define LPDDR2_tREF_MASK (0x7)
78 #define LPDDR2_4_tREF    (0x1)
79 #define LPDDR2_2_tREF    (0x2)
80 #define LPDDR2_1_tREF    (0x3)
81 #define LPDDR2_025_tREF  (0x5)
82 #define LPDDR2_025_tREF_DERATE    (0x6)
83
84 #define LPDDR2_TUF       (0x1<<7)
85
86 //MR8 (Basic configuration 4)
87 #define LPDDR2_S4        (0x0)
88 #define LPDDR2_S2        (0x1)
89 #define LPDDR2_N         (0x2)
90 #define LPDDR2_Density(mr8)  (8<<(((mr8)>>2)&0xf))   // Unit:MB
91 #define LPDDR2_IO_Width(mr8) (32>>(((mr8)>>6)&0x3))
92
93 //MR10 (Calibration)
94 #define LPDDR2_ZQINIT   (0xFF)
95 #define LPDDR2_ZQCL     (0xAB)
96 #define LPDDR2_ZQCS     (0x56)
97 #define LPDDR2_ZQRESET  (0xC3)
98
99 //MR16 (PASR Bank Mask)
100 // S2 SDRAM Only
101 #define LPDDR2_PASR_Full (0x0)    
102 #define LPDDR2_PASR_1_2  (0x1)
103 #define LPDDR2_PASR_1_4  (0x2)
104 #define LPDDR2_PASR_1_8  (0x3)
105
106 //MR17 (PASR Segment Mask) 1Gb-8Gb S4 SDRAM only
107
108 //MR32 (DQ Calibration Pattern A)
109
110 //MR40 (DQ Calibration Pattern B)
111
112 /***********************************
113  * DDR3 define
114  ***********************************/
115 //mr0 for ddr3
116 #define DDR3_BL8          (0)
117 #define DDR3_BC4_8        (1)
118 #define DDR3_BC4          (2)
119 #define DDR3_CL(n)        (((((n)-4)&0x7)<<4)|((((n)-4)&0x8)>>1))
120 #define DDR3_WR(n)        (((n)&0x7)<<9)
121 #define DDR3_DLL_RESET    (1<<8)
122 #define DDR3_DLL_DeRESET  (0<<8)
123     
124 //mr1 for ddr3
125 #define DDR3_DLL_ENABLE    (0)
126 #define DDR3_DLL_DISABLE   (1)
127 #define DDR3_MR1_AL(n)  (((n)&0x7)<<3)
128     
129 #define DDR3_DS_40            (0)
130 #define DDR3_DS_34            (1<<1)
131 #define DDR3_Rtt_Nom_DIS      (0)
132 #define DDR3_Rtt_Nom_60       (1<<2)
133 #define DDR3_Rtt_Nom_120      (1<<6)
134 #define DDR3_Rtt_Nom_40       ((1<<2)|(1<<6))
135     
136     //mr2 for ddr3
137 #define DDR3_MR2_CWL(n) ((((n)-5)&0x7)<<3)
138 #define DDR3_Rtt_WR_DIS       (0)
139 #define DDR3_Rtt_WR_60        (1<<9)
140 #define DDR3_Rtt_WR_120       (2<<9)
141
142 /***********************************
143  * DDR2 define
144  ***********************************/
145 //MR;                     //Mode Register                                                
146 #define DDR2_BL4           (2)
147 #define DDR2_BL8           (3)
148 #define DDR2_CL(n)         (((n)&0x7)<<4)
149 #define DDR2_WR(n)        ((((n)-1)&0x7)<<9)
150 #define DDR2_DLL_RESET    (1<<8)
151 #define DDR2_DLL_DeRESET  (0<<8)
152     
153 //EMR;                    //Extended Mode Register      
154 #define DDR2_DLL_ENABLE    (0)
155 #define DDR2_DLL_DISABLE   (1)
156
157 #define DDR2_STR_FULL     (0)
158 #define DDR2_STR_REDUCE   (1<<1)
159 #define DDR2_AL(n)        (((n)&0x7)<<3)
160 #define DDR2_Rtt_Nom_DIS      (0)
161 #define DDR2_Rtt_Nom_150      (0x40)
162 #define DDR2_Rtt_Nom_75       (0x4)
163 #define DDR2_Rtt_Nom_50       (0x44)
164
165 /***********************************
166  * LPDDR define
167  ***********************************/
168 #define mDDR_BL2           (1)
169 #define mDDR_BL4           (2)
170 #define mDDR_BL8           (3)
171 #define mDDR_CL(n)         (((n)&0x7)<<4)
172     
173 #define mDDR_DS_Full       (0)
174 #define mDDR_DS_1_2        (1<<5)
175 #define mDDR_DS_1_4        (2<<5)
176 #define mDDR_DS_1_8        (3<<5)
177 #define mDDR_DS_3_4        (4<<5)
178
179
180 //PMU_MISC_CON1
181 #define idle_req_cpu_cfg    (1<<1)
182 #define idle_req_peri_cfg   (1<<2)
183 #define idle_req_gpu_cfg    (1<<3)
184 #define idle_req_video_cfg  (1<<4)
185 #define idle_req_vio_cfg    (1<<5)
186
187 //PMU_PWRDN_ST
188 #define idle_cpu    (1<<26)
189 #define idle_peri   (1<<25)
190 #define idle_gpu    (1<<24)
191 #define idle_video  (1<<23)
192 #define idle_vio    (1<<22)
193
194 #define pd_a9_0_pwr_st    (1<<0)
195 #define pd_a9_1_pwr_st    (1<<1)
196 #define pd_peri_pwr_st    (1<<6)
197 #define pd_vio_pwr_st    (1<<7)
198 #define pd_video_pwr_st    (1<<8)
199 #define pd_gpu_pwr_st    (1<<9)
200
201
202 //PMU registers
203 typedef volatile struct tagPMU_FILE
204 {
205     uint32 PMU_WAKEUP_CFG[2];
206     uint32 PMU_PWRDN_CON;
207     uint32 PMU_PWRDN_ST;
208     uint32 PMU_INT_CON;
209     uint32 PMU_INT_ST;
210     uint32 PMU_MISC_CON;
211     uint32 PMU_OSC_CNT;
212     uint32 PMU_PLL_CNT;
213     uint32 PMU_PMU_CNT;
214     uint32 PMU_DDRIO_PWRON_CNT;
215     uint32 PMU_WAKEUP_RST_CLR_CNT;
216     uint32 PMU_SCU_PWRDWN_CNT;
217     uint32 PMU_SCU_PWRUP_CNT;
218     uint32 PMU_MISC_CON1;
219     uint32 PMU_GPIO6_CON;
220     uint32 PMU_PMU_SYS_REG[4];
221 } PMU_FILE, *pPMU_FILE;
222
223 #define pPMU_Reg ((pPMU_FILE)PMU_BASE_ADDR)
224
225 #define PLL_RESET  (((0x1<<5)<<16) | (0x1<<5))
226 #define PLL_DE_RESET  (((0x1<<5)<<16) | (0x0<<5))
227 #define NR(n)      ((0x3F<<(8+16)) | (((n)-1)<<8))
228 #define NO(n)      ((0xF<<16) | ((n)-1))
229 #define NF(n)      ((0x1FFF<<16) | ((n)-1))
230 #define NB(n)      ((0xFFF<<16) | ((n)-1))
231  //CRU Registers
232 typedef volatile struct tagCRU_STRUCT
233 {
234     uint32 CRU_PLL_CON[4][4];
235     uint32 CRU_MODE_CON;
236     uint32 CRU_CLKSEL_CON[35];
237     uint32 CRU_CLKGATE_CON[10];
238     uint32 reserved1[2];
239     uint32 CRU_GLB_SRST_FST_VALUE;
240     uint32 CRU_GLB_SRST_SND_VALUE;
241     uint32 reserved2[2];
242     uint32 CRU_SOFTRST_CON[9];
243     uint32 CRU_MISC_CON;
244     uint32 reserved3[2];
245     uint32 CRU_GLB_CNT_TH;
246 } CRU_REG, *pCRU_REG;
247
248 #define pCRU_Reg ((pCRU_REG)CRU_BASE_ADDR)
249
250 #define bank2_to_rank_en   ((1<<2) | ((1<<2)<<16))
251 #define bank2_to_rank_dis   ((0<<2) | ((1<<2)<<16))
252 #define rank_to_row15_en   ((1<<1) | ((1<<1)<<16))
253 #define rank_to_row15_dis   ((0<<1) | ((1<<1)<<16))
254
255 typedef struct tagGPIO_LH
256 {
257     uint32 GPIOL;
258     uint32 GPIOH;
259 }GPIO_LH_T;
260
261 typedef struct tagGPIO_IOMUX
262 {
263     uint32 GPIOA_IOMUX;
264     uint32 GPIOB_IOMUX;
265     uint32 GPIOC_IOMUX;
266     uint32 GPIOD_IOMUX;
267 }GPIO_IOMUX_T;
268
269 //REG FILE registers
270 typedef volatile struct tagREG_FILE
271 {
272     GPIO_LH_T GRF_GPIO_DIR[7];
273     GPIO_LH_T GRF_GPIO_DO[7];
274     GPIO_LH_T GRF_GPIO_EN[7];
275     GPIO_IOMUX_T GRF_GPIO_IOMUX[7];
276     GPIO_LH_T GRF_GPIO_PULL[7];
277     uint32 GRF_SOC_CON[3];
278     uint32 GRF_SOC_STATUS0;
279     uint32 GRF_DMAC1_CON[3];
280     uint32 GRF_DMAC2_CON[4];
281     uint32 GRF_UOC0_CON[3];
282     uint32 GRF_UOC1_CON[4];
283     uint32 GRF_DDRC_CON0;
284     uint32 GRF_DDRC_STAT;
285     uint32 reserved[(0x1c8-0x1a0)/4];
286     uint32 GRF_OS_REG[4];
287 } REG_FILE, *pREG_FILE;
288
289 #define pGRF_Reg ((pREG_FILE)REG_FILE_BASE_ADDR)
290
291 //SCTL
292 #define INIT_STATE                     (0)
293 #define CFG_STATE                      (1)
294 #define GO_STATE                       (2)
295 #define SLEEP_STATE                    (3)
296 #define WAKEUP_STATE                   (4)
297
298 //STAT
299 #define Init_mem                       (0)
300 #define Config                         (1)
301 #define Config_req                     (2)
302 #define Access                         (3)
303 #define Access_req                     (4)
304 #define Low_power                      (5)
305 #define Low_power_entry_req            (6)
306 #define Low_power_exit_req             (7)
307
308 //MCFG
309 #define mddr_lpddr2_clk_stop_idle(n)   ((n)<<24)
310 #define pd_idle(n)                     ((n)<<8)
311 #define mddr_en                        (2<<22)
312 #define lpddr2_en                      (3<<22)
313 #define ddr2_en                        (0<<5)
314 #define ddr3_en                        (1<<5)
315 #define lpddr2_s2                      (0<<6)
316 #define lpddr2_s4                      (1<<6)
317 #define mddr_lpddr2_bl_2               (0<<20)
318 #define mddr_lpddr2_bl_4               (1<<20)
319 #define mddr_lpddr2_bl_8               (2<<20)
320 #define mddr_lpddr2_bl_16              (3<<20)
321 #define ddr2_ddr3_bl_4                 (0)
322 #define ddr2_ddr3_bl_8                 (1)
323 #define tfaw_cfg(n)                    (((n)-4)<<18)
324 #define pd_exit_slow                   (0<<17)
325 #define pd_exit_fast                   (1<<17)
326 #define pd_type(n)                     ((n)<<16)
327 #define two_t_en(n)                    ((n)<<3)
328 #define bl8int_en(n)                   ((n)<<2)
329 #define cke_or_en(n)                   ((n)<<1)
330
331 //POWCTL
332 #define power_up_start                 (1<<0)
333
334 //POWSTAT
335 #define power_up_done                  (1<<0)
336
337 //DFISTSTAT0
338 #define dfi_init_complete              (1<<0)
339
340 //CMDTSTAT
341 #define cmd_tstat                      (1<<0)
342
343 //CMDTSTATEN
344 #define cmd_tstat_en                   (1<<1)
345
346 //MCMD
347 #define Deselect_cmd                   (0)
348 #define PREA_cmd                       (1)
349 #define REF_cmd                        (2)
350 #define MRS_cmd                        (3)
351 #define ZQCS_cmd                       (4)
352 #define ZQCL_cmd                       (5)
353 #define RSTL_cmd                       (6)
354 #define MRR_cmd                        (8)
355 #define DPDE_cmd                       (9)
356
357 #define lpddr2_op(n)                   ((n)<<12)
358 #define lpddr2_ma(n)                   ((n)<<4)
359
360 #define bank_addr(n)                   ((n)<<17)
361 #define cmd_addr(n)                    ((n)<<4)
362
363 #define start_cmd                      (1u<<31)
364
365 typedef union STAT_Tag
366 {
367     uint32 d32;
368     struct
369     {
370         unsigned ctl_stat : 3;
371         unsigned reserved3 : 1;
372         unsigned lp_trig : 3;
373         unsigned reserved7_31 : 25;
374     }b;
375 }STAT_T;
376
377 typedef union SCFG_Tag
378 {
379     uint32 d32;
380     struct
381     {
382         unsigned hw_low_power_en : 1;
383         unsigned reserved1_5 : 5;
384         unsigned nfifo_nif1_dis : 1;
385         unsigned reserved7 : 1;
386         unsigned bbflags_timing : 4;
387         unsigned reserved12_31 : 20;
388     } b;
389 }SCFG_T;
390
391 /* DDR Controller register struct */
392 typedef volatile struct DDR_REG_Tag
393 {
394     //Operational State, Control, and Status Registers
395     SCFG_T SCFG;                   //State Configuration Register
396     volatile uint32 SCTL;                   //State Control Register
397     STAT_T STAT;                   //State Status Register
398     volatile uint32 INTRSTAT;               //Interrupt Status Register
399     uint32 reserved0[(0x40-0x10)/4];
400     //Initailization Control and Status Registers
401     volatile uint32 MCMD;                   //Memory Command Register
402     volatile uint32 POWCTL;                 //Power Up Control Registers
403     volatile uint32 POWSTAT;                //Power Up Status Register
404     volatile uint32 CMDTSTAT;               //Command Timing Status Register
405     volatile uint32 CMDTSTATEN;             //Command Timing Status Enable Register
406     uint32 reserved1[(0x60-0x54)/4];
407     volatile uint32 MRRCFG0;                //MRR Configuration 0 Register
408     volatile uint32 MRRSTAT0;               //MRR Status 0 Register
409     volatile uint32 MRRSTAT1;               //MRR Status 1 Register
410     uint32 reserved2[(0x7c-0x6c)/4];
411     //Memory Control and Status Registers
412     volatile uint32 MCFG1;                  //Memory Configuration 1 Register
413     volatile uint32 MCFG;                   //Memory Configuration Register
414     volatile uint32 PPCFG;                  //Partially Populated Memories Configuration Register
415     volatile uint32 MSTAT;                  //Memory Status Register
416     volatile uint32 LPDDR2ZQCFG;            //LPDDR2 ZQ Configuration Register
417     uint32 reserved3;
418     //DTU Control and Status Registers
419     volatile uint32 DTUPDES;                //DTU Status Register
420     volatile uint32 DTUNA;                  //DTU Number of Random Addresses Created Register
421     volatile uint32 DTUNE;                  //DTU Number of Errors Register
422     volatile uint32 DTUPRD0;                //DTU Parallel Read 0
423     volatile uint32 DTUPRD1;                //DTU Parallel Read 1
424     volatile uint32 DTUPRD2;                //DTU Parallel Read 2
425     volatile uint32 DTUPRD3;                //DTU Parallel Read 3
426     volatile uint32 DTUAWDT;                //DTU Address Width
427     uint32 reserved4[(0xc0-0xb4)/4];
428     //Memory Timing Registers
429     volatile uint32 TOGCNT1U;               //Toggle Counter 1U Register
430     volatile uint32 TINIT;                  //t_init Timing Register
431     volatile uint32 TRSTH;                  //Reset High Time Register
432     volatile uint32 TOGCNT100N;             //Toggle Counter 100N Register
433     volatile uint32 TREFI;                  //t_refi Timing Register
434     volatile uint32 TMRD;                   //t_mrd Timing Register
435     volatile uint32 TRFC;                   //t_rfc Timing Register
436     volatile uint32 TRP;                    //t_rp Timing Register
437     volatile uint32 TRTW;                   //t_rtw Timing Register
438     volatile uint32 TAL;                    //AL Latency Register
439     volatile uint32 TCL;                    //CL Timing Register
440     volatile uint32 TCWL;                   //CWL Register
441     volatile uint32 TRAS;                   //t_ras Timing Register
442     volatile uint32 TRC;                    //t_rc Timing Register
443     volatile uint32 TRCD;                   //t_rcd Timing Register
444     volatile uint32 TRRD;                   //t_rrd Timing Register
445     volatile uint32 TRTP;                   //t_rtp Timing Register
446     volatile uint32 TWR;                    //t_wr Timing Register
447     volatile uint32 TWTR;                   //t_wtr Timing Register
448     volatile uint32 TEXSR;                  //t_exsr Timing Register
449     volatile uint32 TXP;                    //t_xp Timing Register
450     volatile uint32 TXPDLL;                 //t_xpdll Timing Register
451     volatile uint32 TZQCS;                  //t_zqcs Timing Register
452     volatile uint32 TZQCSI;                 //t_zqcsi Timing Register
453     volatile uint32 TDQS;                   //t_dqs Timing Register
454     volatile uint32 TCKSRE;                 //t_cksre Timing Register
455     volatile uint32 TCKSRX;                 //t_cksrx Timing Register
456     volatile uint32 TCKE;                   //t_cke Timing Register
457     volatile uint32 TMOD;                   //t_mod Timing Register
458     volatile uint32 TRSTL;                  //Reset Low Timing Register
459     volatile uint32 TZQCL;                  //t_zqcl Timing Register
460     volatile uint32 TMRR;                   //t_mrr Timing Register
461     volatile uint32 TCKESR;                 //t_ckesr Timing Register
462     volatile uint32 TDPD;                   //t_dpd Timing Register
463     uint32 reserved5[(0x180-0x148)/4];
464     //ECC Configuration, Control, and Status Registers
465     volatile uint32 ECCCFG;                   //ECC Configuration Register
466     volatile uint32 ECCTST;                   //ECC Test Register
467     volatile uint32 ECCCLR;                   //ECC Clear Register
468     volatile uint32 ECCLOG;                   //ECC Log Register
469     uint32 reserved6[(0x200-0x190)/4];
470     //DTU Control and Status Registers
471     volatile uint32 DTUWACTL;                 //DTU Write Address Control Register
472     volatile uint32 DTURACTL;                 //DTU Read Address Control Register
473     volatile uint32 DTUCFG;                   //DTU Configuration Control Register
474     volatile uint32 DTUECTL;                  //DTU Execute Control Register
475     volatile uint32 DTUWD0;                   //DTU Write Data 0
476     volatile uint32 DTUWD1;                   //DTU Write Data 1
477     volatile uint32 DTUWD2;                   //DTU Write Data 2
478     volatile uint32 DTUWD3;                   //DTU Write Data 3
479     volatile uint32 DTUWDM;                   //DTU Write Data Mask
480     volatile uint32 DTURD0;                   //DTU Read Data 0
481     volatile uint32 DTURD1;                   //DTU Read Data 1
482     volatile uint32 DTURD2;                   //DTU Read Data 2
483     volatile uint32 DTURD3;                   //DTU Read Data 3
484     volatile uint32 DTULFSRWD;                //DTU LFSR Seed for Write Data Generation
485     volatile uint32 DTULFSRRD;                //DTU LFSR Seed for Read Data Generation
486     volatile uint32 DTUEAF;                   //DTU Error Address FIFO
487     //DFI Control Registers
488     volatile uint32 DFITCTRLDELAY;            //DFI tctrl_delay Register
489     volatile uint32 DFIODTCFG;                //DFI ODT Configuration Register
490     volatile uint32 DFIODTCFG1;               //DFI ODT Configuration 1 Register
491     volatile uint32 DFIODTRANKMAP;            //DFI ODT Rank Mapping Register
492     //DFI Write Data Registers
493     volatile uint32 DFITPHYWRDATA;            //DFI tphy_wrdata Register
494     volatile uint32 DFITPHYWRLAT;             //DFI tphy_wrlat Register
495     uint32 reserved7[(0x260-0x258)/4];
496     volatile uint32 DFITRDDATAEN;             //DFI trddata_en Register
497     volatile uint32 DFITPHYRDLAT;             //DFI tphy_rddata Register
498     uint32 reserved8[(0x270-0x268)/4];
499     //DFI Update Registers
500     volatile uint32 DFITPHYUPDTYPE0;          //DFI tphyupd_type0 Register
501     volatile uint32 DFITPHYUPDTYPE1;          //DFI tphyupd_type1 Register
502     volatile uint32 DFITPHYUPDTYPE2;          //DFI tphyupd_type2 Register
503     volatile uint32 DFITPHYUPDTYPE3;          //DFI tphyupd_type3 Register
504     volatile uint32 DFITCTRLUPDMIN;           //DFI tctrlupd_min Register
505     volatile uint32 DFITCTRLUPDMAX;           //DFI tctrlupd_max Register
506     volatile uint32 DFITCTRLUPDDLY;           //DFI tctrlupd_dly Register
507     uint32 reserved9;
508     volatile uint32 DFIUPDCFG;                //DFI Update Configuration Register
509     volatile uint32 DFITREFMSKI;              //DFI Masked Refresh Interval Register
510     volatile uint32 DFITCTRLUPDI;             //DFI tctrlupd_interval Register
511     uint32 reserved10[(0x2ac-0x29c)/4];
512     volatile uint32 DFITRCFG0;                //DFI Training Configuration 0 Register
513     volatile uint32 DFITRSTAT0;               //DFI Training Status 0 Register
514     volatile uint32 DFITRWRLVLEN;             //DFI Training dfi_wrlvl_en Register
515     volatile uint32 DFITRRDLVLEN;             //DFI Training dfi_rdlvl_en Register
516     volatile uint32 DFITRRDLVLGATEEN;         //DFI Training dfi_rdlvl_gate_en Register
517     //DFI Status Registers
518     volatile uint32 DFISTSTAT0;               //DFI Status Status 0 Register
519     volatile uint32 DFISTCFG0;                //DFI Status Configuration 0 Register
520     volatile uint32 DFISTCFG1;                //DFI Status configuration 1 Register
521     uint32 reserved11;
522     volatile uint32 DFITDRAMCLKEN;            //DFI tdram_clk_enalbe Register
523     volatile uint32 DFITDRAMCLKDIS;           //DFI tdram_clk_disalbe Register
524     volatile uint32 DFISTCFG2;                //DFI Status configuration 2 Register
525     volatile uint32 DFISTPARCLR;              //DFI Status Parity Clear Register
526     volatile uint32 DFISTPARLOG;              //DFI Status Parity Log Register
527     uint32 reserved12[(0x2f0-0x2e4)/4];
528     //DFI Low Power Registers
529     volatile uint32 DFILPCFG0;                //DFI Low Power Configuration 0 Register
530     uint32 reserved13[(0x300-0x2f4)/4];
531     //DFI Training 2 Registers
532     volatile uint32 DFITRWRLVLRESP0;          //DFI Training dif_wrlvl_resp Status 0 Register
533     volatile uint32 DFITRWRLVLRESP1;          //DFI Training dif_wrlvl_resp Status 1 Register
534     volatile uint32 DFITRWRLVLRESP2;          //DFI Training dif_wrlvl_resp Status 2 Register
535     volatile uint32 DFITRRDLVLRESP0;          //DFI Training dif_rdlvl_resp Status 0 Register
536     volatile uint32 DFITRRDLVLRESP1;          //DFI Training dif_rdlvl_resp Status 1 Register
537     volatile uint32 DFITRRDLVLRESP2;          //DFI Training dif_rdlvl_resp Status 2 Register
538     volatile uint32 DFITRWRLVLDELAY0;         //DFI Training dif_wrlvl_delay Configuration 0 Register
539     volatile uint32 DFITRWRLVLDELAY1;         //DFI Training dif_wrlvl_delay Configuration 1 Register
540     volatile uint32 DFITRWRLVLDELAY2;         //DFI Training dif_wrlvl_delay Configuration 2 Register
541     volatile uint32 DFITRRDLVLDELAY0;         //DFI Training dif_rdlvl_delay Configuration 0 Register
542     volatile uint32 DFITRRDLVLDELAY1;         //DFI Training dif_rdlvl_delay Configuration 1 Register
543     volatile uint32 DFITRRDLVLDELAY2;         //DFI Training dif_rdlvl_delay Configuration 2 Register
544     volatile uint32 DFITRRDLVLGATEDELAY0;     //DFI Training dif_rdlvl_gate_delay Configuration 0 Register
545     volatile uint32 DFITRRDLVLGATEDELAY1;     //DFI Training dif_rdlvl_gate_delay Configuration 1 Register
546     volatile uint32 DFITRRDLVLGATEDELAY2;     //DFI Training dif_rdlvl_gate_delay Configuration 2 Register
547     volatile uint32 DFITRCMD;                 //DFI Training Command Register
548     uint32 reserved14[(0x3f8-0x340)/4];
549     //IP Status Registers
550     volatile uint32 IPVR;                     //IP Version Register
551     volatile uint32 IPTR;                     //IP Type Register
552 }DDR_REG_T, *pDDR_REG_T;
553
554 #define pDDR_Reg ((pDDR_REG_T)SDRAMC_BASE_ADDR)
555
556 //PIR
557 #define INIT                 (1<<0)
558 #define DLLSRST              (1<<1)
559 #define DLLLOCK              (1<<2)
560 #define ZCAL                 (1<<3)
561 #define ITMSRST              (1<<4)
562 #define DRAMRST              (1<<5)
563 #define DRAMINIT             (1<<6)
564 #define QSTRN                (1<<7)
565 #define EYETRN               (1<<8)
566 #define ICPC                 (1<<16)
567 #define DLLBYP               (1<<17)
568 #define CTLDINIT             (1<<18)
569 #define CLRSR                (1<<28)
570 #define LOCKBYP              (1<<29)
571 #define ZCALBYP              (1<<30)
572 #define INITBYP              (1u<<31)
573
574 //PGCR
575 #define DFTLMT(n)            ((n)<<3)
576 #define DFTCMP(n)            ((n)<<2)
577 #define DQSCFG(n)            ((n)<<1)
578 #define ITMDMD(n)            ((n)<<0)
579 #define RANKEN(n)            ((n)<<18)
580
581 //PGSR
582 #define IDONE                (1<<0)
583 #define DLDONE               (1<<1)
584 #define ZCDONE               (1<<2)
585 #define DIDONE               (1<<3)
586 #define DTDONE               (1<<4)
587 #define DTERR                (1<<5)
588 #define DTIERR               (1<<6)
589 #define DFTERR               (1<<7)
590 #define TQ                   (1u<<31)
591
592 //PTR0
593 #define tITMSRST(n)          ((n)<<18)
594 #define tDLLLOCK(n)          ((n)<<6)
595 #define tDLLSRST(n)          ((n)<<0)
596
597 //PTR1
598 #define tDINIT1(n)           ((n)<<19)
599 #define tDINIT0(n)           ((n)<<0)
600
601 //PTR2
602 #define tDINIT3(n)           ((n)<<17)
603 #define tDINIT2(n)           ((n)<<0)
604
605 //DSGCR
606 #define DQSGE(n)             ((n)<<8)
607 #define DQSGX(n)             ((n)<<5)
608
609 typedef union DCR_Tag
610 {
611     uint32 d32;
612     struct
613     {
614         unsigned DDRMD : 3;
615         unsigned DDR8BNK : 1;
616         unsigned PDQ : 3;
617         unsigned MPRDQ : 1;
618         unsigned DDRTYPE : 2;
619         unsigned reserved10_26 : 17;
620         unsigned NOSRA : 1;
621         unsigned DDR2T : 1;
622         unsigned UDIMM : 1;
623         unsigned RDIMM : 1;
624         unsigned TPD : 1;
625     } b;
626 }DCR_T;
627
628
629 typedef volatile struct DATX8_REG_Tag
630 {
631     volatile uint32 DXGCR;                 //DATX8 General Configuration Register
632     volatile uint32 DXGSR[2];              //DATX8 General Status Register
633     volatile uint32 DXDLLCR;               //DATX8 DLL Control Register
634     volatile uint32 DXDQTR;                //DATX8 DQ Timing Register
635     volatile uint32 DXDQSTR;               //DATX8 DQS Timing Register
636     uint32 reserved[0x80-0x76];
637 }DATX8_REG_T;
638
639 /* DDR PHY register struct */
640 typedef volatile struct DDRPHY_REG_Tag
641 {
642     volatile uint32 RIDR;                   //Revision Identification Register
643     volatile uint32 PIR;                    //PHY Initialization Register
644     volatile uint32 PGCR;                   //PHY General Configuration Register
645     volatile uint32 PGSR;                   //PHY General Status Register
646     volatile uint32 DLLGCR;                 //DLL General Control Register
647     volatile uint32 ACDLLCR;                //AC DLL Control Register
648     volatile uint32 PTR[3];                 //PHY Timing Registers 0-2
649     volatile uint32 ACIOCR;                 //AC I/O Configuration Register
650     volatile uint32 DXCCR;                  //DATX8 Common Configuration Register
651     volatile uint32 DSGCR;                  //DDR System General Configuration Register
652     DCR_T DCR;                    //DRAM Configuration Register
653     volatile uint32 DTPR[3];                //DRAM Timing Parameters Register 0-2
654     volatile uint32 MR[4];                    //Mode Register 0-3
655     volatile uint32 ODTCR;                  //ODT Configuration Register
656     volatile uint32 DTAR;                   //Data Training Address Register
657     volatile uint32 DTDR[2];                //Data Training Data Register 0-1
658
659     uint32 reserved1[0x30-0x18];
660     uint32 DCU[0x38-0x30];
661     uint32 reserved2[0x40-0x38];
662     uint32 BIST[0x51-0x40];
663     uint32 reserved3[0x60-0x51];
664
665     volatile uint32 ZQ0CR[2];               //ZQ 0 Impedance Control Register 0-1
666     volatile uint32 ZQ0SR[2];               //ZQ 0 Impedance Status Register 0-1
667     volatile uint32 ZQ1CR[2];               //ZQ 1 Impedance Control Register 0-1
668     volatile uint32 ZQ1SR[2];               //ZQ 1 Impedance Status Register 0-1
669     volatile uint32 ZQ2CR[2];               //ZQ 2 Impedance Control Register 0-1
670     volatile uint32 ZQ2SR[2];               //ZQ 2 Impedance Status Register 0-1
671     volatile uint32 ZQ3CR[2];               //ZQ 3 Impedance Control Register 0-1
672     volatile uint32 ZQ3SR[2];               //ZQ 3 Impedance Status Register 0-1
673
674     DATX8_REG_T     DATX8[9];               //DATX8 Register
675 }DDRPHY_REG_T, *pDDRPHY_REG_T;
676
677 #define pPHY_Reg ((pDDRPHY_REG_T)DDR_PUBL_BASE)
678
679 typedef enum DRAM_TYPE_Tag
680 {
681     LPDDR = 0,
682     DDR,
683     DDR2,
684     DDR3,
685     LPDDR2,
686
687     DRAM_MAX
688 }DRAM_TYPE;
689
690 typedef struct PCTRL_TIMING_Tag
691 {
692     uint32 ddrFreq;
693     //Memory Timing Registers
694     uint32 togcnt1u;               //Toggle Counter 1U Register
695     uint32 tinit;                  //t_init Timing Register
696     uint32 trsth;                  //Reset High Time Register
697     uint32 togcnt100n;             //Toggle Counter 100N Register
698     uint32 trefi;                  //t_refi Timing Register
699     uint32 tmrd;                   //t_mrd Timing Register
700     uint32 trfc;                   //t_rfc Timing Register
701     uint32 trp;                    //t_rp Timing Register
702     uint32 trtw;                   //t_rtw Timing Register
703     uint32 tal;                    //AL Latency Register
704     uint32 tcl;                    //CL Timing Register
705     uint32 tcwl;                   //CWL Register
706     uint32 tras;                   //t_ras Timing Register
707     uint32 trc;                    //t_rc Timing Register
708     uint32 trcd;                   //t_rcd Timing Register
709     uint32 trrd;                   //t_rrd Timing Register
710     uint32 trtp;                   //t_rtp Timing Register
711     uint32 twr;                    //t_wr Timing Register
712     uint32 twtr;                   //t_wtr Timing Register
713     uint32 texsr;                  //t_exsr Timing Register
714     uint32 txp;                    //t_xp Timing Register
715     uint32 txpdll;                 //t_xpdll Timing Register
716     uint32 tzqcs;                  //t_zqcs Timing Register
717     uint32 tzqcsi;                 //t_zqcsi Timing Register
718     uint32 tdqs;                   //t_dqs Timing Register
719     uint32 tcksre;                 //t_cksre Timing Register
720     uint32 tcksrx;                 //t_cksrx Timing Register
721     uint32 tcke;                   //t_cke Timing Register
722     uint32 tmod;                   //t_mod Timing Register
723     uint32 trstl;                  //Reset Low Timing Register
724     uint32 tzqcl;                  //t_zqcl Timing Register
725     uint32 tmrr;                   //t_mrr Timing Register
726     uint32 tckesr;                 //t_ckesr Timing Register
727     uint32 tdpd;                   //t_dpd Timing Register
728 }PCTL_TIMING_T;
729
730 typedef union DTPR_0_Tag
731 {
732     uint32 d32;
733     struct 
734     {
735         unsigned tMRD : 2;
736         unsigned tRTP : 3;
737         unsigned tWTR : 3;
738         unsigned tRP : 4;
739         unsigned tRCD : 4;
740         unsigned tRAS : 5;
741         unsigned tRRD : 4;
742         unsigned tRC : 6;
743         unsigned tCCD : 1;
744     } b;
745 }DTPR_0_T;
746
747 typedef union DTPR_1_Tag
748 {
749     uint32 d32;
750     struct 
751     {
752         unsigned tAOND : 2;
753         unsigned tRTW : 1;
754         unsigned tFAW : 6;
755         unsigned tMOD : 2;
756         unsigned tRTODT : 1;
757         unsigned reserved12_15 : 4;
758         unsigned tRFC : 8;
759         unsigned tDQSCK : 3;
760         unsigned tDQSCKmax : 3;
761         unsigned reserved30_31 : 2;
762     } b;
763 }DTPR_1_T;
764
765 typedef union DTPR_2_Tag
766 {
767     uint32 d32;
768     struct 
769     {
770         unsigned tXS : 10;
771         unsigned tXP : 5;
772         unsigned tCKE : 4;
773         unsigned tDLLK : 10;
774         unsigned reserved29_31 : 3;
775     } b;
776 }DTPR_2_T;
777
778 typedef struct PHY_TIMING_Tag
779 {
780     DTPR_0_T  dtpr0;
781     DTPR_1_T  dtpr1;
782     DTPR_2_T  dtpr2;
783     uint32    mr[4];   //LPDDR2 no MR0, mr[2] is mDDR MR1
784 }PHY_TIMING_T;
785
786 typedef union NOC_TIMING_Tag
787 {
788     uint32 d32;
789     struct 
790     {
791         unsigned ActToAct : 6;
792         unsigned RdToMiss : 6;
793         unsigned WrToMiss : 6;
794         unsigned BurstLen : 3;
795         unsigned RdToWr : 5;
796         unsigned WrToRd : 5;
797         unsigned BwRatio : 1;
798     } b;
799 }NOC_TIMING_T;
800
801 typedef struct PCTL_REG_Tag
802 {
803     uint32 SCFG;
804     uint32 CMDTSTATEN;
805     uint32 MCFG1;
806     uint32 MCFG;
807     PCTL_TIMING_T pctl_timing;
808     //DFI Control Registers
809     uint32 DFITCTRLDELAY;
810     uint32 DFIODTCFG;
811     uint32 DFIODTCFG1;
812     uint32 DFIODTRANKMAP;
813     //DFI Write Data Registers
814     uint32 DFITPHYWRDATA;
815     uint32 DFITPHYWRLAT;
816     //DFI Read Data Registers
817     uint32 DFITRDDATAEN;
818     uint32 DFITPHYRDLAT;
819     //DFI Update Registers
820     uint32 DFITPHYUPDTYPE0;
821     uint32 DFITPHYUPDTYPE1;
822     uint32 DFITPHYUPDTYPE2;
823     uint32 DFITPHYUPDTYPE3;
824     uint32 DFITCTRLUPDMIN;
825     uint32 DFITCTRLUPDMAX;
826     uint32 DFITCTRLUPDDLY;
827     uint32 DFIUPDCFG;
828     uint32 DFITREFMSKI;
829     uint32 DFITCTRLUPDI;
830     //DFI Status Registers
831     uint32 DFISTCFG0;
832     uint32 DFISTCFG1;
833     uint32 DFITDRAMCLKEN;
834     uint32 DFITDRAMCLKDIS;
835     uint32 DFISTCFG2;
836     //DFI Low Power Register
837     uint32 DFILPCFG0;
838 }PCTL_REG_T;
839
840 typedef struct PUBL_REG_Tag
841 {
842     uint32 PIR;
843     uint32 PGCR;
844     uint32 DLLGCR;
845     uint32 ACDLLCR;
846     uint32 PTR[3];
847     uint32 ACIOCR;
848     uint32 DXCCR;
849     uint32 DSGCR;
850     uint32 DCR;
851     PHY_TIMING_T phy_timing;
852     uint32 ODTCR;
853     uint32 DTAR;
854     uint32 ZQ0CR0;
855     uint32 ZQ1CR0;
856     
857     uint32 DX0GCR;
858     uint32 DX0DLLCR;
859     uint32 DX0DQTR;
860     uint32 DX0DQSTR;
861
862     uint32 DX1GCR;
863     uint32 DX1DLLCR;
864     uint32 DX1DQTR;
865     uint32 DX1DQSTR;
866
867     uint32 DX2GCR;
868     uint32 DX2DLLCR;
869     uint32 DX2DQTR;
870     uint32 DX2DQSTR;
871
872     uint32 DX3GCR;
873     uint32 DX3DLLCR;
874     uint32 DX3DQTR;
875     uint32 DX3DQSTR;
876 }PUBL_REG_T;
877
878 typedef struct BACKUP_REG_Tag
879 {
880     PCTL_REG_T pctl;
881     PUBL_REG_T publ;
882     uint32 DdrConf;
883     NOC_TIMING_T noc_timing;
884     uint32 DdrMode;
885     uint32 ReadLatency;
886 }BACKUP_REG_T;
887
888 __sramdata BACKUP_REG_T ddr_reg;
889
890 typedef struct DDR_CONFIG_2_RBC_Tag
891 {
892     unsigned int row;
893     unsigned int bank;
894     unsigned int col;
895 }DDR_CONFIG_2_RBC_T;
896
897 DDR_CONFIG_2_RBC_T  ddr_cfg_2_rbc[16] = 
898 {
899     {15,3,11},  // bank ahead
900     {15,3,10},
901     {14,3,10},
902     {13,3,10},
903     {15,3,11},
904     {14,3,11},
905     {13,3,11},
906     {14,3,9},
907     {13,3,9},
908     {15,2,11},
909     {14,2,11},
910     {15,2,10},
911     {14,2,10},
912     {14,2,9},
913     {13,2,9},
914     {15,3,10}   // bank ahead
915 };
916
917 uint32_t ddr_data_training_buf[32];
918
919 uint32_t __sramdata ddr3_cl_cwl[22][4]={
920 /*   0~330           330~400         400~533        speed
921 * tCK  >3             2.5~3          1.875~2.5     1.875~1.5
922 *    cl<<16, cwl    cl<<16, cwl     cl<<16, cwl              */
923     {((5<<16)|5),   ((5<<16)|5),    0          ,   0}, //DDR3_800D
924     {((5<<16)|5),   ((6<<16)|5),    0          ,   0}, //DDR3_800E
925
926     {((5<<16)|5),   ((5<<16)|5),    ((6<<16)|6),   0}, //DDR3_1066E
927     {((5<<16)|5),   ((6<<16)|5),    ((7<<16)|6),   0}, //DDR3_1066F
928     {((5<<16)|5),   ((6<<16)|5),    ((8<<16)|6),   0}, //DDR3_1066G
929
930     {((5<<16)|5),   ((5<<16)|5),    ((6<<16)|6),   ((7<<16)|7)}, //DDR3_1333F
931     {((5<<16)|5),   ((5<<16)|5),    ((7<<16)|6),   ((8<<16)|7)}, //DDR3_1333G
932     {((5<<16)|5),   ((6<<16)|5),    ((7<<16)|6),   ((9<<16)|7)}, //DDR3_1333H
933     {((5<<16)|5),   ((6<<16)|5),    ((8<<16)|6),   ((10<<16)|7)}, //DDR3_1333J
934
935     {((5<<16)|5),   ((5<<16)|5),    ((6<<16)|6),   ((7<<16)|7)}, //DDR3_1600G
936     {((5<<16)|5),   ((5<<16)|5),    ((6<<16)|6),   ((8<<16)|7)}, //DDR3_1600H
937     {((5<<16)|5),   ((5<<16)|5),    ((7<<16)|6),   ((9<<16)|7)}, //DDR3_1600J
938     {((5<<16)|5),   ((6<<16)|5),    ((7<<16)|6),   ((10<<16)|7)}, //DDR3_1600K
939
940     {((5<<16)|5),   ((5<<16)|5),    ((6<<16)|6),   ((8<<16)|7)}, //DDR3_1866J
941     {((5<<16)|5),   ((5<<16)|5),    ((7<<16)|6),   ((8<<16)|7)}, //DDR3_1866K
942     {((6<<16)|5),   ((6<<16)|5),    ((7<<16)|6),   ((9<<16)|7)}, //DDR3_1866L
943     {((6<<16)|5),   ((6<<16)|5),    ((8<<16)|6),   ((10<<16)|7)}, //DDR3_1866M
944
945     {((5<<16)|5),   ((5<<16)|5),    ((6<<16)|6),   ((7<<16)|7)}, //DDR3_2133K
946     {((5<<16)|5),   ((5<<16)|5),    ((6<<16)|6),   ((8<<16)|7)}, //DDR3_2133L
947     {((5<<16)|5),   ((5<<16)|5),    ((7<<16)|6),   ((9<<16)|7)}, //DDR3_2133M
948     {((6<<16)|5),   ((6<<16)|5),    ((7<<16)|6),   ((9<<16)|7)},  //DDR3_2133N
949
950     {((6<<16)|5),   ((6<<16)|5),    ((8<<16)|6),   ((10<<16)|7)} //DDR3_DEFAULT
951
952 };
953 uint32_t __sramdata ddr3_tRC_tFAW[22]={
954 /**    tRC    tFAW   */
955     ((50<<16)|50), //DDR3_800D
956     ((53<<16)|50), //DDR3_800E
957
958     ((49<<16)|50), //DDR3_1066E
959     ((51<<16)|50), //DDR3_1066F
960     ((53<<16)|50), //DDR3_1066G
961
962     ((47<<16)|45), //DDR3_1333F
963     ((48<<16)|45), //DDR3_1333G
964     ((50<<16)|45), //DDR3_1333H
965     ((51<<16)|45), //DDR3_1333J
966
967     ((45<<16)|40), //DDR3_1600G
968     ((47<<16)|40), //DDR3_1600H
969     ((48<<16)|40), //DDR3_1600J
970     ((49<<16)|40), //DDR3_1600K
971
972     ((45<<16)|35), //DDR3_1866J
973     ((46<<16)|35), //DDR3_1866K
974     ((47<<16)|35), //DDR3_1866L
975     ((48<<16)|35), //DDR3_1866M
976
977     ((44<<16)|35), //DDR3_2133K
978     ((45<<16)|35), //DDR3_2133L
979     ((46<<16)|35), //DDR3_2133M
980     ((47<<16)|35), //DDR3_2133N
981
982     ((53<<16)|50)  //DDR3_DEFAULT
983 };
984 __sramdata uint32_t mem_type;    // 0:LPDDR, 1:DDR, 2:DDR2, 3:DDR3, 4:LPDDR2
985 static __sramdata uint32_t ddr_speed_bin;    // used for ddr3 only
986 static __sramdata uint32_t ddr_capability_per_die;  // one chip cs capability
987 static __sramdata uint32_t ddr_freq;
988 static __sramdata uint32_t ddr_sr_idle;
989
990 /****************************************************************************
991 Internal sram us delay function
992 Cpu highest frequency is 1.6 GHz
993 1 cycle = 1/1.6 ns
994 1 us = 1000 ns = 1000 * 1.6 cycles = 1600 cycles
995 *****************************************************************************/
996 static __sramdata uint32_t loops_per_us;
997
998 #define LPJ_100MHZ  999456UL
999
1000 /*static*/ void __sramlocalfunc ddr_delayus(uint32_t us)
1001 {   
1002     uint32_t count;
1003      
1004     count = loops_per_us*us;
1005     while(count--)  // 3 cycles
1006         barrier();
1007 }
1008
1009 __sramfunc void ddr_copy(uint32 *pDest, uint32 *pSrc, uint32 words)
1010 {
1011     uint32 i;
1012
1013     for(i=0; i<words; i++)
1014     {
1015         pDest[i] = pSrc[i];
1016     }
1017 }
1018
1019 uint32 ddr_get_row(void)
1020 {
1021     uint32 i;
1022     uint32 row;
1023
1024     i = *(volatile uint32*)SysSrv_DdrConf;
1025     row = ddr_cfg_2_rbc[i].row;
1026     if(pGRF_Reg->GRF_SOC_CON[2] &  (1<<1))
1027     {
1028         row += 1;
1029     }
1030     return row;
1031 }
1032
1033 uint32 ddr_get_bank(void)
1034 {
1035     uint32 i;
1036
1037     i = *(volatile uint32*)SysSrv_DdrConf;
1038     return ddr_cfg_2_rbc[i].bank;
1039 }
1040
1041 uint32 ddr_get_col(void)
1042 {
1043     uint32 i;
1044
1045     i = *(volatile uint32*)SysSrv_DdrConf;
1046     return ddr_cfg_2_rbc[i].col;
1047 }
1048
1049 uint32_t ddr_get_datatraing_addr(void)
1050 {
1051     uint32_t          value=0;
1052     uint32_t          addr;
1053     uint32_t          col = 0;
1054     uint32_t          row = 0;
1055     uint32_t          bank = 0;
1056     
1057     // caculate aglined physical address 
1058     addr =  __pa((unsigned long)ddr_data_training_buf);
1059     if(addr&0x3F)
1060     {
1061         addr += (64-(addr&0x3F));
1062     }
1063     addr -= 0x60000000;
1064     // find out col£¬row£¬bank
1065     row = ddr_get_row();
1066     bank = ddr_get_bank();
1067     col = ddr_get_col();
1068     // according different address mapping, caculate DTAR register value
1069     switch(*(volatile uint32*)SysSrv_DdrConf)
1070     {
1071         case 0:
1072         case 15:
1073             value |= (addr>>2) & ((0x1<<col)-1);  // col
1074             value |= ((addr>>(2+col)) & ((0x1<<row)-1)) << 12;  // row
1075             value |= ((addr>>(2+col+row)) & ((0x1<<bank)-1)) << 28;  // bank
1076             break;
1077         default:
1078             value |= (addr>>2) & ((0x1<<col)-1);  // col
1079             value |= ((addr>>(2+col+bank)) & ((0x1<<row)-1)) << 12;  // row
1080             value |= ((addr>>(2+col)) & ((0x1<<bank)-1)) << 28;  // bank
1081             break;
1082     }
1083
1084     return value;
1085 }
1086
1087 __sramlocalfunc void ddr_reset_dll(void)
1088 {
1089     pPHY_Reg->ACDLLCR &= ~0x40000000;
1090     pPHY_Reg->DATX8[0].DXDLLCR &= ~0x40000000;
1091     pPHY_Reg->DATX8[1].DXDLLCR &= ~0x40000000;
1092     pPHY_Reg->DATX8[2].DXDLLCR &= ~0x40000000;
1093     pPHY_Reg->DATX8[3].DXDLLCR &= ~0x40000000;
1094     ddr_delayus(1);
1095     pPHY_Reg->ACDLLCR |= 0x40000000;
1096     pPHY_Reg->DATX8[0].DXDLLCR |= 0x40000000;
1097     pPHY_Reg->DATX8[1].DXDLLCR |= 0x40000000;
1098     pPHY_Reg->DATX8[2].DXDLLCR |= 0x40000000;
1099     pPHY_Reg->DATX8[3].DXDLLCR |= 0x40000000;
1100     ddr_delayus(1);
1101 }
1102
1103 __sramfunc void ddr_move_to_Lowpower_state(void)
1104 {
1105     volatile uint32 value;
1106
1107     while(1)
1108     {
1109         value = pDDR_Reg->STAT.b.ctl_stat;
1110         if(value == Low_power)
1111         {
1112             break;
1113         }
1114         switch(value)
1115         {
1116             case Init_mem:
1117                 pDDR_Reg->SCTL = CFG_STATE;
1118                 while((pDDR_Reg->STAT.b.ctl_stat) != Config);
1119             case Config:
1120                 pDDR_Reg->SCTL = GO_STATE;
1121                 while((pDDR_Reg->STAT.b.ctl_stat) != Access);
1122             case Access:
1123                 pDDR_Reg->SCTL = SLEEP_STATE;
1124                 while((pDDR_Reg->STAT.b.ctl_stat) != Low_power);
1125                 break;
1126             default:  //Transitional state
1127                 break;
1128         }
1129     }
1130 }
1131
1132 __sramfunc void ddr_move_to_Access_state(void)
1133 {
1134     volatile uint32 value;
1135
1136     //set auto self-refresh idle
1137     pDDR_Reg->MCFG1=(pDDR_Reg->MCFG1&0xffffff00)|ddr_sr_idle;
1138
1139     while(1)
1140     {
1141         value = pDDR_Reg->STAT.b.ctl_stat;
1142         if((value == Access)
1143            || ((pDDR_Reg->STAT.b.lp_trig == 1) && ((pDDR_Reg->STAT.b.ctl_stat) == Low_power)))
1144         {
1145             break;
1146         }
1147         switch(value)
1148         {
1149             case Low_power:
1150                 pDDR_Reg->SCTL = WAKEUP_STATE;
1151                 while((pDDR_Reg->STAT.b.ctl_stat) != Access);
1152                 while((pPHY_Reg->PGSR & DLDONE) != DLDONE);  //wait DLL lock
1153                 break;
1154             case Init_mem:
1155                 pDDR_Reg->SCTL = CFG_STATE;
1156                 while((pDDR_Reg->STAT.b.ctl_stat) != Config);
1157             case Config:
1158                 pDDR_Reg->SCTL = GO_STATE;
1159                 while(!(((pDDR_Reg->STAT.b.ctl_stat) == Access)
1160                       || ((pDDR_Reg->STAT.b.lp_trig == 1) && ((pDDR_Reg->STAT.b.ctl_stat) == Low_power))));
1161                 break;
1162             default:  //Transitional state
1163                 break;
1164         }
1165     }
1166 }
1167
1168 __sramfunc void ddr_move_to_Config_state(void)
1169 {
1170     volatile uint32 value;
1171
1172     //clear auto self-refresh idle
1173     if(pDDR_Reg->MCFG1 & 0xFF)
1174     {
1175         pDDR_Reg->MCFG1=(pDDR_Reg->MCFG1&0xffffff00)|0x0;
1176         dsb();
1177     }
1178
1179     while(1)
1180     {
1181         value = pDDR_Reg->STAT.b.ctl_stat;
1182         if(value == Config)
1183         {
1184             break;
1185         }
1186         switch(value)
1187         {
1188             case Low_power:
1189                 pDDR_Reg->SCTL = WAKEUP_STATE;
1190                 while((pDDR_Reg->STAT.b.ctl_stat) != Access);
1191                 while((pPHY_Reg->PGSR & DLDONE) != DLDONE);  //wait DLL lock
1192             case Access:
1193             case Init_mem:
1194                 pDDR_Reg->SCTL = CFG_STATE;
1195                 while((pDDR_Reg->STAT.b.ctl_stat) != Config);
1196                 break;
1197             default:  //Transitional state
1198                 break;
1199         }
1200     }
1201 }
1202
1203 //arg°üÀ¨bank_addrºÍcmd_addr
1204 void __sramlocalfunc ddr_send_command(uint32 rank, uint32 cmd, uint32 arg)
1205 {
1206     uint32 i;
1207     pDDR_Reg->MCMD = (start_cmd | (rank<<20) | arg | cmd);
1208     for (i = 0; i < 10; i ++) {;}
1209     while(pDDR_Reg->MCMD & start_cmd);
1210 }
1211
1212 //¶ÔtypeÀàÐ͵ÄDDRµÄ¼¸¸öcs½øÐÐDTT
1213 //0  DTT³É¹¦
1214 //!0 DTTʧ°Ü
1215 uint32_t __sramlocalfunc ddr_data_training(void)
1216 {
1217     uint32 i,value,cs;
1218
1219     // disable auto refresh
1220     value = pDDR_Reg->TREFI;
1221     pDDR_Reg->TREFI = 0;
1222     // clear DTDONE status
1223     pPHY_Reg->PIR |= CLRSR;
1224     cs = ((pPHY_Reg->PGCR>>18) & 0xF);
1225     // trigger DTT
1226     pPHY_Reg->PIR |= INIT | QSTRN | LOCKBYP | ZCALBYP | CLRSR | ICPC;
1227     for (i = 0; i < 10; i ++) {;}
1228     // wait echo byte DTDONE
1229     while((pPHY_Reg->DATX8[0].DXGSR[0] & cs) != cs);
1230     while((pPHY_Reg->DATX8[1].DXGSR[0] & cs) != cs);
1231     while((pPHY_Reg->DATX8[2].DXGSR[0] & cs) != cs);
1232     while((pPHY_Reg->DATX8[3].DXGSR[0] & cs) != cs);  //²âµ½1¸öCSµÄDTT×ʱ¼äÊÇ10.7us¡£×î¶à²¹2´ÎË¢ÐÂ
1233     // send some auto refresh to complement the lost while DTT
1234     if(cs > 1)
1235     {
1236         ddr_send_command(cs, REF_cmd, 0);
1237         ddr_send_command(cs, REF_cmd, 0);
1238         ddr_send_command(cs, REF_cmd, 0);
1239         ddr_send_command(cs, REF_cmd, 0);
1240     }
1241     else
1242     {
1243         ddr_send_command(cs, REF_cmd, 0);
1244         ddr_send_command(cs, REF_cmd, 0);
1245     }  
1246     // resume auto refresh
1247     pDDR_Reg->TREFI = value;
1248
1249     if(pPHY_Reg->PGSR & DTERR)
1250     {
1251         return (-1);
1252     }
1253     else
1254     {
1255         return 0;
1256     }
1257 }
1258
1259 void __sramlocalfunc ddr_set_dll_bypass(uint32 freq)
1260 {
1261     if(freq<=150)
1262     {
1263         pPHY_Reg->DLLGCR &= ~(1<<23);
1264         pPHY_Reg->ACDLLCR |= 0x80000000;
1265         pPHY_Reg->DATX8[0].DXDLLCR |= 0x80000000;
1266         pPHY_Reg->DATX8[1].DXDLLCR |= 0x80000000;
1267         pPHY_Reg->DATX8[2].DXDLLCR |= 0x80000000;
1268         pPHY_Reg->DATX8[3].DXDLLCR |= 0x80000000;
1269         pPHY_Reg->PIR |= DLLBYP;
1270     }
1271     else if(freq<=250)
1272     {
1273         pPHY_Reg->DLLGCR |= (1<<23);
1274         pPHY_Reg->ACDLLCR |= 0x80000000;
1275         pPHY_Reg->DATX8[0].DXDLLCR |= 0x80000000;
1276         pPHY_Reg->DATX8[1].DXDLLCR |= 0x80000000;
1277         pPHY_Reg->DATX8[2].DXDLLCR |= 0x80000000;
1278         pPHY_Reg->DATX8[3].DXDLLCR |= 0x80000000;
1279         pPHY_Reg->PIR |= DLLBYP;
1280     }
1281     else
1282     {
1283         pPHY_Reg->DLLGCR &= ~(1<<23);
1284         pPHY_Reg->ACDLLCR &= ~0x80000000;
1285         pPHY_Reg->DATX8[0].DXDLLCR &= ~0x80000000;
1286         pPHY_Reg->DATX8[1].DXDLLCR &= ~0x80000000;
1287         pPHY_Reg->DATX8[2].DXDLLCR &= ~0x80000000;
1288         pPHY_Reg->DATX8[3].DXDLLCR &= ~0x80000000;
1289         pPHY_Reg->PIR &= ~DLLBYP;
1290     }
1291 }
1292
1293 static __sramdata uint32_t clkr;
1294 static __sramdata uint32_t clkf;
1295 static __sramdata uint32_t clkod;
1296 /*****************************************
1297 NR   NO     NF               Fout                       freq Step     finally use
1298 1    8      12.5 - 62.5      37.5MHz  - 187.5MHz        3MHz          50MHz   <= 150MHz
1299 1    6      12.5 - 62.5      50MHz    - 250MHz          4MHz          150MHz  <= 200MHz
1300 1    4      12.5 - 62.5      75MHz    - 375MHz          6MHz          200MHz  <= 300MHz
1301 1    2      12.5 - 62.5      150MHz   - 750MHz          12MHz         300MHz  <= 600MHz
1302 1    1      12.5 - 62.5      300MHz   - 1500MHz         24MHz         600MHz  <= 1200MHz      
1303 ******************************************/
1304 uint32_t __sramlocalfunc ddr_set_pll(uint32_t nMHz, uint32_t set)
1305 {
1306     uint32_t ret = 0;
1307     int delay = 1000;
1308     uint32_t pll_id=1;  //DPLL
1309     //NOÒ»¶¨ÒªÅ¼Êý,NR¾¡Á¿Ð¡£¬jitter¾Í»áС
1310     
1311     if(nMHz == 24)
1312     {
1313         ret = 24;
1314         goto out;
1315     }
1316     
1317     if(!set)
1318     {
1319         if(nMHz <= 150)
1320         {
1321             clkod = 8;
1322         }
1323         else if(nMHz <= 200)
1324         {
1325             clkod = 6;
1326         }
1327         else if(nMHz <= 300)
1328         {
1329             clkod = 4;
1330         }
1331         else if(nMHz <= 600)
1332         {
1333             clkod = 2;
1334         }
1335         else
1336         {
1337             clkod = 1;
1338         }
1339         clkr = 1;
1340         clkf=(nMHz*clkr*clkod)/24;
1341         ret = (24*clkf)/(clkr*clkod);
1342     }
1343     else
1344     {
1345         pCRU_Reg->CRU_MODE_CON = (0x3<<((pll_id*4) +  16)) | (0x0<<(pll_id*4));            //PLL slow-mode
1346     
1347         pCRU_Reg->CRU_PLL_CON[pll_id][3] = PLL_RESET;
1348         pCRU_Reg->CRU_PLL_CON[pll_id][0] = NR(clkr) | NO(clkod);
1349         pCRU_Reg->CRU_PLL_CON[pll_id][1] = NF(clkf);
1350         pCRU_Reg->CRU_PLL_CON[pll_id][2] = NB(clkf>>1);
1351         ddr_delayus(1);
1352         pCRU_Reg->CRU_PLL_CON[pll_id][3] = PLL_DE_RESET;
1353
1354         while (delay > 0) 
1355         {
1356             ddr_delayus(1);
1357                 if (pGRF_Reg->GRF_SOC_STATUS0 & (0x1<<4))
1358                         break;
1359                 delay--;
1360         }
1361         
1362         pCRU_Reg->CRU_CLKSEL_CON[26] = ((0x3 | (0x1<<8))<<16)
1363                                                   | (0x0<<8)     //clk_ddr_src = DDR PLL
1364                                                   | 0;           //clk_ddr_src:clk_ddrphy = 1:1
1365         
1366         pCRU_Reg->CRU_MODE_CON = (0x3<<((pll_id*4) +  16))  | (0x1<<(pll_id*4));            //PLL normal
1367     }
1368 out:
1369     return ret;
1370 }
1371
1372 uint32_t ddr_get_parameter(uint32_t nMHz)
1373 {
1374     uint32_t tmp;
1375     uint32_t ret = 0;
1376     uint32_t al;
1377     uint32_t bl;
1378     uint32_t cl;
1379     uint32_t cwl;
1380     PCTL_TIMING_T *p_pctl_timing=&(ddr_reg.pctl.pctl_timing);
1381     PHY_TIMING_T  *p_publ_timing=&(ddr_reg.publ.phy_timing);
1382     NOC_TIMING_T  *p_noc_timing=&(ddr_reg.noc_timing);
1383
1384     p_pctl_timing->togcnt1u = nMHz;
1385     p_pctl_timing->togcnt100n = nMHz/10;
1386     p_pctl_timing->tinit = 200;
1387     p_pctl_timing->trsth = 500;
1388
1389     if(mem_type == DDR3)
1390     {
1391         if(ddr_speed_bin > DDR3_DEFAULT){
1392             ret = -1;
1393             goto out;
1394         }
1395
1396         #define DDR3_tREFI_7_8_us    (78)
1397         #define DDR3_tMRD            (4)
1398         #define DDR3_tRFC_512Mb      (90)
1399         #define DDR3_tRFC_1Gb        (110)
1400         #define DDR3_tRFC_2Gb        (160)
1401         #define DDR3_tRFC_4Gb        (300)
1402         #define DDR3_tRFC_8Gb        (350)
1403         #define DDR3_tRTW            (2)   //register min valid value
1404         #define DDR3_tRAS            (37)
1405         #define DDR3_tRRD            (10)
1406         #define DDR3_tRTP            (7)
1407         #define DDR3_tWR             (15)
1408         #define DDR3_tWTR            (7)
1409         #define DDR3_tXP             (7)
1410         #define DDR3_tXPDLL          (24)
1411         #define DDR3_tZQCS           (80)
1412         #define DDR3_tZQCSI          (10000)
1413         #define DDR3_tDQS            (1)
1414         #define DDR3_tCKSRE          (10)
1415         #define DDR3_tCKE_400MHz     (7)
1416         #define DDR3_tCKE_533MHz     (6)
1417         #define DDR3_tMOD            (15)
1418         #define DDR3_tRSTL           (100)
1419         #define DDR3_tZQCL           (320)
1420         #define DDR3_tDLLK           (512)
1421
1422         al = 0;
1423         bl = 8;
1424         if(nMHz <= 330)
1425         {
1426             tmp = 0;
1427         }
1428         else if(nMHz<=400)
1429         {
1430             tmp = 1;
1431         }
1432         else if(nMHz<=533)
1433         {
1434             tmp = 2;
1435         }
1436         else //666MHz
1437         {
1438             tmp = 3;
1439         }
1440         cl = ddr3_cl_cwl[ddr_speed_bin][tmp] >> 16;
1441         cwl = ddr3_cl_cwl[ddr_speed_bin][tmp] & 0x0ff;
1442         if(cl == 0)
1443             ret = -4;
1444         if(nMHz <= DDR3_DDR2_ODT_DISABLE_FREQ)
1445         {
1446             p_publ_timing->mr[1] = DDR3_DS_40 | DDR3_Rtt_Nom_DIS;
1447         }
1448         else
1449         {
1450             p_publ_timing->mr[1] = DDR3_DS_40 | DDR3_Rtt_Nom_120;
1451         }
1452         p_publ_timing->mr[2] = DDR3_MR2_CWL(cwl) /* | DDR3_Rtt_WR_60 */;
1453         p_publ_timing->mr[3] = 0;
1454         /**************************************************
1455          * PCTL Timing
1456          **************************************************/
1457         /*
1458          * tREFI, average periodic refresh interval, 7.8us
1459          */
1460         p_pctl_timing->trefi = DDR3_tREFI_7_8_us;
1461         /*
1462          * tMRD, 4 tCK
1463          */
1464         p_pctl_timing->tmrd = DDR3_tMRD & 0x7;
1465         p_publ_timing->dtpr0.b.tMRD = DDR3_tMRD-4;
1466         /*
1467          * tRFC, 90ns(512Mb),110ns(1Gb),160ns(2Gb),300ns(4Gb),350ns(8Gb)
1468          */
1469         if(ddr_capability_per_die <= 0x4000000)         // 512Mb 90ns
1470         {
1471             tmp = DDR3_tRFC_512Mb;
1472         }
1473         else if(ddr_capability_per_die <= 0x8000000)    // 1Gb 110ns
1474         {
1475             tmp = DDR3_tRFC_1Gb;
1476         }
1477         else if(ddr_capability_per_die <= 0x10000000)   // 2Gb 160ns
1478         {
1479             tmp = DDR3_tRFC_2Gb;
1480         }
1481         else if(ddr_capability_per_die <= 0x20000000)   // 4Gb 300ns
1482         {
1483             tmp = DDR3_tRFC_4Gb;
1484         }
1485         else    // 8Gb  350ns
1486         {
1487             tmp = DDR3_tRFC_8Gb;
1488         }
1489         p_pctl_timing->trfc = (tmp*nMHz+999)/1000;
1490         p_publ_timing->dtpr1.b.tRFC = ((tmp*nMHz+999)/1000)&0xFF;
1491         /*
1492          * tXSR, =tDLLK=512 tCK
1493          */
1494         p_pctl_timing->texsr = DDR3_tDLLK;
1495         p_publ_timing->dtpr2.b.tXS = DDR3_tDLLK;
1496         /*
1497          * tRP=CL
1498          */
1499         p_pctl_timing->trp = cl;
1500         p_publ_timing->dtpr0.b.tRP = cl;
1501         /*
1502          * WrToMiss=WL*tCK + tWR + tRP + tRCD
1503          */
1504         p_noc_timing->b.WrToMiss = ((cwl+((DDR3_tWR*nMHz+999)/1000)+cl+cl)&0x3F);
1505         /*
1506          * tRC=tRAS+tRP
1507          */
1508         p_pctl_timing->trc = ((((ddr3_tRC_tFAW[ddr_speed_bin]>>16)*nMHz+999)/1000)&0x3F);
1509         p_noc_timing->b.ActToAct = ((((ddr3_tRC_tFAW[ddr_speed_bin]>>16)*nMHz+999)/1000)&0x3F);
1510         p_publ_timing->dtpr0.b.tRC = (((ddr3_tRC_tFAW[ddr_speed_bin]>>16)*nMHz+999)/1000)&0xF;
1511
1512         p_pctl_timing->trtw = (cl+2-cwl);//DDR3_tRTW;
1513         p_publ_timing->dtpr1.b.tRTW = 0;
1514         p_noc_timing->b.RdToWr = ((cl+2-cwl)&0x1F);
1515         p_pctl_timing->tal = al;
1516         p_pctl_timing->tcl = cl;
1517         p_pctl_timing->tcwl = cwl;
1518         /*
1519          * tRAS, 37.5ns(400MHz)     37.5ns(533MHz)
1520          */
1521         p_pctl_timing->tras = (((DDR3_tRAS*nMHz+(nMHz>>1)+999)/1000)&0x3F);
1522         p_publ_timing->dtpr0.b.tRAS = ((DDR3_tRAS*nMHz+(nMHz>>1)+999)/1000)&0x1F;
1523         /*
1524          * tRCD=CL
1525          */
1526         p_pctl_timing->trcd = cl;
1527         p_publ_timing->dtpr0.b.tRCD = cl;
1528         /*
1529          * tRRD = max(4nCK, 7.5ns), DDR3-1066(1K), DDR3-1333(2K), DDR3-1600(2K)
1530          *        max(4nCK, 10ns), DDR3-800(1K,2K), DDR3-1066(2K)
1531          *        max(4nCK, 6ns), DDR3-1333(1K), DDR3-1600(1K)
1532          *
1533          */
1534         tmp = ((DDR3_tRRD*nMHz+999)/1000);
1535         if(tmp < 4)
1536         {
1537             tmp = 4;
1538         }
1539         p_pctl_timing->trrd = (tmp&0xF);
1540         p_publ_timing->dtpr0.b.tRRD = tmp&0xF;
1541         /*
1542          * tRTP, max(4 tCK,7.5ns)
1543          */
1544         tmp = ((DDR3_tRTP*nMHz+(nMHz>>1)+999)/1000);
1545         if(tmp < 4)
1546         {
1547             tmp = 4;
1548         }
1549         p_pctl_timing->trtp = tmp&0xF;
1550         p_publ_timing->dtpr0.b.tRTP = tmp;
1551         /*
1552          * RdToMiss=tRTP+tRP + tRCD - (BL/2 * tCK)
1553          */
1554         p_noc_timing->b.RdToMiss = ((tmp+cl+cl-(bl>>1))&0x3F);
1555         /*
1556          * tWR, 15ns
1557          */
1558         tmp = ((DDR3_tWR*nMHz+999)/1000);
1559         p_pctl_timing->twr = tmp&0x1F;
1560         if(tmp<9)
1561             tmp = tmp - 4;
1562         else
1563             tmp = tmp>>1;
1564         p_publ_timing->mr[0] = DDR3_BL8 | DDR3_CL(cl) | DDR3_WR(tmp);
1565
1566         /*
1567          * tWTR, max(4 tCK,7.5ns)
1568          */
1569         tmp = ((DDR3_tWTR*nMHz+(nMHz>>1)+999)/1000);
1570         if(tmp < 4)
1571         {
1572             tmp = 4;
1573         }
1574         p_pctl_timing->twtr = tmp&0xF;
1575         p_publ_timing->dtpr0.b.tWTR = tmp&0x7;
1576         p_noc_timing->b.WrToRd = ((tmp+cwl)&0x1F);
1577         /*
1578          * tXP, max(3 tCK, 7.5ns)(<933MHz)
1579          */
1580         tmp = ((DDR3_tXP*nMHz+(nMHz>>1)+999)/1000);
1581         if(tmp < 3)
1582         {
1583             tmp = 3;
1584         }
1585         p_pctl_timing->txp = tmp&0x7;
1586         p_publ_timing->dtpr2.b.tXP = tmp&0x1F;
1587         /*
1588          * tXPDLL, max(10 tCK,24ns)
1589          */
1590         tmp = ((DDR3_tXPDLL*nMHz+999)/1000);
1591         if(tmp < 10)
1592         {
1593             tmp = 10;
1594         }
1595         p_pctl_timing->txpdll = tmp & 0x3F;
1596         /*
1597          * tZQCS, max(64 tCK, 80ns)
1598          */
1599         tmp = ((DDR3_tZQCS*nMHz+999)/1000);
1600         if(tmp < 64)
1601         {
1602             tmp = 64;
1603         }
1604         p_pctl_timing->tzqcs = tmp&0x7F;
1605         /*
1606          * tZQCSI,
1607          */
1608         p_pctl_timing->tzqcsi = DDR3_tZQCSI;
1609         /*
1610          * tDQS,
1611          */
1612         p_pctl_timing->tdqs = DDR3_tDQS;
1613         /*
1614          * tCKSRE, max(5 tCK, 10ns)
1615          */
1616         tmp = ((DDR3_tCKSRE*nMHz+999)/1000);
1617         if(tmp < 5)
1618         {
1619             tmp = 5;
1620         }
1621         p_pctl_timing->tcksre = tmp & 0x1F;
1622         /*
1623          * tCKSRX, max(5 tCK, 10ns)
1624          */
1625         p_pctl_timing->tcksrx = tmp & 0x1F;
1626         /*
1627          * tCKE, max(3 tCK,7.5ns)(400MHz) max(3 tCK,5.625ns)(533MHz)
1628          */
1629         if(nMHz>=533)
1630         {
1631             tmp = ((DDR3_tCKE_533MHz*nMHz+999)/1000);
1632         }
1633         else
1634         {
1635             tmp = ((DDR3_tCKE_400MHz*nMHz+(nMHz>>1)+999)/1000);
1636         }
1637         if(tmp < 3)
1638         {
1639             tmp = 3;
1640         }
1641         p_pctl_timing->tcke = tmp & 0x7;
1642         p_publ_timing->dtpr2.b.tCKE = tmp;
1643         /*
1644          * tCKESR, =tCKE + 1tCK
1645          */
1646         p_pctl_timing->tckesr = (tmp+1)&0xF;
1647         /*
1648          * tMOD, max(12 tCK,15ns)
1649          */
1650         tmp = ((DDR3_tMOD*nMHz+999)/1000);
1651         if(tmp < 12)
1652         {
1653             tmp = 12;
1654         }
1655         p_pctl_timing->tmod = tmp&0x1F;
1656         p_publ_timing->dtpr1.b.tMOD = tmp;
1657         /*
1658          * tRSTL, 100ns
1659          */
1660         p_pctl_timing->trstl = ((DDR3_tRSTL*nMHz+999)/1000)&0x7F;
1661         /*
1662          * tZQCL, max(256 tCK, 320ns)
1663          */
1664         tmp = ((DDR3_tZQCL*nMHz+999)/1000);
1665         if(tmp < 256)
1666         {
1667             tmp = 256;
1668         }
1669         p_pctl_timing->tzqcl = tmp&0x3FF;
1670         /*
1671          * tMRR, 0 tCK
1672          */
1673         p_pctl_timing->tmrr = 0;
1674         /*
1675          * tDPD, 0
1676          */
1677         p_pctl_timing->tdpd = 0;
1678
1679         /**************************************************
1680          * PHY Timing
1681          **************************************************/
1682         /*
1683          * tCCD, BL/2 for DDR2 and 4 for DDR3
1684          */
1685         p_publ_timing->dtpr0.b.tCCD = 0;
1686         /*
1687          * tDQSCKmax,5.5ns
1688          */
1689         p_publ_timing->dtpr1.b.tDQSCKmax = 0;
1690         /*
1691          * tRTODT, 0:ODT may be turned on immediately after read post-amble
1692          *         1:ODT may not be turned on until one clock after the read post-amble
1693          */
1694         p_publ_timing->dtpr1.b.tRTODT = 1;
1695         /*
1696          * tFAW,40ns(400MHz 1KB page) 37.5ns(533MHz 1KB page) 50ns(400MHz 2KB page)   50ns(533MHz 2KB page)
1697          */
1698         p_publ_timing->dtpr1.b.tFAW = (((ddr3_tRC_tFAW[ddr_speed_bin]&0x0ff)*nMHz+999)/1000)&0x7F;
1699         /*
1700          * tAOND_tAOFD
1701          */
1702         p_publ_timing->dtpr1.b.tAOND = 0;
1703         /*
1704          * tDLLK,512 tCK
1705          */
1706         p_publ_timing->dtpr2.b.tDLLK = DDR3_tDLLK;
1707         /**************************************************
1708          * NOC Timing
1709          **************************************************/
1710         p_noc_timing->b.BurstLen = ((bl>>1)&0x7);
1711     }
1712     else if(mem_type == LPDDR2)
1713     {
1714         #define LPDDR2_tREFI_3_9_us    (38)
1715         #define LPDDR2_tREFI_7_8_us    (78)
1716         #define LPDDR2_tMRD            (5)  //tCK
1717         #define LPDDR2_tRFC_8Gb        (210)  //ns
1718         #define LPDDR2_tRFC_4Gb        (130)  //ns
1719         #define LPDDR2_tRP_4_BANK               (24)  //ns
1720         #define LPDDR2_tRPab_SUB_tRPpb_4_BANK   (0)
1721         #define LPDDR2_tRP_8_BANK               (27)  //ns
1722         #define LPDDR2_tRPab_SUB_tRPpb_8_BANK   (3)
1723         #define LPDDR2_tRTW          (1)   //tCK register min valid value
1724         #define LPDDR2_tRAS          (42)  //ns
1725         #define LPDDR2_tRCD          (24)  //ns
1726         #define LPDDR2_tRRD          (10)  //ns
1727         #define LPDDR2_tRTP          (7)   //ns
1728         #define LPDDR2_tWR           (15)  //ns
1729         #define LPDDR2_tWTR_GREAT_200MHz         (7)  //ns
1730         #define LPDDR2_tWTR_LITTLE_200MHz        (10) //ns
1731         #define LPDDR2_tXP           (7)  //ns
1732         #define LPDDR2_tXPDLL        (0)
1733         #define LPDDR2_tZQCS         (90) //ns
1734         #define LPDDR2_tZQCSI        (10000)
1735         #define LPDDR2_tDQS          (1)
1736         #define LPDDR2_tCKSRE        (1)  //tCK
1737         #define LPDDR2_tCKSRX        (2)  //tCK
1738         #define LPDDR2_tCKE          (3)  //tCK
1739         #define LPDDR2_tMOD          (0)
1740         #define LPDDR2_tRSTL         (0)
1741         #define LPDDR2_tZQCL         (360)  //ns
1742         #define LPDDR2_tMRR          (2)    //tCK
1743         #define LPDDR2_tCKESR        (15)   //ns
1744         #define LPDDR2_tDPD_US       (500)
1745         #define LPDDR2_tFAW_GREAT_200MHz    (50)  //ns
1746         #define LPDDR2_tFAW_LITTLE_200MHz   (60)  //ns
1747         #define LPDDR2_tDLLK         (2)  //tCK
1748         #define LPDDR2_tDQSCK_MAX    (3)  //tCK
1749         #define LPDDR2_tDQSCK_MIN    (0)  //tCK
1750         #define LPDDR2_tDQSS         (1)  //tCK
1751
1752         al = 0;
1753         bl = 8;
1754         /*     1066 933 800 667 533 400 333
1755          * RL,   8   7   6   5   4   3   3
1756          * WL,   4   4   3   2   2   1   1
1757          */
1758         if(nMHz<=200)
1759         {
1760             cl = 3;
1761             cwl = 1;
1762             p_publ_timing->mr[2] = LPDDR2_RL3_WL1;
1763         }
1764         else if(nMHz<=266)
1765         {
1766             cl = 4;
1767             cwl = 2;
1768             p_publ_timing->mr[2] = LPDDR2_RL4_WL2;
1769         }
1770         else if(nMHz<=333)
1771         {
1772             cl = 5;
1773             cwl = 2;
1774             p_publ_timing->mr[2] = LPDDR2_RL5_WL2;
1775         }
1776         else if(nMHz<=400)
1777         {
1778             cl = 6;
1779             cwl = 3;
1780             p_publ_timing->mr[2] = LPDDR2_RL6_WL3;
1781         }
1782         else if(nMHz<=466)
1783         {
1784             cl = 7;
1785             cwl = 4;
1786             p_publ_timing->mr[2] = LPDDR2_RL7_WL4;
1787         }
1788         else //(nMHz<=1066)
1789         {
1790             cl = 8;
1791             cwl = 4;
1792             p_publ_timing->mr[2] = LPDDR2_RL8_WL4;
1793         }
1794         p_publ_timing->mr[3] = LPDDR2_DS_34;
1795         p_publ_timing->mr[0] = 0;
1796         /**************************************************
1797          * PCTL Timing
1798          **************************************************/
1799         /*
1800          * tREFI, average periodic refresh interval, 15.6us(<256Mb) 7.8us(256Mb-1Gb) 3.9us(2Gb-8Gb)
1801          */
1802         if(ddr_capability_per_die >= 0x10000000)   // 2Gb
1803         {
1804             p_pctl_timing->trefi = LPDDR2_tREFI_3_9_us;
1805         }
1806         else
1807         {
1808             p_pctl_timing->trefi = LPDDR2_tREFI_7_8_us;
1809         }
1810
1811         /*
1812          * tMRD, (=tMRW), 5 tCK
1813          */
1814         p_pctl_timing->tmrd = LPDDR2_tMRD & 0x7;
1815         p_publ_timing->dtpr0.b.tMRD = 3;
1816         /*
1817          * tRFC, 90ns(<=512Mb) 130ns(1Gb-4Gb) 210ns(8Gb)
1818          */
1819         if(ddr_capability_per_die >= 0x40000000)   // 8Gb
1820         {
1821             p_pctl_timing->trfc = (LPDDR2_tRFC_8Gb*nMHz+999)/1000;
1822             /*
1823              * tXSR, tRFC+10ns
1824              */
1825             p_pctl_timing->texsr = (((LPDDR2_tRFC_8Gb+10)*nMHz+999)/1000)&0x3FF;
1826             p_publ_timing->dtpr1.b.tRFC = ((LPDDR2_tRFC_8Gb*nMHz+999)/1000)&0xFF;
1827             p_publ_timing->dtpr2.b.tXS = (((LPDDR2_tRFC_8Gb+10)*nMHz+999)/1000)&0x3FF;
1828         }
1829         else
1830         {
1831             p_pctl_timing->trfc = (LPDDR2_tRFC_4Gb*nMHz+999)/1000;
1832             p_pctl_timing->texsr = (((LPDDR2_tRFC_4Gb+10)*nMHz+999)/1000)&0x3FF;
1833             p_publ_timing->dtpr1.b.tRFC = ((LPDDR2_tRFC_4Gb*nMHz+999)/1000)&0xFF;
1834             p_publ_timing->dtpr2.b.tXS = (((LPDDR2_tRFC_4Gb+10)*nMHz+999)/1000)&0x3FF;
1835         }
1836
1837         /*
1838          * tRP, 4-bank:15ns(Fast) 18ns(Typ) 24ns(Slow), 8-bank:18ns(Fast) 21ns(Typ) 27ns(Slow)
1839          */
1840         if(pPHY_Reg->DCR.b.DDR8BNK)
1841         {
1842             p_pctl_timing->trp = ((((LPDDR2_tRPab_SUB_tRPpb_8_BANK*nMHz+999)/1000) & 0x3)<<16) | (((LPDDR2_tRP_8_BANK*nMHz+999)/1000)&0xF);
1843             p_publ_timing->dtpr0.b.tRP = ((LPDDR2_tRP_8_BANK*nMHz+999)/1000);
1844             /*
1845              * WrToMiss=WL*tCK + tDQSS + tWR + tRP + tRCD
1846              */
1847             p_noc_timing->b.WrToMiss = ((cwl+LPDDR2_tDQSS+(((LPDDR2_tWR+LPDDR2_tRP_8_BANK+LPDDR2_tRCD)*nMHz+999)/1000))&0x3F);
1848             /*
1849              * RdToMiss=tRTP + tRP + tRCD - (BL/2 * tCK)
1850              */
1851             p_noc_timing->b.RdToMiss = (((((LPDDR2_tRTP+LPDDR2_tRP_8_BANK+LPDDR2_tRCD)*nMHz+(nMHz>>1)+999)/1000)-(bl>>1))&0x3F);
1852             /*
1853              * tRC=tRAS+tRP
1854              */
1855             p_pctl_timing->trc = ((((LPDDR2_tRP_8_BANK+LPDDR2_tRAS)*nMHz+999)/1000)&0x3F);
1856             p_noc_timing->b.ActToAct = ((((LPDDR2_tRP_8_BANK+LPDDR2_tRAS)*nMHz+999)/1000)&0x3F);
1857             p_publ_timing->dtpr0.b.tRC = (((LPDDR2_tRP_8_BANK+LPDDR2_tRAS)*nMHz+999)/1000)&0xF;
1858         }
1859         else
1860         {
1861             p_pctl_timing->trp = (LPDDR2_tRPab_SUB_tRPpb_4_BANK<<16) | (((LPDDR2_tRP_4_BANK*nMHz+999)/1000)&0xF);
1862             p_publ_timing->dtpr0.b.tRP = ((LPDDR2_tRP_4_BANK*nMHz+999)/1000);
1863             p_noc_timing->b.WrToMiss = ((cwl+LPDDR2_tDQSS+(((LPDDR2_tWR+LPDDR2_tRP_4_BANK+LPDDR2_tRCD)*nMHz+999)/1000))&0x3F);
1864             p_noc_timing->b.RdToMiss = (((((LPDDR2_tRTP+LPDDR2_tRP_4_BANK+LPDDR2_tRCD)*nMHz+(nMHz>>1)+999)/1000)-(bl>>1))&0x3F);
1865             p_pctl_timing->trc = ((((LPDDR2_tRP_4_BANK+LPDDR2_tRAS)*nMHz+999)/1000)&0x3F);
1866             p_noc_timing->b.ActToAct = ((((LPDDR2_tRP_4_BANK+LPDDR2_tRAS)*nMHz+999)/1000)&0x3F);
1867             p_publ_timing->dtpr0.b.tRC = (((LPDDR2_tRP_4_BANK+LPDDR2_tRAS)*nMHz+999)/1000)&0xF;
1868         }
1869
1870         p_pctl_timing->trtw = (cl+LPDDR2_tDQSCK_MAX+(bl/2)+1-cwl);//LPDDR2_tRTW;
1871         p_publ_timing->dtpr1.b.tRTW = 0;
1872         /*
1873          * RdToWr=RL+tDQSCK-WL
1874          */
1875         p_noc_timing->b.RdToWr = ((cl+LPDDR2_tDQSCK_MAX+1-cwl)&0x1F);
1876         p_pctl_timing->tal = al;
1877         p_pctl_timing->tcl = cl;
1878         p_pctl_timing->tcwl = cwl;
1879         /*
1880          * tRAS, 42ns
1881          */
1882         p_pctl_timing->tras = (((LPDDR2_tRAS*nMHz+999)/1000)&0x3F);
1883         p_publ_timing->dtpr0.b.tRAS = ((LPDDR2_tRAS*nMHz+999)/1000)&0x1F;
1884         /*
1885          * tRCD, 15ns(Fast) 18ns(Typ) 24ns(Slow)
1886          */
1887         p_pctl_timing->trcd = (((LPDDR2_tRCD*nMHz+999)/1000)&0xF);
1888         p_publ_timing->dtpr0.b.tRCD = ((LPDDR2_tRCD*nMHz+999)/1000)&0xF;
1889         /*
1890          * tRRD, 10ns
1891          */
1892         p_pctl_timing->trrd = (((LPDDR2_tRRD*nMHz+999)/1000)&0xF);
1893         p_publ_timing->dtpr0.b.tRRD = ((LPDDR2_tRRD*nMHz+999)/1000)&0xF;
1894         /*
1895          * tRTP, 7.5ns
1896          */
1897         tmp = ((LPDDR2_tRTP*nMHz+(nMHz>>1)+999)/1000);
1898         p_pctl_timing->trtp = tmp&0xF;
1899         p_publ_timing->dtpr0.b.tRTP = (tmp<2) ? 2 : tmp;
1900         /*
1901          * tWR, 15ns
1902          */
1903         p_pctl_timing->twr = ((LPDDR2_tWR*nMHz+999)/1000)&0x1F;
1904         p_publ_timing->mr[1] = LPDDR2_BL8 | LPDDR2_nWR(((LPDDR2_tWR*nMHz+999)/1000));
1905         /*
1906          * tWTR, 7.5ns(533-266MHz)  10ns(200-166MHz)
1907          */
1908         if(nMHz > 200)
1909         {
1910             p_pctl_timing->twtr = ((LPDDR2_tWTR_GREAT_200MHz*nMHz+(nMHz>>1)+999)/1000)&0xF;
1911             p_publ_timing->dtpr0.b.tWTR = ((LPDDR2_tWTR_GREAT_200MHz*nMHz+(nMHz>>1)+999)/1000)&0x7;
1912             /*
1913              * WrToRd=WL+tDQSS+tWTR
1914              */
1915             p_noc_timing->b.WrToRd = ((LPDDR2_tDQSS+((LPDDR2_tWTR_GREAT_200MHz*nMHz+(nMHz>>1)+999)/1000)+cwl)&0x1F);
1916         }
1917         else
1918         {
1919             p_pctl_timing->twtr = ((LPDDR2_tWTR_LITTLE_200MHz*nMHz+999)/1000)&0xF;
1920             p_publ_timing->dtpr0.b.tWTR = ((LPDDR2_tWTR_LITTLE_200MHz*nMHz+999)/1000)&0x7;
1921             p_noc_timing->b.WrToRd = ((LPDDR2_tDQSS+((LPDDR2_tWTR_LITTLE_200MHz*nMHz+999)/1000)+cwl)&0x1F);
1922         }
1923         /*
1924          * tXP, 7.5ns
1925          */
1926         p_pctl_timing->txp = ((LPDDR2_tXP*nMHz+(nMHz>>1)+999)/1000)&0x7;
1927         p_publ_timing->dtpr2.b.tXP = ((LPDDR2_tXP*nMHz+(nMHz>>1)+999)/1000)&0x1F;
1928         /*
1929          * tXPDLL, 0ns
1930          */
1931         p_pctl_timing->txpdll = LPDDR2_tXPDLL;
1932         /*
1933          * tZQCS, 90ns
1934          */
1935         p_pctl_timing->tzqcs = ((LPDDR2_tZQCS*nMHz+999)/1000)&0x7F;
1936         /*
1937          * tZQCSI,
1938          */
1939         if(pDDR_Reg->MCFG &= lpddr2_s4)
1940         {
1941             p_pctl_timing->tzqcsi = LPDDR2_tZQCSI;
1942         }
1943         else
1944         {
1945             p_pctl_timing->tzqcsi = 0;
1946         }
1947         /*
1948          * tDQS,
1949          */
1950         p_pctl_timing->tdqs = LPDDR2_tDQS;
1951         /*
1952          * tCKSRE, 1 tCK
1953          */
1954         p_pctl_timing->tcksre = LPDDR2_tCKSRE;
1955         /*
1956          * tCKSRX, 2 tCK
1957          */
1958         p_pctl_timing->tcksrx = LPDDR2_tCKSRX;
1959         /*
1960          * tCKE, 3 tCK
1961          */
1962         p_pctl_timing->tcke = LPDDR2_tCKE;
1963         p_publ_timing->dtpr2.b.tCKE = LPDDR2_tCKE;
1964         /*
1965          * tMOD, 0 tCK
1966          */
1967         p_pctl_timing->tmod = LPDDR2_tMOD;
1968         p_publ_timing->dtpr1.b.tMOD = LPDDR2_tMOD;
1969         /*
1970          * tRSTL, 0 tCK
1971          */
1972         p_pctl_timing->trstl = LPDDR2_tRSTL;
1973         /*
1974          * tZQCL, 360ns
1975          */
1976         p_pctl_timing->tzqcl = ((LPDDR2_tZQCL*nMHz+999)/1000)&0x3FF;
1977         /*
1978          * tMRR, 2 tCK
1979          */
1980         p_pctl_timing->tmrr = LPDDR2_tMRR;
1981         /*
1982          * tCKESR, 15ns
1983          */
1984         p_pctl_timing->tckesr = ((LPDDR2_tCKESR*nMHz+999)/1000)&0xF;
1985         /*
1986          * tDPD, 500us
1987          */
1988         p_pctl_timing->tdpd = LPDDR2_tDPD_US;
1989
1990         /**************************************************
1991          * PHY Timing
1992          **************************************************/
1993         /*
1994          * tCCD, BL/2 for DDR2 and 4 for DDR3
1995          */
1996         p_publ_timing->dtpr0.b.tCCD = 0;
1997         /*
1998          * tDQSCKmax,5.5ns
1999          */
2000         p_publ_timing->dtpr1.b.tDQSCKmax = LPDDR2_tDQSCK_MAX;
2001         /*
2002          * tDQSCKmin,2.5ns
2003          */
2004         p_publ_timing->dtpr1.b.tDQSCK = LPDDR2_tDQSCK_MIN;
2005         /*
2006          * tRTODT, 0:ODT may be turned on immediately after read post-amble
2007          *         1:ODT may not be turned on until one clock after the read post-amble
2008          */
2009         p_publ_timing->dtpr1.b.tRTODT = 1;
2010         /*
2011          * tFAW,50ns(200-533MHz)  60ns(166MHz)
2012          */
2013         if(nMHz>=200)
2014         {
2015             p_publ_timing->dtpr1.b.tFAW = ((LPDDR2_tFAW_GREAT_200MHz*nMHz+999)/1000)&0x7F;
2016         }
2017         else
2018         {
2019             p_publ_timing->dtpr1.b.tFAW = ((LPDDR2_tFAW_LITTLE_200MHz*nMHz+999)/1000)&0x7F;
2020         }
2021         /*
2022          * tAOND_tAOFD
2023          */
2024         p_publ_timing->dtpr1.b.tAOND = 0;
2025         /*
2026          * tDLLK,0
2027          */
2028         p_publ_timing->dtpr2.b.tDLLK = LPDDR2_tDLLK;
2029         /**************************************************
2030          * NOC Timing
2031          **************************************************/
2032         p_noc_timing->b.BurstLen = ((bl>>1)&0x7);
2033     }
2034     else if(mem_type == DDR2)
2035     {
2036         #define DDR2_tREFI_7_8_us     (78)
2037         #define DDR2_tMRD             (2)
2038         #define DDR2_tRFC_256Mb       (75)
2039         #define DDR2_tRFC_512Mb       (105)
2040         #define DDR2_tRFC_1Gb         (128)
2041         #define DDR2_tRFC_2Gb         (195)
2042         #define DDR2_tRFC_4Gb         (328)
2043         #define DDR2_tRAS             (45)
2044         #define DDR2_tRTW             (2)  //register min valid value
2045         #define DDR2_tRRD             (10)
2046         #define DDR2_tRTP             (7)
2047         #define DDR2_tWR              (15)
2048         #define DDR2_tWTR_LITTLE_200MHz   (10)
2049         #define DDR2_tWTR_GREAT_200MHz    (7)
2050         #define DDR2_tDQS             (1)
2051         #define DDR2_tCKSRE           (1)
2052         #define DDR2_tCKSRX           (1)
2053         #define DDR2_tCKE             (3)
2054         #define DDR2_tCKESR           DDR2_tCKE
2055         #define DDR2_tMOD             (12)
2056         #define DDR2_tFAW_333MHz      (50)
2057         #define DDR2_tFAW_400MHz      (45)
2058         #define DDR2_tDLLK            (200)
2059
2060         al = 0;
2061         bl = 4;
2062         if(nMHz <= 266)
2063         {
2064             cl =  4;
2065         }
2066         else if((nMHz > 266) && (nMHz <= 333))
2067         {
2068             cl =  5;
2069         }
2070         else if((nMHz > 333) && (nMHz <= 400))
2071         {
2072             cl =  6;
2073         }
2074         else // > 400MHz
2075         {
2076             cl =  7;
2077         }
2078         cwl = cl -1;
2079         if(nMHz <= DDR3_DDR2_ODT_DISABLE_FREQ)
2080         {
2081             p_publ_timing->mr[1] = DDR2_STR_REDUCE | DDR2_Rtt_Nom_DIS;
2082         }
2083         else
2084         {
2085             p_publ_timing->mr[1] = DDR2_STR_REDUCE | DDR2_Rtt_Nom_75;
2086         }
2087         p_publ_timing->mr[2] = 0;
2088         p_publ_timing->mr[3] = 0;
2089         /**************************************************
2090          * PCTL Timing
2091          **************************************************/
2092         /*
2093          * tREFI, average periodic refresh interval, 7.8us
2094          */
2095         p_pctl_timing->trefi = DDR2_tREFI_7_8_us;
2096         /*
2097          * tMRD, 2 tCK
2098          */
2099         p_pctl_timing->tmrd = DDR2_tMRD & 0x7;
2100         p_publ_timing->dtpr0.b.tMRD = DDR2_tMRD;
2101         /*
2102          * tRFC, 75ns(256Mb) 105ns(512Mb) 127.5ns(1Gb) 195ns(2Gb) 327.5ns(4Gb)
2103          */
2104         if(ddr_capability_per_die <= 0x2000000)  // 256Mb
2105         {
2106             tmp = DDR2_tRFC_256Mb;
2107         }
2108         else if(ddr_capability_per_die <= 0x4000000) // 512Mb
2109         {
2110             tmp = DDR2_tRFC_512Mb;
2111         }
2112         else if(ddr_capability_per_die <= 0x8000000)  // 1Gb
2113         {
2114             tmp = DDR2_tRFC_1Gb;
2115         }
2116         else if(ddr_capability_per_die <= 0x10000000)  // 2Gb
2117         {
2118             tmp = DDR2_tRFC_2Gb;
2119         }
2120         else  // 4Gb
2121         {
2122             tmp = DDR2_tRFC_4Gb;
2123         }
2124         p_pctl_timing->trfc = (tmp*nMHz+999)/1000;
2125         p_publ_timing->dtpr1.b.tRFC = ((tmp*nMHz+999)/1000)&0xFF;
2126         /*
2127          * tXSR, max(tRFC+10,200 tCK)
2128          */
2129         tmp = (((tmp+10)*nMHz+999)/1000);
2130         if(tmp<200)
2131         {
2132             tmp = 200;
2133         }
2134         p_pctl_timing->texsr = tmp&0x3FF;
2135         p_publ_timing->dtpr2.b.tXS = tmp&0x3FF;
2136         /*
2137          * tRP=CL
2138          */
2139         if(pPHY_Reg->DCR.b.DDR8BNK)
2140         {
2141             p_pctl_timing->trp = (1<<16) | cl;
2142         }
2143         else
2144         {
2145             p_pctl_timing->trp = cl;
2146         }
2147         p_publ_timing->dtpr0.b.tRP = cl;
2148         /*
2149          * WrToMiss=WL*tCK + tWR + tRP + tRCD
2150          */
2151         p_noc_timing->b.WrToMiss = ((cwl+((DDR2_tWR*nMHz+999)/1000)+cl+cl)&0x3F);
2152         /*
2153          * tRAS, 45ns
2154          */
2155         tmp=((DDR2_tRAS*nMHz+999)/1000);
2156         p_pctl_timing->tras = (tmp&0x3F);
2157         p_publ_timing->dtpr0.b.tRAS = tmp&0x1F;
2158         /*
2159          * tRC=tRAS+tRP
2160          */
2161         p_pctl_timing->trc = ((tmp+cl)&0x3F);
2162         p_noc_timing->b.ActToAct = ((tmp+cl)&0x3F);
2163         p_publ_timing->dtpr0.b.tRC = (tmp+cl)&0xF;
2164
2165         p_pctl_timing->trtw = (cl+2-cwl);//DDR2_tRTW;
2166         p_publ_timing->dtpr1.b.tRTW = 0;
2167         p_noc_timing->b.RdToWr = ((cl+2-cwl)&0x1F);
2168         p_pctl_timing->tal = al;
2169         p_pctl_timing->tcl = cl;
2170         p_pctl_timing->tcwl = cwl;
2171         /*
2172          * tRCD=CL
2173          */
2174         p_pctl_timing->trcd = cl;
2175         p_publ_timing->dtpr0.b.tRCD = cl;
2176         /*
2177          * tRRD = 10ns(2KB page)
2178          *
2179          */
2180         p_pctl_timing->trrd = (((DDR2_tRRD*nMHz+999)/1000)&0xF);
2181         p_publ_timing->dtpr0.b.tRRD = ((DDR2_tRRD*nMHz+999)/1000)&0xF;
2182         /*
2183          * tRTP, 7.5ns
2184          */
2185         tmp = ((DDR2_tRTP*nMHz+(nMHz>>1)+999)/1000);
2186         p_pctl_timing->trtp = tmp&0xF;
2187         p_publ_timing->dtpr0.b.tRTP = tmp;
2188         /*
2189          * RdToMiss=tRTP+tRP + tRCD - (BL/2 * tCK)
2190          */
2191         p_noc_timing->b.RdToMiss = ((tmp+cl+cl-(bl>>1))&0x3F);
2192         /*
2193          * tWR, 15ns
2194          */
2195         tmp = ((DDR2_tWR*nMHz+999)/1000);
2196         p_pctl_timing->twr = tmp&0x1F;
2197         p_publ_timing->mr[0] = DDR2_BL4 | DDR2_CL(cl) | DDR2_WR(tmp);
2198         /*
2199          * tWTR, 10ns(200MHz) 7.5ns(>200MHz)
2200          */
2201         if(nMHz<=200)
2202         {
2203             tmp = ((DDR2_tWTR_LITTLE_200MHz*nMHz+999)/1000);
2204         }
2205         else
2206         {
2207             tmp = ((DDR2_tWTR_GREAT_200MHz*nMHz+(nMHz>>1)+999)/1000);
2208         }
2209         p_pctl_timing->twtr = tmp&0xF;
2210         p_publ_timing->dtpr0.b.tWTR = tmp&0x7;
2211         p_noc_timing->b.WrToRd = ((tmp+cwl)&0x1F);
2212         /*
2213          * tXP, 6-AL(200MHz)         6-AL(266MHz)         7-AL(333MHz)         8-AL(400MHz)        10-AL(533MHz)
2214          */
2215         if(nMHz<=266)
2216         {
2217             tmp = 6-al;
2218         }
2219         else if(nMHz<=333)
2220         {
2221             tmp = 7-al;
2222         }
2223         else if(nMHz<=400)
2224         {
2225             tmp = 8-al;
2226         }
2227         else
2228         {
2229             tmp = 10-al;
2230         }
2231         p_pctl_timing->txp = tmp&0x7;
2232         p_publ_timing->dtpr2.b.tXP = tmp&0x1F;
2233         /*
2234          * tXPDLL, =tXP
2235          */
2236         p_pctl_timing->txpdll = tmp & 0x3F;
2237         /*
2238          * tZQCS, 0
2239          */
2240         p_pctl_timing->tzqcs = 0;
2241         /*
2242          * tZQCSI,
2243          */
2244         p_pctl_timing->tzqcsi = 0;
2245         /*
2246          * tDQS,
2247          */
2248         p_pctl_timing->tdqs = DDR2_tDQS;
2249         /*
2250          * tCKSRE, 1 tCK
2251          */
2252         p_pctl_timing->tcksre = DDR2_tCKSRE & 0x1F;
2253         /*
2254          * tCKSRX, no such timing
2255          */
2256         p_pctl_timing->tcksrx = DDR2_tCKSRX & 0x1F;
2257         /*
2258          * tCKE, 3 tCK
2259          */
2260         p_pctl_timing->tcke = DDR2_tCKE & 0x7;
2261         p_publ_timing->dtpr2.b.tCKE = DDR2_tCKE;
2262         /*
2263          * tCKESR, =tCKE
2264          */
2265         p_pctl_timing->tckesr = DDR2_tCKESR&0xF;
2266         /*
2267          * tMOD, 12ns
2268          */
2269         p_pctl_timing->tmod = ((DDR2_tMOD*nMHz+999)/1000)&0x1F;
2270         p_publ_timing->dtpr1.b.tMOD = ((DDR2_tMOD*nMHz+999)/1000);
2271         /*
2272          * tRSTL, 0
2273          */
2274         p_pctl_timing->trstl = 0;
2275         /*
2276          * tZQCL, 0
2277          */
2278         p_pctl_timing->tzqcl = 0;
2279         /*
2280          * tMRR, 0 tCK
2281          */
2282         p_pctl_timing->tmrr = 0;
2283         /*
2284          * tDPD, 0
2285          */
2286         p_pctl_timing->tdpd = 0;
2287
2288         /**************************************************
2289          * PHY Timing
2290          **************************************************/
2291         /*
2292          * tCCD, BL/2 for DDR2 and 4 for DDR3
2293          */
2294         p_publ_timing->dtpr0.b.tCCD = 0;
2295         /*
2296          * tDQSCKmax,5.5ns
2297          */
2298         p_publ_timing->dtpr1.b.tDQSCKmax = 0;
2299         /*
2300          * tRTODT, 0:ODT may be turned on immediately after read post-amble
2301          *         1:ODT may not be turned on until one clock after the read post-amble
2302          */
2303         p_publ_timing->dtpr1.b.tRTODT = 1;
2304         /*
2305          * tFAW,50ns(<=333MHz 2KB page) 45ns(400MHz 2KB page) 45ns(533MHz 2KB page)
2306          */
2307         if(nMHz<=333)
2308         {
2309             tmp = DDR2_tFAW_333MHz;
2310         }
2311         else
2312         {
2313             tmp = DDR2_tFAW_400MHz;
2314         }
2315         p_publ_timing->dtpr1.b.tFAW = ((tmp*nMHz+999)/1000)&0x7F;
2316         /*
2317          * tAOND_tAOFD
2318          */
2319         p_publ_timing->dtpr1.b.tAOND = 0;
2320         /*
2321          * tDLLK,=tXSRD=200 tCK
2322          */
2323         p_publ_timing->dtpr2.b.tDLLK = DDR2_tDLLK;
2324         /**************************************************
2325          * NOC Timing
2326          **************************************************/
2327         p_noc_timing->b.BurstLen = ((bl>>1)&0x7);
2328     }
2329     else //if(mem_type == LPDDR)
2330     {
2331         #define mDDR_tREFI_7_8_us   (78)
2332         #define mDDR_tMRD           (2)
2333         #define mDDR_tRFC_256Mb     (80)
2334         #define mDDR_tRFC_512Mb     (110)
2335         #define mDDR_tRFC_1Gb       (140)
2336         #define mDDR_tXSR           (200)
2337         #define mDDR_tRAS_100MHz    (50)
2338         #define mDDR_tRAS_133MHz    (45)
2339         #define mDDR_tRAS_185MHz    (42)
2340         #define mDDR_tRAS_200MHz    (40)
2341         #define mDDR_tRTW           (3)  //register min valid value
2342         #define mDDR_tRRD_133MHz    (15)
2343         #define mDDR_tRRD_166MHz    (12)
2344         #define mDDR_tRRD_185MHz    (11)
2345         #define mDDR_tRRD_200MHz    (10)
2346         #define mDDR_tRTP           (0)
2347         #define mDDR_tWR            (15)
2348         #define mDDR_tWTR_133MHz    (1)
2349         #define mDDR_tWTR_200MHz    (2)
2350         #define mDDR_tXP            (25)
2351         #define mDDR_tDQS           (1)
2352         #define mDDR_tCKSRE         (1)
2353         #define mDDR_tCKSRX         (1)
2354         #define mDDR_tCKE           (2)
2355
2356         al = 0;
2357         bl = 4;
2358         /*
2359          * mobile DDR timing USE 3-3-3, CL always = 3
2360          */
2361         cl = 3;
2362         cwl = 1;
2363         p_publ_timing->mr[0] = mDDR_BL4 | mDDR_CL(cl);
2364         p_publ_timing->mr[2] = mDDR_DS_3_4;  //mr[2] is mDDR MR1
2365         p_publ_timing->mr[1] = 0;
2366         p_publ_timing->mr[3] = 0;
2367         /**************************************************
2368          * PCTL Timing
2369          **************************************************/
2370         /*
2371          * tREFI, average periodic refresh interval, 7.8us
2372          */
2373         p_pctl_timing->trefi = mDDR_tREFI_7_8_us;
2374         /*
2375          * tMRD, 2 tCK
2376          */
2377         p_pctl_timing->tmrd = mDDR_tMRD & 0x7;
2378         p_publ_timing->dtpr0.b.tMRD = mDDR_tMRD;
2379         /*
2380          * tRFC, 80ns(128Mb,256Mb) 110ns(512Mb) 140ns(1Gb,2Gb)
2381          */
2382         if(ddr_capability_per_die <= 0x2000000)  // 256Mb
2383         {
2384             tmp = mDDR_tRFC_256Mb;
2385         }
2386         else if(ddr_capability_per_die <= 0x4000000) // 512Mb
2387         {
2388             tmp = mDDR_tRFC_512Mb;
2389         }
2390         else  // 1Gb,2Gb
2391         {
2392             tmp = mDDR_tRFC_1Gb;
2393         }
2394         p_pctl_timing->trfc = (tmp*nMHz+999)/1000;
2395         p_publ_timing->dtpr1.b.tRFC = ((tmp*nMHz+999)/1000)&0xFF;
2396         /*
2397          * tCKESR, =tRFC
2398          */
2399         p_pctl_timing->tckesr = tmp&0xF;
2400         /*
2401          * tXSR, 200ns
2402          */
2403         p_pctl_timing->texsr = ((mDDR_tXSR*nMHz+999)/1000)&0x3FF;
2404         p_publ_timing->dtpr2.b.tXS = ((mDDR_tXSR*nMHz+999)/1000)&0x3FF;
2405         /*
2406          * tRP=CL
2407          */
2408         p_pctl_timing->trp = cl;
2409         p_publ_timing->dtpr0.b.tRP = cl;
2410         /*
2411          * WrToMiss=WL*tCK + tWR + tRP + tRCD
2412          */
2413         p_noc_timing->b.WrToMiss = ((cwl+((mDDR_tWR*nMHz+999)/1000)+cl+cl)&0x3F);
2414         /*
2415          * tRAS, 50ns(100MHz) 45ns(133MHz) 42ns(166MHz) 42ns(185MHz) 40ns(200MHz)
2416          */
2417         if(nMHz<=100)
2418         {
2419             tmp = mDDR_tRAS_100MHz;
2420         }
2421         else if(nMHz<=133)
2422         {
2423             tmp = mDDR_tRAS_133MHz;
2424         }
2425         else if(nMHz<=185)
2426         {
2427             tmp =mDDR_tRAS_185MHz;
2428         }
2429         else
2430         {
2431             tmp = mDDR_tRAS_200MHz;
2432         }
2433         tmp = ((tmp*nMHz+999)/1000);
2434         p_pctl_timing->tras = (tmp&0x3F);
2435         p_publ_timing->dtpr0.b.tRAS = tmp&0x1F;
2436         /*
2437          * tRC=tRAS+tRP
2438          */
2439         p_pctl_timing->trc = ((tmp+cl)&0x3F);
2440         p_noc_timing->b.ActToAct = ((tmp+cl)&0x3F);
2441         p_publ_timing->dtpr0.b.tRC = (tmp+cl)&0xF;
2442         p_pctl_timing->trtw = (cl+2-cwl);//mDDR_tRTW;
2443         p_publ_timing->dtpr1.b.tRTW = 0;
2444         p_noc_timing->b.RdToWr = ((cl+2-cwl)&0x1F);
2445         p_pctl_timing->tal = al;
2446         p_pctl_timing->tcl = cl;
2447         p_pctl_timing->tcwl = cwl;
2448         /*
2449          * tRCD=CL
2450          */
2451         p_pctl_timing->trcd = cl;
2452         p_publ_timing->dtpr0.b.tRCD = cl;
2453         /*
2454          * tRRD,15ns(100MHz) 15ns(133MHz) 12ns(166MHz) 10.8ns(185MHz) 10ns(200MHz)
2455          *
2456          */
2457         if(nMHz<=133)
2458         {
2459             tmp = mDDR_tRRD_133MHz;
2460         }
2461         else if(nMHz<=166)
2462         {
2463             tmp = mDDR_tRRD_166MHz;
2464         }
2465         else if(nMHz<=185)
2466         {
2467             tmp = mDDR_tRRD_185MHz;
2468         }
2469         else
2470         {
2471             tmp = mDDR_tRRD_200MHz;
2472         }
2473         p_pctl_timing->trrd = (((tmp*nMHz+999)/1000)&0xF);
2474         p_publ_timing->dtpr0.b.tRRD = ((tmp*nMHz+999)/1000)&0xF;
2475         /*
2476          * tRTP, 0
2477          */
2478         tmp = ((mDDR_tRTP*nMHz+999)/1000);
2479         p_pctl_timing->trtp = tmp&0xF;
2480         p_publ_timing->dtpr0.b.tRTP = tmp;
2481         /*
2482          * RdToMiss=tRTP+tRP + tRCD - (BL/2 * tCK)
2483          */
2484         p_noc_timing->b.RdToMiss = ((tmp+cl+cl-(bl>>1))&0x3F);
2485         /*
2486          * tWR, 15ns
2487          */
2488         p_pctl_timing->twr = ((mDDR_tWR*nMHz+999)/1000)&0x1F;
2489         /*
2490          * tWTR, 1 tCK(100MHz,133MHz) 2 tCK(166MHz,185MHz,200MHz)
2491          */
2492         if(nMHz <= 133)
2493         {
2494             tmp = mDDR_tWTR_133MHz;
2495         }
2496         else
2497         {
2498             tmp = mDDR_tWTR_200MHz;
2499         }
2500         p_pctl_timing->twtr = tmp&0xF;
2501         p_publ_timing->dtpr0.b.tWTR = tmp&0x7;
2502         p_noc_timing->b.WrToRd = ((tmp+cwl)&0x1F);
2503         /*
2504          * tXP, 25ns
2505          */
2506
2507         p_pctl_timing->txp = ((mDDR_tXP*nMHz+999)/1000)&0x7;
2508         p_publ_timing->dtpr2.b.tXP = ((mDDR_tXP*nMHz+999)/1000)&0x1F;
2509         /*
2510          * tXPDLL, 0
2511          */
2512         p_pctl_timing->txpdll = 0;
2513         /*
2514          * tZQCS, 0
2515          */
2516         p_pctl_timing->tzqcs = 0;
2517         /*
2518          * tZQCSI,
2519          */
2520         p_pctl_timing->tzqcsi = 0;
2521         /*
2522          * tDQS,
2523          */
2524         p_pctl_timing->tdqs = mDDR_tDQS;
2525         /*
2526          * tCKSRE, 1 tCK
2527          */
2528         p_pctl_timing->tcksre = mDDR_tCKSRE & 0x1F;
2529         /*
2530          * tCKSRX, no such timing
2531          */
2532         p_pctl_timing->tcksrx = mDDR_tCKSRX & 0x1F;
2533         /*
2534          * tCKE, 2 tCK
2535          */
2536         p_pctl_timing->tcke = mDDR_tCKE & 0x7;
2537         p_publ_timing->dtpr2.b.tCKE = mDDR_tCKE;
2538         /*
2539          * tMOD, 0
2540          */
2541         p_pctl_timing->tmod = 0;
2542         p_publ_timing->dtpr1.b.tMOD = 0;
2543         /*
2544          * tRSTL, 0
2545          */
2546         p_pctl_timing->trstl = 0;
2547         /*
2548          * tZQCL, 0
2549          */
2550         p_pctl_timing->tzqcl = 0;
2551         /*
2552          * tMRR, 0 tCK
2553          */
2554         p_pctl_timing->tmrr = 0;
2555         /*
2556          * tDPD, 0
2557          */
2558         p_pctl_timing->tdpd = 0;
2559
2560         /**************************************************
2561          * PHY Timing
2562          **************************************************/
2563         /*
2564          * tCCD, BL/2 for DDR2 and 4 for DDR3
2565          */
2566         p_publ_timing->dtpr0.b.tCCD = 0;
2567         /*
2568          * tDQSCKmax,5.5ns
2569          */
2570         p_publ_timing->dtpr1.b.tDQSCKmax = 0;
2571         /*
2572          * tRTODT, 0:ODT may be turned on immediately after read post-amble
2573          *         1:ODT may not be turned on until one clock after the read post-amble
2574          */
2575         p_publ_timing->dtpr1.b.tRTODT = 1;
2576         /*
2577          * tFAW,0
2578          */
2579         p_publ_timing->dtpr1.b.tFAW = 0;
2580         /*
2581          * tAOND_tAOFD
2582          */
2583         p_publ_timing->dtpr1.b.tAOND = 0;
2584         /*
2585          * tDLLK,0
2586          */
2587         p_publ_timing->dtpr2.b.tDLLK = 0;
2588         /**************************************************
2589          * NOC Timing
2590          **************************************************/
2591         p_noc_timing->b.BurstLen = ((bl>>1)&0x7);
2592     }
2593 out:
2594     return ret;
2595 }
2596
2597 uint32_t __sramlocalfunc ddr_update_timing(void)
2598 {
2599     uint32_t i;
2600     PCTL_TIMING_T *p_pctl_timing=&(ddr_reg.pctl.pctl_timing);
2601     PHY_TIMING_T  *p_publ_timing=&(ddr_reg.publ.phy_timing);
2602     NOC_TIMING_T  *p_noc_timing=&(ddr_reg.noc_timing);
2603
2604     ddr_copy((uint32_t *)&(pDDR_Reg->TOGCNT1U), (uint32_t*)&(p_pctl_timing->togcnt1u), 34);
2605     ddr_copy((uint32_t *)&(pPHY_Reg->DTPR[0]), (uint32_t*)&(p_publ_timing->dtpr0), 3);
2606     *(volatile uint32_t *)SysSrv_DdrTiming = p_noc_timing->d32;
2607     // Update PCTL BL
2608     if(mem_type == DDR3)
2609     {
2610         pDDR_Reg->MCFG = (pDDR_Reg->MCFG & (~(0x1|(0x3<<18)|(0x1<<17)|(0x1<<16)))) | ddr2_ddr3_bl_8 | tfaw_cfg(5)|pd_exit_slow|pd_type(1);
2611         pDDR_Reg->DFITRDDATAEN   = pDDR_Reg->TCL-2;
2612         pDDR_Reg->DFITPHYWRLAT   = pDDR_Reg->TCWL-1;
2613     }
2614     else if(mem_type == LPDDR2)
2615     {
2616         if(ddr_freq>=200)
2617         {
2618             pDDR_Reg->MCFG = (pDDR_Reg->MCFG & (~((0x3<<20)|(0x3<<18)|(0x1<<17)|(0x1<<16)))) | mddr_lpddr2_bl_8 | tfaw_cfg(5)|pd_exit_fast|pd_type(1);
2619         }
2620         else
2621         {
2622             pDDR_Reg->MCFG = (pDDR_Reg->MCFG & (~((0x3<<20)|(0x3<<18)|(0x1<<17)|(0x1<<16)))) | mddr_lpddr2_bl_8 | tfaw_cfg(6)|pd_exit_fast|pd_type(1);
2623         }
2624         i = ((pPHY_Reg->DTPR[1] >> 27) & 0x7) - ((pPHY_Reg->DTPR[1] >> 24) & 0x7);
2625         pPHY_Reg->DSGCR = (pPHY_Reg->DSGCR & (~(0x3F<<5))) | (i<<5) | (i<<8);  //tDQSCKmax-tDQSCK
2626         pDDR_Reg->DFITRDDATAEN   = pDDR_Reg->TCL-1;
2627         pDDR_Reg->DFITPHYWRLAT   = pDDR_Reg->TCWL;
2628     }
2629     else if(mem_type == DDR2)
2630     {
2631         pDDR_Reg->MCFG = (pDDR_Reg->MCFG & (~(0x1|(0x3<<18)|(0x1<<17)|(0x1<<16)))) | ddr2_ddr3_bl_8 | tfaw_cfg(5)|pd_exit_fast|pd_type(1);
2632     }
2633     else// if(mem_type == LPDDR)
2634     {
2635         pDDR_Reg->MCFG = (pDDR_Reg->MCFG & (~((0x3<<20)|(0x3<<18)|(0x1<<17)|(0x1<<16)))) | mddr_lpddr2_bl_4 | tfaw_cfg(5)|pd_exit_fast|pd_type(1);
2636     }
2637     return 0;
2638 }
2639
2640 uint32_t __sramlocalfunc ddr_update_mr(void)
2641 {
2642     PHY_TIMING_T  *p_publ_timing=&(ddr_reg.publ.phy_timing);
2643     uint32_t cs;
2644
2645     cs = ((pPHY_Reg->PGCR>>18) & 0xF);
2646     ddr_copy((uint32_t *)&(pPHY_Reg->MR[0]), (uint32_t*)&(p_publ_timing->mr[0]), 4);
2647     if((mem_type == DDR3) || (mem_type == DDR2))
2648     {
2649         if(ddr_freq>DDR3_DDR2_DLL_DISABLE_FREQ)
2650         {
2651             if(pPHY_Reg->MR[1] & DDR3_DLL_DISABLE)  // off -> on
2652             {
2653                 ddr_send_command(cs, MRS_cmd, bank_addr(0x1) | cmd_addr((uint8_t)(p_publ_timing->mr[1])));  //DLL enable
2654                 ddr_send_command(cs, MRS_cmd, bank_addr(0x0) | cmd_addr(((uint8_t)(p_publ_timing->mr[0]))| DDR3_DLL_RESET));  //DLL reset
2655                 ddr_delayus(2);  //at least 200 DDR cycle
2656                 ddr_send_command(cs, MRS_cmd, bank_addr(0x0) | cmd_addr((uint8_t)(p_publ_timing->mr[0])));
2657             }
2658             else // on -> on
2659             {
2660                 ddr_send_command(cs, MRS_cmd, bank_addr(0x1) | cmd_addr((uint8_t)(p_publ_timing->mr[1])));
2661                 ddr_send_command(cs, MRS_cmd, bank_addr(0x0) | cmd_addr((uint8_t)(p_publ_timing->mr[0])));
2662             }
2663         }
2664         else
2665         {
2666             pPHY_Reg->MR[1] = (((uint8_t)(p_publ_timing->mr[1])) | DDR3_DLL_DISABLE);
2667             ddr_send_command(cs, MRS_cmd, bank_addr(0x1) | cmd_addr(((uint8_t)(p_publ_timing->mr[1])) | DDR3_DLL_DISABLE));  //DLL disable
2668             ddr_send_command(cs, MRS_cmd, bank_addr(0x0) | cmd_addr((uint8_t)(p_publ_timing->mr[0])));
2669         }
2670         ddr_send_command(cs, MRS_cmd, bank_addr(0x2) | cmd_addr((uint8_t)(p_publ_timing->mr[2])));
2671     }
2672     else if(mem_type == LPDDR2)
2673     {
2674         ddr_send_command(cs, MRS_cmd, lpddr2_ma(0x1) | lpddr2_op((uint8_t)(p_publ_timing->mr[1])));
2675         ddr_send_command(cs, MRS_cmd, lpddr2_ma(0x2) | lpddr2_op((uint8_t)(p_publ_timing->mr[2])));
2676         ddr_send_command(cs, MRS_cmd, lpddr2_ma(0x3) | lpddr2_op((uint8_t)(p_publ_timing->mr[3])));
2677     }
2678     else //mDDR
2679     {
2680         ddr_send_command(cs, MRS_cmd, bank_addr(0x0) | cmd_addr((uint8_t)(p_publ_timing->mr[0])));
2681         ddr_send_command(cs, MRS_cmd, bank_addr(0x1) | cmd_addr((uint8_t)(p_publ_timing->mr[2]))); //mr[2] is mDDR MR1
2682     }
2683     return 0;
2684 }
2685
2686 void __sramlocalfunc ddr_update_odt(void)
2687 {
2688     uint32_t cs,tmp;
2689     
2690     //adjust DRV and ODT
2691     if((mem_type == DDR3) || (mem_type == DDR2))
2692     {
2693         if(ddr_freq <= DDR3_DDR2_ODT_DISABLE_FREQ)
2694         {
2695             pPHY_Reg->DATX8[0].DXGCR &= ~(0x3<<9);  //dynamic RTT disable
2696             pPHY_Reg->DATX8[1].DXGCR &= ~(0x3<<9);
2697             pPHY_Reg->DATX8[2].DXGCR &= ~(0x3<<9);
2698             pPHY_Reg->DATX8[3].DXGCR &= ~(0x3<<9);
2699         }
2700         else
2701         {
2702             pPHY_Reg->DATX8[0].DXGCR |= (0x3<<9);  //dynamic RTT enable
2703             pPHY_Reg->DATX8[1].DXGCR |= (0x3<<9);
2704             pPHY_Reg->DATX8[2].DXGCR |= (0x3<<9);
2705             pPHY_Reg->DATX8[3].DXGCR |= (0x3<<9);
2706         }
2707     }
2708     else
2709     {
2710         pPHY_Reg->DATX8[0].DXGCR &= ~(0x3<<9);  //dynamic RTT disable
2711         pPHY_Reg->DATX8[1].DXGCR &= ~(0x3<<9);
2712         pPHY_Reg->DATX8[2].DXGCR &= ~(0x3<<9);
2713         pPHY_Reg->DATX8[3].DXGCR &= ~(0x3<<9);
2714     }    
2715     tmp = (0x1<<28) | (0x2<<15) | (0x2<<10) | (0xb<<5) | 0xb;  //DS=34ohm,ODT=171ohm
2716     cs = ((pPHY_Reg->PGCR>>18) & 0xF);
2717     if(cs > 1)
2718     {
2719         pPHY_Reg->ZQ1CR[0] = tmp;
2720     }
2721     pPHY_Reg->ZQ0CR[0] = tmp;
2722 }
2723
2724 __sramfunc void ddr_adjust_config(uint32_t dram_type)
2725 {
2726     uint32 value;
2727     unsigned long save_sp;
2728     u32 i;
2729     volatile u32 n; 
2730     volatile unsigned int * temp=(volatile unsigned int *)SRAM_CODE_OFFSET;
2731
2732     //get data training address before idle port
2733     value = ddr_get_datatraing_addr();
2734
2735     /** 1. Make sure there is no host access */
2736     flush_cache_all();
2737     outer_flush_all();
2738     flush_tlb_all();
2739     DDR_SAVE_SP(save_sp);
2740
2741     for(i=0;i<16;i++)
2742     {
2743         n=temp[1024*i];
2744         barrier();
2745     }
2746     n= pDDR_Reg->SCFG.d32;
2747     n= pPHY_Reg->RIDR;
2748     n= pCRU_Reg->CRU_PLL_CON[0][0];
2749     n= pPMU_Reg->PMU_WAKEUP_CFG[0];
2750     n= *(volatile uint32_t *)SysSrv_DdrConf;
2751     dsb();
2752     
2753     //enter config state
2754     ddr_move_to_Config_state();
2755
2756     //extend capability for debug
2757     if(pGRF_Reg->GRF_SOC_CON[2] & (0x1<<1))
2758     {
2759         pGRF_Reg->GRF_SOC_CON[2] = rank_to_row15_en;
2760     }
2761
2762     //set data training address
2763     pPHY_Reg->DTAR = value;
2764
2765     //set auto power down idle
2766     pDDR_Reg->MCFG=(pDDR_Reg->MCFG&0xffff00ff)|(PD_IDLE<<8);
2767
2768     //CKDV=00
2769     pPHY_Reg->PGCR &= ~(0x3<<12);
2770
2771     //enable the hardware low-power interface
2772     pDDR_Reg->SCFG.b.hw_low_power_en = 1;
2773
2774     ddr_update_odt();
2775
2776     //enter access state
2777     ddr_move_to_Access_state();
2778
2779     DDR_RESTORE_SP(save_sp);
2780 }
2781
2782
2783 void __sramlocalfunc idle_port(void)
2784 {
2785     int i;
2786     uint32 clk_gate[10];
2787
2788     //save clock gate status
2789     for(i=0;i<10;i++)
2790         clk_gate[i]=pCRU_Reg->CRU_CLKGATE_CON[i];
2791
2792     //enable all clock gate for request idle
2793     for(i=0;i<10;i++)
2794         pCRU_Reg->CRU_CLKGATE_CON[i]=0xffff0000;
2795
2796     if ( (pPMU_Reg->PMU_PWRDN_ST & pd_a9_0_pwr_st) == 0 )
2797     {
2798         pPMU_Reg->PMU_MISC_CON1 |= idle_req_cpu_cfg;
2799         while( (pPMU_Reg->PMU_PWRDN_ST & idle_cpu) == 0 );
2800     }
2801
2802     if ( (pPMU_Reg->PMU_PWRDN_ST & pd_peri_pwr_st) == 0 )
2803     {
2804         pPMU_Reg->PMU_MISC_CON1 |= idle_req_peri_cfg;
2805         while( (pPMU_Reg->PMU_PWRDN_ST & idle_peri) == 0 );
2806     }
2807
2808     if ( (pPMU_Reg->PMU_PWRDN_ST & pd_vio_pwr_st) == 0 )
2809     {
2810         pPMU_Reg->PMU_MISC_CON1 |= idle_req_vio_cfg;
2811         while( (pPMU_Reg->PMU_PWRDN_ST & idle_vio) == 0 );
2812     }
2813
2814     if ( (pPMU_Reg->PMU_PWRDN_ST & pd_video_pwr_st) == 0 )
2815     {
2816         pPMU_Reg->PMU_MISC_CON1 |= idle_req_video_cfg;
2817         while( (pPMU_Reg->PMU_PWRDN_ST & idle_video) == 0 );
2818     }
2819
2820     if ( (pPMU_Reg->PMU_PWRDN_ST & pd_gpu_pwr_st) == 0 )
2821     {
2822         pPMU_Reg->PMU_MISC_CON1 |= idle_req_gpu_cfg;
2823         while( (pPMU_Reg->PMU_PWRDN_ST & idle_gpu) == 0 );
2824     }
2825
2826         //resume clock gate status
2827     for(i=0;i<10;i++)
2828         pCRU_Reg->CRU_CLKGATE_CON[i]=  (clk_gate[i] | 0xffff0000);
2829 }
2830
2831 void __sramlocalfunc deidle_port(void)
2832 {
2833     int i;
2834     uint32 clk_gate[10];
2835
2836     //save clock gate status
2837     for(i=0;i<10;i++)
2838         clk_gate[i]=pCRU_Reg->CRU_CLKGATE_CON[i];
2839
2840     //enable all clock gate for request idle
2841     for(i=0;i<10;i++)
2842         pCRU_Reg->CRU_CLKGATE_CON[i]=0xffff0000;
2843
2844     if ( (pPMU_Reg->PMU_PWRDN_ST & pd_a9_0_pwr_st) == 0 )
2845     {
2846         pPMU_Reg->PMU_MISC_CON1 &= ~idle_req_cpu_cfg;
2847         while( (pPMU_Reg->PMU_PWRDN_ST & idle_cpu) != 0 );
2848     }
2849     if ( (pPMU_Reg->PMU_PWRDN_ST & pd_peri_pwr_st) == 0 )
2850     {
2851         pPMU_Reg->PMU_MISC_CON1 &= ~idle_req_peri_cfg;
2852         while( (pPMU_Reg->PMU_PWRDN_ST & idle_peri) != 0 );
2853     }
2854
2855     if ( (pPMU_Reg->PMU_PWRDN_ST & pd_vio_pwr_st) == 0 )
2856     {
2857         pPMU_Reg->PMU_MISC_CON1 &= ~idle_req_vio_cfg;
2858         while( (pPMU_Reg->PMU_PWRDN_ST & idle_vio) != 0 );
2859     }
2860
2861     if ( (pPMU_Reg->PMU_PWRDN_ST & pd_video_pwr_st) == 0 )
2862     {
2863         pPMU_Reg->PMU_MISC_CON1 &= ~idle_req_video_cfg;
2864         while( (pPMU_Reg->PMU_PWRDN_ST & idle_video) != 0 );
2865     }
2866
2867     if ( (pPMU_Reg->PMU_PWRDN_ST & pd_gpu_pwr_st) == 0 )
2868     {
2869         pPMU_Reg->PMU_MISC_CON1 &= ~idle_req_gpu_cfg;
2870         while( (pPMU_Reg->PMU_PWRDN_ST & idle_gpu) != 0 );
2871     }
2872
2873     //resume clock gate status
2874     for(i=0;i<10;i++)
2875         pCRU_Reg->CRU_CLKGATE_CON[i]=  (clk_gate[i] | 0xffff0000);
2876
2877 }
2878
2879 void __sramlocalfunc ddr_selfrefresh_enter(uint32 nMHz)
2880 {
2881     PHY_TIMING_T  *p_publ_timing=&(ddr_reg.publ.phy_timing);
2882     uint32 cs;
2883     
2884     ddr_move_to_Config_state();
2885     pDDR_Reg->TZQCSI = 0;
2886     if((nMHz<=DDR3_DDR2_DLL_DISABLE_FREQ) && ((mem_type == DDR3) || (mem_type == DDR2)))  // DLL disable
2887     {
2888         cs = ((pPHY_Reg->PGCR>>18) & 0xF);
2889         pPHY_Reg->MR[1] = (((uint8_t)(p_publ_timing->mr[1])) | DDR3_DLL_DISABLE);
2890         ddr_send_command(cs, MRS_cmd, bank_addr(0x1) | cmd_addr(((uint8_t)(p_publ_timing->mr[1])) | DDR3_DLL_DISABLE));
2891     }
2892     ddr_move_to_Lowpower_state();
2893     
2894     ddr_set_dll_bypass(0);  //dll bypass
2895     pCRU_Reg->CRU_CLKGATE_CON[0] = ((0x1<<2)<<16) | (1<<2);  //disable DDR PHY clock
2896     ddr_delayus(1);
2897 }
2898
2899 void __sramlocalfunc ddr_selfrefresh_exit(void)
2900 {
2901     uint32 n;
2902
2903     pCRU_Reg->CRU_CLKGATE_CON[0] = ((0x1<<2)<<16) | (0<<2);  //enable DDR PHY clock
2904     dsb();
2905     ddr_set_dll_bypass(ddr_freq);    
2906     ddr_reset_dll();
2907     //ddr_delayus(10);   //wait DLL lock
2908
2909     ddr_move_to_Config_state();
2910     ddr_update_timing();
2911     ddr_update_mr();
2912     ddr_update_odt();
2913     n = ddr_data_training();
2914     ddr_move_to_Access_state();
2915     if(n!=0)
2916     {
2917         sram_printascii("DTT failed!\n");
2918     }
2919 }
2920
2921 uint32_t __sramfunc ddr_change_freq(uint32_t nMHz)
2922 {
2923     uint32_t ret;
2924     u32 i;
2925     volatile u32 n;     
2926     unsigned long flags;
2927     volatile unsigned int * temp=(volatile unsigned int *)SRAM_CODE_OFFSET;
2928     unsigned long save_sp;
2929     uint32_t regvalue = pCRU_Reg->CRU_PLL_CON[0][0];
2930     uint32_t freq;
2931
2932      // freq = (Fin/NR)*NF/OD
2933      if((pCRU_Reg->CRU_MODE_CON&3) == 1)             // CPLL Normal mode
2934          freq = 24 *((pCRU_Reg->CRU_PLL_CON[0][1]&0x1fff)+1)    // NF = 2*(CLKF+1)
2935                 /((((regvalue>>8)&0x3f)+1)           // NR = CLKR+1
2936                 *((regvalue&0xF)+1));             // OD = 2^CLKOD
2937      else
2938         freq = 24;
2939         
2940     loops_per_us = LPJ_100MHZ*freq / 1000000;
2941     
2942     ret=ddr_set_pll(nMHz,0);
2943     ddr_get_parameter(ret);
2944
2945     /** 1. Make sure there is no host access */
2946     local_irq_save(flags);
2947         local_fiq_disable();
2948     flush_cache_all();
2949         outer_flush_all();
2950         flush_tlb_all();
2951         DDR_SAVE_SP(save_sp);
2952         for(i=0;i<16;i++)
2953         {
2954             n=temp[1024*i];
2955         barrier();
2956         }
2957     n= pDDR_Reg->SCFG.d32;
2958     n= pPHY_Reg->RIDR;
2959     n= pCRU_Reg->CRU_PLL_CON[0][0];
2960     n= pPMU_Reg->PMU_WAKEUP_CFG[0];
2961     n= *(volatile uint32_t *)SysSrv_DdrConf;
2962     n= pGRF_Reg->GRF_SOC_STATUS0;
2963     dsb();
2964
2965     /** 2. ddr enter self-refresh mode or precharge power-down mode */
2966     idle_port();
2967     ddr_selfrefresh_enter(ret);
2968
2969     /** 3. change frequence  */
2970     ddr_set_pll(ret,1);
2971     ddr_freq = ret;
2972     
2973     /** 5. Issues a Mode Exit command   */
2974     ddr_selfrefresh_exit();
2975     deidle_port();
2976
2977         dsb();
2978     DDR_RESTORE_SP(save_sp);
2979     local_fiq_enable();
2980     local_irq_restore(flags);
2981     clk_set_rate(clk_get(NULL, "ddr_pll"), 0);
2982     return ret;
2983 }
2984
2985 EXPORT_SYMBOL(ddr_change_freq);
2986
2987 void ddr_set_auto_self_refresh(bool en)
2988 {
2989     //set auto self-refresh idle
2990     ddr_sr_idle = en ? SR_IDLE : 0;
2991 }
2992 EXPORT_SYMBOL(ddr_set_auto_self_refresh);
2993
2994 void __sramfunc ddr_suspend(void)
2995 {
2996     u32 i;
2997     volatile u32 n;     
2998     volatile unsigned int * temp=(volatile unsigned int *)SRAM_CODE_OFFSET;
2999     
3000     /** 1. Make sure there is no host access */
3001     flush_cache_all();
3002     outer_flush_all();
3003     flush_tlb_all();
3004
3005     for(i=0;i<16;i++)
3006     {
3007         n=temp[1024*i];
3008         barrier();
3009     }
3010     n= pDDR_Reg->SCFG.d32;
3011     n= pPHY_Reg->RIDR;
3012     n= pCRU_Reg->CRU_PLL_CON[0][0];
3013     n= pPMU_Reg->PMU_WAKEUP_CFG[0];
3014     n= *(volatile uint32_t *)SysSrv_DdrConf;
3015     dsb();
3016     
3017     ddr_selfrefresh_enter(0);
3018
3019     pCRU_Reg->CRU_MODE_CON = (0x3<<((1*4) +  16)) | (0x0<<(1*4));   //PLL slow-mode
3020     dsb();
3021     ddr_delayus(1);    
3022     pCRU_Reg->CRU_PLL_CON[1][3] = ((0x1<<1)<<16) | (0x1<<1);         //PLL power-down
3023     dsb();
3024     ddr_delayus(1);    
3025
3026     pPHY_Reg->DSGCR = pPHY_Reg->DSGCR&(~((0x1<<28)|(0x1<<29)));  //CKOE
3027 }
3028 EXPORT_SYMBOL(ddr_suspend);
3029
3030 void __sramfunc ddr_resume(void)
3031 {
3032     int delay=1000;
3033     pPHY_Reg->DSGCR = pPHY_Reg->DSGCR|((0x1<<28)|(0x1<<29));  //CKOE
3034     dsb();
3035     
3036     pCRU_Reg->CRU_PLL_CON[1][3] = ((0x1<<1)<<16) | (0x0<<1);         //PLL no power-down
3037     dsb();
3038     while (delay > 0) 
3039     {
3040             ddr_delayus(1);
3041                 if (pGRF_Reg->GRF_SOC_STATUS0 & (0x1<<4))
3042                         break;
3043                 delay--;
3044         }
3045     
3046     pCRU_Reg->CRU_MODE_CON = (0x3<<((1*4) +  16))  | (0x1<<(1*4));   //PLL normal
3047     dsb();
3048
3049     ddr_selfrefresh_exit();
3050 }
3051 EXPORT_SYMBOL(ddr_resume);
3052
3053 //»ñÈ¡ÈÝÁ¿£¬·µ»Ø×Ö½ÚÊý
3054 uint32 ddr_get_cap(void)
3055 {
3056     uint32 i;
3057     uint32 row;
3058     uint32 cs;
3059
3060     i = *(volatile uint32*)SysSrv_DdrConf;
3061     switch((pPHY_Reg->PGCR>>18) & 0xF)
3062     {
3063         case 0xF:
3064             cs = 4;
3065         case 7:
3066             cs = 3;
3067             break;
3068         case 3:
3069             cs = 2;
3070             break;
3071         default:
3072             cs = 1;
3073             break;
3074     }
3075     row = ddr_cfg_2_rbc[i].row;
3076     if(pGRF_Reg->GRF_SOC_CON[2] &  (1<<1))
3077     {
3078         row += 1;
3079     }
3080
3081     return (1 << (row+(ddr_cfg_2_rbc[i].col)+(ddr_cfg_2_rbc[i].bank)+2))*cs;
3082 }
3083 EXPORT_SYMBOL(ddr_get_cap);
3084
3085 void ddr_reg_save(void)
3086 {
3087     //PCTLR
3088     ddr_reg.pctl.SCFG = pDDR_Reg->SCFG.d32;
3089     ddr_reg.pctl.CMDTSTATEN = pDDR_Reg->CMDTSTATEN;
3090     ddr_reg.pctl.MCFG1 = pDDR_Reg->MCFG1;
3091     ddr_reg.pctl.MCFG = pDDR_Reg->MCFG;
3092     ddr_reg.pctl.pctl_timing.ddrFreq = ddr_freq;
3093     ddr_reg.pctl.DFITCTRLDELAY = pDDR_Reg->DFITCTRLDELAY;
3094     ddr_reg.pctl.DFIODTCFG = pDDR_Reg->DFIODTCFG;
3095     ddr_reg.pctl.DFIODTCFG1 = pDDR_Reg->DFIODTCFG1;
3096     ddr_reg.pctl.DFIODTRANKMAP = pDDR_Reg->DFIODTRANKMAP;
3097     ddr_reg.pctl.DFITPHYWRDATA = pDDR_Reg->DFITPHYWRDATA;
3098     ddr_reg.pctl.DFITPHYWRLAT = pDDR_Reg->DFITPHYWRLAT;
3099     ddr_reg.pctl.DFITRDDATAEN = pDDR_Reg->DFITRDDATAEN;
3100     ddr_reg.pctl.DFITPHYRDLAT = pDDR_Reg->DFITPHYRDLAT;
3101     ddr_reg.pctl.DFITPHYUPDTYPE0 = pDDR_Reg->DFITPHYUPDTYPE0;
3102     ddr_reg.pctl.DFITPHYUPDTYPE1 = pDDR_Reg->DFITPHYUPDTYPE1;
3103     ddr_reg.pctl.DFITPHYUPDTYPE2 = pDDR_Reg->DFITPHYUPDTYPE2;
3104     ddr_reg.pctl.DFITPHYUPDTYPE3 = pDDR_Reg->DFITPHYUPDTYPE3;
3105     ddr_reg.pctl.DFITCTRLUPDMIN = pDDR_Reg->DFITCTRLUPDMIN;
3106     ddr_reg.pctl.DFITCTRLUPDMAX = pDDR_Reg->DFITCTRLUPDMAX;
3107     ddr_reg.pctl.DFITCTRLUPDDLY = pDDR_Reg->DFITCTRLUPDDLY;
3108     
3109     ddr_reg.pctl.DFIUPDCFG = pDDR_Reg->DFIUPDCFG;
3110     ddr_reg.pctl.DFITREFMSKI = pDDR_Reg->DFITREFMSKI;
3111     ddr_reg.pctl.DFITCTRLUPDI = pDDR_Reg->DFITCTRLUPDI;
3112     ddr_reg.pctl.DFISTCFG0 = pDDR_Reg->DFISTCFG0;
3113     ddr_reg.pctl.DFISTCFG1 = pDDR_Reg->DFISTCFG1;
3114     ddr_reg.pctl.DFITDRAMCLKEN = pDDR_Reg->DFITDRAMCLKEN;
3115     ddr_reg.pctl.DFITDRAMCLKDIS = pDDR_Reg->DFITDRAMCLKDIS;
3116     ddr_reg.pctl.DFISTCFG2 = pDDR_Reg->DFISTCFG2;
3117     ddr_reg.pctl.DFILPCFG0 = pDDR_Reg->DFILPCFG0;
3118
3119     //PUBL
3120     ddr_reg.publ.PIR = pPHY_Reg->PIR;
3121     ddr_reg.publ.PGCR = pPHY_Reg->PGCR;
3122     ddr_reg.publ.DLLGCR = pPHY_Reg->DLLGCR;
3123     ddr_reg.publ.ACDLLCR = pPHY_Reg->ACDLLCR;
3124     ddr_reg.publ.PTR[0] = pPHY_Reg->PTR[0];
3125     ddr_reg.publ.PTR[1] = pPHY_Reg->PTR[1];
3126     ddr_reg.publ.PTR[2] = pPHY_Reg->PTR[2];
3127     ddr_reg.publ.ACIOCR = pPHY_Reg->ACIOCR;
3128     ddr_reg.publ.DXCCR = pPHY_Reg->DXCCR;
3129     ddr_reg.publ.DSGCR = pPHY_Reg->DSGCR;
3130     ddr_reg.publ.DCR = pPHY_Reg->DCR.d32;
3131     ddr_reg.publ.ODTCR = pPHY_Reg->ODTCR;
3132     ddr_reg.publ.DTAR = pPHY_Reg->DTAR;
3133     ddr_reg.publ.ZQ0CR0 = (pPHY_Reg->ZQ0SR[0] & 0x0FFFFFFF) | (0x1<<28);
3134     ddr_reg.publ.ZQ1CR0 = (pPHY_Reg->ZQ1SR[0] & 0x0FFFFFFF) | (0x1<<28);
3135     
3136     ddr_reg.publ.DX0GCR = pPHY_Reg->DATX8[0].DXGCR;
3137     ddr_reg.publ.DX0DLLCR = pPHY_Reg->DATX8[0].DXDLLCR;
3138     ddr_reg.publ.DX0DQTR = pPHY_Reg->DATX8[0].DXDQTR;
3139     ddr_reg.publ.DX0DQSTR = pPHY_Reg->DATX8[0].DXDQSTR;
3140
3141     ddr_reg.publ.DX1GCR = pPHY_Reg->DATX8[1].DXGCR;
3142     ddr_reg.publ.DX1DLLCR = pPHY_Reg->DATX8[1].DXDLLCR;
3143     ddr_reg.publ.DX1DQTR = pPHY_Reg->DATX8[1].DXDQTR;
3144     ddr_reg.publ.DX1DQSTR = pPHY_Reg->DATX8[1].DXDQSTR;
3145
3146     ddr_reg.publ.DX2GCR = pPHY_Reg->DATX8[2].DXGCR;
3147     ddr_reg.publ.DX2DLLCR = pPHY_Reg->DATX8[2].DXDLLCR;
3148     ddr_reg.publ.DX2DQTR = pPHY_Reg->DATX8[2].DXDQTR;
3149     ddr_reg.publ.DX2DQSTR = pPHY_Reg->DATX8[2].DXDQSTR;
3150
3151     ddr_reg.publ.DX3GCR = pPHY_Reg->DATX8[3].DXGCR;
3152     ddr_reg.publ.DX3DLLCR = pPHY_Reg->DATX8[3].DXDLLCR;
3153     ddr_reg.publ.DX3DQTR = pPHY_Reg->DATX8[3].DXDQTR;
3154     ddr_reg.publ.DX3DQSTR = pPHY_Reg->DATX8[3].DXDQSTR;
3155
3156     //NOC
3157     ddr_reg.DdrConf = *(volatile uint32_t *)SysSrv_DdrConf;
3158     ddr_reg.DdrMode = *(volatile uint32_t *)SysSrv_DdrMode;
3159     ddr_reg.ReadLatency = *(volatile uint32_t *)SysSrv_ReadLatency;
3160 }
3161 EXPORT_SYMBOL(ddr_reg_save);
3162
3163 __attribute__((aligned(4))) __sramdata uint32 ddr_reg_resume[] = 
3164 {
3165 #include "ddr_reg_resume.inc"
3166 };
3167
3168 int ddr_init(uint32_t dram_speed_bin, uint32_t freq)
3169 {
3170     volatile uint32_t value = 0;
3171     uint32_t cs,die=1;
3172     uint32_t gsr,dqstr;
3173
3174     ddr_print("version 1.00 20120608 \n");
3175
3176     mem_type = pPHY_Reg->DCR.b.DDRMD;
3177     ddr_speed_bin = dram_speed_bin;
3178     ddr_freq = freq;
3179     ddr_sr_idle = 0;
3180     switch(mem_type)
3181     {
3182         case DDR3:
3183             die = 2;  //there is two die DDR3 at least
3184             ddr_print("DDR3 Device\n");
3185             break;
3186         case LPDDR2:
3187             ddr_print("LPDDR2 Device\n");
3188             break;
3189         case DDR2:
3190             ddr_print("DDR2 Device\n");
3191             break;
3192         case DDR:
3193             ddr_print("DDR Device\n");
3194             break;
3195         default:
3196             ddr_print("LPDDR Device\n");
3197             break;
3198     }
3199     switch((pPHY_Reg->PGCR>>18) & 0xF)
3200     {
3201         case 0xF:
3202             cs = 4;
3203         case 7:
3204             cs = 3;
3205             break;
3206         case 3:
3207             cs = 2;
3208             break;
3209         default:
3210             cs = 1;
3211             break;
3212     }
3213     //get capability per chip, not total size, used for calculate tRFC
3214     ddr_capability_per_die = ddr_get_cap()/(cs*die);
3215     ddr_print("%d CS, ROW=%d, Bank=%d, COL=%d, Total Capability=%dMB\n", 
3216                                                                     cs, \
3217                                                                     ddr_get_row(), \
3218                                                                     (0x1<<(ddr_get_bank())), \
3219                                                                     ddr_get_col(), \
3220                                                                     (ddr_get_cap()>>20));
3221     ddr_adjust_config(mem_type);
3222
3223     value=ddr_change_freq(freq);
3224     ddr_print("init success!!! freq=%dMHz\n", value);
3225
3226     for(value=0;value<4;value++)
3227     {
3228         gsr = pPHY_Reg->DATX8[value].DXGSR[0];
3229         dqstr = pPHY_Reg->DATX8[value].DXDQSTR;
3230         ddr_print("DTONE=0x%x, DTERR=0x%x, DTIERR=0x%x, DTPASS=0x%x, DGSL=%d extra clock, DGPS=%d\n", \
3231                    (gsr&0xF), ((gsr>>4)&0xF), ((gsr>>8)&0xF), ((gsr>>13)&0xFFF), (dqstr&0x7), (((dqstr>>12)&0x3)*90));
3232     }
3233     ddr_print("ZERR=%x, ZDONE=%x, ZPD=0x%x, ZPU=0x%x, OPD=0x%x, OPU=0x%x\n", \
3234                                                 (pPHY_Reg->ZQ0SR[0]>>30)&0x1, \
3235                                                 (pPHY_Reg->ZQ0SR[0]>>31)&0x1, \
3236                                                 pPHY_Reg->ZQ0SR[1]&0x3,\
3237                                                 (pPHY_Reg->ZQ0SR[1]>>2)&0x3,\
3238                                                 (pPHY_Reg->ZQ0SR[1]>>4)&0x3,\
3239                                                 (pPHY_Reg->ZQ0SR[1]>>6)&0x3);
3240     ddr_print("DRV Pull-Up=0x%x, DRV Pull-Dwn=0x%x\n", pPHY_Reg->ZQ0SR[0]&0x1F, (pPHY_Reg->ZQ0SR[0]>>5)&0x1F);
3241     ddr_print("ODT Pull-Up=0x%x, ODT Pull-Dwn=0x%x\n", (pPHY_Reg->ZQ0SR[0]>>10)&0x1F, (pPHY_Reg->ZQ0SR[0]>>15)&0x1F);
3242
3243     return 0;
3244 }
3245 EXPORT_SYMBOL(ddr_init);
3246