~funderscore blog cgit wiki get in touch
aboutsummaryrefslogtreecommitdiff
blob: c2615f72f4059b975994ba081f0138b8c7a87a0e (plain)
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
/* SPDX-License-Identifier: GPL-2.0+ */
/*
 * Function to read values from the device tree node attached to a udevice.
 *
 * Copyright (c) 2017 Google, Inc
 * Written by Simon Glass <sjg@chromium.org>
 */

#ifndef _DM_READ_H
#define _DM_READ_H

#include <linux/errno.h>

#include <dm/device.h>
#include <dm/fdtaddr.h>
#include <dm/ofnode.h>
#include <dm/uclass.h>

struct resource;

#if CONFIG_IS_ENABLED(OF_LIVE)
static inline const struct device_node *dev_np(const struct udevice *dev)
{
	return ofnode_to_np(dev_ofnode(dev));
}
#else
static inline const struct device_node *dev_np(const struct udevice *dev)
{
	return NULL;
}
#endif

#if !defined(CONFIG_DM_DEV_READ_INLINE) || CONFIG_IS_ENABLED(OF_PLATDATA)
/**
 * dev_read_u8() - read a 8-bit integer from a device's DT property
 *
 * @dev:	device to read DT 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 dev_read_u8(const struct udevice *dev, const char *propname, u8 *outp);

/**
 * dev_read_u8_default() - read a 8-bit integer from a device's DT property
 *
 * @dev:	device to read DT 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 dev_read_u8_default(const struct udevice *dev, const char *propname, u8 def);

/**
 * dev_read_u16() - read a 16-bit integer from a device's DT property
 *
 * @dev:	device to read DT 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 dev_read_u16(const struct udevice *dev, const char *propname, u16 *outp);

/**
 * dev_read_u16_default() - read a 16-bit integer from a device's DT property
 *
 * @dev:	device to read DT 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 dev_read_u16_default(const struct udevice *dev, const char *propname,
			 u16 def);

/**
 * dev_read_u32() - read a 32-bit integer from a device's DT property
 *
 * @dev:	device to read DT 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 dev_read_u32(const struct udevice *dev, const char *propname, u32 *outp);

/**
 * dev_read_u32_default() - read a 32-bit integer from a device's DT property
 *
 * @dev:	device to read DT 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 dev_read_u32_default(const struct udevice *dev, const char *propname,
			 int def);

/**
 * dev_read_u32_index() - read an indexed 32-bit integer from a device's DT
 *                        property
 *
 * @dev:	device to read DT 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 dev_read_u32_index(struct udevice *dev, const char *propname, int index,
		       u32 *outp);

/**
 * dev_read_u32_index_default() - read an indexed 32-bit integer from a device's
 *                                DT property
 *
 * @dev:	device to read DT 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 dev_read_u32_index_default(struct udevice *dev, const char *propname,
			       int index, u32 def);

/**
 * dev_read_s32() - read a signed 32-bit integer from a device's DT property
 *
 * @dev:	device to read DT 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 dev_read_s32(const struct udevice *dev, const char *propname, s32 *outp);

/**
 * dev_read_s32_default() - read a signed 32-bit int from a device's DT property
 *
 * @dev:	device to read DT 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 dev_read_s32_default(const struct udevice *dev, const char *propname,
			 int def);

/**
 * dev_read_u32u() - read a 32-bit integer from a device's DT property
 *
 * This version uses a standard uint type.
 *
 * @dev:	device to read DT 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 dev_read_u32u(const struct udevice *dev, const char *propname, uint *outp);

/**
 * dev_read_u64() - read a 64-bit integer from a device's DT property
 *
 * @dev:        device to read DT 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 dev_read_u64(const struct udevice *dev, const char *propname, u64 *outp);

/**
 * dev_read_u64_default() - read a 64-bit integer from a device's DT property
 *
 * @dev:        device to read DT 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 dev_read_u64_default(const struct udevice *dev, const char *propname,
			 u64 def);

/**
 * dev_read_string() - Read a string from a device's DT property
 *
 * @dev:	device to read DT property from
 * @propname:	name of the property to read
 * Return: string from property value, or NULL if there is no such property
 */
const char *dev_read_string(const struct udevice *dev, const char *propname);

/**
 * dev_read_bool() - read a boolean value from a device's DT property
 *
 * @dev:	device to read DT property from
 * @propname:	name of property to read
 * Return: true if property is present (meaning true), false if not present
 */
bool dev_read_bool(const struct udevice *dev, const char *propname);

/**
 * dev_read_subnode() - find a named subnode of a device
 *
 * @dev:	device whose DT node contains the subnode
 * @subnode_name: name of subnode to find
 * Return: reference to subnode (which can be invalid if there is no such
 * subnode)
 */
ofnode dev_read_subnode(const struct udevice *dev, const char *subnode_name);

/**
 * dev_read_size() - read the size of a property
 *
 * @dev: device to check
 * @propname: property to check
 * Return: size of property if present, or -EINVAL if not
 */
int dev_read_size(const struct udevice *dev, const char *propname);

/**
 * dev_read_addr_index() - Get the indexed reg property of a device
 *
 * @dev: Device to read from
 * @index: the 'reg' property can hold a list of <addr, size> pairs
 *	   and @index is used to select which one is required
 *
 * Return: address or FDT_ADDR_T_NONE if not found
 */
fdt_addr_t dev_read_addr_index(const struct udevice *dev, int index);

/**
 * dev_read_addr_index_ptr() - Get the indexed reg property of a device
 *                             as a pointer
 *
 * @dev: Device to read from
 * @index: the 'reg' property can hold a list of <addr, size> pairs
 *	   and @index is used to select which one is required
 *
 * Return: pointer or NULL if not found
 */
void *dev_read_addr_index_ptr(const struct udevice *dev, int index);

/**
 * dev_read_addr_size_index() - Get the indexed reg property of a device
 *
 * @dev: Device to read from
 * @index: the 'reg' property can hold a list of <addr, size> pairs
 *	   and @index is used to select which one is required
 * @size: place to put size value (on success)
 *
 * Return: address or FDT_ADDR_T_NONE if not found
 */
fdt_addr_t dev_read_addr_size_index(const struct udevice *dev, int index,
				    fdt_size_t *size);

/**
 * dev_read_addr_size_index_ptr() - Get the indexed reg property of a device
 *                                  as a pointer
 *
 * @dev: Device to read from
 * @index: the 'reg' property can hold a list of <addr, size> pairs
 *	   and @index is used to select which one is required
 * @size: place to put size value (on success)
 *
 * Return: pointer or NULL if not found
 */
void *dev_read_addr_size_index_ptr(const struct udevice *dev, int index,
				   fdt_size_t *size);

/**
 * dev_remap_addr_index() - Get the indexed reg property of a device
 *                               as a memory-mapped I/O pointer
 *
 * @dev: Device to read from
 * @index: the 'reg' property can hold a list of <addr, size> pairs
 *	   and @index is used to select which one is required
 *
 * Return: pointer or NULL if not found
 */
void *dev_remap_addr_index(const struct udevice *dev, int index);

/**
 * dev_read_addr_name() - Get the reg property of a device, indexed by name
 *
 * @dev: Device to read from
 * @name: the 'reg' property can hold a list of <addr, size> pairs, with the
 *	  'reg-names' property providing named-based identification. @index
 *	  indicates the value to search for in 'reg-names'.
 *
 * Return: address or FDT_ADDR_T_NONE if not found
 */
fdt_addr_t dev_read_addr_name(const struct udevice *dev, const char *name);

/**
 * dev_read_addr_size_name() - Get the reg property of a device, indexed by name
 *
 * @dev: Device to read from
 * @name: the 'reg' property can hold a list of <addr, size> pairs, with the
 *	  'reg-names' property providing named-based identification. @index
 *	  indicates the value to search for in 'reg-names'.
 *  @size: place to put size value (on success)
 *
 * Return: address or FDT_ADDR_T_NONE if not found
 */
fdt_addr_t dev_read_addr_size_name(const struct udevice *dev, const char *name,
				   fdt_size_t *size);

/**
 * dev_remap_addr_name() - Get the reg property of a device, indexed by name,
 *                         as a memory-mapped I/O pointer
 *
 * @dev: Device to read from
 * @name: the 'reg' property can hold a list of <addr, size> pairs, with the
 *	  'reg-names' property providing named-based identification. @index
 *	  indicates the value to search for in 'reg-names'.
 *
 * Return: pointer or NULL if not found
 */
void *dev_remap_addr_name(const struct udevice *dev, const char *name);

/**
 * dev_read_addr() - Get the reg property of a device
 *
 * @dev: Device to read from
 *
 * Return: address or FDT_ADDR_T_NONE if not found
 */
fdt_addr_t dev_read_addr(const struct udevice *dev);

/**
 * dev_read_addr_ptr() - Get the reg property of a device
 *                       as a pointer
 *
 * @dev: Device to read from
 *
 * Return: pointer or NULL if not found
 */
void *dev_read_addr_ptr(const struct udevice *dev);

/**
 * dev_read_addr_pci() - Read an address and handle PCI address translation
 *
 * At present U-Boot does not have address translation logic for PCI in the
 * livetree implementation (of_addr.c). This special function supports this for
 * the flat tree implementation.
 *
 * This function should be removed (and code should use dev_read() instead)
 * once:
 *
 * 1. PCI address translation is added; and either
 * 2. everything uses livetree where PCI translation is used (which is feasible
 *    in SPL and U-Boot proper) or PCI address translation is added to
 *    fdtdec_get_addr() and friends.
 *
 * @dev: Device to read from
 * Return: address or FDT_ADDR_T_NONE if not found
 */
fdt_addr_t dev_read_addr_pci(const struct udevice *dev);

/**
 * dev_remap_addr() - Get the reg property of a device as a
 *                         memory-mapped I/O pointer
 *
 * @dev: Device to read from
 *
 * Return: pointer or NULL if not found
 */
void *dev_remap_addr(const struct udevice *dev);

/**
 * dev_read_addr_size() - Get the reg property of a device
 *
 * @dev: Device to read from
 * @sizep: place to put size value (on success)
 * Return: address value, or FDT_ADDR_T_NONE on error
 */
fdt_addr_t dev_read_addr_size(const struct udevice *dev, fdt_size_t *sizep);

/**
 * dev_read_name() - get the name of a device's node
 *
 * @dev: Device to read from
 * Return: name of node
 */
const char *dev_read_name(const struct udevice *dev);

/**
 * dev_read_stringlist_search() - find 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.
 *
 * @dev: device 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 dev_read_stringlist_search(const struct udevice *dev, const char *propname,
			       const char *string);

/**
 * dev_read_string_index() - obtain an indexed string from a string list
 *
 * @dev: device to examine
 * @propname: name of the property containing the string list
 * @index: index of the string to return
 * @outp: return location for the string
 *
 * Return:
 *   length of string, if found or -ve error value if not found
 */
int dev_read_string_index(const struct udevice *dev, const char *propname,
			  int index, const char **outp);

/**
 * dev_read_string_count() - find the number of strings in a string list
 *
 * @dev: device to examine
 * @propname: name of the property containing the string list
 * Return:
 *   number of strings in the list, or -ve error value if not found
 */
int dev_read_string_count(const struct udevice *dev, const char *propname);

/**
 * dev_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.
 *
 * @dev: device to examine
 * @propname: 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,
 * -ENOENT if no such property
 */
int dev_read_string_list(const struct udevice *dev, const char *propname,
			 const char ***listp);

/**
 * dev_read_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:
 * dev_read_phandle_with_args(dev, "list", "#list-cells", 0, 1, &args);
 *
 * @dev:	device whose 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 dev_read_phandle_with_args(const struct udevice *dev, const char *list_name,
			       const char *cells_name, int cell_count,
			       int index, struct ofnode_phandle_args *out_args);

/**
 * dev_count_phandle_with_args() - Return phandle number in a list
 *
 * This function is usefull to get phandle number contained in a property list.
 * For example, this allows to allocate the right amount of memory to keep
 * clock's reference contained into the "clocks" property.
 *
 * @dev:	device whose 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 found on success, on error returns appropriate
 * errno value.
 */

int dev_count_phandle_with_args(const struct udevice *dev,
				const char *list_name, const char *cells_name,
				int cell_count);

/**
 * dev_read_addr_cells() - Get the number of address cells for a device's node
 *
 * This walks back up the tree to find the closest #address-cells property
 * which controls the given node.
 *
 * @dev: device to check
 * Return: number of address cells this node uses
 */
int dev_read_addr_cells(const struct udevice *dev);

/**
 * dev_read_size_cells() - Get the number of size cells for a device's node
 *
 * This walks back up the tree to find the closest #size-cells property
 * which controls the given node.
 *
 * @dev: device to check
 * Return: number of size cells this node uses
 */
int dev_read_size_cells(const struct udevice *dev);

/**
 * dev_read_addr_cells() - Get the address cells property in a node
 *
 * This function matches fdt_address_cells().
 *
 * @dev: device to check
 * Return: number of address cells this node uses
 */
int dev_read_simple_addr_cells(const struct udevice *dev);

/**
 * dev_read_size_cells() - Get the size cells property in a node
 *
 * This function matches fdt_size_cells().
 *
 * @dev: device to check
 * Return: number of size cells this node uses
 */
int dev_read_simple_size_cells(const struct udevice *dev);

/**
 * dev_read_phandle() - Get the phandle from a device
 *
 * @dev: device to check
 * Return: phandle (1 or greater), or 0 if no phandle or other error
 */
int dev_read_phandle(const struct udevice *dev);

/**
 * dev_read_prop()- - read a property from a device's node
 *
 * @dev: device to check
 * @propname: property to read
 * @lenp: place to put length on success
 * Return: pointer to property, or NULL if not found
 */
const void *dev_read_prop(const struct udevice *dev, const char *propname,
			  int *lenp);

/**
 * dev_read_first_prop()- 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 dev_read_prop_by_prop().
 *
 * @dev: device to check
 * @prop: place to put argument reference
 * Return: 0 if OK, -ve on error. -FDT_ERR_NOTFOUND if not found
 */
int dev_read_first_prop(const struct udevice *dev, 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 dev_read_prop_by_prop().
 *
 * @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 dev_read_next_prop(struct ofprop *prop);

/**
 * dev_read_prop_by_prop() - 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
 * Return: 0 if OK, -ve on error. -FDT_ERR_NOTFOUND if not found
 */
const void *dev_read_prop_by_prop(struct ofprop *prop,
				  const char **propname, int *lenp);

/**
 * dev_read_alias_seq() - Get the alias sequence number of a node
 *
 * This works out whether a node is pointed to by an alias, and if so, the
 * sequence number of that alias. Aliases are of the form <base><num> where
 * <num> is the sequence number. For example spi2 would be sequence number 2.
 *
 * @dev: device to look up
 * @devnump: set to the sequence number if one is found
 * Return: 0 if a sequence was found, -ve if not
 */
int dev_read_alias_seq(const struct udevice *dev, int *devnump);

/**
 * dev_read_u32_array() - Find and read an array of 32 bit integers
 *
 * 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.
 *
 * @dev: device to look up
 * @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.
 */
int dev_read_u32_array(const struct udevice *dev, const char *propname,
		       u32 *out_values, size_t sz);

/**
 * dev_read_first_subnode() - find the first subnode of a device's node
 *
 * @dev: device to look up
 * Return: reference to the first subnode (which can be invalid if the device's
 * node has no subnodes)
 */
ofnode dev_read_first_subnode(const struct udevice *dev);

/**
 * 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 dev_read_next_subnode(ofnode node);

/**
 * dev_read_u8_array_ptr() - find an 8-bit array
 *
 * Look up a device's node property 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.
 *
 * @dev: device to look up
 * @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 *dev_read_u8_array_ptr(const struct udevice *dev,
				     const char *propname, size_t sz);

/**
 * dev_read_enabled() - check whether a node is enabled
 *
 * This looks for a 'status' property. If this exists, then returns 1 if
 * the status is 'ok' and 0 otherwise. If there is no status property,
 * it returns 1 on the assumption that anything mentioned should be enabled
 * by default.
 *
 * @dev: device to examine
 * Return: integer value 0 (not enabled) or 1 (enabled)
 */
int dev_read_enabled(const struct udevice *dev);

/**
 * dev_read_resource() - obtain an indexed resource from a device.
 *
 * @dev: device to examine
 * @index: index of the resource to retrieve (0 = first)
 * @res: returns the resource
 * Return: 0 if ok, negative on error
 */
int dev_read_resource(const struct udevice *dev, uint index,
		      struct resource *res);

/**
 * dev_read_resource_byname() - obtain a named resource from a device.
 *
 * @dev: device to examine
 * @name: name of the resource to retrieve
 * @res: returns the resource
 * Return: 0 if ok, negative on error
 */
int dev_read_resource_byname(const struct udevice *dev, const char *name,
			     struct resource *res);

/**
 * dev_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.
 *
 * @dev: device 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 dev_translate_address(const struct udevice *dev, const fdt32_t *in_addr);

/**
 * dev_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.
 *
 * @dev: device 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 dev_translate_dma_address(const struct udevice *dev,
			      const fdt32_t *in_addr);

/**
 * dev_get_dma_range() - Get a device's DMA constraints
 *
 * Provide the address bases and size of the linear mapping between the CPU and
 * a device's BUS address space.
 *
 * @dev: device giving the context in which to translate the DMA address
 * @cpu: base address for CPU's view of memory
 * @bus: base address for BUS's view of memory
 * @size: size of the address space
 * Return: 0 if ok, negative on error
 */
int dev_get_dma_range(const struct udevice *dev, phys_addr_t *cpu,
		      dma_addr_t *bus, u64 *size);

/**
 * dev_read_alias_highest_id - Get highest alias id for the given stem
 * @stem:	Alias stem to be examined
 *
 * The function travels the lookup table to get the highest alias id for the
 * given alias stem.
 * Return: alias ID, if found, else -1
 */
int dev_read_alias_highest_id(const char *stem);

/**
 * dev_get_child_count() - get the child count of a device
 *
 * @dev: device to use for interation (`struct udevice *`)
 * Return: the count of child subnode
 */
int dev_get_child_count(const struct udevice *dev);

/**
 * dev_read_pci_bus_range - Read PCI bus-range resource
 *
 * Look at the bus range property of a device node and return the pci bus
 * range for this node.
 *
 * @dev: device to examine
 * @res: returns the resource
 * Return: 0 if ok, negative on error
 */
int dev_read_pci_bus_range(const struct udevice *dev, struct resource *res);

/**
 * dev_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.
 *
 * @dev: device to read DT display timings from. The node linked to the device
 *       contains a child node called 'display-timings' which in turn contains
 *       one or more display timing nodes.
 * @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 dev_decode_display_timing(const struct udevice *dev, int index,
			      struct display_timing *config);

/**
 * dev_decode_panel_timing() - decode panel timings
 *
 * Decode display timings from the supplied 'panel-timings' node.
 *
 * @dev: device to read DT display timings from. The node linked to the device
 *       contains a child node called 'display-timings' which in turn contains
 *       one or more display timing nodes.
 * @config: place to put timings
 * Return: 0 if OK, -FDT_ERR_NOTFOUND if not found
 */
int dev_decode_panel_timing(const struct udevice *dev,
			    struct display_timing *config);

/**
 * dev_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.
 *
 * @dev: device representing the MAC
 * Return: ofnode of the PHY, if it exists, otherwise an invalid ofnode
 */
ofnode dev_get_phy_node(const struct udevice *dev);

/**
 * dev_read_phy_mode() - Read PHY connection type from a MAC
 *
 * This function parses the "phy-mode" / "phy-connection-type" property and
 * returns the corresponding PHY interface type.
 *
 * @dev: device representing the MAC
 * Return: one of PHY_INTERFACE_MODE_* constants, PHY_INTERFACE_MODE_NA on
 *	   error
 */
phy_interface_t dev_read_phy_mode(const struct udevice *dev);

#else /* CONFIG_DM_DEV_READ_INLINE is enabled */
#include <asm/global_data.h>

static inline int dev_read_u8(const struct udevice *dev,
			      const char *propname, u8 *outp)
{
	return ofnode_read_u8(dev_ofnode(dev), propname, outp);
}

static inline int dev_read_u8_default(const struct udevice *dev,
				      const char *propname, u8 def)
{
	return ofnode_read_u8_default(dev_ofnode(dev), propname, def);
}

static inline int dev_read_u16(const struct udevice *dev,
			       const char *propname, u16 *outp)
{
	return ofnode_read_u16(dev_ofnode(dev), propname, outp);
}

static inline int dev_read_u16_default(const struct udevice *dev,
				       const char *propname, u16 def)
{
	return ofnode_read_u16_default(dev_ofnode(dev), propname, def);
}

static inline int dev_read_u32(const struct udevice *dev,
			       const char *propname, u32 *outp)
{
	return ofnode_read_u32(dev_ofnode(dev), propname, outp);
}

static inline int dev_read_u32_default(const struct udevice *dev,
				       const char *propname, int def)
{
	return ofnode_read_u32_default(dev_ofnode(dev), propname, def);
}

static inline int dev_read_u32_index(struct udevice *dev,
				     const char *propname, int index, u32 *outp)
{
	return ofnode_read_u32_index(dev_ofnode(dev), propname, index, outp);
}

static inline u32 dev_read_u32_index_default(struct udevice *dev,
					     const char *propname, int index,
					     u32 def)
{
	return ofnode_read_u32_index_default(dev_ofnode(dev), propname, index,
					     def);
}

static inline int dev_read_s32(const struct udevice *dev,
			       const char *propname, s32 *outp)
{
	return ofnode_read_s32(dev_ofnode(dev), propname, outp);
}

static inline int dev_read_s32_default(const struct udevice *dev,
				       const char *propname, int def)
{
	return ofnode_read_s32_default(dev_ofnode(dev), propname, def);
}

static inline int dev_read_u32u(const struct udevice *dev,
				const char *propname, uint *outp)
{
	u32 val;
	int ret;

	ret = ofnode_read_u32(dev_ofnode(dev), propname, &val);
	if (ret)
		return ret;
	*outp = val;

	return 0;
}

static inline int dev_read_u64(const struct udevice *dev,
			       const char *propname, u64 *outp)
{
	return ofnode_read_u64(dev_ofnode(dev), propname, outp);
}

static inline u64 dev_read_u64_default(const struct udevice *dev,
				       const char *propname, u64 def)
{
	return ofnode_read_u64_default(dev_ofnode(dev), propname, def);
}

static inline const char *dev_read_string(const struct udevice *dev,
					  const char *propname)
{
	return ofnode_read_string(dev_ofnode(dev), propname);
}

static inline bool dev_read_bool(const struct udevice *dev,
				 const char *propname)
{
	return ofnode_read_bool(dev_ofnode(dev), propname);
}

static inline ofnode dev_read_subnode(const struct udevice *dev,
				      const char *subbnode_name)
{
	return ofnode_find_subnode(dev_ofnode(dev), subbnode_name);
}

static inline int dev_read_size(const struct udevice *dev, const char *propname)
{
	return ofnode_read_size(dev_ofnode(dev), propname);
}

static inline fdt_addr_t dev_read_addr_index(const struct udevice *dev,
					     int index)
{
	return devfdt_get_addr_index(dev, index);
}

static inline void *dev_read_addr_index_ptr(const struct udevice *dev,
					    int index)
{
	return devfdt_get_addr_index_ptr(dev, index);
}

static inline fdt_addr_t dev_read_addr_size_index(const struct udevice *dev,
						  int index,
						  fdt_size_t *size)
{
	return devfdt_get_addr_size_index(dev, index, size);
}

static inline void *dev_read_addr_size_index_ptr(const struct udevice *dev,
						 int index,
						 fdt_size_t *size)
{
	return devfdt_get_addr_size_index_ptr(dev, index, size);
}

static inline fdt_addr_t dev_read_addr_name(const struct udevice *dev,
					    const char *name)
{
	return devfdt_get_addr_name(dev, name);
}

static inline fdt_addr_t dev_read_addr_size_name(const struct udevice *dev,
						 const char *name,
						 fdt_size_t *size)
{
	return devfdt_get_addr_size_name(dev, name, size);
}

static inline fdt_addr_t dev_read_addr(const struct udevice *dev)
{
	return devfdt_get_addr(dev);
}

static inline void *dev_read_addr_ptr(const struct udevice *dev)
{
	return devfdt_get_addr_ptr(dev);
}

static inline fdt_addr_t dev_read_addr_pci(const struct udevice *dev)
{
	return devfdt_get_addr_pci(dev);
}

static inline void *dev_remap_addr(const struct udevice *dev)
{
	return devfdt_remap_addr(dev);
}

static inline void *dev_remap_addr_index(const struct udevice *dev, int index)
{
	return devfdt_remap_addr_index(dev, index);
}

static inline void *dev_remap_addr_name(const struct udevice *dev,
					const char *name)
{
	return devfdt_remap_addr_name(dev, name);
}

static inline fdt_addr_t dev_read_addr_size(const struct udevice *dev,
					    fdt_size_t *sizep)
{
	return dev_read_addr_size_index(dev, 0, sizep);
}

static inline const char *dev_read_name(const struct udevice *dev)
{
	return ofnode_get_name(dev_ofnode(dev));
}

static inline int dev_read_stringlist_search(const struct udevice *dev,
					     const char *propname,
					     const char *string)
{
	return ofnode_stringlist_search(dev_ofnode(dev), propname, string);
}

static inline int dev_read_string_index(const struct udevice *dev,
					const char *propname, int index,
					const char **outp)
{
	return ofnode_read_string_index(dev_ofnode(dev), propname, index, outp);
}

static inline int dev_read_string_count(const struct udevice *dev,
					const char *propname)
{
	return ofnode_read_string_count(dev_ofnode(dev), propname);
}

static inline int dev_read_string_list(const struct udevice *dev,
				       const char *propname,
				       const char ***listp)
{
	return ofnode_read_string_list(dev_ofnode(dev), propname, listp);
}

static inline int dev_read_phandle_with_args(const struct udevice *dev,
		const char *list_name, const char *cells_name, int cell_count,
		int index, struct ofnode_phandle_args *out_args)
{
	return ofnode_parse_phandle_with_args(dev_ofnode(dev), list_name,
					      cells_name, cell_count, index,
					      out_args);
}

static inline int dev_count_phandle_with_args(const struct udevice *dev,
		const char *list_name, const char *cells_name, int cell_count)
{
	return ofnode_count_phandle_with_args(dev_ofnode(dev), list_name,
					      cells_name, cell_count);
}

static inline int dev_read_addr_cells(const struct udevice *dev)
{
	int parent = fdt_parent_offset(gd->fdt_blob, dev_of_offset(dev));

	return fdt_address_cells(gd->fdt_blob, parent);
}

static inline int dev_read_size_cells(const struct udevice *dev)
{
	int parent = fdt_parent_offset(gd->fdt_blob, dev_of_offset(dev));

	return fdt_size_cells(gd->fdt_blob, parent);
}

static inline int dev_read_simple_addr_cells(const struct udevice *dev)
{
	return fdt_address_cells(gd->fdt_blob, dev_of_offset(dev));
}

static inline int dev_read_simple_size_cells(const struct udevice *dev)
{
	return fdt_size_cells(gd->fdt_blob, dev_of_offset(dev));
}

static inline int dev_read_phandle(const struct udevice *dev)
{
	return fdt_get_phandle(gd->fdt_blob, dev_of_offset(dev));
}

static inline const void *dev_read_prop(const struct udevice *dev,
					const char *propname, int *lenp)
{
	return ofnode_get_property(dev_ofnode(dev), propname, lenp);
}

static inline int dev_read_first_prop(const struct udevice *dev, struct ofprop *prop)
{
	return ofnode_first_property(dev_ofnode(dev), prop);
}

static inline int dev_read_next_prop(struct ofprop *prop)
{
	return ofnode_next_property(prop);
}

static inline const void *dev_read_prop_by_prop(struct ofprop *prop,
						const char **propname,
						int *lenp)
{
	return ofprop_get_property(prop, propname, lenp);
}

static inline int dev_read_alias_seq(const struct udevice *dev, int *devnump)
{
#if CONFIG_IS_ENABLED(OF_CONTROL)
	return fdtdec_get_alias_seq(gd->fdt_blob, dev->uclass->uc_drv->name,
				    dev_of_offset(dev), devnump);
#else
	return -ENOTSUPP;
#endif
}

static inline int dev_read_u32_array(const struct udevice *dev,
				     const char *propname, u32 *out_values,
				     size_t sz)
{
	return ofnode_read_u32_array(dev_ofnode(dev), propname, out_values, sz);
}

static inline ofnode dev_read_first_subnode(const struct udevice *dev)
{
	return ofnode_first_subnode(dev_ofnode(dev));
}

static inline ofnode dev_read_next_subnode(ofnode node)
{
	return ofnode_next_subnode(node);
}

static inline const uint8_t *dev_read_u8_array_ptr(const struct udevice *dev,
						   const char *propname,
						   size_t sz)
{
	return ofnode_read_u8_array_ptr(dev_ofnode(dev), propname, sz);
}

static inline int dev_read_enabled(const struct udevice *dev)
{
	return fdtdec_get_is_enabled(gd->fdt_blob, dev_of_offset(dev));
}

static inline int dev_read_resource(const struct udevice *dev, uint index,
				    struct resource *res)
{
	return ofnode_read_resource(dev_ofnode(dev), index, res);
}

static inline int dev_read_resource_byname(const struct udevice *dev,
					   const char *name,
					   struct resource *res)
{
	return ofnode_read_resource_byname(dev_ofnode(dev), name, res);
}

static inline u64 dev_translate_address(const struct udevice *dev,
					const fdt32_t *in_addr)
{
	return ofnode_translate_address(dev_ofnode(dev), in_addr);
}

static inline u64 dev_translate_dma_address(const struct udevice *dev,
					    const fdt32_t *in_addr)
{
	return ofnode_translate_dma_address(dev_ofnode(dev), in_addr);
}

static inline int dev_get_dma_range(const struct udevice *dev, phys_addr_t *cpu,
				    dma_addr_t *bus, u64 *size)
{
	return ofnode_get_dma_range(dev_ofnode(dev), cpu, bus, size);
}

static inline int dev_read_alias_highest_id(const char *stem)
{
	if (!CONFIG_IS_ENABLED(OF_LIBFDT) || !gd->fdt_blob)
		return -1;
	return fdtdec_get_alias_highest_id(gd->fdt_blob, stem);
}

static inline int dev_get_child_count(const struct udevice *dev)
{
	return ofnode_get_child_count(dev_ofnode(dev));
}

static inline int dev_decode_display_timing(const struct udevice *dev,
					    int index,
					    struct display_timing *config)
{
	return ofnode_decode_display_timing(dev_ofnode(dev), index, config);
}

static inline int dev_decode_panel_timing(const struct udevice *dev,
					  struct display_timing *config)
{
	return ofnode_decode_panel_timing(dev_ofnode(dev), config);
}

static inline ofnode dev_get_phy_node(const struct udevice *dev)
{
	return ofnode_get_phy_node(dev_ofnode(dev));
}

static inline phy_interface_t dev_read_phy_mode(const struct udevice *dev)
{
	return ofnode_read_phy_mode(dev_ofnode(dev));
}

#endif /* CONFIG_DM_DEV_READ_INLINE */

/**
 * dev_for_each_subnode() - Helper function to iterate through subnodes
 *
 * This creates a for() loop which works through the subnodes in a device's
 * device-tree node.
 *
 * @subnode: ofnode holding the current subnode
 * @dev: device to use for interation (`struct udevice *`)
 */
#define dev_for_each_subnode(subnode, dev) \
	for (subnode = dev_read_first_subnode(dev); \
	     ofnode_valid(subnode); \
	     subnode = ofnode_next_subnode(subnode))

/**
 * dev_for_each_property() - Helper function to iterate through property
 *
 * This creates a for() loop which works through the property in a device's
 * device-tree node.
 *
 * @prop: struct ofprop holding the current property
 * @dev: device to use for interation (`struct udevice *`)
 */
#define dev_for_each_property(prop, dev) \
	for (int ret_prop = dev_read_first_prop(dev, &prop); \
	     !ret_prop; \
	     ret_prop = dev_read_next_prop(&prop))

#endif