blob: afb43d5e178269d33443a59a439245263cb263c0 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Common code to handle map devices which are simple RAM
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
17
18static int mapram_read (struct mtd_info *, loff_t, size_t, size_t *, u_char *);
19static int mapram_write (struct mtd_info *, loff_t, size_t, size_t *, const u_char *);
20static int mapram_erase (struct mtd_info *, struct erase_info *);
21static void mapram_nop (struct mtd_info *);
22static struct mtd_info *map_ram_probe(struct map_info *map);
David Howells402d3262009-02-12 10:40:00 +000023static unsigned long mapram_unmapped_area(struct mtd_info *, unsigned long,
24 unsigned long, unsigned long);
Linus Torvalds1da177e2005-04-16 15:20:36 -070025
26
27static struct mtd_chip_driver mapram_chipdrv = {
28 .probe = map_ram_probe,
29 .name = "map_ram",
30 .module = THIS_MODULE
31};
32
33static struct mtd_info *map_ram_probe(struct map_info *map)
34{
35 struct mtd_info *mtd;
36
37 /* Check the first byte is RAM */
38#if 0
39 map_write8(map, 0x55, 0);
40 if (map_read8(map, 0) != 0x55)
41 return NULL;
42
43 map_write8(map, 0xAA, 0);
44 if (map_read8(map, 0) != 0xAA)
45 return NULL;
46
47 /* Check the last byte is RAM */
48 map_write8(map, 0x55, map->size-1);
49 if (map_read8(map, map->size-1) != 0x55)
50 return NULL;
51
52 map_write8(map, 0xAA, map->size-1);
53 if (map_read8(map, map->size-1) != 0xAA)
54 return NULL;
55#endif
56 /* OK. It seems to be RAM. */
57
Burman Yan95b93a02006-11-15 21:10:29 +020058 mtd = kzalloc(sizeof(*mtd), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 if (!mtd)
60 return NULL;
61
Linus Torvalds1da177e2005-04-16 15:20:36 -070062 map->fldrv = &mapram_chipdrv;
63 mtd->priv = map;
64 mtd->name = map->name;
David Woodhouse21c8db92006-06-14 21:39:48 +010065 mtd->type = MTD_RAM;
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 mtd->size = map->size;
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +020067 mtd->_erase = mapram_erase;
68 mtd->_get_unmapped_area = mapram_unmapped_area;
69 mtd->_read = mapram_read;
70 mtd->_write = mapram_write;
Alessio Igor Bogani106effb2014-12-11 10:38:06 +010071 mtd->_panic_write = mapram_write;
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +020072 mtd->_sync = mapram_nop;
Jörn Engela6c591e2006-04-13 18:54:34 +020073 mtd->flags = MTD_CAP_RAM;
Artem B. Bityutskiy17ffc7b2006-06-22 18:15:48 +040074 mtd->writesize = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070075
76 mtd->erasesize = PAGE_SIZE;
77 while(mtd->size & (mtd->erasesize - 1))
78 mtd->erasesize >>= 1;
79
80 __module_get(THIS_MODULE);
81 return mtd;
82}
83
84
David Howells402d3262009-02-12 10:40:00 +000085/*
86 * Allow NOMMU mmap() to directly map the device (if not NULL)
87 * - return the address to which the offset maps
88 * - return -ENOSYS to indicate refusal to do the mapping
89 */
90static unsigned long mapram_unmapped_area(struct mtd_info *mtd,
91 unsigned long len,
92 unsigned long offset,
93 unsigned long flags)
94{
95 struct map_info *map = mtd->priv;
96 return (unsigned long) map->virt + offset;
97}
98
Linus Torvalds1da177e2005-04-16 15:20:36 -070099static int mapram_read (struct mtd_info *mtd, loff_t from, size_t len, size_t *retlen, u_char *buf)
100{
101 struct map_info *map = mtd->priv;
102
103 map_copy_from(map, buf, from, len);
104 *retlen = len;
105 return 0;
106}
107
108static int mapram_write (struct mtd_info *mtd, loff_t to, size_t len, size_t *retlen, const u_char *buf)
109{
110 struct map_info *map = mtd->priv;
111
112 map_copy_to(map, to, buf, len);
113 *retlen = len;
114 return 0;
115}
116
117static int mapram_erase (struct mtd_info *mtd, struct erase_info *instr)
118{
119 /* Yeah, it's inefficient. Who cares? It's faster than a _real_
120 flash erase. */
121 struct map_info *map = mtd->priv;
122 map_word allff;
123 unsigned long i;
124
125 allff = map_word_ff(map);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 for (i=0; i<instr->len; i += map_bankwidth(map))
127 map_write(map, allff, instr->addr + i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 instr->state = MTD_ERASE_DONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 mtd_erase_callback(instr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 return 0;
131}
132
133static void mapram_nop(struct mtd_info *mtd)
134{
135 /* Nothing to see here */
136}
137
138static int __init map_ram_init(void)
139{
140 register_mtd_chip_driver(&mapram_chipdrv);
141 return 0;
142}
143
144static void __exit map_ram_exit(void)
145{
146 unregister_mtd_chip_driver(&mapram_chipdrv);
147}
148
149module_init(map_ram_init);
150module_exit(map_ram_exit);
151
152MODULE_LICENSE("GPL");
153MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>");
154MODULE_DESCRIPTION("MTD chip driver for RAM chips");