]>
Commit | Line | Data |
---|---|---|
6494d708 SG |
1 | /* |
2 | * Device manager | |
3 | * | |
4 | * Copyright (c) 2013 Google, Inc | |
5 | * | |
6 | * (C) Copyright 2012 | |
7 | * Pavel Herrmann <[email protected]> | |
8 | * | |
9 | * SPDX-License-Identifier: GPL-2.0+ | |
10 | */ | |
11 | ||
12 | #include <common.h> | |
5a66a8ff | 13 | #include <fdtdec.h> |
6494d708 SG |
14 | #include <malloc.h> |
15 | #include <dm/device.h> | |
16 | #include <dm/device-internal.h> | |
17 | #include <dm/lists.h> | |
18 | #include <dm/platdata.h> | |
19 | #include <dm/uclass.h> | |
20 | #include <dm/uclass-internal.h> | |
21 | #include <dm/util.h> | |
22 | #include <linux/err.h> | |
23 | #include <linux/list.h> | |
24 | ||
5a66a8ff SG |
25 | DECLARE_GLOBAL_DATA_PTR; |
26 | ||
6494d708 SG |
27 | /** |
28 | * device_chld_unbind() - Unbind all device's children from the device | |
29 | * | |
30 | * On error, the function continues to unbind all children, and reports the | |
31 | * first error. | |
32 | * | |
33 | * @dev: The device that is to be stripped of its children | |
34 | * @return 0 on success, -ve on error | |
35 | */ | |
54c5d08a | 36 | static int device_chld_unbind(struct udevice *dev) |
6494d708 | 37 | { |
54c5d08a | 38 | struct udevice *pos, *n; |
6494d708 SG |
39 | int ret, saved_ret = 0; |
40 | ||
41 | assert(dev); | |
42 | ||
43 | list_for_each_entry_safe(pos, n, &dev->child_head, sibling_node) { | |
44 | ret = device_unbind(pos); | |
45 | if (ret && !saved_ret) | |
46 | saved_ret = ret; | |
47 | } | |
48 | ||
49 | return saved_ret; | |
50 | } | |
51 | ||
52 | /** | |
53 | * device_chld_remove() - Stop all device's children | |
54 | * @dev: The device whose children are to be removed | |
55 | * @return 0 on success, -ve on error | |
56 | */ | |
54c5d08a | 57 | static int device_chld_remove(struct udevice *dev) |
6494d708 | 58 | { |
54c5d08a | 59 | struct udevice *pos, *n; |
6494d708 SG |
60 | int ret; |
61 | ||
62 | assert(dev); | |
63 | ||
64 | list_for_each_entry_safe(pos, n, &dev->child_head, sibling_node) { | |
65 | ret = device_remove(pos); | |
66 | if (ret) | |
67 | return ret; | |
68 | } | |
69 | ||
70 | return 0; | |
71 | } | |
72 | ||
54c5d08a HS |
73 | int device_bind(struct udevice *parent, struct driver *drv, const char *name, |
74 | void *platdata, int of_offset, struct udevice **devp) | |
6494d708 | 75 | { |
54c5d08a | 76 | struct udevice *dev; |
6494d708 SG |
77 | struct uclass *uc; |
78 | int ret = 0; | |
79 | ||
80 | *devp = NULL; | |
81 | if (!name) | |
82 | return -EINVAL; | |
83 | ||
84 | ret = uclass_get(drv->id, &uc); | |
85 | if (ret) | |
86 | return ret; | |
87 | ||
54c5d08a | 88 | dev = calloc(1, sizeof(struct udevice)); |
6494d708 SG |
89 | if (!dev) |
90 | return -ENOMEM; | |
91 | ||
92 | INIT_LIST_HEAD(&dev->sibling_node); | |
93 | INIT_LIST_HEAD(&dev->child_head); | |
94 | INIT_LIST_HEAD(&dev->uclass_node); | |
95 | dev->platdata = platdata; | |
96 | dev->name = name; | |
97 | dev->of_offset = of_offset; | |
98 | dev->parent = parent; | |
99 | dev->driver = drv; | |
100 | dev->uclass = uc; | |
5a66a8ff SG |
101 | |
102 | /* | |
103 | * For some devices, such as a SPI or I2C bus, the 'reg' property | |
104 | * is a reasonable indicator of the sequence number. But if there is | |
105 | * an alias, we use that in preference. In any case, this is just | |
106 | * a 'requested' sequence, and will be resolved (and ->seq updated) | |
107 | * when the device is probed. | |
108 | */ | |
5a66a8ff | 109 | dev->seq = -1; |
91cbd792 SG |
110 | #ifdef CONFIG_OF_CONTROL |
111 | dev->req_seq = fdtdec_get_int(gd->fdt_blob, of_offset, "reg", -1); | |
cae025aa RB |
112 | if (!IS_ERR_VALUE(dev->req_seq)) |
113 | dev->req_seq &= INT_MAX; | |
5a66a8ff SG |
114 | if (uc->uc_drv->name && of_offset != -1) { |
115 | fdtdec_get_alias_seq(gd->fdt_blob, uc->uc_drv->name, of_offset, | |
116 | &dev->req_seq); | |
117 | } | |
91cbd792 SG |
118 | #else |
119 | dev->req_seq = -1; | |
120 | #endif | |
6494d708 SG |
121 | if (!dev->platdata && drv->platdata_auto_alloc_size) |
122 | dev->flags |= DM_FLAG_ALLOC_PDATA; | |
123 | ||
124 | /* put dev into parent's successor list */ | |
125 | if (parent) | |
126 | list_add_tail(&dev->sibling_node, &parent->child_head); | |
127 | ||
128 | ret = uclass_bind_device(dev); | |
129 | if (ret) | |
130 | goto fail_bind; | |
131 | ||
132 | /* if we fail to bind we remove device from successors and free it */ | |
133 | if (drv->bind) { | |
134 | ret = drv->bind(dev); | |
135 | if (ret) { | |
136 | if (uclass_unbind_device(dev)) { | |
137 | dm_warn("Failed to unbind dev '%s' on error path\n", | |
138 | dev->name); | |
139 | } | |
140 | goto fail_bind; | |
141 | } | |
142 | } | |
143 | if (parent) | |
144 | dm_dbg("Bound device %s to %s\n", dev->name, parent->name); | |
145 | *devp = dev; | |
146 | ||
147 | return 0; | |
148 | ||
149 | fail_bind: | |
150 | list_del(&dev->sibling_node); | |
151 | free(dev); | |
152 | return ret; | |
153 | } | |
154 | ||
00606d7e SG |
155 | int device_bind_by_name(struct udevice *parent, bool pre_reloc_only, |
156 | const struct driver_info *info, struct udevice **devp) | |
6494d708 SG |
157 | { |
158 | struct driver *drv; | |
159 | ||
160 | drv = lists_driver_lookup_name(info->name); | |
161 | if (!drv) | |
162 | return -ENOENT; | |
00606d7e SG |
163 | if (pre_reloc_only && !(drv->flags & DM_FLAG_PRE_RELOC)) |
164 | return -EPERM; | |
6494d708 SG |
165 | |
166 | return device_bind(parent, drv, info->name, (void *)info->platdata, | |
167 | -1, devp); | |
168 | } | |
169 | ||
54c5d08a | 170 | int device_unbind(struct udevice *dev) |
6494d708 SG |
171 | { |
172 | struct driver *drv; | |
173 | int ret; | |
174 | ||
175 | if (!dev) | |
176 | return -EINVAL; | |
177 | ||
178 | if (dev->flags & DM_FLAG_ACTIVATED) | |
179 | return -EINVAL; | |
180 | ||
181 | drv = dev->driver; | |
182 | assert(drv); | |
183 | ||
184 | if (drv->unbind) { | |
185 | ret = drv->unbind(dev); | |
186 | if (ret) | |
187 | return ret; | |
188 | } | |
189 | ||
190 | ret = device_chld_unbind(dev); | |
191 | if (ret) | |
192 | return ret; | |
193 | ||
194 | ret = uclass_unbind_device(dev); | |
195 | if (ret) | |
196 | return ret; | |
197 | ||
198 | if (dev->parent) | |
199 | list_del(&dev->sibling_node); | |
200 | free(dev); | |
201 | ||
202 | return 0; | |
203 | } | |
204 | ||
205 | /** | |
206 | * device_free() - Free memory buffers allocated by a device | |
207 | * @dev: Device that is to be started | |
208 | */ | |
54c5d08a | 209 | static void device_free(struct udevice *dev) |
6494d708 SG |
210 | { |
211 | int size; | |
212 | ||
213 | if (dev->driver->priv_auto_alloc_size) { | |
214 | free(dev->priv); | |
215 | dev->priv = NULL; | |
216 | } | |
217 | if (dev->flags & DM_FLAG_ALLOC_PDATA) { | |
218 | free(dev->platdata); | |
219 | dev->platdata = NULL; | |
220 | } | |
221 | size = dev->uclass->uc_drv->per_device_auto_alloc_size; | |
222 | if (size) { | |
223 | free(dev->uclass_priv); | |
224 | dev->uclass_priv = NULL; | |
225 | } | |
e59f458d SG |
226 | if (dev->parent) { |
227 | size = dev->parent->driver->per_child_auto_alloc_size; | |
228 | if (size) { | |
229 | free(dev->parent_priv); | |
230 | dev->parent_priv = NULL; | |
231 | } | |
232 | } | |
6494d708 SG |
233 | } |
234 | ||
54c5d08a | 235 | int device_probe(struct udevice *dev) |
6494d708 SG |
236 | { |
237 | struct driver *drv; | |
238 | int size = 0; | |
239 | int ret; | |
5a66a8ff | 240 | int seq; |
6494d708 SG |
241 | |
242 | if (!dev) | |
243 | return -EINVAL; | |
244 | ||
245 | if (dev->flags & DM_FLAG_ACTIVATED) | |
246 | return 0; | |
247 | ||
248 | drv = dev->driver; | |
249 | assert(drv); | |
250 | ||
251 | /* Allocate private data and platdata if requested */ | |
252 | if (drv->priv_auto_alloc_size) { | |
253 | dev->priv = calloc(1, drv->priv_auto_alloc_size); | |
254 | if (!dev->priv) { | |
255 | ret = -ENOMEM; | |
256 | goto fail; | |
257 | } | |
258 | } | |
259 | /* Allocate private data if requested */ | |
260 | if (dev->flags & DM_FLAG_ALLOC_PDATA) { | |
261 | dev->platdata = calloc(1, drv->platdata_auto_alloc_size); | |
262 | if (!dev->platdata) { | |
263 | ret = -ENOMEM; | |
264 | goto fail; | |
265 | } | |
266 | } | |
267 | size = dev->uclass->uc_drv->per_device_auto_alloc_size; | |
268 | if (size) { | |
269 | dev->uclass_priv = calloc(1, size); | |
270 | if (!dev->uclass_priv) { | |
271 | ret = -ENOMEM; | |
272 | goto fail; | |
273 | } | |
274 | } | |
275 | ||
276 | /* Ensure all parents are probed */ | |
277 | if (dev->parent) { | |
e59f458d SG |
278 | size = dev->parent->driver->per_child_auto_alloc_size; |
279 | if (size) { | |
280 | dev->parent_priv = calloc(1, size); | |
281 | if (!dev->parent_priv) { | |
282 | ret = -ENOMEM; | |
283 | goto fail; | |
284 | } | |
285 | } | |
286 | ||
6494d708 SG |
287 | ret = device_probe(dev->parent); |
288 | if (ret) | |
289 | goto fail; | |
290 | } | |
291 | ||
5a66a8ff SG |
292 | seq = uclass_resolve_seq(dev); |
293 | if (seq < 0) { | |
294 | ret = seq; | |
295 | goto fail; | |
296 | } | |
297 | dev->seq = seq; | |
298 | ||
a327dee0 SG |
299 | if (dev->parent && dev->parent->driver->child_pre_probe) { |
300 | ret = dev->parent->driver->child_pre_probe(dev); | |
301 | if (ret) | |
302 | goto fail; | |
303 | } | |
304 | ||
6494d708 SG |
305 | if (drv->ofdata_to_platdata && dev->of_offset >= 0) { |
306 | ret = drv->ofdata_to_platdata(dev); | |
307 | if (ret) | |
308 | goto fail; | |
309 | } | |
310 | ||
311 | if (drv->probe) { | |
312 | ret = drv->probe(dev); | |
313 | if (ret) | |
314 | goto fail; | |
315 | } | |
316 | ||
317 | dev->flags |= DM_FLAG_ACTIVATED; | |
318 | ||
319 | ret = uclass_post_probe_device(dev); | |
320 | if (ret) { | |
321 | dev->flags &= ~DM_FLAG_ACTIVATED; | |
322 | goto fail_uclass; | |
323 | } | |
324 | ||
325 | return 0; | |
326 | fail_uclass: | |
327 | if (device_remove(dev)) { | |
328 | dm_warn("%s: Device '%s' failed to remove on error path\n", | |
329 | __func__, dev->name); | |
330 | } | |
331 | fail: | |
5a66a8ff | 332 | dev->seq = -1; |
6494d708 SG |
333 | device_free(dev); |
334 | ||
335 | return ret; | |
336 | } | |
337 | ||
54c5d08a | 338 | int device_remove(struct udevice *dev) |
6494d708 SG |
339 | { |
340 | struct driver *drv; | |
341 | int ret; | |
342 | ||
343 | if (!dev) | |
344 | return -EINVAL; | |
345 | ||
346 | if (!(dev->flags & DM_FLAG_ACTIVATED)) | |
347 | return 0; | |
348 | ||
349 | drv = dev->driver; | |
350 | assert(drv); | |
351 | ||
352 | ret = uclass_pre_remove_device(dev); | |
353 | if (ret) | |
354 | return ret; | |
355 | ||
356 | ret = device_chld_remove(dev); | |
357 | if (ret) | |
358 | goto err; | |
359 | ||
360 | if (drv->remove) { | |
361 | ret = drv->remove(dev); | |
362 | if (ret) | |
363 | goto err_remove; | |
364 | } | |
365 | ||
a327dee0 SG |
366 | if (dev->parent && dev->parent->driver->child_post_remove) { |
367 | ret = dev->parent->driver->child_post_remove(dev); | |
368 | if (ret) { | |
369 | dm_warn("%s: Device '%s' failed child_post_remove()", | |
370 | __func__, dev->name); | |
371 | } | |
372 | } | |
373 | ||
6494d708 SG |
374 | device_free(dev); |
375 | ||
5a66a8ff | 376 | dev->seq = -1; |
6494d708 SG |
377 | dev->flags &= ~DM_FLAG_ACTIVATED; |
378 | ||
a327dee0 | 379 | return ret; |
6494d708 SG |
380 | |
381 | err_remove: | |
382 | /* We can't put the children back */ | |
383 | dm_warn("%s: Device '%s' failed to remove, but children are gone\n", | |
384 | __func__, dev->name); | |
385 | err: | |
386 | ret = uclass_post_probe_device(dev); | |
387 | if (ret) { | |
388 | dm_warn("%s: Device '%s' failed to post_probe on error path\n", | |
389 | __func__, dev->name); | |
390 | } | |
391 | ||
392 | return ret; | |
393 | } | |
394 | ||
54c5d08a | 395 | void *dev_get_platdata(struct udevice *dev) |
6494d708 SG |
396 | { |
397 | if (!dev) { | |
398 | dm_warn("%s: null device", __func__); | |
399 | return NULL; | |
400 | } | |
401 | ||
402 | return dev->platdata; | |
403 | } | |
404 | ||
54c5d08a | 405 | void *dev_get_priv(struct udevice *dev) |
6494d708 SG |
406 | { |
407 | if (!dev) { | |
408 | dm_warn("%s: null device", __func__); | |
409 | return NULL; | |
410 | } | |
411 | ||
412 | return dev->priv; | |
413 | } | |
997c87bb | 414 | |
e59f458d SG |
415 | void *dev_get_parentdata(struct udevice *dev) |
416 | { | |
417 | if (!dev) { | |
418 | dm_warn("%s: null device", __func__); | |
419 | return NULL; | |
420 | } | |
421 | ||
422 | return dev->parent_priv; | |
423 | } | |
424 | ||
997c87bb SG |
425 | static int device_get_device_tail(struct udevice *dev, int ret, |
426 | struct udevice **devp) | |
427 | { | |
428 | if (ret) | |
429 | return ret; | |
430 | ||
431 | ret = device_probe(dev); | |
432 | if (ret) | |
433 | return ret; | |
434 | ||
435 | *devp = dev; | |
436 | ||
437 | return 0; | |
438 | } | |
439 | ||
440 | int device_get_child(struct udevice *parent, int index, struct udevice **devp) | |
441 | { | |
442 | struct udevice *dev; | |
443 | ||
444 | list_for_each_entry(dev, &parent->child_head, sibling_node) { | |
445 | if (!index--) | |
446 | return device_get_device_tail(dev, 0, devp); | |
447 | } | |
448 | ||
449 | return -ENODEV; | |
450 | } | |
451 | ||
452 | int device_find_child_by_seq(struct udevice *parent, int seq_or_req_seq, | |
453 | bool find_req_seq, struct udevice **devp) | |
454 | { | |
455 | struct udevice *dev; | |
456 | ||
457 | *devp = NULL; | |
458 | if (seq_or_req_seq == -1) | |
459 | return -ENODEV; | |
460 | ||
461 | list_for_each_entry(dev, &parent->child_head, sibling_node) { | |
462 | if ((find_req_seq ? dev->req_seq : dev->seq) == | |
463 | seq_or_req_seq) { | |
464 | *devp = dev; | |
465 | return 0; | |
466 | } | |
467 | } | |
468 | ||
469 | return -ENODEV; | |
470 | } | |
471 | ||
472 | int device_get_child_by_seq(struct udevice *parent, int seq, | |
473 | struct udevice **devp) | |
474 | { | |
475 | struct udevice *dev; | |
476 | int ret; | |
477 | ||
478 | *devp = NULL; | |
479 | ret = device_find_child_by_seq(parent, seq, false, &dev); | |
480 | if (ret == -ENODEV) { | |
481 | /* | |
482 | * We didn't find it in probed devices. See if there is one | |
483 | * that will request this seq if probed. | |
484 | */ | |
485 | ret = device_find_child_by_seq(parent, seq, true, &dev); | |
486 | } | |
487 | return device_get_device_tail(dev, ret, devp); | |
488 | } | |
489 | ||
490 | int device_find_child_by_of_offset(struct udevice *parent, int of_offset, | |
491 | struct udevice **devp) | |
492 | { | |
493 | struct udevice *dev; | |
494 | ||
495 | *devp = NULL; | |
496 | ||
497 | list_for_each_entry(dev, &parent->child_head, sibling_node) { | |
498 | if (dev->of_offset == of_offset) { | |
499 | *devp = dev; | |
500 | return 0; | |
501 | } | |
502 | } | |
503 | ||
504 | return -ENODEV; | |
505 | } | |
506 | ||
507 | int device_get_child_by_of_offset(struct udevice *parent, int seq, | |
508 | struct udevice **devp) | |
509 | { | |
510 | struct udevice *dev; | |
511 | int ret; | |
512 | ||
513 | *devp = NULL; | |
514 | ret = device_find_child_by_of_offset(parent, seq, &dev); | |
515 | return device_get_device_tail(dev, ret, devp); | |
516 | } |