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