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 /** 85b5220bc6SSimon Glass * Look up an address property in a node and return it as an address. 86b5220bc6SSimon Glass * The property must hold either one address with no trailing data or 87b5220bc6SSimon Glass * one address with a length. This is only tested on 32-bit machines. 88b5220bc6SSimon Glass * 89b5220bc6SSimon Glass * @param blob FDT blob 90b5220bc6SSimon Glass * @param node node to examine 91b5220bc6SSimon Glass * @param prop_name name of property to find 92b5220bc6SSimon Glass * @return address, if found, or FDT_ADDR_T_NONE if not 93b5220bc6SSimon Glass */ 94b5220bc6SSimon Glass fdt_addr_t fdtdec_get_addr(const void *blob, int node, 95b5220bc6SSimon Glass const char *prop_name); 96b5220bc6SSimon Glass 97b5220bc6SSimon Glass /** 98b5220bc6SSimon Glass * Look up a 32-bit integer property in a node and return it. The property 99b5220bc6SSimon Glass * must have at least 4 bytes of data. The value of the first cell is 100b5220bc6SSimon Glass * returned. 101b5220bc6SSimon Glass * 102b5220bc6SSimon Glass * @param blob FDT blob 103b5220bc6SSimon Glass * @param node node to examine 104b5220bc6SSimon Glass * @param prop_name name of property to find 105b5220bc6SSimon Glass * @param default_val default value to return if the property is not found 106b5220bc6SSimon Glass * @return integer value, if found, or default_val if not 107b5220bc6SSimon Glass */ 108b5220bc6SSimon Glass s32 fdtdec_get_int(const void *blob, int node, const char *prop_name, 109b5220bc6SSimon Glass s32 default_val); 110b5220bc6SSimon Glass 111b5220bc6SSimon Glass /** 112b5220bc6SSimon Glass * Checks whether a node is enabled. 113b5220bc6SSimon Glass * This looks for a 'status' property. If this exists, then returns 1 if 114b5220bc6SSimon Glass * the status is 'ok' and 0 otherwise. If there is no status property, 115b5220bc6SSimon Glass * it returns the default value. 116b5220bc6SSimon Glass * 117b5220bc6SSimon Glass * @param blob FDT blob 118b5220bc6SSimon Glass * @param node node to examine 119b5220bc6SSimon Glass * @param default_val default value to return if no 'status' property exists 120b5220bc6SSimon Glass * @return integer value 0/1, if found, or default_val if not 121b5220bc6SSimon Glass */ 122b5220bc6SSimon Glass int fdtdec_get_is_enabled(const void *blob, int node, int default_val); 123b5220bc6SSimon Glass 124b5220bc6SSimon Glass /** 125b5220bc6SSimon Glass * Checks whether we have a valid fdt available to control U-Boot, and panic 126b5220bc6SSimon Glass * if not. 127b5220bc6SSimon Glass */ 128b5220bc6SSimon Glass int fdtdec_check_fdt(void); 129*a53f4a29SSimon Glass 130*a53f4a29SSimon Glass /** 131*a53f4a29SSimon Glass * Find the nodes for a peripheral and return a list of them in the correct 132*a53f4a29SSimon Glass * order. This is used to enumerate all the peripherals of a certain type. 133*a53f4a29SSimon Glass * 134*a53f4a29SSimon Glass * To use this, optionally set up a /aliases node with alias properties for 135*a53f4a29SSimon Glass * a peripheral. For example, for usb you could have: 136*a53f4a29SSimon Glass * 137*a53f4a29SSimon Glass * aliases { 138*a53f4a29SSimon Glass * usb0 = "/ehci@c5008000"; 139*a53f4a29SSimon Glass * usb1 = "/ehci@c5000000"; 140*a53f4a29SSimon Glass * }; 141*a53f4a29SSimon Glass * 142*a53f4a29SSimon Glass * Pass "usb" as the name to this function and will return a list of two 143*a53f4a29SSimon Glass * nodes offsets: /ehci@c5008000 and ehci@c5000000. 144*a53f4a29SSimon Glass * 145*a53f4a29SSimon Glass * All nodes returned will match the compatible ID, as it is assumed that 146*a53f4a29SSimon Glass * all peripherals use the same driver. 147*a53f4a29SSimon Glass * 148*a53f4a29SSimon Glass * If no alias node is found, then the node list will be returned in the 149*a53f4a29SSimon Glass * order found in the fdt. If the aliases mention a node which doesn't 150*a53f4a29SSimon Glass * exist, then this will be ignored. If nodes are found with no aliases, 151*a53f4a29SSimon Glass * they will be added in any order. 152*a53f4a29SSimon Glass * 153*a53f4a29SSimon Glass * If there is a gap in the aliases, then this function return a 0 node at 154*a53f4a29SSimon Glass * that position. The return value will also count these gaps. 155*a53f4a29SSimon Glass * 156*a53f4a29SSimon Glass * This function checks node properties and will not return nodes which are 157*a53f4a29SSimon Glass * marked disabled (status = "disabled"). 158*a53f4a29SSimon Glass * 159*a53f4a29SSimon Glass * @param blob FDT blob to use 160*a53f4a29SSimon Glass * @param name Root name of alias to search for 161*a53f4a29SSimon Glass * @param id Compatible ID to look for 162*a53f4a29SSimon Glass * @param node_list Place to put list of found nodes 163*a53f4a29SSimon Glass * @param maxcount Maximum number of nodes to find 164*a53f4a29SSimon Glass * @return number of nodes found on success, FTD_ERR_... on error 165*a53f4a29SSimon Glass */ 166*a53f4a29SSimon Glass int fdtdec_find_aliases_for_id(const void *blob, const char *name, 167*a53f4a29SSimon Glass enum fdt_compat_id id, int *node_list, int maxcount); 168*a53f4a29SSimon Glass 169*a53f4a29SSimon Glass /* 170*a53f4a29SSimon Glass * Get the name for a compatible ID 171*a53f4a29SSimon Glass * 172*a53f4a29SSimon Glass * @param id Compatible ID to look for 173*a53f4a29SSimon Glass * @return compatible string for that id 174*a53f4a29SSimon Glass */ 175*a53f4a29SSimon Glass const char *fdtdec_get_compatible(enum fdt_compat_id id); 176