1b5220bc6SSimon Glass /* 2b5220bc6SSimon Glass * Copyright (c) 2011 The Chromium OS Authors. 3b5220bc6SSimon Glass * See file CREDITS for list of people who contributed to this 4b5220bc6SSimon Glass * project. 5b5220bc6SSimon Glass * 6b5220bc6SSimon Glass * This program is free software; you can redistribute it and/or 7b5220bc6SSimon Glass * modify it under the terms of the GNU General Public License as 8b5220bc6SSimon Glass * published by the Free Software Foundation; either version 2 of 9b5220bc6SSimon Glass * the License, or (at your option) any later version. 10b5220bc6SSimon Glass * 11b5220bc6SSimon Glass * This program is distributed in the hope that it will be useful, 12b5220bc6SSimon Glass * but WITHOUT ANY WARRANTY; without even the implied warranty of 13b5220bc6SSimon Glass * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14b5220bc6SSimon Glass * GNU General Public License for more details. 15b5220bc6SSimon Glass * 16b5220bc6SSimon Glass * You should have received a copy of the GNU General Public License 17b5220bc6SSimon Glass * along with this program; if not, write to the Free Software 18b5220bc6SSimon Glass * Foundation, Inc., 59 Temple Place, Suite 330, Boston, 19b5220bc6SSimon Glass * MA 02111-1307 USA 20b5220bc6SSimon Glass */ 21b5220bc6SSimon Glass 22b5220bc6SSimon Glass 23b5220bc6SSimon Glass /* 24b5220bc6SSimon Glass * This file contains convenience functions for decoding useful and 25b5220bc6SSimon Glass * enlightening information from FDTs. It is intended to be used by device 26b5220bc6SSimon Glass * drivers and board-specific code within U-Boot. It aims to reduce the 27b5220bc6SSimon Glass * amount of FDT munging required within U-Boot itself, so that driver code 28b5220bc6SSimon Glass * changes to support FDT are minimized. 29b5220bc6SSimon Glass */ 30b5220bc6SSimon Glass 31b5220bc6SSimon Glass #include <libfdt.h> 32b5220bc6SSimon Glass 33b5220bc6SSimon Glass /* 34b5220bc6SSimon Glass * A typedef for a physical address. Note that fdt data is always big 35b5220bc6SSimon Glass * endian even on a litle endian machine. 36b5220bc6SSimon Glass */ 37b5220bc6SSimon Glass #ifdef CONFIG_PHYS_64BIT 38b5220bc6SSimon Glass typedef u64 fdt_addr_t; 39b5220bc6SSimon Glass #define FDT_ADDR_T_NONE (-1ULL) 40b5220bc6SSimon Glass #define fdt_addr_to_cpu(reg) be64_to_cpu(reg) 41b5220bc6SSimon Glass #else 42b5220bc6SSimon Glass typedef u32 fdt_addr_t; 43b5220bc6SSimon Glass #define FDT_ADDR_T_NONE (-1U) 44b5220bc6SSimon Glass #define fdt_addr_to_cpu(reg) be32_to_cpu(reg) 45b5220bc6SSimon Glass #endif 46b5220bc6SSimon Glass 47b5220bc6SSimon Glass /* Information obtained about memory from the FDT */ 48b5220bc6SSimon Glass struct fdt_memory { 49b5220bc6SSimon Glass fdt_addr_t start; 50b5220bc6SSimon Glass fdt_addr_t end; 51b5220bc6SSimon Glass }; 52b5220bc6SSimon Glass 53b5220bc6SSimon Glass /** 54b5220bc6SSimon Glass * Compat types that we know about and for which we might have drivers. 55b5220bc6SSimon Glass * Each is named COMPAT_<dir>_<filename> where <dir> is the directory 56b5220bc6SSimon Glass * within drivers. 57b5220bc6SSimon Glass */ 58b5220bc6SSimon Glass enum fdt_compat_id { 59b5220bc6SSimon Glass COMPAT_UNKNOWN, 60b5220bc6SSimon Glass 61b5220bc6SSimon Glass COMPAT_COUNT, 62b5220bc6SSimon Glass }; 63b5220bc6SSimon Glass 64b5220bc6SSimon Glass /** 65b5220bc6SSimon Glass * Find the next numbered alias for a peripheral. This is used to enumerate 66b5220bc6SSimon Glass * all the peripherals of a certain type. 67b5220bc6SSimon Glass * 68b5220bc6SSimon Glass * Do the first call with *upto = 0. Assuming /aliases/<name>0 exists then 69b5220bc6SSimon Glass * this function will return a pointer to the node the alias points to, and 70b5220bc6SSimon Glass * then update *upto to 1. Next time you call this function, the next node 71b5220bc6SSimon Glass * will be returned. 72b5220bc6SSimon Glass * 73b5220bc6SSimon Glass * All nodes returned will match the compatible ID, as it is assumed that 74b5220bc6SSimon Glass * all peripherals use the same driver. 75b5220bc6SSimon Glass * 76b5220bc6SSimon Glass * @param blob FDT blob to use 77b5220bc6SSimon Glass * @param name Root name of alias to search for 78b5220bc6SSimon Glass * @param id Compatible ID to look for 79b5220bc6SSimon Glass * @return offset of next compatible node, or -FDT_ERR_NOTFOUND if no more 80b5220bc6SSimon Glass */ 81b5220bc6SSimon Glass int fdtdec_next_alias(const void *blob, const char *name, 82b5220bc6SSimon Glass enum fdt_compat_id id, int *upto); 83b5220bc6SSimon Glass 84b5220bc6SSimon Glass /** 85*f88fe2deSSimon Glass * Find the next compatible node for a peripheral. 86*f88fe2deSSimon Glass * 87*f88fe2deSSimon Glass * Do the first call with node = 0. This function will return a pointer to 88*f88fe2deSSimon Glass * the next compatible node. Next time you call this function, pass the 89*f88fe2deSSimon Glass * value returned, and the next node will be provided. 90*f88fe2deSSimon Glass * 91*f88fe2deSSimon Glass * @param blob FDT blob to use 92*f88fe2deSSimon Glass * @param node Start node for search 93*f88fe2deSSimon Glass * @param id Compatible ID to look for (enum fdt_compat_id) 94*f88fe2deSSimon Glass * @return offset of next compatible node, or -FDT_ERR_NOTFOUND if no more 95*f88fe2deSSimon Glass */ 96*f88fe2deSSimon Glass int fdtdec_next_compatible(const void *blob, int node, 97*f88fe2deSSimon Glass enum fdt_compat_id id); 98*f88fe2deSSimon Glass 99*f88fe2deSSimon Glass /** 100b5220bc6SSimon Glass * Look up an address property in a node and return it as an address. 101b5220bc6SSimon Glass * The property must hold either one address with no trailing data or 102b5220bc6SSimon Glass * one address with a length. This is only tested on 32-bit machines. 103b5220bc6SSimon Glass * 104b5220bc6SSimon Glass * @param blob FDT blob 105b5220bc6SSimon Glass * @param node node to examine 106b5220bc6SSimon Glass * @param prop_name name of property to find 107b5220bc6SSimon Glass * @return address, if found, or FDT_ADDR_T_NONE if not 108b5220bc6SSimon Glass */ 109b5220bc6SSimon Glass fdt_addr_t fdtdec_get_addr(const void *blob, int node, 110b5220bc6SSimon Glass const char *prop_name); 111b5220bc6SSimon Glass 112b5220bc6SSimon Glass /** 113b5220bc6SSimon Glass * Look up a 32-bit integer property in a node and return it. The property 114b5220bc6SSimon Glass * must have at least 4 bytes of data. The value of the first cell is 115b5220bc6SSimon Glass * returned. 116b5220bc6SSimon Glass * 117b5220bc6SSimon Glass * @param blob FDT blob 118b5220bc6SSimon Glass * @param node node to examine 119b5220bc6SSimon Glass * @param prop_name name of property to find 120b5220bc6SSimon Glass * @param default_val default value to return if the property is not found 121b5220bc6SSimon Glass * @return integer value, if found, or default_val if not 122b5220bc6SSimon Glass */ 123b5220bc6SSimon Glass s32 fdtdec_get_int(const void *blob, int node, const char *prop_name, 124b5220bc6SSimon Glass s32 default_val); 125b5220bc6SSimon Glass 126b5220bc6SSimon Glass /** 127b5220bc6SSimon Glass * Checks whether a node is enabled. 128b5220bc6SSimon Glass * This looks for a 'status' property. If this exists, then returns 1 if 129b5220bc6SSimon Glass * the status is 'ok' and 0 otherwise. If there is no status property, 130*f88fe2deSSimon Glass * it returns 1 on the assumption that anything mentioned should be enabled 131*f88fe2deSSimon Glass * by default. 132b5220bc6SSimon Glass * 133b5220bc6SSimon Glass * @param blob FDT blob 134b5220bc6SSimon Glass * @param node node to examine 135*f88fe2deSSimon Glass * @return integer value 0 (not enabled) or 1 (enabled) 136b5220bc6SSimon Glass */ 137*f88fe2deSSimon Glass int fdtdec_get_is_enabled(const void *blob, int node); 138b5220bc6SSimon Glass 139b5220bc6SSimon Glass /** 140b5220bc6SSimon Glass * Checks whether we have a valid fdt available to control U-Boot, and panic 141b5220bc6SSimon Glass * if not. 142b5220bc6SSimon Glass */ 143b5220bc6SSimon Glass int fdtdec_check_fdt(void); 144a53f4a29SSimon Glass 145a53f4a29SSimon Glass /** 146a53f4a29SSimon Glass * Find the nodes for a peripheral and return a list of them in the correct 147a53f4a29SSimon Glass * order. This is used to enumerate all the peripherals of a certain type. 148a53f4a29SSimon Glass * 149a53f4a29SSimon Glass * To use this, optionally set up a /aliases node with alias properties for 150a53f4a29SSimon Glass * a peripheral. For example, for usb you could have: 151a53f4a29SSimon Glass * 152a53f4a29SSimon Glass * aliases { 153a53f4a29SSimon Glass * usb0 = "/ehci@c5008000"; 154a53f4a29SSimon Glass * usb1 = "/ehci@c5000000"; 155a53f4a29SSimon Glass * }; 156a53f4a29SSimon Glass * 157a53f4a29SSimon Glass * Pass "usb" as the name to this function and will return a list of two 158a53f4a29SSimon Glass * nodes offsets: /ehci@c5008000 and ehci@c5000000. 159a53f4a29SSimon Glass * 160a53f4a29SSimon Glass * All nodes returned will match the compatible ID, as it is assumed that 161a53f4a29SSimon Glass * all peripherals use the same driver. 162a53f4a29SSimon Glass * 163a53f4a29SSimon Glass * If no alias node is found, then the node list will be returned in the 164a53f4a29SSimon Glass * order found in the fdt. If the aliases mention a node which doesn't 165a53f4a29SSimon Glass * exist, then this will be ignored. If nodes are found with no aliases, 166a53f4a29SSimon Glass * they will be added in any order. 167a53f4a29SSimon Glass * 168a53f4a29SSimon Glass * If there is a gap in the aliases, then this function return a 0 node at 169a53f4a29SSimon Glass * that position. The return value will also count these gaps. 170a53f4a29SSimon Glass * 171a53f4a29SSimon Glass * This function checks node properties and will not return nodes which are 172a53f4a29SSimon Glass * marked disabled (status = "disabled"). 173a53f4a29SSimon Glass * 174a53f4a29SSimon Glass * @param blob FDT blob to use 175a53f4a29SSimon Glass * @param name Root name of alias to search for 176a53f4a29SSimon Glass * @param id Compatible ID to look for 177a53f4a29SSimon Glass * @param node_list Place to put list of found nodes 178a53f4a29SSimon Glass * @param maxcount Maximum number of nodes to find 179a53f4a29SSimon Glass * @return number of nodes found on success, FTD_ERR_... on error 180a53f4a29SSimon Glass */ 181a53f4a29SSimon Glass int fdtdec_find_aliases_for_id(const void *blob, const char *name, 182a53f4a29SSimon Glass enum fdt_compat_id id, int *node_list, int maxcount); 183a53f4a29SSimon Glass 184a53f4a29SSimon Glass /* 185a53f4a29SSimon Glass * Get the name for a compatible ID 186a53f4a29SSimon Glass * 187a53f4a29SSimon Glass * @param id Compatible ID to look for 188a53f4a29SSimon Glass * @return compatible string for that id 189a53f4a29SSimon Glass */ 190a53f4a29SSimon Glass const char *fdtdec_get_compatible(enum fdt_compat_id id); 191