blob: 08636f3da61e0aff86b86cce614c8451ff5db4ca [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.
Hung-Te Lin6fe0cab2013-01-22 18:57:56 +080013 */
14
15#ifndef _ENDIAN_H_
16#define _ENDIAN_H_
17
18#include <arch/byteorder.h>
Julius Werner9ff8f6f2015-02-23 14:31:09 -080019#include <stdint.h>
20#include <swab.h>
21
22#if defined(__LITTLE_ENDIAN)
23 #define cpu_to_le64(x) ((uint64_t)(x))
24 #define le64_to_cpu(x) ((uint64_t)(x))
25 #define cpu_to_le32(x) ((uint32_t)(x))
26 #define le32_to_cpu(x) ((uint32_t)(x))
27 #define cpu_to_le16(x) ((uint16_t)(x))
28 #define le16_to_cpu(x) ((uint16_t)(x))
29 #define cpu_to_be64(x) swab64(x)
30 #define be64_to_cpu(x) swab64(x)
31 #define cpu_to_be32(x) swab32(x)
32 #define be32_to_cpu(x) swab32(x)
33 #define cpu_to_be16(x) swab16(x)
34 #define be16_to_cpu(x) swab16(x)
35#elif defined(__BIG_ENDIAN)
36 #define cpu_to_le64(x) swab64(x)
37 #define le64_to_cpu(x) swab64(x)
38 #define cpu_to_le32(x) swab32(x)
39 #define le32_to_cpu(x) swab32(x)
40 #define cpu_to_le16(x) swab16(x)
41 #define le16_to_cpu(x) swab16(x)
42 #define cpu_to_be64(x) ((uint64_t)(x))
43 #define be64_to_cpu(x) ((uint64_t)(x))
44 #define cpu_to_be32(x) ((uint32_t)(x))
45 #define be32_to_cpu(x) ((uint32_t)(x))
46 #define cpu_to_be16(x) ((uint16_t)(x))
47 #define be16_to_cpu(x) ((uint16_t)(x))
48#else
49 #error "<arch/byteorder.h> must #define __LITTLE_ENDIAN or __BIG_ENDIAN"
50#endif
51
52#define ntohll(x) be64_to_cpu(x)
53#define htonll(x) cpu_to_be64(x)
54#define ntohl(x) be32_to_cpu(x)
55#define htonl(x) cpu_to_be32(x)
56
57#define __clrsetbits(endian, bits, addr, clear, set) \
58 write##bits(addr, cpu_to_##endian##bits((endian##bits##_to_cpu( \
59 read##bits(addr)) & ~((uint##bits##_t)(clear))) | (set)))
60
61#define clrbits_le64(addr, clear) __clrsetbits(le, 64, addr, clear, 0)
62#define clrbits_be64(addr, clear) __clrsetbits(be, 64, addr, clear, 0)
63#define clrbits_le32(addr, clear) __clrsetbits(le, 32, addr, clear, 0)
64#define clrbits_be32(addr, clear) __clrsetbits(be, 32, addr, clear, 0)
65#define clrbits_le16(addr, clear) __clrsetbits(le, 16, addr, clear, 0)
66#define clrbits_be16(addr, clear) __clrsetbits(be, 16, addr, clear, 0)
67
68#define setbits_le64(addr, set) __clrsetbits(le, 64, addr, 0, set)
69#define setbits_be64(addr, set) __clrsetbits(be, 64, addr, 0, set)
70#define setbits_le32(addr, set) __clrsetbits(le, 32, addr, 0, set)
71#define setbits_be32(addr, set) __clrsetbits(be, 32, addr, 0, set)
72#define setbits_le16(addr, set) __clrsetbits(le, 16, addr, 0, set)
73#define setbits_be16(addr, set) __clrsetbits(be, 16, addr, 0, set)
74
75#define clrsetbits_le64(addr, clear, set) __clrsetbits(le, 64, addr, clear, set)
76#define clrsetbits_be64(addr, clear, set) __clrsetbits(be, 64, addr, clear, set)
77#define clrsetbits_le32(addr, clear, set) __clrsetbits(le, 32, addr, clear, set)
78#define clrsetbits_be32(addr, clear, set) __clrsetbits(be, 32, addr, clear, set)
79#define clrsetbits_le16(addr, clear, set) __clrsetbits(le, 16, addr, clear, set)
80#define clrsetbits_be16(addr, clear, set) __clrsetbits(be, 16, addr, clear, set)
81
82#define clrsetbits_8(addr, clear, set) \
83 write8(addr, (read8(addr) & ~(clear)) | (set))
84#define clrbits_8(addr, clear) clrsetbits_8(addr, clear, 0)
85#define setbits_8(addr, set) setbits_8(addr, 0, set)
Hung-Te Lin6fe0cab2013-01-22 18:57:56 +080086
Aaron Durbin94a74992015-09-10 12:08:34 -050087#ifndef __ROMCC__
88/*
89 * Portable (API) endian support that can be used in code that is shared
90 * with userspace (man 3 endian) tools.
91 */
92static inline uint16_t htobe16(uint16_t host_16bits)
93{
94 return cpu_to_be16(host_16bits);
95}
96
97static inline uint16_t htole16(uint16_t host_16bits)
98{
99 return cpu_to_le16(host_16bits);
100}
101
102static inline uint16_t be16toh(uint16_t big_endian_16bits)
103{
104 return be16_to_cpu(big_endian_16bits);
105}
106
107static inline uint16_t le16toh(uint16_t little_endian_16bits)
108{
109 return le16_to_cpu(little_endian_16bits);
110}
111
112static inline uint32_t htobe32(uint32_t host_32bits)
113{
114 return cpu_to_be32(host_32bits);
115}
116
117static inline uint32_t htole32(uint32_t host_32bits)
118{
119 return cpu_to_le32(host_32bits);
120}
121
122static inline uint32_t be32toh(uint32_t big_endian_32bits)
123{
124 return be32_to_cpu(big_endian_32bits);
125}
126
127static inline uint32_t le32toh(uint32_t little_endian_32bits)
128{
129 return le32_to_cpu(little_endian_32bits);
130}
131
132static inline uint64_t htobe64(uint64_t host_64bits)
133{
134 return cpu_to_be64(host_64bits);
135}
136
137static inline uint64_t htole64(uint64_t host_64bits)
138{
139 return cpu_to_le64(host_64bits);
140}
141
142static inline uint64_t be64toh(uint64_t big_endian_64bits)
143{
144 return be64_to_cpu(big_endian_64bits);
145}
146
147static inline uint64_t le64toh(uint64_t little_endian_64bits)
148{
Aaron Durbin113d8212015-09-10 22:27:55 -0500149 return le64_to_cpu(little_endian_64bits);
Aaron Durbin94a74992015-09-10 12:08:34 -0500150}
151#endif
152
Hung-Te Lin6fe0cab2013-01-22 18:57:56 +0800153#endif