blob: ed080a47b1f8e76d18e80b5b4eeeb2956e568bf3 [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
50static inline void regmap_mmio_count_check(size_t count)
51{
Xiubo Li93258042014-03-27 12:42:43 +080052 BUG_ON(count % 2 != 0);
Xiubo Li41b0c2c2014-03-27 12:42:42 +080053}
54
Stephen Warren45f5ff82012-04-04 15:48:31 -060055static 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 Zabel878ec672013-02-14 17:39:08 +010061 int ret;
Stephen Warren45f5ff82012-04-04 15:48:31 -060062
Xiubo Li41b0c2c2014-03-27 12:42:42 +080063 regmap_mmio_regsize_check(reg_size);
Stephen Warren40606db2012-04-06 15:17:32 -060064
Stephen Warren6b8e0902013-11-25 15:12:47 -070065 if (!IS_ERR(ctx->clk)) {
Philipp Zabel878ec672013-02-14 17:39:08 +010066 ret = clk_enable(ctx->clk);
67 if (ret < 0)
68 return ret;
69 }
70
Stephen Warren6a552442012-05-24 10:47:27 -060071 offset = *(u32 *)reg;
Stephen Warren45f5ff82012-04-04 15:48:31 -060072
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 Warren6a552442012-05-24 10:47:27 -060079 writew(*(u16 *)val, ctx->regs + offset);
Stephen Warren45f5ff82012-04-04 15:48:31 -060080 break;
81 case 4:
Stephen Warren6a552442012-05-24 10:47:27 -060082 writel(*(u32 *)val, ctx->regs + offset);
Stephen Warren45f5ff82012-04-04 15:48:31 -060083 break;
84#ifdef CONFIG_64BIT
85 case 8:
Stephen Warren6a552442012-05-24 10:47:27 -060086 writeq(*(u64 *)val, ctx->regs + offset);
Stephen Warren45f5ff82012-04-04 15:48:31 -060087 break;
88#endif
89 default:
90 /* Should be caught by regmap_mmio_check_config */
Stephen Warren40606db2012-04-06 15:17:32 -060091 BUG();
Stephen Warren45f5ff82012-04-04 15:48:31 -060092 }
93 val_size -= ctx->val_bytes;
94 val += ctx->val_bytes;
95 offset += ctx->val_bytes;
96 }
97
Stephen Warren6b8e0902013-11-25 15:12:47 -070098 if (!IS_ERR(ctx->clk))
Philipp Zabel878ec672013-02-14 17:39:08 +010099 clk_disable(ctx->clk);
100
Stephen Warren45f5ff82012-04-04 15:48:31 -0600101 return 0;
102}
103
104static int regmap_mmio_write(void *context, const void *data, size_t count)
105{
Xiubo Li93258042014-03-27 12:42:43 +0800106 struct regmap_mmio_context *ctx = context;
107 u32 offset = ctx->reg_bytes + ctx->pad_bytes;
108
Xiubo Li41b0c2c2014-03-27 12:42:42 +0800109 regmap_mmio_count_check(count);
Stephen Warren40606db2012-04-06 15:17:32 -0600110
Xiubo Li93258042014-03-27 12:42:43 +0800111 return regmap_mmio_gather_write(context, data, ctx->reg_bytes,
112 data + offset, count - offset);
Stephen Warren45f5ff82012-04-04 15:48:31 -0600113}
114
115static 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 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
Stephen Warren6a552442012-05-24 10:47:27 -0600131 offset = *(u32 *)reg;
Stephen Warren45f5ff82012-04-04 15:48:31 -0600132
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 Warren6a552442012-05-24 10:47:27 -0600139 *(u16 *)val = readw(ctx->regs + offset);
Stephen Warren45f5ff82012-04-04 15:48:31 -0600140 break;
141 case 4:
Stephen Warren6a552442012-05-24 10:47:27 -0600142 *(u32 *)val = readl(ctx->regs + offset);
Stephen Warren45f5ff82012-04-04 15:48:31 -0600143 break;
144#ifdef CONFIG_64BIT
145 case 8:
Stephen Warren6a552442012-05-24 10:47:27 -0600146 *(u64 *)val = readq(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 void regmap_mmio_free_context(void *context)
165{
Philipp Zabel878ec672013-02-14 17:39:08 +0100166 struct regmap_mmio_context *ctx = context;
167
Stephen Warren6b8e0902013-11-25 15:12:47 -0700168 if (!IS_ERR(ctx->clk)) {
Philipp Zabel878ec672013-02-14 17:39:08 +0100169 clk_unprepare(ctx->clk);
170 clk_put(ctx->clk);
171 }
Stephen Warren45f5ff82012-04-04 15:48:31 -0600172 kfree(context);
173}
174
175static 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 Warren6a552442012-05-24 10:47:27 -0600181 .reg_format_endian_default = REGMAP_ENDIAN_NATIVE,
182 .val_format_endian_default = REGMAP_ENDIAN_NATIVE,
Stephen Warren45f5ff82012-04-04 15:48:31 -0600183};
184
Philipp Zabel878ec672013-02-14 17:39:08 +0100185static struct regmap_mmio_context *regmap_mmio_gen_context(struct device *dev,
186 const char *clk_id,
187 void __iomem *regs,
Stephen Warren45f5ff82012-04-04 15:48:31 -0600188 const struct regmap_config *config)
189{
190 struct regmap_mmio_context *ctx;
Stephen Warrenf01ee602012-04-09 13:40:24 -0600191 int min_stride;
Philipp Zabel878ec672013-02-14 17:39:08 +0100192 int ret;
Stephen Warren45f5ff82012-04-04 15:48:31 -0600193
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 Warrenf01ee602012-04-09 13:40:24 -0600202 /* The core treats 0 as 1 */
203 min_stride = 0;
204 break;
Stephen Warren45f5ff82012-04-04 15:48:31 -0600205 case 16:
Stephen Warrenf01ee602012-04-09 13:40:24 -0600206 min_stride = 2;
207 break;
Stephen Warren45f5ff82012-04-04 15:48:31 -0600208 case 32:
Stephen Warrenf01ee602012-04-09 13:40:24 -0600209 min_stride = 4;
210 break;
Stephen Warren45f5ff82012-04-04 15:48:31 -0600211#ifdef CONFIG_64BIT
212 case 64:
Stephen Warrenf01ee602012-04-09 13:40:24 -0600213 min_stride = 8;
214 break;
Stephen Warren45f5ff82012-04-04 15:48:31 -0600215#endif
216 break;
217 default:
218 return ERR_PTR(-EINVAL);
219 }
220
Stephen Warrenf01ee602012-04-09 13:40:24 -0600221 if (config->reg_stride < min_stride)
222 return ERR_PTR(-EINVAL);
223
Stephen Warren6a552442012-05-24 10:47:27 -0600224 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 Papastamos46335112012-07-18 14:17:23 +0100232 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
Stephen Warren45f5ff82012-04-04 15:48:31 -0600233 if (!ctx)
234 return ERR_PTR(-ENOMEM);
235
236 ctx->regs = regs;
237 ctx->val_bytes = config->val_bits / 8;
Xiubo Li93258042014-03-27 12:42:43 +0800238 ctx->reg_bytes = config->reg_bits / 8;
239 ctx->pad_bytes = config->pad_bits / 8;
Stephen Warren6b8e0902013-11-25 15:12:47 -0700240 ctx->clk = ERR_PTR(-ENODEV);
Stephen Warren45f5ff82012-04-04 15:48:31 -0600241
Philipp Zabel878ec672013-02-14 17:39:08 +0100242 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 Warren45f5ff82012-04-04 15:48:31 -0600257 return ctx;
Philipp Zabel878ec672013-02-14 17:39:08 +0100258
259err_free:
260 kfree(ctx);
261
262 return ERR_PTR(ret);
Stephen Warren45f5ff82012-04-04 15:48:31 -0600263}
264
265/**
Philipp Zabel878ec672013-02-14 17:39:08 +0100266 * regmap_init_mmio_clk(): Initialise register map with register clock
Stephen Warren45f5ff82012-04-04 15:48:31 -0600267 *
268 * @dev: Device that will be interacted with
Philipp Zabel878ec672013-02-14 17:39:08 +0100269 * @clk_id: register clock consumer ID
Stephen Warren45f5ff82012-04-04 15:48:31 -0600270 * @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 Zabel878ec672013-02-14 17:39:08 +0100276struct regmap *regmap_init_mmio_clk(struct device *dev, const char *clk_id,
277 void __iomem *regs,
278 const struct regmap_config *config)
Stephen Warren45f5ff82012-04-04 15:48:31 -0600279{
280 struct regmap_mmio_context *ctx;
281
Philipp Zabel878ec672013-02-14 17:39:08 +0100282 ctx = regmap_mmio_gen_context(dev, clk_id, regs, config);
Stephen Warren45f5ff82012-04-04 15:48:31 -0600283 if (IS_ERR(ctx))
284 return ERR_CAST(ctx);
285
286 return regmap_init(dev, &regmap_mmio, ctx, config);
287}
Philipp Zabel878ec672013-02-14 17:39:08 +0100288EXPORT_SYMBOL_GPL(regmap_init_mmio_clk);
Stephen Warren45f5ff82012-04-04 15:48:31 -0600289
290/**
Philipp Zabel878ec672013-02-14 17:39:08 +0100291 * devm_regmap_init_mmio_clk(): Initialise managed register map with clock
Stephen Warren45f5ff82012-04-04 15:48:31 -0600292 *
293 * @dev: Device that will be interacted with
Philipp Zabel878ec672013-02-14 17:39:08 +0100294 * @clk_id: register clock consumer ID
Stephen Warren45f5ff82012-04-04 15:48:31 -0600295 * @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 Zabel878ec672013-02-14 17:39:08 +0100302struct regmap *devm_regmap_init_mmio_clk(struct device *dev, const char *clk_id,
303 void __iomem *regs,
304 const struct regmap_config *config)
Stephen Warren45f5ff82012-04-04 15:48:31 -0600305{
306 struct regmap_mmio_context *ctx;
307
Philipp Zabel878ec672013-02-14 17:39:08 +0100308 ctx = regmap_mmio_gen_context(dev, clk_id, regs, config);
Stephen Warren45f5ff82012-04-04 15:48:31 -0600309 if (IS_ERR(ctx))
310 return ERR_CAST(ctx);
311
312 return devm_regmap_init(dev, &regmap_mmio, ctx, config);
313}
Philipp Zabel878ec672013-02-14 17:39:08 +0100314EXPORT_SYMBOL_GPL(devm_regmap_init_mmio_clk);
Stephen Warren45f5ff82012-04-04 15:48:31 -0600315
316MODULE_LICENSE("GPL v2");