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