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 if (ecc & ONENAND_ECC_2BIT_ALL) {
337 printk(KERN_ERR "onenand_wait: ECC error = 0x%04x\n", ecc);
338 mtd->ecc_stats.failed++;
340 } else if (ecc & ONENAND_ECC_1BIT_ALL) {
341 printk(KERN_INFO "onenand_wait: correctable ECC error = 0x%04x\n", ecc);
342 mtd->ecc_stats.corrected++;
345 } else if (state == FL_READING) {
346 printk(KERN_ERR "onenand_wait: read timeout! ctrl=0x%04x intr=0x%04x\n", ctrl, interrupt);
354 * onenand_interrupt - [DEFAULT] onenand interrupt handler
355 * @param irq onenand interrupt number
356 * @param dev_id interrupt data
360 static irqreturn_t onenand_interrupt(int irq, void *data)
362 struct onenand_chip *this = data;
364 /* To handle shared interrupt */
365 if (!this->complete.done)
366 complete(&this->complete);
372 * onenand_interrupt_wait - [DEFAULT] wait until the command is done
373 * @param mtd MTD device structure
374 * @param state state to select the max. timeout value
376 * Wait for command done.
378 static int onenand_interrupt_wait(struct mtd_info *mtd, int state)
380 struct onenand_chip *this = mtd->priv;
382 wait_for_completion(&this->complete);
384 return onenand_wait(mtd, state);
388 * onenand_try_interrupt_wait - [DEFAULT] try interrupt wait
389 * @param mtd MTD device structure
390 * @param state state to select the max. timeout value
392 * Try interrupt based wait (It is used one-time)
394 static int onenand_try_interrupt_wait(struct mtd_info *mtd, int state)
396 struct onenand_chip *this = mtd->priv;
397 unsigned long remain, timeout;
399 /* We use interrupt wait first */
400 this->wait = onenand_interrupt_wait;
402 timeout = msecs_to_jiffies(100);
403 remain = wait_for_completion_timeout(&this->complete, timeout);
405 printk(KERN_INFO "OneNAND: There's no interrupt. "
406 "We use the normal wait\n");
408 /* Release the irq */
409 free_irq(this->irq, this);
411 this->wait = onenand_wait;
414 return onenand_wait(mtd, state);
418 * onenand_setup_wait - [OneNAND Interface] setup onenand wait method
419 * @param mtd MTD device structure
421 * There's two method to wait onenand work
422 * 1. polling - read interrupt status register
423 * 2. interrupt - use the kernel interrupt method
425 static void onenand_setup_wait(struct mtd_info *mtd)
427 struct onenand_chip *this = mtd->priv;
430 init_completion(&this->complete);
432 if (this->irq <= 0) {
433 this->wait = onenand_wait;
437 if (request_irq(this->irq, &onenand_interrupt,
438 IRQF_SHARED, "onenand", this)) {
439 /* If we can't get irq, use the normal wait */
440 this->wait = onenand_wait;
444 /* Enable interrupt */
445 syscfg = this->read_word(this->base + ONENAND_REG_SYS_CFG1);
446 syscfg |= ONENAND_SYS_CFG1_IOBE;
447 this->write_word(syscfg, this->base + ONENAND_REG_SYS_CFG1);
449 this->wait = onenand_try_interrupt_wait;
453 * onenand_bufferram_offset - [DEFAULT] BufferRAM offset
454 * @param mtd MTD data structure
455 * @param area BufferRAM area
456 * @return offset given area
458 * Return BufferRAM offset given area
460 static inline int onenand_bufferram_offset(struct mtd_info *mtd, int area)
462 struct onenand_chip *this = mtd->priv;
464 if (ONENAND_CURRENT_BUFFERRAM(this)) {
465 /* Note: the 'this->writesize' is a real page size */
466 if (area == ONENAND_DATARAM)
467 return this->writesize;
468 if (area == ONENAND_SPARERAM)
476 * onenand_read_bufferram - [OneNAND Interface] Read the bufferram area
477 * @param mtd MTD data structure
478 * @param area BufferRAM area
479 * @param buffer the databuffer to put/get data
480 * @param offset offset to read from or write to
481 * @param count number of bytes to read/write
483 * Read the BufferRAM area
485 static int onenand_read_bufferram(struct mtd_info *mtd, int area,
486 unsigned char *buffer, int offset, size_t count)
488 struct onenand_chip *this = mtd->priv;
489 void __iomem *bufferram;
491 bufferram = this->base + area;
493 bufferram += onenand_bufferram_offset(mtd, area);
495 if (ONENAND_CHECK_BYTE_ACCESS(count)) {
498 /* Align with word(16-bit) size */
501 /* Read word and save byte */
502 word = this->read_word(bufferram + offset + count);
503 buffer[count] = (word & 0xff);
506 memcpy(buffer, bufferram + offset, count);
512 * onenand_sync_read_bufferram - [OneNAND Interface] Read the bufferram area with Sync. Burst mode
513 * @param mtd MTD data structure
514 * @param area BufferRAM area
515 * @param buffer the databuffer to put/get data
516 * @param offset offset to read from or write to
517 * @param count number of bytes to read/write
519 * Read the BufferRAM area with Sync. Burst Mode
521 static int onenand_sync_read_bufferram(struct mtd_info *mtd, int area,
522 unsigned char *buffer, int offset, size_t count)
524 struct onenand_chip *this = mtd->priv;
525 void __iomem *bufferram;
527 bufferram = this->base + area;
529 bufferram += onenand_bufferram_offset(mtd, area);
531 this->mmcontrol(mtd, ONENAND_SYS_CFG1_SYNC_READ);
533 if (ONENAND_CHECK_BYTE_ACCESS(count)) {
536 /* Align with word(16-bit) size */
539 /* Read word and save byte */
540 word = this->read_word(bufferram + offset + count);
541 buffer[count] = (word & 0xff);
544 memcpy(buffer, bufferram + offset, count);
546 this->mmcontrol(mtd, 0);
552 * onenand_write_bufferram - [OneNAND Interface] Write the bufferram area
553 * @param mtd MTD data structure
554 * @param area BufferRAM area
555 * @param buffer the databuffer to put/get data
556 * @param offset offset to read from or write to
557 * @param count number of bytes to read/write
559 * Write the BufferRAM area
561 static int onenand_write_bufferram(struct mtd_info *mtd, int area,
562 const unsigned char *buffer, int offset, size_t count)
564 struct onenand_chip *this = mtd->priv;
565 void __iomem *bufferram;
567 bufferram = this->base + area;
569 bufferram += onenand_bufferram_offset(mtd, area);
571 if (ONENAND_CHECK_BYTE_ACCESS(count)) {
575 /* Align with word(16-bit) size */
578 /* Calculate byte access offset */
579 byte_offset = offset + count;
581 /* Read word and save byte */
582 word = this->read_word(bufferram + byte_offset);
583 word = (word & ~0xff) | buffer[count];
584 this->write_word(word, bufferram + byte_offset);
587 memcpy(bufferram + offset, buffer, count);
593 * onenand_get_2x_blockpage - [GENERIC] Get blockpage at 2x program mode
594 * @param mtd MTD data structure
595 * @param addr address to check
596 * @return blockpage address
598 * Get blockpage address at 2x program mode
600 static int onenand_get_2x_blockpage(struct mtd_info *mtd, loff_t addr)
602 struct onenand_chip *this = mtd->priv;
603 int blockpage, block, page;
605 /* Calculate the even block number */
606 block = (int) (addr >> this->erase_shift) & ~1;
607 /* Is it the odd plane? */
608 if (addr & this->writesize)
610 page = (int) (addr >> (this->page_shift + 1)) & this->page_mask;
611 blockpage = (block << 7) | page;
617 * onenand_check_bufferram - [GENERIC] Check BufferRAM information
618 * @param mtd MTD data structure
619 * @param addr address to check
620 * @return 1 if there are valid data, otherwise 0
622 * Check bufferram if there is data we required
624 static int onenand_check_bufferram(struct mtd_info *mtd, loff_t addr)
626 struct onenand_chip *this = mtd->priv;
627 int blockpage, found = 0;
630 if (ONENAND_IS_2PLANE(this))
631 blockpage = onenand_get_2x_blockpage(mtd, addr);
633 blockpage = (int) (addr >> this->page_shift);
635 /* Is there valid data? */
636 i = ONENAND_CURRENT_BUFFERRAM(this);
637 if (this->bufferram[i].blockpage == blockpage)
640 /* Check another BufferRAM */
641 i = ONENAND_NEXT_BUFFERRAM(this);
642 if (this->bufferram[i].blockpage == blockpage) {
643 ONENAND_SET_NEXT_BUFFERRAM(this);
648 if (found && ONENAND_IS_DDP(this)) {
649 /* Select DataRAM for DDP */
650 int block = (int) (addr >> this->erase_shift);
651 int value = onenand_bufferram_address(this, block);
652 this->write_word(value, this->base + ONENAND_REG_START_ADDRESS2);
659 * onenand_update_bufferram - [GENERIC] Update BufferRAM information
660 * @param mtd MTD data structure
661 * @param addr address to update
662 * @param valid valid flag
664 * Update BufferRAM information
666 static void onenand_update_bufferram(struct mtd_info *mtd, loff_t addr,
669 struct onenand_chip *this = mtd->priv;
673 if (ONENAND_IS_2PLANE(this))
674 blockpage = onenand_get_2x_blockpage(mtd, addr);
676 blockpage = (int) (addr >> this->page_shift);
678 /* Invalidate another BufferRAM */
679 i = ONENAND_NEXT_BUFFERRAM(this);
680 if (this->bufferram[i].blockpage == blockpage)
681 this->bufferram[i].blockpage = -1;
683 /* Update BufferRAM */
684 i = ONENAND_CURRENT_BUFFERRAM(this);
686 this->bufferram[i].blockpage = blockpage;
688 this->bufferram[i].blockpage = -1;
692 * onenand_invalidate_bufferram - [GENERIC] Invalidate BufferRAM information
693 * @param mtd MTD data structure
694 * @param addr start address to invalidate
695 * @param len length to invalidate
697 * Invalidate BufferRAM information
699 static void onenand_invalidate_bufferram(struct mtd_info *mtd, loff_t addr,
702 struct onenand_chip *this = mtd->priv;
704 loff_t end_addr = addr + len;
706 /* Invalidate BufferRAM */
707 for (i = 0; i < MAX_BUFFERRAM; i++) {
708 loff_t buf_addr = this->bufferram[i].blockpage << this->page_shift;
709 if (buf_addr >= addr && buf_addr < end_addr)
710 this->bufferram[i].blockpage = -1;
715 * onenand_get_device - [GENERIC] Get chip for selected access
716 * @param mtd MTD device structure
717 * @param new_state the state which is requested
719 * Get the device and lock it for exclusive access
721 static int onenand_get_device(struct mtd_info *mtd, int new_state)
723 struct onenand_chip *this = mtd->priv;
724 DECLARE_WAITQUEUE(wait, current);
727 * Grab the lock and see if the device is available
730 spin_lock(&this->chip_lock);
731 if (this->state == FL_READY) {
732 this->state = new_state;
733 spin_unlock(&this->chip_lock);
736 if (new_state == FL_PM_SUSPENDED) {
737 spin_unlock(&this->chip_lock);
738 return (this->state == FL_PM_SUSPENDED) ? 0 : -EAGAIN;
740 set_current_state(TASK_UNINTERRUPTIBLE);
741 add_wait_queue(&this->wq, &wait);
742 spin_unlock(&this->chip_lock);
744 remove_wait_queue(&this->wq, &wait);
751 * onenand_release_device - [GENERIC] release chip
752 * @param mtd MTD device structure
754 * Deselect, release chip lock and wake up anyone waiting on the device
756 static void onenand_release_device(struct mtd_info *mtd)
758 struct onenand_chip *this = mtd->priv;
760 /* Release the chip */
761 spin_lock(&this->chip_lock);
762 this->state = FL_READY;
764 spin_unlock(&this->chip_lock);
768 * onenand_transfer_auto_oob - [Internal] oob auto-placement transfer
769 * @param mtd MTD device structure
770 * @param buf destination address
771 * @param column oob offset to read from
772 * @param thislen oob length to read
774 static int onenand_transfer_auto_oob(struct mtd_info *mtd, uint8_t *buf, int column,
777 struct onenand_chip *this = mtd->priv;
778 struct nand_oobfree *free;
779 int readcol = column;
780 int readend = column + thislen;
783 uint8_t *oob_buf = this->oob_buf;
785 free = this->ecclayout->oobfree;
786 for (i = 0; i < MTD_MAX_OOBFREE_ENTRIES && free->length; i++, free++) {
787 if (readcol >= lastgap)
788 readcol += free->offset - lastgap;
789 if (readend >= lastgap)
790 readend += free->offset - lastgap;
791 lastgap = free->offset + free->length;
793 this->read_bufferram(mtd, ONENAND_SPARERAM, oob_buf, 0, mtd->oobsize);
794 free = this->ecclayout->oobfree;
795 for (i = 0; i < MTD_MAX_OOBFREE_ENTRIES && free->length; i++, free++) {
796 int free_end = free->offset + free->length;
797 if (free->offset < readend && free_end > readcol) {
798 int st = max_t(int,free->offset,readcol);
799 int ed = min_t(int,free_end,readend);
801 memcpy(buf, oob_buf + st, n);
803 } else if (column == 0)
810 * onenand_read_ops_nolock - [OneNAND Interface] OneNAND read main and/or out-of-band
811 * @param mtd MTD device structure
812 * @param from offset to read from
813 * @param ops: oob operation description structure
815 * OneNAND read main and/or out-of-band data
817 static int onenand_read_ops_nolock(struct mtd_info *mtd, loff_t from,
818 struct mtd_oob_ops *ops)
820 struct onenand_chip *this = mtd->priv;
821 struct mtd_ecc_stats stats;
822 size_t len = ops->len;
823 size_t ooblen = ops->ooblen;
824 u_char *buf = ops->datbuf;
825 u_char *oobbuf = ops->oobbuf;
826 int read = 0, column, thislen;
827 int oobread = 0, oobcolumn, thisooblen, oobsize;
828 int ret = 0, boundary = 0;
829 int writesize = this->writesize;
831 DEBUG(MTD_DEBUG_LEVEL3, "onenand_read_ops_nolock: from = 0x%08x, len = %i\n", (unsigned int) from, (int) len);
833 if (ops->mode == MTD_OOB_AUTO)
834 oobsize = this->ecclayout->oobavail;
836 oobsize = mtd->oobsize;
838 oobcolumn = from & (mtd->oobsize - 1);
840 /* Do not allow reads past end of device */
841 if ((from + len) > mtd->size) {
842 printk(KERN_ERR "onenand_read_ops_nolock: Attempt read beyond end of device\n");
848 stats = mtd->ecc_stats;
850 /* Read-while-load method */
852 /* Do first load to bufferRAM */
854 if (!onenand_check_bufferram(mtd, from)) {
855 this->command(mtd, ONENAND_CMD_READ, from, writesize);
856 ret = this->wait(mtd, FL_READING);
857 onenand_update_bufferram(mtd, from, !ret);
863 thislen = min_t(int, writesize, len - read);
864 column = from & (writesize - 1);
865 if (column + thislen > writesize)
866 thislen = writesize - column;
869 /* If there is more to load then start next load */
871 if (read + thislen < len) {
872 this->command(mtd, ONENAND_CMD_READ, from, writesize);
874 * Chip boundary handling in DDP
875 * Now we issued chip 1 read and pointed chip 1
876 * bufferam so we have to point chip 0 bufferam.
878 if (ONENAND_IS_DDP(this) &&
879 unlikely(from == (this->chipsize >> 1))) {
880 this->write_word(ONENAND_DDP_CHIP0, this->base + ONENAND_REG_START_ADDRESS2);
884 ONENAND_SET_PREV_BUFFERRAM(this);
886 /* While load is going, read from last bufferRAM */
887 this->read_bufferram(mtd, ONENAND_DATARAM, buf, column, thislen);
889 /* Read oob area if needed */
891 thisooblen = oobsize - oobcolumn;
892 thisooblen = min_t(int, thisooblen, ooblen - oobread);
894 if (ops->mode == MTD_OOB_AUTO)
895 onenand_transfer_auto_oob(mtd, oobbuf, oobcolumn, thisooblen);
897 this->read_bufferram(mtd, ONENAND_SPARERAM, oobbuf, oobcolumn, thisooblen);
898 oobread += thisooblen;
899 oobbuf += thisooblen;
903 /* See if we are done */
907 /* Set up for next read from bufferRAM */
908 if (unlikely(boundary))
909 this->write_word(ONENAND_DDP_CHIP1, this->base + ONENAND_REG_START_ADDRESS2);
910 ONENAND_SET_NEXT_BUFFERRAM(this);
912 thislen = min_t(int, writesize, len - read);
915 /* Now wait for load */
916 ret = this->wait(mtd, FL_READING);
917 onenand_update_bufferram(mtd, from, !ret);
923 * Return success, if no ECC failures, else -EBADMSG
924 * fs driver will take care of that, because
925 * retlen == desired len and result == -EBADMSG
928 ops->oobretlen = oobread;
933 if (mtd->ecc_stats.failed - stats.failed)
936 return mtd->ecc_stats.corrected - stats.corrected ? -EUCLEAN : 0;
940 * onenand_read_oob_nolock - [MTD Interface] OneNAND read out-of-band
941 * @param mtd MTD device structure
942 * @param from offset to read from
943 * @param ops: oob operation description structure
945 * OneNAND read out-of-band data from the spare area
947 static int onenand_read_oob_nolock(struct mtd_info *mtd, loff_t from,
948 struct mtd_oob_ops *ops)
950 struct onenand_chip *this = mtd->priv;
951 struct mtd_ecc_stats stats;
952 int read = 0, thislen, column, oobsize;
953 size_t len = ops->ooblen;
954 mtd_oob_mode_t mode = ops->mode;
955 u_char *buf = ops->oobbuf;
958 from += ops->ooboffs;
960 DEBUG(MTD_DEBUG_LEVEL3, "onenand_read_oob_nolock: from = 0x%08x, len = %i\n", (unsigned int) from, (int) len);
962 /* Initialize return length value */
965 if (mode == MTD_OOB_AUTO)
966 oobsize = this->ecclayout->oobavail;
968 oobsize = mtd->oobsize;
970 column = from & (mtd->oobsize - 1);
972 if (unlikely(column >= oobsize)) {
973 printk(KERN_ERR "onenand_read_oob_nolock: Attempted to start read outside oob\n");
977 /* Do not allow reads past end of device */
978 if (unlikely(from >= mtd->size ||
979 column + len > ((mtd->size >> this->page_shift) -
980 (from >> this->page_shift)) * oobsize)) {
981 printk(KERN_ERR "onenand_read_oob_nolock: Attempted to read beyond end of device\n");
985 stats = mtd->ecc_stats;
990 thislen = oobsize - column;
991 thislen = min_t(int, thislen, len);
993 this->command(mtd, ONENAND_CMD_READOOB, from, mtd->oobsize);
995 onenand_update_bufferram(mtd, from, 0);
997 ret = this->wait(mtd, FL_READING);
998 if (ret && ret != -EBADMSG) {
999 printk(KERN_ERR "onenand_read_oob_nolock: read failed = 0x%x\n", ret);
1003 if (mode == MTD_OOB_AUTO)
1004 onenand_transfer_auto_oob(mtd, buf, column, thislen);
1006 this->read_bufferram(mtd, ONENAND_SPARERAM, buf, column, thislen);
1018 from += mtd->writesize;
1023 ops->oobretlen = read;
1028 if (mtd->ecc_stats.failed - stats.failed)
1035 * onenand_read - [MTD Interface] Read data from flash
1036 * @param mtd MTD device structure
1037 * @param from offset to read from
1038 * @param len number of bytes to read
1039 * @param retlen pointer to variable to store the number of read bytes
1040 * @param buf the databuffer to put data
1044 static int onenand_read(struct mtd_info *mtd, loff_t from, size_t len,
1045 size_t *retlen, u_char *buf)
1047 struct mtd_oob_ops ops = {
1055 onenand_get_device(mtd, FL_READING);
1056 ret = onenand_read_ops_nolock(mtd, from, &ops);
1057 onenand_release_device(mtd);
1059 *retlen = ops.retlen;
1064 * onenand_read_oob - [MTD Interface] Read main and/or out-of-band
1065 * @param mtd: MTD device structure
1066 * @param from: offset to read from
1067 * @param ops: oob operation description structure
1069 * Read main and/or out-of-band
1071 static int onenand_read_oob(struct mtd_info *mtd, loff_t from,
1072 struct mtd_oob_ops *ops)
1076 switch (ops->mode) {
1081 /* Not implemented yet */
1086 onenand_get_device(mtd, FL_READING);
1088 ret = onenand_read_ops_nolock(mtd, from, ops);
1090 ret = onenand_read_oob_nolock(mtd, from, ops);
1091 onenand_release_device(mtd);
1097 * onenand_bbt_wait - [DEFAULT] wait until the command is done
1098 * @param mtd MTD device structure
1099 * @param state state to select the max. timeout value
1101 * Wait for command done.
1103 static int onenand_bbt_wait(struct mtd_info *mtd, int state)
1105 struct onenand_chip *this = mtd->priv;
1106 unsigned long timeout;
1107 unsigned int interrupt;
1110 /* The 20 msec is enough */
1111 timeout = jiffies + msecs_to_jiffies(20);
1112 while (time_before(jiffies, timeout)) {
1113 interrupt = this->read_word(this->base + ONENAND_REG_INTERRUPT);
1114 if (interrupt & ONENAND_INT_MASTER)
1117 /* To get correct interrupt status in timeout case */
1118 interrupt = this->read_word(this->base + ONENAND_REG_INTERRUPT);
1119 ctrl = this->read_word(this->base + ONENAND_REG_CTRL_STATUS);
1121 if (ctrl & ONENAND_CTRL_ERROR) {
1122 printk(KERN_DEBUG "onenand_bbt_wait: controller error = 0x%04x\n", ctrl);
1123 /* Initial bad block case */
1124 if (ctrl & ONENAND_CTRL_LOAD)
1125 return ONENAND_BBT_READ_ERROR;
1126 return ONENAND_BBT_READ_FATAL_ERROR;
1129 if (interrupt & ONENAND_INT_READ) {
1130 int ecc = this->read_word(this->base + ONENAND_REG_ECC_STATUS);
1131 if (ecc & ONENAND_ECC_2BIT_ALL)
1132 return ONENAND_BBT_READ_ERROR;
1134 printk(KERN_ERR "onenand_bbt_wait: read timeout!"
1135 "ctrl=0x%04x intr=0x%04x\n", ctrl, interrupt);
1136 return ONENAND_BBT_READ_FATAL_ERROR;
1143 * onenand_bbt_read_oob - [MTD Interface] OneNAND read out-of-band for bbt scan
1144 * @param mtd MTD device structure
1145 * @param from offset to read from
1146 * @param ops oob operation description structure
1148 * OneNAND read out-of-band data from the spare area for bbt scan
1150 int onenand_bbt_read_oob(struct mtd_info *mtd, loff_t from,
1151 struct mtd_oob_ops *ops)
1153 struct onenand_chip *this = mtd->priv;
1154 int read = 0, thislen, column;
1156 size_t len = ops->ooblen;
1157 u_char *buf = ops->oobbuf;
1159 DEBUG(MTD_DEBUG_LEVEL3, "onenand_bbt_read_oob: from = 0x%08x, len = %zi\n", (unsigned int) from, len);
1161 /* Initialize return value */
1164 /* Do not allow reads past end of device */
1165 if (unlikely((from + len) > mtd->size)) {
1166 printk(KERN_ERR "onenand_bbt_read_oob: Attempt read beyond end of device\n");
1167 return ONENAND_BBT_READ_FATAL_ERROR;
1170 /* Grab the lock and see if the device is available */
1171 onenand_get_device(mtd, FL_READING);
1173 column = from & (mtd->oobsize - 1);
1175 while (read < len) {
1178 thislen = mtd->oobsize - column;
1179 thislen = min_t(int, thislen, len);
1181 this->command(mtd, ONENAND_CMD_READOOB, from, mtd->oobsize);
1183 onenand_update_bufferram(mtd, from, 0);
1185 ret = onenand_bbt_wait(mtd, FL_READING);
1189 this->read_bufferram(mtd, ONENAND_SPARERAM, buf, column, thislen);
1198 /* Update Page size */
1199 from += this->writesize;
1204 /* Deselect and wake up anyone waiting on the device */
1205 onenand_release_device(mtd);
1207 ops->oobretlen = read;
1211 #ifdef CONFIG_MTD_ONENAND_VERIFY_WRITE
1213 * onenand_verify_oob - [GENERIC] verify the oob contents after a write
1214 * @param mtd MTD device structure
1215 * @param buf the databuffer to verify
1216 * @param to offset to read from
1218 static int onenand_verify_oob(struct mtd_info *mtd, const u_char *buf, loff_t to)
1220 struct onenand_chip *this = mtd->priv;
1224 this->command(mtd, ONENAND_CMD_READOOB, to, mtd->oobsize);
1225 onenand_update_bufferram(mtd, to, 0);
1226 status = this->wait(mtd, FL_READING);
1230 this->read_bufferram(mtd, ONENAND_SPARERAM, oobbuf, 0, mtd->oobsize);
1231 for (i = 0; i < mtd->oobsize; i++)
1232 if (buf[i] != 0xFF && buf[i] != oobbuf[i])
1239 * onenand_verify - [GENERIC] verify the chip contents after a write
1240 * @param mtd MTD device structure
1241 * @param buf the databuffer to verify
1242 * @param addr offset to read from
1243 * @param len number of bytes to read and compare
1245 static int onenand_verify(struct mtd_info *mtd, const u_char *buf, loff_t addr, size_t len)
1247 struct onenand_chip *this = mtd->priv;
1248 void __iomem *dataram;
1250 int thislen, column;
1253 thislen = min_t(int, this->writesize, len);
1254 column = addr & (this->writesize - 1);
1255 if (column + thislen > this->writesize)
1256 thislen = this->writesize - column;
1258 this->command(mtd, ONENAND_CMD_READ, addr, this->writesize);
1260 onenand_update_bufferram(mtd, addr, 0);
1262 ret = this->wait(mtd, FL_READING);
1266 onenand_update_bufferram(mtd, addr, 1);
1268 dataram = this->base + ONENAND_DATARAM;
1269 dataram += onenand_bufferram_offset(mtd, ONENAND_DATARAM);
1271 if (memcmp(buf, dataram + column, thislen))
1282 #define onenand_verify(...) (0)
1283 #define onenand_verify_oob(...) (0)
1286 #define NOTALIGNED(x) ((x & (this->subpagesize - 1)) != 0)
1289 * onenand_fill_auto_oob - [Internal] oob auto-placement transfer
1290 * @param mtd MTD device structure
1291 * @param oob_buf oob buffer
1292 * @param buf source address
1293 * @param column oob offset to write to
1294 * @param thislen oob length to write
1296 static int onenand_fill_auto_oob(struct mtd_info *mtd, u_char *oob_buf,
1297 const u_char *buf, int column, int thislen)
1299 struct onenand_chip *this = mtd->priv;
1300 struct nand_oobfree *free;
1301 int writecol = column;
1302 int writeend = column + thislen;
1306 free = this->ecclayout->oobfree;
1307 for (i = 0; i < MTD_MAX_OOBFREE_ENTRIES && free->length; i++, free++) {
1308 if (writecol >= lastgap)
1309 writecol += free->offset - lastgap;
1310 if (writeend >= lastgap)
1311 writeend += free->offset - lastgap;
1312 lastgap = free->offset + free->length;
1314 free = this->ecclayout->oobfree;
1315 for (i = 0; i < MTD_MAX_OOBFREE_ENTRIES && free->length; i++, free++) {
1316 int free_end = free->offset + free->length;
1317 if (free->offset < writeend && free_end > writecol) {
1318 int st = max_t(int,free->offset,writecol);
1319 int ed = min_t(int,free_end,writeend);
1321 memcpy(oob_buf + st, buf, n);
1323 } else if (column == 0)
1330 * onenand_write_ops_nolock - [OneNAND Interface] write main and/or out-of-band
1331 * @param mtd MTD device structure
1332 * @param to offset to write to
1333 * @param ops oob operation description structure
1335 * Write main and/or oob with ECC
1337 static int onenand_write_ops_nolock(struct mtd_info *mtd, loff_t to,
1338 struct mtd_oob_ops *ops)
1340 struct onenand_chip *this = mtd->priv;
1341 int written = 0, column, thislen, subpage;
1342 int oobwritten = 0, oobcolumn, thisooblen, oobsize;
1343 size_t len = ops->len;
1344 size_t ooblen = ops->ooblen;
1345 const u_char *buf = ops->datbuf;
1346 const u_char *oob = ops->oobbuf;
1350 DEBUG(MTD_DEBUG_LEVEL3, "onenand_write_ops_nolock: to = 0x%08x, len = %i\n", (unsigned int) to, (int) len);
1352 /* Initialize retlen, in case of early exit */
1356 /* Do not allow writes past end of device */
1357 if (unlikely((to + len) > mtd->size)) {
1358 printk(KERN_ERR "onenand_write_ops_nolock: Attempt write to past end of device\n");
1362 /* Reject writes, which are not page aligned */
1363 if (unlikely(NOTALIGNED(to)) || unlikely(NOTALIGNED(len))) {
1364 printk(KERN_ERR "onenand_write_ops_nolock: Attempt to write not page aligned data\n");
1368 if (ops->mode == MTD_OOB_AUTO)
1369 oobsize = this->ecclayout->oobavail;
1371 oobsize = mtd->oobsize;
1373 oobcolumn = to & (mtd->oobsize - 1);
1375 column = to & (mtd->writesize - 1);
1377 /* Loop until all data write */
1378 while (written < len) {
1379 u_char *wbuf = (u_char *) buf;
1381 thislen = min_t(int, mtd->writesize - column, len - written);
1382 thisooblen = min_t(int, oobsize - oobcolumn, ooblen - oobwritten);
1386 this->command(mtd, ONENAND_CMD_BUFFERRAM, to, thislen);
1388 /* Partial page write */
1389 subpage = thislen < mtd->writesize;
1391 memset(this->page_buf, 0xff, mtd->writesize);
1392 memcpy(this->page_buf + column, buf, thislen);
1393 wbuf = this->page_buf;
1396 this->write_bufferram(mtd, ONENAND_DATARAM, wbuf, 0, mtd->writesize);
1399 oobbuf = this->oob_buf;
1401 /* We send data to spare ram with oobsize
1402 * to prevent byte access */
1403 memset(oobbuf, 0xff, mtd->oobsize);
1404 if (ops->mode == MTD_OOB_AUTO)
1405 onenand_fill_auto_oob(mtd, oobbuf, oob, oobcolumn, thisooblen);
1407 memcpy(oobbuf + oobcolumn, oob, thisooblen);
1409 oobwritten += thisooblen;
1413 oobbuf = (u_char *) ffchars;
1415 this->write_bufferram(mtd, ONENAND_SPARERAM, oobbuf, 0, mtd->oobsize);
1417 this->command(mtd, ONENAND_CMD_PROG, to, mtd->writesize);
1419 ret = this->wait(mtd, FL_WRITING);
1421 /* In partial page write we don't update bufferram */
1422 onenand_update_bufferram(mtd, to, !ret && !subpage);
1423 if (ONENAND_IS_2PLANE(this)) {
1424 ONENAND_SET_BUFFERRAM1(this);
1425 onenand_update_bufferram(mtd, to + this->writesize, !ret && !subpage);
1429 printk(KERN_ERR "onenand_write_ops_nolock: write filaed %d\n", ret);
1433 /* Only check verify write turn on */
1434 ret = onenand_verify(mtd, (u_char *) wbuf, to, thislen);
1436 printk(KERN_ERR "onenand_write_ops_nolock: verify failed %d\n", ret);
1450 /* Deselect and wake up anyone waiting on the device */
1451 onenand_release_device(mtd);
1453 ops->retlen = written;
1460 * onenand_write_oob_nolock - [Internal] OneNAND write out-of-band
1461 * @param mtd MTD device structure
1462 * @param to offset to write to
1463 * @param len number of bytes to write
1464 * @param retlen pointer to variable to store the number of written bytes
1465 * @param buf the data to write
1466 * @param mode operation mode
1468 * OneNAND write out-of-band
1470 static int onenand_write_oob_nolock(struct mtd_info *mtd, loff_t to,
1471 struct mtd_oob_ops *ops)
1473 struct onenand_chip *this = mtd->priv;
1474 int column, ret = 0, oobsize;
1477 size_t len = ops->ooblen;
1478 const u_char *buf = ops->oobbuf;
1479 mtd_oob_mode_t mode = ops->mode;
1483 DEBUG(MTD_DEBUG_LEVEL3, "onenand_write_oob_nolock: to = 0x%08x, len = %i\n", (unsigned int) to, (int) len);
1485 /* Initialize retlen, in case of early exit */
1488 if (mode == MTD_OOB_AUTO)
1489 oobsize = this->ecclayout->oobavail;
1491 oobsize = mtd->oobsize;
1493 column = to & (mtd->oobsize - 1);
1495 if (unlikely(column >= oobsize)) {
1496 printk(KERN_ERR "onenand_write_oob_nolock: Attempted to start write outside oob\n");
1500 /* For compatibility with NAND: Do not allow write past end of page */
1501 if (unlikely(column + len > oobsize)) {
1502 printk(KERN_ERR "onenand_write_oob_nolock: "
1503 "Attempt to write past end of page\n");
1507 /* Do not allow reads past end of device */
1508 if (unlikely(to >= mtd->size ||
1509 column + len > ((mtd->size >> this->page_shift) -
1510 (to >> this->page_shift)) * oobsize)) {
1511 printk(KERN_ERR "onenand_write_oob_nolock: Attempted to write past end of device\n");
1515 oobbuf = this->oob_buf;
1517 /* Loop until all data write */
1518 while (written < len) {
1519 int thislen = min_t(int, oobsize, len - written);
1523 this->command(mtd, ONENAND_CMD_BUFFERRAM, to, mtd->oobsize);
1525 /* We send data to spare ram with oobsize
1526 * to prevent byte access */
1527 memset(oobbuf, 0xff, mtd->oobsize);
1528 if (mode == MTD_OOB_AUTO)
1529 onenand_fill_auto_oob(mtd, oobbuf, buf, column, thislen);
1531 memcpy(oobbuf + column, buf, thislen);
1532 this->write_bufferram(mtd, ONENAND_SPARERAM, oobbuf, 0, mtd->oobsize);
1534 this->command(mtd, ONENAND_CMD_PROGOOB, to, mtd->oobsize);
1536 onenand_update_bufferram(mtd, to, 0);
1537 if (ONENAND_IS_2PLANE(this)) {
1538 ONENAND_SET_BUFFERRAM1(this);
1539 onenand_update_bufferram(mtd, to + this->writesize, 0);
1542 ret = this->wait(mtd, FL_WRITING);
1544 printk(KERN_ERR "onenand_write_oob_nolock: write failed %d\n", ret);
1548 ret = onenand_verify_oob(mtd, oobbuf, to);
1550 printk(KERN_ERR "onenand_write_oob_nolock: verify failed %d\n", ret);
1558 to += mtd->writesize;
1563 ops->oobretlen = written;
1569 * onenand_write - [MTD Interface] write buffer to FLASH
1570 * @param mtd MTD device structure
1571 * @param to offset to write to
1572 * @param len number of bytes to write
1573 * @param retlen pointer to variable to store the number of written bytes
1574 * @param buf the data to write
1578 static int onenand_write(struct mtd_info *mtd, loff_t to, size_t len,
1579 size_t *retlen, const u_char *buf)
1581 struct mtd_oob_ops ops = {
1584 .datbuf = (u_char *) buf,
1589 onenand_get_device(mtd, FL_WRITING);
1590 ret = onenand_write_ops_nolock(mtd, to, &ops);
1591 onenand_release_device(mtd);
1593 *retlen = ops.retlen;
1598 * onenand_write_oob - [MTD Interface] NAND write data and/or out-of-band
1599 * @param mtd: MTD device structure
1600 * @param to: offset to write
1601 * @param ops: oob operation description structure
1603 static int onenand_write_oob(struct mtd_info *mtd, loff_t to,
1604 struct mtd_oob_ops *ops)
1608 switch (ops->mode) {
1613 /* Not implemented yet */
1618 onenand_get_device(mtd, FL_WRITING);
1620 ret = onenand_write_ops_nolock(mtd, to, ops);
1622 ret = onenand_write_oob_nolock(mtd, to, ops);
1623 onenand_release_device(mtd);
1629 * onenand_block_isbad_nolock - [GENERIC] Check if a block is marked bad
1630 * @param mtd MTD device structure
1631 * @param ofs offset from device start
1632 * @param allowbbt 1, if its allowed to access the bbt area
1634 * Check, if the block is bad. Either by reading the bad block table or
1635 * calling of the scan function.
1637 static int onenand_block_isbad_nolock(struct mtd_info *mtd, loff_t ofs, int allowbbt)
1639 struct onenand_chip *this = mtd->priv;
1640 struct bbm_info *bbm = this->bbm;
1642 /* Return info from the table */
1643 return bbm->isbad_bbt(mtd, ofs, allowbbt);
1647 * onenand_erase - [MTD Interface] erase block(s)
1648 * @param mtd MTD device structure
1649 * @param instr erase instruction
1651 * Erase one ore more blocks
1653 static int onenand_erase(struct mtd_info *mtd, struct erase_info *instr)
1655 struct onenand_chip *this = mtd->priv;
1656 unsigned int block_size;
1661 DEBUG(MTD_DEBUG_LEVEL3, "onenand_erase: start = 0x%08x, len = %i\n", (unsigned int) instr->addr, (unsigned int) instr->len);
1663 block_size = (1 << this->erase_shift);
1665 /* Start address must align on block boundary */
1666 if (unlikely(instr->addr & (block_size - 1))) {
1667 printk(KERN_ERR "onenand_erase: Unaligned address\n");
1671 /* Length must align on block boundary */
1672 if (unlikely(instr->len & (block_size - 1))) {
1673 printk(KERN_ERR "onenand_erase: Length not block aligned\n");
1677 /* Do not allow erase past end of device */
1678 if (unlikely((instr->len + instr->addr) > mtd->size)) {
1679 printk(KERN_ERR "onenand_erase: Erase past end of device\n");
1683 instr->fail_addr = 0xffffffff;
1685 /* Grab the lock and see if the device is available */
1686 onenand_get_device(mtd, FL_ERASING);
1688 /* Loop throught the pages */
1692 instr->state = MTD_ERASING;
1697 /* Check if we have a bad block, we do not erase bad blocks */
1698 if (onenand_block_isbad_nolock(mtd, addr, 0)) {
1699 printk (KERN_WARNING "onenand_erase: attempt to erase a bad block at addr 0x%08x\n", (unsigned int) addr);
1700 instr->state = MTD_ERASE_FAILED;
1704 this->command(mtd, ONENAND_CMD_ERASE, addr, block_size);
1706 onenand_invalidate_bufferram(mtd, addr, block_size);
1708 ret = this->wait(mtd, FL_ERASING);
1709 /* Check, if it is write protected */
1711 printk(KERN_ERR "onenand_erase: Failed erase, block %d\n", (unsigned) (addr >> this->erase_shift));
1712 instr->state = MTD_ERASE_FAILED;
1713 instr->fail_addr = addr;
1721 instr->state = MTD_ERASE_DONE;
1725 ret = instr->state == MTD_ERASE_DONE ? 0 : -EIO;
1727 /* Deselect and wake up anyone waiting on the device */
1728 onenand_release_device(mtd);
1730 /* Do call back function */
1732 mtd_erase_callback(instr);
1738 * onenand_sync - [MTD Interface] sync
1739 * @param mtd MTD device structure
1741 * Sync is actually a wait for chip ready function
1743 static void onenand_sync(struct mtd_info *mtd)
1745 DEBUG(MTD_DEBUG_LEVEL3, "onenand_sync: called\n");
1747 /* Grab the lock and see if the device is available */
1748 onenand_get_device(mtd, FL_SYNCING);
1750 /* Release it and go back */
1751 onenand_release_device(mtd);
1755 * onenand_block_isbad - [MTD Interface] Check whether the block at the given offset is bad
1756 * @param mtd MTD device structure
1757 * @param ofs offset relative to mtd start
1759 * Check whether the block is bad
1761 static int onenand_block_isbad(struct mtd_info *mtd, loff_t ofs)
1765 /* Check for invalid offset */
1766 if (ofs > mtd->size)
1769 onenand_get_device(mtd, FL_READING);
1770 ret = onenand_block_isbad_nolock(mtd, ofs, 0);
1771 onenand_release_device(mtd);
1776 * onenand_default_block_markbad - [DEFAULT] mark a block bad
1777 * @param mtd MTD device structure
1778 * @param ofs offset from device start
1780 * This is the default implementation, which can be overridden by
1781 * a hardware specific driver.
1783 static int onenand_default_block_markbad(struct mtd_info *mtd, loff_t ofs)
1785 struct onenand_chip *this = mtd->priv;
1786 struct bbm_info *bbm = this->bbm;
1787 u_char buf[2] = {0, 0};
1788 struct mtd_oob_ops ops = {
1789 .mode = MTD_OOB_PLACE,
1796 /* Get block number */
1797 block = ((int) ofs) >> bbm->bbt_erase_shift;
1799 bbm->bbt[block >> 2] |= 0x01 << ((block & 0x03) << 1);
1801 /* We write two bytes, so we dont have to mess with 16 bit access */
1802 ofs += mtd->oobsize + (bbm->badblockpos & ~0x01);
1803 return onenand_write_oob_nolock(mtd, ofs, &ops);
1807 * onenand_block_markbad - [MTD Interface] Mark the block at the given offset as bad
1808 * @param mtd MTD device structure
1809 * @param ofs offset relative to mtd start
1811 * Mark the block as bad
1813 static int onenand_block_markbad(struct mtd_info *mtd, loff_t ofs)
1815 struct onenand_chip *this = mtd->priv;
1818 ret = onenand_block_isbad(mtd, ofs);
1820 /* If it was bad already, return success and do nothing */
1826 onenand_get_device(mtd, FL_WRITING);
1827 ret = this->block_markbad(mtd, ofs);
1828 onenand_release_device(mtd);
1833 * onenand_do_lock_cmd - [OneNAND Interface] Lock or unlock block(s)
1834 * @param mtd MTD device structure
1835 * @param ofs offset relative to mtd start
1836 * @param len number of bytes to lock or unlock
1837 * @param cmd lock or unlock command
1839 * Lock or unlock one or more blocks
1841 static int onenand_do_lock_cmd(struct mtd_info *mtd, loff_t ofs, size_t len, int cmd)
1843 struct onenand_chip *this = mtd->priv;
1844 int start, end, block, value, status;
1847 start = ofs >> this->erase_shift;
1848 end = len >> this->erase_shift;
1850 if (cmd == ONENAND_CMD_LOCK)
1851 wp_status_mask = ONENAND_WP_LS;
1853 wp_status_mask = ONENAND_WP_US;
1855 /* Continuous lock scheme */
1856 if (this->options & ONENAND_HAS_CONT_LOCK) {
1857 /* Set start block address */
1858 this->write_word(start, this->base + ONENAND_REG_START_BLOCK_ADDRESS);
1859 /* Set end block address */
1860 this->write_word(start + end - 1, this->base + ONENAND_REG_END_BLOCK_ADDRESS);
1861 /* Write lock command */
1862 this->command(mtd, cmd, 0, 0);
1864 /* There's no return value */
1865 this->wait(mtd, FL_LOCKING);
1868 while (this->read_word(this->base + ONENAND_REG_CTRL_STATUS)
1869 & ONENAND_CTRL_ONGO)
1872 /* Check lock status */
1873 status = this->read_word(this->base + ONENAND_REG_WP_STATUS);
1874 if (!(status & wp_status_mask))
1875 printk(KERN_ERR "wp status = 0x%x\n", status);
1880 /* Block lock scheme */
1881 for (block = start; block < start + end; block++) {
1882 /* Set block address */
1883 value = onenand_block_address(this, block);
1884 this->write_word(value, this->base + ONENAND_REG_START_ADDRESS1);
1885 /* Select DataRAM for DDP */
1886 value = onenand_bufferram_address(this, block);
1887 this->write_word(value, this->base + ONENAND_REG_START_ADDRESS2);
1888 /* Set start block address */
1889 this->write_word(block, this->base + ONENAND_REG_START_BLOCK_ADDRESS);
1890 /* Write lock command */
1891 this->command(mtd, cmd, 0, 0);
1893 /* There's no return value */
1894 this->wait(mtd, FL_LOCKING);
1897 while (this->read_word(this->base + ONENAND_REG_CTRL_STATUS)
1898 & ONENAND_CTRL_ONGO)
1901 /* Check lock status */
1902 status = this->read_word(this->base + ONENAND_REG_WP_STATUS);
1903 if (!(status & wp_status_mask))
1904 printk(KERN_ERR "block = %d, wp status = 0x%x\n", block, status);
1911 * onenand_lock - [MTD Interface] Lock block(s)
1912 * @param mtd MTD device structure
1913 * @param ofs offset relative to mtd start
1914 * @param len number of bytes to unlock
1916 * Lock one or more blocks
1918 static int onenand_lock(struct mtd_info *mtd, loff_t ofs, size_t len)
1922 onenand_get_device(mtd, FL_LOCKING);
1923 ret = onenand_do_lock_cmd(mtd, ofs, len, ONENAND_CMD_LOCK);
1924 onenand_release_device(mtd);
1929 * onenand_unlock - [MTD Interface] Unlock block(s)
1930 * @param mtd MTD device structure
1931 * @param ofs offset relative to mtd start
1932 * @param len number of bytes to unlock
1934 * Unlock one or more blocks
1936 static int onenand_unlock(struct mtd_info *mtd, loff_t ofs, size_t len)
1940 onenand_get_device(mtd, FL_LOCKING);
1941 ret = onenand_do_lock_cmd(mtd, ofs, len, ONENAND_CMD_UNLOCK);
1942 onenand_release_device(mtd);
1947 * onenand_check_lock_status - [OneNAND Interface] Check lock status
1948 * @param this onenand chip data structure
1952 static void onenand_check_lock_status(struct onenand_chip *this)
1954 unsigned int value, block, status;
1957 end = this->chipsize >> this->erase_shift;
1958 for (block = 0; block < end; block++) {
1959 /* Set block address */
1960 value = onenand_block_address(this, block);
1961 this->write_word(value, this->base + ONENAND_REG_START_ADDRESS1);
1962 /* Select DataRAM for DDP */
1963 value = onenand_bufferram_address(this, block);
1964 this->write_word(value, this->base + ONENAND_REG_START_ADDRESS2);
1965 /* Set start block address */
1966 this->write_word(block, this->base + ONENAND_REG_START_BLOCK_ADDRESS);
1968 /* Check lock status */
1969 status = this->read_word(this->base + ONENAND_REG_WP_STATUS);
1970 if (!(status & ONENAND_WP_US))
1971 printk(KERN_ERR "block = %d, wp status = 0x%x\n", block, status);
1976 * onenand_unlock_all - [OneNAND Interface] unlock all blocks
1977 * @param mtd MTD device structure
1981 static int onenand_unlock_all(struct mtd_info *mtd)
1983 struct onenand_chip *this = mtd->priv;
1985 if (this->options & ONENAND_HAS_UNLOCK_ALL) {
1986 /* Set start block address */
1987 this->write_word(0, this->base + ONENAND_REG_START_BLOCK_ADDRESS);
1988 /* Write unlock command */
1989 this->command(mtd, ONENAND_CMD_UNLOCK_ALL, 0, 0);
1991 /* There's no return value */
1992 this->wait(mtd, FL_LOCKING);
1995 while (this->read_word(this->base + ONENAND_REG_CTRL_STATUS)
1996 & ONENAND_CTRL_ONGO)
1999 /* Workaround for all block unlock in DDP */
2000 if (ONENAND_IS_DDP(this)) {
2001 /* 1st block on another chip */
2002 loff_t ofs = this->chipsize >> 1;
2003 size_t len = mtd->erasesize;
2005 onenand_do_lock_cmd(mtd, ofs, len, ONENAND_CMD_UNLOCK);
2008 onenand_check_lock_status(this);
2013 onenand_do_lock_cmd(mtd, 0x0, this->chipsize, ONENAND_CMD_UNLOCK);
2018 #ifdef CONFIG_MTD_ONENAND_OTP
2020 /* Interal OTP operation */
2021 typedef int (*otp_op_t)(struct mtd_info *mtd, loff_t form, size_t len,
2022 size_t *retlen, u_char *buf);
2025 * do_otp_read - [DEFAULT] Read OTP block area
2026 * @param mtd MTD device structure
2027 * @param from The offset to read
2028 * @param len number of bytes to read
2029 * @param retlen pointer to variable to store the number of readbytes
2030 * @param buf the databuffer to put/get data
2032 * Read OTP block area.
2034 static int do_otp_read(struct mtd_info *mtd, loff_t from, size_t len,
2035 size_t *retlen, u_char *buf)
2037 struct onenand_chip *this = mtd->priv;
2038 struct mtd_oob_ops ops = {
2046 /* Enter OTP access mode */
2047 this->command(mtd, ONENAND_CMD_OTP_ACCESS, 0, 0);
2048 this->wait(mtd, FL_OTPING);
2050 ret = onenand_read_ops_nolock(mtd, from, &ops);
2052 /* Exit OTP access mode */
2053 this->command(mtd, ONENAND_CMD_RESET, 0, 0);
2054 this->wait(mtd, FL_RESETING);
2060 * do_otp_write - [DEFAULT] Write OTP block area
2061 * @param mtd MTD device structure
2062 * @param to The offset to write
2063 * @param len number of bytes to write
2064 * @param retlen pointer to variable to store the number of write bytes
2065 * @param buf the databuffer to put/get data
2067 * Write OTP block area.
2069 static int do_otp_write(struct mtd_info *mtd, loff_t to, size_t len,
2070 size_t *retlen, u_char *buf)
2072 struct onenand_chip *this = mtd->priv;
2073 unsigned char *pbuf = buf;
2075 struct mtd_oob_ops ops;
2077 /* Force buffer page aligned */
2078 if (len < mtd->writesize) {
2079 memcpy(this->page_buf, buf, len);
2080 memset(this->page_buf + len, 0xff, mtd->writesize - len);
2081 pbuf = this->page_buf;
2082 len = mtd->writesize;
2085 /* Enter OTP access mode */
2086 this->command(mtd, ONENAND_CMD_OTP_ACCESS, 0, 0);
2087 this->wait(mtd, FL_OTPING);
2093 ret = onenand_write_ops_nolock(mtd, to, &ops);
2094 *retlen = ops.retlen;
2096 /* Exit OTP access mode */
2097 this->command(mtd, ONENAND_CMD_RESET, 0, 0);
2098 this->wait(mtd, FL_RESETING);
2104 * do_otp_lock - [DEFAULT] Lock OTP block area
2105 * @param mtd MTD device structure
2106 * @param from The offset to lock
2107 * @param len number of bytes to lock
2108 * @param retlen pointer to variable to store the number of lock bytes
2109 * @param buf the databuffer to put/get data
2111 * Lock OTP block area.
2113 static int do_otp_lock(struct mtd_info *mtd, loff_t from, size_t len,
2114 size_t *retlen, u_char *buf)
2116 struct onenand_chip *this = mtd->priv;
2117 struct mtd_oob_ops ops = {
2118 .mode = MTD_OOB_PLACE,
2125 /* Enter OTP access mode */
2126 this->command(mtd, ONENAND_CMD_OTP_ACCESS, 0, 0);
2127 this->wait(mtd, FL_OTPING);
2129 ret = onenand_write_oob_nolock(mtd, from, &ops);
2131 *retlen = ops.oobretlen;
2133 /* Exit OTP access mode */
2134 this->command(mtd, ONENAND_CMD_RESET, 0, 0);
2135 this->wait(mtd, FL_RESETING);
2141 * onenand_otp_walk - [DEFAULT] Handle OTP operation
2142 * @param mtd MTD device structure
2143 * @param from The offset to read/write
2144 * @param len number of bytes to read/write
2145 * @param retlen pointer to variable to store the number of read bytes
2146 * @param buf the databuffer to put/get data
2147 * @param action do given action
2148 * @param mode specify user and factory
2150 * Handle OTP operation.
2152 static int onenand_otp_walk(struct mtd_info *mtd, loff_t from, size_t len,
2153 size_t *retlen, u_char *buf,
2154 otp_op_t action, int mode)
2156 struct onenand_chip *this = mtd->priv;
2163 density = this->device_id >> ONENAND_DEVICE_DENSITY_SHIFT;
2164 if (density < ONENAND_DEVICE_DENSITY_512Mb)
2169 if (mode == MTD_OTP_FACTORY) {
2170 from += mtd->writesize * otp_pages;
2171 otp_pages = 64 - otp_pages;
2174 /* Check User/Factory boundary */
2175 if (((mtd->writesize * otp_pages) - (from + len)) < 0)
2178 onenand_get_device(mtd, FL_OTPING);
2179 while (len > 0 && otp_pages > 0) {
2180 if (!action) { /* OTP Info functions */
2181 struct otp_info *otpinfo;
2183 len -= sizeof(struct otp_info);
2189 otpinfo = (struct otp_info *) buf;
2190 otpinfo->start = from;
2191 otpinfo->length = mtd->writesize;
2192 otpinfo->locked = 0;
2194 from += mtd->writesize;
2195 buf += sizeof(struct otp_info);
2196 *retlen += sizeof(struct otp_info);
2201 ret = action(mtd, from, len, &tmp_retlen, buf);
2212 onenand_release_device(mtd);
2218 * onenand_get_fact_prot_info - [MTD Interface] Read factory OTP info
2219 * @param mtd MTD device structure
2220 * @param buf the databuffer to put/get data
2221 * @param len number of bytes to read
2223 * Read factory OTP info.
2225 static int onenand_get_fact_prot_info(struct mtd_info *mtd,
2226 struct otp_info *buf, size_t len)
2231 ret = onenand_otp_walk(mtd, 0, len, &retlen, (u_char *) buf, NULL, MTD_OTP_FACTORY);
2233 return ret ? : retlen;
2237 * onenand_read_fact_prot_reg - [MTD Interface] Read factory OTP area
2238 * @param mtd MTD device structure
2239 * @param from The offset to read
2240 * @param len number of bytes to read
2241 * @param retlen pointer to variable to store the number of read bytes
2242 * @param buf the databuffer to put/get data
2244 * Read factory OTP area.
2246 static int onenand_read_fact_prot_reg(struct mtd_info *mtd, loff_t from,
2247 size_t len, size_t *retlen, u_char *buf)
2249 return onenand_otp_walk(mtd, from, len, retlen, buf, do_otp_read, MTD_OTP_FACTORY);
2253 * onenand_get_user_prot_info - [MTD Interface] Read user OTP info
2254 * @param mtd MTD device structure
2255 * @param buf the databuffer to put/get data
2256 * @param len number of bytes to read
2258 * Read user OTP info.
2260 static int onenand_get_user_prot_info(struct mtd_info *mtd,
2261 struct otp_info *buf, size_t len)
2266 ret = onenand_otp_walk(mtd, 0, len, &retlen, (u_char *) buf, NULL, MTD_OTP_USER);
2268 return ret ? : retlen;
2272 * onenand_read_user_prot_reg - [MTD Interface] Read user OTP area
2273 * @param mtd MTD device structure
2274 * @param from The offset to read
2275 * @param len number of bytes to read
2276 * @param retlen pointer to variable to store the number of read bytes
2277 * @param buf the databuffer to put/get data
2279 * Read user OTP area.
2281 static int onenand_read_user_prot_reg(struct mtd_info *mtd, loff_t from,
2282 size_t len, size_t *retlen, u_char *buf)
2284 return onenand_otp_walk(mtd, from, len, retlen, buf, do_otp_read, MTD_OTP_USER);
2288 * onenand_write_user_prot_reg - [MTD Interface] Write user OTP area
2289 * @param mtd MTD device structure
2290 * @param from The offset to write
2291 * @param len number of bytes to write
2292 * @param retlen pointer to variable to store the number of write bytes
2293 * @param buf the databuffer to put/get data
2295 * Write user OTP area.
2297 static int onenand_write_user_prot_reg(struct mtd_info *mtd, loff_t from,
2298 size_t len, size_t *retlen, u_char *buf)
2300 return onenand_otp_walk(mtd, from, len, retlen, buf, do_otp_write, MTD_OTP_USER);
2304 * onenand_lock_user_prot_reg - [MTD Interface] Lock user OTP area
2305 * @param mtd MTD device structure
2306 * @param from The offset to lock
2307 * @param len number of bytes to unlock
2309 * Write lock mark on spare area in page 0 in OTP block
2311 static int onenand_lock_user_prot_reg(struct mtd_info *mtd, loff_t from,
2314 unsigned char oob_buf[64];
2318 memset(oob_buf, 0xff, mtd->oobsize);
2320 * Note: OTP lock operation
2321 * OTP block : 0xXXFC
2322 * 1st block : 0xXXF3 (If chip support)
2323 * Both : 0xXXF0 (If chip support)
2325 oob_buf[ONENAND_OTP_LOCK_OFFSET] = 0xFC;
2328 * Write lock mark to 8th word of sector0 of page0 of the spare0.
2329 * We write 16 bytes spare area instead of 2 bytes.
2334 ret = onenand_otp_walk(mtd, from, len, &retlen, oob_buf, do_otp_lock, MTD_OTP_USER);
2336 return ret ? : retlen;
2338 #endif /* CONFIG_MTD_ONENAND_OTP */
2341 * onenand_check_features - Check and set OneNAND features
2342 * @param mtd MTD data structure
2344 * Check and set OneNAND features
2348 static void onenand_check_features(struct mtd_info *mtd)
2350 struct onenand_chip *this = mtd->priv;
2351 unsigned int density, process;
2353 /* Lock scheme depends on density and process */
2354 density = this->device_id >> ONENAND_DEVICE_DENSITY_SHIFT;
2355 process = this->version_id >> ONENAND_VERSION_PROCESS_SHIFT;
2359 case ONENAND_DEVICE_DENSITY_4Gb:
2360 this->options |= ONENAND_HAS_2PLANE;
2362 case ONENAND_DEVICE_DENSITY_2Gb:
2363 /* 2Gb DDP don't have 2 plane */
2364 if (!ONENAND_IS_DDP(this))
2365 this->options |= ONENAND_HAS_2PLANE;
2366 this->options |= ONENAND_HAS_UNLOCK_ALL;
2368 case ONENAND_DEVICE_DENSITY_1Gb:
2369 /* A-Die has all block unlock */
2371 this->options |= ONENAND_HAS_UNLOCK_ALL;
2375 /* Some OneNAND has continuous lock scheme */
2377 this->options |= ONENAND_HAS_CONT_LOCK;
2381 if (this->options & ONENAND_HAS_CONT_LOCK)
2382 printk(KERN_DEBUG "Lock scheme is Continuous Lock\n");
2383 if (this->options & ONENAND_HAS_UNLOCK_ALL)
2384 printk(KERN_DEBUG "Chip support all block unlock\n");
2385 if (this->options & ONENAND_HAS_2PLANE)
2386 printk(KERN_DEBUG "Chip has 2 plane\n");
2390 * onenand_print_device_info - Print device & version ID
2391 * @param device device ID
2392 * @param version version ID
2394 * Print device & version ID
2396 static void onenand_print_device_info(int device, int version)
2398 int vcc, demuxed, ddp, density;
2400 vcc = device & ONENAND_DEVICE_VCC_MASK;
2401 demuxed = device & ONENAND_DEVICE_IS_DEMUX;
2402 ddp = device & ONENAND_DEVICE_IS_DDP;
2403 density = device >> ONENAND_DEVICE_DENSITY_SHIFT;
2404 printk(KERN_INFO "%sOneNAND%s %dMB %sV 16-bit (0x%02x)\n",
2405 demuxed ? "" : "Muxed ",
2408 vcc ? "2.65/3.3" : "1.8",
2410 printk(KERN_INFO "OneNAND version = 0x%04x\n", version);
2413 static const struct onenand_manufacturers onenand_manuf_ids[] = {
2414 {ONENAND_MFR_SAMSUNG, "Samsung"},
2418 * onenand_check_maf - Check manufacturer ID
2419 * @param manuf manufacturer ID
2421 * Check manufacturer ID
2423 static int onenand_check_maf(int manuf)
2425 int size = ARRAY_SIZE(onenand_manuf_ids);
2429 for (i = 0; i < size; i++)
2430 if (manuf == onenand_manuf_ids[i].id)
2434 name = onenand_manuf_ids[i].name;
2438 printk(KERN_DEBUG "OneNAND Manufacturer: %s (0x%0x)\n", name, manuf);
2444 * onenand_probe - [OneNAND Interface] Probe the OneNAND device
2445 * @param mtd MTD device structure
2447 * OneNAND detection method:
2448 * Compare the values from command with ones from register
2450 static int onenand_probe(struct mtd_info *mtd)
2452 struct onenand_chip *this = mtd->priv;
2453 int bram_maf_id, bram_dev_id, maf_id, dev_id, ver_id;
2457 /* Save system configuration 1 */
2458 syscfg = this->read_word(this->base + ONENAND_REG_SYS_CFG1);
2459 /* Clear Sync. Burst Read mode to read BootRAM */
2460 this->write_word((syscfg & ~ONENAND_SYS_CFG1_SYNC_READ), this->base + ONENAND_REG_SYS_CFG1);
2462 /* Send the command for reading device ID from BootRAM */
2463 this->write_word(ONENAND_CMD_READID, this->base + ONENAND_BOOTRAM);
2465 /* Read manufacturer and device IDs from BootRAM */
2466 bram_maf_id = this->read_word(this->base + ONENAND_BOOTRAM + 0x0);
2467 bram_dev_id = this->read_word(this->base + ONENAND_BOOTRAM + 0x2);
2469 /* Reset OneNAND to read default register values */
2470 this->write_word(ONENAND_CMD_RESET, this->base + ONENAND_BOOTRAM);
2472 this->wait(mtd, FL_RESETING);
2474 /* Restore system configuration 1 */
2475 this->write_word(syscfg, this->base + ONENAND_REG_SYS_CFG1);
2477 /* Check manufacturer ID */
2478 if (onenand_check_maf(bram_maf_id))
2481 /* Read manufacturer and device IDs from Register */
2482 maf_id = this->read_word(this->base + ONENAND_REG_MANUFACTURER_ID);
2483 dev_id = this->read_word(this->base + ONENAND_REG_DEVICE_ID);
2484 ver_id = this->read_word(this->base + ONENAND_REG_VERSION_ID);
2486 /* Check OneNAND device */
2487 if (maf_id != bram_maf_id || dev_id != bram_dev_id)
2490 /* Flash device information */
2491 onenand_print_device_info(dev_id, ver_id);
2492 this->device_id = dev_id;
2493 this->version_id = ver_id;
2495 density = dev_id >> ONENAND_DEVICE_DENSITY_SHIFT;
2496 this->chipsize = (16 << density) << 20;
2497 /* Set density mask. it is used for DDP */
2498 if (ONENAND_IS_DDP(this))
2499 this->density_mask = (1 << (density + 6));
2501 this->density_mask = 0;
2503 /* OneNAND page size & block size */
2504 /* The data buffer size is equal to page size */
2505 mtd->writesize = this->read_word(this->base + ONENAND_REG_DATA_BUFFER_SIZE);
2506 mtd->oobsize = mtd->writesize >> 5;
2507 /* Pages per a block are always 64 in OneNAND */
2508 mtd->erasesize = mtd->writesize << 6;
2510 this->erase_shift = ffs(mtd->erasesize) - 1;
2511 this->page_shift = ffs(mtd->writesize) - 1;
2512 this->page_mask = (1 << (this->erase_shift - this->page_shift)) - 1;
2513 /* It's real page size */
2514 this->writesize = mtd->writesize;
2516 /* REVIST: Multichip handling */
2518 mtd->size = this->chipsize;
2520 /* Check OneNAND features */
2521 onenand_check_features(mtd);
2524 * We emulate the 4KiB page and 256KiB erase block size
2525 * But oobsize is still 64 bytes.
2526 * It is only valid if you turn on 2X program support,
2527 * Otherwise it will be ignored by compiler.
2529 if (ONENAND_IS_2PLANE(this)) {
2530 mtd->writesize <<= 1;
2531 mtd->erasesize <<= 1;
2538 * onenand_suspend - [MTD Interface] Suspend the OneNAND flash
2539 * @param mtd MTD device structure
2541 static int onenand_suspend(struct mtd_info *mtd)
2543 return onenand_get_device(mtd, FL_PM_SUSPENDED);
2547 * onenand_resume - [MTD Interface] Resume the OneNAND flash
2548 * @param mtd MTD device structure
2550 static void onenand_resume(struct mtd_info *mtd)
2552 struct onenand_chip *this = mtd->priv;
2554 if (this->state == FL_PM_SUSPENDED)
2555 onenand_release_device(mtd);
2557 printk(KERN_ERR "resume() called for the chip which is not"
2558 "in suspended state\n");
2562 * onenand_scan - [OneNAND Interface] Scan for the OneNAND device
2563 * @param mtd MTD device structure
2564 * @param maxchips Number of chips to scan for
2566 * This fills out all the not initialized function pointers
2567 * with the defaults.
2568 * The flash ID is read and the mtd/chip structures are
2569 * filled with the appropriate values.
2571 int onenand_scan(struct mtd_info *mtd, int maxchips)
2574 struct onenand_chip *this = mtd->priv;
2576 if (!this->read_word)
2577 this->read_word = onenand_readw;
2578 if (!this->write_word)
2579 this->write_word = onenand_writew;
2582 this->command = onenand_command;
2584 onenand_setup_wait(mtd);
2586 if (!this->read_bufferram)
2587 this->read_bufferram = onenand_read_bufferram;
2588 if (!this->write_bufferram)
2589 this->write_bufferram = onenand_write_bufferram;
2591 if (!this->block_markbad)
2592 this->block_markbad = onenand_default_block_markbad;
2593 if (!this->scan_bbt)
2594 this->scan_bbt = onenand_default_bbt;
2596 if (onenand_probe(mtd))
2599 /* Set Sync. Burst Read after probing */
2600 if (this->mmcontrol) {
2601 printk(KERN_INFO "OneNAND Sync. Burst Read support\n");
2602 this->read_bufferram = onenand_sync_read_bufferram;
2605 /* Allocate buffers, if necessary */
2606 if (!this->page_buf) {
2607 this->page_buf = kzalloc(mtd->writesize, GFP_KERNEL);
2608 if (!this->page_buf) {
2609 printk(KERN_ERR "onenand_scan(): Can't allocate page_buf\n");
2612 this->options |= ONENAND_PAGEBUF_ALLOC;
2614 if (!this->oob_buf) {
2615 this->oob_buf = kzalloc(mtd->oobsize, GFP_KERNEL);
2616 if (!this->oob_buf) {
2617 printk(KERN_ERR "onenand_scan(): Can't allocate oob_buf\n");
2618 if (this->options & ONENAND_PAGEBUF_ALLOC) {
2619 this->options &= ~ONENAND_PAGEBUF_ALLOC;
2620 kfree(this->page_buf);
2624 this->options |= ONENAND_OOBBUF_ALLOC;
2627 this->state = FL_READY;
2628 init_waitqueue_head(&this->wq);
2629 spin_lock_init(&this->chip_lock);
2632 * Allow subpage writes up to oobsize.
2634 switch (mtd->oobsize) {
2636 this->ecclayout = &onenand_oob_64;
2637 mtd->subpage_sft = 2;
2641 this->ecclayout = &onenand_oob_32;
2642 mtd->subpage_sft = 1;
2646 printk(KERN_WARNING "No OOB scheme defined for oobsize %d\n",
2648 mtd->subpage_sft = 0;
2649 /* To prevent kernel oops */
2650 this->ecclayout = &onenand_oob_32;
2654 this->subpagesize = mtd->writesize >> mtd->subpage_sft;
2657 * The number of bytes available for a client to place data into
2658 * the out of band area
2660 this->ecclayout->oobavail = 0;
2661 for (i = 0; i < MTD_MAX_OOBFREE_ENTRIES &&
2662 this->ecclayout->oobfree[i].length; i++)
2663 this->ecclayout->oobavail +=
2664 this->ecclayout->oobfree[i].length;
2665 mtd->oobavail = this->ecclayout->oobavail;
2667 mtd->ecclayout = this->ecclayout;
2669 /* Fill in remaining MTD driver data */
2670 mtd->type = MTD_NANDFLASH;
2671 mtd->flags = MTD_CAP_NANDFLASH;
2672 mtd->erase = onenand_erase;
2674 mtd->unpoint = NULL;
2675 mtd->read = onenand_read;
2676 mtd->write = onenand_write;
2677 mtd->read_oob = onenand_read_oob;
2678 mtd->write_oob = onenand_write_oob;
2679 #ifdef CONFIG_MTD_ONENAND_OTP
2680 mtd->get_fact_prot_info = onenand_get_fact_prot_info;
2681 mtd->read_fact_prot_reg = onenand_read_fact_prot_reg;
2682 mtd->get_user_prot_info = onenand_get_user_prot_info;
2683 mtd->read_user_prot_reg = onenand_read_user_prot_reg;
2684 mtd->write_user_prot_reg = onenand_write_user_prot_reg;
2685 mtd->lock_user_prot_reg = onenand_lock_user_prot_reg;
2687 mtd->sync = onenand_sync;
2688 mtd->lock = onenand_lock;
2689 mtd->unlock = onenand_unlock;
2690 mtd->suspend = onenand_suspend;
2691 mtd->resume = onenand_resume;
2692 mtd->block_isbad = onenand_block_isbad;
2693 mtd->block_markbad = onenand_block_markbad;
2694 mtd->owner = THIS_MODULE;
2696 /* Unlock whole block */
2697 onenand_unlock_all(mtd);
2699 return this->scan_bbt(mtd);
2703 * onenand_release - [OneNAND Interface] Free resources held by the OneNAND device
2704 * @param mtd MTD device structure
2706 void onenand_release(struct mtd_info *mtd)
2708 struct onenand_chip *this = mtd->priv;
2710 #ifdef CONFIG_MTD_PARTITIONS
2711 /* Deregister partitions */
2712 del_mtd_partitions (mtd);
2714 /* Deregister the device */
2715 del_mtd_device (mtd);
2717 /* Free bad block table memory, if allocated */
2719 struct bbm_info *bbm = this->bbm;
2723 /* Buffers allocated by onenand_scan */
2724 if (this->options & ONENAND_PAGEBUF_ALLOC)
2725 kfree(this->page_buf);
2726 if (this->options & ONENAND_OOBBUF_ALLOC)
2727 kfree(this->oob_buf);
2730 EXPORT_SYMBOL_GPL(onenand_scan);
2731 EXPORT_SYMBOL_GPL(onenand_release);
2733 MODULE_LICENSE("GPL");
2734 MODULE_AUTHOR("Kyungmin Park <kyungmin.park@samsung.com>");
2735 MODULE_DESCRIPTION("Generic OneNAND flash driver code");