1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (c) 2015 Google, Inc
4 * Written by Simon Glass <sjg@chromium.org>
10 #include <linux/libfdt.h>
15 #include <dm/of_addr.h>
16 #include <linux/ioport.h>
18 DECLARE_GLOBAL_DATA_PTR;
21 * regmap_alloc() - Allocate a regmap with a given number of ranges.
23 * @count: Number of ranges to be allocated for the regmap.
24 * Return: A pointer to the newly allocated regmap, or NULL on error.
26 static struct regmap *regmap_alloc(int count)
30 map = malloc(sizeof(*map) + sizeof(map->ranges[0]) * count);
33 map->range_count = count;
38 #if CONFIG_IS_ENABLED(OF_PLATDATA)
39 int regmap_init_mem_platdata(struct udevice *dev, fdt_val_t *reg, int count,
42 struct regmap_range *range;
45 map = regmap_alloc(count);
49 for (range = map->ranges; count > 0; reg += 2, range++, count--) {
59 int regmap_init_mem(ofnode node, struct regmap **mapp)
61 struct regmap_range *range;
64 int addr_len, size_len, both_len;
69 addr_len = ofnode_read_simple_addr_cells(ofnode_get_parent(node));
70 size_len = ofnode_read_simple_size_cells(ofnode_get_parent(node));
71 both_len = addr_len + size_len;
73 len = ofnode_read_size(node, "reg");
76 len /= sizeof(fdt32_t);
77 count = len / both_len;
81 map = regmap_alloc(count);
85 for (range = map->ranges, index = 0; count > 0;
86 count--, range++, index++) {
88 if (of_live_active()) {
89 of_address_to_resource(ofnode_to_np(node), index, &r);
90 range->start = r.start;
91 range->size = r.end - r.start + 1;
93 range->start = fdtdec_get_addr_size_fixed(gd->fdt_blob,
94 ofnode_to_offset(node), "reg", index,
95 addr_len, size_len, &sz, true);
106 void *regmap_get_range(struct regmap *map, unsigned int range_num)
108 struct regmap_range *range;
110 if (range_num >= map->range_count)
112 range = &map->ranges[range_num];
114 return map_sysmem(range->start, range->size);
117 int regmap_uninit(struct regmap *map)
124 int regmap_read(struct regmap *map, uint offset, uint *valp)
126 u32 *ptr = map_physmem(map->ranges[0].start + offset, 4, MAP_NOCACHE);
128 *valp = le32_to_cpu(readl(ptr));
133 int regmap_write(struct regmap *map, uint offset, uint val)
135 u32 *ptr = map_physmem(map->ranges[0].start + offset, 4, MAP_NOCACHE);
137 writel(cpu_to_le32(val), ptr);
142 int regmap_update_bits(struct regmap *map, uint offset, uint mask, uint val)
147 ret = regmap_read(map, offset, ®);
153 return regmap_write(map, offset, reg | val);