blob: ce66c72e5ec9c194320ec783e340b8c00ceb52ff [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>
Peng Fan6fba32d2018-07-27 10:20:38 +080029#include <power-domain.h>
Simon Glassdd6ab882014-02-26 15:59:18 -070030
Simon Glassdb6f0202014-07-23 06:55:12 -060031DECLARE_GLOBAL_DATA_PTR;
32
Stephen Warren8c93df12016-05-11 15:26:24 -060033static int device_bind_common(struct udevice *parent, const struct driver *drv,
34 const char *name, void *platdata,
Simon Glassb0cf4b32017-05-17 17:18:11 -060035 ulong driver_data, ofnode node,
Simon Glassafbf9b82016-07-04 11:58:18 -060036 uint of_platdata_size, struct udevice **devp)
Simon Glassdd6ab882014-02-26 15:59:18 -070037{
Heiko Schocherb74fcb42014-05-22 12:43:05 +020038 struct udevice *dev;
Simon Glassdd6ab882014-02-26 15:59:18 -070039 struct uclass *uc;
Przemyslaw Marczakd850e672015-04-15 13:07:18 +020040 int size, ret = 0;
Simon Glassdd6ab882014-02-26 15:59:18 -070041
Masahiro Yamadae1cc1f02015-08-27 12:44:28 +090042 if (devp)
43 *devp = NULL;
Simon Glassdd6ab882014-02-26 15:59:18 -070044 if (!name)
45 return -EINVAL;
46
47 ret = uclass_get(drv->id, &uc);
Simon Glass43313de2015-08-30 16:55:16 -060048 if (ret) {
49 debug("Missing uclass for driver %s\n", drv->name);
Simon Glassdd6ab882014-02-26 15:59:18 -070050 return ret;
Simon Glass43313de2015-08-30 16:55:16 -060051 }
Simon Glassdd6ab882014-02-26 15:59:18 -070052
Heiko Schocherb74fcb42014-05-22 12:43:05 +020053 dev = calloc(1, sizeof(struct udevice));
Simon Glassdd6ab882014-02-26 15:59:18 -070054 if (!dev)
55 return -ENOMEM;
56
57 INIT_LIST_HEAD(&dev->sibling_node);
58 INIT_LIST_HEAD(&dev->child_head);
59 INIT_LIST_HEAD(&dev->uclass_node);
Masahiro Yamada029bfca2015-07-25 21:52:37 +090060#ifdef CONFIG_DEVRES
Masahiro Yamada8b15b162015-07-25 21:52:35 +090061 INIT_LIST_HEAD(&dev->devres_head);
Masahiro Yamada029bfca2015-07-25 21:52:37 +090062#endif
Simon Glassdd6ab882014-02-26 15:59:18 -070063 dev->platdata = platdata;
Stephen Warren8c93df12016-05-11 15:26:24 -060064 dev->driver_data = driver_data;
Simon Glassdd6ab882014-02-26 15:59:18 -070065 dev->name = name;
Simon Glassb0cf4b32017-05-17 17:18:11 -060066 dev->node = node;
Simon Glassdd6ab882014-02-26 15:59:18 -070067 dev->parent = parent;
68 dev->driver = drv;
69 dev->uclass = uc;
Simon Glassdb6f0202014-07-23 06:55:12 -060070
Simon Glassdb6f0202014-07-23 06:55:12 -060071 dev->seq = -1;
Simon Glass0ccb0972015-01-25 08:27:05 -070072 dev->req_seq = -1;
Jean-Jacques Hiblota5da3002018-12-07 14:50:39 +010073 if (CONFIG_IS_ENABLED(DM_SEQ_ALIAS) &&
74 (uc->uc_drv->flags & DM_UC_FLAG_SEQ_ALIAS)) {
Simon Glass1bd3deb2015-02-27 22:06:30 -070075 /*
Stefan Roese9d4ce6b2016-03-16 09:58:01 +010076 * Some devices, such as a SPI bus, I2C bus and serial ports
77 * are numbered using aliases.
78 *
79 * This is just a 'requested' sequence, and will be
80 * resolved (and ->seq updated) when the device is probed.
81 */
Jean-Jacques Hiblota5da3002018-12-07 14:50:39 +010082 if (CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)) {
83 if (uc->uc_drv->name && ofnode_valid(node))
Simon Glass322b2902017-05-18 20:09:05 -060084 dev_read_alias_seq(dev, &dev->req_seq);
Jean-Jacques Hiblota5da3002018-12-07 14:50:39 +010085 } else {
86 dev->req_seq = uclass_find_next_free_req_seq(drv->id);
Simon Glass0ccb0972015-01-25 08:27:05 -070087 }
Simon Glassdb6f0202014-07-23 06:55:12 -060088 }
Simon Glass1bd3deb2015-02-27 22:06:30 -070089
Simon Glassafbf9b82016-07-04 11:58:18 -060090 if (drv->platdata_auto_alloc_size) {
91 bool alloc = !platdata;
92
93 if (CONFIG_IS_ENABLED(OF_PLATDATA)) {
94 if (of_platdata_size) {
95 dev->flags |= DM_FLAG_OF_PLATDATA;
96 if (of_platdata_size <
97 drv->platdata_auto_alloc_size)
98 alloc = true;
99 }
100 }
101 if (alloc) {
102 dev->flags |= DM_FLAG_ALLOC_PDATA;
103 dev->platdata = calloc(1,
104 drv->platdata_auto_alloc_size);
105 if (!dev->platdata) {
106 ret = -ENOMEM;
107 goto fail_alloc1;
108 }
109 if (CONFIG_IS_ENABLED(OF_PLATDATA) && platdata) {
110 memcpy(dev->platdata, platdata,
111 of_platdata_size);
112 }
Simon Glassba750492015-01-25 08:27:00 -0700113 }
114 }
Przemyslaw Marczakd850e672015-04-15 13:07:18 +0200115
116 size = uc->uc_drv->per_device_platdata_auto_alloc_size;
117 if (size) {
118 dev->flags |= DM_FLAG_ALLOC_UCLASS_PDATA;
119 dev->uclass_platdata = calloc(1, size);
120 if (!dev->uclass_platdata) {
121 ret = -ENOMEM;
122 goto fail_alloc2;
123 }
124 }
Simon Glass11b61732015-01-25 08:27:01 -0700125
Przemyslaw Marczakd850e672015-04-15 13:07:18 +0200126 if (parent) {
127 size = parent->driver->per_child_platdata_auto_alloc_size;
Simon Glass57f95402015-01-25 08:27:02 -0700128 if (!size) {
129 size = parent->uclass->uc_drv->
130 per_child_platdata_auto_alloc_size;
131 }
Simon Glass11b61732015-01-25 08:27:01 -0700132 if (size) {
133 dev->flags |= DM_FLAG_ALLOC_PARENT_PDATA;
134 dev->parent_platdata = calloc(1, size);
135 if (!dev->parent_platdata) {
136 ret = -ENOMEM;
Przemyslaw Marczakd850e672015-04-15 13:07:18 +0200137 goto fail_alloc3;
Simon Glass11b61732015-01-25 08:27:01 -0700138 }
139 }
140 }
Simon Glassdd6ab882014-02-26 15:59:18 -0700141
142 /* put dev into parent's successor list */
143 if (parent)
144 list_add_tail(&dev->sibling_node, &parent->child_head);
145
146 ret = uclass_bind_device(dev);
147 if (ret)
Simon Glass4ebe01b2015-01-25 08:26:59 -0700148 goto fail_uclass_bind;
Simon Glassdd6ab882014-02-26 15:59:18 -0700149
150 /* if we fail to bind we remove device from successors and free it */
151 if (drv->bind) {
152 ret = drv->bind(dev);
Simon Glass4ebe01b2015-01-25 08:26:59 -0700153 if (ret)
Simon Glassdd6ab882014-02-26 15:59:18 -0700154 goto fail_bind;
Simon Glassdd6ab882014-02-26 15:59:18 -0700155 }
Simon Glassa4a51a02015-01-25 08:27:03 -0700156 if (parent && parent->driver->child_post_bind) {
157 ret = parent->driver->child_post_bind(dev);
158 if (ret)
159 goto fail_child_post_bind;
160 }
Simon Glass286863d2016-01-05 09:30:59 -0700161 if (uc->uc_drv->post_bind) {
162 ret = uc->uc_drv->post_bind(dev);
163 if (ret)
164 goto fail_uclass_post_bind;
165 }
Simon Glassa4a51a02015-01-25 08:27:03 -0700166
Simon Glassdd6ab882014-02-26 15:59:18 -0700167 if (parent)
Masahiro Yamadaf70f39f2017-09-29 12:31:20 +0900168 pr_debug("Bound device %s to %s\n", dev->name, parent->name);
Masahiro Yamadae1cc1f02015-08-27 12:44:28 +0900169 if (devp)
170 *devp = dev;
Simon Glassdd6ab882014-02-26 15:59:18 -0700171
Masahiro Yamadabdbb5dd2015-07-25 21:52:34 +0900172 dev->flags |= DM_FLAG_BOUND;
173
Simon Glassdd6ab882014-02-26 15:59:18 -0700174 return 0;
175
Simon Glass286863d2016-01-05 09:30:59 -0700176fail_uclass_post_bind:
177 /* There is no child unbind() method, so no clean-up required */
Simon Glassa4a51a02015-01-25 08:27:03 -0700178fail_child_post_bind:
Masahiro Yamada04aa00d2015-08-12 07:31:52 +0900179 if (CONFIG_IS_ENABLED(DM_DEVICE_REMOVE)) {
Simon Glasse70a4f42015-02-27 22:06:33 -0700180 if (drv->unbind && drv->unbind(dev)) {
181 dm_warn("unbind() method failed on dev '%s' on error path\n",
182 dev->name);
183 }
Simon Glassa4a51a02015-01-25 08:27:03 -0700184 }
185
Simon Glassdd6ab882014-02-26 15:59:18 -0700186fail_bind:
Masahiro Yamada04aa00d2015-08-12 07:31:52 +0900187 if (CONFIG_IS_ENABLED(DM_DEVICE_REMOVE)) {
Simon Glasse70a4f42015-02-27 22:06:33 -0700188 if (uclass_unbind_device(dev)) {
189 dm_warn("Failed to unbind dev '%s' on error path\n",
190 dev->name);
191 }
Simon Glass4ebe01b2015-01-25 08:26:59 -0700192 }
193fail_uclass_bind:
Masahiro Yamada04aa00d2015-08-12 07:31:52 +0900194 if (CONFIG_IS_ENABLED(DM_DEVICE_REMOVE)) {
Simon Glasse70a4f42015-02-27 22:06:33 -0700195 list_del(&dev->sibling_node);
196 if (dev->flags & DM_FLAG_ALLOC_PARENT_PDATA) {
197 free(dev->parent_platdata);
198 dev->parent_platdata = NULL;
199 }
Simon Glass11b61732015-01-25 08:27:01 -0700200 }
Przemyslaw Marczakd850e672015-04-15 13:07:18 +0200201fail_alloc3:
202 if (dev->flags & DM_FLAG_ALLOC_UCLASS_PDATA) {
203 free(dev->uclass_platdata);
204 dev->uclass_platdata = NULL;
205 }
Simon Glass11b61732015-01-25 08:27:01 -0700206fail_alloc2:
Simon Glassba750492015-01-25 08:27:00 -0700207 if (dev->flags & DM_FLAG_ALLOC_PDATA) {
208 free(dev->platdata);
209 dev->platdata = NULL;
210 }
211fail_alloc1:
Masahiro Yamada8b15b162015-07-25 21:52:35 +0900212 devres_release_all(dev);
213
Simon Glassdd6ab882014-02-26 15:59:18 -0700214 free(dev);
Simon Glass4ebe01b2015-01-25 08:26:59 -0700215
Simon Glassdd6ab882014-02-26 15:59:18 -0700216 return ret;
217}
218
Stephen Warren8c93df12016-05-11 15:26:24 -0600219int device_bind_with_driver_data(struct udevice *parent,
220 const struct driver *drv, const char *name,
Simon Glass322b2902017-05-18 20:09:05 -0600221 ulong driver_data, ofnode node,
Stephen Warren8c93df12016-05-11 15:26:24 -0600222 struct udevice **devp)
223{
Simon Glass322b2902017-05-18 20:09:05 -0600224 return device_bind_common(parent, drv, name, NULL, driver_data, node,
225 0, devp);
Stephen Warren8c93df12016-05-11 15:26:24 -0600226}
227
228int device_bind(struct udevice *parent, const struct driver *drv,
229 const char *name, void *platdata, int of_offset,
230 struct udevice **devp)
231{
Simon Glassb0cf4b32017-05-17 17:18:11 -0600232 return device_bind_common(parent, drv, name, platdata, 0,
233 offset_to_ofnode(of_offset), 0, devp);
Stephen Warren8c93df12016-05-11 15:26:24 -0600234}
235
Simon Glass9eb151e2018-06-11 13:07:15 -0600236int device_bind_ofnode(struct udevice *parent, const struct driver *drv,
237 const char *name, void *platdata, ofnode node,
238 struct udevice **devp)
239{
240 return device_bind_common(parent, drv, name, platdata, 0, node, 0,
241 devp);
242}
243
Simon Glassfef72b72014-07-23 06:55:03 -0600244int device_bind_by_name(struct udevice *parent, bool pre_reloc_only,
245 const struct driver_info *info, struct udevice **devp)
Simon Glassdd6ab882014-02-26 15:59:18 -0700246{
247 struct driver *drv;
Simon Glassafbf9b82016-07-04 11:58:18 -0600248 uint platdata_size = 0;
Simon Glassdd6ab882014-02-26 15:59:18 -0700249
250 drv = lists_driver_lookup_name(info->name);
251 if (!drv)
252 return -ENOENT;
Simon Glassfef72b72014-07-23 06:55:03 -0600253 if (pre_reloc_only && !(drv->flags & DM_FLAG_PRE_RELOC))
254 return -EPERM;
Simon Glassdd6ab882014-02-26 15:59:18 -0700255
Simon Glassafbf9b82016-07-04 11:58:18 -0600256#if CONFIG_IS_ENABLED(OF_PLATDATA)
257 platdata_size = info->platdata_size;
258#endif
259 return device_bind_common(parent, drv, info->name,
Simon Glass322b2902017-05-18 20:09:05 -0600260 (void *)info->platdata, 0, ofnode_null(), platdata_size,
261 devp);
Simon Glassdd6ab882014-02-26 15:59:18 -0700262}
263
Simon Glass825d3f92015-03-25 12:21:53 -0600264static void *alloc_priv(int size, uint flags)
265{
266 void *priv;
267
268 if (flags & DM_FLAG_ALLOC_PRIV_DMA) {
Faiz Abbas03020a92017-09-19 16:53:50 +0530269 size = ROUND(size, ARCH_DMA_MINALIGN);
Simon Glass825d3f92015-03-25 12:21:53 -0600270 priv = memalign(ARCH_DMA_MINALIGN, size);
Simon Glass27bc9b12017-04-04 13:00:19 -0600271 if (priv) {
Simon Glass825d3f92015-03-25 12:21:53 -0600272 memset(priv, '\0', size);
Simon Glass27bc9b12017-04-04 13:00:19 -0600273
274 /*
275 * Ensure that the zero bytes are flushed to memory.
276 * This prevents problems if the driver uses this as
277 * both an input and an output buffer:
278 *
279 * 1. Zeroes written to buffer (here) and sit in the
280 * cache
281 * 2. Driver issues a read command to DMA
282 * 3. CPU runs out of cache space and evicts some cache
283 * data in the buffer, writing zeroes to RAM from
284 * the memset() above
285 * 4. DMA completes
286 * 5. Buffer now has some DMA data and some zeroes
287 * 6. Data being read is now incorrect
288 *
289 * To prevent this, ensure that the cache is clean
290 * within this range at the start. The driver can then
291 * use normal flush-after-write, invalidate-before-read
292 * procedures.
293 *
294 * TODO(sjg@chromium.org): Drop this microblaze
295 * exception.
296 */
297#ifndef CONFIG_MICROBLAZE
298 flush_dcache_range((ulong)priv, (ulong)priv + size);
299#endif
300 }
Simon Glass825d3f92015-03-25 12:21:53 -0600301 } else {
302 priv = calloc(1, size);
303 }
304
305 return priv;
306}
307
Simon Glass6142d752016-01-25 14:58:42 -0700308int device_probe(struct udevice *dev)
Simon Glassdd6ab882014-02-26 15:59:18 -0700309{
Peng Fan6fba32d2018-07-27 10:20:38 +0800310 struct power_domain pd;
Simon Glassa626dff2015-03-25 12:21:54 -0600311 const struct driver *drv;
Simon Glassdd6ab882014-02-26 15:59:18 -0700312 int size = 0;
313 int ret;
Simon Glassdb6f0202014-07-23 06:55:12 -0600314 int seq;
Simon Glassdd6ab882014-02-26 15:59:18 -0700315
316 if (!dev)
317 return -EINVAL;
318
319 if (dev->flags & DM_FLAG_ACTIVATED)
320 return 0;
321
322 drv = dev->driver;
323 assert(drv);
324
Bin Meng82cdd782015-08-24 01:14:02 -0700325 /* Allocate private data if requested and not reentered */
326 if (drv->priv_auto_alloc_size && !dev->priv) {
Simon Glass825d3f92015-03-25 12:21:53 -0600327 dev->priv = alloc_priv(drv->priv_auto_alloc_size, drv->flags);
Simon Glassdd6ab882014-02-26 15:59:18 -0700328 if (!dev->priv) {
329 ret = -ENOMEM;
330 goto fail;
331 }
332 }
Bin Meng82cdd782015-08-24 01:14:02 -0700333 /* Allocate private data if requested and not reentered */
Simon Glassdd6ab882014-02-26 15:59:18 -0700334 size = dev->uclass->uc_drv->per_device_auto_alloc_size;
Bin Meng82cdd782015-08-24 01:14:02 -0700335 if (size && !dev->uclass_priv) {
Simon Glass7ebd13d2018-10-01 12:22:05 -0600336 dev->uclass_priv = alloc_priv(size,
337 dev->uclass->uc_drv->flags);
Simon Glassdd6ab882014-02-26 15:59:18 -0700338 if (!dev->uclass_priv) {
339 ret = -ENOMEM;
340 goto fail;
341 }
342 }
343
344 /* Ensure all parents are probed */
345 if (dev->parent) {
Simon Glass60d971b2014-07-23 06:55:20 -0600346 size = dev->parent->driver->per_child_auto_alloc_size;
Simon Glassc23b4282015-01-25 08:27:06 -0700347 if (!size) {
348 size = dev->parent->uclass->uc_drv->
349 per_child_auto_alloc_size;
350 }
Bin Meng82cdd782015-08-24 01:14:02 -0700351 if (size && !dev->parent_priv) {
Simon Glass825d3f92015-03-25 12:21:53 -0600352 dev->parent_priv = alloc_priv(size, drv->flags);
Simon Glass60d971b2014-07-23 06:55:20 -0600353 if (!dev->parent_priv) {
354 ret = -ENOMEM;
355 goto fail;
356 }
357 }
358
Simon Glassdd6ab882014-02-26 15:59:18 -0700359 ret = device_probe(dev->parent);
360 if (ret)
361 goto fail;
Bin Meng82cdd782015-08-24 01:14:02 -0700362
363 /*
364 * The device might have already been probed during
365 * the call to device_probe() on its parent device
366 * (e.g. PCI bridge devices). Test the flags again
367 * so that we don't mess up the device.
368 */
369 if (dev->flags & DM_FLAG_ACTIVATED)
370 return 0;
Simon Glassdd6ab882014-02-26 15:59:18 -0700371 }
Simon Glassdb6f0202014-07-23 06:55:12 -0600372
373 seq = uclass_resolve_seq(dev);
374 if (seq < 0) {
375 ret = seq;
376 goto fail;
377 }
378 dev->seq = seq;
Simon Glassdd6ab882014-02-26 15:59:18 -0700379
Simon Glass8b04e732015-03-25 12:21:56 -0600380 dev->flags |= DM_FLAG_ACTIVATED;
381
Simon Glassbb53bda2015-09-12 08:45:19 -0600382 /*
383 * Process pinctrl for everything except the root device, and
Simon Glass27ce9622016-01-21 19:43:25 -0700384 * continue regardless of the result of pinctrl. Don't process pinctrl
385 * settings for pinctrl devices since the device may not yet be
386 * probed.
Simon Glassbb53bda2015-09-12 08:45:19 -0600387 */
Simon Glass27ce9622016-01-21 19:43:25 -0700388 if (dev->parent && device_get_uclass_id(dev) != UCLASS_PINCTRL)
Simon Glassbb53bda2015-09-12 08:45:19 -0600389 pinctrl_select_state(dev, "default");
Masahiro Yamadaf8efa632015-08-27 12:44:29 +0900390
Anatolij Gustschin63a34672019-07-14 21:11:01 +0200391 if (CONFIG_IS_ENABLED(POWER_DOMAIN) && dev->parent &&
392 device_get_uclass_id(dev) != UCLASS_POWER_DOMAIN) {
Peng Fan6fba32d2018-07-27 10:20:38 +0800393 if (!power_domain_get(dev, &pd))
394 power_domain_on(&pd);
395 }
396
Simon Glass9c1f3822015-03-05 12:25:22 -0700397 ret = uclass_pre_probe_device(dev);
Simon Glass5104b982015-01-25 08:27:10 -0700398 if (ret)
399 goto fail;
400
Simon Glassd45560d2014-07-23 06:55:21 -0600401 if (dev->parent && dev->parent->driver->child_pre_probe) {
402 ret = dev->parent->driver->child_pre_probe(dev);
403 if (ret)
404 goto fail;
405 }
406
Simon Glassa91e48b2019-09-25 08:55:53 -0600407 if (drv->ofdata_to_platdata &&
408 (CONFIG_IS_ENABLED(OF_PLATDATA) || dev_has_of_node(dev))) {
Simon Glassdd6ab882014-02-26 15:59:18 -0700409 ret = drv->ofdata_to_platdata(dev);
410 if (ret)
411 goto fail;
412 }
413
Bin Meng513cf402019-07-05 09:23:16 -0700414 /* Only handle devices that have a valid ofnode */
415 if (dev_of_valid(dev)) {
416 /*
417 * Process 'assigned-{clocks/clock-parents/clock-rates}'
418 * properties
419 */
420 ret = clk_set_defaults(dev);
421 if (ret)
422 goto fail;
423 }
Philipp Tomsich9cf03b02018-01-08 13:59:18 +0100424
Simon Glassdd6ab882014-02-26 15:59:18 -0700425 if (drv->probe) {
426 ret = drv->probe(dev);
Simon Glassd02009d2015-03-05 12:25:21 -0700427 if (ret) {
428 dev->flags &= ~DM_FLAG_ACTIVATED;
Simon Glassdd6ab882014-02-26 15:59:18 -0700429 goto fail;
Simon Glassd02009d2015-03-05 12:25:21 -0700430 }
Simon Glassdd6ab882014-02-26 15:59:18 -0700431 }
432
Simon Glassdd6ab882014-02-26 15:59:18 -0700433 ret = uclass_post_probe_device(dev);
Simon Glass8b04e732015-03-25 12:21:56 -0600434 if (ret)
Simon Glassdd6ab882014-02-26 15:59:18 -0700435 goto fail_uclass;
Simon Glassdd6ab882014-02-26 15:59:18 -0700436
Peng Fan7cf58dd2016-03-12 13:17:38 +0800437 if (dev->parent && device_get_uclass_id(dev) == UCLASS_PINCTRL)
438 pinctrl_select_state(dev, "default");
439
Simon Glassdd6ab882014-02-26 15:59:18 -0700440 return 0;
441fail_uclass:
Stefan Roese80b5bc92017-03-20 12:51:48 +0100442 if (device_remove(dev, DM_REMOVE_NORMAL)) {
Simon Glassdd6ab882014-02-26 15:59:18 -0700443 dm_warn("%s: Device '%s' failed to remove on error path\n",
444 __func__, dev->name);
445 }
446fail:
Simon Glass8b04e732015-03-25 12:21:56 -0600447 dev->flags &= ~DM_FLAG_ACTIVATED;
448
Simon Glassdb6f0202014-07-23 06:55:12 -0600449 dev->seq = -1;
Simon Glassdd6ab882014-02-26 15:59:18 -0700450 device_free(dev);
451
452 return ret;
453}
454
Simon Glass2e5e5ec2018-10-01 12:22:06 -0600455void *dev_get_platdata(const struct udevice *dev)
Simon Glassdd6ab882014-02-26 15:59:18 -0700456{
457 if (!dev) {
Simon Glassdf1e0722014-12-10 08:55:56 -0700458 dm_warn("%s: null device\n", __func__);
Simon Glassdd6ab882014-02-26 15:59:18 -0700459 return NULL;
460 }
461
462 return dev->platdata;
463}
464
Simon Glass2e5e5ec2018-10-01 12:22:06 -0600465void *dev_get_parent_platdata(const struct udevice *dev)
Simon Glass11b61732015-01-25 08:27:01 -0700466{
467 if (!dev) {
Simon Glasscdfc5052015-07-06 16:47:40 -0600468 dm_warn("%s: null device\n", __func__);
Simon Glass11b61732015-01-25 08:27:01 -0700469 return NULL;
470 }
471
472 return dev->parent_platdata;
473}
474
Simon Glass2e5e5ec2018-10-01 12:22:06 -0600475void *dev_get_uclass_platdata(const struct udevice *dev)
Przemyslaw Marczakd850e672015-04-15 13:07:18 +0200476{
477 if (!dev) {
Simon Glasscdfc5052015-07-06 16:47:40 -0600478 dm_warn("%s: null device\n", __func__);
Przemyslaw Marczakd850e672015-04-15 13:07:18 +0200479 return NULL;
480 }
481
482 return dev->uclass_platdata;
483}
484
Simon Glass2e5e5ec2018-10-01 12:22:06 -0600485void *dev_get_priv(const struct udevice *dev)
Simon Glassdd6ab882014-02-26 15:59:18 -0700486{
487 if (!dev) {
Simon Glassdf1e0722014-12-10 08:55:56 -0700488 dm_warn("%s: null device\n", __func__);
Simon Glassdd6ab882014-02-26 15:59:18 -0700489 return NULL;
490 }
491
492 return dev->priv;
493}
Simon Glass48d4e292014-07-23 06:55:19 -0600494
Simon Glass2e5e5ec2018-10-01 12:22:06 -0600495void *dev_get_uclass_priv(const struct udevice *dev)
Simon Glassde0977b2015-03-05 12:25:20 -0700496{
497 if (!dev) {
498 dm_warn("%s: null device\n", __func__);
499 return NULL;
500 }
501
502 return dev->uclass_priv;
503}
504
Simon Glass2e5e5ec2018-10-01 12:22:06 -0600505void *dev_get_parent_priv(const struct udevice *dev)
Simon Glass60d971b2014-07-23 06:55:20 -0600506{
507 if (!dev) {
Simon Glassdf1e0722014-12-10 08:55:56 -0700508 dm_warn("%s: null device\n", __func__);
Simon Glass60d971b2014-07-23 06:55:20 -0600509 return NULL;
510 }
511
512 return dev->parent_priv;
513}
514
Simon Glass48d4e292014-07-23 06:55:19 -0600515static int device_get_device_tail(struct udevice *dev, int ret,
516 struct udevice **devp)
517{
518 if (ret)
519 return ret;
520
521 ret = device_probe(dev);
522 if (ret)
523 return ret;
524
525 *devp = dev;
526
527 return 0;
528}
529
Marek Vasut7f18c342019-08-31 18:03:28 +0200530#if CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)
Mario Six3bedfbe2018-06-26 08:46:50 +0200531/**
532 * device_find_by_ofnode() - Return device associated with given ofnode
533 *
534 * The returned device is *not* activated.
535 *
536 * @node: The ofnode for which a associated device should be looked up
537 * @devp: Pointer to structure to hold the found device
538 * Return: 0 if OK, -ve on error
539 */
540static int device_find_by_ofnode(ofnode node, struct udevice **devp)
541{
542 struct uclass *uc;
543 struct udevice *dev;
544 int ret;
545
546 list_for_each_entry(uc, &gd->uclass_root, sibling_node) {
547 ret = uclass_find_device_by_ofnode(uc->uc_drv->id, node,
548 &dev);
549 if (!ret || dev) {
550 *devp = dev;
551 return 0;
552 }
553 }
554
555 return -ENODEV;
556}
Marek Vasut7f18c342019-08-31 18:03:28 +0200557#endif
Mario Six3bedfbe2018-06-26 08:46:50 +0200558
Simon Glass48d4e292014-07-23 06:55:19 -0600559int device_get_child(struct udevice *parent, int index, struct udevice **devp)
560{
561 struct udevice *dev;
562
563 list_for_each_entry(dev, &parent->child_head, sibling_node) {
564 if (!index--)
565 return device_get_device_tail(dev, 0, devp);
566 }
567
568 return -ENODEV;
569}
570
Lokesh Vutlafa45a082019-09-04 16:01:26 +0530571int device_get_child_count(struct udevice *parent)
572{
573 struct udevice *dev;
574 int count = 0;
575
576 list_for_each_entry(dev, &parent->child_head, sibling_node)
577 count++;
578
579 return count;
580}
581
Simon Glass48d4e292014-07-23 06:55:19 -0600582int device_find_child_by_seq(struct udevice *parent, int seq_or_req_seq,
583 bool find_req_seq, struct udevice **devp)
584{
585 struct udevice *dev;
586
587 *devp = NULL;
588 if (seq_or_req_seq == -1)
589 return -ENODEV;
590
591 list_for_each_entry(dev, &parent->child_head, sibling_node) {
592 if ((find_req_seq ? dev->req_seq : dev->seq) ==
593 seq_or_req_seq) {
594 *devp = dev;
595 return 0;
596 }
597 }
598
599 return -ENODEV;
600}
601
602int device_get_child_by_seq(struct udevice *parent, int seq,
603 struct udevice **devp)
604{
605 struct udevice *dev;
606 int ret;
607
608 *devp = NULL;
609 ret = device_find_child_by_seq(parent, seq, false, &dev);
610 if (ret == -ENODEV) {
611 /*
612 * We didn't find it in probed devices. See if there is one
613 * that will request this seq if probed.
614 */
615 ret = device_find_child_by_seq(parent, seq, true, &dev);
616 }
617 return device_get_device_tail(dev, ret, devp);
618}
619
620int device_find_child_by_of_offset(struct udevice *parent, int of_offset,
621 struct udevice **devp)
622{
623 struct udevice *dev;
624
625 *devp = NULL;
626
627 list_for_each_entry(dev, &parent->child_head, sibling_node) {
Simon Glassdd79d6e2017-01-17 16:52:55 -0700628 if (dev_of_offset(dev) == of_offset) {
Simon Glass48d4e292014-07-23 06:55:19 -0600629 *devp = dev;
630 return 0;
631 }
632 }
633
634 return -ENODEV;
635}
636
Simon Glass861bc9f2015-06-23 15:38:38 -0600637int device_get_child_by_of_offset(struct udevice *parent, int node,
Simon Glass48d4e292014-07-23 06:55:19 -0600638 struct udevice **devp)
639{
640 struct udevice *dev;
641 int ret;
642
643 *devp = NULL;
Simon Glass861bc9f2015-06-23 15:38:38 -0600644 ret = device_find_child_by_of_offset(parent, node, &dev);
Simon Glass48d4e292014-07-23 06:55:19 -0600645 return device_get_device_tail(dev, ret, devp);
646}
Simon Glass44da7352014-10-13 23:41:49 -0600647
Jean-Jacques Hiblota7b0d6a2018-08-09 16:17:44 +0200648static struct udevice *_device_find_global_by_ofnode(struct udevice *parent,
649 ofnode ofnode)
Simon Glassae2efac2015-06-23 15:38:37 -0600650{
651 struct udevice *dev, *found;
652
Jean-Jacques Hiblota7b0d6a2018-08-09 16:17:44 +0200653 if (ofnode_equal(dev_ofnode(parent), ofnode))
Simon Glassae2efac2015-06-23 15:38:37 -0600654 return parent;
655
656 list_for_each_entry(dev, &parent->child_head, sibling_node) {
Jean-Jacques Hiblota7b0d6a2018-08-09 16:17:44 +0200657 found = _device_find_global_by_ofnode(dev, ofnode);
Simon Glassae2efac2015-06-23 15:38:37 -0600658 if (found)
659 return found;
660 }
661
662 return NULL;
663}
664
Jean-Jacques Hiblota7b0d6a2018-08-09 16:17:44 +0200665int device_find_global_by_ofnode(ofnode ofnode, struct udevice **devp)
666{
667 *devp = _device_find_global_by_ofnode(gd->dm_root, ofnode);
668
669 return *devp ? 0 : -ENOENT;
670}
671
672int device_get_global_by_ofnode(ofnode ofnode, struct udevice **devp)
Simon Glassae2efac2015-06-23 15:38:37 -0600673{
674 struct udevice *dev;
675
Jean-Jacques Hiblota7b0d6a2018-08-09 16:17:44 +0200676 dev = _device_find_global_by_ofnode(gd->dm_root, ofnode);
Simon Glassae2efac2015-06-23 15:38:37 -0600677 return device_get_device_tail(dev, dev ? 0 : -ENOENT, devp);
678}
679
Simon Glass44da7352014-10-13 23:41:49 -0600680int device_find_first_child(struct udevice *parent, struct udevice **devp)
681{
682 if (list_empty(&parent->child_head)) {
683 *devp = NULL;
684 } else {
685 *devp = list_first_entry(&parent->child_head, struct udevice,
686 sibling_node);
687 }
688
689 return 0;
690}
691
692int device_find_next_child(struct udevice **devp)
693{
694 struct udevice *dev = *devp;
695 struct udevice *parent = dev->parent;
696
697 if (list_is_last(&dev->sibling_node, &parent->child_head)) {
698 *devp = NULL;
699 } else {
700 *devp = list_entry(dev->sibling_node.next, struct udevice,
701 sibling_node);
702 }
703
704 return 0;
705}
Simon Glass70c3a0e2014-11-11 10:46:18 -0700706
Simon Glassb775e872018-10-01 12:22:07 -0600707int device_find_first_inactive_child(struct udevice *parent,
708 enum uclass_id uclass_id,
709 struct udevice **devp)
710{
711 struct udevice *dev;
712
713 *devp = NULL;
714 list_for_each_entry(dev, &parent->child_head, sibling_node) {
715 if (!device_active(dev) &&
716 device_get_uclass_id(dev) == uclass_id) {
717 *devp = dev;
718 return 0;
719 }
720 }
721
722 return -ENODEV;
723}
724
Simon Glassa11341a2018-11-18 08:14:31 -0700725int device_find_first_child_by_uclass(struct udevice *parent,
726 enum uclass_id uclass_id,
727 struct udevice **devp)
728{
729 struct udevice *dev;
730
731 *devp = NULL;
732 list_for_each_entry(dev, &parent->child_head, sibling_node) {
733 if (device_get_uclass_id(dev) == uclass_id) {
734 *devp = dev;
735 return 0;
736 }
737 }
738
739 return -ENODEV;
740}
741
742int device_find_child_by_name(struct udevice *parent, const char *name,
743 struct udevice **devp)
744{
745 struct udevice *dev;
746
747 *devp = NULL;
748
749 list_for_each_entry(dev, &parent->child_head, sibling_node) {
750 if (!strcmp(dev->name, name)) {
751 *devp = dev;
752 return 0;
753 }
754 }
755
756 return -ENODEV;
757}
758
Simon Glass2e5e5ec2018-10-01 12:22:06 -0600759struct udevice *dev_get_parent(const struct udevice *child)
Simon Glass43f488a2014-11-11 10:46:19 -0700760{
761 return child->parent;
762}
763
Simon Glass2e5e5ec2018-10-01 12:22:06 -0600764ulong dev_get_driver_data(const struct udevice *dev)
Simon Glass70c3a0e2014-11-11 10:46:18 -0700765{
Simon Glass46227bd2015-03-25 12:21:55 -0600766 return dev->driver_data;
Simon Glass70c3a0e2014-11-11 10:46:18 -0700767}
Simon Glass98fd5d12015-01-25 08:27:04 -0700768
Simon Glass2e5e5ec2018-10-01 12:22:06 -0600769const void *dev_get_driver_ops(const struct udevice *dev)
Przemyslaw Marczakd3ef0d72015-04-15 13:07:24 +0200770{
771 if (!dev || !dev->driver->ops)
772 return NULL;
773
774 return dev->driver->ops;
775}
776
Simon Glass2e5e5ec2018-10-01 12:22:06 -0600777enum uclass_id device_get_uclass_id(const struct udevice *dev)
Simon Glass98fd5d12015-01-25 08:27:04 -0700778{
779 return dev->uclass->uc_drv->id;
780}
Peng Fan99b72352015-02-10 14:46:32 +0800781
Simon Glass2e5e5ec2018-10-01 12:22:06 -0600782const char *dev_get_uclass_name(const struct udevice *dev)
Przemyslaw Marczak5ed2e422015-04-15 13:07:25 +0200783{
784 if (!dev)
785 return NULL;
786
787 return dev->uclass->uc_drv->name;
788}
789
Simon Glass2e5e5ec2018-10-01 12:22:06 -0600790bool device_has_children(const struct udevice *dev)
Simon Glass40f829a2015-03-25 12:21:57 -0600791{
792 return !list_empty(&dev->child_head);
793}
794
795bool device_has_active_children(struct udevice *dev)
796{
797 struct udevice *child;
798
799 for (device_find_first_child(dev, &child);
800 child;
801 device_find_next_child(&child)) {
802 if (device_active(child))
803 return true;
804 }
805
806 return false;
807}
808
809bool device_is_last_sibling(struct udevice *dev)
810{
811 struct udevice *parent = dev->parent;
812
813 if (!parent)
814 return false;
815 return list_is_last(&dev->sibling_node, &parent->child_head);
816}
Simon Glasse3b23e22015-07-30 13:40:39 -0600817
Simon Glass7760ba22016-05-01 13:52:23 -0600818void device_set_name_alloced(struct udevice *dev)
819{
Simon Glass2d4c7dc2016-07-04 11:58:15 -0600820 dev->flags |= DM_FLAG_NAME_ALLOCED;
Simon Glass7760ba22016-05-01 13:52:23 -0600821}
822
Simon Glasse3b23e22015-07-30 13:40:39 -0600823int device_set_name(struct udevice *dev, const char *name)
824{
825 name = strdup(name);
826 if (!name)
827 return -ENOMEM;
828 dev->name = name;
Simon Glass7760ba22016-05-01 13:52:23 -0600829 device_set_name_alloced(dev);
Simon Glasse3b23e22015-07-30 13:40:39 -0600830
831 return 0;
832}
Mugunthan V N4666bd92016-04-28 15:36:02 +0530833
Marek Vasut7f18c342019-08-31 18:03:28 +0200834#if CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)
Simon Glass54cbcc82017-05-18 20:08:57 -0600835bool device_is_compatible(struct udevice *dev, const char *compat)
Mugunthan V N4666bd92016-04-28 15:36:02 +0530836{
Masahiro Yamada9349bcc2018-04-19 12:14:02 +0900837 return ofnode_device_is_compatible(dev_ofnode(dev), compat);
Mugunthan V N4666bd92016-04-28 15:36:02 +0530838}
839
840bool of_machine_is_compatible(const char *compat)
841{
842 const void *fdt = gd->fdt_blob;
843
844 return !fdt_node_check_compatible(fdt, 0, compat);
845}
Mario Six3bedfbe2018-06-26 08:46:50 +0200846
847int dev_disable_by_path(const char *path)
848{
849 struct uclass *uc;
850 ofnode node = ofnode_path(path);
851 struct udevice *dev;
852 int ret = 1;
853
854 if (!of_live_active())
855 return -ENOSYS;
856
857 list_for_each_entry(uc, &gd->uclass_root, sibling_node) {
858 ret = uclass_find_device_by_ofnode(uc->uc_drv->id, node, &dev);
859 if (!ret)
860 break;
861 }
862
863 if (ret)
864 return ret;
865
866 ret = device_remove(dev, DM_REMOVE_NORMAL);
867 if (ret)
868 return ret;
869
870 ret = device_unbind(dev);
871 if (ret)
872 return ret;
873
874 return ofnode_set_enabled(node, false);
875}
876
877int dev_enable_by_path(const char *path)
878{
879 ofnode node = ofnode_path(path);
880 ofnode pnode = ofnode_get_parent(node);
881 struct udevice *parent;
882 int ret = 1;
883
884 if (!of_live_active())
885 return -ENOSYS;
886
887 ret = device_find_by_ofnode(pnode, &parent);
888 if (ret)
889 return ret;
890
891 ret = ofnode_set_enabled(node, true);
892 if (ret)
893 return ret;
894
Bin Meng9a9b0742018-10-10 22:06:58 -0700895 return lists_bind_fdt(parent, node, NULL, false);
Mario Six3bedfbe2018-06-26 08:46:50 +0200896}
Marek Vasut7f18c342019-08-31 18:03:28 +0200897#endif