2 * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
4 * Copyright (C) 2002-2018 Aleph One Ltd.
6 * Created by Charles Manning <charles@aleph1.co.uk>
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.
14 * This is an interface module for handling NAND in yaffs2 mode.
17 /* This code calls a driver that accesses "generic" NAND. In the simulator
18 * this is direceted at a file-backed NAND simulator. The NAND access functions
19 * should also work with real NAND.
21 * This driver is designed for use in yaffs2 mode with a 2k page size and
24 * The spare ares is used as follows:
25 * offset 0: 2 bytes bad block marker.
26 * offset 2: 8x3 bytes of ECC over the data.
27 * offset 26: rest available to store Yaffs tags etc.
30 #include "yaffs_nand_drv.h"
32 #include "yaffs_trace.h"
34 #include "nand_store.h"
35 #include "yaffs_flashif.h"
36 #include "yaffs_guts.h"
38 #include "yaffs_ecc.h"
41 struct nand_chip *chip;
46 static inline struct nand_chip *dev_to_chip(struct yaffs_dev *dev)
48 struct nand_context *ctxt =
49 (struct nand_context *)(dev->driver_context);
53 static inline u8 *dev_to_buffer(struct yaffs_dev *dev)
55 struct nand_context *ctxt =
56 (struct nand_context *)(dev->driver_context);
60 static int yaffs_nand_drv_WriteChunk(struct yaffs_dev *dev, int nand_chunk,
61 const u8 *data, int data_len,
62 const u8 *oob, int oob_len)
64 struct nand_chip *chip = dev_to_chip(dev);
65 u8 *buffer = dev_to_buffer(dev);
67 struct nanddrv_transfer tr[2];
74 /* Calc ECC and marshall the oob bytes into the buffer */
76 memset(buffer, 0xff, chip->spare_bytes_per_page);
78 for(i = 0, e = buffer + 2; i < chip->data_bytes_per_page; i+=256, e+=3)
79 yaffs_ecc_calc(data + i, e);
82 memcpy(buffer + 26, oob, oob_len);
84 /* Set up and execute transfer */
86 tr[0].buffer = (u8 *)data;
88 tr[0].nbytes = data_len;
90 tr[1].buffer = buffer;
91 tr[1].offset = chip->data_bytes_per_page;
92 tr[1].nbytes = chip->spare_bytes_per_page;
94 if(nanddrv_write_tr(chip, nand_chunk, tr, 2) == 0)
100 static int yaffs_nand_drv_ReadChunk(struct yaffs_dev *dev, int nand_chunk,
101 u8 *data, int data_len,
102 u8 *oob, int oob_len,
103 enum yaffs_ecc_result *ecc_result_out)
105 struct nand_chip *chip = dev_to_chip(dev);
106 u8 *buffer = dev_to_buffer(dev);
107 struct nanddrv_transfer tr[2];
108 struct nanddrv_transfer *trp = tr;
111 enum yaffs_ecc_result ecc_result;
119 trp->nbytes = data_len;
125 trp->buffer = buffer;
126 trp->offset = chip->data_bytes_per_page;
127 trp->nbytes = chip->spare_bytes_per_page;
132 ret = nanddrv_read_tr(chip, nand_chunk, tr, n_tr);
136 *ecc_result_out = YAFFS_ECC_RESULT_UNKNOWN;
140 /* Do ECC and marshalling */
142 memcpy(oob, buffer + 26, oob_len);
144 ecc_result = YAFFS_ECC_RESULT_NO_ERROR;
147 for(i = 0, e = buffer + 2; i < chip->data_bytes_per_page; i+=256, e+=3) {
148 yaffs_ecc_calc(data + i, read_ecc);
149 ret = yaffs_ecc_correct(data + i, e, read_ecc);
151 ecc_result = YAFFS_ECC_RESULT_UNFIXED;
152 else if( ret > 0 && ecc_result == YAFFS_ECC_RESULT_NO_ERROR)
153 ecc_result = YAFFS_ECC_RESULT_FIXED;
158 *ecc_result_out = ecc_result;
163 static int yaffs_nand_drv_EraseBlock(struct yaffs_dev *dev, int block_no)
165 struct nand_chip *chip = dev_to_chip(dev);
167 if(nanddrv_erase(chip, block_no) == 0)
173 static int yaffs_nand_drv_MarkBad(struct yaffs_dev *dev, int block_no)
175 struct nand_chip *chip = dev_to_chip(dev);
176 u8 *buffer = dev_to_buffer(dev);
177 int nand_chunk = block_no * chip->pages_per_block;
178 struct nanddrv_transfer tr[1];
180 memset(buffer, 0xff, chip->spare_bytes_per_page);
184 tr[0].buffer = buffer;
185 tr[0].offset = chip->data_bytes_per_page;
186 tr[0].nbytes = chip->spare_bytes_per_page;
188 if(nanddrv_write_tr(chip, nand_chunk, tr, 1) == 0)
194 static int yaffs_nand_drv_CheckBad(struct yaffs_dev *dev, int block_no)
196 struct nand_chip *chip = dev_to_chip(dev);
197 u8 *buffer = dev_to_buffer(dev);
198 int nand_chunk = block_no * chip->pages_per_block;
199 struct nanddrv_transfer tr[1];
201 memset(buffer, 0, chip->spare_bytes_per_page);
203 tr[0].buffer = buffer;
204 tr[0].offset = chip->data_bytes_per_page;
205 tr[0].nbytes = chip->spare_bytes_per_page;
207 nanddrv_read_tr(chip, nand_chunk, tr, 1);
209 /* Check that bad block marker is not set */
210 if(yaffs_hweight8(buffer[0]) + yaffs_hweight8(buffer[1]) < 14)
217 static int yaffs_nand_drv_Initialise(struct yaffs_dev *dev)
219 struct nand_chip *chip = dev_to_chip(dev);
225 static int yaffs_nand_drv_Deinitialise(struct yaffs_dev *dev)
227 struct nand_chip *chip = dev_to_chip(dev);
234 int yaffs_nand_install_drv(struct yaffs_dev *dev, struct nand_chip *chip)
236 struct yaffs_driver *drv = &dev->drv;
238 struct nand_context *ctxt = NULL;
240 ctxt = malloc(sizeof(struct nand_context));
241 buffer = malloc(chip->spare_bytes_per_page);
246 drv->drv_write_chunk_fn = yaffs_nand_drv_WriteChunk;
247 drv->drv_read_chunk_fn = yaffs_nand_drv_ReadChunk;
248 drv->drv_erase_fn = yaffs_nand_drv_EraseBlock;
249 drv->drv_mark_bad_fn = yaffs_nand_drv_MarkBad;
250 drv->drv_check_bad_fn = yaffs_nand_drv_CheckBad;
251 drv->drv_initialise_fn = yaffs_nand_drv_Initialise;
252 drv->drv_deinitialise_fn = yaffs_nand_drv_Deinitialise;
255 ctxt->buffer = buffer;
256 dev->driver_context = (void *) ctxt;