2 * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
4 * Copyright (C) 2002-2011 Aleph One Ltd.
5 * for Toby Churchill Ltd and Brightstar Engineering
7 * Created by Charles Manning <charles@aleph1.co.uk>
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.
15 * This is an interface module for handling NOR in yaffs1 mode.
18 /* This code is intended to be used with "regular" NOR that is bit-modifyable.
20 * Each "chunk" is a contguous area of 512 + 16 bytes.
21 * This makes 248 such chunks with some space left over where a format markerr
25 #include "yaffs_nor_drv.h"
28 #include "yaffs_trace.h"
30 #include "yaffs_flashif.h"
31 #include "yaffs_guts.h"
34 #define DATA_BYTES_PER_CHUNK 512
35 #define SPARE_BYTES_PER_CHUNK 16
36 #define BLOCK_SIZE_IN_BYTES (128*1024)
37 #define BYTES_IN_DEVICE (4*1024*1024)
39 #define BYTES_PER_CHUNK (DATA_BYTES_PER_CHUNK + SPARE_BYTES_PER_CHUNK)
40 #define SPARE_AREA_OFFSET DATA_BYTES_PER_CHUNK
41 #define CHUNKS_PER_BLOCK (BLOCK_SIZE_IN_BYTES/BYTES_PER_CHUNK)
43 #define BLOCKS_IN_DEVICE (BYTES_IN_DEVICE/BLOCK_SIZE_IN_BYTES)
45 #define YNOR_PREMARKER (0xF6)
46 #define YNOR_POSTMARKER (0xF0)
48 #define FORMAT_OFFSET (CHUNKS_PER_BLOCK * BYTES_PER_CHUNK)
49 #define FORMAT_VALUE 0x1234
53 /* Compile this for a simulation */
56 static struct nor_sim *nor_sim;
58 #define nor_drv_FlashInit() do {nor_sim = ynorsim_initialise("emfile-nor", BLOCKS_IN_DEVICE, BLOCK_SIZE_IN_BYTES); } while(0)
59 #define nor_drv_FlashDeinit() ynorsim_shutdown(nor_sim)
60 #define nor_drv_FlashWrite32(addr,buf,nwords) ynorsim_wr32(nor_sim,addr,buf,nwords)
61 #define nor_drv_FlashRead32(addr,buf,nwords) ynorsim_rd32(nor_sim,addr,buf,nwords)
62 #define nor_drv_FlashEraseBlock(addr) ynorsim_erase(nor_sim,addr)
63 #define DEVICE_BASE ynorsim_get_base(nor_sim)
67 /* Compile this to hook up to read hardware */
68 #include "../blob/yflashrw.h"
69 #define nor_drv_FlashInit() do{} while(0)
70 #define nor_drv_FlashDeinit() do {} while(0)
71 #define nor_drv_FlashWrite32(addr,buf,nwords) Y_FlashWrite(addr,buf,nwords)
72 #define nor_drv_FlashRead32(addr,buf,nwords) Y_FlashRead(addr,buf,nwords)
73 #define nor_drv_FlashEraseBlock(addr) Y_FlashErase(addr,BLOCK_SIZE_IN_BYTES)
74 #define DEVICE_BASE (32 * 1024 * 1024)
78 static u32 *Block2Addr(struct yaffs_dev *dev, int blockNumber)
84 addr = (u8*)DEVICE_BASE;
85 addr += blockNumber * BLOCK_SIZE_IN_BYTES;
90 static u32 *Block2FormatAddr(struct yaffs_dev *dev, int blockNumber)
94 addr = (u8*) Block2Addr(dev,blockNumber);
95 addr += FORMAT_OFFSET;
100 static u32 *Chunk2DataAddr(struct yaffs_dev *dev, int chunk_id)
103 unsigned chunkInBlock;
106 block = chunk_id/dev->param.chunks_per_block;
107 chunkInBlock = chunk_id % dev->param.chunks_per_block;
109 addr = (u8*) Block2Addr(dev,block);
110 addr += chunkInBlock * BYTES_PER_CHUNK;
115 static u32 *Chunk2SpareAddr(struct yaffs_dev *dev, int chunk_id)
119 addr = (u8*) Chunk2DataAddr(dev, chunk_id);
120 addr += SPARE_AREA_OFFSET;
125 static void nor_drv_AndBytes(u8*target, const u8 *src, int nbytes)
135 static int nor_drv_WriteChunkToNAND(struct yaffs_dev *dev,int nand_chunk,
136 const u8 *data, int data_len,
137 const u8 *oob, int oob_len)
139 u32 *dataAddr = Chunk2DataAddr(dev,nand_chunk);
140 u32 *spareAddr = Chunk2SpareAddr(dev,nand_chunk);
142 struct yaffs_spare *spare = (struct yaffs_spare *)oob;
143 struct yaffs_spare tmpSpare;
147 /* We should only be getting called for one of 3 reasons:
148 * Writing a chunk: data and spare will not be NULL
149 * Writing a deletion marker: data will be NULL, spare not NULL
150 * Writing a bad block marker: data will be NULL, spare not NULL
153 if(sizeof(struct yaffs_spare) != SPARE_BYTES_PER_CHUNK)
157 if(spare->page_status != 0xff)
159 /* Write a pre-marker */
160 memset(&tmpSpare,0xff,sizeof(tmpSpare));
161 tmpSpare.page_status = YNOR_PREMARKER;
162 nor_drv_FlashWrite32(spareAddr,(u32 *)&tmpSpare,sizeof(struct yaffs_spare)/sizeof(u32));
165 nor_drv_FlashWrite32(dataAddr,(u32 *)data, data_len/ sizeof(u32));
167 memcpy(&tmpSpare,spare,sizeof(struct yaffs_spare));
169 /* Write the real tags, but override the premarker*/
170 tmpSpare.page_status = YNOR_PREMARKER;
171 nor_drv_FlashWrite32(spareAddr,(u32 *)&tmpSpare,sizeof(struct yaffs_spare)/sizeof(u32));
173 /* Write a post-marker */
174 tmpSpare.page_status = YNOR_POSTMARKER;
175 nor_drv_FlashWrite32(spareAddr,(u32 *)&tmpSpare,sizeof(tmpSpare)/sizeof(u32));
178 /* This has to be a read-modify-write operation to handle NOR-ness */
180 nor_drv_FlashRead32(spareAddr,(u32 *)&tmpSpare,sizeof(struct yaffs_spare)/sizeof(u32));
182 nor_drv_AndBytes((u8 *)&tmpSpare,(u8 *)spare,sizeof(struct yaffs_spare));
184 nor_drv_FlashWrite32(spareAddr,(u32 *)&tmpSpare,sizeof(struct yaffs_spare)/sizeof(u32));
192 static int nor_drv_ReadChunkFromNAND(struct yaffs_dev *dev,int nand_chunk,
193 u8 *data, int data_len,
194 u8 *oob, int oob_len,
195 enum yaffs_ecc_result *ecc_result)
197 struct yaffs_spare *spare = (struct yaffs_spare *)oob;
199 u32 *dataAddr = Chunk2DataAddr(dev,nand_chunk);
200 u32 *spareAddr = Chunk2SpareAddr(dev,nand_chunk);
205 nor_drv_FlashRead32(dataAddr,(u32 *)data,dev->param.total_bytes_per_chunk / sizeof(u32));
209 nor_drv_FlashRead32(spareAddr,(u32 *)spare, oob_len/ sizeof(u32));
211 /* If the page status is YNOR_POSTMARKER then it was written properly
212 * so change that to 0xFF so that the rest of yaffs is happy.
214 if(spare->page_status == YNOR_POSTMARKER)
215 spare->page_status = 0xff;
216 else if(spare->page_status != 0xff &&
217 (spare->page_status | YNOR_PREMARKER) != 0xff)
218 spare->page_status = YNOR_PREMARKER;
222 *ecc_result = YAFFS_ECC_RESULT_NO_ERROR;
229 static int nor_drv_FormatBlock(struct yaffs_dev *dev, int blockNumber)
231 u32 *blockAddr = Block2Addr(dev,blockNumber);
232 u32 *formatAddr = Block2FormatAddr(dev,blockNumber);
233 u32 formatValue = FORMAT_VALUE;
235 nor_drv_FlashEraseBlock(blockAddr);
236 nor_drv_FlashWrite32(formatAddr,&formatValue,1);
241 static int nor_drv_UnformatBlock(struct yaffs_dev *dev, int blockNumber)
243 u32 *formatAddr = Block2FormatAddr(dev,blockNumber);
246 nor_drv_FlashWrite32(formatAddr,&formatValue,1);
251 static int nor_drv_IsBlockFormatted(struct yaffs_dev *dev, int blockNumber)
253 u32 *formatAddr = Block2FormatAddr(dev,blockNumber);
257 nor_drv_FlashRead32(formatAddr,&formatValue,1);
259 return (formatValue == FORMAT_VALUE);
262 static int nor_drv_EraseBlockInNAND(struct yaffs_dev *dev, int blockNumber)
265 if(blockNumber < 0 || blockNumber >= BLOCKS_IN_DEVICE)
267 yaffs_trace(YAFFS_TRACE_ALWAYS,
268 "Attempt to erase non-existant block %d\n",
274 nor_drv_UnformatBlock(dev,blockNumber);
275 nor_drv_FormatBlock(dev,blockNumber);
281 static int nor_drv_InitialiseNAND(struct yaffs_dev *dev)
286 /* Go through the blocks formatting them if they are not formatted */
287 for(i = dev->param.start_block; i <= dev->param.end_block; i++){
288 if(!nor_drv_IsBlockFormatted(dev,i)){
289 nor_drv_FormatBlock(dev,i);
295 static int nor_drv_Deinitialise_flash_fn(struct yaffs_dev *dev)
299 nor_drv_FlashDeinit();
305 struct yaffs_dev *yaffs_nor_install_drv(const char *name)
308 struct yaffs_dev *dev = malloc(sizeof(struct yaffs_dev));
309 char *name_copy = strdup(name);
310 struct yaffs_param *param;
311 struct yaffs_driver *drv;
314 if(!dev || !name_copy) {
323 memset(dev, 0, sizeof(*dev));
325 param->name = name_copy;
327 param->total_bytes_per_chunk = DATA_BYTES_PER_CHUNK;
328 param->chunks_per_block = CHUNKS_PER_BLOCK;
329 param->n_reserved_blocks = 2;
330 param->start_block = 0; // Can use block 0
331 param->end_block = BLOCKS_IN_DEVICE - 1; // Last block
332 param->use_nand_ecc = 0; // use YAFFS's ECC
333 param->disable_soft_del = 1;
335 drv->drv_write_chunk_fn = nor_drv_WriteChunkToNAND;
336 drv->drv_read_chunk_fn = nor_drv_ReadChunkFromNAND;
337 drv->drv_erase_fn = nor_drv_EraseBlockInNAND;
338 drv->drv_initialise_fn = nor_drv_InitialiseNAND;
339 drv->drv_deinitialise_fn = nor_drv_Deinitialise_flash_fn;
341 param->n_caches = 10;
342 param->disable_soft_del = 1;
344 dev->driver_context = (void *) nor_sim;
346 yaffs_add_device(dev);