ofw.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750
  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;
  252. if (*stdout_path != '/' && ofw_node_aliases)
  253. {
  254. int alias_len;
  255. struct rt_ofw_prop *prop;
  256. alias_len = strchrnul(stdout_path, ':') - stdout_path;
  257. rt_ofw_foreach_prop(ofw_node_aliases, prop)
  258. {
  259. if (!rt_strncmp(prop->name, stdout_path, alias_len))
  260. {
  261. stdout_path = prop->value;
  262. break;
  263. }
  264. }
  265. }
  266. stdout_np = rt_ofw_find_node_by_path(stdout_path);
  267. if (stdout_np && (ofw_name = ofw_console_serial_find(con_name, stdout_np)))
  268. {
  269. err = RT_EOK;
  270. }
  271. }
  272. if (err == -RT_ENOSYS)
  273. {
  274. rt_device_t serial = rt_device_find(RT_CONSOLE_DEVICE_NAME);
  275. if (serial)
  276. {
  277. ofw_name = rt_ofw_node_full_name(serial->ofw_node);
  278. con = RT_CONSOLE_DEVICE_NAME;
  279. }
  280. else
  281. {
  282. LOG_W("Console will probably fail to setup");
  283. }
  284. }
  285. else
  286. {
  287. con = con_name;
  288. }
  289. rt_console_set_device(con);
  290. if (options)
  291. {
  292. rt_device_t con_dev = rt_console_get_device();
  293. if (con_dev)
  294. {
  295. struct serial_configure con_conf = serial_cfg_from_args(options);
  296. rt_device_control(con_dev, RT_DEVICE_CTRL_CONFIG, &con_conf);
  297. }
  298. }
  299. rt_fdt_earlycon_kick(FDT_EARLYCON_KICK_COMPLETED);
  300. LOG_I("Console: %s (%s)", con, ofw_name ? ofw_name : "<unknown>");
  301. return err;
  302. }
  303. const char *rt_ofw_bootargs_select(const char *key, int index)
  304. {
  305. const char *value = RT_NULL;
  306. if (key && index >= 0)
  307. {
  308. static char **values = RT_NULL;
  309. static rt_size_t bootargs_nr = 1;
  310. const char *bootargs = RT_NULL, *ch;
  311. if (bootargs_nr && !values &&
  312. (bootargs_nr = 0, !rt_ofw_prop_read_string(ofw_node_chosen, "bootargs", &bootargs)) &&
  313. bootargs && (bootargs = rt_strdup(bootargs)))
  314. {
  315. rt_bool_t quotes = RT_TRUE;
  316. rt_size_t length = rt_strlen(bootargs);
  317. const char *bootargs_end = bootargs + length;
  318. for (ch = bootargs; ch < bootargs_end; ++ch)
  319. {
  320. if (*ch == '"')
  321. {
  322. quotes = !quotes;
  323. continue;
  324. }
  325. if (*ch != ' ' || !quotes)
  326. {
  327. continue;
  328. }
  329. ++bootargs_nr;
  330. while (*ch == ' ' && ch < bootargs_end)
  331. {
  332. *(char *)ch++ = '\0';
  333. }
  334. if (*ch == '\0')
  335. {
  336. /* space in the end */
  337. --bootargs_nr;
  338. }
  339. --ch;
  340. }
  341. if (bootargs_nr)
  342. {
  343. /* last arg */
  344. ++bootargs_nr;
  345. values = rt_malloc(sizeof(char *) * bootargs_nr);
  346. }
  347. if (values)
  348. {
  349. int idx = 0;
  350. for (int i = 0; i < length; ++i)
  351. {
  352. for (; i < length && !bootargs[i]; ++i)
  353. {
  354. }
  355. if (i < length)
  356. {
  357. values[idx++] = (char *)&bootargs[i];
  358. }
  359. for (; i < length && bootargs[i]; ++i)
  360. {
  361. }
  362. }
  363. }
  364. else
  365. {
  366. rt_free((char *)bootargs);
  367. }
  368. }
  369. if (values)
  370. {
  371. int keylen = rt_strlen(key);
  372. for (int idx = 0, count = 0; idx < bootargs_nr; ++idx)
  373. {
  374. if (!rt_strncmp(values[idx], key, keylen))
  375. {
  376. if (count == index)
  377. {
  378. value = values[idx] + keylen;
  379. break;
  380. }
  381. ++count;
  382. }
  383. }
  384. }
  385. }
  386. return value;
  387. }
  388. #ifdef RT_USING_CONSOLE
  389. static void dts_put_depth(int depth)
  390. {
  391. while (depth --> 0)
  392. {
  393. rt_kputs(" ");
  394. }
  395. }
  396. static rt_bool_t dts_test_string_list(const void *value, int size)
  397. {
  398. const char *str, *str_start, *str_end;
  399. if (!size)
  400. {
  401. return RT_FALSE;
  402. }
  403. str = value;
  404. /* String end with '\0' */
  405. if (str[size - 1] != '\0')
  406. {
  407. return RT_FALSE;
  408. }
  409. /* Get string list end */
  410. str_end = str + size;
  411. while (str < str_end)
  412. {
  413. str_start = str;
  414. /* Before string list end, not '\0' and a printable characters */
  415. while (str < str_end && *str && ((unsigned char)*str >= ' ' && (unsigned char)*str <= '~'))
  416. {
  417. ++str;
  418. }
  419. /* Not zero, or not increased */
  420. if (*str != '\0' || str == str_start)
  421. {
  422. return RT_FALSE;
  423. }
  424. /* Next string */
  425. ++str;
  426. }
  427. return RT_TRUE;
  428. }
  429. static void ofw_node_dump_dts(struct rt_ofw_node *np, rt_bool_t sibling_too)
  430. {
  431. int depth = 0;
  432. struct rt_ofw_prop *prop;
  433. struct rt_ofw_node *org_np = np;
  434. while (np)
  435. {
  436. dts_put_depth(depth);
  437. rt_kputs(np->full_name);
  438. rt_kputs(" {\n");
  439. prop = np->props;
  440. /* Print prop start */
  441. ++depth;
  442. while (prop)
  443. {
  444. dts_put_depth(depth);
  445. rt_kputs(prop->name);
  446. /* Have value? */
  447. if (prop->length > 0)
  448. {
  449. int length = prop->length;
  450. void *value = prop->value;
  451. rt_kputs(" = ");
  452. if (dts_test_string_list(value, length))
  453. {
  454. /* String list */
  455. char *str = value;
  456. do {
  457. rt_kputs("\"");
  458. rt_kputs(str);
  459. rt_kputs("\", ");
  460. str += rt_strlen(str) + 1;
  461. } while (str < (char *)value + length);
  462. rt_kputs("\b\b");
  463. }
  464. else if ((length % 4) == 0)
  465. {
  466. /* u32 data in <?> */
  467. fdt32_t *cell = value;
  468. rt_kputs("<");
  469. length /= 4;
  470. for (int i = 0; i < length; ++i)
  471. {
  472. rt_kprintf("0x%02x ", fdt32_to_cpu(cell[i]));
  473. }
  474. rt_kputs("\b>");
  475. }
  476. else
  477. {
  478. /* Byte data in [?] */
  479. rt_uint8_t *byte = value;
  480. rt_kputs("[");
  481. for (int i = 0; i < length; ++i)
  482. {
  483. rt_kprintf("%02x ", *byte++);
  484. }
  485. rt_kputs("\b]");
  486. }
  487. }
  488. rt_kputs(";\n");
  489. prop = prop->next;
  490. }
  491. --depth;
  492. /* Print prop end */
  493. if (np->child)
  494. {
  495. rt_kputs("\n");
  496. np = np->child;
  497. ++depth;
  498. }
  499. else
  500. {
  501. dts_put_depth(depth);
  502. rt_kputs("};\n");
  503. if (!sibling_too && org_np == np)
  504. {
  505. break;
  506. }
  507. while (np->parent && !np->sibling)
  508. {
  509. np = np->parent;
  510. --depth;
  511. if (depth >= 0)
  512. {
  513. dts_put_depth(depth);
  514. rt_kputs("};\n");
  515. }
  516. }
  517. if (!sibling_too && org_np == np)
  518. {
  519. break;
  520. }
  521. np = np->sibling;
  522. if (np)
  523. {
  524. rt_kputs("\n");
  525. }
  526. }
  527. }
  528. }
  529. void rt_ofw_node_dump_dts(struct rt_ofw_node *np, rt_bool_t sibling_too)
  530. {
  531. if (np)
  532. {
  533. if (!rt_strcmp(np->name, "/"))
  534. {
  535. struct fdt_info *header = (struct fdt_info *)np->name;
  536. struct fdt_reserve_entry *rsvmap = header->rsvmap;
  537. /*
  538. * Shall be present to identify the file as a version 1 DTS
  539. * (dts files without this tag will be treated by dtc
  540. * as being in the obsolete version 0, which uses
  541. * a different format for integers in addition to
  542. * other small but incompatible changes).
  543. */
  544. rt_kprintf("/dts-v1/;\n\n");
  545. for (int i = header->rsvmap_nr - 1; i >= 0; --i)
  546. {
  547. rt_kprintf("/memreserve/\t%p %p;\n",
  548. ofw_static_cast(rt_size_t, fdt64_to_cpu(rsvmap->address)),
  549. ofw_static_cast(rt_size_t, fdt64_to_cpu(rsvmap->size)));
  550. ++rsvmap;
  551. }
  552. rt_kputs(np->name);
  553. }
  554. ofw_node_dump_dts(np, sibling_too);
  555. }
  556. }
  557. #ifdef RT_USING_MSH
  558. static void ofw_dts(int argc, char**argv)
  559. {
  560. if (ofw_node_root)
  561. {
  562. if (argc == 1)
  563. {
  564. rt_ofw_node_dump_dts(ofw_node_root, RT_TRUE);
  565. }
  566. else if (argv[1][0] == '/')
  567. {
  568. struct rt_ofw_node *np = rt_ofw_find_node_by_path(argv[1]);
  569. if (np)
  570. {
  571. rt_ofw_node_dump_dts(np, RT_FALSE);
  572. }
  573. else
  574. {
  575. rt_kprintf("%s not found.\n", argv[1]);
  576. }
  577. }
  578. else if (argc >= 2 && !rt_strcmp(argv[1], "list") && argv[2][0] == '/')
  579. {
  580. struct rt_ofw_node *np = rt_ofw_find_node_by_path(argv[2]);
  581. if (np)
  582. {
  583. const char *split = np == ofw_node_root ? "" : "/";
  584. np = np->child;
  585. while (np)
  586. {
  587. rt_kprintf("%s%s%s\n", argv[2], split, np->full_name);
  588. np = np->sibling;
  589. }
  590. }
  591. else
  592. {
  593. rt_kprintf("%s not found.\n", argv[2]);
  594. }
  595. }
  596. else
  597. {
  598. rt_kprintf("Usage: %s {list} {path}\n", __func__);
  599. }
  600. }
  601. else
  602. {
  603. rt_kprintf("\"/\" path not found.");
  604. }
  605. }
  606. MSH_CMD_EXPORT(ofw_dts, dump the dts or node for this platform);
  607. #endif /* RT_USING_MSH */
  608. #endif /* RT_USING_CONSOLE */