Coverage Report

Created: 2026-11-13 06:22

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/barebox/drivers/base/regmap/regmap-mmio.c
Line
Count
Source
1
// SPDX-License-Identifier: GPL-2.0-only
2
//
3
// Register map access API - MMIO support
4
//
5
// Copyright (c) 2012, NVIDIA CORPORATION.  All rights reserved.
6
7
#include <linux/clk.h>
8
#include <linux/err.h>
9
#include <io.h>
10
#include <malloc.h>
11
#include <linux/regmap.h>
12
13
#include "internal.h"
14
15
struct regmap_mmio_context {
16
  void __iomem *regs;
17
  unsigned val_bytes;
18
19
  struct clk *clk;
20
21
  void (*reg_write)(struct regmap_mmio_context *ctx,
22
        unsigned int reg, unsigned int val);
23
  unsigned int (*reg_read)(struct regmap_mmio_context *ctx,
24
               unsigned int reg);
25
};
26
27
static int regmap_mmio_regbits_check(size_t reg_bits)
28
0
{
29
0
  switch (reg_bits) {
30
0
  case 8:
31
0
  case 16:
32
0
  case 32:
33
0
#ifdef CONFIG_64BIT
34
0
  case 64:
35
0
#endif
36
0
    return 0;
37
0
  default:
38
0
    return -EINVAL;
39
0
  }
40
0
}
41
42
static int regmap_mmio_get_min_stride(size_t val_bits)
43
0
{
44
0
  int min_stride;
45
46
0
  switch (val_bits) {
47
0
  case 8:
48
    /* The core treats 0 as 1 */
49
0
    min_stride = 0;
50
0
    break;
51
0
  case 16:
52
0
    min_stride = 2;
53
0
    break;
54
0
  case 32:
55
0
    min_stride = 4;
56
0
    break;
57
0
#ifdef CONFIG_64BIT
58
0
  case 64:
59
0
    min_stride = 8;
60
0
    break;
61
0
#endif
62
0
  default:
63
0
    return -EINVAL;
64
0
  }
65
66
0
  return min_stride;
67
0
}
68
69
static void regmap_mmio_write8(struct regmap_mmio_context *ctx,
70
        unsigned int reg,
71
        unsigned int val)
72
0
{
73
0
  writeb(val, ctx->regs + reg);
74
0
}
75
76
static void regmap_mmio_write16le(struct regmap_mmio_context *ctx,
77
          unsigned int reg,
78
          unsigned int val)
79
0
{
80
0
  writew(val, ctx->regs + reg);
81
0
}
82
83
static void regmap_mmio_write32le(struct regmap_mmio_context *ctx,
84
          unsigned int reg,
85
          unsigned int val)
86
0
{
87
0
  writel(val, ctx->regs + reg);
88
0
}
89
90
static void regmap_mmio_write16be(struct regmap_mmio_context *ctx,
91
          unsigned int reg,
92
          unsigned int val)
93
0
{
94
0
  iowrite16be(val, ctx->regs + reg);
95
0
}
96
97
static void regmap_mmio_write32be(struct regmap_mmio_context *ctx,
98
          unsigned int reg,
99
          unsigned int val)
100
0
{
101
0
  iowrite32be(val, ctx->regs + reg);
102
0
}
103
104
#ifdef CONFIG_64BIT
105
static void regmap_mmio_write64le(struct regmap_mmio_context *ctx,
106
          unsigned int reg,
107
          unsigned int val)
108
0
{
109
0
  writeq(val, ctx->regs + reg);
110
0
}
111
#endif
112
113
static int regmap_mmio_write(void *context, unsigned int reg, unsigned int val)
114
0
{
115
0
  struct regmap_mmio_context *ctx = context;
116
0
  int ret;
117
118
0
  ret = clk_enable(ctx->clk);
119
0
  if (ret < 0)
120
0
    return ret;
121
122
0
  ctx->reg_write(ctx, reg, val);
123
124
0
  clk_disable(ctx->clk);
125
126
0
  return 0;
127
0
}
128
129
static unsigned int regmap_mmio_read8(struct regmap_mmio_context *ctx,
130
              unsigned int reg)
131
0
{
132
0
  return readb(ctx->regs + reg);
133
0
}
134
135
static unsigned int regmap_mmio_read16le(struct regmap_mmio_context *ctx,
136
                 unsigned int reg)
137
0
{
138
0
  return readw(ctx->regs + reg);
139
0
}
140
141
static unsigned int regmap_mmio_read32le(struct regmap_mmio_context *ctx,
142
                 unsigned int reg)
143
0
{
144
0
  return readl(ctx->regs + reg);
145
0
}
146
147
#ifdef CONFIG_64BIT
148
static unsigned int regmap_mmio_read64le(struct regmap_mmio_context *ctx,
149
                 unsigned int reg)
150
0
{
151
0
  return readq(ctx->regs + reg);
152
0
}
153
#endif
154
155
static unsigned int regmap_mmio_read16be(struct regmap_mmio_context *ctx,
156
                 unsigned int reg)
157
0
{
158
0
  return ioread16be(ctx->regs + reg);
159
0
}
160
161
static unsigned int regmap_mmio_read32be(struct regmap_mmio_context *ctx,
162
                 unsigned int reg)
163
0
{
164
0
  return ioread32be(ctx->regs + reg);
165
0
}
166
167
static int regmap_mmio_read(void *context, unsigned int reg, unsigned int *val)
168
0
{
169
0
  struct regmap_mmio_context *ctx = context;
170
0
  int ret;
171
172
0
  ret = clk_enable(ctx->clk);
173
0
  if (ret < 0)
174
0
    return ret;
175
176
0
  *val = ctx->reg_read(ctx, reg);
177
178
0
  clk_disable(ctx->clk);
179
180
0
  return 0;
181
0
}
182
183
static const struct regmap_bus regmap_mmio = {
184
  .reg_write = regmap_mmio_write,
185
  .reg_read = regmap_mmio_read,
186
  .val_format_endian_default = REGMAP_ENDIAN_LITTLE,
187
};
188
189
static struct regmap_mmio_context *regmap_mmio_gen_context(struct device *dev,
190
                 void __iomem *regs,
191
                 const struct regmap_config *config)
192
0
{
193
0
  struct regmap_mmio_context *ctx;
194
0
  int min_stride;
195
0
  int ret;
196
197
0
  ret = regmap_mmio_regbits_check(config->reg_bits);
198
0
  if (ret)
199
0
    return ERR_PTR(ret);
200
201
0
  if (config->pad_bits)
202
0
    return ERR_PTR(-EINVAL);
203
204
0
  min_stride = regmap_mmio_get_min_stride(config->val_bits);
205
0
  if (min_stride < 0)
206
0
    return ERR_PTR(min_stride);
207
208
0
  if (config->reg_stride < min_stride)
209
0
    return ERR_PTR(-EINVAL);
210
211
0
  ctx = xzalloc(sizeof(*ctx));
212
213
0
  ctx->regs = regs;
214
0
  ctx->val_bytes = config->val_bits / 8;
215
216
0
  switch (regmap_get_val_endian(dev, &regmap_mmio, config)) {
217
0
  case REGMAP_ENDIAN_DEFAULT:
218
0
  case REGMAP_ENDIAN_LITTLE:
219
0
#ifdef __LITTLE_ENDIAN
220
0
  case REGMAP_ENDIAN_NATIVE:
221
0
#endif
222
0
    switch (config->val_bits) {
223
0
    case 8:
224
0
      ctx->reg_read = regmap_mmio_read8;
225
0
      ctx->reg_write = regmap_mmio_write8;
226
0
      break;
227
0
    case 16:
228
0
      ctx->reg_read = regmap_mmio_read16le;
229
0
      ctx->reg_write = regmap_mmio_write16le;
230
0
      break;
231
0
    case 32:
232
0
      ctx->reg_read = regmap_mmio_read32le;
233
0
      ctx->reg_write = regmap_mmio_write32le;
234
0
      break;
235
0
#ifdef CONFIG_64BIT
236
0
    case 64:
237
0
      ctx->reg_read = regmap_mmio_read64le;
238
0
      ctx->reg_write = regmap_mmio_write64le;
239
0
      break;
240
0
#endif
241
0
    default:
242
0
      ret = -EINVAL;
243
0
      goto err_free;
244
0
    }
245
0
    break;
246
0
  case REGMAP_ENDIAN_BIG:
247
#ifdef __BIG_ENDIAN
248
  case REGMAP_ENDIAN_NATIVE:
249
#endif
250
0
    switch (config->val_bits) {
251
0
    case 8:
252
0
      ctx->reg_read = regmap_mmio_read8;
253
0
      ctx->reg_write = regmap_mmio_write8;
254
0
      break;
255
0
    case 16:
256
0
      ctx->reg_read = regmap_mmio_read16be;
257
0
      ctx->reg_write = regmap_mmio_write16be;
258
0
      break;
259
0
    case 32:
260
0
      ctx->reg_read = regmap_mmio_read32be;
261
0
      ctx->reg_write = regmap_mmio_write32be;
262
0
      break;
263
0
    default:
264
0
      ret = -EINVAL;
265
0
      goto err_free;
266
0
    }
267
0
    break;
268
0
  default:
269
0
    ret = -EINVAL;
270
0
    goto err_free;
271
0
  }
272
273
0
  return ctx;
274
275
0
err_free:
276
0
  free(ctx);
277
278
0
  return ERR_PTR(ret);
279
0
}
280
281
struct regmap *regmap_init_mmio_clk(struct device *dev,
282
            const char *clk_id,
283
            void __iomem *regs,
284
            const struct regmap_config *config)
285
0
{
286
0
  struct regmap_mmio_context *ctx;
287
288
0
  ctx = regmap_mmio_gen_context(dev, regs, config);
289
0
  if (IS_ERR(ctx))
290
0
    return ERR_CAST(ctx);
291
292
0
  if (clk_id) {
293
0
    struct clk *clk;
294
295
0
    clk = clk_get(dev, clk_id);
296
0
    if (IS_ERR(clk)) {
297
0
      free(ctx);
298
0
      return ERR_CAST(clk);
299
0
    }
300
301
0
    ctx->clk = clk;
302
0
  }
303
304
0
  return regmap_init(dev, &regmap_mmio, ctx, config);
305
0
}
306
307
int regmap_mmio_attach_clk(struct regmap *map, struct clk *clk)
308
0
{
309
0
  struct regmap_mmio_context *ctx = map->bus_context;
310
311
0
  ctx->clk = clk;
312
313
0
  return 0;
314
0
}
315
316
void regmap_mmio_detach_clk(struct regmap *map)
317
0
{
318
0
  struct regmap_mmio_context *ctx = map->bus_context;
319
320
0
  ctx->clk = NULL;
321
0
}