1
0

ofw.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731
  1. /*
  2. * Copyright (c) 2006-2024, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2022-08-25 GuEe-GUI first version
  9. */
  10. #include <rtthread.h>
  11. #include <rtdevice.h>
  12. #include <drivers/platform.h>
  13. #include <drivers/core/bus.h>
  14. #include <drivers/serial_dm.h>
  15. #define DBG_TAG "rtdm.ofw"
  16. #define DBG_LVL DBG_INFO
  17. #include <rtdbg.h>
  18. #include "ofw_internal.h"
  19. struct rt_ofw_stub *rt_ofw_stub_probe_range(struct rt_ofw_node *np,
  20. const struct rt_ofw_stub *stub_start, const struct rt_ofw_stub *stub_end)
  21. {
  22. const struct rt_ofw_stub *stub = RT_NULL;
  23. if (np && stub_start && stub_end &&
  24. rt_ofw_node_is_available(np) &&
  25. !rt_ofw_node_test_flag(np, RT_OFW_F_READLY) &&
  26. !rt_ofw_node_test_flag(np, RT_OFW_F_SYSTEM))
  27. {
  28. struct rt_ofw_prop *compat_prop = rt_ofw_get_prop(np, "compatible", RT_NULL);
  29. if (compat_prop)
  30. {
  31. rt_ofw_foreach_stub(stub, stub_start, stub_end)
  32. {
  33. struct rt_ofw_node_id *id;
  34. if (!stub->ids)
  35. {
  36. continue;
  37. }
  38. id = rt_ofw_prop_match(compat_prop, stub->ids);
  39. if (id)
  40. {
  41. if (!stub->handler(np, id))
  42. {
  43. rt_ofw_node_set_flag(np, RT_OFW_F_READLY);
  44. }
  45. break;
  46. }
  47. }
  48. }
  49. }
  50. return (struct rt_ofw_stub *)stub;
  51. }
  52. struct ofw_obj_cmp_list
  53. {
  54. const char *cells_name;
  55. const char *obj_name;
  56. rt_size_t obj_size;
  57. };
  58. static const struct ofw_obj_cmp_list ofw_obj_cmp_list[] =
  59. {
  60. #ifdef RT_USING_CLK
  61. { "#clock-cells", RT_CLK_NODE_OBJ_NAME, sizeof(struct rt_clk_node) },
  62. #endif
  63. #ifdef RT_USING_RESET
  64. { "#reset-cells", RT_RESET_CONTROLLER_OBJ_NAME, sizeof(struct rt_reset_controller) },
  65. #endif
  66. { "#power-domain-cells", RT_POWER_DOMAIN_PROXY_OBJ_NAME, sizeof(struct rt_dm_power_domain_proxy) },
  67. { "#power-domain-cells", RT_POWER_DOMAIN_OBJ_NAME, sizeof(struct rt_dm_power_domain) },
  68. };
  69. static struct rt_object *ofw_parse_object(struct rt_ofw_node *np, const char *cells_name)
  70. {
  71. const struct ofw_obj_cmp_list *item;
  72. struct rt_object *obj = rt_ofw_data(np), *ret_obj = RT_NULL;
  73. RT_BITMAP_DECLARE(idx_mask, RT_ARRAY_SIZE(ofw_obj_cmp_list)) = {};
  74. for (int i = 0; i < RT_ARRAY_SIZE(ofw_obj_cmp_list); ++i)
  75. {
  76. item = &ofw_obj_cmp_list[i];
  77. if (!rt_ofw_prop_read_bool(np, item->cells_name))
  78. {
  79. rt_bitmap_set_bit(idx_mask, i);
  80. }
  81. }
  82. while (!ret_obj)
  83. {
  84. int i = 0;
  85. /* Is print ? */
  86. if (!((rt_uint32_t)(obj->name[0] - 0x20) < 0x5f))
  87. {
  88. break;
  89. }
  90. rt_bitmap_for_each_clear_bit(idx_mask, i, RT_ARRAY_SIZE(ofw_obj_cmp_list))
  91. {
  92. item = &ofw_obj_cmp_list[i];
  93. if (!rt_strncmp(item->obj_name, obj->name, RT_NAME_MAX))
  94. {
  95. if (!rt_strcmp(item->cells_name, cells_name))
  96. {
  97. ret_obj = obj;
  98. break;
  99. }
  100. obj = (struct rt_object *)((rt_ubase_t)obj + item->obj_size);
  101. break;
  102. }
  103. }
  104. if (i >= RT_ARRAY_SIZE(ofw_obj_cmp_list))
  105. {
  106. LOG_E("Invalid rt_object = %s", obj->name);
  107. break;
  108. }
  109. }
  110. return ret_obj;
  111. }
  112. struct rt_object *rt_ofw_parse_object(struct rt_ofw_node *np, const char *obj_name, const char *cells_name)
  113. {
  114. struct rt_object *obj = RT_NULL, *test_obj;
  115. if (np && (test_obj = rt_ofw_data(np)) && cells_name)
  116. {
  117. /* The composite object is rare, so we try to find this object as much as possible at once. */
  118. if (obj_name && !rt_strcmp(test_obj->name, obj_name))
  119. {
  120. obj = test_obj;
  121. }
  122. else
  123. {
  124. obj = ofw_parse_object(np, cells_name);
  125. }
  126. }
  127. return obj;
  128. }
  129. static const char *ofw_console_serial_find(char *dst_con, struct rt_ofw_node *np)
  130. {
  131. rt_object_t rt_obj = RT_NULL;
  132. const char *ofw_name = RT_NULL;
  133. struct rt_serial_device *rt_serial = rt_ofw_data(np);
  134. if (rt_serial)
  135. {
  136. rt_obj = &rt_serial->parent.parent;
  137. }
  138. /*
  139. * This is a dangerous behavior because rt_data can be modified by other
  140. * user. But fortunately, we can check if the rt_data is a rt_device or
  141. * rt_serial_device.
  142. */
  143. if (rt_obj && rt_object_get_type(rt_obj) == RT_Object_Class_Device &&
  144. rt_serial->parent.type == RT_Device_Class_Char)
  145. {
  146. ofw_name = np->full_name;
  147. rt_strncpy(dst_con, rt_obj->name, RT_NAME_MAX);
  148. }
  149. return ofw_name;
  150. }
  151. static int tty_device_compare(rt_device_t dev, void *data)
  152. {
  153. rt_ubase_t *args_list;
  154. char *dst_con;
  155. const char *console, **ofw_name;
  156. const struct rt_ofw_node_id *id;
  157. struct rt_platform_device *pdev;
  158. int *tty_idx, tty_id, tty_name_len;
  159. pdev = rt_container_of(dev, struct rt_platform_device, parent);
  160. id = pdev->id;
  161. args_list = data;
  162. tty_idx = (int *)args_list[0];
  163. tty_id = args_list[1];
  164. console = (const char *)args_list[2];
  165. tty_name_len = args_list[3];
  166. dst_con = (char *)args_list[4];
  167. ofw_name = (const char **)args_list[5];
  168. if (id && id->type[0] && !strncmp(id->type, console, tty_name_len))
  169. {
  170. if (*tty_idx == tty_id)
  171. {
  172. *ofw_name = ofw_console_serial_find(dst_con, pdev->parent.ofw_node);
  173. return RT_EOK;
  174. }
  175. ++*tty_idx;
  176. }
  177. return -RT_EEMPTY;
  178. }
  179. static const char *ofw_console_tty_find(char *dst_con, const char *con)
  180. {
  181. const char *ofw_name = RT_NULL;
  182. static rt_bus_t platform_bus = RT_NULL;
  183. if (!platform_bus)
  184. {
  185. platform_bus = rt_bus_find_by_name("platform");
  186. }
  187. if (platform_bus)
  188. {
  189. rt_ubase_t args_list[6];
  190. const char *console = con;
  191. int tty_idx = 0, tty_id = 0, tty_name_len;
  192. con += sizeof("tty") - 1;
  193. while (*con && !(*con >= '0' && *con <= '9'))
  194. {
  195. ++con;
  196. }
  197. tty_name_len = con - console;
  198. while (*con && *con >= '0' && *con <= '9')
  199. {
  200. tty_id *= 10;
  201. tty_id += *con - '0';
  202. ++con;
  203. }
  204. args_list[0] = (rt_ubase_t)&tty_idx;
  205. args_list[1] = tty_id;
  206. args_list[2] = (rt_ubase_t)console;
  207. args_list[3] = tty_name_len;
  208. args_list[4] = (rt_ubase_t)dst_con;
  209. args_list[5] = (rt_ubase_t)&ofw_name;
  210. rt_bus_for_each_dev(platform_bus, &args_list, tty_device_compare);
  211. }
  212. return ofw_name;
  213. }
  214. rt_err_t rt_ofw_console_setup(void)
  215. {
  216. rt_err_t err = -RT_ENOSYS;
  217. char con_name[RT_NAME_MAX], *options = RT_NULL;
  218. const char *ofw_name = RT_NULL, *stdout_path, *con;
  219. /* chosen.console > chosen.stdout-path > RT_CONSOLE_DEVICE_NAME */
  220. con = rt_ofw_bootargs_select("console=", 0);
  221. for (int i = 1; con; ++i)
  222. {
  223. if (!rt_strncmp(con, "uart", sizeof("uart") - 1))
  224. {
  225. rt_strncpy(con_name, con, RT_NAME_MAX);
  226. err = RT_EOK;
  227. break;
  228. }
  229. else if (!rt_strncmp(con, "tty", sizeof("tty") - 1))
  230. {
  231. ofw_name = ofw_console_tty_find(con_name, con);
  232. if (ofw_name)
  233. {
  234. const char *ch = con;
  235. while (*ch && *ch != ' ')
  236. {
  237. if (*ch++ == ',')
  238. {
  239. options = (char *)ch;
  240. break;
  241. }
  242. }
  243. err = RT_EOK;
  244. break;
  245. }
  246. }
  247. con = rt_ofw_bootargs_select("console=", i);
  248. }
  249. if (err == -RT_ENOSYS && !rt_ofw_prop_read_string(ofw_node_chosen, "stdout-path", &stdout_path))
  250. {
  251. struct rt_ofw_node *stdout_np = rt_ofw_find_node_by_path(stdout_path);
  252. if (stdout_np && (ofw_name = ofw_console_serial_find(con_name, stdout_np)))
  253. {
  254. err = RT_EOK;
  255. }
  256. }
  257. if (err == -RT_ENOSYS)
  258. {
  259. rt_device_t serial = rt_device_find(RT_CONSOLE_DEVICE_NAME);
  260. if (serial)
  261. {
  262. ofw_name = rt_ofw_node_full_name(serial->ofw_node);
  263. con = RT_CONSOLE_DEVICE_NAME;
  264. }
  265. else
  266. {
  267. LOG_W("Console will probably fail to setup");
  268. }
  269. }
  270. else
  271. {
  272. con = con_name;
  273. }
  274. rt_console_set_device(con);
  275. if (options)
  276. {
  277. rt_device_t con_dev = rt_console_get_device();
  278. if (con_dev)
  279. {
  280. struct serial_configure con_conf = serial_cfg_from_args(options);
  281. rt_device_control(con_dev, RT_DEVICE_CTRL_CONFIG, &con_conf);
  282. }
  283. }
  284. rt_fdt_earlycon_kick(FDT_EARLYCON_KICK_COMPLETED);
  285. LOG_I("Console: %s (%s)", con, ofw_name ? ofw_name : "<unknown>");
  286. return err;
  287. }
  288. const char *rt_ofw_bootargs_select(const char *key, int index)
  289. {
  290. const char *value = RT_NULL;
  291. if (key && index >= 0)
  292. {
  293. static char **values = RT_NULL;
  294. static rt_size_t bootargs_nr = 1;
  295. const char *bootargs = RT_NULL, *ch;
  296. if (bootargs_nr && !values &&
  297. (bootargs_nr = 0, !rt_ofw_prop_read_string(ofw_node_chosen, "bootargs", &bootargs)) &&
  298. bootargs && (bootargs = rt_strdup(bootargs)))
  299. {
  300. rt_bool_t quotes = RT_TRUE;
  301. rt_size_t length = rt_strlen(bootargs);
  302. const char *bootargs_end = bootargs + length;
  303. for (ch = bootargs; ch < bootargs_end; ++ch)
  304. {
  305. if (*ch == '"')
  306. {
  307. quotes = !quotes;
  308. continue;
  309. }
  310. if (*ch != ' ' || !quotes)
  311. {
  312. continue;
  313. }
  314. ++bootargs_nr;
  315. while (*ch == ' ' && ch < bootargs_end)
  316. {
  317. *(char *)ch++ = '\0';
  318. }
  319. if (*ch == '\0')
  320. {
  321. /* space in the end */
  322. --bootargs_nr;
  323. }
  324. --ch;
  325. }
  326. if (bootargs_nr)
  327. {
  328. /* last arg */
  329. ++bootargs_nr;
  330. values = rt_malloc(sizeof(char *) * bootargs_nr);
  331. }
  332. if (values)
  333. {
  334. int idx = 0;
  335. for (int i = 0; i < length; ++i)
  336. {
  337. for (; i < length && !bootargs[i]; ++i)
  338. {
  339. }
  340. if (i < length)
  341. {
  342. values[idx++] = (char *)&bootargs[i];
  343. }
  344. for (; i < length && bootargs[i]; ++i)
  345. {
  346. }
  347. }
  348. }
  349. else
  350. {
  351. rt_free((char *)bootargs);
  352. }
  353. }
  354. if (values)
  355. {
  356. int keylen = rt_strlen(key);
  357. for (int idx = 0, count = 0; idx < bootargs_nr; ++idx)
  358. {
  359. if (!rt_strncmp(values[idx], key, keylen))
  360. {
  361. if (count == index)
  362. {
  363. value = values[idx] + keylen;
  364. break;
  365. }
  366. ++count;
  367. }
  368. }
  369. }
  370. }
  371. return value;
  372. }
  373. #ifdef RT_USING_CONSOLE
  374. static void dts_put_depth(int depth)
  375. {
  376. while (depth --> 0)
  377. {
  378. rt_kputs(" ");
  379. }
  380. }
  381. static rt_bool_t dts_test_string_list(const void *value, int size)
  382. {
  383. const char *str, *str_start, *str_end;
  384. if (!size)
  385. {
  386. return RT_FALSE;
  387. }
  388. str = value;
  389. /* String end with '\0' */
  390. if (str[size - 1] != '\0')
  391. {
  392. return RT_FALSE;
  393. }
  394. /* Get string list end */
  395. str_end = str + size;
  396. while (str < str_end)
  397. {
  398. str_start = str;
  399. /* Before string list end, not '\0' and a printable characters */
  400. while (str < str_end && *str && ((unsigned char)*str >= ' ' && (unsigned char)*str <= '~'))
  401. {
  402. ++str;
  403. }
  404. /* Not zero, or not increased */
  405. if (*str != '\0' || str == str_start)
  406. {
  407. return RT_FALSE;
  408. }
  409. /* Next string */
  410. ++str;
  411. }
  412. return RT_TRUE;
  413. }
  414. static void ofw_node_dump_dts(struct rt_ofw_node *np, rt_bool_t sibling_too)
  415. {
  416. int depth = 0;
  417. struct rt_ofw_prop *prop;
  418. struct rt_ofw_node *org_np = np;
  419. while (np)
  420. {
  421. dts_put_depth(depth);
  422. rt_kputs(np->full_name);
  423. rt_kputs(" {\n");
  424. prop = np->props;
  425. /* Print prop start */
  426. ++depth;
  427. while (prop)
  428. {
  429. dts_put_depth(depth);
  430. rt_kputs(prop->name);
  431. /* Have value? */
  432. if (prop->length > 0)
  433. {
  434. int length = prop->length;
  435. void *value = prop->value;
  436. rt_kputs(" = ");
  437. if (dts_test_string_list(value, length))
  438. {
  439. /* String list */
  440. char *str = value;
  441. do {
  442. rt_kputs("\"");
  443. rt_kputs(str);
  444. rt_kputs("\", ");
  445. str += rt_strlen(str) + 1;
  446. } while (str < (char *)value + length);
  447. rt_kputs("\b\b");
  448. }
  449. else if ((length % 4) == 0)
  450. {
  451. /* u32 data in <?> */
  452. fdt32_t *cell = value;
  453. rt_kputs("<");
  454. length /= 4;
  455. for (int i = 0; i < length; ++i)
  456. {
  457. rt_kprintf("0x%02x ", fdt32_to_cpu(cell[i]));
  458. }
  459. rt_kputs("\b>");
  460. }
  461. else
  462. {
  463. /* Byte data in [?] */
  464. rt_uint8_t *byte = value;
  465. rt_kputs("[");
  466. for (int i = 0; i < length; ++i)
  467. {
  468. rt_kprintf("%02x ", *byte++);
  469. }
  470. rt_kputs("\b]");
  471. }
  472. }
  473. rt_kputs(";\n");
  474. prop = prop->next;
  475. }
  476. --depth;
  477. /* Print prop end */
  478. if (np->child)
  479. {
  480. rt_kputs("\n");
  481. np = np->child;
  482. ++depth;
  483. }
  484. else
  485. {
  486. dts_put_depth(depth);
  487. rt_kputs("};\n");
  488. if (!sibling_too && org_np == np)
  489. {
  490. break;
  491. }
  492. while (np->parent && !np->sibling)
  493. {
  494. np = np->parent;
  495. --depth;
  496. if (depth >= 0)
  497. {
  498. dts_put_depth(depth);
  499. rt_kputs("};\n");
  500. }
  501. }
  502. if (!sibling_too && org_np == np)
  503. {
  504. break;
  505. }
  506. np = np->sibling;
  507. if (np)
  508. {
  509. rt_kputs("\n");
  510. }
  511. }
  512. }
  513. }
  514. void rt_ofw_node_dump_dts(struct rt_ofw_node *np, rt_bool_t sibling_too)
  515. {
  516. if (np)
  517. {
  518. if (!rt_strcmp(np->name, "/"))
  519. {
  520. struct fdt_info *header = (struct fdt_info *)np->name;
  521. struct fdt_reserve_entry *rsvmap = header->rsvmap;
  522. /*
  523. * Shall be present to identify the file as a version 1 DTS
  524. * (dts files without this tag will be treated by dtc
  525. * as being in the obsolete version 0, which uses
  526. * a different format for integers in addition to
  527. * other small but incompatible changes).
  528. */
  529. rt_kprintf("/dts-v1/;\n\n");
  530. for (int i = header->rsvmap_nr - 1; i >= 0; --i)
  531. {
  532. rt_kprintf("/memreserve/\t%p %p;\n",
  533. ofw_static_cast(rt_size_t, fdt64_to_cpu(rsvmap->address)),
  534. ofw_static_cast(rt_size_t, fdt64_to_cpu(rsvmap->size)));
  535. ++rsvmap;
  536. }
  537. rt_kputs(np->name);
  538. }
  539. ofw_node_dump_dts(np, sibling_too);
  540. }
  541. }
  542. #ifdef RT_USING_MSH
  543. static void ofw_dts(int argc, char**argv)
  544. {
  545. if (ofw_node_root)
  546. {
  547. if (argc == 1)
  548. {
  549. rt_ofw_node_dump_dts(ofw_node_root, RT_TRUE);
  550. }
  551. else if (argv[1][0] == '/')
  552. {
  553. struct rt_ofw_node *np = rt_ofw_find_node_by_path(argv[1]);
  554. if (np)
  555. {
  556. rt_ofw_node_dump_dts(np, RT_FALSE);
  557. }
  558. else
  559. {
  560. rt_kprintf("%s not found.\n", argv[1]);
  561. }
  562. }
  563. else if (argc >= 2 && !rt_strcmp(argv[1], "list") && argv[2][0] == '/')
  564. {
  565. struct rt_ofw_node *np = rt_ofw_find_node_by_path(argv[2]);
  566. if (np)
  567. {
  568. const char *split = np == ofw_node_root ? "" : "/";
  569. np = np->child;
  570. while (np)
  571. {
  572. rt_kprintf("%s%s%s\n", argv[2], split, np->full_name);
  573. np = np->sibling;
  574. }
  575. }
  576. else
  577. {
  578. rt_kprintf("%s not found.\n", argv[2]);
  579. }
  580. }
  581. else
  582. {
  583. rt_kprintf("Usage: %s {list} {path}\n", __func__);
  584. }
  585. }
  586. else
  587. {
  588. rt_kprintf("\"/\" path not found.");
  589. }
  590. }
  591. MSH_CMD_EXPORT(ofw_dts, dump the dts or node for this platform);
  592. #endif /* RT_USING_MSH */
  593. #endif /* RT_USING_CONSOLE */