2023-06-04 02:52:00 +08:00
|
|
|
// SPDX-License-Identifier: GPL-2.0
|
2005-04-17 06:20:36 +08:00
|
|
|
/*
|
|
|
|
* Common code to handle map devices which are simple ROM
|
2023-06-04 02:52:00 +08:00
|
|
|
* (C) 2000 Red Hat.
|
2005-04-17 06:20:36 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <linux/module.h>
|
|
|
|
#include <linux/types.h>
|
|
|
|
#include <linux/kernel.h>
|
|
|
|
#include <asm/io.h>
|
|
|
|
#include <asm/byteorder.h>
|
|
|
|
#include <linux/errno.h>
|
|
|
|
#include <linux/slab.h>
|
|
|
|
#include <linux/init.h>
|
2014-09-26 01:20:24 +08:00
|
|
|
#include <linux/of.h>
|
2005-04-17 06:20:36 +08:00
|
|
|
#include <linux/mtd/mtd.h>
|
|
|
|
#include <linux/mtd/map.h>
|
|
|
|
|
|
|
|
static int maprom_read (struct mtd_info *, loff_t, size_t, size_t *, u_char *);
|
|
|
|
static int maprom_write (struct mtd_info *, loff_t, size_t, size_t *, const u_char *);
|
|
|
|
static void maprom_nop (struct mtd_info *);
|
|
|
|
static struct mtd_info *map_rom_probe(struct map_info *map);
|
2009-01-12 03:52:19 +08:00
|
|
|
static int maprom_erase (struct mtd_info *mtd, struct erase_info *info);
|
2017-10-31 02:48:31 +08:00
|
|
|
static int maprom_point (struct mtd_info *mtd, loff_t from, size_t len,
|
|
|
|
size_t *retlen, void **virt, resource_size_t *phys);
|
|
|
|
static int maprom_unpoint(struct mtd_info *mtd, loff_t from, size_t len);
|
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
static struct mtd_chip_driver maprom_chipdrv = {
|
|
|
|
.probe = map_rom_probe,
|
|
|
|
.name = "map_rom",
|
|
|
|
.module = THIS_MODULE
|
|
|
|
};
|
|
|
|
|
2014-09-26 01:20:24 +08:00
|
|
|
static unsigned int default_erasesize(struct map_info *map)
|
|
|
|
{
|
|
|
|
const __be32 *erase_size = NULL;
|
|
|
|
|
|
|
|
erase_size = of_get_property(map->device_node, "erase-size", NULL);
|
|
|
|
|
|
|
|
return !erase_size ? map->size : be32_to_cpu(*erase_size);
|
|
|
|
}
|
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
static struct mtd_info *map_rom_probe(struct map_info *map)
|
|
|
|
{
|
|
|
|
struct mtd_info *mtd;
|
|
|
|
|
2006-11-16 03:10:29 +08:00
|
|
|
mtd = kzalloc(sizeof(*mtd), GFP_KERNEL);
|
2005-04-17 06:20:36 +08:00
|
|
|
if (!mtd)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
map->fldrv = &maprom_chipdrv;
|
|
|
|
mtd->priv = map;
|
|
|
|
mtd->name = map->name;
|
2006-06-15 04:39:48 +08:00
|
|
|
mtd->type = MTD_ROM;
|
2005-04-17 06:20:36 +08:00
|
|
|
mtd->size = map->size;
|
2017-10-31 02:48:31 +08:00
|
|
|
mtd->_point = maprom_point;
|
|
|
|
mtd->_unpoint = maprom_unpoint;
|
2012-01-30 20:58:32 +08:00
|
|
|
mtd->_read = maprom_read;
|
|
|
|
mtd->_write = maprom_write;
|
|
|
|
mtd->_sync = maprom_nop;
|
|
|
|
mtd->_erase = maprom_erase;
|
2005-04-17 06:20:36 +08:00
|
|
|
mtd->flags = MTD_CAP_ROM;
|
2014-09-26 01:20:24 +08:00
|
|
|
mtd->erasesize = default_erasesize(map);
|
2006-06-22 22:15:48 +08:00
|
|
|
mtd->writesize = 1;
|
2014-09-26 01:20:24 +08:00
|
|
|
mtd->writebufsize = 1;
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
__module_get(THIS_MODULE);
|
|
|
|
return mtd;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-10-31 02:48:31 +08:00
|
|
|
static int maprom_point(struct mtd_info *mtd, loff_t from, size_t len,
|
|
|
|
size_t *retlen, void **virt, resource_size_t *phys)
|
|
|
|
{
|
|
|
|
struct map_info *map = mtd->priv;
|
|
|
|
|
|
|
|
if (!map->virt)
|
|
|
|
return -EINVAL;
|
|
|
|
*virt = map->virt + from;
|
|
|
|
if (phys)
|
|
|
|
*phys = map->phys + from;
|
|
|
|
*retlen = len;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int maprom_unpoint(struct mtd_info *mtd, loff_t from, size_t len)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
static int maprom_read (struct mtd_info *mtd, loff_t from, size_t len, size_t *retlen, u_char *buf)
|
|
|
|
{
|
|
|
|
struct map_info *map = mtd->priv;
|
|
|
|
|
|
|
|
map_copy_from(map, buf, from, len);
|
|
|
|
*retlen = len;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void maprom_nop(struct mtd_info *mtd)
|
|
|
|
{
|
|
|
|
/* Nothing to see here */
|
|
|
|
}
|
|
|
|
|
|
|
|
static int maprom_write (struct mtd_info *mtd, loff_t to, size_t len, size_t *retlen, const u_char *buf)
|
|
|
|
{
|
2012-02-04 00:13:23 +08:00
|
|
|
return -EROFS;
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
|
2009-01-12 03:52:19 +08:00
|
|
|
static int maprom_erase (struct mtd_info *mtd, struct erase_info *info)
|
|
|
|
{
|
|
|
|
/* We do our best 8) */
|
|
|
|
return -EROFS;
|
|
|
|
}
|
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
static int __init map_rom_init(void)
|
|
|
|
{
|
|
|
|
register_mtd_chip_driver(&maprom_chipdrv);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void __exit map_rom_exit(void)
|
|
|
|
{
|
|
|
|
unregister_mtd_chip_driver(&maprom_chipdrv);
|
|
|
|
}
|
|
|
|
|
|
|
|
module_init(map_rom_init);
|
|
|
|
module_exit(map_rom_exit);
|
|
|
|
|
|
|
|
MODULE_LICENSE("GPL");
|
|
|
|
MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>");
|
|
|
|
MODULE_DESCRIPTION("MTD chip driver for ROM chips");
|