blob: 436b711b85d503282cda3d0d78732619e5a35a41 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Simon Glass265142a2015-07-06 12:54:39 -06002/*
3 * Copyright (C) 2015 Google, Inc
Simon Glass265142a2015-07-06 12:54:39 -06004 */
5
Simon Glass265142a2015-07-06 12:54:39 -06006#include <dm.h>
Simon Glass0f2af882020-05-10 11:40:05 -06007#include <log.h>
Simon Glass265142a2015-07-06 12:54:39 -06008#include <mapmem.h>
9#include <regmap.h>
10#include <syscon.h>
Jean-Jacques Hiblot0b89fc52020-09-24 10:04:18 +053011#include <rand.h>
Tom Rinidec7ea02024-05-20 13:35:03 -060012#include <time.h>
Simon Glass265142a2015-07-06 12:54:39 -060013#include <asm/test.h>
14#include <dm/test.h>
Jean-Jacques Hiblot0b89fc52020-09-24 10:04:18 +053015#include <dm/devres.h>
Simon Glassd66c5f72020-02-03 07:36:15 -070016#include <linux/err.h>
Simon Glass75c4d412020-07-19 10:15:37 -060017#include <test/test.h>
Simon Glass265142a2015-07-06 12:54:39 -060018#include <test/ut.h>
19
Simon Glass265142a2015-07-06 12:54:39 -060020/* Base test of register maps */
21static int dm_test_regmap_base(struct unit_test_state *uts)
22{
23 struct udevice *dev;
24 struct regmap *map;
Masahiro Yamada42ab1072018-04-23 13:26:53 +090025 ofnode node;
Simon Glass265142a2015-07-06 12:54:39 -060026 int i;
27
28 ut_assertok(uclass_get_device(UCLASS_SYSCON, 0, &dev));
29 map = syscon_get_regmap(dev);
30 ut_assertok_ptr(map);
31 ut_asserteq(1, map->range_count);
Masahiro Yamada54c5ecb2018-04-19 12:14:01 +090032 ut_asserteq(0x10, map->ranges[0].start);
Mario Sixe3f59f42018-10-04 09:00:40 +020033 ut_asserteq(16, map->ranges[0].size);
Simon Glass265142a2015-07-06 12:54:39 -060034 ut_asserteq(0x10, map_to_sysmem(regmap_get_range(map, 0)));
35
36 ut_assertok(uclass_get_device(UCLASS_SYSCON, 1, &dev));
37 map = syscon_get_regmap(dev);
38 ut_assertok_ptr(map);
39 ut_asserteq(4, map->range_count);
Masahiro Yamada54c5ecb2018-04-19 12:14:01 +090040 ut_asserteq(0x20, map->ranges[0].start);
Simon Glass265142a2015-07-06 12:54:39 -060041 for (i = 0; i < 4; i++) {
42 const unsigned long addr = 0x20 + 8 * i;
43
Masahiro Yamada54c5ecb2018-04-19 12:14:01 +090044 ut_asserteq(addr, map->ranges[i].start);
45 ut_asserteq(5 + i, map->ranges[i].size);
Simon Glass265142a2015-07-06 12:54:39 -060046 ut_asserteq(addr, map_to_sysmem(regmap_get_range(map, i)));
47 }
48
49 /* Check that we can't pretend a different device is a syscon */
50 ut_assertok(uclass_get_device(UCLASS_I2C, 0, &dev));
51 map = syscon_get_regmap(dev);
52 ut_asserteq_ptr(ERR_PTR(-ENOEXEC), map);
53
Masahiro Yamada42ab1072018-04-23 13:26:53 +090054 /* A different device can be a syscon by using Linux-compat API */
55 node = ofnode_path("/syscon@2");
56 ut_assert(ofnode_valid(node));
57
58 map = syscon_node_to_regmap(node);
59 ut_assertok_ptr(map);
60 ut_asserteq(4, map->range_count);
61 ut_asserteq(0x40, map->ranges[0].start);
62 for (i = 0; i < 4; i++) {
63 const unsigned long addr = 0x40 + 8 * i;
64
65 ut_asserteq(addr, map->ranges[i].start);
66 ut_asserteq(5 + i, map->ranges[i].size);
67 ut_asserteq(addr, map_to_sysmem(regmap_get_range(map, i)));
68 }
69
Simon Glass265142a2015-07-06 12:54:39 -060070 return 0;
71}
Simon Glass1a92f832024-08-22 07:57:48 -060072DM_TEST(dm_test_regmap_base, UTF_SCAN_PDATA | UTF_SCAN_FDT);
Simon Glass265142a2015-07-06 12:54:39 -060073
74/* Test we can access a regmap through syscon */
75static int dm_test_regmap_syscon(struct unit_test_state *uts)
76{
77 struct regmap *map;
78
79 map = syscon_get_regmap_by_driver_data(SYSCON0);
80 ut_assertok_ptr(map);
81 ut_asserteq(1, map->range_count);
82
83 map = syscon_get_regmap_by_driver_data(SYSCON1);
84 ut_assertok_ptr(map);
85 ut_asserteq(4, map->range_count);
86
87 map = syscon_get_regmap_by_driver_data(SYSCON_COUNT);
88 ut_asserteq_ptr(ERR_PTR(-ENODEV), map);
89
90 ut_asserteq(0x10, map_to_sysmem(syscon_get_first_range(SYSCON0)));
91 ut_asserteq(0x20, map_to_sysmem(syscon_get_first_range(SYSCON1)));
92 ut_asserteq_ptr(ERR_PTR(-ENODEV),
93 syscon_get_first_range(SYSCON_COUNT));
94
95 return 0;
96}
Simon Glass1a92f832024-08-22 07:57:48 -060097DM_TEST(dm_test_regmap_syscon, UTF_SCAN_PDATA | UTF_SCAN_FDT);
Neil Armstrong7a59d1d2018-04-27 11:56:15 +020098
99/* Read/Write/Modify test */
100static int dm_test_regmap_rw(struct unit_test_state *uts)
101{
102 struct udevice *dev;
103 struct regmap *map;
104 uint reg;
105
Jean-Jacques Hiblote7e79532019-10-11 16:16:50 -0600106 sandbox_set_enable_memio(true);
Neil Armstrong7a59d1d2018-04-27 11:56:15 +0200107 ut_assertok(uclass_get_device(UCLASS_SYSCON, 0, &dev));
108 map = syscon_get_regmap(dev);
109 ut_assertok_ptr(map);
110
111 ut_assertok(regmap_write(map, 0, 0xcacafafa));
Jean-Jacques Hiblote7e79532019-10-11 16:16:50 -0600112 ut_assertok(regmap_write(map, 5, 0x55aa2211));
Neil Armstrong7a59d1d2018-04-27 11:56:15 +0200113
114 ut_assertok(regmap_read(map, 0, &reg));
Jean-Jacques Hiblote7e79532019-10-11 16:16:50 -0600115 ut_asserteq(0xcacafafa, reg);
116 ut_assertok(regmap_read(map, 5, &reg));
117 ut_asserteq(0x55aa2211, reg);
Neil Armstrong7a59d1d2018-04-27 11:56:15 +0200118
Jean-Jacques Hiblote7e79532019-10-11 16:16:50 -0600119 ut_assertok(regmap_read(map, 0, &reg));
120 ut_asserteq(0xcacafafa, reg);
Neil Armstrong7a59d1d2018-04-27 11:56:15 +0200121 ut_assertok(regmap_update_bits(map, 0, 0xff00ff00, 0x55aa2211));
Jean-Jacques Hiblote7e79532019-10-11 16:16:50 -0600122 ut_assertok(regmap_read(map, 0, &reg));
123 ut_asserteq(0x55ca22fa, reg);
124 ut_assertok(regmap_update_bits(map, 5, 0x00ff00ff, 0xcacafada));
125 ut_assertok(regmap_read(map, 5, &reg));
126 ut_asserteq(0x55ca22da, reg);
Neil Armstrong7a59d1d2018-04-27 11:56:15 +0200127
128 return 0;
129}
Simon Glass1a92f832024-08-22 07:57:48 -0600130DM_TEST(dm_test_regmap_rw, UTF_SCAN_PDATA | UTF_SCAN_FDT);
Mario Six460212e2018-10-15 09:24:13 +0200131
132/* Get/Set test */
133static int dm_test_regmap_getset(struct unit_test_state *uts)
134{
135 struct udevice *dev;
136 struct regmap *map;
137 uint reg;
138 struct layout {
139 u32 val0;
140 u32 val1;
141 u32 val2;
142 u32 val3;
143 };
144
Jean-Jacques Hiblote7e79532019-10-11 16:16:50 -0600145 sandbox_set_enable_memio(true);
Mario Six460212e2018-10-15 09:24:13 +0200146 ut_assertok(uclass_get_device(UCLASS_SYSCON, 0, &dev));
147 map = syscon_get_regmap(dev);
148 ut_assertok_ptr(map);
149
150 regmap_set(map, struct layout, val0, 0xcacafafa);
151 regmap_set(map, struct layout, val3, 0x55aa2211);
152
153 ut_assertok(regmap_get(map, struct layout, val0, &reg));
Jean-Jacques Hiblote7e79532019-10-11 16:16:50 -0600154 ut_asserteq(0xcacafafa, reg);
Mario Six460212e2018-10-15 09:24:13 +0200155 ut_assertok(regmap_get(map, struct layout, val3, &reg));
Jean-Jacques Hiblote7e79532019-10-11 16:16:50 -0600156 ut_asserteq(0x55aa2211, reg);
Mario Six460212e2018-10-15 09:24:13 +0200157
158 return 0;
159}
160
Simon Glass1a92f832024-08-22 07:57:48 -0600161DM_TEST(dm_test_regmap_getset, UTF_SCAN_PDATA | UTF_SCAN_FDT);
Neil Armstrongf9735be2018-11-22 11:01:04 +0100162
163/* Read polling test */
164static int dm_test_regmap_poll(struct unit_test_state *uts)
165{
166 struct udevice *dev;
167 struct regmap *map;
168 uint reg;
169 unsigned long start;
170
171 ut_assertok(uclass_get_device(UCLASS_SYSCON, 0, &dev));
172 map = syscon_get_regmap(dev);
173 ut_assertok_ptr(map);
174
175 start = get_timer(0);
176
Jean-Jacques Hiblote7e79532019-10-11 16:16:50 -0600177 ut_assertok(regmap_write(map, 0, 0x0));
Neil Armstrongf9735be2018-11-22 11:01:04 +0100178 ut_asserteq(-ETIMEDOUT,
Simon Glassc2a7c492018-12-09 17:11:10 -0700179 regmap_read_poll_timeout_test(map, 0, reg,
180 (reg == 0xcacafafa),
181 1, 5 * CONFIG_SYS_HZ,
182 5 * CONFIG_SYS_HZ));
Neil Armstrongf9735be2018-11-22 11:01:04 +0100183
184 ut_assert(get_timer(start) > (5 * CONFIG_SYS_HZ));
185
186 return 0;
187}
Simon Glass1a92f832024-08-22 07:57:48 -0600188DM_TEST(dm_test_regmap_poll, UTF_SCAN_PDATA | UTF_SCAN_FDT);
Jean-Jacques Hiblot0b89fc52020-09-24 10:04:18 +0530189
190struct regmaptest_priv {
191 struct regmap *cfg_regmap; /* For testing regmap_config options. */
192 struct regmap *fld_regmap; /* For testing regmap fields. */
193 struct regmap_field **fields;
194};
195
196static const struct reg_field field_cfgs[] = {
197 {
198 .reg = 0,
199 .lsb = 0,
200 .msb = 6,
201 },
202 {
203 .reg = 2,
204 .lsb = 4,
205 .msb = 12,
206 },
207 {
208 .reg = 2,
209 .lsb = 12,
210 .msb = 15,
211 }
212};
213
214#define REGMAP_TEST_BUF_START 0
215#define REGMAP_TEST_BUF_SZ 5
216
217static int remaptest_probe(struct udevice *dev)
218{
219 struct regmaptest_priv *priv = dev_get_priv(dev);
220 struct regmap *regmap;
221 struct regmap_field *field;
222 struct regmap_config cfg;
223 int i;
224 static const int n = ARRAY_SIZE(field_cfgs);
225
226 /*
227 * To exercise all the regmap config options, create a regmap that
228 * points to a custom memory area instead of the one defined in device
229 * tree. Use 2-byte elements. To allow directly indexing into the
230 * elements, use an offset shift of 1. So, accessing offset 1 gets the
231 * element at index 1 at memory location 2.
232 *
233 * REGMAP_TEST_BUF_SZ is the number of elements, so we need to multiply
234 * it by 2 because r_size expects number of bytes.
235 */
236 cfg.reg_offset_shift = 1;
237 cfg.r_start = REGMAP_TEST_BUF_START;
238 cfg.r_size = REGMAP_TEST_BUF_SZ * 2;
239 cfg.width = REGMAP_SIZE_16;
240
241 regmap = devm_regmap_init(dev, NULL, NULL, &cfg);
242 if (IS_ERR(regmap))
243 return PTR_ERR(regmap);
244 priv->cfg_regmap = regmap;
245
246 memset(&cfg, 0, sizeof(struct regmap_config));
247 cfg.width = REGMAP_SIZE_16;
248
249 regmap = devm_regmap_init(dev, NULL, NULL, &cfg);
250 if (IS_ERR(regmap))
251 return PTR_ERR(regmap);
252 priv->fld_regmap = regmap;
253
254 priv->fields = devm_kzalloc(dev, sizeof(struct regmap_field *) * n,
255 GFP_KERNEL);
256 if (!priv->fields)
257 return -ENOMEM;
258
259 for (i = 0 ; i < n; i++) {
260 field = devm_regmap_field_alloc(dev, priv->fld_regmap,
261 field_cfgs[i]);
262 if (IS_ERR(field))
263 return PTR_ERR(field);
264 priv->fields[i] = field;
265 }
266
267 return 0;
268}
269
270static const struct udevice_id regmaptest_ids[] = {
271 { .compatible = "sandbox,regmap_test" },
272 { }
273};
274
275U_BOOT_DRIVER(regmap_test) = {
276 .name = "regmaptest_drv",
277 .of_match = regmaptest_ids,
278 .id = UCLASS_NOP,
279 .probe = remaptest_probe,
Simon Glass8a2b47f2020-12-03 16:55:17 -0700280 .priv_auto = sizeof(struct regmaptest_priv),
Jean-Jacques Hiblot0b89fc52020-09-24 10:04:18 +0530281};
282
283static int dm_test_devm_regmap(struct unit_test_state *uts)
284{
285 int i = 0;
Andrew Scull2110a462022-04-03 10:39:14 +0000286 uint val;
Jean-Jacques Hiblot0b89fc52020-09-24 10:04:18 +0530287 u16 pattern[REGMAP_TEST_BUF_SZ];
288 u16 *buffer;
289 struct udevice *dev;
290 struct regmaptest_priv *priv;
291
292 sandbox_set_enable_memio(true);
293
294 /*
295 * Map the memory area the regmap should point to so we can make sure
296 * the writes actually go to that location.
297 */
298 buffer = map_physmem(REGMAP_TEST_BUF_START,
299 REGMAP_TEST_BUF_SZ * 2, MAP_NOCACHE);
300
301 ut_assertok(uclass_get_device_by_name(UCLASS_NOP, "regmap-test_0",
302 &dev));
303 priv = dev_get_priv(dev);
304
Jean-Jacques Hiblot0b89fc52020-09-24 10:04:18 +0530305 for (i = 0; i < REGMAP_TEST_BUF_SZ; i++) {
Simon Glass9d6710c2021-05-13 19:39:23 -0600306 pattern[i] = i * 0x87654321;
Jean-Jacques Hiblot0b89fc52020-09-24 10:04:18 +0530307 ut_assertok(regmap_write(priv->cfg_regmap, i, pattern[i]));
308 }
309 for (i = 0; i < REGMAP_TEST_BUF_SZ; i++) {
Andrew Scull2110a462022-04-03 10:39:14 +0000310 ut_assertok(regmap_read(priv->cfg_regmap, i, &val));
Jean-Jacques Hiblot0b89fc52020-09-24 10:04:18 +0530311 ut_asserteq(val, buffer[i]);
312 ut_asserteq(val, pattern[i]);
313 }
314
315 ut_asserteq(-ERANGE, regmap_write(priv->cfg_regmap, REGMAP_TEST_BUF_SZ,
316 val));
317 ut_asserteq(-ERANGE, regmap_read(priv->cfg_regmap, REGMAP_TEST_BUF_SZ,
Andrew Scull2110a462022-04-03 10:39:14 +0000318 &val));
Jean-Jacques Hiblot0b89fc52020-09-24 10:04:18 +0530319 ut_asserteq(-ERANGE, regmap_write(priv->cfg_regmap, -1, val));
Andrew Scull2110a462022-04-03 10:39:14 +0000320 ut_asserteq(-ERANGE, regmap_read(priv->cfg_regmap, -1, &val));
Jean-Jacques Hiblot0b89fc52020-09-24 10:04:18 +0530321
322 return 0;
323}
Simon Glass1a92f832024-08-22 07:57:48 -0600324DM_TEST(dm_test_devm_regmap, UTF_SCAN_PDATA | UTF_SCAN_FDT);
Jean-Jacques Hiblot0b89fc52020-09-24 10:04:18 +0530325
326static int test_one_field(struct unit_test_state *uts,
327 struct regmap *regmap,
328 struct regmap_field *field,
329 struct reg_field field_cfg)
330{
331 int j;
332 unsigned int val;
333 int mask = (1 << (field_cfg.msb - field_cfg.lsb + 1)) - 1;
334 int shift = field_cfg.lsb;
335
336 ut_assertok(regmap_write(regmap, field_cfg.reg, 0));
337 ut_assertok(regmap_read(regmap, field_cfg.reg, &val));
338 ut_asserteq(0, val);
339
340 for (j = 0; j <= mask; j++) {
341 ut_assertok(regmap_field_write(field, j));
342 ut_assertok(regmap_field_read(field, &val));
343 ut_asserteq(j, val);
344 ut_assertok(regmap_read(regmap, field_cfg.reg, &val));
345 ut_asserteq(j << shift, val);
346 }
347
348 ut_assertok(regmap_field_write(field, mask + 1));
349 ut_assertok(regmap_read(regmap, field_cfg.reg, &val));
350 ut_asserteq(0, val);
351
352 ut_assertok(regmap_field_write(field, 0xFFFF));
353 ut_assertok(regmap_read(regmap, field_cfg.reg, &val));
354 ut_asserteq(mask << shift, val);
355
356 ut_assertok(regmap_write(regmap, field_cfg.reg, 0xFFFF));
357 ut_assertok(regmap_field_write(field, 0));
358 ut_assertok(regmap_read(regmap, field_cfg.reg, &val));
359 ut_asserteq(0xFFFF & ~(mask << shift), val);
360 return 0;
361}
362
363static int dm_test_devm_regmap_field(struct unit_test_state *uts)
364{
365 int i, rc;
366 struct udevice *dev;
367 struct regmaptest_priv *priv;
368
369 ut_assertok(uclass_get_device_by_name(UCLASS_NOP, "regmap-test_0",
370 &dev));
371 priv = dev_get_priv(dev);
372
373 sandbox_set_enable_memio(true);
374 for (i = 0 ; i < ARRAY_SIZE(field_cfgs); i++) {
375 rc = test_one_field(uts, priv->fld_regmap, priv->fields[i],
376 field_cfgs[i]);
377 if (rc)
378 break;
379 }
380
381 return 0;
382}
Simon Glass1a92f832024-08-22 07:57:48 -0600383DM_TEST(dm_test_devm_regmap_field, UTF_SCAN_PDATA | UTF_SCAN_FDT);