]> bbs.cooldavid.org Git - net-next-2.6.git/blob - drivers/mtd/onenand/onenand_base.c
cf8009329999e1fd9e61fd862278e628eefe2b3f
[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         if (ctrl & ONENAND_CTRL_ERROR) {
1123                 printk(KERN_DEBUG "onenand_bbt_wait: controller error = 0x%04x\n", ctrl);
1124                 /* Initial bad block case */
1125                 if (ctrl & ONENAND_CTRL_LOAD)
1126                         return ONENAND_BBT_READ_ERROR;
1127                 return ONENAND_BBT_READ_FATAL_ERROR;
1128         }
1129
1130         if (interrupt & ONENAND_INT_READ) {
1131                 int ecc = this->read_word(this->base + ONENAND_REG_ECC_STATUS);
1132                 if (ecc & ONENAND_ECC_2BIT_ALL)
1133                         return ONENAND_BBT_READ_ERROR;
1134         } else {
1135                 printk(KERN_ERR "onenand_bbt_wait: read timeout!"
1136                         "ctrl=0x%04x intr=0x%04x\n", ctrl, interrupt);
1137                 return ONENAND_BBT_READ_FATAL_ERROR;
1138         }
1139
1140         return 0;
1141 }
1142
1143 /**
1144  * onenand_bbt_read_oob - [MTD Interface] OneNAND read out-of-band for bbt scan
1145  * @param mtd           MTD device structure
1146  * @param from          offset to read from
1147  * @param ops           oob operation description structure
1148  *
1149  * OneNAND read out-of-band data from the spare area for bbt scan
1150  */
1151 int onenand_bbt_read_oob(struct mtd_info *mtd, loff_t from, 
1152                             struct mtd_oob_ops *ops)
1153 {
1154         struct onenand_chip *this = mtd->priv;
1155         int read = 0, thislen, column;
1156         int ret = 0;
1157         size_t len = ops->ooblen;
1158         u_char *buf = ops->oobbuf;
1159
1160         DEBUG(MTD_DEBUG_LEVEL3, "onenand_bbt_read_oob: from = 0x%08x, len = %zi\n", (unsigned int) from, len);
1161
1162         /* Initialize return value */
1163         ops->oobretlen = 0;
1164
1165         /* Do not allow reads past end of device */
1166         if (unlikely((from + len) > mtd->size)) {
1167                 printk(KERN_ERR "onenand_bbt_read_oob: Attempt read beyond end of device\n");
1168                 return ONENAND_BBT_READ_FATAL_ERROR;
1169         }
1170
1171         /* Grab the lock and see if the device is available */
1172         onenand_get_device(mtd, FL_READING);
1173
1174         column = from & (mtd->oobsize - 1);
1175
1176         while (read < len) {
1177                 cond_resched();
1178
1179                 thislen = mtd->oobsize - column;
1180                 thislen = min_t(int, thislen, len);
1181
1182                 this->command(mtd, ONENAND_CMD_READOOB, from, mtd->oobsize);
1183
1184                 onenand_update_bufferram(mtd, from, 0);
1185
1186                 ret = onenand_bbt_wait(mtd, FL_READING);
1187                 if (ret)
1188                         break;
1189
1190                 this->read_bufferram(mtd, ONENAND_SPARERAM, buf, column, thislen);
1191                 read += thislen;
1192                 if (read == len)
1193                         break;
1194
1195                 buf += thislen;
1196
1197                 /* Read more? */
1198                 if (read < len) {
1199                         /* Update Page size */
1200                         from += this->writesize;
1201                         column = 0;
1202                 }
1203         }
1204
1205         /* Deselect and wake up anyone waiting on the device */
1206         onenand_release_device(mtd);
1207
1208         ops->oobretlen = read;
1209         return ret;
1210 }
1211
1212 #ifdef CONFIG_MTD_ONENAND_VERIFY_WRITE
1213 /**
1214  * onenand_verify_oob - [GENERIC] verify the oob contents after a write
1215  * @param mtd           MTD device structure
1216  * @param buf           the databuffer to verify
1217  * @param to            offset to read from
1218  */
1219 static int onenand_verify_oob(struct mtd_info *mtd, const u_char *buf, loff_t to)
1220 {
1221         struct onenand_chip *this = mtd->priv;
1222         char oobbuf[64];
1223         int status, i;
1224
1225         this->command(mtd, ONENAND_CMD_READOOB, to, mtd->oobsize);
1226         onenand_update_bufferram(mtd, to, 0);
1227         status = this->wait(mtd, FL_READING);
1228         if (status)
1229                 return status;
1230
1231         this->read_bufferram(mtd, ONENAND_SPARERAM, oobbuf, 0, mtd->oobsize);
1232         for (i = 0; i < mtd->oobsize; i++)
1233                 if (buf[i] != 0xFF && buf[i] != oobbuf[i])
1234                         return -EBADMSG;
1235
1236         return 0;
1237 }
1238
1239 /**
1240  * onenand_verify - [GENERIC] verify the chip contents after a write
1241  * @param mtd          MTD device structure
1242  * @param buf          the databuffer to verify
1243  * @param addr         offset to read from
1244  * @param len          number of bytes to read and compare
1245  */
1246 static int onenand_verify(struct mtd_info *mtd, const u_char *buf, loff_t addr, size_t len)
1247 {
1248         struct onenand_chip *this = mtd->priv;
1249         void __iomem *dataram;
1250         int ret = 0;
1251         int thislen, column;
1252
1253         while (len != 0) {
1254                 thislen = min_t(int, this->writesize, len);
1255                 column = addr & (this->writesize - 1);
1256                 if (column + thislen > this->writesize)
1257                         thislen = this->writesize - column;
1258
1259                 this->command(mtd, ONENAND_CMD_READ, addr, this->writesize);
1260
1261                 onenand_update_bufferram(mtd, addr, 0);
1262
1263                 ret = this->wait(mtd, FL_READING);
1264                 if (ret)
1265                         return ret;
1266
1267                 onenand_update_bufferram(mtd, addr, 1);
1268
1269                 dataram = this->base + ONENAND_DATARAM;
1270                 dataram += onenand_bufferram_offset(mtd, ONENAND_DATARAM);
1271
1272                 if (memcmp(buf, dataram + column, thislen))
1273                         return -EBADMSG;
1274
1275                 len -= thislen;
1276                 buf += thislen;
1277                 addr += thislen;
1278         }
1279
1280         return 0;
1281 }
1282 #else
1283 #define onenand_verify(...)             (0)
1284 #define onenand_verify_oob(...)         (0)
1285 #endif
1286
1287 #define NOTALIGNED(x)   ((x & (this->subpagesize - 1)) != 0)
1288
1289 /**
1290  * onenand_fill_auto_oob - [Internal] oob auto-placement transfer
1291  * @param mtd           MTD device structure
1292  * @param oob_buf       oob buffer
1293  * @param buf           source address
1294  * @param column        oob offset to write to
1295  * @param thislen       oob length to write
1296  */
1297 static int onenand_fill_auto_oob(struct mtd_info *mtd, u_char *oob_buf,
1298                                   const u_char *buf, int column, int thislen)
1299 {
1300         struct onenand_chip *this = mtd->priv;
1301         struct nand_oobfree *free;
1302         int writecol = column;
1303         int writeend = column + thislen;
1304         int lastgap = 0;
1305         unsigned int i;
1306
1307         free = this->ecclayout->oobfree;
1308         for (i = 0; i < MTD_MAX_OOBFREE_ENTRIES && free->length; i++, free++) {
1309                 if (writecol >= lastgap)
1310                         writecol += free->offset - lastgap;
1311                 if (writeend >= lastgap)
1312                         writeend += free->offset - lastgap;
1313                 lastgap = free->offset + free->length;
1314         }
1315         free = this->ecclayout->oobfree;
1316         for (i = 0; i < MTD_MAX_OOBFREE_ENTRIES && free->length; i++, free++) {
1317                 int free_end = free->offset + free->length;
1318                 if (free->offset < writeend && free_end > writecol) {
1319                         int st = max_t(int,free->offset,writecol);
1320                         int ed = min_t(int,free_end,writeend);
1321                         int n = ed - st;
1322                         memcpy(oob_buf + st, buf, n);
1323                         buf += n;
1324                 } else if (column == 0)
1325                         break;
1326         }
1327         return 0;
1328 }
1329
1330 /**
1331  * onenand_write_ops_nolock - [OneNAND Interface] write main and/or out-of-band
1332  * @param mtd           MTD device structure
1333  * @param to            offset to write to
1334  * @param ops           oob operation description structure
1335  *
1336  * Write main and/or oob with ECC
1337  */
1338 static int onenand_write_ops_nolock(struct mtd_info *mtd, loff_t to,
1339                                 struct mtd_oob_ops *ops)
1340 {
1341         struct onenand_chip *this = mtd->priv;
1342         int written = 0, column, thislen, subpage;
1343         int oobwritten = 0, oobcolumn, thisooblen, oobsize;
1344         size_t len = ops->len;
1345         size_t ooblen = ops->ooblen;
1346         const u_char *buf = ops->datbuf;
1347         const u_char *oob = ops->oobbuf;
1348         u_char *oobbuf;
1349         int ret = 0;
1350
1351         DEBUG(MTD_DEBUG_LEVEL3, "onenand_write_ops_nolock: to = 0x%08x, len = %i\n", (unsigned int) to, (int) len);
1352
1353         /* Initialize retlen, in case of early exit */
1354         ops->retlen = 0;
1355         ops->oobretlen = 0;
1356
1357         /* Do not allow writes past end of device */
1358         if (unlikely((to + len) > mtd->size)) {
1359                 printk(KERN_ERR "onenand_write_ops_nolock: Attempt write to past end of device\n");
1360                 return -EINVAL;
1361         }
1362
1363         /* Reject writes, which are not page aligned */
1364         if (unlikely(NOTALIGNED(to)) || unlikely(NOTALIGNED(len))) {
1365                 printk(KERN_ERR "onenand_write_ops_nolock: Attempt to write not page aligned data\n");
1366                 return -EINVAL;
1367         }
1368
1369         if (ops->mode == MTD_OOB_AUTO)
1370                 oobsize = this->ecclayout->oobavail;
1371         else
1372                 oobsize = mtd->oobsize;
1373
1374         oobcolumn = to & (mtd->oobsize - 1);
1375
1376         column = to & (mtd->writesize - 1);
1377
1378         /* Loop until all data write */
1379         while (written < len) {
1380                 u_char *wbuf = (u_char *) buf;
1381
1382                 thislen = min_t(int, mtd->writesize - column, len - written);
1383                 thisooblen = min_t(int, oobsize - oobcolumn, ooblen - oobwritten);
1384
1385                 cond_resched();
1386
1387                 this->command(mtd, ONENAND_CMD_BUFFERRAM, to, thislen);
1388
1389                 /* Partial page write */
1390                 subpage = thislen < mtd->writesize;
1391                 if (subpage) {
1392                         memset(this->page_buf, 0xff, mtd->writesize);
1393                         memcpy(this->page_buf + column, buf, thislen);
1394                         wbuf = this->page_buf;
1395                 }
1396
1397                 this->write_bufferram(mtd, ONENAND_DATARAM, wbuf, 0, mtd->writesize);
1398
1399                 if (oob) {
1400                         oobbuf = this->oob_buf;
1401
1402                         /* We send data to spare ram with oobsize
1403                          * to prevent byte access */
1404                         memset(oobbuf, 0xff, mtd->oobsize);
1405                         if (ops->mode == MTD_OOB_AUTO)
1406                                 onenand_fill_auto_oob(mtd, oobbuf, oob, oobcolumn, thisooblen);
1407                         else
1408                                 memcpy(oobbuf + oobcolumn, oob, thisooblen);
1409
1410                         oobwritten += thisooblen;
1411                         oob += thisooblen;
1412                         oobcolumn = 0;
1413                 } else
1414                         oobbuf = (u_char *) ffchars;
1415
1416                 this->write_bufferram(mtd, ONENAND_SPARERAM, oobbuf, 0, mtd->oobsize);
1417
1418                 this->command(mtd, ONENAND_CMD_PROG, to, mtd->writesize);
1419
1420                 ret = this->wait(mtd, FL_WRITING);
1421
1422                 /* In partial page write we don't update bufferram */
1423                 onenand_update_bufferram(mtd, to, !ret && !subpage);
1424                 if (ONENAND_IS_2PLANE(this)) {
1425                         ONENAND_SET_BUFFERRAM1(this);
1426                         onenand_update_bufferram(mtd, to + this->writesize, !ret && !subpage);
1427                 }
1428
1429                 if (ret) {
1430                         printk(KERN_ERR "onenand_write_ops_nolock: write filaed %d\n", ret);
1431                         break;
1432                 }
1433
1434                 /* Only check verify write turn on */
1435                 ret = onenand_verify(mtd, buf, to, thislen);
1436                 if (ret) {
1437                         printk(KERN_ERR "onenand_write_ops_nolock: verify failed %d\n", ret);
1438                         break;
1439                 }
1440
1441                 written += thislen;
1442
1443                 if (written == len)
1444                         break;
1445
1446                 column = 0;
1447                 to += thislen;
1448                 buf += thislen;
1449         }
1450
1451         ops->retlen = written;
1452
1453         return ret;
1454 }
1455
1456
1457 /**
1458  * onenand_write_oob_nolock - [Internal] OneNAND write out-of-band
1459  * @param mtd           MTD device structure
1460  * @param to            offset to write to
1461  * @param len           number of bytes to write
1462  * @param retlen        pointer to variable to store the number of written bytes
1463  * @param buf           the data to write
1464  * @param mode          operation mode
1465  *
1466  * OneNAND write out-of-band
1467  */
1468 static int onenand_write_oob_nolock(struct mtd_info *mtd, loff_t to,
1469                                     struct mtd_oob_ops *ops)
1470 {
1471         struct onenand_chip *this = mtd->priv;
1472         int column, ret = 0, oobsize;
1473         int written = 0;
1474         u_char *oobbuf;
1475         size_t len = ops->ooblen;
1476         const u_char *buf = ops->oobbuf;
1477         mtd_oob_mode_t mode = ops->mode;
1478
1479         to += ops->ooboffs;
1480
1481         DEBUG(MTD_DEBUG_LEVEL3, "onenand_write_oob_nolock: to = 0x%08x, len = %i\n", (unsigned int) to, (int) len);
1482
1483         /* Initialize retlen, in case of early exit */
1484         ops->oobretlen = 0;
1485
1486         if (mode == MTD_OOB_AUTO)
1487                 oobsize = this->ecclayout->oobavail;
1488         else
1489                 oobsize = mtd->oobsize;
1490
1491         column = to & (mtd->oobsize - 1);
1492
1493         if (unlikely(column >= oobsize)) {
1494                 printk(KERN_ERR "onenand_write_oob_nolock: Attempted to start write outside oob\n");
1495                 return -EINVAL;
1496         }
1497
1498         /* For compatibility with NAND: Do not allow write past end of page */
1499         if (unlikely(column + len > oobsize)) {
1500                 printk(KERN_ERR "onenand_write_oob_nolock: "
1501                       "Attempt to write past end of page\n");
1502                 return -EINVAL;
1503         }
1504
1505         /* Do not allow reads past end of device */
1506         if (unlikely(to >= mtd->size ||
1507                      column + len > ((mtd->size >> this->page_shift) -
1508                                      (to >> this->page_shift)) * oobsize)) {
1509                 printk(KERN_ERR "onenand_write_oob_nolock: Attempted to write past end of device\n");
1510                 return -EINVAL;
1511         }
1512
1513         oobbuf = this->oob_buf;
1514
1515         /* Loop until all data write */
1516         while (written < len) {
1517                 int thislen = min_t(int, oobsize, len - written);
1518
1519                 cond_resched();
1520
1521                 this->command(mtd, ONENAND_CMD_BUFFERRAM, to, mtd->oobsize);
1522
1523                 /* We send data to spare ram with oobsize
1524                  * to prevent byte access */
1525                 memset(oobbuf, 0xff, mtd->oobsize);
1526                 if (mode == MTD_OOB_AUTO)
1527                         onenand_fill_auto_oob(mtd, oobbuf, buf, column, thislen);
1528                 else
1529                         memcpy(oobbuf + column, buf, thislen);
1530                 this->write_bufferram(mtd, ONENAND_SPARERAM, oobbuf, 0, mtd->oobsize);
1531
1532                 this->command(mtd, ONENAND_CMD_PROGOOB, to, mtd->oobsize);
1533
1534                 onenand_update_bufferram(mtd, to, 0);
1535                 if (ONENAND_IS_2PLANE(this)) {
1536                         ONENAND_SET_BUFFERRAM1(this);
1537                         onenand_update_bufferram(mtd, to + this->writesize, 0);
1538                 }
1539
1540                 ret = this->wait(mtd, FL_WRITING);
1541                 if (ret) {
1542                         printk(KERN_ERR "onenand_write_oob_nolock: write failed %d\n", ret);
1543                         break;
1544                 }
1545
1546                 ret = onenand_verify_oob(mtd, oobbuf, to);
1547                 if (ret) {
1548                         printk(KERN_ERR "onenand_write_oob_nolock: verify failed %d\n", ret);
1549                         break;
1550                 }
1551
1552                 written += thislen;
1553                 if (written == len)
1554                         break;
1555
1556                 to += mtd->writesize;
1557                 buf += thislen;
1558                 column = 0;
1559         }
1560
1561         ops->oobretlen = written;
1562
1563         return ret;
1564 }
1565
1566 /**
1567  * onenand_write - [MTD Interface] write buffer to FLASH
1568  * @param mtd           MTD device structure
1569  * @param to            offset to write to
1570  * @param len           number of bytes to write
1571  * @param retlen        pointer to variable to store the number of written bytes
1572  * @param buf           the data to write
1573  *
1574  * Write with ECC
1575  */
1576 static int onenand_write(struct mtd_info *mtd, loff_t to, size_t len,
1577         size_t *retlen, const u_char *buf)
1578 {
1579         struct mtd_oob_ops ops = {
1580                 .len    = len,
1581                 .ooblen = 0,
1582                 .datbuf = (u_char *) buf,
1583                 .oobbuf = NULL,
1584         };
1585         int ret;
1586
1587         onenand_get_device(mtd, FL_WRITING);
1588         ret = onenand_write_ops_nolock(mtd, to, &ops);
1589         onenand_release_device(mtd);
1590
1591         *retlen = ops.retlen;
1592         return ret;
1593 }
1594
1595 /**
1596  * onenand_write_oob - [MTD Interface] NAND write data and/or out-of-band
1597  * @param mtd:          MTD device structure
1598  * @param to:           offset to write
1599  * @param ops:          oob operation description structure
1600  */
1601 static int onenand_write_oob(struct mtd_info *mtd, loff_t to,
1602                              struct mtd_oob_ops *ops)
1603 {
1604         int ret;
1605
1606         switch (ops->mode) {
1607         case MTD_OOB_PLACE:
1608         case MTD_OOB_AUTO:
1609                 break;
1610         case MTD_OOB_RAW:
1611                 /* Not implemented yet */
1612         default:
1613                 return -EINVAL;
1614         }
1615
1616         onenand_get_device(mtd, FL_WRITING);
1617         if (ops->datbuf)
1618                 ret = onenand_write_ops_nolock(mtd, to, ops);
1619         else
1620                 ret = onenand_write_oob_nolock(mtd, to, ops);
1621         onenand_release_device(mtd);
1622
1623         return ret;
1624 }
1625
1626 /**
1627  * onenand_block_isbad_nolock - [GENERIC] Check if a block is marked bad
1628  * @param mtd           MTD device structure
1629  * @param ofs           offset from device start
1630  * @param allowbbt      1, if its allowed to access the bbt area
1631  *
1632  * Check, if the block is bad. Either by reading the bad block table or
1633  * calling of the scan function.
1634  */
1635 static int onenand_block_isbad_nolock(struct mtd_info *mtd, loff_t ofs, int allowbbt)
1636 {
1637         struct onenand_chip *this = mtd->priv;
1638         struct bbm_info *bbm = this->bbm;
1639
1640         /* Return info from the table */
1641         return bbm->isbad_bbt(mtd, ofs, allowbbt);
1642 }
1643
1644 /**
1645  * onenand_erase - [MTD Interface] erase block(s)
1646  * @param mtd           MTD device structure
1647  * @param instr         erase instruction
1648  *
1649  * Erase one ore more blocks
1650  */
1651 static int onenand_erase(struct mtd_info *mtd, struct erase_info *instr)
1652 {
1653         struct onenand_chip *this = mtd->priv;
1654         unsigned int block_size;
1655         loff_t addr;
1656         int len;
1657         int ret = 0;
1658
1659         DEBUG(MTD_DEBUG_LEVEL3, "onenand_erase: start = 0x%08x, len = %i\n", (unsigned int) instr->addr, (unsigned int) instr->len);
1660
1661         block_size = (1 << this->erase_shift);
1662
1663         /* Start address must align on block boundary */
1664         if (unlikely(instr->addr & (block_size - 1))) {
1665                 printk(KERN_ERR "onenand_erase: Unaligned address\n");
1666                 return -EINVAL;
1667         }
1668
1669         /* Length must align on block boundary */
1670         if (unlikely(instr->len & (block_size - 1))) {
1671                 printk(KERN_ERR "onenand_erase: Length not block aligned\n");
1672                 return -EINVAL;
1673         }
1674
1675         /* Do not allow erase past end of device */
1676         if (unlikely((instr->len + instr->addr) > mtd->size)) {
1677                 printk(KERN_ERR "onenand_erase: Erase past end of device\n");
1678                 return -EINVAL;
1679         }
1680
1681         instr->fail_addr = 0xffffffff;
1682
1683         /* Grab the lock and see if the device is available */
1684         onenand_get_device(mtd, FL_ERASING);
1685
1686         /* Loop throught the pages */
1687         len = instr->len;
1688         addr = instr->addr;
1689
1690         instr->state = MTD_ERASING;
1691
1692         while (len) {
1693                 cond_resched();
1694
1695                 /* Check if we have a bad block, we do not erase bad blocks */
1696                 if (onenand_block_isbad_nolock(mtd, addr, 0)) {
1697                         printk (KERN_WARNING "onenand_erase: attempt to erase a bad block at addr 0x%08x\n", (unsigned int) addr);
1698                         instr->state = MTD_ERASE_FAILED;
1699                         goto erase_exit;
1700                 }
1701
1702                 this->command(mtd, ONENAND_CMD_ERASE, addr, block_size);
1703
1704                 onenand_invalidate_bufferram(mtd, addr, block_size);
1705
1706                 ret = this->wait(mtd, FL_ERASING);
1707                 /* Check, if it is write protected */
1708                 if (ret) {
1709                         printk(KERN_ERR "onenand_erase: Failed erase, block %d\n", (unsigned) (addr >> this->erase_shift));
1710                         instr->state = MTD_ERASE_FAILED;
1711                         instr->fail_addr = addr;
1712                         goto erase_exit;
1713                 }
1714
1715                 len -= block_size;
1716                 addr += block_size;
1717         }
1718
1719         instr->state = MTD_ERASE_DONE;
1720
1721 erase_exit:
1722
1723         ret = instr->state == MTD_ERASE_DONE ? 0 : -EIO;
1724
1725         /* Deselect and wake up anyone waiting on the device */
1726         onenand_release_device(mtd);
1727
1728         /* Do call back function */
1729         if (!ret)
1730                 mtd_erase_callback(instr);
1731
1732         return ret;
1733 }
1734
1735 /**
1736  * onenand_sync - [MTD Interface] sync
1737  * @param mtd           MTD device structure
1738  *
1739  * Sync is actually a wait for chip ready function
1740  */
1741 static void onenand_sync(struct mtd_info *mtd)
1742 {
1743         DEBUG(MTD_DEBUG_LEVEL3, "onenand_sync: called\n");
1744
1745         /* Grab the lock and see if the device is available */
1746         onenand_get_device(mtd, FL_SYNCING);
1747
1748         /* Release it and go back */
1749         onenand_release_device(mtd);
1750 }
1751
1752 /**
1753  * onenand_block_isbad - [MTD Interface] Check whether the block at the given offset is bad
1754  * @param mtd           MTD device structure
1755  * @param ofs           offset relative to mtd start
1756  *
1757  * Check whether the block is bad
1758  */
1759 static int onenand_block_isbad(struct mtd_info *mtd, loff_t ofs)
1760 {
1761         int ret;
1762
1763         /* Check for invalid offset */
1764         if (ofs > mtd->size)
1765                 return -EINVAL;
1766
1767         onenand_get_device(mtd, FL_READING);
1768         ret = onenand_block_isbad_nolock(mtd, ofs, 0);
1769         onenand_release_device(mtd);
1770         return ret;
1771 }
1772
1773 /**
1774  * onenand_default_block_markbad - [DEFAULT] mark a block bad
1775  * @param mtd           MTD device structure
1776  * @param ofs           offset from device start
1777  *
1778  * This is the default implementation, which can be overridden by
1779  * a hardware specific driver.
1780  */
1781 static int onenand_default_block_markbad(struct mtd_info *mtd, loff_t ofs)
1782 {
1783         struct onenand_chip *this = mtd->priv;
1784         struct bbm_info *bbm = this->bbm;
1785         u_char buf[2] = {0, 0};
1786         struct mtd_oob_ops ops = {
1787                 .mode = MTD_OOB_PLACE,
1788                 .ooblen = 2,
1789                 .oobbuf = buf,
1790                 .ooboffs = 0,
1791         };
1792         int block;
1793
1794         /* Get block number */
1795         block = ((int) ofs) >> bbm->bbt_erase_shift;
1796         if (bbm->bbt)
1797                 bbm->bbt[block >> 2] |= 0x01 << ((block & 0x03) << 1);
1798
1799         /* We write two bytes, so we dont have to mess with 16 bit access */
1800         ofs += mtd->oobsize + (bbm->badblockpos & ~0x01);
1801         return onenand_write_oob_nolock(mtd, ofs, &ops);
1802 }
1803
1804 /**
1805  * onenand_block_markbad - [MTD Interface] Mark the block at the given offset as bad
1806  * @param mtd           MTD device structure
1807  * @param ofs           offset relative to mtd start
1808  *
1809  * Mark the block as bad
1810  */
1811 static int onenand_block_markbad(struct mtd_info *mtd, loff_t ofs)
1812 {
1813         struct onenand_chip *this = mtd->priv;
1814         int ret;
1815
1816         ret = onenand_block_isbad(mtd, ofs);
1817         if (ret) {
1818                 /* If it was bad already, return success and do nothing */
1819                 if (ret > 0)
1820                         return 0;
1821                 return ret;
1822         }
1823
1824         onenand_get_device(mtd, FL_WRITING);
1825         ret = this->block_markbad(mtd, ofs);
1826         onenand_release_device(mtd);
1827         return ret;
1828 }
1829
1830 /**
1831  * onenand_do_lock_cmd - [OneNAND Interface] Lock or unlock block(s)
1832  * @param mtd           MTD device structure
1833  * @param ofs           offset relative to mtd start
1834  * @param len           number of bytes to lock or unlock
1835  * @param cmd           lock or unlock command
1836  *
1837  * Lock or unlock one or more blocks
1838  */
1839 static int onenand_do_lock_cmd(struct mtd_info *mtd, loff_t ofs, size_t len, int cmd)
1840 {
1841         struct onenand_chip *this = mtd->priv;
1842         int start, end, block, value, status;
1843         int wp_status_mask;
1844
1845         start = ofs >> this->erase_shift;
1846         end = len >> this->erase_shift;
1847
1848         if (cmd == ONENAND_CMD_LOCK)
1849                 wp_status_mask = ONENAND_WP_LS;
1850         else
1851                 wp_status_mask = ONENAND_WP_US;
1852
1853         /* Continuous lock scheme */
1854         if (this->options & ONENAND_HAS_CONT_LOCK) {
1855                 /* Set start block address */
1856                 this->write_word(start, this->base + ONENAND_REG_START_BLOCK_ADDRESS);
1857                 /* Set end block address */
1858                 this->write_word(start + end - 1, this->base + ONENAND_REG_END_BLOCK_ADDRESS);
1859                 /* Write lock command */
1860                 this->command(mtd, cmd, 0, 0);
1861
1862                 /* There's no return value */
1863                 this->wait(mtd, FL_LOCKING);
1864
1865                 /* Sanity check */
1866                 while (this->read_word(this->base + ONENAND_REG_CTRL_STATUS)
1867                     & ONENAND_CTRL_ONGO)
1868                         continue;
1869
1870                 /* Check lock status */
1871                 status = this->read_word(this->base + ONENAND_REG_WP_STATUS);
1872                 if (!(status & wp_status_mask))
1873                         printk(KERN_ERR "wp status = 0x%x\n", status);
1874
1875                 return 0;
1876         }
1877
1878         /* Block lock scheme */
1879         for (block = start; block < start + end; block++) {
1880                 /* Set block address */
1881                 value = onenand_block_address(this, block);
1882                 this->write_word(value, this->base + ONENAND_REG_START_ADDRESS1);
1883                 /* Select DataRAM for DDP */
1884                 value = onenand_bufferram_address(this, block);
1885                 this->write_word(value, this->base + ONENAND_REG_START_ADDRESS2);
1886                 /* Set start block address */
1887                 this->write_word(block, this->base + ONENAND_REG_START_BLOCK_ADDRESS);
1888                 /* Write lock command */
1889                 this->command(mtd, cmd, 0, 0);
1890
1891                 /* There's no return value */
1892                 this->wait(mtd, FL_LOCKING);
1893
1894                 /* Sanity check */
1895                 while (this->read_word(this->base + ONENAND_REG_CTRL_STATUS)
1896                     & ONENAND_CTRL_ONGO)
1897                         continue;
1898
1899                 /* Check lock status */
1900                 status = this->read_word(this->base + ONENAND_REG_WP_STATUS);
1901                 if (!(status & wp_status_mask))
1902                         printk(KERN_ERR "block = %d, wp status = 0x%x\n", block, status);
1903         }
1904
1905         return 0;
1906 }
1907
1908 /**
1909  * onenand_lock - [MTD Interface] Lock block(s)
1910  * @param mtd           MTD device structure
1911  * @param ofs           offset relative to mtd start
1912  * @param len           number of bytes to unlock
1913  *
1914  * Lock one or more blocks
1915  */
1916 static int onenand_lock(struct mtd_info *mtd, loff_t ofs, size_t len)
1917 {
1918         int ret;
1919
1920         onenand_get_device(mtd, FL_LOCKING);
1921         ret = onenand_do_lock_cmd(mtd, ofs, len, ONENAND_CMD_LOCK);
1922         onenand_release_device(mtd);
1923         return ret;
1924 }
1925
1926 /**
1927  * onenand_unlock - [MTD Interface] Unlock block(s)
1928  * @param mtd           MTD device structure
1929  * @param ofs           offset relative to mtd start
1930  * @param len           number of bytes to unlock
1931  *
1932  * Unlock one or more blocks
1933  */
1934 static int onenand_unlock(struct mtd_info *mtd, loff_t ofs, size_t len)
1935 {
1936         int ret;
1937
1938         onenand_get_device(mtd, FL_LOCKING);
1939         ret = onenand_do_lock_cmd(mtd, ofs, len, ONENAND_CMD_UNLOCK);
1940         onenand_release_device(mtd);
1941         return ret;
1942 }
1943
1944 /**
1945  * onenand_check_lock_status - [OneNAND Interface] Check lock status
1946  * @param this          onenand chip data structure
1947  *
1948  * Check lock status
1949  */
1950 static void onenand_check_lock_status(struct onenand_chip *this)
1951 {
1952         unsigned int value, block, status;
1953         unsigned int end;
1954
1955         end = this->chipsize >> this->erase_shift;
1956         for (block = 0; block < end; block++) {
1957                 /* Set block address */
1958                 value = onenand_block_address(this, block);
1959                 this->write_word(value, this->base + ONENAND_REG_START_ADDRESS1);
1960                 /* Select DataRAM for DDP */
1961                 value = onenand_bufferram_address(this, block);
1962                 this->write_word(value, this->base + ONENAND_REG_START_ADDRESS2);
1963                 /* Set start block address */
1964                 this->write_word(block, this->base + ONENAND_REG_START_BLOCK_ADDRESS);
1965
1966                 /* Check lock status */
1967                 status = this->read_word(this->base + ONENAND_REG_WP_STATUS);
1968                 if (!(status & ONENAND_WP_US))
1969                         printk(KERN_ERR "block = %d, wp status = 0x%x\n", block, status);
1970         }
1971 }
1972
1973 /**
1974  * onenand_unlock_all - [OneNAND Interface] unlock all blocks
1975  * @param mtd           MTD device structure
1976  *
1977  * Unlock all blocks
1978  */
1979 static int onenand_unlock_all(struct mtd_info *mtd)
1980 {
1981         struct onenand_chip *this = mtd->priv;
1982
1983         if (this->options & ONENAND_HAS_UNLOCK_ALL) {
1984                 /* Set start block address */
1985                 this->write_word(0, this->base + ONENAND_REG_START_BLOCK_ADDRESS);
1986                 /* Write unlock command */
1987                 this->command(mtd, ONENAND_CMD_UNLOCK_ALL, 0, 0);
1988
1989                 /* There's no return value */
1990                 this->wait(mtd, FL_LOCKING);
1991
1992                 /* Sanity check */
1993                 while (this->read_word(this->base + ONENAND_REG_CTRL_STATUS)
1994                     & ONENAND_CTRL_ONGO)
1995                         continue;
1996
1997                 /* Workaround for all block unlock in DDP */
1998                 if (ONENAND_IS_DDP(this)) {
1999                         /* 1st block on another chip */
2000                         loff_t ofs = this->chipsize >> 1;
2001                         size_t len = mtd->erasesize;
2002
2003                         onenand_do_lock_cmd(mtd, ofs, len, ONENAND_CMD_UNLOCK);
2004                 }
2005
2006                 onenand_check_lock_status(this);
2007
2008                 return 0;
2009         }
2010
2011         onenand_do_lock_cmd(mtd, 0x0, this->chipsize, ONENAND_CMD_UNLOCK);
2012
2013         return 0;
2014 }
2015
2016 #ifdef CONFIG_MTD_ONENAND_OTP
2017
2018 /* Interal OTP operation */
2019 typedef int (*otp_op_t)(struct mtd_info *mtd, loff_t form, size_t len,
2020                 size_t *retlen, u_char *buf);
2021
2022 /**
2023  * do_otp_read - [DEFAULT] Read OTP block area
2024  * @param mtd           MTD device structure
2025  * @param from          The offset to read
2026  * @param len           number of bytes to read
2027  * @param retlen        pointer to variable to store the number of readbytes
2028  * @param buf           the databuffer to put/get data
2029  *
2030  * Read OTP block area.
2031  */
2032 static int do_otp_read(struct mtd_info *mtd, loff_t from, size_t len,
2033                 size_t *retlen, u_char *buf)
2034 {
2035         struct onenand_chip *this = mtd->priv;
2036         struct mtd_oob_ops ops = {
2037                 .len    = len,
2038                 .ooblen = 0,
2039                 .datbuf = buf,
2040                 .oobbuf = NULL,
2041         };
2042         int ret;
2043
2044         /* Enter OTP access mode */
2045         this->command(mtd, ONENAND_CMD_OTP_ACCESS, 0, 0);
2046         this->wait(mtd, FL_OTPING);
2047
2048         ret = onenand_read_ops_nolock(mtd, from, &ops);
2049
2050         /* Exit OTP access mode */
2051         this->command(mtd, ONENAND_CMD_RESET, 0, 0);
2052         this->wait(mtd, FL_RESETING);
2053
2054         return ret;
2055 }
2056
2057 /**
2058  * do_otp_write - [DEFAULT] Write OTP block area
2059  * @param mtd           MTD device structure
2060  * @param to            The offset to write
2061  * @param len           number of bytes to write
2062  * @param retlen        pointer to variable to store the number of write bytes
2063  * @param buf           the databuffer to put/get data
2064  *
2065  * Write OTP block area.
2066  */
2067 static int do_otp_write(struct mtd_info *mtd, loff_t to, size_t len,
2068                 size_t *retlen, u_char *buf)
2069 {
2070         struct onenand_chip *this = mtd->priv;
2071         unsigned char *pbuf = buf;
2072         int ret;
2073         struct mtd_oob_ops ops;
2074
2075         /* Force buffer page aligned */
2076         if (len < mtd->writesize) {
2077                 memcpy(this->page_buf, buf, len);
2078                 memset(this->page_buf + len, 0xff, mtd->writesize - len);
2079                 pbuf = this->page_buf;
2080                 len = mtd->writesize;
2081         }
2082
2083         /* Enter OTP access mode */
2084         this->command(mtd, ONENAND_CMD_OTP_ACCESS, 0, 0);
2085         this->wait(mtd, FL_OTPING);
2086
2087         ops.len = len;
2088         ops.ooblen = 0;
2089         ops.datbuf = pbuf;
2090         ops.oobbuf = NULL;
2091         ret = onenand_write_ops_nolock(mtd, to, &ops);
2092         *retlen = ops.retlen;
2093
2094         /* Exit OTP access mode */
2095         this->command(mtd, ONENAND_CMD_RESET, 0, 0);
2096         this->wait(mtd, FL_RESETING);
2097
2098         return ret;
2099 }
2100
2101 /**
2102  * do_otp_lock - [DEFAULT] Lock OTP block area
2103  * @param mtd           MTD device structure
2104  * @param from          The offset to lock
2105  * @param len           number of bytes to lock
2106  * @param retlen        pointer to variable to store the number of lock bytes
2107  * @param buf           the databuffer to put/get data
2108  *
2109  * Lock OTP block area.
2110  */
2111 static int do_otp_lock(struct mtd_info *mtd, loff_t from, size_t len,
2112                 size_t *retlen, u_char *buf)
2113 {
2114         struct onenand_chip *this = mtd->priv;
2115         struct mtd_oob_ops ops = {
2116                 .mode = MTD_OOB_PLACE,
2117                 .ooblen = len,
2118                 .oobbuf = buf,
2119                 .ooboffs = 0,
2120         };
2121         int ret;
2122
2123         /* Enter OTP access mode */
2124         this->command(mtd, ONENAND_CMD_OTP_ACCESS, 0, 0);
2125         this->wait(mtd, FL_OTPING);
2126
2127         ret = onenand_write_oob_nolock(mtd, from, &ops);
2128
2129         *retlen = ops.oobretlen;
2130
2131         /* Exit OTP access mode */
2132         this->command(mtd, ONENAND_CMD_RESET, 0, 0);
2133         this->wait(mtd, FL_RESETING);
2134
2135         return ret;
2136 }
2137
2138 /**
2139  * onenand_otp_walk - [DEFAULT] Handle OTP operation
2140  * @param mtd           MTD device structure
2141  * @param from          The offset to read/write
2142  * @param len           number of bytes to read/write
2143  * @param retlen        pointer to variable to store the number of read bytes
2144  * @param buf           the databuffer to put/get data
2145  * @param action        do given action
2146  * @param mode          specify user and factory
2147  *
2148  * Handle OTP operation.
2149  */
2150 static int onenand_otp_walk(struct mtd_info *mtd, loff_t from, size_t len,
2151                         size_t *retlen, u_char *buf,
2152                         otp_op_t action, int mode)
2153 {
2154         struct onenand_chip *this = mtd->priv;
2155         int otp_pages;
2156         int density;
2157         int ret = 0;
2158
2159         *retlen = 0;
2160
2161         density = onenand_get_density(this->device_id);
2162         if (density < ONENAND_DEVICE_DENSITY_512Mb)
2163                 otp_pages = 20;
2164         else
2165                 otp_pages = 10;
2166
2167         if (mode == MTD_OTP_FACTORY) {
2168                 from += mtd->writesize * otp_pages;
2169                 otp_pages = 64 - otp_pages;
2170         }
2171
2172         /* Check User/Factory boundary */
2173         if (((mtd->writesize * otp_pages) - (from + len)) < 0)
2174                 return 0;
2175
2176         onenand_get_device(mtd, FL_OTPING);
2177         while (len > 0 && otp_pages > 0) {
2178                 if (!action) {  /* OTP Info functions */
2179                         struct otp_info *otpinfo;
2180
2181                         len -= sizeof(struct otp_info);
2182                         if (len <= 0) {
2183                                 ret = -ENOSPC;
2184                                 break;
2185                         }
2186
2187                         otpinfo = (struct otp_info *) buf;
2188                         otpinfo->start = from;
2189                         otpinfo->length = mtd->writesize;
2190                         otpinfo->locked = 0;
2191
2192                         from += mtd->writesize;
2193                         buf += sizeof(struct otp_info);
2194                         *retlen += sizeof(struct otp_info);
2195                 } else {
2196                         size_t tmp_retlen;
2197                         int size = len;
2198
2199                         ret = action(mtd, from, len, &tmp_retlen, buf);
2200
2201                         buf += size;
2202                         len -= size;
2203                         *retlen += size;
2204
2205                         if (ret)
2206                                 break;
2207                 }
2208                 otp_pages--;
2209         }
2210         onenand_release_device(mtd);
2211
2212         return ret;
2213 }
2214
2215 /**
2216  * onenand_get_fact_prot_info - [MTD Interface] Read factory OTP info
2217  * @param mtd           MTD device structure
2218  * @param buf           the databuffer to put/get data
2219  * @param len           number of bytes to read
2220  *
2221  * Read factory OTP info.
2222  */
2223 static int onenand_get_fact_prot_info(struct mtd_info *mtd,
2224                         struct otp_info *buf, size_t len)
2225 {
2226         size_t retlen;
2227         int ret;
2228
2229         ret = onenand_otp_walk(mtd, 0, len, &retlen, (u_char *) buf, NULL, MTD_OTP_FACTORY);
2230
2231         return ret ? : retlen;
2232 }
2233
2234 /**
2235  * onenand_read_fact_prot_reg - [MTD Interface] Read factory OTP area
2236  * @param mtd           MTD device structure
2237  * @param from          The offset to read
2238  * @param len           number of bytes to read
2239  * @param retlen        pointer to variable to store the number of read bytes
2240  * @param buf           the databuffer to put/get data
2241  *
2242  * Read factory OTP area.
2243  */
2244 static int onenand_read_fact_prot_reg(struct mtd_info *mtd, loff_t from,
2245                         size_t len, size_t *retlen, u_char *buf)
2246 {
2247         return onenand_otp_walk(mtd, from, len, retlen, buf, do_otp_read, MTD_OTP_FACTORY);
2248 }
2249
2250 /**
2251  * onenand_get_user_prot_info - [MTD Interface] Read user OTP info
2252  * @param mtd           MTD device structure
2253  * @param buf           the databuffer to put/get data
2254  * @param len           number of bytes to read
2255  *
2256  * Read user OTP info.
2257  */
2258 static int onenand_get_user_prot_info(struct mtd_info *mtd,
2259                         struct otp_info *buf, size_t len)
2260 {
2261         size_t retlen;
2262         int ret;
2263
2264         ret = onenand_otp_walk(mtd, 0, len, &retlen, (u_char *) buf, NULL, MTD_OTP_USER);
2265
2266         return ret ? : retlen;
2267 }
2268
2269 /**
2270  * onenand_read_user_prot_reg - [MTD Interface] Read user OTP area
2271  * @param mtd           MTD device structure
2272  * @param from          The offset to read
2273  * @param len           number of bytes to read
2274  * @param retlen        pointer to variable to store the number of read bytes
2275  * @param buf           the databuffer to put/get data
2276  *
2277  * Read user OTP area.
2278  */
2279 static int onenand_read_user_prot_reg(struct mtd_info *mtd, loff_t from,
2280                         size_t len, size_t *retlen, u_char *buf)
2281 {
2282         return onenand_otp_walk(mtd, from, len, retlen, buf, do_otp_read, MTD_OTP_USER);
2283 }
2284
2285 /**
2286  * onenand_write_user_prot_reg - [MTD Interface] Write user OTP area
2287  * @param mtd           MTD device structure
2288  * @param from          The offset to write
2289  * @param len           number of bytes to write
2290  * @param retlen        pointer to variable to store the number of write bytes
2291  * @param buf           the databuffer to put/get data
2292  *
2293  * Write user OTP area.
2294  */
2295 static int onenand_write_user_prot_reg(struct mtd_info *mtd, loff_t from,
2296                         size_t len, size_t *retlen, u_char *buf)
2297 {
2298         return onenand_otp_walk(mtd, from, len, retlen, buf, do_otp_write, MTD_OTP_USER);
2299 }
2300
2301 /**
2302  * onenand_lock_user_prot_reg - [MTD Interface] Lock user OTP area
2303  * @param mtd           MTD device structure
2304  * @param from          The offset to lock
2305  * @param len           number of bytes to unlock
2306  *
2307  * Write lock mark on spare area in page 0 in OTP block
2308  */
2309 static int onenand_lock_user_prot_reg(struct mtd_info *mtd, loff_t from,
2310                         size_t len)
2311 {
2312         unsigned char oob_buf[64];
2313         size_t retlen;
2314         int ret;
2315
2316         memset(oob_buf, 0xff, mtd->oobsize);
2317         /*
2318          * Note: OTP lock operation
2319          *       OTP block : 0xXXFC
2320          *       1st block : 0xXXF3 (If chip support)
2321          *       Both      : 0xXXF0 (If chip support)
2322          */
2323         oob_buf[ONENAND_OTP_LOCK_OFFSET] = 0xFC;
2324
2325         /*
2326          * Write lock mark to 8th word of sector0 of page0 of the spare0.
2327          * We write 16 bytes spare area instead of 2 bytes.
2328          */
2329         from = 0;
2330         len = 16;
2331
2332         ret = onenand_otp_walk(mtd, from, len, &retlen, oob_buf, do_otp_lock, MTD_OTP_USER);
2333
2334         return ret ? : retlen;
2335 }
2336 #endif  /* CONFIG_MTD_ONENAND_OTP */
2337
2338 /**
2339  * onenand_check_features - Check and set OneNAND features
2340  * @param mtd           MTD data structure
2341  *
2342  * Check and set OneNAND features
2343  * - lock scheme
2344  * - two plane
2345  */
2346 static void onenand_check_features(struct mtd_info *mtd)
2347 {
2348         struct onenand_chip *this = mtd->priv;
2349         unsigned int density, process;
2350
2351         /* Lock scheme depends on density and process */
2352         density = onenand_get_density(this->device_id);
2353         process = this->version_id >> ONENAND_VERSION_PROCESS_SHIFT;
2354
2355         /* Lock scheme */
2356         switch (density) {
2357         case ONENAND_DEVICE_DENSITY_4Gb:
2358                 this->options |= ONENAND_HAS_2PLANE;
2359
2360         case ONENAND_DEVICE_DENSITY_2Gb:
2361                 /* 2Gb DDP don't have 2 plane */
2362                 if (!ONENAND_IS_DDP(this))
2363                         this->options |= ONENAND_HAS_2PLANE;
2364                 this->options |= ONENAND_HAS_UNLOCK_ALL;
2365
2366         case ONENAND_DEVICE_DENSITY_1Gb:
2367                 /* A-Die has all block unlock */
2368                 if (process)
2369                         this->options |= ONENAND_HAS_UNLOCK_ALL;
2370                 break;
2371
2372         default:
2373                 /* Some OneNAND has continuous lock scheme */
2374                 if (!process)
2375                         this->options |= ONENAND_HAS_CONT_LOCK;
2376                 break;
2377         }
2378
2379         if (this->options & ONENAND_HAS_CONT_LOCK)
2380                 printk(KERN_DEBUG "Lock scheme is Continuous Lock\n");
2381         if (this->options & ONENAND_HAS_UNLOCK_ALL)
2382                 printk(KERN_DEBUG "Chip support all block unlock\n");
2383         if (this->options & ONENAND_HAS_2PLANE)
2384                 printk(KERN_DEBUG "Chip has 2 plane\n");
2385 }
2386
2387 /**
2388  * onenand_print_device_info - Print device & version ID
2389  * @param device        device ID
2390  * @param version       version ID
2391  *
2392  * Print device & version ID
2393  */
2394 static void onenand_print_device_info(int device, int version)
2395 {
2396         int vcc, demuxed, ddp, density;
2397
2398         vcc = device & ONENAND_DEVICE_VCC_MASK;
2399         demuxed = device & ONENAND_DEVICE_IS_DEMUX;
2400         ddp = device & ONENAND_DEVICE_IS_DDP;
2401         density = onenand_get_density(device);
2402         printk(KERN_INFO "%sOneNAND%s %dMB %sV 16-bit (0x%02x)\n",
2403                 demuxed ? "" : "Muxed ",
2404                 ddp ? "(DDP)" : "",
2405                 (16 << density),
2406                 vcc ? "2.65/3.3" : "1.8",
2407                 device);
2408         printk(KERN_INFO "OneNAND version = 0x%04x\n", version);
2409 }
2410
2411 static const struct onenand_manufacturers onenand_manuf_ids[] = {
2412         {ONENAND_MFR_SAMSUNG, "Samsung"},
2413 };
2414
2415 /**
2416  * onenand_check_maf - Check manufacturer ID
2417  * @param manuf         manufacturer ID
2418  *
2419  * Check manufacturer ID
2420  */
2421 static int onenand_check_maf(int manuf)
2422 {
2423         int size = ARRAY_SIZE(onenand_manuf_ids);
2424         char *name;
2425         int i;
2426
2427         for (i = 0; i < size; i++)
2428                 if (manuf == onenand_manuf_ids[i].id)
2429                         break;
2430
2431         if (i < size)
2432                 name = onenand_manuf_ids[i].name;
2433         else
2434                 name = "Unknown";
2435
2436         printk(KERN_DEBUG "OneNAND Manufacturer: %s (0x%0x)\n", name, manuf);
2437
2438         return (i == size);
2439 }
2440
2441 /**
2442  * onenand_probe - [OneNAND Interface] Probe the OneNAND device
2443  * @param mtd           MTD device structure
2444  *
2445  * OneNAND detection method:
2446  *   Compare the values from command with ones from register
2447  */
2448 static int onenand_probe(struct mtd_info *mtd)
2449 {
2450         struct onenand_chip *this = mtd->priv;
2451         int bram_maf_id, bram_dev_id, maf_id, dev_id, ver_id;
2452         int density;
2453         int syscfg;
2454
2455         /* Save system configuration 1 */
2456         syscfg = this->read_word(this->base + ONENAND_REG_SYS_CFG1);
2457         /* Clear Sync. Burst Read mode to read BootRAM */
2458         this->write_word((syscfg & ~ONENAND_SYS_CFG1_SYNC_READ), this->base + ONENAND_REG_SYS_CFG1);
2459
2460         /* Send the command for reading device ID from BootRAM */
2461         this->write_word(ONENAND_CMD_READID, this->base + ONENAND_BOOTRAM);
2462
2463         /* Read manufacturer and device IDs from BootRAM */
2464         bram_maf_id = this->read_word(this->base + ONENAND_BOOTRAM + 0x0);
2465         bram_dev_id = this->read_word(this->base + ONENAND_BOOTRAM + 0x2);
2466
2467         /* Reset OneNAND to read default register values */
2468         this->write_word(ONENAND_CMD_RESET, this->base + ONENAND_BOOTRAM);
2469         /* Wait reset */
2470         this->wait(mtd, FL_RESETING);
2471
2472         /* Restore system configuration 1 */
2473         this->write_word(syscfg, this->base + ONENAND_REG_SYS_CFG1);
2474
2475         /* Check manufacturer ID */
2476         if (onenand_check_maf(bram_maf_id))
2477                 return -ENXIO;
2478
2479         /* Read manufacturer and device IDs from Register */
2480         maf_id = this->read_word(this->base + ONENAND_REG_MANUFACTURER_ID);
2481         dev_id = this->read_word(this->base + ONENAND_REG_DEVICE_ID);
2482         ver_id = this->read_word(this->base + ONENAND_REG_VERSION_ID);
2483
2484         /* Check OneNAND device */
2485         if (maf_id != bram_maf_id || dev_id != bram_dev_id)
2486                 return -ENXIO;
2487
2488         /* Flash device information */
2489         onenand_print_device_info(dev_id, ver_id);
2490         this->device_id = dev_id;
2491         this->version_id = ver_id;
2492
2493         density = onenand_get_density(dev_id);
2494         this->chipsize = (16 << density) << 20;
2495         /* Set density mask. it is used for DDP */
2496         if (ONENAND_IS_DDP(this))
2497                 this->density_mask = (1 << (density + 6));
2498         else
2499                 this->density_mask = 0;
2500
2501         /* OneNAND page size & block size */
2502         /* The data buffer size is equal to page size */
2503         mtd->writesize = this->read_word(this->base + ONENAND_REG_DATA_BUFFER_SIZE);
2504         mtd->oobsize = mtd->writesize >> 5;
2505         /* Pages per a block are always 64 in OneNAND */
2506         mtd->erasesize = mtd->writesize << 6;
2507
2508         this->erase_shift = ffs(mtd->erasesize) - 1;
2509         this->page_shift = ffs(mtd->writesize) - 1;
2510         this->page_mask = (1 << (this->erase_shift - this->page_shift)) - 1;
2511         /* It's real page size */
2512         this->writesize = mtd->writesize;
2513
2514         /* REVIST: Multichip handling */
2515
2516         mtd->size = this->chipsize;
2517
2518         /* Check OneNAND features */
2519         onenand_check_features(mtd);
2520
2521         /*
2522          * We emulate the 4KiB page and 256KiB erase block size
2523          * But oobsize is still 64 bytes.
2524          * It is only valid if you turn on 2X program support,
2525          * Otherwise it will be ignored by compiler.
2526          */
2527         if (ONENAND_IS_2PLANE(this)) {
2528                 mtd->writesize <<= 1;
2529                 mtd->erasesize <<= 1;
2530         }
2531
2532         return 0;
2533 }
2534
2535 /**
2536  * onenand_suspend - [MTD Interface] Suspend the OneNAND flash
2537  * @param mtd           MTD device structure
2538  */
2539 static int onenand_suspend(struct mtd_info *mtd)
2540 {
2541         return onenand_get_device(mtd, FL_PM_SUSPENDED);
2542 }
2543
2544 /**
2545  * onenand_resume - [MTD Interface] Resume the OneNAND flash
2546  * @param mtd           MTD device structure
2547  */
2548 static void onenand_resume(struct mtd_info *mtd)
2549 {
2550         struct onenand_chip *this = mtd->priv;
2551
2552         if (this->state == FL_PM_SUSPENDED)
2553                 onenand_release_device(mtd);
2554         else
2555                 printk(KERN_ERR "resume() called for the chip which is not"
2556                                 "in suspended state\n");
2557 }
2558
2559 /**
2560  * onenand_scan - [OneNAND Interface] Scan for the OneNAND device
2561  * @param mtd           MTD device structure
2562  * @param maxchips      Number of chips to scan for
2563  *
2564  * This fills out all the not initialized function pointers
2565  * with the defaults.
2566  * The flash ID is read and the mtd/chip structures are
2567  * filled with the appropriate values.
2568  */
2569 int onenand_scan(struct mtd_info *mtd, int maxchips)
2570 {
2571         int i;
2572         struct onenand_chip *this = mtd->priv;
2573
2574         if (!this->read_word)
2575                 this->read_word = onenand_readw;
2576         if (!this->write_word)
2577                 this->write_word = onenand_writew;
2578
2579         if (!this->command)
2580                 this->command = onenand_command;
2581         if (!this->wait)
2582                 onenand_setup_wait(mtd);
2583
2584         if (!this->read_bufferram)
2585                 this->read_bufferram = onenand_read_bufferram;
2586         if (!this->write_bufferram)
2587                 this->write_bufferram = onenand_write_bufferram;
2588
2589         if (!this->block_markbad)
2590                 this->block_markbad = onenand_default_block_markbad;
2591         if (!this->scan_bbt)
2592                 this->scan_bbt = onenand_default_bbt;
2593
2594         if (onenand_probe(mtd))
2595                 return -ENXIO;
2596
2597         /* Set Sync. Burst Read after probing */
2598         if (this->mmcontrol) {
2599                 printk(KERN_INFO "OneNAND Sync. Burst Read support\n");
2600                 this->read_bufferram = onenand_sync_read_bufferram;
2601         }
2602
2603         /* Allocate buffers, if necessary */
2604         if (!this->page_buf) {
2605                 this->page_buf = kzalloc(mtd->writesize, GFP_KERNEL);
2606                 if (!this->page_buf) {
2607                         printk(KERN_ERR "onenand_scan(): Can't allocate page_buf\n");
2608                         return -ENOMEM;
2609                 }
2610                 this->options |= ONENAND_PAGEBUF_ALLOC;
2611         }
2612         if (!this->oob_buf) {
2613                 this->oob_buf = kzalloc(mtd->oobsize, GFP_KERNEL);
2614                 if (!this->oob_buf) {
2615                         printk(KERN_ERR "onenand_scan(): Can't allocate oob_buf\n");
2616                         if (this->options & ONENAND_PAGEBUF_ALLOC) {
2617                                 this->options &= ~ONENAND_PAGEBUF_ALLOC;
2618                                 kfree(this->page_buf);
2619                         }
2620                         return -ENOMEM;
2621                 }
2622                 this->options |= ONENAND_OOBBUF_ALLOC;
2623         }
2624
2625         this->state = FL_READY;
2626         init_waitqueue_head(&this->wq);
2627         spin_lock_init(&this->chip_lock);
2628
2629         /*
2630          * Allow subpage writes up to oobsize.
2631          */
2632         switch (mtd->oobsize) {
2633         case 64:
2634                 this->ecclayout = &onenand_oob_64;
2635                 mtd->subpage_sft = 2;
2636                 break;
2637
2638         case 32:
2639                 this->ecclayout = &onenand_oob_32;
2640                 mtd->subpage_sft = 1;
2641                 break;
2642
2643         default:
2644                 printk(KERN_WARNING "No OOB scheme defined for oobsize %d\n",
2645                         mtd->oobsize);
2646                 mtd->subpage_sft = 0;
2647                 /* To prevent kernel oops */
2648                 this->ecclayout = &onenand_oob_32;
2649                 break;
2650         }
2651
2652         this->subpagesize = mtd->writesize >> mtd->subpage_sft;
2653
2654         /*
2655          * The number of bytes available for a client to place data into
2656          * the out of band area
2657          */
2658         this->ecclayout->oobavail = 0;
2659         for (i = 0; i < MTD_MAX_OOBFREE_ENTRIES &&
2660             this->ecclayout->oobfree[i].length; i++)
2661                 this->ecclayout->oobavail +=
2662                         this->ecclayout->oobfree[i].length;
2663         mtd->oobavail = this->ecclayout->oobavail;
2664
2665         mtd->ecclayout = this->ecclayout;
2666
2667         /* Fill in remaining MTD driver data */
2668         mtd->type = MTD_NANDFLASH;
2669         mtd->flags = MTD_CAP_NANDFLASH;
2670         mtd->erase = onenand_erase;
2671         mtd->point = NULL;
2672         mtd->unpoint = NULL;
2673         mtd->read = onenand_read;
2674         mtd->write = onenand_write;
2675         mtd->read_oob = onenand_read_oob;
2676         mtd->write_oob = onenand_write_oob;
2677 #ifdef CONFIG_MTD_ONENAND_OTP
2678         mtd->get_fact_prot_info = onenand_get_fact_prot_info;
2679         mtd->read_fact_prot_reg = onenand_read_fact_prot_reg;
2680         mtd->get_user_prot_info = onenand_get_user_prot_info;
2681         mtd->read_user_prot_reg = onenand_read_user_prot_reg;
2682         mtd->write_user_prot_reg = onenand_write_user_prot_reg;
2683         mtd->lock_user_prot_reg = onenand_lock_user_prot_reg;
2684 #endif
2685         mtd->sync = onenand_sync;
2686         mtd->lock = onenand_lock;
2687         mtd->unlock = onenand_unlock;
2688         mtd->suspend = onenand_suspend;
2689         mtd->resume = onenand_resume;
2690         mtd->block_isbad = onenand_block_isbad;
2691         mtd->block_markbad = onenand_block_markbad;
2692         mtd->owner = THIS_MODULE;
2693
2694         /* Unlock whole block */
2695         onenand_unlock_all(mtd);
2696
2697         return this->scan_bbt(mtd);
2698 }
2699
2700 /**
2701  * onenand_release - [OneNAND Interface] Free resources held by the OneNAND device
2702  * @param mtd           MTD device structure
2703  */
2704 void onenand_release(struct mtd_info *mtd)
2705 {
2706         struct onenand_chip *this = mtd->priv;
2707
2708 #ifdef CONFIG_MTD_PARTITIONS
2709         /* Deregister partitions */
2710         del_mtd_partitions (mtd);
2711 #endif
2712         /* Deregister the device */
2713         del_mtd_device (mtd);
2714
2715         /* Free bad block table memory, if allocated */
2716         if (this->bbm) {
2717                 struct bbm_info *bbm = this->bbm;
2718                 kfree(bbm->bbt);
2719                 kfree(this->bbm);
2720         }
2721         /* Buffers allocated by onenand_scan */
2722         if (this->options & ONENAND_PAGEBUF_ALLOC)
2723                 kfree(this->page_buf);
2724         if (this->options & ONENAND_OOBBUF_ALLOC)
2725                 kfree(this->oob_buf);
2726 }
2727
2728 EXPORT_SYMBOL_GPL(onenand_scan);
2729 EXPORT_SYMBOL_GPL(onenand_release);
2730
2731 MODULE_LICENSE("GPL");
2732 MODULE_AUTHOR("Kyungmin Park <kyungmin.park@samsung.com>");
2733 MODULE_DESCRIPTION("Generic OneNAND flash driver code");