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 | |
Pratyush Yadav | 7eb2476 | 2020-09-24 10:04:14 +0530 | [diff] [blame] | 167 | int regmap_init_mem_range(ofnode node, ulong r_start, ulong r_size, |
| 168 | struct regmap **mapp) |
| 169 | { |
| 170 | struct regmap *map; |
| 171 | struct regmap_range *range; |
| 172 | |
| 173 | map = regmap_alloc(1); |
| 174 | if (!map) |
| 175 | return -ENOMEM; |
| 176 | |
| 177 | range = &map->ranges[0]; |
| 178 | range->start = r_start; |
| 179 | range->size = r_size; |
| 180 | |
| 181 | if (ofnode_read_bool(node, "little-endian")) |
| 182 | map->endianness = REGMAP_LITTLE_ENDIAN; |
| 183 | else if (ofnode_read_bool(node, "big-endian")) |
| 184 | map->endianness = REGMAP_BIG_ENDIAN; |
| 185 | else if (ofnode_read_bool(node, "native-endian")) |
| 186 | map->endianness = REGMAP_NATIVE_ENDIAN; |
| 187 | else /* Default: native endianness */ |
| 188 | map->endianness = REGMAP_NATIVE_ENDIAN; |
| 189 | |
| 190 | *mapp = map; |
| 191 | return 0; |
| 192 | } |
| 193 | |
Masahiro Yamada | e4873e3 | 2018-04-19 12:14:03 +0900 | [diff] [blame] | 194 | int regmap_init_mem(ofnode node, struct regmap **mapp) |
Simon Glass | ad8f8ab | 2015-06-23 15:38:42 -0600 | [diff] [blame] | 195 | { |
Simon Glass | ad8f8ab | 2015-06-23 15:38:42 -0600 | [diff] [blame] | 196 | struct regmap_range *range; |
Simon Glass | ad8f8ab | 2015-06-23 15:38:42 -0600 | [diff] [blame] | 197 | struct regmap *map; |
| 198 | int count; |
| 199 | int addr_len, size_len, both_len; |
Simon Glass | ad8f8ab | 2015-06-23 15:38:42 -0600 | [diff] [blame] | 200 | int len; |
Jean-Jacques Hiblot | 024611b | 2017-02-13 16:17:48 +0100 | [diff] [blame] | 201 | int index; |
Faiz Abbas | 4bba413 | 2019-11-11 15:29:05 +0530 | [diff] [blame] | 202 | int ret; |
Simon Glass | ad8f8ab | 2015-06-23 15:38:42 -0600 | [diff] [blame] | 203 | |
Masahiro Yamada | e4873e3 | 2018-04-19 12:14:03 +0900 | [diff] [blame] | 204 | addr_len = ofnode_read_simple_addr_cells(ofnode_get_parent(node)); |
Mario Six | 1232116 | 2018-10-04 09:00:43 +0200 | [diff] [blame] | 205 | if (addr_len < 0) { |
| 206 | debug("%s: Error while reading the addr length (ret = %d)\n", |
| 207 | ofnode_get_name(node), addr_len); |
| 208 | return addr_len; |
| 209 | } |
| 210 | |
Masahiro Yamada | e4873e3 | 2018-04-19 12:14:03 +0900 | [diff] [blame] | 211 | size_len = ofnode_read_simple_size_cells(ofnode_get_parent(node)); |
Mario Six | 1232116 | 2018-10-04 09:00:43 +0200 | [diff] [blame] | 212 | if (size_len < 0) { |
| 213 | debug("%s: Error while reading the size length: (ret = %d)\n", |
| 214 | ofnode_get_name(node), size_len); |
| 215 | return size_len; |
| 216 | } |
| 217 | |
Simon Glass | ad8f8ab | 2015-06-23 15:38:42 -0600 | [diff] [blame] | 218 | both_len = addr_len + size_len; |
Mario Six | 1232116 | 2018-10-04 09:00:43 +0200 | [diff] [blame] | 219 | if (!both_len) { |
| 220 | debug("%s: Both addr and size length are zero\n", |
| 221 | ofnode_get_name(node)); |
| 222 | return -EINVAL; |
| 223 | } |
Simon Glass | ad8f8ab | 2015-06-23 15:38:42 -0600 | [diff] [blame] | 224 | |
Masahiro Yamada | e4873e3 | 2018-04-19 12:14:03 +0900 | [diff] [blame] | 225 | len = ofnode_read_size(node, "reg"); |
Mario Six | 6e96ba2 | 2018-10-15 09:24:08 +0200 | [diff] [blame] | 226 | if (len < 0) { |
| 227 | debug("%s: Error while reading reg size (ret = %d)\n", |
| 228 | ofnode_get_name(node), len); |
Simon Glass | eeeb519 | 2017-05-18 20:09:10 -0600 | [diff] [blame] | 229 | return len; |
Mario Six | 6e96ba2 | 2018-10-15 09:24:08 +0200 | [diff] [blame] | 230 | } |
Simon Glass | eeeb519 | 2017-05-18 20:09:10 -0600 | [diff] [blame] | 231 | len /= sizeof(fdt32_t); |
Simon Glass | ad8f8ab | 2015-06-23 15:38:42 -0600 | [diff] [blame] | 232 | count = len / both_len; |
Mario Six | 6e96ba2 | 2018-10-15 09:24:08 +0200 | [diff] [blame] | 233 | if (!count) { |
| 234 | debug("%s: Not enough data in reg property\n", |
| 235 | ofnode_get_name(node)); |
Simon Glass | ad8f8ab | 2015-06-23 15:38:42 -0600 | [diff] [blame] | 236 | return -EINVAL; |
Mario Six | 6e96ba2 | 2018-10-15 09:24:08 +0200 | [diff] [blame] | 237 | } |
Simon Glass | ad8f8ab | 2015-06-23 15:38:42 -0600 | [diff] [blame] | 238 | |
Masahiro Yamada | 54c5ecb | 2018-04-19 12:14:01 +0900 | [diff] [blame] | 239 | map = regmap_alloc(count); |
Simon Glass | ad8f8ab | 2015-06-23 15:38:42 -0600 | [diff] [blame] | 240 | if (!map) |
| 241 | return -ENOMEM; |
| 242 | |
Masahiro Yamada | 54c5ecb | 2018-04-19 12:14:01 +0900 | [diff] [blame] | 243 | for (range = map->ranges, index = 0; count > 0; |
Simon Glass | eeeb519 | 2017-05-18 20:09:10 -0600 | [diff] [blame] | 244 | count--, range++, index++) { |
Faiz Abbas | 4bba413 | 2019-11-11 15:29:05 +0530 | [diff] [blame] | 245 | ret = init_range(node, range, addr_len, size_len, index); |
Mario Six | 5159c0c | 2018-10-15 09:24:07 +0200 | [diff] [blame] | 246 | if (ret) |
Faiz Abbas | 4bba413 | 2019-11-11 15:29:05 +0530 | [diff] [blame] | 247 | goto err; |
Simon Glass | ad8f8ab | 2015-06-23 15:38:42 -0600 | [diff] [blame] | 248 | } |
| 249 | |
Mario Six | e5a3d5b | 2018-10-15 09:24:14 +0200 | [diff] [blame] | 250 | if (ofnode_read_bool(node, "little-endian")) |
| 251 | map->endianness = REGMAP_LITTLE_ENDIAN; |
| 252 | else if (ofnode_read_bool(node, "big-endian")) |
| 253 | map->endianness = REGMAP_BIG_ENDIAN; |
| 254 | else if (ofnode_read_bool(node, "native-endian")) |
| 255 | map->endianness = REGMAP_NATIVE_ENDIAN; |
| 256 | else /* Default: native endianness */ |
| 257 | map->endianness = REGMAP_NATIVE_ENDIAN; |
| 258 | |
Simon Glass | ad8f8ab | 2015-06-23 15:38:42 -0600 | [diff] [blame] | 259 | *mapp = map; |
| 260 | |
| 261 | return 0; |
Faiz Abbas | 4bba413 | 2019-11-11 15:29:05 +0530 | [diff] [blame] | 262 | err: |
| 263 | regmap_uninit(map); |
| 264 | |
| 265 | return ret; |
Simon Glass | ad8f8ab | 2015-06-23 15:38:42 -0600 | [diff] [blame] | 266 | } |
Jean-Jacques Hiblot | de03d12 | 2020-09-24 10:04:10 +0530 | [diff] [blame] | 267 | |
| 268 | static void devm_regmap_release(struct udevice *dev, void *res) |
| 269 | { |
| 270 | regmap_uninit(*(struct regmap **)res); |
| 271 | } |
| 272 | |
| 273 | struct regmap *devm_regmap_init(struct udevice *dev, |
| 274 | const struct regmap_bus *bus, |
| 275 | void *bus_context, |
| 276 | const struct regmap_config *config) |
| 277 | { |
| 278 | int rc; |
Pratyush Yadav | 1c9867c | 2020-09-24 10:04:12 +0530 | [diff] [blame] | 279 | struct regmap **mapp, *map; |
Jean-Jacques Hiblot | de03d12 | 2020-09-24 10:04:10 +0530 | [diff] [blame] | 280 | |
| 281 | mapp = devres_alloc(devm_regmap_release, sizeof(struct regmap *), |
| 282 | __GFP_ZERO); |
| 283 | if (unlikely(!mapp)) |
| 284 | return ERR_PTR(-ENOMEM); |
| 285 | |
Pratyush Yadav | 3e4e50a | 2020-09-24 10:04:15 +0530 | [diff] [blame^] | 286 | if (config && config->r_size != 0) |
| 287 | rc = regmap_init_mem_range(dev_ofnode(dev), config->r_start, |
| 288 | config->r_size, mapp); |
| 289 | else |
| 290 | rc = regmap_init_mem(dev_ofnode(dev), mapp); |
Jean-Jacques Hiblot | de03d12 | 2020-09-24 10:04:10 +0530 | [diff] [blame] | 291 | if (rc) |
| 292 | return ERR_PTR(rc); |
| 293 | |
Pratyush Yadav | 1c9867c | 2020-09-24 10:04:12 +0530 | [diff] [blame] | 294 | map = *mapp; |
Pratyush Yadav | 3b94e5d | 2020-09-24 10:04:13 +0530 | [diff] [blame] | 295 | if (config) { |
Pratyush Yadav | 1c9867c | 2020-09-24 10:04:12 +0530 | [diff] [blame] | 296 | map->width = config->width; |
Pratyush Yadav | 3b94e5d | 2020-09-24 10:04:13 +0530 | [diff] [blame] | 297 | map->reg_offset_shift = config->reg_offset_shift; |
| 298 | } |
Pratyush Yadav | 1c9867c | 2020-09-24 10:04:12 +0530 | [diff] [blame] | 299 | |
Jean-Jacques Hiblot | de03d12 | 2020-09-24 10:04:10 +0530 | [diff] [blame] | 300 | devres_add(dev, mapp); |
| 301 | return *mapp; |
| 302 | } |
Simon Glass | b944345 | 2016-07-04 11:57:59 -0600 | [diff] [blame] | 303 | #endif |
Simon Glass | ad8f8ab | 2015-06-23 15:38:42 -0600 | [diff] [blame] | 304 | |
| 305 | void *regmap_get_range(struct regmap *map, unsigned int range_num) |
| 306 | { |
| 307 | struct regmap_range *range; |
| 308 | |
| 309 | if (range_num >= map->range_count) |
| 310 | return NULL; |
Masahiro Yamada | 54c5ecb | 2018-04-19 12:14:01 +0900 | [diff] [blame] | 311 | range = &map->ranges[range_num]; |
Simon Glass | ad8f8ab | 2015-06-23 15:38:42 -0600 | [diff] [blame] | 312 | |
| 313 | return map_sysmem(range->start, range->size); |
| 314 | } |
| 315 | |
| 316 | int regmap_uninit(struct regmap *map) |
| 317 | { |
Simon Glass | ad8f8ab | 2015-06-23 15:38:42 -0600 | [diff] [blame] | 318 | free(map); |
| 319 | |
| 320 | return 0; |
| 321 | } |
Paul Burton | 3977603 | 2016-09-08 07:47:35 +0100 | [diff] [blame] | 322 | |
Mario Six | e5a3d5b | 2018-10-15 09:24:14 +0200 | [diff] [blame] | 323 | static inline u8 __read_8(u8 *addr, enum regmap_endianness_t endianness) |
| 324 | { |
| 325 | return readb(addr); |
| 326 | } |
| 327 | |
| 328 | static inline u16 __read_16(u16 *addr, enum regmap_endianness_t endianness) |
| 329 | { |
| 330 | switch (endianness) { |
| 331 | case REGMAP_LITTLE_ENDIAN: |
| 332 | return in_le16(addr); |
| 333 | case REGMAP_BIG_ENDIAN: |
| 334 | return in_be16(addr); |
| 335 | case REGMAP_NATIVE_ENDIAN: |
| 336 | return readw(addr); |
| 337 | } |
| 338 | |
| 339 | return readw(addr); |
| 340 | } |
| 341 | |
| 342 | static inline u32 __read_32(u32 *addr, enum regmap_endianness_t endianness) |
| 343 | { |
| 344 | switch (endianness) { |
| 345 | case REGMAP_LITTLE_ENDIAN: |
| 346 | return in_le32(addr); |
| 347 | case REGMAP_BIG_ENDIAN: |
| 348 | return in_be32(addr); |
| 349 | case REGMAP_NATIVE_ENDIAN: |
| 350 | return readl(addr); |
| 351 | } |
| 352 | |
| 353 | return readl(addr); |
| 354 | } |
| 355 | |
| 356 | #if defined(in_le64) && defined(in_be64) && defined(readq) |
| 357 | static inline u64 __read_64(u64 *addr, enum regmap_endianness_t endianness) |
| 358 | { |
| 359 | switch (endianness) { |
| 360 | case REGMAP_LITTLE_ENDIAN: |
| 361 | return in_le64(addr); |
| 362 | case REGMAP_BIG_ENDIAN: |
| 363 | return in_be64(addr); |
| 364 | case REGMAP_NATIVE_ENDIAN: |
| 365 | return readq(addr); |
| 366 | } |
| 367 | |
| 368 | return readq(addr); |
| 369 | } |
| 370 | #endif |
| 371 | |
Mario Six | 2f00997 | 2018-10-15 09:24:11 +0200 | [diff] [blame] | 372 | int regmap_raw_read_range(struct regmap *map, uint range_num, uint offset, |
| 373 | void *valp, size_t val_len) |
Paul Burton | 3977603 | 2016-09-08 07:47:35 +0100 | [diff] [blame] | 374 | { |
Mario Six | 2f00997 | 2018-10-15 09:24:11 +0200 | [diff] [blame] | 375 | struct regmap_range *range; |
Mario Six | a4fd59e | 2018-10-15 09:24:10 +0200 | [diff] [blame] | 376 | void *ptr; |
Paul Burton | 3977603 | 2016-09-08 07:47:35 +0100 | [diff] [blame] | 377 | |
Mario Six | 2f00997 | 2018-10-15 09:24:11 +0200 | [diff] [blame] | 378 | if (range_num >= map->range_count) { |
| 379 | debug("%s: range index %d larger than range count\n", |
| 380 | __func__, range_num); |
| 381 | return -ERANGE; |
| 382 | } |
| 383 | range = &map->ranges[range_num]; |
| 384 | |
Pratyush Yadav | 3b94e5d | 2020-09-24 10:04:13 +0530 | [diff] [blame] | 385 | offset <<= map->reg_offset_shift; |
Mario Six | 2f00997 | 2018-10-15 09:24:11 +0200 | [diff] [blame] | 386 | if (offset + val_len > range->size) { |
| 387 | debug("%s: offset/size combination invalid\n", __func__); |
| 388 | return -ERANGE; |
| 389 | } |
Paul Burton | 3977603 | 2016-09-08 07:47:35 +0100 | [diff] [blame] | 390 | |
Pratyush Yadav | 5c42d5d | 2020-05-26 17:35:57 +0530 | [diff] [blame] | 391 | ptr = map_physmem(range->start + offset, val_len, MAP_NOCACHE); |
| 392 | |
Mario Six | a4fd59e | 2018-10-15 09:24:10 +0200 | [diff] [blame] | 393 | switch (val_len) { |
| 394 | case REGMAP_SIZE_8: |
Mario Six | e5a3d5b | 2018-10-15 09:24:14 +0200 | [diff] [blame] | 395 | *((u8 *)valp) = __read_8(ptr, map->endianness); |
Mario Six | a4fd59e | 2018-10-15 09:24:10 +0200 | [diff] [blame] | 396 | break; |
| 397 | case REGMAP_SIZE_16: |
Mario Six | e5a3d5b | 2018-10-15 09:24:14 +0200 | [diff] [blame] | 398 | *((u16 *)valp) = __read_16(ptr, map->endianness); |
Mario Six | a4fd59e | 2018-10-15 09:24:10 +0200 | [diff] [blame] | 399 | break; |
| 400 | case REGMAP_SIZE_32: |
Mario Six | e5a3d5b | 2018-10-15 09:24:14 +0200 | [diff] [blame] | 401 | *((u32 *)valp) = __read_32(ptr, map->endianness); |
Mario Six | a4fd59e | 2018-10-15 09:24:10 +0200 | [diff] [blame] | 402 | break; |
Mario Six | e5a3d5b | 2018-10-15 09:24:14 +0200 | [diff] [blame] | 403 | #if defined(in_le64) && defined(in_be64) && defined(readq) |
Mario Six | a4fd59e | 2018-10-15 09:24:10 +0200 | [diff] [blame] | 404 | case REGMAP_SIZE_64: |
Mario Six | e5a3d5b | 2018-10-15 09:24:14 +0200 | [diff] [blame] | 405 | *((u64 *)valp) = __read_64(ptr, map->endianness); |
Mario Six | a4fd59e | 2018-10-15 09:24:10 +0200 | [diff] [blame] | 406 | break; |
| 407 | #endif |
| 408 | default: |
| 409 | debug("%s: regmap size %zu unknown\n", __func__, val_len); |
| 410 | return -EINVAL; |
| 411 | } |
Mario Six | 2f00997 | 2018-10-15 09:24:11 +0200 | [diff] [blame] | 412 | |
Paul Burton | 3977603 | 2016-09-08 07:47:35 +0100 | [diff] [blame] | 413 | return 0; |
| 414 | } |
| 415 | |
Mario Six | 2f00997 | 2018-10-15 09:24:11 +0200 | [diff] [blame] | 416 | int regmap_raw_read(struct regmap *map, uint offset, void *valp, size_t val_len) |
| 417 | { |
| 418 | return regmap_raw_read_range(map, 0, offset, valp, val_len); |
| 419 | } |
| 420 | |
Mario Six | a4fd59e | 2018-10-15 09:24:10 +0200 | [diff] [blame] | 421 | int regmap_read(struct regmap *map, uint offset, uint *valp) |
Paul Burton | 3977603 | 2016-09-08 07:47:35 +0100 | [diff] [blame] | 422 | { |
Pratyush Yadav | 1c9867c | 2020-09-24 10:04:12 +0530 | [diff] [blame] | 423 | return regmap_raw_read(map, offset, valp, map->width); |
Mario Six | a4fd59e | 2018-10-15 09:24:10 +0200 | [diff] [blame] | 424 | } |
| 425 | |
Mario Six | e5a3d5b | 2018-10-15 09:24:14 +0200 | [diff] [blame] | 426 | static inline void __write_8(u8 *addr, const u8 *val, |
| 427 | enum regmap_endianness_t endianness) |
| 428 | { |
| 429 | writeb(*val, addr); |
| 430 | } |
| 431 | |
| 432 | static inline void __write_16(u16 *addr, const u16 *val, |
| 433 | enum regmap_endianness_t endianness) |
| 434 | { |
| 435 | switch (endianness) { |
| 436 | case REGMAP_NATIVE_ENDIAN: |
| 437 | writew(*val, addr); |
| 438 | break; |
| 439 | case REGMAP_LITTLE_ENDIAN: |
| 440 | out_le16(addr, *val); |
| 441 | break; |
| 442 | case REGMAP_BIG_ENDIAN: |
| 443 | out_be16(addr, *val); |
| 444 | break; |
| 445 | } |
| 446 | } |
| 447 | |
| 448 | static inline void __write_32(u32 *addr, const u32 *val, |
| 449 | enum regmap_endianness_t endianness) |
| 450 | { |
| 451 | switch (endianness) { |
| 452 | case REGMAP_NATIVE_ENDIAN: |
| 453 | writel(*val, addr); |
| 454 | break; |
| 455 | case REGMAP_LITTLE_ENDIAN: |
| 456 | out_le32(addr, *val); |
| 457 | break; |
| 458 | case REGMAP_BIG_ENDIAN: |
| 459 | out_be32(addr, *val); |
| 460 | break; |
| 461 | } |
| 462 | } |
| 463 | |
| 464 | #if defined(out_le64) && defined(out_be64) && defined(writeq) |
| 465 | static inline void __write_64(u64 *addr, const u64 *val, |
| 466 | enum regmap_endianness_t endianness) |
| 467 | { |
| 468 | switch (endianness) { |
| 469 | case REGMAP_NATIVE_ENDIAN: |
| 470 | writeq(*val, addr); |
| 471 | break; |
| 472 | case REGMAP_LITTLE_ENDIAN: |
| 473 | out_le64(addr, *val); |
| 474 | break; |
| 475 | case REGMAP_BIG_ENDIAN: |
| 476 | out_be64(addr, *val); |
| 477 | break; |
| 478 | } |
| 479 | } |
| 480 | #endif |
| 481 | |
Mario Six | 2f00997 | 2018-10-15 09:24:11 +0200 | [diff] [blame] | 482 | int regmap_raw_write_range(struct regmap *map, uint range_num, uint offset, |
| 483 | const void *val, size_t val_len) |
Mario Six | a4fd59e | 2018-10-15 09:24:10 +0200 | [diff] [blame] | 484 | { |
Mario Six | 2f00997 | 2018-10-15 09:24:11 +0200 | [diff] [blame] | 485 | struct regmap_range *range; |
Mario Six | a4fd59e | 2018-10-15 09:24:10 +0200 | [diff] [blame] | 486 | void *ptr; |
Paul Burton | 3977603 | 2016-09-08 07:47:35 +0100 | [diff] [blame] | 487 | |
Mario Six | 2f00997 | 2018-10-15 09:24:11 +0200 | [diff] [blame] | 488 | if (range_num >= map->range_count) { |
| 489 | debug("%s: range index %d larger than range count\n", |
| 490 | __func__, range_num); |
| 491 | return -ERANGE; |
| 492 | } |
| 493 | range = &map->ranges[range_num]; |
| 494 | |
Pratyush Yadav | 3b94e5d | 2020-09-24 10:04:13 +0530 | [diff] [blame] | 495 | offset <<= map->reg_offset_shift; |
Mario Six | 2f00997 | 2018-10-15 09:24:11 +0200 | [diff] [blame] | 496 | if (offset + val_len > range->size) { |
| 497 | debug("%s: offset/size combination invalid\n", __func__); |
| 498 | return -ERANGE; |
| 499 | } |
Mario Six | a4fd59e | 2018-10-15 09:24:10 +0200 | [diff] [blame] | 500 | |
Pratyush Yadav | 5c42d5d | 2020-05-26 17:35:57 +0530 | [diff] [blame] | 501 | ptr = map_physmem(range->start + offset, val_len, MAP_NOCACHE); |
| 502 | |
Mario Six | a4fd59e | 2018-10-15 09:24:10 +0200 | [diff] [blame] | 503 | switch (val_len) { |
| 504 | case REGMAP_SIZE_8: |
Mario Six | e5a3d5b | 2018-10-15 09:24:14 +0200 | [diff] [blame] | 505 | __write_8(ptr, val, map->endianness); |
Mario Six | a4fd59e | 2018-10-15 09:24:10 +0200 | [diff] [blame] | 506 | break; |
| 507 | case REGMAP_SIZE_16: |
Mario Six | e5a3d5b | 2018-10-15 09:24:14 +0200 | [diff] [blame] | 508 | __write_16(ptr, val, map->endianness); |
Mario Six | a4fd59e | 2018-10-15 09:24:10 +0200 | [diff] [blame] | 509 | break; |
| 510 | case REGMAP_SIZE_32: |
Mario Six | e5a3d5b | 2018-10-15 09:24:14 +0200 | [diff] [blame] | 511 | __write_32(ptr, val, map->endianness); |
Mario Six | a4fd59e | 2018-10-15 09:24:10 +0200 | [diff] [blame] | 512 | break; |
Mario Six | e5a3d5b | 2018-10-15 09:24:14 +0200 | [diff] [blame] | 513 | #if defined(out_le64) && defined(out_be64) && defined(writeq) |
Mario Six | a4fd59e | 2018-10-15 09:24:10 +0200 | [diff] [blame] | 514 | case REGMAP_SIZE_64: |
Mario Six | e5a3d5b | 2018-10-15 09:24:14 +0200 | [diff] [blame] | 515 | __write_64(ptr, val, map->endianness); |
Mario Six | a4fd59e | 2018-10-15 09:24:10 +0200 | [diff] [blame] | 516 | break; |
| 517 | #endif |
| 518 | default: |
| 519 | debug("%s: regmap size %zu unknown\n", __func__, val_len); |
| 520 | return -EINVAL; |
| 521 | } |
Paul Burton | 3977603 | 2016-09-08 07:47:35 +0100 | [diff] [blame] | 522 | |
| 523 | return 0; |
| 524 | } |
Neil Armstrong | 5444ec6 | 2018-04-27 11:56:14 +0200 | [diff] [blame] | 525 | |
Mario Six | 2f00997 | 2018-10-15 09:24:11 +0200 | [diff] [blame] | 526 | int regmap_raw_write(struct regmap *map, uint offset, const void *val, |
| 527 | size_t val_len) |
| 528 | { |
| 529 | return regmap_raw_write_range(map, 0, offset, val, val_len); |
| 530 | } |
| 531 | |
Mario Six | a4fd59e | 2018-10-15 09:24:10 +0200 | [diff] [blame] | 532 | int regmap_write(struct regmap *map, uint offset, uint val) |
| 533 | { |
Pratyush Yadav | 1c9867c | 2020-09-24 10:04:12 +0530 | [diff] [blame] | 534 | return regmap_raw_write(map, offset, &val, map->width); |
Mario Six | a4fd59e | 2018-10-15 09:24:10 +0200 | [diff] [blame] | 535 | } |
| 536 | |
Neil Armstrong | 5444ec6 | 2018-04-27 11:56:14 +0200 | [diff] [blame] | 537 | int regmap_update_bits(struct regmap *map, uint offset, uint mask, uint val) |
| 538 | { |
| 539 | uint reg; |
| 540 | int ret; |
| 541 | |
| 542 | ret = regmap_read(map, offset, ®); |
| 543 | if (ret) |
| 544 | return ret; |
| 545 | |
| 546 | reg &= ~mask; |
| 547 | |
Simon Glass | 56bed2a | 2019-10-11 16:16:49 -0600 | [diff] [blame] | 548 | return regmap_write(map, offset, reg | (val & mask)); |
Neil Armstrong | 5444ec6 | 2018-04-27 11:56:14 +0200 | [diff] [blame] | 549 | } |