From 302466d07fe8f10ff2ad059dc5c40ac37935fda9 Mon Sep 17 00:00:00 2001 From: Simon Glass Date: Sat, 5 Aug 2017 15:45:53 -0600 Subject: dm: Add migration plan for CONFIG_BLK The CONFIG_BLK conversion involves quite invasive changes in the U-Boot code, with #ifdefs and different code paths. We should try to move over to this soon so we can drop the old code. Set a deadline of 9 months for this work, rounded up to the next release. Signed-off-by: Simon Glass Reviewed-by: Tom Rini diff --git a/doc/driver-model/MIGRATION.txt b/doc/driver-model/MIGRATION.txt new file mode 100644 index 0000000..d2fe027 --- /dev/null +++ b/doc/driver-model/MIGRATION.txt @@ -0,0 +1,20 @@ +Migration Schedule +==================== + +U-Boot has been migrating to a new driver model since its introduction in +2014. This file describes the schedule for deprecation of pre-driver-model +features. + + +CONFIG_BLK +---------- + +Status: In progress +Deadline: 2018.05 + +Maintainers should submit patches for enabling CONFIG_BLK on all boards in +time for inclusion in the 2018.05 release. Boards not converted by this +time may be removed in a subsequent release. + +Note that this implies use of driver model for all block devices (e.g. +MMC, USB, SCSI, SATA). -- cgit v0.10.2 From cee8c35d1b7101d09a1854a48f867deaa701f7bf Mon Sep 17 00:00:00 2001 From: Hannes Schmelzer Date: Fri, 18 Aug 2017 14:41:14 +0200 Subject: fdt: fix 'prop (...) not found!' error in 'fdt set' command This commit brings things back to the well known working state of the command. - With commit 9620d87259572ef21f0df60988d9a932ca673779 (cmd/fdt: support single value replacement within an array) there was an error introduced modifying (inserting) a property to a device-tree node. fdt_getprop(...) returnes a len with -1 for a non-existing property, but a memcpy with len -1 isn't a good idea and things went wrong (crash). - Some times later Tom did repair this with commit 99bb38e2cce9d99238458e0f6d1880c6d2e80a4d (fdt: Check for NULL return from fdt_getprop in 'fdt set') This repairs the crash but the behaviour of the command isn't like before, it makes it impossible to insert a property. - Signed-off-by: Hannes Schmelzer Acked-by: Simon Glass diff --git a/cmd/fdt.c b/cmd/fdt.c index 118613f..d7654b2 100644 --- a/cmd/fdt.c +++ b/cmd/fdt.c @@ -284,16 +284,14 @@ static int do_fdt(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) len = 0; } else { ptmp = fdt_getprop(working_fdt, nodeoffset, prop, &len); - if (!ptmp) { - printf("prop (%s) not found!\n", prop); - return 1; - } if (len > SCRATCHPAD) { printf("prop (%d) doesn't fit in scratchpad!\n", len); return 1; } - memcpy(data, ptmp, len); + if (ptmp != NULL) + memcpy(data, ptmp, len); + ret = fdt_parse_prop(&argv[4], argc - 4, data, &len); if (ret != 0) return ret; -- cgit v0.10.2 From 34e2c285da287b08a6c8d0e0cfde73e2fa127380 Mon Sep 17 00:00:00 2001 From: Philipp Tomsich Date: Tue, 29 Aug 2017 11:47:16 +0200 Subject: gitignore: add intermediates from libfdt build Since ee95d10 (fdt: Build the new python libfdt module), a number of additional files are auto-generated/installed into the tools directory. List these in .gitignore to suppress having them listed as untracked. Signed-off-by: Philipp Tomsich References: ee95d10 (fdt: Build the new python libfdt module) Reviewed-by: Simon Glass diff --git a/.gitignore b/.gitignore index 29757aa..f6f5dae 100644 --- a/.gitignore +++ b/.gitignore @@ -57,6 +57,14 @@ fit-dtb.blob /include/config/ /include/generated/ +# Since "ee95d10 fdt: Build the new python libfdt module", a number of +# build artifacts and intermediated from the python libfdt module are +# installed into the /tools directory. +/tools/_libfdt.so +/tools/libfdt.py +/tools/libfdt.pyc +/tools/libfdt_wrap.c + # stgit generated dirs patches-* .stgit-edit.txt -- cgit v0.10.2 From d944bf6b5e5c0248eebb035d3f6a67dec70d7dd9 Mon Sep 17 00:00:00 2001 From: Simon Glass Date: Thu, 31 Aug 2017 05:59:55 -0600 Subject: dm: core: Add livetree documentation MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Add some documentation for the live device tree support in U-Boot. This was missing from the initial series. Signed-off-by: Simon Glass Suggested-by: Lukasz Majewski Reviewed-by: Ɓukasz Majewski diff --git a/doc/driver-model/livetree.txt b/doc/driver-model/livetree.txt new file mode 100644 index 0000000..01d4488 --- /dev/null +++ b/doc/driver-model/livetree.txt @@ -0,0 +1,272 @@ +Driver Model with Live Device Tree +================================== + + +Introduction +------------ + +Traditionally U-Boot has used a 'flat' device tree. This means that it +reads directly from the device tree binary structure. It is called a flat +device tree because nodes are listed one after the other, with the +hierarchy detected by tags in the format. + +This document describes U-Boot's support for a 'live' device tree, meaning +that the tree is loaded into a hierarchical data structure within U-Boot. + + +Motivation +---------- + +The flat device tree has several advantages: + +- it is the format produced by the device tree compiler, so no translation +is needed + +- it is fairly compact (e.g. there is no need for pointers) + +- it is accessed by the libfdt library, which is well tested and stable + + +However the flat device tree does have some limitations. Adding new +properties can involve copying large amounts of data around to make room. +The overall tree has a fixed maximum size so sometimes the tree must be +rebuilt in a new location to create more space. Even if not adding new +properties or nodes, scanning the tree can be slow. For example, finding +the parent of a node is a slow process. Reading from nodes involves a +small amount parsing which takes a little time. + +Driver model scans the entire device tree sequentially on start-up which +avoids the worst of the flat tree's limitations. But if the tree is to be +modified at run-time, a live tree is much faster. Even if no modification +is necessary, parsing the tree once and using a live tree from then on +seems to save a little time. + + +Implementation +-------------- + +In U-Boot a live device tree ('livetree') is currently supported only +after relocation. Therefore we need a mechanism to specify a device +tree node regardless of whether it is in the flat tree or livetree. + +The 'ofnode' type provides this. An ofnode can point to either a flat tree +node (when the live tree node is not yet set up) or a livetree node. The +caller of an ofnode function does not need to worry about these details. + +The main users of the information in a device tree are drivers. These have +a 'struct udevice *' which is attached to a device tree node. Therefore it +makes sense to be able to read device tree properties using the +'struct udevice *', rather than having to obtain the ofnode first. + +The 'dev_read_...()' interface provides this. It allows properties to be +easily read from the device tree using only a device pointer. Under the +hood it uses ofnode so it works with both flat and live device trees. + + +Enabling livetree +----------------- + +CONFIG_OF_LIVE enables livetree. When this option is enabled, the flat +tree will be used in SPL and before relocation in U-Boot proper. Just +before relocation a livetree is built, and this is used for U-Boot proper +after relocation. + +Most checks for livetree use CONFIG_IS_ENABLED(OF_LIVE). This means that +for SPL, the CONFIG_SPL_OF_LIVE option is checked. At present this does +not exist, since SPL does not support livetree. + + +Porting drivers +--------------- + +Many existing drivers use the fdtdec interface to read device tree +properties. This only works with a flat device tree. The drivers should be +converted to use the dev_read_() interface. + +For example, the old code may be like this: + + struct udevice *bus; + const void *blob = gd->fdt_blob; + int node = dev_of_offset(bus); + + i2c_bus->regs = (struct i2c_ctlr *)devfdt_get_addr(dev); + plat->frequency = fdtdec_get_int(blob, node, "spi-max-frequency", 500000); + +The new code is: + + struct udevice *bus; + + i2c_bus->regs = (struct i2c_ctlr *)dev_read_addr(dev); + plat->frequency = dev_read_u32_default(bus, "spi-max-frequency", 500000); + +The dev_read_...() interface is more convenient and works with both the +flat and live device trees. See include/dm/read.h for a list of functions. + +Where properties must be read from sub-nodes or other nodes, you must fall +back to using ofnode. For example, for old code like this: + + const void *blob = gd->fdt_blob; + int subnode; + + fdt_for_each_subnode(subnode, blob, dev_of_offset(dev)) { + freq = fdtdec_get_int(blob, node, "spi-max-frequency", 500000); + ... + } + +you should use: + + ofnode subnode; + + ofnode_for_each_subnode(subnode, dev_ofnode(dev)) { + freq = ofnode_read_u32(node, "spi-max-frequency", 500000); + ... + } + + +Useful ofnode functions +----------------------- + +The internal data structures of the livetree are defined in include/dm/of.h : + + struct device_node - holds information about a device tree node + struct property - holds information about a property within a node + +Nodes have pointers to their first property, their parent, their first child +and their sibling. This allows nodes to be linked together in a hierarchical +tree. + +Properties have pointers to the next property. This allows all properties of +a node to be linked together in a chain. + +It should not be necessary to use these data structures in normal code. In +particular, you should refrain from using functions which access the livetree +directly, such as of_read_u32(). Use ofnode functions instead, to allow your +code to work with a flat tree also. + +Some conversion functions are used internally. Generally these are not needed +for driver code. Note that they will not work if called in the wrong context. +For example it is invalid to call ofnode_to_no() when a flat tree is being +used. Similarly it is not possible to call ofnode_to_offset() on a livetree +node. + + ofnode_to_np() - converts ofnode to struct device_node * + ofnode_to_offset() - converts ofnode to offset + + no_to_ofnode() - converts node pointer to ofnode + offset_to_ofnode() - converts offset to ofnode + + +Other useful functions: + + of_live_active() returns true if livetree is in use, false if flat tree + ofnode_valid() return true if a given node is valid + ofnode_is_np() returns true if a given node is a livetree node + ofnode_equal() compares two ofnodes + ofnode_null() returns a null ofnode (for which ofnode_valid() returns false) + + +Phandles +-------- + +There is full phandle support for live tree. All functions make use of +struct ofnode_phandle_args, which has an ofnode within it. This supports both +livetree and flat tree transparently. See for example +ofnode_parse_phandle_with_args(). + + +Reading addresses +----------------- + +You should use dev_read_addr() and friends to read addresses from device-tree +nodes. + + +fdtdec +------ + +The existing fdtdec interface will eventually be retired. Please try to avoid +using it in new code. + + +Modifying the livetree +---------------------- + +This is not currently supported. Once implemented it should provide a much +more efficient implementation for modification of the device tree than using +the flat tree. + + +Internal implementation +----------------------- + +The dev_read_...() functions have two implementations. When +CONFIG_DM_DEV_READ_INLINE is enabled, these functions simply call the ofnode +functions directly. This is useful when livetree is not enabled. The ofnode +functions call ofnode_is_np(node) which will always return false if livetree +is disabled, just falling back to flat tree code. + +This optimisation means that without livetree enabled, the dev_read_...() and +ofnode interfaces do not noticeably add to code size. + +The CONFIG_DM_DEV_READ_INLINE option defaults to enabled when livetree is +disabled. + +Most livetree code comes directly from Linux and is modified as little as +possible. This is deliberate since this code is fairly stable and does what +we want. Some features (such as get/put) are not supported. Internal macros +take care of removing these features silently. + +Within the of_access.c file there are pointers to the alias node, the chosen +node and the stdout-path alias. + + +Errors +------ + +With a flat device tree, libfdt errors are returned (e.g. -FDT_ERR_NOTFOUND). +For livetree normal 'errno' errors are returned (e.g. -ENOTFOUND). At present +the ofnode and dev_read_...() functions return either one or other type of +error. This is clearly not desirable. Once tests are added for all the +functions this can be tidied up. + + +Adding new access functions +--------------------------- + +Adding a new function for device-tree access involves the following steps: + + - Add two dev_read() functions: + - inline version in the read.h header file, which calls an ofnode + function + - standard version in the read.c file (or perhaps another file), which + also calls an ofnode function + + The implementations of these functions can be the same. The purpose + of the inline version is purely to reduce code size impact. + + - Add an ofnode function. This should call ofnode_is_np() to work out + whether a livetree or flat tree is used. For the livetree it should + call an of_...() function. For the flat tree it should call an + fdt_...() function. The livetree version will be optimised out at + compile time if livetree is not enabled. + + - Add an of_...() function for the livetree implementation. If a similar + function is available in Linux, the implementation should be taken + from there and modified as little as possible (generally not at all). + + +Future work +----------- + +Live tree support was introduced in U-Boot 2017.07. There is still quite a bit +of work to do to flesh this out: + +- tests for all access functions +- support for livetree modification +- addition of more access functions as needed +- support for livetree in SPL and before relocation (if desired) + + +-- +Simon Glass +5-Aug-17 -- cgit v0.10.2