Loading...
   1
   2
   3
   4
   5
   6
   7
   8
   9
  10
  11
  12
  13
  14
  15
  16
  17
  18
  19
  20
  21
  22
  23
  24
  25
  26
  27
  28
  29
  30
  31
  32
  33
  34
  35
  36
  37
  38
  39
  40
  41
  42
  43
  44
  45
  46
  47
  48
  49
  50
  51
  52
  53
  54
  55
  56
  57
  58
  59
  60
  61
  62
  63
  64
  65
  66
  67
  68
  69
  70
  71
  72
  73
  74
  75
  76
  77
  78
  79
  80
  81
  82
  83
  84
  85
  86
  87
  88
  89
  90
  91
  92
  93
  94
  95
  96
  97
  98
  99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
 189
 190
 191
 192
 193
 194
 195
 196
 197
 198
 199
 200
 201
 202
 203
 204
 205
 206
 207
 208
 209
 210
 211
 212
 213
 214
 215
 216
 217
 218
 219
 220
 221
 222
 223
 224
 225
 226
 227
 228
 229
 230
 231
 232
 233
 234
 235
 236
 237
 238
 239
 240
 241
 242
 243
 244
 245
 246
 247
 248
 249
 250
 251
 252
 253
 254
 255
 256
 257
 258
 259
 260
 261
 262
 263
 264
 265
 266
 267
 268
 269
 270
 271
 272
 273
 274
 275
 276
 277
 278
 279
 280
 281
 282
 283
 284
 285
 286
 287
 288
 289
 290
 291
 292
 293
 294
 295
 296
 297
 298
 299
 300
 301
 302
 303
 304
 305
 306
 307
 308
 309
 310
 311
 312
 313
 314
 315
 316
 317
 318
 319
 320
 321
 322
 323
 324
 325
 326
 327
 328
 329
 330
 331
 332
 333
 334
 335
 336
 337
 338
 339
 340
 341
 342
 343
 344
 345
 346
 347
 348
 349
 350
 351
 352
 353
 354
 355
 356
 357
 358
 359
 360
 361
 362
 363
 364
 365
 366
 367
 368
 369
 370
 371
 372
 373
 374
 375
 376
 377
 378
 379
 380
 381
 382
 383
 384
 385
 386
 387
 388
 389
 390
 391
 392
 393
 394
 395
 396
 397
 398
 399
 400
 401
 402
 403
 404
 405
 406
 407
 408
 409
 410
 411
 412
 413
 414
 415
 416
 417
 418
 419
 420
 421
 422
 423
 424
 425
 426
 427
 428
 429
 430
 431
 432
 433
 434
 435
 436
 437
 438
 439
 440
 441
 442
 443
 444
 445
 446
 447
 448
 449
 450
 451
 452
 453
 454
 455
 456
 457
 458
 459
 460
 461
 462
 463
 464
 465
 466
 467
 468
 469
 470
 471
 472
 473
 474
 475
 476
 477
 478
 479
 480
 481
 482
 483
 484
 485
 486
 487
 488
 489
 490
 491
 492
 493
 494
 495
 496
 497
 498
 499
 500
 501
 502
 503
 504
 505
 506
 507
 508
 509
 510
 511
 512
 513
 514
 515
 516
 517
 518
 519
 520
 521
 522
 523
 524
 525
 526
 527
 528
 529
 530
 531
 532
 533
 534
 535
 536
 537
 538
 539
 540
 541
 542
 543
 544
 545
 546
 547
 548
 549
 550
 551
 552
 553
 554
 555
 556
 557
 558
 559
 560
 561
 562
 563
 564
 565
 566
 567
 568
 569
 570
 571
 572
 573
 574
 575
 576
 577
 578
 579
 580
 581
 582
 583
 584
 585
 586
 587
 588
 589
 590
 591
 592
 593
 594
 595
 596
 597
 598
 599
 600
 601
 602
 603
 604
 605
 606
 607
 608
 609
 610
 611
 612
 613
 614
 615
 616
 617
 618
 619
 620
 621
 622
 623
 624
 625
 626
 627
 628
 629
 630
 631
 632
 633
 634
 635
 636
 637
 638
 639
 640
 641
 642
 643
 644
 645
 646
 647
 648
 649
 650
 651
 652
 653
 654
 655
 656
 657
 658
 659
 660
 661
 662
 663
 664
 665
 666
 667
 668
 669
 670
 671
 672
 673
 674
 675
 676
 677
 678
 679
 680
 681
 682
 683
 684
 685
 686
 687
 688
 689
 690
 691
 692
 693
 694
 695
 696
 697
 698
 699
 700
 701
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
/* SPDX-License-Identifier: GPL-2.0+ */
/*
 * Copyright (c) 2017 Google, Inc
 * Written by Simon Glass <sjg@chromium.org>
 */

#ifndef _DM_OFNODE_H
#define _DM_OFNODE_H

/* TODO(sjg@chromium.org): Drop fdtdec.h include */
#include <fdtdec.h>
#include <dm/of.h>
#include <dm/of_access.h>
#include <log.h>
#include <phy_interface.h>

/* Enable checks to protect against invalid calls */
#undef OF_CHECKS

struct abuf;
struct resource;

#include <dm/ofnode_decl.h>
#include <linux/errno.h>

struct ofnode_phandle_args {
	ofnode node;
	int args_count;
	uint32_t args[OF_MAX_PHANDLE_ARGS];
};

#if CONFIG_IS_ENABLED(OFNODE_MULTI_TREE)
/**
 * oftree_reset() - reset the state of the oftree list
 *
 * Reset the oftree list so it can be started again. This should be called
 * once the control FDT is in place, but before the ofnode interface is used.
 */
void oftree_reset(void);

/**
 * ofnode_to_fdt() - convert an ofnode to a flat DT pointer
 *
 * This cannot be called if the reference contains a node pointer.
 *
 * @node: Reference containing offset (possibly invalid)
 * Return: DT offset (can be NULL)
 */
__attribute_const__ void *ofnode_to_fdt(ofnode node);

/**
 * ofnode_to_offset() - convert an ofnode to a flat DT offset
 *
 * This cannot be called if the reference contains a node pointer.
 *
 * @node: Reference containing offset (possibly invalid)
 * Return: DT offset (can be -1)
 */
__attribute_const__ int ofnode_to_offset(ofnode node);

/**
 * oftree_from_fdt() - Returns an oftree from a flat device tree pointer
 *
 * If @fdt is not already registered in the list of current device trees, it is
 * added to the list.
 *
 * @fdt: Device tree to use
 *
 * Returns: reference to the given node
 */
oftree oftree_from_fdt(void *fdt);

/**
 * noffset_to_ofnode() - convert a DT offset to an ofnode
 *
 * @other_node: Node in the same tree to use as a reference
 * @of_offset: DT offset (either valid, or -1)
 * Return: reference to the associated DT offset
 */
ofnode noffset_to_ofnode(ofnode other_node, int of_offset);

/**
 * oftree_dispose() - Dispose of an oftree
 *
 * This can be used to dispose of a tree that has been created (other than
 * the control FDT which must not be disposed)
 *
 * @tree: Tree to dispose
 */
void oftree_dispose(oftree tree);

#else /* !OFNODE_MULTI_TREE */
static inline void oftree_reset(void) {}

static inline void *ofnode_to_fdt(ofnode node)
{
#ifdef OF_CHECKS
	if (of_live_active())
		return NULL;
#endif
	/* Use the control FDT by default */
	return (void *)gd->fdt_blob;
}

static inline __attribute_const__ int ofnode_to_offset(ofnode node)
{
#ifdef OF_CHECKS
	if (of_live_active())
		return -1;
#endif
	return node.of_offset;
}

static inline oftree oftree_from_fdt(void *fdt)
{
	oftree tree;

	/* we cannot access other trees without OFNODE_MULTI_TREE */
	if (fdt == gd->fdt_blob)
		tree.fdt = fdt;
	else
		tree.fdt = NULL;

	return tree;
}

static inline ofnode noffset_to_ofnode(ofnode other_node, int of_offset)
{
	ofnode node;

	if (of_live_active())
		node.np = NULL;
	else
		node.of_offset = of_offset;

	return node;
}

static inline void oftree_dispose(oftree tree) {}

#endif /* OFNODE_MULTI_TREE */

/**
 * oftree_new() - Create a new, empty tree
 *
 * @treep: Returns a pointer to the tree, on success
 * Returns: 0 on success, -ENOMEM if out of memory, -E2BIG if !OF_LIVE and
 * there are too many (flattrees) already
 */
int oftree_new(oftree *treep);

/**
 * oftree_to_fdt() - Convert an oftree to a flat FDT
 *
 * @tree: tree to flatten (if livetree) or copy (if not)
 * @buf: Returns inited buffer containing the newly created flat tree. Note
 * that for flat tree the buffer is not allocated. In either case the caller
 * must call abut_uninit() to free any memory used by @buf
 * Return: 0 on success, -ENOMEM if out of memory, other -ve value for any other
 * error
 */
int oftree_to_fdt(oftree tree, struct abuf *buf);

/**
 * ofnode_to_np() - convert an ofnode to a live DT node pointer
 *
 * This cannot be called if the reference contains an offset.
 *
 * @node: Reference containing struct device_node * (possibly invalid)
 * Return: pointer to device node (can be NULL)
 */
static inline struct device_node *ofnode_to_np(ofnode node)
{
#ifdef OF_CHECKS
	if (!of_live_active())
		return NULL;
#endif
	return node.np;
}

/**
 * ofnode_valid() - check if an ofnode is valid
 *
 * @node: Reference containing offset (possibly invalid)
 * Return: true if the reference contains a valid ofnode, false if not
 */
static inline bool ofnode_valid(ofnode node)
{
	if (of_live_active())
		return node.np != NULL;
	else
		return node.of_offset >= 0;
}

/**
 * oftree_lookup_fdt() - obtain the FDT pointer from an oftree
 *
 * This can only be called when flat tree is enabled
 *
 * @tree: Tree to look at
 * @return FDT pointer from the tree
 */
static inline void *oftree_lookup_fdt(oftree tree)
{
	if (of_live_active())
		return NULL;
	else
		return tree.fdt;
}

/**
 * offset_to_ofnode() - convert a DT offset to an ofnode
 *
 * @of_offset: DT offset (either valid, or -1)
 * Return: reference to the associated DT offset
 */
static inline ofnode offset_to_ofnode(int of_offset)
{
	ofnode node;

	if (of_live_active())
		node.np = NULL;
	else
		node.of_offset = of_offset >= 0 ? of_offset : -1;

	return node;
}

/**
 * np_to_ofnode() - convert a node pointer to an ofnode
 *
 * @np: Live node pointer (can be NULL)
 * Return: reference to the associated node pointer
 */
static inline ofnode np_to_ofnode(struct device_node *np)
{
	ofnode node;

	node.np = np;

	return node;
}

/**
 * ofnode_is_np() - check if a reference is a node pointer
 *
 * This function associated that if there is a valid live tree then all
 * references will use it. This is because using the flat DT when the live tree
 * is valid is not permitted.
 *
 * @node: reference to check (possibly invalid)
 * Return: true if the reference is a live node pointer, false if it is a DT
 * offset
 */
static inline bool ofnode_is_np(ofnode node)
{
#ifdef OF_CHECKS
	/*
	 * Check our assumption that flat tree offsets are not used when a
	 * live tree is in use.
	 */
	assert(!ofnode_valid(node) ||
	       (of_live_active() ? ofnode_to_np(node)
				  : ofnode_to_np(node)));
#endif
	return of_live_active() && ofnode_valid(node);
}

/**
 * ofnode_equal() - check if two references are equal
 *
 * @ref1: first reference to check (possibly invalid)
 * @ref2: second reference to check (possibly invalid)
 * Return: true if equal, else false
 */
static inline bool ofnode_equal(ofnode ref1, ofnode ref2)
{
	/* We only need to compare the contents */
	return ref1.of_offset == ref2.of_offset;
}

/**
 * oftree_valid() - check if an oftree is valid
 *
 * @tree: Reference containing oftree
 * Return: true if the reference contains a valid oftree, false if node
 */
static inline bool oftree_valid(oftree tree)
{
	if (of_live_active())
		return tree.np;
	else
		return tree.fdt;
}

/**
 * oftree_null() - Obtain a null oftree
 *
 * This returns an oftree which points to no tree. It works both with the flat
 * tree and livetree.
 */
static inline oftree oftree_null(void)
{
	oftree tree;

	if (of_live_active())
		tree.np = NULL;
	else
		tree.fdt = NULL;

	return tree;
}

/**
 * ofnode_null() - Obtain a null ofnode
 *
 * This returns an ofnode which points to no node. It works both with the flat
 * tree and livetree.
 */
static inline ofnode ofnode_null(void)
{
	ofnode node;

	if (of_live_active())
		node.np = NULL;
	else
		node.of_offset = -1;

	return node;
}

static inline ofnode ofnode_root(void)
{
	ofnode node;

	if (of_live_active())
		node.np = gd_of_root();
	else
		node.of_offset = 0;

	return node;
}

/**
 * ofprop_valid() - check if an ofprop is valid
 *
 * @prop: Pointer to ofprop to check
 * Return: true if the reference contains a valid ofprop, false if not
 */
static inline bool ofprop_valid(struct ofprop *prop)
{
	if (of_live_active())
		return prop->prop;
	else
		return prop->offset >= 0;
}

/**
 * oftree_default() - Returns the default device tree (U-Boot's control FDT)
 *
 * Returns: reference to the control FDT
 */
static inline oftree oftree_default(void)
{
	oftree tree;

	if (of_live_active())
		tree.np = gd_of_root();
	else
		tree.fdt = (void *)gd->fdt_blob;

	return tree;
}

/**
 * oftree_from_np() - Returns an oftree from a node pointer
 *
 * @root: Root node of the tree
 * Returns: reference to the given node
 */
static inline oftree oftree_from_np(struct device_node *root)
{
	oftree tree;

	tree.np = root;

	return tree;
}

/**
 * ofnode_name_eq() - Check a node name ignoring its unit address
 *
 * @node:	valid node to compared, which may have a unit address
 * @name:	name (without unit address) to compare with the node name
 * Return: true if matches, false if it doesn't match
 */
bool ofnode_name_eq(ofnode node, const char *name);

/**
 * ofnode_name_eq_unit() - Check a node name ignoring its unit address
 *
 * This is separate from ofnode_name_eq() to avoid code-size increase for
 * boards which don't need this function
 *
 * @node:	valid node to compared, which may have a unit address
 * @name:	name to compare with the node name. If this contains a unit
 *		address, it is matched, otherwise the unit address is ignored
 *		when searching for matches
 * Return: true if matches, false if it doesn't match
 */
bool ofnode_name_eq_unit(ofnode node, const char *name);

/**
 * ofnode_read_u8() - Read a 8-bit integer from a property
 *
 * @node:	valid node reference to read property from
 * @propname:	name of the property to read from
 * @outp:	place to put value (if found)
 * Return: 0 if OK, -ve on error
 */
int ofnode_read_u8(ofnode node, const char *propname, u8 *outp);

/**
 * ofnode_read_u8_default() - Read a 8-bit integer from a property
 *
 * @node:	valid node reference to read property from
 * @propname:	name of the property to read from
 * @def:	default value to return if the property has no value
 * Return: property value, or @def if not found
 */
u8 ofnode_read_u8_default(ofnode node, const char *propname, u8 def);

/**
 * ofnode_read_u16() - Read a 16-bit integer from a property
 *
 * @node:	valid node reference to read property from
 * @propname:	name of the property to read from
 * @outp:	place to put value (if found)
 * Return: 0 if OK, -ve on error
 */
int ofnode_read_u16(ofnode node, const char *propname, u16 *outp);

/**
 * ofnode_read_u16_default() - Read a 16-bit integer from a property
 *
 * @node:	valid node reference to read property from
 * @propname:	name of the property to read from
 * @def:	default value to return if the property has no value
 * Return: property value, or @def if not found
 */
u16 ofnode_read_u16_default(ofnode node, const char *propname, u16 def);

/**
 * ofnode_read_u32() - Read a 32-bit integer from a property
 *
 * @node:	valid node reference to read property from
 * @propname:	name of the property to read from
 * @outp:	place to put value (if found)
 * Return: 0 if OK, -ve on error
 */
int ofnode_read_u32(ofnode node, const char *propname, u32 *outp);

/**
 * ofnode_read_u32_index() - Read a 32-bit integer from a multi-value property
 *
 * @node:	valid node reference to read property from
 * @propname:	name of the property to read from
 * @index:	index of the integer to return
 * @outp:	place to put value (if found)
 * Return: 0 if OK, -ve on error
 */
int ofnode_read_u32_index(ofnode node, const char *propname, int index,
			  u32 *outp);

/**
 * ofnode_read_u64_index() - Read a 64-bit integer from a multi-value property
 *
 * @node:	valid node reference to read property from
 * @propname:	name of the property to read from
 * @index:	index of the integer to return
 * @outp:	place to put value (if found)
 * Return: 0 if OK, -ve on error
 */
int ofnode_read_u64_index(ofnode node, const char *propname, int index,
			  u64 *outp);

/**
 * ofnode_read_s32() - Read a 32-bit integer from a property
 *
 * @node:	valid node reference to read property from
 * @propname:	name of the property to read from
 * @outp:	place to put value (if found)
 * Return: 0 if OK, -ve on error
 */
static inline int ofnode_read_s32(ofnode node, const char *propname,
				  s32 *outp)
{
	return ofnode_read_u32(node, propname, (u32 *)outp);
}

/**
 * ofnode_read_u32_default() - Read a 32-bit integer from a property
 *
 * @node:	valid node reference to read property from
 * @propname:	name of the property to read from
 * @def:	default value to return if the property has no value
 * Return: property value, or @def if not found
 */
u32 ofnode_read_u32_default(ofnode node, const char *propname, u32 def);

/**
 * ofnode_read_u32_index_default() - Read a 32-bit integer from a multi-value
 *                                   property
 *
 * @node:	valid node reference to read property from
 * @propname:	name of the property to read from
 * @index:	index of the integer to return
 * @def:	default value to return if the property has no value
 * Return: property value, or @def if not found
 */
u32 ofnode_read_u32_index_default(ofnode node, const char *propname, int index,
				  u32 def);

/**
 * ofnode_read_s32_default() - Read a 32-bit integer from a property
 *
 * @node:	valid node reference to read property from
 * @propname:	name of the property to read from
 * @def:	default value to return if the property has no value
 * Return: property value, or @def if not found
 */
int ofnode_read_s32_default(ofnode node, const char *propname, s32 def);

/**
 * ofnode_read_u64() - Read a 64-bit integer from a property
 *
 * @node:	valid node reference to read property from
 * @propname:	name of the property to read from
 * @outp:	place to put value (if found)
 * Return: 0 if OK, -ve on error
 */
int ofnode_read_u64(ofnode node, const char *propname, u64 *outp);

/**
 * ofnode_read_u64_default() - Read a 64-bit integer from a property
 *
 * @node:	valid node reference to read property from
 * @propname:	name of the property to read from
 * @def:	default value to return if the property has no value
 * Return: property value, or @def if not found
 */
u64 ofnode_read_u64_default(ofnode node, const char *propname, u64 def);

/**
 * ofnode_read_prop() - Read a property from a node
 *
 * @node:	valid node reference to read property from
 * @propname:	name of the property to read
 * @sizep:	if non-NULL, returns the size of the property, or an error code
 *              if not found
 * Return: property value, or NULL if there is no such property
 */
const void *ofnode_read_prop(ofnode node, const char *propname, int *sizep);

/**
 * ofnode_read_string() - Read a string from a property
 *
 * @node:	valid node reference to read property from
 * @propname:	name of the property to read
 * Return: string from property value, or NULL if there is no such property
 */
const char *ofnode_read_string(ofnode node, const char *propname);

/**
 * ofnode_read_u32_array() - Find and read an array of 32 bit integers
 *
 * @node:	valid node reference to read property from
 * @propname:	name of the property to read
 * @out_values:	pointer to return value, modified only if return value is 0
 * @sz:		number of array elements to read
 * Return: 0 on success, -EINVAL if the property does not exist,
 * -ENODATA if property does not have a value, and -EOVERFLOW if the
 * property data isn't large enough
 *
 * Search for a property in a device node and read 32-bit value(s) from
 * it.
 *
 * The out_values is modified only if a valid u32 value can be decoded.
 */
int ofnode_read_u32_array(ofnode node, const char *propname,
			  u32 *out_values, size_t sz);

/**
 * ofnode_read_bool() - read a boolean value from a property
 *
 * @node:	valid node reference to read property from
 * @propname:	name of property to read
 * Return: true if property is present (meaning true), false if not present
 */
bool ofnode_read_bool(ofnode node, const char *propname);

/**
 * ofnode_find_subnode() - find a named subnode of a parent node
 *
 * @node:	valid reference to parent node
 * @subnode_name: name of subnode to find
 * Return: reference to subnode (which can be invalid if there is no such
 * subnode)
 */
ofnode ofnode_find_subnode(ofnode node, const char *subnode_name);

/**
 * ofnode_find_subnode_unit() - find a named subnode of a parent node
 *
 * @node:	valid reference to parent node
 * @subnode_name: name of subnode to find, including any unit address. If the
 *	unit address is omitted, any subnode which matches the name (excluding
 *	any unit address) is returned
 * Return: reference to subnode (which can be invalid if there is no such
 * subnode)
 */
ofnode ofnode_find_subnode_unit(ofnode node, const char *subnode_name);

#if CONFIG_IS_ENABLED(DM_INLINE_OFNODE)
#include <asm/global_data.h>

static inline bool ofnode_is_enabled(ofnode node)
{
	if (ofnode_is_np(node)) {
		return of_device_is_available(ofnode_to_np(node));
	} else {
		return fdtdec_get_is_enabled(gd->fdt_blob,
					     ofnode_to_offset(node));
	}
}

static inline ofnode ofnode_first_subnode(ofnode node)
{
	assert(ofnode_valid(node));
	if (ofnode_is_np(node))
		return np_to_ofnode(node.np->child);

	return offset_to_ofnode(
		fdt_first_subnode(gd->fdt_blob, ofnode_to_offset(node)));
}

static inline ofnode ofnode_next_subnode(ofnode node)
{
	assert(ofnode_valid(node));
	if (ofnode_is_np(node))
		return np_to_ofnode(node.np->sibling);

	return offset_to_ofnode(
		fdt_next_subnode(gd->fdt_blob, ofnode_to_offset(node)));
}
#else
/**
 * ofnode_is_enabled() - Checks whether a node is enabled.
 * This looks for a 'status' property. If this exists, then returns true if
 * the status is 'okay' and false otherwise. If there is no status property,
 * it returns true on the assumption that anything mentioned should be enabled
 * by default.
 *
 * @node: node to examine
 * Return: false (not enabled) or true (enabled)
 */
bool ofnode_is_enabled(ofnode node);

/**
 * ofnode_first_subnode() - find the first subnode of a parent node
 *
 * @node:	valid reference to a valid parent node
 * Return: reference to the first subnode (which can be invalid if the parent
 * node has no subnodes)
 */
ofnode ofnode_first_subnode(ofnode node);

/**
 * ofnode_next_subnode() - find the next sibling of a subnode
 *
 * @node:	valid reference to previous node (sibling)
 * Return: reference to the next subnode (which can be invalid if the node
 * has no more siblings)
 */
ofnode ofnode_next_subnode(ofnode node);
#endif /* DM_INLINE_OFNODE */

/**
 * ofnode_get_parent() - get the ofnode's parent (enclosing ofnode)
 *
 * @node: valid node to look up
 * Return: ofnode reference of the parent node
 */
ofnode ofnode_get_parent(ofnode node);

/**
 * ofnode_get_name() - get the name of a node
 *
 * @node: valid node to look up
 * Return: name of node (for the root node this is "")
 */
const char *ofnode_get_name(ofnode node);

/**
 * ofnode_get_path() - get the full path of a node
 *
 * @node: valid node to look up
 * @buf: buffer to write the node path into
 * @buflen: buffer size
 * Return: 0 if OK, -ve on error
 */
int ofnode_get_path(ofnode node, char *buf, int buflen);

/**
 * ofnode_get_by_phandle() - get ofnode from phandle
 *
 * This uses the default (control) device tree
 *
 * @phandle:	phandle to look up
 * Return: ofnode reference to the phandle
 */
ofnode ofnode_get_by_phandle(uint phandle);

/**
 * oftree_get_by_phandle() - get ofnode from phandle
 *
 * @tree:	tree to use
 * @phandle:	phandle to look up
 * Return: ofnode reference to the phandle
 */
ofnode oftree_get_by_phandle(oftree tree, uint phandle);

/**
 * ofnode_read_size() - read the size of a property
 *
 * @node: node to check
 * @propname: property to check
 * Return: size of property if present, or -EINVAL if not
 */
int ofnode_read_size(ofnode node, const char *propname);

/**
 * ofnode_get_addr_size_index() - get an address/size from a node
 *				  based on index
 *
 * This reads the register address/size from a node based on index
 *
 * @node: node to read from
 * @index: Index of address to read (0 for first)
 * @size: Pointer to size of the address
 * Return: address, or FDT_ADDR_T_NONE if not present or invalid
 */
fdt_addr_t ofnode_get_addr_size_index(ofnode node, int index,
				      fdt_size_t *size);

/**
 * ofnode_get_addr_size_index_notrans() - get an address/size from a node
 *					  based on index, without address
 *					  translation
 *
 * This reads the register address/size from a node based on index.
 * The resulting address is not translated. Useful for example for on-disk
 * addresses.
 *
 * @node: node to read from
 * @index: Index of address to read (0 for first)
 * @size: Pointer to size of the address
 * Return: address, or FDT_ADDR_T_NONE if not present or invalid
 */
fdt_addr_t ofnode_get_addr_size_index_notrans(ofnode node, int index,
					      fdt_size_t *size);

/**
 * ofnode_get_addr_index() - get an address from a node
 *
 * This reads the register address from a node
 *
 * @node: node to read from
 * @index: Index of address to read (0 for first)
 * Return: address, or FDT_ADDR_T_NONE if not present or invalid
 */
fdt_addr_t ofnode_get_addr_index(ofnode node, int index);

/**
 * ofnode_get_addr() - get an address from a node
 *
 * This reads the register address from a node
 *
 * @node: node to read from
 * Return: address, or FDT_ADDR_T_NONE if not present or invalid
 */
fdt_addr_t ofnode_get_addr(ofnode node);

/**
 * ofnode_get_size() - get size from a node
 *
 * This reads the register size from a node
 *
 * @node: node to read from
 * Return: size of the address, or FDT_SIZE_T_NONE if not present or invalid
 */
fdt_size_t ofnode_get_size(ofnode node);

/**
 * ofnode_stringlist_search() - find a string in a string list and return index
 *
 * Note that it is possible for this function to succeed on property values
 * that are not NUL-terminated. That's because the function will stop after
 * finding the first occurrence of @string. This can for example happen with
 * small-valued cell properties, such as #address-cells, when searching for
 * the empty string.
 *
 * @node: node to check
 * @propname: name of the property containing the string list
 * @string: string to look up in the string list
 *
 * Return:
 *   the index of the string in the list of strings
 *   -ENODATA if the property is not found
 *   -EINVAL on some other error
 */
int ofnode_stringlist_search(ofnode node, const char *propname,
			     const char *string);

/**
 * ofnode_read_string_index() - obtain an indexed string from a string list
 *
 * Note that this will successfully extract strings from properties with
 * non-NUL-terminated values. For example on small-valued cell properties
 * this function will return the empty string.
 *
 * If non-NULL, the length of the string (on success) or a negative error-code
 * (on failure) will be stored in the integer pointer to by lenp.
 *
 * @node: node to check
 * @propname: name of the property containing the string list
 * @index: index of the string to return (cannot be negative)
 * @outp: return location for the string
 *
 * Return:
 *   0 if found or -ve error value if not found
 */
int ofnode_read_string_index(ofnode node, const char *propname, int index,
			     const char **outp);

/**
 * ofnode_read_string_count() - find the number of strings in a string list
 *
 * @node: node to check
 * @property: name of the property containing the string list
 * Return:
 *   number of strings in the list, or -ve error value if not found
 */
int ofnode_read_string_count(ofnode node, const char *property);

/**
 * ofnode_read_string_list() - read a list of strings
 *
 * This produces a list of string pointers with each one pointing to a string
 * in the string list. If the property does not exist, it returns {NULL}.
 *
 * The data is allocated and the caller is reponsible for freeing the return
 * value (the list of string pointers). The strings themselves may not be
 * changed as they point directly into the devicetree property.
 *
 * @node: node to check
 * @property: name of the property containing the string list
 * @listp: returns an allocated, NULL-terminated list of strings if the return
 *	value is > 0, else is set to NULL
 * Return:
 * number of strings in list, 0 if none, -ENOMEM if out of memory,
 * -EINVAL if no such property, -EENODATA if property is empty
 */
int ofnode_read_string_list(ofnode node, const char *property,
			    const char ***listp);

/**
 * ofnode_parse_phandle() - Resolve a phandle property to an ofnode
 *
 * @node: node to check
 * @phandle_name: Name of property holding a phandle value
 * @index: For properties holding a table of phandles, this is the index into
 *         the table
 * Return: ofnode that the phandle points to or ofnode_null() on error.
 */
ofnode ofnode_parse_phandle(ofnode node, const char *phandle_name,
			    int index);

/**
 * ofnode_parse_phandle_with_args() - Find a node pointed by phandle in a list
 *
 * This function is useful to parse lists of phandles and their arguments.
 * Returns 0 on success and fills out_args, on error returns appropriate
 * errno value.
 *
 * Caller is responsible to call of_node_put() on the returned out_args->np
 * pointer.
 *
 * Example:
 *
 * .. code-block::
 *
 *   phandle1: node1 {
 *       #list-cells = <2>;
 *   };
 *   phandle2: node2 {
 *       #list-cells = <1>;
 *   };
 *   node3 {
 *       list = <&phandle1 1 2 &phandle2 3>;
 *   };
 *
 * To get a device_node of the `node2' node you may call this:
 * ofnode_parse_phandle_with_args(node3, "list", "#list-cells", 0, 1, &args);
 *
 * @node:	device tree node containing a list
 * @list_name:	property name that contains a list
 * @cells_name:	property name that specifies phandles' arguments count
 * @cell_count: Cell count to use if @cells_name is NULL
 * @index:	index of a phandle to parse out
 * @out_args:	optional pointer to output arguments structure (will be filled)
 * Return:
 *   0 on success (with @out_args filled out if not NULL), -ENOENT if
 *   @list_name does not exist, -EINVAL if a phandle was not found,
 *   @cells_name could not be found, the arguments were truncated or there
 *   were too many arguments.
 */
int ofnode_parse_phandle_with_args(ofnode node, const char *list_name,
				   const char *cells_name, int cell_count,
				   int index,
				   struct ofnode_phandle_args *out_args);

/**
 * ofnode_count_phandle_with_args() - Count number of phandle in a list
 *
 * This function is useful to count phandles into a list.
 * Returns number of phandle on success, on error returns appropriate
 * errno value.
 *
 * @node:	device tree node containing a list
 * @list_name:	property name that contains a list
 * @cells_name:	property name that specifies phandles' arguments count
 * @cell_count: Cell count to use if @cells_name is NULL
 * Return:
 *   number of phandle on success, -ENOENT if @list_name does not exist,
 *   -EINVAL if a phandle was not found, @cells_name could not be found.
 */
int ofnode_count_phandle_with_args(ofnode node, const char *list_name,
				   const char *cells_name, int cell_count);

/**
 * oftree_parse_phandle() - Resolve a phandle property to an ofnode
 *			    from a root node
 *
 * @tree: device tree to use
 * @node: node to check
 * @phandle_name: Name of property holding a phandle value
 * @index: For properties holding a table of phandles, this is the index into
 *         the table
 * Return: ofnode that the phandle points to or ofnode_null() on error.
 */
ofnode oftree_parse_phandle(oftree tree, ofnode node, const char *phandle_name,
			    int index);

/**
 * oftree_parse_phandle_with_args() - Find a node pointed by phandle in a list
 *				      from a root node
 *
 * This function is useful to parse lists of phandles and their arguments.
 * Returns 0 on success and fills out_args, on error returns appropriate
 * errno value.
 *
 * Caller is responsible to call of_node_put() on the returned out_args->np
 * pointer.
 *
 * Example:
 *
 * .. code-block::
 *
 *   phandle1: node1 {
 *       #list-cells = <2>;
 *   };
 *   phandle2: node2 {
 *       #list-cells = <1>;
 *   };
 *   node3 {
 *       list = <&phandle1 1 2 &phandle2 3>;
 *   };
 *
 * To get a device_node of the `node2' node you may call this:
 * oftree_parse_phandle_with_args(node3, "list", "#list-cells", 0, 1, &args);
 *
 * @tree:	device tree to use
 * @node:	device tree node containing a list
 * @list_name:	property name that contains a list
 * @cells_name:	property name that specifies phandles' arguments count
 * @cell_count: Cell count to use if @cells_name is NULL
 * @index:	index of a phandle to parse out
 * @out_args:	optional pointer to output arguments structure (will be filled)
 * Return:
 *   0 on success (with @out_args filled out if not NULL), -ENOENT if
 *   @list_name does not exist, -EINVAL if a phandle was not found,
 *   @cells_name could not be found, the arguments were truncated or there
 *   were too many arguments.
 */
int oftree_parse_phandle_with_args(oftree tree, ofnode node, const char *list_name,
				   const char *cells_name, int cell_count,
				   int index,
				   struct ofnode_phandle_args *out_args);

/**
 * oftree_count_phandle_with_args() - Count number of phandle in a list
 *				      from a root node
 *
 * This function is useful to count phandles into a list.
 * Returns number of phandle on success, on error returns appropriate
 * errno value.
 *
 * @tree:	device tree to use
 * @node:	device tree node containing a list
 * @list_name:	property name that contains a list
 * @cells_name:	property name that specifies phandles' arguments count
 * @cell_count: Cell count to use if @cells_name is NULL
 * Return:
 *   number of phandle on success, -ENOENT if @list_name does not exist,
 *   -EINVAL if a phandle was not found, @cells_name could not be found.
 */
int oftree_count_phandle_with_args(oftree tree, ofnode node, const char *list_name,
				   const char *cells_name, int cell_count);

/**
 * ofnode_path() - find a node by full path
 *
 * This uses the control FDT.
 *
 * @path: Full path to node, e.g. "/bus/spi@1"
 * Return: reference to the node found. Use ofnode_valid() to check if it exists
 */
ofnode ofnode_path(const char *path);

/**
 * oftree_path() - find a node by full path from a root node
 *
 * @tree: Device tree to use
 * @path: Full path to node, e.g. "/bus/spi@1"
 * Return: reference to the node found. Use ofnode_valid() to check if it exists
 */
ofnode oftree_path(oftree tree, const char *path);

/**
 * oftree_root() - get the root node of a tree
 *
 * @tree: Device tree to use
 * Return: reference to the root node
 */
ofnode oftree_root(oftree tree);

/**
 * ofnode_read_chosen_prop() - get the value of a chosen property
 *
 * This looks for a property within the /chosen node and returns its value.
 *
 * This only works with the control FDT.
 *
 * @propname: Property name to look for
 * @sizep: Returns size of property, or  `FDT_ERR_...` error code if function
 *	returns NULL
 * Return: property value if found, else NULL
 */
const void *ofnode_read_chosen_prop(const char *propname, int *sizep);

/**
 * ofnode_read_chosen_string() - get the string value of a chosen property
 *
 * This looks for a property within the /chosen node and returns its value,
 * checking that it is a valid nul-terminated string
 *
 * This only works with the control FDT.
 *
 * @propname: Property name to look for
 * Return: string value if found, else NULL
 */
const char *ofnode_read_chosen_string(const char *propname);

/**
 * ofnode_get_chosen_node() - get a referenced node from the chosen node
 *
 * This looks up a named property in the chosen node and uses that as a path to
 * look up a code.
 *
 * This only works with the control FDT.
 *
 * @propname: Property name to look for
 * Return: the referenced node if present, else ofnode_null()
 */
ofnode ofnode_get_chosen_node(const char *propname);

/**
 * ofnode_read_baud() - get the baudrate from string value of chosen property
 *
 * This looks for stdout-path property within the /chosen node and parses its
 * value to return baudrate.
 *
 * This only works with the control FDT.
 *
 * Return: baudrate value if found, else -ve error code
 */
int ofnode_read_baud(void);

/**
 * ofnode_read_aliases_prop() - get the value of a aliases property
 *
 * This looks for a property within the /aliases node and returns its value
 *
 * @propname: Property name to look for
 * @sizep: Returns size of property, or `FDT_ERR_...` error code if function
 *	returns NULL
 * Return: property value if found, else NULL
 */
const void *ofnode_read_aliases_prop(const char *propname, int *sizep);

/**
 * ofnode_get_aliases_node() - get a referenced node from the aliases node
 *
 * This looks up a named property in the aliases node and uses that as a path to
 * look up a code.
 *
 * This only works with the control FDT.
 *
 * @propname: Property name to look for
 * Return: the referenced node if present, else ofnode_null()
 */
ofnode ofnode_get_aliases_node(const char *propname);

struct display_timing;
/**
 * ofnode_decode_display_timing() - decode display timings
 *
 * Decode display timings from the supplied 'display-timings' node.
 * See doc/device-tree-bindings/video/display-timing.txt for binding
 * information.
 *
 * @node:	'display-timing' node containing the timing subnodes
 * @index:	Index number to read (0=first timing subnode)
 * @config:	Place to put timings
 * Return: 0 if OK, -FDT_ERR_NOTFOUND if not found
 */
int ofnode_decode_display_timing(ofnode node, int index,
				 struct display_timing *config);

/**
 * ofnode_decode_panel_timing() - decode display timings
 *
 * Decode panel timings from the supplied 'panel-timings' node.
 *
 * @node:	'display-timing' node containing the timing subnodes
 * @config:	Place to put timings
 * Return: 0 if OK, -FDT_ERR_NOTFOUND if not found
 */
int ofnode_decode_panel_timing(ofnode node,
			       struct display_timing *config);

/**
 * ofnode_get_property() - get a pointer to the value of a node property
 *
 * @node: node to read
 * @propname: property to read
 * @lenp: place to put length on success
 * Return: pointer to property value, or NULL if not found or empty
 */
const void *ofnode_get_property(ofnode node, const char *propname, int *lenp);

/**
 * ofnode_has_property() - check if a node has a named property
 *
 * @node: node to read
 * @propname: property to read
 * Return: true if the property exists in the node, false if not
 */
bool ofnode_has_property(ofnode node, const char *propname);

/**
 * ofnode_first_property()- get the reference of the first property
 *
 * Get reference to the first property of the node, it is used to iterate
 * and read all the property with ofprop_get_property().
 *
 * @node: node to read
 * @prop: place to put argument reference
 * Return: 0 if OK, -ve on error. -FDT_ERR_NOTFOUND if not found
 */
int ofnode_first_property(ofnode node, struct ofprop *prop);

/**
 * ofnode_next_property() - get the reference of the next property
 *
 * Get reference to the next property of the node, it is used to iterate
 * and read all the property with ofprop_get_property().
 *
 * @prop: reference of current argument and place to put reference of next one
 * Return: 0 if OK, -ve on error. -FDT_ERR_NOTFOUND if not found
 */
int ofnode_next_property(struct ofprop *prop);

/**
 * ofnode_for_each_prop() - iterate over all properties of a node
 *
 * @prop:	struct ofprop
 * @node:	node (lvalue, ofnode)
 *
 * This is a wrapper around a for loop and is used like this::
 *
 *   ofnode node;
 *   struct ofprop prop;
 *
 *   ofnode_for_each_prop(prop, node) {
 *       ...use prop...
 *   }
 *
 * Note that this is implemented as a macro and @prop is used as
 * iterator in the loop. The parent variable can be a constant or even a
 * literal.
 */
#define ofnode_for_each_prop(prop, node) \
	for (ofnode_first_property(node, &prop); \
	     ofprop_valid(&prop); \
	     ofnode_next_property(&prop))

/**
 * ofprop_get_property() - get a pointer to the value of a property
 *
 * Get value for the property identified by the provided reference.
 *
 * @prop: reference on property
 * @propname: If non-NULL, place to property name on success,
 * @lenp: If non-NULL, place to put length on success, or error code on failure
 * Return: pointer to property, or NULL if not found
 */
const void *ofprop_get_property(const struct ofprop *prop,
				const char **propname, int *lenp);

/**
 * ofnode_get_addr_size() - get address and size from a property
 *
 * This does no address translation. It simply reads an property that contains
 * an address and a size value, one after the other.
 *
 * @node: node to read from
 * @propname: property to read
 * @sizep: place to put size value (on success)
 * Return: address value, or FDT_ADDR_T_NONE on error
 */
fdt_addr_t ofnode_get_addr_size(ofnode node, const char *propname,
				fdt_size_t *sizep);

/**
 * ofnode_read_u8_array_ptr() - find an 8-bit array
 *
 * Look up a property in a node and return a pointer to its contents as a
 * byte array of given length. The property must have at least enough data
 * for the array (count bytes). It may have more, but this will be ignored.
 * The data is not copied.
 *
 * @node:	node to examine
 * @propname:	name of property to find
 * @sz:		number of array elements
 * Return:
 * pointer to byte array if found, or NULL if the property is not found or
 * there is not enough data
 */
const uint8_t *ofnode_read_u8_array_ptr(ofnode node, const char *propname,
					size_t sz);

/**
 * ofnode_read_pci_addr() - look up a PCI address
 *
 * Look at an address property in a node and return the PCI address which
 * corresponds to the given type in the form of fdt_pci_addr.
 * The property must hold one fdt_pci_addr with a lengh.
 *
 * @node:	node to examine
 * @type:	pci address type (FDT_PCI_SPACE_xxx)
 * @propname:	name of property to find
 * @addr:	returns pci address in the form of fdt_pci_addr
 * @size:	if non-null, returns register-space size
 * Return:
 * 0 if ok, -ENOENT if the property did not exist, -EINVAL if the
 * format of the property was invalid, -ENXIO if the requested
 * address type was not found
 */
int ofnode_read_pci_addr(ofnode node, enum fdt_pci_space type,
			 const char *propname, struct fdt_pci_addr *addr,
			 fdt_size_t *size);

/**
 * ofnode_read_pci_vendev() - look up PCI vendor and device id
 *
 * Look at the compatible property of a device node that represents a PCI
 * device and extract pci vendor id and device id from it.
 *
 * @node:	node to examine
 * @vendor:	vendor id of the pci device
 * @device:	device id of the pci device
 * Return: 0 if ok, negative on error
 */
int ofnode_read_pci_vendev(ofnode node, u16 *vendor, u16 *device);

/**
 * ofnode_read_eth_phy_id() - look up eth phy vendor and device id
 *
 * Look at the compatible property of a device node that represents a eth phy
 * device and extract phy vendor id and device id from it.
 *
 * @node:	node to examine
 * @vendor:	vendor id of the eth phy device
 * @device:	device id of the eth phy device
 * Return:	 0 if ok, negative on error
 */
int ofnode_read_eth_phy_id(ofnode node, u16 *vendor, u16 *device);

/**
 * ofnode_read_addr_cells() - Get the number of address cells for a node
 *
 * This walks back up the tree to find the closest #address-cells property
 * which controls the given node.
 *
 * @node: Node to check
 * Return: number of address cells this node uses
 */
int ofnode_read_addr_cells(ofnode node);

/**
 * ofnode_read_size_cells() - Get the number of size cells for a node
 *
 * This walks back up the tree to find the closest #size-cells property
 * which controls the given node.
 *
 * @node: Node to check
 * Return: number of size cells this node uses
 */
int ofnode_read_size_cells(ofnode node);

/**
 * ofnode_read_simple_addr_cells() - Get the address cells property in a node
 *
 * This function matches fdt_address_cells().
 *
 * @node: Node to check
 * Return: value of #address-cells property in this node, or 2 if none
 */
int ofnode_read_simple_addr_cells(ofnode node);

/**
 * ofnode_read_simple_size_cells() - Get the size cells property in a node
 *
 * This function matches fdt_size_cells().
 *
 * @node: Node to check
 * Return: value of #size-cells property in this node, or 2 if none
 */
int ofnode_read_simple_size_cells(ofnode node);

/**
 * ofnode_pre_reloc() - check if a node should be bound before relocation
 *
 * Device tree nodes can be marked as needing-to-be-bound in the loader stages
 * via special device tree properties.
 *
 * Before relocation this function can be used to check if nodes are required
 * in either SPL or TPL stages.
 *
 * After relocation and jumping into the real U-Boot binary it is possible to
 * determine if a node was bound in one of SPL/TPL stages.
 *
 * There are 4 settings currently in use
 * - bootph-some-ram: U-Boot proper pre-relocation phase
 * - bootph-all: all phases
 * Existing platforms only use it to indicate nodes needed in
 * SPL. Should probably be replaced by bootph-pre-ram for new platforms.
 * - bootph-pre-ram: SPL phase
 * - bootph-pre-sram: TPL phase
 *
 * @node: node to check
 * Return: true if node should be or was bound, false otherwise
 */
bool ofnode_pre_reloc(ofnode node);

/**
 * ofnode_read_resource() - Read a resource from a node
 *
 * Read resource information from a node at the given index
 *
 * @node: Node to read from
 * @index: Index of resource to read (0 = first)
 * @res: Returns resource that was read, on success
 * Return: 0 if OK, -ve on error
 */
int ofnode_read_resource(ofnode node, uint index, struct resource *res);

/**
 * ofnode_read_resource_byname() - Read a resource from a node by name
 *
 * Read resource information from a node matching the given name. This uses a
 * 'reg-names' string list property with the names matching the associated
 * 'reg' property list.
 *
 * @node: Node to read from
 * @name: Name of resource to read
 * @res: Returns resource that was read, on success
 * Return: 0 if OK, -ve on error
 */
int ofnode_read_resource_byname(ofnode node, const char *name,
				struct resource *res);

/**
 * ofnode_by_compatible() - Find the next compatible node
 *
 * Find the next node after @from that is compatible with @compat
 *
 * @from: ofnode to start from (use ofnode_null() to start at the beginning)
 * @compat: Compatible string to match
 * Return: ofnode found, or ofnode_null() if none
 */
ofnode ofnode_by_compatible(ofnode from, const char *compat);

/**
 * ofnode_by_prop_value() - Find the next node with given property value
 *
 * Find the next node after @from that has a @propname with a value
 * @propval and a length @proplen.
 *
 * @from: ofnode to start from. Use ofnode_null() to start at the
 * beginning, or the return value from oftree_root() to start at the first
 * child of the root
 * @propname: property name to check
 * @propval: property value to search for
 * @proplen: length of the value in propval
 * Return: ofnode found, or ofnode_null() if none
 */
ofnode ofnode_by_prop_value(ofnode from, const char *propname,
			    const void *propval, int proplen);

/**
 * ofnode_for_each_subnode() - iterate over all subnodes of a parent
 *
 * @node:       child node (ofnode, lvalue)
 * @parent:     parent node (ofnode)
 *
 * This is a wrapper around a for loop and is used like so::
 *
 *   ofnode node;
 *   ofnode_for_each_subnode(node, parent) {
 *       Use node
 *       ...
 *   }
 *
 * Note that this is implemented as a macro and @node is used as
 * iterator in the loop. The parent variable can be a constant or even a
 * literal.
 */
#define ofnode_for_each_subnode(node, parent) \
	for (node = ofnode_first_subnode(parent); \
	     ofnode_valid(node); \
	     node = ofnode_next_subnode(node))

/**
 * ofnode_for_each_compatible_node() - iterate over all nodes with a given
 *				       compatible string
 *
 * @node:       child node (ofnode, lvalue)
 * @compat:     compatible string to match
 *
 * This is a wrapper around a for loop and is used like so::
 *
 *   ofnode node;
 *   ofnode_for_each_compatible_node(node, parent, compatible) {
 *      Use node
 *      ...
 *   }
 *
 * Note that this is implemented as a macro and @node is used as
 * iterator in the loop.
 */
#define ofnode_for_each_compatible_node(node, compat) \
	for (node = ofnode_by_compatible(ofnode_null(), compat); \
	     ofnode_valid(node); \
	     node = ofnode_by_compatible(node, compat))

/**
 * ofnode_get_child_count() - get the child count of a ofnode
 *
 * @parent: valid node to get its child count
 * Return: the number of subnodes
 */
int ofnode_get_child_count(ofnode parent);

/**
 * ofnode_translate_address() - Translate a device-tree address
 *
 * Translate an address from the device-tree into a CPU physical address. This
 * function walks up the tree and applies the various bus mappings along the
 * way.
 *
 * @node: Device tree node giving the context in which to translate the address
 * @in_addr: pointer to the address to translate
 * Return: the translated address; OF_BAD_ADDR on error
 */
u64 ofnode_translate_address(ofnode node, const fdt32_t *in_addr);

/**
 * ofnode_translate_dma_address() - Translate a device-tree DMA address
 *
 * Translate a DMA address from the device-tree into a CPU physical address.
 * This function walks up the tree and applies the various bus mappings along
 * the way.
 *
 * @node: Device tree node giving the context in which to translate the
 *        DMA address
 * @in_addr: pointer to the DMA address to translate
 * Return: the translated DMA address; OF_BAD_ADDR on error
 */
u64 ofnode_translate_dma_address(ofnode node, const fdt32_t *in_addr);

/**
 * ofnode_get_dma_range() - get dma-ranges for a specific DT node
 *
 * Get DMA ranges for a specifc node, this is useful to perform bus->cpu and
 * cpu->bus address translations
 *
 * @node: Device tree node
 * @cpu: Pointer to variable storing the range's cpu address
 * @bus: Pointer to variable storing the range's bus address
 * @size: Pointer to variable storing the range's size
 * Return: translated DMA address or OF_BAD_ADDR on error
 */
int ofnode_get_dma_range(ofnode node, phys_addr_t *cpu, dma_addr_t *bus,
			 u64 *size);

/**
 * ofnode_device_is_compatible() - check if the node is compatible with compat
 *
 * This allows to check whether the node is comaptible with the compat.
 *
 * @node:	Device tree node for which compatible needs to be verified.
 * @compat:	Compatible string which needs to verified in the given node.
 * Return: true if OK, false if the compatible is not found
 */
int ofnode_device_is_compatible(ofnode node, const char *compat);

/**
 * ofnode_write_prop() - Set a property of a ofnode
 *
 * Note that if @copy is false, the value passed to the function is *not*
 * allocated by the function itself, but must be allocated by the caller if
 * necessary. However it does allocate memory for the property struct and name.
 *
 * @node:	The node for whose property should be set
 * @propname:	The name of the property to set
 * @value:	The new value of the property (must be valid prior to calling
 *		the function)
 * @len:	The length of the new value of the property
 * @copy: true to allocate memory for the value. This only has any effect with
 *	live tree, since flat tree handles this automatically. It allows a
 *	node's value to be written to the tree, without requiring that the
 *	caller allocate it
 * Return: 0 if successful, -ve on error
 */
int ofnode_write_prop(ofnode node, const char *propname, const void *value,
		      int len, bool copy);

/**
 * ofnode_write_string() - Set a string property of a ofnode
 *
 * Note that the value passed to the function is *not* allocated by the
 * function itself, but must be allocated by the caller if necessary.
 *
 * @node:	The node for whose string property should be set
 * @propname:	The name of the string property to set
 * @value:	The new value of the string property (must be valid prior to
 *		calling the function)
 * Return: 0 if successful, -ve on error
 */
int ofnode_write_string(ofnode node, const char *propname, const char *value);

/**
 * ofnode_write_u32() - Set an integer property of an ofnode
 *
 * @node:	The node for whose string property should be set
 * @propname:	The name of the string property to set
 * @value:	The new value of the 32-bit integer property
 * Return: 0 if successful, -ve on error
 */
int ofnode_write_u32(ofnode node, const char *propname, u32 value);

/**
 * ofnode_write_u64() - Set an integer property of an ofnode
 *
 * @node:	The node for whose string property should be set
 * @propname:	The name of the string property to set
 * @value:	The new value of the 64-bit integer property
 * Return: 0 if successful, -ve on error
 */
int ofnode_write_u64(ofnode node, const char *propname, u64 value);

/**
 * ofnode_write_bool() - Set a boolean property of an ofnode
 *
 * This either adds or deleted a property with a zero-length value
 *
 * @node:	The node for whose string property should be set
 * @propname:	The name of the string property to set
 * @value:	The new value of the boolean property
 * Return: 0 if successful, -ve on error
 */
int ofnode_write_bool(ofnode node, const char *propname, bool value);

/**
 * ofnode_delete_prop() - Delete a property
 *
 * @node:	Node containing the property to delete
 * @propname:	Name of property to delete
 * Return: 0 if successful, -ve on error
 */
int ofnode_delete_prop(ofnode node, const char *propname);

/**
 * ofnode_set_enabled() - Enable or disable a device tree node given by its
 *			  ofnode
 *
 * This function effectively sets the node's "status" property to either "okay"
 * or "disable", hence making it available for driver model initialization or
 * not.
 *
 * @node:	The node to enable
 * @value:	Flag that tells the function to either disable or enable the
 *		node
 * Return: 0 if successful, -ve on error
 */
int ofnode_set_enabled(ofnode node, bool value);

/**
 * ofnode_get_phy_node() - Get PHY node for a MAC (if not fixed-link)
 *
 * This function parses PHY handle from the Ethernet controller's ofnode
 * (trying all possible PHY handle property names), and returns the PHY ofnode.
 *
 * Before this is used, ofnode_phy_is_fixed_link() should be checked first, and
 * if the result to that is true, this function should not be called.
 *
 * @eth_node:	ofnode belonging to the Ethernet controller
 * Return: ofnode of the PHY, if it exists, otherwise an invalid ofnode
 */
ofnode ofnode_get_phy_node(ofnode eth_node);

/**
 * ofnode_read_phy_mode() - Read PHY connection type from a MAC node
 *
 * This function parses the "phy-mode" / "phy-connection-type" property and
 * returns the corresponding PHY interface type.
 *
 * @mac_node:	ofnode containing the property
 * Return: one of PHY_INTERFACE_MODE_* constants, PHY_INTERFACE_MODE_NA on
 *	   error
 */
phy_interface_t ofnode_read_phy_mode(ofnode mac_node);

#if CONFIG_IS_ENABLED(DM)
/**
 * ofnode_conf_read_bool() - Read a boolean value from the U-Boot config
 *
 * This reads a property from the /config node of the devicetree.
 *
 * This only works with the control FDT.
 *
 * See doc/device-tree-bindings/config.txt for bindings
 *
 * @prop_name:	property name to look up
 * Return: true, if it exists, false if not
 */
bool ofnode_conf_read_bool(const char *prop_name);

/**
 * ofnode_conf_read_int() - Read an integer value from the U-Boot config
 *
 * This reads a property from the /config node of the devicetree.
 *
 * See doc/device-tree-bindings/config.txt for bindings
 *
 * @prop_name: property name to look up
 * @default_val: default value to return if the property is not found
 * Return: integer value, if found, or @default_val if not
 */
int ofnode_conf_read_int(const char *prop_name, int default_val);

/**
 * ofnode_conf_read_str() - Read a string value from the U-Boot config
 *
 * This reads a property from the /config node of the devicetree.
 *
 * This only works with the control FDT.
 *
 * See doc/device-tree-bindings/config.txt for bindings
 *
 * @prop_name: property name to look up
 * Return: string value, if found, or NULL if not
 */
const char *ofnode_conf_read_str(const char *prop_name);

/**
 * ofnode_options_read_bool() - Read a boolean value from the U-Boot options
 *
 * This reads a property from the /options/u-boot/ node of the devicetree.
 *
 * This only works with the control FDT.
 *
 * See dtschema/schemas/options/u-boot.yaml in dt-schema project for bindings
 *
 * @prop_name:	property name to look up
 * Return: true, if it exists, false if not
 */
bool ofnode_options_read_bool(const char *prop_name);

/**
 * ofnode_options_read_int() - Read an integer value from the U-Boot options
 *
 * This reads a property from the /options/u-boot/ node of the devicetree.
 *
 * See dtschema/schemas/options/u-boot.yaml in dt-schema project for bindings
 *
 * @prop_name: property name to look up
 * @default_val: default value to return if the property is not found
 * Return: integer value, if found, or @default_val if not
 */
int ofnode_options_read_int(const char *prop_name, int default_val);

/**
 * ofnode_options_read_str() - Read a string value from the U-Boot options
 *
 * This reads a property from the /options/u-boot/ node of the devicetree.
 *
 * This only works with the control FDT.
 *
 * See dtschema/schemas/options/u-boot.yaml in dt-schema project for bindings
 *
 * @prop_name: property name to look up
 * Return: string value, if found, or NULL if not
 */
const char *ofnode_options_read_str(const char *prop_name);

/**
 * ofnode_options_get_by_phandle() - Get a ofnode from phandle from the U-Boot options
 *
 * This reads a property from the /options/u-boot/ node of the devicetree.
 *
 * This only works with the control FDT.
 *
 * See dtschema/schemas/options/u-boot.yaml in dt-schema project for bindings
 *
 * @prop_name: property name to look up
 * @nodep: pointer to ofnode where node is stored
 * Return: 0, if found, or negative error if not
 */
int ofnode_options_get_by_phandle(const char *prop_name, ofnode *nodep);

/**
 * ofnode_read_bootscript_address() - Read bootscr-address or bootscr-ram-offset
 *
 * @bootscr_address: pointer to 64bit address where bootscr-address property value
 * is stored
 * @bootscr_offset:  pointer to 64bit offset address where bootscr-ram-offset
 * property value is stored
 *
 * This reads a bootscr-address or bootscr-ram-offset property from
 * the /options/u-boot/ node of the devicetree. bootscr-address holds the full
 * address of the boot script file. bootscr-ram-offset holds the boot script
 * file offset from the start of the ram base address. When bootscr-address is
 * defined, bootscr-ram-offset property is ignored.
 *
 * This only works with the control FDT.
 *
 * Return: 0 if OK, -EINVAL if property is not found.
 */
int ofnode_read_bootscript_address(u64 *bootscr_address, u64 *bootscr_offset);

/**
 * ofnode_read_bootscript_flash() - Read bootscr-flash-offset/size
 *
 * @bootscr_flash_offset: pointer to 64bit offset where bootscr-flash-offset
 * property value is stored
 * @bootscr_flash_size: pointer to 64bit size where bootscr-flash-size property
 * value is stored
 *
 * This reads a bootscr-flash-offset and bootscr-flash-size properties from
 * the /options/u-boot/ node of the devicetree. bootscr-flash-offset holds
 * the offset of the boot script file from start of flash. bootscr-flash-size
 * holds the boot script size in flash. When bootscr-flash-size is not defined,
 * bootscr-flash-offset property is cleaned.
 *
 * This only works with the control FDT.
 *
 * Return: 0 if OK, -EINVAL if property is not found or incorrect.
 */
int ofnode_read_bootscript_flash(u64 *bootscr_flash_offset,
				 u64 *bootscr_flash_size);

#else /* CONFIG_DM */
static inline bool ofnode_conf_read_bool(const char *prop_name)
{
	return false;
}

static inline int ofnode_conf_read_int(const char *prop_name, int default_val)
{
	return default_val;
}

static inline const char *ofnode_conf_read_str(const char *prop_name)
{
	return NULL;
}

static inline int ofnode_read_bootscript_address(u64 *bootscr_address, u64 *bootscr_offset)
{
	return -EINVAL;
}

static inline int ofnode_read_bootscript_flash(u64 *bootscr_flash_offset,
					       u64 *bootscr_flash_size)
{
	return -EINVAL;
}

#endif /* CONFIG_DM */

/**
 * of_add_subnode() - add a new subnode to a node
 *
 * @parent:	parent node to add to
 * @name:	name of subnode (allocated by this function)
 * @nodep:	returns pointer to new subnode (valid if the function returns 0
 *	or -EEXIST)
 * Returns 0 if OK, -EEXIST if already exists, -ENOMEM if out of memory, other
 * -ve on other error
 */
int ofnode_add_subnode(ofnode parent, const char *name, ofnode *nodep);

/**
 * ofnode_copy_props() - copy all properties from one node to another
 *
 * Makes a copy of all properties from the source node to the destination node.
 * Existing properties in the destination node remain unchanged, except that
 * any with the same name are overwritten, including changing the size of the
 * property.
 *
 * For livetree, properties are copied / allocated, so the source tree does not
 * need to be present afterwards.
 *
 * @dst: Destination node to write properties to
 * @src: Source node to read properties from
 */
int ofnode_copy_props(ofnode dst, ofnode src);

/**
 * ofnode_copy_node() - Copy a node to another place
 *
 * If a node with this name already exists in dst_parent, this returns an
 * .error
 *
 * @dst_parent: Parent of the newly copied node
 * @name: Name to give the new node
 * @src: Source node to copy
 * @nodep: Returns the new node, or the existing node if there is one
 * Return: 0 if OK, -EEXIST if dst_parent already has a node with this parent
 */
int ofnode_copy_node(ofnode dst_parent, const char *name, ofnode src,
		     ofnode *nodep);

/**
 * ofnode_delete() - Delete a node
 *
 * Delete a node from the tree
 *
 * @nodep: Pointer to node to delete (set to ofnode_null() on success)
 * Return: 0 if OK, -ENOENT if the node does not exist, -EPERM if it is the root
 * node (wWhich cannot be removed), -EFAULT if the tree is broken (to_remove is
 * not a child of its parent),
 *
 */
int ofnode_delete(ofnode *nodep);

#endif