2007-10-12 08:04:54 +00:00
|
|
|
/*
|
|
|
|
* (C) Copyright 2005
|
|
|
|
* 2N Telekomunikace, a.s. <www.2n.cz>
|
|
|
|
* Ladislav Michl <michl@2n.cz>
|
|
|
|
*
|
|
|
|
* 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
|
|
|
|
* version 2 as published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
* 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>
|
2007-10-19 23:13:46 +00:00
|
|
|
#include <linux/mtd/nand.h>
|
2008-06-04 07:40:44 +00:00
|
|
|
#include <linux/mtd/mtd.h>
|
2007-10-15 15:16:25 +00:00
|
|
|
#include <init.h>
|
|
|
|
#include <xfuncs.h>
|
|
|
|
#include <driver.h>
|
2008-06-04 07:40:44 +00:00
|
|
|
#include <malloc.h>
|
|
|
|
#include <ioctl.h>
|
|
|
|
#include <nand.h>
|
2009-10-20 13:28:16 +00:00
|
|
|
#include <errno.h>
|
2007-10-12 08:04:54 +00:00
|
|
|
|
2009-04-22 21:39:27 +00:00
|
|
|
static ssize_t nand_read(struct cdev *cdev, void* buf, size_t count, ulong offset, ulong flags)
|
2007-10-15 15:16:25 +00:00
|
|
|
{
|
2009-04-22 21:39:27 +00:00
|
|
|
struct mtd_info *info = cdev->priv;
|
2007-10-19 23:13:46 +00:00
|
|
|
size_t retlen;
|
|
|
|
int ret;
|
2007-10-12 08:04:54 +00:00
|
|
|
|
2011-01-07 05:13:00 +00:00
|
|
|
debug("nand_read: 0x%08lx 0x%08x\n", offset, count);
|
2007-10-12 08:04:54 +00:00
|
|
|
|
2008-08-11 08:59:28 +00:00
|
|
|
ret = info->read(info, offset, count, &retlen, buf);
|
2007-10-12 08:04:54 +00:00
|
|
|
|
2009-04-22 21:39:27 +00:00
|
|
|
if(ret) {
|
|
|
|
printf("err %d\n", ret);
|
2007-10-19 23:13:46 +00:00
|
|
|
return ret;
|
2009-04-22 21:39:27 +00:00
|
|
|
}
|
2007-10-19 23:13:46 +00:00
|
|
|
return retlen;
|
2007-10-12 08:04:54 +00:00
|
|
|
}
|
|
|
|
|
2008-08-11 08:59:28 +00:00
|
|
|
#define NOTALIGNED(x) (x & (info->writesize - 1)) != 0
|
2008-06-04 07:40:44 +00:00
|
|
|
|
2011-04-04 09:24:47 +00:00
|
|
|
#ifdef CONFIG_NAND_WRITE
|
2009-12-02 16:58:42 +00:00
|
|
|
static int all_ff(const void *buf, int len)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
const uint8_t *p = buf;
|
|
|
|
|
|
|
|
for (i = 0; i < len; i++)
|
|
|
|
if (p[i] != 0xFF)
|
|
|
|
return 0;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2009-04-22 21:39:27 +00:00
|
|
|
static ssize_t nand_write(struct cdev* cdev, const void *buf, size_t _count, ulong offset, ulong flags)
|
2007-10-12 08:04:54 +00:00
|
|
|
{
|
2009-04-22 21:39:27 +00:00
|
|
|
struct mtd_info *info = cdev->priv;
|
2008-06-04 07:40:44 +00:00
|
|
|
size_t retlen, now;
|
2009-12-02 16:58:42 +00:00
|
|
|
int ret = 0;
|
2008-06-04 07:40:44 +00:00
|
|
|
void *wrbuf = NULL;
|
|
|
|
size_t count = _count;
|
|
|
|
|
2009-12-02 16:58:12 +00:00
|
|
|
if (NOTALIGNED(offset)) {
|
2011-01-07 05:13:00 +00:00
|
|
|
printf("offset 0x%0lx not page aligned\n", offset);
|
2009-12-02 16:58:12 +00:00
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
2011-01-07 05:13:00 +00:00
|
|
|
debug("write: 0x%08lx 0x%08x\n", offset, count);
|
2008-06-04 07:40:44 +00:00
|
|
|
|
|
|
|
while (count) {
|
2008-08-11 08:59:28 +00:00
|
|
|
now = count > info->writesize ? info->writesize : count;
|
|
|
|
|
2009-12-02 16:58:12 +00:00
|
|
|
if (NOTALIGNED(now)) {
|
2011-01-07 05:13:00 +00:00
|
|
|
debug("not aligned: %d %ld\n", info->writesize, (offset % info->writesize));
|
2008-08-11 08:59:28 +00:00
|
|
|
wrbuf = xmalloc(info->writesize);
|
|
|
|
memset(wrbuf, 0xff, info->writesize);
|
|
|
|
memcpy(wrbuf + (offset % info->writesize), buf, now);
|
2009-12-02 16:58:42 +00:00
|
|
|
if (!all_ff(wrbuf, info->writesize))
|
|
|
|
ret = info->write(info, offset & ~(info->writesize - 1),
|
|
|
|
info->writesize, &retlen, wrbuf);
|
|
|
|
free(wrbuf);
|
2008-06-04 07:40:44 +00:00
|
|
|
} else {
|
2009-12-02 16:58:42 +00:00
|
|
|
if (!all_ff(buf, info->writesize))
|
|
|
|
ret = info->write(info, offset, now, &retlen, buf);
|
2011-01-07 05:13:00 +00:00
|
|
|
debug("offset: 0x%08lx now: 0x%08x retlen: 0x%08x\n", offset, now, retlen);
|
2008-06-04 07:40:44 +00:00
|
|
|
}
|
|
|
|
if (ret)
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
offset += now;
|
|
|
|
count -= now;
|
|
|
|
buf += now;
|
|
|
|
}
|
|
|
|
|
|
|
|
out:
|
|
|
|
return ret ? ret : _count;
|
|
|
|
}
|
2011-04-04 09:24:47 +00:00
|
|
|
#endif
|
2008-06-04 07:40:44 +00:00
|
|
|
|
2009-04-22 21:39:27 +00:00
|
|
|
static int nand_ioctl(struct cdev *cdev, int request, void *buf)
|
2008-06-04 07:40:44 +00:00
|
|
|
{
|
2009-04-22 21:39:27 +00:00
|
|
|
struct mtd_info *info = cdev->priv;
|
2008-08-11 08:59:28 +00:00
|
|
|
struct mtd_info_user *user = buf;
|
2008-06-04 07:40:44 +00:00
|
|
|
|
|
|
|
switch (request) {
|
|
|
|
case MEMGETBADBLOCK:
|
2011-01-07 05:13:00 +00:00
|
|
|
debug("MEMGETBADBLOCK: 0x%08lx\n", (off_t)buf);
|
2008-08-11 08:59:28 +00:00
|
|
|
return info->block_isbad(info, (off_t)buf);
|
2011-04-07 15:04:32 +00:00
|
|
|
#ifdef CONFIG_NAND_WRITE
|
2008-09-02 15:20:33 +00:00
|
|
|
case MEMSETBADBLOCK:
|
2011-01-07 05:13:00 +00:00
|
|
|
debug("MEMSETBADBLOCK: 0x%08lx\n", (off_t)buf);
|
2008-09-02 15:20:33 +00:00
|
|
|
return info->block_markbad(info, (off_t)buf);
|
2011-04-07 15:04:32 +00:00
|
|
|
#endif
|
2008-06-04 07:40:44 +00:00
|
|
|
case MEMGETINFO:
|
2008-08-11 08:59:28 +00:00
|
|
|
user->type = info->type;
|
|
|
|
user->flags = info->flags;
|
|
|
|
user->size = info->size;
|
|
|
|
user->erasesize = info->erasesize;
|
|
|
|
user->oobsize = info->oobsize;
|
2010-06-28 08:21:57 +00:00
|
|
|
user->mtd = info;
|
2008-06-04 07:40:44 +00:00
|
|
|
/* The below fields are obsolete */
|
2008-08-11 08:59:28 +00:00
|
|
|
user->ecctype = -1;
|
|
|
|
user->eccsize = 0;
|
2008-06-04 07:40:44 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-04-04 09:24:47 +00:00
|
|
|
#ifdef CONFIG_NAND_WRITE
|
2009-04-22 21:39:27 +00:00
|
|
|
static ssize_t nand_erase(struct cdev *cdev, size_t count, unsigned long offset)
|
2008-06-04 07:40:44 +00:00
|
|
|
{
|
2009-04-22 21:39:27 +00:00
|
|
|
struct mtd_info *info = cdev->priv;
|
2008-06-04 07:40:44 +00:00
|
|
|
struct erase_info erase;
|
2007-10-19 23:13:46 +00:00
|
|
|
int ret;
|
2007-10-15 15:16:25 +00:00
|
|
|
|
2008-06-04 07:40:44 +00:00
|
|
|
memset(&erase, 0, sizeof(erase));
|
2008-08-11 08:59:28 +00:00
|
|
|
erase.mtd = info;
|
2008-06-04 07:40:44 +00:00
|
|
|
erase.addr = offset;
|
2008-08-11 08:59:28 +00:00
|
|
|
erase.len = info->erasesize;
|
2008-06-04 07:40:44 +00:00
|
|
|
|
|
|
|
while (count > 0) {
|
|
|
|
debug("erase %d %d\n", erase.addr, erase.len);
|
|
|
|
|
2008-08-11 08:59:28 +00:00
|
|
|
ret = info->block_isbad(info, erase.addr);
|
2008-06-04 07:40:44 +00:00
|
|
|
if (ret > 0) {
|
|
|
|
printf("Skipping bad block at 0x%08x\n", erase.addr);
|
|
|
|
} else {
|
2008-08-11 08:59:28 +00:00
|
|
|
ret = info->erase(info, &erase);
|
2008-06-04 07:40:44 +00:00
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2008-08-11 08:59:28 +00:00
|
|
|
erase.addr += info->erasesize;
|
|
|
|
count -= count > info->erasesize ? info->erasesize : count;
|
2008-06-04 07:40:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2011-04-04 09:24:47 +00:00
|
|
|
#endif
|
|
|
|
|
2009-07-21 16:37:32 +00:00
|
|
|
#if 0
|
|
|
|
static char* mtd_get_size(struct device_d *, struct param_d *param)
|
|
|
|
{
|
|
|
|
static char
|
|
|
|
}
|
|
|
|
#endif
|
2008-06-04 07:40:44 +00:00
|
|
|
|
2009-04-22 21:39:27 +00:00
|
|
|
static struct file_operations nand_ops = {
|
|
|
|
.read = nand_read,
|
2011-04-04 09:24:47 +00:00
|
|
|
#ifdef CONFIG_NAND_WRITE
|
2009-04-22 21:39:27 +00:00
|
|
|
.write = nand_write,
|
2011-04-04 09:24:47 +00:00
|
|
|
.erase = nand_erase,
|
|
|
|
#endif
|
2009-04-22 21:39:27 +00:00
|
|
|
.ioctl = nand_ioctl,
|
|
|
|
.lseek = dev_lseek_default,
|
|
|
|
};
|
|
|
|
|
2011-12-19 09:35:44 +00:00
|
|
|
#ifdef CONFIG_NAND_OOB_DEVICE
|
2009-10-20 13:28:16 +00:00
|
|
|
static ssize_t nand_read_oob(struct cdev *cdev, void *buf, size_t count, ulong offset, ulong flags)
|
|
|
|
{
|
|
|
|
struct mtd_info *info = cdev->priv;
|
|
|
|
struct nand_chip *chip = info->priv;
|
|
|
|
struct mtd_oob_ops ops;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
if (count < info->oobsize)
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
ops.mode = MTD_OOB_RAW;
|
|
|
|
ops.ooboffs = 0;
|
|
|
|
ops.ooblen = info->oobsize;
|
|
|
|
ops.oobbuf = buf;
|
|
|
|
ops.datbuf = NULL;
|
|
|
|
ops.len = info->oobsize;
|
|
|
|
|
|
|
|
offset /= info->oobsize;
|
|
|
|
ret = info->read_oob(info, offset << chip->page_shift, &ops);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
return info->oobsize;
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct file_operations nand_ops_oob = {
|
|
|
|
.read = nand_read_oob,
|
|
|
|
.ioctl = nand_ioctl,
|
|
|
|
.lseek = dev_lseek_default,
|
|
|
|
};
|
|
|
|
|
2011-04-07 15:59:52 +00:00
|
|
|
static int nand_init_oob_cdev(struct mtd_info *mtd)
|
2009-04-22 21:39:27 +00:00
|
|
|
{
|
2009-10-20 13:28:16 +00:00
|
|
|
struct nand_chip *chip = mtd->priv;
|
2011-04-07 15:59:52 +00:00
|
|
|
|
|
|
|
mtd->cdev_oob.ops = &nand_ops_oob;
|
|
|
|
mtd->cdev_oob.size = (mtd->size >> chip->page_shift) * mtd->oobsize;
|
|
|
|
mtd->cdev_oob.name = asprintf("nand_oob%d", mtd->class_dev.id);
|
|
|
|
mtd->cdev_oob.priv = mtd;
|
|
|
|
mtd->cdev_oob.dev = &mtd->class_dev;
|
|
|
|
devfs_create(&mtd->cdev_oob);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void nand_exit_oob_cdev(struct mtd_info *mtd)
|
|
|
|
{
|
|
|
|
free(mtd->cdev_oob.name);
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
|
|
|
|
static int nand_init_oob_cdev(struct mtd_info *mtd)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void nand_exit_oob_cdev(struct mtd_info *mtd)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
int add_mtd_device(struct mtd_info *mtd)
|
|
|
|
{
|
2010-06-03 08:13:29 +00:00
|
|
|
char str[16];
|
2009-10-20 13:28:16 +00:00
|
|
|
|
2009-06-11 13:33:49 +00:00
|
|
|
strcpy(mtd->class_dev.name, "nand");
|
|
|
|
register_device(&mtd->class_dev);
|
2008-08-11 08:59:28 +00:00
|
|
|
|
2009-04-22 21:39:27 +00:00
|
|
|
mtd->cdev.ops = &nand_ops;
|
|
|
|
mtd->cdev.size = mtd->size;
|
2009-06-11 13:33:49 +00:00
|
|
|
mtd->cdev.name = asprintf("nand%d", mtd->class_dev.id);
|
2009-04-22 21:39:27 +00:00
|
|
|
mtd->cdev.priv = mtd;
|
2009-06-11 13:33:49 +00:00
|
|
|
mtd->cdev.dev = &mtd->class_dev;
|
2010-06-28 08:21:57 +00:00
|
|
|
mtd->cdev.mtd = mtd;
|
2008-08-11 08:59:28 +00:00
|
|
|
|
2010-06-03 08:13:29 +00:00
|
|
|
sprintf(str, "%u", mtd->size);
|
|
|
|
dev_add_param_fixed(&mtd->class_dev, "size", str);
|
2011-10-13 13:38:35 +00:00
|
|
|
sprintf(str, "%u", mtd->erasesize);
|
|
|
|
dev_add_param_fixed(&mtd->class_dev, "erasesize", str);
|
|
|
|
sprintf(str, "%u", mtd->writesize);
|
|
|
|
dev_add_param_fixed(&mtd->class_dev, "writesize", str);
|
|
|
|
sprintf(str, "%u", mtd->oobsize);
|
|
|
|
dev_add_param_fixed(&mtd->class_dev, "oobsize", str);
|
2009-07-21 16:37:32 +00:00
|
|
|
|
2009-04-22 21:39:27 +00:00
|
|
|
devfs_create(&mtd->cdev);
|
2008-08-11 08:59:28 +00:00
|
|
|
|
2011-04-07 15:59:52 +00:00
|
|
|
nand_init_oob_cdev(mtd);
|
2009-10-20 13:28:16 +00:00
|
|
|
|
2008-08-11 08:59:28 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int del_mtd_device (struct mtd_info *mtd)
|
|
|
|
{
|
2009-04-22 21:39:27 +00:00
|
|
|
unregister_device(&mtd->class_dev);
|
2011-04-07 15:59:52 +00:00
|
|
|
nand_exit_oob_cdev(mtd);
|
2009-07-21 16:37:32 +00:00
|
|
|
free(mtd->param_size.value);
|
2009-12-21 09:43:50 +00:00
|
|
|
free(mtd->cdev.name);
|
2008-08-11 08:59:28 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|