blob: 79b1f1de452dde1ac23f2e0a2779995eec884142 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Simon Glassb2c1cac2014-02-26 15:59:21 -07002/*
3 * Copyright (c) 2013 Google, Inc
Simon Glassb2c1cac2014-02-26 15:59:21 -07004 */
5
6#include <common.h>
7#include <dm.h>
8#include <errno.h>
9#include <fdtdec.h>
10#include <malloc.h>
11#include <asm/io.h>
12#include <dm/test.h>
13#include <dm/root.h>
Simon Glass9c433fe2017-04-23 20:10:44 -060014#include <dm/device-internal.h>
Simon Glassb2c1cac2014-02-26 15:59:21 -070015#include <dm/uclass-internal.h>
16#include <dm/util.h>
Mario Six058ef5b2018-06-26 08:46:49 +020017#include <dm/lists.h>
18#include <dm/of_access.h>
Joe Hershberger3a77be52015-05-20 14:27:27 -050019#include <test/ut.h>
Simon Glassb2c1cac2014-02-26 15:59:21 -070020
21DECLARE_GLOBAL_DATA_PTR;
22
Heiko Schocherb74fcb42014-05-22 12:43:05 +020023static int testfdt_drv_ping(struct udevice *dev, int pingval, int *pingret)
Simon Glassb2c1cac2014-02-26 15:59:21 -070024{
25 const struct dm_test_pdata *pdata = dev->platdata;
26 struct dm_test_priv *priv = dev_get_priv(dev);
27
28 *pingret = pingval + pdata->ping_add;
29 priv->ping_total += *pingret;
30
31 return 0;
32}
33
34static const struct test_ops test_ops = {
35 .ping = testfdt_drv_ping,
36};
37
Heiko Schocherb74fcb42014-05-22 12:43:05 +020038static int testfdt_ofdata_to_platdata(struct udevice *dev)
Simon Glassb2c1cac2014-02-26 15:59:21 -070039{
40 struct dm_test_pdata *pdata = dev_get_platdata(dev);
41
Simon Glassdd79d6e2017-01-17 16:52:55 -070042 pdata->ping_add = fdtdec_get_int(gd->fdt_blob, dev_of_offset(dev),
Simon Glassb2c1cac2014-02-26 15:59:21 -070043 "ping-add", -1);
Simon Glassdd79d6e2017-01-17 16:52:55 -070044 pdata->base = fdtdec_get_addr(gd->fdt_blob, dev_of_offset(dev),
Simon Glassa7bb08a2014-07-23 06:54:57 -060045 "ping-expect");
Simon Glassb2c1cac2014-02-26 15:59:21 -070046
47 return 0;
48}
49
Heiko Schocherb74fcb42014-05-22 12:43:05 +020050static int testfdt_drv_probe(struct udevice *dev)
Simon Glassb2c1cac2014-02-26 15:59:21 -070051{
52 struct dm_test_priv *priv = dev_get_priv(dev);
53
54 priv->ping_total += DM_TEST_START_TOTAL;
55
Simon Glass5104b982015-01-25 08:27:10 -070056 /*
57 * If this device is on a bus, the uclass_flag will be set before
58 * calling this function. This is used by
59 * dm_test_bus_child_pre_probe_uclass().
60 */
61 priv->uclass_total += priv->uclass_flag;
62
Simon Glassb2c1cac2014-02-26 15:59:21 -070063 return 0;
64}
65
Simon Glass767827a2014-06-11 23:29:45 -060066static const struct udevice_id testfdt_ids[] = {
Simon Glassb2c1cac2014-02-26 15:59:21 -070067 {
68 .compatible = "denx,u-boot-fdt-test",
69 .data = DM_TEST_TYPE_FIRST },
70 {
71 .compatible = "google,another-fdt-test",
72 .data = DM_TEST_TYPE_SECOND },
73 { }
74};
75
76U_BOOT_DRIVER(testfdt_drv) = {
77 .name = "testfdt_drv",
78 .of_match = testfdt_ids,
79 .id = UCLASS_TEST_FDT,
80 .ofdata_to_platdata = testfdt_ofdata_to_platdata,
81 .probe = testfdt_drv_probe,
82 .ops = &test_ops,
83 .priv_auto_alloc_size = sizeof(struct dm_test_priv),
84 .platdata_auto_alloc_size = sizeof(struct dm_test_pdata),
85};
86
87/* From here is the testfdt uclass code */
Heiko Schocherb74fcb42014-05-22 12:43:05 +020088int testfdt_ping(struct udevice *dev, int pingval, int *pingret)
Simon Glassb2c1cac2014-02-26 15:59:21 -070089{
90 const struct test_ops *ops = device_get_ops(dev);
91
92 if (!ops->ping)
93 return -ENOSYS;
94
95 return ops->ping(dev, pingval, pingret);
96}
97
98UCLASS_DRIVER(testfdt) = {
99 .name = "testfdt",
100 .id = UCLASS_TEST_FDT,
Simon Glass0ccb0972015-01-25 08:27:05 -0700101 .flags = DM_UC_FLAG_SEQ_ALIAS,
Simon Glassb2c1cac2014-02-26 15:59:21 -0700102};
103
Simon Glass9c433fe2017-04-23 20:10:44 -0600104struct dm_testprobe_pdata {
105 int probe_err;
106};
107
108static int testprobe_drv_probe(struct udevice *dev)
109{
110 struct dm_testprobe_pdata *pdata = dev_get_platdata(dev);
111
112 return pdata->probe_err;
113}
114
115static const struct udevice_id testprobe_ids[] = {
116 { .compatible = "denx,u-boot-probe-test" },
117 { }
118};
119
120U_BOOT_DRIVER(testprobe_drv) = {
121 .name = "testprobe_drv",
122 .of_match = testprobe_ids,
123 .id = UCLASS_TEST_PROBE,
124 .probe = testprobe_drv_probe,
125 .platdata_auto_alloc_size = sizeof(struct dm_testprobe_pdata),
126};
127
128UCLASS_DRIVER(testprobe) = {
129 .name = "testprobe",
130 .id = UCLASS_TEST_PROBE,
131 .flags = DM_UC_FLAG_SEQ_ALIAS,
132};
133
Joe Hershberger3a77be52015-05-20 14:27:27 -0500134int dm_check_devices(struct unit_test_state *uts, int num_devices)
Simon Glassb2c1cac2014-02-26 15:59:21 -0700135{
Heiko Schocherb74fcb42014-05-22 12:43:05 +0200136 struct udevice *dev;
Simon Glassb2c1cac2014-02-26 15:59:21 -0700137 int ret;
138 int i;
139
Simon Glassb2c1cac2014-02-26 15:59:21 -0700140 /*
141 * Now check that the ping adds are what we expect. This is using the
142 * ping-add property in each node.
143 */
Simon Glass40717422014-07-23 06:55:18 -0600144 for (i = 0; i < num_devices; i++) {
Simon Glassb2c1cac2014-02-26 15:59:21 -0700145 uint32_t base;
146
147 ret = uclass_get_device(UCLASS_TEST_FDT, i, &dev);
148 ut_assert(!ret);
149
150 /*
Simon Glassa7bb08a2014-07-23 06:54:57 -0600151 * Get the 'ping-expect' property, which tells us what the
152 * ping add should be. We don't use the platdata because we
153 * want to test the code that sets that up
154 * (testfdt_drv_probe()).
Simon Glassb2c1cac2014-02-26 15:59:21 -0700155 */
Simon Glassdd79d6e2017-01-17 16:52:55 -0700156 base = fdtdec_get_addr(gd->fdt_blob, dev_of_offset(dev),
Simon Glassa7bb08a2014-07-23 06:54:57 -0600157 "ping-expect");
Simon Glassb2c1cac2014-02-26 15:59:21 -0700158 debug("dev=%d, base=%d: %s\n", i, base,
Simon Glassdd79d6e2017-01-17 16:52:55 -0700159 fdt_get_name(gd->fdt_blob, dev_of_offset(dev), NULL));
Simon Glassb2c1cac2014-02-26 15:59:21 -0700160
Joe Hershberger3a77be52015-05-20 14:27:27 -0500161 ut_assert(!dm_check_operations(uts, dev, base,
Simon Glassb2c1cac2014-02-26 15:59:21 -0700162 dev_get_priv(dev)));
163 }
164
Simon Glass40717422014-07-23 06:55:18 -0600165 return 0;
166}
167
168/* Test that FDT-based binding works correctly */
Joe Hershberger3a77be52015-05-20 14:27:27 -0500169static int dm_test_fdt(struct unit_test_state *uts)
Simon Glass40717422014-07-23 06:55:18 -0600170{
Rob Clarka471b672018-01-10 11:33:30 +0100171 const int num_devices = 7;
Simon Glass40717422014-07-23 06:55:18 -0600172 struct udevice *dev;
173 struct uclass *uc;
174 int ret;
175 int i;
176
177 ret = dm_scan_fdt(gd->fdt_blob, false);
178 ut_assert(!ret);
179
180 ret = uclass_get(UCLASS_TEST_FDT, &uc);
181 ut_assert(!ret);
182
183 /* These are num_devices compatible root-level device tree nodes */
184 ut_asserteq(num_devices, list_count_items(&uc->dev_head));
185
Simon Glassba750492015-01-25 08:27:00 -0700186 /* Each should have platform data but no private data */
Simon Glass40717422014-07-23 06:55:18 -0600187 for (i = 0; i < num_devices; i++) {
188 ret = uclass_find_device(UCLASS_TEST_FDT, i, &dev);
189 ut_assert(!ret);
190 ut_assert(!dev_get_priv(dev));
Simon Glassba750492015-01-25 08:27:00 -0700191 ut_assert(dev->platdata);
Simon Glass40717422014-07-23 06:55:18 -0600192 }
193
Joe Hershberger3a77be52015-05-20 14:27:27 -0500194 ut_assertok(dm_check_devices(uts, num_devices));
Simon Glass40717422014-07-23 06:55:18 -0600195
Simon Glassb2c1cac2014-02-26 15:59:21 -0700196 return 0;
197}
198DM_TEST(dm_test_fdt, 0);
Simon Glassfef72b72014-07-23 06:55:03 -0600199
Joe Hershberger3a77be52015-05-20 14:27:27 -0500200static int dm_test_fdt_pre_reloc(struct unit_test_state *uts)
Simon Glassfef72b72014-07-23 06:55:03 -0600201{
202 struct uclass *uc;
203 int ret;
204
205 ret = dm_scan_fdt(gd->fdt_blob, true);
206 ut_assert(!ret);
207
208 ret = uclass_get(UCLASS_TEST_FDT, &uc);
209 ut_assert(!ret);
210
211 /* These is only one pre-reloc device */
212 ut_asserteq(1, list_count_items(&uc->dev_head));
213
214 return 0;
215}
216DM_TEST(dm_test_fdt_pre_reloc, 0);
Simon Glassdb6f0202014-07-23 06:55:12 -0600217
218/* Test that sequence numbers are allocated properly */
Joe Hershberger3a77be52015-05-20 14:27:27 -0500219static int dm_test_fdt_uclass_seq(struct unit_test_state *uts)
Simon Glassdb6f0202014-07-23 06:55:12 -0600220{
221 struct udevice *dev;
222
223 /* A few basic santiy tests */
224 ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_FDT, 3, true, &dev));
225 ut_asserteq_str("b-test", dev->name);
226
Simon Glass0ccb0972015-01-25 08:27:05 -0700227 ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_FDT, 8, true, &dev));
Simon Glassdb6f0202014-07-23 06:55:12 -0600228 ut_asserteq_str("a-test", dev->name);
229
230 ut_asserteq(-ENODEV, uclass_find_device_by_seq(UCLASS_TEST_FDT, 5,
231 true, &dev));
232 ut_asserteq_ptr(NULL, dev);
233
234 /* Test aliases */
235 ut_assertok(uclass_get_device_by_seq(UCLASS_TEST_FDT, 6, &dev));
236 ut_asserteq_str("e-test", dev->name);
237
238 ut_asserteq(-ENODEV, uclass_find_device_by_seq(UCLASS_TEST_FDT, 7,
239 true, &dev));
240
Simon Glass40717422014-07-23 06:55:18 -0600241 /*
242 * Note that c-test nodes are not probed since it is not a top-level
243 * node
244 */
Simon Glassdb6f0202014-07-23 06:55:12 -0600245 ut_assertok(uclass_get_device_by_seq(UCLASS_TEST_FDT, 3, &dev));
246 ut_asserteq_str("b-test", dev->name);
247
248 /*
249 * d-test wants sequence number 3 also, but it can't have it because
250 * b-test gets it first.
251 */
252 ut_assertok(uclass_get_device(UCLASS_TEST_FDT, 2, &dev));
253 ut_asserteq_str("d-test", dev->name);
254
255 /* d-test actually gets 0 */
256 ut_assertok(uclass_get_device_by_seq(UCLASS_TEST_FDT, 0, &dev));
257 ut_asserteq_str("d-test", dev->name);
258
259 /* initially no one wants seq 1 */
260 ut_asserteq(-ENODEV, uclass_get_device_by_seq(UCLASS_TEST_FDT, 1,
261 &dev));
262 ut_assertok(uclass_get_device(UCLASS_TEST_FDT, 0, &dev));
Simon Glass0ccb0972015-01-25 08:27:05 -0700263 ut_assertok(uclass_get_device(UCLASS_TEST_FDT, 4, &dev));
Simon Glassdb6f0202014-07-23 06:55:12 -0600264
265 /* But now that it is probed, we can find it */
266 ut_assertok(uclass_get_device_by_seq(UCLASS_TEST_FDT, 1, &dev));
Simon Glass0ccb0972015-01-25 08:27:05 -0700267 ut_asserteq_str("f-test", dev->name);
Simon Glassdb6f0202014-07-23 06:55:12 -0600268
269 return 0;
270}
271DM_TEST(dm_test_fdt_uclass_seq, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
Simon Glassc1464ab2014-07-23 06:55:14 -0600272
273/* Test that we can find a device by device tree offset */
Joe Hershberger3a77be52015-05-20 14:27:27 -0500274static int dm_test_fdt_offset(struct unit_test_state *uts)
Simon Glassc1464ab2014-07-23 06:55:14 -0600275{
276 const void *blob = gd->fdt_blob;
277 struct udevice *dev;
278 int node;
279
280 node = fdt_path_offset(blob, "/e-test");
281 ut_assert(node > 0);
282 ut_assertok(uclass_get_device_by_of_offset(UCLASS_TEST_FDT, node,
283 &dev));
284 ut_asserteq_str("e-test", dev->name);
285
286 /* This node should not be bound */
287 node = fdt_path_offset(blob, "/junk");
288 ut_assert(node > 0);
289 ut_asserteq(-ENODEV, uclass_get_device_by_of_offset(UCLASS_TEST_FDT,
290 node, &dev));
291
292 /* This is not a top level node so should not be probed */
Simon Glass40717422014-07-23 06:55:18 -0600293 node = fdt_path_offset(blob, "/some-bus/c-test@5");
Simon Glassc1464ab2014-07-23 06:55:14 -0600294 ut_assert(node > 0);
295 ut_asserteq(-ENODEV, uclass_get_device_by_of_offset(UCLASS_TEST_FDT,
296 node, &dev));
297
298 return 0;
299}
Simon Glass70b66e72017-05-18 20:09:44 -0600300DM_TEST(dm_test_fdt_offset,
301 DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT | DM_TESTF_FLAT_TREE);
Simon Glass9c433fe2017-04-23 20:10:44 -0600302
303/**
304 * Test various error conditions with uclass_first_device() and
305 * uclass_next_device()
306 */
307static int dm_test_first_next_device(struct unit_test_state *uts)
308{
309 struct dm_testprobe_pdata *pdata;
310 struct udevice *dev, *parent = NULL;
311 int count;
312 int ret;
313
314 /* There should be 4 devices */
315 for (ret = uclass_first_device(UCLASS_TEST_PROBE, &dev), count = 0;
316 dev;
317 ret = uclass_next_device(&dev)) {
318 count++;
319 parent = dev_get_parent(dev);
320 }
321 ut_assertok(ret);
322 ut_asserteq(4, count);
323
324 /* Remove them and try again, with an error on the second one */
325 ut_assertok(uclass_get_device(UCLASS_TEST_PROBE, 1, &dev));
326 pdata = dev_get_platdata(dev);
327 pdata->probe_err = -ENOMEM;
328 device_remove(parent, DM_REMOVE_NORMAL);
329 ut_assertok(uclass_first_device(UCLASS_TEST_PROBE, &dev));
330 ut_asserteq(-ENOMEM, uclass_next_device(&dev));
331 ut_asserteq_ptr(dev, NULL);
332
333 /* Now an error on the first one */
334 ut_assertok(uclass_get_device(UCLASS_TEST_PROBE, 0, &dev));
335 pdata = dev_get_platdata(dev);
336 pdata->probe_err = -ENOENT;
337 device_remove(parent, DM_REMOVE_NORMAL);
338 ut_asserteq(-ENOENT, uclass_first_device(UCLASS_TEST_PROBE, &dev));
339
340 return 0;
341}
342DM_TEST(dm_test_first_next_device, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
Simon Glass3ff83bc2017-04-23 20:10:45 -0600343
344/**
345 * check_devices() - Check return values and pointers
346 *
347 * This runs through a full sequence of uclass_first_device_check()...
348 * uclass_next_device_check() checking that the return values and devices
349 * are correct.
350 *
351 * @uts: Test state
352 * @devlist: List of expected devices
353 * @mask: Indicates which devices should return an error. Device n should
354 * return error (-NOENT - n) if bit n is set, or no error (i.e. 0) if
355 * bit n is clear.
356 */
357static int check_devices(struct unit_test_state *uts,
358 struct udevice *devlist[], int mask)
359{
360 int expected_ret;
361 struct udevice *dev;
362 int i;
363
364 expected_ret = (mask & 1) ? -ENOENT : 0;
365 mask >>= 1;
366 ut_asserteq(expected_ret,
367 uclass_first_device_check(UCLASS_TEST_PROBE, &dev));
368 for (i = 0; i < 4; i++) {
369 ut_asserteq_ptr(devlist[i], dev);
370 expected_ret = (mask & 1) ? -ENOENT - (i + 1) : 0;
371 mask >>= 1;
372 ut_asserteq(expected_ret, uclass_next_device_check(&dev));
373 }
374 ut_asserteq_ptr(NULL, dev);
375
376 return 0;
377}
378
379/* Test uclass_first_device_check() and uclass_next_device_check() */
380static int dm_test_first_next_ok_device(struct unit_test_state *uts)
381{
382 struct dm_testprobe_pdata *pdata;
383 struct udevice *dev, *parent = NULL, *devlist[4];
384 int count;
385 int ret;
386
387 /* There should be 4 devices */
388 count = 0;
389 for (ret = uclass_first_device_check(UCLASS_TEST_PROBE, &dev);
390 dev;
391 ret = uclass_next_device_check(&dev)) {
392 ut_assertok(ret);
393 devlist[count++] = dev;
394 parent = dev_get_parent(dev);
395 }
396 ut_asserteq(4, count);
397 ut_assertok(uclass_first_device_check(UCLASS_TEST_PROBE, &dev));
398 ut_assertok(check_devices(uts, devlist, 0));
399
400 /* Remove them and try again, with an error on the second one */
401 pdata = dev_get_platdata(devlist[1]);
402 pdata->probe_err = -ENOENT - 1;
403 device_remove(parent, DM_REMOVE_NORMAL);
404 ut_assertok(check_devices(uts, devlist, 1 << 1));
405
406 /* Now an error on the first one */
407 pdata = dev_get_platdata(devlist[0]);
408 pdata->probe_err = -ENOENT - 0;
409 device_remove(parent, DM_REMOVE_NORMAL);
410 ut_assertok(check_devices(uts, devlist, 3 << 0));
411
412 /* Now errors on all */
413 pdata = dev_get_platdata(devlist[2]);
414 pdata->probe_err = -ENOENT - 2;
415 pdata = dev_get_platdata(devlist[3]);
416 pdata->probe_err = -ENOENT - 3;
417 device_remove(parent, DM_REMOVE_NORMAL);
418 ut_assertok(check_devices(uts, devlist, 0xf << 0));
419
420 return 0;
421}
422DM_TEST(dm_test_first_next_ok_device, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
Mario Six35616ef2018-03-12 14:53:33 +0100423
424static const struct udevice_id fdt_dummy_ids[] = {
425 { .compatible = "denx,u-boot-fdt-dummy", },
426 { }
427};
428
429UCLASS_DRIVER(fdt_dummy) = {
Eugeniu Rosca5ba71e52018-05-19 14:13:55 +0200430 .name = "fdt-dummy",
Mario Six35616ef2018-03-12 14:53:33 +0100431 .id = UCLASS_TEST_DUMMY,
432 .flags = DM_UC_FLAG_SEQ_ALIAS,
433};
434
435U_BOOT_DRIVER(fdt_dummy_drv) = {
436 .name = "fdt_dummy_drv",
437 .of_match = fdt_dummy_ids,
438 .id = UCLASS_TEST_DUMMY,
439};
440
441static int dm_test_fdt_translation(struct unit_test_state *uts)
442{
443 struct udevice *dev;
444
445 /* Some simple translations */
446 ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_DUMMY, 0, true, &dev));
447 ut_asserteq_str("dev@0,0", dev->name);
448 ut_asserteq(0x8000, dev_read_addr(dev));
449
450 ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_DUMMY, 1, true, &dev));
451 ut_asserteq_str("dev@1,100", dev->name);
452 ut_asserteq(0x9000, dev_read_addr(dev));
453
454 ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_DUMMY, 2, true, &dev));
455 ut_asserteq_str("dev@2,200", dev->name);
456 ut_asserteq(0xA000, dev_read_addr(dev));
457
458 /* No translation for busses with #size-cells == 0 */
459 ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_DUMMY, 3, true, &dev));
460 ut_asserteq_str("dev@42", dev->name);
461 ut_asserteq(0x42, dev_read_addr(dev));
462
463 return 0;
464}
465DM_TEST(dm_test_fdt_translation, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
Álvaro Fernández Rojas670361292018-04-29 21:56:54 +0200466
467/* Test devfdt_remap_addr_index() */
468static int dm_test_fdt_remap_addr_flat(struct unit_test_state *uts)
469{
470 struct udevice *dev;
471 fdt_addr_t addr;
472 void *paddr;
473
474 ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_DUMMY, 0, true, &dev));
475
476 addr = devfdt_get_addr(dev);
477 ut_asserteq(0x8000, addr);
478
479 paddr = map_physmem(addr, 0, MAP_NOCACHE);
480 ut_assertnonnull(paddr);
481 ut_asserteq_ptr(paddr, devfdt_remap_addr(dev));
482
483 return 0;
484}
485DM_TEST(dm_test_fdt_remap_addr_flat,
486 DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT | DM_TESTF_FLAT_TREE);
487
488/* Test dev_remap_addr_index() */
489static int dm_test_fdt_remap_addr_live(struct unit_test_state *uts)
490{
491 struct udevice *dev;
492 fdt_addr_t addr;
493 void *paddr;
494
495 ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_DUMMY, 0, true, &dev));
496
497 addr = dev_read_addr(dev);
498 ut_asserteq(0x8000, addr);
499
500 paddr = map_physmem(addr, 0, MAP_NOCACHE);
501 ut_assertnonnull(paddr);
502 ut_asserteq_ptr(paddr, dev_remap_addr(dev));
503
504 return 0;
505}
506DM_TEST(dm_test_fdt_remap_addr_live,
507 DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
Mario Six058ef5b2018-06-26 08:46:49 +0200508
509static int dm_test_fdt_livetree_writing(struct unit_test_state *uts)
510{
511 struct udevice *dev;
512 ofnode node;
513
514 if (!of_live_active()) {
515 printf("Live tree not active; ignore test\n");
516 return 0;
517 }
518
519 /* Test enabling devices */
520
521 node = ofnode_path("/usb@2");
522
523 ut_assert(!of_device_is_available(ofnode_to_np(node)));
524 ofnode_set_enabled(node, true);
525 ut_assert(of_device_is_available(ofnode_to_np(node)));
526
527 device_bind_driver_to_node(dm_root(), "usb_sandbox", "usb@2", node,
528 &dev);
529 ut_assertok(uclass_find_device_by_seq(UCLASS_USB, 2, true, &dev));
530
531 /* Test string property setting */
532
533 ut_assert(device_is_compatible(dev, "sandbox,usb"));
534 ofnode_write_string(node, "compatible", "gdsys,super-usb");
535 ut_assert(device_is_compatible(dev, "gdsys,super-usb"));
536 ofnode_write_string(node, "compatible", "sandbox,usb");
537 ut_assert(device_is_compatible(dev, "sandbox,usb"));
538
539 /* Test setting generic properties */
540
541 /* Non-existent in DTB */
542 ut_asserteq(FDT_ADDR_T_NONE, dev_read_addr(dev));
543 /* reg = 0x42, size = 0x100 */
544 ut_assertok(ofnode_write_prop(node, "reg", 8,
545 "\x00\x00\x00\x42\x00\x00\x01\x00"));
546 ut_asserteq(0x42, dev_read_addr(dev));
547
548 /* Test disabling devices */
549
550 device_remove(dev, DM_REMOVE_NORMAL);
551 device_unbind(dev);
552
553 ut_assert(of_device_is_available(ofnode_to_np(node)));
554 ofnode_set_enabled(node, false);
555 ut_assert(!of_device_is_available(ofnode_to_np(node)));
556
557 return 0;
558}
559DM_TEST(dm_test_fdt_livetree_writing, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
Mario Sixd6f6dc32018-06-26 08:46:51 +0200560
561static int dm_test_fdt_disable_enable_by_path(struct unit_test_state *uts)
562{
563 ofnode node;
564
565 if (!of_live_active()) {
566 printf("Live tree not active; ignore test\n");
567 return 0;
568 }
569
570 node = ofnode_path("/usb@2");
571
572 /* Test enabling devices */
573
574 ut_assert(!of_device_is_available(ofnode_to_np(node)));
575 dev_enable_by_path("/usb@2");
576 ut_assert(of_device_is_available(ofnode_to_np(node)));
577
578 /* Test disabling devices */
579
580 ut_assert(of_device_is_available(ofnode_to_np(node)));
581 dev_disable_by_path("/usb@2");
582 ut_assert(!of_device_is_available(ofnode_to_np(node)));
583
584 return 0;
585}
586DM_TEST(dm_test_fdt_disable_enable_by_path, DM_TESTF_SCAN_PDATA |
587 DM_TESTF_SCAN_FDT);