18c2ecf20Sopenharmony_ci/*
28c2ecf20Sopenharmony_ci * drivers/sh/superhyway/superhyway.c
38c2ecf20Sopenharmony_ci *
48c2ecf20Sopenharmony_ci * SuperHyway Bus Driver
58c2ecf20Sopenharmony_ci *
68c2ecf20Sopenharmony_ci * Copyright (C) 2004, 2005  Paul Mundt <lethal@linux-sh.org>
78c2ecf20Sopenharmony_ci *
88c2ecf20Sopenharmony_ci * This file is subject to the terms and conditions of the GNU General Public
98c2ecf20Sopenharmony_ci * License.  See the file "COPYING" in the main directory of this archive
108c2ecf20Sopenharmony_ci * for more details.
118c2ecf20Sopenharmony_ci */
128c2ecf20Sopenharmony_ci#include <linux/kernel.h>
138c2ecf20Sopenharmony_ci#include <linux/device.h>
148c2ecf20Sopenharmony_ci#include <linux/init.h>
158c2ecf20Sopenharmony_ci#include <linux/module.h>
168c2ecf20Sopenharmony_ci#include <linux/types.h>
178c2ecf20Sopenharmony_ci#include <linux/list.h>
188c2ecf20Sopenharmony_ci#include <linux/superhyway.h>
198c2ecf20Sopenharmony_ci#include <linux/string.h>
208c2ecf20Sopenharmony_ci#include <linux/slab.h>
218c2ecf20Sopenharmony_ci
228c2ecf20Sopenharmony_cistatic int superhyway_devices;
238c2ecf20Sopenharmony_ci
248c2ecf20Sopenharmony_cistatic struct device superhyway_bus_device = {
258c2ecf20Sopenharmony_ci	.init_name = "superhyway",
268c2ecf20Sopenharmony_ci};
278c2ecf20Sopenharmony_ci
288c2ecf20Sopenharmony_cistatic void superhyway_device_release(struct device *dev)
298c2ecf20Sopenharmony_ci{
308c2ecf20Sopenharmony_ci	struct superhyway_device *sdev = to_superhyway_device(dev);
318c2ecf20Sopenharmony_ci
328c2ecf20Sopenharmony_ci	kfree(sdev->resource);
338c2ecf20Sopenharmony_ci	kfree(sdev);
348c2ecf20Sopenharmony_ci}
358c2ecf20Sopenharmony_ci
368c2ecf20Sopenharmony_ci/**
378c2ecf20Sopenharmony_ci * superhyway_add_device - Add a SuperHyway module
388c2ecf20Sopenharmony_ci * @base: Physical address where module is mapped.
398c2ecf20Sopenharmony_ci * @sdev: SuperHyway device to add, or NULL to allocate a new one.
408c2ecf20Sopenharmony_ci * @bus: Bus where SuperHyway module resides.
418c2ecf20Sopenharmony_ci *
428c2ecf20Sopenharmony_ci * This is responsible for adding a new SuperHyway module. This sets up a new
438c2ecf20Sopenharmony_ci * struct superhyway_device for the module being added if @sdev == NULL.
448c2ecf20Sopenharmony_ci *
458c2ecf20Sopenharmony_ci * Devices are initially added in the order that they are scanned (from the
468c2ecf20Sopenharmony_ci * top-down of the memory map), and are assigned an ID based on the order that
478c2ecf20Sopenharmony_ci * they are added. Any manual addition of a module will thus get the ID after
488c2ecf20Sopenharmony_ci * the devices already discovered regardless of where it resides in memory.
498c2ecf20Sopenharmony_ci *
508c2ecf20Sopenharmony_ci * Further work can and should be done in superhyway_scan_bus(), to be sure
518c2ecf20Sopenharmony_ci * that any new modules are properly discovered and subsequently registered.
528c2ecf20Sopenharmony_ci */
538c2ecf20Sopenharmony_ciint superhyway_add_device(unsigned long base, struct superhyway_device *sdev,
548c2ecf20Sopenharmony_ci			  struct superhyway_bus *bus)
558c2ecf20Sopenharmony_ci{
568c2ecf20Sopenharmony_ci	struct superhyway_device *dev = sdev;
578c2ecf20Sopenharmony_ci
588c2ecf20Sopenharmony_ci	if (!dev) {
598c2ecf20Sopenharmony_ci		dev = kzalloc(sizeof(struct superhyway_device), GFP_KERNEL);
608c2ecf20Sopenharmony_ci		if (!dev)
618c2ecf20Sopenharmony_ci			return -ENOMEM;
628c2ecf20Sopenharmony_ci
638c2ecf20Sopenharmony_ci	}
648c2ecf20Sopenharmony_ci
658c2ecf20Sopenharmony_ci	dev->bus = bus;
668c2ecf20Sopenharmony_ci	superhyway_read_vcr(dev, base, &dev->vcr);
678c2ecf20Sopenharmony_ci
688c2ecf20Sopenharmony_ci	if (!dev->resource) {
698c2ecf20Sopenharmony_ci		dev->resource = kzalloc(sizeof(struct resource), GFP_KERNEL);
708c2ecf20Sopenharmony_ci		if (!dev->resource) {
718c2ecf20Sopenharmony_ci			kfree(dev);
728c2ecf20Sopenharmony_ci			return -ENOMEM;
738c2ecf20Sopenharmony_ci		}
748c2ecf20Sopenharmony_ci
758c2ecf20Sopenharmony_ci		dev->resource->name	= dev->name;
768c2ecf20Sopenharmony_ci		dev->resource->start	= base;
778c2ecf20Sopenharmony_ci		dev->resource->end	= dev->resource->start + 0x01000000;
788c2ecf20Sopenharmony_ci	}
798c2ecf20Sopenharmony_ci
808c2ecf20Sopenharmony_ci	dev->dev.parent		= &superhyway_bus_device;
818c2ecf20Sopenharmony_ci	dev->dev.bus		= &superhyway_bus_type;
828c2ecf20Sopenharmony_ci	dev->dev.release	= superhyway_device_release;
838c2ecf20Sopenharmony_ci	dev->id.id		= dev->vcr.mod_id;
848c2ecf20Sopenharmony_ci
858c2ecf20Sopenharmony_ci	sprintf(dev->name, "SuperHyway device %04x", dev->id.id);
868c2ecf20Sopenharmony_ci	dev_set_name(&dev->dev, "%02x", superhyway_devices);
878c2ecf20Sopenharmony_ci
888c2ecf20Sopenharmony_ci	superhyway_devices++;
898c2ecf20Sopenharmony_ci
908c2ecf20Sopenharmony_ci	return device_register(&dev->dev);
918c2ecf20Sopenharmony_ci}
928c2ecf20Sopenharmony_ci
938c2ecf20Sopenharmony_ciint superhyway_add_devices(struct superhyway_bus *bus,
948c2ecf20Sopenharmony_ci			   struct superhyway_device **devices,
958c2ecf20Sopenharmony_ci			   int nr_devices)
968c2ecf20Sopenharmony_ci{
978c2ecf20Sopenharmony_ci	int i, ret = 0;
988c2ecf20Sopenharmony_ci
998c2ecf20Sopenharmony_ci	for (i = 0; i < nr_devices; i++) {
1008c2ecf20Sopenharmony_ci		struct superhyway_device *dev = devices[i];
1018c2ecf20Sopenharmony_ci		ret |= superhyway_add_device(dev->resource[0].start, dev, bus);
1028c2ecf20Sopenharmony_ci	}
1038c2ecf20Sopenharmony_ci
1048c2ecf20Sopenharmony_ci	return ret;
1058c2ecf20Sopenharmony_ci}
1068c2ecf20Sopenharmony_ci
1078c2ecf20Sopenharmony_cistatic int __init superhyway_init(void)
1088c2ecf20Sopenharmony_ci{
1098c2ecf20Sopenharmony_ci	struct superhyway_bus *bus;
1108c2ecf20Sopenharmony_ci	int ret;
1118c2ecf20Sopenharmony_ci
1128c2ecf20Sopenharmony_ci	ret = device_register(&superhyway_bus_device);
1138c2ecf20Sopenharmony_ci	if (unlikely(ret))
1148c2ecf20Sopenharmony_ci		return ret;
1158c2ecf20Sopenharmony_ci
1168c2ecf20Sopenharmony_ci	for (bus = superhyway_channels; bus->ops; bus++)
1178c2ecf20Sopenharmony_ci		ret |= superhyway_scan_bus(bus);
1188c2ecf20Sopenharmony_ci
1198c2ecf20Sopenharmony_ci	return ret;
1208c2ecf20Sopenharmony_ci}
1218c2ecf20Sopenharmony_cipostcore_initcall(superhyway_init);
1228c2ecf20Sopenharmony_ci
1238c2ecf20Sopenharmony_cistatic const struct superhyway_device_id *
1248c2ecf20Sopenharmony_cisuperhyway_match_id(const struct superhyway_device_id *ids,
1258c2ecf20Sopenharmony_ci		    struct superhyway_device *dev)
1268c2ecf20Sopenharmony_ci{
1278c2ecf20Sopenharmony_ci	while (ids->id) {
1288c2ecf20Sopenharmony_ci		if (ids->id == dev->id.id)
1298c2ecf20Sopenharmony_ci			return ids;
1308c2ecf20Sopenharmony_ci
1318c2ecf20Sopenharmony_ci		ids++;
1328c2ecf20Sopenharmony_ci	}
1338c2ecf20Sopenharmony_ci
1348c2ecf20Sopenharmony_ci	return NULL;
1358c2ecf20Sopenharmony_ci}
1368c2ecf20Sopenharmony_ci
1378c2ecf20Sopenharmony_cistatic int superhyway_device_probe(struct device *dev)
1388c2ecf20Sopenharmony_ci{
1398c2ecf20Sopenharmony_ci	struct superhyway_device *shyway_dev = to_superhyway_device(dev);
1408c2ecf20Sopenharmony_ci	struct superhyway_driver *shyway_drv = to_superhyway_driver(dev->driver);
1418c2ecf20Sopenharmony_ci
1428c2ecf20Sopenharmony_ci	if (shyway_drv && shyway_drv->probe) {
1438c2ecf20Sopenharmony_ci		const struct superhyway_device_id *id;
1448c2ecf20Sopenharmony_ci
1458c2ecf20Sopenharmony_ci		id = superhyway_match_id(shyway_drv->id_table, shyway_dev);
1468c2ecf20Sopenharmony_ci		if (id)
1478c2ecf20Sopenharmony_ci			return shyway_drv->probe(shyway_dev, id);
1488c2ecf20Sopenharmony_ci	}
1498c2ecf20Sopenharmony_ci
1508c2ecf20Sopenharmony_ci	return -ENODEV;
1518c2ecf20Sopenharmony_ci}
1528c2ecf20Sopenharmony_ci
1538c2ecf20Sopenharmony_cistatic int superhyway_device_remove(struct device *dev)
1548c2ecf20Sopenharmony_ci{
1558c2ecf20Sopenharmony_ci	struct superhyway_device *shyway_dev = to_superhyway_device(dev);
1568c2ecf20Sopenharmony_ci	struct superhyway_driver *shyway_drv = to_superhyway_driver(dev->driver);
1578c2ecf20Sopenharmony_ci
1588c2ecf20Sopenharmony_ci	if (shyway_drv && shyway_drv->remove) {
1598c2ecf20Sopenharmony_ci		shyway_drv->remove(shyway_dev);
1608c2ecf20Sopenharmony_ci		return 0;
1618c2ecf20Sopenharmony_ci	}
1628c2ecf20Sopenharmony_ci
1638c2ecf20Sopenharmony_ci	return -ENODEV;
1648c2ecf20Sopenharmony_ci}
1658c2ecf20Sopenharmony_ci
1668c2ecf20Sopenharmony_ci/**
1678c2ecf20Sopenharmony_ci * superhyway_register_driver - Register a new SuperHyway driver
1688c2ecf20Sopenharmony_ci * @drv: SuperHyway driver to register.
1698c2ecf20Sopenharmony_ci *
1708c2ecf20Sopenharmony_ci * This registers the passed in @drv. Any devices matching the id table will
1718c2ecf20Sopenharmony_ci * automatically be populated and handed off to the driver's specified probe
1728c2ecf20Sopenharmony_ci * routine.
1738c2ecf20Sopenharmony_ci */
1748c2ecf20Sopenharmony_ciint superhyway_register_driver(struct superhyway_driver *drv)
1758c2ecf20Sopenharmony_ci{
1768c2ecf20Sopenharmony_ci	drv->drv.name	= drv->name;
1778c2ecf20Sopenharmony_ci	drv->drv.bus	= &superhyway_bus_type;
1788c2ecf20Sopenharmony_ci
1798c2ecf20Sopenharmony_ci	return driver_register(&drv->drv);
1808c2ecf20Sopenharmony_ci}
1818c2ecf20Sopenharmony_ci
1828c2ecf20Sopenharmony_ci/**
1838c2ecf20Sopenharmony_ci * superhyway_unregister_driver - Unregister a SuperHyway driver
1848c2ecf20Sopenharmony_ci * @drv: SuperHyway driver to unregister.
1858c2ecf20Sopenharmony_ci *
1868c2ecf20Sopenharmony_ci * This cleans up after superhyway_register_driver(), and should be invoked in
1878c2ecf20Sopenharmony_ci * the exit path of any module drivers.
1888c2ecf20Sopenharmony_ci */
1898c2ecf20Sopenharmony_civoid superhyway_unregister_driver(struct superhyway_driver *drv)
1908c2ecf20Sopenharmony_ci{
1918c2ecf20Sopenharmony_ci	driver_unregister(&drv->drv);
1928c2ecf20Sopenharmony_ci}
1938c2ecf20Sopenharmony_ci
1948c2ecf20Sopenharmony_cistatic int superhyway_bus_match(struct device *dev, struct device_driver *drv)
1958c2ecf20Sopenharmony_ci{
1968c2ecf20Sopenharmony_ci	struct superhyway_device *shyway_dev = to_superhyway_device(dev);
1978c2ecf20Sopenharmony_ci	struct superhyway_driver *shyway_drv = to_superhyway_driver(drv);
1988c2ecf20Sopenharmony_ci	const struct superhyway_device_id *ids = shyway_drv->id_table;
1998c2ecf20Sopenharmony_ci
2008c2ecf20Sopenharmony_ci	if (!ids)
2018c2ecf20Sopenharmony_ci		return -EINVAL;
2028c2ecf20Sopenharmony_ci	if (superhyway_match_id(ids, shyway_dev))
2038c2ecf20Sopenharmony_ci		return 1;
2048c2ecf20Sopenharmony_ci
2058c2ecf20Sopenharmony_ci	return -ENODEV;
2068c2ecf20Sopenharmony_ci}
2078c2ecf20Sopenharmony_ci
2088c2ecf20Sopenharmony_cistruct bus_type superhyway_bus_type = {
2098c2ecf20Sopenharmony_ci	.name		= "superhyway",
2108c2ecf20Sopenharmony_ci	.match		= superhyway_bus_match,
2118c2ecf20Sopenharmony_ci#ifdef CONFIG_SYSFS
2128c2ecf20Sopenharmony_ci	.dev_groups	= superhyway_dev_groups,
2138c2ecf20Sopenharmony_ci#endif
2148c2ecf20Sopenharmony_ci	.probe		= superhyway_device_probe,
2158c2ecf20Sopenharmony_ci	.remove		= superhyway_device_remove,
2168c2ecf20Sopenharmony_ci};
2178c2ecf20Sopenharmony_ci
2188c2ecf20Sopenharmony_cistatic int __init superhyway_bus_init(void)
2198c2ecf20Sopenharmony_ci{
2208c2ecf20Sopenharmony_ci	return bus_register(&superhyway_bus_type);
2218c2ecf20Sopenharmony_ci}
2228c2ecf20Sopenharmony_ci
2238c2ecf20Sopenharmony_cistatic void __exit superhyway_bus_exit(void)
2248c2ecf20Sopenharmony_ci{
2258c2ecf20Sopenharmony_ci	device_unregister(&superhyway_bus_device);
2268c2ecf20Sopenharmony_ci	bus_unregister(&superhyway_bus_type);
2278c2ecf20Sopenharmony_ci}
2288c2ecf20Sopenharmony_ci
2298c2ecf20Sopenharmony_cicore_initcall(superhyway_bus_init);
2308c2ecf20Sopenharmony_cimodule_exit(superhyway_bus_exit);
2318c2ecf20Sopenharmony_ci
2328c2ecf20Sopenharmony_ciEXPORT_SYMBOL(superhyway_bus_type);
2338c2ecf20Sopenharmony_ciEXPORT_SYMBOL(superhyway_add_device);
2348c2ecf20Sopenharmony_ciEXPORT_SYMBOL(superhyway_add_devices);
2358c2ecf20Sopenharmony_ciEXPORT_SYMBOL(superhyway_register_driver);
2368c2ecf20Sopenharmony_ciEXPORT_SYMBOL(superhyway_unregister_driver);
2378c2ecf20Sopenharmony_ci
2388c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL");
239