1 /*
2  * Copyright (C) 2017 ALLWINNERTECH TECHNOLOGY CO., LTD. All rights reserved.
3  *
4  *  Redistribution and use in source and binary forms, with or without
5  *  modification, are permitted provided that the following conditions
6  *  are met:
7  *    1. Redistributions of source code must retain the above copyright
8  *       notice, this list of conditions and the following disclaimer.
9  *    2. Redistributions in binary form must reproduce the above copyright
10  *       notice, this list of conditions and the following disclaimer in the
11  *       documentation and/or other materials provided with the
12  *       distribution.
13  *    3. Neither the name of ALLWINNERTECH TECHNOLOGY CO., LTD. nor the names of
14  *       its contributors may be used to endorse or promote products derived
15  *       from this software without specific prior written permission.
16  *
17  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18  *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19  *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20  *  A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21  *  OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22  *  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23  *  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24  *  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25  *  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26  *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27  *  OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  */
29 
30 #ifndef _ROM_DEBUG_H_
31 #define _ROM_DEBUG_H_
32 
33 #include <stdio.h>
34 #ifndef CONFIG_KERNEL_FREERTOS
35 #include <log.h>
36 #endif
37 
38 #include "sys/sys_debug.h"
39 
40 
41 /* debug */
42 #ifndef CONFIG_KERNEL_FREERTOS
43 #define ROM_SYSLOG      printk
44 #else
45 #define ROM_SYSLOG      printf
46 #endif
47 
48 #define ROM_DUMP_ON     1
49 #define ROM_DBG_ON      1
50 #define ROM_INF_ON      1
51 #define ROM_WRN_ON      1
52 #define ROM_ERR_ON      1
53 #define ROM_ANY_ON      1
54 #define ROM_ABORT_ON    0
55 
56 #ifndef sys_abort
57 #define sys_abort()     ({volatile int i = 1; printf("%s, %d\n", __FUNCTION__, __LINE__); while(i);})
58 #endif
59 
60 #if ROM_ABORT_ON
61 #define ROM_ABORT()     sys_abort()
62 #else
63 #define ROM_ABORT()
64 #endif
65 
66 #if ROM_DUMP_ON
67 #define ROM_DUMP_BYTES(level, addr, len)                \
68     do {                                            \
69         if (level & ROM_DUMP_MASK)              \
70             print_hex_dump_bytes(addr, len);\
71     } while (0)
72 #define ROM_DUMP_WORDS(level, addr, len)                \
73     do {                                            \
74         if (level & ROM_DUMP_MASK)              \
75             print_hex_dump_words(addr, len);\
76     } while (0)
77 #else
78 #define ROM_DUMP_BYTES(level, addr, len)
79 #define ROM_DUMP_WORDS(level, addr, len)
80 #endif
81 
82 #define ROM_LOG(level, mask, expand, fmt, arg...)       \
83     do {                                            \
84         if (level & mask)                       \
85             ROM_SYSLOG(expand fmt, ##arg);  \
86     } while (0)
87 
88 #if ROM_DBG_ON
89 #define ROM_DBG(level, fmt, arg...) ROM_LOG(level, ROM_DBG_MASK, "[DBG]", fmt, ##arg)
90 #else
91 #define ROM_DBG(level, fmt, arg...)
92 #endif
93 
94 #if ROM_INF_ON
95 #define ROM_INF(level, fmt, arg...) ROM_LOG(level, ROM_INF_MASK, "", fmt, ##arg)
96 #else
97 #define ROM_INF(level, fmt, arg...)
98 #endif
99 
100 #if ROM_WRN_ON
101 #define ROM_WRN(level, fmt, arg...) ROM_LOG(level, ROM_WRN_MASK, "[WRN] ", fmt, ##arg)
102 #else
103 #define ROM_WRN(level, fmt, arg...)
104 #endif
105 
106 #if ROM_ERR_ON
107 #define ROM_ERR(level, fmt, arg...)                     \
108     do {                                            \
109         if (level & ROM_ERR_MASK)               \
110             ROM_SYSLOG("[ERR] "fmt, ##arg); \
111         if (level & ROM_ABORT_ON) {               \
112             ROM_ABORT();            \
113         }                    \
114     } while (0)
115 #else
116 #define ROM_ERR(level, fmt, arg...)
117 #endif
118 
119 #if ROM_ANY_ON
120 #define ROM_ANY(level, fmt, arg...)                     \
121     do {                                \
122         if (level & ROM_ANY_MASK)               \
123             ROM_SYSLOG(fmt, ##arg);         \
124     } while (0)
125 #else
126 #define ROM_ANY(level, fmt, arg...)
127 #endif
128 
129 #define ROM_ASSERT_PARAM(exp)                                           \
130     do {                                                                \
131         if (!(exp)) {                                                   \
132             ROM_SYSLOG("Invalid param at %s:%d\n", __func__, __LINE__); \
133         }                                                               \
134     } while (0)
135 
136 #define ROM_BUG_ON(v) do {if(v) {printf("BUG at %s:%d!\n", __func__, __LINE__); ROM_ABORT();}} while (0)
137 #define ROM_WARN_ON(v) do {if(v) {printf("WARN at %s:%d!\n", __func__, __LINE__);}} while (0)
138 
139 #ifdef __CONFIG_ROM
140 
141 #define ROM_IT_DBG      ROM_DBG
142 #define ROM_IT_INF      ROM_INF
143 #define ROM_IT_WRN      ROM_WRN
144 #define ROM_IT_ERR      ROM_ERR
145 #define ROM_IT_ANY      ROM_ANY
146 
147 #ifndef __CONFIG_SECTION_ATTRIBUTE_NONXIP
148 #define __s_func        __func__
149 #endif
150 
151 #else
152 
153 /* debug in interrupt handler */
154 #ifdef __CONFIG_SECTION_ATTRIBUTE_NONXIP
155 
156 #define ROM_IT_LOG(mask, fmt, arg...)                   \
157     do {                                                \
158         if (mask & ROM_DBG_MASK) {                      \
159             __nonxip_rodata static const char __fmt[] = fmt;  \
160             ROM_SYSLOG(__fmt, ##arg);                   \
161         }                                               \
162     } while (0)
163 
164 #define ROM_IT_DBG(mask, fmt, arg...)                   \
165     do {                            \
166         if (mask & ROM_DBG_MASK) {             \
167         __nonxip_rodata static const char __fmt[] = fmt;  \
168         ROM_SYSLOG(__fmt, ##arg);           \
169         }                           \
170     } while (0)
171 
172 #define ROM_IT_WRN(mask, fmt, arg...) \
173     do {                        \
174         if (mask & ROM_WRN_MASK) {             \
175         __nonxip_rodata static const char __fmt[] = "[WRN] "fmt; \
176         ROM_SYSLOG(__fmt, ##arg);           \
177         }                           \
178     } while (0)
179 
180 #define ROM_IT_ERR(mask, fmt, arg...)                           \
181     do {                                                    \
182         if (mask & ROM_ERR_MASK) {             \
183             __nonxip_rodata static const char __fmt[] = "[ERR] %s():%d, "fmt;   \
184             ROM_SYSLOG(__fmt, __s_func, __LINE__, ##arg);      \
185         }                                                   \
186         if (mask & ROM_ABORT_MASK)                          \
187             ROM_ABORT();                                \
188     } while (0)
189 
190 #else /* __CONFIG_SECTION_ATTRIBUTE_NONXIP */
191 
192 #define __s_func        __func__
193 #define ROM_IT_DBG      ROM_DBG
194 #define ROM_IT_WRN      ROM_WRN
195 #define ROM_IT_ERR      ROM_ERR
196 
197 #endif /* __CONFIG_SECTION_ATTRIBUTE_NONXIP */
198 
199 #endif
200 
201 #endif /* _ROM_DEBUG_H_ */
202