Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Simon Glass | 86075ba | 2015-07-06 12:54:39 -0600 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (C) 2015 Google, Inc |
Simon Glass | 86075ba | 2015-07-06 12:54:39 -0600 | [diff] [blame] | 4 | */ |
| 5 | |
| 6 | #include <common.h> |
| 7 | #include <dm.h> |
Simon Glass | f7ae49f | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 8 | #include <log.h> |
Simon Glass | 86075ba | 2015-07-06 12:54:39 -0600 | [diff] [blame] | 9 | #include <mapmem.h> |
| 10 | #include <regmap.h> |
| 11 | #include <syscon.h> |
| 12 | #include <asm/test.h> |
| 13 | #include <dm/test.h> |
Simon Glass | 61b29b8 | 2020-02-03 07:36:15 -0700 | [diff] [blame] | 14 | #include <linux/err.h> |
Simon Glass | 0e1fad4 | 2020-07-19 10:15:37 -0600 | [diff] [blame] | 15 | #include <test/test.h> |
Simon Glass | 86075ba | 2015-07-06 12:54:39 -0600 | [diff] [blame] | 16 | #include <test/ut.h> |
| 17 | |
Simon Glass | 86075ba | 2015-07-06 12:54:39 -0600 | [diff] [blame] | 18 | /* Base test of register maps */ |
| 19 | static int dm_test_regmap_base(struct unit_test_state *uts) |
| 20 | { |
| 21 | struct udevice *dev; |
| 22 | struct regmap *map; |
Masahiro Yamada | 99552c3 | 2018-04-23 13:26:53 +0900 | [diff] [blame] | 23 | ofnode node; |
Simon Glass | 86075ba | 2015-07-06 12:54:39 -0600 | [diff] [blame] | 24 | 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 Yamada | 8c1de5e | 2018-04-19 12:14:01 +0900 | [diff] [blame] | 30 | ut_asserteq(0x10, map->ranges[0].start); |
Mario Six | 82744c2 | 2018-10-04 09:00:40 +0200 | [diff] [blame] | 31 | ut_asserteq(16, map->ranges[0].size); |
Simon Glass | 86075ba | 2015-07-06 12:54:39 -0600 | [diff] [blame] | 32 | 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 Yamada | 8c1de5e | 2018-04-19 12:14:01 +0900 | [diff] [blame] | 38 | ut_asserteq(0x20, map->ranges[0].start); |
Simon Glass | 86075ba | 2015-07-06 12:54:39 -0600 | [diff] [blame] | 39 | for (i = 0; i < 4; i++) { |
| 40 | const unsigned long addr = 0x20 + 8 * i; |
| 41 | |
Masahiro Yamada | 8c1de5e | 2018-04-19 12:14:01 +0900 | [diff] [blame] | 42 | ut_asserteq(addr, map->ranges[i].start); |
| 43 | ut_asserteq(5 + i, map->ranges[i].size); |
Simon Glass | 86075ba | 2015-07-06 12:54:39 -0600 | [diff] [blame] | 44 | 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 Yamada | 99552c3 | 2018-04-23 13:26:53 +0900 | [diff] [blame] | 52 | /* 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 Glass | 86075ba | 2015-07-06 12:54:39 -0600 | [diff] [blame] | 68 | return 0; |
| 69 | } |
Simon Glass | e180c2b | 2020-07-28 19:41:12 -0600 | [diff] [blame^] | 70 | DM_TEST(dm_test_regmap_base, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT); |
Simon Glass | 86075ba | 2015-07-06 12:54:39 -0600 | [diff] [blame] | 71 | |
| 72 | /* Test we can access a regmap through syscon */ |
| 73 | static 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 Glass | e180c2b | 2020-07-28 19:41:12 -0600 | [diff] [blame^] | 96 | DM_TEST(dm_test_regmap_syscon, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT); |
Neil Armstrong | e068512 | 2018-04-27 11:56:15 +0200 | [diff] [blame] | 97 | |
| 98 | /* Read/Write/Modify test */ |
| 99 | static 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 Hiblot | 2333dc4 | 2019-10-11 16:16:50 -0600 | [diff] [blame] | 105 | sandbox_set_enable_memio(true); |
Neil Armstrong | e068512 | 2018-04-27 11:56:15 +0200 | [diff] [blame] | 106 | 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 Hiblot | 2333dc4 | 2019-10-11 16:16:50 -0600 | [diff] [blame] | 111 | ut_assertok(regmap_write(map, 5, 0x55aa2211)); |
Neil Armstrong | e068512 | 2018-04-27 11:56:15 +0200 | [diff] [blame] | 112 | |
| 113 | ut_assertok(regmap_read(map, 0, ®)); |
Jean-Jacques Hiblot | 2333dc4 | 2019-10-11 16:16:50 -0600 | [diff] [blame] | 114 | ut_asserteq(0xcacafafa, reg); |
| 115 | ut_assertok(regmap_read(map, 5, ®)); |
| 116 | ut_asserteq(0x55aa2211, reg); |
Neil Armstrong | e068512 | 2018-04-27 11:56:15 +0200 | [diff] [blame] | 117 | |
Jean-Jacques Hiblot | 2333dc4 | 2019-10-11 16:16:50 -0600 | [diff] [blame] | 118 | ut_assertok(regmap_read(map, 0, ®)); |
| 119 | ut_asserteq(0xcacafafa, reg); |
Neil Armstrong | e068512 | 2018-04-27 11:56:15 +0200 | [diff] [blame] | 120 | ut_assertok(regmap_update_bits(map, 0, 0xff00ff00, 0x55aa2211)); |
Jean-Jacques Hiblot | 2333dc4 | 2019-10-11 16:16:50 -0600 | [diff] [blame] | 121 | ut_assertok(regmap_read(map, 0, ®)); |
| 122 | ut_asserteq(0x55ca22fa, reg); |
| 123 | ut_assertok(regmap_update_bits(map, 5, 0x00ff00ff, 0xcacafada)); |
| 124 | ut_assertok(regmap_read(map, 5, ®)); |
| 125 | ut_asserteq(0x55ca22da, reg); |
Neil Armstrong | e068512 | 2018-04-27 11:56:15 +0200 | [diff] [blame] | 126 | |
| 127 | return 0; |
| 128 | } |
| 129 | |
Simon Glass | e180c2b | 2020-07-28 19:41:12 -0600 | [diff] [blame^] | 130 | DM_TEST(dm_test_regmap_rw, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT); |
Mario Six | 45ef7f5 | 2018-10-15 09:24:13 +0200 | [diff] [blame] | 131 | |
| 132 | /* Get/Set test */ |
| 133 | static 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 Hiblot | 2333dc4 | 2019-10-11 16:16:50 -0600 | [diff] [blame] | 145 | sandbox_set_enable_memio(true); |
Mario Six | 45ef7f5 | 2018-10-15 09:24:13 +0200 | [diff] [blame] | 146 | 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, ®)); |
Jean-Jacques Hiblot | 2333dc4 | 2019-10-11 16:16:50 -0600 | [diff] [blame] | 154 | ut_asserteq(0xcacafafa, reg); |
Mario Six | 45ef7f5 | 2018-10-15 09:24:13 +0200 | [diff] [blame] | 155 | ut_assertok(regmap_get(map, struct layout, val3, ®)); |
Jean-Jacques Hiblot | 2333dc4 | 2019-10-11 16:16:50 -0600 | [diff] [blame] | 156 | ut_asserteq(0x55aa2211, reg); |
Mario Six | 45ef7f5 | 2018-10-15 09:24:13 +0200 | [diff] [blame] | 157 | |
| 158 | return 0; |
| 159 | } |
| 160 | |
Simon Glass | e180c2b | 2020-07-28 19:41:12 -0600 | [diff] [blame^] | 161 | DM_TEST(dm_test_regmap_getset, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT); |
Neil Armstrong | ebe3497 | 2018-11-22 11:01:04 +0100 | [diff] [blame] | 162 | |
| 163 | /* Read polling test */ |
| 164 | static 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 Hiblot | 2333dc4 | 2019-10-11 16:16:50 -0600 | [diff] [blame] | 177 | ut_assertok(regmap_write(map, 0, 0x0)); |
Neil Armstrong | ebe3497 | 2018-11-22 11:01:04 +0100 | [diff] [blame] | 178 | ut_asserteq(-ETIMEDOUT, |
Simon Glass | df9cf1c | 2018-12-09 17:11:10 -0700 | [diff] [blame] | 179 | regmap_read_poll_timeout_test(map, 0, reg, |
| 180 | (reg == 0xcacafafa), |
| 181 | 1, 5 * CONFIG_SYS_HZ, |
| 182 | 5 * CONFIG_SYS_HZ)); |
Neil Armstrong | ebe3497 | 2018-11-22 11:01:04 +0100 | [diff] [blame] | 183 | |
| 184 | ut_assert(get_timer(start) > (5 * CONFIG_SYS_HZ)); |
| 185 | |
| 186 | return 0; |
| 187 | } |
| 188 | |
Simon Glass | e180c2b | 2020-07-28 19:41:12 -0600 | [diff] [blame^] | 189 | DM_TEST(dm_test_regmap_poll, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT); |