xref: /rk3399_rockchip-uboot/doc/driver-model/of-plat.txt (revision 3a40acd42e66522b944d6f1a6aa3297771c9fa65)
139782afbSSimon GlassDriver Model Compiled-in Device Tree / Platform Data
239782afbSSimon Glass====================================================
339782afbSSimon Glass
439782afbSSimon Glass
539782afbSSimon GlassIntroduction
639782afbSSimon Glass------------
739782afbSSimon Glass
839782afbSSimon GlassDevice tree is the standard configuration method in U-Boot. It is used to
939782afbSSimon Glassdefine what devices are in the system and provide configuration information
1039782afbSSimon Glassto these devices.
1139782afbSSimon Glass
1239782afbSSimon GlassThe overhead of adding device tree access to U-Boot is fairly modest,
1339782afbSSimon Glassapproximately 3KB on Thumb 2 (plus the size of the DT itself). This means
1439782afbSSimon Glassthat in most cases it is best to use device tree for configuration.
1539782afbSSimon Glass
1639782afbSSimon GlassHowever there are some very constrained environments where U-Boot needs to
1739782afbSSimon Glasswork. These include SPL with severe memory limitations. For example, some
1839782afbSSimon GlassSoCs require a 16KB SPL image which must include a full MMC stack. In this
1939782afbSSimon Glasscase the overhead of device tree access may be too great.
2039782afbSSimon Glass
2139782afbSSimon GlassIt is possible to create platform data manually by defining C structures
2212696251SSimon Glassfor it, and reference that data in a U_BOOT_DEVICE() declaration. This
2312696251SSimon Glassbypasses the use of device tree completely, effectively creating a parallel
2412696251SSimon Glassconfiguration mechanism. But it is an available option for SPL.
2539782afbSSimon Glass
2612696251SSimon GlassAs an alternative, a new 'of-platdata' feature is provided. This converts the
2739782afbSSimon Glassdevice tree contents into C code which can be compiled into the SPL binary.
2839782afbSSimon GlassThis saves the 3KB of code overhead and perhaps a few hundred more bytes due
2939782afbSSimon Glassto more efficient storage of the data.
3039782afbSSimon Glass
3112696251SSimon GlassNote: Quite a bit of thought has gone into the design of this feature.
3212696251SSimon GlassHowever it still has many rough edges and comments and suggestions are
3312696251SSimon Glassstrongly encouraged! Quite possibly there is a much better approach.
3412696251SSimon Glass
3539782afbSSimon Glass
3639782afbSSimon GlassCaveats
3739782afbSSimon Glass-------
3839782afbSSimon Glass
3939782afbSSimon GlassThere are many problems with this features. It should only be used when
4012696251SSimon Glassstrictly necessary. Notable problems include:
4139782afbSSimon Glass
4212696251SSimon Glass   - Device tree does not describe data types. But the C code must define a
4312696251SSimon Glass        type for each property. These are guessed using heuristics which
4439782afbSSimon Glass        are wrong in several fairly common cases. For example an 8-byte value
4539782afbSSimon Glass        is considered to be a 2-item integer array, and is byte-swapped. A
4639782afbSSimon Glass        boolean value that is not present means 'false', but cannot be
4739782afbSSimon Glass        included in the structures since there is generally no mention of it
4839782afbSSimon Glass        in the device tree file.
4939782afbSSimon Glass
5039782afbSSimon Glass   - Naming of nodes and properties is automatic. This means that they follow
5139782afbSSimon Glass        the naming in the device tree, which may result in C identifiers that
5212696251SSimon Glass        look a bit strange.
5339782afbSSimon Glass
5439782afbSSimon Glass   - It is not possible to find a value given a property name. Code must use
5539782afbSSimon Glass        the associated C member variable directly in the code. This makes
5639782afbSSimon Glass        the code less robust in the face of device-tree changes. It also
5739782afbSSimon Glass        makes it very unlikely that your driver code will be useful for more
5839782afbSSimon Glass        than one SoC. Even if the code is common, each SoC will end up with
5912696251SSimon Glass        a different C struct name, and a likely a different format for the
6012696251SSimon Glass        platform data.
6139782afbSSimon Glass
6239782afbSSimon Glass   - The platform data is provided to drivers as a C structure. The driver
6339782afbSSimon Glass        must use the same structure to access the data. Since a driver
6439782afbSSimon Glass        normally also supports device tree it must use #ifdef to separate
6539782afbSSimon Glass        out this code, since the structures are only available in SPL.
6639782afbSSimon Glass
6739782afbSSimon Glass
6839782afbSSimon GlassHow it works
6939782afbSSimon Glass------------
7039782afbSSimon Glass
7139782afbSSimon GlassThe feature is enabled by CONFIG SPL_OF_PLATDATA. This is only available
7239782afbSSimon Glassin SPL and should be tested with:
7339782afbSSimon Glass
7439782afbSSimon Glass        #if CONFIG_IS_ENABLED(SPL_OF_PLATDATA)
7539782afbSSimon Glass
7639782afbSSimon GlassA new tool called 'dtoc' converts a device tree file either into a set of
7739782afbSSimon Glassstruct declarations, one for each compatible node, or a set of
7839782afbSSimon GlassU_BOOT_DEVICE() declarations along with the actual platform data for each
7939782afbSSimon Glassdevice. As an example, consider this MMC node:
8039782afbSSimon Glass
8139782afbSSimon Glass        sdmmc: dwmmc@ff0c0000 {
8239782afbSSimon Glass                compatible = "rockchip,rk3288-dw-mshc";
8339782afbSSimon Glass                clock-freq-min-max = <400000 150000000>;
8439782afbSSimon Glass                clocks = <&cru HCLK_SDMMC>, <&cru SCLK_SDMMC>,
8539782afbSSimon Glass                         <&cru SCLK_SDMMC_DRV>, <&cru SCLK_SDMMC_SAMPLE>;
8639782afbSSimon Glass                clock-names = "biu", "ciu", "ciu_drv", "ciu_sample";
8739782afbSSimon Glass                fifo-depth = <0x100>;
8839782afbSSimon Glass                interrupts = <GIC_SPI 32 IRQ_TYPE_LEVEL_HIGH>;
8939782afbSSimon Glass                reg = <0xff0c0000 0x4000>;
9039782afbSSimon Glass                bus-width = <4>;
9139782afbSSimon Glass                cap-mmc-highspeed;
9239782afbSSimon Glass                cap-sd-highspeed;
9339782afbSSimon Glass                card-detect-delay = <200>;
9439782afbSSimon Glass                disable-wp;
9539782afbSSimon Glass                num-slots = <1>;
9639782afbSSimon Glass                pinctrl-names = "default";
9739782afbSSimon Glass                pinctrl-0 = <&sdmmc_clk>, <&sdmmc_cmd>, <&sdmmc_cd>, <&sdmmc_bus4>;
9839782afbSSimon Glass                vmmc-supply = <&vcc_sd>;
9939782afbSSimon Glass                status = "okay";
10039782afbSSimon Glass                u-boot,dm-pre-reloc;
10139782afbSSimon Glass        };
10239782afbSSimon Glass
10339782afbSSimon Glass
10439782afbSSimon GlassSome of these properties are dropped by U-Boot under control of the
10539782afbSSimon GlassCONFIG_OF_SPL_REMOVE_PROPS option. The rest are processed. This will produce
10639782afbSSimon Glassthe following C struct declaration:
10739782afbSSimon Glass
10839782afbSSimon Glassstruct dtd_rockchip_rk3288_dw_mshc {
10939782afbSSimon Glass        fdt32_t         bus_width;
11039782afbSSimon Glass        bool            cap_mmc_highspeed;
11139782afbSSimon Glass        bool            cap_sd_highspeed;
11239782afbSSimon Glass        fdt32_t         card_detect_delay;
11339782afbSSimon Glass        fdt32_t         clock_freq_min_max[2];
114*3a40acd4SSimon Glass        struct phandle_1_arg clocks[4];
11539782afbSSimon Glass        bool            disable_wp;
11639782afbSSimon Glass        fdt32_t         fifo_depth;
11739782afbSSimon Glass        fdt32_t         interrupts[3];
11839782afbSSimon Glass        fdt32_t         num_slots;
11939782afbSSimon Glass        fdt32_t         reg[2];
12039782afbSSimon Glass        fdt32_t         vmmc_supply;
12139782afbSSimon Glass};
12239782afbSSimon Glass
12339782afbSSimon Glassand the following device declaration:
12439782afbSSimon Glass
12539782afbSSimon Glassstatic struct dtd_rockchip_rk3288_dw_mshc dtv_dwmmc_at_ff0c0000 = {
12639782afbSSimon Glass        .fifo_depth             = 0x100,
12739782afbSSimon Glass        .cap_sd_highspeed       = true,
12839782afbSSimon Glass        .interrupts             = {0x0, 0x20, 0x4},
12939782afbSSimon Glass        .clock_freq_min_max     = {0x61a80, 0x8f0d180},
13039782afbSSimon Glass        .vmmc_supply            = 0xb,
13139782afbSSimon Glass        .num_slots              = 0x1,
13212696251SSimon Glass        .clocks                 = {{&dtv_clock_controller_at_ff760000, 456},
13312696251SSimon Glass                                   {&dtv_clock_controller_at_ff760000, 68},
13412696251SSimon Glass                                   {&dtv_clock_controller_at_ff760000, 114},
13512696251SSimon Glass                                   {&dtv_clock_controller_at_ff760000, 118}},
13639782afbSSimon Glass        .cap_mmc_highspeed      = true,
13739782afbSSimon Glass        .disable_wp             = true,
13839782afbSSimon Glass        .bus_width              = 0x4,
13939782afbSSimon Glass        .u_boot_dm_pre_reloc    = true,
14039782afbSSimon Glass        .reg                    = {0xff0c0000, 0x4000},
14139782afbSSimon Glass        .card_detect_delay      = 0xc8,
14239782afbSSimon Glass};
14339782afbSSimon GlassU_BOOT_DEVICE(dwmmc_at_ff0c0000) = {
14439782afbSSimon Glass        .name           = "rockchip_rk3288_dw_mshc",
14539782afbSSimon Glass        .platdata       = &dtv_dwmmc_at_ff0c0000,
14612696251SSimon Glass        .platdata_size  = sizeof(dtv_dwmmc_at_ff0c0000),
14739782afbSSimon Glass};
14839782afbSSimon Glass
14939782afbSSimon GlassThe device is then instantiated at run-time and the platform data can be
15039782afbSSimon Glassaccessed using:
15139782afbSSimon Glass
15239782afbSSimon Glass        struct udevice *dev;
15339782afbSSimon Glass        struct dtd_rockchip_rk3288_dw_mshc *plat = dev_get_platdata(dev);
15439782afbSSimon Glass
15539782afbSSimon GlassThis avoids the code overhead of converting the device tree data to
15639782afbSSimon Glassplatform data in the driver. The ofdata_to_platdata() method should
15739782afbSSimon Glasstherefore do nothing in such a driver.
15839782afbSSimon Glass
1592cce5866SSimon GlassWhere a node has multiple compatible strings, a #define is used to make them
1602cce5866SSimon Glassequivalent, e.g.:
1612cce5866SSimon Glass
1622cce5866SSimon Glass#define dtd_rockchip_rk3299_dw_mshc dtd_rockchip_rk3288_dw_mshc
1632cce5866SSimon Glass
16439782afbSSimon Glass
16512696251SSimon GlassConverting of-platdata to a useful form
16612696251SSimon Glass---------------------------------------
16712696251SSimon Glass
16812696251SSimon GlassOf course it would be possible use the of-platdata directly in your driver
16912696251SSimon Glasswhenever configuration information is required. However this meands that the
17012696251SSimon Glassdriver will not be able to support device tree, since the of-platdata
17112696251SSimon Glassstructure is not available when device tree is used. It would make no sense
17212696251SSimon Glassto use this structure if device tree were available, since the structure has
17312696251SSimon Glassall the limitations metioned in caveats above.
17412696251SSimon Glass
17512696251SSimon GlassTherefore it is recommended that the of-platdata structure should be used
17612696251SSimon Glassonly in the probe() method of your driver. It cannot be used in the
17712696251SSimon Glassofdata_to_platdata() method since this is not called when platform data is
17812696251SSimon Glassalready present.
17912696251SSimon Glass
18012696251SSimon Glass
18139782afbSSimon GlassHow to structure your driver
18239782afbSSimon Glass----------------------------
18339782afbSSimon Glass
18439782afbSSimon GlassDrivers should always support device tree as an option. The of-platdata
18539782afbSSimon Glassfeature is intended as a add-on to existing drivers.
18639782afbSSimon Glass
18712696251SSimon GlassYour driver should convert the platdata struct in its probe() method. The
18812696251SSimon Glassexisting device tree decoding logic should be kept in the
18912696251SSimon Glassofdata_to_platdata() method and wrapped with #if.
19039782afbSSimon Glass
19139782afbSSimon GlassFor example:
19239782afbSSimon Glass
19339782afbSSimon Glass    #include <dt-structs.h>
19439782afbSSimon Glass
19539782afbSSimon Glass    struct mmc_platdata {
19639782afbSSimon Glass    #if CONFIG_IS_ENABLED(SPL_OF_PLATDATA)
19712696251SSimon Glass            /* Put this first since driver model will copy the data here */
19839782afbSSimon Glass            struct dtd_mmc dtplat;
19939782afbSSimon Glass    #endif
20039782afbSSimon Glass            /*
20139782afbSSimon Glass             * Other fields can go here, to be filled in by decoding from
20212696251SSimon Glass             * the device tree (or the C structures when of-platdata is used).
20339782afbSSimon Glass             */
20439782afbSSimon Glass            int fifo_depth;
20539782afbSSimon Glass    };
20639782afbSSimon Glass
20739782afbSSimon Glass    static int mmc_ofdata_to_platdata(struct udevice *dev)
20839782afbSSimon Glass    {
20939782afbSSimon Glass    #if !CONFIG_IS_ENABLED(SPL_OF_PLATDATA)
21012696251SSimon Glass            /* Decode the device tree data */
21139782afbSSimon Glass            struct mmc_platdata *plat = dev_get_platdata(dev);
21239782afbSSimon Glass            const void *blob = gd->fdt_blob;
213e160f7d4SSimon Glass            int node = dev_of_offset(dev);
21439782afbSSimon Glass
21539782afbSSimon Glass            plat->fifo_depth = fdtdec_get_int(blob, node, "fifo-depth", 0);
21639782afbSSimon Glass    #endif
21739782afbSSimon Glass
21839782afbSSimon Glass            return 0;
21939782afbSSimon Glass    }
22039782afbSSimon Glass
22139782afbSSimon Glass    static int mmc_probe(struct udevice *dev)
22239782afbSSimon Glass    {
22339782afbSSimon Glass            struct mmc_platdata *plat = dev_get_platdata(dev);
22412696251SSimon Glass
22539782afbSSimon Glass    #if CONFIG_IS_ENABLED(SPL_OF_PLATDATA)
22612696251SSimon Glass            /* Decode the of-platdata from the C structures */
22739782afbSSimon Glass            struct dtd_mmc *dtplat = &plat->dtplat;
22839782afbSSimon Glass
22912696251SSimon Glass            plat->fifo_depth = dtplat->fifo_depth;
23012696251SSimon Glass    #endif
23139782afbSSimon Glass            /* Set up the device from the plat data */
23239782afbSSimon Glass            writel(plat->fifo_depth, ...)
23339782afbSSimon Glass    }
23439782afbSSimon Glass
23539782afbSSimon Glass    static const struct udevice_id mmc_ids[] = {
23639782afbSSimon Glass            { .compatible = "vendor,mmc" },
23739782afbSSimon Glass            { }
23839782afbSSimon Glass    };
23939782afbSSimon Glass
24039782afbSSimon Glass    U_BOOT_DRIVER(mmc_drv) = {
24139782afbSSimon Glass            .name           = "mmc",
24239782afbSSimon Glass            .id             = UCLASS_MMC,
24339782afbSSimon Glass            .of_match       = mmc_ids,
24439782afbSSimon Glass            .ofdata_to_platdata = mmc_ofdata_to_platdata,
24539782afbSSimon Glass            .probe          = mmc_probe,
24639782afbSSimon Glass            .priv_auto_alloc_size = sizeof(struct mmc_priv),
24739782afbSSimon Glass            .platdata_auto_alloc_size = sizeof(struct mmc_platdata),
24839782afbSSimon Glass    };
24939782afbSSimon Glass
25039782afbSSimon Glass
25139782afbSSimon GlassIn the case where SPL_OF_PLATDATA is enabled, platdata_auto_alloc_size is
25212696251SSimon Glassstill used to allocate space for the platform data. This is different from
25312696251SSimon Glassthe normal behaviour and is triggered by the use of of-platdata (strictly
25412696251SSimon Glassspeaking it is a non-zero platdata_size which triggers this).
25539782afbSSimon Glass
25612696251SSimon GlassThe of-platdata struct contents is copied from the C structure data to the
25712696251SSimon Glassstart of the newly allocated area. In the case where device tree is used,
25812696251SSimon Glassthe platform data is allocated, and starts zeroed. In this case the
25912696251SSimon Glassofdata_to_platdata() method should still set up the platform data (and the
26012696251SSimon Glassof-platdata struct will not be present).
26112696251SSimon Glass
26212696251SSimon GlassSPL must use either of-platdata or device tree. Drivers cannot use both at
26312696251SSimon Glassthe same time, but they must support device tree. Supporting of-platdata is
26412696251SSimon Glassoptional.
26512696251SSimon Glass
26639782afbSSimon GlassThe device tree becomes in accessible when CONFIG_SPL_OF_PLATDATA is enabled,
26712696251SSimon Glasssince the device-tree access code is not compiled in. A corollary is that
26812696251SSimon Glassa board can only move to using of-platdata if all the drivers it uses support
26912696251SSimon Glassit. There would be little point in having some drivers require the device
27012696251SSimon Glasstree data, since then libfdt would still be needed for those drivers and
27112696251SSimon Glassthere would be no code-size benefit.
27239782afbSSimon Glass
27339782afbSSimon GlassInternals
27439782afbSSimon Glass---------
27539782afbSSimon Glass
27639782afbSSimon GlassThe dt-structs.h file includes the generated file
27739782afbSSimon Glass(include/generated//dt-structs.h) if CONFIG_SPL_OF_PLATDATA is enabled.
27839782afbSSimon GlassOtherwise (such as in U-Boot proper) these structs are not available. This
27912696251SSimon Glassprevents them being used inadvertently. All usage must be bracketed with
28012696251SSimon Glass#if CONFIG_IS_ENABLED(SPL_OF_PLATDATA).
28139782afbSSimon Glass
28239782afbSSimon GlassThe dt-platdata.c file contains the device declarations and is is built in
28339782afbSSimon Glassspl/dt-platdata.c.
28439782afbSSimon Glass
28539782afbSSimon GlassSome phandles (thsoe that are recognised as such) are converted into
28639782afbSSimon Glasspoints to platform data. This pointer can potentially be used to access the
28739782afbSSimon Glassreferenced device (by searching for the pointer value). This feature is not
28839782afbSSimon Glassyet implemented, however.
28939782afbSSimon Glass
29039782afbSSimon GlassThe beginnings of a libfdt Python module are provided. So far this only
29139782afbSSimon Glassimplements a subset of the features.
29239782afbSSimon Glass
29312696251SSimon GlassThe 'swig' tool is needed to build the libfdt Python module. If this is not
29412696251SSimon Glassfound then the Python model is not used and a fallback is used instead, which
29512696251SSimon Glassmakes use of fdtget.
29612696251SSimon Glass
29712696251SSimon Glass
29812696251SSimon GlassCredits
29912696251SSimon Glass-------
30012696251SSimon Glass
30112696251SSimon GlassThis is an implementation of an idea by Tom Rini <trini@konsulko.com>.
30239782afbSSimon Glass
30339782afbSSimon Glass
30439782afbSSimon GlassFuture work
30539782afbSSimon Glass-----------
30639782afbSSimon Glass- Consider programmatically reading binding files instead of device tree
30739782afbSSimon Glass     contents
30839782afbSSimon Glass- Complete the phandle feature
30939782afbSSimon Glass- Move to using a full Python libfdt module
31039782afbSSimon Glass
31139782afbSSimon Glass--
31239782afbSSimon GlassSimon Glass <sjg@chromium.org>
31312696251SSimon GlassGoogle, Inc
31439782afbSSimon Glass6/6/16
31512696251SSimon GlassUpdated Independence Day 2016
316