d322e351b466768e65d3ba545f875839a24792ec
[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 #include "yaffs_summary.h"
19
20 static int apply_chunk_offset(struct yaffs_dev *dev, int chunk)
21 {
22         return chunk - dev->chunk_offset;
23 }
24
25 int yaffs_rd_chunk_tags_nand(struct yaffs_dev *dev, int nand_chunk,
26                              u8 *buffer, struct yaffs_ext_tags *tags)
27 {
28         int result;
29         struct yaffs_ext_tags local_tags;
30         int flash_chunk = apply_chunk_offset(dev, nand_chunk);
31
32         dev->n_page_reads++;
33
34         /* If there are no tags provided use local tags. */
35         if (!tags)
36                 tags = &local_tags;
37
38         result = dev->th.read_chunk_tags_fn(dev, flash_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         int result;
55         int flash_chunk = apply_chunk_offset(dev, nand_chunk);
56
57         dev->n_page_writes++;
58
59         if (!tags) {
60                 yaffs_trace(YAFFS_TRACE_ERROR, "Writing with no tags");
61                 BUG();
62                 return YAFFS_FAIL;
63         }
64
65         tags->seq_number = dev->seq_number;
66         tags->chunk_used = 1;
67         yaffs_trace(YAFFS_TRACE_WRITE,
68                 "Writing chunk %d tags %d %d",
69                 nand_chunk, tags->obj_id, tags->chunk_id);
70
71         result = dev->th.write_chunk_tags_fn(dev, flash_chunk,
72                                                         buffer, tags);
73
74         yaffs_summary_add(dev, tags, nand_chunk);
75
76         return result;
77 }
78
79 int yaffs_mark_bad(struct yaffs_dev *dev, int block_no)
80 {
81         block_no -= dev->block_offset;
82         dev->n_bad_markings++;
83         return dev->th.mark_bad_fn(dev, block_no);
84 }
85
86
87 int yaffs_query_init_block_state(struct yaffs_dev *dev,
88                                  int block_no,
89                                  enum yaffs_block_state *state,
90                                  u32 *seq_number)
91 {
92         block_no -= dev->block_offset;
93         return dev->th.query_block_fn(dev, block_no, state, seq_number);
94 }
95
96 int yaffs_erase_block(struct yaffs_dev *dev, int block_no)
97 {
98         int result;
99
100         block_no -= dev->block_offset;
101         dev->n_erasures++;
102         result = dev->drv.drv_erase_fn(dev, block_no);
103         return result;
104 }
105
106 int yaffs_init_nand(struct yaffs_dev *dev)
107 {
108         if (dev->drv.drv_initialise_fn)
109                 return dev->drv.drv_initialise_fn(dev);
110         return YAFFS_OK;
111 }
112
113 int yaffs_deinit_nand(struct yaffs_dev *dev)
114 {
115         if (dev->drv.drv_deinitialise_fn)
116                 return dev->drv.drv_deinitialise_fn(dev);
117         return YAFFS_OK;
118 }