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 NOR in yaffs1 mode.
17 /* This code is intended to be used with "regular" NOR that is bit-modifyable.
19 * Each "chunk" is a contguous area of 512 + 16 bytes.
20 * This makes 248 such chunks with some space left over where a format markerr
24 #include "yaffs_nor_drv.h"
27 #include "yaffs_trace.h"
29 #include "yaffs_flashif.h"
30 #include "yaffs_guts.h"
33 #define DATA_BYTES_PER_CHUNK 512
34 #define SPARE_BYTES_PER_CHUNK 16
35 #define BLOCK_SIZE_IN_BYTES (128*1024)
36 #define BYTES_IN_DEVICE (4*1024*1024)
38 #define BYTES_PER_CHUNK (DATA_BYTES_PER_CHUNK + SPARE_BYTES_PER_CHUNK)
39 #define SPARE_AREA_OFFSET DATA_BYTES_PER_CHUNK
40 #define CHUNKS_PER_BLOCK (BLOCK_SIZE_IN_BYTES/BYTES_PER_CHUNK)
42 #define BLOCKS_IN_DEVICE (BYTES_IN_DEVICE/BLOCK_SIZE_IN_BYTES)
44 #define YNOR_PREMARKER (0xF6)
45 #define YNOR_POSTMARKER (0xF0)
47 #define FORMAT_OFFSET (CHUNKS_PER_BLOCK * BYTES_PER_CHUNK)
48 #define FORMAT_VALUE 0x1234
52 /* Compile this for a simulation */
55 static struct nor_sim *nor_sim;
57 #define nor_drv_FlashInit() do {nor_sim = ynorsim_initialise("emfile-nor", BLOCKS_IN_DEVICE, BLOCK_SIZE_IN_BYTES); } while(0)
58 #define nor_drv_FlashDeinit() ynorsim_shutdown(nor_sim)
59 #define nor_drv_FlashWrite32(addr,buf,nwords) ynorsim_wr32(nor_sim,addr,buf,nwords)
60 #define nor_drv_FlashRead32(addr,buf,nwords) ynorsim_rd32(nor_sim,addr,buf,nwords)
61 #define nor_drv_FlashEraseBlock(addr) ynorsim_erase(nor_sim,addr)
62 #define DEVICE_BASE ynorsim_get_base(nor_sim)
66 /* Compile this to hook up to read hardware */
67 #include "../blob/yflashrw.h"
68 #define nor_drv_FlashInit() do{} while(0)
69 #define nor_drv_FlashDeinit() do {} while(0)
70 #define nor_drv_FlashWrite32(addr,buf,nwords) Y_FlashWrite(addr,buf,nwords)
71 #define nor_drv_FlashRead32(addr,buf,nwords) Y_FlashRead(addr,buf,nwords)
72 #define nor_drv_FlashEraseBlock(addr) Y_FlashErase(addr,BLOCK_SIZE_IN_BYTES)
73 #define DEVICE_BASE (32 * 1024 * 1024)
77 static u32 *Block2Addr(struct yaffs_dev *dev, int blockNumber)
83 addr = (u8*)DEVICE_BASE;
84 addr += blockNumber * BLOCK_SIZE_IN_BYTES;
89 static u32 *Block2FormatAddr(struct yaffs_dev *dev, int blockNumber)
93 addr = (u8*) Block2Addr(dev,blockNumber);
94 addr += FORMAT_OFFSET;
99 static u32 *Chunk2DataAddr(struct yaffs_dev *dev, int chunk_id)
102 unsigned chunkInBlock;
105 block = chunk_id/dev->param.chunks_per_block;
106 chunkInBlock = chunk_id % dev->param.chunks_per_block;
108 addr = (u8*) Block2Addr(dev,block);
109 addr += chunkInBlock * BYTES_PER_CHUNK;
114 static u32 *Chunk2SpareAddr(struct yaffs_dev *dev, int chunk_id)
118 addr = (u8*) Chunk2DataAddr(dev, chunk_id);
119 addr += SPARE_AREA_OFFSET;
124 static void nor_drv_AndBytes(u8*target, const u8 *src, int nbytes)
134 static int nor_drv_WriteChunkToNAND(struct yaffs_dev *dev,int nand_chunk,
135 const u8 *data, int data_len,
136 const u8 *oob, int oob_len)
138 u32 *dataAddr = Chunk2DataAddr(dev,nand_chunk);
139 u32 *spareAddr = Chunk2SpareAddr(dev,nand_chunk);
141 struct yaffs_spare *spare = (struct yaffs_spare *)oob;
142 struct yaffs_spare tmpSpare;
146 /* We should only be getting called for one of 3 reasons:
147 * Writing a chunk: data and spare will not be NULL
148 * Writing a deletion marker: data will be NULL, spare not NULL
149 * Writing a bad block marker: data will be NULL, spare not NULL
152 if(sizeof(struct yaffs_spare) != SPARE_BYTES_PER_CHUNK)
156 if(spare->page_status != 0xff)
158 /* Write a pre-marker */
159 memset(&tmpSpare,0xff,sizeof(tmpSpare));
160 tmpSpare.page_status = YNOR_PREMARKER;
161 nor_drv_FlashWrite32(spareAddr,(u32 *)&tmpSpare,sizeof(struct yaffs_spare)/sizeof(u32));
164 nor_drv_FlashWrite32(dataAddr,(u32 *)data, data_len/ sizeof(u32));
166 memcpy(&tmpSpare,spare,sizeof(struct yaffs_spare));
168 /* Write the real tags, but override the premarker*/
169 tmpSpare.page_status = YNOR_PREMARKER;
170 nor_drv_FlashWrite32(spareAddr,(u32 *)&tmpSpare,sizeof(struct yaffs_spare)/sizeof(u32));
172 /* Write a post-marker */
173 tmpSpare.page_status = YNOR_POSTMARKER;
174 nor_drv_FlashWrite32(spareAddr,(u32 *)&tmpSpare,sizeof(tmpSpare)/sizeof(u32));
177 /* This has to be a read-modify-write operation to handle NOR-ness */
179 nor_drv_FlashRead32(spareAddr,(u32 *)&tmpSpare,sizeof(struct yaffs_spare)/sizeof(u32));
181 nor_drv_AndBytes((u8 *)&tmpSpare,(u8 *)spare,sizeof(struct yaffs_spare));
183 nor_drv_FlashWrite32(spareAddr,(u32 *)&tmpSpare,sizeof(struct yaffs_spare)/sizeof(u32));
191 static int nor_drv_ReadChunkFromNAND(struct yaffs_dev *dev,int nand_chunk,
192 u8 *data, int data_len,
193 u8 *oob, int oob_len,
194 enum yaffs_ecc_result *ecc_result)
196 struct yaffs_spare *spare = (struct yaffs_spare *)oob;
198 u32 *dataAddr = Chunk2DataAddr(dev,nand_chunk);
199 u32 *spareAddr = Chunk2SpareAddr(dev,nand_chunk);
204 nor_drv_FlashRead32(dataAddr,(u32 *)data,dev->param.total_bytes_per_chunk / sizeof(u32));
208 nor_drv_FlashRead32(spareAddr,(u32 *)spare, oob_len/ sizeof(u32));
210 /* If the page status is YNOR_POSTMARKER then it was written properly
211 * so change that to 0xFF so that the rest of yaffs is happy.
213 if(spare->page_status == YNOR_POSTMARKER)
214 spare->page_status = 0xff;
215 else if(spare->page_status != 0xff &&
216 (spare->page_status | YNOR_PREMARKER) != 0xff)
217 spare->page_status = YNOR_PREMARKER;
221 *ecc_result = YAFFS_ECC_RESULT_NO_ERROR;
228 static int nor_drv_FormatBlock(struct yaffs_dev *dev, int blockNumber)
230 u32 *blockAddr = Block2Addr(dev,blockNumber);
231 u32 *formatAddr = Block2FormatAddr(dev,blockNumber);
232 u32 formatValue = FORMAT_VALUE;
234 nor_drv_FlashEraseBlock(blockAddr);
235 nor_drv_FlashWrite32(formatAddr,&formatValue,1);
240 static int nor_drv_UnformatBlock(struct yaffs_dev *dev, int blockNumber)
242 u32 *formatAddr = Block2FormatAddr(dev,blockNumber);
245 nor_drv_FlashWrite32(formatAddr,&formatValue,1);
250 static int nor_drv_IsBlockFormatted(struct yaffs_dev *dev, int blockNumber)
252 u32 *formatAddr = Block2FormatAddr(dev,blockNumber);
256 nor_drv_FlashRead32(formatAddr,&formatValue,1);
258 return (formatValue == FORMAT_VALUE);
261 static int nor_drv_EraseBlockInNAND(struct yaffs_dev *dev, int blockNumber)
264 if(blockNumber < 0 || blockNumber >= BLOCKS_IN_DEVICE)
266 yaffs_trace(YAFFS_TRACE_ALWAYS,
267 "Attempt to erase non-existant block %d\n",
273 nor_drv_UnformatBlock(dev,blockNumber);
274 nor_drv_FormatBlock(dev,blockNumber);
280 static int nor_drv_InitialiseNAND(struct yaffs_dev *dev)
285 /* Go through the blocks formatting them if they are not formatted */
286 for(i = dev->param.start_block; i <= dev->param.end_block; i++){
287 if(!nor_drv_IsBlockFormatted(dev,i)){
288 nor_drv_FormatBlock(dev,i);
294 static int nor_drv_Deinitialise_flash_fn(struct yaffs_dev *dev)
298 nor_drv_FlashDeinit();
304 struct yaffs_dev *yaffs_nor_install_drv(const char *name)
307 struct yaffs_dev *dev = malloc(sizeof(struct yaffs_dev));
308 char *name_copy = strdup(name);
309 struct yaffs_param *param;
310 struct yaffs_driver *drv;
313 if(!dev || !name_copy) {
322 memset(dev, 0, sizeof(*dev));
324 param->name = name_copy;
326 param->total_bytes_per_chunk = DATA_BYTES_PER_CHUNK;
327 param->chunks_per_block = CHUNKS_PER_BLOCK;
328 param->n_reserved_blocks = 2;
329 param->start_block = 0; // Can use block 0
330 param->end_block = BLOCKS_IN_DEVICE - 1; // Last block
331 param->use_nand_ecc = 0; // use YAFFS's ECC
332 param->disable_soft_del = 1;
334 drv->drv_write_chunk_fn = nor_drv_WriteChunkToNAND;
335 drv->drv_read_chunk_fn = nor_drv_ReadChunkFromNAND;
336 drv->drv_erase_fn = nor_drv_EraseBlockInNAND;
337 drv->drv_initialise_fn = nor_drv_InitialiseNAND;
338 drv->drv_deinitialise_fn = nor_drv_Deinitialise_flash_fn;
340 param->n_caches = 10;
341 param->disable_soft_del = 1;
343 dev->driver_context = (void *) nor_sim;
345 yaffs_add_device(dev);