]>
Commit | Line | Data |
---|---|---|
83d290c5 | 1 | // SPDX-License-Identifier: GPL-2.0+ |
2e7d35d2 SG |
2 | /* |
3 | * Copyright (c) 2013 Google, Inc | |
2e7d35d2 SG |
4 | */ |
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> | |
98561572 | 14 | #include <dm/device-internal.h> |
2e7d35d2 SG |
15 | #include <dm/uclass-internal.h> |
16 | #include <dm/util.h> | |
2ea4d0db MS |
17 | #include <dm/lists.h> |
18 | #include <dm/of_access.h> | |
e721b882 | 19 | #include <test/ut.h> |
2e7d35d2 SG |
20 | |
21 | DECLARE_GLOBAL_DATA_PTR; | |
22 | ||
54c5d08a | 23 | static int testfdt_drv_ping(struct udevice *dev, int pingval, int *pingret) |
2e7d35d2 SG |
24 | { |
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 | ||
34 | static const struct test_ops test_ops = { | |
35 | .ping = testfdt_drv_ping, | |
36 | }; | |
37 | ||
54c5d08a | 38 | static int testfdt_ofdata_to_platdata(struct udevice *dev) |
2e7d35d2 SG |
39 | { |
40 | struct dm_test_pdata *pdata = dev_get_platdata(dev); | |
41 | ||
e160f7d4 | 42 | pdata->ping_add = fdtdec_get_int(gd->fdt_blob, dev_of_offset(dev), |
2e7d35d2 | 43 | "ping-add", -1); |
e160f7d4 | 44 | pdata->base = fdtdec_get_addr(gd->fdt_blob, dev_of_offset(dev), |
eb9ef5fe | 45 | "ping-expect"); |
2e7d35d2 SG |
46 | |
47 | return 0; | |
48 | } | |
49 | ||
54c5d08a | 50 | static int testfdt_drv_probe(struct udevice *dev) |
2e7d35d2 SG |
51 | { |
52 | struct dm_test_priv *priv = dev_get_priv(dev); | |
53 | ||
54 | priv->ping_total += DM_TEST_START_TOTAL; | |
55 | ||
83c7e434 SG |
56 | /* |
57 | * If this device is on a bus, the uclass_flag will be set before | |
d92878aa BM |
58 | * calling this function. In the meantime the uclass_postp is |
59 | * initlized to a value -1. These are used respectively by | |
60 | * dm_test_bus_child_pre_probe_uclass() and | |
61 | * dm_test_bus_child_post_probe_uclass(). | |
83c7e434 SG |
62 | */ |
63 | priv->uclass_total += priv->uclass_flag; | |
d92878aa | 64 | priv->uclass_postp = -1; |
83c7e434 | 65 | |
2e7d35d2 SG |
66 | return 0; |
67 | } | |
68 | ||
ae7f4513 | 69 | static const struct udevice_id testfdt_ids[] = { |
2e7d35d2 SG |
70 | { |
71 | .compatible = "denx,u-boot-fdt-test", | |
72 | .data = DM_TEST_TYPE_FIRST }, | |
73 | { | |
74 | .compatible = "google,another-fdt-test", | |
75 | .data = DM_TEST_TYPE_SECOND }, | |
76 | { } | |
77 | }; | |
78 | ||
79 | U_BOOT_DRIVER(testfdt_drv) = { | |
80 | .name = "testfdt_drv", | |
81 | .of_match = testfdt_ids, | |
82 | .id = UCLASS_TEST_FDT, | |
83 | .ofdata_to_platdata = testfdt_ofdata_to_platdata, | |
84 | .probe = testfdt_drv_probe, | |
85 | .ops = &test_ops, | |
86 | .priv_auto_alloc_size = sizeof(struct dm_test_priv), | |
87 | .platdata_auto_alloc_size = sizeof(struct dm_test_pdata), | |
88 | }; | |
89 | ||
2786cd74 BM |
90 | static const struct udevice_id testfdt1_ids[] = { |
91 | { | |
92 | .compatible = "denx,u-boot-fdt-test1", | |
93 | .data = DM_TEST_TYPE_FIRST }, | |
94 | { } | |
95 | }; | |
96 | ||
97 | U_BOOT_DRIVER(testfdt1_drv) = { | |
98 | .name = "testfdt1_drv", | |
99 | .of_match = testfdt1_ids, | |
100 | .id = UCLASS_TEST_FDT, | |
101 | .ofdata_to_platdata = testfdt_ofdata_to_platdata, | |
102 | .probe = testfdt_drv_probe, | |
103 | .ops = &test_ops, | |
104 | .priv_auto_alloc_size = sizeof(struct dm_test_priv), | |
105 | .platdata_auto_alloc_size = sizeof(struct dm_test_pdata), | |
106 | .flags = DM_FLAG_PRE_RELOC, | |
107 | }; | |
108 | ||
2e7d35d2 | 109 | /* From here is the testfdt uclass code */ |
54c5d08a | 110 | int testfdt_ping(struct udevice *dev, int pingval, int *pingret) |
2e7d35d2 SG |
111 | { |
112 | const struct test_ops *ops = device_get_ops(dev); | |
113 | ||
114 | if (!ops->ping) | |
115 | return -ENOSYS; | |
116 | ||
117 | return ops->ping(dev, pingval, pingret); | |
118 | } | |
119 | ||
120 | UCLASS_DRIVER(testfdt) = { | |
121 | .name = "testfdt", | |
122 | .id = UCLASS_TEST_FDT, | |
9cc36a2b | 123 | .flags = DM_UC_FLAG_SEQ_ALIAS, |
2e7d35d2 SG |
124 | }; |
125 | ||
98561572 SG |
126 | struct dm_testprobe_pdata { |
127 | int probe_err; | |
128 | }; | |
129 | ||
130 | static int testprobe_drv_probe(struct udevice *dev) | |
131 | { | |
132 | struct dm_testprobe_pdata *pdata = dev_get_platdata(dev); | |
133 | ||
134 | return pdata->probe_err; | |
135 | } | |
136 | ||
137 | static const struct udevice_id testprobe_ids[] = { | |
138 | { .compatible = "denx,u-boot-probe-test" }, | |
139 | { } | |
140 | }; | |
141 | ||
142 | U_BOOT_DRIVER(testprobe_drv) = { | |
143 | .name = "testprobe_drv", | |
144 | .of_match = testprobe_ids, | |
145 | .id = UCLASS_TEST_PROBE, | |
146 | .probe = testprobe_drv_probe, | |
147 | .platdata_auto_alloc_size = sizeof(struct dm_testprobe_pdata), | |
148 | }; | |
149 | ||
150 | UCLASS_DRIVER(testprobe) = { | |
151 | .name = "testprobe", | |
152 | .id = UCLASS_TEST_PROBE, | |
153 | .flags = DM_UC_FLAG_SEQ_ALIAS, | |
154 | }; | |
155 | ||
dc12ebbb SG |
156 | struct dm_testdevres_pdata { |
157 | void *ptr; | |
158 | }; | |
159 | ||
160 | struct dm_testdevres_priv { | |
161 | void *ptr; | |
42a0ce57 | 162 | void *ptr_ofdata; |
dc12ebbb SG |
163 | }; |
164 | ||
165 | static int testdevres_drv_bind(struct udevice *dev) | |
166 | { | |
167 | struct dm_testdevres_pdata *pdata = dev_get_platdata(dev); | |
168 | ||
169 | pdata->ptr = devm_kmalloc(dev, TEST_DEVRES_SIZE, 0); | |
170 | ||
171 | return 0; | |
172 | } | |
173 | ||
42a0ce57 SG |
174 | static int testdevres_drv_ofdata_to_platdata(struct udevice *dev) |
175 | { | |
176 | struct dm_testdevres_priv *priv = dev_get_priv(dev); | |
177 | ||
178 | priv->ptr_ofdata = devm_kmalloc(dev, TEST_DEVRES_SIZE3, 0); | |
179 | ||
180 | return 0; | |
181 | } | |
182 | ||
dc12ebbb SG |
183 | static int testdevres_drv_probe(struct udevice *dev) |
184 | { | |
185 | struct dm_testdevres_priv *priv = dev_get_priv(dev); | |
186 | ||
187 | priv->ptr = devm_kmalloc(dev, TEST_DEVRES_SIZE2, 0); | |
188 | ||
189 | return 0; | |
190 | } | |
191 | ||
192 | static const struct udevice_id testdevres_ids[] = { | |
193 | { .compatible = "denx,u-boot-devres-test" }, | |
194 | { } | |
195 | }; | |
196 | ||
197 | U_BOOT_DRIVER(testdevres_drv) = { | |
198 | .name = "testdevres_drv", | |
199 | .of_match = testdevres_ids, | |
200 | .id = UCLASS_TEST_DEVRES, | |
201 | .bind = testdevres_drv_bind, | |
42a0ce57 | 202 | .ofdata_to_platdata = testdevres_drv_ofdata_to_platdata, |
dc12ebbb SG |
203 | .probe = testdevres_drv_probe, |
204 | .platdata_auto_alloc_size = sizeof(struct dm_testdevres_pdata), | |
205 | .priv_auto_alloc_size = sizeof(struct dm_testdevres_priv), | |
206 | }; | |
207 | ||
208 | UCLASS_DRIVER(testdevres) = { | |
209 | .name = "testdevres", | |
210 | .id = UCLASS_TEST_DEVRES, | |
211 | .flags = DM_UC_FLAG_SEQ_ALIAS, | |
212 | }; | |
213 | ||
e721b882 | 214 | int dm_check_devices(struct unit_test_state *uts, int num_devices) |
2e7d35d2 | 215 | { |
54c5d08a | 216 | struct udevice *dev; |
2e7d35d2 SG |
217 | int ret; |
218 | int i; | |
219 | ||
2e7d35d2 SG |
220 | /* |
221 | * Now check that the ping adds are what we expect. This is using the | |
222 | * ping-add property in each node. | |
223 | */ | |
1ca7e206 | 224 | for (i = 0; i < num_devices; i++) { |
2e7d35d2 SG |
225 | uint32_t base; |
226 | ||
227 | ret = uclass_get_device(UCLASS_TEST_FDT, i, &dev); | |
228 | ut_assert(!ret); | |
229 | ||
230 | /* | |
eb9ef5fe SG |
231 | * Get the 'ping-expect' property, which tells us what the |
232 | * ping add should be. We don't use the platdata because we | |
233 | * want to test the code that sets that up | |
234 | * (testfdt_drv_probe()). | |
2e7d35d2 | 235 | */ |
e160f7d4 | 236 | base = fdtdec_get_addr(gd->fdt_blob, dev_of_offset(dev), |
eb9ef5fe | 237 | "ping-expect"); |
2e7d35d2 | 238 | debug("dev=%d, base=%d: %s\n", i, base, |
e160f7d4 | 239 | fdt_get_name(gd->fdt_blob, dev_of_offset(dev), NULL)); |
2e7d35d2 | 240 | |
e721b882 | 241 | ut_assert(!dm_check_operations(uts, dev, base, |
2e7d35d2 SG |
242 | dev_get_priv(dev))); |
243 | } | |
244 | ||
245 | return 0; | |
246 | } | |
1ca7e206 SG |
247 | |
248 | /* Test that FDT-based binding works correctly */ | |
e721b882 | 249 | static int dm_test_fdt(struct unit_test_state *uts) |
1ca7e206 | 250 | { |
2786cd74 | 251 | const int num_devices = 8; |
1ca7e206 SG |
252 | struct udevice *dev; |
253 | struct uclass *uc; | |
254 | int ret; | |
255 | int i; | |
256 | ||
257 | ret = dm_scan_fdt(gd->fdt_blob, false); | |
258 | ut_assert(!ret); | |
259 | ||
260 | ret = uclass_get(UCLASS_TEST_FDT, &uc); | |
261 | ut_assert(!ret); | |
262 | ||
263 | /* These are num_devices compatible root-level device tree nodes */ | |
264 | ut_asserteq(num_devices, list_count_items(&uc->dev_head)); | |
265 | ||
f8a85449 | 266 | /* Each should have platform data but no private data */ |
1ca7e206 SG |
267 | for (i = 0; i < num_devices; i++) { |
268 | ret = uclass_find_device(UCLASS_TEST_FDT, i, &dev); | |
269 | ut_assert(!ret); | |
270 | ut_assert(!dev_get_priv(dev)); | |
f8a85449 | 271 | ut_assert(dev->platdata); |
1ca7e206 SG |
272 | } |
273 | ||
e721b882 | 274 | ut_assertok(dm_check_devices(uts, num_devices)); |
1ca7e206 SG |
275 | |
276 | return 0; | |
277 | } | |
2e7d35d2 | 278 | DM_TEST(dm_test_fdt, 0); |
00606d7e | 279 | |
a93eb577 MS |
280 | static int dm_test_alias_highest_id(struct unit_test_state *uts) |
281 | { | |
282 | int ret; | |
283 | ||
284 | ret = dev_read_alias_highest_id("eth"); | |
285 | ut_asserteq(5, ret); | |
286 | ||
287 | ret = dev_read_alias_highest_id("gpio"); | |
288 | ut_asserteq(2, ret); | |
289 | ||
290 | ret = dev_read_alias_highest_id("pci"); | |
291 | ut_asserteq(2, ret); | |
292 | ||
293 | ret = dev_read_alias_highest_id("i2c"); | |
294 | ut_asserteq(0, ret); | |
295 | ||
296 | ret = dev_read_alias_highest_id("deadbeef"); | |
297 | ut_asserteq(-1, ret); | |
298 | ||
299 | return 0; | |
300 | } | |
301 | DM_TEST(dm_test_alias_highest_id, 0); | |
302 | ||
e721b882 | 303 | static int dm_test_fdt_pre_reloc(struct unit_test_state *uts) |
00606d7e SG |
304 | { |
305 | struct uclass *uc; | |
306 | int ret; | |
307 | ||
308 | ret = dm_scan_fdt(gd->fdt_blob, true); | |
309 | ut_assert(!ret); | |
310 | ||
311 | ret = uclass_get(UCLASS_TEST_FDT, &uc); | |
312 | ut_assert(!ret); | |
313 | ||
2786cd74 BM |
314 | /* |
315 | * These are 2 pre-reloc devices: | |
316 | * one with "u-boot,dm-pre-reloc" property (a-test node), and the other | |
317 | * one whose driver marked with DM_FLAG_PRE_RELOC flag (h-test node). | |
318 | */ | |
319 | ut_asserteq(2, list_count_items(&uc->dev_head)); | |
00606d7e SG |
320 | |
321 | return 0; | |
322 | } | |
323 | DM_TEST(dm_test_fdt_pre_reloc, 0); | |
5a66a8ff SG |
324 | |
325 | /* Test that sequence numbers are allocated properly */ | |
e721b882 | 326 | static int dm_test_fdt_uclass_seq(struct unit_test_state *uts) |
5a66a8ff SG |
327 | { |
328 | struct udevice *dev; | |
329 | ||
330 | /* A few basic santiy tests */ | |
331 | ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_FDT, 3, true, &dev)); | |
332 | ut_asserteq_str("b-test", dev->name); | |
333 | ||
9cc36a2b | 334 | ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_FDT, 8, true, &dev)); |
5a66a8ff SG |
335 | ut_asserteq_str("a-test", dev->name); |
336 | ||
337 | ut_asserteq(-ENODEV, uclass_find_device_by_seq(UCLASS_TEST_FDT, 5, | |
338 | true, &dev)); | |
339 | ut_asserteq_ptr(NULL, dev); | |
340 | ||
341 | /* Test aliases */ | |
342 | ut_assertok(uclass_get_device_by_seq(UCLASS_TEST_FDT, 6, &dev)); | |
343 | ut_asserteq_str("e-test", dev->name); | |
344 | ||
345 | ut_asserteq(-ENODEV, uclass_find_device_by_seq(UCLASS_TEST_FDT, 7, | |
346 | true, &dev)); | |
347 | ||
1ca7e206 SG |
348 | /* |
349 | * Note that c-test nodes are not probed since it is not a top-level | |
350 | * node | |
351 | */ | |
5a66a8ff SG |
352 | ut_assertok(uclass_get_device_by_seq(UCLASS_TEST_FDT, 3, &dev)); |
353 | ut_asserteq_str("b-test", dev->name); | |
354 | ||
355 | /* | |
356 | * d-test wants sequence number 3 also, but it can't have it because | |
357 | * b-test gets it first. | |
358 | */ | |
359 | ut_assertok(uclass_get_device(UCLASS_TEST_FDT, 2, &dev)); | |
360 | ut_asserteq_str("d-test", dev->name); | |
361 | ||
362 | /* d-test actually gets 0 */ | |
363 | ut_assertok(uclass_get_device_by_seq(UCLASS_TEST_FDT, 0, &dev)); | |
364 | ut_asserteq_str("d-test", dev->name); | |
365 | ||
366 | /* initially no one wants seq 1 */ | |
367 | ut_asserteq(-ENODEV, uclass_get_device_by_seq(UCLASS_TEST_FDT, 1, | |
368 | &dev)); | |
369 | ut_assertok(uclass_get_device(UCLASS_TEST_FDT, 0, &dev)); | |
9cc36a2b | 370 | ut_assertok(uclass_get_device(UCLASS_TEST_FDT, 4, &dev)); |
5a66a8ff SG |
371 | |
372 | /* But now that it is probed, we can find it */ | |
373 | ut_assertok(uclass_get_device_by_seq(UCLASS_TEST_FDT, 1, &dev)); | |
9cc36a2b | 374 | ut_asserteq_str("f-test", dev->name); |
5a66a8ff SG |
375 | |
376 | return 0; | |
377 | } | |
378 | DM_TEST(dm_test_fdt_uclass_seq, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT); | |
f4cdead2 SG |
379 | |
380 | /* Test that we can find a device by device tree offset */ | |
e721b882 | 381 | static int dm_test_fdt_offset(struct unit_test_state *uts) |
f4cdead2 SG |
382 | { |
383 | const void *blob = gd->fdt_blob; | |
384 | struct udevice *dev; | |
385 | int node; | |
386 | ||
387 | node = fdt_path_offset(blob, "/e-test"); | |
388 | ut_assert(node > 0); | |
389 | ut_assertok(uclass_get_device_by_of_offset(UCLASS_TEST_FDT, node, | |
390 | &dev)); | |
391 | ut_asserteq_str("e-test", dev->name); | |
392 | ||
393 | /* This node should not be bound */ | |
394 | node = fdt_path_offset(blob, "/junk"); | |
395 | ut_assert(node > 0); | |
396 | ut_asserteq(-ENODEV, uclass_get_device_by_of_offset(UCLASS_TEST_FDT, | |
397 | node, &dev)); | |
398 | ||
399 | /* This is not a top level node so should not be probed */ | |
1ca7e206 | 400 | node = fdt_path_offset(blob, "/some-bus/c-test@5"); |
f4cdead2 SG |
401 | ut_assert(node > 0); |
402 | ut_asserteq(-ENODEV, uclass_get_device_by_of_offset(UCLASS_TEST_FDT, | |
403 | node, &dev)); | |
404 | ||
405 | return 0; | |
406 | } | |
86b54ece SG |
407 | DM_TEST(dm_test_fdt_offset, |
408 | DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT | DM_TESTF_FLAT_TREE); | |
98561572 SG |
409 | |
410 | /** | |
411 | * Test various error conditions with uclass_first_device() and | |
412 | * uclass_next_device() | |
413 | */ | |
414 | static int dm_test_first_next_device(struct unit_test_state *uts) | |
415 | { | |
416 | struct dm_testprobe_pdata *pdata; | |
417 | struct udevice *dev, *parent = NULL; | |
418 | int count; | |
419 | int ret; | |
420 | ||
421 | /* There should be 4 devices */ | |
422 | for (ret = uclass_first_device(UCLASS_TEST_PROBE, &dev), count = 0; | |
423 | dev; | |
424 | ret = uclass_next_device(&dev)) { | |
425 | count++; | |
426 | parent = dev_get_parent(dev); | |
427 | } | |
428 | ut_assertok(ret); | |
429 | ut_asserteq(4, count); | |
430 | ||
431 | /* Remove them and try again, with an error on the second one */ | |
432 | ut_assertok(uclass_get_device(UCLASS_TEST_PROBE, 1, &dev)); | |
433 | pdata = dev_get_platdata(dev); | |
434 | pdata->probe_err = -ENOMEM; | |
435 | device_remove(parent, DM_REMOVE_NORMAL); | |
436 | ut_assertok(uclass_first_device(UCLASS_TEST_PROBE, &dev)); | |
437 | ut_asserteq(-ENOMEM, uclass_next_device(&dev)); | |
438 | ut_asserteq_ptr(dev, NULL); | |
439 | ||
440 | /* Now an error on the first one */ | |
441 | ut_assertok(uclass_get_device(UCLASS_TEST_PROBE, 0, &dev)); | |
442 | pdata = dev_get_platdata(dev); | |
443 | pdata->probe_err = -ENOENT; | |
444 | device_remove(parent, DM_REMOVE_NORMAL); | |
445 | ut_asserteq(-ENOENT, uclass_first_device(UCLASS_TEST_PROBE, &dev)); | |
446 | ||
447 | return 0; | |
448 | } | |
449 | DM_TEST(dm_test_first_next_device, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT); | |
95ce385a SG |
450 | |
451 | /** | |
452 | * check_devices() - Check return values and pointers | |
453 | * | |
454 | * This runs through a full sequence of uclass_first_device_check()... | |
455 | * uclass_next_device_check() checking that the return values and devices | |
456 | * are correct. | |
457 | * | |
458 | * @uts: Test state | |
459 | * @devlist: List of expected devices | |
460 | * @mask: Indicates which devices should return an error. Device n should | |
461 | * return error (-NOENT - n) if bit n is set, or no error (i.e. 0) if | |
462 | * bit n is clear. | |
463 | */ | |
464 | static int check_devices(struct unit_test_state *uts, | |
465 | struct udevice *devlist[], int mask) | |
466 | { | |
467 | int expected_ret; | |
468 | struct udevice *dev; | |
469 | int i; | |
470 | ||
471 | expected_ret = (mask & 1) ? -ENOENT : 0; | |
472 | mask >>= 1; | |
473 | ut_asserteq(expected_ret, | |
474 | uclass_first_device_check(UCLASS_TEST_PROBE, &dev)); | |
475 | for (i = 0; i < 4; i++) { | |
476 | ut_asserteq_ptr(devlist[i], dev); | |
477 | expected_ret = (mask & 1) ? -ENOENT - (i + 1) : 0; | |
478 | mask >>= 1; | |
479 | ut_asserteq(expected_ret, uclass_next_device_check(&dev)); | |
480 | } | |
481 | ut_asserteq_ptr(NULL, dev); | |
482 | ||
483 | return 0; | |
484 | } | |
485 | ||
486 | /* Test uclass_first_device_check() and uclass_next_device_check() */ | |
487 | static int dm_test_first_next_ok_device(struct unit_test_state *uts) | |
488 | { | |
489 | struct dm_testprobe_pdata *pdata; | |
490 | struct udevice *dev, *parent = NULL, *devlist[4]; | |
491 | int count; | |
492 | int ret; | |
493 | ||
494 | /* There should be 4 devices */ | |
495 | count = 0; | |
496 | for (ret = uclass_first_device_check(UCLASS_TEST_PROBE, &dev); | |
497 | dev; | |
498 | ret = uclass_next_device_check(&dev)) { | |
499 | ut_assertok(ret); | |
500 | devlist[count++] = dev; | |
501 | parent = dev_get_parent(dev); | |
502 | } | |
503 | ut_asserteq(4, count); | |
504 | ut_assertok(uclass_first_device_check(UCLASS_TEST_PROBE, &dev)); | |
505 | ut_assertok(check_devices(uts, devlist, 0)); | |
506 | ||
507 | /* Remove them and try again, with an error on the second one */ | |
508 | pdata = dev_get_platdata(devlist[1]); | |
509 | pdata->probe_err = -ENOENT - 1; | |
510 | device_remove(parent, DM_REMOVE_NORMAL); | |
511 | ut_assertok(check_devices(uts, devlist, 1 << 1)); | |
512 | ||
513 | /* Now an error on the first one */ | |
514 | pdata = dev_get_platdata(devlist[0]); | |
515 | pdata->probe_err = -ENOENT - 0; | |
516 | device_remove(parent, DM_REMOVE_NORMAL); | |
517 | ut_assertok(check_devices(uts, devlist, 3 << 0)); | |
518 | ||
519 | /* Now errors on all */ | |
520 | pdata = dev_get_platdata(devlist[2]); | |
521 | pdata->probe_err = -ENOENT - 2; | |
522 | pdata = dev_get_platdata(devlist[3]); | |
523 | pdata->probe_err = -ENOENT - 3; | |
524 | device_remove(parent, DM_REMOVE_NORMAL); | |
525 | ut_assertok(check_devices(uts, devlist, 0xf << 0)); | |
526 | ||
527 | return 0; | |
528 | } | |
529 | DM_TEST(dm_test_first_next_ok_device, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT); | |
e8d52918 MS |
530 | |
531 | static const struct udevice_id fdt_dummy_ids[] = { | |
532 | { .compatible = "denx,u-boot-fdt-dummy", }, | |
533 | { } | |
534 | }; | |
535 | ||
536 | UCLASS_DRIVER(fdt_dummy) = { | |
507cef3d | 537 | .name = "fdt-dummy", |
e8d52918 MS |
538 | .id = UCLASS_TEST_DUMMY, |
539 | .flags = DM_UC_FLAG_SEQ_ALIAS, | |
540 | }; | |
541 | ||
542 | U_BOOT_DRIVER(fdt_dummy_drv) = { | |
543 | .name = "fdt_dummy_drv", | |
544 | .of_match = fdt_dummy_ids, | |
545 | .id = UCLASS_TEST_DUMMY, | |
546 | }; | |
547 | ||
548 | static int dm_test_fdt_translation(struct unit_test_state *uts) | |
549 | { | |
550 | struct udevice *dev; | |
641067fb | 551 | fdt32_t dma_addr[2]; |
e8d52918 MS |
552 | |
553 | /* Some simple translations */ | |
554 | ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_DUMMY, 0, true, &dev)); | |
555 | ut_asserteq_str("dev@0,0", dev->name); | |
556 | ut_asserteq(0x8000, dev_read_addr(dev)); | |
557 | ||
558 | ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_DUMMY, 1, true, &dev)); | |
559 | ut_asserteq_str("dev@1,100", dev->name); | |
560 | ut_asserteq(0x9000, dev_read_addr(dev)); | |
561 | ||
562 | ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_DUMMY, 2, true, &dev)); | |
563 | ut_asserteq_str("dev@2,200", dev->name); | |
564 | ut_asserteq(0xA000, dev_read_addr(dev)); | |
565 | ||
566 | /* No translation for busses with #size-cells == 0 */ | |
567 | ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_DUMMY, 3, true, &dev)); | |
568 | ut_asserteq_str("dev@42", dev->name); | |
569 | ut_asserteq(0x42, dev_read_addr(dev)); | |
570 | ||
641067fb FD |
571 | /* dma address translation */ |
572 | ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_DUMMY, 0, true, &dev)); | |
573 | dma_addr[0] = cpu_to_be32(0); | |
574 | dma_addr[1] = cpu_to_be32(0); | |
575 | ut_asserteq(0x10000000, dev_translate_dma_address(dev, dma_addr)); | |
576 | ||
577 | ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_DUMMY, 1, true, &dev)); | |
578 | dma_addr[0] = cpu_to_be32(1); | |
579 | dma_addr[1] = cpu_to_be32(0x100); | |
580 | ut_asserteq(0x20000000, dev_translate_dma_address(dev, dma_addr)); | |
581 | ||
e8d52918 MS |
582 | return 0; |
583 | } | |
584 | DM_TEST(dm_test_fdt_translation, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT); | |
30a90f56 | 585 | |
30a90f56 ÁFR |
586 | static int dm_test_fdt_remap_addr_flat(struct unit_test_state *uts) |
587 | { | |
588 | struct udevice *dev; | |
589 | fdt_addr_t addr; | |
590 | void *paddr; | |
591 | ||
592 | ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_DUMMY, 0, true, &dev)); | |
593 | ||
594 | addr = devfdt_get_addr(dev); | |
595 | ut_asserteq(0x8000, addr); | |
596 | ||
597 | paddr = map_physmem(addr, 0, MAP_NOCACHE); | |
598 | ut_assertnonnull(paddr); | |
599 | ut_asserteq_ptr(paddr, devfdt_remap_addr(dev)); | |
600 | ||
601 | return 0; | |
602 | } | |
603 | DM_TEST(dm_test_fdt_remap_addr_flat, | |
604 | DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT | DM_TESTF_FLAT_TREE); | |
605 | ||
79598820 ÁFR |
606 | static int dm_test_fdt_remap_addr_index_flat(struct unit_test_state *uts) |
607 | { | |
608 | struct udevice *dev; | |
609 | fdt_addr_t addr; | |
f5b90479 | 610 | fdt_size_t size; |
79598820 ÁFR |
611 | void *paddr; |
612 | ||
613 | ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_DUMMY, 0, true, &dev)); | |
614 | ||
f5b90479 | 615 | addr = devfdt_get_addr_size_index(dev, 0, &size); |
79598820 | 616 | ut_asserteq(0x8000, addr); |
f5b90479 | 617 | ut_asserteq(0x1000, size); |
79598820 ÁFR |
618 | |
619 | paddr = map_physmem(addr, 0, MAP_NOCACHE); | |
620 | ut_assertnonnull(paddr); | |
621 | ut_asserteq_ptr(paddr, devfdt_remap_addr_index(dev, 0)); | |
622 | ||
623 | return 0; | |
624 | } | |
625 | DM_TEST(dm_test_fdt_remap_addr_index_flat, | |
626 | DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT | DM_TESTF_FLAT_TREE); | |
627 | ||
628 | static int dm_test_fdt_remap_addr_name_flat(struct unit_test_state *uts) | |
629 | { | |
630 | struct udevice *dev; | |
631 | fdt_addr_t addr; | |
f5b90479 | 632 | fdt_size_t size; |
79598820 ÁFR |
633 | void *paddr; |
634 | ||
635 | ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_DUMMY, 0, true, &dev)); | |
636 | ||
f5b90479 | 637 | addr = devfdt_get_addr_size_name(dev, "sandbox-dummy-0", &size); |
79598820 | 638 | ut_asserteq(0x8000, addr); |
f5b90479 | 639 | ut_asserteq(0x1000, size); |
79598820 ÁFR |
640 | |
641 | paddr = map_physmem(addr, 0, MAP_NOCACHE); | |
642 | ut_assertnonnull(paddr); | |
643 | ut_asserteq_ptr(paddr, devfdt_remap_addr_name(dev, "sandbox-dummy-0")); | |
644 | ||
645 | return 0; | |
646 | } | |
647 | DM_TEST(dm_test_fdt_remap_addr_name_flat, | |
648 | DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT | DM_TESTF_FLAT_TREE); | |
649 | ||
30a90f56 ÁFR |
650 | static int dm_test_fdt_remap_addr_live(struct unit_test_state *uts) |
651 | { | |
652 | struct udevice *dev; | |
653 | fdt_addr_t addr; | |
654 | void *paddr; | |
655 | ||
656 | ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_DUMMY, 0, true, &dev)); | |
657 | ||
658 | addr = dev_read_addr(dev); | |
659 | ut_asserteq(0x8000, addr); | |
660 | ||
661 | paddr = map_physmem(addr, 0, MAP_NOCACHE); | |
662 | ut_assertnonnull(paddr); | |
663 | ut_asserteq_ptr(paddr, dev_remap_addr(dev)); | |
664 | ||
665 | return 0; | |
666 | } | |
667 | DM_TEST(dm_test_fdt_remap_addr_live, | |
668 | DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT); | |
2ea4d0db | 669 | |
79598820 ÁFR |
670 | static int dm_test_fdt_remap_addr_index_live(struct unit_test_state *uts) |
671 | { | |
672 | struct udevice *dev; | |
673 | fdt_addr_t addr; | |
f5b90479 | 674 | fdt_size_t size; |
79598820 ÁFR |
675 | void *paddr; |
676 | ||
677 | ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_DUMMY, 0, true, &dev)); | |
678 | ||
f5b90479 | 679 | addr = dev_read_addr_size_index(dev, 0, &size); |
79598820 | 680 | ut_asserteq(0x8000, addr); |
f5b90479 | 681 | ut_asserteq(0x1000, size); |
79598820 ÁFR |
682 | |
683 | paddr = map_physmem(addr, 0, MAP_NOCACHE); | |
684 | ut_assertnonnull(paddr); | |
685 | ut_asserteq_ptr(paddr, dev_remap_addr_index(dev, 0)); | |
686 | ||
687 | return 0; | |
688 | } | |
689 | DM_TEST(dm_test_fdt_remap_addr_index_live, | |
690 | DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT); | |
691 | ||
692 | static int dm_test_fdt_remap_addr_name_live(struct unit_test_state *uts) | |
693 | { | |
694 | struct udevice *dev; | |
695 | fdt_addr_t addr; | |
f5b90479 | 696 | fdt_size_t size; |
79598820 ÁFR |
697 | void *paddr; |
698 | ||
699 | ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_DUMMY, 0, true, &dev)); | |
700 | ||
f5b90479 | 701 | addr = dev_read_addr_size_name(dev, "sandbox-dummy-0", &size); |
79598820 | 702 | ut_asserteq(0x8000, addr); |
f5b90479 | 703 | ut_asserteq(0x1000, size); |
79598820 ÁFR |
704 | |
705 | paddr = map_physmem(addr, 0, MAP_NOCACHE); | |
706 | ut_assertnonnull(paddr); | |
707 | ut_asserteq_ptr(paddr, dev_remap_addr_name(dev, "sandbox-dummy-0")); | |
708 | ||
709 | return 0; | |
710 | } | |
711 | DM_TEST(dm_test_fdt_remap_addr_name_live, | |
712 | DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT); | |
713 | ||
2ea4d0db MS |
714 | static int dm_test_fdt_livetree_writing(struct unit_test_state *uts) |
715 | { | |
716 | struct udevice *dev; | |
717 | ofnode node; | |
718 | ||
719 | if (!of_live_active()) { | |
720 | printf("Live tree not active; ignore test\n"); | |
721 | return 0; | |
722 | } | |
723 | ||
724 | /* Test enabling devices */ | |
725 | ||
726 | node = ofnode_path("/usb@2"); | |
727 | ||
728 | ut_assert(!of_device_is_available(ofnode_to_np(node))); | |
729 | ofnode_set_enabled(node, true); | |
730 | ut_assert(of_device_is_available(ofnode_to_np(node))); | |
731 | ||
732 | device_bind_driver_to_node(dm_root(), "usb_sandbox", "usb@2", node, | |
733 | &dev); | |
734 | ut_assertok(uclass_find_device_by_seq(UCLASS_USB, 2, true, &dev)); | |
735 | ||
736 | /* Test string property setting */ | |
737 | ||
738 | ut_assert(device_is_compatible(dev, "sandbox,usb")); | |
739 | ofnode_write_string(node, "compatible", "gdsys,super-usb"); | |
740 | ut_assert(device_is_compatible(dev, "gdsys,super-usb")); | |
741 | ofnode_write_string(node, "compatible", "sandbox,usb"); | |
742 | ut_assert(device_is_compatible(dev, "sandbox,usb")); | |
743 | ||
744 | /* Test setting generic properties */ | |
745 | ||
746 | /* Non-existent in DTB */ | |
747 | ut_asserteq(FDT_ADDR_T_NONE, dev_read_addr(dev)); | |
748 | /* reg = 0x42, size = 0x100 */ | |
749 | ut_assertok(ofnode_write_prop(node, "reg", 8, | |
750 | "\x00\x00\x00\x42\x00\x00\x01\x00")); | |
751 | ut_asserteq(0x42, dev_read_addr(dev)); | |
752 | ||
753 | /* Test disabling devices */ | |
754 | ||
755 | device_remove(dev, DM_REMOVE_NORMAL); | |
756 | device_unbind(dev); | |
757 | ||
758 | ut_assert(of_device_is_available(ofnode_to_np(node))); | |
759 | ofnode_set_enabled(node, false); | |
760 | ut_assert(!of_device_is_available(ofnode_to_np(node))); | |
761 | ||
762 | return 0; | |
763 | } | |
764 | DM_TEST(dm_test_fdt_livetree_writing, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT); | |
172942a4 MS |
765 | |
766 | static int dm_test_fdt_disable_enable_by_path(struct unit_test_state *uts) | |
767 | { | |
768 | ofnode node; | |
769 | ||
770 | if (!of_live_active()) { | |
771 | printf("Live tree not active; ignore test\n"); | |
772 | return 0; | |
773 | } | |
774 | ||
775 | node = ofnode_path("/usb@2"); | |
776 | ||
777 | /* Test enabling devices */ | |
778 | ||
779 | ut_assert(!of_device_is_available(ofnode_to_np(node))); | |
780 | dev_enable_by_path("/usb@2"); | |
781 | ut_assert(of_device_is_available(ofnode_to_np(node))); | |
782 | ||
783 | /* Test disabling devices */ | |
784 | ||
785 | ut_assert(of_device_is_available(ofnode_to_np(node))); | |
786 | dev_disable_by_path("/usb@2"); | |
787 | ut_assert(!of_device_is_available(ofnode_to_np(node))); | |
788 | ||
789 | return 0; | |
790 | } | |
791 | DM_TEST(dm_test_fdt_disable_enable_by_path, DM_TESTF_SCAN_PDATA | | |
792 | DM_TESTF_SCAN_FDT); | |
d0b4f68d SG |
793 | |
794 | /* Test a few uclass phandle functions */ | |
795 | static int dm_test_fdt_phandle(struct unit_test_state *uts) | |
796 | { | |
797 | struct udevice *back, *dev, *dev2; | |
798 | ||
799 | ut_assertok(uclass_find_first_device(UCLASS_PANEL_BACKLIGHT, &back)); | |
800 | ut_asserteq(-ENOENT, uclass_find_device_by_phandle(UCLASS_REGULATOR, | |
801 | back, "missing", &dev)); | |
802 | ut_assertok(uclass_find_device_by_phandle(UCLASS_REGULATOR, back, | |
803 | "power-supply", &dev)); | |
804 | ut_asserteq(0, device_active(dev)); | |
805 | ut_asserteq_str("ldo1", dev->name); | |
806 | ut_assertok(uclass_get_device_by_phandle(UCLASS_REGULATOR, back, | |
807 | "power-supply", &dev2)); | |
808 | ut_asserteq_ptr(dev, dev2); | |
809 | ||
810 | return 0; | |
811 | } | |
812 | DM_TEST(dm_test_fdt_phandle, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT); | |
3abe1115 SG |
813 | |
814 | /* Test device_find_first_child_by_uclass() */ | |
815 | static int dm_test_first_child(struct unit_test_state *uts) | |
816 | { | |
817 | struct udevice *i2c, *dev, *dev2; | |
818 | ||
819 | ut_assertok(uclass_first_device_err(UCLASS_I2C, &i2c)); | |
820 | ut_assertok(device_find_first_child_by_uclass(i2c, UCLASS_RTC, &dev)); | |
821 | ut_asserteq_str("rtc@43", dev->name); | |
822 | ut_assertok(device_find_child_by_name(i2c, "rtc@43", &dev2)); | |
823 | ut_asserteq_ptr(dev, dev2); | |
824 | ut_assertok(device_find_child_by_name(i2c, "rtc@61", &dev2)); | |
825 | ut_asserteq_str("rtc@61", dev2->name); | |
826 | ||
827 | ut_assertok(device_find_first_child_by_uclass(i2c, UCLASS_I2C_EEPROM, | |
828 | &dev)); | |
829 | ut_asserteq_str("eeprom@2c", dev->name); | |
830 | ut_assertok(device_find_child_by_name(i2c, "eeprom@2c", &dev2)); | |
831 | ut_asserteq_ptr(dev, dev2); | |
832 | ||
833 | ut_asserteq(-ENODEV, device_find_first_child_by_uclass(i2c, | |
834 | UCLASS_VIDEO, &dev)); | |
835 | ut_asserteq(-ENODEV, device_find_child_by_name(i2c, "missing", &dev)); | |
836 | ||
837 | return 0; | |
838 | } | |
839 | DM_TEST(dm_test_first_child, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT); | |
a1b17e4f SG |
840 | |
841 | /* Test integer functions in dm_read_...() */ | |
842 | static int dm_test_read_int(struct unit_test_state *uts) | |
843 | { | |
844 | struct udevice *dev; | |
845 | u32 val32; | |
846 | s32 sval; | |
847 | uint val; | |
848 | ||
849 | ut_assertok(uclass_first_device_err(UCLASS_TEST_FDT, &dev)); | |
850 | ut_asserteq_str("a-test", dev->name); | |
851 | ut_assertok(dev_read_u32(dev, "int-value", &val32)); | |
852 | ut_asserteq(1234, val32); | |
853 | ||
854 | ut_asserteq(-EINVAL, dev_read_u32(dev, "missing", &val32)); | |
855 | ut_asserteq(6, dev_read_u32_default(dev, "missing", 6)); | |
856 | ||
857 | ut_asserteq(1234, dev_read_u32_default(dev, "int-value", 6)); | |
858 | ut_asserteq(1234, val32); | |
859 | ||
860 | ut_asserteq(-EINVAL, dev_read_s32(dev, "missing", &sval)); | |
861 | ut_asserteq(6, dev_read_s32_default(dev, "missing", 6)); | |
862 | ||
863 | ut_asserteq(-1234, dev_read_s32_default(dev, "uint-value", 6)); | |
864 | ut_assertok(dev_read_s32(dev, "uint-value", &sval)); | |
865 | ut_asserteq(-1234, sval); | |
866 | ||
867 | val = 0; | |
868 | ut_asserteq(-EINVAL, dev_read_u32u(dev, "missing", &val)); | |
869 | ut_assertok(dev_read_u32u(dev, "uint-value", &val)); | |
870 | ut_asserteq(-1234, val); | |
871 | ||
872 | return 0; | |
873 | } | |
874 | DM_TEST(dm_test_read_int, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT); | |
f262d4ca SG |
875 | |
876 | /* Test device_first_child_ofdata_err(), etc. */ | |
877 | static int dm_test_child_ofdata(struct unit_test_state *uts) | |
878 | { | |
879 | struct udevice *bus, *dev; | |
880 | int count; | |
881 | ||
882 | ut_assertok(uclass_first_device_err(UCLASS_TEST_BUS, &bus)); | |
883 | count = 0; | |
884 | device_foreach_child_ofdata_to_platdata(dev, bus) { | |
885 | ut_assert(dev->flags & DM_FLAG_PLATDATA_VALID); | |
886 | ut_assert(!(dev->flags & DM_FLAG_ACTIVATED)); | |
887 | count++; | |
888 | } | |
889 | ut_asserteq(3, count); | |
890 | ||
891 | return 0; | |
892 | } | |
893 | DM_TEST(dm_test_child_ofdata, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT); | |
903e83ee SG |
894 | |
895 | /* Test device_first_child_err(), etc. */ | |
896 | static int dm_test_first_child_probe(struct unit_test_state *uts) | |
897 | { | |
898 | struct udevice *bus, *dev; | |
899 | int count; | |
900 | ||
901 | ut_assertok(uclass_first_device_err(UCLASS_TEST_BUS, &bus)); | |
902 | count = 0; | |
903 | device_foreach_child_probe(dev, bus) { | |
904 | ut_assert(dev->flags & DM_FLAG_PLATDATA_VALID); | |
905 | ut_assert(dev->flags & DM_FLAG_ACTIVATED); | |
906 | count++; | |
907 | } | |
908 | ut_asserteq(3, count); | |
909 | ||
910 | return 0; | |
911 | } | |
912 | DM_TEST(dm_test_first_child_probe, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT); |