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