blob: ee97cc84fff8d83fe77de85efabcd41dde37d362 [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
86 if (size) {
87 dev->flags |= DM_FLAG_ALLOC_PARENT_PDATA;
88 dev->parent_platdata = calloc(1, size);
89 if (!dev->parent_platdata) {
90 ret = -ENOMEM;
91 goto fail_alloc2;
92 }
93 }
94 }
Simon Glassdd6ab882014-02-26 15:59:18 -070095
96 /* put dev into parent's successor list */
97 if (parent)
98 list_add_tail(&dev->sibling_node, &parent->child_head);
99
100 ret = uclass_bind_device(dev);
101 if (ret)
Simon Glass4ebe01b2015-01-25 08:26:59 -0700102 goto fail_uclass_bind;
Simon Glassdd6ab882014-02-26 15:59:18 -0700103
104 /* if we fail to bind we remove device from successors and free it */
105 if (drv->bind) {
106 ret = drv->bind(dev);
Simon Glass4ebe01b2015-01-25 08:26:59 -0700107 if (ret)
Simon Glassdd6ab882014-02-26 15:59:18 -0700108 goto fail_bind;
Simon Glassdd6ab882014-02-26 15:59:18 -0700109 }
110 if (parent)
111 dm_dbg("Bound device %s to %s\n", dev->name, parent->name);
112 *devp = dev;
113
114 return 0;
115
116fail_bind:
Simon Glass4ebe01b2015-01-25 08:26:59 -0700117 if (uclass_unbind_device(dev)) {
118 dm_warn("Failed to unbind dev '%s' on error path\n",
119 dev->name);
120 }
121fail_uclass_bind:
Simon Glass11b61732015-01-25 08:27:01 -0700122 list_del(&dev->sibling_node);
123 if (dev->flags & DM_FLAG_ALLOC_PARENT_PDATA) {
124 free(dev->parent_platdata);
125 dev->parent_platdata = NULL;
126 }
127fail_alloc2:
Simon Glassba750492015-01-25 08:27:00 -0700128 if (dev->flags & DM_FLAG_ALLOC_PDATA) {
129 free(dev->platdata);
130 dev->platdata = NULL;
131 }
132fail_alloc1:
Simon Glassdd6ab882014-02-26 15:59:18 -0700133 free(dev);
Simon Glass4ebe01b2015-01-25 08:26:59 -0700134
Simon Glassdd6ab882014-02-26 15:59:18 -0700135 return ret;
136}
137
Simon Glassfef72b72014-07-23 06:55:03 -0600138int device_bind_by_name(struct udevice *parent, bool pre_reloc_only,
139 const struct driver_info *info, struct udevice **devp)
Simon Glassdd6ab882014-02-26 15:59:18 -0700140{
141 struct driver *drv;
142
143 drv = lists_driver_lookup_name(info->name);
144 if (!drv)
145 return -ENOENT;
Simon Glassfef72b72014-07-23 06:55:03 -0600146 if (pre_reloc_only && !(drv->flags & DM_FLAG_PRE_RELOC))
147 return -EPERM;
Simon Glassdd6ab882014-02-26 15:59:18 -0700148
149 return device_bind(parent, drv, info->name, (void *)info->platdata,
150 -1, devp);
151}
152
Simon Glass1586a842014-10-13 23:41:50 -0600153int device_probe_child(struct udevice *dev, void *parent_priv)
Simon Glassdd6ab882014-02-26 15:59:18 -0700154{
155 struct driver *drv;
156 int size = 0;
157 int ret;
Simon Glassdb6f0202014-07-23 06:55:12 -0600158 int seq;
Simon Glassdd6ab882014-02-26 15:59:18 -0700159
160 if (!dev)
161 return -EINVAL;
162
163 if (dev->flags & DM_FLAG_ACTIVATED)
164 return 0;
165
166 drv = dev->driver;
167 assert(drv);
168
Simon Glassba750492015-01-25 08:27:00 -0700169 /* Allocate private data if requested */
Simon Glassdd6ab882014-02-26 15:59:18 -0700170 if (drv->priv_auto_alloc_size) {
171 dev->priv = calloc(1, drv->priv_auto_alloc_size);
172 if (!dev->priv) {
173 ret = -ENOMEM;
174 goto fail;
175 }
176 }
177 /* Allocate private data if requested */
Simon Glassdd6ab882014-02-26 15:59:18 -0700178 size = dev->uclass->uc_drv->per_device_auto_alloc_size;
179 if (size) {
180 dev->uclass_priv = calloc(1, size);
181 if (!dev->uclass_priv) {
182 ret = -ENOMEM;
183 goto fail;
184 }
185 }
186
187 /* Ensure all parents are probed */
188 if (dev->parent) {
Simon Glass60d971b2014-07-23 06:55:20 -0600189 size = dev->parent->driver->per_child_auto_alloc_size;
190 if (size) {
191 dev->parent_priv = calloc(1, size);
192 if (!dev->parent_priv) {
193 ret = -ENOMEM;
194 goto fail;
195 }
Simon Glass1586a842014-10-13 23:41:50 -0600196 if (parent_priv)
197 memcpy(dev->parent_priv, parent_priv, size);
Simon Glass60d971b2014-07-23 06:55:20 -0600198 }
199
Simon Glassdd6ab882014-02-26 15:59:18 -0700200 ret = device_probe(dev->parent);
201 if (ret)
202 goto fail;
203 }
Simon Glassdb6f0202014-07-23 06:55:12 -0600204
205 seq = uclass_resolve_seq(dev);
206 if (seq < 0) {
207 ret = seq;
208 goto fail;
209 }
210 dev->seq = seq;
Simon Glassdd6ab882014-02-26 15:59:18 -0700211
Simon Glassd45560d2014-07-23 06:55:21 -0600212 if (dev->parent && dev->parent->driver->child_pre_probe) {
213 ret = dev->parent->driver->child_pre_probe(dev);
214 if (ret)
215 goto fail;
216 }
217
Simon Glassdd6ab882014-02-26 15:59:18 -0700218 if (drv->ofdata_to_platdata && dev->of_offset >= 0) {
219 ret = drv->ofdata_to_platdata(dev);
220 if (ret)
221 goto fail;
222 }
223
224 if (drv->probe) {
225 ret = drv->probe(dev);
226 if (ret)
227 goto fail;
228 }
229
230 dev->flags |= DM_FLAG_ACTIVATED;
231
232 ret = uclass_post_probe_device(dev);
233 if (ret) {
234 dev->flags &= ~DM_FLAG_ACTIVATED;
235 goto fail_uclass;
236 }
237
238 return 0;
239fail_uclass:
240 if (device_remove(dev)) {
241 dm_warn("%s: Device '%s' failed to remove on error path\n",
242 __func__, dev->name);
243 }
244fail:
Simon Glassdb6f0202014-07-23 06:55:12 -0600245 dev->seq = -1;
Simon Glassdd6ab882014-02-26 15:59:18 -0700246 device_free(dev);
247
248 return ret;
249}
250
Simon Glass1586a842014-10-13 23:41:50 -0600251int device_probe(struct udevice *dev)
252{
253 return device_probe_child(dev, NULL);
254}
255
Heiko Schocherb74fcb42014-05-22 12:43:05 +0200256void *dev_get_platdata(struct udevice *dev)
Simon Glassdd6ab882014-02-26 15:59:18 -0700257{
258 if (!dev) {
Simon Glassdf1e0722014-12-10 08:55:56 -0700259 dm_warn("%s: null device\n", __func__);
Simon Glassdd6ab882014-02-26 15:59:18 -0700260 return NULL;
261 }
262
263 return dev->platdata;
264}
265
Simon Glass11b61732015-01-25 08:27:01 -0700266void *dev_get_parent_platdata(struct udevice *dev)
267{
268 if (!dev) {
269 dm_warn("%s: null device", __func__);
270 return NULL;
271 }
272
273 return dev->parent_platdata;
274}
275
Heiko Schocherb74fcb42014-05-22 12:43:05 +0200276void *dev_get_priv(struct udevice *dev)
Simon Glassdd6ab882014-02-26 15:59:18 -0700277{
278 if (!dev) {
Simon Glassdf1e0722014-12-10 08:55:56 -0700279 dm_warn("%s: null device\n", __func__);
Simon Glassdd6ab882014-02-26 15:59:18 -0700280 return NULL;
281 }
282
283 return dev->priv;
284}
Simon Glass48d4e292014-07-23 06:55:19 -0600285
Simon Glass60d971b2014-07-23 06:55:20 -0600286void *dev_get_parentdata(struct udevice *dev)
287{
288 if (!dev) {
Simon Glassdf1e0722014-12-10 08:55:56 -0700289 dm_warn("%s: null device\n", __func__);
Simon Glass60d971b2014-07-23 06:55:20 -0600290 return NULL;
291 }
292
293 return dev->parent_priv;
294}
295
Simon Glass48d4e292014-07-23 06:55:19 -0600296static int device_get_device_tail(struct udevice *dev, int ret,
297 struct udevice **devp)
298{
299 if (ret)
300 return ret;
301
302 ret = device_probe(dev);
303 if (ret)
304 return ret;
305
306 *devp = dev;
307
308 return 0;
309}
310
311int device_get_child(struct udevice *parent, int index, struct udevice **devp)
312{
313 struct udevice *dev;
314
315 list_for_each_entry(dev, &parent->child_head, sibling_node) {
316 if (!index--)
317 return device_get_device_tail(dev, 0, devp);
318 }
319
320 return -ENODEV;
321}
322
323int device_find_child_by_seq(struct udevice *parent, int seq_or_req_seq,
324 bool find_req_seq, struct udevice **devp)
325{
326 struct udevice *dev;
327
328 *devp = NULL;
329 if (seq_or_req_seq == -1)
330 return -ENODEV;
331
332 list_for_each_entry(dev, &parent->child_head, sibling_node) {
333 if ((find_req_seq ? dev->req_seq : dev->seq) ==
334 seq_or_req_seq) {
335 *devp = dev;
336 return 0;
337 }
338 }
339
340 return -ENODEV;
341}
342
343int device_get_child_by_seq(struct udevice *parent, int seq,
344 struct udevice **devp)
345{
346 struct udevice *dev;
347 int ret;
348
349 *devp = NULL;
350 ret = device_find_child_by_seq(parent, seq, false, &dev);
351 if (ret == -ENODEV) {
352 /*
353 * We didn't find it in probed devices. See if there is one
354 * that will request this seq if probed.
355 */
356 ret = device_find_child_by_seq(parent, seq, true, &dev);
357 }
358 return device_get_device_tail(dev, ret, devp);
359}
360
361int device_find_child_by_of_offset(struct udevice *parent, int of_offset,
362 struct udevice **devp)
363{
364 struct udevice *dev;
365
366 *devp = NULL;
367
368 list_for_each_entry(dev, &parent->child_head, sibling_node) {
369 if (dev->of_offset == of_offset) {
370 *devp = dev;
371 return 0;
372 }
373 }
374
375 return -ENODEV;
376}
377
378int device_get_child_by_of_offset(struct udevice *parent, int seq,
379 struct udevice **devp)
380{
381 struct udevice *dev;
382 int ret;
383
384 *devp = NULL;
385 ret = device_find_child_by_of_offset(parent, seq, &dev);
386 return device_get_device_tail(dev, ret, devp);
387}
Simon Glass44da7352014-10-13 23:41:49 -0600388
389int device_find_first_child(struct udevice *parent, struct udevice **devp)
390{
391 if (list_empty(&parent->child_head)) {
392 *devp = NULL;
393 } else {
394 *devp = list_first_entry(&parent->child_head, struct udevice,
395 sibling_node);
396 }
397
398 return 0;
399}
400
401int device_find_next_child(struct udevice **devp)
402{
403 struct udevice *dev = *devp;
404 struct udevice *parent = dev->parent;
405
406 if (list_is_last(&dev->sibling_node, &parent->child_head)) {
407 *devp = NULL;
408 } else {
409 *devp = list_entry(dev->sibling_node.next, struct udevice,
410 sibling_node);
411 }
412
413 return 0;
414}
Simon Glass70c3a0e2014-11-11 10:46:18 -0700415
Simon Glass43f488a2014-11-11 10:46:19 -0700416struct udevice *dev_get_parent(struct udevice *child)
417{
418 return child->parent;
419}
420
Simon Glass70c3a0e2014-11-11 10:46:18 -0700421ulong dev_get_of_data(struct udevice *dev)
422{
423 return dev->of_id->data;
424}