1 // SPDX-License-Identifier: GPL-2.0-only
2 /*======================================================================
4 This driver provides a method to access memory not used by the kernel
5 itself (i.e. if the kernel commandline mem=xxx is used). To actually
6 use slram at least mtdblock or mtdchar is required (for block or
7 character device access).
11 if compiled as loadable module:
12 modprobe slram map=<name>,<start>,<end/offset>
13 if statically linked into the kernel use the following kernel cmd.line
14 slram=<name>,<start>,<end/offset>
16 <name>: name of the device that will be listed in /proc/mtd
17 <start>: start of the memory region, decimal or hex (0xabcdef)
18 <end/offset>: end of the memory region. It's possible to use +0x1234
19 to specify the offset instead of the absolute address
22 With slram it's only possible to map a contiguous memory region. Therefore
23 if there's a device mapped somewhere in the region specified slram will
24 fail to load (see kernel log if modprobe fails).
28 Jochen Schaeuble <psionic@psionic.de>
30 ======================================================================*/
33 #include <linux/module.h>
34 #include <linux/uaccess.h>
35 #include <linux/types.h>
36 #include <linux/kernel.h>
37 #include <linux/ptrace.h>
38 #include <linux/slab.h>
39 #include <linux/string.h>
40 #include <linux/timer.h>
41 #include <linux/major.h>
43 #include <linux/ioctl.h>
44 #include <linux/init.h>
47 #include <linux/mtd/mtd.h>
49 #define SLRAM_MAX_DEVICES_PARAMS 6 /* 3 parameters / device */
50 #define SLRAM_BLK_SZ 0x4000
52 #define T(fmt, args...) printk(KERN_DEBUG fmt, ## args)
53 #define E(fmt, args...) printk(KERN_NOTICE fmt, ## args)
55 typedef struct slram_priv
{
60 typedef struct slram_mtd_list
{
61 struct mtd_info
*mtdinfo
;
62 struct slram_mtd_list
*next
;
66 static char *map
[SLRAM_MAX_DEVICES_PARAMS
];
68 module_param_array(map
, charp
, NULL
, 0);
69 MODULE_PARM_DESC(map
, "List of memory regions to map. \"map=<name>, <start>, <length / end>\"");
74 static slram_mtd_list_t
*slram_mtdlist
= NULL
;
76 static int slram_erase(struct mtd_info
*, struct erase_info
*);
77 static int slram_point(struct mtd_info
*, loff_t
, size_t, size_t *, void **,
79 static int slram_unpoint(struct mtd_info
*, loff_t
, size_t);
80 static int slram_read(struct mtd_info
*, loff_t
, size_t, size_t *, u_char
*);
81 static int slram_write(struct mtd_info
*, loff_t
, size_t, size_t *, const u_char
*);
83 static int slram_erase(struct mtd_info
*mtd
, struct erase_info
*instr
)
85 slram_priv_t
*priv
= mtd
->priv
;
87 memset(priv
->start
+ instr
->addr
, 0xff, instr
->len
);
92 static int slram_point(struct mtd_info
*mtd
, loff_t from
, size_t len
,
93 size_t *retlen
, void **virt
, resource_size_t
*phys
)
95 slram_priv_t
*priv
= mtd
->priv
;
97 *virt
= priv
->start
+ from
;
102 static int slram_unpoint(struct mtd_info
*mtd
, loff_t from
, size_t len
)
107 static int slram_read(struct mtd_info
*mtd
, loff_t from
, size_t len
,
108 size_t *retlen
, u_char
*buf
)
110 slram_priv_t
*priv
= mtd
->priv
;
112 memcpy(buf
, priv
->start
+ from
, len
);
117 static int slram_write(struct mtd_info
*mtd
, loff_t to
, size_t len
,
118 size_t *retlen
, const u_char
*buf
)
120 slram_priv_t
*priv
= mtd
->priv
;
122 memcpy(priv
->start
+ to
, buf
, len
);
127 /*====================================================================*/
129 static int register_device(char *name
, unsigned long start
, unsigned long length
)
131 slram_mtd_list_t
**curmtd
;
133 curmtd
= &slram_mtdlist
;
135 curmtd
= &(*curmtd
)->next
;
138 *curmtd
= kmalloc(sizeof(slram_mtd_list_t
), GFP_KERNEL
);
140 E("slram: Cannot allocate new MTD device.\n");
143 (*curmtd
)->mtdinfo
= kzalloc(sizeof(struct mtd_info
), GFP_KERNEL
);
144 (*curmtd
)->next
= NULL
;
146 if ((*curmtd
)->mtdinfo
) {
147 (*curmtd
)->mtdinfo
->priv
=
148 kzalloc(sizeof(slram_priv_t
), GFP_KERNEL
);
150 if (!(*curmtd
)->mtdinfo
->priv
) {
151 kfree((*curmtd
)->mtdinfo
);
152 (*curmtd
)->mtdinfo
= NULL
;
156 if (!(*curmtd
)->mtdinfo
) {
157 E("slram: Cannot allocate new MTD device.\n");
161 if (!(((slram_priv_t
*)(*curmtd
)->mtdinfo
->priv
)->start
=
162 memremap(start
, length
,
163 MEMREMAP_WB
| MEMREMAP_WT
| MEMREMAP_WC
))) {
164 E("slram: memremap failed\n");
167 ((slram_priv_t
*)(*curmtd
)->mtdinfo
->priv
)->end
=
168 ((slram_priv_t
*)(*curmtd
)->mtdinfo
->priv
)->start
+ length
;
171 (*curmtd
)->mtdinfo
->name
= name
;
172 (*curmtd
)->mtdinfo
->size
= length
;
173 (*curmtd
)->mtdinfo
->flags
= MTD_CAP_RAM
;
174 (*curmtd
)->mtdinfo
->_erase
= slram_erase
;
175 (*curmtd
)->mtdinfo
->_point
= slram_point
;
176 (*curmtd
)->mtdinfo
->_unpoint
= slram_unpoint
;
177 (*curmtd
)->mtdinfo
->_read
= slram_read
;
178 (*curmtd
)->mtdinfo
->_write
= slram_write
;
179 (*curmtd
)->mtdinfo
->owner
= THIS_MODULE
;
180 (*curmtd
)->mtdinfo
->type
= MTD_RAM
;
181 (*curmtd
)->mtdinfo
->erasesize
= SLRAM_BLK_SZ
;
182 (*curmtd
)->mtdinfo
->writesize
= 1;
184 if (mtd_device_register((*curmtd
)->mtdinfo
, NULL
, 0)) {
185 E("slram: Failed to register new device\n");
186 memunmap(((slram_priv_t
*)(*curmtd
)->mtdinfo
->priv
)->start
);
187 kfree((*curmtd
)->mtdinfo
->priv
);
188 kfree((*curmtd
)->mtdinfo
);
191 T("slram: Registered device %s from %luKiB to %luKiB\n", name
,
192 (start
/ 1024), ((start
+ length
) / 1024));
193 T("slram: Mapped from 0x%p to 0x%p\n",
194 ((slram_priv_t
*)(*curmtd
)->mtdinfo
->priv
)->start
,
195 ((slram_priv_t
*)(*curmtd
)->mtdinfo
->priv
)->end
);
199 static void unregister_devices(void)
201 slram_mtd_list_t
*nextitem
;
203 while (slram_mtdlist
) {
204 nextitem
= slram_mtdlist
->next
;
205 mtd_device_unregister(slram_mtdlist
->mtdinfo
);
206 memunmap(((slram_priv_t
*)slram_mtdlist
->mtdinfo
->priv
)->start
);
207 kfree(slram_mtdlist
->mtdinfo
->priv
);
208 kfree(slram_mtdlist
->mtdinfo
);
209 kfree(slram_mtdlist
);
210 slram_mtdlist
= nextitem
;
214 static unsigned long handle_unit(unsigned long value
, char *unit
)
216 if ((*unit
== 'M') || (*unit
== 'm')) {
217 return(value
* 1024 * 1024);
218 } else if ((*unit
== 'K') || (*unit
== 'k')) {
219 return(value
* 1024);
224 static int parse_cmdline(char *devname
, char *szstart
, char *szlength
)
227 unsigned long devstart
;
228 unsigned long devlength
;
230 if ((!devname
) || (!szstart
) || (!szlength
)) {
231 unregister_devices();
235 devstart
= simple_strtoul(szstart
, &buffer
, 0);
236 devstart
= handle_unit(devstart
, buffer
);
238 if (*(szlength
) != '+') {
239 devlength
= simple_strtoul(szlength
, &buffer
, 0);
240 devlength
= handle_unit(devlength
, buffer
);
241 if (devlength
< devstart
)
244 devlength
-= devstart
;
246 devlength
= simple_strtoul(szlength
+ 1, &buffer
, 0);
247 devlength
= handle_unit(devlength
, buffer
);
249 T("slram: devname=%s, devstart=0x%lx, devlength=0x%lx\n",
250 devname
, devstart
, devlength
);
251 if (devlength
% SLRAM_BLK_SZ
!= 0)
254 if ((devstart
= register_device(devname
, devstart
, devlength
))){
255 unregister_devices();
256 return((int)devstart
);
261 E("slram: Illegal length parameter.\n");
267 static int __init
mtd_slram_setup(char *str
)
273 __setup("slram=", mtd_slram_setup
);
277 static int __init
init_slram(void)
286 E("slram: not enough parameters.\n");
290 devname
= devstart
= devlength
= NULL
;
292 if (!(devname
= strsep(&map
, ","))) {
293 E("slram: No devicename specified.\n");
296 T("slram: devname = %s\n", devname
);
297 if ((!map
) || (!(devstart
= strsep(&map
, ",")))) {
298 E("slram: No devicestart specified.\n");
300 T("slram: devstart = %s\n", devstart
);
301 if ((!map
) || (!(devlength
= strsep(&map
, ",")))) {
302 E("slram: No devicelength / -end specified.\n");
304 T("slram: devlength = %s\n", devlength
);
305 if (parse_cmdline(devname
, devstart
, devlength
) != 0) {
313 for (count
= 0; count
< SLRAM_MAX_DEVICES_PARAMS
&& map
[count
];
317 if ((count
% 3 != 0) || (count
== 0)) {
318 E("slram: not enough parameters.\n");
321 for (i
= 0; i
< (count
/ 3); i
++) {
322 devname
= map
[i
* 3];
324 if (parse_cmdline(devname
, map
[i
* 3 + 1], map
[i
* 3 + 2])!=0) {
334 static void __exit
cleanup_slram(void)
336 unregister_devices();
339 module_init(init_slram
);
340 module_exit(cleanup_slram
);
342 MODULE_LICENSE("GPL");
343 MODULE_AUTHOR("Jochen Schaeuble <psionic@psionic.de>");
344 MODULE_DESCRIPTION("MTD driver for uncached system RAM");