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 | /* NB For use with inband tags.... |
| 36 | * We assume that the data buffer is of size total_bytes_per_chunk so |
| 37 | * that we can also use it to load the tags. |
| 38 | */ |
| 39 | int nandmtd2_write_chunk_tags(struct yaffs_dev *dev, int nand_chunk, |
| 40 | const u8 *data, |
| 41 | const struct yaffs_ext_tags *tags) |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 42 | { |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 43 | struct mtd_info *mtd = yaffs_dev_to_mtd(dev); |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 44 | struct mtd_oob_ops ops; |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 45 | |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 46 | int retval = 0; |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 47 | loff_t addr; |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 48 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 49 | struct yaffs_packed_tags2 pt; |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 50 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 51 | int packed_tags_size = |
| 52 | dev->param.no_tags_ecc ? sizeof(pt.t) : sizeof(pt); |
| 53 | void *packed_tags_ptr = |
| 54 | dev->param.no_tags_ecc ? (void *)&pt.t : (void *)&pt; |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 55 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 56 | yaffs_trace(YAFFS_TRACE_MTD, |
| 57 | "nandmtd2_write_chunk_tags chunk %d data %p tags %p", |
| 58 | nand_chunk, data, tags); |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 59 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 60 | addr = ((loff_t) nand_chunk) * dev->param.total_bytes_per_chunk; |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 61 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 62 | /* For yaffs2 writing there must be both data and tags. |
| 63 | * If we're using inband tags, then the tags are stuffed into |
| 64 | * the end of the data buffer. |
| 65 | */ |
| 66 | if (!data || !tags) |
| 67 | BUG(); |
| 68 | else if (dev->param.inband_tags) { |
| 69 | struct yaffs_packed_tags2_tags_only *pt2tp; |
| 70 | pt2tp = |
| 71 | (struct yaffs_packed_tags2_tags_only *)(data + |
| 72 | dev-> |
| 73 | data_bytes_per_chunk); |
| 74 | yaffs_pack_tags2_tags_only(pt2tp, tags); |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 75 | } else { |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 76 | yaffs_pack_tags2(&pt, tags, !dev->param.no_tags_ecc); |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 77 | } |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 78 | |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 79 | ops.mode = MTD_OPS_AUTO_OOB; |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 80 | ops.ooblen = (dev->param.inband_tags) ? 0 : packed_tags_size; |
| 81 | ops.len = dev->param.total_bytes_per_chunk; |
| 82 | ops.ooboffs = 0; |
| 83 | ops.datbuf = (u8 *) data; |
| 84 | ops.oobbuf = (dev->param.inband_tags) ? NULL : packed_tags_ptr; |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 85 | retval = mtd_write_oob(mtd, addr, &ops); |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 86 | |
| 87 | if (retval == 0) |
| 88 | return YAFFS_OK; |
| 89 | else |
| 90 | return YAFFS_FAIL; |
| 91 | } |
| 92 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 93 | int nandmtd2_read_chunk_tags(struct yaffs_dev *dev, int nand_chunk, |
| 94 | u8 *data, struct yaffs_ext_tags *tags) |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 95 | { |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 96 | struct mtd_info *mtd = yaffs_dev_to_mtd(dev); |
| 97 | u8 local_spare[128]; |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 98 | struct mtd_oob_ops ops; |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 99 | size_t dummy; |
| 100 | int retval = 0; |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 101 | int local_data = 0; |
| 102 | struct yaffs_packed_tags2 pt; |
| 103 | loff_t addr = ((loff_t) nand_chunk) * dev->param.total_bytes_per_chunk; |
| 104 | int packed_tags_size = |
| 105 | dev->param.no_tags_ecc ? sizeof(pt.t) : sizeof(pt); |
| 106 | void *packed_tags_ptr = |
| 107 | dev->param.no_tags_ecc ? (void *)&pt.t : (void *)&pt; |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 108 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 109 | yaffs_trace(YAFFS_TRACE_MTD, |
| 110 | "nandmtd2_read_chunk_tags chunk %d data %p tags %p", |
| 111 | nand_chunk, data, tags); |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 112 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 113 | if (dev->param.inband_tags) { |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 114 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 115 | if (!data) { |
| 116 | local_data = 1; |
| 117 | data = yaffs_get_temp_buffer(dev); |
| 118 | } |
| 119 | |
| 120 | } |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 121 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 122 | if (dev->param.inband_tags || (data && !tags)) |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 123 | retval = mtd_read(mtd, addr, dev->param.total_bytes_per_chunk, |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 124 | &dummy, data); |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 125 | else if (tags) { |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 126 | ops.mode = MTD_OPS_AUTO_OOB; |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 127 | ops.ooblen = packed_tags_size; |
| 128 | ops.len = data ? dev->data_bytes_per_chunk : packed_tags_size; |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 129 | ops.ooboffs = 0; |
| 130 | ops.datbuf = data; |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 131 | ops.oobbuf = local_spare; |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 132 | retval = mtd_read_oob(mtd, addr, &ops); |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 133 | } |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 134 | |
| 135 | if (dev->param.inband_tags) { |
| 136 | if (tags) { |
| 137 | struct yaffs_packed_tags2_tags_only *pt2tp; |
| 138 | pt2tp = |
| 139 | (struct yaffs_packed_tags2_tags_only *) |
| 140 | &data[dev->data_bytes_per_chunk]; |
| 141 | yaffs_unpack_tags2_tags_only(tags, pt2tp); |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 142 | } |
| 143 | } else { |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 144 | if (tags) { |
| 145 | memcpy(packed_tags_ptr, |
| 146 | local_spare, |
| 147 | packed_tags_size); |
| 148 | yaffs_unpack_tags2(tags, &pt, !dev->param.no_tags_ecc); |
| 149 | } |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 150 | } |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 151 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 152 | if (local_data) |
| 153 | yaffs_release_temp_buffer(dev, data); |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 154 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 155 | if (tags && retval == -EBADMSG |
| 156 | && tags->ecc_result == YAFFS_ECC_RESULT_NO_ERROR) { |
| 157 | tags->ecc_result = YAFFS_ECC_RESULT_UNFIXED; |
| 158 | dev->n_ecc_unfixed++; |
| 159 | } |
| 160 | if (tags && retval == -EUCLEAN |
| 161 | && tags->ecc_result == YAFFS_ECC_RESULT_NO_ERROR) { |
| 162 | tags->ecc_result = YAFFS_ECC_RESULT_FIXED; |
| 163 | dev->n_ecc_fixed++; |
| 164 | } |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 165 | if (retval == 0) |
| 166 | return YAFFS_OK; |
| 167 | else |
| 168 | return YAFFS_FAIL; |
| 169 | } |
| 170 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 171 | int nandmtd2_MarkNANDBlockBad(struct yaffs_dev *dev, int blockNo) |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 172 | { |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 173 | struct mtd_info *mtd = (struct mtd_info *)(dev->driver_context); |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 174 | int retval; |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 175 | |
| 176 | yaffs_trace(YAFFS_TRACE_MTD, |
| 177 | "nandmtd2_MarkNANDBlockBad %d", blockNo); |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 178 | |
| 179 | retval = |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 180 | mtd_block_markbad(mtd, |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 181 | blockNo * dev->param.chunks_per_block * |
| 182 | dev->data_bytes_per_chunk); |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 183 | |
| 184 | if (retval == 0) |
| 185 | return YAFFS_OK; |
| 186 | else |
| 187 | return YAFFS_FAIL; |
| 188 | |
| 189 | } |
| 190 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 191 | int nandmtd2_QueryNANDBlock(struct yaffs_dev *dev, int blockNo, |
| 192 | enum yaffs_block_state *state, u32 *sequenceNumber) |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 193 | { |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 194 | struct mtd_info *mtd = (struct mtd_info *)(dev->driver_context); |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 195 | int retval; |
| 196 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 197 | yaffs_trace(YAFFS_TRACE_MTD, "nandmtd2_QueryNANDBlock %d", blockNo); |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 198 | retval = |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 199 | mtd_block_isbad(mtd, |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 200 | blockNo * dev->param.chunks_per_block * |
| 201 | dev->data_bytes_per_chunk); |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 202 | |
| 203 | if (retval) { |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 204 | yaffs_trace(YAFFS_TRACE_MTD, "block is bad"); |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 205 | |
| 206 | *state = YAFFS_BLOCK_STATE_DEAD; |
| 207 | *sequenceNumber = 0; |
| 208 | } else { |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 209 | struct yaffs_ext_tags t; |
| 210 | nandmtd2_read_chunk_tags(dev, |
| 211 | blockNo * |
| 212 | dev->param.chunks_per_block, NULL, |
| 213 | &t); |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 214 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 215 | if (t.chunk_used) { |
| 216 | *sequenceNumber = t.seq_number; |
| 217 | *state = YAFFS_BLOCK_STATE_NEEDS_SCAN; |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 218 | } else { |
| 219 | *sequenceNumber = 0; |
| 220 | *state = YAFFS_BLOCK_STATE_EMPTY; |
| 221 | } |
| 222 | } |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 223 | yaffs_trace(YAFFS_TRACE_MTD, "block is bad seq %d state %d", |
| 224 | *sequenceNumber, *state); |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 225 | |
| 226 | if (retval == 0) |
| 227 | return YAFFS_OK; |
| 228 | else |
| 229 | return YAFFS_FAIL; |
| 230 | } |