2 * linux/drivers/mtd/onenand/onenand_base.c
4 * Copyright (C) 2005-2007 Samsung Electronics
5 * Kyungmin Park <kyungmin.park@samsung.com>
8 * Adrian Hunter <ext-adrian.hunter@nokia.com>:
9 * auto-placement support, read-while load support, various fixes
10 * Copyright (C) Nokia Corporation, 2007
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.
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>
30 * onenand_oob_64 - oob info for large (2KB) page
32 static struct nand_ecclayout onenand_oob_64 = {
41 {2, 3}, {14, 2}, {18, 3}, {30, 2},
42 {34, 3}, {46, 2}, {50, 3}, {62, 2}
47 * onenand_oob_32 - oob info for middle (1KB) page
49 static struct nand_ecclayout onenand_oob_32 = {
55 .oobfree = { {2, 3}, {14, 2}, {18, 3}, {30, 2} }
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 */
70 * onenand_readw - [OneNAND Interface] Read OneNAND register
71 * @param addr address to read
73 * Read OneNAND register
75 static unsigned short onenand_readw(void __iomem *addr)
81 * onenand_writew - [OneNAND Interface] Write OneNAND register with value
82 * @param value value to write
83 * @param addr address to write
85 * Write OneNAND register with value
87 static void onenand_writew(unsigned short value, void __iomem *addr)
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
98 * Setup Start Address 1 Register (F100h)
100 static int onenand_block_address(struct onenand_chip *this, int block)
102 /* Device Flash Core select, NAND Flash Block Address */
103 if (block & this->density_mask)
104 return ONENAND_DDP_CHIP1 | (block ^ this->density_mask);
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
115 * Setup Start Address 2 Register (F101h) for DDP
117 static int onenand_bufferram_address(struct onenand_chip *this, int block)
119 /* Device BufferRAM Select */
120 if (block & this->density_mask)
121 return ONENAND_DDP_CHIP1;
123 return ONENAND_DDP_CHIP0;
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
132 * Setup Start Address 8 Register (F107h)
134 static int onenand_page_address(int page, int sector)
136 /* Flash Page Address, Flash Sector Address */
139 fpa = page & ONENAND_FPA_MASK;
140 fsa = sector & ONENAND_FSA_MASK;
142 return ((fpa << ONENAND_FPA_SHIFT) | fsa);
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
152 * Setup Start Buffer Register (F200h)
154 static int onenand_buffer_address(int dataram1, int sectors, int count)
158 /* BufferRAM Sector Address */
159 bsa = sectors & ONENAND_BSA_MASK;
162 bsa |= ONENAND_BSA_DATARAM1; /* DataRAM1 */
164 bsa |= ONENAND_BSA_DATARAM0; /* DataRAM0 */
166 /* BufferRAM Sector Count */
167 bsc = count & ONENAND_BSC_MASK;
169 return ((bsa << ONENAND_BSA_SHIFT) | bsc);
173 * onenand_command - [DEFAULT] Send command to OneNAND device
174 * @param mtd MTD device structure
175 * @param cmd the command to be sent
176 * @param addr offset to read from or write to
177 * @param len number of bytes to read or write
179 * Send command to OneNAND device. This function is used for middle/large page
180 * devices (1KB/2KB Bytes per page)
182 static int onenand_command(struct mtd_info *mtd, int cmd, loff_t addr, size_t len)
184 struct onenand_chip *this = mtd->priv;
185 int value, readcmd = 0, block_cmd = 0;
188 /* Address translation */
190 case ONENAND_CMD_UNLOCK:
191 case ONENAND_CMD_LOCK:
192 case ONENAND_CMD_LOCK_TIGHT:
193 case ONENAND_CMD_UNLOCK_ALL:
198 case ONENAND_CMD_ERASE:
199 case ONENAND_CMD_BUFFERRAM:
200 case ONENAND_CMD_OTP_ACCESS:
202 block = (int) (addr >> this->erase_shift);
207 block = (int) (addr >> this->erase_shift);
208 page = (int) (addr >> this->page_shift);
210 if (ONENAND_IS_2PLANE(this)) {
211 /* Make the even block number */
213 /* Is it the odd plane? */
214 if (addr & this->writesize)
218 page &= this->page_mask;
222 /* NOTE: The setting order of the registers is very important! */
223 if (cmd == ONENAND_CMD_BUFFERRAM) {
224 /* Select DataRAM for DDP */
225 value = onenand_bufferram_address(this, block);
226 this->write_word(value, this->base + ONENAND_REG_START_ADDRESS2);
228 if (ONENAND_IS_2PLANE(this))
229 /* It is always BufferRAM0 */
230 ONENAND_SET_BUFFERRAM0(this);
232 /* Switch to the next data buffer */
233 ONENAND_SET_NEXT_BUFFERRAM(this);
239 /* Write 'DFS, FBA' of Flash */
240 value = onenand_block_address(this, block);
241 this->write_word(value, this->base + ONENAND_REG_START_ADDRESS1);
244 /* Select DataRAM for DDP */
245 value = onenand_bufferram_address(this, block);
246 this->write_word(value, this->base + ONENAND_REG_START_ADDRESS2);
251 /* Now we use page size operation */
252 int sectors = 4, count = 4;
256 case ONENAND_CMD_READ:
257 case ONENAND_CMD_READOOB:
258 dataram = ONENAND_SET_NEXT_BUFFERRAM(this);
263 if (ONENAND_IS_2PLANE(this) && cmd == ONENAND_CMD_PROG)
264 cmd = ONENAND_CMD_2X_PROG;
265 dataram = ONENAND_CURRENT_BUFFERRAM(this);
269 /* Write 'FPA, FSA' of Flash */
270 value = onenand_page_address(page, sectors);
271 this->write_word(value, this->base + ONENAND_REG_START_ADDRESS8);
273 /* Write 'BSA, BSC' of DataRAM */
274 value = onenand_buffer_address(dataram, sectors, count);
275 this->write_word(value, this->base + ONENAND_REG_START_BUFFER);
278 /* Select DataRAM for DDP */
279 value = onenand_bufferram_address(this, block);
280 this->write_word(value, this->base + ONENAND_REG_START_ADDRESS2);
284 /* Interrupt clear */
285 this->write_word(ONENAND_INT_CLEAR, this->base + ONENAND_REG_INTERRUPT);
288 this->write_word(cmd, this->base + ONENAND_REG_COMMAND);
294 * onenand_wait - [DEFAULT] wait until the command is done
295 * @param mtd MTD device structure
296 * @param state state to select the max. timeout value
298 * Wait for command done. This applies to all OneNAND command
299 * Read can take up to 30us, erase up to 2ms and program up to 350us
300 * according to general OneNAND specs
302 static int onenand_wait(struct mtd_info *mtd, int state)
304 struct onenand_chip * this = mtd->priv;
305 unsigned long timeout;
306 unsigned int flags = ONENAND_INT_MASTER;
307 unsigned int interrupt = 0;
310 /* The 20 msec is enough */
311 timeout = jiffies + msecs_to_jiffies(20);
312 while (time_before(jiffies, timeout)) {
313 interrupt = this->read_word(this->base + ONENAND_REG_INTERRUPT);
315 if (interrupt & flags)
318 if (state != FL_READING)
321 /* To get correct interrupt status in timeout case */
322 interrupt = this->read_word(this->base + ONENAND_REG_INTERRUPT);
324 ctrl = this->read_word(this->base + ONENAND_REG_CTRL_STATUS);
326 if (ctrl & ONENAND_CTRL_ERROR) {
327 printk(KERN_ERR "onenand_wait: controller error = 0x%04x\n", ctrl);
328 if (ctrl & ONENAND_CTRL_LOCK)
329 printk(KERN_ERR "onenand_wait: it's locked error.\n");
333 if (interrupt & ONENAND_INT_READ) {
334 int ecc = this->read_word(this->base + ONENAND_REG_ECC_STATUS);
336 printk(KERN_ERR "onenand_wait: ECC error = 0x%04x\n", ecc);
337 if (ecc & ONENAND_ECC_2BIT_ALL) {
338 mtd->ecc_stats.failed++;
340 } else if (ecc & ONENAND_ECC_1BIT_ALL)
341 mtd->ecc_stats.corrected++;
343 } else if (state == FL_READING) {
344 printk(KERN_ERR "onenand_wait: read timeout! ctrl=0x%04x intr=0x%04x\n", ctrl, interrupt);
352 * onenand_interrupt - [DEFAULT] onenand interrupt handler
353 * @param irq onenand interrupt number
354 * @param dev_id interrupt data
358 static irqreturn_t onenand_interrupt(int irq, void *data)
360 struct onenand_chip *this = (struct onenand_chip *) data;
362 /* To handle shared interrupt */
363 if (!this->complete.done)
364 complete(&this->complete);
370 * onenand_interrupt_wait - [DEFAULT] wait until the command is done
371 * @param mtd MTD device structure
372 * @param state state to select the max. timeout value
374 * Wait for command done.
376 static int onenand_interrupt_wait(struct mtd_info *mtd, int state)
378 struct onenand_chip *this = mtd->priv;
380 wait_for_completion(&this->complete);
382 return onenand_wait(mtd, state);
386 * onenand_try_interrupt_wait - [DEFAULT] try interrupt wait
387 * @param mtd MTD device structure
388 * @param state state to select the max. timeout value
390 * Try interrupt based wait (It is used one-time)
392 static int onenand_try_interrupt_wait(struct mtd_info *mtd, int state)
394 struct onenand_chip *this = mtd->priv;
395 unsigned long remain, timeout;
397 /* We use interrupt wait first */
398 this->wait = onenand_interrupt_wait;
400 timeout = msecs_to_jiffies(100);
401 remain = wait_for_completion_timeout(&this->complete, timeout);
403 printk(KERN_INFO "OneNAND: There's no interrupt. "
404 "We use the normal wait\n");
406 /* Release the irq */
407 free_irq(this->irq, this);
409 this->wait = onenand_wait;
412 return onenand_wait(mtd, state);
416 * onenand_setup_wait - [OneNAND Interface] setup onenand wait method
417 * @param mtd MTD device structure
419 * There's two method to wait onenand work
420 * 1. polling - read interrupt status register
421 * 2. interrupt - use the kernel interrupt method
423 static void onenand_setup_wait(struct mtd_info *mtd)
425 struct onenand_chip *this = mtd->priv;
428 init_completion(&this->complete);
430 if (this->irq <= 0) {
431 this->wait = onenand_wait;
435 if (request_irq(this->irq, &onenand_interrupt,
436 IRQF_SHARED, "onenand", this)) {
437 /* If we can't get irq, use the normal wait */
438 this->wait = onenand_wait;
442 /* Enable interrupt */
443 syscfg = this->read_word(this->base + ONENAND_REG_SYS_CFG1);
444 syscfg |= ONENAND_SYS_CFG1_IOBE;
445 this->write_word(syscfg, this->base + ONENAND_REG_SYS_CFG1);
447 this->wait = onenand_try_interrupt_wait;
451 * onenand_bufferram_offset - [DEFAULT] BufferRAM offset
452 * @param mtd MTD data structure
453 * @param area BufferRAM area
454 * @return offset given area
456 * Return BufferRAM offset given area
458 static inline int onenand_bufferram_offset(struct mtd_info *mtd, int area)
460 struct onenand_chip *this = mtd->priv;
462 if (ONENAND_CURRENT_BUFFERRAM(this)) {
463 /* Note: the 'this->writesize' is a real page size */
464 if (area == ONENAND_DATARAM)
465 return this->writesize;
466 if (area == ONENAND_SPARERAM)
474 * onenand_read_bufferram - [OneNAND Interface] Read the bufferram area
475 * @param mtd MTD data structure
476 * @param area BufferRAM area
477 * @param buffer the databuffer to put/get data
478 * @param offset offset to read from or write to
479 * @param count number of bytes to read/write
481 * Read the BufferRAM area
483 static int onenand_read_bufferram(struct mtd_info *mtd, int area,
484 unsigned char *buffer, int offset, size_t count)
486 struct onenand_chip *this = mtd->priv;
487 void __iomem *bufferram;
489 bufferram = this->base + area;
491 bufferram += onenand_bufferram_offset(mtd, area);
493 if (ONENAND_CHECK_BYTE_ACCESS(count)) {
496 /* Align with word(16-bit) size */
499 /* Read word and save byte */
500 word = this->read_word(bufferram + offset + count);
501 buffer[count] = (word & 0xff);
504 memcpy(buffer, bufferram + offset, count);
510 * onenand_sync_read_bufferram - [OneNAND Interface] Read the bufferram area with Sync. Burst mode
511 * @param mtd MTD data structure
512 * @param area BufferRAM area
513 * @param buffer the databuffer to put/get data
514 * @param offset offset to read from or write to
515 * @param count number of bytes to read/write
517 * Read the BufferRAM area with Sync. Burst Mode
519 static int onenand_sync_read_bufferram(struct mtd_info *mtd, int area,
520 unsigned char *buffer, int offset, size_t count)
522 struct onenand_chip *this = mtd->priv;
523 void __iomem *bufferram;
525 bufferram = this->base + area;
527 bufferram += onenand_bufferram_offset(mtd, area);
529 this->mmcontrol(mtd, ONENAND_SYS_CFG1_SYNC_READ);
531 if (ONENAND_CHECK_BYTE_ACCESS(count)) {
534 /* Align with word(16-bit) size */
537 /* Read word and save byte */
538 word = this->read_word(bufferram + offset + count);
539 buffer[count] = (word & 0xff);
542 memcpy(buffer, bufferram + offset, count);
544 this->mmcontrol(mtd, 0);
550 * onenand_write_bufferram - [OneNAND Interface] Write the bufferram area
551 * @param mtd MTD data structure
552 * @param area BufferRAM area
553 * @param buffer the databuffer to put/get data
554 * @param offset offset to read from or write to
555 * @param count number of bytes to read/write
557 * Write the BufferRAM area
559 static int onenand_write_bufferram(struct mtd_info *mtd, int area,
560 const unsigned char *buffer, int offset, size_t count)
562 struct onenand_chip *this = mtd->priv;
563 void __iomem *bufferram;
565 bufferram = this->base + area;
567 bufferram += onenand_bufferram_offset(mtd, area);
569 if (ONENAND_CHECK_BYTE_ACCESS(count)) {
573 /* Align with word(16-bit) size */
576 /* Calculate byte access offset */
577 byte_offset = offset + count;
579 /* Read word and save byte */
580 word = this->read_word(bufferram + byte_offset);
581 word = (word & ~0xff) | buffer[count];
582 this->write_word(word, bufferram + byte_offset);
585 memcpy(bufferram + offset, buffer, count);
591 * onenand_get_2x_blockpage - [GENERIC] Get blockpage at 2x program mode
592 * @param mtd MTD data structure
593 * @param addr address to check
594 * @return blockpage address
596 * Get blockpage address at 2x program mode
598 static int onenand_get_2x_blockpage(struct mtd_info *mtd, loff_t addr)
600 struct onenand_chip *this = mtd->priv;
601 int blockpage, block, page;
603 /* Calculate the even block number */
604 block = (int) (addr >> this->erase_shift) & ~1;
605 /* Is it the odd plane? */
606 if (addr & this->writesize)
608 page = (int) (addr >> (this->page_shift + 1)) & this->page_mask;
609 blockpage = (block << 7) | page;
615 * onenand_check_bufferram - [GENERIC] Check BufferRAM information
616 * @param mtd MTD data structure
617 * @param addr address to check
618 * @return 1 if there are valid data, otherwise 0
620 * Check bufferram if there is data we required
622 static int onenand_check_bufferram(struct mtd_info *mtd, loff_t addr)
624 struct onenand_chip *this = mtd->priv;
625 int blockpage, found = 0;
628 if (ONENAND_IS_2PLANE(this))
629 blockpage = onenand_get_2x_blockpage(mtd, addr);
631 blockpage = (int) (addr >> this->page_shift);
633 /* Is there valid data? */
634 i = ONENAND_CURRENT_BUFFERRAM(this);
635 if (this->bufferram[i].blockpage == blockpage)
638 /* Check another BufferRAM */
639 i = ONENAND_NEXT_BUFFERRAM(this);
640 if (this->bufferram[i].blockpage == blockpage) {
641 ONENAND_SET_NEXT_BUFFERRAM(this);
646 if (found && ONENAND_IS_DDP(this)) {
647 /* Select DataRAM for DDP */
648 int block = (int) (addr >> this->erase_shift);
649 int value = onenand_bufferram_address(this, block);
650 this->write_word(value, this->base + ONENAND_REG_START_ADDRESS2);
657 * onenand_update_bufferram - [GENERIC] Update BufferRAM information
658 * @param mtd MTD data structure
659 * @param addr address to update
660 * @param valid valid flag
662 * Update BufferRAM information
664 static void onenand_update_bufferram(struct mtd_info *mtd, loff_t addr,
667 struct onenand_chip *this = mtd->priv;
671 if (ONENAND_IS_2PLANE(this))
672 blockpage = onenand_get_2x_blockpage(mtd, addr);
674 blockpage = (int) (addr >> this->page_shift);
676 /* Invalidate another BufferRAM */
677 i = ONENAND_NEXT_BUFFERRAM(this);
678 if (this->bufferram[i].blockpage == blockpage)
679 this->bufferram[i].blockpage = -1;
681 /* Update BufferRAM */
682 i = ONENAND_CURRENT_BUFFERRAM(this);
684 this->bufferram[i].blockpage = blockpage;
686 this->bufferram[i].blockpage = -1;
690 * onenand_invalidate_bufferram - [GENERIC] Invalidate BufferRAM information
691 * @param mtd MTD data structure
692 * @param addr start address to invalidate
693 * @param len length to invalidate
695 * Invalidate BufferRAM information
697 static void onenand_invalidate_bufferram(struct mtd_info *mtd, loff_t addr,
700 struct onenand_chip *this = mtd->priv;
702 loff_t end_addr = addr + len;
704 /* Invalidate BufferRAM */
705 for (i = 0; i < MAX_BUFFERRAM; i++) {
706 loff_t buf_addr = this->bufferram[i].blockpage << this->page_shift;
707 if (buf_addr >= addr && buf_addr < end_addr)
708 this->bufferram[i].blockpage = -1;
713 * onenand_get_device - [GENERIC] Get chip for selected access
714 * @param mtd MTD device structure
715 * @param new_state the state which is requested
717 * Get the device and lock it for exclusive access
719 static int onenand_get_device(struct mtd_info *mtd, int new_state)
721 struct onenand_chip *this = mtd->priv;
722 DECLARE_WAITQUEUE(wait, current);
725 * Grab the lock and see if the device is available
728 spin_lock(&this->chip_lock);
729 if (this->state == FL_READY) {
730 this->state = new_state;
731 spin_unlock(&this->chip_lock);
734 if (new_state == FL_PM_SUSPENDED) {
735 spin_unlock(&this->chip_lock);
736 return (this->state == FL_PM_SUSPENDED) ? 0 : -EAGAIN;
738 set_current_state(TASK_UNINTERRUPTIBLE);
739 add_wait_queue(&this->wq, &wait);
740 spin_unlock(&this->chip_lock);
742 remove_wait_queue(&this->wq, &wait);
749 * onenand_release_device - [GENERIC] release chip
750 * @param mtd MTD device structure
752 * Deselect, release chip lock and wake up anyone waiting on the device
754 static void onenand_release_device(struct mtd_info *mtd)
756 struct onenand_chip *this = mtd->priv;
758 /* Release the chip */
759 spin_lock(&this->chip_lock);
760 this->state = FL_READY;
762 spin_unlock(&this->chip_lock);
766 * onenand_read - [MTD Interface] Read data from flash
767 * @param mtd MTD device structure
768 * @param from offset to read from
769 * @param len number of bytes to read
770 * @param retlen pointer to variable to store the number of read bytes
771 * @param buf the databuffer to put data
775 static int onenand_read(struct mtd_info *mtd, loff_t from, size_t len,
776 size_t *retlen, u_char *buf)
778 struct onenand_chip *this = mtd->priv;
779 struct mtd_ecc_stats stats;
780 int read = 0, column;
782 int ret = 0, boundary = 0;
783 int writesize = this->writesize;
785 DEBUG(MTD_DEBUG_LEVEL3, "onenand_read: from = 0x%08x, len = %i\n", (unsigned int) from, (int) len);
787 /* Do not allow reads past end of device */
788 if ((from + len) > mtd->size) {
789 printk(KERN_ERR "onenand_read: Attempt read beyond end of device\n");
794 /* Grab the lock and see if the device is available */
795 onenand_get_device(mtd, FL_READING);
797 stats = mtd->ecc_stats;
799 /* Read-while-load method */
801 /* Do first load to bufferRAM */
803 if (!onenand_check_bufferram(mtd, from)) {
804 this->command(mtd, ONENAND_CMD_READ, from, writesize);
805 ret = this->wait(mtd, FL_READING);
806 onenand_update_bufferram(mtd, from, !ret);
810 thislen = min_t(int, writesize, len - read);
811 column = from & (writesize - 1);
812 if (column + thislen > writesize)
813 thislen = writesize - column;
816 /* If there is more to load then start next load */
818 if (read + thislen < len) {
819 this->command(mtd, ONENAND_CMD_READ, from, writesize);
821 * Chip boundary handling in DDP
822 * Now we issued chip 1 read and pointed chip 1
823 * bufferam so we have to point chip 0 bufferam.
825 if (ONENAND_IS_DDP(this) &&
826 unlikely(from == (this->chipsize >> 1))) {
827 this->write_word(ONENAND_DDP_CHIP0, this->base + ONENAND_REG_START_ADDRESS2);
831 ONENAND_SET_PREV_BUFFERRAM(this);
833 /* While load is going, read from last bufferRAM */
834 this->read_bufferram(mtd, ONENAND_DATARAM, buf, column, thislen);
835 /* See if we are done */
839 /* Set up for next read from bufferRAM */
840 if (unlikely(boundary))
841 this->write_word(ONENAND_DDP_CHIP1, this->base + ONENAND_REG_START_ADDRESS2);
842 ONENAND_SET_NEXT_BUFFERRAM(this);
844 thislen = min_t(int, writesize, len - read);
847 /* Now wait for load */
848 ret = this->wait(mtd, FL_READING);
849 onenand_update_bufferram(mtd, from, !ret);
852 /* Deselect and wake up anyone waiting on the device */
853 onenand_release_device(mtd);
856 * Return success, if no ECC failures, else -EBADMSG
857 * fs driver will take care of that, because
858 * retlen == desired len and result == -EBADMSG
862 if (mtd->ecc_stats.failed - stats.failed)
868 return mtd->ecc_stats.corrected - stats.corrected ? -EUCLEAN : 0;
872 * onenand_transfer_auto_oob - [Internal] oob auto-placement transfer
873 * @param mtd MTD device structure
874 * @param buf destination address
875 * @param column oob offset to read from
876 * @param thislen oob length to read
878 static int onenand_transfer_auto_oob(struct mtd_info *mtd, uint8_t *buf, int column,
881 struct onenand_chip *this = mtd->priv;
882 struct nand_oobfree *free;
883 int readcol = column;
884 int readend = column + thislen;
887 uint8_t *oob_buf = this->oob_buf;
889 free = this->ecclayout->oobfree;
890 for (i = 0; i < MTD_MAX_OOBFREE_ENTRIES && free->length; i++, free++) {
891 if (readcol >= lastgap)
892 readcol += free->offset - lastgap;
893 if (readend >= lastgap)
894 readend += free->offset - lastgap;
895 lastgap = free->offset + free->length;
897 this->read_bufferram(mtd, ONENAND_SPARERAM, oob_buf, 0, mtd->oobsize);
898 free = this->ecclayout->oobfree;
899 for (i = 0; i < MTD_MAX_OOBFREE_ENTRIES && free->length; i++, free++) {
900 int free_end = free->offset + free->length;
901 if (free->offset < readend && free_end > readcol) {
902 int st = max_t(int,free->offset,readcol);
903 int ed = min_t(int,free_end,readend);
905 memcpy(buf, oob_buf + st, n);
907 } else if (column == 0)
914 * onenand_do_read_oob - [MTD Interface] OneNAND read out-of-band
915 * @param mtd MTD device structure
916 * @param from offset to read from
917 * @param len number of bytes to read
918 * @param retlen pointer to variable to store the number of read bytes
919 * @param buf the databuffer to put data
920 * @param mode operation mode
922 * OneNAND read out-of-band data from the spare area
924 static int onenand_do_read_oob(struct mtd_info *mtd, loff_t from,
925 struct mtd_oob_ops *ops)
927 struct onenand_chip *this = mtd->priv;
928 int read = 0, thislen, column, oobsize;
929 size_t len = ops->ooblen;
930 mtd_oob_mode_t mode = ops->mode;
931 u_char *buf = ops->oobbuf;
934 from += ops->ooboffs;
936 DEBUG(MTD_DEBUG_LEVEL3, "onenand_read_oob: from = 0x%08x, len = %i\n", (unsigned int) from, (int) len);
938 /* Initialize return length value */
941 if (mode == MTD_OOB_AUTO)
942 oobsize = this->ecclayout->oobavail;
944 oobsize = mtd->oobsize;
946 column = from & (mtd->oobsize - 1);
948 if (unlikely(column >= oobsize)) {
949 printk(KERN_ERR "onenand_read_oob: Attempted to start read outside oob\n");
953 /* Do not allow reads past end of device */
954 if (unlikely(from >= mtd->size ||
955 column + len > ((mtd->size >> this->page_shift) -
956 (from >> this->page_shift)) * oobsize)) {
957 printk(KERN_ERR "onenand_read_oob: Attempted to read beyond end of device\n");
961 /* Grab the lock and see if the device is available */
962 onenand_get_device(mtd, FL_READING);
967 thislen = oobsize - column;
968 thislen = min_t(int, thislen, len);
970 this->command(mtd, ONENAND_CMD_READOOB, from, mtd->oobsize);
972 onenand_update_bufferram(mtd, from, 0);
974 ret = this->wait(mtd, FL_READING);
975 /* First copy data and check return value for ECC handling */
977 if (mode == MTD_OOB_AUTO)
978 onenand_transfer_auto_oob(mtd, buf, column, thislen);
980 this->read_bufferram(mtd, ONENAND_SPARERAM, buf, column, thislen);
983 printk(KERN_ERR "onenand_read_oob: read failed = 0x%x\n", ret);
997 from += mtd->writesize;
1002 /* Deselect and wake up anyone waiting on the device */
1003 onenand_release_device(mtd);
1005 ops->oobretlen = read;
1010 * onenand_read_oob - [MTD Interface] NAND write data and/or out-of-band
1011 * @param mtd: MTD device structure
1012 * @param from: offset to read from
1013 * @param ops: oob operation description structure
1015 static int onenand_read_oob(struct mtd_info *mtd, loff_t from,
1016 struct mtd_oob_ops *ops)
1018 switch (ops->mode) {
1023 /* Not implemented yet */
1027 return onenand_do_read_oob(mtd, from, ops);
1031 * onenand_bbt_wait - [DEFAULT] wait until the command is done
1032 * @param mtd MTD device structure
1033 * @param state state to select the max. timeout value
1035 * Wait for command done.
1037 static int onenand_bbt_wait(struct mtd_info *mtd, int state)
1039 struct onenand_chip *this = mtd->priv;
1040 unsigned long timeout;
1041 unsigned int interrupt;
1044 /* The 20 msec is enough */
1045 timeout = jiffies + msecs_to_jiffies(20);
1046 while (time_before(jiffies, timeout)) {
1047 interrupt = this->read_word(this->base + ONENAND_REG_INTERRUPT);
1048 if (interrupt & ONENAND_INT_MASTER)
1051 /* To get correct interrupt status in timeout case */
1052 interrupt = this->read_word(this->base + ONENAND_REG_INTERRUPT);
1053 ctrl = this->read_word(this->base + ONENAND_REG_CTRL_STATUS);
1055 if (ctrl & ONENAND_CTRL_ERROR) {
1056 printk(KERN_DEBUG "onenand_bbt_wait: controller error = 0x%04x\n", ctrl);
1057 /* Initial bad block case */
1058 if (ctrl & ONENAND_CTRL_LOAD)
1059 return ONENAND_BBT_READ_ERROR;
1060 return ONENAND_BBT_READ_FATAL_ERROR;
1063 if (interrupt & ONENAND_INT_READ) {
1064 int ecc = this->read_word(this->base + ONENAND_REG_ECC_STATUS);
1065 if (ecc & ONENAND_ECC_2BIT_ALL)
1066 return ONENAND_BBT_READ_ERROR;
1068 printk(KERN_ERR "onenand_bbt_wait: read timeout!"
1069 "ctrl=0x%04x intr=0x%04x\n", ctrl, interrupt);
1070 return ONENAND_BBT_READ_FATAL_ERROR;
1077 * onenand_bbt_read_oob - [MTD Interface] OneNAND read out-of-band for bbt scan
1078 * @param mtd MTD device structure
1079 * @param from offset to read from
1080 * @param ops oob operation description structure
1082 * OneNAND read out-of-band data from the spare area for bbt scan
1084 int onenand_bbt_read_oob(struct mtd_info *mtd, loff_t from,
1085 struct mtd_oob_ops *ops)
1087 struct onenand_chip *this = mtd->priv;
1088 int read = 0, thislen, column;
1090 size_t len = ops->ooblen;
1091 u_char *buf = ops->oobbuf;
1093 DEBUG(MTD_DEBUG_LEVEL3, "onenand_bbt_read_oob: from = 0x%08x, len = %zi\n", (unsigned int) from, len);
1095 /* Initialize return value */
1098 /* Do not allow reads past end of device */
1099 if (unlikely((from + len) > mtd->size)) {
1100 printk(KERN_ERR "onenand_bbt_read_oob: Attempt read beyond end of device\n");
1101 return ONENAND_BBT_READ_FATAL_ERROR;
1104 /* Grab the lock and see if the device is available */
1105 onenand_get_device(mtd, FL_READING);
1107 column = from & (mtd->oobsize - 1);
1109 while (read < len) {
1112 thislen = mtd->oobsize - column;
1113 thislen = min_t(int, thislen, len);
1115 this->command(mtd, ONENAND_CMD_READOOB, from, mtd->oobsize);
1117 onenand_update_bufferram(mtd, from, 0);
1119 ret = onenand_bbt_wait(mtd, FL_READING);
1123 this->read_bufferram(mtd, ONENAND_SPARERAM, buf, column, thislen);
1132 /* Update Page size */
1133 from += this->writesize;
1138 /* Deselect and wake up anyone waiting on the device */
1139 onenand_release_device(mtd);
1141 ops->oobretlen = read;
1145 #ifdef CONFIG_MTD_ONENAND_VERIFY_WRITE
1147 * onenand_verify_oob - [GENERIC] verify the oob contents after a write
1148 * @param mtd MTD device structure
1149 * @param buf the databuffer to verify
1150 * @param to offset to read from
1153 static int onenand_verify_oob(struct mtd_info *mtd, const u_char *buf, loff_t to)
1155 struct onenand_chip *this = mtd->priv;
1159 this->command(mtd, ONENAND_CMD_READOOB, to, mtd->oobsize);
1160 onenand_update_bufferram(mtd, to, 0);
1161 status = this->wait(mtd, FL_READING);
1165 this->read_bufferram(mtd, ONENAND_SPARERAM, oobbuf, 0, mtd->oobsize);
1166 for (i = 0; i < mtd->oobsize; i++)
1167 if (buf[i] != 0xFF && buf[i] != oobbuf[i])
1174 * onenand_verify - [GENERIC] verify the chip contents after a write
1175 * @param mtd MTD device structure
1176 * @param buf the databuffer to verify
1177 * @param addr offset to read from
1178 * @param len number of bytes to read and compare
1181 static int onenand_verify(struct mtd_info *mtd, const u_char *buf, loff_t addr, size_t len)
1183 struct onenand_chip *this = mtd->priv;
1184 void __iomem *dataram;
1186 int thislen, column;
1189 thislen = min_t(int, this->writesize, len);
1190 column = addr & (this->writesize - 1);
1191 if (column + thislen > this->writesize)
1192 thislen = this->writesize - column;
1194 this->command(mtd, ONENAND_CMD_READ, addr, this->writesize);
1196 onenand_update_bufferram(mtd, addr, 0);
1198 ret = this->wait(mtd, FL_READING);
1202 onenand_update_bufferram(mtd, addr, 1);
1204 dataram = this->base + ONENAND_DATARAM;
1205 dataram += onenand_bufferram_offset(mtd, ONENAND_DATARAM);
1207 if (memcmp(buf, dataram + column, thislen))
1218 #define onenand_verify(...) (0)
1219 #define onenand_verify_oob(...) (0)
1222 #define NOTALIGNED(x) ((x & (this->subpagesize - 1)) != 0)
1225 * onenand_write - [MTD Interface] write buffer to FLASH
1226 * @param mtd MTD device structure
1227 * @param to offset to write to
1228 * @param len number of bytes to write
1229 * @param retlen pointer to variable to store the number of written bytes
1230 * @param buf the data to write
1234 static int onenand_write(struct mtd_info *mtd, loff_t to, size_t len,
1235 size_t *retlen, const u_char *buf)
1237 struct onenand_chip *this = mtd->priv;
1240 int column, subpage;
1242 DEBUG(MTD_DEBUG_LEVEL3, "onenand_write: to = 0x%08x, len = %i\n", (unsigned int) to, (int) len);
1244 /* Initialize retlen, in case of early exit */
1247 /* Do not allow writes past end of device */
1248 if (unlikely((to + len) > mtd->size)) {
1249 printk(KERN_ERR "onenand_write: Attempt write to past end of device\n");
1253 /* Reject writes, which are not page aligned */
1254 if (unlikely(NOTALIGNED(to)) || unlikely(NOTALIGNED(len))) {
1255 printk(KERN_ERR "onenand_write: Attempt to write not page aligned data\n");
1259 column = to & (mtd->writesize - 1);
1261 /* Grab the lock and see if the device is available */
1262 onenand_get_device(mtd, FL_WRITING);
1264 /* Loop until all data write */
1265 while (written < len) {
1266 int thislen = min_t(int, mtd->writesize - column, len - written);
1267 u_char *wbuf = (u_char *) buf;
1271 this->command(mtd, ONENAND_CMD_BUFFERRAM, to, thislen);
1273 /* Partial page write */
1274 subpage = thislen < mtd->writesize;
1276 memset(this->page_buf, 0xff, mtd->writesize);
1277 memcpy(this->page_buf + column, buf, thislen);
1278 wbuf = this->page_buf;
1281 this->write_bufferram(mtd, ONENAND_DATARAM, wbuf, 0, mtd->writesize);
1282 this->write_bufferram(mtd, ONENAND_SPARERAM, ffchars, 0, mtd->oobsize);
1284 this->command(mtd, ONENAND_CMD_PROG, to, mtd->writesize);
1286 ret = this->wait(mtd, FL_WRITING);
1288 /* In partial page write we don't update bufferram */
1289 onenand_update_bufferram(mtd, to, !ret && !subpage);
1290 if (ONENAND_IS_2PLANE(this)) {
1291 ONENAND_SET_BUFFERRAM1(this);
1292 onenand_update_bufferram(mtd, to + this->writesize, !ret && !subpage);
1296 printk(KERN_ERR "onenand_write: write filaed %d\n", ret);
1300 /* Only check verify write turn on */
1301 ret = onenand_verify(mtd, (u_char *) wbuf, to, thislen);
1303 printk(KERN_ERR "onenand_write: verify failed %d\n", ret);
1317 /* Deselect and wake up anyone waiting on the device */
1318 onenand_release_device(mtd);
1326 * onenand_fill_auto_oob - [Internal] oob auto-placement transfer
1327 * @param mtd MTD device structure
1328 * @param oob_buf oob buffer
1329 * @param buf source address
1330 * @param column oob offset to write to
1331 * @param thislen oob length to write
1333 static int onenand_fill_auto_oob(struct mtd_info *mtd, u_char *oob_buf,
1334 const u_char *buf, int column, int thislen)
1336 struct onenand_chip *this = mtd->priv;
1337 struct nand_oobfree *free;
1338 int writecol = column;
1339 int writeend = column + thislen;
1343 free = this->ecclayout->oobfree;
1344 for (i = 0; i < MTD_MAX_OOBFREE_ENTRIES && free->length; i++, free++) {
1345 if (writecol >= lastgap)
1346 writecol += free->offset - lastgap;
1347 if (writeend >= lastgap)
1348 writeend += free->offset - lastgap;
1349 lastgap = free->offset + free->length;
1351 free = this->ecclayout->oobfree;
1352 for (i = 0; i < MTD_MAX_OOBFREE_ENTRIES && free->length; i++, free++) {
1353 int free_end = free->offset + free->length;
1354 if (free->offset < writeend && free_end > writecol) {
1355 int st = max_t(int,free->offset,writecol);
1356 int ed = min_t(int,free_end,writeend);
1358 memcpy(oob_buf + st, buf, n);
1360 } else if (column == 0)
1367 * onenand_do_write_oob - [Internal] OneNAND write out-of-band
1368 * @param mtd MTD device structure
1369 * @param to offset to write to
1370 * @param len number of bytes to write
1371 * @param retlen pointer to variable to store the number of written bytes
1372 * @param buf the data to write
1373 * @param mode operation mode
1375 * OneNAND write out-of-band
1377 static int onenand_do_write_oob(struct mtd_info *mtd, loff_t to,
1378 struct mtd_oob_ops *ops)
1380 struct onenand_chip *this = mtd->priv;
1381 int column, ret = 0, oobsize;
1384 size_t len = ops->ooblen;
1385 const u_char *buf = ops->oobbuf;
1386 mtd_oob_mode_t mode = ops->mode;
1390 DEBUG(MTD_DEBUG_LEVEL3, "onenand_write_oob: to = 0x%08x, len = %i\n", (unsigned int) to, (int) len);
1392 /* Initialize retlen, in case of early exit */
1395 if (mode == MTD_OOB_AUTO)
1396 oobsize = this->ecclayout->oobavail;
1398 oobsize = mtd->oobsize;
1400 column = to & (mtd->oobsize - 1);
1402 if (unlikely(column >= oobsize)) {
1403 printk(KERN_ERR "onenand_write_oob: Attempted to start write outside oob\n");
1407 /* For compatibility with NAND: Do not allow write past end of page */
1408 if (unlikely(column + len > oobsize)) {
1409 printk(KERN_ERR "onenand_write_oob: "
1410 "Attempt to write past end of page\n");
1414 /* Do not allow reads past end of device */
1415 if (unlikely(to >= mtd->size ||
1416 column + len > ((mtd->size >> this->page_shift) -
1417 (to >> this->page_shift)) * oobsize)) {
1418 printk(KERN_ERR "onenand_write_oob: Attempted to write past end of device\n");
1422 /* Grab the lock and see if the device is available */
1423 onenand_get_device(mtd, FL_WRITING);
1425 oobbuf = this->oob_buf;
1427 /* Loop until all data write */
1428 while (written < len) {
1429 int thislen = min_t(int, oobsize, len - written);
1433 this->command(mtd, ONENAND_CMD_BUFFERRAM, to, mtd->oobsize);
1435 /* We send data to spare ram with oobsize
1436 * to prevent byte access */
1437 memset(oobbuf, 0xff, mtd->oobsize);
1438 if (mode == MTD_OOB_AUTO)
1439 onenand_fill_auto_oob(mtd, oobbuf, buf, column, thislen);
1441 memcpy(oobbuf + column, buf, thislen);
1442 this->write_bufferram(mtd, ONENAND_SPARERAM, oobbuf, 0, mtd->oobsize);
1444 this->command(mtd, ONENAND_CMD_PROGOOB, to, mtd->oobsize);
1446 onenand_update_bufferram(mtd, to, 0);
1447 if (ONENAND_IS_2PLANE(this)) {
1448 ONENAND_SET_BUFFERRAM1(this);
1449 onenand_update_bufferram(mtd, to + this->writesize, 0);
1452 ret = this->wait(mtd, FL_WRITING);
1454 printk(KERN_ERR "onenand_write_oob: write failed %d\n", ret);
1458 ret = onenand_verify_oob(mtd, oobbuf, to);
1460 printk(KERN_ERR "onenand_write_oob: verify failed %d\n", ret);
1468 to += mtd->writesize;
1473 /* Deselect and wake up anyone waiting on the device */
1474 onenand_release_device(mtd);
1476 ops->oobretlen = written;
1482 * onenand_write_oob - [MTD Interface] NAND write data and/or out-of-band
1483 * @param mtd: MTD device structure
1484 * @param to: offset to write
1485 * @param ops: oob operation description structure
1487 static int onenand_write_oob(struct mtd_info *mtd, loff_t to,
1488 struct mtd_oob_ops *ops)
1490 switch (ops->mode) {
1495 /* Not implemented yet */
1499 return onenand_do_write_oob(mtd, to, ops);
1503 * onenand_block_checkbad - [GENERIC] Check if a block is marked bad
1504 * @param mtd MTD device structure
1505 * @param ofs offset from device start
1506 * @param getchip 0, if the chip is already selected
1507 * @param allowbbt 1, if its allowed to access the bbt area
1509 * Check, if the block is bad. Either by reading the bad block table or
1510 * calling of the scan function.
1512 static int onenand_block_checkbad(struct mtd_info *mtd, loff_t ofs, int getchip, int allowbbt)
1514 struct onenand_chip *this = mtd->priv;
1515 struct bbm_info *bbm = this->bbm;
1517 /* Return info from the table */
1518 return bbm->isbad_bbt(mtd, ofs, allowbbt);
1522 * onenand_erase - [MTD Interface] erase block(s)
1523 * @param mtd MTD device structure
1524 * @param instr erase instruction
1526 * Erase one ore more blocks
1528 static int onenand_erase(struct mtd_info *mtd, struct erase_info *instr)
1530 struct onenand_chip *this = mtd->priv;
1531 unsigned int block_size;
1536 DEBUG(MTD_DEBUG_LEVEL3, "onenand_erase: start = 0x%08x, len = %i\n", (unsigned int) instr->addr, (unsigned int) instr->len);
1538 block_size = (1 << this->erase_shift);
1540 /* Start address must align on block boundary */
1541 if (unlikely(instr->addr & (block_size - 1))) {
1542 printk(KERN_ERR "onenand_erase: Unaligned address\n");
1546 /* Length must align on block boundary */
1547 if (unlikely(instr->len & (block_size - 1))) {
1548 printk(KERN_ERR "onenand_erase: Length not block aligned\n");
1552 /* Do not allow erase past end of device */
1553 if (unlikely((instr->len + instr->addr) > mtd->size)) {
1554 printk(KERN_ERR "onenand_erase: Erase past end of device\n");
1558 instr->fail_addr = 0xffffffff;
1560 /* Grab the lock and see if the device is available */
1561 onenand_get_device(mtd, FL_ERASING);
1563 /* Loop throught the pages */
1567 instr->state = MTD_ERASING;
1572 /* Check if we have a bad block, we do not erase bad blocks */
1573 if (onenand_block_checkbad(mtd, addr, 0, 0)) {
1574 printk (KERN_WARNING "onenand_erase: attempt to erase a bad block at addr 0x%08x\n", (unsigned int) addr);
1575 instr->state = MTD_ERASE_FAILED;
1579 this->command(mtd, ONENAND_CMD_ERASE, addr, block_size);
1581 onenand_invalidate_bufferram(mtd, addr, block_size);
1583 ret = this->wait(mtd, FL_ERASING);
1584 /* Check, if it is write protected */
1586 printk(KERN_ERR "onenand_erase: Failed erase, block %d\n", (unsigned) (addr >> this->erase_shift));
1587 instr->state = MTD_ERASE_FAILED;
1588 instr->fail_addr = addr;
1596 instr->state = MTD_ERASE_DONE;
1600 ret = instr->state == MTD_ERASE_DONE ? 0 : -EIO;
1601 /* Do call back function */
1603 mtd_erase_callback(instr);
1605 /* Deselect and wake up anyone waiting on the device */
1606 onenand_release_device(mtd);
1612 * onenand_sync - [MTD Interface] sync
1613 * @param mtd MTD device structure
1615 * Sync is actually a wait for chip ready function
1617 static void onenand_sync(struct mtd_info *mtd)
1619 DEBUG(MTD_DEBUG_LEVEL3, "onenand_sync: called\n");
1621 /* Grab the lock and see if the device is available */
1622 onenand_get_device(mtd, FL_SYNCING);
1624 /* Release it and go back */
1625 onenand_release_device(mtd);
1629 * onenand_block_isbad - [MTD Interface] Check whether the block at the given offset is bad
1630 * @param mtd MTD device structure
1631 * @param ofs offset relative to mtd start
1633 * Check whether the block is bad
1635 static int onenand_block_isbad(struct mtd_info *mtd, loff_t ofs)
1637 /* Check for invalid offset */
1638 if (ofs > mtd->size)
1641 return onenand_block_checkbad(mtd, ofs, 1, 0);
1645 * onenand_default_block_markbad - [DEFAULT] mark a block bad
1646 * @param mtd MTD device structure
1647 * @param ofs offset from device start
1649 * This is the default implementation, which can be overridden by
1650 * a hardware specific driver.
1652 static int onenand_default_block_markbad(struct mtd_info *mtd, loff_t ofs)
1654 struct onenand_chip *this = mtd->priv;
1655 struct bbm_info *bbm = this->bbm;
1656 u_char buf[2] = {0, 0};
1657 struct mtd_oob_ops ops = {
1658 .mode = MTD_OOB_PLACE,
1665 /* Get block number */
1666 block = ((int) ofs) >> bbm->bbt_erase_shift;
1668 bbm->bbt[block >> 2] |= 0x01 << ((block & 0x03) << 1);
1670 /* We write two bytes, so we dont have to mess with 16 bit access */
1671 ofs += mtd->oobsize + (bbm->badblockpos & ~0x01);
1672 return onenand_do_write_oob(mtd, ofs, &ops);
1676 * onenand_block_markbad - [MTD Interface] Mark the block at the given offset as bad
1677 * @param mtd MTD device structure
1678 * @param ofs offset relative to mtd start
1680 * Mark the block as bad
1682 static int onenand_block_markbad(struct mtd_info *mtd, loff_t ofs)
1684 struct onenand_chip *this = mtd->priv;
1687 ret = onenand_block_isbad(mtd, ofs);
1689 /* If it was bad already, return success and do nothing */
1695 return this->block_markbad(mtd, ofs);
1699 * onenand_do_lock_cmd - [OneNAND Interface] Lock or unlock block(s)
1700 * @param mtd MTD device structure
1701 * @param ofs offset relative to mtd start
1702 * @param len number of bytes to lock or unlock
1703 * @param cmd lock or unlock command
1705 * Lock or unlock one or more blocks
1707 static int onenand_do_lock_cmd(struct mtd_info *mtd, loff_t ofs, size_t len, int cmd)
1709 struct onenand_chip *this = mtd->priv;
1710 int start, end, block, value, status;
1713 start = ofs >> this->erase_shift;
1714 end = len >> this->erase_shift;
1716 if (cmd == ONENAND_CMD_LOCK)
1717 wp_status_mask = ONENAND_WP_LS;
1719 wp_status_mask = ONENAND_WP_US;
1721 /* Continuous lock scheme */
1722 if (this->options & ONENAND_HAS_CONT_LOCK) {
1723 /* Set start block address */
1724 this->write_word(start, this->base + ONENAND_REG_START_BLOCK_ADDRESS);
1725 /* Set end block address */
1726 this->write_word(start + end - 1, this->base + ONENAND_REG_END_BLOCK_ADDRESS);
1727 /* Write lock command */
1728 this->command(mtd, cmd, 0, 0);
1730 /* There's no return value */
1731 this->wait(mtd, FL_LOCKING);
1734 while (this->read_word(this->base + ONENAND_REG_CTRL_STATUS)
1735 & ONENAND_CTRL_ONGO)
1738 /* Check lock status */
1739 status = this->read_word(this->base + ONENAND_REG_WP_STATUS);
1740 if (!(status & wp_status_mask))
1741 printk(KERN_ERR "wp status = 0x%x\n", status);
1746 /* Block lock scheme */
1747 for (block = start; block < start + end; block++) {
1748 /* Set block address */
1749 value = onenand_block_address(this, block);
1750 this->write_word(value, this->base + ONENAND_REG_START_ADDRESS1);
1751 /* Select DataRAM for DDP */
1752 value = onenand_bufferram_address(this, block);
1753 this->write_word(value, this->base + ONENAND_REG_START_ADDRESS2);
1754 /* Set start block address */
1755 this->write_word(block, this->base + ONENAND_REG_START_BLOCK_ADDRESS);
1756 /* Write lock command */
1757 this->command(mtd, cmd, 0, 0);
1759 /* There's no return value */
1760 this->wait(mtd, FL_LOCKING);
1763 while (this->read_word(this->base + ONENAND_REG_CTRL_STATUS)
1764 & ONENAND_CTRL_ONGO)
1767 /* Check lock status */
1768 status = this->read_word(this->base + ONENAND_REG_WP_STATUS);
1769 if (!(status & wp_status_mask))
1770 printk(KERN_ERR "block = %d, wp status = 0x%x\n", block, status);
1777 * onenand_lock - [MTD Interface] Lock block(s)
1778 * @param mtd MTD device structure
1779 * @param ofs offset relative to mtd start
1780 * @param len number of bytes to unlock
1782 * Lock one or more blocks
1784 static int onenand_lock(struct mtd_info *mtd, loff_t ofs, size_t len)
1786 return onenand_do_lock_cmd(mtd, ofs, len, ONENAND_CMD_LOCK);
1790 * onenand_unlock - [MTD Interface] Unlock block(s)
1791 * @param mtd MTD device structure
1792 * @param ofs offset relative to mtd start
1793 * @param len number of bytes to unlock
1795 * Unlock one or more blocks
1797 static int onenand_unlock(struct mtd_info *mtd, loff_t ofs, size_t len)
1799 return onenand_do_lock_cmd(mtd, ofs, len, ONENAND_CMD_UNLOCK);
1803 * onenand_check_lock_status - [OneNAND Interface] Check lock status
1804 * @param this onenand chip data structure
1808 static void onenand_check_lock_status(struct onenand_chip *this)
1810 unsigned int value, block, status;
1813 end = this->chipsize >> this->erase_shift;
1814 for (block = 0; block < end; block++) {
1815 /* Set block address */
1816 value = onenand_block_address(this, block);
1817 this->write_word(value, this->base + ONENAND_REG_START_ADDRESS1);
1818 /* Select DataRAM for DDP */
1819 value = onenand_bufferram_address(this, block);
1820 this->write_word(value, this->base + ONENAND_REG_START_ADDRESS2);
1821 /* Set start block address */
1822 this->write_word(block, this->base + ONENAND_REG_START_BLOCK_ADDRESS);
1824 /* Check lock status */
1825 status = this->read_word(this->base + ONENAND_REG_WP_STATUS);
1826 if (!(status & ONENAND_WP_US))
1827 printk(KERN_ERR "block = %d, wp status = 0x%x\n", block, status);
1832 * onenand_unlock_all - [OneNAND Interface] unlock all blocks
1833 * @param mtd MTD device structure
1837 static int onenand_unlock_all(struct mtd_info *mtd)
1839 struct onenand_chip *this = mtd->priv;
1841 if (this->options & ONENAND_HAS_UNLOCK_ALL) {
1842 /* Set start block address */
1843 this->write_word(0, this->base + ONENAND_REG_START_BLOCK_ADDRESS);
1844 /* Write unlock command */
1845 this->command(mtd, ONENAND_CMD_UNLOCK_ALL, 0, 0);
1847 /* There's no return value */
1848 this->wait(mtd, FL_LOCKING);
1851 while (this->read_word(this->base + ONENAND_REG_CTRL_STATUS)
1852 & ONENAND_CTRL_ONGO)
1855 /* Workaround for all block unlock in DDP */
1856 if (ONENAND_IS_DDP(this)) {
1857 /* 1st block on another chip */
1858 loff_t ofs = this->chipsize >> 1;
1859 size_t len = mtd->erasesize;
1861 onenand_unlock(mtd, ofs, len);
1864 onenand_check_lock_status(this);
1869 onenand_unlock(mtd, 0x0, this->chipsize);
1874 #ifdef CONFIG_MTD_ONENAND_OTP
1876 /* Interal OTP operation */
1877 typedef int (*otp_op_t)(struct mtd_info *mtd, loff_t form, size_t len,
1878 size_t *retlen, u_char *buf);
1881 * do_otp_read - [DEFAULT] Read OTP block area
1882 * @param mtd MTD device structure
1883 * @param from The offset to read
1884 * @param len number of bytes to read
1885 * @param retlen pointer to variable to store the number of readbytes
1886 * @param buf the databuffer to put/get data
1888 * Read OTP block area.
1890 static int do_otp_read(struct mtd_info *mtd, loff_t from, size_t len,
1891 size_t *retlen, u_char *buf)
1893 struct onenand_chip *this = mtd->priv;
1896 /* Enter OTP access mode */
1897 this->command(mtd, ONENAND_CMD_OTP_ACCESS, 0, 0);
1898 this->wait(mtd, FL_OTPING);
1900 ret = mtd->read(mtd, from, len, retlen, buf);
1902 /* Exit OTP access mode */
1903 this->command(mtd, ONENAND_CMD_RESET, 0, 0);
1904 this->wait(mtd, FL_RESETING);
1910 * do_otp_write - [DEFAULT] Write OTP block area
1911 * @param mtd MTD device structure
1912 * @param from The offset to write
1913 * @param len number of bytes to write
1914 * @param retlen pointer to variable to store the number of write bytes
1915 * @param buf the databuffer to put/get data
1917 * Write OTP block area.
1919 static int do_otp_write(struct mtd_info *mtd, loff_t from, size_t len,
1920 size_t *retlen, u_char *buf)
1922 struct onenand_chip *this = mtd->priv;
1923 unsigned char *pbuf = buf;
1926 /* Force buffer page aligned */
1927 if (len < mtd->writesize) {
1928 memcpy(this->page_buf, buf, len);
1929 memset(this->page_buf + len, 0xff, mtd->writesize - len);
1930 pbuf = this->page_buf;
1931 len = mtd->writesize;
1934 /* Enter OTP access mode */
1935 this->command(mtd, ONENAND_CMD_OTP_ACCESS, 0, 0);
1936 this->wait(mtd, FL_OTPING);
1938 ret = mtd->write(mtd, from, len, retlen, pbuf);
1940 /* Exit OTP access mode */
1941 this->command(mtd, ONENAND_CMD_RESET, 0, 0);
1942 this->wait(mtd, FL_RESETING);
1948 * do_otp_lock - [DEFAULT] Lock OTP block area
1949 * @param mtd MTD device structure
1950 * @param from The offset to lock
1951 * @param len number of bytes to lock
1952 * @param retlen pointer to variable to store the number of lock bytes
1953 * @param buf the databuffer to put/get data
1955 * Lock OTP block area.
1957 static int do_otp_lock(struct mtd_info *mtd, loff_t from, size_t len,
1958 size_t *retlen, u_char *buf)
1960 struct onenand_chip *this = mtd->priv;
1961 struct mtd_oob_ops ops = {
1962 .mode = MTD_OOB_PLACE,
1969 /* Enter OTP access mode */
1970 this->command(mtd, ONENAND_CMD_OTP_ACCESS, 0, 0);
1971 this->wait(mtd, FL_OTPING);
1973 ret = onenand_do_write_oob(mtd, from, &ops);
1975 *retlen = ops.oobretlen;
1977 /* Exit OTP access mode */
1978 this->command(mtd, ONENAND_CMD_RESET, 0, 0);
1979 this->wait(mtd, FL_RESETING);
1985 * onenand_otp_walk - [DEFAULT] Handle OTP operation
1986 * @param mtd MTD device structure
1987 * @param from The offset to read/write
1988 * @param len number of bytes to read/write
1989 * @param retlen pointer to variable to store the number of read bytes
1990 * @param buf the databuffer to put/get data
1991 * @param action do given action
1992 * @param mode specify user and factory
1994 * Handle OTP operation.
1996 static int onenand_otp_walk(struct mtd_info *mtd, loff_t from, size_t len,
1997 size_t *retlen, u_char *buf,
1998 otp_op_t action, int mode)
2000 struct onenand_chip *this = mtd->priv;
2007 density = this->device_id >> ONENAND_DEVICE_DENSITY_SHIFT;
2008 if (density < ONENAND_DEVICE_DENSITY_512Mb)
2013 if (mode == MTD_OTP_FACTORY) {
2014 from += mtd->writesize * otp_pages;
2015 otp_pages = 64 - otp_pages;
2018 /* Check User/Factory boundary */
2019 if (((mtd->writesize * otp_pages) - (from + len)) < 0)
2022 while (len > 0 && otp_pages > 0) {
2023 if (!action) { /* OTP Info functions */
2024 struct otp_info *otpinfo;
2026 len -= sizeof(struct otp_info);
2030 otpinfo = (struct otp_info *) buf;
2031 otpinfo->start = from;
2032 otpinfo->length = mtd->writesize;
2033 otpinfo->locked = 0;
2035 from += mtd->writesize;
2036 buf += sizeof(struct otp_info);
2037 *retlen += sizeof(struct otp_info);
2042 ret = action(mtd, from, len, &tmp_retlen, buf);
2058 * onenand_get_fact_prot_info - [MTD Interface] Read factory OTP info
2059 * @param mtd MTD device structure
2060 * @param buf the databuffer to put/get data
2061 * @param len number of bytes to read
2063 * Read factory OTP info.
2065 static int onenand_get_fact_prot_info(struct mtd_info *mtd,
2066 struct otp_info *buf, size_t len)
2071 ret = onenand_otp_walk(mtd, 0, len, &retlen, (u_char *) buf, NULL, MTD_OTP_FACTORY);
2073 return ret ? : retlen;
2077 * onenand_read_fact_prot_reg - [MTD Interface] Read factory OTP area
2078 * @param mtd MTD device structure
2079 * @param from The offset to read
2080 * @param len number of bytes to read
2081 * @param retlen pointer to variable to store the number of read bytes
2082 * @param buf the databuffer to put/get data
2084 * Read factory OTP area.
2086 static int onenand_read_fact_prot_reg(struct mtd_info *mtd, loff_t from,
2087 size_t len, size_t *retlen, u_char *buf)
2089 return onenand_otp_walk(mtd, from, len, retlen, buf, do_otp_read, MTD_OTP_FACTORY);
2093 * onenand_get_user_prot_info - [MTD Interface] Read user OTP info
2094 * @param mtd MTD device structure
2095 * @param buf the databuffer to put/get data
2096 * @param len number of bytes to read
2098 * Read user OTP info.
2100 static int onenand_get_user_prot_info(struct mtd_info *mtd,
2101 struct otp_info *buf, size_t len)
2106 ret = onenand_otp_walk(mtd, 0, len, &retlen, (u_char *) buf, NULL, MTD_OTP_USER);
2108 return ret ? : retlen;
2112 * onenand_read_user_prot_reg - [MTD Interface] Read user OTP area
2113 * @param mtd MTD device structure
2114 * @param from The offset to read
2115 * @param len number of bytes to read
2116 * @param retlen pointer to variable to store the number of read bytes
2117 * @param buf the databuffer to put/get data
2119 * Read user OTP area.
2121 static int onenand_read_user_prot_reg(struct mtd_info *mtd, loff_t from,
2122 size_t len, size_t *retlen, u_char *buf)
2124 return onenand_otp_walk(mtd, from, len, retlen, buf, do_otp_read, MTD_OTP_USER);
2128 * onenand_write_user_prot_reg - [MTD Interface] Write user OTP area
2129 * @param mtd MTD device structure
2130 * @param from The offset to write
2131 * @param len number of bytes to write
2132 * @param retlen pointer to variable to store the number of write bytes
2133 * @param buf the databuffer to put/get data
2135 * Write user OTP area.
2137 static int onenand_write_user_prot_reg(struct mtd_info *mtd, loff_t from,
2138 size_t len, size_t *retlen, u_char *buf)
2140 return onenand_otp_walk(mtd, from, len, retlen, buf, do_otp_write, MTD_OTP_USER);
2144 * onenand_lock_user_prot_reg - [MTD Interface] Lock user OTP area
2145 * @param mtd MTD device structure
2146 * @param from The offset to lock
2147 * @param len number of bytes to unlock
2149 * Write lock mark on spare area in page 0 in OTP block
2151 static int onenand_lock_user_prot_reg(struct mtd_info *mtd, loff_t from,
2154 unsigned char oob_buf[64];
2158 memset(oob_buf, 0xff, mtd->oobsize);
2160 * Note: OTP lock operation
2161 * OTP block : 0xXXFC
2162 * 1st block : 0xXXF3 (If chip support)
2163 * Both : 0xXXF0 (If chip support)
2165 oob_buf[ONENAND_OTP_LOCK_OFFSET] = 0xFC;
2168 * Write lock mark to 8th word of sector0 of page0 of the spare0.
2169 * We write 16 bytes spare area instead of 2 bytes.
2174 ret = onenand_otp_walk(mtd, from, len, &retlen, oob_buf, do_otp_lock, MTD_OTP_USER);
2176 return ret ? : retlen;
2178 #endif /* CONFIG_MTD_ONENAND_OTP */
2181 * onenand_check_features - Check and set OneNAND features
2182 * @param mtd MTD data structure
2184 * Check and set OneNAND features
2188 static void onenand_check_features(struct mtd_info *mtd)
2190 struct onenand_chip *this = mtd->priv;
2191 unsigned int density, process;
2193 /* Lock scheme depends on density and process */
2194 density = this->device_id >> ONENAND_DEVICE_DENSITY_SHIFT;
2195 process = this->version_id >> ONENAND_VERSION_PROCESS_SHIFT;
2199 case ONENAND_DEVICE_DENSITY_4Gb:
2200 this->options |= ONENAND_HAS_2PLANE;
2202 case ONENAND_DEVICE_DENSITY_2Gb:
2203 /* 2Gb DDP don't have 2 plane */
2204 if (!ONENAND_IS_DDP(this))
2205 this->options |= ONENAND_HAS_2PLANE;
2206 this->options |= ONENAND_HAS_UNLOCK_ALL;
2208 case ONENAND_DEVICE_DENSITY_1Gb:
2209 /* A-Die has all block unlock */
2211 this->options |= ONENAND_HAS_UNLOCK_ALL;
2215 /* Some OneNAND has continuous lock scheme */
2217 this->options |= ONENAND_HAS_CONT_LOCK;
2221 if (this->options & ONENAND_HAS_CONT_LOCK)
2222 printk(KERN_DEBUG "Lock scheme is Continuous Lock\n");
2223 if (this->options & ONENAND_HAS_UNLOCK_ALL)
2224 printk(KERN_DEBUG "Chip support all block unlock\n");
2225 if (this->options & ONENAND_HAS_2PLANE)
2226 printk(KERN_DEBUG "Chip has 2 plane\n");
2230 * onenand_print_device_info - Print device & version ID
2231 * @param device device ID
2232 * @param version version ID
2234 * Print device & version ID
2236 static void onenand_print_device_info(int device, int version)
2238 int vcc, demuxed, ddp, density;
2240 vcc = device & ONENAND_DEVICE_VCC_MASK;
2241 demuxed = device & ONENAND_DEVICE_IS_DEMUX;
2242 ddp = device & ONENAND_DEVICE_IS_DDP;
2243 density = device >> ONENAND_DEVICE_DENSITY_SHIFT;
2244 printk(KERN_INFO "%sOneNAND%s %dMB %sV 16-bit (0x%02x)\n",
2245 demuxed ? "" : "Muxed ",
2248 vcc ? "2.65/3.3" : "1.8",
2250 printk(KERN_DEBUG "OneNAND version = 0x%04x\n", version);
2253 static const struct onenand_manufacturers onenand_manuf_ids[] = {
2254 {ONENAND_MFR_SAMSUNG, "Samsung"},
2258 * onenand_check_maf - Check manufacturer ID
2259 * @param manuf manufacturer ID
2261 * Check manufacturer ID
2263 static int onenand_check_maf(int manuf)
2265 int size = ARRAY_SIZE(onenand_manuf_ids);
2269 for (i = 0; i < size; i++)
2270 if (manuf == onenand_manuf_ids[i].id)
2274 name = onenand_manuf_ids[i].name;
2278 printk(KERN_DEBUG "OneNAND Manufacturer: %s (0x%0x)\n", name, manuf);
2284 * onenand_probe - [OneNAND Interface] Probe the OneNAND device
2285 * @param mtd MTD device structure
2287 * OneNAND detection method:
2288 * Compare the values from command with ones from register
2290 static int onenand_probe(struct mtd_info *mtd)
2292 struct onenand_chip *this = mtd->priv;
2293 int bram_maf_id, bram_dev_id, maf_id, dev_id, ver_id;
2297 /* Save system configuration 1 */
2298 syscfg = this->read_word(this->base + ONENAND_REG_SYS_CFG1);
2299 /* Clear Sync. Burst Read mode to read BootRAM */
2300 this->write_word((syscfg & ~ONENAND_SYS_CFG1_SYNC_READ), this->base + ONENAND_REG_SYS_CFG1);
2302 /* Send the command for reading device ID from BootRAM */
2303 this->write_word(ONENAND_CMD_READID, this->base + ONENAND_BOOTRAM);
2305 /* Read manufacturer and device IDs from BootRAM */
2306 bram_maf_id = this->read_word(this->base + ONENAND_BOOTRAM + 0x0);
2307 bram_dev_id = this->read_word(this->base + ONENAND_BOOTRAM + 0x2);
2309 /* Reset OneNAND to read default register values */
2310 this->write_word(ONENAND_CMD_RESET, this->base + ONENAND_BOOTRAM);
2312 this->wait(mtd, FL_RESETING);
2314 /* Restore system configuration 1 */
2315 this->write_word(syscfg, this->base + ONENAND_REG_SYS_CFG1);
2317 /* Check manufacturer ID */
2318 if (onenand_check_maf(bram_maf_id))
2321 /* Read manufacturer and device IDs from Register */
2322 maf_id = this->read_word(this->base + ONENAND_REG_MANUFACTURER_ID);
2323 dev_id = this->read_word(this->base + ONENAND_REG_DEVICE_ID);
2324 ver_id = this->read_word(this->base + ONENAND_REG_VERSION_ID);
2326 /* Check OneNAND device */
2327 if (maf_id != bram_maf_id || dev_id != bram_dev_id)
2330 /* Flash device information */
2331 onenand_print_device_info(dev_id, ver_id);
2332 this->device_id = dev_id;
2333 this->version_id = ver_id;
2335 density = dev_id >> ONENAND_DEVICE_DENSITY_SHIFT;
2336 this->chipsize = (16 << density) << 20;
2337 /* Set density mask. it is used for DDP */
2338 if (ONENAND_IS_DDP(this))
2339 this->density_mask = (1 << (density + 6));
2341 this->density_mask = 0;
2343 /* OneNAND page size & block size */
2344 /* The data buffer size is equal to page size */
2345 mtd->writesize = this->read_word(this->base + ONENAND_REG_DATA_BUFFER_SIZE);
2346 mtd->oobsize = mtd->writesize >> 5;
2347 /* Pages per a block are always 64 in OneNAND */
2348 mtd->erasesize = mtd->writesize << 6;
2350 this->erase_shift = ffs(mtd->erasesize) - 1;
2351 this->page_shift = ffs(mtd->writesize) - 1;
2352 this->page_mask = (1 << (this->erase_shift - this->page_shift)) - 1;
2353 /* It's real page size */
2354 this->writesize = mtd->writesize;
2356 /* REVIST: Multichip handling */
2358 mtd->size = this->chipsize;
2360 /* Check OneNAND features */
2361 onenand_check_features(mtd);
2364 * We emulate the 4KiB page and 256KiB erase block size
2365 * But oobsize is still 64 bytes.
2366 * It is only valid if you turn on 2X program support,
2367 * Otherwise it will be ignored by compiler.
2369 if (ONENAND_IS_2PLANE(this)) {
2370 mtd->writesize <<= 1;
2371 mtd->erasesize <<= 1;
2378 * onenand_suspend - [MTD Interface] Suspend the OneNAND flash
2379 * @param mtd MTD device structure
2381 static int onenand_suspend(struct mtd_info *mtd)
2383 return onenand_get_device(mtd, FL_PM_SUSPENDED);
2387 * onenand_resume - [MTD Interface] Resume the OneNAND flash
2388 * @param mtd MTD device structure
2390 static void onenand_resume(struct mtd_info *mtd)
2392 struct onenand_chip *this = mtd->priv;
2394 if (this->state == FL_PM_SUSPENDED)
2395 onenand_release_device(mtd);
2397 printk(KERN_ERR "resume() called for the chip which is not"
2398 "in suspended state\n");
2402 * onenand_scan - [OneNAND Interface] Scan for the OneNAND device
2403 * @param mtd MTD device structure
2404 * @param maxchips Number of chips to scan for
2406 * This fills out all the not initialized function pointers
2407 * with the defaults.
2408 * The flash ID is read and the mtd/chip structures are
2409 * filled with the appropriate values.
2411 int onenand_scan(struct mtd_info *mtd, int maxchips)
2414 struct onenand_chip *this = mtd->priv;
2416 if (!this->read_word)
2417 this->read_word = onenand_readw;
2418 if (!this->write_word)
2419 this->write_word = onenand_writew;
2422 this->command = onenand_command;
2424 onenand_setup_wait(mtd);
2426 if (!this->read_bufferram)
2427 this->read_bufferram = onenand_read_bufferram;
2428 if (!this->write_bufferram)
2429 this->write_bufferram = onenand_write_bufferram;
2431 if (!this->block_markbad)
2432 this->block_markbad = onenand_default_block_markbad;
2433 if (!this->scan_bbt)
2434 this->scan_bbt = onenand_default_bbt;
2436 if (onenand_probe(mtd))
2439 /* Set Sync. Burst Read after probing */
2440 if (this->mmcontrol) {
2441 printk(KERN_INFO "OneNAND Sync. Burst Read support\n");
2442 this->read_bufferram = onenand_sync_read_bufferram;
2445 /* Allocate buffers, if necessary */
2446 if (!this->page_buf) {
2447 this->page_buf = kzalloc(mtd->writesize, GFP_KERNEL);
2448 if (!this->page_buf) {
2449 printk(KERN_ERR "onenand_scan(): Can't allocate page_buf\n");
2452 this->options |= ONENAND_PAGEBUF_ALLOC;
2454 if (!this->oob_buf) {
2455 this->oob_buf = kzalloc(mtd->oobsize, GFP_KERNEL);
2456 if (!this->oob_buf) {
2457 printk(KERN_ERR "onenand_scan(): Can't allocate oob_buf\n");
2458 if (this->options & ONENAND_PAGEBUF_ALLOC) {
2459 this->options &= ~ONENAND_PAGEBUF_ALLOC;
2460 kfree(this->page_buf);
2464 this->options |= ONENAND_OOBBUF_ALLOC;
2467 this->state = FL_READY;
2468 init_waitqueue_head(&this->wq);
2469 spin_lock_init(&this->chip_lock);
2472 * Allow subpage writes up to oobsize.
2474 switch (mtd->oobsize) {
2476 this->ecclayout = &onenand_oob_64;
2477 mtd->subpage_sft = 2;
2481 this->ecclayout = &onenand_oob_32;
2482 mtd->subpage_sft = 1;
2486 printk(KERN_WARNING "No OOB scheme defined for oobsize %d\n",
2488 mtd->subpage_sft = 0;
2489 /* To prevent kernel oops */
2490 this->ecclayout = &onenand_oob_32;
2494 this->subpagesize = mtd->writesize >> mtd->subpage_sft;
2497 * The number of bytes available for a client to place data into
2498 * the out of band area
2500 this->ecclayout->oobavail = 0;
2501 for (i = 0; i < MTD_MAX_OOBFREE_ENTRIES &&
2502 this->ecclayout->oobfree[i].length; i++)
2503 this->ecclayout->oobavail +=
2504 this->ecclayout->oobfree[i].length;
2505 mtd->oobavail = this->ecclayout->oobavail;
2507 mtd->ecclayout = this->ecclayout;
2509 /* Fill in remaining MTD driver data */
2510 mtd->type = MTD_NANDFLASH;
2511 mtd->flags = MTD_CAP_NANDFLASH;
2512 mtd->erase = onenand_erase;
2514 mtd->unpoint = NULL;
2515 mtd->read = onenand_read;
2516 mtd->write = onenand_write;
2517 mtd->read_oob = onenand_read_oob;
2518 mtd->write_oob = onenand_write_oob;
2519 #ifdef CONFIG_MTD_ONENAND_OTP
2520 mtd->get_fact_prot_info = onenand_get_fact_prot_info;
2521 mtd->read_fact_prot_reg = onenand_read_fact_prot_reg;
2522 mtd->get_user_prot_info = onenand_get_user_prot_info;
2523 mtd->read_user_prot_reg = onenand_read_user_prot_reg;
2524 mtd->write_user_prot_reg = onenand_write_user_prot_reg;
2525 mtd->lock_user_prot_reg = onenand_lock_user_prot_reg;
2527 mtd->sync = onenand_sync;
2528 mtd->lock = onenand_lock;
2529 mtd->unlock = onenand_unlock;
2530 mtd->suspend = onenand_suspend;
2531 mtd->resume = onenand_resume;
2532 mtd->block_isbad = onenand_block_isbad;
2533 mtd->block_markbad = onenand_block_markbad;
2534 mtd->owner = THIS_MODULE;
2536 /* Unlock whole block */
2537 onenand_unlock_all(mtd);
2539 return this->scan_bbt(mtd);
2543 * onenand_release - [OneNAND Interface] Free resources held by the OneNAND device
2544 * @param mtd MTD device structure
2546 void onenand_release(struct mtd_info *mtd)
2548 struct onenand_chip *this = mtd->priv;
2550 #ifdef CONFIG_MTD_PARTITIONS
2551 /* Deregister partitions */
2552 del_mtd_partitions (mtd);
2554 /* Deregister the device */
2555 del_mtd_device (mtd);
2557 /* Free bad block table memory, if allocated */
2559 struct bbm_info *bbm = this->bbm;
2563 /* Buffers allocated by onenand_scan */
2564 if (this->options & ONENAND_PAGEBUF_ALLOC)
2565 kfree(this->page_buf);
2566 if (this->options & ONENAND_OOBBUF_ALLOC)
2567 kfree(this->oob_buf);
2570 EXPORT_SYMBOL_GPL(onenand_scan);
2571 EXPORT_SYMBOL_GPL(onenand_release);
2573 MODULE_LICENSE("GPL");
2574 MODULE_AUTHOR("Kyungmin Park <kyungmin.park@samsung.com>");
2575 MODULE_DESCRIPTION("Generic OneNAND flash driver code");