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