Merge branch 'core-rcu-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git...
[sfrench/cifs-2.6.git] / drivers / mtd / onenand / onenand_base.c
1 /*
2  *  linux/drivers/mtd/onenand/onenand_base.c
3  *
4  *  Copyright © 2005-2009 Samsung Electronics
5  *  Copyright © 2007 Nokia Corporation
6  *
7  *  Kyungmin Park <kyungmin.park@samsung.com>
8  *
9  *  Credits:
10  *      Adrian Hunter <ext-adrian.hunter@nokia.com>:
11  *      auto-placement support, read-while load support, various fixes
12  *
13  *      Vishak G <vishak.g at samsung.com>, Rohit Hagargundgi <h.rohit at samsung.com>
14  *      Flex-OneNAND support
15  *      Amul Kumar Saha <amul.saha at samsung.com>
16  *      OTP support
17  *
18  * This program is free software; you can redistribute it and/or modify
19  * it under the terms of the GNU General Public License version 2 as
20  * published by the Free Software Foundation.
21  */
22
23 #include <linux/kernel.h>
24 #include <linux/module.h>
25 #include <linux/moduleparam.h>
26 #include <linux/slab.h>
27 #include <linux/sched.h>
28 #include <linux/delay.h>
29 #include <linux/interrupt.h>
30 #include <linux/jiffies.h>
31 #include <linux/mtd/mtd.h>
32 #include <linux/mtd/onenand.h>
33 #include <linux/mtd/partitions.h>
34
35 #include <asm/io.h>
36
37 /*
38  * Multiblock erase if number of blocks to erase is 2 or more.
39  * Maximum number of blocks for simultaneous erase is 64.
40  */
41 #define MB_ERASE_MIN_BLK_COUNT 2
42 #define MB_ERASE_MAX_BLK_COUNT 64
43
44 /* Default Flex-OneNAND boundary and lock respectively */
45 static int flex_bdry[MAX_DIES * 2] = { -1, 0, -1, 0 };
46
47 module_param_array(flex_bdry, int, NULL, 0400);
48 MODULE_PARM_DESC(flex_bdry,     "SLC Boundary information for Flex-OneNAND"
49                                 "Syntax:flex_bdry=DIE_BDRY,LOCK,..."
50                                 "DIE_BDRY: SLC boundary of the die"
51                                 "LOCK: Locking information for SLC boundary"
52                                 "    : 0->Set boundary in unlocked status"
53                                 "    : 1->Set boundary in locked status");
54
55 /* Default OneNAND/Flex-OneNAND OTP options*/
56 static int otp;
57
58 module_param(otp, int, 0400);
59 MODULE_PARM_DESC(otp,   "Corresponding behaviour of OneNAND in OTP"
60                         "Syntax : otp=LOCK_TYPE"
61                         "LOCK_TYPE : Keys issued, for specific OTP Lock type"
62                         "          : 0 -> Default (No Blocks Locked)"
63                         "          : 1 -> OTP Block lock"
64                         "          : 2 -> 1st Block lock"
65                         "          : 3 -> BOTH OTP Block and 1st Block lock");
66
67 /*
68  * flexonenand_oob_128 - oob info for Flex-Onenand with 4KB page
69  * For now, we expose only 64 out of 80 ecc bytes
70  */
71 static int flexonenand_ooblayout_ecc(struct mtd_info *mtd, int section,
72                                      struct mtd_oob_region *oobregion)
73 {
74         if (section > 7)
75                 return -ERANGE;
76
77         oobregion->offset = (section * 16) + 6;
78         oobregion->length = 10;
79
80         return 0;
81 }
82
83 static int flexonenand_ooblayout_free(struct mtd_info *mtd, int section,
84                                       struct mtd_oob_region *oobregion)
85 {
86         if (section > 7)
87                 return -ERANGE;
88
89         oobregion->offset = (section * 16) + 2;
90         oobregion->length = 4;
91
92         return 0;
93 }
94
95 static const struct mtd_ooblayout_ops flexonenand_ooblayout_ops = {
96         .ecc = flexonenand_ooblayout_ecc,
97         .free = flexonenand_ooblayout_free,
98 };
99
100 /*
101  * onenand_oob_128 - oob info for OneNAND with 4KB page
102  *
103  * Based on specification:
104  * 4Gb M-die OneNAND Flash (KFM4G16Q4M, KFN8G16Q4M). Rev. 1.3, Apr. 2010
105  *
106  */
107 static int onenand_ooblayout_128_ecc(struct mtd_info *mtd, int section,
108                                      struct mtd_oob_region *oobregion)
109 {
110         if (section > 7)
111                 return -ERANGE;
112
113         oobregion->offset = (section * 16) + 7;
114         oobregion->length = 9;
115
116         return 0;
117 }
118
119 static int onenand_ooblayout_128_free(struct mtd_info *mtd, int section,
120                                       struct mtd_oob_region *oobregion)
121 {
122         if (section >= 8)
123                 return -ERANGE;
124
125         /*
126          * free bytes are using the spare area fields marked as
127          * "Managed by internal ECC logic for Logical Sector Number area"
128          */
129         oobregion->offset = (section * 16) + 2;
130         oobregion->length = 3;
131
132         return 0;
133 }
134
135 static const struct mtd_ooblayout_ops onenand_oob_128_ooblayout_ops = {
136         .ecc = onenand_ooblayout_128_ecc,
137         .free = onenand_ooblayout_128_free,
138 };
139
140 /**
141  * onenand_oob_32_64 - oob info for large (2KB) page
142  */
143 static int onenand_ooblayout_32_64_ecc(struct mtd_info *mtd, int section,
144                                        struct mtd_oob_region *oobregion)
145 {
146         if (section > 3)
147                 return -ERANGE;
148
149         oobregion->offset = (section * 16) + 8;
150         oobregion->length = 5;
151
152         return 0;
153 }
154
155 static int onenand_ooblayout_32_64_free(struct mtd_info *mtd, int section,
156                                         struct mtd_oob_region *oobregion)
157 {
158         int sections = (mtd->oobsize / 32) * 2;
159
160         if (section >= sections)
161                 return -ERANGE;
162
163         if (section & 1) {
164                 oobregion->offset = ((section - 1) * 16) + 14;
165                 oobregion->length = 2;
166         } else  {
167                 oobregion->offset = (section * 16) + 2;
168                 oobregion->length = 3;
169         }
170
171         return 0;
172 }
173
174 static const struct mtd_ooblayout_ops onenand_oob_32_64_ooblayout_ops = {
175         .ecc = onenand_ooblayout_32_64_ecc,
176         .free = onenand_ooblayout_32_64_free,
177 };
178
179 static const unsigned char ffchars[] = {
180         0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
181         0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 16 */
182         0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
183         0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 32 */
184         0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
185         0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 48 */
186         0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
187         0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 64 */
188         0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
189         0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 80 */
190         0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
191         0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 96 */
192         0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
193         0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 112 */
194         0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
195         0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 128 */
196 };
197
198 /**
199  * onenand_readw - [OneNAND Interface] Read OneNAND register
200  * @param addr          address to read
201  *
202  * Read OneNAND register
203  */
204 static unsigned short onenand_readw(void __iomem *addr)
205 {
206         return readw(addr);
207 }
208
209 /**
210  * onenand_writew - [OneNAND Interface] Write OneNAND register with value
211  * @param value         value to write
212  * @param addr          address to write
213  *
214  * Write OneNAND register with value
215  */
216 static void onenand_writew(unsigned short value, void __iomem *addr)
217 {
218         writew(value, addr);
219 }
220
221 /**
222  * onenand_block_address - [DEFAULT] Get block address
223  * @param this          onenand chip data structure
224  * @param block         the block
225  * @return              translated block address if DDP, otherwise same
226  *
227  * Setup Start Address 1 Register (F100h)
228  */
229 static int onenand_block_address(struct onenand_chip *this, int block)
230 {
231         /* Device Flash Core select, NAND Flash Block Address */
232         if (block & this->density_mask)
233                 return ONENAND_DDP_CHIP1 | (block ^ this->density_mask);
234
235         return block;
236 }
237
238 /**
239  * onenand_bufferram_address - [DEFAULT] Get bufferram address
240  * @param this          onenand chip data structure
241  * @param block         the block
242  * @return              set DBS value if DDP, otherwise 0
243  *
244  * Setup Start Address 2 Register (F101h) for DDP
245  */
246 static int onenand_bufferram_address(struct onenand_chip *this, int block)
247 {
248         /* Device BufferRAM Select */
249         if (block & this->density_mask)
250                 return ONENAND_DDP_CHIP1;
251
252         return ONENAND_DDP_CHIP0;
253 }
254
255 /**
256  * onenand_page_address - [DEFAULT] Get page address
257  * @param page          the page address
258  * @param sector        the sector address
259  * @return              combined page and sector address
260  *
261  * Setup Start Address 8 Register (F107h)
262  */
263 static int onenand_page_address(int page, int sector)
264 {
265         /* Flash Page Address, Flash Sector Address */
266         int fpa, fsa;
267
268         fpa = page & ONENAND_FPA_MASK;
269         fsa = sector & ONENAND_FSA_MASK;
270
271         return ((fpa << ONENAND_FPA_SHIFT) | fsa);
272 }
273
274 /**
275  * onenand_buffer_address - [DEFAULT] Get buffer address
276  * @param dataram1      DataRAM index
277  * @param sectors       the sector address
278  * @param count         the number of sectors
279  * @return              the start buffer value
280  *
281  * Setup Start Buffer Register (F200h)
282  */
283 static int onenand_buffer_address(int dataram1, int sectors, int count)
284 {
285         int bsa, bsc;
286
287         /* BufferRAM Sector Address */
288         bsa = sectors & ONENAND_BSA_MASK;
289
290         if (dataram1)
291                 bsa |= ONENAND_BSA_DATARAM1;    /* DataRAM1 */
292         else
293                 bsa |= ONENAND_BSA_DATARAM0;    /* DataRAM0 */
294
295         /* BufferRAM Sector Count */
296         bsc = count & ONENAND_BSC_MASK;
297
298         return ((bsa << ONENAND_BSA_SHIFT) | bsc);
299 }
300
301 /**
302  * flexonenand_block- For given address return block number
303  * @param this         - OneNAND device structure
304  * @param addr          - Address for which block number is needed
305  */
306 static unsigned flexonenand_block(struct onenand_chip *this, loff_t addr)
307 {
308         unsigned boundary, blk, die = 0;
309
310         if (ONENAND_IS_DDP(this) && addr >= this->diesize[0]) {
311                 die = 1;
312                 addr -= this->diesize[0];
313         }
314
315         boundary = this->boundary[die];
316
317         blk = addr >> (this->erase_shift - 1);
318         if (blk > boundary)
319                 blk = (blk + boundary + 1) >> 1;
320
321         blk += die ? this->density_mask : 0;
322         return blk;
323 }
324
325 inline unsigned onenand_block(struct onenand_chip *this, loff_t addr)
326 {
327         if (!FLEXONENAND(this))
328                 return addr >> this->erase_shift;
329         return flexonenand_block(this, addr);
330 }
331
332 /**
333  * flexonenand_addr - Return address of the block
334  * @this:               OneNAND device structure
335  * @block:              Block number on Flex-OneNAND
336  *
337  * Return address of the block
338  */
339 static loff_t flexonenand_addr(struct onenand_chip *this, int block)
340 {
341         loff_t ofs = 0;
342         int die = 0, boundary;
343
344         if (ONENAND_IS_DDP(this) && block >= this->density_mask) {
345                 block -= this->density_mask;
346                 die = 1;
347                 ofs = this->diesize[0];
348         }
349
350         boundary = this->boundary[die];
351         ofs += (loff_t)block << (this->erase_shift - 1);
352         if (block > (boundary + 1))
353                 ofs += (loff_t)(block - boundary - 1) << (this->erase_shift - 1);
354         return ofs;
355 }
356
357 loff_t onenand_addr(struct onenand_chip *this, int block)
358 {
359         if (!FLEXONENAND(this))
360                 return (loff_t)block << this->erase_shift;
361         return flexonenand_addr(this, block);
362 }
363 EXPORT_SYMBOL(onenand_addr);
364
365 /**
366  * onenand_get_density - [DEFAULT] Get OneNAND density
367  * @param dev_id        OneNAND device ID
368  *
369  * Get OneNAND density from device ID
370  */
371 static inline int onenand_get_density(int dev_id)
372 {
373         int density = dev_id >> ONENAND_DEVICE_DENSITY_SHIFT;
374         return (density & ONENAND_DEVICE_DENSITY_MASK);
375 }
376
377 /**
378  * flexonenand_region - [Flex-OneNAND] Return erase region of addr
379  * @param mtd           MTD device structure
380  * @param addr          address whose erase region needs to be identified
381  */
382 int flexonenand_region(struct mtd_info *mtd, loff_t addr)
383 {
384         int i;
385
386         for (i = 0; i < mtd->numeraseregions; i++)
387                 if (addr < mtd->eraseregions[i].offset)
388                         break;
389         return i - 1;
390 }
391 EXPORT_SYMBOL(flexonenand_region);
392
393 /**
394  * onenand_command - [DEFAULT] Send command to OneNAND device
395  * @param mtd           MTD device structure
396  * @param cmd           the command to be sent
397  * @param addr          offset to read from or write to
398  * @param len           number of bytes to read or write
399  *
400  * Send command to OneNAND device. This function is used for middle/large page
401  * devices (1KB/2KB Bytes per page)
402  */
403 static int onenand_command(struct mtd_info *mtd, int cmd, loff_t addr, size_t len)
404 {
405         struct onenand_chip *this = mtd->priv;
406         int value, block, page;
407
408         /* Address translation */
409         switch (cmd) {
410         case ONENAND_CMD_UNLOCK:
411         case ONENAND_CMD_LOCK:
412         case ONENAND_CMD_LOCK_TIGHT:
413         case ONENAND_CMD_UNLOCK_ALL:
414                 block = -1;
415                 page = -1;
416                 break;
417
418         case FLEXONENAND_CMD_PI_ACCESS:
419                 /* addr contains die index */
420                 block = addr * this->density_mask;
421                 page = -1;
422                 break;
423
424         case ONENAND_CMD_ERASE:
425         case ONENAND_CMD_MULTIBLOCK_ERASE:
426         case ONENAND_CMD_ERASE_VERIFY:
427         case ONENAND_CMD_BUFFERRAM:
428         case ONENAND_CMD_OTP_ACCESS:
429                 block = onenand_block(this, addr);
430                 page = -1;
431                 break;
432
433         case FLEXONENAND_CMD_READ_PI:
434                 cmd = ONENAND_CMD_READ;
435                 block = addr * this->density_mask;
436                 page = 0;
437                 break;
438
439         default:
440                 block = onenand_block(this, addr);
441                 if (FLEXONENAND(this))
442                         page = (int) (addr - onenand_addr(this, block))>>\
443                                 this->page_shift;
444                 else
445                         page = (int) (addr >> this->page_shift);
446                 if (ONENAND_IS_2PLANE(this)) {
447                         /* Make the even block number */
448                         block &= ~1;
449                         /* Is it the odd plane? */
450                         if (addr & this->writesize)
451                                 block++;
452                         page >>= 1;
453                 }
454                 page &= this->page_mask;
455                 break;
456         }
457
458         /* NOTE: The setting order of the registers is very important! */
459         if (cmd == ONENAND_CMD_BUFFERRAM) {
460                 /* Select DataRAM for DDP */
461                 value = onenand_bufferram_address(this, block);
462                 this->write_word(value, this->base + ONENAND_REG_START_ADDRESS2);
463
464                 if (ONENAND_IS_2PLANE(this) || ONENAND_IS_4KB_PAGE(this))
465                         /* It is always BufferRAM0 */
466                         ONENAND_SET_BUFFERRAM0(this);
467                 else
468                         /* Switch to the next data buffer */
469                         ONENAND_SET_NEXT_BUFFERRAM(this);
470
471                 return 0;
472         }
473
474         if (block != -1) {
475                 /* Write 'DFS, FBA' of Flash */
476                 value = onenand_block_address(this, block);
477                 this->write_word(value, this->base + ONENAND_REG_START_ADDRESS1);
478
479                 /* Select DataRAM for DDP */
480                 value = onenand_bufferram_address(this, block);
481                 this->write_word(value, this->base + ONENAND_REG_START_ADDRESS2);
482         }
483
484         if (page != -1) {
485                 /* Now we use page size operation */
486                 int sectors = 0, count = 0;
487                 int dataram;
488
489                 switch (cmd) {
490                 case FLEXONENAND_CMD_RECOVER_LSB:
491                 case ONENAND_CMD_READ:
492                 case ONENAND_CMD_READOOB:
493                         if (ONENAND_IS_4KB_PAGE(this))
494                                 /* It is always BufferRAM0 */
495                                 dataram = ONENAND_SET_BUFFERRAM0(this);
496                         else
497                                 dataram = ONENAND_SET_NEXT_BUFFERRAM(this);
498                         break;
499
500                 default:
501                         if (ONENAND_IS_2PLANE(this) && cmd == ONENAND_CMD_PROG)
502                                 cmd = ONENAND_CMD_2X_PROG;
503                         dataram = ONENAND_CURRENT_BUFFERRAM(this);
504                         break;
505                 }
506
507                 /* Write 'FPA, FSA' of Flash */
508                 value = onenand_page_address(page, sectors);
509                 this->write_word(value, this->base + ONENAND_REG_START_ADDRESS8);
510
511                 /* Write 'BSA, BSC' of DataRAM */
512                 value = onenand_buffer_address(dataram, sectors, count);
513                 this->write_word(value, this->base + ONENAND_REG_START_BUFFER);
514         }
515
516         /* Interrupt clear */
517         this->write_word(ONENAND_INT_CLEAR, this->base + ONENAND_REG_INTERRUPT);
518
519         /* Write command */
520         this->write_word(cmd, this->base + ONENAND_REG_COMMAND);
521
522         return 0;
523 }
524
525 /**
526  * onenand_read_ecc - return ecc status
527  * @param this          onenand chip structure
528  */
529 static inline int onenand_read_ecc(struct onenand_chip *this)
530 {
531         int ecc, i, result = 0;
532
533         if (!FLEXONENAND(this) && !ONENAND_IS_4KB_PAGE(this))
534                 return this->read_word(this->base + ONENAND_REG_ECC_STATUS);
535
536         for (i = 0; i < 4; i++) {
537                 ecc = this->read_word(this->base + ONENAND_REG_ECC_STATUS + i*2);
538                 if (likely(!ecc))
539                         continue;
540                 if (ecc & FLEXONENAND_UNCORRECTABLE_ERROR)
541                         return ONENAND_ECC_2BIT_ALL;
542                 else
543                         result = ONENAND_ECC_1BIT_ALL;
544         }
545
546         return result;
547 }
548
549 /**
550  * onenand_wait - [DEFAULT] wait until the command is done
551  * @param mtd           MTD device structure
552  * @param state         state to select the max. timeout value
553  *
554  * Wait for command done. This applies to all OneNAND command
555  * Read can take up to 30us, erase up to 2ms and program up to 350us
556  * according to general OneNAND specs
557  */
558 static int onenand_wait(struct mtd_info *mtd, int state)
559 {
560         struct onenand_chip * this = mtd->priv;
561         unsigned long timeout;
562         unsigned int flags = ONENAND_INT_MASTER;
563         unsigned int interrupt = 0;
564         unsigned int ctrl;
565
566         /* The 20 msec is enough */
567         timeout = jiffies + msecs_to_jiffies(20);
568         while (time_before(jiffies, timeout)) {
569                 interrupt = this->read_word(this->base + ONENAND_REG_INTERRUPT);
570
571                 if (interrupt & flags)
572                         break;
573
574                 if (state != FL_READING && state != FL_PREPARING_ERASE)
575                         cond_resched();
576         }
577         /* To get correct interrupt status in timeout case */
578         interrupt = this->read_word(this->base + ONENAND_REG_INTERRUPT);
579
580         ctrl = this->read_word(this->base + ONENAND_REG_CTRL_STATUS);
581
582         /*
583          * In the Spec. it checks the controller status first
584          * However if you get the correct information in case of
585          * power off recovery (POR) test, it should read ECC status first
586          */
587         if (interrupt & ONENAND_INT_READ) {
588                 int ecc = onenand_read_ecc(this);
589                 if (ecc) {
590                         if (ecc & ONENAND_ECC_2BIT_ALL) {
591                                 printk(KERN_ERR "%s: ECC error = 0x%04x\n",
592                                         __func__, ecc);
593                                 mtd->ecc_stats.failed++;
594                                 return -EBADMSG;
595                         } else if (ecc & ONENAND_ECC_1BIT_ALL) {
596                                 printk(KERN_DEBUG "%s: correctable ECC error = 0x%04x\n",
597                                         __func__, ecc);
598                                 mtd->ecc_stats.corrected++;
599                         }
600                 }
601         } else if (state == FL_READING) {
602                 printk(KERN_ERR "%s: read timeout! ctrl=0x%04x intr=0x%04x\n",
603                         __func__, ctrl, interrupt);
604                 return -EIO;
605         }
606
607         if (state == FL_PREPARING_ERASE && !(interrupt & ONENAND_INT_ERASE)) {
608                 printk(KERN_ERR "%s: mb erase timeout! ctrl=0x%04x intr=0x%04x\n",
609                        __func__, ctrl, interrupt);
610                 return -EIO;
611         }
612
613         if (!(interrupt & ONENAND_INT_MASTER)) {
614                 printk(KERN_ERR "%s: timeout! ctrl=0x%04x intr=0x%04x\n",
615                        __func__, ctrl, interrupt);
616                 return -EIO;
617         }
618
619         /* If there's controller error, it's a real error */
620         if (ctrl & ONENAND_CTRL_ERROR) {
621                 printk(KERN_ERR "%s: controller error = 0x%04x\n",
622                         __func__, ctrl);
623                 if (ctrl & ONENAND_CTRL_LOCK)
624                         printk(KERN_ERR "%s: it's locked error.\n", __func__);
625                 return -EIO;
626         }
627
628         return 0;
629 }
630
631 /*
632  * onenand_interrupt - [DEFAULT] onenand interrupt handler
633  * @param irq           onenand interrupt number
634  * @param dev_id        interrupt data
635  *
636  * complete the work
637  */
638 static irqreturn_t onenand_interrupt(int irq, void *data)
639 {
640         struct onenand_chip *this = data;
641
642         /* To handle shared interrupt */
643         if (!this->complete.done)
644                 complete(&this->complete);
645
646         return IRQ_HANDLED;
647 }
648
649 /*
650  * onenand_interrupt_wait - [DEFAULT] wait until the command is done
651  * @param mtd           MTD device structure
652  * @param state         state to select the max. timeout value
653  *
654  * Wait for command done.
655  */
656 static int onenand_interrupt_wait(struct mtd_info *mtd, int state)
657 {
658         struct onenand_chip *this = mtd->priv;
659
660         wait_for_completion(&this->complete);
661
662         return onenand_wait(mtd, state);
663 }
664
665 /*
666  * onenand_try_interrupt_wait - [DEFAULT] try interrupt wait
667  * @param mtd           MTD device structure
668  * @param state         state to select the max. timeout value
669  *
670  * Try interrupt based wait (It is used one-time)
671  */
672 static int onenand_try_interrupt_wait(struct mtd_info *mtd, int state)
673 {
674         struct onenand_chip *this = mtd->priv;
675         unsigned long remain, timeout;
676
677         /* We use interrupt wait first */
678         this->wait = onenand_interrupt_wait;
679
680         timeout = msecs_to_jiffies(100);
681         remain = wait_for_completion_timeout(&this->complete, timeout);
682         if (!remain) {
683                 printk(KERN_INFO "OneNAND: There's no interrupt. "
684                                 "We use the normal wait\n");
685
686                 /* Release the irq */
687                 free_irq(this->irq, this);
688
689                 this->wait = onenand_wait;
690         }
691
692         return onenand_wait(mtd, state);
693 }
694
695 /*
696  * onenand_setup_wait - [OneNAND Interface] setup onenand wait method
697  * @param mtd           MTD device structure
698  *
699  * There's two method to wait onenand work
700  * 1. polling - read interrupt status register
701  * 2. interrupt - use the kernel interrupt method
702  */
703 static void onenand_setup_wait(struct mtd_info *mtd)
704 {
705         struct onenand_chip *this = mtd->priv;
706         int syscfg;
707
708         init_completion(&this->complete);
709
710         if (this->irq <= 0) {
711                 this->wait = onenand_wait;
712                 return;
713         }
714
715         if (request_irq(this->irq, &onenand_interrupt,
716                                 IRQF_SHARED, "onenand", this)) {
717                 /* If we can't get irq, use the normal wait */
718                 this->wait = onenand_wait;
719                 return;
720         }
721
722         /* Enable interrupt */
723         syscfg = this->read_word(this->base + ONENAND_REG_SYS_CFG1);
724         syscfg |= ONENAND_SYS_CFG1_IOBE;
725         this->write_word(syscfg, this->base + ONENAND_REG_SYS_CFG1);
726
727         this->wait = onenand_try_interrupt_wait;
728 }
729
730 /**
731  * onenand_bufferram_offset - [DEFAULT] BufferRAM offset
732  * @param mtd           MTD data structure
733  * @param area          BufferRAM area
734  * @return              offset given area
735  *
736  * Return BufferRAM offset given area
737  */
738 static inline int onenand_bufferram_offset(struct mtd_info *mtd, int area)
739 {
740         struct onenand_chip *this = mtd->priv;
741
742         if (ONENAND_CURRENT_BUFFERRAM(this)) {
743                 /* Note: the 'this->writesize' is a real page size */
744                 if (area == ONENAND_DATARAM)
745                         return this->writesize;
746                 if (area == ONENAND_SPARERAM)
747                         return mtd->oobsize;
748         }
749
750         return 0;
751 }
752
753 /**
754  * onenand_read_bufferram - [OneNAND Interface] Read the bufferram area
755  * @param mtd           MTD data structure
756  * @param area          BufferRAM area
757  * @param buffer        the databuffer to put/get data
758  * @param offset        offset to read from or write to
759  * @param count         number of bytes to read/write
760  *
761  * Read the BufferRAM area
762  */
763 static int onenand_read_bufferram(struct mtd_info *mtd, int area,
764                 unsigned char *buffer, int offset, size_t count)
765 {
766         struct onenand_chip *this = mtd->priv;
767         void __iomem *bufferram;
768
769         bufferram = this->base + area;
770
771         bufferram += onenand_bufferram_offset(mtd, area);
772
773         if (ONENAND_CHECK_BYTE_ACCESS(count)) {
774                 unsigned short word;
775
776                 /* Align with word(16-bit) size */
777                 count--;
778
779                 /* Read word and save byte */
780                 word = this->read_word(bufferram + offset + count);
781                 buffer[count] = (word & 0xff);
782         }
783
784         memcpy(buffer, bufferram + offset, count);
785
786         return 0;
787 }
788
789 /**
790  * onenand_sync_read_bufferram - [OneNAND Interface] Read the bufferram area with Sync. Burst mode
791  * @param mtd           MTD data structure
792  * @param area          BufferRAM area
793  * @param buffer        the databuffer to put/get data
794  * @param offset        offset to read from or write to
795  * @param count         number of bytes to read/write
796  *
797  * Read the BufferRAM area with Sync. Burst Mode
798  */
799 static int onenand_sync_read_bufferram(struct mtd_info *mtd, int area,
800                 unsigned char *buffer, int offset, size_t count)
801 {
802         struct onenand_chip *this = mtd->priv;
803         void __iomem *bufferram;
804
805         bufferram = this->base + area;
806
807         bufferram += onenand_bufferram_offset(mtd, area);
808
809         this->mmcontrol(mtd, ONENAND_SYS_CFG1_SYNC_READ);
810
811         if (ONENAND_CHECK_BYTE_ACCESS(count)) {
812                 unsigned short word;
813
814                 /* Align with word(16-bit) size */
815                 count--;
816
817                 /* Read word and save byte */
818                 word = this->read_word(bufferram + offset + count);
819                 buffer[count] = (word & 0xff);
820         }
821
822         memcpy(buffer, bufferram + offset, count);
823
824         this->mmcontrol(mtd, 0);
825
826         return 0;
827 }
828
829 /**
830  * onenand_write_bufferram - [OneNAND Interface] Write the bufferram area
831  * @param mtd           MTD data structure
832  * @param area          BufferRAM area
833  * @param buffer        the databuffer to put/get data
834  * @param offset        offset to read from or write to
835  * @param count         number of bytes to read/write
836  *
837  * Write the BufferRAM area
838  */
839 static int onenand_write_bufferram(struct mtd_info *mtd, int area,
840                 const unsigned char *buffer, int offset, size_t count)
841 {
842         struct onenand_chip *this = mtd->priv;
843         void __iomem *bufferram;
844
845         bufferram = this->base + area;
846
847         bufferram += onenand_bufferram_offset(mtd, area);
848
849         if (ONENAND_CHECK_BYTE_ACCESS(count)) {
850                 unsigned short word;
851                 int byte_offset;
852
853                 /* Align with word(16-bit) size */
854                 count--;
855
856                 /* Calculate byte access offset */
857                 byte_offset = offset + count;
858
859                 /* Read word and save byte */
860                 word = this->read_word(bufferram + byte_offset);
861                 word = (word & ~0xff) | buffer[count];
862                 this->write_word(word, bufferram + byte_offset);
863         }
864
865         memcpy(bufferram + offset, buffer, count);
866
867         return 0;
868 }
869
870 /**
871  * onenand_get_2x_blockpage - [GENERIC] Get blockpage at 2x program mode
872  * @param mtd           MTD data structure
873  * @param addr          address to check
874  * @return              blockpage address
875  *
876  * Get blockpage address at 2x program mode
877  */
878 static int onenand_get_2x_blockpage(struct mtd_info *mtd, loff_t addr)
879 {
880         struct onenand_chip *this = mtd->priv;
881         int blockpage, block, page;
882
883         /* Calculate the even block number */
884         block = (int) (addr >> this->erase_shift) & ~1;
885         /* Is it the odd plane? */
886         if (addr & this->writesize)
887                 block++;
888         page = (int) (addr >> (this->page_shift + 1)) & this->page_mask;
889         blockpage = (block << 7) | page;
890
891         return blockpage;
892 }
893
894 /**
895  * onenand_check_bufferram - [GENERIC] Check BufferRAM information
896  * @param mtd           MTD data structure
897  * @param addr          address to check
898  * @return              1 if there are valid data, otherwise 0
899  *
900  * Check bufferram if there is data we required
901  */
902 static int onenand_check_bufferram(struct mtd_info *mtd, loff_t addr)
903 {
904         struct onenand_chip *this = mtd->priv;
905         int blockpage, found = 0;
906         unsigned int i;
907
908         if (ONENAND_IS_2PLANE(this))
909                 blockpage = onenand_get_2x_blockpage(mtd, addr);
910         else
911                 blockpage = (int) (addr >> this->page_shift);
912
913         /* Is there valid data? */
914         i = ONENAND_CURRENT_BUFFERRAM(this);
915         if (this->bufferram[i].blockpage == blockpage)
916                 found = 1;
917         else {
918                 /* Check another BufferRAM */
919                 i = ONENAND_NEXT_BUFFERRAM(this);
920                 if (this->bufferram[i].blockpage == blockpage) {
921                         ONENAND_SET_NEXT_BUFFERRAM(this);
922                         found = 1;
923                 }
924         }
925
926         if (found && ONENAND_IS_DDP(this)) {
927                 /* Select DataRAM for DDP */
928                 int block = onenand_block(this, addr);
929                 int value = onenand_bufferram_address(this, block);
930                 this->write_word(value, this->base + ONENAND_REG_START_ADDRESS2);
931         }
932
933         return found;
934 }
935
936 /**
937  * onenand_update_bufferram - [GENERIC] Update BufferRAM information
938  * @param mtd           MTD data structure
939  * @param addr          address to update
940  * @param valid         valid flag
941  *
942  * Update BufferRAM information
943  */
944 static void onenand_update_bufferram(struct mtd_info *mtd, loff_t addr,
945                 int valid)
946 {
947         struct onenand_chip *this = mtd->priv;
948         int blockpage;
949         unsigned int i;
950
951         if (ONENAND_IS_2PLANE(this))
952                 blockpage = onenand_get_2x_blockpage(mtd, addr);
953         else
954                 blockpage = (int) (addr >> this->page_shift);
955
956         /* Invalidate another BufferRAM */
957         i = ONENAND_NEXT_BUFFERRAM(this);
958         if (this->bufferram[i].blockpage == blockpage)
959                 this->bufferram[i].blockpage = -1;
960
961         /* Update BufferRAM */
962         i = ONENAND_CURRENT_BUFFERRAM(this);
963         if (valid)
964                 this->bufferram[i].blockpage = blockpage;
965         else
966                 this->bufferram[i].blockpage = -1;
967 }
968
969 /**
970  * onenand_invalidate_bufferram - [GENERIC] Invalidate BufferRAM information
971  * @param mtd           MTD data structure
972  * @param addr          start address to invalidate
973  * @param len           length to invalidate
974  *
975  * Invalidate BufferRAM information
976  */
977 static void onenand_invalidate_bufferram(struct mtd_info *mtd, loff_t addr,
978                 unsigned int len)
979 {
980         struct onenand_chip *this = mtd->priv;
981         int i;
982         loff_t end_addr = addr + len;
983
984         /* Invalidate BufferRAM */
985         for (i = 0; i < MAX_BUFFERRAM; i++) {
986                 loff_t buf_addr = this->bufferram[i].blockpage << this->page_shift;
987                 if (buf_addr >= addr && buf_addr < end_addr)
988                         this->bufferram[i].blockpage = -1;
989         }
990 }
991
992 /**
993  * onenand_get_device - [GENERIC] Get chip for selected access
994  * @param mtd           MTD device structure
995  * @param new_state     the state which is requested
996  *
997  * Get the device and lock it for exclusive access
998  */
999 static int onenand_get_device(struct mtd_info *mtd, int new_state)
1000 {
1001         struct onenand_chip *this = mtd->priv;
1002         DECLARE_WAITQUEUE(wait, current);
1003
1004         /*
1005          * Grab the lock and see if the device is available
1006          */
1007         while (1) {
1008                 spin_lock(&this->chip_lock);
1009                 if (this->state == FL_READY) {
1010                         this->state = new_state;
1011                         spin_unlock(&this->chip_lock);
1012                         if (new_state != FL_PM_SUSPENDED && this->enable)
1013                                 this->enable(mtd);
1014                         break;
1015                 }
1016                 if (new_state == FL_PM_SUSPENDED) {
1017                         spin_unlock(&this->chip_lock);
1018                         return (this->state == FL_PM_SUSPENDED) ? 0 : -EAGAIN;
1019                 }
1020                 set_current_state(TASK_UNINTERRUPTIBLE);
1021                 add_wait_queue(&this->wq, &wait);
1022                 spin_unlock(&this->chip_lock);
1023                 schedule();
1024                 remove_wait_queue(&this->wq, &wait);
1025         }
1026
1027         return 0;
1028 }
1029
1030 /**
1031  * onenand_release_device - [GENERIC] release chip
1032  * @param mtd           MTD device structure
1033  *
1034  * Deselect, release chip lock and wake up anyone waiting on the device
1035  */
1036 static void onenand_release_device(struct mtd_info *mtd)
1037 {
1038         struct onenand_chip *this = mtd->priv;
1039
1040         if (this->state != FL_PM_SUSPENDED && this->disable)
1041                 this->disable(mtd);
1042         /* Release the chip */
1043         spin_lock(&this->chip_lock);
1044         this->state = FL_READY;
1045         wake_up(&this->wq);
1046         spin_unlock(&this->chip_lock);
1047 }
1048
1049 /**
1050  * onenand_transfer_auto_oob - [INTERN] oob auto-placement transfer
1051  * @param mtd           MTD device structure
1052  * @param buf           destination address
1053  * @param column        oob offset to read from
1054  * @param thislen       oob length to read
1055  */
1056 static int onenand_transfer_auto_oob(struct mtd_info *mtd, uint8_t *buf, int column,
1057                                 int thislen)
1058 {
1059         struct onenand_chip *this = mtd->priv;
1060         int ret;
1061
1062         this->read_bufferram(mtd, ONENAND_SPARERAM, this->oob_buf, 0,
1063                              mtd->oobsize);
1064         ret = mtd_ooblayout_get_databytes(mtd, buf, this->oob_buf,
1065                                           column, thislen);
1066         if (ret)
1067                 return ret;
1068
1069         return 0;
1070 }
1071
1072 /**
1073  * onenand_recover_lsb - [Flex-OneNAND] Recover LSB page data
1074  * @param mtd           MTD device structure
1075  * @param addr          address to recover
1076  * @param status        return value from onenand_wait / onenand_bbt_wait
1077  *
1078  * MLC NAND Flash cell has paired pages - LSB page and MSB page. LSB page has
1079  * lower page address and MSB page has higher page address in paired pages.
1080  * If power off occurs during MSB page program, the paired LSB page data can
1081  * become corrupt. LSB page recovery read is a way to read LSB page though page
1082  * data are corrupted. When uncorrectable error occurs as a result of LSB page
1083  * read after power up, issue LSB page recovery read.
1084  */
1085 static int onenand_recover_lsb(struct mtd_info *mtd, loff_t addr, int status)
1086 {
1087         struct onenand_chip *this = mtd->priv;
1088         int i;
1089
1090         /* Recovery is only for Flex-OneNAND */
1091         if (!FLEXONENAND(this))
1092                 return status;
1093
1094         /* check if we failed due to uncorrectable error */
1095         if (!mtd_is_eccerr(status) && status != ONENAND_BBT_READ_ECC_ERROR)
1096                 return status;
1097
1098         /* check if address lies in MLC region */
1099         i = flexonenand_region(mtd, addr);
1100         if (mtd->eraseregions[i].erasesize < (1 << this->erase_shift))
1101                 return status;
1102
1103         /* We are attempting to reread, so decrement stats.failed
1104          * which was incremented by onenand_wait due to read failure
1105          */
1106         printk(KERN_INFO "%s: Attempting to recover from uncorrectable read\n",
1107                 __func__);
1108         mtd->ecc_stats.failed--;
1109
1110         /* Issue the LSB page recovery command */
1111         this->command(mtd, FLEXONENAND_CMD_RECOVER_LSB, addr, this->writesize);
1112         return this->wait(mtd, FL_READING);
1113 }
1114
1115 /**
1116  * onenand_mlc_read_ops_nolock - MLC OneNAND read main and/or out-of-band
1117  * @param mtd           MTD device structure
1118  * @param from          offset to read from
1119  * @param ops:          oob operation description structure
1120  *
1121  * MLC OneNAND / Flex-OneNAND has 4KB page size and 4KB dataram.
1122  * So, read-while-load is not present.
1123  */
1124 static int onenand_mlc_read_ops_nolock(struct mtd_info *mtd, loff_t from,
1125                                 struct mtd_oob_ops *ops)
1126 {
1127         struct onenand_chip *this = mtd->priv;
1128         struct mtd_ecc_stats stats;
1129         size_t len = ops->len;
1130         size_t ooblen = ops->ooblen;
1131         u_char *buf = ops->datbuf;
1132         u_char *oobbuf = ops->oobbuf;
1133         int read = 0, column, thislen;
1134         int oobread = 0, oobcolumn, thisooblen, oobsize;
1135         int ret = 0;
1136         int writesize = this->writesize;
1137
1138         pr_debug("%s: from = 0x%08x, len = %i\n", __func__, (unsigned int)from,
1139                         (int)len);
1140
1141         oobsize = mtd_oobavail(mtd, ops);
1142         oobcolumn = from & (mtd->oobsize - 1);
1143
1144         /* Do not allow reads past end of device */
1145         if (from + len > mtd->size) {
1146                 printk(KERN_ERR "%s: Attempt read beyond end of device\n",
1147                         __func__);
1148                 ops->retlen = 0;
1149                 ops->oobretlen = 0;
1150                 return -EINVAL;
1151         }
1152
1153         stats = mtd->ecc_stats;
1154
1155         while (read < len) {
1156                 cond_resched();
1157
1158                 thislen = min_t(int, writesize, len - read);
1159
1160                 column = from & (writesize - 1);
1161                 if (column + thislen > writesize)
1162                         thislen = writesize - column;
1163
1164                 if (!onenand_check_bufferram(mtd, from)) {
1165                         this->command(mtd, ONENAND_CMD_READ, from, writesize);
1166
1167                         ret = this->wait(mtd, FL_READING);
1168                         if (unlikely(ret))
1169                                 ret = onenand_recover_lsb(mtd, from, ret);
1170                         onenand_update_bufferram(mtd, from, !ret);
1171                         if (mtd_is_eccerr(ret))
1172                                 ret = 0;
1173                         if (ret)
1174                                 break;
1175                 }
1176
1177                 this->read_bufferram(mtd, ONENAND_DATARAM, buf, column, thislen);
1178                 if (oobbuf) {
1179                         thisooblen = oobsize - oobcolumn;
1180                         thisooblen = min_t(int, thisooblen, ooblen - oobread);
1181
1182                         if (ops->mode == MTD_OPS_AUTO_OOB)
1183                                 onenand_transfer_auto_oob(mtd, oobbuf, oobcolumn, thisooblen);
1184                         else
1185                                 this->read_bufferram(mtd, ONENAND_SPARERAM, oobbuf, oobcolumn, thisooblen);
1186                         oobread += thisooblen;
1187                         oobbuf += thisooblen;
1188                         oobcolumn = 0;
1189                 }
1190
1191                 read += thislen;
1192                 if (read == len)
1193                         break;
1194
1195                 from += thislen;
1196                 buf += thislen;
1197         }
1198
1199         /*
1200          * Return success, if no ECC failures, else -EBADMSG
1201          * fs driver will take care of that, because
1202          * retlen == desired len and result == -EBADMSG
1203          */
1204         ops->retlen = read;
1205         ops->oobretlen = oobread;
1206
1207         if (ret)
1208                 return ret;
1209
1210         if (mtd->ecc_stats.failed - stats.failed)
1211                 return -EBADMSG;
1212
1213         /* return max bitflips per ecc step; ONENANDs correct 1 bit only */
1214         return mtd->ecc_stats.corrected != stats.corrected ? 1 : 0;
1215 }
1216
1217 /**
1218  * onenand_read_ops_nolock - [OneNAND Interface] OneNAND read main and/or out-of-band
1219  * @param mtd           MTD device structure
1220  * @param from          offset to read from
1221  * @param ops:          oob operation description structure
1222  *
1223  * OneNAND read main and/or out-of-band data
1224  */
1225 static int onenand_read_ops_nolock(struct mtd_info *mtd, loff_t from,
1226                                 struct mtd_oob_ops *ops)
1227 {
1228         struct onenand_chip *this = mtd->priv;
1229         struct mtd_ecc_stats stats;
1230         size_t len = ops->len;
1231         size_t ooblen = ops->ooblen;
1232         u_char *buf = ops->datbuf;
1233         u_char *oobbuf = ops->oobbuf;
1234         int read = 0, column, thislen;
1235         int oobread = 0, oobcolumn, thisooblen, oobsize;
1236         int ret = 0, boundary = 0;
1237         int writesize = this->writesize;
1238
1239         pr_debug("%s: from = 0x%08x, len = %i\n", __func__, (unsigned int)from,
1240                         (int)len);
1241
1242         oobsize = mtd_oobavail(mtd, ops);
1243         oobcolumn = from & (mtd->oobsize - 1);
1244
1245         /* Do not allow reads past end of device */
1246         if ((from + len) > mtd->size) {
1247                 printk(KERN_ERR "%s: Attempt read beyond end of device\n",
1248                         __func__);
1249                 ops->retlen = 0;
1250                 ops->oobretlen = 0;
1251                 return -EINVAL;
1252         }
1253
1254         stats = mtd->ecc_stats;
1255
1256         /* Read-while-load method */
1257
1258         /* Do first load to bufferRAM */
1259         if (read < len) {
1260                 if (!onenand_check_bufferram(mtd, from)) {
1261                         this->command(mtd, ONENAND_CMD_READ, from, writesize);
1262                         ret = this->wait(mtd, FL_READING);
1263                         onenand_update_bufferram(mtd, from, !ret);
1264                         if (mtd_is_eccerr(ret))
1265                                 ret = 0;
1266                 }
1267         }
1268
1269         thislen = min_t(int, writesize, len - read);
1270         column = from & (writesize - 1);
1271         if (column + thislen > writesize)
1272                 thislen = writesize - column;
1273
1274         while (!ret) {
1275                 /* If there is more to load then start next load */
1276                 from += thislen;
1277                 if (read + thislen < len) {
1278                         this->command(mtd, ONENAND_CMD_READ, from, writesize);
1279                         /*
1280                          * Chip boundary handling in DDP
1281                          * Now we issued chip 1 read and pointed chip 1
1282                          * bufferram so we have to point chip 0 bufferram.
1283                          */
1284                         if (ONENAND_IS_DDP(this) &&
1285                             unlikely(from == (this->chipsize >> 1))) {
1286                                 this->write_word(ONENAND_DDP_CHIP0, this->base + ONENAND_REG_START_ADDRESS2);
1287                                 boundary = 1;
1288                         } else
1289                                 boundary = 0;
1290                         ONENAND_SET_PREV_BUFFERRAM(this);
1291                 }
1292                 /* While load is going, read from last bufferRAM */
1293                 this->read_bufferram(mtd, ONENAND_DATARAM, buf, column, thislen);
1294
1295                 /* Read oob area if needed */
1296                 if (oobbuf) {
1297                         thisooblen = oobsize - oobcolumn;
1298                         thisooblen = min_t(int, thisooblen, ooblen - oobread);
1299
1300                         if (ops->mode == MTD_OPS_AUTO_OOB)
1301                                 onenand_transfer_auto_oob(mtd, oobbuf, oobcolumn, thisooblen);
1302                         else
1303                                 this->read_bufferram(mtd, ONENAND_SPARERAM, oobbuf, oobcolumn, thisooblen);
1304                         oobread += thisooblen;
1305                         oobbuf += thisooblen;
1306                         oobcolumn = 0;
1307                 }
1308
1309                 /* See if we are done */
1310                 read += thislen;
1311                 if (read == len)
1312                         break;
1313                 /* Set up for next read from bufferRAM */
1314                 if (unlikely(boundary))
1315                         this->write_word(ONENAND_DDP_CHIP1, this->base + ONENAND_REG_START_ADDRESS2);
1316                 ONENAND_SET_NEXT_BUFFERRAM(this);
1317                 buf += thislen;
1318                 thislen = min_t(int, writesize, len - read);
1319                 column = 0;
1320                 cond_resched();
1321                 /* Now wait for load */
1322                 ret = this->wait(mtd, FL_READING);
1323                 onenand_update_bufferram(mtd, from, !ret);
1324                 if (mtd_is_eccerr(ret))
1325                         ret = 0;
1326         }
1327
1328         /*
1329          * Return success, if no ECC failures, else -EBADMSG
1330          * fs driver will take care of that, because
1331          * retlen == desired len and result == -EBADMSG
1332          */
1333         ops->retlen = read;
1334         ops->oobretlen = oobread;
1335
1336         if (ret)
1337                 return ret;
1338
1339         if (mtd->ecc_stats.failed - stats.failed)
1340                 return -EBADMSG;
1341
1342         /* return max bitflips per ecc step; ONENANDs correct 1 bit only */
1343         return mtd->ecc_stats.corrected != stats.corrected ? 1 : 0;
1344 }
1345
1346 /**
1347  * onenand_read_oob_nolock - [MTD Interface] OneNAND read out-of-band
1348  * @param mtd           MTD device structure
1349  * @param from          offset to read from
1350  * @param ops:          oob operation description structure
1351  *
1352  * OneNAND read out-of-band data from the spare area
1353  */
1354 static int onenand_read_oob_nolock(struct mtd_info *mtd, loff_t from,
1355                         struct mtd_oob_ops *ops)
1356 {
1357         struct onenand_chip *this = mtd->priv;
1358         struct mtd_ecc_stats stats;
1359         int read = 0, thislen, column, oobsize;
1360         size_t len = ops->ooblen;
1361         unsigned int mode = ops->mode;
1362         u_char *buf = ops->oobbuf;
1363         int ret = 0, readcmd;
1364
1365         from += ops->ooboffs;
1366
1367         pr_debug("%s: from = 0x%08x, len = %i\n", __func__, (unsigned int)from,
1368                         (int)len);
1369
1370         /* Initialize return length value */
1371         ops->oobretlen = 0;
1372
1373         if (mode == MTD_OPS_AUTO_OOB)
1374                 oobsize = mtd->oobavail;
1375         else
1376                 oobsize = mtd->oobsize;
1377
1378         column = from & (mtd->oobsize - 1);
1379
1380         if (unlikely(column >= oobsize)) {
1381                 printk(KERN_ERR "%s: Attempted to start read outside oob\n",
1382                         __func__);
1383                 return -EINVAL;
1384         }
1385
1386         stats = mtd->ecc_stats;
1387
1388         readcmd = ONENAND_IS_4KB_PAGE(this) ? ONENAND_CMD_READ : ONENAND_CMD_READOOB;
1389
1390         while (read < len) {
1391                 cond_resched();
1392
1393                 thislen = oobsize - column;
1394                 thislen = min_t(int, thislen, len);
1395
1396                 this->command(mtd, readcmd, from, mtd->oobsize);
1397
1398                 onenand_update_bufferram(mtd, from, 0);
1399
1400                 ret = this->wait(mtd, FL_READING);
1401                 if (unlikely(ret))
1402                         ret = onenand_recover_lsb(mtd, from, ret);
1403
1404                 if (ret && !mtd_is_eccerr(ret)) {
1405                         printk(KERN_ERR "%s: read failed = 0x%x\n",
1406                                 __func__, ret);
1407                         break;
1408                 }
1409
1410                 if (mode == MTD_OPS_AUTO_OOB)
1411                         onenand_transfer_auto_oob(mtd, buf, column, thislen);
1412                 else
1413                         this->read_bufferram(mtd, ONENAND_SPARERAM, buf, column, thislen);
1414
1415                 read += thislen;
1416
1417                 if (read == len)
1418                         break;
1419
1420                 buf += thislen;
1421
1422                 /* Read more? */
1423                 if (read < len) {
1424                         /* Page size */
1425                         from += mtd->writesize;
1426                         column = 0;
1427                 }
1428         }
1429
1430         ops->oobretlen = read;
1431
1432         if (ret)
1433                 return ret;
1434
1435         if (mtd->ecc_stats.failed - stats.failed)
1436                 return -EBADMSG;
1437
1438         return 0;
1439 }
1440
1441 /**
1442  * onenand_read_oob - [MTD Interface] Read main and/or out-of-band
1443  * @param mtd:          MTD device structure
1444  * @param from:         offset to read from
1445  * @param ops:          oob operation description structure
1446
1447  * Read main and/or out-of-band
1448  */
1449 static int onenand_read_oob(struct mtd_info *mtd, loff_t from,
1450                             struct mtd_oob_ops *ops)
1451 {
1452         struct onenand_chip *this = mtd->priv;
1453         int ret;
1454
1455         switch (ops->mode) {
1456         case MTD_OPS_PLACE_OOB:
1457         case MTD_OPS_AUTO_OOB:
1458                 break;
1459         case MTD_OPS_RAW:
1460                 /* Not implemented yet */
1461         default:
1462                 return -EINVAL;
1463         }
1464
1465         onenand_get_device(mtd, FL_READING);
1466         if (ops->datbuf)
1467                 ret = ONENAND_IS_4KB_PAGE(this) ?
1468                         onenand_mlc_read_ops_nolock(mtd, from, ops) :
1469                         onenand_read_ops_nolock(mtd, from, ops);
1470         else
1471                 ret = onenand_read_oob_nolock(mtd, from, ops);
1472         onenand_release_device(mtd);
1473
1474         return ret;
1475 }
1476
1477 /**
1478  * onenand_bbt_wait - [DEFAULT] wait until the command is done
1479  * @param mtd           MTD device structure
1480  * @param state         state to select the max. timeout value
1481  *
1482  * Wait for command done.
1483  */
1484 static int onenand_bbt_wait(struct mtd_info *mtd, int state)
1485 {
1486         struct onenand_chip *this = mtd->priv;
1487         unsigned long timeout;
1488         unsigned int interrupt, ctrl, ecc, addr1, addr8;
1489
1490         /* The 20 msec is enough */
1491         timeout = jiffies + msecs_to_jiffies(20);
1492         while (time_before(jiffies, timeout)) {
1493                 interrupt = this->read_word(this->base + ONENAND_REG_INTERRUPT);
1494                 if (interrupt & ONENAND_INT_MASTER)
1495                         break;
1496         }
1497         /* To get correct interrupt status in timeout case */
1498         interrupt = this->read_word(this->base + ONENAND_REG_INTERRUPT);
1499         ctrl = this->read_word(this->base + ONENAND_REG_CTRL_STATUS);
1500         addr1 = this->read_word(this->base + ONENAND_REG_START_ADDRESS1);
1501         addr8 = this->read_word(this->base + ONENAND_REG_START_ADDRESS8);
1502
1503         if (interrupt & ONENAND_INT_READ) {
1504                 ecc = onenand_read_ecc(this);
1505                 if (ecc & ONENAND_ECC_2BIT_ALL) {
1506                         printk(KERN_DEBUG "%s: ecc 0x%04x ctrl 0x%04x "
1507                                "intr 0x%04x addr1 %#x addr8 %#x\n",
1508                                __func__, ecc, ctrl, interrupt, addr1, addr8);
1509                         return ONENAND_BBT_READ_ECC_ERROR;
1510                 }
1511         } else {
1512                 printk(KERN_ERR "%s: read timeout! ctrl 0x%04x "
1513                        "intr 0x%04x addr1 %#x addr8 %#x\n",
1514                        __func__, ctrl, interrupt, addr1, addr8);
1515                 return ONENAND_BBT_READ_FATAL_ERROR;
1516         }
1517
1518         /* Initial bad block case: 0x2400 or 0x0400 */
1519         if (ctrl & ONENAND_CTRL_ERROR) {
1520                 printk(KERN_DEBUG "%s: ctrl 0x%04x intr 0x%04x addr1 %#x "
1521                        "addr8 %#x\n", __func__, ctrl, interrupt, addr1, addr8);
1522                 return ONENAND_BBT_READ_ERROR;
1523         }
1524
1525         return 0;
1526 }
1527
1528 /**
1529  * onenand_bbt_read_oob - [MTD Interface] OneNAND read out-of-band for bbt scan
1530  * @param mtd           MTD device structure
1531  * @param from          offset to read from
1532  * @param ops           oob operation description structure
1533  *
1534  * OneNAND read out-of-band data from the spare area for bbt scan
1535  */
1536 int onenand_bbt_read_oob(struct mtd_info *mtd, loff_t from, 
1537                             struct mtd_oob_ops *ops)
1538 {
1539         struct onenand_chip *this = mtd->priv;
1540         int read = 0, thislen, column;
1541         int ret = 0, readcmd;
1542         size_t len = ops->ooblen;
1543         u_char *buf = ops->oobbuf;
1544
1545         pr_debug("%s: from = 0x%08x, len = %zi\n", __func__, (unsigned int)from,
1546                         len);
1547
1548         /* Initialize return value */
1549         ops->oobretlen = 0;
1550
1551         /* Do not allow reads past end of device */
1552         if (unlikely((from + len) > mtd->size)) {
1553                 printk(KERN_ERR "%s: Attempt read beyond end of device\n",
1554                         __func__);
1555                 return ONENAND_BBT_READ_FATAL_ERROR;
1556         }
1557
1558         /* Grab the lock and see if the device is available */
1559         onenand_get_device(mtd, FL_READING);
1560
1561         column = from & (mtd->oobsize - 1);
1562
1563         readcmd = ONENAND_IS_4KB_PAGE(this) ? ONENAND_CMD_READ : ONENAND_CMD_READOOB;
1564
1565         while (read < len) {
1566                 cond_resched();
1567
1568                 thislen = mtd->oobsize - column;
1569                 thislen = min_t(int, thislen, len);
1570
1571                 this->command(mtd, readcmd, from, mtd->oobsize);
1572
1573                 onenand_update_bufferram(mtd, from, 0);
1574
1575                 ret = this->bbt_wait(mtd, FL_READING);
1576                 if (unlikely(ret))
1577                         ret = onenand_recover_lsb(mtd, from, ret);
1578
1579                 if (ret)
1580                         break;
1581
1582                 this->read_bufferram(mtd, ONENAND_SPARERAM, buf, column, thislen);
1583                 read += thislen;
1584                 if (read == len)
1585                         break;
1586
1587                 buf += thislen;
1588
1589                 /* Read more? */
1590                 if (read < len) {
1591                         /* Update Page size */
1592                         from += this->writesize;
1593                         column = 0;
1594                 }
1595         }
1596
1597         /* Deselect and wake up anyone waiting on the device */
1598         onenand_release_device(mtd);
1599
1600         ops->oobretlen = read;
1601         return ret;
1602 }
1603
1604 #ifdef CONFIG_MTD_ONENAND_VERIFY_WRITE
1605 /**
1606  * onenand_verify_oob - [GENERIC] verify the oob contents after a write
1607  * @param mtd           MTD device structure
1608  * @param buf           the databuffer to verify
1609  * @param to            offset to read from
1610  */
1611 static int onenand_verify_oob(struct mtd_info *mtd, const u_char *buf, loff_t to)
1612 {
1613         struct onenand_chip *this = mtd->priv;
1614         u_char *oob_buf = this->oob_buf;
1615         int status, i, readcmd;
1616
1617         readcmd = ONENAND_IS_4KB_PAGE(this) ? ONENAND_CMD_READ : ONENAND_CMD_READOOB;
1618
1619         this->command(mtd, readcmd, to, mtd->oobsize);
1620         onenand_update_bufferram(mtd, to, 0);
1621         status = this->wait(mtd, FL_READING);
1622         if (status)
1623                 return status;
1624
1625         this->read_bufferram(mtd, ONENAND_SPARERAM, oob_buf, 0, mtd->oobsize);
1626         for (i = 0; i < mtd->oobsize; i++)
1627                 if (buf[i] != 0xFF && buf[i] != oob_buf[i])
1628                         return -EBADMSG;
1629
1630         return 0;
1631 }
1632
1633 /**
1634  * onenand_verify - [GENERIC] verify the chip contents after a write
1635  * @param mtd          MTD device structure
1636  * @param buf          the databuffer to verify
1637  * @param addr         offset to read from
1638  * @param len          number of bytes to read and compare
1639  */
1640 static int onenand_verify(struct mtd_info *mtd, const u_char *buf, loff_t addr, size_t len)
1641 {
1642         struct onenand_chip *this = mtd->priv;
1643         int ret = 0;
1644         int thislen, column;
1645
1646         column = addr & (this->writesize - 1);
1647
1648         while (len != 0) {
1649                 thislen = min_t(int, this->writesize - column, len);
1650
1651                 this->command(mtd, ONENAND_CMD_READ, addr, this->writesize);
1652
1653                 onenand_update_bufferram(mtd, addr, 0);
1654
1655                 ret = this->wait(mtd, FL_READING);
1656                 if (ret)
1657                         return ret;
1658
1659                 onenand_update_bufferram(mtd, addr, 1);
1660
1661                 this->read_bufferram(mtd, ONENAND_DATARAM, this->verify_buf, 0, mtd->writesize);
1662
1663                 if (memcmp(buf, this->verify_buf + column, thislen))
1664                         return -EBADMSG;
1665
1666                 len -= thislen;
1667                 buf += thislen;
1668                 addr += thislen;
1669                 column = 0;
1670         }
1671
1672         return 0;
1673 }
1674 #else
1675 #define onenand_verify(...)             (0)
1676 #define onenand_verify_oob(...)         (0)
1677 #endif
1678
1679 #define NOTALIGNED(x)   ((x & (this->subpagesize - 1)) != 0)
1680
1681 static void onenand_panic_wait(struct mtd_info *mtd)
1682 {
1683         struct onenand_chip *this = mtd->priv;
1684         unsigned int interrupt;
1685         int i;
1686         
1687         for (i = 0; i < 2000; i++) {
1688                 interrupt = this->read_word(this->base + ONENAND_REG_INTERRUPT);
1689                 if (interrupt & ONENAND_INT_MASTER)
1690                         break;
1691                 udelay(10);
1692         }
1693 }
1694
1695 /**
1696  * onenand_panic_write - [MTD Interface] write buffer to FLASH in a panic context
1697  * @param mtd           MTD device structure
1698  * @param to            offset to write to
1699  * @param len           number of bytes to write
1700  * @param retlen        pointer to variable to store the number of written bytes
1701  * @param buf           the data to write
1702  *
1703  * Write with ECC
1704  */
1705 static int onenand_panic_write(struct mtd_info *mtd, loff_t to, size_t len,
1706                          size_t *retlen, const u_char *buf)
1707 {
1708         struct onenand_chip *this = mtd->priv;
1709         int column, subpage;
1710         int written = 0;
1711
1712         if (this->state == FL_PM_SUSPENDED)
1713                 return -EBUSY;
1714
1715         /* Wait for any existing operation to clear */
1716         onenand_panic_wait(mtd);
1717
1718         pr_debug("%s: to = 0x%08x, len = %i\n", __func__, (unsigned int)to,
1719                         (int)len);
1720
1721         /* Reject writes, which are not page aligned */
1722         if (unlikely(NOTALIGNED(to) || NOTALIGNED(len))) {
1723                 printk(KERN_ERR "%s: Attempt to write not page aligned data\n",
1724                         __func__);
1725                 return -EINVAL;
1726         }
1727
1728         column = to & (mtd->writesize - 1);
1729
1730         /* Loop until all data write */
1731         while (written < len) {
1732                 int thislen = min_t(int, mtd->writesize - column, len - written);
1733                 u_char *wbuf = (u_char *) buf;
1734
1735                 this->command(mtd, ONENAND_CMD_BUFFERRAM, to, thislen);
1736
1737                 /* Partial page write */
1738                 subpage = thislen < mtd->writesize;
1739                 if (subpage) {
1740                         memset(this->page_buf, 0xff, mtd->writesize);
1741                         memcpy(this->page_buf + column, buf, thislen);
1742                         wbuf = this->page_buf;
1743                 }
1744
1745                 this->write_bufferram(mtd, ONENAND_DATARAM, wbuf, 0, mtd->writesize);
1746                 this->write_bufferram(mtd, ONENAND_SPARERAM, ffchars, 0, mtd->oobsize);
1747
1748                 this->command(mtd, ONENAND_CMD_PROG, to, mtd->writesize);
1749
1750                 onenand_panic_wait(mtd);
1751
1752                 /* In partial page write we don't update bufferram */
1753                 onenand_update_bufferram(mtd, to, !subpage);
1754                 if (ONENAND_IS_2PLANE(this)) {
1755                         ONENAND_SET_BUFFERRAM1(this);
1756                         onenand_update_bufferram(mtd, to + this->writesize, !subpage);
1757                 }
1758
1759                 written += thislen;
1760
1761                 if (written == len)
1762                         break;
1763
1764                 column = 0;
1765                 to += thislen;
1766                 buf += thislen;
1767         }
1768
1769         *retlen = written;
1770         return 0;
1771 }
1772
1773 /**
1774  * onenand_fill_auto_oob - [INTERN] oob auto-placement transfer
1775  * @param mtd           MTD device structure
1776  * @param oob_buf       oob buffer
1777  * @param buf           source address
1778  * @param column        oob offset to write to
1779  * @param thislen       oob length to write
1780  */
1781 static int onenand_fill_auto_oob(struct mtd_info *mtd, u_char *oob_buf,
1782                                   const u_char *buf, int column, int thislen)
1783 {
1784         return mtd_ooblayout_set_databytes(mtd, buf, oob_buf, column, thislen);
1785 }
1786
1787 /**
1788  * onenand_write_ops_nolock - [OneNAND Interface] write main and/or out-of-band
1789  * @param mtd           MTD device structure
1790  * @param to            offset to write to
1791  * @param ops           oob operation description structure
1792  *
1793  * Write main and/or oob with ECC
1794  */
1795 static int onenand_write_ops_nolock(struct mtd_info *mtd, loff_t to,
1796                                 struct mtd_oob_ops *ops)
1797 {
1798         struct onenand_chip *this = mtd->priv;
1799         int written = 0, column, thislen = 0, subpage = 0;
1800         int prev = 0, prevlen = 0, prev_subpage = 0, first = 1;
1801         int oobwritten = 0, oobcolumn, thisooblen, oobsize;
1802         size_t len = ops->len;
1803         size_t ooblen = ops->ooblen;
1804         const u_char *buf = ops->datbuf;
1805         const u_char *oob = ops->oobbuf;
1806         u_char *oobbuf;
1807         int ret = 0, cmd;
1808
1809         pr_debug("%s: to = 0x%08x, len = %i\n", __func__, (unsigned int)to,
1810                         (int)len);
1811
1812         /* Initialize retlen, in case of early exit */
1813         ops->retlen = 0;
1814         ops->oobretlen = 0;
1815
1816         /* Reject writes, which are not page aligned */
1817         if (unlikely(NOTALIGNED(to) || NOTALIGNED(len))) {
1818                 printk(KERN_ERR "%s: Attempt to write not page aligned data\n",
1819                         __func__);
1820                 return -EINVAL;
1821         }
1822
1823         /* Check zero length */
1824         if (!len)
1825                 return 0;
1826         oobsize = mtd_oobavail(mtd, ops);
1827         oobcolumn = to & (mtd->oobsize - 1);
1828
1829         column = to & (mtd->writesize - 1);
1830
1831         /* Loop until all data write */
1832         while (1) {
1833                 if (written < len) {
1834                         u_char *wbuf = (u_char *) buf;
1835
1836                         thislen = min_t(int, mtd->writesize - column, len - written);
1837                         thisooblen = min_t(int, oobsize - oobcolumn, ooblen - oobwritten);
1838
1839                         cond_resched();
1840
1841                         this->command(mtd, ONENAND_CMD_BUFFERRAM, to, thislen);
1842
1843                         /* Partial page write */
1844                         subpage = thislen < mtd->writesize;
1845                         if (subpage) {
1846                                 memset(this->page_buf, 0xff, mtd->writesize);
1847                                 memcpy(this->page_buf + column, buf, thislen);
1848                                 wbuf = this->page_buf;
1849                         }
1850
1851                         this->write_bufferram(mtd, ONENAND_DATARAM, wbuf, 0, mtd->writesize);
1852
1853                         if (oob) {
1854                                 oobbuf = this->oob_buf;
1855
1856                                 /* We send data to spare ram with oobsize
1857                                  * to prevent byte access */
1858                                 memset(oobbuf, 0xff, mtd->oobsize);
1859                                 if (ops->mode == MTD_OPS_AUTO_OOB)
1860                                         onenand_fill_auto_oob(mtd, oobbuf, oob, oobcolumn, thisooblen);
1861                                 else
1862                                         memcpy(oobbuf + oobcolumn, oob, thisooblen);
1863
1864                                 oobwritten += thisooblen;
1865                                 oob += thisooblen;
1866                                 oobcolumn = 0;
1867                         } else
1868                                 oobbuf = (u_char *) ffchars;
1869
1870                         this->write_bufferram(mtd, ONENAND_SPARERAM, oobbuf, 0, mtd->oobsize);
1871                 } else
1872                         ONENAND_SET_NEXT_BUFFERRAM(this);
1873
1874                 /*
1875                  * 2 PLANE, MLC, and Flex-OneNAND do not support
1876                  * write-while-program feature.
1877                  */
1878                 if (!ONENAND_IS_2PLANE(this) && !ONENAND_IS_4KB_PAGE(this) && !first) {
1879                         ONENAND_SET_PREV_BUFFERRAM(this);
1880
1881                         ret = this->wait(mtd, FL_WRITING);
1882
1883                         /* In partial page write we don't update bufferram */
1884                         onenand_update_bufferram(mtd, prev, !ret && !prev_subpage);
1885                         if (ret) {
1886                                 written -= prevlen;
1887                                 printk(KERN_ERR "%s: write failed %d\n",
1888                                         __func__, ret);
1889                                 break;
1890                         }
1891
1892                         if (written == len) {
1893                                 /* Only check verify write turn on */
1894                                 ret = onenand_verify(mtd, buf - len, to - len, len);
1895                                 if (ret)
1896                                         printk(KERN_ERR "%s: verify failed %d\n",
1897                                                 __func__, ret);
1898                                 break;
1899                         }
1900
1901                         ONENAND_SET_NEXT_BUFFERRAM(this);
1902                 }
1903
1904                 this->ongoing = 0;
1905                 cmd = ONENAND_CMD_PROG;
1906
1907                 /* Exclude 1st OTP and OTP blocks for cache program feature */
1908                 if (ONENAND_IS_CACHE_PROGRAM(this) &&
1909                     likely(onenand_block(this, to) != 0) &&
1910                     ONENAND_IS_4KB_PAGE(this) &&
1911                     ((written + thislen) < len)) {
1912                         cmd = ONENAND_CMD_2X_CACHE_PROG;
1913                         this->ongoing = 1;
1914                 }
1915
1916                 this->command(mtd, cmd, to, mtd->writesize);
1917
1918                 /*
1919                  * 2 PLANE, MLC, and Flex-OneNAND wait here
1920                  */
1921                 if (ONENAND_IS_2PLANE(this) || ONENAND_IS_4KB_PAGE(this)) {
1922                         ret = this->wait(mtd, FL_WRITING);
1923
1924                         /* In partial page write we don't update bufferram */
1925                         onenand_update_bufferram(mtd, to, !ret && !subpage);
1926                         if (ret) {
1927                                 printk(KERN_ERR "%s: write failed %d\n",
1928                                         __func__, ret);
1929                                 break;
1930                         }
1931
1932                         /* Only check verify write turn on */
1933                         ret = onenand_verify(mtd, buf, to, thislen);
1934                         if (ret) {
1935                                 printk(KERN_ERR "%s: verify failed %d\n",
1936                                         __func__, ret);
1937                                 break;
1938                         }
1939
1940                         written += thislen;
1941
1942                         if (written == len)
1943                                 break;
1944
1945                 } else
1946                         written += thislen;
1947
1948                 column = 0;
1949                 prev_subpage = subpage;
1950                 prev = to;
1951                 prevlen = thislen;
1952                 to += thislen;
1953                 buf += thislen;
1954                 first = 0;
1955         }
1956
1957         /* In error case, clear all bufferrams */
1958         if (written != len)
1959                 onenand_invalidate_bufferram(mtd, 0, -1);
1960
1961         ops->retlen = written;
1962         ops->oobretlen = oobwritten;
1963
1964         return ret;
1965 }
1966
1967
1968 /**
1969  * onenand_write_oob_nolock - [INTERN] OneNAND write out-of-band
1970  * @param mtd           MTD device structure
1971  * @param to            offset to write to
1972  * @param len           number of bytes to write
1973  * @param retlen        pointer to variable to store the number of written bytes
1974  * @param buf           the data to write
1975  * @param mode          operation mode
1976  *
1977  * OneNAND write out-of-band
1978  */
1979 static int onenand_write_oob_nolock(struct mtd_info *mtd, loff_t to,
1980                                     struct mtd_oob_ops *ops)
1981 {
1982         struct onenand_chip *this = mtd->priv;
1983         int column, ret = 0, oobsize;
1984         int written = 0, oobcmd;
1985         u_char *oobbuf;
1986         size_t len = ops->ooblen;
1987         const u_char *buf = ops->oobbuf;
1988         unsigned int mode = ops->mode;
1989
1990         to += ops->ooboffs;
1991
1992         pr_debug("%s: to = 0x%08x, len = %i\n", __func__, (unsigned int)to,
1993                         (int)len);
1994
1995         /* Initialize retlen, in case of early exit */
1996         ops->oobretlen = 0;
1997
1998         if (mode == MTD_OPS_AUTO_OOB)
1999                 oobsize = mtd->oobavail;
2000         else
2001                 oobsize = mtd->oobsize;
2002
2003         column = to & (mtd->oobsize - 1);
2004
2005         if (unlikely(column >= oobsize)) {
2006                 printk(KERN_ERR "%s: Attempted to start write outside oob\n",
2007                         __func__);
2008                 return -EINVAL;
2009         }
2010
2011         /* For compatibility with NAND: Do not allow write past end of page */
2012         if (unlikely(column + len > oobsize)) {
2013                 printk(KERN_ERR "%s: Attempt to write past end of page\n",
2014                         __func__);
2015                 return -EINVAL;
2016         }
2017
2018         oobbuf = this->oob_buf;
2019
2020         oobcmd = ONENAND_IS_4KB_PAGE(this) ? ONENAND_CMD_PROG : ONENAND_CMD_PROGOOB;
2021
2022         /* Loop until all data write */
2023         while (written < len) {
2024                 int thislen = min_t(int, oobsize, len - written);
2025
2026                 cond_resched();
2027
2028                 this->command(mtd, ONENAND_CMD_BUFFERRAM, to, mtd->oobsize);
2029
2030                 /* We send data to spare ram with oobsize
2031                  * to prevent byte access */
2032                 memset(oobbuf, 0xff, mtd->oobsize);
2033                 if (mode == MTD_OPS_AUTO_OOB)
2034                         onenand_fill_auto_oob(mtd, oobbuf, buf, column, thislen);
2035                 else
2036                         memcpy(oobbuf + column, buf, thislen);
2037                 this->write_bufferram(mtd, ONENAND_SPARERAM, oobbuf, 0, mtd->oobsize);
2038
2039                 if (ONENAND_IS_4KB_PAGE(this)) {
2040                         /* Set main area of DataRAM to 0xff*/
2041                         memset(this->page_buf, 0xff, mtd->writesize);
2042                         this->write_bufferram(mtd, ONENAND_DATARAM,
2043                                          this->page_buf, 0, mtd->writesize);
2044                 }
2045
2046                 this->command(mtd, oobcmd, to, mtd->oobsize);
2047
2048                 onenand_update_bufferram(mtd, to, 0);
2049                 if (ONENAND_IS_2PLANE(this)) {
2050                         ONENAND_SET_BUFFERRAM1(this);
2051                         onenand_update_bufferram(mtd, to + this->writesize, 0);
2052                 }
2053
2054                 ret = this->wait(mtd, FL_WRITING);
2055                 if (ret) {
2056                         printk(KERN_ERR "%s: write failed %d\n", __func__, ret);
2057                         break;
2058                 }
2059
2060                 ret = onenand_verify_oob(mtd, oobbuf, to);
2061                 if (ret) {
2062                         printk(KERN_ERR "%s: verify failed %d\n",
2063                                 __func__, ret);
2064                         break;
2065                 }
2066
2067                 written += thislen;
2068                 if (written == len)
2069                         break;
2070
2071                 to += mtd->writesize;
2072                 buf += thislen;
2073                 column = 0;
2074         }
2075
2076         ops->oobretlen = written;
2077
2078         return ret;
2079 }
2080
2081 /**
2082  * onenand_write_oob - [MTD Interface] NAND write data and/or out-of-band
2083  * @param mtd:          MTD device structure
2084  * @param to:           offset to write
2085  * @param ops:          oob operation description structure
2086  */
2087 static int onenand_write_oob(struct mtd_info *mtd, loff_t to,
2088                              struct mtd_oob_ops *ops)
2089 {
2090         int ret;
2091
2092         switch (ops->mode) {
2093         case MTD_OPS_PLACE_OOB:
2094         case MTD_OPS_AUTO_OOB:
2095                 break;
2096         case MTD_OPS_RAW:
2097                 /* Not implemented yet */
2098         default:
2099                 return -EINVAL;
2100         }
2101
2102         onenand_get_device(mtd, FL_WRITING);
2103         if (ops->datbuf)
2104                 ret = onenand_write_ops_nolock(mtd, to, ops);
2105         else
2106                 ret = onenand_write_oob_nolock(mtd, to, ops);
2107         onenand_release_device(mtd);
2108
2109         return ret;
2110 }
2111
2112 /**
2113  * onenand_block_isbad_nolock - [GENERIC] Check if a block is marked bad
2114  * @param mtd           MTD device structure
2115  * @param ofs           offset from device start
2116  * @param allowbbt      1, if its allowed to access the bbt area
2117  *
2118  * Check, if the block is bad. Either by reading the bad block table or
2119  * calling of the scan function.
2120  */
2121 static int onenand_block_isbad_nolock(struct mtd_info *mtd, loff_t ofs, int allowbbt)
2122 {
2123         struct onenand_chip *this = mtd->priv;
2124         struct bbm_info *bbm = this->bbm;
2125
2126         /* Return info from the table */
2127         return bbm->isbad_bbt(mtd, ofs, allowbbt);
2128 }
2129
2130
2131 static int onenand_multiblock_erase_verify(struct mtd_info *mtd,
2132                                            struct erase_info *instr)
2133 {
2134         struct onenand_chip *this = mtd->priv;
2135         loff_t addr = instr->addr;
2136         int len = instr->len;
2137         unsigned int block_size = (1 << this->erase_shift);
2138         int ret = 0;
2139
2140         while (len) {
2141                 this->command(mtd, ONENAND_CMD_ERASE_VERIFY, addr, block_size);
2142                 ret = this->wait(mtd, FL_VERIFYING_ERASE);
2143                 if (ret) {
2144                         printk(KERN_ERR "%s: Failed verify, block %d\n",
2145                                __func__, onenand_block(this, addr));
2146                         instr->state = MTD_ERASE_FAILED;
2147                         instr->fail_addr = addr;
2148                         return -1;
2149                 }
2150                 len -= block_size;
2151                 addr += block_size;
2152         }
2153         return 0;
2154 }
2155
2156 /**
2157  * onenand_multiblock_erase - [INTERN] erase block(s) using multiblock erase
2158  * @param mtd           MTD device structure
2159  * @param instr         erase instruction
2160  * @param region        erase region
2161  *
2162  * Erase one or more blocks up to 64 block at a time
2163  */
2164 static int onenand_multiblock_erase(struct mtd_info *mtd,
2165                                     struct erase_info *instr,
2166                                     unsigned int block_size)
2167 {
2168         struct onenand_chip *this = mtd->priv;
2169         loff_t addr = instr->addr;
2170         int len = instr->len;
2171         int eb_count = 0;
2172         int ret = 0;
2173         int bdry_block = 0;
2174
2175         instr->state = MTD_ERASING;
2176
2177         if (ONENAND_IS_DDP(this)) {
2178                 loff_t bdry_addr = this->chipsize >> 1;
2179                 if (addr < bdry_addr && (addr + len) > bdry_addr)
2180                         bdry_block = bdry_addr >> this->erase_shift;
2181         }
2182
2183         /* Pre-check bbs */
2184         while (len) {
2185                 /* Check if we have a bad block, we do not erase bad blocks */
2186                 if (onenand_block_isbad_nolock(mtd, addr, 0)) {
2187                         printk(KERN_WARNING "%s: attempt to erase a bad block "
2188                                "at addr 0x%012llx\n",
2189                                __func__, (unsigned long long) addr);
2190                         instr->state = MTD_ERASE_FAILED;
2191                         return -EIO;
2192                 }
2193                 len -= block_size;
2194                 addr += block_size;
2195         }
2196
2197         len = instr->len;
2198         addr = instr->addr;
2199
2200         /* loop over 64 eb batches */
2201         while (len) {
2202                 struct erase_info verify_instr = *instr;
2203                 int max_eb_count = MB_ERASE_MAX_BLK_COUNT;
2204
2205                 verify_instr.addr = addr;
2206                 verify_instr.len = 0;
2207
2208                 /* do not cross chip boundary */
2209                 if (bdry_block) {
2210                         int this_block = (addr >> this->erase_shift);
2211
2212                         if (this_block < bdry_block) {
2213                                 max_eb_count = min(max_eb_count,
2214                                                    (bdry_block - this_block));
2215                         }
2216                 }
2217
2218                 eb_count = 0;
2219
2220                 while (len > block_size && eb_count < (max_eb_count - 1)) {
2221                         this->command(mtd, ONENAND_CMD_MULTIBLOCK_ERASE,
2222                                       addr, block_size);
2223                         onenand_invalidate_bufferram(mtd, addr, block_size);
2224
2225                         ret = this->wait(mtd, FL_PREPARING_ERASE);
2226                         if (ret) {
2227                                 printk(KERN_ERR "%s: Failed multiblock erase, "
2228                                        "block %d\n", __func__,
2229                                        onenand_block(this, addr));
2230                                 instr->state = MTD_ERASE_FAILED;
2231                                 instr->fail_addr = MTD_FAIL_ADDR_UNKNOWN;
2232                                 return -EIO;
2233                         }
2234
2235                         len -= block_size;
2236                         addr += block_size;
2237                         eb_count++;
2238                 }
2239
2240                 /* last block of 64-eb series */
2241                 cond_resched();
2242                 this->command(mtd, ONENAND_CMD_ERASE, addr, block_size);
2243                 onenand_invalidate_bufferram(mtd, addr, block_size);
2244
2245                 ret = this->wait(mtd, FL_ERASING);
2246                 /* Check if it is write protected */
2247                 if (ret) {
2248                         printk(KERN_ERR "%s: Failed erase, block %d\n",
2249                                __func__, onenand_block(this, addr));
2250                         instr->state = MTD_ERASE_FAILED;
2251                         instr->fail_addr = MTD_FAIL_ADDR_UNKNOWN;
2252                         return -EIO;
2253                 }
2254
2255                 len -= block_size;
2256                 addr += block_size;
2257                 eb_count++;
2258
2259                 /* verify */
2260                 verify_instr.len = eb_count * block_size;
2261                 if (onenand_multiblock_erase_verify(mtd, &verify_instr)) {
2262                         instr->state = verify_instr.state;
2263                         instr->fail_addr = verify_instr.fail_addr;
2264                         return -EIO;
2265                 }
2266
2267         }
2268         return 0;
2269 }
2270
2271
2272 /**
2273  * onenand_block_by_block_erase - [INTERN] erase block(s) using regular erase
2274  * @param mtd           MTD device structure
2275  * @param instr         erase instruction
2276  * @param region        erase region
2277  * @param block_size    erase block size
2278  *
2279  * Erase one or more blocks one block at a time
2280  */
2281 static int onenand_block_by_block_erase(struct mtd_info *mtd,
2282                                         struct erase_info *instr,
2283                                         struct mtd_erase_region_info *region,
2284                                         unsigned int block_size)
2285 {
2286         struct onenand_chip *this = mtd->priv;
2287         loff_t addr = instr->addr;
2288         int len = instr->len;
2289         loff_t region_end = 0;
2290         int ret = 0;
2291
2292         if (region) {
2293                 /* region is set for Flex-OneNAND */
2294                 region_end = region->offset + region->erasesize * region->numblocks;
2295         }
2296
2297         instr->state = MTD_ERASING;
2298
2299         /* Loop through the blocks */
2300         while (len) {
2301                 cond_resched();
2302
2303                 /* Check if we have a bad block, we do not erase bad blocks */
2304                 if (onenand_block_isbad_nolock(mtd, addr, 0)) {
2305                         printk(KERN_WARNING "%s: attempt to erase a bad block "
2306                                         "at addr 0x%012llx\n",
2307                                         __func__, (unsigned long long) addr);
2308                         instr->state = MTD_ERASE_FAILED;
2309                         return -EIO;
2310                 }
2311
2312                 this->command(mtd, ONENAND_CMD_ERASE, addr, block_size);
2313
2314                 onenand_invalidate_bufferram(mtd, addr, block_size);
2315
2316                 ret = this->wait(mtd, FL_ERASING);
2317                 /* Check, if it is write protected */
2318                 if (ret) {
2319                         printk(KERN_ERR "%s: Failed erase, block %d\n",
2320                                 __func__, onenand_block(this, addr));
2321                         instr->state = MTD_ERASE_FAILED;
2322                         instr->fail_addr = addr;
2323                         return -EIO;
2324                 }
2325
2326                 len -= block_size;
2327                 addr += block_size;
2328
2329                 if (region && addr == region_end) {
2330                         if (!len)
2331                                 break;
2332                         region++;
2333
2334                         block_size = region->erasesize;
2335                         region_end = region->offset + region->erasesize * region->numblocks;
2336
2337                         if (len & (block_size - 1)) {
2338                                 /* FIXME: This should be handled at MTD partitioning level. */
2339                                 printk(KERN_ERR "%s: Unaligned address\n",
2340                                         __func__);
2341                                 return -EIO;
2342                         }
2343                 }
2344         }
2345         return 0;
2346 }
2347
2348 /**
2349  * onenand_erase - [MTD Interface] erase block(s)
2350  * @param mtd           MTD device structure
2351  * @param instr         erase instruction
2352  *
2353  * Erase one or more blocks
2354  */
2355 static int onenand_erase(struct mtd_info *mtd, struct erase_info *instr)
2356 {
2357         struct onenand_chip *this = mtd->priv;
2358         unsigned int block_size;
2359         loff_t addr = instr->addr;
2360         loff_t len = instr->len;
2361         int ret = 0;
2362         struct mtd_erase_region_info *region = NULL;
2363         loff_t region_offset = 0;
2364
2365         pr_debug("%s: start=0x%012llx, len=%llu\n", __func__,
2366                         (unsigned long long)instr->addr,
2367                         (unsigned long long)instr->len);
2368
2369         if (FLEXONENAND(this)) {
2370                 /* Find the eraseregion of this address */
2371                 int i = flexonenand_region(mtd, addr);
2372
2373                 region = &mtd->eraseregions[i];
2374                 block_size = region->erasesize;
2375
2376                 /* Start address within region must align on block boundary.
2377                  * Erase region's start offset is always block start address.
2378                  */
2379                 region_offset = region->offset;
2380         } else
2381                 block_size = 1 << this->erase_shift;
2382
2383         /* Start address must align on block boundary */
2384         if (unlikely((addr - region_offset) & (block_size - 1))) {
2385                 printk(KERN_ERR "%s: Unaligned address\n", __func__);
2386                 return -EINVAL;
2387         }
2388
2389         /* Length must align on block boundary */
2390         if (unlikely(len & (block_size - 1))) {
2391                 printk(KERN_ERR "%s: Length not block aligned\n", __func__);
2392                 return -EINVAL;
2393         }
2394
2395         /* Grab the lock and see if the device is available */
2396         onenand_get_device(mtd, FL_ERASING);
2397
2398         if (ONENAND_IS_4KB_PAGE(this) || region ||
2399             instr->len < MB_ERASE_MIN_BLK_COUNT * block_size) {
2400                 /* region is set for Flex-OneNAND (no mb erase) */
2401                 ret = onenand_block_by_block_erase(mtd, instr,
2402                                                    region, block_size);
2403         } else {
2404                 ret = onenand_multiblock_erase(mtd, instr, block_size);
2405         }
2406
2407         /* Deselect and wake up anyone waiting on the device */
2408         onenand_release_device(mtd);
2409
2410         /* Do call back function */
2411         if (!ret) {
2412                 instr->state = MTD_ERASE_DONE;
2413                 mtd_erase_callback(instr);
2414         }
2415
2416         return ret;
2417 }
2418
2419 /**
2420  * onenand_sync - [MTD Interface] sync
2421  * @param mtd           MTD device structure
2422  *
2423  * Sync is actually a wait for chip ready function
2424  */
2425 static void onenand_sync(struct mtd_info *mtd)
2426 {
2427         pr_debug("%s: called\n", __func__);
2428
2429         /* Grab the lock and see if the device is available */
2430         onenand_get_device(mtd, FL_SYNCING);
2431
2432         /* Release it and go back */
2433         onenand_release_device(mtd);
2434 }
2435
2436 /**
2437  * onenand_block_isbad - [MTD Interface] Check whether the block at the given offset is bad
2438  * @param mtd           MTD device structure
2439  * @param ofs           offset relative to mtd start
2440  *
2441  * Check whether the block is bad
2442  */
2443 static int onenand_block_isbad(struct mtd_info *mtd, loff_t ofs)
2444 {
2445         int ret;
2446
2447         onenand_get_device(mtd, FL_READING);
2448         ret = onenand_block_isbad_nolock(mtd, ofs, 0);
2449         onenand_release_device(mtd);
2450         return ret;
2451 }
2452
2453 /**
2454  * onenand_default_block_markbad - [DEFAULT] mark a block bad
2455  * @param mtd           MTD device structure
2456  * @param ofs           offset from device start
2457  *
2458  * This is the default implementation, which can be overridden by
2459  * a hardware specific driver.
2460  */
2461 static int onenand_default_block_markbad(struct mtd_info *mtd, loff_t ofs)
2462 {
2463         struct onenand_chip *this = mtd->priv;
2464         struct bbm_info *bbm = this->bbm;
2465         u_char buf[2] = {0, 0};
2466         struct mtd_oob_ops ops = {
2467                 .mode = MTD_OPS_PLACE_OOB,
2468                 .ooblen = 2,
2469                 .oobbuf = buf,
2470                 .ooboffs = 0,
2471         };
2472         int block;
2473
2474         /* Get block number */
2475         block = onenand_block(this, ofs);
2476         if (bbm->bbt)
2477                 bbm->bbt[block >> 2] |= 0x01 << ((block & 0x03) << 1);
2478
2479         /* We write two bytes, so we don't have to mess with 16-bit access */
2480         ofs += mtd->oobsize + (bbm->badblockpos & ~0x01);
2481         /* FIXME : What to do when marking SLC block in partition
2482          *         with MLC erasesize? For now, it is not advisable to
2483          *         create partitions containing both SLC and MLC regions.
2484          */
2485         return onenand_write_oob_nolock(mtd, ofs, &ops);
2486 }
2487
2488 /**
2489  * onenand_block_markbad - [MTD Interface] Mark the block at the given offset as bad
2490  * @param mtd           MTD device structure
2491  * @param ofs           offset relative to mtd start
2492  *
2493  * Mark the block as bad
2494  */
2495 static int onenand_block_markbad(struct mtd_info *mtd, loff_t ofs)
2496 {
2497         struct onenand_chip *this = mtd->priv;
2498         int ret;
2499
2500         ret = onenand_block_isbad(mtd, ofs);
2501         if (ret) {
2502                 /* If it was bad already, return success and do nothing */
2503                 if (ret > 0)
2504                         return 0;
2505                 return ret;
2506         }
2507
2508         onenand_get_device(mtd, FL_WRITING);
2509         ret = this->block_markbad(mtd, ofs);
2510         onenand_release_device(mtd);
2511         return ret;
2512 }
2513
2514 /**
2515  * onenand_do_lock_cmd - [OneNAND Interface] Lock or unlock block(s)
2516  * @param mtd           MTD device structure
2517  * @param ofs           offset relative to mtd start
2518  * @param len           number of bytes to lock or unlock
2519  * @param cmd           lock or unlock command
2520  *
2521  * Lock or unlock one or more blocks
2522  */
2523 static int onenand_do_lock_cmd(struct mtd_info *mtd, loff_t ofs, size_t len, int cmd)
2524 {
2525         struct onenand_chip *this = mtd->priv;
2526         int start, end, block, value, status;
2527         int wp_status_mask;
2528
2529         start = onenand_block(this, ofs);
2530         end = onenand_block(this, ofs + len) - 1;
2531
2532         if (cmd == ONENAND_CMD_LOCK)
2533                 wp_status_mask = ONENAND_WP_LS;
2534         else
2535                 wp_status_mask = ONENAND_WP_US;
2536
2537         /* Continuous lock scheme */
2538         if (this->options & ONENAND_HAS_CONT_LOCK) {
2539                 /* Set start block address */
2540                 this->write_word(start, this->base + ONENAND_REG_START_BLOCK_ADDRESS);
2541                 /* Set end block address */
2542                 this->write_word(end, this->base +  ONENAND_REG_END_BLOCK_ADDRESS);
2543                 /* Write lock command */
2544                 this->command(mtd, cmd, 0, 0);
2545
2546                 /* There's no return value */
2547                 this->wait(mtd, FL_LOCKING);
2548
2549                 /* Sanity check */
2550                 while (this->read_word(this->base + ONENAND_REG_CTRL_STATUS)
2551                     & ONENAND_CTRL_ONGO)
2552                         continue;
2553
2554                 /* Check lock status */
2555                 status = this->read_word(this->base + ONENAND_REG_WP_STATUS);
2556                 if (!(status & wp_status_mask))
2557                         printk(KERN_ERR "%s: wp status = 0x%x\n",
2558                                 __func__, status);
2559
2560                 return 0;
2561         }
2562
2563         /* Block lock scheme */
2564         for (block = start; block < end + 1; block++) {
2565                 /* Set block address */
2566                 value = onenand_block_address(this, block);
2567                 this->write_word(value, this->base + ONENAND_REG_START_ADDRESS1);
2568                 /* Select DataRAM for DDP */
2569                 value = onenand_bufferram_address(this, block);
2570                 this->write_word(value, this->base + ONENAND_REG_START_ADDRESS2);
2571                 /* Set start block address */
2572                 this->write_word(block, this->base + ONENAND_REG_START_BLOCK_ADDRESS);
2573                 /* Write lock command */
2574                 this->command(mtd, cmd, 0, 0);
2575
2576                 /* There's no return value */
2577                 this->wait(mtd, FL_LOCKING);
2578
2579                 /* Sanity check */
2580                 while (this->read_word(this->base + ONENAND_REG_CTRL_STATUS)
2581                     & ONENAND_CTRL_ONGO)
2582                         continue;
2583
2584                 /* Check lock status */
2585                 status = this->read_word(this->base + ONENAND_REG_WP_STATUS);
2586                 if (!(status & wp_status_mask))
2587                         printk(KERN_ERR "%s: block = %d, wp status = 0x%x\n",
2588                                 __func__, block, status);
2589         }
2590
2591         return 0;
2592 }
2593
2594 /**
2595  * onenand_lock - [MTD Interface] Lock block(s)
2596  * @param mtd           MTD device structure
2597  * @param ofs           offset relative to mtd start
2598  * @param len           number of bytes to unlock
2599  *
2600  * Lock one or more blocks
2601  */
2602 static int onenand_lock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
2603 {
2604         int ret;
2605
2606         onenand_get_device(mtd, FL_LOCKING);
2607         ret = onenand_do_lock_cmd(mtd, ofs, len, ONENAND_CMD_LOCK);
2608         onenand_release_device(mtd);
2609         return ret;
2610 }
2611
2612 /**
2613  * onenand_unlock - [MTD Interface] Unlock block(s)
2614  * @param mtd           MTD device structure
2615  * @param ofs           offset relative to mtd start
2616  * @param len           number of bytes to unlock
2617  *
2618  * Unlock one or more blocks
2619  */
2620 static int onenand_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
2621 {
2622         int ret;
2623
2624         onenand_get_device(mtd, FL_LOCKING);
2625         ret = onenand_do_lock_cmd(mtd, ofs, len, ONENAND_CMD_UNLOCK);
2626         onenand_release_device(mtd);
2627         return ret;
2628 }
2629
2630 /**
2631  * onenand_check_lock_status - [OneNAND Interface] Check lock status
2632  * @param this          onenand chip data structure
2633  *
2634  * Check lock status
2635  */
2636 static int onenand_check_lock_status(struct onenand_chip *this)
2637 {
2638         unsigned int value, block, status;
2639         unsigned int end;
2640
2641         end = this->chipsize >> this->erase_shift;
2642         for (block = 0; block < end; block++) {
2643                 /* Set block address */
2644                 value = onenand_block_address(this, block);
2645                 this->write_word(value, this->base + ONENAND_REG_START_ADDRESS1);
2646                 /* Select DataRAM for DDP */
2647                 value = onenand_bufferram_address(this, block);
2648                 this->write_word(value, this->base + ONENAND_REG_START_ADDRESS2);
2649                 /* Set start block address */
2650                 this->write_word(block, this->base + ONENAND_REG_START_BLOCK_ADDRESS);
2651
2652                 /* Check lock status */
2653                 status = this->read_word(this->base + ONENAND_REG_WP_STATUS);
2654                 if (!(status & ONENAND_WP_US)) {
2655                         printk(KERN_ERR "%s: block = %d, wp status = 0x%x\n",
2656                                 __func__, block, status);
2657                         return 0;
2658                 }
2659         }
2660
2661         return 1;
2662 }
2663
2664 /**
2665  * onenand_unlock_all - [OneNAND Interface] unlock all blocks
2666  * @param mtd           MTD device structure
2667  *
2668  * Unlock all blocks
2669  */
2670 static void onenand_unlock_all(struct mtd_info *mtd)
2671 {
2672         struct onenand_chip *this = mtd->priv;
2673         loff_t ofs = 0;
2674         loff_t len = mtd->size;
2675
2676         if (this->options & ONENAND_HAS_UNLOCK_ALL) {
2677                 /* Set start block address */
2678                 this->write_word(0, this->base + ONENAND_REG_START_BLOCK_ADDRESS);
2679                 /* Write unlock command */
2680                 this->command(mtd, ONENAND_CMD_UNLOCK_ALL, 0, 0);
2681
2682                 /* There's no return value */
2683                 this->wait(mtd, FL_LOCKING);
2684
2685                 /* Sanity check */
2686                 while (this->read_word(this->base + ONENAND_REG_CTRL_STATUS)
2687                     & ONENAND_CTRL_ONGO)
2688                         continue;
2689
2690                 /* Don't check lock status */
2691                 if (this->options & ONENAND_SKIP_UNLOCK_CHECK)
2692                         return;
2693
2694                 /* Check lock status */
2695                 if (onenand_check_lock_status(this))
2696                         return;
2697
2698                 /* Workaround for all block unlock in DDP */
2699                 if (ONENAND_IS_DDP(this) && !FLEXONENAND(this)) {
2700                         /* All blocks on another chip */
2701                         ofs = this->chipsize >> 1;
2702                         len = this->chipsize >> 1;
2703                 }
2704         }
2705
2706         onenand_do_lock_cmd(mtd, ofs, len, ONENAND_CMD_UNLOCK);
2707 }
2708
2709 #ifdef CONFIG_MTD_ONENAND_OTP
2710
2711 /**
2712  * onenand_otp_command - Send OTP specific command to OneNAND device
2713  * @param mtd    MTD device structure
2714  * @param cmd    the command to be sent
2715  * @param addr   offset to read from or write to
2716  * @param len    number of bytes to read or write
2717  */
2718 static int onenand_otp_command(struct mtd_info *mtd, int cmd, loff_t addr,
2719                                 size_t len)
2720 {
2721         struct onenand_chip *this = mtd->priv;
2722         int value, block, page;
2723
2724         /* Address translation */
2725         switch (cmd) {
2726         case ONENAND_CMD_OTP_ACCESS:
2727                 block = (int) (addr >> this->erase_shift);
2728                 page = -1;
2729                 break;
2730
2731         default:
2732                 block = (int) (addr >> this->erase_shift);
2733                 page = (int) (addr >> this->page_shift);
2734
2735                 if (ONENAND_IS_2PLANE(this)) {
2736                         /* Make the even block number */
2737                         block &= ~1;
2738                         /* Is it the odd plane? */
2739                         if (addr & this->writesize)
2740                                 block++;
2741                         page >>= 1;
2742                 }
2743                 page &= this->page_mask;
2744                 break;
2745         }
2746
2747         if (block != -1) {
2748                 /* Write 'DFS, FBA' of Flash */
2749                 value = onenand_block_address(this, block);
2750                 this->write_word(value, this->base +
2751                                 ONENAND_REG_START_ADDRESS1);
2752         }
2753
2754         if (page != -1) {
2755                 /* Now we use page size operation */
2756                 int sectors = 4, count = 4;
2757                 int dataram;
2758
2759                 switch (cmd) {
2760                 default:
2761                         if (ONENAND_IS_2PLANE(this) && cmd == ONENAND_CMD_PROG)
2762                                 cmd = ONENAND_CMD_2X_PROG;
2763                         dataram = ONENAND_CURRENT_BUFFERRAM(this);
2764                         break;
2765                 }
2766
2767                 /* Write 'FPA, FSA' of Flash */
2768                 value = onenand_page_address(page, sectors);
2769                 this->write_word(value, this->base +
2770                                 ONENAND_REG_START_ADDRESS8);
2771
2772                 /* Write 'BSA, BSC' of DataRAM */
2773                 value = onenand_buffer_address(dataram, sectors, count);
2774                 this->write_word(value, this->base + ONENAND_REG_START_BUFFER);
2775         }
2776
2777         /* Interrupt clear */
2778         this->write_word(ONENAND_INT_CLEAR, this->base + ONENAND_REG_INTERRUPT);
2779
2780         /* Write command */
2781         this->write_word(cmd, this->base + ONENAND_REG_COMMAND);
2782
2783         return 0;
2784 }
2785
2786 /**
2787  * onenand_otp_write_oob_nolock - [INTERN] OneNAND write out-of-band, specific to OTP
2788  * @param mtd           MTD device structure
2789  * @param to            offset to write to
2790  * @param len           number of bytes to write
2791  * @param retlen        pointer to variable to store the number of written bytes
2792  * @param buf           the data to write
2793  *
2794  * OneNAND write out-of-band only for OTP
2795  */
2796 static int onenand_otp_write_oob_nolock(struct mtd_info *mtd, loff_t to,
2797                                     struct mtd_oob_ops *ops)
2798 {
2799         struct onenand_chip *this = mtd->priv;
2800         int column, ret = 0, oobsize;
2801         int written = 0;
2802         u_char *oobbuf;
2803         size_t len = ops->ooblen;
2804         const u_char *buf = ops->oobbuf;
2805         int block, value, status;
2806
2807         to += ops->ooboffs;
2808
2809         /* Initialize retlen, in case of early exit */
2810         ops->oobretlen = 0;
2811
2812         oobsize = mtd->oobsize;
2813
2814         column = to & (mtd->oobsize - 1);
2815
2816         oobbuf = this->oob_buf;
2817
2818         /* Loop until all data write */
2819         while (written < len) {
2820                 int thislen = min_t(int, oobsize, len - written);
2821
2822                 cond_resched();
2823
2824                 block = (int) (to >> this->erase_shift);
2825                 /*
2826                  * Write 'DFS, FBA' of Flash
2827                  * Add: F100h DQ=DFS, FBA
2828                  */
2829
2830                 value = onenand_block_address(this, block);
2831                 this->write_word(value, this->base +
2832                                 ONENAND_REG_START_ADDRESS1);
2833
2834                 /*
2835                  * Select DataRAM for DDP
2836                  * Add: F101h DQ=DBS
2837                  */
2838
2839                 value = onenand_bufferram_address(this, block);
2840                 this->write_word(value, this->base +
2841                                 ONENAND_REG_START_ADDRESS2);
2842                 ONENAND_SET_NEXT_BUFFERRAM(this);
2843
2844                 /*
2845                  * Enter OTP access mode
2846                  */
2847                 this->command(mtd, ONENAND_CMD_OTP_ACCESS, 0, 0);
2848                 this->wait(mtd, FL_OTPING);
2849
2850                 /* We send data to spare ram with oobsize
2851                  * to prevent byte access */
2852                 memcpy(oobbuf + column, buf, thislen);
2853
2854                 /*
2855                  * Write Data into DataRAM
2856                  * Add: 8th Word
2857                  * in sector0/spare/page0
2858                  * DQ=XXFCh
2859                  */
2860                 this->write_bufferram(mtd, ONENAND_SPARERAM,
2861                                         oobbuf, 0, mtd->oobsize);
2862
2863                 onenand_otp_command(mtd, ONENAND_CMD_PROGOOB, to, mtd->oobsize);
2864                 onenand_update_bufferram(mtd, to, 0);
2865                 if (ONENAND_IS_2PLANE(this)) {
2866                         ONENAND_SET_BUFFERRAM1(this);
2867                         onenand_update_bufferram(mtd, to + this->writesize, 0);
2868                 }
2869
2870                 ret = this->wait(mtd, FL_WRITING);
2871                 if (ret) {
2872                         printk(KERN_ERR "%s: write failed %d\n", __func__, ret);
2873                         break;
2874                 }
2875
2876                 /* Exit OTP access mode */
2877                 this->command(mtd, ONENAND_CMD_RESET, 0, 0);
2878                 this->wait(mtd, FL_RESETING);
2879
2880                 status = this->read_word(this->base + ONENAND_REG_CTRL_STATUS);
2881                 status &= 0x60;
2882
2883                 if (status == 0x60) {
2884                         printk(KERN_DEBUG "\nBLOCK\tSTATUS\n");
2885                         printk(KERN_DEBUG "1st Block\tLOCKED\n");
2886                         printk(KERN_DEBUG "OTP Block\tLOCKED\n");
2887                 } else if (status == 0x20) {
2888                         printk(KERN_DEBUG "\nBLOCK\tSTATUS\n");
2889                         printk(KERN_DEBUG "1st Block\tLOCKED\n");
2890                         printk(KERN_DEBUG "OTP Block\tUN-LOCKED\n");
2891                 } else if (status == 0x40) {
2892                         printk(KERN_DEBUG "\nBLOCK\tSTATUS\n");
2893                         printk(KERN_DEBUG "1st Block\tUN-LOCKED\n");
2894                         printk(KERN_DEBUG "OTP Block\tLOCKED\n");
2895                 } else {
2896                         printk(KERN_DEBUG "Reboot to check\n");
2897                 }
2898
2899                 written += thislen;
2900                 if (written == len)
2901                         break;
2902
2903                 to += mtd->writesize;
2904                 buf += thislen;
2905                 column = 0;
2906         }
2907
2908         ops->oobretlen = written;
2909
2910         return ret;
2911 }
2912
2913 /* Internal OTP operation */
2914 typedef int (*otp_op_t)(struct mtd_info *mtd, loff_t form, size_t len,
2915                 size_t *retlen, u_char *buf);
2916
2917 /**
2918  * do_otp_read - [DEFAULT] Read OTP block area
2919  * @param mtd           MTD device structure
2920  * @param from          The offset to read
2921  * @param len           number of bytes to read
2922  * @param retlen        pointer to variable to store the number of readbytes
2923  * @param buf           the databuffer to put/get data
2924  *
2925  * Read OTP block area.
2926  */
2927 static int do_otp_read(struct mtd_info *mtd, loff_t from, size_t len,
2928                 size_t *retlen, u_char *buf)
2929 {
2930         struct onenand_chip *this = mtd->priv;
2931         struct mtd_oob_ops ops = {
2932                 .len    = len,
2933                 .ooblen = 0,
2934                 .datbuf = buf,
2935                 .oobbuf = NULL,
2936         };
2937         int ret;
2938
2939         /* Enter OTP access mode */
2940         this->command(mtd, ONENAND_CMD_OTP_ACCESS, 0, 0);
2941         this->wait(mtd, FL_OTPING);
2942
2943         ret = ONENAND_IS_4KB_PAGE(this) ?
2944                 onenand_mlc_read_ops_nolock(mtd, from, &ops) :
2945                 onenand_read_ops_nolock(mtd, from, &ops);
2946
2947         /* Exit OTP access mode */
2948         this->command(mtd, ONENAND_CMD_RESET, 0, 0);
2949         this->wait(mtd, FL_RESETING);
2950
2951         return ret;
2952 }
2953
2954 /**
2955  * do_otp_write - [DEFAULT] Write OTP block area
2956  * @param mtd           MTD device structure
2957  * @param to            The offset to write
2958  * @param len           number of bytes to write
2959  * @param retlen        pointer to variable to store the number of write bytes
2960  * @param buf           the databuffer to put/get data
2961  *
2962  * Write OTP block area.
2963  */
2964 static int do_otp_write(struct mtd_info *mtd, loff_t to, size_t len,
2965                 size_t *retlen, u_char *buf)
2966 {
2967         struct onenand_chip *this = mtd->priv;
2968         unsigned char *pbuf = buf;
2969         int ret;
2970         struct mtd_oob_ops ops;
2971
2972         /* Force buffer page aligned */
2973         if (len < mtd->writesize) {
2974                 memcpy(this->page_buf, buf, len);
2975                 memset(this->page_buf + len, 0xff, mtd->writesize - len);
2976                 pbuf = this->page_buf;
2977                 len = mtd->writesize;
2978         }
2979
2980         /* Enter OTP access mode */
2981         this->command(mtd, ONENAND_CMD_OTP_ACCESS, 0, 0);
2982         this->wait(mtd, FL_OTPING);
2983
2984         ops.len = len;
2985         ops.ooblen = 0;
2986         ops.datbuf = pbuf;
2987         ops.oobbuf = NULL;
2988         ret = onenand_write_ops_nolock(mtd, to, &ops);
2989         *retlen = ops.retlen;
2990
2991         /* Exit OTP access mode */
2992         this->command(mtd, ONENAND_CMD_RESET, 0, 0);
2993         this->wait(mtd, FL_RESETING);
2994
2995         return ret;
2996 }
2997
2998 /**
2999  * do_otp_lock - [DEFAULT] Lock OTP block area
3000  * @param mtd           MTD device structure
3001  * @param from          The offset to lock
3002  * @param len           number of bytes to lock
3003  * @param retlen        pointer to variable to store the number of lock bytes
3004  * @param buf           the databuffer to put/get data
3005  *
3006  * Lock OTP block area.
3007  */
3008 static int do_otp_lock(struct mtd_info *mtd, loff_t from, size_t len,
3009                 size_t *retlen, u_char *buf)
3010 {
3011         struct onenand_chip *this = mtd->priv;
3012         struct mtd_oob_ops ops;
3013         int ret;
3014
3015         if (FLEXONENAND(this)) {
3016
3017                 /* Enter OTP access mode */
3018                 this->command(mtd, ONENAND_CMD_OTP_ACCESS, 0, 0);
3019                 this->wait(mtd, FL_OTPING);
3020                 /*
3021                  * For Flex-OneNAND, we write lock mark to 1st word of sector 4 of
3022                  * main area of page 49.
3023                  */
3024                 ops.len = mtd->writesize;
3025                 ops.ooblen = 0;
3026                 ops.datbuf = buf;
3027                 ops.oobbuf = NULL;
3028                 ret = onenand_write_ops_nolock(mtd, mtd->writesize * 49, &ops);
3029                 *retlen = ops.retlen;
3030
3031                 /* Exit OTP access mode */
3032                 this->command(mtd, ONENAND_CMD_RESET, 0, 0);
3033                 this->wait(mtd, FL_RESETING);
3034         } else {
3035                 ops.mode = MTD_OPS_PLACE_OOB;
3036                 ops.ooblen = len;
3037                 ops.oobbuf = buf;
3038                 ops.ooboffs = 0;
3039                 ret = onenand_otp_write_oob_nolock(mtd, from, &ops);
3040                 *retlen = ops.oobretlen;
3041         }
3042
3043         return ret;
3044 }
3045
3046 /**
3047  * onenand_otp_walk - [DEFAULT] Handle OTP operation
3048  * @param mtd           MTD device structure
3049  * @param from          The offset to read/write
3050  * @param len           number of bytes to read/write
3051  * @param retlen        pointer to variable to store the number of read bytes
3052  * @param buf           the databuffer to put/get data
3053  * @param action        do given action
3054  * @param mode          specify user and factory
3055  *
3056  * Handle OTP operation.
3057  */
3058 static int onenand_otp_walk(struct mtd_info *mtd, loff_t from, size_t len,
3059                         size_t *retlen, u_char *buf,
3060                         otp_op_t action, int mode)
3061 {
3062         struct onenand_chip *this = mtd->priv;
3063         int otp_pages;
3064         int density;
3065         int ret = 0;
3066
3067         *retlen = 0;
3068
3069         density = onenand_get_density(this->device_id);
3070         if (density < ONENAND_DEVICE_DENSITY_512Mb)
3071                 otp_pages = 20;
3072         else
3073                 otp_pages = 50;
3074
3075         if (mode == MTD_OTP_FACTORY) {
3076                 from += mtd->writesize * otp_pages;
3077                 otp_pages = ONENAND_PAGES_PER_BLOCK - otp_pages;
3078         }
3079
3080         /* Check User/Factory boundary */
3081         if (mode == MTD_OTP_USER) {
3082                 if (mtd->writesize * otp_pages < from + len)
3083                         return 0;
3084         } else {
3085                 if (mtd->writesize * otp_pages <  len)
3086                         return 0;
3087         }
3088
3089         onenand_get_device(mtd, FL_OTPING);
3090         while (len > 0 && otp_pages > 0) {
3091                 if (!action) {  /* OTP Info functions */
3092                         struct otp_info *otpinfo;
3093
3094                         len -= sizeof(struct otp_info);
3095                         if (len <= 0) {
3096                                 ret = -ENOSPC;
3097                                 break;
3098                         }
3099
3100                         otpinfo = (struct otp_info *) buf;
3101                         otpinfo->start = from;
3102                         otpinfo->length = mtd->writesize;
3103                         otpinfo->locked = 0;
3104
3105                         from += mtd->writesize;
3106                         buf += sizeof(struct otp_info);
3107                         *retlen += sizeof(struct otp_info);
3108                 } else {
3109                         size_t tmp_retlen;
3110
3111                         ret = action(mtd, from, len, &tmp_retlen, buf);
3112                         if (ret)
3113                                 break;
3114
3115                         buf += tmp_retlen;
3116                         len -= tmp_retlen;
3117                         *retlen += tmp_retlen;
3118
3119                 }
3120                 otp_pages--;
3121         }
3122         onenand_release_device(mtd);
3123
3124         return ret;
3125 }
3126
3127 /**
3128  * onenand_get_fact_prot_info - [MTD Interface] Read factory OTP info
3129  * @param mtd           MTD device structure
3130  * @param len           number of bytes to read
3131  * @param retlen        pointer to variable to store the number of read bytes
3132  * @param buf           the databuffer to put/get data
3133  *
3134  * Read factory OTP info.
3135  */
3136 static int onenand_get_fact_prot_info(struct mtd_info *mtd, size_t len,
3137                                       size_t *retlen, struct otp_info *buf)
3138 {
3139         return onenand_otp_walk(mtd, 0, len, retlen, (u_char *) buf, NULL,
3140                                 MTD_OTP_FACTORY);
3141 }
3142
3143 /**
3144  * onenand_read_fact_prot_reg - [MTD Interface] Read factory OTP area
3145  * @param mtd           MTD device structure
3146  * @param from          The offset to read
3147  * @param len           number of bytes to read
3148  * @param retlen        pointer to variable to store the number of read bytes
3149  * @param buf           the databuffer to put/get data
3150  *
3151  * Read factory OTP area.
3152  */
3153 static int onenand_read_fact_prot_reg(struct mtd_info *mtd, loff_t from,
3154                         size_t len, size_t *retlen, u_char *buf)
3155 {
3156         return onenand_otp_walk(mtd, from, len, retlen, buf, do_otp_read, MTD_OTP_FACTORY);
3157 }
3158
3159 /**
3160  * onenand_get_user_prot_info - [MTD Interface] Read user OTP info
3161  * @param mtd           MTD device structure
3162  * @param retlen        pointer to variable to store the number of read bytes
3163  * @param len           number of bytes to read
3164  * @param buf           the databuffer to put/get data
3165  *
3166  * Read user OTP info.
3167  */
3168 static int onenand_get_user_prot_info(struct mtd_info *mtd, size_t len,
3169                                       size_t *retlen, struct otp_info *buf)
3170 {
3171         return onenand_otp_walk(mtd, 0, len, retlen, (u_char *) buf, NULL,
3172                                 MTD_OTP_USER);
3173 }
3174
3175 /**
3176  * onenand_read_user_prot_reg - [MTD Interface] Read user OTP area
3177  * @param mtd           MTD device structure
3178  * @param from          The offset to read
3179  * @param len           number of bytes to read
3180  * @param retlen        pointer to variable to store the number of read bytes
3181  * @param buf           the databuffer to put/get data
3182  *
3183  * Read user OTP area.
3184  */
3185 static int onenand_read_user_prot_reg(struct mtd_info *mtd, loff_t from,
3186                         size_t len, size_t *retlen, u_char *buf)
3187 {
3188         return onenand_otp_walk(mtd, from, len, retlen, buf, do_otp_read, MTD_OTP_USER);
3189 }
3190
3191 /**
3192  * onenand_write_user_prot_reg - [MTD Interface] Write user OTP area
3193  * @param mtd           MTD device structure
3194  * @param from          The offset to write
3195  * @param len           number of bytes to write
3196  * @param retlen        pointer to variable to store the number of write bytes
3197  * @param buf           the databuffer to put/get data
3198  *
3199  * Write user OTP area.
3200  */
3201 static int onenand_write_user_prot_reg(struct mtd_info *mtd, loff_t from,
3202                         size_t len, size_t *retlen, u_char *buf)
3203 {
3204         return onenand_otp_walk(mtd, from, len, retlen, buf, do_otp_write, MTD_OTP_USER);
3205 }
3206
3207 /**
3208  * onenand_lock_user_prot_reg - [MTD Interface] Lock user OTP area
3209  * @param mtd           MTD device structure
3210  * @param from          The offset to lock
3211  * @param len           number of bytes to unlock
3212  *
3213  * Write lock mark on spare area in page 0 in OTP block
3214  */
3215 static int onenand_lock_user_prot_reg(struct mtd_info *mtd, loff_t from,
3216                         size_t len)
3217 {
3218         struct onenand_chip *this = mtd->priv;
3219         u_char *buf = FLEXONENAND(this) ? this->page_buf : this->oob_buf;
3220         size_t retlen;
3221         int ret;
3222         unsigned int otp_lock_offset = ONENAND_OTP_LOCK_OFFSET;
3223
3224         memset(buf, 0xff, FLEXONENAND(this) ? this->writesize
3225                                                  : mtd->oobsize);
3226         /*
3227          * Write lock mark to 8th word of sector0 of page0 of the spare0.
3228          * We write 16 bytes spare area instead of 2 bytes.
3229          * For Flex-OneNAND, we write lock mark to 1st word of sector 4 of
3230          * main area of page 49.
3231          */
3232
3233         from = 0;
3234         len = FLEXONENAND(this) ? mtd->writesize : 16;
3235
3236         /*
3237          * Note: OTP lock operation
3238          *       OTP block : 0xXXFC                     XX 1111 1100
3239          *       1st block : 0xXXF3 (If chip support)   XX 1111 0011
3240          *       Both      : 0xXXF0 (If chip support)   XX 1111 0000
3241          */
3242         if (FLEXONENAND(this))
3243                 otp_lock_offset = FLEXONENAND_OTP_LOCK_OFFSET;
3244
3245         /* ONENAND_OTP_AREA | ONENAND_OTP_BLOCK0 | ONENAND_OTP_AREA_BLOCK0 */
3246         if (otp == 1)
3247                 buf[otp_lock_offset] = 0xFC;
3248         else if (otp == 2)
3249                 buf[otp_lock_offset] = 0xF3;
3250         else if (otp == 3)
3251                 buf[otp_lock_offset] = 0xF0;
3252         else if (otp != 0)
3253                 printk(KERN_DEBUG "[OneNAND] Invalid option selected for OTP\n");
3254
3255         ret = onenand_otp_walk(mtd, from, len, &retlen, buf, do_otp_lock, MTD_OTP_USER);
3256
3257         return ret ? : retlen;
3258 }
3259
3260 #endif  /* CONFIG_MTD_ONENAND_OTP */
3261
3262 /**
3263  * onenand_check_features - Check and set OneNAND features
3264  * @param mtd           MTD data structure
3265  *
3266  * Check and set OneNAND features
3267  * - lock scheme
3268  * - two plane
3269  */
3270 static void onenand_check_features(struct mtd_info *mtd)
3271 {
3272         struct onenand_chip *this = mtd->priv;
3273         unsigned int density, process, numbufs;
3274
3275         /* Lock scheme depends on density and process */
3276         density = onenand_get_density(this->device_id);
3277         process = this->version_id >> ONENAND_VERSION_PROCESS_SHIFT;
3278         numbufs = this->read_word(this->base + ONENAND_REG_NUM_BUFFERS) >> 8;
3279
3280         /* Lock scheme */
3281         switch (density) {
3282         case ONENAND_DEVICE_DENSITY_4Gb:
3283                 if (ONENAND_IS_DDP(this))
3284                         this->options |= ONENAND_HAS_2PLANE;
3285                 else if (numbufs == 1) {
3286                         this->options |= ONENAND_HAS_4KB_PAGE;
3287                         this->options |= ONENAND_HAS_CACHE_PROGRAM;
3288                         /*
3289                          * There are two different 4KiB pagesize chips
3290                          * and no way to detect it by H/W config values.
3291                          *
3292                          * To detect the correct NOP for each chips,
3293                          * It should check the version ID as workaround.
3294                          *
3295                          * Now it has as following
3296                          * KFM4G16Q4M has NOP 4 with version ID 0x0131
3297                          * KFM4G16Q5M has NOP 1 with versoin ID 0x013e
3298                          */
3299                         if ((this->version_id & 0xf) == 0xe)
3300                                 this->options |= ONENAND_HAS_NOP_1;
3301                 }
3302
3303         case ONENAND_DEVICE_DENSITY_2Gb:
3304                 /* 2Gb DDP does not have 2 plane */
3305                 if (!ONENAND_IS_DDP(this))
3306                         this->options |= ONENAND_HAS_2PLANE;
3307                 this->options |= ONENAND_HAS_UNLOCK_ALL;
3308
3309         case ONENAND_DEVICE_DENSITY_1Gb:
3310                 /* A-Die has all block unlock */
3311                 if (process)
3312                         this->options |= ONENAND_HAS_UNLOCK_ALL;
3313                 break;
3314
3315         default:
3316                 /* Some OneNAND has continuous lock scheme */
3317                 if (!process)
3318                         this->options |= ONENAND_HAS_CONT_LOCK;
3319                 break;
3320         }
3321
3322         /* The MLC has 4KiB pagesize. */
3323         if (ONENAND_IS_MLC(this))
3324                 this->options |= ONENAND_HAS_4KB_PAGE;
3325
3326         if (ONENAND_IS_4KB_PAGE(this))
3327                 this->options &= ~ONENAND_HAS_2PLANE;
3328
3329         if (FLEXONENAND(this)) {
3330                 this->options &= ~ONENAND_HAS_CONT_LOCK;
3331                 this->options |= ONENAND_HAS_UNLOCK_ALL;
3332         }
3333
3334         if (this->options & ONENAND_HAS_CONT_LOCK)
3335                 printk(KERN_DEBUG "Lock scheme is Continuous Lock\n");
3336         if (this->options & ONENAND_HAS_UNLOCK_ALL)
3337                 printk(KERN_DEBUG "Chip support all block unlock\n");
3338         if (this->options & ONENAND_HAS_2PLANE)
3339                 printk(KERN_DEBUG "Chip has 2 plane\n");
3340         if (this->options & ONENAND_HAS_4KB_PAGE)
3341                 printk(KERN_DEBUG "Chip has 4KiB pagesize\n");
3342         if (this->options & ONENAND_HAS_CACHE_PROGRAM)
3343                 printk(KERN_DEBUG "Chip has cache program feature\n");
3344 }
3345
3346 /**
3347  * onenand_print_device_info - Print device & version ID
3348  * @param device        device ID
3349  * @param version       version ID
3350  *
3351  * Print device & version ID
3352  */
3353 static void onenand_print_device_info(int device, int version)
3354 {
3355         int vcc, demuxed, ddp, density, flexonenand;
3356
3357         vcc = device & ONENAND_DEVICE_VCC_MASK;
3358         demuxed = device & ONENAND_DEVICE_IS_DEMUX;
3359         ddp = device & ONENAND_DEVICE_IS_DDP;
3360         density = onenand_get_density(device);
3361         flexonenand = device & DEVICE_IS_FLEXONENAND;
3362         printk(KERN_INFO "%s%sOneNAND%s %dMB %sV 16-bit (0x%02x)\n",
3363                 demuxed ? "" : "Muxed ",
3364                 flexonenand ? "Flex-" : "",
3365                 ddp ? "(DDP)" : "",
3366                 (16 << density),
3367                 vcc ? "2.65/3.3" : "1.8",
3368                 device);
3369         printk(KERN_INFO "OneNAND version = 0x%04x\n", version);
3370 }
3371
3372 static const struct onenand_manufacturers onenand_manuf_ids[] = {
3373         {ONENAND_MFR_SAMSUNG, "Samsung"},
3374         {ONENAND_MFR_NUMONYX, "Numonyx"},
3375 };
3376
3377 /**
3378  * onenand_check_maf - Check manufacturer ID
3379  * @param manuf         manufacturer ID
3380  *
3381  * Check manufacturer ID
3382  */
3383 static int onenand_check_maf(int manuf)
3384 {
3385         int size = ARRAY_SIZE(onenand_manuf_ids);
3386         char *name;
3387         int i;
3388
3389         for (i = 0; i < size; i++)
3390                 if (manuf == onenand_manuf_ids[i].id)
3391                         break;
3392
3393         if (i < size)
3394                 name = onenand_manuf_ids[i].name;
3395         else
3396                 name = "Unknown";
3397
3398         printk(KERN_DEBUG "OneNAND Manufacturer: %s (0x%0x)\n", name, manuf);
3399
3400         return (i == size);
3401 }
3402
3403 /**
3404 * flexonenand_get_boundary      - Reads the SLC boundary
3405 * @param onenand_info           - onenand info structure
3406 **/
3407 static int flexonenand_get_boundary(struct mtd_info *mtd)
3408 {
3409         struct onenand_chip *this = mtd->priv;
3410         unsigned die, bdry;
3411         int syscfg, locked;
3412
3413         /* Disable ECC */
3414         syscfg = this->read_word(this->base + ONENAND_REG_SYS_CFG1);
3415         this->write_word((syscfg | 0x0100), this->base + ONENAND_REG_SYS_CFG1);
3416
3417         for (die = 0; die < this->dies; die++) {
3418                 this->command(mtd, FLEXONENAND_CMD_PI_ACCESS, die, 0);
3419                 this->wait(mtd, FL_SYNCING);
3420
3421                 this->command(mtd, FLEXONENAND_CMD_READ_PI, die, 0);
3422                 this->wait(mtd, FL_READING);
3423
3424                 bdry = this->read_word(this->base + ONENAND_DATARAM);
3425                 if ((bdry >> FLEXONENAND_PI_UNLOCK_SHIFT) == 3)
3426                         locked = 0;
3427                 else
3428                         locked = 1;
3429                 this->boundary[die] = bdry & FLEXONENAND_PI_MASK;
3430
3431                 this->command(mtd, ONENAND_CMD_RESET, 0, 0);
3432                 this->wait(mtd, FL_RESETING);
3433
3434                 printk(KERN_INFO "Die %d boundary: %d%s\n", die,
3435                        this->boundary[die], locked ? "(Locked)" : "(Unlocked)");
3436         }
3437
3438         /* Enable ECC */
3439         this->write_word(syscfg, this->base + ONENAND_REG_SYS_CFG1);
3440         return 0;
3441 }
3442
3443 /**
3444  * flexonenand_get_size - Fill up fields in onenand_chip and mtd_info
3445  *                        boundary[], diesize[], mtd->size, mtd->erasesize
3446  * @param mtd           - MTD device structure
3447  */
3448 static void flexonenand_get_size(struct mtd_info *mtd)
3449 {
3450         struct onenand_chip *this = mtd->priv;
3451         int die, i, eraseshift, density;
3452         int blksperdie, maxbdry;
3453         loff_t ofs;
3454
3455         density = onenand_get_density(this->device_id);
3456         blksperdie = ((loff_t)(16 << density) << 20) >> (this->erase_shift);
3457         blksperdie >>= ONENAND_IS_DDP(this) ? 1 : 0;
3458         maxbdry = blksperdie - 1;
3459         eraseshift = this->erase_shift - 1;
3460
3461         mtd->numeraseregions = this->dies << 1;
3462
3463         /* This fills up the device boundary */
3464         flexonenand_get_boundary(mtd);
3465         die = ofs = 0;
3466         i = -1;
3467         for (; die < this->dies; die++) {
3468                 if (!die || this->boundary[die-1] != maxbdry) {
3469                         i++;
3470                         mtd->eraseregions[i].offset = ofs;
3471                         mtd->eraseregions[i].erasesize = 1 << eraseshift;
3472                         mtd->eraseregions[i].numblocks =
3473                                                         this->boundary[die] + 1;
3474                         ofs += mtd->eraseregions[i].numblocks << eraseshift;
3475                         eraseshift++;
3476                 } else {
3477                         mtd->numeraseregions -= 1;
3478                         mtd->eraseregions[i].numblocks +=
3479                                                         this->boundary[die] + 1;
3480                         ofs += (this->boundary[die] + 1) << (eraseshift - 1);
3481                 }
3482                 if (this->boundary[die] != maxbdry) {
3483                         i++;
3484                         mtd->eraseregions[i].offset = ofs;
3485                         mtd->eraseregions[i].erasesize = 1 << eraseshift;
3486                         mtd->eraseregions[i].numblocks = maxbdry ^
3487                                                          this->boundary[die];
3488                         ofs += mtd->eraseregions[i].numblocks << eraseshift;
3489                         eraseshift--;
3490                 } else
3491                         mtd->numeraseregions -= 1;
3492         }
3493
3494         /* Expose MLC erase size except when all blocks are SLC */
3495         mtd->erasesize = 1 << this->erase_shift;
3496         if (mtd->numeraseregions == 1)
3497                 mtd->erasesize >>= 1;
3498
3499         printk(KERN_INFO "Device has %d eraseregions\n", mtd->numeraseregions);
3500         for (i = 0; i < mtd->numeraseregions; i++)
3501                 printk(KERN_INFO "[offset: 0x%08x, erasesize: 0x%05x,"
3502                         " numblocks: %04u]\n",
3503                         (unsigned int) mtd->eraseregions[i].offset,
3504                         mtd->eraseregions[i].erasesize,
3505                         mtd->eraseregions[i].numblocks);
3506
3507         for (die = 0, mtd->size = 0; die < this->dies; die++) {
3508                 this->diesize[die] = (loff_t)blksperdie << this->erase_shift;
3509                 this->diesize[die] -= (loff_t)(this->boundary[die] + 1)
3510                                                  << (this->erase_shift - 1);
3511                 mtd->size += this->diesize[die];
3512         }
3513 }
3514
3515 /**
3516  * flexonenand_check_blocks_erased - Check if blocks are erased
3517  * @param mtd_info      - mtd info structure
3518  * @param start         - first erase block to check
3519  * @param end           - last erase block to check
3520  *
3521  * Converting an unerased block from MLC to SLC
3522  * causes byte values to change. Since both data and its ECC
3523  * have changed, reads on the block give uncorrectable error.
3524  * This might lead to the block being detected as bad.
3525  *
3526  * Avoid this by ensuring that the block to be converted is
3527  * erased.
3528  */
3529 static int flexonenand_check_blocks_erased(struct mtd_info *mtd, int start, int end)
3530 {
3531         struct onenand_chip *this = mtd->priv;
3532         int i, ret;
3533         int block;
3534         struct mtd_oob_ops ops = {
3535                 .mode = MTD_OPS_PLACE_OOB,
3536                 .ooboffs = 0,
3537                 .ooblen = mtd->oobsize,
3538                 .datbuf = NULL,
3539                 .oobbuf = this->oob_buf,
3540         };
3541         loff_t addr;
3542
3543         printk(KERN_DEBUG "Check blocks from %d to %d\n", start, end);
3544
3545         for (block = start; block <= end; block++) {
3546                 addr = flexonenand_addr(this, block);
3547                 if (onenand_block_isbad_nolock(mtd, addr, 0))
3548                         continue;
3549
3550                 /*
3551                  * Since main area write results in ECC write to spare,
3552                  * it is sufficient to check only ECC bytes for change.
3553                  */
3554                 ret = onenand_read_oob_nolock(mtd, addr, &ops);
3555                 if (ret)
3556                         return ret;
3557
3558                 for (i = 0; i < mtd->oobsize; i++)
3559                         if (this->oob_buf[i] != 0xff)
3560                                 break;
3561
3562                 if (i != mtd->oobsize) {
3563                         printk(KERN_WARNING "%s: Block %d not erased.\n",
3564                                 __func__, block);
3565                         return 1;
3566                 }
3567         }
3568
3569         return 0;
3570 }
3571
3572 /**
3573  * flexonenand_set_boundary     - Writes the SLC boundary
3574  * @param mtd                   - mtd info structure
3575  */
3576 static int flexonenand_set_boundary(struct mtd_info *mtd, int die,
3577                                     int boundary, int lock)
3578 {
3579         struct onenand_chip *this = mtd->priv;
3580         int ret, density, blksperdie, old, new, thisboundary;
3581         loff_t addr;
3582
3583         /* Change only once for SDP Flex-OneNAND */
3584         if (die && (!ONENAND_IS_DDP(this)))
3585                 return 0;
3586
3587         /* boundary value of -1 indicates no required change */
3588         if (boundary < 0 || boundary == this->boundary[die])
3589                 return 0;
3590
3591         density = onenand_get_density(this->device_id);
3592         blksperdie = ((16 << density) << 20) >> this->erase_shift;
3593         blksperdie >>= ONENAND_IS_DDP(this) ? 1 : 0;
3594
3595         if (boundary >= blksperdie) {
3596                 printk(KERN_ERR "%s: Invalid boundary value. "
3597                                 "Boundary not changed.\n", __func__);
3598                 return -EINVAL;
3599         }
3600
3601         /* Check if converting blocks are erased */
3602         old = this->boundary[die] + (die * this->density_mask);
3603         new = boundary + (die * this->density_mask);
3604         ret = flexonenand_check_blocks_erased(mtd, min(old, new) + 1, max(old, new));
3605         if (ret) {
3606                 printk(KERN_ERR "%s: Please erase blocks "
3607                                 "before boundary change\n", __func__);
3608                 return ret;
3609         }
3610
3611         this->command(mtd, FLEXONENAND_CMD_PI_ACCESS, die, 0);
3612         this->wait(mtd, FL_SYNCING);
3613
3614         /* Check is boundary is locked */
3615         this->command(mtd, FLEXONENAND_CMD_READ_PI, die, 0);
3616         this->wait(mtd, FL_READING);
3617
3618         thisboundary = this->read_word(this->base + ONENAND_DATARAM);
3619         if ((thisboundary >> FLEXONENAND_PI_UNLOCK_SHIFT) != 3) {
3620                 printk(KERN_ERR "%s: boundary locked\n", __func__);
3621                 ret = 1;
3622                 goto out;
3623         }
3624
3625         printk(KERN_INFO "Changing die %d boundary: %d%s\n",
3626                         die, boundary, lock ? "(Locked)" : "(Unlocked)");
3627
3628         addr = die ? this->diesize[0] : 0;
3629
3630         boundary &= FLEXONENAND_PI_MASK;
3631         boundary |= lock ? 0 : (3 << FLEXONENAND_PI_UNLOCK_SHIFT);
3632
3633         this->command(mtd, ONENAND_CMD_ERASE, addr, 0);
3634         ret = this->wait(mtd, FL_ERASING);
3635         if (ret) {
3636                 printk(KERN_ERR "%s: Failed PI erase for Die %d\n",
3637                        __func__, die);
3638                 goto out;
3639         }
3640
3641         this->write_word(boundary, this->base + ONENAND_DATARAM);
3642         this->command(mtd, ONENAND_CMD_PROG, addr, 0);
3643         ret = this->wait(mtd, FL_WRITING);
3644         if (ret) {
3645                 printk(KERN_ERR "%s: Failed PI write for Die %d\n",
3646                         __func__, die);
3647                 goto out;
3648         }
3649
3650         this->command(mtd, FLEXONENAND_CMD_PI_UPDATE, die, 0);
3651         ret = this->wait(mtd, FL_WRITING);
3652 out:
3653         this->write_word(ONENAND_CMD_RESET, this->base + ONENAND_REG_COMMAND);
3654         this->wait(mtd, FL_RESETING);
3655         if (!ret)
3656                 /* Recalculate device size on boundary change*/
3657                 flexonenand_get_size(mtd);
3658
3659         return ret;
3660 }
3661
3662 /**
3663  * onenand_chip_probe - [OneNAND Interface] The generic chip probe
3664  * @param mtd           MTD device structure
3665  *
3666  * OneNAND detection method:
3667  *   Compare the values from command with ones from register
3668  */
3669 static int onenand_chip_probe(struct mtd_info *mtd)
3670 {
3671         struct onenand_chip *this = mtd->priv;
3672         int bram_maf_id, bram_dev_id, maf_id, dev_id;
3673         int syscfg;
3674
3675         /* Save system configuration 1 */
3676         syscfg = this->read_word(this->base + ONENAND_REG_SYS_CFG1);
3677         /* Clear Sync. Burst Read mode to read BootRAM */
3678         this->write_word((syscfg & ~ONENAND_SYS_CFG1_SYNC_READ & ~ONENAND_SYS_CFG1_SYNC_WRITE), this->base + ONENAND_REG_SYS_CFG1);
3679
3680         /* Send the command for reading device ID from BootRAM */
3681         this->write_word(ONENAND_CMD_READID, this->base + ONENAND_BOOTRAM);
3682
3683         /* Read manufacturer and device IDs from BootRAM */
3684         bram_maf_id = this->read_word(this->base + ONENAND_BOOTRAM + 0x0);
3685         bram_dev_id = this->read_word(this->base + ONENAND_BOOTRAM + 0x2);
3686
3687         /* Reset OneNAND to read default register values */
3688         this->write_word(ONENAND_CMD_RESET, this->base + ONENAND_BOOTRAM);
3689         /* Wait reset */
3690         this->wait(mtd, FL_RESETING);
3691
3692         /* Restore system configuration 1 */
3693         this->write_word(syscfg, this->base + ONENAND_REG_SYS_CFG1);
3694
3695         /* Check manufacturer ID */
3696         if (onenand_check_maf(bram_maf_id))
3697                 return -ENXIO;
3698
3699         /* Read manufacturer and device IDs from Register */
3700         maf_id = this->read_word(this->base + ONENAND_REG_MANUFACTURER_ID);
3701         dev_id = this->read_word(this->base + ONENAND_REG_DEVICE_ID);
3702
3703         /* Check OneNAND device */
3704         if (maf_id != bram_maf_id || dev_id != bram_dev_id)
3705                 return -ENXIO;
3706
3707         return 0;
3708 }
3709
3710 /**
3711  * onenand_probe - [OneNAND Interface] Probe the OneNAND device
3712  * @param mtd           MTD device structure
3713  */
3714 static int onenand_probe(struct mtd_info *mtd)
3715 {
3716         struct onenand_chip *this = mtd->priv;
3717         int dev_id, ver_id;
3718         int density;
3719         int ret;
3720
3721         ret = this->chip_probe(mtd);
3722         if (ret)
3723                 return ret;
3724
3725         /* Device and version IDs from Register */
3726         dev_id = this->read_word(this->base + ONENAND_REG_DEVICE_ID);
3727         ver_id = this->read_word(this->base + ONENAND_REG_VERSION_ID);
3728         this->technology = this->read_word(this->base + ONENAND_REG_TECHNOLOGY);
3729
3730         /* Flash device information */
3731         onenand_print_device_info(dev_id, ver_id);
3732         this->device_id = dev_id;
3733         this->version_id = ver_id;
3734
3735         /* Check OneNAND features */
3736         onenand_check_features(mtd);
3737
3738         density = onenand_get_density(dev_id);
3739         if (FLEXONENAND(this)) {
3740                 this->dies = ONENAND_IS_DDP(this) ? 2 : 1;
3741                 /* Maximum possible erase regions */
3742                 mtd->numeraseregions = this->dies << 1;
3743                 mtd->eraseregions = kzalloc(sizeof(struct mtd_erase_region_info)
3744                                         * (this->dies << 1), GFP_KERNEL);
3745                 if (!mtd->eraseregions)
3746                         return -ENOMEM;
3747         }
3748
3749         /*
3750          * For Flex-OneNAND, chipsize represents maximum possible device size.
3751          * mtd->size represents the actual device size.
3752          */
3753         this->chipsize = (16 << density) << 20;
3754
3755         /* OneNAND page size & block size */
3756         /* The data buffer size is equal to page size */
3757         mtd->writesize = this->read_word(this->base + ONENAND_REG_DATA_BUFFER_SIZE);
3758         /* We use the full BufferRAM */
3759         if (ONENAND_IS_4KB_PAGE(this))
3760                 mtd->writesize <<= 1;
3761
3762         mtd->oobsize = mtd->writesize >> 5;
3763         /* Pages per a block are always 64 in OneNAND */
3764         mtd->erasesize = mtd->writesize << 6;
3765         /*
3766          * Flex-OneNAND SLC area has 64 pages per block.
3767          * Flex-OneNAND MLC area has 128 pages per block.
3768          * Expose MLC erase size to find erase_shift and page_mask.
3769          */
3770         if (FLEXONENAND(this))
3771                 mtd->erasesize <<= 1;
3772
3773         this->erase_shift = ffs(mtd->erasesize) - 1;
3774         this->page_shift = ffs(mtd->writesize) - 1;
3775         this->page_mask = (1 << (this->erase_shift - this->page_shift)) - 1;
3776         /* Set density mask. it is used for DDP */
3777         if (ONENAND_IS_DDP(this))
3778                 this->density_mask = this->chipsize >> (this->erase_shift + 1);
3779         /* It's real page size */
3780         this->writesize = mtd->writesize;
3781
3782         /* REVISIT: Multichip handling */
3783
3784         if (FLEXONENAND(this))
3785                 flexonenand_get_size(mtd);
3786         else
3787                 mtd->size = this->chipsize;
3788
3789         /*
3790          * We emulate the 4KiB page and 256KiB erase block size
3791          * But oobsize is still 64 bytes.
3792          * It is only valid if you turn on 2X program support,
3793          * Otherwise it will be ignored by compiler.
3794          */
3795         if (ONENAND_IS_2PLANE(this)) {
3796                 mtd->writesize <<= 1;
3797                 mtd->erasesize <<= 1;
3798         }
3799
3800         return 0;
3801 }
3802
3803 /**
3804  * onenand_suspend - [MTD Interface] Suspend the OneNAND flash
3805  * @param mtd           MTD device structure
3806  */
3807 static int onenand_suspend(struct mtd_info *mtd)
3808 {
3809         return onenand_get_device(mtd, FL_PM_SUSPENDED);
3810 }
3811
3812 /**
3813  * onenand_resume - [MTD Interface] Resume the OneNAND flash
3814  * @param mtd           MTD device structure
3815  */
3816 static void onenand_resume(struct mtd_info *mtd)
3817 {
3818         struct onenand_chip *this = mtd->priv;
3819
3820         if (this->state == FL_PM_SUSPENDED)
3821                 onenand_release_device(mtd);
3822         else
3823                 printk(KERN_ERR "%s: resume() called for the chip which is not "
3824                                 "in suspended state\n", __func__);
3825 }
3826
3827 /**
3828  * onenand_scan - [OneNAND Interface] Scan for the OneNAND device
3829  * @param mtd           MTD device structure
3830  * @param maxchips      Number of chips to scan for
3831  *
3832  * This fills out all the not initialized function pointers
3833  * with the defaults.
3834  * The flash ID is read and the mtd/chip structures are
3835  * filled with the appropriate values.
3836  */
3837 int onenand_scan(struct mtd_info *mtd, int maxchips)
3838 {
3839         int i, ret;
3840         struct onenand_chip *this = mtd->priv;
3841
3842         if (!this->read_word)
3843                 this->read_word = onenand_readw;
3844         if (!this->write_word)
3845                 this->write_word = onenand_writew;
3846
3847         if (!this->command)
3848                 this->command = onenand_command;
3849         if (!this->wait)
3850                 onenand_setup_wait(mtd);
3851         if (!this->bbt_wait)
3852                 this->bbt_wait = onenand_bbt_wait;
3853         if (!this->unlock_all)
3854                 this->unlock_all = onenand_unlock_all;
3855
3856         if (!this->chip_probe)
3857                 this->chip_probe = onenand_chip_probe;
3858
3859         if (!this->read_bufferram)
3860                 this->read_bufferram = onenand_read_bufferram;
3861         if (!this->write_bufferram)
3862                 this->write_bufferram = onenand_write_bufferram;
3863
3864         if (!this->block_markbad)
3865                 this->block_markbad = onenand_default_block_markbad;
3866         if (!this->scan_bbt)
3867                 this->scan_bbt = onenand_default_bbt;
3868
3869         if (onenand_probe(mtd))
3870                 return -ENXIO;
3871
3872         /* Set Sync. Burst Read after probing */
3873         if (this->mmcontrol) {
3874                 printk(KERN_INFO "OneNAND Sync. Burst Read support\n");
3875                 this->read_bufferram = onenand_sync_read_bufferram;
3876         }
3877
3878         /* Allocate buffers, if necessary */
3879         if (!this->page_buf) {
3880                 this->page_buf = kzalloc(mtd->writesize, GFP_KERNEL);
3881                 if (!this->page_buf)
3882                         return -ENOMEM;
3883 #ifdef CONFIG_MTD_ONENAND_VERIFY_WRITE
3884                 this->verify_buf = kzalloc(mtd->writesize, GFP_KERNEL);
3885                 if (!this->verify_buf) {
3886                         kfree(this->page_buf);
3887                         return -ENOMEM;
3888                 }
3889 #endif
3890                 this->options |= ONENAND_PAGEBUF_ALLOC;
3891         }
3892         if (!this->oob_buf) {
3893                 this->oob_buf = kzalloc(mtd->oobsize, GFP_KERNEL);
3894                 if (!this->oob_buf) {
3895                         if (this->options & ONENAND_PAGEBUF_ALLOC) {
3896                                 this->options &= ~ONENAND_PAGEBUF_ALLOC;
3897                                 kfree(this->page_buf);
3898                         }
3899                         return -ENOMEM;
3900                 }
3901                 this->options |= ONENAND_OOBBUF_ALLOC;
3902         }
3903
3904         this->state = FL_READY;
3905         init_waitqueue_head(&this->wq);
3906         spin_lock_init(&this->chip_lock);
3907
3908         /*
3909          * Allow subpage writes up to oobsize.
3910          */
3911         switch (mtd->oobsize) {
3912         case 128:
3913                 if (FLEXONENAND(this)) {
3914                         mtd_set_ooblayout(mtd, &flexonenand_ooblayout_ops);
3915                         mtd->subpage_sft = 0;
3916                 } else {
3917                         mtd_set_ooblayout(mtd, &onenand_oob_128_ooblayout_ops);
3918                         mtd->subpage_sft = 2;
3919                 }
3920                 if (ONENAND_IS_NOP_1(this))
3921                         mtd->subpage_sft = 0;
3922                 break;
3923         case 64:
3924                 mtd_set_ooblayout(mtd, &onenand_oob_32_64_ooblayout_ops);
3925                 mtd->subpage_sft = 2;
3926                 break;
3927
3928         case 32:
3929                 mtd_set_ooblayout(mtd, &onenand_oob_32_64_ooblayout_ops);
3930                 mtd->subpage_sft = 1;
3931                 break;
3932
3933         default:
3934                 printk(KERN_WARNING "%s: No OOB scheme defined for oobsize %d\n",
3935                         __func__, mtd->oobsize);
3936                 mtd->subpage_sft = 0;
3937                 /* To prevent kernel oops */
3938                 mtd_set_ooblayout(mtd, &onenand_oob_32_64_ooblayout_ops);
3939                 break;
3940         }
3941
3942         this->subpagesize = mtd->writesize >> mtd->subpage_sft;
3943
3944         /*
3945          * The number of bytes available for a client to place data into
3946          * the out of band area
3947          */
3948         ret = mtd_ooblayout_count_freebytes(mtd);
3949         if (ret < 0)
3950                 ret = 0;
3951
3952         mtd->oobavail = ret;
3953
3954         mtd->ecc_strength = 1;
3955
3956         /* Fill in remaining MTD driver data */
3957         mtd->type = ONENAND_IS_MLC(this) ? MTD_MLCNANDFLASH : MTD_NANDFLASH;
3958         mtd->flags = MTD_CAP_NANDFLASH;
3959         mtd->_erase = onenand_erase;
3960         mtd->_point = NULL;
3961         mtd->_unpoint = NULL;
3962         mtd->_read_oob = onenand_read_oob;
3963         mtd->_write_oob = onenand_write_oob;
3964         mtd->_panic_write = onenand_panic_write;
3965 #ifdef CONFIG_MTD_ONENAND_OTP
3966         mtd->_get_fact_prot_info = onenand_get_fact_prot_info;
3967         mtd->_read_fact_prot_reg = onenand_read_fact_prot_reg;
3968         mtd->_get_user_prot_info = onenand_get_user_prot_info;
3969         mtd->_read_user_prot_reg = onenand_read_user_prot_reg;
3970         mtd->_write_user_prot_reg = onenand_write_user_prot_reg;
3971         mtd->_lock_user_prot_reg = onenand_lock_user_prot_reg;
3972 #endif
3973         mtd->_sync = onenand_sync;
3974         mtd->_lock = onenand_lock;
3975         mtd->_unlock = onenand_unlock;
3976         mtd->_suspend = onenand_suspend;
3977         mtd->_resume = onenand_resume;
3978         mtd->_block_isbad = onenand_block_isbad;
3979         mtd->_block_markbad = onenand_block_markbad;
3980         mtd->owner = THIS_MODULE;
3981         mtd->writebufsize = mtd->writesize;
3982
3983         /* Unlock whole block */
3984         if (!(this->options & ONENAND_SKIP_INITIAL_UNLOCKING))
3985                 this->unlock_all(mtd);
3986
3987         ret = this->scan_bbt(mtd);
3988         if ((!FLEXONENAND(this)) || ret)
3989                 return ret;
3990
3991         /* Change Flex-OneNAND boundaries if required */
3992         for (i = 0; i < MAX_DIES; i++)
3993                 flexonenand_set_boundary(mtd, i, flex_bdry[2 * i],
3994                                                  flex_bdry[(2 * i) + 1]);
3995
3996         return 0;
3997 }
3998
3999 /**
4000  * onenand_release - [OneNAND Interface] Free resources held by the OneNAND device
4001  * @param mtd           MTD device structure
4002  */
4003 void onenand_release(struct mtd_info *mtd)
4004 {
4005         struct onenand_chip *this = mtd->priv;
4006
4007         /* Deregister partitions */
4008         mtd_device_unregister(mtd);
4009
4010         /* Free bad block table memory, if allocated */
4011         if (this->bbm) {
4012                 struct bbm_info *bbm = this->bbm;
4013                 kfree(bbm->bbt);
4014                 kfree(this->bbm);
4015         }
4016         /* Buffers allocated by onenand_scan */
4017         if (this->options & ONENAND_PAGEBUF_ALLOC) {
4018                 kfree(this->page_buf);
4019 #ifdef CONFIG_MTD_ONENAND_VERIFY_WRITE
4020                 kfree(this->verify_buf);
4021 #endif
4022         }
4023         if (this->options & ONENAND_OOBBUF_ALLOC)
4024                 kfree(this->oob_buf);
4025         kfree(mtd->eraseregions);
4026 }
4027
4028 EXPORT_SYMBOL_GPL(onenand_scan);
4029 EXPORT_SYMBOL_GPL(onenand_release);
4030
4031 MODULE_LICENSE("GPL");
4032 MODULE_AUTHOR("Kyungmin Park <kyungmin.park@samsung.com>");
4033 MODULE_DESCRIPTION("Generic OneNAND flash driver code");