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; |
Pratyush Yadav | 3b94e5d | 2020-09-24 10:04:13 +0530 | [diff] [blame^] | 264 | if (config) { |
Pratyush Yadav | 1c9867c | 2020-09-24 10:04:12 +0530 | [diff] [blame] | 265 | map->width = config->width; |
Pratyush Yadav | 3b94e5d | 2020-09-24 10:04:13 +0530 | [diff] [blame^] | 266 | map->reg_offset_shift = config->reg_offset_shift; |
| 267 | } |
Pratyush Yadav | 1c9867c | 2020-09-24 10:04:12 +0530 | [diff] [blame] | 268 | |
Jean-Jacques Hiblot | de03d12 | 2020-09-24 10:04:10 +0530 | [diff] [blame] | 269 | devres_add(dev, mapp); |
| 270 | return *mapp; |
| 271 | } |
Simon Glass | b944345 | 2016-07-04 11:57:59 -0600 | [diff] [blame] | 272 | #endif |
Simon Glass | ad8f8ab | 2015-06-23 15:38:42 -0600 | [diff] [blame] | 273 | |
| 274 | void *regmap_get_range(struct regmap *map, unsigned int range_num) |
| 275 | { |
| 276 | struct regmap_range *range; |
| 277 | |
| 278 | if (range_num >= map->range_count) |
| 279 | return NULL; |
Masahiro Yamada | 54c5ecb | 2018-04-19 12:14:01 +0900 | [diff] [blame] | 280 | range = &map->ranges[range_num]; |
Simon Glass | ad8f8ab | 2015-06-23 15:38:42 -0600 | [diff] [blame] | 281 | |
| 282 | return map_sysmem(range->start, range->size); |
| 283 | } |
| 284 | |
| 285 | int regmap_uninit(struct regmap *map) |
| 286 | { |
Simon Glass | ad8f8ab | 2015-06-23 15:38:42 -0600 | [diff] [blame] | 287 | free(map); |
| 288 | |
| 289 | return 0; |
| 290 | } |
Paul Burton | 3977603 | 2016-09-08 07:47:35 +0100 | [diff] [blame] | 291 | |
Mario Six | e5a3d5b | 2018-10-15 09:24:14 +0200 | [diff] [blame] | 292 | static inline u8 __read_8(u8 *addr, enum regmap_endianness_t endianness) |
| 293 | { |
| 294 | return readb(addr); |
| 295 | } |
| 296 | |
| 297 | static inline u16 __read_16(u16 *addr, enum regmap_endianness_t endianness) |
| 298 | { |
| 299 | switch (endianness) { |
| 300 | case REGMAP_LITTLE_ENDIAN: |
| 301 | return in_le16(addr); |
| 302 | case REGMAP_BIG_ENDIAN: |
| 303 | return in_be16(addr); |
| 304 | case REGMAP_NATIVE_ENDIAN: |
| 305 | return readw(addr); |
| 306 | } |
| 307 | |
| 308 | return readw(addr); |
| 309 | } |
| 310 | |
| 311 | static inline u32 __read_32(u32 *addr, enum regmap_endianness_t endianness) |
| 312 | { |
| 313 | switch (endianness) { |
| 314 | case REGMAP_LITTLE_ENDIAN: |
| 315 | return in_le32(addr); |
| 316 | case REGMAP_BIG_ENDIAN: |
| 317 | return in_be32(addr); |
| 318 | case REGMAP_NATIVE_ENDIAN: |
| 319 | return readl(addr); |
| 320 | } |
| 321 | |
| 322 | return readl(addr); |
| 323 | } |
| 324 | |
| 325 | #if defined(in_le64) && defined(in_be64) && defined(readq) |
| 326 | static inline u64 __read_64(u64 *addr, enum regmap_endianness_t endianness) |
| 327 | { |
| 328 | switch (endianness) { |
| 329 | case REGMAP_LITTLE_ENDIAN: |
| 330 | return in_le64(addr); |
| 331 | case REGMAP_BIG_ENDIAN: |
| 332 | return in_be64(addr); |
| 333 | case REGMAP_NATIVE_ENDIAN: |
| 334 | return readq(addr); |
| 335 | } |
| 336 | |
| 337 | return readq(addr); |
| 338 | } |
| 339 | #endif |
| 340 | |
Mario Six | 2f00997 | 2018-10-15 09:24:11 +0200 | [diff] [blame] | 341 | int regmap_raw_read_range(struct regmap *map, uint range_num, uint offset, |
| 342 | void *valp, size_t val_len) |
Paul Burton | 3977603 | 2016-09-08 07:47:35 +0100 | [diff] [blame] | 343 | { |
Mario Six | 2f00997 | 2018-10-15 09:24:11 +0200 | [diff] [blame] | 344 | struct regmap_range *range; |
Mario Six | a4fd59e | 2018-10-15 09:24:10 +0200 | [diff] [blame] | 345 | void *ptr; |
Paul Burton | 3977603 | 2016-09-08 07:47:35 +0100 | [diff] [blame] | 346 | |
Mario Six | 2f00997 | 2018-10-15 09:24:11 +0200 | [diff] [blame] | 347 | if (range_num >= map->range_count) { |
| 348 | debug("%s: range index %d larger than range count\n", |
| 349 | __func__, range_num); |
| 350 | return -ERANGE; |
| 351 | } |
| 352 | range = &map->ranges[range_num]; |
| 353 | |
Pratyush Yadav | 3b94e5d | 2020-09-24 10:04:13 +0530 | [diff] [blame^] | 354 | offset <<= map->reg_offset_shift; |
Mario Six | 2f00997 | 2018-10-15 09:24:11 +0200 | [diff] [blame] | 355 | if (offset + val_len > range->size) { |
| 356 | debug("%s: offset/size combination invalid\n", __func__); |
| 357 | return -ERANGE; |
| 358 | } |
Paul Burton | 3977603 | 2016-09-08 07:47:35 +0100 | [diff] [blame] | 359 | |
Pratyush Yadav | 5c42d5d | 2020-05-26 17:35:57 +0530 | [diff] [blame] | 360 | ptr = map_physmem(range->start + offset, val_len, MAP_NOCACHE); |
| 361 | |
Mario Six | a4fd59e | 2018-10-15 09:24:10 +0200 | [diff] [blame] | 362 | switch (val_len) { |
| 363 | case REGMAP_SIZE_8: |
Mario Six | e5a3d5b | 2018-10-15 09:24:14 +0200 | [diff] [blame] | 364 | *((u8 *)valp) = __read_8(ptr, map->endianness); |
Mario Six | a4fd59e | 2018-10-15 09:24:10 +0200 | [diff] [blame] | 365 | break; |
| 366 | case REGMAP_SIZE_16: |
Mario Six | e5a3d5b | 2018-10-15 09:24:14 +0200 | [diff] [blame] | 367 | *((u16 *)valp) = __read_16(ptr, map->endianness); |
Mario Six | a4fd59e | 2018-10-15 09:24:10 +0200 | [diff] [blame] | 368 | break; |
| 369 | case REGMAP_SIZE_32: |
Mario Six | e5a3d5b | 2018-10-15 09:24:14 +0200 | [diff] [blame] | 370 | *((u32 *)valp) = __read_32(ptr, map->endianness); |
Mario Six | a4fd59e | 2018-10-15 09:24:10 +0200 | [diff] [blame] | 371 | break; |
Mario Six | e5a3d5b | 2018-10-15 09:24:14 +0200 | [diff] [blame] | 372 | #if defined(in_le64) && defined(in_be64) && defined(readq) |
Mario Six | a4fd59e | 2018-10-15 09:24:10 +0200 | [diff] [blame] | 373 | case REGMAP_SIZE_64: |
Mario Six | e5a3d5b | 2018-10-15 09:24:14 +0200 | [diff] [blame] | 374 | *((u64 *)valp) = __read_64(ptr, map->endianness); |
Mario Six | a4fd59e | 2018-10-15 09:24:10 +0200 | [diff] [blame] | 375 | break; |
| 376 | #endif |
| 377 | default: |
| 378 | debug("%s: regmap size %zu unknown\n", __func__, val_len); |
| 379 | return -EINVAL; |
| 380 | } |
Mario Six | 2f00997 | 2018-10-15 09:24:11 +0200 | [diff] [blame] | 381 | |
Paul Burton | 3977603 | 2016-09-08 07:47:35 +0100 | [diff] [blame] | 382 | return 0; |
| 383 | } |
| 384 | |
Mario Six | 2f00997 | 2018-10-15 09:24:11 +0200 | [diff] [blame] | 385 | int regmap_raw_read(struct regmap *map, uint offset, void *valp, size_t val_len) |
| 386 | { |
| 387 | return regmap_raw_read_range(map, 0, offset, valp, val_len); |
| 388 | } |
| 389 | |
Mario Six | a4fd59e | 2018-10-15 09:24:10 +0200 | [diff] [blame] | 390 | int regmap_read(struct regmap *map, uint offset, uint *valp) |
Paul Burton | 3977603 | 2016-09-08 07:47:35 +0100 | [diff] [blame] | 391 | { |
Pratyush Yadav | 1c9867c | 2020-09-24 10:04:12 +0530 | [diff] [blame] | 392 | return regmap_raw_read(map, offset, valp, map->width); |
Mario Six | a4fd59e | 2018-10-15 09:24:10 +0200 | [diff] [blame] | 393 | } |
| 394 | |
Mario Six | e5a3d5b | 2018-10-15 09:24:14 +0200 | [diff] [blame] | 395 | static inline void __write_8(u8 *addr, const u8 *val, |
| 396 | enum regmap_endianness_t endianness) |
| 397 | { |
| 398 | writeb(*val, addr); |
| 399 | } |
| 400 | |
| 401 | static inline void __write_16(u16 *addr, const u16 *val, |
| 402 | enum regmap_endianness_t endianness) |
| 403 | { |
| 404 | switch (endianness) { |
| 405 | case REGMAP_NATIVE_ENDIAN: |
| 406 | writew(*val, addr); |
| 407 | break; |
| 408 | case REGMAP_LITTLE_ENDIAN: |
| 409 | out_le16(addr, *val); |
| 410 | break; |
| 411 | case REGMAP_BIG_ENDIAN: |
| 412 | out_be16(addr, *val); |
| 413 | break; |
| 414 | } |
| 415 | } |
| 416 | |
| 417 | static inline void __write_32(u32 *addr, const u32 *val, |
| 418 | enum regmap_endianness_t endianness) |
| 419 | { |
| 420 | switch (endianness) { |
| 421 | case REGMAP_NATIVE_ENDIAN: |
| 422 | writel(*val, addr); |
| 423 | break; |
| 424 | case REGMAP_LITTLE_ENDIAN: |
| 425 | out_le32(addr, *val); |
| 426 | break; |
| 427 | case REGMAP_BIG_ENDIAN: |
| 428 | out_be32(addr, *val); |
| 429 | break; |
| 430 | } |
| 431 | } |
| 432 | |
| 433 | #if defined(out_le64) && defined(out_be64) && defined(writeq) |
| 434 | static inline void __write_64(u64 *addr, const u64 *val, |
| 435 | enum regmap_endianness_t endianness) |
| 436 | { |
| 437 | switch (endianness) { |
| 438 | case REGMAP_NATIVE_ENDIAN: |
| 439 | writeq(*val, addr); |
| 440 | break; |
| 441 | case REGMAP_LITTLE_ENDIAN: |
| 442 | out_le64(addr, *val); |
| 443 | break; |
| 444 | case REGMAP_BIG_ENDIAN: |
| 445 | out_be64(addr, *val); |
| 446 | break; |
| 447 | } |
| 448 | } |
| 449 | #endif |
| 450 | |
Mario Six | 2f00997 | 2018-10-15 09:24:11 +0200 | [diff] [blame] | 451 | int regmap_raw_write_range(struct regmap *map, uint range_num, uint offset, |
| 452 | const void *val, size_t val_len) |
Mario Six | a4fd59e | 2018-10-15 09:24:10 +0200 | [diff] [blame] | 453 | { |
Mario Six | 2f00997 | 2018-10-15 09:24:11 +0200 | [diff] [blame] | 454 | struct regmap_range *range; |
Mario Six | a4fd59e | 2018-10-15 09:24:10 +0200 | [diff] [blame] | 455 | void *ptr; |
Paul Burton | 3977603 | 2016-09-08 07:47:35 +0100 | [diff] [blame] | 456 | |
Mario Six | 2f00997 | 2018-10-15 09:24:11 +0200 | [diff] [blame] | 457 | if (range_num >= map->range_count) { |
| 458 | debug("%s: range index %d larger than range count\n", |
| 459 | __func__, range_num); |
| 460 | return -ERANGE; |
| 461 | } |
| 462 | range = &map->ranges[range_num]; |
| 463 | |
Pratyush Yadav | 3b94e5d | 2020-09-24 10:04:13 +0530 | [diff] [blame^] | 464 | offset <<= map->reg_offset_shift; |
Mario Six | 2f00997 | 2018-10-15 09:24:11 +0200 | [diff] [blame] | 465 | if (offset + val_len > range->size) { |
| 466 | debug("%s: offset/size combination invalid\n", __func__); |
| 467 | return -ERANGE; |
| 468 | } |
Mario Six | a4fd59e | 2018-10-15 09:24:10 +0200 | [diff] [blame] | 469 | |
Pratyush Yadav | 5c42d5d | 2020-05-26 17:35:57 +0530 | [diff] [blame] | 470 | ptr = map_physmem(range->start + offset, val_len, MAP_NOCACHE); |
| 471 | |
Mario Six | a4fd59e | 2018-10-15 09:24:10 +0200 | [diff] [blame] | 472 | switch (val_len) { |
| 473 | case REGMAP_SIZE_8: |
Mario Six | e5a3d5b | 2018-10-15 09:24:14 +0200 | [diff] [blame] | 474 | __write_8(ptr, val, map->endianness); |
Mario Six | a4fd59e | 2018-10-15 09:24:10 +0200 | [diff] [blame] | 475 | break; |
| 476 | case REGMAP_SIZE_16: |
Mario Six | e5a3d5b | 2018-10-15 09:24:14 +0200 | [diff] [blame] | 477 | __write_16(ptr, val, map->endianness); |
Mario Six | a4fd59e | 2018-10-15 09:24:10 +0200 | [diff] [blame] | 478 | break; |
| 479 | case REGMAP_SIZE_32: |
Mario Six | e5a3d5b | 2018-10-15 09:24:14 +0200 | [diff] [blame] | 480 | __write_32(ptr, val, map->endianness); |
Mario Six | a4fd59e | 2018-10-15 09:24:10 +0200 | [diff] [blame] | 481 | break; |
Mario Six | e5a3d5b | 2018-10-15 09:24:14 +0200 | [diff] [blame] | 482 | #if defined(out_le64) && defined(out_be64) && defined(writeq) |
Mario Six | a4fd59e | 2018-10-15 09:24:10 +0200 | [diff] [blame] | 483 | case REGMAP_SIZE_64: |
Mario Six | e5a3d5b | 2018-10-15 09:24:14 +0200 | [diff] [blame] | 484 | __write_64(ptr, val, map->endianness); |
Mario Six | a4fd59e | 2018-10-15 09:24:10 +0200 | [diff] [blame] | 485 | break; |
| 486 | #endif |
| 487 | default: |
| 488 | debug("%s: regmap size %zu unknown\n", __func__, val_len); |
| 489 | return -EINVAL; |
| 490 | } |
Paul Burton | 3977603 | 2016-09-08 07:47:35 +0100 | [diff] [blame] | 491 | |
| 492 | return 0; |
| 493 | } |
Neil Armstrong | 5444ec6 | 2018-04-27 11:56:14 +0200 | [diff] [blame] | 494 | |
Mario Six | 2f00997 | 2018-10-15 09:24:11 +0200 | [diff] [blame] | 495 | int regmap_raw_write(struct regmap *map, uint offset, const void *val, |
| 496 | size_t val_len) |
| 497 | { |
| 498 | return regmap_raw_write_range(map, 0, offset, val, val_len); |
| 499 | } |
| 500 | |
Mario Six | a4fd59e | 2018-10-15 09:24:10 +0200 | [diff] [blame] | 501 | int regmap_write(struct regmap *map, uint offset, uint val) |
| 502 | { |
Pratyush Yadav | 1c9867c | 2020-09-24 10:04:12 +0530 | [diff] [blame] | 503 | return regmap_raw_write(map, offset, &val, map->width); |
Mario Six | a4fd59e | 2018-10-15 09:24:10 +0200 | [diff] [blame] | 504 | } |
| 505 | |
Neil Armstrong | 5444ec6 | 2018-04-27 11:56:14 +0200 | [diff] [blame] | 506 | int regmap_update_bits(struct regmap *map, uint offset, uint mask, uint val) |
| 507 | { |
| 508 | uint reg; |
| 509 | int ret; |
| 510 | |
| 511 | ret = regmap_read(map, offset, ®); |
| 512 | if (ret) |
| 513 | return ret; |
| 514 | |
| 515 | reg &= ~mask; |
| 516 | |
Simon Glass | 56bed2a | 2019-10-11 16:16:49 -0600 | [diff] [blame] | 517 | return regmap_write(map, offset, reg | (val & mask)); |
Neil Armstrong | 5444ec6 | 2018-04-27 11:56:14 +0200 | [diff] [blame] | 518 | } |