u-boot device_alloc_priv函数
分配设备的私有数据
static int device_alloc_priv(struct udevice *dev)
{
const struct driver *drv;
void *ptr;
int size;
//通过设备来获得他的驱动
drv = dev->driver;
assert(drv);
//设定dev->priv指针和分配空间
/* Allocate private data if requested and not reentered */
if (drv->priv_auto && !dev_get_priv(dev)) {
ptr = alloc_priv(drv->priv_auto, drv->flags);
if (!ptr)
return -ENOMEM;
dev_set_priv(dev, ptr);
}
//设定dev->uclass_priv_指针和分配空间
/* Allocate private data if requested and not reentered */
size = dev->uclass->uc_drv->per_device_auto;
if (size && !dev_get_uclass_priv(dev)) {
ptr = alloc_priv(size, dev->uclass->uc_drv->flags);
if (!ptr)
return -ENOMEM;
dev_set_uclass_priv(dev, ptr);
}
//设定dev->parent_priv_指针和分配空间
/* Allocate parent data for this child */
if (dev->parent) {
size = dev->parent->driver->per_child_auto;
if (!size)
size = dev->parent->uclass->uc_drv->per_child_auto;
if (size && !dev_get_parent_priv(dev)) {
ptr = alloc_priv(size, drv->flags);
if (!ptr)
return -ENOMEM;
dev_set_parent_priv(dev, ptr);
}
}
return 0;
}
//
//DM_FLAG_ALLOC_PRIV_DMA
//
static void *alloc_priv(int size, uint flags)
{
void *priv;
if (flags & DM_FLAG_ALLOC_PRIV_DMA) { size = ROUND(size, ARCH_DMA_MINALIGN); priv = memalign(ARCH_DMA_MINALIGN, size); if (priv) { memset(priv, '\0', size); #ifndef CONFIG_MICROBLAZE flush_dcache_range((ulong)priv, (ulong)priv + size); #endif } } else { priv = calloc(1, size); }
return priv; }