Cyril Bur | 314929b | 2016-10-14 15:55:16 +1100 | [diff] [blame] | 1 | /* 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 Singh | e39c916 | 2017-03-28 10:47:43 +1100 | [diff] [blame] | 7 | * http://www.apache.org/licenses/LICENSE-2.0 |
Cyril Bur | 314929b | 2016-10-14 15:55:16 +1100 | [diff] [blame] | 8 | * |
Suraj Jitindar Singh | e39c916 | 2017-03-28 10:47:43 +1100 | [diff] [blame] | 9 | * 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 Bur | 314929b | 2016-10-14 15:55:16 +1100 | [diff] [blame] | 14 | * |
| 15 | */ |
| 16 | |
Suraj Jitindar Singh | e39c916 | 2017-03-28 10:47:43 +1100 | [diff] [blame] | 17 | #ifndef COMMON_H |
| 18 | #define COMMON_H |
| 19 | |
Cyril Bur | 314929b | 2016-10-14 15:55:16 +1100 | [diff] [blame] | 20 | #ifndef PREFIX |
| 21 | #define PREFIX "" |
| 22 | #endif |
| 23 | |
| 24 | enum { |
| 25 | MBOX_LOG_NONE = 0, |
| 26 | MBOX_LOG_VERBOSE = 1, |
| 27 | MBOX_LOG_DEBUG = 2 |
| 28 | } verbosity; |
| 29 | |
Suraj Jitindar Singh | e39c916 | 2017-03-28 10:47:43 +1100 | [diff] [blame] | 30 | #define MSG_OUT(f_, ...) do { if (verbosity >= MBOX_LOG_DEBUG) { \ |
| 31 | mbox_log(LOG_INFO, f_, ##__VA_ARGS__); \ |
| 32 | } } while (0) |
| 33 | #define MSG_ERR(f_, ...) do { if (verbosity >= MBOX_LOG_VERBOSE) { \ |
| 34 | mbox_log(LOG_ERR, f_, ##__VA_ARGS__); \ |
| 35 | } } while (0) |
| 36 | |
Cyril Bur | 314929b | 2016-10-14 15:55:16 +1100 | [diff] [blame] | 37 | void (*mbox_vlog)(int p, const char *fmt, va_list args); |
| 38 | |
| 39 | void mbox_log_console(int p, const char *fmt, va_list args); |
| 40 | |
| 41 | __attribute__((format(printf, 2, 3))) |
| 42 | void mbox_log(int p, const char *fmt, ...); |
| 43 | |
| 44 | uint16_t get_u16(uint8_t *ptr); |
| 45 | |
| 46 | void put_u16(uint8_t *ptr, uint16_t val); |
| 47 | |
| 48 | uint32_t get_u32(uint8_t *ptr); |
| 49 | |
| 50 | void put_u32(uint8_t *ptr, uint32_t val); |
| 51 | |
Suraj Jitindar Singh | e39c916 | 2017-03-28 10:47:43 +1100 | [diff] [blame] | 52 | static inline uint32_t align_up(uint32_t val, uint32_t size) |
| 53 | { |
| 54 | return (((val) + (size) - 1) & ~((size) - 1)); |
| 55 | } |
| 56 | |
| 57 | static inline uint32_t align_down(uint32_t val, uint32_t size) |
| 58 | { |
| 59 | return ((val) & ~(((size) - 1))); |
| 60 | } |
| 61 | |
| 62 | static inline uint32_t min_u32(uint32_t a, uint32_t b) |
| 63 | { |
| 64 | if (a <= b) { |
| 65 | return a; |
| 66 | } |
| 67 | |
| 68 | return b; |
| 69 | } |
| 70 | |
| 71 | static inline int log_2(int val) |
| 72 | { |
| 73 | int ret = 0; |
| 74 | |
| 75 | if (val <= 0) { |
| 76 | return -1; |
| 77 | } |
| 78 | |
| 79 | while (val >>= 1) { |
| 80 | ret++; |
| 81 | } |
| 82 | |
| 83 | return ret; |
| 84 | } |
| 85 | |
Suraj Jitindar Singh | 0aff80c | 2017-04-12 14:37:24 +1000 | [diff] [blame] | 86 | static inline bool is_power_of_2(unsigned val) |
| 87 | { |
| 88 | return __builtin_popcount(val) == 1; |
| 89 | } |
| 90 | |
Cyril Bur | 314929b | 2016-10-14 15:55:16 +1100 | [diff] [blame] | 91 | char *get_dev_mtd(void); |
Suraj Jitindar Singh | e39c916 | 2017-03-28 10:47:43 +1100 | [diff] [blame] | 92 | |
| 93 | #endif /* COMMON_H */ |