blob: bd21c8365dc3ef67348bdb6d0b5dc3cc450394e1 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Simon Glass86075ba2015-07-06 12:54:39 -06002/*
3 * Copyright (C) 2015 Google, Inc
Simon Glass86075ba2015-07-06 12:54:39 -06004 */
5
6#include <common.h>
7#include <dm.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -06008#include <log.h>
Simon Glass86075ba2015-07-06 12:54:39 -06009#include <mapmem.h>
10#include <regmap.h>
11#include <syscon.h>
12#include <asm/test.h>
13#include <dm/test.h>
Simon Glass61b29b82020-02-03 07:36:15 -070014#include <linux/err.h>
Simon Glass0e1fad42020-07-19 10:15:37 -060015#include <test/test.h>
Simon Glass86075ba2015-07-06 12:54:39 -060016#include <test/ut.h>
17
Simon Glass86075ba2015-07-06 12:54:39 -060018/* Base test of register maps */
19static int dm_test_regmap_base(struct unit_test_state *uts)
20{
21 struct udevice *dev;
22 struct regmap *map;
Masahiro Yamada99552c32018-04-23 13:26:53 +090023 ofnode node;
Simon Glass86075ba2015-07-06 12:54:39 -060024 int i;
25
26 ut_assertok(uclass_get_device(UCLASS_SYSCON, 0, &dev));
27 map = syscon_get_regmap(dev);
28 ut_assertok_ptr(map);
29 ut_asserteq(1, map->range_count);
Masahiro Yamada8c1de5e2018-04-19 12:14:01 +090030 ut_asserteq(0x10, map->ranges[0].start);
Mario Six82744c22018-10-04 09:00:40 +020031 ut_asserteq(16, map->ranges[0].size);
Simon Glass86075ba2015-07-06 12:54:39 -060032 ut_asserteq(0x10, map_to_sysmem(regmap_get_range(map, 0)));
33
34 ut_assertok(uclass_get_device(UCLASS_SYSCON, 1, &dev));
35 map = syscon_get_regmap(dev);
36 ut_assertok_ptr(map);
37 ut_asserteq(4, map->range_count);
Masahiro Yamada8c1de5e2018-04-19 12:14:01 +090038 ut_asserteq(0x20, map->ranges[0].start);
Simon Glass86075ba2015-07-06 12:54:39 -060039 for (i = 0; i < 4; i++) {
40 const unsigned long addr = 0x20 + 8 * i;
41
Masahiro Yamada8c1de5e2018-04-19 12:14:01 +090042 ut_asserteq(addr, map->ranges[i].start);
43 ut_asserteq(5 + i, map->ranges[i].size);
Simon Glass86075ba2015-07-06 12:54:39 -060044 ut_asserteq(addr, map_to_sysmem(regmap_get_range(map, i)));
45 }
46
47 /* Check that we can't pretend a different device is a syscon */
48 ut_assertok(uclass_get_device(UCLASS_I2C, 0, &dev));
49 map = syscon_get_regmap(dev);
50 ut_asserteq_ptr(ERR_PTR(-ENOEXEC), map);
51
Masahiro Yamada99552c32018-04-23 13:26:53 +090052 /* A different device can be a syscon by using Linux-compat API */
53 node = ofnode_path("/syscon@2");
54 ut_assert(ofnode_valid(node));
55
56 map = syscon_node_to_regmap(node);
57 ut_assertok_ptr(map);
58 ut_asserteq(4, map->range_count);
59 ut_asserteq(0x40, map->ranges[0].start);
60 for (i = 0; i < 4; i++) {
61 const unsigned long addr = 0x40 + 8 * i;
62
63 ut_asserteq(addr, map->ranges[i].start);
64 ut_asserteq(5 + i, map->ranges[i].size);
65 ut_asserteq(addr, map_to_sysmem(regmap_get_range(map, i)));
66 }
67
Simon Glass86075ba2015-07-06 12:54:39 -060068 return 0;
69}
Simon Glasse180c2b2020-07-28 19:41:12 -060070DM_TEST(dm_test_regmap_base, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Simon Glass86075ba2015-07-06 12:54:39 -060071
72/* Test we can access a regmap through syscon */
73static int dm_test_regmap_syscon(struct unit_test_state *uts)
74{
75 struct regmap *map;
76
77 map = syscon_get_regmap_by_driver_data(SYSCON0);
78 ut_assertok_ptr(map);
79 ut_asserteq(1, map->range_count);
80
81 map = syscon_get_regmap_by_driver_data(SYSCON1);
82 ut_assertok_ptr(map);
83 ut_asserteq(4, map->range_count);
84
85 map = syscon_get_regmap_by_driver_data(SYSCON_COUNT);
86 ut_asserteq_ptr(ERR_PTR(-ENODEV), map);
87
88 ut_asserteq(0x10, map_to_sysmem(syscon_get_first_range(SYSCON0)));
89 ut_asserteq(0x20, map_to_sysmem(syscon_get_first_range(SYSCON1)));
90 ut_asserteq_ptr(ERR_PTR(-ENODEV),
91 syscon_get_first_range(SYSCON_COUNT));
92
93 return 0;
94}
95
Simon Glasse180c2b2020-07-28 19:41:12 -060096DM_TEST(dm_test_regmap_syscon, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Neil Armstronge0685122018-04-27 11:56:15 +020097
98/* Read/Write/Modify test */
99static int dm_test_regmap_rw(struct unit_test_state *uts)
100{
101 struct udevice *dev;
102 struct regmap *map;
103 uint reg;
104
Jean-Jacques Hiblot2333dc42019-10-11 16:16:50 -0600105 sandbox_set_enable_memio(true);
Neil Armstronge0685122018-04-27 11:56:15 +0200106 ut_assertok(uclass_get_device(UCLASS_SYSCON, 0, &dev));
107 map = syscon_get_regmap(dev);
108 ut_assertok_ptr(map);
109
110 ut_assertok(regmap_write(map, 0, 0xcacafafa));
Jean-Jacques Hiblot2333dc42019-10-11 16:16:50 -0600111 ut_assertok(regmap_write(map, 5, 0x55aa2211));
Neil Armstronge0685122018-04-27 11:56:15 +0200112
113 ut_assertok(regmap_read(map, 0, &reg));
Jean-Jacques Hiblot2333dc42019-10-11 16:16:50 -0600114 ut_asserteq(0xcacafafa, reg);
115 ut_assertok(regmap_read(map, 5, &reg));
116 ut_asserteq(0x55aa2211, reg);
Neil Armstronge0685122018-04-27 11:56:15 +0200117
Jean-Jacques Hiblot2333dc42019-10-11 16:16:50 -0600118 ut_assertok(regmap_read(map, 0, &reg));
119 ut_asserteq(0xcacafafa, reg);
Neil Armstronge0685122018-04-27 11:56:15 +0200120 ut_assertok(regmap_update_bits(map, 0, 0xff00ff00, 0x55aa2211));
Jean-Jacques Hiblot2333dc42019-10-11 16:16:50 -0600121 ut_assertok(regmap_read(map, 0, &reg));
122 ut_asserteq(0x55ca22fa, reg);
123 ut_assertok(regmap_update_bits(map, 5, 0x00ff00ff, 0xcacafada));
124 ut_assertok(regmap_read(map, 5, &reg));
125 ut_asserteq(0x55ca22da, reg);
Neil Armstronge0685122018-04-27 11:56:15 +0200126
127 return 0;
128}
129
Simon Glasse180c2b2020-07-28 19:41:12 -0600130DM_TEST(dm_test_regmap_rw, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Mario Six45ef7f52018-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 Hiblot2333dc42019-10-11 16:16:50 -0600145 sandbox_set_enable_memio(true);
Mario Six45ef7f52018-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 Hiblot2333dc42019-10-11 16:16:50 -0600154 ut_asserteq(0xcacafafa, reg);
Mario Six45ef7f52018-10-15 09:24:13 +0200155 ut_assertok(regmap_get(map, struct layout, val3, &reg));
Jean-Jacques Hiblot2333dc42019-10-11 16:16:50 -0600156 ut_asserteq(0x55aa2211, reg);
Mario Six45ef7f52018-10-15 09:24:13 +0200157
158 return 0;
159}
160
Simon Glasse180c2b2020-07-28 19:41:12 -0600161DM_TEST(dm_test_regmap_getset, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Neil Armstrongebe34972018-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 Hiblot2333dc42019-10-11 16:16:50 -0600177 ut_assertok(regmap_write(map, 0, 0x0));
Neil Armstrongebe34972018-11-22 11:01:04 +0100178 ut_asserteq(-ETIMEDOUT,
Simon Glassdf9cf1c2018-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 Armstrongebe34972018-11-22 11:01:04 +0100183
184 ut_assert(get_timer(start) > (5 * CONFIG_SYS_HZ));
185
186 return 0;
187}
188
Simon Glasse180c2b2020-07-28 19:41:12 -0600189DM_TEST(dm_test_regmap_poll, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);