]> bbs.cooldavid.org Git - net-next-2.6.git/blob - drivers/mtd/onenand/onenand_base.c
[MTD] [OneNAND] Use pre-alloced oob buffer instead of local buffer
[net-next-2.6.git] / 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  * This program is free software; you can redistribute it and/or modify
13  * it under the terms of the GNU General Public License version 2 as
14  * published by the Free Software Foundation.
15  */
16
17 #include <linux/kernel.h>
18 #include <linux/module.h>
19 #include <linux/init.h>
20 #include <linux/sched.h>
21 #include <linux/interrupt.h>
22 #include <linux/jiffies.h>
23 #include <linux/mtd/mtd.h>
24 #include <linux/mtd/onenand.h>
25 #include <linux/mtd/partitions.h>
26
27 #include <asm/io.h>
28
29 /**
30  * onenand_oob_64 - oob info for large (2KB) page
31  */
32 static struct nand_ecclayout onenand_oob_64 = {
33         .eccbytes       = 20,
34         .eccpos         = {
35                 8, 9, 10, 11, 12,
36                 24, 25, 26, 27, 28,
37                 40, 41, 42, 43, 44,
38                 56, 57, 58, 59, 60,
39                 },
40         .oobfree        = {
41                 {2, 3}, {14, 2}, {18, 3}, {30, 2},
42                 {34, 3}, {46, 2}, {50, 3}, {62, 2}
43         }
44 };
45
46 /**
47  * onenand_oob_32 - oob info for middle (1KB) page
48  */
49 static struct nand_ecclayout onenand_oob_32 = {
50         .eccbytes       = 10,
51         .eccpos         = {
52                 8, 9, 10, 11, 12,
53                 24, 25, 26, 27, 28,
54                 },
55         .oobfree        = { {2, 3}, {14, 2}, {18, 3}, {30, 2} }
56 };
57
58 static const unsigned char ffchars[] = {
59         0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
60         0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 16 */
61         0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
62         0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 32 */
63         0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
64         0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 48 */
65         0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
66         0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 64 */
67 };
68
69 /**
70  * onenand_readw - [OneNAND Interface] Read OneNAND register
71  * @param addr          address to read
72  *
73  * Read OneNAND register
74  */
75 static unsigned short onenand_readw(void __iomem *addr)
76 {
77         return readw(addr);
78 }
79
80 /**
81  * onenand_writew - [OneNAND Interface] Write OneNAND register with value
82  * @param value         value to write
83  * @param addr          address to write
84  *
85  * Write OneNAND register with value
86  */
87 static void onenand_writew(unsigned short value, void __iomem *addr)
88 {
89         writew(value, addr);
90 }
91
92 /**
93  * onenand_block_address - [DEFAULT] Get block address
94  * @param this          onenand chip data structure
95  * @param block         the block
96  * @return              translated block address if DDP, otherwise same
97  *
98  * Setup Start Address 1 Register (F100h)
99  */
100 static int onenand_block_address(struct onenand_chip *this, int block)
101 {
102         /* Device Flash Core select, NAND Flash Block Address */
103         if (block & this->density_mask)
104                 return ONENAND_DDP_CHIP1 | (block ^ this->density_mask);
105
106         return block;
107 }
108
109 /**
110  * onenand_bufferram_address - [DEFAULT] Get bufferram address
111  * @param this          onenand chip data structure
112  * @param block         the block
113  * @return              set DBS value if DDP, otherwise 0
114  *
115  * Setup Start Address 2 Register (F101h) for DDP
116  */
117 static int onenand_bufferram_address(struct onenand_chip *this, int block)
118 {
119         /* Device BufferRAM Select */
120         if (block & this->density_mask)
121                 return ONENAND_DDP_CHIP1;
122
123         return ONENAND_DDP_CHIP0;
124 }
125
126 /**
127  * onenand_page_address - [DEFAULT] Get page address
128  * @param page          the page address
129  * @param sector        the sector address
130  * @return              combined page and sector address
131  *
132  * Setup Start Address 8 Register (F107h)
133  */
134 static int onenand_page_address(int page, int sector)
135 {
136         /* Flash Page Address, Flash Sector Address */
137         int fpa, fsa;
138
139         fpa = page & ONENAND_FPA_MASK;
140         fsa = sector & ONENAND_FSA_MASK;
141
142         return ((fpa << ONENAND_FPA_SHIFT) | fsa);
143 }
144
145 /**
146  * onenand_buffer_address - [DEFAULT] Get buffer address
147  * @param dataram1      DataRAM index
148  * @param sectors       the sector address
149  * @param count         the number of sectors
150  * @return              the start buffer value
151  *
152  * Setup Start Buffer Register (F200h)
153  */
154 static int onenand_buffer_address(int dataram1, int sectors, int count)
155 {
156         int bsa, bsc;
157
158         /* BufferRAM Sector Address */
159         bsa = sectors & ONENAND_BSA_MASK;
160
161         if (dataram1)
162                 bsa |= ONENAND_BSA_DATARAM1;    /* DataRAM1 */
163         else
164                 bsa |= ONENAND_BSA_DATARAM0;    /* DataRAM0 */
165
166         /* BufferRAM Sector Count */
167         bsc = count & ONENAND_BSC_MASK;
168
169         return ((bsa << ONENAND_BSA_SHIFT) | bsc);
170 }
171
172 /**
173  * onenand_get_density - [DEFAULT] Get OneNAND density
174  * @param dev_id        OneNAND device ID
175  *
176  * Get OneNAND density from device ID
177  */
178 static inline int onenand_get_density(int dev_id)
179 {
180         int density = dev_id >> ONENAND_DEVICE_DENSITY_SHIFT;
181         return (density & ONENAND_DEVICE_DENSITY_MASK);
182 }
183
184 /**
185  * onenand_command - [DEFAULT] Send command to OneNAND device
186  * @param mtd           MTD device structure
187  * @param cmd           the command to be sent
188  * @param addr          offset to read from or write to
189  * @param len           number of bytes to read or write
190  *
191  * Send command to OneNAND device. This function is used for middle/large page
192  * devices (1KB/2KB Bytes per page)
193  */
194 static int onenand_command(struct mtd_info *mtd, int cmd, loff_t addr, size_t len)
195 {
196         struct onenand_chip *this = mtd->priv;
197         int value, block, page;
198
199         /* Address translation */
200         switch (cmd) {
201         case ONENAND_CMD_UNLOCK:
202         case ONENAND_CMD_LOCK:
203         case ONENAND_CMD_LOCK_TIGHT:
204         case ONENAND_CMD_UNLOCK_ALL:
205                 block = -1;
206                 page = -1;
207                 break;
208
209         case ONENAND_CMD_ERASE:
210         case ONENAND_CMD_BUFFERRAM:
211         case ONENAND_CMD_OTP_ACCESS:
212                 block = (int) (addr >> this->erase_shift);
213                 page = -1;
214                 break;
215
216         default:
217                 block = (int) (addr >> this->erase_shift);
218                 page = (int) (addr >> this->page_shift);
219
220                 if (ONENAND_IS_2PLANE(this)) {
221                         /* Make the even block number */
222                         block &= ~1;
223                         /* Is it the odd plane? */
224                         if (addr & this->writesize)
225                                 block++;
226                         page >>= 1;
227                 }
228                 page &= this->page_mask;
229                 break;
230         }
231
232         /* NOTE: The setting order of the registers is very important! */
233         if (cmd == ONENAND_CMD_BUFFERRAM) {
234                 /* Select DataRAM for DDP */
235                 value = onenand_bufferram_address(this, block);
236                 this->write_word(value, this->base + ONENAND_REG_START_ADDRESS2);
237
238                 if (ONENAND_IS_2PLANE(this))
239                         /* It is always BufferRAM0 */
240                         ONENAND_SET_BUFFERRAM0(this);
241                 else
242                         /* Switch to the next data buffer */
243                         ONENAND_SET_NEXT_BUFFERRAM(this);
244
245                 return 0;
246         }
247
248         if (block != -1) {
249                 /* Write 'DFS, FBA' of Flash */
250                 value = onenand_block_address(this, block);
251                 this->write_word(value, this->base + ONENAND_REG_START_ADDRESS1);
252
253                 /* Select DataRAM for DDP */
254                 value = onenand_bufferram_address(this, block);
255                 this->write_word(value, this->base + ONENAND_REG_START_ADDRESS2);
256         }
257
258         if (page != -1) {
259                 /* Now we use page size operation */
260                 int sectors = 4, count = 4;
261                 int dataram;
262
263                 switch (cmd) {
264                 case ONENAND_CMD_READ:
265                 case ONENAND_CMD_READOOB:
266                         dataram = ONENAND_SET_NEXT_BUFFERRAM(this);
267                         break;
268
269                 default:
270                         if (ONENAND_IS_2PLANE(this) && cmd == ONENAND_CMD_PROG)
271                                 cmd = ONENAND_CMD_2X_PROG;
272                         dataram = ONENAND_CURRENT_BUFFERRAM(this);
273                         break;
274                 }
275
276                 /* Write 'FPA, FSA' of Flash */
277                 value = onenand_page_address(page, sectors);
278                 this->write_word(value, this->base + ONENAND_REG_START_ADDRESS8);
279
280                 /* Write 'BSA, BSC' of DataRAM */
281                 value = onenand_buffer_address(dataram, sectors, count);
282                 this->write_word(value, this->base + ONENAND_REG_START_BUFFER);
283         }
284
285         /* Interrupt clear */
286         this->write_word(ONENAND_INT_CLEAR, this->base + ONENAND_REG_INTERRUPT);
287
288         /* Write command */
289         this->write_word(cmd, this->base + ONENAND_REG_COMMAND);
290
291         return 0;
292 }
293
294 /**
295  * onenand_wait - [DEFAULT] wait until the command is done
296  * @param mtd           MTD device structure
297  * @param state         state to select the max. timeout value
298  *
299  * Wait for command done. This applies to all OneNAND command
300  * Read can take up to 30us, erase up to 2ms and program up to 350us
301  * according to general OneNAND specs
302  */
303 static int onenand_wait(struct mtd_info *mtd, int state)
304 {
305         struct onenand_chip * this = mtd->priv;
306         unsigned long timeout;
307         unsigned int flags = ONENAND_INT_MASTER;
308         unsigned int interrupt = 0;
309         unsigned int ctrl;
310
311         /* The 20 msec is enough */
312         timeout = jiffies + msecs_to_jiffies(20);
313         while (time_before(jiffies, timeout)) {
314                 interrupt = this->read_word(this->base + ONENAND_REG_INTERRUPT);
315
316                 if (interrupt & flags)
317                         break;
318
319                 if (state != FL_READING)
320                         cond_resched();
321         }
322         /* To get correct interrupt status in timeout case */
323         interrupt = this->read_word(this->base + ONENAND_REG_INTERRUPT);
324
325         ctrl = this->read_word(this->base + ONENAND_REG_CTRL_STATUS);
326
327         if (ctrl & ONENAND_CTRL_ERROR) {
328                 printk(KERN_ERR "onenand_wait: controller error = 0x%04x\n", ctrl);
329                 if (ctrl & ONENAND_CTRL_LOCK)
330                         printk(KERN_ERR "onenand_wait: it's locked error.\n");
331                 return -EIO;
332         }
333
334         if (interrupt & ONENAND_INT_READ) {
335                 int ecc = this->read_word(this->base + ONENAND_REG_ECC_STATUS);
336                 if (ecc) {
337                         if (ecc & ONENAND_ECC_2BIT_ALL) {
338                                 printk(KERN_ERR "onenand_wait: ECC error = 0x%04x\n", ecc);
339                                 mtd->ecc_stats.failed++;
340                                 return -EBADMSG;
341                         } else if (ecc & ONENAND_ECC_1BIT_ALL) {
342                                 printk(KERN_INFO "onenand_wait: correctable ECC error = 0x%04x\n", ecc);
343                                 mtd->ecc_stats.corrected++;
344                         }
345                 }
346         } else if (state == FL_READING) {
347                 printk(KERN_ERR "onenand_wait: read timeout! ctrl=0x%04x intr=0x%04x\n", ctrl, interrupt);
348                 return -EIO;
349         }
350
351         return 0;
352 }
353
354 /*
355  * onenand_interrupt - [DEFAULT] onenand interrupt handler
356  * @param irq           onenand interrupt number
357  * @param dev_id        interrupt data
358  *
359  * complete the work
360  */
361 static irqreturn_t onenand_interrupt(int irq, void *data)
362 {
363         struct onenand_chip *this = data;
364
365         /* To handle shared interrupt */
366         if (!this->complete.done)
367                 complete(&this->complete);
368
369         return IRQ_HANDLED;
370 }
371
372 /*
373  * onenand_interrupt_wait - [DEFAULT] wait until the command is done
374  * @param mtd           MTD device structure
375  * @param state         state to select the max. timeout value
376  *
377  * Wait for command done.
378  */
379 static int onenand_interrupt_wait(struct mtd_info *mtd, int state)
380 {
381         struct onenand_chip *this = mtd->priv;
382
383         wait_for_completion(&this->complete);
384
385         return onenand_wait(mtd, state);
386 }
387
388 /*
389  * onenand_try_interrupt_wait - [DEFAULT] try interrupt wait
390  * @param mtd           MTD device structure
391  * @param state         state to select the max. timeout value
392  *
393  * Try interrupt based wait (It is used one-time)
394  */
395 static int onenand_try_interrupt_wait(struct mtd_info *mtd, int state)
396 {
397         struct onenand_chip *this = mtd->priv;
398         unsigned long remain, timeout;
399
400         /* We use interrupt wait first */
401         this->wait = onenand_interrupt_wait;
402
403         timeout = msecs_to_jiffies(100);
404         remain = wait_for_completion_timeout(&this->complete, timeout);
405         if (!remain) {
406                 printk(KERN_INFO "OneNAND: There's no interrupt. "
407                                 "We use the normal wait\n");
408
409                 /* Release the irq */
410                 free_irq(this->irq, this);
411
412                 this->wait = onenand_wait;
413         }
414
415         return onenand_wait(mtd, state);
416 }
417
418 /*
419  * onenand_setup_wait - [OneNAND Interface] setup onenand wait method
420  * @param mtd           MTD device structure
421  *
422  * There's two method to wait onenand work
423  * 1. polling - read interrupt status register
424  * 2. interrupt - use the kernel interrupt method
425  */
426 static void onenand_setup_wait(struct mtd_info *mtd)
427 {
428         struct onenand_chip *this = mtd->priv;
429         int syscfg;
430
431         init_completion(&this->complete);
432
433         if (this->irq <= 0) {
434                 this->wait = onenand_wait;
435                 return;
436         }
437
438         if (request_irq(this->irq, &onenand_interrupt,
439                                 IRQF_SHARED, "onenand", this)) {
440                 /* If we can't get irq, use the normal wait */
441                 this->wait = onenand_wait;
442                 return;
443         }
444
445         /* Enable interrupt */
446         syscfg = this->read_word(this->base + ONENAND_REG_SYS_CFG1);
447         syscfg |= ONENAND_SYS_CFG1_IOBE;
448         this->write_word(syscfg, this->base + ONENAND_REG_SYS_CFG1);
449
450         this->wait = onenand_try_interrupt_wait;
451 }
452
453 /**
454  * onenand_bufferram_offset - [DEFAULT] BufferRAM offset
455  * @param mtd           MTD data structure
456  * @param area          BufferRAM area
457  * @return              offset given area
458  *
459  * Return BufferRAM offset given area
460  */
461 static inline int onenand_bufferram_offset(struct mtd_info *mtd, int area)
462 {
463         struct onenand_chip *this = mtd->priv;
464
465         if (ONENAND_CURRENT_BUFFERRAM(this)) {
466                 /* Note: the 'this->writesize' is a real page size */
467                 if (area == ONENAND_DATARAM)
468                         return this->writesize;
469                 if (area == ONENAND_SPARERAM)
470                         return mtd->oobsize;
471         }
472
473         return 0;
474 }
475
476 /**
477  * onenand_read_bufferram - [OneNAND Interface] Read the bufferram area
478  * @param mtd           MTD data structure
479  * @param area          BufferRAM area
480  * @param buffer        the databuffer to put/get data
481  * @param offset        offset to read from or write to
482  * @param count         number of bytes to read/write
483  *
484  * Read the BufferRAM area
485  */
486 static int onenand_read_bufferram(struct mtd_info *mtd, int area,
487                 unsigned char *buffer, int offset, size_t count)
488 {
489         struct onenand_chip *this = mtd->priv;
490         void __iomem *bufferram;
491
492         bufferram = this->base + area;
493
494         bufferram += onenand_bufferram_offset(mtd, area);
495
496         if (ONENAND_CHECK_BYTE_ACCESS(count)) {
497                 unsigned short word;
498
499                 /* Align with word(16-bit) size */
500                 count--;
501
502                 /* Read word and save byte */
503                 word = this->read_word(bufferram + offset + count);
504                 buffer[count] = (word & 0xff);
505         }
506
507         memcpy(buffer, bufferram + offset, count);
508
509         return 0;
510 }
511
512 /**
513  * onenand_sync_read_bufferram - [OneNAND Interface] Read the bufferram area with Sync. Burst mode
514  * @param mtd           MTD data structure
515  * @param area          BufferRAM area
516  * @param buffer        the databuffer to put/get data
517  * @param offset        offset to read from or write to
518  * @param count         number of bytes to read/write
519  *
520  * Read the BufferRAM area with Sync. Burst Mode
521  */
522 static int onenand_sync_read_bufferram(struct mtd_info *mtd, int area,
523                 unsigned char *buffer, int offset, size_t count)
524 {
525         struct onenand_chip *this = mtd->priv;
526         void __iomem *bufferram;
527
528         bufferram = this->base + area;
529
530         bufferram += onenand_bufferram_offset(mtd, area);
531
532         this->mmcontrol(mtd, ONENAND_SYS_CFG1_SYNC_READ);
533
534         if (ONENAND_CHECK_BYTE_ACCESS(count)) {
535                 unsigned short word;
536
537                 /* Align with word(16-bit) size */
538                 count--;
539
540                 /* Read word and save byte */
541                 word = this->read_word(bufferram + offset + count);
542                 buffer[count] = (word & 0xff);
543         }
544
545         memcpy(buffer, bufferram + offset, count);
546
547         this->mmcontrol(mtd, 0);
548
549         return 0;
550 }
551
552 /**
553  * onenand_write_bufferram - [OneNAND Interface] Write the bufferram area
554  * @param mtd           MTD data structure
555  * @param area          BufferRAM area
556  * @param buffer        the databuffer to put/get data
557  * @param offset        offset to read from or write to
558  * @param count         number of bytes to read/write
559  *
560  * Write the BufferRAM area
561  */
562 static int onenand_write_bufferram(struct mtd_info *mtd, int area,
563                 const unsigned char *buffer, int offset, size_t count)
564 {
565         struct onenand_chip *this = mtd->priv;
566         void __iomem *bufferram;
567
568         bufferram = this->base + area;
569
570         bufferram += onenand_bufferram_offset(mtd, area);
571
572         if (ONENAND_CHECK_BYTE_ACCESS(count)) {
573                 unsigned short word;
574                 int byte_offset;
575
576                 /* Align with word(16-bit) size */
577                 count--;
578
579                 /* Calculate byte access offset */
580                 byte_offset = offset + count;
581
582                 /* Read word and save byte */
583                 word = this->read_word(bufferram + byte_offset);
584                 word = (word & ~0xff) | buffer[count];
585                 this->write_word(word, bufferram + byte_offset);
586         }
587
588         memcpy(bufferram + offset, buffer, count);
589
590         return 0;
591 }
592
593 /**
594  * onenand_get_2x_blockpage - [GENERIC] Get blockpage at 2x program mode
595  * @param mtd           MTD data structure
596  * @param addr          address to check
597  * @return              blockpage address
598  *
599  * Get blockpage address at 2x program mode
600  */
601 static int onenand_get_2x_blockpage(struct mtd_info *mtd, loff_t addr)
602 {
603         struct onenand_chip *this = mtd->priv;
604         int blockpage, block, page;
605
606         /* Calculate the even block number */
607         block = (int) (addr >> this->erase_shift) & ~1;
608         /* Is it the odd plane? */
609         if (addr & this->writesize)
610                 block++;
611         page = (int) (addr >> (this->page_shift + 1)) & this->page_mask;
612         blockpage = (block << 7) | page;
613
614         return blockpage;
615 }
616
617 /**
618  * onenand_check_bufferram - [GENERIC] Check BufferRAM information
619  * @param mtd           MTD data structure
620  * @param addr          address to check
621  * @return              1 if there are valid data, otherwise 0
622  *
623  * Check bufferram if there is data we required
624  */
625 static int onenand_check_bufferram(struct mtd_info *mtd, loff_t addr)
626 {
627         struct onenand_chip *this = mtd->priv;
628         int blockpage, found = 0;
629         unsigned int i;
630
631         if (ONENAND_IS_2PLANE(this))
632                 blockpage = onenand_get_2x_blockpage(mtd, addr);
633         else
634                 blockpage = (int) (addr >> this->page_shift);
635
636         /* Is there valid data? */
637         i = ONENAND_CURRENT_BUFFERRAM(this);
638         if (this->bufferram[i].blockpage == blockpage)
639                 found = 1;
640         else {
641                 /* Check another BufferRAM */
642                 i = ONENAND_NEXT_BUFFERRAM(this);
643                 if (this->bufferram[i].blockpage == blockpage) {
644                         ONENAND_SET_NEXT_BUFFERRAM(this);
645                         found = 1;
646                 }
647         }
648
649         if (found && ONENAND_IS_DDP(this)) {
650                 /* Select DataRAM for DDP */
651                 int block = (int) (addr >> this->erase_shift);
652                 int value = onenand_bufferram_address(this, block);
653                 this->write_word(value, this->base + ONENAND_REG_START_ADDRESS2);
654         }
655
656         return found;
657 }
658
659 /**
660  * onenand_update_bufferram - [GENERIC] Update BufferRAM information
661  * @param mtd           MTD data structure
662  * @param addr          address to update
663  * @param valid         valid flag
664  *
665  * Update BufferRAM information
666  */
667 static void onenand_update_bufferram(struct mtd_info *mtd, loff_t addr,
668                 int valid)
669 {
670         struct onenand_chip *this = mtd->priv;
671         int blockpage;
672         unsigned int i;
673
674         if (ONENAND_IS_2PLANE(this))
675                 blockpage = onenand_get_2x_blockpage(mtd, addr);
676         else
677                 blockpage = (int) (addr >> this->page_shift);
678
679         /* Invalidate another BufferRAM */
680         i = ONENAND_NEXT_BUFFERRAM(this);
681         if (this->bufferram[i].blockpage == blockpage)
682                 this->bufferram[i].blockpage = -1;
683
684         /* Update BufferRAM */
685         i = ONENAND_CURRENT_BUFFERRAM(this);
686         if (valid)
687                 this->bufferram[i].blockpage = blockpage;
688         else
689                 this->bufferram[i].blockpage = -1;
690 }
691
692 /**
693  * onenand_invalidate_bufferram - [GENERIC] Invalidate BufferRAM information
694  * @param mtd           MTD data structure
695  * @param addr          start address to invalidate
696  * @param len           length to invalidate
697  *
698  * Invalidate BufferRAM information
699  */
700 static void onenand_invalidate_bufferram(struct mtd_info *mtd, loff_t addr,
701                 unsigned int len)
702 {
703         struct onenand_chip *this = mtd->priv;
704         int i;
705         loff_t end_addr = addr + len;
706
707         /* Invalidate BufferRAM */
708         for (i = 0; i < MAX_BUFFERRAM; i++) {
709                 loff_t buf_addr = this->bufferram[i].blockpage << this->page_shift;
710                 if (buf_addr >= addr && buf_addr < end_addr)
711                         this->bufferram[i].blockpage = -1;
712         }
713 }
714
715 /**
716  * onenand_get_device - [GENERIC] Get chip for selected access
717  * @param mtd           MTD device structure
718  * @param new_state     the state which is requested
719  *
720  * Get the device and lock it for exclusive access
721  */
722 static int onenand_get_device(struct mtd_info *mtd, int new_state)
723 {
724         struct onenand_chip *this = mtd->priv;
725         DECLARE_WAITQUEUE(wait, current);
726
727         /*
728          * Grab the lock and see if the device is available
729          */
730         while (1) {
731                 spin_lock(&this->chip_lock);
732                 if (this->state == FL_READY) {
733                         this->state = new_state;
734                         spin_unlock(&this->chip_lock);
735                         break;
736                 }
737                 if (new_state == FL_PM_SUSPENDED) {
738                         spin_unlock(&this->chip_lock);
739                         return (this->state == FL_PM_SUSPENDED) ? 0 : -EAGAIN;
740                 }
741                 set_current_state(TASK_UNINTERRUPTIBLE);
742                 add_wait_queue(&this->wq, &wait);
743                 spin_unlock(&this->chip_lock);
744                 schedule();
745                 remove_wait_queue(&this->wq, &wait);
746         }
747
748         return 0;
749 }
750
751 /**
752  * onenand_release_device - [GENERIC] release chip
753  * @param mtd           MTD device structure
754  *
755  * Deselect, release chip lock and wake up anyone waiting on the device
756  */
757 static void onenand_release_device(struct mtd_info *mtd)
758 {
759         struct onenand_chip *this = mtd->priv;
760
761         /* Release the chip */
762         spin_lock(&this->chip_lock);
763         this->state = FL_READY;
764         wake_up(&this->wq);
765         spin_unlock(&this->chip_lock);
766 }
767
768 /**
769  * onenand_transfer_auto_oob - [Internal] oob auto-placement transfer
770  * @param mtd           MTD device structure
771  * @param buf           destination address
772  * @param column        oob offset to read from
773  * @param thislen       oob length to read
774  */
775 static int onenand_transfer_auto_oob(struct mtd_info *mtd, uint8_t *buf, int column,
776                                 int thislen)
777 {
778         struct onenand_chip *this = mtd->priv;
779         struct nand_oobfree *free;
780         int readcol = column;
781         int readend = column + thislen;
782         int lastgap = 0;
783         unsigned int i;
784         uint8_t *oob_buf = this->oob_buf;
785
786         free = this->ecclayout->oobfree;
787         for (i = 0; i < MTD_MAX_OOBFREE_ENTRIES && free->length; i++, free++) {
788                 if (readcol >= lastgap)
789                         readcol += free->offset - lastgap;
790                 if (readend >= lastgap)
791                         readend += free->offset - lastgap;
792                 lastgap = free->offset + free->length;
793         }
794         this->read_bufferram(mtd, ONENAND_SPARERAM, oob_buf, 0, mtd->oobsize);
795         free = this->ecclayout->oobfree;
796         for (i = 0; i < MTD_MAX_OOBFREE_ENTRIES && free->length; i++, free++) {
797                 int free_end = free->offset + free->length;
798                 if (free->offset < readend && free_end > readcol) {
799                         int st = max_t(int,free->offset,readcol);
800                         int ed = min_t(int,free_end,readend);
801                         int n = ed - st;
802                         memcpy(buf, oob_buf + st, n);
803                         buf += n;
804                 } else if (column == 0)
805                         break;
806         }
807         return 0;
808 }
809
810 /**
811  * onenand_read_ops_nolock - [OneNAND Interface] OneNAND read main and/or out-of-band
812  * @param mtd           MTD device structure
813  * @param from          offset to read from
814  * @param ops:          oob operation description structure
815  *
816  * OneNAND read main and/or out-of-band data
817  */
818 static int onenand_read_ops_nolock(struct mtd_info *mtd, loff_t from,
819                                 struct mtd_oob_ops *ops)
820 {
821         struct onenand_chip *this = mtd->priv;
822         struct mtd_ecc_stats stats;
823         size_t len = ops->len;
824         size_t ooblen = ops->ooblen;
825         u_char *buf = ops->datbuf;
826         u_char *oobbuf = ops->oobbuf;
827         int read = 0, column, thislen;
828         int oobread = 0, oobcolumn, thisooblen, oobsize;
829         int ret = 0, boundary = 0;
830         int writesize = this->writesize;
831
832         DEBUG(MTD_DEBUG_LEVEL3, "onenand_read_ops_nolock: from = 0x%08x, len = %i\n", (unsigned int) from, (int) len);
833
834         if (ops->mode == MTD_OOB_AUTO)
835                 oobsize = this->ecclayout->oobavail;
836         else
837                 oobsize = mtd->oobsize;
838
839         oobcolumn = from & (mtd->oobsize - 1);
840
841         /* Do not allow reads past end of device */
842         if ((from + len) > mtd->size) {
843                 printk(KERN_ERR "onenand_read_ops_nolock: Attempt read beyond end of device\n");
844                 ops->retlen = 0;
845                 ops->oobretlen = 0;
846                 return -EINVAL;
847         }
848
849         stats = mtd->ecc_stats;
850
851         /* Read-while-load method */
852
853         /* Do first load to bufferRAM */
854         if (read < len) {
855                 if (!onenand_check_bufferram(mtd, from)) {
856                         this->command(mtd, ONENAND_CMD_READ, from, writesize);
857                         ret = this->wait(mtd, FL_READING);
858                         onenand_update_bufferram(mtd, from, !ret);
859                         if (ret == -EBADMSG)
860                                 ret = 0;
861                 }
862         }
863
864         thislen = min_t(int, writesize, len - read);
865         column = from & (writesize - 1);
866         if (column + thislen > writesize)
867                 thislen = writesize - column;
868
869         while (!ret) {
870                 /* If there is more to load then start next load */
871                 from += thislen;
872                 if (read + thislen < len) {
873                         this->command(mtd, ONENAND_CMD_READ, from, writesize);
874                         /*
875                          * Chip boundary handling in DDP
876                          * Now we issued chip 1 read and pointed chip 1
877                          * bufferam so we have to point chip 0 bufferam.
878                          */
879                         if (ONENAND_IS_DDP(this) &&
880                             unlikely(from == (this->chipsize >> 1))) {
881                                 this->write_word(ONENAND_DDP_CHIP0, this->base + ONENAND_REG_START_ADDRESS2);
882                                 boundary = 1;
883                         } else
884                                 boundary = 0;
885                         ONENAND_SET_PREV_BUFFERRAM(this);
886                 }
887                 /* While load is going, read from last bufferRAM */
888                 this->read_bufferram(mtd, ONENAND_DATARAM, buf, column, thislen);
889
890                 /* Read oob area if needed */
891                 if (oobbuf) {
892                         thisooblen = oobsize - oobcolumn;
893                         thisooblen = min_t(int, thisooblen, ooblen - oobread);
894
895                         if (ops->mode == MTD_OOB_AUTO)
896                                 onenand_transfer_auto_oob(mtd, oobbuf, oobcolumn, thisooblen);
897                         else
898                                 this->read_bufferram(mtd, ONENAND_SPARERAM, oobbuf, oobcolumn, thisooblen);
899                         oobread += thisooblen;
900                         oobbuf += thisooblen;
901                         oobcolumn = 0;
902                 }
903
904                 /* See if we are done */
905                 read += thislen;
906                 if (read == len)
907                         break;
908                 /* Set up for next read from bufferRAM */
909                 if (unlikely(boundary))
910                         this->write_word(ONENAND_DDP_CHIP1, this->base + ONENAND_REG_START_ADDRESS2);
911                 ONENAND_SET_NEXT_BUFFERRAM(this);
912                 buf += thislen;
913                 thislen = min_t(int, writesize, len - read);
914                 column = 0;
915                 cond_resched();
916                 /* Now wait for load */
917                 ret = this->wait(mtd, FL_READING);
918                 onenand_update_bufferram(mtd, from, !ret);
919                 if (ret == -EBADMSG)
920                         ret = 0;
921         }
922
923         /*
924          * Return success, if no ECC failures, else -EBADMSG
925          * fs driver will take care of that, because
926          * retlen == desired len and result == -EBADMSG
927          */
928         ops->retlen = read;
929         ops->oobretlen = oobread;
930
931         if (ret)
932                 return ret;
933
934         if (mtd->ecc_stats.failed - stats.failed)
935                 return -EBADMSG;
936
937         return mtd->ecc_stats.corrected - stats.corrected ? -EUCLEAN : 0;
938 }
939
940 /**
941  * onenand_read_oob_nolock - [MTD Interface] OneNAND read out-of-band
942  * @param mtd           MTD device structure
943  * @param from          offset to read from
944  * @param ops:          oob operation description structure
945  *
946  * OneNAND read out-of-band data from the spare area
947  */
948 static int onenand_read_oob_nolock(struct mtd_info *mtd, loff_t from,
949                         struct mtd_oob_ops *ops)
950 {
951         struct onenand_chip *this = mtd->priv;
952         struct mtd_ecc_stats stats;
953         int read = 0, thislen, column, oobsize;
954         size_t len = ops->ooblen;
955         mtd_oob_mode_t mode = ops->mode;
956         u_char *buf = ops->oobbuf;
957         int ret = 0;
958
959         from += ops->ooboffs;
960
961         DEBUG(MTD_DEBUG_LEVEL3, "onenand_read_oob_nolock: from = 0x%08x, len = %i\n", (unsigned int) from, (int) len);
962
963         /* Initialize return length value */
964         ops->oobretlen = 0;
965
966         if (mode == MTD_OOB_AUTO)
967                 oobsize = this->ecclayout->oobavail;
968         else
969                 oobsize = mtd->oobsize;
970
971         column = from & (mtd->oobsize - 1);
972
973         if (unlikely(column >= oobsize)) {
974                 printk(KERN_ERR "onenand_read_oob_nolock: Attempted to start read outside oob\n");
975                 return -EINVAL;
976         }
977
978         /* Do not allow reads past end of device */
979         if (unlikely(from >= mtd->size ||
980                      column + len > ((mtd->size >> this->page_shift) -
981                                      (from >> this->page_shift)) * oobsize)) {
982                 printk(KERN_ERR "onenand_read_oob_nolock: Attempted to read beyond end of device\n");
983                 return -EINVAL;
984         }
985
986         stats = mtd->ecc_stats;
987
988         while (read < len) {
989                 cond_resched();
990
991                 thislen = oobsize - column;
992                 thislen = min_t(int, thislen, len);
993
994                 this->command(mtd, ONENAND_CMD_READOOB, from, mtd->oobsize);
995
996                 onenand_update_bufferram(mtd, from, 0);
997
998                 ret = this->wait(mtd, FL_READING);
999                 if (ret && ret != -EBADMSG) {
1000                         printk(KERN_ERR "onenand_read_oob_nolock: read failed = 0x%x\n", ret);
1001                         break;
1002                 }
1003
1004                 if (mode == MTD_OOB_AUTO)
1005                         onenand_transfer_auto_oob(mtd, buf, column, thislen);
1006                 else
1007                         this->read_bufferram(mtd, ONENAND_SPARERAM, buf, column, thislen);
1008
1009                 read += thislen;
1010
1011                 if (read == len)
1012                         break;
1013
1014                 buf += thislen;
1015
1016                 /* Read more? */
1017                 if (read < len) {
1018                         /* Page size */
1019                         from += mtd->writesize;
1020                         column = 0;
1021                 }
1022         }
1023
1024         ops->oobretlen = read;
1025
1026         if (ret)
1027                 return ret;
1028
1029         if (mtd->ecc_stats.failed - stats.failed)
1030                 return -EBADMSG;
1031
1032         return 0;
1033 }
1034
1035 /**
1036  * onenand_read - [MTD Interface] Read data from flash
1037  * @param mtd           MTD device structure
1038  * @param from          offset to read from
1039  * @param len           number of bytes to read
1040  * @param retlen        pointer to variable to store the number of read bytes
1041  * @param buf           the databuffer to put data
1042  *
1043  * Read with ecc
1044 */
1045 static int onenand_read(struct mtd_info *mtd, loff_t from, size_t len,
1046         size_t *retlen, u_char *buf)
1047 {
1048         struct mtd_oob_ops ops = {
1049                 .len    = len,
1050                 .ooblen = 0,
1051                 .datbuf = buf,
1052                 .oobbuf = NULL,
1053         };
1054         int ret;
1055
1056         onenand_get_device(mtd, FL_READING);
1057         ret = onenand_read_ops_nolock(mtd, from, &ops);
1058         onenand_release_device(mtd);
1059
1060         *retlen = ops.retlen;
1061         return ret;
1062 }
1063
1064 /**
1065  * onenand_read_oob - [MTD Interface] Read main and/or out-of-band
1066  * @param mtd:          MTD device structure
1067  * @param from:         offset to read from
1068  * @param ops:          oob operation description structure
1069
1070  * Read main and/or out-of-band
1071  */
1072 static int onenand_read_oob(struct mtd_info *mtd, loff_t from,
1073                             struct mtd_oob_ops *ops)
1074 {
1075         int ret;
1076
1077         switch (ops->mode) {
1078         case MTD_OOB_PLACE:
1079         case MTD_OOB_AUTO:
1080                 break;
1081         case MTD_OOB_RAW:
1082                 /* Not implemented yet */
1083         default:
1084                 return -EINVAL;
1085         }
1086
1087         onenand_get_device(mtd, FL_READING);
1088         if (ops->datbuf)
1089                 ret = onenand_read_ops_nolock(mtd, from, ops);
1090         else
1091                 ret = onenand_read_oob_nolock(mtd, from, ops);
1092         onenand_release_device(mtd);
1093
1094         return ret;
1095 }
1096
1097 /**
1098  * onenand_bbt_wait - [DEFAULT] wait until the command is done
1099  * @param mtd           MTD device structure
1100  * @param state         state to select the max. timeout value
1101  *
1102  * Wait for command done.
1103  */
1104 static int onenand_bbt_wait(struct mtd_info *mtd, int state)
1105 {
1106         struct onenand_chip *this = mtd->priv;
1107         unsigned long timeout;
1108         unsigned int interrupt;
1109         unsigned int ctrl;
1110
1111         /* The 20 msec is enough */
1112         timeout = jiffies + msecs_to_jiffies(20);
1113         while (time_before(jiffies, timeout)) {
1114                 interrupt = this->read_word(this->base + ONENAND_REG_INTERRUPT);
1115                 if (interrupt & ONENAND_INT_MASTER)
1116                         break;
1117         }
1118         /* To get correct interrupt status in timeout case */
1119         interrupt = this->read_word(this->base + ONENAND_REG_INTERRUPT);
1120         ctrl = this->read_word(this->base + ONENAND_REG_CTRL_STATUS);
1121
1122         /* Initial bad block case: 0x2400 or 0x0400 */
1123         if (ctrl & ONENAND_CTRL_ERROR) {
1124                 printk(KERN_DEBUG "onenand_bbt_wait: controller error = 0x%04x\n", ctrl);
1125                 return ONENAND_BBT_READ_ERROR;
1126         }
1127
1128         if (interrupt & ONENAND_INT_READ) {
1129                 int ecc = this->read_word(this->base + ONENAND_REG_ECC_STATUS);
1130                 if (ecc & ONENAND_ECC_2BIT_ALL)
1131                         return ONENAND_BBT_READ_ERROR;
1132         } else {
1133                 printk(KERN_ERR "onenand_bbt_wait: read timeout!"
1134                         "ctrl=0x%04x intr=0x%04x\n", ctrl, interrupt);
1135                 return ONENAND_BBT_READ_FATAL_ERROR;
1136         }
1137
1138         return 0;
1139 }
1140
1141 /**
1142  * onenand_bbt_read_oob - [MTD Interface] OneNAND read out-of-band for bbt scan
1143  * @param mtd           MTD device structure
1144  * @param from          offset to read from
1145  * @param ops           oob operation description structure
1146  *
1147  * OneNAND read out-of-band data from the spare area for bbt scan
1148  */
1149 int onenand_bbt_read_oob(struct mtd_info *mtd, loff_t from, 
1150                             struct mtd_oob_ops *ops)
1151 {
1152         struct onenand_chip *this = mtd->priv;
1153         int read = 0, thislen, column;
1154         int ret = 0;
1155         size_t len = ops->ooblen;
1156         u_char *buf = ops->oobbuf;
1157
1158         DEBUG(MTD_DEBUG_LEVEL3, "onenand_bbt_read_oob: from = 0x%08x, len = %zi\n", (unsigned int) from, len);
1159
1160         /* Initialize return value */
1161         ops->oobretlen = 0;
1162
1163         /* Do not allow reads past end of device */
1164         if (unlikely((from + len) > mtd->size)) {
1165                 printk(KERN_ERR "onenand_bbt_read_oob: Attempt read beyond end of device\n");
1166                 return ONENAND_BBT_READ_FATAL_ERROR;
1167         }
1168
1169         /* Grab the lock and see if the device is available */
1170         onenand_get_device(mtd, FL_READING);
1171
1172         column = from & (mtd->oobsize - 1);
1173
1174         while (read < len) {
1175                 cond_resched();
1176
1177                 thislen = mtd->oobsize - column;
1178                 thislen = min_t(int, thislen, len);
1179
1180                 this->command(mtd, ONENAND_CMD_READOOB, from, mtd->oobsize);
1181
1182                 onenand_update_bufferram(mtd, from, 0);
1183
1184                 ret = onenand_bbt_wait(mtd, FL_READING);
1185                 if (ret)
1186                         break;
1187
1188                 this->read_bufferram(mtd, ONENAND_SPARERAM, buf, column, thislen);
1189                 read += thislen;
1190                 if (read == len)
1191                         break;
1192
1193                 buf += thislen;
1194
1195                 /* Read more? */
1196                 if (read < len) {
1197                         /* Update Page size */
1198                         from += this->writesize;
1199                         column = 0;
1200                 }
1201         }
1202
1203         /* Deselect and wake up anyone waiting on the device */
1204         onenand_release_device(mtd);
1205
1206         ops->oobretlen = read;
1207         return ret;
1208 }
1209
1210 #ifdef CONFIG_MTD_ONENAND_VERIFY_WRITE
1211 /**
1212  * onenand_verify_oob - [GENERIC] verify the oob contents after a write
1213  * @param mtd           MTD device structure
1214  * @param buf           the databuffer to verify
1215  * @param to            offset to read from
1216  */
1217 static int onenand_verify_oob(struct mtd_info *mtd, const u_char *buf, loff_t to)
1218 {
1219         struct onenand_chip *this = mtd->priv;
1220         u_char *oob_buf = this->oob_buf;
1221         int status, i;
1222
1223         this->command(mtd, ONENAND_CMD_READOOB, to, mtd->oobsize);
1224         onenand_update_bufferram(mtd, to, 0);
1225         status = this->wait(mtd, FL_READING);
1226         if (status)
1227                 return status;
1228
1229         this->read_bufferram(mtd, ONENAND_SPARERAM, oob_buf, 0, mtd->oobsize);
1230         for (i = 0; i < mtd->oobsize; i++)
1231                 if (buf[i] != 0xFF && buf[i] != oob_buf[i])
1232                         return -EBADMSG;
1233
1234         return 0;
1235 }
1236
1237 /**
1238  * onenand_verify - [GENERIC] verify the chip contents after a write
1239  * @param mtd          MTD device structure
1240  * @param buf          the databuffer to verify
1241  * @param addr         offset to read from
1242  * @param len          number of bytes to read and compare
1243  */
1244 static int onenand_verify(struct mtd_info *mtd, const u_char *buf, loff_t addr, size_t len)
1245 {
1246         struct onenand_chip *this = mtd->priv;
1247         void __iomem *dataram;
1248         int ret = 0;
1249         int thislen, column;
1250
1251         while (len != 0) {
1252                 thislen = min_t(int, this->writesize, len);
1253                 column = addr & (this->writesize - 1);
1254                 if (column + thislen > this->writesize)
1255                         thislen = this->writesize - column;
1256
1257                 this->command(mtd, ONENAND_CMD_READ, addr, this->writesize);
1258
1259                 onenand_update_bufferram(mtd, addr, 0);
1260
1261                 ret = this->wait(mtd, FL_READING);
1262                 if (ret)
1263                         return ret;
1264
1265                 onenand_update_bufferram(mtd, addr, 1);
1266
1267                 dataram = this->base + ONENAND_DATARAM;
1268                 dataram += onenand_bufferram_offset(mtd, ONENAND_DATARAM);
1269
1270                 if (memcmp(buf, dataram + column, thislen))
1271                         return -EBADMSG;
1272
1273                 len -= thislen;
1274                 buf += thislen;
1275                 addr += thislen;
1276         }
1277
1278         return 0;
1279 }
1280 #else
1281 #define onenand_verify(...)             (0)
1282 #define onenand_verify_oob(...)         (0)
1283 #endif
1284
1285 #define NOTALIGNED(x)   ((x & (this->subpagesize - 1)) != 0)
1286
1287 /**
1288  * onenand_fill_auto_oob - [Internal] oob auto-placement transfer
1289  * @param mtd           MTD device structure
1290  * @param oob_buf       oob buffer
1291  * @param buf           source address
1292  * @param column        oob offset to write to
1293  * @param thislen       oob length to write
1294  */
1295 static int onenand_fill_auto_oob(struct mtd_info *mtd, u_char *oob_buf,
1296                                   const u_char *buf, int column, int thislen)
1297 {
1298         struct onenand_chip *this = mtd->priv;
1299         struct nand_oobfree *free;
1300         int writecol = column;
1301         int writeend = column + thislen;
1302         int lastgap = 0;
1303         unsigned int i;
1304
1305         free = this->ecclayout->oobfree;
1306         for (i = 0; i < MTD_MAX_OOBFREE_ENTRIES && free->length; i++, free++) {
1307                 if (writecol >= lastgap)
1308                         writecol += free->offset - lastgap;
1309                 if (writeend >= lastgap)
1310                         writeend += free->offset - lastgap;
1311                 lastgap = free->offset + free->length;
1312         }
1313         free = this->ecclayout->oobfree;
1314         for (i = 0; i < MTD_MAX_OOBFREE_ENTRIES && free->length; i++, free++) {
1315                 int free_end = free->offset + free->length;
1316                 if (free->offset < writeend && free_end > writecol) {
1317                         int st = max_t(int,free->offset,writecol);
1318                         int ed = min_t(int,free_end,writeend);
1319                         int n = ed - st;
1320                         memcpy(oob_buf + st, buf, n);
1321                         buf += n;
1322                 } else if (column == 0)
1323                         break;
1324         }
1325         return 0;
1326 }
1327
1328 /**
1329  * onenand_write_ops_nolock - [OneNAND Interface] write main and/or out-of-band
1330  * @param mtd           MTD device structure
1331  * @param to            offset to write to
1332  * @param ops           oob operation description structure
1333  *
1334  * Write main and/or oob with ECC
1335  */
1336 static int onenand_write_ops_nolock(struct mtd_info *mtd, loff_t to,
1337                                 struct mtd_oob_ops *ops)
1338 {
1339         struct onenand_chip *this = mtd->priv;
1340         int written = 0, column, thislen, subpage;
1341         int oobwritten = 0, oobcolumn, thisooblen, oobsize;
1342         size_t len = ops->len;
1343         size_t ooblen = ops->ooblen;
1344         const u_char *buf = ops->datbuf;
1345         const u_char *oob = ops->oobbuf;
1346         u_char *oobbuf;
1347         int ret = 0;
1348
1349         DEBUG(MTD_DEBUG_LEVEL3, "onenand_write_ops_nolock: to = 0x%08x, len = %i\n", (unsigned int) to, (int) len);
1350
1351         /* Initialize retlen, in case of early exit */
1352         ops->retlen = 0;
1353         ops->oobretlen = 0;
1354
1355         /* Do not allow writes past end of device */
1356         if (unlikely((to + len) > mtd->size)) {
1357                 printk(KERN_ERR "onenand_write_ops_nolock: Attempt write to past end of device\n");
1358                 return -EINVAL;
1359         }
1360
1361         /* Reject writes, which are not page aligned */
1362         if (unlikely(NOTALIGNED(to)) || unlikely(NOTALIGNED(len))) {
1363                 printk(KERN_ERR "onenand_write_ops_nolock: Attempt to write not page aligned data\n");
1364                 return -EINVAL;
1365         }
1366
1367         if (ops->mode == MTD_OOB_AUTO)
1368                 oobsize = this->ecclayout->oobavail;
1369         else
1370                 oobsize = mtd->oobsize;
1371
1372         oobcolumn = to & (mtd->oobsize - 1);
1373
1374         column = to & (mtd->writesize - 1);
1375
1376         /* Loop until all data write */
1377         while (written < len) {
1378                 u_char *wbuf = (u_char *) buf;
1379
1380                 thislen = min_t(int, mtd->writesize - column, len - written);
1381                 thisooblen = min_t(int, oobsize - oobcolumn, ooblen - oobwritten);
1382
1383                 cond_resched();
1384
1385                 this->command(mtd, ONENAND_CMD_BUFFERRAM, to, thislen);
1386
1387                 /* Partial page write */
1388                 subpage = thislen < mtd->writesize;
1389                 if (subpage) {
1390                         memset(this->page_buf, 0xff, mtd->writesize);
1391                         memcpy(this->page_buf + column, buf, thislen);
1392                         wbuf = this->page_buf;
1393                 }
1394
1395                 this->write_bufferram(mtd, ONENAND_DATARAM, wbuf, 0, mtd->writesize);
1396
1397                 if (oob) {
1398                         oobbuf = this->oob_buf;
1399
1400                         /* We send data to spare ram with oobsize
1401                          * to prevent byte access */
1402                         memset(oobbuf, 0xff, mtd->oobsize);
1403                         if (ops->mode == MTD_OOB_AUTO)
1404                                 onenand_fill_auto_oob(mtd, oobbuf, oob, oobcolumn, thisooblen);
1405                         else
1406                                 memcpy(oobbuf + oobcolumn, oob, thisooblen);
1407
1408                         oobwritten += thisooblen;
1409                         oob += thisooblen;
1410                         oobcolumn = 0;
1411                 } else
1412                         oobbuf = (u_char *) ffchars;
1413
1414                 this->write_bufferram(mtd, ONENAND_SPARERAM, oobbuf, 0, mtd->oobsize);
1415
1416                 this->command(mtd, ONENAND_CMD_PROG, to, mtd->writesize);
1417
1418                 ret = this->wait(mtd, FL_WRITING);
1419
1420                 /* In partial page write we don't update bufferram */
1421                 onenand_update_bufferram(mtd, to, !ret && !subpage);
1422                 if (ONENAND_IS_2PLANE(this)) {
1423                         ONENAND_SET_BUFFERRAM1(this);
1424                         onenand_update_bufferram(mtd, to + this->writesize, !ret && !subpage);
1425                 }
1426
1427                 if (ret) {
1428                         printk(KERN_ERR "onenand_write_ops_nolock: write filaed %d\n", ret);
1429                         break;
1430                 }
1431
1432                 /* Only check verify write turn on */
1433                 ret = onenand_verify(mtd, buf, to, thislen);
1434                 if (ret) {
1435                         printk(KERN_ERR "onenand_write_ops_nolock: verify failed %d\n", ret);
1436                         break;
1437                 }
1438
1439                 written += thislen;
1440
1441                 if (written == len)
1442                         break;
1443
1444                 column = 0;
1445                 to += thislen;
1446                 buf += thislen;
1447         }
1448
1449         ops->retlen = written;
1450
1451         return ret;
1452 }
1453
1454
1455 /**
1456  * onenand_write_oob_nolock - [Internal] OneNAND write out-of-band
1457  * @param mtd           MTD device structure
1458  * @param to            offset to write to
1459  * @param len           number of bytes to write
1460  * @param retlen        pointer to variable to store the number of written bytes
1461  * @param buf           the data to write
1462  * @param mode          operation mode
1463  *
1464  * OneNAND write out-of-band
1465  */
1466 static int onenand_write_oob_nolock(struct mtd_info *mtd, loff_t to,
1467                                     struct mtd_oob_ops *ops)
1468 {
1469         struct onenand_chip *this = mtd->priv;
1470         int column, ret = 0, oobsize;
1471         int written = 0;
1472         u_char *oobbuf;
1473         size_t len = ops->ooblen;
1474         const u_char *buf = ops->oobbuf;
1475         mtd_oob_mode_t mode = ops->mode;
1476
1477         to += ops->ooboffs;
1478
1479         DEBUG(MTD_DEBUG_LEVEL3, "onenand_write_oob_nolock: to = 0x%08x, len = %i\n", (unsigned int) to, (int) len);
1480
1481         /* Initialize retlen, in case of early exit */
1482         ops->oobretlen = 0;
1483
1484         if (mode == MTD_OOB_AUTO)
1485                 oobsize = this->ecclayout->oobavail;
1486         else
1487                 oobsize = mtd->oobsize;
1488
1489         column = to & (mtd->oobsize - 1);
1490
1491         if (unlikely(column >= oobsize)) {
1492                 printk(KERN_ERR "onenand_write_oob_nolock: Attempted to start write outside oob\n");
1493                 return -EINVAL;
1494         }
1495
1496         /* For compatibility with NAND: Do not allow write past end of page */
1497         if (unlikely(column + len > oobsize)) {
1498                 printk(KERN_ERR "onenand_write_oob_nolock: "
1499                       "Attempt to write past end of page\n");
1500                 return -EINVAL;
1501         }
1502
1503         /* Do not allow reads past end of device */
1504         if (unlikely(to >= mtd->size ||
1505                      column + len > ((mtd->size >> this->page_shift) -
1506                                      (to >> this->page_shift)) * oobsize)) {
1507                 printk(KERN_ERR "onenand_write_oob_nolock: Attempted to write past end of device\n");
1508                 return -EINVAL;
1509         }
1510
1511         oobbuf = this->oob_buf;
1512
1513         /* Loop until all data write */
1514         while (written < len) {
1515                 int thislen = min_t(int, oobsize, len - written);
1516
1517                 cond_resched();
1518
1519                 this->command(mtd, ONENAND_CMD_BUFFERRAM, to, mtd->oobsize);
1520
1521                 /* We send data to spare ram with oobsize
1522                  * to prevent byte access */
1523                 memset(oobbuf, 0xff, mtd->oobsize);
1524                 if (mode == MTD_OOB_AUTO)
1525                         onenand_fill_auto_oob(mtd, oobbuf, buf, column, thislen);
1526                 else
1527                         memcpy(oobbuf + column, buf, thislen);
1528                 this->write_bufferram(mtd, ONENAND_SPARERAM, oobbuf, 0, mtd->oobsize);
1529
1530                 this->command(mtd, ONENAND_CMD_PROGOOB, to, mtd->oobsize);
1531
1532                 onenand_update_bufferram(mtd, to, 0);
1533                 if (ONENAND_IS_2PLANE(this)) {
1534                         ONENAND_SET_BUFFERRAM1(this);
1535                         onenand_update_bufferram(mtd, to + this->writesize, 0);
1536                 }
1537
1538                 ret = this->wait(mtd, FL_WRITING);
1539                 if (ret) {
1540                         printk(KERN_ERR "onenand_write_oob_nolock: write failed %d\n", ret);
1541                         break;
1542                 }
1543
1544                 ret = onenand_verify_oob(mtd, oobbuf, to);
1545                 if (ret) {
1546                         printk(KERN_ERR "onenand_write_oob_nolock: verify failed %d\n", ret);
1547                         break;
1548                 }
1549
1550                 written += thislen;
1551                 if (written == len)
1552                         break;
1553
1554                 to += mtd->writesize;
1555                 buf += thislen;
1556                 column = 0;
1557         }
1558
1559         ops->oobretlen = written;
1560
1561         return ret;
1562 }
1563
1564 /**
1565  * onenand_write - [MTD Interface] write buffer to FLASH
1566  * @param mtd           MTD device structure
1567  * @param to            offset to write to
1568  * @param len           number of bytes to write
1569  * @param retlen        pointer to variable to store the number of written bytes
1570  * @param buf           the data to write
1571  *
1572  * Write with ECC
1573  */
1574 static int onenand_write(struct mtd_info *mtd, loff_t to, size_t len,
1575         size_t *retlen, const u_char *buf)
1576 {
1577         struct mtd_oob_ops ops = {
1578                 .len    = len,
1579                 .ooblen = 0,
1580                 .datbuf = (u_char *) buf,
1581                 .oobbuf = NULL,
1582         };
1583         int ret;
1584
1585         onenand_get_device(mtd, FL_WRITING);
1586         ret = onenand_write_ops_nolock(mtd, to, &ops);
1587         onenand_release_device(mtd);
1588
1589         *retlen = ops.retlen;
1590         return ret;
1591 }
1592
1593 /**
1594  * onenand_write_oob - [MTD Interface] NAND write data and/or out-of-band
1595  * @param mtd:          MTD device structure
1596  * @param to:           offset to write
1597  * @param ops:          oob operation description structure
1598  */
1599 static int onenand_write_oob(struct mtd_info *mtd, loff_t to,
1600                              struct mtd_oob_ops *ops)
1601 {
1602         int ret;
1603
1604         switch (ops->mode) {
1605         case MTD_OOB_PLACE:
1606         case MTD_OOB_AUTO:
1607                 break;
1608         case MTD_OOB_RAW:
1609                 /* Not implemented yet */
1610         default:
1611                 return -EINVAL;
1612         }
1613
1614         onenand_get_device(mtd, FL_WRITING);
1615         if (ops->datbuf)
1616                 ret = onenand_write_ops_nolock(mtd, to, ops);
1617         else
1618                 ret = onenand_write_oob_nolock(mtd, to, ops);
1619         onenand_release_device(mtd);
1620
1621         return ret;
1622 }
1623
1624 /**
1625  * onenand_block_isbad_nolock - [GENERIC] Check if a block is marked bad
1626  * @param mtd           MTD device structure
1627  * @param ofs           offset from device start
1628  * @param allowbbt      1, if its allowed to access the bbt area
1629  *
1630  * Check, if the block is bad. Either by reading the bad block table or
1631  * calling of the scan function.
1632  */
1633 static int onenand_block_isbad_nolock(struct mtd_info *mtd, loff_t ofs, int allowbbt)
1634 {
1635         struct onenand_chip *this = mtd->priv;
1636         struct bbm_info *bbm = this->bbm;
1637
1638         /* Return info from the table */
1639         return bbm->isbad_bbt(mtd, ofs, allowbbt);
1640 }
1641
1642 /**
1643  * onenand_erase - [MTD Interface] erase block(s)
1644  * @param mtd           MTD device structure
1645  * @param instr         erase instruction
1646  *
1647  * Erase one ore more blocks
1648  */
1649 static int onenand_erase(struct mtd_info *mtd, struct erase_info *instr)
1650 {
1651         struct onenand_chip *this = mtd->priv;
1652         unsigned int block_size;
1653         loff_t addr;
1654         int len;
1655         int ret = 0;
1656
1657         DEBUG(MTD_DEBUG_LEVEL3, "onenand_erase: start = 0x%08x, len = %i\n", (unsigned int) instr->addr, (unsigned int) instr->len);
1658
1659         block_size = (1 << this->erase_shift);
1660
1661         /* Start address must align on block boundary */
1662         if (unlikely(instr->addr & (block_size - 1))) {
1663                 printk(KERN_ERR "onenand_erase: Unaligned address\n");
1664                 return -EINVAL;
1665         }
1666
1667         /* Length must align on block boundary */
1668         if (unlikely(instr->len & (block_size - 1))) {
1669                 printk(KERN_ERR "onenand_erase: Length not block aligned\n");
1670                 return -EINVAL;
1671         }
1672
1673         /* Do not allow erase past end of device */
1674         if (unlikely((instr->len + instr->addr) > mtd->size)) {
1675                 printk(KERN_ERR "onenand_erase: Erase past end of device\n");
1676                 return -EINVAL;
1677         }
1678
1679         instr->fail_addr = 0xffffffff;
1680
1681         /* Grab the lock and see if the device is available */
1682         onenand_get_device(mtd, FL_ERASING);
1683
1684         /* Loop throught the pages */
1685         len = instr->len;
1686         addr = instr->addr;
1687
1688         instr->state = MTD_ERASING;
1689
1690         while (len) {
1691                 cond_resched();
1692
1693                 /* Check if we have a bad block, we do not erase bad blocks */
1694                 if (onenand_block_isbad_nolock(mtd, addr, 0)) {
1695                         printk (KERN_WARNING "onenand_erase: attempt to erase a bad block at addr 0x%08x\n", (unsigned int) addr);
1696                         instr->state = MTD_ERASE_FAILED;
1697                         goto erase_exit;
1698                 }
1699
1700                 this->command(mtd, ONENAND_CMD_ERASE, addr, block_size);
1701
1702                 onenand_invalidate_bufferram(mtd, addr, block_size);
1703
1704                 ret = this->wait(mtd, FL_ERASING);
1705                 /* Check, if it is write protected */
1706                 if (ret) {
1707                         printk(KERN_ERR "onenand_erase: Failed erase, block %d\n", (unsigned) (addr >> this->erase_shift));
1708                         instr->state = MTD_ERASE_FAILED;
1709                         instr->fail_addr = addr;
1710                         goto erase_exit;
1711                 }
1712
1713                 len -= block_size;
1714                 addr += block_size;
1715         }
1716
1717         instr->state = MTD_ERASE_DONE;
1718
1719 erase_exit:
1720
1721         ret = instr->state == MTD_ERASE_DONE ? 0 : -EIO;
1722
1723         /* Deselect and wake up anyone waiting on the device */
1724         onenand_release_device(mtd);
1725
1726         /* Do call back function */
1727         if (!ret)
1728                 mtd_erase_callback(instr);
1729
1730         return ret;
1731 }
1732
1733 /**
1734  * onenand_sync - [MTD Interface] sync
1735  * @param mtd           MTD device structure
1736  *
1737  * Sync is actually a wait for chip ready function
1738  */
1739 static void onenand_sync(struct mtd_info *mtd)
1740 {
1741         DEBUG(MTD_DEBUG_LEVEL3, "onenand_sync: called\n");
1742
1743         /* Grab the lock and see if the device is available */
1744         onenand_get_device(mtd, FL_SYNCING);
1745
1746         /* Release it and go back */
1747         onenand_release_device(mtd);
1748 }
1749
1750 /**
1751  * onenand_block_isbad - [MTD Interface] Check whether the block at the given offset is bad
1752  * @param mtd           MTD device structure
1753  * @param ofs           offset relative to mtd start
1754  *
1755  * Check whether the block is bad
1756  */
1757 static int onenand_block_isbad(struct mtd_info *mtd, loff_t ofs)
1758 {
1759         int ret;
1760
1761         /* Check for invalid offset */
1762         if (ofs > mtd->size)
1763                 return -EINVAL;
1764
1765         onenand_get_device(mtd, FL_READING);
1766         ret = onenand_block_isbad_nolock(mtd, ofs, 0);
1767         onenand_release_device(mtd);
1768         return ret;
1769 }
1770
1771 /**
1772  * onenand_default_block_markbad - [DEFAULT] mark a block bad
1773  * @param mtd           MTD device structure
1774  * @param ofs           offset from device start
1775  *
1776  * This is the default implementation, which can be overridden by
1777  * a hardware specific driver.
1778  */
1779 static int onenand_default_block_markbad(struct mtd_info *mtd, loff_t ofs)
1780 {
1781         struct onenand_chip *this = mtd->priv;
1782         struct bbm_info *bbm = this->bbm;
1783         u_char buf[2] = {0, 0};
1784         struct mtd_oob_ops ops = {
1785                 .mode = MTD_OOB_PLACE,
1786                 .ooblen = 2,
1787                 .oobbuf = buf,
1788                 .ooboffs = 0,
1789         };
1790         int block;
1791
1792         /* Get block number */
1793         block = ((int) ofs) >> bbm->bbt_erase_shift;
1794         if (bbm->bbt)
1795                 bbm->bbt[block >> 2] |= 0x01 << ((block & 0x03) << 1);
1796
1797         /* We write two bytes, so we dont have to mess with 16 bit access */
1798         ofs += mtd->oobsize + (bbm->badblockpos & ~0x01);
1799         return onenand_write_oob_nolock(mtd, ofs, &ops);
1800 }
1801
1802 /**
1803  * onenand_block_markbad - [MTD Interface] Mark the block at the given offset as bad
1804  * @param mtd           MTD device structure
1805  * @param ofs           offset relative to mtd start
1806  *
1807  * Mark the block as bad
1808  */
1809 static int onenand_block_markbad(struct mtd_info *mtd, loff_t ofs)
1810 {
1811         struct onenand_chip *this = mtd->priv;
1812         int ret;
1813
1814         ret = onenand_block_isbad(mtd, ofs);
1815         if (ret) {
1816                 /* If it was bad already, return success and do nothing */
1817                 if (ret > 0)
1818                         return 0;
1819                 return ret;
1820         }
1821
1822         onenand_get_device(mtd, FL_WRITING);
1823         ret = this->block_markbad(mtd, ofs);
1824         onenand_release_device(mtd);
1825         return ret;
1826 }
1827
1828 /**
1829  * onenand_do_lock_cmd - [OneNAND Interface] Lock or unlock block(s)
1830  * @param mtd           MTD device structure
1831  * @param ofs           offset relative to mtd start
1832  * @param len           number of bytes to lock or unlock
1833  * @param cmd           lock or unlock command
1834  *
1835  * Lock or unlock one or more blocks
1836  */
1837 static int onenand_do_lock_cmd(struct mtd_info *mtd, loff_t ofs, size_t len, int cmd)
1838 {
1839         struct onenand_chip *this = mtd->priv;
1840         int start, end, block, value, status;
1841         int wp_status_mask;
1842
1843         start = ofs >> this->erase_shift;
1844         end = len >> this->erase_shift;
1845
1846         if (cmd == ONENAND_CMD_LOCK)
1847                 wp_status_mask = ONENAND_WP_LS;
1848         else
1849                 wp_status_mask = ONENAND_WP_US;
1850
1851         /* Continuous lock scheme */
1852         if (this->options & ONENAND_HAS_CONT_LOCK) {
1853                 /* Set start block address */
1854                 this->write_word(start, this->base + ONENAND_REG_START_BLOCK_ADDRESS);
1855                 /* Set end block address */
1856                 this->write_word(start + end - 1, this->base + ONENAND_REG_END_BLOCK_ADDRESS);
1857                 /* Write lock command */
1858                 this->command(mtd, cmd, 0, 0);
1859
1860                 /* There's no return value */
1861                 this->wait(mtd, FL_LOCKING);
1862
1863                 /* Sanity check */
1864                 while (this->read_word(this->base + ONENAND_REG_CTRL_STATUS)
1865                     & ONENAND_CTRL_ONGO)
1866                         continue;
1867
1868                 /* Check lock status */
1869                 status = this->read_word(this->base + ONENAND_REG_WP_STATUS);
1870                 if (!(status & wp_status_mask))
1871                         printk(KERN_ERR "wp status = 0x%x\n", status);
1872
1873                 return 0;
1874         }
1875
1876         /* Block lock scheme */
1877         for (block = start; block < start + end; block++) {
1878                 /* Set block address */
1879                 value = onenand_block_address(this, block);
1880                 this->write_word(value, this->base + ONENAND_REG_START_ADDRESS1);
1881                 /* Select DataRAM for DDP */
1882                 value = onenand_bufferram_address(this, block);
1883                 this->write_word(value, this->base + ONENAND_REG_START_ADDRESS2);
1884                 /* Set start block address */
1885                 this->write_word(block, this->base + ONENAND_REG_START_BLOCK_ADDRESS);
1886                 /* Write lock command */
1887                 this->command(mtd, cmd, 0, 0);
1888
1889                 /* There's no return value */
1890                 this->wait(mtd, FL_LOCKING);
1891
1892                 /* Sanity check */
1893                 while (this->read_word(this->base + ONENAND_REG_CTRL_STATUS)
1894                     & ONENAND_CTRL_ONGO)
1895                         continue;
1896
1897                 /* Check lock status */
1898                 status = this->read_word(this->base + ONENAND_REG_WP_STATUS);
1899                 if (!(status & wp_status_mask))
1900                         printk(KERN_ERR "block = %d, wp status = 0x%x\n", block, status);
1901         }
1902
1903         return 0;
1904 }
1905
1906 /**
1907  * onenand_lock - [MTD Interface] Lock block(s)
1908  * @param mtd           MTD device structure
1909  * @param ofs           offset relative to mtd start
1910  * @param len           number of bytes to unlock
1911  *
1912  * Lock one or more blocks
1913  */
1914 static int onenand_lock(struct mtd_info *mtd, loff_t ofs, size_t len)
1915 {
1916         int ret;
1917
1918         onenand_get_device(mtd, FL_LOCKING);
1919         ret = onenand_do_lock_cmd(mtd, ofs, len, ONENAND_CMD_LOCK);
1920         onenand_release_device(mtd);
1921         return ret;
1922 }
1923
1924 /**
1925  * onenand_unlock - [MTD Interface] Unlock block(s)
1926  * @param mtd           MTD device structure
1927  * @param ofs           offset relative to mtd start
1928  * @param len           number of bytes to unlock
1929  *
1930  * Unlock one or more blocks
1931  */
1932 static int onenand_unlock(struct mtd_info *mtd, loff_t ofs, size_t len)
1933 {
1934         int ret;
1935
1936         onenand_get_device(mtd, FL_LOCKING);
1937         ret = onenand_do_lock_cmd(mtd, ofs, len, ONENAND_CMD_UNLOCK);
1938         onenand_release_device(mtd);
1939         return ret;
1940 }
1941
1942 /**
1943  * onenand_check_lock_status - [OneNAND Interface] Check lock status
1944  * @param this          onenand chip data structure
1945  *
1946  * Check lock status
1947  */
1948 static void onenand_check_lock_status(struct onenand_chip *this)
1949 {
1950         unsigned int value, block, status;
1951         unsigned int end;
1952
1953         end = this->chipsize >> this->erase_shift;
1954         for (block = 0; block < end; block++) {
1955                 /* Set block address */
1956                 value = onenand_block_address(this, block);
1957                 this->write_word(value, this->base + ONENAND_REG_START_ADDRESS1);
1958                 /* Select DataRAM for DDP */
1959                 value = onenand_bufferram_address(this, block);
1960                 this->write_word(value, this->base + ONENAND_REG_START_ADDRESS2);
1961                 /* Set start block address */
1962                 this->write_word(block, this->base + ONENAND_REG_START_BLOCK_ADDRESS);
1963
1964                 /* Check lock status */
1965                 status = this->read_word(this->base + ONENAND_REG_WP_STATUS);
1966                 if (!(status & ONENAND_WP_US))
1967                         printk(KERN_ERR "block = %d, wp status = 0x%x\n", block, status);
1968         }
1969 }
1970
1971 /**
1972  * onenand_unlock_all - [OneNAND Interface] unlock all blocks
1973  * @param mtd           MTD device structure
1974  *
1975  * Unlock all blocks
1976  */
1977 static int onenand_unlock_all(struct mtd_info *mtd)
1978 {
1979         struct onenand_chip *this = mtd->priv;
1980
1981         if (this->options & ONENAND_HAS_UNLOCK_ALL) {
1982                 /* Set start block address */
1983                 this->write_word(0, this->base + ONENAND_REG_START_BLOCK_ADDRESS);
1984                 /* Write unlock command */
1985                 this->command(mtd, ONENAND_CMD_UNLOCK_ALL, 0, 0);
1986
1987                 /* There's no return value */
1988                 this->wait(mtd, FL_LOCKING);
1989
1990                 /* Sanity check */
1991                 while (this->read_word(this->base + ONENAND_REG_CTRL_STATUS)
1992                     & ONENAND_CTRL_ONGO)
1993                         continue;
1994
1995                 /* Workaround for all block unlock in DDP */
1996                 if (ONENAND_IS_DDP(this)) {
1997                         /* 1st block on another chip */
1998                         loff_t ofs = this->chipsize >> 1;
1999                         size_t len = mtd->erasesize;
2000
2001                         onenand_do_lock_cmd(mtd, ofs, len, ONENAND_CMD_UNLOCK);
2002                 }
2003
2004                 onenand_check_lock_status(this);
2005
2006                 return 0;
2007         }
2008
2009         onenand_do_lock_cmd(mtd, 0x0, this->chipsize, ONENAND_CMD_UNLOCK);
2010
2011         return 0;
2012 }
2013
2014 #ifdef CONFIG_MTD_ONENAND_OTP
2015
2016 /* Interal OTP operation */
2017 typedef int (*otp_op_t)(struct mtd_info *mtd, loff_t form, size_t len,
2018                 size_t *retlen, u_char *buf);
2019
2020 /**
2021  * do_otp_read - [DEFAULT] Read OTP block area
2022  * @param mtd           MTD device structure
2023  * @param from          The offset to read
2024  * @param len           number of bytes to read
2025  * @param retlen        pointer to variable to store the number of readbytes
2026  * @param buf           the databuffer to put/get data
2027  *
2028  * Read OTP block area.
2029  */
2030 static int do_otp_read(struct mtd_info *mtd, loff_t from, size_t len,
2031                 size_t *retlen, u_char *buf)
2032 {
2033         struct onenand_chip *this = mtd->priv;
2034         struct mtd_oob_ops ops = {
2035                 .len    = len,
2036                 .ooblen = 0,
2037                 .datbuf = buf,
2038                 .oobbuf = NULL,
2039         };
2040         int ret;
2041
2042         /* Enter OTP access mode */
2043         this->command(mtd, ONENAND_CMD_OTP_ACCESS, 0, 0);
2044         this->wait(mtd, FL_OTPING);
2045
2046         ret = onenand_read_ops_nolock(mtd, from, &ops);
2047
2048         /* Exit OTP access mode */
2049         this->command(mtd, ONENAND_CMD_RESET, 0, 0);
2050         this->wait(mtd, FL_RESETING);
2051
2052         return ret;
2053 }
2054
2055 /**
2056  * do_otp_write - [DEFAULT] Write OTP block area
2057  * @param mtd           MTD device structure
2058  * @param to            The offset to write
2059  * @param len           number of bytes to write
2060  * @param retlen        pointer to variable to store the number of write bytes
2061  * @param buf           the databuffer to put/get data
2062  *
2063  * Write OTP block area.
2064  */
2065 static int do_otp_write(struct mtd_info *mtd, loff_t to, size_t len,
2066                 size_t *retlen, u_char *buf)
2067 {
2068         struct onenand_chip *this = mtd->priv;
2069         unsigned char *pbuf = buf;
2070         int ret;
2071         struct mtd_oob_ops ops;
2072
2073         /* Force buffer page aligned */
2074         if (len < mtd->writesize) {
2075                 memcpy(this->page_buf, buf, len);
2076                 memset(this->page_buf + len, 0xff, mtd->writesize - len);
2077                 pbuf = this->page_buf;
2078                 len = mtd->writesize;
2079         }
2080
2081         /* Enter OTP access mode */
2082         this->command(mtd, ONENAND_CMD_OTP_ACCESS, 0, 0);
2083         this->wait(mtd, FL_OTPING);
2084
2085         ops.len = len;
2086         ops.ooblen = 0;
2087         ops.datbuf = pbuf;
2088         ops.oobbuf = NULL;
2089         ret = onenand_write_ops_nolock(mtd, to, &ops);
2090         *retlen = ops.retlen;
2091
2092         /* Exit OTP access mode */
2093         this->command(mtd, ONENAND_CMD_RESET, 0, 0);
2094         this->wait(mtd, FL_RESETING);
2095
2096         return ret;
2097 }
2098
2099 /**
2100  * do_otp_lock - [DEFAULT] Lock OTP block area
2101  * @param mtd           MTD device structure
2102  * @param from          The offset to lock
2103  * @param len           number of bytes to lock
2104  * @param retlen        pointer to variable to store the number of lock bytes
2105  * @param buf           the databuffer to put/get data
2106  *
2107  * Lock OTP block area.
2108  */
2109 static int do_otp_lock(struct mtd_info *mtd, loff_t from, size_t len,
2110                 size_t *retlen, u_char *buf)
2111 {
2112         struct onenand_chip *this = mtd->priv;
2113         struct mtd_oob_ops ops = {
2114                 .mode = MTD_OOB_PLACE,
2115                 .ooblen = len,
2116                 .oobbuf = buf,
2117                 .ooboffs = 0,
2118         };
2119         int ret;
2120
2121         /* Enter OTP access mode */
2122         this->command(mtd, ONENAND_CMD_OTP_ACCESS, 0, 0);
2123         this->wait(mtd, FL_OTPING);
2124
2125         ret = onenand_write_oob_nolock(mtd, from, &ops);
2126
2127         *retlen = ops.oobretlen;
2128
2129         /* Exit OTP access mode */
2130         this->command(mtd, ONENAND_CMD_RESET, 0, 0);
2131         this->wait(mtd, FL_RESETING);
2132
2133         return ret;
2134 }
2135
2136 /**
2137  * onenand_otp_walk - [DEFAULT] Handle OTP operation
2138  * @param mtd           MTD device structure
2139  * @param from          The offset to read/write
2140  * @param len           number of bytes to read/write
2141  * @param retlen        pointer to variable to store the number of read bytes
2142  * @param buf           the databuffer to put/get data
2143  * @param action        do given action
2144  * @param mode          specify user and factory
2145  *
2146  * Handle OTP operation.
2147  */
2148 static int onenand_otp_walk(struct mtd_info *mtd, loff_t from, size_t len,
2149                         size_t *retlen, u_char *buf,
2150                         otp_op_t action, int mode)
2151 {
2152         struct onenand_chip *this = mtd->priv;
2153         int otp_pages;
2154         int density;
2155         int ret = 0;
2156
2157         *retlen = 0;
2158
2159         density = onenand_get_density(this->device_id);
2160         if (density < ONENAND_DEVICE_DENSITY_512Mb)
2161                 otp_pages = 20;
2162         else
2163                 otp_pages = 10;
2164
2165         if (mode == MTD_OTP_FACTORY) {
2166                 from += mtd->writesize * otp_pages;
2167                 otp_pages = 64 - otp_pages;
2168         }
2169
2170         /* Check User/Factory boundary */
2171         if (((mtd->writesize * otp_pages) - (from + len)) < 0)
2172                 return 0;
2173
2174         onenand_get_device(mtd, FL_OTPING);
2175         while (len > 0 && otp_pages > 0) {
2176                 if (!action) {  /* OTP Info functions */
2177                         struct otp_info *otpinfo;
2178
2179                         len -= sizeof(struct otp_info);
2180                         if (len <= 0) {
2181                                 ret = -ENOSPC;
2182                                 break;
2183                         }
2184
2185                         otpinfo = (struct otp_info *) buf;
2186                         otpinfo->start = from;
2187                         otpinfo->length = mtd->writesize;
2188                         otpinfo->locked = 0;
2189
2190                         from += mtd->writesize;
2191                         buf += sizeof(struct otp_info);
2192                         *retlen += sizeof(struct otp_info);
2193                 } else {
2194                         size_t tmp_retlen;
2195                         int size = len;
2196
2197                         ret = action(mtd, from, len, &tmp_retlen, buf);
2198
2199                         buf += size;
2200                         len -= size;
2201                         *retlen += size;
2202
2203                         if (ret)
2204                                 break;
2205                 }
2206                 otp_pages--;
2207         }
2208         onenand_release_device(mtd);
2209
2210         return ret;
2211 }
2212
2213 /**
2214  * onenand_get_fact_prot_info - [MTD Interface] Read factory OTP info
2215  * @param mtd           MTD device structure
2216  * @param buf           the databuffer to put/get data
2217  * @param len           number of bytes to read
2218  *
2219  * Read factory OTP info.
2220  */
2221 static int onenand_get_fact_prot_info(struct mtd_info *mtd,
2222                         struct otp_info *buf, size_t len)
2223 {
2224         size_t retlen;
2225         int ret;
2226
2227         ret = onenand_otp_walk(mtd, 0, len, &retlen, (u_char *) buf, NULL, MTD_OTP_FACTORY);
2228
2229         return ret ? : retlen;
2230 }
2231
2232 /**
2233  * onenand_read_fact_prot_reg - [MTD Interface] Read factory OTP area
2234  * @param mtd           MTD device structure
2235  * @param from          The offset to read
2236  * @param len           number of bytes to read
2237  * @param retlen        pointer to variable to store the number of read bytes
2238  * @param buf           the databuffer to put/get data
2239  *
2240  * Read factory OTP area.
2241  */
2242 static int onenand_read_fact_prot_reg(struct mtd_info *mtd, loff_t from,
2243                         size_t len, size_t *retlen, u_char *buf)
2244 {
2245         return onenand_otp_walk(mtd, from, len, retlen, buf, do_otp_read, MTD_OTP_FACTORY);
2246 }
2247
2248 /**
2249  * onenand_get_user_prot_info - [MTD Interface] Read user OTP info
2250  * @param mtd           MTD device structure
2251  * @param buf           the databuffer to put/get data
2252  * @param len           number of bytes to read
2253  *
2254  * Read user OTP info.
2255  */
2256 static int onenand_get_user_prot_info(struct mtd_info *mtd,
2257                         struct otp_info *buf, size_t len)
2258 {
2259         size_t retlen;
2260         int ret;
2261
2262         ret = onenand_otp_walk(mtd, 0, len, &retlen, (u_char *) buf, NULL, MTD_OTP_USER);
2263
2264         return ret ? : retlen;
2265 }
2266
2267 /**
2268  * onenand_read_user_prot_reg - [MTD Interface] Read user OTP area
2269  * @param mtd           MTD device structure
2270  * @param from          The offset to read
2271  * @param len           number of bytes to read
2272  * @param retlen        pointer to variable to store the number of read bytes
2273  * @param buf           the databuffer to put/get data
2274  *
2275  * Read user OTP area.
2276  */
2277 static int onenand_read_user_prot_reg(struct mtd_info *mtd, loff_t from,
2278                         size_t len, size_t *retlen, u_char *buf)
2279 {
2280         return onenand_otp_walk(mtd, from, len, retlen, buf, do_otp_read, MTD_OTP_USER);
2281 }
2282
2283 /**
2284  * onenand_write_user_prot_reg - [MTD Interface] Write user OTP area
2285  * @param mtd           MTD device structure
2286  * @param from          The offset to write
2287  * @param len           number of bytes to write
2288  * @param retlen        pointer to variable to store the number of write bytes
2289  * @param buf           the databuffer to put/get data
2290  *
2291  * Write user OTP area.
2292  */
2293 static int onenand_write_user_prot_reg(struct mtd_info *mtd, loff_t from,
2294                         size_t len, size_t *retlen, u_char *buf)
2295 {
2296         return onenand_otp_walk(mtd, from, len, retlen, buf, do_otp_write, MTD_OTP_USER);
2297 }
2298
2299 /**
2300  * onenand_lock_user_prot_reg - [MTD Interface] Lock user OTP area
2301  * @param mtd           MTD device structure
2302  * @param from          The offset to lock
2303  * @param len           number of bytes to unlock
2304  *
2305  * Write lock mark on spare area in page 0 in OTP block
2306  */
2307 static int onenand_lock_user_prot_reg(struct mtd_info *mtd, loff_t from,
2308                         size_t len)
2309 {
2310         struct onenand_chip *this = mtd->priv;
2311         u_char *oob_buf = this->oob_buf;
2312         size_t retlen;
2313         int ret;
2314
2315         memset(oob_buf, 0xff, mtd->oobsize);
2316         /*
2317          * Note: OTP lock operation
2318          *       OTP block : 0xXXFC
2319          *       1st block : 0xXXF3 (If chip support)
2320          *       Both      : 0xXXF0 (If chip support)
2321          */
2322         oob_buf[ONENAND_OTP_LOCK_OFFSET] = 0xFC;
2323
2324         /*
2325          * Write lock mark to 8th word of sector0 of page0 of the spare0.
2326          * We write 16 bytes spare area instead of 2 bytes.
2327          */
2328         from = 0;
2329         len = 16;
2330
2331         ret = onenand_otp_walk(mtd, from, len, &retlen, oob_buf, do_otp_lock, MTD_OTP_USER);
2332
2333         return ret ? : retlen;
2334 }
2335 #endif  /* CONFIG_MTD_ONENAND_OTP */
2336
2337 /**
2338  * onenand_check_features - Check and set OneNAND features
2339  * @param mtd           MTD data structure
2340  *
2341  * Check and set OneNAND features
2342  * - lock scheme
2343  * - two plane
2344  */
2345 static void onenand_check_features(struct mtd_info *mtd)
2346 {
2347         struct onenand_chip *this = mtd->priv;
2348         unsigned int density, process;
2349
2350         /* Lock scheme depends on density and process */
2351         density = onenand_get_density(this->device_id);
2352         process = this->version_id >> ONENAND_VERSION_PROCESS_SHIFT;
2353
2354         /* Lock scheme */
2355         switch (density) {
2356         case ONENAND_DEVICE_DENSITY_4Gb:
2357                 this->options |= ONENAND_HAS_2PLANE;
2358
2359         case ONENAND_DEVICE_DENSITY_2Gb:
2360                 /* 2Gb DDP don't have 2 plane */
2361                 if (!ONENAND_IS_DDP(this))
2362                         this->options |= ONENAND_HAS_2PLANE;
2363                 this->options |= ONENAND_HAS_UNLOCK_ALL;
2364
2365         case ONENAND_DEVICE_DENSITY_1Gb:
2366                 /* A-Die has all block unlock */
2367                 if (process)
2368                         this->options |= ONENAND_HAS_UNLOCK_ALL;
2369                 break;
2370
2371         default:
2372                 /* Some OneNAND has continuous lock scheme */
2373                 if (!process)
2374                         this->options |= ONENAND_HAS_CONT_LOCK;
2375                 break;
2376         }
2377
2378         if (this->options & ONENAND_HAS_CONT_LOCK)
2379                 printk(KERN_DEBUG "Lock scheme is Continuous Lock\n");
2380         if (this->options & ONENAND_HAS_UNLOCK_ALL)
2381                 printk(KERN_DEBUG "Chip support all block unlock\n");
2382         if (this->options & ONENAND_HAS_2PLANE)
2383                 printk(KERN_DEBUG "Chip has 2 plane\n");
2384 }
2385
2386 /**
2387  * onenand_print_device_info - Print device & version ID
2388  * @param device        device ID
2389  * @param version       version ID
2390  *
2391  * Print device & version ID
2392  */
2393 static void onenand_print_device_info(int device, int version)
2394 {
2395         int vcc, demuxed, ddp, density;
2396
2397         vcc = device & ONENAND_DEVICE_VCC_MASK;
2398         demuxed = device & ONENAND_DEVICE_IS_DEMUX;
2399         ddp = device & ONENAND_DEVICE_IS_DDP;
2400         density = onenand_get_density(device);
2401         printk(KERN_INFO "%sOneNAND%s %dMB %sV 16-bit (0x%02x)\n",
2402                 demuxed ? "" : "Muxed ",
2403                 ddp ? "(DDP)" : "",
2404                 (16 << density),
2405                 vcc ? "2.65/3.3" : "1.8",
2406                 device);
2407         printk(KERN_INFO "OneNAND version = 0x%04x\n", version);
2408 }
2409
2410 static const struct onenand_manufacturers onenand_manuf_ids[] = {
2411         {ONENAND_MFR_SAMSUNG, "Samsung"},
2412 };
2413
2414 /**
2415  * onenand_check_maf - Check manufacturer ID
2416  * @param manuf         manufacturer ID
2417  *
2418  * Check manufacturer ID
2419  */
2420 static int onenand_check_maf(int manuf)
2421 {
2422         int size = ARRAY_SIZE(onenand_manuf_ids);
2423         char *name;
2424         int i;
2425
2426         for (i = 0; i < size; i++)
2427                 if (manuf == onenand_manuf_ids[i].id)
2428                         break;
2429
2430         if (i < size)
2431                 name = onenand_manuf_ids[i].name;
2432         else
2433                 name = "Unknown";
2434
2435         printk(KERN_DEBUG "OneNAND Manufacturer: %s (0x%0x)\n", name, manuf);
2436
2437         return (i == size);
2438 }
2439
2440 /**
2441  * onenand_probe - [OneNAND Interface] Probe the OneNAND device
2442  * @param mtd           MTD device structure
2443  *
2444  * OneNAND detection method:
2445  *   Compare the values from command with ones from register
2446  */
2447 static int onenand_probe(struct mtd_info *mtd)
2448 {
2449         struct onenand_chip *this = mtd->priv;
2450         int bram_maf_id, bram_dev_id, maf_id, dev_id, ver_id;
2451         int density;
2452         int syscfg;
2453
2454         /* Save system configuration 1 */
2455         syscfg = this->read_word(this->base + ONENAND_REG_SYS_CFG1);
2456         /* Clear Sync. Burst Read mode to read BootRAM */
2457         this->write_word((syscfg & ~ONENAND_SYS_CFG1_SYNC_READ), this->base + ONENAND_REG_SYS_CFG1);
2458
2459         /* Send the command for reading device ID from BootRAM */
2460         this->write_word(ONENAND_CMD_READID, this->base + ONENAND_BOOTRAM);
2461
2462         /* Read manufacturer and device IDs from BootRAM */
2463         bram_maf_id = this->read_word(this->base + ONENAND_BOOTRAM + 0x0);
2464         bram_dev_id = this->read_word(this->base + ONENAND_BOOTRAM + 0x2);
2465
2466         /* Reset OneNAND to read default register values */
2467         this->write_word(ONENAND_CMD_RESET, this->base + ONENAND_BOOTRAM);
2468         /* Wait reset */
2469         this->wait(mtd, FL_RESETING);
2470
2471         /* Restore system configuration 1 */
2472         this->write_word(syscfg, this->base + ONENAND_REG_SYS_CFG1);
2473
2474         /* Check manufacturer ID */
2475         if (onenand_check_maf(bram_maf_id))
2476                 return -ENXIO;
2477
2478         /* Read manufacturer and device IDs from Register */
2479         maf_id = this->read_word(this->base + ONENAND_REG_MANUFACTURER_ID);
2480         dev_id = this->read_word(this->base + ONENAND_REG_DEVICE_ID);
2481         ver_id = this->read_word(this->base + ONENAND_REG_VERSION_ID);
2482
2483         /* Check OneNAND device */
2484         if (maf_id != bram_maf_id || dev_id != bram_dev_id)
2485                 return -ENXIO;
2486
2487         /* Flash device information */
2488         onenand_print_device_info(dev_id, ver_id);
2489         this->device_id = dev_id;
2490         this->version_id = ver_id;
2491
2492         density = onenand_get_density(dev_id);
2493         this->chipsize = (16 << density) << 20;
2494         /* Set density mask. it is used for DDP */
2495         if (ONENAND_IS_DDP(this))
2496                 this->density_mask = (1 << (density + 6));
2497         else
2498                 this->density_mask = 0;
2499
2500         /* OneNAND page size & block size */
2501         /* The data buffer size is equal to page size */
2502         mtd->writesize = this->read_word(this->base + ONENAND_REG_DATA_BUFFER_SIZE);
2503         mtd->oobsize = mtd->writesize >> 5;
2504         /* Pages per a block are always 64 in OneNAND */
2505         mtd->erasesize = mtd->writesize << 6;
2506
2507         this->erase_shift = ffs(mtd->erasesize) - 1;
2508         this->page_shift = ffs(mtd->writesize) - 1;
2509         this->page_mask = (1 << (this->erase_shift - this->page_shift)) - 1;
2510         /* It's real page size */
2511         this->writesize = mtd->writesize;
2512
2513         /* REVIST: Multichip handling */
2514
2515         mtd->size = this->chipsize;
2516
2517         /* Check OneNAND features */
2518         onenand_check_features(mtd);
2519
2520         /*
2521          * We emulate the 4KiB page and 256KiB erase block size
2522          * But oobsize is still 64 bytes.
2523          * It is only valid if you turn on 2X program support,
2524          * Otherwise it will be ignored by compiler.
2525          */
2526         if (ONENAND_IS_2PLANE(this)) {
2527                 mtd->writesize <<= 1;
2528                 mtd->erasesize <<= 1;
2529         }
2530
2531         return 0;
2532 }
2533
2534 /**
2535  * onenand_suspend - [MTD Interface] Suspend the OneNAND flash
2536  * @param mtd           MTD device structure
2537  */
2538 static int onenand_suspend(struct mtd_info *mtd)
2539 {
2540         return onenand_get_device(mtd, FL_PM_SUSPENDED);
2541 }
2542
2543 /**
2544  * onenand_resume - [MTD Interface] Resume the OneNAND flash
2545  * @param mtd           MTD device structure
2546  */
2547 static void onenand_resume(struct mtd_info *mtd)
2548 {
2549         struct onenand_chip *this = mtd->priv;
2550
2551         if (this->state == FL_PM_SUSPENDED)
2552                 onenand_release_device(mtd);
2553         else
2554                 printk(KERN_ERR "resume() called for the chip which is not"
2555                                 "in suspended state\n");
2556 }
2557
2558 /**
2559  * onenand_scan - [OneNAND Interface] Scan for the OneNAND device
2560  * @param mtd           MTD device structure
2561  * @param maxchips      Number of chips to scan for
2562  *
2563  * This fills out all the not initialized function pointers
2564  * with the defaults.
2565  * The flash ID is read and the mtd/chip structures are
2566  * filled with the appropriate values.
2567  */
2568 int onenand_scan(struct mtd_info *mtd, int maxchips)
2569 {
2570         int i;
2571         struct onenand_chip *this = mtd->priv;
2572
2573         if (!this->read_word)
2574                 this->read_word = onenand_readw;
2575         if (!this->write_word)
2576                 this->write_word = onenand_writew;
2577
2578         if (!this->command)
2579                 this->command = onenand_command;
2580         if (!this->wait)
2581                 onenand_setup_wait(mtd);
2582
2583         if (!this->read_bufferram)
2584                 this->read_bufferram = onenand_read_bufferram;
2585         if (!this->write_bufferram)
2586                 this->write_bufferram = onenand_write_bufferram;
2587
2588         if (!this->block_markbad)
2589                 this->block_markbad = onenand_default_block_markbad;
2590         if (!this->scan_bbt)
2591                 this->scan_bbt = onenand_default_bbt;
2592
2593         if (onenand_probe(mtd))
2594                 return -ENXIO;
2595
2596         /* Set Sync. Burst Read after probing */
2597         if (this->mmcontrol) {
2598                 printk(KERN_INFO "OneNAND Sync. Burst Read support\n");
2599                 this->read_bufferram = onenand_sync_read_bufferram;
2600         }
2601
2602         /* Allocate buffers, if necessary */
2603         if (!this->page_buf) {
2604                 this->page_buf = kzalloc(mtd->writesize, GFP_KERNEL);
2605                 if (!this->page_buf) {
2606                         printk(KERN_ERR "onenand_scan(): Can't allocate page_buf\n");
2607                         return -ENOMEM;
2608                 }
2609                 this->options |= ONENAND_PAGEBUF_ALLOC;
2610         }
2611         if (!this->oob_buf) {
2612                 this->oob_buf = kzalloc(mtd->oobsize, GFP_KERNEL);
2613                 if (!this->oob_buf) {
2614                         printk(KERN_ERR "onenand_scan(): Can't allocate oob_buf\n");
2615                         if (this->options & ONENAND_PAGEBUF_ALLOC) {
2616                                 this->options &= ~ONENAND_PAGEBUF_ALLOC;
2617                                 kfree(this->page_buf);
2618                         }
2619                         return -ENOMEM;
2620                 }
2621                 this->options |= ONENAND_OOBBUF_ALLOC;
2622         }
2623
2624         this->state = FL_READY;
2625         init_waitqueue_head(&this->wq);
2626         spin_lock_init(&this->chip_lock);
2627
2628         /*
2629          * Allow subpage writes up to oobsize.
2630          */
2631         switch (mtd->oobsize) {
2632         case 64:
2633                 this->ecclayout = &onenand_oob_64;
2634                 mtd->subpage_sft = 2;
2635                 break;
2636
2637         case 32:
2638                 this->ecclayout = &onenand_oob_32;
2639                 mtd->subpage_sft = 1;
2640                 break;
2641
2642         default:
2643                 printk(KERN_WARNING "No OOB scheme defined for oobsize %d\n",
2644                         mtd->oobsize);
2645                 mtd->subpage_sft = 0;
2646                 /* To prevent kernel oops */
2647                 this->ecclayout = &onenand_oob_32;
2648                 break;
2649         }
2650
2651         this->subpagesize = mtd->writesize >> mtd->subpage_sft;
2652
2653         /*
2654          * The number of bytes available for a client to place data into
2655          * the out of band area
2656          */
2657         this->ecclayout->oobavail = 0;
2658         for (i = 0; i < MTD_MAX_OOBFREE_ENTRIES &&
2659             this->ecclayout->oobfree[i].length; i++)
2660                 this->ecclayout->oobavail +=
2661                         this->ecclayout->oobfree[i].length;
2662         mtd->oobavail = this->ecclayout->oobavail;
2663
2664         mtd->ecclayout = this->ecclayout;
2665
2666         /* Fill in remaining MTD driver data */
2667         mtd->type = MTD_NANDFLASH;
2668         mtd->flags = MTD_CAP_NANDFLASH;
2669         mtd->erase = onenand_erase;
2670         mtd->point = NULL;
2671         mtd->unpoint = NULL;
2672         mtd->read = onenand_read;
2673         mtd->write = onenand_write;
2674         mtd->read_oob = onenand_read_oob;
2675         mtd->write_oob = onenand_write_oob;
2676 #ifdef CONFIG_MTD_ONENAND_OTP
2677         mtd->get_fact_prot_info = onenand_get_fact_prot_info;
2678         mtd->read_fact_prot_reg = onenand_read_fact_prot_reg;
2679         mtd->get_user_prot_info = onenand_get_user_prot_info;
2680         mtd->read_user_prot_reg = onenand_read_user_prot_reg;
2681         mtd->write_user_prot_reg = onenand_write_user_prot_reg;
2682         mtd->lock_user_prot_reg = onenand_lock_user_prot_reg;
2683 #endif
2684         mtd->sync = onenand_sync;
2685         mtd->lock = onenand_lock;
2686         mtd->unlock = onenand_unlock;
2687         mtd->suspend = onenand_suspend;
2688         mtd->resume = onenand_resume;
2689         mtd->block_isbad = onenand_block_isbad;
2690         mtd->block_markbad = onenand_block_markbad;
2691         mtd->owner = THIS_MODULE;
2692
2693         /* Unlock whole block */
2694         onenand_unlock_all(mtd);
2695
2696         return this->scan_bbt(mtd);
2697 }
2698
2699 /**
2700  * onenand_release - [OneNAND Interface] Free resources held by the OneNAND device
2701  * @param mtd           MTD device structure
2702  */
2703 void onenand_release(struct mtd_info *mtd)
2704 {
2705         struct onenand_chip *this = mtd->priv;
2706
2707 #ifdef CONFIG_MTD_PARTITIONS
2708         /* Deregister partitions */
2709         del_mtd_partitions (mtd);
2710 #endif
2711         /* Deregister the device */
2712         del_mtd_device (mtd);
2713
2714         /* Free bad block table memory, if allocated */
2715         if (this->bbm) {
2716                 struct bbm_info *bbm = this->bbm;
2717                 kfree(bbm->bbt);
2718                 kfree(this->bbm);
2719         }
2720         /* Buffers allocated by onenand_scan */
2721         if (this->options & ONENAND_PAGEBUF_ALLOC)
2722                 kfree(this->page_buf);
2723         if (this->options & ONENAND_OOBBUF_ALLOC)
2724                 kfree(this->oob_buf);
2725 }
2726
2727 EXPORT_SYMBOL_GPL(onenand_scan);
2728 EXPORT_SYMBOL_GPL(onenand_release);
2729
2730 MODULE_LICENSE("GPL");
2731 MODULE_AUTHOR("Kyungmin Park <kyungmin.park@samsung.com>");
2732 MODULE_DESCRIPTION("Generic OneNAND flash driver code");