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> |
| 21 | #include <linux/init.h> |
| 22 | #include <linux/io.h> |
| 23 | #include <linux/module.h> |
| 24 | #include <linux/regmap.h> |
| 25 | #include <linux/slab.h> |
| 26 | |
| 27 | struct regmap_mmio_context { |
| 28 | void __iomem *regs; |
Xiubo Li | 9325804 | 2014-03-27 12:42:43 +0800 | [diff] [blame^] | 29 | unsigned reg_bytes; |
Stephen Warren | 45f5ff8 | 2012-04-04 15:48:31 -0600 | [diff] [blame] | 30 | unsigned val_bytes; |
Xiubo Li | 9325804 | 2014-03-27 12:42:43 +0800 | [diff] [blame^] | 31 | unsigned pad_bytes; |
Philipp Zabel | 878ec67 | 2013-02-14 17:39:08 +0100 | [diff] [blame] | 32 | struct clk *clk; |
Stephen Warren | 45f5ff8 | 2012-04-04 15:48:31 -0600 | [diff] [blame] | 33 | }; |
| 34 | |
Xiubo Li | 41b0c2c | 2014-03-27 12:42:42 +0800 | [diff] [blame] | 35 | static inline void regmap_mmio_regsize_check(size_t reg_size) |
| 36 | { |
Xiubo Li | 9325804 | 2014-03-27 12:42:43 +0800 | [diff] [blame^] | 37 | switch (reg_size) { |
| 38 | case 1: |
| 39 | case 2: |
| 40 | case 4: |
| 41 | #ifdef CONFIG_64BIT |
| 42 | case 8: |
| 43 | #endif |
| 44 | break; |
| 45 | default: |
| 46 | BUG(); |
| 47 | } |
Xiubo Li | 41b0c2c | 2014-03-27 12:42:42 +0800 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | static inline void regmap_mmio_count_check(size_t count) |
| 51 | { |
Xiubo Li | 9325804 | 2014-03-27 12:42:43 +0800 | [diff] [blame^] | 52 | BUG_ON(count % 2 != 0); |
Xiubo Li | 41b0c2c | 2014-03-27 12:42:42 +0800 | [diff] [blame] | 53 | } |
| 54 | |
Stephen Warren | 45f5ff8 | 2012-04-04 15:48:31 -0600 | [diff] [blame] | 55 | static int regmap_mmio_gather_write(void *context, |
| 56 | const void *reg, size_t reg_size, |
| 57 | const void *val, size_t val_size) |
| 58 | { |
| 59 | struct regmap_mmio_context *ctx = context; |
| 60 | u32 offset; |
Philipp Zabel | 878ec67 | 2013-02-14 17:39:08 +0100 | [diff] [blame] | 61 | int ret; |
Stephen Warren | 45f5ff8 | 2012-04-04 15:48:31 -0600 | [diff] [blame] | 62 | |
Xiubo Li | 41b0c2c | 2014-03-27 12:42:42 +0800 | [diff] [blame] | 63 | regmap_mmio_regsize_check(reg_size); |
Stephen Warren | 40606db | 2012-04-06 15:17:32 -0600 | [diff] [blame] | 64 | |
Stephen Warren | 6b8e090 | 2013-11-25 15:12:47 -0700 | [diff] [blame] | 65 | if (!IS_ERR(ctx->clk)) { |
Philipp Zabel | 878ec67 | 2013-02-14 17:39:08 +0100 | [diff] [blame] | 66 | ret = clk_enable(ctx->clk); |
| 67 | if (ret < 0) |
| 68 | return ret; |
| 69 | } |
| 70 | |
Stephen Warren | 6a55244 | 2012-05-24 10:47:27 -0600 | [diff] [blame] | 71 | offset = *(u32 *)reg; |
Stephen Warren | 45f5ff8 | 2012-04-04 15:48:31 -0600 | [diff] [blame] | 72 | |
| 73 | while (val_size) { |
| 74 | switch (ctx->val_bytes) { |
| 75 | case 1: |
| 76 | writeb(*(u8 *)val, ctx->regs + offset); |
| 77 | break; |
| 78 | case 2: |
Stephen Warren | 6a55244 | 2012-05-24 10:47:27 -0600 | [diff] [blame] | 79 | writew(*(u16 *)val, ctx->regs + offset); |
Stephen Warren | 45f5ff8 | 2012-04-04 15:48:31 -0600 | [diff] [blame] | 80 | break; |
| 81 | case 4: |
Stephen Warren | 6a55244 | 2012-05-24 10:47:27 -0600 | [diff] [blame] | 82 | writel(*(u32 *)val, ctx->regs + offset); |
Stephen Warren | 45f5ff8 | 2012-04-04 15:48:31 -0600 | [diff] [blame] | 83 | break; |
| 84 | #ifdef CONFIG_64BIT |
| 85 | case 8: |
Stephen Warren | 6a55244 | 2012-05-24 10:47:27 -0600 | [diff] [blame] | 86 | writeq(*(u64 *)val, ctx->regs + offset); |
Stephen Warren | 45f5ff8 | 2012-04-04 15:48:31 -0600 | [diff] [blame] | 87 | break; |
| 88 | #endif |
| 89 | default: |
| 90 | /* Should be caught by regmap_mmio_check_config */ |
Stephen Warren | 40606db | 2012-04-06 15:17:32 -0600 | [diff] [blame] | 91 | BUG(); |
Stephen Warren | 45f5ff8 | 2012-04-04 15:48:31 -0600 | [diff] [blame] | 92 | } |
| 93 | val_size -= ctx->val_bytes; |
| 94 | val += ctx->val_bytes; |
| 95 | offset += ctx->val_bytes; |
| 96 | } |
| 97 | |
Stephen Warren | 6b8e090 | 2013-11-25 15:12:47 -0700 | [diff] [blame] | 98 | if (!IS_ERR(ctx->clk)) |
Philipp Zabel | 878ec67 | 2013-02-14 17:39:08 +0100 | [diff] [blame] | 99 | clk_disable(ctx->clk); |
| 100 | |
Stephen Warren | 45f5ff8 | 2012-04-04 15:48:31 -0600 | [diff] [blame] | 101 | return 0; |
| 102 | } |
| 103 | |
| 104 | static int regmap_mmio_write(void *context, const void *data, size_t count) |
| 105 | { |
Xiubo Li | 9325804 | 2014-03-27 12:42:43 +0800 | [diff] [blame^] | 106 | struct regmap_mmio_context *ctx = context; |
| 107 | u32 offset = ctx->reg_bytes + ctx->pad_bytes; |
| 108 | |
Xiubo Li | 41b0c2c | 2014-03-27 12:42:42 +0800 | [diff] [blame] | 109 | regmap_mmio_count_check(count); |
Stephen Warren | 40606db | 2012-04-06 15:17:32 -0600 | [diff] [blame] | 110 | |
Xiubo Li | 9325804 | 2014-03-27 12:42:43 +0800 | [diff] [blame^] | 111 | return regmap_mmio_gather_write(context, data, ctx->reg_bytes, |
| 112 | data + offset, count - offset); |
Stephen Warren | 45f5ff8 | 2012-04-04 15:48:31 -0600 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | static int regmap_mmio_read(void *context, |
| 116 | const void *reg, size_t reg_size, |
| 117 | void *val, size_t val_size) |
| 118 | { |
| 119 | struct regmap_mmio_context *ctx = context; |
| 120 | u32 offset; |
Philipp Zabel | 878ec67 | 2013-02-14 17:39:08 +0100 | [diff] [blame] | 121 | int ret; |
Stephen Warren | 45f5ff8 | 2012-04-04 15:48:31 -0600 | [diff] [blame] | 122 | |
Xiubo Li | 41b0c2c | 2014-03-27 12:42:42 +0800 | [diff] [blame] | 123 | regmap_mmio_regsize_check(reg_size); |
Stephen Warren | 40606db | 2012-04-06 15:17:32 -0600 | [diff] [blame] | 124 | |
Stephen Warren | 6b8e090 | 2013-11-25 15:12:47 -0700 | [diff] [blame] | 125 | if (!IS_ERR(ctx->clk)) { |
Philipp Zabel | 878ec67 | 2013-02-14 17:39:08 +0100 | [diff] [blame] | 126 | ret = clk_enable(ctx->clk); |
| 127 | if (ret < 0) |
| 128 | return ret; |
| 129 | } |
| 130 | |
Stephen Warren | 6a55244 | 2012-05-24 10:47:27 -0600 | [diff] [blame] | 131 | offset = *(u32 *)reg; |
Stephen Warren | 45f5ff8 | 2012-04-04 15:48:31 -0600 | [diff] [blame] | 132 | |
| 133 | while (val_size) { |
| 134 | switch (ctx->val_bytes) { |
| 135 | case 1: |
| 136 | *(u8 *)val = readb(ctx->regs + offset); |
| 137 | break; |
| 138 | case 2: |
Stephen Warren | 6a55244 | 2012-05-24 10:47:27 -0600 | [diff] [blame] | 139 | *(u16 *)val = readw(ctx->regs + offset); |
Stephen Warren | 45f5ff8 | 2012-04-04 15:48:31 -0600 | [diff] [blame] | 140 | break; |
| 141 | case 4: |
Stephen Warren | 6a55244 | 2012-05-24 10:47:27 -0600 | [diff] [blame] | 142 | *(u32 *)val = readl(ctx->regs + offset); |
Stephen Warren | 45f5ff8 | 2012-04-04 15:48:31 -0600 | [diff] [blame] | 143 | break; |
| 144 | #ifdef CONFIG_64BIT |
| 145 | case 8: |
Stephen Warren | 6a55244 | 2012-05-24 10:47:27 -0600 | [diff] [blame] | 146 | *(u64 *)val = readq(ctx->regs + offset); |
Stephen Warren | 45f5ff8 | 2012-04-04 15:48:31 -0600 | [diff] [blame] | 147 | break; |
| 148 | #endif |
| 149 | default: |
| 150 | /* Should be caught by regmap_mmio_check_config */ |
Stephen Warren | 40606db | 2012-04-06 15:17:32 -0600 | [diff] [blame] | 151 | BUG(); |
Stephen Warren | 45f5ff8 | 2012-04-04 15:48:31 -0600 | [diff] [blame] | 152 | } |
| 153 | val_size -= ctx->val_bytes; |
| 154 | val += ctx->val_bytes; |
| 155 | offset += ctx->val_bytes; |
| 156 | } |
| 157 | |
Stephen Warren | 6b8e090 | 2013-11-25 15:12:47 -0700 | [diff] [blame] | 158 | if (!IS_ERR(ctx->clk)) |
Philipp Zabel | 878ec67 | 2013-02-14 17:39:08 +0100 | [diff] [blame] | 159 | clk_disable(ctx->clk); |
| 160 | |
Stephen Warren | 45f5ff8 | 2012-04-04 15:48:31 -0600 | [diff] [blame] | 161 | return 0; |
| 162 | } |
| 163 | |
| 164 | static void regmap_mmio_free_context(void *context) |
| 165 | { |
Philipp Zabel | 878ec67 | 2013-02-14 17:39:08 +0100 | [diff] [blame] | 166 | struct regmap_mmio_context *ctx = context; |
| 167 | |
Stephen Warren | 6b8e090 | 2013-11-25 15:12:47 -0700 | [diff] [blame] | 168 | if (!IS_ERR(ctx->clk)) { |
Philipp Zabel | 878ec67 | 2013-02-14 17:39:08 +0100 | [diff] [blame] | 169 | clk_unprepare(ctx->clk); |
| 170 | clk_put(ctx->clk); |
| 171 | } |
Stephen Warren | 45f5ff8 | 2012-04-04 15:48:31 -0600 | [diff] [blame] | 172 | kfree(context); |
| 173 | } |
| 174 | |
| 175 | static struct regmap_bus regmap_mmio = { |
| 176 | .fast_io = true, |
| 177 | .write = regmap_mmio_write, |
| 178 | .gather_write = regmap_mmio_gather_write, |
| 179 | .read = regmap_mmio_read, |
| 180 | .free_context = regmap_mmio_free_context, |
Stephen Warren | 6a55244 | 2012-05-24 10:47:27 -0600 | [diff] [blame] | 181 | .reg_format_endian_default = REGMAP_ENDIAN_NATIVE, |
| 182 | .val_format_endian_default = REGMAP_ENDIAN_NATIVE, |
Stephen Warren | 45f5ff8 | 2012-04-04 15:48:31 -0600 | [diff] [blame] | 183 | }; |
| 184 | |
Philipp Zabel | 878ec67 | 2013-02-14 17:39:08 +0100 | [diff] [blame] | 185 | static struct regmap_mmio_context *regmap_mmio_gen_context(struct device *dev, |
| 186 | const char *clk_id, |
| 187 | void __iomem *regs, |
Stephen Warren | 45f5ff8 | 2012-04-04 15:48:31 -0600 | [diff] [blame] | 188 | const struct regmap_config *config) |
| 189 | { |
| 190 | struct regmap_mmio_context *ctx; |
Stephen Warren | f01ee60 | 2012-04-09 13:40:24 -0600 | [diff] [blame] | 191 | int min_stride; |
Philipp Zabel | 878ec67 | 2013-02-14 17:39:08 +0100 | [diff] [blame] | 192 | int ret; |
Stephen Warren | 45f5ff8 | 2012-04-04 15:48:31 -0600 | [diff] [blame] | 193 | |
| 194 | if (config->reg_bits != 32) |
| 195 | return ERR_PTR(-EINVAL); |
| 196 | |
| 197 | if (config->pad_bits) |
| 198 | return ERR_PTR(-EINVAL); |
| 199 | |
| 200 | switch (config->val_bits) { |
| 201 | case 8: |
Stephen Warren | f01ee60 | 2012-04-09 13:40:24 -0600 | [diff] [blame] | 202 | /* The core treats 0 as 1 */ |
| 203 | min_stride = 0; |
| 204 | break; |
Stephen Warren | 45f5ff8 | 2012-04-04 15:48:31 -0600 | [diff] [blame] | 205 | case 16: |
Stephen Warren | f01ee60 | 2012-04-09 13:40:24 -0600 | [diff] [blame] | 206 | min_stride = 2; |
| 207 | break; |
Stephen Warren | 45f5ff8 | 2012-04-04 15:48:31 -0600 | [diff] [blame] | 208 | case 32: |
Stephen Warren | f01ee60 | 2012-04-09 13:40:24 -0600 | [diff] [blame] | 209 | min_stride = 4; |
| 210 | break; |
Stephen Warren | 45f5ff8 | 2012-04-04 15:48:31 -0600 | [diff] [blame] | 211 | #ifdef CONFIG_64BIT |
| 212 | case 64: |
Stephen Warren | f01ee60 | 2012-04-09 13:40:24 -0600 | [diff] [blame] | 213 | min_stride = 8; |
| 214 | break; |
Stephen Warren | 45f5ff8 | 2012-04-04 15:48:31 -0600 | [diff] [blame] | 215 | #endif |
| 216 | break; |
| 217 | default: |
| 218 | return ERR_PTR(-EINVAL); |
| 219 | } |
| 220 | |
Stephen Warren | f01ee60 | 2012-04-09 13:40:24 -0600 | [diff] [blame] | 221 | if (config->reg_stride < min_stride) |
| 222 | return ERR_PTR(-EINVAL); |
| 223 | |
Stephen Warren | 6a55244 | 2012-05-24 10:47:27 -0600 | [diff] [blame] | 224 | switch (config->reg_format_endian) { |
| 225 | case REGMAP_ENDIAN_DEFAULT: |
| 226 | case REGMAP_ENDIAN_NATIVE: |
| 227 | break; |
| 228 | default: |
| 229 | return ERR_PTR(-EINVAL); |
| 230 | } |
| 231 | |
Dimitris Papastamos | 4633511 | 2012-07-18 14:17:23 +0100 | [diff] [blame] | 232 | ctx = kzalloc(sizeof(*ctx), GFP_KERNEL); |
Stephen Warren | 45f5ff8 | 2012-04-04 15:48:31 -0600 | [diff] [blame] | 233 | if (!ctx) |
| 234 | return ERR_PTR(-ENOMEM); |
| 235 | |
| 236 | ctx->regs = regs; |
| 237 | ctx->val_bytes = config->val_bits / 8; |
Xiubo Li | 9325804 | 2014-03-27 12:42:43 +0800 | [diff] [blame^] | 238 | ctx->reg_bytes = config->reg_bits / 8; |
| 239 | ctx->pad_bytes = config->pad_bits / 8; |
Stephen Warren | 6b8e090 | 2013-11-25 15:12:47 -0700 | [diff] [blame] | 240 | ctx->clk = ERR_PTR(-ENODEV); |
Stephen Warren | 45f5ff8 | 2012-04-04 15:48:31 -0600 | [diff] [blame] | 241 | |
Philipp Zabel | 878ec67 | 2013-02-14 17:39:08 +0100 | [diff] [blame] | 242 | if (clk_id == NULL) |
| 243 | return ctx; |
| 244 | |
| 245 | ctx->clk = clk_get(dev, clk_id); |
| 246 | if (IS_ERR(ctx->clk)) { |
| 247 | ret = PTR_ERR(ctx->clk); |
| 248 | goto err_free; |
| 249 | } |
| 250 | |
| 251 | ret = clk_prepare(ctx->clk); |
| 252 | if (ret < 0) { |
| 253 | clk_put(ctx->clk); |
| 254 | goto err_free; |
| 255 | } |
| 256 | |
Stephen Warren | 45f5ff8 | 2012-04-04 15:48:31 -0600 | [diff] [blame] | 257 | return ctx; |
Philipp Zabel | 878ec67 | 2013-02-14 17:39:08 +0100 | [diff] [blame] | 258 | |
| 259 | err_free: |
| 260 | kfree(ctx); |
| 261 | |
| 262 | return ERR_PTR(ret); |
Stephen Warren | 45f5ff8 | 2012-04-04 15:48:31 -0600 | [diff] [blame] | 263 | } |
| 264 | |
| 265 | /** |
Philipp Zabel | 878ec67 | 2013-02-14 17:39:08 +0100 | [diff] [blame] | 266 | * regmap_init_mmio_clk(): Initialise register map with register clock |
Stephen Warren | 45f5ff8 | 2012-04-04 15:48:31 -0600 | [diff] [blame] | 267 | * |
| 268 | * @dev: Device that will be interacted with |
Philipp Zabel | 878ec67 | 2013-02-14 17:39:08 +0100 | [diff] [blame] | 269 | * @clk_id: register clock consumer ID |
Stephen Warren | 45f5ff8 | 2012-04-04 15:48:31 -0600 | [diff] [blame] | 270 | * @regs: Pointer to memory-mapped IO region |
| 271 | * @config: Configuration for register map |
| 272 | * |
| 273 | * The return value will be an ERR_PTR() on error or a valid pointer to |
| 274 | * a struct regmap. |
| 275 | */ |
Philipp Zabel | 878ec67 | 2013-02-14 17:39:08 +0100 | [diff] [blame] | 276 | struct regmap *regmap_init_mmio_clk(struct device *dev, const char *clk_id, |
| 277 | void __iomem *regs, |
| 278 | const struct regmap_config *config) |
Stephen Warren | 45f5ff8 | 2012-04-04 15:48:31 -0600 | [diff] [blame] | 279 | { |
| 280 | struct regmap_mmio_context *ctx; |
| 281 | |
Philipp Zabel | 878ec67 | 2013-02-14 17:39:08 +0100 | [diff] [blame] | 282 | ctx = regmap_mmio_gen_context(dev, clk_id, regs, config); |
Stephen Warren | 45f5ff8 | 2012-04-04 15:48:31 -0600 | [diff] [blame] | 283 | if (IS_ERR(ctx)) |
| 284 | return ERR_CAST(ctx); |
| 285 | |
| 286 | return regmap_init(dev, ®map_mmio, ctx, config); |
| 287 | } |
Philipp Zabel | 878ec67 | 2013-02-14 17:39:08 +0100 | [diff] [blame] | 288 | EXPORT_SYMBOL_GPL(regmap_init_mmio_clk); |
Stephen Warren | 45f5ff8 | 2012-04-04 15:48:31 -0600 | [diff] [blame] | 289 | |
| 290 | /** |
Philipp Zabel | 878ec67 | 2013-02-14 17:39:08 +0100 | [diff] [blame] | 291 | * devm_regmap_init_mmio_clk(): Initialise managed register map with clock |
Stephen Warren | 45f5ff8 | 2012-04-04 15:48:31 -0600 | [diff] [blame] | 292 | * |
| 293 | * @dev: Device that will be interacted with |
Philipp Zabel | 878ec67 | 2013-02-14 17:39:08 +0100 | [diff] [blame] | 294 | * @clk_id: register clock consumer ID |
Stephen Warren | 45f5ff8 | 2012-04-04 15:48:31 -0600 | [diff] [blame] | 295 | * @regs: Pointer to memory-mapped IO region |
| 296 | * @config: Configuration for register map |
| 297 | * |
| 298 | * The return value will be an ERR_PTR() on error or a valid pointer |
| 299 | * to a struct regmap. The regmap will be automatically freed by the |
| 300 | * device management code. |
| 301 | */ |
Philipp Zabel | 878ec67 | 2013-02-14 17:39:08 +0100 | [diff] [blame] | 302 | struct regmap *devm_regmap_init_mmio_clk(struct device *dev, const char *clk_id, |
| 303 | void __iomem *regs, |
| 304 | const struct regmap_config *config) |
Stephen Warren | 45f5ff8 | 2012-04-04 15:48:31 -0600 | [diff] [blame] | 305 | { |
| 306 | struct regmap_mmio_context *ctx; |
| 307 | |
Philipp Zabel | 878ec67 | 2013-02-14 17:39:08 +0100 | [diff] [blame] | 308 | ctx = regmap_mmio_gen_context(dev, clk_id, regs, config); |
Stephen Warren | 45f5ff8 | 2012-04-04 15:48:31 -0600 | [diff] [blame] | 309 | if (IS_ERR(ctx)) |
| 310 | return ERR_CAST(ctx); |
| 311 | |
| 312 | return devm_regmap_init(dev, ®map_mmio, ctx, config); |
| 313 | } |
Philipp Zabel | 878ec67 | 2013-02-14 17:39:08 +0100 | [diff] [blame] | 314 | EXPORT_SYMBOL_GPL(devm_regmap_init_mmio_clk); |
Stephen Warren | 45f5ff8 | 2012-04-04 15:48:31 -0600 | [diff] [blame] | 315 | |
| 316 | MODULE_LICENSE("GPL v2"); |