Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 1 | /* |
| 2 | * YAFFS: Yet Another Flash File System. A NAND-flash specific file system. |
| 3 | * |
| 4 | * Copyright (C) 2002-2011 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 | #include "yportenv.h" |
| 15 | #include "yaffs_guts.h" |
Simon Glass | 9bc1564 | 2020-02-03 07:36:16 -0700 | [diff] [blame] | 16 | #include <malloc.h> |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 17 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 18 | #include "yaffs_nandif.h" |
| 19 | #include "yaffs_packedtags2.h" |
| 20 | |
| 21 | #include "yramsim.h" |
| 22 | |
| 23 | #include "yaffs_trace.h" |
| 24 | #include "yaffsfs.h" |
| 25 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 26 | /* NB For use with inband tags.... |
| 27 | * We assume that the data buffer is of size totalBytersPerChunk so that |
| 28 | * we can also use it to load the tags. |
| 29 | */ |
| 30 | int ynandif_WriteChunkWithTagsToNAND(struct yaffs_dev *dev, int nand_chunk, |
| 31 | const u8 *data, |
| 32 | const struct yaffs_ext_tags *tags) |
| 33 | { |
| 34 | |
| 35 | int retval = 0; |
| 36 | struct yaffs_packed_tags2 pt; |
| 37 | void *spare; |
| 38 | unsigned spareSize = 0; |
| 39 | struct ynandif_Geometry *geometry = (struct ynandif_Geometry *)(dev->driver_context); |
| 40 | |
| 41 | yaffs_trace(YAFFS_TRACE_MTD, |
| 42 | "nandmtd2_WriteChunkWithTagsToNAND chunk %d data %p tags %p", |
| 43 | nand_chunk, data, tags); |
| 44 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 45 | /* For yaffs2 writing there must be both data and tags. |
| 46 | * If we're using inband tags, then the tags are stuffed into |
| 47 | * the end of the data buffer. |
| 48 | */ |
| 49 | |
| 50 | if (dev->param.inband_tags) { |
| 51 | struct yaffs_packed_tags2_tags_only *pt2tp; |
| 52 | |
| 53 | pt2tp = (struct yaffs_packed_tags2_tags_only *) |
| 54 | (data + dev->data_bytes_per_chunk); |
| 55 | yaffs_pack_tags2_tags_only(pt2tp, tags); |
| 56 | spare = NULL; |
| 57 | spareSize = 0; |
| 58 | } else { |
| 59 | yaffs_pack_tags2(&pt, tags, !dev->param.no_tags_ecc); |
| 60 | spare = &pt; |
| 61 | spareSize = sizeof(struct yaffs_packed_tags2); |
| 62 | } |
| 63 | |
| 64 | retval = geometry->writeChunk(dev, nand_chunk, |
| 65 | data, dev->param.total_bytes_per_chunk, |
| 66 | spare, spareSize); |
| 67 | |
| 68 | return retval; |
| 69 | } |
| 70 | |
| 71 | int ynandif_ReadChunkWithTagsFromNAND(struct yaffs_dev *dev, int nand_chunk, |
| 72 | u8 *data, struct yaffs_ext_tags *tags) |
| 73 | { |
| 74 | struct yaffs_packed_tags2 pt; |
| 75 | int localData = 0; |
| 76 | void *spare = NULL; |
| 77 | unsigned spareSize; |
| 78 | int retval = 0; |
| 79 | int eccStatus; /* 0 = ok, 1 = fixed, -1 = unfixed */ |
| 80 | struct ynandif_Geometry *geometry = (struct ynandif_Geometry *)(dev->driver_context); |
| 81 | |
| 82 | yaffs_trace(YAFFS_TRACE_MTD, |
| 83 | "nandmtd2_ReadChunkWithTagsFromNAND chunk %d data %p tags %p", |
| 84 | nand_chunk, data, tags); |
| 85 | |
| 86 | if (!tags) { |
| 87 | spare = NULL; |
| 88 | spareSize = 0; |
| 89 | } else if (dev->param.inband_tags) { |
| 90 | |
| 91 | if (!data) { |
| 92 | localData = 1; |
| 93 | data = yaffs_get_temp_buffer(dev); |
| 94 | } |
| 95 | spare = NULL; |
| 96 | spareSize = 0; |
| 97 | } else { |
| 98 | spare = &pt; |
| 99 | spareSize = sizeof(struct yaffs_packed_tags2); |
| 100 | } |
| 101 | |
| 102 | retval = geometry->readChunk(dev, nand_chunk, |
| 103 | data, |
| 104 | data ? dev->param.total_bytes_per_chunk : 0, |
| 105 | spare, spareSize, |
| 106 | &eccStatus); |
| 107 | |
| 108 | if (dev->param.inband_tags) { |
| 109 | if (tags) { |
| 110 | struct yaffs_packed_tags2_tags_only *pt2tp; |
| 111 | pt2tp = (struct yaffs_packed_tags2_tags_only *) |
| 112 | &data[dev->data_bytes_per_chunk]; |
| 113 | yaffs_unpack_tags2_tags_only(tags, pt2tp); |
| 114 | } |
| 115 | } else { |
| 116 | if (tags) |
| 117 | yaffs_unpack_tags2(tags, &pt, !dev->param.no_tags_ecc); |
| 118 | } |
| 119 | |
| 120 | if (tags && tags->chunk_used) { |
| 121 | if (eccStatus < 0 || |
| 122 | tags->ecc_result == YAFFS_ECC_RESULT_UNFIXED) |
| 123 | tags->ecc_result = YAFFS_ECC_RESULT_UNFIXED; |
| 124 | else if (eccStatus > 0 || |
| 125 | tags->ecc_result == YAFFS_ECC_RESULT_FIXED) |
| 126 | tags->ecc_result = YAFFS_ECC_RESULT_FIXED; |
| 127 | else |
| 128 | tags->ecc_result = YAFFS_ECC_RESULT_NO_ERROR; |
| 129 | } |
| 130 | |
| 131 | if (localData) |
| 132 | yaffs_release_temp_buffer(dev, data); |
| 133 | |
| 134 | return retval; |
| 135 | } |
| 136 | |
| 137 | int ynandif_MarkNANDBlockBad(struct yaffs_dev *dev, int blockId) |
| 138 | { |
| 139 | struct ynandif_Geometry *geometry = (struct ynandif_Geometry *)(dev->driver_context); |
| 140 | |
| 141 | return geometry->markBlockBad(dev, blockId); |
| 142 | } |
| 143 | |
| 144 | int ynandif_EraseBlockInNAND(struct yaffs_dev *dev, int blockId) |
| 145 | { |
| 146 | struct ynandif_Geometry *geometry = (struct ynandif_Geometry *)(dev->driver_context); |
| 147 | |
| 148 | return geometry->eraseBlock(dev, blockId); |
| 149 | |
| 150 | } |
| 151 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 152 | static int ynandif_IsBlockOk(struct yaffs_dev *dev, int blockId) |
| 153 | { |
| 154 | struct ynandif_Geometry *geometry = (struct ynandif_Geometry *)(dev->driver_context); |
| 155 | |
| 156 | return geometry->checkBlockOk(dev, blockId); |
| 157 | } |
| 158 | |
| 159 | int ynandif_QueryNANDBlock(struct yaffs_dev *dev, int blockId, |
| 160 | enum yaffs_block_state *state, u32 *seq_number) |
| 161 | { |
| 162 | unsigned chunkNo; |
| 163 | struct yaffs_ext_tags tags; |
| 164 | |
| 165 | *seq_number = 0; |
| 166 | |
| 167 | chunkNo = blockId * dev->param.chunks_per_block; |
| 168 | |
| 169 | if (!ynandif_IsBlockOk(dev, blockId)) { |
| 170 | *state = YAFFS_BLOCK_STATE_DEAD; |
| 171 | } else { |
| 172 | ynandif_ReadChunkWithTagsFromNAND(dev, chunkNo, NULL, &tags); |
| 173 | |
| 174 | if (!tags.chunk_used) { |
| 175 | *state = YAFFS_BLOCK_STATE_EMPTY; |
| 176 | } else { |
| 177 | *state = YAFFS_BLOCK_STATE_NEEDS_SCAN; |
| 178 | *seq_number = tags.seq_number; |
| 179 | } |
| 180 | } |
| 181 | |
| 182 | return YAFFS_OK; |
| 183 | } |
| 184 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 185 | int ynandif_InitialiseNAND(struct yaffs_dev *dev) |
| 186 | { |
| 187 | struct ynandif_Geometry *geometry = (struct ynandif_Geometry *)(dev->driver_context); |
| 188 | |
| 189 | geometry->initialise(dev); |
| 190 | |
| 191 | return YAFFS_OK; |
| 192 | } |
| 193 | |
| 194 | int ynandif_Deinitialise_flash_fn(struct yaffs_dev *dev) |
| 195 | { |
| 196 | struct ynandif_Geometry *geometry = (struct ynandif_Geometry *)(dev->driver_context); |
| 197 | |
| 198 | geometry->deinitialise(dev); |
| 199 | |
| 200 | return YAFFS_OK; |
| 201 | } |
| 202 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 203 | struct yaffs_dev * |
| 204 | yaffs_add_dev_from_geometry(const YCHAR *name, |
| 205 | const struct ynandif_Geometry *geometry) |
| 206 | { |
| 207 | YCHAR *clonedName = malloc(sizeof(YCHAR) * |
| 208 | (strnlen(name, YAFFS_MAX_NAME_LENGTH)+1)); |
| 209 | struct yaffs_dev *dev = malloc(sizeof(struct yaffs_dev)); |
| 210 | struct yaffs_param *param; |
| 211 | |
| 212 | if (dev && clonedName) { |
| 213 | memset(dev, 0, sizeof(struct yaffs_dev)); |
| 214 | strcpy(clonedName, name); |
| 215 | |
| 216 | param = &dev->param; |
| 217 | |
| 218 | param->name = clonedName; |
| 219 | param->write_chunk_tags_fn = ynandif_WriteChunkWithTagsToNAND; |
| 220 | param->read_chunk_tags_fn = ynandif_ReadChunkWithTagsFromNAND; |
| 221 | param->erase_fn = ynandif_EraseBlockInNAND; |
| 222 | param->initialise_flash_fn = ynandif_InitialiseNAND; |
| 223 | param->query_block_fn = ynandif_QueryNANDBlock; |
| 224 | param->bad_block_fn = ynandif_MarkNANDBlockBad; |
| 225 | param->n_caches = 20; |
| 226 | param->start_block = geometry->start_block; |
| 227 | param->end_block = geometry->end_block; |
| 228 | param->total_bytes_per_chunk = geometry->dataSize; |
| 229 | param->spare_bytes_per_chunk = geometry->spareSize; |
| 230 | param->inband_tags = geometry->inband_tags; |
| 231 | param->chunks_per_block = geometry->pagesPerBlock; |
| 232 | param->use_nand_ecc = geometry->hasECC; |
| 233 | param->is_yaffs2 = geometry->useYaffs2; |
| 234 | param->n_reserved_blocks = 5; |
| 235 | dev->driver_context = (void *)geometry; |
| 236 | |
| 237 | yaffs_add_device(dev); |
| 238 | |
| 239 | return dev; |
| 240 | } |
| 241 | |
| 242 | free(dev); |
| 243 | free(clonedName); |
| 244 | |
| 245 | return NULL; |
| 246 | } |