Adding fsx test to yaffs direct
[yaffs2.git] / mtdemul / nandemul2k.c
1 /*
2  * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
3  *
4  * Copyright (C) 2002-2007 Aleph One Ltd.
5  *   for Toby Churchill Ltd and Brightstar Engineering
6  *
7  * Created by Charles Manning <charles@aleph1.co.uk>
8  *
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.
12  */
13
14 /*
15  *  This version hacked for emulating 2kpage NAND for YAFFS2 testing.
16  */
17
18 #include <linux/config.h>
19 #include <linux/kernel.h>
20 #include <linux/module.h>
21 #include <linux/version.h>
22 #include <linux/slab.h>
23 #include <linux/init.h>
24 #include <linux/list.h>
25 #include <linux/fs.h>
26 #include <linux/proc_fs.h>
27 #include <linux/pagemap.h>
28 #include <linux/mtd/mtd.h>
29 #include <linux/interrupt.h>
30 #include <linux/string.h>
31 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
32 #include <linux/locks.h>
33 #endif
34
35 #include <asm/uaccess.h>
36 #include <linux/mtd/mtd.h>
37 #include <linux/mtd/partitions.h>
38 #include <linux/mtd/nand.h>
39 #include "../yaffs_nandemul2k.h"
40
41 #define ALLOCATE(x) kmalloc(x,GFP_KERNEL)
42 #define FREE(x)     kfree(x)
43
44
45
46
47
48 #define NAND_SHIFT      (11)   // Shifter for 2k
49 #define PAGE_DATA_SIZE  (1 << NAND_SHIFT)
50 #define PAGE_SPARE_SIZE (64)
51 #define BLK_SHIFT       6
52 #define PAGES_PER_BLOCK (1 << BLK_SHIFT)        // = 64
53
54
55 #define EM_SIZE_IN_MEG 4
56 #define EM_SIZE_IN_BYTES (EM_SIZE_IN_MEG * (1<<20))
57
58 #define PAGE_TOTAL_SIZE (PAGE_DATA_SIZE+PAGE_SPARE_SIZE)
59
60 #define BLOCK_TOTAL_SIZE (PAGES_PER_BLOCK * PAGE_TOTAL_SIZE)
61
62 #define BLOCKS_PER_MEG ((1<<20)/(PAGES_PER_BLOCK * PAGE_DATA_SIZE))
63
64
65 static struct mtd_info nandemul2k_mtd;
66
67 typedef struct 
68 {
69         __u8 data[PAGE_TOTAL_SIZE]; // Data + spare
70         int empty;      // is this empty?
71 } nandemul_Page;
72
73
74 typedef struct
75 {
76         nandemul_Page *page[PAGES_PER_BLOCK];
77         int damaged;    
78 } nandemul_Block;
79
80
81
82 typedef struct
83 {
84         nandemul_Block**block;
85         int nBlocks;
86 } nandemul_Device;
87
88 static nandemul_Device ned;
89
90 static int sizeInMB = EM_SIZE_IN_MEG;
91
92
93 static void nandemul_yield(int n)
94 {
95 #ifdef __KERNEL__
96         if(n > 0) schedule_timeout(n);
97 #endif
98
99 }
100
101
102 static void nandemul2k_Read(void *buffer, int page, int start, int nBytes)
103 {
104         int pg = page%PAGES_PER_BLOCK;
105         int blk = page/PAGES_PER_BLOCK;
106         if(buffer && nBytes > 0)
107         {
108                 memcpy(buffer,&ned.block[blk]->page[pg]->data[start],nBytes);
109         }
110         
111 }
112
113 static void nandemul2k_Program(const void *buffer, int page, int start, int nBytes)
114 {
115         int pg = page%PAGES_PER_BLOCK;
116         int blk = page/PAGES_PER_BLOCK;
117         __u8 *p;
118         __u8 *b = (__u8 *)buffer;
119
120         p = &ned.block[blk]->page[pg]->data[start];
121         
122         while(buffer && nBytes>0)
123         {
124                 *p = *p & *b;
125                 p++;
126                 b++;
127                 nBytes--;
128         }
129 }
130
131 static void nandemul2k_DoErase(int blockNumber)
132 {
133         int i;
134         
135         nandemul_Block *blk;
136         
137         if(blockNumber < 0 || blockNumber >= ned.nBlocks)
138         {
139                 return;
140         }
141         
142         blk = ned.block[blockNumber];
143         
144         for(i = 0; i < PAGES_PER_BLOCK; i++)
145         {
146                 memset(blk->page[i],0xff,sizeof(nandemul_Page));
147                 blk->page[i]->empty = 1;
148         }
149         nandemul_yield(2);
150 }
151
152
153 static int nandemul2k_CalcNBlocks(void)
154 {
155         return EM_SIZE_IN_MEG * BLOCKS_PER_MEG;
156 }
157
158
159
160 static int  CheckInit(void)
161 {
162         static int initialised = 0;
163         
164         int i,j;
165         
166         int fail = 0;
167         int nBlocks; 
168
169         int nAllocated = 0;
170         
171         if(initialised) 
172         {
173                 return 0;
174         }
175         
176         
177         ned.nBlocks = nBlocks = nandemul2k_CalcNBlocks();
178
179         
180         ned.block = ALLOCATE(sizeof(nandemul_Block*) * nBlocks );
181         
182         if(!ned.block) return ENOMEM;
183         
184         
185         
186
187                 
188         for(i=fail=0; i <nBlocks; i++)
189         {
190                 
191                 nandemul_Block *blk;
192                 
193                 if(!(blk = ned.block[i] = ALLOCATE(sizeof(nandemul_Block))))
194                 {
195                  fail = 1;
196                 }  
197                 else
198                 {
199                         for(j = 0; j < PAGES_PER_BLOCK; j++)
200                         {
201                                 if((blk->page[j] = ALLOCATE(sizeof(nandemul_Page))) == 0)
202                                 {
203                                         fail = 1;
204                                 }
205                         }
206                         nandemul2k_DoErase(i);
207                         ned.block[i]->damaged = 0;
208                         nAllocated++;
209                 }
210         }
211         
212         if(fail)
213         {
214                 //Todo thump pages
215                 
216                 for(i = 0; i < nAllocated; i++)
217                 {
218                         FREE(ned.block[i]);
219                 }
220                 FREE(ned.block);
221                 
222                 return ENOMEM;
223         }
224         
225         ned.nBlocks = nBlocks;
226         
227         initialised = 1;
228         
229         return 1;
230 }
231
232
233
234 static void nandemul2k_CleanUp(void)
235 {
236         int i,j;
237         
238         for(i = 0; i < ned.nBlocks; i++)
239         {
240                 for(j = 0; j < PAGES_PER_BLOCK; j++)
241                 {
242                    FREE(ned.block[i]->page[j]);
243                 }
244                 FREE(ned.block[i]);
245                 
246         }
247         FREE(ned.block);
248         ned.block = 0;
249 }
250
251 int nandemul2k_GetBytesPerChunk(void) { return PAGE_DATA_SIZE;}
252
253 int nandemul2k_GetChunksPerBlock(void) { return PAGES_PER_BLOCK; }
254 int nandemul2k_GetNumberOfBlocks(void) {return nandemul2k_CalcNBlocks();}
255
256
257
258 static int nandemul2k_ReadId(__u8 *vendorId, __u8 *deviceId)
259 {
260         *vendorId = 'Y'; 
261         *deviceId = '2';
262         
263         return 1;
264 }
265
266
267 static int nandemul2k_ReadStatus(__u8 *status)
268 {
269                 *status = 0;
270                 return 1;
271 }
272
273
274 #ifdef CONFIG_MTD_NAND_ECC
275 #include <linux/mtd/nand_ecc.h>
276 #endif
277
278 /*
279  * NAND low-level MTD interface functions
280  */
281 static int nand_read (struct mtd_info *mtd, loff_t from, size_t len,
282                         size_t *retlen, u_char *buf);
283 static int nand_read_ecc (struct mtd_info *mtd, loff_t from, size_t len,
284                                 size_t *retlen, u_char *buf, u_char *oob_buf, struct nand_oobinfo *dummy);
285 static int nand_read_oob (struct mtd_info *mtd, loff_t from, size_t len,
286                                 size_t *retlen, u_char *buf);
287 static int nand_write (struct mtd_info *mtd, loff_t to, size_t len,
288                         size_t *retlen, const u_char *buf);
289 static int nand_write_ecc (struct mtd_info *mtd, loff_t to, size_t len,
290                                 size_t *retlen, const u_char *buf,
291                                 u_char *oob_buf, struct nand_oobinfo *dummy);
292 static int nand_write_oob (struct mtd_info *mtd, loff_t to, size_t len,
293                                 size_t *retlen, const u_char *buf);
294 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,7))
295 static int nand_writev (struct mtd_info *mtd, const struct kvec *vecs,
296                                 unsigned long count, loff_t to, size_t *retlen);
297 #else
298 static int nand_writev (struct mtd_info *mtd, const struct iovec *vecs,
299                                 unsigned long count, loff_t to, size_t *retlen);
300 #endif
301 static int nand_erase (struct mtd_info *mtd, struct erase_info *instr);
302 static void nand_sync (struct mtd_info *mtd);
303
304
305
306 /*
307  * NAND read
308  */
309 static int nand_read (struct mtd_info *mtd, loff_t from, size_t len,
310                         size_t *retlen, u_char *buf)
311 {
312         return nand_read_ecc (mtd, from, len, retlen, buf, NULL,NULL);
313 }
314
315
316 /*
317  * NAND read with ECC
318  */
319 static int nand_read_ecc (struct mtd_info *mtd, loff_t from, size_t len,
320                                 size_t *retlen, u_char *buf, u_char *oob_buf,struct nand_oobinfo *oobsel)
321 {
322         int     start, page;
323         int n = len;
324         int nToCopy;
325
326
327
328         /* Do not allow reads past end of device */
329         if ((from + len) > mtd->size) {
330                 *retlen = 0;
331                 return -EINVAL;
332         }
333
334
335         /* Initialize return value */
336         *retlen = 0;
337
338         while(n > 0)
339         {
340
341                 /* First we calculate the starting page */
342                 page = from >> NAND_SHIFT;
343
344                 /* Get raw starting column */
345
346                 start = from & (mtd->oobblock-1);
347
348                 // OK now check for the curveball where the start and end are in
349                 // the same page
350                 if((start + n) < mtd->oobblock)
351                 {
352                         nToCopy = n;
353                 }
354                 else
355                 {
356                         nToCopy =  mtd->oobblock - start;
357                 }
358
359                 nandemul2k_Read(buf, page, start, nToCopy);
360                 nandemul2k_Read(oob_buf,page,PAGE_DATA_SIZE,PAGE_SPARE_SIZE);
361
362                 n -= nToCopy;
363                 from += nToCopy;
364                 buf += nToCopy;
365                 if(oob_buf) oob_buf += PAGE_SPARE_SIZE;
366                 *retlen += nToCopy;
367
368         }
369
370
371         return 0;
372 }
373
374 /*
375  * NAND read out-of-band
376  */
377 static int nand_read_oob (struct mtd_info *mtd, loff_t from, size_t len,
378                                 size_t *retlen, u_char *buf)
379 {
380         int col, page;
381
382         T(0,("nand_read_oob: from = 0x%08x, buf = 0x%08x, len = %i\n", (unsigned int) from, (unsigned int) buf,
383                 (int) len));
384
385         /* Shift to get page */
386         page = ((int) from) >> NAND_SHIFT;
387
388         /* Mask to get column */
389         col = from & 0x0f;
390
391         /* Initialize return length value */
392         *retlen = 0;
393
394         /* Do not allow reads past end of device */
395         if ((from + len) > mtd->size) {
396                 T(0,
397                         ("nand_read_oob: Attempt read beyond end of device\n"));
398                 *retlen = 0;
399                 return -EINVAL;
400         }
401
402         nandemul2k_Read(buf,page,PAGE_DATA_SIZE + col,len);
403
404         /* Return happy */
405         *retlen = len;
406         return 0;
407 }
408
409 /*
410  * NAND write
411  */
412 static int nand_write (struct mtd_info *mtd, loff_t to, size_t len,
413                         size_t *retlen, const u_char *buf)
414 {
415         return nand_write_ecc (mtd, to, len, retlen, buf, NULL,NULL);
416 }
417
418 /*
419  * NAND write with ECC
420  */
421 static int nand_write_ecc (struct mtd_info *mtd, loff_t to, size_t len,
422                                 size_t *retlen, const u_char *buf,
423                                 u_char *oob_buf, struct nand_oobinfo *dummy)
424 {
425
426         int     start, page;
427         int n = len;
428         int nToCopy;
429
430
431
432         /* Do not allow reads past end of device */
433         if ((to + len) > mtd->size) {
434                 *retlen = 0;
435                 return -EINVAL;
436         }
437
438
439         /* Initialize return value */
440         *retlen = 0;
441
442         while(n > 0)
443         {
444
445                 /* First we calculate the starting page */
446                 page = to >> NAND_SHIFT;
447
448                 /* Get raw starting column */
449
450                 start = to & (mtd->oobblock - 1);
451
452                 // OK now check for the curveball where the start and end are in
453                 // the same page
454                 if((start + n) < mtd->oobblock)
455                 {
456                         nToCopy = n;
457                 }
458                 else
459                 {
460                         nToCopy =  mtd->oobblock - start;
461                 }
462
463                 nandemul2k_Program(buf, page, start, nToCopy);
464                 nandemul2k_Program(oob_buf, page, PAGE_DATA_SIZE, PAGE_SPARE_SIZE);
465
466                 n -= nToCopy;
467                 to += nToCopy;
468                 buf += nToCopy;
469                 if(oob_buf) oob_buf += PAGE_SPARE_SIZE;
470                 *retlen += nToCopy;
471
472         }
473
474
475         return 0;
476 }
477
478 /*
479  * NAND write out-of-band
480  */
481 static int nand_write_oob (struct mtd_info *mtd, loff_t to, size_t len,
482                                 size_t *retlen, const u_char *buf)
483 {
484         int col, page;
485
486
487         T(0,(
488                 "nand_read_oob: to = 0x%08x, len = %i\n", (unsigned int) to,
489                 (int) len));
490
491         /* Shift to get page */
492         page = ((int) to) >> NAND_SHIFT;
493
494         /* Mask to get column */
495         col = to & 0x0f;
496
497         /* Initialize return length value */
498         *retlen = 0;
499
500         /* Do not allow reads past end of device */
501         if ((to + len) > mtd->size) {
502                 T(0,(
503                    "nand_read_oob: Attempt read beyond end of device\n"));
504                 *retlen = 0;
505                 return -EINVAL;
506         }
507
508         nandemul2k_Program(buf,page,512 + col,len);
509
510         /* Return happy */
511         *retlen = len;
512         return 0;
513
514 }
515
516 /*
517  * NAND write with iovec
518  */
519 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,7))
520 static int nand_writev (struct mtd_info *mtd, const struct kvec *vecs,
521                                 unsigned long count, loff_t to, size_t *retlen)
522 #else
523 static int nand_writev (struct mtd_info *mtd, const struct iovec *vecs,
524                                 unsigned long count, loff_t to, size_t *retlen)
525 #endif
526 {
527         return -EINVAL;
528 }
529
530 /*
531  * NAND erase a block
532  */
533 static int nand_erase (struct mtd_info *mtd, struct erase_info *instr)
534 {
535         int i, nBlocks,block;
536
537         T(0,(
538                 "nand_erase: start = 0x%08x, len = %i\n",
539                 (unsigned int) instr->addr, (unsigned int) instr->len));
540
541         /* Start address must align on block boundary */
542         if (instr->addr & (mtd->erasesize - 1)) {
543                 T(0,(
544                         "nand_erase: Unaligned address\n"));
545                 return -EINVAL;
546         }
547
548         /* Length must align on block boundary */
549         if (instr->len & (mtd->erasesize - 1)) {
550                 T(0,(
551                         "nand_erase: Length not block aligned\n"));
552                 return -EINVAL;
553         }
554
555         /* Do not allow erase past end of device */
556         if ((instr->len + instr->addr) > mtd->size) {
557                 T(0,(
558                         "nand_erase: Erase past end of device\n"));
559                 return -EINVAL;
560         }
561
562         nBlocks = instr->len >> (NAND_SHIFT + BLK_SHIFT);
563         block = instr->addr >> (NAND_SHIFT + BLK_SHIFT);
564
565         for(i = 0; i < nBlocks; i++)
566         {
567                 nandemul2k_DoErase(block);
568                 block++;
569         }
570
571
572
573         return 0;
574
575
576 }
577
578
579 static int nand_block_isbad(struct mtd_info *mtd, loff_t ofs)
580 {
581         return 0;
582 }
583
584 static int nand_block_markbad(struct mtd_info *mtd, loff_t ofs)
585 {
586         return 0;
587 }
588
589
590 /*
591  * NAND sync
592  */
593 static void nand_sync (struct mtd_info *mtd)
594 {
595         T(0,("nand_sync: called\n"));
596
597 }
598
599 /*
600  * Scan for the NAND device
601  */
602 static int nandemul2k_scan (struct mtd_info *mtd,int nchips)
603 {
604         mtd->oobblock = PAGE_DATA_SIZE;
605         mtd->oobsize =  PAGE_SPARE_SIZE;
606         mtd->erasesize = PAGE_DATA_SIZE * PAGES_PER_BLOCK;
607         mtd->size = sizeInMB * 1024*1024;
608
609
610
611         /* Fill in remaining MTD driver data */
612         mtd->type = MTD_NANDFLASH;
613         mtd->flags = MTD_CAP_NANDFLASH;
614         mtd->owner = THIS_MODULE;
615         mtd->ecctype = MTD_ECC_NONE;
616         mtd->erase = nand_erase;
617         mtd->point = NULL;
618         mtd->unpoint = NULL;
619         mtd->read = nand_read;
620         mtd->write = nand_write;
621         mtd->read_ecc = nand_read_ecc;
622         mtd->write_ecc = nand_write_ecc;
623         mtd->read_oob = nand_read_oob;
624         mtd->write_oob = nand_write_oob;
625         mtd->block_isbad = nand_block_isbad;
626         mtd->block_markbad = nand_block_markbad;
627         mtd->readv = NULL;
628         mtd->writev = nand_writev;
629         mtd->sync = nand_sync;
630         mtd->lock = NULL;
631         mtd->unlock = NULL;
632         mtd->suspend = NULL;
633         mtd->resume = NULL;
634
635         mtd->name = "NANDemul2k";
636
637         /* Return happy */
638         return 0;
639 }
640
641 #if 0
642 #ifdef MODULE
643 MODULE_PARM(sizeInMB, "i");
644
645 __setup("sizeInMB=",sizeInMB);
646 #endif
647 #endif
648
649 /*
650  * Define partitions for flash devices
651  */
652
653 static struct mtd_partition nandemul2k_partition[] =
654 {
655         { .name         = "NANDemul partition 1",
656           .offset       = 0,
657           .size         = 0 },
658 };
659
660 static int nPartitions = sizeof(nandemul2k_partition)/sizeof(nandemul2k_partition[0]);
661
662 /*
663  * Main initialization routine
664  */
665 int __init nandemul2k_init (void)
666 {
667
668         // Do the nand init
669         
670         CheckInit();
671
672         nandemul2k_scan(&nandemul2k_mtd,1);
673
674         // Build the partition table
675
676         nandemul2k_partition[0].size = sizeInMB * 1024 * 1024;
677
678         // Register the partition
679         add_mtd_partitions(&nandemul2k_mtd,nandemul2k_partition,nPartitions);
680
681         return 0;
682
683 }
684
685 module_init(nandemul2k_init);
686
687 /*
688  * Clean up routine
689  */
690 #ifdef MODULE
691 static void __exit nandemul2k_cleanup (void)
692 {
693
694         nandemul2k_CleanUp();
695
696         /* Unregister partitions */
697         del_mtd_partitions(&nandemul2k_mtd);
698
699         /* Unregister the device */
700         del_mtd_device (&nandemul2k_mtd);
701
702 }
703 module_exit(nandemul2k_cleanup);
704 #endif
705
706 MODULE_LICENSE("GPL");
707 MODULE_AUTHOR("Charles Manning <manningc@aleph1.co.uk>");
708 MODULE_DESCRIPTION("2k Page/128k Block NAND emulated in RAM");
709
710
711
712