zmatt

device tree summary

Jan 7th, 2019
207
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.62 KB | None | 0 0
  1. // Typically dts files are compiled using a makefile which will run them
  2. // through the C Preprocessor, which means you can use things like #include
  3. // and #define just like in C.  Include files use the suffix .dtsi, except that
  4. // include files that only contain constants/macros for the c preprocessor are
  5. // sometimes given the .h suffix.
  6.  
  7. // Required at the top of a top-level device-tree source (dts).
  8. // Never present in an include-file (dtsi).
  9. /dts-v1/;
  10.  
  11. // The device tree is a datastructure very similar to a filesystem, except
  12. // directories are called "nodes" and files are called "properties".  You can
  13. // actually inspect a filesystem-representation of the current device-tree
  14. // in /proc/device-tree.
  15.  
  16.  
  17. // A device-tree source file consists of fragments that modify the content of
  18. // some existing node by adding, changing, or deleting properties and/or child
  19. // nodes, thus incrementally building the final device tree.
  20.  
  21. // Modify the root node:
  22. / {
  23.     // Add or replace a property:
  24.     some-property = "some value";
  25.  
  26.     // Add or modify a child node:
  27.     some-node {
  28.         some-property = "another value";
  29.  
  30.         // You can give a node a global label to reference it from
  31.         // anywhere in the dts.  Unlike node names, a label must be a
  32.         // valid C identifier.
  33.         some_global_label: another-node {
  34.         };
  35.     };
  36.     // Don't forget the semicolon after each block!
  37. };
  38.  
  39. // Modify an existing node referenced by path:
  40. &{/some-node/another-node} {
  41.     // An empty property (i.e. one whose value consists of no bytes)
  42.     // can be created like this.  This is useful since booleans are
  43.     // typically represented by the presence or absence of a property.
  44.     some-bool;
  45.  
  46.     // Within a node block, child nodes have to come *after* the property
  47.     // declarations (if any).  You cannot intersperse the two.
  48.     foo {
  49.     };
  50. };
  51.  
  52. // Modify an existing node referenced by label:
  53. &some_global_label {
  54.     // You can also delete previously declared properties or child nodes,
  55.     // but beware that this doesn't work in overlays:
  56.     /delete-property/ some-bool;
  57.  
  58.     /delete-node/ foo;
  59. };
  60.  
  61.  
  62. / {
  63.     // The value of a property is ultimately just an array of bytes (just
  64.     // like the contents of a file), but the device tree source format lets
  65.     // you specify them in a variety of ways.
  66.     //
  67.     // The property value is a comma-separated list of items (which are
  68.     // simply concatenated), each of which is one of:
  69.     //
  70.     //  "..."       C-string
  71.     //  [...]       bytes in hex format (whitespace ignored)
  72.     //  /bits/ N <...>  array of N-bit integers (N = 8, 16, 32, 64)
  73.     //  <...>       array of 32-bit integers
  74.     //  &foo        full path of labeled node as C-string
  75.     //
  76.     // Array contents (inside the <>) is a whitespace-separated list of
  77.     // values, each of which is one of:
  78.     //
  79.     //  123     decimal integer
  80.     //  0x123       hexadecimal integer
  81.     //  &foo        phandle of labeled node
  82.     //  &{/path}    phandle of node at given path
  83.     //  (...)       expression
  84.     //
  85.     // Integers are encoded in big-endian format.
  86.  
  87.     // The following properties all have the same (empty) value:
  88.     property-0-0;
  89.     property-0-1 = <>;
  90.     property-0-2 = [];
  91.  
  92.     // The following properties all have the same 8-byte value:
  93.     property-1-0 = "foo", "bar";
  94.     property-1-1 = "foo", [62 61 72], "";
  95.     property-1-2 = [66 6f 6f 00], [62 61 72 00];
  96.     property-1-3 = [66 6f 6f 00 62 61 72 00];
  97.     property-1-4 = <0x666f6f00>, <0x62617200>;
  98.     property-1-5 = <0x666f6f00 0x62617200>;
  99.     property-1-6 = /bits/ 32 <0x666f6f00 0x62617200>;
  100.     property-1-7 = /bits/ 64 <0x666f6f0062617200>;
  101.     property-1-8 = /bits/ 64 <( 0x666f6f << 40 | 0x626172 << 8 )>;
  102.     property-1-9 = /bits/ 8 <0x66 0x6f 0x6f 0
  103.                              0x62 0x61 0x72 0>;
  104. };
  105.  
  106.  
  107. #include "gpio.h"
  108. #include "irq.h"
  109.  
  110. / {
  111.     // plumbing devices together -- i.e. how to reference interrupts,
  112.     // gpios, and other types of resources a device might expose.
  113.  
  114.     // NOTE: "cell" means (32-bit) word.
  115.  
  116.     gpio0: my-gpio-controller {
  117.         // indicate this device is a gpio controller.
  118.         gpio-controller;
  119.         // gpios of this controller need 2 words to describe them
  120.         #gpio-cells = <2>;  // gpio-index, gpio-flags
  121.  
  122.         // it is also an interrupt controller
  123.         interrupt-controller;
  124.         #interrupt-cells = <2>; // gpio-index, irq-flags
  125.  
  126.         // (I have no clue why gpio- and interrupt-controllers need to
  127.         // be marked as such with boolean properties.  This isn't the
  128.         // case for other stuff.)
  129.  
  130.         // it is also a pwm controller
  131.         #pwm-cells = <3>;   // gpio-index, period, pwm-flags
  132.     };
  133.  
  134.     my-needy-device {
  135.         // each gpio spec consists of:
  136.         // 1. a reference to a gpio controller node
  137.         // 2. as many additional words as indicated in the gpio
  138.         //    controller's #gpio-cells property.
  139.         enable-gpios = <&gpio0 0 ACTIVE_HIGH>;
  140.         reset-gpios = <&gpio0 1 ACTIVE_HIGH>;
  141.         // even if only one gpio is allowed in each of these properties
  142.         // it is conventional to still use "gpios" rather than "gpio"
  143.         // in the property name.
  144.  
  145.         // pwms is similar, but uses a different convention for naming
  146.         pwms = <&gpio0 2 50000 0>, <&gpio0 3 50000 0>;
  147.         pwm-names = "backlight", "frontlight";
  148.         // using pwm-names seems to be relatively rare.
  149.  
  150.         // interrupts has some history, and traditionally uses a
  151.         // different property for the controller and its arguments:
  152.         interrupt-parent = <&gpio0>;  // inherited from parent if not specified
  153.         interrupts = <4 IRQ_TYPE_EDGE_FALLING>,
  154.                      <5 IRQ_TYPE_EDGE_FALLING>;
  155.         // which implies that all irqs of the device have to use the
  156.         // same irq controller.   nowadays you can replace it with
  157.         interrupts-extended = <&gpio0 4 IRQ_TYPE_EDGE_FALLING>,
  158.                               <&gpio0 5 IRQ_TYPE_EDGE_FALLING>;
  159.         // and as usual you can name interrupts
  160.         interrupt-names = "rx", "tx";
  161.     };
  162. };
Add Comment
Please, Sign In to add comment