blob: 076090a67b9088319b676ce58fc7cf82263d6a85 [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>
16#include <linux/mtd/compatmac.h>
17
18static int maprom_read (struct mtd_info *, loff_t, size_t, size_t *, u_char *);
19static int maprom_write (struct mtd_info *, loff_t, size_t, size_t *, const u_char *);
20static void maprom_nop (struct mtd_info *);
21static struct mtd_info *map_rom_probe(struct map_info *map);
Alan Cox5f877602009-01-11 19:52:19 +000022static int maprom_erase (struct mtd_info *mtd, struct erase_info *info);
David Howells402d3262009-02-12 10:40:00 +000023static unsigned long maprom_unmapped_area(struct mtd_info *, unsigned long,
24 unsigned long, unsigned long);
Linus Torvalds1da177e2005-04-16 15:20:36 -070025
26static struct mtd_chip_driver maprom_chipdrv = {
27 .probe = map_rom_probe,
28 .name = "map_rom",
29 .module = THIS_MODULE
30};
31
32static struct mtd_info *map_rom_probe(struct map_info *map)
33{
34 struct mtd_info *mtd;
35
Burman Yan95b93a02006-11-15 21:10:29 +020036 mtd = kzalloc(sizeof(*mtd), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070037 if (!mtd)
38 return NULL;
39
Linus Torvalds1da177e2005-04-16 15:20:36 -070040 map->fldrv = &maprom_chipdrv;
41 mtd->priv = map;
42 mtd->name = map->name;
David Woodhouse21c8db92006-06-14 21:39:48 +010043 mtd->type = MTD_ROM;
Linus Torvalds1da177e2005-04-16 15:20:36 -070044 mtd->size = map->size;
David Howells402d3262009-02-12 10:40:00 +000045 mtd->get_unmapped_area = maprom_unmapped_area;
Linus Torvalds1da177e2005-04-16 15:20:36 -070046 mtd->read = maprom_read;
47 mtd->write = maprom_write;
48 mtd->sync = maprom_nop;
Alan Cox5f877602009-01-11 19:52:19 +000049 mtd->erase = maprom_erase;
Linus Torvalds1da177e2005-04-16 15:20:36 -070050 mtd->flags = MTD_CAP_ROM;
Joern Engele369d622006-05-30 14:25:17 +020051 mtd->erasesize = map->size;
Artem B. Bityutskiy17ffc7b2006-06-22 18:15:48 +040052 mtd->writesize = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070053
54 __module_get(THIS_MODULE);
55 return mtd;
56}
57
58
David Howells402d3262009-02-12 10:40:00 +000059/*
60 * Allow NOMMU mmap() to directly map the device (if not NULL)
61 * - return the address to which the offset maps
62 * - return -ENOSYS to indicate refusal to do the mapping
63 */
64static unsigned long maprom_unmapped_area(struct mtd_info *mtd,
65 unsigned long len,
66 unsigned long offset,
67 unsigned long flags)
68{
69 struct map_info *map = mtd->priv;
70 return (unsigned long) map->virt + offset;
71}
72
Linus Torvalds1da177e2005-04-16 15:20:36 -070073static int maprom_read (struct mtd_info *mtd, loff_t from, size_t len, size_t *retlen, u_char *buf)
74{
75 struct map_info *map = mtd->priv;
76
77 map_copy_from(map, buf, from, len);
78 *retlen = len;
79 return 0;
80}
81
82static void maprom_nop(struct mtd_info *mtd)
83{
84 /* Nothing to see here */
85}
86
87static int maprom_write (struct mtd_info *mtd, loff_t to, size_t len, size_t *retlen, const u_char *buf)
88{
89 printk(KERN_NOTICE "maprom_write called\n");
90 return -EIO;
91}
92
Alan Cox5f877602009-01-11 19:52:19 +000093static int maprom_erase (struct mtd_info *mtd, struct erase_info *info)
94{
95 /* We do our best 8) */
96 return -EROFS;
97}
98
Linus Torvalds1da177e2005-04-16 15:20:36 -070099static int __init map_rom_init(void)
100{
101 register_mtd_chip_driver(&maprom_chipdrv);
102 return 0;
103}
104
105static void __exit map_rom_exit(void)
106{
107 unregister_mtd_chip_driver(&maprom_chipdrv);
108}
109
110module_init(map_rom_init);
111module_exit(map_rom_exit);
112
113MODULE_LICENSE("GPL");
114MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>");
115MODULE_DESCRIPTION("MTD chip driver for ROM chips");