2 * Copyright (c) 2015 Google, Inc
3 * Written by Simon Glass <sjg@chromium.org>
5 * SPDX-License-Identifier: GPL-2.0+
16 DECLARE_GLOBAL_DATA_PTR;
18 static struct regmap *regmap_alloc_count(int count)
22 map = malloc(sizeof(struct regmap));
26 map->range = &map->base_range;
28 map->range = malloc(count * sizeof(struct regmap_range));
34 map->range_count = count;
39 #if CONFIG_IS_ENABLED(OF_PLATDATA)
40 int regmap_init_mem_platdata(struct udevice *dev, fdt32_t *reg, int size,
43 /* TODO(sjg@chromium.org): Implement this when needed */
47 int regmap_init_mem(struct udevice *dev, struct regmap **mapp)
49 const void *blob = gd->fdt_blob;
50 struct regmap_range *range;
54 int addr_len, size_len, both_len;
58 parent = dev->parent->of_offset;
59 addr_len = fdt_address_cells(blob, parent);
60 size_len = fdt_size_cells(blob, parent);
61 both_len = addr_len + size_len;
63 cell = fdt_getprop(blob, dev->of_offset, "reg", &len);
65 count = len / both_len;
69 map = regmap_alloc_count(count);
73 map->base = fdtdec_get_number(cell, addr_len);
75 for (range = map->range; count > 0;
76 count--, cell += both_len, range++) {
77 range->start = fdtdec_get_number(cell, addr_len);
78 range->size = fdtdec_get_number(cell + addr_len, size_len);
87 void *regmap_get_range(struct regmap *map, unsigned int range_num)
89 struct regmap_range *range;
91 if (range_num >= map->range_count)
93 range = &map->range[range_num];
95 return map_sysmem(range->start, range->size);
98 int regmap_uninit(struct regmap *map)
100 if (map->range_count > 1)