ofw.c 17 KB

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