0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022 #define pr_fmt(fmt) "OF: " fmt
0023
0024 #include <linux/of.h>
0025 #include <linux/of_address.h>
0026 #include <linux/of_device.h>
0027 #include <linux/of_graph.h>
0028 #include <linux/of_irq.h>
0029 #include <linux/string.h>
0030 #include <linux/moduleparam.h>
0031
0032 #include "of_private.h"
0033
0034
0035
0036
0037
0038
0039
0040
0041 bool of_graph_is_present(const struct device_node *node)
0042 {
0043 struct device_node *ports, *port;
0044
0045 ports = of_get_child_by_name(node, "ports");
0046 if (ports)
0047 node = ports;
0048
0049 port = of_get_child_by_name(node, "port");
0050 of_node_put(ports);
0051 of_node_put(port);
0052
0053 return !!port;
0054 }
0055 EXPORT_SYMBOL(of_graph_is_present);
0056
0057
0058
0059
0060
0061
0062
0063
0064
0065
0066
0067
0068
0069
0070
0071 int of_property_count_elems_of_size(const struct device_node *np,
0072 const char *propname, int elem_size)
0073 {
0074 struct property *prop = of_find_property(np, propname, NULL);
0075
0076 if (!prop)
0077 return -EINVAL;
0078 if (!prop->value)
0079 return -ENODATA;
0080
0081 if (prop->length % elem_size != 0) {
0082 pr_err("size of %s in node %pOF is not a multiple of %d\n",
0083 propname, np, elem_size);
0084 return -EINVAL;
0085 }
0086
0087 return prop->length / elem_size;
0088 }
0089 EXPORT_SYMBOL_GPL(of_property_count_elems_of_size);
0090
0091
0092
0093
0094
0095
0096
0097
0098
0099
0100
0101
0102
0103
0104
0105
0106
0107 static void *of_find_property_value_of_size(const struct device_node *np,
0108 const char *propname, u32 min, u32 max, size_t *len)
0109 {
0110 struct property *prop = of_find_property(np, propname, NULL);
0111
0112 if (!prop)
0113 return ERR_PTR(-EINVAL);
0114 if (!prop->value)
0115 return ERR_PTR(-ENODATA);
0116 if (prop->length < min)
0117 return ERR_PTR(-EOVERFLOW);
0118 if (max && prop->length > max)
0119 return ERR_PTR(-EOVERFLOW);
0120
0121 if (len)
0122 *len = prop->length;
0123
0124 return prop->value;
0125 }
0126
0127
0128
0129
0130
0131
0132
0133
0134
0135
0136
0137
0138
0139
0140
0141
0142
0143
0144 int of_property_read_u32_index(const struct device_node *np,
0145 const char *propname,
0146 u32 index, u32 *out_value)
0147 {
0148 const u32 *val = of_find_property_value_of_size(np, propname,
0149 ((index + 1) * sizeof(*out_value)),
0150 0,
0151 NULL);
0152
0153 if (IS_ERR(val))
0154 return PTR_ERR(val);
0155
0156 *out_value = be32_to_cpup(((__be32 *)val) + index);
0157 return 0;
0158 }
0159 EXPORT_SYMBOL_GPL(of_property_read_u32_index);
0160
0161
0162
0163
0164
0165
0166
0167
0168
0169
0170
0171
0172
0173
0174
0175
0176
0177
0178 int of_property_read_u64_index(const struct device_node *np,
0179 const char *propname,
0180 u32 index, u64 *out_value)
0181 {
0182 const u64 *val = of_find_property_value_of_size(np, propname,
0183 ((index + 1) * sizeof(*out_value)),
0184 0, NULL);
0185
0186 if (IS_ERR(val))
0187 return PTR_ERR(val);
0188
0189 *out_value = be64_to_cpup(((__be64 *)val) + index);
0190 return 0;
0191 }
0192 EXPORT_SYMBOL_GPL(of_property_read_u64_index);
0193
0194
0195
0196
0197
0198
0199
0200
0201
0202
0203
0204
0205
0206
0207
0208
0209
0210
0211
0212
0213
0214
0215
0216
0217
0218 int of_property_read_variable_u8_array(const struct device_node *np,
0219 const char *propname, u8 *out_values,
0220 size_t sz_min, size_t sz_max)
0221 {
0222 size_t sz, count;
0223 const u8 *val = of_find_property_value_of_size(np, propname,
0224 (sz_min * sizeof(*out_values)),
0225 (sz_max * sizeof(*out_values)),
0226 &sz);
0227
0228 if (IS_ERR(val))
0229 return PTR_ERR(val);
0230
0231 if (!sz_max)
0232 sz = sz_min;
0233 else
0234 sz /= sizeof(*out_values);
0235
0236 count = sz;
0237 while (count--)
0238 *out_values++ = *val++;
0239
0240 return sz;
0241 }
0242 EXPORT_SYMBOL_GPL(of_property_read_variable_u8_array);
0243
0244
0245
0246
0247
0248
0249
0250
0251
0252
0253
0254
0255
0256
0257
0258
0259
0260
0261
0262
0263
0264
0265
0266
0267
0268 int of_property_read_variable_u16_array(const struct device_node *np,
0269 const char *propname, u16 *out_values,
0270 size_t sz_min, size_t sz_max)
0271 {
0272 size_t sz, count;
0273 const __be16 *val = of_find_property_value_of_size(np, propname,
0274 (sz_min * sizeof(*out_values)),
0275 (sz_max * sizeof(*out_values)),
0276 &sz);
0277
0278 if (IS_ERR(val))
0279 return PTR_ERR(val);
0280
0281 if (!sz_max)
0282 sz = sz_min;
0283 else
0284 sz /= sizeof(*out_values);
0285
0286 count = sz;
0287 while (count--)
0288 *out_values++ = be16_to_cpup(val++);
0289
0290 return sz;
0291 }
0292 EXPORT_SYMBOL_GPL(of_property_read_variable_u16_array);
0293
0294
0295
0296
0297
0298
0299
0300
0301
0302
0303
0304
0305
0306
0307
0308
0309
0310
0311
0312
0313
0314
0315 int of_property_read_variable_u32_array(const struct device_node *np,
0316 const char *propname, u32 *out_values,
0317 size_t sz_min, size_t sz_max)
0318 {
0319 size_t sz, count;
0320 const __be32 *val = of_find_property_value_of_size(np, propname,
0321 (sz_min * sizeof(*out_values)),
0322 (sz_max * sizeof(*out_values)),
0323 &sz);
0324
0325 if (IS_ERR(val))
0326 return PTR_ERR(val);
0327
0328 if (!sz_max)
0329 sz = sz_min;
0330 else
0331 sz /= sizeof(*out_values);
0332
0333 count = sz;
0334 while (count--)
0335 *out_values++ = be32_to_cpup(val++);
0336
0337 return sz;
0338 }
0339 EXPORT_SYMBOL_GPL(of_property_read_variable_u32_array);
0340
0341
0342
0343
0344
0345
0346
0347
0348
0349
0350
0351
0352
0353
0354
0355
0356 int of_property_read_u64(const struct device_node *np, const char *propname,
0357 u64 *out_value)
0358 {
0359 const __be32 *val = of_find_property_value_of_size(np, propname,
0360 sizeof(*out_value),
0361 0,
0362 NULL);
0363
0364 if (IS_ERR(val))
0365 return PTR_ERR(val);
0366
0367 *out_value = of_read_number(val, 2);
0368 return 0;
0369 }
0370 EXPORT_SYMBOL_GPL(of_property_read_u64);
0371
0372
0373
0374
0375
0376
0377
0378
0379
0380
0381
0382
0383
0384
0385
0386
0387
0388
0389
0390
0391
0392
0393 int of_property_read_variable_u64_array(const struct device_node *np,
0394 const char *propname, u64 *out_values,
0395 size_t sz_min, size_t sz_max)
0396 {
0397 size_t sz, count;
0398 const __be32 *val = of_find_property_value_of_size(np, propname,
0399 (sz_min * sizeof(*out_values)),
0400 (sz_max * sizeof(*out_values)),
0401 &sz);
0402
0403 if (IS_ERR(val))
0404 return PTR_ERR(val);
0405
0406 if (!sz_max)
0407 sz = sz_min;
0408 else
0409 sz /= sizeof(*out_values);
0410
0411 count = sz;
0412 while (count--) {
0413 *out_values++ = of_read_number(val, 2);
0414 val += 2;
0415 }
0416
0417 return sz;
0418 }
0419 EXPORT_SYMBOL_GPL(of_property_read_variable_u64_array);
0420
0421
0422
0423
0424
0425
0426
0427
0428
0429
0430
0431
0432
0433
0434
0435
0436
0437
0438
0439
0440 int of_property_read_string(const struct device_node *np, const char *propname,
0441 const char **out_string)
0442 {
0443 const struct property *prop = of_find_property(np, propname, NULL);
0444 if (!prop)
0445 return -EINVAL;
0446 if (!prop->length)
0447 return -ENODATA;
0448 if (strnlen(prop->value, prop->length) >= prop->length)
0449 return -EILSEQ;
0450 *out_string = prop->value;
0451 return 0;
0452 }
0453 EXPORT_SYMBOL_GPL(of_property_read_string);
0454
0455
0456
0457
0458
0459
0460
0461
0462
0463
0464 int of_property_match_string(const struct device_node *np, const char *propname,
0465 const char *string)
0466 {
0467 const struct property *prop = of_find_property(np, propname, NULL);
0468 size_t l;
0469 int i;
0470 const char *p, *end;
0471
0472 if (!prop)
0473 return -EINVAL;
0474 if (!prop->value)
0475 return -ENODATA;
0476
0477 p = prop->value;
0478 end = p + prop->length;
0479
0480 for (i = 0; p < end; i++, p += l) {
0481 l = strnlen(p, end - p) + 1;
0482 if (p + l > end)
0483 return -EILSEQ;
0484 pr_debug("comparing %s with %s\n", string, p);
0485 if (strcmp(string, p) == 0)
0486 return i;
0487 }
0488 return -ENODATA;
0489 }
0490 EXPORT_SYMBOL_GPL(of_property_match_string);
0491
0492
0493
0494
0495
0496
0497
0498
0499
0500
0501
0502
0503 int of_property_read_string_helper(const struct device_node *np,
0504 const char *propname, const char **out_strs,
0505 size_t sz, int skip)
0506 {
0507 const struct property *prop = of_find_property(np, propname, NULL);
0508 int l = 0, i = 0;
0509 const char *p, *end;
0510
0511 if (!prop)
0512 return -EINVAL;
0513 if (!prop->value)
0514 return -ENODATA;
0515 p = prop->value;
0516 end = p + prop->length;
0517
0518 for (i = 0; p < end && (!out_strs || i < skip + sz); i++, p += l) {
0519 l = strnlen(p, end - p) + 1;
0520 if (p + l > end)
0521 return -EILSEQ;
0522 if (out_strs && i >= skip)
0523 *out_strs++ = p;
0524 }
0525 i -= skip;
0526 return i <= 0 ? -ENODATA : i;
0527 }
0528 EXPORT_SYMBOL_GPL(of_property_read_string_helper);
0529
0530 const __be32 *of_prop_next_u32(struct property *prop, const __be32 *cur,
0531 u32 *pu)
0532 {
0533 const void *curv = cur;
0534
0535 if (!prop)
0536 return NULL;
0537
0538 if (!cur) {
0539 curv = prop->value;
0540 goto out_val;
0541 }
0542
0543 curv += sizeof(*cur);
0544 if (curv >= prop->value + prop->length)
0545 return NULL;
0546
0547 out_val:
0548 *pu = be32_to_cpup(curv);
0549 return curv;
0550 }
0551 EXPORT_SYMBOL_GPL(of_prop_next_u32);
0552
0553 const char *of_prop_next_string(struct property *prop, const char *cur)
0554 {
0555 const void *curv = cur;
0556
0557 if (!prop)
0558 return NULL;
0559
0560 if (!cur)
0561 return prop->value;
0562
0563 curv += strlen(cur) + 1;
0564 if (curv >= prop->value + prop->length)
0565 return NULL;
0566
0567 return curv;
0568 }
0569 EXPORT_SYMBOL_GPL(of_prop_next_string);
0570
0571
0572
0573
0574
0575
0576
0577
0578 int of_graph_parse_endpoint(const struct device_node *node,
0579 struct of_endpoint *endpoint)
0580 {
0581 struct device_node *port_node = of_get_parent(node);
0582
0583 WARN_ONCE(!port_node, "%s(): endpoint %pOF has no parent node\n",
0584 __func__, node);
0585
0586 memset(endpoint, 0, sizeof(*endpoint));
0587
0588 endpoint->local_node = node;
0589
0590
0591
0592
0593 of_property_read_u32(port_node, "reg", &endpoint->port);
0594 of_property_read_u32(node, "reg", &endpoint->id);
0595
0596 of_node_put(port_node);
0597
0598 return 0;
0599 }
0600 EXPORT_SYMBOL(of_graph_parse_endpoint);
0601
0602
0603
0604
0605
0606
0607
0608
0609
0610 struct device_node *of_graph_get_port_by_id(struct device_node *parent, u32 id)
0611 {
0612 struct device_node *node, *port;
0613
0614 node = of_get_child_by_name(parent, "ports");
0615 if (node)
0616 parent = node;
0617
0618 for_each_child_of_node(parent, port) {
0619 u32 port_id = 0;
0620
0621 if (!of_node_name_eq(port, "port"))
0622 continue;
0623 of_property_read_u32(port, "reg", &port_id);
0624 if (id == port_id)
0625 break;
0626 }
0627
0628 of_node_put(node);
0629
0630 return port;
0631 }
0632 EXPORT_SYMBOL(of_graph_get_port_by_id);
0633
0634
0635
0636
0637
0638
0639
0640
0641
0642 struct device_node *of_graph_get_next_endpoint(const struct device_node *parent,
0643 struct device_node *prev)
0644 {
0645 struct device_node *endpoint;
0646 struct device_node *port;
0647
0648 if (!parent)
0649 return NULL;
0650
0651
0652
0653
0654
0655
0656 if (!prev) {
0657 struct device_node *node;
0658
0659 node = of_get_child_by_name(parent, "ports");
0660 if (node)
0661 parent = node;
0662
0663 port = of_get_child_by_name(parent, "port");
0664 of_node_put(node);
0665
0666 if (!port) {
0667 pr_err("graph: no port node found in %pOF\n", parent);
0668 return NULL;
0669 }
0670 } else {
0671 port = of_get_parent(prev);
0672 if (WARN_ONCE(!port, "%s(): endpoint %pOF has no parent node\n",
0673 __func__, prev))
0674 return NULL;
0675 }
0676
0677 while (1) {
0678
0679
0680
0681
0682
0683 endpoint = of_get_next_child(port, prev);
0684 if (endpoint) {
0685 of_node_put(port);
0686 return endpoint;
0687 }
0688
0689
0690 prev = NULL;
0691
0692 do {
0693 port = of_get_next_child(parent, port);
0694 if (!port)
0695 return NULL;
0696 } while (!of_node_name_eq(port, "port"));
0697 }
0698 }
0699 EXPORT_SYMBOL(of_graph_get_next_endpoint);
0700
0701
0702
0703
0704
0705
0706
0707
0708
0709
0710
0711 struct device_node *of_graph_get_endpoint_by_regs(
0712 const struct device_node *parent, int port_reg, int reg)
0713 {
0714 struct of_endpoint endpoint;
0715 struct device_node *node = NULL;
0716
0717 for_each_endpoint_of_node(parent, node) {
0718 of_graph_parse_endpoint(node, &endpoint);
0719 if (((port_reg == -1) || (endpoint.port == port_reg)) &&
0720 ((reg == -1) || (endpoint.id == reg)))
0721 return node;
0722 }
0723
0724 return NULL;
0725 }
0726 EXPORT_SYMBOL(of_graph_get_endpoint_by_regs);
0727
0728
0729
0730
0731
0732
0733
0734
0735 struct device_node *of_graph_get_remote_endpoint(const struct device_node *node)
0736 {
0737
0738 return of_parse_phandle(node, "remote-endpoint", 0);
0739 }
0740 EXPORT_SYMBOL(of_graph_get_remote_endpoint);
0741
0742
0743
0744
0745
0746
0747
0748
0749 struct device_node *of_graph_get_port_parent(struct device_node *node)
0750 {
0751 unsigned int depth;
0752
0753 if (!node)
0754 return NULL;
0755
0756
0757
0758
0759
0760 of_node_get(node);
0761
0762
0763 for (depth = 3; depth && node; depth--) {
0764 node = of_get_next_parent(node);
0765 if (depth == 2 && !of_node_name_eq(node, "ports"))
0766 break;
0767 }
0768 return node;
0769 }
0770 EXPORT_SYMBOL(of_graph_get_port_parent);
0771
0772
0773
0774
0775
0776
0777
0778
0779 struct device_node *of_graph_get_remote_port_parent(
0780 const struct device_node *node)
0781 {
0782 struct device_node *np, *pp;
0783
0784
0785 np = of_graph_get_remote_endpoint(node);
0786
0787 pp = of_graph_get_port_parent(np);
0788
0789 of_node_put(np);
0790
0791 return pp;
0792 }
0793 EXPORT_SYMBOL(of_graph_get_remote_port_parent);
0794
0795
0796
0797
0798
0799
0800
0801
0802 struct device_node *of_graph_get_remote_port(const struct device_node *node)
0803 {
0804 struct device_node *np;
0805
0806
0807 np = of_graph_get_remote_endpoint(node);
0808 if (!np)
0809 return NULL;
0810 return of_get_next_parent(np);
0811 }
0812 EXPORT_SYMBOL(of_graph_get_remote_port);
0813
0814 int of_graph_get_endpoint_count(const struct device_node *np)
0815 {
0816 struct device_node *endpoint;
0817 int num = 0;
0818
0819 for_each_endpoint_of_node(np, endpoint)
0820 num++;
0821
0822 return num;
0823 }
0824 EXPORT_SYMBOL(of_graph_get_endpoint_count);
0825
0826
0827
0828
0829
0830
0831
0832
0833
0834
0835 struct device_node *of_graph_get_remote_node(const struct device_node *node,
0836 u32 port, u32 endpoint)
0837 {
0838 struct device_node *endpoint_node, *remote;
0839
0840 endpoint_node = of_graph_get_endpoint_by_regs(node, port, endpoint);
0841 if (!endpoint_node) {
0842 pr_debug("no valid endpoint (%d, %d) for node %pOF\n",
0843 port, endpoint, node);
0844 return NULL;
0845 }
0846
0847 remote = of_graph_get_remote_port_parent(endpoint_node);
0848 of_node_put(endpoint_node);
0849 if (!remote) {
0850 pr_debug("no valid remote node\n");
0851 return NULL;
0852 }
0853
0854 if (!of_device_is_available(remote)) {
0855 pr_debug("not available for remote node\n");
0856 of_node_put(remote);
0857 return NULL;
0858 }
0859
0860 return remote;
0861 }
0862 EXPORT_SYMBOL(of_graph_get_remote_node);
0863
0864 static struct fwnode_handle *of_fwnode_get(struct fwnode_handle *fwnode)
0865 {
0866 return of_fwnode_handle(of_node_get(to_of_node(fwnode)));
0867 }
0868
0869 static void of_fwnode_put(struct fwnode_handle *fwnode)
0870 {
0871 of_node_put(to_of_node(fwnode));
0872 }
0873
0874 static bool of_fwnode_device_is_available(const struct fwnode_handle *fwnode)
0875 {
0876 return of_device_is_available(to_of_node(fwnode));
0877 }
0878
0879 static bool of_fwnode_device_dma_supported(const struct fwnode_handle *fwnode)
0880 {
0881 return true;
0882 }
0883
0884 static enum dev_dma_attr
0885 of_fwnode_device_get_dma_attr(const struct fwnode_handle *fwnode)
0886 {
0887 if (of_dma_is_coherent(to_of_node(fwnode)))
0888 return DEV_DMA_COHERENT;
0889 else
0890 return DEV_DMA_NON_COHERENT;
0891 }
0892
0893 static bool of_fwnode_property_present(const struct fwnode_handle *fwnode,
0894 const char *propname)
0895 {
0896 return of_property_read_bool(to_of_node(fwnode), propname);
0897 }
0898
0899 static int of_fwnode_property_read_int_array(const struct fwnode_handle *fwnode,
0900 const char *propname,
0901 unsigned int elem_size, void *val,
0902 size_t nval)
0903 {
0904 const struct device_node *node = to_of_node(fwnode);
0905
0906 if (!val)
0907 return of_property_count_elems_of_size(node, propname,
0908 elem_size);
0909
0910 switch (elem_size) {
0911 case sizeof(u8):
0912 return of_property_read_u8_array(node, propname, val, nval);
0913 case sizeof(u16):
0914 return of_property_read_u16_array(node, propname, val, nval);
0915 case sizeof(u32):
0916 return of_property_read_u32_array(node, propname, val, nval);
0917 case sizeof(u64):
0918 return of_property_read_u64_array(node, propname, val, nval);
0919 }
0920
0921 return -ENXIO;
0922 }
0923
0924 static int
0925 of_fwnode_property_read_string_array(const struct fwnode_handle *fwnode,
0926 const char *propname, const char **val,
0927 size_t nval)
0928 {
0929 const struct device_node *node = to_of_node(fwnode);
0930
0931 return val ?
0932 of_property_read_string_array(node, propname, val, nval) :
0933 of_property_count_strings(node, propname);
0934 }
0935
0936 static const char *of_fwnode_get_name(const struct fwnode_handle *fwnode)
0937 {
0938 return kbasename(to_of_node(fwnode)->full_name);
0939 }
0940
0941 static const char *of_fwnode_get_name_prefix(const struct fwnode_handle *fwnode)
0942 {
0943
0944 if (!to_of_node(fwnode)->parent)
0945 return "";
0946
0947 return "/";
0948 }
0949
0950 static struct fwnode_handle *
0951 of_fwnode_get_parent(const struct fwnode_handle *fwnode)
0952 {
0953 return of_fwnode_handle(of_get_parent(to_of_node(fwnode)));
0954 }
0955
0956 static struct fwnode_handle *
0957 of_fwnode_get_next_child_node(const struct fwnode_handle *fwnode,
0958 struct fwnode_handle *child)
0959 {
0960 return of_fwnode_handle(of_get_next_available_child(to_of_node(fwnode),
0961 to_of_node(child)));
0962 }
0963
0964 static struct fwnode_handle *
0965 of_fwnode_get_named_child_node(const struct fwnode_handle *fwnode,
0966 const char *childname)
0967 {
0968 const struct device_node *node = to_of_node(fwnode);
0969 struct device_node *child;
0970
0971 for_each_available_child_of_node(node, child)
0972 if (of_node_name_eq(child, childname))
0973 return of_fwnode_handle(child);
0974
0975 return NULL;
0976 }
0977
0978 static int
0979 of_fwnode_get_reference_args(const struct fwnode_handle *fwnode,
0980 const char *prop, const char *nargs_prop,
0981 unsigned int nargs, unsigned int index,
0982 struct fwnode_reference_args *args)
0983 {
0984 struct of_phandle_args of_args;
0985 unsigned int i;
0986 int ret;
0987
0988 if (nargs_prop)
0989 ret = of_parse_phandle_with_args(to_of_node(fwnode), prop,
0990 nargs_prop, index, &of_args);
0991 else
0992 ret = of_parse_phandle_with_fixed_args(to_of_node(fwnode), prop,
0993 nargs, index, &of_args);
0994 if (ret < 0)
0995 return ret;
0996 if (!args)
0997 return 0;
0998
0999 args->nargs = of_args.args_count;
1000 args->fwnode = of_fwnode_handle(of_args.np);
1001
1002 for (i = 0; i < NR_FWNODE_REFERENCE_ARGS; i++)
1003 args->args[i] = i < of_args.args_count ? of_args.args[i] : 0;
1004
1005 return 0;
1006 }
1007
1008 static struct fwnode_handle *
1009 of_fwnode_graph_get_next_endpoint(const struct fwnode_handle *fwnode,
1010 struct fwnode_handle *prev)
1011 {
1012 return of_fwnode_handle(of_graph_get_next_endpoint(to_of_node(fwnode),
1013 to_of_node(prev)));
1014 }
1015
1016 static struct fwnode_handle *
1017 of_fwnode_graph_get_remote_endpoint(const struct fwnode_handle *fwnode)
1018 {
1019 return of_fwnode_handle(
1020 of_graph_get_remote_endpoint(to_of_node(fwnode)));
1021 }
1022
1023 static struct fwnode_handle *
1024 of_fwnode_graph_get_port_parent(struct fwnode_handle *fwnode)
1025 {
1026 struct device_node *np;
1027
1028
1029 np = of_get_parent(to_of_node(fwnode));
1030 if (!np)
1031 return NULL;
1032
1033
1034 if (!of_node_name_eq(np, "ports"))
1035 return of_fwnode_handle(np);
1036
1037 return of_fwnode_handle(of_get_next_parent(np));
1038 }
1039
1040 static int of_fwnode_graph_parse_endpoint(const struct fwnode_handle *fwnode,
1041 struct fwnode_endpoint *endpoint)
1042 {
1043 const struct device_node *node = to_of_node(fwnode);
1044 struct device_node *port_node = of_get_parent(node);
1045
1046 endpoint->local_fwnode = fwnode;
1047
1048 of_property_read_u32(port_node, "reg", &endpoint->port);
1049 of_property_read_u32(node, "reg", &endpoint->id);
1050
1051 of_node_put(port_node);
1052
1053 return 0;
1054 }
1055
1056 static const void *
1057 of_fwnode_device_get_match_data(const struct fwnode_handle *fwnode,
1058 const struct device *dev)
1059 {
1060 return of_device_get_match_data(dev);
1061 }
1062
1063 static bool of_is_ancestor_of(struct device_node *test_ancestor,
1064 struct device_node *child)
1065 {
1066 of_node_get(child);
1067 while (child) {
1068 if (child == test_ancestor) {
1069 of_node_put(child);
1070 return true;
1071 }
1072 child = of_get_next_parent(child);
1073 }
1074 return false;
1075 }
1076
1077 static struct device_node *of_get_compat_node(struct device_node *np)
1078 {
1079 of_node_get(np);
1080
1081 while (np) {
1082 if (!of_device_is_available(np)) {
1083 of_node_put(np);
1084 np = NULL;
1085 }
1086
1087 if (of_find_property(np, "compatible", NULL))
1088 break;
1089
1090 np = of_get_next_parent(np);
1091 }
1092
1093 return np;
1094 }
1095
1096 static struct device_node *of_get_compat_node_parent(struct device_node *np)
1097 {
1098 struct device_node *parent, *node;
1099
1100 parent = of_get_parent(np);
1101 node = of_get_compat_node(parent);
1102 of_node_put(parent);
1103
1104 return node;
1105 }
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124 static int of_link_to_phandle(struct device_node *con_np,
1125 struct device_node *sup_np)
1126 {
1127 struct device *sup_dev;
1128 struct device_node *tmp_np = sup_np;
1129
1130
1131
1132
1133
1134 sup_np = of_get_compat_node(sup_np);
1135 if (!sup_np) {
1136 pr_debug("Not linking %pOFP to %pOFP - No device\n",
1137 con_np, tmp_np);
1138 return -ENODEV;
1139 }
1140
1141
1142
1143
1144
1145
1146 if (of_is_ancestor_of(con_np, sup_np)) {
1147 pr_debug("Not linking %pOFP to %pOFP - is descendant\n",
1148 con_np, sup_np);
1149 of_node_put(sup_np);
1150 return -EINVAL;
1151 }
1152
1153
1154
1155
1156
1157 sup_dev = get_dev_from_fwnode(&sup_np->fwnode);
1158 if (!sup_dev &&
1159 (of_node_check_flag(sup_np, OF_POPULATED) ||
1160 sup_np->fwnode.flags & FWNODE_FLAG_NOT_DEVICE)) {
1161 pr_debug("Not linking %pOFP to %pOFP - No struct device\n",
1162 con_np, sup_np);
1163 of_node_put(sup_np);
1164 return -ENODEV;
1165 }
1166 put_device(sup_dev);
1167
1168 fwnode_link_add(of_fwnode_handle(con_np), of_fwnode_handle(sup_np));
1169 of_node_put(sup_np);
1170
1171 return 0;
1172 }
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193 static struct device_node *parse_prop_cells(struct device_node *np,
1194 const char *prop_name, int index,
1195 const char *list_name,
1196 const char *cells_name)
1197 {
1198 struct of_phandle_args sup_args;
1199
1200 if (strcmp(prop_name, list_name))
1201 return NULL;
1202
1203 if (of_parse_phandle_with_args(np, list_name, cells_name, index,
1204 &sup_args))
1205 return NULL;
1206
1207 return sup_args.np;
1208 }
1209
1210 #define DEFINE_SIMPLE_PROP(fname, name, cells) \
1211 static struct device_node *parse_##fname(struct device_node *np, \
1212 const char *prop_name, int index) \
1213 { \
1214 return parse_prop_cells(np, prop_name, index, name, cells); \
1215 }
1216
1217 static int strcmp_suffix(const char *str, const char *suffix)
1218 {
1219 unsigned int len, suffix_len;
1220
1221 len = strlen(str);
1222 suffix_len = strlen(suffix);
1223 if (len <= suffix_len)
1224 return -1;
1225 return strcmp(str + len - suffix_len, suffix);
1226 }
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247 static struct device_node *parse_suffix_prop_cells(struct device_node *np,
1248 const char *prop_name, int index,
1249 const char *suffix,
1250 const char *cells_name)
1251 {
1252 struct of_phandle_args sup_args;
1253
1254 if (strcmp_suffix(prop_name, suffix))
1255 return NULL;
1256
1257 if (of_parse_phandle_with_args(np, prop_name, cells_name, index,
1258 &sup_args))
1259 return NULL;
1260
1261 return sup_args.np;
1262 }
1263
1264 #define DEFINE_SUFFIX_PROP(fname, suffix, cells) \
1265 static struct device_node *parse_##fname(struct device_node *np, \
1266 const char *prop_name, int index) \
1267 { \
1268 return parse_suffix_prop_cells(np, prop_name, index, suffix, cells); \
1269 }
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291 struct supplier_bindings {
1292 struct device_node *(*parse_prop)(struct device_node *np,
1293 const char *prop_name, int index);
1294 bool optional;
1295 bool node_not_dev;
1296 };
1297
1298 DEFINE_SIMPLE_PROP(clocks, "clocks", "#clock-cells")
1299 DEFINE_SIMPLE_PROP(interconnects, "interconnects", "#interconnect-cells")
1300 DEFINE_SIMPLE_PROP(iommus, "iommus", "#iommu-cells")
1301 DEFINE_SIMPLE_PROP(mboxes, "mboxes", "#mbox-cells")
1302 DEFINE_SIMPLE_PROP(io_channels, "io-channel", "#io-channel-cells")
1303 DEFINE_SIMPLE_PROP(interrupt_parent, "interrupt-parent", NULL)
1304 DEFINE_SIMPLE_PROP(dmas, "dmas", "#dma-cells")
1305 DEFINE_SIMPLE_PROP(power_domains, "power-domains", "#power-domain-cells")
1306 DEFINE_SIMPLE_PROP(hwlocks, "hwlocks", "#hwlock-cells")
1307 DEFINE_SIMPLE_PROP(extcon, "extcon", NULL)
1308 DEFINE_SIMPLE_PROP(nvmem_cells, "nvmem-cells", NULL)
1309 DEFINE_SIMPLE_PROP(phys, "phys", "#phy-cells")
1310 DEFINE_SIMPLE_PROP(wakeup_parent, "wakeup-parent", NULL)
1311 DEFINE_SIMPLE_PROP(pinctrl0, "pinctrl-0", NULL)
1312 DEFINE_SIMPLE_PROP(pinctrl1, "pinctrl-1", NULL)
1313 DEFINE_SIMPLE_PROP(pinctrl2, "pinctrl-2", NULL)
1314 DEFINE_SIMPLE_PROP(pinctrl3, "pinctrl-3", NULL)
1315 DEFINE_SIMPLE_PROP(pinctrl4, "pinctrl-4", NULL)
1316 DEFINE_SIMPLE_PROP(pinctrl5, "pinctrl-5", NULL)
1317 DEFINE_SIMPLE_PROP(pinctrl6, "pinctrl-6", NULL)
1318 DEFINE_SIMPLE_PROP(pinctrl7, "pinctrl-7", NULL)
1319 DEFINE_SIMPLE_PROP(pinctrl8, "pinctrl-8", NULL)
1320 DEFINE_SIMPLE_PROP(remote_endpoint, "remote-endpoint", NULL)
1321 DEFINE_SIMPLE_PROP(pwms, "pwms", "#pwm-cells")
1322 DEFINE_SIMPLE_PROP(resets, "resets", "#reset-cells")
1323 DEFINE_SIMPLE_PROP(leds, "leds", NULL)
1324 DEFINE_SIMPLE_PROP(backlight, "backlight", NULL)
1325 DEFINE_SUFFIX_PROP(regulators, "-supply", NULL)
1326 DEFINE_SUFFIX_PROP(gpio, "-gpio", "#gpio-cells")
1327
1328 static struct device_node *parse_gpios(struct device_node *np,
1329 const char *prop_name, int index)
1330 {
1331 if (!strcmp_suffix(prop_name, ",nr-gpios"))
1332 return NULL;
1333
1334 return parse_suffix_prop_cells(np, prop_name, index, "-gpios",
1335 "#gpio-cells");
1336 }
1337
1338 static struct device_node *parse_iommu_maps(struct device_node *np,
1339 const char *prop_name, int index)
1340 {
1341 if (strcmp(prop_name, "iommu-map"))
1342 return NULL;
1343
1344 return of_parse_phandle(np, prop_name, (index * 4) + 1);
1345 }
1346
1347 static struct device_node *parse_gpio_compat(struct device_node *np,
1348 const char *prop_name, int index)
1349 {
1350 struct of_phandle_args sup_args;
1351
1352 if (strcmp(prop_name, "gpio") && strcmp(prop_name, "gpios"))
1353 return NULL;
1354
1355
1356
1357
1358
1359 if (of_find_property(np, "gpio-hog", NULL))
1360 return NULL;
1361
1362 if (of_parse_phandle_with_args(np, prop_name, "#gpio-cells", index,
1363 &sup_args))
1364 return NULL;
1365
1366 return sup_args.np;
1367 }
1368
1369 static struct device_node *parse_interrupts(struct device_node *np,
1370 const char *prop_name, int index)
1371 {
1372 struct of_phandle_args sup_args;
1373
1374 if (!IS_ENABLED(CONFIG_OF_IRQ) || IS_ENABLED(CONFIG_PPC))
1375 return NULL;
1376
1377 if (strcmp(prop_name, "interrupts") &&
1378 strcmp(prop_name, "interrupts-extended"))
1379 return NULL;
1380
1381 return of_irq_parse_one(np, index, &sup_args) ? NULL : sup_args.np;
1382 }
1383
1384 static const struct supplier_bindings of_supplier_bindings[] = {
1385 { .parse_prop = parse_clocks, },
1386 { .parse_prop = parse_interconnects, },
1387 { .parse_prop = parse_iommus, .optional = true, },
1388 { .parse_prop = parse_iommu_maps, .optional = true, },
1389 { .parse_prop = parse_mboxes, },
1390 { .parse_prop = parse_io_channels, },
1391 { .parse_prop = parse_interrupt_parent, },
1392 { .parse_prop = parse_dmas, .optional = true, },
1393 { .parse_prop = parse_power_domains, },
1394 { .parse_prop = parse_hwlocks, },
1395 { .parse_prop = parse_extcon, },
1396 { .parse_prop = parse_nvmem_cells, },
1397 { .parse_prop = parse_phys, },
1398 { .parse_prop = parse_wakeup_parent, },
1399 { .parse_prop = parse_pinctrl0, },
1400 { .parse_prop = parse_pinctrl1, },
1401 { .parse_prop = parse_pinctrl2, },
1402 { .parse_prop = parse_pinctrl3, },
1403 { .parse_prop = parse_pinctrl4, },
1404 { .parse_prop = parse_pinctrl5, },
1405 { .parse_prop = parse_pinctrl6, },
1406 { .parse_prop = parse_pinctrl7, },
1407 { .parse_prop = parse_pinctrl8, },
1408 { .parse_prop = parse_remote_endpoint, .node_not_dev = true, },
1409 { .parse_prop = parse_pwms, },
1410 { .parse_prop = parse_resets, },
1411 { .parse_prop = parse_leds, },
1412 { .parse_prop = parse_backlight, },
1413 { .parse_prop = parse_gpio_compat, },
1414 { .parse_prop = parse_interrupts, },
1415 { .parse_prop = parse_regulators, },
1416 { .parse_prop = parse_gpio, },
1417 { .parse_prop = parse_gpios, },
1418 {}
1419 };
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440 static int of_link_property(struct device_node *con_np, const char *prop_name)
1441 {
1442 struct device_node *phandle;
1443 const struct supplier_bindings *s = of_supplier_bindings;
1444 unsigned int i = 0;
1445 bool matched = false;
1446
1447
1448 while (!matched && s->parse_prop) {
1449 if (s->optional && !fw_devlink_is_strict()) {
1450 s++;
1451 continue;
1452 }
1453
1454 while ((phandle = s->parse_prop(con_np, prop_name, i))) {
1455 struct device_node *con_dev_np;
1456
1457 con_dev_np = s->node_not_dev
1458 ? of_get_compat_node_parent(con_np)
1459 : of_node_get(con_np);
1460 matched = true;
1461 i++;
1462 of_link_to_phandle(con_dev_np, phandle);
1463 of_node_put(phandle);
1464 of_node_put(con_dev_np);
1465 }
1466 s++;
1467 }
1468 return 0;
1469 }
1470
1471 static void __iomem *of_fwnode_iomap(struct fwnode_handle *fwnode, int index)
1472 {
1473 #ifdef CONFIG_OF_ADDRESS
1474 return of_iomap(to_of_node(fwnode), index);
1475 #else
1476 return NULL;
1477 #endif
1478 }
1479
1480 static int of_fwnode_irq_get(const struct fwnode_handle *fwnode,
1481 unsigned int index)
1482 {
1483 return of_irq_get(to_of_node(fwnode), index);
1484 }
1485
1486 static int of_fwnode_add_links(struct fwnode_handle *fwnode)
1487 {
1488 struct property *p;
1489 struct device_node *con_np = to_of_node(fwnode);
1490
1491 if (IS_ENABLED(CONFIG_X86))
1492 return 0;
1493
1494 if (!con_np)
1495 return -EINVAL;
1496
1497 for_each_property_of_node(con_np, p)
1498 of_link_property(con_np, p->name);
1499
1500 return 0;
1501 }
1502
1503 const struct fwnode_operations of_fwnode_ops = {
1504 .get = of_fwnode_get,
1505 .put = of_fwnode_put,
1506 .device_is_available = of_fwnode_device_is_available,
1507 .device_get_match_data = of_fwnode_device_get_match_data,
1508 .device_dma_supported = of_fwnode_device_dma_supported,
1509 .device_get_dma_attr = of_fwnode_device_get_dma_attr,
1510 .property_present = of_fwnode_property_present,
1511 .property_read_int_array = of_fwnode_property_read_int_array,
1512 .property_read_string_array = of_fwnode_property_read_string_array,
1513 .get_name = of_fwnode_get_name,
1514 .get_name_prefix = of_fwnode_get_name_prefix,
1515 .get_parent = of_fwnode_get_parent,
1516 .get_next_child_node = of_fwnode_get_next_child_node,
1517 .get_named_child_node = of_fwnode_get_named_child_node,
1518 .get_reference_args = of_fwnode_get_reference_args,
1519 .graph_get_next_endpoint = of_fwnode_graph_get_next_endpoint,
1520 .graph_get_remote_endpoint = of_fwnode_graph_get_remote_endpoint,
1521 .graph_get_port_parent = of_fwnode_graph_get_port_parent,
1522 .graph_parse_endpoint = of_fwnode_graph_parse_endpoint,
1523 .iomap = of_fwnode_iomap,
1524 .irq_get = of_fwnode_irq_get,
1525 .add_links = of_fwnode_add_links,
1526 };
1527 EXPORT_SYMBOL_GPL(of_fwnode_ops);