[processor-sdk/performance-audio-sr.git] / psdk_cust / pdk_k2g_1_0_1_1_eng / packages / ti / board / src / evmK2L / evmK2L_ddr.c
1 /******************************************************************************
2 * Copyright (c) 2010-2015 Texas Instruments Incorporated - http://www.ti.com
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 *
8 * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 *
11 * Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the
14 * distribution.
15 *
16 * Neither the name of Texas Instruments Incorporated nor the names of
17 * its contributors may be used to endorse or promote products derived
18 * from this software without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 *
32 *****************************************************************************/
34 #include <ti/csl/csl_bootcfgAux.h>
35 #include <ti/csl/cslr_device.h>
36 #include <ti/csl/csl_xmcAux.h>
37 #include <ti/csl/csl_emif4f.h>
38 #include <ti/csl/hw_types.h>
40 #include "board_internal.h"
42 /** \brief DDR configuration dealy in usec */
43 #define DDR_CFG_DELAY 200
45 /** \brief DDR PHY register offset */
46 #define DDRPHY_PIR_OFFSET 0x04
47 #define DDRPHY_PGCR0_OFFSET 0x08
48 #define DDRPHY_PGCR1_OFFSET 0x0C
49 #define DDRPHY_PGSR0_OFFSET 0x10
50 #define DDRPHY_PGSR1_OFFSET 0x14
51 #define DDRPHY_PLLCR_OFFSET 0x18
52 #define DDRPHY_PTR0_OFFSET 0x1C
53 #define DDRPHY_PTR1_OFFSET 0x20
54 #define DDRPHY_PTR2_OFFSET 0x24
55 #define DDRPHY_PTR3_OFFSET 0x28
56 #define DDRPHY_PTR4_OFFSET 0x2C
57 #define DDRPHY_DCR_OFFSET 0x44
58 #define DDRPHY_DTPR0_OFFSET 0x48
59 #define DDRPHY_DTPR1_OFFSET 0x4C
60 #define DDRPHY_DTPR2_OFFSET 0x50
61 #define DDRPHY_MR0_OFFSET 0x54
62 #define DDRPHY_MR1_OFFSET 0x58
63 #define DDRPHY_MR2_OFFSET 0x5C
64 #define DDRPHY_DTCR_OFFSET 0x68
65 #define DDRPHY_PGCR2_OFFSET 0x8C
66 #define DDRPHY_ZQ0CR1_OFFSET 0x184
67 #define DDRPHY_ZQ1CR1_OFFSET 0x194
68 #define DDRPHY_ZQ2CR1_OFFSET 0x1A4
69 #define DDRPHY_ZQ3CR1_OFFSET 0x1B4
70 #define DDRPHY_DATX8_8_OFFSET 0x3C0
72 #define IODDRM_MASK 0x00000180
73 #define ZCKSEL_MASK 0x01800000
74 #define CL_MASK 0x00000072
75 #define WR_MASK 0x00000E00
76 #define BL_MASK 0x00000003
77 #define RRMODE_MASK 0x00040000
78 #define UDIMM_MASK 0x20000000
79 #define BYTEMASK_MASK 0x0000FC00
80 #define MPRDQ_MASK 0x00000080
81 #define PDQ_MASK 0x00000070
82 #define NOSRA_MASK 0x08000000
83 #define ECC_MASK 0x00000001
86 /**
87 * \brief This structure defines the various Configuration Parameters for
88 * DDR3 PHY.
89 */
90 typedef struct ddr3_phy_config {
91 uint32_t pllcr;
92 uint32_t pgcr1_mask;
93 uint32_t pgcr1_val;
94 uint32_t ptr0;
95 uint32_t ptr1;
96 uint32_t ptr2;
97 uint32_t ptr3;
98 uint32_t ptr4;
99 uint32_t dcr_mask;
100 uint32_t dcr_val;
101 uint32_t dtpr0;
102 uint32_t dtpr1;
103 uint32_t dtpr2;
104 uint32_t mr0;
105 uint32_t mr1;
106 uint32_t mr2;
107 uint32_t dtcr;
108 uint32_t pgcr2;
109 uint32_t zq0cr1;
110 uint32_t zq1cr1;
111 uint32_t zq2cr1;
112 uint32_t pir_v1;
113 uint32_t pir_v2;
114 } ddr3_phy_config;
116 /**
117 * \brief This structure defines the various Configuration Parameters for
118 * EMIF4 controller.
119 */
120 typedef struct ddr3_emif_config {
121 uint32_t sdcfg;
122 uint32_t sdtim1;
123 uint32_t sdtim2;
124 uint32_t sdtim3;
125 uint32_t sdtim4;
126 uint32_t zqcfg;
127 uint32_t sdrfc;
128 } ddr3_emif_config;
130 static const ddr3_emif_config ddr3_1600 = {
131 0x6200CE62,
132 0x166C9855,
133 0x00001D4A,
134 0x435DFF53,
135 0x543F0CFF,
136 0x70073200,
137 0x00001869,
138 };
140 static const ddr3_phy_config ddr3phy_1600 = {
141 0x1C000,
142 (IODDRM_MASK | ZCKSEL_MASK | ZCKSEL_MASK),
143 ((1 << 2) | (1 << 7) | (1 << 23)),
144 0x42C21590,
145 0xD05612C0,
146 0, /* not set in gel */
147 0x0D861A80,
148 0x0C827100,
149 (PDQ_MASK | MPRDQ_MASK | BYTEMASK_MASK),
150 ((1 << 10)),
151 0x9D5CBB66,
152 0x12868300,
153 0x5002D200,
154 0x00001C70,
155 0x00000006,
156 0x00000018,
157 0x710035C7,
158 0x00F07A12,
159 0x0001005D,
160 0x0001005B,
161 0x0001005B,
162 0x00000033,
163 0x0000FF81,
164 };
166 extern void BOARD_delay(uint32_t usecs);
168 static void xmc_add_emif_cfg_region()
169 {
170 /* mapping for ddr emif registers XMPAX*2 */
171 CSL_XMC_XMPAXL mpaxl;
172 CSL_XMC_XMPAXH mpaxh;
174 mpaxh.bAddr = 0x21010; /* Segment Base Address */
175 mpaxh.segSize = 0xB; /* Segment size 4KB */
177 mpaxl.rAddr = 0x121010; /* Replacement Address */
178 mpaxl.sr = 1;
179 mpaxl.sw = 1;
180 mpaxl.sx = 1;
181 mpaxl.ur = 1;
182 mpaxl.uw = 1;
183 mpaxl.ux = 1;
185 /* set the xmpax for index2 */
186 CSL_XMC_setXMPAXH(2, &mpaxh);
187 CSL_XMC_setXMPAXL(2, &mpaxl);
188 }
190 static bool init_ddrphy(uint32_t base, const ddr3_phy_config *phy_cfg)
191 {
192 uint32_t tmp;
193 uint32_t timeoutCnt = 5;
195 while((HW_RD_REG32(base + DDRPHY_PGSR0_OFFSET)
196 & 0x00000001) != 0x00000001) {
197 BOARD_delay(DDR_CFG_DELAY);
198 timeoutCnt--;
199 if (timeoutCnt == 0)
200 return false;
201 }
203 HW_WR_REG32(base + DDRPHY_PLLCR_OFFSET, phy_cfg->pllcr);
204 BOARD_delay(DDR_CFG_DELAY);
206 tmp = HW_RD_REG32(base + DDRPHY_PGCR1_OFFSET);
207 tmp &= ~(phy_cfg->pgcr1_mask);
208 tmp |= phy_cfg->pgcr1_val;
209 HW_WR_REG32(base + DDRPHY_PGCR1_OFFSET, tmp);
210 BOARD_delay(DDR_CFG_DELAY);
212 HW_WR_REG32(base + DDRPHY_PTR0_OFFSET, phy_cfg->ptr0);
213 HW_WR_REG32(base + DDRPHY_PTR1_OFFSET, phy_cfg->ptr1);
214 HW_WR_REG32(base + DDRPHY_PTR3_OFFSET, phy_cfg->ptr3);
215 HW_WR_REG32(base + DDRPHY_PTR4_OFFSET, phy_cfg->ptr4);
217 tmp = HW_RD_REG32(base + DDRPHY_DCR_OFFSET);
218 tmp &= ~(phy_cfg->dcr_mask);
219 tmp |= phy_cfg->dcr_val;
220 HW_WR_REG32(base + DDRPHY_DCR_OFFSET, tmp);
221 BOARD_delay(DDR_CFG_DELAY);
223 HW_WR_REG32(base + DDRPHY_DTPR0_OFFSET, phy_cfg->dtpr0);
224 HW_WR_REG32(base + DDRPHY_DTPR1_OFFSET, phy_cfg->dtpr1);
225 HW_WR_REG32(base + DDRPHY_DTPR2_OFFSET, phy_cfg->dtpr2);
227 HW_WR_REG32(base + DDRPHY_MR0_OFFSET, phy_cfg->mr0);
228 HW_WR_REG32(base + DDRPHY_MR1_OFFSET, phy_cfg->mr1);
229 HW_WR_REG32(base + DDRPHY_MR2_OFFSET, phy_cfg->mr2);
231 HW_WR_REG32(base + DDRPHY_DTCR_OFFSET, phy_cfg->dtcr);
232 BOARD_delay(DDR_CFG_DELAY);
233 HW_WR_REG32(base + DDRPHY_PGCR2_OFFSET, phy_cfg->pgcr2);
234 BOARD_delay(DDR_CFG_DELAY);
236 HW_WR_REG32(base + DDRPHY_ZQ0CR1_OFFSET, phy_cfg->zq0cr1);
237 HW_WR_REG32(base + DDRPHY_ZQ1CR1_OFFSET, phy_cfg->zq1cr1);
238 HW_WR_REG32(base + DDRPHY_ZQ2CR1_OFFSET, phy_cfg->zq2cr1);
240 HW_WR_REG32(base + DDRPHY_PIR_OFFSET, phy_cfg->pir_v1);
241 BOARD_delay(DDR_CFG_DELAY);
243 timeoutCnt = 5;
244 while((HW_RD_REG32(base + DDRPHY_PGSR0_OFFSET) & 0x1) != 0x1) {
245 BOARD_delay(DDR_CFG_DELAY);
246 timeoutCnt--;
247 if (timeoutCnt == 0)
248 return false;
249 }
251 HW_WR_REG32(base + DDRPHY_PIR_OFFSET, phy_cfg->pir_v2);
252 BOARD_delay(DDR_CFG_DELAY);
253 timeoutCnt = 5;
254 while((HW_RD_REG32(base + DDRPHY_PGSR0_OFFSET) & 0x1) != 0x1) {
255 BOARD_delay(DDR_CFG_DELAY);
256 timeoutCnt--;
257 if (timeoutCnt == 0)
258 return false;
259 }
260 return true;
261 }
263 static void init_ddremif(CSL_Emif4fHandle hEmif4, const ddr3_emif_config *emif_cfg)
264 {
265 hEmif4->SDRAM_CONFIG = emif_cfg->sdcfg;
266 hEmif4->SDRAM_TIM_1 = emif_cfg->sdtim1;
267 hEmif4->SDRAM_TIM_2 = emif_cfg->sdtim2;
268 hEmif4->SDRAM_TIM_3 = emif_cfg->sdtim3;
269 hEmif4->SDRAM_TIM_4 = emif_cfg->sdtim4;
270 hEmif4->ZQ_CONFIG = emif_cfg->zqcfg;
271 hEmif4->SDRAM_REF_CTRL = emif_cfg->sdrfc;
272 }
274 /* Set the desired DDR3 configuration -- assumes 66.67 MHz DDR3 clock input */
275 Board_STATUS Board_DDR3Init()
276 {
277 xmc_add_emif_cfg_region();
279 CSL_BootCfgUnlockKicker();
281 if (init_ddrphy(CSL_DDR3_0_PHY_CFG_REGS, &ddr3phy_1600) == false)
282 return BOARD_INIT_DDR_FAIL;
283 init_ddremif((CSL_Emif4fHandle)CSL_DDR3_0_SLV_CFG_REGS, &ddr3_1600);
285 return BOARD_SOK;
286 }