blob: 89ea820d487e294cc39cdf84e2ba4d4ee8b35a33 [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 }
146 }
Simon Glassdd6ab882014-02-26 15:59:18 -0700147
148 /* put dev into parent's successor list */
149 if (parent)
150 list_add_tail(&dev->sibling_node, &parent->child_head);
151
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
326 drv = dev->driver;
327 assert(drv);
328
Bin Meng82cdd782015-08-24 01:14:02 -0700329 /* Allocate private data if requested and not reentered */
330 if (drv->priv_auto_alloc_size && !dev->priv) {
Simon Glass825d3f92015-03-25 12:21:53 -0600331 dev->priv = alloc_priv(drv->priv_auto_alloc_size, drv->flags);
Simon Glassdd6ab882014-02-26 15:59:18 -0700332 if (!dev->priv) {
333 ret = -ENOMEM;
334 goto fail;
335 }
336 }
Bin Meng82cdd782015-08-24 01:14:02 -0700337 /* Allocate private data if requested and not reentered */
Simon Glassdd6ab882014-02-26 15:59:18 -0700338 size = dev->uclass->uc_drv->per_device_auto_alloc_size;
Bin Meng82cdd782015-08-24 01:14:02 -0700339 if (size && !dev->uclass_priv) {
Simon Glass7ebd13d2018-10-01 12:22:05 -0600340 dev->uclass_priv = alloc_priv(size,
341 dev->uclass->uc_drv->flags);
Simon Glassdd6ab882014-02-26 15:59:18 -0700342 if (!dev->uclass_priv) {
343 ret = -ENOMEM;
344 goto fail;
345 }
346 }
347
Simon Glassa8d8f162019-12-29 21:19:18 -0700348 /* Allocate parent data for this child */
Simon Glassdd6ab882014-02-26 15:59:18 -0700349 if (dev->parent) {
Simon Glass60d971b2014-07-23 06:55:20 -0600350 size = dev->parent->driver->per_child_auto_alloc_size;
Simon Glassc23b4282015-01-25 08:27:06 -0700351 if (!size) {
352 size = dev->parent->uclass->uc_drv->
353 per_child_auto_alloc_size;
354 }
Bin Meng82cdd782015-08-24 01:14:02 -0700355 if (size && !dev->parent_priv) {
Simon Glass825d3f92015-03-25 12:21:53 -0600356 dev->parent_priv = alloc_priv(size, drv->flags);
Simon Glass60d971b2014-07-23 06:55:20 -0600357 if (!dev->parent_priv) {
358 ret = -ENOMEM;
359 goto fail;
360 }
361 }
Simon Glassa8d8f162019-12-29 21:19:18 -0700362 }
363
364 if (drv->ofdata_to_platdata &&
365 (CONFIG_IS_ENABLED(OF_PLATDATA) || dev_has_of_node(dev))) {
366 ret = drv->ofdata_to_platdata(dev);
367 if (ret)
368 goto fail;
369 }
Simon Glass60d971b2014-07-23 06:55:20 -0600370
Simon Glassd3c9d302019-12-29 21:19:21 -0700371 dev->flags |= DM_FLAG_PLATDATA_VALID;
372
Simon Glass4a263852019-12-29 21:19:20 -0700373 return 0;
374fail:
375 device_free(dev);
376
377 return ret;
378}
379
380int device_probe(struct udevice *dev)
381{
382 const struct driver *drv;
383 int ret;
384 int seq;
385
386 if (!dev)
387 return -EINVAL;
388
389 if (dev->flags & DM_FLAG_ACTIVATED)
390 return 0;
391
392 drv = dev->driver;
393 assert(drv);
394
395 ret = device_ofdata_to_platdata(dev);
396 if (ret)
397 goto fail;
398
Simon Glassa8d8f162019-12-29 21:19:18 -0700399 /* Ensure all parents are probed */
400 if (dev->parent) {
Simon Glassdd6ab882014-02-26 15:59:18 -0700401 ret = device_probe(dev->parent);
402 if (ret)
403 goto fail;
Bin Meng82cdd782015-08-24 01:14:02 -0700404
405 /*
406 * The device might have already been probed during
407 * the call to device_probe() on its parent device
408 * (e.g. PCI bridge devices). Test the flags again
409 * so that we don't mess up the device.
410 */
411 if (dev->flags & DM_FLAG_ACTIVATED)
412 return 0;
Simon Glassdd6ab882014-02-26 15:59:18 -0700413 }
Simon Glassdb6f0202014-07-23 06:55:12 -0600414
415 seq = uclass_resolve_seq(dev);
416 if (seq < 0) {
417 ret = seq;
418 goto fail;
419 }
420 dev->seq = seq;
Simon Glassdd6ab882014-02-26 15:59:18 -0700421
Simon Glass8b04e732015-03-25 12:21:56 -0600422 dev->flags |= DM_FLAG_ACTIVATED;
423
Simon Glassbb53bda2015-09-12 08:45:19 -0600424 /*
425 * Process pinctrl for everything except the root device, and
Simon Glass27ce9622016-01-21 19:43:25 -0700426 * continue regardless of the result of pinctrl. Don't process pinctrl
427 * settings for pinctrl devices since the device may not yet be
428 * probed.
Simon Glassbb53bda2015-09-12 08:45:19 -0600429 */
Simon Glass27ce9622016-01-21 19:43:25 -0700430 if (dev->parent && device_get_uclass_id(dev) != UCLASS_PINCTRL)
Simon Glassbb53bda2015-09-12 08:45:19 -0600431 pinctrl_select_state(dev, "default");
Masahiro Yamadaf8efa632015-08-27 12:44:29 +0900432
Anatolij Gustschin63a34672019-07-14 21:11:01 +0200433 if (CONFIG_IS_ENABLED(POWER_DOMAIN) && dev->parent &&
Lokesh Vutlac10e52b2019-09-27 13:48:12 +0530434 (device_get_uclass_id(dev) != UCLASS_POWER_DOMAIN) &&
435 !(drv->flags & DM_FLAG_DEFAULT_PD_CTRL_OFF)) {
Peng Fand2201112019-09-17 09:29:22 +0000436 ret = dev_power_domain_on(dev);
437 if (ret)
438 goto fail;
Peng Fan6fba32d2018-07-27 10:20:38 +0800439 }
440
Simon Glass9c1f3822015-03-05 12:25:22 -0700441 ret = uclass_pre_probe_device(dev);
Simon Glass5104b982015-01-25 08:27:10 -0700442 if (ret)
443 goto fail;
444
Simon Glassd45560d2014-07-23 06:55:21 -0600445 if (dev->parent && dev->parent->driver->child_pre_probe) {
446 ret = dev->parent->driver->child_pre_probe(dev);
447 if (ret)
448 goto fail;
449 }
450
Bin Meng513cf402019-07-05 09:23:16 -0700451 /* Only handle devices that have a valid ofnode */
452 if (dev_of_valid(dev)) {
453 /*
454 * Process 'assigned-{clocks/clock-parents/clock-rates}'
455 * properties
456 */
Jean-Jacques Hiblot9601f322019-10-22 14:00:06 +0200457 ret = clk_set_defaults(dev, 0);
Bin Meng513cf402019-07-05 09:23:16 -0700458 if (ret)
459 goto fail;
460 }
Philipp Tomsich9cf03b02018-01-08 13:59:18 +0100461
Simon Glassdd6ab882014-02-26 15:59:18 -0700462 if (drv->probe) {
463 ret = drv->probe(dev);
Simon Glass9046dec2019-12-29 21:19:16 -0700464 if (ret)
Simon Glassdd6ab882014-02-26 15:59:18 -0700465 goto fail;
466 }
467
Simon Glassdd6ab882014-02-26 15:59:18 -0700468 ret = uclass_post_probe_device(dev);
Simon Glass8b04e732015-03-25 12:21:56 -0600469 if (ret)
Simon Glassdd6ab882014-02-26 15:59:18 -0700470 goto fail_uclass;
Simon Glassdd6ab882014-02-26 15:59:18 -0700471
Peng Fan7cf58dd2016-03-12 13:17:38 +0800472 if (dev->parent && device_get_uclass_id(dev) == UCLASS_PINCTRL)
473 pinctrl_select_state(dev, "default");
474
Simon Glassdd6ab882014-02-26 15:59:18 -0700475 return 0;
476fail_uclass:
Stefan Roese80b5bc92017-03-20 12:51:48 +0100477 if (device_remove(dev, DM_REMOVE_NORMAL)) {
Simon Glassdd6ab882014-02-26 15:59:18 -0700478 dm_warn("%s: Device '%s' failed to remove on error path\n",
479 __func__, dev->name);
480 }
481fail:
Simon Glass8b04e732015-03-25 12:21:56 -0600482 dev->flags &= ~DM_FLAG_ACTIVATED;
483
Simon Glassdb6f0202014-07-23 06:55:12 -0600484 dev->seq = -1;
Simon Glassdd6ab882014-02-26 15:59:18 -0700485 device_free(dev);
486
487 return ret;
488}
489
Simon Glass2e5e5ec2018-10-01 12:22:06 -0600490void *dev_get_platdata(const struct udevice *dev)
Simon Glassdd6ab882014-02-26 15:59:18 -0700491{
492 if (!dev) {
Simon Glassdf1e0722014-12-10 08:55:56 -0700493 dm_warn("%s: null device\n", __func__);
Simon Glassdd6ab882014-02-26 15:59:18 -0700494 return NULL;
495 }
496
497 return dev->platdata;
498}
499
Simon Glass2e5e5ec2018-10-01 12:22:06 -0600500void *dev_get_parent_platdata(const struct udevice *dev)
Simon Glass11b61732015-01-25 08:27:01 -0700501{
502 if (!dev) {
Simon Glasscdfc5052015-07-06 16:47:40 -0600503 dm_warn("%s: null device\n", __func__);
Simon Glass11b61732015-01-25 08:27:01 -0700504 return NULL;
505 }
506
507 return dev->parent_platdata;
508}
509
Simon Glass2e5e5ec2018-10-01 12:22:06 -0600510void *dev_get_uclass_platdata(const struct udevice *dev)
Przemyslaw Marczakd850e672015-04-15 13:07:18 +0200511{
512 if (!dev) {
Simon Glasscdfc5052015-07-06 16:47:40 -0600513 dm_warn("%s: null device\n", __func__);
Przemyslaw Marczakd850e672015-04-15 13:07:18 +0200514 return NULL;
515 }
516
517 return dev->uclass_platdata;
518}
519
Simon Glass2e5e5ec2018-10-01 12:22:06 -0600520void *dev_get_priv(const struct udevice *dev)
Simon Glassdd6ab882014-02-26 15:59:18 -0700521{
522 if (!dev) {
Simon Glassdf1e0722014-12-10 08:55:56 -0700523 dm_warn("%s: null device\n", __func__);
Simon Glassdd6ab882014-02-26 15:59:18 -0700524 return NULL;
525 }
526
527 return dev->priv;
528}
Simon Glass48d4e292014-07-23 06:55:19 -0600529
Simon Glass2e5e5ec2018-10-01 12:22:06 -0600530void *dev_get_uclass_priv(const struct udevice *dev)
Simon Glassde0977b2015-03-05 12:25:20 -0700531{
532 if (!dev) {
533 dm_warn("%s: null device\n", __func__);
534 return NULL;
535 }
536
537 return dev->uclass_priv;
538}
539
Simon Glass2e5e5ec2018-10-01 12:22:06 -0600540void *dev_get_parent_priv(const struct udevice *dev)
Simon Glass60d971b2014-07-23 06:55:20 -0600541{
542 if (!dev) {
Simon Glassdf1e0722014-12-10 08:55:56 -0700543 dm_warn("%s: null device\n", __func__);
Simon Glass60d971b2014-07-23 06:55:20 -0600544 return NULL;
545 }
546
547 return dev->parent_priv;
548}
549
Simon Glass48d4e292014-07-23 06:55:19 -0600550static int device_get_device_tail(struct udevice *dev, int ret,
551 struct udevice **devp)
552{
553 if (ret)
554 return ret;
555
556 ret = device_probe(dev);
557 if (ret)
558 return ret;
559
560 *devp = dev;
561
562 return 0;
563}
564
Marek Vasut7f18c342019-08-31 18:03:28 +0200565#if CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)
Mario Six3bedfbe2018-06-26 08:46:50 +0200566/**
567 * device_find_by_ofnode() - Return device associated with given ofnode
568 *
569 * The returned device is *not* activated.
570 *
571 * @node: The ofnode for which a associated device should be looked up
572 * @devp: Pointer to structure to hold the found device
573 * Return: 0 if OK, -ve on error
574 */
575static int device_find_by_ofnode(ofnode node, struct udevice **devp)
576{
577 struct uclass *uc;
578 struct udevice *dev;
579 int ret;
580
581 list_for_each_entry(uc, &gd->uclass_root, sibling_node) {
582 ret = uclass_find_device_by_ofnode(uc->uc_drv->id, node,
583 &dev);
584 if (!ret || dev) {
585 *devp = dev;
586 return 0;
587 }
588 }
589
590 return -ENODEV;
591}
Marek Vasut7f18c342019-08-31 18:03:28 +0200592#endif
Mario Six3bedfbe2018-06-26 08:46:50 +0200593
Simon Glass2a580882020-01-27 08:49:36 -0700594int device_get_child(const struct udevice *parent, int index,
595 struct udevice **devp)
Simon Glass48d4e292014-07-23 06:55:19 -0600596{
597 struct udevice *dev;
598
599 list_for_each_entry(dev, &parent->child_head, sibling_node) {
600 if (!index--)
601 return device_get_device_tail(dev, 0, devp);
602 }
603
604 return -ENODEV;
605}
606
Simon Glass2a580882020-01-27 08:49:36 -0700607int device_get_child_count(const struct udevice *parent)
Lokesh Vutlafa45a082019-09-04 16:01:26 +0530608{
609 struct udevice *dev;
610 int count = 0;
611
612 list_for_each_entry(dev, &parent->child_head, sibling_node)
613 count++;
614
615 return count;
616}
617
Simon Glass2a580882020-01-27 08:49:36 -0700618int device_find_child_by_seq(const struct udevice *parent, int seq_or_req_seq,
Simon Glass48d4e292014-07-23 06:55:19 -0600619 bool find_req_seq, struct udevice **devp)
620{
621 struct udevice *dev;
622
623 *devp = NULL;
624 if (seq_or_req_seq == -1)
625 return -ENODEV;
626
627 list_for_each_entry(dev, &parent->child_head, sibling_node) {
628 if ((find_req_seq ? dev->req_seq : dev->seq) ==
629 seq_or_req_seq) {
630 *devp = dev;
631 return 0;
632 }
633 }
634
635 return -ENODEV;
636}
637
Simon Glass2a580882020-01-27 08:49:36 -0700638int device_get_child_by_seq(const struct udevice *parent, int seq,
Simon Glass48d4e292014-07-23 06:55:19 -0600639 struct udevice **devp)
640{
641 struct udevice *dev;
642 int ret;
643
644 *devp = NULL;
645 ret = device_find_child_by_seq(parent, seq, false, &dev);
646 if (ret == -ENODEV) {
647 /*
648 * We didn't find it in probed devices. See if there is one
649 * that will request this seq if probed.
650 */
651 ret = device_find_child_by_seq(parent, seq, true, &dev);
652 }
653 return device_get_device_tail(dev, ret, devp);
654}
655
Simon Glass2a580882020-01-27 08:49:36 -0700656int device_find_child_by_of_offset(const struct udevice *parent, int of_offset,
Simon Glass48d4e292014-07-23 06:55:19 -0600657 struct udevice **devp)
658{
659 struct udevice *dev;
660
661 *devp = NULL;
662
663 list_for_each_entry(dev, &parent->child_head, sibling_node) {
Simon Glassdd79d6e2017-01-17 16:52:55 -0700664 if (dev_of_offset(dev) == of_offset) {
Simon Glass48d4e292014-07-23 06:55:19 -0600665 *devp = dev;
666 return 0;
667 }
668 }
669
670 return -ENODEV;
671}
672
Simon Glass2a580882020-01-27 08:49:36 -0700673int device_get_child_by_of_offset(const struct udevice *parent, int node,
Simon Glass48d4e292014-07-23 06:55:19 -0600674 struct udevice **devp)
675{
676 struct udevice *dev;
677 int ret;
678
679 *devp = NULL;
Simon Glass861bc9f2015-06-23 15:38:38 -0600680 ret = device_find_child_by_of_offset(parent, node, &dev);
Simon Glass48d4e292014-07-23 06:55:19 -0600681 return device_get_device_tail(dev, ret, devp);
682}
Simon Glass44da7352014-10-13 23:41:49 -0600683
Jean-Jacques Hiblota7b0d6a2018-08-09 16:17:44 +0200684static struct udevice *_device_find_global_by_ofnode(struct udevice *parent,
685 ofnode ofnode)
Simon Glassae2efac2015-06-23 15:38:37 -0600686{
687 struct udevice *dev, *found;
688
Jean-Jacques Hiblota7b0d6a2018-08-09 16:17:44 +0200689 if (ofnode_equal(dev_ofnode(parent), ofnode))
Simon Glassae2efac2015-06-23 15:38:37 -0600690 return parent;
691
692 list_for_each_entry(dev, &parent->child_head, sibling_node) {
Jean-Jacques Hiblota7b0d6a2018-08-09 16:17:44 +0200693 found = _device_find_global_by_ofnode(dev, ofnode);
Simon Glassae2efac2015-06-23 15:38:37 -0600694 if (found)
695 return found;
696 }
697
698 return NULL;
699}
700
Jean-Jacques Hiblota7b0d6a2018-08-09 16:17:44 +0200701int device_find_global_by_ofnode(ofnode ofnode, struct udevice **devp)
702{
703 *devp = _device_find_global_by_ofnode(gd->dm_root, ofnode);
704
705 return *devp ? 0 : -ENOENT;
706}
707
708int device_get_global_by_ofnode(ofnode ofnode, struct udevice **devp)
Simon Glassae2efac2015-06-23 15:38:37 -0600709{
710 struct udevice *dev;
711
Jean-Jacques Hiblota7b0d6a2018-08-09 16:17:44 +0200712 dev = _device_find_global_by_ofnode(gd->dm_root, ofnode);
Simon Glassae2efac2015-06-23 15:38:37 -0600713 return device_get_device_tail(dev, dev ? 0 : -ENOENT, devp);
714}
715
Simon Glass2a580882020-01-27 08:49:36 -0700716int device_find_first_child(const struct udevice *parent, struct udevice **devp)
Simon Glass44da7352014-10-13 23:41:49 -0600717{
718 if (list_empty(&parent->child_head)) {
719 *devp = NULL;
720 } else {
721 *devp = list_first_entry(&parent->child_head, struct udevice,
722 sibling_node);
723 }
724
725 return 0;
726}
727
728int device_find_next_child(struct udevice **devp)
729{
730 struct udevice *dev = *devp;
731 struct udevice *parent = dev->parent;
732
733 if (list_is_last(&dev->sibling_node, &parent->child_head)) {
734 *devp = NULL;
735 } else {
736 *devp = list_entry(dev->sibling_node.next, struct udevice,
737 sibling_node);
738 }
739
740 return 0;
741}
Simon Glass70c3a0e2014-11-11 10:46:18 -0700742
Simon Glass2a580882020-01-27 08:49:36 -0700743int device_find_first_inactive_child(const struct udevice *parent,
Simon Glassb775e872018-10-01 12:22:07 -0600744 enum uclass_id uclass_id,
745 struct udevice **devp)
746{
747 struct udevice *dev;
748
749 *devp = NULL;
750 list_for_each_entry(dev, &parent->child_head, sibling_node) {
751 if (!device_active(dev) &&
752 device_get_uclass_id(dev) == uclass_id) {
753 *devp = dev;
754 return 0;
755 }
756 }
757
758 return -ENODEV;
759}
760
Simon Glass2a580882020-01-27 08:49:36 -0700761int device_find_first_child_by_uclass(const struct udevice *parent,
Simon Glassa11341a2018-11-18 08:14:31 -0700762 enum uclass_id uclass_id,
763 struct udevice **devp)
764{
765 struct udevice *dev;
766
767 *devp = NULL;
768 list_for_each_entry(dev, &parent->child_head, sibling_node) {
769 if (device_get_uclass_id(dev) == uclass_id) {
770 *devp = dev;
771 return 0;
772 }
773 }
774
775 return -ENODEV;
776}
777
Simon Glass2a580882020-01-27 08:49:36 -0700778int device_find_child_by_name(const struct udevice *parent, const char *name,
Simon Glassa11341a2018-11-18 08:14:31 -0700779 struct udevice **devp)
780{
781 struct udevice *dev;
782
783 *devp = NULL;
784
785 list_for_each_entry(dev, &parent->child_head, sibling_node) {
786 if (!strcmp(dev->name, name)) {
787 *devp = dev;
788 return 0;
789 }
790 }
791
792 return -ENODEV;
793}
794
Simon Glass76db9b72020-01-27 08:49:48 -0700795int device_first_child_err(struct udevice *parent, struct udevice **devp)
796{
797 struct udevice *dev;
798
799 device_find_first_child(parent, &dev);
800 if (!dev)
801 return -ENODEV;
802
803 return device_get_device_tail(dev, 0, devp);
804}
805
806int device_next_child_err(struct udevice **devp)
807{
808 struct udevice *dev = *devp;
809
810 device_find_next_child(&dev);
811 if (!dev)
812 return -ENODEV;
813
814 return device_get_device_tail(dev, 0, devp);
815}
816
Simon Glass000676b2020-01-27 08:49:47 -0700817int device_first_child_ofdata_err(struct udevice *parent, struct udevice **devp)
818{
819 struct udevice *dev;
820 int ret;
821
822 device_find_first_child(parent, &dev);
823 if (!dev)
824 return -ENODEV;
825
826 ret = device_ofdata_to_platdata(dev);
827 if (ret)
828 return ret;
829
830 *devp = dev;
831
832 return 0;
833}
834
835int device_next_child_ofdata_err(struct udevice **devp)
836{
837 struct udevice *dev = *devp;
838 int ret;
839
840 device_find_next_child(&dev);
841 if (!dev)
842 return -ENODEV;
843
844 ret = device_ofdata_to_platdata(dev);
845 if (ret)
846 return ret;
847
848 *devp = dev;
849
850 return 0;
851}
852
Simon Glass2e5e5ec2018-10-01 12:22:06 -0600853struct udevice *dev_get_parent(const struct udevice *child)
Simon Glass43f488a2014-11-11 10:46:19 -0700854{
855 return child->parent;
856}
857
Simon Glass2e5e5ec2018-10-01 12:22:06 -0600858ulong dev_get_driver_data(const struct udevice *dev)
Simon Glass70c3a0e2014-11-11 10:46:18 -0700859{
Simon Glass46227bd2015-03-25 12:21:55 -0600860 return dev->driver_data;
Simon Glass70c3a0e2014-11-11 10:46:18 -0700861}
Simon Glass98fd5d12015-01-25 08:27:04 -0700862
Simon Glass2e5e5ec2018-10-01 12:22:06 -0600863const void *dev_get_driver_ops(const struct udevice *dev)
Przemyslaw Marczakd3ef0d72015-04-15 13:07:24 +0200864{
865 if (!dev || !dev->driver->ops)
866 return NULL;
867
868 return dev->driver->ops;
869}
870
Simon Glass2e5e5ec2018-10-01 12:22:06 -0600871enum uclass_id device_get_uclass_id(const struct udevice *dev)
Simon Glass98fd5d12015-01-25 08:27:04 -0700872{
873 return dev->uclass->uc_drv->id;
874}
Peng Fan99b72352015-02-10 14:46:32 +0800875
Simon Glass2e5e5ec2018-10-01 12:22:06 -0600876const char *dev_get_uclass_name(const struct udevice *dev)
Przemyslaw Marczak5ed2e422015-04-15 13:07:25 +0200877{
878 if (!dev)
879 return NULL;
880
881 return dev->uclass->uc_drv->name;
882}
883
Simon Glass2e5e5ec2018-10-01 12:22:06 -0600884bool device_has_children(const struct udevice *dev)
Simon Glass40f829a2015-03-25 12:21:57 -0600885{
886 return !list_empty(&dev->child_head);
887}
888
Simon Glass2a580882020-01-27 08:49:36 -0700889bool device_has_active_children(const struct udevice *dev)
Simon Glass40f829a2015-03-25 12:21:57 -0600890{
891 struct udevice *child;
892
893 for (device_find_first_child(dev, &child);
894 child;
895 device_find_next_child(&child)) {
896 if (device_active(child))
897 return true;
898 }
899
900 return false;
901}
902
Simon Glass2a580882020-01-27 08:49:36 -0700903bool device_is_last_sibling(const struct udevice *dev)
Simon Glass40f829a2015-03-25 12:21:57 -0600904{
905 struct udevice *parent = dev->parent;
906
907 if (!parent)
908 return false;
909 return list_is_last(&dev->sibling_node, &parent->child_head);
910}
Simon Glasse3b23e22015-07-30 13:40:39 -0600911
Simon Glass7760ba22016-05-01 13:52:23 -0600912void device_set_name_alloced(struct udevice *dev)
913{
Simon Glass2d4c7dc2016-07-04 11:58:15 -0600914 dev->flags |= DM_FLAG_NAME_ALLOCED;
Simon Glass7760ba22016-05-01 13:52:23 -0600915}
916
Simon Glasse3b23e22015-07-30 13:40:39 -0600917int device_set_name(struct udevice *dev, const char *name)
918{
919 name = strdup(name);
920 if (!name)
921 return -ENOMEM;
922 dev->name = name;
Simon Glass7760ba22016-05-01 13:52:23 -0600923 device_set_name_alloced(dev);
Simon Glasse3b23e22015-07-30 13:40:39 -0600924
925 return 0;
926}
Mugunthan V N4666bd92016-04-28 15:36:02 +0530927
Marek Vasut7f18c342019-08-31 18:03:28 +0200928#if CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)
Simon Glass2a580882020-01-27 08:49:36 -0700929bool device_is_compatible(const struct udevice *dev, const char *compat)
Mugunthan V N4666bd92016-04-28 15:36:02 +0530930{
Masahiro Yamada9349bcc2018-04-19 12:14:02 +0900931 return ofnode_device_is_compatible(dev_ofnode(dev), compat);
Mugunthan V N4666bd92016-04-28 15:36:02 +0530932}
933
934bool of_machine_is_compatible(const char *compat)
935{
936 const void *fdt = gd->fdt_blob;
937
938 return !fdt_node_check_compatible(fdt, 0, compat);
939}
Mario Six3bedfbe2018-06-26 08:46:50 +0200940
941int dev_disable_by_path(const char *path)
942{
943 struct uclass *uc;
944 ofnode node = ofnode_path(path);
945 struct udevice *dev;
946 int ret = 1;
947
948 if (!of_live_active())
949 return -ENOSYS;
950
951 list_for_each_entry(uc, &gd->uclass_root, sibling_node) {
952 ret = uclass_find_device_by_ofnode(uc->uc_drv->id, node, &dev);
953 if (!ret)
954 break;
955 }
956
957 if (ret)
958 return ret;
959
960 ret = device_remove(dev, DM_REMOVE_NORMAL);
961 if (ret)
962 return ret;
963
964 ret = device_unbind(dev);
965 if (ret)
966 return ret;
967
968 return ofnode_set_enabled(node, false);
969}
970
971int dev_enable_by_path(const char *path)
972{
973 ofnode node = ofnode_path(path);
974 ofnode pnode = ofnode_get_parent(node);
975 struct udevice *parent;
976 int ret = 1;
977
978 if (!of_live_active())
979 return -ENOSYS;
980
981 ret = device_find_by_ofnode(pnode, &parent);
982 if (ret)
983 return ret;
984
985 ret = ofnode_set_enabled(node, true);
986 if (ret)
987 return ret;
988
Bin Meng9a9b0742018-10-10 22:06:58 -0700989 return lists_bind_fdt(parent, node, NULL, false);
Mario Six3bedfbe2018-06-26 08:46:50 +0200990}
Marek Vasut7f18c342019-08-31 18:03:28 +0200991#endif