2019-05-27 14:55:05 +08:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
2005-01-24 08:37:04 +08:00
|
|
|
/* drivers/mtd/maps/plat-ram.c
|
|
|
|
*
|
|
|
|
* (c) 2004-2005 Simtec Electronics
|
|
|
|
* http://www.simtec.co.uk/products/SWLINUX/
|
|
|
|
* Ben Dooks <ben@simtec.co.uk>
|
|
|
|
*
|
2010-01-07 07:03:52 +08:00
|
|
|
* Generic platform device based RAM map
|
2005-01-24 08:37:04 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <linux/module.h>
|
|
|
|
#include <linux/types.h>
|
|
|
|
#include <linux/kernel.h>
|
|
|
|
#include <linux/string.h>
|
|
|
|
#include <linux/ioport.h>
|
|
|
|
#include <linux/device.h>
|
2005-10-31 07:03:48 +08:00
|
|
|
#include <linux/slab.h>
|
2005-10-30 02:07:23 +08:00
|
|
|
#include <linux/platform_device.h>
|
2005-01-24 08:37:04 +08:00
|
|
|
|
|
|
|
#include <linux/mtd/mtd.h>
|
|
|
|
#include <linux/mtd/map.h>
|
|
|
|
#include <linux/mtd/partitions.h>
|
|
|
|
#include <linux/mtd/plat-ram.h>
|
|
|
|
|
|
|
|
#include <asm/io.h>
|
|
|
|
|
|
|
|
/* private structure for each mtd platform ram device created */
|
|
|
|
|
|
|
|
struct platram_info {
|
|
|
|
struct device *dev;
|
|
|
|
struct mtd_info *mtd;
|
|
|
|
struct map_info map;
|
|
|
|
struct platdata_mtd_ram *pdata;
|
|
|
|
};
|
|
|
|
|
|
|
|
/* to_platram_info()
|
|
|
|
*
|
|
|
|
* device private data to struct platram_info conversion
|
|
|
|
*/
|
|
|
|
|
2005-11-10 06:32:44 +08:00
|
|
|
static inline struct platram_info *to_platram_info(struct platform_device *dev)
|
2005-01-24 08:37:04 +08:00
|
|
|
{
|
2013-09-09 13:42:52 +08:00
|
|
|
return platform_get_drvdata(dev);
|
2005-01-24 08:37:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* platram_setrw
|
|
|
|
*
|
|
|
|
* call the platform device's set rw/ro control
|
|
|
|
*
|
|
|
|
* to = 0 => read-only
|
|
|
|
* = 1 => read-write
|
|
|
|
*/
|
|
|
|
|
|
|
|
static inline void platram_setrw(struct platram_info *info, int to)
|
|
|
|
{
|
|
|
|
if (info->pdata == NULL)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (info->pdata->set_rw != NULL)
|
|
|
|
(info->pdata->set_rw)(info->dev, to);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* platram_remove
|
|
|
|
*
|
|
|
|
* called to remove the device from the driver's control
|
|
|
|
*/
|
|
|
|
|
2005-11-10 06:32:44 +08:00
|
|
|
static int platram_remove(struct platform_device *pdev)
|
2005-01-24 08:37:04 +08:00
|
|
|
{
|
2005-11-10 06:32:44 +08:00
|
|
|
struct platram_info *info = to_platram_info(pdev);
|
2005-01-24 08:37:04 +08:00
|
|
|
|
2005-11-10 06:32:44 +08:00
|
|
|
dev_dbg(&pdev->dev, "removing device\n");
|
2005-01-24 08:37:04 +08:00
|
|
|
|
2005-11-07 19:15:40 +08:00
|
|
|
if (info == NULL)
|
2005-01-24 08:37:04 +08:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (info->mtd) {
|
2011-05-23 17:23:07 +08:00
|
|
|
mtd_device_unregister(info->mtd);
|
2005-01-24 08:37:04 +08:00
|
|
|
map_destroy(info->mtd);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ensure ram is left read-only */
|
|
|
|
|
|
|
|
platram_setrw(info, PLATRAM_RO);
|
|
|
|
|
|
|
|
kfree(info);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* platram_probe
|
|
|
|
*
|
|
|
|
* called from device drive system when a device matching our
|
|
|
|
* driver is found.
|
|
|
|
*/
|
|
|
|
|
2005-11-10 06:32:44 +08:00
|
|
|
static int platram_probe(struct platform_device *pdev)
|
2005-01-24 08:37:04 +08:00
|
|
|
{
|
|
|
|
struct platdata_mtd_ram *pdata;
|
|
|
|
struct platram_info *info;
|
|
|
|
struct resource *res;
|
|
|
|
int err = 0;
|
|
|
|
|
2005-11-10 06:32:44 +08:00
|
|
|
dev_dbg(&pdev->dev, "probe entered\n");
|
2005-11-07 19:15:40 +08:00
|
|
|
|
2013-07-30 16:18:06 +08:00
|
|
|
if (dev_get_platdata(&pdev->dev) == NULL) {
|
2005-11-10 06:32:44 +08:00
|
|
|
dev_err(&pdev->dev, "no platform data supplied\n");
|
2005-01-24 08:37:04 +08:00
|
|
|
err = -ENOENT;
|
|
|
|
goto exit_error;
|
|
|
|
}
|
|
|
|
|
2013-07-30 16:18:06 +08:00
|
|
|
pdata = dev_get_platdata(&pdev->dev);
|
2005-01-24 08:37:04 +08:00
|
|
|
|
2006-11-16 03:10:29 +08:00
|
|
|
info = kzalloc(sizeof(*info), GFP_KERNEL);
|
2005-01-24 08:37:04 +08:00
|
|
|
if (info == NULL) {
|
|
|
|
err = -ENOMEM;
|
|
|
|
goto exit_error;
|
|
|
|
}
|
|
|
|
|
2005-11-10 06:32:44 +08:00
|
|
|
platform_set_drvdata(pdev, info);
|
2005-01-24 08:37:04 +08:00
|
|
|
|
2005-11-10 06:32:44 +08:00
|
|
|
info->dev = &pdev->dev;
|
2005-01-24 08:37:04 +08:00
|
|
|
info->pdata = pdata;
|
|
|
|
|
|
|
|
/* get the resource for the memory mapping */
|
2005-11-10 06:32:44 +08:00
|
|
|
res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
|
2017-08-22 21:24:11 +08:00
|
|
|
info->map.virt = devm_ioremap_resource(&pdev->dev, res);
|
|
|
|
if (IS_ERR(info->map.virt)) {
|
|
|
|
err = PTR_ERR(info->map.virt);
|
|
|
|
dev_err(&pdev->dev, "failed to ioremap() region\n");
|
2005-01-24 08:37:04 +08:00
|
|
|
goto exit_free;
|
|
|
|
}
|
|
|
|
|
2007-04-26 13:41:34 +08:00
|
|
|
dev_dbg(&pdev->dev, "got platform resource %p (0x%llx)\n", res,
|
|
|
|
(unsigned long long)res->start);
|
2005-01-24 08:37:04 +08:00
|
|
|
|
|
|
|
/* setup map parameters */
|
|
|
|
|
|
|
|
info->map.phys = res->start;
|
2009-07-17 23:47:37 +08:00
|
|
|
info->map.size = resource_size(res);
|
2008-03-04 01:30:24 +08:00
|
|
|
info->map.name = pdata->mapname != NULL ?
|
|
|
|
(char *)pdata->mapname : (char *)pdev->name;
|
2005-01-24 08:37:04 +08:00
|
|
|
info->map.bankwidth = pdata->bankwidth;
|
|
|
|
|
2005-11-10 06:32:44 +08:00
|
|
|
dev_dbg(&pdev->dev, "virt %p, %lu bytes\n", info->map.virt, info->map.size);
|
2005-01-24 08:37:04 +08:00
|
|
|
|
|
|
|
simple_map_init(&info->map);
|
|
|
|
|
2005-11-10 06:32:44 +08:00
|
|
|
dev_dbg(&pdev->dev, "initialised map, probing for mtd\n");
|
2005-01-24 08:37:04 +08:00
|
|
|
|
2008-03-04 01:30:24 +08:00
|
|
|
/* probe for the right mtd map driver
|
|
|
|
* supplied by the platform_data struct */
|
|
|
|
|
2008-04-29 07:50:04 +08:00
|
|
|
if (pdata->map_probes) {
|
2013-03-12 16:28:31 +08:00
|
|
|
const char * const *map_probes = pdata->map_probes;
|
2008-03-04 01:30:24 +08:00
|
|
|
|
|
|
|
for ( ; !info->mtd && *map_probes; map_probes++)
|
|
|
|
info->mtd = do_map_probe(*map_probes , &info->map);
|
|
|
|
}
|
|
|
|
/* fallback to map_ram */
|
|
|
|
else
|
|
|
|
info->mtd = do_map_probe("map_ram", &info->map);
|
2005-01-24 08:37:04 +08:00
|
|
|
|
|
|
|
if (info->mtd == NULL) {
|
2005-11-10 06:32:44 +08:00
|
|
|
dev_err(&pdev->dev, "failed to probe for map_ram\n");
|
2005-01-24 08:37:04 +08:00
|
|
|
err = -ENOMEM;
|
|
|
|
goto exit_free;
|
|
|
|
}
|
|
|
|
|
2009-03-26 15:42:50 +08:00
|
|
|
info->mtd->dev.parent = &pdev->dev;
|
2005-01-24 08:37:04 +08:00
|
|
|
|
|
|
|
platram_setrw(info, PLATRAM_RW);
|
|
|
|
|
2012-09-20 09:48:00 +08:00
|
|
|
/* check to see if there are any available partitions, or whether
|
2005-01-24 08:37:04 +08:00
|
|
|
* to add this device whole */
|
|
|
|
|
mtd: do not use plain 0 as NULL
The first 3 arguments of 'mtd_device_parse_register()' are pointers,
but many callers pass '0' instead of 'NULL'. Fix this globally. Thanks
to coccinelle for making it easy to do with the following semantic patch:
@@
expression mtd, types, parser_data, parts, nr_parts;
@@
(
-mtd_device_parse_register(mtd, 0, parser_data, parts, nr_parts)
+mtd_device_parse_register(mtd, NULL, parser_data, parts, nr_parts)
|
-mtd_device_parse_register(mtd, types, 0, parts, nr_parts)
+mtd_device_parse_register(mtd, types, NULL, parts, nr_parts)
|
-mtd_device_parse_register(mtd, types, parser_data, 0, nr_parts)
+mtd_device_parse_register(mtd, types, parser_data, NULL, nr_parts)
)
Signed-off-by: Artem Bityutskiy <artem.bityutskiy@linux.intel.com>
Signed-off-by: David Woodhouse <David.Woodhouse@intel.com>
2012-03-10 01:24:26 +08:00
|
|
|
err = mtd_device_parse_register(info->mtd, pdata->probes, NULL,
|
|
|
|
pdata->partitions,
|
|
|
|
pdata->nr_partitions);
|
2008-03-04 01:30:24 +08:00
|
|
|
if (!err)
|
|
|
|
dev_info(&pdev->dev, "registered mtd device\n");
|
|
|
|
|
2011-12-13 07:37:56 +08:00
|
|
|
if (pdata->nr_partitions) {
|
|
|
|
/* add the whole device. */
|
|
|
|
err = mtd_device_register(info->mtd, NULL, 0);
|
|
|
|
if (err) {
|
|
|
|
dev_err(&pdev->dev,
|
|
|
|
"failed to register the entire device\n");
|
|
|
|
}
|
|
|
|
}
|
2011-05-23 17:23:07 +08:00
|
|
|
|
2005-01-24 08:37:04 +08:00
|
|
|
return err;
|
|
|
|
|
|
|
|
exit_free:
|
2005-11-10 06:32:44 +08:00
|
|
|
platram_remove(pdev);
|
2005-01-24 08:37:04 +08:00
|
|
|
exit_error:
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* device driver info */
|
|
|
|
|
2008-04-19 04:44:26 +08:00
|
|
|
/* work with hotplug and coldplug */
|
|
|
|
MODULE_ALIAS("platform:mtd-ram");
|
|
|
|
|
2005-11-10 06:32:44 +08:00
|
|
|
static struct platform_driver platram_driver = {
|
2005-01-24 08:37:04 +08:00
|
|
|
.probe = platram_probe,
|
|
|
|
.remove = platram_remove,
|
2005-11-10 06:32:44 +08:00
|
|
|
.driver = {
|
|
|
|
.name = "mtd-ram",
|
|
|
|
},
|
2005-01-24 08:37:04 +08:00
|
|
|
};
|
|
|
|
|
2013-10-11 12:41:23 +08:00
|
|
|
module_platform_driver(platram_driver);
|
2005-01-24 08:37:04 +08:00
|
|
|
|
|
|
|
MODULE_LICENSE("GPL");
|
|
|
|
MODULE_AUTHOR("Ben Dooks <ben@simtec.co.uk>");
|
|
|
|
MODULE_DESCRIPTION("MTD platform RAM map driver");
|