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