summaryrefslogtreecommitdiffstats
path: root/drivers/mtd/slram.c
blob: d2e4f8ed294c5b74e06dcbcb178c5a991f589b0d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
/*======================================================================

  $Id: slram.c,v 1.10 2000/07/03 10:01:38 dwmw2 Exp $

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


#include <linux/module.h>
#include <asm/uaccess.h>
#include <linux/types.h>
#include <linux/kernel.h>
#include <linux/sched.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 <asm/segment.h>
#include <stdarg.h>

#include <linux/mtd/mtd.h>

struct mypriv {
	u_char *start;
	u_char *end;
};

int physmem_erase (struct mtd_info *mtd, struct erase_info *instr);
int physmem_point (struct mtd_info *mtd, loff_t from, size_t len, size_t *retlen, u_char **mtdbuf);
void physmem_unpoint (struct mtd_info *mtd, u_char *addr);
int physmem_read (struct mtd_info *mtd, loff_t from, size_t len, size_t *retlen, u_char *buf);
int physmem_write (struct mtd_info *mtd, loff_t to, size_t len, size_t *retlen, const u_char *buf);
	

int physmem_erase (struct mtd_info *mtd, struct erase_info *instr)
{
	struct mypriv *priv = mtd->priv;

	if (instr->addr + instr->len > mtd->size)
		return -EINVAL;
	
	memset(priv->start + instr->addr, 0xff, instr->len);

	/* This'll catch a few races. Free the thing before returning :) 
	 * I don't feel at all ashamed. This kind of thing is possible anyway
	 * with flash, but unlikely.
	 */

	instr->state = MTD_ERASE_DONE;

	if (instr->callback)
		(*(instr->callback))(instr);
	else
		kfree(instr);

	return 0;
}


int physmem_point (struct mtd_info *mtd, loff_t from, size_t len, size_t *retlen, u_char **mtdbuf)
{
	struct mypriv *priv = (struct mypriv *)mtd->priv;

	*mtdbuf = priv->start + from;
	*retlen = len;
	return 0;
}

void physmem_unpoint (struct mtd_info *mtd, u_char *addr)
{
}

int physmem_read (struct mtd_info *mtd, loff_t from, size_t len, size_t *retlen, u_char *buf)
{
	struct mypriv *priv = (struct mypriv *)mtd->priv;
	
	memcpy (buf, priv->start + from, len);

	*retlen=len;
	return 0;
}

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

	memcpy (priv->start + to, buf, len);

	*retlen=len;
	return 0;
}




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

/* Place your defaults here */

static u_long start = 100663296;
static u_long length = 33554432;
static u_long end = 0;

#if LINUX_VERSION_CODE < 0x20300
#ifdef MODULE
#define init_slram init_module
#define cleanup_slram cleanup_module
#endif
#define __exit
#endif

#ifdef MODULE
MODULE_PARM(start,"l");
MODULE_PARM(length,"l");
MODULE_PARM(end,"l");
#endif

struct mtd_info *mymtd;

void __init mtd_slram_setup(char *str, int *ints)
{
	if (ints[0] > 0)
		start=ints[1];
	if (ints[0] > 1)
		length=ints[2];
}

int init_slram(void)
{
	if (!start)
	{
		printk(KERN_NOTICE "physmem: No start address for memory device.\n");
		return -EINVAL;
	}
	
	if (!length && !end)
	{
		printk(KERN_NOTICE "physmem: No length or endpointer given.\n");
		return -EINVAL;
	}
	
	if (!end)
		end = start + length;
	
	if (!length)
	  length = end - start;

	if (start + length != end)
	  {
	    printk(KERN_NOTICE "physmem: start(%lx) + length(%lx) != end(%lx) !\n",
		   start, length, end);
	    return -EINVAL;
	  }

	mymtd = kmalloc(sizeof(struct mtd_info), GFP_KERNEL);
	
	memset(mymtd, 0, sizeof(*mymtd));
	
	if (mymtd)
	{
		memset((char *)mymtd, 0, sizeof(struct mtd_info));
		mymtd->priv = (void *) kmalloc (sizeof(struct mypriv), GFP_KERNEL);
		if (!mymtd->priv)
		{
			kfree(mymtd);
			mymtd = NULL;
		}
		memset(mymtd->priv, 0, sizeof(struct mypriv));
	}

	if (!mymtd)
	{
		printk(KERN_NOTICE "physmem: Cannot allocate new MTD device.\n");
		return -ENOMEM;
	}
	
	
	((struct mypriv *)mymtd->priv)->start = ioremap(start, length);
	((struct mypriv *)mymtd->priv)->end = ((struct mypriv *)mymtd->priv)->start + length;


	mymtd->name = "Raw memory";

	mymtd->size = length;
	mymtd->flags = MTD_CLEAR_BITS | MTD_SET_BITS | MTD_WRITEB_WRITEABLE | MTD_VOLATILE;
        mymtd->erase = physmem_erase;
	mymtd->point = physmem_point;
	mymtd->unpoint = physmem_unpoint;
	mymtd->read = physmem_read;
	mymtd->write = physmem_write;
	mymtd->module = THIS_MODULE;
	mymtd->type = MTD_RAM;
	mymtd->erasesize = 0x10000;

	if (add_mtd_device(mymtd))
	{
		printk("Failed to register new device\n");
	        iounmap(((struct mypriv *)mymtd->priv)->start);
		kfree(mymtd->priv);
		kfree(mymtd);
		return -EAGAIN;
	}
	printk("Registered physmem device from %dKb to %dKb\n",
	       (int)(start / 1024), (int)(end / 1024));
	printk("Mapped from 0x%p to 0x%p\n",((struct mypriv *)mymtd->priv)->start,
((struct mypriv *)mymtd->priv)->end);

	return 0;
}

static void __exit cleanup_slram(void)
{
	iounmap(((struct mypriv *)mymtd->priv)->start);
	kfree (mymtd->priv);
	del_mtd_device(mymtd);
	kfree(mymtd);
}

#if LINUX_VERSION_CODE > 0x20300
module_init(init_slram);
module_exit(cleanup_slram);
#endif