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_index(ofnode node, struct regmap **mapp, int index)
114 int addr_len, size_len;
117 addr_len = ofnode_read_simple_addr_cells(ofnode_get_parent(node));
119 debug("%s: Error while reading the addr length (ret = %d)\n",
120 ofnode_get_name(node), addr_len);
124 size_len = ofnode_read_simple_size_cells(ofnode_get_parent(node));
126 debug("%s: Error while reading the size length: (ret = %d)\n",
127 ofnode_get_name(node), size_len);
131 map = regmap_alloc(1);
135 ret = init_range(node, map->ranges, addr_len, size_len, index);
139 if (ofnode_read_bool(node, "little-endian"))
140 map->endianness = REGMAP_LITTLE_ENDIAN;
141 else if (ofnode_read_bool(node, "big-endian"))
142 map->endianness = REGMAP_BIG_ENDIAN;
143 else if (ofnode_read_bool(node, "native-endian"))
144 map->endianness = REGMAP_NATIVE_ENDIAN;
145 else /* Default: native endianness */
146 map->endianness = REGMAP_NATIVE_ENDIAN;
153 int regmap_init_mem(ofnode node, struct regmap **mapp)
155 struct regmap_range *range;
158 int addr_len, size_len, both_len;
162 addr_len = ofnode_read_simple_addr_cells(ofnode_get_parent(node));
164 debug("%s: Error while reading the addr length (ret = %d)\n",
165 ofnode_get_name(node), addr_len);
169 size_len = ofnode_read_simple_size_cells(ofnode_get_parent(node));
171 debug("%s: Error while reading the size length: (ret = %d)\n",
172 ofnode_get_name(node), size_len);
176 both_len = addr_len + size_len;
178 debug("%s: Both addr and size length are zero\n",
179 ofnode_get_name(node));
183 len = ofnode_read_size(node, "reg");
185 debug("%s: Error while reading reg size (ret = %d)\n",
186 ofnode_get_name(node), len);
189 len /= sizeof(fdt32_t);
190 count = len / both_len;
192 debug("%s: Not enough data in reg property\n",
193 ofnode_get_name(node));
197 map = regmap_alloc(count);
201 for (range = map->ranges, index = 0; count > 0;
202 count--, range++, index++) {
203 int ret = init_range(node, range, addr_len, size_len, index);
209 if (ofnode_read_bool(node, "little-endian"))
210 map->endianness = REGMAP_LITTLE_ENDIAN;
211 else if (ofnode_read_bool(node, "big-endian"))
212 map->endianness = REGMAP_BIG_ENDIAN;
213 else if (ofnode_read_bool(node, "native-endian"))
214 map->endianness = REGMAP_NATIVE_ENDIAN;
215 else /* Default: native endianness */
216 map->endianness = REGMAP_NATIVE_ENDIAN;
224 void *regmap_get_range(struct regmap *map, unsigned int range_num)
226 struct regmap_range *range;
228 if (range_num >= map->range_count)
230 range = &map->ranges[range_num];
232 return map_sysmem(range->start, range->size);
235 int regmap_uninit(struct regmap *map)
242 static inline u8 __read_8(u8 *addr, enum regmap_endianness_t endianness)
247 static inline u16 __read_16(u16 *addr, enum regmap_endianness_t endianness)
249 switch (endianness) {
250 case REGMAP_LITTLE_ENDIAN:
251 return in_le16(addr);
252 case REGMAP_BIG_ENDIAN:
253 return in_be16(addr);
254 case REGMAP_NATIVE_ENDIAN:
261 static inline u32 __read_32(u32 *addr, enum regmap_endianness_t endianness)
263 switch (endianness) {
264 case REGMAP_LITTLE_ENDIAN:
265 return in_le32(addr);
266 case REGMAP_BIG_ENDIAN:
267 return in_be32(addr);
268 case REGMAP_NATIVE_ENDIAN:
275 #if defined(in_le64) && defined(in_be64) && defined(readq)
276 static inline u64 __read_64(u64 *addr, enum regmap_endianness_t endianness)
278 switch (endianness) {
279 case REGMAP_LITTLE_ENDIAN:
280 return in_le64(addr);
281 case REGMAP_BIG_ENDIAN:
282 return in_be64(addr);
283 case REGMAP_NATIVE_ENDIAN:
291 int regmap_raw_read_range(struct regmap *map, uint range_num, uint offset,
292 void *valp, size_t val_len)
294 struct regmap_range *range;
297 if (range_num >= map->range_count) {
298 debug("%s: range index %d larger than range count\n",
299 __func__, range_num);
302 range = &map->ranges[range_num];
304 ptr = map_physmem(range->start + offset, val_len, MAP_NOCACHE);
306 if (offset + val_len > range->size) {
307 debug("%s: offset/size combination invalid\n", __func__);
313 *((u8 *)valp) = __read_8(ptr, map->endianness);
316 *((u16 *)valp) = __read_16(ptr, map->endianness);
319 *((u32 *)valp) = __read_32(ptr, map->endianness);
321 #if defined(in_le64) && defined(in_be64) && defined(readq)
323 *((u64 *)valp) = __read_64(ptr, map->endianness);
327 debug("%s: regmap size %zu unknown\n", __func__, val_len);
334 int regmap_raw_read(struct regmap *map, uint offset, void *valp, size_t val_len)
336 return regmap_raw_read_range(map, 0, offset, valp, val_len);
339 int regmap_read(struct regmap *map, uint offset, uint *valp)
341 return regmap_raw_read(map, offset, valp, REGMAP_SIZE_32);
344 static inline void __write_8(u8 *addr, const u8 *val,
345 enum regmap_endianness_t endianness)
350 static inline void __write_16(u16 *addr, const u16 *val,
351 enum regmap_endianness_t endianness)
353 switch (endianness) {
354 case REGMAP_NATIVE_ENDIAN:
357 case REGMAP_LITTLE_ENDIAN:
358 out_le16(addr, *val);
360 case REGMAP_BIG_ENDIAN:
361 out_be16(addr, *val);
366 static inline void __write_32(u32 *addr, const u32 *val,
367 enum regmap_endianness_t endianness)
369 switch (endianness) {
370 case REGMAP_NATIVE_ENDIAN:
373 case REGMAP_LITTLE_ENDIAN:
374 out_le32(addr, *val);
376 case REGMAP_BIG_ENDIAN:
377 out_be32(addr, *val);
382 #if defined(out_le64) && defined(out_be64) && defined(writeq)
383 static inline void __write_64(u64 *addr, const u64 *val,
384 enum regmap_endianness_t endianness)
386 switch (endianness) {
387 case REGMAP_NATIVE_ENDIAN:
390 case REGMAP_LITTLE_ENDIAN:
391 out_le64(addr, *val);
393 case REGMAP_BIG_ENDIAN:
394 out_be64(addr, *val);
400 int regmap_raw_write_range(struct regmap *map, uint range_num, uint offset,
401 const void *val, size_t val_len)
403 struct regmap_range *range;
406 if (range_num >= map->range_count) {
407 debug("%s: range index %d larger than range count\n",
408 __func__, range_num);
411 range = &map->ranges[range_num];
413 ptr = map_physmem(range->start + offset, val_len, MAP_NOCACHE);
415 if (offset + val_len > range->size) {
416 debug("%s: offset/size combination invalid\n", __func__);
422 __write_8(ptr, val, map->endianness);
425 __write_16(ptr, val, map->endianness);
428 __write_32(ptr, val, map->endianness);
430 #if defined(out_le64) && defined(out_be64) && defined(writeq)
432 __write_64(ptr, val, map->endianness);
436 debug("%s: regmap size %zu unknown\n", __func__, val_len);
443 int regmap_raw_write(struct regmap *map, uint offset, const void *val,
446 return regmap_raw_write_range(map, 0, offset, val, val_len);
449 int regmap_write(struct regmap *map, uint offset, uint val)
451 return regmap_raw_write(map, offset, &val, REGMAP_SIZE_32);
454 int regmap_update_bits(struct regmap *map, uint offset, uint mask, uint val)
459 ret = regmap_read(map, offset, ®);
465 return regmap_write(map, offset, reg | val);