xref: /utopia/UTPA2-700.0.x/projects/tmplib/include/MsCommon.h (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 //
81 // Copyright (c) 2008-2009 MStar Semiconductor, Inc.
82 // All rights reserved.
83 //
84 // Unless otherwise stipulated in writing, any and all information contained
85 // herein regardless in any format shall remain the sole proprietary of
86 // MStar Semiconductor Inc. and be kept in strict confidence
87 // ("MStar Confidential Information") by the recipient.
88 // Any unauthorized act including without limitation unauthorized disclosure,
89 // copying, use, reproduction, sale, distribution, modification, disassembling,
90 // reverse engineering and compiling of the contents of MStar Confidential
91 // Information is unlawful and strictly prohibited. MStar hereby reserves the
92 // rights to any and all damages, losses, costs and expenses resulting therefrom.
93 //
94 ////////////////////////////////////////////////////////////////////////////////
95 
96 ///////////////////////////////////////////////////////////////////////////////////////////////////
97 ///
98 /// @file   MsCommon.h
99 /// @brief  MStar Common Interface Header File
100 /// @author MStar Semiconductor Inc.
101 /// @note   MsCommon.h includes most command header files including basic data type, macro(MsTypes.h),\n
102 /// board configuration(MsBoard.h), and OS related API(MsOS.h).\n
103 ///////////////////////////////////////////////////////////////////////////////////////////////////
104 
105 #ifndef _MS_COMMON_H_
106 #define _MS_COMMON_H_
107 
108 #ifndef MSOS_TYPE_LINUX_KERNEL
109 #include <stdio.h>
110 #include <stdlib.h>
111 #else
112 #endif
113 
114 #ifdef MSOS_TYPE_ECOS
115 #include <cyg/infra/diag.h>
116 #endif
117 
118 #include <stdarg.h>
119 
120 #ifdef MSOS_TYPE_LINUX
121     #include <limits.h>
122 #else
123     #define ULONG_MAX 0xffffffff
124 #endif
125 
126 //#include <setjmp.h>
127 
128 #include "MsOS.h"                                                       // Plan to be obsoleted in next generation.
129 #include "MsTypes.h"
130 #include "MsIRQ.h"
131 //#include "MsVersion.h"
132 #include "MsDevice.h"
133 
134 #ifdef CONFIG_ENABLE_MENUCONFIG
135     #include "autoconf.h"
136 #endif
137 //-------------------------------------------------------------------------------------------------
138 // Defines
139 //-------------------------------------------------------------------------------------------------
140 #ifndef DLL_PUBLIC
141 #define DLL_PUBLIC __attribute__ ((visibility ("default")))
142 #endif
143 #ifndef DLL_LOCAL
144 #define DLL_LOCAL __attribute__ ((visibility ("hidden")))
145 #endif
146 
147 #define SYMBOL_WEAK __attribute__((weak))
148 
149 //-------------------------------------------------------------------------------------------------
150 // Macros
151 //-------------------------------------------------------------------------------------------------
152 #define GEN_EXCEP   { while(1); }
153 
154 //#define REG(addr) (*(volatile U32 *)(addr))
155 #if 0
156 #ifdef __aeon__
157 
158 //-------------------------------------------------------------------------------------------------
159 //  In order to keep the compatiblity of the source code from Venus,
160 //  keep these memory address translation for a while.
161 //  They will be removed in the future.
162 //-------------------------------------------------------------------------------------------------
163 #define CACHED_BASE            ((void *)0x00000000)
164 #define UNCACHED_BASE       ((void *)0x80000000)
165 
166 #define CACHED_SIZE         ((void *)0x20000000)
167 #define UNCACHED_SIZE        ((void *)0x20000000)
168 //  0xA0000000~0xA000FFFF belongs to RIU
169 //  0xA1000000~           belongs to SPI
170 
171 //cached/unchched segment
172 #define KSEG0_BASE        CACHED_BASE
173 #define KSEG1_BASE        UNCACHED_BASE
174 #define KSEG0_SIZE        CACHED_SIZE
175 #define KSEG1_SIZE        UNCACHED_SIZE
176 
177 //cached <-> uncached
178 #define KSEG02KSEG1(addr)  ((void *)((U32)(addr)|0x80000000))
179 #define KSEG12KSEG0(addr)  ((void *)((U32)(addr)&~0x80000000))
180 
181 //virtual <-> physical
182 #define VA2PA(addr)         ((void *)(((U32)addr) & 0x1fffffff))
183 #define PA2KSEG0(addr)         ((void *)(((U32)addr) | 0x00000000))
184 #define PA2KSEG1(addr)         ((void *)(((U32)addr) | 0x80000000))
185 #endif
186 
187 #if defined(__mips__)
188 
189 //cached/unchched segment
190 #define KSEG0_BASE                ((void *)0x80000000)
191 #define KSEG1_BASE                ((void *)0xa0000000)
192 #define KSEG0_SIZE                0x20000000
193 #define KSEG1_SIZE                0x20000000
194 
195 //cached addr <-> unchched addr
196 #define KSEG02KSEG1(addr)       ((void *)((MS_U32)(addr)|0x20000000))  //cached -> unchched
197 #define KSEG12KSEG0(addr)       ((void *)((MS_U32)(addr)&~0x20000000)) //unchched -> cached
198 
199 //virtual addr <-> physical addr
200 #define VA2PA(addr)             ((void *)(((MS_U32)addr) & 0x1fffffff)) //virtual -> physical
201 #define PA2KSEG0(addr)             ((void *)(((MS_U32)addr) | 0x80000000)) //physical -> cached
202 #define PA2KSEG1(addr)             ((void *)(((MS_U32)addr) | 0xa0000000)) //physical -> unchched
203 #endif
204 #endif
205 
206 #if 0
207 //cached/unchched segment
208 #define KSEG0_BASE        ((void *)0x80000000)
209 #define KSEG1_BASE        ((void *)0xa0000000)
210 #define KSEG0_SIZE        0x20000000
211 #define KSEG1_SIZE        0x20000000
212 
213 //cached <-> unchched
214 #define KSEG02KSEG1(addr)  ((void *)((U32)(addr)|0x20000000))
215 #define KSEG12KSEG0(addr)  ((void *)((U32)(addr)&~0x20000000))
216 
217 //virtual <-> physical
218 #define VA2PA(addr)         ((void *)(((U32)addr) & 0x1fffffff))
219 #define PA2KSEG0(addr)         ((void *)(((U32)addr) | 0x80000000))
220 #define PA2KSEG1(addr)         ((void *)(((U32)addr) | 0xa0000000))
221 #endif
222 
223 //user-defined warning
224 #ifdef MSOS_TYPE_ECOS
225     #ifdef BLOCK_ASSERT
226         #define MS_ASSERT(_bool_)                                                                                           \
227                 {                                                                                                           \
228                     if ( ! ( _bool_ ) )                                                                                     \
229                     {                                                                                                       \
230                        diag_printf("\033[35m UTOPIA ASSERT: %s, %s %s %d\033[35m\n", #_bool_, __FILE__, __PRETTY_FUNCTION__, __LINE__);       \
231                         *(volatile unsigned int *)(ULONG_MAX) = 0;                                                         \
232                     }                                                                                                       \
233                 }
234     #else
235         #define MS_ASSERT(_bool_)                                                                                           \
236                 {                                                                                                           \
237                     if ( ! ( _bool_ ) )                                                                                     \
238                     {                                                                                                       \
239                        diag_printf("\033[35m UTOPIA ASSERT: %s, %s %s %d\033[35m\n", #_bool_, __FILE__, __PRETTY_FUNCTION__, __LINE__);       \
240                     }                                                                                                       \
241                 }
242     #endif
243 #else
244 #ifdef MSOS_TYPE_LINUX_KERNEL
245     #ifdef BLOCK_ASSERT
246         #define MS_ASSERT(_bool_)                                                                                   \
247                 {                                                                                                   \
248                     if (!(_bool_))                                                                                  \
249                     {                                                                                               \
250                         printk("\033[35m ASSERT FAIL: %s, %s %s %d\033[35m\n", #_bool_, __FILE__, __PRETTY_FUNCTION__, __LINE__);    \
251                         panic("mstar panic\n");                                                                     \
252                         *(volatile unsigned int *)(ULONG_MAX) = 0;                                                         \
253                     }                                                                                               \
254                 }
255     #else
256         #define MS_ASSERT(_bool_)                                                                                   \
257                 {                                                                                                   \
258                     if (!(_bool_))                                                                                  \
259                     {                                                                                               \
260                         printk("\033[35m ASSERT FAIL: %s, %s %s %d\033[35m\n", #_bool_, __FILE__, __PRETTY_FUNCTION__, __LINE__);    \
261                         panic("mstar panic\n");                                                                     \
262                     }                                                                                               \
263                 }
264     #endif
265 #else
266     #ifdef BLOCK_ASSERT
267         #define MS_ASSERT(_bool_)                                                                                       \
268                 {                                                                                                       \
269                     if ( ! ( _bool_ ) )                                                                                 \
270                     {                                                                                                   \
271                         printf("\033[35m UTOPIA ASSERT: %s, %s %s %d\033[35m\n", #_bool_, __FILE__, __PRETTY_FUNCTION__, __LINE__);       \
272                         *(volatile unsigned int *)(ULONG_MAX) = 0;                                                         \
273                     }                                                                                                   \
274                 }
275     #else
276         #define MS_ASSERT(_bool_)                                                                                       \
277                 {                                                                                                       \
278                     if ( ! ( _bool_ ) )                                                                                 \
279                     {                                                                                                   \
280                         printf("\033[35m UTOPIA ASSERT: %s, %s %s %d\033[35m\n", #_bool_, __FILE__, __PRETTY_FUNCTION__, __LINE__);       \
281                     }                                                                                                   \
282                 }
283     #endif
284 #endif
285 #endif
286 
287 //user-defined panic
288 //access null pointer 0xFFFFFFFF to make exception
289 #ifdef MSOS_TYPE_ECOS
290 #define MS_PANIC(_bool_)                                                                                           \
291         {                                                                                                           \
292             if ( ! ( _bool_ ) )                                                                                     \
293             {                                                                                                       \
294                 diag_printf("\033[35m UTOPIA ASSERT: %s %s %s %d\033[35m\n", #_bool_, __FILE__, __PRETTY_FUNCTION__, __LINE__);       \
295                 *(volatile unsigned int *)(ULONG_MAX) = 0;                                                         \
296             }                                                                                                       \
297         }
298 #else
299 #define MS_PANIC(_bool_)                                                                                       \
300         {                                                                                                       \
301             if ( ! ( _bool_ ) )                                                                                 \
302             {                                                                                                   \
303                 printf("\033[35m UTOPIA ASSERT: %s %s %s %d\033[35m\n", #_bool_, __FILE__, __PRETTY_FUNCTION__, __LINE__);       \
304                 *(volatile unsigned int *)(ULONG_MAX) = 0;                                                         \
305             }                                                                                                   \
306         }
307 #endif
308 
309 
310 #ifndef MIN
311 #define MIN(_a_, _b_)               ((_a_) < (_b_) ? (_a_) : (_b_))
312 #endif
313 #ifndef MAX
314 #define MAX(_a_, _b_)               ((_a_) > (_b_) ? (_a_) : (_b_))
315 #endif
316 
317 
318 #define ALIGN_4(_x_)                (((_x_) + 3) & ~3)
319 #define ALIGN_8(_x_)                (((_x_) + 7) & ~7)
320 #define ALIGN_16(_x_)               (((_x_) + 15) & ~15)           // No data type specified, optimized by complier
321 #define ALIGN_32(_x_)               (((_x_) + 31) & ~31)           // No data type specified, optimized by complier
322 
323 #define MASK(x)     (((1<<(x##_BITS))-1) << x##_SHIFT)
324 #ifndef BIT    //for Linux_kernel type, BIT redefined in <linux/bitops.h>
325 #define BIT(_bit_)                  (1 << (_bit_))
326 #endif
327 #define BIT_(x)                     BIT(x) //[OBSOLETED] //TODO: remove it later
328 #define BITS(_bits_, _val_)         ((BIT(((1)?_bits_)+1)-BIT(((0)?_bits_))) & (_val_<<((0)?_bits_)))
329 #define BMASK(_bits_)               (BIT(((1)?_bits_)+1)-BIT(((0)?_bits_)))
330 
331 
332 #define READ_BYTE(_reg)             (*(volatile MS_U8*)(_reg))
333 #define READ_WORD(_reg)             (*(volatile MS_U16*)(_reg))
334 #define READ_LONG(_reg)             (*(volatile MS_U32*)(_reg))
335 #define WRITE_BYTE(_reg, _val)      { (*((volatile MS_U8*)(_reg))) = (MS_U8)(_val); }
336 #define WRITE_WORD(_reg, _val)      { (*((volatile MS_U16*)(_reg))) = (MS_U16)(_val); }
337 #define WRITE_LONG(_reg, _val)      { (*((volatile MS_U32*)(_reg))) = (MS_U32)(_val); }
338 
339 
340 #endif // _MS_COMMON_H_
341