Pantelis Antoniou | c663176 | 2013-03-14 05:32:52 +0000 | [diff] [blame] | 1 | /* |
| 2 | * dfu_nand.c -- DFU for NAND routines. |
| 3 | * |
| 4 | * Copyright (C) 2012-2013 Texas Instruments, Inc. |
| 5 | * |
| 6 | * Based on dfu_mmc.c which is: |
| 7 | * Copyright (C) 2012 Samsung Electronics |
| 8 | * author: Lukasz Majewski <l.majewski@samsung.com> |
| 9 | * |
Wolfgang Denk | 1a45966 | 2013-07-08 09:37:19 +0200 | [diff] [blame] | 10 | * SPDX-License-Identifier: GPL-2.0+ |
Pantelis Antoniou | c663176 | 2013-03-14 05:32:52 +0000 | [diff] [blame] | 11 | */ |
| 12 | |
| 13 | #include <common.h> |
| 14 | #include <malloc.h> |
| 15 | #include <errno.h> |
| 16 | #include <div64.h> |
| 17 | #include <dfu.h> |
| 18 | #include <linux/mtd/mtd.h> |
| 19 | #include <jffs2/load_kernel.h> |
| 20 | #include <nand.h> |
| 21 | |
Afzal Mohammed | 5a127c8 | 2013-09-18 01:14:50 +0530 | [diff] [blame] | 22 | static int nand_block_op(enum dfu_op op, struct dfu_entity *dfu, |
Pantelis Antoniou | c663176 | 2013-03-14 05:32:52 +0000 | [diff] [blame] | 23 | u64 offset, void *buf, long *len) |
| 24 | { |
| 25 | loff_t start, lim; |
| 26 | size_t count, actual; |
| 27 | int ret; |
| 28 | nand_info_t *nand; |
| 29 | |
| 30 | /* if buf == NULL return total size of the area */ |
| 31 | if (buf == NULL) { |
| 32 | *len = dfu->data.nand.size; |
| 33 | return 0; |
| 34 | } |
| 35 | |
| 36 | start = dfu->data.nand.start + offset + dfu->bad_skip; |
| 37 | lim = dfu->data.nand.start + dfu->data.nand.size - start; |
| 38 | count = *len; |
| 39 | |
| 40 | if (nand_curr_device < 0 || |
| 41 | nand_curr_device >= CONFIG_SYS_MAX_NAND_DEVICE || |
| 42 | !nand_info[nand_curr_device].name) { |
| 43 | printf("%s: invalid nand device\n", __func__); |
| 44 | return -1; |
| 45 | } |
| 46 | |
| 47 | nand = &nand_info[nand_curr_device]; |
| 48 | |
Heiko Schocher | a67cc37 | 2013-06-24 18:50:40 +0200 | [diff] [blame] | 49 | if (op == DFU_OP_READ) { |
Pantelis Antoniou | c663176 | 2013-03-14 05:32:52 +0000 | [diff] [blame] | 50 | ret = nand_read_skip_bad(nand, start, &count, &actual, |
| 51 | lim, buf); |
Heiko Schocher | a67cc37 | 2013-06-24 18:50:40 +0200 | [diff] [blame] | 52 | } else { |
| 53 | nand_erase_options_t opts; |
| 54 | |
| 55 | memset(&opts, 0, sizeof(opts)); |
| 56 | opts.offset = start; |
| 57 | opts.length = count; |
| 58 | opts.spread = 1; |
| 59 | opts.quiet = 1; |
| 60 | opts.lim = lim; |
| 61 | /* first erase */ |
| 62 | ret = nand_erase_opts(nand, &opts); |
| 63 | if (ret) |
| 64 | return ret; |
| 65 | /* then write */ |
Pantelis Antoniou | c663176 | 2013-03-14 05:32:52 +0000 | [diff] [blame] | 66 | ret = nand_write_skip_bad(nand, start, &count, &actual, |
| 67 | lim, buf, 0); |
Heiko Schocher | a67cc37 | 2013-06-24 18:50:40 +0200 | [diff] [blame] | 68 | } |
Pantelis Antoniou | c663176 | 2013-03-14 05:32:52 +0000 | [diff] [blame] | 69 | |
| 70 | if (ret != 0) { |
| 71 | printf("%s: nand_%s_skip_bad call failed at %llx!\n", |
| 72 | __func__, op == DFU_OP_READ ? "read" : "write", |
| 73 | start); |
| 74 | return ret; |
| 75 | } |
| 76 | |
| 77 | /* |
| 78 | * Find out where we stopped writing data. This can be deeper into |
| 79 | * the NAND than we expected due to having to skip bad blocks. So |
| 80 | * we must take this into account for the next write, if any. |
| 81 | */ |
| 82 | if (actual > count) |
| 83 | dfu->bad_skip += actual - count; |
| 84 | |
| 85 | return ret; |
| 86 | } |
| 87 | |
| 88 | static inline int nand_block_write(struct dfu_entity *dfu, |
| 89 | u64 offset, void *buf, long *len) |
| 90 | { |
| 91 | return nand_block_op(DFU_OP_WRITE, dfu, offset, buf, len); |
| 92 | } |
| 93 | |
| 94 | static inline int nand_block_read(struct dfu_entity *dfu, |
| 95 | u64 offset, void *buf, long *len) |
| 96 | { |
| 97 | return nand_block_op(DFU_OP_READ, dfu, offset, buf, len); |
| 98 | } |
| 99 | |
| 100 | static int dfu_write_medium_nand(struct dfu_entity *dfu, |
| 101 | u64 offset, void *buf, long *len) |
| 102 | { |
| 103 | int ret = -1; |
| 104 | |
| 105 | switch (dfu->layout) { |
| 106 | case DFU_RAW_ADDR: |
| 107 | ret = nand_block_write(dfu, offset, buf, len); |
| 108 | break; |
| 109 | default: |
| 110 | printf("%s: Layout (%s) not (yet) supported!\n", __func__, |
| 111 | dfu_get_layout(dfu->layout)); |
| 112 | } |
| 113 | |
| 114 | return ret; |
| 115 | } |
| 116 | |
| 117 | static int dfu_read_medium_nand(struct dfu_entity *dfu, u64 offset, void *buf, |
| 118 | long *len) |
| 119 | { |
| 120 | int ret = -1; |
| 121 | |
| 122 | switch (dfu->layout) { |
| 123 | case DFU_RAW_ADDR: |
| 124 | ret = nand_block_read(dfu, offset, buf, len); |
| 125 | break; |
| 126 | default: |
| 127 | printf("%s: Layout (%s) not (yet) supported!\n", __func__, |
| 128 | dfu_get_layout(dfu->layout)); |
| 129 | } |
| 130 | |
| 131 | return ret; |
| 132 | } |
| 133 | |
Heiko Schocher | 815c30b | 2013-07-25 06:43:11 +0200 | [diff] [blame] | 134 | static int dfu_flush_medium_nand(struct dfu_entity *dfu) |
| 135 | { |
| 136 | int ret = 0; |
| 137 | |
| 138 | /* in case of ubi partition, erase rest of the partition */ |
| 139 | if (dfu->data.nand.ubi) { |
| 140 | nand_info_t *nand; |
| 141 | nand_erase_options_t opts; |
| 142 | |
| 143 | if (nand_curr_device < 0 || |
| 144 | nand_curr_device >= CONFIG_SYS_MAX_NAND_DEVICE || |
| 145 | !nand_info[nand_curr_device].name) { |
| 146 | printf("%s: invalid nand device\n", __func__); |
| 147 | return -1; |
| 148 | } |
| 149 | |
| 150 | nand = &nand_info[nand_curr_device]; |
| 151 | |
| 152 | memset(&opts, 0, sizeof(opts)); |
| 153 | opts.offset = dfu->data.nand.start + dfu->offset + |
| 154 | dfu->bad_skip; |
| 155 | opts.length = dfu->data.nand.start + |
| 156 | dfu->data.nand.size - opts.offset; |
| 157 | ret = nand_erase_opts(nand, &opts); |
| 158 | if (ret != 0) |
| 159 | printf("Failure erase: %d\n", ret); |
| 160 | } |
| 161 | |
| 162 | return ret; |
| 163 | } |
| 164 | |
Pantelis Antoniou | c663176 | 2013-03-14 05:32:52 +0000 | [diff] [blame] | 165 | int dfu_fill_entity_nand(struct dfu_entity *dfu, char *s) |
| 166 | { |
| 167 | char *st; |
| 168 | int ret, dev, part; |
| 169 | |
Heiko Schocher | 815c30b | 2013-07-25 06:43:11 +0200 | [diff] [blame] | 170 | dfu->data.nand.ubi = 0; |
Pantelis Antoniou | c663176 | 2013-03-14 05:32:52 +0000 | [diff] [blame] | 171 | dfu->dev_type = DFU_DEV_NAND; |
| 172 | st = strsep(&s, " "); |
| 173 | if (!strcmp(st, "raw")) { |
| 174 | dfu->layout = DFU_RAW_ADDR; |
| 175 | dfu->data.nand.start = simple_strtoul(s, &s, 16); |
| 176 | s++; |
| 177 | dfu->data.nand.size = simple_strtoul(s, &s, 16); |
Heiko Schocher | 815c30b | 2013-07-25 06:43:11 +0200 | [diff] [blame] | 178 | } else if ((!strcmp(st, "part")) || (!strcmp(st, "partubi"))) { |
Pantelis Antoniou | c663176 | 2013-03-14 05:32:52 +0000 | [diff] [blame] | 179 | char mtd_id[32]; |
| 180 | struct mtd_device *mtd_dev; |
| 181 | u8 part_num; |
| 182 | struct part_info *pi; |
| 183 | |
| 184 | dfu->layout = DFU_RAW_ADDR; |
| 185 | |
| 186 | dev = simple_strtoul(s, &s, 10); |
| 187 | s++; |
| 188 | part = simple_strtoul(s, &s, 10); |
| 189 | |
| 190 | sprintf(mtd_id, "%s%d,%d", "nand", dev, part - 1); |
| 191 | printf("using id '%s'\n", mtd_id); |
| 192 | |
| 193 | mtdparts_init(); |
| 194 | |
| 195 | ret = find_dev_and_part(mtd_id, &mtd_dev, &part_num, &pi); |
| 196 | if (ret != 0) { |
| 197 | printf("Could not locate '%s'\n", mtd_id); |
| 198 | return -1; |
| 199 | } |
| 200 | |
| 201 | dfu->data.nand.start = pi->offset; |
| 202 | dfu->data.nand.size = pi->size; |
Heiko Schocher | 815c30b | 2013-07-25 06:43:11 +0200 | [diff] [blame] | 203 | if (!strcmp(st, "partubi")) |
| 204 | dfu->data.nand.ubi = 1; |
Pantelis Antoniou | c663176 | 2013-03-14 05:32:52 +0000 | [diff] [blame] | 205 | } else { |
| 206 | printf("%s: Memory layout (%s) not supported!\n", __func__, st); |
| 207 | return -1; |
| 208 | } |
| 209 | |
| 210 | dfu->read_medium = dfu_read_medium_nand; |
| 211 | dfu->write_medium = dfu_write_medium_nand; |
Heiko Schocher | 815c30b | 2013-07-25 06:43:11 +0200 | [diff] [blame] | 212 | dfu->flush_medium = dfu_flush_medium_nand; |
Pantelis Antoniou | c663176 | 2013-03-14 05:32:52 +0000 | [diff] [blame] | 213 | |
| 214 | /* initial state */ |
| 215 | dfu->inited = 0; |
| 216 | |
| 217 | return 0; |
| 218 | } |