2018-05-06 21:58:06 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0+
|
2008-11-17 14:57:58 +00:00
|
|
|
/*
|
|
|
|
* (C) Copyright 2008 Semihalf
|
|
|
|
*
|
|
|
|
* Written by: Piotr Ziecik <kosmo@semihalf.com>
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <common.h>
|
2020-09-17 11:23:08 +00:00
|
|
|
#include <dma.h>
|
2008-11-17 14:57:58 +00:00
|
|
|
#include <flash.h>
|
2009-05-12 12:29:39 +00:00
|
|
|
#include <malloc.h>
|
2008-11-17 14:57:58 +00:00
|
|
|
|
2016-09-21 02:28:55 +00:00
|
|
|
#include <linux/errno.h>
|
2008-11-17 14:57:58 +00:00
|
|
|
#include <linux/mtd/mtd.h>
|
2009-05-12 12:29:39 +00:00
|
|
|
#include <linux/mtd/concat.h>
|
2010-08-31 08:04:11 +00:00
|
|
|
#include <mtd/cfi_flash.h>
|
2008-11-17 14:57:58 +00:00
|
|
|
|
2009-07-14 21:00:24 +00:00
|
|
|
static struct mtd_info cfi_mtd_info[CFI_MAX_FLASH_BANKS];
|
|
|
|
static char cfi_mtd_names[CFI_MAX_FLASH_BANKS][16];
|
2009-05-12 12:29:39 +00:00
|
|
|
#ifdef CONFIG_MTD_CONCAT
|
|
|
|
static char c_mtd_name[16];
|
|
|
|
#endif
|
2008-11-17 14:57:58 +00:00
|
|
|
|
|
|
|
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;
|
|
|
|
|
2009-05-11 13:54:13 +00:00
|
|
|
for (sect = 0; sect < fi->sector_count; sect++) {
|
2008-11-17 14:57:58 +00:00
|
|
|
if (a_start == fi->start[sect])
|
|
|
|
s_first = sect;
|
|
|
|
|
2009-05-11 13:54:13 +00:00
|
|
|
if (sect < fi->sector_count - 1) {
|
|
|
|
if (a_end == fi->start[sect + 1]) {
|
|
|
|
s_last = sect;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} else {
|
2008-11-17 14:57:58 +00:00
|
|
|
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;
|
|
|
|
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;
|
|
|
|
|
2020-09-17 11:23:08 +00:00
|
|
|
if (dma_memcpy(buf, f, len) < 0)
|
|
|
|
memcpy(buf, f, len);
|
2008-11-17 14:57:58 +00:00
|
|
|
*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
|
|
|
|
*/
|
|
|
|
}
|
|
|
|
|
2009-05-11 14:03:55 +00:00
|
|
|
static int cfi_mtd_lock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
|
2008-11-17 14:57:58 +00:00
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2009-05-11 14:03:55 +00:00
|
|
|
static int cfi_mtd_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
|
2008-11-17 14:57:58 +00:00
|
|
|
{
|
|
|
|
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;
|
2009-05-12 12:29:39 +00:00
|
|
|
int sect_size_old = 0;
|
2008-11-17 14:57:58 +00:00
|
|
|
int sect;
|
2009-05-12 12:29:39 +00:00
|
|
|
int regions = 0;
|
|
|
|
int numblocks = 0;
|
2010-01-28 11:27:14 +00:00
|
|
|
ulong offset;
|
|
|
|
ulong base_addr;
|
2008-11-17 14:57:58 +00:00
|
|
|
|
2009-03-18 10:17:37 +00:00
|
|
|
/*
|
2009-05-12 12:29:39 +00:00
|
|
|
* First detect the number of eraseregions so that we can allocate
|
|
|
|
* the array of eraseregions correctly
|
2009-03-18 10:17:37 +00:00
|
|
|
*/
|
2008-11-17 14:57:58 +00:00
|
|
|
for (sect = 0; sect < fi->sector_count; sect++) {
|
2009-05-12 12:29:39 +00:00
|
|
|
if (sect_size_old != flash_sector_size(fi, sect))
|
|
|
|
regions++;
|
|
|
|
sect_size_old = flash_sector_size(fi, sect);
|
|
|
|
}
|
|
|
|
|
2010-01-28 11:27:14 +00:00
|
|
|
switch (regions) {
|
|
|
|
case 0:
|
|
|
|
return 1;
|
|
|
|
case 1: /* flash has uniform erase size */
|
|
|
|
mtd->numeraseregions = 0;
|
|
|
|
mtd->erasesize = sect_size_old;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
mtd->numeraseregions = regions;
|
2009-05-12 12:29:39 +00:00
|
|
|
mtd->eraseregions = malloc(sizeof(struct mtd_erase_region_info) * regions);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Now detect the largest sector and fill the eraseregions
|
|
|
|
*/
|
|
|
|
regions = 0;
|
2010-01-28 11:27:14 +00:00
|
|
|
base_addr = offset = fi->start[0];
|
|
|
|
sect_size_old = flash_sector_size(fi, 0);
|
2009-05-12 12:29:39 +00:00
|
|
|
for (sect = 0; sect < fi->sector_count; sect++) {
|
2010-01-28 11:27:14 +00:00
|
|
|
if (sect_size_old != flash_sector_size(fi, sect)) {
|
2009-05-12 12:29:39 +00:00
|
|
|
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++;
|
|
|
|
offset = fi->start[sect];
|
2010-01-28 11:27:14 +00:00
|
|
|
}
|
|
|
|
numblocks++;
|
2009-05-12 12:29:39 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Select the largest sector size as erasesize (e.g. for UBI)
|
|
|
|
*/
|
2009-03-18 10:17:37 +00:00
|
|
|
if (flash_sector_size(fi, sect) > sect_size)
|
2008-11-17 14:57:58 +00:00
|
|
|
sect_size = flash_sector_size(fi, sect);
|
2009-05-12 12:29:39 +00:00
|
|
|
|
|
|
|
sect_size_old = flash_sector_size(fi, sect);
|
2008-11-17 14:57:58 +00:00
|
|
|
}
|
|
|
|
|
2009-05-12 12:29:39 +00:00
|
|
|
/*
|
|
|
|
* Set the last region
|
|
|
|
*/
|
|
|
|
mtd->eraseregions[regions].offset = offset - base_addr;
|
|
|
|
mtd->eraseregions[regions].erasesize = sect_size_old;
|
2010-01-28 11:27:14 +00:00
|
|
|
mtd->eraseregions[regions].numblocks = numblocks;
|
2009-05-12 12:29:39 +00:00
|
|
|
|
2008-11-17 14:57:58 +00:00
|
|
|
mtd->erasesize = sect_size;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int cfi_mtd_init(void)
|
|
|
|
{
|
|
|
|
struct mtd_info *mtd;
|
|
|
|
flash_info_t *fi;
|
|
|
|
int error, i;
|
2011-10-05 20:48:12 +00:00
|
|
|
#ifdef CONFIG_MTD_CONCAT
|
2009-05-12 12:29:39 +00:00
|
|
|
int devices_found = 0;
|
|
|
|
struct mtd_info *mtd_list[CONFIG_SYS_MAX_FLASH_BANKS];
|
2011-10-05 20:48:12 +00:00
|
|
|
#endif
|
2008-11-17 14:57:58 +00:00
|
|
|
|
|
|
|
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;
|
|
|
|
|
2009-04-02 15:15:34 +00:00
|
|
|
sprintf(cfi_mtd_names[i], "nor%d", i);
|
|
|
|
mtd->name = cfi_mtd_names[i];
|
2008-11-17 14:57:58 +00:00
|
|
|
mtd->type = MTD_NORFLASH;
|
|
|
|
mtd->flags = MTD_CAP_NORFLASH;
|
|
|
|
mtd->size = fi->size;
|
|
|
|
mtd->writesize = 1;
|
2014-10-31 10:29:45 +00:00
|
|
|
mtd->writebufsize = mtd->writesize;
|
2008-11-17 14:57:58 +00:00
|
|
|
|
2013-01-14 03:46:50 +00:00
|
|
|
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;
|
2008-11-17 14:57:58 +00:00
|
|
|
mtd->priv = fi;
|
|
|
|
|
|
|
|
if (add_mtd_device(mtd))
|
|
|
|
return -ENOMEM;
|
2009-05-12 12:29:39 +00:00
|
|
|
|
2011-10-05 20:48:12 +00:00
|
|
|
#ifdef CONFIG_MTD_CONCAT
|
2009-05-12 12:29:39 +00:00
|
|
|
mtd_list[devices_found++] = mtd;
|
2011-10-05 20:48:12 +00:00
|
|
|
#endif
|
2009-05-12 12:29:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#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;
|
2008-11-17 14:57:58 +00:00
|
|
|
}
|
2009-05-12 12:29:39 +00:00
|
|
|
#endif /* CONFIG_MTD_CONCAT */
|
2008-11-17 14:57:58 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|