blob: 927618256f0ac88d73cf6e1955a8412a7101a2d0 [file] [log] [blame]
Simon Glass079ac592020-12-23 08:11:18 -07001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Copyright (c) 2014 Google, Inc
4 */
5
6#include <common.h>
7#include <dm.h>
8#include <dm/test.h>
Simon Glass401d1c42020-10-30 21:38:53 -06009#include <asm/global_data.h>
Simon Glass079ac592020-12-23 08:11:18 -070010
11/* Records the last testbus device that was removed */
12static struct udevice *testbus_removed;
13
14struct udevice *testbus_get_clear_removed(void)
15{
16 struct udevice *removed = testbus_removed;
17
18 testbus_removed = NULL;
19
20 return removed;
21}
22
23static int testbus_drv_probe(struct udevice *dev)
24{
25 if (!CONFIG_IS_ENABLED(OF_PLATDATA)) {
26 int ret;
27
28 ret = dm_scan_fdt_dev(dev);
29 if (ret)
30 return ret;
31 }
32
33 return 0;
34}
35
36static int testbus_child_post_bind(struct udevice *dev)
37{
38 struct dm_test_parent_plat *plat;
39
40 plat = dev_get_parent_plat(dev);
41 plat->bind_flag = 1;
42 plat->uclass_bind_flag = 2;
43
44 return 0;
45}
46
47static int testbus_child_pre_probe(struct udevice *dev)
48{
49 struct dm_test_parent_data *parent_data = dev_get_parent_priv(dev);
50
51 parent_data->flag += TEST_FLAG_CHILD_PROBED;
52
53 return 0;
54}
55
56static int testbus_child_pre_probe_uclass(struct udevice *dev)
57{
58 struct dm_test_priv *priv = dev_get_priv(dev);
59
60 priv->uclass_flag++;
61
62 return 0;
63}
64
65static int testbus_child_post_probe_uclass(struct udevice *dev)
66{
67 struct dm_test_priv *priv = dev_get_priv(dev);
68
69 priv->uclass_postp++;
70
71 return 0;
72}
73
74static int testbus_child_post_remove(struct udevice *dev)
75{
76 struct dm_test_parent_data *parent_data = dev_get_parent_priv(dev);
77
78 parent_data->flag += TEST_FLAG_CHILD_REMOVED;
79 testbus_removed = dev;
80
81 return 0;
82}
83
84static const struct udevice_id testbus_ids[] = {
85 { .compatible = "denx,u-boot-test-bus", .data = DM_TEST_TYPE_FIRST },
86 { }
87};
88
Simon Glass074197a2021-02-03 06:01:09 -070089U_BOOT_DRIVER(denx_u_boot_test_bus) = {
Simon Glass079ac592020-12-23 08:11:18 -070090 .name = "testbus_drv",
91 .of_match = testbus_ids,
92 .id = UCLASS_TEST_BUS,
93 .probe = testbus_drv_probe,
94 .child_post_bind = testbus_child_post_bind,
95 .priv_auto = sizeof(struct dm_test_priv),
96 .plat_auto = sizeof(struct dm_test_pdata),
97 .per_child_auto = sizeof(struct dm_test_parent_data),
98 .per_child_plat_auto = sizeof(struct dm_test_parent_plat),
99 .child_pre_probe = testbus_child_pre_probe,
100 .child_post_remove = testbus_child_post_remove,
Simon Glassd392d322021-02-03 06:01:21 -0700101 DM_HEADER(<test.h>)
Simon Glass079ac592020-12-23 08:11:18 -0700102};
103
104UCLASS_DRIVER(testbus) = {
105 .name = "testbus",
106 .id = UCLASS_TEST_BUS,
107 .flags = DM_UC_FLAG_SEQ_ALIAS,
108 .child_pre_probe = testbus_child_pre_probe_uclass,
109 .child_post_probe = testbus_child_post_probe_uclass,
Simon Glassd392d322021-02-03 06:01:21 -0700110
Simon Glass930a3dd2022-05-08 04:39:24 -0600111 .per_device_auto = sizeof(struct dm_test_uclass_priv),
112
113 /* Note: this is for dtoc testing as well as tags*/
Simon Glass53c20be2022-05-08 04:39:23 -0600114 .per_device_plat_auto = sizeof(struct dm_test_uclass_plat),
Simon Glass079ac592020-12-23 08:11:18 -0700115};
116
117static int testfdt_drv_ping(struct udevice *dev, int pingval, int *pingret)
118{
119 const struct dm_test_pdata *pdata = dev_get_plat(dev);
120 struct dm_test_priv *priv = dev_get_priv(dev);
121
122 *pingret = pingval + pdata->ping_add;
123 priv->ping_total += *pingret;
124
125 return 0;
126}
127
128static const struct test_ops test_ops = {
129 .ping = testfdt_drv_ping,
130};
131
132static int testfdt_of_to_plat(struct udevice *dev)
133{
134 struct dm_test_pdata *pdata = dev_get_plat(dev);
135
136 pdata->ping_add = fdtdec_get_int(gd->fdt_blob, dev_of_offset(dev),
137 "ping-add", -1);
138 pdata->base = fdtdec_get_addr(gd->fdt_blob, dev_of_offset(dev),
139 "ping-expect");
140
141 return 0;
142}
143
144static int testfdt_drv_probe(struct udevice *dev)
145{
146 struct dm_test_priv *priv = dev_get_priv(dev);
147
148 priv->ping_total += DM_TEST_START_TOTAL;
149
150 /*
151 * If this device is on a bus, the uclass_flag will be set before
152 * calling this function. In the meantime the uclass_postp is
153 * initlized to a value -1. These are used respectively by
154 * dm_test_bus_child_pre_probe_uclass() and
155 * dm_test_bus_child_post_probe_uclass().
156 */
157 priv->uclass_total += priv->uclass_flag;
158 priv->uclass_postp = -1;
159
160 return 0;
161}
162
163static const struct udevice_id testfdt_ids[] = {
164 { .compatible = "denx,u-boot-fdt-test", .data = DM_TEST_TYPE_FIRST },
165 { .compatible = "google,another-fdt-test", .data = DM_TEST_TYPE_SECOND },
166 { }
167};
168
Simon Glass074197a2021-02-03 06:01:09 -0700169DM_DRIVER_ALIAS(denx_u_boot_fdt_test, google_another_fdt_test)
170
171U_BOOT_DRIVER(denx_u_boot_fdt_test) = {
Simon Glass079ac592020-12-23 08:11:18 -0700172 .name = "testfdt_drv",
173 .of_match = testfdt_ids,
174 .id = UCLASS_TEST_FDT,
175 .of_to_plat = testfdt_of_to_plat,
176 .probe = testfdt_drv_probe,
177 .ops = &test_ops,
178 .priv_auto = sizeof(struct dm_test_priv),
179 .plat_auto = sizeof(struct dm_test_pdata),
180};
181
182static const struct udevice_id testfdt1_ids[] = {
183 { .compatible = "denx,u-boot-fdt-test1", .data = DM_TEST_TYPE_FIRST },
184 { }
185};
186
187U_BOOT_DRIVER(testfdt1_drv) = {
188 .name = "testfdt1_drv",
189 .of_match = testfdt1_ids,
190 .id = UCLASS_TEST_FDT,
191 .of_to_plat = testfdt_of_to_plat,
192 .probe = testfdt_drv_probe,
193 .ops = &test_ops,
194 .priv_auto = sizeof(struct dm_test_priv),
195 .plat_auto = sizeof(struct dm_test_pdata),
196 .flags = DM_FLAG_PRE_RELOC,
197};
198
199/* From here is the testfdt uclass code */
200int testfdt_ping(struct udevice *dev, int pingval, int *pingret)
201{
202 const struct test_ops *ops = device_get_ops(dev);
203
204 if (!ops->ping)
205 return -ENOSYS;
206
207 return ops->ping(dev, pingval, pingret);
208}
209
210UCLASS_DRIVER(testfdt) = {
211 .name = "testfdt",
212 .id = UCLASS_TEST_FDT,
213 .flags = DM_UC_FLAG_SEQ_ALIAS,
Simon Glassea74c952021-02-03 06:01:20 -0700214 .priv_auto = sizeof(struct dm_test_uc_priv),
Simon Glass079ac592020-12-23 08:11:18 -0700215};
216
217static const struct udevice_id testfdtm_ids[] = {
218 { .compatible = "denx,u-boot-fdtm-test" },
219 { }
220};
221
222U_BOOT_DRIVER(testfdtm_drv) = {
223 .name = "testfdtm_drv",
224 .of_match = testfdtm_ids,
225 .id = UCLASS_TEST_FDT_MANUAL,
226};
227
228UCLASS_DRIVER(testfdtm) = {
229 .name = "testfdtm",
230 .id = UCLASS_TEST_FDT_MANUAL,
231 .flags = DM_UC_FLAG_SEQ_ALIAS | DM_UC_FLAG_NO_AUTO_SEQ,
232};