1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (c) 2015 Google, Inc
4 * Written by Simon Glass <sjg@chromium.org>
11 #include <linux/libfdt.h>
16 #include <dm/of_addr.h>
17 #include <dm/devres.h>
18 #include <linux/ioport.h>
19 #include <linux/compat.h>
20 #include <linux/err.h>
22 DECLARE_GLOBAL_DATA_PTR;
25 * regmap_alloc() - Allocate a regmap with a given number of ranges.
27 * @count: Number of ranges to be allocated for the regmap.
28 * Return: A pointer to the newly allocated regmap, or NULL on error.
30 static struct regmap *regmap_alloc(int count)
34 map = malloc(sizeof(*map) + sizeof(map->ranges[0]) * count);
37 map->range_count = count;
42 #if CONFIG_IS_ENABLED(OF_PLATDATA)
43 int regmap_init_mem_platdata(struct udevice *dev, fdt_val_t *reg, int count,
46 struct regmap_range *range;
49 map = regmap_alloc(count);
53 for (range = map->ranges; count > 0; reg += 2, range++, count--) {
64 * init_range() - Initialize a single range of a regmap
65 * @node: Device node that will use the map in question
66 * @range: Pointer to a regmap_range structure that will be initialized
67 * @addr_len: The length of the addr parts of the reg property
68 * @size_len: The length of the size parts of the reg property
69 * @index: The index of the range to initialize
71 * This function will read the necessary 'reg' information from the device tree
72 * (the 'addr' part, and the 'length' part), and initialize the range in
75 * Return: 0 if OK, -ve on error
77 static int init_range(ofnode node, struct regmap_range *range, int addr_len,
78 int size_len, int index)
83 if (of_live_active()) {
86 ret = of_address_to_resource(ofnode_to_np(node),
89 debug("%s: Could not read resource of range %d (ret = %d)\n",
90 ofnode_get_name(node), index, ret);
94 range->start = r.start;
95 range->size = r.end - r.start + 1;
97 int offset = ofnode_to_offset(node);
99 range->start = fdtdec_get_addr_size_fixed(gd->fdt_blob, offset,
103 if (range->start == FDT_ADDR_T_NONE) {
104 debug("%s: Could not read start of range %d\n",
105 ofnode_get_name(node), index);
115 int regmap_init_mem_index(ofnode node, struct regmap **mapp, int index)
118 int addr_len, size_len;
121 addr_len = ofnode_read_simple_addr_cells(ofnode_get_parent(node));
123 debug("%s: Error while reading the addr length (ret = %d)\n",
124 ofnode_get_name(node), addr_len);
128 size_len = ofnode_read_simple_size_cells(ofnode_get_parent(node));
130 debug("%s: Error while reading the size length: (ret = %d)\n",
131 ofnode_get_name(node), size_len);
135 map = regmap_alloc(1);
139 ret = init_range(node, map->ranges, addr_len, size_len, index);
143 if (ofnode_read_bool(node, "little-endian"))
144 map->endianness = REGMAP_LITTLE_ENDIAN;
145 else if (ofnode_read_bool(node, "big-endian"))
146 map->endianness = REGMAP_BIG_ENDIAN;
147 else if (ofnode_read_bool(node, "native-endian"))
148 map->endianness = REGMAP_NATIVE_ENDIAN;
149 else /* Default: native endianness */
150 map->endianness = REGMAP_NATIVE_ENDIAN;
161 int regmap_init_mem(ofnode node, struct regmap **mapp)
163 struct regmap_range *range;
166 int addr_len, size_len, both_len;
171 addr_len = ofnode_read_simple_addr_cells(ofnode_get_parent(node));
173 debug("%s: Error while reading the addr length (ret = %d)\n",
174 ofnode_get_name(node), addr_len);
178 size_len = ofnode_read_simple_size_cells(ofnode_get_parent(node));
180 debug("%s: Error while reading the size length: (ret = %d)\n",
181 ofnode_get_name(node), size_len);
185 both_len = addr_len + size_len;
187 debug("%s: Both addr and size length are zero\n",
188 ofnode_get_name(node));
192 len = ofnode_read_size(node, "reg");
194 debug("%s: Error while reading reg size (ret = %d)\n",
195 ofnode_get_name(node), len);
198 len /= sizeof(fdt32_t);
199 count = len / both_len;
201 debug("%s: Not enough data in reg property\n",
202 ofnode_get_name(node));
206 map = regmap_alloc(count);
210 for (range = map->ranges, index = 0; count > 0;
211 count--, range++, index++) {
212 ret = init_range(node, range, addr_len, size_len, index);
217 if (ofnode_read_bool(node, "little-endian"))
218 map->endianness = REGMAP_LITTLE_ENDIAN;
219 else if (ofnode_read_bool(node, "big-endian"))
220 map->endianness = REGMAP_BIG_ENDIAN;
221 else if (ofnode_read_bool(node, "native-endian"))
222 map->endianness = REGMAP_NATIVE_ENDIAN;
223 else /* Default: native endianness */
224 map->endianness = REGMAP_NATIVE_ENDIAN;
235 static void devm_regmap_release(struct udevice *dev, void *res)
237 regmap_uninit(*(struct regmap **)res);
240 struct regmap *devm_regmap_init(struct udevice *dev,
241 const struct regmap_bus *bus,
243 const struct regmap_config *config)
246 struct regmap **mapp;
248 mapp = devres_alloc(devm_regmap_release, sizeof(struct regmap *),
251 return ERR_PTR(-ENOMEM);
253 rc = regmap_init_mem(dev_ofnode(dev), mapp);
257 devres_add(dev, mapp);
262 void *regmap_get_range(struct regmap *map, unsigned int range_num)
264 struct regmap_range *range;
266 if (range_num >= map->range_count)
268 range = &map->ranges[range_num];
270 return map_sysmem(range->start, range->size);
273 int regmap_uninit(struct regmap *map)
280 static inline u8 __read_8(u8 *addr, enum regmap_endianness_t endianness)
285 static inline u16 __read_16(u16 *addr, enum regmap_endianness_t endianness)
287 switch (endianness) {
288 case REGMAP_LITTLE_ENDIAN:
289 return in_le16(addr);
290 case REGMAP_BIG_ENDIAN:
291 return in_be16(addr);
292 case REGMAP_NATIVE_ENDIAN:
299 static inline u32 __read_32(u32 *addr, enum regmap_endianness_t endianness)
301 switch (endianness) {
302 case REGMAP_LITTLE_ENDIAN:
303 return in_le32(addr);
304 case REGMAP_BIG_ENDIAN:
305 return in_be32(addr);
306 case REGMAP_NATIVE_ENDIAN:
313 #if defined(in_le64) && defined(in_be64) && defined(readq)
314 static inline u64 __read_64(u64 *addr, enum regmap_endianness_t endianness)
316 switch (endianness) {
317 case REGMAP_LITTLE_ENDIAN:
318 return in_le64(addr);
319 case REGMAP_BIG_ENDIAN:
320 return in_be64(addr);
321 case REGMAP_NATIVE_ENDIAN:
329 int regmap_raw_read_range(struct regmap *map, uint range_num, uint offset,
330 void *valp, size_t val_len)
332 struct regmap_range *range;
335 if (range_num >= map->range_count) {
336 debug("%s: range index %d larger than range count\n",
337 __func__, range_num);
340 range = &map->ranges[range_num];
342 if (offset + val_len > range->size) {
343 debug("%s: offset/size combination invalid\n", __func__);
347 ptr = map_physmem(range->start + offset, val_len, MAP_NOCACHE);
351 *((u8 *)valp) = __read_8(ptr, map->endianness);
354 *((u16 *)valp) = __read_16(ptr, map->endianness);
357 *((u32 *)valp) = __read_32(ptr, map->endianness);
359 #if defined(in_le64) && defined(in_be64) && defined(readq)
361 *((u64 *)valp) = __read_64(ptr, map->endianness);
365 debug("%s: regmap size %zu unknown\n", __func__, val_len);
372 int regmap_raw_read(struct regmap *map, uint offset, void *valp, size_t val_len)
374 return regmap_raw_read_range(map, 0, offset, valp, val_len);
377 int regmap_read(struct regmap *map, uint offset, uint *valp)
379 return regmap_raw_read(map, offset, valp, REGMAP_SIZE_32);
382 static inline void __write_8(u8 *addr, const u8 *val,
383 enum regmap_endianness_t endianness)
388 static inline void __write_16(u16 *addr, const u16 *val,
389 enum regmap_endianness_t endianness)
391 switch (endianness) {
392 case REGMAP_NATIVE_ENDIAN:
395 case REGMAP_LITTLE_ENDIAN:
396 out_le16(addr, *val);
398 case REGMAP_BIG_ENDIAN:
399 out_be16(addr, *val);
404 static inline void __write_32(u32 *addr, const u32 *val,
405 enum regmap_endianness_t endianness)
407 switch (endianness) {
408 case REGMAP_NATIVE_ENDIAN:
411 case REGMAP_LITTLE_ENDIAN:
412 out_le32(addr, *val);
414 case REGMAP_BIG_ENDIAN:
415 out_be32(addr, *val);
420 #if defined(out_le64) && defined(out_be64) && defined(writeq)
421 static inline void __write_64(u64 *addr, const u64 *val,
422 enum regmap_endianness_t endianness)
424 switch (endianness) {
425 case REGMAP_NATIVE_ENDIAN:
428 case REGMAP_LITTLE_ENDIAN:
429 out_le64(addr, *val);
431 case REGMAP_BIG_ENDIAN:
432 out_be64(addr, *val);
438 int regmap_raw_write_range(struct regmap *map, uint range_num, uint offset,
439 const void *val, size_t val_len)
441 struct regmap_range *range;
444 if (range_num >= map->range_count) {
445 debug("%s: range index %d larger than range count\n",
446 __func__, range_num);
449 range = &map->ranges[range_num];
451 if (offset + val_len > range->size) {
452 debug("%s: offset/size combination invalid\n", __func__);
456 ptr = map_physmem(range->start + offset, val_len, MAP_NOCACHE);
460 __write_8(ptr, val, map->endianness);
463 __write_16(ptr, val, map->endianness);
466 __write_32(ptr, val, map->endianness);
468 #if defined(out_le64) && defined(out_be64) && defined(writeq)
470 __write_64(ptr, val, map->endianness);
474 debug("%s: regmap size %zu unknown\n", __func__, val_len);
481 int regmap_raw_write(struct regmap *map, uint offset, const void *val,
484 return regmap_raw_write_range(map, 0, offset, val, val_len);
487 int regmap_write(struct regmap *map, uint offset, uint val)
489 return regmap_raw_write(map, offset, &val, REGMAP_SIZE_32);
492 int regmap_update_bits(struct regmap *map, uint offset, uint mask, uint val)
497 ret = regmap_read(map, offset, ®);
503 return regmap_write(map, offset, reg | (val & mask));