Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Common code to handle map devices which are simple ROM |
| 3 | * (C) 2000 Red Hat. GPL'd. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4 | */ |
| 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> |
Aaron Sierra | 6958024 | 2014-09-25 12:20:24 -0500 | [diff] [blame] | 14 | #include <linux/of.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 15 | #include <linux/mtd/mtd.h> |
| 16 | #include <linux/mtd/map.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 | |
| 18 | static int maprom_read (struct mtd_info *, loff_t, size_t, size_t *, u_char *); |
| 19 | static int maprom_write (struct mtd_info *, loff_t, size_t, size_t *, const u_char *); |
| 20 | static void maprom_nop (struct mtd_info *); |
| 21 | static struct mtd_info *map_rom_probe(struct map_info *map); |
Alan Cox | 5f87760 | 2009-01-11 19:52:19 +0000 | [diff] [blame] | 22 | static int maprom_erase (struct mtd_info *mtd, struct erase_info *info); |
David Howells | 402d326 | 2009-02-12 10:40:00 +0000 | [diff] [blame] | 23 | static unsigned long maprom_unmapped_area(struct mtd_info *, unsigned long, |
| 24 | unsigned long, unsigned long); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | |
| 26 | static struct mtd_chip_driver maprom_chipdrv = { |
| 27 | .probe = map_rom_probe, |
| 28 | .name = "map_rom", |
| 29 | .module = THIS_MODULE |
| 30 | }; |
| 31 | |
Aaron Sierra | 6958024 | 2014-09-25 12:20:24 -0500 | [diff] [blame] | 32 | static unsigned int default_erasesize(struct map_info *map) |
| 33 | { |
| 34 | const __be32 *erase_size = NULL; |
| 35 | |
| 36 | erase_size = of_get_property(map->device_node, "erase-size", NULL); |
| 37 | |
| 38 | return !erase_size ? map->size : be32_to_cpu(*erase_size); |
| 39 | } |
| 40 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | static struct mtd_info *map_rom_probe(struct map_info *map) |
| 42 | { |
| 43 | struct mtd_info *mtd; |
| 44 | |
Burman Yan | 95b93a0 | 2006-11-15 21:10:29 +0200 | [diff] [blame] | 45 | mtd = kzalloc(sizeof(*mtd), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 46 | if (!mtd) |
| 47 | return NULL; |
| 48 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 49 | map->fldrv = &maprom_chipdrv; |
| 50 | mtd->priv = map; |
| 51 | mtd->name = map->name; |
David Woodhouse | 21c8db9 | 2006-06-14 21:39:48 +0100 | [diff] [blame] | 52 | mtd->type = MTD_ROM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 53 | mtd->size = map->size; |
Artem Bityutskiy | 3c3c10b | 2012-01-30 14:58:32 +0200 | [diff] [blame] | 54 | mtd->_get_unmapped_area = maprom_unmapped_area; |
| 55 | mtd->_read = maprom_read; |
| 56 | mtd->_write = maprom_write; |
| 57 | mtd->_sync = maprom_nop; |
| 58 | mtd->_erase = maprom_erase; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 59 | mtd->flags = MTD_CAP_ROM; |
Aaron Sierra | 6958024 | 2014-09-25 12:20:24 -0500 | [diff] [blame] | 60 | mtd->erasesize = default_erasesize(map); |
Artem B. Bityutskiy | 17ffc7b | 2006-06-22 18:15:48 +0400 | [diff] [blame] | 61 | mtd->writesize = 1; |
Aaron Sierra | 6958024 | 2014-09-25 12:20:24 -0500 | [diff] [blame] | 62 | mtd->writebufsize = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 63 | |
| 64 | __module_get(THIS_MODULE); |
| 65 | return mtd; |
| 66 | } |
| 67 | |
| 68 | |
David Howells | 402d326 | 2009-02-12 10:40:00 +0000 | [diff] [blame] | 69 | /* |
| 70 | * Allow NOMMU mmap() to directly map the device (if not NULL) |
| 71 | * - return the address to which the offset maps |
| 72 | * - return -ENOSYS to indicate refusal to do the mapping |
| 73 | */ |
| 74 | static unsigned long maprom_unmapped_area(struct mtd_info *mtd, |
| 75 | unsigned long len, |
| 76 | unsigned long offset, |
| 77 | unsigned long flags) |
| 78 | { |
| 79 | struct map_info *map = mtd->priv; |
| 80 | return (unsigned long) map->virt + offset; |
| 81 | } |
| 82 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 83 | static int maprom_read (struct mtd_info *mtd, loff_t from, size_t len, size_t *retlen, u_char *buf) |
| 84 | { |
| 85 | struct map_info *map = mtd->priv; |
| 86 | |
| 87 | map_copy_from(map, buf, from, len); |
| 88 | *retlen = len; |
| 89 | return 0; |
| 90 | } |
| 91 | |
| 92 | static void maprom_nop(struct mtd_info *mtd) |
| 93 | { |
| 94 | /* Nothing to see here */ |
| 95 | } |
| 96 | |
| 97 | static int maprom_write (struct mtd_info *mtd, loff_t to, size_t len, size_t *retlen, const u_char *buf) |
| 98 | { |
Artem Bityutskiy | 664addc | 2012-02-03 18:13:23 +0200 | [diff] [blame] | 99 | return -EROFS; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 100 | } |
| 101 | |
Alan Cox | 5f87760 | 2009-01-11 19:52:19 +0000 | [diff] [blame] | 102 | static int maprom_erase (struct mtd_info *mtd, struct erase_info *info) |
| 103 | { |
| 104 | /* We do our best 8) */ |
| 105 | return -EROFS; |
| 106 | } |
| 107 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 108 | static int __init map_rom_init(void) |
| 109 | { |
| 110 | register_mtd_chip_driver(&maprom_chipdrv); |
| 111 | return 0; |
| 112 | } |
| 113 | |
| 114 | static void __exit map_rom_exit(void) |
| 115 | { |
| 116 | unregister_mtd_chip_driver(&maprom_chipdrv); |
| 117 | } |
| 118 | |
| 119 | module_init(map_rom_init); |
| 120 | module_exit(map_rom_exit); |
| 121 | |
| 122 | MODULE_LICENSE("GPL"); |
| 123 | MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>"); |
| 124 | MODULE_DESCRIPTION("MTD chip driver for ROM chips"); |