blob: dc4ebf9adaf90e4aecc0848c3af9f873d5d8e911 [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>
15#include <dm/ut.h>
16#include <dm/uclass-internal.h>
17#include <dm/util.h>
18
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
54 return 0;
55}
56
Simon Glassae7f4512014-06-11 23:29:45 -060057static const struct udevice_id testfdt_ids[] = {
Simon Glass2e7d35d2014-02-26 15:59:21 -070058 {
59 .compatible = "denx,u-boot-fdt-test",
60 .data = DM_TEST_TYPE_FIRST },
61 {
62 .compatible = "google,another-fdt-test",
63 .data = DM_TEST_TYPE_SECOND },
64 { }
65};
66
67U_BOOT_DRIVER(testfdt_drv) = {
68 .name = "testfdt_drv",
69 .of_match = testfdt_ids,
70 .id = UCLASS_TEST_FDT,
71 .ofdata_to_platdata = testfdt_ofdata_to_platdata,
72 .probe = testfdt_drv_probe,
73 .ops = &test_ops,
74 .priv_auto_alloc_size = sizeof(struct dm_test_priv),
75 .platdata_auto_alloc_size = sizeof(struct dm_test_pdata),
76};
77
78/* From here is the testfdt uclass code */
Heiko Schocher54c5d082014-05-22 12:43:05 +020079int testfdt_ping(struct udevice *dev, int pingval, int *pingret)
Simon Glass2e7d35d2014-02-26 15:59:21 -070080{
81 const struct test_ops *ops = device_get_ops(dev);
82
83 if (!ops->ping)
84 return -ENOSYS;
85
86 return ops->ping(dev, pingval, pingret);
87}
88
89UCLASS_DRIVER(testfdt) = {
90 .name = "testfdt",
91 .id = UCLASS_TEST_FDT,
92};
93
Simon Glass1ca7e202014-07-23 06:55:18 -060094int dm_check_devices(struct dm_test_state *dms, int num_devices)
Simon Glass2e7d35d2014-02-26 15:59:21 -070095{
Heiko Schocher54c5d082014-05-22 12:43:05 +020096 struct udevice *dev;
Simon Glass2e7d35d2014-02-26 15:59:21 -070097 int ret;
98 int i;
99
Simon Glass2e7d35d2014-02-26 15:59:21 -0700100 /*
101 * Now check that the ping adds are what we expect. This is using the
102 * ping-add property in each node.
103 */
Simon Glass1ca7e202014-07-23 06:55:18 -0600104 for (i = 0; i < num_devices; i++) {
Simon Glass2e7d35d2014-02-26 15:59:21 -0700105 uint32_t base;
106
107 ret = uclass_get_device(UCLASS_TEST_FDT, i, &dev);
108 ut_assert(!ret);
109
110 /*
Simon Glasseb9ef5f2014-07-23 06:54:57 -0600111 * Get the 'ping-expect' property, which tells us what the
112 * ping add should be. We don't use the platdata because we
113 * want to test the code that sets that up
114 * (testfdt_drv_probe()).
Simon Glass2e7d35d2014-02-26 15:59:21 -0700115 */
Simon Glasseb9ef5f2014-07-23 06:54:57 -0600116 base = fdtdec_get_addr(gd->fdt_blob, dev->of_offset,
117 "ping-expect");
Simon Glass2e7d35d2014-02-26 15:59:21 -0700118 debug("dev=%d, base=%d: %s\n", i, base,
119 fdt_get_name(gd->fdt_blob, dev->of_offset, NULL));
120
121 ut_assert(!dm_check_operations(dms, dev, base,
122 dev_get_priv(dev)));
123 }
124
125 return 0;
126}
Simon Glass1ca7e202014-07-23 06:55:18 -0600127
128/* Test that FDT-based binding works correctly */
129static int dm_test_fdt(struct dm_test_state *dms)
130{
131 const int num_devices = 4;
132 struct udevice *dev;
133 struct uclass *uc;
134 int ret;
135 int i;
136
137 ret = dm_scan_fdt(gd->fdt_blob, false);
138 ut_assert(!ret);
139
140 ret = uclass_get(UCLASS_TEST_FDT, &uc);
141 ut_assert(!ret);
142
143 /* These are num_devices compatible root-level device tree nodes */
144 ut_asserteq(num_devices, list_count_items(&uc->dev_head));
145
Simon Glassf8a85442015-01-25 08:27:00 -0700146 /* Each should have platform data but no private data */
Simon Glass1ca7e202014-07-23 06:55:18 -0600147 for (i = 0; i < num_devices; i++) {
148 ret = uclass_find_device(UCLASS_TEST_FDT, i, &dev);
149 ut_assert(!ret);
150 ut_assert(!dev_get_priv(dev));
Simon Glassf8a85442015-01-25 08:27:00 -0700151 ut_assert(dev->platdata);
Simon Glass1ca7e202014-07-23 06:55:18 -0600152 }
153
154 ut_assertok(dm_check_devices(dms, num_devices));
155
156 return 0;
157}
Simon Glass2e7d35d2014-02-26 15:59:21 -0700158DM_TEST(dm_test_fdt, 0);
Simon Glass00606d72014-07-23 06:55:03 -0600159
160static int dm_test_fdt_pre_reloc(struct dm_test_state *dms)
161{
162 struct uclass *uc;
163 int ret;
164
165 ret = dm_scan_fdt(gd->fdt_blob, true);
166 ut_assert(!ret);
167
168 ret = uclass_get(UCLASS_TEST_FDT, &uc);
169 ut_assert(!ret);
170
171 /* These is only one pre-reloc device */
172 ut_asserteq(1, list_count_items(&uc->dev_head));
173
174 return 0;
175}
176DM_TEST(dm_test_fdt_pre_reloc, 0);
Simon Glass5a66a8f2014-07-23 06:55:12 -0600177
178/* Test that sequence numbers are allocated properly */
179static int dm_test_fdt_uclass_seq(struct dm_test_state *dms)
180{
181 struct udevice *dev;
182
183 /* A few basic santiy tests */
184 ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_FDT, 3, true, &dev));
185 ut_asserteq_str("b-test", dev->name);
186
187 ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_FDT, 0, true, &dev));
188 ut_asserteq_str("a-test", dev->name);
189
190 ut_asserteq(-ENODEV, uclass_find_device_by_seq(UCLASS_TEST_FDT, 5,
191 true, &dev));
192 ut_asserteq_ptr(NULL, dev);
193
194 /* Test aliases */
195 ut_assertok(uclass_get_device_by_seq(UCLASS_TEST_FDT, 6, &dev));
196 ut_asserteq_str("e-test", dev->name);
197
198 ut_asserteq(-ENODEV, uclass_find_device_by_seq(UCLASS_TEST_FDT, 7,
199 true, &dev));
200
Simon Glass1ca7e202014-07-23 06:55:18 -0600201 /*
202 * Note that c-test nodes are not probed since it is not a top-level
203 * node
204 */
Simon Glass5a66a8f2014-07-23 06:55:12 -0600205 ut_assertok(uclass_get_device_by_seq(UCLASS_TEST_FDT, 3, &dev));
206 ut_asserteq_str("b-test", dev->name);
207
208 /*
209 * d-test wants sequence number 3 also, but it can't have it because
210 * b-test gets it first.
211 */
212 ut_assertok(uclass_get_device(UCLASS_TEST_FDT, 2, &dev));
213 ut_asserteq_str("d-test", dev->name);
214
215 /* d-test actually gets 0 */
216 ut_assertok(uclass_get_device_by_seq(UCLASS_TEST_FDT, 0, &dev));
217 ut_asserteq_str("d-test", dev->name);
218
219 /* initially no one wants seq 1 */
220 ut_asserteq(-ENODEV, uclass_get_device_by_seq(UCLASS_TEST_FDT, 1,
221 &dev));
222 ut_assertok(uclass_get_device(UCLASS_TEST_FDT, 0, &dev));
223 ut_assertok(uclass_get_device(UCLASS_TEST_FDT, 1, &dev));
224
225 /* But now that it is probed, we can find it */
226 ut_assertok(uclass_get_device_by_seq(UCLASS_TEST_FDT, 1, &dev));
227 ut_asserteq_str("a-test", dev->name);
228
229 return 0;
230}
231DM_TEST(dm_test_fdt_uclass_seq, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
Simon Glassf4cdead2014-07-23 06:55:14 -0600232
233/* Test that we can find a device by device tree offset */
234static int dm_test_fdt_offset(struct dm_test_state *dms)
235{
236 const void *blob = gd->fdt_blob;
237 struct udevice *dev;
238 int node;
239
240 node = fdt_path_offset(blob, "/e-test");
241 ut_assert(node > 0);
242 ut_assertok(uclass_get_device_by_of_offset(UCLASS_TEST_FDT, node,
243 &dev));
244 ut_asserteq_str("e-test", dev->name);
245
246 /* This node should not be bound */
247 node = fdt_path_offset(blob, "/junk");
248 ut_assert(node > 0);
249 ut_asserteq(-ENODEV, uclass_get_device_by_of_offset(UCLASS_TEST_FDT,
250 node, &dev));
251
252 /* This is not a top level node so should not be probed */
Simon Glass1ca7e202014-07-23 06:55:18 -0600253 node = fdt_path_offset(blob, "/some-bus/c-test@5");
Simon Glassf4cdead2014-07-23 06:55:14 -0600254 ut_assert(node > 0);
255 ut_asserteq(-ENODEV, uclass_get_device_by_of_offset(UCLASS_TEST_FDT,
256 node, &dev));
257
258 return 0;
259}
Simon Glassf4cdead2014-07-23 06:55:14 -0600260DM_TEST(dm_test_fdt_offset, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);