diff mbox

[2/2] drivers/amba: probe via device tree

Message ID 1305829704-11774-3-git-send-email-robherring2@gmail.com (mailing list archive)
State New, archived
Headers show

Commit Message

Rob Herring May 19, 2011, 6:28 p.m. UTC
From: Rob Herring <rob.herring@calxeda.com>

Add functions to parse the AMBA bus through the device tree.

Based on the original version by Jeremy Kerr. This reworks the original amba
bus device tree probing to be more inline with how platform bus probing via
device tree is done using a match table.

Cc: Jeremy Kerr <jeremy.kerr@canonical.com>
Cc: Grant Likely <grant.likely@secretlab.ca>
Signed-off-by: Rob Herring <rob.herring@calxeda.com>
---
 drivers/amba/bus.c       |  133 ++++++++++++++++++++++++++++++++++++++++++++++
 include/linux/amba/bus.h |    7 +++
 2 files changed, 140 insertions(+), 0 deletions(-)

Comments

Grant Likely May 19, 2011, 8:01 p.m. UTC | #1
On Thu, May 19, 2011 at 01:28:24PM -0500, Rob Herring wrote:
> From: Rob Herring <rob.herring@calxeda.com>
> 
> Add functions to parse the AMBA bus through the device tree.
> 
> Based on the original version by Jeremy Kerr. This reworks the original amba
> bus device tree probing to be more inline with how platform bus probing via
> device tree is done using a match table.
> 
> Cc: Jeremy Kerr <jeremy.kerr@canonical.com>
> Cc: Grant Likely <grant.likely@secretlab.ca>
> Signed-off-by: Rob Herring <rob.herring@calxeda.com>
> ---
>  drivers/amba/bus.c       |  133 ++++++++++++++++++++++++++++++++++++++++++++++
>  include/linux/amba/bus.h |    7 +++
>  2 files changed, 140 insertions(+), 0 deletions(-)
> 
> diff --git a/drivers/amba/bus.c b/drivers/amba/bus.c
> index 7025593..8e55754 100644
> --- a/drivers/amba/bus.c
> +++ b/drivers/amba/bus.c
> @@ -13,6 +13,11 @@
>  #include <linux/string.h>
>  #include <linux/slab.h>
>  #include <linux/io.h>
> +#include <linux/of.h>
> +#include <linux/of_irq.h>
> +#include <linux/of_address.h>
> +#include <linux/of_device.h>
> +#include <linux/of_platform.h>
>  #include <linux/pm.h>
>  #include <linux/pm_runtime.h>
>  #include <linux/amba/bus.h>
> @@ -780,3 +785,131 @@ EXPORT_SYMBOL(amba_device_unregister);
>  EXPORT_SYMBOL(amba_find_device);
>  EXPORT_SYMBOL(amba_request_regions);
>  EXPORT_SYMBOL(amba_release_regions);
> +
> +#ifdef CONFIG_OF
> +int of_amba_device_create(struct device_node *node,struct device *parent)
> +{
> +	struct amba_device *dev;
> +	const void *prop;
> +	int i, ret;
> +
> +	dev = kzalloc(sizeof(*dev), GFP_KERNEL);
> +	if (!dev)
> +		return -ENOMEM;
> +
> +	/* setup generic device info */
> +	dev->dev.coherent_dma_mask = ~0;
> +	dev->dev.of_node = node;
> +	dev->dev.parent = parent;
> +	of_device_make_bus_id(&dev->dev);
> +
> +	/* setup amba-specific device info */
> +	dev->dma_mask = ~0;
> +
> +	/* Decode the IRQs and address ranges */
> +	for (i = 0; i < AMBA_NR_IRQS; i++)
> +		dev->irq[i] = irq_of_parse_and_map(node, i);
> +
> +	ret = of_address_to_resource(node, 0, &dev->res);
> +	if (ret)
> +		goto err_free;
> +
> +	ret = amba_device_register(dev, &iomem_resource);
> +	if (ret)
> +		goto err_free;
> +
> +	/* Sanity check the arm,amba-deviceid value */
> +	prop = of_get_property(node, "arm,amba-deviceid", NULL);
> +	if (!prop)
> +		dev_warn(&dev->dev, "arm,amba-deviceid property missing; "
> +				    "probe gives 0x%08x.\n", dev->periphid);
> +
> +	if (prop && (dev->periphid != of_read_ulong(prop, 1)))
> +		dev_warn(&dev->dev, "arm,amba-deviceid value (0x%08lx) and "
> +				    "probed value (0x%08x) don't agree.",
> +				    of_read_ulong(prop, 1), dev->periphid);
> +
> +	return 0;
> +
> +err_free:
> +	kfree(dev);
> +	return ret;
> +}
> +
> +/**
> + * of_amba_bus_create() - Create a device for a node and its children.
> + * @bus: device node of the bus to instantiate
> + * @matches: match table for bus nodes
> + * disallow recursive creation of child buses
> + * @parent: parent for new device, or NULL for top level.
> + *
> + * Recursively create devices for all the child nodes.
> + */
> +static int of_amba_bus_create(struct device_node *bus,
> +			      const struct of_device_id *matches,
> +			      struct device *parent)
> +{
> +	struct of_platform_prepare_data *prep;
> +	struct device_node *child;
> +	struct platform_device *dev;
> +	int rc = 0;
> +
> +	/* Make sure it has a compatible property */
> +	if (!of_get_property(bus, "compatible", NULL)) {
> +		pr_debug("%s() - skipping %s, no compatible prop\n",
> +			 __func__, bus->full_name);
> +		return 0;
> +	}
> +
> +	if (!of_match_node(matches, bus))
> +		return 0;
> +
> +	dev = of_platform_device_create(bus, NULL, parent);
> +	if (!dev)
> +		return 0;

Hahaha, I think I see where we're getting our models crossed.

The use-case of of_amba_bus_populate should be that the device
representing the amba bus (which will be a platform device) should
have already been created before calling of_amba_bus_populate().
of_amba_bus_populate should then be responsible for creating all the
child devices that are on the AMBA bus.

of_platform_populate does the same thing, except it has some added and
*optional* helper logic that allows the populate code to dive deeper
into the device hierarchy for any nodes that match the passed in
device table.

I'd actually like to be able to integrate AMBA population in
of_platform_populate() when it encounters an AMBA bus, but I'm not
sure the result will be pretty.  I haven't had a chance to try it.

g.

> +
> +	for_each_child_of_node(bus, child) {
> +		pr_debug("   create child: %s\n", child->full_name);
> +		if (of_device_is_compatible(child, "arm,amba-device"))
> +			of_amba_device_create(child, &dev->dev);
> +		else
> +			rc = of_amba_bus_create(child, matches, &dev->dev);
> +		if (rc) {
> +			of_node_put(child);
> +			break;
> +		}
> +	}
> +	return rc;
> +}
> +
> +/**
> + * of_amba_bus_populate() - Probe the device-tree for amba buses
> + * @root: parent of the first level to probe or NULL for the root of the tree
> + * @matches: match table for bus nodes
> + * @parent: parent to hook devices from, NULL for toplevel
> + *
> + * Returns 0 on success, < 0 on failure.
> + */
> +int of_amba_bus_populate(struct device_node *root,
> +			 const struct of_device_id *matches,
> +			 struct device *parent)
> +{
> +	struct device_node *child;
> +	int rc = 0;
> +
> +	root = root ? of_node_get(root) : of_find_node_by_path("/");
> +	if (!root)
> +		return -EINVAL;
> +
> +	for_each_child_of_node(root, child) {
> +		rc = of_amba_bus_create(child, matches, parent);
> +		if (rc)
> +			break;
> +	}
> +
> +	of_node_put(root);
> +	return rc;
> +}
> +EXPORT_SYMBOL(of_amba_bus_populate);
> +
> +#endif /* CONFIG_OF */
> diff --git a/include/linux/amba/bus.h b/include/linux/amba/bus.h
> index fcbbe71..9968354 100644
> --- a/include/linux/amba/bus.h
> +++ b/include/linux/amba/bus.h
> @@ -94,4 +94,11 @@ void amba_release_regions(struct amba_device *);
>  #define amba_manf(d)	AMBA_MANF_BITS((d)->periphid)
>  #define amba_part(d)	AMBA_PART_BITS((d)->periphid)
>  
> +#ifdef CONFIG_OF
> +struct device_node;
> +int of_amba_bus_populate(struct device_node *root,
> +			 const struct of_device_id *matches,
> +			 struct device *parent);
> +#endif
> +
>  #endif
> -- 
> 1.7.4.1
>
Rob Herring May 19, 2011, 11:30 p.m. UTC | #2
Grant,

On 05/19/2011 03:01 PM, Grant Likely wrote:
> On Thu, May 19, 2011 at 01:28:24PM -0500, Rob Herring wrote:
>> From: Rob Herring<rob.herring@calxeda.com>
>>
>> Add functions to parse the AMBA bus through the device tree.
>>
>> Based on the original version by Jeremy Kerr. This reworks the original amba
>> bus device tree probing to be more inline with how platform bus probing via
>> device tree is done using a match table.
>>
>> Cc: Jeremy Kerr<jeremy.kerr@canonical.com>
>> Cc: Grant Likely<grant.likely@secretlab.ca>
>> Signed-off-by: Rob Herring<rob.herring@calxeda.com>
>> ---
>>   drivers/amba/bus.c       |  133 ++++++++++++++++++++++++++++++++++++++++++++++
>>   include/linux/amba/bus.h |    7 +++
>>   2 files changed, 140 insertions(+), 0 deletions(-)
>>
>> diff --git a/drivers/amba/bus.c b/drivers/amba/bus.c
>> index 7025593..8e55754 100644
>> --- a/drivers/amba/bus.c
>> +++ b/drivers/amba/bus.c
>> @@ -13,6 +13,11 @@
>>   #include<linux/string.h>
>>   #include<linux/slab.h>
>>   #include<linux/io.h>
>> +#include<linux/of.h>
>> +#include<linux/of_irq.h>
>> +#include<linux/of_address.h>
>> +#include<linux/of_device.h>
>> +#include<linux/of_platform.h>
>>   #include<linux/pm.h>
>>   #include<linux/pm_runtime.h>
>>   #include<linux/amba/bus.h>
>> @@ -780,3 +785,131 @@ EXPORT_SYMBOL(amba_device_unregister);
>>   EXPORT_SYMBOL(amba_find_device);
>>   EXPORT_SYMBOL(amba_request_regions);
>>   EXPORT_SYMBOL(amba_release_regions);
>> +
>> +#ifdef CONFIG_OF
>> +int of_amba_device_create(struct device_node *node,struct device *parent)
>> +{
>> +	struct amba_device *dev;
>> +	const void *prop;
>> +	int i, ret;
>> +
>> +	dev = kzalloc(sizeof(*dev), GFP_KERNEL);
>> +	if (!dev)
>> +		return -ENOMEM;
>> +
>> +	/* setup generic device info */
>> +	dev->dev.coherent_dma_mask = ~0;
>> +	dev->dev.of_node = node;
>> +	dev->dev.parent = parent;
>> +	of_device_make_bus_id(&dev->dev);
>> +
>> +	/* setup amba-specific device info */
>> +	dev->dma_mask = ~0;
>> +
>> +	/* Decode the IRQs and address ranges */
>> +	for (i = 0; i<  AMBA_NR_IRQS; i++)
>> +		dev->irq[i] = irq_of_parse_and_map(node, i);
>> +
>> +	ret = of_address_to_resource(node, 0,&dev->res);
>> +	if (ret)
>> +		goto err_free;
>> +
>> +	ret = amba_device_register(dev,&iomem_resource);
>> +	if (ret)
>> +		goto err_free;
>> +
>> +	/* Sanity check the arm,amba-deviceid value */
>> +	prop = of_get_property(node, "arm,amba-deviceid", NULL);
>> +	if (!prop)
>> +		dev_warn(&dev->dev, "arm,amba-deviceid property missing; "
>> +				    "probe gives 0x%08x.\n", dev->periphid);
>> +
>> +	if (prop&&  (dev->periphid != of_read_ulong(prop, 1)))
>> +		dev_warn(&dev->dev, "arm,amba-deviceid value (0x%08lx) and "
>> +				    "probed value (0x%08x) don't agree.",
>> +				    of_read_ulong(prop, 1), dev->periphid);
>> +
>> +	return 0;
>> +
>> +err_free:
>> +	kfree(dev);
>> +	return ret;
>> +}
>> +
>> +/**
>> + * of_amba_bus_create() - Create a device for a node and its children.
>> + * @bus: device node of the bus to instantiate
>> + * @matches: match table for bus nodes
>> + * disallow recursive creation of child buses
>> + * @parent: parent for new device, or NULL for top level.
>> + *
>> + * Recursively create devices for all the child nodes.
>> + */
>> +static int of_amba_bus_create(struct device_node *bus,
>> +			      const struct of_device_id *matches,
>> +			      struct device *parent)
>> +{
>> +	struct of_platform_prepare_data *prep;
>> +	struct device_node *child;
>> +	struct platform_device *dev;
>> +	int rc = 0;
>> +
>> +	/* Make sure it has a compatible property */
>> +	if (!of_get_property(bus, "compatible", NULL)) {
>> +		pr_debug("%s() - skipping %s, no compatible prop\n",
>> +			 __func__, bus->full_name);
>> +		return 0;
>> +	}
>> +
>> +	if (!of_match_node(matches, bus))
>> +		return 0;
>> +
>> +	dev = of_platform_device_create(bus, NULL, parent);
>> +	if (!dev)
>> +		return 0;
>
> Hahaha, I think I see where we're getting our models crossed.
>
> The use-case of of_amba_bus_populate should be that the device
> representing the amba bus (which will be a platform device) should
> have already been created before calling of_amba_bus_populate().
> of_amba_bus_populate should then be responsible for creating all the
> child devices that are on the AMBA bus.
>

That was one option I had thought about. I happened to put amba call 
first so I hit the issue. It's a bit fragile to require a certain 
calling order.

> of_platform_populate does the same thing, except it has some added and
> *optional* helper logic that allows the populate code to dive deeper
> into the device hierarchy for any nodes that match the passed in
> device table.
>
That's a bit of an orthogonal problem. In my case, all devices are 
created from the DT. This issue is scanning the devicetree multiple times.

> I'd actually like to be able to integrate AMBA population in
> of_platform_populate() when it encounters an AMBA bus, but I'm not
> sure the result will be pretty.  I haven't had a chance to try it.
>

That seems like a bad idea to me. It's fine with 1 other bus type, but 
if you had 10 others it would get messy.

Perhaps scanning the tree for buses first and then scanning for devices 
is a better solution.

Rob
Grant Likely May 19, 2011, 11:39 p.m. UTC | #3
On Thu, May 19, 2011 at 06:30:23PM -0500, Rob Herring wrote:
> Grant,
> 
> On 05/19/2011 03:01 PM, Grant Likely wrote:
> >On Thu, May 19, 2011 at 01:28:24PM -0500, Rob Herring wrote:
> >>From: Rob Herring<rob.herring@calxeda.com>
> >>
> >>Add functions to parse the AMBA bus through the device tree.
> >>
> >>Based on the original version by Jeremy Kerr. This reworks the original amba
> >>bus device tree probing to be more inline with how platform bus probing via
> >>device tree is done using a match table.
> >>
> >>Cc: Jeremy Kerr<jeremy.kerr@canonical.com>
> >>Cc: Grant Likely<grant.likely@secretlab.ca>
> >>Signed-off-by: Rob Herring<rob.herring@calxeda.com>
> >>---
> >>  drivers/amba/bus.c       |  133 ++++++++++++++++++++++++++++++++++++++++++++++
> >>  include/linux/amba/bus.h |    7 +++
> >>  2 files changed, 140 insertions(+), 0 deletions(-)
> >>
> >>diff --git a/drivers/amba/bus.c b/drivers/amba/bus.c
> >>index 7025593..8e55754 100644
> >>--- a/drivers/amba/bus.c
> >>+++ b/drivers/amba/bus.c
> >>@@ -13,6 +13,11 @@
> >>  #include<linux/string.h>
> >>  #include<linux/slab.h>
> >>  #include<linux/io.h>
> >>+#include<linux/of.h>
> >>+#include<linux/of_irq.h>
> >>+#include<linux/of_address.h>
> >>+#include<linux/of_device.h>
> >>+#include<linux/of_platform.h>
> >>  #include<linux/pm.h>
> >>  #include<linux/pm_runtime.h>
> >>  #include<linux/amba/bus.h>
> >>@@ -780,3 +785,131 @@ EXPORT_SYMBOL(amba_device_unregister);
> >>  EXPORT_SYMBOL(amba_find_device);
> >>  EXPORT_SYMBOL(amba_request_regions);
> >>  EXPORT_SYMBOL(amba_release_regions);
> >>+
> >>+#ifdef CONFIG_OF
> >>+int of_amba_device_create(struct device_node *node,struct device *parent)
> >>+{
> >>+	struct amba_device *dev;
> >>+	const void *prop;
> >>+	int i, ret;
> >>+
> >>+	dev = kzalloc(sizeof(*dev), GFP_KERNEL);
> >>+	if (!dev)
> >>+		return -ENOMEM;
> >>+
> >>+	/* setup generic device info */
> >>+	dev->dev.coherent_dma_mask = ~0;
> >>+	dev->dev.of_node = node;
> >>+	dev->dev.parent = parent;
> >>+	of_device_make_bus_id(&dev->dev);
> >>+
> >>+	/* setup amba-specific device info */
> >>+	dev->dma_mask = ~0;
> >>+
> >>+	/* Decode the IRQs and address ranges */
> >>+	for (i = 0; i<  AMBA_NR_IRQS; i++)
> >>+		dev->irq[i] = irq_of_parse_and_map(node, i);
> >>+
> >>+	ret = of_address_to_resource(node, 0,&dev->res);
> >>+	if (ret)
> >>+		goto err_free;
> >>+
> >>+	ret = amba_device_register(dev,&iomem_resource);
> >>+	if (ret)
> >>+		goto err_free;
> >>+
> >>+	/* Sanity check the arm,amba-deviceid value */
> >>+	prop = of_get_property(node, "arm,amba-deviceid", NULL);
> >>+	if (!prop)
> >>+		dev_warn(&dev->dev, "arm,amba-deviceid property missing; "
> >>+				    "probe gives 0x%08x.\n", dev->periphid);
> >>+
> >>+	if (prop&&  (dev->periphid != of_read_ulong(prop, 1)))
> >>+		dev_warn(&dev->dev, "arm,amba-deviceid value (0x%08lx) and "
> >>+				    "probed value (0x%08x) don't agree.",
> >>+				    of_read_ulong(prop, 1), dev->periphid);
> >>+
> >>+	return 0;
> >>+
> >>+err_free:
> >>+	kfree(dev);
> >>+	return ret;
> >>+}
> >>+
> >>+/**
> >>+ * of_amba_bus_create() - Create a device for a node and its children.
> >>+ * @bus: device node of the bus to instantiate
> >>+ * @matches: match table for bus nodes
> >>+ * disallow recursive creation of child buses
> >>+ * @parent: parent for new device, or NULL for top level.
> >>+ *
> >>+ * Recursively create devices for all the child nodes.
> >>+ */
> >>+static int of_amba_bus_create(struct device_node *bus,
> >>+			      const struct of_device_id *matches,
> >>+			      struct device *parent)
> >>+{
> >>+	struct of_platform_prepare_data *prep;
> >>+	struct device_node *child;
> >>+	struct platform_device *dev;
> >>+	int rc = 0;
> >>+
> >>+	/* Make sure it has a compatible property */
> >>+	if (!of_get_property(bus, "compatible", NULL)) {
> >>+		pr_debug("%s() - skipping %s, no compatible prop\n",
> >>+			 __func__, bus->full_name);
> >>+		return 0;
> >>+	}
> >>+
> >>+	if (!of_match_node(matches, bus))
> >>+		return 0;
> >>+
> >>+	dev = of_platform_device_create(bus, NULL, parent);
> >>+	if (!dev)
> >>+		return 0;
> >
> >Hahaha, I think I see where we're getting our models crossed.
> >
> >The use-case of of_amba_bus_populate should be that the device
> >representing the amba bus (which will be a platform device) should
> >have already been created before calling of_amba_bus_populate().
> >of_amba_bus_populate should then be responsible for creating all the
> >child devices that are on the AMBA bus.
> >
> 
> That was one option I had thought about. I happened to put amba call
> first so I hit the issue. It's a bit fragile to require a certain
> calling order.

Yes, so the goal is not to require a calling order.

> 
> >of_platform_populate does the same thing, except it has some added and
> >*optional* helper logic that allows the populate code to dive deeper
> >into the device hierarchy for any nodes that match the passed in
> >device table.
> >
> That's a bit of an orthogonal problem. In my case, all devices are
> created from the DT. This issue is scanning the devicetree multiple
> times.
> 
> >I'd actually like to be able to integrate AMBA population in
> >of_platform_populate() when it encounters an AMBA bus, but I'm not
> >sure the result will be pretty.  I haven't had a chance to try it.
> >
> 
> That seems like a bad idea to me. It's fine with 1 other bus type,
> but if you had 10 others it would get messy.
> 
> Perhaps scanning the tree for buses first and then scanning for
> devices is a better solution.

However, it would all work with one call if the match table passed
into of_platform_populate() also included a populate hook for each bus
type.  That way it still is up to the platform as to which bus types
to probe.  Or a default list of bus types could be provided too.

g.
Rob Herring May 24, 2011, 3:03 p.m. UTC | #4
Grant,

On 05/23/2011 10:09 AM, Grant Likely wrote:
> On Mon, May 23, 2011 at 3:58 AM, Russell King - ARM Linux
> <linux@arm.linux.org.uk>  wrote:
>> On Mon, May 23, 2011 at 11:37:04AM +0200, Kristoffer Glembo wrote:
>>> Grant Likely wrote:
>>>> In the case we're talking about the bus really is an AMBA bus, and all
>>>> the devices on it are in some sense real amba devices.  The problem is
>>>> that not all of the devices on the bus implement peripheral ID
>>>> registers or other mechanisms that good upstanding AMBA devices are
>>>> expected to have.
>>>
>>> Before we go hardware bashing of non primecell AMBA devices I would just
>>> want to point out that the primecell stuff is not part of the AMBA
>>> specification.
>>
>> And before we go down that route, let me point out that the 'amba bus'
>> stuff in the kernel is there to support primecells, rather than all
>> devices which the AMBA specification covers.
>>
>> The reason it's called 'amba' is because back in 2001 or so when the
>> first primecell drivers were created, there was little information
>> available as to what AMBA, AHB, or APB even covered.  All I had to go
>> on were the primecell documents themselves.  The higher level documents
>> were not available to me.
>>
>> So, despite it being called 'amba', it really is just for primecells
>> and if we didn't have the exposure to userspace, I'd have renamed it to
>> 'apb' or similar instead.
>
> Okay, that clarifies things a lot, and lends weight to the arguement
> that it is perfectly normal and acceptable to have both amba_devices
> and platform_devices on the same bus segment.  Are there any cases
> where amba primecells are being driven by platform_drivers?  If so,
> should those drivers have an amba_driver registration added?

I would be surprised if there are any implemented as platform_drivers 
that are not duplicates of an amba driver. The STMP uart is actually a 
pl011 and it's platform driver was recently removed IIRC. So I think we 
can consider platform drivers something that should be fixed in this case.

Do you still think we should have a global match table of all devices or 
a generic "arm,primecell" compatible property would work. Several 
drivers like the pl022 have several h/w variations they support, so we 
would either need to list all those variations or have a generic name 
per device.

I think having "arm,amba-deviceid" is not needed. The current code does 
nothing but warn if it doesn't match the h/w value. The drivers already 
have a list of id's that they support and the amba bus only matches 
against the h/w id value. The only use I can see is overriding a broken 
h/w value. Certainly seems like it should be optional at least.

Rob
Shawn Guo May 25, 2011, 3:02 a.m. UTC | #5
On Tue, May 24, 2011 at 10:03:35AM -0500, Rob Herring wrote:
> Grant,
> 
> On 05/23/2011 10:09 AM, Grant Likely wrote:
> >On Mon, May 23, 2011 at 3:58 AM, Russell King - ARM Linux
> ><linux@arm.linux.org.uk>  wrote:
> >>On Mon, May 23, 2011 at 11:37:04AM +0200, Kristoffer Glembo wrote:
> >>>Grant Likely wrote:
> >>>>In the case we're talking about the bus really is an AMBA bus, and all
> >>>>the devices on it are in some sense real amba devices.  The problem is
> >>>>that not all of the devices on the bus implement peripheral ID
> >>>>registers or other mechanisms that good upstanding AMBA devices are
> >>>>expected to have.
> >>>
> >>>Before we go hardware bashing of non primecell AMBA devices I would just
> >>>want to point out that the primecell stuff is not part of the AMBA
> >>>specification.
> >>
> >>And before we go down that route, let me point out that the 'amba bus'
> >>stuff in the kernel is there to support primecells, rather than all
> >>devices which the AMBA specification covers.
> >>
> >>The reason it's called 'amba' is because back in 2001 or so when the
> >>first primecell drivers were created, there was little information
> >>available as to what AMBA, AHB, or APB even covered.  All I had to go
> >>on were the primecell documents themselves.  The higher level documents
> >>were not available to me.
> >>
> >>So, despite it being called 'amba', it really is just for primecells
> >>and if we didn't have the exposure to userspace, I'd have renamed it to
> >>'apb' or similar instead.
> >
> >Okay, that clarifies things a lot, and lends weight to the arguement
> >that it is perfectly normal and acceptable to have both amba_devices
> >and platform_devices on the same bus segment.  Are there any cases
> >where amba primecells are being driven by platform_drivers?  If so,
> >should those drivers have an amba_driver registration added?
> 
> I would be surprised if there are any implemented as
> platform_drivers that are not duplicates of an amba driver. The STMP
> uart is actually a pl011 and it's platform driver was recently

It (duart than auart) is a platform driver in Freesccale BSP, and was
turned into 'amba' one when being upstreamed.

> removed IIRC. So I think we can consider platform drivers something
> that should be fixed in this case.
>
Linus Walleij May 25, 2011, 9:07 a.m. UTC | #6
2011/5/24 Rob Herring <robherring2@gmail.com>:

> I think having "arm,amba-deviceid" is not needed. The current code does
> nothing but warn if it doesn't match the h/w value. The drivers already have
> a list of id's that they support and the amba bus only matches against the
> h/w id value. The only use I can see is overriding a broken h/w value.

We have this usecase in the Ux500. See these patches:
http://www.arm.linux.org.uk/developer/patches/viewpatch.php?id=6829/1
http://www.arm.linux.org.uk/developer/patches/viewpatch.php?id=6830/1

Alas, it's not yet merged for the old boardfile world usecase, and
causing us problems to drive our hardware already.

Yours,
Linus Walleij
diff mbox

Patch

diff --git a/drivers/amba/bus.c b/drivers/amba/bus.c
index 7025593..8e55754 100644
--- a/drivers/amba/bus.c
+++ b/drivers/amba/bus.c
@@ -13,6 +13,11 @@ 
 #include <linux/string.h>
 #include <linux/slab.h>
 #include <linux/io.h>
+#include <linux/of.h>
+#include <linux/of_irq.h>
+#include <linux/of_address.h>
+#include <linux/of_device.h>
+#include <linux/of_platform.h>
 #include <linux/pm.h>
 #include <linux/pm_runtime.h>
 #include <linux/amba/bus.h>
@@ -780,3 +785,131 @@  EXPORT_SYMBOL(amba_device_unregister);
 EXPORT_SYMBOL(amba_find_device);
 EXPORT_SYMBOL(amba_request_regions);
 EXPORT_SYMBOL(amba_release_regions);
+
+#ifdef CONFIG_OF
+int of_amba_device_create(struct device_node *node,struct device *parent)
+{
+	struct amba_device *dev;
+	const void *prop;
+	int i, ret;
+
+	dev = kzalloc(sizeof(*dev), GFP_KERNEL);
+	if (!dev)
+		return -ENOMEM;
+
+	/* setup generic device info */
+	dev->dev.coherent_dma_mask = ~0;
+	dev->dev.of_node = node;
+	dev->dev.parent = parent;
+	of_device_make_bus_id(&dev->dev);
+
+	/* setup amba-specific device info */
+	dev->dma_mask = ~0;
+
+	/* Decode the IRQs and address ranges */
+	for (i = 0; i < AMBA_NR_IRQS; i++)
+		dev->irq[i] = irq_of_parse_and_map(node, i);
+
+	ret = of_address_to_resource(node, 0, &dev->res);
+	if (ret)
+		goto err_free;
+
+	ret = amba_device_register(dev, &iomem_resource);
+	if (ret)
+		goto err_free;
+
+	/* Sanity check the arm,amba-deviceid value */
+	prop = of_get_property(node, "arm,amba-deviceid", NULL);
+	if (!prop)
+		dev_warn(&dev->dev, "arm,amba-deviceid property missing; "
+				    "probe gives 0x%08x.\n", dev->periphid);
+
+	if (prop && (dev->periphid != of_read_ulong(prop, 1)))
+		dev_warn(&dev->dev, "arm,amba-deviceid value (0x%08lx) and "
+				    "probed value (0x%08x) don't agree.",
+				    of_read_ulong(prop, 1), dev->periphid);
+
+	return 0;
+
+err_free:
+	kfree(dev);
+	return ret;
+}
+
+/**
+ * of_amba_bus_create() - Create a device for a node and its children.
+ * @bus: device node of the bus to instantiate
+ * @matches: match table for bus nodes
+ * disallow recursive creation of child buses
+ * @parent: parent for new device, or NULL for top level.
+ *
+ * Recursively create devices for all the child nodes.
+ */
+static int of_amba_bus_create(struct device_node *bus,
+			      const struct of_device_id *matches,
+			      struct device *parent)
+{
+	struct of_platform_prepare_data *prep;
+	struct device_node *child;
+	struct platform_device *dev;
+	int rc = 0;
+
+	/* Make sure it has a compatible property */
+	if (!of_get_property(bus, "compatible", NULL)) {
+		pr_debug("%s() - skipping %s, no compatible prop\n",
+			 __func__, bus->full_name);
+		return 0;
+	}
+
+	if (!of_match_node(matches, bus))
+		return 0;
+
+	dev = of_platform_device_create(bus, NULL, parent);
+	if (!dev)
+		return 0;
+
+	for_each_child_of_node(bus, child) {
+		pr_debug("   create child: %s\n", child->full_name);
+		if (of_device_is_compatible(child, "arm,amba-device"))
+			of_amba_device_create(child, &dev->dev);
+		else
+			rc = of_amba_bus_create(child, matches, &dev->dev);
+		if (rc) {
+			of_node_put(child);
+			break;
+		}
+	}
+	return rc;
+}
+
+/**
+ * of_amba_bus_populate() - Probe the device-tree for amba buses
+ * @root: parent of the first level to probe or NULL for the root of the tree
+ * @matches: match table for bus nodes
+ * @parent: parent to hook devices from, NULL for toplevel
+ *
+ * Returns 0 on success, < 0 on failure.
+ */
+int of_amba_bus_populate(struct device_node *root,
+			 const struct of_device_id *matches,
+			 struct device *parent)
+{
+	struct device_node *child;
+	int rc = 0;
+
+	root = root ? of_node_get(root) : of_find_node_by_path("/");
+	if (!root)
+		return -EINVAL;
+
+	for_each_child_of_node(root, child) {
+		rc = of_amba_bus_create(child, matches, parent);
+		if (rc)
+			break;
+	}
+
+	of_node_put(root);
+	return rc;
+}
+EXPORT_SYMBOL(of_amba_bus_populate);
+
+#endif /* CONFIG_OF */
diff --git a/include/linux/amba/bus.h b/include/linux/amba/bus.h
index fcbbe71..9968354 100644
--- a/include/linux/amba/bus.h
+++ b/include/linux/amba/bus.h
@@ -94,4 +94,11 @@  void amba_release_regions(struct amba_device *);
 #define amba_manf(d)	AMBA_MANF_BITS((d)->periphid)
 #define amba_part(d)	AMBA_PART_BITS((d)->periphid)
 
+#ifdef CONFIG_OF
+struct device_node;
+int of_amba_bus_populate(struct device_node *root,
+			 const struct of_device_id *matches,
+			 struct device *parent);
+#endif
+
 #endif