blob: eca8edac26a597bd23f25d13c20ac3b1d4624ae4 [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 Glassdd6ab882014-02-26 15:59:18 -070075 if (!dev->platdata && drv->platdata_auto_alloc_size)
76 dev->flags |= DM_FLAG_ALLOC_PDATA;
77
78 /* put dev into parent's successor list */
79 if (parent)
80 list_add_tail(&dev->sibling_node, &parent->child_head);
81
82 ret = uclass_bind_device(dev);
83 if (ret)
Simon Glass4ebe01b2015-01-25 08:26:59 -070084 goto fail_uclass_bind;
Simon Glassdd6ab882014-02-26 15:59:18 -070085
86 /* if we fail to bind we remove device from successors and free it */
87 if (drv->bind) {
88 ret = drv->bind(dev);
Simon Glass4ebe01b2015-01-25 08:26:59 -070089 if (ret)
Simon Glassdd6ab882014-02-26 15:59:18 -070090 goto fail_bind;
Simon Glassdd6ab882014-02-26 15:59:18 -070091 }
92 if (parent)
93 dm_dbg("Bound device %s to %s\n", dev->name, parent->name);
94 *devp = dev;
95
96 return 0;
97
98fail_bind:
Simon Glass4ebe01b2015-01-25 08:26:59 -070099 if (uclass_unbind_device(dev)) {
100 dm_warn("Failed to unbind dev '%s' on error path\n",
101 dev->name);
102 }
103fail_uclass_bind:
104 if (parent)
105 list_del(&dev->sibling_node);
Simon Glassdd6ab882014-02-26 15:59:18 -0700106 free(dev);
Simon Glass4ebe01b2015-01-25 08:26:59 -0700107
Simon Glassdd6ab882014-02-26 15:59:18 -0700108 return ret;
109}
110
Simon Glassfef72b72014-07-23 06:55:03 -0600111int device_bind_by_name(struct udevice *parent, bool pre_reloc_only,
112 const struct driver_info *info, struct udevice **devp)
Simon Glassdd6ab882014-02-26 15:59:18 -0700113{
114 struct driver *drv;
115
116 drv = lists_driver_lookup_name(info->name);
117 if (!drv)
118 return -ENOENT;
Simon Glassfef72b72014-07-23 06:55:03 -0600119 if (pre_reloc_only && !(drv->flags & DM_FLAG_PRE_RELOC))
120 return -EPERM;
Simon Glassdd6ab882014-02-26 15:59:18 -0700121
122 return device_bind(parent, drv, info->name, (void *)info->platdata,
123 -1, devp);
124}
125
Simon Glass1586a842014-10-13 23:41:50 -0600126int device_probe_child(struct udevice *dev, void *parent_priv)
Simon Glassdd6ab882014-02-26 15:59:18 -0700127{
128 struct driver *drv;
129 int size = 0;
130 int ret;
Simon Glassdb6f0202014-07-23 06:55:12 -0600131 int seq;
Simon Glassdd6ab882014-02-26 15:59:18 -0700132
133 if (!dev)
134 return -EINVAL;
135
136 if (dev->flags & DM_FLAG_ACTIVATED)
137 return 0;
138
139 drv = dev->driver;
140 assert(drv);
141
142 /* Allocate private data and platdata if requested */
143 if (drv->priv_auto_alloc_size) {
144 dev->priv = calloc(1, drv->priv_auto_alloc_size);
145 if (!dev->priv) {
146 ret = -ENOMEM;
147 goto fail;
148 }
149 }
150 /* Allocate private data if requested */
151 if (dev->flags & DM_FLAG_ALLOC_PDATA) {
152 dev->platdata = calloc(1, drv->platdata_auto_alloc_size);
153 if (!dev->platdata) {
154 ret = -ENOMEM;
155 goto fail;
156 }
157 }
158 size = dev->uclass->uc_drv->per_device_auto_alloc_size;
159 if (size) {
160 dev->uclass_priv = calloc(1, size);
161 if (!dev->uclass_priv) {
162 ret = -ENOMEM;
163 goto fail;
164 }
165 }
166
167 /* Ensure all parents are probed */
168 if (dev->parent) {
Simon Glass60d971b2014-07-23 06:55:20 -0600169 size = dev->parent->driver->per_child_auto_alloc_size;
170 if (size) {
171 dev->parent_priv = calloc(1, size);
172 if (!dev->parent_priv) {
173 ret = -ENOMEM;
174 goto fail;
175 }
Simon Glass1586a842014-10-13 23:41:50 -0600176 if (parent_priv)
177 memcpy(dev->parent_priv, parent_priv, size);
Simon Glass60d971b2014-07-23 06:55:20 -0600178 }
179
Simon Glassdd6ab882014-02-26 15:59:18 -0700180 ret = device_probe(dev->parent);
181 if (ret)
182 goto fail;
183 }
Simon Glassdb6f0202014-07-23 06:55:12 -0600184
185 seq = uclass_resolve_seq(dev);
186 if (seq < 0) {
187 ret = seq;
188 goto fail;
189 }
190 dev->seq = seq;
Simon Glassdd6ab882014-02-26 15:59:18 -0700191
Simon Glassd45560d2014-07-23 06:55:21 -0600192 if (dev->parent && dev->parent->driver->child_pre_probe) {
193 ret = dev->parent->driver->child_pre_probe(dev);
194 if (ret)
195 goto fail;
196 }
197
Simon Glassdd6ab882014-02-26 15:59:18 -0700198 if (drv->ofdata_to_platdata && dev->of_offset >= 0) {
199 ret = drv->ofdata_to_platdata(dev);
200 if (ret)
201 goto fail;
202 }
203
204 if (drv->probe) {
205 ret = drv->probe(dev);
206 if (ret)
207 goto fail;
208 }
209
210 dev->flags |= DM_FLAG_ACTIVATED;
211
212 ret = uclass_post_probe_device(dev);
213 if (ret) {
214 dev->flags &= ~DM_FLAG_ACTIVATED;
215 goto fail_uclass;
216 }
217
218 return 0;
219fail_uclass:
220 if (device_remove(dev)) {
221 dm_warn("%s: Device '%s' failed to remove on error path\n",
222 __func__, dev->name);
223 }
224fail:
Simon Glassdb6f0202014-07-23 06:55:12 -0600225 dev->seq = -1;
Simon Glassdd6ab882014-02-26 15:59:18 -0700226 device_free(dev);
227
228 return ret;
229}
230
Simon Glass1586a842014-10-13 23:41:50 -0600231int device_probe(struct udevice *dev)
232{
233 return device_probe_child(dev, NULL);
234}
235
Heiko Schocherb74fcb42014-05-22 12:43:05 +0200236void *dev_get_platdata(struct udevice *dev)
Simon Glassdd6ab882014-02-26 15:59:18 -0700237{
238 if (!dev) {
Simon Glassdf1e0722014-12-10 08:55:56 -0700239 dm_warn("%s: null device\n", __func__);
Simon Glassdd6ab882014-02-26 15:59:18 -0700240 return NULL;
241 }
242
243 return dev->platdata;
244}
245
Heiko Schocherb74fcb42014-05-22 12:43:05 +0200246void *dev_get_priv(struct udevice *dev)
Simon Glassdd6ab882014-02-26 15:59:18 -0700247{
248 if (!dev) {
Simon Glassdf1e0722014-12-10 08:55:56 -0700249 dm_warn("%s: null device\n", __func__);
Simon Glassdd6ab882014-02-26 15:59:18 -0700250 return NULL;
251 }
252
253 return dev->priv;
254}
Simon Glass48d4e292014-07-23 06:55:19 -0600255
Simon Glass60d971b2014-07-23 06:55:20 -0600256void *dev_get_parentdata(struct udevice *dev)
257{
258 if (!dev) {
Simon Glassdf1e0722014-12-10 08:55:56 -0700259 dm_warn("%s: null device\n", __func__);
Simon Glass60d971b2014-07-23 06:55:20 -0600260 return NULL;
261 }
262
263 return dev->parent_priv;
264}
265
Simon Glass48d4e292014-07-23 06:55:19 -0600266static int device_get_device_tail(struct udevice *dev, int ret,
267 struct udevice **devp)
268{
269 if (ret)
270 return ret;
271
272 ret = device_probe(dev);
273 if (ret)
274 return ret;
275
276 *devp = dev;
277
278 return 0;
279}
280
281int device_get_child(struct udevice *parent, int index, struct udevice **devp)
282{
283 struct udevice *dev;
284
285 list_for_each_entry(dev, &parent->child_head, sibling_node) {
286 if (!index--)
287 return device_get_device_tail(dev, 0, devp);
288 }
289
290 return -ENODEV;
291}
292
293int device_find_child_by_seq(struct udevice *parent, int seq_or_req_seq,
294 bool find_req_seq, struct udevice **devp)
295{
296 struct udevice *dev;
297
298 *devp = NULL;
299 if (seq_or_req_seq == -1)
300 return -ENODEV;
301
302 list_for_each_entry(dev, &parent->child_head, sibling_node) {
303 if ((find_req_seq ? dev->req_seq : dev->seq) ==
304 seq_or_req_seq) {
305 *devp = dev;
306 return 0;
307 }
308 }
309
310 return -ENODEV;
311}
312
313int device_get_child_by_seq(struct udevice *parent, int seq,
314 struct udevice **devp)
315{
316 struct udevice *dev;
317 int ret;
318
319 *devp = NULL;
320 ret = device_find_child_by_seq(parent, seq, false, &dev);
321 if (ret == -ENODEV) {
322 /*
323 * We didn't find it in probed devices. See if there is one
324 * that will request this seq if probed.
325 */
326 ret = device_find_child_by_seq(parent, seq, true, &dev);
327 }
328 return device_get_device_tail(dev, ret, devp);
329}
330
331int device_find_child_by_of_offset(struct udevice *parent, int of_offset,
332 struct udevice **devp)
333{
334 struct udevice *dev;
335
336 *devp = NULL;
337
338 list_for_each_entry(dev, &parent->child_head, sibling_node) {
339 if (dev->of_offset == of_offset) {
340 *devp = dev;
341 return 0;
342 }
343 }
344
345 return -ENODEV;
346}
347
348int device_get_child_by_of_offset(struct udevice *parent, int seq,
349 struct udevice **devp)
350{
351 struct udevice *dev;
352 int ret;
353
354 *devp = NULL;
355 ret = device_find_child_by_of_offset(parent, seq, &dev);
356 return device_get_device_tail(dev, ret, devp);
357}
Simon Glass44da7352014-10-13 23:41:49 -0600358
359int device_find_first_child(struct udevice *parent, struct udevice **devp)
360{
361 if (list_empty(&parent->child_head)) {
362 *devp = NULL;
363 } else {
364 *devp = list_first_entry(&parent->child_head, struct udevice,
365 sibling_node);
366 }
367
368 return 0;
369}
370
371int device_find_next_child(struct udevice **devp)
372{
373 struct udevice *dev = *devp;
374 struct udevice *parent = dev->parent;
375
376 if (list_is_last(&dev->sibling_node, &parent->child_head)) {
377 *devp = NULL;
378 } else {
379 *devp = list_entry(dev->sibling_node.next, struct udevice,
380 sibling_node);
381 }
382
383 return 0;
384}
Simon Glass70c3a0e2014-11-11 10:46:18 -0700385
Simon Glass43f488a2014-11-11 10:46:19 -0700386struct udevice *dev_get_parent(struct udevice *child)
387{
388 return child->parent;
389}
390
Simon Glass70c3a0e2014-11-11 10:46:18 -0700391ulong dev_get_of_data(struct udevice *dev)
392{
393 return dev->of_id->data;
394}