Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Simon Glass | c4fc562 | 2017-05-18 20:08:58 -0600 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (c) 2017 Google, Inc |
| 4 | * Written by Simon Glass <sjg@chromium.org> |
Simon Glass | c4fc562 | 2017-05-18 20:08:58 -0600 | [diff] [blame] | 5 | */ |
| 6 | |
| 7 | #include <common.h> |
| 8 | #include <dm.h> |
| 9 | #include <fdtdec.h> |
| 10 | #include <fdt_support.h> |
Masahiro Yamada | 75f82d0 | 2018-03-05 01:20:11 +0900 | [diff] [blame] | 11 | #include <linux/libfdt.h> |
Simon Glass | c4fc562 | 2017-05-18 20:08:58 -0600 | [diff] [blame] | 12 | #include <dm/of_access.h> |
Simon Glass | 049ae1b | 2017-05-18 20:09:01 -0600 | [diff] [blame] | 13 | #include <dm/of_addr.h> |
Simon Glass | c4fc562 | 2017-05-18 20:08:58 -0600 | [diff] [blame] | 14 | #include <dm/ofnode.h> |
| 15 | #include <linux/err.h> |
Simon Glass | f7bfcc4 | 2017-07-25 08:29:55 -0600 | [diff] [blame] | 16 | #include <linux/ioport.h> |
Simon Glass | c4fc562 | 2017-05-18 20:08:58 -0600 | [diff] [blame] | 17 | |
| 18 | int ofnode_read_u32(ofnode node, const char *propname, u32 *outp) |
| 19 | { |
| 20 | assert(ofnode_valid(node)); |
| 21 | debug("%s: %s: ", __func__, propname); |
| 22 | |
| 23 | if (ofnode_is_np(node)) { |
| 24 | return of_read_u32(ofnode_to_np(node), propname, outp); |
| 25 | } else { |
Masahiro Yamada | 5c5991e | 2017-06-22 17:57:50 +0900 | [diff] [blame] | 26 | const fdt32_t *cell; |
Simon Glass | c4fc562 | 2017-05-18 20:08:58 -0600 | [diff] [blame] | 27 | int len; |
| 28 | |
| 29 | cell = fdt_getprop(gd->fdt_blob, ofnode_to_offset(node), |
| 30 | propname, &len); |
| 31 | if (!cell || len < sizeof(int)) { |
| 32 | debug("(not found)\n"); |
| 33 | return -EINVAL; |
| 34 | } |
| 35 | *outp = fdt32_to_cpu(cell[0]); |
| 36 | } |
| 37 | debug("%#x (%d)\n", *outp, *outp); |
| 38 | |
| 39 | return 0; |
| 40 | } |
| 41 | |
Trent Piepho | 5b77541 | 2019-05-10 17:48:20 +0000 | [diff] [blame] | 42 | u32 ofnode_read_u32_default(ofnode node, const char *propname, u32 def) |
Simon Glass | c4fc562 | 2017-05-18 20:08:58 -0600 | [diff] [blame] | 43 | { |
| 44 | assert(ofnode_valid(node)); |
| 45 | ofnode_read_u32(node, propname, &def); |
| 46 | |
| 47 | return def; |
| 48 | } |
| 49 | |
| 50 | int ofnode_read_s32_default(ofnode node, const char *propname, s32 def) |
| 51 | { |
| 52 | assert(ofnode_valid(node)); |
| 53 | ofnode_read_u32(node, propname, (u32 *)&def); |
| 54 | |
| 55 | return def; |
| 56 | } |
| 57 | |
Simon Glass | 9d54a7a | 2018-06-11 13:07:10 -0600 | [diff] [blame] | 58 | int ofnode_read_u64(ofnode node, const char *propname, u64 *outp) |
| 59 | { |
| 60 | const fdt64_t *cell; |
| 61 | int len; |
| 62 | |
| 63 | assert(ofnode_valid(node)); |
| 64 | debug("%s: %s: ", __func__, propname); |
| 65 | |
| 66 | if (ofnode_is_np(node)) |
| 67 | return of_read_u64(ofnode_to_np(node), propname, outp); |
| 68 | |
| 69 | cell = fdt_getprop(gd->fdt_blob, ofnode_to_offset(node), propname, |
| 70 | &len); |
| 71 | if (!cell || len < sizeof(*cell)) { |
| 72 | debug("(not found)\n"); |
| 73 | return -EINVAL; |
| 74 | } |
| 75 | *outp = fdt64_to_cpu(cell[0]); |
| 76 | debug("%#llx (%lld)\n", (unsigned long long)*outp, |
| 77 | (unsigned long long)*outp); |
| 78 | |
| 79 | return 0; |
| 80 | } |
| 81 | |
| 82 | int ofnode_read_u64_default(ofnode node, const char *propname, u64 def) |
| 83 | { |
| 84 | assert(ofnode_valid(node)); |
| 85 | ofnode_read_u64(node, propname, &def); |
| 86 | |
| 87 | return def; |
| 88 | } |
| 89 | |
Simon Glass | c4fc562 | 2017-05-18 20:08:58 -0600 | [diff] [blame] | 90 | bool ofnode_read_bool(ofnode node, const char *propname) |
| 91 | { |
Masahiro Yamada | 5d43445 | 2017-06-22 16:54:07 +0900 | [diff] [blame] | 92 | const void *prop; |
Simon Glass | c4fc562 | 2017-05-18 20:08:58 -0600 | [diff] [blame] | 93 | |
| 94 | assert(ofnode_valid(node)); |
| 95 | debug("%s: %s: ", __func__, propname); |
| 96 | |
Masahiro Yamada | 5d43445 | 2017-06-22 16:54:07 +0900 | [diff] [blame] | 97 | prop = ofnode_get_property(node, propname, NULL); |
| 98 | |
| 99 | debug("%s\n", prop ? "true" : "false"); |
Simon Glass | c4fc562 | 2017-05-18 20:08:58 -0600 | [diff] [blame] | 100 | |
Masahiro Yamada | 5d43445 | 2017-06-22 16:54:07 +0900 | [diff] [blame] | 101 | return prop ? true : false; |
Simon Glass | c4fc562 | 2017-05-18 20:08:58 -0600 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | const char *ofnode_read_string(ofnode node, const char *propname) |
| 105 | { |
| 106 | const char *str = NULL; |
| 107 | int len = -1; |
| 108 | |
| 109 | assert(ofnode_valid(node)); |
| 110 | debug("%s: %s: ", __func__, propname); |
| 111 | |
| 112 | if (ofnode_is_np(node)) { |
| 113 | struct property *prop = of_find_property( |
| 114 | ofnode_to_np(node), propname, NULL); |
| 115 | |
| 116 | if (prop) { |
| 117 | str = prop->value; |
| 118 | len = prop->length; |
| 119 | } |
| 120 | } else { |
| 121 | str = fdt_getprop(gd->fdt_blob, ofnode_to_offset(node), |
| 122 | propname, &len); |
| 123 | } |
| 124 | if (!str) { |
| 125 | debug("<not found>\n"); |
| 126 | return NULL; |
| 127 | } |
| 128 | if (strnlen(str, len) >= len) { |
| 129 | debug("<invalid>\n"); |
| 130 | return NULL; |
| 131 | } |
| 132 | debug("%s\n", str); |
| 133 | |
| 134 | return str; |
| 135 | } |
| 136 | |
| 137 | ofnode ofnode_find_subnode(ofnode node, const char *subnode_name) |
| 138 | { |
| 139 | ofnode subnode; |
| 140 | |
| 141 | assert(ofnode_valid(node)); |
| 142 | debug("%s: %s: ", __func__, subnode_name); |
| 143 | |
| 144 | if (ofnode_is_np(node)) { |
| 145 | const struct device_node *np = ofnode_to_np(node); |
| 146 | |
| 147 | for (np = np->child; np; np = np->sibling) { |
| 148 | if (!strcmp(subnode_name, np->name)) |
| 149 | break; |
| 150 | } |
| 151 | subnode = np_to_ofnode(np); |
| 152 | } else { |
| 153 | int ooffset = fdt_subnode_offset(gd->fdt_blob, |
| 154 | ofnode_to_offset(node), subnode_name); |
| 155 | subnode = offset_to_ofnode(ooffset); |
| 156 | } |
| 157 | debug("%s\n", ofnode_valid(subnode) ? |
| 158 | ofnode_get_name(subnode) : "<none>"); |
| 159 | |
| 160 | return subnode; |
| 161 | } |
| 162 | |
| 163 | int ofnode_read_u32_array(ofnode node, const char *propname, |
| 164 | u32 *out_values, size_t sz) |
| 165 | { |
| 166 | assert(ofnode_valid(node)); |
| 167 | debug("%s: %s: ", __func__, propname); |
| 168 | |
| 169 | if (ofnode_is_np(node)) { |
| 170 | return of_read_u32_array(ofnode_to_np(node), propname, |
| 171 | out_values, sz); |
| 172 | } else { |
| 173 | return fdtdec_get_int_array(gd->fdt_blob, |
| 174 | ofnode_to_offset(node), propname, |
| 175 | out_values, sz); |
| 176 | } |
| 177 | } |
| 178 | |
| 179 | ofnode ofnode_first_subnode(ofnode node) |
| 180 | { |
| 181 | assert(ofnode_valid(node)); |
| 182 | if (ofnode_is_np(node)) |
| 183 | return np_to_ofnode(node.np->child); |
| 184 | |
| 185 | return offset_to_ofnode( |
| 186 | fdt_first_subnode(gd->fdt_blob, ofnode_to_offset(node))); |
| 187 | } |
| 188 | |
| 189 | ofnode ofnode_next_subnode(ofnode node) |
| 190 | { |
| 191 | assert(ofnode_valid(node)); |
| 192 | if (ofnode_is_np(node)) |
| 193 | return np_to_ofnode(node.np->sibling); |
| 194 | |
| 195 | return offset_to_ofnode( |
| 196 | fdt_next_subnode(gd->fdt_blob, ofnode_to_offset(node))); |
| 197 | } |
| 198 | |
Philipp Tomsich | 6fce1dd | 2018-02-23 17:38:49 +0100 | [diff] [blame] | 199 | ofnode ofnode_get_parent(ofnode node) |
| 200 | { |
| 201 | ofnode parent; |
| 202 | |
| 203 | assert(ofnode_valid(node)); |
| 204 | if (ofnode_is_np(node)) |
| 205 | parent = np_to_ofnode(of_get_parent(ofnode_to_np(node))); |
| 206 | else |
| 207 | parent.of_offset = fdt_parent_offset(gd->fdt_blob, |
| 208 | ofnode_to_offset(node)); |
| 209 | |
| 210 | return parent; |
| 211 | } |
| 212 | |
Simon Glass | c4fc562 | 2017-05-18 20:08:58 -0600 | [diff] [blame] | 213 | const char *ofnode_get_name(ofnode node) |
| 214 | { |
| 215 | assert(ofnode_valid(node)); |
| 216 | if (ofnode_is_np(node)) |
| 217 | return strrchr(node.np->full_name, '/') + 1; |
| 218 | |
| 219 | return fdt_get_name(gd->fdt_blob, ofnode_to_offset(node), NULL); |
| 220 | } |
| 221 | |
Kever Yang | 37df0e0 | 2018-02-23 17:38:50 +0100 | [diff] [blame] | 222 | ofnode ofnode_get_by_phandle(uint phandle) |
| 223 | { |
| 224 | ofnode node; |
| 225 | |
| 226 | if (of_live_active()) |
| 227 | node = np_to_ofnode(of_find_node_by_phandle(phandle)); |
| 228 | else |
| 229 | node.of_offset = fdt_node_offset_by_phandle(gd->fdt_blob, |
| 230 | phandle); |
| 231 | |
| 232 | return node; |
| 233 | } |
| 234 | |
Simon Glass | c4fc562 | 2017-05-18 20:08:58 -0600 | [diff] [blame] | 235 | int ofnode_read_size(ofnode node, const char *propname) |
| 236 | { |
| 237 | int len; |
| 238 | |
| 239 | if (ofnode_is_np(node)) { |
| 240 | struct property *prop = of_find_property( |
| 241 | ofnode_to_np(node), propname, NULL); |
| 242 | |
| 243 | if (prop) |
| 244 | return prop->length; |
| 245 | } else { |
| 246 | if (fdt_getprop(gd->fdt_blob, ofnode_to_offset(node), propname, |
| 247 | &len)) |
| 248 | return len; |
| 249 | } |
| 250 | |
| 251 | return -EINVAL; |
| 252 | } |
| 253 | |
Keerthy | d332e6e | 2019-04-24 17:19:53 +0530 | [diff] [blame] | 254 | fdt_addr_t ofnode_get_addr_size_index(ofnode node, int index, fdt_size_t *size) |
Simon Glass | 049ae1b | 2017-05-18 20:09:01 -0600 | [diff] [blame] | 255 | { |
Keerthy | 34222a3 | 2018-11-19 11:44:47 +0530 | [diff] [blame] | 256 | int na, ns; |
Keerthy | 34222a3 | 2018-11-19 11:44:47 +0530 | [diff] [blame] | 257 | |
Simon Glass | 049ae1b | 2017-05-18 20:09:01 -0600 | [diff] [blame] | 258 | if (ofnode_is_np(node)) { |
| 259 | const __be32 *prop_val; |
| 260 | uint flags; |
Simon Glass | 049ae1b | 2017-05-18 20:09:01 -0600 | [diff] [blame] | 261 | |
Keerthy | 34222a3 | 2018-11-19 11:44:47 +0530 | [diff] [blame] | 262 | prop_val = of_get_address(ofnode_to_np(node), index, |
Keerthy | d332e6e | 2019-04-24 17:19:53 +0530 | [diff] [blame] | 263 | (u64 *)size, &flags); |
Simon Glass | 049ae1b | 2017-05-18 20:09:01 -0600 | [diff] [blame] | 264 | if (!prop_val) |
| 265 | return FDT_ADDR_T_NONE; |
Mario Six | d007ebc | 2017-12-20 09:52:12 +0100 | [diff] [blame] | 266 | |
Mario Six | 35616ef | 2018-03-12 14:53:33 +0100 | [diff] [blame] | 267 | ns = of_n_size_cells(ofnode_to_np(node)); |
| 268 | |
| 269 | if (IS_ENABLED(CONFIG_OF_TRANSLATE) && ns > 0) { |
Mario Six | d007ebc | 2017-12-20 09:52:12 +0100 | [diff] [blame] | 270 | return of_translate_address(ofnode_to_np(node), prop_val); |
| 271 | } else { |
| 272 | na = of_n_addr_cells(ofnode_to_np(node)); |
| 273 | return of_read_number(prop_val, na); |
| 274 | } |
Simon Glass | 049ae1b | 2017-05-18 20:09:01 -0600 | [diff] [blame] | 275 | } else { |
Keerthy | 34222a3 | 2018-11-19 11:44:47 +0530 | [diff] [blame] | 276 | na = ofnode_read_simple_addr_cells(ofnode_get_parent(node)); |
| 277 | ns = ofnode_read_simple_size_cells(ofnode_get_parent(node)); |
| 278 | return fdtdec_get_addr_size_fixed(gd->fdt_blob, |
| 279 | ofnode_to_offset(node), "reg", |
Keerthy | d332e6e | 2019-04-24 17:19:53 +0530 | [diff] [blame] | 280 | index, na, ns, size, true); |
Simon Glass | 049ae1b | 2017-05-18 20:09:01 -0600 | [diff] [blame] | 281 | } |
| 282 | |
| 283 | return FDT_ADDR_T_NONE; |
| 284 | } |
| 285 | |
Keerthy | d332e6e | 2019-04-24 17:19:53 +0530 | [diff] [blame] | 286 | fdt_addr_t ofnode_get_addr_index(ofnode node, int index) |
| 287 | { |
| 288 | fdt_size_t size; |
| 289 | |
| 290 | return ofnode_get_addr_size_index(node, index, &size); |
| 291 | } |
| 292 | |
Simon Glass | 049ae1b | 2017-05-18 20:09:01 -0600 | [diff] [blame] | 293 | fdt_addr_t ofnode_get_addr(ofnode node) |
| 294 | { |
| 295 | return ofnode_get_addr_index(node, 0); |
| 296 | } |
| 297 | |
Simon Glass | c4fc562 | 2017-05-18 20:08:58 -0600 | [diff] [blame] | 298 | int ofnode_stringlist_search(ofnode node, const char *property, |
| 299 | const char *string) |
| 300 | { |
| 301 | if (ofnode_is_np(node)) { |
| 302 | return of_property_match_string(ofnode_to_np(node), |
| 303 | property, string); |
| 304 | } else { |
| 305 | int ret; |
| 306 | |
| 307 | ret = fdt_stringlist_search(gd->fdt_blob, |
| 308 | ofnode_to_offset(node), property, |
| 309 | string); |
| 310 | if (ret == -FDT_ERR_NOTFOUND) |
| 311 | return -ENODATA; |
| 312 | else if (ret < 0) |
| 313 | return -EINVAL; |
| 314 | |
| 315 | return ret; |
| 316 | } |
| 317 | } |
| 318 | |
| 319 | int ofnode_read_string_index(ofnode node, const char *property, int index, |
| 320 | const char **outp) |
| 321 | { |
| 322 | if (ofnode_is_np(node)) { |
| 323 | return of_property_read_string_index(ofnode_to_np(node), |
| 324 | property, index, outp); |
| 325 | } else { |
| 326 | int len; |
| 327 | |
| 328 | *outp = fdt_stringlist_get(gd->fdt_blob, ofnode_to_offset(node), |
| 329 | property, index, &len); |
| 330 | if (len < 0) |
| 331 | return -EINVAL; |
| 332 | return 0; |
| 333 | } |
| 334 | } |
| 335 | |
Simon Glass | 5fdb005 | 2017-06-12 06:21:28 -0600 | [diff] [blame] | 336 | int ofnode_read_string_count(ofnode node, const char *property) |
| 337 | { |
| 338 | if (ofnode_is_np(node)) { |
| 339 | return of_property_count_strings(ofnode_to_np(node), property); |
| 340 | } else { |
| 341 | return fdt_stringlist_count(gd->fdt_blob, |
| 342 | ofnode_to_offset(node), property); |
| 343 | } |
| 344 | } |
| 345 | |
Simon Glass | c4fc562 | 2017-05-18 20:08:58 -0600 | [diff] [blame] | 346 | static void ofnode_from_fdtdec_phandle_args(struct fdtdec_phandle_args *in, |
| 347 | struct ofnode_phandle_args *out) |
| 348 | { |
| 349 | assert(OF_MAX_PHANDLE_ARGS == MAX_PHANDLE_ARGS); |
| 350 | out->node = offset_to_ofnode(in->node); |
| 351 | out->args_count = in->args_count; |
| 352 | memcpy(out->args, in->args, sizeof(out->args)); |
| 353 | } |
| 354 | |
| 355 | static void ofnode_from_of_phandle_args(struct of_phandle_args *in, |
| 356 | struct ofnode_phandle_args *out) |
| 357 | { |
| 358 | assert(OF_MAX_PHANDLE_ARGS == MAX_PHANDLE_ARGS); |
| 359 | out->node = np_to_ofnode(in->np); |
| 360 | out->args_count = in->args_count; |
| 361 | memcpy(out->args, in->args, sizeof(out->args)); |
| 362 | } |
| 363 | |
| 364 | int ofnode_parse_phandle_with_args(ofnode node, const char *list_name, |
| 365 | const char *cells_name, int cell_count, |
| 366 | int index, |
| 367 | struct ofnode_phandle_args *out_args) |
| 368 | { |
| 369 | if (ofnode_is_np(node)) { |
| 370 | struct of_phandle_args args; |
| 371 | int ret; |
| 372 | |
| 373 | ret = of_parse_phandle_with_args(ofnode_to_np(node), |
Mario Six | f40d82c | 2018-01-15 11:07:17 +0100 | [diff] [blame] | 374 | list_name, cells_name, index, |
| 375 | &args); |
Simon Glass | c4fc562 | 2017-05-18 20:08:58 -0600 | [diff] [blame] | 376 | if (ret) |
| 377 | return ret; |
| 378 | ofnode_from_of_phandle_args(&args, out_args); |
| 379 | } else { |
| 380 | struct fdtdec_phandle_args args; |
| 381 | int ret; |
| 382 | |
| 383 | ret = fdtdec_parse_phandle_with_args(gd->fdt_blob, |
Mario Six | f40d82c | 2018-01-15 11:07:17 +0100 | [diff] [blame] | 384 | ofnode_to_offset(node), |
| 385 | list_name, cells_name, |
| 386 | cell_count, index, &args); |
Simon Glass | c4fc562 | 2017-05-18 20:08:58 -0600 | [diff] [blame] | 387 | if (ret) |
| 388 | return ret; |
| 389 | ofnode_from_fdtdec_phandle_args(&args, out_args); |
| 390 | } |
| 391 | |
| 392 | return 0; |
| 393 | } |
| 394 | |
Patrice Chotard | be7dd60 | 2017-07-18 11:57:08 +0200 | [diff] [blame] | 395 | int ofnode_count_phandle_with_args(ofnode node, const char *list_name, |
| 396 | const char *cells_name) |
| 397 | { |
| 398 | if (ofnode_is_np(node)) |
| 399 | return of_count_phandle_with_args(ofnode_to_np(node), |
| 400 | list_name, cells_name); |
| 401 | else |
| 402 | return fdtdec_parse_phandle_with_args(gd->fdt_blob, |
| 403 | ofnode_to_offset(node), list_name, cells_name, |
| 404 | 0, -1, NULL); |
| 405 | } |
| 406 | |
Simon Glass | c4fc562 | 2017-05-18 20:08:58 -0600 | [diff] [blame] | 407 | ofnode ofnode_path(const char *path) |
| 408 | { |
| 409 | if (of_live_active()) |
| 410 | return np_to_ofnode(of_find_node_by_path(path)); |
| 411 | else |
| 412 | return offset_to_ofnode(fdt_path_offset(gd->fdt_blob, path)); |
| 413 | } |
| 414 | |
| 415 | const char *ofnode_get_chosen_prop(const char *name) |
| 416 | { |
| 417 | ofnode chosen_node; |
| 418 | |
| 419 | chosen_node = ofnode_path("/chosen"); |
| 420 | |
| 421 | return ofnode_read_string(chosen_node, name); |
| 422 | } |
| 423 | |
| 424 | ofnode ofnode_get_chosen_node(const char *name) |
| 425 | { |
| 426 | const char *prop; |
| 427 | |
| 428 | prop = ofnode_get_chosen_prop(name); |
| 429 | if (!prop) |
| 430 | return ofnode_null(); |
| 431 | |
| 432 | return ofnode_path(prop); |
| 433 | } |
| 434 | |
| 435 | static int decode_timing_property(ofnode node, const char *name, |
| 436 | struct timing_entry *result) |
| 437 | { |
| 438 | int length, ret = 0; |
| 439 | |
| 440 | length = ofnode_read_size(node, name); |
| 441 | if (length < 0) { |
| 442 | debug("%s: could not find property %s\n", |
| 443 | ofnode_get_name(node), name); |
| 444 | return length; |
| 445 | } |
| 446 | |
| 447 | if (length == sizeof(u32)) { |
| 448 | result->typ = ofnode_read_u32_default(node, name, 0); |
| 449 | result->min = result->typ; |
| 450 | result->max = result->typ; |
| 451 | } else { |
| 452 | ret = ofnode_read_u32_array(node, name, &result->min, 3); |
| 453 | } |
| 454 | |
| 455 | return ret; |
| 456 | } |
| 457 | |
| 458 | int ofnode_decode_display_timing(ofnode parent, int index, |
| 459 | struct display_timing *dt) |
| 460 | { |
| 461 | int i; |
| 462 | ofnode timings, node; |
| 463 | u32 val = 0; |
| 464 | int ret = 0; |
| 465 | |
| 466 | timings = ofnode_find_subnode(parent, "display-timings"); |
| 467 | if (!ofnode_valid(timings)) |
| 468 | return -EINVAL; |
| 469 | |
Simon Glass | 2852976 | 2017-08-05 15:45:54 -0600 | [diff] [blame] | 470 | i = 0; |
| 471 | ofnode_for_each_subnode(node, timings) { |
| 472 | if (i++ == index) |
| 473 | break; |
| 474 | } |
Simon Glass | c4fc562 | 2017-05-18 20:08:58 -0600 | [diff] [blame] | 475 | |
| 476 | if (!ofnode_valid(node)) |
| 477 | return -EINVAL; |
| 478 | |
| 479 | memset(dt, 0, sizeof(*dt)); |
| 480 | |
| 481 | ret |= decode_timing_property(node, "hback-porch", &dt->hback_porch); |
| 482 | ret |= decode_timing_property(node, "hfront-porch", &dt->hfront_porch); |
| 483 | ret |= decode_timing_property(node, "hactive", &dt->hactive); |
| 484 | ret |= decode_timing_property(node, "hsync-len", &dt->hsync_len); |
| 485 | ret |= decode_timing_property(node, "vback-porch", &dt->vback_porch); |
| 486 | ret |= decode_timing_property(node, "vfront-porch", &dt->vfront_porch); |
| 487 | ret |= decode_timing_property(node, "vactive", &dt->vactive); |
| 488 | ret |= decode_timing_property(node, "vsync-len", &dt->vsync_len); |
| 489 | ret |= decode_timing_property(node, "clock-frequency", &dt->pixelclock); |
| 490 | |
| 491 | dt->flags = 0; |
| 492 | val = ofnode_read_u32_default(node, "vsync-active", -1); |
| 493 | if (val != -1) { |
| 494 | dt->flags |= val ? DISPLAY_FLAGS_VSYNC_HIGH : |
| 495 | DISPLAY_FLAGS_VSYNC_LOW; |
| 496 | } |
| 497 | val = ofnode_read_u32_default(node, "hsync-active", -1); |
| 498 | if (val != -1) { |
| 499 | dt->flags |= val ? DISPLAY_FLAGS_HSYNC_HIGH : |
| 500 | DISPLAY_FLAGS_HSYNC_LOW; |
| 501 | } |
| 502 | val = ofnode_read_u32_default(node, "de-active", -1); |
| 503 | if (val != -1) { |
| 504 | dt->flags |= val ? DISPLAY_FLAGS_DE_HIGH : |
| 505 | DISPLAY_FLAGS_DE_LOW; |
| 506 | } |
| 507 | val = ofnode_read_u32_default(node, "pixelclk-active", -1); |
| 508 | if (val != -1) { |
| 509 | dt->flags |= val ? DISPLAY_FLAGS_PIXDATA_POSEDGE : |
| 510 | DISPLAY_FLAGS_PIXDATA_NEGEDGE; |
| 511 | } |
| 512 | |
| 513 | if (ofnode_read_bool(node, "interlaced")) |
| 514 | dt->flags |= DISPLAY_FLAGS_INTERLACED; |
| 515 | if (ofnode_read_bool(node, "doublescan")) |
| 516 | dt->flags |= DISPLAY_FLAGS_DOUBLESCAN; |
| 517 | if (ofnode_read_bool(node, "doubleclk")) |
| 518 | dt->flags |= DISPLAY_FLAGS_DOUBLECLK; |
| 519 | |
| 520 | return ret; |
| 521 | } |
| 522 | |
Masahiro Yamada | 9cf85cb | 2017-06-22 16:54:05 +0900 | [diff] [blame] | 523 | const void *ofnode_get_property(ofnode node, const char *propname, int *lenp) |
Simon Glass | c4fc562 | 2017-05-18 20:08:58 -0600 | [diff] [blame] | 524 | { |
Masahiro Yamada | 5052f1b | 2017-06-22 16:54:04 +0900 | [diff] [blame] | 525 | if (ofnode_is_np(node)) |
| 526 | return of_get_property(ofnode_to_np(node), propname, lenp); |
| 527 | else |
Simon Glass | c4fc562 | 2017-05-18 20:08:58 -0600 | [diff] [blame] | 528 | return fdt_getprop(gd->fdt_blob, ofnode_to_offset(node), |
| 529 | propname, lenp); |
Simon Glass | c4fc562 | 2017-05-18 20:08:58 -0600 | [diff] [blame] | 530 | } |
| 531 | |
| 532 | bool ofnode_is_available(ofnode node) |
| 533 | { |
| 534 | if (ofnode_is_np(node)) |
| 535 | return of_device_is_available(ofnode_to_np(node)); |
| 536 | else |
| 537 | return fdtdec_get_is_enabled(gd->fdt_blob, |
| 538 | ofnode_to_offset(node)); |
| 539 | } |
| 540 | |
| 541 | fdt_addr_t ofnode_get_addr_size(ofnode node, const char *property, |
| 542 | fdt_size_t *sizep) |
| 543 | { |
| 544 | if (ofnode_is_np(node)) { |
| 545 | int na, ns; |
| 546 | int psize; |
| 547 | const struct device_node *np = ofnode_to_np(node); |
Klaus Goger | af4b021 | 2017-09-20 13:50:41 +0200 | [diff] [blame] | 548 | const __be32 *prop = of_get_property(np, property, &psize); |
Simon Glass | c4fc562 | 2017-05-18 20:08:58 -0600 | [diff] [blame] | 549 | |
Klaus Goger | af4b021 | 2017-09-20 13:50:41 +0200 | [diff] [blame] | 550 | if (!prop) |
| 551 | return FDT_ADDR_T_NONE; |
Simon Glass | c4fc562 | 2017-05-18 20:08:58 -0600 | [diff] [blame] | 552 | na = of_n_addr_cells(np); |
Marek Vasut | 1638c17 | 2018-10-01 12:37:19 +0200 | [diff] [blame] | 553 | ns = of_n_size_cells(np); |
Simon Glass | a67cc63 | 2017-05-18 20:09:27 -0600 | [diff] [blame] | 554 | *sizep = of_read_number(prop + na, ns); |
Marek Vasut | a9dac49 | 2018-10-01 12:37:20 +0200 | [diff] [blame] | 555 | |
Simon Glass | 019f956 | 2019-04-25 21:58:36 -0600 | [diff] [blame] | 556 | if (CONFIG_IS_ENABLED(OF_TRANSLATE) && ns > 0) |
Marek Vasut | a9dac49 | 2018-10-01 12:37:20 +0200 | [diff] [blame] | 557 | return of_translate_address(np, prop); |
| 558 | else |
| 559 | return of_read_number(prop, na); |
Simon Glass | c4fc562 | 2017-05-18 20:08:58 -0600 | [diff] [blame] | 560 | } else { |
| 561 | return fdtdec_get_addr_size(gd->fdt_blob, |
| 562 | ofnode_to_offset(node), property, |
| 563 | sizep); |
| 564 | } |
| 565 | } |
| 566 | |
| 567 | const uint8_t *ofnode_read_u8_array_ptr(ofnode node, const char *propname, |
| 568 | size_t sz) |
| 569 | { |
| 570 | if (ofnode_is_np(node)) { |
| 571 | const struct device_node *np = ofnode_to_np(node); |
| 572 | int psize; |
| 573 | const __be32 *prop = of_get_property(np, propname, &psize); |
| 574 | |
| 575 | if (!prop || sz != psize) |
| 576 | return NULL; |
| 577 | return (uint8_t *)prop; |
| 578 | |
| 579 | } else { |
| 580 | return fdtdec_locate_byte_array(gd->fdt_blob, |
| 581 | ofnode_to_offset(node), propname, sz); |
| 582 | } |
| 583 | } |
| 584 | |
| 585 | int ofnode_read_pci_addr(ofnode node, enum fdt_pci_space type, |
| 586 | const char *propname, struct fdt_pci_addr *addr) |
| 587 | { |
Masahiro Yamada | 5c5991e | 2017-06-22 17:57:50 +0900 | [diff] [blame] | 588 | const fdt32_t *cell; |
Simon Glass | c4fc562 | 2017-05-18 20:08:58 -0600 | [diff] [blame] | 589 | int len; |
| 590 | int ret = -ENOENT; |
| 591 | |
| 592 | debug("%s: %s: ", __func__, propname); |
| 593 | |
| 594 | /* |
| 595 | * If we follow the pci bus bindings strictly, we should check |
| 596 | * the value of the node's parent node's #address-cells and |
| 597 | * #size-cells. They need to be 3 and 2 accordingly. However, |
| 598 | * for simplicity we skip the check here. |
| 599 | */ |
Masahiro Yamada | 9cf85cb | 2017-06-22 16:54:05 +0900 | [diff] [blame] | 600 | cell = ofnode_get_property(node, propname, &len); |
Simon Glass | c4fc562 | 2017-05-18 20:08:58 -0600 | [diff] [blame] | 601 | if (!cell) |
| 602 | goto fail; |
| 603 | |
| 604 | if ((len % FDT_PCI_REG_SIZE) == 0) { |
| 605 | int num = len / FDT_PCI_REG_SIZE; |
| 606 | int i; |
| 607 | |
| 608 | for (i = 0; i < num; i++) { |
| 609 | debug("pci address #%d: %08lx %08lx %08lx\n", i, |
| 610 | (ulong)fdt32_to_cpu(cell[0]), |
| 611 | (ulong)fdt32_to_cpu(cell[1]), |
| 612 | (ulong)fdt32_to_cpu(cell[2])); |
| 613 | if ((fdt32_to_cpu(*cell) & type) == type) { |
| 614 | addr->phys_hi = fdt32_to_cpu(cell[0]); |
| 615 | addr->phys_mid = fdt32_to_cpu(cell[1]); |
| 616 | addr->phys_lo = fdt32_to_cpu(cell[1]); |
| 617 | break; |
Simon Glass | c4fc562 | 2017-05-18 20:08:58 -0600 | [diff] [blame] | 618 | } |
Mario Six | f40d82c | 2018-01-15 11:07:17 +0100 | [diff] [blame] | 619 | |
| 620 | cell += (FDT_PCI_ADDR_CELLS + |
| 621 | FDT_PCI_SIZE_CELLS); |
Simon Glass | c4fc562 | 2017-05-18 20:08:58 -0600 | [diff] [blame] | 622 | } |
| 623 | |
| 624 | if (i == num) { |
| 625 | ret = -ENXIO; |
| 626 | goto fail; |
| 627 | } |
| 628 | |
| 629 | return 0; |
Simon Glass | c4fc562 | 2017-05-18 20:08:58 -0600 | [diff] [blame] | 630 | } |
| 631 | |
Mario Six | f40d82c | 2018-01-15 11:07:17 +0100 | [diff] [blame] | 632 | ret = -EINVAL; |
| 633 | |
Simon Glass | c4fc562 | 2017-05-18 20:08:58 -0600 | [diff] [blame] | 634 | fail: |
| 635 | debug("(not found)\n"); |
| 636 | return ret; |
| 637 | } |
| 638 | |
Bin Meng | fa15771 | 2018-08-03 01:14:35 -0700 | [diff] [blame] | 639 | int ofnode_read_pci_vendev(ofnode node, u16 *vendor, u16 *device) |
| 640 | { |
| 641 | const char *list, *end; |
| 642 | int len; |
| 643 | |
| 644 | list = ofnode_get_property(node, "compatible", &len); |
| 645 | if (!list) |
| 646 | return -ENOENT; |
| 647 | |
| 648 | end = list + len; |
| 649 | while (list < end) { |
| 650 | len = strlen(list); |
| 651 | if (len >= strlen("pciVVVV,DDDD")) { |
| 652 | char *s = strstr(list, "pci"); |
| 653 | |
| 654 | /* |
| 655 | * check if the string is something like pciVVVV,DDDD.RR |
| 656 | * or just pciVVVV,DDDD |
| 657 | */ |
| 658 | if (s && s[7] == ',' && |
| 659 | (s[12] == '.' || s[12] == 0)) { |
| 660 | s += 3; |
| 661 | *vendor = simple_strtol(s, NULL, 16); |
| 662 | |
| 663 | s += 5; |
| 664 | *device = simple_strtol(s, NULL, 16); |
| 665 | |
| 666 | return 0; |
| 667 | } |
| 668 | } |
| 669 | list += (len + 1); |
| 670 | } |
| 671 | |
| 672 | return -ENOENT; |
| 673 | } |
| 674 | |
Simon Glass | c4fc562 | 2017-05-18 20:08:58 -0600 | [diff] [blame] | 675 | int ofnode_read_addr_cells(ofnode node) |
| 676 | { |
| 677 | if (ofnode_is_np(node)) |
| 678 | return of_n_addr_cells(ofnode_to_np(node)); |
Simon Glass | 4191dc1 | 2017-06-12 06:21:31 -0600 | [diff] [blame] | 679 | else /* NOTE: this call should walk up the parent stack */ |
Simon Glass | c4fc562 | 2017-05-18 20:08:58 -0600 | [diff] [blame] | 680 | return fdt_address_cells(gd->fdt_blob, ofnode_to_offset(node)); |
| 681 | } |
| 682 | |
| 683 | int ofnode_read_size_cells(ofnode node) |
| 684 | { |
| 685 | if (ofnode_is_np(node)) |
| 686 | return of_n_size_cells(ofnode_to_np(node)); |
Simon Glass | 4191dc1 | 2017-06-12 06:21:31 -0600 | [diff] [blame] | 687 | else /* NOTE: this call should walk up the parent stack */ |
| 688 | return fdt_size_cells(gd->fdt_blob, ofnode_to_offset(node)); |
| 689 | } |
| 690 | |
| 691 | int ofnode_read_simple_addr_cells(ofnode node) |
| 692 | { |
| 693 | if (ofnode_is_np(node)) |
| 694 | return of_simple_addr_cells(ofnode_to_np(node)); |
| 695 | else |
| 696 | return fdt_address_cells(gd->fdt_blob, ofnode_to_offset(node)); |
| 697 | } |
| 698 | |
| 699 | int ofnode_read_simple_size_cells(ofnode node) |
| 700 | { |
| 701 | if (ofnode_is_np(node)) |
| 702 | return of_simple_size_cells(ofnode_to_np(node)); |
Simon Glass | c4fc562 | 2017-05-18 20:08:58 -0600 | [diff] [blame] | 703 | else |
| 704 | return fdt_size_cells(gd->fdt_blob, ofnode_to_offset(node)); |
| 705 | } |
| 706 | |
| 707 | bool ofnode_pre_reloc(ofnode node) |
| 708 | { |
Patrick Delaunay | 0b025b8 | 2019-02-11 12:49:57 +0100 | [diff] [blame] | 709 | #if defined(CONFIG_SPL_BUILD) || defined(CONFIG_TPL_BUILD) |
| 710 | /* for SPL and TPL the remaining nodes after the fdtgrep 1st pass |
| 711 | * had property dm-pre-reloc or u-boot,dm-spl/tpl. |
| 712 | * They are removed in final dtb (fdtgrep 2nd pass) |
| 713 | */ |
| 714 | return true; |
| 715 | #else |
Masahiro Yamada | 6a61dd9 | 2017-06-22 16:54:03 +0900 | [diff] [blame] | 716 | if (ofnode_read_bool(node, "u-boot,dm-pre-reloc")) |
Simon Glass | c4fc562 | 2017-05-18 20:08:58 -0600 | [diff] [blame] | 717 | return true; |
Simon Glass | 23f2284 | 2018-10-01 12:22:18 -0600 | [diff] [blame] | 718 | if (ofnode_read_bool(node, "u-boot,dm-pre-proper")) |
| 719 | return true; |
Simon Glass | c4fc562 | 2017-05-18 20:08:58 -0600 | [diff] [blame] | 720 | |
Simon Glass | c4fc562 | 2017-05-18 20:08:58 -0600 | [diff] [blame] | 721 | /* |
| 722 | * In regular builds individual spl and tpl handling both |
| 723 | * count as handled pre-relocation for later second init. |
| 724 | */ |
Masahiro Yamada | 6a61dd9 | 2017-06-22 16:54:03 +0900 | [diff] [blame] | 725 | if (ofnode_read_bool(node, "u-boot,dm-spl") || |
| 726 | ofnode_read_bool(node, "u-boot,dm-tpl")) |
Simon Glass | c4fc562 | 2017-05-18 20:08:58 -0600 | [diff] [blame] | 727 | return true; |
Simon Glass | c4fc562 | 2017-05-18 20:08:58 -0600 | [diff] [blame] | 728 | |
| 729 | return false; |
Patrick Delaunay | 0b025b8 | 2019-02-11 12:49:57 +0100 | [diff] [blame] | 730 | #endif |
Simon Glass | c4fc562 | 2017-05-18 20:08:58 -0600 | [diff] [blame] | 731 | } |
Simon Glass | f7bfcc4 | 2017-07-25 08:29:55 -0600 | [diff] [blame] | 732 | |
| 733 | int ofnode_read_resource(ofnode node, uint index, struct resource *res) |
| 734 | { |
| 735 | if (ofnode_is_np(node)) { |
| 736 | return of_address_to_resource(ofnode_to_np(node), index, res); |
| 737 | } else { |
| 738 | struct fdt_resource fres; |
| 739 | int ret; |
| 740 | |
| 741 | ret = fdt_get_resource(gd->fdt_blob, ofnode_to_offset(node), |
| 742 | "reg", index, &fres); |
| 743 | if (ret < 0) |
| 744 | return -EINVAL; |
| 745 | memset(res, '\0', sizeof(*res)); |
| 746 | res->start = fres.start; |
| 747 | res->end = fres.end; |
| 748 | |
| 749 | return 0; |
| 750 | } |
| 751 | } |
Masahiro Yamada | 4dada2c | 2017-08-26 01:12:30 +0900 | [diff] [blame] | 752 | |
| 753 | int ofnode_read_resource_byname(ofnode node, const char *name, |
| 754 | struct resource *res) |
| 755 | { |
| 756 | int index; |
| 757 | |
| 758 | index = ofnode_stringlist_search(node, "reg-names", name); |
| 759 | if (index < 0) |
| 760 | return index; |
| 761 | |
| 762 | return ofnode_read_resource(node, index, res); |
| 763 | } |
Mario Six | aefac06 | 2018-01-15 11:07:19 +0100 | [diff] [blame] | 764 | |
| 765 | u64 ofnode_translate_address(ofnode node, const fdt32_t *in_addr) |
| 766 | { |
| 767 | if (ofnode_is_np(node)) |
| 768 | return of_translate_address(ofnode_to_np(node), in_addr); |
| 769 | else |
| 770 | return fdt_translate_address(gd->fdt_blob, ofnode_to_offset(node), in_addr); |
| 771 | } |
Masahiro Yamada | 9349bcc | 2018-04-19 12:14:02 +0900 | [diff] [blame] | 772 | |
| 773 | int ofnode_device_is_compatible(ofnode node, const char *compat) |
| 774 | { |
| 775 | if (ofnode_is_np(node)) |
| 776 | return of_device_is_compatible(ofnode_to_np(node), compat, |
| 777 | NULL, NULL); |
| 778 | else |
| 779 | return !fdt_node_check_compatible(gd->fdt_blob, |
| 780 | ofnode_to_offset(node), |
| 781 | compat); |
| 782 | } |
Simon Glass | 954eeae | 2018-06-11 13:07:13 -0600 | [diff] [blame] | 783 | |
| 784 | ofnode ofnode_by_compatible(ofnode from, const char *compat) |
| 785 | { |
| 786 | if (of_live_active()) { |
| 787 | return np_to_ofnode(of_find_compatible_node( |
| 788 | (struct device_node *)ofnode_to_np(from), NULL, |
| 789 | compat)); |
| 790 | } else { |
| 791 | return offset_to_ofnode(fdt_node_offset_by_compatible( |
| 792 | gd->fdt_blob, ofnode_to_offset(from), compat)); |
| 793 | } |
| 794 | } |
Jens Wiklander | 7b68dad | 2018-08-20 11:09:58 +0200 | [diff] [blame] | 795 | |
| 796 | ofnode ofnode_by_prop_value(ofnode from, const char *propname, |
| 797 | const void *propval, int proplen) |
| 798 | { |
| 799 | if (of_live_active()) { |
| 800 | return np_to_ofnode(of_find_node_by_prop_value( |
| 801 | (struct device_node *)ofnode_to_np(from), propname, |
| 802 | propval, proplen)); |
| 803 | } else { |
| 804 | return offset_to_ofnode(fdt_node_offset_by_prop_value( |
| 805 | gd->fdt_blob, ofnode_to_offset(from), |
| 806 | propname, propval, proplen)); |
| 807 | } |
| 808 | } |
Mario Six | 047dafc | 2018-06-26 08:46:48 +0200 | [diff] [blame] | 809 | |
| 810 | int ofnode_write_prop(ofnode node, const char *propname, int len, |
| 811 | const void *value) |
| 812 | { |
| 813 | const struct device_node *np = ofnode_to_np(node); |
| 814 | struct property *pp; |
| 815 | struct property *pp_last = NULL; |
| 816 | struct property *new; |
| 817 | |
| 818 | if (!of_live_active()) |
| 819 | return -ENOSYS; |
| 820 | |
| 821 | if (!np) |
| 822 | return -EINVAL; |
| 823 | |
| 824 | for (pp = np->properties; pp; pp = pp->next) { |
| 825 | if (strcmp(pp->name, propname) == 0) { |
| 826 | /* Property exists -> change value */ |
| 827 | pp->value = (void *)value; |
| 828 | pp->length = len; |
| 829 | return 0; |
| 830 | } |
| 831 | pp_last = pp; |
| 832 | } |
| 833 | |
| 834 | if (!pp_last) |
| 835 | return -ENOENT; |
| 836 | |
| 837 | /* Property does not exist -> append new property */ |
| 838 | new = malloc(sizeof(struct property)); |
| 839 | if (!new) |
| 840 | return -ENOMEM; |
| 841 | |
| 842 | new->name = strdup(propname); |
Mario Six | 95e885d | 2018-10-04 09:22:24 +0200 | [diff] [blame] | 843 | if (!new->name) { |
| 844 | free(new); |
Mario Six | 047dafc | 2018-06-26 08:46:48 +0200 | [diff] [blame] | 845 | return -ENOMEM; |
Mario Six | 95e885d | 2018-10-04 09:22:24 +0200 | [diff] [blame] | 846 | } |
Mario Six | 047dafc | 2018-06-26 08:46:48 +0200 | [diff] [blame] | 847 | |
| 848 | new->value = (void *)value; |
| 849 | new->length = len; |
| 850 | new->next = NULL; |
| 851 | |
| 852 | pp_last->next = new; |
| 853 | |
| 854 | return 0; |
| 855 | } |
| 856 | |
| 857 | int ofnode_write_string(ofnode node, const char *propname, const char *value) |
| 858 | { |
| 859 | if (!of_live_active()) |
| 860 | return -ENOSYS; |
| 861 | |
| 862 | assert(ofnode_valid(node)); |
| 863 | |
| 864 | debug("%s: %s = %s", __func__, propname, value); |
| 865 | |
| 866 | return ofnode_write_prop(node, propname, strlen(value) + 1, value); |
| 867 | } |
| 868 | |
| 869 | int ofnode_set_enabled(ofnode node, bool value) |
| 870 | { |
| 871 | if (!of_live_active()) |
| 872 | return -ENOSYS; |
| 873 | |
| 874 | assert(ofnode_valid(node)); |
| 875 | |
| 876 | if (value) |
| 877 | return ofnode_write_string(node, "status", "okay"); |
| 878 | else |
| 879 | return ofnode_write_string(node, "status", "disable"); |
| 880 | } |