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> |
| 12 | #include <libfdt.h> |
| 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 | |
| 168 | const char *ofnode_get_name(ofnode node) |
| 169 | { |
| 170 | assert(ofnode_valid(node)); |
| 171 | if (ofnode_is_np(node)) |
| 172 | return strrchr(node.np->full_name, '/') + 1; |
| 173 | |
| 174 | return fdt_get_name(gd->fdt_blob, ofnode_to_offset(node), NULL); |
| 175 | } |
| 176 | |
| 177 | int ofnode_read_size(ofnode node, const char *propname) |
| 178 | { |
| 179 | int len; |
| 180 | |
| 181 | if (ofnode_is_np(node)) { |
| 182 | struct property *prop = of_find_property( |
| 183 | ofnode_to_np(node), propname, NULL); |
| 184 | |
| 185 | if (prop) |
| 186 | return prop->length; |
| 187 | } else { |
| 188 | if (fdt_getprop(gd->fdt_blob, ofnode_to_offset(node), propname, |
| 189 | &len)) |
| 190 | return len; |
| 191 | } |
| 192 | |
| 193 | return -EINVAL; |
| 194 | } |
| 195 | |
Simon Glass | 049ae1b | 2017-05-18 20:09:01 -0600 | [diff] [blame] | 196 | fdt_addr_t ofnode_get_addr_index(ofnode node, int index) |
| 197 | { |
| 198 | if (ofnode_is_np(node)) { |
| 199 | const __be32 *prop_val; |
| 200 | uint flags; |
| 201 | u64 size; |
Simon Glass | 76a6666 | 2017-07-25 08:29:56 -0600 | [diff] [blame] | 202 | int na; |
Simon Glass | 049ae1b | 2017-05-18 20:09:01 -0600 | [diff] [blame] | 203 | |
Simon Glass | 76a6666 | 2017-07-25 08:29:56 -0600 | [diff] [blame] | 204 | prop_val = of_get_address(ofnode_to_np(node), index, &size, |
| 205 | &flags); |
Simon Glass | 049ae1b | 2017-05-18 20:09:01 -0600 | [diff] [blame] | 206 | if (!prop_val) |
| 207 | return FDT_ADDR_T_NONE; |
Simon Glass | 76a6666 | 2017-07-25 08:29:56 -0600 | [diff] [blame] | 208 | na = of_n_addr_cells(ofnode_to_np(node)); |
| 209 | return of_read_number(prop_val, na); |
Simon Glass | 049ae1b | 2017-05-18 20:09:01 -0600 | [diff] [blame] | 210 | } else { |
| 211 | return fdt_get_base_address(gd->fdt_blob, |
| 212 | ofnode_to_offset(node)); |
| 213 | } |
| 214 | |
| 215 | return FDT_ADDR_T_NONE; |
| 216 | } |
| 217 | |
| 218 | fdt_addr_t ofnode_get_addr(ofnode node) |
| 219 | { |
| 220 | return ofnode_get_addr_index(node, 0); |
| 221 | } |
| 222 | |
Simon Glass | c4fc562 | 2017-05-18 20:08:58 -0600 | [diff] [blame] | 223 | int ofnode_stringlist_search(ofnode node, const char *property, |
| 224 | const char *string) |
| 225 | { |
| 226 | if (ofnode_is_np(node)) { |
| 227 | return of_property_match_string(ofnode_to_np(node), |
| 228 | property, string); |
| 229 | } else { |
| 230 | int ret; |
| 231 | |
| 232 | ret = fdt_stringlist_search(gd->fdt_blob, |
| 233 | ofnode_to_offset(node), property, |
| 234 | string); |
| 235 | if (ret == -FDT_ERR_NOTFOUND) |
| 236 | return -ENODATA; |
| 237 | else if (ret < 0) |
| 238 | return -EINVAL; |
| 239 | |
| 240 | return ret; |
| 241 | } |
| 242 | } |
| 243 | |
| 244 | int ofnode_read_string_index(ofnode node, const char *property, int index, |
| 245 | const char **outp) |
| 246 | { |
| 247 | if (ofnode_is_np(node)) { |
| 248 | return of_property_read_string_index(ofnode_to_np(node), |
| 249 | property, index, outp); |
| 250 | } else { |
| 251 | int len; |
| 252 | |
| 253 | *outp = fdt_stringlist_get(gd->fdt_blob, ofnode_to_offset(node), |
| 254 | property, index, &len); |
| 255 | if (len < 0) |
| 256 | return -EINVAL; |
| 257 | return 0; |
| 258 | } |
| 259 | } |
| 260 | |
Simon Glass | 5fdb005 | 2017-06-12 06:21:28 -0600 | [diff] [blame] | 261 | int ofnode_read_string_count(ofnode node, const char *property) |
| 262 | { |
| 263 | if (ofnode_is_np(node)) { |
| 264 | return of_property_count_strings(ofnode_to_np(node), property); |
| 265 | } else { |
| 266 | return fdt_stringlist_count(gd->fdt_blob, |
| 267 | ofnode_to_offset(node), property); |
| 268 | } |
| 269 | } |
| 270 | |
Simon Glass | c4fc562 | 2017-05-18 20:08:58 -0600 | [diff] [blame] | 271 | static void ofnode_from_fdtdec_phandle_args(struct fdtdec_phandle_args *in, |
| 272 | struct ofnode_phandle_args *out) |
| 273 | { |
| 274 | assert(OF_MAX_PHANDLE_ARGS == MAX_PHANDLE_ARGS); |
| 275 | out->node = offset_to_ofnode(in->node); |
| 276 | out->args_count = in->args_count; |
| 277 | memcpy(out->args, in->args, sizeof(out->args)); |
| 278 | } |
| 279 | |
| 280 | static void ofnode_from_of_phandle_args(struct of_phandle_args *in, |
| 281 | struct ofnode_phandle_args *out) |
| 282 | { |
| 283 | assert(OF_MAX_PHANDLE_ARGS == MAX_PHANDLE_ARGS); |
| 284 | out->node = np_to_ofnode(in->np); |
| 285 | out->args_count = in->args_count; |
| 286 | memcpy(out->args, in->args, sizeof(out->args)); |
| 287 | } |
| 288 | |
| 289 | int ofnode_parse_phandle_with_args(ofnode node, const char *list_name, |
| 290 | const char *cells_name, int cell_count, |
| 291 | int index, |
| 292 | struct ofnode_phandle_args *out_args) |
| 293 | { |
| 294 | if (ofnode_is_np(node)) { |
| 295 | struct of_phandle_args args; |
| 296 | int ret; |
| 297 | |
| 298 | ret = of_parse_phandle_with_args(ofnode_to_np(node), |
| 299 | list_name, cells_name, index, &args); |
| 300 | if (ret) |
| 301 | return ret; |
| 302 | ofnode_from_of_phandle_args(&args, out_args); |
| 303 | } else { |
| 304 | struct fdtdec_phandle_args args; |
| 305 | int ret; |
| 306 | |
| 307 | ret = fdtdec_parse_phandle_with_args(gd->fdt_blob, |
| 308 | ofnode_to_offset(node), list_name, cells_name, |
| 309 | cell_count, index, &args); |
| 310 | if (ret) |
| 311 | return ret; |
| 312 | ofnode_from_fdtdec_phandle_args(&args, out_args); |
| 313 | } |
| 314 | |
| 315 | return 0; |
| 316 | } |
| 317 | |
Patrice Chotard | be7dd60 | 2017-07-18 11:57:08 +0200 | [diff] [blame] | 318 | int ofnode_count_phandle_with_args(ofnode node, const char *list_name, |
| 319 | const char *cells_name) |
| 320 | { |
| 321 | if (ofnode_is_np(node)) |
| 322 | return of_count_phandle_with_args(ofnode_to_np(node), |
| 323 | list_name, cells_name); |
| 324 | else |
| 325 | return fdtdec_parse_phandle_with_args(gd->fdt_blob, |
| 326 | ofnode_to_offset(node), list_name, cells_name, |
| 327 | 0, -1, NULL); |
| 328 | } |
| 329 | |
Simon Glass | c4fc562 | 2017-05-18 20:08:58 -0600 | [diff] [blame] | 330 | ofnode ofnode_path(const char *path) |
| 331 | { |
| 332 | if (of_live_active()) |
| 333 | return np_to_ofnode(of_find_node_by_path(path)); |
| 334 | else |
| 335 | return offset_to_ofnode(fdt_path_offset(gd->fdt_blob, path)); |
| 336 | } |
| 337 | |
| 338 | const char *ofnode_get_chosen_prop(const char *name) |
| 339 | { |
| 340 | ofnode chosen_node; |
| 341 | |
| 342 | chosen_node = ofnode_path("/chosen"); |
| 343 | |
| 344 | return ofnode_read_string(chosen_node, name); |
| 345 | } |
| 346 | |
| 347 | ofnode ofnode_get_chosen_node(const char *name) |
| 348 | { |
| 349 | const char *prop; |
| 350 | |
| 351 | prop = ofnode_get_chosen_prop(name); |
| 352 | if (!prop) |
| 353 | return ofnode_null(); |
| 354 | |
| 355 | return ofnode_path(prop); |
| 356 | } |
| 357 | |
| 358 | static int decode_timing_property(ofnode node, const char *name, |
| 359 | struct timing_entry *result) |
| 360 | { |
| 361 | int length, ret = 0; |
| 362 | |
| 363 | length = ofnode_read_size(node, name); |
| 364 | if (length < 0) { |
| 365 | debug("%s: could not find property %s\n", |
| 366 | ofnode_get_name(node), name); |
| 367 | return length; |
| 368 | } |
| 369 | |
| 370 | if (length == sizeof(u32)) { |
| 371 | result->typ = ofnode_read_u32_default(node, name, 0); |
| 372 | result->min = result->typ; |
| 373 | result->max = result->typ; |
| 374 | } else { |
| 375 | ret = ofnode_read_u32_array(node, name, &result->min, 3); |
| 376 | } |
| 377 | |
| 378 | return ret; |
| 379 | } |
| 380 | |
| 381 | int ofnode_decode_display_timing(ofnode parent, int index, |
| 382 | struct display_timing *dt) |
| 383 | { |
| 384 | int i; |
| 385 | ofnode timings, node; |
| 386 | u32 val = 0; |
| 387 | int ret = 0; |
| 388 | |
| 389 | timings = ofnode_find_subnode(parent, "display-timings"); |
| 390 | if (!ofnode_valid(timings)) |
| 391 | return -EINVAL; |
| 392 | |
Simon Glass | 2852976 | 2017-08-05 15:45:54 -0600 | [diff] [blame] | 393 | i = 0; |
| 394 | ofnode_for_each_subnode(node, timings) { |
| 395 | if (i++ == index) |
| 396 | break; |
| 397 | } |
Simon Glass | c4fc562 | 2017-05-18 20:08:58 -0600 | [diff] [blame] | 398 | |
| 399 | if (!ofnode_valid(node)) |
| 400 | return -EINVAL; |
| 401 | |
| 402 | memset(dt, 0, sizeof(*dt)); |
| 403 | |
| 404 | ret |= decode_timing_property(node, "hback-porch", &dt->hback_porch); |
| 405 | ret |= decode_timing_property(node, "hfront-porch", &dt->hfront_porch); |
| 406 | ret |= decode_timing_property(node, "hactive", &dt->hactive); |
| 407 | ret |= decode_timing_property(node, "hsync-len", &dt->hsync_len); |
| 408 | ret |= decode_timing_property(node, "vback-porch", &dt->vback_porch); |
| 409 | ret |= decode_timing_property(node, "vfront-porch", &dt->vfront_porch); |
| 410 | ret |= decode_timing_property(node, "vactive", &dt->vactive); |
| 411 | ret |= decode_timing_property(node, "vsync-len", &dt->vsync_len); |
| 412 | ret |= decode_timing_property(node, "clock-frequency", &dt->pixelclock); |
| 413 | |
| 414 | dt->flags = 0; |
| 415 | val = ofnode_read_u32_default(node, "vsync-active", -1); |
| 416 | if (val != -1) { |
| 417 | dt->flags |= val ? DISPLAY_FLAGS_VSYNC_HIGH : |
| 418 | DISPLAY_FLAGS_VSYNC_LOW; |
| 419 | } |
| 420 | val = ofnode_read_u32_default(node, "hsync-active", -1); |
| 421 | if (val != -1) { |
| 422 | dt->flags |= val ? DISPLAY_FLAGS_HSYNC_HIGH : |
| 423 | DISPLAY_FLAGS_HSYNC_LOW; |
| 424 | } |
| 425 | val = ofnode_read_u32_default(node, "de-active", -1); |
| 426 | if (val != -1) { |
| 427 | dt->flags |= val ? DISPLAY_FLAGS_DE_HIGH : |
| 428 | DISPLAY_FLAGS_DE_LOW; |
| 429 | } |
| 430 | val = ofnode_read_u32_default(node, "pixelclk-active", -1); |
| 431 | if (val != -1) { |
| 432 | dt->flags |= val ? DISPLAY_FLAGS_PIXDATA_POSEDGE : |
| 433 | DISPLAY_FLAGS_PIXDATA_NEGEDGE; |
| 434 | } |
| 435 | |
| 436 | if (ofnode_read_bool(node, "interlaced")) |
| 437 | dt->flags |= DISPLAY_FLAGS_INTERLACED; |
| 438 | if (ofnode_read_bool(node, "doublescan")) |
| 439 | dt->flags |= DISPLAY_FLAGS_DOUBLESCAN; |
| 440 | if (ofnode_read_bool(node, "doubleclk")) |
| 441 | dt->flags |= DISPLAY_FLAGS_DOUBLECLK; |
| 442 | |
| 443 | return ret; |
| 444 | } |
| 445 | |
Masahiro Yamada | 9cf85cb | 2017-06-22 16:54:05 +0900 | [diff] [blame] | 446 | const void *ofnode_get_property(ofnode node, const char *propname, int *lenp) |
Simon Glass | c4fc562 | 2017-05-18 20:08:58 -0600 | [diff] [blame] | 447 | { |
Masahiro Yamada | 5052f1b | 2017-06-22 16:54:04 +0900 | [diff] [blame] | 448 | if (ofnode_is_np(node)) |
| 449 | return of_get_property(ofnode_to_np(node), propname, lenp); |
| 450 | else |
Simon Glass | c4fc562 | 2017-05-18 20:08:58 -0600 | [diff] [blame] | 451 | return fdt_getprop(gd->fdt_blob, ofnode_to_offset(node), |
| 452 | propname, lenp); |
Simon Glass | c4fc562 | 2017-05-18 20:08:58 -0600 | [diff] [blame] | 453 | } |
| 454 | |
| 455 | bool ofnode_is_available(ofnode node) |
| 456 | { |
| 457 | if (ofnode_is_np(node)) |
| 458 | return of_device_is_available(ofnode_to_np(node)); |
| 459 | else |
| 460 | return fdtdec_get_is_enabled(gd->fdt_blob, |
| 461 | ofnode_to_offset(node)); |
| 462 | } |
| 463 | |
| 464 | fdt_addr_t ofnode_get_addr_size(ofnode node, const char *property, |
| 465 | fdt_size_t *sizep) |
| 466 | { |
| 467 | if (ofnode_is_np(node)) { |
| 468 | int na, ns; |
| 469 | int psize; |
| 470 | const struct device_node *np = ofnode_to_np(node); |
Klaus Goger | af4b021 | 2017-09-20 13:50:41 +0200 | [diff] [blame^] | 471 | const __be32 *prop = of_get_property(np, property, &psize); |
Simon Glass | c4fc562 | 2017-05-18 20:08:58 -0600 | [diff] [blame] | 472 | |
Klaus Goger | af4b021 | 2017-09-20 13:50:41 +0200 | [diff] [blame^] | 473 | if (!prop) |
| 474 | return FDT_ADDR_T_NONE; |
Simon Glass | c4fc562 | 2017-05-18 20:08:58 -0600 | [diff] [blame] | 475 | na = of_n_addr_cells(np); |
| 476 | ns = of_n_addr_cells(np); |
Simon Glass | a67cc63 | 2017-05-18 20:09:27 -0600 | [diff] [blame] | 477 | *sizep = of_read_number(prop + na, ns); |
| 478 | return of_read_number(prop, na); |
Simon Glass | c4fc562 | 2017-05-18 20:08:58 -0600 | [diff] [blame] | 479 | } else { |
| 480 | return fdtdec_get_addr_size(gd->fdt_blob, |
| 481 | ofnode_to_offset(node), property, |
| 482 | sizep); |
| 483 | } |
| 484 | } |
| 485 | |
| 486 | const uint8_t *ofnode_read_u8_array_ptr(ofnode node, const char *propname, |
| 487 | size_t sz) |
| 488 | { |
| 489 | if (ofnode_is_np(node)) { |
| 490 | const struct device_node *np = ofnode_to_np(node); |
| 491 | int psize; |
| 492 | const __be32 *prop = of_get_property(np, propname, &psize); |
| 493 | |
| 494 | if (!prop || sz != psize) |
| 495 | return NULL; |
| 496 | return (uint8_t *)prop; |
| 497 | |
| 498 | } else { |
| 499 | return fdtdec_locate_byte_array(gd->fdt_blob, |
| 500 | ofnode_to_offset(node), propname, sz); |
| 501 | } |
| 502 | } |
| 503 | |
| 504 | int ofnode_read_pci_addr(ofnode node, enum fdt_pci_space type, |
| 505 | const char *propname, struct fdt_pci_addr *addr) |
| 506 | { |
Masahiro Yamada | 5c5991e | 2017-06-22 17:57:50 +0900 | [diff] [blame] | 507 | const fdt32_t *cell; |
Simon Glass | c4fc562 | 2017-05-18 20:08:58 -0600 | [diff] [blame] | 508 | int len; |
| 509 | int ret = -ENOENT; |
| 510 | |
| 511 | debug("%s: %s: ", __func__, propname); |
| 512 | |
| 513 | /* |
| 514 | * If we follow the pci bus bindings strictly, we should check |
| 515 | * the value of the node's parent node's #address-cells and |
| 516 | * #size-cells. They need to be 3 and 2 accordingly. However, |
| 517 | * for simplicity we skip the check here. |
| 518 | */ |
Masahiro Yamada | 9cf85cb | 2017-06-22 16:54:05 +0900 | [diff] [blame] | 519 | cell = ofnode_get_property(node, propname, &len); |
Simon Glass | c4fc562 | 2017-05-18 20:08:58 -0600 | [diff] [blame] | 520 | if (!cell) |
| 521 | goto fail; |
| 522 | |
| 523 | if ((len % FDT_PCI_REG_SIZE) == 0) { |
| 524 | int num = len / FDT_PCI_REG_SIZE; |
| 525 | int i; |
| 526 | |
| 527 | for (i = 0; i < num; i++) { |
| 528 | debug("pci address #%d: %08lx %08lx %08lx\n", i, |
| 529 | (ulong)fdt32_to_cpu(cell[0]), |
| 530 | (ulong)fdt32_to_cpu(cell[1]), |
| 531 | (ulong)fdt32_to_cpu(cell[2])); |
| 532 | if ((fdt32_to_cpu(*cell) & type) == type) { |
| 533 | addr->phys_hi = fdt32_to_cpu(cell[0]); |
| 534 | addr->phys_mid = fdt32_to_cpu(cell[1]); |
| 535 | addr->phys_lo = fdt32_to_cpu(cell[1]); |
| 536 | break; |
| 537 | } else { |
| 538 | cell += (FDT_PCI_ADDR_CELLS + |
| 539 | FDT_PCI_SIZE_CELLS); |
| 540 | } |
| 541 | } |
| 542 | |
| 543 | if (i == num) { |
| 544 | ret = -ENXIO; |
| 545 | goto fail; |
| 546 | } |
| 547 | |
| 548 | return 0; |
| 549 | } else { |
| 550 | ret = -EINVAL; |
| 551 | } |
| 552 | |
| 553 | fail: |
| 554 | debug("(not found)\n"); |
| 555 | return ret; |
| 556 | } |
| 557 | |
| 558 | int ofnode_read_addr_cells(ofnode node) |
| 559 | { |
| 560 | if (ofnode_is_np(node)) |
| 561 | return of_n_addr_cells(ofnode_to_np(node)); |
Simon Glass | 4191dc1 | 2017-06-12 06:21:31 -0600 | [diff] [blame] | 562 | else /* NOTE: this call should walk up the parent stack */ |
Simon Glass | c4fc562 | 2017-05-18 20:08:58 -0600 | [diff] [blame] | 563 | return fdt_address_cells(gd->fdt_blob, ofnode_to_offset(node)); |
| 564 | } |
| 565 | |
| 566 | int ofnode_read_size_cells(ofnode node) |
| 567 | { |
| 568 | if (ofnode_is_np(node)) |
| 569 | return of_n_size_cells(ofnode_to_np(node)); |
Simon Glass | 4191dc1 | 2017-06-12 06:21:31 -0600 | [diff] [blame] | 570 | else /* NOTE: this call should walk up the parent stack */ |
| 571 | return fdt_size_cells(gd->fdt_blob, ofnode_to_offset(node)); |
| 572 | } |
| 573 | |
| 574 | int ofnode_read_simple_addr_cells(ofnode node) |
| 575 | { |
| 576 | if (ofnode_is_np(node)) |
| 577 | return of_simple_addr_cells(ofnode_to_np(node)); |
| 578 | else |
| 579 | return fdt_address_cells(gd->fdt_blob, ofnode_to_offset(node)); |
| 580 | } |
| 581 | |
| 582 | int ofnode_read_simple_size_cells(ofnode node) |
| 583 | { |
| 584 | if (ofnode_is_np(node)) |
| 585 | return of_simple_size_cells(ofnode_to_np(node)); |
Simon Glass | c4fc562 | 2017-05-18 20:08:58 -0600 | [diff] [blame] | 586 | else |
| 587 | return fdt_size_cells(gd->fdt_blob, ofnode_to_offset(node)); |
| 588 | } |
| 589 | |
| 590 | bool ofnode_pre_reloc(ofnode node) |
| 591 | { |
Masahiro Yamada | 6a61dd9 | 2017-06-22 16:54:03 +0900 | [diff] [blame] | 592 | if (ofnode_read_bool(node, "u-boot,dm-pre-reloc")) |
Simon Glass | c4fc562 | 2017-05-18 20:08:58 -0600 | [diff] [blame] | 593 | return true; |
| 594 | |
| 595 | #ifdef CONFIG_TPL_BUILD |
Masahiro Yamada | 6a61dd9 | 2017-06-22 16:54:03 +0900 | [diff] [blame] | 596 | if (ofnode_read_bool(node, "u-boot,dm-tpl")) |
Simon Glass | c4fc562 | 2017-05-18 20:08:58 -0600 | [diff] [blame] | 597 | return true; |
| 598 | #elif defined(CONFIG_SPL_BUILD) |
Masahiro Yamada | 6a61dd9 | 2017-06-22 16:54:03 +0900 | [diff] [blame] | 599 | if (ofnode_read_bool(node, "u-boot,dm-spl")) |
Simon Glass | c4fc562 | 2017-05-18 20:08:58 -0600 | [diff] [blame] | 600 | return true; |
| 601 | #else |
| 602 | /* |
| 603 | * In regular builds individual spl and tpl handling both |
| 604 | * count as handled pre-relocation for later second init. |
| 605 | */ |
Masahiro Yamada | 6a61dd9 | 2017-06-22 16:54:03 +0900 | [diff] [blame] | 606 | if (ofnode_read_bool(node, "u-boot,dm-spl") || |
| 607 | ofnode_read_bool(node, "u-boot,dm-tpl")) |
Simon Glass | c4fc562 | 2017-05-18 20:08:58 -0600 | [diff] [blame] | 608 | return true; |
| 609 | #endif |
| 610 | |
| 611 | return false; |
| 612 | } |
Simon Glass | f7bfcc4 | 2017-07-25 08:29:55 -0600 | [diff] [blame] | 613 | |
| 614 | int ofnode_read_resource(ofnode node, uint index, struct resource *res) |
| 615 | { |
| 616 | if (ofnode_is_np(node)) { |
| 617 | return of_address_to_resource(ofnode_to_np(node), index, res); |
| 618 | } else { |
| 619 | struct fdt_resource fres; |
| 620 | int ret; |
| 621 | |
| 622 | ret = fdt_get_resource(gd->fdt_blob, ofnode_to_offset(node), |
| 623 | "reg", index, &fres); |
| 624 | if (ret < 0) |
| 625 | return -EINVAL; |
| 626 | memset(res, '\0', sizeof(*res)); |
| 627 | res->start = fres.start; |
| 628 | res->end = fres.end; |
| 629 | |
| 630 | return 0; |
| 631 | } |
| 632 | } |
Masahiro Yamada | 4dada2c | 2017-08-26 01:12:30 +0900 | [diff] [blame] | 633 | |
| 634 | int ofnode_read_resource_byname(ofnode node, const char *name, |
| 635 | struct resource *res) |
| 636 | { |
| 637 | int index; |
| 638 | |
| 639 | index = ofnode_stringlist_search(node, "reg-names", name); |
| 640 | if (index < 0) |
| 641 | return index; |
| 642 | |
| 643 | return ofnode_read_resource(node, index, res); |
| 644 | } |