Skip to content

Commit a41980f

Browse files
mszyprowRussell King
authored andcommitted
ARM: 8566/1: drivers: amba: properly handle devices with power domains
To read pid/cid registers, the probed device need to be properly turned on. When it is inside a power domain, the bus code should ensure that the given power domain is enabled before trying to access device's registers. However in some cases power domain (or clocks) might not be yet available. Returning -EPROBE_DEFER is not a solution in such case, because callers don't handle this special error code. Instead such devices are added to the special list and their registration is retried from periodic worker until all resources are available. Signed-off-by: Marek Szyprowski <m.szyprowski@samsung.com> Signed-off-by: Russell King <rmk+kernel@arm.linux.org.uk>
1 parent f55532a commit a41980f

1 file changed

Lines changed: 90 additions & 10 deletions

File tree

‎drivers/amba/bus.c‎

Lines changed: 90 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -336,16 +336,7 @@ static void amba_device_release(struct device *dev)
336336
kfree(d);
337337
}
338338

339-
/**
340-
* amba_device_add - add a previously allocated AMBA device structure
341-
* @dev: AMBA device allocated by amba_device_alloc
342-
* @parent: resource parent for this devices resources
343-
*
344-
* Claim the resource, and read the device cell ID if not already
345-
* initialized. Register the AMBA device with the Linux device
346-
* manager.
347-
*/
348-
int amba_device_add(struct amba_device *dev, struct resource *parent)
339+
static int amba_device_try_add(struct amba_device *dev, struct resource *parent)
349340
{
350341
u32 size;
351342
void __iomem *tmp;
@@ -373,6 +364,12 @@ int amba_device_add(struct amba_device *dev, struct resource *parent)
373364
goto err_release;
374365
}
375366

367+
ret = dev_pm_domain_attach(&dev->dev, true);
368+
if (ret == -EPROBE_DEFER) {
369+
iounmap(tmp);
370+
goto err_release;
371+
}
372+
376373
ret = amba_get_enable_pclk(dev);
377374
if (ret == 0) {
378375
u32 pid, cid;
@@ -398,6 +395,7 @@ int amba_device_add(struct amba_device *dev, struct resource *parent)
398395
}
399396

400397
iounmap(tmp);
398+
dev_pm_domain_detach(&dev->dev, true);
401399

402400
if (ret)
403401
goto err_release;
@@ -421,6 +419,88 @@ int amba_device_add(struct amba_device *dev, struct resource *parent)
421419
err_out:
422420
return ret;
423421
}
422+
423+
/*
424+
* Registration of AMBA device require reading its pid and cid registers.
425+
* To do this, the device must be turned on (if it is a part of power domain)
426+
* and have clocks enabled. However in some cases those resources might not be
427+
* yet available. Returning EPROBE_DEFER is not a solution in such case,
428+
* because callers don't handle this special error code. Instead such devices
429+
* are added to the special list and their registration is retried from
430+
* periodic worker, until all resources are available and registration succeeds.
431+
*/
432+
struct deferred_device {
433+
struct amba_device *dev;
434+
struct resource *parent;
435+
struct list_head node;
436+
};
437+
438+
static LIST_HEAD(deferred_devices);
439+
static DEFINE_MUTEX(deferred_devices_lock);
440+
441+
static void amba_deferred_retry_func(struct work_struct *dummy);
442+
static DECLARE_DELAYED_WORK(deferred_retry_work, amba_deferred_retry_func);
443+
444+
#define DEFERRED_DEVICE_TIMEOUT (msecs_to_jiffies(5 * 1000))
445+
446+
static void amba_deferred_retry_func(struct work_struct *dummy)
447+
{
448+
struct deferred_device *ddev, *tmp;
449+
450+
mutex_lock(&deferred_devices_lock);
451+
452+
list_for_each_entry_safe(ddev, tmp, &deferred_devices, node) {
453+
int ret = amba_device_try_add(ddev->dev, ddev->parent);
454+
455+
if (ret == -EPROBE_DEFER)
456+
continue;
457+
458+
list_del_init(&ddev->node);
459+
kfree(ddev);
460+
}
461+
462+
if (!list_empty(&deferred_devices))
463+
schedule_delayed_work(&deferred_retry_work,
464+
DEFERRED_DEVICE_TIMEOUT);
465+
466+
mutex_unlock(&deferred_devices_lock);
467+
}
468+
469+
/**
470+
* amba_device_add - add a previously allocated AMBA device structure
471+
* @dev: AMBA device allocated by amba_device_alloc
472+
* @parent: resource parent for this devices resources
473+
*
474+
* Claim the resource, and read the device cell ID if not already
475+
* initialized. Register the AMBA device with the Linux device
476+
* manager.
477+
*/
478+
int amba_device_add(struct amba_device *dev, struct resource *parent)
479+
{
480+
int ret = amba_device_try_add(dev, parent);
481+
482+
if (ret == -EPROBE_DEFER) {
483+
struct deferred_device *ddev;
484+
485+
ddev = kmalloc(sizeof(*ddev), GFP_KERNEL);
486+
if (!ddev)
487+
return -ENOMEM;
488+
489+
ddev->dev = dev;
490+
ddev->parent = parent;
491+
ret = 0;
492+
493+
mutex_lock(&deferred_devices_lock);
494+
495+
if (list_empty(&deferred_devices))
496+
schedule_delayed_work(&deferred_retry_work,
497+
DEFERRED_DEVICE_TIMEOUT);
498+
list_add_tail(&ddev->node, &deferred_devices);
499+
500+
mutex_unlock(&deferred_devices_lock);
501+
}
502+
return ret;
503+
}
424504
EXPORT_SYMBOL_GPL(amba_device_add);
425505

426506
static struct amba_device *

0 commit comments

Comments
 (0)