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");
143 debug("%s: Error while reading reg size (ret = %d)\n",
144 ofnode_get_name(node), len);
147 len /= sizeof(fdt32_t);
148 count = len / both_len;
150 debug("%s: Not enough data in reg property\n",
151 ofnode_get_name(node));
155 map = regmap_alloc(count);
159 for (range = map->ranges, index = 0; count > 0;
160 count--, range++, index++) {
161 int ret = init_range(node, range, addr_len, size_len, index);
167 if (ofnode_read_bool(node, "little-endian"))
168 map->endianness = REGMAP_LITTLE_ENDIAN;
169 else if (ofnode_read_bool(node, "big-endian"))
170 map->endianness = REGMAP_BIG_ENDIAN;
171 else if (ofnode_read_bool(node, "native-endian"))
172 map->endianness = REGMAP_NATIVE_ENDIAN;
173 else /* Default: native endianness */
174 map->endianness = REGMAP_NATIVE_ENDIAN;
182 void *regmap_get_range(struct regmap *map, unsigned int range_num)
184 struct regmap_range *range;
186 if (range_num >= map->range_count)
188 range = &map->ranges[range_num];
190 return map_sysmem(range->start, range->size);
193 int regmap_uninit(struct regmap *map)
200 static inline u8 __read_8(u8 *addr, enum regmap_endianness_t endianness)
205 static inline u16 __read_16(u16 *addr, enum regmap_endianness_t endianness)
207 switch (endianness) {
208 case REGMAP_LITTLE_ENDIAN:
209 return in_le16(addr);
210 case REGMAP_BIG_ENDIAN:
211 return in_be16(addr);
212 case REGMAP_NATIVE_ENDIAN:
219 static inline u32 __read_32(u32 *addr, enum regmap_endianness_t endianness)
221 switch (endianness) {
222 case REGMAP_LITTLE_ENDIAN:
223 return in_le32(addr);
224 case REGMAP_BIG_ENDIAN:
225 return in_be32(addr);
226 case REGMAP_NATIVE_ENDIAN:
233 #if defined(in_le64) && defined(in_be64) && defined(readq)
234 static inline u64 __read_64(u64 *addr, enum regmap_endianness_t endianness)
236 switch (endianness) {
237 case REGMAP_LITTLE_ENDIAN:
238 return in_le64(addr);
239 case REGMAP_BIG_ENDIAN:
240 return in_be64(addr);
241 case REGMAP_NATIVE_ENDIAN:
249 int regmap_raw_read_range(struct regmap *map, uint range_num, uint offset,
250 void *valp, size_t val_len)
252 struct regmap_range *range;
255 if (range_num >= map->range_count) {
256 debug("%s: range index %d larger than range count\n",
257 __func__, range_num);
260 range = &map->ranges[range_num];
262 ptr = map_physmem(range->start + offset, val_len, MAP_NOCACHE);
264 if (offset + val_len > range->size) {
265 debug("%s: offset/size combination invalid\n", __func__);
271 *((u8 *)valp) = __read_8(ptr, map->endianness);
274 *((u16 *)valp) = __read_16(ptr, map->endianness);
277 *((u32 *)valp) = __read_32(ptr, map->endianness);
279 #if defined(in_le64) && defined(in_be64) && defined(readq)
281 *((u64 *)valp) = __read_64(ptr, map->endianness);
285 debug("%s: regmap size %zu unknown\n", __func__, val_len);
292 int regmap_raw_read(struct regmap *map, uint offset, void *valp, size_t val_len)
294 return regmap_raw_read_range(map, 0, offset, valp, val_len);
297 int regmap_read(struct regmap *map, uint offset, uint *valp)
299 return regmap_raw_read(map, offset, valp, REGMAP_SIZE_32);
302 static inline void __write_8(u8 *addr, const u8 *val,
303 enum regmap_endianness_t endianness)
308 static inline void __write_16(u16 *addr, const u16 *val,
309 enum regmap_endianness_t endianness)
311 switch (endianness) {
312 case REGMAP_NATIVE_ENDIAN:
315 case REGMAP_LITTLE_ENDIAN:
316 out_le16(addr, *val);
318 case REGMAP_BIG_ENDIAN:
319 out_be16(addr, *val);
324 static inline void __write_32(u32 *addr, const u32 *val,
325 enum regmap_endianness_t endianness)
327 switch (endianness) {
328 case REGMAP_NATIVE_ENDIAN:
331 case REGMAP_LITTLE_ENDIAN:
332 out_le32(addr, *val);
334 case REGMAP_BIG_ENDIAN:
335 out_be32(addr, *val);
340 #if defined(out_le64) && defined(out_be64) && defined(writeq)
341 static inline void __write_64(u64 *addr, const u64 *val,
342 enum regmap_endianness_t endianness)
344 switch (endianness) {
345 case REGMAP_NATIVE_ENDIAN:
348 case REGMAP_LITTLE_ENDIAN:
349 out_le64(addr, *val);
351 case REGMAP_BIG_ENDIAN:
352 out_be64(addr, *val);
358 int regmap_raw_write_range(struct regmap *map, uint range_num, uint offset,
359 const void *val, size_t val_len)
361 struct regmap_range *range;
364 if (range_num >= map->range_count) {
365 debug("%s: range index %d larger than range count\n",
366 __func__, range_num);
369 range = &map->ranges[range_num];
371 ptr = map_physmem(range->start + offset, val_len, MAP_NOCACHE);
373 if (offset + val_len > range->size) {
374 debug("%s: offset/size combination invalid\n", __func__);
380 __write_8(ptr, val, map->endianness);
383 __write_16(ptr, val, map->endianness);
386 __write_32(ptr, val, map->endianness);
388 #if defined(out_le64) && defined(out_be64) && defined(writeq)
390 __write_64(ptr, val, map->endianness);
394 debug("%s: regmap size %zu unknown\n", __func__, val_len);
401 int regmap_raw_write(struct regmap *map, uint offset, const void *val,
404 return regmap_raw_write_range(map, 0, offset, val, val_len);
407 int regmap_write(struct regmap *map, uint offset, uint val)
409 return regmap_raw_write(map, offset, &val, REGMAP_SIZE_32);
412 int regmap_update_bits(struct regmap *map, uint offset, uint mask, uint val)
417 ret = regmap_read(map, offset, ®);
423 return regmap_write(map, offset, reg | val);