xref: /utopia/UTPA2-700.0.x/modules/msos/hal/curry/mmio/halMMIO.c (revision 53ee8cc121a030b8d368113ac3e966b4705770ef)
1 //<MStar Software>
2 //******************************************************************************
3 // MStar Software
4 // Copyright (c) 2010 - 2012 MStar Semiconductor, Inc. All rights reserved.
5 // All software, firmware and related documentation herein ("MStar Software") are
6 // intellectual property of MStar Semiconductor, Inc. ("MStar") and protected by
7 // law, including, but not limited to, copyright law and international treaties.
8 // Any use, modification, reproduction, retransmission, or republication of all
9 // or part of MStar Software is expressly prohibited, unless prior written
10 // permission has been granted by MStar.
11 //
12 // By accessing, browsing and/or using MStar Software, you acknowledge that you
13 // have read, understood, and agree, to be bound by below terms ("Terms") and to
14 // comply with all applicable laws and regulations:
15 //
16 // 1. MStar shall retain any and all right, ownership and interest to MStar
17 //    Software and any modification/derivatives thereof.
18 //    No right, ownership, or interest to MStar Software and any
19 //    modification/derivatives thereof is transferred to you under Terms.
20 //
21 // 2. You understand that MStar Software might include, incorporate or be
22 //    supplied together with third party`s software and the use of MStar
23 //    Software may require additional licenses from third parties.
24 //    Therefore, you hereby agree it is your sole responsibility to separately
25 //    obtain any and all third party right and license necessary for your use of
26 //    such third party`s software.
27 //
28 // 3. MStar Software and any modification/derivatives thereof shall be deemed as
29 //    MStar`s confidential information and you agree to keep MStar`s
30 //    confidential information in strictest confidence and not disclose to any
31 //    third party.
32 //
33 // 4. MStar Software is provided on an "AS IS" basis without warranties of any
34 //    kind. Any warranties are hereby expressly disclaimed by MStar, including
35 //    without limitation, any warranties of merchantability, non-infringement of
36 //    intellectual property rights, fitness for a particular purpose, error free
37 //    and in conformity with any international standard.  You agree to waive any
38 //    claim against MStar for any loss, damage, cost or expense that you may
39 //    incur related to your use of MStar Software.
40 //    In no event shall MStar be liable for any direct, indirect, incidental or
41 //    consequential damages, including without limitation, lost of profit or
42 //    revenues, lost or damage of data, and unauthorized system use.
43 //    You agree that this Section 4 shall still apply without being affected
44 //    even if MStar Software has been modified by MStar in accordance with your
45 //    request or instruction for your use, except otherwise agreed by both
46 //    parties in writing.
47 //
48 // 5. If requested, MStar may from time to time provide technical supports or
49 //    services in relation with MStar Software to you for your use of
50 //    MStar Software in conjunction with your or your customer`s product
51 //    ("Services").
52 //    You understand and agree that, except otherwise agreed by both parties in
53 //    writing, Services are provided on an "AS IS" basis and the warranty
54 //    disclaimer set forth in Section 4 above shall apply.
55 //
56 // 6. Nothing contained herein shall be construed as by implication, estoppels
57 //    or otherwise:
58 //    (a) conferring any license or right to use MStar name, trademark, service
59 //        mark, symbol or any other identification;
60 //    (b) obligating MStar or any of its affiliates to furnish any person,
61 //        including without limitation, you and your customers, any assistance
62 //        of any kind whatsoever, or any information; or
63 //    (c) conferring any license or right under any intellectual property right.
64 //
65 // 7. These terms shall be governed by and construed in accordance with the laws
66 //    of Taiwan, R.O.C., excluding its conflict of law rules.
67 //    Any and all dispute arising out hereof or related hereto shall be finally
68 //    settled by arbitration referred to the Chinese Arbitration Association,
69 //    Taipei in accordance with the ROC Arbitration Law and the Arbitration
70 //    Rules of the Association by three (3) arbitrators appointed in accordance
71 //    with the said Rules.
72 //    The place of arbitration shall be in Taipei, Taiwan and the language shall
73 //    be English.
74 //    The arbitration award shall be final and binding to both parties.
75 //
76 //******************************************************************************
77 //<MStar Software>
78 ///////////////////////////////////////////////////////////////////////////////
79 //
80 // Copyright (c) 2008-2009 MStar Semiconductor, Inc.
81 // All rights reserved.
82 //
83 // Unless otherwise stipulated in writing, any and all information contained
84 // herein regardless in any format shall remain the sole proprietary of
85 // MStar Semiconductor Inc. and be kept in strict confidence
86 // ("MStar Confidential Information") by the recipient.
87 // Any unauthorized act including without limitation unauthorized disclosure,
88 // copying, use, reproduction, sale, distribution, modification, disassembling,
89 // reverse engineering and compiling of the contents of MStar Confidential
90 // Information is unlawful and strictly prohibited. MStar hereby reserves the
91 // rights to any and all damages, losses, costs and expenses resulting therefrom.
92 //
93 ///////////////////////////////////////////////////////////////////////////////
94 
95 ///////////////////////////////////////////////////////////////////////////////////////////////////
96 ///
97 /// file    halMMIO.c
98 /// @brief  memory map io (MMIO) HAL
99 /// @author MStar Semiconductor Inc.
100 ///////////////////////////////////////////////////////////////////////////////////////////////////
101 
102 //-------------------------------------------------------------------------------------------------
103 //  Include Files
104 //-------------------------------------------------------------------------------------------------
105 
106 #include "MsCommon.h"
107 #include "halMMIO.h"
108 #include "drvMMIO.h"
109 
110 // for getting mapped IO base from DRV
111 extern MS_VIRT   _virtPM_Bank         ;
112 extern MS_U32   _u32PM_Bank_SIZE    ;
113 extern MS_VIRT   _virtNonPM_Bank      ;
114 extern MS_U32   _u32NonPM_Bank_SIZE ;
115 
116 //-------------------------------------------------------------------------------------------------
117 //  Driver Compiler Options
118 //-------------------------------------------------------------------------------------------------
119 
120 //--------------------------------------------------------------------------------------------------
121 //  Global Variables
122 //--------------------------------------------------------------------------------------------------
123 MS_VIRT virt_ge0_mmio_base;
124 
125 //-------------------------------------------------------------------------------------------------
126 //  Local Defines
127 //-------------------------------------------------------------------------------------------------
128 #ifdef MSOS_TYPE_LINUX
129 
130 #define HAL_MMIO_PM_BASE                0x1f000000
131 #define HAL_MMIO_PM_SIZE                0x00400000
132 #define HAL_MMIO_NONPM_BASE             0x1f200000
133 #define HAL_MMIO_NONPM_SIZE             0x00200000
134 #define HAL_MMIO_FLASH_BASE0            0x14000000
135 #define HAL_MMIO_FLASH_SIZE0            0x01000000
136 #define HAL_MMIO_OTP_BASE               0x10000000
137 #define HAL_MMIO_OTP_SIZE               0x00024000
138 #define HAL_MMIO_OTP_RAW_BASE           0x1f320000
139 #elif defined(MSOS_TYPE_LINUX_KERNEL)
140 
141     #if defined(CONFIG_UTOPIA_FRAMEWORK_KERNEL_DRIVER_64BIT)
142         extern ptrdiff_t mstar_pm_base;
143         #define RIU_BASE    mstar_pm_base
144     #else
145         #define RIU_BASE     0xfd000000UL
146     #endif
147     #define HAL_MMIO_PM_BASE            RIU_BASE
148     #define HAL_MMIO_PM_SIZE            0x00400000UL
149     #define HAL_MMIO_NONPM_BASE         RIU_BASE+0x200000UL
150     #define HAL_MMIO_NONPM_SIZE         0x00200000UL
151     #define HAL_MMIO_FLASH_BASE0        0x14000000UL
152     #define HAL_MMIO_FLASH_SIZE0        0x0100000UL
153     #define HAL_MMIO_OTP_BASE           0x10000000
154     #define HAL_MMIO_OTP_SIZE           0x00024000
155     #define HAL_MMIO_OTP_RAW_BASE       RIU_BASE+0x320000UL
156 #else
157 
158 #if defined(MCU_AEON)
159 #define HAL_MMIO_PM_BASE                0xFA000000
160 #define HAL_MMIO_PM_SIZE                0x00007B80
161 #define HAL_MMIO_NONPM_BASE             0xFA200000
162 #define HAL_MMIO_NONPM_SIZE             0x00025600
163 #define HAL_MMIO_FLASH_BASE0            0xF9000000 // non-cache // 0xA1000000 for cache
164 #define HAL_MMIO_FLASH_SIZE0            0x01000000
165 
166 #elif defined(__mips__)
167 #define HAL_MMIO_PM_BASE                0xbf000000
168 #define HAL_MMIO_PM_SIZE                0x00007B80
169 #define HAL_MMIO_NONPM_BASE             0xbf200000
170 #define HAL_MMIO_NONPM_SIZE             0x00025600
171 #define HAL_MMIO_FLASH_BASE0            0xB4000000 // 0x94000000 for cache
172 #define HAL_MMIO_FLASH_SIZE0            0x01000000
173 #define HAL_MMIO_OTP_BASE               0xB0000000
174 #define HAL_MMIO_OTP_SIZE               0x00002000
175 
176 #elif defined(__arm__) || defined(__aarch64__)
177 #if defined (CONFIG_MBOOT) || defined (MSOS_TYPE_NUTTX)  || defined (MSOS_TYPE_OPTEE)
178 #define HAL_MMIO_PM_BASE                0x1f000000
179 #define HAL_MMIO_PM_SIZE                0x00400000
180 #define HAL_MMIO_NONPM_BASE             0x1f200000
181 #define HAL_MMIO_NONPM_SIZE             0x00200000
182 #define HAL_MMIO_FLASH_BASE0            0x14000000
183 #define HAL_MMIO_FLASH_SIZE0            0x01000000
184 #define HAL_MMIO_OTP_BASE               0xfd800000
185 #define HAL_MMIO_OTP_SIZE               0x00004000
186 #define HAL_MMIO_OTP_RAW_BASE		    0x1f190000
187 
188 #else
189 #define HAL_MMIO_PM_BASE                0xfd000000
190 #define HAL_MMIO_PM_SIZE                0x00400000
191 #define HAL_MMIO_NONPM_BASE             0xfd200000
192 #define HAL_MMIO_NONPM_SIZE             0x00200000
193 #define HAL_MMIO_FLASH_BASE0            0xfe000000
194 #define HAL_MMIO_FLASH_SIZE0            0x01000000
195 #define HAL_MMIO_OTP_BASE               0xfd800000
196 #define HAL_MMIO_OTP_SIZE               0x00024000
197 #define HAL_MMIO_OTP_RAW_BASE		0xfd320000
198 
199 #endif
200 
201 #else
202 #error  "Invalid MCU Type";
203 #endif
204 
205 #endif  //MSOS_TYPE_LINUX
206 
207 //-------------------------------------------------------------------------------------------------
208 //  Local Structurs
209 //-------------------------------------------------------------------------------------------------
210 
211 
212 //-------------------------------------------------------------------------------------------------
213 //  Global Variables
214 //-------------------------------------------------------------------------------------------------
215 
216 
217 //-------------------------------------------------------------------------------------------------
218 //  Local Variables
219 //-------------------------------------------------------------------------------------------------
220 
221 
222 //-------------------------------------------------------------------------------------------------
223 //  Debug Functions
224 //-------------------------------------------------------------------------------------------------
225 
226 
227 //-------------------------------------------------------------------------------------------------
228 //  Local Functions
229 //-------------------------------------------------------------------------------------------------
230 
231 
232 //-------------------------------------------------------------------------------------------------
233 //  Global Functions
234 //-------------------------------------------------------------------------------------------------
HAL_MMIO_GetType(MS_U32 u32Module)235 MS_U16  HAL_MMIO_GetType(MS_U32 u32Module)
236 {
237     switch (u32Module)
238     {
239     //HAL_MMIO_PM_BANK
240     case MS_MODULE_PM       :
241     case MS_MODULE_IR       :
242     case MS_MODULE_ISP      :
243     case MS_MODULE_PWS      :
244         return DRV_MMIO_PM_BANK;
245 
246     //HAL_MMIO_NONPM_BANK
247     case MS_MODULE_HW       :
248     case MS_MODULE_CHIPTOP  :
249     case MS_MODULE_MIU      :
250     case MS_MODULE_ACE      :
251     case MS_MODULE_AUDIO    :
252     case MS_MODULE_AVD      :
253     case MS_MODULE_BDMA     :
254     case MS_MODULE_DLC      :
255     case MS_MODULE_DMD      :
256     case MS_MODULE_GE       :
257     case MS_MODULE_GOP      :
258     case MS_MODULE_GPIO     :
259     case MS_MODULE_HVD      :
260     case MS_MODULE_HWI2C    :
261     case MS_MODULE_IRQ      :
262     case MS_MODULE_JPD      :
263     case MS_MODULE_MBX      :
264     case MS_MODULE_MFE      :
265     case MS_MODULE_MHEG5    :
266     case MS_MODULE_MVD      :
267     case MS_MODULE_MVOP     :
268     case MS_MODULE_RVD      :
269     case MS_MODULE_TSP      :
270     case MS_MODULE_UART     :
271     case MS_MODULE_VPU      :
272     case MS_MODULE_XC       :
273     case MS_MODULE_PCMCIA   :
274     case MS_MODULE_PFSH     :
275     case MS_MODULE_PNL      :
276     case MS_MODULE_PWM      :
277     case MS_MODULE_SEM      :
278     case MS_MODULE_VBI      :
279     case MS_MODULE_VIF      :
280     case MS_MODULE_DIP      :
281 	case MS_MODULE_MPIF     :
282 	case MS_MODULE_MMFILEIN :
283     case MS_MODULE_GPD      :
284     case MS_MODULE_TSO      :
285     case MS_MODULE_CMDQ     :
286         return DRV_MMIO_NONPM_BANK;
287 
288     case MS_MODULE_SC       :
289         return DRV_MMIO_SC_BANK;
290     case MS_MODULE_SC1       :
291         return DRV_MMIO_SC1_BANK;
292 
293      //HAL_MMIO_FLASH_BANK0
294     case MS_MODULE_FLASH    :
295         return DRV_MMIO_FLASH_BANK0;
296 
297     case MS_MODULE_OTP2      :
298         return DRV_MMIO_OTP_BANK2;
299 
300     default:
301         return 0xFFFF; //undefine type
302     }
303 
304     return 0xFFFF; //undefine type
305 }
306 
307 
HAL_MMIO_GetBase(MS_PHY * virtBaseAddr,MS_PHY * pu32BaseSize,MS_U32 u32BankType)308 MS_BOOL HAL_MMIO_GetBase(MS_PHY* virtBaseAddr, MS_PHY* pu32BaseSize, MS_U32 u32BankType)
309 {
310     MS_BOOL bRet = TRUE;
311 
312     *virtBaseAddr = 0;
313     *pu32BaseSize = 0;
314     switch (u32BankType)
315     {
316         case DRV_MMIO_FRC_BANK:
317             break;
318         case DRV_MMIO_PM_BANK:
319         *virtBaseAddr = (MS_PHY)HAL_MMIO_PM_BASE;
320         *pu32BaseSize = (MS_PHY)HAL_MMIO_PM_SIZE;
321             break;
322 
323         case DRV_MMIO_NONPM_BANK:
324         *virtBaseAddr = (MS_PHY)HAL_MMIO_NONPM_BASE;
325         *pu32BaseSize = (MS_PHY)HAL_MMIO_NONPM_SIZE;
326             break;
327 
328         case DRV_MMIO_FLASH_BANK0:
329         *virtBaseAddr = (MS_PHY)HAL_MMIO_FLASH_BASE0;
330         *pu32BaseSize = (MS_PHY)HAL_MMIO_FLASH_SIZE0;
331             break;
332 
333         case DRV_MMIO_OTP_BANK:
334 #if defined (MCU_AEON)
335             bRet = FALSE;
336 #else
337             *virtBaseAddr = (MS_PHY)HAL_MMIO_OTP_BASE;
338             *pu32BaseSize = (MS_PHY)HAL_MMIO_OTP_SIZE;
339 #endif
340             break;
341         case DRV_MMIO_OTP_BANK2:
342 #if defined (MCU_AEON)
343             bRet = FALSE;
344 #else
345             *virtBaseAddr = (MS_PHY)HAL_MMIO_OTP_RAW_BASE;
346             *pu32BaseSize = (MS_PHY)HAL_MMIO_OTP_SIZE;
347 #endif
348             break;
349 
350         default:
351 //          MS_ASSERT(0);
352             bRet = FALSE;
353             break;
354     }
355 
356     return bRet;
357 }
358 
359 
360 // @NOTE: Only run after MMIO_Init
HAL_MMIO_GetIPBase(MS_VIRT * virtBaseAddr,MS_U16 u16BankType)361 MS_BOOL HAL_MMIO_GetIPBase(MS_VIRT *virtBaseAddr, MS_U16 u16BankType)
362 {
363     *virtBaseAddr = 0;
364 
365     // if MMIO_Init is not yet initialized.
366     if ( (_u32PM_Bank_SIZE == 0x0) || (_u32NonPM_Bank_SIZE == 0x0) )
367     {
368         return FALSE;
369     }
370 
371     switch (u16BankType)
372     {
373     case DRV_MMIO_SC_BANK:
374         *virtBaseAddr =_virtNonPM_Bank  + 0x00005200;
375         break;
376     case DRV_MMIO_SC1_BANK:
377         *virtBaseAddr =_virtNonPM_Bank  + 0x00005400;
378         break;
379     default:
380         return FALSE;
381     }
382 
383     return TRUE;
384 }
385 
386 #if defined(__aarch64__) || defined(__arm__)
387 #if !defined (MSOS_TYPE_LINUX) && !defined (MSOS_TYPE_LINUX_KERNEL)
_chip_flush_miu_pipe(void)388 static void _chip_flush_miu_pipe(void)
389 {
390     unsigned int    dwReadData = 0;
391 
392 
393      //toggle the flush miu pipe fire bit
394     *(volatile unsigned int *)(HAL_MMIO_PM_BASE + (0x10188A << 1)) &= ~(0x0001);
395     *(volatile unsigned int *)(HAL_MMIO_PM_BASE + (0x10188A << 1)) |= 0x0001;
396 
397     do
398     {
399         dwReadData = *(volatile unsigned int *)(HAL_MMIO_PM_BASE+ (0x1018A0 << 1));
400         dwReadData &= BIT(12);  //Check Status of Flush Pipe Finish
401 
402     } while(dwReadData == 0);
403 }
404 #endif
405 #endif
406 
HAL_MMIO_FlushMemory(void)407 void HAL_MMIO_FlushMemory(void)
408 {
409 #if defined(__aarch64__) || defined(__arm__)
410 
411 #if !defined (MSOS_TYPE_LINUX) && !defined (MSOS_TYPE_LINUX_KERNEL)
412     _chip_flush_miu_pipe();
413 #endif
414 #endif
415 }
416 
HAL_MMIO_ReadMemory(void)417 void HAL_MMIO_ReadMemory(void)
418 {
419 #if defined(__aarch64__) || defined(__arm__)
420 #if !defined (MSOS_TYPE_LINUX) && !defined (MSOS_TYPE_LINUX_KERNEL)
421     _chip_flush_miu_pipe();
422 #endif
423 #endif
424 }
425 
426 
427