blob: 2f33b0e6a3b1bf87e532e777a901564231fdd71d [file] [log] [blame]
Simon Glassdd6ab882014-02-26 15:59:18 -07001/*
2 * Device manager
3 *
4 * Copyright (c) 2013 Google, Inc
5 *
6 * (C) Copyright 2012
7 * Pavel Herrmann <morpheus.ibis@gmail.com>
8 *
9 * SPDX-License-Identifier: GPL-2.0+
10 */
11
12#include <common.h>
Simon Glassdb6f0202014-07-23 06:55:12 -060013#include <fdtdec.h>
Simon Glassdd6ab882014-02-26 15:59:18 -070014#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
Simon Glassdb6f0202014-07-23 06:55:12 -060025DECLARE_GLOBAL_DATA_PTR;
26
Heiko Schocherb74fcb42014-05-22 12:43:05 +020027int device_bind(struct udevice *parent, struct driver *drv, const char *name,
28 void *platdata, int of_offset, struct udevice **devp)
Simon Glassdd6ab882014-02-26 15:59:18 -070029{
Heiko Schocherb74fcb42014-05-22 12:43:05 +020030 struct udevice *dev;
Simon Glassdd6ab882014-02-26 15:59:18 -070031 struct uclass *uc;
32 int ret = 0;
33
34 *devp = NULL;
35 if (!name)
36 return -EINVAL;
37
38 ret = uclass_get(drv->id, &uc);
39 if (ret)
40 return ret;
41
Heiko Schocherb74fcb42014-05-22 12:43:05 +020042 dev = calloc(1, sizeof(struct udevice));
Simon Glassdd6ab882014-02-26 15:59:18 -070043 if (!dev)
44 return -ENOMEM;
45
46 INIT_LIST_HEAD(&dev->sibling_node);
47 INIT_LIST_HEAD(&dev->child_head);
48 INIT_LIST_HEAD(&dev->uclass_node);
49 dev->platdata = platdata;
50 dev->name = name;
51 dev->of_offset = of_offset;
52 dev->parent = parent;
53 dev->driver = drv;
54 dev->uclass = uc;
Simon Glassdb6f0202014-07-23 06:55:12 -060055
56 /*
57 * For some devices, such as a SPI or I2C bus, the 'reg' property
58 * is a reasonable indicator of the sequence number. But if there is
59 * an alias, we use that in preference. In any case, this is just
60 * a 'requested' sequence, and will be resolved (and ->seq updated)
61 * when the device is probed.
62 */
Simon Glassdb6f0202014-07-23 06:55:12 -060063 dev->seq = -1;
Simon Glassc2ac1412014-09-17 09:02:38 -060064#ifdef CONFIG_OF_CONTROL
65 dev->req_seq = fdtdec_get_int(gd->fdt_blob, of_offset, "reg", -1);
Robert Baldyga2e9edbf2014-09-18 17:13:07 +020066 if (!IS_ERR_VALUE(dev->req_seq))
67 dev->req_seq &= INT_MAX;
Simon Glassdb6f0202014-07-23 06:55:12 -060068 if (uc->uc_drv->name && of_offset != -1) {
69 fdtdec_get_alias_seq(gd->fdt_blob, uc->uc_drv->name, of_offset,
70 &dev->req_seq);
71 }
Simon Glassc2ac1412014-09-17 09:02:38 -060072#else
73 dev->req_seq = -1;
74#endif
Simon Glassba750492015-01-25 08:27:00 -070075 if (!dev->platdata && drv->platdata_auto_alloc_size) {
Simon Glassdd6ab882014-02-26 15:59:18 -070076 dev->flags |= DM_FLAG_ALLOC_PDATA;
Simon Glassba750492015-01-25 08:27:00 -070077 dev->platdata = calloc(1, drv->platdata_auto_alloc_size);
78 if (!dev->platdata) {
79 ret = -ENOMEM;
80 goto fail_alloc1;
81 }
82 }
Simon Glass11b61732015-01-25 08:27:01 -070083 if (parent) {
84 int size = parent->driver->per_child_platdata_auto_alloc_size;
85
Simon Glass57f95402015-01-25 08:27:02 -070086 if (!size) {
87 size = parent->uclass->uc_drv->
88 per_child_platdata_auto_alloc_size;
89 }
Simon Glass11b61732015-01-25 08:27:01 -070090 if (size) {
91 dev->flags |= DM_FLAG_ALLOC_PARENT_PDATA;
92 dev->parent_platdata = calloc(1, size);
93 if (!dev->parent_platdata) {
94 ret = -ENOMEM;
95 goto fail_alloc2;
96 }
97 }
98 }
Simon Glassdd6ab882014-02-26 15:59:18 -070099
100 /* put dev into parent's successor list */
101 if (parent)
102 list_add_tail(&dev->sibling_node, &parent->child_head);
103
104 ret = uclass_bind_device(dev);
105 if (ret)
Simon Glass4ebe01b2015-01-25 08:26:59 -0700106 goto fail_uclass_bind;
Simon Glassdd6ab882014-02-26 15:59:18 -0700107
108 /* if we fail to bind we remove device from successors and free it */
109 if (drv->bind) {
110 ret = drv->bind(dev);
Simon Glass4ebe01b2015-01-25 08:26:59 -0700111 if (ret)
Simon Glassdd6ab882014-02-26 15:59:18 -0700112 goto fail_bind;
Simon Glassdd6ab882014-02-26 15:59:18 -0700113 }
114 if (parent)
115 dm_dbg("Bound device %s to %s\n", dev->name, parent->name);
116 *devp = dev;
117
118 return 0;
119
120fail_bind:
Simon Glass4ebe01b2015-01-25 08:26:59 -0700121 if (uclass_unbind_device(dev)) {
122 dm_warn("Failed to unbind dev '%s' on error path\n",
123 dev->name);
124 }
125fail_uclass_bind:
Simon Glass11b61732015-01-25 08:27:01 -0700126 list_del(&dev->sibling_node);
127 if (dev->flags & DM_FLAG_ALLOC_PARENT_PDATA) {
128 free(dev->parent_platdata);
129 dev->parent_platdata = NULL;
130 }
131fail_alloc2:
Simon Glassba750492015-01-25 08:27:00 -0700132 if (dev->flags & DM_FLAG_ALLOC_PDATA) {
133 free(dev->platdata);
134 dev->platdata = NULL;
135 }
136fail_alloc1:
Simon Glassdd6ab882014-02-26 15:59:18 -0700137 free(dev);
Simon Glass4ebe01b2015-01-25 08:26:59 -0700138
Simon Glassdd6ab882014-02-26 15:59:18 -0700139 return ret;
140}
141
Simon Glassfef72b72014-07-23 06:55:03 -0600142int device_bind_by_name(struct udevice *parent, bool pre_reloc_only,
143 const struct driver_info *info, struct udevice **devp)
Simon Glassdd6ab882014-02-26 15:59:18 -0700144{
145 struct driver *drv;
146
147 drv = lists_driver_lookup_name(info->name);
148 if (!drv)
149 return -ENOENT;
Simon Glassfef72b72014-07-23 06:55:03 -0600150 if (pre_reloc_only && !(drv->flags & DM_FLAG_PRE_RELOC))
151 return -EPERM;
Simon Glassdd6ab882014-02-26 15:59:18 -0700152
153 return device_bind(parent, drv, info->name, (void *)info->platdata,
154 -1, devp);
155}
156
Simon Glass1586a842014-10-13 23:41:50 -0600157int device_probe_child(struct udevice *dev, void *parent_priv)
Simon Glassdd6ab882014-02-26 15:59:18 -0700158{
159 struct driver *drv;
160 int size = 0;
161 int ret;
Simon Glassdb6f0202014-07-23 06:55:12 -0600162 int seq;
Simon Glassdd6ab882014-02-26 15:59:18 -0700163
164 if (!dev)
165 return -EINVAL;
166
167 if (dev->flags & DM_FLAG_ACTIVATED)
168 return 0;
169
170 drv = dev->driver;
171 assert(drv);
172
Simon Glassba750492015-01-25 08:27:00 -0700173 /* Allocate private data if requested */
Simon Glassdd6ab882014-02-26 15:59:18 -0700174 if (drv->priv_auto_alloc_size) {
175 dev->priv = calloc(1, drv->priv_auto_alloc_size);
176 if (!dev->priv) {
177 ret = -ENOMEM;
178 goto fail;
179 }
180 }
181 /* Allocate private data if requested */
Simon Glassdd6ab882014-02-26 15:59:18 -0700182 size = dev->uclass->uc_drv->per_device_auto_alloc_size;
183 if (size) {
184 dev->uclass_priv = calloc(1, size);
185 if (!dev->uclass_priv) {
186 ret = -ENOMEM;
187 goto fail;
188 }
189 }
190
191 /* Ensure all parents are probed */
192 if (dev->parent) {
Simon Glass60d971b2014-07-23 06:55:20 -0600193 size = dev->parent->driver->per_child_auto_alloc_size;
194 if (size) {
195 dev->parent_priv = calloc(1, size);
196 if (!dev->parent_priv) {
197 ret = -ENOMEM;
198 goto fail;
199 }
Simon Glass1586a842014-10-13 23:41:50 -0600200 if (parent_priv)
201 memcpy(dev->parent_priv, parent_priv, size);
Simon Glass60d971b2014-07-23 06:55:20 -0600202 }
203
Simon Glassdd6ab882014-02-26 15:59:18 -0700204 ret = device_probe(dev->parent);
205 if (ret)
206 goto fail;
207 }
Simon Glassdb6f0202014-07-23 06:55:12 -0600208
209 seq = uclass_resolve_seq(dev);
210 if (seq < 0) {
211 ret = seq;
212 goto fail;
213 }
214 dev->seq = seq;
Simon Glassdd6ab882014-02-26 15:59:18 -0700215
Simon Glassd45560d2014-07-23 06:55:21 -0600216 if (dev->parent && dev->parent->driver->child_pre_probe) {
217 ret = dev->parent->driver->child_pre_probe(dev);
218 if (ret)
219 goto fail;
220 }
221
Simon Glassdd6ab882014-02-26 15:59:18 -0700222 if (drv->ofdata_to_platdata && dev->of_offset >= 0) {
223 ret = drv->ofdata_to_platdata(dev);
224 if (ret)
225 goto fail;
226 }
227
228 if (drv->probe) {
229 ret = drv->probe(dev);
230 if (ret)
231 goto fail;
232 }
233
234 dev->flags |= DM_FLAG_ACTIVATED;
235
236 ret = uclass_post_probe_device(dev);
237 if (ret) {
238 dev->flags &= ~DM_FLAG_ACTIVATED;
239 goto fail_uclass;
240 }
241
242 return 0;
243fail_uclass:
244 if (device_remove(dev)) {
245 dm_warn("%s: Device '%s' failed to remove on error path\n",
246 __func__, dev->name);
247 }
248fail:
Simon Glassdb6f0202014-07-23 06:55:12 -0600249 dev->seq = -1;
Simon Glassdd6ab882014-02-26 15:59:18 -0700250 device_free(dev);
251
252 return ret;
253}
254
Simon Glass1586a842014-10-13 23:41:50 -0600255int device_probe(struct udevice *dev)
256{
257 return device_probe_child(dev, NULL);
258}
259
Heiko Schocherb74fcb42014-05-22 12:43:05 +0200260void *dev_get_platdata(struct udevice *dev)
Simon Glassdd6ab882014-02-26 15:59:18 -0700261{
262 if (!dev) {
Simon Glassdf1e0722014-12-10 08:55:56 -0700263 dm_warn("%s: null device\n", __func__);
Simon Glassdd6ab882014-02-26 15:59:18 -0700264 return NULL;
265 }
266
267 return dev->platdata;
268}
269
Simon Glass11b61732015-01-25 08:27:01 -0700270void *dev_get_parent_platdata(struct udevice *dev)
271{
272 if (!dev) {
273 dm_warn("%s: null device", __func__);
274 return NULL;
275 }
276
277 return dev->parent_platdata;
278}
279
Heiko Schocherb74fcb42014-05-22 12:43:05 +0200280void *dev_get_priv(struct udevice *dev)
Simon Glassdd6ab882014-02-26 15:59:18 -0700281{
282 if (!dev) {
Simon Glassdf1e0722014-12-10 08:55:56 -0700283 dm_warn("%s: null device\n", __func__);
Simon Glassdd6ab882014-02-26 15:59:18 -0700284 return NULL;
285 }
286
287 return dev->priv;
288}
Simon Glass48d4e292014-07-23 06:55:19 -0600289
Simon Glass60d971b2014-07-23 06:55:20 -0600290void *dev_get_parentdata(struct udevice *dev)
291{
292 if (!dev) {
Simon Glassdf1e0722014-12-10 08:55:56 -0700293 dm_warn("%s: null device\n", __func__);
Simon Glass60d971b2014-07-23 06:55:20 -0600294 return NULL;
295 }
296
297 return dev->parent_priv;
298}
299
Simon Glass48d4e292014-07-23 06:55:19 -0600300static int device_get_device_tail(struct udevice *dev, int ret,
301 struct udevice **devp)
302{
303 if (ret)
304 return ret;
305
306 ret = device_probe(dev);
307 if (ret)
308 return ret;
309
310 *devp = dev;
311
312 return 0;
313}
314
315int device_get_child(struct udevice *parent, int index, struct udevice **devp)
316{
317 struct udevice *dev;
318
319 list_for_each_entry(dev, &parent->child_head, sibling_node) {
320 if (!index--)
321 return device_get_device_tail(dev, 0, devp);
322 }
323
324 return -ENODEV;
325}
326
327int device_find_child_by_seq(struct udevice *parent, int seq_or_req_seq,
328 bool find_req_seq, struct udevice **devp)
329{
330 struct udevice *dev;
331
332 *devp = NULL;
333 if (seq_or_req_seq == -1)
334 return -ENODEV;
335
336 list_for_each_entry(dev, &parent->child_head, sibling_node) {
337 if ((find_req_seq ? dev->req_seq : dev->seq) ==
338 seq_or_req_seq) {
339 *devp = dev;
340 return 0;
341 }
342 }
343
344 return -ENODEV;
345}
346
347int device_get_child_by_seq(struct udevice *parent, int seq,
348 struct udevice **devp)
349{
350 struct udevice *dev;
351 int ret;
352
353 *devp = NULL;
354 ret = device_find_child_by_seq(parent, seq, false, &dev);
355 if (ret == -ENODEV) {
356 /*
357 * We didn't find it in probed devices. See if there is one
358 * that will request this seq if probed.
359 */
360 ret = device_find_child_by_seq(parent, seq, true, &dev);
361 }
362 return device_get_device_tail(dev, ret, devp);
363}
364
365int device_find_child_by_of_offset(struct udevice *parent, int of_offset,
366 struct udevice **devp)
367{
368 struct udevice *dev;
369
370 *devp = NULL;
371
372 list_for_each_entry(dev, &parent->child_head, sibling_node) {
373 if (dev->of_offset == of_offset) {
374 *devp = dev;
375 return 0;
376 }
377 }
378
379 return -ENODEV;
380}
381
382int device_get_child_by_of_offset(struct udevice *parent, int seq,
383 struct udevice **devp)
384{
385 struct udevice *dev;
386 int ret;
387
388 *devp = NULL;
389 ret = device_find_child_by_of_offset(parent, seq, &dev);
390 return device_get_device_tail(dev, ret, devp);
391}
Simon Glass44da7352014-10-13 23:41:49 -0600392
393int device_find_first_child(struct udevice *parent, struct udevice **devp)
394{
395 if (list_empty(&parent->child_head)) {
396 *devp = NULL;
397 } else {
398 *devp = list_first_entry(&parent->child_head, struct udevice,
399 sibling_node);
400 }
401
402 return 0;
403}
404
405int device_find_next_child(struct udevice **devp)
406{
407 struct udevice *dev = *devp;
408 struct udevice *parent = dev->parent;
409
410 if (list_is_last(&dev->sibling_node, &parent->child_head)) {
411 *devp = NULL;
412 } else {
413 *devp = list_entry(dev->sibling_node.next, struct udevice,
414 sibling_node);
415 }
416
417 return 0;
418}
Simon Glass70c3a0e2014-11-11 10:46:18 -0700419
Simon Glass43f488a2014-11-11 10:46:19 -0700420struct udevice *dev_get_parent(struct udevice *child)
421{
422 return child->parent;
423}
424
Simon Glass70c3a0e2014-11-11 10:46:18 -0700425ulong dev_get_of_data(struct udevice *dev)
426{
427 return dev->of_id->data;
428}