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--) {
60 * init_range() - Initialize a single range of a regmap
61 * @node: Device node that will use the map in question
62 * @range: Pointer to a regmap_range structure that will be initialized
63 * @addr_len: The length of the addr parts of the reg property
64 * @size_len: The length of the size parts of the reg property
65 * @index: The index of the range to initialize
67 * This function will read the necessary 'reg' information from the device tree
68 * (the 'addr' part, and the 'length' part), and initialize the range in
71 * Return: 0 if OK, -ve on error
73 static int init_range(ofnode node, struct regmap_range *range, int addr_len,
74 int size_len, int index)
79 if (of_live_active()) {
82 ret = of_address_to_resource(ofnode_to_np(node),
85 debug("%s: Could not read resource of range %d (ret = %d)\n",
86 ofnode_get_name(node), index, ret);
90 range->start = r.start;
91 range->size = r.end - r.start + 1;
93 int offset = ofnode_to_offset(node);
95 range->start = fdtdec_get_addr_size_fixed(gd->fdt_blob, offset,
99 if (range->start == FDT_ADDR_T_NONE) {
100 debug("%s: Could not read start of range %d\n",
101 ofnode_get_name(node), index);
111 int regmap_init_mem(ofnode node, struct regmap **mapp)
113 struct regmap_range *range;
116 int addr_len, size_len, both_len;
120 addr_len = ofnode_read_simple_addr_cells(ofnode_get_parent(node));
122 debug("%s: Error while reading the addr length (ret = %d)\n",
123 ofnode_get_name(node), addr_len);
127 size_len = ofnode_read_simple_size_cells(ofnode_get_parent(node));
129 debug("%s: Error while reading the size length: (ret = %d)\n",
130 ofnode_get_name(node), size_len);
134 both_len = addr_len + size_len;
136 debug("%s: Both addr and size length are zero\n",
137 ofnode_get_name(node));
141 len = ofnode_read_size(node, "reg");
144 len /= sizeof(fdt32_t);
145 count = len / both_len;
149 map = regmap_alloc(count);
153 for (range = map->ranges, index = 0; count > 0;
154 count--, range++, index++) {
155 int ret = init_range(node, range, addr_len, size_len, index);
167 void *regmap_get_range(struct regmap *map, unsigned int range_num)
169 struct regmap_range *range;
171 if (range_num >= map->range_count)
173 range = &map->ranges[range_num];
175 return map_sysmem(range->start, range->size);
178 int regmap_uninit(struct regmap *map)
185 int regmap_read(struct regmap *map, uint offset, uint *valp)
187 u32 *ptr = map_physmem(map->ranges[0].start + offset, 4, MAP_NOCACHE);
189 *valp = le32_to_cpu(readl(ptr));
194 int regmap_write(struct regmap *map, uint offset, uint val)
196 u32 *ptr = map_physmem(map->ranges[0].start + offset, 4, MAP_NOCACHE);
198 writel(cpu_to_le32(val), ptr);
203 int regmap_update_bits(struct regmap *map, uint offset, uint mask, uint val)
208 ret = regmap_read(map, offset, ®);
214 return regmap_write(map, offset, reg | val);