Simon Glass | c4fc562 | 2017-05-18 20:08:58 -0600 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2017 Google, Inc |
| 3 | * Written by Simon Glass <sjg@chromium.org> |
| 4 | * |
| 5 | * SPDX-License-Identifier: GPL-2.0+ |
| 6 | */ |
| 7 | |
| 8 | #include <common.h> |
| 9 | #include <dm.h> |
| 10 | #include <fdtdec.h> |
| 11 | #include <fdt_support.h> |
Masahiro Yamada | 75f82d0 | 2018-03-05 01:20:11 +0900 | [diff] [blame] | 12 | #include <linux/libfdt.h> |
Simon Glass | c4fc562 | 2017-05-18 20:08:58 -0600 | [diff] [blame] | 13 | #include <dm/of_access.h> |
Simon Glass | 049ae1b | 2017-05-18 20:09:01 -0600 | [diff] [blame] | 14 | #include <dm/of_addr.h> |
Simon Glass | c4fc562 | 2017-05-18 20:08:58 -0600 | [diff] [blame] | 15 | #include <dm/ofnode.h> |
| 16 | #include <linux/err.h> |
Simon Glass | f7bfcc4 | 2017-07-25 08:29:55 -0600 | [diff] [blame] | 17 | #include <linux/ioport.h> |
Simon Glass | c4fc562 | 2017-05-18 20:08:58 -0600 | [diff] [blame] | 18 | |
| 19 | int ofnode_read_u32(ofnode node, const char *propname, u32 *outp) |
| 20 | { |
| 21 | assert(ofnode_valid(node)); |
| 22 | debug("%s: %s: ", __func__, propname); |
| 23 | |
| 24 | if (ofnode_is_np(node)) { |
| 25 | return of_read_u32(ofnode_to_np(node), propname, outp); |
| 26 | } else { |
Masahiro Yamada | 5c5991e | 2017-06-22 17:57:50 +0900 | [diff] [blame] | 27 | const fdt32_t *cell; |
Simon Glass | c4fc562 | 2017-05-18 20:08:58 -0600 | [diff] [blame] | 28 | int len; |
| 29 | |
| 30 | cell = fdt_getprop(gd->fdt_blob, ofnode_to_offset(node), |
| 31 | propname, &len); |
| 32 | if (!cell || len < sizeof(int)) { |
| 33 | debug("(not found)\n"); |
| 34 | return -EINVAL; |
| 35 | } |
| 36 | *outp = fdt32_to_cpu(cell[0]); |
| 37 | } |
| 38 | debug("%#x (%d)\n", *outp, *outp); |
| 39 | |
| 40 | return 0; |
| 41 | } |
| 42 | |
| 43 | int ofnode_read_u32_default(ofnode node, const char *propname, u32 def) |
| 44 | { |
| 45 | assert(ofnode_valid(node)); |
| 46 | ofnode_read_u32(node, propname, &def); |
| 47 | |
| 48 | return def; |
| 49 | } |
| 50 | |
| 51 | int ofnode_read_s32_default(ofnode node, const char *propname, s32 def) |
| 52 | { |
| 53 | assert(ofnode_valid(node)); |
| 54 | ofnode_read_u32(node, propname, (u32 *)&def); |
| 55 | |
| 56 | return def; |
| 57 | } |
| 58 | |
| 59 | bool ofnode_read_bool(ofnode node, const char *propname) |
| 60 | { |
Masahiro Yamada | 5d43445 | 2017-06-22 16:54:07 +0900 | [diff] [blame] | 61 | const void *prop; |
Simon Glass | c4fc562 | 2017-05-18 20:08:58 -0600 | [diff] [blame] | 62 | |
| 63 | assert(ofnode_valid(node)); |
| 64 | debug("%s: %s: ", __func__, propname); |
| 65 | |
Masahiro Yamada | 5d43445 | 2017-06-22 16:54:07 +0900 | [diff] [blame] | 66 | prop = ofnode_get_property(node, propname, NULL); |
| 67 | |
| 68 | debug("%s\n", prop ? "true" : "false"); |
Simon Glass | c4fc562 | 2017-05-18 20:08:58 -0600 | [diff] [blame] | 69 | |
Masahiro Yamada | 5d43445 | 2017-06-22 16:54:07 +0900 | [diff] [blame] | 70 | return prop ? true : false; |
Simon Glass | c4fc562 | 2017-05-18 20:08:58 -0600 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | const char *ofnode_read_string(ofnode node, const char *propname) |
| 74 | { |
| 75 | const char *str = NULL; |
| 76 | int len = -1; |
| 77 | |
| 78 | assert(ofnode_valid(node)); |
| 79 | debug("%s: %s: ", __func__, propname); |
| 80 | |
| 81 | if (ofnode_is_np(node)) { |
| 82 | struct property *prop = of_find_property( |
| 83 | ofnode_to_np(node), propname, NULL); |
| 84 | |
| 85 | if (prop) { |
| 86 | str = prop->value; |
| 87 | len = prop->length; |
| 88 | } |
| 89 | } else { |
| 90 | str = fdt_getprop(gd->fdt_blob, ofnode_to_offset(node), |
| 91 | propname, &len); |
| 92 | } |
| 93 | if (!str) { |
| 94 | debug("<not found>\n"); |
| 95 | return NULL; |
| 96 | } |
| 97 | if (strnlen(str, len) >= len) { |
| 98 | debug("<invalid>\n"); |
| 99 | return NULL; |
| 100 | } |
| 101 | debug("%s\n", str); |
| 102 | |
| 103 | return str; |
| 104 | } |
| 105 | |
| 106 | ofnode ofnode_find_subnode(ofnode node, const char *subnode_name) |
| 107 | { |
| 108 | ofnode subnode; |
| 109 | |
| 110 | assert(ofnode_valid(node)); |
| 111 | debug("%s: %s: ", __func__, subnode_name); |
| 112 | |
| 113 | if (ofnode_is_np(node)) { |
| 114 | const struct device_node *np = ofnode_to_np(node); |
| 115 | |
| 116 | for (np = np->child; np; np = np->sibling) { |
| 117 | if (!strcmp(subnode_name, np->name)) |
| 118 | break; |
| 119 | } |
| 120 | subnode = np_to_ofnode(np); |
| 121 | } else { |
| 122 | int ooffset = fdt_subnode_offset(gd->fdt_blob, |
| 123 | ofnode_to_offset(node), subnode_name); |
| 124 | subnode = offset_to_ofnode(ooffset); |
| 125 | } |
| 126 | debug("%s\n", ofnode_valid(subnode) ? |
| 127 | ofnode_get_name(subnode) : "<none>"); |
| 128 | |
| 129 | return subnode; |
| 130 | } |
| 131 | |
| 132 | int ofnode_read_u32_array(ofnode node, const char *propname, |
| 133 | u32 *out_values, size_t sz) |
| 134 | { |
| 135 | assert(ofnode_valid(node)); |
| 136 | debug("%s: %s: ", __func__, propname); |
| 137 | |
| 138 | if (ofnode_is_np(node)) { |
| 139 | return of_read_u32_array(ofnode_to_np(node), propname, |
| 140 | out_values, sz); |
| 141 | } else { |
| 142 | return fdtdec_get_int_array(gd->fdt_blob, |
| 143 | ofnode_to_offset(node), propname, |
| 144 | out_values, sz); |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | ofnode ofnode_first_subnode(ofnode node) |
| 149 | { |
| 150 | assert(ofnode_valid(node)); |
| 151 | if (ofnode_is_np(node)) |
| 152 | return np_to_ofnode(node.np->child); |
| 153 | |
| 154 | return offset_to_ofnode( |
| 155 | fdt_first_subnode(gd->fdt_blob, ofnode_to_offset(node))); |
| 156 | } |
| 157 | |
| 158 | ofnode ofnode_next_subnode(ofnode node) |
| 159 | { |
| 160 | assert(ofnode_valid(node)); |
| 161 | if (ofnode_is_np(node)) |
| 162 | return np_to_ofnode(node.np->sibling); |
| 163 | |
| 164 | return offset_to_ofnode( |
| 165 | fdt_next_subnode(gd->fdt_blob, ofnode_to_offset(node))); |
| 166 | } |
| 167 | |
Philipp Tomsich | 6fce1dd | 2018-02-23 17:38:49 +0100 | [diff] [blame] | 168 | ofnode ofnode_get_parent(ofnode node) |
| 169 | { |
| 170 | ofnode parent; |
| 171 | |
| 172 | assert(ofnode_valid(node)); |
| 173 | if (ofnode_is_np(node)) |
| 174 | parent = np_to_ofnode(of_get_parent(ofnode_to_np(node))); |
| 175 | else |
| 176 | parent.of_offset = fdt_parent_offset(gd->fdt_blob, |
| 177 | ofnode_to_offset(node)); |
| 178 | |
| 179 | return parent; |
| 180 | } |
| 181 | |
Simon Glass | c4fc562 | 2017-05-18 20:08:58 -0600 | [diff] [blame] | 182 | const char *ofnode_get_name(ofnode node) |
| 183 | { |
| 184 | assert(ofnode_valid(node)); |
| 185 | if (ofnode_is_np(node)) |
| 186 | return strrchr(node.np->full_name, '/') + 1; |
| 187 | |
| 188 | return fdt_get_name(gd->fdt_blob, ofnode_to_offset(node), NULL); |
| 189 | } |
| 190 | |
Kever Yang | 37df0e0 | 2018-02-23 17:38:50 +0100 | [diff] [blame] | 191 | ofnode ofnode_get_by_phandle(uint phandle) |
| 192 | { |
| 193 | ofnode node; |
| 194 | |
| 195 | if (of_live_active()) |
| 196 | node = np_to_ofnode(of_find_node_by_phandle(phandle)); |
| 197 | else |
| 198 | node.of_offset = fdt_node_offset_by_phandle(gd->fdt_blob, |
| 199 | phandle); |
| 200 | |
| 201 | return node; |
| 202 | } |
| 203 | |
Simon Glass | c4fc562 | 2017-05-18 20:08:58 -0600 | [diff] [blame] | 204 | int ofnode_read_size(ofnode node, const char *propname) |
| 205 | { |
| 206 | int len; |
| 207 | |
| 208 | if (ofnode_is_np(node)) { |
| 209 | struct property *prop = of_find_property( |
| 210 | ofnode_to_np(node), propname, NULL); |
| 211 | |
| 212 | if (prop) |
| 213 | return prop->length; |
| 214 | } else { |
| 215 | if (fdt_getprop(gd->fdt_blob, ofnode_to_offset(node), propname, |
| 216 | &len)) |
| 217 | return len; |
| 218 | } |
| 219 | |
| 220 | return -EINVAL; |
| 221 | } |
| 222 | |
Simon Glass | 049ae1b | 2017-05-18 20:09:01 -0600 | [diff] [blame] | 223 | fdt_addr_t ofnode_get_addr_index(ofnode node, int index) |
| 224 | { |
| 225 | if (ofnode_is_np(node)) { |
| 226 | const __be32 *prop_val; |
| 227 | uint flags; |
| 228 | u64 size; |
Simon Glass | 76a6666 | 2017-07-25 08:29:56 -0600 | [diff] [blame] | 229 | int na; |
Mario Six | 35616ef | 2018-03-12 14:53:33 +0100 | [diff] [blame] | 230 | int ns; |
Simon Glass | 049ae1b | 2017-05-18 20:09:01 -0600 | [diff] [blame] | 231 | |
Simon Glass | 76a6666 | 2017-07-25 08:29:56 -0600 | [diff] [blame] | 232 | prop_val = of_get_address(ofnode_to_np(node), index, &size, |
| 233 | &flags); |
Simon Glass | 049ae1b | 2017-05-18 20:09:01 -0600 | [diff] [blame] | 234 | if (!prop_val) |
| 235 | return FDT_ADDR_T_NONE; |
Mario Six | d007ebc | 2017-12-20 09:52:12 +0100 | [diff] [blame] | 236 | |
Mario Six | 35616ef | 2018-03-12 14:53:33 +0100 | [diff] [blame] | 237 | ns = of_n_size_cells(ofnode_to_np(node)); |
| 238 | |
| 239 | if (IS_ENABLED(CONFIG_OF_TRANSLATE) && ns > 0) { |
Mario Six | d007ebc | 2017-12-20 09:52:12 +0100 | [diff] [blame] | 240 | return of_translate_address(ofnode_to_np(node), prop_val); |
| 241 | } else { |
| 242 | na = of_n_addr_cells(ofnode_to_np(node)); |
| 243 | return of_read_number(prop_val, na); |
| 244 | } |
Simon Glass | 049ae1b | 2017-05-18 20:09:01 -0600 | [diff] [blame] | 245 | } else { |
| 246 | return fdt_get_base_address(gd->fdt_blob, |
| 247 | ofnode_to_offset(node)); |
| 248 | } |
| 249 | |
| 250 | return FDT_ADDR_T_NONE; |
| 251 | } |
| 252 | |
| 253 | fdt_addr_t ofnode_get_addr(ofnode node) |
| 254 | { |
| 255 | return ofnode_get_addr_index(node, 0); |
| 256 | } |
| 257 | |
Simon Glass | c4fc562 | 2017-05-18 20:08:58 -0600 | [diff] [blame] | 258 | int ofnode_stringlist_search(ofnode node, const char *property, |
| 259 | const char *string) |
| 260 | { |
| 261 | if (ofnode_is_np(node)) { |
| 262 | return of_property_match_string(ofnode_to_np(node), |
| 263 | property, string); |
| 264 | } else { |
| 265 | int ret; |
| 266 | |
| 267 | ret = fdt_stringlist_search(gd->fdt_blob, |
| 268 | ofnode_to_offset(node), property, |
| 269 | string); |
| 270 | if (ret == -FDT_ERR_NOTFOUND) |
| 271 | return -ENODATA; |
| 272 | else if (ret < 0) |
| 273 | return -EINVAL; |
| 274 | |
| 275 | return ret; |
| 276 | } |
| 277 | } |
| 278 | |
| 279 | int ofnode_read_string_index(ofnode node, const char *property, int index, |
| 280 | const char **outp) |
| 281 | { |
| 282 | if (ofnode_is_np(node)) { |
| 283 | return of_property_read_string_index(ofnode_to_np(node), |
| 284 | property, index, outp); |
| 285 | } else { |
| 286 | int len; |
| 287 | |
| 288 | *outp = fdt_stringlist_get(gd->fdt_blob, ofnode_to_offset(node), |
| 289 | property, index, &len); |
| 290 | if (len < 0) |
| 291 | return -EINVAL; |
| 292 | return 0; |
| 293 | } |
| 294 | } |
| 295 | |
Simon Glass | 5fdb005 | 2017-06-12 06:21:28 -0600 | [diff] [blame] | 296 | int ofnode_read_string_count(ofnode node, const char *property) |
| 297 | { |
| 298 | if (ofnode_is_np(node)) { |
| 299 | return of_property_count_strings(ofnode_to_np(node), property); |
| 300 | } else { |
| 301 | return fdt_stringlist_count(gd->fdt_blob, |
| 302 | ofnode_to_offset(node), property); |
| 303 | } |
| 304 | } |
| 305 | |
Simon Glass | c4fc562 | 2017-05-18 20:08:58 -0600 | [diff] [blame] | 306 | static void ofnode_from_fdtdec_phandle_args(struct fdtdec_phandle_args *in, |
| 307 | struct ofnode_phandle_args *out) |
| 308 | { |
| 309 | assert(OF_MAX_PHANDLE_ARGS == MAX_PHANDLE_ARGS); |
| 310 | out->node = offset_to_ofnode(in->node); |
| 311 | out->args_count = in->args_count; |
| 312 | memcpy(out->args, in->args, sizeof(out->args)); |
| 313 | } |
| 314 | |
| 315 | static void ofnode_from_of_phandle_args(struct of_phandle_args *in, |
| 316 | struct ofnode_phandle_args *out) |
| 317 | { |
| 318 | assert(OF_MAX_PHANDLE_ARGS == MAX_PHANDLE_ARGS); |
| 319 | out->node = np_to_ofnode(in->np); |
| 320 | out->args_count = in->args_count; |
| 321 | memcpy(out->args, in->args, sizeof(out->args)); |
| 322 | } |
| 323 | |
| 324 | int ofnode_parse_phandle_with_args(ofnode node, const char *list_name, |
| 325 | const char *cells_name, int cell_count, |
| 326 | int index, |
| 327 | struct ofnode_phandle_args *out_args) |
| 328 | { |
| 329 | if (ofnode_is_np(node)) { |
| 330 | struct of_phandle_args args; |
| 331 | int ret; |
| 332 | |
| 333 | ret = of_parse_phandle_with_args(ofnode_to_np(node), |
Mario Six | f40d82c | 2018-01-15 11:07:17 +0100 | [diff] [blame] | 334 | list_name, cells_name, index, |
| 335 | &args); |
Simon Glass | c4fc562 | 2017-05-18 20:08:58 -0600 | [diff] [blame] | 336 | if (ret) |
| 337 | return ret; |
| 338 | ofnode_from_of_phandle_args(&args, out_args); |
| 339 | } else { |
| 340 | struct fdtdec_phandle_args args; |
| 341 | int ret; |
| 342 | |
| 343 | ret = fdtdec_parse_phandle_with_args(gd->fdt_blob, |
Mario Six | f40d82c | 2018-01-15 11:07:17 +0100 | [diff] [blame] | 344 | ofnode_to_offset(node), |
| 345 | list_name, cells_name, |
| 346 | cell_count, index, &args); |
Simon Glass | c4fc562 | 2017-05-18 20:08:58 -0600 | [diff] [blame] | 347 | if (ret) |
| 348 | return ret; |
| 349 | ofnode_from_fdtdec_phandle_args(&args, out_args); |
| 350 | } |
| 351 | |
| 352 | return 0; |
| 353 | } |
| 354 | |
Patrice Chotard | be7dd60 | 2017-07-18 11:57:08 +0200 | [diff] [blame] | 355 | int ofnode_count_phandle_with_args(ofnode node, const char *list_name, |
| 356 | const char *cells_name) |
| 357 | { |
| 358 | if (ofnode_is_np(node)) |
| 359 | return of_count_phandle_with_args(ofnode_to_np(node), |
| 360 | list_name, cells_name); |
| 361 | else |
| 362 | return fdtdec_parse_phandle_with_args(gd->fdt_blob, |
| 363 | ofnode_to_offset(node), list_name, cells_name, |
| 364 | 0, -1, NULL); |
| 365 | } |
| 366 | |
Simon Glass | c4fc562 | 2017-05-18 20:08:58 -0600 | [diff] [blame] | 367 | ofnode ofnode_path(const char *path) |
| 368 | { |
| 369 | if (of_live_active()) |
| 370 | return np_to_ofnode(of_find_node_by_path(path)); |
| 371 | else |
| 372 | return offset_to_ofnode(fdt_path_offset(gd->fdt_blob, path)); |
| 373 | } |
| 374 | |
| 375 | const char *ofnode_get_chosen_prop(const char *name) |
| 376 | { |
| 377 | ofnode chosen_node; |
| 378 | |
| 379 | chosen_node = ofnode_path("/chosen"); |
| 380 | |
| 381 | return ofnode_read_string(chosen_node, name); |
| 382 | } |
| 383 | |
| 384 | ofnode ofnode_get_chosen_node(const char *name) |
| 385 | { |
| 386 | const char *prop; |
| 387 | |
| 388 | prop = ofnode_get_chosen_prop(name); |
| 389 | if (!prop) |
| 390 | return ofnode_null(); |
| 391 | |
| 392 | return ofnode_path(prop); |
| 393 | } |
| 394 | |
| 395 | static int decode_timing_property(ofnode node, const char *name, |
| 396 | struct timing_entry *result) |
| 397 | { |
| 398 | int length, ret = 0; |
| 399 | |
| 400 | length = ofnode_read_size(node, name); |
| 401 | if (length < 0) { |
| 402 | debug("%s: could not find property %s\n", |
| 403 | ofnode_get_name(node), name); |
| 404 | return length; |
| 405 | } |
| 406 | |
| 407 | if (length == sizeof(u32)) { |
| 408 | result->typ = ofnode_read_u32_default(node, name, 0); |
| 409 | result->min = result->typ; |
| 410 | result->max = result->typ; |
| 411 | } else { |
| 412 | ret = ofnode_read_u32_array(node, name, &result->min, 3); |
| 413 | } |
| 414 | |
| 415 | return ret; |
| 416 | } |
| 417 | |
| 418 | int ofnode_decode_display_timing(ofnode parent, int index, |
| 419 | struct display_timing *dt) |
| 420 | { |
| 421 | int i; |
| 422 | ofnode timings, node; |
| 423 | u32 val = 0; |
| 424 | int ret = 0; |
| 425 | |
| 426 | timings = ofnode_find_subnode(parent, "display-timings"); |
| 427 | if (!ofnode_valid(timings)) |
| 428 | return -EINVAL; |
| 429 | |
Simon Glass | 2852976 | 2017-08-05 15:45:54 -0600 | [diff] [blame] | 430 | i = 0; |
| 431 | ofnode_for_each_subnode(node, timings) { |
| 432 | if (i++ == index) |
| 433 | break; |
| 434 | } |
Simon Glass | c4fc562 | 2017-05-18 20:08:58 -0600 | [diff] [blame] | 435 | |
| 436 | if (!ofnode_valid(node)) |
| 437 | return -EINVAL; |
| 438 | |
| 439 | memset(dt, 0, sizeof(*dt)); |
| 440 | |
| 441 | ret |= decode_timing_property(node, "hback-porch", &dt->hback_porch); |
| 442 | ret |= decode_timing_property(node, "hfront-porch", &dt->hfront_porch); |
| 443 | ret |= decode_timing_property(node, "hactive", &dt->hactive); |
| 444 | ret |= decode_timing_property(node, "hsync-len", &dt->hsync_len); |
| 445 | ret |= decode_timing_property(node, "vback-porch", &dt->vback_porch); |
| 446 | ret |= decode_timing_property(node, "vfront-porch", &dt->vfront_porch); |
| 447 | ret |= decode_timing_property(node, "vactive", &dt->vactive); |
| 448 | ret |= decode_timing_property(node, "vsync-len", &dt->vsync_len); |
| 449 | ret |= decode_timing_property(node, "clock-frequency", &dt->pixelclock); |
| 450 | |
| 451 | dt->flags = 0; |
| 452 | val = ofnode_read_u32_default(node, "vsync-active", -1); |
| 453 | if (val != -1) { |
| 454 | dt->flags |= val ? DISPLAY_FLAGS_VSYNC_HIGH : |
| 455 | DISPLAY_FLAGS_VSYNC_LOW; |
| 456 | } |
| 457 | val = ofnode_read_u32_default(node, "hsync-active", -1); |
| 458 | if (val != -1) { |
| 459 | dt->flags |= val ? DISPLAY_FLAGS_HSYNC_HIGH : |
| 460 | DISPLAY_FLAGS_HSYNC_LOW; |
| 461 | } |
| 462 | val = ofnode_read_u32_default(node, "de-active", -1); |
| 463 | if (val != -1) { |
| 464 | dt->flags |= val ? DISPLAY_FLAGS_DE_HIGH : |
| 465 | DISPLAY_FLAGS_DE_LOW; |
| 466 | } |
| 467 | val = ofnode_read_u32_default(node, "pixelclk-active", -1); |
| 468 | if (val != -1) { |
| 469 | dt->flags |= val ? DISPLAY_FLAGS_PIXDATA_POSEDGE : |
| 470 | DISPLAY_FLAGS_PIXDATA_NEGEDGE; |
| 471 | } |
| 472 | |
| 473 | if (ofnode_read_bool(node, "interlaced")) |
| 474 | dt->flags |= DISPLAY_FLAGS_INTERLACED; |
| 475 | if (ofnode_read_bool(node, "doublescan")) |
| 476 | dt->flags |= DISPLAY_FLAGS_DOUBLESCAN; |
| 477 | if (ofnode_read_bool(node, "doubleclk")) |
| 478 | dt->flags |= DISPLAY_FLAGS_DOUBLECLK; |
| 479 | |
| 480 | return ret; |
| 481 | } |
| 482 | |
Masahiro Yamada | 9cf85cb | 2017-06-22 16:54:05 +0900 | [diff] [blame] | 483 | const void *ofnode_get_property(ofnode node, const char *propname, int *lenp) |
Simon Glass | c4fc562 | 2017-05-18 20:08:58 -0600 | [diff] [blame] | 484 | { |
Masahiro Yamada | 5052f1b | 2017-06-22 16:54:04 +0900 | [diff] [blame] | 485 | if (ofnode_is_np(node)) |
| 486 | return of_get_property(ofnode_to_np(node), propname, lenp); |
| 487 | else |
Simon Glass | c4fc562 | 2017-05-18 20:08:58 -0600 | [diff] [blame] | 488 | return fdt_getprop(gd->fdt_blob, ofnode_to_offset(node), |
| 489 | propname, lenp); |
Simon Glass | c4fc562 | 2017-05-18 20:08:58 -0600 | [diff] [blame] | 490 | } |
| 491 | |
| 492 | bool ofnode_is_available(ofnode node) |
| 493 | { |
| 494 | if (ofnode_is_np(node)) |
| 495 | return of_device_is_available(ofnode_to_np(node)); |
| 496 | else |
| 497 | return fdtdec_get_is_enabled(gd->fdt_blob, |
| 498 | ofnode_to_offset(node)); |
| 499 | } |
| 500 | |
| 501 | fdt_addr_t ofnode_get_addr_size(ofnode node, const char *property, |
| 502 | fdt_size_t *sizep) |
| 503 | { |
| 504 | if (ofnode_is_np(node)) { |
| 505 | int na, ns; |
| 506 | int psize; |
| 507 | const struct device_node *np = ofnode_to_np(node); |
Klaus Goger | af4b021 | 2017-09-20 13:50:41 +0200 | [diff] [blame] | 508 | const __be32 *prop = of_get_property(np, property, &psize); |
Simon Glass | c4fc562 | 2017-05-18 20:08:58 -0600 | [diff] [blame] | 509 | |
Klaus Goger | af4b021 | 2017-09-20 13:50:41 +0200 | [diff] [blame] | 510 | if (!prop) |
| 511 | return FDT_ADDR_T_NONE; |
Simon Glass | c4fc562 | 2017-05-18 20:08:58 -0600 | [diff] [blame] | 512 | na = of_n_addr_cells(np); |
| 513 | ns = of_n_addr_cells(np); |
Simon Glass | a67cc63 | 2017-05-18 20:09:27 -0600 | [diff] [blame] | 514 | *sizep = of_read_number(prop + na, ns); |
| 515 | return of_read_number(prop, na); |
Simon Glass | c4fc562 | 2017-05-18 20:08:58 -0600 | [diff] [blame] | 516 | } else { |
| 517 | return fdtdec_get_addr_size(gd->fdt_blob, |
| 518 | ofnode_to_offset(node), property, |
| 519 | sizep); |
| 520 | } |
| 521 | } |
| 522 | |
| 523 | const uint8_t *ofnode_read_u8_array_ptr(ofnode node, const char *propname, |
| 524 | size_t sz) |
| 525 | { |
| 526 | if (ofnode_is_np(node)) { |
| 527 | const struct device_node *np = ofnode_to_np(node); |
| 528 | int psize; |
| 529 | const __be32 *prop = of_get_property(np, propname, &psize); |
| 530 | |
| 531 | if (!prop || sz != psize) |
| 532 | return NULL; |
| 533 | return (uint8_t *)prop; |
| 534 | |
| 535 | } else { |
| 536 | return fdtdec_locate_byte_array(gd->fdt_blob, |
| 537 | ofnode_to_offset(node), propname, sz); |
| 538 | } |
| 539 | } |
| 540 | |
| 541 | int ofnode_read_pci_addr(ofnode node, enum fdt_pci_space type, |
| 542 | const char *propname, struct fdt_pci_addr *addr) |
| 543 | { |
Masahiro Yamada | 5c5991e | 2017-06-22 17:57:50 +0900 | [diff] [blame] | 544 | const fdt32_t *cell; |
Simon Glass | c4fc562 | 2017-05-18 20:08:58 -0600 | [diff] [blame] | 545 | int len; |
| 546 | int ret = -ENOENT; |
| 547 | |
| 548 | debug("%s: %s: ", __func__, propname); |
| 549 | |
| 550 | /* |
| 551 | * If we follow the pci bus bindings strictly, we should check |
| 552 | * the value of the node's parent node's #address-cells and |
| 553 | * #size-cells. They need to be 3 and 2 accordingly. However, |
| 554 | * for simplicity we skip the check here. |
| 555 | */ |
Masahiro Yamada | 9cf85cb | 2017-06-22 16:54:05 +0900 | [diff] [blame] | 556 | cell = ofnode_get_property(node, propname, &len); |
Simon Glass | c4fc562 | 2017-05-18 20:08:58 -0600 | [diff] [blame] | 557 | if (!cell) |
| 558 | goto fail; |
| 559 | |
| 560 | if ((len % FDT_PCI_REG_SIZE) == 0) { |
| 561 | int num = len / FDT_PCI_REG_SIZE; |
| 562 | int i; |
| 563 | |
| 564 | for (i = 0; i < num; i++) { |
| 565 | debug("pci address #%d: %08lx %08lx %08lx\n", i, |
| 566 | (ulong)fdt32_to_cpu(cell[0]), |
| 567 | (ulong)fdt32_to_cpu(cell[1]), |
| 568 | (ulong)fdt32_to_cpu(cell[2])); |
| 569 | if ((fdt32_to_cpu(*cell) & type) == type) { |
| 570 | addr->phys_hi = fdt32_to_cpu(cell[0]); |
| 571 | addr->phys_mid = fdt32_to_cpu(cell[1]); |
| 572 | addr->phys_lo = fdt32_to_cpu(cell[1]); |
| 573 | break; |
Simon Glass | c4fc562 | 2017-05-18 20:08:58 -0600 | [diff] [blame] | 574 | } |
Mario Six | f40d82c | 2018-01-15 11:07:17 +0100 | [diff] [blame] | 575 | |
| 576 | cell += (FDT_PCI_ADDR_CELLS + |
| 577 | FDT_PCI_SIZE_CELLS); |
Simon Glass | c4fc562 | 2017-05-18 20:08:58 -0600 | [diff] [blame] | 578 | } |
| 579 | |
| 580 | if (i == num) { |
| 581 | ret = -ENXIO; |
| 582 | goto fail; |
| 583 | } |
| 584 | |
| 585 | return 0; |
Simon Glass | c4fc562 | 2017-05-18 20:08:58 -0600 | [diff] [blame] | 586 | } |
| 587 | |
Mario Six | f40d82c | 2018-01-15 11:07:17 +0100 | [diff] [blame] | 588 | ret = -EINVAL; |
| 589 | |
Simon Glass | c4fc562 | 2017-05-18 20:08:58 -0600 | [diff] [blame] | 590 | fail: |
| 591 | debug("(not found)\n"); |
| 592 | return ret; |
| 593 | } |
| 594 | |
| 595 | int ofnode_read_addr_cells(ofnode node) |
| 596 | { |
| 597 | if (ofnode_is_np(node)) |
| 598 | return of_n_addr_cells(ofnode_to_np(node)); |
Simon Glass | 4191dc1 | 2017-06-12 06:21:31 -0600 | [diff] [blame] | 599 | else /* NOTE: this call should walk up the parent stack */ |
Simon Glass | c4fc562 | 2017-05-18 20:08:58 -0600 | [diff] [blame] | 600 | return fdt_address_cells(gd->fdt_blob, ofnode_to_offset(node)); |
| 601 | } |
| 602 | |
| 603 | int ofnode_read_size_cells(ofnode node) |
| 604 | { |
| 605 | if (ofnode_is_np(node)) |
| 606 | return of_n_size_cells(ofnode_to_np(node)); |
Simon Glass | 4191dc1 | 2017-06-12 06:21:31 -0600 | [diff] [blame] | 607 | else /* NOTE: this call should walk up the parent stack */ |
| 608 | return fdt_size_cells(gd->fdt_blob, ofnode_to_offset(node)); |
| 609 | } |
| 610 | |
| 611 | int ofnode_read_simple_addr_cells(ofnode node) |
| 612 | { |
| 613 | if (ofnode_is_np(node)) |
| 614 | return of_simple_addr_cells(ofnode_to_np(node)); |
| 615 | else |
| 616 | return fdt_address_cells(gd->fdt_blob, ofnode_to_offset(node)); |
| 617 | } |
| 618 | |
| 619 | int ofnode_read_simple_size_cells(ofnode node) |
| 620 | { |
| 621 | if (ofnode_is_np(node)) |
| 622 | return of_simple_size_cells(ofnode_to_np(node)); |
Simon Glass | c4fc562 | 2017-05-18 20:08:58 -0600 | [diff] [blame] | 623 | else |
| 624 | return fdt_size_cells(gd->fdt_blob, ofnode_to_offset(node)); |
| 625 | } |
| 626 | |
| 627 | bool ofnode_pre_reloc(ofnode node) |
| 628 | { |
Masahiro Yamada | 6a61dd9 | 2017-06-22 16:54:03 +0900 | [diff] [blame] | 629 | if (ofnode_read_bool(node, "u-boot,dm-pre-reloc")) |
Simon Glass | c4fc562 | 2017-05-18 20:08:58 -0600 | [diff] [blame] | 630 | return true; |
| 631 | |
| 632 | #ifdef CONFIG_TPL_BUILD |
Masahiro Yamada | 6a61dd9 | 2017-06-22 16:54:03 +0900 | [diff] [blame] | 633 | if (ofnode_read_bool(node, "u-boot,dm-tpl")) |
Simon Glass | c4fc562 | 2017-05-18 20:08:58 -0600 | [diff] [blame] | 634 | return true; |
| 635 | #elif defined(CONFIG_SPL_BUILD) |
Masahiro Yamada | 6a61dd9 | 2017-06-22 16:54:03 +0900 | [diff] [blame] | 636 | if (ofnode_read_bool(node, "u-boot,dm-spl")) |
Simon Glass | c4fc562 | 2017-05-18 20:08:58 -0600 | [diff] [blame] | 637 | return true; |
| 638 | #else |
| 639 | /* |
| 640 | * In regular builds individual spl and tpl handling both |
| 641 | * count as handled pre-relocation for later second init. |
| 642 | */ |
Masahiro Yamada | 6a61dd9 | 2017-06-22 16:54:03 +0900 | [diff] [blame] | 643 | if (ofnode_read_bool(node, "u-boot,dm-spl") || |
| 644 | ofnode_read_bool(node, "u-boot,dm-tpl")) |
Simon Glass | c4fc562 | 2017-05-18 20:08:58 -0600 | [diff] [blame] | 645 | return true; |
| 646 | #endif |
| 647 | |
| 648 | return false; |
| 649 | } |
Simon Glass | f7bfcc4 | 2017-07-25 08:29:55 -0600 | [diff] [blame] | 650 | |
| 651 | int ofnode_read_resource(ofnode node, uint index, struct resource *res) |
| 652 | { |
| 653 | if (ofnode_is_np(node)) { |
| 654 | return of_address_to_resource(ofnode_to_np(node), index, res); |
| 655 | } else { |
| 656 | struct fdt_resource fres; |
| 657 | int ret; |
| 658 | |
| 659 | ret = fdt_get_resource(gd->fdt_blob, ofnode_to_offset(node), |
| 660 | "reg", index, &fres); |
| 661 | if (ret < 0) |
| 662 | return -EINVAL; |
| 663 | memset(res, '\0', sizeof(*res)); |
| 664 | res->start = fres.start; |
| 665 | res->end = fres.end; |
| 666 | |
| 667 | return 0; |
| 668 | } |
| 669 | } |
Masahiro Yamada | 4dada2c | 2017-08-26 01:12:30 +0900 | [diff] [blame] | 670 | |
| 671 | int ofnode_read_resource_byname(ofnode node, const char *name, |
| 672 | struct resource *res) |
| 673 | { |
| 674 | int index; |
| 675 | |
| 676 | index = ofnode_stringlist_search(node, "reg-names", name); |
| 677 | if (index < 0) |
| 678 | return index; |
| 679 | |
| 680 | return ofnode_read_resource(node, index, res); |
| 681 | } |
Mario Six | aefac06 | 2018-01-15 11:07:19 +0100 | [diff] [blame] | 682 | |
| 683 | u64 ofnode_translate_address(ofnode node, const fdt32_t *in_addr) |
| 684 | { |
| 685 | if (ofnode_is_np(node)) |
| 686 | return of_translate_address(ofnode_to_np(node), in_addr); |
| 687 | else |
| 688 | return fdt_translate_address(gd->fdt_blob, ofnode_to_offset(node), in_addr); |
| 689 | } |