Gabe Black | 0af03d2 | 2012-03-19 03:06:46 -0700 | [diff] [blame] | 1 | /* |
Gabe Black | 7c7b5ff | 2013-11-23 00:38:49 -0800 | [diff] [blame] | 2 | * This file is part of the libpayload project. |
| 3 | * |
Gabe Black | 0af03d2 | 2012-03-19 03:06:46 -0700 | [diff] [blame] | 4 | * Copyright (c) 2012 The Chromium OS Authors. |
| 5 | * |
Gabe Black | 7c7b5ff | 2013-11-23 00:38:49 -0800 | [diff] [blame] | 6 | * Redistribution and use in source and binary forms, with or without |
| 7 | * modification, are permitted provided that the following conditions |
| 8 | * are met: |
| 9 | * 1. Redistributions of source code must retain the above copyright |
| 10 | * notice, this list of conditions and the following disclaimer. |
| 11 | * 2. Redistributions in binary form must reproduce the above copyright |
| 12 | * notice, this list of conditions and the following disclaimer in the |
| 13 | * documentation and/or other materials provided with the distribution. |
| 14 | * 3. The name of the author may not be used to endorse or promote products |
| 15 | * derived from this software without specific prior written permission. |
Gabe Black | 0af03d2 | 2012-03-19 03:06:46 -0700 | [diff] [blame] | 16 | * |
Gabe Black | 7c7b5ff | 2013-11-23 00:38:49 -0800 | [diff] [blame] | 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND |
| 18 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 19 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 20 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE |
| 21 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 22 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 23 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 24 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 25 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 26 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 27 | * SUCH DAMAGE. |
Gabe Black | 0af03d2 | 2012-03-19 03:06:46 -0700 | [diff] [blame] | 28 | */ |
| 29 | |
| 30 | #ifndef _ENDIAN_H_ |
| 31 | #define _ENDIAN_H_ |
| 32 | |
Julius Werner | 8a1d11f | 2014-07-17 10:43:15 -0700 | [diff] [blame] | 33 | #include <arch/io.h> |
Gabe Black | 0af03d2 | 2012-03-19 03:06:46 -0700 | [diff] [blame] | 34 | #include <arch/types.h> |
| 35 | #include <libpayload-config.h> |
| 36 | |
Gabe Black | 1cb414d | 2013-03-08 04:38:13 -0800 | [diff] [blame] | 37 | static inline uint16_t swap_bytes16(uint16_t in) |
| 38 | { |
| 39 | return ((in & 0xFF) << 8) | ((in & 0xFF00) >> 8); |
| 40 | } |
| 41 | |
| 42 | static inline uint32_t swap_bytes32(uint32_t in) |
| 43 | { |
| 44 | return ((in & 0xFF) << 24) | ((in & 0xFF00) << 8) | |
| 45 | ((in & 0xFF0000) >> 8) | ((in & 0xFF000000) >> 24); |
| 46 | } |
| 47 | |
| 48 | static inline uint64_t swap_bytes64(uint64_t in) |
| 49 | { |
| 50 | return ((uint64_t)swap_bytes32((uint32_t)in) << 32) | |
| 51 | ((uint64_t)swap_bytes32((uint32_t)(in >> 32))); |
| 52 | } |
Gabe Black | 0af03d2 | 2012-03-19 03:06:46 -0700 | [diff] [blame] | 53 | |
Hung-Te Lin | e112b74 | 2013-03-13 18:08:29 +0800 | [diff] [blame] | 54 | /* Endian functions from glibc 2.9 / BSD "endian.h" */ |
Gabe Black | 0af03d2 | 2012-03-19 03:06:46 -0700 | [diff] [blame] | 55 | |
Stefan Reinauer | 1b4d394 | 2015-06-29 15:47:34 -0700 | [diff] [blame] | 56 | #if IS_ENABLED(CONFIG_LP_BIG_ENDIAN) |
Gabe Black | 0af03d2 | 2012-03-19 03:06:46 -0700 | [diff] [blame] | 57 | |
Hung-Te Lin | e112b74 | 2013-03-13 18:08:29 +0800 | [diff] [blame] | 58 | #define htobe16(in) (in) |
| 59 | #define htobe32(in) (in) |
| 60 | #define htobe64(in) (in) |
Gabe Black | 0af03d2 | 2012-03-19 03:06:46 -0700 | [diff] [blame] | 61 | |
Hung-Te Lin | e112b74 | 2013-03-13 18:08:29 +0800 | [diff] [blame] | 62 | #define htole16(in) swap_bytes16(in) |
| 63 | #define htole32(in) swap_bytes32(in) |
| 64 | #define htole64(in) swap_bytes64(in) |
Gabe Black | 0af03d2 | 2012-03-19 03:06:46 -0700 | [diff] [blame] | 65 | |
Stefan Reinauer | 1b4d394 | 2015-06-29 15:47:34 -0700 | [diff] [blame] | 66 | #elif IS_ENABLED(CONFIG_LP_LITTLE_ENDIAN) |
Gabe Black | 0af03d2 | 2012-03-19 03:06:46 -0700 | [diff] [blame] | 67 | |
Hung-Te Lin | e112b74 | 2013-03-13 18:08:29 +0800 | [diff] [blame] | 68 | #define htobe16(in) swap_bytes16(in) |
| 69 | #define htobe32(in) swap_bytes32(in) |
| 70 | #define htobe64(in) swap_bytes64(in) |
Gabe Black | 0af03d2 | 2012-03-19 03:06:46 -0700 | [diff] [blame] | 71 | |
Hung-Te Lin | e112b74 | 2013-03-13 18:08:29 +0800 | [diff] [blame] | 72 | #define htole16(in) (in) |
| 73 | #define htole32(in) (in) |
| 74 | #define htole64(in) (in) |
Gabe Black | 0af03d2 | 2012-03-19 03:06:46 -0700 | [diff] [blame] | 75 | |
| 76 | #else |
| 77 | |
| 78 | #error Cant tell if the CPU is little or big endian. |
| 79 | |
Edward O'Callaghan | 6bedc27 | 2014-02-12 21:53:44 +1100 | [diff] [blame] | 80 | #endif /* CONFIG_*_ENDIAN */ |
Gabe Black | 0af03d2 | 2012-03-19 03:06:46 -0700 | [diff] [blame] | 81 | |
Hung-Te Lin | e112b74 | 2013-03-13 18:08:29 +0800 | [diff] [blame] | 82 | #define be16toh(in) htobe16(in) |
| 83 | #define be32toh(in) htobe32(in) |
| 84 | #define be64toh(in) htobe64(in) |
Gabe Black | 0af03d2 | 2012-03-19 03:06:46 -0700 | [diff] [blame] | 85 | |
Hung-Te Lin | e112b74 | 2013-03-13 18:08:29 +0800 | [diff] [blame] | 86 | #define le16toh(in) htole16(in) |
| 87 | #define le32toh(in) htole32(in) |
| 88 | #define le64toh(in) htole64(in) |
Gabe Black | 0af03d2 | 2012-03-19 03:06:46 -0700 | [diff] [blame] | 89 | |
Hung-Te Lin | e112b74 | 2013-03-13 18:08:29 +0800 | [diff] [blame] | 90 | #define htonw(in) htobe16(in) |
| 91 | #define htonl(in) htobe32(in) |
| 92 | #define htonll(in) htobe64(in) |
Gabe Black | 0af03d2 | 2012-03-19 03:06:46 -0700 | [diff] [blame] | 93 | |
Hung-Te Lin | e112b74 | 2013-03-13 18:08:29 +0800 | [diff] [blame] | 94 | #define ntohw(in) be16toh(in) |
| 95 | #define ntohl(in) be32toh(in) |
| 96 | #define ntohll(in) be64toh(in) |
| 97 | |
Edward O'Callaghan | 6bedc27 | 2014-02-12 21:53:44 +1100 | [diff] [blame] | 98 | /* |
| 99 | * Alignment-agnostic encode/decode bytestream to/from little/big endian. |
| 100 | */ |
| 101 | |
| 102 | static inline uint16_t be16dec(const void *pp) |
| 103 | { |
| 104 | uint8_t const *p = (uint8_t const *)pp; |
| 105 | |
| 106 | return ((p[0] << 8) | p[1]); |
| 107 | } |
| 108 | |
| 109 | static inline uint32_t be32dec(const void *pp) |
| 110 | { |
| 111 | uint8_t const *p = (uint8_t const *)pp; |
| 112 | |
| 113 | return (((unsigned)p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3]); |
| 114 | } |
| 115 | |
| 116 | static inline uint16_t le16dec(const void *pp) |
| 117 | { |
| 118 | uint8_t const *p = (uint8_t const *)pp; |
| 119 | |
| 120 | return ((p[1] << 8) | p[0]); |
| 121 | } |
| 122 | |
| 123 | static inline uint32_t le32dec(const void *pp) |
| 124 | { |
| 125 | uint8_t const *p = (uint8_t const *)pp; |
| 126 | |
| 127 | return ((p[3] << 24) | (p[2] << 16) | (p[1] << 8) | p[0]); |
| 128 | } |
| 129 | |
| 130 | static inline void bebitenc(void *pp, uint32_t u, uint8_t b) |
| 131 | { |
| 132 | uint8_t *p = (uint8_t *)pp; |
| 133 | int i; |
| 134 | |
| 135 | for (i = (b - 1); i >= 0; i++) |
| 136 | p[i] = (u >> i*8) & 0xFF; |
| 137 | } |
| 138 | |
| 139 | static inline void be16enc(void *pp, uint16_t u) |
| 140 | { |
| 141 | bebitenc(pp, u, 2); |
| 142 | } |
| 143 | |
| 144 | static inline void be32enc(void *pp, uint32_t u) |
| 145 | { |
| 146 | bebitenc(pp, u, 4); |
| 147 | } |
| 148 | |
| 149 | static inline void lebitenc(void *pp, uint32_t u, uint8_t b) |
| 150 | { |
| 151 | uint8_t *p = (uint8_t *)pp; |
| 152 | int i; |
| 153 | |
| 154 | for (i = 0; i < b; i++) |
| 155 | p[i] = (u >> i*8) & 0xFF; |
| 156 | } |
| 157 | |
| 158 | static inline void le16enc(void *pp, uint16_t u) |
| 159 | { |
| 160 | lebitenc(pp, u, 2); |
| 161 | } |
| 162 | |
| 163 | static inline void le32enc(void *pp, uint32_t u) |
| 164 | { |
| 165 | lebitenc(pp, u, 4); |
| 166 | } |
| 167 | |
Hung-Te Lin | e112b74 | 2013-03-13 18:08:29 +0800 | [diff] [blame] | 168 | /* Deprecated names (not in glibc / BSD) */ |
| 169 | #define htobew(in) htobe16(in) |
| 170 | #define htobel(in) htobe32(in) |
| 171 | #define htobell(in) htobe64(in) |
| 172 | #define htolew(in) htole16(in) |
| 173 | #define htolel(in) htole32(in) |
| 174 | #define htolell(in) htole64(in) |
| 175 | #define betohw(in) be16toh(in) |
| 176 | #define betohl(in) be32toh(in) |
| 177 | #define betohll(in) be64toh(in) |
| 178 | #define letohw(in) le16toh(in) |
| 179 | #define letohl(in) le32toh(in) |
| 180 | #define letohll(in) le64toh(in) |
Gabe Black | 0af03d2 | 2012-03-19 03:06:46 -0700 | [diff] [blame] | 181 | |
Julius Werner | 8a1d11f | 2014-07-17 10:43:15 -0700 | [diff] [blame] | 182 | /* Handy bit manipulation macros */ |
| 183 | |
| 184 | #define clrsetbits_le32(addr, clear, set) writel(htole32((le32toh(readl(addr)) \ |
| 185 | & ~(clear)) | (set)), (addr)) |
| 186 | #define setbits_le32(addr, set) writel(htole32(le32toh(readl(addr)) \ |
| 187 | | (set)), (addr)) |
| 188 | #define clrbits_le32(addr, clear) writel(htole32(le32toh(readl(addr)) \ |
| 189 | & ~(clear)), (addr)) |
| 190 | |
| 191 | #define clrsetbits_be32(addr, clear, set) writel(htobe32((be32toh(readl(addr)) \ |
| 192 | & ~(clear)) | (set)), (addr)) |
| 193 | #define setbits_be32(addr, set) writel(htobe32(be32toh(readl(addr)) \ |
| 194 | | (set)), (addr)) |
| 195 | #define clrbits_be32(addr, clear) writel(htobe32(be32toh(readl(addr)) \ |
| 196 | & ~(clear)), (addr)) |
| 197 | |
Edward O'Callaghan | 6bedc27 | 2014-02-12 21:53:44 +1100 | [diff] [blame] | 198 | #endif /* _ENDIAN_H_ */ |