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