OneNAND: Superload command support
[kernel/u-boot.git] / drivers / mtd / onenand / onenand_base.c
1 /*
2  *  linux/drivers/mtd/onenand/onenand_base.c
3  *
4  *  Copyright (C) 2005-2007 Samsung Electronics
5  *  Kyungmin Park <kyungmin.park@samsung.com>
6  *
7  *  Credits:
8  *      Adrian Hunter <ext-adrian.hunter@nokia.com>:
9  *      auto-placement support, read-while load support, various fixes
10  *      Copyright (C) Nokia Corporation, 2007
11  *
12  *      Rohit Hagargundgi <h.rohit at samsung.com>,
13  *      Amul Kumar Saha <amul.saha@samsung.com>:
14  *      Flex-OneNAND support
15  *      Copyright (C) Samsung Electronics, 2009
16  *
17  * This program is free software; you can redistribute it and/or modify
18  * it under the terms of the GNU General Public License version 2 as
19  * published by the Free Software Foundation.
20  */
21
22 #include <common.h>
23 #include <linux/mtd/compat.h>
24 #include <linux/mtd/mtd.h>
25 #include <linux/mtd/onenand.h>
26
27 #include <asm/io.h>
28 #include <asm/errno.h>
29 #include <malloc.h>
30
31 extern void *memcpy32(void *dst, const void *src, int len);
32
33 /* It should access 16-bit instead of 8-bit */
34 static void *memcpy_16(void *dst, const void *src, unsigned int len)
35 {
36         void *ret = dst;
37         short *d = dst;
38         const short *s = src;
39
40         if (len >= 32 && (len & (32 - 1)) == 0)
41                 return memcpy32(dst, src, len);
42
43         len >>= 1;
44         while (len-- > 0)
45                 *d++ = *s++;
46         return ret;
47 }
48
49 /**
50  *  onenand_oob_128 - oob info for Flex-Onenand with 4KB page
51  *  For now, we expose only 64 out of 80 ecc bytes
52  */
53 static struct nand_ecclayout onenand_oob_128 = {
54         .eccbytes       = 64,
55         .eccpos         = {
56                 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
57                 22, 23, 24, 25, 26, 27, 28, 29, 30, 31,
58                 38, 39, 40, 41, 42, 43, 44, 45, 46, 47,
59                 54, 55, 56, 57, 58, 59, 60, 61, 62, 63,
60                 70, 71, 72, 73, 74, 75, 76, 77, 78, 79,
61                 86, 87, 88, 89, 90, 91, 92, 93, 94, 95,
62                 102, 103, 104, 105
63         },
64         .oobfree        = {
65                 {2, 4}, {18, 4}, {34, 4}, {50, 4},
66                 {66, 4}, {82, 4}, {98, 4}, {114, 4}
67         }
68 };
69
70 /**
71  * onenand_oob_64 - oob info for large (2KB) page
72  */
73 static struct nand_ecclayout onenand_oob_64 = {
74         .eccbytes       = 20,
75         .eccpos         = {
76                 8, 9, 10, 11, 12,
77                 24, 25, 26, 27, 28,
78                 40, 41, 42, 43, 44,
79                 56, 57, 58, 59, 60,
80                 },
81         .oobfree        = {
82                 {2, 3}, {14, 2}, {18, 3}, {30, 2},
83                 {34, 3}, {46, 2}, {50, 3}, {62, 2}
84         }
85 };
86
87 /**
88  * onenand_oob_32 - oob info for middle (1KB) page
89  */
90 static struct nand_ecclayout onenand_oob_32 = {
91         .eccbytes       = 10,
92         .eccpos         = {
93                 8, 9, 10, 11, 12,
94                 24, 25, 26, 27, 28,
95                 },
96         .oobfree        = { {2, 3}, {14, 2}, {18, 3}, {30, 2} }
97 };
98
99 static const unsigned char ffchars[] __attribute__((aligned(4))) = {
100         0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
101         0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 16 */
102         0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
103         0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 32 */
104         0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
105         0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 48 */
106         0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
107         0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 64 */
108         0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
109         0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 80 */
110         0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
111         0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 96 */
112         0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
113         0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 112 */
114         0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
115         0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 128 */
116 };
117
118 /**
119  * onenand_readw - [OneNAND Interface] Read OneNAND register
120  * @param addr          address to read
121  *
122  * Read OneNAND register
123  */
124 static unsigned short onenand_readw(void __iomem * addr)
125 {
126         return readw(addr);
127 }
128
129 /**
130  * onenand_writew - [OneNAND Interface] Write OneNAND register with value
131  * @param value         value to write
132  * @param addr          address to write
133  *
134  * Write OneNAND register with value
135  */
136 static void onenand_writew(unsigned short value, void __iomem * addr)
137 {
138         writew(value, addr);
139 }
140
141 /**
142  * onenand_block_address - [DEFAULT] Get block address
143  * @param device        the device id
144  * @param block         the block
145  * @return              translated block address if DDP, otherwise same
146  *
147  * Setup Start Address 1 Register (F100h)
148  */
149 static int onenand_block_address(struct onenand_chip *this, int block)
150 {
151         /* Device Flash Core select, NAND Flash Block Address */
152         if (block & this->density_mask)
153                 return ONENAND_DDP_CHIP1 | (block ^ this->density_mask);
154
155         return block;
156 }
157
158 /**
159  * onenand_bufferram_address - [DEFAULT] Get bufferram address
160  * @param device        the device id
161  * @param block         the block
162  * @return              set DBS value if DDP, otherwise 0
163  *
164  * Setup Start Address 2 Register (F101h) for DDP
165  */
166 static int onenand_bufferram_address(struct onenand_chip *this, int block)
167 {
168         /* Device BufferRAM Select */
169         if (block & this->density_mask)
170                 return ONENAND_DDP_CHIP1;
171
172         return ONENAND_DDP_CHIP0;
173 }
174
175 /**
176  * onenand_page_address - [DEFAULT] Get page address
177  * @param page          the page address
178  * @param sector        the sector address
179  * @return              combined page and sector address
180  *
181  * Setup Start Address 8 Register (F107h)
182  */
183 static int onenand_page_address(int page, int sector)
184 {
185         /* Flash Page Address, Flash Sector Address */
186         int fpa, fsa;
187
188         fpa = page & ONENAND_FPA_MASK;
189         fsa = sector & ONENAND_FSA_MASK;
190
191         return ((fpa << ONENAND_FPA_SHIFT) | fsa);
192 }
193
194 /**
195  * onenand_buffer_address - [DEFAULT] Get buffer address
196  * @param dataram1      DataRAM index
197  * @param sectors       the sector address
198  * @param count         the number of sectors
199  * @return              the start buffer value
200  *
201  * Setup Start Buffer Register (F200h)
202  */
203 static int onenand_buffer_address(int dataram1, int sectors, int count)
204 {
205         int bsa, bsc;
206
207         /* BufferRAM Sector Address */
208         bsa = sectors & ONENAND_BSA_MASK;
209
210         if (dataram1)
211                 bsa |= ONENAND_BSA_DATARAM1;    /* DataRAM1 */
212         else
213                 bsa |= ONENAND_BSA_DATARAM0;    /* DataRAM0 */
214
215         /* BufferRAM Sector Count */
216         bsc = count & ONENAND_BSC_MASK;
217
218         return ((bsa << ONENAND_BSA_SHIFT) | bsc);
219 }
220
221 /**
222  * flexonenand_block - Return block number for flash address
223  * @param this          - OneNAND device structure
224  * @param addr          - Address for which block number is needed
225  */
226 static unsigned int flexonenand_block(struct onenand_chip *this, loff_t addr)
227 {
228         unsigned int boundary, blk, die = 0;
229
230         if (ONENAND_IS_DDP(this) && addr >= this->diesize[0]) {
231                 die = 1;
232                 addr -= this->diesize[0];
233         }
234
235         boundary = this->boundary[die];
236
237         blk = addr >> (this->erase_shift - 1);
238         if (blk > boundary)
239                 blk = (blk + boundary + 1) >> 1;
240
241         blk += die ? this->density_mask : 0;
242         return blk;
243 }
244
245 unsigned int onenand_block(struct onenand_chip *this, loff_t addr)
246 {
247         if (!FLEXONENAND(this))
248                 return addr >> this->erase_shift;
249         return flexonenand_block(this, addr);
250 }
251
252 /**
253  * flexonenand_addr - Return address of the block
254  * @this:               OneNAND device structure
255  * @block:              Block number on Flex-OneNAND
256  *
257  * Return address of the block
258  */
259 static loff_t flexonenand_addr(struct onenand_chip *this, int block)
260 {
261         loff_t ofs = 0;
262         int die = 0, boundary;
263
264         if (ONENAND_IS_DDP(this) && block >= this->density_mask) {
265                 block -= this->density_mask;
266                 die = 1;
267                 ofs = this->diesize[0];
268         }
269
270         boundary = this->boundary[die];
271         ofs += (loff_t) block << (this->erase_shift - 1);
272         if (block > (boundary + 1))
273                 ofs += (loff_t) (block - boundary - 1)
274                         << (this->erase_shift - 1);
275         return ofs;
276 }
277
278 loff_t onenand_addr(struct onenand_chip *this, int block)
279 {
280         if (!FLEXONENAND(this))
281                 return (loff_t) block << this->erase_shift;
282         return flexonenand_addr(this, block);
283 }
284
285 /**
286  * flexonenand_region - [Flex-OneNAND] Return erase region of addr
287  * @param mtd           MTD device structure
288  * @param addr          address whose erase region needs to be identified
289  */
290 int flexonenand_region(struct mtd_info *mtd, loff_t addr)
291 {
292         int i;
293
294         for (i = 0; i < mtd->numeraseregions; i++)
295                 if (addr < mtd->eraseregions[i].offset)
296                         break;
297         return i - 1;
298 }
299
300 /**
301  * onenand_get_density - [DEFAULT] Get OneNAND density
302  * @param dev_id        OneNAND device ID
303  *
304  * Get OneNAND density from device ID
305  */
306 static inline int onenand_get_density(int dev_id)
307 {
308         int density = dev_id >> ONENAND_DEVICE_DENSITY_SHIFT;
309         return (density & ONENAND_DEVICE_DENSITY_MASK);
310 }
311
312 /**
313  * onenand_command - [DEFAULT] Send command to OneNAND device
314  * @param mtd           MTD device structure
315  * @param cmd           the command to be sent
316  * @param addr          offset to read from or write to
317  * @param len           number of bytes to read or write
318  *
319  * Send command to OneNAND device. This function is used for middle/large page
320  * devices (1KB/2KB Bytes per page)
321  */
322 static int onenand_command(struct mtd_info *mtd, int cmd, loff_t addr,
323                            size_t len)
324 {
325         struct onenand_chip *this = mtd->priv;
326         int value, block, page;
327
328         /* Address translation */
329         switch (cmd) {
330         case ONENAND_CMD_UNLOCK:
331         case ONENAND_CMD_LOCK:
332         case ONENAND_CMD_LOCK_TIGHT:
333         case ONENAND_CMD_UNLOCK_ALL:
334                 block = -1;
335                 page = -1;
336                 break;
337
338         case FLEXONENAND_CMD_PI_ACCESS:
339                 /* addr contains die index */
340                 block = addr * this->density_mask;
341                 page = -1;
342                 break;
343
344         case ONENAND_CMD_ERASE:
345         case ONENAND_CMD_BUFFERRAM:
346                 block = onenand_block(this, addr);
347                 page = -1;
348                 break;
349
350         case FLEXONENAND_CMD_READ_PI:
351                 cmd = ONENAND_CMD_READ;
352                 block = addr * this->density_mask;
353                 page = 0;
354                 break;
355
356         default:
357                 block = onenand_block(this, addr);
358                 page = (int) (addr
359                         - onenand_addr(this, block)) >> this->page_shift;
360                 page &= this->page_mask;
361                 break;
362         }
363
364         /* NOTE: The setting order of the registers is very important! */
365         if (cmd == ONENAND_CMD_BUFFERRAM) {
366                 /* Select DataRAM for DDP */
367                 value = onenand_bufferram_address(this, block);
368                 this->write_word(value,
369                                  this->base + ONENAND_REG_START_ADDRESS2);
370
371                 if (ONENAND_IS_4KB_PAGE(this))
372                         /* It is always BufferRAM0 */
373                         ONENAND_SET_BUFFERRAM0(this);
374                 else
375                         /* Switch to the next data buffer */
376                         ONENAND_SET_NEXT_BUFFERRAM(this);
377
378                 return 0;
379         }
380
381         if (block != -1) {
382                 /* Write 'DFS, FBA' of Flash */
383                 value = onenand_block_address(this, block);
384                 this->write_word(value,
385                                  this->base + ONENAND_REG_START_ADDRESS1);
386
387                 /* Select DataRAM for DDP */
388                 value = onenand_bufferram_address(this, block);
389                 this->write_word(value,
390                                  this->base + ONENAND_REG_START_ADDRESS2);
391         }
392
393         if (page != -1) {
394                 /* Now we use page size operation */
395                 int sectors = 0, count = 0;
396                 int dataram;
397
398                 switch (cmd) {
399                 case FLEXONENAND_CMD_RECOVER_LSB:
400                 case ONENAND_CMD_READ:
401                 case ONENAND_CMD_READOOB:
402                 case ONENAND_CMD_SUPERLOAD:
403                         if (ONENAND_IS_4KB_PAGE(this))
404                                 /* It is always BufferRAM0 */
405                                 dataram = ONENAND_SET_BUFFERRAM0(this);
406                         else
407                                 dataram = ONENAND_SET_NEXT_BUFFERRAM(this);
408                         break;
409
410                 default:
411                         dataram = ONENAND_CURRENT_BUFFERRAM(this);
412                         break;
413                 }
414
415                 /* Write 'FPA, FSA' of Flash */
416                 value = onenand_page_address(page, sectors);
417                 this->write_word(value,
418                                  this->base + ONENAND_REG_START_ADDRESS8);
419
420                 /* Write 'BSA, BSC' of DataRAM */
421                 value = onenand_buffer_address(dataram, sectors, count);
422                 this->write_word(value, this->base + ONENAND_REG_START_BUFFER);
423         }
424
425         /* Interrupt clear */
426         this->write_word(ONENAND_INT_CLEAR, this->base + ONENAND_REG_INTERRUPT);
427         /* Write command */
428         this->write_word(cmd, this->base + ONENAND_REG_COMMAND);
429
430         return 0;
431 }
432
433 /**
434  * onenand_read_ecc - return ecc status
435  * @param this          onenand chip structure
436  */
437 static int onenand_read_ecc(struct onenand_chip *this)
438 {
439         int ecc, i;
440
441         if (!ONENAND_IS_4KB_PAGE(this))
442                 return this->read_word(this->base + ONENAND_REG_ECC_STATUS);
443
444         for (i = 0; i < 4; i++) {
445                 ecc = this->read_word(this->base
446                                 + ((ONENAND_REG_ECC_STATUS + i) << 1));
447                 if (likely(!ecc))
448                         continue;
449                 if (ecc & FLEXONENAND_UNCORRECTABLE_ERROR)
450                         return ONENAND_ECC_2BIT_ALL;
451         }
452
453         return 0;
454 }
455
456 /**
457  * onenand_wait - [DEFAULT] wait until the command is done
458  * @param mtd           MTD device structure
459  * @param state         state to select the max. timeout value
460  *
461  * Wait for command done. This applies to all OneNAND command
462  * Read can take up to 30us, erase up to 2ms and program up to 350us
463  * according to general OneNAND specs
464  */
465 static int onenand_wait(struct mtd_info *mtd, int state)
466 {
467         struct onenand_chip *this = mtd->priv;
468         unsigned int flags = ONENAND_INT_MASTER;
469         unsigned int interrupt = 0;
470         unsigned int ctrl;
471
472         while (1) {
473                 interrupt = this->read_word(this->base + ONENAND_REG_INTERRUPT);
474                 if (interrupt & flags)
475                         break;
476         }
477
478         ctrl = this->read_word(this->base + ONENAND_REG_CTRL_STATUS);
479
480         if (interrupt & ONENAND_INT_READ) {
481                 int ecc = onenand_read_ecc(this);
482                 if (ecc & ONENAND_ECC_2BIT_ALL) {
483                         printk("onenand_wait: ECC error = 0x%04x\n", ecc);
484                         return -EBADMSG;
485                 }
486         }
487
488         if (ctrl & ONENAND_CTRL_ERROR) {
489                 printk("onenand_wait: controller error = 0x%04x\n", ctrl);
490                 if (ctrl & ONENAND_CTRL_LOCK)
491                         printk("onenand_wait: it's locked error = 0x%04x\n",
492                                 ctrl);
493
494                 return -EIO;
495         }
496
497         return 0;
498 }
499
500 /**
501  * onenand_bufferram_offset - [DEFAULT] BufferRAM offset
502  * @param mtd           MTD data structure
503  * @param area          BufferRAM area
504  * @return              offset given area
505  *
506  * Return BufferRAM offset given area
507  */
508 static inline int onenand_bufferram_offset(struct mtd_info *mtd, int area)
509 {
510         struct onenand_chip *this = mtd->priv;
511
512         if (ONENAND_CURRENT_BUFFERRAM(this)) {
513                 if (area == ONENAND_DATARAM)
514                         return mtd->writesize;
515                 if (area == ONENAND_SPARERAM)
516                         return mtd->oobsize;
517         }
518
519         return 0;
520 }
521
522 /**
523  * onenand_read_bufferram - [OneNAND Interface] Read the bufferram area
524  * @param mtd           MTD data structure
525  * @param area          BufferRAM area
526  * @param buffer        the databuffer to put/get data
527  * @param offset        offset to read from or write to
528  * @param count         number of bytes to read/write
529  *
530  * Read the BufferRAM area
531  */
532 static int onenand_read_bufferram(struct mtd_info *mtd, loff_t addr, int area,
533                                   unsigned char *buffer, int offset,
534                                   size_t count)
535 {
536         struct onenand_chip *this = mtd->priv;
537         void __iomem *bufferram;
538
539         bufferram = this->base + area;
540         bufferram += onenand_bufferram_offset(mtd, area);
541
542         memcpy_16(buffer, bufferram + offset, count);
543
544         return 0;
545 }
546
547 /**
548  * onenand_write_bufferram - [OneNAND Interface] Write the bufferram area
549  * @param mtd           MTD data structure
550  * @param area          BufferRAM area
551  * @param buffer        the databuffer to put/get data
552  * @param offset        offset to read from or write to
553  * @param count         number of bytes to read/write
554  *
555  * Write the BufferRAM area
556  */
557 static int onenand_write_bufferram(struct mtd_info *mtd, loff_t addr, int area,
558                                    const unsigned char *buffer, int offset,
559                                    size_t count)
560 {
561         struct onenand_chip *this = mtd->priv;
562         void __iomem *bufferram;
563
564         bufferram = this->base + area;
565         bufferram += onenand_bufferram_offset(mtd, area);
566
567         memcpy_16(bufferram + offset, buffer, count);
568
569         return 0;
570 }
571
572 /**
573  * onenand_get_2x_blockpage - [GENERIC] Get blockpage at 2x program mode
574  * @param mtd           MTD data structure
575  * @param addr          address to check
576  * @return              blockpage address
577  *
578  * Get blockpage address at 2x program mode
579  */
580 static int onenand_get_2x_blockpage(struct mtd_info *mtd, loff_t addr)
581 {
582         struct onenand_chip *this = mtd->priv;
583         int blockpage, block, page;
584
585         /* Calculate the even block number */
586         block = (int) (addr >> this->erase_shift) & ~1;
587         /* Is it the odd plane? */
588         if (addr & this->writesize)
589                 block++;
590         page = (int) (addr >> (this->page_shift + 1)) & this->page_mask;
591         blockpage = (block << 7) | page;
592
593         return blockpage;
594 }
595
596 /**
597  * onenand_check_bufferram - [GENERIC] Check BufferRAM information
598  * @param mtd           MTD data structure
599  * @param addr          address to check
600  * @return              1 if there are valid data, otherwise 0
601  *
602  * Check bufferram if there is data we required
603  */
604 static int onenand_check_bufferram(struct mtd_info *mtd, loff_t addr)
605 {
606         struct onenand_chip *this = mtd->priv;
607         int blockpage, found = 0;
608         unsigned int i;
609
610         if (ONENAND_IS_2PLANE(this))
611                 blockpage = onenand_get_2x_blockpage(mtd, addr);
612         else
613                 blockpage = (int) (addr >> this->page_shift);
614
615         /* Is there valid data? */
616         i = ONENAND_CURRENT_BUFFERRAM(this);
617         if (this->bufferram[i].blockpage == blockpage)
618                 found = 1;
619         else {
620                 /* Check another BufferRAM */
621                 i = ONENAND_NEXT_BUFFERRAM(this);
622                 if (this->bufferram[i].blockpage == blockpage) {
623                         ONENAND_SET_NEXT_BUFFERRAM(this);
624                         found = 1;
625                 }
626         }
627
628         if (found && ONENAND_IS_DDP(this)) {
629                 /* Select DataRAM for DDP */
630                 int block = onenand_block(this, addr);
631                 int value = onenand_bufferram_address(this, block);
632                 this->write_word(value, this->base + ONENAND_REG_START_ADDRESS2);
633         }
634
635         return found;
636 }
637
638 /**
639  * onenand_update_bufferram - [GENERIC] Update BufferRAM information
640  * @param mtd           MTD data structure
641  * @param addr          address to update
642  * @param valid         valid flag
643  *
644  * Update BufferRAM information
645  */
646 static int onenand_update_bufferram(struct mtd_info *mtd, loff_t addr,
647                                     int valid)
648 {
649         struct onenand_chip *this = mtd->priv;
650         int blockpage;
651         unsigned int i;
652
653         if (ONENAND_IS_2PLANE(this))
654                 blockpage = onenand_get_2x_blockpage(mtd, addr);
655         else
656                 blockpage = (int)(addr >> this->page_shift);
657
658         /* Invalidate another BufferRAM */
659         i = ONENAND_NEXT_BUFFERRAM(this);
660         if (this->bufferram[i].blockpage == blockpage)
661                 this->bufferram[i].blockpage = -1;
662
663         /* Update BufferRAM */
664         i = ONENAND_CURRENT_BUFFERRAM(this);
665         if (valid)
666                 this->bufferram[i].blockpage = blockpage;
667         else
668                 this->bufferram[i].blockpage = -1;
669
670         return 0;
671 }
672
673 /**
674  * onenand_invalidate_bufferram - [GENERIC] Invalidate BufferRAM information
675  * @param mtd           MTD data structure
676  * @param addr          start address to invalidate
677  * @param len           length to invalidate
678  *
679  * Invalidate BufferRAM information
680  */
681 static void onenand_invalidate_bufferram(struct mtd_info *mtd, loff_t addr,
682                                          unsigned int len)
683 {
684         struct onenand_chip *this = mtd->priv;
685         int i;
686         loff_t end_addr = addr + len;
687
688         /* Invalidate BufferRAM */
689         for (i = 0; i < MAX_BUFFERRAM; i++) {
690                 loff_t buf_addr = this->bufferram[i].blockpage << this->page_shift;
691
692                 if (buf_addr >= addr && buf_addr < end_addr)
693                         this->bufferram[i].blockpage = -1;
694         }
695 }
696
697 /**
698  * onenand_get_device - [GENERIC] Get chip for selected access
699  * @param mtd           MTD device structure
700  * @param new_state     the state which is requested
701  *
702  * Get the device and lock it for exclusive access
703  */
704 static void onenand_get_device(struct mtd_info *mtd, int new_state)
705 {
706         /* Do nothing */
707 }
708
709 /**
710  * onenand_release_device - [GENERIC] release chip
711  * @param mtd           MTD device structure
712  *
713  * Deselect, release chip lock and wake up anyone waiting on the device
714  */
715 static void onenand_release_device(struct mtd_info *mtd)
716 {
717         /* Do nothing */
718 }
719
720 /**
721  * onenand_transfer_auto_oob - [Internal] oob auto-placement transfer
722  * @param mtd           MTD device structure
723  * @param buf           destination address
724  * @param column        oob offset to read from
725  * @param thislen       oob length to read
726  */
727 static int onenand_transfer_auto_oob(struct mtd_info *mtd, uint8_t *buf,
728                                         int column, int thislen)
729 {
730         struct onenand_chip *this = mtd->priv;
731         struct nand_oobfree *free;
732         int readcol = column;
733         int readend = column + thislen;
734         int lastgap = 0;
735         unsigned int i;
736         uint8_t *oob_buf = this->oob_buf;
737
738         free = this->ecclayout->oobfree;
739         for (i = 0; i < MTD_MAX_OOBFREE_ENTRIES && free->length; i++, free++) {
740                 if (readcol >= lastgap)
741                         readcol += free->offset - lastgap;
742                 if (readend >= lastgap)
743                         readend += free->offset - lastgap;
744                 lastgap = free->offset + free->length;
745         }
746         this->read_bufferram(mtd, 0, ONENAND_SPARERAM, oob_buf, 0, mtd->oobsize);
747         free = this->ecclayout->oobfree;
748         for (i = 0; i < MTD_MAX_OOBFREE_ENTRIES && free->length; i++, free++) {
749                 int free_end = free->offset + free->length;
750                 if (free->offset < readend && free_end > readcol) {
751                         int st = max_t(int,free->offset,readcol);
752                         int ed = min_t(int,free_end,readend);
753                         int n = ed - st;
754                         memcpy(buf, oob_buf + st, n);
755                         buf += n;
756                 } else if (column == 0)
757                         break;
758         }
759         return 0;
760 }
761
762 /**
763  * onenand_recover_lsb - [Flex-OneNAND] Recover LSB page data
764  * @param mtd           MTD device structure
765  * @param addr          address to recover
766  * @param status        return value from onenand_wait
767  *
768  * MLC NAND Flash cell has paired pages - LSB page and MSB page. LSB page has
769  * lower page address and MSB page has higher page address in paired pages.
770  * If power off occurs during MSB page program, the paired LSB page data can
771  * become corrupt. LSB page recovery read is a way to read LSB page though page
772  * data are corrupted. When uncorrectable error occurs as a result of LSB page
773  * read after power up, issue LSB page recovery read.
774  */
775 static int onenand_recover_lsb(struct mtd_info *mtd, loff_t addr, int status)
776 {
777         struct onenand_chip *this = mtd->priv;
778         int i;
779
780         /* Recovery is only for Flex-OneNAND */
781         if (!FLEXONENAND(this))
782                 return status;
783
784         /* check if we failed due to uncorrectable error */
785         if (status != -EBADMSG && status != ONENAND_BBT_READ_ECC_ERROR)
786                 return status;
787
788         /* check if address lies in MLC region */
789         i = flexonenand_region(mtd, addr);
790         if (mtd->eraseregions[i].erasesize < (1 << this->erase_shift))
791                 return status;
792
793         printk("onenand_recover_lsb:"
794                 "Attempting to recover from uncorrectable read\n");
795
796         /* Issue the LSB page recovery command */
797         this->command(mtd, FLEXONENAND_CMD_RECOVER_LSB, addr, this->writesize);
798         return this->wait(mtd, FL_READING);
799 }
800
801 /**
802  * onenand_read_ops_nolock - [OneNAND Interface] OneNAND read main and/or out-of-band
803  * @param mtd           MTD device structure
804  * @param from          offset to read from
805  * @param ops           oob operation description structure
806  *
807  * OneNAND read main and/or out-of-band data
808  */
809 static int onenand_normal_read_ops_nolock(struct mtd_info *mtd, loff_t from,
810                 struct mtd_oob_ops *ops)
811 {
812         struct onenand_chip *this = mtd->priv;
813         struct mtd_ecc_stats stats;
814         size_t len = ops->len;
815         size_t ooblen = ops->ooblen;
816         u_char *buf = ops->datbuf;
817         u_char *oobbuf = ops->oobbuf;
818         int read = 0, column, thislen;
819         int oobread = 0, oobcolumn, thisooblen, oobsize;
820         int ret = 0, boundary = 0;
821         int writesize = this->writesize;
822
823         MTDDEBUG(MTD_DEBUG_LEVEL3, "onenand_read_ops_nolock: from = 0x%08x, len = %i\n", (unsigned int) from, (int) len);
824
825         if (ops->mode == MTD_OOB_AUTO)
826                 oobsize = this->ecclayout->oobavail;
827         else
828                 oobsize = mtd->oobsize;
829
830         oobcolumn = from & (mtd->oobsize - 1);
831
832         /* Do not allow reads past end of device */
833         if ((from + len) > mtd->size) {
834                 printk(KERN_ERR "onenand_read_ops_nolock: Attempt read beyond end of device\n");
835                 ops->retlen = 0;
836                 ops->oobretlen = 0;
837                 return -EINVAL;
838         }
839
840         stats = mtd->ecc_stats;
841
842         /* Read-while-load method */
843         /* Note: We can't use this feature in MLC */
844
845         /* Do first load to bufferRAM */
846         if (read < len) {
847                 if (!onenand_check_bufferram(mtd, from)) {
848                         this->command(mtd, ONENAND_CMD_READ, from, writesize);
849                         ret = this->wait(mtd, FL_READING);
850                         if (unlikely(ret))
851                                 ret = onenand_recover_lsb(mtd, from, ret);
852                         onenand_update_bufferram(mtd, from, !ret);
853                         if (ret == -EBADMSG)
854                                 ret = 0;
855                 }
856         }
857
858         thislen = min_t(int, writesize, len - read);
859         column = from & (writesize - 1);
860         if (column + thislen > writesize)
861                 thislen = writesize - column;
862
863         while (!ret) {
864                 /* If there is more to load then start next load */
865                 from += thislen;
866
867                 if (ONENAND_IS_4KB_PAGE(this))
868                         goto skip_read_while_load;
869
870                 if (read + thislen < len) {
871                         this->command(mtd, ONENAND_CMD_READ, from, writesize);
872                         /*
873                          * Chip boundary handling in DDP
874                          * Now we issued chip 1 read and pointed chip 1
875                          * bufferam so we have to point chip 0 bufferam.
876                          */
877                         if (ONENAND_IS_DDP(this) &&
878                                         unlikely(from == (this->chipsize >> 1))) {
879                                 this->write_word(ONENAND_DDP_CHIP0, this->base + ONENAND_REG_START_ADDRESS2);
880                                 boundary = 1;
881                         } else
882                                 boundary = 0;
883                         ONENAND_SET_PREV_BUFFERRAM(this);
884                 }
885 skip_read_while_load:
886                 /* While load is going, read from last bufferRAM */
887                 this->read_bufferram(mtd, from - thislen, ONENAND_DATARAM, buf, column, thislen);
888
889                 /* Read oob area if needed */
890                 if (oobbuf) {
891                         thisooblen = oobsize - oobcolumn;
892                         thisooblen = min_t(int, thisooblen, ooblen - oobread);
893
894                         if (ops->mode == MTD_OOB_AUTO)
895                                 onenand_transfer_auto_oob(mtd, oobbuf, oobcolumn, thisooblen);
896                         else
897                                 this->read_bufferram(mtd, 0, ONENAND_SPARERAM, oobbuf, oobcolumn, thisooblen);
898                         oobread += thisooblen;
899                         oobbuf += thisooblen;
900                         oobcolumn = 0;
901                 }
902
903                 /* See if we are done */
904                 read += thislen;
905                 if (read == len)
906                         break;
907                 /* Set up for next read from bufferRAM */
908                 if (unlikely(boundary))
909                         this->write_word(ONENAND_DDP_CHIP1, this->base + ONENAND_REG_START_ADDRESS2);
910                 if (ONENAND_IS_4KB_PAGE(this))
911                         this->command(mtd, ONENAND_CMD_READ, from, writesize);
912                 else
913                         ONENAND_SET_NEXT_BUFFERRAM(this);
914                 buf += thislen;
915                 thislen = min_t(int, writesize, len - read);
916                 column = 0;
917
918                 if (ONENAND_IS_4KB_PAGE(this)) {
919                         /* Now wait for load */
920                         ret = this->wait(mtd, FL_READING);
921                         onenand_update_bufferram(mtd, from, !ret);
922                         if (ret == -EBADMSG)
923                                 ret = 0;
924                 }
925         }
926
927         /*
928          * Return success, if no ECC failures, else -EBADMSG
929          * fs driver will take care of that, because
930          * retlen == desired len and result == -EBADMSG
931          */
932         ops->retlen = read;
933         ops->oobretlen = oobread;
934
935         if (ret)
936                 return ret;
937
938         if (mtd->ecc_stats.failed - stats.failed)
939                 return -EBADMSG;
940
941         return mtd->ecc_stats.corrected - stats.corrected ? -EUCLEAN : 0;
942 }
943
944 static int onenand_superload_read_ops_nolock(struct mtd_info *mtd, loff_t from,
945                 struct mtd_oob_ops *ops)
946 {
947         struct onenand_chip *this = mtd->priv;
948         struct mtd_ecc_stats stats;
949         size_t len = ops->len;
950         size_t ooblen = ops->ooblen;
951         u_char *buf = ops->datbuf;
952         u_char *oobbuf = ops->oobbuf;
953         int read = 0, column, thislen;
954         int oobread = 0, oobcolumn, thisooblen, oobsize;
955         int ret = 0, boundary = 0;
956         int writesize = this->writesize;
957
958         MTDDEBUG(MTD_DEBUG_LEVEL3, "onenand_read_ops_nolock: from = 0x%08x, len = %i\n", (unsigned int) from, (int) len);
959
960         if (ops->mode == MTD_OOB_AUTO)
961                 oobsize = this->ecclayout->oobavail;
962         else
963                 oobsize = mtd->oobsize;
964
965         oobcolumn = from & (mtd->oobsize - 1);
966
967         /* Do not allow reads past end of device */
968         if ((from + len) > mtd->size) {
969                 printk(KERN_ERR "onenand_read_ops_nolock: Attempt read beyond end of device\n");
970                 ops->retlen = 0;
971                 ops->oobretlen = 0;
972                 return -EINVAL;
973         }
974
975         stats = mtd->ecc_stats;
976
977         this->command(mtd, ONENAND_CMD_READ, from, writesize);
978         ret = this->wait(mtd, FL_READING);
979         if (unlikely(ret))
980                 ret = onenand_recover_lsb(mtd, from, ret);
981         onenand_update_bufferram(mtd, from, !ret);
982         if (ret == -EBADMSG)
983                 ret = 0;
984
985         thislen = min_t(int, writesize, len - read);
986         column = from & (writesize - 1);
987         if (column + thislen > writesize)
988                 thislen = writesize - column;
989
990         while (!ret) {
991                 /* If there is more to load then start next load */
992                 from += thislen;
993
994                 if (read + thislen < len) {
995                         this->command(mtd, ONENAND_CMD_SUPERLOAD, from, writesize);
996                         /*
997                          * Chip boundary handling in DDP
998                          * Now we issued chip 1 read and pointed chip 1
999                          * bufferam so we have to point chip 0 bufferam.
1000                          */
1001                         if (ONENAND_IS_DDP(this) &&
1002                                         unlikely(from == (this->chipsize >> 1))) {
1003                                 this->write_word(ONENAND_DDP_CHIP0, this->base + ONENAND_REG_START_ADDRESS2);
1004                                 boundary = 1;
1005                         } else
1006                                 boundary = 0;
1007                 }
1008                 /* While load is going, read from last bufferRAM */
1009                 this->read_bufferram(mtd, from - thislen, ONENAND_DATARAM, buf, column, thislen);
1010
1011                 /* Read oob area if needed */
1012                 if (oobbuf) {
1013                         thisooblen = oobsize - oobcolumn;
1014                         thisooblen = min_t(int, thisooblen, ooblen - oobread);
1015
1016                         if (ops->mode == MTD_OOB_AUTO)
1017                                 onenand_transfer_auto_oob(mtd, oobbuf, oobcolumn, thisooblen);
1018                         else
1019                                 this->read_bufferram(mtd, 0, ONENAND_SPARERAM, oobbuf, oobcolumn, thisooblen);
1020                         oobread += thisooblen;
1021                         oobbuf += thisooblen;
1022                         oobcolumn = 0;
1023                 } else {
1024                         /* It should be read at least 4 bytes
1025                          * to the end of the spare area
1026                          */
1027                         this->read_bufferram(mtd, 0, ONENAND_SPARERAM, this->oob_buf, 0, mtd->oobsize);
1028                 }
1029
1030                 /* See if we are done */
1031                 read += thislen;
1032                 if (read == len)
1033                         break;
1034                 /* Set up for next read from bufferRAM */
1035                 if (unlikely(boundary))
1036                         this->write_word(ONENAND_DDP_CHIP1, this->base + ONENAND_REG_START_ADDRESS2);
1037
1038                 buf += thislen;
1039                 thislen = min_t(int, writesize, len - read);
1040                 column = 0;
1041
1042                 /* Now wait for load */
1043                 ret = this->wait(mtd, FL_READING);
1044                 onenand_update_bufferram(mtd, from, !ret);
1045                 if (ret == -EBADMSG)
1046                         ret = 0;
1047         }
1048
1049         /*
1050          * Return success, if no ECC failures, else -EBADMSG
1051          * fs driver will take care of that, because
1052          * retlen == desired len and result == -EBADMSG
1053          */
1054         ops->retlen = read;
1055         ops->oobretlen = oobread;
1056
1057         if (ret)
1058                 return ret;
1059
1060         if (mtd->ecc_stats.failed - stats.failed)
1061                 return -EBADMSG;
1062
1063         return mtd->ecc_stats.corrected - stats.corrected ? -EUCLEAN : 0;
1064 }
1065
1066 /**
1067  * onenand_read_ops_nolock - [OneNAND Interface] OneNAND read main and/or out-of-band
1068  * @param mtd           MTD device structure
1069  * @param from          offset to read from
1070  * @param ops           oob operation description structure
1071  *
1072  * OneNAND read main and/or out-of-band data
1073  */
1074 static int onenand_read_ops_nolock(struct mtd_info *mtd, loff_t from,
1075                 struct mtd_oob_ops *ops)
1076 {
1077         struct onenand_chip *this = mtd->priv;
1078
1079         if (ONENAND_IS_4KB_PAGE(this) && ONENAND_IS_SYNC_MODE(this))
1080                 return onenand_superload_read_ops_nolock(mtd, from, ops);
1081
1082         return onenand_normal_read_ops_nolock(mtd, from, ops);
1083 }
1084
1085
1086 /**
1087  * onenand_read_oob_nolock - [MTD Interface] OneNAND read out-of-band
1088  * @param mtd           MTD device structure
1089  * @param from          offset to read from
1090  * @param ops           oob operation description structure
1091  *
1092  * OneNAND read out-of-band data from the spare area
1093  */
1094 static int onenand_read_oob_nolock(struct mtd_info *mtd, loff_t from,
1095                 struct mtd_oob_ops *ops)
1096 {
1097         struct onenand_chip *this = mtd->priv;
1098         struct mtd_ecc_stats stats;
1099         int read = 0, thislen, column, oobsize;
1100         size_t len = ops->ooblen;
1101         mtd_oob_mode_t mode = ops->mode;
1102         u_char *buf = ops->oobbuf;
1103         int ret = 0, readcmd;
1104
1105         from += ops->ooboffs;
1106
1107         MTDDEBUG(MTD_DEBUG_LEVEL3, "onenand_read_oob_nolock: from = 0x%08x, len = %i\n", (unsigned int) from, (int) len);
1108
1109         /* Initialize return length value */
1110         ops->oobretlen = 0;
1111
1112         if (mode == MTD_OOB_AUTO)
1113                 oobsize = this->ecclayout->oobavail;
1114         else
1115                 oobsize = mtd->oobsize;
1116
1117         column = from & (mtd->oobsize - 1);
1118
1119         if (unlikely(column >= oobsize)) {
1120                 printk(KERN_ERR "onenand_read_oob_nolock: Attempted to start read outside oob\n");
1121                 return -EINVAL;
1122         }
1123
1124         /* Do not allow reads past end of device */
1125         if (unlikely(from >= mtd->size ||
1126                 column + len > ((mtd->size >> this->page_shift) -
1127                                 (from >> this->page_shift)) * oobsize)) {
1128                 printk(KERN_ERR "onenand_read_oob_nolock: Attempted to read beyond end of device\n");
1129                 return -EINVAL;
1130         }
1131
1132         stats = mtd->ecc_stats;
1133
1134         readcmd = ONENAND_IS_MLC(this) ? ONENAND_CMD_READ : ONENAND_CMD_READOOB;
1135
1136         while (read < len) {
1137                 thislen = oobsize - column;
1138                 thislen = min_t(int, thislen, len);
1139
1140                 this->command(mtd, readcmd, from, mtd->oobsize);
1141
1142                 onenand_update_bufferram(mtd, from, 0);
1143
1144                 ret = this->wait(mtd, FL_READING);
1145                 if (unlikely(ret))
1146                         ret = onenand_recover_lsb(mtd, from, ret);
1147
1148                 if (ret && ret != -EBADMSG) {
1149                         printk(KERN_ERR "onenand_read_oob_nolock: read failed = 0x%x\n", ret);
1150                         break;
1151                 }
1152
1153                 if (mode == MTD_OOB_AUTO)
1154                         onenand_transfer_auto_oob(mtd, buf, column, thislen);
1155                 else
1156                         this->read_bufferram(mtd, 0, ONENAND_SPARERAM, buf, column, thislen);
1157
1158                 read += thislen;
1159
1160                 if (read == len)
1161                         break;
1162
1163                 buf += thislen;
1164
1165                 /* Read more? */
1166                 if (read < len) {
1167                         /* Page size */
1168                         from += mtd->writesize;
1169                         column = 0;
1170                 }
1171         }
1172
1173         ops->oobretlen = read;
1174
1175         if (ret)
1176                 return ret;
1177
1178         if (mtd->ecc_stats.failed - stats.failed)
1179                 return -EBADMSG;
1180
1181         return 0;
1182 }
1183
1184 /**
1185  * onenand_read - [MTD Interface] MTD compability function for onenand_read_ecc
1186  * @param mtd           MTD device structure
1187  * @param from          offset to read from
1188  * @param len           number of bytes to read
1189  * @param retlen        pointer to variable to store the number of read bytes
1190  * @param buf           the databuffer to put data
1191  *
1192  * This function simply calls onenand_read_ecc with oob buffer and oobsel = NULL
1193 */
1194 static int onenand_read(struct mtd_info *mtd, loff_t from, size_t len,
1195                  size_t * retlen, u_char * buf)
1196 {
1197         struct mtd_oob_ops ops = {
1198                 .len    = len,
1199                 .ooblen = 0,
1200                 .datbuf = buf,
1201                 .oobbuf = NULL,
1202         };
1203         int ret;
1204
1205         onenand_get_device(mtd, FL_READING);
1206         ret = onenand_read_ops_nolock(mtd, from, &ops);
1207         onenand_release_device(mtd);
1208
1209         *retlen = ops.retlen;
1210         return ret;
1211 }
1212
1213 /**
1214  * onenand_read_oob - [MTD Interface] OneNAND read out-of-band
1215  * @param mtd           MTD device structure
1216  * @param from          offset to read from
1217  * @param ops           oob operations description structure
1218  *
1219  * OneNAND main and/or out-of-band
1220  */
1221 static int onenand_read_oob(struct mtd_info *mtd, loff_t from,
1222                         struct mtd_oob_ops *ops)
1223 {
1224         int ret;
1225
1226         switch (ops->mode) {
1227         case MTD_OOB_PLACE:
1228         case MTD_OOB_AUTO:
1229                 break;
1230         case MTD_OOB_RAW:
1231                 /* Not implemented yet */
1232         default:
1233                 return -EINVAL;
1234         }
1235
1236         onenand_get_device(mtd, FL_READING);
1237         if (ops->datbuf)
1238                 ret = onenand_read_ops_nolock(mtd, from, ops);
1239         else
1240                 ret = onenand_read_oob_nolock(mtd, from, ops);
1241         onenand_release_device(mtd);
1242
1243         return ret;
1244 }
1245
1246 /**
1247  * onenand_bbt_wait - [DEFAULT] wait until the command is done
1248  * @param mtd           MTD device structure
1249  * @param state         state to select the max. timeout value
1250  *
1251  * Wait for command done.
1252  */
1253 static int onenand_bbt_wait(struct mtd_info *mtd, int state)
1254 {
1255         struct onenand_chip *this = mtd->priv;
1256         unsigned int flags = ONENAND_INT_MASTER;
1257         unsigned int interrupt;
1258         unsigned int ctrl;
1259
1260         while (1) {
1261                 interrupt = this->read_word(this->base + ONENAND_REG_INTERRUPT);
1262                 if (interrupt & flags)
1263                         break;
1264         }
1265
1266         /* To get correct interrupt status in timeout case */
1267         interrupt = this->read_word(this->base + ONENAND_REG_INTERRUPT);
1268         ctrl = this->read_word(this->base + ONENAND_REG_CTRL_STATUS);
1269
1270         if (interrupt & ONENAND_INT_READ) {
1271                 int ecc = onenand_read_ecc(this);
1272                 if (ecc & ONENAND_ECC_2BIT_ALL) {
1273                         printk(KERN_INFO "onenand_bbt_wait: ecc error = 0x%04x"
1274                                 ", controller = 0x%04x\n", ecc, ctrl);
1275                         return ONENAND_BBT_READ_ERROR;
1276                 }
1277         } else {
1278                 printk(KERN_ERR "onenand_bbt_wait: read timeout!"
1279                                 "ctrl=0x%04x intr=0x%04x\n", ctrl, interrupt);
1280                 return ONENAND_BBT_READ_FATAL_ERROR;
1281         }
1282
1283         /* Initial bad block case: 0x2400 or 0x0400 */
1284         if (ctrl & ONENAND_CTRL_ERROR) {
1285                 printk(KERN_DEBUG "onenand_bbt_wait: controller error = 0x%04x\n", ctrl);
1286                 return ONENAND_BBT_READ_ERROR;
1287         }
1288
1289         return 0;
1290 }
1291
1292 /**
1293  * onenand_bbt_read_oob - [MTD Interface] OneNAND read out-of-band for bbt scan
1294  * @param mtd           MTD device structure
1295  * @param from          offset to read from
1296  * @param ops           oob operation description structure
1297  *
1298  * OneNAND read out-of-band data from the spare area for bbt scan
1299  */
1300 int onenand_bbt_read_oob(struct mtd_info *mtd, loff_t from,
1301                 struct mtd_oob_ops *ops)
1302 {
1303         struct onenand_chip *this = mtd->priv;
1304         int read = 0, thislen, column;
1305         int ret = 0, readcmd;
1306         size_t len = ops->ooblen;
1307         u_char *buf = ops->oobbuf;
1308
1309         MTDDEBUG(MTD_DEBUG_LEVEL3, "onenand_bbt_read_oob: from = 0x%08x, len = %zi\n", (unsigned int) from, len);
1310
1311         readcmd = ONENAND_IS_MLC(this) ? ONENAND_CMD_READ : ONENAND_CMD_READOOB;
1312
1313         /* Initialize return value */
1314         ops->oobretlen = 0;
1315
1316         /* Do not allow reads past end of device */
1317         if (unlikely((from + len) > mtd->size)) {
1318                 printk(KERN_ERR "onenand_bbt_read_oob: Attempt read beyond end of device\n");
1319                 return ONENAND_BBT_READ_FATAL_ERROR;
1320         }
1321
1322         /* Grab the lock and see if the device is available */
1323         onenand_get_device(mtd, FL_READING);
1324
1325         column = from & (mtd->oobsize - 1);
1326
1327         while (read < len) {
1328
1329                 thislen = mtd->oobsize - column;
1330                 thislen = min_t(int, thislen, len);
1331
1332                 this->command(mtd, readcmd, from, mtd->oobsize);
1333
1334                 onenand_update_bufferram(mtd, from, 0);
1335
1336                 ret = this->bbt_wait(mtd, FL_READING);
1337                 if (unlikely(ret))
1338                         ret = onenand_recover_lsb(mtd, from, ret);
1339
1340                 if (ret)
1341                         break;
1342
1343                 this->read_bufferram(mtd, 0, ONENAND_SPARERAM, buf, column, thislen);
1344                 read += thislen;
1345                 if (read == len)
1346                         break;
1347
1348                 buf += thislen;
1349
1350                 /* Read more? */
1351                 if (read < len) {
1352                         /* Update Page size */
1353                         from += this->writesize;
1354                         column = 0;
1355                 }
1356         }
1357
1358         /* Deselect and wake up anyone waiting on the device */
1359         onenand_release_device(mtd);
1360
1361         ops->oobretlen = read;
1362         return ret;
1363 }
1364
1365 #ifdef CONFIG_MTD_ONENAND_VERIFY_WRITE
1366 /**
1367  * onenand_verify_oob - [GENERIC] verify the oob contents after a write
1368  * @param mtd           MTD device structure
1369  * @param buf           the databuffer to verify
1370  * @param to            offset to read from
1371  */
1372 static int onenand_verify_oob(struct mtd_info *mtd, const u_char *buf, loff_t to)
1373 {
1374         struct onenand_chip *this = mtd->priv;
1375         u_char *oob_buf = this->oob_buf;
1376         int status, i, readcmd;
1377
1378         readcmd = ONENAND_IS_MLC(this) ? ONENAND_CMD_READ : ONENAND_CMD_READOOB;
1379
1380         this->command(mtd, readcmd, to, mtd->oobsize);
1381         onenand_update_bufferram(mtd, to, 0);
1382         status = this->wait(mtd, FL_READING);
1383         if (status)
1384                 return status;
1385
1386         this->read_bufferram(mtd, 0, ONENAND_SPARERAM, oob_buf, 0, mtd->oobsize);
1387         for (i = 0; i < mtd->oobsize; i++)
1388                 if (buf[i] != 0xFF && buf[i] != oob_buf[i])
1389                         return -EBADMSG;
1390
1391         return 0;
1392 }
1393
1394 /**
1395  * onenand_verify - [GENERIC] verify the chip contents after a write
1396  * @param mtd          MTD device structure
1397  * @param buf          the databuffer to verify
1398  * @param addr         offset to read from
1399  * @param len          number of bytes to read and compare
1400  */
1401 static int onenand_verify(struct mtd_info *mtd, const u_char *buf, loff_t addr, size_t len)
1402 {
1403         struct onenand_chip *this = mtd->priv;
1404         int ret = 0;
1405         int thislen, column;
1406
1407         while (len != 0) {
1408                 thislen = min_t(int, this->writesize, len);
1409                 column = addr & (this->writesize - 1);
1410                 if (column + thislen > this->writesize)
1411                         thislen = this->writesize - column;
1412
1413                 this->command(mtd, ONENAND_CMD_READ, addr, this->writesize);
1414
1415                 onenand_update_bufferram(mtd, addr, 0);
1416
1417                 ret = this->wait(mtd, FL_READING);
1418                 if (ret)
1419                         return ret;
1420
1421                 onenand_update_bufferram(mtd, addr, 1);
1422
1423                 this->read_bufferram(mtd, 0, ONENAND_DATARAM, this->verify_buf, 0, mtd->writesize);
1424
1425                 if (memcmp(buf, this->verify_buf, thislen))
1426                         return -EBADMSG;
1427
1428                 len -= thislen;
1429                 buf += thislen;
1430                 addr += thislen;
1431         }
1432
1433         return 0;
1434 }
1435 #else
1436 #define onenand_verify(...)             (0)
1437 #define onenand_verify_oob(...)         (0)
1438 #endif
1439
1440 #define NOTALIGNED(x)   ((x & (this->subpagesize - 1)) != 0)
1441
1442 /**
1443  * onenand_fill_auto_oob - [Internal] oob auto-placement transfer
1444  * @param mtd           MTD device structure
1445  * @param oob_buf       oob buffer
1446  * @param buf           source address
1447  * @param column        oob offset to write to
1448  * @param thislen       oob length to write
1449  */
1450 static int onenand_fill_auto_oob(struct mtd_info *mtd, u_char *oob_buf,
1451                 const u_char *buf, int column, int thislen)
1452 {
1453         struct onenand_chip *this = mtd->priv;
1454         struct nand_oobfree *free;
1455         int writecol = column;
1456         int writeend = column + thislen;
1457         int lastgap = 0;
1458         unsigned int i;
1459
1460         free = this->ecclayout->oobfree;
1461         for (i = 0; i < MTD_MAX_OOBFREE_ENTRIES && free->length; i++, free++) {
1462                 if (writecol >= lastgap)
1463                         writecol += free->offset - lastgap;
1464                 if (writeend >= lastgap)
1465                         writeend += free->offset - lastgap;
1466                 lastgap = free->offset + free->length;
1467         }
1468         free = this->ecclayout->oobfree;
1469         for (i = 0; i < MTD_MAX_OOBFREE_ENTRIES && free->length; i++, free++) {
1470                 int free_end = free->offset + free->length;
1471                 if (free->offset < writeend && free_end > writecol) {
1472                         int st = max_t(int,free->offset,writecol);
1473                         int ed = min_t(int,free_end,writeend);
1474                         int n = ed - st;
1475                         memcpy(oob_buf + st, buf, n);
1476                         buf += n;
1477                 } else if (column == 0)
1478                         break;
1479         }
1480         return 0;
1481 }
1482
1483 /**
1484  * onenand_write_ops_nolock - [OneNAND Interface] write main and/or out-of-band
1485  * @param mtd           MTD device structure
1486  * @param to            offset to write to
1487  * @param ops           oob operation description structure
1488  *
1489  * Write main and/or oob with ECC
1490  */
1491 static int onenand_write_ops_nolock(struct mtd_info *mtd, loff_t to,
1492                 struct mtd_oob_ops *ops)
1493 {
1494         struct onenand_chip *this = mtd->priv;
1495         int written = 0, column, thislen, subpage;
1496         int oobwritten = 0, oobcolumn, thisooblen, oobsize;
1497         size_t len = ops->len;
1498         size_t ooblen = ops->ooblen;
1499         const u_char *buf = ops->datbuf;
1500         const u_char *oob = ops->oobbuf;
1501         u_char *oobbuf;
1502         int ret = 0;
1503
1504         MTDDEBUG(MTD_DEBUG_LEVEL3, "onenand_write_ops_nolock: to = 0x%08x, len = %i\n", (unsigned int) to, (int) len);
1505
1506         /* Initialize retlen, in case of early exit */
1507         ops->retlen = 0;
1508         ops->oobretlen = 0;
1509
1510         /* Do not allow writes past end of device */
1511         if (unlikely((to + len) > mtd->size)) {
1512                 printk(KERN_ERR "onenand_write_ops_nolock: Attempt write to past end of device\n");
1513                 return -EINVAL;
1514         }
1515
1516         /* Reject writes, which are not page aligned */
1517         if (unlikely(NOTALIGNED(to) || NOTALIGNED(len))) {
1518                 printk(KERN_ERR "onenand_write_ops_nolock: Attempt to write not page aligned data\n");
1519                 return -EINVAL;
1520         }
1521
1522         if (ops->mode == MTD_OOB_AUTO)
1523                 oobsize = this->ecclayout->oobavail;
1524         else
1525                 oobsize = mtd->oobsize;
1526
1527         oobcolumn = to & (mtd->oobsize - 1);
1528
1529         column = to & (mtd->writesize - 1);
1530
1531         /* Loop until all data write */
1532         while (written < len) {
1533                 u_char *wbuf = (u_char *) buf;
1534
1535                 thislen = min_t(int, mtd->writesize - column, len - written);
1536                 thisooblen = min_t(int, oobsize - oobcolumn, ooblen - oobwritten);
1537
1538                 this->command(mtd, ONENAND_CMD_BUFFERRAM, to, thislen);
1539
1540                 /* Partial page write */
1541                 subpage = thislen < mtd->writesize;
1542                 if (subpage) {
1543                         memset(this->page_buf, 0xff, mtd->writesize);
1544                         memcpy(this->page_buf + column, buf, thislen);
1545                         wbuf = this->page_buf;
1546                 }
1547
1548                 this->write_bufferram(mtd, to, ONENAND_DATARAM, wbuf, 0, mtd->writesize);
1549
1550                 if (oob) {
1551                         oobbuf = this->oob_buf;
1552
1553                         /* We send data to spare ram with oobsize
1554                          *                          * to prevent byte access */
1555                         memset(oobbuf, 0xff, mtd->oobsize);
1556                         if (ops->mode == MTD_OOB_AUTO)
1557                                 onenand_fill_auto_oob(mtd, oobbuf, oob, oobcolumn, thisooblen);
1558                         else
1559                                 memcpy(oobbuf + oobcolumn, oob, thisooblen);
1560
1561                         oobwritten += thisooblen;
1562                         oob += thisooblen;
1563                         oobcolumn = 0;
1564                 } else
1565                         oobbuf = (u_char *) ffchars;
1566
1567                 this->write_bufferram(mtd, to, ONENAND_SPARERAM, oobbuf, 0, mtd->oobsize);
1568
1569                 this->command(mtd, ONENAND_CMD_PROG, to, mtd->writesize);
1570
1571                 ret = this->wait(mtd, FL_WRITING);
1572
1573                 /* In partial page write we don't update bufferram */
1574                 onenand_update_bufferram(mtd, to, !ret && !subpage);
1575                 if (ONENAND_IS_2PLANE(this)) {
1576                         ONENAND_SET_BUFFERRAM1(this);
1577                         onenand_update_bufferram(mtd, to + this->writesize, !ret && !subpage);
1578                 }
1579
1580                 if (ret) {
1581                         printk(KERN_ERR "onenand_write_ops_nolock: write filaed %d\n", ret);
1582                         break;
1583                 }
1584
1585                 /* Only check verify write turn on */
1586                 ret = onenand_verify(mtd, buf, to, thislen);
1587                 if (ret) {
1588                         printk(KERN_ERR "onenand_write_ops_nolock: verify failed %d\n", ret);
1589                         break;
1590                 }
1591
1592                 written += thislen;
1593
1594                 if (written == len)
1595                         break;
1596
1597                 column = 0;
1598                 to += thislen;
1599                 buf += thislen;
1600         }
1601
1602         ops->retlen = written;
1603
1604         return ret;
1605 }
1606
1607 /**
1608  * onenand_write_oob_nolock - [Internal] OneNAND write out-of-band
1609  * @param mtd           MTD device structure
1610  * @param to            offset to write to
1611  * @param len           number of bytes to write
1612  * @param retlen        pointer to variable to store the number of written bytes
1613  * @param buf           the data to write
1614  * @param mode          operation mode
1615  *
1616  * OneNAND write out-of-band
1617  */
1618 static int onenand_write_oob_nolock(struct mtd_info *mtd, loff_t to,
1619                 struct mtd_oob_ops *ops)
1620 {
1621         struct onenand_chip *this = mtd->priv;
1622         int column, ret = 0, oobsize;
1623         int written = 0, oobcmd;
1624         u_char *oobbuf;
1625         size_t len = ops->ooblen;
1626         const u_char *buf = ops->oobbuf;
1627         mtd_oob_mode_t mode = ops->mode;
1628
1629         to += ops->ooboffs;
1630
1631         MTDDEBUG(MTD_DEBUG_LEVEL3, "onenand_write_oob_nolock: to = 0x%08x, len = %i\n", (unsigned int) to, (int) len);
1632
1633         /* Initialize retlen, in case of early exit */
1634         ops->oobretlen = 0;
1635
1636         if (mode == MTD_OOB_AUTO)
1637                 oobsize = this->ecclayout->oobavail;
1638         else
1639                 oobsize = mtd->oobsize;
1640
1641         column = to & (mtd->oobsize - 1);
1642
1643         if (unlikely(column >= oobsize)) {
1644                 printk(KERN_ERR "onenand_write_oob_nolock: Attempted to start write outside oob\n");
1645                 return -EINVAL;
1646         }
1647
1648         /* For compatibility with NAND: Do not allow write past end of page */
1649         if (unlikely(column + len > oobsize)) {
1650                 printk(KERN_ERR "onenand_write_oob_nolock: "
1651                                 "Attempt to write past end of page\n");
1652                 return -EINVAL;
1653         }
1654
1655         /* Do not allow reads past end of device */
1656         if (unlikely(to >= mtd->size ||
1657                                 column + len > ((mtd->size >> this->page_shift) -
1658                                         (to >> this->page_shift)) * oobsize)) {
1659                 printk(KERN_ERR "onenand_write_oob_nolock: Attempted to write past end of device\n");
1660                 return -EINVAL;
1661         }
1662
1663         oobbuf = this->oob_buf;
1664
1665         oobcmd = ONENAND_IS_MLC(this) ? ONENAND_CMD_PROG : ONENAND_CMD_PROGOOB;
1666
1667         /* Loop until all data write */
1668         while (written < len) {
1669                 int thislen = min_t(int, oobsize, len - written);
1670
1671                 this->command(mtd, ONENAND_CMD_BUFFERRAM, to, mtd->oobsize);
1672
1673                 /* We send data to spare ram with oobsize
1674                  * to prevent byte access */
1675                 memset(oobbuf, 0xff, mtd->oobsize);
1676                 if (mode == MTD_OOB_AUTO)
1677                         onenand_fill_auto_oob(mtd, oobbuf, buf, column, thislen);
1678                 else
1679                         memcpy(oobbuf + column, buf, thislen);
1680                 this->write_bufferram(mtd, 0, ONENAND_SPARERAM, oobbuf, 0, mtd->oobsize);
1681
1682                 if (ONENAND_IS_MLC(this)) {
1683                         /* Set main area of DataRAM to 0xff*/
1684                         memset(this->page_buf, 0xff, mtd->writesize);
1685                         this->write_bufferram(mtd, 0, ONENAND_DATARAM,
1686                                 this->page_buf, 0, mtd->writesize);
1687                 }
1688
1689                 this->command(mtd, oobcmd, to, mtd->oobsize);
1690
1691                 onenand_update_bufferram(mtd, to, 0);
1692                 if (ONENAND_IS_2PLANE(this)) {
1693                         ONENAND_SET_BUFFERRAM1(this);
1694                         onenand_update_bufferram(mtd, to + this->writesize, 0);
1695                 }
1696
1697                 ret = this->wait(mtd, FL_WRITING);
1698                 if (ret) {
1699                         printk(KERN_ERR "onenand_write_oob_nolock: write failed %d\n", ret);
1700                         break;
1701                 }
1702
1703                 ret = onenand_verify_oob(mtd, oobbuf, to);
1704                 if (ret) {
1705                         printk(KERN_ERR "onenand_write_oob_nolock: verify failed %d\n", ret);
1706                         break;
1707                 }
1708
1709                 written += thislen;
1710                 if (written == len)
1711                         break;
1712
1713                 to += mtd->writesize;
1714                 buf += thislen;
1715                 column = 0;
1716         }
1717
1718         ops->oobretlen = written;
1719
1720         return ret;
1721 }
1722
1723 /**
1724  * onenand_write - [MTD Interface] compability function for onenand_write_ecc
1725  * @param mtd           MTD device structure
1726  * @param to            offset to write to
1727  * @param len           number of bytes to write
1728  * @param retlen        pointer to variable to store the number of written bytes
1729  * @param buf           the data to write
1730  *
1731  * Write with ECC
1732  */
1733 static int onenand_write(struct mtd_info *mtd, loff_t to, size_t len,
1734                   size_t * retlen, const u_char * buf)
1735 {
1736         struct mtd_oob_ops ops = {
1737                 .len    = len,
1738                 .ooblen = 0,
1739                 .datbuf = (u_char *) buf,
1740                 .oobbuf = NULL,
1741                 .mode   = MTD_OOB_AUTO,
1742         };
1743         int ret;
1744
1745         onenand_get_device(mtd, FL_WRITING);
1746         ret = onenand_write_ops_nolock(mtd, to, &ops);
1747         onenand_release_device(mtd);
1748
1749         *retlen = ops.retlen;
1750         return ret;
1751 }
1752
1753 /**
1754  * onenand_write_oob - [MTD Interface] OneNAND write out-of-band
1755  * @param mtd           MTD device structure
1756  * @param to            offset to write to
1757  * @param ops           oob operation description structure
1758  *
1759  * OneNAND write main and/or out-of-band
1760  */
1761 int onenand_write_oob(struct mtd_info *mtd, loff_t to,
1762                         struct mtd_oob_ops *ops)
1763 {
1764         int ret;
1765
1766         switch (ops->mode) {
1767         case MTD_OOB_PLACE:
1768         case MTD_OOB_AUTO:
1769                 break;
1770         case MTD_OOB_RAW:
1771                 /* Not implemented yet */
1772         default:
1773                 return -EINVAL;
1774         }
1775
1776         onenand_get_device(mtd, FL_WRITING);
1777         if (ops->datbuf)
1778                 ret = onenand_write_ops_nolock(mtd, to, ops);
1779         else
1780                 ret = onenand_write_oob_nolock(mtd, to, ops);
1781         onenand_release_device(mtd);
1782
1783         return ret;
1784
1785 }
1786
1787 /**
1788  * onenand_block_isbad_nolock - [GENERIC] Check if a block is marked bad
1789  * @param mtd           MTD device structure
1790  * @param ofs           offset from device start
1791  * @param allowbbt      1, if its allowed to access the bbt area
1792  *
1793  * Check, if the block is bad, Either by reading the bad block table or
1794  * calling of the scan function.
1795  */
1796 static int onenand_block_isbad_nolock(struct mtd_info *mtd, loff_t ofs, int allowbbt)
1797 {
1798         struct onenand_chip *this = mtd->priv;
1799         struct bbm_info *bbm = this->bbm;
1800
1801         /* Return info from the table */
1802         return bbm->isbad_bbt(mtd, ofs, allowbbt);
1803 }
1804
1805
1806 /**
1807  * onenand_erase - [MTD Interface] erase block(s)
1808  * @param mtd           MTD device structure
1809  * @param instr         erase instruction
1810  *
1811  * Erase one ore more blocks
1812  */
1813 static int onenand_erase(struct mtd_info *mtd, struct erase_info *instr)
1814 {
1815         struct onenand_chip *this = mtd->priv;
1816         unsigned int block_size;
1817         loff_t addr = instr->addr;
1818         unsigned int len = instr->len;
1819         int ret = 0, i;
1820         struct mtd_erase_region_info *region = NULL;
1821         unsigned int region_end = 0;
1822
1823         MTDDEBUG(MTD_DEBUG_LEVEL3, "onenand_erase: start = 0x%08x, len = %i\n",
1824                         (unsigned int) addr, len);
1825
1826         /* Do not allow erase past end of device */
1827         if (unlikely((len + addr) > mtd->size)) {
1828                 MTDDEBUG(MTD_DEBUG_LEVEL0, "onenand_erase:"
1829                                         "Erase past end of device\n");
1830                 return -EINVAL;
1831         }
1832
1833         if (FLEXONENAND(this)) {
1834                 /* Find the eraseregion of this address */
1835                 i = flexonenand_region(mtd, addr);
1836                 region = &mtd->eraseregions[i];
1837
1838                 block_size = region->erasesize;
1839                 region_end = region->offset
1840                         + region->erasesize * region->numblocks;
1841
1842                 /* Start address within region must align on block boundary.
1843                  * Erase region's start offset is always block start address.
1844                  */
1845                 if (unlikely((addr - region->offset) & (block_size - 1))) {
1846                         MTDDEBUG(MTD_DEBUG_LEVEL0, "onenand_erase:"
1847                                 " Unaligned address\n");
1848                         return -EINVAL;
1849                 }
1850         } else {
1851                 block_size = 1 << this->erase_shift;
1852
1853                 /* Start address must align on block boundary */
1854                 if (unlikely(addr & (block_size - 1))) {
1855                         MTDDEBUG(MTD_DEBUG_LEVEL0, "onenand_erase:"
1856                                                 "Unaligned address\n");
1857                         return -EINVAL;
1858                 }
1859         }
1860
1861         /* Length must align on block boundary */
1862         if (unlikely(len & (block_size - 1))) {
1863                 MTDDEBUG (MTD_DEBUG_LEVEL0,
1864                          "onenand_erase: Length not block aligned\n");
1865                 return -EINVAL;
1866         }
1867
1868         instr->fail_addr = 0xffffffff;
1869
1870         /* Grab the lock and see if the device is available */
1871         onenand_get_device(mtd, FL_ERASING);
1872
1873         /* Loop throught the pages */
1874         instr->state = MTD_ERASING;
1875
1876         while (len) {
1877
1878                 /* Check if we have a bad block, we do not erase bad blocks */
1879                 if (instr->priv == 0 && onenand_block_isbad_nolock(mtd, addr, 0)) {
1880                         printk(KERN_WARNING "onenand_erase: attempt to erase"
1881                                 " a bad block at addr 0x%08x\n",
1882                                 (unsigned int) addr);
1883                         instr->state = MTD_ERASE_FAILED;
1884                         goto erase_exit;
1885                 }
1886
1887                 this->command(mtd, ONENAND_CMD_ERASE, addr, block_size);
1888
1889                 onenand_invalidate_bufferram(mtd, addr, block_size);
1890
1891                 ret = this->wait(mtd, FL_ERASING);
1892                 /* Check, if it is write protected */
1893                 if (ret) {
1894                         if (ret == -EPERM)
1895                                 MTDDEBUG (MTD_DEBUG_LEVEL0, "onenand_erase: "
1896                                           "Device is write protected!!!\n");
1897                         else
1898                                 MTDDEBUG (MTD_DEBUG_LEVEL0, "onenand_erase: "
1899                                           "Failed erase, block %d\n",
1900                                         onenand_block(this, addr));
1901                         instr->state = MTD_ERASE_FAILED;
1902                         instr->fail_addr = addr;
1903                 }
1904
1905                 len -= block_size;
1906                 addr += block_size;
1907
1908                 if (addr == region_end) {
1909                         if (!len)
1910                                 break;
1911                         region++;
1912
1913                         block_size = region->erasesize;
1914                         region_end = region->offset
1915                                 + region->erasesize * region->numblocks;
1916
1917                         if (len & (block_size - 1)) {
1918                                 /* This has been checked at MTD
1919                                  * partitioning level. */
1920                                 printk("onenand_erase: Unaligned address\n");
1921                                 goto erase_exit;
1922                         }
1923                 }
1924         }
1925
1926         instr->state = MTD_ERASE_DONE;
1927
1928 erase_exit:
1929
1930         ret = instr->state == MTD_ERASE_DONE ? 0 : -EIO;
1931         /* Do call back function */
1932         if (!ret)
1933                 mtd_erase_callback(instr);
1934
1935         /* Deselect and wake up anyone waiting on the device */
1936         onenand_release_device(mtd);
1937
1938         return ret;
1939 }
1940
1941 /**
1942  * onenand_sync - [MTD Interface] sync
1943  * @param mtd           MTD device structure
1944  *
1945  * Sync is actually a wait for chip ready function
1946  */
1947 static void onenand_sync(struct mtd_info *mtd)
1948 {
1949         MTDDEBUG (MTD_DEBUG_LEVEL3, "onenand_sync: called\n");
1950
1951         /* Grab the lock and see if the device is available */
1952         onenand_get_device(mtd, FL_SYNCING);
1953
1954         /* Release it and go back */
1955         onenand_release_device(mtd);
1956 }
1957
1958 /**
1959  * onenand_block_isbad - [MTD Interface] Check whether the block at the given offset is bad
1960  * @param mtd           MTD device structure
1961  * @param ofs           offset relative to mtd start
1962  *
1963  * Check whether the block is bad
1964  */
1965 static int onenand_block_isbad(struct mtd_info *mtd, loff_t ofs)
1966 {
1967         int ret;
1968
1969         /* Check for invalid offset */
1970         if (ofs > mtd->size)
1971                 return -EINVAL;
1972
1973         onenand_get_device(mtd, FL_READING);
1974         ret = onenand_block_isbad_nolock(mtd, ofs, 0);
1975         onenand_release_device(mtd);
1976         return ret;
1977 }
1978
1979 /**
1980  * onenand_default_block_markbad - [DEFAULT] mark a block bad
1981  * @param mtd           MTD device structure
1982  * @param ofs           offset from device start
1983  *
1984  * This is the default implementation, which can be overridden by
1985  * a hardware specific driver.
1986  */
1987 static int onenand_default_block_markbad(struct mtd_info *mtd, loff_t ofs)
1988 {
1989         struct onenand_chip *this = mtd->priv;
1990         struct bbm_info *bbm = this->bbm;
1991         u_char buf[2] = {0, 0};
1992         struct mtd_oob_ops ops = {
1993                 .mode = MTD_OOB_PLACE,
1994                 .ooblen = 2,
1995                 .oobbuf = buf,
1996                 .ooboffs = 0,
1997         };
1998         int block;
1999
2000         /* Get block number */
2001         block = onenand_block(this, ofs);
2002         if (bbm->bbt)
2003                 bbm->bbt[block >> 2] |= 0x01 << ((block & 0x03) << 1);
2004
2005         /* We write two bytes, so we dont have to mess with 16 bit access */
2006         ofs += mtd->oobsize + (bbm->badblockpos & ~0x01);
2007         return onenand_write_oob_nolock(mtd, ofs, &ops);
2008 }
2009
2010 /**
2011  * onenand_block_markbad - [MTD Interface] Mark the block at the given offset as bad
2012  * @param mtd           MTD device structure
2013  * @param ofs           offset relative to mtd start
2014  *
2015  * Mark the block as bad
2016  */
2017 static int onenand_block_markbad(struct mtd_info *mtd, loff_t ofs)
2018 {
2019         struct onenand_chip *this = mtd->priv;
2020         int ret;
2021
2022         ret = onenand_block_isbad(mtd, ofs);
2023         if (ret) {
2024                 /* If it was bad already, return success and do nothing */
2025                 if (ret > 0)
2026                         return 0;
2027                 return ret;
2028         }
2029
2030         ret = this->block_markbad(mtd, ofs);
2031         return ret;
2032 }
2033
2034 /**
2035  * onenand_do_lock_cmd - [OneNAND Interface] Lock or unlock block(s)
2036  * @param mtd           MTD device structure
2037  * @param ofs           offset relative to mtd start
2038  * @param len           number of bytes to lock or unlock
2039  * @param cmd           lock or unlock command
2040  *
2041  * Lock or unlock one or more blocks
2042  */
2043 static int onenand_do_lock_cmd(struct mtd_info *mtd, loff_t ofs, size_t len, int cmd)
2044 {
2045         struct onenand_chip *this = mtd->priv;
2046         int start, end, block, value, status;
2047         int wp_status_mask;
2048
2049         start = onenand_block(this, ofs);
2050         end = onenand_block(this, ofs + len);
2051
2052         if (cmd == ONENAND_CMD_LOCK)
2053                 wp_status_mask = ONENAND_WP_LS;
2054         else
2055                 wp_status_mask = ONENAND_WP_US;
2056
2057         /* Continuous lock scheme */
2058         if (this->options & ONENAND_HAS_CONT_LOCK) {
2059                 /* Set start block address */
2060                 this->write_word(start,
2061                                  this->base + ONENAND_REG_START_BLOCK_ADDRESS);
2062                 /* Set end block address */
2063                 this->write_word(end - 1,
2064                                  this->base + ONENAND_REG_END_BLOCK_ADDRESS);
2065                 /* Write unlock command */
2066                 this->command(mtd, cmd, 0, 0);
2067
2068                 /* There's no return value */
2069                 this->wait(mtd, FL_UNLOCKING);
2070
2071                 /* Sanity check */
2072                 while (this->read_word(this->base + ONENAND_REG_CTRL_STATUS)
2073                        & ONENAND_CTRL_ONGO)
2074                         continue;
2075
2076                 /* Check lock status */
2077                 status = this->read_word(this->base + ONENAND_REG_WP_STATUS);
2078                 if (!(status & ONENAND_WP_US))
2079                         printk(KERN_ERR "wp status = 0x%x\n", status);
2080
2081                 return 0;
2082         }
2083
2084         /* Block lock scheme */
2085         for (block = start; block < end; block++) {
2086                 /* Set block address */
2087                 value = onenand_block_address(this, block);
2088                 this->write_word(value, this->base + ONENAND_REG_START_ADDRESS1);
2089                 /* Select DataRAM for DDP */
2090                 value = onenand_bufferram_address(this, block);
2091                 this->write_word(value, this->base + ONENAND_REG_START_ADDRESS2);
2092
2093                 /* Set start block address */
2094                 this->write_word(block,
2095                                  this->base + ONENAND_REG_START_BLOCK_ADDRESS);
2096                 /* Write unlock command */
2097                 this->command(mtd, ONENAND_CMD_UNLOCK, 0, 0);
2098
2099                 /* There's no return value */
2100                 this->wait(mtd, FL_UNLOCKING);
2101
2102                 /* Sanity check */
2103                 while (this->read_word(this->base + ONENAND_REG_CTRL_STATUS)
2104                        & ONENAND_CTRL_ONGO)
2105                         continue;
2106
2107                 /* Check lock status */
2108                 status = this->read_word(this->base + ONENAND_REG_WP_STATUS);
2109                 if (!(status & ONENAND_WP_US))
2110                         printk(KERN_ERR "block = %d, wp status = 0x%x\n",
2111                                block, status);
2112         }
2113
2114         return 0;
2115 }
2116
2117 #ifdef ONENAND_LINUX
2118 /**
2119  * onenand_lock - [MTD Interface] Lock block(s)
2120  * @param mtd           MTD device structure
2121  * @param ofs           offset relative to mtd start
2122  * @param len           number of bytes to unlock
2123  *
2124  * Lock one or more blocks
2125  */
2126 static int onenand_lock(struct mtd_info *mtd, loff_t ofs, size_t len)
2127 {
2128         int ret;
2129
2130         onenand_get_device(mtd, FL_LOCKING);
2131         ret = onenand_do_lock_cmd(mtd, ofs, len, ONENAND_CMD_LOCK);
2132         onenand_release_device(mtd);
2133         return ret;
2134 }
2135
2136 /**
2137  * onenand_unlock - [MTD Interface] Unlock block(s)
2138  * @param mtd           MTD device structure
2139  * @param ofs           offset relative to mtd start
2140  * @param len           number of bytes to unlock
2141  *
2142  * Unlock one or more blocks
2143  */
2144 static int onenand_unlock(struct mtd_info *mtd, loff_t ofs, size_t len)
2145 {
2146         int ret;
2147
2148         onenand_get_device(mtd, FL_LOCKING);
2149         ret = onenand_do_lock_cmd(mtd, ofs, len, ONENAND_CMD_UNLOCK);
2150         onenand_release_device(mtd);
2151         return ret;
2152 }
2153 #endif
2154
2155 /**
2156  * onenand_check_lock_status - [OneNAND Interface] Check lock status
2157  * @param this          onenand chip data structure
2158  *
2159  * Check lock status
2160  */
2161 static int onenand_check_lock_status(struct onenand_chip *this)
2162 {
2163         unsigned int value, block, status;
2164         unsigned int end;
2165
2166         end = this->chipsize >> this->erase_shift;
2167         for (block = 0; block < end; block++) {
2168                 /* Set block address */
2169                 value = onenand_block_address(this, block);
2170                 this->write_word(value, this->base + ONENAND_REG_START_ADDRESS1);
2171                 /* Select DataRAM for DDP */
2172                 value = onenand_bufferram_address(this, block);
2173                 this->write_word(value, this->base + ONENAND_REG_START_ADDRESS2);
2174                 /* Set start block address */
2175                 this->write_word(block, this->base + ONENAND_REG_START_BLOCK_ADDRESS);
2176
2177                 /* Check lock status */
2178                 status = this->read_word(this->base + ONENAND_REG_WP_STATUS);
2179                 if (!(status & ONENAND_WP_US)) {
2180                         printk(KERN_ERR "block = %d, wp status = 0x%x\n", block, status);
2181                         return 0;
2182                 }
2183         }
2184
2185         return 1;
2186 }
2187
2188 /**
2189  * onenand_unlock_all - [OneNAND Interface] unlock all blocks
2190  * @param mtd           MTD device structure
2191  *
2192  * Unlock all blocks
2193  */
2194 static void onenand_unlock_all(struct mtd_info *mtd)
2195 {
2196         struct onenand_chip *this = mtd->priv;
2197         loff_t ofs = 0;
2198         size_t len = mtd->size;
2199
2200         if (this->options & ONENAND_HAS_UNLOCK_ALL) {
2201                 /* Set start block address */
2202                 this->write_word(0, this->base + ONENAND_REG_START_BLOCK_ADDRESS);
2203                 /* Write unlock command */
2204                 this->command(mtd, ONENAND_CMD_UNLOCK_ALL, 0, 0);
2205
2206                 /* There's no return value */
2207                 this->wait(mtd, FL_LOCKING);
2208
2209                 /* Sanity check */
2210                 while (this->read_word(this->base + ONENAND_REG_CTRL_STATUS)
2211                                 & ONENAND_CTRL_ONGO)
2212                         continue;
2213
2214                 /* Check lock status */
2215                 if (onenand_check_lock_status(this))
2216                         return;
2217
2218                 /* Workaround for all block unlock in DDP */
2219                 if (ONENAND_IS_DDP(this) && !FLEXONENAND(this)) {
2220                         /* All blocks on another chip */
2221                         ofs = this->chipsize >> 1;
2222                         len = this->chipsize >> 1;
2223                 }
2224         }
2225
2226         onenand_do_lock_cmd(mtd, ofs, len, ONENAND_CMD_UNLOCK);
2227 }
2228
2229
2230 /**
2231  * onenand_check_features - Check and set OneNAND features
2232  * @param mtd           MTD data structure
2233  *
2234  * Check and set OneNAND features
2235  * - lock scheme
2236  * - two plane
2237  */
2238 static void onenand_check_features(struct mtd_info *mtd)
2239 {
2240         struct onenand_chip *this = mtd->priv;
2241         unsigned int density, process;
2242         unsigned int syscfg;
2243
2244         /* Lock scheme depends on density and process */
2245         density = onenand_get_density(this->device_id);
2246         process = this->version_id >> ONENAND_VERSION_PROCESS_SHIFT;
2247
2248         /* Lock scheme */
2249         switch (density) {
2250         case ONENAND_DEVICE_DENSITY_4Gb:
2251                 if (ONENAND_IS_DDP(this))
2252                         this->options |= ONENAND_HAS_2PLANE;
2253                 else
2254                         this->options |= ONENAND_HAS_4KB_PAGE;
2255
2256         case ONENAND_DEVICE_DENSITY_2Gb:
2257                 /* 2Gb DDP don't have 2 plane */
2258                 if (!ONENAND_IS_DDP(this))
2259                         this->options |= ONENAND_HAS_2PLANE;
2260                 this->options |= ONENAND_HAS_UNLOCK_ALL;
2261
2262         case ONENAND_DEVICE_DENSITY_1Gb:
2263                 /* A-Die has all block unlock */
2264                 if (process)
2265                         this->options |= ONENAND_HAS_UNLOCK_ALL;
2266                 break;
2267
2268         default:
2269                 /* Some OneNAND has continuous lock scheme */
2270                 if (!process)
2271                         this->options |= ONENAND_HAS_CONT_LOCK;
2272                 break;
2273         }
2274
2275         if (ONENAND_IS_4KB_PAGE(this))
2276                 this->options &= ~ONENAND_HAS_2PLANE;
2277
2278         if (FLEXONENAND(this)) {
2279                 this->options &= ~ONENAND_HAS_CONT_LOCK;
2280                 this->options |= ONENAND_HAS_UNLOCK_ALL;
2281         }
2282
2283         syscfg = this->read_word(this->base + ONENAND_REG_SYS_CFG1);
2284         if (syscfg & ONENAND_SYS_CFG1_SYNC_READ) {
2285                 printk(KERN_DEBUG "Sync Burst Mode\n");
2286                 this->options |= ONENAND_SYNC_MODE;
2287         }
2288
2289         if (this->options & ONENAND_HAS_CONT_LOCK)
2290                 printk(KERN_DEBUG "Lock scheme is Continuous Lock\n");
2291         if (this->options & ONENAND_HAS_UNLOCK_ALL)
2292                 printk(KERN_DEBUG "Chip support all block unlock\n");
2293 #ifdef ONENAND_LINUX
2294         if (this->options & ONENAND_HAS_2PLANE)
2295                 printk(KERN_DEBUG "Chip has 2 plane\n");
2296 #endif
2297         if (this->options & ONENAND_HAS_4KB_PAGE)
2298                 printk(KERN_DEBUG "Chip has 4KiB pagesize\n");
2299 }
2300
2301 /**
2302  * onenand_print_device_info - Print device ID
2303  * @param device        device ID
2304  *
2305  * Print device ID
2306  */
2307 char *onenand_print_device_info(int device, int version)
2308 {
2309         int vcc, demuxed, ddp, density, flexonenand;
2310         char *dev_info = malloc(80);
2311         char *p = dev_info;
2312
2313         vcc = device & ONENAND_DEVICE_VCC_MASK;
2314         demuxed = device & ONENAND_DEVICE_IS_DEMUX;
2315         ddp = device & ONENAND_DEVICE_IS_DDP;
2316         density = onenand_get_density(device);
2317         flexonenand = device & DEVICE_IS_FLEXONENAND;
2318         p += sprintf(dev_info, "%s%sOneNAND%s %dMB %sV 16-bit (0x%02x)",
2319                demuxed ? "" : "Muxed ",
2320                flexonenand ? "Flex-" : "",
2321                ddp ? "(DDP)" : "",
2322                (16 << density), vcc ? "2.65/3.3" : "1.8", device);
2323
2324         sprintf(p, "\nOneNAND version = 0x%04x", version);
2325         printk("%s\n", dev_info);
2326
2327         return dev_info;
2328 }
2329
2330 static const struct onenand_manufacturers onenand_manuf_ids[] = {
2331         {ONENAND_MFR_SAMSUNG, "Samsung"},
2332 };
2333
2334 /**
2335  * onenand_check_maf - Check manufacturer ID
2336  * @param manuf         manufacturer ID
2337  *
2338  * Check manufacturer ID
2339  */
2340 static int onenand_check_maf(int manuf)
2341 {
2342         int size = ARRAY_SIZE(onenand_manuf_ids);
2343         char *name;
2344         int i;
2345
2346         for (i = 0; i < size; i++)
2347                 if (manuf == onenand_manuf_ids[i].id)
2348                         break;
2349
2350         if (i < size)
2351                 name = onenand_manuf_ids[i].name;
2352         else
2353                 name = "Unknown";
2354
2355 #ifdef ONENAND_DEBUG
2356         printk(KERN_DEBUG "OneNAND Manufacturer: %s (0x%0x)\n", name, manuf);
2357 #endif
2358
2359         return i == size;
2360 }
2361
2362 /**
2363 * flexonenand_get_boundary      - Reads the SLC boundary
2364 * @param onenand_info           - onenand info structure
2365 *
2366 * Fill up boundary[] field in onenand_chip
2367 **/
2368 static int flexonenand_get_boundary(struct mtd_info *mtd)
2369 {
2370         struct onenand_chip *this = mtd->priv;
2371         unsigned int die, bdry;
2372         int ret, syscfg, locked;
2373
2374         /* Disable ECC */
2375         syscfg = this->read_word(this->base + ONENAND_REG_SYS_CFG1);
2376         this->write_word((syscfg | 0x0100), this->base + ONENAND_REG_SYS_CFG1);
2377
2378         for (die = 0; die < this->dies; die++) {
2379                 this->command(mtd, FLEXONENAND_CMD_PI_ACCESS, die, 0);
2380                 this->wait(mtd, FL_SYNCING);
2381
2382                 this->command(mtd, FLEXONENAND_CMD_READ_PI, die, 0);
2383                 ret = this->wait(mtd, FL_READING);
2384
2385                 bdry = this->read_word(this->base + ONENAND_DATARAM);
2386                 if ((bdry >> FLEXONENAND_PI_UNLOCK_SHIFT) == 3)
2387                         locked = 0;
2388                 else
2389                         locked = 1;
2390                 this->boundary[die] = bdry & FLEXONENAND_PI_MASK;
2391
2392                 this->command(mtd, ONENAND_CMD_RESET, 0, 0);
2393                 ret = this->wait(mtd, FL_RESETING);
2394
2395                 printk(KERN_INFO "Die %d boundary: %d%s\n", die,
2396                        this->boundary[die], locked ? "(Locked)" : "(Unlocked)");
2397         }
2398
2399         /* Enable ECC */
2400         this->write_word(syscfg, this->base + ONENAND_REG_SYS_CFG1);
2401         return 0;
2402 }
2403
2404 /**
2405  * flexonenand_get_size - Fill up fields in onenand_chip and mtd_info
2406  *                        boundary[], diesize[], mtd->size, mtd->erasesize,
2407  *                        mtd->eraseregions
2408  * @param mtd           - MTD device structure
2409  */
2410 static void flexonenand_get_size(struct mtd_info *mtd)
2411 {
2412         struct onenand_chip *this = mtd->priv;
2413         int die, i, eraseshift, density;
2414         int blksperdie, maxbdry;
2415         loff_t ofs;
2416
2417         density = onenand_get_density(this->device_id);
2418         blksperdie = ((loff_t)(16 << density) << 20) >> (this->erase_shift);
2419         blksperdie >>= ONENAND_IS_DDP(this) ? 1 : 0;
2420         maxbdry = blksperdie - 1;
2421         eraseshift = this->erase_shift - 1;
2422
2423         mtd->numeraseregions = this->dies << 1;
2424
2425         /* This fills up the device boundary */
2426         flexonenand_get_boundary(mtd);
2427         die = 0;
2428         ofs = 0;
2429         i = -1;
2430         for (; die < this->dies; die++) {
2431                 if (!die || this->boundary[die-1] != maxbdry) {
2432                         i++;
2433                         mtd->eraseregions[i].offset = ofs;
2434                         mtd->eraseregions[i].erasesize = 1 << eraseshift;
2435                         mtd->eraseregions[i].numblocks =
2436                                                         this->boundary[die] + 1;
2437                         ofs += mtd->eraseregions[i].numblocks << eraseshift;
2438                         eraseshift++;
2439                 } else {
2440                         mtd->numeraseregions -= 1;
2441                         mtd->eraseregions[i].numblocks +=
2442                                                         this->boundary[die] + 1;
2443                         ofs += (this->boundary[die] + 1) << (eraseshift - 1);
2444                 }
2445                 if (this->boundary[die] != maxbdry) {
2446                         i++;
2447                         mtd->eraseregions[i].offset = ofs;
2448                         mtd->eraseregions[i].erasesize = 1 << eraseshift;
2449                         mtd->eraseregions[i].numblocks = maxbdry ^
2450                                                          this->boundary[die];
2451                         ofs += mtd->eraseregions[i].numblocks << eraseshift;
2452                         eraseshift--;
2453                 } else
2454                         mtd->numeraseregions -= 1;
2455         }
2456
2457         /* Expose MLC erase size except when all blocks are SLC */
2458         mtd->erasesize = 1 << this->erase_shift;
2459         if (mtd->numeraseregions == 1)
2460                 mtd->erasesize >>= 1;
2461
2462         printk(KERN_INFO "Device has %d eraseregions\n", mtd->numeraseregions);
2463         for (i = 0; i < mtd->numeraseregions; i++)
2464                 printk(KERN_INFO "[offset: 0x%08llx, erasesize: 0x%05x,"
2465                         " numblocks: %04u]\n", mtd->eraseregions[i].offset,
2466                         mtd->eraseregions[i].erasesize,
2467                         mtd->eraseregions[i].numblocks);
2468
2469         for (die = 0, mtd->size = 0; die < this->dies; die++) {
2470                 this->diesize[die] = (loff_t) (blksperdie << this->erase_shift);
2471                 this->diesize[die] -= (loff_t) (this->boundary[die] + 1)
2472                                                  << (this->erase_shift - 1);
2473                 mtd->size += this->diesize[die];
2474         }
2475 }
2476
2477 /**
2478  * flexonenand_check_blocks_erased - Check if blocks are erased
2479  * @param mtd_info      - mtd info structure
2480  * @param start         - first erase block to check
2481  * @param end           - last erase block to check
2482  *
2483  * Converting an unerased block from MLC to SLC
2484  * causes byte values to change. Since both data and its ECC
2485  * have changed, reads on the block give uncorrectable error.
2486  * This might lead to the block being detected as bad.
2487  *
2488  * Avoid this by ensuring that the block to be converted is
2489  * erased.
2490  */
2491 static int flexonenand_check_blocks_erased(struct mtd_info *mtd,
2492                                         int start, int end)
2493 {
2494         struct onenand_chip *this = mtd->priv;
2495         int i, ret;
2496         int block;
2497         struct mtd_oob_ops ops = {
2498                 .mode = MTD_OOB_PLACE,
2499                 .ooboffs = 0,
2500                 .ooblen = mtd->oobsize,
2501                 .datbuf = NULL,
2502                 .oobbuf = this->oob_buf,
2503         };
2504         loff_t addr;
2505
2506         printk(KERN_DEBUG "Check blocks from %d to %d\n", start, end);
2507
2508         for (block = start; block <= end; block++) {
2509                 addr = flexonenand_addr(this, block);
2510                 if (onenand_block_isbad_nolock(mtd, addr, 0))
2511                         continue;
2512
2513                 /*
2514                  * Since main area write results in ECC write to spare,
2515                  * it is sufficient to check only ECC bytes for change.
2516                  */
2517                 ret = onenand_read_oob_nolock(mtd, addr, &ops);
2518                 if (ret)
2519                         return ret;
2520
2521                 for (i = 0; i < mtd->oobsize; i++)
2522                         if (this->oob_buf[i] != 0xff)
2523                                 break;
2524
2525                 if (i != mtd->oobsize) {
2526                         printk(KERN_WARNING "Block %d not erased.\n", block);
2527                         return 1;
2528                 }
2529         }
2530
2531         return 0;
2532 }
2533
2534 /**
2535  * flexonenand_set_boundary     - Writes the SLC boundary
2536  * @param mtd                   - mtd info structure
2537  */
2538 int flexonenand_set_boundary(struct mtd_info *mtd, int die,
2539                                     int boundary, int lock)
2540 {
2541         struct onenand_chip *this = mtd->priv;
2542         int ret, density, blksperdie, old, new, thisboundary;
2543         loff_t addr;
2544
2545         if (die >= this->dies)
2546                 return -EINVAL;
2547
2548         if (boundary == this->boundary[die])
2549                 return 0;
2550
2551         density = onenand_get_density(this->device_id);
2552         blksperdie = ((16 << density) << 20) >> this->erase_shift;
2553         blksperdie >>= ONENAND_IS_DDP(this) ? 1 : 0;
2554
2555         if (boundary >= blksperdie) {
2556                 printk("flexonenand_set_boundary:"
2557                         "Invalid boundary value. "
2558                         "Boundary not changed.\n");
2559                 return -EINVAL;
2560         }
2561
2562         /* Check if converting blocks are erased */
2563         old = this->boundary[die] + (die * this->density_mask);
2564         new = boundary + (die * this->density_mask);
2565         ret = flexonenand_check_blocks_erased(mtd, min(old, new)
2566                                                 + 1, max(old, new));
2567         if (ret) {
2568                 printk(KERN_ERR "flexonenand_set_boundary: Please erase blocks before boundary change\n");
2569                 return ret;
2570         }
2571
2572         this->command(mtd, FLEXONENAND_CMD_PI_ACCESS, die, 0);
2573         this->wait(mtd, FL_SYNCING);
2574
2575         /* Check is boundary is locked */
2576         this->command(mtd, FLEXONENAND_CMD_READ_PI, die, 0);
2577         ret = this->wait(mtd, FL_READING);
2578
2579         thisboundary = this->read_word(this->base + ONENAND_DATARAM);
2580         if ((thisboundary >> FLEXONENAND_PI_UNLOCK_SHIFT) != 3) {
2581                 printk(KERN_ERR "flexonenand_set_boundary: boundary locked\n");
2582                 goto out;
2583         }
2584
2585         printk(KERN_INFO "flexonenand_set_boundary: Changing die %d boundary: %d%s\n",
2586                         die, boundary, lock ? "(Locked)" : "(Unlocked)");
2587
2588         boundary &= FLEXONENAND_PI_MASK;
2589         boundary |= lock ? 0 : (3 << FLEXONENAND_PI_UNLOCK_SHIFT);
2590
2591         addr = die ? this->diesize[0] : 0;
2592         this->command(mtd, ONENAND_CMD_ERASE, addr, 0);
2593         ret = this->wait(mtd, FL_ERASING);
2594         if (ret) {
2595                 printk("flexonenand_set_boundary:"
2596                         "Failed PI erase for Die %d\n", die);
2597                 goto out;
2598         }
2599
2600         this->write_word(boundary, this->base + ONENAND_DATARAM);
2601         this->command(mtd, ONENAND_CMD_PROG, addr, 0);
2602         ret = this->wait(mtd, FL_WRITING);
2603         if (ret) {
2604                 printk("flexonenand_set_boundary:"
2605                         "Failed PI write for Die %d\n", die);
2606                 goto out;
2607         }
2608
2609         this->command(mtd, FLEXONENAND_CMD_PI_UPDATE, die, 0);
2610         ret = this->wait(mtd, FL_WRITING);
2611 out:
2612         this->write_word(ONENAND_CMD_RESET, this->base + ONENAND_REG_COMMAND);
2613         this->wait(mtd, FL_RESETING);
2614         if (!ret)
2615                 /* Recalculate device size on boundary change*/
2616                 flexonenand_get_size(mtd);
2617
2618         return ret;
2619 }
2620
2621 /**
2622  * onenand_probe - [OneNAND Interface] Probe the OneNAND device
2623  * @param mtd           MTD device structure
2624  *
2625  * OneNAND detection method:
2626  *   Compare the the values from command with ones from register
2627  */
2628 static int onenand_probe(struct mtd_info *mtd)
2629 {
2630         struct onenand_chip *this = mtd->priv;
2631         int bram_maf_id, bram_dev_id, maf_id, dev_id, ver_id;
2632         int density;
2633         int syscfg;
2634
2635         /* Save system configuration 1 */
2636         syscfg = this->read_word(this->base + ONENAND_REG_SYS_CFG1);
2637         /* Clear Sync. Burst Read mode to read BootRAM */
2638         this->write_word((syscfg & ~ONENAND_SYS_CFG1_SYNC_READ), this->base + ONENAND_REG_SYS_CFG1);
2639
2640         /* Send the command for reading device ID from BootRAM */
2641         this->write_word(ONENAND_CMD_READID, this->base + ONENAND_BOOTRAM);
2642
2643         /* Read manufacturer and device IDs from BootRAM */
2644         bram_maf_id = this->read_word(this->base + ONENAND_BOOTRAM + 0x0);
2645         bram_dev_id = this->read_word(this->base + ONENAND_BOOTRAM + 0x2);
2646
2647         /* Reset OneNAND to read default register values */
2648         this->write_word(ONENAND_CMD_RESET, this->base + ONENAND_BOOTRAM);
2649
2650         /* Wait reset */
2651         this->wait(mtd, FL_RESETING);
2652
2653         /* Restore system configuration 1 */
2654         this->write_word(syscfg, this->base + ONENAND_REG_SYS_CFG1);
2655
2656         /* Check manufacturer ID */
2657         if (onenand_check_maf(bram_maf_id))
2658                 return -ENXIO;
2659
2660         /* Read manufacturer and device IDs from Register */
2661         maf_id = this->read_word(this->base + ONENAND_REG_MANUFACTURER_ID);
2662         dev_id = this->read_word(this->base + ONENAND_REG_DEVICE_ID);
2663         ver_id = this->read_word(this->base + ONENAND_REG_VERSION_ID);
2664         this->technology = this->read_word(this->base + ONENAND_REG_TECHNOLOGY);
2665
2666         /* Check OneNAND device */
2667         if (maf_id != bram_maf_id || dev_id != bram_dev_id)
2668                 return -ENXIO;
2669
2670         /* Flash device information */
2671         mtd->name = onenand_print_device_info(dev_id, ver_id);
2672         this->device_id = dev_id;
2673         this->version_id = ver_id;
2674
2675         /* Check OneNAND features */
2676         onenand_check_features(mtd);
2677
2678         density = onenand_get_density(dev_id);
2679         if (FLEXONENAND(this)) {
2680                 this->dies = ONENAND_IS_DDP(this) ? 2 : 1;
2681                 /* Maximum possible erase regions */
2682                 mtd->numeraseregions = this->dies << 1;
2683                 mtd->eraseregions = malloc(sizeof(struct mtd_erase_region_info)
2684                                         * (this->dies << 1));
2685                 if (!mtd->eraseregions)
2686                         return -ENOMEM;
2687         }
2688
2689         /*
2690          * For Flex-OneNAND, chipsize represents maximum possible device size.
2691          * mtd->size represents the actual device size.
2692          */
2693         this->chipsize = (16 << density) << 20;
2694
2695         /* OneNAND page size & block size */
2696         /* The data buffer size is equal to page size */
2697         mtd->writesize =
2698             this->read_word(this->base + ONENAND_REG_DATA_BUFFER_SIZE);
2699         /* We use the full BufferRAM */
2700         if (ONENAND_IS_4KB_PAGE(this))
2701                 mtd->writesize <<= 1;
2702
2703         mtd->oobsize = mtd->writesize >> 5;
2704         /* Pagers per block is always 64 in OneNAND */
2705         mtd->erasesize = mtd->writesize << 6;
2706         /*
2707          * Flex-OneNAND SLC area has 64 pages per block.
2708          * Flex-OneNAND MLC area has 128 pages per block.
2709          * Expose MLC erase size to find erase_shift and page_mask.
2710          */
2711         if (FLEXONENAND(this))
2712                 mtd->erasesize <<= 1;
2713
2714         this->erase_shift = ffs(mtd->erasesize) - 1;
2715         this->page_shift = ffs(mtd->writesize) - 1;
2716         this->ppb_shift = (this->erase_shift - this->page_shift);
2717         this->page_mask = (mtd->erasesize / mtd->writesize) - 1;
2718         /* Set density mask. it is used for DDP */
2719         if (ONENAND_IS_DDP(this))
2720                 this->density_mask = this->chipsize >> (this->erase_shift + 1);
2721         /* It's real page size */
2722         this->writesize = mtd->writesize;
2723
2724         /* REVIST: Multichip handling */
2725
2726         if (FLEXONENAND(this))
2727                 flexonenand_get_size(mtd);
2728         else
2729                 mtd->size = this->chipsize;
2730
2731         mtd->flags = MTD_CAP_NANDFLASH;
2732         mtd->erase = onenand_erase;
2733         mtd->read = onenand_read;
2734         mtd->write = onenand_write;
2735         mtd->read_oob = onenand_read_oob;
2736         mtd->write_oob = onenand_write_oob;
2737         mtd->sync = onenand_sync;
2738         mtd->block_isbad = onenand_block_isbad;
2739         mtd->block_markbad = onenand_block_markbad;
2740
2741         return 0;
2742 }
2743
2744 /**
2745  * onenand_scan - [OneNAND Interface] Scan for the OneNAND device
2746  * @param mtd           MTD device structure
2747  * @param maxchips      Number of chips to scan for
2748  *
2749  * This fills out all the not initialized function pointers
2750  * with the defaults.
2751  * The flash ID is read and the mtd/chip structures are
2752  * filled with the appropriate values.
2753  */
2754 int onenand_scan(struct mtd_info *mtd, int maxchips)
2755 {
2756         int i;
2757         struct onenand_chip *this = mtd->priv;
2758
2759         if (!this->read_word)
2760                 this->read_word = onenand_readw;
2761         if (!this->write_word)
2762                 this->write_word = onenand_writew;
2763
2764         if (!this->command)
2765                 this->command = onenand_command;
2766         if (!this->wait)
2767                 this->wait = onenand_wait;
2768         if (!this->bbt_wait)
2769                 this->bbt_wait = onenand_bbt_wait;
2770         if (!this->unlock_all)
2771                 this->unlock_all = onenand_unlock_all;
2772
2773         if (!this->read_bufferram)
2774                 this->read_bufferram = onenand_read_bufferram;
2775         if (!this->write_bufferram)
2776                 this->write_bufferram = onenand_write_bufferram;
2777
2778         if (!this->block_markbad)
2779                 this->block_markbad = onenand_default_block_markbad;
2780         if (!this->scan_bbt)
2781                 this->scan_bbt = onenand_default_bbt;
2782
2783         if (onenand_probe(mtd))
2784                 return -ENXIO;
2785
2786         /* Allocate buffers, if necessary */
2787         if (!this->page_buf) {
2788                 this->page_buf = kzalloc(mtd->writesize, GFP_KERNEL);
2789                 if (!this->page_buf) {
2790                         printk(KERN_ERR "onenand_scan(): Can't allocate page_buf\n");
2791                         return -ENOMEM;
2792                 }
2793 #ifdef CONFIG_MTD_ONENAND_VERIFY_WRITE
2794                 this->verify_buf = kzalloc(mtd->writesize, GFP_KERNEL);
2795                 if (!this->verify_buf) {
2796                         kfree(this->page_buf);
2797                         return -ENOMEM;
2798                 }
2799 #endif
2800                 this->options |= ONENAND_PAGEBUF_ALLOC;
2801         }
2802         if (!this->oob_buf) {
2803                 this->oob_buf = kzalloc(mtd->oobsize, GFP_KERNEL);
2804                 if (!this->oob_buf) {
2805                         printk(KERN_ERR "onenand_scan: Can't allocate oob_buf\n");
2806                         if (this->options & ONENAND_PAGEBUF_ALLOC) {
2807                                 this->options &= ~ONENAND_PAGEBUF_ALLOC;
2808                                 kfree(this->page_buf);
2809                         }
2810                         return -ENOMEM;
2811                 }
2812                 this->options |= ONENAND_OOBBUF_ALLOC;
2813         }
2814
2815         this->state = FL_READY;
2816
2817         /*
2818          * Allow subpage writes up to oobsize.
2819          */
2820         switch (mtd->oobsize) {
2821         case 128:
2822                 this->ecclayout = &onenand_oob_128;
2823                 mtd->subpage_sft = 0;
2824                 break;
2825
2826         case 64:
2827                 this->ecclayout = &onenand_oob_64;
2828                 mtd->subpage_sft = 2;
2829                 break;
2830
2831         case 32:
2832                 this->ecclayout = &onenand_oob_32;
2833                 mtd->subpage_sft = 1;
2834                 break;
2835
2836         default:
2837                 printk(KERN_WARNING "No OOB scheme defined for oobsize %d\n",
2838                         mtd->oobsize);
2839                 mtd->subpage_sft = 0;
2840                 /* To prevent kernel oops */
2841                 this->ecclayout = &onenand_oob_32;
2842                 break;
2843         }
2844
2845         this->subpagesize = mtd->writesize >> mtd->subpage_sft;
2846
2847         /*
2848          * The number of bytes available for a client to place data into
2849          * the out of band area
2850          */
2851         this->ecclayout->oobavail = 0;
2852         for (i = 0; i < MTD_MAX_OOBFREE_ENTRIES &&
2853             this->ecclayout->oobfree[i].length; i++)
2854                 this->ecclayout->oobavail +=
2855                         this->ecclayout->oobfree[i].length;
2856         mtd->oobavail = this->ecclayout->oobavail;
2857
2858         mtd->ecclayout = this->ecclayout;
2859
2860         /* Unlock whole block */
2861         this->unlock_all(mtd);
2862
2863         return this->scan_bbt(mtd);
2864 }
2865
2866 /**
2867  * onenand_release - [OneNAND Interface] Free resources held by the OneNAND device
2868  * @param mtd           MTD device structure
2869  */
2870 void onenand_release(struct mtd_info *mtd)
2871 {
2872 }