2 * linux/drivers/mtd/onenand/onenand_base.c
4 * Copyright © 2005-2009 Samsung Electronics
5 * Copyright © 2007 Nokia Corporation
7 * Kyungmin Park <kyungmin.park@samsung.com>
10 * Adrian Hunter <ext-adrian.hunter@nokia.com>:
11 * auto-placement support, read-while load support, various fixes
13 * Vishak G <vishak.g at samsung.com>, Rohit Hagargundgi <h.rohit at samsung.com>
14 * Flex-OneNAND support
15 * Amul Kumar Saha <amul.saha at samsung.com>
18 * This program is free software; you can redistribute it and/or modify
19 * it under the terms of the GNU General Public License version 2 as
20 * published by the Free Software Foundation.
23 #include <linux/kernel.h>
24 #include <linux/module.h>
25 #include <linux/moduleparam.h>
26 #include <linux/slab.h>
27 #include <linux/init.h>
28 #include <linux/sched.h>
29 #include <linux/delay.h>
30 #include <linux/interrupt.h>
31 #include <linux/jiffies.h>
32 #include <linux/mtd/mtd.h>
33 #include <linux/mtd/onenand.h>
34 #include <linux/mtd/partitions.h>
39 * Multiblock erase if number of blocks to erase is 2 or more.
40 * Maximum number of blocks for simultaneous erase is 64.
42 #define MB_ERASE_MIN_BLK_COUNT 2
43 #define MB_ERASE_MAX_BLK_COUNT 64
45 /* Default Flex-OneNAND boundary and lock respectively */
46 static int flex_bdry[MAX_DIES * 2] = { -1, 0, -1, 0 };
48 module_param_array(flex_bdry, int, NULL, 0400);
49 MODULE_PARM_DESC(flex_bdry, "SLC Boundary information for Flex-OneNAND"
50 "Syntax:flex_bdry=DIE_BDRY,LOCK,..."
51 "DIE_BDRY: SLC boundary of the die"
52 "LOCK: Locking information for SLC boundary"
53 " : 0->Set boundary in unlocked status"
54 " : 1->Set boundary in locked status");
56 /* Default OneNAND/Flex-OneNAND OTP options*/
59 module_param(otp, int, 0400);
60 MODULE_PARM_DESC(otp, "Corresponding behaviour of OneNAND in OTP"
61 "Syntax : otp=LOCK_TYPE"
62 "LOCK_TYPE : Keys issued, for specific OTP Lock type"
63 " : 0 -> Default (No Blocks Locked)"
64 " : 1 -> OTP Block lock"
65 " : 2 -> 1st Block lock"
66 " : 3 -> BOTH OTP Block and 1st Block lock");
69 * onenand_oob_128 - oob info for Flex-Onenand with 4KB page
70 * For now, we expose only 64 out of 80 ecc bytes
72 static struct nand_ecclayout onenand_oob_128 = {
75 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
76 22, 23, 24, 25, 26, 27, 28, 29, 30, 31,
77 38, 39, 40, 41, 42, 43, 44, 45, 46, 47,
78 54, 55, 56, 57, 58, 59, 60, 61, 62, 63,
79 70, 71, 72, 73, 74, 75, 76, 77, 78, 79,
80 86, 87, 88, 89, 90, 91, 92, 93, 94, 95,
84 {2, 4}, {18, 4}, {34, 4}, {50, 4},
85 {66, 4}, {82, 4}, {98, 4}, {114, 4}
90 * onenand_oob_64 - oob info for large (2KB) page
92 static struct nand_ecclayout onenand_oob_64 = {
101 {2, 3}, {14, 2}, {18, 3}, {30, 2},
102 {34, 3}, {46, 2}, {50, 3}, {62, 2}
107 * onenand_oob_32 - oob info for middle (1KB) page
109 static struct nand_ecclayout onenand_oob_32 = {
115 .oobfree = { {2, 3}, {14, 2}, {18, 3}, {30, 2} }
118 static const unsigned char ffchars[] = {
119 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
120 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 16 */
121 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
122 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 32 */
123 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
124 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 48 */
125 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
126 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 64 */
127 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
128 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 80 */
129 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
130 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 96 */
131 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
132 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 112 */
133 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
134 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 128 */
138 * onenand_readw - [OneNAND Interface] Read OneNAND register
139 * @param addr address to read
141 * Read OneNAND register
143 static unsigned short onenand_readw(void __iomem *addr)
149 * onenand_writew - [OneNAND Interface] Write OneNAND register with value
150 * @param value value to write
151 * @param addr address to write
153 * Write OneNAND register with value
155 static void onenand_writew(unsigned short value, void __iomem *addr)
161 * onenand_block_address - [DEFAULT] Get block address
162 * @param this onenand chip data structure
163 * @param block the block
164 * @return translated block address if DDP, otherwise same
166 * Setup Start Address 1 Register (F100h)
168 static int onenand_block_address(struct onenand_chip *this, int block)
170 /* Device Flash Core select, NAND Flash Block Address */
171 if (block & this->density_mask)
172 return ONENAND_DDP_CHIP1 | (block ^ this->density_mask);
178 * onenand_bufferram_address - [DEFAULT] Get bufferram address
179 * @param this onenand chip data structure
180 * @param block the block
181 * @return set DBS value if DDP, otherwise 0
183 * Setup Start Address 2 Register (F101h) for DDP
185 static int onenand_bufferram_address(struct onenand_chip *this, int block)
187 /* Device BufferRAM Select */
188 if (block & this->density_mask)
189 return ONENAND_DDP_CHIP1;
191 return ONENAND_DDP_CHIP0;
195 * onenand_page_address - [DEFAULT] Get page address
196 * @param page the page address
197 * @param sector the sector address
198 * @return combined page and sector address
200 * Setup Start Address 8 Register (F107h)
202 static int onenand_page_address(int page, int sector)
204 /* Flash Page Address, Flash Sector Address */
207 fpa = page & ONENAND_FPA_MASK;
208 fsa = sector & ONENAND_FSA_MASK;
210 return ((fpa << ONENAND_FPA_SHIFT) | fsa);
214 * onenand_buffer_address - [DEFAULT] Get buffer address
215 * @param dataram1 DataRAM index
216 * @param sectors the sector address
217 * @param count the number of sectors
218 * @return the start buffer value
220 * Setup Start Buffer Register (F200h)
222 static int onenand_buffer_address(int dataram1, int sectors, int count)
226 /* BufferRAM Sector Address */
227 bsa = sectors & ONENAND_BSA_MASK;
230 bsa |= ONENAND_BSA_DATARAM1; /* DataRAM1 */
232 bsa |= ONENAND_BSA_DATARAM0; /* DataRAM0 */
234 /* BufferRAM Sector Count */
235 bsc = count & ONENAND_BSC_MASK;
237 return ((bsa << ONENAND_BSA_SHIFT) | bsc);
241 * flexonenand_block- For given address return block number
242 * @param this - OneNAND device structure
243 * @param addr - Address for which block number is needed
245 static unsigned flexonenand_block(struct onenand_chip *this, loff_t addr)
247 unsigned boundary, blk, die = 0;
249 if (ONENAND_IS_DDP(this) && addr >= this->diesize[0]) {
251 addr -= this->diesize[0];
254 boundary = this->boundary[die];
256 blk = addr >> (this->erase_shift - 1);
258 blk = (blk + boundary + 1) >> 1;
260 blk += die ? this->density_mask : 0;
264 inline unsigned onenand_block(struct onenand_chip *this, loff_t addr)
266 if (!FLEXONENAND(this))
267 return addr >> this->erase_shift;
268 return flexonenand_block(this, addr);
272 * flexonenand_addr - Return address of the block
273 * @this: OneNAND device structure
274 * @block: Block number on Flex-OneNAND
276 * Return address of the block
278 static loff_t flexonenand_addr(struct onenand_chip *this, int block)
281 int die = 0, boundary;
283 if (ONENAND_IS_DDP(this) && block >= this->density_mask) {
284 block -= this->density_mask;
286 ofs = this->diesize[0];
289 boundary = this->boundary[die];
290 ofs += (loff_t)block << (this->erase_shift - 1);
291 if (block > (boundary + 1))
292 ofs += (loff_t)(block - boundary - 1) << (this->erase_shift - 1);
296 loff_t onenand_addr(struct onenand_chip *this, int block)
298 if (!FLEXONENAND(this))
299 return (loff_t)block << this->erase_shift;
300 return flexonenand_addr(this, block);
302 EXPORT_SYMBOL(onenand_addr);
305 * onenand_get_density - [DEFAULT] Get OneNAND density
306 * @param dev_id OneNAND device ID
308 * Get OneNAND density from device ID
310 static inline int onenand_get_density(int dev_id)
312 int density = dev_id >> ONENAND_DEVICE_DENSITY_SHIFT;
313 return (density & ONENAND_DEVICE_DENSITY_MASK);
317 * flexonenand_region - [Flex-OneNAND] Return erase region of addr
318 * @param mtd MTD device structure
319 * @param addr address whose erase region needs to be identified
321 int flexonenand_region(struct mtd_info *mtd, loff_t addr)
325 for (i = 0; i < mtd->numeraseregions; i++)
326 if (addr < mtd->eraseregions[i].offset)
330 EXPORT_SYMBOL(flexonenand_region);
333 * onenand_command - [DEFAULT] Send command to OneNAND device
334 * @param mtd MTD device structure
335 * @param cmd the command to be sent
336 * @param addr offset to read from or write to
337 * @param len number of bytes to read or write
339 * Send command to OneNAND device. This function is used for middle/large page
340 * devices (1KB/2KB Bytes per page)
342 static int onenand_command(struct mtd_info *mtd, int cmd, loff_t addr, size_t len)
344 struct onenand_chip *this = mtd->priv;
345 int value, block, page;
347 /* Address translation */
349 case ONENAND_CMD_UNLOCK:
350 case ONENAND_CMD_LOCK:
351 case ONENAND_CMD_LOCK_TIGHT:
352 case ONENAND_CMD_UNLOCK_ALL:
357 case FLEXONENAND_CMD_PI_ACCESS:
358 /* addr contains die index */
359 block = addr * this->density_mask;
363 case ONENAND_CMD_ERASE:
364 case ONENAND_CMD_MULTIBLOCK_ERASE:
365 case ONENAND_CMD_ERASE_VERIFY:
366 case ONENAND_CMD_BUFFERRAM:
367 case ONENAND_CMD_OTP_ACCESS:
368 block = onenand_block(this, addr);
372 case FLEXONENAND_CMD_READ_PI:
373 cmd = ONENAND_CMD_READ;
374 block = addr * this->density_mask;
379 block = onenand_block(this, addr);
380 if (FLEXONENAND(this))
381 page = (int) (addr - onenand_addr(this, block))>>\
384 page = (int) (addr >> this->page_shift);
385 if (ONENAND_IS_2PLANE(this)) {
386 /* Make the even block number */
388 /* Is it the odd plane? */
389 if (addr & this->writesize)
393 page &= this->page_mask;
397 /* NOTE: The setting order of the registers is very important! */
398 if (cmd == ONENAND_CMD_BUFFERRAM) {
399 /* Select DataRAM for DDP */
400 value = onenand_bufferram_address(this, block);
401 this->write_word(value, this->base + ONENAND_REG_START_ADDRESS2);
403 if (ONENAND_IS_2PLANE(this) || ONENAND_IS_4KB_PAGE(this))
404 /* It is always BufferRAM0 */
405 ONENAND_SET_BUFFERRAM0(this);
407 /* Switch to the next data buffer */
408 ONENAND_SET_NEXT_BUFFERRAM(this);
414 /* Write 'DFS, FBA' of Flash */
415 value = onenand_block_address(this, block);
416 this->write_word(value, this->base + ONENAND_REG_START_ADDRESS1);
418 /* Select DataRAM for DDP */
419 value = onenand_bufferram_address(this, block);
420 this->write_word(value, this->base + ONENAND_REG_START_ADDRESS2);
424 /* Now we use page size operation */
425 int sectors = 0, count = 0;
429 case FLEXONENAND_CMD_RECOVER_LSB:
430 case ONENAND_CMD_READ:
431 case ONENAND_CMD_READOOB:
432 if (ONENAND_IS_4KB_PAGE(this))
433 /* It is always BufferRAM0 */
434 dataram = ONENAND_SET_BUFFERRAM0(this);
436 dataram = ONENAND_SET_NEXT_BUFFERRAM(this);
440 if (ONENAND_IS_2PLANE(this) && cmd == ONENAND_CMD_PROG)
441 cmd = ONENAND_CMD_2X_PROG;
442 dataram = ONENAND_CURRENT_BUFFERRAM(this);
446 /* Write 'FPA, FSA' of Flash */
447 value = onenand_page_address(page, sectors);
448 this->write_word(value, this->base + ONENAND_REG_START_ADDRESS8);
450 /* Write 'BSA, BSC' of DataRAM */
451 value = onenand_buffer_address(dataram, sectors, count);
452 this->write_word(value, this->base + ONENAND_REG_START_BUFFER);
455 /* Interrupt clear */
456 this->write_word(ONENAND_INT_CLEAR, this->base + ONENAND_REG_INTERRUPT);
459 this->write_word(cmd, this->base + ONENAND_REG_COMMAND);
465 * onenand_read_ecc - return ecc status
466 * @param this onenand chip structure
468 static inline int onenand_read_ecc(struct onenand_chip *this)
470 int ecc, i, result = 0;
472 if (!FLEXONENAND(this) && !ONENAND_IS_4KB_PAGE(this))
473 return this->read_word(this->base + ONENAND_REG_ECC_STATUS);
475 for (i = 0; i < 4; i++) {
476 ecc = this->read_word(this->base + ONENAND_REG_ECC_STATUS + i*2);
479 if (ecc & FLEXONENAND_UNCORRECTABLE_ERROR)
480 return ONENAND_ECC_2BIT_ALL;
482 result = ONENAND_ECC_1BIT_ALL;
489 * onenand_wait - [DEFAULT] wait until the command is done
490 * @param mtd MTD device structure
491 * @param state state to select the max. timeout value
493 * Wait for command done. This applies to all OneNAND command
494 * Read can take up to 30us, erase up to 2ms and program up to 350us
495 * according to general OneNAND specs
497 static int onenand_wait(struct mtd_info *mtd, int state)
499 struct onenand_chip * this = mtd->priv;
500 unsigned long timeout;
501 unsigned int flags = ONENAND_INT_MASTER;
502 unsigned int interrupt = 0;
505 /* The 20 msec is enough */
506 timeout = jiffies + msecs_to_jiffies(20);
507 while (time_before(jiffies, timeout)) {
508 interrupt = this->read_word(this->base + ONENAND_REG_INTERRUPT);
510 if (interrupt & flags)
513 if (state != FL_READING && state != FL_PREPARING_ERASE)
516 /* To get correct interrupt status in timeout case */
517 interrupt = this->read_word(this->base + ONENAND_REG_INTERRUPT);
519 ctrl = this->read_word(this->base + ONENAND_REG_CTRL_STATUS);
522 * In the Spec. it checks the controller status first
523 * However if you get the correct information in case of
524 * power off recovery (POR) test, it should read ECC status first
526 if (interrupt & ONENAND_INT_READ) {
527 int ecc = onenand_read_ecc(this);
529 if (ecc & ONENAND_ECC_2BIT_ALL) {
530 printk(KERN_ERR "%s: ECC error = 0x%04x\n",
532 mtd->ecc_stats.failed++;
534 } else if (ecc & ONENAND_ECC_1BIT_ALL) {
535 printk(KERN_DEBUG "%s: correctable ECC error = 0x%04x\n",
537 mtd->ecc_stats.corrected++;
540 } else if (state == FL_READING) {
541 printk(KERN_ERR "%s: read timeout! ctrl=0x%04x intr=0x%04x\n",
542 __func__, ctrl, interrupt);
546 if (state == FL_PREPARING_ERASE && !(interrupt & ONENAND_INT_ERASE)) {
547 printk(KERN_ERR "%s: mb erase timeout! ctrl=0x%04x intr=0x%04x\n",
548 __func__, ctrl, interrupt);
552 if (!(interrupt & ONENAND_INT_MASTER)) {
553 printk(KERN_ERR "%s: timeout! ctrl=0x%04x intr=0x%04x\n",
554 __func__, ctrl, interrupt);
558 /* If there's controller error, it's a real error */
559 if (ctrl & ONENAND_CTRL_ERROR) {
560 printk(KERN_ERR "%s: controller error = 0x%04x\n",
562 if (ctrl & ONENAND_CTRL_LOCK)
563 printk(KERN_ERR "%s: it's locked error.\n", __func__);
571 * onenand_interrupt - [DEFAULT] onenand interrupt handler
572 * @param irq onenand interrupt number
573 * @param dev_id interrupt data
577 static irqreturn_t onenand_interrupt(int irq, void *data)
579 struct onenand_chip *this = data;
581 /* To handle shared interrupt */
582 if (!this->complete.done)
583 complete(&this->complete);
589 * onenand_interrupt_wait - [DEFAULT] wait until the command is done
590 * @param mtd MTD device structure
591 * @param state state to select the max. timeout value
593 * Wait for command done.
595 static int onenand_interrupt_wait(struct mtd_info *mtd, int state)
597 struct onenand_chip *this = mtd->priv;
599 wait_for_completion(&this->complete);
601 return onenand_wait(mtd, state);
605 * onenand_try_interrupt_wait - [DEFAULT] try interrupt wait
606 * @param mtd MTD device structure
607 * @param state state to select the max. timeout value
609 * Try interrupt based wait (It is used one-time)
611 static int onenand_try_interrupt_wait(struct mtd_info *mtd, int state)
613 struct onenand_chip *this = mtd->priv;
614 unsigned long remain, timeout;
616 /* We use interrupt wait first */
617 this->wait = onenand_interrupt_wait;
619 timeout = msecs_to_jiffies(100);
620 remain = wait_for_completion_timeout(&this->complete, timeout);
622 printk(KERN_INFO "OneNAND: There's no interrupt. "
623 "We use the normal wait\n");
625 /* Release the irq */
626 free_irq(this->irq, this);
628 this->wait = onenand_wait;
631 return onenand_wait(mtd, state);
635 * onenand_setup_wait - [OneNAND Interface] setup onenand wait method
636 * @param mtd MTD device structure
638 * There's two method to wait onenand work
639 * 1. polling - read interrupt status register
640 * 2. interrupt - use the kernel interrupt method
642 static void onenand_setup_wait(struct mtd_info *mtd)
644 struct onenand_chip *this = mtd->priv;
647 init_completion(&this->complete);
649 if (this->irq <= 0) {
650 this->wait = onenand_wait;
654 if (request_irq(this->irq, &onenand_interrupt,
655 IRQF_SHARED, "onenand", this)) {
656 /* If we can't get irq, use the normal wait */
657 this->wait = onenand_wait;
661 /* Enable interrupt */
662 syscfg = this->read_word(this->base + ONENAND_REG_SYS_CFG1);
663 syscfg |= ONENAND_SYS_CFG1_IOBE;
664 this->write_word(syscfg, this->base + ONENAND_REG_SYS_CFG1);
666 this->wait = onenand_try_interrupt_wait;
670 * onenand_bufferram_offset - [DEFAULT] BufferRAM offset
671 * @param mtd MTD data structure
672 * @param area BufferRAM area
673 * @return offset given area
675 * Return BufferRAM offset given area
677 static inline int onenand_bufferram_offset(struct mtd_info *mtd, int area)
679 struct onenand_chip *this = mtd->priv;
681 if (ONENAND_CURRENT_BUFFERRAM(this)) {
682 /* Note: the 'this->writesize' is a real page size */
683 if (area == ONENAND_DATARAM)
684 return this->writesize;
685 if (area == ONENAND_SPARERAM)
693 * onenand_read_bufferram - [OneNAND Interface] Read the bufferram area
694 * @param mtd MTD data structure
695 * @param area BufferRAM area
696 * @param buffer the databuffer to put/get data
697 * @param offset offset to read from or write to
698 * @param count number of bytes to read/write
700 * Read the BufferRAM area
702 static int onenand_read_bufferram(struct mtd_info *mtd, int area,
703 unsigned char *buffer, int offset, size_t count)
705 struct onenand_chip *this = mtd->priv;
706 void __iomem *bufferram;
708 bufferram = this->base + area;
710 bufferram += onenand_bufferram_offset(mtd, area);
712 if (ONENAND_CHECK_BYTE_ACCESS(count)) {
715 /* Align with word(16-bit) size */
718 /* Read word and save byte */
719 word = this->read_word(bufferram + offset + count);
720 buffer[count] = (word & 0xff);
723 memcpy(buffer, bufferram + offset, count);
729 * onenand_sync_read_bufferram - [OneNAND Interface] Read the bufferram area with Sync. Burst mode
730 * @param mtd MTD data structure
731 * @param area BufferRAM area
732 * @param buffer the databuffer to put/get data
733 * @param offset offset to read from or write to
734 * @param count number of bytes to read/write
736 * Read the BufferRAM area with Sync. Burst Mode
738 static int onenand_sync_read_bufferram(struct mtd_info *mtd, int area,
739 unsigned char *buffer, int offset, size_t count)
741 struct onenand_chip *this = mtd->priv;
742 void __iomem *bufferram;
744 bufferram = this->base + area;
746 bufferram += onenand_bufferram_offset(mtd, area);
748 this->mmcontrol(mtd, ONENAND_SYS_CFG1_SYNC_READ);
750 if (ONENAND_CHECK_BYTE_ACCESS(count)) {
753 /* Align with word(16-bit) size */
756 /* Read word and save byte */
757 word = this->read_word(bufferram + offset + count);
758 buffer[count] = (word & 0xff);
761 memcpy(buffer, bufferram + offset, count);
763 this->mmcontrol(mtd, 0);
769 * onenand_write_bufferram - [OneNAND Interface] Write the bufferram area
770 * @param mtd MTD data structure
771 * @param area BufferRAM area
772 * @param buffer the databuffer to put/get data
773 * @param offset offset to read from or write to
774 * @param count number of bytes to read/write
776 * Write the BufferRAM area
778 static int onenand_write_bufferram(struct mtd_info *mtd, int area,
779 const unsigned char *buffer, int offset, size_t count)
781 struct onenand_chip *this = mtd->priv;
782 void __iomem *bufferram;
784 bufferram = this->base + area;
786 bufferram += onenand_bufferram_offset(mtd, area);
788 if (ONENAND_CHECK_BYTE_ACCESS(count)) {
792 /* Align with word(16-bit) size */
795 /* Calculate byte access offset */
796 byte_offset = offset + count;
798 /* Read word and save byte */
799 word = this->read_word(bufferram + byte_offset);
800 word = (word & ~0xff) | buffer[count];
801 this->write_word(word, bufferram + byte_offset);
804 memcpy(bufferram + offset, buffer, count);
810 * onenand_get_2x_blockpage - [GENERIC] Get blockpage at 2x program mode
811 * @param mtd MTD data structure
812 * @param addr address to check
813 * @return blockpage address
815 * Get blockpage address at 2x program mode
817 static int onenand_get_2x_blockpage(struct mtd_info *mtd, loff_t addr)
819 struct onenand_chip *this = mtd->priv;
820 int blockpage, block, page;
822 /* Calculate the even block number */
823 block = (int) (addr >> this->erase_shift) & ~1;
824 /* Is it the odd plane? */
825 if (addr & this->writesize)
827 page = (int) (addr >> (this->page_shift + 1)) & this->page_mask;
828 blockpage = (block << 7) | page;
834 * onenand_check_bufferram - [GENERIC] Check BufferRAM information
835 * @param mtd MTD data structure
836 * @param addr address to check
837 * @return 1 if there are valid data, otherwise 0
839 * Check bufferram if there is data we required
841 static int onenand_check_bufferram(struct mtd_info *mtd, loff_t addr)
843 struct onenand_chip *this = mtd->priv;
844 int blockpage, found = 0;
847 if (ONENAND_IS_2PLANE(this))
848 blockpage = onenand_get_2x_blockpage(mtd, addr);
850 blockpage = (int) (addr >> this->page_shift);
852 /* Is there valid data? */
853 i = ONENAND_CURRENT_BUFFERRAM(this);
854 if (this->bufferram[i].blockpage == blockpage)
857 /* Check another BufferRAM */
858 i = ONENAND_NEXT_BUFFERRAM(this);
859 if (this->bufferram[i].blockpage == blockpage) {
860 ONENAND_SET_NEXT_BUFFERRAM(this);
865 if (found && ONENAND_IS_DDP(this)) {
866 /* Select DataRAM for DDP */
867 int block = onenand_block(this, addr);
868 int value = onenand_bufferram_address(this, block);
869 this->write_word(value, this->base + ONENAND_REG_START_ADDRESS2);
876 * onenand_update_bufferram - [GENERIC] Update BufferRAM information
877 * @param mtd MTD data structure
878 * @param addr address to update
879 * @param valid valid flag
881 * Update BufferRAM information
883 static void onenand_update_bufferram(struct mtd_info *mtd, loff_t addr,
886 struct onenand_chip *this = mtd->priv;
890 if (ONENAND_IS_2PLANE(this))
891 blockpage = onenand_get_2x_blockpage(mtd, addr);
893 blockpage = (int) (addr >> this->page_shift);
895 /* Invalidate another BufferRAM */
896 i = ONENAND_NEXT_BUFFERRAM(this);
897 if (this->bufferram[i].blockpage == blockpage)
898 this->bufferram[i].blockpage = -1;
900 /* Update BufferRAM */
901 i = ONENAND_CURRENT_BUFFERRAM(this);
903 this->bufferram[i].blockpage = blockpage;
905 this->bufferram[i].blockpage = -1;
909 * onenand_invalidate_bufferram - [GENERIC] Invalidate BufferRAM information
910 * @param mtd MTD data structure
911 * @param addr start address to invalidate
912 * @param len length to invalidate
914 * Invalidate BufferRAM information
916 static void onenand_invalidate_bufferram(struct mtd_info *mtd, loff_t addr,
919 struct onenand_chip *this = mtd->priv;
921 loff_t end_addr = addr + len;
923 /* Invalidate BufferRAM */
924 for (i = 0; i < MAX_BUFFERRAM; i++) {
925 loff_t buf_addr = this->bufferram[i].blockpage << this->page_shift;
926 if (buf_addr >= addr && buf_addr < end_addr)
927 this->bufferram[i].blockpage = -1;
932 * onenand_get_device - [GENERIC] Get chip for selected access
933 * @param mtd MTD device structure
934 * @param new_state the state which is requested
936 * Get the device and lock it for exclusive access
938 static int onenand_get_device(struct mtd_info *mtd, int new_state)
940 struct onenand_chip *this = mtd->priv;
941 DECLARE_WAITQUEUE(wait, current);
944 * Grab the lock and see if the device is available
947 spin_lock(&this->chip_lock);
948 if (this->state == FL_READY) {
949 this->state = new_state;
950 spin_unlock(&this->chip_lock);
951 if (new_state != FL_PM_SUSPENDED && this->enable)
955 if (new_state == FL_PM_SUSPENDED) {
956 spin_unlock(&this->chip_lock);
957 return (this->state == FL_PM_SUSPENDED) ? 0 : -EAGAIN;
959 set_current_state(TASK_UNINTERRUPTIBLE);
960 add_wait_queue(&this->wq, &wait);
961 spin_unlock(&this->chip_lock);
963 remove_wait_queue(&this->wq, &wait);
970 * onenand_release_device - [GENERIC] release chip
971 * @param mtd MTD device structure
973 * Deselect, release chip lock and wake up anyone waiting on the device
975 static void onenand_release_device(struct mtd_info *mtd)
977 struct onenand_chip *this = mtd->priv;
979 if (this->state != FL_PM_SUSPENDED && this->disable)
981 /* Release the chip */
982 spin_lock(&this->chip_lock);
983 this->state = FL_READY;
985 spin_unlock(&this->chip_lock);
989 * onenand_transfer_auto_oob - [Internal] oob auto-placement transfer
990 * @param mtd MTD device structure
991 * @param buf destination address
992 * @param column oob offset to read from
993 * @param thislen oob length to read
995 static int onenand_transfer_auto_oob(struct mtd_info *mtd, uint8_t *buf, int column,
998 struct onenand_chip *this = mtd->priv;
999 struct nand_oobfree *free;
1000 int readcol = column;
1001 int readend = column + thislen;
1004 uint8_t *oob_buf = this->oob_buf;
1006 free = this->ecclayout->oobfree;
1007 for (i = 0; i < MTD_MAX_OOBFREE_ENTRIES && free->length; i++, free++) {
1008 if (readcol >= lastgap)
1009 readcol += free->offset - lastgap;
1010 if (readend >= lastgap)
1011 readend += free->offset - lastgap;
1012 lastgap = free->offset + free->length;
1014 this->read_bufferram(mtd, ONENAND_SPARERAM, oob_buf, 0, mtd->oobsize);
1015 free = this->ecclayout->oobfree;
1016 for (i = 0; i < MTD_MAX_OOBFREE_ENTRIES && free->length; i++, free++) {
1017 int free_end = free->offset + free->length;
1018 if (free->offset < readend && free_end > readcol) {
1019 int st = max_t(int,free->offset,readcol);
1020 int ed = min_t(int,free_end,readend);
1022 memcpy(buf, oob_buf + st, n);
1024 } else if (column == 0)
1031 * onenand_recover_lsb - [Flex-OneNAND] Recover LSB page data
1032 * @param mtd MTD device structure
1033 * @param addr address to recover
1034 * @param status return value from onenand_wait / onenand_bbt_wait
1036 * MLC NAND Flash cell has paired pages - LSB page and MSB page. LSB page has
1037 * lower page address and MSB page has higher page address in paired pages.
1038 * If power off occurs during MSB page program, the paired LSB page data can
1039 * become corrupt. LSB page recovery read is a way to read LSB page though page
1040 * data are corrupted. When uncorrectable error occurs as a result of LSB page
1041 * read after power up, issue LSB page recovery read.
1043 static int onenand_recover_lsb(struct mtd_info *mtd, loff_t addr, int status)
1045 struct onenand_chip *this = mtd->priv;
1048 /* Recovery is only for Flex-OneNAND */
1049 if (!FLEXONENAND(this))
1052 /* check if we failed due to uncorrectable error */
1053 if (status != -EBADMSG && status != ONENAND_BBT_READ_ECC_ERROR)
1056 /* check if address lies in MLC region */
1057 i = flexonenand_region(mtd, addr);
1058 if (mtd->eraseregions[i].erasesize < (1 << this->erase_shift))
1061 /* We are attempting to reread, so decrement stats.failed
1062 * which was incremented by onenand_wait due to read failure
1064 printk(KERN_INFO "%s: Attempting to recover from uncorrectable read\n",
1066 mtd->ecc_stats.failed--;
1068 /* Issue the LSB page recovery command */
1069 this->command(mtd, FLEXONENAND_CMD_RECOVER_LSB, addr, this->writesize);
1070 return this->wait(mtd, FL_READING);
1074 * onenand_mlc_read_ops_nolock - MLC OneNAND read main and/or out-of-band
1075 * @param mtd MTD device structure
1076 * @param from offset to read from
1077 * @param ops: oob operation description structure
1079 * MLC OneNAND / Flex-OneNAND has 4KB page size and 4KB dataram.
1080 * So, read-while-load is not present.
1082 static int onenand_mlc_read_ops_nolock(struct mtd_info *mtd, loff_t from,
1083 struct mtd_oob_ops *ops)
1085 struct onenand_chip *this = mtd->priv;
1086 struct mtd_ecc_stats stats;
1087 size_t len = ops->len;
1088 size_t ooblen = ops->ooblen;
1089 u_char *buf = ops->datbuf;
1090 u_char *oobbuf = ops->oobbuf;
1091 int read = 0, column, thislen;
1092 int oobread = 0, oobcolumn, thisooblen, oobsize;
1094 int writesize = this->writesize;
1096 DEBUG(MTD_DEBUG_LEVEL3, "%s: from = 0x%08x, len = %i\n",
1097 __func__, (unsigned int) from, (int) len);
1099 if (ops->mode == MTD_OOB_AUTO)
1100 oobsize = this->ecclayout->oobavail;
1102 oobsize = mtd->oobsize;
1104 oobcolumn = from & (mtd->oobsize - 1);
1106 /* Do not allow reads past end of device */
1107 if (from + len > mtd->size) {
1108 printk(KERN_ERR "%s: Attempt read beyond end of device\n",
1115 stats = mtd->ecc_stats;
1117 while (read < len) {
1120 thislen = min_t(int, writesize, len - read);
1122 column = from & (writesize - 1);
1123 if (column + thislen > writesize)
1124 thislen = writesize - column;
1126 if (!onenand_check_bufferram(mtd, from)) {
1127 this->command(mtd, ONENAND_CMD_READ, from, writesize);
1129 ret = this->wait(mtd, FL_READING);
1131 ret = onenand_recover_lsb(mtd, from, ret);
1132 onenand_update_bufferram(mtd, from, !ret);
1133 if (ret == -EBADMSG)
1139 this->read_bufferram(mtd, ONENAND_DATARAM, buf, column, thislen);
1141 thisooblen = oobsize - oobcolumn;
1142 thisooblen = min_t(int, thisooblen, ooblen - oobread);
1144 if (ops->mode == MTD_OOB_AUTO)
1145 onenand_transfer_auto_oob(mtd, oobbuf, oobcolumn, thisooblen);
1147 this->read_bufferram(mtd, ONENAND_SPARERAM, oobbuf, oobcolumn, thisooblen);
1148 oobread += thisooblen;
1149 oobbuf += thisooblen;
1162 * Return success, if no ECC failures, else -EBADMSG
1163 * fs driver will take care of that, because
1164 * retlen == desired len and result == -EBADMSG
1167 ops->oobretlen = oobread;
1172 if (mtd->ecc_stats.failed - stats.failed)
1175 return mtd->ecc_stats.corrected - stats.corrected ? -EUCLEAN : 0;
1179 * onenand_read_ops_nolock - [OneNAND Interface] OneNAND read main and/or out-of-band
1180 * @param mtd MTD device structure
1181 * @param from offset to read from
1182 * @param ops: oob operation description structure
1184 * OneNAND read main and/or out-of-band data
1186 static int onenand_read_ops_nolock(struct mtd_info *mtd, loff_t from,
1187 struct mtd_oob_ops *ops)
1189 struct onenand_chip *this = mtd->priv;
1190 struct mtd_ecc_stats stats;
1191 size_t len = ops->len;
1192 size_t ooblen = ops->ooblen;
1193 u_char *buf = ops->datbuf;
1194 u_char *oobbuf = ops->oobbuf;
1195 int read = 0, column, thislen;
1196 int oobread = 0, oobcolumn, thisooblen, oobsize;
1197 int ret = 0, boundary = 0;
1198 int writesize = this->writesize;
1200 DEBUG(MTD_DEBUG_LEVEL3, "%s: from = 0x%08x, len = %i\n",
1201 __func__, (unsigned int) from, (int) len);
1203 if (ops->mode == MTD_OOB_AUTO)
1204 oobsize = this->ecclayout->oobavail;
1206 oobsize = mtd->oobsize;
1208 oobcolumn = from & (mtd->oobsize - 1);
1210 /* Do not allow reads past end of device */
1211 if ((from + len) > mtd->size) {
1212 printk(KERN_ERR "%s: Attempt read beyond end of device\n",
1219 stats = mtd->ecc_stats;
1221 /* Read-while-load method */
1223 /* Do first load to bufferRAM */
1225 if (!onenand_check_bufferram(mtd, from)) {
1226 this->command(mtd, ONENAND_CMD_READ, from, writesize);
1227 ret = this->wait(mtd, FL_READING);
1228 onenand_update_bufferram(mtd, from, !ret);
1229 if (ret == -EBADMSG)
1234 thislen = min_t(int, writesize, len - read);
1235 column = from & (writesize - 1);
1236 if (column + thislen > writesize)
1237 thislen = writesize - column;
1240 /* If there is more to load then start next load */
1242 if (read + thislen < len) {
1243 this->command(mtd, ONENAND_CMD_READ, from, writesize);
1245 * Chip boundary handling in DDP
1246 * Now we issued chip 1 read and pointed chip 1
1247 * bufferram so we have to point chip 0 bufferram.
1249 if (ONENAND_IS_DDP(this) &&
1250 unlikely(from == (this->chipsize >> 1))) {
1251 this->write_word(ONENAND_DDP_CHIP0, this->base + ONENAND_REG_START_ADDRESS2);
1255 ONENAND_SET_PREV_BUFFERRAM(this);
1257 /* While load is going, read from last bufferRAM */
1258 this->read_bufferram(mtd, ONENAND_DATARAM, buf, column, thislen);
1260 /* Read oob area if needed */
1262 thisooblen = oobsize - oobcolumn;
1263 thisooblen = min_t(int, thisooblen, ooblen - oobread);
1265 if (ops->mode == MTD_OOB_AUTO)
1266 onenand_transfer_auto_oob(mtd, oobbuf, oobcolumn, thisooblen);
1268 this->read_bufferram(mtd, ONENAND_SPARERAM, oobbuf, oobcolumn, thisooblen);
1269 oobread += thisooblen;
1270 oobbuf += thisooblen;
1274 /* See if we are done */
1278 /* Set up for next read from bufferRAM */
1279 if (unlikely(boundary))
1280 this->write_word(ONENAND_DDP_CHIP1, this->base + ONENAND_REG_START_ADDRESS2);
1281 ONENAND_SET_NEXT_BUFFERRAM(this);
1283 thislen = min_t(int, writesize, len - read);
1286 /* Now wait for load */
1287 ret = this->wait(mtd, FL_READING);
1288 onenand_update_bufferram(mtd, from, !ret);
1289 if (ret == -EBADMSG)
1294 * Return success, if no ECC failures, else -EBADMSG
1295 * fs driver will take care of that, because
1296 * retlen == desired len and result == -EBADMSG
1299 ops->oobretlen = oobread;
1304 if (mtd->ecc_stats.failed - stats.failed)
1307 return mtd->ecc_stats.corrected - stats.corrected ? -EUCLEAN : 0;
1311 * onenand_read_oob_nolock - [MTD Interface] OneNAND read out-of-band
1312 * @param mtd MTD device structure
1313 * @param from offset to read from
1314 * @param ops: oob operation description structure
1316 * OneNAND read out-of-band data from the spare area
1318 static int onenand_read_oob_nolock(struct mtd_info *mtd, loff_t from,
1319 struct mtd_oob_ops *ops)
1321 struct onenand_chip *this = mtd->priv;
1322 struct mtd_ecc_stats stats;
1323 int read = 0, thislen, column, oobsize;
1324 size_t len = ops->ooblen;
1325 mtd_oob_mode_t mode = ops->mode;
1326 u_char *buf = ops->oobbuf;
1327 int ret = 0, readcmd;
1329 from += ops->ooboffs;
1331 DEBUG(MTD_DEBUG_LEVEL3, "%s: from = 0x%08x, len = %i\n",
1332 __func__, (unsigned int) from, (int) len);
1334 /* Initialize return length value */
1337 if (mode == MTD_OOB_AUTO)
1338 oobsize = this->ecclayout->oobavail;
1340 oobsize = mtd->oobsize;
1342 column = from & (mtd->oobsize - 1);
1344 if (unlikely(column >= oobsize)) {
1345 printk(KERN_ERR "%s: Attempted to start read outside oob\n",
1350 /* Do not allow reads past end of device */
1351 if (unlikely(from >= mtd->size ||
1352 column + len > ((mtd->size >> this->page_shift) -
1353 (from >> this->page_shift)) * oobsize)) {
1354 printk(KERN_ERR "%s: Attempted to read beyond end of device\n",
1359 stats = mtd->ecc_stats;
1361 readcmd = ONENAND_IS_4KB_PAGE(this) ? ONENAND_CMD_READ : ONENAND_CMD_READOOB;
1363 while (read < len) {
1366 thislen = oobsize - column;
1367 thislen = min_t(int, thislen, len);
1369 this->command(mtd, readcmd, from, mtd->oobsize);
1371 onenand_update_bufferram(mtd, from, 0);
1373 ret = this->wait(mtd, FL_READING);
1375 ret = onenand_recover_lsb(mtd, from, ret);
1377 if (ret && ret != -EBADMSG) {
1378 printk(KERN_ERR "%s: read failed = 0x%x\n",
1383 if (mode == MTD_OOB_AUTO)
1384 onenand_transfer_auto_oob(mtd, buf, column, thislen);
1386 this->read_bufferram(mtd, ONENAND_SPARERAM, buf, column, thislen);
1398 from += mtd->writesize;
1403 ops->oobretlen = read;
1408 if (mtd->ecc_stats.failed - stats.failed)
1415 * onenand_read - [MTD Interface] Read data from flash
1416 * @param mtd MTD device structure
1417 * @param from offset to read from
1418 * @param len number of bytes to read
1419 * @param retlen pointer to variable to store the number of read bytes
1420 * @param buf the databuffer to put data
1424 static int onenand_read(struct mtd_info *mtd, loff_t from, size_t len,
1425 size_t *retlen, u_char *buf)
1427 struct onenand_chip *this = mtd->priv;
1428 struct mtd_oob_ops ops = {
1436 onenand_get_device(mtd, FL_READING);
1437 ret = ONENAND_IS_4KB_PAGE(this) ?
1438 onenand_mlc_read_ops_nolock(mtd, from, &ops) :
1439 onenand_read_ops_nolock(mtd, from, &ops);
1440 onenand_release_device(mtd);
1442 *retlen = ops.retlen;
1447 * onenand_read_oob - [MTD Interface] Read main and/or out-of-band
1448 * @param mtd: MTD device structure
1449 * @param from: offset to read from
1450 * @param ops: oob operation description structure
1452 * Read main and/or out-of-band
1454 static int onenand_read_oob(struct mtd_info *mtd, loff_t from,
1455 struct mtd_oob_ops *ops)
1457 struct onenand_chip *this = mtd->priv;
1460 switch (ops->mode) {
1465 /* Not implemented yet */
1470 onenand_get_device(mtd, FL_READING);
1472 ret = ONENAND_IS_4KB_PAGE(this) ?
1473 onenand_mlc_read_ops_nolock(mtd, from, ops) :
1474 onenand_read_ops_nolock(mtd, from, ops);
1476 ret = onenand_read_oob_nolock(mtd, from, ops);
1477 onenand_release_device(mtd);
1483 * onenand_bbt_wait - [DEFAULT] wait until the command is done
1484 * @param mtd MTD device structure
1485 * @param state state to select the max. timeout value
1487 * Wait for command done.
1489 static int onenand_bbt_wait(struct mtd_info *mtd, int state)
1491 struct onenand_chip *this = mtd->priv;
1492 unsigned long timeout;
1493 unsigned int interrupt, ctrl, ecc, addr1, addr8;
1495 /* The 20 msec is enough */
1496 timeout = jiffies + msecs_to_jiffies(20);
1497 while (time_before(jiffies, timeout)) {
1498 interrupt = this->read_word(this->base + ONENAND_REG_INTERRUPT);
1499 if (interrupt & ONENAND_INT_MASTER)
1502 /* To get correct interrupt status in timeout case */
1503 interrupt = this->read_word(this->base + ONENAND_REG_INTERRUPT);
1504 ctrl = this->read_word(this->base + ONENAND_REG_CTRL_STATUS);
1505 addr1 = this->read_word(this->base + ONENAND_REG_START_ADDRESS1);
1506 addr8 = this->read_word(this->base + ONENAND_REG_START_ADDRESS8);
1508 if (interrupt & ONENAND_INT_READ) {
1509 ecc = onenand_read_ecc(this);
1510 if (ecc & ONENAND_ECC_2BIT_ALL) {
1511 printk(KERN_DEBUG "%s: ecc 0x%04x ctrl 0x%04x "
1512 "intr 0x%04x addr1 %#x addr8 %#x\n",
1513 __func__, ecc, ctrl, interrupt, addr1, addr8);
1514 return ONENAND_BBT_READ_ECC_ERROR;
1517 printk(KERN_ERR "%s: read timeout! ctrl 0x%04x "
1518 "intr 0x%04x addr1 %#x addr8 %#x\n",
1519 __func__, ctrl, interrupt, addr1, addr8);
1520 return ONENAND_BBT_READ_FATAL_ERROR;
1523 /* Initial bad block case: 0x2400 or 0x0400 */
1524 if (ctrl & ONENAND_CTRL_ERROR) {
1525 printk(KERN_DEBUG "%s: ctrl 0x%04x intr 0x%04x addr1 %#x "
1526 "addr8 %#x\n", __func__, ctrl, interrupt, addr1, addr8);
1527 return ONENAND_BBT_READ_ERROR;
1534 * onenand_bbt_read_oob - [MTD Interface] OneNAND read out-of-band for bbt scan
1535 * @param mtd MTD device structure
1536 * @param from offset to read from
1537 * @param ops oob operation description structure
1539 * OneNAND read out-of-band data from the spare area for bbt scan
1541 int onenand_bbt_read_oob(struct mtd_info *mtd, loff_t from,
1542 struct mtd_oob_ops *ops)
1544 struct onenand_chip *this = mtd->priv;
1545 int read = 0, thislen, column;
1546 int ret = 0, readcmd;
1547 size_t len = ops->ooblen;
1548 u_char *buf = ops->oobbuf;
1550 DEBUG(MTD_DEBUG_LEVEL3, "%s: from = 0x%08x, len = %zi\n",
1551 __func__, (unsigned int) from, len);
1553 /* Initialize return value */
1556 /* Do not allow reads past end of device */
1557 if (unlikely((from + len) > mtd->size)) {
1558 printk(KERN_ERR "%s: Attempt read beyond end of device\n",
1560 return ONENAND_BBT_READ_FATAL_ERROR;
1563 /* Grab the lock and see if the device is available */
1564 onenand_get_device(mtd, FL_READING);
1566 column = from & (mtd->oobsize - 1);
1568 readcmd = ONENAND_IS_4KB_PAGE(this) ? ONENAND_CMD_READ : ONENAND_CMD_READOOB;
1570 while (read < len) {
1573 thislen = mtd->oobsize - column;
1574 thislen = min_t(int, thislen, len);
1576 this->command(mtd, readcmd, from, mtd->oobsize);
1578 onenand_update_bufferram(mtd, from, 0);
1580 ret = this->bbt_wait(mtd, FL_READING);
1582 ret = onenand_recover_lsb(mtd, from, ret);
1587 this->read_bufferram(mtd, ONENAND_SPARERAM, buf, column, thislen);
1596 /* Update Page size */
1597 from += this->writesize;
1602 /* Deselect and wake up anyone waiting on the device */
1603 onenand_release_device(mtd);
1605 ops->oobretlen = read;
1609 #ifdef CONFIG_MTD_ONENAND_VERIFY_WRITE
1611 * onenand_verify_oob - [GENERIC] verify the oob contents after a write
1612 * @param mtd MTD device structure
1613 * @param buf the databuffer to verify
1614 * @param to offset to read from
1616 static int onenand_verify_oob(struct mtd_info *mtd, const u_char *buf, loff_t to)
1618 struct onenand_chip *this = mtd->priv;
1619 u_char *oob_buf = this->oob_buf;
1620 int status, i, readcmd;
1622 readcmd = ONENAND_IS_4KB_PAGE(this) ? ONENAND_CMD_READ : ONENAND_CMD_READOOB;
1624 this->command(mtd, readcmd, to, mtd->oobsize);
1625 onenand_update_bufferram(mtd, to, 0);
1626 status = this->wait(mtd, FL_READING);
1630 this->read_bufferram(mtd, ONENAND_SPARERAM, oob_buf, 0, mtd->oobsize);
1631 for (i = 0; i < mtd->oobsize; i++)
1632 if (buf[i] != 0xFF && buf[i] != oob_buf[i])
1639 * onenand_verify - [GENERIC] verify the chip contents after a write
1640 * @param mtd MTD device structure
1641 * @param buf the databuffer to verify
1642 * @param addr offset to read from
1643 * @param len number of bytes to read and compare
1645 static int onenand_verify(struct mtd_info *mtd, const u_char *buf, loff_t addr, size_t len)
1647 struct onenand_chip *this = mtd->priv;
1649 int thislen, column;
1651 column = addr & (this->writesize - 1);
1654 thislen = min_t(int, this->writesize - column, len);
1656 this->command(mtd, ONENAND_CMD_READ, addr, this->writesize);
1658 onenand_update_bufferram(mtd, addr, 0);
1660 ret = this->wait(mtd, FL_READING);
1664 onenand_update_bufferram(mtd, addr, 1);
1666 this->read_bufferram(mtd, ONENAND_DATARAM, this->verify_buf, 0, mtd->writesize);
1668 if (memcmp(buf, this->verify_buf + column, thislen))
1680 #define onenand_verify(...) (0)
1681 #define onenand_verify_oob(...) (0)
1684 #define NOTALIGNED(x) ((x & (this->subpagesize - 1)) != 0)
1686 static void onenand_panic_wait(struct mtd_info *mtd)
1688 struct onenand_chip *this = mtd->priv;
1689 unsigned int interrupt;
1692 for (i = 0; i < 2000; i++) {
1693 interrupt = this->read_word(this->base + ONENAND_REG_INTERRUPT);
1694 if (interrupt & ONENAND_INT_MASTER)
1701 * onenand_panic_write - [MTD Interface] write buffer to FLASH in a panic context
1702 * @param mtd MTD device structure
1703 * @param to offset to write to
1704 * @param len number of bytes to write
1705 * @param retlen pointer to variable to store the number of written bytes
1706 * @param buf the data to write
1710 static int onenand_panic_write(struct mtd_info *mtd, loff_t to, size_t len,
1711 size_t *retlen, const u_char *buf)
1713 struct onenand_chip *this = mtd->priv;
1714 int column, subpage;
1718 if (this->state == FL_PM_SUSPENDED)
1721 /* Wait for any existing operation to clear */
1722 onenand_panic_wait(mtd);
1724 DEBUG(MTD_DEBUG_LEVEL3, "%s: to = 0x%08x, len = %i\n",
1725 __func__, (unsigned int) to, (int) len);
1727 /* Initialize retlen, in case of early exit */
1730 /* Do not allow writes past end of device */
1731 if (unlikely((to + len) > mtd->size)) {
1732 printk(KERN_ERR "%s: Attempt write to past end of device\n",
1737 /* Reject writes, which are not page aligned */
1738 if (unlikely(NOTALIGNED(to) || NOTALIGNED(len))) {
1739 printk(KERN_ERR "%s: Attempt to write not page aligned data\n",
1744 column = to & (mtd->writesize - 1);
1746 /* Loop until all data write */
1747 while (written < len) {
1748 int thislen = min_t(int, mtd->writesize - column, len - written);
1749 u_char *wbuf = (u_char *) buf;
1751 this->command(mtd, ONENAND_CMD_BUFFERRAM, to, thislen);
1753 /* Partial page write */
1754 subpage = thislen < mtd->writesize;
1756 memset(this->page_buf, 0xff, mtd->writesize);
1757 memcpy(this->page_buf + column, buf, thislen);
1758 wbuf = this->page_buf;
1761 this->write_bufferram(mtd, ONENAND_DATARAM, wbuf, 0, mtd->writesize);
1762 this->write_bufferram(mtd, ONENAND_SPARERAM, ffchars, 0, mtd->oobsize);
1764 this->command(mtd, ONENAND_CMD_PROG, to, mtd->writesize);
1766 onenand_panic_wait(mtd);
1768 /* In partial page write we don't update bufferram */
1769 onenand_update_bufferram(mtd, to, !ret && !subpage);
1770 if (ONENAND_IS_2PLANE(this)) {
1771 ONENAND_SET_BUFFERRAM1(this);
1772 onenand_update_bufferram(mtd, to + this->writesize, !ret && !subpage);
1776 printk(KERN_ERR "%s: write failed %d\n", __func__, ret);
1795 * onenand_fill_auto_oob - [Internal] oob auto-placement transfer
1796 * @param mtd MTD device structure
1797 * @param oob_buf oob buffer
1798 * @param buf source address
1799 * @param column oob offset to write to
1800 * @param thislen oob length to write
1802 static int onenand_fill_auto_oob(struct mtd_info *mtd, u_char *oob_buf,
1803 const u_char *buf, int column, int thislen)
1805 struct onenand_chip *this = mtd->priv;
1806 struct nand_oobfree *free;
1807 int writecol = column;
1808 int writeend = column + thislen;
1812 free = this->ecclayout->oobfree;
1813 for (i = 0; i < MTD_MAX_OOBFREE_ENTRIES && free->length; i++, free++) {
1814 if (writecol >= lastgap)
1815 writecol += free->offset - lastgap;
1816 if (writeend >= lastgap)
1817 writeend += free->offset - lastgap;
1818 lastgap = free->offset + free->length;
1820 free = this->ecclayout->oobfree;
1821 for (i = 0; i < MTD_MAX_OOBFREE_ENTRIES && free->length; i++, free++) {
1822 int free_end = free->offset + free->length;
1823 if (free->offset < writeend && free_end > writecol) {
1824 int st = max_t(int,free->offset,writecol);
1825 int ed = min_t(int,free_end,writeend);
1827 memcpy(oob_buf + st, buf, n);
1829 } else if (column == 0)
1836 * onenand_write_ops_nolock - [OneNAND Interface] write main and/or out-of-band
1837 * @param mtd MTD device structure
1838 * @param to offset to write to
1839 * @param ops oob operation description structure
1841 * Write main and/or oob with ECC
1843 static int onenand_write_ops_nolock(struct mtd_info *mtd, loff_t to,
1844 struct mtd_oob_ops *ops)
1846 struct onenand_chip *this = mtd->priv;
1847 int written = 0, column, thislen = 0, subpage = 0;
1848 int prev = 0, prevlen = 0, prev_subpage = 0, first = 1;
1849 int oobwritten = 0, oobcolumn, thisooblen, oobsize;
1850 size_t len = ops->len;
1851 size_t ooblen = ops->ooblen;
1852 const u_char *buf = ops->datbuf;
1853 const u_char *oob = ops->oobbuf;
1857 DEBUG(MTD_DEBUG_LEVEL3, "%s: to = 0x%08x, len = %i\n",
1858 __func__, (unsigned int) to, (int) len);
1860 /* Initialize retlen, in case of early exit */
1864 /* Do not allow writes past end of device */
1865 if (unlikely((to + len) > mtd->size)) {
1866 printk(KERN_ERR "%s: Attempt write to past end of device\n",
1871 /* Reject writes, which are not page aligned */
1872 if (unlikely(NOTALIGNED(to) || NOTALIGNED(len))) {
1873 printk(KERN_ERR "%s: Attempt to write not page aligned data\n",
1878 /* Check zero length */
1882 if (ops->mode == MTD_OOB_AUTO)
1883 oobsize = this->ecclayout->oobavail;
1885 oobsize = mtd->oobsize;
1887 oobcolumn = to & (mtd->oobsize - 1);
1889 column = to & (mtd->writesize - 1);
1891 /* Loop until all data write */
1893 if (written < len) {
1894 u_char *wbuf = (u_char *) buf;
1896 thislen = min_t(int, mtd->writesize - column, len - written);
1897 thisooblen = min_t(int, oobsize - oobcolumn, ooblen - oobwritten);
1901 this->command(mtd, ONENAND_CMD_BUFFERRAM, to, thislen);
1903 /* Partial page write */
1904 subpage = thislen < mtd->writesize;
1906 memset(this->page_buf, 0xff, mtd->writesize);
1907 memcpy(this->page_buf + column, buf, thislen);
1908 wbuf = this->page_buf;
1911 this->write_bufferram(mtd, ONENAND_DATARAM, wbuf, 0, mtd->writesize);
1914 oobbuf = this->oob_buf;
1916 /* We send data to spare ram with oobsize
1917 * to prevent byte access */
1918 memset(oobbuf, 0xff, mtd->oobsize);
1919 if (ops->mode == MTD_OOB_AUTO)
1920 onenand_fill_auto_oob(mtd, oobbuf, oob, oobcolumn, thisooblen);
1922 memcpy(oobbuf + oobcolumn, oob, thisooblen);
1924 oobwritten += thisooblen;
1928 oobbuf = (u_char *) ffchars;
1930 this->write_bufferram(mtd, ONENAND_SPARERAM, oobbuf, 0, mtd->oobsize);
1932 ONENAND_SET_NEXT_BUFFERRAM(this);
1935 * 2 PLANE, MLC, and Flex-OneNAND do not support
1936 * write-while-program feature.
1938 if (!ONENAND_IS_2PLANE(this) && !ONENAND_IS_4KB_PAGE(this) && !first) {
1939 ONENAND_SET_PREV_BUFFERRAM(this);
1941 ret = this->wait(mtd, FL_WRITING);
1943 /* In partial page write we don't update bufferram */
1944 onenand_update_bufferram(mtd, prev, !ret && !prev_subpage);
1947 printk(KERN_ERR "%s: write failed %d\n",
1952 if (written == len) {
1953 /* Only check verify write turn on */
1954 ret = onenand_verify(mtd, buf - len, to - len, len);
1956 printk(KERN_ERR "%s: verify failed %d\n",
1961 ONENAND_SET_NEXT_BUFFERRAM(this);
1965 cmd = ONENAND_CMD_PROG;
1967 /* Exclude 1st OTP and OTP blocks for cache program feature */
1968 if (ONENAND_IS_CACHE_PROGRAM(this) &&
1969 likely(onenand_block(this, to) != 0) &&
1970 ONENAND_IS_4KB_PAGE(this) &&
1971 ((written + thislen) < len)) {
1972 cmd = ONENAND_CMD_2X_CACHE_PROG;
1976 this->command(mtd, cmd, to, mtd->writesize);
1979 * 2 PLANE, MLC, and Flex-OneNAND wait here
1981 if (ONENAND_IS_2PLANE(this) || ONENAND_IS_4KB_PAGE(this)) {
1982 ret = this->wait(mtd, FL_WRITING);
1984 /* In partial page write we don't update bufferram */
1985 onenand_update_bufferram(mtd, to, !ret && !subpage);
1987 printk(KERN_ERR "%s: write failed %d\n",
1992 /* Only check verify write turn on */
1993 ret = onenand_verify(mtd, buf, to, thislen);
1995 printk(KERN_ERR "%s: verify failed %d\n",
2009 prev_subpage = subpage;
2017 /* In error case, clear all bufferrams */
2019 onenand_invalidate_bufferram(mtd, 0, -1);
2021 ops->retlen = written;
2022 ops->oobretlen = oobwritten;
2029 * onenand_write_oob_nolock - [Internal] OneNAND write out-of-band
2030 * @param mtd MTD device structure
2031 * @param to offset to write to
2032 * @param len number of bytes to write
2033 * @param retlen pointer to variable to store the number of written bytes
2034 * @param buf the data to write
2035 * @param mode operation mode
2037 * OneNAND write out-of-band
2039 static int onenand_write_oob_nolock(struct mtd_info *mtd, loff_t to,
2040 struct mtd_oob_ops *ops)
2042 struct onenand_chip *this = mtd->priv;
2043 int column, ret = 0, oobsize;
2044 int written = 0, oobcmd;
2046 size_t len = ops->ooblen;
2047 const u_char *buf = ops->oobbuf;
2048 mtd_oob_mode_t mode = ops->mode;
2052 DEBUG(MTD_DEBUG_LEVEL3, "%s: to = 0x%08x, len = %i\n",
2053 __func__, (unsigned int) to, (int) len);
2055 /* Initialize retlen, in case of early exit */
2058 if (mode == MTD_OOB_AUTO)
2059 oobsize = this->ecclayout->oobavail;
2061 oobsize = mtd->oobsize;
2063 column = to & (mtd->oobsize - 1);
2065 if (unlikely(column >= oobsize)) {
2066 printk(KERN_ERR "%s: Attempted to start write outside oob\n",
2071 /* For compatibility with NAND: Do not allow write past end of page */
2072 if (unlikely(column + len > oobsize)) {
2073 printk(KERN_ERR "%s: Attempt to write past end of page\n",
2078 /* Do not allow reads past end of device */
2079 if (unlikely(to >= mtd->size ||
2080 column + len > ((mtd->size >> this->page_shift) -
2081 (to >> this->page_shift)) * oobsize)) {
2082 printk(KERN_ERR "%s: Attempted to write past end of device\n",
2087 oobbuf = this->oob_buf;
2089 oobcmd = ONENAND_IS_4KB_PAGE(this) ? ONENAND_CMD_PROG : ONENAND_CMD_PROGOOB;
2091 /* Loop until all data write */
2092 while (written < len) {
2093 int thislen = min_t(int, oobsize, len - written);
2097 this->command(mtd, ONENAND_CMD_BUFFERRAM, to, mtd->oobsize);
2099 /* We send data to spare ram with oobsize
2100 * to prevent byte access */
2101 memset(oobbuf, 0xff, mtd->oobsize);
2102 if (mode == MTD_OOB_AUTO)
2103 onenand_fill_auto_oob(mtd, oobbuf, buf, column, thislen);
2105 memcpy(oobbuf + column, buf, thislen);
2106 this->write_bufferram(mtd, ONENAND_SPARERAM, oobbuf, 0, mtd->oobsize);
2108 if (ONENAND_IS_4KB_PAGE(this)) {
2109 /* Set main area of DataRAM to 0xff*/
2110 memset(this->page_buf, 0xff, mtd->writesize);
2111 this->write_bufferram(mtd, ONENAND_DATARAM,
2112 this->page_buf, 0, mtd->writesize);
2115 this->command(mtd, oobcmd, to, mtd->oobsize);
2117 onenand_update_bufferram(mtd, to, 0);
2118 if (ONENAND_IS_2PLANE(this)) {
2119 ONENAND_SET_BUFFERRAM1(this);
2120 onenand_update_bufferram(mtd, to + this->writesize, 0);
2123 ret = this->wait(mtd, FL_WRITING);
2125 printk(KERN_ERR "%s: write failed %d\n", __func__, ret);
2129 ret = onenand_verify_oob(mtd, oobbuf, to);
2131 printk(KERN_ERR "%s: verify failed %d\n",
2140 to += mtd->writesize;
2145 ops->oobretlen = written;
2151 * onenand_write - [MTD Interface] write buffer to FLASH
2152 * @param mtd MTD device structure
2153 * @param to offset to write to
2154 * @param len number of bytes to write
2155 * @param retlen pointer to variable to store the number of written bytes
2156 * @param buf the data to write
2160 static int onenand_write(struct mtd_info *mtd, loff_t to, size_t len,
2161 size_t *retlen, const u_char *buf)
2163 struct mtd_oob_ops ops = {
2166 .datbuf = (u_char *) buf,
2171 onenand_get_device(mtd, FL_WRITING);
2172 ret = onenand_write_ops_nolock(mtd, to, &ops);
2173 onenand_release_device(mtd);
2175 *retlen = ops.retlen;
2180 * onenand_write_oob - [MTD Interface] NAND write data and/or out-of-band
2181 * @param mtd: MTD device structure
2182 * @param to: offset to write
2183 * @param ops: oob operation description structure
2185 static int onenand_write_oob(struct mtd_info *mtd, loff_t to,
2186 struct mtd_oob_ops *ops)
2190 switch (ops->mode) {
2195 /* Not implemented yet */
2200 onenand_get_device(mtd, FL_WRITING);
2202 ret = onenand_write_ops_nolock(mtd, to, ops);
2204 ret = onenand_write_oob_nolock(mtd, to, ops);
2205 onenand_release_device(mtd);
2211 * onenand_block_isbad_nolock - [GENERIC] Check if a block is marked bad
2212 * @param mtd MTD device structure
2213 * @param ofs offset from device start
2214 * @param allowbbt 1, if its allowed to access the bbt area
2216 * Check, if the block is bad. Either by reading the bad block table or
2217 * calling of the scan function.
2219 static int onenand_block_isbad_nolock(struct mtd_info *mtd, loff_t ofs, int allowbbt)
2221 struct onenand_chip *this = mtd->priv;
2222 struct bbm_info *bbm = this->bbm;
2224 /* Return info from the table */
2225 return bbm->isbad_bbt(mtd, ofs, allowbbt);
2229 static int onenand_multiblock_erase_verify(struct mtd_info *mtd,
2230 struct erase_info *instr)
2232 struct onenand_chip *this = mtd->priv;
2233 loff_t addr = instr->addr;
2234 int len = instr->len;
2235 unsigned int block_size = (1 << this->erase_shift);
2239 this->command(mtd, ONENAND_CMD_ERASE_VERIFY, addr, block_size);
2240 ret = this->wait(mtd, FL_VERIFYING_ERASE);
2242 printk(KERN_ERR "%s: Failed verify, block %d\n",
2243 __func__, onenand_block(this, addr));
2244 instr->state = MTD_ERASE_FAILED;
2245 instr->fail_addr = addr;
2255 * onenand_multiblock_erase - [Internal] erase block(s) using multiblock erase
2256 * @param mtd MTD device structure
2257 * @param instr erase instruction
2258 * @param region erase region
2260 * Erase one or more blocks up to 64 block at a time
2262 static int onenand_multiblock_erase(struct mtd_info *mtd,
2263 struct erase_info *instr,
2264 unsigned int block_size)
2266 struct onenand_chip *this = mtd->priv;
2267 loff_t addr = instr->addr;
2268 int len = instr->len;
2273 instr->state = MTD_ERASING;
2275 if (ONENAND_IS_DDP(this)) {
2276 loff_t bdry_addr = this->chipsize >> 1;
2277 if (addr < bdry_addr && (addr + len) > bdry_addr)
2278 bdry_block = bdry_addr >> this->erase_shift;
2283 /* Check if we have a bad block, we do not erase bad blocks */
2284 if (onenand_block_isbad_nolock(mtd, addr, 0)) {
2285 printk(KERN_WARNING "%s: attempt to erase a bad block "
2286 "at addr 0x%012llx\n",
2287 __func__, (unsigned long long) addr);
2288 instr->state = MTD_ERASE_FAILED;
2298 /* loop over 64 eb batches */
2300 struct erase_info verify_instr = *instr;
2301 int max_eb_count = MB_ERASE_MAX_BLK_COUNT;
2303 verify_instr.addr = addr;
2304 verify_instr.len = 0;
2306 /* do not cross chip boundary */
2308 int this_block = (addr >> this->erase_shift);
2310 if (this_block < bdry_block) {
2311 max_eb_count = min(max_eb_count,
2312 (bdry_block - this_block));
2318 while (len > block_size && eb_count < (max_eb_count - 1)) {
2319 this->command(mtd, ONENAND_CMD_MULTIBLOCK_ERASE,
2321 onenand_invalidate_bufferram(mtd, addr, block_size);
2323 ret = this->wait(mtd, FL_PREPARING_ERASE);
2325 printk(KERN_ERR "%s: Failed multiblock erase, "
2326 "block %d\n", __func__,
2327 onenand_block(this, addr));
2328 instr->state = MTD_ERASE_FAILED;
2329 instr->fail_addr = MTD_FAIL_ADDR_UNKNOWN;
2338 /* last block of 64-eb series */
2340 this->command(mtd, ONENAND_CMD_ERASE, addr, block_size);
2341 onenand_invalidate_bufferram(mtd, addr, block_size);
2343 ret = this->wait(mtd, FL_ERASING);
2344 /* Check if it is write protected */
2346 printk(KERN_ERR "%s: Failed erase, block %d\n",
2347 __func__, onenand_block(this, addr));
2348 instr->state = MTD_ERASE_FAILED;
2349 instr->fail_addr = MTD_FAIL_ADDR_UNKNOWN;
2358 verify_instr.len = eb_count * block_size;
2359 if (onenand_multiblock_erase_verify(mtd, &verify_instr)) {
2360 instr->state = verify_instr.state;
2361 instr->fail_addr = verify_instr.fail_addr;
2371 * onenand_block_by_block_erase - [Internal] erase block(s) using regular erase
2372 * @param mtd MTD device structure
2373 * @param instr erase instruction
2374 * @param region erase region
2375 * @param block_size erase block size
2377 * Erase one or more blocks one block at a time
2379 static int onenand_block_by_block_erase(struct mtd_info *mtd,
2380 struct erase_info *instr,
2381 struct mtd_erase_region_info *region,
2382 unsigned int block_size)
2384 struct onenand_chip *this = mtd->priv;
2385 loff_t addr = instr->addr;
2386 int len = instr->len;
2387 loff_t region_end = 0;
2391 /* region is set for Flex-OneNAND */
2392 region_end = region->offset + region->erasesize * region->numblocks;
2395 instr->state = MTD_ERASING;
2397 /* Loop through the blocks */
2401 /* Check if we have a bad block, we do not erase bad blocks */
2402 if (onenand_block_isbad_nolock(mtd, addr, 0)) {
2403 printk(KERN_WARNING "%s: attempt to erase a bad block "
2404 "at addr 0x%012llx\n",
2405 __func__, (unsigned long long) addr);
2406 instr->state = MTD_ERASE_FAILED;
2410 this->command(mtd, ONENAND_CMD_ERASE, addr, block_size);
2412 onenand_invalidate_bufferram(mtd, addr, block_size);
2414 ret = this->wait(mtd, FL_ERASING);
2415 /* Check, if it is write protected */
2417 printk(KERN_ERR "%s: Failed erase, block %d\n",
2418 __func__, onenand_block(this, addr));
2419 instr->state = MTD_ERASE_FAILED;
2420 instr->fail_addr = addr;
2427 if (addr == region_end) {
2432 block_size = region->erasesize;
2433 region_end = region->offset + region->erasesize * region->numblocks;
2435 if (len & (block_size - 1)) {
2436 /* FIXME: This should be handled at MTD partitioning level. */
2437 printk(KERN_ERR "%s: Unaligned address\n",
2447 * onenand_erase - [MTD Interface] erase block(s)
2448 * @param mtd MTD device structure
2449 * @param instr erase instruction
2451 * Erase one or more blocks
2453 static int onenand_erase(struct mtd_info *mtd, struct erase_info *instr)
2455 struct onenand_chip *this = mtd->priv;
2456 unsigned int block_size;
2457 loff_t addr = instr->addr;
2458 loff_t len = instr->len;
2460 struct mtd_erase_region_info *region = NULL;
2461 loff_t region_offset = 0;
2463 DEBUG(MTD_DEBUG_LEVEL3, "%s: start=0x%012llx, len=%llu\n", __func__,
2464 (unsigned long long) instr->addr, (unsigned long long) instr->len);
2466 /* Do not allow erase past end of device */
2467 if (unlikely((len + addr) > mtd->size)) {
2468 printk(KERN_ERR "%s: Erase past end of device\n", __func__);
2472 if (FLEXONENAND(this)) {
2473 /* Find the eraseregion of this address */
2474 int i = flexonenand_region(mtd, addr);
2476 region = &mtd->eraseregions[i];
2477 block_size = region->erasesize;
2479 /* Start address within region must align on block boundary.
2480 * Erase region's start offset is always block start address.
2482 region_offset = region->offset;
2484 block_size = 1 << this->erase_shift;
2486 /* Start address must align on block boundary */
2487 if (unlikely((addr - region_offset) & (block_size - 1))) {
2488 printk(KERN_ERR "%s: Unaligned address\n", __func__);
2492 /* Length must align on block boundary */
2493 if (unlikely(len & (block_size - 1))) {
2494 printk(KERN_ERR "%s: Length not block aligned\n", __func__);
2498 instr->fail_addr = MTD_FAIL_ADDR_UNKNOWN;
2500 /* Grab the lock and see if the device is available */
2501 onenand_get_device(mtd, FL_ERASING);
2503 if (ONENAND_IS_4KB_PAGE(this) || region ||
2504 instr->len < MB_ERASE_MIN_BLK_COUNT * block_size) {
2505 /* region is set for Flex-OneNAND (no mb erase) */
2506 ret = onenand_block_by_block_erase(mtd, instr,
2507 region, block_size);
2509 ret = onenand_multiblock_erase(mtd, instr, block_size);
2512 /* Deselect and wake up anyone waiting on the device */
2513 onenand_release_device(mtd);
2515 /* Do call back function */
2517 instr->state = MTD_ERASE_DONE;
2518 mtd_erase_callback(instr);
2525 * onenand_sync - [MTD Interface] sync
2526 * @param mtd MTD device structure
2528 * Sync is actually a wait for chip ready function
2530 static void onenand_sync(struct mtd_info *mtd)
2532 DEBUG(MTD_DEBUG_LEVEL3, "%s: called\n", __func__);
2534 /* Grab the lock and see if the device is available */
2535 onenand_get_device(mtd, FL_SYNCING);
2537 /* Release it and go back */
2538 onenand_release_device(mtd);
2542 * onenand_block_isbad - [MTD Interface] Check whether the block at the given offset is bad
2543 * @param mtd MTD device structure
2544 * @param ofs offset relative to mtd start
2546 * Check whether the block is bad
2548 static int onenand_block_isbad(struct mtd_info *mtd, loff_t ofs)
2552 /* Check for invalid offset */
2553 if (ofs > mtd->size)
2556 onenand_get_device(mtd, FL_READING);
2557 ret = onenand_block_isbad_nolock(mtd, ofs, 0);
2558 onenand_release_device(mtd);
2563 * onenand_default_block_markbad - [DEFAULT] mark a block bad
2564 * @param mtd MTD device structure
2565 * @param ofs offset from device start
2567 * This is the default implementation, which can be overridden by
2568 * a hardware specific driver.
2570 static int onenand_default_block_markbad(struct mtd_info *mtd, loff_t ofs)
2572 struct onenand_chip *this = mtd->priv;
2573 struct bbm_info *bbm = this->bbm;
2574 u_char buf[2] = {0, 0};
2575 struct mtd_oob_ops ops = {
2576 .mode = MTD_OOB_PLACE,
2583 /* Get block number */
2584 block = onenand_block(this, ofs);
2586 bbm->bbt[block >> 2] |= 0x01 << ((block & 0x03) << 1);
2588 /* We write two bytes, so we don't have to mess with 16-bit access */
2589 ofs += mtd->oobsize + (bbm->badblockpos & ~0x01);
2590 /* FIXME : What to do when marking SLC block in partition
2591 * with MLC erasesize? For now, it is not advisable to
2592 * create partitions containing both SLC and MLC regions.
2594 return onenand_write_oob_nolock(mtd, ofs, &ops);
2598 * onenand_block_markbad - [MTD Interface] Mark the block at the given offset as bad
2599 * @param mtd MTD device structure
2600 * @param ofs offset relative to mtd start
2602 * Mark the block as bad
2604 static int onenand_block_markbad(struct mtd_info *mtd, loff_t ofs)
2606 struct onenand_chip *this = mtd->priv;
2609 ret = onenand_block_isbad(mtd, ofs);
2611 /* If it was bad already, return success and do nothing */
2617 onenand_get_device(mtd, FL_WRITING);
2618 ret = this->block_markbad(mtd, ofs);
2619 onenand_release_device(mtd);
2624 * onenand_do_lock_cmd - [OneNAND Interface] Lock or unlock block(s)
2625 * @param mtd MTD device structure
2626 * @param ofs offset relative to mtd start
2627 * @param len number of bytes to lock or unlock
2628 * @param cmd lock or unlock command
2630 * Lock or unlock one or more blocks
2632 static int onenand_do_lock_cmd(struct mtd_info *mtd, loff_t ofs, size_t len, int cmd)
2634 struct onenand_chip *this = mtd->priv;
2635 int start, end, block, value, status;
2638 start = onenand_block(this, ofs);
2639 end = onenand_block(this, ofs + len) - 1;
2641 if (cmd == ONENAND_CMD_LOCK)
2642 wp_status_mask = ONENAND_WP_LS;
2644 wp_status_mask = ONENAND_WP_US;
2646 /* Continuous lock scheme */
2647 if (this->options & ONENAND_HAS_CONT_LOCK) {
2648 /* Set start block address */
2649 this->write_word(start, this->base + ONENAND_REG_START_BLOCK_ADDRESS);
2650 /* Set end block address */
2651 this->write_word(end, this->base + ONENAND_REG_END_BLOCK_ADDRESS);
2652 /* Write lock command */
2653 this->command(mtd, cmd, 0, 0);
2655 /* There's no return value */
2656 this->wait(mtd, FL_LOCKING);
2659 while (this->read_word(this->base + ONENAND_REG_CTRL_STATUS)
2660 & ONENAND_CTRL_ONGO)
2663 /* Check lock status */
2664 status = this->read_word(this->base + ONENAND_REG_WP_STATUS);
2665 if (!(status & wp_status_mask))
2666 printk(KERN_ERR "%s: wp status = 0x%x\n",
2672 /* Block lock scheme */
2673 for (block = start; block < end + 1; block++) {
2674 /* Set block address */
2675 value = onenand_block_address(this, block);
2676 this->write_word(value, this->base + ONENAND_REG_START_ADDRESS1);
2677 /* Select DataRAM for DDP */
2678 value = onenand_bufferram_address(this, block);
2679 this->write_word(value, this->base + ONENAND_REG_START_ADDRESS2);
2680 /* Set start block address */
2681 this->write_word(block, this->base + ONENAND_REG_START_BLOCK_ADDRESS);
2682 /* Write lock command */
2683 this->command(mtd, cmd, 0, 0);
2685 /* There's no return value */
2686 this->wait(mtd, FL_LOCKING);
2689 while (this->read_word(this->base + ONENAND_REG_CTRL_STATUS)
2690 & ONENAND_CTRL_ONGO)
2693 /* Check lock status */
2694 status = this->read_word(this->base + ONENAND_REG_WP_STATUS);
2695 if (!(status & wp_status_mask))
2696 printk(KERN_ERR "%s: block = %d, wp status = 0x%x\n",
2697 __func__, block, status);
2704 * onenand_lock - [MTD Interface] Lock block(s)
2705 * @param mtd MTD device structure
2706 * @param ofs offset relative to mtd start
2707 * @param len number of bytes to unlock
2709 * Lock one or more blocks
2711 static int onenand_lock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
2715 onenand_get_device(mtd, FL_LOCKING);
2716 ret = onenand_do_lock_cmd(mtd, ofs, len, ONENAND_CMD_LOCK);
2717 onenand_release_device(mtd);
2722 * onenand_unlock - [MTD Interface] Unlock block(s)
2723 * @param mtd MTD device structure
2724 * @param ofs offset relative to mtd start
2725 * @param len number of bytes to unlock
2727 * Unlock one or more blocks
2729 static int onenand_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
2733 onenand_get_device(mtd, FL_LOCKING);
2734 ret = onenand_do_lock_cmd(mtd, ofs, len, ONENAND_CMD_UNLOCK);
2735 onenand_release_device(mtd);
2740 * onenand_check_lock_status - [OneNAND Interface] Check lock status
2741 * @param this onenand chip data structure
2745 static int onenand_check_lock_status(struct onenand_chip *this)
2747 unsigned int value, block, status;
2750 end = this->chipsize >> this->erase_shift;
2751 for (block = 0; block < end; block++) {
2752 /* Set block address */
2753 value = onenand_block_address(this, block);
2754 this->write_word(value, this->base + ONENAND_REG_START_ADDRESS1);
2755 /* Select DataRAM for DDP */
2756 value = onenand_bufferram_address(this, block);
2757 this->write_word(value, this->base + ONENAND_REG_START_ADDRESS2);
2758 /* Set start block address */
2759 this->write_word(block, this->base + ONENAND_REG_START_BLOCK_ADDRESS);
2761 /* Check lock status */
2762 status = this->read_word(this->base + ONENAND_REG_WP_STATUS);
2763 if (!(status & ONENAND_WP_US)) {
2764 printk(KERN_ERR "%s: block = %d, wp status = 0x%x\n",
2765 __func__, block, status);
2774 * onenand_unlock_all - [OneNAND Interface] unlock all blocks
2775 * @param mtd MTD device structure
2779 static void onenand_unlock_all(struct mtd_info *mtd)
2781 struct onenand_chip *this = mtd->priv;
2783 loff_t len = mtd->size;
2785 if (this->options & ONENAND_HAS_UNLOCK_ALL) {
2786 /* Set start block address */
2787 this->write_word(0, this->base + ONENAND_REG_START_BLOCK_ADDRESS);
2788 /* Write unlock command */
2789 this->command(mtd, ONENAND_CMD_UNLOCK_ALL, 0, 0);
2791 /* There's no return value */
2792 this->wait(mtd, FL_LOCKING);
2795 while (this->read_word(this->base + ONENAND_REG_CTRL_STATUS)
2796 & ONENAND_CTRL_ONGO)
2799 /* Don't check lock status */
2800 if (this->options & ONENAND_SKIP_UNLOCK_CHECK)
2803 /* Check lock status */
2804 if (onenand_check_lock_status(this))
2807 /* Workaround for all block unlock in DDP */
2808 if (ONENAND_IS_DDP(this) && !FLEXONENAND(this)) {
2809 /* All blocks on another chip */
2810 ofs = this->chipsize >> 1;
2811 len = this->chipsize >> 1;
2815 onenand_do_lock_cmd(mtd, ofs, len, ONENAND_CMD_UNLOCK);
2818 #ifdef CONFIG_MTD_ONENAND_OTP
2821 * onenand_otp_command - Send OTP specific command to OneNAND device
2822 * @param mtd MTD device structure
2823 * @param cmd the command to be sent
2824 * @param addr offset to read from or write to
2825 * @param len number of bytes to read or write
2827 static int onenand_otp_command(struct mtd_info *mtd, int cmd, loff_t addr,
2830 struct onenand_chip *this = mtd->priv;
2831 int value, block, page;
2833 /* Address translation */
2835 case ONENAND_CMD_OTP_ACCESS:
2836 block = (int) (addr >> this->erase_shift);
2841 block = (int) (addr >> this->erase_shift);
2842 page = (int) (addr >> this->page_shift);
2844 if (ONENAND_IS_2PLANE(this)) {
2845 /* Make the even block number */
2847 /* Is it the odd plane? */
2848 if (addr & this->writesize)
2852 page &= this->page_mask;
2857 /* Write 'DFS, FBA' of Flash */
2858 value = onenand_block_address(this, block);
2859 this->write_word(value, this->base +
2860 ONENAND_REG_START_ADDRESS1);
2864 /* Now we use page size operation */
2865 int sectors = 4, count = 4;
2870 if (ONENAND_IS_2PLANE(this) && cmd == ONENAND_CMD_PROG)
2871 cmd = ONENAND_CMD_2X_PROG;
2872 dataram = ONENAND_CURRENT_BUFFERRAM(this);
2876 /* Write 'FPA, FSA' of Flash */
2877 value = onenand_page_address(page, sectors);
2878 this->write_word(value, this->base +
2879 ONENAND_REG_START_ADDRESS8);
2881 /* Write 'BSA, BSC' of DataRAM */
2882 value = onenand_buffer_address(dataram, sectors, count);
2883 this->write_word(value, this->base + ONENAND_REG_START_BUFFER);
2886 /* Interrupt clear */
2887 this->write_word(ONENAND_INT_CLEAR, this->base + ONENAND_REG_INTERRUPT);
2890 this->write_word(cmd, this->base + ONENAND_REG_COMMAND);
2896 * onenand_otp_write_oob_nolock - [Internal] OneNAND write out-of-band, specific to OTP
2897 * @param mtd MTD device structure
2898 * @param to offset to write to
2899 * @param len number of bytes to write
2900 * @param retlen pointer to variable to store the number of written bytes
2901 * @param buf the data to write
2903 * OneNAND write out-of-band only for OTP
2905 static int onenand_otp_write_oob_nolock(struct mtd_info *mtd, loff_t to,
2906 struct mtd_oob_ops *ops)
2908 struct onenand_chip *this = mtd->priv;
2909 int column, ret = 0, oobsize;
2912 size_t len = ops->ooblen;
2913 const u_char *buf = ops->oobbuf;
2914 int block, value, status;
2918 /* Initialize retlen, in case of early exit */
2921 oobsize = mtd->oobsize;
2923 column = to & (mtd->oobsize - 1);
2925 oobbuf = this->oob_buf;
2927 /* Loop until all data write */
2928 while (written < len) {
2929 int thislen = min_t(int, oobsize, len - written);
2933 block = (int) (to >> this->erase_shift);
2935 * Write 'DFS, FBA' of Flash
2936 * Add: F100h DQ=DFS, FBA
2939 value = onenand_block_address(this, block);
2940 this->write_word(value, this->base +
2941 ONENAND_REG_START_ADDRESS1);
2944 * Select DataRAM for DDP
2948 value = onenand_bufferram_address(this, block);
2949 this->write_word(value, this->base +
2950 ONENAND_REG_START_ADDRESS2);
2951 ONENAND_SET_NEXT_BUFFERRAM(this);
2954 * Enter OTP access mode
2956 this->command(mtd, ONENAND_CMD_OTP_ACCESS, 0, 0);
2957 this->wait(mtd, FL_OTPING);
2959 /* We send data to spare ram with oobsize
2960 * to prevent byte access */
2961 memcpy(oobbuf + column, buf, thislen);
2964 * Write Data into DataRAM
2966 * in sector0/spare/page0
2969 this->write_bufferram(mtd, ONENAND_SPARERAM,
2970 oobbuf, 0, mtd->oobsize);
2972 onenand_otp_command(mtd, ONENAND_CMD_PROGOOB, to, mtd->oobsize);
2973 onenand_update_bufferram(mtd, to, 0);
2974 if (ONENAND_IS_2PLANE(this)) {
2975 ONENAND_SET_BUFFERRAM1(this);
2976 onenand_update_bufferram(mtd, to + this->writesize, 0);
2979 ret = this->wait(mtd, FL_WRITING);
2981 printk(KERN_ERR "%s: write failed %d\n", __func__, ret);
2985 /* Exit OTP access mode */
2986 this->command(mtd, ONENAND_CMD_RESET, 0, 0);
2987 this->wait(mtd, FL_RESETING);
2989 status = this->read_word(this->base + ONENAND_REG_CTRL_STATUS);
2992 if (status == 0x60) {
2993 printk(KERN_DEBUG "\nBLOCK\tSTATUS\n");
2994 printk(KERN_DEBUG "1st Block\tLOCKED\n");
2995 printk(KERN_DEBUG "OTP Block\tLOCKED\n");
2996 } else if (status == 0x20) {
2997 printk(KERN_DEBUG "\nBLOCK\tSTATUS\n");
2998 printk(KERN_DEBUG "1st Block\tLOCKED\n");
2999 printk(KERN_DEBUG "OTP Block\tUN-LOCKED\n");
3000 } else if (status == 0x40) {
3001 printk(KERN_DEBUG "\nBLOCK\tSTATUS\n");
3002 printk(KERN_DEBUG "1st Block\tUN-LOCKED\n");
3003 printk(KERN_DEBUG "OTP Block\tLOCKED\n");
3005 printk(KERN_DEBUG "Reboot to check\n");
3012 to += mtd->writesize;
3017 ops->oobretlen = written;
3022 /* Internal OTP operation */
3023 typedef int (*otp_op_t)(struct mtd_info *mtd, loff_t form, size_t len,
3024 size_t *retlen, u_char *buf);
3027 * do_otp_read - [DEFAULT] Read OTP block area
3028 * @param mtd MTD device structure
3029 * @param from The offset to read
3030 * @param len number of bytes to read
3031 * @param retlen pointer to variable to store the number of readbytes
3032 * @param buf the databuffer to put/get data
3034 * Read OTP block area.
3036 static int do_otp_read(struct mtd_info *mtd, loff_t from, size_t len,
3037 size_t *retlen, u_char *buf)
3039 struct onenand_chip *this = mtd->priv;
3040 struct mtd_oob_ops ops = {
3048 /* Enter OTP access mode */
3049 this->command(mtd, ONENAND_CMD_OTP_ACCESS, 0, 0);
3050 this->wait(mtd, FL_OTPING);
3052 ret = ONENAND_IS_4KB_PAGE(this) ?
3053 onenand_mlc_read_ops_nolock(mtd, from, &ops) :
3054 onenand_read_ops_nolock(mtd, from, &ops);
3056 /* Exit OTP access mode */
3057 this->command(mtd, ONENAND_CMD_RESET, 0, 0);
3058 this->wait(mtd, FL_RESETING);
3064 * do_otp_write - [DEFAULT] Write OTP block area
3065 * @param mtd MTD device structure
3066 * @param to The offset to write
3067 * @param len number of bytes to write
3068 * @param retlen pointer to variable to store the number of write bytes
3069 * @param buf the databuffer to put/get data
3071 * Write OTP block area.
3073 static int do_otp_write(struct mtd_info *mtd, loff_t to, size_t len,
3074 size_t *retlen, u_char *buf)
3076 struct onenand_chip *this = mtd->priv;
3077 unsigned char *pbuf = buf;
3079 struct mtd_oob_ops ops;
3081 /* Force buffer page aligned */
3082 if (len < mtd->writesize) {
3083 memcpy(this->page_buf, buf, len);
3084 memset(this->page_buf + len, 0xff, mtd->writesize - len);
3085 pbuf = this->page_buf;
3086 len = mtd->writesize;
3089 /* Enter OTP access mode */
3090 this->command(mtd, ONENAND_CMD_OTP_ACCESS, 0, 0);
3091 this->wait(mtd, FL_OTPING);
3097 ret = onenand_write_ops_nolock(mtd, to, &ops);
3098 *retlen = ops.retlen;
3100 /* Exit OTP access mode */
3101 this->command(mtd, ONENAND_CMD_RESET, 0, 0);
3102 this->wait(mtd, FL_RESETING);
3108 * do_otp_lock - [DEFAULT] Lock OTP block area
3109 * @param mtd MTD device structure
3110 * @param from The offset to lock
3111 * @param len number of bytes to lock
3112 * @param retlen pointer to variable to store the number of lock bytes
3113 * @param buf the databuffer to put/get data
3115 * Lock OTP block area.
3117 static int do_otp_lock(struct mtd_info *mtd, loff_t from, size_t len,
3118 size_t *retlen, u_char *buf)
3120 struct onenand_chip *this = mtd->priv;
3121 struct mtd_oob_ops ops;
3124 if (FLEXONENAND(this)) {
3126 /* Enter OTP access mode */
3127 this->command(mtd, ONENAND_CMD_OTP_ACCESS, 0, 0);
3128 this->wait(mtd, FL_OTPING);
3130 * For Flex-OneNAND, we write lock mark to 1st word of sector 4 of
3131 * main area of page 49.
3133 ops.len = mtd->writesize;
3137 ret = onenand_write_ops_nolock(mtd, mtd->writesize * 49, &ops);
3138 *retlen = ops.retlen;
3140 /* Exit OTP access mode */
3141 this->command(mtd, ONENAND_CMD_RESET, 0, 0);
3142 this->wait(mtd, FL_RESETING);
3144 ops.mode = MTD_OOB_PLACE;
3148 ret = onenand_otp_write_oob_nolock(mtd, from, &ops);
3149 *retlen = ops.oobretlen;
3156 * onenand_otp_walk - [DEFAULT] Handle OTP operation
3157 * @param mtd MTD device structure
3158 * @param from The offset to read/write
3159 * @param len number of bytes to read/write
3160 * @param retlen pointer to variable to store the number of read bytes
3161 * @param buf the databuffer to put/get data
3162 * @param action do given action
3163 * @param mode specify user and factory
3165 * Handle OTP operation.
3167 static int onenand_otp_walk(struct mtd_info *mtd, loff_t from, size_t len,
3168 size_t *retlen, u_char *buf,
3169 otp_op_t action, int mode)
3171 struct onenand_chip *this = mtd->priv;
3178 density = onenand_get_density(this->device_id);
3179 if (density < ONENAND_DEVICE_DENSITY_512Mb)
3184 if (mode == MTD_OTP_FACTORY) {
3185 from += mtd->writesize * otp_pages;
3186 otp_pages = ONENAND_PAGES_PER_BLOCK - otp_pages;
3189 /* Check User/Factory boundary */
3190 if (mode == MTD_OTP_USER) {
3191 if (mtd->writesize * otp_pages < from + len)
3194 if (mtd->writesize * otp_pages < len)
3198 onenand_get_device(mtd, FL_OTPING);
3199 while (len > 0 && otp_pages > 0) {
3200 if (!action) { /* OTP Info functions */
3201 struct otp_info *otpinfo;
3203 len -= sizeof(struct otp_info);
3209 otpinfo = (struct otp_info *) buf;
3210 otpinfo->start = from;
3211 otpinfo->length = mtd->writesize;
3212 otpinfo->locked = 0;
3214 from += mtd->writesize;
3215 buf += sizeof(struct otp_info);
3216 *retlen += sizeof(struct otp_info);
3220 ret = action(mtd, from, len, &tmp_retlen, buf);
3224 *retlen += tmp_retlen;
3231 onenand_release_device(mtd);
3237 * onenand_get_fact_prot_info - [MTD Interface] Read factory OTP info
3238 * @param mtd MTD device structure
3239 * @param buf the databuffer to put/get data
3240 * @param len number of bytes to read
3242 * Read factory OTP info.
3244 static int onenand_get_fact_prot_info(struct mtd_info *mtd,
3245 struct otp_info *buf, size_t len)
3250 ret = onenand_otp_walk(mtd, 0, len, &retlen, (u_char *) buf, NULL, MTD_OTP_FACTORY);
3252 return ret ? : retlen;
3256 * onenand_read_fact_prot_reg - [MTD Interface] Read factory OTP area
3257 * @param mtd MTD device structure
3258 * @param from The offset to read
3259 * @param len number of bytes to read
3260 * @param retlen pointer to variable to store the number of read bytes
3261 * @param buf the databuffer to put/get data
3263 * Read factory OTP area.
3265 static int onenand_read_fact_prot_reg(struct mtd_info *mtd, loff_t from,
3266 size_t len, size_t *retlen, u_char *buf)
3268 return onenand_otp_walk(mtd, from, len, retlen, buf, do_otp_read, MTD_OTP_FACTORY);
3272 * onenand_get_user_prot_info - [MTD Interface] Read user OTP info
3273 * @param mtd MTD device structure
3274 * @param buf the databuffer to put/get data
3275 * @param len number of bytes to read
3277 * Read user OTP info.
3279 static int onenand_get_user_prot_info(struct mtd_info *mtd,
3280 struct otp_info *buf, size_t len)
3285 ret = onenand_otp_walk(mtd, 0, len, &retlen, (u_char *) buf, NULL, MTD_OTP_USER);
3287 return ret ? : retlen;
3291 * onenand_read_user_prot_reg - [MTD Interface] Read user OTP area
3292 * @param mtd MTD device structure
3293 * @param from The offset to read
3294 * @param len number of bytes to read
3295 * @param retlen pointer to variable to store the number of read bytes
3296 * @param buf the databuffer to put/get data
3298 * Read user OTP area.
3300 static int onenand_read_user_prot_reg(struct mtd_info *mtd, loff_t from,
3301 size_t len, size_t *retlen, u_char *buf)
3303 return onenand_otp_walk(mtd, from, len, retlen, buf, do_otp_read, MTD_OTP_USER);
3307 * onenand_write_user_prot_reg - [MTD Interface] Write user OTP area
3308 * @param mtd MTD device structure
3309 * @param from The offset to write
3310 * @param len number of bytes to write
3311 * @param retlen pointer to variable to store the number of write bytes
3312 * @param buf the databuffer to put/get data
3314 * Write user OTP area.
3316 static int onenand_write_user_prot_reg(struct mtd_info *mtd, loff_t from,
3317 size_t len, size_t *retlen, u_char *buf)
3319 return onenand_otp_walk(mtd, from, len, retlen, buf, do_otp_write, MTD_OTP_USER);
3323 * onenand_lock_user_prot_reg - [MTD Interface] Lock user OTP area
3324 * @param mtd MTD device structure
3325 * @param from The offset to lock
3326 * @param len number of bytes to unlock
3328 * Write lock mark on spare area in page 0 in OTP block
3330 static int onenand_lock_user_prot_reg(struct mtd_info *mtd, loff_t from,
3333 struct onenand_chip *this = mtd->priv;
3334 u_char *buf = FLEXONENAND(this) ? this->page_buf : this->oob_buf;
3337 unsigned int otp_lock_offset = ONENAND_OTP_LOCK_OFFSET;
3339 memset(buf, 0xff, FLEXONENAND(this) ? this->writesize
3342 * Write lock mark to 8th word of sector0 of page0 of the spare0.
3343 * We write 16 bytes spare area instead of 2 bytes.
3344 * For Flex-OneNAND, we write lock mark to 1st word of sector 4 of
3345 * main area of page 49.
3349 len = FLEXONENAND(this) ? mtd->writesize : 16;
3352 * Note: OTP lock operation
3353 * OTP block : 0xXXFC XX 1111 1100
3354 * 1st block : 0xXXF3 (If chip support) XX 1111 0011
3355 * Both : 0xXXF0 (If chip support) XX 1111 0000
3357 if (FLEXONENAND(this))
3358 otp_lock_offset = FLEXONENAND_OTP_LOCK_OFFSET;
3360 /* ONENAND_OTP_AREA | ONENAND_OTP_BLOCK0 | ONENAND_OTP_AREA_BLOCK0 */
3362 buf[otp_lock_offset] = 0xFC;
3364 buf[otp_lock_offset] = 0xF3;
3366 buf[otp_lock_offset] = 0xF0;
3368 printk(KERN_DEBUG "[OneNAND] Invalid option selected for OTP\n");
3370 ret = onenand_otp_walk(mtd, from, len, &retlen, buf, do_otp_lock, MTD_OTP_USER);
3372 return ret ? : retlen;
3375 #endif /* CONFIG_MTD_ONENAND_OTP */
3378 * onenand_check_features - Check and set OneNAND features
3379 * @param mtd MTD data structure
3381 * Check and set OneNAND features
3385 static void onenand_check_features(struct mtd_info *mtd)
3387 struct onenand_chip *this = mtd->priv;
3388 unsigned int density, process, numbufs;
3390 /* Lock scheme depends on density and process */
3391 density = onenand_get_density(this->device_id);
3392 process = this->version_id >> ONENAND_VERSION_PROCESS_SHIFT;
3393 numbufs = this->read_word(this->base + ONENAND_REG_NUM_BUFFERS) >> 8;
3397 case ONENAND_DEVICE_DENSITY_4Gb:
3398 if (ONENAND_IS_DDP(this))
3399 this->options |= ONENAND_HAS_2PLANE;
3400 else if (numbufs == 1) {
3401 this->options |= ONENAND_HAS_4KB_PAGE;
3402 this->options |= ONENAND_HAS_CACHE_PROGRAM;
3405 case ONENAND_DEVICE_DENSITY_2Gb:
3406 /* 2Gb DDP does not have 2 plane */
3407 if (!ONENAND_IS_DDP(this))
3408 this->options |= ONENAND_HAS_2PLANE;
3409 this->options |= ONENAND_HAS_UNLOCK_ALL;
3411 case ONENAND_DEVICE_DENSITY_1Gb:
3412 /* A-Die has all block unlock */
3414 this->options |= ONENAND_HAS_UNLOCK_ALL;
3418 /* Some OneNAND has continuous lock scheme */
3420 this->options |= ONENAND_HAS_CONT_LOCK;
3424 /* The MLC has 4KiB pagesize. */
3425 if (ONENAND_IS_MLC(this))
3426 this->options |= ONENAND_HAS_4KB_PAGE;
3428 if (ONENAND_IS_4KB_PAGE(this))
3429 this->options &= ~ONENAND_HAS_2PLANE;
3431 if (FLEXONENAND(this)) {
3432 this->options &= ~ONENAND_HAS_CONT_LOCK;
3433 this->options |= ONENAND_HAS_UNLOCK_ALL;
3436 if (this->options & ONENAND_HAS_CONT_LOCK)
3437 printk(KERN_DEBUG "Lock scheme is Continuous Lock\n");
3438 if (this->options & ONENAND_HAS_UNLOCK_ALL)
3439 printk(KERN_DEBUG "Chip support all block unlock\n");
3440 if (this->options & ONENAND_HAS_2PLANE)
3441 printk(KERN_DEBUG "Chip has 2 plane\n");
3442 if (this->options & ONENAND_HAS_4KB_PAGE)
3443 printk(KERN_DEBUG "Chip has 4KiB pagesize\n");
3444 if (this->options & ONENAND_HAS_CACHE_PROGRAM)
3445 printk(KERN_DEBUG "Chip has cache program feature\n");
3449 * onenand_print_device_info - Print device & version ID
3450 * @param device device ID
3451 * @param version version ID
3453 * Print device & version ID
3455 static void onenand_print_device_info(int device, int version)
3457 int vcc, demuxed, ddp, density, flexonenand;
3459 vcc = device & ONENAND_DEVICE_VCC_MASK;
3460 demuxed = device & ONENAND_DEVICE_IS_DEMUX;
3461 ddp = device & ONENAND_DEVICE_IS_DDP;
3462 density = onenand_get_density(device);
3463 flexonenand = device & DEVICE_IS_FLEXONENAND;
3464 printk(KERN_INFO "%s%sOneNAND%s %dMB %sV 16-bit (0x%02x)\n",
3465 demuxed ? "" : "Muxed ",
3466 flexonenand ? "Flex-" : "",
3469 vcc ? "2.65/3.3" : "1.8",
3471 printk(KERN_INFO "OneNAND version = 0x%04x\n", version);
3474 static const struct onenand_manufacturers onenand_manuf_ids[] = {
3475 {ONENAND_MFR_SAMSUNG, "Samsung"},
3476 {ONENAND_MFR_NUMONYX, "Numonyx"},
3480 * onenand_check_maf - Check manufacturer ID
3481 * @param manuf manufacturer ID
3483 * Check manufacturer ID
3485 static int onenand_check_maf(int manuf)
3487 int size = ARRAY_SIZE(onenand_manuf_ids);
3491 for (i = 0; i < size; i++)
3492 if (manuf == onenand_manuf_ids[i].id)
3496 name = onenand_manuf_ids[i].name;
3500 printk(KERN_DEBUG "OneNAND Manufacturer: %s (0x%0x)\n", name, manuf);
3506 * flexonenand_get_boundary - Reads the SLC boundary
3507 * @param onenand_info - onenand info structure
3509 static int flexonenand_get_boundary(struct mtd_info *mtd)
3511 struct onenand_chip *this = mtd->priv;
3513 int ret, syscfg, locked;
3516 syscfg = this->read_word(this->base + ONENAND_REG_SYS_CFG1);
3517 this->write_word((syscfg | 0x0100), this->base + ONENAND_REG_SYS_CFG1);
3519 for (die = 0; die < this->dies; die++) {
3520 this->command(mtd, FLEXONENAND_CMD_PI_ACCESS, die, 0);
3521 this->wait(mtd, FL_SYNCING);
3523 this->command(mtd, FLEXONENAND_CMD_READ_PI, die, 0);
3524 ret = this->wait(mtd, FL_READING);
3526 bdry = this->read_word(this->base + ONENAND_DATARAM);
3527 if ((bdry >> FLEXONENAND_PI_UNLOCK_SHIFT) == 3)
3531 this->boundary[die] = bdry & FLEXONENAND_PI_MASK;
3533 this->command(mtd, ONENAND_CMD_RESET, 0, 0);
3534 ret = this->wait(mtd, FL_RESETING);
3536 printk(KERN_INFO "Die %d boundary: %d%s\n", die,
3537 this->boundary[die], locked ? "(Locked)" : "(Unlocked)");
3541 this->write_word(syscfg, this->base + ONENAND_REG_SYS_CFG1);
3546 * flexonenand_get_size - Fill up fields in onenand_chip and mtd_info
3547 * boundary[], diesize[], mtd->size, mtd->erasesize
3548 * @param mtd - MTD device structure
3550 static void flexonenand_get_size(struct mtd_info *mtd)
3552 struct onenand_chip *this = mtd->priv;
3553 int die, i, eraseshift, density;
3554 int blksperdie, maxbdry;
3557 density = onenand_get_density(this->device_id);
3558 blksperdie = ((loff_t)(16 << density) << 20) >> (this->erase_shift);
3559 blksperdie >>= ONENAND_IS_DDP(this) ? 1 : 0;
3560 maxbdry = blksperdie - 1;
3561 eraseshift = this->erase_shift - 1;
3563 mtd->numeraseregions = this->dies << 1;
3565 /* This fills up the device boundary */
3566 flexonenand_get_boundary(mtd);
3569 for (; die < this->dies; die++) {
3570 if (!die || this->boundary[die-1] != maxbdry) {
3572 mtd->eraseregions[i].offset = ofs;
3573 mtd->eraseregions[i].erasesize = 1 << eraseshift;
3574 mtd->eraseregions[i].numblocks =
3575 this->boundary[die] + 1;
3576 ofs += mtd->eraseregions[i].numblocks << eraseshift;
3579 mtd->numeraseregions -= 1;
3580 mtd->eraseregions[i].numblocks +=
3581 this->boundary[die] + 1;
3582 ofs += (this->boundary[die] + 1) << (eraseshift - 1);
3584 if (this->boundary[die] != maxbdry) {
3586 mtd->eraseregions[i].offset = ofs;
3587 mtd->eraseregions[i].erasesize = 1 << eraseshift;
3588 mtd->eraseregions[i].numblocks = maxbdry ^
3589 this->boundary[die];
3590 ofs += mtd->eraseregions[i].numblocks << eraseshift;
3593 mtd->numeraseregions -= 1;
3596 /* Expose MLC erase size except when all blocks are SLC */
3597 mtd->erasesize = 1 << this->erase_shift;
3598 if (mtd->numeraseregions == 1)
3599 mtd->erasesize >>= 1;
3601 printk(KERN_INFO "Device has %d eraseregions\n", mtd->numeraseregions);
3602 for (i = 0; i < mtd->numeraseregions; i++)
3603 printk(KERN_INFO "[offset: 0x%08x, erasesize: 0x%05x,"
3604 " numblocks: %04u]\n",
3605 (unsigned int) mtd->eraseregions[i].offset,
3606 mtd->eraseregions[i].erasesize,
3607 mtd->eraseregions[i].numblocks);
3609 for (die = 0, mtd->size = 0; die < this->dies; die++) {
3610 this->diesize[die] = (loff_t)blksperdie << this->erase_shift;
3611 this->diesize[die] -= (loff_t)(this->boundary[die] + 1)
3612 << (this->erase_shift - 1);
3613 mtd->size += this->diesize[die];
3618 * flexonenand_check_blocks_erased - Check if blocks are erased
3619 * @param mtd_info - mtd info structure
3620 * @param start - first erase block to check
3621 * @param end - last erase block to check
3623 * Converting an unerased block from MLC to SLC
3624 * causes byte values to change. Since both data and its ECC
3625 * have changed, reads on the block give uncorrectable error.
3626 * This might lead to the block being detected as bad.
3628 * Avoid this by ensuring that the block to be converted is
3631 static int flexonenand_check_blocks_erased(struct mtd_info *mtd, int start, int end)
3633 struct onenand_chip *this = mtd->priv;
3636 struct mtd_oob_ops ops = {
3637 .mode = MTD_OOB_PLACE,
3639 .ooblen = mtd->oobsize,
3641 .oobbuf = this->oob_buf,
3645 printk(KERN_DEBUG "Check blocks from %d to %d\n", start, end);
3647 for (block = start; block <= end; block++) {
3648 addr = flexonenand_addr(this, block);
3649 if (onenand_block_isbad_nolock(mtd, addr, 0))
3653 * Since main area write results in ECC write to spare,
3654 * it is sufficient to check only ECC bytes for change.
3656 ret = onenand_read_oob_nolock(mtd, addr, &ops);
3660 for (i = 0; i < mtd->oobsize; i++)
3661 if (this->oob_buf[i] != 0xff)
3664 if (i != mtd->oobsize) {
3665 printk(KERN_WARNING "%s: Block %d not erased.\n",
3675 * flexonenand_set_boundary - Writes the SLC boundary
3676 * @param mtd - mtd info structure
3678 int flexonenand_set_boundary(struct mtd_info *mtd, int die,
3679 int boundary, int lock)
3681 struct onenand_chip *this = mtd->priv;
3682 int ret, density, blksperdie, old, new, thisboundary;
3685 /* Change only once for SDP Flex-OneNAND */
3686 if (die && (!ONENAND_IS_DDP(this)))
3689 /* boundary value of -1 indicates no required change */
3690 if (boundary < 0 || boundary == this->boundary[die])
3693 density = onenand_get_density(this->device_id);
3694 blksperdie = ((16 << density) << 20) >> this->erase_shift;
3695 blksperdie >>= ONENAND_IS_DDP(this) ? 1 : 0;
3697 if (boundary >= blksperdie) {
3698 printk(KERN_ERR "%s: Invalid boundary value. "
3699 "Boundary not changed.\n", __func__);
3703 /* Check if converting blocks are erased */
3704 old = this->boundary[die] + (die * this->density_mask);
3705 new = boundary + (die * this->density_mask);
3706 ret = flexonenand_check_blocks_erased(mtd, min(old, new) + 1, max(old, new));
3708 printk(KERN_ERR "%s: Please erase blocks "
3709 "before boundary change\n", __func__);
3713 this->command(mtd, FLEXONENAND_CMD_PI_ACCESS, die, 0);
3714 this->wait(mtd, FL_SYNCING);
3716 /* Check is boundary is locked */
3717 this->command(mtd, FLEXONENAND_CMD_READ_PI, die, 0);
3718 ret = this->wait(mtd, FL_READING);
3720 thisboundary = this->read_word(this->base + ONENAND_DATARAM);
3721 if ((thisboundary >> FLEXONENAND_PI_UNLOCK_SHIFT) != 3) {
3722 printk(KERN_ERR "%s: boundary locked\n", __func__);
3727 printk(KERN_INFO "Changing die %d boundary: %d%s\n",
3728 die, boundary, lock ? "(Locked)" : "(Unlocked)");
3730 addr = die ? this->diesize[0] : 0;
3732 boundary &= FLEXONENAND_PI_MASK;
3733 boundary |= lock ? 0 : (3 << FLEXONENAND_PI_UNLOCK_SHIFT);
3735 this->command(mtd, ONENAND_CMD_ERASE, addr, 0);
3736 ret = this->wait(mtd, FL_ERASING);
3738 printk(KERN_ERR "%s: Failed PI erase for Die %d\n",
3743 this->write_word(boundary, this->base + ONENAND_DATARAM);
3744 this->command(mtd, ONENAND_CMD_PROG, addr, 0);
3745 ret = this->wait(mtd, FL_WRITING);
3747 printk(KERN_ERR "%s: Failed PI write for Die %d\n",
3752 this->command(mtd, FLEXONENAND_CMD_PI_UPDATE, die, 0);
3753 ret = this->wait(mtd, FL_WRITING);
3755 this->write_word(ONENAND_CMD_RESET, this->base + ONENAND_REG_COMMAND);
3756 this->wait(mtd, FL_RESETING);
3758 /* Recalculate device size on boundary change*/
3759 flexonenand_get_size(mtd);
3765 * onenand_chip_probe - [OneNAND Interface] The generic chip probe
3766 * @param mtd MTD device structure
3768 * OneNAND detection method:
3769 * Compare the values from command with ones from register
3771 static int onenand_chip_probe(struct mtd_info *mtd)
3773 struct onenand_chip *this = mtd->priv;
3774 int bram_maf_id, bram_dev_id, maf_id, dev_id;
3777 /* Save system configuration 1 */
3778 syscfg = this->read_word(this->base + ONENAND_REG_SYS_CFG1);
3779 /* Clear Sync. Burst Read mode to read BootRAM */
3780 this->write_word((syscfg & ~ONENAND_SYS_CFG1_SYNC_READ & ~ONENAND_SYS_CFG1_SYNC_WRITE), this->base + ONENAND_REG_SYS_CFG1);
3782 /* Send the command for reading device ID from BootRAM */
3783 this->write_word(ONENAND_CMD_READID, this->base + ONENAND_BOOTRAM);
3785 /* Read manufacturer and device IDs from BootRAM */
3786 bram_maf_id = this->read_word(this->base + ONENAND_BOOTRAM + 0x0);
3787 bram_dev_id = this->read_word(this->base + ONENAND_BOOTRAM + 0x2);
3789 /* Reset OneNAND to read default register values */
3790 this->write_word(ONENAND_CMD_RESET, this->base + ONENAND_BOOTRAM);
3792 this->wait(mtd, FL_RESETING);
3794 /* Restore system configuration 1 */
3795 this->write_word(syscfg, this->base + ONENAND_REG_SYS_CFG1);
3797 /* Check manufacturer ID */
3798 if (onenand_check_maf(bram_maf_id))
3801 /* Read manufacturer and device IDs from Register */
3802 maf_id = this->read_word(this->base + ONENAND_REG_MANUFACTURER_ID);
3803 dev_id = this->read_word(this->base + ONENAND_REG_DEVICE_ID);
3805 /* Check OneNAND device */
3806 if (maf_id != bram_maf_id || dev_id != bram_dev_id)
3813 * onenand_probe - [OneNAND Interface] Probe the OneNAND device
3814 * @param mtd MTD device structure
3816 static int onenand_probe(struct mtd_info *mtd)
3818 struct onenand_chip *this = mtd->priv;
3819 int maf_id, dev_id, ver_id;
3823 ret = this->chip_probe(mtd);
3827 /* Read manufacturer and device IDs from Register */
3828 maf_id = this->read_word(this->base + ONENAND_REG_MANUFACTURER_ID);
3829 dev_id = this->read_word(this->base + ONENAND_REG_DEVICE_ID);
3830 ver_id = this->read_word(this->base + ONENAND_REG_VERSION_ID);
3831 this->technology = this->read_word(this->base + ONENAND_REG_TECHNOLOGY);
3833 /* Flash device information */
3834 onenand_print_device_info(dev_id, ver_id);
3835 this->device_id = dev_id;
3836 this->version_id = ver_id;
3838 /* Check OneNAND features */
3839 onenand_check_features(mtd);
3841 density = onenand_get_density(dev_id);
3842 if (FLEXONENAND(this)) {
3843 this->dies = ONENAND_IS_DDP(this) ? 2 : 1;
3844 /* Maximum possible erase regions */
3845 mtd->numeraseregions = this->dies << 1;
3846 mtd->eraseregions = kzalloc(sizeof(struct mtd_erase_region_info)
3847 * (this->dies << 1), GFP_KERNEL);
3848 if (!mtd->eraseregions)
3853 * For Flex-OneNAND, chipsize represents maximum possible device size.
3854 * mtd->size represents the actual device size.
3856 this->chipsize = (16 << density) << 20;
3858 /* OneNAND page size & block size */
3859 /* The data buffer size is equal to page size */
3860 mtd->writesize = this->read_word(this->base + ONENAND_REG_DATA_BUFFER_SIZE);
3861 /* We use the full BufferRAM */
3862 if (ONENAND_IS_4KB_PAGE(this))
3863 mtd->writesize <<= 1;
3865 mtd->oobsize = mtd->writesize >> 5;
3866 /* Pages per a block are always 64 in OneNAND */
3867 mtd->erasesize = mtd->writesize << 6;
3869 * Flex-OneNAND SLC area has 64 pages per block.
3870 * Flex-OneNAND MLC area has 128 pages per block.
3871 * Expose MLC erase size to find erase_shift and page_mask.
3873 if (FLEXONENAND(this))
3874 mtd->erasesize <<= 1;
3876 this->erase_shift = ffs(mtd->erasesize) - 1;
3877 this->page_shift = ffs(mtd->writesize) - 1;
3878 this->page_mask = (1 << (this->erase_shift - this->page_shift)) - 1;
3879 /* Set density mask. it is used for DDP */
3880 if (ONENAND_IS_DDP(this))
3881 this->density_mask = this->chipsize >> (this->erase_shift + 1);
3882 /* It's real page size */
3883 this->writesize = mtd->writesize;
3885 /* REVISIT: Multichip handling */
3887 if (FLEXONENAND(this))
3888 flexonenand_get_size(mtd);
3890 mtd->size = this->chipsize;
3893 * We emulate the 4KiB page and 256KiB erase block size
3894 * But oobsize is still 64 bytes.
3895 * It is only valid if you turn on 2X program support,
3896 * Otherwise it will be ignored by compiler.
3898 if (ONENAND_IS_2PLANE(this)) {
3899 mtd->writesize <<= 1;
3900 mtd->erasesize <<= 1;
3907 * onenand_suspend - [MTD Interface] Suspend the OneNAND flash
3908 * @param mtd MTD device structure
3910 static int onenand_suspend(struct mtd_info *mtd)
3912 return onenand_get_device(mtd, FL_PM_SUSPENDED);
3916 * onenand_resume - [MTD Interface] Resume the OneNAND flash
3917 * @param mtd MTD device structure
3919 static void onenand_resume(struct mtd_info *mtd)
3921 struct onenand_chip *this = mtd->priv;
3923 if (this->state == FL_PM_SUSPENDED)
3924 onenand_release_device(mtd);
3926 printk(KERN_ERR "%s: resume() called for the chip which is not "
3927 "in suspended state\n", __func__);
3931 * onenand_scan - [OneNAND Interface] Scan for the OneNAND device
3932 * @param mtd MTD device structure
3933 * @param maxchips Number of chips to scan for
3935 * This fills out all the not initialized function pointers
3936 * with the defaults.
3937 * The flash ID is read and the mtd/chip structures are
3938 * filled with the appropriate values.
3940 int onenand_scan(struct mtd_info *mtd, int maxchips)
3943 struct onenand_chip *this = mtd->priv;
3945 if (!this->read_word)
3946 this->read_word = onenand_readw;
3947 if (!this->write_word)
3948 this->write_word = onenand_writew;
3951 this->command = onenand_command;
3953 onenand_setup_wait(mtd);
3954 if (!this->bbt_wait)
3955 this->bbt_wait = onenand_bbt_wait;
3956 if (!this->unlock_all)
3957 this->unlock_all = onenand_unlock_all;
3959 if (!this->chip_probe)
3960 this->chip_probe = onenand_chip_probe;
3962 if (!this->read_bufferram)
3963 this->read_bufferram = onenand_read_bufferram;
3964 if (!this->write_bufferram)
3965 this->write_bufferram = onenand_write_bufferram;
3967 if (!this->block_markbad)
3968 this->block_markbad = onenand_default_block_markbad;
3969 if (!this->scan_bbt)
3970 this->scan_bbt = onenand_default_bbt;
3972 if (onenand_probe(mtd))
3975 /* Set Sync. Burst Read after probing */
3976 if (this->mmcontrol) {
3977 printk(KERN_INFO "OneNAND Sync. Burst Read support\n");
3978 this->read_bufferram = onenand_sync_read_bufferram;
3981 /* Allocate buffers, if necessary */
3982 if (!this->page_buf) {
3983 this->page_buf = kzalloc(mtd->writesize, GFP_KERNEL);
3984 if (!this->page_buf) {
3985 printk(KERN_ERR "%s: Can't allocate page_buf\n",
3989 #ifdef CONFIG_MTD_ONENAND_VERIFY_WRITE
3990 this->verify_buf = kzalloc(mtd->writesize, GFP_KERNEL);
3991 if (!this->verify_buf) {
3992 kfree(this->page_buf);
3996 this->options |= ONENAND_PAGEBUF_ALLOC;
3998 if (!this->oob_buf) {
3999 this->oob_buf = kzalloc(mtd->oobsize, GFP_KERNEL);
4000 if (!this->oob_buf) {
4001 printk(KERN_ERR "%s: Can't allocate oob_buf\n",
4003 if (this->options & ONENAND_PAGEBUF_ALLOC) {
4004 this->options &= ~ONENAND_PAGEBUF_ALLOC;
4005 kfree(this->page_buf);
4009 this->options |= ONENAND_OOBBUF_ALLOC;
4012 this->state = FL_READY;
4013 init_waitqueue_head(&this->wq);
4014 spin_lock_init(&this->chip_lock);
4017 * Allow subpage writes up to oobsize.
4019 switch (mtd->oobsize) {
4021 this->ecclayout = &onenand_oob_128;
4022 mtd->subpage_sft = 0;
4025 this->ecclayout = &onenand_oob_64;
4026 mtd->subpage_sft = 2;
4030 this->ecclayout = &onenand_oob_32;
4031 mtd->subpage_sft = 1;
4035 printk(KERN_WARNING "%s: No OOB scheme defined for oobsize %d\n",
4036 __func__, mtd->oobsize);
4037 mtd->subpage_sft = 0;
4038 /* To prevent kernel oops */
4039 this->ecclayout = &onenand_oob_32;
4043 this->subpagesize = mtd->writesize >> mtd->subpage_sft;
4046 * The number of bytes available for a client to place data into
4047 * the out of band area
4049 this->ecclayout->oobavail = 0;
4050 for (i = 0; i < MTD_MAX_OOBFREE_ENTRIES &&
4051 this->ecclayout->oobfree[i].length; i++)
4052 this->ecclayout->oobavail +=
4053 this->ecclayout->oobfree[i].length;
4054 mtd->oobavail = this->ecclayout->oobavail;
4056 mtd->ecclayout = this->ecclayout;
4058 /* Fill in remaining MTD driver data */
4059 mtd->type = ONENAND_IS_MLC(this) ? MTD_MLCNANDFLASH : MTD_NANDFLASH;
4060 mtd->flags = MTD_CAP_NANDFLASH;
4061 mtd->erase = onenand_erase;
4063 mtd->unpoint = NULL;
4064 mtd->read = onenand_read;
4065 mtd->write = onenand_write;
4066 mtd->read_oob = onenand_read_oob;
4067 mtd->write_oob = onenand_write_oob;
4068 mtd->panic_write = onenand_panic_write;
4069 #ifdef CONFIG_MTD_ONENAND_OTP
4070 mtd->get_fact_prot_info = onenand_get_fact_prot_info;
4071 mtd->read_fact_prot_reg = onenand_read_fact_prot_reg;
4072 mtd->get_user_prot_info = onenand_get_user_prot_info;
4073 mtd->read_user_prot_reg = onenand_read_user_prot_reg;
4074 mtd->write_user_prot_reg = onenand_write_user_prot_reg;
4075 mtd->lock_user_prot_reg = onenand_lock_user_prot_reg;
4077 mtd->sync = onenand_sync;
4078 mtd->lock = onenand_lock;
4079 mtd->unlock = onenand_unlock;
4080 mtd->suspend = onenand_suspend;
4081 mtd->resume = onenand_resume;
4082 mtd->block_isbad = onenand_block_isbad;
4083 mtd->block_markbad = onenand_block_markbad;
4084 mtd->owner = THIS_MODULE;
4085 mtd->writebufsize = mtd->writesize;
4087 /* Unlock whole block */
4088 if (!(this->options & ONENAND_SKIP_INITIAL_UNLOCKING))
4089 this->unlock_all(mtd);
4091 ret = this->scan_bbt(mtd);
4092 if ((!FLEXONENAND(this)) || ret)
4095 /* Change Flex-OneNAND boundaries if required */
4096 for (i = 0; i < MAX_DIES; i++)
4097 flexonenand_set_boundary(mtd, i, flex_bdry[2 * i],
4098 flex_bdry[(2 * i) + 1]);
4104 * onenand_release - [OneNAND Interface] Free resources held by the OneNAND device
4105 * @param mtd MTD device structure
4107 void onenand_release(struct mtd_info *mtd)
4109 struct onenand_chip *this = mtd->priv;
4111 #ifdef CONFIG_MTD_PARTITIONS
4112 /* Deregister partitions */
4113 del_mtd_partitions (mtd);
4115 /* Deregister the device */
4116 del_mtd_device (mtd);
4118 /* Free bad block table memory, if allocated */
4120 struct bbm_info *bbm = this->bbm;
4124 /* Buffers allocated by onenand_scan */
4125 if (this->options & ONENAND_PAGEBUF_ALLOC) {
4126 kfree(this->page_buf);
4127 #ifdef CONFIG_MTD_ONENAND_VERIFY_WRITE
4128 kfree(this->verify_buf);
4131 if (this->options & ONENAND_OOBBUF_ALLOC)
4132 kfree(this->oob_buf);
4133 kfree(mtd->eraseregions);
4136 EXPORT_SYMBOL_GPL(onenand_scan);
4137 EXPORT_SYMBOL_GPL(onenand_release);
4139 MODULE_LICENSE("GPL");
4140 MODULE_AUTHOR("Kyungmin Park <kyungmin.park@samsung.com>");
4141 MODULE_DESCRIPTION("Generic OneNAND flash driver code");