Kumar Gala | 87915cd | 2018-11-15 11:51:50 -0600 | [diff] [blame] | 1 | # Copyright (c) 2018-2019 Linaro |
Carles Cufi | fa26ef0 | 2019-01-30 17:54:21 +0100 | [diff] [blame] | 2 | # Copyright (c) 2019 Nordic Semiconductor ASA |
Kumar Gala | 87915cd | 2018-11-15 11:51:50 -0600 | [diff] [blame] | 3 | # |
| 4 | # SPDX-License-Identifier: Apache-2.0 |
| 5 | |
Keith Short | df0942f | 2023-03-24 15:22:42 -0600 | [diff] [blame] | 6 | import inspect |
Kumar Gala | 87915cd | 2018-11-15 11:51:50 -0600 | [diff] [blame] | 7 | import os |
Martí Bolívar | 269f350 | 2020-07-01 12:17:02 -0700 | [diff] [blame] | 8 | import pickle |
Torsten Rasmussen | 8dc3f85 | 2022-09-14 22:23:15 +0200 | [diff] [blame] | 9 | import re |
Kumar Gala | 5735397 | 2019-08-28 09:30:23 -0500 | [diff] [blame] | 10 | import sys |
Henrik Brix Andersen | c6b3094 | 2022-05-18 17:35:23 +0200 | [diff] [blame] | 11 | from pathlib import Path |
Kumar Gala | 5735397 | 2019-08-28 09:30:23 -0500 | [diff] [blame] | 12 | |
Henrik Brix Andersen | c6b3094 | 2022-05-18 17:35:23 +0200 | [diff] [blame] | 13 | ZEPHYR_BASE = str(Path(__file__).resolve().parents[2]) |
Martí Bolívar | 5332847 | 2021-03-26 16:18:58 -0700 | [diff] [blame] | 14 | sys.path.insert(0, os.path.join(ZEPHYR_BASE, "scripts", "dts", |
| 15 | "python-devicetree", "src")) |
Kumar Gala | 5735397 | 2019-08-28 09:30:23 -0500 | [diff] [blame] | 16 | |
Kumar Gala | 87915cd | 2018-11-15 11:51:50 -0600 | [diff] [blame] | 17 | # Types we support |
| 18 | # 'string', 'int', 'hex', 'bool' |
| 19 | |
Carles Cufi | fa26ef0 | 2019-01-30 17:54:21 +0100 | [diff] [blame] | 20 | doc_mode = os.environ.get('KCONFIG_DOC_MODE') == "1" |
Sebastian Bøe | cd43543 | 2019-02-11 15:57:34 +0100 | [diff] [blame] | 21 | |
Ulf Magnusson | ba312fe | 2019-03-20 19:30:29 +0100 | [diff] [blame] | 22 | if not doc_mode: |
Martí Bolívar | 269f350 | 2020-07-01 12:17:02 -0700 | [diff] [blame] | 23 | EDT_PICKLE = os.environ.get("EDT_PICKLE") |
Kumar Gala | 9aefdaf | 2020-04-15 10:32:41 -0500 | [diff] [blame] | 24 | |
Martí Bolívar | 269f350 | 2020-07-01 12:17:02 -0700 | [diff] [blame] | 25 | # The "if" handles a missing dts. |
| 26 | if EDT_PICKLE is not None and os.path.isfile(EDT_PICKLE): |
| 27 | with open(EDT_PICKLE, 'rb') as f: |
| 28 | edt = pickle.load(f) |
Keith Short | df0942f | 2023-03-24 15:22:42 -0600 | [diff] [blame] | 29 | edtlib = inspect.getmodule(edt) |
Kumar Gala | 5735397 | 2019-08-28 09:30:23 -0500 | [diff] [blame] | 30 | else: |
| 31 | edt = None |
| 32 | |
Kumar Gala | 1baf2f3 | 2019-09-04 14:14:26 -0500 | [diff] [blame] | 33 | |
| 34 | def _warn(kconf, msg): |
| 35 | print("{}:{}: WARNING: {}".format(kconf.filename, kconf.linenr, msg)) |
| 36 | |
| 37 | |
Kumar Gala | 87915cd | 2018-11-15 11:51:50 -0600 | [diff] [blame] | 38 | def _dt_units_to_scale(unit): |
| 39 | if not unit: |
| 40 | return 0 |
| 41 | if unit in {'k', 'K'}: |
| 42 | return 10 |
| 43 | if unit in {'m', 'M'}: |
| 44 | return 20 |
| 45 | if unit in {'g', 'G'}: |
| 46 | return 30 |
Daniel DeGrasse | 6064da8 | 2022-05-25 17:38:59 -0500 | [diff] [blame] | 47 | if unit in {'kb', 'Kb'}: |
| 48 | return 13 |
| 49 | if unit in {'mb', 'Mb'}: |
| 50 | return 23 |
| 51 | if unit in {'gb', 'Gb'}: |
| 52 | return 33 |
Kumar Gala | 87915cd | 2018-11-15 11:51:50 -0600 | [diff] [blame] | 53 | |
Kumar Gala | 5735397 | 2019-08-28 09:30:23 -0500 | [diff] [blame] | 54 | |
| 55 | def dt_chosen_label(kconf, _, chosen): |
| 56 | """ |
| 57 | This function takes a 'chosen' property and treats that property as a path |
Ulf Magnusson | 73ac146 | 2019-09-23 05:14:18 +0200 | [diff] [blame] | 58 | to an EDT node. If it finds an EDT node, it will look to see if that node |
Martí Bolívar | b3151bc | 2022-08-18 16:13:50 -0700 | [diff] [blame] | 59 | has a "label" property and return the value of that "label". If not, we |
| 60 | return the node's name in the devicetree. |
Kumar Gala | 5735397 | 2019-08-28 09:30:23 -0500 | [diff] [blame] | 61 | """ |
| 62 | if doc_mode or edt is None: |
| 63 | return "" |
| 64 | |
Ulf Magnusson | 73ac146 | 2019-09-23 05:14:18 +0200 | [diff] [blame] | 65 | node = edt.chosen_node(chosen) |
| 66 | if not node: |
Kumar Gala | 5735397 | 2019-08-28 09:30:23 -0500 | [diff] [blame] | 67 | return "" |
| 68 | |
Ulf Magnusson | 73ac146 | 2019-09-23 05:14:18 +0200 | [diff] [blame] | 69 | if "label" not in node.props: |
Kumar Gala | 5753826 | 2022-08-04 11:23:13 -0500 | [diff] [blame] | 70 | return node.name |
Kumar Gala | 5735397 | 2019-08-28 09:30:23 -0500 | [diff] [blame] | 71 | |
Ulf Magnusson | 73ac146 | 2019-09-23 05:14:18 +0200 | [diff] [blame] | 72 | return node.props["label"].val |
Kumar Gala | 5735397 | 2019-08-28 09:30:23 -0500 | [diff] [blame] | 73 | |
| 74 | |
Kumar Gala | 07e5d89 | 2019-11-04 10:20:59 -0600 | [diff] [blame] | 75 | def dt_chosen_enabled(kconf, _, chosen): |
| 76 | """ |
| 77 | This function returns "y" if /chosen contains a property named 'chosen' |
| 78 | that points to an enabled node, and "n" otherwise |
| 79 | """ |
| 80 | if doc_mode or edt is None: |
| 81 | return "n" |
| 82 | |
| 83 | node = edt.chosen_node(chosen) |
Martí Bolívar | 8165008 | 2020-10-05 20:02:13 -0700 | [diff] [blame] | 84 | return "y" if node and node.status == "okay" else "n" |
Kumar Gala | 07e5d89 | 2019-11-04 10:20:59 -0600 | [diff] [blame] | 85 | |
Kumar Gala | 0353d22 | 2020-02-13 23:43:42 -0600 | [diff] [blame] | 86 | |
Martí Bolívar | 7ff3ebc | 2020-04-23 12:28:06 -0700 | [diff] [blame] | 87 | def dt_chosen_path(kconf, _, chosen): |
| 88 | """ |
| 89 | This function takes a /chosen node property and returns the path |
| 90 | to the node in the property value, or the empty string. |
| 91 | """ |
| 92 | if doc_mode or edt is None: |
| 93 | return "n" |
| 94 | |
| 95 | node = edt.chosen_node(chosen) |
| 96 | |
| 97 | return node.path if node else "" |
| 98 | |
Daniel DeGrasse | fbefc50 | 2022-04-11 11:53:03 -0500 | [diff] [blame] | 99 | def dt_chosen_has_compat(kconf, _, chosen, compat): |
| 100 | """ |
| 101 | This function takes a /chosen node property and returns 'y' if the |
| 102 | chosen node has the provided compatible string 'compat' |
| 103 | """ |
| 104 | if doc_mode or edt is None: |
| 105 | return "n" |
| 106 | |
| 107 | node = edt.chosen_node(chosen) |
| 108 | |
| 109 | if node is None: |
| 110 | return "n" |
| 111 | |
| 112 | if compat in node.compats: |
| 113 | return "y" |
| 114 | |
| 115 | return "n" |
Martí Bolívar | 7ff3ebc | 2020-04-23 12:28:06 -0700 | [diff] [blame] | 116 | |
Martí Bolívar | 24677f9 | 2020-04-28 16:52:35 -0700 | [diff] [blame] | 117 | def dt_node_enabled(kconf, name, node): |
| 118 | """ |
| 119 | This function is used to test if a node is enabled (has status |
| 120 | 'okay') or not. |
| 121 | |
| 122 | The 'node' argument is a string which is either a path or an |
| 123 | alias, or both, depending on 'name'. |
| 124 | |
| 125 | If 'name' is 'dt_path_enabled', 'node' is an alias or a path. If |
| 126 | 'name' is 'dt_alias_enabled, 'node' is an alias. |
| 127 | """ |
| 128 | |
| 129 | if doc_mode or edt is None: |
| 130 | return "n" |
| 131 | |
| 132 | if name == "dt_alias_enabled": |
| 133 | if node.startswith("/"): |
| 134 | # EDT.get_node() works with either aliases or paths. If we |
| 135 | # are specifically being asked about an alias, reject paths. |
| 136 | return "n" |
| 137 | else: |
| 138 | # Make sure this is being called appropriately. |
| 139 | assert name == "dt_path_enabled" |
| 140 | |
| 141 | try: |
| 142 | node = edt.get_node(node) |
| 143 | except edtlib.EDTError: |
| 144 | return "n" |
| 145 | |
Martí Bolívar | 8165008 | 2020-10-05 20:02:13 -0700 | [diff] [blame] | 146 | return "y" if node and node.status == "okay" else "n" |
Martí Bolívar | 24677f9 | 2020-04-28 16:52:35 -0700 | [diff] [blame] | 147 | |
| 148 | |
Kumar Gala | 0353d22 | 2020-02-13 23:43:42 -0600 | [diff] [blame] | 149 | def dt_nodelabel_enabled(kconf, _, label): |
| 150 | """ |
Martí Bolívar | 24677f9 | 2020-04-28 16:52:35 -0700 | [diff] [blame] | 151 | This function is like dt_node_enabled(), but the 'label' argument |
| 152 | should be a node label, like "foo" is here: |
| 153 | |
| 154 | foo: some-node { ... }; |
Kumar Gala | 0353d22 | 2020-02-13 23:43:42 -0600 | [diff] [blame] | 155 | """ |
| 156 | if doc_mode or edt is None: |
| 157 | return "n" |
| 158 | |
Martí Bolívar | 0682c46 | 2020-04-22 17:59:23 -0700 | [diff] [blame] | 159 | node = edt.label2node.get(label) |
Kumar Gala | 0353d22 | 2020-02-13 23:43:42 -0600 | [diff] [blame] | 160 | |
Martí Bolívar | 8165008 | 2020-10-05 20:02:13 -0700 | [diff] [blame] | 161 | return "y" if node and node.status == "okay" else "n" |
Kumar Gala | 0353d22 | 2020-02-13 23:43:42 -0600 | [diff] [blame] | 162 | |
| 163 | |
Ulf Magnusson | 73ac146 | 2019-09-23 05:14:18 +0200 | [diff] [blame] | 164 | def _node_reg_addr(node, index, unit): |
| 165 | if not node: |
Kumar Gala | 22e7449 | 2019-10-23 15:15:59 -0500 | [diff] [blame] | 166 | return 0 |
Kumar Gala | 5735397 | 2019-08-28 09:30:23 -0500 | [diff] [blame] | 167 | |
Ulf Magnusson | 73ac146 | 2019-09-23 05:14:18 +0200 | [diff] [blame] | 168 | if not node.regs: |
Kumar Gala | 22e7449 | 2019-10-23 15:15:59 -0500 | [diff] [blame] | 169 | return 0 |
Kumar Gala | 5735397 | 2019-08-28 09:30:23 -0500 | [diff] [blame] | 170 | |
Ulf Magnusson | 73ac146 | 2019-09-23 05:14:18 +0200 | [diff] [blame] | 171 | if int(index) >= len(node.regs): |
Kumar Gala | 22e7449 | 2019-10-23 15:15:59 -0500 | [diff] [blame] | 172 | return 0 |
Kumar Gala | 5735397 | 2019-08-28 09:30:23 -0500 | [diff] [blame] | 173 | |
Kumar Gala | 8af311a | 2020-03-13 12:43:56 -0500 | [diff] [blame] | 174 | if node.regs[int(index)].addr is None: |
| 175 | return 0 |
| 176 | |
Kumar Gala | 22e7449 | 2019-10-23 15:15:59 -0500 | [diff] [blame] | 177 | return node.regs[int(index)].addr >> _dt_units_to_scale(unit) |
Kumar Gala | 5735397 | 2019-08-28 09:30:23 -0500 | [diff] [blame] | 178 | |
| 179 | |
Ulf Magnusson | 73ac146 | 2019-09-23 05:14:18 +0200 | [diff] [blame] | 180 | def _node_reg_size(node, index, unit): |
| 181 | if not node: |
Kumar Gala | 22e7449 | 2019-10-23 15:15:59 -0500 | [diff] [blame] | 182 | return 0 |
Kumar Gala | 5735397 | 2019-08-28 09:30:23 -0500 | [diff] [blame] | 183 | |
Ulf Magnusson | 73ac146 | 2019-09-23 05:14:18 +0200 | [diff] [blame] | 184 | if not node.regs: |
Kumar Gala | 22e7449 | 2019-10-23 15:15:59 -0500 | [diff] [blame] | 185 | return 0 |
Kumar Gala | 5735397 | 2019-08-28 09:30:23 -0500 | [diff] [blame] | 186 | |
Ulf Magnusson | 73ac146 | 2019-09-23 05:14:18 +0200 | [diff] [blame] | 187 | if int(index) >= len(node.regs): |
Kumar Gala | 22e7449 | 2019-10-23 15:15:59 -0500 | [diff] [blame] | 188 | return 0 |
Kumar Gala | 5735397 | 2019-08-28 09:30:23 -0500 | [diff] [blame] | 189 | |
Kumar Gala | 8af311a | 2020-03-13 12:43:56 -0500 | [diff] [blame] | 190 | if node.regs[int(index)].size is None: |
| 191 | return 0 |
| 192 | |
Kumar Gala | 22e7449 | 2019-10-23 15:15:59 -0500 | [diff] [blame] | 193 | return node.regs[int(index)].size >> _dt_units_to_scale(unit) |
Kumar Gala | 5735397 | 2019-08-28 09:30:23 -0500 | [diff] [blame] | 194 | |
| 195 | |
David Leach | 1e5a830 | 2021-11-08 23:24:22 -0600 | [diff] [blame] | 196 | def _node_int_prop(node, prop, unit=None): |
| 197 | """ |
| 198 | This function takes a 'node' and will look to see if that 'node' has a |
| 199 | property called 'prop' and if that 'prop' is an integer type will return |
| 200 | the value of the property 'prop' as either a string int or string hex |
| 201 | value, if not we return 0. |
| 202 | |
| 203 | The function will divide the value based on 'unit': |
| 204 | None No division |
| 205 | 'k' or 'K' divide by 1024 (1 << 10) |
| 206 | 'm' or 'M' divide by 1,048,576 (1 << 20) |
| 207 | 'g' or 'G' divide by 1,073,741,824 (1 << 30) |
Daniel DeGrasse | 6064da8 | 2022-05-25 17:38:59 -0500 | [diff] [blame] | 208 | 'kb' or 'Kb' divide by 8192 (1 << 13) |
| 209 | 'mb' or 'Mb' divide by 8,388,608 (1 << 23) |
| 210 | 'gb' or 'Gb' divide by 8,589,934,592 (1 << 33) |
David Leach | 1e5a830 | 2021-11-08 23:24:22 -0600 | [diff] [blame] | 211 | """ |
Kumar Gala | 338b4319 | 2020-04-03 05:02:35 -0500 | [diff] [blame] | 212 | if not node: |
| 213 | return 0 |
| 214 | |
| 215 | if prop not in node.props: |
| 216 | return 0 |
| 217 | |
| 218 | if node.props[prop].type != "int": |
| 219 | return 0 |
| 220 | |
David Leach | 1e5a830 | 2021-11-08 23:24:22 -0600 | [diff] [blame] | 221 | return node.props[prop].val >> _dt_units_to_scale(unit) |
Kumar Gala | 338b4319 | 2020-04-03 05:02:35 -0500 | [diff] [blame] | 222 | |
| 223 | |
Eivind Jølsgard | 3865e08 | 2022-03-18 10:50:00 +0100 | [diff] [blame] | 224 | def _node_array_prop(node, prop, index=0, unit=None): |
| 225 | """ |
| 226 | This function takes a 'node' and will look to see if that 'node' has a |
| 227 | property called 'prop' and if that 'prop' is an array type will return |
| 228 | the value of the property 'prop' at the given 'index' as either a string int |
| 229 | or string hex value. If the property 'prop' is not found or the given 'index' |
| 230 | is out of range it will return 0. |
| 231 | |
| 232 | The function will divide the value based on 'unit': |
| 233 | None No division |
| 234 | 'k' or 'K' divide by 1024 (1 << 10) |
| 235 | 'm' or 'M' divide by 1,048,576 (1 << 20) |
| 236 | 'g' or 'G' divide by 1,073,741,824 (1 << 30) |
| 237 | """ |
| 238 | if not node: |
| 239 | return 0 |
| 240 | |
| 241 | if prop not in node.props: |
| 242 | return 0 |
| 243 | if node.props[prop].type != "array": |
| 244 | return 0 |
| 245 | if int(index) >= len(node.props[prop].val): |
| 246 | return 0 |
| 247 | return node.props[prop].val[int(index)] >> _dt_units_to_scale(unit) |
| 248 | |
Erwan Gouriou | a59e01d | 2023-12-18 12:03:43 +0100 | [diff] [blame] | 249 | def _node_ph_array_prop(node, prop, index, cell, unit=None): |
| 250 | """ |
| 251 | This function takes a 'node', a property name ('prop'), index ('index') and |
| 252 | a cell ('cell') and it will look to see if that node has a property |
| 253 | called 'prop' and if that 'prop' is an phandle-array type. |
| 254 | Then it will check if that phandle array has a cell matching the given index |
| 255 | and then return the value of the cell named 'cell' in this array index. |
| 256 | If not found it will return 0. |
| 257 | |
| 258 | The function will divide the value based on 'unit': |
| 259 | None No division |
| 260 | 'k' or 'K' divide by 1024 (1 << 10) |
| 261 | 'm' or 'M' divide by 1,048,576 (1 << 20) |
| 262 | 'g' or 'G' divide by 1,073,741,824 (1 << 30) |
| 263 | """ |
| 264 | if not node: |
| 265 | return 0 |
| 266 | |
| 267 | if prop not in node.props: |
| 268 | return 0 |
| 269 | if node.props[prop].type != "phandle-array": |
| 270 | return 0 |
| 271 | if int(index) >= len(node.props[prop].val): |
| 272 | return 0 |
| 273 | if cell not in node.props[prop].val[int(index)].data.keys(): |
| 274 | return 0 |
| 275 | return node.props[prop].val[int(index)].data[cell] >> _dt_units_to_scale(unit) |
Eivind Jølsgard | 3865e08 | 2022-03-18 10:50:00 +0100 | [diff] [blame] | 276 | |
Kumar Gala | 22e7449 | 2019-10-23 15:15:59 -0500 | [diff] [blame] | 277 | def _dt_chosen_reg_addr(kconf, chosen, index=0, unit=None): |
Kumar Gala | 5735397 | 2019-08-28 09:30:23 -0500 | [diff] [blame] | 278 | """ |
| 279 | This function takes a 'chosen' property and treats that property as a path |
Ulf Magnusson | 73ac146 | 2019-09-23 05:14:18 +0200 | [diff] [blame] | 280 | to an EDT node. If it finds an EDT node, it will look to see if that |
Nazar Kazakov | f483b1b | 2022-03-16 21:07:43 +0000 | [diff] [blame] | 281 | node has a register at the given 'index' and return the address value of |
Kumar Gala | 5735397 | 2019-08-28 09:30:23 -0500 | [diff] [blame] | 282 | that reg, if not we return 0. |
| 283 | |
| 284 | The function will divide the value based on 'unit': |
| 285 | None No division |
| 286 | 'k' or 'K' divide by 1024 (1 << 10) |
| 287 | 'm' or 'M' divide by 1,048,576 (1 << 20) |
| 288 | 'g' or 'G' divide by 1,073,741,824 (1 << 30) |
Daniel DeGrasse | 6064da8 | 2022-05-25 17:38:59 -0500 | [diff] [blame] | 289 | 'kb' or 'Kb' divide by 8192 (1 << 13) |
| 290 | 'mb' or 'Mb' divide by 8,388,608 (1 << 23) |
| 291 | 'gb' or 'Gb' divide by 8,589,934,592 (1 << 33) |
Kumar Gala | 5735397 | 2019-08-28 09:30:23 -0500 | [diff] [blame] | 292 | """ |
| 293 | if doc_mode or edt is None: |
Kumar Gala | 22e7449 | 2019-10-23 15:15:59 -0500 | [diff] [blame] | 294 | return 0 |
Kumar Gala | 5735397 | 2019-08-28 09:30:23 -0500 | [diff] [blame] | 295 | |
Ulf Magnusson | 73ac146 | 2019-09-23 05:14:18 +0200 | [diff] [blame] | 296 | node = edt.chosen_node(chosen) |
Kumar Gala | 5735397 | 2019-08-28 09:30:23 -0500 | [diff] [blame] | 297 | |
Ulf Magnusson | 73ac146 | 2019-09-23 05:14:18 +0200 | [diff] [blame] | 298 | return _node_reg_addr(node, index, unit) |
Kumar Gala | 5735397 | 2019-08-28 09:30:23 -0500 | [diff] [blame] | 299 | |
| 300 | |
Kumar Gala | 22e7449 | 2019-10-23 15:15:59 -0500 | [diff] [blame] | 301 | def _dt_chosen_reg_size(kconf, chosen, index=0, unit=None): |
Kumar Gala | 5735397 | 2019-08-28 09:30:23 -0500 | [diff] [blame] | 302 | """ |
| 303 | This function takes a 'chosen' property and treats that property as a path |
Ulf Magnusson | 73ac146 | 2019-09-23 05:14:18 +0200 | [diff] [blame] | 304 | to an EDT node. If it finds an EDT node, it will look to see if that node |
| 305 | has a register at the given 'index' and return the size value of that reg, |
| 306 | if not we return 0. |
Kumar Gala | 5735397 | 2019-08-28 09:30:23 -0500 | [diff] [blame] | 307 | |
| 308 | The function will divide the value based on 'unit': |
| 309 | None No division |
| 310 | 'k' or 'K' divide by 1024 (1 << 10) |
| 311 | 'm' or 'M' divide by 1,048,576 (1 << 20) |
| 312 | 'g' or 'G' divide by 1,073,741,824 (1 << 30) |
Daniel DeGrasse | 6064da8 | 2022-05-25 17:38:59 -0500 | [diff] [blame] | 313 | 'kb' or 'Kb' divide by 8192 (1 << 13) |
| 314 | 'mb' or 'Mb' divide by 8,388,608 (1 << 23) |
| 315 | 'gb' or 'Gb' divide by 8,589,934,592 (1 << 33) |
Kumar Gala | 5735397 | 2019-08-28 09:30:23 -0500 | [diff] [blame] | 316 | """ |
| 317 | if doc_mode or edt is None: |
Kumar Gala | 22e7449 | 2019-10-23 15:15:59 -0500 | [diff] [blame] | 318 | return 0 |
Kumar Gala | 5735397 | 2019-08-28 09:30:23 -0500 | [diff] [blame] | 319 | |
Ulf Magnusson | 73ac146 | 2019-09-23 05:14:18 +0200 | [diff] [blame] | 320 | node = edt.chosen_node(chosen) |
Kumar Gala | 5735397 | 2019-08-28 09:30:23 -0500 | [diff] [blame] | 321 | |
Ulf Magnusson | 73ac146 | 2019-09-23 05:14:18 +0200 | [diff] [blame] | 322 | return _node_reg_size(node, index, unit) |
Kumar Gala | 5735397 | 2019-08-28 09:30:23 -0500 | [diff] [blame] | 323 | |
| 324 | |
Kumar Gala | 22e7449 | 2019-10-23 15:15:59 -0500 | [diff] [blame] | 325 | def dt_chosen_reg(kconf, name, chosen, index=0, unit=None): |
| 326 | """ |
| 327 | This function just routes to the proper function and converts |
| 328 | the result to either a string int or string hex value. |
| 329 | """ |
| 330 | if name == "dt_chosen_reg_size_int": |
| 331 | return str(_dt_chosen_reg_size(kconf, chosen, index, unit)) |
| 332 | if name == "dt_chosen_reg_size_hex": |
| 333 | return hex(_dt_chosen_reg_size(kconf, chosen, index, unit)) |
| 334 | if name == "dt_chosen_reg_addr_int": |
| 335 | return str(_dt_chosen_reg_addr(kconf, chosen, index, unit)) |
| 336 | if name == "dt_chosen_reg_addr_hex": |
| 337 | return hex(_dt_chosen_reg_addr(kconf, chosen, index, unit)) |
| 338 | |
| 339 | |
Gerard Marull-Paretas | 018cf08 | 2024-02-01 10:25:18 +0100 | [diff] [blame] | 340 | def _dt_chosen_partition_addr(kconf, chosen, index=0, unit=None): |
| 341 | """ |
| 342 | This function takes a 'chosen' property and treats that property as a path |
| 343 | to an EDT node. If it finds an EDT node, it will look to see if that |
| 344 | node has a register, and if that node has a grandparent that has a register |
| 345 | at the given 'index'. The addition of both addresses will be returned, if |
| 346 | not, we return 0. |
| 347 | |
| 348 | The function will divide the value based on 'unit': |
| 349 | None No division |
| 350 | 'k' or 'K' divide by 1024 (1 << 10) |
| 351 | 'm' or 'M' divide by 1,048,576 (1 << 20) |
| 352 | 'g' or 'G' divide by 1,073,741,824 (1 << 30) |
| 353 | 'kb' or 'Kb' divide by 8192 (1 << 13) |
| 354 | 'mb' or 'Mb' divide by 8,388,608 (1 << 23) |
| 355 | 'gb' or 'Gb' divide by 8,589,934,592 (1 << 33) |
| 356 | """ |
| 357 | if doc_mode or edt is None: |
| 358 | return 0 |
| 359 | |
| 360 | node = edt.chosen_node(chosen) |
| 361 | if not node: |
| 362 | return 0 |
| 363 | |
| 364 | p_node = node.parent |
| 365 | if not p_node: |
| 366 | return 0 |
| 367 | |
| 368 | return _node_reg_addr(p_node.parent, index, unit) + _node_reg_addr(node, 0, unit) |
| 369 | |
| 370 | |
| 371 | def dt_chosen_partition_addr(kconf, name, chosen, index=0, unit=None): |
| 372 | """ |
| 373 | This function just routes to the proper function and converts |
| 374 | the result to either a string int or string hex value. |
| 375 | """ |
| 376 | if name == "dt_chosen_partition_addr_int": |
| 377 | return str(_dt_chosen_partition_addr(kconf, chosen, index, unit)) |
| 378 | if name == "dt_chosen_partition_addr_hex": |
| 379 | return hex(_dt_chosen_partition_addr(kconf, chosen, index, unit)) |
| 380 | |
| 381 | |
Kumar Gala | 22e7449 | 2019-10-23 15:15:59 -0500 | [diff] [blame] | 382 | def _dt_node_reg_addr(kconf, path, index=0, unit=None): |
Kumar Gala | 5735397 | 2019-08-28 09:30:23 -0500 | [diff] [blame] | 383 | """ |
Ulf Magnusson | 73ac146 | 2019-09-23 05:14:18 +0200 | [diff] [blame] | 384 | This function takes a 'path' and looks for an EDT node at that path. If it |
| 385 | finds an EDT node, it will look to see if that node has a register at the |
| 386 | given 'index' and return the address value of that reg, if not we return 0. |
Kumar Gala | 5735397 | 2019-08-28 09:30:23 -0500 | [diff] [blame] | 387 | |
| 388 | The function will divide the value based on 'unit': |
| 389 | None No division |
| 390 | 'k' or 'K' divide by 1024 (1 << 10) |
| 391 | 'm' or 'M' divide by 1,048,576 (1 << 20) |
| 392 | 'g' or 'G' divide by 1,073,741,824 (1 << 30) |
Daniel DeGrasse | 6064da8 | 2022-05-25 17:38:59 -0500 | [diff] [blame] | 393 | 'kb' or 'Kb' divide by 8192 (1 << 13) |
| 394 | 'mb' or 'Mb' divide by 8,388,608 (1 << 23) |
| 395 | 'gb' or 'Gb' divide by 8,589,934,592 (1 << 33) |
Kumar Gala | 5735397 | 2019-08-28 09:30:23 -0500 | [diff] [blame] | 396 | """ |
| 397 | if doc_mode or edt is None: |
Kumar Gala | 22e7449 | 2019-10-23 15:15:59 -0500 | [diff] [blame] | 398 | return 0 |
Kumar Gala | 5735397 | 2019-08-28 09:30:23 -0500 | [diff] [blame] | 399 | |
| 400 | try: |
Ulf Magnusson | 73ac146 | 2019-09-23 05:14:18 +0200 | [diff] [blame] | 401 | node = edt.get_node(path) |
Kumar Gala | 5735397 | 2019-08-28 09:30:23 -0500 | [diff] [blame] | 402 | except edtlib.EDTError: |
Kumar Gala | 22e7449 | 2019-10-23 15:15:59 -0500 | [diff] [blame] | 403 | return 0 |
Kumar Gala | 5735397 | 2019-08-28 09:30:23 -0500 | [diff] [blame] | 404 | |
Ulf Magnusson | 73ac146 | 2019-09-23 05:14:18 +0200 | [diff] [blame] | 405 | return _node_reg_addr(node, index, unit) |
Kumar Gala | 5735397 | 2019-08-28 09:30:23 -0500 | [diff] [blame] | 406 | |
| 407 | |
Kumar Gala | 22e7449 | 2019-10-23 15:15:59 -0500 | [diff] [blame] | 408 | def _dt_node_reg_size(kconf, path, index=0, unit=None): |
Kumar Gala | 5735397 | 2019-08-28 09:30:23 -0500 | [diff] [blame] | 409 | """ |
Ulf Magnusson | 73ac146 | 2019-09-23 05:14:18 +0200 | [diff] [blame] | 410 | This function takes a 'path' and looks for an EDT node at that path. If it |
| 411 | finds an EDT node, it will look to see if that node has a register at the |
| 412 | given 'index' and return the size value of that reg, if not we return 0. |
Kumar Gala | 5735397 | 2019-08-28 09:30:23 -0500 | [diff] [blame] | 413 | |
| 414 | The function will divide the value based on 'unit': |
| 415 | None No division |
| 416 | 'k' or 'K' divide by 1024 (1 << 10) |
| 417 | 'm' or 'M' divide by 1,048,576 (1 << 20) |
| 418 | 'g' or 'G' divide by 1,073,741,824 (1 << 30) |
Daniel DeGrasse | 6064da8 | 2022-05-25 17:38:59 -0500 | [diff] [blame] | 419 | 'kb' or 'Kb' divide by 8192 (1 << 13) |
| 420 | 'mb' or 'Mb' divide by 8,388,608 (1 << 23) |
| 421 | 'gb' or 'Gb' divide by 8,589,934,592 (1 << 33) |
Kumar Gala | 5735397 | 2019-08-28 09:30:23 -0500 | [diff] [blame] | 422 | """ |
| 423 | if doc_mode or edt is None: |
Kumar Gala | 22e7449 | 2019-10-23 15:15:59 -0500 | [diff] [blame] | 424 | return 0 |
Kumar Gala | 5735397 | 2019-08-28 09:30:23 -0500 | [diff] [blame] | 425 | |
| 426 | try: |
Ulf Magnusson | 73ac146 | 2019-09-23 05:14:18 +0200 | [diff] [blame] | 427 | node = edt.get_node(path) |
Kumar Gala | 5735397 | 2019-08-28 09:30:23 -0500 | [diff] [blame] | 428 | except edtlib.EDTError: |
Kumar Gala | 22e7449 | 2019-10-23 15:15:59 -0500 | [diff] [blame] | 429 | return 0 |
Kumar Gala | 5735397 | 2019-08-28 09:30:23 -0500 | [diff] [blame] | 430 | |
Ulf Magnusson | 73ac146 | 2019-09-23 05:14:18 +0200 | [diff] [blame] | 431 | return _node_reg_size(node, index, unit) |
Kumar Gala | 5735397 | 2019-08-28 09:30:23 -0500 | [diff] [blame] | 432 | |
| 433 | |
Kumar Gala | 22e7449 | 2019-10-23 15:15:59 -0500 | [diff] [blame] | 434 | def dt_node_reg(kconf, name, path, index=0, unit=None): |
| 435 | """ |
| 436 | This function just routes to the proper function and converts |
| 437 | the result to either a string int or string hex value. |
| 438 | """ |
| 439 | if name == "dt_node_reg_size_int": |
| 440 | return str(_dt_node_reg_size(kconf, path, index, unit)) |
| 441 | if name == "dt_node_reg_size_hex": |
| 442 | return hex(_dt_node_reg_size(kconf, path, index, unit)) |
| 443 | if name == "dt_node_reg_addr_int": |
| 444 | return str(_dt_node_reg_addr(kconf, path, index, unit)) |
| 445 | if name == "dt_node_reg_addr_hex": |
| 446 | return hex(_dt_node_reg_addr(kconf, path, index, unit)) |
| 447 | |
Jordan Yates | 999afdc | 2023-07-30 13:33:33 +1000 | [diff] [blame] | 448 | def dt_nodelabel_reg(kconf, name, label, index=0, unit=None): |
| 449 | """ |
| 450 | This function is like dt_node_reg(), but the 'label' argument |
| 451 | should be a node label, like "foo" is here: |
| 452 | |
| 453 | foo: some-node { ... }; |
| 454 | """ |
| 455 | if doc_mode or edt is None: |
| 456 | node = None |
| 457 | else: |
| 458 | node = edt.label2node.get(label) |
| 459 | |
| 460 | if name == "dt_nodelabel_reg_size_int": |
| 461 | return str(_dt_node_reg_size(kconf, node.path, index, unit)) if node else "0" |
| 462 | if name == "dt_nodelabel_reg_size_hex": |
| 463 | return hex(_dt_node_reg_size(kconf, node.path, index, unit)) if node else "0x0" |
| 464 | if name == "dt_nodelabel_reg_addr_int": |
| 465 | return str(_dt_node_reg_addr(kconf, node.path, index, unit)) if node else "0" |
| 466 | if name == "dt_nodelabel_reg_addr_hex": |
| 467 | return hex(_dt_node_reg_addr(kconf, node.path, index, unit)) if node else "0x0" |
| 468 | |
| 469 | |
Michał Barnaś | a1ab8da | 2022-03-08 17:44:04 +0100 | [diff] [blame] | 470 | def _dt_node_bool_prop_generic(node_search_function, search_arg, prop): |
Kumar Gala | 5735397 | 2019-08-28 09:30:23 -0500 | [diff] [blame] | 471 | """ |
Michał Barnaś | a1ab8da | 2022-03-08 17:44:04 +0100 | [diff] [blame] | 472 | This function takes the 'node_search_function' and uses it to search for |
| 473 | a node with 'search_arg' and if node exists, checks if 'prop' exists |
| 474 | inside the node and is a boolean, if it is true, returns "y". |
| 475 | Otherwise, it returns "n". |
Kumar Gala | 5735397 | 2019-08-28 09:30:23 -0500 | [diff] [blame] | 476 | """ |
Kumar Gala | 5735397 | 2019-08-28 09:30:23 -0500 | [diff] [blame] | 477 | try: |
Michał Barnaś | a1ab8da | 2022-03-08 17:44:04 +0100 | [diff] [blame] | 478 | node = node_search_function(search_arg) |
Kumar Gala | 5735397 | 2019-08-28 09:30:23 -0500 | [diff] [blame] | 479 | except edtlib.EDTError: |
| 480 | return "n" |
| 481 | |
Andrzej Głąbek | 2399433 | 2022-03-23 14:45:11 +0100 | [diff] [blame] | 482 | if node is None: |
| 483 | return "n" |
| 484 | |
Ulf Magnusson | 73ac146 | 2019-09-23 05:14:18 +0200 | [diff] [blame] | 485 | if prop not in node.props: |
Kumar Gala | 5735397 | 2019-08-28 09:30:23 -0500 | [diff] [blame] | 486 | return "n" |
| 487 | |
Ulf Magnusson | 73ac146 | 2019-09-23 05:14:18 +0200 | [diff] [blame] | 488 | if node.props[prop].type != "boolean": |
Kumar Gala | 5735397 | 2019-08-28 09:30:23 -0500 | [diff] [blame] | 489 | return "n" |
| 490 | |
Ulf Magnusson | 73ac146 | 2019-09-23 05:14:18 +0200 | [diff] [blame] | 491 | if node.props[prop].val: |
Kumar Gala | 5735397 | 2019-08-28 09:30:23 -0500 | [diff] [blame] | 492 | return "y" |
| 493 | |
| 494 | return "n" |
| 495 | |
Michał Barnaś | a1ab8da | 2022-03-08 17:44:04 +0100 | [diff] [blame] | 496 | def dt_node_bool_prop(kconf, _, path, prop): |
Erwan Gouriou | b711028 | 2021-01-07 12:06:31 +0100 | [diff] [blame] | 497 | """ |
Michał Barnaś | a1ab8da | 2022-03-08 17:44:04 +0100 | [diff] [blame] | 498 | This function takes a 'path' and looks for an EDT node at that path. If it |
| 499 | finds an EDT node, it will look to see if that node has a boolean property |
Erwan Gouriou | b711028 | 2021-01-07 12:06:31 +0100 | [diff] [blame] | 500 | by the name of 'prop'. If the 'prop' exists it will return "y" otherwise |
| 501 | we return "n". |
| 502 | """ |
Erwan Gouriou | b711028 | 2021-01-07 12:06:31 +0100 | [diff] [blame] | 503 | if doc_mode or edt is None: |
| 504 | return "n" |
| 505 | |
Michał Barnaś | a1ab8da | 2022-03-08 17:44:04 +0100 | [diff] [blame] | 506 | return _dt_node_bool_prop_generic(edt.get_node, path, prop) |
| 507 | |
| 508 | def dt_nodelabel_bool_prop(kconf, _, label, prop): |
| 509 | """ |
| 510 | This function takes a 'label' and looks for an EDT node with that label. |
| 511 | If it finds an EDT node, it will look to see if that node has a boolean |
| 512 | property by the name of 'prop'. If the 'prop' exists it will return "y" |
| 513 | otherwise we return "n". |
| 514 | """ |
| 515 | if doc_mode or edt is None: |
| 516 | return "n" |
| 517 | |
| 518 | return _dt_node_bool_prop_generic(edt.label2node.get, label, prop) |
| 519 | |
Stephen Stauts | d4b8db7 | 2022-12-27 11:40:17 +0100 | [diff] [blame] | 520 | def dt_chosen_bool_prop(kconf, _, chosen, prop): |
| 521 | """ |
| 522 | This function takes a /chosen node property named 'chosen', and |
| 523 | looks for the chosen node. If that node exists and has a boolean |
| 524 | property 'prop', it returns "y". Otherwise, it returns "n". |
| 525 | """ |
| 526 | if doc_mode or edt is None: |
| 527 | return "n" |
| 528 | |
| 529 | return _dt_node_bool_prop_generic(edt.chosen_node, chosen, prop) |
| 530 | |
Michał Barnaś | a1ab8da | 2022-03-08 17:44:04 +0100 | [diff] [blame] | 531 | def _dt_node_has_prop_generic(node_search_function, search_arg, prop): |
| 532 | """ |
| 533 | This function takes the 'node_search_function' and uses it to search for |
| 534 | a node with 'search_arg' and if node exists, then checks if 'prop' |
| 535 | exists inside the node and returns "y". Otherwise, it returns "n". |
| 536 | """ |
Erwan Gouriou | b711028 | 2021-01-07 12:06:31 +0100 | [diff] [blame] | 537 | try: |
Michał Barnaś | a1ab8da | 2022-03-08 17:44:04 +0100 | [diff] [blame] | 538 | node = node_search_function(search_arg) |
Erwan Gouriou | b711028 | 2021-01-07 12:06:31 +0100 | [diff] [blame] | 539 | except edtlib.EDTError: |
| 540 | return "n" |
| 541 | |
| 542 | if node is None: |
| 543 | return "n" |
| 544 | |
| 545 | if prop in node.props: |
| 546 | return "y" |
| 547 | |
| 548 | return "n" |
Kumar Gala | 5735397 | 2019-08-28 09:30:23 -0500 | [diff] [blame] | 549 | |
Michał Barnaś | a1ab8da | 2022-03-08 17:44:04 +0100 | [diff] [blame] | 550 | def dt_node_has_prop(kconf, _, path, prop): |
| 551 | """ |
| 552 | This function takes a 'path' and looks for an EDT node at that path. If it |
| 553 | finds an EDT node, it will look to see if that node has a property |
| 554 | by the name of 'prop'. If the 'prop' exists it will return "y" otherwise |
| 555 | it returns "n". |
| 556 | """ |
| 557 | if doc_mode or edt is None: |
| 558 | return "n" |
| 559 | |
| 560 | return _dt_node_has_prop_generic(edt.get_node, path, prop) |
| 561 | |
| 562 | def dt_nodelabel_has_prop(kconf, _, label, prop): |
| 563 | """ |
| 564 | This function takes a 'label' and looks for an EDT node with that label. |
| 565 | If it finds an EDT node, it will look to see if that node has a property |
| 566 | by the name of 'prop'. If the 'prop' exists it will return "y" otherwise |
| 567 | it returns "n". |
| 568 | """ |
| 569 | if doc_mode or edt is None: |
| 570 | return "n" |
| 571 | |
| 572 | return _dt_node_has_prop_generic(edt.label2node.get, label, prop) |
| 573 | |
David Leach | 1e5a830 | 2021-11-08 23:24:22 -0600 | [diff] [blame] | 574 | def dt_node_int_prop(kconf, name, path, prop, unit=None): |
Kumar Gala | 338b4319 | 2020-04-03 05:02:35 -0500 | [diff] [blame] | 575 | """ |
| 576 | This function takes a 'path' and property name ('prop') looks for an EDT |
| 577 | node at that path. If it finds an EDT node, it will look to see if that |
| 578 | node has a property called 'prop' and if that 'prop' is an integer type |
| 579 | will return the value of the property 'prop' as either a string int or |
| 580 | string hex value, if not we return 0. |
Kumar Gala | 338b4319 | 2020-04-03 05:02:35 -0500 | [diff] [blame] | 581 | |
David Leach | 1e5a830 | 2021-11-08 23:24:22 -0600 | [diff] [blame] | 582 | The function will divide the value based on 'unit': |
| 583 | None No division |
| 584 | 'k' or 'K' divide by 1024 (1 << 10) |
| 585 | 'm' or 'M' divide by 1,048,576 (1 << 20) |
| 586 | 'g' or 'G' divide by 1,073,741,824 (1 << 30) |
Daniel DeGrasse | 6064da8 | 2022-05-25 17:38:59 -0500 | [diff] [blame] | 587 | 'kb' or 'Kb' divide by 8192 (1 << 13) |
| 588 | 'mb' or 'Mb' divide by 8,388,608 (1 << 23) |
| 589 | 'gb' or 'Gb' divide by 8,589,934,592 (1 << 33) |
David Leach | 1e5a830 | 2021-11-08 23:24:22 -0600 | [diff] [blame] | 590 | """ |
Kumar Gala | 338b4319 | 2020-04-03 05:02:35 -0500 | [diff] [blame] | 591 | if doc_mode or edt is None: |
| 592 | return "0" |
| 593 | |
| 594 | try: |
| 595 | node = edt.get_node(path) |
| 596 | except edtlib.EDTError: |
| 597 | return "0" |
| 598 | |
| 599 | if name == "dt_node_int_prop_int": |
David Leach | 1e5a830 | 2021-11-08 23:24:22 -0600 | [diff] [blame] | 600 | return str(_node_int_prop(node, prop, unit)) |
Kumar Gala | 338b4319 | 2020-04-03 05:02:35 -0500 | [diff] [blame] | 601 | if name == "dt_node_int_prop_hex": |
David Leach | 1e5a830 | 2021-11-08 23:24:22 -0600 | [diff] [blame] | 602 | return hex(_node_int_prop(node, prop, unit)) |
Kumar Gala | 338b4319 | 2020-04-03 05:02:35 -0500 | [diff] [blame] | 603 | |
Eivind Jølsgard | 3865e08 | 2022-03-18 10:50:00 +0100 | [diff] [blame] | 604 | |
| 605 | def dt_node_array_prop(kconf, name, path, prop, index, unit=None): |
| 606 | """ |
| 607 | This function takes a 'path', property name ('prop') and index ('index') |
| 608 | and looks for an EDT node at that path. If it finds an EDT node, it will |
| 609 | look to see if that node has a property called 'prop' and if that 'prop' |
| 610 | is an array type will return the value of the property 'prop' at the given |
| 611 | 'index' as either a string int or string hex value. If not found we return 0. |
| 612 | |
| 613 | The function will divide the value based on 'unit': |
| 614 | None No division |
| 615 | 'k' or 'K' divide by 1024 (1 << 10) |
| 616 | 'm' or 'M' divide by 1,048,576 (1 << 20) |
| 617 | 'g' or 'G' divide by 1,073,741,824 (1 << 30) |
| 618 | """ |
| 619 | if doc_mode or edt is None: |
| 620 | return "0" |
| 621 | |
| 622 | try: |
| 623 | node = edt.get_node(path) |
| 624 | except edtlib.EDTError: |
| 625 | return "0" |
| 626 | if name == "dt_node_array_prop_int": |
| 627 | return str(_node_array_prop(node, prop, index, unit)) |
| 628 | if name == "dt_node_array_prop_hex": |
| 629 | return hex(_node_array_prop(node, prop, index, unit)) |
| 630 | |
| 631 | |
Erwan Gouriou | a59e01d | 2023-12-18 12:03:43 +0100 | [diff] [blame] | 632 | def dt_node_ph_array_prop(kconf, name, path, prop, index, cell, unit=None): |
| 633 | """ |
| 634 | This function takes a 'path', property name ('prop'), index ('index') and |
| 635 | a cell ('cell') and looks for an EDT node at that path. |
| 636 | If it finds an EDT node, it will look to see if that node has a property |
| 637 | called 'prop' and if that 'prop' is an phandle-array type. |
| 638 | Then it will check if that phandle array has a cell matching the given index |
| 639 | and ten return the value of the cell named 'cell' in this array index as |
| 640 | either a string int or string hex value. If not found we return 0. |
| 641 | |
| 642 | The function will divide the value based on 'unit': |
| 643 | None No division |
| 644 | 'k' or 'K' divide by 1024 (1 << 10) |
| 645 | 'm' or 'M' divide by 1,048,576 (1 << 20) |
| 646 | 'g' or 'G' divide by 1,073,741,824 (1 << 30) |
| 647 | """ |
| 648 | if doc_mode or edt is None: |
| 649 | return "0" |
| 650 | |
| 651 | try: |
| 652 | node = edt.get_node(path) |
| 653 | except edtlib.EDTError: |
| 654 | return "0" |
| 655 | if name == "dt_node_ph_array_prop_int": |
| 656 | return str(_node_ph_array_prop(node, prop, index, cell, unit)) |
| 657 | if name == "dt_node_ph_array_prop_hex": |
| 658 | return hex(_node_ph_array_prop(node, prop, index, cell, unit)) |
| 659 | |
Johann Fischer | 73199f2 | 2021-11-18 17:12:23 +0100 | [diff] [blame] | 660 | def dt_node_str_prop_equals(kconf, _, path, prop, val): |
| 661 | """ |
| 662 | This function takes a 'path' and property name ('prop') looks for an EDT |
| 663 | node at that path. If it finds an EDT node, it will look to see if that |
| 664 | node has a property 'prop' of type string. If that 'prop' is equal to 'val' |
| 665 | it will return "y" otherwise return "n". |
| 666 | """ |
| 667 | |
| 668 | if doc_mode or edt is None: |
| 669 | return "n" |
| 670 | |
| 671 | try: |
| 672 | node = edt.get_node(path) |
| 673 | except edtlib.EDTError: |
| 674 | return "n" |
| 675 | |
| 676 | if prop not in node.props: |
| 677 | return "n" |
| 678 | |
| 679 | if node.props[prop].type != "string": |
| 680 | return "n" |
| 681 | |
| 682 | if node.props[prop].val == val: |
| 683 | return "y" |
| 684 | |
| 685 | return "n" |
Kumar Gala | 338b4319 | 2020-04-03 05:02:35 -0500 | [diff] [blame] | 686 | |
Andrzej Głąbek | 9f2a27b | 2022-03-15 14:08:21 +0100 | [diff] [blame] | 687 | |
| 688 | def dt_has_compat(kconf, _, compat): |
| 689 | """ |
| 690 | This function takes a 'compat' and returns "y" if any compatible node |
| 691 | can be found in the EDT, otherwise it returns "n". |
| 692 | """ |
| 693 | if doc_mode or edt is None: |
| 694 | return "n" |
| 695 | |
| 696 | return "y" if compat in edt.compat2nodes else "n" |
| 697 | |
| 698 | |
Kumar Gala | 5735397 | 2019-08-28 09:30:23 -0500 | [diff] [blame] | 699 | def dt_compat_enabled(kconf, _, compat): |
| 700 | """ |
Martí Bolívar | 8165008 | 2020-10-05 20:02:13 -0700 | [diff] [blame] | 701 | This function takes a 'compat' and returns "y" if we find a status "okay" |
Ulf Magnusson | 73ac146 | 2019-09-23 05:14:18 +0200 | [diff] [blame] | 702 | compatible node in the EDT otherwise we return "n" |
Kumar Gala | 5735397 | 2019-08-28 09:30:23 -0500 | [diff] [blame] | 703 | """ |
| 704 | if doc_mode or edt is None: |
| 705 | return "n" |
| 706 | |
Martí Bolívar | 8165008 | 2020-10-05 20:02:13 -0700 | [diff] [blame] | 707 | return "y" if compat in edt.compat2okay else "n" |
Kumar Gala | 5735397 | 2019-08-28 09:30:23 -0500 | [diff] [blame] | 708 | |
| 709 | |
Martí Bolívar | 3cd4f6c | 2020-04-10 15:37:58 -0700 | [diff] [blame] | 710 | def dt_compat_on_bus(kconf, _, compat, bus): |
| 711 | """ |
| 712 | This function takes a 'compat' and returns "y" if we find an "enabled" |
| 713 | compatible node in the EDT which is on bus 'bus'. It returns "n" otherwise. |
| 714 | """ |
| 715 | if doc_mode or edt is None: |
| 716 | return "n" |
| 717 | |
Andrzej Głąbek | f9c1f89 | 2021-02-03 13:53:48 +0100 | [diff] [blame] | 718 | if compat in edt.compat2okay: |
| 719 | for node in edt.compat2okay[compat]: |
Daniel Leung | 418c915 | 2022-08-26 10:52:32 -0700 | [diff] [blame] | 720 | if node.on_buses is not None and bus in node.on_buses: |
Andrzej Głąbek | f9c1f89 | 2021-02-03 13:53:48 +0100 | [diff] [blame] | 721 | return "y" |
Martí Bolívar | 3cd4f6c | 2020-04-10 15:37:58 -0700 | [diff] [blame] | 722 | |
| 723 | return "n" |
| 724 | |
| 725 | |
Andrzej Głąbek | 3331a20 | 2020-02-14 11:53:21 +0100 | [diff] [blame] | 726 | def dt_nodelabel_has_compat(kconf, _, label, compat): |
| 727 | """ |
Andrzej Głąbek | cd00a3a | 2022-03-28 12:16:51 +0200 | [diff] [blame] | 728 | This function takes a 'label' and looks for an EDT node with that label. |
| 729 | If it finds such node, it returns "y" if this node is compatible with |
| 730 | the provided 'compat'. Otherwise, it return "n" . |
| 731 | """ |
| 732 | if doc_mode or edt is None: |
| 733 | return "n" |
| 734 | |
| 735 | node = edt.label2node.get(label) |
| 736 | |
| 737 | if node and compat in node.compats: |
| 738 | return "y" |
| 739 | |
| 740 | return "n" |
| 741 | |
Daniel DeGrasse | 6aa7194 | 2022-08-26 12:12:24 -0500 | [diff] [blame] | 742 | def dt_node_has_compat(kconf, _, path, compat): |
| 743 | """ |
| 744 | This function takes a 'path' and looks for an EDT node at that path. If it |
| 745 | finds an EDT node, it returns "y" if this node is compatible with |
| 746 | the provided 'compat'. Otherwise, it return "n" . |
| 747 | """ |
| 748 | |
| 749 | if doc_mode or edt is None: |
| 750 | return "n" |
| 751 | |
| 752 | try: |
| 753 | node = edt.get_node(path) |
| 754 | except edtlib.EDTError: |
| 755 | return "n" |
| 756 | |
| 757 | if node and compat in node.compats: |
| 758 | return "y" |
| 759 | |
| 760 | return "n" |
Andrzej Głąbek | cd00a3a | 2022-03-28 12:16:51 +0200 | [diff] [blame] | 761 | |
| 762 | def dt_nodelabel_enabled_with_compat(kconf, _, label, compat): |
| 763 | """ |
Andrzej Głąbek | 3331a20 | 2020-02-14 11:53:21 +0100 | [diff] [blame] | 764 | This function takes a 'label' and returns "y" if an "enabled" node with |
| 765 | such label can be found in the EDT and that node is compatible with the |
| 766 | provided 'compat', otherwise it returns "n". |
| 767 | """ |
| 768 | if doc_mode or edt is None: |
| 769 | return "n" |
| 770 | |
Andrzej Głąbek | f9c1f89 | 2021-02-03 13:53:48 +0100 | [diff] [blame] | 771 | if compat in edt.compat2okay: |
| 772 | for node in edt.compat2okay[compat]: |
| 773 | if label in node.labels: |
| 774 | return "y" |
Andrzej Głąbek | 3331a20 | 2020-02-14 11:53:21 +0100 | [diff] [blame] | 775 | |
| 776 | return "n" |
| 777 | |
| 778 | |
Stephen Stauts | d4b8db7 | 2022-12-27 11:40:17 +0100 | [diff] [blame] | 779 | def dt_nodelabel_array_prop_has_val(kconf, _, label, prop, val): |
| 780 | """ |
| 781 | This function looks for a node with node label 'label'. |
| 782 | If the node exists, it checks if the node node has a property |
| 783 | 'prop' with type "array". If so, and the property contains |
| 784 | an element equal to the integer 'val', it returns "y". |
| 785 | Otherwise, it returns "n". |
| 786 | """ |
| 787 | if doc_mode or edt is None: |
| 788 | return "n" |
| 789 | |
| 790 | node = edt.label2node.get(label) |
| 791 | |
| 792 | if not node or (prop not in node.props) or (node.props[prop].type != "array"): |
| 793 | return "n" |
| 794 | else: |
| 795 | return "y" if int(val, base=0) in node.props[prop].val else "n" |
| 796 | |
| 797 | |
Martí Bolívar | 1fff235 | 2020-04-22 18:06:18 -0700 | [diff] [blame] | 798 | def dt_nodelabel_path(kconf, _, label): |
| 799 | """ |
| 800 | This function takes a node label (not a label property) and |
| 801 | returns the path to the node which has that label, or an empty |
| 802 | string if there is no such node. |
| 803 | """ |
| 804 | if doc_mode or edt is None: |
| 805 | return "" |
| 806 | |
| 807 | node = edt.label2node.get(label) |
| 808 | |
| 809 | return node.path if node else "" |
| 810 | |
Daniel DeGrasse | db9bcd9 | 2022-08-26 12:10:14 -0500 | [diff] [blame] | 811 | def dt_node_parent(kconf, _, path): |
| 812 | """ |
| 813 | This function takes a 'path' and looks for an EDT node at that path. If it |
| 814 | finds an EDT node, it will look for the parent of that node. If the parent |
| 815 | exists, it will return the path to that parent. Otherwise, an empty string |
| 816 | will be returned. |
| 817 | """ |
| 818 | if doc_mode or edt is None: |
| 819 | return "" |
| 820 | |
| 821 | try: |
| 822 | node = edt.get_node(path) |
| 823 | except edtlib.EDTError: |
| 824 | return "" |
| 825 | |
| 826 | if node is None: |
| 827 | return "" |
| 828 | |
| 829 | return node.parent.path if node.parent else "" |
Martí Bolívar | 1fff235 | 2020-04-22 18:06:18 -0700 | [diff] [blame] | 830 | |
Henrik Brix Andersen | fba6c7f | 2023-01-18 17:43:02 +0100 | [diff] [blame] | 831 | def dt_gpio_hogs_enabled(kconf, _): |
| 832 | """ |
| 833 | Return "y" if any GPIO hog node is enabled. Otherwise, return "n". |
| 834 | """ |
| 835 | if doc_mode or edt is None: |
| 836 | return "n" |
| 837 | |
| 838 | for node in edt.nodes: |
| 839 | if node.gpio_hogs and node.status == "okay": |
| 840 | return "y" |
| 841 | |
| 842 | return "n" |
| 843 | |
Torsten Rasmussen | 8dc3f85 | 2022-09-14 22:23:15 +0200 | [diff] [blame] | 844 | |
Torsten Rasmussen | 732c504 | 2024-03-19 15:26:59 +0100 | [diff] [blame] | 845 | def normalize_upper(kconf, _, string): |
Torsten Rasmussen | 8dc3f85 | 2022-09-14 22:23:15 +0200 | [diff] [blame] | 846 | """ |
Torsten Rasmussen | 732c504 | 2024-03-19 15:26:59 +0100 | [diff] [blame] | 847 | Normalize the string, so that the string only contains alpha-numeric |
Torsten Rasmussen | 8dc3f85 | 2022-09-14 22:23:15 +0200 | [diff] [blame] | 848 | characters or underscores. All non-alpha-numeric characters are replaced |
| 849 | with an underscore, '_'. |
Torsten Rasmussen | 732c504 | 2024-03-19 15:26:59 +0100 | [diff] [blame] | 850 | When string has been normalized it will be converted into upper case. |
Torsten Rasmussen | 8dc3f85 | 2022-09-14 22:23:15 +0200 | [diff] [blame] | 851 | """ |
| 852 | return re.sub(r'[^a-zA-Z0-9_]', '_', string).upper() |
| 853 | |
| 854 | |
Erwan Gouriou | 17537c7 | 2019-11-22 10:06:57 +0100 | [diff] [blame] | 855 | def shields_list_contains(kconf, _, shield): |
| 856 | """ |
| 857 | Return "n" if cmake environment variable 'SHIELD_AS_LIST' doesn't exist. |
| 858 | Return "y" if 'shield' is present list obtained after 'SHIELD_AS_LIST' |
| 859 | has been split using ";" as a separator and "n" otherwise. |
| 860 | """ |
| 861 | try: |
| 862 | list = os.environ['SHIELD_AS_LIST'] |
| 863 | except KeyError: |
| 864 | return "n" |
| 865 | |
| 866 | return "y" if shield in list.split(";") else "n" |
| 867 | |
| 868 | |
Jamie McCrae | a430294 | 2024-03-13 08:21:33 +0000 | [diff] [blame] | 869 | def substring(kconf, _, string, start, stop=None): |
| 870 | """ |
| 871 | Extracts a portion of the string, removing characters from the front, back or both. |
| 872 | """ |
| 873 | if stop is not None: |
| 874 | return string[int(start):int(stop)] |
| 875 | else: |
| 876 | return string[int(start):] |
| 877 | |
| 878 | |
Martí Bolívar | 006319f | 2020-07-21 15:20:18 -0700 | [diff] [blame] | 879 | # Keys in this dict are the function names as they appear |
| 880 | # in Kconfig files. The values are tuples in this form: |
| 881 | # |
| 882 | # (python_function, minimum_number_of_args, maximum_number_of_args) |
| 883 | # |
| 884 | # Each python function is given a kconf object and its name in the |
| 885 | # Kconfig file, followed by arguments from the Kconfig file. |
| 886 | # |
| 887 | # See the kconfiglib documentation for more details. |
Kumar Gala | 87915cd | 2018-11-15 11:51:50 -0600 | [diff] [blame] | 888 | functions = { |
Andrzej Głąbek | 9f2a27b | 2022-03-15 14:08:21 +0100 | [diff] [blame] | 889 | "dt_has_compat": (dt_has_compat, 1, 1), |
Kumar Gala | 5735397 | 2019-08-28 09:30:23 -0500 | [diff] [blame] | 890 | "dt_compat_enabled": (dt_compat_enabled, 1, 1), |
Martí Bolívar | 3cd4f6c | 2020-04-10 15:37:58 -0700 | [diff] [blame] | 891 | "dt_compat_on_bus": (dt_compat_on_bus, 2, 2), |
Kumar Gala | 5735397 | 2019-08-28 09:30:23 -0500 | [diff] [blame] | 892 | "dt_chosen_label": (dt_chosen_label, 1, 1), |
Kumar Gala | 07e5d89 | 2019-11-04 10:20:59 -0600 | [diff] [blame] | 893 | "dt_chosen_enabled": (dt_chosen_enabled, 1, 1), |
Martí Bolívar | 7ff3ebc | 2020-04-23 12:28:06 -0700 | [diff] [blame] | 894 | "dt_chosen_path": (dt_chosen_path, 1, 1), |
Daniel DeGrasse | fbefc50 | 2022-04-11 11:53:03 -0500 | [diff] [blame] | 895 | "dt_chosen_has_compat": (dt_chosen_has_compat, 2, 2), |
Martí Bolívar | 24677f9 | 2020-04-28 16:52:35 -0700 | [diff] [blame] | 896 | "dt_path_enabled": (dt_node_enabled, 1, 1), |
| 897 | "dt_alias_enabled": (dt_node_enabled, 1, 1), |
Kumar Gala | 0353d22 | 2020-02-13 23:43:42 -0600 | [diff] [blame] | 898 | "dt_nodelabel_enabled": (dt_nodelabel_enabled, 1, 1), |
Andrzej Głąbek | cd00a3a | 2022-03-28 12:16:51 +0200 | [diff] [blame] | 899 | "dt_nodelabel_enabled_with_compat": (dt_nodelabel_enabled_with_compat, 2, 2), |
Ulf Magnusson | b4e1807 | 2019-12-23 11:07:27 +0100 | [diff] [blame] | 900 | "dt_chosen_reg_addr_int": (dt_chosen_reg, 1, 3), |
| 901 | "dt_chosen_reg_addr_hex": (dt_chosen_reg, 1, 3), |
| 902 | "dt_chosen_reg_size_int": (dt_chosen_reg, 1, 3), |
| 903 | "dt_chosen_reg_size_hex": (dt_chosen_reg, 1, 3), |
| 904 | "dt_node_reg_addr_int": (dt_node_reg, 1, 3), |
| 905 | "dt_node_reg_addr_hex": (dt_node_reg, 1, 3), |
| 906 | "dt_node_reg_size_int": (dt_node_reg, 1, 3), |
| 907 | "dt_node_reg_size_hex": (dt_node_reg, 1, 3), |
Jordan Yates | 999afdc | 2023-07-30 13:33:33 +1000 | [diff] [blame] | 908 | "dt_nodelabel_reg_addr_int": (dt_nodelabel_reg, 1, 3), |
| 909 | "dt_nodelabel_reg_addr_hex": (dt_nodelabel_reg, 1, 3), |
| 910 | "dt_nodelabel_reg_size_int": (dt_nodelabel_reg, 1, 3), |
| 911 | "dt_nodelabel_reg_size_hex": (dt_nodelabel_reg, 1, 3), |
Michał Barnaś | a1ab8da | 2022-03-08 17:44:04 +0100 | [diff] [blame] | 912 | "dt_node_bool_prop": (dt_node_bool_prop, 2, 2), |
| 913 | "dt_nodelabel_bool_prop": (dt_nodelabel_bool_prop, 2, 2), |
Stephen Stauts | d4b8db7 | 2022-12-27 11:40:17 +0100 | [diff] [blame] | 914 | "dt_chosen_bool_prop": (dt_chosen_bool_prop, 2, 2), |
Erwan Gouriou | b711028 | 2021-01-07 12:06:31 +0100 | [diff] [blame] | 915 | "dt_node_has_prop": (dt_node_has_prop, 2, 2), |
Michał Barnaś | a1ab8da | 2022-03-08 17:44:04 +0100 | [diff] [blame] | 916 | "dt_nodelabel_has_prop": (dt_nodelabel_has_prop, 2, 2), |
David Leach | 1e5a830 | 2021-11-08 23:24:22 -0600 | [diff] [blame] | 917 | "dt_node_int_prop_int": (dt_node_int_prop, 2, 3), |
| 918 | "dt_node_int_prop_hex": (dt_node_int_prop, 2, 3), |
Eivind Jølsgard | 3865e08 | 2022-03-18 10:50:00 +0100 | [diff] [blame] | 919 | "dt_node_array_prop_int": (dt_node_array_prop, 3, 4), |
| 920 | "dt_node_array_prop_hex": (dt_node_array_prop, 3, 4), |
Erwan Gouriou | a59e01d | 2023-12-18 12:03:43 +0100 | [diff] [blame] | 921 | "dt_node_ph_array_prop_int": (dt_node_ph_array_prop, 4, 5), |
| 922 | "dt_node_ph_array_prop_hex": (dt_node_ph_array_prop, 4, 5), |
Johann Fischer | 73199f2 | 2021-11-18 17:12:23 +0100 | [diff] [blame] | 923 | "dt_node_str_prop_equals": (dt_node_str_prop_equals, 3, 3), |
Andrzej Głąbek | 3331a20 | 2020-02-14 11:53:21 +0100 | [diff] [blame] | 924 | "dt_nodelabel_has_compat": (dt_nodelabel_has_compat, 2, 2), |
Daniel DeGrasse | 6aa7194 | 2022-08-26 12:12:24 -0500 | [diff] [blame] | 925 | "dt_node_has_compat": (dt_node_has_compat, 2, 2), |
Martí Bolívar | 1fff235 | 2020-04-22 18:06:18 -0700 | [diff] [blame] | 926 | "dt_nodelabel_path": (dt_nodelabel_path, 1, 1), |
Daniel DeGrasse | db9bcd9 | 2022-08-26 12:10:14 -0500 | [diff] [blame] | 927 | "dt_node_parent": (dt_node_parent, 1, 1), |
Stephen Stauts | d4b8db7 | 2022-12-27 11:40:17 +0100 | [diff] [blame] | 928 | "dt_nodelabel_array_prop_has_val": (dt_nodelabel_array_prop_has_val, 3, 3), |
Henrik Brix Andersen | fba6c7f | 2023-01-18 17:43:02 +0100 | [diff] [blame] | 929 | "dt_gpio_hogs_enabled": (dt_gpio_hogs_enabled, 0, 0), |
Gerard Marull-Paretas | 018cf08 | 2024-02-01 10:25:18 +0100 | [diff] [blame] | 930 | "dt_chosen_partition_addr_int": (dt_chosen_partition_addr, 1, 3), |
| 931 | "dt_chosen_partition_addr_hex": (dt_chosen_partition_addr, 1, 3), |
Torsten Rasmussen | 732c504 | 2024-03-19 15:26:59 +0100 | [diff] [blame] | 932 | "normalize_upper": (normalize_upper, 1, 1), |
Erwan Gouriou | 17537c7 | 2019-11-22 10:06:57 +0100 | [diff] [blame] | 933 | "shields_list_contains": (shields_list_contains, 1, 1), |
Jamie McCrae | a430294 | 2024-03-13 08:21:33 +0000 | [diff] [blame] | 934 | "substring": (substring, 2, 3), |
Kumar Gala | 87915cd | 2018-11-15 11:51:50 -0600 | [diff] [blame] | 935 | } |