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