Simon Glass | cfdae5c | 2017-05-18 20:09:04 -0600 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2017 Google, Inc |
| 3 | * Written by Simon Glass <sjg@chromium.org> |
| 4 | * |
| 5 | * SPDX-License-Identifier: GPL-2.0+ |
| 6 | */ |
| 7 | |
| 8 | #include <common.h> |
| 9 | #include <dm.h> |
| 10 | #include <dm/of_access.h> |
| 11 | |
| 12 | int dev_read_u32_default(struct udevice *dev, const char *propname, int def) |
| 13 | { |
| 14 | return ofnode_read_u32_default(dev_ofnode(dev), propname, def); |
| 15 | } |
| 16 | |
| 17 | const char *dev_read_string(struct udevice *dev, const char *propname) |
| 18 | { |
| 19 | return ofnode_read_string(dev_ofnode(dev), propname); |
| 20 | } |
| 21 | |
| 22 | bool dev_read_bool(struct udevice *dev, const char *propname) |
| 23 | { |
| 24 | return ofnode_read_bool(dev_ofnode(dev), propname); |
| 25 | } |
| 26 | |
| 27 | ofnode dev_read_subnode(struct udevice *dev, const char *subnode_name) |
| 28 | { |
| 29 | return ofnode_find_subnode(dev_ofnode(dev), subnode_name); |
| 30 | } |
| 31 | |
| 32 | ofnode dev_read_first_subnode(struct udevice *dev) |
| 33 | { |
| 34 | return ofnode_first_subnode(dev_ofnode(dev)); |
| 35 | } |
| 36 | |
| 37 | ofnode dev_read_next_subnode(ofnode node) |
| 38 | { |
| 39 | return ofnode_next_subnode(node); |
| 40 | } |
| 41 | |
| 42 | int dev_read_size(struct udevice *dev, const char *propname) |
| 43 | { |
| 44 | return ofnode_read_size(dev_ofnode(dev), propname); |
| 45 | } |
| 46 | |
| 47 | fdt_addr_t dev_read_addr_index(struct udevice *dev, int index) |
| 48 | { |
| 49 | if (ofnode_is_np(dev_ofnode(dev))) |
| 50 | return ofnode_get_addr_index(dev_ofnode(dev), index); |
| 51 | else |
| 52 | return devfdt_get_addr_index(dev, index); |
| 53 | } |
| 54 | |
| 55 | fdt_addr_t dev_read_addr(struct udevice *dev) |
| 56 | { |
| 57 | return dev_read_addr_index(dev, 0); |
| 58 | } |
| 59 | |
Philipp Tomsich | 7719b39 | 2017-09-11 22:04:12 +0200 | [diff] [blame] | 60 | void *dev_read_addr_ptr(struct udevice *dev) |
| 61 | { |
| 62 | fdt_addr_t addr = dev_read_addr(dev); |
| 63 | |
| 64 | return (addr == FDT_ADDR_T_NONE) ? NULL : (void *)addr; |
| 65 | } |
| 66 | |
Simon Glass | cfdae5c | 2017-05-18 20:09:04 -0600 | [diff] [blame] | 67 | fdt_addr_t dev_read_addr_size(struct udevice *dev, const char *property, |
| 68 | fdt_size_t *sizep) |
| 69 | { |
| 70 | return ofnode_get_addr_size(dev_ofnode(dev), property, sizep); |
| 71 | } |
| 72 | |
| 73 | const char *dev_read_name(struct udevice *dev) |
| 74 | { |
| 75 | return ofnode_get_name(dev_ofnode(dev)); |
| 76 | } |
| 77 | |
| 78 | int dev_read_stringlist_search(struct udevice *dev, const char *property, |
| 79 | const char *string) |
| 80 | { |
| 81 | return ofnode_stringlist_search(dev_ofnode(dev), property, string); |
| 82 | } |
| 83 | |
Jean-Jacques Hiblot | 8365ebd | 2017-09-21 17:03:09 +0200 | [diff] [blame] | 84 | int dev_read_string_index(struct udevice *dev, const char *propname, int index, |
| 85 | const char **outp) |
| 86 | { |
| 87 | return ofnode_read_string_index(dev_ofnode(dev), propname, index, outp); |
| 88 | } |
| 89 | |
| 90 | int dev_read_string_count(struct udevice *dev, const char *propname) |
| 91 | { |
| 92 | return ofnode_read_string_count(dev_ofnode(dev), propname); |
| 93 | } |
| 94 | |
Simon Glass | cfdae5c | 2017-05-18 20:09:04 -0600 | [diff] [blame] | 95 | int dev_read_phandle_with_args(struct udevice *dev, const char *list_name, |
| 96 | const char *cells_name, int cell_count, |
| 97 | int index, |
| 98 | struct ofnode_phandle_args *out_args) |
| 99 | { |
| 100 | return ofnode_parse_phandle_with_args(dev_ofnode(dev), list_name, |
| 101 | cells_name, cell_count, index, |
| 102 | out_args); |
| 103 | } |
| 104 | |
| 105 | int dev_read_addr_cells(struct udevice *dev) |
| 106 | { |
| 107 | return ofnode_read_addr_cells(dev_ofnode(dev)); |
| 108 | } |
| 109 | |
| 110 | int dev_read_size_cells(struct udevice *dev) |
| 111 | { |
| 112 | return ofnode_read_size_cells(dev_ofnode(dev)); |
| 113 | } |
| 114 | |
Simon Glass | 4191dc1 | 2017-06-12 06:21:31 -0600 | [diff] [blame] | 115 | int dev_read_simple_addr_cells(struct udevice *dev) |
| 116 | { |
| 117 | return ofnode_read_simple_addr_cells(dev_ofnode(dev)); |
| 118 | } |
| 119 | |
| 120 | int dev_read_simple_size_cells(struct udevice *dev) |
| 121 | { |
| 122 | return ofnode_read_simple_size_cells(dev_ofnode(dev)); |
| 123 | } |
| 124 | |
Simon Glass | cfdae5c | 2017-05-18 20:09:04 -0600 | [diff] [blame] | 125 | int dev_read_phandle(struct udevice *dev) |
| 126 | { |
| 127 | ofnode node = dev_ofnode(dev); |
| 128 | |
| 129 | if (ofnode_is_np(node)) |
| 130 | return ofnode_to_np(node)->phandle; |
| 131 | else |
| 132 | return fdt_get_phandle(gd->fdt_blob, ofnode_to_offset(node)); |
| 133 | } |
| 134 | |
Masahiro Yamada | baebec1 | 2017-07-17 12:18:39 +0900 | [diff] [blame] | 135 | const void *dev_read_prop(struct udevice *dev, const char *propname, int *lenp) |
Simon Glass | cfdae5c | 2017-05-18 20:09:04 -0600 | [diff] [blame] | 136 | { |
Masahiro Yamada | 9cf85cb | 2017-06-22 16:54:05 +0900 | [diff] [blame] | 137 | return ofnode_get_property(dev_ofnode(dev), propname, lenp); |
Simon Glass | cfdae5c | 2017-05-18 20:09:04 -0600 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | int dev_read_alias_seq(struct udevice *dev, int *devnump) |
| 141 | { |
| 142 | ofnode node = dev_ofnode(dev); |
| 143 | const char *uc_name = dev->uclass->uc_drv->name; |
| 144 | int ret; |
| 145 | |
| 146 | if (ofnode_is_np(node)) { |
| 147 | ret = of_alias_get_id(ofnode_to_np(node), uc_name); |
| 148 | if (ret >= 0) |
| 149 | *devnump = ret; |
| 150 | } else { |
| 151 | ret = fdtdec_get_alias_seq(gd->fdt_blob, uc_name, |
| 152 | ofnode_to_offset(node), devnump); |
| 153 | } |
| 154 | |
| 155 | return ret; |
| 156 | } |
| 157 | |
| 158 | int dev_read_u32_array(struct udevice *dev, const char *propname, |
| 159 | u32 *out_values, size_t sz) |
| 160 | { |
| 161 | return ofnode_read_u32_array(dev_ofnode(dev), propname, out_values, sz); |
| 162 | } |
| 163 | |
| 164 | const uint8_t *dev_read_u8_array_ptr(struct udevice *dev, const char *propname, |
| 165 | size_t sz) |
| 166 | { |
| 167 | return ofnode_read_u8_array_ptr(dev_ofnode(dev), propname, sz); |
| 168 | } |
Simon Glass | fa031f8 | 2017-06-12 06:21:30 -0600 | [diff] [blame] | 169 | |
| 170 | int dev_read_enabled(struct udevice *dev) |
| 171 | { |
| 172 | ofnode node = dev_ofnode(dev); |
| 173 | |
| 174 | if (ofnode_is_np(node)) |
| 175 | return of_device_is_available(ofnode_to_np(node)); |
| 176 | else |
| 177 | return fdtdec_get_is_enabled(gd->fdt_blob, |
| 178 | ofnode_to_offset(node)); |
| 179 | } |
Simon Glass | f7bfcc4 | 2017-07-25 08:29:55 -0600 | [diff] [blame] | 180 | |
| 181 | int dev_read_resource(struct udevice *dev, uint index, struct resource *res) |
| 182 | { |
| 183 | return ofnode_read_resource(dev_ofnode(dev), index, res); |
| 184 | } |
Masahiro Yamada | 4dada2c | 2017-08-26 01:12:30 +0900 | [diff] [blame] | 185 | |
| 186 | int dev_read_resource_byname(struct udevice *dev, const char *name, |
| 187 | struct resource *res) |
| 188 | { |
| 189 | return ofnode_read_resource_byname(dev_ofnode(dev), name, res); |
| 190 | } |