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