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);
209 page &= this->page_mask;
213 /* NOTE: The setting order of the registers is very important! */
214 if (cmd == ONENAND_CMD_BUFFERRAM) {
215 /* Select DataRAM for DDP */
216 value = onenand_bufferram_address(this, block);
217 this->write_word(value, this->base + ONENAND_REG_START_ADDRESS2);
219 /* Switch to the next data buffer */
220 ONENAND_SET_NEXT_BUFFERRAM(this);
226 /* Write 'DFS, FBA' of Flash */
227 value = onenand_block_address(this, block);
228 this->write_word(value, this->base + ONENAND_REG_START_ADDRESS1);
231 /* Select DataRAM for DDP */
232 value = onenand_bufferram_address(this, block);
233 this->write_word(value, this->base + ONENAND_REG_START_ADDRESS2);
238 /* Now we use page size operation */
239 int sectors = 4, count = 4;
243 case ONENAND_CMD_READ:
244 case ONENAND_CMD_READOOB:
245 dataram = ONENAND_SET_NEXT_BUFFERRAM(this);
250 dataram = ONENAND_CURRENT_BUFFERRAM(this);
254 /* Write 'FPA, FSA' of Flash */
255 value = onenand_page_address(page, sectors);
256 this->write_word(value, this->base + ONENAND_REG_START_ADDRESS8);
258 /* Write 'BSA, BSC' of DataRAM */
259 value = onenand_buffer_address(dataram, sectors, count);
260 this->write_word(value, this->base + ONENAND_REG_START_BUFFER);
263 /* Select DataRAM for DDP */
264 value = onenand_bufferram_address(this, block);
265 this->write_word(value, this->base + ONENAND_REG_START_ADDRESS2);
269 /* Interrupt clear */
270 this->write_word(ONENAND_INT_CLEAR, this->base + ONENAND_REG_INTERRUPT);
273 this->write_word(cmd, this->base + ONENAND_REG_COMMAND);
279 * onenand_wait - [DEFAULT] wait until the command is done
280 * @param mtd MTD device structure
281 * @param state state to select the max. timeout value
283 * Wait for command done. This applies to all OneNAND command
284 * Read can take up to 30us, erase up to 2ms and program up to 350us
285 * according to general OneNAND specs
287 static int onenand_wait(struct mtd_info *mtd, int state)
289 struct onenand_chip * this = mtd->priv;
290 unsigned long timeout;
291 unsigned int flags = ONENAND_INT_MASTER;
292 unsigned int interrupt = 0;
295 /* The 20 msec is enough */
296 timeout = jiffies + msecs_to_jiffies(20);
297 while (time_before(jiffies, timeout)) {
298 interrupt = this->read_word(this->base + ONENAND_REG_INTERRUPT);
300 if (interrupt & flags)
303 if (state != FL_READING)
306 /* To get correct interrupt status in timeout case */
307 interrupt = this->read_word(this->base + ONENAND_REG_INTERRUPT);
309 ctrl = this->read_word(this->base + ONENAND_REG_CTRL_STATUS);
311 if (ctrl & ONENAND_CTRL_ERROR) {
312 printk(KERN_ERR "onenand_wait: controller error = 0x%04x\n", ctrl);
313 if (ctrl & ONENAND_CTRL_LOCK)
314 printk(KERN_ERR "onenand_wait: it's locked error.\n");
318 if (interrupt & ONENAND_INT_READ) {
319 int ecc = this->read_word(this->base + ONENAND_REG_ECC_STATUS);
321 printk(KERN_ERR "onenand_wait: ECC error = 0x%04x\n", ecc);
322 if (ecc & ONENAND_ECC_2BIT_ALL) {
323 mtd->ecc_stats.failed++;
325 } else if (ecc & ONENAND_ECC_1BIT_ALL)
326 mtd->ecc_stats.corrected++;
328 } else if (state == FL_READING) {
329 printk(KERN_ERR "onenand_wait: read timeout! ctrl=0x%04x intr=0x%04x\n", ctrl, interrupt);
337 * onenand_interrupt - [DEFAULT] onenand interrupt handler
338 * @param irq onenand interrupt number
339 * @param dev_id interrupt data
343 static irqreturn_t onenand_interrupt(int irq, void *data)
345 struct onenand_chip *this = (struct onenand_chip *) data;
347 /* To handle shared interrupt */
348 if (!this->complete.done)
349 complete(&this->complete);
355 * onenand_interrupt_wait - [DEFAULT] wait until the command is done
356 * @param mtd MTD device structure
357 * @param state state to select the max. timeout value
359 * Wait for command done.
361 static int onenand_interrupt_wait(struct mtd_info *mtd, int state)
363 struct onenand_chip *this = mtd->priv;
365 wait_for_completion(&this->complete);
367 return onenand_wait(mtd, state);
371 * onenand_try_interrupt_wait - [DEFAULT] try interrupt wait
372 * @param mtd MTD device structure
373 * @param state state to select the max. timeout value
375 * Try interrupt based wait (It is used one-time)
377 static int onenand_try_interrupt_wait(struct mtd_info *mtd, int state)
379 struct onenand_chip *this = mtd->priv;
380 unsigned long remain, timeout;
382 /* We use interrupt wait first */
383 this->wait = onenand_interrupt_wait;
385 timeout = msecs_to_jiffies(100);
386 remain = wait_for_completion_timeout(&this->complete, timeout);
388 printk(KERN_INFO "OneNAND: There's no interrupt. "
389 "We use the normal wait\n");
391 /* Release the irq */
392 free_irq(this->irq, this);
394 this->wait = onenand_wait;
397 return onenand_wait(mtd, state);
401 * onenand_setup_wait - [OneNAND Interface] setup onenand wait method
402 * @param mtd MTD device structure
404 * There's two method to wait onenand work
405 * 1. polling - read interrupt status register
406 * 2. interrupt - use the kernel interrupt method
408 static void onenand_setup_wait(struct mtd_info *mtd)
410 struct onenand_chip *this = mtd->priv;
413 init_completion(&this->complete);
415 if (this->irq <= 0) {
416 this->wait = onenand_wait;
420 if (request_irq(this->irq, &onenand_interrupt,
421 IRQF_SHARED, "onenand", this)) {
422 /* If we can't get irq, use the normal wait */
423 this->wait = onenand_wait;
427 /* Enable interrupt */
428 syscfg = this->read_word(this->base + ONENAND_REG_SYS_CFG1);
429 syscfg |= ONENAND_SYS_CFG1_IOBE;
430 this->write_word(syscfg, this->base + ONENAND_REG_SYS_CFG1);
432 this->wait = onenand_try_interrupt_wait;
436 * onenand_bufferram_offset - [DEFAULT] BufferRAM offset
437 * @param mtd MTD data structure
438 * @param area BufferRAM area
439 * @return offset given area
441 * Return BufferRAM offset given area
443 static inline int onenand_bufferram_offset(struct mtd_info *mtd, int area)
445 struct onenand_chip *this = mtd->priv;
447 if (ONENAND_CURRENT_BUFFERRAM(this)) {
448 if (area == ONENAND_DATARAM)
449 return mtd->writesize;
450 if (area == ONENAND_SPARERAM)
458 * onenand_read_bufferram - [OneNAND Interface] Read the bufferram area
459 * @param mtd MTD data structure
460 * @param area BufferRAM area
461 * @param buffer the databuffer to put/get data
462 * @param offset offset to read from or write to
463 * @param count number of bytes to read/write
465 * Read the BufferRAM area
467 static int onenand_read_bufferram(struct mtd_info *mtd, int area,
468 unsigned char *buffer, int offset, size_t count)
470 struct onenand_chip *this = mtd->priv;
471 void __iomem *bufferram;
473 bufferram = this->base + area;
475 bufferram += onenand_bufferram_offset(mtd, area);
477 if (ONENAND_CHECK_BYTE_ACCESS(count)) {
480 /* Align with word(16-bit) size */
483 /* Read word and save byte */
484 word = this->read_word(bufferram + offset + count);
485 buffer[count] = (word & 0xff);
488 memcpy(buffer, bufferram + offset, count);
494 * onenand_sync_read_bufferram - [OneNAND Interface] Read the bufferram area with Sync. Burst mode
495 * @param mtd MTD data structure
496 * @param area BufferRAM area
497 * @param buffer the databuffer to put/get data
498 * @param offset offset to read from or write to
499 * @param count number of bytes to read/write
501 * Read the BufferRAM area with Sync. Burst Mode
503 static int onenand_sync_read_bufferram(struct mtd_info *mtd, int area,
504 unsigned char *buffer, int offset, size_t count)
506 struct onenand_chip *this = mtd->priv;
507 void __iomem *bufferram;
509 bufferram = this->base + area;
511 bufferram += onenand_bufferram_offset(mtd, area);
513 this->mmcontrol(mtd, ONENAND_SYS_CFG1_SYNC_READ);
515 if (ONENAND_CHECK_BYTE_ACCESS(count)) {
518 /* Align with word(16-bit) size */
521 /* Read word and save byte */
522 word = this->read_word(bufferram + offset + count);
523 buffer[count] = (word & 0xff);
526 memcpy(buffer, bufferram + offset, count);
528 this->mmcontrol(mtd, 0);
534 * onenand_write_bufferram - [OneNAND Interface] Write the bufferram area
535 * @param mtd MTD data structure
536 * @param area BufferRAM area
537 * @param buffer the databuffer to put/get data
538 * @param offset offset to read from or write to
539 * @param count number of bytes to read/write
541 * Write the BufferRAM area
543 static int onenand_write_bufferram(struct mtd_info *mtd, int area,
544 const unsigned char *buffer, int offset, size_t count)
546 struct onenand_chip *this = mtd->priv;
547 void __iomem *bufferram;
549 bufferram = this->base + area;
551 bufferram += onenand_bufferram_offset(mtd, area);
553 if (ONENAND_CHECK_BYTE_ACCESS(count)) {
557 /* Align with word(16-bit) size */
560 /* Calculate byte access offset */
561 byte_offset = offset + count;
563 /* Read word and save byte */
564 word = this->read_word(bufferram + byte_offset);
565 word = (word & ~0xff) | buffer[count];
566 this->write_word(word, bufferram + byte_offset);
569 memcpy(bufferram + offset, buffer, count);
575 * onenand_check_bufferram - [GENERIC] Check BufferRAM information
576 * @param mtd MTD data structure
577 * @param addr address to check
578 * @return 1 if there are valid data, otherwise 0
580 * Check bufferram if there is data we required
582 static int onenand_check_bufferram(struct mtd_info *mtd, loff_t addr)
584 struct onenand_chip *this = mtd->priv;
585 int blockpage, found = 0;
588 blockpage = (int) (addr >> this->page_shift);
590 /* Is there valid data? */
591 i = ONENAND_CURRENT_BUFFERRAM(this);
592 if (this->bufferram[i].blockpage == blockpage)
595 /* Check another BufferRAM */
596 i = ONENAND_NEXT_BUFFERRAM(this);
597 if (this->bufferram[i].blockpage == blockpage) {
598 ONENAND_SET_NEXT_BUFFERRAM(this);
603 if (found && ONENAND_IS_DDP(this)) {
604 /* Select DataRAM for DDP */
605 int block = (int) (addr >> this->erase_shift);
606 int value = onenand_bufferram_address(this, block);
607 this->write_word(value, this->base + ONENAND_REG_START_ADDRESS2);
614 * onenand_update_bufferram - [GENERIC] Update BufferRAM information
615 * @param mtd MTD data structure
616 * @param addr address to update
617 * @param valid valid flag
619 * Update BufferRAM information
621 static void onenand_update_bufferram(struct mtd_info *mtd, loff_t addr,
624 struct onenand_chip *this = mtd->priv;
628 blockpage = (int) (addr >> this->page_shift);
630 /* Invalidate another BufferRAM */
631 i = ONENAND_NEXT_BUFFERRAM(this);
632 if (this->bufferram[i].blockpage == blockpage)
633 this->bufferram[i].blockpage = -1;
635 /* Update BufferRAM */
636 i = ONENAND_CURRENT_BUFFERRAM(this);
638 this->bufferram[i].blockpage = blockpage;
640 this->bufferram[i].blockpage = -1;
644 * onenand_invalidate_bufferram - [GENERIC] Invalidate BufferRAM information
645 * @param mtd MTD data structure
646 * @param addr start address to invalidate
647 * @param len length to invalidate
649 * Invalidate BufferRAM information
651 static void onenand_invalidate_bufferram(struct mtd_info *mtd, loff_t addr,
654 struct onenand_chip *this = mtd->priv;
656 loff_t end_addr = addr + len;
658 /* Invalidate BufferRAM */
659 for (i = 0; i < MAX_BUFFERRAM; i++) {
660 loff_t buf_addr = this->bufferram[i].blockpage << this->page_shift;
661 if (buf_addr >= addr && buf_addr < end_addr)
662 this->bufferram[i].blockpage = -1;
667 * onenand_get_device - [GENERIC] Get chip for selected access
668 * @param mtd MTD device structure
669 * @param new_state the state which is requested
671 * Get the device and lock it for exclusive access
673 static int onenand_get_device(struct mtd_info *mtd, int new_state)
675 struct onenand_chip *this = mtd->priv;
676 DECLARE_WAITQUEUE(wait, current);
679 * Grab the lock and see if the device is available
682 spin_lock(&this->chip_lock);
683 if (this->state == FL_READY) {
684 this->state = new_state;
685 spin_unlock(&this->chip_lock);
688 if (new_state == FL_PM_SUSPENDED) {
689 spin_unlock(&this->chip_lock);
690 return (this->state == FL_PM_SUSPENDED) ? 0 : -EAGAIN;
692 set_current_state(TASK_UNINTERRUPTIBLE);
693 add_wait_queue(&this->wq, &wait);
694 spin_unlock(&this->chip_lock);
696 remove_wait_queue(&this->wq, &wait);
703 * onenand_release_device - [GENERIC] release chip
704 * @param mtd MTD device structure
706 * Deselect, release chip lock and wake up anyone waiting on the device
708 static void onenand_release_device(struct mtd_info *mtd)
710 struct onenand_chip *this = mtd->priv;
712 /* Release the chip */
713 spin_lock(&this->chip_lock);
714 this->state = FL_READY;
716 spin_unlock(&this->chip_lock);
720 * onenand_read - [MTD Interface] Read data from flash
721 * @param mtd MTD device structure
722 * @param from offset to read from
723 * @param len number of bytes to read
724 * @param retlen pointer to variable to store the number of read bytes
725 * @param buf the databuffer to put data
729 static int onenand_read(struct mtd_info *mtd, loff_t from, size_t len,
730 size_t *retlen, u_char *buf)
732 struct onenand_chip *this = mtd->priv;
733 struct mtd_ecc_stats stats;
734 int read = 0, column;
736 int ret = 0, boundary = 0;
738 DEBUG(MTD_DEBUG_LEVEL3, "onenand_read: from = 0x%08x, len = %i\n", (unsigned int) from, (int) len);
740 /* Do not allow reads past end of device */
741 if ((from + len) > mtd->size) {
742 printk(KERN_ERR "onenand_read: Attempt read beyond end of device\n");
747 /* Grab the lock and see if the device is available */
748 onenand_get_device(mtd, FL_READING);
750 stats = mtd->ecc_stats;
752 /* Read-while-load method */
754 /* Do first load to bufferRAM */
756 if (!onenand_check_bufferram(mtd, from)) {
757 this->command(mtd, ONENAND_CMD_READ, from, mtd->writesize);
758 ret = this->wait(mtd, FL_READING);
759 onenand_update_bufferram(mtd, from, !ret);
763 thislen = min_t(int, mtd->writesize, len - read);
764 column = from & (mtd->writesize - 1);
765 if (column + thislen > mtd->writesize)
766 thislen = mtd->writesize - column;
769 /* If there is more to load then start next load */
771 if (read + thislen < len) {
772 this->command(mtd, ONENAND_CMD_READ, from, mtd->writesize);
774 * Chip boundary handling in DDP
775 * Now we issued chip 1 read and pointed chip 1
776 * bufferam so we have to point chip 0 bufferam.
778 if (ONENAND_IS_DDP(this) &&
779 unlikely(from == (this->chipsize >> 1))) {
780 this->write_word(ONENAND_DDP_CHIP0, this->base + ONENAND_REG_START_ADDRESS2);
784 ONENAND_SET_PREV_BUFFERRAM(this);
786 /* While load is going, read from last bufferRAM */
787 this->read_bufferram(mtd, ONENAND_DATARAM, buf, column, thislen);
788 /* See if we are done */
792 /* Set up for next read from bufferRAM */
793 if (unlikely(boundary))
794 this->write_word(ONENAND_DDP_CHIP1, this->base + ONENAND_REG_START_ADDRESS2);
795 ONENAND_SET_NEXT_BUFFERRAM(this);
797 thislen = min_t(int, mtd->writesize, len - read);
800 /* Now wait for load */
801 ret = this->wait(mtd, FL_READING);
802 onenand_update_bufferram(mtd, from, !ret);
805 /* Deselect and wake up anyone waiting on the device */
806 onenand_release_device(mtd);
809 * Return success, if no ECC failures, else -EBADMSG
810 * fs driver will take care of that, because
811 * retlen == desired len and result == -EBADMSG
815 if (mtd->ecc_stats.failed - stats.failed)
821 return mtd->ecc_stats.corrected - stats.corrected ? -EUCLEAN : 0;
825 * onenand_transfer_auto_oob - [Internal] oob auto-placement transfer
826 * @param mtd MTD device structure
827 * @param buf destination address
828 * @param column oob offset to read from
829 * @param thislen oob length to read
831 static int onenand_transfer_auto_oob(struct mtd_info *mtd, uint8_t *buf, int column,
834 struct onenand_chip *this = mtd->priv;
835 struct nand_oobfree *free;
836 int readcol = column;
837 int readend = column + thislen;
839 uint8_t *oob_buf = this->oob_buf;
841 for (free = this->ecclayout->oobfree; free->length; ++free) {
842 if (readcol >= lastgap)
843 readcol += free->offset - lastgap;
844 if (readend >= lastgap)
845 readend += free->offset - lastgap;
846 lastgap = free->offset + free->length;
848 this->read_bufferram(mtd, ONENAND_SPARERAM, oob_buf, 0, mtd->oobsize);
849 for (free = this->ecclayout->oobfree; free->length; ++free) {
850 int free_end = free->offset + free->length;
851 if (free->offset < readend && free_end > readcol) {
852 int st = max_t(int,free->offset,readcol);
853 int ed = min_t(int,free_end,readend);
855 memcpy(buf, oob_buf + st, n);
864 * onenand_do_read_oob - [MTD Interface] OneNAND read out-of-band
865 * @param mtd MTD device structure
866 * @param from offset to read from
867 * @param len number of bytes to read
868 * @param retlen pointer to variable to store the number of read bytes
869 * @param buf the databuffer to put data
870 * @param mode operation mode
872 * OneNAND read out-of-band data from the spare area
874 static int onenand_do_read_oob(struct mtd_info *mtd, loff_t from, size_t len,
875 size_t *retlen, u_char *buf, mtd_oob_mode_t mode)
877 struct onenand_chip *this = mtd->priv;
878 int read = 0, thislen, column, oobsize;
881 DEBUG(MTD_DEBUG_LEVEL3, "onenand_read_oob: from = 0x%08x, len = %i\n", (unsigned int) from, (int) len);
883 /* Initialize return length value */
886 if (mode == MTD_OOB_AUTO)
887 oobsize = this->ecclayout->oobavail;
889 oobsize = mtd->oobsize;
891 column = from & (mtd->oobsize - 1);
893 if (unlikely(column >= oobsize)) {
894 printk(KERN_ERR "onenand_read_oob: Attempted to start read outside oob\n");
898 /* Do not allow reads past end of device */
899 if (unlikely(from >= mtd->size ||
900 column + len > ((mtd->size >> this->page_shift) -
901 (from >> this->page_shift)) * oobsize)) {
902 printk(KERN_ERR "onenand_read_oob: Attempted to read beyond end of device\n");
906 /* Grab the lock and see if the device is available */
907 onenand_get_device(mtd, FL_READING);
912 thislen = oobsize - column;
913 thislen = min_t(int, thislen, len);
915 this->command(mtd, ONENAND_CMD_READOOB, from, mtd->oobsize);
917 onenand_update_bufferram(mtd, from, 0);
919 ret = this->wait(mtd, FL_READING);
920 /* First copy data and check return value for ECC handling */
922 if (mode == MTD_OOB_AUTO)
923 onenand_transfer_auto_oob(mtd, buf, column, thislen);
925 this->read_bufferram(mtd, ONENAND_SPARERAM, buf, column, thislen);
928 printk(KERN_ERR "onenand_read_oob: read failed = 0x%x\n", ret);
942 from += mtd->writesize;
947 /* Deselect and wake up anyone waiting on the device */
948 onenand_release_device(mtd);
955 * onenand_read_oob - [MTD Interface] NAND write data and/or out-of-band
956 * @param mtd: MTD device structure
957 * @param from: offset to read from
958 * @param ops: oob operation description structure
960 static int onenand_read_oob(struct mtd_info *mtd, loff_t from,
961 struct mtd_oob_ops *ops)
968 /* Not implemented yet */
972 return onenand_do_read_oob(mtd, from + ops->ooboffs, ops->ooblen,
973 &ops->oobretlen, ops->oobbuf, ops->mode);
977 * onenand_bbt_wait - [DEFAULT] wait until the command is done
978 * @param mtd MTD device structure
979 * @param state state to select the max. timeout value
981 * Wait for command done.
983 static int onenand_bbt_wait(struct mtd_info *mtd, int state)
985 struct onenand_chip *this = mtd->priv;
986 unsigned long timeout;
987 unsigned int interrupt;
990 /* The 20 msec is enough */
991 timeout = jiffies + msecs_to_jiffies(20);
992 while (time_before(jiffies, timeout)) {
993 interrupt = this->read_word(this->base + ONENAND_REG_INTERRUPT);
994 if (interrupt & ONENAND_INT_MASTER)
997 /* To get correct interrupt status in timeout case */
998 interrupt = this->read_word(this->base + ONENAND_REG_INTERRUPT);
999 ctrl = this->read_word(this->base + ONENAND_REG_CTRL_STATUS);
1001 if (ctrl & ONENAND_CTRL_ERROR) {
1002 printk(KERN_DEBUG "onenand_bbt_wait: controller error = 0x%04x\n", ctrl);
1003 /* Initial bad block case */
1004 if (ctrl & ONENAND_CTRL_LOAD)
1005 return ONENAND_BBT_READ_ERROR;
1006 return ONENAND_BBT_READ_FATAL_ERROR;
1009 if (interrupt & ONENAND_INT_READ) {
1010 int ecc = this->read_word(this->base + ONENAND_REG_ECC_STATUS);
1011 if (ecc & ONENAND_ECC_2BIT_ALL)
1012 return ONENAND_BBT_READ_ERROR;
1014 printk(KERN_ERR "onenand_bbt_wait: read timeout!"
1015 "ctrl=0x%04x intr=0x%04x\n", ctrl, interrupt);
1016 return ONENAND_BBT_READ_FATAL_ERROR;
1023 * onenand_bbt_read_oob - [MTD Interface] OneNAND read out-of-band for bbt scan
1024 * @param mtd MTD device structure
1025 * @param from offset to read from
1026 * @param ops oob operation description structure
1028 * OneNAND read out-of-band data from the spare area for bbt scan
1030 int onenand_bbt_read_oob(struct mtd_info *mtd, loff_t from,
1031 struct mtd_oob_ops *ops)
1033 struct onenand_chip *this = mtd->priv;
1034 int read = 0, thislen, column;
1036 size_t len = ops->ooblen;
1037 u_char *buf = ops->oobbuf;
1039 DEBUG(MTD_DEBUG_LEVEL3, "onenand_bbt_read_oob: from = 0x%08x, len = %zi\n", (unsigned int) from, len);
1041 /* Initialize return value */
1044 /* Do not allow reads past end of device */
1045 if (unlikely((from + len) > mtd->size)) {
1046 printk(KERN_ERR "onenand_bbt_read_oob: Attempt read beyond end of device\n");
1047 return ONENAND_BBT_READ_FATAL_ERROR;
1050 /* Grab the lock and see if the device is available */
1051 onenand_get_device(mtd, FL_READING);
1053 column = from & (mtd->oobsize - 1);
1055 while (read < len) {
1058 thislen = mtd->oobsize - column;
1059 thislen = min_t(int, thislen, len);
1061 this->command(mtd, ONENAND_CMD_READOOB, from, mtd->oobsize);
1063 onenand_update_bufferram(mtd, from, 0);
1065 ret = onenand_bbt_wait(mtd, FL_READING);
1069 this->read_bufferram(mtd, ONENAND_SPARERAM, buf, column, thislen);
1078 /* Update Page size */
1079 from += mtd->writesize;
1084 /* Deselect and wake up anyone waiting on the device */
1085 onenand_release_device(mtd);
1087 ops->oobretlen = read;
1091 #ifdef CONFIG_MTD_ONENAND_VERIFY_WRITE
1093 * onenand_verify_oob - [GENERIC] verify the oob contents after a write
1094 * @param mtd MTD device structure
1095 * @param buf the databuffer to verify
1096 * @param to offset to read from
1099 static int onenand_verify_oob(struct mtd_info *mtd, const u_char *buf, loff_t to)
1101 struct onenand_chip *this = mtd->priv;
1105 this->command(mtd, ONENAND_CMD_READOOB, to, mtd->oobsize);
1106 onenand_update_bufferram(mtd, to, 0);
1107 status = this->wait(mtd, FL_READING);
1111 this->read_bufferram(mtd, ONENAND_SPARERAM, oobbuf, 0, mtd->oobsize);
1112 for (i = 0; i < mtd->oobsize; i++)
1113 if (buf[i] != 0xFF && buf[i] != oobbuf[i])
1120 * onenand_verify - [GENERIC] verify the chip contents after a write
1121 * @param mtd MTD device structure
1122 * @param buf the databuffer to verify
1123 * @param addr offset to read from
1124 * @param len number of bytes to read and compare
1127 static int onenand_verify(struct mtd_info *mtd, const u_char *buf, loff_t addr, size_t len)
1129 struct onenand_chip *this = mtd->priv;
1130 void __iomem *dataram;
1132 int thislen, column;
1135 thislen = min_t(int, mtd->writesize, len);
1136 column = addr & (mtd->writesize - 1);
1137 if (column + thislen > mtd->writesize)
1138 thislen = mtd->writesize - column;
1140 this->command(mtd, ONENAND_CMD_READ, addr, mtd->writesize);
1142 onenand_update_bufferram(mtd, addr, 0);
1144 ret = this->wait(mtd, FL_READING);
1148 onenand_update_bufferram(mtd, addr, 1);
1150 dataram = this->base + ONENAND_DATARAM;
1151 dataram += onenand_bufferram_offset(mtd, ONENAND_DATARAM);
1153 if (memcmp(buf, dataram + column, thislen))
1164 #define onenand_verify(...) (0)
1165 #define onenand_verify_oob(...) (0)
1168 #define NOTALIGNED(x) ((x & (this->subpagesize - 1)) != 0)
1171 * onenand_write - [MTD Interface] write buffer to FLASH
1172 * @param mtd MTD device structure
1173 * @param to offset to write to
1174 * @param len number of bytes to write
1175 * @param retlen pointer to variable to store the number of written bytes
1176 * @param buf the data to write
1180 static int onenand_write(struct mtd_info *mtd, loff_t to, size_t len,
1181 size_t *retlen, const u_char *buf)
1183 struct onenand_chip *this = mtd->priv;
1186 int column, subpage;
1188 DEBUG(MTD_DEBUG_LEVEL3, "onenand_write: to = 0x%08x, len = %i\n", (unsigned int) to, (int) len);
1190 /* Initialize retlen, in case of early exit */
1193 /* Do not allow writes past end of device */
1194 if (unlikely((to + len) > mtd->size)) {
1195 printk(KERN_ERR "onenand_write: Attempt write to past end of device\n");
1199 /* Reject writes, which are not page aligned */
1200 if (unlikely(NOTALIGNED(to)) || unlikely(NOTALIGNED(len))) {
1201 printk(KERN_ERR "onenand_write: Attempt to write not page aligned data\n");
1205 column = to & (mtd->writesize - 1);
1207 /* Grab the lock and see if the device is available */
1208 onenand_get_device(mtd, FL_WRITING);
1210 /* Loop until all data write */
1211 while (written < len) {
1212 int thislen = min_t(int, mtd->writesize - column, len - written);
1213 u_char *wbuf = (u_char *) buf;
1217 this->command(mtd, ONENAND_CMD_BUFFERRAM, to, thislen);
1219 /* Partial page write */
1220 subpage = thislen < mtd->writesize;
1222 memset(this->page_buf, 0xff, mtd->writesize);
1223 memcpy(this->page_buf + column, buf, thislen);
1224 wbuf = this->page_buf;
1227 this->write_bufferram(mtd, ONENAND_DATARAM, wbuf, 0, mtd->writesize);
1228 this->write_bufferram(mtd, ONENAND_SPARERAM, ffchars, 0, mtd->oobsize);
1230 this->command(mtd, ONENAND_CMD_PROG, to, mtd->writesize);
1232 ret = this->wait(mtd, FL_WRITING);
1234 /* In partial page write we don't update bufferram */
1235 onenand_update_bufferram(mtd, to, !ret && !subpage);
1238 printk(KERN_ERR "onenand_write: write filaed %d\n", ret);
1242 /* Only check verify write turn on */
1243 ret = onenand_verify(mtd, (u_char *) wbuf, to, thislen);
1245 printk(KERN_ERR "onenand_write: verify failed %d\n", ret);
1259 /* Deselect and wake up anyone waiting on the device */
1260 onenand_release_device(mtd);
1268 * onenand_fill_auto_oob - [Internal] oob auto-placement transfer
1269 * @param mtd MTD device structure
1270 * @param oob_buf oob buffer
1271 * @param buf source address
1272 * @param column oob offset to write to
1273 * @param thislen oob length to write
1275 static int onenand_fill_auto_oob(struct mtd_info *mtd, u_char *oob_buf,
1276 const u_char *buf, int column, int thislen)
1278 struct onenand_chip *this = mtd->priv;
1279 struct nand_oobfree *free;
1280 int writecol = column;
1281 int writeend = column + thislen;
1284 for (free = this->ecclayout->oobfree; free->length; ++free) {
1285 if (writecol >= lastgap)
1286 writecol += free->offset - lastgap;
1287 if (writeend >= lastgap)
1288 writeend += free->offset - lastgap;
1289 lastgap = free->offset + free->length;
1291 for (free = this->ecclayout->oobfree; free->length; ++free) {
1292 int free_end = free->offset + free->length;
1293 if (free->offset < writeend && free_end > writecol) {
1294 int st = max_t(int,free->offset,writecol);
1295 int ed = min_t(int,free_end,writeend);
1297 memcpy(oob_buf + st, buf, n);
1306 * onenand_do_write_oob - [Internal] OneNAND write out-of-band
1307 * @param mtd MTD device structure
1308 * @param to offset to write to
1309 * @param len number of bytes to write
1310 * @param retlen pointer to variable to store the number of written bytes
1311 * @param buf the data to write
1312 * @param mode operation mode
1314 * OneNAND write out-of-band
1316 static int onenand_do_write_oob(struct mtd_info *mtd, loff_t to, size_t len,
1317 size_t *retlen, const u_char *buf, mtd_oob_mode_t mode)
1319 struct onenand_chip *this = mtd->priv;
1320 int column, ret = 0, oobsize;
1324 DEBUG(MTD_DEBUG_LEVEL3, "onenand_write_oob: to = 0x%08x, len = %i\n", (unsigned int) to, (int) len);
1326 /* Initialize retlen, in case of early exit */
1329 if (mode == MTD_OOB_AUTO)
1330 oobsize = this->ecclayout->oobavail;
1332 oobsize = mtd->oobsize;
1334 column = to & (mtd->oobsize - 1);
1336 if (unlikely(column >= oobsize)) {
1337 printk(KERN_ERR "onenand_write_oob: Attempted to start write outside oob\n");
1341 /* For compatibility with NAND: Do not allow write past end of page */
1342 if (unlikely(column + len > oobsize)) {
1343 printk(KERN_ERR "onenand_write_oob: "
1344 "Attempt to write past end of page\n");
1348 /* Do not allow reads past end of device */
1349 if (unlikely(to >= mtd->size ||
1350 column + len > ((mtd->size >> this->page_shift) -
1351 (to >> this->page_shift)) * oobsize)) {
1352 printk(KERN_ERR "onenand_write_oob: Attempted to write past end of device\n");
1356 /* Grab the lock and see if the device is available */
1357 onenand_get_device(mtd, FL_WRITING);
1359 oobbuf = this->oob_buf;
1361 /* Loop until all data write */
1362 while (written < len) {
1363 int thislen = min_t(int, oobsize, len - written);
1367 this->command(mtd, ONENAND_CMD_BUFFERRAM, to, mtd->oobsize);
1369 /* We send data to spare ram with oobsize
1370 * to prevent byte access */
1371 memset(oobbuf, 0xff, mtd->oobsize);
1372 if (mode == MTD_OOB_AUTO)
1373 onenand_fill_auto_oob(mtd, oobbuf, buf, column, thislen);
1375 memcpy(oobbuf + column, buf, thislen);
1376 this->write_bufferram(mtd, ONENAND_SPARERAM, oobbuf, 0, mtd->oobsize);
1378 this->command(mtd, ONENAND_CMD_PROGOOB, to, mtd->oobsize);
1380 onenand_update_bufferram(mtd, to, 0);
1382 ret = this->wait(mtd, FL_WRITING);
1384 printk(KERN_ERR "onenand_write_oob: write failed %d\n", ret);
1388 ret = onenand_verify_oob(mtd, oobbuf, to);
1390 printk(KERN_ERR "onenand_write_oob: verify failed %d\n", ret);
1398 to += mtd->writesize;
1403 /* Deselect and wake up anyone waiting on the device */
1404 onenand_release_device(mtd);
1412 * onenand_write_oob - [MTD Interface] NAND write data and/or out-of-band
1413 * @param mtd: MTD device structure
1414 * @param to: offset to write
1415 * @param ops: oob operation description structure
1417 static int onenand_write_oob(struct mtd_info *mtd, loff_t to,
1418 struct mtd_oob_ops *ops)
1420 switch (ops->mode) {
1425 /* Not implemented yet */
1429 return onenand_do_write_oob(mtd, to + ops->ooboffs, ops->ooblen,
1430 &ops->oobretlen, ops->oobbuf, ops->mode);
1434 * onenand_block_checkbad - [GENERIC] Check if a block is marked bad
1435 * @param mtd MTD device structure
1436 * @param ofs offset from device start
1437 * @param getchip 0, if the chip is already selected
1438 * @param allowbbt 1, if its allowed to access the bbt area
1440 * Check, if the block is bad. Either by reading the bad block table or
1441 * calling of the scan function.
1443 static int onenand_block_checkbad(struct mtd_info *mtd, loff_t ofs, int getchip, int allowbbt)
1445 struct onenand_chip *this = mtd->priv;
1446 struct bbm_info *bbm = this->bbm;
1448 /* Return info from the table */
1449 return bbm->isbad_bbt(mtd, ofs, allowbbt);
1453 * onenand_erase - [MTD Interface] erase block(s)
1454 * @param mtd MTD device structure
1455 * @param instr erase instruction
1457 * Erase one ore more blocks
1459 static int onenand_erase(struct mtd_info *mtd, struct erase_info *instr)
1461 struct onenand_chip *this = mtd->priv;
1462 unsigned int block_size;
1467 DEBUG(MTD_DEBUG_LEVEL3, "onenand_erase: start = 0x%08x, len = %i\n", (unsigned int) instr->addr, (unsigned int) instr->len);
1469 block_size = (1 << this->erase_shift);
1471 /* Start address must align on block boundary */
1472 if (unlikely(instr->addr & (block_size - 1))) {
1473 printk(KERN_ERR "onenand_erase: Unaligned address\n");
1477 /* Length must align on block boundary */
1478 if (unlikely(instr->len & (block_size - 1))) {
1479 printk(KERN_ERR "onenand_erase: Length not block aligned\n");
1483 /* Do not allow erase past end of device */
1484 if (unlikely((instr->len + instr->addr) > mtd->size)) {
1485 printk(KERN_ERR "onenand_erase: Erase past end of device\n");
1489 instr->fail_addr = 0xffffffff;
1491 /* Grab the lock and see if the device is available */
1492 onenand_get_device(mtd, FL_ERASING);
1494 /* Loop throught the pages */
1498 instr->state = MTD_ERASING;
1503 /* Check if we have a bad block, we do not erase bad blocks */
1504 if (onenand_block_checkbad(mtd, addr, 0, 0)) {
1505 printk (KERN_WARNING "onenand_erase: attempt to erase a bad block at addr 0x%08x\n", (unsigned int) addr);
1506 instr->state = MTD_ERASE_FAILED;
1510 this->command(mtd, ONENAND_CMD_ERASE, addr, block_size);
1512 onenand_invalidate_bufferram(mtd, addr, block_size);
1514 ret = this->wait(mtd, FL_ERASING);
1515 /* Check, if it is write protected */
1517 printk(KERN_ERR "onenand_erase: Failed erase, block %d\n", (unsigned) (addr >> this->erase_shift));
1518 instr->state = MTD_ERASE_FAILED;
1519 instr->fail_addr = addr;
1527 instr->state = MTD_ERASE_DONE;
1531 ret = instr->state == MTD_ERASE_DONE ? 0 : -EIO;
1532 /* Do call back function */
1534 mtd_erase_callback(instr);
1536 /* Deselect and wake up anyone waiting on the device */
1537 onenand_release_device(mtd);
1543 * onenand_sync - [MTD Interface] sync
1544 * @param mtd MTD device structure
1546 * Sync is actually a wait for chip ready function
1548 static void onenand_sync(struct mtd_info *mtd)
1550 DEBUG(MTD_DEBUG_LEVEL3, "onenand_sync: called\n");
1552 /* Grab the lock and see if the device is available */
1553 onenand_get_device(mtd, FL_SYNCING);
1555 /* Release it and go back */
1556 onenand_release_device(mtd);
1560 * onenand_block_isbad - [MTD Interface] Check whether the block at the given offset is bad
1561 * @param mtd MTD device structure
1562 * @param ofs offset relative to mtd start
1564 * Check whether the block is bad
1566 static int onenand_block_isbad(struct mtd_info *mtd, loff_t ofs)
1568 /* Check for invalid offset */
1569 if (ofs > mtd->size)
1572 return onenand_block_checkbad(mtd, ofs, 1, 0);
1576 * onenand_default_block_markbad - [DEFAULT] mark a block bad
1577 * @param mtd MTD device structure
1578 * @param ofs offset from device start
1580 * This is the default implementation, which can be overridden by
1581 * a hardware specific driver.
1583 static int onenand_default_block_markbad(struct mtd_info *mtd, loff_t ofs)
1585 struct onenand_chip *this = mtd->priv;
1586 struct bbm_info *bbm = this->bbm;
1587 u_char buf[2] = {0, 0};
1591 /* Get block number */
1592 block = ((int) ofs) >> bbm->bbt_erase_shift;
1594 bbm->bbt[block >> 2] |= 0x01 << ((block & 0x03) << 1);
1596 /* We write two bytes, so we dont have to mess with 16 bit access */
1597 ofs += mtd->oobsize + (bbm->badblockpos & ~0x01);
1598 return onenand_do_write_oob(mtd, ofs , 2, &retlen, buf, MTD_OOB_PLACE);
1602 * onenand_block_markbad - [MTD Interface] Mark the block at the given offset as bad
1603 * @param mtd MTD device structure
1604 * @param ofs offset relative to mtd start
1606 * Mark the block as bad
1608 static int onenand_block_markbad(struct mtd_info *mtd, loff_t ofs)
1610 struct onenand_chip *this = mtd->priv;
1613 ret = onenand_block_isbad(mtd, ofs);
1615 /* If it was bad already, return success and do nothing */
1621 return this->block_markbad(mtd, ofs);
1625 * onenand_do_lock_cmd - [OneNAND Interface] Lock or unlock block(s)
1626 * @param mtd MTD device structure
1627 * @param ofs offset relative to mtd start
1628 * @param len number of bytes to lock or unlock
1629 * @param cmd lock or unlock command
1631 * Lock or unlock one or more blocks
1633 static int onenand_do_lock_cmd(struct mtd_info *mtd, loff_t ofs, size_t len, int cmd)
1635 struct onenand_chip *this = mtd->priv;
1636 int start, end, block, value, status;
1639 start = ofs >> this->erase_shift;
1640 end = len >> this->erase_shift;
1642 if (cmd == ONENAND_CMD_LOCK)
1643 wp_status_mask = ONENAND_WP_LS;
1645 wp_status_mask = ONENAND_WP_US;
1647 /* Continuous lock scheme */
1648 if (this->options & ONENAND_HAS_CONT_LOCK) {
1649 /* Set start block address */
1650 this->write_word(start, this->base + ONENAND_REG_START_BLOCK_ADDRESS);
1651 /* Set end block address */
1652 this->write_word(start + end - 1, this->base + ONENAND_REG_END_BLOCK_ADDRESS);
1653 /* Write lock command */
1654 this->command(mtd, cmd, 0, 0);
1656 /* There's no return value */
1657 this->wait(mtd, FL_LOCKING);
1660 while (this->read_word(this->base + ONENAND_REG_CTRL_STATUS)
1661 & ONENAND_CTRL_ONGO)
1664 /* Check lock status */
1665 status = this->read_word(this->base + ONENAND_REG_WP_STATUS);
1666 if (!(status & wp_status_mask))
1667 printk(KERN_ERR "wp status = 0x%x\n", status);
1672 /* Block lock scheme */
1673 for (block = start; block < start + end; block++) {
1674 /* Set block address */
1675 value = onenand_block_address(this, block);
1676 this->write_word(value, this->base + ONENAND_REG_START_ADDRESS1);
1677 /* Select DataRAM for DDP */
1678 value = onenand_bufferram_address(this, block);
1679 this->write_word(value, this->base + ONENAND_REG_START_ADDRESS2);
1680 /* Set start block address */
1681 this->write_word(block, this->base + ONENAND_REG_START_BLOCK_ADDRESS);
1682 /* Write lock command */
1683 this->command(mtd, cmd, 0, 0);
1685 /* There's no return value */
1686 this->wait(mtd, FL_LOCKING);
1689 while (this->read_word(this->base + ONENAND_REG_CTRL_STATUS)
1690 & ONENAND_CTRL_ONGO)
1693 /* Check lock status */
1694 status = this->read_word(this->base + ONENAND_REG_WP_STATUS);
1695 if (!(status & wp_status_mask))
1696 printk(KERN_ERR "block = %d, wp status = 0x%x\n", block, status);
1703 * onenand_lock - [MTD Interface] Lock block(s)
1704 * @param mtd MTD device structure
1705 * @param ofs offset relative to mtd start
1706 * @param len number of bytes to unlock
1708 * Lock one or more blocks
1710 static int onenand_lock(struct mtd_info *mtd, loff_t ofs, size_t len)
1712 return onenand_do_lock_cmd(mtd, ofs, len, ONENAND_CMD_LOCK);
1716 * onenand_unlock - [MTD Interface] Unlock block(s)
1717 * @param mtd MTD device structure
1718 * @param ofs offset relative to mtd start
1719 * @param len number of bytes to unlock
1721 * Unlock one or more blocks
1723 static int onenand_unlock(struct mtd_info *mtd, loff_t ofs, size_t len)
1725 return onenand_do_lock_cmd(mtd, ofs, len, ONENAND_CMD_UNLOCK);
1729 * onenand_check_lock_status - [OneNAND Interface] Check lock status
1730 * @param this onenand chip data structure
1734 static void onenand_check_lock_status(struct onenand_chip *this)
1736 unsigned int value, block, status;
1739 end = this->chipsize >> this->erase_shift;
1740 for (block = 0; block < end; block++) {
1741 /* Set block address */
1742 value = onenand_block_address(this, block);
1743 this->write_word(value, this->base + ONENAND_REG_START_ADDRESS1);
1744 /* Select DataRAM for DDP */
1745 value = onenand_bufferram_address(this, block);
1746 this->write_word(value, this->base + ONENAND_REG_START_ADDRESS2);
1747 /* Set start block address */
1748 this->write_word(block, this->base + ONENAND_REG_START_BLOCK_ADDRESS);
1750 /* Check lock status */
1751 status = this->read_word(this->base + ONENAND_REG_WP_STATUS);
1752 if (!(status & ONENAND_WP_US))
1753 printk(KERN_ERR "block = %d, wp status = 0x%x\n", block, status);
1758 * onenand_unlock_all - [OneNAND Interface] unlock all blocks
1759 * @param mtd MTD device structure
1763 static int onenand_unlock_all(struct mtd_info *mtd)
1765 struct onenand_chip *this = mtd->priv;
1767 if (this->options & ONENAND_HAS_UNLOCK_ALL) {
1768 /* Set start block address */
1769 this->write_word(0, this->base + ONENAND_REG_START_BLOCK_ADDRESS);
1770 /* Write unlock command */
1771 this->command(mtd, ONENAND_CMD_UNLOCK_ALL, 0, 0);
1773 /* There's no return value */
1774 this->wait(mtd, FL_LOCKING);
1777 while (this->read_word(this->base + ONENAND_REG_CTRL_STATUS)
1778 & ONENAND_CTRL_ONGO)
1781 /* Workaround for all block unlock in DDP */
1782 if (ONENAND_IS_DDP(this)) {
1783 /* 1st block on another chip */
1784 loff_t ofs = this->chipsize >> 1;
1785 size_t len = mtd->erasesize;
1787 onenand_unlock(mtd, ofs, len);
1790 onenand_check_lock_status(this);
1795 onenand_unlock(mtd, 0x0, this->chipsize);
1800 #ifdef CONFIG_MTD_ONENAND_OTP
1802 /* Interal OTP operation */
1803 typedef int (*otp_op_t)(struct mtd_info *mtd, loff_t form, size_t len,
1804 size_t *retlen, u_char *buf);
1807 * do_otp_read - [DEFAULT] Read OTP block area
1808 * @param mtd MTD device structure
1809 * @param from The offset to read
1810 * @param len number of bytes to read
1811 * @param retlen pointer to variable to store the number of readbytes
1812 * @param buf the databuffer to put/get data
1814 * Read OTP block area.
1816 static int do_otp_read(struct mtd_info *mtd, loff_t from, size_t len,
1817 size_t *retlen, u_char *buf)
1819 struct onenand_chip *this = mtd->priv;
1822 /* Enter OTP access mode */
1823 this->command(mtd, ONENAND_CMD_OTP_ACCESS, 0, 0);
1824 this->wait(mtd, FL_OTPING);
1826 ret = mtd->read(mtd, from, len, retlen, buf);
1828 /* Exit OTP access mode */
1829 this->command(mtd, ONENAND_CMD_RESET, 0, 0);
1830 this->wait(mtd, FL_RESETING);
1836 * do_otp_write - [DEFAULT] Write OTP block area
1837 * @param mtd MTD device structure
1838 * @param from The offset to write
1839 * @param len number of bytes to write
1840 * @param retlen pointer to variable to store the number of write bytes
1841 * @param buf the databuffer to put/get data
1843 * Write OTP block area.
1845 static int do_otp_write(struct mtd_info *mtd, loff_t from, size_t len,
1846 size_t *retlen, u_char *buf)
1848 struct onenand_chip *this = mtd->priv;
1849 unsigned char *pbuf = buf;
1852 /* Force buffer page aligned */
1853 if (len < mtd->writesize) {
1854 memcpy(this->page_buf, buf, len);
1855 memset(this->page_buf + len, 0xff, mtd->writesize - len);
1856 pbuf = this->page_buf;
1857 len = mtd->writesize;
1860 /* Enter OTP access mode */
1861 this->command(mtd, ONENAND_CMD_OTP_ACCESS, 0, 0);
1862 this->wait(mtd, FL_OTPING);
1864 ret = mtd->write(mtd, from, len, retlen, pbuf);
1866 /* Exit OTP access mode */
1867 this->command(mtd, ONENAND_CMD_RESET, 0, 0);
1868 this->wait(mtd, FL_RESETING);
1874 * do_otp_lock - [DEFAULT] Lock OTP block area
1875 * @param mtd MTD device structure
1876 * @param from The offset to lock
1877 * @param len number of bytes to lock
1878 * @param retlen pointer to variable to store the number of lock bytes
1879 * @param buf the databuffer to put/get data
1881 * Lock OTP block area.
1883 static int do_otp_lock(struct mtd_info *mtd, loff_t from, size_t len,
1884 size_t *retlen, u_char *buf)
1886 struct onenand_chip *this = mtd->priv;
1889 /* Enter OTP access mode */
1890 this->command(mtd, ONENAND_CMD_OTP_ACCESS, 0, 0);
1891 this->wait(mtd, FL_OTPING);
1893 ret = onenand_do_write_oob(mtd, from, len, retlen, buf, MTD_OOB_PLACE);
1895 /* Exit OTP access mode */
1896 this->command(mtd, ONENAND_CMD_RESET, 0, 0);
1897 this->wait(mtd, FL_RESETING);
1903 * onenand_otp_walk - [DEFAULT] Handle OTP operation
1904 * @param mtd MTD device structure
1905 * @param from The offset to read/write
1906 * @param len number of bytes to read/write
1907 * @param retlen pointer to variable to store the number of read bytes
1908 * @param buf the databuffer to put/get data
1909 * @param action do given action
1910 * @param mode specify user and factory
1912 * Handle OTP operation.
1914 static int onenand_otp_walk(struct mtd_info *mtd, loff_t from, size_t len,
1915 size_t *retlen, u_char *buf,
1916 otp_op_t action, int mode)
1918 struct onenand_chip *this = mtd->priv;
1925 density = this->device_id >> ONENAND_DEVICE_DENSITY_SHIFT;
1926 if (density < ONENAND_DEVICE_DENSITY_512Mb)
1931 if (mode == MTD_OTP_FACTORY) {
1932 from += mtd->writesize * otp_pages;
1933 otp_pages = 64 - otp_pages;
1936 /* Check User/Factory boundary */
1937 if (((mtd->writesize * otp_pages) - (from + len)) < 0)
1940 while (len > 0 && otp_pages > 0) {
1941 if (!action) { /* OTP Info functions */
1942 struct otp_info *otpinfo;
1944 len -= sizeof(struct otp_info);
1948 otpinfo = (struct otp_info *) buf;
1949 otpinfo->start = from;
1950 otpinfo->length = mtd->writesize;
1951 otpinfo->locked = 0;
1953 from += mtd->writesize;
1954 buf += sizeof(struct otp_info);
1955 *retlen += sizeof(struct otp_info);
1960 ret = action(mtd, from, len, &tmp_retlen, buf);
1976 * onenand_get_fact_prot_info - [MTD Interface] Read factory OTP info
1977 * @param mtd MTD device structure
1978 * @param buf the databuffer to put/get data
1979 * @param len number of bytes to read
1981 * Read factory OTP info.
1983 static int onenand_get_fact_prot_info(struct mtd_info *mtd,
1984 struct otp_info *buf, size_t len)
1989 ret = onenand_otp_walk(mtd, 0, len, &retlen, (u_char *) buf, NULL, MTD_OTP_FACTORY);
1991 return ret ? : retlen;
1995 * onenand_read_fact_prot_reg - [MTD Interface] Read factory OTP area
1996 * @param mtd MTD device structure
1997 * @param from The offset to read
1998 * @param len number of bytes to read
1999 * @param retlen pointer to variable to store the number of read bytes
2000 * @param buf the databuffer to put/get data
2002 * Read factory OTP area.
2004 static int onenand_read_fact_prot_reg(struct mtd_info *mtd, loff_t from,
2005 size_t len, size_t *retlen, u_char *buf)
2007 return onenand_otp_walk(mtd, from, len, retlen, buf, do_otp_read, MTD_OTP_FACTORY);
2011 * onenand_get_user_prot_info - [MTD Interface] Read user OTP info
2012 * @param mtd MTD device structure
2013 * @param buf the databuffer to put/get data
2014 * @param len number of bytes to read
2016 * Read user OTP info.
2018 static int onenand_get_user_prot_info(struct mtd_info *mtd,
2019 struct otp_info *buf, size_t len)
2024 ret = onenand_otp_walk(mtd, 0, len, &retlen, (u_char *) buf, NULL, MTD_OTP_USER);
2026 return ret ? : retlen;
2030 * onenand_read_user_prot_reg - [MTD Interface] Read user OTP area
2031 * @param mtd MTD device structure
2032 * @param from The offset to read
2033 * @param len number of bytes to read
2034 * @param retlen pointer to variable to store the number of read bytes
2035 * @param buf the databuffer to put/get data
2037 * Read user OTP area.
2039 static int onenand_read_user_prot_reg(struct mtd_info *mtd, loff_t from,
2040 size_t len, size_t *retlen, u_char *buf)
2042 return onenand_otp_walk(mtd, from, len, retlen, buf, do_otp_read, MTD_OTP_USER);
2046 * onenand_write_user_prot_reg - [MTD Interface] Write user OTP area
2047 * @param mtd MTD device structure
2048 * @param from The offset to write
2049 * @param len number of bytes to write
2050 * @param retlen pointer to variable to store the number of write bytes
2051 * @param buf the databuffer to put/get data
2053 * Write user OTP area.
2055 static int onenand_write_user_prot_reg(struct mtd_info *mtd, loff_t from,
2056 size_t len, size_t *retlen, u_char *buf)
2058 return onenand_otp_walk(mtd, from, len, retlen, buf, do_otp_write, MTD_OTP_USER);
2062 * onenand_lock_user_prot_reg - [MTD Interface] Lock user OTP area
2063 * @param mtd MTD device structure
2064 * @param from The offset to lock
2065 * @param len number of bytes to unlock
2067 * Write lock mark on spare area in page 0 in OTP block
2069 static int onenand_lock_user_prot_reg(struct mtd_info *mtd, loff_t from,
2072 unsigned char oob_buf[64];
2076 memset(oob_buf, 0xff, mtd->oobsize);
2078 * Note: OTP lock operation
2079 * OTP block : 0xXXFC
2080 * 1st block : 0xXXF3 (If chip support)
2081 * Both : 0xXXF0 (If chip support)
2083 oob_buf[ONENAND_OTP_LOCK_OFFSET] = 0xFC;
2086 * Write lock mark to 8th word of sector0 of page0 of the spare0.
2087 * We write 16 bytes spare area instead of 2 bytes.
2092 ret = onenand_otp_walk(mtd, from, len, &retlen, oob_buf, do_otp_lock, MTD_OTP_USER);
2094 return ret ? : retlen;
2096 #endif /* CONFIG_MTD_ONENAND_OTP */
2099 * onenand_check_features - Check and set OneNAND features
2100 * @param mtd MTD data structure
2102 * Check and set OneNAND features
2105 static void onenand_check_features(struct mtd_info *mtd)
2107 struct onenand_chip *this = mtd->priv;
2108 unsigned int density, process;
2110 /* Lock scheme depends on density and process */
2111 density = this->device_id >> ONENAND_DEVICE_DENSITY_SHIFT;
2112 process = this->version_id >> ONENAND_VERSION_PROCESS_SHIFT;
2115 if (density >= ONENAND_DEVICE_DENSITY_1Gb) {
2116 /* A-Die has all block unlock */
2118 printk(KERN_DEBUG "Chip support all block unlock\n");
2119 this->options |= ONENAND_HAS_UNLOCK_ALL;
2122 /* Some OneNAND has continues lock scheme */
2124 printk(KERN_DEBUG "Lock scheme is Continues Lock\n");
2125 this->options |= ONENAND_HAS_CONT_LOCK;
2131 * onenand_print_device_info - Print device & version ID
2132 * @param device device ID
2133 * @param version version ID
2135 * Print device & version ID
2137 static void onenand_print_device_info(int device, int version)
2139 int vcc, demuxed, ddp, density;
2141 vcc = device & ONENAND_DEVICE_VCC_MASK;
2142 demuxed = device & ONENAND_DEVICE_IS_DEMUX;
2143 ddp = device & ONENAND_DEVICE_IS_DDP;
2144 density = device >> ONENAND_DEVICE_DENSITY_SHIFT;
2145 printk(KERN_INFO "%sOneNAND%s %dMB %sV 16-bit (0x%02x)\n",
2146 demuxed ? "" : "Muxed ",
2149 vcc ? "2.65/3.3" : "1.8",
2151 printk(KERN_DEBUG "OneNAND version = 0x%04x\n", version);
2154 static const struct onenand_manufacturers onenand_manuf_ids[] = {
2155 {ONENAND_MFR_SAMSUNG, "Samsung"},
2159 * onenand_check_maf - Check manufacturer ID
2160 * @param manuf manufacturer ID
2162 * Check manufacturer ID
2164 static int onenand_check_maf(int manuf)
2166 int size = ARRAY_SIZE(onenand_manuf_ids);
2170 for (i = 0; i < size; i++)
2171 if (manuf == onenand_manuf_ids[i].id)
2175 name = onenand_manuf_ids[i].name;
2179 printk(KERN_DEBUG "OneNAND Manufacturer: %s (0x%0x)\n", name, manuf);
2185 * onenand_probe - [OneNAND Interface] Probe the OneNAND device
2186 * @param mtd MTD device structure
2188 * OneNAND detection method:
2189 * Compare the the values from command with ones from register
2191 static int onenand_probe(struct mtd_info *mtd)
2193 struct onenand_chip *this = mtd->priv;
2194 int bram_maf_id, bram_dev_id, maf_id, dev_id, ver_id;
2198 /* Save system configuration 1 */
2199 syscfg = this->read_word(this->base + ONENAND_REG_SYS_CFG1);
2200 /* Clear Sync. Burst Read mode to read BootRAM */
2201 this->write_word((syscfg & ~ONENAND_SYS_CFG1_SYNC_READ), this->base + ONENAND_REG_SYS_CFG1);
2203 /* Send the command for reading device ID from BootRAM */
2204 this->write_word(ONENAND_CMD_READID, this->base + ONENAND_BOOTRAM);
2206 /* Read manufacturer and device IDs from BootRAM */
2207 bram_maf_id = this->read_word(this->base + ONENAND_BOOTRAM + 0x0);
2208 bram_dev_id = this->read_word(this->base + ONENAND_BOOTRAM + 0x2);
2210 /* Reset OneNAND to read default register values */
2211 this->write_word(ONENAND_CMD_RESET, this->base + ONENAND_BOOTRAM);
2213 this->wait(mtd, FL_RESETING);
2215 /* Restore system configuration 1 */
2216 this->write_word(syscfg, this->base + ONENAND_REG_SYS_CFG1);
2218 /* Check manufacturer ID */
2219 if (onenand_check_maf(bram_maf_id))
2222 /* Read manufacturer and device IDs from Register */
2223 maf_id = this->read_word(this->base + ONENAND_REG_MANUFACTURER_ID);
2224 dev_id = this->read_word(this->base + ONENAND_REG_DEVICE_ID);
2225 ver_id = this->read_word(this->base + ONENAND_REG_VERSION_ID);
2227 /* Check OneNAND device */
2228 if (maf_id != bram_maf_id || dev_id != bram_dev_id)
2231 /* Flash device information */
2232 onenand_print_device_info(dev_id, ver_id);
2233 this->device_id = dev_id;
2234 this->version_id = ver_id;
2236 density = dev_id >> ONENAND_DEVICE_DENSITY_SHIFT;
2237 this->chipsize = (16 << density) << 20;
2238 /* Set density mask. it is used for DDP */
2239 if (ONENAND_IS_DDP(this))
2240 this->density_mask = (1 << (density + 6));
2242 this->density_mask = 0;
2244 /* OneNAND page size & block size */
2245 /* The data buffer size is equal to page size */
2246 mtd->writesize = this->read_word(this->base + ONENAND_REG_DATA_BUFFER_SIZE);
2247 mtd->oobsize = mtd->writesize >> 5;
2248 /* Pages per a block are always 64 in OneNAND */
2249 mtd->erasesize = mtd->writesize << 6;
2251 this->erase_shift = ffs(mtd->erasesize) - 1;
2252 this->page_shift = ffs(mtd->writesize) - 1;
2253 this->page_mask = (1 << (this->erase_shift - this->page_shift)) - 1;
2255 /* REVIST: Multichip handling */
2257 mtd->size = this->chipsize;
2259 /* Check OneNAND features */
2260 onenand_check_features(mtd);
2266 * onenand_suspend - [MTD Interface] Suspend the OneNAND flash
2267 * @param mtd MTD device structure
2269 static int onenand_suspend(struct mtd_info *mtd)
2271 return onenand_get_device(mtd, FL_PM_SUSPENDED);
2275 * onenand_resume - [MTD Interface] Resume the OneNAND flash
2276 * @param mtd MTD device structure
2278 static void onenand_resume(struct mtd_info *mtd)
2280 struct onenand_chip *this = mtd->priv;
2282 if (this->state == FL_PM_SUSPENDED)
2283 onenand_release_device(mtd);
2285 printk(KERN_ERR "resume() called for the chip which is not"
2286 "in suspended state\n");
2290 * onenand_scan - [OneNAND Interface] Scan for the OneNAND device
2291 * @param mtd MTD device structure
2292 * @param maxchips Number of chips to scan for
2294 * This fills out all the not initialized function pointers
2295 * with the defaults.
2296 * The flash ID is read and the mtd/chip structures are
2297 * filled with the appropriate values.
2299 int onenand_scan(struct mtd_info *mtd, int maxchips)
2302 struct onenand_chip *this = mtd->priv;
2304 if (!this->read_word)
2305 this->read_word = onenand_readw;
2306 if (!this->write_word)
2307 this->write_word = onenand_writew;
2310 this->command = onenand_command;
2312 onenand_setup_wait(mtd);
2314 if (!this->read_bufferram)
2315 this->read_bufferram = onenand_read_bufferram;
2316 if (!this->write_bufferram)
2317 this->write_bufferram = onenand_write_bufferram;
2319 if (!this->block_markbad)
2320 this->block_markbad = onenand_default_block_markbad;
2321 if (!this->scan_bbt)
2322 this->scan_bbt = onenand_default_bbt;
2324 if (onenand_probe(mtd))
2327 /* Set Sync. Burst Read after probing */
2328 if (this->mmcontrol) {
2329 printk(KERN_INFO "OneNAND Sync. Burst Read support\n");
2330 this->read_bufferram = onenand_sync_read_bufferram;
2333 /* Allocate buffers, if necessary */
2334 if (!this->page_buf) {
2335 this->page_buf = kzalloc(mtd->writesize, GFP_KERNEL);
2336 if (!this->page_buf) {
2337 printk(KERN_ERR "onenand_scan(): Can't allocate page_buf\n");
2340 this->options |= ONENAND_PAGEBUF_ALLOC;
2342 if (!this->oob_buf) {
2343 this->oob_buf = kzalloc(mtd->oobsize, GFP_KERNEL);
2344 if (!this->oob_buf) {
2345 printk(KERN_ERR "onenand_scan(): Can't allocate oob_buf\n");
2346 if (this->options & ONENAND_PAGEBUF_ALLOC) {
2347 this->options &= ~ONENAND_PAGEBUF_ALLOC;
2348 kfree(this->page_buf);
2352 this->options |= ONENAND_OOBBUF_ALLOC;
2355 this->state = FL_READY;
2356 init_waitqueue_head(&this->wq);
2357 spin_lock_init(&this->chip_lock);
2360 * Allow subpage writes up to oobsize.
2362 switch (mtd->oobsize) {
2364 this->ecclayout = &onenand_oob_64;
2365 mtd->subpage_sft = 2;
2369 this->ecclayout = &onenand_oob_32;
2370 mtd->subpage_sft = 1;
2374 printk(KERN_WARNING "No OOB scheme defined for oobsize %d\n",
2376 mtd->subpage_sft = 0;
2377 /* To prevent kernel oops */
2378 this->ecclayout = &onenand_oob_32;
2382 this->subpagesize = mtd->writesize >> mtd->subpage_sft;
2385 * The number of bytes available for a client to place data into
2386 * the out of band area
2388 this->ecclayout->oobavail = 0;
2389 for (i = 0; this->ecclayout->oobfree[i].length; i++)
2390 this->ecclayout->oobavail +=
2391 this->ecclayout->oobfree[i].length;
2392 mtd->oobavail = this->ecclayout->oobavail;
2394 mtd->ecclayout = this->ecclayout;
2396 /* Fill in remaining MTD driver data */
2397 mtd->type = MTD_NANDFLASH;
2398 mtd->flags = MTD_CAP_NANDFLASH;
2399 mtd->erase = onenand_erase;
2401 mtd->unpoint = NULL;
2402 mtd->read = onenand_read;
2403 mtd->write = onenand_write;
2404 mtd->read_oob = onenand_read_oob;
2405 mtd->write_oob = onenand_write_oob;
2406 #ifdef CONFIG_MTD_ONENAND_OTP
2407 mtd->get_fact_prot_info = onenand_get_fact_prot_info;
2408 mtd->read_fact_prot_reg = onenand_read_fact_prot_reg;
2409 mtd->get_user_prot_info = onenand_get_user_prot_info;
2410 mtd->read_user_prot_reg = onenand_read_user_prot_reg;
2411 mtd->write_user_prot_reg = onenand_write_user_prot_reg;
2412 mtd->lock_user_prot_reg = onenand_lock_user_prot_reg;
2414 mtd->sync = onenand_sync;
2415 mtd->lock = onenand_lock;
2416 mtd->unlock = onenand_unlock;
2417 mtd->suspend = onenand_suspend;
2418 mtd->resume = onenand_resume;
2419 mtd->block_isbad = onenand_block_isbad;
2420 mtd->block_markbad = onenand_block_markbad;
2421 mtd->owner = THIS_MODULE;
2423 /* Unlock whole block */
2424 onenand_unlock_all(mtd);
2426 return this->scan_bbt(mtd);
2430 * onenand_release - [OneNAND Interface] Free resources held by the OneNAND device
2431 * @param mtd MTD device structure
2433 void onenand_release(struct mtd_info *mtd)
2435 struct onenand_chip *this = mtd->priv;
2437 #ifdef CONFIG_MTD_PARTITIONS
2438 /* Deregister partitions */
2439 del_mtd_partitions (mtd);
2441 /* Deregister the device */
2442 del_mtd_device (mtd);
2444 /* Free bad block table memory, if allocated */
2446 struct bbm_info *bbm = this->bbm;
2450 /* Buffers allocated by onenand_scan */
2451 if (this->options & ONENAND_PAGEBUF_ALLOC)
2452 kfree(this->page_buf);
2453 if (this->options & ONENAND_OOBBUF_ALLOC)
2454 kfree(this->oob_buf);
2457 EXPORT_SYMBOL_GPL(onenand_scan);
2458 EXPORT_SYMBOL_GPL(onenand_release);
2460 MODULE_LICENSE("GPL");
2461 MODULE_AUTHOR("Kyungmin Park <kyungmin.park@samsung.com>");
2462 MODULE_DESCRIPTION("Generic OneNAND flash driver code");