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