]> git.kernelconcepts.de Git - karo-tx-uboot.git/blob - drivers/core/device.c
dm: core: Convert driver_bind() to use const
[karo-tx-uboot.git] / drivers / core / device.c
1 /*
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>
13 #include <fdtdec.h>
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
25 DECLARE_GLOBAL_DATA_PTR;
26
27 int device_bind(struct udevice *parent, const struct driver *drv,
28                 const char *name, void *platdata, int of_offset,
29                 struct udevice **devp)
30 {
31         struct udevice *dev;
32         struct uclass *uc;
33         int ret = 0;
34
35         *devp = NULL;
36         if (!name)
37                 return -EINVAL;
38
39         ret = uclass_get(drv->id, &uc);
40         if (ret)
41                 return ret;
42
43         dev = calloc(1, sizeof(struct udevice));
44         if (!dev)
45                 return -ENOMEM;
46
47         INIT_LIST_HEAD(&dev->sibling_node);
48         INIT_LIST_HEAD(&dev->child_head);
49         INIT_LIST_HEAD(&dev->uclass_node);
50         dev->platdata = platdata;
51         dev->name = name;
52         dev->of_offset = of_offset;
53         dev->parent = parent;
54         dev->driver = drv;
55         dev->uclass = uc;
56
57         dev->seq = -1;
58         dev->req_seq = -1;
59 #ifdef CONFIG_OF_CONTROL
60         /*
61          * Some devices, such as a SPI bus, I2C bus and serial ports are
62          * numbered using aliases.
63          *
64          * This is just a 'requested' sequence, and will be
65          * resolved (and ->seq updated) when the device is probed.
66          */
67         if (uc->uc_drv->flags & DM_UC_FLAG_SEQ_ALIAS) {
68                 if (uc->uc_drv->name && of_offset != -1) {
69                         fdtdec_get_alias_seq(gd->fdt_blob, uc->uc_drv->name,
70                                              of_offset, &dev->req_seq);
71                 }
72         }
73 #endif
74         if (!dev->platdata && drv->platdata_auto_alloc_size) {
75                 dev->flags |= DM_FLAG_ALLOC_PDATA;
76                 dev->platdata = calloc(1, drv->platdata_auto_alloc_size);
77                 if (!dev->platdata) {
78                         ret = -ENOMEM;
79                         goto fail_alloc1;
80                 }
81         }
82         if (parent) {
83                 int size = parent->driver->per_child_platdata_auto_alloc_size;
84
85                 if (!size) {
86                         size = parent->uclass->uc_drv->
87                                         per_child_platdata_auto_alloc_size;
88                 }
89                 if (size) {
90                         dev->flags |= DM_FLAG_ALLOC_PARENT_PDATA;
91                         dev->parent_platdata = calloc(1, size);
92                         if (!dev->parent_platdata) {
93                                 ret = -ENOMEM;
94                                 goto fail_alloc2;
95                         }
96                 }
97         }
98
99         /* put dev into parent's successor list */
100         if (parent)
101                 list_add_tail(&dev->sibling_node, &parent->child_head);
102
103         ret = uclass_bind_device(dev);
104         if (ret)
105                 goto fail_uclass_bind;
106
107         /* if we fail to bind we remove device from successors and free it */
108         if (drv->bind) {
109                 ret = drv->bind(dev);
110                 if (ret)
111                         goto fail_bind;
112         }
113         if (parent && parent->driver->child_post_bind) {
114                 ret = parent->driver->child_post_bind(dev);
115                 if (ret)
116                         goto fail_child_post_bind;
117         }
118
119         if (parent)
120                 dm_dbg("Bound device %s to %s\n", dev->name, parent->name);
121         *devp = dev;
122
123         return 0;
124
125 fail_child_post_bind:
126         if (drv->unbind && drv->unbind(dev)) {
127                 dm_warn("unbind() method failed on dev '%s' on error path\n",
128                         dev->name);
129         }
130
131 fail_bind:
132         if (uclass_unbind_device(dev)) {
133                 dm_warn("Failed to unbind dev '%s' on error path\n",
134                         dev->name);
135         }
136 fail_uclass_bind:
137         list_del(&dev->sibling_node);
138         if (dev->flags & DM_FLAG_ALLOC_PARENT_PDATA) {
139                 free(dev->parent_platdata);
140                 dev->parent_platdata = NULL;
141         }
142 fail_alloc2:
143         if (dev->flags & DM_FLAG_ALLOC_PDATA) {
144                 free(dev->platdata);
145                 dev->platdata = NULL;
146         }
147 fail_alloc1:
148         free(dev);
149
150         return ret;
151 }
152
153 int device_bind_by_name(struct udevice *parent, bool pre_reloc_only,
154                         const struct driver_info *info, struct udevice **devp)
155 {
156         struct driver *drv;
157
158         drv = lists_driver_lookup_name(info->name);
159         if (!drv)
160                 return -ENOENT;
161         if (pre_reloc_only && !(drv->flags & DM_FLAG_PRE_RELOC))
162                 return -EPERM;
163
164         return device_bind(parent, drv, info->name, (void *)info->platdata,
165                            -1, devp);
166 }
167
168 static void *alloc_priv(int size, uint flags)
169 {
170         void *priv;
171
172         if (flags & DM_FLAG_ALLOC_PRIV_DMA) {
173                 priv = memalign(ARCH_DMA_MINALIGN, size);
174                 if (priv)
175                         memset(priv, '\0', size);
176         } else {
177                 priv = calloc(1, size);
178         }
179
180         return priv;
181 }
182
183 int device_probe_child(struct udevice *dev, void *parent_priv)
184 {
185         const struct driver *drv;
186         int size = 0;
187         int ret;
188         int seq;
189
190         if (!dev)
191                 return -EINVAL;
192
193         if (dev->flags & DM_FLAG_ACTIVATED)
194                 return 0;
195
196         drv = dev->driver;
197         assert(drv);
198
199         /* Allocate private data if requested */
200         if (drv->priv_auto_alloc_size) {
201                 dev->priv = alloc_priv(drv->priv_auto_alloc_size, drv->flags);
202                 if (!dev->priv) {
203                         ret = -ENOMEM;
204                         goto fail;
205                 }
206         }
207         /* Allocate private data if requested */
208         size = dev->uclass->uc_drv->per_device_auto_alloc_size;
209         if (size) {
210                 dev->uclass_priv = calloc(1, size);
211                 if (!dev->uclass_priv) {
212                         ret = -ENOMEM;
213                         goto fail;
214                 }
215         }
216
217         /* Ensure all parents are probed */
218         if (dev->parent) {
219                 size = dev->parent->driver->per_child_auto_alloc_size;
220                 if (!size) {
221                         size = dev->parent->uclass->uc_drv->
222                                         per_child_auto_alloc_size;
223                 }
224                 if (size) {
225                         dev->parent_priv = alloc_priv(size, drv->flags);
226                         if (!dev->parent_priv) {
227                                 ret = -ENOMEM;
228                                 goto fail;
229                         }
230                         if (parent_priv)
231                                 memcpy(dev->parent_priv, parent_priv, size);
232                 }
233
234                 ret = device_probe(dev->parent);
235                 if (ret)
236                         goto fail;
237         }
238
239         seq = uclass_resolve_seq(dev);
240         if (seq < 0) {
241                 ret = seq;
242                 goto fail;
243         }
244         dev->seq = seq;
245
246         ret = uclass_pre_probe_device(dev);
247         if (ret)
248                 goto fail;
249
250         if (dev->parent && dev->parent->driver->child_pre_probe) {
251                 ret = dev->parent->driver->child_pre_probe(dev);
252                 if (ret)
253                         goto fail;
254         }
255
256         if (drv->ofdata_to_platdata && dev->of_offset >= 0) {
257                 ret = drv->ofdata_to_platdata(dev);
258                 if (ret)
259                         goto fail;
260         }
261
262         dev->flags |= DM_FLAG_ACTIVATED;
263         if (drv->probe) {
264                 ret = drv->probe(dev);
265                 if (ret) {
266                         dev->flags &= ~DM_FLAG_ACTIVATED;
267                         goto fail;
268                 }
269         }
270
271         ret = uclass_post_probe_device(dev);
272         if (ret) {
273                 dev->flags &= ~DM_FLAG_ACTIVATED;
274                 goto fail_uclass;
275         }
276
277         return 0;
278 fail_uclass:
279         if (device_remove(dev)) {
280                 dm_warn("%s: Device '%s' failed to remove on error path\n",
281                         __func__, dev->name);
282         }
283 fail:
284         dev->seq = -1;
285         device_free(dev);
286
287         return ret;
288 }
289
290 int device_probe(struct udevice *dev)
291 {
292         return device_probe_child(dev, NULL);
293 }
294
295 void *dev_get_platdata(struct udevice *dev)
296 {
297         if (!dev) {
298                 dm_warn("%s: null device\n", __func__);
299                 return NULL;
300         }
301
302         return dev->platdata;
303 }
304
305 void *dev_get_parent_platdata(struct udevice *dev)
306 {
307         if (!dev) {
308                 dm_warn("%s: null device", __func__);
309                 return NULL;
310         }
311
312         return dev->parent_platdata;
313 }
314
315 void *dev_get_priv(struct udevice *dev)
316 {
317         if (!dev) {
318                 dm_warn("%s: null device\n", __func__);
319                 return NULL;
320         }
321
322         return dev->priv;
323 }
324
325 void *dev_get_uclass_priv(struct udevice *dev)
326 {
327         if (!dev) {
328                 dm_warn("%s: null device\n", __func__);
329                 return NULL;
330         }
331
332         return dev->uclass_priv;
333 }
334
335 void *dev_get_parentdata(struct udevice *dev)
336 {
337         if (!dev) {
338                 dm_warn("%s: null device\n", __func__);
339                 return NULL;
340         }
341
342         return dev->parent_priv;
343 }
344
345 static int device_get_device_tail(struct udevice *dev, int ret,
346                                   struct udevice **devp)
347 {
348         if (ret)
349                 return ret;
350
351         ret = device_probe(dev);
352         if (ret)
353                 return ret;
354
355         *devp = dev;
356
357         return 0;
358 }
359
360 int device_get_child(struct udevice *parent, int index, struct udevice **devp)
361 {
362         struct udevice *dev;
363
364         list_for_each_entry(dev, &parent->child_head, sibling_node) {
365                 if (!index--)
366                         return device_get_device_tail(dev, 0, devp);
367         }
368
369         return -ENODEV;
370 }
371
372 int device_find_child_by_seq(struct udevice *parent, int seq_or_req_seq,
373                              bool find_req_seq, struct udevice **devp)
374 {
375         struct udevice *dev;
376
377         *devp = NULL;
378         if (seq_or_req_seq == -1)
379                 return -ENODEV;
380
381         list_for_each_entry(dev, &parent->child_head, sibling_node) {
382                 if ((find_req_seq ? dev->req_seq : dev->seq) ==
383                                 seq_or_req_seq) {
384                         *devp = dev;
385                         return 0;
386                 }
387         }
388
389         return -ENODEV;
390 }
391
392 int device_get_child_by_seq(struct udevice *parent, int seq,
393                             struct udevice **devp)
394 {
395         struct udevice *dev;
396         int ret;
397
398         *devp = NULL;
399         ret = device_find_child_by_seq(parent, seq, false, &dev);
400         if (ret == -ENODEV) {
401                 /*
402                  * We didn't find it in probed devices. See if there is one
403                  * that will request this seq if probed.
404                  */
405                 ret = device_find_child_by_seq(parent, seq, true, &dev);
406         }
407         return device_get_device_tail(dev, ret, devp);
408 }
409
410 int device_find_child_by_of_offset(struct udevice *parent, int of_offset,
411                                    struct udevice **devp)
412 {
413         struct udevice *dev;
414
415         *devp = NULL;
416
417         list_for_each_entry(dev, &parent->child_head, sibling_node) {
418                 if (dev->of_offset == of_offset) {
419                         *devp = dev;
420                         return 0;
421                 }
422         }
423
424         return -ENODEV;
425 }
426
427 int device_get_child_by_of_offset(struct udevice *parent, int seq,
428                                   struct udevice **devp)
429 {
430         struct udevice *dev;
431         int ret;
432
433         *devp = NULL;
434         ret = device_find_child_by_of_offset(parent, seq, &dev);
435         return device_get_device_tail(dev, ret, devp);
436 }
437
438 int device_find_first_child(struct udevice *parent, struct udevice **devp)
439 {
440         if (list_empty(&parent->child_head)) {
441                 *devp = NULL;
442         } else {
443                 *devp = list_first_entry(&parent->child_head, struct udevice,
444                                          sibling_node);
445         }
446
447         return 0;
448 }
449
450 int device_find_next_child(struct udevice **devp)
451 {
452         struct udevice *dev = *devp;
453         struct udevice *parent = dev->parent;
454
455         if (list_is_last(&dev->sibling_node, &parent->child_head)) {
456                 *devp = NULL;
457         } else {
458                 *devp = list_entry(dev->sibling_node.next, struct udevice,
459                                    sibling_node);
460         }
461
462         return 0;
463 }
464
465 struct udevice *dev_get_parent(struct udevice *child)
466 {
467         return child->parent;
468 }
469
470 ulong dev_get_of_data(struct udevice *dev)
471 {
472         return dev->of_id->data;
473 }
474
475 enum uclass_id device_get_uclass_id(struct udevice *dev)
476 {
477         return dev->uclass->uc_drv->id;
478 }
479
480 #ifdef CONFIG_OF_CONTROL
481 fdt_addr_t dev_get_addr(struct udevice *dev)
482 {
483         return fdtdec_get_addr(gd->fdt_blob, dev->of_offset, "reg");
484 }
485 #else
486 fdt_addr_t dev_get_addr(struct udevice *dev)
487 {
488         return FDT_ADDR_T_NONE;
489 }
490 #endif