blob: 355dbd147a9e9027ed2dc1855838ef57af10f57a [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>
Simon Glass63334482019-11-14 12:57:39 -070012#include <cpu_func.h>
Simon Glass0f2af882020-05-10 11:40:05 -060013#include <log.h>
Vignesh R0dff3702016-07-06 09:58:55 +053014#include <asm/io.h>
Philipp Tomsich9cf03b02018-01-08 13:59:18 +010015#include <clk.h>
Simon Glassdb6f0202014-07-23 06:55:12 -060016#include <fdtdec.h>
Stefan Roeseadc09052015-09-02 07:41:12 +020017#include <fdt_support.h>
Simon Glassdd6ab882014-02-26 15:59:18 -070018#include <malloc.h>
Simon Glass274e0b02020-05-10 11:39:56 -060019#include <asm/cache.h>
Simon Glassdd6ab882014-02-26 15:59:18 -070020#include <dm/device.h>
21#include <dm/device-internal.h>
22#include <dm/lists.h>
Mario Six1f1c1ec2018-01-15 11:07:20 +010023#include <dm/of_access.h>
Masahiro Yamadaf8efa632015-08-27 12:44:29 +090024#include <dm/pinctrl.h>
Simon Glassdd6ab882014-02-26 15:59:18 -070025#include <dm/platdata.h>
Simon Glass322b2902017-05-18 20:09:05 -060026#include <dm/read.h>
Simon Glassdd6ab882014-02-26 15:59:18 -070027#include <dm/uclass.h>
28#include <dm/uclass-internal.h>
29#include <dm/util.h>
30#include <linux/err.h>
31#include <linux/list.h>
Peng Fan6fba32d2018-07-27 10:20:38 +080032#include <power-domain.h>
Simon Glassdd6ab882014-02-26 15:59:18 -070033
Simon Glassdb6f0202014-07-23 06:55:12 -060034DECLARE_GLOBAL_DATA_PTR;
35
Stephen Warren8c93df12016-05-11 15:26:24 -060036static int device_bind_common(struct udevice *parent, const struct driver *drv,
37 const char *name, void *platdata,
Simon Glassb0cf4b32017-05-17 17:18:11 -060038 ulong driver_data, ofnode node,
Simon Glassafbf9b82016-07-04 11:58:18 -060039 uint of_platdata_size, struct udevice **devp)
Simon Glassdd6ab882014-02-26 15:59:18 -070040{
Heiko Schocherb74fcb42014-05-22 12:43:05 +020041 struct udevice *dev;
Simon Glassdd6ab882014-02-26 15:59:18 -070042 struct uclass *uc;
Przemyslaw Marczakd850e672015-04-15 13:07:18 +020043 int size, ret = 0;
Simon Glassdd6ab882014-02-26 15:59:18 -070044
Masahiro Yamadae1cc1f02015-08-27 12:44:28 +090045 if (devp)
46 *devp = NULL;
Simon Glassdd6ab882014-02-26 15:59:18 -070047 if (!name)
48 return -EINVAL;
49
50 ret = uclass_get(drv->id, &uc);
Simon Glass43313de2015-08-30 16:55:16 -060051 if (ret) {
52 debug("Missing uclass for driver %s\n", drv->name);
Simon Glassdd6ab882014-02-26 15:59:18 -070053 return ret;
Simon Glass43313de2015-08-30 16:55:16 -060054 }
Simon Glassdd6ab882014-02-26 15:59:18 -070055
Heiko Schocherb74fcb42014-05-22 12:43:05 +020056 dev = calloc(1, sizeof(struct udevice));
Simon Glassdd6ab882014-02-26 15:59:18 -070057 if (!dev)
58 return -ENOMEM;
59
60 INIT_LIST_HEAD(&dev->sibling_node);
61 INIT_LIST_HEAD(&dev->child_head);
62 INIT_LIST_HEAD(&dev->uclass_node);
Masahiro Yamada029bfca2015-07-25 21:52:37 +090063#ifdef CONFIG_DEVRES
Masahiro Yamada8b15b162015-07-25 21:52:35 +090064 INIT_LIST_HEAD(&dev->devres_head);
Masahiro Yamada029bfca2015-07-25 21:52:37 +090065#endif
Simon Glassdd6ab882014-02-26 15:59:18 -070066 dev->platdata = platdata;
Stephen Warren8c93df12016-05-11 15:26:24 -060067 dev->driver_data = driver_data;
Simon Glassdd6ab882014-02-26 15:59:18 -070068 dev->name = name;
Simon Glassb0cf4b32017-05-17 17:18:11 -060069 dev->node = node;
Simon Glassdd6ab882014-02-26 15:59:18 -070070 dev->parent = parent;
71 dev->driver = drv;
72 dev->uclass = uc;
Simon Glassdb6f0202014-07-23 06:55:12 -060073
Simon Glassdb6f0202014-07-23 06:55:12 -060074 dev->seq = -1;
Simon Glass0ccb0972015-01-25 08:27:05 -070075 dev->req_seq = -1;
Jean-Jacques Hiblota5da3002018-12-07 14:50:39 +010076 if (CONFIG_IS_ENABLED(DM_SEQ_ALIAS) &&
77 (uc->uc_drv->flags & DM_UC_FLAG_SEQ_ALIAS)) {
Simon Glass1bd3deb2015-02-27 22:06:30 -070078 /*
Stefan Roese9d4ce6b2016-03-16 09:58:01 +010079 * Some devices, such as a SPI bus, I2C bus and serial ports
80 * are numbered using aliases.
81 *
82 * This is just a 'requested' sequence, and will be
83 * resolved (and ->seq updated) when the device is probed.
84 */
Simon Glasse422f8c2020-07-09 18:39:24 -060085 if (CONFIG_IS_ENABLED(OF_CONTROL) &&
86 !CONFIG_IS_ENABLED(OF_PLATDATA)) {
Jean-Jacques Hiblota5da3002018-12-07 14:50:39 +010087 if (uc->uc_drv->name && ofnode_valid(node))
Simon Glass322b2902017-05-18 20:09:05 -060088 dev_read_alias_seq(dev, &dev->req_seq);
Thomas Fitzsimmonsf327ecb2019-09-06 07:51:18 -040089#if CONFIG_IS_ENABLED(OF_PRIOR_STAGE)
90 if (dev->req_seq == -1)
91 dev->req_seq =
92 uclass_find_next_free_req_seq(drv->id);
93#endif
Jean-Jacques Hiblota5da3002018-12-07 14:50:39 +010094 } else {
95 dev->req_seq = uclass_find_next_free_req_seq(drv->id);
Simon Glass0ccb0972015-01-25 08:27:05 -070096 }
Simon Glassdb6f0202014-07-23 06:55:12 -060097 }
Simon Glass1bd3deb2015-02-27 22:06:30 -070098
Simon Glassafbf9b82016-07-04 11:58:18 -060099 if (drv->platdata_auto_alloc_size) {
100 bool alloc = !platdata;
101
102 if (CONFIG_IS_ENABLED(OF_PLATDATA)) {
103 if (of_platdata_size) {
104 dev->flags |= DM_FLAG_OF_PLATDATA;
105 if (of_platdata_size <
106 drv->platdata_auto_alloc_size)
107 alloc = true;
108 }
109 }
110 if (alloc) {
111 dev->flags |= DM_FLAG_ALLOC_PDATA;
112 dev->platdata = calloc(1,
113 drv->platdata_auto_alloc_size);
114 if (!dev->platdata) {
115 ret = -ENOMEM;
116 goto fail_alloc1;
117 }
118 if (CONFIG_IS_ENABLED(OF_PLATDATA) && platdata) {
119 memcpy(dev->platdata, platdata,
120 of_platdata_size);
121 }
Simon Glassba750492015-01-25 08:27:00 -0700122 }
123 }
Przemyslaw Marczakd850e672015-04-15 13:07:18 +0200124
125 size = uc->uc_drv->per_device_platdata_auto_alloc_size;
126 if (size) {
127 dev->flags |= DM_FLAG_ALLOC_UCLASS_PDATA;
128 dev->uclass_platdata = calloc(1, size);
129 if (!dev->uclass_platdata) {
130 ret = -ENOMEM;
131 goto fail_alloc2;
132 }
133 }
Simon Glass11b61732015-01-25 08:27:01 -0700134
Przemyslaw Marczakd850e672015-04-15 13:07:18 +0200135 if (parent) {
136 size = parent->driver->per_child_platdata_auto_alloc_size;
Simon Glass57f95402015-01-25 08:27:02 -0700137 if (!size) {
138 size = parent->uclass->uc_drv->
139 per_child_platdata_auto_alloc_size;
140 }
Simon Glass11b61732015-01-25 08:27:01 -0700141 if (size) {
142 dev->flags |= DM_FLAG_ALLOC_PARENT_PDATA;
143 dev->parent_platdata = calloc(1, size);
144 if (!dev->parent_platdata) {
145 ret = -ENOMEM;
Przemyslaw Marczakd850e672015-04-15 13:07:18 +0200146 goto fail_alloc3;
Simon Glass11b61732015-01-25 08:27:01 -0700147 }
148 }
Heinrich Schuchardt5246b372020-02-15 21:38:48 +0100149 /* put dev into parent's successor list */
Simon Glassdd6ab882014-02-26 15:59:18 -0700150 list_add_tail(&dev->sibling_node, &parent->child_head);
Heinrich Schuchardt5246b372020-02-15 21:38:48 +0100151 }
Simon Glassdd6ab882014-02-26 15:59:18 -0700152
153 ret = uclass_bind_device(dev);
154 if (ret)
Simon Glass4ebe01b2015-01-25 08:26:59 -0700155 goto fail_uclass_bind;
Simon Glassdd6ab882014-02-26 15:59:18 -0700156
157 /* if we fail to bind we remove device from successors and free it */
158 if (drv->bind) {
159 ret = drv->bind(dev);
Simon Glass4ebe01b2015-01-25 08:26:59 -0700160 if (ret)
Simon Glassdd6ab882014-02-26 15:59:18 -0700161 goto fail_bind;
Simon Glassdd6ab882014-02-26 15:59:18 -0700162 }
Simon Glassa4a51a02015-01-25 08:27:03 -0700163 if (parent && parent->driver->child_post_bind) {
164 ret = parent->driver->child_post_bind(dev);
165 if (ret)
166 goto fail_child_post_bind;
167 }
Simon Glass286863d2016-01-05 09:30:59 -0700168 if (uc->uc_drv->post_bind) {
169 ret = uc->uc_drv->post_bind(dev);
170 if (ret)
171 goto fail_uclass_post_bind;
172 }
Simon Glassa4a51a02015-01-25 08:27:03 -0700173
Simon Glassdd6ab882014-02-26 15:59:18 -0700174 if (parent)
Masahiro Yamadaf70f39f2017-09-29 12:31:20 +0900175 pr_debug("Bound device %s to %s\n", dev->name, parent->name);
Masahiro Yamadae1cc1f02015-08-27 12:44:28 +0900176 if (devp)
177 *devp = dev;
Simon Glassdd6ab882014-02-26 15:59:18 -0700178
Masahiro Yamadabdbb5dd2015-07-25 21:52:34 +0900179 dev->flags |= DM_FLAG_BOUND;
180
Simon Glassdd6ab882014-02-26 15:59:18 -0700181 return 0;
182
Simon Glass286863d2016-01-05 09:30:59 -0700183fail_uclass_post_bind:
184 /* There is no child unbind() method, so no clean-up required */
Simon Glassa4a51a02015-01-25 08:27:03 -0700185fail_child_post_bind:
Masahiro Yamada04aa00d2015-08-12 07:31:52 +0900186 if (CONFIG_IS_ENABLED(DM_DEVICE_REMOVE)) {
Simon Glasse70a4f42015-02-27 22:06:33 -0700187 if (drv->unbind && drv->unbind(dev)) {
188 dm_warn("unbind() method failed on dev '%s' on error path\n",
189 dev->name);
190 }
Simon Glassa4a51a02015-01-25 08:27:03 -0700191 }
192
Simon Glassdd6ab882014-02-26 15:59:18 -0700193fail_bind:
Masahiro Yamada04aa00d2015-08-12 07:31:52 +0900194 if (CONFIG_IS_ENABLED(DM_DEVICE_REMOVE)) {
Simon Glasse70a4f42015-02-27 22:06:33 -0700195 if (uclass_unbind_device(dev)) {
196 dm_warn("Failed to unbind dev '%s' on error path\n",
197 dev->name);
198 }
Simon Glass4ebe01b2015-01-25 08:26:59 -0700199 }
200fail_uclass_bind:
Masahiro Yamada04aa00d2015-08-12 07:31:52 +0900201 if (CONFIG_IS_ENABLED(DM_DEVICE_REMOVE)) {
Simon Glasse70a4f42015-02-27 22:06:33 -0700202 list_del(&dev->sibling_node);
203 if (dev->flags & DM_FLAG_ALLOC_PARENT_PDATA) {
204 free(dev->parent_platdata);
205 dev->parent_platdata = NULL;
206 }
Simon Glass11b61732015-01-25 08:27:01 -0700207 }
Przemyslaw Marczakd850e672015-04-15 13:07:18 +0200208fail_alloc3:
209 if (dev->flags & DM_FLAG_ALLOC_UCLASS_PDATA) {
210 free(dev->uclass_platdata);
211 dev->uclass_platdata = NULL;
212 }
Simon Glass11b61732015-01-25 08:27:01 -0700213fail_alloc2:
Simon Glassba750492015-01-25 08:27:00 -0700214 if (dev->flags & DM_FLAG_ALLOC_PDATA) {
215 free(dev->platdata);
216 dev->platdata = NULL;
217 }
218fail_alloc1:
Masahiro Yamada8b15b162015-07-25 21:52:35 +0900219 devres_release_all(dev);
220
Simon Glassdd6ab882014-02-26 15:59:18 -0700221 free(dev);
Simon Glass4ebe01b2015-01-25 08:26:59 -0700222
Simon Glassdd6ab882014-02-26 15:59:18 -0700223 return ret;
224}
225
Stephen Warren8c93df12016-05-11 15:26:24 -0600226int device_bind_with_driver_data(struct udevice *parent,
227 const struct driver *drv, const char *name,
Simon Glass322b2902017-05-18 20:09:05 -0600228 ulong driver_data, ofnode node,
Stephen Warren8c93df12016-05-11 15:26:24 -0600229 struct udevice **devp)
230{
Simon Glass322b2902017-05-18 20:09:05 -0600231 return device_bind_common(parent, drv, name, NULL, driver_data, node,
232 0, devp);
Stephen Warren8c93df12016-05-11 15:26:24 -0600233}
234
235int device_bind(struct udevice *parent, const struct driver *drv,
236 const char *name, void *platdata, int of_offset,
237 struct udevice **devp)
238{
Simon Glassb0cf4b32017-05-17 17:18:11 -0600239 return device_bind_common(parent, drv, name, platdata, 0,
240 offset_to_ofnode(of_offset), 0, devp);
Stephen Warren8c93df12016-05-11 15:26:24 -0600241}
242
Simon Glass9eb151e2018-06-11 13:07:15 -0600243int device_bind_ofnode(struct udevice *parent, const struct driver *drv,
244 const char *name, void *platdata, ofnode node,
245 struct udevice **devp)
246{
247 return device_bind_common(parent, drv, name, platdata, 0, node, 0,
248 devp);
249}
250
Simon Glassfef72b72014-07-23 06:55:03 -0600251int device_bind_by_name(struct udevice *parent, bool pre_reloc_only,
Walter Lozano95282d62020-06-25 01:10:10 -0300252 struct driver_info *info, struct udevice **devp)
Simon Glassdd6ab882014-02-26 15:59:18 -0700253{
254 struct driver *drv;
Simon Glassafbf9b82016-07-04 11:58:18 -0600255 uint platdata_size = 0;
Walter Lozanoffc41b02020-06-25 01:10:11 -0300256 int ret;
Simon Glassdd6ab882014-02-26 15:59:18 -0700257
258 drv = lists_driver_lookup_name(info->name);
259 if (!drv)
260 return -ENOENT;
Simon Glassfef72b72014-07-23 06:55:03 -0600261 if (pre_reloc_only && !(drv->flags & DM_FLAG_PRE_RELOC))
262 return -EPERM;
Simon Glassdd6ab882014-02-26 15:59:18 -0700263
Simon Glassafbf9b82016-07-04 11:58:18 -0600264#if CONFIG_IS_ENABLED(OF_PLATDATA)
265 platdata_size = info->platdata_size;
266#endif
Walter Lozanoffc41b02020-06-25 01:10:11 -0300267 ret = device_bind_common(parent, drv, info->name,
268 (void *)info->platdata, 0, ofnode_null(),
269 platdata_size, devp);
270 if (ret)
271 return ret;
272#if CONFIG_IS_ENABLED(OF_PLATDATA)
273 info->dev = *devp;
274#endif
275
276 return ret;
Simon Glassdd6ab882014-02-26 15:59:18 -0700277}
278
Simon Glass825d3f92015-03-25 12:21:53 -0600279static void *alloc_priv(int size, uint flags)
280{
281 void *priv;
282
283 if (flags & DM_FLAG_ALLOC_PRIV_DMA) {
Faiz Abbas03020a92017-09-19 16:53:50 +0530284 size = ROUND(size, ARCH_DMA_MINALIGN);
Simon Glass825d3f92015-03-25 12:21:53 -0600285 priv = memalign(ARCH_DMA_MINALIGN, size);
Simon Glass27bc9b12017-04-04 13:00:19 -0600286 if (priv) {
Simon Glass825d3f92015-03-25 12:21:53 -0600287 memset(priv, '\0', size);
Simon Glass27bc9b12017-04-04 13:00:19 -0600288
289 /*
290 * Ensure that the zero bytes are flushed to memory.
291 * This prevents problems if the driver uses this as
292 * both an input and an output buffer:
293 *
294 * 1. Zeroes written to buffer (here) and sit in the
295 * cache
296 * 2. Driver issues a read command to DMA
297 * 3. CPU runs out of cache space and evicts some cache
298 * data in the buffer, writing zeroes to RAM from
299 * the memset() above
300 * 4. DMA completes
301 * 5. Buffer now has some DMA data and some zeroes
302 * 6. Data being read is now incorrect
303 *
304 * To prevent this, ensure that the cache is clean
305 * within this range at the start. The driver can then
306 * use normal flush-after-write, invalidate-before-read
307 * procedures.
308 *
309 * TODO(sjg@chromium.org): Drop this microblaze
310 * exception.
311 */
312#ifndef CONFIG_MICROBLAZE
313 flush_dcache_range((ulong)priv, (ulong)priv + size);
314#endif
315 }
Simon Glass825d3f92015-03-25 12:21:53 -0600316 } else {
317 priv = calloc(1, size);
318 }
319
320 return priv;
321}
322
Simon Glass4a263852019-12-29 21:19:20 -0700323int device_ofdata_to_platdata(struct udevice *dev)
Simon Glassdd6ab882014-02-26 15:59:18 -0700324{
Simon Glassa626dff2015-03-25 12:21:54 -0600325 const struct driver *drv;
Simon Glassdd6ab882014-02-26 15:59:18 -0700326 int size = 0;
327 int ret;
328
329 if (!dev)
330 return -EINVAL;
331
Simon Glassd3c9d302019-12-29 21:19:21 -0700332 if (dev->flags & DM_FLAG_PLATDATA_VALID)
Simon Glassdd6ab882014-02-26 15:59:18 -0700333 return 0;
334
Simon Glass44c47892020-04-05 15:38:19 -0600335 /* Ensure all parents have ofdata */
336 if (dev->parent) {
337 ret = device_ofdata_to_platdata(dev->parent);
338 if (ret)
339 goto fail;
340
341 /*
342 * The device might have already been probed during
343 * the call to device_probe() on its parent device
344 * (e.g. PCI bridge devices). Test the flags again
345 * so that we don't mess up the device.
346 */
347 if (dev->flags & DM_FLAG_PLATDATA_VALID)
348 return 0;
349 }
350
Simon Glassdd6ab882014-02-26 15:59:18 -0700351 drv = dev->driver;
352 assert(drv);
353
Bin Meng82cdd782015-08-24 01:14:02 -0700354 /* Allocate private data if requested and not reentered */
355 if (drv->priv_auto_alloc_size && !dev->priv) {
Simon Glass825d3f92015-03-25 12:21:53 -0600356 dev->priv = alloc_priv(drv->priv_auto_alloc_size, drv->flags);
Simon Glassdd6ab882014-02-26 15:59:18 -0700357 if (!dev->priv) {
358 ret = -ENOMEM;
359 goto fail;
360 }
361 }
Bin Meng82cdd782015-08-24 01:14:02 -0700362 /* Allocate private data if requested and not reentered */
Simon Glassdd6ab882014-02-26 15:59:18 -0700363 size = dev->uclass->uc_drv->per_device_auto_alloc_size;
Bin Meng82cdd782015-08-24 01:14:02 -0700364 if (size && !dev->uclass_priv) {
Simon Glass7ebd13d2018-10-01 12:22:05 -0600365 dev->uclass_priv = alloc_priv(size,
366 dev->uclass->uc_drv->flags);
Simon Glassdd6ab882014-02-26 15:59:18 -0700367 if (!dev->uclass_priv) {
368 ret = -ENOMEM;
369 goto fail;
370 }
371 }
372
Simon Glassa8d8f162019-12-29 21:19:18 -0700373 /* Allocate parent data for this child */
Simon Glassdd6ab882014-02-26 15:59:18 -0700374 if (dev->parent) {
Simon Glass60d971b2014-07-23 06:55:20 -0600375 size = dev->parent->driver->per_child_auto_alloc_size;
Simon Glassc23b4282015-01-25 08:27:06 -0700376 if (!size) {
377 size = dev->parent->uclass->uc_drv->
378 per_child_auto_alloc_size;
379 }
Bin Meng82cdd782015-08-24 01:14:02 -0700380 if (size && !dev->parent_priv) {
Simon Glass825d3f92015-03-25 12:21:53 -0600381 dev->parent_priv = alloc_priv(size, drv->flags);
Simon Glass60d971b2014-07-23 06:55:20 -0600382 if (!dev->parent_priv) {
383 ret = -ENOMEM;
384 goto fail;
385 }
386 }
Simon Glassa8d8f162019-12-29 21:19:18 -0700387 }
388
389 if (drv->ofdata_to_platdata &&
390 (CONFIG_IS_ENABLED(OF_PLATDATA) || dev_has_of_node(dev))) {
391 ret = drv->ofdata_to_platdata(dev);
392 if (ret)
393 goto fail;
394 }
Simon Glass60d971b2014-07-23 06:55:20 -0600395
Simon Glassd3c9d302019-12-29 21:19:21 -0700396 dev->flags |= DM_FLAG_PLATDATA_VALID;
397
Simon Glass4a263852019-12-29 21:19:20 -0700398 return 0;
399fail:
400 device_free(dev);
401
402 return ret;
403}
404
405int device_probe(struct udevice *dev)
406{
407 const struct driver *drv;
408 int ret;
409 int seq;
410
411 if (!dev)
412 return -EINVAL;
413
414 if (dev->flags & DM_FLAG_ACTIVATED)
415 return 0;
416
417 drv = dev->driver;
418 assert(drv);
419
420 ret = device_ofdata_to_platdata(dev);
421 if (ret)
422 goto fail;
423
Simon Glassa8d8f162019-12-29 21:19:18 -0700424 /* Ensure all parents are probed */
425 if (dev->parent) {
Simon Glassdd6ab882014-02-26 15:59:18 -0700426 ret = device_probe(dev->parent);
427 if (ret)
428 goto fail;
Bin Meng82cdd782015-08-24 01:14:02 -0700429
430 /*
431 * The device might have already been probed during
432 * the call to device_probe() on its parent device
433 * (e.g. PCI bridge devices). Test the flags again
434 * so that we don't mess up the device.
435 */
436 if (dev->flags & DM_FLAG_ACTIVATED)
437 return 0;
Simon Glassdd6ab882014-02-26 15:59:18 -0700438 }
Simon Glassdb6f0202014-07-23 06:55:12 -0600439
440 seq = uclass_resolve_seq(dev);
441 if (seq < 0) {
442 ret = seq;
443 goto fail;
444 }
445 dev->seq = seq;
Simon Glassdd6ab882014-02-26 15:59:18 -0700446
Simon Glass8b04e732015-03-25 12:21:56 -0600447 dev->flags |= DM_FLAG_ACTIVATED;
448
Simon Glassbb53bda2015-09-12 08:45:19 -0600449 /*
450 * Process pinctrl for everything except the root device, and
Simon Glass27ce9622016-01-21 19:43:25 -0700451 * continue regardless of the result of pinctrl. Don't process pinctrl
452 * settings for pinctrl devices since the device may not yet be
453 * probed.
Simon Glassbb53bda2015-09-12 08:45:19 -0600454 */
Simon Glass27ce9622016-01-21 19:43:25 -0700455 if (dev->parent && device_get_uclass_id(dev) != UCLASS_PINCTRL)
Simon Glassbb53bda2015-09-12 08:45:19 -0600456 pinctrl_select_state(dev, "default");
Masahiro Yamadaf8efa632015-08-27 12:44:29 +0900457
Anatolij Gustschin63a34672019-07-14 21:11:01 +0200458 if (CONFIG_IS_ENABLED(POWER_DOMAIN) && dev->parent &&
Lokesh Vutlac10e52b2019-09-27 13:48:12 +0530459 (device_get_uclass_id(dev) != UCLASS_POWER_DOMAIN) &&
460 !(drv->flags & DM_FLAG_DEFAULT_PD_CTRL_OFF)) {
Peng Fand2201112019-09-17 09:29:22 +0000461 ret = dev_power_domain_on(dev);
462 if (ret)
463 goto fail;
Peng Fan6fba32d2018-07-27 10:20:38 +0800464 }
465
Simon Glass9c1f3822015-03-05 12:25:22 -0700466 ret = uclass_pre_probe_device(dev);
Simon Glass5104b982015-01-25 08:27:10 -0700467 if (ret)
468 goto fail;
469
Simon Glassd45560d2014-07-23 06:55:21 -0600470 if (dev->parent && dev->parent->driver->child_pre_probe) {
471 ret = dev->parent->driver->child_pre_probe(dev);
472 if (ret)
473 goto fail;
474 }
475
Bin Meng513cf402019-07-05 09:23:16 -0700476 /* Only handle devices that have a valid ofnode */
477 if (dev_of_valid(dev)) {
478 /*
479 * Process 'assigned-{clocks/clock-parents/clock-rates}'
480 * properties
481 */
Jean-Jacques Hiblot9601f322019-10-22 14:00:06 +0200482 ret = clk_set_defaults(dev, 0);
Bin Meng513cf402019-07-05 09:23:16 -0700483 if (ret)
484 goto fail;
485 }
Philipp Tomsich9cf03b02018-01-08 13:59:18 +0100486
Simon Glassdd6ab882014-02-26 15:59:18 -0700487 if (drv->probe) {
488 ret = drv->probe(dev);
Simon Glass9046dec2019-12-29 21:19:16 -0700489 if (ret)
Simon Glassdd6ab882014-02-26 15:59:18 -0700490 goto fail;
491 }
492
Simon Glassdd6ab882014-02-26 15:59:18 -0700493 ret = uclass_post_probe_device(dev);
Simon Glass8b04e732015-03-25 12:21:56 -0600494 if (ret)
Simon Glassdd6ab882014-02-26 15:59:18 -0700495 goto fail_uclass;
Simon Glassdd6ab882014-02-26 15:59:18 -0700496
Peng Fan7cf58dd2016-03-12 13:17:38 +0800497 if (dev->parent && device_get_uclass_id(dev) == UCLASS_PINCTRL)
498 pinctrl_select_state(dev, "default");
499
Simon Glassdd6ab882014-02-26 15:59:18 -0700500 return 0;
501fail_uclass:
Stefan Roese80b5bc92017-03-20 12:51:48 +0100502 if (device_remove(dev, DM_REMOVE_NORMAL)) {
Simon Glassdd6ab882014-02-26 15:59:18 -0700503 dm_warn("%s: Device '%s' failed to remove on error path\n",
504 __func__, dev->name);
505 }
506fail:
Simon Glass8b04e732015-03-25 12:21:56 -0600507 dev->flags &= ~DM_FLAG_ACTIVATED;
508
Simon Glassdb6f0202014-07-23 06:55:12 -0600509 dev->seq = -1;
Simon Glassdd6ab882014-02-26 15:59:18 -0700510 device_free(dev);
511
512 return ret;
513}
514
Simon Glass2e5e5ec2018-10-01 12:22:06 -0600515void *dev_get_platdata(const struct udevice *dev)
Simon Glassdd6ab882014-02-26 15:59:18 -0700516{
517 if (!dev) {
Simon Glassdf1e0722014-12-10 08:55:56 -0700518 dm_warn("%s: null device\n", __func__);
Simon Glassdd6ab882014-02-26 15:59:18 -0700519 return NULL;
520 }
521
522 return dev->platdata;
523}
524
Simon Glass2e5e5ec2018-10-01 12:22:06 -0600525void *dev_get_parent_platdata(const struct udevice *dev)
Simon Glass11b61732015-01-25 08:27:01 -0700526{
527 if (!dev) {
Simon Glasscdfc5052015-07-06 16:47:40 -0600528 dm_warn("%s: null device\n", __func__);
Simon Glass11b61732015-01-25 08:27:01 -0700529 return NULL;
530 }
531
532 return dev->parent_platdata;
533}
534
Simon Glass2e5e5ec2018-10-01 12:22:06 -0600535void *dev_get_uclass_platdata(const struct udevice *dev)
Przemyslaw Marczakd850e672015-04-15 13:07:18 +0200536{
537 if (!dev) {
Simon Glasscdfc5052015-07-06 16:47:40 -0600538 dm_warn("%s: null device\n", __func__);
Przemyslaw Marczakd850e672015-04-15 13:07:18 +0200539 return NULL;
540 }
541
542 return dev->uclass_platdata;
543}
544
Simon Glass2e5e5ec2018-10-01 12:22:06 -0600545void *dev_get_priv(const struct udevice *dev)
Simon Glassdd6ab882014-02-26 15:59:18 -0700546{
547 if (!dev) {
Simon Glassdf1e0722014-12-10 08:55:56 -0700548 dm_warn("%s: null device\n", __func__);
Simon Glassdd6ab882014-02-26 15:59:18 -0700549 return NULL;
550 }
551
552 return dev->priv;
553}
Simon Glass48d4e292014-07-23 06:55:19 -0600554
Simon Glass2e5e5ec2018-10-01 12:22:06 -0600555void *dev_get_uclass_priv(const struct udevice *dev)
Simon Glassde0977b2015-03-05 12:25:20 -0700556{
557 if (!dev) {
558 dm_warn("%s: null device\n", __func__);
559 return NULL;
560 }
561
562 return dev->uclass_priv;
563}
564
Simon Glass2e5e5ec2018-10-01 12:22:06 -0600565void *dev_get_parent_priv(const struct udevice *dev)
Simon Glass60d971b2014-07-23 06:55:20 -0600566{
567 if (!dev) {
Simon Glassdf1e0722014-12-10 08:55:56 -0700568 dm_warn("%s: null device\n", __func__);
Simon Glass60d971b2014-07-23 06:55:20 -0600569 return NULL;
570 }
571
572 return dev->parent_priv;
573}
574
Simon Glass48d4e292014-07-23 06:55:19 -0600575static int device_get_device_tail(struct udevice *dev, int ret,
576 struct udevice **devp)
577{
578 if (ret)
579 return ret;
580
581 ret = device_probe(dev);
582 if (ret)
583 return ret;
584
585 *devp = dev;
586
587 return 0;
588}
589
Marek Vasut7f18c342019-08-31 18:03:28 +0200590#if CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)
Mario Six3bedfbe2018-06-26 08:46:50 +0200591/**
592 * device_find_by_ofnode() - Return device associated with given ofnode
593 *
594 * The returned device is *not* activated.
595 *
596 * @node: The ofnode for which a associated device should be looked up
597 * @devp: Pointer to structure to hold the found device
598 * Return: 0 if OK, -ve on error
599 */
600static int device_find_by_ofnode(ofnode node, struct udevice **devp)
601{
602 struct uclass *uc;
603 struct udevice *dev;
604 int ret;
605
606 list_for_each_entry(uc, &gd->uclass_root, sibling_node) {
607 ret = uclass_find_device_by_ofnode(uc->uc_drv->id, node,
608 &dev);
609 if (!ret || dev) {
610 *devp = dev;
611 return 0;
612 }
613 }
614
615 return -ENODEV;
616}
Marek Vasut7f18c342019-08-31 18:03:28 +0200617#endif
Mario Six3bedfbe2018-06-26 08:46:50 +0200618
Simon Glass2a580882020-01-27 08:49:36 -0700619int device_get_child(const struct udevice *parent, int index,
620 struct udevice **devp)
Simon Glass48d4e292014-07-23 06:55:19 -0600621{
622 struct udevice *dev;
623
624 list_for_each_entry(dev, &parent->child_head, sibling_node) {
625 if (!index--)
626 return device_get_device_tail(dev, 0, devp);
627 }
628
629 return -ENODEV;
630}
631
Simon Glass2a580882020-01-27 08:49:36 -0700632int device_get_child_count(const struct udevice *parent)
Lokesh Vutlafa45a082019-09-04 16:01:26 +0530633{
634 struct udevice *dev;
635 int count = 0;
636
637 list_for_each_entry(dev, &parent->child_head, sibling_node)
638 count++;
639
640 return count;
641}
642
Simon Glass2a580882020-01-27 08:49:36 -0700643int device_find_child_by_seq(const struct udevice *parent, int seq_or_req_seq,
Simon Glass48d4e292014-07-23 06:55:19 -0600644 bool find_req_seq, struct udevice **devp)
645{
646 struct udevice *dev;
647
648 *devp = NULL;
649 if (seq_or_req_seq == -1)
650 return -ENODEV;
651
652 list_for_each_entry(dev, &parent->child_head, sibling_node) {
653 if ((find_req_seq ? dev->req_seq : dev->seq) ==
654 seq_or_req_seq) {
655 *devp = dev;
656 return 0;
657 }
658 }
659
660 return -ENODEV;
661}
662
Simon Glass2a580882020-01-27 08:49:36 -0700663int device_get_child_by_seq(const struct udevice *parent, int seq,
Simon Glass48d4e292014-07-23 06:55:19 -0600664 struct udevice **devp)
665{
666 struct udevice *dev;
667 int ret;
668
669 *devp = NULL;
670 ret = device_find_child_by_seq(parent, seq, false, &dev);
671 if (ret == -ENODEV) {
672 /*
673 * We didn't find it in probed devices. See if there is one
674 * that will request this seq if probed.
675 */
676 ret = device_find_child_by_seq(parent, seq, true, &dev);
677 }
678 return device_get_device_tail(dev, ret, devp);
679}
680
Simon Glass2a580882020-01-27 08:49:36 -0700681int device_find_child_by_of_offset(const struct udevice *parent, int of_offset,
Simon Glass48d4e292014-07-23 06:55:19 -0600682 struct udevice **devp)
683{
684 struct udevice *dev;
685
686 *devp = NULL;
687
688 list_for_each_entry(dev, &parent->child_head, sibling_node) {
Simon Glassdd79d6e2017-01-17 16:52:55 -0700689 if (dev_of_offset(dev) == of_offset) {
Simon Glass48d4e292014-07-23 06:55:19 -0600690 *devp = dev;
691 return 0;
692 }
693 }
694
695 return -ENODEV;
696}
697
Simon Glass2a580882020-01-27 08:49:36 -0700698int device_get_child_by_of_offset(const struct udevice *parent, int node,
Simon Glass48d4e292014-07-23 06:55:19 -0600699 struct udevice **devp)
700{
701 struct udevice *dev;
702 int ret;
703
704 *devp = NULL;
Simon Glass861bc9f2015-06-23 15:38:38 -0600705 ret = device_find_child_by_of_offset(parent, node, &dev);
Simon Glass48d4e292014-07-23 06:55:19 -0600706 return device_get_device_tail(dev, ret, devp);
707}
Simon Glass44da7352014-10-13 23:41:49 -0600708
Jean-Jacques Hiblota7b0d6a2018-08-09 16:17:44 +0200709static struct udevice *_device_find_global_by_ofnode(struct udevice *parent,
710 ofnode ofnode)
Simon Glassae2efac2015-06-23 15:38:37 -0600711{
712 struct udevice *dev, *found;
713
Jean-Jacques Hiblota7b0d6a2018-08-09 16:17:44 +0200714 if (ofnode_equal(dev_ofnode(parent), ofnode))
Simon Glassae2efac2015-06-23 15:38:37 -0600715 return parent;
716
717 list_for_each_entry(dev, &parent->child_head, sibling_node) {
Jean-Jacques Hiblota7b0d6a2018-08-09 16:17:44 +0200718 found = _device_find_global_by_ofnode(dev, ofnode);
Simon Glassae2efac2015-06-23 15:38:37 -0600719 if (found)
720 return found;
721 }
722
723 return NULL;
724}
725
Jean-Jacques Hiblota7b0d6a2018-08-09 16:17:44 +0200726int device_find_global_by_ofnode(ofnode ofnode, struct udevice **devp)
727{
728 *devp = _device_find_global_by_ofnode(gd->dm_root, ofnode);
729
730 return *devp ? 0 : -ENOENT;
731}
732
733int device_get_global_by_ofnode(ofnode ofnode, struct udevice **devp)
Simon Glassae2efac2015-06-23 15:38:37 -0600734{
735 struct udevice *dev;
736
Jean-Jacques Hiblota7b0d6a2018-08-09 16:17:44 +0200737 dev = _device_find_global_by_ofnode(gd->dm_root, ofnode);
Simon Glassae2efac2015-06-23 15:38:37 -0600738 return device_get_device_tail(dev, dev ? 0 : -ENOENT, devp);
739}
Walter Lozanoffc41b02020-06-25 01:10:11 -0300740
741#if CONFIG_IS_ENABLED(OF_PLATDATA)
742int device_get_by_driver_info(const struct driver_info *info,
743 struct udevice **devp)
744{
745 struct udevice *dev;
746
747 dev = info->dev;
748
749 return device_get_device_tail(dev, dev ? 0 : -ENOENT, devp);
750}
751#endif
Simon Glassae2efac2015-06-23 15:38:37 -0600752
Simon Glass2a580882020-01-27 08:49:36 -0700753int device_find_first_child(const struct udevice *parent, struct udevice **devp)
Simon Glass44da7352014-10-13 23:41:49 -0600754{
755 if (list_empty(&parent->child_head)) {
756 *devp = NULL;
757 } else {
758 *devp = list_first_entry(&parent->child_head, struct udevice,
759 sibling_node);
760 }
761
762 return 0;
763}
764
765int device_find_next_child(struct udevice **devp)
766{
767 struct udevice *dev = *devp;
768 struct udevice *parent = dev->parent;
769
770 if (list_is_last(&dev->sibling_node, &parent->child_head)) {
771 *devp = NULL;
772 } else {
773 *devp = list_entry(dev->sibling_node.next, struct udevice,
774 sibling_node);
775 }
776
777 return 0;
778}
Simon Glass70c3a0e2014-11-11 10:46:18 -0700779
Simon Glass2a580882020-01-27 08:49:36 -0700780int device_find_first_inactive_child(const struct udevice *parent,
Simon Glassb775e872018-10-01 12:22:07 -0600781 enum uclass_id uclass_id,
782 struct udevice **devp)
783{
784 struct udevice *dev;
785
786 *devp = NULL;
787 list_for_each_entry(dev, &parent->child_head, sibling_node) {
788 if (!device_active(dev) &&
789 device_get_uclass_id(dev) == uclass_id) {
790 *devp = dev;
791 return 0;
792 }
793 }
794
795 return -ENODEV;
796}
797
Simon Glass2a580882020-01-27 08:49:36 -0700798int device_find_first_child_by_uclass(const struct udevice *parent,
Simon Glassa11341a2018-11-18 08:14:31 -0700799 enum uclass_id uclass_id,
800 struct udevice **devp)
801{
802 struct udevice *dev;
803
804 *devp = NULL;
805 list_for_each_entry(dev, &parent->child_head, sibling_node) {
806 if (device_get_uclass_id(dev) == uclass_id) {
807 *devp = dev;
808 return 0;
809 }
810 }
811
812 return -ENODEV;
813}
814
Simon Glass2a580882020-01-27 08:49:36 -0700815int device_find_child_by_name(const struct udevice *parent, const char *name,
Simon Glassa11341a2018-11-18 08:14:31 -0700816 struct udevice **devp)
817{
818 struct udevice *dev;
819
820 *devp = NULL;
821
822 list_for_each_entry(dev, &parent->child_head, sibling_node) {
823 if (!strcmp(dev->name, name)) {
824 *devp = dev;
825 return 0;
826 }
827 }
828
829 return -ENODEV;
830}
831
Simon Glass76db9b72020-01-27 08:49:48 -0700832int device_first_child_err(struct udevice *parent, struct udevice **devp)
833{
834 struct udevice *dev;
835
836 device_find_first_child(parent, &dev);
837 if (!dev)
838 return -ENODEV;
839
840 return device_get_device_tail(dev, 0, devp);
841}
842
843int device_next_child_err(struct udevice **devp)
844{
845 struct udevice *dev = *devp;
846
847 device_find_next_child(&dev);
848 if (!dev)
849 return -ENODEV;
850
851 return device_get_device_tail(dev, 0, devp);
852}
853
Simon Glass000676b2020-01-27 08:49:47 -0700854int device_first_child_ofdata_err(struct udevice *parent, struct udevice **devp)
855{
856 struct udevice *dev;
857 int ret;
858
859 device_find_first_child(parent, &dev);
860 if (!dev)
861 return -ENODEV;
862
863 ret = device_ofdata_to_platdata(dev);
864 if (ret)
865 return ret;
866
867 *devp = dev;
868
869 return 0;
870}
871
872int device_next_child_ofdata_err(struct udevice **devp)
873{
874 struct udevice *dev = *devp;
875 int ret;
876
877 device_find_next_child(&dev);
878 if (!dev)
879 return -ENODEV;
880
881 ret = device_ofdata_to_platdata(dev);
882 if (ret)
883 return ret;
884
885 *devp = dev;
886
887 return 0;
888}
889
Simon Glass2e5e5ec2018-10-01 12:22:06 -0600890struct udevice *dev_get_parent(const struct udevice *child)
Simon Glass43f488a2014-11-11 10:46:19 -0700891{
892 return child->parent;
893}
894
Simon Glass2e5e5ec2018-10-01 12:22:06 -0600895ulong dev_get_driver_data(const struct udevice *dev)
Simon Glass70c3a0e2014-11-11 10:46:18 -0700896{
Simon Glass46227bd2015-03-25 12:21:55 -0600897 return dev->driver_data;
Simon Glass70c3a0e2014-11-11 10:46:18 -0700898}
Simon Glass98fd5d12015-01-25 08:27:04 -0700899
Simon Glass2e5e5ec2018-10-01 12:22:06 -0600900const void *dev_get_driver_ops(const struct udevice *dev)
Przemyslaw Marczakd3ef0d72015-04-15 13:07:24 +0200901{
902 if (!dev || !dev->driver->ops)
903 return NULL;
904
905 return dev->driver->ops;
906}
907
Simon Glass2e5e5ec2018-10-01 12:22:06 -0600908enum uclass_id device_get_uclass_id(const struct udevice *dev)
Simon Glass98fd5d12015-01-25 08:27:04 -0700909{
910 return dev->uclass->uc_drv->id;
911}
Peng Fan99b72352015-02-10 14:46:32 +0800912
Simon Glass2e5e5ec2018-10-01 12:22:06 -0600913const char *dev_get_uclass_name(const struct udevice *dev)
Przemyslaw Marczak5ed2e422015-04-15 13:07:25 +0200914{
915 if (!dev)
916 return NULL;
917
918 return dev->uclass->uc_drv->name;
919}
920
Simon Glass2e5e5ec2018-10-01 12:22:06 -0600921bool device_has_children(const struct udevice *dev)
Simon Glass40f829a2015-03-25 12:21:57 -0600922{
923 return !list_empty(&dev->child_head);
924}
925
Simon Glass2a580882020-01-27 08:49:36 -0700926bool device_has_active_children(const struct udevice *dev)
Simon Glass40f829a2015-03-25 12:21:57 -0600927{
928 struct udevice *child;
929
930 for (device_find_first_child(dev, &child);
931 child;
932 device_find_next_child(&child)) {
933 if (device_active(child))
934 return true;
935 }
936
937 return false;
938}
939
Simon Glass2a580882020-01-27 08:49:36 -0700940bool device_is_last_sibling(const struct udevice *dev)
Simon Glass40f829a2015-03-25 12:21:57 -0600941{
942 struct udevice *parent = dev->parent;
943
944 if (!parent)
945 return false;
946 return list_is_last(&dev->sibling_node, &parent->child_head);
947}
Simon Glasse3b23e22015-07-30 13:40:39 -0600948
Simon Glass7760ba22016-05-01 13:52:23 -0600949void device_set_name_alloced(struct udevice *dev)
950{
Simon Glass2d4c7dc2016-07-04 11:58:15 -0600951 dev->flags |= DM_FLAG_NAME_ALLOCED;
Simon Glass7760ba22016-05-01 13:52:23 -0600952}
953
Simon Glasse3b23e22015-07-30 13:40:39 -0600954int device_set_name(struct udevice *dev, const char *name)
955{
956 name = strdup(name);
957 if (!name)
958 return -ENOMEM;
959 dev->name = name;
Simon Glass7760ba22016-05-01 13:52:23 -0600960 device_set_name_alloced(dev);
Simon Glasse3b23e22015-07-30 13:40:39 -0600961
962 return 0;
963}
Mugunthan V N4666bd92016-04-28 15:36:02 +0530964
Marek Vasut7f18c342019-08-31 18:03:28 +0200965#if CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)
Simon Glass2a580882020-01-27 08:49:36 -0700966bool device_is_compatible(const struct udevice *dev, const char *compat)
Mugunthan V N4666bd92016-04-28 15:36:02 +0530967{
Masahiro Yamada9349bcc2018-04-19 12:14:02 +0900968 return ofnode_device_is_compatible(dev_ofnode(dev), compat);
Mugunthan V N4666bd92016-04-28 15:36:02 +0530969}
970
971bool of_machine_is_compatible(const char *compat)
972{
973 const void *fdt = gd->fdt_blob;
974
975 return !fdt_node_check_compatible(fdt, 0, compat);
976}
Mario Six3bedfbe2018-06-26 08:46:50 +0200977
978int dev_disable_by_path(const char *path)
979{
980 struct uclass *uc;
981 ofnode node = ofnode_path(path);
982 struct udevice *dev;
983 int ret = 1;
984
985 if (!of_live_active())
986 return -ENOSYS;
987
988 list_for_each_entry(uc, &gd->uclass_root, sibling_node) {
989 ret = uclass_find_device_by_ofnode(uc->uc_drv->id, node, &dev);
990 if (!ret)
991 break;
992 }
993
994 if (ret)
995 return ret;
996
997 ret = device_remove(dev, DM_REMOVE_NORMAL);
998 if (ret)
999 return ret;
1000
1001 ret = device_unbind(dev);
1002 if (ret)
1003 return ret;
1004
1005 return ofnode_set_enabled(node, false);
1006}
1007
1008int dev_enable_by_path(const char *path)
1009{
1010 ofnode node = ofnode_path(path);
1011 ofnode pnode = ofnode_get_parent(node);
1012 struct udevice *parent;
1013 int ret = 1;
1014
1015 if (!of_live_active())
1016 return -ENOSYS;
1017
1018 ret = device_find_by_ofnode(pnode, &parent);
1019 if (ret)
1020 return ret;
1021
1022 ret = ofnode_set_enabled(node, true);
1023 if (ret)
1024 return ret;
1025
Bin Meng9a9b0742018-10-10 22:06:58 -07001026 return lists_bind_fdt(parent, node, NULL, false);
Mario Six3bedfbe2018-06-26 08:46:50 +02001027}
Marek Vasut7f18c342019-08-31 18:03:28 +02001028#endif