1 /*
2  * mtdram - a test mtd device
3  * Author: Alexander Larsson <alex@cendio.se>
4  *
5  * Copyright (c) 1999 Alexander Larsson <alex@cendio.se>
6  * Copyright (c) 2005 Joern Engel <joern@wh.fh-wedel.de>
7  *
8  * This code is GPL
9  *
10  */
11 
12 #include <linux/module.h>
13 #include <linux/slab.h>
14 #include <linux/ioport.h>
15 #include <linux/vmalloc.h>
16 #include <linux/init.h>
17 #include <linux/mtd/mtd.h>
18 #include <linux/mtd/mtdram.h>
19 
20 static unsigned long total_size = CONFIG_MTDRAM_TOTAL_SIZE;
21 static unsigned long erase_size = CONFIG_MTDRAM_ERASE_SIZE;
22 #define MTDRAM_TOTAL_SIZE (total_size * 1024)
23 #define MTDRAM_ERASE_SIZE (erase_size * 1024)
24 
25 #ifdef MODULE
26 module_param(total_size, ulong, 0);
27 MODULE_PARM_DESC(total_size, "Total device size in KiB");
28 module_param(erase_size, ulong, 0);
29 MODULE_PARM_DESC(erase_size, "Device erase block size in KiB");
30 #endif
31 
32 // We could store these in the mtd structure, but we only support 1 device..
33 static struct mtd_info *mtd_info;
34 
ram_erase(struct mtd_info * mtd,struct erase_info * instr)35 static int ram_erase(struct mtd_info *mtd, struct erase_info *instr)
36 {
37 	if (instr->addr + instr->len > mtd->size)
38 		return -EINVAL;
39 
40 	memset((char *)mtd->priv + instr->addr, 0xff, instr->len);
41 
42 	instr->state = MTD_ERASE_DONE;
43 	mtd_erase_callback(instr);
44 
45 	return 0;
46 }
47 
ram_point(struct mtd_info * mtd,loff_t from,size_t len,size_t * retlen,void ** virt,resource_size_t * phys)48 static int ram_point(struct mtd_info *mtd, loff_t from, size_t len,
49 		size_t *retlen, void **virt, resource_size_t *phys)
50 {
51 	if (from + len > mtd->size)
52 		return -EINVAL;
53 
54 	/* can we return a physical address with this driver? */
55 	if (phys)
56 		return -EINVAL;
57 
58 	*virt = mtd->priv + from;
59 	*retlen = len;
60 	return 0;
61 }
62 
ram_unpoint(struct mtd_info * mtd,loff_t from,size_t len)63 static void ram_unpoint(struct mtd_info *mtd, loff_t from, size_t len)
64 {
65 }
66 
67 /*
68  * Allow NOMMU mmap() to directly map the device (if not NULL)
69  * - return the address to which the offset maps
70  * - return -ENOSYS to indicate refusal to do the mapping
71  */
ram_get_unmapped_area(struct mtd_info * mtd,unsigned long len,unsigned long offset,unsigned long flags)72 static unsigned long ram_get_unmapped_area(struct mtd_info *mtd,
73 					   unsigned long len,
74 					   unsigned long offset,
75 					   unsigned long flags)
76 {
77 	return (unsigned long) mtd->priv + offset;
78 }
79 
ram_read(struct mtd_info * mtd,loff_t from,size_t len,size_t * retlen,u_char * buf)80 static int ram_read(struct mtd_info *mtd, loff_t from, size_t len,
81 		size_t *retlen, u_char *buf)
82 {
83 	if (from + len > mtd->size)
84 		return -EINVAL;
85 
86 	memcpy(buf, mtd->priv + from, len);
87 
88 	*retlen = len;
89 	return 0;
90 }
91 
ram_write(struct mtd_info * mtd,loff_t to,size_t len,size_t * retlen,const u_char * buf)92 static int ram_write(struct mtd_info *mtd, loff_t to, size_t len,
93 		size_t *retlen, const u_char *buf)
94 {
95 	if (to + len > mtd->size)
96 		return -EINVAL;
97 
98 	memcpy((char *)mtd->priv + to, buf, len);
99 
100 	*retlen = len;
101 	return 0;
102 }
103 
cleanup_mtdram(void)104 static void __exit cleanup_mtdram(void)
105 {
106 	if (mtd_info) {
107 		del_mtd_device(mtd_info);
108 		vfree(mtd_info->priv);
109 		kfree(mtd_info);
110 	}
111 }
112 
mtdram_init_device(struct mtd_info * mtd,void * mapped_address,unsigned long size,char * name)113 int mtdram_init_device(struct mtd_info *mtd, void *mapped_address,
114 		unsigned long size, char *name)
115 {
116 	memset(mtd, 0, sizeof(*mtd));
117 
118 	/* Setup the MTD structure */
119 	mtd->name = name;
120 	mtd->type = MTD_RAM;
121 	mtd->flags = MTD_CAP_RAM;
122 	mtd->size = size;
123 	mtd->writesize = 1;
124 	mtd->writebufsize = 64; /* Mimic CFI NOR flashes */
125 	mtd->erasesize = MTDRAM_ERASE_SIZE;
126 	mtd->priv = mapped_address;
127 
128 	mtd->owner = THIS_MODULE;
129 	mtd->erase = ram_erase;
130 	mtd->point = ram_point;
131 	mtd->unpoint = ram_unpoint;
132 	mtd->get_unmapped_area = ram_get_unmapped_area;
133 	mtd->read = ram_read;
134 	mtd->write = ram_write;
135 
136 	if (add_mtd_device(mtd)) {
137 		return -EIO;
138 	}
139 
140 	return 0;
141 }
142 
init_mtdram(void)143 static int __init init_mtdram(void)
144 {
145 	void *addr;
146 	int err;
147 
148 	if (!total_size)
149 		return -EINVAL;
150 
151 	/* Allocate some memory */
152 	mtd_info = kmalloc(sizeof(struct mtd_info), GFP_KERNEL);
153 	if (!mtd_info)
154 		return -ENOMEM;
155 
156 	addr = vmalloc(MTDRAM_TOTAL_SIZE);
157 	if (!addr) {
158 		kfree(mtd_info);
159 		mtd_info = NULL;
160 		return -ENOMEM;
161 	}
162 	err = mtdram_init_device(mtd_info, addr, MTDRAM_TOTAL_SIZE, "mtdram test device");
163 	if (err) {
164 		vfree(addr);
165 		kfree(mtd_info);
166 		mtd_info = NULL;
167 		return err;
168 	}
169 	memset(mtd_info->priv, 0xff, MTDRAM_TOTAL_SIZE);
170 	return err;
171 }
172 
173 module_init(init_mtdram);
174 module_exit(cleanup_mtdram);
175 
176 MODULE_LICENSE("GPL");
177 MODULE_AUTHOR("Alexander Larsson <alexl@redhat.com>");
178 MODULE_DESCRIPTION("Simulated MTD driver for testing");
179