blob: 2655634c934179c936bf59f21e67ad89c76f313e [file] [log] [blame]
Gabe Black0af03d22012-03-19 03:06:46 -07001/*
Gabe Black7c7b5ff2013-11-23 00:38:49 -08002 * This file is part of the libpayload project.
3 *
Gabe Black0af03d22012-03-19 03:06:46 -07004 * Copyright (c) 2012 The Chromium OS Authors.
5 *
Gabe Black7c7b5ff2013-11-23 00:38:49 -08006 * 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 Black0af03d22012-03-19 03:06:46 -070016 *
Gabe Black7c7b5ff2013-11-23 00:38:49 -080017 * 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 Black0af03d22012-03-19 03:06:46 -070028 */
29
30#ifndef _ENDIAN_H_
31#define _ENDIAN_H_
32
Julius Werner8a1d11f2014-07-17 10:43:15 -070033#include <arch/io.h>
Gabe Black0af03d22012-03-19 03:06:46 -070034#include <arch/types.h>
35#include <libpayload-config.h>
36
Gabe Black1cb414d2013-03-08 04:38:13 -080037static inline uint16_t swap_bytes16(uint16_t in)
38{
39 return ((in & 0xFF) << 8) | ((in & 0xFF00) >> 8);
40}
41
42static 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
48static 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 Black0af03d22012-03-19 03:06:46 -070053
Hung-Te Line112b742013-03-13 18:08:29 +080054/* Endian functions from glibc 2.9 / BSD "endian.h" */
Gabe Black0af03d22012-03-19 03:06:46 -070055
Stefan Reinauer1b4d3942015-06-29 15:47:34 -070056#if IS_ENABLED(CONFIG_LP_BIG_ENDIAN)
Gabe Black0af03d22012-03-19 03:06:46 -070057
Hung-Te Line112b742013-03-13 18:08:29 +080058#define htobe16(in) (in)
59#define htobe32(in) (in)
60#define htobe64(in) (in)
Gabe Black0af03d22012-03-19 03:06:46 -070061
Hung-Te Line112b742013-03-13 18:08:29 +080062#define htole16(in) swap_bytes16(in)
63#define htole32(in) swap_bytes32(in)
64#define htole64(in) swap_bytes64(in)
Gabe Black0af03d22012-03-19 03:06:46 -070065
Stefan Reinauer1b4d3942015-06-29 15:47:34 -070066#elif IS_ENABLED(CONFIG_LP_LITTLE_ENDIAN)
Gabe Black0af03d22012-03-19 03:06:46 -070067
Hung-Te Line112b742013-03-13 18:08:29 +080068#define htobe16(in) swap_bytes16(in)
69#define htobe32(in) swap_bytes32(in)
70#define htobe64(in) swap_bytes64(in)
Gabe Black0af03d22012-03-19 03:06:46 -070071
Hung-Te Line112b742013-03-13 18:08:29 +080072#define htole16(in) (in)
73#define htole32(in) (in)
74#define htole64(in) (in)
Gabe Black0af03d22012-03-19 03:06:46 -070075
76#else
77
78#error Cant tell if the CPU is little or big endian.
79
Edward O'Callaghan6bedc272014-02-12 21:53:44 +110080#endif /* CONFIG_*_ENDIAN */
Gabe Black0af03d22012-03-19 03:06:46 -070081
Hung-Te Line112b742013-03-13 18:08:29 +080082#define be16toh(in) htobe16(in)
83#define be32toh(in) htobe32(in)
84#define be64toh(in) htobe64(in)
Gabe Black0af03d22012-03-19 03:06:46 -070085
Hung-Te Line112b742013-03-13 18:08:29 +080086#define le16toh(in) htole16(in)
87#define le32toh(in) htole32(in)
88#define le64toh(in) htole64(in)
Gabe Black0af03d22012-03-19 03:06:46 -070089
Hung-Te Line112b742013-03-13 18:08:29 +080090#define htonw(in) htobe16(in)
91#define htonl(in) htobe32(in)
92#define htonll(in) htobe64(in)
Gabe Black0af03d22012-03-19 03:06:46 -070093
Hung-Te Line112b742013-03-13 18:08:29 +080094#define ntohw(in) be16toh(in)
95#define ntohl(in) be32toh(in)
96#define ntohll(in) be64toh(in)
97
Edward O'Callaghan6bedc272014-02-12 21:53:44 +110098/*
99 * Alignment-agnostic encode/decode bytestream to/from little/big endian.
100 */
101
102static 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
109static 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
116static 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
123static 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
130static 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
139static inline void be16enc(void *pp, uint16_t u)
140{
141 bebitenc(pp, u, 2);
142}
143
144static inline void be32enc(void *pp, uint32_t u)
145{
146 bebitenc(pp, u, 4);
147}
148
149static 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
158static inline void le16enc(void *pp, uint16_t u)
159{
160 lebitenc(pp, u, 2);
161}
162
163static inline void le32enc(void *pp, uint32_t u)
164{
165 lebitenc(pp, u, 4);
166}
167
Hung-Te Line112b742013-03-13 18:08:29 +0800168/* 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 Black0af03d22012-03-19 03:06:46 -0700181
Julius Werner8a1d11f2014-07-17 10:43:15 -0700182/* 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'Callaghan6bedc272014-02-12 21:53:44 +1100198#endif /* _ENDIAN_H_ */