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 | |
| 42 | int ofnode_read_u32_default(ofnode node, const char *propname, u32 def) |
| 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 | |
Simon Glass | 049ae1b | 2017-05-18 20:09:01 -0600 | [diff] [blame] | 254 | fdt_addr_t ofnode_get_addr_index(ofnode node, int index) |
| 255 | { |
| 256 | if (ofnode_is_np(node)) { |
| 257 | const __be32 *prop_val; |
| 258 | uint flags; |
| 259 | u64 size; |
Simon Glass | 76a6666 | 2017-07-25 08:29:56 -0600 | [diff] [blame] | 260 | int na; |
Mario Six | 35616ef | 2018-03-12 14:53:33 +0100 | [diff] [blame] | 261 | int ns; |
Simon Glass | 049ae1b | 2017-05-18 20:09:01 -0600 | [diff] [blame] | 262 | |
Simon Glass | 76a6666 | 2017-07-25 08:29:56 -0600 | [diff] [blame] | 263 | prop_val = of_get_address(ofnode_to_np(node), index, &size, |
| 264 | &flags); |
Simon Glass | 049ae1b | 2017-05-18 20:09:01 -0600 | [diff] [blame] | 265 | if (!prop_val) |
| 266 | return FDT_ADDR_T_NONE; |
Mario Six | d007ebc | 2017-12-20 09:52:12 +0100 | [diff] [blame] | 267 | |
Mario Six | 35616ef | 2018-03-12 14:53:33 +0100 | [diff] [blame] | 268 | ns = of_n_size_cells(ofnode_to_np(node)); |
| 269 | |
| 270 | if (IS_ENABLED(CONFIG_OF_TRANSLATE) && ns > 0) { |
Mario Six | d007ebc | 2017-12-20 09:52:12 +0100 | [diff] [blame] | 271 | return of_translate_address(ofnode_to_np(node), prop_val); |
| 272 | } else { |
| 273 | na = of_n_addr_cells(ofnode_to_np(node)); |
| 274 | return of_read_number(prop_val, na); |
| 275 | } |
Simon Glass | 049ae1b | 2017-05-18 20:09:01 -0600 | [diff] [blame] | 276 | } else { |
| 277 | return fdt_get_base_address(gd->fdt_blob, |
| 278 | ofnode_to_offset(node)); |
| 279 | } |
| 280 | |
| 281 | return FDT_ADDR_T_NONE; |
| 282 | } |
| 283 | |
| 284 | fdt_addr_t ofnode_get_addr(ofnode node) |
| 285 | { |
| 286 | return ofnode_get_addr_index(node, 0); |
| 287 | } |
| 288 | |
Simon Glass | c4fc562 | 2017-05-18 20:08:58 -0600 | [diff] [blame] | 289 | int ofnode_stringlist_search(ofnode node, const char *property, |
| 290 | const char *string) |
| 291 | { |
| 292 | if (ofnode_is_np(node)) { |
| 293 | return of_property_match_string(ofnode_to_np(node), |
| 294 | property, string); |
| 295 | } else { |
| 296 | int ret; |
| 297 | |
| 298 | ret = fdt_stringlist_search(gd->fdt_blob, |
| 299 | ofnode_to_offset(node), property, |
| 300 | string); |
| 301 | if (ret == -FDT_ERR_NOTFOUND) |
| 302 | return -ENODATA; |
| 303 | else if (ret < 0) |
| 304 | return -EINVAL; |
| 305 | |
| 306 | return ret; |
| 307 | } |
| 308 | } |
| 309 | |
| 310 | int ofnode_read_string_index(ofnode node, const char *property, int index, |
| 311 | const char **outp) |
| 312 | { |
| 313 | if (ofnode_is_np(node)) { |
| 314 | return of_property_read_string_index(ofnode_to_np(node), |
| 315 | property, index, outp); |
| 316 | } else { |
| 317 | int len; |
| 318 | |
| 319 | *outp = fdt_stringlist_get(gd->fdt_blob, ofnode_to_offset(node), |
| 320 | property, index, &len); |
| 321 | if (len < 0) |
| 322 | return -EINVAL; |
| 323 | return 0; |
| 324 | } |
| 325 | } |
| 326 | |
Simon Glass | 5fdb005 | 2017-06-12 06:21:28 -0600 | [diff] [blame] | 327 | int ofnode_read_string_count(ofnode node, const char *property) |
| 328 | { |
| 329 | if (ofnode_is_np(node)) { |
| 330 | return of_property_count_strings(ofnode_to_np(node), property); |
| 331 | } else { |
| 332 | return fdt_stringlist_count(gd->fdt_blob, |
| 333 | ofnode_to_offset(node), property); |
| 334 | } |
| 335 | } |
| 336 | |
Simon Glass | c4fc562 | 2017-05-18 20:08:58 -0600 | [diff] [blame] | 337 | static void ofnode_from_fdtdec_phandle_args(struct fdtdec_phandle_args *in, |
| 338 | struct ofnode_phandle_args *out) |
| 339 | { |
| 340 | assert(OF_MAX_PHANDLE_ARGS == MAX_PHANDLE_ARGS); |
| 341 | out->node = offset_to_ofnode(in->node); |
| 342 | out->args_count = in->args_count; |
| 343 | memcpy(out->args, in->args, sizeof(out->args)); |
| 344 | } |
| 345 | |
| 346 | static void ofnode_from_of_phandle_args(struct of_phandle_args *in, |
| 347 | struct ofnode_phandle_args *out) |
| 348 | { |
| 349 | assert(OF_MAX_PHANDLE_ARGS == MAX_PHANDLE_ARGS); |
| 350 | out->node = np_to_ofnode(in->np); |
| 351 | out->args_count = in->args_count; |
| 352 | memcpy(out->args, in->args, sizeof(out->args)); |
| 353 | } |
| 354 | |
| 355 | int ofnode_parse_phandle_with_args(ofnode node, const char *list_name, |
| 356 | const char *cells_name, int cell_count, |
| 357 | int index, |
| 358 | struct ofnode_phandle_args *out_args) |
| 359 | { |
| 360 | if (ofnode_is_np(node)) { |
| 361 | struct of_phandle_args args; |
| 362 | int ret; |
| 363 | |
| 364 | ret = of_parse_phandle_with_args(ofnode_to_np(node), |
Mario Six | f40d82c | 2018-01-15 11:07:17 +0100 | [diff] [blame] | 365 | list_name, cells_name, index, |
| 366 | &args); |
Simon Glass | c4fc562 | 2017-05-18 20:08:58 -0600 | [diff] [blame] | 367 | if (ret) |
| 368 | return ret; |
| 369 | ofnode_from_of_phandle_args(&args, out_args); |
| 370 | } else { |
| 371 | struct fdtdec_phandle_args args; |
| 372 | int ret; |
| 373 | |
| 374 | ret = fdtdec_parse_phandle_with_args(gd->fdt_blob, |
Mario Six | f40d82c | 2018-01-15 11:07:17 +0100 | [diff] [blame] | 375 | ofnode_to_offset(node), |
| 376 | list_name, cells_name, |
| 377 | cell_count, index, &args); |
Simon Glass | c4fc562 | 2017-05-18 20:08:58 -0600 | [diff] [blame] | 378 | if (ret) |
| 379 | return ret; |
| 380 | ofnode_from_fdtdec_phandle_args(&args, out_args); |
| 381 | } |
| 382 | |
| 383 | return 0; |
| 384 | } |
| 385 | |
Patrice Chotard | be7dd60 | 2017-07-18 11:57:08 +0200 | [diff] [blame] | 386 | int ofnode_count_phandle_with_args(ofnode node, const char *list_name, |
| 387 | const char *cells_name) |
| 388 | { |
| 389 | if (ofnode_is_np(node)) |
| 390 | return of_count_phandle_with_args(ofnode_to_np(node), |
| 391 | list_name, cells_name); |
| 392 | else |
| 393 | return fdtdec_parse_phandle_with_args(gd->fdt_blob, |
| 394 | ofnode_to_offset(node), list_name, cells_name, |
| 395 | 0, -1, NULL); |
| 396 | } |
| 397 | |
Simon Glass | c4fc562 | 2017-05-18 20:08:58 -0600 | [diff] [blame] | 398 | ofnode ofnode_path(const char *path) |
| 399 | { |
| 400 | if (of_live_active()) |
| 401 | return np_to_ofnode(of_find_node_by_path(path)); |
| 402 | else |
| 403 | return offset_to_ofnode(fdt_path_offset(gd->fdt_blob, path)); |
| 404 | } |
| 405 | |
| 406 | const char *ofnode_get_chosen_prop(const char *name) |
| 407 | { |
| 408 | ofnode chosen_node; |
| 409 | |
| 410 | chosen_node = ofnode_path("/chosen"); |
| 411 | |
| 412 | return ofnode_read_string(chosen_node, name); |
| 413 | } |
| 414 | |
| 415 | ofnode ofnode_get_chosen_node(const char *name) |
| 416 | { |
| 417 | const char *prop; |
| 418 | |
| 419 | prop = ofnode_get_chosen_prop(name); |
| 420 | if (!prop) |
| 421 | return ofnode_null(); |
| 422 | |
| 423 | return ofnode_path(prop); |
| 424 | } |
| 425 | |
| 426 | static int decode_timing_property(ofnode node, const char *name, |
| 427 | struct timing_entry *result) |
| 428 | { |
| 429 | int length, ret = 0; |
| 430 | |
| 431 | length = ofnode_read_size(node, name); |
| 432 | if (length < 0) { |
| 433 | debug("%s: could not find property %s\n", |
| 434 | ofnode_get_name(node), name); |
| 435 | return length; |
| 436 | } |
| 437 | |
| 438 | if (length == sizeof(u32)) { |
| 439 | result->typ = ofnode_read_u32_default(node, name, 0); |
| 440 | result->min = result->typ; |
| 441 | result->max = result->typ; |
| 442 | } else { |
| 443 | ret = ofnode_read_u32_array(node, name, &result->min, 3); |
| 444 | } |
| 445 | |
| 446 | return ret; |
| 447 | } |
| 448 | |
| 449 | int ofnode_decode_display_timing(ofnode parent, int index, |
| 450 | struct display_timing *dt) |
| 451 | { |
| 452 | int i; |
| 453 | ofnode timings, node; |
| 454 | u32 val = 0; |
| 455 | int ret = 0; |
| 456 | |
| 457 | timings = ofnode_find_subnode(parent, "display-timings"); |
| 458 | if (!ofnode_valid(timings)) |
| 459 | return -EINVAL; |
| 460 | |
Simon Glass | 2852976 | 2017-08-05 15:45:54 -0600 | [diff] [blame] | 461 | i = 0; |
| 462 | ofnode_for_each_subnode(node, timings) { |
| 463 | if (i++ == index) |
| 464 | break; |
| 465 | } |
Simon Glass | c4fc562 | 2017-05-18 20:08:58 -0600 | [diff] [blame] | 466 | |
| 467 | if (!ofnode_valid(node)) |
| 468 | return -EINVAL; |
| 469 | |
| 470 | memset(dt, 0, sizeof(*dt)); |
| 471 | |
| 472 | ret |= decode_timing_property(node, "hback-porch", &dt->hback_porch); |
| 473 | ret |= decode_timing_property(node, "hfront-porch", &dt->hfront_porch); |
| 474 | ret |= decode_timing_property(node, "hactive", &dt->hactive); |
| 475 | ret |= decode_timing_property(node, "hsync-len", &dt->hsync_len); |
| 476 | ret |= decode_timing_property(node, "vback-porch", &dt->vback_porch); |
| 477 | ret |= decode_timing_property(node, "vfront-porch", &dt->vfront_porch); |
| 478 | ret |= decode_timing_property(node, "vactive", &dt->vactive); |
| 479 | ret |= decode_timing_property(node, "vsync-len", &dt->vsync_len); |
| 480 | ret |= decode_timing_property(node, "clock-frequency", &dt->pixelclock); |
| 481 | |
| 482 | dt->flags = 0; |
| 483 | val = ofnode_read_u32_default(node, "vsync-active", -1); |
| 484 | if (val != -1) { |
| 485 | dt->flags |= val ? DISPLAY_FLAGS_VSYNC_HIGH : |
| 486 | DISPLAY_FLAGS_VSYNC_LOW; |
| 487 | } |
| 488 | val = ofnode_read_u32_default(node, "hsync-active", -1); |
| 489 | if (val != -1) { |
| 490 | dt->flags |= val ? DISPLAY_FLAGS_HSYNC_HIGH : |
| 491 | DISPLAY_FLAGS_HSYNC_LOW; |
| 492 | } |
| 493 | val = ofnode_read_u32_default(node, "de-active", -1); |
| 494 | if (val != -1) { |
| 495 | dt->flags |= val ? DISPLAY_FLAGS_DE_HIGH : |
| 496 | DISPLAY_FLAGS_DE_LOW; |
| 497 | } |
| 498 | val = ofnode_read_u32_default(node, "pixelclk-active", -1); |
| 499 | if (val != -1) { |
| 500 | dt->flags |= val ? DISPLAY_FLAGS_PIXDATA_POSEDGE : |
| 501 | DISPLAY_FLAGS_PIXDATA_NEGEDGE; |
| 502 | } |
| 503 | |
| 504 | if (ofnode_read_bool(node, "interlaced")) |
| 505 | dt->flags |= DISPLAY_FLAGS_INTERLACED; |
| 506 | if (ofnode_read_bool(node, "doublescan")) |
| 507 | dt->flags |= DISPLAY_FLAGS_DOUBLESCAN; |
| 508 | if (ofnode_read_bool(node, "doubleclk")) |
| 509 | dt->flags |= DISPLAY_FLAGS_DOUBLECLK; |
| 510 | |
| 511 | return ret; |
| 512 | } |
| 513 | |
Masahiro Yamada | 9cf85cb | 2017-06-22 16:54:05 +0900 | [diff] [blame] | 514 | const void *ofnode_get_property(ofnode node, const char *propname, int *lenp) |
Simon Glass | c4fc562 | 2017-05-18 20:08:58 -0600 | [diff] [blame] | 515 | { |
Masahiro Yamada | 5052f1b | 2017-06-22 16:54:04 +0900 | [diff] [blame] | 516 | if (ofnode_is_np(node)) |
| 517 | return of_get_property(ofnode_to_np(node), propname, lenp); |
| 518 | else |
Simon Glass | c4fc562 | 2017-05-18 20:08:58 -0600 | [diff] [blame] | 519 | return fdt_getprop(gd->fdt_blob, ofnode_to_offset(node), |
| 520 | propname, lenp); |
Simon Glass | c4fc562 | 2017-05-18 20:08:58 -0600 | [diff] [blame] | 521 | } |
| 522 | |
| 523 | bool ofnode_is_available(ofnode node) |
| 524 | { |
| 525 | if (ofnode_is_np(node)) |
| 526 | return of_device_is_available(ofnode_to_np(node)); |
| 527 | else |
| 528 | return fdtdec_get_is_enabled(gd->fdt_blob, |
| 529 | ofnode_to_offset(node)); |
| 530 | } |
| 531 | |
| 532 | fdt_addr_t ofnode_get_addr_size(ofnode node, const char *property, |
| 533 | fdt_size_t *sizep) |
| 534 | { |
| 535 | if (ofnode_is_np(node)) { |
| 536 | int na, ns; |
| 537 | int psize; |
| 538 | const struct device_node *np = ofnode_to_np(node); |
Klaus Goger | af4b021 | 2017-09-20 13:50:41 +0200 | [diff] [blame] | 539 | const __be32 *prop = of_get_property(np, property, &psize); |
Simon Glass | c4fc562 | 2017-05-18 20:08:58 -0600 | [diff] [blame] | 540 | |
Klaus Goger | af4b021 | 2017-09-20 13:50:41 +0200 | [diff] [blame] | 541 | if (!prop) |
| 542 | return FDT_ADDR_T_NONE; |
Simon Glass | c4fc562 | 2017-05-18 20:08:58 -0600 | [diff] [blame] | 543 | na = of_n_addr_cells(np); |
| 544 | ns = of_n_addr_cells(np); |
Simon Glass | a67cc63 | 2017-05-18 20:09:27 -0600 | [diff] [blame] | 545 | *sizep = of_read_number(prop + na, ns); |
| 546 | return of_read_number(prop, na); |
Simon Glass | c4fc562 | 2017-05-18 20:08:58 -0600 | [diff] [blame] | 547 | } else { |
| 548 | return fdtdec_get_addr_size(gd->fdt_blob, |
| 549 | ofnode_to_offset(node), property, |
| 550 | sizep); |
| 551 | } |
| 552 | } |
| 553 | |
| 554 | const uint8_t *ofnode_read_u8_array_ptr(ofnode node, const char *propname, |
| 555 | size_t sz) |
| 556 | { |
| 557 | if (ofnode_is_np(node)) { |
| 558 | const struct device_node *np = ofnode_to_np(node); |
| 559 | int psize; |
| 560 | const __be32 *prop = of_get_property(np, propname, &psize); |
| 561 | |
| 562 | if (!prop || sz != psize) |
| 563 | return NULL; |
| 564 | return (uint8_t *)prop; |
| 565 | |
| 566 | } else { |
| 567 | return fdtdec_locate_byte_array(gd->fdt_blob, |
| 568 | ofnode_to_offset(node), propname, sz); |
| 569 | } |
| 570 | } |
| 571 | |
| 572 | int ofnode_read_pci_addr(ofnode node, enum fdt_pci_space type, |
| 573 | const char *propname, struct fdt_pci_addr *addr) |
| 574 | { |
Masahiro Yamada | 5c5991e | 2017-06-22 17:57:50 +0900 | [diff] [blame] | 575 | const fdt32_t *cell; |
Simon Glass | c4fc562 | 2017-05-18 20:08:58 -0600 | [diff] [blame] | 576 | int len; |
| 577 | int ret = -ENOENT; |
| 578 | |
| 579 | debug("%s: %s: ", __func__, propname); |
| 580 | |
| 581 | /* |
| 582 | * If we follow the pci bus bindings strictly, we should check |
| 583 | * the value of the node's parent node's #address-cells and |
| 584 | * #size-cells. They need to be 3 and 2 accordingly. However, |
| 585 | * for simplicity we skip the check here. |
| 586 | */ |
Masahiro Yamada | 9cf85cb | 2017-06-22 16:54:05 +0900 | [diff] [blame] | 587 | cell = ofnode_get_property(node, propname, &len); |
Simon Glass | c4fc562 | 2017-05-18 20:08:58 -0600 | [diff] [blame] | 588 | if (!cell) |
| 589 | goto fail; |
| 590 | |
| 591 | if ((len % FDT_PCI_REG_SIZE) == 0) { |
| 592 | int num = len / FDT_PCI_REG_SIZE; |
| 593 | int i; |
| 594 | |
| 595 | for (i = 0; i < num; i++) { |
| 596 | debug("pci address #%d: %08lx %08lx %08lx\n", i, |
| 597 | (ulong)fdt32_to_cpu(cell[0]), |
| 598 | (ulong)fdt32_to_cpu(cell[1]), |
| 599 | (ulong)fdt32_to_cpu(cell[2])); |
| 600 | if ((fdt32_to_cpu(*cell) & type) == type) { |
| 601 | addr->phys_hi = fdt32_to_cpu(cell[0]); |
| 602 | addr->phys_mid = fdt32_to_cpu(cell[1]); |
| 603 | addr->phys_lo = fdt32_to_cpu(cell[1]); |
| 604 | break; |
Simon Glass | c4fc562 | 2017-05-18 20:08:58 -0600 | [diff] [blame] | 605 | } |
Mario Six | f40d82c | 2018-01-15 11:07:17 +0100 | [diff] [blame] | 606 | |
| 607 | cell += (FDT_PCI_ADDR_CELLS + |
| 608 | FDT_PCI_SIZE_CELLS); |
Simon Glass | c4fc562 | 2017-05-18 20:08:58 -0600 | [diff] [blame] | 609 | } |
| 610 | |
| 611 | if (i == num) { |
| 612 | ret = -ENXIO; |
| 613 | goto fail; |
| 614 | } |
| 615 | |
| 616 | return 0; |
Simon Glass | c4fc562 | 2017-05-18 20:08:58 -0600 | [diff] [blame] | 617 | } |
| 618 | |
Mario Six | f40d82c | 2018-01-15 11:07:17 +0100 | [diff] [blame] | 619 | ret = -EINVAL; |
| 620 | |
Simon Glass | c4fc562 | 2017-05-18 20:08:58 -0600 | [diff] [blame] | 621 | fail: |
| 622 | debug("(not found)\n"); |
| 623 | return ret; |
| 624 | } |
| 625 | |
Bin Meng | fa15771 | 2018-08-03 01:14:35 -0700 | [diff] [blame] | 626 | int ofnode_read_pci_vendev(ofnode node, u16 *vendor, u16 *device) |
| 627 | { |
| 628 | const char *list, *end; |
| 629 | int len; |
| 630 | |
| 631 | list = ofnode_get_property(node, "compatible", &len); |
| 632 | if (!list) |
| 633 | return -ENOENT; |
| 634 | |
| 635 | end = list + len; |
| 636 | while (list < end) { |
| 637 | len = strlen(list); |
| 638 | if (len >= strlen("pciVVVV,DDDD")) { |
| 639 | char *s = strstr(list, "pci"); |
| 640 | |
| 641 | /* |
| 642 | * check if the string is something like pciVVVV,DDDD.RR |
| 643 | * or just pciVVVV,DDDD |
| 644 | */ |
| 645 | if (s && s[7] == ',' && |
| 646 | (s[12] == '.' || s[12] == 0)) { |
| 647 | s += 3; |
| 648 | *vendor = simple_strtol(s, NULL, 16); |
| 649 | |
| 650 | s += 5; |
| 651 | *device = simple_strtol(s, NULL, 16); |
| 652 | |
| 653 | return 0; |
| 654 | } |
| 655 | } |
| 656 | list += (len + 1); |
| 657 | } |
| 658 | |
| 659 | return -ENOENT; |
| 660 | } |
| 661 | |
Simon Glass | c4fc562 | 2017-05-18 20:08:58 -0600 | [diff] [blame] | 662 | int ofnode_read_addr_cells(ofnode node) |
| 663 | { |
| 664 | if (ofnode_is_np(node)) |
| 665 | return of_n_addr_cells(ofnode_to_np(node)); |
Simon Glass | 4191dc1 | 2017-06-12 06:21:31 -0600 | [diff] [blame] | 666 | else /* NOTE: this call should walk up the parent stack */ |
Simon Glass | c4fc562 | 2017-05-18 20:08:58 -0600 | [diff] [blame] | 667 | return fdt_address_cells(gd->fdt_blob, ofnode_to_offset(node)); |
| 668 | } |
| 669 | |
| 670 | int ofnode_read_size_cells(ofnode node) |
| 671 | { |
| 672 | if (ofnode_is_np(node)) |
| 673 | return of_n_size_cells(ofnode_to_np(node)); |
Simon Glass | 4191dc1 | 2017-06-12 06:21:31 -0600 | [diff] [blame] | 674 | else /* NOTE: this call should walk up the parent stack */ |
| 675 | return fdt_size_cells(gd->fdt_blob, ofnode_to_offset(node)); |
| 676 | } |
| 677 | |
| 678 | int ofnode_read_simple_addr_cells(ofnode node) |
| 679 | { |
| 680 | if (ofnode_is_np(node)) |
| 681 | return of_simple_addr_cells(ofnode_to_np(node)); |
| 682 | else |
| 683 | return fdt_address_cells(gd->fdt_blob, ofnode_to_offset(node)); |
| 684 | } |
| 685 | |
| 686 | int ofnode_read_simple_size_cells(ofnode node) |
| 687 | { |
| 688 | if (ofnode_is_np(node)) |
| 689 | return of_simple_size_cells(ofnode_to_np(node)); |
Simon Glass | c4fc562 | 2017-05-18 20:08:58 -0600 | [diff] [blame] | 690 | else |
| 691 | return fdt_size_cells(gd->fdt_blob, ofnode_to_offset(node)); |
| 692 | } |
| 693 | |
| 694 | bool ofnode_pre_reloc(ofnode node) |
| 695 | { |
Masahiro Yamada | 6a61dd9 | 2017-06-22 16:54:03 +0900 | [diff] [blame] | 696 | if (ofnode_read_bool(node, "u-boot,dm-pre-reloc")) |
Simon Glass | c4fc562 | 2017-05-18 20:08:58 -0600 | [diff] [blame] | 697 | return true; |
| 698 | |
| 699 | #ifdef CONFIG_TPL_BUILD |
Masahiro Yamada | 6a61dd9 | 2017-06-22 16:54:03 +0900 | [diff] [blame] | 700 | if (ofnode_read_bool(node, "u-boot,dm-tpl")) |
Simon Glass | c4fc562 | 2017-05-18 20:08:58 -0600 | [diff] [blame] | 701 | return true; |
| 702 | #elif defined(CONFIG_SPL_BUILD) |
Masahiro Yamada | 6a61dd9 | 2017-06-22 16:54:03 +0900 | [diff] [blame] | 703 | if (ofnode_read_bool(node, "u-boot,dm-spl")) |
Simon Glass | c4fc562 | 2017-05-18 20:08:58 -0600 | [diff] [blame] | 704 | return true; |
| 705 | #else |
| 706 | /* |
| 707 | * In regular builds individual spl and tpl handling both |
| 708 | * count as handled pre-relocation for later second init. |
| 709 | */ |
Masahiro Yamada | 6a61dd9 | 2017-06-22 16:54:03 +0900 | [diff] [blame] | 710 | if (ofnode_read_bool(node, "u-boot,dm-spl") || |
| 711 | ofnode_read_bool(node, "u-boot,dm-tpl")) |
Simon Glass | c4fc562 | 2017-05-18 20:08:58 -0600 | [diff] [blame] | 712 | return true; |
| 713 | #endif |
| 714 | |
| 715 | return false; |
| 716 | } |
Simon Glass | f7bfcc4 | 2017-07-25 08:29:55 -0600 | [diff] [blame] | 717 | |
| 718 | int ofnode_read_resource(ofnode node, uint index, struct resource *res) |
| 719 | { |
| 720 | if (ofnode_is_np(node)) { |
| 721 | return of_address_to_resource(ofnode_to_np(node), index, res); |
| 722 | } else { |
| 723 | struct fdt_resource fres; |
| 724 | int ret; |
| 725 | |
| 726 | ret = fdt_get_resource(gd->fdt_blob, ofnode_to_offset(node), |
| 727 | "reg", index, &fres); |
| 728 | if (ret < 0) |
| 729 | return -EINVAL; |
| 730 | memset(res, '\0', sizeof(*res)); |
| 731 | res->start = fres.start; |
| 732 | res->end = fres.end; |
| 733 | |
| 734 | return 0; |
| 735 | } |
| 736 | } |
Masahiro Yamada | 4dada2c | 2017-08-26 01:12:30 +0900 | [diff] [blame] | 737 | |
| 738 | int ofnode_read_resource_byname(ofnode node, const char *name, |
| 739 | struct resource *res) |
| 740 | { |
| 741 | int index; |
| 742 | |
| 743 | index = ofnode_stringlist_search(node, "reg-names", name); |
| 744 | if (index < 0) |
| 745 | return index; |
| 746 | |
| 747 | return ofnode_read_resource(node, index, res); |
| 748 | } |
Mario Six | aefac06 | 2018-01-15 11:07:19 +0100 | [diff] [blame] | 749 | |
| 750 | u64 ofnode_translate_address(ofnode node, const fdt32_t *in_addr) |
| 751 | { |
| 752 | if (ofnode_is_np(node)) |
| 753 | return of_translate_address(ofnode_to_np(node), in_addr); |
| 754 | else |
| 755 | return fdt_translate_address(gd->fdt_blob, ofnode_to_offset(node), in_addr); |
| 756 | } |
Masahiro Yamada | 9349bcc | 2018-04-19 12:14:02 +0900 | [diff] [blame] | 757 | |
| 758 | int ofnode_device_is_compatible(ofnode node, const char *compat) |
| 759 | { |
| 760 | if (ofnode_is_np(node)) |
| 761 | return of_device_is_compatible(ofnode_to_np(node), compat, |
| 762 | NULL, NULL); |
| 763 | else |
| 764 | return !fdt_node_check_compatible(gd->fdt_blob, |
| 765 | ofnode_to_offset(node), |
| 766 | compat); |
| 767 | } |
Simon Glass | 954eeae | 2018-06-11 13:07:13 -0600 | [diff] [blame] | 768 | |
| 769 | ofnode ofnode_by_compatible(ofnode from, const char *compat) |
| 770 | { |
| 771 | if (of_live_active()) { |
| 772 | return np_to_ofnode(of_find_compatible_node( |
| 773 | (struct device_node *)ofnode_to_np(from), NULL, |
| 774 | compat)); |
| 775 | } else { |
| 776 | return offset_to_ofnode(fdt_node_offset_by_compatible( |
| 777 | gd->fdt_blob, ofnode_to_offset(from), compat)); |
| 778 | } |
| 779 | } |
Jens Wiklander | 7b68dad | 2018-08-20 11:09:58 +0200 | [diff] [blame] | 780 | |
| 781 | ofnode ofnode_by_prop_value(ofnode from, const char *propname, |
| 782 | const void *propval, int proplen) |
| 783 | { |
| 784 | if (of_live_active()) { |
| 785 | return np_to_ofnode(of_find_node_by_prop_value( |
| 786 | (struct device_node *)ofnode_to_np(from), propname, |
| 787 | propval, proplen)); |
| 788 | } else { |
| 789 | return offset_to_ofnode(fdt_node_offset_by_prop_value( |
| 790 | gd->fdt_blob, ofnode_to_offset(from), |
| 791 | propname, propval, proplen)); |
| 792 | } |
| 793 | } |
Mario Six | 047dafc | 2018-06-26 08:46:48 +0200 | [diff] [blame] | 794 | |
| 795 | int ofnode_write_prop(ofnode node, const char *propname, int len, |
| 796 | const void *value) |
| 797 | { |
| 798 | const struct device_node *np = ofnode_to_np(node); |
| 799 | struct property *pp; |
| 800 | struct property *pp_last = NULL; |
| 801 | struct property *new; |
| 802 | |
| 803 | if (!of_live_active()) |
| 804 | return -ENOSYS; |
| 805 | |
| 806 | if (!np) |
| 807 | return -EINVAL; |
| 808 | |
| 809 | for (pp = np->properties; pp; pp = pp->next) { |
| 810 | if (strcmp(pp->name, propname) == 0) { |
| 811 | /* Property exists -> change value */ |
| 812 | pp->value = (void *)value; |
| 813 | pp->length = len; |
| 814 | return 0; |
| 815 | } |
| 816 | pp_last = pp; |
| 817 | } |
| 818 | |
| 819 | if (!pp_last) |
| 820 | return -ENOENT; |
| 821 | |
| 822 | /* Property does not exist -> append new property */ |
| 823 | new = malloc(sizeof(struct property)); |
| 824 | if (!new) |
| 825 | return -ENOMEM; |
| 826 | |
| 827 | new->name = strdup(propname); |
| 828 | if (!new->name) |
| 829 | return -ENOMEM; |
| 830 | |
| 831 | new->value = (void *)value; |
| 832 | new->length = len; |
| 833 | new->next = NULL; |
| 834 | |
| 835 | pp_last->next = new; |
| 836 | |
| 837 | return 0; |
| 838 | } |
| 839 | |
| 840 | int ofnode_write_string(ofnode node, const char *propname, const char *value) |
| 841 | { |
| 842 | if (!of_live_active()) |
| 843 | return -ENOSYS; |
| 844 | |
| 845 | assert(ofnode_valid(node)); |
| 846 | |
| 847 | debug("%s: %s = %s", __func__, propname, value); |
| 848 | |
| 849 | return ofnode_write_prop(node, propname, strlen(value) + 1, value); |
| 850 | } |
| 851 | |
| 852 | int ofnode_set_enabled(ofnode node, bool value) |
| 853 | { |
| 854 | if (!of_live_active()) |
| 855 | return -ENOSYS; |
| 856 | |
| 857 | assert(ofnode_valid(node)); |
| 858 | |
| 859 | if (value) |
| 860 | return ofnode_write_string(node, "status", "okay"); |
| 861 | else |
| 862 | return ofnode_write_string(node, "status", "disable"); |
| 863 | } |