blob: 297ffd7733933290420a3670d0f5021fa59ee7a0 [file] [log] [blame]
Hung-Te Lin6fe0cab2013-01-22 18:57:56 +08001/*
2 * Copyright (C) 2013 The Chromium OS Authors. All rights reserved.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License as
6 * published by the Free Software Foundation; either version 2 of
7 * the License, or (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but without any warranty; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
Patrick Georgib890a122015-03-26 15:17:45 +010016 * Foundation, Inc.
Hung-Te Lin6fe0cab2013-01-22 18:57:56 +080017 */
18
19#ifndef _ENDIAN_H_
20#define _ENDIAN_H_
21
22#include <arch/byteorder.h>
Julius Werner9ff8f6f2015-02-23 14:31:09 -080023#include <stdint.h>
24#include <swab.h>
25
26#if defined(__LITTLE_ENDIAN)
27 #define cpu_to_le64(x) ((uint64_t)(x))
28 #define le64_to_cpu(x) ((uint64_t)(x))
29 #define cpu_to_le32(x) ((uint32_t)(x))
30 #define le32_to_cpu(x) ((uint32_t)(x))
31 #define cpu_to_le16(x) ((uint16_t)(x))
32 #define le16_to_cpu(x) ((uint16_t)(x))
33 #define cpu_to_be64(x) swab64(x)
34 #define be64_to_cpu(x) swab64(x)
35 #define cpu_to_be32(x) swab32(x)
36 #define be32_to_cpu(x) swab32(x)
37 #define cpu_to_be16(x) swab16(x)
38 #define be16_to_cpu(x) swab16(x)
39#elif defined(__BIG_ENDIAN)
40 #define cpu_to_le64(x) swab64(x)
41 #define le64_to_cpu(x) swab64(x)
42 #define cpu_to_le32(x) swab32(x)
43 #define le32_to_cpu(x) swab32(x)
44 #define cpu_to_le16(x) swab16(x)
45 #define le16_to_cpu(x) swab16(x)
46 #define cpu_to_be64(x) ((uint64_t)(x))
47 #define be64_to_cpu(x) ((uint64_t)(x))
48 #define cpu_to_be32(x) ((uint32_t)(x))
49 #define be32_to_cpu(x) ((uint32_t)(x))
50 #define cpu_to_be16(x) ((uint16_t)(x))
51 #define be16_to_cpu(x) ((uint16_t)(x))
52#else
53 #error "<arch/byteorder.h> must #define __LITTLE_ENDIAN or __BIG_ENDIAN"
54#endif
55
56#define ntohll(x) be64_to_cpu(x)
57#define htonll(x) cpu_to_be64(x)
58#define ntohl(x) be32_to_cpu(x)
59#define htonl(x) cpu_to_be32(x)
60
61#define __clrsetbits(endian, bits, addr, clear, set) \
62 write##bits(addr, cpu_to_##endian##bits((endian##bits##_to_cpu( \
63 read##bits(addr)) & ~((uint##bits##_t)(clear))) | (set)))
64
65#define clrbits_le64(addr, clear) __clrsetbits(le, 64, addr, clear, 0)
66#define clrbits_be64(addr, clear) __clrsetbits(be, 64, addr, clear, 0)
67#define clrbits_le32(addr, clear) __clrsetbits(le, 32, addr, clear, 0)
68#define clrbits_be32(addr, clear) __clrsetbits(be, 32, addr, clear, 0)
69#define clrbits_le16(addr, clear) __clrsetbits(le, 16, addr, clear, 0)
70#define clrbits_be16(addr, clear) __clrsetbits(be, 16, addr, clear, 0)
71
72#define setbits_le64(addr, set) __clrsetbits(le, 64, addr, 0, set)
73#define setbits_be64(addr, set) __clrsetbits(be, 64, addr, 0, set)
74#define setbits_le32(addr, set) __clrsetbits(le, 32, addr, 0, set)
75#define setbits_be32(addr, set) __clrsetbits(be, 32, addr, 0, set)
76#define setbits_le16(addr, set) __clrsetbits(le, 16, addr, 0, set)
77#define setbits_be16(addr, set) __clrsetbits(be, 16, addr, 0, set)
78
79#define clrsetbits_le64(addr, clear, set) __clrsetbits(le, 64, addr, clear, set)
80#define clrsetbits_be64(addr, clear, set) __clrsetbits(be, 64, addr, clear, set)
81#define clrsetbits_le32(addr, clear, set) __clrsetbits(le, 32, addr, clear, set)
82#define clrsetbits_be32(addr, clear, set) __clrsetbits(be, 32, addr, clear, set)
83#define clrsetbits_le16(addr, clear, set) __clrsetbits(le, 16, addr, clear, set)
84#define clrsetbits_be16(addr, clear, set) __clrsetbits(be, 16, addr, clear, set)
85
86#define clrsetbits_8(addr, clear, set) \
87 write8(addr, (read8(addr) & ~(clear)) | (set))
88#define clrbits_8(addr, clear) clrsetbits_8(addr, clear, 0)
89#define setbits_8(addr, set) setbits_8(addr, 0, set)
Hung-Te Lin6fe0cab2013-01-22 18:57:56 +080090
Aaron Durbin94a74992015-09-10 12:08:34 -050091#ifndef __ROMCC__
92/*
93 * Portable (API) endian support that can be used in code that is shared
94 * with userspace (man 3 endian) tools.
95 */
96static inline uint16_t htobe16(uint16_t host_16bits)
97{
98 return cpu_to_be16(host_16bits);
99}
100
101static inline uint16_t htole16(uint16_t host_16bits)
102{
103 return cpu_to_le16(host_16bits);
104}
105
106static inline uint16_t be16toh(uint16_t big_endian_16bits)
107{
108 return be16_to_cpu(big_endian_16bits);
109}
110
111static inline uint16_t le16toh(uint16_t little_endian_16bits)
112{
113 return le16_to_cpu(little_endian_16bits);
114}
115
116static inline uint32_t htobe32(uint32_t host_32bits)
117{
118 return cpu_to_be32(host_32bits);
119}
120
121static inline uint32_t htole32(uint32_t host_32bits)
122{
123 return cpu_to_le32(host_32bits);
124}
125
126static inline uint32_t be32toh(uint32_t big_endian_32bits)
127{
128 return be32_to_cpu(big_endian_32bits);
129}
130
131static inline uint32_t le32toh(uint32_t little_endian_32bits)
132{
133 return le32_to_cpu(little_endian_32bits);
134}
135
136static inline uint64_t htobe64(uint64_t host_64bits)
137{
138 return cpu_to_be64(host_64bits);
139}
140
141static inline uint64_t htole64(uint64_t host_64bits)
142{
143 return cpu_to_le64(host_64bits);
144}
145
146static inline uint64_t be64toh(uint64_t big_endian_64bits)
147{
148 return be64_to_cpu(big_endian_64bits);
149}
150
151static inline uint64_t le64toh(uint64_t little_endian_64bits)
152{
153 return le16_to_cpu(little_endian_64bits);
154}
155#endif
156
Hung-Te Lin6fe0cab2013-01-22 18:57:56 +0800157#endif