blob: 3206f3d112856dac26bbf833afcdb29ac75c8a5c [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Simon Glassad8f8ab2015-06-23 15:38:42 -06002/*
3 * Copyright (c) 2015 Google, Inc
4 * Written by Simon Glass <sjg@chromium.org>
Simon Glassad8f8ab2015-06-23 15:38:42 -06005 */
6
7#include <common.h>
8#include <dm.h>
9#include <errno.h>
Simon Glass0f2af882020-05-10 11:40:05 -060010#include <log.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060011#include <asm/global_data.h>
Masahiro Yamada75f82d02018-03-05 01:20:11 +090012#include <linux/libfdt.h>
Simon Glassad8f8ab2015-06-23 15:38:42 -060013#include <malloc.h>
14#include <mapmem.h>
15#include <regmap.h>
Paul Burton39776032016-09-08 07:47:35 +010016#include <asm/io.h>
Simon Glasseeeb5192017-05-18 20:09:10 -060017#include <dm/of_addr.h>
Jean-Jacques Hiblotde03d122020-09-24 10:04:10 +053018#include <dm/devres.h>
Simon Glasseeeb5192017-05-18 20:09:10 -060019#include <linux/ioport.h>
Jean-Jacques Hiblotde03d122020-09-24 10:04:10 +053020#include <linux/compat.h>
21#include <linux/err.h>
Jean-Jacques Hiblotb3bbbcf2020-09-24 10:04:16 +053022#include <linux/bitops.h>
23
24/*
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.
28 */
29struct regmap_field {
30 struct regmap *regmap;
31 unsigned int mask;
32 /* lsb */
33 unsigned int shift;
34 unsigned int reg;
35};
Paul Burton39776032016-09-08 07:47:35 +010036
Simon Glassad8f8ab2015-06-23 15:38:42 -060037DECLARE_GLOBAL_DATA_PTR;
38
Mario Six0aa52992018-10-04 09:00:42 +020039/**
40 * regmap_alloc() - Allocate a regmap with a given number of ranges.
41 *
42 * @count: Number of ranges to be allocated for the regmap.
Pratyush Yadav1c9867c2020-09-24 10:04:12 +053043 *
44 * The default regmap width is set to REGMAP_SIZE_32. Callers can override it
45 * if they need.
46 *
Mario Six0aa52992018-10-04 09:00:42 +020047 * Return: A pointer to the newly allocated regmap, or NULL on error.
48 */
Masahiro Yamada54c5ecb2018-04-19 12:14:01 +090049static struct regmap *regmap_alloc(int count)
Simon Glass30d73e82016-07-04 11:58:21 -060050{
51 struct regmap *map;
Pratyush Yadav279cad82020-09-24 10:04:11 +053052 size_t size = sizeof(*map) + sizeof(map->ranges[0]) * count;
Simon Glass30d73e82016-07-04 11:58:21 -060053
Pratyush Yadav279cad82020-09-24 10:04:11 +053054 map = calloc(1, size);
Simon Glass30d73e82016-07-04 11:58:21 -060055 if (!map)
56 return NULL;
Simon Glass30d73e82016-07-04 11:58:21 -060057 map->range_count = count;
Pratyush Yadav1c9867c2020-09-24 10:04:12 +053058 map->width = REGMAP_SIZE_32;
Simon Glass30d73e82016-07-04 11:58:21 -060059
60 return map;
61}
62
Simon Glassb9443452016-07-04 11:57:59 -060063#if CONFIG_IS_ENABLED(OF_PLATDATA)
Simon Glassb75b15b2020-12-03 16:55:23 -070064int regmap_init_mem_plat(struct udevice *dev, fdt_val_t *reg, int count,
65 struct regmap **mapp)
Simon Glassb9443452016-07-04 11:57:59 -060066{
Simon Glassb6114332016-07-04 11:58:22 -060067 struct regmap_range *range;
68 struct regmap *map;
69
Masahiro Yamada54c5ecb2018-04-19 12:14:01 +090070 map = regmap_alloc(count);
Simon Glassb6114332016-07-04 11:58:22 -060071 if (!map)
72 return -ENOMEM;
73
Masahiro Yamada54c5ecb2018-04-19 12:14:01 +090074 for (range = map->ranges; count > 0; reg += 2, range++, count--) {
Simon Glassb6114332016-07-04 11:58:22 -060075 range->start = *reg;
76 range->size = reg[1];
77 }
78
79 *mapp = map;
80
Simon Glassb9443452016-07-04 11:57:59 -060081 return 0;
82}
83#else
Mario Six5159c0c2018-10-15 09:24:07 +020084/**
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
91 *
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
94 * quesion.
95 *
96 * Return: 0 if OK, -ve on error
97 */
98static int init_range(ofnode node, struct regmap_range *range, int addr_len,
99 int size_len, int index)
100{
101 fdt_size_t sz;
102 struct resource r;
103
104 if (of_live_active()) {
105 int ret;
106
107 ret = of_address_to_resource(ofnode_to_np(node),
108 index, &r);
109 if (ret) {
110 debug("%s: Could not read resource of range %d (ret = %d)\n",
111 ofnode_get_name(node), index, ret);
112 return ret;
113 }
114
115 range->start = r.start;
116 range->size = r.end - r.start + 1;
117 } else {
118 int offset = ofnode_to_offset(node);
119
120 range->start = fdtdec_get_addr_size_fixed(gd->fdt_blob, offset,
121 "reg", index,
122 addr_len, size_len,
123 &sz, true);
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);
127 return -EINVAL;
128 }
129
130 range->size = sz;
131 }
132
133 return 0;
134}
135
Faiz Abbas52742f42019-06-11 00:43:33 +0530136int regmap_init_mem_index(ofnode node, struct regmap **mapp, int index)
137{
138 struct regmap *map;
139 int addr_len, size_len;
140 int ret;
141
142 addr_len = ofnode_read_simple_addr_cells(ofnode_get_parent(node));
143 if (addr_len < 0) {
144 debug("%s: Error while reading the addr length (ret = %d)\n",
145 ofnode_get_name(node), addr_len);
146 return addr_len;
147 }
148
149 size_len = ofnode_read_simple_size_cells(ofnode_get_parent(node));
150 if (size_len < 0) {
151 debug("%s: Error while reading the size length: (ret = %d)\n",
152 ofnode_get_name(node), size_len);
153 return size_len;
154 }
155
156 map = regmap_alloc(1);
157 if (!map)
158 return -ENOMEM;
159
160 ret = init_range(node, map->ranges, addr_len, size_len, index);
161 if (ret)
Faiz Abbas4bba4132019-11-11 15:29:05 +0530162 goto err;
Faiz Abbas52742f42019-06-11 00:43:33 +0530163
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;
172
173 *mapp = map;
174
Faiz Abbas4bba4132019-11-11 15:29:05 +0530175 return 0;
176err:
177 regmap_uninit(map);
178
Faiz Abbas52742f42019-06-11 00:43:33 +0530179 return ret;
180}
181
Pratyush Yadav7eb24762020-09-24 10:04:14 +0530182int regmap_init_mem_range(ofnode node, ulong r_start, ulong r_size,
183 struct regmap **mapp)
184{
185 struct regmap *map;
186 struct regmap_range *range;
187
188 map = regmap_alloc(1);
189 if (!map)
190 return -ENOMEM;
191
192 range = &map->ranges[0];
193 range->start = r_start;
194 range->size = r_size;
195
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;
204
205 *mapp = map;
206 return 0;
207}
208
Masahiro Yamadae4873e32018-04-19 12:14:03 +0900209int regmap_init_mem(ofnode node, struct regmap **mapp)
Simon Glassad8f8ab2015-06-23 15:38:42 -0600210{
Simon Glassad8f8ab2015-06-23 15:38:42 -0600211 struct regmap_range *range;
Simon Glassad8f8ab2015-06-23 15:38:42 -0600212 struct regmap *map;
213 int count;
214 int addr_len, size_len, both_len;
Simon Glassad8f8ab2015-06-23 15:38:42 -0600215 int len;
Jean-Jacques Hiblot024611b2017-02-13 16:17:48 +0100216 int index;
Faiz Abbas4bba4132019-11-11 15:29:05 +0530217 int ret;
Simon Glassad8f8ab2015-06-23 15:38:42 -0600218
Masahiro Yamadae4873e32018-04-19 12:14:03 +0900219 addr_len = ofnode_read_simple_addr_cells(ofnode_get_parent(node));
Mario Six12321162018-10-04 09:00:43 +0200220 if (addr_len < 0) {
221 debug("%s: Error while reading the addr length (ret = %d)\n",
222 ofnode_get_name(node), addr_len);
223 return addr_len;
224 }
225
Masahiro Yamadae4873e32018-04-19 12:14:03 +0900226 size_len = ofnode_read_simple_size_cells(ofnode_get_parent(node));
Mario Six12321162018-10-04 09:00:43 +0200227 if (size_len < 0) {
228 debug("%s: Error while reading the size length: (ret = %d)\n",
229 ofnode_get_name(node), size_len);
230 return size_len;
231 }
232
Simon Glassad8f8ab2015-06-23 15:38:42 -0600233 both_len = addr_len + size_len;
Mario Six12321162018-10-04 09:00:43 +0200234 if (!both_len) {
235 debug("%s: Both addr and size length are zero\n",
236 ofnode_get_name(node));
237 return -EINVAL;
238 }
Simon Glassad8f8ab2015-06-23 15:38:42 -0600239
Masahiro Yamadae4873e32018-04-19 12:14:03 +0900240 len = ofnode_read_size(node, "reg");
Mario Six6e96ba22018-10-15 09:24:08 +0200241 if (len < 0) {
242 debug("%s: Error while reading reg size (ret = %d)\n",
243 ofnode_get_name(node), len);
Simon Glasseeeb5192017-05-18 20:09:10 -0600244 return len;
Mario Six6e96ba22018-10-15 09:24:08 +0200245 }
Simon Glasseeeb5192017-05-18 20:09:10 -0600246 len /= sizeof(fdt32_t);
Simon Glassad8f8ab2015-06-23 15:38:42 -0600247 count = len / both_len;
Mario Six6e96ba22018-10-15 09:24:08 +0200248 if (!count) {
249 debug("%s: Not enough data in reg property\n",
250 ofnode_get_name(node));
Simon Glassad8f8ab2015-06-23 15:38:42 -0600251 return -EINVAL;
Mario Six6e96ba22018-10-15 09:24:08 +0200252 }
Simon Glassad8f8ab2015-06-23 15:38:42 -0600253
Masahiro Yamada54c5ecb2018-04-19 12:14:01 +0900254 map = regmap_alloc(count);
Simon Glassad8f8ab2015-06-23 15:38:42 -0600255 if (!map)
256 return -ENOMEM;
257
Masahiro Yamada54c5ecb2018-04-19 12:14:01 +0900258 for (range = map->ranges, index = 0; count > 0;
Simon Glasseeeb5192017-05-18 20:09:10 -0600259 count--, range++, index++) {
Faiz Abbas4bba4132019-11-11 15:29:05 +0530260 ret = init_range(node, range, addr_len, size_len, index);
Mario Six5159c0c2018-10-15 09:24:07 +0200261 if (ret)
Faiz Abbas4bba4132019-11-11 15:29:05 +0530262 goto err;
Simon Glassad8f8ab2015-06-23 15:38:42 -0600263 }
264
Mario Sixe5a3d5b2018-10-15 09:24:14 +0200265 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;
273
Simon Glassad8f8ab2015-06-23 15:38:42 -0600274 *mapp = map;
275
276 return 0;
Faiz Abbas4bba4132019-11-11 15:29:05 +0530277err:
278 regmap_uninit(map);
279
280 return ret;
Simon Glassad8f8ab2015-06-23 15:38:42 -0600281}
Jean-Jacques Hiblotde03d122020-09-24 10:04:10 +0530282
283static void devm_regmap_release(struct udevice *dev, void *res)
284{
285 regmap_uninit(*(struct regmap **)res);
286}
287
288struct regmap *devm_regmap_init(struct udevice *dev,
289 const struct regmap_bus *bus,
290 void *bus_context,
291 const struct regmap_config *config)
292{
293 int rc;
Pratyush Yadav1c9867c2020-09-24 10:04:12 +0530294 struct regmap **mapp, *map;
Jean-Jacques Hiblotde03d122020-09-24 10:04:10 +0530295
296 mapp = devres_alloc(devm_regmap_release, sizeof(struct regmap *),
297 __GFP_ZERO);
298 if (unlikely(!mapp))
299 return ERR_PTR(-ENOMEM);
300
Pratyush Yadav3e4e50a2020-09-24 10:04:15 +0530301 if (config && config->r_size != 0)
302 rc = regmap_init_mem_range(dev_ofnode(dev), config->r_start,
303 config->r_size, mapp);
304 else
305 rc = regmap_init_mem(dev_ofnode(dev), mapp);
Jean-Jacques Hiblotde03d122020-09-24 10:04:10 +0530306 if (rc)
307 return ERR_PTR(rc);
308
Pratyush Yadav1c9867c2020-09-24 10:04:12 +0530309 map = *mapp;
Pratyush Yadav3b94e5d2020-09-24 10:04:13 +0530310 if (config) {
Pratyush Yadav1c9867c2020-09-24 10:04:12 +0530311 map->width = config->width;
Pratyush Yadav3b94e5d2020-09-24 10:04:13 +0530312 map->reg_offset_shift = config->reg_offset_shift;
313 }
Pratyush Yadav1c9867c2020-09-24 10:04:12 +0530314
Jean-Jacques Hiblotde03d122020-09-24 10:04:10 +0530315 devres_add(dev, mapp);
316 return *mapp;
317}
Simon Glassb9443452016-07-04 11:57:59 -0600318#endif
Simon Glassad8f8ab2015-06-23 15:38:42 -0600319
320void *regmap_get_range(struct regmap *map, unsigned int range_num)
321{
322 struct regmap_range *range;
323
324 if (range_num >= map->range_count)
325 return NULL;
Masahiro Yamada54c5ecb2018-04-19 12:14:01 +0900326 range = &map->ranges[range_num];
Simon Glassad8f8ab2015-06-23 15:38:42 -0600327
328 return map_sysmem(range->start, range->size);
329}
330
331int regmap_uninit(struct regmap *map)
332{
Simon Glassad8f8ab2015-06-23 15:38:42 -0600333 free(map);
334
335 return 0;
336}
Paul Burton39776032016-09-08 07:47:35 +0100337
Mario Sixe5a3d5b2018-10-15 09:24:14 +0200338static inline u8 __read_8(u8 *addr, enum regmap_endianness_t endianness)
339{
340 return readb(addr);
341}
342
343static inline u16 __read_16(u16 *addr, enum regmap_endianness_t endianness)
344{
345 switch (endianness) {
346 case REGMAP_LITTLE_ENDIAN:
347 return in_le16(addr);
348 case REGMAP_BIG_ENDIAN:
349 return in_be16(addr);
350 case REGMAP_NATIVE_ENDIAN:
351 return readw(addr);
352 }
353
354 return readw(addr);
355}
356
357static inline u32 __read_32(u32 *addr, enum regmap_endianness_t endianness)
358{
359 switch (endianness) {
360 case REGMAP_LITTLE_ENDIAN:
361 return in_le32(addr);
362 case REGMAP_BIG_ENDIAN:
363 return in_be32(addr);
364 case REGMAP_NATIVE_ENDIAN:
365 return readl(addr);
366 }
367
368 return readl(addr);
369}
370
371#if defined(in_le64) && defined(in_be64) && defined(readq)
372static inline u64 __read_64(u64 *addr, enum regmap_endianness_t endianness)
373{
374 switch (endianness) {
375 case REGMAP_LITTLE_ENDIAN:
376 return in_le64(addr);
377 case REGMAP_BIG_ENDIAN:
378 return in_be64(addr);
379 case REGMAP_NATIVE_ENDIAN:
380 return readq(addr);
381 }
382
383 return readq(addr);
384}
385#endif
386
Mario Six2f009972018-10-15 09:24:11 +0200387int regmap_raw_read_range(struct regmap *map, uint range_num, uint offset,
388 void *valp, size_t val_len)
Paul Burton39776032016-09-08 07:47:35 +0100389{
Mario Six2f009972018-10-15 09:24:11 +0200390 struct regmap_range *range;
Mario Sixa4fd59e2018-10-15 09:24:10 +0200391 void *ptr;
Paul Burton39776032016-09-08 07:47:35 +0100392
Mario Six2f009972018-10-15 09:24:11 +0200393 if (range_num >= map->range_count) {
394 debug("%s: range index %d larger than range count\n",
395 __func__, range_num);
396 return -ERANGE;
397 }
398 range = &map->ranges[range_num];
399
Pratyush Yadav3b94e5d2020-09-24 10:04:13 +0530400 offset <<= map->reg_offset_shift;
Mario Six2f009972018-10-15 09:24:11 +0200401 if (offset + val_len > range->size) {
402 debug("%s: offset/size combination invalid\n", __func__);
403 return -ERANGE;
404 }
Paul Burton39776032016-09-08 07:47:35 +0100405
Pratyush Yadav5c42d5d2020-05-26 17:35:57 +0530406 ptr = map_physmem(range->start + offset, val_len, MAP_NOCACHE);
407
Mario Sixa4fd59e2018-10-15 09:24:10 +0200408 switch (val_len) {
409 case REGMAP_SIZE_8:
Mario Sixe5a3d5b2018-10-15 09:24:14 +0200410 *((u8 *)valp) = __read_8(ptr, map->endianness);
Mario Sixa4fd59e2018-10-15 09:24:10 +0200411 break;
412 case REGMAP_SIZE_16:
Mario Sixe5a3d5b2018-10-15 09:24:14 +0200413 *((u16 *)valp) = __read_16(ptr, map->endianness);
Mario Sixa4fd59e2018-10-15 09:24:10 +0200414 break;
415 case REGMAP_SIZE_32:
Mario Sixe5a3d5b2018-10-15 09:24:14 +0200416 *((u32 *)valp) = __read_32(ptr, map->endianness);
Mario Sixa4fd59e2018-10-15 09:24:10 +0200417 break;
Mario Sixe5a3d5b2018-10-15 09:24:14 +0200418#if defined(in_le64) && defined(in_be64) && defined(readq)
Mario Sixa4fd59e2018-10-15 09:24:10 +0200419 case REGMAP_SIZE_64:
Mario Sixe5a3d5b2018-10-15 09:24:14 +0200420 *((u64 *)valp) = __read_64(ptr, map->endianness);
Mario Sixa4fd59e2018-10-15 09:24:10 +0200421 break;
422#endif
423 default:
424 debug("%s: regmap size %zu unknown\n", __func__, val_len);
425 return -EINVAL;
426 }
Mario Six2f009972018-10-15 09:24:11 +0200427
Paul Burton39776032016-09-08 07:47:35 +0100428 return 0;
429}
430
Mario Six2f009972018-10-15 09:24:11 +0200431int regmap_raw_read(struct regmap *map, uint offset, void *valp, size_t val_len)
432{
433 return regmap_raw_read_range(map, 0, offset, valp, val_len);
434}
435
Mario Sixa4fd59e2018-10-15 09:24:10 +0200436int regmap_read(struct regmap *map, uint offset, uint *valp)
Paul Burton39776032016-09-08 07:47:35 +0100437{
Marek BehĂșn10448862021-05-20 13:23:50 +0200438 union {
439 u8 v8;
440 u16 v16;
441 u32 v32;
442 u64 v64;
443 } u;
444 int res;
445
446 res = regmap_raw_read(map, offset, &u, map->width);
447 if (res)
448 return res;
449
450 switch (map->width) {
451 case REGMAP_SIZE_8:
452 *valp = u.v8;
453 break;
454 case REGMAP_SIZE_16:
455 *valp = u.v16;
456 break;
457 case REGMAP_SIZE_32:
458 *valp = u.v32;
459 break;
460 case REGMAP_SIZE_64:
461 *valp = u.v64;
462 break;
463 default:
464 unreachable();
465 }
466
467 return 0;
Mario Sixa4fd59e2018-10-15 09:24:10 +0200468}
469
Mario Sixe5a3d5b2018-10-15 09:24:14 +0200470static inline void __write_8(u8 *addr, const u8 *val,
471 enum regmap_endianness_t endianness)
472{
473 writeb(*val, addr);
474}
475
476static inline void __write_16(u16 *addr, const u16 *val,
477 enum regmap_endianness_t endianness)
478{
479 switch (endianness) {
480 case REGMAP_NATIVE_ENDIAN:
481 writew(*val, addr);
482 break;
483 case REGMAP_LITTLE_ENDIAN:
484 out_le16(addr, *val);
485 break;
486 case REGMAP_BIG_ENDIAN:
487 out_be16(addr, *val);
488 break;
489 }
490}
491
492static inline void __write_32(u32 *addr, const u32 *val,
493 enum regmap_endianness_t endianness)
494{
495 switch (endianness) {
496 case REGMAP_NATIVE_ENDIAN:
497 writel(*val, addr);
498 break;
499 case REGMAP_LITTLE_ENDIAN:
500 out_le32(addr, *val);
501 break;
502 case REGMAP_BIG_ENDIAN:
503 out_be32(addr, *val);
504 break;
505 }
506}
507
508#if defined(out_le64) && defined(out_be64) && defined(writeq)
509static inline void __write_64(u64 *addr, const u64 *val,
510 enum regmap_endianness_t endianness)
511{
512 switch (endianness) {
513 case REGMAP_NATIVE_ENDIAN:
514 writeq(*val, addr);
515 break;
516 case REGMAP_LITTLE_ENDIAN:
517 out_le64(addr, *val);
518 break;
519 case REGMAP_BIG_ENDIAN:
520 out_be64(addr, *val);
521 break;
522 }
523}
524#endif
525
Mario Six2f009972018-10-15 09:24:11 +0200526int regmap_raw_write_range(struct regmap *map, uint range_num, uint offset,
527 const void *val, size_t val_len)
Mario Sixa4fd59e2018-10-15 09:24:10 +0200528{
Mario Six2f009972018-10-15 09:24:11 +0200529 struct regmap_range *range;
Mario Sixa4fd59e2018-10-15 09:24:10 +0200530 void *ptr;
Paul Burton39776032016-09-08 07:47:35 +0100531
Mario Six2f009972018-10-15 09:24:11 +0200532 if (range_num >= map->range_count) {
533 debug("%s: range index %d larger than range count\n",
534 __func__, range_num);
535 return -ERANGE;
536 }
537 range = &map->ranges[range_num];
538
Pratyush Yadav3b94e5d2020-09-24 10:04:13 +0530539 offset <<= map->reg_offset_shift;
Mario Six2f009972018-10-15 09:24:11 +0200540 if (offset + val_len > range->size) {
541 debug("%s: offset/size combination invalid\n", __func__);
542 return -ERANGE;
543 }
Mario Sixa4fd59e2018-10-15 09:24:10 +0200544
Pratyush Yadav5c42d5d2020-05-26 17:35:57 +0530545 ptr = map_physmem(range->start + offset, val_len, MAP_NOCACHE);
546
Mario Sixa4fd59e2018-10-15 09:24:10 +0200547 switch (val_len) {
548 case REGMAP_SIZE_8:
Mario Sixe5a3d5b2018-10-15 09:24:14 +0200549 __write_8(ptr, val, map->endianness);
Mario Sixa4fd59e2018-10-15 09:24:10 +0200550 break;
551 case REGMAP_SIZE_16:
Mario Sixe5a3d5b2018-10-15 09:24:14 +0200552 __write_16(ptr, val, map->endianness);
Mario Sixa4fd59e2018-10-15 09:24:10 +0200553 break;
554 case REGMAP_SIZE_32:
Mario Sixe5a3d5b2018-10-15 09:24:14 +0200555 __write_32(ptr, val, map->endianness);
Mario Sixa4fd59e2018-10-15 09:24:10 +0200556 break;
Mario Sixe5a3d5b2018-10-15 09:24:14 +0200557#if defined(out_le64) && defined(out_be64) && defined(writeq)
Mario Sixa4fd59e2018-10-15 09:24:10 +0200558 case REGMAP_SIZE_64:
Mario Sixe5a3d5b2018-10-15 09:24:14 +0200559 __write_64(ptr, val, map->endianness);
Mario Sixa4fd59e2018-10-15 09:24:10 +0200560 break;
561#endif
562 default:
563 debug("%s: regmap size %zu unknown\n", __func__, val_len);
564 return -EINVAL;
565 }
Paul Burton39776032016-09-08 07:47:35 +0100566
567 return 0;
568}
Neil Armstrong5444ec62018-04-27 11:56:14 +0200569
Mario Six2f009972018-10-15 09:24:11 +0200570int regmap_raw_write(struct regmap *map, uint offset, const void *val,
571 size_t val_len)
572{
573 return regmap_raw_write_range(map, 0, offset, val, val_len);
574}
575
Mario Sixa4fd59e2018-10-15 09:24:10 +0200576int regmap_write(struct regmap *map, uint offset, uint val)
577{
Marek BehĂșn10448862021-05-20 13:23:50 +0200578 union {
579 u8 v8;
580 u16 v16;
581 u32 v32;
582 u64 v64;
583 } u;
584
585 switch (map->width) {
586 case REGMAP_SIZE_8:
587 u.v8 = val;
588 break;
589 case REGMAP_SIZE_16:
590 u.v16 = val;
591 break;
592 case REGMAP_SIZE_32:
593 u.v32 = val;
594 break;
595 case REGMAP_SIZE_64:
596 u.v64 = val;
597 break;
598 default:
599 debug("%s: regmap size %zu unknown\n", __func__,
600 (size_t)map->width);
601 return -EINVAL;
602 }
603
604 return regmap_raw_write(map, offset, &u, map->width);
Mario Sixa4fd59e2018-10-15 09:24:10 +0200605}
606
Neil Armstrong5444ec62018-04-27 11:56:14 +0200607int regmap_update_bits(struct regmap *map, uint offset, uint mask, uint val)
608{
609 uint reg;
610 int ret;
611
612 ret = regmap_read(map, offset, &reg);
613 if (ret)
614 return ret;
615
616 reg &= ~mask;
617
Simon Glass56bed2a2019-10-11 16:16:49 -0600618 return regmap_write(map, offset, reg | (val & mask));
Neil Armstrong5444ec62018-04-27 11:56:14 +0200619}
Jean-Jacques Hiblotb3bbbcf2020-09-24 10:04:16 +0530620
621int regmap_field_read(struct regmap_field *field, unsigned int *val)
622{
623 int ret;
624 unsigned int reg_val;
625
626 ret = regmap_read(field->regmap, field->reg, &reg_val);
627 if (ret != 0)
628 return ret;
629
630 reg_val &= field->mask;
631 reg_val >>= field->shift;
632 *val = reg_val;
633
634 return ret;
635}
636
637int regmap_field_write(struct regmap_field *field, unsigned int val)
638{
639 return regmap_update_bits(field->regmap, field->reg, field->mask,
640 val << field->shift);
641}
642
643static void regmap_field_init(struct regmap_field *rm_field,
644 struct regmap *regmap,
645 struct reg_field reg_field)
646{
647 rm_field->regmap = regmap;
648 rm_field->reg = reg_field.reg;
649 rm_field->shift = reg_field.lsb;
650 rm_field->mask = GENMASK(reg_field.msb, reg_field.lsb);
651}
652
653struct regmap_field *devm_regmap_field_alloc(struct udevice *dev,
654 struct regmap *regmap,
655 struct reg_field reg_field)
656{
657 struct regmap_field *rm_field = devm_kzalloc(dev, sizeof(*rm_field),
658 GFP_KERNEL);
659 if (!rm_field)
660 return ERR_PTR(-ENOMEM);
661
662 regmap_field_init(rm_field, regmap, reg_field);
663
664 return rm_field;
665}
666
667void devm_regmap_field_free(struct udevice *dev, struct regmap_field *field)
668{
669 devm_kfree(dev, field);
670}
671
672struct regmap_field *regmap_field_alloc(struct regmap *regmap,
673 struct reg_field reg_field)
674{
675 struct regmap_field *rm_field = kzalloc(sizeof(*rm_field), GFP_KERNEL);
676
677 if (!rm_field)
678 return ERR_PTR(-ENOMEM);
679
680 regmap_field_init(rm_field, regmap, reg_field);
681
682 return rm_field;
683}
684
685void regmap_field_free(struct regmap_field *field)
686{
687 kfree(field);
688}