add chaos_calmer branch
[15.05/openwrt.git] / target / linux / generic / files / crypto / ocf / kirkwood / mvHal / kw_family / ctrlEnv / sys / mvSysDram.c
1 /*******************************************************************************
2 Copyright (C) Marvell International Ltd. and its affiliates
3
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.
11
12 ********************************************************************************
13 Marvell Commercial License Option
14
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.
18
19 ********************************************************************************
20 Marvell GPL License Option
21
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. 
28
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 
32 disclaimer.
33 ********************************************************************************
34 Marvell BSD License Option
35
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:
40
41     *   Redistributions of source code must retain the above copyright notice,
42             this list of conditions and the following disclaimer. 
43
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. 
47
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. 
51     
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.
62
63 *******************************************************************************/
64
65
66 /* includes */
67
68 #include "ddr2/mvDramIf.h"
69 #include "ctrlEnv/sys/mvCpuIf.h"
70 #include "ctrlEnv/sys/mvSysDram.h"
71
72 /* #define MV_DEBUG */
73 #ifdef MV_DEBUG
74 #define DB(x) x
75 #else
76 #define DB(x)
77 #endif
78
79 static MV_BOOL sdramIfWinOverlap(MV_TARGET target, MV_ADDR_WIN *pAddrWin);
80
81 /*******************************************************************************
82 * mvDramIfWinSet - Set DRAM interface address decode window
83 *
84 * DESCRIPTION: 
85 *       This function sets DRAM interface address decode window.
86 *
87 * INPUT:
88 *           target      - System target. Use only SDRAM targets.
89 *       pAddrDecWin - SDRAM address window structure.
90 *
91 * OUTPUT:
92 *       None
93 *
94 * RETURN:
95 *       MV_BAD_PARAM if parameters are invalid or window is invalid, MV_OK
96 *       otherwise.
97 *******************************************************************************/
98 MV_STATUS mvDramIfWinSet(MV_TARGET target, MV_DRAM_DEC_WIN *pAddrDecWin)
99 {
100         MV_U32 baseReg=0,sizeReg=0;
101         MV_U32 baseToReg=0 , sizeToReg=0;
102
103     /* Check parameters */
104         if (!MV_TARGET_IS_DRAM(target))
105         {
106                 mvOsPrintf("mvDramIfWinSet: target %d is not SDRAM\n", target);
107                 return MV_BAD_PARAM;
108         }
109
110     /* Check if the requested window overlaps with current enabled windows      */
111     if (MV_TRUE == sdramIfWinOverlap(target, &pAddrDecWin->addrWin))
112         {
113         mvOsPrintf("mvDramIfWinSet: ERR. Target %d overlaps\n", target);
114                 return MV_BAD_PARAM;
115         }
116
117         /* check if address is aligned to the size */
118         if(MV_IS_NOT_ALIGN(pAddrDecWin->addrWin.baseLow, pAddrDecWin->addrWin.size))
119         {
120                 mvOsPrintf("mvDramIfWinSet:Error setting DRAM interface window %d."\
121                                    "\nAddress 0x%08x is unaligned to size 0x%x.\n",
122                    target, 
123                                    pAddrDecWin->addrWin.baseLow,
124                                    pAddrDecWin->addrWin.size);
125                 return MV_ERROR;
126         }
127
128         /* read base register*/
129         baseReg = MV_REG_READ(SDRAM_BASE_ADDR_REG(0,target));
130
131         /* read size register */
132         sizeReg = MV_REG_READ(SDRAM_SIZE_REG(0,target));
133
134         /* BaseLow[31:16] => base register [31:16]              */
135         baseToReg = pAddrDecWin->addrWin.baseLow & SCBAR_BASE_MASK;
136
137         /* Write to address decode Base Address Register                  */
138         baseReg &= ~SCBAR_BASE_MASK;
139         baseReg |= baseToReg;
140
141         /* Translate the given window size to register format                   */
142         sizeToReg = ctrlSizeToReg(pAddrDecWin->addrWin.size, SCSR_SIZE_ALIGNMENT);
143
144         /* Size parameter validity check.                                   */
145         if (-1 == sizeToReg)
146         {
147                 mvOsPrintf("mvCtrlAddrDecToReg: ERR. Win %d size invalid.\n",target);
148                 return MV_BAD_PARAM;
149         }
150
151         /* set size */
152         sizeReg &= ~SCSR_SIZE_MASK;
153         /* Size is located at upper 16 bits */
154         sizeReg |= (sizeToReg << SCSR_SIZE_OFFS);
155
156         /* enable/Disable */
157         if (MV_TRUE == pAddrDecWin->enable)
158         {
159                 sizeReg |= SCSR_WIN_EN;
160         }
161         else
162         {
163                 sizeReg &= ~SCSR_WIN_EN;
164         }
165
166         /* 3) Write to address decode Base Address Register                   */
167         MV_REG_WRITE(SDRAM_BASE_ADDR_REG(0,target), baseReg);
168
169         /* Write to address decode Size Register                                */
170         MV_REG_WRITE(SDRAM_SIZE_REG(0,target), sizeReg);
171         
172         return MV_OK;
173 }
174 /*******************************************************************************
175 * mvDramIfWinGet - Get DRAM interface address decode window
176 *
177 * DESCRIPTION: 
178 *       This function gets DRAM interface address decode window.
179 *
180 * INPUT:
181 *           target - System target. Use only SDRAM targets.
182 *
183 * OUTPUT:
184 *       pAddrDecWin - SDRAM address window structure.
185 *
186 * RETURN:
187 *       MV_BAD_PARAM if parameters are invalid or window is invalid, MV_OK
188 *       otherwise.
189 *******************************************************************************/
190 MV_STATUS mvDramIfWinGet(MV_TARGET target, MV_DRAM_DEC_WIN *pAddrDecWin)
191 {
192         MV_U32 baseReg,sizeReg;
193         MV_U32 sizeRegVal;
194         /* Check parameters */
195         if (!MV_TARGET_IS_DRAM(target))
196         {
197                 mvOsPrintf("mvDramIfWinGet: target %d is Illigal\n", target);
198                 return MV_ERROR;
199         }
200
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));
204
205         sizeRegVal = (sizeReg & SCSR_SIZE_MASK) >> SCSR_SIZE_OFFS;
206
207         pAddrDecWin->addrWin.size = ctrlRegToSize(sizeRegVal,
208                                                         SCSR_SIZE_ALIGNMENT);
209
210         /* Check if ctrlRegToSize returned OK */
211         if (-1 == pAddrDecWin->addrWin.size)
212         {
213                 mvOsPrintf("mvDramIfWinGet: size of target %d is Illigal\n", target);
214                 return MV_ERROR;
215         }
216
217         /* Extract base address                                         */
218         /* Base register [31:16] ==> baseLow[31:16]             */
219         pAddrDecWin->addrWin.baseLow = baseReg & SCBAR_BASE_MASK;
220
221         pAddrDecWin->addrWin.baseHigh =  0;
222
223
224         if (sizeReg & SCSR_WIN_EN)
225         {
226                 pAddrDecWin->enable = MV_TRUE;
227         }
228         else
229         {
230                 pAddrDecWin->enable = MV_FALSE;                 
231         }
232
233         return MV_OK;
234 }
235 /*******************************************************************************
236 * mvDramIfWinEnable - Enable/Disable SDRAM address decode window
237 *
238 * DESCRIPTION: 
239 *               This function enable/Disable SDRAM address decode window.
240 *
241 * INPUT:
242 *           target - System target. Use only SDRAM targets.
243 *
244 * OUTPUT:
245 *               None.
246 *
247 * RETURN:
248 *               MV_ERROR in case function parameter are invalid, MV_OK otherewise.
249 *
250 *******************************************************************************/
251 MV_STATUS mvDramIfWinEnable(MV_TARGET target, MV_BOOL enable)
252 {
253         MV_DRAM_DEC_WIN         addrDecWin;
254
255         /* Check parameters */
256         if (!MV_TARGET_IS_DRAM(target))
257         {
258                 mvOsPrintf("mvDramIfWinEnable: target %d is Illigal\n", target);
259                 return MV_ERROR;
260         }
261
262         if (enable == MV_TRUE) 
263         {   /* First check for overlap with other enabled windows                               */
264                 if (MV_OK != mvDramIfWinGet(target, &addrDecWin))
265                 {
266                         mvOsPrintf("mvDramIfWinEnable:ERR. Getting target %d failed.\n", 
267                                                                         target);
268                         return MV_ERROR;
269                 }
270                 /* Check for overlapping */
271                 if (MV_FALSE == sdramIfWinOverlap(target, &(addrDecWin.addrWin)))
272                 {
273                         /* No Overlap. Enable address decode winNum window              */
274                         MV_REG_BIT_SET(SDRAM_SIZE_REG(0,target), SCSR_WIN_EN);
275                 }
276                 else
277                 {   /* Overlap detected */
278                         mvOsPrintf("mvDramIfWinEnable: ERR. Target %d overlap detect\n",
279                                                                         target);
280                         return MV_ERROR;
281                 }
282         }
283         else
284         {   /* Disable address decode winNum window                             */
285                 MV_REG_BIT_RESET(SDRAM_SIZE_REG(0, target), SCSR_WIN_EN);
286         }
287
288         return MV_OK;
289 }
290
291 /*******************************************************************************
292 * sdramIfWinOverlap - Check if an address window overlap an SDRAM address window
293 *
294 * DESCRIPTION:
295 *               This function scan each SDRAM address decode window to test if it 
296 *               overlapps the given address windoow 
297 *
298 * INPUT:
299 *       target      - SDRAM target where the function skips checking.
300 *       pAddrDecWin - The tested address window for overlapping with 
301 *                                         SDRAM windows.
302 *
303 * OUTPUT:
304 *       None.
305 *
306 * RETURN:
307 *       MV_TRUE if the given address window overlaps any enabled address
308 *       decode map, MV_FALSE otherwise.
309 *
310 *******************************************************************************/
311 static MV_BOOL sdramIfWinOverlap(MV_TARGET target, MV_ADDR_WIN *pAddrWin)
312 {
313         MV_TARGET       targetNum;
314         MV_DRAM_DEC_WIN         addrDecWin;
315         
316         for(targetNum = SDRAM_CS0; targetNum < MV_DRAM_MAX_CS ; targetNum++)
317         {
318                 /* don't check our winNum or illegal targets */
319                 if (targetNum == target)
320                 {
321                         continue;
322                 }
323                 
324                 /* Get window parameters        */
325                 if (MV_OK != mvDramIfWinGet(targetNum, &addrDecWin))
326                 {
327                         mvOsPrintf("sdramIfWinOverlap: ERR. TargetWinGet failed\n");
328                         return MV_ERROR;
329                 }
330         
331                 /* Do not check disabled windows        */
332                 if (MV_FALSE == addrDecWin.enable)
333                 {
334                         continue;
335                 }
336         
337                 if(MV_TRUE == ctrlWinOverlapTest(pAddrWin, &addrDecWin.addrWin))
338                 {                    
339                         mvOsPrintf(
340                         "sdramIfWinOverlap: Required target %d overlap winNum %d\n", 
341                         target, targetNum);
342                         return MV_TRUE;           
343                 }
344         }
345         
346         return MV_FALSE;
347 }
348