blob: 269087a084cf6edbf1637de12602b5773c79e394 [file] [log] [blame]
Simon Glass6494d702014-02-26 15:59:18 -07001/*
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>
Simon Glass5a66a8f2014-07-23 06:55:12 -060013#include <fdtdec.h>
Stefan Roeseef5cd332015-09-02 07:41:12 +020014#include <fdt_support.h>
Simon Glass6494d702014-02-26 15:59:18 -070015#include <malloc.h>
16#include <dm/device.h>
17#include <dm/device-internal.h>
18#include <dm/lists.h>
Masahiro Yamadad90a5a32015-08-27 12:44:29 +090019#include <dm/pinctrl.h>
Simon Glass6494d702014-02-26 15:59:18 -070020#include <dm/platdata.h>
21#include <dm/uclass.h>
22#include <dm/uclass-internal.h>
23#include <dm/util.h>
24#include <linux/err.h>
25#include <linux/list.h>
26
Simon Glass5a66a8f2014-07-23 06:55:12 -060027DECLARE_GLOBAL_DATA_PTR;
28
Simon Glass34792532015-03-25 12:21:54 -060029int device_bind(struct udevice *parent, const struct driver *drv,
30 const char *name, void *platdata, int of_offset,
31 struct udevice **devp)
Simon Glass6494d702014-02-26 15:59:18 -070032{
Heiko Schocher54c5d082014-05-22 12:43:05 +020033 struct udevice *dev;
Simon Glass6494d702014-02-26 15:59:18 -070034 struct uclass *uc;
Przemyslaw Marczak5eaed882015-04-15 13:07:18 +020035 int size, ret = 0;
Simon Glass6494d702014-02-26 15:59:18 -070036
Masahiro Yamadae6cabe42015-08-27 12:44:28 +090037 if (devp)
38 *devp = NULL;
Simon Glass6494d702014-02-26 15:59:18 -070039 if (!name)
40 return -EINVAL;
41
42 ret = uclass_get(drv->id, &uc);
Simon Glass3346c872015-08-30 16:55:16 -060043 if (ret) {
44 debug("Missing uclass for driver %s\n", drv->name);
Simon Glass6494d702014-02-26 15:59:18 -070045 return ret;
Simon Glass3346c872015-08-30 16:55:16 -060046 }
Simon Glass6494d702014-02-26 15:59:18 -070047
Heiko Schocher54c5d082014-05-22 12:43:05 +020048 dev = calloc(1, sizeof(struct udevice));
Simon Glass6494d702014-02-26 15:59:18 -070049 if (!dev)
50 return -ENOMEM;
51
52 INIT_LIST_HEAD(&dev->sibling_node);
53 INIT_LIST_HEAD(&dev->child_head);
54 INIT_LIST_HEAD(&dev->uclass_node);
Masahiro Yamadae2282d72015-07-25 21:52:37 +090055#ifdef CONFIG_DEVRES
Masahiro Yamada608f26c2015-07-25 21:52:35 +090056 INIT_LIST_HEAD(&dev->devres_head);
Masahiro Yamadae2282d72015-07-25 21:52:37 +090057#endif
Simon Glass6494d702014-02-26 15:59:18 -070058 dev->platdata = platdata;
59 dev->name = name;
60 dev->of_offset = of_offset;
61 dev->parent = parent;
62 dev->driver = drv;
63 dev->uclass = uc;
Simon Glass5a66a8f2014-07-23 06:55:12 -060064
Simon Glass5a66a8f2014-07-23 06:55:12 -060065 dev->seq = -1;
Simon Glass91cbd792014-09-17 09:02:38 -060066 dev->req_seq = -1;
Nathan Rossi4f627c52016-01-08 03:00:45 +100067 if (CONFIG_IS_ENABLED(OF_CONTROL) && CONFIG_IS_ENABLED(DM_SEQ_ALIAS)) {
Simon Glass36fa61d2015-02-27 22:06:30 -070068 /*
Stefan Roese770eb302016-03-16 09:58:01 +010069 * Some devices, such as a SPI bus, I2C bus and serial ports
70 * are numbered using aliases.
71 *
72 * This is just a 'requested' sequence, and will be
73 * resolved (and ->seq updated) when the device is probed.
74 */
Simon Glass36fa61d2015-02-27 22:06:30 -070075 if (uc->uc_drv->flags & DM_UC_FLAG_SEQ_ALIAS) {
76 if (uc->uc_drv->name && of_offset != -1) {
77 fdtdec_get_alias_seq(gd->fdt_blob,
78 uc->uc_drv->name, of_offset,
79 &dev->req_seq);
80 }
Simon Glass9cc36a22015-01-25 08:27:05 -070081 }
82 }
Simon Glass36fa61d2015-02-27 22:06:30 -070083
Simon Glassf8a85442015-01-25 08:27:00 -070084 if (!dev->platdata && drv->platdata_auto_alloc_size) {
Simon Glass6494d702014-02-26 15:59:18 -070085 dev->flags |= DM_FLAG_ALLOC_PDATA;
Simon Glassf8a85442015-01-25 08:27:00 -070086 dev->platdata = calloc(1, drv->platdata_auto_alloc_size);
87 if (!dev->platdata) {
88 ret = -ENOMEM;
89 goto fail_alloc1;
90 }
91 }
Simon Glasscdc133b2015-01-25 08:27:01 -070092
Przemyslaw Marczak5eaed882015-04-15 13:07:18 +020093 size = uc->uc_drv->per_device_platdata_auto_alloc_size;
94 if (size) {
95 dev->flags |= DM_FLAG_ALLOC_UCLASS_PDATA;
96 dev->uclass_platdata = calloc(1, size);
97 if (!dev->uclass_platdata) {
98 ret = -ENOMEM;
99 goto fail_alloc2;
100 }
101 }
102
103 if (parent) {
104 size = parent->driver->per_child_platdata_auto_alloc_size;
Simon Glassba8da9d2015-01-25 08:27:02 -0700105 if (!size) {
106 size = parent->uclass->uc_drv->
107 per_child_platdata_auto_alloc_size;
108 }
Simon Glasscdc133b2015-01-25 08:27:01 -0700109 if (size) {
110 dev->flags |= DM_FLAG_ALLOC_PARENT_PDATA;
111 dev->parent_platdata = calloc(1, size);
112 if (!dev->parent_platdata) {
113 ret = -ENOMEM;
Przemyslaw Marczak5eaed882015-04-15 13:07:18 +0200114 goto fail_alloc3;
Simon Glasscdc133b2015-01-25 08:27:01 -0700115 }
116 }
117 }
Simon Glass6494d702014-02-26 15:59:18 -0700118
119 /* put dev into parent's successor list */
120 if (parent)
121 list_add_tail(&dev->sibling_node, &parent->child_head);
122
123 ret = uclass_bind_device(dev);
124 if (ret)
Simon Glass72ebfe82015-01-25 08:26:59 -0700125 goto fail_uclass_bind;
Simon Glass6494d702014-02-26 15:59:18 -0700126
127 /* if we fail to bind we remove device from successors and free it */
128 if (drv->bind) {
129 ret = drv->bind(dev);
Simon Glass72ebfe82015-01-25 08:26:59 -0700130 if (ret)
Simon Glass6494d702014-02-26 15:59:18 -0700131 goto fail_bind;
Simon Glass6494d702014-02-26 15:59:18 -0700132 }
Simon Glass0118ce72015-01-25 08:27:03 -0700133 if (parent && parent->driver->child_post_bind) {
134 ret = parent->driver->child_post_bind(dev);
135 if (ret)
136 goto fail_child_post_bind;
137 }
Simon Glass20af3c02016-01-05 09:30:59 -0700138 if (uc->uc_drv->post_bind) {
139 ret = uc->uc_drv->post_bind(dev);
140 if (ret)
141 goto fail_uclass_post_bind;
142 }
Simon Glass0118ce72015-01-25 08:27:03 -0700143
Simon Glass6494d702014-02-26 15:59:18 -0700144 if (parent)
145 dm_dbg("Bound device %s to %s\n", dev->name, parent->name);
Masahiro Yamadae6cabe42015-08-27 12:44:28 +0900146 if (devp)
147 *devp = dev;
Simon Glass6494d702014-02-26 15:59:18 -0700148
Masahiro Yamadaaed1a4d2015-07-25 21:52:34 +0900149 dev->flags |= DM_FLAG_BOUND;
150
Simon Glass6494d702014-02-26 15:59:18 -0700151 return 0;
152
Simon Glass20af3c02016-01-05 09:30:59 -0700153fail_uclass_post_bind:
154 /* There is no child unbind() method, so no clean-up required */
Simon Glass0118ce72015-01-25 08:27:03 -0700155fail_child_post_bind:
Masahiro Yamada0a5804b2015-08-12 07:31:52 +0900156 if (CONFIG_IS_ENABLED(DM_DEVICE_REMOVE)) {
Simon Glass5a87c412015-02-27 22:06:33 -0700157 if (drv->unbind && drv->unbind(dev)) {
158 dm_warn("unbind() method failed on dev '%s' on error path\n",
159 dev->name);
160 }
Simon Glass0118ce72015-01-25 08:27:03 -0700161 }
162
Simon Glass6494d702014-02-26 15:59:18 -0700163fail_bind:
Masahiro Yamada0a5804b2015-08-12 07:31:52 +0900164 if (CONFIG_IS_ENABLED(DM_DEVICE_REMOVE)) {
Simon Glass5a87c412015-02-27 22:06:33 -0700165 if (uclass_unbind_device(dev)) {
166 dm_warn("Failed to unbind dev '%s' on error path\n",
167 dev->name);
168 }
Simon Glass72ebfe82015-01-25 08:26:59 -0700169 }
170fail_uclass_bind:
Masahiro Yamada0a5804b2015-08-12 07:31:52 +0900171 if (CONFIG_IS_ENABLED(DM_DEVICE_REMOVE)) {
Simon Glass5a87c412015-02-27 22:06:33 -0700172 list_del(&dev->sibling_node);
173 if (dev->flags & DM_FLAG_ALLOC_PARENT_PDATA) {
174 free(dev->parent_platdata);
175 dev->parent_platdata = NULL;
176 }
Simon Glasscdc133b2015-01-25 08:27:01 -0700177 }
Przemyslaw Marczak5eaed882015-04-15 13:07:18 +0200178fail_alloc3:
179 if (dev->flags & DM_FLAG_ALLOC_UCLASS_PDATA) {
180 free(dev->uclass_platdata);
181 dev->uclass_platdata = NULL;
182 }
Simon Glasscdc133b2015-01-25 08:27:01 -0700183fail_alloc2:
Simon Glassf8a85442015-01-25 08:27:00 -0700184 if (dev->flags & DM_FLAG_ALLOC_PDATA) {
185 free(dev->platdata);
186 dev->platdata = NULL;
187 }
188fail_alloc1:
Masahiro Yamada608f26c2015-07-25 21:52:35 +0900189 devres_release_all(dev);
190
Simon Glass6494d702014-02-26 15:59:18 -0700191 free(dev);
Simon Glass72ebfe82015-01-25 08:26:59 -0700192
Simon Glass6494d702014-02-26 15:59:18 -0700193 return ret;
194}
195
Simon Glass00606d72014-07-23 06:55:03 -0600196int device_bind_by_name(struct udevice *parent, bool pre_reloc_only,
197 const struct driver_info *info, struct udevice **devp)
Simon Glass6494d702014-02-26 15:59:18 -0700198{
199 struct driver *drv;
200
201 drv = lists_driver_lookup_name(info->name);
202 if (!drv)
203 return -ENOENT;
Simon Glass00606d72014-07-23 06:55:03 -0600204 if (pre_reloc_only && !(drv->flags & DM_FLAG_PRE_RELOC))
205 return -EPERM;
Simon Glass6494d702014-02-26 15:59:18 -0700206
207 return device_bind(parent, drv, info->name, (void *)info->platdata,
208 -1, devp);
209}
210
Simon Glass2c03c462015-03-25 12:21:53 -0600211static void *alloc_priv(int size, uint flags)
212{
213 void *priv;
214
215 if (flags & DM_FLAG_ALLOC_PRIV_DMA) {
216 priv = memalign(ARCH_DMA_MINALIGN, size);
217 if (priv)
218 memset(priv, '\0', size);
219 } else {
220 priv = calloc(1, size);
221 }
222
223 return priv;
224}
225
Simon Glassc6db9652016-01-25 14:58:42 -0700226int device_probe(struct udevice *dev)
Simon Glass6494d702014-02-26 15:59:18 -0700227{
Simon Glass34792532015-03-25 12:21:54 -0600228 const struct driver *drv;
Simon Glass6494d702014-02-26 15:59:18 -0700229 int size = 0;
230 int ret;
Simon Glass5a66a8f2014-07-23 06:55:12 -0600231 int seq;
Simon Glass6494d702014-02-26 15:59:18 -0700232
233 if (!dev)
234 return -EINVAL;
235
236 if (dev->flags & DM_FLAG_ACTIVATED)
237 return 0;
238
239 drv = dev->driver;
240 assert(drv);
241
Bin Mengcdeb2ba2015-08-24 01:14:02 -0700242 /* Allocate private data if requested and not reentered */
243 if (drv->priv_auto_alloc_size && !dev->priv) {
Simon Glass2c03c462015-03-25 12:21:53 -0600244 dev->priv = alloc_priv(drv->priv_auto_alloc_size, drv->flags);
Simon Glass6494d702014-02-26 15:59:18 -0700245 if (!dev->priv) {
246 ret = -ENOMEM;
247 goto fail;
248 }
249 }
Bin Mengcdeb2ba2015-08-24 01:14:02 -0700250 /* Allocate private data if requested and not reentered */
Simon Glass6494d702014-02-26 15:59:18 -0700251 size = dev->uclass->uc_drv->per_device_auto_alloc_size;
Bin Mengcdeb2ba2015-08-24 01:14:02 -0700252 if (size && !dev->uclass_priv) {
Simon Glass6494d702014-02-26 15:59:18 -0700253 dev->uclass_priv = calloc(1, size);
254 if (!dev->uclass_priv) {
255 ret = -ENOMEM;
256 goto fail;
257 }
258 }
259
260 /* Ensure all parents are probed */
261 if (dev->parent) {
Simon Glasse59f4582014-07-23 06:55:20 -0600262 size = dev->parent->driver->per_child_auto_alloc_size;
Simon Glassdac8db22015-01-25 08:27:06 -0700263 if (!size) {
264 size = dev->parent->uclass->uc_drv->
265 per_child_auto_alloc_size;
266 }
Bin Mengcdeb2ba2015-08-24 01:14:02 -0700267 if (size && !dev->parent_priv) {
Simon Glass2c03c462015-03-25 12:21:53 -0600268 dev->parent_priv = alloc_priv(size, drv->flags);
Simon Glasse59f4582014-07-23 06:55:20 -0600269 if (!dev->parent_priv) {
270 ret = -ENOMEM;
271 goto fail;
272 }
273 }
274
Simon Glass6494d702014-02-26 15:59:18 -0700275 ret = device_probe(dev->parent);
276 if (ret)
277 goto fail;
Bin Mengcdeb2ba2015-08-24 01:14:02 -0700278
279 /*
280 * The device might have already been probed during
281 * the call to device_probe() on its parent device
282 * (e.g. PCI bridge devices). Test the flags again
283 * so that we don't mess up the device.
284 */
285 if (dev->flags & DM_FLAG_ACTIVATED)
286 return 0;
Simon Glass6494d702014-02-26 15:59:18 -0700287 }
288
Simon Glass5a66a8f2014-07-23 06:55:12 -0600289 seq = uclass_resolve_seq(dev);
290 if (seq < 0) {
291 ret = seq;
292 goto fail;
293 }
294 dev->seq = seq;
295
Simon Glass206d4d22015-03-25 12:21:56 -0600296 dev->flags |= DM_FLAG_ACTIVATED;
297
Simon Glass84d26e22015-09-12 08:45:19 -0600298 /*
299 * Process pinctrl for everything except the root device, and
Simon Glass03795972016-01-21 19:43:25 -0700300 * continue regardless of the result of pinctrl. Don't process pinctrl
301 * settings for pinctrl devices since the device may not yet be
302 * probed.
Simon Glass84d26e22015-09-12 08:45:19 -0600303 */
Simon Glass03795972016-01-21 19:43:25 -0700304 if (dev->parent && device_get_uclass_id(dev) != UCLASS_PINCTRL)
Simon Glass84d26e22015-09-12 08:45:19 -0600305 pinctrl_select_state(dev, "default");
Masahiro Yamadad90a5a32015-08-27 12:44:29 +0900306
Simon Glass02c07b32015-03-05 12:25:22 -0700307 ret = uclass_pre_probe_device(dev);
Simon Glass83c7e432015-01-25 08:27:10 -0700308 if (ret)
309 goto fail;
310
Simon Glassa327dee2014-07-23 06:55:21 -0600311 if (dev->parent && dev->parent->driver->child_pre_probe) {
312 ret = dev->parent->driver->child_pre_probe(dev);
313 if (ret)
314 goto fail;
315 }
316
Simon Glass6494d702014-02-26 15:59:18 -0700317 if (drv->ofdata_to_platdata && dev->of_offset >= 0) {
318 ret = drv->ofdata_to_platdata(dev);
319 if (ret)
320 goto fail;
321 }
322
323 if (drv->probe) {
324 ret = drv->probe(dev);
Simon Glass02eeb1b2015-03-05 12:25:21 -0700325 if (ret) {
326 dev->flags &= ~DM_FLAG_ACTIVATED;
Simon Glass6494d702014-02-26 15:59:18 -0700327 goto fail;
Simon Glass02eeb1b2015-03-05 12:25:21 -0700328 }
Simon Glass6494d702014-02-26 15:59:18 -0700329 }
330
Simon Glass6494d702014-02-26 15:59:18 -0700331 ret = uclass_post_probe_device(dev);
Simon Glass206d4d22015-03-25 12:21:56 -0600332 if (ret)
Simon Glass6494d702014-02-26 15:59:18 -0700333 goto fail_uclass;
Simon Glass6494d702014-02-26 15:59:18 -0700334
Peng Fanc3ab9852016-03-12 13:17:38 +0800335 if (dev->parent && device_get_uclass_id(dev) == UCLASS_PINCTRL)
336 pinctrl_select_state(dev, "default");
337
Simon Glass6494d702014-02-26 15:59:18 -0700338 return 0;
339fail_uclass:
340 if (device_remove(dev)) {
341 dm_warn("%s: Device '%s' failed to remove on error path\n",
342 __func__, dev->name);
343 }
344fail:
Simon Glass206d4d22015-03-25 12:21:56 -0600345 dev->flags &= ~DM_FLAG_ACTIVATED;
346
Simon Glass5a66a8f2014-07-23 06:55:12 -0600347 dev->seq = -1;
Simon Glass6494d702014-02-26 15:59:18 -0700348 device_free(dev);
349
350 return ret;
351}
352
Heiko Schocher54c5d082014-05-22 12:43:05 +0200353void *dev_get_platdata(struct udevice *dev)
Simon Glass6494d702014-02-26 15:59:18 -0700354{
355 if (!dev) {
Simon Glass964d1532014-12-10 08:55:56 -0700356 dm_warn("%s: null device\n", __func__);
Simon Glass6494d702014-02-26 15:59:18 -0700357 return NULL;
358 }
359
360 return dev->platdata;
361}
362
Simon Glasscdc133b2015-01-25 08:27:01 -0700363void *dev_get_parent_platdata(struct udevice *dev)
364{
365 if (!dev) {
Simon Glass36d7cc12015-07-06 16:47:40 -0600366 dm_warn("%s: null device\n", __func__);
Simon Glasscdc133b2015-01-25 08:27:01 -0700367 return NULL;
368 }
369
370 return dev->parent_platdata;
371}
372
Przemyslaw Marczak5eaed882015-04-15 13:07:18 +0200373void *dev_get_uclass_platdata(struct udevice *dev)
374{
375 if (!dev) {
Simon Glass36d7cc12015-07-06 16:47:40 -0600376 dm_warn("%s: null device\n", __func__);
Przemyslaw Marczak5eaed882015-04-15 13:07:18 +0200377 return NULL;
378 }
379
380 return dev->uclass_platdata;
381}
382
Heiko Schocher54c5d082014-05-22 12:43:05 +0200383void *dev_get_priv(struct udevice *dev)
Simon Glass6494d702014-02-26 15:59:18 -0700384{
385 if (!dev) {
Simon Glass964d1532014-12-10 08:55:56 -0700386 dm_warn("%s: null device\n", __func__);
Simon Glass6494d702014-02-26 15:59:18 -0700387 return NULL;
388 }
389
390 return dev->priv;
391}
Simon Glass997c87b2014-07-23 06:55:19 -0600392
Simon Glasse564f052015-03-05 12:25:20 -0700393void *dev_get_uclass_priv(struct udevice *dev)
394{
395 if (!dev) {
396 dm_warn("%s: null device\n", __func__);
397 return NULL;
398 }
399
400 return dev->uclass_priv;
401}
402
Simon Glassbcbe3d12015-09-28 23:32:01 -0600403void *dev_get_parent_priv(struct udevice *dev)
Simon Glasse59f4582014-07-23 06:55:20 -0600404{
405 if (!dev) {
Simon Glass964d1532014-12-10 08:55:56 -0700406 dm_warn("%s: null device\n", __func__);
Simon Glasse59f4582014-07-23 06:55:20 -0600407 return NULL;
408 }
409
410 return dev->parent_priv;
411}
412
Simon Glass997c87b2014-07-23 06:55:19 -0600413static int device_get_device_tail(struct udevice *dev, int ret,
414 struct udevice **devp)
415{
416 if (ret)
417 return ret;
418
419 ret = device_probe(dev);
420 if (ret)
421 return ret;
422
423 *devp = dev;
424
425 return 0;
426}
427
428int device_get_child(struct udevice *parent, int index, struct udevice **devp)
429{
430 struct udevice *dev;
431
432 list_for_each_entry(dev, &parent->child_head, sibling_node) {
433 if (!index--)
434 return device_get_device_tail(dev, 0, devp);
435 }
436
437 return -ENODEV;
438}
439
440int device_find_child_by_seq(struct udevice *parent, int seq_or_req_seq,
441 bool find_req_seq, struct udevice **devp)
442{
443 struct udevice *dev;
444
445 *devp = NULL;
446 if (seq_or_req_seq == -1)
447 return -ENODEV;
448
449 list_for_each_entry(dev, &parent->child_head, sibling_node) {
450 if ((find_req_seq ? dev->req_seq : dev->seq) ==
451 seq_or_req_seq) {
452 *devp = dev;
453 return 0;
454 }
455 }
456
457 return -ENODEV;
458}
459
460int device_get_child_by_seq(struct udevice *parent, int seq,
461 struct udevice **devp)
462{
463 struct udevice *dev;
464 int ret;
465
466 *devp = NULL;
467 ret = device_find_child_by_seq(parent, seq, false, &dev);
468 if (ret == -ENODEV) {
469 /*
470 * We didn't find it in probed devices. See if there is one
471 * that will request this seq if probed.
472 */
473 ret = device_find_child_by_seq(parent, seq, true, &dev);
474 }
475 return device_get_device_tail(dev, ret, devp);
476}
477
478int device_find_child_by_of_offset(struct udevice *parent, int of_offset,
479 struct udevice **devp)
480{
481 struct udevice *dev;
482
483 *devp = NULL;
484
485 list_for_each_entry(dev, &parent->child_head, sibling_node) {
486 if (dev->of_offset == of_offset) {
487 *devp = dev;
488 return 0;
489 }
490 }
491
492 return -ENODEV;
493}
494
Simon Glass132f9bf2015-06-23 15:38:38 -0600495int device_get_child_by_of_offset(struct udevice *parent, int node,
Simon Glass997c87b2014-07-23 06:55:19 -0600496 struct udevice **devp)
497{
498 struct udevice *dev;
499 int ret;
500
501 *devp = NULL;
Simon Glass132f9bf2015-06-23 15:38:38 -0600502 ret = device_find_child_by_of_offset(parent, node, &dev);
Simon Glass997c87b2014-07-23 06:55:19 -0600503 return device_get_device_tail(dev, ret, devp);
504}
Simon Glassa8981d42014-10-13 23:41:49 -0600505
Simon Glass26930472015-06-23 15:38:37 -0600506static struct udevice *_device_find_global_by_of_offset(struct udevice *parent,
507 int of_offset)
508{
509 struct udevice *dev, *found;
510
511 if (parent->of_offset == of_offset)
512 return parent;
513
514 list_for_each_entry(dev, &parent->child_head, sibling_node) {
515 found = _device_find_global_by_of_offset(dev, of_offset);
516 if (found)
517 return found;
518 }
519
520 return NULL;
521}
522
523int device_get_global_by_of_offset(int of_offset, struct udevice **devp)
524{
525 struct udevice *dev;
526
527 dev = _device_find_global_by_of_offset(gd->dm_root, of_offset);
528 return device_get_device_tail(dev, dev ? 0 : -ENOENT, devp);
529}
530
Simon Glassa8981d42014-10-13 23:41:49 -0600531int device_find_first_child(struct udevice *parent, struct udevice **devp)
532{
533 if (list_empty(&parent->child_head)) {
534 *devp = NULL;
535 } else {
536 *devp = list_first_entry(&parent->child_head, struct udevice,
537 sibling_node);
538 }
539
540 return 0;
541}
542
543int device_find_next_child(struct udevice **devp)
544{
545 struct udevice *dev = *devp;
546 struct udevice *parent = dev->parent;
547
548 if (list_is_last(&dev->sibling_node, &parent->child_head)) {
549 *devp = NULL;
550 } else {
551 *devp = list_entry(dev->sibling_node.next, struct udevice,
552 sibling_node);
553 }
554
555 return 0;
556}
Simon Glass2ef249b2014-11-11 10:46:18 -0700557
Simon Glass479728c2014-11-11 10:46:19 -0700558struct udevice *dev_get_parent(struct udevice *child)
559{
560 return child->parent;
561}
562
Simon Glass39de8432015-03-25 12:21:55 -0600563ulong dev_get_driver_data(struct udevice *dev)
Simon Glass2ef249b2014-11-11 10:46:18 -0700564{
Simon Glass39de8432015-03-25 12:21:55 -0600565 return dev->driver_data;
Simon Glass2ef249b2014-11-11 10:46:18 -0700566}
Simon Glassb3670532015-01-25 08:27:04 -0700567
Przemyslaw Marczakcc73d372015-04-15 13:07:24 +0200568const void *dev_get_driver_ops(struct udevice *dev)
569{
570 if (!dev || !dev->driver->ops)
571 return NULL;
572
573 return dev->driver->ops;
574}
575
Simon Glassb3670532015-01-25 08:27:04 -0700576enum uclass_id device_get_uclass_id(struct udevice *dev)
577{
578 return dev->uclass->uc_drv->id;
579}
Peng Fanc9cac3f2015-02-10 14:46:32 +0800580
Przemyslaw Marczakf9c370d2015-04-15 13:07:25 +0200581const char *dev_get_uclass_name(struct udevice *dev)
582{
583 if (!dev)
584 return NULL;
585
586 return dev->uclass->uc_drv->name;
587}
588
Mugunthan V N69b41382015-12-23 20:39:36 +0530589fdt_addr_t dev_get_addr_index(struct udevice *dev, int index)
Simon Glassf3301772015-07-07 20:53:44 -0600590{
Masahiro Yamada0f925822015-08-12 07:31:55 +0900591#if CONFIG_IS_ENABLED(OF_CONTROL)
Simon Glassf3301772015-07-07 20:53:44 -0600592 fdt_addr_t addr;
593
Stefan Roeseef5cd332015-09-02 07:41:12 +0200594 if (CONFIG_IS_ENABLED(OF_TRANSLATE)) {
595 const fdt32_t *reg;
Mugunthan V N69b41382015-12-23 20:39:36 +0530596 int len = 0;
597 int na, ns;
Stefan Roeseef5cd332015-09-02 07:41:12 +0200598
Mugunthan V N69b41382015-12-23 20:39:36 +0530599 na = fdt_address_cells(gd->fdt_blob, dev->parent->of_offset);
600 if (na < 1) {
601 debug("bad #address-cells\n");
Stefan Roeseef5cd332015-09-02 07:41:12 +0200602 return FDT_ADDR_T_NONE;
Mugunthan V N69b41382015-12-23 20:39:36 +0530603 }
604
605 ns = fdt_size_cells(gd->fdt_blob, dev->parent->of_offset);
606 if (ns < 0) {
607 debug("bad #size-cells\n");
608 return FDT_ADDR_T_NONE;
609 }
610
611 reg = fdt_getprop(gd->fdt_blob, dev->of_offset, "reg", &len);
612 if (!reg || (len <= (index * sizeof(fdt32_t) * (na + ns)))) {
613 debug("Req index out of range\n");
614 return FDT_ADDR_T_NONE;
615 }
616
617 reg += index * (na + ns);
Stefan Roeseef5cd332015-09-02 07:41:12 +0200618
619 /*
620 * Use the full-fledged translate function for complex
621 * bus setups.
622 */
Stefan Roese66eaea62015-12-14 16:18:15 +0100623 addr = fdt_translate_address((void *)gd->fdt_blob,
Stefan Roeseef5cd332015-09-02 07:41:12 +0200624 dev->of_offset, reg);
Stefan Roese66eaea62015-12-14 16:18:15 +0100625 } else {
626 /*
627 * Use the "simple" translate function for less complex
628 * bus setups.
629 */
630 addr = fdtdec_get_addr_size_auto_parent(gd->fdt_blob,
631 dev->parent->of_offset,
632 dev->of_offset, "reg",
Mugunthan V N69b41382015-12-23 20:39:36 +0530633 index, NULL);
Stefan Roese66eaea62015-12-14 16:18:15 +0100634 if (CONFIG_IS_ENABLED(SIMPLE_BUS) && addr != FDT_ADDR_T_NONE) {
635 if (device_get_uclass_id(dev->parent) ==
636 UCLASS_SIMPLE_BUS)
637 addr = simple_bus_translate(dev->parent, addr);
638 }
Stefan Roeseef5cd332015-09-02 07:41:12 +0200639 }
640
641 /*
Stefan Roese66eaea62015-12-14 16:18:15 +0100642 * Some platforms need a special address translation. Those
643 * platforms (e.g. mvebu in SPL) can configure a translation
644 * offset in the DM by calling dm_set_translation_offset() that
645 * will get added to all addresses returned by dev_get_addr().
Stefan Roeseef5cd332015-09-02 07:41:12 +0200646 */
Stefan Roese66eaea62015-12-14 16:18:15 +0100647 addr += dm_get_translation_offset();
Simon Glassf3301772015-07-07 20:53:44 -0600648
649 return addr;
Peng Fanc9cac3f2015-02-10 14:46:32 +0800650#else
Peng Fanc9cac3f2015-02-10 14:46:32 +0800651 return FDT_ADDR_T_NONE;
Peng Fanc9cac3f2015-02-10 14:46:32 +0800652#endif
Simon Glassf3301772015-07-07 20:53:44 -0600653}
Simon Glassc5785672015-03-25 12:21:57 -0600654
Stephen Warren43c4d442016-04-06 12:49:19 -0600655fdt_addr_t dev_get_addr_name(struct udevice *dev, const char *name)
656{
657#if CONFIG_IS_ENABLED(OF_CONTROL)
658 int index;
659
660 index = fdt_find_string(gd->fdt_blob, dev->parent->of_offset,
661 "reg-names", name);
662 if (index < 0)
663 return index;
664
665 return dev_get_addr_index(dev, index);
666#else
667 return FDT_ADDR_T_NONE;
668#endif
669}
670
Mugunthan V N69b41382015-12-23 20:39:36 +0530671fdt_addr_t dev_get_addr(struct udevice *dev)
672{
673 return dev_get_addr_index(dev, 0);
674}
675
Simon Glassc5785672015-03-25 12:21:57 -0600676bool device_has_children(struct udevice *dev)
677{
678 return !list_empty(&dev->child_head);
679}
680
681bool device_has_active_children(struct udevice *dev)
682{
683 struct udevice *child;
684
685 for (device_find_first_child(dev, &child);
686 child;
687 device_find_next_child(&child)) {
688 if (device_active(child))
689 return true;
690 }
691
692 return false;
693}
694
695bool device_is_last_sibling(struct udevice *dev)
696{
697 struct udevice *parent = dev->parent;
698
699 if (!parent)
700 return false;
701 return list_is_last(&dev->sibling_node, &parent->child_head);
702}
Simon Glassf5c67ea2015-07-30 13:40:39 -0600703
704int device_set_name(struct udevice *dev, const char *name)
705{
706 name = strdup(name);
707 if (!name)
708 return -ENOMEM;
709 dev->name = name;
710
711 return 0;
712}