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