提交 f8a85449 编写于 作者: S Simon Glass

dm: core: Allocate platform data when binding a device

When using allocated platform data, allocate it when we bind the device.
This makes it possible to fill in this information before the device is
probed.

This fits with the platform data model (when not using device tree),
since platform data exists at bind-time.
Signed-off-by: NSimon Glass <sjg@chromium.org>
Reviewed-by: NMasahiro Yamada <yamada.m@jp.panasonic.com>
上级 72ebfe86
...@@ -88,6 +88,10 @@ int device_unbind(struct udevice *dev) ...@@ -88,6 +88,10 @@ int device_unbind(struct udevice *dev)
if (ret) if (ret)
return ret; return ret;
if (dev->flags & DM_FLAG_ALLOC_PDATA) {
free(dev->platdata);
dev->platdata = NULL;
}
ret = uclass_unbind_device(dev); ret = uclass_unbind_device(dev);
if (ret) if (ret)
return ret; return ret;
...@@ -111,10 +115,6 @@ void device_free(struct udevice *dev) ...@@ -111,10 +115,6 @@ void device_free(struct udevice *dev)
free(dev->priv); free(dev->priv);
dev->priv = NULL; dev->priv = NULL;
} }
if (dev->flags & DM_FLAG_ALLOC_PDATA) {
free(dev->platdata);
dev->platdata = NULL;
}
size = dev->uclass->uc_drv->per_device_auto_alloc_size; size = dev->uclass->uc_drv->per_device_auto_alloc_size;
if (size) { if (size) {
free(dev->uclass_priv); free(dev->uclass_priv);
......
...@@ -72,8 +72,14 @@ int device_bind(struct udevice *parent, struct driver *drv, const char *name, ...@@ -72,8 +72,14 @@ int device_bind(struct udevice *parent, struct driver *drv, const char *name,
#else #else
dev->req_seq = -1; dev->req_seq = -1;
#endif #endif
if (!dev->platdata && drv->platdata_auto_alloc_size) if (!dev->platdata && drv->platdata_auto_alloc_size) {
dev->flags |= DM_FLAG_ALLOC_PDATA; dev->flags |= DM_FLAG_ALLOC_PDATA;
dev->platdata = calloc(1, drv->platdata_auto_alloc_size);
if (!dev->platdata) {
ret = -ENOMEM;
goto fail_alloc1;
}
}
/* put dev into parent's successor list */ /* put dev into parent's successor list */
if (parent) if (parent)
...@@ -103,6 +109,11 @@ fail_bind: ...@@ -103,6 +109,11 @@ fail_bind:
fail_uclass_bind: fail_uclass_bind:
if (parent) if (parent)
list_del(&dev->sibling_node); list_del(&dev->sibling_node);
if (dev->flags & DM_FLAG_ALLOC_PDATA) {
free(dev->platdata);
dev->platdata = NULL;
}
fail_alloc1:
free(dev); free(dev);
return ret; return ret;
...@@ -139,7 +150,7 @@ int device_probe_child(struct udevice *dev, void *parent_priv) ...@@ -139,7 +150,7 @@ int device_probe_child(struct udevice *dev, void *parent_priv)
drv = dev->driver; drv = dev->driver;
assert(drv); assert(drv);
/* Allocate private data and platdata if requested */ /* Allocate private data if requested */
if (drv->priv_auto_alloc_size) { if (drv->priv_auto_alloc_size) {
dev->priv = calloc(1, drv->priv_auto_alloc_size); dev->priv = calloc(1, drv->priv_auto_alloc_size);
if (!dev->priv) { if (!dev->priv) {
...@@ -148,13 +159,6 @@ int device_probe_child(struct udevice *dev, void *parent_priv) ...@@ -148,13 +159,6 @@ int device_probe_child(struct udevice *dev, void *parent_priv)
} }
} }
/* Allocate private data if requested */ /* Allocate private data if requested */
if (dev->flags & DM_FLAG_ALLOC_PDATA) {
dev->platdata = calloc(1, drv->platdata_auto_alloc_size);
if (!dev->platdata) {
ret = -ENOMEM;
goto fail;
}
}
size = dev->uclass->uc_drv->per_device_auto_alloc_size; size = dev->uclass->uc_drv->per_device_auto_alloc_size;
if (size) { if (size) {
dev->uclass_priv = calloc(1, size); dev->uclass_priv = calloc(1, size);
......
...@@ -143,12 +143,12 @@ static int dm_test_fdt(struct dm_test_state *dms) ...@@ -143,12 +143,12 @@ static int dm_test_fdt(struct dm_test_state *dms)
/* These are num_devices compatible root-level device tree nodes */ /* These are num_devices compatible root-level device tree nodes */
ut_asserteq(num_devices, list_count_items(&uc->dev_head)); ut_asserteq(num_devices, list_count_items(&uc->dev_head));
/* Each should have no platdata / priv */ /* Each should have platform data but no private data */
for (i = 0; i < num_devices; i++) { for (i = 0; i < num_devices; i++) {
ret = uclass_find_device(UCLASS_TEST_FDT, i, &dev); ret = uclass_find_device(UCLASS_TEST_FDT, i, &dev);
ut_assert(!ret); ut_assert(!ret);
ut_assert(!dev_get_priv(dev)); ut_assert(!dev_get_priv(dev));
ut_assert(!dev->platdata); ut_assert(dev->platdata);
} }
ut_assertok(dm_check_devices(dms, num_devices)); ut_assertok(dm_check_devices(dms, num_devices));
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册