blob: 49a36cb1932e67794d5f55ec7359e2c54920885a [file] [log] [blame]
Simon Glass2e7d35d2014-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 Glass2e7d35d2014-02-26 15:59:21 -070015#include <dm/uclass-internal.h>
16#include <dm/util.h>
Joe Hershbergere721b882015-05-20 14:27:27 -050017#include <test/ut.h>
Simon Glass2e7d35d2014-02-26 15:59:21 -070018
19DECLARE_GLOBAL_DATA_PTR;
20
Heiko Schocher54c5d082014-05-22 12:43:05 +020021static int testfdt_drv_ping(struct udevice *dev, int pingval, int *pingret)
Simon Glass2e7d35d2014-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 Schocher54c5d082014-05-22 12:43:05 +020036static int testfdt_ofdata_to_platdata(struct udevice *dev)
Simon Glass2e7d35d2014-02-26 15:59:21 -070037{
38 struct dm_test_pdata *pdata = dev_get_platdata(dev);
39
40 pdata->ping_add = fdtdec_get_int(gd->fdt_blob, dev->of_offset,
41 "ping-add", -1);
Simon Glasseb9ef5f2014-07-23 06:54:57 -060042 pdata->base = fdtdec_get_addr(gd->fdt_blob, dev->of_offset,
43 "ping-expect");
Simon Glass2e7d35d2014-02-26 15:59:21 -070044
45 return 0;
46}
47
Heiko Schocher54c5d082014-05-22 12:43:05 +020048static int testfdt_drv_probe(struct udevice *dev)
Simon Glass2e7d35d2014-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 Glass83c7e432015-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 Glass2e7d35d2014-02-26 15:59:21 -070061 return 0;
62}
63
Simon Glassae7f4512014-06-11 23:29:45 -060064static const struct udevice_id testfdt_ids[] = {
Simon Glass2e7d35d2014-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 Schocher54c5d082014-05-22 12:43:05 +020086int testfdt_ping(struct udevice *dev, int pingval, int *pingret)
Simon Glass2e7d35d2014-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 Glass9cc36a22015-01-25 08:27:05 -070099 .flags = DM_UC_FLAG_SEQ_ALIAS,
Simon Glass2e7d35d2014-02-26 15:59:21 -0700100};
101
Joe Hershbergere721b882015-05-20 14:27:27 -0500102int dm_check_devices(struct unit_test_state *uts, int num_devices)
Simon Glass2e7d35d2014-02-26 15:59:21 -0700103{
Heiko Schocher54c5d082014-05-22 12:43:05 +0200104 struct udevice *dev;
Simon Glass2e7d35d2014-02-26 15:59:21 -0700105 int ret;
106 int i;
107
Simon Glass2e7d35d2014-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 Glass1ca7e202014-07-23 06:55:18 -0600112 for (i = 0; i < num_devices; i++) {
Simon Glass2e7d35d2014-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 Glasseb9ef5f2014-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 Glass2e7d35d2014-02-26 15:59:21 -0700123 */
Simon Glasseb9ef5f2014-07-23 06:54:57 -0600124 base = fdtdec_get_addr(gd->fdt_blob, dev->of_offset,
125 "ping-expect");
Simon Glass2e7d35d2014-02-26 15:59:21 -0700126 debug("dev=%d, base=%d: %s\n", i, base,
127 fdt_get_name(gd->fdt_blob, dev->of_offset, NULL));
128
Joe Hershbergere721b882015-05-20 14:27:27 -0500129 ut_assert(!dm_check_operations(uts, dev, base,
Simon Glass2e7d35d2014-02-26 15:59:21 -0700130 dev_get_priv(dev)));
131 }
132
133 return 0;
134}
Simon Glass1ca7e202014-07-23 06:55:18 -0600135
136/* Test that FDT-based binding works correctly */
Joe Hershbergere721b882015-05-20 14:27:27 -0500137static int dm_test_fdt(struct unit_test_state *uts)
Simon Glass1ca7e202014-07-23 06:55:18 -0600138{
Simon Glass9cc36a22015-01-25 08:27:05 -0700139 const int num_devices = 6;
Simon Glass1ca7e202014-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 Glassf8a85442015-01-25 08:27:00 -0700154 /* Each should have platform data but no private data */
Simon Glass1ca7e202014-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 Glassf8a85442015-01-25 08:27:00 -0700159 ut_assert(dev->platdata);
Simon Glass1ca7e202014-07-23 06:55:18 -0600160 }
161
Joe Hershbergere721b882015-05-20 14:27:27 -0500162 ut_assertok(dm_check_devices(uts, num_devices));
Simon Glass1ca7e202014-07-23 06:55:18 -0600163
164 return 0;
165}
Simon Glass2e7d35d2014-02-26 15:59:21 -0700166DM_TEST(dm_test_fdt, 0);
Simon Glass00606d72014-07-23 06:55:03 -0600167
Joe Hershbergere721b882015-05-20 14:27:27 -0500168static int dm_test_fdt_pre_reloc(struct unit_test_state *uts)
Simon Glass00606d72014-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 Glass5a66a8f2014-07-23 06:55:12 -0600185
186/* Test that sequence numbers are allocated properly */
Joe Hershbergere721b882015-05-20 14:27:27 -0500187static int dm_test_fdt_uclass_seq(struct unit_test_state *uts)
Simon Glass5a66a8f2014-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 Glass9cc36a22015-01-25 08:27:05 -0700195 ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_FDT, 8, true, &dev));
Simon Glass5a66a8f2014-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 Glass1ca7e202014-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 Glass5a66a8f2014-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 Glass9cc36a22015-01-25 08:27:05 -0700231 ut_assertok(uclass_get_device(UCLASS_TEST_FDT, 4, &dev));
Simon Glass5a66a8f2014-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 Glass9cc36a22015-01-25 08:27:05 -0700235 ut_asserteq_str("f-test", dev->name);
Simon Glass5a66a8f2014-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 Glassf4cdead2014-07-23 06:55:14 -0600240
241/* Test that we can find a device by device tree offset */
Joe Hershbergere721b882015-05-20 14:27:27 -0500242static int dm_test_fdt_offset(struct unit_test_state *uts)
Simon Glassf4cdead2014-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 Glass1ca7e202014-07-23 06:55:18 -0600261 node = fdt_path_offset(blob, "/some-bus/c-test@5");
Simon Glassf4cdead2014-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 Glassf4cdead2014-07-23 06:55:14 -0600268DM_TEST(dm_test_fdt_offset, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);