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