162306a36Sopenharmony_ci/*
262306a36Sopenharmony_ci * drivers/sh/superhyway/superhyway.c
362306a36Sopenharmony_ci *
462306a36Sopenharmony_ci * SuperHyway Bus Driver
562306a36Sopenharmony_ci *
662306a36Sopenharmony_ci * Copyright (C) 2004, 2005  Paul Mundt <lethal@linux-sh.org>
762306a36Sopenharmony_ci *
862306a36Sopenharmony_ci * This file is subject to the terms and conditions of the GNU General Public
962306a36Sopenharmony_ci * License.  See the file "COPYING" in the main directory of this archive
1062306a36Sopenharmony_ci * for more details.
1162306a36Sopenharmony_ci */
1262306a36Sopenharmony_ci#include <linux/kernel.h>
1362306a36Sopenharmony_ci#include <linux/device.h>
1462306a36Sopenharmony_ci#include <linux/init.h>
1562306a36Sopenharmony_ci#include <linux/module.h>
1662306a36Sopenharmony_ci#include <linux/types.h>
1762306a36Sopenharmony_ci#include <linux/list.h>
1862306a36Sopenharmony_ci#include <linux/superhyway.h>
1962306a36Sopenharmony_ci#include <linux/string.h>
2062306a36Sopenharmony_ci#include <linux/slab.h>
2162306a36Sopenharmony_ci
2262306a36Sopenharmony_cistatic int superhyway_devices;
2362306a36Sopenharmony_ci
2462306a36Sopenharmony_cistatic struct device superhyway_bus_device = {
2562306a36Sopenharmony_ci	.init_name = "superhyway",
2662306a36Sopenharmony_ci};
2762306a36Sopenharmony_ci
2862306a36Sopenharmony_cistatic void superhyway_device_release(struct device *dev)
2962306a36Sopenharmony_ci{
3062306a36Sopenharmony_ci	struct superhyway_device *sdev = to_superhyway_device(dev);
3162306a36Sopenharmony_ci
3262306a36Sopenharmony_ci	kfree(sdev->resource);
3362306a36Sopenharmony_ci	kfree(sdev);
3462306a36Sopenharmony_ci}
3562306a36Sopenharmony_ci
3662306a36Sopenharmony_ci/**
3762306a36Sopenharmony_ci * superhyway_add_device - Add a SuperHyway module
3862306a36Sopenharmony_ci * @base: Physical address where module is mapped.
3962306a36Sopenharmony_ci * @sdev: SuperHyway device to add, or NULL to allocate a new one.
4062306a36Sopenharmony_ci * @bus: Bus where SuperHyway module resides.
4162306a36Sopenharmony_ci *
4262306a36Sopenharmony_ci * This is responsible for adding a new SuperHyway module. This sets up a new
4362306a36Sopenharmony_ci * struct superhyway_device for the module being added if @sdev == NULL.
4462306a36Sopenharmony_ci *
4562306a36Sopenharmony_ci * Devices are initially added in the order that they are scanned (from the
4662306a36Sopenharmony_ci * top-down of the memory map), and are assigned an ID based on the order that
4762306a36Sopenharmony_ci * they are added. Any manual addition of a module will thus get the ID after
4862306a36Sopenharmony_ci * the devices already discovered regardless of where it resides in memory.
4962306a36Sopenharmony_ci *
5062306a36Sopenharmony_ci * Further work can and should be done in superhyway_scan_bus(), to be sure
5162306a36Sopenharmony_ci * that any new modules are properly discovered and subsequently registered.
5262306a36Sopenharmony_ci */
5362306a36Sopenharmony_ciint superhyway_add_device(unsigned long base, struct superhyway_device *sdev,
5462306a36Sopenharmony_ci			  struct superhyway_bus *bus)
5562306a36Sopenharmony_ci{
5662306a36Sopenharmony_ci	struct superhyway_device *dev = sdev;
5762306a36Sopenharmony_ci
5862306a36Sopenharmony_ci	if (!dev) {
5962306a36Sopenharmony_ci		dev = kzalloc(sizeof(struct superhyway_device), GFP_KERNEL);
6062306a36Sopenharmony_ci		if (!dev)
6162306a36Sopenharmony_ci			return -ENOMEM;
6262306a36Sopenharmony_ci
6362306a36Sopenharmony_ci	}
6462306a36Sopenharmony_ci
6562306a36Sopenharmony_ci	dev->bus = bus;
6662306a36Sopenharmony_ci	superhyway_read_vcr(dev, base, &dev->vcr);
6762306a36Sopenharmony_ci
6862306a36Sopenharmony_ci	if (!dev->resource) {
6962306a36Sopenharmony_ci		dev->resource = kzalloc(sizeof(struct resource), GFP_KERNEL);
7062306a36Sopenharmony_ci		if (!dev->resource) {
7162306a36Sopenharmony_ci			kfree(dev);
7262306a36Sopenharmony_ci			return -ENOMEM;
7362306a36Sopenharmony_ci		}
7462306a36Sopenharmony_ci
7562306a36Sopenharmony_ci		dev->resource->name	= dev->name;
7662306a36Sopenharmony_ci		dev->resource->start	= base;
7762306a36Sopenharmony_ci		dev->resource->end	= dev->resource->start + 0x01000000;
7862306a36Sopenharmony_ci	}
7962306a36Sopenharmony_ci
8062306a36Sopenharmony_ci	dev->dev.parent		= &superhyway_bus_device;
8162306a36Sopenharmony_ci	dev->dev.bus		= &superhyway_bus_type;
8262306a36Sopenharmony_ci	dev->dev.release	= superhyway_device_release;
8362306a36Sopenharmony_ci	dev->id.id		= dev->vcr.mod_id;
8462306a36Sopenharmony_ci
8562306a36Sopenharmony_ci	sprintf(dev->name, "SuperHyway device %04x", dev->id.id);
8662306a36Sopenharmony_ci	dev_set_name(&dev->dev, "%02x", superhyway_devices);
8762306a36Sopenharmony_ci
8862306a36Sopenharmony_ci	superhyway_devices++;
8962306a36Sopenharmony_ci
9062306a36Sopenharmony_ci	return device_register(&dev->dev);
9162306a36Sopenharmony_ci}
9262306a36Sopenharmony_ci
9362306a36Sopenharmony_ciint superhyway_add_devices(struct superhyway_bus *bus,
9462306a36Sopenharmony_ci			   struct superhyway_device **devices,
9562306a36Sopenharmony_ci			   int nr_devices)
9662306a36Sopenharmony_ci{
9762306a36Sopenharmony_ci	int i, ret = 0;
9862306a36Sopenharmony_ci
9962306a36Sopenharmony_ci	for (i = 0; i < nr_devices; i++) {
10062306a36Sopenharmony_ci		struct superhyway_device *dev = devices[i];
10162306a36Sopenharmony_ci		ret |= superhyway_add_device(dev->resource[0].start, dev, bus);
10262306a36Sopenharmony_ci	}
10362306a36Sopenharmony_ci
10462306a36Sopenharmony_ci	return ret;
10562306a36Sopenharmony_ci}
10662306a36Sopenharmony_ci
10762306a36Sopenharmony_cistatic int __init superhyway_init(void)
10862306a36Sopenharmony_ci{
10962306a36Sopenharmony_ci	struct superhyway_bus *bus;
11062306a36Sopenharmony_ci	int ret;
11162306a36Sopenharmony_ci
11262306a36Sopenharmony_ci	ret = device_register(&superhyway_bus_device);
11362306a36Sopenharmony_ci	if (unlikely(ret))
11462306a36Sopenharmony_ci		return ret;
11562306a36Sopenharmony_ci
11662306a36Sopenharmony_ci	for (bus = superhyway_channels; bus->ops; bus++)
11762306a36Sopenharmony_ci		ret |= superhyway_scan_bus(bus);
11862306a36Sopenharmony_ci
11962306a36Sopenharmony_ci	return ret;
12062306a36Sopenharmony_ci}
12162306a36Sopenharmony_cipostcore_initcall(superhyway_init);
12262306a36Sopenharmony_ci
12362306a36Sopenharmony_cistatic const struct superhyway_device_id *
12462306a36Sopenharmony_cisuperhyway_match_id(const struct superhyway_device_id *ids,
12562306a36Sopenharmony_ci		    struct superhyway_device *dev)
12662306a36Sopenharmony_ci{
12762306a36Sopenharmony_ci	while (ids->id) {
12862306a36Sopenharmony_ci		if (ids->id == dev->id.id)
12962306a36Sopenharmony_ci			return ids;
13062306a36Sopenharmony_ci
13162306a36Sopenharmony_ci		ids++;
13262306a36Sopenharmony_ci	}
13362306a36Sopenharmony_ci
13462306a36Sopenharmony_ci	return NULL;
13562306a36Sopenharmony_ci}
13662306a36Sopenharmony_ci
13762306a36Sopenharmony_cistatic int superhyway_device_probe(struct device *dev)
13862306a36Sopenharmony_ci{
13962306a36Sopenharmony_ci	struct superhyway_device *shyway_dev = to_superhyway_device(dev);
14062306a36Sopenharmony_ci	struct superhyway_driver *shyway_drv = to_superhyway_driver(dev->driver);
14162306a36Sopenharmony_ci
14262306a36Sopenharmony_ci	if (shyway_drv && shyway_drv->probe) {
14362306a36Sopenharmony_ci		const struct superhyway_device_id *id;
14462306a36Sopenharmony_ci
14562306a36Sopenharmony_ci		id = superhyway_match_id(shyway_drv->id_table, shyway_dev);
14662306a36Sopenharmony_ci		if (id)
14762306a36Sopenharmony_ci			return shyway_drv->probe(shyway_dev, id);
14862306a36Sopenharmony_ci	}
14962306a36Sopenharmony_ci
15062306a36Sopenharmony_ci	return -ENODEV;
15162306a36Sopenharmony_ci}
15262306a36Sopenharmony_ci
15362306a36Sopenharmony_cistatic void superhyway_device_remove(struct device *dev)
15462306a36Sopenharmony_ci{
15562306a36Sopenharmony_ci	struct superhyway_device *shyway_dev = to_superhyway_device(dev);
15662306a36Sopenharmony_ci	struct superhyway_driver *shyway_drv = to_superhyway_driver(dev->driver);
15762306a36Sopenharmony_ci
15862306a36Sopenharmony_ci	if (shyway_drv->remove)
15962306a36Sopenharmony_ci		shyway_drv->remove(shyway_dev);
16062306a36Sopenharmony_ci}
16162306a36Sopenharmony_ci
16262306a36Sopenharmony_ci/**
16362306a36Sopenharmony_ci * superhyway_register_driver - Register a new SuperHyway driver
16462306a36Sopenharmony_ci * @drv: SuperHyway driver to register.
16562306a36Sopenharmony_ci *
16662306a36Sopenharmony_ci * This registers the passed in @drv. Any devices matching the id table will
16762306a36Sopenharmony_ci * automatically be populated and handed off to the driver's specified probe
16862306a36Sopenharmony_ci * routine.
16962306a36Sopenharmony_ci */
17062306a36Sopenharmony_ciint superhyway_register_driver(struct superhyway_driver *drv)
17162306a36Sopenharmony_ci{
17262306a36Sopenharmony_ci	drv->drv.name	= drv->name;
17362306a36Sopenharmony_ci	drv->drv.bus	= &superhyway_bus_type;
17462306a36Sopenharmony_ci
17562306a36Sopenharmony_ci	return driver_register(&drv->drv);
17662306a36Sopenharmony_ci}
17762306a36Sopenharmony_ci
17862306a36Sopenharmony_ci/**
17962306a36Sopenharmony_ci * superhyway_unregister_driver - Unregister a SuperHyway driver
18062306a36Sopenharmony_ci * @drv: SuperHyway driver to unregister.
18162306a36Sopenharmony_ci *
18262306a36Sopenharmony_ci * This cleans up after superhyway_register_driver(), and should be invoked in
18362306a36Sopenharmony_ci * the exit path of any module drivers.
18462306a36Sopenharmony_ci */
18562306a36Sopenharmony_civoid superhyway_unregister_driver(struct superhyway_driver *drv)
18662306a36Sopenharmony_ci{
18762306a36Sopenharmony_ci	driver_unregister(&drv->drv);
18862306a36Sopenharmony_ci}
18962306a36Sopenharmony_ci
19062306a36Sopenharmony_cistatic int superhyway_bus_match(struct device *dev, struct device_driver *drv)
19162306a36Sopenharmony_ci{
19262306a36Sopenharmony_ci	struct superhyway_device *shyway_dev = to_superhyway_device(dev);
19362306a36Sopenharmony_ci	struct superhyway_driver *shyway_drv = to_superhyway_driver(drv);
19462306a36Sopenharmony_ci	const struct superhyway_device_id *ids = shyway_drv->id_table;
19562306a36Sopenharmony_ci
19662306a36Sopenharmony_ci	if (!ids)
19762306a36Sopenharmony_ci		return -EINVAL;
19862306a36Sopenharmony_ci	if (superhyway_match_id(ids, shyway_dev))
19962306a36Sopenharmony_ci		return 1;
20062306a36Sopenharmony_ci
20162306a36Sopenharmony_ci	return -ENODEV;
20262306a36Sopenharmony_ci}
20362306a36Sopenharmony_ci
20462306a36Sopenharmony_cistruct bus_type superhyway_bus_type = {
20562306a36Sopenharmony_ci	.name		= "superhyway",
20662306a36Sopenharmony_ci	.match		= superhyway_bus_match,
20762306a36Sopenharmony_ci#ifdef CONFIG_SYSFS
20862306a36Sopenharmony_ci	.dev_groups	= superhyway_dev_groups,
20962306a36Sopenharmony_ci#endif
21062306a36Sopenharmony_ci	.probe		= superhyway_device_probe,
21162306a36Sopenharmony_ci	.remove		= superhyway_device_remove,
21262306a36Sopenharmony_ci};
21362306a36Sopenharmony_ci
21462306a36Sopenharmony_cistatic int __init superhyway_bus_init(void)
21562306a36Sopenharmony_ci{
21662306a36Sopenharmony_ci	return bus_register(&superhyway_bus_type);
21762306a36Sopenharmony_ci}
21862306a36Sopenharmony_ci
21962306a36Sopenharmony_cistatic void __exit superhyway_bus_exit(void)
22062306a36Sopenharmony_ci{
22162306a36Sopenharmony_ci	device_unregister(&superhyway_bus_device);
22262306a36Sopenharmony_ci	bus_unregister(&superhyway_bus_type);
22362306a36Sopenharmony_ci}
22462306a36Sopenharmony_ci
22562306a36Sopenharmony_cicore_initcall(superhyway_bus_init);
22662306a36Sopenharmony_cimodule_exit(superhyway_bus_exit);
22762306a36Sopenharmony_ci
22862306a36Sopenharmony_ciEXPORT_SYMBOL(superhyway_bus_type);
22962306a36Sopenharmony_ciEXPORT_SYMBOL(superhyway_add_device);
23062306a36Sopenharmony_ciEXPORT_SYMBOL(superhyway_add_devices);
23162306a36Sopenharmony_ciEXPORT_SYMBOL(superhyway_register_driver);
23262306a36Sopenharmony_ciEXPORT_SYMBOL(superhyway_unregister_driver);
23362306a36Sopenharmony_ci
23462306a36Sopenharmony_ciMODULE_LICENSE("GPL");
235