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> |
Simon Glass | 3ba929a | 2020-10-30 21:38:53 -0600 | [diff] [blame] | 11 | #include <asm/global_data.h> |
Masahiro Yamada | 75f82d0 | 2018-03-05 01:20:11 +0900 | [diff] [blame] | 12 | #include <linux/libfdt.h> |
Simon Glass | ad8f8ab | 2015-06-23 15:38:42 -0600 | [diff] [blame] | 13 | #include <malloc.h> |
| 14 | #include <mapmem.h> |
| 15 | #include <regmap.h> |
Paul Burton | 3977603 | 2016-09-08 07:47:35 +0100 | [diff] [blame] | 16 | #include <asm/io.h> |
Simon Glass | eeeb519 | 2017-05-18 20:09:10 -0600 | [diff] [blame] | 17 | #include <dm/of_addr.h> |
Jean-Jacques Hiblot | de03d12 | 2020-09-24 10:04:10 +0530 | [diff] [blame] | 18 | #include <dm/devres.h> |
Simon Glass | eeeb519 | 2017-05-18 20:09:10 -0600 | [diff] [blame] | 19 | #include <linux/ioport.h> |
Jean-Jacques Hiblot | de03d12 | 2020-09-24 10:04:10 +0530 | [diff] [blame] | 20 | #include <linux/compat.h> |
| 21 | #include <linux/err.h> |
Jean-Jacques Hiblot | b3bbbcf | 2020-09-24 10:04:16 +0530 | [diff] [blame] | 22 | #include <linux/bitops.h> |
| 23 | |
| 24 | /* |
| 25 | * Internal representation of a regmap field. Instead of storing the MSB and |
| 26 | * LSB, store the shift and mask. This makes the code a bit cleaner and faster |
| 27 | * because the shift and mask don't have to be calculated every time. |
| 28 | */ |
| 29 | struct regmap_field { |
| 30 | struct regmap *regmap; |
| 31 | unsigned int mask; |
| 32 | /* lsb */ |
| 33 | unsigned int shift; |
| 34 | unsigned int reg; |
| 35 | }; |
Paul Burton | 3977603 | 2016-09-08 07:47:35 +0100 | [diff] [blame] | 36 | |
Simon Glass | ad8f8ab | 2015-06-23 15:38:42 -0600 | [diff] [blame] | 37 | DECLARE_GLOBAL_DATA_PTR; |
| 38 | |
Mario Six | 0aa5299 | 2018-10-04 09:00:42 +0200 | [diff] [blame] | 39 | /** |
| 40 | * regmap_alloc() - Allocate a regmap with a given number of ranges. |
| 41 | * |
| 42 | * @count: Number of ranges to be allocated for the regmap. |
Pratyush Yadav | 1c9867c | 2020-09-24 10:04:12 +0530 | [diff] [blame] | 43 | * |
| 44 | * The default regmap width is set to REGMAP_SIZE_32. Callers can override it |
| 45 | * if they need. |
| 46 | * |
Mario Six | 0aa5299 | 2018-10-04 09:00:42 +0200 | [diff] [blame] | 47 | * Return: A pointer to the newly allocated regmap, or NULL on error. |
| 48 | */ |
Masahiro Yamada | 54c5ecb | 2018-04-19 12:14:01 +0900 | [diff] [blame] | 49 | static struct regmap *regmap_alloc(int count) |
Simon Glass | 30d73e8 | 2016-07-04 11:58:21 -0600 | [diff] [blame] | 50 | { |
| 51 | struct regmap *map; |
Pratyush Yadav | 279cad8 | 2020-09-24 10:04:11 +0530 | [diff] [blame] | 52 | size_t size = sizeof(*map) + sizeof(map->ranges[0]) * count; |
Simon Glass | 30d73e8 | 2016-07-04 11:58:21 -0600 | [diff] [blame] | 53 | |
Pratyush Yadav | 279cad8 | 2020-09-24 10:04:11 +0530 | [diff] [blame] | 54 | map = calloc(1, size); |
Simon Glass | 30d73e8 | 2016-07-04 11:58:21 -0600 | [diff] [blame] | 55 | if (!map) |
| 56 | return NULL; |
Simon Glass | 30d73e8 | 2016-07-04 11:58:21 -0600 | [diff] [blame] | 57 | map->range_count = count; |
Pratyush Yadav | 1c9867c | 2020-09-24 10:04:12 +0530 | [diff] [blame] | 58 | map->width = REGMAP_SIZE_32; |
Simon Glass | 30d73e8 | 2016-07-04 11:58:21 -0600 | [diff] [blame] | 59 | |
| 60 | return map; |
| 61 | } |
| 62 | |
Simon Glass | b944345 | 2016-07-04 11:57:59 -0600 | [diff] [blame] | 63 | #if CONFIG_IS_ENABLED(OF_PLATDATA) |
Simon Glass | b75b15b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 64 | int regmap_init_mem_plat(struct udevice *dev, fdt_val_t *reg, int count, |
| 65 | struct regmap **mapp) |
Simon Glass | b944345 | 2016-07-04 11:57:59 -0600 | [diff] [blame] | 66 | { |
Simon Glass | b611433 | 2016-07-04 11:58:22 -0600 | [diff] [blame] | 67 | struct regmap_range *range; |
| 68 | struct regmap *map; |
| 69 | |
Masahiro Yamada | 54c5ecb | 2018-04-19 12:14:01 +0900 | [diff] [blame] | 70 | map = regmap_alloc(count); |
Simon Glass | b611433 | 2016-07-04 11:58:22 -0600 | [diff] [blame] | 71 | if (!map) |
| 72 | return -ENOMEM; |
| 73 | |
Masahiro Yamada | 54c5ecb | 2018-04-19 12:14:01 +0900 | [diff] [blame] | 74 | for (range = map->ranges; count > 0; reg += 2, range++, count--) { |
Simon Glass | b611433 | 2016-07-04 11:58:22 -0600 | [diff] [blame] | 75 | range->start = *reg; |
| 76 | range->size = reg[1]; |
| 77 | } |
| 78 | |
| 79 | *mapp = map; |
| 80 | |
Simon Glass | b944345 | 2016-07-04 11:57:59 -0600 | [diff] [blame] | 81 | return 0; |
| 82 | } |
| 83 | #else |
Mario Six | 5159c0c | 2018-10-15 09:24:07 +0200 | [diff] [blame] | 84 | /** |
| 85 | * init_range() - Initialize a single range of a regmap |
| 86 | * @node: Device node that will use the map in question |
| 87 | * @range: Pointer to a regmap_range structure that will be initialized |
| 88 | * @addr_len: The length of the addr parts of the reg property |
| 89 | * @size_len: The length of the size parts of the reg property |
| 90 | * @index: The index of the range to initialize |
| 91 | * |
| 92 | * This function will read the necessary 'reg' information from the device tree |
| 93 | * (the 'addr' part, and the 'length' part), and initialize the range in |
| 94 | * quesion. |
| 95 | * |
| 96 | * Return: 0 if OK, -ve on error |
| 97 | */ |
| 98 | static int init_range(ofnode node, struct regmap_range *range, int addr_len, |
| 99 | int size_len, int index) |
| 100 | { |
| 101 | fdt_size_t sz; |
| 102 | struct resource r; |
| 103 | |
| 104 | if (of_live_active()) { |
| 105 | int ret; |
| 106 | |
| 107 | ret = of_address_to_resource(ofnode_to_np(node), |
| 108 | index, &r); |
| 109 | if (ret) { |
| 110 | debug("%s: Could not read resource of range %d (ret = %d)\n", |
| 111 | ofnode_get_name(node), index, ret); |
| 112 | return ret; |
| 113 | } |
| 114 | |
| 115 | range->start = r.start; |
| 116 | range->size = r.end - r.start + 1; |
| 117 | } else { |
| 118 | int offset = ofnode_to_offset(node); |
| 119 | |
| 120 | range->start = fdtdec_get_addr_size_fixed(gd->fdt_blob, offset, |
| 121 | "reg", index, |
| 122 | addr_len, size_len, |
| 123 | &sz, true); |
| 124 | if (range->start == FDT_ADDR_T_NONE) { |
| 125 | debug("%s: Could not read start of range %d\n", |
| 126 | ofnode_get_name(node), index); |
| 127 | return -EINVAL; |
| 128 | } |
| 129 | |
| 130 | range->size = sz; |
| 131 | } |
| 132 | |
| 133 | return 0; |
| 134 | } |
| 135 | |
Faiz Abbas | 52742f4 | 2019-06-11 00:43:33 +0530 | [diff] [blame] | 136 | int regmap_init_mem_index(ofnode node, struct regmap **mapp, int index) |
| 137 | { |
| 138 | struct regmap *map; |
| 139 | int addr_len, size_len; |
| 140 | int ret; |
| 141 | |
| 142 | addr_len = ofnode_read_simple_addr_cells(ofnode_get_parent(node)); |
| 143 | if (addr_len < 0) { |
| 144 | debug("%s: Error while reading the addr length (ret = %d)\n", |
| 145 | ofnode_get_name(node), addr_len); |
| 146 | return addr_len; |
| 147 | } |
| 148 | |
| 149 | size_len = ofnode_read_simple_size_cells(ofnode_get_parent(node)); |
| 150 | if (size_len < 0) { |
| 151 | debug("%s: Error while reading the size length: (ret = %d)\n", |
| 152 | ofnode_get_name(node), size_len); |
| 153 | return size_len; |
| 154 | } |
| 155 | |
| 156 | map = regmap_alloc(1); |
| 157 | if (!map) |
| 158 | return -ENOMEM; |
| 159 | |
| 160 | ret = init_range(node, map->ranges, addr_len, size_len, index); |
| 161 | if (ret) |
Faiz Abbas | 4bba413 | 2019-11-11 15:29:05 +0530 | [diff] [blame] | 162 | goto err; |
Faiz Abbas | 52742f4 | 2019-06-11 00:43:33 +0530 | [diff] [blame] | 163 | |
| 164 | if (ofnode_read_bool(node, "little-endian")) |
| 165 | map->endianness = REGMAP_LITTLE_ENDIAN; |
| 166 | else if (ofnode_read_bool(node, "big-endian")) |
| 167 | map->endianness = REGMAP_BIG_ENDIAN; |
| 168 | else if (ofnode_read_bool(node, "native-endian")) |
| 169 | map->endianness = REGMAP_NATIVE_ENDIAN; |
| 170 | else /* Default: native endianness */ |
| 171 | map->endianness = REGMAP_NATIVE_ENDIAN; |
| 172 | |
| 173 | *mapp = map; |
| 174 | |
Faiz Abbas | 4bba413 | 2019-11-11 15:29:05 +0530 | [diff] [blame] | 175 | return 0; |
| 176 | err: |
| 177 | regmap_uninit(map); |
| 178 | |
Faiz Abbas | 52742f4 | 2019-06-11 00:43:33 +0530 | [diff] [blame] | 179 | return ret; |
| 180 | } |
| 181 | |
Pratyush Yadav | 7eb2476 | 2020-09-24 10:04:14 +0530 | [diff] [blame] | 182 | int regmap_init_mem_range(ofnode node, ulong r_start, ulong r_size, |
| 183 | struct regmap **mapp) |
| 184 | { |
| 185 | struct regmap *map; |
| 186 | struct regmap_range *range; |
| 187 | |
| 188 | map = regmap_alloc(1); |
| 189 | if (!map) |
| 190 | return -ENOMEM; |
| 191 | |
| 192 | range = &map->ranges[0]; |
| 193 | range->start = r_start; |
| 194 | range->size = r_size; |
| 195 | |
| 196 | if (ofnode_read_bool(node, "little-endian")) |
| 197 | map->endianness = REGMAP_LITTLE_ENDIAN; |
| 198 | else if (ofnode_read_bool(node, "big-endian")) |
| 199 | map->endianness = REGMAP_BIG_ENDIAN; |
| 200 | else if (ofnode_read_bool(node, "native-endian")) |
| 201 | map->endianness = REGMAP_NATIVE_ENDIAN; |
| 202 | else /* Default: native endianness */ |
| 203 | map->endianness = REGMAP_NATIVE_ENDIAN; |
| 204 | |
| 205 | *mapp = map; |
| 206 | return 0; |
| 207 | } |
| 208 | |
Masahiro Yamada | e4873e3 | 2018-04-19 12:14:03 +0900 | [diff] [blame] | 209 | int regmap_init_mem(ofnode node, struct regmap **mapp) |
Simon Glass | ad8f8ab | 2015-06-23 15:38:42 -0600 | [diff] [blame] | 210 | { |
Simon Glass | ad8f8ab | 2015-06-23 15:38:42 -0600 | [diff] [blame] | 211 | struct regmap_range *range; |
Simon Glass | ad8f8ab | 2015-06-23 15:38:42 -0600 | [diff] [blame] | 212 | struct regmap *map; |
| 213 | int count; |
| 214 | int addr_len, size_len, both_len; |
Simon Glass | ad8f8ab | 2015-06-23 15:38:42 -0600 | [diff] [blame] | 215 | int len; |
Jean-Jacques Hiblot | 024611b | 2017-02-13 16:17:48 +0100 | [diff] [blame] | 216 | int index; |
Faiz Abbas | 4bba413 | 2019-11-11 15:29:05 +0530 | [diff] [blame] | 217 | int ret; |
Simon Glass | ad8f8ab | 2015-06-23 15:38:42 -0600 | [diff] [blame] | 218 | |
Masahiro Yamada | e4873e3 | 2018-04-19 12:14:03 +0900 | [diff] [blame] | 219 | addr_len = ofnode_read_simple_addr_cells(ofnode_get_parent(node)); |
Mario Six | 1232116 | 2018-10-04 09:00:43 +0200 | [diff] [blame] | 220 | if (addr_len < 0) { |
| 221 | debug("%s: Error while reading the addr length (ret = %d)\n", |
| 222 | ofnode_get_name(node), addr_len); |
| 223 | return addr_len; |
| 224 | } |
| 225 | |
Masahiro Yamada | e4873e3 | 2018-04-19 12:14:03 +0900 | [diff] [blame] | 226 | size_len = ofnode_read_simple_size_cells(ofnode_get_parent(node)); |
Mario Six | 1232116 | 2018-10-04 09:00:43 +0200 | [diff] [blame] | 227 | if (size_len < 0) { |
| 228 | debug("%s: Error while reading the size length: (ret = %d)\n", |
| 229 | ofnode_get_name(node), size_len); |
| 230 | return size_len; |
| 231 | } |
| 232 | |
Simon Glass | ad8f8ab | 2015-06-23 15:38:42 -0600 | [diff] [blame] | 233 | both_len = addr_len + size_len; |
Mario Six | 1232116 | 2018-10-04 09:00:43 +0200 | [diff] [blame] | 234 | if (!both_len) { |
| 235 | debug("%s: Both addr and size length are zero\n", |
| 236 | ofnode_get_name(node)); |
| 237 | return -EINVAL; |
| 238 | } |
Simon Glass | ad8f8ab | 2015-06-23 15:38:42 -0600 | [diff] [blame] | 239 | |
Masahiro Yamada | e4873e3 | 2018-04-19 12:14:03 +0900 | [diff] [blame] | 240 | len = ofnode_read_size(node, "reg"); |
Mario Six | 6e96ba2 | 2018-10-15 09:24:08 +0200 | [diff] [blame] | 241 | if (len < 0) { |
| 242 | debug("%s: Error while reading reg size (ret = %d)\n", |
| 243 | ofnode_get_name(node), len); |
Simon Glass | eeeb519 | 2017-05-18 20:09:10 -0600 | [diff] [blame] | 244 | return len; |
Mario Six | 6e96ba2 | 2018-10-15 09:24:08 +0200 | [diff] [blame] | 245 | } |
Simon Glass | eeeb519 | 2017-05-18 20:09:10 -0600 | [diff] [blame] | 246 | len /= sizeof(fdt32_t); |
Simon Glass | ad8f8ab | 2015-06-23 15:38:42 -0600 | [diff] [blame] | 247 | count = len / both_len; |
Mario Six | 6e96ba2 | 2018-10-15 09:24:08 +0200 | [diff] [blame] | 248 | if (!count) { |
| 249 | debug("%s: Not enough data in reg property\n", |
| 250 | ofnode_get_name(node)); |
Simon Glass | ad8f8ab | 2015-06-23 15:38:42 -0600 | [diff] [blame] | 251 | return -EINVAL; |
Mario Six | 6e96ba2 | 2018-10-15 09:24:08 +0200 | [diff] [blame] | 252 | } |
Simon Glass | ad8f8ab | 2015-06-23 15:38:42 -0600 | [diff] [blame] | 253 | |
Masahiro Yamada | 54c5ecb | 2018-04-19 12:14:01 +0900 | [diff] [blame] | 254 | map = regmap_alloc(count); |
Simon Glass | ad8f8ab | 2015-06-23 15:38:42 -0600 | [diff] [blame] | 255 | if (!map) |
| 256 | return -ENOMEM; |
| 257 | |
Masahiro Yamada | 54c5ecb | 2018-04-19 12:14:01 +0900 | [diff] [blame] | 258 | for (range = map->ranges, index = 0; count > 0; |
Simon Glass | eeeb519 | 2017-05-18 20:09:10 -0600 | [diff] [blame] | 259 | count--, range++, index++) { |
Faiz Abbas | 4bba413 | 2019-11-11 15:29:05 +0530 | [diff] [blame] | 260 | ret = init_range(node, range, addr_len, size_len, index); |
Mario Six | 5159c0c | 2018-10-15 09:24:07 +0200 | [diff] [blame] | 261 | if (ret) |
Faiz Abbas | 4bba413 | 2019-11-11 15:29:05 +0530 | [diff] [blame] | 262 | goto err; |
Simon Glass | ad8f8ab | 2015-06-23 15:38:42 -0600 | [diff] [blame] | 263 | } |
| 264 | |
Mario Six | e5a3d5b | 2018-10-15 09:24:14 +0200 | [diff] [blame] | 265 | if (ofnode_read_bool(node, "little-endian")) |
| 266 | map->endianness = REGMAP_LITTLE_ENDIAN; |
| 267 | else if (ofnode_read_bool(node, "big-endian")) |
| 268 | map->endianness = REGMAP_BIG_ENDIAN; |
| 269 | else if (ofnode_read_bool(node, "native-endian")) |
| 270 | map->endianness = REGMAP_NATIVE_ENDIAN; |
| 271 | else /* Default: native endianness */ |
| 272 | map->endianness = REGMAP_NATIVE_ENDIAN; |
| 273 | |
Simon Glass | ad8f8ab | 2015-06-23 15:38:42 -0600 | [diff] [blame] | 274 | *mapp = map; |
| 275 | |
| 276 | return 0; |
Faiz Abbas | 4bba413 | 2019-11-11 15:29:05 +0530 | [diff] [blame] | 277 | err: |
| 278 | regmap_uninit(map); |
| 279 | |
| 280 | return ret; |
Simon Glass | ad8f8ab | 2015-06-23 15:38:42 -0600 | [diff] [blame] | 281 | } |
Jean-Jacques Hiblot | de03d12 | 2020-09-24 10:04:10 +0530 | [diff] [blame] | 282 | |
| 283 | static void devm_regmap_release(struct udevice *dev, void *res) |
| 284 | { |
| 285 | regmap_uninit(*(struct regmap **)res); |
| 286 | } |
| 287 | |
| 288 | struct regmap *devm_regmap_init(struct udevice *dev, |
| 289 | const struct regmap_bus *bus, |
| 290 | void *bus_context, |
| 291 | const struct regmap_config *config) |
| 292 | { |
| 293 | int rc; |
Pratyush Yadav | 1c9867c | 2020-09-24 10:04:12 +0530 | [diff] [blame] | 294 | struct regmap **mapp, *map; |
Jean-Jacques Hiblot | de03d12 | 2020-09-24 10:04:10 +0530 | [diff] [blame] | 295 | |
| 296 | mapp = devres_alloc(devm_regmap_release, sizeof(struct regmap *), |
| 297 | __GFP_ZERO); |
| 298 | if (unlikely(!mapp)) |
| 299 | return ERR_PTR(-ENOMEM); |
| 300 | |
Pratyush Yadav | 3e4e50a | 2020-09-24 10:04:15 +0530 | [diff] [blame] | 301 | if (config && config->r_size != 0) |
| 302 | rc = regmap_init_mem_range(dev_ofnode(dev), config->r_start, |
| 303 | config->r_size, mapp); |
| 304 | else |
| 305 | rc = regmap_init_mem(dev_ofnode(dev), mapp); |
Jean-Jacques Hiblot | de03d12 | 2020-09-24 10:04:10 +0530 | [diff] [blame] | 306 | if (rc) |
| 307 | return ERR_PTR(rc); |
| 308 | |
Pratyush Yadav | 1c9867c | 2020-09-24 10:04:12 +0530 | [diff] [blame] | 309 | map = *mapp; |
Pratyush Yadav | 3b94e5d | 2020-09-24 10:04:13 +0530 | [diff] [blame] | 310 | if (config) { |
Pratyush Yadav | 1c9867c | 2020-09-24 10:04:12 +0530 | [diff] [blame] | 311 | map->width = config->width; |
Pratyush Yadav | 3b94e5d | 2020-09-24 10:04:13 +0530 | [diff] [blame] | 312 | map->reg_offset_shift = config->reg_offset_shift; |
| 313 | } |
Pratyush Yadav | 1c9867c | 2020-09-24 10:04:12 +0530 | [diff] [blame] | 314 | |
Jean-Jacques Hiblot | de03d12 | 2020-09-24 10:04:10 +0530 | [diff] [blame] | 315 | devres_add(dev, mapp); |
| 316 | return *mapp; |
| 317 | } |
Simon Glass | b944345 | 2016-07-04 11:57:59 -0600 | [diff] [blame] | 318 | #endif |
Simon Glass | ad8f8ab | 2015-06-23 15:38:42 -0600 | [diff] [blame] | 319 | |
| 320 | void *regmap_get_range(struct regmap *map, unsigned int range_num) |
| 321 | { |
| 322 | struct regmap_range *range; |
| 323 | |
| 324 | if (range_num >= map->range_count) |
| 325 | return NULL; |
Masahiro Yamada | 54c5ecb | 2018-04-19 12:14:01 +0900 | [diff] [blame] | 326 | range = &map->ranges[range_num]; |
Simon Glass | ad8f8ab | 2015-06-23 15:38:42 -0600 | [diff] [blame] | 327 | |
| 328 | return map_sysmem(range->start, range->size); |
| 329 | } |
| 330 | |
| 331 | int regmap_uninit(struct regmap *map) |
| 332 | { |
Simon Glass | ad8f8ab | 2015-06-23 15:38:42 -0600 | [diff] [blame] | 333 | free(map); |
| 334 | |
| 335 | return 0; |
| 336 | } |
Paul Burton | 3977603 | 2016-09-08 07:47:35 +0100 | [diff] [blame] | 337 | |
Mario Six | e5a3d5b | 2018-10-15 09:24:14 +0200 | [diff] [blame] | 338 | static inline u8 __read_8(u8 *addr, enum regmap_endianness_t endianness) |
| 339 | { |
| 340 | return readb(addr); |
| 341 | } |
| 342 | |
| 343 | static inline u16 __read_16(u16 *addr, enum regmap_endianness_t endianness) |
| 344 | { |
| 345 | switch (endianness) { |
| 346 | case REGMAP_LITTLE_ENDIAN: |
| 347 | return in_le16(addr); |
| 348 | case REGMAP_BIG_ENDIAN: |
| 349 | return in_be16(addr); |
| 350 | case REGMAP_NATIVE_ENDIAN: |
| 351 | return readw(addr); |
| 352 | } |
| 353 | |
| 354 | return readw(addr); |
| 355 | } |
| 356 | |
| 357 | static inline u32 __read_32(u32 *addr, enum regmap_endianness_t endianness) |
| 358 | { |
| 359 | switch (endianness) { |
| 360 | case REGMAP_LITTLE_ENDIAN: |
| 361 | return in_le32(addr); |
| 362 | case REGMAP_BIG_ENDIAN: |
| 363 | return in_be32(addr); |
| 364 | case REGMAP_NATIVE_ENDIAN: |
| 365 | return readl(addr); |
| 366 | } |
| 367 | |
| 368 | return readl(addr); |
| 369 | } |
| 370 | |
| 371 | #if defined(in_le64) && defined(in_be64) && defined(readq) |
| 372 | static inline u64 __read_64(u64 *addr, enum regmap_endianness_t endianness) |
| 373 | { |
| 374 | switch (endianness) { |
| 375 | case REGMAP_LITTLE_ENDIAN: |
| 376 | return in_le64(addr); |
| 377 | case REGMAP_BIG_ENDIAN: |
| 378 | return in_be64(addr); |
| 379 | case REGMAP_NATIVE_ENDIAN: |
| 380 | return readq(addr); |
| 381 | } |
| 382 | |
| 383 | return readq(addr); |
| 384 | } |
| 385 | #endif |
| 386 | |
Mario Six | 2f00997 | 2018-10-15 09:24:11 +0200 | [diff] [blame] | 387 | int regmap_raw_read_range(struct regmap *map, uint range_num, uint offset, |
| 388 | void *valp, size_t val_len) |
Paul Burton | 3977603 | 2016-09-08 07:47:35 +0100 | [diff] [blame] | 389 | { |
Mario Six | 2f00997 | 2018-10-15 09:24:11 +0200 | [diff] [blame] | 390 | struct regmap_range *range; |
Mario Six | a4fd59e | 2018-10-15 09:24:10 +0200 | [diff] [blame] | 391 | void *ptr; |
Paul Burton | 3977603 | 2016-09-08 07:47:35 +0100 | [diff] [blame] | 392 | |
Mario Six | 2f00997 | 2018-10-15 09:24:11 +0200 | [diff] [blame] | 393 | if (range_num >= map->range_count) { |
| 394 | debug("%s: range index %d larger than range count\n", |
| 395 | __func__, range_num); |
| 396 | return -ERANGE; |
| 397 | } |
| 398 | range = &map->ranges[range_num]; |
| 399 | |
Pratyush Yadav | 3b94e5d | 2020-09-24 10:04:13 +0530 | [diff] [blame] | 400 | offset <<= map->reg_offset_shift; |
Mario Six | 2f00997 | 2018-10-15 09:24:11 +0200 | [diff] [blame] | 401 | if (offset + val_len > range->size) { |
| 402 | debug("%s: offset/size combination invalid\n", __func__); |
| 403 | return -ERANGE; |
| 404 | } |
Paul Burton | 3977603 | 2016-09-08 07:47:35 +0100 | [diff] [blame] | 405 | |
Pratyush Yadav | 5c42d5d | 2020-05-26 17:35:57 +0530 | [diff] [blame] | 406 | ptr = map_physmem(range->start + offset, val_len, MAP_NOCACHE); |
| 407 | |
Mario Six | a4fd59e | 2018-10-15 09:24:10 +0200 | [diff] [blame] | 408 | switch (val_len) { |
| 409 | case REGMAP_SIZE_8: |
Mario Six | e5a3d5b | 2018-10-15 09:24:14 +0200 | [diff] [blame] | 410 | *((u8 *)valp) = __read_8(ptr, map->endianness); |
Mario Six | a4fd59e | 2018-10-15 09:24:10 +0200 | [diff] [blame] | 411 | break; |
| 412 | case REGMAP_SIZE_16: |
Mario Six | e5a3d5b | 2018-10-15 09:24:14 +0200 | [diff] [blame] | 413 | *((u16 *)valp) = __read_16(ptr, map->endianness); |
Mario Six | a4fd59e | 2018-10-15 09:24:10 +0200 | [diff] [blame] | 414 | break; |
| 415 | case REGMAP_SIZE_32: |
Mario Six | e5a3d5b | 2018-10-15 09:24:14 +0200 | [diff] [blame] | 416 | *((u32 *)valp) = __read_32(ptr, map->endianness); |
Mario Six | a4fd59e | 2018-10-15 09:24:10 +0200 | [diff] [blame] | 417 | break; |
Mario Six | e5a3d5b | 2018-10-15 09:24:14 +0200 | [diff] [blame] | 418 | #if defined(in_le64) && defined(in_be64) && defined(readq) |
Mario Six | a4fd59e | 2018-10-15 09:24:10 +0200 | [diff] [blame] | 419 | case REGMAP_SIZE_64: |
Mario Six | e5a3d5b | 2018-10-15 09:24:14 +0200 | [diff] [blame] | 420 | *((u64 *)valp) = __read_64(ptr, map->endianness); |
Mario Six | a4fd59e | 2018-10-15 09:24:10 +0200 | [diff] [blame] | 421 | break; |
| 422 | #endif |
| 423 | default: |
| 424 | debug("%s: regmap size %zu unknown\n", __func__, val_len); |
| 425 | return -EINVAL; |
| 426 | } |
Mario Six | 2f00997 | 2018-10-15 09:24:11 +0200 | [diff] [blame] | 427 | |
Paul Burton | 3977603 | 2016-09-08 07:47:35 +0100 | [diff] [blame] | 428 | return 0; |
| 429 | } |
| 430 | |
Mario Six | 2f00997 | 2018-10-15 09:24:11 +0200 | [diff] [blame] | 431 | int regmap_raw_read(struct regmap *map, uint offset, void *valp, size_t val_len) |
| 432 | { |
| 433 | return regmap_raw_read_range(map, 0, offset, valp, val_len); |
| 434 | } |
| 435 | |
Mario Six | a4fd59e | 2018-10-15 09:24:10 +0200 | [diff] [blame] | 436 | int regmap_read(struct regmap *map, uint offset, uint *valp) |
Paul Burton | 3977603 | 2016-09-08 07:47:35 +0100 | [diff] [blame] | 437 | { |
Marek BehĂșn | 1044886 | 2021-05-20 13:23:50 +0200 | [diff] [blame] | 438 | union { |
| 439 | u8 v8; |
| 440 | u16 v16; |
| 441 | u32 v32; |
| 442 | u64 v64; |
| 443 | } u; |
| 444 | int res; |
| 445 | |
| 446 | res = regmap_raw_read(map, offset, &u, map->width); |
| 447 | if (res) |
| 448 | return res; |
| 449 | |
| 450 | switch (map->width) { |
| 451 | case REGMAP_SIZE_8: |
| 452 | *valp = u.v8; |
| 453 | break; |
| 454 | case REGMAP_SIZE_16: |
| 455 | *valp = u.v16; |
| 456 | break; |
| 457 | case REGMAP_SIZE_32: |
| 458 | *valp = u.v32; |
| 459 | break; |
| 460 | case REGMAP_SIZE_64: |
| 461 | *valp = u.v64; |
| 462 | break; |
| 463 | default: |
| 464 | unreachable(); |
| 465 | } |
| 466 | |
| 467 | return 0; |
Mario Six | a4fd59e | 2018-10-15 09:24:10 +0200 | [diff] [blame] | 468 | } |
| 469 | |
Mario Six | e5a3d5b | 2018-10-15 09:24:14 +0200 | [diff] [blame] | 470 | static inline void __write_8(u8 *addr, const u8 *val, |
| 471 | enum regmap_endianness_t endianness) |
| 472 | { |
| 473 | writeb(*val, addr); |
| 474 | } |
| 475 | |
| 476 | static inline void __write_16(u16 *addr, const u16 *val, |
| 477 | enum regmap_endianness_t endianness) |
| 478 | { |
| 479 | switch (endianness) { |
| 480 | case REGMAP_NATIVE_ENDIAN: |
| 481 | writew(*val, addr); |
| 482 | break; |
| 483 | case REGMAP_LITTLE_ENDIAN: |
| 484 | out_le16(addr, *val); |
| 485 | break; |
| 486 | case REGMAP_BIG_ENDIAN: |
| 487 | out_be16(addr, *val); |
| 488 | break; |
| 489 | } |
| 490 | } |
| 491 | |
| 492 | static inline void __write_32(u32 *addr, const u32 *val, |
| 493 | enum regmap_endianness_t endianness) |
| 494 | { |
| 495 | switch (endianness) { |
| 496 | case REGMAP_NATIVE_ENDIAN: |
| 497 | writel(*val, addr); |
| 498 | break; |
| 499 | case REGMAP_LITTLE_ENDIAN: |
| 500 | out_le32(addr, *val); |
| 501 | break; |
| 502 | case REGMAP_BIG_ENDIAN: |
| 503 | out_be32(addr, *val); |
| 504 | break; |
| 505 | } |
| 506 | } |
| 507 | |
| 508 | #if defined(out_le64) && defined(out_be64) && defined(writeq) |
| 509 | static inline void __write_64(u64 *addr, const u64 *val, |
| 510 | enum regmap_endianness_t endianness) |
| 511 | { |
| 512 | switch (endianness) { |
| 513 | case REGMAP_NATIVE_ENDIAN: |
| 514 | writeq(*val, addr); |
| 515 | break; |
| 516 | case REGMAP_LITTLE_ENDIAN: |
| 517 | out_le64(addr, *val); |
| 518 | break; |
| 519 | case REGMAP_BIG_ENDIAN: |
| 520 | out_be64(addr, *val); |
| 521 | break; |
| 522 | } |
| 523 | } |
| 524 | #endif |
| 525 | |
Mario Six | 2f00997 | 2018-10-15 09:24:11 +0200 | [diff] [blame] | 526 | int regmap_raw_write_range(struct regmap *map, uint range_num, uint offset, |
| 527 | const void *val, size_t val_len) |
Mario Six | a4fd59e | 2018-10-15 09:24:10 +0200 | [diff] [blame] | 528 | { |
Mario Six | 2f00997 | 2018-10-15 09:24:11 +0200 | [diff] [blame] | 529 | struct regmap_range *range; |
Mario Six | a4fd59e | 2018-10-15 09:24:10 +0200 | [diff] [blame] | 530 | void *ptr; |
Paul Burton | 3977603 | 2016-09-08 07:47:35 +0100 | [diff] [blame] | 531 | |
Mario Six | 2f00997 | 2018-10-15 09:24:11 +0200 | [diff] [blame] | 532 | if (range_num >= map->range_count) { |
| 533 | debug("%s: range index %d larger than range count\n", |
| 534 | __func__, range_num); |
| 535 | return -ERANGE; |
| 536 | } |
| 537 | range = &map->ranges[range_num]; |
| 538 | |
Pratyush Yadav | 3b94e5d | 2020-09-24 10:04:13 +0530 | [diff] [blame] | 539 | offset <<= map->reg_offset_shift; |
Mario Six | 2f00997 | 2018-10-15 09:24:11 +0200 | [diff] [blame] | 540 | if (offset + val_len > range->size) { |
| 541 | debug("%s: offset/size combination invalid\n", __func__); |
| 542 | return -ERANGE; |
| 543 | } |
Mario Six | a4fd59e | 2018-10-15 09:24:10 +0200 | [diff] [blame] | 544 | |
Pratyush Yadav | 5c42d5d | 2020-05-26 17:35:57 +0530 | [diff] [blame] | 545 | ptr = map_physmem(range->start + offset, val_len, MAP_NOCACHE); |
| 546 | |
Mario Six | a4fd59e | 2018-10-15 09:24:10 +0200 | [diff] [blame] | 547 | switch (val_len) { |
| 548 | case REGMAP_SIZE_8: |
Mario Six | e5a3d5b | 2018-10-15 09:24:14 +0200 | [diff] [blame] | 549 | __write_8(ptr, val, map->endianness); |
Mario Six | a4fd59e | 2018-10-15 09:24:10 +0200 | [diff] [blame] | 550 | break; |
| 551 | case REGMAP_SIZE_16: |
Mario Six | e5a3d5b | 2018-10-15 09:24:14 +0200 | [diff] [blame] | 552 | __write_16(ptr, val, map->endianness); |
Mario Six | a4fd59e | 2018-10-15 09:24:10 +0200 | [diff] [blame] | 553 | break; |
| 554 | case REGMAP_SIZE_32: |
Mario Six | e5a3d5b | 2018-10-15 09:24:14 +0200 | [diff] [blame] | 555 | __write_32(ptr, val, map->endianness); |
Mario Six | a4fd59e | 2018-10-15 09:24:10 +0200 | [diff] [blame] | 556 | break; |
Mario Six | e5a3d5b | 2018-10-15 09:24:14 +0200 | [diff] [blame] | 557 | #if defined(out_le64) && defined(out_be64) && defined(writeq) |
Mario Six | a4fd59e | 2018-10-15 09:24:10 +0200 | [diff] [blame] | 558 | case REGMAP_SIZE_64: |
Mario Six | e5a3d5b | 2018-10-15 09:24:14 +0200 | [diff] [blame] | 559 | __write_64(ptr, val, map->endianness); |
Mario Six | a4fd59e | 2018-10-15 09:24:10 +0200 | [diff] [blame] | 560 | break; |
| 561 | #endif |
| 562 | default: |
| 563 | debug("%s: regmap size %zu unknown\n", __func__, val_len); |
| 564 | return -EINVAL; |
| 565 | } |
Paul Burton | 3977603 | 2016-09-08 07:47:35 +0100 | [diff] [blame] | 566 | |
| 567 | return 0; |
| 568 | } |
Neil Armstrong | 5444ec6 | 2018-04-27 11:56:14 +0200 | [diff] [blame] | 569 | |
Mario Six | 2f00997 | 2018-10-15 09:24:11 +0200 | [diff] [blame] | 570 | int regmap_raw_write(struct regmap *map, uint offset, const void *val, |
| 571 | size_t val_len) |
| 572 | { |
| 573 | return regmap_raw_write_range(map, 0, offset, val, val_len); |
| 574 | } |
| 575 | |
Mario Six | a4fd59e | 2018-10-15 09:24:10 +0200 | [diff] [blame] | 576 | int regmap_write(struct regmap *map, uint offset, uint val) |
| 577 | { |
Marek BehĂșn | 1044886 | 2021-05-20 13:23:50 +0200 | [diff] [blame] | 578 | union { |
| 579 | u8 v8; |
| 580 | u16 v16; |
| 581 | u32 v32; |
| 582 | u64 v64; |
| 583 | } u; |
| 584 | |
| 585 | switch (map->width) { |
| 586 | case REGMAP_SIZE_8: |
| 587 | u.v8 = val; |
| 588 | break; |
| 589 | case REGMAP_SIZE_16: |
| 590 | u.v16 = val; |
| 591 | break; |
| 592 | case REGMAP_SIZE_32: |
| 593 | u.v32 = val; |
| 594 | break; |
| 595 | case REGMAP_SIZE_64: |
| 596 | u.v64 = val; |
| 597 | break; |
| 598 | default: |
| 599 | debug("%s: regmap size %zu unknown\n", __func__, |
| 600 | (size_t)map->width); |
| 601 | return -EINVAL; |
| 602 | } |
| 603 | |
| 604 | return regmap_raw_write(map, offset, &u, map->width); |
Mario Six | a4fd59e | 2018-10-15 09:24:10 +0200 | [diff] [blame] | 605 | } |
| 606 | |
Neil Armstrong | 5444ec6 | 2018-04-27 11:56:14 +0200 | [diff] [blame] | 607 | int regmap_update_bits(struct regmap *map, uint offset, uint mask, uint val) |
| 608 | { |
| 609 | uint reg; |
| 610 | int ret; |
| 611 | |
| 612 | ret = regmap_read(map, offset, ®); |
| 613 | if (ret) |
| 614 | return ret; |
| 615 | |
| 616 | reg &= ~mask; |
| 617 | |
Simon Glass | 56bed2a | 2019-10-11 16:16:49 -0600 | [diff] [blame] | 618 | return regmap_write(map, offset, reg | (val & mask)); |
Neil Armstrong | 5444ec6 | 2018-04-27 11:56:14 +0200 | [diff] [blame] | 619 | } |
Jean-Jacques Hiblot | b3bbbcf | 2020-09-24 10:04:16 +0530 | [diff] [blame] | 620 | |
| 621 | int regmap_field_read(struct regmap_field *field, unsigned int *val) |
| 622 | { |
| 623 | int ret; |
| 624 | unsigned int reg_val; |
| 625 | |
| 626 | ret = regmap_read(field->regmap, field->reg, ®_val); |
| 627 | if (ret != 0) |
| 628 | return ret; |
| 629 | |
| 630 | reg_val &= field->mask; |
| 631 | reg_val >>= field->shift; |
| 632 | *val = reg_val; |
| 633 | |
| 634 | return ret; |
| 635 | } |
| 636 | |
| 637 | int regmap_field_write(struct regmap_field *field, unsigned int val) |
| 638 | { |
| 639 | return regmap_update_bits(field->regmap, field->reg, field->mask, |
| 640 | val << field->shift); |
| 641 | } |
| 642 | |
| 643 | static void regmap_field_init(struct regmap_field *rm_field, |
| 644 | struct regmap *regmap, |
| 645 | struct reg_field reg_field) |
| 646 | { |
| 647 | rm_field->regmap = regmap; |
| 648 | rm_field->reg = reg_field.reg; |
| 649 | rm_field->shift = reg_field.lsb; |
| 650 | rm_field->mask = GENMASK(reg_field.msb, reg_field.lsb); |
| 651 | } |
| 652 | |
| 653 | struct regmap_field *devm_regmap_field_alloc(struct udevice *dev, |
| 654 | struct regmap *regmap, |
| 655 | struct reg_field reg_field) |
| 656 | { |
| 657 | struct regmap_field *rm_field = devm_kzalloc(dev, sizeof(*rm_field), |
| 658 | GFP_KERNEL); |
| 659 | if (!rm_field) |
| 660 | return ERR_PTR(-ENOMEM); |
| 661 | |
| 662 | regmap_field_init(rm_field, regmap, reg_field); |
| 663 | |
| 664 | return rm_field; |
| 665 | } |
| 666 | |
| 667 | void devm_regmap_field_free(struct udevice *dev, struct regmap_field *field) |
| 668 | { |
| 669 | devm_kfree(dev, field); |
| 670 | } |
| 671 | |
| 672 | struct regmap_field *regmap_field_alloc(struct regmap *regmap, |
| 673 | struct reg_field reg_field) |
| 674 | { |
| 675 | struct regmap_field *rm_field = kzalloc(sizeof(*rm_field), GFP_KERNEL); |
| 676 | |
| 677 | if (!rm_field) |
| 678 | return ERR_PTR(-ENOMEM); |
| 679 | |
| 680 | regmap_field_init(rm_field, regmap, reg_field); |
| 681 | |
| 682 | return rm_field; |
| 683 | } |
| 684 | |
| 685 | void regmap_field_free(struct regmap_field *field) |
| 686 | { |
| 687 | kfree(field); |
| 688 | } |