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