slram.c 9.1 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/*======================================================================

  This driver provides a method to access memory not used by the kernel
  itself (i.e. if the kernel commandline mem=xxx is used). To actually
  use slram at least mtdblock or mtdchar is required (for block or
  character device access).

  Usage:

  if compiled as loadable module:
    modprobe slram map=<name>,<start>,<end/offset>
  if statically linked into the kernel use the following kernel cmd.line
    slram=<name>,<start>,<end/offset>

  <name>: name of the device that will be listed in /proc/mtd
  <start>: start of the memory region, decimal or hex (0xabcdef)
  <end/offset>: end of the memory region. It's possible to use +0x1234
                to specify the offset instead of the absolute address
19

L
Linus Torvalds 已提交
20
  NOTE:
21
  With slram it's only possible to map a contiguous memory region. Therefore
L
Linus Torvalds 已提交
22 23 24 25
  if there's a device mapped somewhere in the region specified slram will
  fail to load (see kernel log if modprobe fails).

  -
26

L
Linus Torvalds 已提交
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
  Jochen Schaeuble <psionic@psionic.de>

======================================================================*/


#include <linux/module.h>
#include <asm/uaccess.h>
#include <linux/types.h>
#include <linux/kernel.h>
#include <linux/ptrace.h>
#include <linux/slab.h>
#include <linux/string.h>
#include <linux/timer.h>
#include <linux/major.h>
#include <linux/fs.h>
#include <linux/ioctl.h>
#include <linux/init.h>
#include <asm/io.h>
#include <asm/system.h>

#include <linux/mtd/mtd.h>

#define SLRAM_MAX_DEVICES_PARAMS 6		/* 3 parameters / device */
J
Josh Boyer 已提交
50
#define SLRAM_BLK_SZ 0x4000
L
Linus Torvalds 已提交
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76

#define T(fmt, args...) printk(KERN_DEBUG fmt, ## args)
#define E(fmt, args...) printk(KERN_NOTICE fmt, ## args)

typedef struct slram_priv {
	u_char *start;
	u_char *end;
} slram_priv_t;

typedef struct slram_mtd_list {
	struct mtd_info *mtdinfo;
	struct slram_mtd_list *next;
} slram_mtd_list_t;

#ifdef MODULE
static char *map[SLRAM_MAX_DEVICES_PARAMS];

module_param_array(map, charp, NULL, 0);
MODULE_PARM_DESC(map, "List of memory regions to map. \"map=<name>, <start>, <length / end>\"");
#else
static char *map;
#endif

static slram_mtd_list_t *slram_mtdlist = NULL;

static int slram_erase(struct mtd_info *, struct erase_info *);
77 78 79
static int slram_point(struct mtd_info *, loff_t, size_t, size_t *, void **,
		resource_size_t *);
static void slram_unpoint(struct mtd_info *, loff_t, size_t);
L
Linus Torvalds 已提交
80 81 82 83 84 85 86 87 88 89
static int slram_read(struct mtd_info *, loff_t, size_t, size_t *, u_char *);
static int slram_write(struct mtd_info *, loff_t, size_t, size_t *, const u_char *);

static int slram_erase(struct mtd_info *mtd, struct erase_info *instr)
{
	slram_priv_t *priv = mtd->priv;

	if (instr->addr + instr->len > mtd->size) {
		return(-EINVAL);
	}
90

L
Linus Torvalds 已提交
91 92
	memset(priv->start + instr->addr, 0xff, instr->len);

93
	/* This'll catch a few races. Free the thing before returning :)
L
Linus Torvalds 已提交
94 95 96 97 98 99 100 101 102 103 104 105
	 * I don't feel at all ashamed. This kind of thing is possible anyway
	 * with flash, but unlikely.
	 */

	instr->state = MTD_ERASE_DONE;

	mtd_erase_callback(instr);

	return(0);
}

static int slram_point(struct mtd_info *mtd, loff_t from, size_t len,
106
		size_t *retlen, void **virt, resource_size_t *phys)
L
Linus Torvalds 已提交
107 108 109
{
	slram_priv_t *priv = mtd->priv;

110 111 112 113
	/* can we return a physical address with this driver? */
	if (phys)
		return -EINVAL;

J
Josh Boyer 已提交
114 115 116
	if (from + len > mtd->size)
		return -EINVAL;

117
	*virt = priv->start + from;
L
Linus Torvalds 已提交
118 119 120 121
	*retlen = len;
	return(0);
}

122
static void slram_unpoint(struct mtd_info *mtd, loff_t from, size_t len)
L
Linus Torvalds 已提交
123 124 125 126 127 128 129
{
}

static int slram_read(struct mtd_info *mtd, loff_t from, size_t len,
		size_t *retlen, u_char *buf)
{
	slram_priv_t *priv = mtd->priv;
J
Josh Boyer 已提交
130 131 132 133 134 135 136

	if (from > mtd->size)
		return -EINVAL;

	if (from + len > mtd->size)
		len = mtd->size - from;

L
Linus Torvalds 已提交
137 138 139 140 141 142 143 144 145 146 147
	memcpy(buf, priv->start + from, len);

	*retlen = len;
	return(0);
}

static int slram_write(struct mtd_info *mtd, loff_t to, size_t len,
		size_t *retlen, const u_char *buf)
{
	slram_priv_t *priv = mtd->priv;

J
Josh Boyer 已提交
148 149 150
	if (to + len > mtd->size)
		return -EINVAL;

L
Linus Torvalds 已提交
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172
	memcpy(priv->start + to, buf, len);

	*retlen = len;
	return(0);
}

/*====================================================================*/

static int register_device(char *name, unsigned long start, unsigned long length)
{
	slram_mtd_list_t **curmtd;

	curmtd = &slram_mtdlist;
	while (*curmtd) {
		curmtd = &(*curmtd)->next;
	}

	*curmtd = kmalloc(sizeof(slram_mtd_list_t), GFP_KERNEL);
	if (!(*curmtd)) {
		E("slram: Cannot allocate new MTD device.\n");
		return(-ENOMEM);
	}
173
	(*curmtd)->mtdinfo = kzalloc(sizeof(struct mtd_info), GFP_KERNEL);
L
Linus Torvalds 已提交
174
	(*curmtd)->next = NULL;
175

L
Linus Torvalds 已提交
176 177
	if ((*curmtd)->mtdinfo)	{
		(*curmtd)->mtdinfo->priv =
178
			kzalloc(sizeof(slram_priv_t), GFP_KERNEL);
179

L
Linus Torvalds 已提交
180 181 182 183 184 185 186 187 188 189
		if (!(*curmtd)->mtdinfo->priv) {
			kfree((*curmtd)->mtdinfo);
			(*curmtd)->mtdinfo = NULL;
		}
	}

	if (!(*curmtd)->mtdinfo) {
		E("slram: Cannot allocate new MTD device.\n");
		return(-ENOMEM);
	}
190

L
Linus Torvalds 已提交
191 192 193 194 195 196 197 198 199 200 201
	if (!(((slram_priv_t *)(*curmtd)->mtdinfo->priv)->start =
				ioremap(start, length))) {
		E("slram: ioremap failed\n");
		return -EIO;
	}
	((slram_priv_t *)(*curmtd)->mtdinfo->priv)->end =
		((slram_priv_t *)(*curmtd)->mtdinfo->priv)->start + length;


	(*curmtd)->mtdinfo->name = name;
	(*curmtd)->mtdinfo->size = length;
J
Jörn Engel 已提交
202
	(*curmtd)->mtdinfo->flags = MTD_CAP_RAM;
L
Linus Torvalds 已提交
203 204 205 206 207 208
        (*curmtd)->mtdinfo->erase = slram_erase;
	(*curmtd)->mtdinfo->point = slram_point;
	(*curmtd)->mtdinfo->unpoint = slram_unpoint;
	(*curmtd)->mtdinfo->read = slram_read;
	(*curmtd)->mtdinfo->write = slram_write;
	(*curmtd)->mtdinfo->owner = THIS_MODULE;
209
	(*curmtd)->mtdinfo->type = MTD_RAM;
J
Josh Boyer 已提交
210
	(*curmtd)->mtdinfo->erasesize = SLRAM_BLK_SZ;
211
	(*curmtd)->mtdinfo->writesize = 1;
L
Linus Torvalds 已提交
212

213
	if (mtd_device_register((*curmtd)->mtdinfo, NULL, 0))	{
L
Linus Torvalds 已提交
214 215 216 217 218 219 220 221 222 223 224
		E("slram: Failed to register new device\n");
		iounmap(((slram_priv_t *)(*curmtd)->mtdinfo->priv)->start);
		kfree((*curmtd)->mtdinfo->priv);
		kfree((*curmtd)->mtdinfo);
		return(-EAGAIN);
	}
	T("slram: Registered device %s from %luKiB to %luKiB\n", name,
			(start / 1024), ((start + length) / 1024));
	T("slram: Mapped from 0x%p to 0x%p\n",
			((slram_priv_t *)(*curmtd)->mtdinfo->priv)->start,
			((slram_priv_t *)(*curmtd)->mtdinfo->priv)->end);
225
	return(0);
L
Linus Torvalds 已提交
226 227 228 229 230 231 232 233
}

static void unregister_devices(void)
{
	slram_mtd_list_t *nextitem;

	while (slram_mtdlist) {
		nextitem = slram_mtdlist->next;
234
		mtd_device_unregister(slram_mtdlist->mtdinfo);
L
Linus Torvalds 已提交
235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257
		iounmap(((slram_priv_t *)slram_mtdlist->mtdinfo->priv)->start);
		kfree(slram_mtdlist->mtdinfo->priv);
		kfree(slram_mtdlist->mtdinfo);
		kfree(slram_mtdlist);
		slram_mtdlist = nextitem;
	}
}

static unsigned long handle_unit(unsigned long value, char *unit)
{
	if ((*unit == 'M') || (*unit == 'm')) {
		return(value * 1024 * 1024);
	} else if ((*unit == 'K') || (*unit == 'k')) {
		return(value * 1024);
	}
	return(value);
}

static int parse_cmdline(char *devname, char *szstart, char *szlength)
{
	char *buffer;
	unsigned long devstart;
	unsigned long devlength;
258

L
Linus Torvalds 已提交
259 260 261 262 263 264 265
	if ((!devname) || (!szstart) || (!szlength)) {
		unregister_devices();
		return(-EINVAL);
	}

	devstart = simple_strtoul(szstart, &buffer, 0);
	devstart = handle_unit(devstart, buffer);
266

L
Linus Torvalds 已提交
267 268 269
	if (*(szlength) != '+') {
		devlength = simple_strtoul(szlength, &buffer, 0);
		devlength = handle_unit(devlength, buffer) - devstart;
270 271 272 273
		if (devlength < devstart)
			goto err_out;

		devlength -= devstart;
L
Linus Torvalds 已提交
274 275 276 277 278 279
	} else {
		devlength = simple_strtoul(szlength + 1, &buffer, 0);
		devlength = handle_unit(devlength, buffer);
	}
	T("slram: devname=%s, devstart=0x%lx, devlength=0x%lx\n",
			devname, devstart, devlength);
280 281
	if (devlength % SLRAM_BLK_SZ != 0)
		goto err_out;
282

L
Linus Torvalds 已提交
283 284 285 286 287
	if ((devstart = register_device(devname, devstart, devlength))){
		unregister_devices();
		return((int)devstart);
	}
	return(0);
288 289 290 291

err_out:
	E("slram: Illegal length parameter.\n");
	return(-EINVAL);
L
Linus Torvalds 已提交
292 293 294 295 296 297 298 299 300 301 302 303 304 305
}

#ifndef MODULE

static int __init mtd_slram_setup(char *str)
{
	map = str;
	return(1);
}

__setup("slram=", mtd_slram_setup);

#endif

306
static int __init init_slram(void)
L
Linus Torvalds 已提交
307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342
{
	char *devname;
	int i;

#ifndef MODULE
	char *devstart;
	char *devlength;

	i = 0;

	if (!map) {
		E("slram: not enough parameters.\n");
		return(-EINVAL);
	}
	while (map) {
		devname = devstart = devlength = NULL;

		if (!(devname = strsep(&map, ","))) {
			E("slram: No devicename specified.\n");
			break;
		}
		T("slram: devname = %s\n", devname);
		if ((!map) || (!(devstart = strsep(&map, ",")))) {
			E("slram: No devicestart specified.\n");
		}
		T("slram: devstart = %s\n", devstart);
		if ((!map) || (!(devlength = strsep(&map, ",")))) {
			E("slram: No devicelength / -end specified.\n");
		}
		T("slram: devlength = %s\n", devlength);
		if (parse_cmdline(devname, devstart, devlength) != 0) {
			return(-EINVAL);
		}
	}
#else
	int count;
343

R
Roel Kluin 已提交
344
	for (count = 0; count < SLRAM_MAX_DEVICES_PARAMS && map[count];
L
Linus Torvalds 已提交
345 346 347 348 349 350 351 352 353 354 355 356 357
			count++) {
	}

	if ((count % 3 != 0) || (count == 0)) {
		E("slram: not enough parameters.\n");
		return(-EINVAL);
	}
	for (i = 0; i < (count / 3); i++) {
		devname = map[i * 3];

		if (parse_cmdline(devname, map[i * 3 + 1], map[i * 3 + 2])!=0) {
			return(-EINVAL);
		}
358

L
Linus Torvalds 已提交
359 360
	}
#endif /* !MODULE */
361

L
Linus Torvalds 已提交
362 363 364 365 366 367 368 369 370 371 372 373 374 375
	return(0);
}

static void __exit cleanup_slram(void)
{
	unregister_devices();
}

module_init(init_slram);
module_exit(cleanup_slram);

MODULE_LICENSE("GPL");
MODULE_AUTHOR("Jochen Schaeuble <psionic@psionic.de>");
MODULE_DESCRIPTION("MTD driver for uncached system RAM");