blob: d5f5fc31b0359982db36e71a341779438e398b10 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Simon Glassdd6ab882014-02-26 15:59:18 -07002/*
3 * Device manager
4 *
5 * Copyright (c) 2013 Google, Inc
6 *
7 * (C) Copyright 2012
8 * Pavel Herrmann <morpheus.ibis@gmail.com>
Simon Glassdd6ab882014-02-26 15:59:18 -07009 */
10
11#include <common.h>
Vignesh R0dff3702016-07-06 09:58:55 +053012#include <asm/io.h>
Philipp Tomsich9cf03b02018-01-08 13:59:18 +010013#include <clk.h>
Simon Glassdb6f0202014-07-23 06:55:12 -060014#include <fdtdec.h>
Stefan Roeseadc09052015-09-02 07:41:12 +020015#include <fdt_support.h>
Simon Glassdd6ab882014-02-26 15:59:18 -070016#include <malloc.h>
17#include <dm/device.h>
18#include <dm/device-internal.h>
19#include <dm/lists.h>
Mario Six1f1c1ec2018-01-15 11:07:20 +010020#include <dm/of_access.h>
Masahiro Yamadaf8efa632015-08-27 12:44:29 +090021#include <dm/pinctrl.h>
Simon Glassdd6ab882014-02-26 15:59:18 -070022#include <dm/platdata.h>
Simon Glass322b2902017-05-18 20:09:05 -060023#include <dm/read.h>
Simon Glassdd6ab882014-02-26 15:59:18 -070024#include <dm/uclass.h>
25#include <dm/uclass-internal.h>
26#include <dm/util.h>
27#include <linux/err.h>
28#include <linux/list.h>
29
Simon Glassdb6f0202014-07-23 06:55:12 -060030DECLARE_GLOBAL_DATA_PTR;
31
Stephen Warren8c93df12016-05-11 15:26:24 -060032static int device_bind_common(struct udevice *parent, const struct driver *drv,
33 const char *name, void *platdata,
Simon Glassb0cf4b32017-05-17 17:18:11 -060034 ulong driver_data, ofnode node,
Simon Glassafbf9b82016-07-04 11:58:18 -060035 uint of_platdata_size, struct udevice **devp)
Simon Glassdd6ab882014-02-26 15:59:18 -070036{
Heiko Schocherb74fcb42014-05-22 12:43:05 +020037 struct udevice *dev;
Simon Glassdd6ab882014-02-26 15:59:18 -070038 struct uclass *uc;
Przemyslaw Marczakd850e672015-04-15 13:07:18 +020039 int size, ret = 0;
Simon Glassdd6ab882014-02-26 15:59:18 -070040
Masahiro Yamadae1cc1f02015-08-27 12:44:28 +090041 if (devp)
42 *devp = NULL;
Simon Glassdd6ab882014-02-26 15:59:18 -070043 if (!name)
44 return -EINVAL;
45
46 ret = uclass_get(drv->id, &uc);
Simon Glass43313de2015-08-30 16:55:16 -060047 if (ret) {
48 debug("Missing uclass for driver %s\n", drv->name);
Simon Glassdd6ab882014-02-26 15:59:18 -070049 return ret;
Simon Glass43313de2015-08-30 16:55:16 -060050 }
Simon Glassdd6ab882014-02-26 15:59:18 -070051
Heiko Schocherb74fcb42014-05-22 12:43:05 +020052 dev = calloc(1, sizeof(struct udevice));
Simon Glassdd6ab882014-02-26 15:59:18 -070053 if (!dev)
54 return -ENOMEM;
55
56 INIT_LIST_HEAD(&dev->sibling_node);
57 INIT_LIST_HEAD(&dev->child_head);
58 INIT_LIST_HEAD(&dev->uclass_node);
Masahiro Yamada029bfca2015-07-25 21:52:37 +090059#ifdef CONFIG_DEVRES
Masahiro Yamada8b15b162015-07-25 21:52:35 +090060 INIT_LIST_HEAD(&dev->devres_head);
Masahiro Yamada029bfca2015-07-25 21:52:37 +090061#endif
Simon Glassdd6ab882014-02-26 15:59:18 -070062 dev->platdata = platdata;
Stephen Warren8c93df12016-05-11 15:26:24 -060063 dev->driver_data = driver_data;
Simon Glassdd6ab882014-02-26 15:59:18 -070064 dev->name = name;
Simon Glassb0cf4b32017-05-17 17:18:11 -060065 dev->node = node;
Simon Glassdd6ab882014-02-26 15:59:18 -070066 dev->parent = parent;
67 dev->driver = drv;
68 dev->uclass = uc;
Simon Glassdb6f0202014-07-23 06:55:12 -060069
Simon Glassdb6f0202014-07-23 06:55:12 -060070 dev->seq = -1;
Simon Glass0ccb0972015-01-25 08:27:05 -070071 dev->req_seq = -1;
Nathan Rossi7fa6ff42016-01-08 03:00:45 +100072 if (CONFIG_IS_ENABLED(OF_CONTROL) && CONFIG_IS_ENABLED(DM_SEQ_ALIAS)) {
Simon Glass1bd3deb2015-02-27 22:06:30 -070073 /*
Stefan Roese9d4ce6b2016-03-16 09:58:01 +010074 * Some devices, such as a SPI bus, I2C bus and serial ports
75 * are numbered using aliases.
76 *
77 * This is just a 'requested' sequence, and will be
78 * resolved (and ->seq updated) when the device is probed.
79 */
Simon Glass1bd3deb2015-02-27 22:06:30 -070080 if (uc->uc_drv->flags & DM_UC_FLAG_SEQ_ALIAS) {
Simon Glassb0cf4b32017-05-17 17:18:11 -060081 if (uc->uc_drv->name && ofnode_valid(node)) {
Simon Glass322b2902017-05-18 20:09:05 -060082 dev_read_alias_seq(dev, &dev->req_seq);
Simon Glass1bd3deb2015-02-27 22:06:30 -070083 }
Simon Glass0ccb0972015-01-25 08:27:05 -070084 }
Simon Glassdb6f0202014-07-23 06:55:12 -060085 }
Simon Glass1bd3deb2015-02-27 22:06:30 -070086
Simon Glassafbf9b82016-07-04 11:58:18 -060087 if (drv->platdata_auto_alloc_size) {
88 bool alloc = !platdata;
89
90 if (CONFIG_IS_ENABLED(OF_PLATDATA)) {
91 if (of_platdata_size) {
92 dev->flags |= DM_FLAG_OF_PLATDATA;
93 if (of_platdata_size <
94 drv->platdata_auto_alloc_size)
95 alloc = true;
96 }
97 }
98 if (alloc) {
99 dev->flags |= DM_FLAG_ALLOC_PDATA;
100 dev->platdata = calloc(1,
101 drv->platdata_auto_alloc_size);
102 if (!dev->platdata) {
103 ret = -ENOMEM;
104 goto fail_alloc1;
105 }
106 if (CONFIG_IS_ENABLED(OF_PLATDATA) && platdata) {
107 memcpy(dev->platdata, platdata,
108 of_platdata_size);
109 }
Simon Glassba750492015-01-25 08:27:00 -0700110 }
111 }
Przemyslaw Marczakd850e672015-04-15 13:07:18 +0200112
113 size = uc->uc_drv->per_device_platdata_auto_alloc_size;
114 if (size) {
115 dev->flags |= DM_FLAG_ALLOC_UCLASS_PDATA;
116 dev->uclass_platdata = calloc(1, size);
117 if (!dev->uclass_platdata) {
118 ret = -ENOMEM;
119 goto fail_alloc2;
120 }
121 }
Simon Glass11b61732015-01-25 08:27:01 -0700122
Przemyslaw Marczakd850e672015-04-15 13:07:18 +0200123 if (parent) {
124 size = parent->driver->per_child_platdata_auto_alloc_size;
Simon Glass57f95402015-01-25 08:27:02 -0700125 if (!size) {
126 size = parent->uclass->uc_drv->
127 per_child_platdata_auto_alloc_size;
128 }
Simon Glass11b61732015-01-25 08:27:01 -0700129 if (size) {
130 dev->flags |= DM_FLAG_ALLOC_PARENT_PDATA;
131 dev->parent_platdata = calloc(1, size);
132 if (!dev->parent_platdata) {
133 ret = -ENOMEM;
Przemyslaw Marczakd850e672015-04-15 13:07:18 +0200134 goto fail_alloc3;
Simon Glass11b61732015-01-25 08:27:01 -0700135 }
136 }
137 }
Simon Glassdd6ab882014-02-26 15:59:18 -0700138
139 /* put dev into parent's successor list */
140 if (parent)
141 list_add_tail(&dev->sibling_node, &parent->child_head);
142
143 ret = uclass_bind_device(dev);
144 if (ret)
Simon Glass4ebe01b2015-01-25 08:26:59 -0700145 goto fail_uclass_bind;
Simon Glassdd6ab882014-02-26 15:59:18 -0700146
147 /* if we fail to bind we remove device from successors and free it */
148 if (drv->bind) {
149 ret = drv->bind(dev);
Simon Glass4ebe01b2015-01-25 08:26:59 -0700150 if (ret)
Simon Glassdd6ab882014-02-26 15:59:18 -0700151 goto fail_bind;
Simon Glassdd6ab882014-02-26 15:59:18 -0700152 }
Simon Glassa4a51a02015-01-25 08:27:03 -0700153 if (parent && parent->driver->child_post_bind) {
154 ret = parent->driver->child_post_bind(dev);
155 if (ret)
156 goto fail_child_post_bind;
157 }
Simon Glass286863d2016-01-05 09:30:59 -0700158 if (uc->uc_drv->post_bind) {
159 ret = uc->uc_drv->post_bind(dev);
160 if (ret)
161 goto fail_uclass_post_bind;
162 }
Simon Glassa4a51a02015-01-25 08:27:03 -0700163
Simon Glassdd6ab882014-02-26 15:59:18 -0700164 if (parent)
Masahiro Yamadaf70f39f2017-09-29 12:31:20 +0900165 pr_debug("Bound device %s to %s\n", dev->name, parent->name);
Masahiro Yamadae1cc1f02015-08-27 12:44:28 +0900166 if (devp)
167 *devp = dev;
Simon Glassdd6ab882014-02-26 15:59:18 -0700168
Masahiro Yamadabdbb5dd2015-07-25 21:52:34 +0900169 dev->flags |= DM_FLAG_BOUND;
170
Simon Glassdd6ab882014-02-26 15:59:18 -0700171 return 0;
172
Simon Glass286863d2016-01-05 09:30:59 -0700173fail_uclass_post_bind:
174 /* There is no child unbind() method, so no clean-up required */
Simon Glassa4a51a02015-01-25 08:27:03 -0700175fail_child_post_bind:
Masahiro Yamada04aa00d2015-08-12 07:31:52 +0900176 if (CONFIG_IS_ENABLED(DM_DEVICE_REMOVE)) {
Simon Glasse70a4f42015-02-27 22:06:33 -0700177 if (drv->unbind && drv->unbind(dev)) {
178 dm_warn("unbind() method failed on dev '%s' on error path\n",
179 dev->name);
180 }
Simon Glassa4a51a02015-01-25 08:27:03 -0700181 }
182
Simon Glassdd6ab882014-02-26 15:59:18 -0700183fail_bind:
Masahiro Yamada04aa00d2015-08-12 07:31:52 +0900184 if (CONFIG_IS_ENABLED(DM_DEVICE_REMOVE)) {
Simon Glasse70a4f42015-02-27 22:06:33 -0700185 if (uclass_unbind_device(dev)) {
186 dm_warn("Failed to unbind dev '%s' on error path\n",
187 dev->name);
188 }
Simon Glass4ebe01b2015-01-25 08:26:59 -0700189 }
190fail_uclass_bind:
Masahiro Yamada04aa00d2015-08-12 07:31:52 +0900191 if (CONFIG_IS_ENABLED(DM_DEVICE_REMOVE)) {
Simon Glasse70a4f42015-02-27 22:06:33 -0700192 list_del(&dev->sibling_node);
193 if (dev->flags & DM_FLAG_ALLOC_PARENT_PDATA) {
194 free(dev->parent_platdata);
195 dev->parent_platdata = NULL;
196 }
Simon Glass11b61732015-01-25 08:27:01 -0700197 }
Przemyslaw Marczakd850e672015-04-15 13:07:18 +0200198fail_alloc3:
199 if (dev->flags & DM_FLAG_ALLOC_UCLASS_PDATA) {
200 free(dev->uclass_platdata);
201 dev->uclass_platdata = NULL;
202 }
Simon Glass11b61732015-01-25 08:27:01 -0700203fail_alloc2:
Simon Glassba750492015-01-25 08:27:00 -0700204 if (dev->flags & DM_FLAG_ALLOC_PDATA) {
205 free(dev->platdata);
206 dev->platdata = NULL;
207 }
208fail_alloc1:
Masahiro Yamada8b15b162015-07-25 21:52:35 +0900209 devres_release_all(dev);
210
Simon Glassdd6ab882014-02-26 15:59:18 -0700211 free(dev);
Simon Glass4ebe01b2015-01-25 08:26:59 -0700212
Simon Glassdd6ab882014-02-26 15:59:18 -0700213 return ret;
214}
215
Stephen Warren8c93df12016-05-11 15:26:24 -0600216int device_bind_with_driver_data(struct udevice *parent,
217 const struct driver *drv, const char *name,
Simon Glass322b2902017-05-18 20:09:05 -0600218 ulong driver_data, ofnode node,
Stephen Warren8c93df12016-05-11 15:26:24 -0600219 struct udevice **devp)
220{
Simon Glass322b2902017-05-18 20:09:05 -0600221 return device_bind_common(parent, drv, name, NULL, driver_data, node,
222 0, devp);
Stephen Warren8c93df12016-05-11 15:26:24 -0600223}
224
225int device_bind(struct udevice *parent, const struct driver *drv,
226 const char *name, void *platdata, int of_offset,
227 struct udevice **devp)
228{
Simon Glassb0cf4b32017-05-17 17:18:11 -0600229 return device_bind_common(parent, drv, name, platdata, 0,
230 offset_to_ofnode(of_offset), 0, devp);
Stephen Warren8c93df12016-05-11 15:26:24 -0600231}
232
Simon Glass9eb151e2018-06-11 13:07:15 -0600233int device_bind_ofnode(struct udevice *parent, const struct driver *drv,
234 const char *name, void *platdata, ofnode node,
235 struct udevice **devp)
236{
237 return device_bind_common(parent, drv, name, platdata, 0, node, 0,
238 devp);
239}
240
Simon Glassfef72b72014-07-23 06:55:03 -0600241int device_bind_by_name(struct udevice *parent, bool pre_reloc_only,
242 const struct driver_info *info, struct udevice **devp)
Simon Glassdd6ab882014-02-26 15:59:18 -0700243{
244 struct driver *drv;
Simon Glassafbf9b82016-07-04 11:58:18 -0600245 uint platdata_size = 0;
Simon Glassdd6ab882014-02-26 15:59:18 -0700246
247 drv = lists_driver_lookup_name(info->name);
248 if (!drv)
249 return -ENOENT;
Simon Glassfef72b72014-07-23 06:55:03 -0600250 if (pre_reloc_only && !(drv->flags & DM_FLAG_PRE_RELOC))
251 return -EPERM;
Simon Glassdd6ab882014-02-26 15:59:18 -0700252
Simon Glassafbf9b82016-07-04 11:58:18 -0600253#if CONFIG_IS_ENABLED(OF_PLATDATA)
254 platdata_size = info->platdata_size;
255#endif
256 return device_bind_common(parent, drv, info->name,
Simon Glass322b2902017-05-18 20:09:05 -0600257 (void *)info->platdata, 0, ofnode_null(), platdata_size,
258 devp);
Simon Glassdd6ab882014-02-26 15:59:18 -0700259}
260
Simon Glass825d3f92015-03-25 12:21:53 -0600261static void *alloc_priv(int size, uint flags)
262{
263 void *priv;
264
265 if (flags & DM_FLAG_ALLOC_PRIV_DMA) {
Faiz Abbas03020a92017-09-19 16:53:50 +0530266 size = ROUND(size, ARCH_DMA_MINALIGN);
Simon Glass825d3f92015-03-25 12:21:53 -0600267 priv = memalign(ARCH_DMA_MINALIGN, size);
Simon Glass27bc9b12017-04-04 13:00:19 -0600268 if (priv) {
Simon Glass825d3f92015-03-25 12:21:53 -0600269 memset(priv, '\0', size);
Simon Glass27bc9b12017-04-04 13:00:19 -0600270
271 /*
272 * Ensure that the zero bytes are flushed to memory.
273 * This prevents problems if the driver uses this as
274 * both an input and an output buffer:
275 *
276 * 1. Zeroes written to buffer (here) and sit in the
277 * cache
278 * 2. Driver issues a read command to DMA
279 * 3. CPU runs out of cache space and evicts some cache
280 * data in the buffer, writing zeroes to RAM from
281 * the memset() above
282 * 4. DMA completes
283 * 5. Buffer now has some DMA data and some zeroes
284 * 6. Data being read is now incorrect
285 *
286 * To prevent this, ensure that the cache is clean
287 * within this range at the start. The driver can then
288 * use normal flush-after-write, invalidate-before-read
289 * procedures.
290 *
291 * TODO(sjg@chromium.org): Drop this microblaze
292 * exception.
293 */
294#ifndef CONFIG_MICROBLAZE
295 flush_dcache_range((ulong)priv, (ulong)priv + size);
296#endif
297 }
Simon Glass825d3f92015-03-25 12:21:53 -0600298 } else {
299 priv = calloc(1, size);
300 }
301
302 return priv;
303}
304
Simon Glass6142d752016-01-25 14:58:42 -0700305int device_probe(struct udevice *dev)
Simon Glassdd6ab882014-02-26 15:59:18 -0700306{
Simon Glassa626dff2015-03-25 12:21:54 -0600307 const struct driver *drv;
Simon Glassdd6ab882014-02-26 15:59:18 -0700308 int size = 0;
309 int ret;
Simon Glassdb6f0202014-07-23 06:55:12 -0600310 int seq;
Simon Glassdd6ab882014-02-26 15:59:18 -0700311
312 if (!dev)
313 return -EINVAL;
314
315 if (dev->flags & DM_FLAG_ACTIVATED)
316 return 0;
317
318 drv = dev->driver;
319 assert(drv);
320
Bin Meng82cdd782015-08-24 01:14:02 -0700321 /* Allocate private data if requested and not reentered */
322 if (drv->priv_auto_alloc_size && !dev->priv) {
Simon Glass825d3f92015-03-25 12:21:53 -0600323 dev->priv = alloc_priv(drv->priv_auto_alloc_size, drv->flags);
Simon Glassdd6ab882014-02-26 15:59:18 -0700324 if (!dev->priv) {
325 ret = -ENOMEM;
326 goto fail;
327 }
328 }
Bin Meng82cdd782015-08-24 01:14:02 -0700329 /* Allocate private data if requested and not reentered */
Simon Glassdd6ab882014-02-26 15:59:18 -0700330 size = dev->uclass->uc_drv->per_device_auto_alloc_size;
Bin Meng82cdd782015-08-24 01:14:02 -0700331 if (size && !dev->uclass_priv) {
Simon Glassdd6ab882014-02-26 15:59:18 -0700332 dev->uclass_priv = calloc(1, size);
333 if (!dev->uclass_priv) {
334 ret = -ENOMEM;
335 goto fail;
336 }
337 }
338
339 /* Ensure all parents are probed */
340 if (dev->parent) {
Simon Glass60d971b2014-07-23 06:55:20 -0600341 size = dev->parent->driver->per_child_auto_alloc_size;
Simon Glassc23b4282015-01-25 08:27:06 -0700342 if (!size) {
343 size = dev->parent->uclass->uc_drv->
344 per_child_auto_alloc_size;
345 }
Bin Meng82cdd782015-08-24 01:14:02 -0700346 if (size && !dev->parent_priv) {
Simon Glass825d3f92015-03-25 12:21:53 -0600347 dev->parent_priv = alloc_priv(size, drv->flags);
Simon Glass60d971b2014-07-23 06:55:20 -0600348 if (!dev->parent_priv) {
349 ret = -ENOMEM;
350 goto fail;
351 }
352 }
353
Simon Glassdd6ab882014-02-26 15:59:18 -0700354 ret = device_probe(dev->parent);
355 if (ret)
356 goto fail;
Bin Meng82cdd782015-08-24 01:14:02 -0700357
358 /*
359 * The device might have already been probed during
360 * the call to device_probe() on its parent device
361 * (e.g. PCI bridge devices). Test the flags again
362 * so that we don't mess up the device.
363 */
364 if (dev->flags & DM_FLAG_ACTIVATED)
365 return 0;
Simon Glassdd6ab882014-02-26 15:59:18 -0700366 }
Simon Glassdb6f0202014-07-23 06:55:12 -0600367
368 seq = uclass_resolve_seq(dev);
369 if (seq < 0) {
370 ret = seq;
371 goto fail;
372 }
373 dev->seq = seq;
Simon Glassdd6ab882014-02-26 15:59:18 -0700374
Simon Glass8b04e732015-03-25 12:21:56 -0600375 dev->flags |= DM_FLAG_ACTIVATED;
376
Simon Glassbb53bda2015-09-12 08:45:19 -0600377 /*
378 * Process pinctrl for everything except the root device, and
Simon Glass27ce9622016-01-21 19:43:25 -0700379 * continue regardless of the result of pinctrl. Don't process pinctrl
380 * settings for pinctrl devices since the device may not yet be
381 * probed.
Simon Glassbb53bda2015-09-12 08:45:19 -0600382 */
Simon Glass27ce9622016-01-21 19:43:25 -0700383 if (dev->parent && device_get_uclass_id(dev) != UCLASS_PINCTRL)
Simon Glassbb53bda2015-09-12 08:45:19 -0600384 pinctrl_select_state(dev, "default");
Masahiro Yamadaf8efa632015-08-27 12:44:29 +0900385
Simon Glass9c1f3822015-03-05 12:25:22 -0700386 ret = uclass_pre_probe_device(dev);
Simon Glass5104b982015-01-25 08:27:10 -0700387 if (ret)
388 goto fail;
389
Simon Glassd45560d2014-07-23 06:55:21 -0600390 if (dev->parent && dev->parent->driver->child_pre_probe) {
391 ret = dev->parent->driver->child_pre_probe(dev);
392 if (ret)
393 goto fail;
394 }
395
Simon Glass322b2902017-05-18 20:09:05 -0600396 if (drv->ofdata_to_platdata && dev_has_of_node(dev)) {
Simon Glassdd6ab882014-02-26 15:59:18 -0700397 ret = drv->ofdata_to_platdata(dev);
398 if (ret)
399 goto fail;
400 }
401
Philipp Tomsich9cf03b02018-01-08 13:59:18 +0100402 /* Process 'assigned-{clocks/clock-parents/clock-rates}' properties */
403 ret = clk_set_defaults(dev);
404 if (ret)
405 goto fail;
406
Simon Glassdd6ab882014-02-26 15:59:18 -0700407 if (drv->probe) {
408 ret = drv->probe(dev);
Simon Glassd02009d2015-03-05 12:25:21 -0700409 if (ret) {
410 dev->flags &= ~DM_FLAG_ACTIVATED;
Simon Glassdd6ab882014-02-26 15:59:18 -0700411 goto fail;
Simon Glassd02009d2015-03-05 12:25:21 -0700412 }
Simon Glassdd6ab882014-02-26 15:59:18 -0700413 }
414
Simon Glassdd6ab882014-02-26 15:59:18 -0700415 ret = uclass_post_probe_device(dev);
Simon Glass8b04e732015-03-25 12:21:56 -0600416 if (ret)
Simon Glassdd6ab882014-02-26 15:59:18 -0700417 goto fail_uclass;
Simon Glassdd6ab882014-02-26 15:59:18 -0700418
Peng Fan7cf58dd2016-03-12 13:17:38 +0800419 if (dev->parent && device_get_uclass_id(dev) == UCLASS_PINCTRL)
420 pinctrl_select_state(dev, "default");
421
Simon Glassdd6ab882014-02-26 15:59:18 -0700422 return 0;
423fail_uclass:
Stefan Roese80b5bc92017-03-20 12:51:48 +0100424 if (device_remove(dev, DM_REMOVE_NORMAL)) {
Simon Glassdd6ab882014-02-26 15:59:18 -0700425 dm_warn("%s: Device '%s' failed to remove on error path\n",
426 __func__, dev->name);
427 }
428fail:
Simon Glass8b04e732015-03-25 12:21:56 -0600429 dev->flags &= ~DM_FLAG_ACTIVATED;
430
Simon Glassdb6f0202014-07-23 06:55:12 -0600431 dev->seq = -1;
Simon Glassdd6ab882014-02-26 15:59:18 -0700432 device_free(dev);
433
434 return ret;
435}
436
Heiko Schocherb74fcb42014-05-22 12:43:05 +0200437void *dev_get_platdata(struct udevice *dev)
Simon Glassdd6ab882014-02-26 15:59:18 -0700438{
439 if (!dev) {
Simon Glassdf1e0722014-12-10 08:55:56 -0700440 dm_warn("%s: null device\n", __func__);
Simon Glassdd6ab882014-02-26 15:59:18 -0700441 return NULL;
442 }
443
444 return dev->platdata;
445}
446
Simon Glass11b61732015-01-25 08:27:01 -0700447void *dev_get_parent_platdata(struct udevice *dev)
448{
449 if (!dev) {
Simon Glasscdfc5052015-07-06 16:47:40 -0600450 dm_warn("%s: null device\n", __func__);
Simon Glass11b61732015-01-25 08:27:01 -0700451 return NULL;
452 }
453
454 return dev->parent_platdata;
455}
456
Przemyslaw Marczakd850e672015-04-15 13:07:18 +0200457void *dev_get_uclass_platdata(struct udevice *dev)
458{
459 if (!dev) {
Simon Glasscdfc5052015-07-06 16:47:40 -0600460 dm_warn("%s: null device\n", __func__);
Przemyslaw Marczakd850e672015-04-15 13:07:18 +0200461 return NULL;
462 }
463
464 return dev->uclass_platdata;
465}
466
Heiko Schocherb74fcb42014-05-22 12:43:05 +0200467void *dev_get_priv(struct udevice *dev)
Simon Glassdd6ab882014-02-26 15:59:18 -0700468{
469 if (!dev) {
Simon Glassdf1e0722014-12-10 08:55:56 -0700470 dm_warn("%s: null device\n", __func__);
Simon Glassdd6ab882014-02-26 15:59:18 -0700471 return NULL;
472 }
473
474 return dev->priv;
475}
Simon Glass48d4e292014-07-23 06:55:19 -0600476
Simon Glassde0977b2015-03-05 12:25:20 -0700477void *dev_get_uclass_priv(struct udevice *dev)
478{
479 if (!dev) {
480 dm_warn("%s: null device\n", __func__);
481 return NULL;
482 }
483
484 return dev->uclass_priv;
485}
486
Simon Glassde44acf2015-09-28 23:32:01 -0600487void *dev_get_parent_priv(struct udevice *dev)
Simon Glass60d971b2014-07-23 06:55:20 -0600488{
489 if (!dev) {
Simon Glassdf1e0722014-12-10 08:55:56 -0700490 dm_warn("%s: null device\n", __func__);
Simon Glass60d971b2014-07-23 06:55:20 -0600491 return NULL;
492 }
493
494 return dev->parent_priv;
495}
496
Simon Glass48d4e292014-07-23 06:55:19 -0600497static int device_get_device_tail(struct udevice *dev, int ret,
498 struct udevice **devp)
499{
500 if (ret)
501 return ret;
502
503 ret = device_probe(dev);
504 if (ret)
505 return ret;
506
507 *devp = dev;
508
509 return 0;
510}
511
512int device_get_child(struct udevice *parent, int index, struct udevice **devp)
513{
514 struct udevice *dev;
515
516 list_for_each_entry(dev, &parent->child_head, sibling_node) {
517 if (!index--)
518 return device_get_device_tail(dev, 0, devp);
519 }
520
521 return -ENODEV;
522}
523
524int device_find_child_by_seq(struct udevice *parent, int seq_or_req_seq,
525 bool find_req_seq, struct udevice **devp)
526{
527 struct udevice *dev;
528
529 *devp = NULL;
530 if (seq_or_req_seq == -1)
531 return -ENODEV;
532
533 list_for_each_entry(dev, &parent->child_head, sibling_node) {
534 if ((find_req_seq ? dev->req_seq : dev->seq) ==
535 seq_or_req_seq) {
536 *devp = dev;
537 return 0;
538 }
539 }
540
541 return -ENODEV;
542}
543
544int device_get_child_by_seq(struct udevice *parent, int seq,
545 struct udevice **devp)
546{
547 struct udevice *dev;
548 int ret;
549
550 *devp = NULL;
551 ret = device_find_child_by_seq(parent, seq, false, &dev);
552 if (ret == -ENODEV) {
553 /*
554 * We didn't find it in probed devices. See if there is one
555 * that will request this seq if probed.
556 */
557 ret = device_find_child_by_seq(parent, seq, true, &dev);
558 }
559 return device_get_device_tail(dev, ret, devp);
560}
561
562int device_find_child_by_of_offset(struct udevice *parent, int of_offset,
563 struct udevice **devp)
564{
565 struct udevice *dev;
566
567 *devp = NULL;
568
569 list_for_each_entry(dev, &parent->child_head, sibling_node) {
Simon Glassdd79d6e2017-01-17 16:52:55 -0700570 if (dev_of_offset(dev) == of_offset) {
Simon Glass48d4e292014-07-23 06:55:19 -0600571 *devp = dev;
572 return 0;
573 }
574 }
575
576 return -ENODEV;
577}
578
Simon Glass861bc9f2015-06-23 15:38:38 -0600579int device_get_child_by_of_offset(struct udevice *parent, int node,
Simon Glass48d4e292014-07-23 06:55:19 -0600580 struct udevice **devp)
581{
582 struct udevice *dev;
583 int ret;
584
585 *devp = NULL;
Simon Glass861bc9f2015-06-23 15:38:38 -0600586 ret = device_find_child_by_of_offset(parent, node, &dev);
Simon Glass48d4e292014-07-23 06:55:19 -0600587 return device_get_device_tail(dev, ret, devp);
588}
Simon Glass44da7352014-10-13 23:41:49 -0600589
Simon Glassae2efac2015-06-23 15:38:37 -0600590static struct udevice *_device_find_global_by_of_offset(struct udevice *parent,
591 int of_offset)
592{
593 struct udevice *dev, *found;
594
Simon Glassdd79d6e2017-01-17 16:52:55 -0700595 if (dev_of_offset(parent) == of_offset)
Simon Glassae2efac2015-06-23 15:38:37 -0600596 return parent;
597
598 list_for_each_entry(dev, &parent->child_head, sibling_node) {
599 found = _device_find_global_by_of_offset(dev, of_offset);
600 if (found)
601 return found;
602 }
603
604 return NULL;
605}
606
607int device_get_global_by_of_offset(int of_offset, struct udevice **devp)
608{
609 struct udevice *dev;
610
611 dev = _device_find_global_by_of_offset(gd->dm_root, of_offset);
612 return device_get_device_tail(dev, dev ? 0 : -ENOENT, devp);
613}
614
Simon Glass44da7352014-10-13 23:41:49 -0600615int device_find_first_child(struct udevice *parent, struct udevice **devp)
616{
617 if (list_empty(&parent->child_head)) {
618 *devp = NULL;
619 } else {
620 *devp = list_first_entry(&parent->child_head, struct udevice,
621 sibling_node);
622 }
623
624 return 0;
625}
626
627int device_find_next_child(struct udevice **devp)
628{
629 struct udevice *dev = *devp;
630 struct udevice *parent = dev->parent;
631
632 if (list_is_last(&dev->sibling_node, &parent->child_head)) {
633 *devp = NULL;
634 } else {
635 *devp = list_entry(dev->sibling_node.next, struct udevice,
636 sibling_node);
637 }
638
639 return 0;
640}
Simon Glass70c3a0e2014-11-11 10:46:18 -0700641
Simon Glass43f488a2014-11-11 10:46:19 -0700642struct udevice *dev_get_parent(struct udevice *child)
643{
644 return child->parent;
645}
646
Simon Glass46227bd2015-03-25 12:21:55 -0600647ulong dev_get_driver_data(struct udevice *dev)
Simon Glass70c3a0e2014-11-11 10:46:18 -0700648{
Simon Glass46227bd2015-03-25 12:21:55 -0600649 return dev->driver_data;
Simon Glass70c3a0e2014-11-11 10:46:18 -0700650}
Simon Glass98fd5d12015-01-25 08:27:04 -0700651
Przemyslaw Marczakd3ef0d72015-04-15 13:07:24 +0200652const void *dev_get_driver_ops(struct udevice *dev)
653{
654 if (!dev || !dev->driver->ops)
655 return NULL;
656
657 return dev->driver->ops;
658}
659
Simon Glass98fd5d12015-01-25 08:27:04 -0700660enum uclass_id device_get_uclass_id(struct udevice *dev)
661{
662 return dev->uclass->uc_drv->id;
663}
Peng Fan99b72352015-02-10 14:46:32 +0800664
Przemyslaw Marczak5ed2e422015-04-15 13:07:25 +0200665const char *dev_get_uclass_name(struct udevice *dev)
666{
667 if (!dev)
668 return NULL;
669
670 return dev->uclass->uc_drv->name;
671}
672
Simon Glass40f829a2015-03-25 12:21:57 -0600673bool device_has_children(struct udevice *dev)
674{
675 return !list_empty(&dev->child_head);
676}
677
678bool device_has_active_children(struct udevice *dev)
679{
680 struct udevice *child;
681
682 for (device_find_first_child(dev, &child);
683 child;
684 device_find_next_child(&child)) {
685 if (device_active(child))
686 return true;
687 }
688
689 return false;
690}
691
692bool device_is_last_sibling(struct udevice *dev)
693{
694 struct udevice *parent = dev->parent;
695
696 if (!parent)
697 return false;
698 return list_is_last(&dev->sibling_node, &parent->child_head);
699}
Simon Glasse3b23e22015-07-30 13:40:39 -0600700
Simon Glass7760ba22016-05-01 13:52:23 -0600701void device_set_name_alloced(struct udevice *dev)
702{
Simon Glass2d4c7dc2016-07-04 11:58:15 -0600703 dev->flags |= DM_FLAG_NAME_ALLOCED;
Simon Glass7760ba22016-05-01 13:52:23 -0600704}
705
Simon Glasse3b23e22015-07-30 13:40:39 -0600706int device_set_name(struct udevice *dev, const char *name)
707{
708 name = strdup(name);
709 if (!name)
710 return -ENOMEM;
711 dev->name = name;
Simon Glass7760ba22016-05-01 13:52:23 -0600712 device_set_name_alloced(dev);
Simon Glasse3b23e22015-07-30 13:40:39 -0600713
714 return 0;
715}
Mugunthan V N4666bd92016-04-28 15:36:02 +0530716
Simon Glass54cbcc82017-05-18 20:08:57 -0600717bool device_is_compatible(struct udevice *dev, const char *compat)
Mugunthan V N4666bd92016-04-28 15:36:02 +0530718{
Masahiro Yamada9349bcc2018-04-19 12:14:02 +0900719 return ofnode_device_is_compatible(dev_ofnode(dev), compat);
Mugunthan V N4666bd92016-04-28 15:36:02 +0530720}
721
722bool of_machine_is_compatible(const char *compat)
723{
724 const void *fdt = gd->fdt_blob;
725
726 return !fdt_node_check_compatible(fdt, 0, compat);
727}