summaryrefslogtreecommitdiffstats
path: root/drivers/mtd/map_ram.c
blob: 1a4e5a06a76193350cb3cad93f8b5b945d4c17ea (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
/*
 * Common code to handle map devices which are simple RAM
 * (C) 2000 Red Hat. GPL'd.
 * $Id: map_ram.c,v 1.7 2000/12/10 01:39:13 dwmw2 Exp $
 */

#include <linux/module.h>
#include <linux/types.h>
#include <linux/kernel.h>
#include <asm/io.h>
#include <asm/byteorder.h>
#include <linux/errno.h>
#include <linux/slab.h>

#include <linux/mtd/map.h>


static int mapram_read (struct mtd_info *, loff_t, size_t, size_t *, u_char *);
static int mapram_write (struct mtd_info *, loff_t, size_t, size_t *, const u_char *);
static int mapram_erase (struct mtd_info *, struct erase_info *);
static void mapram_nop (struct mtd_info *);

static const char im_name[] = "map_ram_probe";

/* This routine is made available to other mtd code via
 * inter_module_register.  It must only be accessed through
 * inter_module_get which will bump the use count of this module.  The
 * addresses passed back in mtd are valid as long as the use count of
 * this module is non-zero, i.e. between inter_module_get and
 * inter_module_put.  Keith Owens <kaos@ocs.com.au> 29 Oct 2000.
 */

static struct mtd_info *map_ram_probe(struct map_info *map)
{
	struct mtd_info *mtd;

	/* Check the first byte is RAM */
	map->write8(map, 0x55, 0);
	if (map->read8(map, 0) != 0x55)
		return NULL;

	map->write8(map, 0xAA, 0);
	if (map->read8(map, 0) != 0xAA)
		return NULL;

	/* Check the last byte is RAM */
	map->write8(map, 0x55, map->size-1);
	if (map->read8(map, map->size-1) != 0x55)
		return NULL;

	map->write8(map, 0xAA, map->size-1);
	if (map->read8(map, map->size-1) != 0xAA)
		return NULL;

	/* OK. It seems to be RAM. */

	mtd = kmalloc(sizeof(*mtd), GFP_KERNEL);
	if (!mtd)
		return NULL;

	memset(mtd, 0, sizeof(*mtd));

	map->im_name = im_name;
	map->fldrv_destroy = mapram_nop;
	mtd->priv = map;
	mtd->name = map->name;
	mtd->type = MTD_RAM;
	mtd->erasesize = 0x10000;
	mtd->size = map->size;
	mtd->erase = mapram_erase;
	mtd->read = mapram_read;
	mtd->write = mapram_write;
	mtd->sync = mapram_nop;
	mtd->flags = MTD_CAP_RAM | MTD_VOLATILE;
	mtd->erasesize = PAGE_SIZE;

	return mtd;
}


static int mapram_read (struct mtd_info *mtd, loff_t from, size_t len, size_t *retlen, u_char *buf)
{
	struct map_info *map = (struct map_info *)mtd->priv;

	map->copy_from(map, buf, from, len);
	*retlen = len;
	return 0;
}

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

	map->copy_to(map, to, buf, len);
	*retlen = len;
	return 0;
}

static int mapram_erase (struct mtd_info *mtd, struct erase_info *instr)
{
	/* Yeah, it's inefficient. Who cares? It's faster than a _real_
	   flash erase. */
	struct map_info *map = (struct map_info *)mtd->priv;
	unsigned long i;

	for (i=0; i<instr->len; i++)
		map->write8(map, 0xFF, instr->addr + i);

	if (instr->callback)
		instr->callback(instr);

	return 0;
}

static void mapram_nop(struct mtd_info *mtd)
{
	/* Nothing to see here */
}

#if LINUX_VERSION_CODE < 0x20212 && defined(MODULE)
#define map_ram_init init_module
#define map_ram_exit cleanup_module
#endif

static int __init map_ram_init(void)
{
	inter_module_register(im_name, THIS_MODULE, &map_ram_probe);
	return 0;
}

static void __exit map_ram_exit(void)
{
	inter_module_unregister(im_name);
}

module_init(map_ram_init);
module_exit(map_ram_exit);