xj | b04a402 | 2021-11-25 15:01:52 +0800 | [diff] [blame] | 1 | /* |
| 2 | * YAFFS: Yet Another Flash File System. A NAND-flash specific file system. |
| 3 | * |
| 4 | * Copyright (C) 2002-2018 Aleph One Ltd. |
| 5 | * |
| 6 | * Created by Charles Manning <charles@aleph1.co.uk> |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or modify |
| 9 | * it under the terms of the GNU General Public License version 2 as |
| 10 | * published by the Free Software Foundation. |
| 11 | */ |
| 12 | |
| 13 | #include "yaffs_nand.h" |
| 14 | #include "yaffs_tagscompat.h" |
| 15 | |
| 16 | #include "yaffs_getblockinfo.h" |
| 17 | #include "yaffs_summary.h" |
| 18 | |
| 19 | static int apply_chunk_offset(struct yaffs_dev *dev, int chunk) |
| 20 | { |
| 21 | return chunk - dev->chunk_offset; |
| 22 | } |
| 23 | |
| 24 | int yaffs_rd_chunk_tags_nand(struct yaffs_dev *dev, int nand_chunk, |
| 25 | u8 *buffer, struct yaffs_ext_tags *tags) |
| 26 | { |
| 27 | int result; |
| 28 | struct yaffs_ext_tags local_tags; |
| 29 | int flash_chunk = apply_chunk_offset(dev, nand_chunk); |
| 30 | |
| 31 | dev->n_page_reads++; |
| 32 | |
| 33 | /* If there are no tags provided use local tags. */ |
| 34 | if (!tags) |
| 35 | tags = &local_tags; |
| 36 | |
| 37 | result = dev->tagger.read_chunk_tags_fn(dev, flash_chunk, buffer, tags); |
| 38 | if (tags && tags->ecc_result > YAFFS_ECC_RESULT_NO_ERROR) { |
| 39 | |
| 40 | struct yaffs_block_info *bi; |
| 41 | bi = yaffs_get_block_info(dev, |
| 42 | nand_chunk / |
| 43 | dev->param.chunks_per_block); |
| 44 | yaffs_handle_chunk_error(dev, bi); |
| 45 | } |
| 46 | return result; |
| 47 | } |
| 48 | |
| 49 | int yaffs_wr_chunk_tags_nand(struct yaffs_dev *dev, |
| 50 | int nand_chunk, |
| 51 | const u8 *buffer, struct yaffs_ext_tags *tags) |
| 52 | { |
| 53 | int result; |
| 54 | int flash_chunk = apply_chunk_offset(dev, nand_chunk); |
| 55 | |
| 56 | dev->n_page_writes++; |
| 57 | |
| 58 | if (!tags) { |
| 59 | yaffs_trace(YAFFS_TRACE_ERROR, "Writing with no tags"); |
| 60 | BUG(); |
| 61 | return YAFFS_FAIL; |
| 62 | } |
| 63 | |
| 64 | tags->seq_number = dev->seq_number; |
| 65 | tags->chunk_used = 1; |
| 66 | yaffs_trace(YAFFS_TRACE_WRITE, |
| 67 | "Writing chunk %d tags %d %d", |
| 68 | nand_chunk, tags->obj_id, tags->chunk_id); |
| 69 | |
| 70 | result = dev->tagger.write_chunk_tags_fn(dev, flash_chunk, |
| 71 | buffer, tags); |
| 72 | |
| 73 | yaffs_summary_add(dev, tags, nand_chunk); |
| 74 | |
| 75 | return result; |
| 76 | } |
| 77 | |
| 78 | int yaffs_mark_bad(struct yaffs_dev *dev, int block_no) |
| 79 | { |
| 80 | block_no -= dev->block_offset; |
| 81 | dev->n_bad_markings++; |
| 82 | |
| 83 | if (dev->param.disable_bad_block_marking) |
| 84 | return YAFFS_OK; |
| 85 | |
| 86 | return dev->tagger.mark_bad_fn(dev, block_no); |
| 87 | } |
| 88 | |
| 89 | |
| 90 | int yaffs_query_init_block_state(struct yaffs_dev *dev, |
| 91 | int block_no, |
| 92 | enum yaffs_block_state *state, |
| 93 | u32 *seq_number) |
| 94 | { |
| 95 | block_no -= dev->block_offset; |
| 96 | return dev->tagger.query_block_fn(dev, block_no, state, seq_number); |
| 97 | } |
| 98 | |
| 99 | int yaffs_erase_block(struct yaffs_dev *dev, int block_no) |
| 100 | { |
| 101 | int result; |
| 102 | |
| 103 | block_no -= dev->block_offset; |
| 104 | dev->n_erasures++; |
| 105 | result = dev->drv.drv_erase_fn(dev, block_no); |
| 106 | return result; |
| 107 | } |
| 108 | |
| 109 | int yaffs_init_nand(struct yaffs_dev *dev) |
| 110 | { |
| 111 | if (dev->drv.drv_initialise_fn) |
| 112 | return dev->drv.drv_initialise_fn(dev); |
| 113 | return YAFFS_OK; |
| 114 | } |
| 115 | |
| 116 | int yaffs_deinit_nand(struct yaffs_dev *dev) |
| 117 | { |
| 118 | if (dev->drv.drv_deinitialise_fn) |
| 119 | return dev->drv.drv_deinitialise_fn(dev); |
| 120 | return YAFFS_OK; |
| 121 | } |