Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Simon Glass | ad8f8ab | 2015-06-23 15:38:42 -0600 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (c) 2015 Google, Inc |
| 4 | * Written by Simon Glass <sjg@chromium.org> |
Simon Glass | ad8f8ab | 2015-06-23 15:38:42 -0600 | [diff] [blame] | 5 | */ |
| 6 | |
| 7 | #include <common.h> |
| 8 | #include <dm.h> |
| 9 | #include <errno.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 10 | #include <log.h> |
Masahiro Yamada | 75f82d0 | 2018-03-05 01:20:11 +0900 | [diff] [blame] | 11 | #include <linux/libfdt.h> |
Simon Glass | ad8f8ab | 2015-06-23 15:38:42 -0600 | [diff] [blame] | 12 | #include <malloc.h> |
| 13 | #include <mapmem.h> |
| 14 | #include <regmap.h> |
Paul Burton | 3977603 | 2016-09-08 07:47:35 +0100 | [diff] [blame] | 15 | #include <asm/io.h> |
Simon Glass | eeeb519 | 2017-05-18 20:09:10 -0600 | [diff] [blame] | 16 | #include <dm/of_addr.h> |
| 17 | #include <linux/ioport.h> |
Paul Burton | 3977603 | 2016-09-08 07:47:35 +0100 | [diff] [blame] | 18 | |
Simon Glass | ad8f8ab | 2015-06-23 15:38:42 -0600 | [diff] [blame] | 19 | DECLARE_GLOBAL_DATA_PTR; |
| 20 | |
Mario Six | 0aa5299 | 2018-10-04 09:00:42 +0200 | [diff] [blame] | 21 | /** |
| 22 | * regmap_alloc() - Allocate a regmap with a given number of ranges. |
| 23 | * |
| 24 | * @count: Number of ranges to be allocated for the regmap. |
| 25 | * Return: A pointer to the newly allocated regmap, or NULL on error. |
| 26 | */ |
Masahiro Yamada | 54c5ecb | 2018-04-19 12:14:01 +0900 | [diff] [blame] | 27 | static struct regmap *regmap_alloc(int count) |
Simon Glass | 30d73e8 | 2016-07-04 11:58:21 -0600 | [diff] [blame] | 28 | { |
| 29 | struct regmap *map; |
| 30 | |
Masahiro Yamada | 54c5ecb | 2018-04-19 12:14:01 +0900 | [diff] [blame] | 31 | map = malloc(sizeof(*map) + sizeof(map->ranges[0]) * count); |
Simon Glass | 30d73e8 | 2016-07-04 11:58:21 -0600 | [diff] [blame] | 32 | if (!map) |
| 33 | return NULL; |
Simon Glass | 30d73e8 | 2016-07-04 11:58:21 -0600 | [diff] [blame] | 34 | map->range_count = count; |
| 35 | |
| 36 | return map; |
| 37 | } |
| 38 | |
Simon Glass | b944345 | 2016-07-04 11:57:59 -0600 | [diff] [blame] | 39 | #if CONFIG_IS_ENABLED(OF_PLATDATA) |
Simon Glass | 1b1fe41 | 2017-08-29 14:15:50 -0600 | [diff] [blame] | 40 | int regmap_init_mem_platdata(struct udevice *dev, fdt_val_t *reg, int count, |
Simon Glass | b944345 | 2016-07-04 11:57:59 -0600 | [diff] [blame] | 41 | struct regmap **mapp) |
| 42 | { |
Simon Glass | b611433 | 2016-07-04 11:58:22 -0600 | [diff] [blame] | 43 | struct regmap_range *range; |
| 44 | struct regmap *map; |
| 45 | |
Masahiro Yamada | 54c5ecb | 2018-04-19 12:14:01 +0900 | [diff] [blame] | 46 | map = regmap_alloc(count); |
Simon Glass | b611433 | 2016-07-04 11:58:22 -0600 | [diff] [blame] | 47 | if (!map) |
| 48 | return -ENOMEM; |
| 49 | |
Masahiro Yamada | 54c5ecb | 2018-04-19 12:14:01 +0900 | [diff] [blame] | 50 | for (range = map->ranges; count > 0; reg += 2, range++, count--) { |
Simon Glass | b611433 | 2016-07-04 11:58:22 -0600 | [diff] [blame] | 51 | range->start = *reg; |
| 52 | range->size = reg[1]; |
| 53 | } |
| 54 | |
| 55 | *mapp = map; |
| 56 | |
Simon Glass | b944345 | 2016-07-04 11:57:59 -0600 | [diff] [blame] | 57 | return 0; |
| 58 | } |
| 59 | #else |
Mario Six | 5159c0c | 2018-10-15 09:24:07 +0200 | [diff] [blame] | 60 | /** |
| 61 | * init_range() - Initialize a single range of a regmap |
| 62 | * @node: Device node that will use the map in question |
| 63 | * @range: Pointer to a regmap_range structure that will be initialized |
| 64 | * @addr_len: The length of the addr parts of the reg property |
| 65 | * @size_len: The length of the size parts of the reg property |
| 66 | * @index: The index of the range to initialize |
| 67 | * |
| 68 | * This function will read the necessary 'reg' information from the device tree |
| 69 | * (the 'addr' part, and the 'length' part), and initialize the range in |
| 70 | * quesion. |
| 71 | * |
| 72 | * Return: 0 if OK, -ve on error |
| 73 | */ |
| 74 | static int init_range(ofnode node, struct regmap_range *range, int addr_len, |
| 75 | int size_len, int index) |
| 76 | { |
| 77 | fdt_size_t sz; |
| 78 | struct resource r; |
| 79 | |
| 80 | if (of_live_active()) { |
| 81 | int ret; |
| 82 | |
| 83 | ret = of_address_to_resource(ofnode_to_np(node), |
| 84 | index, &r); |
| 85 | if (ret) { |
| 86 | debug("%s: Could not read resource of range %d (ret = %d)\n", |
| 87 | ofnode_get_name(node), index, ret); |
| 88 | return ret; |
| 89 | } |
| 90 | |
| 91 | range->start = r.start; |
| 92 | range->size = r.end - r.start + 1; |
| 93 | } else { |
| 94 | int offset = ofnode_to_offset(node); |
| 95 | |
| 96 | range->start = fdtdec_get_addr_size_fixed(gd->fdt_blob, offset, |
| 97 | "reg", index, |
| 98 | addr_len, size_len, |
| 99 | &sz, true); |
| 100 | if (range->start == FDT_ADDR_T_NONE) { |
| 101 | debug("%s: Could not read start of range %d\n", |
| 102 | ofnode_get_name(node), index); |
| 103 | return -EINVAL; |
| 104 | } |
| 105 | |
| 106 | range->size = sz; |
| 107 | } |
| 108 | |
| 109 | return 0; |
| 110 | } |
| 111 | |
Faiz Abbas | 52742f4 | 2019-06-11 00:43:33 +0530 | [diff] [blame] | 112 | int regmap_init_mem_index(ofnode node, struct regmap **mapp, int index) |
| 113 | { |
| 114 | struct regmap *map; |
| 115 | int addr_len, size_len; |
| 116 | int ret; |
| 117 | |
| 118 | addr_len = ofnode_read_simple_addr_cells(ofnode_get_parent(node)); |
| 119 | if (addr_len < 0) { |
| 120 | debug("%s: Error while reading the addr length (ret = %d)\n", |
| 121 | ofnode_get_name(node), addr_len); |
| 122 | return addr_len; |
| 123 | } |
| 124 | |
| 125 | size_len = ofnode_read_simple_size_cells(ofnode_get_parent(node)); |
| 126 | if (size_len < 0) { |
| 127 | debug("%s: Error while reading the size length: (ret = %d)\n", |
| 128 | ofnode_get_name(node), size_len); |
| 129 | return size_len; |
| 130 | } |
| 131 | |
| 132 | map = regmap_alloc(1); |
| 133 | if (!map) |
| 134 | return -ENOMEM; |
| 135 | |
| 136 | ret = init_range(node, map->ranges, addr_len, size_len, index); |
| 137 | if (ret) |
Faiz Abbas | 4bba413 | 2019-11-11 15:29:05 +0530 | [diff] [blame] | 138 | goto err; |
Faiz Abbas | 52742f4 | 2019-06-11 00:43:33 +0530 | [diff] [blame] | 139 | |
| 140 | if (ofnode_read_bool(node, "little-endian")) |
| 141 | map->endianness = REGMAP_LITTLE_ENDIAN; |
| 142 | else if (ofnode_read_bool(node, "big-endian")) |
| 143 | map->endianness = REGMAP_BIG_ENDIAN; |
| 144 | else if (ofnode_read_bool(node, "native-endian")) |
| 145 | map->endianness = REGMAP_NATIVE_ENDIAN; |
| 146 | else /* Default: native endianness */ |
| 147 | map->endianness = REGMAP_NATIVE_ENDIAN; |
| 148 | |
| 149 | *mapp = map; |
| 150 | |
Faiz Abbas | 4bba413 | 2019-11-11 15:29:05 +0530 | [diff] [blame] | 151 | return 0; |
| 152 | err: |
| 153 | regmap_uninit(map); |
| 154 | |
Faiz Abbas | 52742f4 | 2019-06-11 00:43:33 +0530 | [diff] [blame] | 155 | return ret; |
| 156 | } |
| 157 | |
Masahiro Yamada | e4873e3 | 2018-04-19 12:14:03 +0900 | [diff] [blame] | 158 | int regmap_init_mem(ofnode node, struct regmap **mapp) |
Simon Glass | ad8f8ab | 2015-06-23 15:38:42 -0600 | [diff] [blame] | 159 | { |
Simon Glass | ad8f8ab | 2015-06-23 15:38:42 -0600 | [diff] [blame] | 160 | struct regmap_range *range; |
Simon Glass | ad8f8ab | 2015-06-23 15:38:42 -0600 | [diff] [blame] | 161 | struct regmap *map; |
| 162 | int count; |
| 163 | int addr_len, size_len, both_len; |
Simon Glass | ad8f8ab | 2015-06-23 15:38:42 -0600 | [diff] [blame] | 164 | int len; |
Jean-Jacques Hiblot | 024611b | 2017-02-13 16:17:48 +0100 | [diff] [blame] | 165 | int index; |
Faiz Abbas | 4bba413 | 2019-11-11 15:29:05 +0530 | [diff] [blame] | 166 | int ret; |
Simon Glass | ad8f8ab | 2015-06-23 15:38:42 -0600 | [diff] [blame] | 167 | |
Masahiro Yamada | e4873e3 | 2018-04-19 12:14:03 +0900 | [diff] [blame] | 168 | addr_len = ofnode_read_simple_addr_cells(ofnode_get_parent(node)); |
Mario Six | 1232116 | 2018-10-04 09:00:43 +0200 | [diff] [blame] | 169 | if (addr_len < 0) { |
| 170 | debug("%s: Error while reading the addr length (ret = %d)\n", |
| 171 | ofnode_get_name(node), addr_len); |
| 172 | return addr_len; |
| 173 | } |
| 174 | |
Masahiro Yamada | e4873e3 | 2018-04-19 12:14:03 +0900 | [diff] [blame] | 175 | size_len = ofnode_read_simple_size_cells(ofnode_get_parent(node)); |
Mario Six | 1232116 | 2018-10-04 09:00:43 +0200 | [diff] [blame] | 176 | if (size_len < 0) { |
| 177 | debug("%s: Error while reading the size length: (ret = %d)\n", |
| 178 | ofnode_get_name(node), size_len); |
| 179 | return size_len; |
| 180 | } |
| 181 | |
Simon Glass | ad8f8ab | 2015-06-23 15:38:42 -0600 | [diff] [blame] | 182 | both_len = addr_len + size_len; |
Mario Six | 1232116 | 2018-10-04 09:00:43 +0200 | [diff] [blame] | 183 | if (!both_len) { |
| 184 | debug("%s: Both addr and size length are zero\n", |
| 185 | ofnode_get_name(node)); |
| 186 | return -EINVAL; |
| 187 | } |
Simon Glass | ad8f8ab | 2015-06-23 15:38:42 -0600 | [diff] [blame] | 188 | |
Masahiro Yamada | e4873e3 | 2018-04-19 12:14:03 +0900 | [diff] [blame] | 189 | len = ofnode_read_size(node, "reg"); |
Mario Six | 6e96ba2 | 2018-10-15 09:24:08 +0200 | [diff] [blame] | 190 | if (len < 0) { |
| 191 | debug("%s: Error while reading reg size (ret = %d)\n", |
| 192 | ofnode_get_name(node), len); |
Simon Glass | eeeb519 | 2017-05-18 20:09:10 -0600 | [diff] [blame] | 193 | return len; |
Mario Six | 6e96ba2 | 2018-10-15 09:24:08 +0200 | [diff] [blame] | 194 | } |
Simon Glass | eeeb519 | 2017-05-18 20:09:10 -0600 | [diff] [blame] | 195 | len /= sizeof(fdt32_t); |
Simon Glass | ad8f8ab | 2015-06-23 15:38:42 -0600 | [diff] [blame] | 196 | count = len / both_len; |
Mario Six | 6e96ba2 | 2018-10-15 09:24:08 +0200 | [diff] [blame] | 197 | if (!count) { |
| 198 | debug("%s: Not enough data in reg property\n", |
| 199 | ofnode_get_name(node)); |
Simon Glass | ad8f8ab | 2015-06-23 15:38:42 -0600 | [diff] [blame] | 200 | return -EINVAL; |
Mario Six | 6e96ba2 | 2018-10-15 09:24:08 +0200 | [diff] [blame] | 201 | } |
Simon Glass | ad8f8ab | 2015-06-23 15:38:42 -0600 | [diff] [blame] | 202 | |
Masahiro Yamada | 54c5ecb | 2018-04-19 12:14:01 +0900 | [diff] [blame] | 203 | map = regmap_alloc(count); |
Simon Glass | ad8f8ab | 2015-06-23 15:38:42 -0600 | [diff] [blame] | 204 | if (!map) |
| 205 | return -ENOMEM; |
| 206 | |
Masahiro Yamada | 54c5ecb | 2018-04-19 12:14:01 +0900 | [diff] [blame] | 207 | for (range = map->ranges, index = 0; count > 0; |
Simon Glass | eeeb519 | 2017-05-18 20:09:10 -0600 | [diff] [blame] | 208 | count--, range++, index++) { |
Faiz Abbas | 4bba413 | 2019-11-11 15:29:05 +0530 | [diff] [blame] | 209 | ret = init_range(node, range, addr_len, size_len, index); |
Mario Six | 5159c0c | 2018-10-15 09:24:07 +0200 | [diff] [blame] | 210 | if (ret) |
Faiz Abbas | 4bba413 | 2019-11-11 15:29:05 +0530 | [diff] [blame] | 211 | goto err; |
Simon Glass | ad8f8ab | 2015-06-23 15:38:42 -0600 | [diff] [blame] | 212 | } |
| 213 | |
Mario Six | e5a3d5b | 2018-10-15 09:24:14 +0200 | [diff] [blame] | 214 | if (ofnode_read_bool(node, "little-endian")) |
| 215 | map->endianness = REGMAP_LITTLE_ENDIAN; |
| 216 | else if (ofnode_read_bool(node, "big-endian")) |
| 217 | map->endianness = REGMAP_BIG_ENDIAN; |
| 218 | else if (ofnode_read_bool(node, "native-endian")) |
| 219 | map->endianness = REGMAP_NATIVE_ENDIAN; |
| 220 | else /* Default: native endianness */ |
| 221 | map->endianness = REGMAP_NATIVE_ENDIAN; |
| 222 | |
Simon Glass | ad8f8ab | 2015-06-23 15:38:42 -0600 | [diff] [blame] | 223 | *mapp = map; |
| 224 | |
| 225 | return 0; |
Faiz Abbas | 4bba413 | 2019-11-11 15:29:05 +0530 | [diff] [blame] | 226 | err: |
| 227 | regmap_uninit(map); |
| 228 | |
| 229 | return ret; |
Simon Glass | ad8f8ab | 2015-06-23 15:38:42 -0600 | [diff] [blame] | 230 | } |
Simon Glass | b944345 | 2016-07-04 11:57:59 -0600 | [diff] [blame] | 231 | #endif |
Simon Glass | ad8f8ab | 2015-06-23 15:38:42 -0600 | [diff] [blame] | 232 | |
| 233 | void *regmap_get_range(struct regmap *map, unsigned int range_num) |
| 234 | { |
| 235 | struct regmap_range *range; |
| 236 | |
| 237 | if (range_num >= map->range_count) |
| 238 | return NULL; |
Masahiro Yamada | 54c5ecb | 2018-04-19 12:14:01 +0900 | [diff] [blame] | 239 | range = &map->ranges[range_num]; |
Simon Glass | ad8f8ab | 2015-06-23 15:38:42 -0600 | [diff] [blame] | 240 | |
| 241 | return map_sysmem(range->start, range->size); |
| 242 | } |
| 243 | |
| 244 | int regmap_uninit(struct regmap *map) |
| 245 | { |
Simon Glass | ad8f8ab | 2015-06-23 15:38:42 -0600 | [diff] [blame] | 246 | free(map); |
| 247 | |
| 248 | return 0; |
| 249 | } |
Paul Burton | 3977603 | 2016-09-08 07:47:35 +0100 | [diff] [blame] | 250 | |
Mario Six | e5a3d5b | 2018-10-15 09:24:14 +0200 | [diff] [blame] | 251 | static inline u8 __read_8(u8 *addr, enum regmap_endianness_t endianness) |
| 252 | { |
| 253 | return readb(addr); |
| 254 | } |
| 255 | |
| 256 | static inline u16 __read_16(u16 *addr, enum regmap_endianness_t endianness) |
| 257 | { |
| 258 | switch (endianness) { |
| 259 | case REGMAP_LITTLE_ENDIAN: |
| 260 | return in_le16(addr); |
| 261 | case REGMAP_BIG_ENDIAN: |
| 262 | return in_be16(addr); |
| 263 | case REGMAP_NATIVE_ENDIAN: |
| 264 | return readw(addr); |
| 265 | } |
| 266 | |
| 267 | return readw(addr); |
| 268 | } |
| 269 | |
| 270 | static inline u32 __read_32(u32 *addr, enum regmap_endianness_t endianness) |
| 271 | { |
| 272 | switch (endianness) { |
| 273 | case REGMAP_LITTLE_ENDIAN: |
| 274 | return in_le32(addr); |
| 275 | case REGMAP_BIG_ENDIAN: |
| 276 | return in_be32(addr); |
| 277 | case REGMAP_NATIVE_ENDIAN: |
| 278 | return readl(addr); |
| 279 | } |
| 280 | |
| 281 | return readl(addr); |
| 282 | } |
| 283 | |
| 284 | #if defined(in_le64) && defined(in_be64) && defined(readq) |
| 285 | static inline u64 __read_64(u64 *addr, enum regmap_endianness_t endianness) |
| 286 | { |
| 287 | switch (endianness) { |
| 288 | case REGMAP_LITTLE_ENDIAN: |
| 289 | return in_le64(addr); |
| 290 | case REGMAP_BIG_ENDIAN: |
| 291 | return in_be64(addr); |
| 292 | case REGMAP_NATIVE_ENDIAN: |
| 293 | return readq(addr); |
| 294 | } |
| 295 | |
| 296 | return readq(addr); |
| 297 | } |
| 298 | #endif |
| 299 | |
Mario Six | 2f00997 | 2018-10-15 09:24:11 +0200 | [diff] [blame] | 300 | int regmap_raw_read_range(struct regmap *map, uint range_num, uint offset, |
| 301 | void *valp, size_t val_len) |
Paul Burton | 3977603 | 2016-09-08 07:47:35 +0100 | [diff] [blame] | 302 | { |
Mario Six | 2f00997 | 2018-10-15 09:24:11 +0200 | [diff] [blame] | 303 | struct regmap_range *range; |
Mario Six | a4fd59e | 2018-10-15 09:24:10 +0200 | [diff] [blame] | 304 | void *ptr; |
Paul Burton | 3977603 | 2016-09-08 07:47:35 +0100 | [diff] [blame] | 305 | |
Mario Six | 2f00997 | 2018-10-15 09:24:11 +0200 | [diff] [blame] | 306 | if (range_num >= map->range_count) { |
| 307 | debug("%s: range index %d larger than range count\n", |
| 308 | __func__, range_num); |
| 309 | return -ERANGE; |
| 310 | } |
| 311 | range = &map->ranges[range_num]; |
| 312 | |
Mario Six | 2f00997 | 2018-10-15 09:24:11 +0200 | [diff] [blame] | 313 | if (offset + val_len > range->size) { |
| 314 | debug("%s: offset/size combination invalid\n", __func__); |
| 315 | return -ERANGE; |
| 316 | } |
Paul Burton | 3977603 | 2016-09-08 07:47:35 +0100 | [diff] [blame] | 317 | |
Pratyush Yadav | 5c42d5d | 2020-05-26 17:35:57 +0530 | [diff] [blame] | 318 | ptr = map_physmem(range->start + offset, val_len, MAP_NOCACHE); |
| 319 | |
Mario Six | a4fd59e | 2018-10-15 09:24:10 +0200 | [diff] [blame] | 320 | switch (val_len) { |
| 321 | case REGMAP_SIZE_8: |
Mario Six | e5a3d5b | 2018-10-15 09:24:14 +0200 | [diff] [blame] | 322 | *((u8 *)valp) = __read_8(ptr, map->endianness); |
Mario Six | a4fd59e | 2018-10-15 09:24:10 +0200 | [diff] [blame] | 323 | break; |
| 324 | case REGMAP_SIZE_16: |
Mario Six | e5a3d5b | 2018-10-15 09:24:14 +0200 | [diff] [blame] | 325 | *((u16 *)valp) = __read_16(ptr, map->endianness); |
Mario Six | a4fd59e | 2018-10-15 09:24:10 +0200 | [diff] [blame] | 326 | break; |
| 327 | case REGMAP_SIZE_32: |
Mario Six | e5a3d5b | 2018-10-15 09:24:14 +0200 | [diff] [blame] | 328 | *((u32 *)valp) = __read_32(ptr, map->endianness); |
Mario Six | a4fd59e | 2018-10-15 09:24:10 +0200 | [diff] [blame] | 329 | break; |
Mario Six | e5a3d5b | 2018-10-15 09:24:14 +0200 | [diff] [blame] | 330 | #if defined(in_le64) && defined(in_be64) && defined(readq) |
Mario Six | a4fd59e | 2018-10-15 09:24:10 +0200 | [diff] [blame] | 331 | case REGMAP_SIZE_64: |
Mario Six | e5a3d5b | 2018-10-15 09:24:14 +0200 | [diff] [blame] | 332 | *((u64 *)valp) = __read_64(ptr, map->endianness); |
Mario Six | a4fd59e | 2018-10-15 09:24:10 +0200 | [diff] [blame] | 333 | break; |
| 334 | #endif |
| 335 | default: |
| 336 | debug("%s: regmap size %zu unknown\n", __func__, val_len); |
| 337 | return -EINVAL; |
| 338 | } |
Mario Six | 2f00997 | 2018-10-15 09:24:11 +0200 | [diff] [blame] | 339 | |
Paul Burton | 3977603 | 2016-09-08 07:47:35 +0100 | [diff] [blame] | 340 | return 0; |
| 341 | } |
| 342 | |
Mario Six | 2f00997 | 2018-10-15 09:24:11 +0200 | [diff] [blame] | 343 | int regmap_raw_read(struct regmap *map, uint offset, void *valp, size_t val_len) |
| 344 | { |
| 345 | return regmap_raw_read_range(map, 0, offset, valp, val_len); |
| 346 | } |
| 347 | |
Mario Six | a4fd59e | 2018-10-15 09:24:10 +0200 | [diff] [blame] | 348 | int regmap_read(struct regmap *map, uint offset, uint *valp) |
Paul Burton | 3977603 | 2016-09-08 07:47:35 +0100 | [diff] [blame] | 349 | { |
Mario Six | a4fd59e | 2018-10-15 09:24:10 +0200 | [diff] [blame] | 350 | return regmap_raw_read(map, offset, valp, REGMAP_SIZE_32); |
| 351 | } |
| 352 | |
Mario Six | e5a3d5b | 2018-10-15 09:24:14 +0200 | [diff] [blame] | 353 | static inline void __write_8(u8 *addr, const u8 *val, |
| 354 | enum regmap_endianness_t endianness) |
| 355 | { |
| 356 | writeb(*val, addr); |
| 357 | } |
| 358 | |
| 359 | static inline void __write_16(u16 *addr, const u16 *val, |
| 360 | enum regmap_endianness_t endianness) |
| 361 | { |
| 362 | switch (endianness) { |
| 363 | case REGMAP_NATIVE_ENDIAN: |
| 364 | writew(*val, addr); |
| 365 | break; |
| 366 | case REGMAP_LITTLE_ENDIAN: |
| 367 | out_le16(addr, *val); |
| 368 | break; |
| 369 | case REGMAP_BIG_ENDIAN: |
| 370 | out_be16(addr, *val); |
| 371 | break; |
| 372 | } |
| 373 | } |
| 374 | |
| 375 | static inline void __write_32(u32 *addr, const u32 *val, |
| 376 | enum regmap_endianness_t endianness) |
| 377 | { |
| 378 | switch (endianness) { |
| 379 | case REGMAP_NATIVE_ENDIAN: |
| 380 | writel(*val, addr); |
| 381 | break; |
| 382 | case REGMAP_LITTLE_ENDIAN: |
| 383 | out_le32(addr, *val); |
| 384 | break; |
| 385 | case REGMAP_BIG_ENDIAN: |
| 386 | out_be32(addr, *val); |
| 387 | break; |
| 388 | } |
| 389 | } |
| 390 | |
| 391 | #if defined(out_le64) && defined(out_be64) && defined(writeq) |
| 392 | static inline void __write_64(u64 *addr, const u64 *val, |
| 393 | enum regmap_endianness_t endianness) |
| 394 | { |
| 395 | switch (endianness) { |
| 396 | case REGMAP_NATIVE_ENDIAN: |
| 397 | writeq(*val, addr); |
| 398 | break; |
| 399 | case REGMAP_LITTLE_ENDIAN: |
| 400 | out_le64(addr, *val); |
| 401 | break; |
| 402 | case REGMAP_BIG_ENDIAN: |
| 403 | out_be64(addr, *val); |
| 404 | break; |
| 405 | } |
| 406 | } |
| 407 | #endif |
| 408 | |
Mario Six | 2f00997 | 2018-10-15 09:24:11 +0200 | [diff] [blame] | 409 | int regmap_raw_write_range(struct regmap *map, uint range_num, uint offset, |
| 410 | const void *val, size_t val_len) |
Mario Six | a4fd59e | 2018-10-15 09:24:10 +0200 | [diff] [blame] | 411 | { |
Mario Six | 2f00997 | 2018-10-15 09:24:11 +0200 | [diff] [blame] | 412 | struct regmap_range *range; |
Mario Six | a4fd59e | 2018-10-15 09:24:10 +0200 | [diff] [blame] | 413 | void *ptr; |
Paul Burton | 3977603 | 2016-09-08 07:47:35 +0100 | [diff] [blame] | 414 | |
Mario Six | 2f00997 | 2018-10-15 09:24:11 +0200 | [diff] [blame] | 415 | if (range_num >= map->range_count) { |
| 416 | debug("%s: range index %d larger than range count\n", |
| 417 | __func__, range_num); |
| 418 | return -ERANGE; |
| 419 | } |
| 420 | range = &map->ranges[range_num]; |
| 421 | |
Mario Six | 2f00997 | 2018-10-15 09:24:11 +0200 | [diff] [blame] | 422 | if (offset + val_len > range->size) { |
| 423 | debug("%s: offset/size combination invalid\n", __func__); |
| 424 | return -ERANGE; |
| 425 | } |
Mario Six | a4fd59e | 2018-10-15 09:24:10 +0200 | [diff] [blame] | 426 | |
Pratyush Yadav | 5c42d5d | 2020-05-26 17:35:57 +0530 | [diff] [blame] | 427 | ptr = map_physmem(range->start + offset, val_len, MAP_NOCACHE); |
| 428 | |
Mario Six | a4fd59e | 2018-10-15 09:24:10 +0200 | [diff] [blame] | 429 | switch (val_len) { |
| 430 | case REGMAP_SIZE_8: |
Mario Six | e5a3d5b | 2018-10-15 09:24:14 +0200 | [diff] [blame] | 431 | __write_8(ptr, val, map->endianness); |
Mario Six | a4fd59e | 2018-10-15 09:24:10 +0200 | [diff] [blame] | 432 | break; |
| 433 | case REGMAP_SIZE_16: |
Mario Six | e5a3d5b | 2018-10-15 09:24:14 +0200 | [diff] [blame] | 434 | __write_16(ptr, val, map->endianness); |
Mario Six | a4fd59e | 2018-10-15 09:24:10 +0200 | [diff] [blame] | 435 | break; |
| 436 | case REGMAP_SIZE_32: |
Mario Six | e5a3d5b | 2018-10-15 09:24:14 +0200 | [diff] [blame] | 437 | __write_32(ptr, val, map->endianness); |
Mario Six | a4fd59e | 2018-10-15 09:24:10 +0200 | [diff] [blame] | 438 | break; |
Mario Six | e5a3d5b | 2018-10-15 09:24:14 +0200 | [diff] [blame] | 439 | #if defined(out_le64) && defined(out_be64) && defined(writeq) |
Mario Six | a4fd59e | 2018-10-15 09:24:10 +0200 | [diff] [blame] | 440 | case REGMAP_SIZE_64: |
Mario Six | e5a3d5b | 2018-10-15 09:24:14 +0200 | [diff] [blame] | 441 | __write_64(ptr, val, map->endianness); |
Mario Six | a4fd59e | 2018-10-15 09:24:10 +0200 | [diff] [blame] | 442 | break; |
| 443 | #endif |
| 444 | default: |
| 445 | debug("%s: regmap size %zu unknown\n", __func__, val_len); |
| 446 | return -EINVAL; |
| 447 | } |
Paul Burton | 3977603 | 2016-09-08 07:47:35 +0100 | [diff] [blame] | 448 | |
| 449 | return 0; |
| 450 | } |
Neil Armstrong | 5444ec6 | 2018-04-27 11:56:14 +0200 | [diff] [blame] | 451 | |
Mario Six | 2f00997 | 2018-10-15 09:24:11 +0200 | [diff] [blame] | 452 | int regmap_raw_write(struct regmap *map, uint offset, const void *val, |
| 453 | size_t val_len) |
| 454 | { |
| 455 | return regmap_raw_write_range(map, 0, offset, val, val_len); |
| 456 | } |
| 457 | |
Mario Six | a4fd59e | 2018-10-15 09:24:10 +0200 | [diff] [blame] | 458 | int regmap_write(struct regmap *map, uint offset, uint val) |
| 459 | { |
| 460 | return regmap_raw_write(map, offset, &val, REGMAP_SIZE_32); |
| 461 | } |
| 462 | |
Neil Armstrong | 5444ec6 | 2018-04-27 11:56:14 +0200 | [diff] [blame] | 463 | int regmap_update_bits(struct regmap *map, uint offset, uint mask, uint val) |
| 464 | { |
| 465 | uint reg; |
| 466 | int ret; |
| 467 | |
| 468 | ret = regmap_read(map, offset, ®); |
| 469 | if (ret) |
| 470 | return ret; |
| 471 | |
| 472 | reg &= ~mask; |
| 473 | |
Simon Glass | 56bed2a | 2019-10-11 16:16:49 -0600 | [diff] [blame] | 474 | return regmap_write(map, offset, reg | (val & mask)); |
Neil Armstrong | 5444ec6 | 2018-04-27 11:56:14 +0200 | [diff] [blame] | 475 | } |