blob: a7408d9c76c61aa451833145d23644888633a9aa [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 */
Jean-Jacques Hiblota5da3002018-12-07 14:50:39 +010085 if (CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)) {
86 if (uc->uc_drv->name && ofnode_valid(node))
Simon Glass322b2902017-05-18 20:09:05 -060087 dev_read_alias_seq(dev, &dev->req_seq);
Thomas Fitzsimmonsf327ecb2019-09-06 07:51:18 -040088#if CONFIG_IS_ENABLED(OF_PRIOR_STAGE)
89 if (dev->req_seq == -1)
90 dev->req_seq =
91 uclass_find_next_free_req_seq(drv->id);
92#endif
Jean-Jacques Hiblota5da3002018-12-07 14:50:39 +010093 } else {
94 dev->req_seq = uclass_find_next_free_req_seq(drv->id);
Simon Glass0ccb0972015-01-25 08:27:05 -070095 }
Simon Glassdb6f0202014-07-23 06:55:12 -060096 }
Simon Glass1bd3deb2015-02-27 22:06:30 -070097
Simon Glassafbf9b82016-07-04 11:58:18 -060098 if (drv->platdata_auto_alloc_size) {
99 bool alloc = !platdata;
100
101 if (CONFIG_IS_ENABLED(OF_PLATDATA)) {
102 if (of_platdata_size) {
103 dev->flags |= DM_FLAG_OF_PLATDATA;
104 if (of_platdata_size <
105 drv->platdata_auto_alloc_size)
106 alloc = true;
107 }
108 }
109 if (alloc) {
110 dev->flags |= DM_FLAG_ALLOC_PDATA;
111 dev->platdata = calloc(1,
112 drv->platdata_auto_alloc_size);
113 if (!dev->platdata) {
114 ret = -ENOMEM;
115 goto fail_alloc1;
116 }
117 if (CONFIG_IS_ENABLED(OF_PLATDATA) && platdata) {
118 memcpy(dev->platdata, platdata,
119 of_platdata_size);
120 }
Simon Glassba750492015-01-25 08:27:00 -0700121 }
122 }
Przemyslaw Marczakd850e672015-04-15 13:07:18 +0200123
124 size = uc->uc_drv->per_device_platdata_auto_alloc_size;
125 if (size) {
126 dev->flags |= DM_FLAG_ALLOC_UCLASS_PDATA;
127 dev->uclass_platdata = calloc(1, size);
128 if (!dev->uclass_platdata) {
129 ret = -ENOMEM;
130 goto fail_alloc2;
131 }
132 }
Simon Glass11b61732015-01-25 08:27:01 -0700133
Przemyslaw Marczakd850e672015-04-15 13:07:18 +0200134 if (parent) {
135 size = parent->driver->per_child_platdata_auto_alloc_size;
Simon Glass57f95402015-01-25 08:27:02 -0700136 if (!size) {
137 size = parent->uclass->uc_drv->
138 per_child_platdata_auto_alloc_size;
139 }
Simon Glass11b61732015-01-25 08:27:01 -0700140 if (size) {
141 dev->flags |= DM_FLAG_ALLOC_PARENT_PDATA;
142 dev->parent_platdata = calloc(1, size);
143 if (!dev->parent_platdata) {
144 ret = -ENOMEM;
Przemyslaw Marczakd850e672015-04-15 13:07:18 +0200145 goto fail_alloc3;
Simon Glass11b61732015-01-25 08:27:01 -0700146 }
147 }
Heinrich Schuchardt5246b372020-02-15 21:38:48 +0100148 /* put dev into parent's successor list */
Simon Glassdd6ab882014-02-26 15:59:18 -0700149 list_add_tail(&dev->sibling_node, &parent->child_head);
Heinrich Schuchardt5246b372020-02-15 21:38:48 +0100150 }
Simon Glassdd6ab882014-02-26 15:59:18 -0700151
152 ret = uclass_bind_device(dev);
153 if (ret)
Simon Glass4ebe01b2015-01-25 08:26:59 -0700154 goto fail_uclass_bind;
Simon Glassdd6ab882014-02-26 15:59:18 -0700155
156 /* if we fail to bind we remove device from successors and free it */
157 if (drv->bind) {
158 ret = drv->bind(dev);
Simon Glass4ebe01b2015-01-25 08:26:59 -0700159 if (ret)
Simon Glassdd6ab882014-02-26 15:59:18 -0700160 goto fail_bind;
Simon Glassdd6ab882014-02-26 15:59:18 -0700161 }
Simon Glassa4a51a02015-01-25 08:27:03 -0700162 if (parent && parent->driver->child_post_bind) {
163 ret = parent->driver->child_post_bind(dev);
164 if (ret)
165 goto fail_child_post_bind;
166 }
Simon Glass286863d2016-01-05 09:30:59 -0700167 if (uc->uc_drv->post_bind) {
168 ret = uc->uc_drv->post_bind(dev);
169 if (ret)
170 goto fail_uclass_post_bind;
171 }
Simon Glassa4a51a02015-01-25 08:27:03 -0700172
Simon Glassdd6ab882014-02-26 15:59:18 -0700173 if (parent)
Masahiro Yamadaf70f39f2017-09-29 12:31:20 +0900174 pr_debug("Bound device %s to %s\n", dev->name, parent->name);
Masahiro Yamadae1cc1f02015-08-27 12:44:28 +0900175 if (devp)
176 *devp = dev;
Simon Glassdd6ab882014-02-26 15:59:18 -0700177
Masahiro Yamadabdbb5dd2015-07-25 21:52:34 +0900178 dev->flags |= DM_FLAG_BOUND;
179
Simon Glassdd6ab882014-02-26 15:59:18 -0700180 return 0;
181
Simon Glass286863d2016-01-05 09:30:59 -0700182fail_uclass_post_bind:
183 /* There is no child unbind() method, so no clean-up required */
Simon Glassa4a51a02015-01-25 08:27:03 -0700184fail_child_post_bind:
Masahiro Yamada04aa00d2015-08-12 07:31:52 +0900185 if (CONFIG_IS_ENABLED(DM_DEVICE_REMOVE)) {
Simon Glasse70a4f42015-02-27 22:06:33 -0700186 if (drv->unbind && drv->unbind(dev)) {
187 dm_warn("unbind() method failed on dev '%s' on error path\n",
188 dev->name);
189 }
Simon Glassa4a51a02015-01-25 08:27:03 -0700190 }
191
Simon Glassdd6ab882014-02-26 15:59:18 -0700192fail_bind:
Masahiro Yamada04aa00d2015-08-12 07:31:52 +0900193 if (CONFIG_IS_ENABLED(DM_DEVICE_REMOVE)) {
Simon Glasse70a4f42015-02-27 22:06:33 -0700194 if (uclass_unbind_device(dev)) {
195 dm_warn("Failed to unbind dev '%s' on error path\n",
196 dev->name);
197 }
Simon Glass4ebe01b2015-01-25 08:26:59 -0700198 }
199fail_uclass_bind:
Masahiro Yamada04aa00d2015-08-12 07:31:52 +0900200 if (CONFIG_IS_ENABLED(DM_DEVICE_REMOVE)) {
Simon Glasse70a4f42015-02-27 22:06:33 -0700201 list_del(&dev->sibling_node);
202 if (dev->flags & DM_FLAG_ALLOC_PARENT_PDATA) {
203 free(dev->parent_platdata);
204 dev->parent_platdata = NULL;
205 }
Simon Glass11b61732015-01-25 08:27:01 -0700206 }
Przemyslaw Marczakd850e672015-04-15 13:07:18 +0200207fail_alloc3:
208 if (dev->flags & DM_FLAG_ALLOC_UCLASS_PDATA) {
209 free(dev->uclass_platdata);
210 dev->uclass_platdata = NULL;
211 }
Simon Glass11b61732015-01-25 08:27:01 -0700212fail_alloc2:
Simon Glassba750492015-01-25 08:27:00 -0700213 if (dev->flags & DM_FLAG_ALLOC_PDATA) {
214 free(dev->platdata);
215 dev->platdata = NULL;
216 }
217fail_alloc1:
Masahiro Yamada8b15b162015-07-25 21:52:35 +0900218 devres_release_all(dev);
219
Simon Glassdd6ab882014-02-26 15:59:18 -0700220 free(dev);
Simon Glass4ebe01b2015-01-25 08:26:59 -0700221
Simon Glassdd6ab882014-02-26 15:59:18 -0700222 return ret;
223}
224
Stephen Warren8c93df12016-05-11 15:26:24 -0600225int device_bind_with_driver_data(struct udevice *parent,
226 const struct driver *drv, const char *name,
Simon Glass322b2902017-05-18 20:09:05 -0600227 ulong driver_data, ofnode node,
Stephen Warren8c93df12016-05-11 15:26:24 -0600228 struct udevice **devp)
229{
Simon Glass322b2902017-05-18 20:09:05 -0600230 return device_bind_common(parent, drv, name, NULL, driver_data, node,
231 0, devp);
Stephen Warren8c93df12016-05-11 15:26:24 -0600232}
233
234int device_bind(struct udevice *parent, const struct driver *drv,
235 const char *name, void *platdata, int of_offset,
236 struct udevice **devp)
237{
Simon Glassb0cf4b32017-05-17 17:18:11 -0600238 return device_bind_common(parent, drv, name, platdata, 0,
239 offset_to_ofnode(of_offset), 0, devp);
Stephen Warren8c93df12016-05-11 15:26:24 -0600240}
241
Simon Glass9eb151e2018-06-11 13:07:15 -0600242int device_bind_ofnode(struct udevice *parent, const struct driver *drv,
243 const char *name, void *platdata, ofnode node,
244 struct udevice **devp)
245{
246 return device_bind_common(parent, drv, name, platdata, 0, node, 0,
247 devp);
248}
249
Simon Glassfef72b72014-07-23 06:55:03 -0600250int device_bind_by_name(struct udevice *parent, bool pre_reloc_only,
251 const struct driver_info *info, struct udevice **devp)
Simon Glassdd6ab882014-02-26 15:59:18 -0700252{
253 struct driver *drv;
Simon Glassafbf9b82016-07-04 11:58:18 -0600254 uint platdata_size = 0;
Simon Glassdd6ab882014-02-26 15:59:18 -0700255
256 drv = lists_driver_lookup_name(info->name);
257 if (!drv)
258 return -ENOENT;
Simon Glassfef72b72014-07-23 06:55:03 -0600259 if (pre_reloc_only && !(drv->flags & DM_FLAG_PRE_RELOC))
260 return -EPERM;
Simon Glassdd6ab882014-02-26 15:59:18 -0700261
Simon Glassafbf9b82016-07-04 11:58:18 -0600262#if CONFIG_IS_ENABLED(OF_PLATDATA)
263 platdata_size = info->platdata_size;
264#endif
265 return device_bind_common(parent, drv, info->name,
Simon Glass322b2902017-05-18 20:09:05 -0600266 (void *)info->platdata, 0, ofnode_null(), platdata_size,
267 devp);
Simon Glassdd6ab882014-02-26 15:59:18 -0700268}
269
Simon Glass825d3f92015-03-25 12:21:53 -0600270static void *alloc_priv(int size, uint flags)
271{
272 void *priv;
273
274 if (flags & DM_FLAG_ALLOC_PRIV_DMA) {
Faiz Abbas03020a92017-09-19 16:53:50 +0530275 size = ROUND(size, ARCH_DMA_MINALIGN);
Simon Glass825d3f92015-03-25 12:21:53 -0600276 priv = memalign(ARCH_DMA_MINALIGN, size);
Simon Glass27bc9b12017-04-04 13:00:19 -0600277 if (priv) {
Simon Glass825d3f92015-03-25 12:21:53 -0600278 memset(priv, '\0', size);
Simon Glass27bc9b12017-04-04 13:00:19 -0600279
280 /*
281 * Ensure that the zero bytes are flushed to memory.
282 * This prevents problems if the driver uses this as
283 * both an input and an output buffer:
284 *
285 * 1. Zeroes written to buffer (here) and sit in the
286 * cache
287 * 2. Driver issues a read command to DMA
288 * 3. CPU runs out of cache space and evicts some cache
289 * data in the buffer, writing zeroes to RAM from
290 * the memset() above
291 * 4. DMA completes
292 * 5. Buffer now has some DMA data and some zeroes
293 * 6. Data being read is now incorrect
294 *
295 * To prevent this, ensure that the cache is clean
296 * within this range at the start. The driver can then
297 * use normal flush-after-write, invalidate-before-read
298 * procedures.
299 *
300 * TODO(sjg@chromium.org): Drop this microblaze
301 * exception.
302 */
303#ifndef CONFIG_MICROBLAZE
304 flush_dcache_range((ulong)priv, (ulong)priv + size);
305#endif
306 }
Simon Glass825d3f92015-03-25 12:21:53 -0600307 } else {
308 priv = calloc(1, size);
309 }
310
311 return priv;
312}
313
Simon Glass4a263852019-12-29 21:19:20 -0700314int device_ofdata_to_platdata(struct udevice *dev)
Simon Glassdd6ab882014-02-26 15:59:18 -0700315{
Simon Glassa626dff2015-03-25 12:21:54 -0600316 const struct driver *drv;
Simon Glassdd6ab882014-02-26 15:59:18 -0700317 int size = 0;
318 int ret;
319
320 if (!dev)
321 return -EINVAL;
322
Simon Glassd3c9d302019-12-29 21:19:21 -0700323 if (dev->flags & DM_FLAG_PLATDATA_VALID)
Simon Glassdd6ab882014-02-26 15:59:18 -0700324 return 0;
325
Simon Glass44c47892020-04-05 15:38:19 -0600326 /* Ensure all parents have ofdata */
327 if (dev->parent) {
328 ret = device_ofdata_to_platdata(dev->parent);
329 if (ret)
330 goto fail;
331
332 /*
333 * The device might have already been probed during
334 * the call to device_probe() on its parent device
335 * (e.g. PCI bridge devices). Test the flags again
336 * so that we don't mess up the device.
337 */
338 if (dev->flags & DM_FLAG_PLATDATA_VALID)
339 return 0;
340 }
341
Simon Glassdd6ab882014-02-26 15:59:18 -0700342 drv = dev->driver;
343 assert(drv);
344
Bin Meng82cdd782015-08-24 01:14:02 -0700345 /* Allocate private data if requested and not reentered */
346 if (drv->priv_auto_alloc_size && !dev->priv) {
Simon Glass825d3f92015-03-25 12:21:53 -0600347 dev->priv = alloc_priv(drv->priv_auto_alloc_size, drv->flags);
Simon Glassdd6ab882014-02-26 15:59:18 -0700348 if (!dev->priv) {
349 ret = -ENOMEM;
350 goto fail;
351 }
352 }
Bin Meng82cdd782015-08-24 01:14:02 -0700353 /* Allocate private data if requested and not reentered */
Simon Glassdd6ab882014-02-26 15:59:18 -0700354 size = dev->uclass->uc_drv->per_device_auto_alloc_size;
Bin Meng82cdd782015-08-24 01:14:02 -0700355 if (size && !dev->uclass_priv) {
Simon Glass7ebd13d2018-10-01 12:22:05 -0600356 dev->uclass_priv = alloc_priv(size,
357 dev->uclass->uc_drv->flags);
Simon Glassdd6ab882014-02-26 15:59:18 -0700358 if (!dev->uclass_priv) {
359 ret = -ENOMEM;
360 goto fail;
361 }
362 }
363
Simon Glassa8d8f162019-12-29 21:19:18 -0700364 /* Allocate parent data for this child */
Simon Glassdd6ab882014-02-26 15:59:18 -0700365 if (dev->parent) {
Simon Glass60d971b2014-07-23 06:55:20 -0600366 size = dev->parent->driver->per_child_auto_alloc_size;
Simon Glassc23b4282015-01-25 08:27:06 -0700367 if (!size) {
368 size = dev->parent->uclass->uc_drv->
369 per_child_auto_alloc_size;
370 }
Bin Meng82cdd782015-08-24 01:14:02 -0700371 if (size && !dev->parent_priv) {
Simon Glass825d3f92015-03-25 12:21:53 -0600372 dev->parent_priv = alloc_priv(size, drv->flags);
Simon Glass60d971b2014-07-23 06:55:20 -0600373 if (!dev->parent_priv) {
374 ret = -ENOMEM;
375 goto fail;
376 }
377 }
Simon Glassa8d8f162019-12-29 21:19:18 -0700378 }
379
380 if (drv->ofdata_to_platdata &&
381 (CONFIG_IS_ENABLED(OF_PLATDATA) || dev_has_of_node(dev))) {
382 ret = drv->ofdata_to_platdata(dev);
383 if (ret)
384 goto fail;
385 }
Simon Glass60d971b2014-07-23 06:55:20 -0600386
Simon Glassd3c9d302019-12-29 21:19:21 -0700387 dev->flags |= DM_FLAG_PLATDATA_VALID;
388
Simon Glass4a263852019-12-29 21:19:20 -0700389 return 0;
390fail:
391 device_free(dev);
392
393 return ret;
394}
395
396int device_probe(struct udevice *dev)
397{
398 const struct driver *drv;
399 int ret;
400 int seq;
401
402 if (!dev)
403 return -EINVAL;
404
405 if (dev->flags & DM_FLAG_ACTIVATED)
406 return 0;
407
408 drv = dev->driver;
409 assert(drv);
410
411 ret = device_ofdata_to_platdata(dev);
412 if (ret)
413 goto fail;
414
Simon Glassa8d8f162019-12-29 21:19:18 -0700415 /* Ensure all parents are probed */
416 if (dev->parent) {
Simon Glassdd6ab882014-02-26 15:59:18 -0700417 ret = device_probe(dev->parent);
418 if (ret)
419 goto fail;
Bin Meng82cdd782015-08-24 01:14:02 -0700420
421 /*
422 * The device might have already been probed during
423 * the call to device_probe() on its parent device
424 * (e.g. PCI bridge devices). Test the flags again
425 * so that we don't mess up the device.
426 */
427 if (dev->flags & DM_FLAG_ACTIVATED)
428 return 0;
Simon Glassdd6ab882014-02-26 15:59:18 -0700429 }
Simon Glassdb6f0202014-07-23 06:55:12 -0600430
431 seq = uclass_resolve_seq(dev);
432 if (seq < 0) {
433 ret = seq;
434 goto fail;
435 }
436 dev->seq = seq;
Simon Glassdd6ab882014-02-26 15:59:18 -0700437
Simon Glass8b04e732015-03-25 12:21:56 -0600438 dev->flags |= DM_FLAG_ACTIVATED;
439
Simon Glassbb53bda2015-09-12 08:45:19 -0600440 /*
441 * Process pinctrl for everything except the root device, and
Simon Glass27ce9622016-01-21 19:43:25 -0700442 * continue regardless of the result of pinctrl. Don't process pinctrl
443 * settings for pinctrl devices since the device may not yet be
444 * probed.
Simon Glassbb53bda2015-09-12 08:45:19 -0600445 */
Simon Glass27ce9622016-01-21 19:43:25 -0700446 if (dev->parent && device_get_uclass_id(dev) != UCLASS_PINCTRL)
Simon Glassbb53bda2015-09-12 08:45:19 -0600447 pinctrl_select_state(dev, "default");
Masahiro Yamadaf8efa632015-08-27 12:44:29 +0900448
Anatolij Gustschin63a34672019-07-14 21:11:01 +0200449 if (CONFIG_IS_ENABLED(POWER_DOMAIN) && dev->parent &&
Lokesh Vutlac10e52b2019-09-27 13:48:12 +0530450 (device_get_uclass_id(dev) != UCLASS_POWER_DOMAIN) &&
451 !(drv->flags & DM_FLAG_DEFAULT_PD_CTRL_OFF)) {
Peng Fand2201112019-09-17 09:29:22 +0000452 ret = dev_power_domain_on(dev);
453 if (ret)
454 goto fail;
Peng Fan6fba32d2018-07-27 10:20:38 +0800455 }
456
Simon Glass9c1f3822015-03-05 12:25:22 -0700457 ret = uclass_pre_probe_device(dev);
Simon Glass5104b982015-01-25 08:27:10 -0700458 if (ret)
459 goto fail;
460
Simon Glassd45560d2014-07-23 06:55:21 -0600461 if (dev->parent && dev->parent->driver->child_pre_probe) {
462 ret = dev->parent->driver->child_pre_probe(dev);
463 if (ret)
464 goto fail;
465 }
466
Bin Meng513cf402019-07-05 09:23:16 -0700467 /* Only handle devices that have a valid ofnode */
468 if (dev_of_valid(dev)) {
469 /*
470 * Process 'assigned-{clocks/clock-parents/clock-rates}'
471 * properties
472 */
Jean-Jacques Hiblot9601f322019-10-22 14:00:06 +0200473 ret = clk_set_defaults(dev, 0);
Bin Meng513cf402019-07-05 09:23:16 -0700474 if (ret)
475 goto fail;
476 }
Philipp Tomsich9cf03b02018-01-08 13:59:18 +0100477
Simon Glassdd6ab882014-02-26 15:59:18 -0700478 if (drv->probe) {
479 ret = drv->probe(dev);
Simon Glass9046dec2019-12-29 21:19:16 -0700480 if (ret)
Simon Glassdd6ab882014-02-26 15:59:18 -0700481 goto fail;
482 }
483
Simon Glassdd6ab882014-02-26 15:59:18 -0700484 ret = uclass_post_probe_device(dev);
Simon Glass8b04e732015-03-25 12:21:56 -0600485 if (ret)
Simon Glassdd6ab882014-02-26 15:59:18 -0700486 goto fail_uclass;
Simon Glassdd6ab882014-02-26 15:59:18 -0700487
Peng Fan7cf58dd2016-03-12 13:17:38 +0800488 if (dev->parent && device_get_uclass_id(dev) == UCLASS_PINCTRL)
489 pinctrl_select_state(dev, "default");
490
Simon Glassdd6ab882014-02-26 15:59:18 -0700491 return 0;
492fail_uclass:
Stefan Roese80b5bc92017-03-20 12:51:48 +0100493 if (device_remove(dev, DM_REMOVE_NORMAL)) {
Simon Glassdd6ab882014-02-26 15:59:18 -0700494 dm_warn("%s: Device '%s' failed to remove on error path\n",
495 __func__, dev->name);
496 }
497fail:
Simon Glass8b04e732015-03-25 12:21:56 -0600498 dev->flags &= ~DM_FLAG_ACTIVATED;
499
Simon Glassdb6f0202014-07-23 06:55:12 -0600500 dev->seq = -1;
Simon Glassdd6ab882014-02-26 15:59:18 -0700501 device_free(dev);
502
503 return ret;
504}
505
Simon Glass2e5e5ec2018-10-01 12:22:06 -0600506void *dev_get_platdata(const struct udevice *dev)
Simon Glassdd6ab882014-02-26 15:59:18 -0700507{
508 if (!dev) {
Simon Glassdf1e0722014-12-10 08:55:56 -0700509 dm_warn("%s: null device\n", __func__);
Simon Glassdd6ab882014-02-26 15:59:18 -0700510 return NULL;
511 }
512
513 return dev->platdata;
514}
515
Simon Glass2e5e5ec2018-10-01 12:22:06 -0600516void *dev_get_parent_platdata(const struct udevice *dev)
Simon Glass11b61732015-01-25 08:27:01 -0700517{
518 if (!dev) {
Simon Glasscdfc5052015-07-06 16:47:40 -0600519 dm_warn("%s: null device\n", __func__);
Simon Glass11b61732015-01-25 08:27:01 -0700520 return NULL;
521 }
522
523 return dev->parent_platdata;
524}
525
Simon Glass2e5e5ec2018-10-01 12:22:06 -0600526void *dev_get_uclass_platdata(const struct udevice *dev)
Przemyslaw Marczakd850e672015-04-15 13:07:18 +0200527{
528 if (!dev) {
Simon Glasscdfc5052015-07-06 16:47:40 -0600529 dm_warn("%s: null device\n", __func__);
Przemyslaw Marczakd850e672015-04-15 13:07:18 +0200530 return NULL;
531 }
532
533 return dev->uclass_platdata;
534}
535
Simon Glass2e5e5ec2018-10-01 12:22:06 -0600536void *dev_get_priv(const struct udevice *dev)
Simon Glassdd6ab882014-02-26 15:59:18 -0700537{
538 if (!dev) {
Simon Glassdf1e0722014-12-10 08:55:56 -0700539 dm_warn("%s: null device\n", __func__);
Simon Glassdd6ab882014-02-26 15:59:18 -0700540 return NULL;
541 }
542
543 return dev->priv;
544}
Simon Glass48d4e292014-07-23 06:55:19 -0600545
Simon Glass2e5e5ec2018-10-01 12:22:06 -0600546void *dev_get_uclass_priv(const struct udevice *dev)
Simon Glassde0977b2015-03-05 12:25:20 -0700547{
548 if (!dev) {
549 dm_warn("%s: null device\n", __func__);
550 return NULL;
551 }
552
553 return dev->uclass_priv;
554}
555
Simon Glass2e5e5ec2018-10-01 12:22:06 -0600556void *dev_get_parent_priv(const struct udevice *dev)
Simon Glass60d971b2014-07-23 06:55:20 -0600557{
558 if (!dev) {
Simon Glassdf1e0722014-12-10 08:55:56 -0700559 dm_warn("%s: null device\n", __func__);
Simon Glass60d971b2014-07-23 06:55:20 -0600560 return NULL;
561 }
562
563 return dev->parent_priv;
564}
565
Simon Glass48d4e292014-07-23 06:55:19 -0600566static int device_get_device_tail(struct udevice *dev, int ret,
567 struct udevice **devp)
568{
569 if (ret)
570 return ret;
571
572 ret = device_probe(dev);
573 if (ret)
574 return ret;
575
576 *devp = dev;
577
578 return 0;
579}
580
Marek Vasut7f18c342019-08-31 18:03:28 +0200581#if CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)
Mario Six3bedfbe2018-06-26 08:46:50 +0200582/**
583 * device_find_by_ofnode() - Return device associated with given ofnode
584 *
585 * The returned device is *not* activated.
586 *
587 * @node: The ofnode for which a associated device should be looked up
588 * @devp: Pointer to structure to hold the found device
589 * Return: 0 if OK, -ve on error
590 */
591static int device_find_by_ofnode(ofnode node, struct udevice **devp)
592{
593 struct uclass *uc;
594 struct udevice *dev;
595 int ret;
596
597 list_for_each_entry(uc, &gd->uclass_root, sibling_node) {
598 ret = uclass_find_device_by_ofnode(uc->uc_drv->id, node,
599 &dev);
600 if (!ret || dev) {
601 *devp = dev;
602 return 0;
603 }
604 }
605
606 return -ENODEV;
607}
Marek Vasut7f18c342019-08-31 18:03:28 +0200608#endif
Mario Six3bedfbe2018-06-26 08:46:50 +0200609
Simon Glass2a580882020-01-27 08:49:36 -0700610int device_get_child(const struct udevice *parent, int index,
611 struct udevice **devp)
Simon Glass48d4e292014-07-23 06:55:19 -0600612{
613 struct udevice *dev;
614
615 list_for_each_entry(dev, &parent->child_head, sibling_node) {
616 if (!index--)
617 return device_get_device_tail(dev, 0, devp);
618 }
619
620 return -ENODEV;
621}
622
Simon Glass2a580882020-01-27 08:49:36 -0700623int device_get_child_count(const struct udevice *parent)
Lokesh Vutlafa45a082019-09-04 16:01:26 +0530624{
625 struct udevice *dev;
626 int count = 0;
627
628 list_for_each_entry(dev, &parent->child_head, sibling_node)
629 count++;
630
631 return count;
632}
633
Simon Glass2a580882020-01-27 08:49:36 -0700634int device_find_child_by_seq(const struct udevice *parent, int seq_or_req_seq,
Simon Glass48d4e292014-07-23 06:55:19 -0600635 bool find_req_seq, struct udevice **devp)
636{
637 struct udevice *dev;
638
639 *devp = NULL;
640 if (seq_or_req_seq == -1)
641 return -ENODEV;
642
643 list_for_each_entry(dev, &parent->child_head, sibling_node) {
644 if ((find_req_seq ? dev->req_seq : dev->seq) ==
645 seq_or_req_seq) {
646 *devp = dev;
647 return 0;
648 }
649 }
650
651 return -ENODEV;
652}
653
Simon Glass2a580882020-01-27 08:49:36 -0700654int device_get_child_by_seq(const struct udevice *parent, int seq,
Simon Glass48d4e292014-07-23 06:55:19 -0600655 struct udevice **devp)
656{
657 struct udevice *dev;
658 int ret;
659
660 *devp = NULL;
661 ret = device_find_child_by_seq(parent, seq, false, &dev);
662 if (ret == -ENODEV) {
663 /*
664 * We didn't find it in probed devices. See if there is one
665 * that will request this seq if probed.
666 */
667 ret = device_find_child_by_seq(parent, seq, true, &dev);
668 }
669 return device_get_device_tail(dev, ret, devp);
670}
671
Simon Glass2a580882020-01-27 08:49:36 -0700672int device_find_child_by_of_offset(const struct udevice *parent, int of_offset,
Simon Glass48d4e292014-07-23 06:55:19 -0600673 struct udevice **devp)
674{
675 struct udevice *dev;
676
677 *devp = NULL;
678
679 list_for_each_entry(dev, &parent->child_head, sibling_node) {
Simon Glassdd79d6e2017-01-17 16:52:55 -0700680 if (dev_of_offset(dev) == of_offset) {
Simon Glass48d4e292014-07-23 06:55:19 -0600681 *devp = dev;
682 return 0;
683 }
684 }
685
686 return -ENODEV;
687}
688
Simon Glass2a580882020-01-27 08:49:36 -0700689int device_get_child_by_of_offset(const struct udevice *parent, int node,
Simon Glass48d4e292014-07-23 06:55:19 -0600690 struct udevice **devp)
691{
692 struct udevice *dev;
693 int ret;
694
695 *devp = NULL;
Simon Glass861bc9f2015-06-23 15:38:38 -0600696 ret = device_find_child_by_of_offset(parent, node, &dev);
Simon Glass48d4e292014-07-23 06:55:19 -0600697 return device_get_device_tail(dev, ret, devp);
698}
Simon Glass44da7352014-10-13 23:41:49 -0600699
Jean-Jacques Hiblota7b0d6a2018-08-09 16:17:44 +0200700static struct udevice *_device_find_global_by_ofnode(struct udevice *parent,
701 ofnode ofnode)
Simon Glassae2efac2015-06-23 15:38:37 -0600702{
703 struct udevice *dev, *found;
704
Jean-Jacques Hiblota7b0d6a2018-08-09 16:17:44 +0200705 if (ofnode_equal(dev_ofnode(parent), ofnode))
Simon Glassae2efac2015-06-23 15:38:37 -0600706 return parent;
707
708 list_for_each_entry(dev, &parent->child_head, sibling_node) {
Jean-Jacques Hiblota7b0d6a2018-08-09 16:17:44 +0200709 found = _device_find_global_by_ofnode(dev, ofnode);
Simon Glassae2efac2015-06-23 15:38:37 -0600710 if (found)
711 return found;
712 }
713
714 return NULL;
715}
716
Jean-Jacques Hiblota7b0d6a2018-08-09 16:17:44 +0200717int device_find_global_by_ofnode(ofnode ofnode, struct udevice **devp)
718{
719 *devp = _device_find_global_by_ofnode(gd->dm_root, ofnode);
720
721 return *devp ? 0 : -ENOENT;
722}
723
724int device_get_global_by_ofnode(ofnode ofnode, struct udevice **devp)
Simon Glassae2efac2015-06-23 15:38:37 -0600725{
726 struct udevice *dev;
727
Jean-Jacques Hiblota7b0d6a2018-08-09 16:17:44 +0200728 dev = _device_find_global_by_ofnode(gd->dm_root, ofnode);
Simon Glassae2efac2015-06-23 15:38:37 -0600729 return device_get_device_tail(dev, dev ? 0 : -ENOENT, devp);
730}
731
Simon Glass2a580882020-01-27 08:49:36 -0700732int device_find_first_child(const struct udevice *parent, struct udevice **devp)
Simon Glass44da7352014-10-13 23:41:49 -0600733{
734 if (list_empty(&parent->child_head)) {
735 *devp = NULL;
736 } else {
737 *devp = list_first_entry(&parent->child_head, struct udevice,
738 sibling_node);
739 }
740
741 return 0;
742}
743
744int device_find_next_child(struct udevice **devp)
745{
746 struct udevice *dev = *devp;
747 struct udevice *parent = dev->parent;
748
749 if (list_is_last(&dev->sibling_node, &parent->child_head)) {
750 *devp = NULL;
751 } else {
752 *devp = list_entry(dev->sibling_node.next, struct udevice,
753 sibling_node);
754 }
755
756 return 0;
757}
Simon Glass70c3a0e2014-11-11 10:46:18 -0700758
Simon Glass2a580882020-01-27 08:49:36 -0700759int device_find_first_inactive_child(const struct udevice *parent,
Simon Glassb775e872018-10-01 12:22:07 -0600760 enum uclass_id uclass_id,
761 struct udevice **devp)
762{
763 struct udevice *dev;
764
765 *devp = NULL;
766 list_for_each_entry(dev, &parent->child_head, sibling_node) {
767 if (!device_active(dev) &&
768 device_get_uclass_id(dev) == uclass_id) {
769 *devp = dev;
770 return 0;
771 }
772 }
773
774 return -ENODEV;
775}
776
Simon Glass2a580882020-01-27 08:49:36 -0700777int device_find_first_child_by_uclass(const struct udevice *parent,
Simon Glassa11341a2018-11-18 08:14:31 -0700778 enum uclass_id uclass_id,
779 struct udevice **devp)
780{
781 struct udevice *dev;
782
783 *devp = NULL;
784 list_for_each_entry(dev, &parent->child_head, sibling_node) {
785 if (device_get_uclass_id(dev) == uclass_id) {
786 *devp = dev;
787 return 0;
788 }
789 }
790
791 return -ENODEV;
792}
793
Simon Glass2a580882020-01-27 08:49:36 -0700794int device_find_child_by_name(const struct udevice *parent, const char *name,
Simon Glassa11341a2018-11-18 08:14:31 -0700795 struct udevice **devp)
796{
797 struct udevice *dev;
798
799 *devp = NULL;
800
801 list_for_each_entry(dev, &parent->child_head, sibling_node) {
802 if (!strcmp(dev->name, name)) {
803 *devp = dev;
804 return 0;
805 }
806 }
807
808 return -ENODEV;
809}
810
Simon Glass76db9b72020-01-27 08:49:48 -0700811int device_first_child_err(struct udevice *parent, struct udevice **devp)
812{
813 struct udevice *dev;
814
815 device_find_first_child(parent, &dev);
816 if (!dev)
817 return -ENODEV;
818
819 return device_get_device_tail(dev, 0, devp);
820}
821
822int device_next_child_err(struct udevice **devp)
823{
824 struct udevice *dev = *devp;
825
826 device_find_next_child(&dev);
827 if (!dev)
828 return -ENODEV;
829
830 return device_get_device_tail(dev, 0, devp);
831}
832
Simon Glass000676b2020-01-27 08:49:47 -0700833int device_first_child_ofdata_err(struct udevice *parent, struct udevice **devp)
834{
835 struct udevice *dev;
836 int ret;
837
838 device_find_first_child(parent, &dev);
839 if (!dev)
840 return -ENODEV;
841
842 ret = device_ofdata_to_platdata(dev);
843 if (ret)
844 return ret;
845
846 *devp = dev;
847
848 return 0;
849}
850
851int device_next_child_ofdata_err(struct udevice **devp)
852{
853 struct udevice *dev = *devp;
854 int ret;
855
856 device_find_next_child(&dev);
857 if (!dev)
858 return -ENODEV;
859
860 ret = device_ofdata_to_platdata(dev);
861 if (ret)
862 return ret;
863
864 *devp = dev;
865
866 return 0;
867}
868
Simon Glass2e5e5ec2018-10-01 12:22:06 -0600869struct udevice *dev_get_parent(const struct udevice *child)
Simon Glass43f488a2014-11-11 10:46:19 -0700870{
871 return child->parent;
872}
873
Simon Glass2e5e5ec2018-10-01 12:22:06 -0600874ulong dev_get_driver_data(const struct udevice *dev)
Simon Glass70c3a0e2014-11-11 10:46:18 -0700875{
Simon Glass46227bd2015-03-25 12:21:55 -0600876 return dev->driver_data;
Simon Glass70c3a0e2014-11-11 10:46:18 -0700877}
Simon Glass98fd5d12015-01-25 08:27:04 -0700878
Simon Glass2e5e5ec2018-10-01 12:22:06 -0600879const void *dev_get_driver_ops(const struct udevice *dev)
Przemyslaw Marczakd3ef0d72015-04-15 13:07:24 +0200880{
881 if (!dev || !dev->driver->ops)
882 return NULL;
883
884 return dev->driver->ops;
885}
886
Simon Glass2e5e5ec2018-10-01 12:22:06 -0600887enum uclass_id device_get_uclass_id(const struct udevice *dev)
Simon Glass98fd5d12015-01-25 08:27:04 -0700888{
889 return dev->uclass->uc_drv->id;
890}
Peng Fan99b72352015-02-10 14:46:32 +0800891
Simon Glass2e5e5ec2018-10-01 12:22:06 -0600892const char *dev_get_uclass_name(const struct udevice *dev)
Przemyslaw Marczak5ed2e422015-04-15 13:07:25 +0200893{
894 if (!dev)
895 return NULL;
896
897 return dev->uclass->uc_drv->name;
898}
899
Simon Glass2e5e5ec2018-10-01 12:22:06 -0600900bool device_has_children(const struct udevice *dev)
Simon Glass40f829a2015-03-25 12:21:57 -0600901{
902 return !list_empty(&dev->child_head);
903}
904
Simon Glass2a580882020-01-27 08:49:36 -0700905bool device_has_active_children(const struct udevice *dev)
Simon Glass40f829a2015-03-25 12:21:57 -0600906{
907 struct udevice *child;
908
909 for (device_find_first_child(dev, &child);
910 child;
911 device_find_next_child(&child)) {
912 if (device_active(child))
913 return true;
914 }
915
916 return false;
917}
918
Simon Glass2a580882020-01-27 08:49:36 -0700919bool device_is_last_sibling(const struct udevice *dev)
Simon Glass40f829a2015-03-25 12:21:57 -0600920{
921 struct udevice *parent = dev->parent;
922
923 if (!parent)
924 return false;
925 return list_is_last(&dev->sibling_node, &parent->child_head);
926}
Simon Glasse3b23e22015-07-30 13:40:39 -0600927
Simon Glass7760ba22016-05-01 13:52:23 -0600928void device_set_name_alloced(struct udevice *dev)
929{
Simon Glass2d4c7dc2016-07-04 11:58:15 -0600930 dev->flags |= DM_FLAG_NAME_ALLOCED;
Simon Glass7760ba22016-05-01 13:52:23 -0600931}
932
Simon Glasse3b23e22015-07-30 13:40:39 -0600933int device_set_name(struct udevice *dev, const char *name)
934{
935 name = strdup(name);
936 if (!name)
937 return -ENOMEM;
938 dev->name = name;
Simon Glass7760ba22016-05-01 13:52:23 -0600939 device_set_name_alloced(dev);
Simon Glasse3b23e22015-07-30 13:40:39 -0600940
941 return 0;
942}
Mugunthan V N4666bd92016-04-28 15:36:02 +0530943
Marek Vasut7f18c342019-08-31 18:03:28 +0200944#if CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)
Simon Glass2a580882020-01-27 08:49:36 -0700945bool device_is_compatible(const struct udevice *dev, const char *compat)
Mugunthan V N4666bd92016-04-28 15:36:02 +0530946{
Masahiro Yamada9349bcc2018-04-19 12:14:02 +0900947 return ofnode_device_is_compatible(dev_ofnode(dev), compat);
Mugunthan V N4666bd92016-04-28 15:36:02 +0530948}
949
950bool of_machine_is_compatible(const char *compat)
951{
952 const void *fdt = gd->fdt_blob;
953
954 return !fdt_node_check_compatible(fdt, 0, compat);
955}
Mario Six3bedfbe2018-06-26 08:46:50 +0200956
957int dev_disable_by_path(const char *path)
958{
959 struct uclass *uc;
960 ofnode node = ofnode_path(path);
961 struct udevice *dev;
962 int ret = 1;
963
964 if (!of_live_active())
965 return -ENOSYS;
966
967 list_for_each_entry(uc, &gd->uclass_root, sibling_node) {
968 ret = uclass_find_device_by_ofnode(uc->uc_drv->id, node, &dev);
969 if (!ret)
970 break;
971 }
972
973 if (ret)
974 return ret;
975
976 ret = device_remove(dev, DM_REMOVE_NORMAL);
977 if (ret)
978 return ret;
979
980 ret = device_unbind(dev);
981 if (ret)
982 return ret;
983
984 return ofnode_set_enabled(node, false);
985}
986
987int dev_enable_by_path(const char *path)
988{
989 ofnode node = ofnode_path(path);
990 ofnode pnode = ofnode_get_parent(node);
991 struct udevice *parent;
992 int ret = 1;
993
994 if (!of_live_active())
995 return -ENOSYS;
996
997 ret = device_find_by_ofnode(pnode, &parent);
998 if (ret)
999 return ret;
1000
1001 ret = ofnode_set_enabled(node, true);
1002 if (ret)
1003 return ret;
1004
Bin Meng9a9b0742018-10-10 22:06:58 -07001005 return lists_bind_fdt(parent, node, NULL, false);
Mario Six3bedfbe2018-06-26 08:46:50 +02001006}
Marek Vasut7f18c342019-08-31 18:03:28 +02001007#endif