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