cfi_mtd.c 6.43 KB
Newer Older
Piotr Ziecik's avatar
Piotr Ziecik committed
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
/*
 * (C) Copyright 2008 Semihalf
 *
 * Written by: Piotr Ziecik <kosmo@semihalf.com>
 *
 * See file CREDITS for list of people who contributed to this
 * project.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of
 * the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.	 See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
 * MA 02111-1307 USA
 *
 */

#include <common.h>
#include <flash.h>
28
#include <malloc.h>
Piotr Ziecik's avatar
Piotr Ziecik committed
29 30 31

#include <asm/errno.h>
#include <linux/mtd/mtd.h>
32
#include <linux/mtd/concat.h>
Piotr Ziecik's avatar
Piotr Ziecik committed
33

34 35 36 37 38 39 40
/* use CONFIG_SYS_MAX_FLASH_BANKS_DETECT if defined */
#ifdef CONFIG_SYS_MAX_FLASH_BANKS_DETECT
# define CFI_MAX_FLASH_BANKS	CONFIG_SYS_MAX_FLASH_BANKS_DETECT
#else
# define CFI_MAX_FLASH_BANKS	CONFIG_SYS_MAX_FLASH_BANKS
#endif

Piotr Ziecik's avatar
Piotr Ziecik committed
41 42
extern flash_info_t flash_info[];

43 44
static struct mtd_info cfi_mtd_info[CFI_MAX_FLASH_BANKS];
static char cfi_mtd_names[CFI_MAX_FLASH_BANKS][16];
45 46 47
#ifdef CONFIG_MTD_CONCAT
static char c_mtd_name[16];
#endif
Piotr Ziecik's avatar
Piotr Ziecik committed
48 49 50 51 52 53 54 55 56 57

static int cfi_mtd_erase(struct mtd_info *mtd, struct erase_info *instr)
{
	flash_info_t *fi = mtd->priv;
	size_t a_start = fi->start[0] + instr->addr;
	size_t a_end = a_start + instr->len;
	int s_first = -1;
	int s_last = -1;
	int error, sect;

58
	for (sect = 0; sect < fi->sector_count; sect++) {
Piotr Ziecik's avatar
Piotr Ziecik committed
59 60 61
		if (a_start == fi->start[sect])
			s_first = sect;

62 63 64 65 66 67
		if (sect < fi->sector_count - 1) {
			if (a_end == fi->start[sect + 1]) {
				s_last = sect;
				break;
			}
		} else {
Piotr Ziecik's avatar
Piotr Ziecik committed
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
			s_last = sect;
			break;
		}
	}

	if (s_first >= 0 && s_first <= s_last) {
		instr->state = MTD_ERASING;

		flash_set_verbose(0);
		error = flash_erase(fi, s_first, s_last);
		flash_set_verbose(1);

		if (error) {
			instr->state = MTD_ERASE_FAILED;
			return -EIO;
		}

		instr->state = MTD_ERASE_DONE;
		mtd_erase_callback(instr);
		return 0;
	}

	return -EINVAL;
}

static int cfi_mtd_read(struct mtd_info *mtd, loff_t from, size_t len,
	size_t *retlen, u_char *buf)
{
	flash_info_t *fi = mtd->priv;
	u_char *f = (u_char*)(fi->start[0]) + from;

	memcpy(buf, f, len);
	*retlen = len;

	return 0;
}

static int cfi_mtd_write(struct mtd_info *mtd, loff_t to, size_t len,
	size_t *retlen, const u_char *buf)
{
	flash_info_t *fi = mtd->priv;
	u_long t = fi->start[0] + to;
	int error;

	flash_set_verbose(0);
	error = write_buff(fi, (u_char*)buf, t, len);
	flash_set_verbose(1);

	if (!error) {
		*retlen = len;
		return 0;
	}

	return -EIO;
}

static void cfi_mtd_sync(struct mtd_info *mtd)
{
	/*
	 * This function should wait until all pending operations
	 * finish. However this driver is fully synchronous, so
	 * this function returns immediately
	 */
}

133
static int cfi_mtd_lock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
Piotr Ziecik's avatar
Piotr Ziecik committed
134 135 136 137 138 139 140 141 142 143 144
{
	flash_info_t *fi = mtd->priv;

	flash_set_verbose(0);
	flash_protect(FLAG_PROTECT_SET, fi->start[0] + ofs,
					fi->start[0] + ofs + len - 1, fi);
	flash_set_verbose(1);

	return 0;
}

145
static int cfi_mtd_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
Piotr Ziecik's avatar
Piotr Ziecik committed
146 147 148 149 150 151 152 153 154 155 156 157 158 159
{
	flash_info_t *fi = mtd->priv;

	flash_set_verbose(0);
	flash_protect(FLAG_PROTECT_CLEAR, fi->start[0] + ofs,
					fi->start[0] + ofs + len - 1, fi);
	flash_set_verbose(1);

	return 0;
}

static int cfi_mtd_set_erasesize(struct mtd_info *mtd, flash_info_t *fi)
{
	int sect_size = 0;
160
	int sect_size_old = 0;
Piotr Ziecik's avatar
Piotr Ziecik committed
161
	int sect;
162 163 164 165
	int regions = 0;
	int numblocks = 0;
	ulong offset = 0;
	ulong base_addr = fi->start[0];
Piotr Ziecik's avatar
Piotr Ziecik committed
166

167
	/*
168 169
	 * First detect the number of eraseregions so that we can allocate
	 * the array of eraseregions correctly
170
	 */
Piotr Ziecik's avatar
Piotr Ziecik committed
171
	for (sect = 0; sect < fi->sector_count; sect++) {
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
		if (sect_size_old != flash_sector_size(fi, sect))
			regions++;
		sect_size_old = flash_sector_size(fi, sect);
	}

	mtd->eraseregions = malloc(sizeof(struct mtd_erase_region_info) * regions);

	/*
	 * Now detect the largest sector and fill the eraseregions
	 */
	sect_size_old = 0;
	regions = 0;
	for (sect = 0; sect < fi->sector_count; sect++) {
		if ((sect_size_old != flash_sector_size(fi, sect)) &&
		    (sect_size_old != 0)) {
			mtd->eraseregions[regions].offset = offset - base_addr;
			mtd->eraseregions[regions].erasesize = sect_size_old;
			mtd->eraseregions[regions].numblocks = numblocks;

			/* Now start counting the next eraseregions */
			numblocks = 0;
			regions++;
		} else {
			numblocks++;
		}

		if (sect_size_old != flash_sector_size(fi, sect))
			offset = fi->start[sect];

		/*
		 * Select the largest sector size as erasesize (e.g. for UBI)
		 */
204
		if (flash_sector_size(fi, sect) > sect_size)
Piotr Ziecik's avatar
Piotr Ziecik committed
205
			sect_size = flash_sector_size(fi, sect);
206 207

		sect_size_old = flash_sector_size(fi, sect);
Piotr Ziecik's avatar
Piotr Ziecik committed
208 209
	}

210 211 212 213 214 215 216 217 218 219 220 221
	/*
	 * Set the last region
	 */
	mtd->eraseregions[regions].offset = offset - base_addr;
	mtd->eraseregions[regions].erasesize = sect_size_old;
	mtd->eraseregions[regions].numblocks = numblocks + 1;

	if (regions)
		mtd->numeraseregions = regions + 1;
	else
		mtd->numeraseregions = 0;

Piotr Ziecik's avatar
Piotr Ziecik committed
222 223 224 225 226 227 228 229 230 231
	mtd->erasesize = sect_size;

	return 0;
}

int cfi_mtd_init(void)
{
	struct mtd_info *mtd;
	flash_info_t *fi;
	int error, i;
232 233
	int devices_found = 0;
	struct mtd_info *mtd_list[CONFIG_SYS_MAX_FLASH_BANKS];
Piotr Ziecik's avatar
Piotr Ziecik committed
234 235 236 237 238 239 240 241 242 243 244

	for (i = 0; i < CONFIG_SYS_MAX_FLASH_BANKS; i++) {
		fi = &flash_info[i];
		mtd = &cfi_mtd_info[i];

		memset(mtd, 0, sizeof(struct mtd_info));

		error = cfi_mtd_set_erasesize(mtd, fi);
		if (error)
			continue;

245 246
		sprintf(cfi_mtd_names[i], "nor%d", i);
		mtd->name		= cfi_mtd_names[i];
Piotr Ziecik's avatar
Piotr Ziecik committed
247 248 249 250 251 252 253 254 255 256 257 258 259 260 261
		mtd->type		= MTD_NORFLASH;
		mtd->flags		= MTD_CAP_NORFLASH;
		mtd->size		= fi->size;
		mtd->writesize		= 1;

		mtd->erase		= cfi_mtd_erase;
		mtd->read		= cfi_mtd_read;
		mtd->write		= cfi_mtd_write;
		mtd->sync		= cfi_mtd_sync;
		mtd->lock		= cfi_mtd_lock;
		mtd->unlock		= cfi_mtd_unlock;
		mtd->priv		= fi;

		if (add_mtd_device(mtd))
			return -ENOMEM;
262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278

		mtd_list[devices_found++] = mtd;
	}

#ifdef CONFIG_MTD_CONCAT
	if (devices_found > 1) {
		/*
		 * We detected multiple devices. Concatenate them together.
		 */
		sprintf(c_mtd_name, "nor%d", devices_found);
		mtd = mtd_concat_create(mtd_list, devices_found, c_mtd_name);

		if (mtd == NULL)
			return -ENXIO;

		if (add_mtd_device(mtd))
			return -ENOMEM;
Piotr Ziecik's avatar
Piotr Ziecik committed
279
	}
280
#endif /* CONFIG_MTD_CONCAT */
Piotr Ziecik's avatar
Piotr Ziecik committed
281 282 283

	return 0;
}