mtdram.c 4.2 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5
/*
 * mtdram - a test mtd device
 * Author: Alexander Larsson <alex@cendio.se>
 *
 * Copyright (c) 1999 Alexander Larsson <alex@cendio.se>
6
 * Copyright (c) 2005 Joern Engel <joern@wh.fh-wedel.de>
L
Linus Torvalds 已提交
7 8 9 10 11 12 13 14 15
 *
 * This code is GPL
 *
 */

#include <linux/module.h>
#include <linux/slab.h>
#include <linux/ioport.h>
#include <linux/vmalloc.h>
16
#include <linux/mm.h>
L
Linus Torvalds 已提交
17 18
#include <linux/init.h>
#include <linux/mtd/mtd.h>
19
#include <linux/mtd/mtdram.h>
L
Linus Torvalds 已提交
20 21 22

static unsigned long total_size = CONFIG_MTDRAM_TOTAL_SIZE;
static unsigned long erase_size = CONFIG_MTDRAM_ERASE_SIZE;
23
static unsigned long writebuf_size = 64;
L
Linus Torvalds 已提交
24 25 26
#define MTDRAM_TOTAL_SIZE (total_size * 1024)
#define MTDRAM_ERASE_SIZE (erase_size * 1024)

27 28 29 30 31
#ifdef MODULE
module_param(total_size, ulong, 0);
MODULE_PARM_DESC(total_size, "Total device size in KiB");
module_param(erase_size, ulong, 0);
MODULE_PARM_DESC(erase_size, "Device erase block size in KiB");
32 33
module_param(writebuf_size, ulong, 0);
MODULE_PARM_DESC(writebuf_size, "Device write buf size in Bytes (Default: 64)");
34
#endif
L
Linus Torvalds 已提交
35 36 37 38

// We could store these in the mtd structure, but we only support 1 device..
static struct mtd_info *mtd_info;

39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
static int check_offs_len(struct mtd_info *mtd, loff_t ofs, uint64_t len)
{
	int ret = 0;

	/* Start address must align on block boundary */
	if (mtd_mod_by_eb(ofs, mtd)) {
		pr_debug("%s: unaligned address\n", __func__);
		ret = -EINVAL;
	}

	/* Length must align on block boundary */
	if (mtd_mod_by_eb(len, mtd)) {
		pr_debug("%s: length not block aligned\n", __func__);
		ret = -EINVAL;
	}

	return ret;
}

58
static int ram_erase(struct mtd_info *mtd, struct erase_info *instr)
L
Linus Torvalds 已提交
59
{
60 61
	if (check_offs_len(mtd, instr->addr, instr->len))
		return -EINVAL;
62
	memset((char *)mtd->priv + instr->addr, 0xff, instr->len);
63

64
	return 0;
L
Linus Torvalds 已提交
65 66
}

67
static int ram_point(struct mtd_info *mtd, loff_t from, size_t len,
68
		size_t *retlen, void **virt, resource_size_t *phys)
L
Linus Torvalds 已提交
69
{
70
	*virt = mtd->priv + from;
71
	*retlen = len;
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92

	if (phys) {
		/* limit retlen to the number of contiguous physical pages */
		unsigned long page_ofs = offset_in_page(*virt);
		void *addr = *virt - page_ofs;
		unsigned long pfn1, pfn0 = vmalloc_to_pfn(addr);

		*phys = __pfn_to_phys(pfn0) + page_ofs;
		len += page_ofs;
		while (len > PAGE_SIZE) {
			len -= PAGE_SIZE;
			addr += PAGE_SIZE;
			pfn0++;
			pfn1 = vmalloc_to_pfn(addr);
			if (pfn1 != pfn0) {
				*retlen = addr - *virt;
				break;
			}
		}
	}

93
	return 0;
L
Linus Torvalds 已提交
94 95
}

96
static int ram_unpoint(struct mtd_info *mtd, loff_t from, size_t len)
L
Linus Torvalds 已提交
97
{
98
	return 0;
L
Linus Torvalds 已提交
99 100 101
}

static int ram_read(struct mtd_info *mtd, loff_t from, size_t len,
102
		size_t *retlen, u_char *buf)
L
Linus Torvalds 已提交
103
{
104 105 106
	memcpy(buf, mtd->priv + from, len);
	*retlen = len;
	return 0;
L
Linus Torvalds 已提交
107 108 109
}

static int ram_write(struct mtd_info *mtd, loff_t to, size_t len,
110
		size_t *retlen, const u_char *buf)
L
Linus Torvalds 已提交
111
{
112 113 114
	memcpy((char *)mtd->priv + to, buf, len);
	*retlen = len;
	return 0;
L
Linus Torvalds 已提交
115 116 117 118
}

static void __exit cleanup_mtdram(void)
{
119
	if (mtd_info) {
120
		mtd_device_unregister(mtd_info);
121
		vfree(mtd_info->priv);
122 123
		kfree(mtd_info);
	}
L
Linus Torvalds 已提交
124 125
}

126
int mtdram_init_device(struct mtd_info *mtd, void *mapped_address,
B
Brian Norris 已提交
127
		unsigned long size, const char *name)
L
Linus Torvalds 已提交
128
{
129 130 131 132
	memset(mtd, 0, sizeof(*mtd));

	/* Setup the MTD structure */
	mtd->name = name;
133
	mtd->type = MTD_RAM;
134 135
	mtd->flags = MTD_CAP_RAM;
	mtd->size = size;
136
	mtd->writesize = 1;
137
	mtd->writebufsize = writebuf_size;
138 139 140 141
	mtd->erasesize = MTDRAM_ERASE_SIZE;
	mtd->priv = mapped_address;

	mtd->owner = THIS_MODULE;
142 143 144 145 146
	mtd->_erase = ram_erase;
	mtd->_point = ram_point;
	mtd->_unpoint = ram_unpoint;
	mtd->_read = ram_read;
	mtd->_write = ram_write;
147

148
	if (mtd_device_register(mtd, NULL, 0))
149 150 151
		return -EIO;

	return 0;
L
Linus Torvalds 已提交
152 153 154 155
}

static int __init init_mtdram(void)
{
156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181
	void *addr;
	int err;

	if (!total_size)
		return -EINVAL;

	/* Allocate some memory */
	mtd_info = kmalloc(sizeof(struct mtd_info), GFP_KERNEL);
	if (!mtd_info)
		return -ENOMEM;

	addr = vmalloc(MTDRAM_TOTAL_SIZE);
	if (!addr) {
		kfree(mtd_info);
		mtd_info = NULL;
		return -ENOMEM;
	}
	err = mtdram_init_device(mtd_info, addr, MTDRAM_TOTAL_SIZE, "mtdram test device");
	if (err) {
		vfree(addr);
		kfree(mtd_info);
		mtd_info = NULL;
		return err;
	}
	memset(mtd_info->priv, 0xff, MTDRAM_TOTAL_SIZE);
	return err;
L
Linus Torvalds 已提交
182 183 184 185 186 187 188 189
}

module_init(init_mtdram);
module_exit(cleanup_mtdram);

MODULE_LICENSE("GPL");
MODULE_AUTHOR("Alexander Larsson <alexl@redhat.com>");
MODULE_DESCRIPTION("Simulated MTD driver for testing");