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 /* First set up for M18 with 1k chunks and 16-byte spares.
19 * NB We're using the oddball M18 modes of operation here
20 * The chip is 64MB based at 0x0000, but YAFFS only going to use the top half
21 * ie. YAFFS will be from 32MB to 64MB.
23 * The M18 has two ways of writing data. Every Programming Region (1kbytes)
24 * can be programmed in two modes:
25 * * Object Mode 1024 bytes of write once data.
26 * * Control Mode: 512bytes of bit-writeable data.
27 * This is arranged as 32 * (16 bytes of bit-writable followed by 16 bytes of "dont touch")
29 * The block size is 256kB, making 128 blocks in the 32MB YAFFS area.
30 * Each block comprises:
31 * Offset 0k: 248 x 1k data pages
32 * Offset 248k: 248 x 32-byte spare areas implemented as 16 bytes of spare followed by 16 bytes untouched)
33 * Offset 248k + (248 * 32): Format marker
37 #include "yaffs_m18_drv.h"
40 #include "yaffs_trace.h"
42 #include "yaffs_flashif.h"
43 #include "yaffs_guts.h"
45 #define SPARE_BYTES_PER_CHUNK 16
47 #define PROG_REGION_SIZE 1024
48 #define BLOCK_SIZE_IN_BYTES (256*1024)
49 #define CHUNKS_PER_BLOCK 248
50 #define SPARE_AREA_OFFSET (CHUNKS_PER_BLOCK * PROG_REGION_SIZE)
52 #define FORMAT_OFFSET (SPARE_AREA_OFFSET + CHUNKS_PER_BLOCK * \
53 (SPARE_BYTES_PER_CHUNK + M18_SKIP))
55 #define FORMAT_VALUE 0x1234
57 #define DATA_BYTES_PER_CHUNK 1024
58 #define BLOCKS_IN_DEVICE (8*1024/256)
61 #define YNOR_PREMARKER (0xF6)
62 #define YNOR_POSTMARKER (0xF0)
67 /* Compile this for a simulation */
70 static struct nor_sim *nor_sim;
72 #define m18_drv_FlashInit() do {nor_sim = ynorsim_initialise("emfile-m18", BLOCKS_IN_DEVICE, BLOCK_SIZE_IN_BYTES); } while(0)
73 #define m18_drv_FlashDeinit() ynorsim_shutdown(nor_sim)
74 #define m18_drv_FlashWrite32(addr,buf,nwords) ynorsim_wr32(nor_sim,addr,buf,nwords)
75 #define m18_drv_FlashRead32(addr,buf,nwords) ynorsim_rd32(nor_sim,addr,buf,nwords)
76 #define m18_drv_FlashEraseBlock(addr) ynorsim_erase(nor_sim,addr)
77 #define DEVICE_BASE ynorsim_get_base(nor_sim)
81 /* Compile this to hook up to read hardware */
82 #include "../blob/yflashrw.h"
83 #define m18_drv_FlashInit() do{} while(0)
84 #define m18_drv_FlashDeinit() do {} while(0)
85 #define m18_drv_FlashWrite32(addr,buf,nwords) Y_FlashWrite(addr,buf,nwords)
86 #define m18_drv_FlashRead32(addr,buf,nwords) Y_FlashRead(addr,buf,nwords)
87 #define m18_drv_FlashEraseBlock(addr) Y_FlashErase(addr,BLOCK_SIZE_IN_BYTES)
88 #define DEVICE_BASE (32 * 1024 * 1024)
92 static u32 *Block2Addr(struct yaffs_dev *dev, int blockNumber)
97 addr = (u8*) DEVICE_BASE;
98 addr += blockNumber * BLOCK_SIZE_IN_BYTES;
103 static u32 *Block2FormatAddr(struct yaffs_dev *dev,int blockNumber)
107 addr = (u8*) Block2Addr(dev,blockNumber);
108 addr += FORMAT_OFFSET;
113 static u32 *Chunk2DataAddr(struct yaffs_dev *dev,int chunk_id)
116 unsigned chunkInBlock;
119 block = chunk_id/dev->param.chunks_per_block;
120 chunkInBlock = chunk_id % dev->param.chunks_per_block;
122 addr = (u8*) Block2Addr(dev,block);
123 addr += chunkInBlock * DATA_BYTES_PER_CHUNK;
128 static u32 *Chunk2SpareAddr(struct yaffs_dev *dev,int chunk_id)
131 unsigned chunkInBlock;
134 block = chunk_id/dev->param.chunks_per_block;
135 chunkInBlock = chunk_id % dev->param.chunks_per_block;
137 addr = (u8*) Block2Addr(dev,block);
138 addr += SPARE_AREA_OFFSET;
139 addr += chunkInBlock * (SPARE_BYTES_PER_CHUNK + M18_SKIP);
144 static void m18_drv_AndBytes(u8*target, const u8 *src, int nbytes)
154 static int m18_drv_WriteChunkToNAND(struct yaffs_dev *dev,int nand_chunk,
155 const u8 *data, int data_len,
156 const u8 *oob, int oob_len)
158 u32 *dataAddr = Chunk2DataAddr(dev,nand_chunk);
159 u32 *spareAddr = Chunk2SpareAddr(dev,nand_chunk);
161 struct yaffs_spare *spare = (struct yaffs_spare *)oob;
162 struct yaffs_spare tmpSpare;
166 /* We should only be getting called for one of 3 reasons:
167 * Writing a chunk: data and spare will not be NULL
168 * Writing a deletion marker: data will be NULL, spare not NULL
169 * Writing a bad block marker: data will be NULL, spare not NULL
172 if(sizeof(struct yaffs_spare) != 16)
177 if(spare->page_status != 0xff)
179 /* Write a pre-marker */
180 memset(&tmpSpare,0xff,sizeof(tmpSpare));
181 tmpSpare.page_status = YNOR_PREMARKER;
182 m18_drv_FlashWrite32(spareAddr,(u32 *)&tmpSpare,sizeof(struct yaffs_spare)/4);
185 m18_drv_FlashWrite32(dataAddr,(u32 *)data, data_len/ 4);
188 memcpy(&tmpSpare,spare,sizeof(struct yaffs_spare));
190 /* Write the real tags, but override the premarker*/
191 tmpSpare.page_status = YNOR_PREMARKER;
192 m18_drv_FlashWrite32(spareAddr,(u32 *)&tmpSpare,sizeof(struct yaffs_spare)/4);
194 /* Write a post-marker */
195 tmpSpare.page_status = YNOR_POSTMARKER;
196 m18_drv_FlashWrite32(spareAddr,(u32 *)&tmpSpare,sizeof(tmpSpare)/4);
199 /* This has to be a read-modify-write operation to handle NOR-ness */
201 m18_drv_FlashRead32(spareAddr,(u32 *)&tmpSpare,16/ 4);
203 m18_drv_AndBytes((u8 *)&tmpSpare,(u8 *)spare,sizeof(struct yaffs_spare));
205 m18_drv_FlashWrite32(spareAddr,(u32 *)&tmpSpare,16/ 4);
216 static int m18_drv_ReadChunkFromNAND(struct yaffs_dev *dev,int nand_chunk,
217 u8 *data, int data_len,
218 u8 *oob, int oob_len,
219 enum yaffs_ecc_result *ecc_result)
221 struct yaffs_spare *spare = (struct yaffs_spare *)oob;
223 u32 *dataAddr = Chunk2DataAddr(dev,nand_chunk);
224 u32 *spareAddr = Chunk2SpareAddr(dev,nand_chunk);
230 m18_drv_FlashRead32(dataAddr,(u32 *)data,dev->param.total_bytes_per_chunk / 4);
235 m18_drv_FlashRead32(spareAddr,(u32 *)spare, oob_len/ 4);
237 /* If the page status is YNOR_POSTMARKER then it was written properly
238 * so change that to 0xFF so that the rest of yaffs is happy.
240 if(spare->page_status == YNOR_POSTMARKER)
241 spare->page_status = 0xFF;
242 else if(spare->page_status != 0xff &&
243 (spare->page_status | YNOR_PREMARKER) != 0xff)
244 spare->page_status = YNOR_PREMARKER;
248 *ecc_result = YAFFS_ECC_RESULT_NO_ERROR;
255 static int m18_drv_FormatBlock(struct yaffs_dev *dev, int blockNumber)
257 u32 *blockAddr = Block2Addr(dev,blockNumber);
258 u32 *formatAddr = Block2FormatAddr(dev,blockNumber);
259 u32 formatValue = FORMAT_VALUE;
261 m18_drv_FlashEraseBlock(blockAddr);
262 m18_drv_FlashWrite32(formatAddr,&formatValue,1);
267 static int m18_drv_UnformatBlock(struct yaffs_dev *dev, int blockNumber)
269 u32 *formatAddr = Block2FormatAddr(dev,blockNumber);
272 m18_drv_FlashWrite32(formatAddr,&formatValue,1);
277 static int m18_drv_IsBlockFormatted(struct yaffs_dev *dev, int blockNumber)
279 u32 *formatAddr = Block2FormatAddr(dev,blockNumber);
283 m18_drv_FlashRead32(formatAddr,&formatValue,1);
285 return (formatValue == FORMAT_VALUE);
288 static int m18_drv_EraseBlockInNAND(struct yaffs_dev *dev, int blockNumber)
291 if(blockNumber < 0 || blockNumber >= BLOCKS_IN_DEVICE)
293 yaffs_trace(YAFFS_TRACE_ALWAYS,
294 "Attempt to erase non-existant block %d\n",
300 m18_drv_UnformatBlock(dev,blockNumber);
301 m18_drv_FormatBlock(dev,blockNumber);
307 static int m18_drv_InitialiseNAND(struct yaffs_dev *dev)
312 /* Go through the blocks formatting them if they are not formatted */
313 for(i = dev->param.start_block; i <= dev->param.end_block; i++){
314 if(!m18_drv_IsBlockFormatted(dev,i)){
315 m18_drv_FormatBlock(dev,i);
321 static int m18_drv_Deinitialise_flash_fn(struct yaffs_dev *dev)
325 m18_drv_FlashDeinit();
331 struct yaffs_dev *yaffs_m18_install_drv(const char *name)
334 struct yaffs_dev *dev = malloc(sizeof(struct yaffs_dev));
335 char *name_copy = strdup(name);
336 struct yaffs_param *param;
337 struct yaffs_driver *drv;
340 if(!dev || !name_copy) {
349 memset(dev, 0, sizeof(*dev));
351 param->name = name_copy;
353 param->total_bytes_per_chunk = 1024;
354 param->chunks_per_block =248;
355 param->n_reserved_blocks = 2;
356 param->start_block = 0; // Can use block 0
357 param->end_block = 31; // Last block
358 param->use_nand_ecc = 0; // use YAFFS's ECC
360 drv->drv_write_chunk_fn = m18_drv_WriteChunkToNAND;
361 drv->drv_read_chunk_fn = m18_drv_ReadChunkFromNAND;
362 drv->drv_erase_fn = m18_drv_EraseBlockInNAND;
363 drv->drv_initialise_fn = m18_drv_InitialiseNAND;
364 drv->drv_deinitialise_fn = m18_drv_Deinitialise_flash_fn;
366 param->n_caches = 10;
367 param->disable_soft_del = 1;
369 dev->driver_context = (void *) 1; // Used to identify the device in fstat.
371 yaffs_add_device(dev);