2018-05-06 21:58:06 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0+
|
2013-03-14 05:32:52 +00:00
|
|
|
/*
|
|
|
|
* dfu_nand.c -- DFU for NAND routines.
|
|
|
|
*
|
|
|
|
* Copyright (C) 2012-2013 Texas Instruments, Inc.
|
|
|
|
*
|
|
|
|
* Based on dfu_mmc.c which is:
|
|
|
|
* Copyright (C) 2012 Samsung Electronics
|
|
|
|
* author: Lukasz Majewski <l.majewski@samsung.com>
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <common.h>
|
2020-05-10 17:40:05 +00:00
|
|
|
#include <log.h>
|
2013-03-14 05:32:52 +00:00
|
|
|
#include <malloc.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <div64.h>
|
|
|
|
#include <dfu.h>
|
|
|
|
#include <linux/mtd/mtd.h>
|
|
|
|
#include <jffs2/load_kernel.h>
|
|
|
|
#include <nand.h>
|
|
|
|
|
2013-09-17 19:44:50 +00:00
|
|
|
static int nand_block_op(enum dfu_op op, struct dfu_entity *dfu,
|
2013-03-14 05:32:52 +00:00
|
|
|
u64 offset, void *buf, long *len)
|
|
|
|
{
|
|
|
|
loff_t start, lim;
|
|
|
|
size_t count, actual;
|
|
|
|
int ret;
|
2016-05-30 18:57:54 +00:00
|
|
|
struct mtd_info *mtd;
|
2013-03-14 05:32:52 +00:00
|
|
|
|
|
|
|
/* if buf == NULL return total size of the area */
|
|
|
|
if (buf == NULL) {
|
|
|
|
*len = dfu->data.nand.size;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
start = dfu->data.nand.start + offset + dfu->bad_skip;
|
|
|
|
lim = dfu->data.nand.start + dfu->data.nand.size - start;
|
|
|
|
count = *len;
|
|
|
|
|
2017-06-27 00:12:53 +00:00
|
|
|
mtd = get_nand_dev_by_index(nand_curr_device);
|
|
|
|
|
2013-03-14 05:32:52 +00:00
|
|
|
if (nand_curr_device < 0 ||
|
|
|
|
nand_curr_device >= CONFIG_SYS_MAX_NAND_DEVICE ||
|
2017-06-27 00:12:53 +00:00
|
|
|
!mtd) {
|
2013-03-14 05:32:52 +00:00
|
|
|
printf("%s: invalid nand device\n", __func__);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2013-06-24 16:50:40 +00:00
|
|
|
if (op == DFU_OP_READ) {
|
2016-05-30 18:57:54 +00:00
|
|
|
ret = nand_read_skip_bad(mtd, start, &count, &actual,
|
|
|
|
lim, buf);
|
2013-06-24 16:50:40 +00:00
|
|
|
} else {
|
|
|
|
nand_erase_options_t opts;
|
2019-12-16 15:27:57 +00:00
|
|
|
int write_flags = WITH_WR_VERIFY;
|
2013-06-24 16:50:40 +00:00
|
|
|
|
|
|
|
memset(&opts, 0, sizeof(opts));
|
|
|
|
opts.offset = start;
|
|
|
|
opts.length = count;
|
|
|
|
opts.spread = 1;
|
|
|
|
opts.quiet = 1;
|
|
|
|
opts.lim = lim;
|
|
|
|
/* first erase */
|
2016-05-30 18:57:54 +00:00
|
|
|
ret = nand_erase_opts(mtd, &opts);
|
2013-06-24 16:50:40 +00:00
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
/* then write */
|
2019-12-16 15:27:57 +00:00
|
|
|
#ifdef CONFIG_DFU_NAND_TRIMFFS
|
|
|
|
if (dfu->data.nand.ubi)
|
|
|
|
write_flags |= WITH_DROP_FFS;
|
|
|
|
#endif
|
2016-05-30 18:57:54 +00:00
|
|
|
ret = nand_write_skip_bad(mtd, start, &count, &actual,
|
2019-12-16 15:27:57 +00:00
|
|
|
lim, buf, write_flags);
|
2013-06-24 16:50:40 +00:00
|
|
|
}
|
2013-03-14 05:32:52 +00:00
|
|
|
|
|
|
|
if (ret != 0) {
|
|
|
|
printf("%s: nand_%s_skip_bad call failed at %llx!\n",
|
|
|
|
__func__, op == DFU_OP_READ ? "read" : "write",
|
|
|
|
start);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Find out where we stopped writing data. This can be deeper into
|
|
|
|
* the NAND than we expected due to having to skip bad blocks. So
|
|
|
|
* we must take this into account for the next write, if any.
|
|
|
|
*/
|
|
|
|
if (actual > count)
|
|
|
|
dfu->bad_skip += actual - count;
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline int nand_block_write(struct dfu_entity *dfu,
|
|
|
|
u64 offset, void *buf, long *len)
|
|
|
|
{
|
|
|
|
return nand_block_op(DFU_OP_WRITE, dfu, offset, buf, len);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline int nand_block_read(struct dfu_entity *dfu,
|
|
|
|
u64 offset, void *buf, long *len)
|
|
|
|
{
|
|
|
|
return nand_block_op(DFU_OP_READ, dfu, offset, buf, len);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int dfu_write_medium_nand(struct dfu_entity *dfu,
|
|
|
|
u64 offset, void *buf, long *len)
|
|
|
|
{
|
|
|
|
int ret = -1;
|
|
|
|
|
|
|
|
switch (dfu->layout) {
|
|
|
|
case DFU_RAW_ADDR:
|
|
|
|
ret = nand_block_write(dfu, offset, buf, len);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
printf("%s: Layout (%s) not (yet) supported!\n", __func__,
|
|
|
|
dfu_get_layout(dfu->layout));
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2017-07-19 14:39:23 +00:00
|
|
|
int dfu_get_medium_size_nand(struct dfu_entity *dfu, u64 *size)
|
2014-06-11 18:47:27 +00:00
|
|
|
{
|
2017-07-19 14:39:22 +00:00
|
|
|
*size = dfu->data.nand.size;
|
|
|
|
|
|
|
|
return 0;
|
2014-06-11 18:47:27 +00:00
|
|
|
}
|
|
|
|
|
2013-03-14 05:32:52 +00:00
|
|
|
static int dfu_read_medium_nand(struct dfu_entity *dfu, u64 offset, void *buf,
|
|
|
|
long *len)
|
|
|
|
{
|
|
|
|
int ret = -1;
|
|
|
|
|
|
|
|
switch (dfu->layout) {
|
|
|
|
case DFU_RAW_ADDR:
|
|
|
|
ret = nand_block_read(dfu, offset, buf, len);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
printf("%s: Layout (%s) not (yet) supported!\n", __func__,
|
|
|
|
dfu_get_layout(dfu->layout));
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2013-07-25 04:43:11 +00:00
|
|
|
static int dfu_flush_medium_nand(struct dfu_entity *dfu)
|
|
|
|
{
|
|
|
|
int ret = 0;
|
2016-06-07 06:55:44 +00:00
|
|
|
u64 off;
|
2013-07-25 04:43:11 +00:00
|
|
|
|
|
|
|
/* in case of ubi partition, erase rest of the partition */
|
|
|
|
if (dfu->data.nand.ubi) {
|
2017-06-27 00:12:53 +00:00
|
|
|
struct mtd_info *mtd = get_nand_dev_by_index(nand_curr_device);
|
2013-07-25 04:43:11 +00:00
|
|
|
nand_erase_options_t opts;
|
|
|
|
|
|
|
|
if (nand_curr_device < 0 ||
|
|
|
|
nand_curr_device >= CONFIG_SYS_MAX_NAND_DEVICE ||
|
2017-06-27 00:12:53 +00:00
|
|
|
!mtd) {
|
2013-07-25 04:43:11 +00:00
|
|
|
printf("%s: invalid nand device\n", __func__);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
memset(&opts, 0, sizeof(opts));
|
2016-06-07 06:55:44 +00:00
|
|
|
off = dfu->offset;
|
|
|
|
if ((off & (mtd->erasesize - 1)) != 0) {
|
|
|
|
/*
|
|
|
|
* last write ended with unaligned length
|
|
|
|
* sector is erased, jump to next
|
|
|
|
*/
|
|
|
|
off = off & ~((mtd->erasesize - 1));
|
|
|
|
off += mtd->erasesize;
|
|
|
|
}
|
|
|
|
opts.offset = dfu->data.nand.start + off +
|
2013-07-25 04:43:11 +00:00
|
|
|
dfu->bad_skip;
|
|
|
|
opts.length = dfu->data.nand.start +
|
|
|
|
dfu->data.nand.size - opts.offset;
|
2016-05-30 18:57:54 +00:00
|
|
|
ret = nand_erase_opts(mtd, &opts);
|
2013-07-25 04:43:11 +00:00
|
|
|
if (ret != 0)
|
|
|
|
printf("Failure erase: %d\n", ret);
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2014-04-11 05:59:47 +00:00
|
|
|
unsigned int dfu_polltimeout_nand(struct dfu_entity *dfu)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* Currently, Poll Timeout != 0 is only needed on nand
|
|
|
|
* ubi partition, as the not used sectors need an erase
|
|
|
|
*/
|
|
|
|
if (dfu->data.nand.ubi)
|
|
|
|
return DFU_MANIFEST_POLL_TIMEOUT;
|
|
|
|
|
|
|
|
return DFU_DEFAULT_POLL_TIMEOUT;
|
|
|
|
}
|
|
|
|
|
2014-06-11 22:03:33 +00:00
|
|
|
int dfu_fill_entity_nand(struct dfu_entity *dfu, char *devstr, char *s)
|
2013-03-14 05:32:52 +00:00
|
|
|
{
|
|
|
|
char *st;
|
|
|
|
int ret, dev, part;
|
|
|
|
|
2013-07-25 04:43:11 +00:00
|
|
|
dfu->data.nand.ubi = 0;
|
2013-03-14 05:32:52 +00:00
|
|
|
dfu->dev_type = DFU_DEV_NAND;
|
|
|
|
st = strsep(&s, " ");
|
|
|
|
if (!strcmp(st, "raw")) {
|
|
|
|
dfu->layout = DFU_RAW_ADDR;
|
2021-07-24 15:03:29 +00:00
|
|
|
dfu->data.nand.start = hextoul(s, &s);
|
2013-03-14 05:32:52 +00:00
|
|
|
s++;
|
2021-07-24 15:03:29 +00:00
|
|
|
dfu->data.nand.size = hextoul(s, &s);
|
2013-07-25 04:43:11 +00:00
|
|
|
} else if ((!strcmp(st, "part")) || (!strcmp(st, "partubi"))) {
|
2013-03-14 05:32:52 +00:00
|
|
|
char mtd_id[32];
|
|
|
|
struct mtd_device *mtd_dev;
|
|
|
|
u8 part_num;
|
|
|
|
struct part_info *pi;
|
|
|
|
|
|
|
|
dfu->layout = DFU_RAW_ADDR;
|
|
|
|
|
2021-07-24 15:03:30 +00:00
|
|
|
dev = dectoul(s, &s);
|
2013-03-14 05:32:52 +00:00
|
|
|
s++;
|
2021-07-24 15:03:30 +00:00
|
|
|
part = dectoul(s, &s);
|
2013-03-14 05:32:52 +00:00
|
|
|
|
|
|
|
sprintf(mtd_id, "%s%d,%d", "nand", dev, part - 1);
|
2019-08-27 18:28:19 +00:00
|
|
|
debug("using id '%s'\n", mtd_id);
|
2013-03-14 05:32:52 +00:00
|
|
|
|
|
|
|
mtdparts_init();
|
|
|
|
|
|
|
|
ret = find_dev_and_part(mtd_id, &mtd_dev, &part_num, &pi);
|
|
|
|
if (ret != 0) {
|
|
|
|
printf("Could not locate '%s'\n", mtd_id);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
dfu->data.nand.start = pi->offset;
|
|
|
|
dfu->data.nand.size = pi->size;
|
2013-07-25 04:43:11 +00:00
|
|
|
if (!strcmp(st, "partubi"))
|
|
|
|
dfu->data.nand.ubi = 1;
|
2013-03-14 05:32:52 +00:00
|
|
|
} else {
|
|
|
|
printf("%s: Memory layout (%s) not supported!\n", __func__, st);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2014-06-11 18:47:27 +00:00
|
|
|
dfu->get_medium_size = dfu_get_medium_size_nand;
|
2013-03-14 05:32:52 +00:00
|
|
|
dfu->read_medium = dfu_read_medium_nand;
|
|
|
|
dfu->write_medium = dfu_write_medium_nand;
|
2013-07-25 04:43:11 +00:00
|
|
|
dfu->flush_medium = dfu_flush_medium_nand;
|
2014-04-11 05:59:47 +00:00
|
|
|
dfu->poll_timeout = dfu_polltimeout_nand;
|
2013-03-14 05:32:52 +00:00
|
|
|
|
|
|
|
/* initial state */
|
|
|
|
dfu->inited = 0;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|