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-2010 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
26         int realigned_chunk = nand_chunk - dev->chunk_offset;
27
28         dev->n_page_reads++;
29
30         /* If there are no tags provided, use local tags to get prioritised gc working */
31         if (!tags)
32                 tags = &local_tags;
33
34         if (dev->param.read_chunk_tags_fn)
35                 result =
36                     dev->param.read_chunk_tags_fn(dev, realigned_chunk, buffer,
37                                                   tags);
38         else
39                 result = yaffs_tags_compat_rd(dev,
40                                               realigned_chunk, buffer, tags);
41         if (tags && tags->ecc_result > YAFFS_ECC_RESULT_NO_ERROR) {
42
43                 struct yaffs_block_info *bi;
44                 bi = yaffs_get_block_info(dev,
45                                           nand_chunk /
46                                           dev->param.chunks_per_block);
47                 yaffs_handle_chunk_error(dev, bi);
48         }
49
50         return result;
51 }
52
53 int yaffs_wr_chunk_tags_nand(struct yaffs_dev *dev,
54                              int nand_chunk,
55                              const u8 * buffer, struct yaffs_ext_tags *tags)
56 {
57
58         dev->n_page_writes++;
59
60         nand_chunk -= dev->chunk_offset;
61
62         if (tags) {
63                 tags->seq_number = dev->seq_number;
64                 tags->chunk_used = 1;
65                 if (!yaffs_validate_tags(tags)) {
66                         T(YAFFS_TRACE_ERROR,
67                           (TSTR("Writing uninitialised tags" TENDSTR)));
68                         YBUG();
69                 }
70                 T(YAFFS_TRACE_WRITE,
71                   (TSTR("Writing chunk %d tags %d %d" TENDSTR), nand_chunk,
72                    tags->obj_id, tags->chunk_id));
73         } else {
74                 T(YAFFS_TRACE_ERROR, (TSTR("Writing with no tags" TENDSTR)));
75                 YBUG();
76         }
77
78         if (dev->param.write_chunk_tags_fn)
79                 return dev->param.write_chunk_tags_fn(dev, nand_chunk, buffer,
80                                                       tags);
81         else
82                 return yaffs_tags_compat_wr(dev, nand_chunk, buffer, tags);
83 }
84
85 int yaffs_mark_bad(struct yaffs_dev *dev, int block_no)
86 {
87         block_no -= dev->block_offset;
88
89         if (dev->param.bad_block_fn)
90                 return dev->param.bad_block_fn(dev, block_no);
91         else
92                 return yaffs_tags_compat_mark_bad(dev, block_no);
93 }
94
95 int yaffs_query_init_block_state(struct yaffs_dev *dev,
96                                  int block_no,
97                                  enum yaffs_block_state *state,
98                                  u32 * seq_number)
99 {
100         block_no -= dev->block_offset;
101
102         if (dev->param.query_block_fn)
103                 return dev->param.query_block_fn(dev, block_no, state,
104                                                  seq_number);
105         else
106                 return yaffs_tags_compat_query_block(dev, block_no,
107                                                      state, seq_number);
108 }
109
110 int yaffs_erase_block(struct yaffs_dev *dev, int flash_block)
111 {
112         int result;
113
114         flash_block -= dev->block_offset;
115
116         dev->n_erasures++;
117
118         result = dev->param.erase_fn(dev, flash_block);
119
120         return result;
121 }
122
123 int yaffs_init_nand(struct yaffs_dev *dev)
124 {
125         if (dev->param.initialise_flash_fn)
126                 return dev->param.initialise_flash_fn(dev);
127         return YAFFS_OK;
128 }