]> Git Repo - J-u-boot.git/blobdiff - drivers/core/device.c
dm: core: Add flags parameter to device_remove()
[J-u-boot.git] / drivers / core / device.c
index 45d5e3e12c0fb9b1b380f3db41948b22de5b5e85..e1b0ebffc559c9ddf52c86c68b4404eafe966dae 100644 (file)
@@ -10,6 +10,7 @@
  */
 
 #include <common.h>
+#include <asm/io.h>
 #include <fdtdec.h>
 #include <fdt_support.h>
 #include <malloc.h>
 
 DECLARE_GLOBAL_DATA_PTR;
 
-int device_bind(struct udevice *parent, const struct driver *drv,
-               const char *name, void *platdata, int of_offset,
-               struct udevice **devp)
+static int device_bind_common(struct udevice *parent, const struct driver *drv,
+                             const char *name, void *platdata,
+                             ulong driver_data, int of_offset,
+                             uint of_platdata_size, struct udevice **devp)
 {
        struct udevice *dev;
        struct uclass *uc;
@@ -56,6 +58,7 @@ int device_bind(struct udevice *parent, const struct driver *drv,
        INIT_LIST_HEAD(&dev->devres_head);
 #endif
        dev->platdata = platdata;
+       dev->driver_data = driver_data;
        dev->name = name;
        dev->of_offset = of_offset;
        dev->parent = parent;
@@ -81,12 +84,29 @@ int device_bind(struct udevice *parent, const struct driver *drv,
                }
        }
 
-       if (!dev->platdata && drv->platdata_auto_alloc_size) {
-               dev->flags |= DM_FLAG_ALLOC_PDATA;
-               dev->platdata = calloc(1, drv->platdata_auto_alloc_size);
-               if (!dev->platdata) {
-                       ret = -ENOMEM;
-                       goto fail_alloc1;
+       if (drv->platdata_auto_alloc_size) {
+               bool alloc = !platdata;
+
+               if (CONFIG_IS_ENABLED(OF_PLATDATA)) {
+                       if (of_platdata_size) {
+                               dev->flags |= DM_FLAG_OF_PLATDATA;
+                               if (of_platdata_size <
+                                               drv->platdata_auto_alloc_size)
+                                       alloc = true;
+                       }
+               }
+               if (alloc) {
+                       dev->flags |= DM_FLAG_ALLOC_PDATA;
+                       dev->platdata = calloc(1,
+                                              drv->platdata_auto_alloc_size);
+                       if (!dev->platdata) {
+                               ret = -ENOMEM;
+                               goto fail_alloc1;
+                       }
+                       if (CONFIG_IS_ENABLED(OF_PLATDATA) && platdata) {
+                               memcpy(dev->platdata, platdata,
+                                      of_platdata_size);
+                       }
                }
        }
 
@@ -193,10 +213,28 @@ fail_alloc1:
        return ret;
 }
 
+int device_bind_with_driver_data(struct udevice *parent,
+                                const struct driver *drv, const char *name,
+                                ulong driver_data, int of_offset,
+                                struct udevice **devp)
+{
+       return device_bind_common(parent, drv, name, NULL, driver_data,
+                                 of_offset, 0, devp);
+}
+
+int device_bind(struct udevice *parent, const struct driver *drv,
+               const char *name, void *platdata, int of_offset,
+               struct udevice **devp)
+{
+       return device_bind_common(parent, drv, name, platdata, 0, of_offset, 0,
+                                 devp);
+}
+
 int device_bind_by_name(struct udevice *parent, bool pre_reloc_only,
                        const struct driver_info *info, struct udevice **devp)
 {
        struct driver *drv;
+       uint platdata_size = 0;
 
        drv = lists_driver_lookup_name(info->name);
        if (!drv)
@@ -204,8 +242,11 @@ int device_bind_by_name(struct udevice *parent, bool pre_reloc_only,
        if (pre_reloc_only && !(drv->flags & DM_FLAG_PRE_RELOC))
                return -EPERM;
 
-       return device_bind(parent, drv, info->name, (void *)info->platdata,
-                          -1, devp);
+#if CONFIG_IS_ENABLED(OF_PLATDATA)
+       platdata_size = info->platdata_size;
+#endif
+       return device_bind_common(parent, drv, info->name,
+                       (void *)info->platdata, 0, -1, platdata_size, devp);
 }
 
 static void *alloc_priv(int size, uint flags)
@@ -314,7 +355,7 @@ int device_probe(struct udevice *dev)
                        goto fail;
        }
 
-       if (drv->ofdata_to_platdata && dev->of_offset >= 0) {
+       if (drv->ofdata_to_platdata && dev_of_offset(dev) >= 0) {
                ret = drv->ofdata_to_platdata(dev);
                if (ret)
                        goto fail;
@@ -337,7 +378,7 @@ int device_probe(struct udevice *dev)
 
        return 0;
 fail_uclass:
-       if (device_remove(dev)) {
+       if (device_remove(dev, DM_REMOVE_NORMAL)) {
                dm_warn("%s: Device '%s' failed to remove on error path\n",
                        __func__, dev->name);
        }
@@ -483,7 +524,7 @@ int device_find_child_by_of_offset(struct udevice *parent, int of_offset,
        *devp = NULL;
 
        list_for_each_entry(dev, &parent->child_head, sibling_node) {
-               if (dev->of_offset == of_offset) {
+               if (dev_of_offset(dev) == of_offset) {
                        *devp = dev;
                        return 0;
                }
@@ -508,7 +549,7 @@ static struct udevice *_device_find_global_by_of_offset(struct udevice *parent,
 {
        struct udevice *dev, *found;
 
-       if (parent->of_offset == of_offset)
+       if (dev_of_offset(parent) == of_offset)
                return parent;
 
        list_for_each_entry(dev, &parent->child_head, sibling_node) {
@@ -588,7 +629,7 @@ const char *dev_get_uclass_name(struct udevice *dev)
 
 fdt_addr_t dev_get_addr_index(struct udevice *dev, int index)
 {
-#if CONFIG_IS_ENABLED(OF_CONTROL)
+#if CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)
        fdt_addr_t addr;
 
        if (CONFIG_IS_ENABLED(OF_TRANSLATE)) {
@@ -596,19 +637,21 @@ fdt_addr_t dev_get_addr_index(struct udevice *dev, int index)
                int len = 0;
                int na, ns;
 
-               na = fdt_address_cells(gd->fdt_blob, dev->parent->of_offset);
+               na = fdt_address_cells(gd->fdt_blob,
+                                      dev_of_offset(dev->parent));
                if (na < 1) {
                        debug("bad #address-cells\n");
                        return FDT_ADDR_T_NONE;
                }
 
-               ns = fdt_size_cells(gd->fdt_blob, dev->parent->of_offset);
+               ns = fdt_size_cells(gd->fdt_blob, dev_of_offset(dev->parent));
                if (ns < 0) {
                        debug("bad #size-cells\n");
                        return FDT_ADDR_T_NONE;
                }
 
-               reg = fdt_getprop(gd->fdt_blob, dev->of_offset, "reg", &len);
+               reg = fdt_getprop(gd->fdt_blob, dev_of_offset(dev), "reg",
+                                 &len);
                if (!reg || (len <= (index * sizeof(fdt32_t) * (na + ns)))) {
                        debug("Req index out of range\n");
                        return FDT_ADDR_T_NONE;
@@ -621,16 +664,15 @@ fdt_addr_t dev_get_addr_index(struct udevice *dev, int index)
                 * bus setups.
                 */
                addr = fdt_translate_address((void *)gd->fdt_blob,
-                                            dev->of_offset, reg);
+                                            dev_of_offset(dev), reg);
        } else {
                /*
                 * Use the "simple" translate function for less complex
                 * bus setups.
                 */
                addr = fdtdec_get_addr_size_auto_parent(gd->fdt_blob,
-                                                       dev->parent->of_offset,
-                                                       dev->of_offset, "reg",
-                                                       index, NULL);
+                               dev_of_offset(dev->parent), dev_of_offset(dev),
+                               "reg", index, NULL, false);
                if (CONFIG_IS_ENABLED(SIMPLE_BUS) && addr != FDT_ADDR_T_NONE) {
                        if (device_get_uclass_id(dev->parent) ==
                            UCLASS_SIMPLE_BUS)
@@ -652,13 +694,35 @@ fdt_addr_t dev_get_addr_index(struct udevice *dev, int index)
 #endif
 }
 
+fdt_addr_t dev_get_addr_size_index(struct udevice *dev, int index,
+                                  fdt_size_t *size)
+{
+#if CONFIG_IS_ENABLED(OF_CONTROL)
+       /*
+        * Only get the size in this first call. We'll get the addr in the
+        * next call to the exisiting dev_get_xxx function which handles
+        * all config options.
+        */
+       fdtdec_get_addr_size_auto_noparent(gd->fdt_blob, dev_of_offset(dev),
+                                          "reg", index, size, false);
+
+       /*
+        * Get the base address via the existing function which handles
+        * all Kconfig cases
+        */
+       return dev_get_addr_index(dev, index);
+#else
+       return FDT_ADDR_T_NONE;
+#endif
+}
+
 fdt_addr_t dev_get_addr_name(struct udevice *dev, const char *name)
 {
 #if CONFIG_IS_ENABLED(OF_CONTROL)
        int index;
 
-       index = fdt_find_string(gd->fdt_blob, dev->of_offset, "reg-names",
-                               name);
+       index = fdt_stringlist_search(gd->fdt_blob, dev_of_offset(dev),
+                                     "reg-names", name);
        if (index < 0)
                return index;
 
@@ -678,6 +742,16 @@ void *dev_get_addr_ptr(struct udevice *dev)
        return (void *)(uintptr_t)dev_get_addr_index(dev, 0);
 }
 
+void *dev_map_physmem(struct udevice *dev, unsigned long size)
+{
+       fdt_addr_t addr = dev_get_addr(dev);
+
+       if (addr == FDT_ADDR_T_NONE)
+               return NULL;
+
+       return map_physmem(addr, size, MAP_NOCACHE);
+}
+
 bool device_has_children(struct udevice *dev)
 {
        return !list_empty(&dev->child_head);
@@ -708,7 +782,7 @@ bool device_is_last_sibling(struct udevice *dev)
 
 void device_set_name_alloced(struct udevice *dev)
 {
-       dev->flags |= DM_NAME_ALLOCED;
+       dev->flags |= DM_FLAG_NAME_ALLOCED;
 }
 
 int device_set_name(struct udevice *dev, const char *name)
@@ -726,7 +800,7 @@ bool of_device_is_compatible(struct udevice *dev, const char *compat)
 {
        const void *fdt = gd->fdt_blob;
 
-       return !fdt_node_check_compatible(fdt, dev->of_offset, compat);
+       return !fdt_node_check_compatible(fdt, dev_of_offset(dev), compat);
 }
 
 bool of_machine_is_compatible(const char *compat)
This page took 0.034272 seconds and 4 git commands to generate.