blob: 8812bfb9e3b89256f4a5d1468cf45484b18e4cd1 [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>
Stephen Warren45f5ff82012-04-04 15:48:31 -060021#include <linux/io.h>
22#include <linux/module.h>
23#include <linux/regmap.h>
24#include <linux/slab.h>
25
26struct regmap_mmio_context {
27 void __iomem *regs;
Xiubo Li93258042014-03-27 12:42:43 +080028 unsigned reg_bytes;
Stephen Warren45f5ff82012-04-04 15:48:31 -060029 unsigned val_bytes;
Xiubo Li93258042014-03-27 12:42:43 +080030 unsigned pad_bytes;
Philipp Zabel878ec672013-02-14 17:39:08 +010031 struct clk *clk;
Stephen Warren45f5ff82012-04-04 15:48:31 -060032};
33
Xiubo Li41b0c2c2014-03-27 12:42:42 +080034static inline void regmap_mmio_regsize_check(size_t reg_size)
35{
Xiubo Li93258042014-03-27 12:42:43 +080036 switch (reg_size) {
37 case 1:
38 case 2:
39 case 4:
40#ifdef CONFIG_64BIT
41 case 8:
42#endif
43 break;
44 default:
45 BUG();
46 }
Xiubo Li41b0c2c2014-03-27 12:42:42 +080047}
48
Xiubo Li451485b2014-03-28 13:12:56 +080049static int regmap_mmio_regbits_check(size_t reg_bits)
50{
51 switch (reg_bits) {
52 case 8:
53 case 16:
54 case 32:
55#ifdef CONFIG_64BIT
56 case 64:
57#endif
58 return 0;
59 default:
60 return -EINVAL;
61 }
62}
63
Xiubo Li75fb0aa2015-12-03 13:27:21 +080064static int regmap_mmio_get_min_stride(size_t val_bits)
65{
66 int min_stride;
67
68 switch (val_bits) {
69 case 8:
70 /* The core treats 0 as 1 */
71 min_stride = 0;
72 return 0;
73 case 16:
74 min_stride = 2;
75 break;
76 case 32:
77 min_stride = 4;
78 break;
79#ifdef CONFIG_64BIT
80 case 64:
81 min_stride = 8;
82 break;
83#endif
84 default:
85 return -EINVAL;
86 }
87
88 return min_stride;
89}
90
Philipp Zabel2e804b72014-05-16 16:25:34 +020091static inline void regmap_mmio_count_check(size_t count, u32 offset)
Xiubo Li41b0c2c2014-03-27 12:42:42 +080092{
Philipp Zabel2e804b72014-05-16 16:25:34 +020093 BUG_ON(count <= offset);
Xiubo Li41b0c2c2014-03-27 12:42:42 +080094}
95
Xiubo Li88cb32c2014-04-02 10:20:17 +080096static inline unsigned int
97regmap_mmio_get_offset(const void *reg, size_t reg_size)
98{
99 switch (reg_size) {
100 case 1:
101 return *(u8 *)reg;
102 case 2:
103 return *(u16 *)reg;
104 case 4:
105 return *(u32 *)reg;
106#ifdef CONFIG_64BIT
107 case 8:
108 return *(u64 *)reg;
109#endif
110 default:
111 BUG();
112 }
Stephen Warren45f5ff82012-04-04 15:48:31 -0600113}
114
115static int regmap_mmio_gather_write(void *context,
116 const void *reg, size_t reg_size,
117 const void *val, size_t val_size)
118{
119 struct regmap_mmio_context *ctx = context;
Xiubo Li88cb32c2014-04-02 10:20:17 +0800120 unsigned int offset;
Philipp Zabel878ec672013-02-14 17:39:08 +0100121 int ret;
Stephen Warren45f5ff82012-04-04 15:48:31 -0600122
Xiubo Li41b0c2c2014-03-27 12:42:42 +0800123 regmap_mmio_regsize_check(reg_size);
Stephen Warren40606db2012-04-06 15:17:32 -0600124
Stephen Warren6b8e0902013-11-25 15:12:47 -0700125 if (!IS_ERR(ctx->clk)) {
Philipp Zabel878ec672013-02-14 17:39:08 +0100126 ret = clk_enable(ctx->clk);
127 if (ret < 0)
128 return ret;
129 }
130
Xiubo Li88cb32c2014-04-02 10:20:17 +0800131 offset = regmap_mmio_get_offset(reg, reg_size);
Stephen Warren45f5ff82012-04-04 15:48:31 -0600132
133 while (val_size) {
134 switch (ctx->val_bytes) {
135 case 1:
Simon Arlott29bb45f2015-10-29 19:58:47 +0000136 __raw_writeb(*(u8 *)val, ctx->regs + offset);
Stephen Warren45f5ff82012-04-04 15:48:31 -0600137 break;
138 case 2:
Simon Arlott29bb45f2015-10-29 19:58:47 +0000139 __raw_writew(*(u16 *)val, ctx->regs + offset);
Stephen Warren45f5ff82012-04-04 15:48:31 -0600140 break;
141 case 4:
Simon Arlott29bb45f2015-10-29 19:58:47 +0000142 __raw_writel(*(u32 *)val, ctx->regs + offset);
Stephen Warren45f5ff82012-04-04 15:48:31 -0600143 break;
144#ifdef CONFIG_64BIT
145 case 8:
Simon Arlott29bb45f2015-10-29 19:58:47 +0000146 __raw_writeq(*(u64 *)val, ctx->regs + offset);
Stephen Warren45f5ff82012-04-04 15:48:31 -0600147 break;
148#endif
149 default:
150 /* Should be caught by regmap_mmio_check_config */
Stephen Warren40606db2012-04-06 15:17:32 -0600151 BUG();
Stephen Warren45f5ff82012-04-04 15:48:31 -0600152 }
153 val_size -= ctx->val_bytes;
154 val += ctx->val_bytes;
155 offset += ctx->val_bytes;
156 }
157
Stephen Warren6b8e0902013-11-25 15:12:47 -0700158 if (!IS_ERR(ctx->clk))
Philipp Zabel878ec672013-02-14 17:39:08 +0100159 clk_disable(ctx->clk);
160
Stephen Warren45f5ff82012-04-04 15:48:31 -0600161 return 0;
162}
163
164static int regmap_mmio_write(void *context, const void *data, size_t count)
165{
Xiubo Li93258042014-03-27 12:42:43 +0800166 struct regmap_mmio_context *ctx = context;
Xiubo Li88cb32c2014-04-02 10:20:17 +0800167 unsigned int offset = ctx->reg_bytes + ctx->pad_bytes;
Xiubo Li93258042014-03-27 12:42:43 +0800168
Philipp Zabel2e804b72014-05-16 16:25:34 +0200169 regmap_mmio_count_check(count, offset);
Stephen Warren40606db2012-04-06 15:17:32 -0600170
Xiubo Li93258042014-03-27 12:42:43 +0800171 return regmap_mmio_gather_write(context, data, ctx->reg_bytes,
172 data + offset, count - offset);
Stephen Warren45f5ff82012-04-04 15:48:31 -0600173}
174
175static int regmap_mmio_read(void *context,
176 const void *reg, size_t reg_size,
177 void *val, size_t val_size)
178{
179 struct regmap_mmio_context *ctx = context;
Xiubo Li88cb32c2014-04-02 10:20:17 +0800180 unsigned int offset;
Philipp Zabel878ec672013-02-14 17:39:08 +0100181 int ret;
Stephen Warren45f5ff82012-04-04 15:48:31 -0600182
Xiubo Li41b0c2c2014-03-27 12:42:42 +0800183 regmap_mmio_regsize_check(reg_size);
Stephen Warren40606db2012-04-06 15:17:32 -0600184
Stephen Warren6b8e0902013-11-25 15:12:47 -0700185 if (!IS_ERR(ctx->clk)) {
Philipp Zabel878ec672013-02-14 17:39:08 +0100186 ret = clk_enable(ctx->clk);
187 if (ret < 0)
188 return ret;
189 }
190
Xiubo Li88cb32c2014-04-02 10:20:17 +0800191 offset = regmap_mmio_get_offset(reg, reg_size);
Stephen Warren45f5ff82012-04-04 15:48:31 -0600192
193 while (val_size) {
194 switch (ctx->val_bytes) {
195 case 1:
Simon Arlott29bb45f2015-10-29 19:58:47 +0000196 *(u8 *)val = __raw_readb(ctx->regs + offset);
Stephen Warren45f5ff82012-04-04 15:48:31 -0600197 break;
198 case 2:
Simon Arlott29bb45f2015-10-29 19:58:47 +0000199 *(u16 *)val = __raw_readw(ctx->regs + offset);
Stephen Warren45f5ff82012-04-04 15:48:31 -0600200 break;
201 case 4:
Simon Arlott29bb45f2015-10-29 19:58:47 +0000202 *(u32 *)val = __raw_readl(ctx->regs + offset);
Stephen Warren45f5ff82012-04-04 15:48:31 -0600203 break;
204#ifdef CONFIG_64BIT
205 case 8:
Simon Arlott29bb45f2015-10-29 19:58:47 +0000206 *(u64 *)val = __raw_readq(ctx->regs + offset);
Stephen Warren45f5ff82012-04-04 15:48:31 -0600207 break;
208#endif
209 default:
210 /* Should be caught by regmap_mmio_check_config */
Stephen Warren40606db2012-04-06 15:17:32 -0600211 BUG();
Stephen Warren45f5ff82012-04-04 15:48:31 -0600212 }
213 val_size -= ctx->val_bytes;
214 val += ctx->val_bytes;
215 offset += ctx->val_bytes;
216 }
217
Stephen Warren6b8e0902013-11-25 15:12:47 -0700218 if (!IS_ERR(ctx->clk))
Philipp Zabel878ec672013-02-14 17:39:08 +0100219 clk_disable(ctx->clk);
220
Stephen Warren45f5ff82012-04-04 15:48:31 -0600221 return 0;
222}
223
224static void regmap_mmio_free_context(void *context)
225{
Philipp Zabel878ec672013-02-14 17:39:08 +0100226 struct regmap_mmio_context *ctx = context;
227
Stephen Warren6b8e0902013-11-25 15:12:47 -0700228 if (!IS_ERR(ctx->clk)) {
Philipp Zabel878ec672013-02-14 17:39:08 +0100229 clk_unprepare(ctx->clk);
230 clk_put(ctx->clk);
231 }
Stephen Warren45f5ff82012-04-04 15:48:31 -0600232 kfree(context);
233}
234
235static struct regmap_bus regmap_mmio = {
236 .fast_io = true,
237 .write = regmap_mmio_write,
238 .gather_write = regmap_mmio_gather_write,
239 .read = regmap_mmio_read,
240 .free_context = regmap_mmio_free_context,
Stephen Warren6a552442012-05-24 10:47:27 -0600241 .reg_format_endian_default = REGMAP_ENDIAN_NATIVE,
242 .val_format_endian_default = REGMAP_ENDIAN_NATIVE,
Stephen Warren45f5ff82012-04-04 15:48:31 -0600243};
244
Philipp Zabel878ec672013-02-14 17:39:08 +0100245static struct regmap_mmio_context *regmap_mmio_gen_context(struct device *dev,
246 const char *clk_id,
247 void __iomem *regs,
Stephen Warren45f5ff82012-04-04 15:48:31 -0600248 const struct regmap_config *config)
249{
250 struct regmap_mmio_context *ctx;
Stephen Warrenf01ee602012-04-09 13:40:24 -0600251 int min_stride;
Philipp Zabel878ec672013-02-14 17:39:08 +0100252 int ret;
Stephen Warren45f5ff82012-04-04 15:48:31 -0600253
Xiubo Li451485b2014-03-28 13:12:56 +0800254 ret = regmap_mmio_regbits_check(config->reg_bits);
255 if (ret)
256 return ERR_PTR(ret);
Stephen Warren45f5ff82012-04-04 15:48:31 -0600257
258 if (config->pad_bits)
259 return ERR_PTR(-EINVAL);
260
Xiubo Li75fb0aa2015-12-03 13:27:21 +0800261 min_stride = regmap_mmio_get_min_stride(config->val_bits);
262 if (min_stride < 0)
263 return ERR_PTR(min_stride);
Stephen Warren45f5ff82012-04-04 15:48:31 -0600264
Stephen Warrenf01ee602012-04-09 13:40:24 -0600265 if (config->reg_stride < min_stride)
266 return ERR_PTR(-EINVAL);
267
Stephen Warren6a552442012-05-24 10:47:27 -0600268 switch (config->reg_format_endian) {
269 case REGMAP_ENDIAN_DEFAULT:
270 case REGMAP_ENDIAN_NATIVE:
271 break;
272 default:
273 return ERR_PTR(-EINVAL);
274 }
275
Dimitris Papastamos46335112012-07-18 14:17:23 +0100276 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
Stephen Warren45f5ff82012-04-04 15:48:31 -0600277 if (!ctx)
278 return ERR_PTR(-ENOMEM);
279
280 ctx->regs = regs;
281 ctx->val_bytes = config->val_bits / 8;
Xiubo Li93258042014-03-27 12:42:43 +0800282 ctx->reg_bytes = config->reg_bits / 8;
283 ctx->pad_bytes = config->pad_bits / 8;
Stephen Warren6b8e0902013-11-25 15:12:47 -0700284 ctx->clk = ERR_PTR(-ENODEV);
Stephen Warren45f5ff82012-04-04 15:48:31 -0600285
Philipp Zabel878ec672013-02-14 17:39:08 +0100286 if (clk_id == NULL)
287 return ctx;
288
289 ctx->clk = clk_get(dev, clk_id);
290 if (IS_ERR(ctx->clk)) {
291 ret = PTR_ERR(ctx->clk);
292 goto err_free;
293 }
294
295 ret = clk_prepare(ctx->clk);
296 if (ret < 0) {
297 clk_put(ctx->clk);
298 goto err_free;
299 }
300
Stephen Warren45f5ff82012-04-04 15:48:31 -0600301 return ctx;
Philipp Zabel878ec672013-02-14 17:39:08 +0100302
303err_free:
304 kfree(ctx);
305
306 return ERR_PTR(ret);
Stephen Warren45f5ff82012-04-04 15:48:31 -0600307}
308
Nicolas Boichat3cfe7a72015-07-08 14:30:18 +0800309struct regmap *__regmap_init_mmio_clk(struct device *dev, const char *clk_id,
310 void __iomem *regs,
311 const struct regmap_config *config,
312 struct lock_class_key *lock_key,
313 const char *lock_name)
Stephen Warren45f5ff82012-04-04 15:48:31 -0600314{
315 struct regmap_mmio_context *ctx;
316
Philipp Zabel878ec672013-02-14 17:39:08 +0100317 ctx = regmap_mmio_gen_context(dev, clk_id, regs, config);
Stephen Warren45f5ff82012-04-04 15:48:31 -0600318 if (IS_ERR(ctx))
319 return ERR_CAST(ctx);
320
Nicolas Boichat3cfe7a72015-07-08 14:30:18 +0800321 return __regmap_init(dev, &regmap_mmio, ctx, config,
322 lock_key, lock_name);
Stephen Warren45f5ff82012-04-04 15:48:31 -0600323}
Nicolas Boichat3cfe7a72015-07-08 14:30:18 +0800324EXPORT_SYMBOL_GPL(__regmap_init_mmio_clk);
Stephen Warren45f5ff82012-04-04 15:48:31 -0600325
Nicolas Boichat3cfe7a72015-07-08 14:30:18 +0800326struct regmap *__devm_regmap_init_mmio_clk(struct device *dev,
327 const char *clk_id,
328 void __iomem *regs,
329 const struct regmap_config *config,
330 struct lock_class_key *lock_key,
331 const char *lock_name)
Stephen Warren45f5ff82012-04-04 15:48:31 -0600332{
333 struct regmap_mmio_context *ctx;
334
Philipp Zabel878ec672013-02-14 17:39:08 +0100335 ctx = regmap_mmio_gen_context(dev, clk_id, regs, config);
Stephen Warren45f5ff82012-04-04 15:48:31 -0600336 if (IS_ERR(ctx))
337 return ERR_CAST(ctx);
338
Nicolas Boichat3cfe7a72015-07-08 14:30:18 +0800339 return __devm_regmap_init(dev, &regmap_mmio, ctx, config,
340 lock_key, lock_name);
Stephen Warren45f5ff82012-04-04 15:48:31 -0600341}
Nicolas Boichat3cfe7a72015-07-08 14:30:18 +0800342EXPORT_SYMBOL_GPL(__devm_regmap_init_mmio_clk);
Stephen Warren45f5ff82012-04-04 15:48:31 -0600343
344MODULE_LICENSE("GPL v2");