blob: 987a265ba90523b67cb7129168d6fb0cb73f70f4 [file] [log] [blame]
Simon Glassb2c1cac2014-02-26 15:59:21 -07001/*
2 * Copyright (c) 2013 Google, Inc
3 *
4 * SPDX-License-Identifier: GPL-2.0+
5 */
6
7#include <common.h>
8#include <dm.h>
9#include <errno.h>
10#include <fdtdec.h>
11#include <malloc.h>
12#include <asm/io.h>
13#include <dm/test.h>
14#include <dm/root.h>
Simon Glassb2c1cac2014-02-26 15:59:21 -070015#include <dm/uclass-internal.h>
16#include <dm/util.h>
Joe Hershberger3a77be52015-05-20 14:27:27 -050017#include <test/ut.h>
Simon Glassb2c1cac2014-02-26 15:59:21 -070018
19DECLARE_GLOBAL_DATA_PTR;
20
Heiko Schocherb74fcb42014-05-22 12:43:05 +020021static int testfdt_drv_ping(struct udevice *dev, int pingval, int *pingret)
Simon Glassb2c1cac2014-02-26 15:59:21 -070022{
23 const struct dm_test_pdata *pdata = dev->platdata;
24 struct dm_test_priv *priv = dev_get_priv(dev);
25
26 *pingret = pingval + pdata->ping_add;
27 priv->ping_total += *pingret;
28
29 return 0;
30}
31
32static const struct test_ops test_ops = {
33 .ping = testfdt_drv_ping,
34};
35
Heiko Schocherb74fcb42014-05-22 12:43:05 +020036static int testfdt_ofdata_to_platdata(struct udevice *dev)
Simon Glassb2c1cac2014-02-26 15:59:21 -070037{
38 struct dm_test_pdata *pdata = dev_get_platdata(dev);
39
Simon Glassdd79d6e2017-01-17 16:52:55 -070040 pdata->ping_add = fdtdec_get_int(gd->fdt_blob, dev_of_offset(dev),
Simon Glassb2c1cac2014-02-26 15:59:21 -070041 "ping-add", -1);
Simon Glassdd79d6e2017-01-17 16:52:55 -070042 pdata->base = fdtdec_get_addr(gd->fdt_blob, dev_of_offset(dev),
Simon Glassa7bb08a2014-07-23 06:54:57 -060043 "ping-expect");
Simon Glassb2c1cac2014-02-26 15:59:21 -070044
45 return 0;
46}
47
Heiko Schocherb74fcb42014-05-22 12:43:05 +020048static int testfdt_drv_probe(struct udevice *dev)
Simon Glassb2c1cac2014-02-26 15:59:21 -070049{
50 struct dm_test_priv *priv = dev_get_priv(dev);
51
52 priv->ping_total += DM_TEST_START_TOTAL;
53
Simon Glass5104b982015-01-25 08:27:10 -070054 /*
55 * If this device is on a bus, the uclass_flag will be set before
56 * calling this function. This is used by
57 * dm_test_bus_child_pre_probe_uclass().
58 */
59 priv->uclass_total += priv->uclass_flag;
60
Simon Glassb2c1cac2014-02-26 15:59:21 -070061 return 0;
62}
63
Simon Glass767827a2014-06-11 23:29:45 -060064static const struct udevice_id testfdt_ids[] = {
Simon Glassb2c1cac2014-02-26 15:59:21 -070065 {
66 .compatible = "denx,u-boot-fdt-test",
67 .data = DM_TEST_TYPE_FIRST },
68 {
69 .compatible = "google,another-fdt-test",
70 .data = DM_TEST_TYPE_SECOND },
71 { }
72};
73
74U_BOOT_DRIVER(testfdt_drv) = {
75 .name = "testfdt_drv",
76 .of_match = testfdt_ids,
77 .id = UCLASS_TEST_FDT,
78 .ofdata_to_platdata = testfdt_ofdata_to_platdata,
79 .probe = testfdt_drv_probe,
80 .ops = &test_ops,
81 .priv_auto_alloc_size = sizeof(struct dm_test_priv),
82 .platdata_auto_alloc_size = sizeof(struct dm_test_pdata),
83};
84
85/* From here is the testfdt uclass code */
Heiko Schocherb74fcb42014-05-22 12:43:05 +020086int testfdt_ping(struct udevice *dev, int pingval, int *pingret)
Simon Glassb2c1cac2014-02-26 15:59:21 -070087{
88 const struct test_ops *ops = device_get_ops(dev);
89
90 if (!ops->ping)
91 return -ENOSYS;
92
93 return ops->ping(dev, pingval, pingret);
94}
95
96UCLASS_DRIVER(testfdt) = {
97 .name = "testfdt",
98 .id = UCLASS_TEST_FDT,
Simon Glass0ccb0972015-01-25 08:27:05 -070099 .flags = DM_UC_FLAG_SEQ_ALIAS,
Simon Glassb2c1cac2014-02-26 15:59:21 -0700100};
101
Joe Hershberger3a77be52015-05-20 14:27:27 -0500102int dm_check_devices(struct unit_test_state *uts, int num_devices)
Simon Glassb2c1cac2014-02-26 15:59:21 -0700103{
Heiko Schocherb74fcb42014-05-22 12:43:05 +0200104 struct udevice *dev;
Simon Glassb2c1cac2014-02-26 15:59:21 -0700105 int ret;
106 int i;
107
Simon Glassb2c1cac2014-02-26 15:59:21 -0700108 /*
109 * Now check that the ping adds are what we expect. This is using the
110 * ping-add property in each node.
111 */
Simon Glass40717422014-07-23 06:55:18 -0600112 for (i = 0; i < num_devices; i++) {
Simon Glassb2c1cac2014-02-26 15:59:21 -0700113 uint32_t base;
114
115 ret = uclass_get_device(UCLASS_TEST_FDT, i, &dev);
116 ut_assert(!ret);
117
118 /*
Simon Glassa7bb08a2014-07-23 06:54:57 -0600119 * Get the 'ping-expect' property, which tells us what the
120 * ping add should be. We don't use the platdata because we
121 * want to test the code that sets that up
122 * (testfdt_drv_probe()).
Simon Glassb2c1cac2014-02-26 15:59:21 -0700123 */
Simon Glassdd79d6e2017-01-17 16:52:55 -0700124 base = fdtdec_get_addr(gd->fdt_blob, dev_of_offset(dev),
Simon Glassa7bb08a2014-07-23 06:54:57 -0600125 "ping-expect");
Simon Glassb2c1cac2014-02-26 15:59:21 -0700126 debug("dev=%d, base=%d: %s\n", i, base,
Simon Glassdd79d6e2017-01-17 16:52:55 -0700127 fdt_get_name(gd->fdt_blob, dev_of_offset(dev), NULL));
Simon Glassb2c1cac2014-02-26 15:59:21 -0700128
Joe Hershberger3a77be52015-05-20 14:27:27 -0500129 ut_assert(!dm_check_operations(uts, dev, base,
Simon Glassb2c1cac2014-02-26 15:59:21 -0700130 dev_get_priv(dev)));
131 }
132
Simon Glass40717422014-07-23 06:55:18 -0600133 return 0;
134}
135
136/* Test that FDT-based binding works correctly */
Joe Hershberger3a77be52015-05-20 14:27:27 -0500137static int dm_test_fdt(struct unit_test_state *uts)
Simon Glass40717422014-07-23 06:55:18 -0600138{
Simon Glass0ccb0972015-01-25 08:27:05 -0700139 const int num_devices = 6;
Simon Glass40717422014-07-23 06:55:18 -0600140 struct udevice *dev;
141 struct uclass *uc;
142 int ret;
143 int i;
144
145 ret = dm_scan_fdt(gd->fdt_blob, false);
146 ut_assert(!ret);
147
148 ret = uclass_get(UCLASS_TEST_FDT, &uc);
149 ut_assert(!ret);
150
151 /* These are num_devices compatible root-level device tree nodes */
152 ut_asserteq(num_devices, list_count_items(&uc->dev_head));
153
Simon Glassba750492015-01-25 08:27:00 -0700154 /* Each should have platform data but no private data */
Simon Glass40717422014-07-23 06:55:18 -0600155 for (i = 0; i < num_devices; i++) {
156 ret = uclass_find_device(UCLASS_TEST_FDT, i, &dev);
157 ut_assert(!ret);
158 ut_assert(!dev_get_priv(dev));
Simon Glassba750492015-01-25 08:27:00 -0700159 ut_assert(dev->platdata);
Simon Glass40717422014-07-23 06:55:18 -0600160 }
161
Joe Hershberger3a77be52015-05-20 14:27:27 -0500162 ut_assertok(dm_check_devices(uts, num_devices));
Simon Glass40717422014-07-23 06:55:18 -0600163
Simon Glassb2c1cac2014-02-26 15:59:21 -0700164 return 0;
165}
166DM_TEST(dm_test_fdt, 0);
Simon Glassfef72b72014-07-23 06:55:03 -0600167
Joe Hershberger3a77be52015-05-20 14:27:27 -0500168static int dm_test_fdt_pre_reloc(struct unit_test_state *uts)
Simon Glassfef72b72014-07-23 06:55:03 -0600169{
170 struct uclass *uc;
171 int ret;
172
173 ret = dm_scan_fdt(gd->fdt_blob, true);
174 ut_assert(!ret);
175
176 ret = uclass_get(UCLASS_TEST_FDT, &uc);
177 ut_assert(!ret);
178
179 /* These is only one pre-reloc device */
180 ut_asserteq(1, list_count_items(&uc->dev_head));
181
182 return 0;
183}
184DM_TEST(dm_test_fdt_pre_reloc, 0);
Simon Glassdb6f0202014-07-23 06:55:12 -0600185
186/* Test that sequence numbers are allocated properly */
Joe Hershberger3a77be52015-05-20 14:27:27 -0500187static int dm_test_fdt_uclass_seq(struct unit_test_state *uts)
Simon Glassdb6f0202014-07-23 06:55:12 -0600188{
189 struct udevice *dev;
190
191 /* A few basic santiy tests */
192 ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_FDT, 3, true, &dev));
193 ut_asserteq_str("b-test", dev->name);
194
Simon Glass0ccb0972015-01-25 08:27:05 -0700195 ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_FDT, 8, true, &dev));
Simon Glassdb6f0202014-07-23 06:55:12 -0600196 ut_asserteq_str("a-test", dev->name);
197
198 ut_asserteq(-ENODEV, uclass_find_device_by_seq(UCLASS_TEST_FDT, 5,
199 true, &dev));
200 ut_asserteq_ptr(NULL, dev);
201
202 /* Test aliases */
203 ut_assertok(uclass_get_device_by_seq(UCLASS_TEST_FDT, 6, &dev));
204 ut_asserteq_str("e-test", dev->name);
205
206 ut_asserteq(-ENODEV, uclass_find_device_by_seq(UCLASS_TEST_FDT, 7,
207 true, &dev));
208
Simon Glass40717422014-07-23 06:55:18 -0600209 /*
210 * Note that c-test nodes are not probed since it is not a top-level
211 * node
212 */
Simon Glassdb6f0202014-07-23 06:55:12 -0600213 ut_assertok(uclass_get_device_by_seq(UCLASS_TEST_FDT, 3, &dev));
214 ut_asserteq_str("b-test", dev->name);
215
216 /*
217 * d-test wants sequence number 3 also, but it can't have it because
218 * b-test gets it first.
219 */
220 ut_assertok(uclass_get_device(UCLASS_TEST_FDT, 2, &dev));
221 ut_asserteq_str("d-test", dev->name);
222
223 /* d-test actually gets 0 */
224 ut_assertok(uclass_get_device_by_seq(UCLASS_TEST_FDT, 0, &dev));
225 ut_asserteq_str("d-test", dev->name);
226
227 /* initially no one wants seq 1 */
228 ut_asserteq(-ENODEV, uclass_get_device_by_seq(UCLASS_TEST_FDT, 1,
229 &dev));
230 ut_assertok(uclass_get_device(UCLASS_TEST_FDT, 0, &dev));
Simon Glass0ccb0972015-01-25 08:27:05 -0700231 ut_assertok(uclass_get_device(UCLASS_TEST_FDT, 4, &dev));
Simon Glassdb6f0202014-07-23 06:55:12 -0600232
233 /* But now that it is probed, we can find it */
234 ut_assertok(uclass_get_device_by_seq(UCLASS_TEST_FDT, 1, &dev));
Simon Glass0ccb0972015-01-25 08:27:05 -0700235 ut_asserteq_str("f-test", dev->name);
Simon Glassdb6f0202014-07-23 06:55:12 -0600236
237 return 0;
238}
239DM_TEST(dm_test_fdt_uclass_seq, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
Simon Glassc1464ab2014-07-23 06:55:14 -0600240
241/* Test that we can find a device by device tree offset */
Joe Hershberger3a77be52015-05-20 14:27:27 -0500242static int dm_test_fdt_offset(struct unit_test_state *uts)
Simon Glassc1464ab2014-07-23 06:55:14 -0600243{
244 const void *blob = gd->fdt_blob;
245 struct udevice *dev;
246 int node;
247
248 node = fdt_path_offset(blob, "/e-test");
249 ut_assert(node > 0);
250 ut_assertok(uclass_get_device_by_of_offset(UCLASS_TEST_FDT, node,
251 &dev));
252 ut_asserteq_str("e-test", dev->name);
253
254 /* This node should not be bound */
255 node = fdt_path_offset(blob, "/junk");
256 ut_assert(node > 0);
257 ut_asserteq(-ENODEV, uclass_get_device_by_of_offset(UCLASS_TEST_FDT,
258 node, &dev));
259
260 /* This is not a top level node so should not be probed */
Simon Glass40717422014-07-23 06:55:18 -0600261 node = fdt_path_offset(blob, "/some-bus/c-test@5");
Simon Glassc1464ab2014-07-23 06:55:14 -0600262 ut_assert(node > 0);
263 ut_asserteq(-ENODEV, uclass_get_device_by_of_offset(UCLASS_TEST_FDT,
264 node, &dev));
265
266 return 0;
267}
Simon Glass70b66e72017-05-18 20:09:44 -0600268DM_TEST(dm_test_fdt_offset,
269 DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT | DM_TESTF_FLAT_TREE);