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