xref: /openbmc/linux/drivers/mtd/devices/phram.c (revision 9ac8d3fb)
1 /**
2  * Copyright (c) ????		Jochen Schäuble <psionic@psionic.de>
3  * Copyright (c) 2003-2004	Joern Engel <joern@wh.fh-wedel.de>
4  *
5  * Usage:
6  *
7  * one commend line parameter per device, each in the form:
8  *   phram=<name>,<start>,<len>
9  * <name> may be up to 63 characters.
10  * <start> and <len> can be octal, decimal or hexadecimal.  If followed
11  * by "ki", "Mi" or "Gi", the numbers will be interpreted as kilo, mega or
12  * gigabytes.
13  *
14  * Example:
15  *	phram=swap,64Mi,128Mi phram=test,900Mi,1Mi
16  */
17 #include <asm/io.h>
18 #include <linux/init.h>
19 #include <linux/kernel.h>
20 #include <linux/list.h>
21 #include <linux/module.h>
22 #include <linux/moduleparam.h>
23 #include <linux/slab.h>
24 #include <linux/mtd/mtd.h>
25 
26 #define ERROR(fmt, args...) printk(KERN_ERR "phram: " fmt , ## args)
27 
28 struct phram_mtd_list {
29 	struct mtd_info mtd;
30 	struct list_head list;
31 };
32 
33 static LIST_HEAD(phram_list);
34 
35 
36 static int phram_erase(struct mtd_info *mtd, struct erase_info *instr)
37 {
38 	u_char *start = mtd->priv;
39 
40 	if (instr->addr + instr->len > mtd->size)
41 		return -EINVAL;
42 
43 	memset(start + instr->addr, 0xff, instr->len);
44 
45 	/* This'll catch a few races. Free the thing before returning :)
46 	 * I don't feel at all ashamed. This kind of thing is possible anyway
47 	 * with flash, but unlikely.
48 	 */
49 
50 	instr->state = MTD_ERASE_DONE;
51 
52 	mtd_erase_callback(instr);
53 
54 	return 0;
55 }
56 
57 static int phram_point(struct mtd_info *mtd, loff_t from, size_t len,
58 		size_t *retlen, void **virt, resource_size_t *phys)
59 {
60 	if (from + len > mtd->size)
61 		return -EINVAL;
62 
63 	/* can we return a physical address with this driver? */
64 	if (phys)
65 		return -EINVAL;
66 
67 	*virt = mtd->priv + from;
68 	*retlen = len;
69 	return 0;
70 }
71 
72 static void phram_unpoint(struct mtd_info *mtd, loff_t from, size_t len)
73 {
74 }
75 
76 static int phram_read(struct mtd_info *mtd, loff_t from, size_t len,
77 		size_t *retlen, u_char *buf)
78 {
79 	u_char *start = mtd->priv;
80 
81 	if (from >= mtd->size)
82 		return -EINVAL;
83 
84 	if (len > mtd->size - from)
85 		len = mtd->size - from;
86 
87 	memcpy(buf, start + from, len);
88 
89 	*retlen = len;
90 	return 0;
91 }
92 
93 static int phram_write(struct mtd_info *mtd, loff_t to, size_t len,
94 		size_t *retlen, const u_char *buf)
95 {
96 	u_char *start = mtd->priv;
97 
98 	if (to >= mtd->size)
99 		return -EINVAL;
100 
101 	if (len > mtd->size - to)
102 		len = mtd->size - to;
103 
104 	memcpy(start + to, buf, len);
105 
106 	*retlen = len;
107 	return 0;
108 }
109 
110 
111 
112 static void unregister_devices(void)
113 {
114 	struct phram_mtd_list *this, *safe;
115 
116 	list_for_each_entry_safe(this, safe, &phram_list, list) {
117 		del_mtd_device(&this->mtd);
118 		iounmap(this->mtd.priv);
119 		kfree(this);
120 	}
121 }
122 
123 static int register_device(char *name, unsigned long start, unsigned long len)
124 {
125 	struct phram_mtd_list *new;
126 	int ret = -ENOMEM;
127 
128 	new = kzalloc(sizeof(*new), GFP_KERNEL);
129 	if (!new)
130 		goto out0;
131 
132 	ret = -EIO;
133 	new->mtd.priv = ioremap(start, len);
134 	if (!new->mtd.priv) {
135 		ERROR("ioremap failed\n");
136 		goto out1;
137 	}
138 
139 
140 	new->mtd.name = name;
141 	new->mtd.size = len;
142 	new->mtd.flags = MTD_CAP_RAM;
143         new->mtd.erase = phram_erase;
144 	new->mtd.point = phram_point;
145 	new->mtd.unpoint = phram_unpoint;
146 	new->mtd.read = phram_read;
147 	new->mtd.write = phram_write;
148 	new->mtd.owner = THIS_MODULE;
149 	new->mtd.type = MTD_RAM;
150 	new->mtd.erasesize = PAGE_SIZE;
151 	new->mtd.writesize = 1;
152 
153 	ret = -EAGAIN;
154 	if (add_mtd_device(&new->mtd)) {
155 		ERROR("Failed to register new device\n");
156 		goto out2;
157 	}
158 
159 	list_add_tail(&new->list, &phram_list);
160 	return 0;
161 
162 out2:
163 	iounmap(new->mtd.priv);
164 out1:
165 	kfree(new);
166 out0:
167 	return ret;
168 }
169 
170 static int ustrtoul(const char *cp, char **endp, unsigned int base)
171 {
172 	unsigned long result = simple_strtoul(cp, endp, base);
173 
174 	switch (**endp) {
175 	case 'G':
176 		result *= 1024;
177 	case 'M':
178 		result *= 1024;
179 	case 'k':
180 		result *= 1024;
181 	/* By dwmw2 editorial decree, "ki", "Mi" or "Gi" are to be used. */
182 		if ((*endp)[1] == 'i')
183 			(*endp) += 2;
184 	}
185 	return result;
186 }
187 
188 static int parse_num32(uint32_t *num32, const char *token)
189 {
190 	char *endp;
191 	unsigned long n;
192 
193 	n = ustrtoul(token, &endp, 0);
194 	if (*endp)
195 		return -EINVAL;
196 
197 	*num32 = n;
198 	return 0;
199 }
200 
201 static int parse_name(char **pname, const char *token)
202 {
203 	size_t len;
204 	char *name;
205 
206 	len = strlen(token) + 1;
207 	if (len > 64)
208 		return -ENOSPC;
209 
210 	name = kmalloc(len, GFP_KERNEL);
211 	if (!name)
212 		return -ENOMEM;
213 
214 	strcpy(name, token);
215 
216 	*pname = name;
217 	return 0;
218 }
219 
220 
221 static inline void kill_final_newline(char *str)
222 {
223 	char *newline = strrchr(str, '\n');
224 	if (newline && !newline[1])
225 		*newline = 0;
226 }
227 
228 
229 #define parse_err(fmt, args...) do {	\
230 	ERROR(fmt , ## args);	\
231 	return 0;		\
232 } while (0)
233 
234 static int phram_setup(const char *val, struct kernel_param *kp)
235 {
236 	char buf[64+12+12], *str = buf;
237 	char *token[3];
238 	char *name;
239 	uint32_t start;
240 	uint32_t len;
241 	int i, ret;
242 
243 	if (strnlen(val, sizeof(buf)) >= sizeof(buf))
244 		parse_err("parameter too long\n");
245 
246 	strcpy(str, val);
247 	kill_final_newline(str);
248 
249 	for (i=0; i<3; i++)
250 		token[i] = strsep(&str, ",");
251 
252 	if (str)
253 		parse_err("too many arguments\n");
254 
255 	if (!token[2])
256 		parse_err("not enough arguments\n");
257 
258 	ret = parse_name(&name, token[0]);
259 	if (ret == -ENOMEM)
260 		parse_err("out of memory\n");
261 	if (ret == -ENOSPC)
262 		parse_err("name too long\n");
263 	if (ret)
264 		return 0;
265 
266 	ret = parse_num32(&start, token[1]);
267 	if (ret) {
268 		kfree(name);
269 		parse_err("illegal start address\n");
270 	}
271 
272 	ret = parse_num32(&len, token[2]);
273 	if (ret) {
274 		kfree(name);
275 		parse_err("illegal device length\n");
276 	}
277 
278 	register_device(name, start, len);
279 
280 	return 0;
281 }
282 
283 module_param_call(phram, phram_setup, NULL, NULL, 000);
284 MODULE_PARM_DESC(phram, "Memory region to map. \"phram=<name>,<start>,<length>\"");
285 
286 
287 static int __init init_phram(void)
288 {
289 	return 0;
290 }
291 
292 static void __exit cleanup_phram(void)
293 {
294 	unregister_devices();
295 }
296 
297 module_init(init_phram);
298 module_exit(cleanup_phram);
299 
300 MODULE_LICENSE("GPL");
301 MODULE_AUTHOR("Joern Engel <joern@wh.fh-wedel.de>");
302 MODULE_DESCRIPTION("MTD driver for physical RAM");
303