2 * linux/drivers/mtd/onenand/onenand_base.c
4 * Copyright (C) 2005-2007 Samsung Electronics
5 * Kyungmin Park <kyungmin.park@samsung.com>
8 * Adrian Hunter <ext-adrian.hunter@nokia.com>:
9 * auto-placement support, read-while load support, various fixes
10 * Copyright (C) Nokia Corporation, 2007
12 * Vishak G <vishak.g at samsung.com>, Rohit Hagargundgi <h.rohit at samsung.com>
13 * Flex-OneNAND support
14 * Copyright (C) Samsung Electronics, 2008
16 * This program is free software; you can redistribute it and/or modify
17 * it under the terms of the GNU General Public License version 2 as
18 * published by the Free Software Foundation.
21 #include <linux/kernel.h>
22 #include <linux/module.h>
23 #include <linux/init.h>
24 #include <linux/sched.h>
25 #include <linux/delay.h>
26 #include <linux/interrupt.h>
27 #include <linux/jiffies.h>
28 #include <linux/mtd/mtd.h>
29 #include <linux/mtd/onenand.h>
30 #include <linux/mtd/partitions.h>
34 /* Default Flex-OneNAND boundary and lock respectively */
35 static int flex_bdry[MAX_DIES * 2] = { -1, 0, -1, 0 };
38 * onenand_oob_128 - oob info for Flex-Onenand with 4KB page
39 * For now, we expose only 64 out of 80 ecc bytes
41 static struct nand_ecclayout onenand_oob_128 = {
44 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
45 22, 23, 24, 25, 26, 27, 28, 29, 30, 31,
46 38, 39, 40, 41, 42, 43, 44, 45, 46, 47,
47 54, 55, 56, 57, 58, 59, 60, 61, 62, 63,
48 70, 71, 72, 73, 74, 75, 76, 77, 78, 79,
49 86, 87, 88, 89, 90, 91, 92, 93, 94, 95,
53 {2, 4}, {18, 4}, {34, 4}, {50, 4},
54 {66, 4}, {82, 4}, {98, 4}, {114, 4}
59 * onenand_oob_64 - oob info for large (2KB) page
61 static struct nand_ecclayout onenand_oob_64 = {
70 {2, 3}, {14, 2}, {18, 3}, {30, 2},
71 {34, 3}, {46, 2}, {50, 3}, {62, 2}
76 * onenand_oob_32 - oob info for middle (1KB) page
78 static struct nand_ecclayout onenand_oob_32 = {
84 .oobfree = { {2, 3}, {14, 2}, {18, 3}, {30, 2} }
87 static const unsigned char ffchars[] = {
88 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
89 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 16 */
90 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
91 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 32 */
92 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
93 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 48 */
94 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
95 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 64 */
96 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
97 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 80 */
98 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
99 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 96 */
100 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
101 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 112 */
102 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
103 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 128 */
107 * onenand_readw - [OneNAND Interface] Read OneNAND register
108 * @param addr address to read
110 * Read OneNAND register
112 static unsigned short onenand_readw(void __iomem *addr)
118 * onenand_writew - [OneNAND Interface] Write OneNAND register with value
119 * @param value value to write
120 * @param addr address to write
122 * Write OneNAND register with value
124 static void onenand_writew(unsigned short value, void __iomem *addr)
130 * onenand_block_address - [DEFAULT] Get block address
131 * @param this onenand chip data structure
132 * @param block the block
133 * @return translated block address if DDP, otherwise same
135 * Setup Start Address 1 Register (F100h)
137 static int onenand_block_address(struct onenand_chip *this, int block)
139 /* Device Flash Core select, NAND Flash Block Address */
140 if (block & this->density_mask)
141 return ONENAND_DDP_CHIP1 | (block ^ this->density_mask);
147 * onenand_bufferram_address - [DEFAULT] Get bufferram address
148 * @param this onenand chip data structure
149 * @param block the block
150 * @return set DBS value if DDP, otherwise 0
152 * Setup Start Address 2 Register (F101h) for DDP
154 static int onenand_bufferram_address(struct onenand_chip *this, int block)
156 /* Device BufferRAM Select */
157 if (block & this->density_mask)
158 return ONENAND_DDP_CHIP1;
160 return ONENAND_DDP_CHIP0;
164 * onenand_page_address - [DEFAULT] Get page address
165 * @param page the page address
166 * @param sector the sector address
167 * @return combined page and sector address
169 * Setup Start Address 8 Register (F107h)
171 static int onenand_page_address(int page, int sector)
173 /* Flash Page Address, Flash Sector Address */
176 fpa = page & ONENAND_FPA_MASK;
177 fsa = sector & ONENAND_FSA_MASK;
179 return ((fpa << ONENAND_FPA_SHIFT) | fsa);
183 * onenand_buffer_address - [DEFAULT] Get buffer address
184 * @param dataram1 DataRAM index
185 * @param sectors the sector address
186 * @param count the number of sectors
187 * @return the start buffer value
189 * Setup Start Buffer Register (F200h)
191 static int onenand_buffer_address(int dataram1, int sectors, int count)
195 /* BufferRAM Sector Address */
196 bsa = sectors & ONENAND_BSA_MASK;
199 bsa |= ONENAND_BSA_DATARAM1; /* DataRAM1 */
201 bsa |= ONENAND_BSA_DATARAM0; /* DataRAM0 */
203 /* BufferRAM Sector Count */
204 bsc = count & ONENAND_BSC_MASK;
206 return ((bsa << ONENAND_BSA_SHIFT) | bsc);
210 * flexonenand_block- For given address return block number
211 * @param this - OneNAND device structure
212 * @param addr - Address for which block number is needed
214 static unsigned flexonenand_block(struct onenand_chip *this, loff_t addr)
216 unsigned boundary, blk, die = 0;
218 if (ONENAND_IS_DDP(this) && addr >= this->diesize[0]) {
220 addr -= this->diesize[0];
223 boundary = this->boundary[die];
225 blk = addr >> (this->erase_shift - 1);
227 blk = (blk + boundary + 1) >> 1;
229 blk += die ? this->density_mask : 0;
233 inline unsigned onenand_block(struct onenand_chip *this, loff_t addr)
235 if (!FLEXONENAND(this))
236 return addr >> this->erase_shift;
237 return flexonenand_block(this, addr);
241 * flexonenand_addr - Return address of the block
242 * @this: OneNAND device structure
243 * @block: Block number on Flex-OneNAND
245 * Return address of the block
247 static loff_t flexonenand_addr(struct onenand_chip *this, int block)
250 int die = 0, boundary;
252 if (ONENAND_IS_DDP(this) && block >= this->density_mask) {
253 block -= this->density_mask;
255 ofs = this->diesize[0];
258 boundary = this->boundary[die];
259 ofs += (loff_t)block << (this->erase_shift - 1);
260 if (block > (boundary + 1))
261 ofs += (loff_t)(block - boundary - 1) << (this->erase_shift - 1);
265 loff_t onenand_addr(struct onenand_chip *this, int block)
267 if (!FLEXONENAND(this))
268 return (loff_t)block << this->erase_shift;
269 return flexonenand_addr(this, block);
271 EXPORT_SYMBOL(onenand_addr);
274 * onenand_get_density - [DEFAULT] Get OneNAND density
275 * @param dev_id OneNAND device ID
277 * Get OneNAND density from device ID
279 static inline int onenand_get_density(int dev_id)
281 int density = dev_id >> ONENAND_DEVICE_DENSITY_SHIFT;
282 return (density & ONENAND_DEVICE_DENSITY_MASK);
286 * flexonenand_region - [Flex-OneNAND] Return erase region of addr
287 * @param mtd MTD device structure
288 * @param addr address whose erase region needs to be identified
290 int flexonenand_region(struct mtd_info *mtd, loff_t addr)
294 for (i = 0; i < mtd->numeraseregions; i++)
295 if (addr < mtd->eraseregions[i].offset)
299 EXPORT_SYMBOL(flexonenand_region);
302 * onenand_command - [DEFAULT] Send command to OneNAND device
303 * @param mtd MTD device structure
304 * @param cmd the command to be sent
305 * @param addr offset to read from or write to
306 * @param len number of bytes to read or write
308 * Send command to OneNAND device. This function is used for middle/large page
309 * devices (1KB/2KB Bytes per page)
311 static int onenand_command(struct mtd_info *mtd, int cmd, loff_t addr, size_t len)
313 struct onenand_chip *this = mtd->priv;
314 int value, block, page;
316 /* Address translation */
318 case ONENAND_CMD_UNLOCK:
319 case ONENAND_CMD_LOCK:
320 case ONENAND_CMD_LOCK_TIGHT:
321 case ONENAND_CMD_UNLOCK_ALL:
326 case FLEXONENAND_CMD_PI_ACCESS:
327 /* addr contains die index */
328 block = addr * this->density_mask;
332 case ONENAND_CMD_ERASE:
333 case ONENAND_CMD_BUFFERRAM:
334 case ONENAND_CMD_OTP_ACCESS:
335 block = onenand_block(this, addr);
339 case FLEXONENAND_CMD_READ_PI:
340 cmd = ONENAND_CMD_READ;
341 block = addr * this->density_mask;
346 block = onenand_block(this, addr);
347 page = (int) (addr - onenand_addr(this, block)) >> this->page_shift;
349 if (ONENAND_IS_2PLANE(this)) {
350 /* Make the even block number */
352 /* Is it the odd plane? */
353 if (addr & this->writesize)
357 page &= this->page_mask;
361 /* NOTE: The setting order of the registers is very important! */
362 if (cmd == ONENAND_CMD_BUFFERRAM) {
363 /* Select DataRAM for DDP */
364 value = onenand_bufferram_address(this, block);
365 this->write_word(value, this->base + ONENAND_REG_START_ADDRESS2);
367 if (ONENAND_IS_MLC(this) || ONENAND_IS_2PLANE(this))
368 /* It is always BufferRAM0 */
369 ONENAND_SET_BUFFERRAM0(this);
371 /* Switch to the next data buffer */
372 ONENAND_SET_NEXT_BUFFERRAM(this);
378 /* Write 'DFS, FBA' of Flash */
379 value = onenand_block_address(this, block);
380 this->write_word(value, this->base + ONENAND_REG_START_ADDRESS1);
382 /* Select DataRAM for DDP */
383 value = onenand_bufferram_address(this, block);
384 this->write_word(value, this->base + ONENAND_REG_START_ADDRESS2);
388 /* Now we use page size operation */
389 int sectors = 0, count = 0;
393 case FLEXONENAND_CMD_RECOVER_LSB:
394 case ONENAND_CMD_READ:
395 case ONENAND_CMD_READOOB:
396 if (ONENAND_IS_MLC(this))
397 /* It is always BufferRAM0 */
398 dataram = ONENAND_SET_BUFFERRAM0(this);
400 dataram = ONENAND_SET_NEXT_BUFFERRAM(this);
404 if (ONENAND_IS_2PLANE(this) && cmd == ONENAND_CMD_PROG)
405 cmd = ONENAND_CMD_2X_PROG;
406 dataram = ONENAND_CURRENT_BUFFERRAM(this);
410 /* Write 'FPA, FSA' of Flash */
411 value = onenand_page_address(page, sectors);
412 this->write_word(value, this->base + ONENAND_REG_START_ADDRESS8);
414 /* Write 'BSA, BSC' of DataRAM */
415 value = onenand_buffer_address(dataram, sectors, count);
416 this->write_word(value, this->base + ONENAND_REG_START_BUFFER);
419 /* Interrupt clear */
420 this->write_word(ONENAND_INT_CLEAR, this->base + ONENAND_REG_INTERRUPT);
423 this->write_word(cmd, this->base + ONENAND_REG_COMMAND);
429 * onenand_read_ecc - return ecc status
430 * @param this onenand chip structure
432 static inline int onenand_read_ecc(struct onenand_chip *this)
434 int ecc, i, result = 0;
436 if (!FLEXONENAND(this))
437 return this->read_word(this->base + ONENAND_REG_ECC_STATUS);
439 for (i = 0; i < 4; i++) {
440 ecc = this->read_word(this->base + ONENAND_REG_ECC_STATUS + i);
443 if (ecc & FLEXONENAND_UNCORRECTABLE_ERROR)
444 return ONENAND_ECC_2BIT_ALL;
446 result = ONENAND_ECC_1BIT_ALL;
453 * onenand_wait - [DEFAULT] wait until the command is done
454 * @param mtd MTD device structure
455 * @param state state to select the max. timeout value
457 * Wait for command done. This applies to all OneNAND command
458 * Read can take up to 30us, erase up to 2ms and program up to 350us
459 * according to general OneNAND specs
461 static int onenand_wait(struct mtd_info *mtd, int state)
463 struct onenand_chip * this = mtd->priv;
464 unsigned long timeout;
465 unsigned int flags = ONENAND_INT_MASTER;
466 unsigned int interrupt = 0;
469 /* The 20 msec is enough */
470 timeout = jiffies + msecs_to_jiffies(20);
471 while (time_before(jiffies, timeout)) {
472 interrupt = this->read_word(this->base + ONENAND_REG_INTERRUPT);
474 if (interrupt & flags)
477 if (state != FL_READING)
480 /* To get correct interrupt status in timeout case */
481 interrupt = this->read_word(this->base + ONENAND_REG_INTERRUPT);
483 ctrl = this->read_word(this->base + ONENAND_REG_CTRL_STATUS);
486 * In the Spec. it checks the controller status first
487 * However if you get the correct information in case of
488 * power off recovery (POR) test, it should read ECC status first
490 if (interrupt & ONENAND_INT_READ) {
491 int ecc = onenand_read_ecc(this);
493 if (ecc & ONENAND_ECC_2BIT_ALL) {
494 printk(KERN_ERR "onenand_wait: ECC error = 0x%04x\n", ecc);
495 mtd->ecc_stats.failed++;
497 } else if (ecc & ONENAND_ECC_1BIT_ALL) {
498 printk(KERN_DEBUG "onenand_wait: correctable ECC error = 0x%04x\n", ecc);
499 mtd->ecc_stats.corrected++;
502 } else if (state == FL_READING) {
503 printk(KERN_ERR "onenand_wait: read timeout! ctrl=0x%04x intr=0x%04x\n", ctrl, interrupt);
507 /* If there's controller error, it's a real error */
508 if (ctrl & ONENAND_CTRL_ERROR) {
509 printk(KERN_ERR "onenand_wait: controller error = 0x%04x\n",
511 if (ctrl & ONENAND_CTRL_LOCK)
512 printk(KERN_ERR "onenand_wait: it's locked error.\n");
520 * onenand_interrupt - [DEFAULT] onenand interrupt handler
521 * @param irq onenand interrupt number
522 * @param dev_id interrupt data
526 static irqreturn_t onenand_interrupt(int irq, void *data)
528 struct onenand_chip *this = data;
530 /* To handle shared interrupt */
531 if (!this->complete.done)
532 complete(&this->complete);
538 * onenand_interrupt_wait - [DEFAULT] wait until the command is done
539 * @param mtd MTD device structure
540 * @param state state to select the max. timeout value
542 * Wait for command done.
544 static int onenand_interrupt_wait(struct mtd_info *mtd, int state)
546 struct onenand_chip *this = mtd->priv;
548 wait_for_completion(&this->complete);
550 return onenand_wait(mtd, state);
554 * onenand_try_interrupt_wait - [DEFAULT] try interrupt wait
555 * @param mtd MTD device structure
556 * @param state state to select the max. timeout value
558 * Try interrupt based wait (It is used one-time)
560 static int onenand_try_interrupt_wait(struct mtd_info *mtd, int state)
562 struct onenand_chip *this = mtd->priv;
563 unsigned long remain, timeout;
565 /* We use interrupt wait first */
566 this->wait = onenand_interrupt_wait;
568 timeout = msecs_to_jiffies(100);
569 remain = wait_for_completion_timeout(&this->complete, timeout);
571 printk(KERN_INFO "OneNAND: There's no interrupt. "
572 "We use the normal wait\n");
574 /* Release the irq */
575 free_irq(this->irq, this);
577 this->wait = onenand_wait;
580 return onenand_wait(mtd, state);
584 * onenand_setup_wait - [OneNAND Interface] setup onenand wait method
585 * @param mtd MTD device structure
587 * There's two method to wait onenand work
588 * 1. polling - read interrupt status register
589 * 2. interrupt - use the kernel interrupt method
591 static void onenand_setup_wait(struct mtd_info *mtd)
593 struct onenand_chip *this = mtd->priv;
596 init_completion(&this->complete);
598 if (this->irq <= 0) {
599 this->wait = onenand_wait;
603 if (request_irq(this->irq, &onenand_interrupt,
604 IRQF_SHARED, "onenand", this)) {
605 /* If we can't get irq, use the normal wait */
606 this->wait = onenand_wait;
610 /* Enable interrupt */
611 syscfg = this->read_word(this->base + ONENAND_REG_SYS_CFG1);
612 syscfg |= ONENAND_SYS_CFG1_IOBE;
613 this->write_word(syscfg, this->base + ONENAND_REG_SYS_CFG1);
615 this->wait = onenand_try_interrupt_wait;
619 * onenand_bufferram_offset - [DEFAULT] BufferRAM offset
620 * @param mtd MTD data structure
621 * @param area BufferRAM area
622 * @return offset given area
624 * Return BufferRAM offset given area
626 static inline int onenand_bufferram_offset(struct mtd_info *mtd, int area)
628 struct onenand_chip *this = mtd->priv;
630 if (ONENAND_CURRENT_BUFFERRAM(this)) {
631 /* Note: the 'this->writesize' is a real page size */
632 if (area == ONENAND_DATARAM)
633 return this->writesize;
634 if (area == ONENAND_SPARERAM)
642 * onenand_read_bufferram - [OneNAND Interface] Read the bufferram area
643 * @param mtd MTD data structure
644 * @param area BufferRAM area
645 * @param buffer the databuffer to put/get data
646 * @param offset offset to read from or write to
647 * @param count number of bytes to read/write
649 * Read the BufferRAM area
651 static int onenand_read_bufferram(struct mtd_info *mtd, int area,
652 unsigned char *buffer, int offset, size_t count)
654 struct onenand_chip *this = mtd->priv;
655 void __iomem *bufferram;
657 bufferram = this->base + area;
659 bufferram += onenand_bufferram_offset(mtd, area);
661 if (ONENAND_CHECK_BYTE_ACCESS(count)) {
664 /* Align with word(16-bit) size */
667 /* Read word and save byte */
668 word = this->read_word(bufferram + offset + count);
669 buffer[count] = (word & 0xff);
672 memcpy(buffer, bufferram + offset, count);
678 * onenand_sync_read_bufferram - [OneNAND Interface] Read the bufferram area with Sync. Burst mode
679 * @param mtd MTD data structure
680 * @param area BufferRAM area
681 * @param buffer the databuffer to put/get data
682 * @param offset offset to read from or write to
683 * @param count number of bytes to read/write
685 * Read the BufferRAM area with Sync. Burst Mode
687 static int onenand_sync_read_bufferram(struct mtd_info *mtd, int area,
688 unsigned char *buffer, int offset, size_t count)
690 struct onenand_chip *this = mtd->priv;
691 void __iomem *bufferram;
693 bufferram = this->base + area;
695 bufferram += onenand_bufferram_offset(mtd, area);
697 this->mmcontrol(mtd, ONENAND_SYS_CFG1_SYNC_READ);
699 if (ONENAND_CHECK_BYTE_ACCESS(count)) {
702 /* Align with word(16-bit) size */
705 /* Read word and save byte */
706 word = this->read_word(bufferram + offset + count);
707 buffer[count] = (word & 0xff);
710 memcpy(buffer, bufferram + offset, count);
712 this->mmcontrol(mtd, 0);
718 * onenand_write_bufferram - [OneNAND Interface] Write the bufferram area
719 * @param mtd MTD data structure
720 * @param area BufferRAM area
721 * @param buffer the databuffer to put/get data
722 * @param offset offset to read from or write to
723 * @param count number of bytes to read/write
725 * Write the BufferRAM area
727 static int onenand_write_bufferram(struct mtd_info *mtd, int area,
728 const unsigned char *buffer, int offset, size_t count)
730 struct onenand_chip *this = mtd->priv;
731 void __iomem *bufferram;
733 bufferram = this->base + area;
735 bufferram += onenand_bufferram_offset(mtd, area);
737 if (ONENAND_CHECK_BYTE_ACCESS(count)) {
741 /* Align with word(16-bit) size */
744 /* Calculate byte access offset */
745 byte_offset = offset + count;
747 /* Read word and save byte */
748 word = this->read_word(bufferram + byte_offset);
749 word = (word & ~0xff) | buffer[count];
750 this->write_word(word, bufferram + byte_offset);
753 memcpy(bufferram + offset, buffer, count);
759 * onenand_get_2x_blockpage - [GENERIC] Get blockpage at 2x program mode
760 * @param mtd MTD data structure
761 * @param addr address to check
762 * @return blockpage address
764 * Get blockpage address at 2x program mode
766 static int onenand_get_2x_blockpage(struct mtd_info *mtd, loff_t addr)
768 struct onenand_chip *this = mtd->priv;
769 int blockpage, block, page;
771 /* Calculate the even block number */
772 block = (int) (addr >> this->erase_shift) & ~1;
773 /* Is it the odd plane? */
774 if (addr & this->writesize)
776 page = (int) (addr >> (this->page_shift + 1)) & this->page_mask;
777 blockpage = (block << 7) | page;
783 * onenand_check_bufferram - [GENERIC] Check BufferRAM information
784 * @param mtd MTD data structure
785 * @param addr address to check
786 * @return 1 if there are valid data, otherwise 0
788 * Check bufferram if there is data we required
790 static int onenand_check_bufferram(struct mtd_info *mtd, loff_t addr)
792 struct onenand_chip *this = mtd->priv;
793 int blockpage, found = 0;
796 if (ONENAND_IS_2PLANE(this))
797 blockpage = onenand_get_2x_blockpage(mtd, addr);
799 blockpage = (int) (addr >> this->page_shift);
801 /* Is there valid data? */
802 i = ONENAND_CURRENT_BUFFERRAM(this);
803 if (this->bufferram[i].blockpage == blockpage)
806 /* Check another BufferRAM */
807 i = ONENAND_NEXT_BUFFERRAM(this);
808 if (this->bufferram[i].blockpage == blockpage) {
809 ONENAND_SET_NEXT_BUFFERRAM(this);
814 if (found && ONENAND_IS_DDP(this)) {
815 /* Select DataRAM for DDP */
816 int block = onenand_block(this, addr);
817 int value = onenand_bufferram_address(this, block);
818 this->write_word(value, this->base + ONENAND_REG_START_ADDRESS2);
825 * onenand_update_bufferram - [GENERIC] Update BufferRAM information
826 * @param mtd MTD data structure
827 * @param addr address to update
828 * @param valid valid flag
830 * Update BufferRAM information
832 static void onenand_update_bufferram(struct mtd_info *mtd, loff_t addr,
835 struct onenand_chip *this = mtd->priv;
839 if (ONENAND_IS_2PLANE(this))
840 blockpage = onenand_get_2x_blockpage(mtd, addr);
842 blockpage = (int) (addr >> this->page_shift);
844 /* Invalidate another BufferRAM */
845 i = ONENAND_NEXT_BUFFERRAM(this);
846 if (this->bufferram[i].blockpage == blockpage)
847 this->bufferram[i].blockpage = -1;
849 /* Update BufferRAM */
850 i = ONENAND_CURRENT_BUFFERRAM(this);
852 this->bufferram[i].blockpage = blockpage;
854 this->bufferram[i].blockpage = -1;
858 * onenand_invalidate_bufferram - [GENERIC] Invalidate BufferRAM information
859 * @param mtd MTD data structure
860 * @param addr start address to invalidate
861 * @param len length to invalidate
863 * Invalidate BufferRAM information
865 static void onenand_invalidate_bufferram(struct mtd_info *mtd, loff_t addr,
868 struct onenand_chip *this = mtd->priv;
870 loff_t end_addr = addr + len;
872 /* Invalidate BufferRAM */
873 for (i = 0; i < MAX_BUFFERRAM; i++) {
874 loff_t buf_addr = this->bufferram[i].blockpage << this->page_shift;
875 if (buf_addr >= addr && buf_addr < end_addr)
876 this->bufferram[i].blockpage = -1;
881 * onenand_get_device - [GENERIC] Get chip for selected access
882 * @param mtd MTD device structure
883 * @param new_state the state which is requested
885 * Get the device and lock it for exclusive access
887 static int onenand_get_device(struct mtd_info *mtd, int new_state)
889 struct onenand_chip *this = mtd->priv;
890 DECLARE_WAITQUEUE(wait, current);
893 * Grab the lock and see if the device is available
896 spin_lock(&this->chip_lock);
897 if (this->state == FL_READY) {
898 this->state = new_state;
899 spin_unlock(&this->chip_lock);
902 if (new_state == FL_PM_SUSPENDED) {
903 spin_unlock(&this->chip_lock);
904 return (this->state == FL_PM_SUSPENDED) ? 0 : -EAGAIN;
906 set_current_state(TASK_UNINTERRUPTIBLE);
907 add_wait_queue(&this->wq, &wait);
908 spin_unlock(&this->chip_lock);
910 remove_wait_queue(&this->wq, &wait);
917 * onenand_release_device - [GENERIC] release chip
918 * @param mtd MTD device structure
920 * Deselect, release chip lock and wake up anyone waiting on the device
922 static void onenand_release_device(struct mtd_info *mtd)
924 struct onenand_chip *this = mtd->priv;
926 /* Release the chip */
927 spin_lock(&this->chip_lock);
928 this->state = FL_READY;
930 spin_unlock(&this->chip_lock);
934 * onenand_transfer_auto_oob - [Internal] oob auto-placement transfer
935 * @param mtd MTD device structure
936 * @param buf destination address
937 * @param column oob offset to read from
938 * @param thislen oob length to read
940 static int onenand_transfer_auto_oob(struct mtd_info *mtd, uint8_t *buf, int column,
943 struct onenand_chip *this = mtd->priv;
944 struct nand_oobfree *free;
945 int readcol = column;
946 int readend = column + thislen;
949 uint8_t *oob_buf = this->oob_buf;
951 free = this->ecclayout->oobfree;
952 for (i = 0; i < MTD_MAX_OOBFREE_ENTRIES && free->length; i++, free++) {
953 if (readcol >= lastgap)
954 readcol += free->offset - lastgap;
955 if (readend >= lastgap)
956 readend += free->offset - lastgap;
957 lastgap = free->offset + free->length;
959 this->read_bufferram(mtd, ONENAND_SPARERAM, oob_buf, 0, mtd->oobsize);
960 free = this->ecclayout->oobfree;
961 for (i = 0; i < MTD_MAX_OOBFREE_ENTRIES && free->length; i++, free++) {
962 int free_end = free->offset + free->length;
963 if (free->offset < readend && free_end > readcol) {
964 int st = max_t(int,free->offset,readcol);
965 int ed = min_t(int,free_end,readend);
967 memcpy(buf, oob_buf + st, n);
969 } else if (column == 0)
976 * onenand_recover_lsb - [Flex-OneNAND] Recover LSB page data
977 * @param mtd MTD device structure
978 * @param addr address to recover
979 * @param status return value from onenand_wait / onenand_bbt_wait
981 * MLC NAND Flash cell has paired pages - LSB page and MSB page. LSB page has
982 * lower page address and MSB page has higher page address in paired pages.
983 * If power off occurs during MSB page program, the paired LSB page data can
984 * become corrupt. LSB page recovery read is a way to read LSB page though page
985 * data are corrupted. When uncorrectable error occurs as a result of LSB page
986 * read after power up, issue LSB page recovery read.
988 static int onenand_recover_lsb(struct mtd_info *mtd, loff_t addr, int status)
990 struct onenand_chip *this = mtd->priv;
993 /* Recovery is only for Flex-OneNAND */
994 if (!FLEXONENAND(this))
997 /* check if we failed due to uncorrectable error */
998 if (status != -EBADMSG && status != ONENAND_BBT_READ_ECC_ERROR)
1001 /* check if address lies in MLC region */
1002 i = flexonenand_region(mtd, addr);
1003 if (mtd->eraseregions[i].erasesize < (1 << this->erase_shift))
1006 /* We are attempting to reread, so decrement stats.failed
1007 * which was incremented by onenand_wait due to read failure
1009 printk(KERN_INFO "onenand_recover_lsb: Attempting to recover from uncorrectable read\n");
1010 mtd->ecc_stats.failed--;
1012 /* Issue the LSB page recovery command */
1013 this->command(mtd, FLEXONENAND_CMD_RECOVER_LSB, addr, this->writesize);
1014 return this->wait(mtd, FL_READING);
1018 * onenand_mlc_read_ops_nolock - MLC OneNAND read main and/or out-of-band
1019 * @param mtd MTD device structure
1020 * @param from offset to read from
1021 * @param ops: oob operation description structure
1023 * MLC OneNAND / Flex-OneNAND has 4KB page size and 4KB dataram.
1024 * So, read-while-load is not present.
1026 static int onenand_mlc_read_ops_nolock(struct mtd_info *mtd, loff_t from,
1027 struct mtd_oob_ops *ops)
1029 struct onenand_chip *this = mtd->priv;
1030 struct mtd_ecc_stats stats;
1031 size_t len = ops->len;
1032 size_t ooblen = ops->ooblen;
1033 u_char *buf = ops->datbuf;
1034 u_char *oobbuf = ops->oobbuf;
1035 int read = 0, column, thislen;
1036 int oobread = 0, oobcolumn, thisooblen, oobsize;
1038 int writesize = this->writesize;
1040 DEBUG(MTD_DEBUG_LEVEL3, "onenand_mlc_read_ops_nolock: from = 0x%08x, len = %i\n", (unsigned int) from, (int) len);
1042 if (ops->mode == MTD_OOB_AUTO)
1043 oobsize = this->ecclayout->oobavail;
1045 oobsize = mtd->oobsize;
1047 oobcolumn = from & (mtd->oobsize - 1);
1049 /* Do not allow reads past end of device */
1050 if (from + len > mtd->size) {
1051 printk(KERN_ERR "onenand_mlc_read_ops_nolock: Attempt read beyond end of device\n");
1057 stats = mtd->ecc_stats;
1059 while (read < len) {
1062 thislen = min_t(int, writesize, len - read);
1064 column = from & (writesize - 1);
1065 if (column + thislen > writesize)
1066 thislen = writesize - column;
1068 if (!onenand_check_bufferram(mtd, from)) {
1069 this->command(mtd, ONENAND_CMD_READ, from, writesize);
1071 ret = this->wait(mtd, FL_READING);
1073 ret = onenand_recover_lsb(mtd, from, ret);
1074 onenand_update_bufferram(mtd, from, !ret);
1075 if (ret == -EBADMSG)
1079 this->read_bufferram(mtd, ONENAND_DATARAM, buf, column, thislen);
1081 thisooblen = oobsize - oobcolumn;
1082 thisooblen = min_t(int, thisooblen, ooblen - oobread);
1084 if (ops->mode == MTD_OOB_AUTO)
1085 onenand_transfer_auto_oob(mtd, oobbuf, oobcolumn, thisooblen);
1087 this->read_bufferram(mtd, ONENAND_SPARERAM, oobbuf, oobcolumn, thisooblen);
1088 oobread += thisooblen;
1089 oobbuf += thisooblen;
1102 * Return success, if no ECC failures, else -EBADMSG
1103 * fs driver will take care of that, because
1104 * retlen == desired len and result == -EBADMSG
1107 ops->oobretlen = oobread;
1112 if (mtd->ecc_stats.failed - stats.failed)
1115 return mtd->ecc_stats.corrected - stats.corrected ? -EUCLEAN : 0;
1119 * onenand_read_ops_nolock - [OneNAND Interface] OneNAND read main and/or out-of-band
1120 * @param mtd MTD device structure
1121 * @param from offset to read from
1122 * @param ops: oob operation description structure
1124 * OneNAND read main and/or out-of-band data
1126 static int onenand_read_ops_nolock(struct mtd_info *mtd, loff_t from,
1127 struct mtd_oob_ops *ops)
1129 struct onenand_chip *this = mtd->priv;
1130 struct mtd_ecc_stats stats;
1131 size_t len = ops->len;
1132 size_t ooblen = ops->ooblen;
1133 u_char *buf = ops->datbuf;
1134 u_char *oobbuf = ops->oobbuf;
1135 int read = 0, column, thislen;
1136 int oobread = 0, oobcolumn, thisooblen, oobsize;
1137 int ret = 0, boundary = 0;
1138 int writesize = this->writesize;
1140 DEBUG(MTD_DEBUG_LEVEL3, "onenand_read_ops_nolock: from = 0x%08x, len = %i\n", (unsigned int) from, (int) len);
1142 if (ops->mode == MTD_OOB_AUTO)
1143 oobsize = this->ecclayout->oobavail;
1145 oobsize = mtd->oobsize;
1147 oobcolumn = from & (mtd->oobsize - 1);
1149 /* Do not allow reads past end of device */
1150 if ((from + len) > mtd->size) {
1151 printk(KERN_ERR "onenand_read_ops_nolock: Attempt read beyond end of device\n");
1157 stats = mtd->ecc_stats;
1159 /* Read-while-load method */
1161 /* Do first load to bufferRAM */
1163 if (!onenand_check_bufferram(mtd, from)) {
1164 this->command(mtd, ONENAND_CMD_READ, from, writesize);
1165 ret = this->wait(mtd, FL_READING);
1166 onenand_update_bufferram(mtd, from, !ret);
1167 if (ret == -EBADMSG)
1172 thislen = min_t(int, writesize, len - read);
1173 column = from & (writesize - 1);
1174 if (column + thislen > writesize)
1175 thislen = writesize - column;
1178 /* If there is more to load then start next load */
1180 if (read + thislen < len) {
1181 this->command(mtd, ONENAND_CMD_READ, from, writesize);
1183 * Chip boundary handling in DDP
1184 * Now we issued chip 1 read and pointed chip 1
1185 * bufferam so we have to point chip 0 bufferam.
1187 if (ONENAND_IS_DDP(this) &&
1188 unlikely(from == (this->chipsize >> 1))) {
1189 this->write_word(ONENAND_DDP_CHIP0, this->base + ONENAND_REG_START_ADDRESS2);
1193 ONENAND_SET_PREV_BUFFERRAM(this);
1195 /* While load is going, read from last bufferRAM */
1196 this->read_bufferram(mtd, ONENAND_DATARAM, buf, column, thislen);
1198 /* Read oob area if needed */
1200 thisooblen = oobsize - oobcolumn;
1201 thisooblen = min_t(int, thisooblen, ooblen - oobread);
1203 if (ops->mode == MTD_OOB_AUTO)
1204 onenand_transfer_auto_oob(mtd, oobbuf, oobcolumn, thisooblen);
1206 this->read_bufferram(mtd, ONENAND_SPARERAM, oobbuf, oobcolumn, thisooblen);
1207 oobread += thisooblen;
1208 oobbuf += thisooblen;
1212 /* See if we are done */
1216 /* Set up for next read from bufferRAM */
1217 if (unlikely(boundary))
1218 this->write_word(ONENAND_DDP_CHIP1, this->base + ONENAND_REG_START_ADDRESS2);
1219 ONENAND_SET_NEXT_BUFFERRAM(this);
1221 thislen = min_t(int, writesize, len - read);
1224 /* Now wait for load */
1225 ret = this->wait(mtd, FL_READING);
1226 onenand_update_bufferram(mtd, from, !ret);
1227 if (ret == -EBADMSG)
1232 * Return success, if no ECC failures, else -EBADMSG
1233 * fs driver will take care of that, because
1234 * retlen == desired len and result == -EBADMSG
1237 ops->oobretlen = oobread;
1242 if (mtd->ecc_stats.failed - stats.failed)
1245 return mtd->ecc_stats.corrected - stats.corrected ? -EUCLEAN : 0;
1249 * onenand_read_oob_nolock - [MTD Interface] OneNAND read out-of-band
1250 * @param mtd MTD device structure
1251 * @param from offset to read from
1252 * @param ops: oob operation description structure
1254 * OneNAND read out-of-band data from the spare area
1256 static int onenand_read_oob_nolock(struct mtd_info *mtd, loff_t from,
1257 struct mtd_oob_ops *ops)
1259 struct onenand_chip *this = mtd->priv;
1260 struct mtd_ecc_stats stats;
1261 int read = 0, thislen, column, oobsize;
1262 size_t len = ops->ooblen;
1263 mtd_oob_mode_t mode = ops->mode;
1264 u_char *buf = ops->oobbuf;
1265 int ret = 0, readcmd;
1267 from += ops->ooboffs;
1269 DEBUG(MTD_DEBUG_LEVEL3, "onenand_read_oob_nolock: from = 0x%08x, len = %i\n", (unsigned int) from, (int) len);
1271 /* Initialize return length value */
1274 if (mode == MTD_OOB_AUTO)
1275 oobsize = this->ecclayout->oobavail;
1277 oobsize = mtd->oobsize;
1279 column = from & (mtd->oobsize - 1);
1281 if (unlikely(column >= oobsize)) {
1282 printk(KERN_ERR "onenand_read_oob_nolock: Attempted to start read outside oob\n");
1286 /* Do not allow reads past end of device */
1287 if (unlikely(from >= mtd->size ||
1288 column + len > ((mtd->size >> this->page_shift) -
1289 (from >> this->page_shift)) * oobsize)) {
1290 printk(KERN_ERR "onenand_read_oob_nolock: Attempted to read beyond end of device\n");
1294 stats = mtd->ecc_stats;
1296 readcmd = ONENAND_IS_MLC(this) ? ONENAND_CMD_READ : ONENAND_CMD_READOOB;
1298 while (read < len) {
1301 thislen = oobsize - column;
1302 thislen = min_t(int, thislen, len);
1304 this->command(mtd, readcmd, from, mtd->oobsize);
1306 onenand_update_bufferram(mtd, from, 0);
1308 ret = this->wait(mtd, FL_READING);
1310 ret = onenand_recover_lsb(mtd, from, ret);
1312 if (ret && ret != -EBADMSG) {
1313 printk(KERN_ERR "onenand_read_oob_nolock: read failed = 0x%x\n", ret);
1317 if (mode == MTD_OOB_AUTO)
1318 onenand_transfer_auto_oob(mtd, buf, column, thislen);
1320 this->read_bufferram(mtd, ONENAND_SPARERAM, buf, column, thislen);
1332 from += mtd->writesize;
1337 ops->oobretlen = read;
1342 if (mtd->ecc_stats.failed - stats.failed)
1349 * onenand_read - [MTD Interface] Read data from flash
1350 * @param mtd MTD device structure
1351 * @param from offset to read from
1352 * @param len number of bytes to read
1353 * @param retlen pointer to variable to store the number of read bytes
1354 * @param buf the databuffer to put data
1358 static int onenand_read(struct mtd_info *mtd, loff_t from, size_t len,
1359 size_t *retlen, u_char *buf)
1361 struct onenand_chip *this = mtd->priv;
1362 struct mtd_oob_ops ops = {
1370 onenand_get_device(mtd, FL_READING);
1371 ret = ONENAND_IS_MLC(this) ?
1372 onenand_mlc_read_ops_nolock(mtd, from, &ops) :
1373 onenand_read_ops_nolock(mtd, from, &ops);
1374 onenand_release_device(mtd);
1376 *retlen = ops.retlen;
1381 * onenand_read_oob - [MTD Interface] Read main and/or out-of-band
1382 * @param mtd: MTD device structure
1383 * @param from: offset to read from
1384 * @param ops: oob operation description structure
1386 * Read main and/or out-of-band
1388 static int onenand_read_oob(struct mtd_info *mtd, loff_t from,
1389 struct mtd_oob_ops *ops)
1391 struct onenand_chip *this = mtd->priv;
1394 switch (ops->mode) {
1399 /* Not implemented yet */
1404 onenand_get_device(mtd, FL_READING);
1406 ret = ONENAND_IS_MLC(this) ?
1407 onenand_mlc_read_ops_nolock(mtd, from, ops) :
1408 onenand_read_ops_nolock(mtd, from, ops);
1410 ret = onenand_read_oob_nolock(mtd, from, ops);
1411 onenand_release_device(mtd);
1417 * onenand_bbt_wait - [DEFAULT] wait until the command is done
1418 * @param mtd MTD device structure
1419 * @param state state to select the max. timeout value
1421 * Wait for command done.
1423 static int onenand_bbt_wait(struct mtd_info *mtd, int state)
1425 struct onenand_chip *this = mtd->priv;
1426 unsigned long timeout;
1427 unsigned int interrupt;
1430 /* The 20 msec is enough */
1431 timeout = jiffies + msecs_to_jiffies(20);
1432 while (time_before(jiffies, timeout)) {
1433 interrupt = this->read_word(this->base + ONENAND_REG_INTERRUPT);
1434 if (interrupt & ONENAND_INT_MASTER)
1437 /* To get correct interrupt status in timeout case */
1438 interrupt = this->read_word(this->base + ONENAND_REG_INTERRUPT);
1439 ctrl = this->read_word(this->base + ONENAND_REG_CTRL_STATUS);
1441 if (interrupt & ONENAND_INT_READ) {
1442 int ecc = onenand_read_ecc(this);
1443 if (ecc & ONENAND_ECC_2BIT_ALL) {
1444 printk(KERN_INFO "onenand_bbt_wait: ecc error = 0x%04x"
1445 ", controller error 0x%04x\n", ecc, ctrl);
1446 return ONENAND_BBT_READ_ECC_ERROR;
1449 printk(KERN_ERR "onenand_bbt_wait: read timeout!"
1450 "ctrl=0x%04x intr=0x%04x\n", ctrl, interrupt);
1451 return ONENAND_BBT_READ_FATAL_ERROR;
1454 /* Initial bad block case: 0x2400 or 0x0400 */
1455 if (ctrl & ONENAND_CTRL_ERROR) {
1456 printk(KERN_DEBUG "onenand_bbt_wait: "
1457 "controller error = 0x%04x\n", ctrl);
1458 return ONENAND_BBT_READ_ERROR;
1465 * onenand_bbt_read_oob - [MTD Interface] OneNAND read out-of-band for bbt scan
1466 * @param mtd MTD device structure
1467 * @param from offset to read from
1468 * @param ops oob operation description structure
1470 * OneNAND read out-of-band data from the spare area for bbt scan
1472 int onenand_bbt_read_oob(struct mtd_info *mtd, loff_t from,
1473 struct mtd_oob_ops *ops)
1475 struct onenand_chip *this = mtd->priv;
1476 int read = 0, thislen, column;
1477 int ret = 0, readcmd;
1478 size_t len = ops->ooblen;
1479 u_char *buf = ops->oobbuf;
1481 DEBUG(MTD_DEBUG_LEVEL3, "onenand_bbt_read_oob: from = 0x%08x, len = %zi\n", (unsigned int) from, len);
1483 /* Initialize return value */
1486 /* Do not allow reads past end of device */
1487 if (unlikely((from + len) > mtd->size)) {
1488 printk(KERN_ERR "onenand_bbt_read_oob: Attempt read beyond end of device\n");
1489 return ONENAND_BBT_READ_FATAL_ERROR;
1492 /* Grab the lock and see if the device is available */
1493 onenand_get_device(mtd, FL_READING);
1495 column = from & (mtd->oobsize - 1);
1497 readcmd = ONENAND_IS_MLC(this) ? ONENAND_CMD_READ : ONENAND_CMD_READOOB;
1499 while (read < len) {
1502 thislen = mtd->oobsize - column;
1503 thislen = min_t(int, thislen, len);
1505 this->command(mtd, readcmd, from, mtd->oobsize);
1507 onenand_update_bufferram(mtd, from, 0);
1509 ret = onenand_bbt_wait(mtd, FL_READING);
1511 ret = onenand_recover_lsb(mtd, from, ret);
1516 this->read_bufferram(mtd, ONENAND_SPARERAM, buf, column, thislen);
1525 /* Update Page size */
1526 from += this->writesize;
1531 /* Deselect and wake up anyone waiting on the device */
1532 onenand_release_device(mtd);
1534 ops->oobretlen = read;
1538 #ifdef CONFIG_MTD_ONENAND_VERIFY_WRITE
1540 * onenand_verify_oob - [GENERIC] verify the oob contents after a write
1541 * @param mtd MTD device structure
1542 * @param buf the databuffer to verify
1543 * @param to offset to read from
1545 static int onenand_verify_oob(struct mtd_info *mtd, const u_char *buf, loff_t to)
1547 struct onenand_chip *this = mtd->priv;
1548 u_char *oob_buf = this->oob_buf;
1549 int status, i, readcmd;
1551 readcmd = ONENAND_IS_MLC(this) ? ONENAND_CMD_READ : ONENAND_CMD_READOOB;
1553 this->command(mtd, readcmd, to, mtd->oobsize);
1554 onenand_update_bufferram(mtd, to, 0);
1555 status = this->wait(mtd, FL_READING);
1559 this->read_bufferram(mtd, ONENAND_SPARERAM, oob_buf, 0, mtd->oobsize);
1560 for (i = 0; i < mtd->oobsize; i++)
1561 if (buf[i] != 0xFF && buf[i] != oob_buf[i])
1568 * onenand_verify - [GENERIC] verify the chip contents after a write
1569 * @param mtd MTD device structure
1570 * @param buf the databuffer to verify
1571 * @param addr offset to read from
1572 * @param len number of bytes to read and compare
1574 static int onenand_verify(struct mtd_info *mtd, const u_char *buf, loff_t addr, size_t len)
1576 struct onenand_chip *this = mtd->priv;
1577 void __iomem *dataram;
1579 int thislen, column;
1582 thislen = min_t(int, this->writesize, len);
1583 column = addr & (this->writesize - 1);
1584 if (column + thislen > this->writesize)
1585 thislen = this->writesize - column;
1587 this->command(mtd, ONENAND_CMD_READ, addr, this->writesize);
1589 onenand_update_bufferram(mtd, addr, 0);
1591 ret = this->wait(mtd, FL_READING);
1595 onenand_update_bufferram(mtd, addr, 1);
1597 dataram = this->base + ONENAND_DATARAM;
1598 dataram += onenand_bufferram_offset(mtd, ONENAND_DATARAM);
1600 if (memcmp(buf, dataram + column, thislen))
1611 #define onenand_verify(...) (0)
1612 #define onenand_verify_oob(...) (0)
1615 #define NOTALIGNED(x) ((x & (this->subpagesize - 1)) != 0)
1617 static void onenand_panic_wait(struct mtd_info *mtd)
1619 struct onenand_chip *this = mtd->priv;
1620 unsigned int interrupt;
1623 for (i = 0; i < 2000; i++) {
1624 interrupt = this->read_word(this->base + ONENAND_REG_INTERRUPT);
1625 if (interrupt & ONENAND_INT_MASTER)
1632 * onenand_panic_write - [MTD Interface] write buffer to FLASH in a panic context
1633 * @param mtd MTD device structure
1634 * @param to offset to write to
1635 * @param len number of bytes to write
1636 * @param retlen pointer to variable to store the number of written bytes
1637 * @param buf the data to write
1641 static int onenand_panic_write(struct mtd_info *mtd, loff_t to, size_t len,
1642 size_t *retlen, const u_char *buf)
1644 struct onenand_chip *this = mtd->priv;
1645 int column, subpage;
1649 if (this->state == FL_PM_SUSPENDED)
1652 /* Wait for any existing operation to clear */
1653 onenand_panic_wait(mtd);
1655 DEBUG(MTD_DEBUG_LEVEL3, "onenand_panic_write: to = 0x%08x, len = %i\n",
1656 (unsigned int) to, (int) len);
1658 /* Initialize retlen, in case of early exit */
1661 /* Do not allow writes past end of device */
1662 if (unlikely((to + len) > mtd->size)) {
1663 printk(KERN_ERR "onenand_panic_write: Attempt write to past end of device\n");
1667 /* Reject writes, which are not page aligned */
1668 if (unlikely(NOTALIGNED(to) || NOTALIGNED(len))) {
1669 printk(KERN_ERR "onenand_panic_write: Attempt to write not page aligned data\n");
1673 column = to & (mtd->writesize - 1);
1675 /* Loop until all data write */
1676 while (written < len) {
1677 int thislen = min_t(int, mtd->writesize - column, len - written);
1678 u_char *wbuf = (u_char *) buf;
1680 this->command(mtd, ONENAND_CMD_BUFFERRAM, to, thislen);
1682 /* Partial page write */
1683 subpage = thislen < mtd->writesize;
1685 memset(this->page_buf, 0xff, mtd->writesize);
1686 memcpy(this->page_buf + column, buf, thislen);
1687 wbuf = this->page_buf;
1690 this->write_bufferram(mtd, ONENAND_DATARAM, wbuf, 0, mtd->writesize);
1691 this->write_bufferram(mtd, ONENAND_SPARERAM, ffchars, 0, mtd->oobsize);
1693 this->command(mtd, ONENAND_CMD_PROG, to, mtd->writesize);
1695 onenand_panic_wait(mtd);
1697 /* In partial page write we don't update bufferram */
1698 onenand_update_bufferram(mtd, to, !ret && !subpage);
1699 if (ONENAND_IS_2PLANE(this)) {
1700 ONENAND_SET_BUFFERRAM1(this);
1701 onenand_update_bufferram(mtd, to + this->writesize, !ret && !subpage);
1705 printk(KERN_ERR "onenand_panic_write: write failed %d\n", ret);
1724 * onenand_fill_auto_oob - [Internal] oob auto-placement transfer
1725 * @param mtd MTD device structure
1726 * @param oob_buf oob buffer
1727 * @param buf source address
1728 * @param column oob offset to write to
1729 * @param thislen oob length to write
1731 static int onenand_fill_auto_oob(struct mtd_info *mtd, u_char *oob_buf,
1732 const u_char *buf, int column, int thislen)
1734 struct onenand_chip *this = mtd->priv;
1735 struct nand_oobfree *free;
1736 int writecol = column;
1737 int writeend = column + thislen;
1741 free = this->ecclayout->oobfree;
1742 for (i = 0; i < MTD_MAX_OOBFREE_ENTRIES && free->length; i++, free++) {
1743 if (writecol >= lastgap)
1744 writecol += free->offset - lastgap;
1745 if (writeend >= lastgap)
1746 writeend += free->offset - lastgap;
1747 lastgap = free->offset + free->length;
1749 free = this->ecclayout->oobfree;
1750 for (i = 0; i < MTD_MAX_OOBFREE_ENTRIES && free->length; i++, free++) {
1751 int free_end = free->offset + free->length;
1752 if (free->offset < writeend && free_end > writecol) {
1753 int st = max_t(int,free->offset,writecol);
1754 int ed = min_t(int,free_end,writeend);
1756 memcpy(oob_buf + st, buf, n);
1758 } else if (column == 0)
1765 * onenand_write_ops_nolock - [OneNAND Interface] write main and/or out-of-band
1766 * @param mtd MTD device structure
1767 * @param to offset to write to
1768 * @param ops oob operation description structure
1770 * Write main and/or oob with ECC
1772 static int onenand_write_ops_nolock(struct mtd_info *mtd, loff_t to,
1773 struct mtd_oob_ops *ops)
1775 struct onenand_chip *this = mtd->priv;
1776 int written = 0, column, thislen = 0, subpage = 0;
1777 int prev = 0, prevlen = 0, prev_subpage = 0, first = 1;
1778 int oobwritten = 0, oobcolumn, thisooblen, oobsize;
1779 size_t len = ops->len;
1780 size_t ooblen = ops->ooblen;
1781 const u_char *buf = ops->datbuf;
1782 const u_char *oob = ops->oobbuf;
1786 DEBUG(MTD_DEBUG_LEVEL3, "onenand_write_ops_nolock: to = 0x%08x, len = %i\n", (unsigned int) to, (int) len);
1788 /* Initialize retlen, in case of early exit */
1792 /* Do not allow writes past end of device */
1793 if (unlikely((to + len) > mtd->size)) {
1794 printk(KERN_ERR "onenand_write_ops_nolock: Attempt write to past end of device\n");
1798 /* Reject writes, which are not page aligned */
1799 if (unlikely(NOTALIGNED(to) || NOTALIGNED(len))) {
1800 printk(KERN_ERR "onenand_write_ops_nolock: Attempt to write not page aligned data\n");
1804 /* Check zero length */
1808 if (ops->mode == MTD_OOB_AUTO)
1809 oobsize = this->ecclayout->oobavail;
1811 oobsize = mtd->oobsize;
1813 oobcolumn = to & (mtd->oobsize - 1);
1815 column = to & (mtd->writesize - 1);
1817 /* Loop until all data write */
1819 if (written < len) {
1820 u_char *wbuf = (u_char *) buf;
1822 thislen = min_t(int, mtd->writesize - column, len - written);
1823 thisooblen = min_t(int, oobsize - oobcolumn, ooblen - oobwritten);
1827 this->command(mtd, ONENAND_CMD_BUFFERRAM, to, thislen);
1829 /* Partial page write */
1830 subpage = thislen < mtd->writesize;
1832 memset(this->page_buf, 0xff, mtd->writesize);
1833 memcpy(this->page_buf + column, buf, thislen);
1834 wbuf = this->page_buf;
1837 this->write_bufferram(mtd, ONENAND_DATARAM, wbuf, 0, mtd->writesize);
1840 oobbuf = this->oob_buf;
1842 /* We send data to spare ram with oobsize
1843 * to prevent byte access */
1844 memset(oobbuf, 0xff, mtd->oobsize);
1845 if (ops->mode == MTD_OOB_AUTO)
1846 onenand_fill_auto_oob(mtd, oobbuf, oob, oobcolumn, thisooblen);
1848 memcpy(oobbuf + oobcolumn, oob, thisooblen);
1850 oobwritten += thisooblen;
1854 oobbuf = (u_char *) ffchars;
1856 this->write_bufferram(mtd, ONENAND_SPARERAM, oobbuf, 0, mtd->oobsize);
1858 ONENAND_SET_NEXT_BUFFERRAM(this);
1861 * 2 PLANE, MLC, and Flex-OneNAND doesn't support
1862 * write-while-programe feature.
1864 if (!ONENAND_IS_2PLANE(this) && !first) {
1865 ONENAND_SET_PREV_BUFFERRAM(this);
1867 ret = this->wait(mtd, FL_WRITING);
1869 /* In partial page write we don't update bufferram */
1870 onenand_update_bufferram(mtd, prev, !ret && !prev_subpage);
1873 printk(KERN_ERR "onenand_write_ops_nolock: write filaed %d\n", ret);
1877 if (written == len) {
1878 /* Only check verify write turn on */
1879 ret = onenand_verify(mtd, buf - len, to - len, len);
1881 printk(KERN_ERR "onenand_write_ops_nolock: verify failed %d\n", ret);
1885 ONENAND_SET_NEXT_BUFFERRAM(this);
1888 this->command(mtd, ONENAND_CMD_PROG, to, mtd->writesize);
1891 * 2 PLANE, MLC, and Flex-OneNAND wait here
1893 if (ONENAND_IS_2PLANE(this)) {
1894 ret = this->wait(mtd, FL_WRITING);
1896 /* In partial page write we don't update bufferram */
1897 onenand_update_bufferram(mtd, to, !ret && !subpage);
1899 printk(KERN_ERR "onenand_write_ops_nolock: write filaed %d\n", ret);
1903 /* Only check verify write turn on */
1904 ret = onenand_verify(mtd, buf, to, thislen);
1906 printk(KERN_ERR "onenand_write_ops_nolock: verify failed %d\n", ret);
1919 prev_subpage = subpage;
1927 /* In error case, clear all bufferrams */
1929 onenand_invalidate_bufferram(mtd, 0, -1);
1931 ops->retlen = written;
1932 ops->oobretlen = oobwritten;
1939 * onenand_write_oob_nolock - [Internal] OneNAND write out-of-band
1940 * @param mtd MTD device structure
1941 * @param to offset to write to
1942 * @param len number of bytes to write
1943 * @param retlen pointer to variable to store the number of written bytes
1944 * @param buf the data to write
1945 * @param mode operation mode
1947 * OneNAND write out-of-band
1949 static int onenand_write_oob_nolock(struct mtd_info *mtd, loff_t to,
1950 struct mtd_oob_ops *ops)
1952 struct onenand_chip *this = mtd->priv;
1953 int column, ret = 0, oobsize;
1954 int written = 0, oobcmd;
1956 size_t len = ops->ooblen;
1957 const u_char *buf = ops->oobbuf;
1958 mtd_oob_mode_t mode = ops->mode;
1962 DEBUG(MTD_DEBUG_LEVEL3, "onenand_write_oob_nolock: to = 0x%08x, len = %i\n", (unsigned int) to, (int) len);
1964 /* Initialize retlen, in case of early exit */
1967 if (mode == MTD_OOB_AUTO)
1968 oobsize = this->ecclayout->oobavail;
1970 oobsize = mtd->oobsize;
1972 column = to & (mtd->oobsize - 1);
1974 if (unlikely(column >= oobsize)) {
1975 printk(KERN_ERR "onenand_write_oob_nolock: Attempted to start write outside oob\n");
1979 /* For compatibility with NAND: Do not allow write past end of page */
1980 if (unlikely(column + len > oobsize)) {
1981 printk(KERN_ERR "onenand_write_oob_nolock: "
1982 "Attempt to write past end of page\n");
1986 /* Do not allow reads past end of device */
1987 if (unlikely(to >= mtd->size ||
1988 column + len > ((mtd->size >> this->page_shift) -
1989 (to >> this->page_shift)) * oobsize)) {
1990 printk(KERN_ERR "onenand_write_oob_nolock: Attempted to write past end of device\n");
1994 oobbuf = this->oob_buf;
1996 oobcmd = ONENAND_IS_MLC(this) ? ONENAND_CMD_PROG : ONENAND_CMD_PROGOOB;
1998 /* Loop until all data write */
1999 while (written < len) {
2000 int thislen = min_t(int, oobsize, len - written);
2004 this->command(mtd, ONENAND_CMD_BUFFERRAM, to, mtd->oobsize);
2006 /* We send data to spare ram with oobsize
2007 * to prevent byte access */
2008 memset(oobbuf, 0xff, mtd->oobsize);
2009 if (mode == MTD_OOB_AUTO)
2010 onenand_fill_auto_oob(mtd, oobbuf, buf, column, thislen);
2012 memcpy(oobbuf + column, buf, thislen);
2013 this->write_bufferram(mtd, ONENAND_SPARERAM, oobbuf, 0, mtd->oobsize);
2015 if (ONENAND_IS_MLC(this)) {
2016 /* Set main area of DataRAM to 0xff*/
2017 memset(this->page_buf, 0xff, mtd->writesize);
2018 this->write_bufferram(mtd, ONENAND_DATARAM,
2019 this->page_buf, 0, mtd->writesize);
2022 this->command(mtd, oobcmd, to, mtd->oobsize);
2024 onenand_update_bufferram(mtd, to, 0);
2025 if (ONENAND_IS_2PLANE(this)) {
2026 ONENAND_SET_BUFFERRAM1(this);
2027 onenand_update_bufferram(mtd, to + this->writesize, 0);
2030 ret = this->wait(mtd, FL_WRITING);
2032 printk(KERN_ERR "onenand_write_oob_nolock: write failed %d\n", ret);
2036 ret = onenand_verify_oob(mtd, oobbuf, to);
2038 printk(KERN_ERR "onenand_write_oob_nolock: verify failed %d\n", ret);
2046 to += mtd->writesize;
2051 ops->oobretlen = written;
2057 * onenand_write - [MTD Interface] write buffer to FLASH
2058 * @param mtd MTD device structure
2059 * @param to offset to write to
2060 * @param len number of bytes to write
2061 * @param retlen pointer to variable to store the number of written bytes
2062 * @param buf the data to write
2066 static int onenand_write(struct mtd_info *mtd, loff_t to, size_t len,
2067 size_t *retlen, const u_char *buf)
2069 struct mtd_oob_ops ops = {
2072 .datbuf = (u_char *) buf,
2077 onenand_get_device(mtd, FL_WRITING);
2078 ret = onenand_write_ops_nolock(mtd, to, &ops);
2079 onenand_release_device(mtd);
2081 *retlen = ops.retlen;
2086 * onenand_write_oob - [MTD Interface] NAND write data and/or out-of-band
2087 * @param mtd: MTD device structure
2088 * @param to: offset to write
2089 * @param ops: oob operation description structure
2091 static int onenand_write_oob(struct mtd_info *mtd, loff_t to,
2092 struct mtd_oob_ops *ops)
2096 switch (ops->mode) {
2101 /* Not implemented yet */
2106 onenand_get_device(mtd, FL_WRITING);
2108 ret = onenand_write_ops_nolock(mtd, to, ops);
2110 ret = onenand_write_oob_nolock(mtd, to, ops);
2111 onenand_release_device(mtd);
2117 * onenand_block_isbad_nolock - [GENERIC] Check if a block is marked bad
2118 * @param mtd MTD device structure
2119 * @param ofs offset from device start
2120 * @param allowbbt 1, if its allowed to access the bbt area
2122 * Check, if the block is bad. Either by reading the bad block table or
2123 * calling of the scan function.
2125 static int onenand_block_isbad_nolock(struct mtd_info *mtd, loff_t ofs, int allowbbt)
2127 struct onenand_chip *this = mtd->priv;
2128 struct bbm_info *bbm = this->bbm;
2130 /* Return info from the table */
2131 return bbm->isbad_bbt(mtd, ofs, allowbbt);
2135 * onenand_erase - [MTD Interface] erase block(s)
2136 * @param mtd MTD device structure
2137 * @param instr erase instruction
2139 * Erase one ore more blocks
2141 static int onenand_erase(struct mtd_info *mtd, struct erase_info *instr)
2143 struct onenand_chip *this = mtd->priv;
2144 unsigned int block_size;
2145 loff_t addr = instr->addr;
2146 loff_t len = instr->len;
2148 struct mtd_erase_region_info *region = NULL;
2149 loff_t region_end = 0;
2151 DEBUG(MTD_DEBUG_LEVEL3, "onenand_erase: start = 0x%012llx, len = %llu\n", (unsigned long long) instr->addr, (unsigned long long) instr->len);
2153 /* Do not allow erase past end of device */
2154 if (unlikely((len + addr) > mtd->size)) {
2155 printk(KERN_ERR "onenand_erase: Erase past end of device\n");
2159 if (FLEXONENAND(this)) {
2160 /* Find the eraseregion of this address */
2161 i = flexonenand_region(mtd, addr);
2162 region = &mtd->eraseregions[i];
2164 block_size = region->erasesize;
2165 region_end = region->offset + region->erasesize * region->numblocks;
2167 /* Start address within region must align on block boundary.
2168 * Erase region's start offset is always block start address.
2170 if (unlikely((addr - region->offset) & (block_size - 1))) {
2171 printk(KERN_ERR "onenand_erase: Unaligned address\n");
2175 block_size = 1 << this->erase_shift;
2177 /* Start address must align on block boundary */
2178 if (unlikely(addr & (block_size - 1))) {
2179 printk(KERN_ERR "onenand_erase: Unaligned address\n");
2184 /* Length must align on block boundary */
2185 if (unlikely(len & (block_size - 1))) {
2186 printk(KERN_ERR "onenand_erase: Length not block aligned\n");
2190 instr->fail_addr = MTD_FAIL_ADDR_UNKNOWN;
2192 /* Grab the lock and see if the device is available */
2193 onenand_get_device(mtd, FL_ERASING);
2195 /* Loop throught the pages */
2196 instr->state = MTD_ERASING;
2201 /* Check if we have a bad block, we do not erase bad blocks */
2202 if (onenand_block_isbad_nolock(mtd, addr, 0)) {
2203 printk (KERN_WARNING "onenand_erase: attempt to erase a bad block at addr 0x%012llx\n", (unsigned long long) addr);
2204 instr->state = MTD_ERASE_FAILED;
2208 this->command(mtd, ONENAND_CMD_ERASE, addr, block_size);
2210 onenand_invalidate_bufferram(mtd, addr, block_size);
2212 ret = this->wait(mtd, FL_ERASING);
2213 /* Check, if it is write protected */
2215 printk(KERN_ERR "onenand_erase: Failed erase, block %d\n",
2216 onenand_block(this, addr));
2217 instr->state = MTD_ERASE_FAILED;
2218 instr->fail_addr = addr;
2225 if (addr == region_end) {
2230 block_size = region->erasesize;
2231 region_end = region->offset + region->erasesize * region->numblocks;
2233 if (len & (block_size - 1)) {
2234 /* FIXME: This should be handled at MTD partitioning level. */
2235 printk(KERN_ERR "onenand_erase: Unaligned address\n");
2242 instr->state = MTD_ERASE_DONE;
2246 ret = instr->state == MTD_ERASE_DONE ? 0 : -EIO;
2248 /* Deselect and wake up anyone waiting on the device */
2249 onenand_release_device(mtd);
2251 /* Do call back function */
2253 mtd_erase_callback(instr);
2259 * onenand_sync - [MTD Interface] sync
2260 * @param mtd MTD device structure
2262 * Sync is actually a wait for chip ready function
2264 static void onenand_sync(struct mtd_info *mtd)
2266 DEBUG(MTD_DEBUG_LEVEL3, "onenand_sync: called\n");
2268 /* Grab the lock and see if the device is available */
2269 onenand_get_device(mtd, FL_SYNCING);
2271 /* Release it and go back */
2272 onenand_release_device(mtd);
2276 * onenand_block_isbad - [MTD Interface] Check whether the block at the given offset is bad
2277 * @param mtd MTD device structure
2278 * @param ofs offset relative to mtd start
2280 * Check whether the block is bad
2282 static int onenand_block_isbad(struct mtd_info *mtd, loff_t ofs)
2286 /* Check for invalid offset */
2287 if (ofs > mtd->size)
2290 onenand_get_device(mtd, FL_READING);
2291 ret = onenand_block_isbad_nolock(mtd, ofs, 0);
2292 onenand_release_device(mtd);
2297 * onenand_default_block_markbad - [DEFAULT] mark a block bad
2298 * @param mtd MTD device structure
2299 * @param ofs offset from device start
2301 * This is the default implementation, which can be overridden by
2302 * a hardware specific driver.
2304 static int onenand_default_block_markbad(struct mtd_info *mtd, loff_t ofs)
2306 struct onenand_chip *this = mtd->priv;
2307 struct bbm_info *bbm = this->bbm;
2308 u_char buf[2] = {0, 0};
2309 struct mtd_oob_ops ops = {
2310 .mode = MTD_OOB_PLACE,
2317 /* Get block number */
2318 block = onenand_block(this, ofs);
2320 bbm->bbt[block >> 2] |= 0x01 << ((block & 0x03) << 1);
2322 /* We write two bytes, so we dont have to mess with 16 bit access */
2323 ofs += mtd->oobsize + (bbm->badblockpos & ~0x01);
2324 /* FIXME : What to do when marking SLC block in partition
2325 * with MLC erasesize? For now, it is not advisable to
2326 * create partitions containing both SLC and MLC regions.
2328 return onenand_write_oob_nolock(mtd, ofs, &ops);
2332 * onenand_block_markbad - [MTD Interface] Mark the block at the given offset as bad
2333 * @param mtd MTD device structure
2334 * @param ofs offset relative to mtd start
2336 * Mark the block as bad
2338 static int onenand_block_markbad(struct mtd_info *mtd, loff_t ofs)
2340 struct onenand_chip *this = mtd->priv;
2343 ret = onenand_block_isbad(mtd, ofs);
2345 /* If it was bad already, return success and do nothing */
2351 onenand_get_device(mtd, FL_WRITING);
2352 ret = this->block_markbad(mtd, ofs);
2353 onenand_release_device(mtd);
2358 * onenand_do_lock_cmd - [OneNAND Interface] Lock or unlock block(s)
2359 * @param mtd MTD device structure
2360 * @param ofs offset relative to mtd start
2361 * @param len number of bytes to lock or unlock
2362 * @param cmd lock or unlock command
2364 * Lock or unlock one or more blocks
2366 static int onenand_do_lock_cmd(struct mtd_info *mtd, loff_t ofs, size_t len, int cmd)
2368 struct onenand_chip *this = mtd->priv;
2369 int start, end, block, value, status;
2372 start = onenand_block(this, ofs);
2373 end = onenand_block(this, ofs + len) - 1;
2375 if (cmd == ONENAND_CMD_LOCK)
2376 wp_status_mask = ONENAND_WP_LS;
2378 wp_status_mask = ONENAND_WP_US;
2380 /* Continuous lock scheme */
2381 if (this->options & ONENAND_HAS_CONT_LOCK) {
2382 /* Set start block address */
2383 this->write_word(start, this->base + ONENAND_REG_START_BLOCK_ADDRESS);
2384 /* Set end block address */
2385 this->write_word(end, this->base + ONENAND_REG_END_BLOCK_ADDRESS);
2386 /* Write lock command */
2387 this->command(mtd, cmd, 0, 0);
2389 /* There's no return value */
2390 this->wait(mtd, FL_LOCKING);
2393 while (this->read_word(this->base + ONENAND_REG_CTRL_STATUS)
2394 & ONENAND_CTRL_ONGO)
2397 /* Check lock status */
2398 status = this->read_word(this->base + ONENAND_REG_WP_STATUS);
2399 if (!(status & wp_status_mask))
2400 printk(KERN_ERR "wp status = 0x%x\n", status);
2405 /* Block lock scheme */
2406 for (block = start; block < end + 1; block++) {
2407 /* Set block address */
2408 value = onenand_block_address(this, block);
2409 this->write_word(value, this->base + ONENAND_REG_START_ADDRESS1);
2410 /* Select DataRAM for DDP */
2411 value = onenand_bufferram_address(this, block);
2412 this->write_word(value, this->base + ONENAND_REG_START_ADDRESS2);
2413 /* Set start block address */
2414 this->write_word(block, this->base + ONENAND_REG_START_BLOCK_ADDRESS);
2415 /* Write lock command */
2416 this->command(mtd, cmd, 0, 0);
2418 /* There's no return value */
2419 this->wait(mtd, FL_LOCKING);
2422 while (this->read_word(this->base + ONENAND_REG_CTRL_STATUS)
2423 & ONENAND_CTRL_ONGO)
2426 /* Check lock status */
2427 status = this->read_word(this->base + ONENAND_REG_WP_STATUS);
2428 if (!(status & wp_status_mask))
2429 printk(KERN_ERR "block = %d, wp status = 0x%x\n", block, status);
2436 * onenand_lock - [MTD Interface] Lock block(s)
2437 * @param mtd MTD device structure
2438 * @param ofs offset relative to mtd start
2439 * @param len number of bytes to unlock
2441 * Lock one or more blocks
2443 static int onenand_lock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
2447 onenand_get_device(mtd, FL_LOCKING);
2448 ret = onenand_do_lock_cmd(mtd, ofs, len, ONENAND_CMD_LOCK);
2449 onenand_release_device(mtd);
2454 * onenand_unlock - [MTD Interface] Unlock block(s)
2455 * @param mtd MTD device structure
2456 * @param ofs offset relative to mtd start
2457 * @param len number of bytes to unlock
2459 * Unlock one or more blocks
2461 static int onenand_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
2465 onenand_get_device(mtd, FL_LOCKING);
2466 ret = onenand_do_lock_cmd(mtd, ofs, len, ONENAND_CMD_UNLOCK);
2467 onenand_release_device(mtd);
2472 * onenand_check_lock_status - [OneNAND Interface] Check lock status
2473 * @param this onenand chip data structure
2477 static int onenand_check_lock_status(struct onenand_chip *this)
2479 unsigned int value, block, status;
2482 end = this->chipsize >> this->erase_shift;
2483 for (block = 0; block < end; block++) {
2484 /* Set block address */
2485 value = onenand_block_address(this, block);
2486 this->write_word(value, this->base + ONENAND_REG_START_ADDRESS1);
2487 /* Select DataRAM for DDP */
2488 value = onenand_bufferram_address(this, block);
2489 this->write_word(value, this->base + ONENAND_REG_START_ADDRESS2);
2490 /* Set start block address */
2491 this->write_word(block, this->base + ONENAND_REG_START_BLOCK_ADDRESS);
2493 /* Check lock status */
2494 status = this->read_word(this->base + ONENAND_REG_WP_STATUS);
2495 if (!(status & ONENAND_WP_US)) {
2496 printk(KERN_ERR "block = %d, wp status = 0x%x\n", block, status);
2505 * onenand_unlock_all - [OneNAND Interface] unlock all blocks
2506 * @param mtd MTD device structure
2510 static void onenand_unlock_all(struct mtd_info *mtd)
2512 struct onenand_chip *this = mtd->priv;
2514 loff_t len = mtd->size;
2516 if (this->options & ONENAND_HAS_UNLOCK_ALL) {
2517 /* Set start block address */
2518 this->write_word(0, this->base + ONENAND_REG_START_BLOCK_ADDRESS);
2519 /* Write unlock command */
2520 this->command(mtd, ONENAND_CMD_UNLOCK_ALL, 0, 0);
2522 /* There's no return value */
2523 this->wait(mtd, FL_LOCKING);
2526 while (this->read_word(this->base + ONENAND_REG_CTRL_STATUS)
2527 & ONENAND_CTRL_ONGO)
2530 /* Check lock status */
2531 if (onenand_check_lock_status(this))
2534 /* Workaround for all block unlock in DDP */
2535 if (ONENAND_IS_DDP(this) && !FLEXONENAND(this)) {
2536 /* All blocks on another chip */
2537 ofs = this->chipsize >> 1;
2538 len = this->chipsize >> 1;
2542 onenand_do_lock_cmd(mtd, ofs, len, ONENAND_CMD_UNLOCK);
2545 #ifdef CONFIG_MTD_ONENAND_OTP
2547 /* Interal OTP operation */
2548 typedef int (*otp_op_t)(struct mtd_info *mtd, loff_t form, size_t len,
2549 size_t *retlen, u_char *buf);
2552 * do_otp_read - [DEFAULT] Read OTP block area
2553 * @param mtd MTD device structure
2554 * @param from The offset to read
2555 * @param len number of bytes to read
2556 * @param retlen pointer to variable to store the number of readbytes
2557 * @param buf the databuffer to put/get data
2559 * Read OTP block area.
2561 static int do_otp_read(struct mtd_info *mtd, loff_t from, size_t len,
2562 size_t *retlen, u_char *buf)
2564 struct onenand_chip *this = mtd->priv;
2565 struct mtd_oob_ops ops = {
2573 /* Enter OTP access mode */
2574 this->command(mtd, ONENAND_CMD_OTP_ACCESS, 0, 0);
2575 this->wait(mtd, FL_OTPING);
2577 ret = ONENAND_IS_MLC(this) ?
2578 onenand_mlc_read_ops_nolock(mtd, from, &ops) :
2579 onenand_read_ops_nolock(mtd, from, &ops);
2581 /* Exit OTP access mode */
2582 this->command(mtd, ONENAND_CMD_RESET, 0, 0);
2583 this->wait(mtd, FL_RESETING);
2589 * do_otp_write - [DEFAULT] Write OTP block area
2590 * @param mtd MTD device structure
2591 * @param to The offset to write
2592 * @param len number of bytes to write
2593 * @param retlen pointer to variable to store the number of write bytes
2594 * @param buf the databuffer to put/get data
2596 * Write OTP block area.
2598 static int do_otp_write(struct mtd_info *mtd, loff_t to, size_t len,
2599 size_t *retlen, u_char *buf)
2601 struct onenand_chip *this = mtd->priv;
2602 unsigned char *pbuf = buf;
2604 struct mtd_oob_ops ops;
2606 /* Force buffer page aligned */
2607 if (len < mtd->writesize) {
2608 memcpy(this->page_buf, buf, len);
2609 memset(this->page_buf + len, 0xff, mtd->writesize - len);
2610 pbuf = this->page_buf;
2611 len = mtd->writesize;
2614 /* Enter OTP access mode */
2615 this->command(mtd, ONENAND_CMD_OTP_ACCESS, 0, 0);
2616 this->wait(mtd, FL_OTPING);
2622 ret = onenand_write_ops_nolock(mtd, to, &ops);
2623 *retlen = ops.retlen;
2625 /* Exit OTP access mode */
2626 this->command(mtd, ONENAND_CMD_RESET, 0, 0);
2627 this->wait(mtd, FL_RESETING);
2633 * do_otp_lock - [DEFAULT] Lock OTP block area
2634 * @param mtd MTD device structure
2635 * @param from The offset to lock
2636 * @param len number of bytes to lock
2637 * @param retlen pointer to variable to store the number of lock bytes
2638 * @param buf the databuffer to put/get data
2640 * Lock OTP block area.
2642 static int do_otp_lock(struct mtd_info *mtd, loff_t from, size_t len,
2643 size_t *retlen, u_char *buf)
2645 struct onenand_chip *this = mtd->priv;
2646 struct mtd_oob_ops ops;
2649 /* Enter OTP access mode */
2650 this->command(mtd, ONENAND_CMD_OTP_ACCESS, 0, 0);
2651 this->wait(mtd, FL_OTPING);
2653 if (FLEXONENAND(this)) {
2655 * For Flex-OneNAND, we write lock mark to 1st word of sector 4 of
2656 * main area of page 49.
2658 ops.len = mtd->writesize;
2662 ret = onenand_write_ops_nolock(mtd, mtd->writesize * 49, &ops);
2663 *retlen = ops.retlen;
2665 ops.mode = MTD_OOB_PLACE;
2669 ret = onenand_write_oob_nolock(mtd, from, &ops);
2670 *retlen = ops.oobretlen;
2673 /* Exit OTP access mode */
2674 this->command(mtd, ONENAND_CMD_RESET, 0, 0);
2675 this->wait(mtd, FL_RESETING);
2681 * onenand_otp_walk - [DEFAULT] Handle OTP operation
2682 * @param mtd MTD device structure
2683 * @param from The offset to read/write
2684 * @param len number of bytes to read/write
2685 * @param retlen pointer to variable to store the number of read bytes
2686 * @param buf the databuffer to put/get data
2687 * @param action do given action
2688 * @param mode specify user and factory
2690 * Handle OTP operation.
2692 static int onenand_otp_walk(struct mtd_info *mtd, loff_t from, size_t len,
2693 size_t *retlen, u_char *buf,
2694 otp_op_t action, int mode)
2696 struct onenand_chip *this = mtd->priv;
2703 density = onenand_get_density(this->device_id);
2704 if (density < ONENAND_DEVICE_DENSITY_512Mb)
2709 if (mode == MTD_OTP_FACTORY) {
2710 from += mtd->writesize * otp_pages;
2711 otp_pages = 64 - otp_pages;
2714 /* Check User/Factory boundary */
2715 if (((mtd->writesize * otp_pages) - (from + len)) < 0)
2718 onenand_get_device(mtd, FL_OTPING);
2719 while (len > 0 && otp_pages > 0) {
2720 if (!action) { /* OTP Info functions */
2721 struct otp_info *otpinfo;
2723 len -= sizeof(struct otp_info);
2729 otpinfo = (struct otp_info *) buf;
2730 otpinfo->start = from;
2731 otpinfo->length = mtd->writesize;
2732 otpinfo->locked = 0;
2734 from += mtd->writesize;
2735 buf += sizeof(struct otp_info);
2736 *retlen += sizeof(struct otp_info);
2741 ret = action(mtd, from, len, &tmp_retlen, buf);
2752 onenand_release_device(mtd);
2758 * onenand_get_fact_prot_info - [MTD Interface] Read factory OTP info
2759 * @param mtd MTD device structure
2760 * @param buf the databuffer to put/get data
2761 * @param len number of bytes to read
2763 * Read factory OTP info.
2765 static int onenand_get_fact_prot_info(struct mtd_info *mtd,
2766 struct otp_info *buf, size_t len)
2771 ret = onenand_otp_walk(mtd, 0, len, &retlen, (u_char *) buf, NULL, MTD_OTP_FACTORY);
2773 return ret ? : retlen;
2777 * onenand_read_fact_prot_reg - [MTD Interface] Read factory OTP area
2778 * @param mtd MTD device structure
2779 * @param from The offset to read
2780 * @param len number of bytes to read
2781 * @param retlen pointer to variable to store the number of read bytes
2782 * @param buf the databuffer to put/get data
2784 * Read factory OTP area.
2786 static int onenand_read_fact_prot_reg(struct mtd_info *mtd, loff_t from,
2787 size_t len, size_t *retlen, u_char *buf)
2789 return onenand_otp_walk(mtd, from, len, retlen, buf, do_otp_read, MTD_OTP_FACTORY);
2793 * onenand_get_user_prot_info - [MTD Interface] Read user OTP info
2794 * @param mtd MTD device structure
2795 * @param buf the databuffer to put/get data
2796 * @param len number of bytes to read
2798 * Read user OTP info.
2800 static int onenand_get_user_prot_info(struct mtd_info *mtd,
2801 struct otp_info *buf, size_t len)
2806 ret = onenand_otp_walk(mtd, 0, len, &retlen, (u_char *) buf, NULL, MTD_OTP_USER);
2808 return ret ? : retlen;
2812 * onenand_read_user_prot_reg - [MTD Interface] Read user OTP area
2813 * @param mtd MTD device structure
2814 * @param from The offset to read
2815 * @param len number of bytes to read
2816 * @param retlen pointer to variable to store the number of read bytes
2817 * @param buf the databuffer to put/get data
2819 * Read user OTP area.
2821 static int onenand_read_user_prot_reg(struct mtd_info *mtd, loff_t from,
2822 size_t len, size_t *retlen, u_char *buf)
2824 return onenand_otp_walk(mtd, from, len, retlen, buf, do_otp_read, MTD_OTP_USER);
2828 * onenand_write_user_prot_reg - [MTD Interface] Write user OTP area
2829 * @param mtd MTD device structure
2830 * @param from The offset to write
2831 * @param len number of bytes to write
2832 * @param retlen pointer to variable to store the number of write bytes
2833 * @param buf the databuffer to put/get data
2835 * Write user OTP area.
2837 static int onenand_write_user_prot_reg(struct mtd_info *mtd, loff_t from,
2838 size_t len, size_t *retlen, u_char *buf)
2840 return onenand_otp_walk(mtd, from, len, retlen, buf, do_otp_write, MTD_OTP_USER);
2844 * onenand_lock_user_prot_reg - [MTD Interface] Lock user OTP area
2845 * @param mtd MTD device structure
2846 * @param from The offset to lock
2847 * @param len number of bytes to unlock
2849 * Write lock mark on spare area in page 0 in OTP block
2851 static int onenand_lock_user_prot_reg(struct mtd_info *mtd, loff_t from,
2854 struct onenand_chip *this = mtd->priv;
2855 u_char *buf = FLEXONENAND(this) ? this->page_buf : this->oob_buf;
2859 memset(buf, 0xff, FLEXONENAND(this) ? this->writesize
2862 * Note: OTP lock operation
2863 * OTP block : 0xXXFC
2864 * 1st block : 0xXXF3 (If chip support)
2865 * Both : 0xXXF0 (If chip support)
2867 if (FLEXONENAND(this))
2868 buf[FLEXONENAND_OTP_LOCK_OFFSET] = 0xFC;
2870 buf[ONENAND_OTP_LOCK_OFFSET] = 0xFC;
2873 * Write lock mark to 8th word of sector0 of page0 of the spare0.
2874 * We write 16 bytes spare area instead of 2 bytes.
2875 * For Flex-OneNAND, we write lock mark to 1st word of sector 4 of
2876 * main area of page 49.
2880 len = FLEXONENAND(this) ? mtd->writesize : 16;
2882 ret = onenand_otp_walk(mtd, from, len, &retlen, buf, do_otp_lock, MTD_OTP_USER);
2884 return ret ? : retlen;
2886 #endif /* CONFIG_MTD_ONENAND_OTP */
2889 * onenand_check_features - Check and set OneNAND features
2890 * @param mtd MTD data structure
2892 * Check and set OneNAND features
2896 static void onenand_check_features(struct mtd_info *mtd)
2898 struct onenand_chip *this = mtd->priv;
2899 unsigned int density, process;
2901 /* Lock scheme depends on density and process */
2902 density = onenand_get_density(this->device_id);
2903 process = this->version_id >> ONENAND_VERSION_PROCESS_SHIFT;
2907 case ONENAND_DEVICE_DENSITY_4Gb:
2908 this->options |= ONENAND_HAS_2PLANE;
2910 case ONENAND_DEVICE_DENSITY_2Gb:
2911 /* 2Gb DDP don't have 2 plane */
2912 if (!ONENAND_IS_DDP(this))
2913 this->options |= ONENAND_HAS_2PLANE;
2914 this->options |= ONENAND_HAS_UNLOCK_ALL;
2916 case ONENAND_DEVICE_DENSITY_1Gb:
2917 /* A-Die has all block unlock */
2919 this->options |= ONENAND_HAS_UNLOCK_ALL;
2923 /* Some OneNAND has continuous lock scheme */
2925 this->options |= ONENAND_HAS_CONT_LOCK;
2929 if (ONENAND_IS_MLC(this))
2930 this->options &= ~ONENAND_HAS_2PLANE;
2932 if (FLEXONENAND(this)) {
2933 this->options &= ~ONENAND_HAS_CONT_LOCK;
2934 this->options |= ONENAND_HAS_UNLOCK_ALL;
2937 if (this->options & ONENAND_HAS_CONT_LOCK)
2938 printk(KERN_DEBUG "Lock scheme is Continuous Lock\n");
2939 if (this->options & ONENAND_HAS_UNLOCK_ALL)
2940 printk(KERN_DEBUG "Chip support all block unlock\n");
2941 if (this->options & ONENAND_HAS_2PLANE)
2942 printk(KERN_DEBUG "Chip has 2 plane\n");
2946 * onenand_print_device_info - Print device & version ID
2947 * @param device device ID
2948 * @param version version ID
2950 * Print device & version ID
2952 static void onenand_print_device_info(int device, int version)
2954 int vcc, demuxed, ddp, density, flexonenand;
2956 vcc = device & ONENAND_DEVICE_VCC_MASK;
2957 demuxed = device & ONENAND_DEVICE_IS_DEMUX;
2958 ddp = device & ONENAND_DEVICE_IS_DDP;
2959 density = onenand_get_density(device);
2960 flexonenand = device & DEVICE_IS_FLEXONENAND;
2961 printk(KERN_INFO "%s%sOneNAND%s %dMB %sV 16-bit (0x%02x)\n",
2962 demuxed ? "" : "Muxed ",
2963 flexonenand ? "Flex-" : "",
2966 vcc ? "2.65/3.3" : "1.8",
2968 printk(KERN_INFO "OneNAND version = 0x%04x\n", version);
2971 static const struct onenand_manufacturers onenand_manuf_ids[] = {
2972 {ONENAND_MFR_SAMSUNG, "Samsung"},
2973 {ONENAND_MFR_NUMONYX, "Numonyx"},
2977 * onenand_check_maf - Check manufacturer ID
2978 * @param manuf manufacturer ID
2980 * Check manufacturer ID
2982 static int onenand_check_maf(int manuf)
2984 int size = ARRAY_SIZE(onenand_manuf_ids);
2988 for (i = 0; i < size; i++)
2989 if (manuf == onenand_manuf_ids[i].id)
2993 name = onenand_manuf_ids[i].name;
2997 printk(KERN_DEBUG "OneNAND Manufacturer: %s (0x%0x)\n", name, manuf);
3003 * flexonenand_get_boundary - Reads the SLC boundary
3004 * @param onenand_info - onenand info structure
3006 static int flexonenand_get_boundary(struct mtd_info *mtd)
3008 struct onenand_chip *this = mtd->priv;
3010 int ret, syscfg, locked;
3013 syscfg = this->read_word(this->base + ONENAND_REG_SYS_CFG1);
3014 this->write_word((syscfg | 0x0100), this->base + ONENAND_REG_SYS_CFG1);
3016 for (die = 0; die < this->dies; die++) {
3017 this->command(mtd, FLEXONENAND_CMD_PI_ACCESS, die, 0);
3018 this->wait(mtd, FL_SYNCING);
3020 this->command(mtd, FLEXONENAND_CMD_READ_PI, die, 0);
3021 ret = this->wait(mtd, FL_READING);
3023 bdry = this->read_word(this->base + ONENAND_DATARAM);
3024 if ((bdry >> FLEXONENAND_PI_UNLOCK_SHIFT) == 3)
3028 this->boundary[die] = bdry & FLEXONENAND_PI_MASK;
3030 this->command(mtd, ONENAND_CMD_RESET, 0, 0);
3031 ret = this->wait(mtd, FL_RESETING);
3033 printk(KERN_INFO "Die %d boundary: %d%s\n", die,
3034 this->boundary[die], locked ? "(Locked)" : "(Unlocked)");
3038 this->write_word(syscfg, this->base + ONENAND_REG_SYS_CFG1);
3043 * flexonenand_get_size - Fill up fields in onenand_chip and mtd_info
3044 * boundary[], diesize[], mtd->size, mtd->erasesize
3045 * @param mtd - MTD device structure
3047 static void flexonenand_get_size(struct mtd_info *mtd)
3049 struct onenand_chip *this = mtd->priv;
3050 int die, i, eraseshift, density;
3051 int blksperdie, maxbdry;
3054 density = onenand_get_density(this->device_id);
3055 blksperdie = ((loff_t)(16 << density) << 20) >> (this->erase_shift);
3056 blksperdie >>= ONENAND_IS_DDP(this) ? 1 : 0;
3057 maxbdry = blksperdie - 1;
3058 eraseshift = this->erase_shift - 1;
3060 mtd->numeraseregions = this->dies << 1;
3062 /* This fills up the device boundary */
3063 flexonenand_get_boundary(mtd);
3066 for (; die < this->dies; die++) {
3067 if (!die || this->boundary[die-1] != maxbdry) {
3069 mtd->eraseregions[i].offset = ofs;
3070 mtd->eraseregions[i].erasesize = 1 << eraseshift;
3071 mtd->eraseregions[i].numblocks =
3072 this->boundary[die] + 1;
3073 ofs += mtd->eraseregions[i].numblocks << eraseshift;
3076 mtd->numeraseregions -= 1;
3077 mtd->eraseregions[i].numblocks +=
3078 this->boundary[die] + 1;
3079 ofs += (this->boundary[die] + 1) << (eraseshift - 1);
3081 if (this->boundary[die] != maxbdry) {
3083 mtd->eraseregions[i].offset = ofs;
3084 mtd->eraseregions[i].erasesize = 1 << eraseshift;
3085 mtd->eraseregions[i].numblocks = maxbdry ^
3086 this->boundary[die];
3087 ofs += mtd->eraseregions[i].numblocks << eraseshift;
3090 mtd->numeraseregions -= 1;
3093 /* Expose MLC erase size except when all blocks are SLC */
3094 mtd->erasesize = 1 << this->erase_shift;
3095 if (mtd->numeraseregions == 1)
3096 mtd->erasesize >>= 1;
3098 printk(KERN_INFO "Device has %d eraseregions\n", mtd->numeraseregions);
3099 for (i = 0; i < mtd->numeraseregions; i++)
3100 printk(KERN_INFO "[offset: 0x%08x, erasesize: 0x%05x,"
3101 " numblocks: %04u]\n",
3102 (unsigned int) mtd->eraseregions[i].offset,
3103 mtd->eraseregions[i].erasesize,
3104 mtd->eraseregions[i].numblocks);
3106 for (die = 0, mtd->size = 0; die < this->dies; die++) {
3107 this->diesize[die] = (loff_t)blksperdie << this->erase_shift;
3108 this->diesize[die] -= (loff_t)(this->boundary[die] + 1)
3109 << (this->erase_shift - 1);
3110 mtd->size += this->diesize[die];
3115 * flexonenand_check_blocks_erased - Check if blocks are erased
3116 * @param mtd_info - mtd info structure
3117 * @param start - first erase block to check
3118 * @param end - last erase block to check
3120 * Converting an unerased block from MLC to SLC
3121 * causes byte values to change. Since both data and its ECC
3122 * have changed, reads on the block give uncorrectable error.
3123 * This might lead to the block being detected as bad.
3125 * Avoid this by ensuring that the block to be converted is
3128 static int flexonenand_check_blocks_erased(struct mtd_info *mtd, int start, int end)
3130 struct onenand_chip *this = mtd->priv;
3133 struct mtd_oob_ops ops = {
3134 .mode = MTD_OOB_PLACE,
3136 .ooblen = mtd->oobsize,
3138 .oobbuf = this->oob_buf,
3142 printk(KERN_DEBUG "Check blocks from %d to %d\n", start, end);
3144 for (block = start; block <= end; block++) {
3145 addr = flexonenand_addr(this, block);
3146 if (onenand_block_isbad_nolock(mtd, addr, 0))
3150 * Since main area write results in ECC write to spare,
3151 * it is sufficient to check only ECC bytes for change.
3153 ret = onenand_read_oob_nolock(mtd, addr, &ops);
3157 for (i = 0; i < mtd->oobsize; i++)
3158 if (this->oob_buf[i] != 0xff)
3161 if (i != mtd->oobsize) {
3162 printk(KERN_WARNING "Block %d not erased.\n", block);
3171 * flexonenand_set_boundary - Writes the SLC boundary
3172 * @param mtd - mtd info structure
3174 int flexonenand_set_boundary(struct mtd_info *mtd, int die,
3175 int boundary, int lock)
3177 struct onenand_chip *this = mtd->priv;
3178 int ret, density, blksperdie, old, new, thisboundary;
3181 /* Change only once for SDP Flex-OneNAND */
3182 if (die && (!ONENAND_IS_DDP(this)))
3185 /* boundary value of -1 indicates no required change */
3186 if (boundary < 0 || boundary == this->boundary[die])
3189 density = onenand_get_density(this->device_id);
3190 blksperdie = ((16 << density) << 20) >> this->erase_shift;
3191 blksperdie >>= ONENAND_IS_DDP(this) ? 1 : 0;
3193 if (boundary >= blksperdie) {
3194 printk(KERN_ERR "flexonenand_set_boundary: Invalid boundary value. "
3195 "Boundary not changed.\n");
3199 /* Check if converting blocks are erased */
3200 old = this->boundary[die] + (die * this->density_mask);
3201 new = boundary + (die * this->density_mask);
3202 ret = flexonenand_check_blocks_erased(mtd, min(old, new) + 1, max(old, new));
3204 printk(KERN_ERR "flexonenand_set_boundary: Please erase blocks before boundary change\n");
3208 this->command(mtd, FLEXONENAND_CMD_PI_ACCESS, die, 0);
3209 this->wait(mtd, FL_SYNCING);
3211 /* Check is boundary is locked */
3212 this->command(mtd, FLEXONENAND_CMD_READ_PI, die, 0);
3213 ret = this->wait(mtd, FL_READING);
3215 thisboundary = this->read_word(this->base + ONENAND_DATARAM);
3216 if ((thisboundary >> FLEXONENAND_PI_UNLOCK_SHIFT) != 3) {
3217 printk(KERN_ERR "flexonenand_set_boundary: boundary locked\n");
3222 printk(KERN_INFO "flexonenand_set_boundary: Changing die %d boundary: %d%s\n",
3223 die, boundary, lock ? "(Locked)" : "(Unlocked)");
3225 addr = die ? this->diesize[0] : 0;
3227 boundary &= FLEXONENAND_PI_MASK;
3228 boundary |= lock ? 0 : (3 << FLEXONENAND_PI_UNLOCK_SHIFT);
3230 this->command(mtd, ONENAND_CMD_ERASE, addr, 0);
3231 ret = this->wait(mtd, FL_ERASING);
3233 printk(KERN_ERR "flexonenand_set_boundary: Failed PI erase for Die %d\n", die);
3237 this->write_word(boundary, this->base + ONENAND_DATARAM);
3238 this->command(mtd, ONENAND_CMD_PROG, addr, 0);
3239 ret = this->wait(mtd, FL_WRITING);
3241 printk(KERN_ERR "flexonenand_set_boundary: Failed PI write for Die %d\n", die);
3245 this->command(mtd, FLEXONENAND_CMD_PI_UPDATE, die, 0);
3246 ret = this->wait(mtd, FL_WRITING);
3248 this->write_word(ONENAND_CMD_RESET, this->base + ONENAND_REG_COMMAND);
3249 this->wait(mtd, FL_RESETING);
3251 /* Recalculate device size on boundary change*/
3252 flexonenand_get_size(mtd);
3258 * flexonenand_setup - capture Flex-OneNAND boundary and lock
3259 * values passed as kernel parameters
3260 * @param s kernel parameter string
3262 static int flexonenand_setup(char *s)
3266 s = get_options(s, 5, ints);
3268 for (i = 0; i < ints[0]; i++)
3269 flex_bdry[i] = ints[i + 1];
3274 __setup("onenand.bdry=", flexonenand_setup);
3277 * onenand_probe - [OneNAND Interface] Probe the OneNAND device
3278 * @param mtd MTD device structure
3280 * OneNAND detection method:
3281 * Compare the values from command with ones from register
3283 static int onenand_probe(struct mtd_info *mtd)
3285 struct onenand_chip *this = mtd->priv;
3286 int bram_maf_id, bram_dev_id, maf_id, dev_id, ver_id;
3290 /* Save system configuration 1 */
3291 syscfg = this->read_word(this->base + ONENAND_REG_SYS_CFG1);
3292 /* Clear Sync. Burst Read mode to read BootRAM */
3293 this->write_word((syscfg & ~ONENAND_SYS_CFG1_SYNC_READ & ~ONENAND_SYS_CFG1_SYNC_WRITE), this->base + ONENAND_REG_SYS_CFG1);
3295 /* Send the command for reading device ID from BootRAM */
3296 this->write_word(ONENAND_CMD_READID, this->base + ONENAND_BOOTRAM);
3298 /* Read manufacturer and device IDs from BootRAM */
3299 bram_maf_id = this->read_word(this->base + ONENAND_BOOTRAM + 0x0);
3300 bram_dev_id = this->read_word(this->base + ONENAND_BOOTRAM + 0x2);
3302 /* Reset OneNAND to read default register values */
3303 this->write_word(ONENAND_CMD_RESET, this->base + ONENAND_BOOTRAM);
3305 this->wait(mtd, FL_RESETING);
3307 /* Restore system configuration 1 */
3308 this->write_word(syscfg, this->base + ONENAND_REG_SYS_CFG1);
3310 /* Check manufacturer ID */
3311 if (onenand_check_maf(bram_maf_id))
3314 /* Read manufacturer and device IDs from Register */
3315 maf_id = this->read_word(this->base + ONENAND_REG_MANUFACTURER_ID);
3316 dev_id = this->read_word(this->base + ONENAND_REG_DEVICE_ID);
3317 ver_id = this->read_word(this->base + ONENAND_REG_VERSION_ID);
3318 this->technology = this->read_word(this->base + ONENAND_REG_TECHNOLOGY);
3320 /* Check OneNAND device */
3321 if (maf_id != bram_maf_id || dev_id != bram_dev_id)
3324 /* Flash device information */
3325 onenand_print_device_info(dev_id, ver_id);
3326 this->device_id = dev_id;
3327 this->version_id = ver_id;
3329 density = onenand_get_density(dev_id);
3330 if (FLEXONENAND(this)) {
3331 this->dies = ONENAND_IS_DDP(this) ? 2 : 1;
3332 /* Maximum possible erase regions */
3333 mtd->numeraseregions = this->dies << 1;
3334 mtd->eraseregions = kzalloc(sizeof(struct mtd_erase_region_info)
3335 * (this->dies << 1), GFP_KERNEL);
3336 if (!mtd->eraseregions)
3341 * For Flex-OneNAND, chipsize represents maximum possible device size.
3342 * mtd->size represents the actual device size.
3344 this->chipsize = (16 << density) << 20;
3346 /* OneNAND page size & block size */
3347 /* The data buffer size is equal to page size */
3348 mtd->writesize = this->read_word(this->base + ONENAND_REG_DATA_BUFFER_SIZE);
3349 /* We use the full BufferRAM */
3350 if (ONENAND_IS_MLC(this))
3351 mtd->writesize <<= 1;
3353 mtd->oobsize = mtd->writesize >> 5;
3354 /* Pages per a block are always 64 in OneNAND */
3355 mtd->erasesize = mtd->writesize << 6;
3357 * Flex-OneNAND SLC area has 64 pages per block.
3358 * Flex-OneNAND MLC area has 128 pages per block.
3359 * Expose MLC erase size to find erase_shift and page_mask.
3361 if (FLEXONENAND(this))
3362 mtd->erasesize <<= 1;
3364 this->erase_shift = ffs(mtd->erasesize) - 1;
3365 this->page_shift = ffs(mtd->writesize) - 1;
3366 this->page_mask = (1 << (this->erase_shift - this->page_shift)) - 1;
3367 /* Set density mask. it is used for DDP */
3368 if (ONENAND_IS_DDP(this))
3369 this->density_mask = this->chipsize >> (this->erase_shift + 1);
3370 /* It's real page size */
3371 this->writesize = mtd->writesize;
3373 /* REVIST: Multichip handling */
3375 if (FLEXONENAND(this))
3376 flexonenand_get_size(mtd);
3378 mtd->size = this->chipsize;
3380 /* Check OneNAND features */
3381 onenand_check_features(mtd);
3384 * We emulate the 4KiB page and 256KiB erase block size
3385 * But oobsize is still 64 bytes.
3386 * It is only valid if you turn on 2X program support,
3387 * Otherwise it will be ignored by compiler.
3389 if (ONENAND_IS_2PLANE(this)) {
3390 mtd->writesize <<= 1;
3391 mtd->erasesize <<= 1;
3398 * onenand_suspend - [MTD Interface] Suspend the OneNAND flash
3399 * @param mtd MTD device structure
3401 static int onenand_suspend(struct mtd_info *mtd)
3403 return onenand_get_device(mtd, FL_PM_SUSPENDED);
3407 * onenand_resume - [MTD Interface] Resume the OneNAND flash
3408 * @param mtd MTD device structure
3410 static void onenand_resume(struct mtd_info *mtd)
3412 struct onenand_chip *this = mtd->priv;
3414 if (this->state == FL_PM_SUSPENDED)
3415 onenand_release_device(mtd);
3417 printk(KERN_ERR "resume() called for the chip which is not"
3418 "in suspended state\n");
3422 * onenand_scan - [OneNAND Interface] Scan for the OneNAND device
3423 * @param mtd MTD device structure
3424 * @param maxchips Number of chips to scan for
3426 * This fills out all the not initialized function pointers
3427 * with the defaults.
3428 * The flash ID is read and the mtd/chip structures are
3429 * filled with the appropriate values.
3431 int onenand_scan(struct mtd_info *mtd, int maxchips)
3434 struct onenand_chip *this = mtd->priv;
3436 if (!this->read_word)
3437 this->read_word = onenand_readw;
3438 if (!this->write_word)
3439 this->write_word = onenand_writew;
3442 this->command = onenand_command;
3444 onenand_setup_wait(mtd);
3446 if (!this->read_bufferram)
3447 this->read_bufferram = onenand_read_bufferram;
3448 if (!this->write_bufferram)
3449 this->write_bufferram = onenand_write_bufferram;
3451 if (!this->block_markbad)
3452 this->block_markbad = onenand_default_block_markbad;
3453 if (!this->scan_bbt)
3454 this->scan_bbt = onenand_default_bbt;
3456 if (onenand_probe(mtd))
3459 /* Set Sync. Burst Read after probing */
3460 if (this->mmcontrol) {
3461 printk(KERN_INFO "OneNAND Sync. Burst Read support\n");
3462 this->read_bufferram = onenand_sync_read_bufferram;
3465 /* Allocate buffers, if necessary */
3466 if (!this->page_buf) {
3467 this->page_buf = kzalloc(mtd->writesize, GFP_KERNEL);
3468 if (!this->page_buf) {
3469 printk(KERN_ERR "onenand_scan(): Can't allocate page_buf\n");
3472 this->options |= ONENAND_PAGEBUF_ALLOC;
3474 if (!this->oob_buf) {
3475 this->oob_buf = kzalloc(mtd->oobsize, GFP_KERNEL);
3476 if (!this->oob_buf) {
3477 printk(KERN_ERR "onenand_scan(): Can't allocate oob_buf\n");
3478 if (this->options & ONENAND_PAGEBUF_ALLOC) {
3479 this->options &= ~ONENAND_PAGEBUF_ALLOC;
3480 kfree(this->page_buf);
3484 this->options |= ONENAND_OOBBUF_ALLOC;
3487 this->state = FL_READY;
3488 init_waitqueue_head(&this->wq);
3489 spin_lock_init(&this->chip_lock);
3492 * Allow subpage writes up to oobsize.
3494 switch (mtd->oobsize) {
3496 this->ecclayout = &onenand_oob_128;
3497 mtd->subpage_sft = 0;
3500 this->ecclayout = &onenand_oob_64;
3501 mtd->subpage_sft = 2;
3505 this->ecclayout = &onenand_oob_32;
3506 mtd->subpage_sft = 1;
3510 printk(KERN_WARNING "No OOB scheme defined for oobsize %d\n",
3512 mtd->subpage_sft = 0;
3513 /* To prevent kernel oops */
3514 this->ecclayout = &onenand_oob_32;
3518 this->subpagesize = mtd->writesize >> mtd->subpage_sft;
3521 * The number of bytes available for a client to place data into
3522 * the out of band area
3524 this->ecclayout->oobavail = 0;
3525 for (i = 0; i < MTD_MAX_OOBFREE_ENTRIES &&
3526 this->ecclayout->oobfree[i].length; i++)
3527 this->ecclayout->oobavail +=
3528 this->ecclayout->oobfree[i].length;
3529 mtd->oobavail = this->ecclayout->oobavail;
3531 mtd->ecclayout = this->ecclayout;
3533 /* Fill in remaining MTD driver data */
3534 mtd->type = MTD_NANDFLASH;
3535 mtd->flags = MTD_CAP_NANDFLASH;
3536 mtd->erase = onenand_erase;
3538 mtd->unpoint = NULL;
3539 mtd->read = onenand_read;
3540 mtd->write = onenand_write;
3541 mtd->read_oob = onenand_read_oob;
3542 mtd->write_oob = onenand_write_oob;
3543 mtd->panic_write = onenand_panic_write;
3544 #ifdef CONFIG_MTD_ONENAND_OTP
3545 mtd->get_fact_prot_info = onenand_get_fact_prot_info;
3546 mtd->read_fact_prot_reg = onenand_read_fact_prot_reg;
3547 mtd->get_user_prot_info = onenand_get_user_prot_info;
3548 mtd->read_user_prot_reg = onenand_read_user_prot_reg;
3549 mtd->write_user_prot_reg = onenand_write_user_prot_reg;
3550 mtd->lock_user_prot_reg = onenand_lock_user_prot_reg;
3552 mtd->sync = onenand_sync;
3553 mtd->lock = onenand_lock;
3554 mtd->unlock = onenand_unlock;
3555 mtd->suspend = onenand_suspend;
3556 mtd->resume = onenand_resume;
3557 mtd->block_isbad = onenand_block_isbad;
3558 mtd->block_markbad = onenand_block_markbad;
3559 mtd->owner = THIS_MODULE;
3561 /* Unlock whole block */
3562 onenand_unlock_all(mtd);
3564 ret = this->scan_bbt(mtd);
3565 if ((!FLEXONENAND(this)) || ret)
3568 /* Change Flex-OneNAND boundaries if required */
3569 for (i = 0; i < MAX_DIES; i++)
3570 flexonenand_set_boundary(mtd, i, flex_bdry[2 * i],
3571 flex_bdry[(2 * i) + 1]);
3577 * onenand_release - [OneNAND Interface] Free resources held by the OneNAND device
3578 * @param mtd MTD device structure
3580 void onenand_release(struct mtd_info *mtd)
3582 struct onenand_chip *this = mtd->priv;
3584 #ifdef CONFIG_MTD_PARTITIONS
3585 /* Deregister partitions */
3586 del_mtd_partitions (mtd);
3588 /* Deregister the device */
3589 del_mtd_device (mtd);
3591 /* Free bad block table memory, if allocated */
3593 struct bbm_info *bbm = this->bbm;
3597 /* Buffers allocated by onenand_scan */
3598 if (this->options & ONENAND_PAGEBUF_ALLOC)
3599 kfree(this->page_buf);
3600 if (this->options & ONENAND_OOBBUF_ALLOC)
3601 kfree(this->oob_buf);
3602 kfree(mtd->eraseregions);
3605 EXPORT_SYMBOL_GPL(onenand_scan);
3606 EXPORT_SYMBOL_GPL(onenand_release);
3608 MODULE_LICENSE("GPL");
3609 MODULE_AUTHOR("Kyungmin Park <kyungmin.park@samsung.com>");
3610 MODULE_DESCRIPTION("Generic OneNAND flash driver code");