yaffs Added some stuff to direct/python/yaffs_browser.py
[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                         yaffs_trace(YAFFS_TRACE_ERROR, "Writing uninitialised tags");
67                         YBUG();
68                 }
69                 yaffs_trace(YAFFS_TRACE_WRITE,
70                         "Writing chunk %d tags %d %d",
71                         nand_chunk, tags->obj_id, tags->chunk_id);
72         } else {
73                 yaffs_trace(YAFFS_TRACE_ERROR, "Writing with no tags");
74                 YBUG();
75         }
76
77         if (dev->param.write_chunk_tags_fn)
78                 return dev->param.write_chunk_tags_fn(dev, nand_chunk, buffer,
79                                                       tags);
80         else
81                 return yaffs_tags_compat_wr(dev, nand_chunk, buffer, tags);
82 }
83
84 int yaffs_mark_bad(struct yaffs_dev *dev, int block_no)
85 {
86         block_no -= dev->block_offset;
87
88         if (dev->param.bad_block_fn)
89                 return dev->param.bad_block_fn(dev, block_no);
90         else
91                 return yaffs_tags_compat_mark_bad(dev, block_no);
92 }
93
94 int yaffs_query_init_block_state(struct yaffs_dev *dev,
95                                  int block_no,
96                                  enum yaffs_block_state *state,
97                                  u32 * seq_number)
98 {
99         block_no -= dev->block_offset;
100
101         if (dev->param.query_block_fn)
102                 return dev->param.query_block_fn(dev, block_no, state,
103                                                  seq_number);
104         else
105                 return yaffs_tags_compat_query_block(dev, block_no,
106                                                      state, seq_number);
107 }
108
109 int yaffs_erase_block(struct yaffs_dev *dev, int flash_block)
110 {
111         int result;
112
113         flash_block -= dev->block_offset;
114
115         dev->n_erasures++;
116
117         result = dev->param.erase_fn(dev, flash_block);
118
119         return result;
120 }
121
122 int yaffs_init_nand(struct yaffs_dev *dev)
123 {
124         if (dev->param.initialise_flash_fn)
125                 return dev->param.initialise_flash_fn(dev);
126         return YAFFS_OK;
127 }