summaryrefslogtreecommitdiffstats
path: root/target/linux/rdc-2.6/files/drivers/mtd/maps/rdc3210.c
blob: f6a634a9cac20a79f64268926cfa0323dc025841 (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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
/*******************************************************************
 * Simple Flash mapping for RDC3210                                *
 *                                                                 *
 *                                                     2005.03.23  *
 *                              Dante Su (dante_su@gemtek.com.tw)  *
 *                          Copyright (C) 2005 Gemtek Corporation  *
 *******************************************************************/

#include <linux/module.h>
#include <linux/types.h>
#include <linux/kernel.h>
#include <asm/io.h>
#include <linux/mtd/mtd.h>
#include <linux/mtd/map.h>
#include <linux/mtd/partitions.h>
#include <linux/config.h>

#ifndef RDC3210_STATIC_MAP
#define RDC3210_STATIC_MAP	0
#endif
#ifndef RDC3210_FACTORY_DFLT
#define RDC3210_FACTORY_DFLT	0
#endif
#ifndef RDC3210_USING_JFFS2
#define RDC3210_USING_JFFS2	1
#endif

#define WINDOW_ADDR		0xFFC00000
#define WINDOW_SIZE		0x00400000

#define BUSWIDTH 2

/* Dante: linked from linux-2.4.x/drivers/mtd/chips/flashdrv.c */
extern int flashdrv_get_size(void);
extern int flashdrv_get_sector(int addr);
extern int flashdrv_get_sector_addr(int sector);
extern int flashdrv_get_sector_size(int sector);

static struct mtd_info		*rdc3210_mtd;

__u8  rdc3210_map_read8(struct map_info *map, unsigned long ofs)
{
	return *(__u8 *)(map->map_priv_1 + ofs);
}

__u16 rdc3210_map_read16(struct map_info *map, unsigned long ofs)
{
	return *(__u16 *)(map->map_priv_1 + ofs);
}

__u32 rdc3210_map_read32(struct map_info *map, unsigned long ofs)
{
	return *(__u32 *)(map->map_priv_1 + ofs);
}

void rdc3210_map_write8(struct map_info *map, __u8 d, unsigned long adr)
{
	*(__u8 *)(map->map_priv_1 + adr) = d;
}

void rdc3210_map_write16(struct map_info *map, __u16 d, unsigned long adr)
{
	*(__u16 *)(map->map_priv_1 + adr) = d;
}

void rdc3210_map_write32(struct map_info *map, __u32 d, unsigned long adr)
{
	*(__u32 *)(map->map_priv_1 + adr) = d;
}

void rdc3210_map_copy_from(struct map_info *map, void *to, unsigned long from, ssize_t len)
{
	int	i;
	u16	*dst = (u16 *)(to);
	u16	*src = (u16 *)(map->map_priv_1 + from);

	for(i = 0; i < (len / 2); ++i)
		dst[i] = src[i];

	if(len & 1)
	{
		printk("# WARNNING!!! rdc3210_map_copy_from has odd length\n");
		//dst[len - 1] = B0(src[i]);
	}
}

void rdc3210_map_copy_to(struct map_info *map, void *to, unsigned long from, ssize_t len)
{
	int	i;
	u16	*dst = (u16 *)(map->map_priv_1 + to);
	u16	*src = (u16 *)(from);
	
	for(i = 0; i < (len / 2); ++i)
		dst[i] = src[i];

	if(len & 1)
	{
		printk("# WARNNING!!! rdc3210_map_copy_from has odd length\n");
		//dst[len - 1] = B0(src[i]);
	}
}

struct map_info rdc3210_map = 
{
	name:		"RDC3210 Flash",
	size:		WINDOW_SIZE,
	buswidth:	BUSWIDTH,
	read8:		rdc3210_map_read8,
	read16:		rdc3210_map_read16,
	read32:		rdc3210_map_read32,
	copy_from:	rdc3210_map_copy_from,
	write8:		rdc3210_map_write8,
	write16:	rdc3210_map_write16,
	write32:	rdc3210_map_write32,
	copy_to:	rdc3210_map_copy_to,
};

/* Dante: This is the default static mapping, however this is nothing but a hint. (Say dynamic mapping) */
static struct mtd_partition rdc3210_parts[] = 
{
	{ name: "linux",   offset:  0,          size: 0x003C0000 },	/* 3840 KB = (Kernel + ROMFS) = (768 KB + 3072 KB) */
	{ name: "romfs",   offset:  0x000C0000, size: 0x00300000 },	/* 3072 KB */
	{ name: "nvram",   offset:  0x003C0000, size: 0x00010000 },	/*   64 KB */
#if RDC3210_STATIC_MAP || RDC3210_FACTORY_DFLT
	{ name: "factory", offset:  0x003D0000, size: 0x00010000 },	/*   64 KB */
#endif
	{ name: "bootldr", offset:  0x003E0000, size: 0x00020000 },	/*  128 KB */
};

#if LINUX_VERSION_CODE < 0x20212 && defined(MODULE)
#define init_rdc3210_map		init_module
#define cleanup_rdc3210_map		cleanup_module
#endif

mod_init_t init_rdc3210_map(void)
{
       	printk(KERN_NOTICE "flash device: %x at %x\n", WINDOW_SIZE, WINDOW_ADDR);
       	
	rdc3210_map.map_priv_1 = ioremap(WINDOW_ADDR, WINDOW_SIZE);

	if (!rdc3210_map.map_priv_1) 
	{
		printk("Failed to ioremap\n");
		return -EIO;
	}
	rdc3210_mtd = do_map_probe("cfi_probe", &rdc3210_map);
#if RDC3210_STATIC_MAP	/* Dante: This is for fixed map */
	if (rdc3210_mtd) 
	{
		rdc3210_mtd->module = THIS_MODULE;
		add_mtd_partitions(rdc3210_mtd, rdc3210_parts, sizeof(rdc3210_parts)/sizeof(rdc3210_parts[0]));
		return 0;
	}
#else	/* Dante: This is for dynamic mapping */

#include <gemtek/sysdep.h>
#include <gemtek/imghdr.h>

	if (rdc3210_mtd) 
	{	// Dante
#if RDC3210_USING_JFFS2
		unsigned int	tmp, tmp2 = rdc3210_mtd->erasesize;
#else
		unsigned int	tmp, tmp2 = 32;
#endif
		gt_imghdr_t	*hdr;
		
		hdr = (gt_imghdr_t *)(rdc3210_map.map_priv_1);
		
		if(memcmp(hdr->magic, GTIMG_MAGIC, 4))
		{
			printk("Invalid MAGIC for Firmware Image!!!\n");
			return -EIO;
		}

#if RDC3210_FACTORY_DFLT
		/* 1. Adjust Redboot */
		tmp = flashdrv_get_size() - rdc3210_parts[4].size;
		rdc3210_parts[4].offset = flashdrv_get_sector_addr(flashdrv_get_sector(tmp));
		rdc3210_parts[4].size   = flashdrv_get_size() - rdc3210_parts[4].offset;
		
		/* 2. Adjust NVRAM */
		tmp -= rdc3210_parts[3].size;
		rdc3210_parts[3].offset = flashdrv_get_sector_addr(flashdrv_get_sector(tmp));
		rdc3210_parts[3].size   = rdc3210_parts[4].offset - rdc3210_parts[3].offset;
		
		/* 3. Adjust Factory Default */
		tmp -= rdc3210_parts[2].size;
		rdc3210_parts[2].offset = flashdrv_get_sector_addr(flashdrv_get_sector(tmp));
		rdc3210_parts[2].size   = rdc3210_parts[3].offset - rdc3210_parts[2].offset;
		
		/* 4. Adjust Linux (Kernel + ROMFS) */
		rdc3210_parts[0].size   = rdc3210_parts[2].offset - rdc3210_parts[0].offset;

		/* 5. Adjust ROMFS */
		tmp = hdr->kernelsz + sizeof(gt_imghdr_t);
		rdc3210_parts[1].offset = rdc3210_parts[0].offset + (((tmp / tmp2) + ((tmp % tmp2) ? 1 : 0)) * tmp2);
		rdc3210_parts[1].size   = rdc3210_parts[2].offset - rdc3210_parts[1].offset;
#else
		/* 1. Adjust Redboot */
		tmp = flashdrv_get_size() - rdc3210_parts[3].size;
		rdc3210_parts[3].offset = flashdrv_get_sector_addr(flashdrv_get_sector(tmp));
		rdc3210_parts[3].size   = flashdrv_get_size() - rdc3210_parts[3].offset;
		
		/* 2. Adjust NVRAM */
		tmp -= rdc3210_parts[2].size;
		rdc3210_parts[2].offset = flashdrv_get_sector_addr(flashdrv_get_sector(tmp));
		rdc3210_parts[2].size   = rdc3210_parts[3].offset - rdc3210_parts[2].offset;
				
		/* 4. Adjust Linux (Kernel + ROMFS) */
		rdc3210_parts[0].size   = rdc3210_parts[2].offset - rdc3210_parts[0].offset;

		/* 5. Adjust ROMFS */
		tmp = hdr->kernelsz + sizeof(gt_imghdr_t);
		rdc3210_parts[1].offset = rdc3210_parts[0].offset + (((tmp / tmp2) + ((tmp % tmp2) ? 1 : 0)) * tmp2);
		rdc3210_parts[1].size   = rdc3210_parts[2].offset - rdc3210_parts[1].offset;
#endif
		
		rdc3210_mtd->module = THIS_MODULE;
		add_mtd_partitions(rdc3210_mtd, rdc3210_parts, sizeof(rdc3210_parts)/sizeof(rdc3210_parts[0]));
		return 0;
	}
#endif

	iounmap((void *)rdc3210_map.map_priv_1);
	return -ENXIO;
}

mod_exit_t cleanup_rdc3210_map(void)
{
	if (rdc3210_mtd) 
	{
		del_mtd_partitions(rdc3210_mtd);
		map_destroy(rdc3210_mtd);
	}
	
	if (rdc3210_map.map_priv_1) 
	{
		iounmap((void *)rdc3210_map.map_priv_1);
		rdc3210_map.map_priv_1 = NULL;
	}
}

module_init(init_rdc3210_map);
module_exit(cleanup_rdc3210_map);