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 /* 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 #include "yaffs_m18_drv.h"
41 #include "yaffs_trace.h"
43 #include "yaffs_flashif.h"
44 #include "yaffs_guts.h"
46 #define SPARE_BYTES_PER_CHUNK 16
48 #define PROG_REGION_SIZE 1024
49 #define BLOCK_SIZE_IN_BYTES (256*1024)
50 #define CHUNKS_PER_BLOCK 248
51 #define SPARE_AREA_OFFSET (CHUNKS_PER_BLOCK * PROG_REGION_SIZE)
53 #define FORMAT_OFFSET (SPARE_AREA_OFFSET + CHUNKS_PER_BLOCK * \
54 (SPARE_BYTES_PER_CHUNK + M18_SKIP))
56 #define FORMAT_VALUE 0x1234
58 #define DATA_BYTES_PER_CHUNK 1024
59 #define BLOCKS_IN_DEVICE (8*1024/256)
62 #define YNOR_PREMARKER (0xF6)
63 #define YNOR_POSTMARKER (0xF0)
68 /* Compile this for a simulation */
71 static struct nor_sim *nor_sim;
73 #define m18_drv_FlashInit() do {nor_sim = ynorsim_initialise("emfile-m18", BLOCKS_IN_DEVICE, BLOCK_SIZE_IN_BYTES); } while(0)
74 #define m18_drv_FlashDeinit() ynorsim_shutdown(nor_sim)
75 #define m18_drv_FlashWrite32(addr,buf,nwords) ynorsim_wr32(nor_sim,addr,buf,nwords)
76 #define m18_drv_FlashRead32(addr,buf,nwords) ynorsim_rd32(nor_sim,addr,buf,nwords)
77 #define m18_drv_FlashEraseBlock(addr) ynorsim_erase(nor_sim,addr)
78 #define DEVICE_BASE ynorsim_get_base(nor_sim)
82 /* Compile this to hook up to read hardware */
83 #include "../blob/yflashrw.h"
84 #define m18_drv_FlashInit() do{} while(0)
85 #define m18_drv_FlashDeinit() do {} while(0)
86 #define m18_drv_FlashWrite32(addr,buf,nwords) Y_FlashWrite(addr,buf,nwords)
87 #define m18_drv_FlashRead32(addr,buf,nwords) Y_FlashRead(addr,buf,nwords)
88 #define m18_drv_FlashEraseBlock(addr) Y_FlashErase(addr,BLOCK_SIZE_IN_BYTES)
89 #define DEVICE_BASE (32 * 1024 * 1024)
93 static u32 *Block2Addr(struct yaffs_dev *dev, int blockNumber)
98 addr = (u8*) DEVICE_BASE;
99 addr += blockNumber * BLOCK_SIZE_IN_BYTES;
104 static u32 *Block2FormatAddr(struct yaffs_dev *dev,int blockNumber)
108 addr = (u8*) Block2Addr(dev,blockNumber);
109 addr += FORMAT_OFFSET;
114 static u32 *Chunk2DataAddr(struct yaffs_dev *dev,int chunk_id)
117 unsigned chunkInBlock;
120 block = chunk_id/dev->param.chunks_per_block;
121 chunkInBlock = chunk_id % dev->param.chunks_per_block;
123 addr = (u8*) Block2Addr(dev,block);
124 addr += chunkInBlock * DATA_BYTES_PER_CHUNK;
129 static u32 *Chunk2SpareAddr(struct yaffs_dev *dev,int chunk_id)
132 unsigned chunkInBlock;
135 block = chunk_id/dev->param.chunks_per_block;
136 chunkInBlock = chunk_id % dev->param.chunks_per_block;
138 addr = (u8*) Block2Addr(dev,block);
139 addr += SPARE_AREA_OFFSET;
140 addr += chunkInBlock * (SPARE_BYTES_PER_CHUNK + M18_SKIP);
145 static void m18_drv_AndBytes(u8*target, const u8 *src, int nbytes)
155 static int m18_drv_WriteChunkToNAND(struct yaffs_dev *dev,int nand_chunk,
156 const u8 *data, int data_len,
157 const u8 *oob, int oob_len)
159 u32 *dataAddr = Chunk2DataAddr(dev,nand_chunk);
160 u32 *spareAddr = Chunk2SpareAddr(dev,nand_chunk);
162 struct yaffs_spare *spare = (struct yaffs_spare *)oob;
163 struct yaffs_spare tmpSpare;
167 /* We should only be getting called for one of 3 reasons:
168 * Writing a chunk: data and spare will not be NULL
169 * Writing a deletion marker: data will be NULL, spare not NULL
170 * Writing a bad block marker: data will be NULL, spare not NULL
173 if(sizeof(struct yaffs_spare) != 16)
178 if(spare->page_status != 0xff)
180 /* Write a pre-marker */
181 memset(&tmpSpare,0xff,sizeof(tmpSpare));
182 tmpSpare.page_status = YNOR_PREMARKER;
183 m18_drv_FlashWrite32(spareAddr,(u32 *)&tmpSpare,sizeof(struct yaffs_spare)/4);
186 m18_drv_FlashWrite32(dataAddr,(u32 *)data, data_len/ 4);
189 memcpy(&tmpSpare,spare,sizeof(struct yaffs_spare));
191 /* Write the real tags, but override the premarker*/
192 tmpSpare.page_status = YNOR_PREMARKER;
193 m18_drv_FlashWrite32(spareAddr,(u32 *)&tmpSpare,sizeof(struct yaffs_spare)/4);
195 /* Write a post-marker */
196 tmpSpare.page_status = YNOR_POSTMARKER;
197 m18_drv_FlashWrite32(spareAddr,(u32 *)&tmpSpare,sizeof(tmpSpare)/4);
200 /* This has to be a read-modify-write operation to handle NOR-ness */
202 m18_drv_FlashRead32(spareAddr,(u32 *)&tmpSpare,16/ 4);
204 m18_drv_AndBytes((u8 *)&tmpSpare,(u8 *)spare,sizeof(struct yaffs_spare));
206 m18_drv_FlashWrite32(spareAddr,(u32 *)&tmpSpare,16/ 4);
217 static int m18_drv_ReadChunkFromNAND(struct yaffs_dev *dev,int nand_chunk,
218 u8 *data, int data_len,
219 u8 *oob, int oob_len,
220 enum yaffs_ecc_result *ecc_result)
222 struct yaffs_spare *spare = (struct yaffs_spare *)oob;
224 u32 *dataAddr = Chunk2DataAddr(dev,nand_chunk);
225 u32 *spareAddr = Chunk2SpareAddr(dev,nand_chunk);
231 m18_drv_FlashRead32(dataAddr,(u32 *)data,dev->param.total_bytes_per_chunk / 4);
236 m18_drv_FlashRead32(spareAddr,(u32 *)spare, oob_len/ 4);
238 /* If the page status is YNOR_POSTMARKER then it was written properly
239 * so change that to 0xFF so that the rest of yaffs is happy.
241 if(spare->page_status == YNOR_POSTMARKER)
242 spare->page_status = 0xFF;
243 else if(spare->page_status != 0xff &&
244 (spare->page_status | YNOR_PREMARKER) != 0xff)
245 spare->page_status = YNOR_PREMARKER;
249 *ecc_result = YAFFS_ECC_RESULT_NO_ERROR;
256 static int m18_drv_FormatBlock(struct yaffs_dev *dev, int blockNumber)
258 u32 *blockAddr = Block2Addr(dev,blockNumber);
259 u32 *formatAddr = Block2FormatAddr(dev,blockNumber);
260 u32 formatValue = FORMAT_VALUE;
262 m18_drv_FlashEraseBlock(blockAddr);
263 m18_drv_FlashWrite32(formatAddr,&formatValue,1);
268 static int m18_drv_UnformatBlock(struct yaffs_dev *dev, int blockNumber)
270 u32 *formatAddr = Block2FormatAddr(dev,blockNumber);
273 m18_drv_FlashWrite32(formatAddr,&formatValue,1);
278 static int m18_drv_IsBlockFormatted(struct yaffs_dev *dev, int blockNumber)
280 u32 *formatAddr = Block2FormatAddr(dev,blockNumber);
284 m18_drv_FlashRead32(formatAddr,&formatValue,1);
286 return (formatValue == FORMAT_VALUE);
289 static int m18_drv_EraseBlockInNAND(struct yaffs_dev *dev, int blockNumber)
292 if(blockNumber < 0 || blockNumber >= BLOCKS_IN_DEVICE)
294 yaffs_trace(YAFFS_TRACE_ALWAYS,
295 "Attempt to erase non-existant block %d\n",
301 m18_drv_UnformatBlock(dev,blockNumber);
302 m18_drv_FormatBlock(dev,blockNumber);
308 static int m18_drv_InitialiseNAND(struct yaffs_dev *dev)
313 /* Go through the blocks formatting them if they are not formatted */
314 for(i = dev->param.start_block; i <= dev->param.end_block; i++){
315 if(!m18_drv_IsBlockFormatted(dev,i)){
316 m18_drv_FormatBlock(dev,i);
322 static int m18_drv_Deinitialise_flash_fn(struct yaffs_dev *dev)
326 m18_drv_FlashDeinit();
332 struct yaffs_dev *yaffs_m18_install_drv(const char *name)
335 struct yaffs_dev *dev = malloc(sizeof(struct yaffs_dev));
336 char *name_copy = strdup(name);
337 struct yaffs_param *param;
338 struct yaffs_driver *drv;
341 if(!dev || !name_copy) {
350 memset(dev, 0, sizeof(*dev));
352 param->name = name_copy;
354 param->total_bytes_per_chunk = 1024;
355 param->chunks_per_block =248;
356 param->n_reserved_blocks = 2;
357 param->start_block = 0; // Can use block 0
358 param->end_block = 31; // Last block
359 param->use_nand_ecc = 0; // use YAFFS's ECC
361 drv->drv_write_chunk_fn = m18_drv_WriteChunkToNAND;
362 drv->drv_read_chunk_fn = m18_drv_ReadChunkFromNAND;
363 drv->drv_erase_fn = m18_drv_EraseBlockInNAND;
364 drv->drv_initialise_fn = m18_drv_InitialiseNAND;
365 drv->drv_deinitialise_fn = m18_drv_Deinitialise_flash_fn;
367 param->n_caches = 10;
368 param->disable_soft_del = 1;
370 dev->driver_context = (void *) 1; // Used to identify the device in fstat.
372 yaffs_add_device(dev);