ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / drivers / mtd / devices / mtdram.c
1 /*
2  * mtdram - a test mtd device
3  * $Id: mtdram.c,v 1.32 2003/05/21 15:15:07 dwmw2 Exp $
4  * Author: Alexander Larsson <alex@cendio.se>
5  *
6  * Copyright (c) 1999 Alexander Larsson <alex@cendio.se>
7  *
8  * This code is GPL
9  *
10  */
11
12 #include <linux/config.h>
13 #include <linux/module.h>
14 #include <linux/slab.h>
15 #include <linux/ioport.h>
16 #include <linux/vmalloc.h>
17 #include <linux/init.h>
18 #include <linux/mtd/compatmac.h>
19 #include <linux/mtd/mtd.h>
20
21 #ifndef CONFIG_MTDRAM_ABS_POS
22   #define CONFIG_MTDRAM_ABS_POS 0
23 #endif
24
25 #if CONFIG_MTDRAM_ABS_POS > 0
26   #include <asm/io.h>
27 #endif
28
29 #ifdef MODULE
30 static unsigned long total_size = CONFIG_MTDRAM_TOTAL_SIZE;
31 static unsigned long erase_size = CONFIG_MTDRAM_ERASE_SIZE;
32 MODULE_PARM(total_size,"l");
33 MODULE_PARM_DESC(total_size, "Total device size in KiB");
34 MODULE_PARM(erase_size,"l");
35 MODULE_PARM_DESC(erase_size, "Device erase block size in KiB");
36 #define MTDRAM_TOTAL_SIZE (total_size * 1024)
37 #define MTDRAM_ERASE_SIZE (erase_size * 1024)
38 #else
39 #define MTDRAM_TOTAL_SIZE (CONFIG_MTDRAM_TOTAL_SIZE * 1024)
40 #define MTDRAM_ERASE_SIZE (CONFIG_MTDRAM_ERASE_SIZE * 1024)
41 #endif
42
43
44 // We could store these in the mtd structure, but we only support 1 device..
45 static struct mtd_info *mtd_info;
46
47
48 static int
49 ram_erase(struct mtd_info *mtd, struct erase_info *instr)
50 {
51   DEBUG(MTD_DEBUG_LEVEL2, "ram_erase(pos:%ld, len:%ld)\n", (long)instr->addr, (long)instr->len);
52   if (instr->addr + instr->len > mtd->size) {
53     DEBUG(MTD_DEBUG_LEVEL1, "ram_erase() out of bounds (%ld > %ld)\n", (long)(instr->addr + instr->len), (long)mtd->size);
54     return -EINVAL;
55   }
56         
57   memset((char *)mtd->priv + instr->addr, 0xff, instr->len);
58         
59   instr->state = MTD_ERASE_DONE;
60
61   if (instr->callback)
62     (*(instr->callback))(instr);
63   return 0;
64 }
65
66 static int ram_point (struct mtd_info *mtd, loff_t from, size_t len, size_t *retlen, u_char **mtdbuf)
67 {
68   if (from + len > mtd->size)
69     return -EINVAL;
70         
71   *mtdbuf = mtd->priv + from;
72   *retlen = len;
73   return 0;
74 }
75
76 static void ram_unpoint (struct mtd_info *mtd, u_char *addr, loff_t from,
77                          size_t len)
78 {
79   DEBUG(MTD_DEBUG_LEVEL2, "ram_unpoint\n");
80 }
81
82 static int ram_read(struct mtd_info *mtd, loff_t from, size_t len,
83              size_t *retlen, u_char *buf)
84 {
85   DEBUG(MTD_DEBUG_LEVEL2, "ram_read(pos:%ld, len:%ld)\n", (long)from, (long)len);
86   if (from + len > mtd->size) {
87     DEBUG(MTD_DEBUG_LEVEL1, "ram_read() out of bounds (%ld > %ld)\n", (long)(from + len), (long)mtd->size);
88     return -EINVAL;
89   }
90
91   memcpy(buf, mtd->priv + from, len);
92
93   *retlen=len;
94   return 0;
95 }
96
97 static int ram_write(struct mtd_info *mtd, loff_t to, size_t len,
98               size_t *retlen, const u_char *buf)
99 {
100   DEBUG(MTD_DEBUG_LEVEL2, "ram_write(pos:%ld, len:%ld)\n", (long)to, (long)len);
101   if (to + len > mtd->size) {
102     DEBUG(MTD_DEBUG_LEVEL1, "ram_write() out of bounds (%ld > %ld)\n", (long)(to + len), (long)mtd->size);
103     return -EINVAL;
104   }
105
106   memcpy ((char *)mtd->priv + to, buf, len);
107
108   *retlen=len;
109   return 0;
110 }
111
112 static void __exit cleanup_mtdram(void)
113 {
114   if (mtd_info) {
115     del_mtd_device(mtd_info);
116 #if CONFIG_MTDRAM_TOTAL_SIZE > 0
117     if (mtd_info->priv)
118 #if CONFIG_MTDRAM_ABS_POS > 0
119       iounmap(mtd_info->priv);
120 #else
121       vfree(mtd_info->priv);
122 #endif  
123 #endif
124     kfree(mtd_info);
125   }
126 }
127
128 int mtdram_init_device(struct mtd_info *mtd, void *mapped_address, 
129                        unsigned long size, char *name)
130 {
131    memset(mtd, 0, sizeof(*mtd));
132
133    /* Setup the MTD structure */
134    mtd->name = name;
135    mtd->type = MTD_RAM;
136    mtd->flags = MTD_CAP_RAM;
137    mtd->size = size;
138    mtd->erasesize = MTDRAM_ERASE_SIZE;
139    mtd->priv = mapped_address;
140
141    mtd->owner = THIS_MODULE;
142    mtd->erase = ram_erase;
143    mtd->point = ram_point;
144    mtd->unpoint = ram_unpoint;
145    mtd->read = ram_read;
146    mtd->write = ram_write;
147
148    if (add_mtd_device(mtd)) {
149      return -EIO;
150    }
151    
152    return 0;
153 }
154
155 #if CONFIG_MTDRAM_TOTAL_SIZE > 0
156 #if CONFIG_MTDRAM_ABS_POS > 0
157 int __init init_mtdram(void)
158 {
159   void *addr;
160   int err;
161   /* Allocate some memory */
162    mtd_info = (struct mtd_info *)kmalloc(sizeof(struct mtd_info), GFP_KERNEL);
163    if (!mtd_info)
164      return -ENOMEM;
165    
166   addr = ioremap(CONFIG_MTDRAM_ABS_POS, MTDRAM_TOTAL_SIZE);
167   if (!addr) {
168     DEBUG(MTD_DEBUG_LEVEL1, 
169           "Failed to ioremap) memory region of size %ld at ABS_POS:%ld\n", 
170           (long)MTDRAM_TOTAL_SIZE, (long)CONFIG_MTDRAM_ABS_POS);
171     kfree(mtd_info);
172     mtd_info = NULL;
173     return -ENOMEM;
174   }
175   err = mtdram_init_device(mtd_info, addr, 
176                            MTDRAM_TOTAL_SIZE, "mtdram test device");
177   if (err) 
178   {
179     iounmap(addr);
180     kfree(mtd_info);
181     mtd_info = NULL;
182     return err;
183   }
184   memset(mtd_info->priv, 0xff, MTDRAM_TOTAL_SIZE);
185   return err;
186 }
187
188 #else /* CONFIG_MTDRAM_ABS_POS > 0 */
189
190 int __init init_mtdram(void)
191 {
192   void *addr;
193   int err;
194   /* Allocate some memory */
195    mtd_info = (struct mtd_info *)kmalloc(sizeof(struct mtd_info), GFP_KERNEL);
196    if (!mtd_info)
197      return -ENOMEM;
198
199   addr = vmalloc(MTDRAM_TOTAL_SIZE);
200   if (!addr) {
201     DEBUG(MTD_DEBUG_LEVEL1, 
202           "Failed to vmalloc memory region of size %ld\n", 
203           (long)MTDRAM_TOTAL_SIZE);
204     kfree(mtd_info);
205     mtd_info = NULL;
206     return -ENOMEM;
207   }
208   err = mtdram_init_device(mtd_info, addr, 
209                            MTDRAM_TOTAL_SIZE, "mtdram test device");
210   if (err) 
211   {
212     vfree(addr);
213     kfree(mtd_info);
214     mtd_info = NULL;
215     return err;
216   }
217   memset(mtd_info->priv, 0xff, MTDRAM_TOTAL_SIZE);
218   return err;
219 }
220 #endif /* !(CONFIG_MTDRAM_ABS_POS > 0) */
221
222 #else /* CONFIG_MTDRAM_TOTAL_SIZE > 0 */
223
224 int __init init_mtdram(void)
225 {
226   return 0;
227 }
228 #endif /* !(CONFIG_MTDRAM_TOTAL_SIZE > 0) */
229
230 module_init(init_mtdram);
231 module_exit(cleanup_mtdram);
232
233 MODULE_LICENSE("GPL");
234 MODULE_AUTHOR("Alexander Larsson <alexl@redhat.com>");
235 MODULE_DESCRIPTION("Simulated MTD driver for testing");
236