Stephen Warren | 45f5ff8 | 2012-04-04 15:48:31 -0600 | [diff] [blame] | 1 | /* |
| 2 | * Register map access API - MMIO support |
| 3 | * |
| 4 | * Copyright (c) 2012, NVIDIA CORPORATION. All rights reserved. |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify it |
| 7 | * under the terms and conditions of the GNU General Public License, |
| 8 | * version 2, as published by the Free Software Foundation. |
| 9 | * |
| 10 | * This program is distributed in the hope it will be useful, but WITHOUT |
| 11 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 12 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for |
| 13 | * more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 17 | */ |
| 18 | |
Philipp Zabel | 878ec67 | 2013-02-14 17:39:08 +0100 | [diff] [blame] | 19 | #include <linux/clk.h> |
Stephen Warren | 45f5ff8 | 2012-04-04 15:48:31 -0600 | [diff] [blame] | 20 | #include <linux/err.h> |
Stephen Warren | 45f5ff8 | 2012-04-04 15:48:31 -0600 | [diff] [blame] | 21 | #include <linux/io.h> |
| 22 | #include <linux/module.h> |
| 23 | #include <linux/regmap.h> |
| 24 | #include <linux/slab.h> |
| 25 | |
| 26 | struct regmap_mmio_context { |
| 27 | void __iomem *regs; |
| 28 | unsigned val_bytes; |
Philipp Zabel | 878ec67 | 2013-02-14 17:39:08 +0100 | [diff] [blame] | 29 | struct clk *clk; |
Stephen Warren | 45f5ff8 | 2012-04-04 15:48:31 -0600 | [diff] [blame] | 30 | |
Mark Brown | 922a9f9 | 2016-01-27 04:50:07 +0000 | [diff] [blame] | 31 | void (*reg_write)(struct regmap_mmio_context *ctx, |
| 32 | unsigned int reg, unsigned int val); |
| 33 | unsigned int (*reg_read)(struct regmap_mmio_context *ctx, |
| 34 | unsigned int reg); |
| 35 | }; |
Xiubo Li | 41b0c2c | 2014-03-27 12:42:42 +0800 | [diff] [blame] | 36 | |
Xiubo Li | 451485b | 2014-03-28 13:12:56 +0800 | [diff] [blame] | 37 | static int regmap_mmio_regbits_check(size_t reg_bits) |
| 38 | { |
| 39 | switch (reg_bits) { |
| 40 | case 8: |
| 41 | case 16: |
| 42 | case 32: |
| 43 | #ifdef CONFIG_64BIT |
| 44 | case 64: |
| 45 | #endif |
| 46 | return 0; |
| 47 | default: |
| 48 | return -EINVAL; |
| 49 | } |
| 50 | } |
| 51 | |
Xiubo Li | 75fb0aa | 2015-12-03 13:27:21 +0800 | [diff] [blame] | 52 | static int regmap_mmio_get_min_stride(size_t val_bits) |
| 53 | { |
| 54 | int min_stride; |
| 55 | |
| 56 | switch (val_bits) { |
| 57 | case 8: |
| 58 | /* The core treats 0 as 1 */ |
| 59 | min_stride = 0; |
| 60 | return 0; |
| 61 | case 16: |
| 62 | min_stride = 2; |
| 63 | break; |
| 64 | case 32: |
| 65 | min_stride = 4; |
| 66 | break; |
| 67 | #ifdef CONFIG_64BIT |
| 68 | case 64: |
| 69 | min_stride = 8; |
| 70 | break; |
| 71 | #endif |
| 72 | default: |
| 73 | return -EINVAL; |
| 74 | } |
| 75 | |
| 76 | return min_stride; |
| 77 | } |
| 78 | |
Mark Brown | 922a9f9 | 2016-01-27 04:50:07 +0000 | [diff] [blame] | 79 | static void regmap_mmio_write8(struct regmap_mmio_context *ctx, |
| 80 | unsigned int reg, |
| 81 | unsigned int val) |
Xiubo Li | 41b0c2c | 2014-03-27 12:42:42 +0800 | [diff] [blame] | 82 | { |
Mark Brown | 922a9f9 | 2016-01-27 04:50:07 +0000 | [diff] [blame] | 83 | writeb(val, ctx->regs + reg); |
Xiubo Li | 41b0c2c | 2014-03-27 12:42:42 +0800 | [diff] [blame] | 84 | } |
| 85 | |
Mark Brown | 922a9f9 | 2016-01-27 04:50:07 +0000 | [diff] [blame] | 86 | static void regmap_mmio_write16le(struct regmap_mmio_context *ctx, |
| 87 | unsigned int reg, |
| 88 | unsigned int val) |
Xiubo Li | 88cb32c | 2014-04-02 10:20:17 +0800 | [diff] [blame] | 89 | { |
Mark Brown | 922a9f9 | 2016-01-27 04:50:07 +0000 | [diff] [blame] | 90 | writew(val, ctx->regs + reg); |
| 91 | } |
| 92 | |
| 93 | static void regmap_mmio_write16be(struct regmap_mmio_context *ctx, |
| 94 | unsigned int reg, |
| 95 | unsigned int val) |
| 96 | { |
| 97 | iowrite16be(val, ctx->regs + reg); |
| 98 | } |
| 99 | |
| 100 | static void regmap_mmio_write32le(struct regmap_mmio_context *ctx, |
| 101 | unsigned int reg, |
| 102 | unsigned int val) |
| 103 | { |
| 104 | writel(val, ctx->regs + reg); |
| 105 | } |
| 106 | |
| 107 | static void regmap_mmio_write32be(struct regmap_mmio_context *ctx, |
| 108 | unsigned int reg, |
| 109 | unsigned int val) |
| 110 | { |
| 111 | iowrite32be(val, ctx->regs + reg); |
| 112 | } |
| 113 | |
Xiubo Li | 88cb32c | 2014-04-02 10:20:17 +0800 | [diff] [blame] | 114 | #ifdef CONFIG_64BIT |
Mark Brown | 922a9f9 | 2016-01-27 04:50:07 +0000 | [diff] [blame] | 115 | static void regmap_mmio_write64le(struct regmap_mmio_context *ctx, |
| 116 | unsigned int reg, |
| 117 | unsigned int val) |
| 118 | { |
| 119 | writeq(val, ctx->regs + reg); |
Stephen Warren | 45f5ff8 | 2012-04-04 15:48:31 -0600 | [diff] [blame] | 120 | } |
Mark Brown | 922a9f9 | 2016-01-27 04:50:07 +0000 | [diff] [blame] | 121 | #endif |
Stephen Warren | 45f5ff8 | 2012-04-04 15:48:31 -0600 | [diff] [blame] | 122 | |
Mark Brown | 922a9f9 | 2016-01-27 04:50:07 +0000 | [diff] [blame] | 123 | static int regmap_mmio_write(void *context, unsigned int reg, unsigned int val) |
Stephen Warren | 45f5ff8 | 2012-04-04 15:48:31 -0600 | [diff] [blame] | 124 | { |
| 125 | struct regmap_mmio_context *ctx = context; |
Philipp Zabel | 878ec67 | 2013-02-14 17:39:08 +0100 | [diff] [blame] | 126 | int ret; |
Stephen Warren | 45f5ff8 | 2012-04-04 15:48:31 -0600 | [diff] [blame] | 127 | |
Stephen Warren | 6b8e090 | 2013-11-25 15:12:47 -0700 | [diff] [blame] | 128 | if (!IS_ERR(ctx->clk)) { |
Philipp Zabel | 878ec67 | 2013-02-14 17:39:08 +0100 | [diff] [blame] | 129 | ret = clk_enable(ctx->clk); |
| 130 | if (ret < 0) |
| 131 | return ret; |
| 132 | } |
| 133 | |
Mark Brown | 922a9f9 | 2016-01-27 04:50:07 +0000 | [diff] [blame] | 134 | ctx->reg_write(ctx, reg, val); |
Stephen Warren | 45f5ff8 | 2012-04-04 15:48:31 -0600 | [diff] [blame] | 135 | |
Stephen Warren | 6b8e090 | 2013-11-25 15:12:47 -0700 | [diff] [blame] | 136 | if (!IS_ERR(ctx->clk)) |
Philipp Zabel | 878ec67 | 2013-02-14 17:39:08 +0100 | [diff] [blame] | 137 | clk_disable(ctx->clk); |
| 138 | |
Stephen Warren | 45f5ff8 | 2012-04-04 15:48:31 -0600 | [diff] [blame] | 139 | return 0; |
| 140 | } |
| 141 | |
Mark Brown | 922a9f9 | 2016-01-27 04:50:07 +0000 | [diff] [blame] | 142 | static unsigned int regmap_mmio_read8(struct regmap_mmio_context *ctx, |
| 143 | unsigned int reg) |
Stephen Warren | 45f5ff8 | 2012-04-04 15:48:31 -0600 | [diff] [blame] | 144 | { |
Mark Brown | 922a9f9 | 2016-01-27 04:50:07 +0000 | [diff] [blame] | 145 | return readb(ctx->regs + reg); |
Stephen Warren | 45f5ff8 | 2012-04-04 15:48:31 -0600 | [diff] [blame] | 146 | } |
| 147 | |
Mark Brown | 922a9f9 | 2016-01-27 04:50:07 +0000 | [diff] [blame] | 148 | static unsigned int regmap_mmio_read16le(struct regmap_mmio_context *ctx, |
| 149 | unsigned int reg) |
| 150 | { |
| 151 | return readw(ctx->regs + reg); |
| 152 | } |
| 153 | |
| 154 | static unsigned int regmap_mmio_read16be(struct regmap_mmio_context *ctx, |
| 155 | unsigned int reg) |
| 156 | { |
| 157 | return ioread16be(ctx->regs + reg); |
| 158 | } |
| 159 | |
| 160 | static unsigned int regmap_mmio_read32le(struct regmap_mmio_context *ctx, |
| 161 | unsigned int reg) |
| 162 | { |
| 163 | return readl(ctx->regs + reg); |
| 164 | } |
| 165 | |
| 166 | static unsigned int regmap_mmio_read32be(struct regmap_mmio_context *ctx, |
| 167 | unsigned int reg) |
| 168 | { |
| 169 | return ioread32be(ctx->regs + reg); |
| 170 | } |
| 171 | |
| 172 | #ifdef CONFIG_64BIT |
| 173 | static unsigned int regmap_mmio_read64le(struct regmap_mmio_context *ctx, |
| 174 | unsigned int reg) |
| 175 | { |
| 176 | return readq(ctx->regs + reg); |
| 177 | } |
| 178 | #endif |
| 179 | |
| 180 | static int regmap_mmio_read(void *context, unsigned int reg, unsigned int *val) |
Stephen Warren | 45f5ff8 | 2012-04-04 15:48:31 -0600 | [diff] [blame] | 181 | { |
| 182 | struct regmap_mmio_context *ctx = context; |
Philipp Zabel | 878ec67 | 2013-02-14 17:39:08 +0100 | [diff] [blame] | 183 | int ret; |
Stephen Warren | 45f5ff8 | 2012-04-04 15:48:31 -0600 | [diff] [blame] | 184 | |
Stephen Warren | 6b8e090 | 2013-11-25 15:12:47 -0700 | [diff] [blame] | 185 | if (!IS_ERR(ctx->clk)) { |
Philipp Zabel | 878ec67 | 2013-02-14 17:39:08 +0100 | [diff] [blame] | 186 | ret = clk_enable(ctx->clk); |
| 187 | if (ret < 0) |
| 188 | return ret; |
| 189 | } |
| 190 | |
Mark Brown | 922a9f9 | 2016-01-27 04:50:07 +0000 | [diff] [blame] | 191 | *val = ctx->reg_read(ctx, reg); |
Stephen Warren | 45f5ff8 | 2012-04-04 15:48:31 -0600 | [diff] [blame] | 192 | |
Stephen Warren | 6b8e090 | 2013-11-25 15:12:47 -0700 | [diff] [blame] | 193 | if (!IS_ERR(ctx->clk)) |
Philipp Zabel | 878ec67 | 2013-02-14 17:39:08 +0100 | [diff] [blame] | 194 | clk_disable(ctx->clk); |
| 195 | |
Stephen Warren | 45f5ff8 | 2012-04-04 15:48:31 -0600 | [diff] [blame] | 196 | return 0; |
| 197 | } |
| 198 | |
| 199 | static void regmap_mmio_free_context(void *context) |
| 200 | { |
Philipp Zabel | 878ec67 | 2013-02-14 17:39:08 +0100 | [diff] [blame] | 201 | struct regmap_mmio_context *ctx = context; |
| 202 | |
Stephen Warren | 6b8e090 | 2013-11-25 15:12:47 -0700 | [diff] [blame] | 203 | if (!IS_ERR(ctx->clk)) { |
Philipp Zabel | 878ec67 | 2013-02-14 17:39:08 +0100 | [diff] [blame] | 204 | clk_unprepare(ctx->clk); |
| 205 | clk_put(ctx->clk); |
| 206 | } |
Stephen Warren | 45f5ff8 | 2012-04-04 15:48:31 -0600 | [diff] [blame] | 207 | kfree(context); |
| 208 | } |
| 209 | |
Mark Brown | 922a9f9 | 2016-01-27 04:50:07 +0000 | [diff] [blame] | 210 | static const struct regmap_bus regmap_mmio = { |
Stephen Warren | 45f5ff8 | 2012-04-04 15:48:31 -0600 | [diff] [blame] | 211 | .fast_io = true, |
Mark Brown | 922a9f9 | 2016-01-27 04:50:07 +0000 | [diff] [blame] | 212 | .reg_write = regmap_mmio_write, |
| 213 | .reg_read = regmap_mmio_read, |
Stephen Warren | 45f5ff8 | 2012-04-04 15:48:31 -0600 | [diff] [blame] | 214 | .free_context = regmap_mmio_free_context, |
| 215 | }; |
| 216 | |
Philipp Zabel | 878ec67 | 2013-02-14 17:39:08 +0100 | [diff] [blame] | 217 | static struct regmap_mmio_context *regmap_mmio_gen_context(struct device *dev, |
| 218 | const char *clk_id, |
| 219 | void __iomem *regs, |
Stephen Warren | 45f5ff8 | 2012-04-04 15:48:31 -0600 | [diff] [blame] | 220 | const struct regmap_config *config) |
| 221 | { |
| 222 | struct regmap_mmio_context *ctx; |
Stephen Warren | f01ee60 | 2012-04-09 13:40:24 -0600 | [diff] [blame] | 223 | int min_stride; |
Philipp Zabel | 878ec67 | 2013-02-14 17:39:08 +0100 | [diff] [blame] | 224 | int ret; |
Stephen Warren | 45f5ff8 | 2012-04-04 15:48:31 -0600 | [diff] [blame] | 225 | |
Xiubo Li | 451485b | 2014-03-28 13:12:56 +0800 | [diff] [blame] | 226 | ret = regmap_mmio_regbits_check(config->reg_bits); |
| 227 | if (ret) |
| 228 | return ERR_PTR(ret); |
Stephen Warren | 45f5ff8 | 2012-04-04 15:48:31 -0600 | [diff] [blame] | 229 | |
| 230 | if (config->pad_bits) |
| 231 | return ERR_PTR(-EINVAL); |
| 232 | |
Xiubo Li | 75fb0aa | 2015-12-03 13:27:21 +0800 | [diff] [blame] | 233 | min_stride = regmap_mmio_get_min_stride(config->val_bits); |
| 234 | if (min_stride < 0) |
| 235 | return ERR_PTR(min_stride); |
Stephen Warren | 45f5ff8 | 2012-04-04 15:48:31 -0600 | [diff] [blame] | 236 | |
Stephen Warren | f01ee60 | 2012-04-09 13:40:24 -0600 | [diff] [blame] | 237 | if (config->reg_stride < min_stride) |
| 238 | return ERR_PTR(-EINVAL); |
| 239 | |
Dimitris Papastamos | 4633511 | 2012-07-18 14:17:23 +0100 | [diff] [blame] | 240 | ctx = kzalloc(sizeof(*ctx), GFP_KERNEL); |
Stephen Warren | 45f5ff8 | 2012-04-04 15:48:31 -0600 | [diff] [blame] | 241 | if (!ctx) |
| 242 | return ERR_PTR(-ENOMEM); |
| 243 | |
| 244 | ctx->regs = regs; |
| 245 | ctx->val_bytes = config->val_bits / 8; |
Stephen Warren | 6b8e090 | 2013-11-25 15:12:47 -0700 | [diff] [blame] | 246 | ctx->clk = ERR_PTR(-ENODEV); |
Stephen Warren | 45f5ff8 | 2012-04-04 15:48:31 -0600 | [diff] [blame] | 247 | |
Mark Brown | 9f9f8b8 | 2016-03-23 12:13:12 +0000 | [diff] [blame] | 248 | switch (config->val_format_endian) { |
Mark Brown | 922a9f9 | 2016-01-27 04:50:07 +0000 | [diff] [blame] | 249 | case REGMAP_ENDIAN_DEFAULT: |
| 250 | case REGMAP_ENDIAN_LITTLE: |
| 251 | #ifdef __LITTLE_ENDIAN |
| 252 | case REGMAP_ENDIAN_NATIVE: |
| 253 | #endif |
| 254 | switch (config->val_bits) { |
| 255 | case 8: |
| 256 | ctx->reg_read = regmap_mmio_read8; |
| 257 | ctx->reg_write = regmap_mmio_write8; |
| 258 | break; |
| 259 | case 16: |
| 260 | ctx->reg_read = regmap_mmio_read16le; |
| 261 | ctx->reg_write = regmap_mmio_write16le; |
| 262 | break; |
| 263 | case 32: |
| 264 | ctx->reg_read = regmap_mmio_read32le; |
| 265 | ctx->reg_write = regmap_mmio_write32le; |
| 266 | break; |
| 267 | #ifdef CONFIG_64BIT |
| 268 | case 64: |
| 269 | ctx->reg_read = regmap_mmio_read64le; |
| 270 | ctx->reg_write = regmap_mmio_write64le; |
| 271 | break; |
| 272 | #endif |
| 273 | default: |
| 274 | ret = -EINVAL; |
| 275 | goto err_free; |
| 276 | } |
| 277 | break; |
| 278 | case REGMAP_ENDIAN_BIG: |
| 279 | #ifdef __BIG_ENDIAN |
| 280 | case REGMAP_ENDIAN_NATIVE: |
| 281 | #endif |
| 282 | switch (config->val_bits) { |
| 283 | case 8: |
| 284 | ctx->reg_read = regmap_mmio_read8; |
| 285 | ctx->reg_write = regmap_mmio_write8; |
| 286 | break; |
| 287 | case 16: |
| 288 | ctx->reg_read = regmap_mmio_read16be; |
| 289 | ctx->reg_write = regmap_mmio_write16be; |
| 290 | break; |
| 291 | case 32: |
| 292 | ctx->reg_read = regmap_mmio_read32be; |
| 293 | ctx->reg_write = regmap_mmio_write32be; |
| 294 | break; |
| 295 | default: |
| 296 | ret = -EINVAL; |
| 297 | goto err_free; |
| 298 | } |
| 299 | break; |
| 300 | default: |
| 301 | ret = -EINVAL; |
| 302 | goto err_free; |
| 303 | } |
| 304 | |
Philipp Zabel | 878ec67 | 2013-02-14 17:39:08 +0100 | [diff] [blame] | 305 | if (clk_id == NULL) |
| 306 | return ctx; |
| 307 | |
| 308 | ctx->clk = clk_get(dev, clk_id); |
| 309 | if (IS_ERR(ctx->clk)) { |
| 310 | ret = PTR_ERR(ctx->clk); |
| 311 | goto err_free; |
| 312 | } |
| 313 | |
| 314 | ret = clk_prepare(ctx->clk); |
| 315 | if (ret < 0) { |
| 316 | clk_put(ctx->clk); |
| 317 | goto err_free; |
| 318 | } |
| 319 | |
Stephen Warren | 45f5ff8 | 2012-04-04 15:48:31 -0600 | [diff] [blame] | 320 | return ctx; |
Philipp Zabel | 878ec67 | 2013-02-14 17:39:08 +0100 | [diff] [blame] | 321 | |
| 322 | err_free: |
| 323 | kfree(ctx); |
| 324 | |
| 325 | return ERR_PTR(ret); |
Stephen Warren | 45f5ff8 | 2012-04-04 15:48:31 -0600 | [diff] [blame] | 326 | } |
| 327 | |
Nicolas Boichat | 3cfe7a7 | 2015-07-08 14:30:18 +0800 | [diff] [blame] | 328 | struct regmap *__regmap_init_mmio_clk(struct device *dev, const char *clk_id, |
| 329 | void __iomem *regs, |
| 330 | const struct regmap_config *config, |
| 331 | struct lock_class_key *lock_key, |
| 332 | const char *lock_name) |
Stephen Warren | 45f5ff8 | 2012-04-04 15:48:31 -0600 | [diff] [blame] | 333 | { |
| 334 | struct regmap_mmio_context *ctx; |
| 335 | |
Philipp Zabel | 878ec67 | 2013-02-14 17:39:08 +0100 | [diff] [blame] | 336 | ctx = regmap_mmio_gen_context(dev, clk_id, regs, config); |
Stephen Warren | 45f5ff8 | 2012-04-04 15:48:31 -0600 | [diff] [blame] | 337 | if (IS_ERR(ctx)) |
| 338 | return ERR_CAST(ctx); |
| 339 | |
Nicolas Boichat | 3cfe7a7 | 2015-07-08 14:30:18 +0800 | [diff] [blame] | 340 | return __regmap_init(dev, ®map_mmio, ctx, config, |
| 341 | lock_key, lock_name); |
Stephen Warren | 45f5ff8 | 2012-04-04 15:48:31 -0600 | [diff] [blame] | 342 | } |
Nicolas Boichat | 3cfe7a7 | 2015-07-08 14:30:18 +0800 | [diff] [blame] | 343 | EXPORT_SYMBOL_GPL(__regmap_init_mmio_clk); |
Stephen Warren | 45f5ff8 | 2012-04-04 15:48:31 -0600 | [diff] [blame] | 344 | |
Nicolas Boichat | 3cfe7a7 | 2015-07-08 14:30:18 +0800 | [diff] [blame] | 345 | struct regmap *__devm_regmap_init_mmio_clk(struct device *dev, |
| 346 | const char *clk_id, |
| 347 | void __iomem *regs, |
| 348 | const struct regmap_config *config, |
| 349 | struct lock_class_key *lock_key, |
| 350 | const char *lock_name) |
Stephen Warren | 45f5ff8 | 2012-04-04 15:48:31 -0600 | [diff] [blame] | 351 | { |
| 352 | struct regmap_mmio_context *ctx; |
| 353 | |
Philipp Zabel | 878ec67 | 2013-02-14 17:39:08 +0100 | [diff] [blame] | 354 | ctx = regmap_mmio_gen_context(dev, clk_id, regs, config); |
Stephen Warren | 45f5ff8 | 2012-04-04 15:48:31 -0600 | [diff] [blame] | 355 | if (IS_ERR(ctx)) |
| 356 | return ERR_CAST(ctx); |
| 357 | |
Nicolas Boichat | 3cfe7a7 | 2015-07-08 14:30:18 +0800 | [diff] [blame] | 358 | return __devm_regmap_init(dev, ®map_mmio, ctx, config, |
| 359 | lock_key, lock_name); |
Stephen Warren | 45f5ff8 | 2012-04-04 15:48:31 -0600 | [diff] [blame] | 360 | } |
Nicolas Boichat | 3cfe7a7 | 2015-07-08 14:30:18 +0800 | [diff] [blame] | 361 | EXPORT_SYMBOL_GPL(__devm_regmap_init_mmio_clk); |
Stephen Warren | 45f5ff8 | 2012-04-04 15:48:31 -0600 | [diff] [blame] | 362 | |
| 363 | MODULE_LICENSE("GPL v2"); |