Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Simon Glass | cfdae5c | 2017-05-18 20:09:04 -0600 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (c) 2017 Google, Inc |
| 4 | * Written by Simon Glass <sjg@chromium.org> |
Simon Glass | cfdae5c | 2017-05-18 20:09:04 -0600 | [diff] [blame] | 5 | */ |
| 6 | |
Álvaro Fernández Rojas | 67036129 | 2018-04-29 21:56:54 +0200 | [diff] [blame] | 7 | #include <asm/types.h> |
| 8 | #include <asm/io.h> |
Simon Glass | cfdae5c | 2017-05-18 20:09:04 -0600 | [diff] [blame] | 9 | #include <common.h> |
| 10 | #include <dm.h> |
Simon Glass | c47a388 | 2017-09-28 06:35:15 -0600 | [diff] [blame] | 11 | #include <mapmem.h> |
Simon Glass | cfdae5c | 2017-05-18 20:09:04 -0600 | [diff] [blame] | 12 | #include <dm/of_access.h> |
| 13 | |
Masahiro Yamada | 71d115f | 2017-12-30 02:00:05 +0900 | [diff] [blame] | 14 | int dev_read_u32(struct udevice *dev, const char *propname, u32 *outp) |
| 15 | { |
| 16 | return ofnode_read_u32(dev_ofnode(dev), propname, outp); |
| 17 | } |
| 18 | |
Simon Glass | cfdae5c | 2017-05-18 20:09:04 -0600 | [diff] [blame] | 19 | int dev_read_u32_default(struct udevice *dev, const char *propname, int def) |
| 20 | { |
| 21 | return ofnode_read_u32_default(dev_ofnode(dev), propname, def); |
| 22 | } |
| 23 | |
Simon Glass | 6df01f9 | 2018-12-10 10:37:37 -0700 | [diff] [blame] | 24 | int dev_read_s32(struct udevice *dev, const char *propname, s32 *outp) |
| 25 | { |
| 26 | return ofnode_read_u32(dev_ofnode(dev), propname, (u32 *)outp); |
| 27 | } |
| 28 | |
| 29 | int dev_read_s32_default(struct udevice *dev, const char *propname, int def) |
| 30 | { |
| 31 | return ofnode_read_u32_default(dev_ofnode(dev), propname, def); |
| 32 | } |
| 33 | |
| 34 | int dev_read_u32u(struct udevice *dev, const char *propname, uint *outp) |
| 35 | { |
| 36 | u32 val; |
| 37 | int ret; |
| 38 | |
| 39 | ret = ofnode_read_u32(dev_ofnode(dev), propname, &val); |
| 40 | if (ret) |
| 41 | return ret; |
| 42 | *outp = val; |
| 43 | |
| 44 | return 0; |
| 45 | } |
| 46 | |
T Karthik Reddy | 478860d | 2019-09-02 16:34:30 +0200 | [diff] [blame] | 47 | int dev_read_u64(struct udevice *dev, const char *propname, u64 *outp) |
| 48 | { |
| 49 | return ofnode_read_u64(dev_ofnode(dev), propname, outp); |
| 50 | } |
| 51 | |
| 52 | u64 dev_read_u64_default(struct udevice *dev, const char *propname, u64 def) |
| 53 | { |
| 54 | return ofnode_read_u64_default(dev_ofnode(dev), propname, def); |
| 55 | } |
| 56 | |
Simon Glass | cfdae5c | 2017-05-18 20:09:04 -0600 | [diff] [blame] | 57 | const char *dev_read_string(struct udevice *dev, const char *propname) |
| 58 | { |
| 59 | return ofnode_read_string(dev_ofnode(dev), propname); |
| 60 | } |
| 61 | |
| 62 | bool dev_read_bool(struct udevice *dev, const char *propname) |
| 63 | { |
| 64 | return ofnode_read_bool(dev_ofnode(dev), propname); |
| 65 | } |
| 66 | |
| 67 | ofnode dev_read_subnode(struct udevice *dev, const char *subnode_name) |
| 68 | { |
| 69 | return ofnode_find_subnode(dev_ofnode(dev), subnode_name); |
| 70 | } |
| 71 | |
| 72 | ofnode dev_read_first_subnode(struct udevice *dev) |
| 73 | { |
| 74 | return ofnode_first_subnode(dev_ofnode(dev)); |
| 75 | } |
| 76 | |
| 77 | ofnode dev_read_next_subnode(ofnode node) |
| 78 | { |
| 79 | return ofnode_next_subnode(node); |
| 80 | } |
| 81 | |
| 82 | int dev_read_size(struct udevice *dev, const char *propname) |
| 83 | { |
| 84 | return ofnode_read_size(dev_ofnode(dev), propname); |
| 85 | } |
| 86 | |
| 87 | fdt_addr_t dev_read_addr_index(struct udevice *dev, int index) |
| 88 | { |
| 89 | if (ofnode_is_np(dev_ofnode(dev))) |
| 90 | return ofnode_get_addr_index(dev_ofnode(dev), index); |
| 91 | else |
| 92 | return devfdt_get_addr_index(dev, index); |
| 93 | } |
| 94 | |
Sekhar Nori | f677c6f | 2019-08-01 19:12:56 +0530 | [diff] [blame] | 95 | fdt_addr_t dev_read_addr_size_index(struct udevice *dev, int index, |
| 96 | fdt_size_t *size) |
| 97 | { |
| 98 | if (ofnode_is_np(dev_ofnode(dev))) |
| 99 | return ofnode_get_addr_size_index(dev_ofnode(dev), index, size); |
| 100 | else |
| 101 | return devfdt_get_addr_size_index(dev, index, size); |
| 102 | } |
| 103 | |
Álvaro Fernández Rojas | 67036129 | 2018-04-29 21:56:54 +0200 | [diff] [blame] | 104 | void *dev_remap_addr_index(struct udevice *dev, int index) |
| 105 | { |
| 106 | fdt_addr_t addr = dev_read_addr_index(dev, index); |
| 107 | |
| 108 | if (addr == FDT_ADDR_T_NONE) |
| 109 | return NULL; |
| 110 | |
| 111 | return map_physmem(addr, 0, MAP_NOCACHE); |
| 112 | } |
| 113 | |
Álvaro Fernández Rojas | a318115 | 2018-12-03 19:37:09 +0100 | [diff] [blame] | 114 | fdt_addr_t dev_read_addr_name(struct udevice *dev, const char *name) |
| 115 | { |
| 116 | int index = dev_read_stringlist_search(dev, "reg-names", name); |
| 117 | |
| 118 | if (index < 0) |
| 119 | return FDT_ADDR_T_NONE; |
| 120 | else |
| 121 | return dev_read_addr_index(dev, index); |
| 122 | } |
| 123 | |
Sekhar Nori | f677c6f | 2019-08-01 19:12:56 +0530 | [diff] [blame] | 124 | fdt_addr_t dev_read_addr_size_name(struct udevice *dev, const char *name, |
| 125 | fdt_size_t *size) |
| 126 | { |
| 127 | int index = dev_read_stringlist_search(dev, "reg-names", name); |
| 128 | |
| 129 | if (index < 0) |
| 130 | return FDT_ADDR_T_NONE; |
| 131 | else |
| 132 | return dev_read_addr_size_index(dev, index, size); |
| 133 | } |
| 134 | |
Álvaro Fernández Rojas | a318115 | 2018-12-03 19:37:09 +0100 | [diff] [blame] | 135 | void *dev_remap_addr_name(struct udevice *dev, const char *name) |
| 136 | { |
| 137 | fdt_addr_t addr = dev_read_addr_name(dev, name); |
| 138 | |
| 139 | if (addr == FDT_ADDR_T_NONE) |
| 140 | return NULL; |
| 141 | |
| 142 | return map_physmem(addr, 0, MAP_NOCACHE); |
| 143 | } |
| 144 | |
Simon Glass | cfdae5c | 2017-05-18 20:09:04 -0600 | [diff] [blame] | 145 | fdt_addr_t dev_read_addr(struct udevice *dev) |
| 146 | { |
| 147 | return dev_read_addr_index(dev, 0); |
| 148 | } |
| 149 | |
Philipp Tomsich | 7719b39 | 2017-09-11 22:04:12 +0200 | [diff] [blame] | 150 | void *dev_read_addr_ptr(struct udevice *dev) |
| 151 | { |
| 152 | fdt_addr_t addr = dev_read_addr(dev); |
| 153 | |
Simon Glass | c47a388 | 2017-09-28 06:35:15 -0600 | [diff] [blame] | 154 | return (addr == FDT_ADDR_T_NONE) ? NULL : map_sysmem(addr, 0); |
Philipp Tomsich | 7719b39 | 2017-09-11 22:04:12 +0200 | [diff] [blame] | 155 | } |
| 156 | |
Álvaro Fernández Rojas | 67036129 | 2018-04-29 21:56:54 +0200 | [diff] [blame] | 157 | void *dev_remap_addr(struct udevice *dev) |
| 158 | { |
| 159 | return dev_remap_addr_index(dev, 0); |
| 160 | } |
| 161 | |
Simon Glass | cfdae5c | 2017-05-18 20:09:04 -0600 | [diff] [blame] | 162 | fdt_addr_t dev_read_addr_size(struct udevice *dev, const char *property, |
Mario Six | 6eac222 | 2018-01-15 11:07:18 +0100 | [diff] [blame] | 163 | fdt_size_t *sizep) |
Simon Glass | cfdae5c | 2017-05-18 20:09:04 -0600 | [diff] [blame] | 164 | { |
| 165 | return ofnode_get_addr_size(dev_ofnode(dev), property, sizep); |
| 166 | } |
| 167 | |
| 168 | const char *dev_read_name(struct udevice *dev) |
| 169 | { |
| 170 | return ofnode_get_name(dev_ofnode(dev)); |
| 171 | } |
| 172 | |
| 173 | int dev_read_stringlist_search(struct udevice *dev, const char *property, |
Mario Six | 6eac222 | 2018-01-15 11:07:18 +0100 | [diff] [blame] | 174 | const char *string) |
Simon Glass | cfdae5c | 2017-05-18 20:09:04 -0600 | [diff] [blame] | 175 | { |
| 176 | return ofnode_stringlist_search(dev_ofnode(dev), property, string); |
| 177 | } |
| 178 | |
Jean-Jacques Hiblot | 8365ebd | 2017-09-21 17:03:09 +0200 | [diff] [blame] | 179 | int dev_read_string_index(struct udevice *dev, const char *propname, int index, |
| 180 | const char **outp) |
| 181 | { |
| 182 | return ofnode_read_string_index(dev_ofnode(dev), propname, index, outp); |
| 183 | } |
| 184 | |
| 185 | int dev_read_string_count(struct udevice *dev, const char *propname) |
| 186 | { |
| 187 | return ofnode_read_string_count(dev_ofnode(dev), propname); |
| 188 | } |
| 189 | |
Simon Glass | cfdae5c | 2017-05-18 20:09:04 -0600 | [diff] [blame] | 190 | int dev_read_phandle_with_args(struct udevice *dev, const char *list_name, |
Mario Six | 6eac222 | 2018-01-15 11:07:18 +0100 | [diff] [blame] | 191 | const char *cells_name, int cell_count, |
| 192 | int index, struct ofnode_phandle_args *out_args) |
Simon Glass | cfdae5c | 2017-05-18 20:09:04 -0600 | [diff] [blame] | 193 | { |
| 194 | return ofnode_parse_phandle_with_args(dev_ofnode(dev), list_name, |
| 195 | cells_name, cell_count, index, |
| 196 | out_args); |
| 197 | } |
| 198 | |
Patrice Chotard | 99503c1 | 2017-11-29 09:06:10 +0100 | [diff] [blame] | 199 | int dev_count_phandle_with_args(struct udevice *dev, const char *list_name, |
| 200 | const char *cells_name) |
| 201 | { |
| 202 | return ofnode_count_phandle_with_args(dev_ofnode(dev), list_name, |
| 203 | cells_name); |
| 204 | } |
| 205 | |
Simon Glass | cfdae5c | 2017-05-18 20:09:04 -0600 | [diff] [blame] | 206 | int dev_read_addr_cells(struct udevice *dev) |
| 207 | { |
| 208 | return ofnode_read_addr_cells(dev_ofnode(dev)); |
| 209 | } |
| 210 | |
| 211 | int dev_read_size_cells(struct udevice *dev) |
| 212 | { |
| 213 | return ofnode_read_size_cells(dev_ofnode(dev)); |
| 214 | } |
| 215 | |
Simon Glass | 4191dc1 | 2017-06-12 06:21:31 -0600 | [diff] [blame] | 216 | int dev_read_simple_addr_cells(struct udevice *dev) |
| 217 | { |
| 218 | return ofnode_read_simple_addr_cells(dev_ofnode(dev)); |
| 219 | } |
| 220 | |
| 221 | int dev_read_simple_size_cells(struct udevice *dev) |
| 222 | { |
| 223 | return ofnode_read_simple_size_cells(dev_ofnode(dev)); |
| 224 | } |
| 225 | |
Simon Glass | cfdae5c | 2017-05-18 20:09:04 -0600 | [diff] [blame] | 226 | int dev_read_phandle(struct udevice *dev) |
| 227 | { |
| 228 | ofnode node = dev_ofnode(dev); |
| 229 | |
| 230 | if (ofnode_is_np(node)) |
| 231 | return ofnode_to_np(node)->phandle; |
| 232 | else |
| 233 | return fdt_get_phandle(gd->fdt_blob, ofnode_to_offset(node)); |
| 234 | } |
| 235 | |
Masahiro Yamada | baebec1 | 2017-07-17 12:18:39 +0900 | [diff] [blame] | 236 | 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] | 237 | { |
Masahiro Yamada | 9cf85cb | 2017-06-22 16:54:05 +0900 | [diff] [blame] | 238 | return ofnode_get_property(dev_ofnode(dev), propname, lenp); |
Simon Glass | cfdae5c | 2017-05-18 20:09:04 -0600 | [diff] [blame] | 239 | } |
| 240 | |
| 241 | int dev_read_alias_seq(struct udevice *dev, int *devnump) |
| 242 | { |
| 243 | ofnode node = dev_ofnode(dev); |
| 244 | const char *uc_name = dev->uclass->uc_drv->name; |
| 245 | int ret; |
| 246 | |
| 247 | if (ofnode_is_np(node)) { |
| 248 | ret = of_alias_get_id(ofnode_to_np(node), uc_name); |
| 249 | if (ret >= 0) |
| 250 | *devnump = ret; |
| 251 | } else { |
| 252 | ret = fdtdec_get_alias_seq(gd->fdt_blob, uc_name, |
| 253 | ofnode_to_offset(node), devnump); |
| 254 | } |
| 255 | |
| 256 | return ret; |
| 257 | } |
| 258 | |
| 259 | int dev_read_u32_array(struct udevice *dev, const char *propname, |
| 260 | u32 *out_values, size_t sz) |
| 261 | { |
| 262 | return ofnode_read_u32_array(dev_ofnode(dev), propname, out_values, sz); |
| 263 | } |
| 264 | |
| 265 | const uint8_t *dev_read_u8_array_ptr(struct udevice *dev, const char *propname, |
| 266 | size_t sz) |
| 267 | { |
| 268 | return ofnode_read_u8_array_ptr(dev_ofnode(dev), propname, sz); |
| 269 | } |
Simon Glass | fa031f8 | 2017-06-12 06:21:30 -0600 | [diff] [blame] | 270 | |
| 271 | int dev_read_enabled(struct udevice *dev) |
| 272 | { |
| 273 | ofnode node = dev_ofnode(dev); |
| 274 | |
| 275 | if (ofnode_is_np(node)) |
| 276 | return of_device_is_available(ofnode_to_np(node)); |
| 277 | else |
| 278 | return fdtdec_get_is_enabled(gd->fdt_blob, |
| 279 | ofnode_to_offset(node)); |
| 280 | } |
Simon Glass | f7bfcc4 | 2017-07-25 08:29:55 -0600 | [diff] [blame] | 281 | |
| 282 | int dev_read_resource(struct udevice *dev, uint index, struct resource *res) |
| 283 | { |
| 284 | return ofnode_read_resource(dev_ofnode(dev), index, res); |
| 285 | } |
Masahiro Yamada | 4dada2c | 2017-08-26 01:12:30 +0900 | [diff] [blame] | 286 | |
| 287 | int dev_read_resource_byname(struct udevice *dev, const char *name, |
| 288 | struct resource *res) |
| 289 | { |
| 290 | return ofnode_read_resource_byname(dev_ofnode(dev), name, res); |
| 291 | } |
Mario Six | aefac06 | 2018-01-15 11:07:19 +0100 | [diff] [blame] | 292 | |
| 293 | u64 dev_translate_address(struct udevice *dev, const fdt32_t *in_addr) |
| 294 | { |
| 295 | return ofnode_translate_address(dev_ofnode(dev), in_addr); |
| 296 | } |
Michal Simek | d0e30a0 | 2019-01-31 16:30:59 +0100 | [diff] [blame] | 297 | |
Fabien Dessenne | 22236e0 | 2019-05-31 15:11:30 +0200 | [diff] [blame] | 298 | u64 dev_translate_dma_address(struct udevice *dev, const fdt32_t *in_addr) |
| 299 | { |
| 300 | return ofnode_translate_dma_address(dev_ofnode(dev), in_addr); |
| 301 | } |
| 302 | |
Michal Simek | d0e30a0 | 2019-01-31 16:30:59 +0100 | [diff] [blame] | 303 | int dev_read_alias_highest_id(const char *stem) |
| 304 | { |
| 305 | if (of_live_active()) |
| 306 | return of_alias_get_highest_id(stem); |
| 307 | |
| 308 | return fdtdec_get_alias_highest_id(gd->fdt_blob, stem); |
| 309 | } |
Simon Glass | 23b2759 | 2019-09-15 12:08:58 -0600 | [diff] [blame^] | 310 | |
| 311 | fdt_addr_t dev_read_addr_pci(struct udevice *dev) |
| 312 | { |
| 313 | ulong addr; |
| 314 | |
| 315 | addr = dev_read_addr(dev); |
| 316 | if (addr == FDT_ADDR_T_NONE && !of_live_active()) |
| 317 | addr = devfdt_get_addr_pci(dev); |
| 318 | |
| 319 | return addr; |
| 320 | } |