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