1*4882a593SmuzhiyunDriver Model with Live Device Tree 2*4882a593Smuzhiyun================================== 3*4882a593Smuzhiyun 4*4882a593Smuzhiyun 5*4882a593SmuzhiyunIntroduction 6*4882a593Smuzhiyun------------ 7*4882a593Smuzhiyun 8*4882a593SmuzhiyunTraditionally U-Boot has used a 'flat' device tree. This means that it 9*4882a593Smuzhiyunreads directly from the device tree binary structure. It is called a flat 10*4882a593Smuzhiyundevice tree because nodes are listed one after the other, with the 11*4882a593Smuzhiyunhierarchy detected by tags in the format. 12*4882a593Smuzhiyun 13*4882a593SmuzhiyunThis document describes U-Boot's support for a 'live' device tree, meaning 14*4882a593Smuzhiyunthat the tree is loaded into a hierarchical data structure within U-Boot. 15*4882a593Smuzhiyun 16*4882a593Smuzhiyun 17*4882a593SmuzhiyunMotivation 18*4882a593Smuzhiyun---------- 19*4882a593Smuzhiyun 20*4882a593SmuzhiyunThe flat device tree has several advantages: 21*4882a593Smuzhiyun 22*4882a593Smuzhiyun- it is the format produced by the device tree compiler, so no translation 23*4882a593Smuzhiyunis needed 24*4882a593Smuzhiyun 25*4882a593Smuzhiyun- it is fairly compact (e.g. there is no need for pointers) 26*4882a593Smuzhiyun 27*4882a593Smuzhiyun- it is accessed by the libfdt library, which is well tested and stable 28*4882a593Smuzhiyun 29*4882a593Smuzhiyun 30*4882a593SmuzhiyunHowever the flat device tree does have some limitations. Adding new 31*4882a593Smuzhiyunproperties can involve copying large amounts of data around to make room. 32*4882a593SmuzhiyunThe overall tree has a fixed maximum size so sometimes the tree must be 33*4882a593Smuzhiyunrebuilt in a new location to create more space. Even if not adding new 34*4882a593Smuzhiyunproperties or nodes, scanning the tree can be slow. For example, finding 35*4882a593Smuzhiyunthe parent of a node is a slow process. Reading from nodes involves a 36*4882a593Smuzhiyunsmall amount parsing which takes a little time. 37*4882a593Smuzhiyun 38*4882a593SmuzhiyunDriver model scans the entire device tree sequentially on start-up which 39*4882a593Smuzhiyunavoids the worst of the flat tree's limitations. But if the tree is to be 40*4882a593Smuzhiyunmodified at run-time, a live tree is much faster. Even if no modification 41*4882a593Smuzhiyunis necessary, parsing the tree once and using a live tree from then on 42*4882a593Smuzhiyunseems to save a little time. 43*4882a593Smuzhiyun 44*4882a593Smuzhiyun 45*4882a593SmuzhiyunImplementation 46*4882a593Smuzhiyun-------------- 47*4882a593Smuzhiyun 48*4882a593SmuzhiyunIn U-Boot a live device tree ('livetree') is currently supported only 49*4882a593Smuzhiyunafter relocation. Therefore we need a mechanism to specify a device 50*4882a593Smuzhiyuntree node regardless of whether it is in the flat tree or livetree. 51*4882a593Smuzhiyun 52*4882a593SmuzhiyunThe 'ofnode' type provides this. An ofnode can point to either a flat tree 53*4882a593Smuzhiyunnode (when the live tree node is not yet set up) or a livetree node. The 54*4882a593Smuzhiyuncaller of an ofnode function does not need to worry about these details. 55*4882a593Smuzhiyun 56*4882a593SmuzhiyunThe main users of the information in a device tree are drivers. These have 57*4882a593Smuzhiyuna 'struct udevice *' which is attached to a device tree node. Therefore it 58*4882a593Smuzhiyunmakes sense to be able to read device tree properties using the 59*4882a593Smuzhiyun'struct udevice *', rather than having to obtain the ofnode first. 60*4882a593Smuzhiyun 61*4882a593SmuzhiyunThe 'dev_read_...()' interface provides this. It allows properties to be 62*4882a593Smuzhiyuneasily read from the device tree using only a device pointer. Under the 63*4882a593Smuzhiyunhood it uses ofnode so it works with both flat and live device trees. 64*4882a593Smuzhiyun 65*4882a593Smuzhiyun 66*4882a593SmuzhiyunEnabling livetree 67*4882a593Smuzhiyun----------------- 68*4882a593Smuzhiyun 69*4882a593SmuzhiyunCONFIG_OF_LIVE enables livetree. When this option is enabled, the flat 70*4882a593Smuzhiyuntree will be used in SPL and before relocation in U-Boot proper. Just 71*4882a593Smuzhiyunbefore relocation a livetree is built, and this is used for U-Boot proper 72*4882a593Smuzhiyunafter relocation. 73*4882a593Smuzhiyun 74*4882a593SmuzhiyunMost checks for livetree use CONFIG_IS_ENABLED(OF_LIVE). This means that 75*4882a593Smuzhiyunfor SPL, the CONFIG_SPL_OF_LIVE option is checked. At present this does 76*4882a593Smuzhiyunnot exist, since SPL does not support livetree. 77*4882a593Smuzhiyun 78*4882a593Smuzhiyun 79*4882a593SmuzhiyunPorting drivers 80*4882a593Smuzhiyun--------------- 81*4882a593Smuzhiyun 82*4882a593SmuzhiyunMany existing drivers use the fdtdec interface to read device tree 83*4882a593Smuzhiyunproperties. This only works with a flat device tree. The drivers should be 84*4882a593Smuzhiyunconverted to use the dev_read_() interface. 85*4882a593Smuzhiyun 86*4882a593SmuzhiyunFor example, the old code may be like this: 87*4882a593Smuzhiyun 88*4882a593Smuzhiyun struct udevice *bus; 89*4882a593Smuzhiyun const void *blob = gd->fdt_blob; 90*4882a593Smuzhiyun int node = dev_of_offset(bus); 91*4882a593Smuzhiyun 92*4882a593Smuzhiyun i2c_bus->regs = (struct i2c_ctlr *)devfdt_get_addr(dev); 93*4882a593Smuzhiyun plat->frequency = fdtdec_get_int(blob, node, "spi-max-frequency", 500000); 94*4882a593Smuzhiyun 95*4882a593SmuzhiyunThe new code is: 96*4882a593Smuzhiyun 97*4882a593Smuzhiyun struct udevice *bus; 98*4882a593Smuzhiyun 99*4882a593Smuzhiyun i2c_bus->regs = (struct i2c_ctlr *)dev_read_addr(dev); 100*4882a593Smuzhiyun plat->frequency = dev_read_u32_default(bus, "spi-max-frequency", 500000); 101*4882a593Smuzhiyun 102*4882a593SmuzhiyunThe dev_read_...() interface is more convenient and works with both the 103*4882a593Smuzhiyunflat and live device trees. See include/dm/read.h for a list of functions. 104*4882a593Smuzhiyun 105*4882a593SmuzhiyunWhere properties must be read from sub-nodes or other nodes, you must fall 106*4882a593Smuzhiyunback to using ofnode. For example, for old code like this: 107*4882a593Smuzhiyun 108*4882a593Smuzhiyun const void *blob = gd->fdt_blob; 109*4882a593Smuzhiyun int subnode; 110*4882a593Smuzhiyun 111*4882a593Smuzhiyun fdt_for_each_subnode(subnode, blob, dev_of_offset(dev)) { 112*4882a593Smuzhiyun freq = fdtdec_get_int(blob, node, "spi-max-frequency", 500000); 113*4882a593Smuzhiyun ... 114*4882a593Smuzhiyun } 115*4882a593Smuzhiyun 116*4882a593Smuzhiyunyou should use: 117*4882a593Smuzhiyun 118*4882a593Smuzhiyun ofnode subnode; 119*4882a593Smuzhiyun 120*4882a593Smuzhiyun ofnode_for_each_subnode(subnode, dev_ofnode(dev)) { 121*4882a593Smuzhiyun freq = ofnode_read_u32(node, "spi-max-frequency", 500000); 122*4882a593Smuzhiyun ... 123*4882a593Smuzhiyun } 124*4882a593Smuzhiyun 125*4882a593Smuzhiyun 126*4882a593SmuzhiyunUseful ofnode functions 127*4882a593Smuzhiyun----------------------- 128*4882a593Smuzhiyun 129*4882a593SmuzhiyunThe internal data structures of the livetree are defined in include/dm/of.h : 130*4882a593Smuzhiyun 131*4882a593Smuzhiyun struct device_node - holds information about a device tree node 132*4882a593Smuzhiyun struct property - holds information about a property within a node 133*4882a593Smuzhiyun 134*4882a593SmuzhiyunNodes have pointers to their first property, their parent, their first child 135*4882a593Smuzhiyunand their sibling. This allows nodes to be linked together in a hierarchical 136*4882a593Smuzhiyuntree. 137*4882a593Smuzhiyun 138*4882a593SmuzhiyunProperties have pointers to the next property. This allows all properties of 139*4882a593Smuzhiyuna node to be linked together in a chain. 140*4882a593Smuzhiyun 141*4882a593SmuzhiyunIt should not be necessary to use these data structures in normal code. In 142*4882a593Smuzhiyunparticular, you should refrain from using functions which access the livetree 143*4882a593Smuzhiyundirectly, such as of_read_u32(). Use ofnode functions instead, to allow your 144*4882a593Smuzhiyuncode to work with a flat tree also. 145*4882a593Smuzhiyun 146*4882a593SmuzhiyunSome conversion functions are used internally. Generally these are not needed 147*4882a593Smuzhiyunfor driver code. Note that they will not work if called in the wrong context. 148*4882a593SmuzhiyunFor example it is invalid to call ofnode_to_no() when a flat tree is being 149*4882a593Smuzhiyunused. Similarly it is not possible to call ofnode_to_offset() on a livetree 150*4882a593Smuzhiyunnode. 151*4882a593Smuzhiyun 152*4882a593Smuzhiyun ofnode_to_np() - converts ofnode to struct device_node * 153*4882a593Smuzhiyun ofnode_to_offset() - converts ofnode to offset 154*4882a593Smuzhiyun 155*4882a593Smuzhiyun no_to_ofnode() - converts node pointer to ofnode 156*4882a593Smuzhiyun offset_to_ofnode() - converts offset to ofnode 157*4882a593Smuzhiyun 158*4882a593Smuzhiyun 159*4882a593SmuzhiyunOther useful functions: 160*4882a593Smuzhiyun 161*4882a593Smuzhiyun of_live_active() returns true if livetree is in use, false if flat tree 162*4882a593Smuzhiyun ofnode_valid() return true if a given node is valid 163*4882a593Smuzhiyun ofnode_is_np() returns true if a given node is a livetree node 164*4882a593Smuzhiyun ofnode_equal() compares two ofnodes 165*4882a593Smuzhiyun ofnode_null() returns a null ofnode (for which ofnode_valid() returns false) 166*4882a593Smuzhiyun 167*4882a593Smuzhiyun 168*4882a593SmuzhiyunPhandles 169*4882a593Smuzhiyun-------- 170*4882a593Smuzhiyun 171*4882a593SmuzhiyunThere is full phandle support for live tree. All functions make use of 172*4882a593Smuzhiyunstruct ofnode_phandle_args, which has an ofnode within it. This supports both 173*4882a593Smuzhiyunlivetree and flat tree transparently. See for example 174*4882a593Smuzhiyunofnode_parse_phandle_with_args(). 175*4882a593Smuzhiyun 176*4882a593Smuzhiyun 177*4882a593SmuzhiyunReading addresses 178*4882a593Smuzhiyun----------------- 179*4882a593Smuzhiyun 180*4882a593SmuzhiyunYou should use dev_read_addr() and friends to read addresses from device-tree 181*4882a593Smuzhiyunnodes. 182*4882a593Smuzhiyun 183*4882a593Smuzhiyun 184*4882a593Smuzhiyunfdtdec 185*4882a593Smuzhiyun------ 186*4882a593Smuzhiyun 187*4882a593SmuzhiyunThe existing fdtdec interface will eventually be retired. Please try to avoid 188*4882a593Smuzhiyunusing it in new code. 189*4882a593Smuzhiyun 190*4882a593Smuzhiyun 191*4882a593SmuzhiyunModifying the livetree 192*4882a593Smuzhiyun---------------------- 193*4882a593Smuzhiyun 194*4882a593SmuzhiyunThis is not currently supported. Once implemented it should provide a much 195*4882a593Smuzhiyunmore efficient implementation for modification of the device tree than using 196*4882a593Smuzhiyunthe flat tree. 197*4882a593Smuzhiyun 198*4882a593Smuzhiyun 199*4882a593SmuzhiyunInternal implementation 200*4882a593Smuzhiyun----------------------- 201*4882a593Smuzhiyun 202*4882a593SmuzhiyunThe dev_read_...() functions have two implementations. When 203*4882a593SmuzhiyunCONFIG_DM_DEV_READ_INLINE is enabled, these functions simply call the ofnode 204*4882a593Smuzhiyunfunctions directly. This is useful when livetree is not enabled. The ofnode 205*4882a593Smuzhiyunfunctions call ofnode_is_np(node) which will always return false if livetree 206*4882a593Smuzhiyunis disabled, just falling back to flat tree code. 207*4882a593Smuzhiyun 208*4882a593SmuzhiyunThis optimisation means that without livetree enabled, the dev_read_...() and 209*4882a593Smuzhiyunofnode interfaces do not noticeably add to code size. 210*4882a593Smuzhiyun 211*4882a593SmuzhiyunThe CONFIG_DM_DEV_READ_INLINE option defaults to enabled when livetree is 212*4882a593Smuzhiyundisabled. 213*4882a593Smuzhiyun 214*4882a593SmuzhiyunMost livetree code comes directly from Linux and is modified as little as 215*4882a593Smuzhiyunpossible. This is deliberate since this code is fairly stable and does what 216*4882a593Smuzhiyunwe want. Some features (such as get/put) are not supported. Internal macros 217*4882a593Smuzhiyuntake care of removing these features silently. 218*4882a593Smuzhiyun 219*4882a593SmuzhiyunWithin the of_access.c file there are pointers to the alias node, the chosen 220*4882a593Smuzhiyunnode and the stdout-path alias. 221*4882a593Smuzhiyun 222*4882a593Smuzhiyun 223*4882a593SmuzhiyunErrors 224*4882a593Smuzhiyun------ 225*4882a593Smuzhiyun 226*4882a593SmuzhiyunWith a flat device tree, libfdt errors are returned (e.g. -FDT_ERR_NOTFOUND). 227*4882a593SmuzhiyunFor livetree normal 'errno' errors are returned (e.g. -ENOTFOUND). At present 228*4882a593Smuzhiyunthe ofnode and dev_read_...() functions return either one or other type of 229*4882a593Smuzhiyunerror. This is clearly not desirable. Once tests are added for all the 230*4882a593Smuzhiyunfunctions this can be tidied up. 231*4882a593Smuzhiyun 232*4882a593Smuzhiyun 233*4882a593SmuzhiyunAdding new access functions 234*4882a593Smuzhiyun--------------------------- 235*4882a593Smuzhiyun 236*4882a593SmuzhiyunAdding a new function for device-tree access involves the following steps: 237*4882a593Smuzhiyun 238*4882a593Smuzhiyun - Add two dev_read() functions: 239*4882a593Smuzhiyun - inline version in the read.h header file, which calls an ofnode 240*4882a593Smuzhiyun function 241*4882a593Smuzhiyun - standard version in the read.c file (or perhaps another file), which 242*4882a593Smuzhiyun also calls an ofnode function 243*4882a593Smuzhiyun 244*4882a593Smuzhiyun The implementations of these functions can be the same. The purpose 245*4882a593Smuzhiyun of the inline version is purely to reduce code size impact. 246*4882a593Smuzhiyun 247*4882a593Smuzhiyun - Add an ofnode function. This should call ofnode_is_np() to work out 248*4882a593Smuzhiyun whether a livetree or flat tree is used. For the livetree it should 249*4882a593Smuzhiyun call an of_...() function. For the flat tree it should call an 250*4882a593Smuzhiyun fdt_...() function. The livetree version will be optimised out at 251*4882a593Smuzhiyun compile time if livetree is not enabled. 252*4882a593Smuzhiyun 253*4882a593Smuzhiyun - Add an of_...() function for the livetree implementation. If a similar 254*4882a593Smuzhiyun function is available in Linux, the implementation should be taken 255*4882a593Smuzhiyun from there and modified as little as possible (generally not at all). 256*4882a593Smuzhiyun 257*4882a593Smuzhiyun 258*4882a593SmuzhiyunFuture work 259*4882a593Smuzhiyun----------- 260*4882a593Smuzhiyun 261*4882a593SmuzhiyunLive tree support was introduced in U-Boot 2017.07. There is still quite a bit 262*4882a593Smuzhiyunof work to do to flesh this out: 263*4882a593Smuzhiyun 264*4882a593Smuzhiyun- tests for all access functions 265*4882a593Smuzhiyun- support for livetree modification 266*4882a593Smuzhiyun- addition of more access functions as needed 267*4882a593Smuzhiyun- support for livetree in SPL and before relocation (if desired) 268*4882a593Smuzhiyun 269*4882a593Smuzhiyun 270*4882a593Smuzhiyun-- 271*4882a593SmuzhiyunSimon Glass <sjg@chromium.org> 272*4882a593Smuzhiyun5-Aug-17 273