2 * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
4 * Copyright (C) 2002-2011 Aleph One Ltd.
5 * for Toby Churchill Ltd and Brightstar Engineering
7 * Created by Charles Manning <charles@aleph1.co.uk>
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.
15 #include "yaffs_guts.h"
18 #include "yaffs_nandif.h"
19 #include "yaffs_packedtags2.h"
23 #include "yaffs_trace.h"
27 /* NB For use with inband tags....
28 * We assume that the data buffer is of size totalBytersPerChunk so that
29 * we can also use it to load the tags.
31 int ynandif_WriteChunkWithTagsToNAND(struct yaffs_dev *dev, int nand_chunk,
33 const struct yaffs_ext_tags *tags)
37 struct yaffs_packed_tags2 pt;
39 unsigned spareSize = 0;
40 struct ynandif_Geometry *geometry = (struct ynandif_Geometry *)(dev->driver_context);
42 yaffs_trace(YAFFS_TRACE_MTD,
43 "nandmtd2_WriteChunkWithTagsToNAND chunk %d data %p tags %p",
44 nand_chunk, data, tags);
47 /* For yaffs2 writing there must be both data and tags.
48 * If we're using inband tags, then the tags are stuffed into
49 * the end of the data buffer.
52 if (dev->param.inband_tags) {
53 struct yaffs_packed_tags2_tags_only *pt2tp;
55 pt2tp = (struct yaffs_packed_tags2_tags_only *)
56 (data + dev->data_bytes_per_chunk);
57 yaffs_pack_tags2_tags_only(pt2tp, tags);
61 yaffs_pack_tags2(&pt, tags, !dev->param.no_tags_ecc);
63 spareSize = sizeof(struct yaffs_packed_tags2);
66 retval = geometry->writeChunk(dev, nand_chunk,
67 data, dev->param.total_bytes_per_chunk,
73 int ynandif_ReadChunkWithTagsFromNAND(struct yaffs_dev *dev, int nand_chunk,
74 u8 *data, struct yaffs_ext_tags *tags)
76 struct yaffs_packed_tags2 pt;
81 int eccStatus; /* 0 = ok, 1 = fixed, -1 = unfixed */
82 struct ynandif_Geometry *geometry = (struct ynandif_Geometry *)(dev->driver_context);
84 yaffs_trace(YAFFS_TRACE_MTD,
85 "nandmtd2_ReadChunkWithTagsFromNAND chunk %d data %p tags %p",
86 nand_chunk, data, tags);
91 } else if (dev->param.inband_tags) {
95 data = yaffs_get_temp_buffer(dev);
101 spareSize = sizeof(struct yaffs_packed_tags2);
104 retval = geometry->readChunk(dev, nand_chunk,
106 data ? dev->param.total_bytes_per_chunk : 0,
110 if (dev->param.inband_tags) {
112 struct yaffs_packed_tags2_tags_only *pt2tp;
113 pt2tp = (struct yaffs_packed_tags2_tags_only *)
114 &data[dev->data_bytes_per_chunk];
115 yaffs_unpack_tags2_tags_only(tags, pt2tp);
119 yaffs_unpack_tags2(tags, &pt, !dev->param.no_tags_ecc);
122 if (tags && tags->chunk_used) {
124 tags->ecc_result == YAFFS_ECC_RESULT_UNFIXED)
125 tags->ecc_result = YAFFS_ECC_RESULT_UNFIXED;
126 else if (eccStatus > 0 ||
127 tags->ecc_result == YAFFS_ECC_RESULT_FIXED)
128 tags->ecc_result = YAFFS_ECC_RESULT_FIXED;
130 tags->ecc_result = YAFFS_ECC_RESULT_NO_ERROR;
134 yaffs_release_temp_buffer(dev, data);
139 int ynandif_MarkNANDBlockBad(struct yaffs_dev *dev, int blockId)
141 struct ynandif_Geometry *geometry = (struct ynandif_Geometry *)(dev->driver_context);
143 return geometry->markBlockBad(dev, blockId);
146 int ynandif_EraseBlockInNAND(struct yaffs_dev *dev, int blockId)
148 struct ynandif_Geometry *geometry = (struct ynandif_Geometry *)(dev->driver_context);
150 return geometry->eraseBlock(dev, blockId);
155 static int ynandif_IsBlockOk(struct yaffs_dev *dev, int blockId)
157 struct ynandif_Geometry *geometry = (struct ynandif_Geometry *)(dev->driver_context);
159 return geometry->checkBlockOk(dev, blockId);
162 int ynandif_QueryNANDBlock(struct yaffs_dev *dev, int blockId,
163 enum yaffs_block_state *state, u32 *seq_number)
166 struct yaffs_ext_tags tags;
170 chunkNo = blockId * dev->param.chunks_per_block;
172 if (!ynandif_IsBlockOk(dev, blockId)) {
173 *state = YAFFS_BLOCK_STATE_DEAD;
175 ynandif_ReadChunkWithTagsFromNAND(dev, chunkNo, NULL, &tags);
177 if (!tags.chunk_used) {
178 *state = YAFFS_BLOCK_STATE_EMPTY;
180 *state = YAFFS_BLOCK_STATE_NEEDS_SCAN;
181 *seq_number = tags.seq_number;
189 int ynandif_InitialiseNAND(struct yaffs_dev *dev)
191 struct ynandif_Geometry *geometry = (struct ynandif_Geometry *)(dev->driver_context);
193 geometry->initialise(dev);
198 int ynandif_Deinitialise_flash_fn(struct yaffs_dev *dev)
200 struct ynandif_Geometry *geometry = (struct ynandif_Geometry *)(dev->driver_context);
202 geometry->deinitialise(dev);
209 yaffs_add_dev_from_geometry(const YCHAR *name,
210 const struct ynandif_Geometry *geometry)
212 YCHAR *clonedName = malloc(sizeof(YCHAR) *
213 (strnlen(name, YAFFS_MAX_NAME_LENGTH)+1));
214 struct yaffs_dev *dev = malloc(sizeof(struct yaffs_dev));
215 struct yaffs_param *param;
217 if (dev && clonedName) {
218 memset(dev, 0, sizeof(struct yaffs_dev));
219 strcpy(clonedName, name);
223 param->name = clonedName;
224 param->write_chunk_tags_fn = ynandif_WriteChunkWithTagsToNAND;
225 param->read_chunk_tags_fn = ynandif_ReadChunkWithTagsFromNAND;
226 param->erase_fn = ynandif_EraseBlockInNAND;
227 param->initialise_flash_fn = ynandif_InitialiseNAND;
228 param->query_block_fn = ynandif_QueryNANDBlock;
229 param->bad_block_fn = ynandif_MarkNANDBlockBad;
230 param->n_caches = 20;
231 param->start_block = geometry->start_block;
232 param->end_block = geometry->end_block;
233 param->total_bytes_per_chunk = geometry->dataSize;
234 param->spare_bytes_per_chunk = geometry->spareSize;
235 param->inband_tags = geometry->inband_tags;
236 param->chunks_per_block = geometry->pagesPerBlock;
237 param->use_nand_ecc = geometry->hasECC;
238 param->is_yaffs2 = geometry->useYaffs2;
239 param->n_reserved_blocks = 5;
240 dev->driver_context = (void *)geometry;
242 yaffs_add_device(dev);