1 /* 2 * Copyright (c) 2016, Linaro Limited 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions are met: 7 * 8 * 1. Redistributions of source code must retain the above copyright notice, 9 * this list of conditions and the following disclaimer. 10 * 11 * 2. Redistributions in binary form must reproduce the above copyright notice, 12 * this list of conditions and the following disclaimer in the documentation 13 * and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 19 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 20 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 21 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 22 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 23 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 24 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 25 * POSSIBILITY OF SUCH DAMAGE. 26 */ 27 28 #ifndef KERNEL_DT_H 29 #define KERNEL_DT_H 30 31 #include <compiler.h> 32 #include <stdint.h> 33 #include <types_ext.h> 34 #include <util.h> 35 36 /* 37 * Bitfield to reflect status and secure-status values ("ok", "disabled" or not 38 * present) 39 */ 40 #define DT_STATUS_DISABLED 0 41 #define DT_STATUS_OK_NSEC BIT(0) 42 #define DT_STATUS_OK_SEC BIT(1) 43 44 #if defined(CFG_DT) 45 46 /* 47 * DT-aware drivers 48 */ 49 50 struct dt_device_match { 51 const char *compatible; 52 }; 53 54 struct dt_driver { 55 const char *name; 56 const struct dt_device_match *match_table; /* null-terminated */ 57 const void *driver; 58 }; 59 60 #define __dt_driver __section(".rodata.dtdrv") 61 62 extern const struct dt_driver __rodata_dtdrv_start, __rodata_dtdrv_end; 63 64 /* 65 * Find a driver that is suitable for the given DT node, that is, with 66 * a matching "compatible" property. 67 * 68 * @fdt: pointer to the device tree 69 * @offs: node offset 70 */ 71 const struct dt_driver *dt_find_compatible_driver(const void *fdt, int offs); 72 73 const struct dt_driver *__dt_driver_start(void); 74 75 const struct dt_driver *__dt_driver_end(void); 76 77 /* 78 * FDT manipulation functions, not provided by <libfdt.h> 79 */ 80 81 /* 82 * Return the base address for the "reg" property of the specified node or 83 * (paddr_t)-1 in case of error 84 */ 85 paddr_t _fdt_reg_base_address(const void *fdt, int offs); 86 87 /* 88 * Return the reg size for the reg property of the specified node or -1 in case 89 * of error 90 */ 91 ssize_t _fdt_reg_size(const void *fdt, int offs); 92 93 /* 94 * Read the status and secure-status properties into a bitfield. 95 * @status is set to DT_STATUS_DISABLED or a combination of DT_STATUS_OK_NSEC 96 * and DT_STATUS_OK_SEC 97 * Returns 0 on success or -1 in case of error. 98 */ 99 int _fdt_get_status(const void *fdt, int offs); 100 101 #else /* !CFG_DT */ 102 103 static inline const struct dt_driver *__dt_driver_start(void) 104 { 105 return NULL; 106 } 107 108 static inline const struct dt_driver *__dt_driver_end(void) 109 { 110 return NULL; 111 } 112 113 static inline const struct dt_driver *dt_find_compatible_driver( 114 const void *fdt __unused, 115 int offs __unused) 116 { 117 return NULL; 118 } 119 120 static inline paddr_t _fdt_reg_base_address(const void *fdt __unused, 121 int offs __unused) 122 { 123 return (paddr_t)-1; 124 } 125 126 static inline ssize_t _fdt_reg_size(const void *fdt __unused, 127 int offs __unused) 128 { 129 return -1; 130 } 131 132 static inline int _fdt_get_status(const void *fdt __unused, int offs __unused) 133 { 134 return -1; 135 } 136 137 #endif /* !CFG_DT */ 138 139 #define for_each_dt_driver(drv) \ 140 for (drv = __dt_driver_start(); drv < __dt_driver_end(); drv++) 141 142 #endif /* KERNEL_DT_H */ 143