]>
Commit | Line | Data |
---|---|---|
83d290c5 | 1 | // SPDX-License-Identifier: GPL-2.0+ |
6494d708 SG |
2 | /* |
3 | * Device manager | |
4 | * | |
5 | * Copyright (c) 2013 Google, Inc | |
6 | * | |
7 | * (C) Copyright 2012 | |
8 | * Pavel Herrmann <[email protected]> | |
6494d708 SG |
9 | */ |
10 | ||
11 | #include <common.h> | |
1eb69ae4 | 12 | #include <cpu_func.h> |
f7ae49fc | 13 | #include <log.h> |
7c616862 | 14 | #include <asm/io.h> |
f4fcba5c | 15 | #include <clk.h> |
5a66a8ff | 16 | #include <fdtdec.h> |
ef5cd330 | 17 | #include <fdt_support.h> |
6494d708 | 18 | #include <malloc.h> |
90526e9f | 19 | #include <asm/cache.h> |
6494d708 SG |
20 | #include <dm/device.h> |
21 | #include <dm/device-internal.h> | |
22 | #include <dm/lists.h> | |
29d11b88 | 23 | #include <dm/of_access.h> |
d90a5a30 | 24 | #include <dm/pinctrl.h> |
6494d708 | 25 | #include <dm/platdata.h> |
396e343b | 26 | #include <dm/read.h> |
6494d708 SG |
27 | #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> | |
3ad30778 | 32 | #include <power-domain.h> |
6494d708 | 33 | |
5a66a8ff SG |
34 | DECLARE_GLOBAL_DATA_PTR; |
35 | ||
daac3bfe SW |
36 | static int device_bind_common(struct udevice *parent, const struct driver *drv, |
37 | const char *name, void *platdata, | |
7a61b0b5 | 38 | ulong driver_data, ofnode node, |
9fa28190 | 39 | uint of_platdata_size, struct udevice **devp) |
6494d708 | 40 | { |
54c5d08a | 41 | struct udevice *dev; |
6494d708 | 42 | struct uclass *uc; |
5eaed880 | 43 | int size, ret = 0; |
6494d708 | 44 | |
e6cabe4a MY |
45 | if (devp) |
46 | *devp = NULL; | |
6494d708 SG |
47 | if (!name) |
48 | return -EINVAL; | |
49 | ||
50 | ret = uclass_get(drv->id, &uc); | |
3346c876 SG |
51 | if (ret) { |
52 | debug("Missing uclass for driver %s\n", drv->name); | |
6494d708 | 53 | return ret; |
3346c876 | 54 | } |
6494d708 | 55 | |
54c5d08a | 56 | dev = calloc(1, sizeof(struct udevice)); |
6494d708 SG |
57 | 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); | |
e2282d70 | 63 | #ifdef CONFIG_DEVRES |
608f26c5 | 64 | INIT_LIST_HEAD(&dev->devres_head); |
e2282d70 | 65 | #endif |
6494d708 | 66 | dev->platdata = platdata; |
daac3bfe | 67 | dev->driver_data = driver_data; |
6494d708 | 68 | dev->name = name; |
7a61b0b5 | 69 | dev->node = node; |
6494d708 SG |
70 | dev->parent = parent; |
71 | dev->driver = drv; | |
72 | dev->uclass = uc; | |
5a66a8ff | 73 | |
5a66a8ff | 74 | dev->seq = -1; |
9cc36a2b | 75 | dev->req_seq = -1; |
3542ff29 JJH |
76 | if (CONFIG_IS_ENABLED(DM_SEQ_ALIAS) && |
77 | (uc->uc_drv->flags & DM_UC_FLAG_SEQ_ALIAS)) { | |
36fa61dc | 78 | /* |
770eb30e SR |
79 | * 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 | */ | |
6d65ac31 SG |
85 | if (CONFIG_IS_ENABLED(OF_CONTROL) && |
86 | !CONFIG_IS_ENABLED(OF_PLATDATA)) { | |
3542ff29 | 87 | if (uc->uc_drv->name && ofnode_valid(node)) |
396e343b | 88 | dev_read_alias_seq(dev, &dev->req_seq); |
7f3289bf TF |
89 | #if CONFIG_IS_ENABLED(OF_PRIOR_STAGE) |
90 | if (dev->req_seq == -1) | |
91 | dev->req_seq = | |
92 | uclass_find_next_free_req_seq(drv->id); | |
93 | #endif | |
3542ff29 JJH |
94 | } else { |
95 | dev->req_seq = uclass_find_next_free_req_seq(drv->id); | |
9cc36a2b | 96 | } |
5a66a8ff | 97 | } |
36fa61dc | 98 | |
9fa28190 SG |
99 | if (drv->platdata_auto_alloc_size) { |
100 | bool alloc = !platdata; | |
101 | ||
102 | if (CONFIG_IS_ENABLED(OF_PLATDATA)) { | |
103 | if (of_platdata_size) { | |
104 | dev->flags |= DM_FLAG_OF_PLATDATA; | |
105 | if (of_platdata_size < | |
106 | drv->platdata_auto_alloc_size) | |
107 | alloc = true; | |
108 | } | |
109 | } | |
110 | if (alloc) { | |
111 | dev->flags |= DM_FLAG_ALLOC_PDATA; | |
112 | dev->platdata = calloc(1, | |
113 | drv->platdata_auto_alloc_size); | |
114 | if (!dev->platdata) { | |
115 | ret = -ENOMEM; | |
116 | goto fail_alloc1; | |
117 | } | |
118 | if (CONFIG_IS_ENABLED(OF_PLATDATA) && platdata) { | |
119 | memcpy(dev->platdata, platdata, | |
120 | of_platdata_size); | |
121 | } | |
f8a85449 SG |
122 | } |
123 | } | |
cdc133bd | 124 | |
5eaed880 PM |
125 | size = uc->uc_drv->per_device_platdata_auto_alloc_size; |
126 | if (size) { | |
127 | dev->flags |= DM_FLAG_ALLOC_UCLASS_PDATA; | |
128 | dev->uclass_platdata = calloc(1, size); | |
129 | if (!dev->uclass_platdata) { | |
130 | ret = -ENOMEM; | |
131 | goto fail_alloc2; | |
132 | } | |
133 | } | |
134 | ||
135 | if (parent) { | |
136 | size = parent->driver->per_child_platdata_auto_alloc_size; | |
ba8da9dc SG |
137 | if (!size) { |
138 | size = parent->uclass->uc_drv-> | |
139 | per_child_platdata_auto_alloc_size; | |
140 | } | |
cdc133bd SG |
141 | if (size) { |
142 | dev->flags |= DM_FLAG_ALLOC_PARENT_PDATA; | |
143 | dev->parent_platdata = calloc(1, size); | |
144 | if (!dev->parent_platdata) { | |
145 | ret = -ENOMEM; | |
5eaed880 | 146 | goto fail_alloc3; |
cdc133bd SG |
147 | } |
148 | } | |
f93a07dd | 149 | /* put dev into parent's successor list */ |
6494d708 | 150 | list_add_tail(&dev->sibling_node, &parent->child_head); |
f93a07dd | 151 | } |
6494d708 SG |
152 | |
153 | ret = uclass_bind_device(dev); | |
154 | if (ret) | |
72ebfe86 | 155 | goto fail_uclass_bind; |
6494d708 SG |
156 | |
157 | /* if we fail to bind we remove device from successors and free it */ | |
158 | if (drv->bind) { | |
159 | ret = drv->bind(dev); | |
72ebfe86 | 160 | if (ret) |
6494d708 | 161 | goto fail_bind; |
6494d708 | 162 | } |
0118ce79 SG |
163 | if (parent && parent->driver->child_post_bind) { |
164 | ret = parent->driver->child_post_bind(dev); | |
165 | if (ret) | |
166 | goto fail_child_post_bind; | |
167 | } | |
20af3c0a SG |
168 | if (uc->uc_drv->post_bind) { |
169 | ret = uc->uc_drv->post_bind(dev); | |
170 | if (ret) | |
171 | goto fail_uclass_post_bind; | |
172 | } | |
0118ce79 | 173 | |
6494d708 | 174 | if (parent) |
ceb91909 | 175 | pr_debug("Bound device %s to %s\n", dev->name, parent->name); |
e6cabe4a MY |
176 | if (devp) |
177 | *devp = dev; | |
6494d708 | 178 | |
aed1a4dd MY |
179 | dev->flags |= DM_FLAG_BOUND; |
180 | ||
6494d708 SG |
181 | return 0; |
182 | ||
20af3c0a SG |
183 | fail_uclass_post_bind: |
184 | /* There is no child unbind() method, so no clean-up required */ | |
0118ce79 | 185 | fail_child_post_bind: |
0a5804b5 | 186 | if (CONFIG_IS_ENABLED(DM_DEVICE_REMOVE)) { |
5a87c417 SG |
187 | if (drv->unbind && drv->unbind(dev)) { |
188 | dm_warn("unbind() method failed on dev '%s' on error path\n", | |
189 | dev->name); | |
190 | } | |
0118ce79 SG |
191 | } |
192 | ||
6494d708 | 193 | fail_bind: |
0a5804b5 | 194 | if (CONFIG_IS_ENABLED(DM_DEVICE_REMOVE)) { |
5a87c417 SG |
195 | if (uclass_unbind_device(dev)) { |
196 | dm_warn("Failed to unbind dev '%s' on error path\n", | |
197 | dev->name); | |
198 | } | |
72ebfe86 SG |
199 | } |
200 | fail_uclass_bind: | |
0a5804b5 | 201 | if (CONFIG_IS_ENABLED(DM_DEVICE_REMOVE)) { |
5a87c417 SG |
202 | list_del(&dev->sibling_node); |
203 | if (dev->flags & DM_FLAG_ALLOC_PARENT_PDATA) { | |
204 | free(dev->parent_platdata); | |
205 | dev->parent_platdata = NULL; | |
206 | } | |
cdc133bd | 207 | } |
5eaed880 PM |
208 | fail_alloc3: |
209 | if (dev->flags & DM_FLAG_ALLOC_UCLASS_PDATA) { | |
210 | free(dev->uclass_platdata); | |
211 | dev->uclass_platdata = NULL; | |
212 | } | |
cdc133bd | 213 | fail_alloc2: |
f8a85449 SG |
214 | if (dev->flags & DM_FLAG_ALLOC_PDATA) { |
215 | free(dev->platdata); | |
216 | dev->platdata = NULL; | |
217 | } | |
218 | fail_alloc1: | |
608f26c5 MY |
219 | devres_release_all(dev); |
220 | ||
6494d708 | 221 | free(dev); |
72ebfe86 | 222 | |
6494d708 SG |
223 | return ret; |
224 | } | |
225 | ||
daac3bfe SW |
226 | int device_bind_with_driver_data(struct udevice *parent, |
227 | const struct driver *drv, const char *name, | |
396e343b | 228 | ulong driver_data, ofnode node, |
daac3bfe SW |
229 | struct udevice **devp) |
230 | { | |
396e343b SG |
231 | return device_bind_common(parent, drv, name, NULL, driver_data, node, |
232 | 0, devp); | |
daac3bfe SW |
233 | } |
234 | ||
235 | int device_bind(struct udevice *parent, const struct driver *drv, | |
236 | const char *name, void *platdata, int of_offset, | |
237 | struct udevice **devp) | |
238 | { | |
7a61b0b5 SG |
239 | return device_bind_common(parent, drv, name, platdata, 0, |
240 | offset_to_ofnode(of_offset), 0, devp); | |
daac3bfe SW |
241 | } |
242 | ||
d677b00c SG |
243 | int device_bind_ofnode(struct udevice *parent, const struct driver *drv, |
244 | const char *name, void *platdata, ofnode node, | |
245 | struct udevice **devp) | |
246 | { | |
247 | return device_bind_common(parent, drv, name, platdata, 0, node, 0, | |
248 | devp); | |
249 | } | |
250 | ||
00606d7e | 251 | int device_bind_by_name(struct udevice *parent, bool pre_reloc_only, |
908d0243 | 252 | struct driver_info *info, struct udevice **devp) |
6494d708 SG |
253 | { |
254 | struct driver *drv; | |
9fa28190 | 255 | uint platdata_size = 0; |
fed0f891 | 256 | int ret; |
6494d708 SG |
257 | |
258 | drv = lists_driver_lookup_name(info->name); | |
259 | if (!drv) | |
260 | return -ENOENT; | |
00606d7e SG |
261 | if (pre_reloc_only && !(drv->flags & DM_FLAG_PRE_RELOC)) |
262 | return -EPERM; | |
6494d708 | 263 | |
9fa28190 SG |
264 | #if CONFIG_IS_ENABLED(OF_PLATDATA) |
265 | platdata_size = info->platdata_size; | |
266 | #endif | |
fed0f891 WL |
267 | ret = device_bind_common(parent, drv, info->name, |
268 | (void *)info->platdata, 0, ofnode_null(), | |
269 | platdata_size, devp); | |
270 | if (ret) | |
271 | return ret; | |
272 | #if CONFIG_IS_ENABLED(OF_PLATDATA) | |
273 | info->dev = *devp; | |
274 | #endif | |
275 | ||
276 | return ret; | |
6494d708 SG |
277 | } |
278 | ||
cfecbaf4 CB |
279 | int device_reparent(struct udevice *dev, struct udevice *new_parent) |
280 | { | |
281 | struct udevice *pos, *n; | |
282 | ||
283 | assert(dev); | |
284 | assert(new_parent); | |
285 | ||
286 | list_for_each_entry_safe(pos, n, &dev->parent->child_head, | |
287 | sibling_node) { | |
288 | if (pos->driver != dev->driver) | |
289 | continue; | |
290 | ||
291 | list_del(&dev->sibling_node); | |
292 | list_add_tail(&dev->sibling_node, &new_parent->child_head); | |
293 | dev->parent = new_parent; | |
294 | ||
295 | break; | |
296 | } | |
297 | ||
298 | return 0; | |
299 | } | |
300 | ||
2c03c463 SG |
301 | static void *alloc_priv(int size, uint flags) |
302 | { | |
303 | void *priv; | |
304 | ||
305 | if (flags & DM_FLAG_ALLOC_PRIV_DMA) { | |
5924da1d | 306 | size = ROUND(size, ARCH_DMA_MINALIGN); |
2c03c463 | 307 | priv = memalign(ARCH_DMA_MINALIGN, size); |
5a8a8045 | 308 | if (priv) { |
2c03c463 | 309 | memset(priv, '\0', size); |
5a8a8045 SG |
310 | |
311 | /* | |
312 | * Ensure that the zero bytes are flushed to memory. | |
313 | * This prevents problems if the driver uses this as | |
314 | * both an input and an output buffer: | |
315 | * | |
316 | * 1. Zeroes written to buffer (here) and sit in the | |
317 | * cache | |
318 | * 2. Driver issues a read command to DMA | |
319 | * 3. CPU runs out of cache space and evicts some cache | |
320 | * data in the buffer, writing zeroes to RAM from | |
321 | * the memset() above | |
322 | * 4. DMA completes | |
323 | * 5. Buffer now has some DMA data and some zeroes | |
324 | * 6. Data being read is now incorrect | |
325 | * | |
326 | * To prevent this, ensure that the cache is clean | |
327 | * within this range at the start. The driver can then | |
328 | * use normal flush-after-write, invalidate-before-read | |
329 | * procedures. | |
330 | * | |
331 | * TODO([email protected]): Drop this microblaze | |
332 | * exception. | |
333 | */ | |
334 | #ifndef CONFIG_MICROBLAZE | |
335 | flush_dcache_range((ulong)priv, (ulong)priv + size); | |
336 | #endif | |
337 | } | |
2c03c463 SG |
338 | } else { |
339 | priv = calloc(1, size); | |
340 | } | |
341 | ||
342 | return priv; | |
343 | } | |
344 | ||
bcd90cb6 | 345 | int device_ofdata_to_platdata(struct udevice *dev) |
6494d708 | 346 | { |
3479253d | 347 | const struct driver *drv; |
6494d708 SG |
348 | int size = 0; |
349 | int ret; | |
350 | ||
351 | if (!dev) | |
352 | return -EINVAL; | |
353 | ||
153851dd | 354 | if (dev->flags & DM_FLAG_PLATDATA_VALID) |
6494d708 SG |
355 | return 0; |
356 | ||
b0dcc871 SG |
357 | /* Ensure all parents have ofdata */ |
358 | if (dev->parent) { | |
359 | ret = device_ofdata_to_platdata(dev->parent); | |
360 | if (ret) | |
361 | goto fail; | |
362 | ||
363 | /* | |
364 | * The device might have already been probed during | |
365 | * the call to device_probe() on its parent device | |
366 | * (e.g. PCI bridge devices). Test the flags again | |
367 | * so that we don't mess up the device. | |
368 | */ | |
369 | if (dev->flags & DM_FLAG_PLATDATA_VALID) | |
370 | return 0; | |
371 | } | |
372 | ||
6494d708 SG |
373 | drv = dev->driver; |
374 | assert(drv); | |
375 | ||
cdeb2ba9 BM |
376 | /* Allocate private data if requested and not reentered */ |
377 | if (drv->priv_auto_alloc_size && !dev->priv) { | |
2c03c463 | 378 | dev->priv = alloc_priv(drv->priv_auto_alloc_size, drv->flags); |
6494d708 SG |
379 | if (!dev->priv) { |
380 | ret = -ENOMEM; | |
381 | goto fail; | |
382 | } | |
383 | } | |
cdeb2ba9 | 384 | /* Allocate private data if requested and not reentered */ |
6494d708 | 385 | size = dev->uclass->uc_drv->per_device_auto_alloc_size; |
cdeb2ba9 | 386 | if (size && !dev->uclass_priv) { |
c7a3accc SG |
387 | dev->uclass_priv = alloc_priv(size, |
388 | dev->uclass->uc_drv->flags); | |
6494d708 SG |
389 | if (!dev->uclass_priv) { |
390 | ret = -ENOMEM; | |
391 | goto fail; | |
392 | } | |
393 | } | |
394 | ||
82de42fa | 395 | /* Allocate parent data for this child */ |
6494d708 | 396 | if (dev->parent) { |
e59f458d | 397 | size = dev->parent->driver->per_child_auto_alloc_size; |
dac8db2c SG |
398 | if (!size) { |
399 | size = dev->parent->uclass->uc_drv-> | |
400 | per_child_auto_alloc_size; | |
401 | } | |
cdeb2ba9 | 402 | if (size && !dev->parent_priv) { |
2c03c463 | 403 | dev->parent_priv = alloc_priv(size, drv->flags); |
e59f458d SG |
404 | if (!dev->parent_priv) { |
405 | ret = -ENOMEM; | |
406 | goto fail; | |
407 | } | |
408 | } | |
82de42fa SG |
409 | } |
410 | ||
411 | if (drv->ofdata_to_platdata && | |
412 | (CONFIG_IS_ENABLED(OF_PLATDATA) || dev_has_of_node(dev))) { | |
413 | ret = drv->ofdata_to_platdata(dev); | |
414 | if (ret) | |
415 | goto fail; | |
416 | } | |
e59f458d | 417 | |
153851dd SG |
418 | dev->flags |= DM_FLAG_PLATDATA_VALID; |
419 | ||
bcd90cb6 SG |
420 | return 0; |
421 | fail: | |
422 | device_free(dev); | |
423 | ||
424 | return ret; | |
425 | } | |
426 | ||
427 | int device_probe(struct udevice *dev) | |
428 | { | |
429 | const struct driver *drv; | |
430 | int ret; | |
431 | int seq; | |
432 | ||
433 | if (!dev) | |
434 | return -EINVAL; | |
435 | ||
436 | if (dev->flags & DM_FLAG_ACTIVATED) | |
437 | return 0; | |
438 | ||
439 | drv = dev->driver; | |
440 | assert(drv); | |
441 | ||
442 | ret = device_ofdata_to_platdata(dev); | |
443 | if (ret) | |
444 | goto fail; | |
445 | ||
82de42fa SG |
446 | /* Ensure all parents are probed */ |
447 | if (dev->parent) { | |
6494d708 SG |
448 | ret = device_probe(dev->parent); |
449 | if (ret) | |
450 | goto fail; | |
cdeb2ba9 BM |
451 | |
452 | /* | |
453 | * The device might have already been probed during | |
454 | * the call to device_probe() on its parent device | |
455 | * (e.g. PCI bridge devices). Test the flags again | |
456 | * so that we don't mess up the device. | |
457 | */ | |
458 | if (dev->flags & DM_FLAG_ACTIVATED) | |
459 | return 0; | |
6494d708 SG |
460 | } |
461 | ||
5a66a8ff SG |
462 | seq = uclass_resolve_seq(dev); |
463 | if (seq < 0) { | |
464 | ret = seq; | |
465 | goto fail; | |
466 | } | |
467 | dev->seq = seq; | |
468 | ||
206d4d2b SG |
469 | dev->flags |= DM_FLAG_ACTIVATED; |
470 | ||
84d26e29 SG |
471 | /* |
472 | * Process pinctrl for everything except the root device, and | |
0379597e SG |
473 | * continue regardless of the result of pinctrl. Don't process pinctrl |
474 | * settings for pinctrl devices since the device may not yet be | |
475 | * probed. | |
84d26e29 | 476 | */ |
0379597e | 477 | if (dev->parent && device_get_uclass_id(dev) != UCLASS_PINCTRL) |
84d26e29 | 478 | pinctrl_select_state(dev, "default"); |
d90a5a30 | 479 | |
44e02e39 | 480 | if (CONFIG_IS_ENABLED(POWER_DOMAIN) && dev->parent && |
af94ad41 LV |
481 | (device_get_uclass_id(dev) != UCLASS_POWER_DOMAIN) && |
482 | !(drv->flags & DM_FLAG_DEFAULT_PD_CTRL_OFF)) { | |
f0cc4eae PF |
483 | ret = dev_power_domain_on(dev); |
484 | if (ret) | |
485 | goto fail; | |
3ad30778 PF |
486 | } |
487 | ||
02c07b37 | 488 | ret = uclass_pre_probe_device(dev); |
83c7e434 SG |
489 | if (ret) |
490 | goto fail; | |
491 | ||
a327dee0 SG |
492 | if (dev->parent && dev->parent->driver->child_pre_probe) { |
493 | ret = dev->parent->driver->child_pre_probe(dev); | |
494 | if (ret) | |
495 | goto fail; | |
496 | } | |
497 | ||
a1f99e46 BM |
498 | /* Only handle devices that have a valid ofnode */ |
499 | if (dev_of_valid(dev)) { | |
500 | /* | |
501 | * Process 'assigned-{clocks/clock-parents/clock-rates}' | |
502 | * properties | |
503 | */ | |
fd1ba296 | 504 | ret = clk_set_defaults(dev, 0); |
a1f99e46 BM |
505 | if (ret) |
506 | goto fail; | |
507 | } | |
f4fcba5c | 508 | |
6494d708 SG |
509 | if (drv->probe) { |
510 | ret = drv->probe(dev); | |
a41e6daf | 511 | if (ret) |
6494d708 SG |
512 | goto fail; |
513 | } | |
514 | ||
6494d708 | 515 | ret = uclass_post_probe_device(dev); |
206d4d2b | 516 | if (ret) |
6494d708 | 517 | goto fail_uclass; |
6494d708 | 518 | |
c3ab9853 PF |
519 | if (dev->parent && device_get_uclass_id(dev) == UCLASS_PINCTRL) |
520 | pinctrl_select_state(dev, "default"); | |
521 | ||
6494d708 SG |
522 | return 0; |
523 | fail_uclass: | |
706865af | 524 | if (device_remove(dev, DM_REMOVE_NORMAL)) { |
6494d708 SG |
525 | dm_warn("%s: Device '%s' failed to remove on error path\n", |
526 | __func__, dev->name); | |
527 | } | |
528 | fail: | |
206d4d2b SG |
529 | dev->flags &= ~DM_FLAG_ACTIVATED; |
530 | ||
5a66a8ff | 531 | dev->seq = -1; |
6494d708 SG |
532 | device_free(dev); |
533 | ||
534 | return ret; | |
535 | } | |
536 | ||
9f15cc14 | 537 | void *dev_get_platdata(const struct udevice *dev) |
6494d708 SG |
538 | { |
539 | if (!dev) { | |
964d153c | 540 | dm_warn("%s: null device\n", __func__); |
6494d708 SG |
541 | return NULL; |
542 | } | |
543 | ||
544 | return dev->platdata; | |
545 | } | |
546 | ||
9f15cc14 | 547 | void *dev_get_parent_platdata(const struct udevice *dev) |
cdc133bd SG |
548 | { |
549 | if (!dev) { | |
36d7cc17 | 550 | dm_warn("%s: null device\n", __func__); |
cdc133bd SG |
551 | return NULL; |
552 | } | |
553 | ||
554 | return dev->parent_platdata; | |
555 | } | |
556 | ||
9f15cc14 | 557 | void *dev_get_uclass_platdata(const struct udevice *dev) |
5eaed880 PM |
558 | { |
559 | if (!dev) { | |
36d7cc17 | 560 | dm_warn("%s: null device\n", __func__); |
5eaed880 PM |
561 | return NULL; |
562 | } | |
563 | ||
564 | return dev->uclass_platdata; | |
565 | } | |
566 | ||
9f15cc14 | 567 | void *dev_get_priv(const struct udevice *dev) |
6494d708 SG |
568 | { |
569 | if (!dev) { | |
964d153c | 570 | dm_warn("%s: null device\n", __func__); |
6494d708 SG |
571 | return NULL; |
572 | } | |
573 | ||
574 | return dev->priv; | |
575 | } | |
997c87bb | 576 | |
9f15cc14 | 577 | void *dev_get_uclass_priv(const struct udevice *dev) |
e564f054 SG |
578 | { |
579 | if (!dev) { | |
580 | dm_warn("%s: null device\n", __func__); | |
581 | return NULL; | |
582 | } | |
583 | ||
584 | return dev->uclass_priv; | |
585 | } | |
586 | ||
9f15cc14 | 587 | void *dev_get_parent_priv(const struct udevice *dev) |
e59f458d SG |
588 | { |
589 | if (!dev) { | |
964d153c | 590 | dm_warn("%s: null device\n", __func__); |
e59f458d SG |
591 | return NULL; |
592 | } | |
593 | ||
594 | return dev->parent_priv; | |
595 | } | |
596 | ||
997c87bb SG |
597 | static int device_get_device_tail(struct udevice *dev, int ret, |
598 | struct udevice **devp) | |
599 | { | |
600 | if (ret) | |
601 | return ret; | |
602 | ||
603 | ret = device_probe(dev); | |
604 | if (ret) | |
605 | return ret; | |
606 | ||
607 | *devp = dev; | |
608 | ||
609 | return 0; | |
610 | } | |
611 | ||
d7677bfc | 612 | #if CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA) |
e4c98a59 MS |
613 | /** |
614 | * device_find_by_ofnode() - Return device associated with given ofnode | |
615 | * | |
616 | * The returned device is *not* activated. | |
617 | * | |
618 | * @node: The ofnode for which a associated device should be looked up | |
619 | * @devp: Pointer to structure to hold the found device | |
620 | * Return: 0 if OK, -ve on error | |
621 | */ | |
622 | static int device_find_by_ofnode(ofnode node, struct udevice **devp) | |
623 | { | |
624 | struct uclass *uc; | |
625 | struct udevice *dev; | |
626 | int ret; | |
627 | ||
628 | list_for_each_entry(uc, &gd->uclass_root, sibling_node) { | |
629 | ret = uclass_find_device_by_ofnode(uc->uc_drv->id, node, | |
630 | &dev); | |
631 | if (!ret || dev) { | |
632 | *devp = dev; | |
633 | return 0; | |
634 | } | |
635 | } | |
636 | ||
637 | return -ENODEV; | |
638 | } | |
d7677bfc | 639 | #endif |
e4c98a59 | 640 | |
fc347fbd SG |
641 | int device_get_child(const struct udevice *parent, int index, |
642 | struct udevice **devp) | |
997c87bb SG |
643 | { |
644 | struct udevice *dev; | |
645 | ||
646 | list_for_each_entry(dev, &parent->child_head, sibling_node) { | |
647 | if (!index--) | |
648 | return device_get_device_tail(dev, 0, devp); | |
649 | } | |
650 | ||
651 | return -ENODEV; | |
652 | } | |
653 | ||
fc347fbd | 654 | int device_get_child_count(const struct udevice *parent) |
240b9320 LV |
655 | { |
656 | struct udevice *dev; | |
657 | int count = 0; | |
658 | ||
659 | list_for_each_entry(dev, &parent->child_head, sibling_node) | |
660 | count++; | |
661 | ||
662 | return count; | |
663 | } | |
664 | ||
fc347fbd | 665 | int device_find_child_by_seq(const struct udevice *parent, int seq_or_req_seq, |
997c87bb SG |
666 | bool find_req_seq, struct udevice **devp) |
667 | { | |
668 | struct udevice *dev; | |
669 | ||
670 | *devp = NULL; | |
671 | if (seq_or_req_seq == -1) | |
672 | return -ENODEV; | |
673 | ||
674 | list_for_each_entry(dev, &parent->child_head, sibling_node) { | |
675 | if ((find_req_seq ? dev->req_seq : dev->seq) == | |
676 | seq_or_req_seq) { | |
677 | *devp = dev; | |
678 | return 0; | |
679 | } | |
680 | } | |
681 | ||
682 | return -ENODEV; | |
683 | } | |
684 | ||
fc347fbd | 685 | int device_get_child_by_seq(const struct udevice *parent, int seq, |
997c87bb SG |
686 | struct udevice **devp) |
687 | { | |
688 | struct udevice *dev; | |
689 | int ret; | |
690 | ||
691 | *devp = NULL; | |
692 | ret = device_find_child_by_seq(parent, seq, false, &dev); | |
693 | if (ret == -ENODEV) { | |
694 | /* | |
695 | * We didn't find it in probed devices. See if there is one | |
696 | * that will request this seq if probed. | |
697 | */ | |
698 | ret = device_find_child_by_seq(parent, seq, true, &dev); | |
699 | } | |
700 | return device_get_device_tail(dev, ret, devp); | |
701 | } | |
702 | ||
fc347fbd | 703 | int device_find_child_by_of_offset(const struct udevice *parent, int of_offset, |
997c87bb SG |
704 | struct udevice **devp) |
705 | { | |
706 | struct udevice *dev; | |
707 | ||
708 | *devp = NULL; | |
709 | ||
710 | list_for_each_entry(dev, &parent->child_head, sibling_node) { | |
e160f7d4 | 711 | if (dev_of_offset(dev) == of_offset) { |
997c87bb SG |
712 | *devp = dev; |
713 | return 0; | |
714 | } | |
715 | } | |
716 | ||
717 | return -ENODEV; | |
718 | } | |
719 | ||
fc347fbd | 720 | int device_get_child_by_of_offset(const struct udevice *parent, int node, |
997c87bb SG |
721 | struct udevice **devp) |
722 | { | |
723 | struct udevice *dev; | |
724 | int ret; | |
725 | ||
726 | *devp = NULL; | |
132f9bfc | 727 | ret = device_find_child_by_of_offset(parent, node, &dev); |
997c87bb SG |
728 | return device_get_device_tail(dev, ret, devp); |
729 | } | |
a8981d4f | 730 | |
7ec9181d JJH |
731 | static struct udevice *_device_find_global_by_ofnode(struct udevice *parent, |
732 | ofnode ofnode) | |
2693047a SG |
733 | { |
734 | struct udevice *dev, *found; | |
735 | ||
7ec9181d | 736 | if (ofnode_equal(dev_ofnode(parent), ofnode)) |
2693047a SG |
737 | return parent; |
738 | ||
739 | list_for_each_entry(dev, &parent->child_head, sibling_node) { | |
7ec9181d | 740 | found = _device_find_global_by_ofnode(dev, ofnode); |
2693047a SG |
741 | if (found) |
742 | return found; | |
743 | } | |
744 | ||
745 | return NULL; | |
746 | } | |
747 | ||
7ec9181d JJH |
748 | int device_find_global_by_ofnode(ofnode ofnode, struct udevice **devp) |
749 | { | |
750 | *devp = _device_find_global_by_ofnode(gd->dm_root, ofnode); | |
751 | ||
752 | return *devp ? 0 : -ENOENT; | |
753 | } | |
754 | ||
755 | int device_get_global_by_ofnode(ofnode ofnode, struct udevice **devp) | |
2693047a SG |
756 | { |
757 | struct udevice *dev; | |
758 | ||
7ec9181d | 759 | dev = _device_find_global_by_ofnode(gd->dm_root, ofnode); |
2693047a SG |
760 | return device_get_device_tail(dev, dev ? 0 : -ENOENT, devp); |
761 | } | |
762 | ||
fed0f891 WL |
763 | #if CONFIG_IS_ENABLED(OF_PLATDATA) |
764 | int device_get_by_driver_info(const struct driver_info *info, | |
765 | struct udevice **devp) | |
766 | { | |
767 | struct udevice *dev; | |
768 | ||
769 | dev = info->dev; | |
770 | ||
771 | return device_get_device_tail(dev, dev ? 0 : -ENOENT, devp); | |
772 | } | |
773 | #endif | |
774 | ||
fc347fbd | 775 | int device_find_first_child(const struct udevice *parent, struct udevice **devp) |
a8981d4f SG |
776 | { |
777 | if (list_empty(&parent->child_head)) { | |
778 | *devp = NULL; | |
779 | } else { | |
780 | *devp = list_first_entry(&parent->child_head, struct udevice, | |
781 | sibling_node); | |
782 | } | |
783 | ||
784 | return 0; | |
785 | } | |
786 | ||
787 | int device_find_next_child(struct udevice **devp) | |
788 | { | |
789 | struct udevice *dev = *devp; | |
790 | struct udevice *parent = dev->parent; | |
791 | ||
792 | if (list_is_last(&dev->sibling_node, &parent->child_head)) { | |
793 | *devp = NULL; | |
794 | } else { | |
795 | *devp = list_entry(dev->sibling_node.next, struct udevice, | |
796 | sibling_node); | |
797 | } | |
798 | ||
799 | return 0; | |
800 | } | |
2ef249b4 | 801 | |
fc347fbd | 802 | int device_find_first_inactive_child(const struct udevice *parent, |
cdb6aa0a SG |
803 | enum uclass_id uclass_id, |
804 | struct udevice **devp) | |
805 | { | |
806 | struct udevice *dev; | |
807 | ||
808 | *devp = NULL; | |
809 | list_for_each_entry(dev, &parent->child_head, sibling_node) { | |
810 | if (!device_active(dev) && | |
811 | device_get_uclass_id(dev) == uclass_id) { | |
812 | *devp = dev; | |
813 | return 0; | |
814 | } | |
815 | } | |
816 | ||
817 | return -ENODEV; | |
818 | } | |
819 | ||
fc347fbd | 820 | int device_find_first_child_by_uclass(const struct udevice *parent, |
3abe1115 SG |
821 | enum uclass_id uclass_id, |
822 | struct udevice **devp) | |
823 | { | |
824 | struct udevice *dev; | |
825 | ||
826 | *devp = NULL; | |
827 | list_for_each_entry(dev, &parent->child_head, sibling_node) { | |
828 | if (device_get_uclass_id(dev) == uclass_id) { | |
829 | *devp = dev; | |
830 | return 0; | |
831 | } | |
832 | } | |
833 | ||
834 | return -ENODEV; | |
835 | } | |
836 | ||
fc347fbd | 837 | int device_find_child_by_name(const struct udevice *parent, const char *name, |
3abe1115 SG |
838 | struct udevice **devp) |
839 | { | |
840 | struct udevice *dev; | |
841 | ||
842 | *devp = NULL; | |
843 | ||
844 | list_for_each_entry(dev, &parent->child_head, sibling_node) { | |
845 | if (!strcmp(dev->name, name)) { | |
846 | *devp = dev; | |
847 | return 0; | |
848 | } | |
849 | } | |
850 | ||
851 | return -ENODEV; | |
852 | } | |
853 | ||
903e83ee SG |
854 | int device_first_child_err(struct udevice *parent, struct udevice **devp) |
855 | { | |
856 | struct udevice *dev; | |
857 | ||
858 | device_find_first_child(parent, &dev); | |
859 | if (!dev) | |
860 | return -ENODEV; | |
861 | ||
862 | return device_get_device_tail(dev, 0, devp); | |
863 | } | |
864 | ||
865 | int device_next_child_err(struct udevice **devp) | |
866 | { | |
867 | struct udevice *dev = *devp; | |
868 | ||
869 | device_find_next_child(&dev); | |
870 | if (!dev) | |
871 | return -ENODEV; | |
872 | ||
873 | return device_get_device_tail(dev, 0, devp); | |
874 | } | |
875 | ||
f262d4ca SG |
876 | int device_first_child_ofdata_err(struct udevice *parent, struct udevice **devp) |
877 | { | |
878 | struct udevice *dev; | |
879 | int ret; | |
880 | ||
881 | device_find_first_child(parent, &dev); | |
882 | if (!dev) | |
883 | return -ENODEV; | |
884 | ||
885 | ret = device_ofdata_to_platdata(dev); | |
886 | if (ret) | |
887 | return ret; | |
888 | ||
889 | *devp = dev; | |
890 | ||
891 | return 0; | |
892 | } | |
893 | ||
894 | int device_next_child_ofdata_err(struct udevice **devp) | |
895 | { | |
896 | struct udevice *dev = *devp; | |
897 | int ret; | |
898 | ||
899 | device_find_next_child(&dev); | |
900 | if (!dev) | |
901 | return -ENODEV; | |
902 | ||
903 | ret = device_ofdata_to_platdata(dev); | |
904 | if (ret) | |
905 | return ret; | |
906 | ||
907 | *devp = dev; | |
908 | ||
909 | return 0; | |
910 | } | |
911 | ||
9f15cc14 | 912 | struct udevice *dev_get_parent(const struct udevice *child) |
479728cb SG |
913 | { |
914 | return child->parent; | |
915 | } | |
916 | ||
9f15cc14 | 917 | ulong dev_get_driver_data(const struct udevice *dev) |
2ef249b4 | 918 | { |
39de8433 | 919 | return dev->driver_data; |
2ef249b4 | 920 | } |
b3670531 | 921 | |
9f15cc14 | 922 | const void *dev_get_driver_ops(const struct udevice *dev) |
cc73d37b PM |
923 | { |
924 | if (!dev || !dev->driver->ops) | |
925 | return NULL; | |
926 | ||
927 | return dev->driver->ops; | |
928 | } | |
929 | ||
9f15cc14 | 930 | enum uclass_id device_get_uclass_id(const struct udevice *dev) |
b3670531 SG |
931 | { |
932 | return dev->uclass->uc_drv->id; | |
933 | } | |
c9cac3f8 | 934 | |
9f15cc14 | 935 | const char *dev_get_uclass_name(const struct udevice *dev) |
f9c370dc PM |
936 | { |
937 | if (!dev) | |
938 | return NULL; | |
939 | ||
940 | return dev->uclass->uc_drv->name; | |
941 | } | |
942 | ||
9f15cc14 | 943 | bool device_has_children(const struct udevice *dev) |
c5785673 SG |
944 | { |
945 | return !list_empty(&dev->child_head); | |
946 | } | |
947 | ||
fc347fbd | 948 | bool device_has_active_children(const struct udevice *dev) |
c5785673 SG |
949 | { |
950 | struct udevice *child; | |
951 | ||
952 | for (device_find_first_child(dev, &child); | |
953 | child; | |
954 | device_find_next_child(&child)) { | |
955 | if (device_active(child)) | |
956 | return true; | |
957 | } | |
958 | ||
959 | return false; | |
960 | } | |
961 | ||
fc347fbd | 962 | bool device_is_last_sibling(const struct udevice *dev) |
c5785673 SG |
963 | { |
964 | struct udevice *parent = dev->parent; | |
965 | ||
966 | if (!parent) | |
967 | return false; | |
968 | return list_is_last(&dev->sibling_node, &parent->child_head); | |
969 | } | |
f5c67ea0 | 970 | |
a2040fac SG |
971 | void device_set_name_alloced(struct udevice *dev) |
972 | { | |
fd1c2d9b | 973 | dev->flags |= DM_FLAG_NAME_ALLOCED; |
a2040fac SG |
974 | } |
975 | ||
f5c67ea0 SG |
976 | int device_set_name(struct udevice *dev, const char *name) |
977 | { | |
978 | name = strdup(name); | |
979 | if (!name) | |
980 | return -ENOMEM; | |
981 | dev->name = name; | |
a2040fac | 982 | device_set_name_alloced(dev); |
f5c67ea0 SG |
983 | |
984 | return 0; | |
985 | } | |
73443b9e | 986 | |
d7677bfc | 987 | #if CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA) |
fc347fbd | 988 | bool device_is_compatible(const struct udevice *dev, const char *compat) |
73443b9e | 989 | { |
5ccc2c21 | 990 | return ofnode_device_is_compatible(dev_ofnode(dev), compat); |
73443b9e M |
991 | } |
992 | ||
993 | bool of_machine_is_compatible(const char *compat) | |
994 | { | |
995 | const void *fdt = gd->fdt_blob; | |
996 | ||
997 | return !fdt_node_check_compatible(fdt, 0, compat); | |
998 | } | |
e4c98a59 MS |
999 | |
1000 | int dev_disable_by_path(const char *path) | |
1001 | { | |
1002 | struct uclass *uc; | |
1003 | ofnode node = ofnode_path(path); | |
1004 | struct udevice *dev; | |
1005 | int ret = 1; | |
1006 | ||
1007 | if (!of_live_active()) | |
1008 | return -ENOSYS; | |
1009 | ||
1010 | list_for_each_entry(uc, &gd->uclass_root, sibling_node) { | |
1011 | ret = uclass_find_device_by_ofnode(uc->uc_drv->id, node, &dev); | |
1012 | if (!ret) | |
1013 | break; | |
1014 | } | |
1015 | ||
1016 | if (ret) | |
1017 | return ret; | |
1018 | ||
1019 | ret = device_remove(dev, DM_REMOVE_NORMAL); | |
1020 | if (ret) | |
1021 | return ret; | |
1022 | ||
1023 | ret = device_unbind(dev); | |
1024 | if (ret) | |
1025 | return ret; | |
1026 | ||
1027 | return ofnode_set_enabled(node, false); | |
1028 | } | |
1029 | ||
1030 | int dev_enable_by_path(const char *path) | |
1031 | { | |
1032 | ofnode node = ofnode_path(path); | |
1033 | ofnode pnode = ofnode_get_parent(node); | |
1034 | struct udevice *parent; | |
1035 | int ret = 1; | |
1036 | ||
1037 | if (!of_live_active()) | |
1038 | return -ENOSYS; | |
1039 | ||
1040 | ret = device_find_by_ofnode(pnode, &parent); | |
1041 | if (ret) | |
1042 | return ret; | |
1043 | ||
1044 | ret = ofnode_set_enabled(node, true); | |
1045 | if (ret) | |
1046 | return ret; | |
1047 | ||
8d773c4a | 1048 | return lists_bind_fdt(parent, node, NULL, false); |
e4c98a59 | 1049 | } |
d7677bfc | 1050 | #endif |