Merge branch 'master' of ssh://www.aleph1.co.uk/home/aleph1/git/yaffs2
[yaffs2.git] / yaffs_nand.c
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 "yaffs_nand.h"
15 #include "yaffs_tagscompat.h"
16 #include "yaffs_tagsvalidity.h"
17
18 #include "yaffs_getblockinfo.h"
19
20 int yaffs_rd_chunk_tags_nand(struct yaffs_dev *dev, int nand_chunk,
21                              u8 *buffer, struct yaffs_ext_tags *tags)
22 {
23         int result;
24         struct yaffs_ext_tags local_tags;
25         int realigned_chunk = nand_chunk - dev->chunk_offset;
26
27         dev->n_page_reads++;
28
29         /* If there are no tags provided use local tags. */
30         if (!tags)
31                 tags = &local_tags;
32
33         if (dev->param.read_chunk_tags_fn)
34                 result =
35                     dev->param.read_chunk_tags_fn(dev, realigned_chunk, buffer,
36                                                   tags);
37         else
38                 result = yaffs_tags_compat_rd(dev,
39                                               realigned_chunk, buffer, tags);
40         if (tags && tags->ecc_result > YAFFS_ECC_RESULT_NO_ERROR) {
41
42                 struct yaffs_block_info *bi;
43                 bi = yaffs_get_block_info(dev,
44                                           nand_chunk /
45                                           dev->param.chunks_per_block);
46                 yaffs_handle_chunk_error(dev, bi);
47         }
48         return result;
49 }
50
51 int yaffs_wr_chunk_tags_nand(struct yaffs_dev *dev,
52                                 int nand_chunk,
53                                 const u8 *buffer, struct yaffs_ext_tags *tags)
54 {
55         dev->n_page_writes++;
56         nand_chunk -= dev->chunk_offset;
57
58         if (tags) {
59                 tags->seq_number = dev->seq_number;
60                 tags->chunk_used = 1;
61                 if (!yaffs_validate_tags(tags)) {
62                         yaffs_trace(YAFFS_TRACE_ERROR,
63                                 "Writing uninitialised tags");
64                         BUG();
65                 }
66                 yaffs_trace(YAFFS_TRACE_WRITE,
67                         "Writing chunk %d tags %d %d",
68                         nand_chunk, tags->obj_id, tags->chunk_id);
69         } else {
70                 yaffs_trace(YAFFS_TRACE_ERROR, "Writing with no tags");
71                 BUG();
72                 return YAFFS_FAIL;
73         }
74
75         if (dev->param.write_chunk_tags_fn)
76                 return dev->param.write_chunk_tags_fn(dev, nand_chunk, buffer,
77                                                         tags);
78
79         return yaffs_tags_compat_wr(dev, nand_chunk, buffer, tags);
80 }
81
82 int yaffs_mark_bad(struct yaffs_dev *dev, int block_no)
83 {
84         block_no -= dev->block_offset;
85         if (dev->param.bad_block_fn)
86                 return dev->param.bad_block_fn(dev, block_no);
87
88         return yaffs_tags_compat_mark_bad(dev, block_no);
89 }
90
91 int yaffs_query_init_block_state(struct yaffs_dev *dev,
92                                  int block_no,
93                                  enum yaffs_block_state *state,
94                                  u32 *seq_number)
95 {
96         block_no -= dev->block_offset;
97         if (dev->param.query_block_fn)
98                 return dev->param.query_block_fn(dev, block_no, state,
99                                                  seq_number);
100
101         return yaffs_tags_compat_query_block(dev, block_no, state, seq_number);
102 }
103
104 int yaffs_erase_block(struct yaffs_dev *dev, int flash_block)
105 {
106         int result;
107
108         flash_block -= dev->block_offset;
109         dev->n_erasures++;
110         result = dev->param.erase_fn(dev, flash_block);
111         return result;
112 }
113
114 int yaffs_init_nand(struct yaffs_dev *dev)
115 {
116         if (dev->param.initialise_flash_fn)
117                 return dev->param.initialise_flash_fn(dev);
118         return YAFFS_OK;
119 }