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"
19 #include "yaffs_nandif.h"
20 #include "yaffs_packedtags2.h"
24 #include "yaffs_trace.h"
28 /* NB For use with inband tags....
29 * We assume that the data buffer is of size totalBytersPerChunk so that
30 * we can also use it to load the tags.
32 int ynandif_WriteChunkWithTagsToNAND(struct yaffs_dev *dev, int nand_chunk,
34 const struct yaffs_ext_tags *tags)
38 struct yaffs_packed_tags2 pt;
40 unsigned spareSize = 0;
41 struct ynandif_Geometry *geometry = (struct ynandif_Geometry *)(dev->driver_context);
43 yaffs_trace(YAFFS_TRACE_MTD,
44 "nandmtd2_WriteChunkWithTagsToNAND chunk %d data %p tags %p",
45 nand_chunk, data, tags);
48 /* For yaffs2 writing there must be both data and tags.
49 * If we're using inband tags, then the tags are stuffed into
50 * the end of the data buffer.
53 if (dev->param.inband_tags) {
54 struct yaffs_packed_tags2_tags_only *pt2tp;
56 pt2tp = (struct yaffs_packed_tags2_tags_only *)
57 (data + dev->data_bytes_per_chunk);
58 yaffs_pack_tags2_tags_only(pt2tp, tags);
62 yaffs_pack_tags2(&pt, tags, !dev->param.no_tags_ecc);
64 spareSize = sizeof(struct yaffs_packed_tags2);
67 retval = geometry->writeChunk(dev, nand_chunk,
68 data, dev->param.total_bytes_per_chunk,
74 int ynandif_ReadChunkWithTagsFromNAND(struct yaffs_dev *dev, int nand_chunk,
75 u8 *data, struct yaffs_ext_tags *tags)
77 struct yaffs_packed_tags2 pt;
82 int eccStatus; /* 0 = ok, 1 = fixed, -1 = unfixed */
83 struct ynandif_Geometry *geometry = (struct ynandif_Geometry *)(dev->driver_context);
85 yaffs_trace(YAFFS_TRACE_MTD,
86 "nandmtd2_ReadChunkWithTagsFromNAND chunk %d data %p tags %p",
87 nand_chunk, data, tags);
92 } else if (dev->param.inband_tags) {
96 data = yaffs_get_temp_buffer(dev);
102 spareSize = sizeof(struct yaffs_packed_tags2);
105 retval = geometry->readChunk(dev, nand_chunk,
107 data ? dev->param.total_bytes_per_chunk : 0,
111 if (dev->param.inband_tags) {
113 struct yaffs_packed_tags2_tags_only *pt2tp;
114 pt2tp = (struct yaffs_packed_tags2_tags_only *)
115 &data[dev->data_bytes_per_chunk];
116 yaffs_unpack_tags2_tags_only(tags, pt2tp);
120 yaffs_unpack_tags2(tags, &pt, !dev->param.no_tags_ecc);
123 if (tags && tags->chunk_used) {
125 tags->ecc_result == YAFFS_ECC_RESULT_UNFIXED)
126 tags->ecc_result = YAFFS_ECC_RESULT_UNFIXED;
127 else if (eccStatus > 0 ||
128 tags->ecc_result == YAFFS_ECC_RESULT_FIXED)
129 tags->ecc_result = YAFFS_ECC_RESULT_FIXED;
131 tags->ecc_result = YAFFS_ECC_RESULT_NO_ERROR;
135 yaffs_release_temp_buffer(dev, data);
140 int ynandif_MarkNANDBlockBad(struct yaffs_dev *dev, int blockId)
142 struct ynandif_Geometry *geometry = (struct ynandif_Geometry *)(dev->driver_context);
144 return geometry->markBlockBad(dev, blockId);
147 int ynandif_EraseBlockInNAND(struct yaffs_dev *dev, int blockId)
149 struct ynandif_Geometry *geometry = (struct ynandif_Geometry *)(dev->driver_context);
151 return geometry->eraseBlock(dev, blockId);
156 static int ynandif_IsBlockOk(struct yaffs_dev *dev, int blockId)
158 struct ynandif_Geometry *geometry = (struct ynandif_Geometry *)(dev->driver_context);
160 return geometry->checkBlockOk(dev, blockId);
163 int ynandif_QueryNANDBlock(struct yaffs_dev *dev, int blockId,
164 enum yaffs_block_state *state, u32 *seq_number)
167 struct yaffs_ext_tags tags;
171 chunkNo = blockId * dev->param.chunks_per_block;
173 if (!ynandif_IsBlockOk(dev, blockId)) {
174 *state = YAFFS_BLOCK_STATE_DEAD;
176 ynandif_ReadChunkWithTagsFromNAND(dev, chunkNo, NULL, &tags);
178 if (!tags.chunk_used) {
179 *state = YAFFS_BLOCK_STATE_EMPTY;
181 *state = YAFFS_BLOCK_STATE_NEEDS_SCAN;
182 *seq_number = tags.seq_number;
190 int ynandif_InitialiseNAND(struct yaffs_dev *dev)
192 struct ynandif_Geometry *geometry = (struct ynandif_Geometry *)(dev->driver_context);
194 geometry->initialise(dev);
199 int ynandif_Deinitialise_flash_fn(struct yaffs_dev *dev)
201 struct ynandif_Geometry *geometry = (struct ynandif_Geometry *)(dev->driver_context);
203 geometry->deinitialise(dev);
210 yaffs_add_dev_from_geometry(const YCHAR *name,
211 const struct ynandif_Geometry *geometry)
213 YCHAR *clonedName = malloc(sizeof(YCHAR) *
214 (strnlen(name, YAFFS_MAX_NAME_LENGTH)+1));
215 struct yaffs_dev *dev = malloc(sizeof(struct yaffs_dev));
216 struct yaffs_param *param;
218 if (dev && clonedName) {
219 memset(dev, 0, sizeof(struct yaffs_dev));
220 strcpy(clonedName, name);
224 param->name = clonedName;
225 param->write_chunk_tags_fn = ynandif_WriteChunkWithTagsToNAND;
226 param->read_chunk_tags_fn = ynandif_ReadChunkWithTagsFromNAND;
227 param->erase_fn = ynandif_EraseBlockInNAND;
228 param->initialise_flash_fn = ynandif_InitialiseNAND;
229 param->query_block_fn = ynandif_QueryNANDBlock;
230 param->bad_block_fn = ynandif_MarkNANDBlockBad;
231 param->n_caches = 20;
232 param->start_block = geometry->start_block;
233 param->end_block = geometry->end_block;
234 param->total_bytes_per_chunk = geometry->dataSize;
235 param->spare_bytes_per_chunk = geometry->spareSize;
236 param->inband_tags = geometry->inband_tags;
237 param->chunks_per_block = geometry->pagesPerBlock;
238 param->use_nand_ecc = geometry->hasECC;
239 param->is_yaffs2 = geometry->useYaffs2;
240 param->n_reserved_blocks = 5;
241 dev->driver_context = (void *)geometry;
243 yaffs_add_device(dev);