blob: 581cd0c4696eea18e9ad19b8169f461e62845c12 [file] [log] [blame]
Cyril Bur314929b2016-10-14 15:55:16 +11001/* Copyright 2016 IBM
2 *
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +11007 * http://www.apache.org/licenses/LICENSE-2.0
Cyril Bur314929b2016-10-14 15:55:16 +11008 *
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +11009 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
Cyril Bur314929b2016-10-14 15:55:16 +110014 *
15 */
16
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +110017#ifndef COMMON_H
18#define COMMON_H
19
Andrew Jeffery36a39f62017-04-10 16:25:04 +093020#include <stdarg.h>
21#include <stdbool.h>
22#include <stdint.h>
23
Cyril Bur314929b2016-10-14 15:55:16 +110024#ifndef PREFIX
25#define PREFIX ""
26#endif
27
28enum {
29 MBOX_LOG_NONE = 0,
Suraj Jitindar Singh28519592017-04-27 14:48:58 +100030 MBOX_LOG_INFO = 1,
Cyril Bur314929b2016-10-14 15:55:16 +110031 MBOX_LOG_DEBUG = 2
32} verbosity;
33
Suraj Jitindar Singh28519592017-04-27 14:48:58 +100034/* Error Messages */
35#define MSG_ERR(f_, ...) mbox_log(LOG_ERR, f_, ##__VA_ARGS__)
36/* Informational Messages */
37#define MSG_INFO(f_, ...) do { if (verbosity >= MBOX_LOG_INFO) { \
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +110038 mbox_log(LOG_INFO, f_, ##__VA_ARGS__); \
39 } } while (0)
Suraj Jitindar Singh28519592017-04-27 14:48:58 +100040/* Debug Messages */
41#define MSG_DBG(f_, ...) do { if (verbosity >= MBOX_LOG_DEBUG) { \
42 mbox_log(LOG_DEBUG, f_, ##__VA_ARGS__); \
43 } } while(0)
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +110044
Cyril Bur314929b2016-10-14 15:55:16 +110045void (*mbox_vlog)(int p, const char *fmt, va_list args);
46
47void mbox_log_console(int p, const char *fmt, va_list args);
48
49__attribute__((format(printf, 2, 3)))
50void mbox_log(int p, const char *fmt, ...);
51
52uint16_t get_u16(uint8_t *ptr);
53
54void put_u16(uint8_t *ptr, uint16_t val);
55
56uint32_t get_u32(uint8_t *ptr);
57
58void put_u32(uint8_t *ptr, uint32_t val);
59
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +110060static inline uint32_t align_up(uint32_t val, uint32_t size)
61{
62 return (((val) + (size) - 1) & ~((size) - 1));
63}
64
65static inline uint32_t align_down(uint32_t val, uint32_t size)
66{
67 return ((val) & ~(((size) - 1)));
68}
69
70static inline uint32_t min_u32(uint32_t a, uint32_t b)
71{
72 if (a <= b) {
73 return a;
74 }
75
76 return b;
77}
78
79static inline int log_2(int val)
80{
81 int ret = 0;
82
83 if (val <= 0) {
84 return -1;
85 }
86
87 while (val >>= 1) {
88 ret++;
89 }
90
91 return ret;
92}
93
Suraj Jitindar Singh0aff80c2017-04-12 14:37:24 +100094static inline bool is_power_of_2(unsigned val)
95{
96 return __builtin_popcount(val) == 1;
97}
98
Cyril Bur314929b2016-10-14 15:55:16 +110099char *get_dev_mtd(void);
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100100
101#endif /* COMMON_H */