William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 1 | /* |
| 2 | * YAFFS: Yet Another Flash File System. A NAND-flash specific file system. |
| 3 | * |
| 4 | * Copyright (C) 2002-2007 Aleph One Ltd. |
| 5 | * for Toby Churchill Ltd and Brightstar Engineering |
| 6 | * |
| 7 | * Created by Charles Manning <charles@aleph1.co.uk> |
| 8 | * |
| 9 | * This program is free software; you can redistribute it and/or modify |
| 10 | * it under the terms of the GNU General Public License version 2 as |
| 11 | * published by the Free Software Foundation. |
| 12 | */ |
| 13 | |
| 14 | /* mtd interface for YAFFS2 */ |
| 15 | |
William Juul | e24ebad | 2007-11-15 12:23:57 +0100 | [diff] [blame] | 16 | /* XXX U-BOOT XXX */ |
Simon Glass | c06c1be | 2020-05-10 11:40:08 -0600 | [diff] [blame] | 17 | #include <linux/bug.h> |
Masahiro Yamada | 56a931c | 2016-09-21 11:28:55 +0900 | [diff] [blame] | 18 | #include <linux/errno.h> |
William Juul | e24ebad | 2007-11-15 12:23:57 +0100 | [diff] [blame] | 19 | |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 20 | #include "yportenv.h" |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 21 | #include "yaffs_trace.h" |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 22 | |
| 23 | #include "yaffs_mtdif2.h" |
| 24 | |
Masahiro Yamada | adae2ec | 2016-09-21 11:28:53 +0900 | [diff] [blame] | 25 | #include <linux/mtd/mtd.h> |
| 26 | #include <linux/types.h> |
| 27 | #include <linux/time.h> |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 28 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 29 | #include "yaffs_trace.h" |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 30 | #include "yaffs_packedtags2.h" |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 31 | |
| 32 | #define yaffs_dev_to_mtd(dev) ((struct mtd_info *)((dev)->driver_context)) |
| 33 | #define yaffs_dev_to_lc(dev) ((struct yaffs_linux_context *)((dev)->os_context)) |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 34 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 35 | |
| 36 | /* NB For use with inband tags.... |
| 37 | * We assume that the data buffer is of size total_bytes_per_chunk so |
| 38 | * that we can also use it to load the tags. |
| 39 | */ |
| 40 | int nandmtd2_write_chunk_tags(struct yaffs_dev *dev, int nand_chunk, |
| 41 | const u8 *data, |
| 42 | const struct yaffs_ext_tags *tags) |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 43 | { |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 44 | struct mtd_info *mtd = yaffs_dev_to_mtd(dev); |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 45 | struct mtd_oob_ops ops; |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 46 | |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 47 | int retval = 0; |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 48 | loff_t addr; |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 49 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 50 | struct yaffs_packed_tags2 pt; |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 51 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 52 | int packed_tags_size = |
| 53 | dev->param.no_tags_ecc ? sizeof(pt.t) : sizeof(pt); |
| 54 | void *packed_tags_ptr = |
| 55 | dev->param.no_tags_ecc ? (void *)&pt.t : (void *)&pt; |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 56 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 57 | yaffs_trace(YAFFS_TRACE_MTD, |
| 58 | "nandmtd2_write_chunk_tags chunk %d data %p tags %p", |
| 59 | nand_chunk, data, tags); |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 60 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 61 | addr = ((loff_t) nand_chunk) * dev->param.total_bytes_per_chunk; |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 62 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 63 | /* For yaffs2 writing there must be both data and tags. |
| 64 | * If we're using inband tags, then the tags are stuffed into |
| 65 | * the end of the data buffer. |
| 66 | */ |
| 67 | if (!data || !tags) |
| 68 | BUG(); |
| 69 | else if (dev->param.inband_tags) { |
| 70 | struct yaffs_packed_tags2_tags_only *pt2tp; |
| 71 | pt2tp = |
| 72 | (struct yaffs_packed_tags2_tags_only *)(data + |
| 73 | dev-> |
| 74 | data_bytes_per_chunk); |
| 75 | yaffs_pack_tags2_tags_only(pt2tp, tags); |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 76 | } else { |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 77 | yaffs_pack_tags2(&pt, tags, !dev->param.no_tags_ecc); |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 78 | } |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 79 | |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 80 | ops.mode = MTD_OPS_AUTO_OOB; |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 81 | ops.ooblen = (dev->param.inband_tags) ? 0 : packed_tags_size; |
| 82 | ops.len = dev->param.total_bytes_per_chunk; |
| 83 | ops.ooboffs = 0; |
| 84 | ops.datbuf = (u8 *) data; |
| 85 | ops.oobbuf = (dev->param.inband_tags) ? NULL : packed_tags_ptr; |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 86 | retval = mtd_write_oob(mtd, addr, &ops); |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 87 | |
| 88 | if (retval == 0) |
| 89 | return YAFFS_OK; |
| 90 | else |
| 91 | return YAFFS_FAIL; |
| 92 | } |
| 93 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 94 | int nandmtd2_read_chunk_tags(struct yaffs_dev *dev, int nand_chunk, |
| 95 | u8 *data, struct yaffs_ext_tags *tags) |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 96 | { |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 97 | struct mtd_info *mtd = yaffs_dev_to_mtd(dev); |
| 98 | u8 local_spare[128]; |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 99 | struct mtd_oob_ops ops; |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 100 | size_t dummy; |
| 101 | int retval = 0; |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 102 | int local_data = 0; |
| 103 | struct yaffs_packed_tags2 pt; |
| 104 | loff_t addr = ((loff_t) nand_chunk) * dev->param.total_bytes_per_chunk; |
| 105 | int packed_tags_size = |
| 106 | dev->param.no_tags_ecc ? sizeof(pt.t) : sizeof(pt); |
| 107 | void *packed_tags_ptr = |
| 108 | dev->param.no_tags_ecc ? (void *)&pt.t : (void *)&pt; |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 109 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 110 | yaffs_trace(YAFFS_TRACE_MTD, |
| 111 | "nandmtd2_read_chunk_tags chunk %d data %p tags %p", |
| 112 | nand_chunk, data, tags); |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 113 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 114 | if (dev->param.inband_tags) { |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 115 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 116 | if (!data) { |
| 117 | local_data = 1; |
| 118 | data = yaffs_get_temp_buffer(dev); |
| 119 | } |
| 120 | |
| 121 | } |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 122 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 123 | if (dev->param.inband_tags || (data && !tags)) |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 124 | retval = mtd_read(mtd, addr, dev->param.total_bytes_per_chunk, |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 125 | &dummy, data); |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 126 | else if (tags) { |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 127 | ops.mode = MTD_OPS_AUTO_OOB; |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 128 | ops.ooblen = packed_tags_size; |
| 129 | ops.len = data ? dev->data_bytes_per_chunk : packed_tags_size; |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 130 | ops.ooboffs = 0; |
| 131 | ops.datbuf = data; |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 132 | ops.oobbuf = local_spare; |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 133 | retval = mtd_read_oob(mtd, addr, &ops); |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 134 | } |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 135 | |
| 136 | if (dev->param.inband_tags) { |
| 137 | if (tags) { |
| 138 | struct yaffs_packed_tags2_tags_only *pt2tp; |
| 139 | pt2tp = |
| 140 | (struct yaffs_packed_tags2_tags_only *) |
| 141 | &data[dev->data_bytes_per_chunk]; |
| 142 | yaffs_unpack_tags2_tags_only(tags, pt2tp); |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 143 | } |
| 144 | } else { |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 145 | if (tags) { |
| 146 | memcpy(packed_tags_ptr, |
| 147 | local_spare, |
| 148 | packed_tags_size); |
| 149 | yaffs_unpack_tags2(tags, &pt, !dev->param.no_tags_ecc); |
| 150 | } |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 151 | } |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 152 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 153 | if (local_data) |
| 154 | yaffs_release_temp_buffer(dev, data); |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 155 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 156 | if (tags && retval == -EBADMSG |
| 157 | && tags->ecc_result == YAFFS_ECC_RESULT_NO_ERROR) { |
| 158 | tags->ecc_result = YAFFS_ECC_RESULT_UNFIXED; |
| 159 | dev->n_ecc_unfixed++; |
| 160 | } |
| 161 | if (tags && retval == -EUCLEAN |
| 162 | && tags->ecc_result == YAFFS_ECC_RESULT_NO_ERROR) { |
| 163 | tags->ecc_result = YAFFS_ECC_RESULT_FIXED; |
| 164 | dev->n_ecc_fixed++; |
| 165 | } |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 166 | if (retval == 0) |
| 167 | return YAFFS_OK; |
| 168 | else |
| 169 | return YAFFS_FAIL; |
| 170 | } |
| 171 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 172 | |
| 173 | int nandmtd2_MarkNANDBlockBad(struct yaffs_dev *dev, int blockNo) |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 174 | { |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 175 | struct mtd_info *mtd = (struct mtd_info *)(dev->driver_context); |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 176 | int retval; |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 177 | |
| 178 | yaffs_trace(YAFFS_TRACE_MTD, |
| 179 | "nandmtd2_MarkNANDBlockBad %d", blockNo); |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 180 | |
| 181 | retval = |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 182 | mtd_block_markbad(mtd, |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 183 | blockNo * dev->param.chunks_per_block * |
| 184 | dev->data_bytes_per_chunk); |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 185 | |
| 186 | if (retval == 0) |
| 187 | return YAFFS_OK; |
| 188 | else |
| 189 | return YAFFS_FAIL; |
| 190 | |
| 191 | } |
| 192 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 193 | int nandmtd2_QueryNANDBlock(struct yaffs_dev *dev, int blockNo, |
| 194 | enum yaffs_block_state *state, u32 *sequenceNumber) |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 195 | { |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 196 | struct mtd_info *mtd = (struct mtd_info *)(dev->driver_context); |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 197 | int retval; |
| 198 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 199 | yaffs_trace(YAFFS_TRACE_MTD, "nandmtd2_QueryNANDBlock %d", blockNo); |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 200 | retval = |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 201 | mtd_block_isbad(mtd, |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 202 | blockNo * dev->param.chunks_per_block * |
| 203 | dev->data_bytes_per_chunk); |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 204 | |
| 205 | if (retval) { |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 206 | yaffs_trace(YAFFS_TRACE_MTD, "block is bad"); |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 207 | |
| 208 | *state = YAFFS_BLOCK_STATE_DEAD; |
| 209 | *sequenceNumber = 0; |
| 210 | } else { |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 211 | struct yaffs_ext_tags t; |
| 212 | nandmtd2_read_chunk_tags(dev, |
| 213 | blockNo * |
| 214 | dev->param.chunks_per_block, NULL, |
| 215 | &t); |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 216 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 217 | if (t.chunk_used) { |
| 218 | *sequenceNumber = t.seq_number; |
| 219 | *state = YAFFS_BLOCK_STATE_NEEDS_SCAN; |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 220 | } else { |
| 221 | *sequenceNumber = 0; |
| 222 | *state = YAFFS_BLOCK_STATE_EMPTY; |
| 223 | } |
| 224 | } |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 225 | yaffs_trace(YAFFS_TRACE_MTD, "block is bad seq %d state %d", |
| 226 | *sequenceNumber, *state); |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 227 | |
| 228 | if (retval == 0) |
| 229 | return YAFFS_OK; |
| 230 | else |
| 231 | return YAFFS_FAIL; |
| 232 | } |