uboot/drivers/core/read.c
<<
>>
Prefs
   1// SPDX-License-Identifier: GPL-2.0+
   2/*
   3 * Copyright (c) 2017 Google, Inc
   4 * Written by Simon Glass <sjg@chromium.org>
   5 */
   6
   7#include <common.h>
   8#include <dm.h>
   9#include <dm/of_access.h>
  10#include <mapmem.h>
  11#include <asm/types.h>
  12#include <asm/io.h>
  13#include <linux/ioport.h>
  14
  15int dev_read_u32(const struct udevice *dev, const char *propname, u32 *outp)
  16{
  17        return ofnode_read_u32(dev_ofnode(dev), propname, outp);
  18}
  19
  20int dev_read_u32_default(const struct udevice *dev, const char *propname,
  21                         int def)
  22{
  23        return ofnode_read_u32_default(dev_ofnode(dev), propname, def);
  24}
  25
  26int dev_read_u32_index(struct udevice *dev, const char *propname, int index,
  27                       u32 *outp)
  28{
  29        return ofnode_read_u32_index(dev_ofnode(dev), propname, index, outp);
  30}
  31
  32u32 dev_read_u32_index_default(struct udevice *dev, const char *propname,
  33                               int index, u32 def)
  34{
  35        return ofnode_read_u32_index_default(dev_ofnode(dev), propname, index,
  36                                             def);
  37}
  38
  39int dev_read_s32(const struct udevice *dev, const char *propname, s32 *outp)
  40{
  41        return ofnode_read_u32(dev_ofnode(dev), propname, (u32 *)outp);
  42}
  43
  44int dev_read_s32_default(const struct udevice *dev, const char *propname,
  45                         int def)
  46{
  47        return ofnode_read_u32_default(dev_ofnode(dev), propname, def);
  48}
  49
  50int dev_read_u32u(const struct udevice *dev, const char *propname, uint *outp)
  51{
  52        u32 val;
  53        int ret;
  54
  55        ret = ofnode_read_u32(dev_ofnode(dev), propname, &val);
  56        if (ret)
  57                return ret;
  58        *outp = val;
  59
  60        return 0;
  61}
  62
  63int dev_read_u64(const struct udevice *dev, const char *propname, u64 *outp)
  64{
  65        return ofnode_read_u64(dev_ofnode(dev), propname, outp);
  66}
  67
  68u64 dev_read_u64_default(const struct udevice *dev, const char *propname,
  69                         u64 def)
  70{
  71        return ofnode_read_u64_default(dev_ofnode(dev), propname, def);
  72}
  73
  74const char *dev_read_string(const struct udevice *dev, const char *propname)
  75{
  76        return ofnode_read_string(dev_ofnode(dev), propname);
  77}
  78
  79bool dev_read_bool(const struct udevice *dev, const char *propname)
  80{
  81        return ofnode_read_bool(dev_ofnode(dev), propname);
  82}
  83
  84ofnode dev_read_subnode(const struct udevice *dev, const char *subnode_name)
  85{
  86        return ofnode_find_subnode(dev_ofnode(dev), subnode_name);
  87}
  88
  89ofnode dev_read_first_subnode(const struct udevice *dev)
  90{
  91        return ofnode_first_subnode(dev_ofnode(dev));
  92}
  93
  94ofnode dev_read_next_subnode(ofnode node)
  95{
  96        return ofnode_next_subnode(node);
  97}
  98
  99int dev_read_size(const struct udevice *dev, const char *propname)
 100{
 101        return ofnode_read_size(dev_ofnode(dev), propname);
 102}
 103
 104fdt_addr_t dev_read_addr_index(const struct udevice *dev, int index)
 105{
 106        if (ofnode_is_np(dev_ofnode(dev)))
 107                return ofnode_get_addr_index(dev_ofnode(dev), index);
 108        else
 109                return devfdt_get_addr_index(dev, index);
 110}
 111
 112fdt_addr_t dev_read_addr_size_index(const struct udevice *dev, int index,
 113                                    fdt_size_t *size)
 114{
 115        if (ofnode_is_np(dev_ofnode(dev)))
 116                return ofnode_get_addr_size_index(dev_ofnode(dev), index, size);
 117        else
 118                return devfdt_get_addr_size_index(dev, index, size);
 119}
 120
 121void *dev_remap_addr_index(const struct udevice *dev, int index)
 122{
 123        fdt_addr_t addr = dev_read_addr_index(dev, index);
 124
 125        if (addr == FDT_ADDR_T_NONE)
 126                return NULL;
 127
 128        return map_physmem(addr, 0, MAP_NOCACHE);
 129}
 130
 131fdt_addr_t dev_read_addr_name(const struct udevice *dev, const char *name)
 132{
 133        int index = dev_read_stringlist_search(dev, "reg-names", name);
 134
 135        if (index < 0)
 136                return FDT_ADDR_T_NONE;
 137        else
 138                return dev_read_addr_index(dev, index);
 139}
 140
 141fdt_addr_t dev_read_addr_size_name(const struct udevice *dev, const char *name,
 142                                   fdt_size_t *size)
 143{
 144        int index = dev_read_stringlist_search(dev, "reg-names", name);
 145
 146        if (index < 0)
 147                return FDT_ADDR_T_NONE;
 148        else
 149                return dev_read_addr_size_index(dev, index, size);
 150}
 151
 152void *dev_remap_addr_name(const struct udevice *dev, const char *name)
 153{
 154        fdt_addr_t addr = dev_read_addr_name(dev, name);
 155
 156        if (addr == FDT_ADDR_T_NONE)
 157                return NULL;
 158
 159        return map_physmem(addr, 0, MAP_NOCACHE);
 160}
 161
 162fdt_addr_t dev_read_addr(const struct udevice *dev)
 163{
 164        return dev_read_addr_index(dev, 0);
 165}
 166
 167void *dev_read_addr_ptr(const struct udevice *dev)
 168{
 169        fdt_addr_t addr = dev_read_addr(dev);
 170
 171        return (addr == FDT_ADDR_T_NONE) ? NULL : (void *)(uintptr_t)addr;
 172}
 173
 174void *dev_remap_addr(const struct udevice *dev)
 175{
 176        return dev_remap_addr_index(dev, 0);
 177}
 178
 179fdt_addr_t dev_read_addr_size(const struct udevice *dev, const char *property,
 180                              fdt_size_t *sizep)
 181{
 182        return ofnode_get_addr_size(dev_ofnode(dev), property, sizep);
 183}
 184
 185const char *dev_read_name(const struct udevice *dev)
 186{
 187        return ofnode_get_name(dev_ofnode(dev));
 188}
 189
 190int dev_read_stringlist_search(const struct udevice *dev, const char *property,
 191                               const char *string)
 192{
 193        return ofnode_stringlist_search(dev_ofnode(dev), property, string);
 194}
 195
 196int dev_read_string_index(const struct udevice *dev, const char *propname,
 197                          int index, const char **outp)
 198{
 199        return ofnode_read_string_index(dev_ofnode(dev), propname, index, outp);
 200}
 201
 202int dev_read_string_count(const struct udevice *dev, const char *propname)
 203{
 204        return ofnode_read_string_count(dev_ofnode(dev), propname);
 205}
 206
 207int dev_read_phandle_with_args(const struct udevice *dev, const char *list_name,
 208                               const char *cells_name, int cell_count,
 209                               int index, struct ofnode_phandle_args *out_args)
 210{
 211        return ofnode_parse_phandle_with_args(dev_ofnode(dev), list_name,
 212                                              cells_name, cell_count, index,
 213                                              out_args);
 214}
 215
 216int dev_count_phandle_with_args(const struct udevice *dev,
 217                                const char *list_name, const char *cells_name,
 218                                int cell_count)
 219{
 220        return ofnode_count_phandle_with_args(dev_ofnode(dev), list_name,
 221                                              cells_name, cell_count);
 222}
 223
 224int dev_read_addr_cells(const struct udevice *dev)
 225{
 226        return ofnode_read_addr_cells(dev_ofnode(dev));
 227}
 228
 229int dev_read_size_cells(const struct udevice *dev)
 230{
 231        return ofnode_read_size_cells(dev_ofnode(dev));
 232}
 233
 234int dev_read_simple_addr_cells(const struct udevice *dev)
 235{
 236        return ofnode_read_simple_addr_cells(dev_ofnode(dev));
 237}
 238
 239int dev_read_simple_size_cells(const struct udevice *dev)
 240{
 241        return ofnode_read_simple_size_cells(dev_ofnode(dev));
 242}
 243
 244int dev_read_phandle(const struct udevice *dev)
 245{
 246        ofnode node = dev_ofnode(dev);
 247
 248        if (ofnode_is_np(node))
 249                return ofnode_to_np(node)->phandle;
 250        else
 251                return fdt_get_phandle(gd->fdt_blob, ofnode_to_offset(node));
 252}
 253
 254const void *dev_read_prop(const struct udevice *dev, const char *propname,
 255                          int *lenp)
 256{
 257        return ofnode_get_property(dev_ofnode(dev), propname, lenp);
 258}
 259
 260int dev_read_first_prop(const struct udevice *dev, struct ofprop *prop)
 261{
 262        return ofnode_get_first_property(dev_ofnode(dev), prop);
 263}
 264
 265int dev_read_next_prop(struct ofprop *prop)
 266{
 267        return ofnode_get_next_property(prop);
 268}
 269
 270const void *dev_read_prop_by_prop(struct ofprop *prop,
 271                                  const char **propname, int *lenp)
 272{
 273        return ofnode_get_property_by_prop(prop, propname, lenp);
 274}
 275
 276int dev_read_alias_seq(const struct udevice *dev, int *devnump)
 277{
 278        ofnode node = dev_ofnode(dev);
 279        const char *uc_name = dev->uclass->uc_drv->name;
 280        int ret = -ENOTSUPP;
 281
 282        if (ofnode_is_np(node)) {
 283                ret = of_alias_get_id(ofnode_to_np(node), uc_name);
 284                if (ret >= 0)
 285                        *devnump = ret;
 286        } else {
 287#if CONFIG_IS_ENABLED(OF_CONTROL)
 288                ret = fdtdec_get_alias_seq(gd->fdt_blob, uc_name,
 289                                           ofnode_to_offset(node), devnump);
 290#endif
 291        }
 292
 293        return ret;
 294}
 295
 296int dev_read_u32_array(const struct udevice *dev, const char *propname,
 297                       u32 *out_values, size_t sz)
 298{
 299        return ofnode_read_u32_array(dev_ofnode(dev), propname, out_values, sz);
 300}
 301
 302const uint8_t *dev_read_u8_array_ptr(const struct udevice *dev,
 303                                     const char *propname, size_t sz)
 304{
 305        return ofnode_read_u8_array_ptr(dev_ofnode(dev), propname, sz);
 306}
 307
 308int dev_read_enabled(const struct udevice *dev)
 309{
 310        ofnode node = dev_ofnode(dev);
 311
 312        if (ofnode_is_np(node))
 313                return of_device_is_available(ofnode_to_np(node));
 314        else
 315                return fdtdec_get_is_enabled(gd->fdt_blob,
 316                                             ofnode_to_offset(node));
 317}
 318
 319int dev_read_resource(const struct udevice *dev, uint index,
 320                      struct resource *res)
 321{
 322        return ofnode_read_resource(dev_ofnode(dev), index, res);
 323}
 324
 325int dev_read_resource_byname(const struct udevice *dev, const char *name,
 326                             struct resource *res)
 327{
 328        return ofnode_read_resource_byname(dev_ofnode(dev), name, res);
 329}
 330
 331u64 dev_translate_address(const struct udevice *dev, const fdt32_t *in_addr)
 332{
 333        return ofnode_translate_address(dev_ofnode(dev), in_addr);
 334}
 335
 336u64 dev_translate_dma_address(const struct udevice *dev, const fdt32_t *in_addr)
 337{
 338        return ofnode_translate_dma_address(dev_ofnode(dev), in_addr);
 339}
 340
 341int dev_read_alias_highest_id(const char *stem)
 342{
 343        if (of_live_active())
 344                return of_alias_get_highest_id(stem);
 345
 346        return fdtdec_get_alias_highest_id(gd->fdt_blob, stem);
 347}
 348
 349fdt_addr_t dev_read_addr_pci(const struct udevice *dev)
 350{
 351        ulong addr;
 352
 353        addr = dev_read_addr(dev);
 354        if (addr == FDT_ADDR_T_NONE && !of_live_active())
 355                addr = devfdt_get_addr_pci(dev);
 356
 357        return addr;
 358}
 359
 360int dev_get_child_count(const struct udevice *dev)
 361{
 362        return ofnode_get_child_count(dev_ofnode(dev));
 363}
 364
 365int dev_read_pci_bus_range(const struct udevice *dev,
 366                           struct resource *res)
 367{
 368        const u32 *values;
 369        int len;
 370
 371        values = dev_read_prop(dev, "bus-range", &len);
 372        if (!values || len < sizeof(*values) * 2)
 373                return -EINVAL;
 374
 375        res->start = *values++;
 376        res->end = *values;
 377
 378        return 0;
 379}
 380