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;
20 static struct regmap *regmap_alloc(int count)
24 map = malloc(sizeof(*map) + sizeof(map->ranges[0]) * count);
27 map->range_count = count;
32 #if CONFIG_IS_ENABLED(OF_PLATDATA)
33 int regmap_init_mem_platdata(struct udevice *dev, fdt_val_t *reg, int count,
36 struct regmap_range *range;
39 map = regmap_alloc(count);
43 for (range = map->ranges; count > 0; reg += 2, range++, count--) {
53 int regmap_init_mem(ofnode node, struct regmap **mapp)
55 struct regmap_range *range;
58 int addr_len, size_len, both_len;
63 addr_len = ofnode_read_simple_addr_cells(ofnode_get_parent(node));
64 size_len = ofnode_read_simple_size_cells(ofnode_get_parent(node));
65 both_len = addr_len + size_len;
67 len = ofnode_read_size(node, "reg");
70 len /= sizeof(fdt32_t);
71 count = len / both_len;
75 map = regmap_alloc(count);
79 for (range = map->ranges, index = 0; count > 0;
80 count--, range++, index++) {
82 if (of_live_active()) {
83 of_address_to_resource(ofnode_to_np(node), index, &r);
84 range->start = r.start;
85 range->size = r.end - r.start + 1;
87 range->start = fdtdec_get_addr_size_fixed(gd->fdt_blob,
88 ofnode_to_offset(node), "reg", index,
89 addr_len, size_len, &sz, true);
100 void *regmap_get_range(struct regmap *map, unsigned int range_num)
102 struct regmap_range *range;
104 if (range_num >= map->range_count)
106 range = &map->ranges[range_num];
108 return map_sysmem(range->start, range->size);
111 int regmap_uninit(struct regmap *map)
118 int regmap_read(struct regmap *map, uint offset, uint *valp)
120 u32 *ptr = map_physmem(map->ranges[0].start + offset, 4, MAP_NOCACHE);
122 *valp = le32_to_cpu(readl(ptr));
127 int regmap_write(struct regmap *map, uint offset, uint val)
129 u32 *ptr = map_physmem(map->ranges[0].start + offset, 4, MAP_NOCACHE);
131 writel(cpu_to_le32(val), ptr);
136 int regmap_update_bits(struct regmap *map, uint offset, uint mask, uint val)
141 ret = regmap_read(map, offset, ®);
147 return regmap_write(map, offset, reg | val);