2 * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
4 * Copyright (C) 2002-2010 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 /* First set up for M18 with 1k chunks and 16-byte spares.
20 * NB We're using the oddball M18 modes of operation here
21 * The chip is 64MB based at 0x0000, but YAFFS only going to use the top half
22 * ie. YAFFS will be from 32MB to 64MB.
24 * The M18 has two ways of writing data. Every Programming Region (1kbytes)
25 * can be programmed in two modes:
26 * * Object Mode 1024 bytes of write once data.
27 * * Control Mode: 512bytes of bit-writeable data.
28 * This is arranged as 32 * (16 bytes of bit-writable followed by 16 bytes of "dont touch")
30 * The block size is 256kB, making 128 blocks in the 32MB YAFFS area.
31 * Each block comprises:
32 * Offset 0k: 248 x 1k data pages
33 * Offset 248k: 248 x 32-byte spare areas implemented as 16 bytes of spare followed by 16 bytes untouched)
34 * Offset 248k + (248 * 32): Format marker
38 const char *yaffs_norif1_c_version = "$Id: yaffs_norif1.c,v 1.6 2010-02-18 01:18:04 charles Exp $";
40 #include "yaffs_norif1.h"
43 #include "yaffs_trace.h"
45 #include "yaffs_flashif.h"
46 #include "yaffs_guts.h"
48 #include "devextras.h"
50 #define SPARE_BYTES_PER_CHUNK 16
52 #define PROG_REGION_SIZE 1024
53 #define BLOCK_SIZE_IN_BYTES (256*1024)
54 #define CHUNKS_PER_BLOCK 248
55 #define SPARE_AREA_OFFSET (CHUNKS_PER_BLOCK * PROG_REGION_SIZE)
57 #define FORMAT_OFFSET (SPARE_AREA_OFFSET + CHUNKS_PER_BLOCK * (SPARE_BYTES_PER_CHUNK + M18_SKIP))
59 #define FORMAT_VALUE 0x1234
61 #define DATA_BYTES_PER_CHUNK 1024
62 #define BLOCKS_IN_DEVICE (8*1024/256)
65 #define YNOR_PREMARKER (0xF6)
66 #define YNOR_POSTMARKER (0xF0)
71 /* Compile this for a simulation */
73 #define ynorif1_FlashInit() ynorsim_initialise()
74 #define ynorif1_FlashDeinit() ynorsim_shutdown()
75 #define ynorif1_FlashWrite32(addr,buf,nwords) ynorsim_wr32(addr,buf,nwords)
76 #define ynorif1_FlashRead32(addr,buf,nwords) ynorsim_rd32(addr,buf,nwords)
77 #define ynorif1_FlashEraseBlock(addr) ynorsim_erase(addr)
78 #define DEVICE_BASE ynorsim_get_base()
81 /* Compile this for running on blob, hacked for yaffs access */
82 #include "../blob/yflashrw.h"
83 #define ynorif1_FlashInit() do{} while(0)
84 #define ynorif1_FlashDeinit() do {} while(0)
85 #define ynorif1_FlashWrite32(addr,buf,nwords) Y_FlashWrite(addr,buf,nwords)
86 #define ynorif1_FlashRead32(addr,buf,nwords) Y_FlashRead(addr,buf,nwords)
87 #define ynorif1_FlashEraseBlock(addr) Y_FlashErase(addr,BLOCK_SIZE_IN_BYTES)
88 #define DEVICE_BASE (32 * 1024 * 1024)
91 __u32 *Block2Addr(yaffs_dev_t *dev, int blockNumber)
96 addr = (__u32) DEVICE_BASE;
97 addr += blockNumber * BLOCK_SIZE_IN_BYTES;
99 return (__u32 *) addr;
102 __u32 *Block2FormatAddr(yaffs_dev_t *dev,int blockNumber)
106 addr = (__u32) Block2Addr(dev,blockNumber);
107 addr += FORMAT_OFFSET;
109 return (__u32 *)addr;
111 __u32 *Chunk2DataAddr(yaffs_dev_t *dev,int chunk_id)
114 unsigned chunkInBlock;
117 block = chunk_id/dev->param.chunks_per_block;
118 chunkInBlock = chunk_id % dev->param.chunks_per_block;
120 addr = (__u32) Block2Addr(dev,block);
121 addr += chunkInBlock * DATA_BYTES_PER_CHUNK;
123 return (__u32 *)addr;
126 __u32 *Chunk2SpareAddr(yaffs_dev_t *dev,int chunk_id)
129 unsigned chunkInBlock;
132 block = chunk_id/dev->param.chunks_per_block;
133 chunkInBlock = chunk_id % dev->param.chunks_per_block;
135 addr = (__u32) Block2Addr(dev,block);
136 addr += SPARE_AREA_OFFSET;
137 addr += chunkInBlock * (SPARE_BYTES_PER_CHUNK + M18_SKIP);
138 return (__u32 *)addr;
142 void ynorif1_AndBytes(__u8*target, const __u8 *src, int nbytes)
152 int ynorif1_WriteChunkToNAND(yaffs_dev_t *dev,int nand_chunk,const __u8 *data, const yaffs_spare *spare)
154 __u32 *dataAddr = Chunk2DataAddr(dev,nand_chunk);
155 __u32 *spareAddr = Chunk2SpareAddr(dev,nand_chunk);
157 yaffs_spare tmpSpare;
159 /* We should only be getting called for one of 3 reasons:
160 * Writing a chunk: data and spare will not be NULL
161 * Writing a deletion marker: data will be NULL, spare not NULL
162 * Writing a bad block marker: data will be NULL, spare not NULL
165 if(sizeof(yaffs_spare) != 16)
170 if(spare->page_status != 0xff)
172 /* Write a pre-marker */
173 memset(&tmpSpare,0xff,sizeof(tmpSpare));
174 tmpSpare.page_status = YNOR_PREMARKER;
175 ynorif1_FlashWrite32(spareAddr,(__u32 *)&tmpSpare,sizeof(yaffs_spare)/4);
178 ynorif1_FlashWrite32(dataAddr,(__u32 *)data,dev->param.total_bytes_per_chunk / 4);
181 memcpy(&tmpSpare,spare,sizeof(yaffs_spare));
183 /* Write the real tags, but override the premarker*/
184 tmpSpare.page_status = YNOR_PREMARKER;
185 ynorif1_FlashWrite32(spareAddr,(__u32 *)&tmpSpare,sizeof(yaffs_spare)/4);
187 /* Write a post-marker */
188 tmpSpare.page_status = YNOR_POSTMARKER;
189 ynorif1_FlashWrite32(spareAddr,(__u32 *)&tmpSpare,sizeof(tmpSpare)/4);
192 /* This has to be a read-modify-write operation to handle NOR-ness */
194 ynorif1_FlashRead32(spareAddr,(__u32 *)&tmpSpare,16/ 4);
196 ynorif1_AndBytes((__u8 *)&tmpSpare,(__u8 *)spare,sizeof(yaffs_spare));
198 ynorif1_FlashWrite32(spareAddr,(__u32 *)&tmpSpare,16/ 4);
209 int ynorif1_ReadChunkFromNAND(yaffs_dev_t *dev,int nand_chunk, __u8 *data, yaffs_spare *spare)
212 __u32 *dataAddr = Chunk2DataAddr(dev,nand_chunk);
213 __u32 *spareAddr = Chunk2SpareAddr(dev,nand_chunk);
217 ynorif1_FlashRead32(dataAddr,(__u32 *)data,dev->param.total_bytes_per_chunk / 4);
222 ynorif1_FlashRead32(spareAddr,(__u32 *)spare,16/ 4);
224 /* If the page status is YNOR_POSTMARKER then it was written properly
225 * so change that to 0xFF so that the rest of yaffs is happy.
227 if(spare->page_status == YNOR_POSTMARKER)
228 spare->page_status = 0xFF;
229 else if(spare->page_status != 0xff &&
230 (spare->page_status | YNOR_PREMARKER) != 0xff)
231 spare->page_status = YNOR_PREMARKER;
239 static int ynorif1_FormatBlock(yaffs_dev_t *dev, int blockNumber)
241 __u32 *blockAddr = Block2Addr(dev,blockNumber);
242 __u32 *formatAddr = Block2FormatAddr(dev,blockNumber);
243 __u32 formatValue = FORMAT_VALUE;
245 ynorif1_FlashEraseBlock(blockAddr);
246 ynorif1_FlashWrite32(formatAddr,&formatValue,1);
251 static int ynorif1_UnformatBlock(yaffs_dev_t *dev, int blockNumber)
253 __u32 *formatAddr = Block2FormatAddr(dev,blockNumber);
254 __u32 formatValue = 0;
256 ynorif1_FlashWrite32(formatAddr,&formatValue,1);
261 static int ynorif1_IsBlockFormatted(yaffs_dev_t *dev, int blockNumber)
263 __u32 *formatAddr = Block2FormatAddr(dev,blockNumber);
267 ynorif1_FlashRead32(formatAddr,&formatValue,1);
269 return (formatValue == FORMAT_VALUE);
272 int ynorif1_EraseBlockInNAND(yaffs_dev_t *dev, int blockNumber)
275 if(blockNumber < 0 || blockNumber >= BLOCKS_IN_DEVICE)
277 T(YAFFS_TRACE_ALWAYS,("Attempt to erase non-existant block %d\n",blockNumber));
282 ynorif1_UnformatBlock(dev,blockNumber);
283 ynorif1_FormatBlock(dev,blockNumber);
289 int ynorif1_InitialiseNAND(yaffs_dev_t *dev)
294 /* Go through the blocks formatting them if they are not formatted */
295 for(i = dev->param.start_block; i <= dev->param.end_block; i++){
296 if(!ynorif1_IsBlockFormatted(dev,i)){
297 ynorif1_FormatBlock(dev,i);
303 int ynorif1_Deinitialise_flash_fn(yaffs_dev_t *dev)
306 ynorif1_FlashDeinit();