xref: /rk3399_rockchip-uboot/drivers/core/device.c (revision ba8da9dc43ac8ae3351345df12dc7f9d1cd07ae0)
1 /*
2  * Device manager
3  *
4  * Copyright (c) 2013 Google, Inc
5  *
6  * (C) Copyright 2012
7  * Pavel Herrmann <morpheus.ibis@gmail.com>
8  *
9  * SPDX-License-Identifier:	GPL-2.0+
10  */
11 
12 #include <common.h>
13 #include <fdtdec.h>
14 #include <malloc.h>
15 #include <dm/device.h>
16 #include <dm/device-internal.h>
17 #include <dm/lists.h>
18 #include <dm/platdata.h>
19 #include <dm/uclass.h>
20 #include <dm/uclass-internal.h>
21 #include <dm/util.h>
22 #include <linux/err.h>
23 #include <linux/list.h>
24 
25 DECLARE_GLOBAL_DATA_PTR;
26 
27 int device_bind(struct udevice *parent, struct driver *drv, const char *name,
28 		void *platdata, int of_offset, struct udevice **devp)
29 {
30 	struct udevice *dev;
31 	struct uclass *uc;
32 	int ret = 0;
33 
34 	*devp = NULL;
35 	if (!name)
36 		return -EINVAL;
37 
38 	ret = uclass_get(drv->id, &uc);
39 	if (ret)
40 		return ret;
41 
42 	dev = calloc(1, sizeof(struct udevice));
43 	if (!dev)
44 		return -ENOMEM;
45 
46 	INIT_LIST_HEAD(&dev->sibling_node);
47 	INIT_LIST_HEAD(&dev->child_head);
48 	INIT_LIST_HEAD(&dev->uclass_node);
49 	dev->platdata = platdata;
50 	dev->name = name;
51 	dev->of_offset = of_offset;
52 	dev->parent = parent;
53 	dev->driver = drv;
54 	dev->uclass = uc;
55 
56 	/*
57 	 * For some devices, such as a SPI or I2C bus, the 'reg' property
58 	 * is a reasonable indicator of the sequence number. But if there is
59 	 * an alias, we use that in preference. In any case, this is just
60 	 * a 'requested' sequence, and will be resolved (and ->seq updated)
61 	 * when the device is probed.
62 	 */
63 	dev->seq = -1;
64 #ifdef CONFIG_OF_CONTROL
65 	dev->req_seq = fdtdec_get_int(gd->fdt_blob, of_offset, "reg", -1);
66 	if (!IS_ERR_VALUE(dev->req_seq))
67 		dev->req_seq &= INT_MAX;
68 	if (uc->uc_drv->name && of_offset != -1) {
69 		fdtdec_get_alias_seq(gd->fdt_blob, uc->uc_drv->name, of_offset,
70 				     &dev->req_seq);
71 	}
72 #else
73 	dev->req_seq = -1;
74 #endif
75 	if (!dev->platdata && drv->platdata_auto_alloc_size) {
76 		dev->flags |= DM_FLAG_ALLOC_PDATA;
77 		dev->platdata = calloc(1, drv->platdata_auto_alloc_size);
78 		if (!dev->platdata) {
79 			ret = -ENOMEM;
80 			goto fail_alloc1;
81 		}
82 	}
83 	if (parent) {
84 		int size = parent->driver->per_child_platdata_auto_alloc_size;
85 
86 		if (!size) {
87 			size = parent->uclass->uc_drv->
88 					per_child_platdata_auto_alloc_size;
89 		}
90 		if (size) {
91 			dev->flags |= DM_FLAG_ALLOC_PARENT_PDATA;
92 			dev->parent_platdata = calloc(1, size);
93 			if (!dev->parent_platdata) {
94 				ret = -ENOMEM;
95 				goto fail_alloc2;
96 			}
97 		}
98 	}
99 
100 	/* put dev into parent's successor list */
101 	if (parent)
102 		list_add_tail(&dev->sibling_node, &parent->child_head);
103 
104 	ret = uclass_bind_device(dev);
105 	if (ret)
106 		goto fail_uclass_bind;
107 
108 	/* if we fail to bind we remove device from successors and free it */
109 	if (drv->bind) {
110 		ret = drv->bind(dev);
111 		if (ret)
112 			goto fail_bind;
113 	}
114 	if (parent)
115 		dm_dbg("Bound device %s to %s\n", dev->name, parent->name);
116 	*devp = dev;
117 
118 	return 0;
119 
120 fail_bind:
121 	if (uclass_unbind_device(dev)) {
122 		dm_warn("Failed to unbind dev '%s' on error path\n",
123 			dev->name);
124 	}
125 fail_uclass_bind:
126 	list_del(&dev->sibling_node);
127 	if (dev->flags & DM_FLAG_ALLOC_PARENT_PDATA) {
128 		free(dev->parent_platdata);
129 		dev->parent_platdata = NULL;
130 	}
131 fail_alloc2:
132 	if (dev->flags & DM_FLAG_ALLOC_PDATA) {
133 		free(dev->platdata);
134 		dev->platdata = NULL;
135 	}
136 fail_alloc1:
137 	free(dev);
138 
139 	return ret;
140 }
141 
142 int device_bind_by_name(struct udevice *parent, bool pre_reloc_only,
143 			const struct driver_info *info, struct udevice **devp)
144 {
145 	struct driver *drv;
146 
147 	drv = lists_driver_lookup_name(info->name);
148 	if (!drv)
149 		return -ENOENT;
150 	if (pre_reloc_only && !(drv->flags & DM_FLAG_PRE_RELOC))
151 		return -EPERM;
152 
153 	return device_bind(parent, drv, info->name, (void *)info->platdata,
154 			   -1, devp);
155 }
156 
157 int device_probe_child(struct udevice *dev, void *parent_priv)
158 {
159 	struct driver *drv;
160 	int size = 0;
161 	int ret;
162 	int seq;
163 
164 	if (!dev)
165 		return -EINVAL;
166 
167 	if (dev->flags & DM_FLAG_ACTIVATED)
168 		return 0;
169 
170 	drv = dev->driver;
171 	assert(drv);
172 
173 	/* Allocate private data if requested */
174 	if (drv->priv_auto_alloc_size) {
175 		dev->priv = calloc(1, drv->priv_auto_alloc_size);
176 		if (!dev->priv) {
177 			ret = -ENOMEM;
178 			goto fail;
179 		}
180 	}
181 	/* Allocate private data if requested */
182 	size = dev->uclass->uc_drv->per_device_auto_alloc_size;
183 	if (size) {
184 		dev->uclass_priv = calloc(1, size);
185 		if (!dev->uclass_priv) {
186 			ret = -ENOMEM;
187 			goto fail;
188 		}
189 	}
190 
191 	/* Ensure all parents are probed */
192 	if (dev->parent) {
193 		size = dev->parent->driver->per_child_auto_alloc_size;
194 		if (size) {
195 			dev->parent_priv = calloc(1, size);
196 			if (!dev->parent_priv) {
197 				ret = -ENOMEM;
198 				goto fail;
199 			}
200 			if (parent_priv)
201 				memcpy(dev->parent_priv, parent_priv, size);
202 		}
203 
204 		ret = device_probe(dev->parent);
205 		if (ret)
206 			goto fail;
207 	}
208 
209 	seq = uclass_resolve_seq(dev);
210 	if (seq < 0) {
211 		ret = seq;
212 		goto fail;
213 	}
214 	dev->seq = seq;
215 
216 	if (dev->parent && dev->parent->driver->child_pre_probe) {
217 		ret = dev->parent->driver->child_pre_probe(dev);
218 		if (ret)
219 			goto fail;
220 	}
221 
222 	if (drv->ofdata_to_platdata && dev->of_offset >= 0) {
223 		ret = drv->ofdata_to_platdata(dev);
224 		if (ret)
225 			goto fail;
226 	}
227 
228 	if (drv->probe) {
229 		ret = drv->probe(dev);
230 		if (ret)
231 			goto fail;
232 	}
233 
234 	dev->flags |= DM_FLAG_ACTIVATED;
235 
236 	ret = uclass_post_probe_device(dev);
237 	if (ret) {
238 		dev->flags &= ~DM_FLAG_ACTIVATED;
239 		goto fail_uclass;
240 	}
241 
242 	return 0;
243 fail_uclass:
244 	if (device_remove(dev)) {
245 		dm_warn("%s: Device '%s' failed to remove on error path\n",
246 			__func__, dev->name);
247 	}
248 fail:
249 	dev->seq = -1;
250 	device_free(dev);
251 
252 	return ret;
253 }
254 
255 int device_probe(struct udevice *dev)
256 {
257 	return device_probe_child(dev, NULL);
258 }
259 
260 void *dev_get_platdata(struct udevice *dev)
261 {
262 	if (!dev) {
263 		dm_warn("%s: null device\n", __func__);
264 		return NULL;
265 	}
266 
267 	return dev->platdata;
268 }
269 
270 void *dev_get_parent_platdata(struct udevice *dev)
271 {
272 	if (!dev) {
273 		dm_warn("%s: null device", __func__);
274 		return NULL;
275 	}
276 
277 	return dev->parent_platdata;
278 }
279 
280 void *dev_get_priv(struct udevice *dev)
281 {
282 	if (!dev) {
283 		dm_warn("%s: null device\n", __func__);
284 		return NULL;
285 	}
286 
287 	return dev->priv;
288 }
289 
290 void *dev_get_parentdata(struct udevice *dev)
291 {
292 	if (!dev) {
293 		dm_warn("%s: null device\n", __func__);
294 		return NULL;
295 	}
296 
297 	return dev->parent_priv;
298 }
299 
300 static int device_get_device_tail(struct udevice *dev, int ret,
301 				  struct udevice **devp)
302 {
303 	if (ret)
304 		return ret;
305 
306 	ret = device_probe(dev);
307 	if (ret)
308 		return ret;
309 
310 	*devp = dev;
311 
312 	return 0;
313 }
314 
315 int device_get_child(struct udevice *parent, int index, struct udevice **devp)
316 {
317 	struct udevice *dev;
318 
319 	list_for_each_entry(dev, &parent->child_head, sibling_node) {
320 		if (!index--)
321 			return device_get_device_tail(dev, 0, devp);
322 	}
323 
324 	return -ENODEV;
325 }
326 
327 int device_find_child_by_seq(struct udevice *parent, int seq_or_req_seq,
328 			     bool find_req_seq, struct udevice **devp)
329 {
330 	struct udevice *dev;
331 
332 	*devp = NULL;
333 	if (seq_or_req_seq == -1)
334 		return -ENODEV;
335 
336 	list_for_each_entry(dev, &parent->child_head, sibling_node) {
337 		if ((find_req_seq ? dev->req_seq : dev->seq) ==
338 				seq_or_req_seq) {
339 			*devp = dev;
340 			return 0;
341 		}
342 	}
343 
344 	return -ENODEV;
345 }
346 
347 int device_get_child_by_seq(struct udevice *parent, int seq,
348 			    struct udevice **devp)
349 {
350 	struct udevice *dev;
351 	int ret;
352 
353 	*devp = NULL;
354 	ret = device_find_child_by_seq(parent, seq, false, &dev);
355 	if (ret == -ENODEV) {
356 		/*
357 		 * We didn't find it in probed devices. See if there is one
358 		 * that will request this seq if probed.
359 		 */
360 		ret = device_find_child_by_seq(parent, seq, true, &dev);
361 	}
362 	return device_get_device_tail(dev, ret, devp);
363 }
364 
365 int device_find_child_by_of_offset(struct udevice *parent, int of_offset,
366 				   struct udevice **devp)
367 {
368 	struct udevice *dev;
369 
370 	*devp = NULL;
371 
372 	list_for_each_entry(dev, &parent->child_head, sibling_node) {
373 		if (dev->of_offset == of_offset) {
374 			*devp = dev;
375 			return 0;
376 		}
377 	}
378 
379 	return -ENODEV;
380 }
381 
382 int device_get_child_by_of_offset(struct udevice *parent, int seq,
383 				  struct udevice **devp)
384 {
385 	struct udevice *dev;
386 	int ret;
387 
388 	*devp = NULL;
389 	ret = device_find_child_by_of_offset(parent, seq, &dev);
390 	return device_get_device_tail(dev, ret, devp);
391 }
392 
393 int device_find_first_child(struct udevice *parent, struct udevice **devp)
394 {
395 	if (list_empty(&parent->child_head)) {
396 		*devp = NULL;
397 	} else {
398 		*devp = list_first_entry(&parent->child_head, struct udevice,
399 					 sibling_node);
400 	}
401 
402 	return 0;
403 }
404 
405 int device_find_next_child(struct udevice **devp)
406 {
407 	struct udevice *dev = *devp;
408 	struct udevice *parent = dev->parent;
409 
410 	if (list_is_last(&dev->sibling_node, &parent->child_head)) {
411 		*devp = NULL;
412 	} else {
413 		*devp = list_entry(dev->sibling_node.next, struct udevice,
414 				   sibling_node);
415 	}
416 
417 	return 0;
418 }
419 
420 struct udevice *dev_get_parent(struct udevice *child)
421 {
422 	return child->parent;
423 }
424 
425 ulong dev_get_of_data(struct udevice *dev)
426 {
427 	return dev->of_id->data;
428 }
429