Skip to contentSkip to conversation

PATCH add support for system on chip (SoC) devices.

PATCH Ian Molton 5 participants 5 messages Apr 8, 2005 – Apr 11, 2005

Subsystems (2)
lore
Share & tools
lore

Ian Molton

author
#0 lore raw

Hi.

This patch add support for a new 'System on Chip' or SoC bus type.

This allows common drivers used in different SoC devices to be shared in a clean and healthy manner, for example, the MMC function on toshiba t7l66xb, tc6393xb, and Compaq IPAQ ASIC3.

This is in common use in the handhelds.org CVS tree.

The only real issue is that drivers using this currently tend to assume that the SoC is attached to a platfrom_bus. This can be resolved as and when it becomes an issue for people.

Please apply.

drivers/base/soc-device.c new file +239 -0
@@ -0,0 +1,239 @@
+/*
+* Driver/bus support for the system on chip (SOC)
+* On-chip devices attached to on-chip buses.
+*
+* Copyright 2003 Hewlett-Packard Company
+*
+* Use consistent with the GNU GPL is permitted, provided that this
+* copyright notice is preserved in its entirety in all copies and
+* derived works.
+*
+* HEWLET-PACKARD COMPANY MAKES NO WARRANTIES, EXPRESSED OR IMPLIED, AS
+* TO THE USEFULNESS OR CORRECTNESS OF THIS CODE OR ITS FITNESS FOR ANY
+* PARTICULAR PURPOSE.
+*
+* Author:  Jamey Hicks
+*          <[email protected]>
+*          July 2003
+*
+*/
+
+#include <linux/device.h>
+#include <linux/soc-device.h>
+#include <linux/err.h>
+#include <linux/init.h>
+#include <linux/module.h>
+#include <linux/slab.h>
+#include <linux/bitops.h>
+
+static int soc_bus_match(struct device * dev, struct device_driver *drv)
+{
+	const struct soc_device *soc_dev = to_soc_device(dev);
+	const struct soc_device_driver *soc_drv = to_soc_device_driver(drv);
+	soc_device_id *device_ids = soc_drv->device_ids;
+	soc_device_id dev_device_id = soc_dev->device_id;
+	soc_device_id drv_device_id;
+	while (drv_device_id = *device_ids++, drv_device_id.id != 0) {
+		if (drv_device_id.id == dev_device_id.id)
+			return 1;
+	}
+	return 0;
+}
+
+static int soc_bus_hotplug (struct device *dev, char **envp, 
+			    int num_envp, char *buffer, int buffer_size)
+{
+	const struct soc_device *soc_dev = to_soc_device(dev);
+	int i = 0;
+	int length = 0;
+
+	if (add_hotplug_env_var (envp, num_envp, &i,
+				 buffer, buffer_size, &length,
+				 "SOC_ID=%08x", soc_dev->device_id.id))
+		return -ENOMEM;
+
+	envp[i] = NULL;
+
+	return 0;
+}
+
+static int soc_bus_suspend(struct device * dev, u32 state)
+{
+	struct device_driver * drv = dev->driver;
+	int ret = 0;
+
+	if (drv && drv->suspend) {
+		ret = drv->suspend(dev, state, SUSPEND_DISABLE);
+		if (ret == 0)
+			ret = drv->suspend(dev, state, SUSPEND_SAVE_STATE);
+		if (ret == 0)
+			ret = drv->suspend(dev, state, SUSPEND_POWER_DOWN);
+	}
+
+	return ret;
+}
+
+static int soc_bus_resume(struct device * dev)
+{
+	struct device_driver * drv = dev->driver;
+	int ret = 0;
+
+	if (drv && drv->resume) {
+		ret = drv->resume(dev, RESUME_POWER_ON);
+		if (ret == 0)
+			ret = drv->resume(dev, RESUME_RESTORE_STATE);
+		if (ret == 0)
+			ret = drv->resume(dev, RESUME_ENABLE);
+	}
+
+	return ret;
+}
+
+struct bus_type soc_bus_type = {
+	.name = "soc",
+	.match  = soc_bus_match,
+	.hotplug = soc_bus_hotplug,
+	.suspend = soc_bus_suspend,
+	.resume  = soc_bus_resume
+};
+
+extern int soc_driver_register(struct soc_device_driver * drv)
+{
+	drv->driver.bus = &soc_bus_type;
+	return driver_register(&drv->driver);
+}
+EXPORT_SYMBOL(soc_driver_register);
+
+extern void soc_driver_unregister(struct soc_device_driver * drv)
+{
+	driver_unregister(&drv->driver);
+}
+EXPORT_SYMBOL(soc_driver_unregister);
+
+extern int soc_device_register(struct soc_device * dev)
+{
+	int i, j, rc = 0;
+	struct platform_device *pdev = to_platform_device (dev->device.parent);
+
+	dev->device.bus = &soc_bus_type;
+
+	snprintf(dev->device.bus_id, BUS_ID_SIZE, "%s", dev->name);
+
+	if (dev->num_resources) {
+		dev->parent_resource = kmalloc (dev->num_resources * sizeof (struct resource *), GFP_KERNEL);
+		if (!dev->parent_resource)
+			return -ENOMEM;
+	} else
+		dev->parent_resource = NULL;
+	dev->num_parent_resources = 0;
+
+	for (i = 0; i < dev->num_resources; i++) {
+		struct resource *res = &dev->resource[i];
+		int best = -1;
+
+		res->name = dev->device.bus_id;
+
+		/* For now just skip bus-specific resources */
+		if (res->flags & (IORESOURCE_BITS | IORESOURCE_SOC_VIRTUAL))
+			continue;
+
+		/* We must claim the platform resource containing this subresource */
+		for (j = 0; j < pdev->num_resources; j++) {
+			/* Shamelessly borrowed from pci.c */
+			struct resource *r = &pdev->resource[j];
+			if (res->start && !(res->start >= r->start && res->end <= r->end))
+				continue;	/* Not contained */
+			if ((res->flags ^ r->flags) & (IORESOURCE_IO | IORESOURCE_MEM))
+				continue;	/* Wrong type */
+			if (!((res->flags ^ r->flags) & IORESOURCE_PREFETCH)) {
+				best = j;	/* Exact match */
+				break;
+			}
+			if ((res->flags & IORESOURCE_PREFETCH) && !(r->flags & IORESOURCE_PREFETCH))
+				best = j;	/* Approximating prefetchable by non-prefetchable */
+		}
+		if (best >= 0) {
+			struct resource *r = &pdev->resource[best];
+			if ((rc = request_resource (r, res))) {
+				printk(KERN_ERR "%s: failed to claim resource %d (at %08lx-%08lx)\n",
+				       dev->name, i, res->start, res->end);
+				goto err1;
+			}
+			dev->parent_resource[dev->num_parent_resources++] = res;
+		} else {
+			if (!(res->flags & IORESOURCE_SOC_VIRTUAL)) {
+				printk (KERN_ERR "%s: cannot find resource %d in platform resources\n",
+					dev->name, i);
+				rc = -ENOENT;
+				goto err1;
+			}
+		}
+	}
+
+	if (!(rc = device_register(&dev->device)))
+		return 0;
+
+err1:
+	for (i = 0; i < dev->num_parent_resources; i++) {
+		struct resource *r = dev->parent_resource[i];
+		release_resource (r); 
+	}
+	if (dev->parent_resource)
+		kfree (dev->parent_resource);
+	return rc;
+}
+EXPORT_SYMBOL(soc_device_register);
+
+extern void soc_device_unregister(struct soc_device * dev)
+{
+	int i;
+
+	for (i = 0; i < dev->num_parent_resources; i++) {
+		struct resource *r = dev->parent_resource[i];
+		release_resource (r); 
+	}
+
+	if (dev->parent_resource)
+		kfree (dev->parent_resource);
+
+	device_unregister(&dev->device);
+}
+EXPORT_SYMBOL(soc_device_unregister);
+
+struct soc_device *soc_device_get(struct soc_device *dev)
+{
+	struct device *tmp;
+
+	if (!dev)
+		return NULL;
+
+	tmp = get_device(&dev->device);
+	if (tmp)        
+		return to_soc_device(tmp);
+	else
+		return NULL;
+}
+
+void soc_device_put(struct soc_device *dev)
+{
+	if (dev)
+		put_device(&dev->device);
+}
+
+static int __init soc_driver_init(void)
+{
+	return bus_register(&soc_bus_type);
+}
+
+static void __exit soc_driver_exit(void)
+{
+	bus_unregister (&soc_bus_type);
+}
+
+postcore_initcall(soc_driver_init);
+module_exit(soc_driver_exit);
+
+MODULE_AUTHOR("Jamey Hicks <[email protected]>");
+MODULE_DESCRIPTION("Driver/bus support for the system on chip (SOC)");
+MODULE_LICENSE("GPL");
+
include/linux/soc-device.h new file +100 -0

100 lines not on this page · raw message

Documentation/soc.txt new file +67 -0

67 lines not on this page · raw message

#1 lore raw
Quoted 3 lines · Ian Molton

Hi.

This patch add support for a new 'System on Chip' or SoC bus type.

Hi Ian.

A few general comments.
1) Please add kernel-doc style comments for all exported functions
2) Keep source within 80 coloumns
3) Do not use extern in front of function definition

Sam

#2 lore raw
In reply to Ian Molton
Quoted 8 lines · Ian Molton

Hi.

This patch add support for a new 'System on Chip' or SoC bus type.

This allows common drivers used in different SoC devices to be shared in a clean and healthy manner, for example, the MMC function on toshiba t7l66xb, tc6393xb, and Compaq IPAQ ASIC3.

Quoted 5 lines

This is in common use in the handhelds.org CVS tree.

The only real issue is that drivers using this currently tend to assume that the SoC is attached to a platfrom_bus. This can be resolved as and when it becomes an issue for people.

What is wrong with using platform_device for this? We have been doing that on PPC for SoCs for a few months now and I'm pretty sure ARM has been doing this even longer.

- kumar

#3 lore raw
In reply to Ian Molton
Quoted 3 lines · Ian Molton

This allows common drivers used in different SoC devices to be shared in a clean and healthy manner, for example, the MMC function on toshiba t7l66xb, tc6393xb, and Compaq IPAQ ASIC3.

Here's some comments on the patch itself. I'm not endorsing it by replying to it though.

Quoted 1 line

Please apply.

No. Please review first.

drivers/base/soc-device.c line 70

Quoted 11 lines
drivers/base/soc-device.c new file +11 -0
+static int soc_bus_suspend(struct device * dev, u32 state)
+{
+	struct device_driver * drv = dev->driver;
+	int ret = 0;
+
+	if (drv && drv->suspend) {
+		ret = drv->suspend(dev, state, SUSPEND_DISABLE);
+		if (ret == 0)
+			ret = drv->suspend(dev, state, SUSPEND_SAVE_STATE);
+		if (ret == 0)
+			ret = drv->suspend(dev, state, SUSPEND_POWER_DOWN);

Please don't use the obsolete "level" parameter. This appears for platform devices solely because that is how an older revision of the Linux power management infrastructure was implemented, and it was deemed to be wrong. Unfortunately, platform devices never got fixed.

Therefore, it's completely wrong for any new subsystem to re-implement this.

Quoted 6 lines
drivers/base/soc-device.c new file +6 -0
+	}
+
+	return ret;
+}
+
+static int soc_bus_resume(struct device * dev)

drivers/base/soc-device.c line 86

Quoted 10 lines
drivers/base/soc-device.c new file +10 -0
+{
+	struct device_driver * drv = dev->driver;
+	int ret = 0;
+
+	if (drv && drv->resume) {
+		ret = drv->resume(dev, RESUME_POWER_ON);
+		if (ret == 0)
+			ret = drv->resume(dev, RESUME_RESTORE_STATE);
+		if (ret == 0)
+			ret = drv->resume(dev, RESUME_ENABLE);

Ditto.

drivers/base/soc-device.c line 116

Quoted 4 lines
drivers/base/soc-device.c new file +4 -0
+extern int soc_device_register(struct soc_device * dev)
+{
+	int i, j, rc = 0;
+	struct platform_device *pdev = to_platform_device (dev->device.parent);

What if dev->device.parent is NULL? Do all SoC devices have to have a parent? How says that the parent device has to be a platform device?

Quoted 28 lines
drivers/base/soc-device.c new file +28 -0
+
+	dev->device.bus = &soc_bus_type;
+
+	snprintf(dev->device.bus_id, BUS_ID_SIZE, "%s", dev->name);
+
+	if (dev->num_resources) {
+		dev->parent_resource = kmalloc (dev->num_resources * sizeof (struct resource *), GFP_KERNEL);
+		if (!dev->parent_resource)
+			return -ENOMEM;
+	} else
+		dev->parent_resource = NULL;
+	dev->num_parent_resources = 0;
+
+	for (i = 0; i < dev->num_resources; i++) {
+		struct resource *res = &dev->resource[i];
+		int best = -1;
+
+		res->name = dev->device.bus_id;
+
+		/* For now just skip bus-specific resources */
+		if (res->flags & (IORESOURCE_BITS | IORESOURCE_SOC_VIRTUAL))
+			continue;
+
+		/* We must claim the platform resource containing this subresource */
+		for (j = 0; j < pdev->num_resources; j++) {
+			/* Shamelessly borrowed from pci.c */
+			struct resource *r = &pdev->resource[j];
+			if (res->start && !(res->start >= r->start && res->end <= r->end))

drivers/base/soc-device.c line 154

Quoted 10 lines
drivers/base/soc-device.c new file +10 -0
+				continue;	/* Not contained */
+			if ((res->flags ^ r->flags) & (IORESOURCE_IO | IORESOURCE_MEM))
+				continue;	/* Wrong type */
+			if (!((res->flags ^ r->flags) & IORESOURCE_PREFETCH)) {
+				best = j;	/* Exact match */
+				break;
+			}
+			if ((res->flags & IORESOURCE_PREFETCH) && !(r->flags & IORESOURCE_PREFETCH))
+				best = j;	/* Approximating prefetchable by non-prefetchable */
+		}

This is actually something which should be fixed for platform devices as well. Platform devices can be heirarcial as well, so this functionality should be abstracted from both this _and_ PCI.

I'm sure it also applies to other buses as well.

Quoted 10 lines
drivers/base/soc-device.c new file +8 -0
+		if (best >= 0) {
+			struct resource *r = &pdev->resource[best];
+			if ((rc = request_resource (r, res))) {
+				printk(KERN_ERR "%s: failed to claim resource %d (at %08lx-%08lx)\n",
+				       dev->name, i, res->start, res->end);
+				goto err1;
+			}
+			dev->parent_resource[dev->num_parent_resources++] = res;

From what I can tell, dev->parent_resource is misnamed. It isn't about

parent resources at all, but about which resources have been successfully requested and which haven't. You know that already - your index 'i' tells you how far through dev->resource[] you are, so when you come to clean up after an error, you start from i-1 to 0.

drivers/base/soc-device.c line 164

Quoted 2 lines
drivers/base/soc-device.c new file +2 -0
+		} else {
+			if (!(res->flags & IORESOURCE_SOC_VIRTUAL)) {

What's a SoC virtual resource?

Quoted 27 lines
drivers/base/soc-device.c new file +27 -0
+				printk (KERN_ERR "%s: cannot find resource %d in platform resources\n",
+					dev->name, i);
+				rc = -ENOENT;
+				goto err1;
+			}
+		}
+	}
+
+	if (!(rc = device_register(&dev->device)))
+		return 0;
+
+err1:
+	for (i = 0; i < dev->num_parent_resources; i++) {
+		struct resource *r = dev->parent_resource[i];
+		release_resource (r); 
+	}
+	if (dev->parent_resource)
+		kfree (dev->parent_resource);
+	return rc;
+}
+EXPORT_SYMBOL(soc_device_register);
+
+extern void soc_device_unregister(struct soc_device * dev)
+{
+	int i;
+
+	for (i = 0; i < dev->num_parent_resources; i++) {

drivers/base/soc-device.c line 201

Quoted 10 lines
drivers/base/soc-device.c new file +10 -0
+		struct resource *r = dev->parent_resource[i];
+		release_resource (r); 
+	}
+
+	if (dev->parent_resource)
+		kfree (dev->parent_resource);
+
+	device_unregister(&dev->device);
+}
+EXPORT_SYMBOL(soc_device_unregister);

How are SoC devices freed? With the infrastructure as currently written:

1. you can't use soc_device_register() with a data structure located in module data.
2. I can't see how you're handling any of the lifetime issues associated with the device model.

Quoted 4 lines
Documentation/soc.txt new file +4 -0
+Why do we need a SoC bus?
+-------------------------
+
+Particularly in embedded platforms, it is now becomming common to find that

Documentation/soc.txt line 21

Quoted 10 lines
Documentation/soc.txt new file +10 -0
+chips are being developed to contain many 'subdevices', including video
+display, audio, serial, USB, etc. From now on, the 'subdevices' will be
+referred to as 'cells'.
+
+These cells may also share local memory pools and power control, perhaps via
+common register sets.
+
+The SoC bus doesnt directly address issues relating to the above, but it does
+provide a framework for them to be tackled, and a method for registering such
+devices cleanly.

If:
1. this code doesn't address these issues,
2. apart from the heirarchial resource issue mentioned above,
3. using numeric IDs rather than strings to match devices to drivers

how is this any different from the existing platform device infrastructure?

Documentation/soc.txt line 37

Quoted 6 lines
Documentation/soc.txt new file +6 -0
+The SoC base driver handles registration and allocation of basic resources,
+along with any initialisation required to bring up the core of the device. It
+is not specified wether the base driver is to handle mapping of any IO
+regions - this is left to the discretion of the driver author to decide as
+best fits each case. Some SoC drivers pass virtual addresses to their cell
+drivers, others may pass physical addresses.

I don't see any support for this in the submitted code.

Documentation/soc.txt line 66

Quoted 5 lines
Documentation/soc.txt new file +5 -0
+Once it has setup the struct soc_device, it simply calls soc_device_register.
+
+Cell drivers work much like any normal driver - they supply probe_ and remove_
+functions, and register them in a struct soc_device_driver, via a call to
+soc_driver_register().

Again, it appears that there has been no consideration of the lifetime issues associated with the device model. These issues are not taken care of for you - failure to consider them means that it's trivially easy to cause a kernel oops.

Signature · 3 lines
Russell King
 Linux kernel    2.6 ARM Linux   - http://www.arm.linux.org.uk/
 maintainer of:  2.6 Serial core
#4 lore raw
In reply to Ian Molton
Quoted 8 lines · Ian Molton

Hi.

This patch add support for a new 'System on Chip' or SoC bus type.

This allows common drivers used in different SoC devices to be shared in a clean and healthy manner, for example, the MMC function on toshiba t7l66xb, tc6393xb, and Compaq IPAQ ASIC3.

Quoted 7 lines

This is in common use in the handhelds.org CVS tree.

The only real issue is that drivers using this currently tend to assume that the SoC is attached to a platfrom_bus. This can be resolved as and when it becomes an issue for people.

Please apply.

Sorry, but I agree with everyone else. Why do you need this? Why is this different from a platform device?

thanks,

greg k-h