blob: 593f73d480d2cf9e615ced4e2e66d758e3c4f8f3 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Common code to handle map devices which are simple ROM
3 * (C) 2000 Red Hat. GPL'd.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 */
5
6#include <linux/module.h>
7#include <linux/types.h>
8#include <linux/kernel.h>
9#include <asm/io.h>
10#include <asm/byteorder.h>
11#include <linux/errno.h>
12#include <linux/slab.h>
13#include <linux/init.h>
14#include <linux/mtd/mtd.h>
15#include <linux/mtd/map.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016
17static int maprom_read (struct mtd_info *, loff_t, size_t, size_t *, u_char *);
18static int maprom_write (struct mtd_info *, loff_t, size_t, size_t *, const u_char *);
19static void maprom_nop (struct mtd_info *);
20static struct mtd_info *map_rom_probe(struct map_info *map);
Alan Cox5f877602009-01-11 19:52:19 +000021static int maprom_erase (struct mtd_info *mtd, struct erase_info *info);
David Howells402d3262009-02-12 10:40:00 +000022static unsigned long maprom_unmapped_area(struct mtd_info *, unsigned long,
23 unsigned long, unsigned long);
Linus Torvalds1da177e2005-04-16 15:20:36 -070024
25static struct mtd_chip_driver maprom_chipdrv = {
26 .probe = map_rom_probe,
27 .name = "map_rom",
28 .module = THIS_MODULE
29};
30
31static struct mtd_info *map_rom_probe(struct map_info *map)
32{
33 struct mtd_info *mtd;
34
Burman Yan95b93a02006-11-15 21:10:29 +020035 mtd = kzalloc(sizeof(*mtd), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070036 if (!mtd)
37 return NULL;
38
Linus Torvalds1da177e2005-04-16 15:20:36 -070039 map->fldrv = &maprom_chipdrv;
40 mtd->priv = map;
41 mtd->name = map->name;
David Woodhouse21c8db92006-06-14 21:39:48 +010042 mtd->type = MTD_ROM;
Linus Torvalds1da177e2005-04-16 15:20:36 -070043 mtd->size = map->size;
David Howells402d3262009-02-12 10:40:00 +000044 mtd->get_unmapped_area = maprom_unmapped_area;
Linus Torvalds1da177e2005-04-16 15:20:36 -070045 mtd->read = maprom_read;
46 mtd->write = maprom_write;
47 mtd->sync = maprom_nop;
Alan Cox5f877602009-01-11 19:52:19 +000048 mtd->erase = maprom_erase;
Linus Torvalds1da177e2005-04-16 15:20:36 -070049 mtd->flags = MTD_CAP_ROM;
Joern Engele369d622006-05-30 14:25:17 +020050 mtd->erasesize = map->size;
Artem B. Bityutskiy17ffc7b2006-06-22 18:15:48 +040051 mtd->writesize = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
53 __module_get(THIS_MODULE);
54 return mtd;
55}
56
57
David Howells402d3262009-02-12 10:40:00 +000058/*
59 * Allow NOMMU mmap() to directly map the device (if not NULL)
60 * - return the address to which the offset maps
61 * - return -ENOSYS to indicate refusal to do the mapping
62 */
63static unsigned long maprom_unmapped_area(struct mtd_info *mtd,
64 unsigned long len,
65 unsigned long offset,
66 unsigned long flags)
67{
68 struct map_info *map = mtd->priv;
69 return (unsigned long) map->virt + offset;
70}
71
Linus Torvalds1da177e2005-04-16 15:20:36 -070072static int maprom_read (struct mtd_info *mtd, loff_t from, size_t len, size_t *retlen, u_char *buf)
73{
74 struct map_info *map = mtd->priv;
75
76 map_copy_from(map, buf, from, len);
77 *retlen = len;
78 return 0;
79}
80
81static void maprom_nop(struct mtd_info *mtd)
82{
83 /* Nothing to see here */
84}
85
86static int maprom_write (struct mtd_info *mtd, loff_t to, size_t len, size_t *retlen, const u_char *buf)
87{
88 printk(KERN_NOTICE "maprom_write called\n");
89 return -EIO;
90}
91
Alan Cox5f877602009-01-11 19:52:19 +000092static int maprom_erase (struct mtd_info *mtd, struct erase_info *info)
93{
94 /* We do our best 8) */
95 return -EROFS;
96}
97
Linus Torvalds1da177e2005-04-16 15:20:36 -070098static int __init map_rom_init(void)
99{
100 register_mtd_chip_driver(&maprom_chipdrv);
101 return 0;
102}
103
104static void __exit map_rom_exit(void)
105{
106 unregister_mtd_chip_driver(&maprom_chipdrv);
107}
108
109module_init(map_rom_init);
110module_exit(map_rom_exit);
111
112MODULE_LICENSE("GPL");
113MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>");
114MODULE_DESCRIPTION("MTD chip driver for ROM chips");