blob: 0157bb1fe021225165f8f38bf1fe003b8ba81c5d [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>
Vignesh R0dff3702016-07-06 09:58:55 +053013#include <asm/io.h>
Philipp Tomsich9cf03b02018-01-08 13:59:18 +010014#include <clk.h>
Simon Glassdb6f0202014-07-23 06:55:12 -060015#include <fdtdec.h>
Stefan Roeseadc09052015-09-02 07:41:12 +020016#include <fdt_support.h>
Simon Glassdd6ab882014-02-26 15:59:18 -070017#include <malloc.h>
18#include <dm/device.h>
19#include <dm/device-internal.h>
20#include <dm/lists.h>
Mario Six1f1c1ec2018-01-15 11:07:20 +010021#include <dm/of_access.h>
Masahiro Yamadaf8efa632015-08-27 12:44:29 +090022#include <dm/pinctrl.h>
Simon Glassdd6ab882014-02-26 15:59:18 -070023#include <dm/platdata.h>
Simon Glass322b2902017-05-18 20:09:05 -060024#include <dm/read.h>
Simon Glassdd6ab882014-02-26 15:59:18 -070025#include <dm/uclass.h>
26#include <dm/uclass-internal.h>
27#include <dm/util.h>
28#include <linux/err.h>
29#include <linux/list.h>
Peng Fan6fba32d2018-07-27 10:20:38 +080030#include <power-domain.h>
Simon Glassdd6ab882014-02-26 15:59:18 -070031
Simon Glassdb6f0202014-07-23 06:55:12 -060032DECLARE_GLOBAL_DATA_PTR;
33
Stephen Warren8c93df12016-05-11 15:26:24 -060034static int device_bind_common(struct udevice *parent, const struct driver *drv,
35 const char *name, void *platdata,
Simon Glassb0cf4b32017-05-17 17:18:11 -060036 ulong driver_data, ofnode node,
Simon Glassafbf9b82016-07-04 11:58:18 -060037 uint of_platdata_size, struct udevice **devp)
Simon Glassdd6ab882014-02-26 15:59:18 -070038{
Heiko Schocherb74fcb42014-05-22 12:43:05 +020039 struct udevice *dev;
Simon Glassdd6ab882014-02-26 15:59:18 -070040 struct uclass *uc;
Przemyslaw Marczakd850e672015-04-15 13:07:18 +020041 int size, ret = 0;
Simon Glassdd6ab882014-02-26 15:59:18 -070042
Masahiro Yamadae1cc1f02015-08-27 12:44:28 +090043 if (devp)
44 *devp = NULL;
Simon Glassdd6ab882014-02-26 15:59:18 -070045 if (!name)
46 return -EINVAL;
47
48 ret = uclass_get(drv->id, &uc);
Simon Glass43313de2015-08-30 16:55:16 -060049 if (ret) {
50 debug("Missing uclass for driver %s\n", drv->name);
Simon Glassdd6ab882014-02-26 15:59:18 -070051 return ret;
Simon Glass43313de2015-08-30 16:55:16 -060052 }
Simon Glassdd6ab882014-02-26 15:59:18 -070053
Heiko Schocherb74fcb42014-05-22 12:43:05 +020054 dev = calloc(1, sizeof(struct udevice));
Simon Glassdd6ab882014-02-26 15:59:18 -070055 if (!dev)
56 return -ENOMEM;
57
58 INIT_LIST_HEAD(&dev->sibling_node);
59 INIT_LIST_HEAD(&dev->child_head);
60 INIT_LIST_HEAD(&dev->uclass_node);
Masahiro Yamada029bfca2015-07-25 21:52:37 +090061#ifdef CONFIG_DEVRES
Masahiro Yamada8b15b162015-07-25 21:52:35 +090062 INIT_LIST_HEAD(&dev->devres_head);
Masahiro Yamada029bfca2015-07-25 21:52:37 +090063#endif
Simon Glassdd6ab882014-02-26 15:59:18 -070064 dev->platdata = platdata;
Stephen Warren8c93df12016-05-11 15:26:24 -060065 dev->driver_data = driver_data;
Simon Glassdd6ab882014-02-26 15:59:18 -070066 dev->name = name;
Simon Glassb0cf4b32017-05-17 17:18:11 -060067 dev->node = node;
Simon Glassdd6ab882014-02-26 15:59:18 -070068 dev->parent = parent;
69 dev->driver = drv;
70 dev->uclass = uc;
Simon Glassdb6f0202014-07-23 06:55:12 -060071
Simon Glassdb6f0202014-07-23 06:55:12 -060072 dev->seq = -1;
Simon Glass0ccb0972015-01-25 08:27:05 -070073 dev->req_seq = -1;
Jean-Jacques Hiblota5da3002018-12-07 14:50:39 +010074 if (CONFIG_IS_ENABLED(DM_SEQ_ALIAS) &&
75 (uc->uc_drv->flags & DM_UC_FLAG_SEQ_ALIAS)) {
Simon Glass1bd3deb2015-02-27 22:06:30 -070076 /*
Stefan Roese9d4ce6b2016-03-16 09:58:01 +010077 * Some devices, such as a SPI bus, I2C bus and serial ports
78 * are numbered using aliases.
79 *
80 * This is just a 'requested' sequence, and will be
81 * resolved (and ->seq updated) when the device is probed.
82 */
Jean-Jacques Hiblota5da3002018-12-07 14:50:39 +010083 if (CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)) {
84 if (uc->uc_drv->name && ofnode_valid(node))
Simon Glass322b2902017-05-18 20:09:05 -060085 dev_read_alias_seq(dev, &dev->req_seq);
Thomas Fitzsimmonsf327ecb2019-09-06 07:51:18 -040086#if CONFIG_IS_ENABLED(OF_PRIOR_STAGE)
87 if (dev->req_seq == -1)
88 dev->req_seq =
89 uclass_find_next_free_req_seq(drv->id);
90#endif
Jean-Jacques Hiblota5da3002018-12-07 14:50:39 +010091 } else {
92 dev->req_seq = uclass_find_next_free_req_seq(drv->id);
Simon Glass0ccb0972015-01-25 08:27:05 -070093 }
Simon Glassdb6f0202014-07-23 06:55:12 -060094 }
Simon Glass1bd3deb2015-02-27 22:06:30 -070095
Simon Glassafbf9b82016-07-04 11:58:18 -060096 if (drv->platdata_auto_alloc_size) {
97 bool alloc = !platdata;
98
99 if (CONFIG_IS_ENABLED(OF_PLATDATA)) {
100 if (of_platdata_size) {
101 dev->flags |= DM_FLAG_OF_PLATDATA;
102 if (of_platdata_size <
103 drv->platdata_auto_alloc_size)
104 alloc = true;
105 }
106 }
107 if (alloc) {
108 dev->flags |= DM_FLAG_ALLOC_PDATA;
109 dev->platdata = calloc(1,
110 drv->platdata_auto_alloc_size);
111 if (!dev->platdata) {
112 ret = -ENOMEM;
113 goto fail_alloc1;
114 }
115 if (CONFIG_IS_ENABLED(OF_PLATDATA) && platdata) {
116 memcpy(dev->platdata, platdata,
117 of_platdata_size);
118 }
Simon Glassba750492015-01-25 08:27:00 -0700119 }
120 }
Przemyslaw Marczakd850e672015-04-15 13:07:18 +0200121
122 size = uc->uc_drv->per_device_platdata_auto_alloc_size;
123 if (size) {
124 dev->flags |= DM_FLAG_ALLOC_UCLASS_PDATA;
125 dev->uclass_platdata = calloc(1, size);
126 if (!dev->uclass_platdata) {
127 ret = -ENOMEM;
128 goto fail_alloc2;
129 }
130 }
Simon Glass11b61732015-01-25 08:27:01 -0700131
Przemyslaw Marczakd850e672015-04-15 13:07:18 +0200132 if (parent) {
133 size = parent->driver->per_child_platdata_auto_alloc_size;
Simon Glass57f95402015-01-25 08:27:02 -0700134 if (!size) {
135 size = parent->uclass->uc_drv->
136 per_child_platdata_auto_alloc_size;
137 }
Simon Glass11b61732015-01-25 08:27:01 -0700138 if (size) {
139 dev->flags |= DM_FLAG_ALLOC_PARENT_PDATA;
140 dev->parent_platdata = calloc(1, size);
141 if (!dev->parent_platdata) {
142 ret = -ENOMEM;
Przemyslaw Marczakd850e672015-04-15 13:07:18 +0200143 goto fail_alloc3;
Simon Glass11b61732015-01-25 08:27:01 -0700144 }
145 }
Heinrich Schuchardt5246b372020-02-15 21:38:48 +0100146 /* put dev into parent's successor list */
Simon Glassdd6ab882014-02-26 15:59:18 -0700147 list_add_tail(&dev->sibling_node, &parent->child_head);
Heinrich Schuchardt5246b372020-02-15 21:38:48 +0100148 }
Simon Glassdd6ab882014-02-26 15:59:18 -0700149
150 ret = uclass_bind_device(dev);
151 if (ret)
Simon Glass4ebe01b2015-01-25 08:26:59 -0700152 goto fail_uclass_bind;
Simon Glassdd6ab882014-02-26 15:59:18 -0700153
154 /* if we fail to bind we remove device from successors and free it */
155 if (drv->bind) {
156 ret = drv->bind(dev);
Simon Glass4ebe01b2015-01-25 08:26:59 -0700157 if (ret)
Simon Glassdd6ab882014-02-26 15:59:18 -0700158 goto fail_bind;
Simon Glassdd6ab882014-02-26 15:59:18 -0700159 }
Simon Glassa4a51a02015-01-25 08:27:03 -0700160 if (parent && parent->driver->child_post_bind) {
161 ret = parent->driver->child_post_bind(dev);
162 if (ret)
163 goto fail_child_post_bind;
164 }
Simon Glass286863d2016-01-05 09:30:59 -0700165 if (uc->uc_drv->post_bind) {
166 ret = uc->uc_drv->post_bind(dev);
167 if (ret)
168 goto fail_uclass_post_bind;
169 }
Simon Glassa4a51a02015-01-25 08:27:03 -0700170
Simon Glassdd6ab882014-02-26 15:59:18 -0700171 if (parent)
Masahiro Yamadaf70f39f2017-09-29 12:31:20 +0900172 pr_debug("Bound device %s to %s\n", dev->name, parent->name);
Masahiro Yamadae1cc1f02015-08-27 12:44:28 +0900173 if (devp)
174 *devp = dev;
Simon Glassdd6ab882014-02-26 15:59:18 -0700175
Masahiro Yamadabdbb5dd2015-07-25 21:52:34 +0900176 dev->flags |= DM_FLAG_BOUND;
177
Simon Glassdd6ab882014-02-26 15:59:18 -0700178 return 0;
179
Simon Glass286863d2016-01-05 09:30:59 -0700180fail_uclass_post_bind:
181 /* There is no child unbind() method, so no clean-up required */
Simon Glassa4a51a02015-01-25 08:27:03 -0700182fail_child_post_bind:
Masahiro Yamada04aa00d2015-08-12 07:31:52 +0900183 if (CONFIG_IS_ENABLED(DM_DEVICE_REMOVE)) {
Simon Glasse70a4f42015-02-27 22:06:33 -0700184 if (drv->unbind && drv->unbind(dev)) {
185 dm_warn("unbind() method failed on dev '%s' on error path\n",
186 dev->name);
187 }
Simon Glassa4a51a02015-01-25 08:27:03 -0700188 }
189
Simon Glassdd6ab882014-02-26 15:59:18 -0700190fail_bind:
Masahiro Yamada04aa00d2015-08-12 07:31:52 +0900191 if (CONFIG_IS_ENABLED(DM_DEVICE_REMOVE)) {
Simon Glasse70a4f42015-02-27 22:06:33 -0700192 if (uclass_unbind_device(dev)) {
193 dm_warn("Failed to unbind dev '%s' on error path\n",
194 dev->name);
195 }
Simon Glass4ebe01b2015-01-25 08:26:59 -0700196 }
197fail_uclass_bind:
Masahiro Yamada04aa00d2015-08-12 07:31:52 +0900198 if (CONFIG_IS_ENABLED(DM_DEVICE_REMOVE)) {
Simon Glasse70a4f42015-02-27 22:06:33 -0700199 list_del(&dev->sibling_node);
200 if (dev->flags & DM_FLAG_ALLOC_PARENT_PDATA) {
201 free(dev->parent_platdata);
202 dev->parent_platdata = NULL;
203 }
Simon Glass11b61732015-01-25 08:27:01 -0700204 }
Przemyslaw Marczakd850e672015-04-15 13:07:18 +0200205fail_alloc3:
206 if (dev->flags & DM_FLAG_ALLOC_UCLASS_PDATA) {
207 free(dev->uclass_platdata);
208 dev->uclass_platdata = NULL;
209 }
Simon Glass11b61732015-01-25 08:27:01 -0700210fail_alloc2:
Simon Glassba750492015-01-25 08:27:00 -0700211 if (dev->flags & DM_FLAG_ALLOC_PDATA) {
212 free(dev->platdata);
213 dev->platdata = NULL;
214 }
215fail_alloc1:
Masahiro Yamada8b15b162015-07-25 21:52:35 +0900216 devres_release_all(dev);
217
Simon Glassdd6ab882014-02-26 15:59:18 -0700218 free(dev);
Simon Glass4ebe01b2015-01-25 08:26:59 -0700219
Simon Glassdd6ab882014-02-26 15:59:18 -0700220 return ret;
221}
222
Stephen Warren8c93df12016-05-11 15:26:24 -0600223int device_bind_with_driver_data(struct udevice *parent,
224 const struct driver *drv, const char *name,
Simon Glass322b2902017-05-18 20:09:05 -0600225 ulong driver_data, ofnode node,
Stephen Warren8c93df12016-05-11 15:26:24 -0600226 struct udevice **devp)
227{
Simon Glass322b2902017-05-18 20:09:05 -0600228 return device_bind_common(parent, drv, name, NULL, driver_data, node,
229 0, devp);
Stephen Warren8c93df12016-05-11 15:26:24 -0600230}
231
232int device_bind(struct udevice *parent, const struct driver *drv,
233 const char *name, void *platdata, int of_offset,
234 struct udevice **devp)
235{
Simon Glassb0cf4b32017-05-17 17:18:11 -0600236 return device_bind_common(parent, drv, name, platdata, 0,
237 offset_to_ofnode(of_offset), 0, devp);
Stephen Warren8c93df12016-05-11 15:26:24 -0600238}
239
Simon Glass9eb151e2018-06-11 13:07:15 -0600240int device_bind_ofnode(struct udevice *parent, const struct driver *drv,
241 const char *name, void *platdata, ofnode node,
242 struct udevice **devp)
243{
244 return device_bind_common(parent, drv, name, platdata, 0, node, 0,
245 devp);
246}
247
Simon Glassfef72b72014-07-23 06:55:03 -0600248int device_bind_by_name(struct udevice *parent, bool pre_reloc_only,
249 const struct driver_info *info, struct udevice **devp)
Simon Glassdd6ab882014-02-26 15:59:18 -0700250{
251 struct driver *drv;
Simon Glassafbf9b82016-07-04 11:58:18 -0600252 uint platdata_size = 0;
Simon Glassdd6ab882014-02-26 15:59:18 -0700253
254 drv = lists_driver_lookup_name(info->name);
255 if (!drv)
256 return -ENOENT;
Simon Glassfef72b72014-07-23 06:55:03 -0600257 if (pre_reloc_only && !(drv->flags & DM_FLAG_PRE_RELOC))
258 return -EPERM;
Simon Glassdd6ab882014-02-26 15:59:18 -0700259
Simon Glassafbf9b82016-07-04 11:58:18 -0600260#if CONFIG_IS_ENABLED(OF_PLATDATA)
261 platdata_size = info->platdata_size;
262#endif
263 return device_bind_common(parent, drv, info->name,
Simon Glass322b2902017-05-18 20:09:05 -0600264 (void *)info->platdata, 0, ofnode_null(), platdata_size,
265 devp);
Simon Glassdd6ab882014-02-26 15:59:18 -0700266}
267
Simon Glass825d3f92015-03-25 12:21:53 -0600268static void *alloc_priv(int size, uint flags)
269{
270 void *priv;
271
272 if (flags & DM_FLAG_ALLOC_PRIV_DMA) {
Faiz Abbas03020a92017-09-19 16:53:50 +0530273 size = ROUND(size, ARCH_DMA_MINALIGN);
Simon Glass825d3f92015-03-25 12:21:53 -0600274 priv = memalign(ARCH_DMA_MINALIGN, size);
Simon Glass27bc9b12017-04-04 13:00:19 -0600275 if (priv) {
Simon Glass825d3f92015-03-25 12:21:53 -0600276 memset(priv, '\0', size);
Simon Glass27bc9b12017-04-04 13:00:19 -0600277
278 /*
279 * Ensure that the zero bytes are flushed to memory.
280 * This prevents problems if the driver uses this as
281 * both an input and an output buffer:
282 *
283 * 1. Zeroes written to buffer (here) and sit in the
284 * cache
285 * 2. Driver issues a read command to DMA
286 * 3. CPU runs out of cache space and evicts some cache
287 * data in the buffer, writing zeroes to RAM from
288 * the memset() above
289 * 4. DMA completes
290 * 5. Buffer now has some DMA data and some zeroes
291 * 6. Data being read is now incorrect
292 *
293 * To prevent this, ensure that the cache is clean
294 * within this range at the start. The driver can then
295 * use normal flush-after-write, invalidate-before-read
296 * procedures.
297 *
298 * TODO(sjg@chromium.org): Drop this microblaze
299 * exception.
300 */
301#ifndef CONFIG_MICROBLAZE
302 flush_dcache_range((ulong)priv, (ulong)priv + size);
303#endif
304 }
Simon Glass825d3f92015-03-25 12:21:53 -0600305 } else {
306 priv = calloc(1, size);
307 }
308
309 return priv;
310}
311
Simon Glass4a263852019-12-29 21:19:20 -0700312int device_ofdata_to_platdata(struct udevice *dev)
Simon Glassdd6ab882014-02-26 15:59:18 -0700313{
Simon Glassa626dff2015-03-25 12:21:54 -0600314 const struct driver *drv;
Simon Glassdd6ab882014-02-26 15:59:18 -0700315 int size = 0;
316 int ret;
317
318 if (!dev)
319 return -EINVAL;
320
Simon Glassd3c9d302019-12-29 21:19:21 -0700321 if (dev->flags & DM_FLAG_PLATDATA_VALID)
Simon Glassdd6ab882014-02-26 15:59:18 -0700322 return 0;
323
Simon Glass44c47892020-04-05 15:38:19 -0600324 /* Ensure all parents have ofdata */
325 if (dev->parent) {
326 ret = device_ofdata_to_platdata(dev->parent);
327 if (ret)
328 goto fail;
329
330 /*
331 * The device might have already been probed during
332 * the call to device_probe() on its parent device
333 * (e.g. PCI bridge devices). Test the flags again
334 * so that we don't mess up the device.
335 */
336 if (dev->flags & DM_FLAG_PLATDATA_VALID)
337 return 0;
338 }
339
Simon Glassdd6ab882014-02-26 15:59:18 -0700340 drv = dev->driver;
341 assert(drv);
342
Bin Meng82cdd782015-08-24 01:14:02 -0700343 /* Allocate private data if requested and not reentered */
344 if (drv->priv_auto_alloc_size && !dev->priv) {
Simon Glass825d3f92015-03-25 12:21:53 -0600345 dev->priv = alloc_priv(drv->priv_auto_alloc_size, drv->flags);
Simon Glassdd6ab882014-02-26 15:59:18 -0700346 if (!dev->priv) {
347 ret = -ENOMEM;
348 goto fail;
349 }
350 }
Bin Meng82cdd782015-08-24 01:14:02 -0700351 /* Allocate private data if requested and not reentered */
Simon Glassdd6ab882014-02-26 15:59:18 -0700352 size = dev->uclass->uc_drv->per_device_auto_alloc_size;
Bin Meng82cdd782015-08-24 01:14:02 -0700353 if (size && !dev->uclass_priv) {
Simon Glass7ebd13d2018-10-01 12:22:05 -0600354 dev->uclass_priv = alloc_priv(size,
355 dev->uclass->uc_drv->flags);
Simon Glassdd6ab882014-02-26 15:59:18 -0700356 if (!dev->uclass_priv) {
357 ret = -ENOMEM;
358 goto fail;
359 }
360 }
361
Simon Glassa8d8f162019-12-29 21:19:18 -0700362 /* Allocate parent data for this child */
Simon Glassdd6ab882014-02-26 15:59:18 -0700363 if (dev->parent) {
Simon Glass60d971b2014-07-23 06:55:20 -0600364 size = dev->parent->driver->per_child_auto_alloc_size;
Simon Glassc23b4282015-01-25 08:27:06 -0700365 if (!size) {
366 size = dev->parent->uclass->uc_drv->
367 per_child_auto_alloc_size;
368 }
Bin Meng82cdd782015-08-24 01:14:02 -0700369 if (size && !dev->parent_priv) {
Simon Glass825d3f92015-03-25 12:21:53 -0600370 dev->parent_priv = alloc_priv(size, drv->flags);
Simon Glass60d971b2014-07-23 06:55:20 -0600371 if (!dev->parent_priv) {
372 ret = -ENOMEM;
373 goto fail;
374 }
375 }
Simon Glassa8d8f162019-12-29 21:19:18 -0700376 }
377
378 if (drv->ofdata_to_platdata &&
379 (CONFIG_IS_ENABLED(OF_PLATDATA) || dev_has_of_node(dev))) {
380 ret = drv->ofdata_to_platdata(dev);
381 if (ret)
382 goto fail;
383 }
Simon Glass60d971b2014-07-23 06:55:20 -0600384
Simon Glassd3c9d302019-12-29 21:19:21 -0700385 dev->flags |= DM_FLAG_PLATDATA_VALID;
386
Simon Glass4a263852019-12-29 21:19:20 -0700387 return 0;
388fail:
389 device_free(dev);
390
391 return ret;
392}
393
394int device_probe(struct udevice *dev)
395{
396 const struct driver *drv;
397 int ret;
398 int seq;
399
400 if (!dev)
401 return -EINVAL;
402
403 if (dev->flags & DM_FLAG_ACTIVATED)
404 return 0;
405
406 drv = dev->driver;
407 assert(drv);
408
409 ret = device_ofdata_to_platdata(dev);
410 if (ret)
411 goto fail;
412
Simon Glassa8d8f162019-12-29 21:19:18 -0700413 /* Ensure all parents are probed */
414 if (dev->parent) {
Simon Glassdd6ab882014-02-26 15:59:18 -0700415 ret = device_probe(dev->parent);
416 if (ret)
417 goto fail;
Bin Meng82cdd782015-08-24 01:14:02 -0700418
419 /*
420 * The device might have already been probed during
421 * the call to device_probe() on its parent device
422 * (e.g. PCI bridge devices). Test the flags again
423 * so that we don't mess up the device.
424 */
425 if (dev->flags & DM_FLAG_ACTIVATED)
426 return 0;
Simon Glassdd6ab882014-02-26 15:59:18 -0700427 }
Simon Glassdb6f0202014-07-23 06:55:12 -0600428
429 seq = uclass_resolve_seq(dev);
430 if (seq < 0) {
431 ret = seq;
432 goto fail;
433 }
434 dev->seq = seq;
Simon Glassdd6ab882014-02-26 15:59:18 -0700435
Simon Glass8b04e732015-03-25 12:21:56 -0600436 dev->flags |= DM_FLAG_ACTIVATED;
437
Simon Glassbb53bda2015-09-12 08:45:19 -0600438 /*
439 * Process pinctrl for everything except the root device, and
Simon Glass27ce9622016-01-21 19:43:25 -0700440 * continue regardless of the result of pinctrl. Don't process pinctrl
441 * settings for pinctrl devices since the device may not yet be
442 * probed.
Simon Glassbb53bda2015-09-12 08:45:19 -0600443 */
Simon Glass27ce9622016-01-21 19:43:25 -0700444 if (dev->parent && device_get_uclass_id(dev) != UCLASS_PINCTRL)
Simon Glassbb53bda2015-09-12 08:45:19 -0600445 pinctrl_select_state(dev, "default");
Masahiro Yamadaf8efa632015-08-27 12:44:29 +0900446
Anatolij Gustschin63a34672019-07-14 21:11:01 +0200447 if (CONFIG_IS_ENABLED(POWER_DOMAIN) && dev->parent &&
Lokesh Vutlac10e52b2019-09-27 13:48:12 +0530448 (device_get_uclass_id(dev) != UCLASS_POWER_DOMAIN) &&
449 !(drv->flags & DM_FLAG_DEFAULT_PD_CTRL_OFF)) {
Peng Fand2201112019-09-17 09:29:22 +0000450 ret = dev_power_domain_on(dev);
451 if (ret)
452 goto fail;
Peng Fan6fba32d2018-07-27 10:20:38 +0800453 }
454
Simon Glass9c1f3822015-03-05 12:25:22 -0700455 ret = uclass_pre_probe_device(dev);
Simon Glass5104b982015-01-25 08:27:10 -0700456 if (ret)
457 goto fail;
458
Simon Glassd45560d2014-07-23 06:55:21 -0600459 if (dev->parent && dev->parent->driver->child_pre_probe) {
460 ret = dev->parent->driver->child_pre_probe(dev);
461 if (ret)
462 goto fail;
463 }
464
Bin Meng513cf402019-07-05 09:23:16 -0700465 /* Only handle devices that have a valid ofnode */
466 if (dev_of_valid(dev)) {
467 /*
468 * Process 'assigned-{clocks/clock-parents/clock-rates}'
469 * properties
470 */
Jean-Jacques Hiblot9601f322019-10-22 14:00:06 +0200471 ret = clk_set_defaults(dev, 0);
Bin Meng513cf402019-07-05 09:23:16 -0700472 if (ret)
473 goto fail;
474 }
Philipp Tomsich9cf03b02018-01-08 13:59:18 +0100475
Simon Glassdd6ab882014-02-26 15:59:18 -0700476 if (drv->probe) {
477 ret = drv->probe(dev);
Simon Glass9046dec2019-12-29 21:19:16 -0700478 if (ret)
Simon Glassdd6ab882014-02-26 15:59:18 -0700479 goto fail;
480 }
481
Simon Glassdd6ab882014-02-26 15:59:18 -0700482 ret = uclass_post_probe_device(dev);
Simon Glass8b04e732015-03-25 12:21:56 -0600483 if (ret)
Simon Glassdd6ab882014-02-26 15:59:18 -0700484 goto fail_uclass;
Simon Glassdd6ab882014-02-26 15:59:18 -0700485
Peng Fan7cf58dd2016-03-12 13:17:38 +0800486 if (dev->parent && device_get_uclass_id(dev) == UCLASS_PINCTRL)
487 pinctrl_select_state(dev, "default");
488
Simon Glassdd6ab882014-02-26 15:59:18 -0700489 return 0;
490fail_uclass:
Stefan Roese80b5bc92017-03-20 12:51:48 +0100491 if (device_remove(dev, DM_REMOVE_NORMAL)) {
Simon Glassdd6ab882014-02-26 15:59:18 -0700492 dm_warn("%s: Device '%s' failed to remove on error path\n",
493 __func__, dev->name);
494 }
495fail:
Simon Glass8b04e732015-03-25 12:21:56 -0600496 dev->flags &= ~DM_FLAG_ACTIVATED;
497
Simon Glassdb6f0202014-07-23 06:55:12 -0600498 dev->seq = -1;
Simon Glassdd6ab882014-02-26 15:59:18 -0700499 device_free(dev);
500
501 return ret;
502}
503
Simon Glass2e5e5ec2018-10-01 12:22:06 -0600504void *dev_get_platdata(const struct udevice *dev)
Simon Glassdd6ab882014-02-26 15:59:18 -0700505{
506 if (!dev) {
Simon Glassdf1e0722014-12-10 08:55:56 -0700507 dm_warn("%s: null device\n", __func__);
Simon Glassdd6ab882014-02-26 15:59:18 -0700508 return NULL;
509 }
510
511 return dev->platdata;
512}
513
Simon Glass2e5e5ec2018-10-01 12:22:06 -0600514void *dev_get_parent_platdata(const struct udevice *dev)
Simon Glass11b61732015-01-25 08:27:01 -0700515{
516 if (!dev) {
Simon Glasscdfc5052015-07-06 16:47:40 -0600517 dm_warn("%s: null device\n", __func__);
Simon Glass11b61732015-01-25 08:27:01 -0700518 return NULL;
519 }
520
521 return dev->parent_platdata;
522}
523
Simon Glass2e5e5ec2018-10-01 12:22:06 -0600524void *dev_get_uclass_platdata(const struct udevice *dev)
Przemyslaw Marczakd850e672015-04-15 13:07:18 +0200525{
526 if (!dev) {
Simon Glasscdfc5052015-07-06 16:47:40 -0600527 dm_warn("%s: null device\n", __func__);
Przemyslaw Marczakd850e672015-04-15 13:07:18 +0200528 return NULL;
529 }
530
531 return dev->uclass_platdata;
532}
533
Simon Glass2e5e5ec2018-10-01 12:22:06 -0600534void *dev_get_priv(const struct udevice *dev)
Simon Glassdd6ab882014-02-26 15:59:18 -0700535{
536 if (!dev) {
Simon Glassdf1e0722014-12-10 08:55:56 -0700537 dm_warn("%s: null device\n", __func__);
Simon Glassdd6ab882014-02-26 15:59:18 -0700538 return NULL;
539 }
540
541 return dev->priv;
542}
Simon Glass48d4e292014-07-23 06:55:19 -0600543
Simon Glass2e5e5ec2018-10-01 12:22:06 -0600544void *dev_get_uclass_priv(const struct udevice *dev)
Simon Glassde0977b2015-03-05 12:25:20 -0700545{
546 if (!dev) {
547 dm_warn("%s: null device\n", __func__);
548 return NULL;
549 }
550
551 return dev->uclass_priv;
552}
553
Simon Glass2e5e5ec2018-10-01 12:22:06 -0600554void *dev_get_parent_priv(const struct udevice *dev)
Simon Glass60d971b2014-07-23 06:55:20 -0600555{
556 if (!dev) {
Simon Glassdf1e0722014-12-10 08:55:56 -0700557 dm_warn("%s: null device\n", __func__);
Simon Glass60d971b2014-07-23 06:55:20 -0600558 return NULL;
559 }
560
561 return dev->parent_priv;
562}
563
Simon Glass48d4e292014-07-23 06:55:19 -0600564static int device_get_device_tail(struct udevice *dev, int ret,
565 struct udevice **devp)
566{
567 if (ret)
568 return ret;
569
570 ret = device_probe(dev);
571 if (ret)
572 return ret;
573
574 *devp = dev;
575
576 return 0;
577}
578
Marek Vasut7f18c342019-08-31 18:03:28 +0200579#if CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)
Mario Six3bedfbe2018-06-26 08:46:50 +0200580/**
581 * device_find_by_ofnode() - Return device associated with given ofnode
582 *
583 * The returned device is *not* activated.
584 *
585 * @node: The ofnode for which a associated device should be looked up
586 * @devp: Pointer to structure to hold the found device
587 * Return: 0 if OK, -ve on error
588 */
589static int device_find_by_ofnode(ofnode node, struct udevice **devp)
590{
591 struct uclass *uc;
592 struct udevice *dev;
593 int ret;
594
595 list_for_each_entry(uc, &gd->uclass_root, sibling_node) {
596 ret = uclass_find_device_by_ofnode(uc->uc_drv->id, node,
597 &dev);
598 if (!ret || dev) {
599 *devp = dev;
600 return 0;
601 }
602 }
603
604 return -ENODEV;
605}
Marek Vasut7f18c342019-08-31 18:03:28 +0200606#endif
Mario Six3bedfbe2018-06-26 08:46:50 +0200607
Simon Glass2a580882020-01-27 08:49:36 -0700608int device_get_child(const struct udevice *parent, int index,
609 struct udevice **devp)
Simon Glass48d4e292014-07-23 06:55:19 -0600610{
611 struct udevice *dev;
612
613 list_for_each_entry(dev, &parent->child_head, sibling_node) {
614 if (!index--)
615 return device_get_device_tail(dev, 0, devp);
616 }
617
618 return -ENODEV;
619}
620
Simon Glass2a580882020-01-27 08:49:36 -0700621int device_get_child_count(const struct udevice *parent)
Lokesh Vutlafa45a082019-09-04 16:01:26 +0530622{
623 struct udevice *dev;
624 int count = 0;
625
626 list_for_each_entry(dev, &parent->child_head, sibling_node)
627 count++;
628
629 return count;
630}
631
Simon Glass2a580882020-01-27 08:49:36 -0700632int device_find_child_by_seq(const struct udevice *parent, int seq_or_req_seq,
Simon Glass48d4e292014-07-23 06:55:19 -0600633 bool find_req_seq, struct udevice **devp)
634{
635 struct udevice *dev;
636
637 *devp = NULL;
638 if (seq_or_req_seq == -1)
639 return -ENODEV;
640
641 list_for_each_entry(dev, &parent->child_head, sibling_node) {
642 if ((find_req_seq ? dev->req_seq : dev->seq) ==
643 seq_or_req_seq) {
644 *devp = dev;
645 return 0;
646 }
647 }
648
649 return -ENODEV;
650}
651
Simon Glass2a580882020-01-27 08:49:36 -0700652int device_get_child_by_seq(const struct udevice *parent, int seq,
Simon Glass48d4e292014-07-23 06:55:19 -0600653 struct udevice **devp)
654{
655 struct udevice *dev;
656 int ret;
657
658 *devp = NULL;
659 ret = device_find_child_by_seq(parent, seq, false, &dev);
660 if (ret == -ENODEV) {
661 /*
662 * We didn't find it in probed devices. See if there is one
663 * that will request this seq if probed.
664 */
665 ret = device_find_child_by_seq(parent, seq, true, &dev);
666 }
667 return device_get_device_tail(dev, ret, devp);
668}
669
Simon Glass2a580882020-01-27 08:49:36 -0700670int device_find_child_by_of_offset(const struct udevice *parent, int of_offset,
Simon Glass48d4e292014-07-23 06:55:19 -0600671 struct udevice **devp)
672{
673 struct udevice *dev;
674
675 *devp = NULL;
676
677 list_for_each_entry(dev, &parent->child_head, sibling_node) {
Simon Glassdd79d6e2017-01-17 16:52:55 -0700678 if (dev_of_offset(dev) == of_offset) {
Simon Glass48d4e292014-07-23 06:55:19 -0600679 *devp = dev;
680 return 0;
681 }
682 }
683
684 return -ENODEV;
685}
686
Simon Glass2a580882020-01-27 08:49:36 -0700687int device_get_child_by_of_offset(const struct udevice *parent, int node,
Simon Glass48d4e292014-07-23 06:55:19 -0600688 struct udevice **devp)
689{
690 struct udevice *dev;
691 int ret;
692
693 *devp = NULL;
Simon Glass861bc9f2015-06-23 15:38:38 -0600694 ret = device_find_child_by_of_offset(parent, node, &dev);
Simon Glass48d4e292014-07-23 06:55:19 -0600695 return device_get_device_tail(dev, ret, devp);
696}
Simon Glass44da7352014-10-13 23:41:49 -0600697
Jean-Jacques Hiblota7b0d6a2018-08-09 16:17:44 +0200698static struct udevice *_device_find_global_by_ofnode(struct udevice *parent,
699 ofnode ofnode)
Simon Glassae2efac2015-06-23 15:38:37 -0600700{
701 struct udevice *dev, *found;
702
Jean-Jacques Hiblota7b0d6a2018-08-09 16:17:44 +0200703 if (ofnode_equal(dev_ofnode(parent), ofnode))
Simon Glassae2efac2015-06-23 15:38:37 -0600704 return parent;
705
706 list_for_each_entry(dev, &parent->child_head, sibling_node) {
Jean-Jacques Hiblota7b0d6a2018-08-09 16:17:44 +0200707 found = _device_find_global_by_ofnode(dev, ofnode);
Simon Glassae2efac2015-06-23 15:38:37 -0600708 if (found)
709 return found;
710 }
711
712 return NULL;
713}
714
Jean-Jacques Hiblota7b0d6a2018-08-09 16:17:44 +0200715int device_find_global_by_ofnode(ofnode ofnode, struct udevice **devp)
716{
717 *devp = _device_find_global_by_ofnode(gd->dm_root, ofnode);
718
719 return *devp ? 0 : -ENOENT;
720}
721
722int device_get_global_by_ofnode(ofnode ofnode, struct udevice **devp)
Simon Glassae2efac2015-06-23 15:38:37 -0600723{
724 struct udevice *dev;
725
Jean-Jacques Hiblota7b0d6a2018-08-09 16:17:44 +0200726 dev = _device_find_global_by_ofnode(gd->dm_root, ofnode);
Simon Glassae2efac2015-06-23 15:38:37 -0600727 return device_get_device_tail(dev, dev ? 0 : -ENOENT, devp);
728}
729
Simon Glass2a580882020-01-27 08:49:36 -0700730int device_find_first_child(const struct udevice *parent, struct udevice **devp)
Simon Glass44da7352014-10-13 23:41:49 -0600731{
732 if (list_empty(&parent->child_head)) {
733 *devp = NULL;
734 } else {
735 *devp = list_first_entry(&parent->child_head, struct udevice,
736 sibling_node);
737 }
738
739 return 0;
740}
741
742int device_find_next_child(struct udevice **devp)
743{
744 struct udevice *dev = *devp;
745 struct udevice *parent = dev->parent;
746
747 if (list_is_last(&dev->sibling_node, &parent->child_head)) {
748 *devp = NULL;
749 } else {
750 *devp = list_entry(dev->sibling_node.next, struct udevice,
751 sibling_node);
752 }
753
754 return 0;
755}
Simon Glass70c3a0e2014-11-11 10:46:18 -0700756
Simon Glass2a580882020-01-27 08:49:36 -0700757int device_find_first_inactive_child(const struct udevice *parent,
Simon Glassb775e872018-10-01 12:22:07 -0600758 enum uclass_id uclass_id,
759 struct udevice **devp)
760{
761 struct udevice *dev;
762
763 *devp = NULL;
764 list_for_each_entry(dev, &parent->child_head, sibling_node) {
765 if (!device_active(dev) &&
766 device_get_uclass_id(dev) == uclass_id) {
767 *devp = dev;
768 return 0;
769 }
770 }
771
772 return -ENODEV;
773}
774
Simon Glass2a580882020-01-27 08:49:36 -0700775int device_find_first_child_by_uclass(const struct udevice *parent,
Simon Glassa11341a2018-11-18 08:14:31 -0700776 enum uclass_id uclass_id,
777 struct udevice **devp)
778{
779 struct udevice *dev;
780
781 *devp = NULL;
782 list_for_each_entry(dev, &parent->child_head, sibling_node) {
783 if (device_get_uclass_id(dev) == uclass_id) {
784 *devp = dev;
785 return 0;
786 }
787 }
788
789 return -ENODEV;
790}
791
Simon Glass2a580882020-01-27 08:49:36 -0700792int device_find_child_by_name(const struct udevice *parent, const char *name,
Simon Glassa11341a2018-11-18 08:14:31 -0700793 struct udevice **devp)
794{
795 struct udevice *dev;
796
797 *devp = NULL;
798
799 list_for_each_entry(dev, &parent->child_head, sibling_node) {
800 if (!strcmp(dev->name, name)) {
801 *devp = dev;
802 return 0;
803 }
804 }
805
806 return -ENODEV;
807}
808
Simon Glass76db9b72020-01-27 08:49:48 -0700809int device_first_child_err(struct udevice *parent, struct udevice **devp)
810{
811 struct udevice *dev;
812
813 device_find_first_child(parent, &dev);
814 if (!dev)
815 return -ENODEV;
816
817 return device_get_device_tail(dev, 0, devp);
818}
819
820int device_next_child_err(struct udevice **devp)
821{
822 struct udevice *dev = *devp;
823
824 device_find_next_child(&dev);
825 if (!dev)
826 return -ENODEV;
827
828 return device_get_device_tail(dev, 0, devp);
829}
830
Simon Glass000676b2020-01-27 08:49:47 -0700831int device_first_child_ofdata_err(struct udevice *parent, struct udevice **devp)
832{
833 struct udevice *dev;
834 int ret;
835
836 device_find_first_child(parent, &dev);
837 if (!dev)
838 return -ENODEV;
839
840 ret = device_ofdata_to_platdata(dev);
841 if (ret)
842 return ret;
843
844 *devp = dev;
845
846 return 0;
847}
848
849int device_next_child_ofdata_err(struct udevice **devp)
850{
851 struct udevice *dev = *devp;
852 int ret;
853
854 device_find_next_child(&dev);
855 if (!dev)
856 return -ENODEV;
857
858 ret = device_ofdata_to_platdata(dev);
859 if (ret)
860 return ret;
861
862 *devp = dev;
863
864 return 0;
865}
866
Simon Glass2e5e5ec2018-10-01 12:22:06 -0600867struct udevice *dev_get_parent(const struct udevice *child)
Simon Glass43f488a2014-11-11 10:46:19 -0700868{
869 return child->parent;
870}
871
Simon Glass2e5e5ec2018-10-01 12:22:06 -0600872ulong dev_get_driver_data(const struct udevice *dev)
Simon Glass70c3a0e2014-11-11 10:46:18 -0700873{
Simon Glass46227bd2015-03-25 12:21:55 -0600874 return dev->driver_data;
Simon Glass70c3a0e2014-11-11 10:46:18 -0700875}
Simon Glass98fd5d12015-01-25 08:27:04 -0700876
Simon Glass2e5e5ec2018-10-01 12:22:06 -0600877const void *dev_get_driver_ops(const struct udevice *dev)
Przemyslaw Marczakd3ef0d72015-04-15 13:07:24 +0200878{
879 if (!dev || !dev->driver->ops)
880 return NULL;
881
882 return dev->driver->ops;
883}
884
Simon Glass2e5e5ec2018-10-01 12:22:06 -0600885enum uclass_id device_get_uclass_id(const struct udevice *dev)
Simon Glass98fd5d12015-01-25 08:27:04 -0700886{
887 return dev->uclass->uc_drv->id;
888}
Peng Fan99b72352015-02-10 14:46:32 +0800889
Simon Glass2e5e5ec2018-10-01 12:22:06 -0600890const char *dev_get_uclass_name(const struct udevice *dev)
Przemyslaw Marczak5ed2e422015-04-15 13:07:25 +0200891{
892 if (!dev)
893 return NULL;
894
895 return dev->uclass->uc_drv->name;
896}
897
Simon Glass2e5e5ec2018-10-01 12:22:06 -0600898bool device_has_children(const struct udevice *dev)
Simon Glass40f829a2015-03-25 12:21:57 -0600899{
900 return !list_empty(&dev->child_head);
901}
902
Simon Glass2a580882020-01-27 08:49:36 -0700903bool device_has_active_children(const struct udevice *dev)
Simon Glass40f829a2015-03-25 12:21:57 -0600904{
905 struct udevice *child;
906
907 for (device_find_first_child(dev, &child);
908 child;
909 device_find_next_child(&child)) {
910 if (device_active(child))
911 return true;
912 }
913
914 return false;
915}
916
Simon Glass2a580882020-01-27 08:49:36 -0700917bool device_is_last_sibling(const struct udevice *dev)
Simon Glass40f829a2015-03-25 12:21:57 -0600918{
919 struct udevice *parent = dev->parent;
920
921 if (!parent)
922 return false;
923 return list_is_last(&dev->sibling_node, &parent->child_head);
924}
Simon Glasse3b23e22015-07-30 13:40:39 -0600925
Simon Glass7760ba22016-05-01 13:52:23 -0600926void device_set_name_alloced(struct udevice *dev)
927{
Simon Glass2d4c7dc2016-07-04 11:58:15 -0600928 dev->flags |= DM_FLAG_NAME_ALLOCED;
Simon Glass7760ba22016-05-01 13:52:23 -0600929}
930
Simon Glasse3b23e22015-07-30 13:40:39 -0600931int device_set_name(struct udevice *dev, const char *name)
932{
933 name = strdup(name);
934 if (!name)
935 return -ENOMEM;
936 dev->name = name;
Simon Glass7760ba22016-05-01 13:52:23 -0600937 device_set_name_alloced(dev);
Simon Glasse3b23e22015-07-30 13:40:39 -0600938
939 return 0;
940}
Mugunthan V N4666bd92016-04-28 15:36:02 +0530941
Marek Vasut7f18c342019-08-31 18:03:28 +0200942#if CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)
Simon Glass2a580882020-01-27 08:49:36 -0700943bool device_is_compatible(const struct udevice *dev, const char *compat)
Mugunthan V N4666bd92016-04-28 15:36:02 +0530944{
Masahiro Yamada9349bcc2018-04-19 12:14:02 +0900945 return ofnode_device_is_compatible(dev_ofnode(dev), compat);
Mugunthan V N4666bd92016-04-28 15:36:02 +0530946}
947
948bool of_machine_is_compatible(const char *compat)
949{
950 const void *fdt = gd->fdt_blob;
951
952 return !fdt_node_check_compatible(fdt, 0, compat);
953}
Mario Six3bedfbe2018-06-26 08:46:50 +0200954
955int dev_disable_by_path(const char *path)
956{
957 struct uclass *uc;
958 ofnode node = ofnode_path(path);
959 struct udevice *dev;
960 int ret = 1;
961
962 if (!of_live_active())
963 return -ENOSYS;
964
965 list_for_each_entry(uc, &gd->uclass_root, sibling_node) {
966 ret = uclass_find_device_by_ofnode(uc->uc_drv->id, node, &dev);
967 if (!ret)
968 break;
969 }
970
971 if (ret)
972 return ret;
973
974 ret = device_remove(dev, DM_REMOVE_NORMAL);
975 if (ret)
976 return ret;
977
978 ret = device_unbind(dev);
979 if (ret)
980 return ret;
981
982 return ofnode_set_enabled(node, false);
983}
984
985int dev_enable_by_path(const char *path)
986{
987 ofnode node = ofnode_path(path);
988 ofnode pnode = ofnode_get_parent(node);
989 struct udevice *parent;
990 int ret = 1;
991
992 if (!of_live_active())
993 return -ENOSYS;
994
995 ret = device_find_by_ofnode(pnode, &parent);
996 if (ret)
997 return ret;
998
999 ret = ofnode_set_enabled(node, true);
1000 if (ret)
1001 return ret;
1002
Bin Meng9a9b0742018-10-10 22:06:58 -07001003 return lists_bind_fdt(parent, node, NULL, false);
Mario Six3bedfbe2018-06-26 08:46:50 +02001004}
Marek Vasut7f18c342019-08-31 18:03:28 +02001005#endif