blob: de45a1e1548fa31894c2afd3af1b658869939afe [file] [log] [blame]
Stephen Warren45f5ff82012-04-04 15:48:31 -06001/*
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 Zabel878ec672013-02-14 17:39:08 +010019#include <linux/clk.h>
Stephen Warren45f5ff82012-04-04 15:48:31 -060020#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
27struct regmap_mmio_context {
28 void __iomem *regs;
Xiubo Li93258042014-03-27 12:42:43 +080029 unsigned reg_bytes;
Stephen Warren45f5ff82012-04-04 15:48:31 -060030 unsigned val_bytes;
Xiubo Li93258042014-03-27 12:42:43 +080031 unsigned pad_bytes;
Philipp Zabel878ec672013-02-14 17:39:08 +010032 struct clk *clk;
Stephen Warren45f5ff82012-04-04 15:48:31 -060033};
34
Xiubo Li41b0c2c2014-03-27 12:42:42 +080035static inline void regmap_mmio_regsize_check(size_t reg_size)
36{
Xiubo Li93258042014-03-27 12:42:43 +080037 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 Li41b0c2c2014-03-27 12:42:42 +080048}
49
Xiubo Li451485b2014-03-28 13:12:56 +080050static int regmap_mmio_regbits_check(size_t reg_bits)
51{
52 switch (reg_bits) {
53 case 8:
54 case 16:
55 case 32:
56#ifdef CONFIG_64BIT
57 case 64:
58#endif
59 return 0;
60 default:
61 return -EINVAL;
62 }
63}
64
Xiubo Li41b0c2c2014-03-27 12:42:42 +080065static inline void regmap_mmio_count_check(size_t count)
66{
Xiubo Li93258042014-03-27 12:42:43 +080067 BUG_ON(count % 2 != 0);
Xiubo Li41b0c2c2014-03-27 12:42:42 +080068}
69
Stephen Warren45f5ff82012-04-04 15:48:31 -060070static int regmap_mmio_gather_write(void *context,
71 const void *reg, size_t reg_size,
72 const void *val, size_t val_size)
73{
74 struct regmap_mmio_context *ctx = context;
75 u32 offset;
Philipp Zabel878ec672013-02-14 17:39:08 +010076 int ret;
Stephen Warren45f5ff82012-04-04 15:48:31 -060077
Xiubo Li41b0c2c2014-03-27 12:42:42 +080078 regmap_mmio_regsize_check(reg_size);
Stephen Warren40606db2012-04-06 15:17:32 -060079
Stephen Warren6b8e0902013-11-25 15:12:47 -070080 if (!IS_ERR(ctx->clk)) {
Philipp Zabel878ec672013-02-14 17:39:08 +010081 ret = clk_enable(ctx->clk);
82 if (ret < 0)
83 return ret;
84 }
85
Stephen Warren6a552442012-05-24 10:47:27 -060086 offset = *(u32 *)reg;
Stephen Warren45f5ff82012-04-04 15:48:31 -060087
88 while (val_size) {
89 switch (ctx->val_bytes) {
90 case 1:
91 writeb(*(u8 *)val, ctx->regs + offset);
92 break;
93 case 2:
Stephen Warren6a552442012-05-24 10:47:27 -060094 writew(*(u16 *)val, ctx->regs + offset);
Stephen Warren45f5ff82012-04-04 15:48:31 -060095 break;
96 case 4:
Stephen Warren6a552442012-05-24 10:47:27 -060097 writel(*(u32 *)val, ctx->regs + offset);
Stephen Warren45f5ff82012-04-04 15:48:31 -060098 break;
99#ifdef CONFIG_64BIT
100 case 8:
Stephen Warren6a552442012-05-24 10:47:27 -0600101 writeq(*(u64 *)val, ctx->regs + offset);
Stephen Warren45f5ff82012-04-04 15:48:31 -0600102 break;
103#endif
104 default:
105 /* Should be caught by regmap_mmio_check_config */
Stephen Warren40606db2012-04-06 15:17:32 -0600106 BUG();
Stephen Warren45f5ff82012-04-04 15:48:31 -0600107 }
108 val_size -= ctx->val_bytes;
109 val += ctx->val_bytes;
110 offset += ctx->val_bytes;
111 }
112
Stephen Warren6b8e0902013-11-25 15:12:47 -0700113 if (!IS_ERR(ctx->clk))
Philipp Zabel878ec672013-02-14 17:39:08 +0100114 clk_disable(ctx->clk);
115
Stephen Warren45f5ff82012-04-04 15:48:31 -0600116 return 0;
117}
118
119static int regmap_mmio_write(void *context, const void *data, size_t count)
120{
Xiubo Li93258042014-03-27 12:42:43 +0800121 struct regmap_mmio_context *ctx = context;
122 u32 offset = ctx->reg_bytes + ctx->pad_bytes;
123
Xiubo Li41b0c2c2014-03-27 12:42:42 +0800124 regmap_mmio_count_check(count);
Stephen Warren40606db2012-04-06 15:17:32 -0600125
Xiubo Li93258042014-03-27 12:42:43 +0800126 return regmap_mmio_gather_write(context, data, ctx->reg_bytes,
127 data + offset, count - offset);
Stephen Warren45f5ff82012-04-04 15:48:31 -0600128}
129
130static int regmap_mmio_read(void *context,
131 const void *reg, size_t reg_size,
132 void *val, size_t val_size)
133{
134 struct regmap_mmio_context *ctx = context;
135 u32 offset;
Philipp Zabel878ec672013-02-14 17:39:08 +0100136 int ret;
Stephen Warren45f5ff82012-04-04 15:48:31 -0600137
Xiubo Li41b0c2c2014-03-27 12:42:42 +0800138 regmap_mmio_regsize_check(reg_size);
Stephen Warren40606db2012-04-06 15:17:32 -0600139
Stephen Warren6b8e0902013-11-25 15:12:47 -0700140 if (!IS_ERR(ctx->clk)) {
Philipp Zabel878ec672013-02-14 17:39:08 +0100141 ret = clk_enable(ctx->clk);
142 if (ret < 0)
143 return ret;
144 }
145
Stephen Warren6a552442012-05-24 10:47:27 -0600146 offset = *(u32 *)reg;
Stephen Warren45f5ff82012-04-04 15:48:31 -0600147
148 while (val_size) {
149 switch (ctx->val_bytes) {
150 case 1:
151 *(u8 *)val = readb(ctx->regs + offset);
152 break;
153 case 2:
Stephen Warren6a552442012-05-24 10:47:27 -0600154 *(u16 *)val = readw(ctx->regs + offset);
Stephen Warren45f5ff82012-04-04 15:48:31 -0600155 break;
156 case 4:
Stephen Warren6a552442012-05-24 10:47:27 -0600157 *(u32 *)val = readl(ctx->regs + offset);
Stephen Warren45f5ff82012-04-04 15:48:31 -0600158 break;
159#ifdef CONFIG_64BIT
160 case 8:
Stephen Warren6a552442012-05-24 10:47:27 -0600161 *(u64 *)val = readq(ctx->regs + offset);
Stephen Warren45f5ff82012-04-04 15:48:31 -0600162 break;
163#endif
164 default:
165 /* Should be caught by regmap_mmio_check_config */
Stephen Warren40606db2012-04-06 15:17:32 -0600166 BUG();
Stephen Warren45f5ff82012-04-04 15:48:31 -0600167 }
168 val_size -= ctx->val_bytes;
169 val += ctx->val_bytes;
170 offset += ctx->val_bytes;
171 }
172
Stephen Warren6b8e0902013-11-25 15:12:47 -0700173 if (!IS_ERR(ctx->clk))
Philipp Zabel878ec672013-02-14 17:39:08 +0100174 clk_disable(ctx->clk);
175
Stephen Warren45f5ff82012-04-04 15:48:31 -0600176 return 0;
177}
178
179static void regmap_mmio_free_context(void *context)
180{
Philipp Zabel878ec672013-02-14 17:39:08 +0100181 struct regmap_mmio_context *ctx = context;
182
Stephen Warren6b8e0902013-11-25 15:12:47 -0700183 if (!IS_ERR(ctx->clk)) {
Philipp Zabel878ec672013-02-14 17:39:08 +0100184 clk_unprepare(ctx->clk);
185 clk_put(ctx->clk);
186 }
Stephen Warren45f5ff82012-04-04 15:48:31 -0600187 kfree(context);
188}
189
190static struct regmap_bus regmap_mmio = {
191 .fast_io = true,
192 .write = regmap_mmio_write,
193 .gather_write = regmap_mmio_gather_write,
194 .read = regmap_mmio_read,
195 .free_context = regmap_mmio_free_context,
Stephen Warren6a552442012-05-24 10:47:27 -0600196 .reg_format_endian_default = REGMAP_ENDIAN_NATIVE,
197 .val_format_endian_default = REGMAP_ENDIAN_NATIVE,
Stephen Warren45f5ff82012-04-04 15:48:31 -0600198};
199
Philipp Zabel878ec672013-02-14 17:39:08 +0100200static struct regmap_mmio_context *regmap_mmio_gen_context(struct device *dev,
201 const char *clk_id,
202 void __iomem *regs,
Stephen Warren45f5ff82012-04-04 15:48:31 -0600203 const struct regmap_config *config)
204{
205 struct regmap_mmio_context *ctx;
Stephen Warrenf01ee602012-04-09 13:40:24 -0600206 int min_stride;
Philipp Zabel878ec672013-02-14 17:39:08 +0100207 int ret;
Stephen Warren45f5ff82012-04-04 15:48:31 -0600208
Xiubo Li451485b2014-03-28 13:12:56 +0800209 ret = regmap_mmio_regbits_check(config->reg_bits);
210 if (ret)
211 return ERR_PTR(ret);
Stephen Warren45f5ff82012-04-04 15:48:31 -0600212
213 if (config->pad_bits)
214 return ERR_PTR(-EINVAL);
215
216 switch (config->val_bits) {
217 case 8:
Stephen Warrenf01ee602012-04-09 13:40:24 -0600218 /* The core treats 0 as 1 */
219 min_stride = 0;
220 break;
Stephen Warren45f5ff82012-04-04 15:48:31 -0600221 case 16:
Stephen Warrenf01ee602012-04-09 13:40:24 -0600222 min_stride = 2;
223 break;
Stephen Warren45f5ff82012-04-04 15:48:31 -0600224 case 32:
Stephen Warrenf01ee602012-04-09 13:40:24 -0600225 min_stride = 4;
226 break;
Stephen Warren45f5ff82012-04-04 15:48:31 -0600227#ifdef CONFIG_64BIT
228 case 64:
Stephen Warrenf01ee602012-04-09 13:40:24 -0600229 min_stride = 8;
230 break;
Stephen Warren45f5ff82012-04-04 15:48:31 -0600231#endif
232 break;
233 default:
234 return ERR_PTR(-EINVAL);
235 }
236
Stephen Warrenf01ee602012-04-09 13:40:24 -0600237 if (config->reg_stride < min_stride)
238 return ERR_PTR(-EINVAL);
239
Stephen Warren6a552442012-05-24 10:47:27 -0600240 switch (config->reg_format_endian) {
241 case REGMAP_ENDIAN_DEFAULT:
242 case REGMAP_ENDIAN_NATIVE:
243 break;
244 default:
245 return ERR_PTR(-EINVAL);
246 }
247
Dimitris Papastamos46335112012-07-18 14:17:23 +0100248 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
Stephen Warren45f5ff82012-04-04 15:48:31 -0600249 if (!ctx)
250 return ERR_PTR(-ENOMEM);
251
252 ctx->regs = regs;
253 ctx->val_bytes = config->val_bits / 8;
Xiubo Li93258042014-03-27 12:42:43 +0800254 ctx->reg_bytes = config->reg_bits / 8;
255 ctx->pad_bytes = config->pad_bits / 8;
Stephen Warren6b8e0902013-11-25 15:12:47 -0700256 ctx->clk = ERR_PTR(-ENODEV);
Stephen Warren45f5ff82012-04-04 15:48:31 -0600257
Philipp Zabel878ec672013-02-14 17:39:08 +0100258 if (clk_id == NULL)
259 return ctx;
260
261 ctx->clk = clk_get(dev, clk_id);
262 if (IS_ERR(ctx->clk)) {
263 ret = PTR_ERR(ctx->clk);
264 goto err_free;
265 }
266
267 ret = clk_prepare(ctx->clk);
268 if (ret < 0) {
269 clk_put(ctx->clk);
270 goto err_free;
271 }
272
Stephen Warren45f5ff82012-04-04 15:48:31 -0600273 return ctx;
Philipp Zabel878ec672013-02-14 17:39:08 +0100274
275err_free:
276 kfree(ctx);
277
278 return ERR_PTR(ret);
Stephen Warren45f5ff82012-04-04 15:48:31 -0600279}
280
281/**
Philipp Zabel878ec672013-02-14 17:39:08 +0100282 * regmap_init_mmio_clk(): Initialise register map with register clock
Stephen Warren45f5ff82012-04-04 15:48:31 -0600283 *
284 * @dev: Device that will be interacted with
Philipp Zabel878ec672013-02-14 17:39:08 +0100285 * @clk_id: register clock consumer ID
Stephen Warren45f5ff82012-04-04 15:48:31 -0600286 * @regs: Pointer to memory-mapped IO region
287 * @config: Configuration for register map
288 *
289 * The return value will be an ERR_PTR() on error or a valid pointer to
290 * a struct regmap.
291 */
Philipp Zabel878ec672013-02-14 17:39:08 +0100292struct regmap *regmap_init_mmio_clk(struct device *dev, const char *clk_id,
293 void __iomem *regs,
294 const struct regmap_config *config)
Stephen Warren45f5ff82012-04-04 15:48:31 -0600295{
296 struct regmap_mmio_context *ctx;
297
Philipp Zabel878ec672013-02-14 17:39:08 +0100298 ctx = regmap_mmio_gen_context(dev, clk_id, regs, config);
Stephen Warren45f5ff82012-04-04 15:48:31 -0600299 if (IS_ERR(ctx))
300 return ERR_CAST(ctx);
301
302 return regmap_init(dev, &regmap_mmio, ctx, config);
303}
Philipp Zabel878ec672013-02-14 17:39:08 +0100304EXPORT_SYMBOL_GPL(regmap_init_mmio_clk);
Stephen Warren45f5ff82012-04-04 15:48:31 -0600305
306/**
Philipp Zabel878ec672013-02-14 17:39:08 +0100307 * devm_regmap_init_mmio_clk(): Initialise managed register map with clock
Stephen Warren45f5ff82012-04-04 15:48:31 -0600308 *
309 * @dev: Device that will be interacted with
Philipp Zabel878ec672013-02-14 17:39:08 +0100310 * @clk_id: register clock consumer ID
Stephen Warren45f5ff82012-04-04 15:48:31 -0600311 * @regs: Pointer to memory-mapped IO region
312 * @config: Configuration for register map
313 *
314 * The return value will be an ERR_PTR() on error or a valid pointer
315 * to a struct regmap. The regmap will be automatically freed by the
316 * device management code.
317 */
Philipp Zabel878ec672013-02-14 17:39:08 +0100318struct regmap *devm_regmap_init_mmio_clk(struct device *dev, const char *clk_id,
319 void __iomem *regs,
320 const struct regmap_config *config)
Stephen Warren45f5ff82012-04-04 15:48:31 -0600321{
322 struct regmap_mmio_context *ctx;
323
Philipp Zabel878ec672013-02-14 17:39:08 +0100324 ctx = regmap_mmio_gen_context(dev, clk_id, regs, config);
Stephen Warren45f5ff82012-04-04 15:48:31 -0600325 if (IS_ERR(ctx))
326 return ERR_CAST(ctx);
327
328 return devm_regmap_init(dev, &regmap_mmio, ctx, config);
329}
Philipp Zabel878ec672013-02-14 17:39:08 +0100330EXPORT_SYMBOL_GPL(devm_regmap_init_mmio_clk);
Stephen Warren45f5ff82012-04-04 15:48:31 -0600331
332MODULE_LICENSE("GPL v2");