blob: dd32328098c42d61d5be496bba40cf1c6282e5a4 [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
Simon Glass9e0a0902022-10-15 08:08:54 -06007#define LOG_CATEGORY LOGC_DM
8
Simon Glassad8f8ab2015-06-23 15:38:42 -06009#include <common.h>
10#include <dm.h>
11#include <errno.h>
Simon Glass0f2af882020-05-10 11:40:05 -060012#include <log.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060013#include <asm/global_data.h>
Masahiro Yamada75f82d02018-03-05 01:20:11 +090014#include <linux/libfdt.h>
Simon Glassad8f8ab2015-06-23 15:38:42 -060015#include <malloc.h>
16#include <mapmem.h>
17#include <regmap.h>
Paul Burton39776032016-09-08 07:47:35 +010018#include <asm/io.h>
Simon Glasseeeb5192017-05-18 20:09:10 -060019#include <dm/of_addr.h>
Jean-Jacques Hiblotde03d122020-09-24 10:04:10 +053020#include <dm/devres.h>
Simon Glasseeeb5192017-05-18 20:09:10 -060021#include <linux/ioport.h>
Jean-Jacques Hiblotde03d122020-09-24 10:04:10 +053022#include <linux/compat.h>
23#include <linux/err.h>
Jean-Jacques Hiblotb3bbbcf2020-09-24 10:04:16 +053024#include <linux/bitops.h>
25
26/*
27 * Internal representation of a regmap field. Instead of storing the MSB and
28 * LSB, store the shift and mask. This makes the code a bit cleaner and faster
29 * because the shift and mask don't have to be calculated every time.
30 */
31struct regmap_field {
32 struct regmap *regmap;
33 unsigned int mask;
34 /* lsb */
35 unsigned int shift;
36 unsigned int reg;
37};
Paul Burton39776032016-09-08 07:47:35 +010038
Simon Glassad8f8ab2015-06-23 15:38:42 -060039DECLARE_GLOBAL_DATA_PTR;
40
Mario Six0aa52992018-10-04 09:00:42 +020041/**
Simon Glass9e0a0902022-10-15 08:08:54 -060042 * do_range_check() - Control whether range checks are done
43 *
44 * Returns: true to do range checks, false to skip
45 *
46 * This is used to reduce code size on SPL where range checks are known not to
47 * be needed
48 *
49 * Add this to the top of the file to enable them: #define LOG_DEBUG
50 */
51static inline bool do_range_check(void)
52{
53 return _LOG_DEBUG || !IS_ENABLED(CONFIG_SPL);
54
55}
56
57/**
Mario Six0aa52992018-10-04 09:00:42 +020058 * regmap_alloc() - Allocate a regmap with a given number of ranges.
59 *
60 * @count: Number of ranges to be allocated for the regmap.
Pratyush Yadav1c9867c2020-09-24 10:04:12 +053061 *
62 * The default regmap width is set to REGMAP_SIZE_32. Callers can override it
63 * if they need.
64 *
Mario Six0aa52992018-10-04 09:00:42 +020065 * Return: A pointer to the newly allocated regmap, or NULL on error.
66 */
Masahiro Yamada54c5ecb2018-04-19 12:14:01 +090067static struct regmap *regmap_alloc(int count)
Simon Glass30d73e82016-07-04 11:58:21 -060068{
69 struct regmap *map;
Pratyush Yadav279cad82020-09-24 10:04:11 +053070 size_t size = sizeof(*map) + sizeof(map->ranges[0]) * count;
Simon Glass30d73e82016-07-04 11:58:21 -060071
Pratyush Yadav279cad82020-09-24 10:04:11 +053072 map = calloc(1, size);
Simon Glass30d73e82016-07-04 11:58:21 -060073 if (!map)
74 return NULL;
Simon Glass30d73e82016-07-04 11:58:21 -060075 map->range_count = count;
Pratyush Yadav1c9867c2020-09-24 10:04:12 +053076 map->width = REGMAP_SIZE_32;
Simon Glass30d73e82016-07-04 11:58:21 -060077
78 return map;
79}
80
Simon Glassb9443452016-07-04 11:57:59 -060081#if CONFIG_IS_ENABLED(OF_PLATDATA)
Johan Jonker2e304a22023-03-13 01:30:46 +010082int regmap_init_mem_plat(struct udevice *dev, void *reg, int size, int count,
Simon Glassb75b15b2020-12-03 16:55:23 -070083 struct regmap **mapp)
Simon Glassb9443452016-07-04 11:57:59 -060084{
Simon Glassb6114332016-07-04 11:58:22 -060085 struct regmap_range *range;
86 struct regmap *map;
87
Masahiro Yamada54c5ecb2018-04-19 12:14:01 +090088 map = regmap_alloc(count);
Simon Glassb6114332016-07-04 11:58:22 -060089 if (!map)
90 return -ENOMEM;
91
Johan Jonker2e304a22023-03-13 01:30:46 +010092 if (size == sizeof(fdt32_t)) {
93 fdt32_t *ptr = (fdt32_t *)reg;
94
95 for (range = map->ranges; count > 0;
96 ptr += 2, range++, count--) {
97 range->start = *ptr;
98 range->size = ptr[1];
99 }
100 } else if (size == sizeof(fdt64_t)) {
101 fdt64_t *ptr = (fdt64_t *)reg;
102
103 for (range = map->ranges; count > 0;
104 ptr += 2, range++, count--) {
105 range->start = *ptr;
106 range->size = ptr[1];
107 }
108 } else {
109 return -EINVAL;
Simon Glassb6114332016-07-04 11:58:22 -0600110 }
111
112 *mapp = map;
113
Simon Glassb9443452016-07-04 11:57:59 -0600114 return 0;
115}
116#else
Mario Six5159c0c2018-10-15 09:24:07 +0200117/**
118 * init_range() - Initialize a single range of a regmap
119 * @node: Device node that will use the map in question
120 * @range: Pointer to a regmap_range structure that will be initialized
121 * @addr_len: The length of the addr parts of the reg property
122 * @size_len: The length of the size parts of the reg property
123 * @index: The index of the range to initialize
124 *
125 * This function will read the necessary 'reg' information from the device tree
126 * (the 'addr' part, and the 'length' part), and initialize the range in
127 * quesion.
128 *
129 * Return: 0 if OK, -ve on error
130 */
131static int init_range(ofnode node, struct regmap_range *range, int addr_len,
132 int size_len, int index)
133{
134 fdt_size_t sz;
135 struct resource r;
136
137 if (of_live_active()) {
138 int ret;
139
140 ret = of_address_to_resource(ofnode_to_np(node),
141 index, &r);
142 if (ret) {
143 debug("%s: Could not read resource of range %d (ret = %d)\n",
144 ofnode_get_name(node), index, ret);
145 return ret;
146 }
147
148 range->start = r.start;
149 range->size = r.end - r.start + 1;
150 } else {
151 int offset = ofnode_to_offset(node);
152
153 range->start = fdtdec_get_addr_size_fixed(gd->fdt_blob, offset,
154 "reg", index,
155 addr_len, size_len,
156 &sz, true);
157 if (range->start == FDT_ADDR_T_NONE) {
158 debug("%s: Could not read start of range %d\n",
159 ofnode_get_name(node), index);
160 return -EINVAL;
161 }
162
163 range->size = sz;
164 }
165
166 return 0;
167}
168
Faiz Abbas52742f42019-06-11 00:43:33 +0530169int regmap_init_mem_index(ofnode node, struct regmap **mapp, int index)
170{
171 struct regmap *map;
172 int addr_len, size_len;
173 int ret;
174
175 addr_len = ofnode_read_simple_addr_cells(ofnode_get_parent(node));
176 if (addr_len < 0) {
177 debug("%s: Error while reading the addr length (ret = %d)\n",
178 ofnode_get_name(node), addr_len);
179 return addr_len;
180 }
181
182 size_len = ofnode_read_simple_size_cells(ofnode_get_parent(node));
183 if (size_len < 0) {
184 debug("%s: Error while reading the size length: (ret = %d)\n",
185 ofnode_get_name(node), size_len);
186 return size_len;
187 }
188
189 map = regmap_alloc(1);
190 if (!map)
191 return -ENOMEM;
192
193 ret = init_range(node, map->ranges, addr_len, size_len, index);
194 if (ret)
Faiz Abbas4bba4132019-11-11 15:29:05 +0530195 goto err;
Faiz Abbas52742f42019-06-11 00:43:33 +0530196
197 if (ofnode_read_bool(node, "little-endian"))
198 map->endianness = REGMAP_LITTLE_ENDIAN;
199 else if (ofnode_read_bool(node, "big-endian"))
200 map->endianness = REGMAP_BIG_ENDIAN;
201 else if (ofnode_read_bool(node, "native-endian"))
202 map->endianness = REGMAP_NATIVE_ENDIAN;
203 else /* Default: native endianness */
204 map->endianness = REGMAP_NATIVE_ENDIAN;
205
206 *mapp = map;
207
Faiz Abbas4bba4132019-11-11 15:29:05 +0530208 return 0;
209err:
210 regmap_uninit(map);
211
Faiz Abbas52742f42019-06-11 00:43:33 +0530212 return ret;
213}
214
Pratyush Yadav7eb24762020-09-24 10:04:14 +0530215int regmap_init_mem_range(ofnode node, ulong r_start, ulong r_size,
216 struct regmap **mapp)
217{
218 struct regmap *map;
219 struct regmap_range *range;
220
221 map = regmap_alloc(1);
222 if (!map)
223 return -ENOMEM;
224
225 range = &map->ranges[0];
226 range->start = r_start;
227 range->size = r_size;
228
229 if (ofnode_read_bool(node, "little-endian"))
230 map->endianness = REGMAP_LITTLE_ENDIAN;
231 else if (ofnode_read_bool(node, "big-endian"))
232 map->endianness = REGMAP_BIG_ENDIAN;
233 else if (ofnode_read_bool(node, "native-endian"))
234 map->endianness = REGMAP_NATIVE_ENDIAN;
235 else /* Default: native endianness */
236 map->endianness = REGMAP_NATIVE_ENDIAN;
237
238 *mapp = map;
239 return 0;
240}
241
Masahiro Yamadae4873e32018-04-19 12:14:03 +0900242int regmap_init_mem(ofnode node, struct regmap **mapp)
Simon Glassad8f8ab2015-06-23 15:38:42 -0600243{
Simon Glassad8f8ab2015-06-23 15:38:42 -0600244 struct regmap_range *range;
Simon Glassad8f8ab2015-06-23 15:38:42 -0600245 struct regmap *map;
246 int count;
247 int addr_len, size_len, both_len;
Simon Glassad8f8ab2015-06-23 15:38:42 -0600248 int len;
Jean-Jacques Hiblot024611b2017-02-13 16:17:48 +0100249 int index;
Faiz Abbas4bba4132019-11-11 15:29:05 +0530250 int ret;
Simon Glassad8f8ab2015-06-23 15:38:42 -0600251
Masahiro Yamadae4873e32018-04-19 12:14:03 +0900252 addr_len = ofnode_read_simple_addr_cells(ofnode_get_parent(node));
Mario Six12321162018-10-04 09:00:43 +0200253 if (addr_len < 0) {
254 debug("%s: Error while reading the addr length (ret = %d)\n",
255 ofnode_get_name(node), addr_len);
256 return addr_len;
257 }
258
Masahiro Yamadae4873e32018-04-19 12:14:03 +0900259 size_len = ofnode_read_simple_size_cells(ofnode_get_parent(node));
Mario Six12321162018-10-04 09:00:43 +0200260 if (size_len < 0) {
261 debug("%s: Error while reading the size length: (ret = %d)\n",
262 ofnode_get_name(node), size_len);
263 return size_len;
264 }
265
Simon Glassad8f8ab2015-06-23 15:38:42 -0600266 both_len = addr_len + size_len;
Mario Six12321162018-10-04 09:00:43 +0200267 if (!both_len) {
268 debug("%s: Both addr and size length are zero\n",
269 ofnode_get_name(node));
270 return -EINVAL;
271 }
Simon Glassad8f8ab2015-06-23 15:38:42 -0600272
Masahiro Yamadae4873e32018-04-19 12:14:03 +0900273 len = ofnode_read_size(node, "reg");
Mario Six6e96ba22018-10-15 09:24:08 +0200274 if (len < 0) {
275 debug("%s: Error while reading reg size (ret = %d)\n",
276 ofnode_get_name(node), len);
Simon Glasseeeb5192017-05-18 20:09:10 -0600277 return len;
Mario Six6e96ba22018-10-15 09:24:08 +0200278 }
Simon Glasseeeb5192017-05-18 20:09:10 -0600279 len /= sizeof(fdt32_t);
Simon Glassad8f8ab2015-06-23 15:38:42 -0600280 count = len / both_len;
Mario Six6e96ba22018-10-15 09:24:08 +0200281 if (!count) {
282 debug("%s: Not enough data in reg property\n",
283 ofnode_get_name(node));
Simon Glassad8f8ab2015-06-23 15:38:42 -0600284 return -EINVAL;
Mario Six6e96ba22018-10-15 09:24:08 +0200285 }
Simon Glassad8f8ab2015-06-23 15:38:42 -0600286
Masahiro Yamada54c5ecb2018-04-19 12:14:01 +0900287 map = regmap_alloc(count);
Simon Glassad8f8ab2015-06-23 15:38:42 -0600288 if (!map)
289 return -ENOMEM;
290
Masahiro Yamada54c5ecb2018-04-19 12:14:01 +0900291 for (range = map->ranges, index = 0; count > 0;
Simon Glasseeeb5192017-05-18 20:09:10 -0600292 count--, range++, index++) {
Faiz Abbas4bba4132019-11-11 15:29:05 +0530293 ret = init_range(node, range, addr_len, size_len, index);
Mario Six5159c0c2018-10-15 09:24:07 +0200294 if (ret)
Faiz Abbas4bba4132019-11-11 15:29:05 +0530295 goto err;
Simon Glassad8f8ab2015-06-23 15:38:42 -0600296 }
297
Mario Sixe5a3d5b2018-10-15 09:24:14 +0200298 if (ofnode_read_bool(node, "little-endian"))
299 map->endianness = REGMAP_LITTLE_ENDIAN;
300 else if (ofnode_read_bool(node, "big-endian"))
301 map->endianness = REGMAP_BIG_ENDIAN;
302 else if (ofnode_read_bool(node, "native-endian"))
303 map->endianness = REGMAP_NATIVE_ENDIAN;
304 else /* Default: native endianness */
305 map->endianness = REGMAP_NATIVE_ENDIAN;
306
Simon Glassad8f8ab2015-06-23 15:38:42 -0600307 *mapp = map;
308
309 return 0;
Faiz Abbas4bba4132019-11-11 15:29:05 +0530310err:
311 regmap_uninit(map);
312
313 return ret;
Simon Glassad8f8ab2015-06-23 15:38:42 -0600314}
Jean-Jacques Hiblotde03d122020-09-24 10:04:10 +0530315
316static void devm_regmap_release(struct udevice *dev, void *res)
317{
318 regmap_uninit(*(struct regmap **)res);
319}
320
321struct regmap *devm_regmap_init(struct udevice *dev,
322 const struct regmap_bus *bus,
323 void *bus_context,
324 const struct regmap_config *config)
325{
326 int rc;
Pratyush Yadav1c9867c2020-09-24 10:04:12 +0530327 struct regmap **mapp, *map;
Jean-Jacques Hiblotde03d122020-09-24 10:04:10 +0530328
Simon Glasse0ea9792021-05-13 19:39:22 -0600329 /* this looks like a leak, but devres takes care of it */
Jean-Jacques Hiblotde03d122020-09-24 10:04:10 +0530330 mapp = devres_alloc(devm_regmap_release, sizeof(struct regmap *),
331 __GFP_ZERO);
332 if (unlikely(!mapp))
333 return ERR_PTR(-ENOMEM);
334
Pratyush Yadav3e4e50a2020-09-24 10:04:15 +0530335 if (config && config->r_size != 0)
336 rc = regmap_init_mem_range(dev_ofnode(dev), config->r_start,
337 config->r_size, mapp);
338 else
339 rc = regmap_init_mem(dev_ofnode(dev), mapp);
Jean-Jacques Hiblotde03d122020-09-24 10:04:10 +0530340 if (rc)
341 return ERR_PTR(rc);
342
Pratyush Yadav1c9867c2020-09-24 10:04:12 +0530343 map = *mapp;
Pratyush Yadav3b94e5d2020-09-24 10:04:13 +0530344 if (config) {
Pratyush Yadav1c9867c2020-09-24 10:04:12 +0530345 map->width = config->width;
Pratyush Yadav3b94e5d2020-09-24 10:04:13 +0530346 map->reg_offset_shift = config->reg_offset_shift;
347 }
Pratyush Yadav1c9867c2020-09-24 10:04:12 +0530348
Jean-Jacques Hiblotde03d122020-09-24 10:04:10 +0530349 devres_add(dev, mapp);
350 return *mapp;
351}
Simon Glassb9443452016-07-04 11:57:59 -0600352#endif
Simon Glassad8f8ab2015-06-23 15:38:42 -0600353
354void *regmap_get_range(struct regmap *map, unsigned int range_num)
355{
356 struct regmap_range *range;
357
358 if (range_num >= map->range_count)
359 return NULL;
Masahiro Yamada54c5ecb2018-04-19 12:14:01 +0900360 range = &map->ranges[range_num];
Simon Glassad8f8ab2015-06-23 15:38:42 -0600361
362 return map_sysmem(range->start, range->size);
363}
364
365int regmap_uninit(struct regmap *map)
366{
Simon Glassad8f8ab2015-06-23 15:38:42 -0600367 free(map);
368
369 return 0;
370}
Paul Burton39776032016-09-08 07:47:35 +0100371
Mario Sixe5a3d5b2018-10-15 09:24:14 +0200372static inline u8 __read_8(u8 *addr, enum regmap_endianness_t endianness)
373{
374 return readb(addr);
375}
376
377static inline u16 __read_16(u16 *addr, enum regmap_endianness_t endianness)
378{
379 switch (endianness) {
380 case REGMAP_LITTLE_ENDIAN:
381 return in_le16(addr);
382 case REGMAP_BIG_ENDIAN:
383 return in_be16(addr);
384 case REGMAP_NATIVE_ENDIAN:
385 return readw(addr);
386 }
387
388 return readw(addr);
389}
390
391static inline u32 __read_32(u32 *addr, enum regmap_endianness_t endianness)
392{
393 switch (endianness) {
394 case REGMAP_LITTLE_ENDIAN:
395 return in_le32(addr);
396 case REGMAP_BIG_ENDIAN:
397 return in_be32(addr);
398 case REGMAP_NATIVE_ENDIAN:
399 return readl(addr);
400 }
401
402 return readl(addr);
403}
404
405#if defined(in_le64) && defined(in_be64) && defined(readq)
406static inline u64 __read_64(u64 *addr, enum regmap_endianness_t endianness)
407{
408 switch (endianness) {
409 case REGMAP_LITTLE_ENDIAN:
410 return in_le64(addr);
411 case REGMAP_BIG_ENDIAN:
412 return in_be64(addr);
413 case REGMAP_NATIVE_ENDIAN:
414 return readq(addr);
415 }
416
417 return readq(addr);
418}
419#endif
420
Mario Six2f009972018-10-15 09:24:11 +0200421int regmap_raw_read_range(struct regmap *map, uint range_num, uint offset,
422 void *valp, size_t val_len)
Paul Burton39776032016-09-08 07:47:35 +0100423{
Mario Six2f009972018-10-15 09:24:11 +0200424 struct regmap_range *range;
Mario Sixa4fd59e2018-10-15 09:24:10 +0200425 void *ptr;
Paul Burton39776032016-09-08 07:47:35 +0100426
Simon Glass9e0a0902022-10-15 08:08:54 -0600427 if (do_range_check() && range_num >= map->range_count) {
Mario Six2f009972018-10-15 09:24:11 +0200428 debug("%s: range index %d larger than range count\n",
429 __func__, range_num);
430 return -ERANGE;
431 }
432 range = &map->ranges[range_num];
433
Pratyush Yadav3b94e5d2020-09-24 10:04:13 +0530434 offset <<= map->reg_offset_shift;
Simon Glass9e0a0902022-10-15 08:08:54 -0600435 if (do_range_check() &&
436 (offset + val_len > range->size || offset + val_len < offset)) {
Mario Six2f009972018-10-15 09:24:11 +0200437 debug("%s: offset/size combination invalid\n", __func__);
438 return -ERANGE;
439 }
Paul Burton39776032016-09-08 07:47:35 +0100440
Pratyush Yadav5c42d5d2020-05-26 17:35:57 +0530441 ptr = map_physmem(range->start + offset, val_len, MAP_NOCACHE);
442
Mario Sixa4fd59e2018-10-15 09:24:10 +0200443 switch (val_len) {
444 case REGMAP_SIZE_8:
Mario Sixe5a3d5b2018-10-15 09:24:14 +0200445 *((u8 *)valp) = __read_8(ptr, map->endianness);
Mario Sixa4fd59e2018-10-15 09:24:10 +0200446 break;
447 case REGMAP_SIZE_16:
Mario Sixe5a3d5b2018-10-15 09:24:14 +0200448 *((u16 *)valp) = __read_16(ptr, map->endianness);
Mario Sixa4fd59e2018-10-15 09:24:10 +0200449 break;
450 case REGMAP_SIZE_32:
Mario Sixe5a3d5b2018-10-15 09:24:14 +0200451 *((u32 *)valp) = __read_32(ptr, map->endianness);
Mario Sixa4fd59e2018-10-15 09:24:10 +0200452 break;
Mario Sixe5a3d5b2018-10-15 09:24:14 +0200453#if defined(in_le64) && defined(in_be64) && defined(readq)
Mario Sixa4fd59e2018-10-15 09:24:10 +0200454 case REGMAP_SIZE_64:
Mario Sixe5a3d5b2018-10-15 09:24:14 +0200455 *((u64 *)valp) = __read_64(ptr, map->endianness);
Mario Sixa4fd59e2018-10-15 09:24:10 +0200456 break;
457#endif
458 default:
459 debug("%s: regmap size %zu unknown\n", __func__, val_len);
460 return -EINVAL;
461 }
Mario Six2f009972018-10-15 09:24:11 +0200462
Paul Burton39776032016-09-08 07:47:35 +0100463 return 0;
464}
465
Mario Six2f009972018-10-15 09:24:11 +0200466int regmap_raw_read(struct regmap *map, uint offset, void *valp, size_t val_len)
467{
468 return regmap_raw_read_range(map, 0, offset, valp, val_len);
469}
470
Mario Sixa4fd59e2018-10-15 09:24:10 +0200471int regmap_read(struct regmap *map, uint offset, uint *valp)
Paul Burton39776032016-09-08 07:47:35 +0100472{
Marek BehĂșn10448862021-05-20 13:23:50 +0200473 union {
474 u8 v8;
475 u16 v16;
476 u32 v32;
477 u64 v64;
478 } u;
479 int res;
480
481 res = regmap_raw_read(map, offset, &u, map->width);
482 if (res)
483 return res;
484
485 switch (map->width) {
486 case REGMAP_SIZE_8:
487 *valp = u.v8;
488 break;
489 case REGMAP_SIZE_16:
490 *valp = u.v16;
491 break;
492 case REGMAP_SIZE_32:
493 *valp = u.v32;
494 break;
495 case REGMAP_SIZE_64:
496 *valp = u.v64;
497 break;
498 default:
499 unreachable();
500 }
501
502 return 0;
Mario Sixa4fd59e2018-10-15 09:24:10 +0200503}
504
Mario Sixe5a3d5b2018-10-15 09:24:14 +0200505static inline void __write_8(u8 *addr, const u8 *val,
506 enum regmap_endianness_t endianness)
507{
508 writeb(*val, addr);
509}
510
511static inline void __write_16(u16 *addr, const u16 *val,
512 enum regmap_endianness_t endianness)
513{
514 switch (endianness) {
515 case REGMAP_NATIVE_ENDIAN:
516 writew(*val, addr);
517 break;
518 case REGMAP_LITTLE_ENDIAN:
519 out_le16(addr, *val);
520 break;
521 case REGMAP_BIG_ENDIAN:
522 out_be16(addr, *val);
523 break;
524 }
525}
526
527static inline void __write_32(u32 *addr, const u32 *val,
528 enum regmap_endianness_t endianness)
529{
530 switch (endianness) {
531 case REGMAP_NATIVE_ENDIAN:
532 writel(*val, addr);
533 break;
534 case REGMAP_LITTLE_ENDIAN:
535 out_le32(addr, *val);
536 break;
537 case REGMAP_BIG_ENDIAN:
538 out_be32(addr, *val);
539 break;
540 }
541}
542
543#if defined(out_le64) && defined(out_be64) && defined(writeq)
544static inline void __write_64(u64 *addr, const u64 *val,
545 enum regmap_endianness_t endianness)
546{
547 switch (endianness) {
548 case REGMAP_NATIVE_ENDIAN:
549 writeq(*val, addr);
550 break;
551 case REGMAP_LITTLE_ENDIAN:
552 out_le64(addr, *val);
553 break;
554 case REGMAP_BIG_ENDIAN:
555 out_be64(addr, *val);
556 break;
557 }
558}
559#endif
560
Mario Six2f009972018-10-15 09:24:11 +0200561int regmap_raw_write_range(struct regmap *map, uint range_num, uint offset,
562 const void *val, size_t val_len)
Mario Sixa4fd59e2018-10-15 09:24:10 +0200563{
Mario Six2f009972018-10-15 09:24:11 +0200564 struct regmap_range *range;
Mario Sixa4fd59e2018-10-15 09:24:10 +0200565 void *ptr;
Paul Burton39776032016-09-08 07:47:35 +0100566
Mario Six2f009972018-10-15 09:24:11 +0200567 if (range_num >= map->range_count) {
568 debug("%s: range index %d larger than range count\n",
569 __func__, range_num);
570 return -ERANGE;
571 }
572 range = &map->ranges[range_num];
573
Pratyush Yadav3b94e5d2020-09-24 10:04:13 +0530574 offset <<= map->reg_offset_shift;
Heinrich Schuchardt9aaf70e2022-09-29 22:27:06 +0000575 if (offset + val_len > range->size || offset + val_len < offset) {
Mario Six2f009972018-10-15 09:24:11 +0200576 debug("%s: offset/size combination invalid\n", __func__);
577 return -ERANGE;
578 }
Mario Sixa4fd59e2018-10-15 09:24:10 +0200579
Pratyush Yadav5c42d5d2020-05-26 17:35:57 +0530580 ptr = map_physmem(range->start + offset, val_len, MAP_NOCACHE);
581
Mario Sixa4fd59e2018-10-15 09:24:10 +0200582 switch (val_len) {
583 case REGMAP_SIZE_8:
Mario Sixe5a3d5b2018-10-15 09:24:14 +0200584 __write_8(ptr, val, map->endianness);
Mario Sixa4fd59e2018-10-15 09:24:10 +0200585 break;
586 case REGMAP_SIZE_16:
Mario Sixe5a3d5b2018-10-15 09:24:14 +0200587 __write_16(ptr, val, map->endianness);
Mario Sixa4fd59e2018-10-15 09:24:10 +0200588 break;
589 case REGMAP_SIZE_32:
Mario Sixe5a3d5b2018-10-15 09:24:14 +0200590 __write_32(ptr, val, map->endianness);
Mario Sixa4fd59e2018-10-15 09:24:10 +0200591 break;
Mario Sixe5a3d5b2018-10-15 09:24:14 +0200592#if defined(out_le64) && defined(out_be64) && defined(writeq)
Mario Sixa4fd59e2018-10-15 09:24:10 +0200593 case REGMAP_SIZE_64:
Mario Sixe5a3d5b2018-10-15 09:24:14 +0200594 __write_64(ptr, val, map->endianness);
Mario Sixa4fd59e2018-10-15 09:24:10 +0200595 break;
596#endif
597 default:
598 debug("%s: regmap size %zu unknown\n", __func__, val_len);
599 return -EINVAL;
600 }
Paul Burton39776032016-09-08 07:47:35 +0100601
602 return 0;
603}
Neil Armstrong5444ec62018-04-27 11:56:14 +0200604
Mario Six2f009972018-10-15 09:24:11 +0200605int regmap_raw_write(struct regmap *map, uint offset, const void *val,
606 size_t val_len)
607{
608 return regmap_raw_write_range(map, 0, offset, val, val_len);
609}
610
Mario Sixa4fd59e2018-10-15 09:24:10 +0200611int regmap_write(struct regmap *map, uint offset, uint val)
612{
Marek BehĂșn10448862021-05-20 13:23:50 +0200613 union {
614 u8 v8;
615 u16 v16;
616 u32 v32;
617 u64 v64;
618 } u;
619
620 switch (map->width) {
621 case REGMAP_SIZE_8:
622 u.v8 = val;
623 break;
624 case REGMAP_SIZE_16:
625 u.v16 = val;
626 break;
627 case REGMAP_SIZE_32:
628 u.v32 = val;
629 break;
630 case REGMAP_SIZE_64:
631 u.v64 = val;
632 break;
633 default:
634 debug("%s: regmap size %zu unknown\n", __func__,
635 (size_t)map->width);
636 return -EINVAL;
637 }
638
639 return regmap_raw_write(map, offset, &u, map->width);
Mario Sixa4fd59e2018-10-15 09:24:10 +0200640}
641
Neil Armstrong5444ec62018-04-27 11:56:14 +0200642int regmap_update_bits(struct regmap *map, uint offset, uint mask, uint val)
643{
644 uint reg;
645 int ret;
646
647 ret = regmap_read(map, offset, &reg);
648 if (ret)
649 return ret;
650
651 reg &= ~mask;
652
Simon Glass56bed2a2019-10-11 16:16:49 -0600653 return regmap_write(map, offset, reg | (val & mask));
Neil Armstrong5444ec62018-04-27 11:56:14 +0200654}
Jean-Jacques Hiblotb3bbbcf2020-09-24 10:04:16 +0530655
656int regmap_field_read(struct regmap_field *field, unsigned int *val)
657{
658 int ret;
659 unsigned int reg_val;
660
661 ret = regmap_read(field->regmap, field->reg, &reg_val);
662 if (ret != 0)
663 return ret;
664
665 reg_val &= field->mask;
666 reg_val >>= field->shift;
667 *val = reg_val;
668
669 return ret;
670}
671
672int regmap_field_write(struct regmap_field *field, unsigned int val)
673{
674 return regmap_update_bits(field->regmap, field->reg, field->mask,
675 val << field->shift);
676}
677
678static void regmap_field_init(struct regmap_field *rm_field,
679 struct regmap *regmap,
680 struct reg_field reg_field)
681{
682 rm_field->regmap = regmap;
683 rm_field->reg = reg_field.reg;
684 rm_field->shift = reg_field.lsb;
685 rm_field->mask = GENMASK(reg_field.msb, reg_field.lsb);
686}
687
688struct regmap_field *devm_regmap_field_alloc(struct udevice *dev,
689 struct regmap *regmap,
690 struct reg_field reg_field)
691{
692 struct regmap_field *rm_field = devm_kzalloc(dev, sizeof(*rm_field),
693 GFP_KERNEL);
694 if (!rm_field)
695 return ERR_PTR(-ENOMEM);
696
697 regmap_field_init(rm_field, regmap, reg_field);
698
699 return rm_field;
700}
701
702void devm_regmap_field_free(struct udevice *dev, struct regmap_field *field)
703{
704 devm_kfree(dev, field);
705}
706
707struct regmap_field *regmap_field_alloc(struct regmap *regmap,
708 struct reg_field reg_field)
709{
710 struct regmap_field *rm_field = kzalloc(sizeof(*rm_field), GFP_KERNEL);
711
712 if (!rm_field)
713 return ERR_PTR(-ENOMEM);
714
715 regmap_field_init(rm_field, regmap, reg_field);
716
717 return rm_field;
718}
719
720void regmap_field_free(struct regmap_field *field)
721{
722 kfree(field);
723}