1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (c) 2015 Google, Inc
4 * Written by Simon Glass <sjg@chromium.org>
11 #include <asm/global_data.h>
12 #include <linux/libfdt.h>
17 #include <dm/of_addr.h>
18 #include <dm/devres.h>
19 #include <linux/ioport.h>
20 #include <linux/compat.h>
21 #include <linux/err.h>
22 #include <linux/bitops.h>
25 * Internal representation of a regmap field. Instead of storing the MSB and
26 * LSB, store the shift and mask. This makes the code a bit cleaner and faster
27 * because the shift and mask don't have to be calculated every time.
30 struct regmap *regmap;
37 DECLARE_GLOBAL_DATA_PTR;
40 * regmap_alloc() - Allocate a regmap with a given number of ranges.
42 * @count: Number of ranges to be allocated for the regmap.
44 * The default regmap width is set to REGMAP_SIZE_32. Callers can override it
47 * Return: A pointer to the newly allocated regmap, or NULL on error.
49 static struct regmap *regmap_alloc(int count)
52 size_t size = sizeof(*map) + sizeof(map->ranges[0]) * count;
54 map = calloc(1, size);
57 map->range_count = count;
58 map->width = REGMAP_SIZE_32;
63 #if CONFIG_IS_ENABLED(OF_PLATDATA)
64 int regmap_init_mem_plat(struct udevice *dev, fdt_val_t *reg, int count,
67 struct regmap_range *range;
70 map = regmap_alloc(count);
74 for (range = map->ranges; count > 0; reg += 2, range++, count--) {
85 * init_range() - Initialize a single range of a regmap
86 * @node: Device node that will use the map in question
87 * @range: Pointer to a regmap_range structure that will be initialized
88 * @addr_len: The length of the addr parts of the reg property
89 * @size_len: The length of the size parts of the reg property
90 * @index: The index of the range to initialize
92 * This function will read the necessary 'reg' information from the device tree
93 * (the 'addr' part, and the 'length' part), and initialize the range in
96 * Return: 0 if OK, -ve on error
98 static int init_range(ofnode node, struct regmap_range *range, int addr_len,
99 int size_len, int index)
104 if (of_live_active()) {
107 ret = of_address_to_resource(ofnode_to_np(node),
110 debug("%s: Could not read resource of range %d (ret = %d)\n",
111 ofnode_get_name(node), index, ret);
115 range->start = r.start;
116 range->size = r.end - r.start + 1;
118 int offset = ofnode_to_offset(node);
120 range->start = fdtdec_get_addr_size_fixed(gd->fdt_blob, offset,
124 if (range->start == FDT_ADDR_T_NONE) {
125 debug("%s: Could not read start of range %d\n",
126 ofnode_get_name(node), index);
136 int regmap_init_mem_index(ofnode node, struct regmap **mapp, int index)
139 int addr_len, size_len;
142 addr_len = ofnode_read_simple_addr_cells(ofnode_get_parent(node));
144 debug("%s: Error while reading the addr length (ret = %d)\n",
145 ofnode_get_name(node), addr_len);
149 size_len = ofnode_read_simple_size_cells(ofnode_get_parent(node));
151 debug("%s: Error while reading the size length: (ret = %d)\n",
152 ofnode_get_name(node), size_len);
156 map = regmap_alloc(1);
160 ret = init_range(node, map->ranges, addr_len, size_len, index);
164 if (ofnode_read_bool(node, "little-endian"))
165 map->endianness = REGMAP_LITTLE_ENDIAN;
166 else if (ofnode_read_bool(node, "big-endian"))
167 map->endianness = REGMAP_BIG_ENDIAN;
168 else if (ofnode_read_bool(node, "native-endian"))
169 map->endianness = REGMAP_NATIVE_ENDIAN;
170 else /* Default: native endianness */
171 map->endianness = REGMAP_NATIVE_ENDIAN;
182 int regmap_init_mem_range(ofnode node, ulong r_start, ulong r_size,
183 struct regmap **mapp)
186 struct regmap_range *range;
188 map = regmap_alloc(1);
192 range = &map->ranges[0];
193 range->start = r_start;
194 range->size = r_size;
196 if (ofnode_read_bool(node, "little-endian"))
197 map->endianness = REGMAP_LITTLE_ENDIAN;
198 else if (ofnode_read_bool(node, "big-endian"))
199 map->endianness = REGMAP_BIG_ENDIAN;
200 else if (ofnode_read_bool(node, "native-endian"))
201 map->endianness = REGMAP_NATIVE_ENDIAN;
202 else /* Default: native endianness */
203 map->endianness = REGMAP_NATIVE_ENDIAN;
209 int regmap_init_mem(ofnode node, struct regmap **mapp)
211 struct regmap_range *range;
214 int addr_len, size_len, both_len;
219 addr_len = ofnode_read_simple_addr_cells(ofnode_get_parent(node));
221 debug("%s: Error while reading the addr length (ret = %d)\n",
222 ofnode_get_name(node), addr_len);
226 size_len = ofnode_read_simple_size_cells(ofnode_get_parent(node));
228 debug("%s: Error while reading the size length: (ret = %d)\n",
229 ofnode_get_name(node), size_len);
233 both_len = addr_len + size_len;
235 debug("%s: Both addr and size length are zero\n",
236 ofnode_get_name(node));
240 len = ofnode_read_size(node, "reg");
242 debug("%s: Error while reading reg size (ret = %d)\n",
243 ofnode_get_name(node), len);
246 len /= sizeof(fdt32_t);
247 count = len / both_len;
249 debug("%s: Not enough data in reg property\n",
250 ofnode_get_name(node));
254 map = regmap_alloc(count);
258 for (range = map->ranges, index = 0; count > 0;
259 count--, range++, index++) {
260 ret = init_range(node, range, addr_len, size_len, index);
265 if (ofnode_read_bool(node, "little-endian"))
266 map->endianness = REGMAP_LITTLE_ENDIAN;
267 else if (ofnode_read_bool(node, "big-endian"))
268 map->endianness = REGMAP_BIG_ENDIAN;
269 else if (ofnode_read_bool(node, "native-endian"))
270 map->endianness = REGMAP_NATIVE_ENDIAN;
271 else /* Default: native endianness */
272 map->endianness = REGMAP_NATIVE_ENDIAN;
283 static void devm_regmap_release(struct udevice *dev, void *res)
285 regmap_uninit(*(struct regmap **)res);
288 struct regmap *devm_regmap_init(struct udevice *dev,
289 const struct regmap_bus *bus,
291 const struct regmap_config *config)
294 struct regmap **mapp, *map;
296 /* this looks like a leak, but devres takes care of it */
297 mapp = devres_alloc(devm_regmap_release, sizeof(struct regmap *),
300 return ERR_PTR(-ENOMEM);
302 if (config && config->r_size != 0)
303 rc = regmap_init_mem_range(dev_ofnode(dev), config->r_start,
304 config->r_size, mapp);
306 rc = regmap_init_mem(dev_ofnode(dev), mapp);
312 map->width = config->width;
313 map->reg_offset_shift = config->reg_offset_shift;
316 devres_add(dev, mapp);
321 void *regmap_get_range(struct regmap *map, unsigned int range_num)
323 struct regmap_range *range;
325 if (range_num >= map->range_count)
327 range = &map->ranges[range_num];
329 return map_sysmem(range->start, range->size);
332 int regmap_uninit(struct regmap *map)
339 static inline u8 __read_8(u8 *addr, enum regmap_endianness_t endianness)
344 static inline u16 __read_16(u16 *addr, enum regmap_endianness_t endianness)
346 switch (endianness) {
347 case REGMAP_LITTLE_ENDIAN:
348 return in_le16(addr);
349 case REGMAP_BIG_ENDIAN:
350 return in_be16(addr);
351 case REGMAP_NATIVE_ENDIAN:
358 static inline u32 __read_32(u32 *addr, enum regmap_endianness_t endianness)
360 switch (endianness) {
361 case REGMAP_LITTLE_ENDIAN:
362 return in_le32(addr);
363 case REGMAP_BIG_ENDIAN:
364 return in_be32(addr);
365 case REGMAP_NATIVE_ENDIAN:
372 #if defined(in_le64) && defined(in_be64) && defined(readq)
373 static inline u64 __read_64(u64 *addr, enum regmap_endianness_t endianness)
375 switch (endianness) {
376 case REGMAP_LITTLE_ENDIAN:
377 return in_le64(addr);
378 case REGMAP_BIG_ENDIAN:
379 return in_be64(addr);
380 case REGMAP_NATIVE_ENDIAN:
388 int regmap_raw_read_range(struct regmap *map, uint range_num, uint offset,
389 void *valp, size_t val_len)
391 struct regmap_range *range;
394 if (range_num >= map->range_count) {
395 debug("%s: range index %d larger than range count\n",
396 __func__, range_num);
399 range = &map->ranges[range_num];
401 offset <<= map->reg_offset_shift;
402 if (offset + val_len > range->size) {
403 debug("%s: offset/size combination invalid\n", __func__);
407 ptr = map_physmem(range->start + offset, val_len, MAP_NOCACHE);
411 *((u8 *)valp) = __read_8(ptr, map->endianness);
414 *((u16 *)valp) = __read_16(ptr, map->endianness);
417 *((u32 *)valp) = __read_32(ptr, map->endianness);
419 #if defined(in_le64) && defined(in_be64) && defined(readq)
421 *((u64 *)valp) = __read_64(ptr, map->endianness);
425 debug("%s: regmap size %zu unknown\n", __func__, val_len);
432 int regmap_raw_read(struct regmap *map, uint offset, void *valp, size_t val_len)
434 return regmap_raw_read_range(map, 0, offset, valp, val_len);
437 int regmap_read(struct regmap *map, uint offset, uint *valp)
447 res = regmap_raw_read(map, offset, &u, map->width);
451 switch (map->width) {
471 static inline void __write_8(u8 *addr, const u8 *val,
472 enum regmap_endianness_t endianness)
477 static inline void __write_16(u16 *addr, const u16 *val,
478 enum regmap_endianness_t endianness)
480 switch (endianness) {
481 case REGMAP_NATIVE_ENDIAN:
484 case REGMAP_LITTLE_ENDIAN:
485 out_le16(addr, *val);
487 case REGMAP_BIG_ENDIAN:
488 out_be16(addr, *val);
493 static inline void __write_32(u32 *addr, const u32 *val,
494 enum regmap_endianness_t endianness)
496 switch (endianness) {
497 case REGMAP_NATIVE_ENDIAN:
500 case REGMAP_LITTLE_ENDIAN:
501 out_le32(addr, *val);
503 case REGMAP_BIG_ENDIAN:
504 out_be32(addr, *val);
509 #if defined(out_le64) && defined(out_be64) && defined(writeq)
510 static inline void __write_64(u64 *addr, const u64 *val,
511 enum regmap_endianness_t endianness)
513 switch (endianness) {
514 case REGMAP_NATIVE_ENDIAN:
517 case REGMAP_LITTLE_ENDIAN:
518 out_le64(addr, *val);
520 case REGMAP_BIG_ENDIAN:
521 out_be64(addr, *val);
527 int regmap_raw_write_range(struct regmap *map, uint range_num, uint offset,
528 const void *val, size_t val_len)
530 struct regmap_range *range;
533 if (range_num >= map->range_count) {
534 debug("%s: range index %d larger than range count\n",
535 __func__, range_num);
538 range = &map->ranges[range_num];
540 offset <<= map->reg_offset_shift;
541 if (offset + val_len > range->size) {
542 debug("%s: offset/size combination invalid\n", __func__);
546 ptr = map_physmem(range->start + offset, val_len, MAP_NOCACHE);
550 __write_8(ptr, val, map->endianness);
553 __write_16(ptr, val, map->endianness);
556 __write_32(ptr, val, map->endianness);
558 #if defined(out_le64) && defined(out_be64) && defined(writeq)
560 __write_64(ptr, val, map->endianness);
564 debug("%s: regmap size %zu unknown\n", __func__, val_len);
571 int regmap_raw_write(struct regmap *map, uint offset, const void *val,
574 return regmap_raw_write_range(map, 0, offset, val, val_len);
577 int regmap_write(struct regmap *map, uint offset, uint val)
586 switch (map->width) {
600 debug("%s: regmap size %zu unknown\n", __func__,
605 return regmap_raw_write(map, offset, &u, map->width);
608 int regmap_update_bits(struct regmap *map, uint offset, uint mask, uint val)
613 ret = regmap_read(map, offset, ®);
619 return regmap_write(map, offset, reg | (val & mask));
622 int regmap_field_read(struct regmap_field *field, unsigned int *val)
625 unsigned int reg_val;
627 ret = regmap_read(field->regmap, field->reg, ®_val);
631 reg_val &= field->mask;
632 reg_val >>= field->shift;
638 int regmap_field_write(struct regmap_field *field, unsigned int val)
640 return regmap_update_bits(field->regmap, field->reg, field->mask,
641 val << field->shift);
644 static void regmap_field_init(struct regmap_field *rm_field,
645 struct regmap *regmap,
646 struct reg_field reg_field)
648 rm_field->regmap = regmap;
649 rm_field->reg = reg_field.reg;
650 rm_field->shift = reg_field.lsb;
651 rm_field->mask = GENMASK(reg_field.msb, reg_field.lsb);
654 struct regmap_field *devm_regmap_field_alloc(struct udevice *dev,
655 struct regmap *regmap,
656 struct reg_field reg_field)
658 struct regmap_field *rm_field = devm_kzalloc(dev, sizeof(*rm_field),
661 return ERR_PTR(-ENOMEM);
663 regmap_field_init(rm_field, regmap, reg_field);
668 void devm_regmap_field_free(struct udevice *dev, struct regmap_field *field)
670 devm_kfree(dev, field);
673 struct regmap_field *regmap_field_alloc(struct regmap *regmap,
674 struct reg_field reg_field)
676 struct regmap_field *rm_field = kzalloc(sizeof(*rm_field), GFP_KERNEL);
679 return ERR_PTR(-ENOMEM);
681 regmap_field_init(rm_field, regmap, reg_field);
686 void regmap_field_free(struct regmap_field *field)