
From: Paul Mundt <paul.mundt@nokia.com>

This adds support for the SuperHyway bus currently used by sh64 and also us
ed by newer sh4a cores.  This just adds the common bus support, with things
like bus scanning being architecture dependent.  sh64 presently makes use
of this for its DMAC, with further device support planned.

I've added this under drivers/sh/superhyway/ as there are some other
sh-specific buses that I intend to add under drivers/sh/ at a later point. 
Some of the sh dma code is also intended for consolidation there so sh64
can use it as well.

Signed-off-by: Paul Mundt <paul.mundt@nokia.com>
Signed-off-by: Andrew Morton <akpm@osdl.org>
---

 25-akpm/drivers/Makefile                         |    1 
 25-akpm/drivers/sh/Makefile                      |    6 
 25-akpm/drivers/sh/superhyway/Makefile           |   18 +
 25-akpm/drivers/sh/superhyway/superhyway-sysfs.c |   50 ++++
 25-akpm/drivers/sh/superhyway/superhyway.c       |  234 +++++++++++++++++++++++
 25-akpm/drivers/sh/superhyway/superhyway.ids     |   13 +
 25-akpm/include/linux/superhyway.h               |   96 +++++++++
 7 files changed, 418 insertions(+)

diff -puN drivers/Makefile~superhyway-bus-support drivers/Makefile
--- 25/drivers/Makefile~superhyway-bus-support	Mon Nov  8 15:01:00 2004
+++ 25-akpm/drivers/Makefile	Mon Nov  8 15:01:00 2004
@@ -60,3 +60,4 @@ obj-$(CONFIG_EISA)		+= eisa/
 obj-$(CONFIG_CPU_FREQ)		+= cpufreq/
 obj-$(CONFIG_MMC)		+= mmc/
 obj-y				+= firmware/
+obj-$(CONFIG_SUPERH)		+= sh/
diff -puN /dev/null drivers/sh/Makefile
--- /dev/null	Thu Apr 11 07:25:15 2002
+++ 25-akpm/drivers/sh/Makefile	Mon Nov  8 15:01:00 2004
@@ -0,0 +1,6 @@
+#
+# Makefile for the SuperH specific drivers.
+#
+
+obj-$(CONFIG_SUPERHYWAY) += superhyway/
+
diff -puN /dev/null drivers/sh/superhyway/Makefile
--- /dev/null	Thu Apr 11 07:25:15 2002
+++ 25-akpm/drivers/sh/superhyway/Makefile	Mon Nov  8 15:01:00 2004
@@ -0,0 +1,18 @@
+#
+# Makefile for the SuperHyway bus drivers.
+#
+
+obj-$(CONFIG_SUPERHYWAY)	+= superhyway.o
+obj-$(CONFIG_SYSFS)		+= superhyway-sysfs.o
+
+clean-files := devlist.h
+
+$(obj)/superhyway.o: $(obj)/devlist.h
+
+quiet_cmd_devlist = GEN     $@
+      cmd_devlist = sed -e 's/^\#.*$$//g;s/^[ ].*$$//g;/^$$/d; \
+		    	    s/\(0x.*\)	/	{ \1, /g;s/$$/ },/' < $< > $@
+
+$(obj)/devlist.h: $(src)/superhyway.ids
+	$(call cmd,devlist)
+
diff -puN /dev/null drivers/sh/superhyway/superhyway.c
--- /dev/null	Thu Apr 11 07:25:15 2002
+++ 25-akpm/drivers/sh/superhyway/superhyway.c	Mon Nov  8 15:01:00 2004
@@ -0,0 +1,234 @@
+/*
+ * drivers/sh/superhyway/superhyway.c
+ *
+ * SuperHyway Bus Driver
+ *
+ * Copyright (C) 2004  Paul Mundt <lethal@linux-sh.org>
+ *
+ * This file is subject to the terms and conditions of the GNU General Public
+ * License.  See the file "COPYING" in the main directory of this archive
+ * for more details.
+ *
+ */
+
+#include <linux/kernel.h>
+#include <linux/device.h>
+#include <linux/init.h>
+#include <linux/module.h>
+#include <linux/types.h>
+#include <linux/list.h>
+#include <linux/superhyway.h>
+
+static struct superhyway_device_info {
+	unsigned int id;
+	const char *name;
+} superhyway_device_table[] __initdata = {
+#include "devlist.h"
+};
+
+static int superhyway_devices;
+
+static struct superhyway_bus superhyway_bus = {
+	.name	= "SuperHyway bus",
+};
+
+static void superhyway_name_device(struct superhyway_device *dev)
+{
+	int i;
+
+	for (i = 0; i < ARRAY_SIZE(superhyway_device_table); i++)
+		if (superhyway_device_table[i].id == dev->id.id)
+			sprintf(dev->pretty_name, "%s",
+				superhyway_device_table[i].name);
+}
+
+/**
+ * superhyway_add_device - Add a SuperHyway module
+ * @mod_id: Module ID (taken from MODULE.VCR.MOD_ID).
+ * @base: Physical address where module is mapped.
+ * @vcr: VCR value.
+ *
+ * This is responsible for adding a new SuperHyway module. This sets up a new
+ * struct superhyway_device for the module being added. Each one of @mod_id,
+ * @base, and @vcr are registered with the new device for further use
+ * elsewhere.
+ *
+ * Devices are initially added in the order that they are scanned (from the
+ * top-down of the memory map), and are assigned an ID based on the order that
+ * they are added. Any manual addition of a module will thus get the ID after
+ * the devices already discovered regardless of where it resides in memory.
+ *
+ * Further work can and should be done in superhyway_scan_bus(), to be sure
+ * that any new modules are properly discovered and subsequently registered.
+ */
+int superhyway_add_device(unsigned int mod_id, unsigned long base,
+			  unsigned long long vcr)
+{
+	struct superhyway_device *dev;
+	int ret;
+
+	dev = kmalloc(sizeof(struct superhyway_device), GFP_KERNEL);
+	if (!dev)
+		return -ENOMEM;
+
+	memset(dev, 0, sizeof(struct superhyway_device));
+
+	dev->id.id = mod_id;
+	sprintf(dev->name, "SuperHyway device %04x\n", dev->id.id);
+
+	superhyway_name_device(dev);
+
+	dev->vcr		= *((struct vcr_info *)(&vcr));
+	dev->resource.name	= dev->name;
+	dev->resource.start	= base;
+	dev->resource.end	= dev->resource.start + 0x01000000;
+	dev->dev.parent		= &superhyway_bus.dev;
+	dev->dev.bus		= &superhyway_bus_type;
+
+	sprintf(dev->dev.bus_id, "%02x", superhyway_devices);
+
+	pr_info("    0x%04x (%s) control block at 0x%08lx\n",
+		dev->id.id, dev->pretty_name, dev->resource.start);
+
+	superhyway_devices++;
+
+	list_add(&dev->node, &superhyway_bus.devices);
+
+	ret = device_register(&dev->dev);
+	superhyway_create_sysfs_files(dev);
+
+	return ret;
+}
+
+static int __init superhyway_init(void)
+{
+	extern int superhyway_scan_bus(void);
+
+	INIT_LIST_HEAD(&superhyway_bus.devices);
+	strcpy(superhyway_bus.dev.bus_id, "superhyway");
+	device_register(&superhyway_bus.dev);
+
+	return superhyway_scan_bus();
+}
+
+postcore_initcall(superhyway_init);
+
+static const struct superhyway_device_id *
+superhyway_match_id(const struct superhyway_device_id *ids,
+		    struct superhyway_device *dev)
+{
+	while (ids->id) {
+		if (ids->id == dev->id.id)
+			return ids;
+
+		ids++;
+	}
+
+	return NULL;
+}
+
+static int superhyway_device_probe(struct device *dev)
+{
+	struct superhyway_device *shyway_dev = to_superhyway_device(dev);
+	struct superhyway_driver *shyway_drv = to_superhyway_driver(dev->driver);
+
+	if (shyway_drv && shyway_drv->probe) {
+		const struct superhyway_device_id *id;
+
+		id = superhyway_match_id(shyway_drv->id_table, shyway_dev);
+		if (id)
+			return shyway_drv->probe(shyway_dev, id);
+	}
+
+	return -ENODEV;
+}
+
+static int superhyway_device_remove(struct device *dev)
+{
+	struct superhyway_device *shyway_dev = to_superhyway_device(dev);
+	struct superhyway_driver *shyway_drv = to_superhyway_driver(dev->driver);
+
+	if (shyway_drv && shyway_drv->remove) {
+		shyway_drv->remove(shyway_dev);
+		return 0;
+	}
+
+	return -ENODEV;
+}
+
+/**
+ * superhyway_register_driver - Register a new SuperHyway driver
+ * @drv: SuperHyway driver to register.
+ *
+ * This registers the passed in @drv. Any devices matching the id table will
+ * automatically be populated and handed off to the driver's specified probe
+ * routine.
+ */
+int superhyway_register_driver(struct superhyway_driver *drv)
+{
+	drv->drv.name	= drv->name;
+	drv->drv.bus	= &superhyway_bus_type;
+	drv->drv.probe	= superhyway_device_probe;
+	drv->drv.remove	= superhyway_device_remove;
+
+	return driver_register(&drv->drv);
+}
+
+/**
+ * superhyway_unregister_driver - Unregister a SuperHyway driver
+ * @drv: SuperHyway driver to unregister.
+ *
+ * This cleans up after superhyway_register_driver(), and should be invoked in
+ * the exit path of any module drivers.
+ */
+void superhyway_unregister_driver(struct superhyway_driver *drv)
+{
+	driver_unregister(&drv->drv);
+}
+
+static int superhyway_bus_match(struct device *dev, struct device_driver *drv)
+{
+	struct superhyway_device *shyway_dev = to_superhyway_device(dev);
+	struct superhyway_driver *shyway_drv = to_superhyway_driver(drv);
+	const struct superhyway_device_id *ids = shyway_drv->id_table;
+
+	if (!ids)
+		return -EINVAL;
+	if (superhyway_match_id(ids, shyway_dev))
+		return 1;
+
+	return -ENODEV;
+}
+
+struct bus_type superhyway_bus_type = {
+	.name	= "superhyway",
+	.match	= superhyway_bus_match,
+};
+
+static int __init superhyway_bus_init(void)
+{
+	return bus_register(&superhyway_bus_type);
+}
+
+static void __exit superhyway_bus_exit(void)
+{
+	struct superhyway_device *dev;
+
+	list_for_each_entry(dev, &superhyway_bus.devices, node) {
+		device_unregister(&dev->dev);
+		kfree(dev);
+	}
+
+	device_unregister(&superhyway_bus.dev);
+	bus_unregister(&superhyway_bus_type);
+}
+
+core_initcall(superhyway_bus_init);
+module_exit(superhyway_bus_exit);
+
+EXPORT_SYMBOL(superhyway_bus_type);
+EXPORT_SYMBOL(superhyway_add_device);
+EXPORT_SYMBOL(superhyway_register_driver);
+EXPORT_SYMBOL(superhyway_unregister_driver);
+
+
diff -puN /dev/null drivers/sh/superhyway/superhyway.ids
--- /dev/null	Thu Apr 11 07:25:15 2002
+++ 25-akpm/drivers/sh/superhyway/superhyway.ids	Mon Nov  8 15:01:00 2004
@@ -0,0 +1,13 @@
+#
+# This list is a compilation of SuperHyway module IDs and
+# their associated "pretty" names.
+#
+
+0x0183	"SH-5 DMA controller"
+0x1823	"SH-5 Debug module"
+0x204f	"SH-5 PCI bridge"
+0x2184	"SH-5 External Memory Interface"
+0x2185	"SH-5 Flash ROM Interface"
+0x448f	"SH-5 Peripheral bridge"
+0x51e2	"SH-5 CPU (SH5-101) core"
+
diff -puN /dev/null drivers/sh/superhyway/superhyway-sysfs.c
--- /dev/null	Thu Apr 11 07:25:15 2002
+++ 25-akpm/drivers/sh/superhyway/superhyway-sysfs.c	Mon Nov  8 15:01:00 2004
@@ -0,0 +1,50 @@
+/*
+ * drivers/sh/superhyway/superhyway-sysfs.c
+ *
+ * SuperHyway Bus sysfs interface
+ *
+ * Copyright (C) 2004  Paul Mundt <lethal@linux-sh.org>
+ *
+ * This file is subject to the terms and conditions of the GNU General Public
+ * License.  See the file "COPYING" in the main directory of this archive
+ * for more details.
+ *
+ */
+
+#include <linux/kernel.h>
+#include <linux/device.h>
+#include <linux/types.h>
+#include <linux/superhyway.h>
+
+#define superhyway_ro_attr(name, fmt, field)				\
+static ssize_t superhyway_show_##name(struct device *dev, char *buf)	\
+{									\
+	struct superhyway_device *s = to_superhyway_device(dev);	\
+	return sprintf(buf, fmt, s->field);				\
+}									\
+static DEVICE_ATTR(name, S_IRUGO, superhyway_show_##name, NULL);
+
+/* VCR flags */
+superhyway_ro_attr(perr_flags, "0x%02x\n", vcr.perr_flags);
+superhyway_ro_attr(merr_flags, "0x%02x\n", vcr.merr_flags);
+superhyway_ro_attr(mod_vers, "0x%04x\n", vcr.mod_vers);
+superhyway_ro_attr(mod_id, "0x%04x\n", vcr.mod_id);
+superhyway_ro_attr(bot_mb, "0x%02x\n", vcr.bot_mb);
+superhyway_ro_attr(top_mb, "0x%02x\n", vcr.top_mb);
+
+/* Misc */
+superhyway_ro_attr(resource, "0x%08lx\n", resource.start);
+
+void superhyway_create_sysfs_files(struct superhyway_device *s)
+{
+	struct device *dev = &s->dev;
+
+	device_create_file(dev, &dev_attr_perr_flags);
+	device_create_file(dev, &dev_attr_merr_flags);
+	device_create_file(dev, &dev_attr_mod_vers);
+	device_create_file(dev, &dev_attr_mod_id);
+	device_create_file(dev, &dev_attr_bot_mb);
+	device_create_file(dev, &dev_attr_top_mb);
+	device_create_file(dev, &dev_attr_resource);
+}
+
diff -puN /dev/null include/linux/superhyway.h
--- /dev/null	Thu Apr 11 07:25:15 2002
+++ 25-akpm/include/linux/superhyway.h	Mon Nov  8 15:01:00 2004
@@ -0,0 +1,96 @@
+/*
+ * include/linux/superhyway.h
+ *
+ * SuperHyway Bus definitions
+ *
+ * Copyright (C) 2004  Paul Mundt <lethal@linux-sh.org>
+ *
+ * This file is subject to the terms and conditions of the GNU General Public
+ * License.  See the file "COPYING" in the main directory of this archive
+ * for more details.
+ *
+ */
+
+#ifndef __LINUX_SUPERHYWAY_H
+#define __LINUX_SUPERHYWAY_H
+
+#include <linux/device.h>
+
+/*
+ * SuperHyway IDs
+ */
+#define SUPERHYWAY_DEVICE_ID_SH5_DMAC	0x0183
+
+
+struct vcr_info {
+	u8	perr_flags;	/* P-port Error flags */
+	u8	merr_flags;	/* Module Error flags */
+	u16	mod_vers;	/* Module Version */
+	u16	mod_id;		/* Module ID */
+	u8	bot_mb;		/* Bottom Memory block */
+	u8	top_mb;		/* Top Memory block */
+};
+
+struct superhyway_device_id {
+	unsigned int id;
+	unsigned long driver_data;
+};
+
+struct superhyway_device;
+extern struct bus_type superhyway_bus_type;
+
+struct superhyway_driver {
+	char *name;
+
+	const struct superhyway_device_id *id_table;
+	struct device_driver drv;
+
+	int (*probe)(struct superhyway_device *dev, const struct superhyway_device_id *id);
+	void (*remove)(struct superhyway_device *dev);
+};
+
+#define to_superhyway_driver(d)	container_of((d), struct superhyway_driver, drv)
+
+struct superhyway_device {
+	struct list_head node;
+
+	char name[32];
+	char pretty_name[64];
+
+	struct device dev;
+
+	struct superhyway_device_id id;
+	struct superhyway_driver *drv;
+
+	struct resource resource;
+	struct vcr_info vcr;
+};
+
+#define to_superhyway_device(d)	container_of((d), struct superhyway_device, dev)
+
+struct superhyway_bus {
+	struct list_head devices;
+	struct device dev;
+	char name[16];
+};
+
+#define superhyway_get_drvdata(d)	dev_get_drvdata(&(d)->dev)
+#define superhyway_set_drvdata(d,p)	dev_set_drvdata(&(d)->dev, (p))
+
+/* drivers/sh/superhyway/superhyway.c */
+int superhyway_register_driver(struct superhyway_driver *);
+void superhyway_unregister_driver(struct superhyway_driver *);
+int superhyway_add_device(unsigned int, unsigned long, unsigned long long);
+
+/* drivers/sh/superhyway/superhyway-sysfs.c */
+#ifdef CONFIG_SYSFS
+void superhyway_create_sysfs_files(struct superhyway_device *);
+#else
+void superhyway_create_sysfs_files(struct superhyway_device *s)
+{
+	/* Nothing to do */
+}
+#endif
+
+#endif /* __LINUX_SUPERHYWAY_H */
+
_
