libfdt_env.h revision 9537:ad5b3252dcc6
1/*
2 * libfdt - Flat Device Tree manipulation
3 * Copyright (C) 2006 David Gibson, IBM Corporation.
4 *
5 *     Redistribution and use in source and binary forms, with or
6 *     without modification, are permitted provided that the following
7 *     conditions are met:
8 *
9 *     1. Redistributions of source code must retain the above
10 *        copyright notice, this list of conditions and the following
11 *        disclaimer.
12 *     2. Redistributions in binary form must reproduce the above
13 *        copyright notice, this list of conditions and the following
14 *        disclaimer in the documentation and/or other materials
15 *        provided with the distribution.
16 *
17 *     THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
18 *     CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
19 *     INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
20 *     MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21 *     DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
22 *     CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 *     SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24 *     NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25 *     LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 *     HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 *     CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
28 *     OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
29 *     EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 */
31#ifndef _LIBFDT_ENV_H
32#define _LIBFDT_ENV_H
33
34#include <stddef.h>
35#include <stdint.h>
36#include <string.h>
37
38#ifdef __CHECKER__
39#define __force __attribute__((force))
40#define __bitwise __attribute__((bitwise))
41#else
42#define __force
43#define __bitwise
44#endif
45
46typedef uint16_t __bitwise fdt16_t;
47typedef uint32_t __bitwise fdt32_t;
48typedef uint64_t __bitwise fdt64_t;
49
50#define EXTRACT_BYTE(x, n)	((unsigned long long)((uint8_t *)&x)[n])
51#define CPU_TO_FDT16(x) ((EXTRACT_BYTE(x, 0) << 8) | EXTRACT_BYTE(x, 1))
52#define CPU_TO_FDT32(x) ((EXTRACT_BYTE(x, 0) << 24) | (EXTRACT_BYTE(x, 1) << 16) | \
53                         (EXTRACT_BYTE(x, 2) << 8) | EXTRACT_BYTE(x, 3))
54#define CPU_TO_FDT64(x) ((EXTRACT_BYTE(x, 0) << 56) | (EXTRACT_BYTE(x, 1) << 48) | \
55                         (EXTRACT_BYTE(x, 2) << 40) | (EXTRACT_BYTE(x, 3) << 32) | \
56                         (EXTRACT_BYTE(x, 4) << 24) | (EXTRACT_BYTE(x, 5) << 16) | \
57                         (EXTRACT_BYTE(x, 6) << 8) | EXTRACT_BYTE(x, 7))
58
59static inline uint16_t fdt16_to_cpu(fdt16_t x)
60{
61        return (__force uint16_t)CPU_TO_FDT16(x);
62}
63static inline fdt16_t cpu_to_fdt16(uint16_t x)
64{
65        return (__force fdt16_t)CPU_TO_FDT16(x);
66}
67
68static inline uint32_t fdt32_to_cpu(fdt32_t x)
69{
70        return (__force uint32_t)CPU_TO_FDT32(x);
71}
72static inline fdt32_t cpu_to_fdt32(uint32_t x)
73{
74        return (__force fdt32_t)CPU_TO_FDT32(x);
75}
76
77static inline uint64_t fdt64_to_cpu(fdt64_t x)
78{
79        return (__force uint64_t)CPU_TO_FDT64(x);
80}
81static inline fdt64_t cpu_to_fdt64(uint64_t x)
82{
83        return (__force fdt64_t)CPU_TO_FDT64(x);
84}
85#undef CPU_TO_FDT64
86#undef CPU_TO_FDT32
87#undef CPU_TO_FDT16
88#undef EXTRACT_BYTE
89
90#endif /* _LIBFDT_ENV_H */
91