ofnode.c 13 KB

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