blob: 1e03e7f8bacb220a47d0edf29700f6a1814fc21e [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 Li41b0c2c2014-03-27 12:42:42 +080064static inline void regmap_mmio_count_check(size_t count)
65{
Xiubo Li93258042014-03-27 12:42:43 +080066 BUG_ON(count % 2 != 0);
Xiubo Li41b0c2c2014-03-27 12:42:42 +080067}
68
Stephen Warren45f5ff82012-04-04 15:48:31 -060069static int regmap_mmio_gather_write(void *context,
70 const void *reg, size_t reg_size,
71 const void *val, size_t val_size)
72{
73 struct regmap_mmio_context *ctx = context;
74 u32 offset;
Philipp Zabel878ec672013-02-14 17:39:08 +010075 int ret;
Stephen Warren45f5ff82012-04-04 15:48:31 -060076
Xiubo Li41b0c2c2014-03-27 12:42:42 +080077 regmap_mmio_regsize_check(reg_size);
Stephen Warren40606db2012-04-06 15:17:32 -060078
Stephen Warren6b8e0902013-11-25 15:12:47 -070079 if (!IS_ERR(ctx->clk)) {
Philipp Zabel878ec672013-02-14 17:39:08 +010080 ret = clk_enable(ctx->clk);
81 if (ret < 0)
82 return ret;
83 }
84
Stephen Warren6a552442012-05-24 10:47:27 -060085 offset = *(u32 *)reg;
Stephen Warren45f5ff82012-04-04 15:48:31 -060086
87 while (val_size) {
88 switch (ctx->val_bytes) {
89 case 1:
90 writeb(*(u8 *)val, ctx->regs + offset);
91 break;
92 case 2:
Stephen Warren6a552442012-05-24 10:47:27 -060093 writew(*(u16 *)val, ctx->regs + offset);
Stephen Warren45f5ff82012-04-04 15:48:31 -060094 break;
95 case 4:
Stephen Warren6a552442012-05-24 10:47:27 -060096 writel(*(u32 *)val, ctx->regs + offset);
Stephen Warren45f5ff82012-04-04 15:48:31 -060097 break;
98#ifdef CONFIG_64BIT
99 case 8:
Stephen Warren6a552442012-05-24 10:47:27 -0600100 writeq(*(u64 *)val, ctx->regs + offset);
Stephen Warren45f5ff82012-04-04 15:48:31 -0600101 break;
102#endif
103 default:
104 /* Should be caught by regmap_mmio_check_config */
Stephen Warren40606db2012-04-06 15:17:32 -0600105 BUG();
Stephen Warren45f5ff82012-04-04 15:48:31 -0600106 }
107 val_size -= ctx->val_bytes;
108 val += ctx->val_bytes;
109 offset += ctx->val_bytes;
110 }
111
Stephen Warren6b8e0902013-11-25 15:12:47 -0700112 if (!IS_ERR(ctx->clk))
Philipp Zabel878ec672013-02-14 17:39:08 +0100113 clk_disable(ctx->clk);
114
Stephen Warren45f5ff82012-04-04 15:48:31 -0600115 return 0;
116}
117
118static int regmap_mmio_write(void *context, const void *data, size_t count)
119{
Xiubo Li93258042014-03-27 12:42:43 +0800120 struct regmap_mmio_context *ctx = context;
121 u32 offset = ctx->reg_bytes + ctx->pad_bytes;
122
Xiubo Li41b0c2c2014-03-27 12:42:42 +0800123 regmap_mmio_count_check(count);
Stephen Warren40606db2012-04-06 15:17:32 -0600124
Xiubo Li93258042014-03-27 12:42:43 +0800125 return regmap_mmio_gather_write(context, data, ctx->reg_bytes,
126 data + offset, count - offset);
Stephen Warren45f5ff82012-04-04 15:48:31 -0600127}
128
129static int regmap_mmio_read(void *context,
130 const void *reg, size_t reg_size,
131 void *val, size_t val_size)
132{
133 struct regmap_mmio_context *ctx = context;
134 u32 offset;
Philipp Zabel878ec672013-02-14 17:39:08 +0100135 int ret;
Stephen Warren45f5ff82012-04-04 15:48:31 -0600136
Xiubo Li41b0c2c2014-03-27 12:42:42 +0800137 regmap_mmio_regsize_check(reg_size);
Stephen Warren40606db2012-04-06 15:17:32 -0600138
Stephen Warren6b8e0902013-11-25 15:12:47 -0700139 if (!IS_ERR(ctx->clk)) {
Philipp Zabel878ec672013-02-14 17:39:08 +0100140 ret = clk_enable(ctx->clk);
141 if (ret < 0)
142 return ret;
143 }
144
Stephen Warren6a552442012-05-24 10:47:27 -0600145 offset = *(u32 *)reg;
Stephen Warren45f5ff82012-04-04 15:48:31 -0600146
147 while (val_size) {
148 switch (ctx->val_bytes) {
149 case 1:
150 *(u8 *)val = readb(ctx->regs + offset);
151 break;
152 case 2:
Stephen Warren6a552442012-05-24 10:47:27 -0600153 *(u16 *)val = readw(ctx->regs + offset);
Stephen Warren45f5ff82012-04-04 15:48:31 -0600154 break;
155 case 4:
Stephen Warren6a552442012-05-24 10:47:27 -0600156 *(u32 *)val = readl(ctx->regs + offset);
Stephen Warren45f5ff82012-04-04 15:48:31 -0600157 break;
158#ifdef CONFIG_64BIT
159 case 8:
Stephen Warren6a552442012-05-24 10:47:27 -0600160 *(u64 *)val = readq(ctx->regs + offset);
Stephen Warren45f5ff82012-04-04 15:48:31 -0600161 break;
162#endif
163 default:
164 /* Should be caught by regmap_mmio_check_config */
Stephen Warren40606db2012-04-06 15:17:32 -0600165 BUG();
Stephen Warren45f5ff82012-04-04 15:48:31 -0600166 }
167 val_size -= ctx->val_bytes;
168 val += ctx->val_bytes;
169 offset += ctx->val_bytes;
170 }
171
Stephen Warren6b8e0902013-11-25 15:12:47 -0700172 if (!IS_ERR(ctx->clk))
Philipp Zabel878ec672013-02-14 17:39:08 +0100173 clk_disable(ctx->clk);
174
Stephen Warren45f5ff82012-04-04 15:48:31 -0600175 return 0;
176}
177
178static void regmap_mmio_free_context(void *context)
179{
Philipp Zabel878ec672013-02-14 17:39:08 +0100180 struct regmap_mmio_context *ctx = context;
181
Stephen Warren6b8e0902013-11-25 15:12:47 -0700182 if (!IS_ERR(ctx->clk)) {
Philipp Zabel878ec672013-02-14 17:39:08 +0100183 clk_unprepare(ctx->clk);
184 clk_put(ctx->clk);
185 }
Stephen Warren45f5ff82012-04-04 15:48:31 -0600186 kfree(context);
187}
188
189static struct regmap_bus regmap_mmio = {
190 .fast_io = true,
191 .write = regmap_mmio_write,
192 .gather_write = regmap_mmio_gather_write,
193 .read = regmap_mmio_read,
194 .free_context = regmap_mmio_free_context,
Stephen Warren6a552442012-05-24 10:47:27 -0600195 .reg_format_endian_default = REGMAP_ENDIAN_NATIVE,
196 .val_format_endian_default = REGMAP_ENDIAN_NATIVE,
Stephen Warren45f5ff82012-04-04 15:48:31 -0600197};
198
Philipp Zabel878ec672013-02-14 17:39:08 +0100199static struct regmap_mmio_context *regmap_mmio_gen_context(struct device *dev,
200 const char *clk_id,
201 void __iomem *regs,
Stephen Warren45f5ff82012-04-04 15:48:31 -0600202 const struct regmap_config *config)
203{
204 struct regmap_mmio_context *ctx;
Stephen Warrenf01ee602012-04-09 13:40:24 -0600205 int min_stride;
Philipp Zabel878ec672013-02-14 17:39:08 +0100206 int ret;
Stephen Warren45f5ff82012-04-04 15:48:31 -0600207
Xiubo Li451485b2014-03-28 13:12:56 +0800208 ret = regmap_mmio_regbits_check(config->reg_bits);
209 if (ret)
210 return ERR_PTR(ret);
Stephen Warren45f5ff82012-04-04 15:48:31 -0600211
212 if (config->pad_bits)
213 return ERR_PTR(-EINVAL);
214
215 switch (config->val_bits) {
216 case 8:
Stephen Warrenf01ee602012-04-09 13:40:24 -0600217 /* The core treats 0 as 1 */
218 min_stride = 0;
219 break;
Stephen Warren45f5ff82012-04-04 15:48:31 -0600220 case 16:
Stephen Warrenf01ee602012-04-09 13:40:24 -0600221 min_stride = 2;
222 break;
Stephen Warren45f5ff82012-04-04 15:48:31 -0600223 case 32:
Stephen Warrenf01ee602012-04-09 13:40:24 -0600224 min_stride = 4;
225 break;
Stephen Warren45f5ff82012-04-04 15:48:31 -0600226#ifdef CONFIG_64BIT
227 case 64:
Stephen Warrenf01ee602012-04-09 13:40:24 -0600228 min_stride = 8;
229 break;
Stephen Warren45f5ff82012-04-04 15:48:31 -0600230#endif
231 break;
232 default:
233 return ERR_PTR(-EINVAL);
234 }
235
Stephen Warrenf01ee602012-04-09 13:40:24 -0600236 if (config->reg_stride < min_stride)
237 return ERR_PTR(-EINVAL);
238
Stephen Warren6a552442012-05-24 10:47:27 -0600239 switch (config->reg_format_endian) {
240 case REGMAP_ENDIAN_DEFAULT:
241 case REGMAP_ENDIAN_NATIVE:
242 break;
243 default:
244 return ERR_PTR(-EINVAL);
245 }
246
Dimitris Papastamos46335112012-07-18 14:17:23 +0100247 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
Stephen Warren45f5ff82012-04-04 15:48:31 -0600248 if (!ctx)
249 return ERR_PTR(-ENOMEM);
250
251 ctx->regs = regs;
252 ctx->val_bytes = config->val_bits / 8;
Xiubo Li93258042014-03-27 12:42:43 +0800253 ctx->reg_bytes = config->reg_bits / 8;
254 ctx->pad_bytes = config->pad_bits / 8;
Stephen Warren6b8e0902013-11-25 15:12:47 -0700255 ctx->clk = ERR_PTR(-ENODEV);
Stephen Warren45f5ff82012-04-04 15:48:31 -0600256
Philipp Zabel878ec672013-02-14 17:39:08 +0100257 if (clk_id == NULL)
258 return ctx;
259
260 ctx->clk = clk_get(dev, clk_id);
261 if (IS_ERR(ctx->clk)) {
262 ret = PTR_ERR(ctx->clk);
263 goto err_free;
264 }
265
266 ret = clk_prepare(ctx->clk);
267 if (ret < 0) {
268 clk_put(ctx->clk);
269 goto err_free;
270 }
271
Stephen Warren45f5ff82012-04-04 15:48:31 -0600272 return ctx;
Philipp Zabel878ec672013-02-14 17:39:08 +0100273
274err_free:
275 kfree(ctx);
276
277 return ERR_PTR(ret);
Stephen Warren45f5ff82012-04-04 15:48:31 -0600278}
279
280/**
Philipp Zabel878ec672013-02-14 17:39:08 +0100281 * regmap_init_mmio_clk(): Initialise register map with register clock
Stephen Warren45f5ff82012-04-04 15:48:31 -0600282 *
283 * @dev: Device that will be interacted with
Philipp Zabel878ec672013-02-14 17:39:08 +0100284 * @clk_id: register clock consumer ID
Stephen Warren45f5ff82012-04-04 15:48:31 -0600285 * @regs: Pointer to memory-mapped IO region
286 * @config: Configuration for register map
287 *
288 * The return value will be an ERR_PTR() on error or a valid pointer to
289 * a struct regmap.
290 */
Philipp Zabel878ec672013-02-14 17:39:08 +0100291struct regmap *regmap_init_mmio_clk(struct device *dev, const char *clk_id,
292 void __iomem *regs,
293 const struct regmap_config *config)
Stephen Warren45f5ff82012-04-04 15:48:31 -0600294{
295 struct regmap_mmio_context *ctx;
296
Philipp Zabel878ec672013-02-14 17:39:08 +0100297 ctx = regmap_mmio_gen_context(dev, clk_id, regs, config);
Stephen Warren45f5ff82012-04-04 15:48:31 -0600298 if (IS_ERR(ctx))
299 return ERR_CAST(ctx);
300
301 return regmap_init(dev, &regmap_mmio, ctx, config);
302}
Philipp Zabel878ec672013-02-14 17:39:08 +0100303EXPORT_SYMBOL_GPL(regmap_init_mmio_clk);
Stephen Warren45f5ff82012-04-04 15:48:31 -0600304
305/**
Philipp Zabel878ec672013-02-14 17:39:08 +0100306 * devm_regmap_init_mmio_clk(): Initialise managed register map with clock
Stephen Warren45f5ff82012-04-04 15:48:31 -0600307 *
308 * @dev: Device that will be interacted with
Philipp Zabel878ec672013-02-14 17:39:08 +0100309 * @clk_id: register clock consumer ID
Stephen Warren45f5ff82012-04-04 15:48:31 -0600310 * @regs: Pointer to memory-mapped IO region
311 * @config: Configuration for register map
312 *
313 * The return value will be an ERR_PTR() on error or a valid pointer
314 * to a struct regmap. The regmap will be automatically freed by the
315 * device management code.
316 */
Philipp Zabel878ec672013-02-14 17:39:08 +0100317struct regmap *devm_regmap_init_mmio_clk(struct device *dev, const char *clk_id,
318 void __iomem *regs,
319 const struct regmap_config *config)
Stephen Warren45f5ff82012-04-04 15:48:31 -0600320{
321 struct regmap_mmio_context *ctx;
322
Philipp Zabel878ec672013-02-14 17:39:08 +0100323 ctx = regmap_mmio_gen_context(dev, clk_id, regs, config);
Stephen Warren45f5ff82012-04-04 15:48:31 -0600324 if (IS_ERR(ctx))
325 return ERR_CAST(ctx);
326
327 return devm_regmap_init(dev, &regmap_mmio, ctx, config);
328}
Philipp Zabel878ec672013-02-14 17:39:08 +0100329EXPORT_SYMBOL_GPL(devm_regmap_init_mmio_clk);
Stephen Warren45f5ff82012-04-04 15:48:31 -0600330
331MODULE_LICENSE("GPL v2");