ofnode.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579
  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. #include <common.h>
  8. #include <dm.h>
  9. #include <fdtdec.h>
  10. #include <fdt_support.h>
  11. #include <libfdt.h>
  12. #include <dm/of_access.h>
  13. #include <dm/of_addr.h>
  14. #include <dm/ofnode.h>
  15. #include <linux/err.h>
  16. int ofnode_read_u32(ofnode node, const char *propname, u32 *outp)
  17. {
  18. assert(ofnode_valid(node));
  19. debug("%s: %s: ", __func__, propname);
  20. if (ofnode_is_np(node)) {
  21. return of_read_u32(ofnode_to_np(node), propname, outp);
  22. } else {
  23. const int *cell;
  24. int len;
  25. cell = fdt_getprop(gd->fdt_blob, ofnode_to_offset(node),
  26. propname, &len);
  27. if (!cell || len < sizeof(int)) {
  28. debug("(not found)\n");
  29. return -EINVAL;
  30. }
  31. *outp = fdt32_to_cpu(cell[0]);
  32. }
  33. debug("%#x (%d)\n", *outp, *outp);
  34. return 0;
  35. }
  36. int ofnode_read_u32_default(ofnode node, const char *propname, u32 def)
  37. {
  38. assert(ofnode_valid(node));
  39. ofnode_read_u32(node, propname, &def);
  40. return def;
  41. }
  42. int ofnode_read_s32_default(ofnode node, const char *propname, s32 def)
  43. {
  44. assert(ofnode_valid(node));
  45. ofnode_read_u32(node, propname, (u32 *)&def);
  46. return def;
  47. }
  48. bool ofnode_read_bool(ofnode node, const char *propname)
  49. {
  50. bool val;
  51. assert(ofnode_valid(node));
  52. debug("%s: %s: ", __func__, propname);
  53. if (ofnode_is_np(node)) {
  54. val = !!of_find_property(ofnode_to_np(node), propname, NULL);
  55. } else {
  56. val = !!fdt_getprop(gd->fdt_blob, ofnode_to_offset(node),
  57. propname, NULL);
  58. }
  59. debug("%s\n", val ? "true" : "false");
  60. return val;
  61. }
  62. const char *ofnode_read_string(ofnode node, const char *propname)
  63. {
  64. const char *str = NULL;
  65. int len = -1;
  66. assert(ofnode_valid(node));
  67. debug("%s: %s: ", __func__, propname);
  68. if (ofnode_is_np(node)) {
  69. struct property *prop = of_find_property(
  70. ofnode_to_np(node), propname, NULL);
  71. if (prop) {
  72. str = prop->value;
  73. len = prop->length;
  74. }
  75. } else {
  76. str = fdt_getprop(gd->fdt_blob, ofnode_to_offset(node),
  77. propname, &len);
  78. }
  79. if (!str) {
  80. debug("<not found>\n");
  81. return NULL;
  82. }
  83. if (strnlen(str, len) >= len) {
  84. debug("<invalid>\n");
  85. return NULL;
  86. }
  87. debug("%s\n", str);
  88. return str;
  89. }
  90. ofnode ofnode_find_subnode(ofnode node, const char *subnode_name)
  91. {
  92. ofnode subnode;
  93. assert(ofnode_valid(node));
  94. debug("%s: %s: ", __func__, subnode_name);
  95. if (ofnode_is_np(node)) {
  96. const struct device_node *np = ofnode_to_np(node);
  97. for (np = np->child; np; np = np->sibling) {
  98. if (!strcmp(subnode_name, np->name))
  99. break;
  100. }
  101. subnode = np_to_ofnode(np);
  102. } else {
  103. int ooffset = fdt_subnode_offset(gd->fdt_blob,
  104. ofnode_to_offset(node), subnode_name);
  105. subnode = offset_to_ofnode(ooffset);
  106. }
  107. debug("%s\n", ofnode_valid(subnode) ?
  108. ofnode_get_name(subnode) : "<none>");
  109. return subnode;
  110. }
  111. int ofnode_read_u32_array(ofnode node, const char *propname,
  112. u32 *out_values, size_t sz)
  113. {
  114. assert(ofnode_valid(node));
  115. debug("%s: %s: ", __func__, propname);
  116. if (ofnode_is_np(node)) {
  117. return of_read_u32_array(ofnode_to_np(node), propname,
  118. out_values, sz);
  119. } else {
  120. return fdtdec_get_int_array(gd->fdt_blob,
  121. ofnode_to_offset(node), propname,
  122. out_values, sz);
  123. }
  124. }
  125. ofnode ofnode_first_subnode(ofnode node)
  126. {
  127. assert(ofnode_valid(node));
  128. if (ofnode_is_np(node))
  129. return np_to_ofnode(node.np->child);
  130. return offset_to_ofnode(
  131. fdt_first_subnode(gd->fdt_blob, ofnode_to_offset(node)));
  132. }
  133. ofnode ofnode_next_subnode(ofnode node)
  134. {
  135. assert(ofnode_valid(node));
  136. if (ofnode_is_np(node))
  137. return np_to_ofnode(node.np->sibling);
  138. return offset_to_ofnode(
  139. fdt_next_subnode(gd->fdt_blob, ofnode_to_offset(node)));
  140. }
  141. const char *ofnode_get_name(ofnode node)
  142. {
  143. assert(ofnode_valid(node));
  144. if (ofnode_is_np(node))
  145. return strrchr(node.np->full_name, '/') + 1;
  146. return fdt_get_name(gd->fdt_blob, ofnode_to_offset(node), NULL);
  147. }
  148. int ofnode_read_size(ofnode node, const char *propname)
  149. {
  150. int len;
  151. if (ofnode_is_np(node)) {
  152. struct property *prop = of_find_property(
  153. ofnode_to_np(node), propname, NULL);
  154. if (prop)
  155. return prop->length;
  156. } else {
  157. if (fdt_getprop(gd->fdt_blob, ofnode_to_offset(node), propname,
  158. &len))
  159. return len;
  160. }
  161. return -EINVAL;
  162. }
  163. fdt_addr_t ofnode_get_addr_index(ofnode node, int index)
  164. {
  165. if (ofnode_is_np(node)) {
  166. const __be32 *prop_val;
  167. uint flags;
  168. u64 size;
  169. prop_val = of_get_address(
  170. (struct device_node *)ofnode_to_np(node), index,
  171. &size, &flags);
  172. if (!prop_val)
  173. return FDT_ADDR_T_NONE;
  174. return be32_to_cpup(prop_val);
  175. } else {
  176. return fdt_get_base_address(gd->fdt_blob,
  177. ofnode_to_offset(node));
  178. }
  179. return FDT_ADDR_T_NONE;
  180. }
  181. fdt_addr_t ofnode_get_addr(ofnode node)
  182. {
  183. return ofnode_get_addr_index(node, 0);
  184. }
  185. int ofnode_stringlist_search(ofnode node, const char *property,
  186. const char *string)
  187. {
  188. if (ofnode_is_np(node)) {
  189. return of_property_match_string(ofnode_to_np(node),
  190. property, string);
  191. } else {
  192. int ret;
  193. ret = fdt_stringlist_search(gd->fdt_blob,
  194. ofnode_to_offset(node), property,
  195. string);
  196. if (ret == -FDT_ERR_NOTFOUND)
  197. return -ENODATA;
  198. else if (ret < 0)
  199. return -EINVAL;
  200. return ret;
  201. }
  202. }
  203. int ofnode_read_string_index(ofnode node, const char *property, int index,
  204. const char **outp)
  205. {
  206. if (ofnode_is_np(node)) {
  207. return of_property_read_string_index(ofnode_to_np(node),
  208. property, index, outp);
  209. } else {
  210. int len;
  211. *outp = fdt_stringlist_get(gd->fdt_blob, ofnode_to_offset(node),
  212. property, index, &len);
  213. if (len < 0)
  214. return -EINVAL;
  215. return 0;
  216. }
  217. }
  218. static void ofnode_from_fdtdec_phandle_args(struct fdtdec_phandle_args *in,
  219. struct ofnode_phandle_args *out)
  220. {
  221. assert(OF_MAX_PHANDLE_ARGS == MAX_PHANDLE_ARGS);
  222. out->node = offset_to_ofnode(in->node);
  223. out->args_count = in->args_count;
  224. memcpy(out->args, in->args, sizeof(out->args));
  225. }
  226. static void ofnode_from_of_phandle_args(struct of_phandle_args *in,
  227. struct ofnode_phandle_args *out)
  228. {
  229. assert(OF_MAX_PHANDLE_ARGS == MAX_PHANDLE_ARGS);
  230. out->node = np_to_ofnode(in->np);
  231. out->args_count = in->args_count;
  232. memcpy(out->args, in->args, sizeof(out->args));
  233. }
  234. int ofnode_parse_phandle_with_args(ofnode node, const char *list_name,
  235. const char *cells_name, int cell_count,
  236. int index,
  237. struct ofnode_phandle_args *out_args)
  238. {
  239. if (ofnode_is_np(node)) {
  240. struct of_phandle_args args;
  241. int ret;
  242. ret = of_parse_phandle_with_args(ofnode_to_np(node),
  243. list_name, cells_name, index, &args);
  244. if (ret)
  245. return ret;
  246. ofnode_from_of_phandle_args(&args, out_args);
  247. } else {
  248. struct fdtdec_phandle_args args;
  249. int ret;
  250. ret = fdtdec_parse_phandle_with_args(gd->fdt_blob,
  251. ofnode_to_offset(node), list_name, cells_name,
  252. cell_count, index, &args);
  253. if (ret)
  254. return ret;
  255. ofnode_from_fdtdec_phandle_args(&args, out_args);
  256. }
  257. return 0;
  258. }
  259. ofnode ofnode_path(const char *path)
  260. {
  261. if (of_live_active())
  262. return np_to_ofnode(of_find_node_by_path(path));
  263. else
  264. return offset_to_ofnode(fdt_path_offset(gd->fdt_blob, path));
  265. }
  266. const char *ofnode_get_chosen_prop(const char *name)
  267. {
  268. ofnode chosen_node;
  269. chosen_node = ofnode_path("/chosen");
  270. return ofnode_read_string(chosen_node, name);
  271. }
  272. ofnode ofnode_get_chosen_node(const char *name)
  273. {
  274. const char *prop;
  275. prop = ofnode_get_chosen_prop(name);
  276. if (!prop)
  277. return ofnode_null();
  278. return ofnode_path(prop);
  279. }
  280. static int decode_timing_property(ofnode node, const char *name,
  281. struct timing_entry *result)
  282. {
  283. int length, ret = 0;
  284. length = ofnode_read_size(node, name);
  285. if (length < 0) {
  286. debug("%s: could not find property %s\n",
  287. ofnode_get_name(node), name);
  288. return length;
  289. }
  290. if (length == sizeof(u32)) {
  291. result->typ = ofnode_read_u32_default(node, name, 0);
  292. result->min = result->typ;
  293. result->max = result->typ;
  294. } else {
  295. ret = ofnode_read_u32_array(node, name, &result->min, 3);
  296. }
  297. return ret;
  298. }
  299. int ofnode_decode_display_timing(ofnode parent, int index,
  300. struct display_timing *dt)
  301. {
  302. int i;
  303. ofnode timings, node;
  304. u32 val = 0;
  305. int ret = 0;
  306. timings = ofnode_find_subnode(parent, "display-timings");
  307. if (!ofnode_valid(timings))
  308. return -EINVAL;
  309. for (i = 0, node = ofnode_first_subnode(timings);
  310. ofnode_valid(node) && i != index;
  311. node = ofnode_first_subnode(node))
  312. i++;
  313. if (!ofnode_valid(node))
  314. return -EINVAL;
  315. memset(dt, 0, sizeof(*dt));
  316. ret |= decode_timing_property(node, "hback-porch", &dt->hback_porch);
  317. ret |= decode_timing_property(node, "hfront-porch", &dt->hfront_porch);
  318. ret |= decode_timing_property(node, "hactive", &dt->hactive);
  319. ret |= decode_timing_property(node, "hsync-len", &dt->hsync_len);
  320. ret |= decode_timing_property(node, "vback-porch", &dt->vback_porch);
  321. ret |= decode_timing_property(node, "vfront-porch", &dt->vfront_porch);
  322. ret |= decode_timing_property(node, "vactive", &dt->vactive);
  323. ret |= decode_timing_property(node, "vsync-len", &dt->vsync_len);
  324. ret |= decode_timing_property(node, "clock-frequency", &dt->pixelclock);
  325. dt->flags = 0;
  326. val = ofnode_read_u32_default(node, "vsync-active", -1);
  327. if (val != -1) {
  328. dt->flags |= val ? DISPLAY_FLAGS_VSYNC_HIGH :
  329. DISPLAY_FLAGS_VSYNC_LOW;
  330. }
  331. val = ofnode_read_u32_default(node, "hsync-active", -1);
  332. if (val != -1) {
  333. dt->flags |= val ? DISPLAY_FLAGS_HSYNC_HIGH :
  334. DISPLAY_FLAGS_HSYNC_LOW;
  335. }
  336. val = ofnode_read_u32_default(node, "de-active", -1);
  337. if (val != -1) {
  338. dt->flags |= val ? DISPLAY_FLAGS_DE_HIGH :
  339. DISPLAY_FLAGS_DE_LOW;
  340. }
  341. val = ofnode_read_u32_default(node, "pixelclk-active", -1);
  342. if (val != -1) {
  343. dt->flags |= val ? DISPLAY_FLAGS_PIXDATA_POSEDGE :
  344. DISPLAY_FLAGS_PIXDATA_NEGEDGE;
  345. }
  346. if (ofnode_read_bool(node, "interlaced"))
  347. dt->flags |= DISPLAY_FLAGS_INTERLACED;
  348. if (ofnode_read_bool(node, "doublescan"))
  349. dt->flags |= DISPLAY_FLAGS_DOUBLESCAN;
  350. if (ofnode_read_bool(node, "doubleclk"))
  351. dt->flags |= DISPLAY_FLAGS_DOUBLECLK;
  352. return ret;
  353. }
  354. const u32 *ofnode_read_prop(ofnode node, const char *propname, int *lenp)
  355. {
  356. if (ofnode_is_np(node)) {
  357. struct property *prop;
  358. prop = of_find_property(ofnode_to_np(node), propname, lenp);
  359. if (!prop)
  360. return NULL;
  361. return prop->value;
  362. } else {
  363. return fdt_getprop(gd->fdt_blob, ofnode_to_offset(node),
  364. propname, lenp);
  365. }
  366. }
  367. bool ofnode_is_available(ofnode node)
  368. {
  369. if (ofnode_is_np(node))
  370. return of_device_is_available(ofnode_to_np(node));
  371. else
  372. return fdtdec_get_is_enabled(gd->fdt_blob,
  373. ofnode_to_offset(node));
  374. }
  375. fdt_addr_t ofnode_get_addr_size(ofnode node, const char *property,
  376. fdt_size_t *sizep)
  377. {
  378. if (ofnode_is_np(node)) {
  379. int na, ns;
  380. int psize;
  381. const struct device_node *np = ofnode_to_np(node);
  382. const __be32 *prop = of_get_property(np, "reg", &psize);
  383. na = of_n_addr_cells(np);
  384. ns = of_n_addr_cells(np);
  385. *sizep = of_read_number(prop + na, ns);
  386. return of_read_number(prop, na);
  387. } else {
  388. return fdtdec_get_addr_size(gd->fdt_blob,
  389. ofnode_to_offset(node), property,
  390. sizep);
  391. }
  392. }
  393. const uint8_t *ofnode_read_u8_array_ptr(ofnode node, const char *propname,
  394. size_t sz)
  395. {
  396. if (ofnode_is_np(node)) {
  397. const struct device_node *np = ofnode_to_np(node);
  398. int psize;
  399. const __be32 *prop = of_get_property(np, propname, &psize);
  400. if (!prop || sz != psize)
  401. return NULL;
  402. return (uint8_t *)prop;
  403. } else {
  404. return fdtdec_locate_byte_array(gd->fdt_blob,
  405. ofnode_to_offset(node), propname, sz);
  406. }
  407. }
  408. int ofnode_read_pci_addr(ofnode node, enum fdt_pci_space type,
  409. const char *propname, struct fdt_pci_addr *addr)
  410. {
  411. const u32 *cell;
  412. int len;
  413. int ret = -ENOENT;
  414. debug("%s: %s: ", __func__, propname);
  415. /*
  416. * If we follow the pci bus bindings strictly, we should check
  417. * the value of the node's parent node's #address-cells and
  418. * #size-cells. They need to be 3 and 2 accordingly. However,
  419. * for simplicity we skip the check here.
  420. */
  421. cell = ofnode_read_prop(node, propname, &len);
  422. if (!cell)
  423. goto fail;
  424. if ((len % FDT_PCI_REG_SIZE) == 0) {
  425. int num = len / FDT_PCI_REG_SIZE;
  426. int i;
  427. for (i = 0; i < num; i++) {
  428. debug("pci address #%d: %08lx %08lx %08lx\n", i,
  429. (ulong)fdt32_to_cpu(cell[0]),
  430. (ulong)fdt32_to_cpu(cell[1]),
  431. (ulong)fdt32_to_cpu(cell[2]));
  432. if ((fdt32_to_cpu(*cell) & type) == type) {
  433. addr->phys_hi = fdt32_to_cpu(cell[0]);
  434. addr->phys_mid = fdt32_to_cpu(cell[1]);
  435. addr->phys_lo = fdt32_to_cpu(cell[1]);
  436. break;
  437. } else {
  438. cell += (FDT_PCI_ADDR_CELLS +
  439. FDT_PCI_SIZE_CELLS);
  440. }
  441. }
  442. if (i == num) {
  443. ret = -ENXIO;
  444. goto fail;
  445. }
  446. return 0;
  447. } else {
  448. ret = -EINVAL;
  449. }
  450. fail:
  451. debug("(not found)\n");
  452. return ret;
  453. }
  454. int ofnode_read_addr_cells(ofnode node)
  455. {
  456. if (ofnode_is_np(node))
  457. return of_n_addr_cells(ofnode_to_np(node));
  458. else
  459. return fdt_address_cells(gd->fdt_blob, ofnode_to_offset(node));
  460. }
  461. int ofnode_read_size_cells(ofnode node)
  462. {
  463. if (ofnode_is_np(node))
  464. return of_n_size_cells(ofnode_to_np(node));
  465. else
  466. return fdt_size_cells(gd->fdt_blob, ofnode_to_offset(node));
  467. }
  468. bool ofnode_pre_reloc(ofnode node)
  469. {
  470. if (ofnode_read_prop(node, "u-boot,dm-pre-reloc", NULL))
  471. return true;
  472. #ifdef CONFIG_TPL_BUILD
  473. if (ofnode_read_prop(node, "u-boot,dm-tpl", NULL))
  474. return true;
  475. #elif defined(CONFIG_SPL_BUILD)
  476. if (ofnode_read_prop(node, "u-boot,dm-spl", NULL))
  477. return true;
  478. #else
  479. /*
  480. * In regular builds individual spl and tpl handling both
  481. * count as handled pre-relocation for later second init.
  482. */
  483. if (ofnode_read_prop(node, "u-boot,dm-spl", NULL) ||
  484. ofnode_read_prop(node, "u-boot,dm-tpl", NULL))
  485. return true;
  486. #endif
  487. return false;
  488. }