blob: b21f66732b9d14cb1f93ab1d5d1a166bd8a0bea7 [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
6#include <common.h>
7#include <dm.h>
8#include <mapmem.h>
9#include <regmap.h>
10#include <syscon.h>
11#include <asm/test.h>
12#include <dm/test.h>
Simon Glassd66c5f72020-02-03 07:36:15 -070013#include <linux/err.h>
Simon Glass265142a2015-07-06 12:54:39 -060014#include <test/ut.h>
15
Simon Glass265142a2015-07-06 12:54:39 -060016/* Base test of register maps */
17static int dm_test_regmap_base(struct unit_test_state *uts)
18{
19 struct udevice *dev;
20 struct regmap *map;
Masahiro Yamada42ab1072018-04-23 13:26:53 +090021 ofnode node;
Simon Glass265142a2015-07-06 12:54:39 -060022 int i;
23
24 ut_assertok(uclass_get_device(UCLASS_SYSCON, 0, &dev));
25 map = syscon_get_regmap(dev);
26 ut_assertok_ptr(map);
27 ut_asserteq(1, map->range_count);
Masahiro Yamada54c5ecb2018-04-19 12:14:01 +090028 ut_asserteq(0x10, map->ranges[0].start);
Mario Sixe3f59f42018-10-04 09:00:40 +020029 ut_asserteq(16, map->ranges[0].size);
Simon Glass265142a2015-07-06 12:54:39 -060030 ut_asserteq(0x10, map_to_sysmem(regmap_get_range(map, 0)));
31
32 ut_assertok(uclass_get_device(UCLASS_SYSCON, 1, &dev));
33 map = syscon_get_regmap(dev);
34 ut_assertok_ptr(map);
35 ut_asserteq(4, map->range_count);
Masahiro Yamada54c5ecb2018-04-19 12:14:01 +090036 ut_asserteq(0x20, map->ranges[0].start);
Simon Glass265142a2015-07-06 12:54:39 -060037 for (i = 0; i < 4; i++) {
38 const unsigned long addr = 0x20 + 8 * i;
39
Masahiro Yamada54c5ecb2018-04-19 12:14:01 +090040 ut_asserteq(addr, map->ranges[i].start);
41 ut_asserteq(5 + i, map->ranges[i].size);
Simon Glass265142a2015-07-06 12:54:39 -060042 ut_asserteq(addr, map_to_sysmem(regmap_get_range(map, i)));
43 }
44
45 /* Check that we can't pretend a different device is a syscon */
46 ut_assertok(uclass_get_device(UCLASS_I2C, 0, &dev));
47 map = syscon_get_regmap(dev);
48 ut_asserteq_ptr(ERR_PTR(-ENOEXEC), map);
49
Masahiro Yamada42ab1072018-04-23 13:26:53 +090050 /* A different device can be a syscon by using Linux-compat API */
51 node = ofnode_path("/syscon@2");
52 ut_assert(ofnode_valid(node));
53
54 map = syscon_node_to_regmap(node);
55 ut_assertok_ptr(map);
56 ut_asserteq(4, map->range_count);
57 ut_asserteq(0x40, map->ranges[0].start);
58 for (i = 0; i < 4; i++) {
59 const unsigned long addr = 0x40 + 8 * i;
60
61 ut_asserteq(addr, map->ranges[i].start);
62 ut_asserteq(5 + i, map->ranges[i].size);
63 ut_asserteq(addr, map_to_sysmem(regmap_get_range(map, i)));
64 }
65
Simon Glass265142a2015-07-06 12:54:39 -060066 return 0;
67}
68DM_TEST(dm_test_regmap_base, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
69
70/* Test we can access a regmap through syscon */
71static int dm_test_regmap_syscon(struct unit_test_state *uts)
72{
73 struct regmap *map;
74
75 map = syscon_get_regmap_by_driver_data(SYSCON0);
76 ut_assertok_ptr(map);
77 ut_asserteq(1, map->range_count);
78
79 map = syscon_get_regmap_by_driver_data(SYSCON1);
80 ut_assertok_ptr(map);
81 ut_asserteq(4, map->range_count);
82
83 map = syscon_get_regmap_by_driver_data(SYSCON_COUNT);
84 ut_asserteq_ptr(ERR_PTR(-ENODEV), map);
85
86 ut_asserteq(0x10, map_to_sysmem(syscon_get_first_range(SYSCON0)));
87 ut_asserteq(0x20, map_to_sysmem(syscon_get_first_range(SYSCON1)));
88 ut_asserteq_ptr(ERR_PTR(-ENODEV),
89 syscon_get_first_range(SYSCON_COUNT));
90
91 return 0;
92}
93
94DM_TEST(dm_test_regmap_syscon, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
Neil Armstrong7a59d1d2018-04-27 11:56:15 +020095
96/* Read/Write/Modify test */
97static int dm_test_regmap_rw(struct unit_test_state *uts)
98{
99 struct udevice *dev;
100 struct regmap *map;
101 uint reg;
102
Jean-Jacques Hiblote7e79532019-10-11 16:16:50 -0600103 sandbox_set_enable_memio(true);
Neil Armstrong7a59d1d2018-04-27 11:56:15 +0200104 ut_assertok(uclass_get_device(UCLASS_SYSCON, 0, &dev));
105 map = syscon_get_regmap(dev);
106 ut_assertok_ptr(map);
107
108 ut_assertok(regmap_write(map, 0, 0xcacafafa));
Jean-Jacques Hiblote7e79532019-10-11 16:16:50 -0600109 ut_assertok(regmap_write(map, 5, 0x55aa2211));
Neil Armstrong7a59d1d2018-04-27 11:56:15 +0200110
111 ut_assertok(regmap_read(map, 0, &reg));
Jean-Jacques Hiblote7e79532019-10-11 16:16:50 -0600112 ut_asserteq(0xcacafafa, reg);
113 ut_assertok(regmap_read(map, 5, &reg));
114 ut_asserteq(0x55aa2211, reg);
Neil Armstrong7a59d1d2018-04-27 11:56:15 +0200115
Jean-Jacques Hiblote7e79532019-10-11 16:16:50 -0600116 ut_assertok(regmap_read(map, 0, &reg));
117 ut_asserteq(0xcacafafa, reg);
Neil Armstrong7a59d1d2018-04-27 11:56:15 +0200118 ut_assertok(regmap_update_bits(map, 0, 0xff00ff00, 0x55aa2211));
Jean-Jacques Hiblote7e79532019-10-11 16:16:50 -0600119 ut_assertok(regmap_read(map, 0, &reg));
120 ut_asserteq(0x55ca22fa, reg);
121 ut_assertok(regmap_update_bits(map, 5, 0x00ff00ff, 0xcacafada));
122 ut_assertok(regmap_read(map, 5, &reg));
123 ut_asserteq(0x55ca22da, reg);
Neil Armstrong7a59d1d2018-04-27 11:56:15 +0200124
125 return 0;
126}
127
128DM_TEST(dm_test_regmap_rw, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
Mario Six460212e2018-10-15 09:24:13 +0200129
130/* Get/Set test */
131static int dm_test_regmap_getset(struct unit_test_state *uts)
132{
133 struct udevice *dev;
134 struct regmap *map;
135 uint reg;
136 struct layout {
137 u32 val0;
138 u32 val1;
139 u32 val2;
140 u32 val3;
141 };
142
Jean-Jacques Hiblote7e79532019-10-11 16:16:50 -0600143 sandbox_set_enable_memio(true);
Mario Six460212e2018-10-15 09:24:13 +0200144 ut_assertok(uclass_get_device(UCLASS_SYSCON, 0, &dev));
145 map = syscon_get_regmap(dev);
146 ut_assertok_ptr(map);
147
148 regmap_set(map, struct layout, val0, 0xcacafafa);
149 regmap_set(map, struct layout, val3, 0x55aa2211);
150
151 ut_assertok(regmap_get(map, struct layout, val0, &reg));
Jean-Jacques Hiblote7e79532019-10-11 16:16:50 -0600152 ut_asserteq(0xcacafafa, reg);
Mario Six460212e2018-10-15 09:24:13 +0200153 ut_assertok(regmap_get(map, struct layout, val3, &reg));
Jean-Jacques Hiblote7e79532019-10-11 16:16:50 -0600154 ut_asserteq(0x55aa2211, reg);
Mario Six460212e2018-10-15 09:24:13 +0200155
156 return 0;
157}
158
159DM_TEST(dm_test_regmap_getset, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
Neil Armstrongf9735be2018-11-22 11:01:04 +0100160
161/* Read polling test */
162static int dm_test_regmap_poll(struct unit_test_state *uts)
163{
164 struct udevice *dev;
165 struct regmap *map;
166 uint reg;
167 unsigned long start;
168
169 ut_assertok(uclass_get_device(UCLASS_SYSCON, 0, &dev));
170 map = syscon_get_regmap(dev);
171 ut_assertok_ptr(map);
172
173 start = get_timer(0);
174
Jean-Jacques Hiblote7e79532019-10-11 16:16:50 -0600175 ut_assertok(regmap_write(map, 0, 0x0));
Neil Armstrongf9735be2018-11-22 11:01:04 +0100176 ut_asserteq(-ETIMEDOUT,
Simon Glassc2a7c492018-12-09 17:11:10 -0700177 regmap_read_poll_timeout_test(map, 0, reg,
178 (reg == 0xcacafafa),
179 1, 5 * CONFIG_SYS_HZ,
180 5 * CONFIG_SYS_HZ));
Neil Armstrongf9735be2018-11-22 11:01:04 +0100181
182 ut_assert(get_timer(start) > (5 * CONFIG_SYS_HZ));
183
184 return 0;
185}
186
187DM_TEST(dm_test_regmap_poll, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);