1 /*******************************************************************************
2 Copyright (C) Marvell International Ltd. and its affiliates
4 This software file (the "File") is owned and distributed by Marvell
5 International Ltd. and/or its affiliates ("Marvell") under the following
6 alternative licensing terms. Once you have made an election to distribute the
7 File under one of the following license alternatives, please (i) delete this
8 introductory statement regarding license alternatives, (ii) delete the two
9 license alternatives that you have not elected to use and (iii) preserve the
10 Marvell copyright notice above.
12 ********************************************************************************
13 Marvell Commercial License Option
15 If you received this File from Marvell and you have entered into a commercial
16 license agreement (a "Commercial License") with Marvell, the File is licensed
17 to you under the terms of the applicable Commercial License.
19 ********************************************************************************
20 Marvell GPL License Option
22 If you received this File from Marvell, you may opt to use, redistribute and/or
23 modify this File in accordance with the terms and conditions of the General
24 Public License Version 2, June 1991 (the "GPL License"), a copy of which is
25 available along with the File in the license.txt file or by writing to the Free
26 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 or
27 on the worldwide web at http://www.gnu.org/licenses/gpl.txt.
29 THE FILE IS DISTRIBUTED AS-IS, WITHOUT WARRANTY OF ANY KIND, AND THE IMPLIED
30 WARRANTIES OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE ARE EXPRESSLY
31 DISCLAIMED. The GPL License provides additional details about this warranty
33 ********************************************************************************
34 Marvell BSD License Option
36 If you received this File from Marvell, you may opt to use, redistribute and/or
37 modify this File under the following licensing terms.
38 Redistribution and use in source and binary forms, with or without modification,
39 are permitted provided that the following conditions are met:
41 * Redistributions of source code must retain the above copyright notice,
42 this list of conditions and the following disclaimer.
44 * Redistributions in binary form must reproduce the above copyright
45 notice, this list of conditions and the following disclaimer in the
46 documentation and/or other materials provided with the distribution.
48 * Neither the name of Marvell nor the names of its contributors may be
49 used to endorse or promote products derived from this software without
50 specific prior written permission.
52 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
53 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
54 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
55 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
56 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
57 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
58 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
59 ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
60 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
61 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
63 *******************************************************************************/
68 #include "ddr2/mvDramIf.h"
69 #include "ctrlEnv/sys/mvCpuIf.h"
70 #include "ctrlEnv/sys/mvSysDram.h"
72 /* #define MV_DEBUG */
79 static MV_BOOL
sdramIfWinOverlap(MV_TARGET target
, MV_ADDR_WIN
*pAddrWin
);
81 /*******************************************************************************
82 * mvDramIfWinSet - Set DRAM interface address decode window
85 * This function sets DRAM interface address decode window.
88 * target - System target. Use only SDRAM targets.
89 * pAddrDecWin - SDRAM address window structure.
95 * MV_BAD_PARAM if parameters are invalid or window is invalid, MV_OK
97 *******************************************************************************/
98 MV_STATUS
mvDramIfWinSet(MV_TARGET target
, MV_DRAM_DEC_WIN
*pAddrDecWin
)
100 MV_U32 baseReg
=0,sizeReg
=0;
101 MV_U32 baseToReg
=0 , sizeToReg
=0;
103 /* Check parameters */
104 if (!MV_TARGET_IS_DRAM(target
))
106 mvOsPrintf("mvDramIfWinSet: target %d is not SDRAM\n", target
);
110 /* Check if the requested window overlaps with current enabled windows */
111 if (MV_TRUE
== sdramIfWinOverlap(target
, &pAddrDecWin
->addrWin
))
113 mvOsPrintf("mvDramIfWinSet: ERR. Target %d overlaps\n", target
);
117 /* check if address is aligned to the size */
118 if(MV_IS_NOT_ALIGN(pAddrDecWin
->addrWin
.baseLow
, pAddrDecWin
->addrWin
.size
))
120 mvOsPrintf("mvDramIfWinSet:Error setting DRAM interface window %d."\
121 "\nAddress 0x%08x is unaligned to size 0x%x.\n",
123 pAddrDecWin
->addrWin
.baseLow
,
124 pAddrDecWin
->addrWin
.size
);
128 /* read base register*/
129 baseReg
= MV_REG_READ(SDRAM_BASE_ADDR_REG(0,target
));
131 /* read size register */
132 sizeReg
= MV_REG_READ(SDRAM_SIZE_REG(0,target
));
134 /* BaseLow[31:16] => base register [31:16] */
135 baseToReg
= pAddrDecWin
->addrWin
.baseLow
& SCBAR_BASE_MASK
;
137 /* Write to address decode Base Address Register */
138 baseReg
&= ~SCBAR_BASE_MASK
;
139 baseReg
|= baseToReg
;
141 /* Translate the given window size to register format */
142 sizeToReg
= ctrlSizeToReg(pAddrDecWin
->addrWin
.size
, SCSR_SIZE_ALIGNMENT
);
144 /* Size parameter validity check. */
147 mvOsPrintf("mvCtrlAddrDecToReg: ERR. Win %d size invalid.\n",target
);
152 sizeReg
&= ~SCSR_SIZE_MASK
;
153 /* Size is located at upper 16 bits */
154 sizeReg
|= (sizeToReg
<< SCSR_SIZE_OFFS
);
157 if (MV_TRUE
== pAddrDecWin
->enable
)
159 sizeReg
|= SCSR_WIN_EN
;
163 sizeReg
&= ~SCSR_WIN_EN
;
166 /* 3) Write to address decode Base Address Register */
167 MV_REG_WRITE(SDRAM_BASE_ADDR_REG(0,target
), baseReg
);
169 /* Write to address decode Size Register */
170 MV_REG_WRITE(SDRAM_SIZE_REG(0,target
), sizeReg
);
174 /*******************************************************************************
175 * mvDramIfWinGet - Get DRAM interface address decode window
178 * This function gets DRAM interface address decode window.
181 * target - System target. Use only SDRAM targets.
184 * pAddrDecWin - SDRAM address window structure.
187 * MV_BAD_PARAM if parameters are invalid or window is invalid, MV_OK
189 *******************************************************************************/
190 MV_STATUS
mvDramIfWinGet(MV_TARGET target
, MV_DRAM_DEC_WIN
*pAddrDecWin
)
192 MV_U32 baseReg
,sizeReg
;
194 /* Check parameters */
195 if (!MV_TARGET_IS_DRAM(target
))
197 mvOsPrintf("mvDramIfWinGet: target %d is Illigal\n", target
);
201 /* Read base and size registers */
202 sizeReg
= MV_REG_READ(SDRAM_SIZE_REG(0,target
));
203 baseReg
= MV_REG_READ(SDRAM_BASE_ADDR_REG(0,target
));
205 sizeRegVal
= (sizeReg
& SCSR_SIZE_MASK
) >> SCSR_SIZE_OFFS
;
207 pAddrDecWin
->addrWin
.size
= ctrlRegToSize(sizeRegVal
,
208 SCSR_SIZE_ALIGNMENT
);
210 /* Check if ctrlRegToSize returned OK */
211 if (-1 == pAddrDecWin
->addrWin
.size
)
213 mvOsPrintf("mvDramIfWinGet: size of target %d is Illigal\n", target
);
217 /* Extract base address */
218 /* Base register [31:16] ==> baseLow[31:16] */
219 pAddrDecWin
->addrWin
.baseLow
= baseReg
& SCBAR_BASE_MASK
;
221 pAddrDecWin
->addrWin
.baseHigh
= 0;
224 if (sizeReg
& SCSR_WIN_EN
)
226 pAddrDecWin
->enable
= MV_TRUE
;
230 pAddrDecWin
->enable
= MV_FALSE
;
235 /*******************************************************************************
236 * mvDramIfWinEnable - Enable/Disable SDRAM address decode window
239 * This function enable/Disable SDRAM address decode window.
242 * target - System target. Use only SDRAM targets.
248 * MV_ERROR in case function parameter are invalid, MV_OK otherewise.
250 *******************************************************************************/
251 MV_STATUS
mvDramIfWinEnable(MV_TARGET target
, MV_BOOL enable
)
253 MV_DRAM_DEC_WIN addrDecWin
;
255 /* Check parameters */
256 if (!MV_TARGET_IS_DRAM(target
))
258 mvOsPrintf("mvDramIfWinEnable: target %d is Illigal\n", target
);
262 if (enable
== MV_TRUE
)
263 { /* First check for overlap with other enabled windows */
264 if (MV_OK
!= mvDramIfWinGet(target
, &addrDecWin
))
266 mvOsPrintf("mvDramIfWinEnable:ERR. Getting target %d failed.\n",
270 /* Check for overlapping */
271 if (MV_FALSE
== sdramIfWinOverlap(target
, &(addrDecWin
.addrWin
)))
273 /* No Overlap. Enable address decode winNum window */
274 MV_REG_BIT_SET(SDRAM_SIZE_REG(0,target
), SCSR_WIN_EN
);
277 { /* Overlap detected */
278 mvOsPrintf("mvDramIfWinEnable: ERR. Target %d overlap detect\n",
284 { /* Disable address decode winNum window */
285 MV_REG_BIT_RESET(SDRAM_SIZE_REG(0, target
), SCSR_WIN_EN
);
291 /*******************************************************************************
292 * sdramIfWinOverlap - Check if an address window overlap an SDRAM address window
295 * This function scan each SDRAM address decode window to test if it
296 * overlapps the given address windoow
299 * target - SDRAM target where the function skips checking.
300 * pAddrDecWin - The tested address window for overlapping with
307 * MV_TRUE if the given address window overlaps any enabled address
308 * decode map, MV_FALSE otherwise.
310 *******************************************************************************/
311 static MV_BOOL
sdramIfWinOverlap(MV_TARGET target
, MV_ADDR_WIN
*pAddrWin
)
314 MV_DRAM_DEC_WIN addrDecWin
;
316 for(targetNum
= SDRAM_CS0
; targetNum
< MV_DRAM_MAX_CS
; targetNum
++)
318 /* don't check our winNum or illegal targets */
319 if (targetNum
== target
)
324 /* Get window parameters */
325 if (MV_OK
!= mvDramIfWinGet(targetNum
, &addrDecWin
))
327 mvOsPrintf("sdramIfWinOverlap: ERR. TargetWinGet failed\n");
331 /* Do not check disabled windows */
332 if (MV_FALSE
== addrDecWin
.enable
)
337 if(MV_TRUE
== ctrlWinOverlapTest(pAddrWin
, &addrDecWin
.addrWin
))
340 "sdramIfWinOverlap: Required target %d overlap winNum %d\n",