xref: /utopia/UTPA2-700.0.x/mxlib/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 CONFIG_ENABLE_MENUCONFIG
121     #include "autoconf.h"
122 #endif
123 
124 #ifdef MSOS_TYPE_LINUX
125     #include <limits.h>
126 #elif defined(MSOS_TYPE_ECOS)
127     #include <limits.h>
128 #elif defined(MSOS_TYPE_OPTEE)
129     #include <limits.h>
130 #else
131     #ifndef MSOS_TYPE_LINUX_KERNEL
132 	#ifndef MSOS_TYPE_OPTEE
133 		#define ULONG_MAX 0xffffffff
134     #else
135 		//#define ULONG_MAX 0xffffffff
136     #endif
137     #endif
138 #endif
139 
140 //#include <setjmp.h>
141 
142 #include "MsOS.h"                                                       // Plan to be obsoleted in next generation.
143 #include "MsTypes.h"
144 #include "MsIRQ.h"
145 //#include "MsVersion.h"
146 #include "MsDevice.h"
147 
148 #ifdef CONFIG_ENABLE_MENUCONFIG
149     #include "autoconf.h"
150 #endif
151 //-------------------------------------------------------------------------------------------------
152 // Defines
153 //-------------------------------------------------------------------------------------------------
154 #ifndef DLL_PUBLIC
155 #define DLL_PUBLIC __attribute__ ((visibility ("default")))
156 #endif
157 #ifndef DLL_LOCAL
158 #define DLL_LOCAL __attribute__ ((visibility ("hidden")))
159 #endif
160 
161 #ifndef MSOS_TYPE_LINUX_KERNEL
162 #define SYMBOL_WEAK __attribute__((weak))
163 #else
164 #define SYMBOL_WEAK
165 #endif
166 
167 //-------------------------------------------------------------------------------------------------
168 // Macros
169 //-------------------------------------------------------------------------------------------------
170 #define GEN_EXCEP   { while(1); }
171 
172 //#define REG(addr) (*(volatile U32 *)(addr))
173 #if 0
174 #ifdef __aeon__
175 
176 //-------------------------------------------------------------------------------------------------
177 //  In order to keep the compatiblity of the source code from Venus,
178 //  keep these memory address translation for a while.
179 //  They will be removed in the future.
180 //-------------------------------------------------------------------------------------------------
181 #define CACHED_BASE            ((void *)0x00000000)
182 #define UNCACHED_BASE       ((void *)0x80000000)
183 
184 #define CACHED_SIZE         ((void *)0x20000000)
185 #define UNCACHED_SIZE        ((void *)0x20000000)
186 //  0xA0000000~0xA000FFFF belongs to RIU
187 //  0xA1000000~           belongs to SPI
188 
189 //cached/unchched segment
190 #define KSEG0_BASE        CACHED_BASE
191 #define KSEG1_BASE        UNCACHED_BASE
192 #define KSEG0_SIZE        CACHED_SIZE
193 #define KSEG1_SIZE        UNCACHED_SIZE
194 
195 //cached <-> uncached
196 #define KSEG02KSEG1(addr)  ((void *)((U32)(addr)|0x80000000))
197 #define KSEG12KSEG0(addr)  ((void *)((U32)(addr)&~0x80000000))
198 
199 //virtual <-> physical
200 #define VA2PA(addr)         ((void *)(((U32)addr) & 0x1fffffff))
201 #define PA2KSEG0(addr)         ((void *)(((U32)addr) | 0x00000000))
202 #define PA2KSEG1(addr)         ((void *)(((U32)addr) | 0x80000000))
203 #endif
204 
205 #if defined(__mips__)
206 
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 addr <-> unchched addr
214 #define KSEG02KSEG1(addr)       ((void *)((MS_U32)(addr)|0x20000000))  //cached -> unchched
215 #define KSEG12KSEG0(addr)       ((void *)((MS_U32)(addr)&~0x20000000)) //unchched -> cached
216 
217 //virtual addr <-> physical addr
218 #define VA2PA(addr)             ((void *)(((MS_U32)addr) & 0x1fffffff)) //virtual -> physical
219 #define PA2KSEG0(addr)             ((void *)(((MS_U32)addr) | 0x80000000)) //physical -> cached
220 #define PA2KSEG1(addr)             ((void *)(((MS_U32)addr) | 0xa0000000)) //physical -> unchched
221 #endif
222 #endif
223 
224 #if 0
225 //cached/unchched segment
226 #define KSEG0_BASE        ((void *)0x80000000)
227 #define KSEG1_BASE        ((void *)0xa0000000)
228 #define KSEG0_SIZE        0x20000000
229 #define KSEG1_SIZE        0x20000000
230 
231 //cached <-> unchched
232 #define KSEG02KSEG1(addr)  ((void *)((U32)(addr)|0x20000000))
233 #define KSEG12KSEG0(addr)  ((void *)((U32)(addr)&~0x20000000))
234 
235 //virtual <-> physical
236 #define VA2PA(addr)         ((void *)(((U32)addr) & 0x1fffffff))
237 #define PA2KSEG0(addr)         ((void *)(((U32)addr) | 0x80000000))
238 #define PA2KSEG1(addr)         ((void *)(((U32)addr) | 0xa0000000))
239 #endif
240 
241 //user-defined warning
242 #ifdef MSOS_TYPE_ECOS
243     #ifdef BLOCK_ASSERT
244         #define MS_ASSERT(_bool_)                                                                                   		\
245                 {                                                                                                   		\
246                     if ( ! ( _bool_ ) )                                                                             		\
247                     {                                                                                               		\
248                        diag_printf("\033[35m UTOPIA ASSERT: %s, %s %s %d\033[35m\n", #_bool_, __FILE__, __PRETTY_FUNCTION__, __LINE__);   	\
249                         *(volatile unsigned int *)(ULONG_MAX) = 0;                                                         \
250                     }                                                                                               		\
251                 }
252     #else
253         #define MS_ASSERT(_bool_)                                                                                   		\
254                 {                                                                                                   		\
255                     if ( ! ( _bool_ ) )                                                                             		\
256                     {                                                                                               		\
257                        diag_printf("\033[35m UTOPIA ASSERT: %s, %s %s %d\033[35m\n", #_bool_, __FILE__, __PRETTY_FUNCTION__, __LINE__);   	\
258                     }                                                                                               		\
259                 }
260     #endif
261 #else
262 #ifdef MSOS_TYPE_LINUX_KERNEL
263     #ifdef BLOCK_ASSERT
264         #define MS_ASSERT(_bool_)                                                                                   \
265                 {                                                                                                   \
266                     if (!(_bool_))                                                                                  \
267                     {                                                                                               \
268                         printk("\033[35m ASSERT FAIL: %s, %s %s %d\033[35m\n", #_bool_, __FILE__, __PRETTY_FUNCTION__, __LINE__);    \
269                         panic("mstar panic\n");                                                                     \
270                         *(volatile unsigned int *)(ULONG_MAX) = 0;                                                         \
271                     }                                                                                               \
272                 }
273     #else
274         #define MS_ASSERT(_bool_)                                                                                   \
275                 {                                                                                                   \
276                     if (!(_bool_))                                                                                  \
277                     {                                                                                               \
278                         printk("\033[35m ASSERT FAIL: %s, %s %s %d\033[35m\n", #_bool_, __FILE__, __PRETTY_FUNCTION__, __LINE__);    \
279                         panic("mstar panic\n");                                                                     \
280                     }                                                                                               \
281                 }
282     #endif
283 #else
284     #ifdef BLOCK_ASSERT
285         #define MS_ASSERT(_bool_)                                                                                   	\
286                 {                                                                                                   	\
287                     if ( ! ( _bool_ ) )                                                                             	\
288                     {                                                                                               	\
289                         printf("\033[35m UTOPIA ASSERT: %s, %s %s %d\033[35m\n", #_bool_, __FILE__, __PRETTY_FUNCTION__, __LINE__);   	\
290                         *(volatile unsigned int *)(ULONG_MAX) = 0;                                                         \
291                     }                                                                                               	\
292                 }
293     #else
294         #define MS_ASSERT(_bool_)                                                                                   	\
295                 {                                                                                                   	\
296                     if ( ! ( _bool_ ) )                                                                             	\
297                     {                                                                                               	\
298                         printf("\033[35m UTOPIA ASSERT: %s, %s %s %d\033[35m\n", #_bool_, __FILE__, __PRETTY_FUNCTION__, __LINE__);   	\
299                     }                                                                                               	\
300                 }
301     #endif
302 #endif
303 #endif
304 
305 //user-defined panic
306 //access null pointer 0xFFFFFFFF to make exception
307 #ifdef MSOS_TYPE_ECOS
308 #define MS_PANIC(_bool_)                                                                                   	    \
309         {                                                                                                   		\
310             if ( ! ( _bool_ ) )                                                                             		\
311             {                                                                                               		\
312                 diag_printf("\033[35m UTOPIA ASSERT: %s %s %s %d\033[35m\n", #_bool_, __FILE__, __PRETTY_FUNCTION__, __LINE__);   	\
313                 *(volatile unsigned int *)(ULONG_MAX) = 0;                                                         \
314             }                                                                                               		\
315         }
316 #elif defined(MSOS_TYPE_OPTEE)
317 #define MS_PANIC(_bool_)                                                                                   	\
318         {                                                                                                   	\
319             if ( ! ( _bool_ ) )                                                                             	\
320             {                                                                                               	\
321                 printf("\033[35m UTOPIA ASSERT: %s %s %s %d\033[35m\n", #_bool_, __FILE__, __PRETTY_FUNCTION__, __LINE__);   	\
322                 while(1);                                                                                   	\
323             }                                                                                               	\
324         }
325 #else
326 #define MS_PANIC(_bool_)                                                                                   	\
327         {                                                                                                   	\
328             if ( ! ( _bool_ ) )                                                                             	\
329             {                                                                                               	\
330                 printf("\033[35m UTOPIA ASSERT: %s %s %s %d\033[35m\n", #_bool_, __FILE__, __PRETTY_FUNCTION__, __LINE__);   	\
331                 *(volatile unsigned int *)(ULONG_MAX) = 0;                                                         \
332             }                                                                                               	\
333         }
334 #endif
335 
336 
337 #ifndef MIN
338 #define MIN(_a_, _b_)               ((_a_) < (_b_) ? (_a_) : (_b_))
339 #endif
340 #ifndef MAX
341 #define MAX(_a_, _b_)               ((_a_) > (_b_) ? (_a_) : (_b_))
342 #endif
343 
344 
345 #define ALIGN_4(_x_)                (((_x_) + 3) & ~3)
346 #define ALIGN_8(_x_)                (((_x_) + 7) & ~7)
347 #define ALIGN_16(_x_)               (((_x_) + 15) & ~15)           // No data type specified, optimized by complier
348 #define ALIGN_32(_x_)               (((_x_) + 31) & ~31)           // No data type specified, optimized by complier
349 
350 #define MASK(x)     (((1<<(x##_BITS))-1) << x##_SHIFT)
351 #ifndef BIT	//for Linux_kernel type, BIT redefined in <linux/bitops.h>
352 #define BIT(_bit_)                  (1 << (_bit_))
353 #endif
354 #define BIT_(x)                     BIT(x) //[OBSOLETED] //TODO: remove it later
355 #define BITS(_bits_, _val_)         ((BIT(((1)?_bits_)+1)-BIT(((0)?_bits_))) & (_val_<<((0)?_bits_)))
356 #define BMASK(_bits_)               (BIT(((1)?_bits_)+1)-BIT(((0)?_bits_)))
357 
358 
359 #define READ_BYTE(_reg)             (*(volatile MS_U8*)(_reg))
360 #define READ_WORD(_reg)             (*(volatile MS_U16*)(_reg))
361 #define READ_LONG(_reg)             (*(volatile MS_U32*)(_reg))
362 #define WRITE_BYTE(_reg, _val)      { (*((volatile MS_U8*)(_reg))) = (MS_U8)(_val); }
363 #define WRITE_WORD(_reg, _val)      { (*((volatile MS_U16*)(_reg))) = (MS_U16)(_val); }
364 #define WRITE_LONG(_reg, _val)      { (*((volatile MS_U32*)(_reg))) = (MS_U32)(_val); }
365 
366 
367 #if 0
368 typedef struct _cmd_mload_utopia_interface_s cmd_mload_utopia_interface;
369 struct _cmd_mload_utopia_interface_s
370 {
371   //menuload ring buffer dynamic allocation service
372   MS_PHY (*get_next_mload_ringbuf_write_ptr)(cmd_mload_utopia_interface *cmdinf);
373   MS_PHY (*update_mload_ringbuf_read_ptr)(cmd_mload_utopia_interface *cmdinf, MS_PHY next_read_ptr);
374   MS_S32 (*mload_copy_buf)(cmd_mload_utopia_interface *cmdinf, void *mload_buf, MS_U32 size,
375                                                                                 MS_U16 u16Alignment, MS_PHY *phyRetAddr);
376   MS_U16  (*check_buf_available)(cmd_mload_utopia_interface *cmdinf , MS_U32 cmdq_num);
377   MS_S32  (*write_dummy_reg_cmdq)(cmd_mload_utopia_interface *cmdinf, MS_U16 value);
378   MS_U32  (*read_dummy_reg_cmdq)(cmd_mload_utopia_interface *cmdinf);
379 
380   MS_S32 (*write_reg_cmdq_mask)(cmd_mload_utopia_interface *cmdinf, MS_U32 reg_addr, MS_U16 value,  MS_U16 write_mask);
381   MS_S32 (*write_reg_cmdq)(cmd_mload_utopia_interface *cmdinf, MS_U32 reg_addr, MS_U16 value);
382   MS_S32 (*cmdq_poll_reg_bits)(cmd_mload_utopia_interface *cmdinf, MS_U32 reg_addr, MS_U16 value,  MS_U16 write_mask);
383   MS_S32 (*cmdq_add_wait_event_cmd)(cmd_mload_utopia_interface *cmdinf, MS_U16 event);
384   MS_S32  (*cmdq_abort_command)(cmd_mload_utopia_interface *cmdinf);
385 
386   MS_S32 (*kick_off_cmdq)(cmd_mload_utopia_interface *cmdinf);
387 
388   MS_S32  (*is_cmdq_empty_idle)(cmd_mload_utopia_interface *cmdinf);
389 
390   VOID *ctx;
391 };
392 #endif
393 
394 #endif // _MS_COMMON_H_
395