blob: 8196844e89a7efecb5f2a998015b19420e7232da [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Simon Glass2e7d35d2014-02-26 15:59:21 -07002/*
3 * Copyright (c) 2013 Google, Inc
Simon Glass2e7d35d2014-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 Glass98561572017-04-23 20:10:44 -060014#include <dm/device-internal.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
Simon Glasse160f7d2017-01-17 16:52:55 -070040 pdata->ping_add = fdtdec_get_int(gd->fdt_blob, dev_of_offset(dev),
Simon Glass2e7d35d2014-02-26 15:59:21 -070041 "ping-add", -1);
Simon Glasse160f7d2017-01-17 16:52:55 -070042 pdata->base = fdtdec_get_addr(gd->fdt_blob, dev_of_offset(dev),
Simon Glasseb9ef5f2014-07-23 06:54:57 -060043 "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
Simon Glass98561572017-04-23 20:10:44 -0600102struct dm_testprobe_pdata {
103 int probe_err;
104};
105
106static int testprobe_drv_probe(struct udevice *dev)
107{
108 struct dm_testprobe_pdata *pdata = dev_get_platdata(dev);
109
110 return pdata->probe_err;
111}
112
113static const struct udevice_id testprobe_ids[] = {
114 { .compatible = "denx,u-boot-probe-test" },
115 { }
116};
117
118U_BOOT_DRIVER(testprobe_drv) = {
119 .name = "testprobe_drv",
120 .of_match = testprobe_ids,
121 .id = UCLASS_TEST_PROBE,
122 .probe = testprobe_drv_probe,
123 .platdata_auto_alloc_size = sizeof(struct dm_testprobe_pdata),
124};
125
126UCLASS_DRIVER(testprobe) = {
127 .name = "testprobe",
128 .id = UCLASS_TEST_PROBE,
129 .flags = DM_UC_FLAG_SEQ_ALIAS,
130};
131
Joe Hershbergere721b882015-05-20 14:27:27 -0500132int dm_check_devices(struct unit_test_state *uts, int num_devices)
Simon Glass2e7d35d2014-02-26 15:59:21 -0700133{
Heiko Schocher54c5d082014-05-22 12:43:05 +0200134 struct udevice *dev;
Simon Glass2e7d35d2014-02-26 15:59:21 -0700135 int ret;
136 int i;
137
Simon Glass2e7d35d2014-02-26 15:59:21 -0700138 /*
139 * Now check that the ping adds are what we expect. This is using the
140 * ping-add property in each node.
141 */
Simon Glass1ca7e202014-07-23 06:55:18 -0600142 for (i = 0; i < num_devices; i++) {
Simon Glass2e7d35d2014-02-26 15:59:21 -0700143 uint32_t base;
144
145 ret = uclass_get_device(UCLASS_TEST_FDT, i, &dev);
146 ut_assert(!ret);
147
148 /*
Simon Glasseb9ef5f2014-07-23 06:54:57 -0600149 * Get the 'ping-expect' property, which tells us what the
150 * ping add should be. We don't use the platdata because we
151 * want to test the code that sets that up
152 * (testfdt_drv_probe()).
Simon Glass2e7d35d2014-02-26 15:59:21 -0700153 */
Simon Glasse160f7d2017-01-17 16:52:55 -0700154 base = fdtdec_get_addr(gd->fdt_blob, dev_of_offset(dev),
Simon Glasseb9ef5f2014-07-23 06:54:57 -0600155 "ping-expect");
Simon Glass2e7d35d2014-02-26 15:59:21 -0700156 debug("dev=%d, base=%d: %s\n", i, base,
Simon Glasse160f7d2017-01-17 16:52:55 -0700157 fdt_get_name(gd->fdt_blob, dev_of_offset(dev), NULL));
Simon Glass2e7d35d2014-02-26 15:59:21 -0700158
Joe Hershbergere721b882015-05-20 14:27:27 -0500159 ut_assert(!dm_check_operations(uts, dev, base,
Simon Glass2e7d35d2014-02-26 15:59:21 -0700160 dev_get_priv(dev)));
161 }
162
163 return 0;
164}
Simon Glass1ca7e202014-07-23 06:55:18 -0600165
166/* Test that FDT-based binding works correctly */
Joe Hershbergere721b882015-05-20 14:27:27 -0500167static int dm_test_fdt(struct unit_test_state *uts)
Simon Glass1ca7e202014-07-23 06:55:18 -0600168{
Rob Clarkf2006802018-01-10 11:33:30 +0100169 const int num_devices = 7;
Simon Glass1ca7e202014-07-23 06:55:18 -0600170 struct udevice *dev;
171 struct uclass *uc;
172 int ret;
173 int i;
174
175 ret = dm_scan_fdt(gd->fdt_blob, false);
176 ut_assert(!ret);
177
178 ret = uclass_get(UCLASS_TEST_FDT, &uc);
179 ut_assert(!ret);
180
181 /* These are num_devices compatible root-level device tree nodes */
182 ut_asserteq(num_devices, list_count_items(&uc->dev_head));
183
Simon Glassf8a85442015-01-25 08:27:00 -0700184 /* Each should have platform data but no private data */
Simon Glass1ca7e202014-07-23 06:55:18 -0600185 for (i = 0; i < num_devices; i++) {
186 ret = uclass_find_device(UCLASS_TEST_FDT, i, &dev);
187 ut_assert(!ret);
188 ut_assert(!dev_get_priv(dev));
Simon Glassf8a85442015-01-25 08:27:00 -0700189 ut_assert(dev->platdata);
Simon Glass1ca7e202014-07-23 06:55:18 -0600190 }
191
Joe Hershbergere721b882015-05-20 14:27:27 -0500192 ut_assertok(dm_check_devices(uts, num_devices));
Simon Glass1ca7e202014-07-23 06:55:18 -0600193
194 return 0;
195}
Simon Glass2e7d35d2014-02-26 15:59:21 -0700196DM_TEST(dm_test_fdt, 0);
Simon Glass00606d72014-07-23 06:55:03 -0600197
Joe Hershbergere721b882015-05-20 14:27:27 -0500198static int dm_test_fdt_pre_reloc(struct unit_test_state *uts)
Simon Glass00606d72014-07-23 06:55:03 -0600199{
200 struct uclass *uc;
201 int ret;
202
203 ret = dm_scan_fdt(gd->fdt_blob, true);
204 ut_assert(!ret);
205
206 ret = uclass_get(UCLASS_TEST_FDT, &uc);
207 ut_assert(!ret);
208
209 /* These is only one pre-reloc device */
210 ut_asserteq(1, list_count_items(&uc->dev_head));
211
212 return 0;
213}
214DM_TEST(dm_test_fdt_pre_reloc, 0);
Simon Glass5a66a8f2014-07-23 06:55:12 -0600215
216/* Test that sequence numbers are allocated properly */
Joe Hershbergere721b882015-05-20 14:27:27 -0500217static int dm_test_fdt_uclass_seq(struct unit_test_state *uts)
Simon Glass5a66a8f2014-07-23 06:55:12 -0600218{
219 struct udevice *dev;
220
221 /* A few basic santiy tests */
222 ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_FDT, 3, true, &dev));
223 ut_asserteq_str("b-test", dev->name);
224
Simon Glass9cc36a22015-01-25 08:27:05 -0700225 ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_FDT, 8, true, &dev));
Simon Glass5a66a8f2014-07-23 06:55:12 -0600226 ut_asserteq_str("a-test", dev->name);
227
228 ut_asserteq(-ENODEV, uclass_find_device_by_seq(UCLASS_TEST_FDT, 5,
229 true, &dev));
230 ut_asserteq_ptr(NULL, dev);
231
232 /* Test aliases */
233 ut_assertok(uclass_get_device_by_seq(UCLASS_TEST_FDT, 6, &dev));
234 ut_asserteq_str("e-test", dev->name);
235
236 ut_asserteq(-ENODEV, uclass_find_device_by_seq(UCLASS_TEST_FDT, 7,
237 true, &dev));
238
Simon Glass1ca7e202014-07-23 06:55:18 -0600239 /*
240 * Note that c-test nodes are not probed since it is not a top-level
241 * node
242 */
Simon Glass5a66a8f2014-07-23 06:55:12 -0600243 ut_assertok(uclass_get_device_by_seq(UCLASS_TEST_FDT, 3, &dev));
244 ut_asserteq_str("b-test", dev->name);
245
246 /*
247 * d-test wants sequence number 3 also, but it can't have it because
248 * b-test gets it first.
249 */
250 ut_assertok(uclass_get_device(UCLASS_TEST_FDT, 2, &dev));
251 ut_asserteq_str("d-test", dev->name);
252
253 /* d-test actually gets 0 */
254 ut_assertok(uclass_get_device_by_seq(UCLASS_TEST_FDT, 0, &dev));
255 ut_asserteq_str("d-test", dev->name);
256
257 /* initially no one wants seq 1 */
258 ut_asserteq(-ENODEV, uclass_get_device_by_seq(UCLASS_TEST_FDT, 1,
259 &dev));
260 ut_assertok(uclass_get_device(UCLASS_TEST_FDT, 0, &dev));
Simon Glass9cc36a22015-01-25 08:27:05 -0700261 ut_assertok(uclass_get_device(UCLASS_TEST_FDT, 4, &dev));
Simon Glass5a66a8f2014-07-23 06:55:12 -0600262
263 /* But now that it is probed, we can find it */
264 ut_assertok(uclass_get_device_by_seq(UCLASS_TEST_FDT, 1, &dev));
Simon Glass9cc36a22015-01-25 08:27:05 -0700265 ut_asserteq_str("f-test", dev->name);
Simon Glass5a66a8f2014-07-23 06:55:12 -0600266
267 return 0;
268}
269DM_TEST(dm_test_fdt_uclass_seq, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
Simon Glassf4cdead2014-07-23 06:55:14 -0600270
271/* Test that we can find a device by device tree offset */
Joe Hershbergere721b882015-05-20 14:27:27 -0500272static int dm_test_fdt_offset(struct unit_test_state *uts)
Simon Glassf4cdead2014-07-23 06:55:14 -0600273{
274 const void *blob = gd->fdt_blob;
275 struct udevice *dev;
276 int node;
277
278 node = fdt_path_offset(blob, "/e-test");
279 ut_assert(node > 0);
280 ut_assertok(uclass_get_device_by_of_offset(UCLASS_TEST_FDT, node,
281 &dev));
282 ut_asserteq_str("e-test", dev->name);
283
284 /* This node should not be bound */
285 node = fdt_path_offset(blob, "/junk");
286 ut_assert(node > 0);
287 ut_asserteq(-ENODEV, uclass_get_device_by_of_offset(UCLASS_TEST_FDT,
288 node, &dev));
289
290 /* This is not a top level node so should not be probed */
Simon Glass1ca7e202014-07-23 06:55:18 -0600291 node = fdt_path_offset(blob, "/some-bus/c-test@5");
Simon Glassf4cdead2014-07-23 06:55:14 -0600292 ut_assert(node > 0);
293 ut_asserteq(-ENODEV, uclass_get_device_by_of_offset(UCLASS_TEST_FDT,
294 node, &dev));
295
296 return 0;
297}
Simon Glass86b54ec2017-05-18 20:09:44 -0600298DM_TEST(dm_test_fdt_offset,
299 DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT | DM_TESTF_FLAT_TREE);
Simon Glass98561572017-04-23 20:10:44 -0600300
301/**
302 * Test various error conditions with uclass_first_device() and
303 * uclass_next_device()
304 */
305static int dm_test_first_next_device(struct unit_test_state *uts)
306{
307 struct dm_testprobe_pdata *pdata;
308 struct udevice *dev, *parent = NULL;
309 int count;
310 int ret;
311
312 /* There should be 4 devices */
313 for (ret = uclass_first_device(UCLASS_TEST_PROBE, &dev), count = 0;
314 dev;
315 ret = uclass_next_device(&dev)) {
316 count++;
317 parent = dev_get_parent(dev);
318 }
319 ut_assertok(ret);
320 ut_asserteq(4, count);
321
322 /* Remove them and try again, with an error on the second one */
323 ut_assertok(uclass_get_device(UCLASS_TEST_PROBE, 1, &dev));
324 pdata = dev_get_platdata(dev);
325 pdata->probe_err = -ENOMEM;
326 device_remove(parent, DM_REMOVE_NORMAL);
327 ut_assertok(uclass_first_device(UCLASS_TEST_PROBE, &dev));
328 ut_asserteq(-ENOMEM, uclass_next_device(&dev));
329 ut_asserteq_ptr(dev, NULL);
330
331 /* Now an error on the first one */
332 ut_assertok(uclass_get_device(UCLASS_TEST_PROBE, 0, &dev));
333 pdata = dev_get_platdata(dev);
334 pdata->probe_err = -ENOENT;
335 device_remove(parent, DM_REMOVE_NORMAL);
336 ut_asserteq(-ENOENT, uclass_first_device(UCLASS_TEST_PROBE, &dev));
337
338 return 0;
339}
340DM_TEST(dm_test_first_next_device, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
Simon Glass95ce3852017-04-23 20:10:45 -0600341
342/**
343 * check_devices() - Check return values and pointers
344 *
345 * This runs through a full sequence of uclass_first_device_check()...
346 * uclass_next_device_check() checking that the return values and devices
347 * are correct.
348 *
349 * @uts: Test state
350 * @devlist: List of expected devices
351 * @mask: Indicates which devices should return an error. Device n should
352 * return error (-NOENT - n) if bit n is set, or no error (i.e. 0) if
353 * bit n is clear.
354 */
355static int check_devices(struct unit_test_state *uts,
356 struct udevice *devlist[], int mask)
357{
358 int expected_ret;
359 struct udevice *dev;
360 int i;
361
362 expected_ret = (mask & 1) ? -ENOENT : 0;
363 mask >>= 1;
364 ut_asserteq(expected_ret,
365 uclass_first_device_check(UCLASS_TEST_PROBE, &dev));
366 for (i = 0; i < 4; i++) {
367 ut_asserteq_ptr(devlist[i], dev);
368 expected_ret = (mask & 1) ? -ENOENT - (i + 1) : 0;
369 mask >>= 1;
370 ut_asserteq(expected_ret, uclass_next_device_check(&dev));
371 }
372 ut_asserteq_ptr(NULL, dev);
373
374 return 0;
375}
376
377/* Test uclass_first_device_check() and uclass_next_device_check() */
378static int dm_test_first_next_ok_device(struct unit_test_state *uts)
379{
380 struct dm_testprobe_pdata *pdata;
381 struct udevice *dev, *parent = NULL, *devlist[4];
382 int count;
383 int ret;
384
385 /* There should be 4 devices */
386 count = 0;
387 for (ret = uclass_first_device_check(UCLASS_TEST_PROBE, &dev);
388 dev;
389 ret = uclass_next_device_check(&dev)) {
390 ut_assertok(ret);
391 devlist[count++] = dev;
392 parent = dev_get_parent(dev);
393 }
394 ut_asserteq(4, count);
395 ut_assertok(uclass_first_device_check(UCLASS_TEST_PROBE, &dev));
396 ut_assertok(check_devices(uts, devlist, 0));
397
398 /* Remove them and try again, with an error on the second one */
399 pdata = dev_get_platdata(devlist[1]);
400 pdata->probe_err = -ENOENT - 1;
401 device_remove(parent, DM_REMOVE_NORMAL);
402 ut_assertok(check_devices(uts, devlist, 1 << 1));
403
404 /* Now an error on the first one */
405 pdata = dev_get_platdata(devlist[0]);
406 pdata->probe_err = -ENOENT - 0;
407 device_remove(parent, DM_REMOVE_NORMAL);
408 ut_assertok(check_devices(uts, devlist, 3 << 0));
409
410 /* Now errors on all */
411 pdata = dev_get_platdata(devlist[2]);
412 pdata->probe_err = -ENOENT - 2;
413 pdata = dev_get_platdata(devlist[3]);
414 pdata->probe_err = -ENOENT - 3;
415 device_remove(parent, DM_REMOVE_NORMAL);
416 ut_assertok(check_devices(uts, devlist, 0xf << 0));
417
418 return 0;
419}
420DM_TEST(dm_test_first_next_ok_device, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
Mario Sixe8d52912018-03-12 14:53:33 +0100421
422static const struct udevice_id fdt_dummy_ids[] = {
423 { .compatible = "denx,u-boot-fdt-dummy", },
424 { }
425};
426
427UCLASS_DRIVER(fdt_dummy) = {
428 .name = "fdt_dummy",
429 .id = UCLASS_TEST_DUMMY,
430 .flags = DM_UC_FLAG_SEQ_ALIAS,
431};
432
433U_BOOT_DRIVER(fdt_dummy_drv) = {
434 .name = "fdt_dummy_drv",
435 .of_match = fdt_dummy_ids,
436 .id = UCLASS_TEST_DUMMY,
437};
438
439static int dm_test_fdt_translation(struct unit_test_state *uts)
440{
441 struct udevice *dev;
442
443 /* Some simple translations */
444 ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_DUMMY, 0, true, &dev));
445 ut_asserteq_str("dev@0,0", dev->name);
446 ut_asserteq(0x8000, dev_read_addr(dev));
447
448 ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_DUMMY, 1, true, &dev));
449 ut_asserteq_str("dev@1,100", dev->name);
450 ut_asserteq(0x9000, dev_read_addr(dev));
451
452 ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_DUMMY, 2, true, &dev));
453 ut_asserteq_str("dev@2,200", dev->name);
454 ut_asserteq(0xA000, dev_read_addr(dev));
455
456 /* No translation for busses with #size-cells == 0 */
457 ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_DUMMY, 3, true, &dev));
458 ut_asserteq_str("dev@42", dev->name);
459 ut_asserteq(0x42, dev_read_addr(dev));
460
461 return 0;
462}
463DM_TEST(dm_test_fdt_translation, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);