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