1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright © 2005-2009 Samsung Electronics
4 * Copyright © 2007 Nokia Corporation
6 * Kyungmin Park <kyungmin.park@samsung.com>
9 * Adrian Hunter <ext-adrian.hunter@nokia.com>:
10 * auto-placement support, read-while load support, various fixes
12 * Vishak G <vishak.g at samsung.com>, Rohit Hagargundgi <h.rohit at samsung.com>
13 * Flex-OneNAND support
14 * Amul Kumar Saha <amul.saha at samsung.com>
18 #include <linux/kernel.h>
19 #include <linux/module.h>
20 #include <linux/moduleparam.h>
21 #include <linux/slab.h>
22 #include <linux/sched.h>
23 #include <linux/delay.h>
24 #include <linux/interrupt.h>
25 #include <linux/jiffies.h>
26 #include <linux/mtd/mtd.h>
27 #include <linux/mtd/onenand.h>
28 #include <linux/mtd/partitions.h>
33 * Multiblock erase if number of blocks to erase is 2 or more.
34 * Maximum number of blocks for simultaneous erase is 64.
36 #define MB_ERASE_MIN_BLK_COUNT 2
37 #define MB_ERASE_MAX_BLK_COUNT 64
39 /* Default Flex-OneNAND boundary and lock respectively */
40 static int flex_bdry[MAX_DIES * 2] = { -1, 0, -1, 0 };
42 module_param_array(flex_bdry, int, NULL, 0400);
43 MODULE_PARM_DESC(flex_bdry, "SLC Boundary information for Flex-OneNAND"
44 "Syntax:flex_bdry=DIE_BDRY,LOCK,..."
45 "DIE_BDRY: SLC boundary of the die"
46 "LOCK: Locking information for SLC boundary"
47 " : 0->Set boundary in unlocked status"
48 " : 1->Set boundary in locked status");
50 /* Default OneNAND/Flex-OneNAND OTP options*/
53 module_param(otp, int, 0400);
54 MODULE_PARM_DESC(otp, "Corresponding behaviour of OneNAND in OTP"
55 "Syntax : otp=LOCK_TYPE"
56 "LOCK_TYPE : Keys issued, for specific OTP Lock type"
57 " : 0 -> Default (No Blocks Locked)"
58 " : 1 -> OTP Block lock"
59 " : 2 -> 1st Block lock"
60 " : 3 -> BOTH OTP Block and 1st Block lock");
63 * flexonenand_oob_128 - oob info for Flex-Onenand with 4KB page
64 * For now, we expose only 64 out of 80 ecc bytes
66 static int flexonenand_ooblayout_ecc(struct mtd_info *mtd, int section,
67 struct mtd_oob_region *oobregion)
72 oobregion->offset = (section * 16) + 6;
73 oobregion->length = 10;
78 static int flexonenand_ooblayout_free(struct mtd_info *mtd, int section,
79 struct mtd_oob_region *oobregion)
84 oobregion->offset = (section * 16) + 2;
85 oobregion->length = 4;
90 static const struct mtd_ooblayout_ops flexonenand_ooblayout_ops = {
91 .ecc = flexonenand_ooblayout_ecc,
92 .free = flexonenand_ooblayout_free,
96 * onenand_oob_128 - oob info for OneNAND with 4KB page
98 * Based on specification:
99 * 4Gb M-die OneNAND Flash (KFM4G16Q4M, KFN8G16Q4M). Rev. 1.3, Apr. 2010
102 static int onenand_ooblayout_128_ecc(struct mtd_info *mtd, int section,
103 struct mtd_oob_region *oobregion)
108 oobregion->offset = (section * 16) + 7;
109 oobregion->length = 9;
114 static int onenand_ooblayout_128_free(struct mtd_info *mtd, int section,
115 struct mtd_oob_region *oobregion)
121 * free bytes are using the spare area fields marked as
122 * "Managed by internal ECC logic for Logical Sector Number area"
124 oobregion->offset = (section * 16) + 2;
125 oobregion->length = 3;
130 static const struct mtd_ooblayout_ops onenand_oob_128_ooblayout_ops = {
131 .ecc = onenand_ooblayout_128_ecc,
132 .free = onenand_ooblayout_128_free,
136 * onenand_oob_32_64 - oob info for large (2KB) page
138 static int onenand_ooblayout_32_64_ecc(struct mtd_info *mtd, int section,
139 struct mtd_oob_region *oobregion)
144 oobregion->offset = (section * 16) + 8;
145 oobregion->length = 5;
150 static int onenand_ooblayout_32_64_free(struct mtd_info *mtd, int section,
151 struct mtd_oob_region *oobregion)
153 int sections = (mtd->oobsize / 32) * 2;
155 if (section >= sections)
159 oobregion->offset = ((section - 1) * 16) + 14;
160 oobregion->length = 2;
162 oobregion->offset = (section * 16) + 2;
163 oobregion->length = 3;
169 static const struct mtd_ooblayout_ops onenand_oob_32_64_ooblayout_ops = {
170 .ecc = onenand_ooblayout_32_64_ecc,
171 .free = onenand_ooblayout_32_64_free,
174 static const unsigned char ffchars[] = {
175 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
176 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 16 */
177 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
178 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 32 */
179 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
180 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 48 */
181 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
182 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 64 */
183 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
184 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 80 */
185 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
186 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 96 */
187 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
188 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 112 */
189 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
190 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 128 */
194 * onenand_readw - [OneNAND Interface] Read OneNAND register
195 * @param addr address to read
197 * Read OneNAND register
199 static unsigned short onenand_readw(void __iomem *addr)
205 * onenand_writew - [OneNAND Interface] Write OneNAND register with value
206 * @param value value to write
207 * @param addr address to write
209 * Write OneNAND register with value
211 static void onenand_writew(unsigned short value, void __iomem *addr)
217 * onenand_block_address - [DEFAULT] Get block address
218 * @param this onenand chip data structure
219 * @param block the block
220 * @return translated block address if DDP, otherwise same
222 * Setup Start Address 1 Register (F100h)
224 static int onenand_block_address(struct onenand_chip *this, int block)
226 /* Device Flash Core select, NAND Flash Block Address */
227 if (block & this->density_mask)
228 return ONENAND_DDP_CHIP1 | (block ^ this->density_mask);
234 * onenand_bufferram_address - [DEFAULT] Get bufferram address
235 * @param this onenand chip data structure
236 * @param block the block
237 * @return set DBS value if DDP, otherwise 0
239 * Setup Start Address 2 Register (F101h) for DDP
241 static int onenand_bufferram_address(struct onenand_chip *this, int block)
243 /* Device BufferRAM Select */
244 if (block & this->density_mask)
245 return ONENAND_DDP_CHIP1;
247 return ONENAND_DDP_CHIP0;
251 * onenand_page_address - [DEFAULT] Get page address
252 * @param page the page address
253 * @param sector the sector address
254 * @return combined page and sector address
256 * Setup Start Address 8 Register (F107h)
258 static int onenand_page_address(int page, int sector)
260 /* Flash Page Address, Flash Sector Address */
263 fpa = page & ONENAND_FPA_MASK;
264 fsa = sector & ONENAND_FSA_MASK;
266 return ((fpa << ONENAND_FPA_SHIFT) | fsa);
270 * onenand_buffer_address - [DEFAULT] Get buffer address
271 * @param dataram1 DataRAM index
272 * @param sectors the sector address
273 * @param count the number of sectors
274 * @return the start buffer value
276 * Setup Start Buffer Register (F200h)
278 static int onenand_buffer_address(int dataram1, int sectors, int count)
282 /* BufferRAM Sector Address */
283 bsa = sectors & ONENAND_BSA_MASK;
286 bsa |= ONENAND_BSA_DATARAM1; /* DataRAM1 */
288 bsa |= ONENAND_BSA_DATARAM0; /* DataRAM0 */
290 /* BufferRAM Sector Count */
291 bsc = count & ONENAND_BSC_MASK;
293 return ((bsa << ONENAND_BSA_SHIFT) | bsc);
297 * flexonenand_block- For given address return block number
298 * @param this - OneNAND device structure
299 * @param addr - Address for which block number is needed
301 static unsigned flexonenand_block(struct onenand_chip *this, loff_t addr)
303 unsigned boundary, blk, die = 0;
305 if (ONENAND_IS_DDP(this) && addr >= this->diesize[0]) {
307 addr -= this->diesize[0];
310 boundary = this->boundary[die];
312 blk = addr >> (this->erase_shift - 1);
314 blk = (blk + boundary + 1) >> 1;
316 blk += die ? this->density_mask : 0;
320 inline unsigned onenand_block(struct onenand_chip *this, loff_t addr)
322 if (!FLEXONENAND(this))
323 return addr >> this->erase_shift;
324 return flexonenand_block(this, addr);
328 * flexonenand_addr - Return address of the block
329 * @this: OneNAND device structure
330 * @block: Block number on Flex-OneNAND
332 * Return address of the block
334 static loff_t flexonenand_addr(struct onenand_chip *this, int block)
337 int die = 0, boundary;
339 if (ONENAND_IS_DDP(this) && block >= this->density_mask) {
340 block -= this->density_mask;
342 ofs = this->diesize[0];
345 boundary = this->boundary[die];
346 ofs += (loff_t)block << (this->erase_shift - 1);
347 if (block > (boundary + 1))
348 ofs += (loff_t)(block - boundary - 1) << (this->erase_shift - 1);
352 loff_t onenand_addr(struct onenand_chip *this, int block)
354 if (!FLEXONENAND(this))
355 return (loff_t)block << this->erase_shift;
356 return flexonenand_addr(this, block);
358 EXPORT_SYMBOL(onenand_addr);
361 * onenand_get_density - [DEFAULT] Get OneNAND density
362 * @param dev_id OneNAND device ID
364 * Get OneNAND density from device ID
366 static inline int onenand_get_density(int dev_id)
368 int density = dev_id >> ONENAND_DEVICE_DENSITY_SHIFT;
369 return (density & ONENAND_DEVICE_DENSITY_MASK);
373 * flexonenand_region - [Flex-OneNAND] Return erase region of addr
374 * @param mtd MTD device structure
375 * @param addr address whose erase region needs to be identified
377 int flexonenand_region(struct mtd_info *mtd, loff_t addr)
381 for (i = 0; i < mtd->numeraseregions; i++)
382 if (addr < mtd->eraseregions[i].offset)
386 EXPORT_SYMBOL(flexonenand_region);
389 * onenand_command - [DEFAULT] Send command to OneNAND device
390 * @param mtd MTD device structure
391 * @param cmd the command to be sent
392 * @param addr offset to read from or write to
393 * @param len number of bytes to read or write
395 * Send command to OneNAND device. This function is used for middle/large page
396 * devices (1KB/2KB Bytes per page)
398 static int onenand_command(struct mtd_info *mtd, int cmd, loff_t addr, size_t len)
400 struct onenand_chip *this = mtd->priv;
401 int value, block, page;
403 /* Address translation */
405 case ONENAND_CMD_UNLOCK:
406 case ONENAND_CMD_LOCK:
407 case ONENAND_CMD_LOCK_TIGHT:
408 case ONENAND_CMD_UNLOCK_ALL:
413 case FLEXONENAND_CMD_PI_ACCESS:
414 /* addr contains die index */
415 block = addr * this->density_mask;
419 case ONENAND_CMD_ERASE:
420 case ONENAND_CMD_MULTIBLOCK_ERASE:
421 case ONENAND_CMD_ERASE_VERIFY:
422 case ONENAND_CMD_BUFFERRAM:
423 case ONENAND_CMD_OTP_ACCESS:
424 block = onenand_block(this, addr);
428 case FLEXONENAND_CMD_READ_PI:
429 cmd = ONENAND_CMD_READ;
430 block = addr * this->density_mask;
435 block = onenand_block(this, addr);
436 if (FLEXONENAND(this))
437 page = (int) (addr - onenand_addr(this, block))>>\
440 page = (int) (addr >> this->page_shift);
441 if (ONENAND_IS_2PLANE(this)) {
442 /* Make the even block number */
444 /* Is it the odd plane? */
445 if (addr & this->writesize)
449 page &= this->page_mask;
453 /* NOTE: The setting order of the registers is very important! */
454 if (cmd == ONENAND_CMD_BUFFERRAM) {
455 /* Select DataRAM for DDP */
456 value = onenand_bufferram_address(this, block);
457 this->write_word(value, this->base + ONENAND_REG_START_ADDRESS2);
459 if (ONENAND_IS_2PLANE(this) || ONENAND_IS_4KB_PAGE(this))
460 /* It is always BufferRAM0 */
461 ONENAND_SET_BUFFERRAM0(this);
463 /* Switch to the next data buffer */
464 ONENAND_SET_NEXT_BUFFERRAM(this);
470 /* Write 'DFS, FBA' of Flash */
471 value = onenand_block_address(this, block);
472 this->write_word(value, this->base + ONENAND_REG_START_ADDRESS1);
474 /* Select DataRAM for DDP */
475 value = onenand_bufferram_address(this, block);
476 this->write_word(value, this->base + ONENAND_REG_START_ADDRESS2);
480 /* Now we use page size operation */
481 int sectors = 0, count = 0;
485 case FLEXONENAND_CMD_RECOVER_LSB:
486 case ONENAND_CMD_READ:
487 case ONENAND_CMD_READOOB:
488 if (ONENAND_IS_4KB_PAGE(this))
489 /* It is always BufferRAM0 */
490 dataram = ONENAND_SET_BUFFERRAM0(this);
492 dataram = ONENAND_SET_NEXT_BUFFERRAM(this);
496 if (ONENAND_IS_2PLANE(this) && cmd == ONENAND_CMD_PROG)
497 cmd = ONENAND_CMD_2X_PROG;
498 dataram = ONENAND_CURRENT_BUFFERRAM(this);
502 /* Write 'FPA, FSA' of Flash */
503 value = onenand_page_address(page, sectors);
504 this->write_word(value, this->base + ONENAND_REG_START_ADDRESS8);
506 /* Write 'BSA, BSC' of DataRAM */
507 value = onenand_buffer_address(dataram, sectors, count);
508 this->write_word(value, this->base + ONENAND_REG_START_BUFFER);
511 /* Interrupt clear */
512 this->write_word(ONENAND_INT_CLEAR, this->base + ONENAND_REG_INTERRUPT);
515 this->write_word(cmd, this->base + ONENAND_REG_COMMAND);
521 * onenand_read_ecc - return ecc status
522 * @param this onenand chip structure
524 static inline int onenand_read_ecc(struct onenand_chip *this)
526 int ecc, i, result = 0;
528 if (!FLEXONENAND(this) && !ONENAND_IS_4KB_PAGE(this))
529 return this->read_word(this->base + ONENAND_REG_ECC_STATUS);
531 for (i = 0; i < 4; i++) {
532 ecc = this->read_word(this->base + ONENAND_REG_ECC_STATUS + i*2);
535 if (ecc & FLEXONENAND_UNCORRECTABLE_ERROR)
536 return ONENAND_ECC_2BIT_ALL;
538 result = ONENAND_ECC_1BIT_ALL;
545 * onenand_wait - [DEFAULT] wait until the command is done
546 * @param mtd MTD device structure
547 * @param state state to select the max. timeout value
549 * Wait for command done. This applies to all OneNAND command
550 * Read can take up to 30us, erase up to 2ms and program up to 350us
551 * according to general OneNAND specs
553 static int onenand_wait(struct mtd_info *mtd, int state)
555 struct onenand_chip * this = mtd->priv;
556 unsigned long timeout;
557 unsigned int flags = ONENAND_INT_MASTER;
558 unsigned int interrupt = 0;
561 /* The 20 msec is enough */
562 timeout = jiffies + msecs_to_jiffies(20);
563 while (time_before(jiffies, timeout)) {
564 interrupt = this->read_word(this->base + ONENAND_REG_INTERRUPT);
566 if (interrupt & flags)
569 if (state != FL_READING && state != FL_PREPARING_ERASE)
572 /* To get correct interrupt status in timeout case */
573 interrupt = this->read_word(this->base + ONENAND_REG_INTERRUPT);
575 ctrl = this->read_word(this->base + ONENAND_REG_CTRL_STATUS);
578 * In the Spec. it checks the controller status first
579 * However if you get the correct information in case of
580 * power off recovery (POR) test, it should read ECC status first
582 if (interrupt & ONENAND_INT_READ) {
583 int ecc = onenand_read_ecc(this);
585 if (ecc & ONENAND_ECC_2BIT_ALL) {
586 printk(KERN_ERR "%s: ECC error = 0x%04x\n",
588 mtd->ecc_stats.failed++;
590 } else if (ecc & ONENAND_ECC_1BIT_ALL) {
591 printk(KERN_DEBUG "%s: correctable ECC error = 0x%04x\n",
593 mtd->ecc_stats.corrected++;
596 } else if (state == FL_READING) {
597 printk(KERN_ERR "%s: read timeout! ctrl=0x%04x intr=0x%04x\n",
598 __func__, ctrl, interrupt);
602 if (state == FL_PREPARING_ERASE && !(interrupt & ONENAND_INT_ERASE)) {
603 printk(KERN_ERR "%s: mb erase timeout! ctrl=0x%04x intr=0x%04x\n",
604 __func__, ctrl, interrupt);
608 if (!(interrupt & ONENAND_INT_MASTER)) {
609 printk(KERN_ERR "%s: timeout! ctrl=0x%04x intr=0x%04x\n",
610 __func__, ctrl, interrupt);
614 /* If there's controller error, it's a real error */
615 if (ctrl & ONENAND_CTRL_ERROR) {
616 printk(KERN_ERR "%s: controller error = 0x%04x\n",
618 if (ctrl & ONENAND_CTRL_LOCK)
619 printk(KERN_ERR "%s: it's locked error.\n", __func__);
627 * onenand_interrupt - [DEFAULT] onenand interrupt handler
628 * @param irq onenand interrupt number
629 * @param dev_id interrupt data
633 static irqreturn_t onenand_interrupt(int irq, void *data)
635 struct onenand_chip *this = data;
637 /* To handle shared interrupt */
638 if (!this->complete.done)
639 complete(&this->complete);
645 * onenand_interrupt_wait - [DEFAULT] wait until the command is done
646 * @param mtd MTD device structure
647 * @param state state to select the max. timeout value
649 * Wait for command done.
651 static int onenand_interrupt_wait(struct mtd_info *mtd, int state)
653 struct onenand_chip *this = mtd->priv;
655 wait_for_completion(&this->complete);
657 return onenand_wait(mtd, state);
661 * onenand_try_interrupt_wait - [DEFAULT] try interrupt wait
662 * @param mtd MTD device structure
663 * @param state state to select the max. timeout value
665 * Try interrupt based wait (It is used one-time)
667 static int onenand_try_interrupt_wait(struct mtd_info *mtd, int state)
669 struct onenand_chip *this = mtd->priv;
670 unsigned long remain, timeout;
672 /* We use interrupt wait first */
673 this->wait = onenand_interrupt_wait;
675 timeout = msecs_to_jiffies(100);
676 remain = wait_for_completion_timeout(&this->complete, timeout);
678 printk(KERN_INFO "OneNAND: There's no interrupt. "
679 "We use the normal wait\n");
681 /* Release the irq */
682 free_irq(this->irq, this);
684 this->wait = onenand_wait;
687 return onenand_wait(mtd, state);
691 * onenand_setup_wait - [OneNAND Interface] setup onenand wait method
692 * @param mtd MTD device structure
694 * There's two method to wait onenand work
695 * 1. polling - read interrupt status register
696 * 2. interrupt - use the kernel interrupt method
698 static void onenand_setup_wait(struct mtd_info *mtd)
700 struct onenand_chip *this = mtd->priv;
703 init_completion(&this->complete);
705 if (this->irq <= 0) {
706 this->wait = onenand_wait;
710 if (request_irq(this->irq, &onenand_interrupt,
711 IRQF_SHARED, "onenand", this)) {
712 /* If we can't get irq, use the normal wait */
713 this->wait = onenand_wait;
717 /* Enable interrupt */
718 syscfg = this->read_word(this->base + ONENAND_REG_SYS_CFG1);
719 syscfg |= ONENAND_SYS_CFG1_IOBE;
720 this->write_word(syscfg, this->base + ONENAND_REG_SYS_CFG1);
722 this->wait = onenand_try_interrupt_wait;
726 * onenand_bufferram_offset - [DEFAULT] BufferRAM offset
727 * @param mtd MTD data structure
728 * @param area BufferRAM area
729 * @return offset given area
731 * Return BufferRAM offset given area
733 static inline int onenand_bufferram_offset(struct mtd_info *mtd, int area)
735 struct onenand_chip *this = mtd->priv;
737 if (ONENAND_CURRENT_BUFFERRAM(this)) {
738 /* Note: the 'this->writesize' is a real page size */
739 if (area == ONENAND_DATARAM)
740 return this->writesize;
741 if (area == ONENAND_SPARERAM)
749 * onenand_read_bufferram - [OneNAND Interface] Read the bufferram area
750 * @param mtd MTD data structure
751 * @param area BufferRAM area
752 * @param buffer the databuffer to put/get data
753 * @param offset offset to read from or write to
754 * @param count number of bytes to read/write
756 * Read the BufferRAM area
758 static int onenand_read_bufferram(struct mtd_info *mtd, int area,
759 unsigned char *buffer, int offset, size_t count)
761 struct onenand_chip *this = mtd->priv;
762 void __iomem *bufferram;
764 bufferram = this->base + area;
766 bufferram += onenand_bufferram_offset(mtd, area);
768 if (ONENAND_CHECK_BYTE_ACCESS(count)) {
771 /* Align with word(16-bit) size */
774 /* Read word and save byte */
775 word = this->read_word(bufferram + offset + count);
776 buffer[count] = (word & 0xff);
779 memcpy(buffer, bufferram + offset, count);
785 * onenand_sync_read_bufferram - [OneNAND Interface] Read the bufferram area with Sync. Burst mode
786 * @param mtd MTD data structure
787 * @param area BufferRAM area
788 * @param buffer the databuffer to put/get data
789 * @param offset offset to read from or write to
790 * @param count number of bytes to read/write
792 * Read the BufferRAM area with Sync. Burst Mode
794 static int onenand_sync_read_bufferram(struct mtd_info *mtd, int area,
795 unsigned char *buffer, int offset, size_t count)
797 struct onenand_chip *this = mtd->priv;
798 void __iomem *bufferram;
800 bufferram = this->base + area;
802 bufferram += onenand_bufferram_offset(mtd, area);
804 this->mmcontrol(mtd, ONENAND_SYS_CFG1_SYNC_READ);
806 if (ONENAND_CHECK_BYTE_ACCESS(count)) {
809 /* Align with word(16-bit) size */
812 /* Read word and save byte */
813 word = this->read_word(bufferram + offset + count);
814 buffer[count] = (word & 0xff);
817 memcpy(buffer, bufferram + offset, count);
819 this->mmcontrol(mtd, 0);
825 * onenand_write_bufferram - [OneNAND Interface] Write the bufferram area
826 * @param mtd MTD data structure
827 * @param area BufferRAM area
828 * @param buffer the databuffer to put/get data
829 * @param offset offset to read from or write to
830 * @param count number of bytes to read/write
832 * Write the BufferRAM area
834 static int onenand_write_bufferram(struct mtd_info *mtd, int area,
835 const unsigned char *buffer, int offset, size_t count)
837 struct onenand_chip *this = mtd->priv;
838 void __iomem *bufferram;
840 bufferram = this->base + area;
842 bufferram += onenand_bufferram_offset(mtd, area);
844 if (ONENAND_CHECK_BYTE_ACCESS(count)) {
848 /* Align with word(16-bit) size */
851 /* Calculate byte access offset */
852 byte_offset = offset + count;
854 /* Read word and save byte */
855 word = this->read_word(bufferram + byte_offset);
856 word = (word & ~0xff) | buffer[count];
857 this->write_word(word, bufferram + byte_offset);
860 memcpy(bufferram + offset, buffer, count);
866 * onenand_get_2x_blockpage - [GENERIC] Get blockpage at 2x program mode
867 * @param mtd MTD data structure
868 * @param addr address to check
869 * @return blockpage address
871 * Get blockpage address at 2x program mode
873 static int onenand_get_2x_blockpage(struct mtd_info *mtd, loff_t addr)
875 struct onenand_chip *this = mtd->priv;
876 int blockpage, block, page;
878 /* Calculate the even block number */
879 block = (int) (addr >> this->erase_shift) & ~1;
880 /* Is it the odd plane? */
881 if (addr & this->writesize)
883 page = (int) (addr >> (this->page_shift + 1)) & this->page_mask;
884 blockpage = (block << 7) | page;
890 * onenand_check_bufferram - [GENERIC] Check BufferRAM information
891 * @param mtd MTD data structure
892 * @param addr address to check
893 * @return 1 if there are valid data, otherwise 0
895 * Check bufferram if there is data we required
897 static int onenand_check_bufferram(struct mtd_info *mtd, loff_t addr)
899 struct onenand_chip *this = mtd->priv;
900 int blockpage, found = 0;
903 if (ONENAND_IS_2PLANE(this))
904 blockpage = onenand_get_2x_blockpage(mtd, addr);
906 blockpage = (int) (addr >> this->page_shift);
908 /* Is there valid data? */
909 i = ONENAND_CURRENT_BUFFERRAM(this);
910 if (this->bufferram[i].blockpage == blockpage)
913 /* Check another BufferRAM */
914 i = ONENAND_NEXT_BUFFERRAM(this);
915 if (this->bufferram[i].blockpage == blockpage) {
916 ONENAND_SET_NEXT_BUFFERRAM(this);
921 if (found && ONENAND_IS_DDP(this)) {
922 /* Select DataRAM for DDP */
923 int block = onenand_block(this, addr);
924 int value = onenand_bufferram_address(this, block);
925 this->write_word(value, this->base + ONENAND_REG_START_ADDRESS2);
932 * onenand_update_bufferram - [GENERIC] Update BufferRAM information
933 * @param mtd MTD data structure
934 * @param addr address to update
935 * @param valid valid flag
937 * Update BufferRAM information
939 static void onenand_update_bufferram(struct mtd_info *mtd, loff_t addr,
942 struct onenand_chip *this = mtd->priv;
946 if (ONENAND_IS_2PLANE(this))
947 blockpage = onenand_get_2x_blockpage(mtd, addr);
949 blockpage = (int) (addr >> this->page_shift);
951 /* Invalidate another BufferRAM */
952 i = ONENAND_NEXT_BUFFERRAM(this);
953 if (this->bufferram[i].blockpage == blockpage)
954 this->bufferram[i].blockpage = -1;
956 /* Update BufferRAM */
957 i = ONENAND_CURRENT_BUFFERRAM(this);
959 this->bufferram[i].blockpage = blockpage;
961 this->bufferram[i].blockpage = -1;
965 * onenand_invalidate_bufferram - [GENERIC] Invalidate BufferRAM information
966 * @param mtd MTD data structure
967 * @param addr start address to invalidate
968 * @param len length to invalidate
970 * Invalidate BufferRAM information
972 static void onenand_invalidate_bufferram(struct mtd_info *mtd, loff_t addr,
975 struct onenand_chip *this = mtd->priv;
977 loff_t end_addr = addr + len;
979 /* Invalidate BufferRAM */
980 for (i = 0; i < MAX_BUFFERRAM; i++) {
981 loff_t buf_addr = this->bufferram[i].blockpage << this->page_shift;
982 if (buf_addr >= addr && buf_addr < end_addr)
983 this->bufferram[i].blockpage = -1;
988 * onenand_get_device - [GENERIC] Get chip for selected access
989 * @param mtd MTD device structure
990 * @param new_state the state which is requested
992 * Get the device and lock it for exclusive access
994 static int onenand_get_device(struct mtd_info *mtd, int new_state)
996 struct onenand_chip *this = mtd->priv;
997 DECLARE_WAITQUEUE(wait, current);
1000 * Grab the lock and see if the device is available
1003 spin_lock(&this->chip_lock);
1004 if (this->state == FL_READY) {
1005 this->state = new_state;
1006 spin_unlock(&this->chip_lock);
1007 if (new_state != FL_PM_SUSPENDED && this->enable)
1011 if (new_state == FL_PM_SUSPENDED) {
1012 spin_unlock(&this->chip_lock);
1013 return (this->state == FL_PM_SUSPENDED) ? 0 : -EAGAIN;
1015 set_current_state(TASK_UNINTERRUPTIBLE);
1016 add_wait_queue(&this->wq, &wait);
1017 spin_unlock(&this->chip_lock);
1019 remove_wait_queue(&this->wq, &wait);
1026 * onenand_release_device - [GENERIC] release chip
1027 * @param mtd MTD device structure
1029 * Deselect, release chip lock and wake up anyone waiting on the device
1031 static void onenand_release_device(struct mtd_info *mtd)
1033 struct onenand_chip *this = mtd->priv;
1035 if (this->state != FL_PM_SUSPENDED && this->disable)
1037 /* Release the chip */
1038 spin_lock(&this->chip_lock);
1039 this->state = FL_READY;
1041 spin_unlock(&this->chip_lock);
1045 * onenand_transfer_auto_oob - [INTERN] oob auto-placement transfer
1046 * @param mtd MTD device structure
1047 * @param buf destination address
1048 * @param column oob offset to read from
1049 * @param thislen oob length to read
1051 static int onenand_transfer_auto_oob(struct mtd_info *mtd, uint8_t *buf, int column,
1054 struct onenand_chip *this = mtd->priv;
1056 this->read_bufferram(mtd, ONENAND_SPARERAM, this->oob_buf, 0,
1058 return mtd_ooblayout_get_databytes(mtd, buf, this->oob_buf,
1063 * onenand_recover_lsb - [Flex-OneNAND] Recover LSB page data
1064 * @param mtd MTD device structure
1065 * @param addr address to recover
1066 * @param status return value from onenand_wait / onenand_bbt_wait
1068 * MLC NAND Flash cell has paired pages - LSB page and MSB page. LSB page has
1069 * lower page address and MSB page has higher page address in paired pages.
1070 * If power off occurs during MSB page program, the paired LSB page data can
1071 * become corrupt. LSB page recovery read is a way to read LSB page though page
1072 * data are corrupted. When uncorrectable error occurs as a result of LSB page
1073 * read after power up, issue LSB page recovery read.
1075 static int onenand_recover_lsb(struct mtd_info *mtd, loff_t addr, int status)
1077 struct onenand_chip *this = mtd->priv;
1080 /* Recovery is only for Flex-OneNAND */
1081 if (!FLEXONENAND(this))
1084 /* check if we failed due to uncorrectable error */
1085 if (!mtd_is_eccerr(status) && status != ONENAND_BBT_READ_ECC_ERROR)
1088 /* check if address lies in MLC region */
1089 i = flexonenand_region(mtd, addr);
1090 if (mtd->eraseregions[i].erasesize < (1 << this->erase_shift))
1093 /* We are attempting to reread, so decrement stats.failed
1094 * which was incremented by onenand_wait due to read failure
1096 printk(KERN_INFO "%s: Attempting to recover from uncorrectable read\n",
1098 mtd->ecc_stats.failed--;
1100 /* Issue the LSB page recovery command */
1101 this->command(mtd, FLEXONENAND_CMD_RECOVER_LSB, addr, this->writesize);
1102 return this->wait(mtd, FL_READING);
1106 * onenand_mlc_read_ops_nolock - MLC OneNAND read main and/or out-of-band
1107 * @param mtd MTD device structure
1108 * @param from offset to read from
1109 * @param ops: oob operation description structure
1111 * MLC OneNAND / Flex-OneNAND has 4KB page size and 4KB dataram.
1112 * So, read-while-load is not present.
1114 static int onenand_mlc_read_ops_nolock(struct mtd_info *mtd, loff_t from,
1115 struct mtd_oob_ops *ops)
1117 struct onenand_chip *this = mtd->priv;
1118 struct mtd_ecc_stats stats;
1119 size_t len = ops->len;
1120 size_t ooblen = ops->ooblen;
1121 u_char *buf = ops->datbuf;
1122 u_char *oobbuf = ops->oobbuf;
1123 int read = 0, column, thislen;
1124 int oobread = 0, oobcolumn, thisooblen, oobsize;
1126 int writesize = this->writesize;
1128 pr_debug("%s: from = 0x%08x, len = %i\n", __func__, (unsigned int)from,
1131 oobsize = mtd_oobavail(mtd, ops);
1132 oobcolumn = from & (mtd->oobsize - 1);
1134 /* Do not allow reads past end of device */
1135 if (from + len > mtd->size) {
1136 printk(KERN_ERR "%s: Attempt read beyond end of device\n",
1143 stats = mtd->ecc_stats;
1145 while (read < len) {
1148 thislen = min_t(int, writesize, len - read);
1150 column = from & (writesize - 1);
1151 if (column + thislen > writesize)
1152 thislen = writesize - column;
1154 if (!onenand_check_bufferram(mtd, from)) {
1155 this->command(mtd, ONENAND_CMD_READ, from, writesize);
1157 ret = this->wait(mtd, FL_READING);
1159 ret = onenand_recover_lsb(mtd, from, ret);
1160 onenand_update_bufferram(mtd, from, !ret);
1161 if (mtd_is_eccerr(ret))
1167 this->read_bufferram(mtd, ONENAND_DATARAM, buf, column, thislen);
1169 thisooblen = oobsize - oobcolumn;
1170 thisooblen = min_t(int, thisooblen, ooblen - oobread);
1172 if (ops->mode == MTD_OPS_AUTO_OOB)
1173 onenand_transfer_auto_oob(mtd, oobbuf, oobcolumn, thisooblen);
1175 this->read_bufferram(mtd, ONENAND_SPARERAM, oobbuf, oobcolumn, thisooblen);
1176 oobread += thisooblen;
1177 oobbuf += thisooblen;
1190 * Return success, if no ECC failures, else -EBADMSG
1191 * fs driver will take care of that, because
1192 * retlen == desired len and result == -EBADMSG
1195 ops->oobretlen = oobread;
1200 if (mtd->ecc_stats.failed - stats.failed)
1203 /* return max bitflips per ecc step; ONENANDs correct 1 bit only */
1204 return mtd->ecc_stats.corrected != stats.corrected ? 1 : 0;
1208 * onenand_read_ops_nolock - [OneNAND Interface] OneNAND read main and/or out-of-band
1209 * @param mtd MTD device structure
1210 * @param from offset to read from
1211 * @param ops: oob operation description structure
1213 * OneNAND read main and/or out-of-band data
1215 static int onenand_read_ops_nolock(struct mtd_info *mtd, loff_t from,
1216 struct mtd_oob_ops *ops)
1218 struct onenand_chip *this = mtd->priv;
1219 struct mtd_ecc_stats stats;
1220 size_t len = ops->len;
1221 size_t ooblen = ops->ooblen;
1222 u_char *buf = ops->datbuf;
1223 u_char *oobbuf = ops->oobbuf;
1224 int read = 0, column, thislen;
1225 int oobread = 0, oobcolumn, thisooblen, oobsize;
1226 int ret = 0, boundary = 0;
1227 int writesize = this->writesize;
1229 pr_debug("%s: from = 0x%08x, len = %i\n", __func__, (unsigned int)from,
1232 oobsize = mtd_oobavail(mtd, ops);
1233 oobcolumn = from & (mtd->oobsize - 1);
1235 /* Do not allow reads past end of device */
1236 if ((from + len) > mtd->size) {
1237 printk(KERN_ERR "%s: Attempt read beyond end of device\n",
1244 stats = mtd->ecc_stats;
1246 /* Read-while-load method */
1248 /* Do first load to bufferRAM */
1250 if (!onenand_check_bufferram(mtd, from)) {
1251 this->command(mtd, ONENAND_CMD_READ, from, writesize);
1252 ret = this->wait(mtd, FL_READING);
1253 onenand_update_bufferram(mtd, from, !ret);
1254 if (mtd_is_eccerr(ret))
1259 thislen = min_t(int, writesize, len - read);
1260 column = from & (writesize - 1);
1261 if (column + thislen > writesize)
1262 thislen = writesize - column;
1265 /* If there is more to load then start next load */
1267 if (read + thislen < len) {
1268 this->command(mtd, ONENAND_CMD_READ, from, writesize);
1270 * Chip boundary handling in DDP
1271 * Now we issued chip 1 read and pointed chip 1
1272 * bufferram so we have to point chip 0 bufferram.
1274 if (ONENAND_IS_DDP(this) &&
1275 unlikely(from == (this->chipsize >> 1))) {
1276 this->write_word(ONENAND_DDP_CHIP0, this->base + ONENAND_REG_START_ADDRESS2);
1280 ONENAND_SET_PREV_BUFFERRAM(this);
1282 /* While load is going, read from last bufferRAM */
1283 this->read_bufferram(mtd, ONENAND_DATARAM, buf, column, thislen);
1285 /* Read oob area if needed */
1287 thisooblen = oobsize - oobcolumn;
1288 thisooblen = min_t(int, thisooblen, ooblen - oobread);
1290 if (ops->mode == MTD_OPS_AUTO_OOB)
1291 onenand_transfer_auto_oob(mtd, oobbuf, oobcolumn, thisooblen);
1293 this->read_bufferram(mtd, ONENAND_SPARERAM, oobbuf, oobcolumn, thisooblen);
1294 oobread += thisooblen;
1295 oobbuf += thisooblen;
1299 /* See if we are done */
1303 /* Set up for next read from bufferRAM */
1304 if (unlikely(boundary))
1305 this->write_word(ONENAND_DDP_CHIP1, this->base + ONENAND_REG_START_ADDRESS2);
1306 ONENAND_SET_NEXT_BUFFERRAM(this);
1308 thislen = min_t(int, writesize, len - read);
1311 /* Now wait for load */
1312 ret = this->wait(mtd, FL_READING);
1313 onenand_update_bufferram(mtd, from, !ret);
1314 if (mtd_is_eccerr(ret))
1319 * Return success, if no ECC failures, else -EBADMSG
1320 * fs driver will take care of that, because
1321 * retlen == desired len and result == -EBADMSG
1324 ops->oobretlen = oobread;
1329 if (mtd->ecc_stats.failed - stats.failed)
1332 /* return max bitflips per ecc step; ONENANDs correct 1 bit only */
1333 return mtd->ecc_stats.corrected != stats.corrected ? 1 : 0;
1337 * onenand_read_oob_nolock - [MTD Interface] OneNAND read out-of-band
1338 * @param mtd MTD device structure
1339 * @param from offset to read from
1340 * @param ops: oob operation description structure
1342 * OneNAND read out-of-band data from the spare area
1344 static int onenand_read_oob_nolock(struct mtd_info *mtd, loff_t from,
1345 struct mtd_oob_ops *ops)
1347 struct onenand_chip *this = mtd->priv;
1348 struct mtd_ecc_stats stats;
1349 int read = 0, thislen, column, oobsize;
1350 size_t len = ops->ooblen;
1351 unsigned int mode = ops->mode;
1352 u_char *buf = ops->oobbuf;
1353 int ret = 0, readcmd;
1355 from += ops->ooboffs;
1357 pr_debug("%s: from = 0x%08x, len = %i\n", __func__, (unsigned int)from,
1360 /* Initialize return length value */
1363 if (mode == MTD_OPS_AUTO_OOB)
1364 oobsize = mtd->oobavail;
1366 oobsize = mtd->oobsize;
1368 column = from & (mtd->oobsize - 1);
1370 if (unlikely(column >= oobsize)) {
1371 printk(KERN_ERR "%s: Attempted to start read outside oob\n",
1376 stats = mtd->ecc_stats;
1378 readcmd = ONENAND_IS_4KB_PAGE(this) ? ONENAND_CMD_READ : ONENAND_CMD_READOOB;
1380 while (read < len) {
1383 thislen = oobsize - column;
1384 thislen = min_t(int, thislen, len);
1386 this->command(mtd, readcmd, from, mtd->oobsize);
1388 onenand_update_bufferram(mtd, from, 0);
1390 ret = this->wait(mtd, FL_READING);
1392 ret = onenand_recover_lsb(mtd, from, ret);
1394 if (ret && !mtd_is_eccerr(ret)) {
1395 printk(KERN_ERR "%s: read failed = 0x%x\n",
1400 if (mode == MTD_OPS_AUTO_OOB)
1401 onenand_transfer_auto_oob(mtd, buf, column, thislen);
1403 this->read_bufferram(mtd, ONENAND_SPARERAM, buf, column, thislen);
1415 from += mtd->writesize;
1420 ops->oobretlen = read;
1425 if (mtd->ecc_stats.failed - stats.failed)
1432 * onenand_read_oob - [MTD Interface] Read main and/or out-of-band
1433 * @param mtd: MTD device structure
1434 * @param from: offset to read from
1435 * @param ops: oob operation description structure
1437 * Read main and/or out-of-band
1439 static int onenand_read_oob(struct mtd_info *mtd, loff_t from,
1440 struct mtd_oob_ops *ops)
1442 struct onenand_chip *this = mtd->priv;
1445 switch (ops->mode) {
1446 case MTD_OPS_PLACE_OOB:
1447 case MTD_OPS_AUTO_OOB:
1450 /* Not implemented yet */
1455 onenand_get_device(mtd, FL_READING);
1457 ret = ONENAND_IS_4KB_PAGE(this) ?
1458 onenand_mlc_read_ops_nolock(mtd, from, ops) :
1459 onenand_read_ops_nolock(mtd, from, ops);
1461 ret = onenand_read_oob_nolock(mtd, from, ops);
1462 onenand_release_device(mtd);
1468 * onenand_bbt_wait - [DEFAULT] wait until the command is done
1469 * @param mtd MTD device structure
1470 * @param state state to select the max. timeout value
1472 * Wait for command done.
1474 static int onenand_bbt_wait(struct mtd_info *mtd, int state)
1476 struct onenand_chip *this = mtd->priv;
1477 unsigned long timeout;
1478 unsigned int interrupt, ctrl, ecc, addr1, addr8;
1480 /* The 20 msec is enough */
1481 timeout = jiffies + msecs_to_jiffies(20);
1482 while (time_before(jiffies, timeout)) {
1483 interrupt = this->read_word(this->base + ONENAND_REG_INTERRUPT);
1484 if (interrupt & ONENAND_INT_MASTER)
1487 /* To get correct interrupt status in timeout case */
1488 interrupt = this->read_word(this->base + ONENAND_REG_INTERRUPT);
1489 ctrl = this->read_word(this->base + ONENAND_REG_CTRL_STATUS);
1490 addr1 = this->read_word(this->base + ONENAND_REG_START_ADDRESS1);
1491 addr8 = this->read_word(this->base + ONENAND_REG_START_ADDRESS8);
1493 if (interrupt & ONENAND_INT_READ) {
1494 ecc = onenand_read_ecc(this);
1495 if (ecc & ONENAND_ECC_2BIT_ALL) {
1496 printk(KERN_DEBUG "%s: ecc 0x%04x ctrl 0x%04x "
1497 "intr 0x%04x addr1 %#x addr8 %#x\n",
1498 __func__, ecc, ctrl, interrupt, addr1, addr8);
1499 return ONENAND_BBT_READ_ECC_ERROR;
1502 printk(KERN_ERR "%s: read timeout! ctrl 0x%04x "
1503 "intr 0x%04x addr1 %#x addr8 %#x\n",
1504 __func__, ctrl, interrupt, addr1, addr8);
1505 return ONENAND_BBT_READ_FATAL_ERROR;
1508 /* Initial bad block case: 0x2400 or 0x0400 */
1509 if (ctrl & ONENAND_CTRL_ERROR) {
1510 printk(KERN_DEBUG "%s: ctrl 0x%04x intr 0x%04x addr1 %#x "
1511 "addr8 %#x\n", __func__, ctrl, interrupt, addr1, addr8);
1512 return ONENAND_BBT_READ_ERROR;
1519 * onenand_bbt_read_oob - [MTD Interface] OneNAND read out-of-band for bbt scan
1520 * @param mtd MTD device structure
1521 * @param from offset to read from
1522 * @param ops oob operation description structure
1524 * OneNAND read out-of-band data from the spare area for bbt scan
1526 int onenand_bbt_read_oob(struct mtd_info *mtd, loff_t from,
1527 struct mtd_oob_ops *ops)
1529 struct onenand_chip *this = mtd->priv;
1530 int read = 0, thislen, column;
1531 int ret = 0, readcmd;
1532 size_t len = ops->ooblen;
1533 u_char *buf = ops->oobbuf;
1535 pr_debug("%s: from = 0x%08x, len = %zi\n", __func__, (unsigned int)from,
1538 /* Initialize return value */
1541 /* Do not allow reads past end of device */
1542 if (unlikely((from + len) > mtd->size)) {
1543 printk(KERN_ERR "%s: Attempt read beyond end of device\n",
1545 return ONENAND_BBT_READ_FATAL_ERROR;
1548 /* Grab the lock and see if the device is available */
1549 onenand_get_device(mtd, FL_READING);
1551 column = from & (mtd->oobsize - 1);
1553 readcmd = ONENAND_IS_4KB_PAGE(this) ? ONENAND_CMD_READ : ONENAND_CMD_READOOB;
1555 while (read < len) {
1558 thislen = mtd->oobsize - column;
1559 thislen = min_t(int, thislen, len);
1561 this->command(mtd, readcmd, from, mtd->oobsize);
1563 onenand_update_bufferram(mtd, from, 0);
1565 ret = this->bbt_wait(mtd, FL_READING);
1567 ret = onenand_recover_lsb(mtd, from, ret);
1572 this->read_bufferram(mtd, ONENAND_SPARERAM, buf, column, thislen);
1581 /* Update Page size */
1582 from += this->writesize;
1587 /* Deselect and wake up anyone waiting on the device */
1588 onenand_release_device(mtd);
1590 ops->oobretlen = read;
1594 #ifdef CONFIG_MTD_ONENAND_VERIFY_WRITE
1596 * onenand_verify_oob - [GENERIC] verify the oob contents after a write
1597 * @param mtd MTD device structure
1598 * @param buf the databuffer to verify
1599 * @param to offset to read from
1601 static int onenand_verify_oob(struct mtd_info *mtd, const u_char *buf, loff_t to)
1603 struct onenand_chip *this = mtd->priv;
1604 u_char *oob_buf = this->oob_buf;
1605 int status, i, readcmd;
1607 readcmd = ONENAND_IS_4KB_PAGE(this) ? ONENAND_CMD_READ : ONENAND_CMD_READOOB;
1609 this->command(mtd, readcmd, to, mtd->oobsize);
1610 onenand_update_bufferram(mtd, to, 0);
1611 status = this->wait(mtd, FL_READING);
1615 this->read_bufferram(mtd, ONENAND_SPARERAM, oob_buf, 0, mtd->oobsize);
1616 for (i = 0; i < mtd->oobsize; i++)
1617 if (buf[i] != 0xFF && buf[i] != oob_buf[i])
1624 * onenand_verify - [GENERIC] verify the chip contents after a write
1625 * @param mtd MTD device structure
1626 * @param buf the databuffer to verify
1627 * @param addr offset to read from
1628 * @param len number of bytes to read and compare
1630 static int onenand_verify(struct mtd_info *mtd, const u_char *buf, loff_t addr, size_t len)
1632 struct onenand_chip *this = mtd->priv;
1634 int thislen, column;
1636 column = addr & (this->writesize - 1);
1639 thislen = min_t(int, this->writesize - column, len);
1641 this->command(mtd, ONENAND_CMD_READ, addr, this->writesize);
1643 onenand_update_bufferram(mtd, addr, 0);
1645 ret = this->wait(mtd, FL_READING);
1649 onenand_update_bufferram(mtd, addr, 1);
1651 this->read_bufferram(mtd, ONENAND_DATARAM, this->verify_buf, 0, mtd->writesize);
1653 if (memcmp(buf, this->verify_buf + column, thislen))
1665 #define onenand_verify(...) (0)
1666 #define onenand_verify_oob(...) (0)
1669 #define NOTALIGNED(x) ((x & (this->subpagesize - 1)) != 0)
1671 static void onenand_panic_wait(struct mtd_info *mtd)
1673 struct onenand_chip *this = mtd->priv;
1674 unsigned int interrupt;
1677 for (i = 0; i < 2000; i++) {
1678 interrupt = this->read_word(this->base + ONENAND_REG_INTERRUPT);
1679 if (interrupt & ONENAND_INT_MASTER)
1686 * onenand_panic_write - [MTD Interface] write buffer to FLASH in a panic context
1687 * @param mtd MTD device structure
1688 * @param to offset to write to
1689 * @param len number of bytes to write
1690 * @param retlen pointer to variable to store the number of written bytes
1691 * @param buf the data to write
1695 static int onenand_panic_write(struct mtd_info *mtd, loff_t to, size_t len,
1696 size_t *retlen, const u_char *buf)
1698 struct onenand_chip *this = mtd->priv;
1699 int column, subpage;
1702 if (this->state == FL_PM_SUSPENDED)
1705 /* Wait for any existing operation to clear */
1706 onenand_panic_wait(mtd);
1708 pr_debug("%s: to = 0x%08x, len = %i\n", __func__, (unsigned int)to,
1711 /* Reject writes, which are not page aligned */
1712 if (unlikely(NOTALIGNED(to) || NOTALIGNED(len))) {
1713 printk(KERN_ERR "%s: Attempt to write not page aligned data\n",
1718 column = to & (mtd->writesize - 1);
1720 /* Loop until all data write */
1721 while (written < len) {
1722 int thislen = min_t(int, mtd->writesize - column, len - written);
1723 u_char *wbuf = (u_char *) buf;
1725 this->command(mtd, ONENAND_CMD_BUFFERRAM, to, thislen);
1727 /* Partial page write */
1728 subpage = thislen < mtd->writesize;
1730 memset(this->page_buf, 0xff, mtd->writesize);
1731 memcpy(this->page_buf + column, buf, thislen);
1732 wbuf = this->page_buf;
1735 this->write_bufferram(mtd, ONENAND_DATARAM, wbuf, 0, mtd->writesize);
1736 this->write_bufferram(mtd, ONENAND_SPARERAM, ffchars, 0, mtd->oobsize);
1738 this->command(mtd, ONENAND_CMD_PROG, to, mtd->writesize);
1740 onenand_panic_wait(mtd);
1742 /* In partial page write we don't update bufferram */
1743 onenand_update_bufferram(mtd, to, !subpage);
1744 if (ONENAND_IS_2PLANE(this)) {
1745 ONENAND_SET_BUFFERRAM1(this);
1746 onenand_update_bufferram(mtd, to + this->writesize, !subpage);
1764 * onenand_fill_auto_oob - [INTERN] oob auto-placement transfer
1765 * @param mtd MTD device structure
1766 * @param oob_buf oob buffer
1767 * @param buf source address
1768 * @param column oob offset to write to
1769 * @param thislen oob length to write
1771 static int onenand_fill_auto_oob(struct mtd_info *mtd, u_char *oob_buf,
1772 const u_char *buf, int column, int thislen)
1774 return mtd_ooblayout_set_databytes(mtd, buf, oob_buf, column, thislen);
1778 * onenand_write_ops_nolock - [OneNAND Interface] write main and/or out-of-band
1779 * @param mtd MTD device structure
1780 * @param to offset to write to
1781 * @param ops oob operation description structure
1783 * Write main and/or oob with ECC
1785 static int onenand_write_ops_nolock(struct mtd_info *mtd, loff_t to,
1786 struct mtd_oob_ops *ops)
1788 struct onenand_chip *this = mtd->priv;
1789 int written = 0, column, thislen = 0, subpage = 0;
1790 int prev = 0, prevlen = 0, prev_subpage = 0, first = 1;
1791 int oobwritten = 0, oobcolumn, thisooblen, oobsize;
1792 size_t len = ops->len;
1793 size_t ooblen = ops->ooblen;
1794 const u_char *buf = ops->datbuf;
1795 const u_char *oob = ops->oobbuf;
1799 pr_debug("%s: to = 0x%08x, len = %i\n", __func__, (unsigned int)to,
1802 /* Initialize retlen, in case of early exit */
1806 /* Reject writes, which are not page aligned */
1807 if (unlikely(NOTALIGNED(to) || NOTALIGNED(len))) {
1808 printk(KERN_ERR "%s: Attempt to write not page aligned data\n",
1813 /* Check zero length */
1816 oobsize = mtd_oobavail(mtd, ops);
1817 oobcolumn = to & (mtd->oobsize - 1);
1819 column = to & (mtd->writesize - 1);
1821 /* Loop until all data write */
1823 if (written < len) {
1824 u_char *wbuf = (u_char *) buf;
1826 thislen = min_t(int, mtd->writesize - column, len - written);
1827 thisooblen = min_t(int, oobsize - oobcolumn, ooblen - oobwritten);
1831 this->command(mtd, ONENAND_CMD_BUFFERRAM, to, thislen);
1833 /* Partial page write */
1834 subpage = thislen < mtd->writesize;
1836 memset(this->page_buf, 0xff, mtd->writesize);
1837 memcpy(this->page_buf + column, buf, thislen);
1838 wbuf = this->page_buf;
1841 this->write_bufferram(mtd, ONENAND_DATARAM, wbuf, 0, mtd->writesize);
1844 oobbuf = this->oob_buf;
1846 /* We send data to spare ram with oobsize
1847 * to prevent byte access */
1848 memset(oobbuf, 0xff, mtd->oobsize);
1849 if (ops->mode == MTD_OPS_AUTO_OOB)
1850 onenand_fill_auto_oob(mtd, oobbuf, oob, oobcolumn, thisooblen);
1852 memcpy(oobbuf + oobcolumn, oob, thisooblen);
1854 oobwritten += thisooblen;
1858 oobbuf = (u_char *) ffchars;
1860 this->write_bufferram(mtd, ONENAND_SPARERAM, oobbuf, 0, mtd->oobsize);
1862 ONENAND_SET_NEXT_BUFFERRAM(this);
1865 * 2 PLANE, MLC, and Flex-OneNAND do not support
1866 * write-while-program feature.
1868 if (!ONENAND_IS_2PLANE(this) && !ONENAND_IS_4KB_PAGE(this) && !first) {
1869 ONENAND_SET_PREV_BUFFERRAM(this);
1871 ret = this->wait(mtd, FL_WRITING);
1873 /* In partial page write we don't update bufferram */
1874 onenand_update_bufferram(mtd, prev, !ret && !prev_subpage);
1877 printk(KERN_ERR "%s: write failed %d\n",
1882 if (written == len) {
1883 /* Only check verify write turn on */
1884 ret = onenand_verify(mtd, buf - len, to - len, len);
1886 printk(KERN_ERR "%s: verify failed %d\n",
1891 ONENAND_SET_NEXT_BUFFERRAM(this);
1895 cmd = ONENAND_CMD_PROG;
1897 /* Exclude 1st OTP and OTP blocks for cache program feature */
1898 if (ONENAND_IS_CACHE_PROGRAM(this) &&
1899 likely(onenand_block(this, to) != 0) &&
1900 ONENAND_IS_4KB_PAGE(this) &&
1901 ((written + thislen) < len)) {
1902 cmd = ONENAND_CMD_2X_CACHE_PROG;
1906 this->command(mtd, cmd, to, mtd->writesize);
1909 * 2 PLANE, MLC, and Flex-OneNAND wait here
1911 if (ONENAND_IS_2PLANE(this) || ONENAND_IS_4KB_PAGE(this)) {
1912 ret = this->wait(mtd, FL_WRITING);
1914 /* In partial page write we don't update bufferram */
1915 onenand_update_bufferram(mtd, to, !ret && !subpage);
1917 printk(KERN_ERR "%s: write failed %d\n",
1922 /* Only check verify write turn on */
1923 ret = onenand_verify(mtd, buf, to, thislen);
1925 printk(KERN_ERR "%s: verify failed %d\n",
1939 prev_subpage = subpage;
1947 /* In error case, clear all bufferrams */
1949 onenand_invalidate_bufferram(mtd, 0, -1);
1951 ops->retlen = written;
1952 ops->oobretlen = oobwritten;
1959 * onenand_write_oob_nolock - [INTERN] OneNAND write out-of-band
1960 * @param mtd MTD device structure
1961 * @param to offset to write to
1962 * @param len number of bytes to write
1963 * @param retlen pointer to variable to store the number of written bytes
1964 * @param buf the data to write
1965 * @param mode operation mode
1967 * OneNAND write out-of-band
1969 static int onenand_write_oob_nolock(struct mtd_info *mtd, loff_t to,
1970 struct mtd_oob_ops *ops)
1972 struct onenand_chip *this = mtd->priv;
1973 int column, ret = 0, oobsize;
1974 int written = 0, oobcmd;
1976 size_t len = ops->ooblen;
1977 const u_char *buf = ops->oobbuf;
1978 unsigned int mode = ops->mode;
1982 pr_debug("%s: to = 0x%08x, len = %i\n", __func__, (unsigned int)to,
1985 /* Initialize retlen, in case of early exit */
1988 if (mode == MTD_OPS_AUTO_OOB)
1989 oobsize = mtd->oobavail;
1991 oobsize = mtd->oobsize;
1993 column = to & (mtd->oobsize - 1);
1995 if (unlikely(column >= oobsize)) {
1996 printk(KERN_ERR "%s: Attempted to start write outside oob\n",
2001 /* For compatibility with NAND: Do not allow write past end of page */
2002 if (unlikely(column + len > oobsize)) {
2003 printk(KERN_ERR "%s: Attempt to write past end of page\n",
2008 oobbuf = this->oob_buf;
2010 oobcmd = ONENAND_IS_4KB_PAGE(this) ? ONENAND_CMD_PROG : ONENAND_CMD_PROGOOB;
2012 /* Loop until all data write */
2013 while (written < len) {
2014 int thislen = min_t(int, oobsize, len - written);
2018 this->command(mtd, ONENAND_CMD_BUFFERRAM, to, mtd->oobsize);
2020 /* We send data to spare ram with oobsize
2021 * to prevent byte access */
2022 memset(oobbuf, 0xff, mtd->oobsize);
2023 if (mode == MTD_OPS_AUTO_OOB)
2024 onenand_fill_auto_oob(mtd, oobbuf, buf, column, thislen);
2026 memcpy(oobbuf + column, buf, thislen);
2027 this->write_bufferram(mtd, ONENAND_SPARERAM, oobbuf, 0, mtd->oobsize);
2029 if (ONENAND_IS_4KB_PAGE(this)) {
2030 /* Set main area of DataRAM to 0xff*/
2031 memset(this->page_buf, 0xff, mtd->writesize);
2032 this->write_bufferram(mtd, ONENAND_DATARAM,
2033 this->page_buf, 0, mtd->writesize);
2036 this->command(mtd, oobcmd, to, mtd->oobsize);
2038 onenand_update_bufferram(mtd, to, 0);
2039 if (ONENAND_IS_2PLANE(this)) {
2040 ONENAND_SET_BUFFERRAM1(this);
2041 onenand_update_bufferram(mtd, to + this->writesize, 0);
2044 ret = this->wait(mtd, FL_WRITING);
2046 printk(KERN_ERR "%s: write failed %d\n", __func__, ret);
2050 ret = onenand_verify_oob(mtd, oobbuf, to);
2052 printk(KERN_ERR "%s: verify failed %d\n",
2061 to += mtd->writesize;
2066 ops->oobretlen = written;
2072 * onenand_write_oob - [MTD Interface] NAND write data and/or out-of-band
2073 * @param mtd: MTD device structure
2074 * @param to: offset to write
2075 * @param ops: oob operation description structure
2077 static int onenand_write_oob(struct mtd_info *mtd, loff_t to,
2078 struct mtd_oob_ops *ops)
2082 switch (ops->mode) {
2083 case MTD_OPS_PLACE_OOB:
2084 case MTD_OPS_AUTO_OOB:
2087 /* Not implemented yet */
2092 onenand_get_device(mtd, FL_WRITING);
2094 ret = onenand_write_ops_nolock(mtd, to, ops);
2096 ret = onenand_write_oob_nolock(mtd, to, ops);
2097 onenand_release_device(mtd);
2103 * onenand_block_isbad_nolock - [GENERIC] Check if a block is marked bad
2104 * @param mtd MTD device structure
2105 * @param ofs offset from device start
2106 * @param allowbbt 1, if its allowed to access the bbt area
2108 * Check, if the block is bad. Either by reading the bad block table or
2109 * calling of the scan function.
2111 static int onenand_block_isbad_nolock(struct mtd_info *mtd, loff_t ofs, int allowbbt)
2113 struct onenand_chip *this = mtd->priv;
2114 struct bbm_info *bbm = this->bbm;
2116 /* Return info from the table */
2117 return bbm->isbad_bbt(mtd, ofs, allowbbt);
2121 static int onenand_multiblock_erase_verify(struct mtd_info *mtd,
2122 struct erase_info *instr)
2124 struct onenand_chip *this = mtd->priv;
2125 loff_t addr = instr->addr;
2126 int len = instr->len;
2127 unsigned int block_size = (1 << this->erase_shift);
2131 this->command(mtd, ONENAND_CMD_ERASE_VERIFY, addr, block_size);
2132 ret = this->wait(mtd, FL_VERIFYING_ERASE);
2134 printk(KERN_ERR "%s: Failed verify, block %d\n",
2135 __func__, onenand_block(this, addr));
2136 instr->fail_addr = addr;
2146 * onenand_multiblock_erase - [INTERN] erase block(s) using multiblock erase
2147 * @param mtd MTD device structure
2148 * @param instr erase instruction
2149 * @param region erase region
2151 * Erase one or more blocks up to 64 block at a time
2153 static int onenand_multiblock_erase(struct mtd_info *mtd,
2154 struct erase_info *instr,
2155 unsigned int block_size)
2157 struct onenand_chip *this = mtd->priv;
2158 loff_t addr = instr->addr;
2159 int len = instr->len;
2164 if (ONENAND_IS_DDP(this)) {
2165 loff_t bdry_addr = this->chipsize >> 1;
2166 if (addr < bdry_addr && (addr + len) > bdry_addr)
2167 bdry_block = bdry_addr >> this->erase_shift;
2172 /* Check if we have a bad block, we do not erase bad blocks */
2173 if (onenand_block_isbad_nolock(mtd, addr, 0)) {
2174 printk(KERN_WARNING "%s: attempt to erase a bad block "
2175 "at addr 0x%012llx\n",
2176 __func__, (unsigned long long) addr);
2186 /* loop over 64 eb batches */
2188 struct erase_info verify_instr = *instr;
2189 int max_eb_count = MB_ERASE_MAX_BLK_COUNT;
2191 verify_instr.addr = addr;
2192 verify_instr.len = 0;
2194 /* do not cross chip boundary */
2196 int this_block = (addr >> this->erase_shift);
2198 if (this_block < bdry_block) {
2199 max_eb_count = min(max_eb_count,
2200 (bdry_block - this_block));
2206 while (len > block_size && eb_count < (max_eb_count - 1)) {
2207 this->command(mtd, ONENAND_CMD_MULTIBLOCK_ERASE,
2209 onenand_invalidate_bufferram(mtd, addr, block_size);
2211 ret = this->wait(mtd, FL_PREPARING_ERASE);
2213 printk(KERN_ERR "%s: Failed multiblock erase, "
2214 "block %d\n", __func__,
2215 onenand_block(this, addr));
2216 instr->fail_addr = MTD_FAIL_ADDR_UNKNOWN;
2225 /* last block of 64-eb series */
2227 this->command(mtd, ONENAND_CMD_ERASE, addr, block_size);
2228 onenand_invalidate_bufferram(mtd, addr, block_size);
2230 ret = this->wait(mtd, FL_ERASING);
2231 /* Check if it is write protected */
2233 printk(KERN_ERR "%s: Failed erase, block %d\n",
2234 __func__, onenand_block(this, addr));
2235 instr->fail_addr = MTD_FAIL_ADDR_UNKNOWN;
2244 verify_instr.len = eb_count * block_size;
2245 if (onenand_multiblock_erase_verify(mtd, &verify_instr)) {
2246 instr->fail_addr = verify_instr.fail_addr;
2256 * onenand_block_by_block_erase - [INTERN] erase block(s) using regular erase
2257 * @param mtd MTD device structure
2258 * @param instr erase instruction
2259 * @param region erase region
2260 * @param block_size erase block size
2262 * Erase one or more blocks one block at a time
2264 static int onenand_block_by_block_erase(struct mtd_info *mtd,
2265 struct erase_info *instr,
2266 struct mtd_erase_region_info *region,
2267 unsigned int block_size)
2269 struct onenand_chip *this = mtd->priv;
2270 loff_t addr = instr->addr;
2271 int len = instr->len;
2272 loff_t region_end = 0;
2276 /* region is set for Flex-OneNAND */
2277 region_end = region->offset + region->erasesize * region->numblocks;
2280 /* Loop through the blocks */
2284 /* Check if we have a bad block, we do not erase bad blocks */
2285 if (onenand_block_isbad_nolock(mtd, addr, 0)) {
2286 printk(KERN_WARNING "%s: attempt to erase a bad block "
2287 "at addr 0x%012llx\n",
2288 __func__, (unsigned long long) addr);
2292 this->command(mtd, ONENAND_CMD_ERASE, addr, block_size);
2294 onenand_invalidate_bufferram(mtd, addr, block_size);
2296 ret = this->wait(mtd, FL_ERASING);
2297 /* Check, if it is write protected */
2299 printk(KERN_ERR "%s: Failed erase, block %d\n",
2300 __func__, onenand_block(this, addr));
2301 instr->fail_addr = addr;
2308 if (region && addr == region_end) {
2313 block_size = region->erasesize;
2314 region_end = region->offset + region->erasesize * region->numblocks;
2316 if (len & (block_size - 1)) {
2317 /* FIXME: This should be handled at MTD partitioning level. */
2318 printk(KERN_ERR "%s: Unaligned address\n",
2328 * onenand_erase - [MTD Interface] erase block(s)
2329 * @param mtd MTD device structure
2330 * @param instr erase instruction
2332 * Erase one or more blocks
2334 static int onenand_erase(struct mtd_info *mtd, struct erase_info *instr)
2336 struct onenand_chip *this = mtd->priv;
2337 unsigned int block_size;
2338 loff_t addr = instr->addr;
2339 loff_t len = instr->len;
2341 struct mtd_erase_region_info *region = NULL;
2342 loff_t region_offset = 0;
2344 pr_debug("%s: start=0x%012llx, len=%llu\n", __func__,
2345 (unsigned long long)instr->addr,
2346 (unsigned long long)instr->len);
2348 if (FLEXONENAND(this)) {
2349 /* Find the eraseregion of this address */
2350 int i = flexonenand_region(mtd, addr);
2352 region = &mtd->eraseregions[i];
2353 block_size = region->erasesize;
2355 /* Start address within region must align on block boundary.
2356 * Erase region's start offset is always block start address.
2358 region_offset = region->offset;
2360 block_size = 1 << this->erase_shift;
2362 /* Start address must align on block boundary */
2363 if (unlikely((addr - region_offset) & (block_size - 1))) {
2364 printk(KERN_ERR "%s: Unaligned address\n", __func__);
2368 /* Length must align on block boundary */
2369 if (unlikely(len & (block_size - 1))) {
2370 printk(KERN_ERR "%s: Length not block aligned\n", __func__);
2374 /* Grab the lock and see if the device is available */
2375 onenand_get_device(mtd, FL_ERASING);
2377 if (ONENAND_IS_4KB_PAGE(this) || region ||
2378 instr->len < MB_ERASE_MIN_BLK_COUNT * block_size) {
2379 /* region is set for Flex-OneNAND (no mb erase) */
2380 ret = onenand_block_by_block_erase(mtd, instr,
2381 region, block_size);
2383 ret = onenand_multiblock_erase(mtd, instr, block_size);
2386 /* Deselect and wake up anyone waiting on the device */
2387 onenand_release_device(mtd);
2393 * onenand_sync - [MTD Interface] sync
2394 * @param mtd MTD device structure
2396 * Sync is actually a wait for chip ready function
2398 static void onenand_sync(struct mtd_info *mtd)
2400 pr_debug("%s: called\n", __func__);
2402 /* Grab the lock and see if the device is available */
2403 onenand_get_device(mtd, FL_SYNCING);
2405 /* Release it and go back */
2406 onenand_release_device(mtd);
2410 * onenand_block_isbad - [MTD Interface] Check whether the block at the given offset is bad
2411 * @param mtd MTD device structure
2412 * @param ofs offset relative to mtd start
2414 * Check whether the block is bad
2416 static int onenand_block_isbad(struct mtd_info *mtd, loff_t ofs)
2420 onenand_get_device(mtd, FL_READING);
2421 ret = onenand_block_isbad_nolock(mtd, ofs, 0);
2422 onenand_release_device(mtd);
2427 * onenand_default_block_markbad - [DEFAULT] mark a block bad
2428 * @param mtd MTD device structure
2429 * @param ofs offset from device start
2431 * This is the default implementation, which can be overridden by
2432 * a hardware specific driver.
2434 static int onenand_default_block_markbad(struct mtd_info *mtd, loff_t ofs)
2436 struct onenand_chip *this = mtd->priv;
2437 struct bbm_info *bbm = this->bbm;
2438 u_char buf[2] = {0, 0};
2439 struct mtd_oob_ops ops = {
2440 .mode = MTD_OPS_PLACE_OOB,
2447 /* Get block number */
2448 block = onenand_block(this, ofs);
2450 bbm->bbt[block >> 2] |= 0x01 << ((block & 0x03) << 1);
2452 /* We write two bytes, so we don't have to mess with 16-bit access */
2453 ofs += mtd->oobsize + (this->badblockpos & ~0x01);
2454 /* FIXME : What to do when marking SLC block in partition
2455 * with MLC erasesize? For now, it is not advisable to
2456 * create partitions containing both SLC and MLC regions.
2458 return onenand_write_oob_nolock(mtd, ofs, &ops);
2462 * onenand_block_markbad - [MTD Interface] Mark the block at the given offset as bad
2463 * @param mtd MTD device structure
2464 * @param ofs offset relative to mtd start
2466 * Mark the block as bad
2468 static int onenand_block_markbad(struct mtd_info *mtd, loff_t ofs)
2470 struct onenand_chip *this = mtd->priv;
2473 ret = onenand_block_isbad(mtd, ofs);
2475 /* If it was bad already, return success and do nothing */
2481 onenand_get_device(mtd, FL_WRITING);
2482 ret = this->block_markbad(mtd, ofs);
2483 onenand_release_device(mtd);
2488 * onenand_do_lock_cmd - [OneNAND Interface] Lock or unlock block(s)
2489 * @param mtd MTD device structure
2490 * @param ofs offset relative to mtd start
2491 * @param len number of bytes to lock or unlock
2492 * @param cmd lock or unlock command
2494 * Lock or unlock one or more blocks
2496 static int onenand_do_lock_cmd(struct mtd_info *mtd, loff_t ofs, size_t len, int cmd)
2498 struct onenand_chip *this = mtd->priv;
2499 int start, end, block, value, status;
2502 start = onenand_block(this, ofs);
2503 end = onenand_block(this, ofs + len) - 1;
2505 if (cmd == ONENAND_CMD_LOCK)
2506 wp_status_mask = ONENAND_WP_LS;
2508 wp_status_mask = ONENAND_WP_US;
2510 /* Continuous lock scheme */
2511 if (this->options & ONENAND_HAS_CONT_LOCK) {
2512 /* Set start block address */
2513 this->write_word(start, this->base + ONENAND_REG_START_BLOCK_ADDRESS);
2514 /* Set end block address */
2515 this->write_word(end, this->base + ONENAND_REG_END_BLOCK_ADDRESS);
2516 /* Write lock command */
2517 this->command(mtd, cmd, 0, 0);
2519 /* There's no return value */
2520 this->wait(mtd, FL_LOCKING);
2523 while (this->read_word(this->base + ONENAND_REG_CTRL_STATUS)
2524 & ONENAND_CTRL_ONGO)
2527 /* Check lock status */
2528 status = this->read_word(this->base + ONENAND_REG_WP_STATUS);
2529 if (!(status & wp_status_mask))
2530 printk(KERN_ERR "%s: wp status = 0x%x\n",
2536 /* Block lock scheme */
2537 for (block = start; block < end + 1; block++) {
2538 /* Set block address */
2539 value = onenand_block_address(this, block);
2540 this->write_word(value, this->base + ONENAND_REG_START_ADDRESS1);
2541 /* Select DataRAM for DDP */
2542 value = onenand_bufferram_address(this, block);
2543 this->write_word(value, this->base + ONENAND_REG_START_ADDRESS2);
2544 /* Set start block address */
2545 this->write_word(block, this->base + ONENAND_REG_START_BLOCK_ADDRESS);
2546 /* Write lock command */
2547 this->command(mtd, cmd, 0, 0);
2549 /* There's no return value */
2550 this->wait(mtd, FL_LOCKING);
2553 while (this->read_word(this->base + ONENAND_REG_CTRL_STATUS)
2554 & ONENAND_CTRL_ONGO)
2557 /* Check lock status */
2558 status = this->read_word(this->base + ONENAND_REG_WP_STATUS);
2559 if (!(status & wp_status_mask))
2560 printk(KERN_ERR "%s: block = %d, wp status = 0x%x\n",
2561 __func__, block, status);
2568 * onenand_lock - [MTD Interface] Lock block(s)
2569 * @param mtd MTD device structure
2570 * @param ofs offset relative to mtd start
2571 * @param len number of bytes to unlock
2573 * Lock one or more blocks
2575 static int onenand_lock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
2579 onenand_get_device(mtd, FL_LOCKING);
2580 ret = onenand_do_lock_cmd(mtd, ofs, len, ONENAND_CMD_LOCK);
2581 onenand_release_device(mtd);
2586 * onenand_unlock - [MTD Interface] Unlock block(s)
2587 * @param mtd MTD device structure
2588 * @param ofs offset relative to mtd start
2589 * @param len number of bytes to unlock
2591 * Unlock one or more blocks
2593 static int onenand_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
2597 onenand_get_device(mtd, FL_LOCKING);
2598 ret = onenand_do_lock_cmd(mtd, ofs, len, ONENAND_CMD_UNLOCK);
2599 onenand_release_device(mtd);
2604 * onenand_check_lock_status - [OneNAND Interface] Check lock status
2605 * @param this onenand chip data structure
2609 static int onenand_check_lock_status(struct onenand_chip *this)
2611 unsigned int value, block, status;
2614 end = this->chipsize >> this->erase_shift;
2615 for (block = 0; block < end; block++) {
2616 /* Set block address */
2617 value = onenand_block_address(this, block);
2618 this->write_word(value, this->base + ONENAND_REG_START_ADDRESS1);
2619 /* Select DataRAM for DDP */
2620 value = onenand_bufferram_address(this, block);
2621 this->write_word(value, this->base + ONENAND_REG_START_ADDRESS2);
2622 /* Set start block address */
2623 this->write_word(block, this->base + ONENAND_REG_START_BLOCK_ADDRESS);
2625 /* Check lock status */
2626 status = this->read_word(this->base + ONENAND_REG_WP_STATUS);
2627 if (!(status & ONENAND_WP_US)) {
2628 printk(KERN_ERR "%s: block = %d, wp status = 0x%x\n",
2629 __func__, block, status);
2638 * onenand_unlock_all - [OneNAND Interface] unlock all blocks
2639 * @param mtd MTD device structure
2643 static void onenand_unlock_all(struct mtd_info *mtd)
2645 struct onenand_chip *this = mtd->priv;
2647 loff_t len = mtd->size;
2649 if (this->options & ONENAND_HAS_UNLOCK_ALL) {
2650 /* Set start block address */
2651 this->write_word(0, this->base + ONENAND_REG_START_BLOCK_ADDRESS);
2652 /* Write unlock command */
2653 this->command(mtd, ONENAND_CMD_UNLOCK_ALL, 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 /* Don't check lock status */
2664 if (this->options & ONENAND_SKIP_UNLOCK_CHECK)
2667 /* Check lock status */
2668 if (onenand_check_lock_status(this))
2671 /* Workaround for all block unlock in DDP */
2672 if (ONENAND_IS_DDP(this) && !FLEXONENAND(this)) {
2673 /* All blocks on another chip */
2674 ofs = this->chipsize >> 1;
2675 len = this->chipsize >> 1;
2679 onenand_do_lock_cmd(mtd, ofs, len, ONENAND_CMD_UNLOCK);
2682 #ifdef CONFIG_MTD_ONENAND_OTP
2685 * onenand_otp_command - Send OTP specific command to OneNAND device
2686 * @param mtd MTD device structure
2687 * @param cmd the command to be sent
2688 * @param addr offset to read from or write to
2689 * @param len number of bytes to read or write
2691 static int onenand_otp_command(struct mtd_info *mtd, int cmd, loff_t addr,
2694 struct onenand_chip *this = mtd->priv;
2695 int value, block, page;
2697 /* Address translation */
2699 case ONENAND_CMD_OTP_ACCESS:
2700 block = (int) (addr >> this->erase_shift);
2705 block = (int) (addr >> this->erase_shift);
2706 page = (int) (addr >> this->page_shift);
2708 if (ONENAND_IS_2PLANE(this)) {
2709 /* Make the even block number */
2711 /* Is it the odd plane? */
2712 if (addr & this->writesize)
2716 page &= this->page_mask;
2721 /* Write 'DFS, FBA' of Flash */
2722 value = onenand_block_address(this, block);
2723 this->write_word(value, this->base +
2724 ONENAND_REG_START_ADDRESS1);
2728 /* Now we use page size operation */
2729 int sectors = 4, count = 4;
2734 if (ONENAND_IS_2PLANE(this) && cmd == ONENAND_CMD_PROG)
2735 cmd = ONENAND_CMD_2X_PROG;
2736 dataram = ONENAND_CURRENT_BUFFERRAM(this);
2740 /* Write 'FPA, FSA' of Flash */
2741 value = onenand_page_address(page, sectors);
2742 this->write_word(value, this->base +
2743 ONENAND_REG_START_ADDRESS8);
2745 /* Write 'BSA, BSC' of DataRAM */
2746 value = onenand_buffer_address(dataram, sectors, count);
2747 this->write_word(value, this->base + ONENAND_REG_START_BUFFER);
2750 /* Interrupt clear */
2751 this->write_word(ONENAND_INT_CLEAR, this->base + ONENAND_REG_INTERRUPT);
2754 this->write_word(cmd, this->base + ONENAND_REG_COMMAND);
2760 * onenand_otp_write_oob_nolock - [INTERN] OneNAND write out-of-band, specific to OTP
2761 * @param mtd MTD device structure
2762 * @param to offset to write to
2763 * @param len number of bytes to write
2764 * @param retlen pointer to variable to store the number of written bytes
2765 * @param buf the data to write
2767 * OneNAND write out-of-band only for OTP
2769 static int onenand_otp_write_oob_nolock(struct mtd_info *mtd, loff_t to,
2770 struct mtd_oob_ops *ops)
2772 struct onenand_chip *this = mtd->priv;
2773 int column, ret = 0, oobsize;
2776 size_t len = ops->ooblen;
2777 const u_char *buf = ops->oobbuf;
2778 int block, value, status;
2782 /* Initialize retlen, in case of early exit */
2785 oobsize = mtd->oobsize;
2787 column = to & (mtd->oobsize - 1);
2789 oobbuf = this->oob_buf;
2791 /* Loop until all data write */
2792 while (written < len) {
2793 int thislen = min_t(int, oobsize, len - written);
2797 block = (int) (to >> this->erase_shift);
2799 * Write 'DFS, FBA' of Flash
2800 * Add: F100h DQ=DFS, FBA
2803 value = onenand_block_address(this, block);
2804 this->write_word(value, this->base +
2805 ONENAND_REG_START_ADDRESS1);
2808 * Select DataRAM for DDP
2812 value = onenand_bufferram_address(this, block);
2813 this->write_word(value, this->base +
2814 ONENAND_REG_START_ADDRESS2);
2815 ONENAND_SET_NEXT_BUFFERRAM(this);
2818 * Enter OTP access mode
2820 this->command(mtd, ONENAND_CMD_OTP_ACCESS, 0, 0);
2821 this->wait(mtd, FL_OTPING);
2823 /* We send data to spare ram with oobsize
2824 * to prevent byte access */
2825 memcpy(oobbuf + column, buf, thislen);
2828 * Write Data into DataRAM
2830 * in sector0/spare/page0
2833 this->write_bufferram(mtd, ONENAND_SPARERAM,
2834 oobbuf, 0, mtd->oobsize);
2836 onenand_otp_command(mtd, ONENAND_CMD_PROGOOB, to, mtd->oobsize);
2837 onenand_update_bufferram(mtd, to, 0);
2838 if (ONENAND_IS_2PLANE(this)) {
2839 ONENAND_SET_BUFFERRAM1(this);
2840 onenand_update_bufferram(mtd, to + this->writesize, 0);
2843 ret = this->wait(mtd, FL_WRITING);
2845 printk(KERN_ERR "%s: write failed %d\n", __func__, ret);
2849 /* Exit OTP access mode */
2850 this->command(mtd, ONENAND_CMD_RESET, 0, 0);
2851 this->wait(mtd, FL_RESETTING);
2853 status = this->read_word(this->base + ONENAND_REG_CTRL_STATUS);
2856 if (status == 0x60) {
2857 printk(KERN_DEBUG "\nBLOCK\tSTATUS\n");
2858 printk(KERN_DEBUG "1st Block\tLOCKED\n");
2859 printk(KERN_DEBUG "OTP Block\tLOCKED\n");
2860 } else if (status == 0x20) {
2861 printk(KERN_DEBUG "\nBLOCK\tSTATUS\n");
2862 printk(KERN_DEBUG "1st Block\tLOCKED\n");
2863 printk(KERN_DEBUG "OTP Block\tUN-LOCKED\n");
2864 } else if (status == 0x40) {
2865 printk(KERN_DEBUG "\nBLOCK\tSTATUS\n");
2866 printk(KERN_DEBUG "1st Block\tUN-LOCKED\n");
2867 printk(KERN_DEBUG "OTP Block\tLOCKED\n");
2869 printk(KERN_DEBUG "Reboot to check\n");
2876 to += mtd->writesize;
2881 ops->oobretlen = written;
2886 /* Internal OTP operation */
2887 typedef int (*otp_op_t)(struct mtd_info *mtd, loff_t form, size_t len,
2888 size_t *retlen, u_char *buf);
2891 * do_otp_read - [DEFAULT] Read OTP block area
2892 * @param mtd MTD device structure
2893 * @param from The offset to read
2894 * @param len number of bytes to read
2895 * @param retlen pointer to variable to store the number of readbytes
2896 * @param buf the databuffer to put/get data
2898 * Read OTP block area.
2900 static int do_otp_read(struct mtd_info *mtd, loff_t from, size_t len,
2901 size_t *retlen, u_char *buf)
2903 struct onenand_chip *this = mtd->priv;
2904 struct mtd_oob_ops ops = {
2912 /* Enter OTP access mode */
2913 this->command(mtd, ONENAND_CMD_OTP_ACCESS, 0, 0);
2914 this->wait(mtd, FL_OTPING);
2916 ret = ONENAND_IS_4KB_PAGE(this) ?
2917 onenand_mlc_read_ops_nolock(mtd, from, &ops) :
2918 onenand_read_ops_nolock(mtd, from, &ops);
2920 /* Exit OTP access mode */
2921 this->command(mtd, ONENAND_CMD_RESET, 0, 0);
2922 this->wait(mtd, FL_RESETTING);
2928 * do_otp_write - [DEFAULT] Write OTP block area
2929 * @param mtd MTD device structure
2930 * @param to The offset to write
2931 * @param len number of bytes to write
2932 * @param retlen pointer to variable to store the number of write bytes
2933 * @param buf the databuffer to put/get data
2935 * Write OTP block area.
2937 static int do_otp_write(struct mtd_info *mtd, loff_t to, size_t len,
2938 size_t *retlen, u_char *buf)
2940 struct onenand_chip *this = mtd->priv;
2941 unsigned char *pbuf = buf;
2943 struct mtd_oob_ops ops;
2945 /* Force buffer page aligned */
2946 if (len < mtd->writesize) {
2947 memcpy(this->page_buf, buf, len);
2948 memset(this->page_buf + len, 0xff, mtd->writesize - len);
2949 pbuf = this->page_buf;
2950 len = mtd->writesize;
2953 /* Enter OTP access mode */
2954 this->command(mtd, ONENAND_CMD_OTP_ACCESS, 0, 0);
2955 this->wait(mtd, FL_OTPING);
2961 ret = onenand_write_ops_nolock(mtd, to, &ops);
2962 *retlen = ops.retlen;
2964 /* Exit OTP access mode */
2965 this->command(mtd, ONENAND_CMD_RESET, 0, 0);
2966 this->wait(mtd, FL_RESETTING);
2972 * do_otp_lock - [DEFAULT] Lock OTP block area
2973 * @param mtd MTD device structure
2974 * @param from The offset to lock
2975 * @param len number of bytes to lock
2976 * @param retlen pointer to variable to store the number of lock bytes
2977 * @param buf the databuffer to put/get data
2979 * Lock OTP block area.
2981 static int do_otp_lock(struct mtd_info *mtd, loff_t from, size_t len,
2982 size_t *retlen, u_char *buf)
2984 struct onenand_chip *this = mtd->priv;
2985 struct mtd_oob_ops ops;
2988 if (FLEXONENAND(this)) {
2990 /* Enter OTP access mode */
2991 this->command(mtd, ONENAND_CMD_OTP_ACCESS, 0, 0);
2992 this->wait(mtd, FL_OTPING);
2994 * For Flex-OneNAND, we write lock mark to 1st word of sector 4 of
2995 * main area of page 49.
2997 ops.len = mtd->writesize;
3001 ret = onenand_write_ops_nolock(mtd, mtd->writesize * 49, &ops);
3002 *retlen = ops.retlen;
3004 /* Exit OTP access mode */
3005 this->command(mtd, ONENAND_CMD_RESET, 0, 0);
3006 this->wait(mtd, FL_RESETTING);
3008 ops.mode = MTD_OPS_PLACE_OOB;
3012 ret = onenand_otp_write_oob_nolock(mtd, from, &ops);
3013 *retlen = ops.oobretlen;
3020 * onenand_otp_walk - [DEFAULT] Handle OTP operation
3021 * @param mtd MTD device structure
3022 * @param from The offset to read/write
3023 * @param len number of bytes to read/write
3024 * @param retlen pointer to variable to store the number of read bytes
3025 * @param buf the databuffer to put/get data
3026 * @param action do given action
3027 * @param mode specify user and factory
3029 * Handle OTP operation.
3031 static int onenand_otp_walk(struct mtd_info *mtd, loff_t from, size_t len,
3032 size_t *retlen, u_char *buf,
3033 otp_op_t action, int mode)
3035 struct onenand_chip *this = mtd->priv;
3042 density = onenand_get_density(this->device_id);
3043 if (density < ONENAND_DEVICE_DENSITY_512Mb)
3048 if (mode == MTD_OTP_FACTORY) {
3049 from += mtd->writesize * otp_pages;
3050 otp_pages = ONENAND_PAGES_PER_BLOCK - otp_pages;
3053 /* Check User/Factory boundary */
3054 if (mode == MTD_OTP_USER) {
3055 if (mtd->writesize * otp_pages < from + len)
3058 if (mtd->writesize * otp_pages < len)
3062 onenand_get_device(mtd, FL_OTPING);
3063 while (len > 0 && otp_pages > 0) {
3064 if (!action) { /* OTP Info functions */
3065 struct otp_info *otpinfo;
3067 len -= sizeof(struct otp_info);
3073 otpinfo = (struct otp_info *) buf;
3074 otpinfo->start = from;
3075 otpinfo->length = mtd->writesize;
3076 otpinfo->locked = 0;
3078 from += mtd->writesize;
3079 buf += sizeof(struct otp_info);
3080 *retlen += sizeof(struct otp_info);
3084 ret = action(mtd, from, len, &tmp_retlen, buf);
3090 *retlen += tmp_retlen;
3095 onenand_release_device(mtd);
3101 * onenand_get_fact_prot_info - [MTD Interface] Read factory OTP info
3102 * @param mtd MTD device structure
3103 * @param len number of bytes to read
3104 * @param retlen pointer to variable to store the number of read bytes
3105 * @param buf the databuffer to put/get data
3107 * Read factory OTP info.
3109 static int onenand_get_fact_prot_info(struct mtd_info *mtd, size_t len,
3110 size_t *retlen, struct otp_info *buf)
3112 return onenand_otp_walk(mtd, 0, len, retlen, (u_char *) buf, NULL,
3117 * onenand_read_fact_prot_reg - [MTD Interface] Read factory OTP area
3118 * @param mtd MTD device structure
3119 * @param from The offset to read
3120 * @param len number of bytes to read
3121 * @param retlen pointer to variable to store the number of read bytes
3122 * @param buf the databuffer to put/get data
3124 * Read factory OTP area.
3126 static int onenand_read_fact_prot_reg(struct mtd_info *mtd, loff_t from,
3127 size_t len, size_t *retlen, u_char *buf)
3129 return onenand_otp_walk(mtd, from, len, retlen, buf, do_otp_read, MTD_OTP_FACTORY);
3133 * onenand_get_user_prot_info - [MTD Interface] Read user OTP info
3134 * @param mtd MTD device structure
3135 * @param retlen pointer to variable to store the number of read bytes
3136 * @param len number of bytes to read
3137 * @param buf the databuffer to put/get data
3139 * Read user OTP info.
3141 static int onenand_get_user_prot_info(struct mtd_info *mtd, size_t len,
3142 size_t *retlen, struct otp_info *buf)
3144 return onenand_otp_walk(mtd, 0, len, retlen, (u_char *) buf, NULL,
3149 * onenand_read_user_prot_reg - [MTD Interface] Read user OTP area
3150 * @param mtd MTD device structure
3151 * @param from The offset to read
3152 * @param len number of bytes to read
3153 * @param retlen pointer to variable to store the number of read bytes
3154 * @param buf the databuffer to put/get data
3156 * Read user OTP area.
3158 static int onenand_read_user_prot_reg(struct mtd_info *mtd, loff_t from,
3159 size_t len, size_t *retlen, u_char *buf)
3161 return onenand_otp_walk(mtd, from, len, retlen, buf, do_otp_read, MTD_OTP_USER);
3165 * onenand_write_user_prot_reg - [MTD Interface] Write user OTP area
3166 * @param mtd MTD device structure
3167 * @param from The offset to write
3168 * @param len number of bytes to write
3169 * @param retlen pointer to variable to store the number of write bytes
3170 * @param buf the databuffer to put/get data
3172 * Write user OTP area.
3174 static int onenand_write_user_prot_reg(struct mtd_info *mtd, loff_t from,
3175 size_t len, size_t *retlen, u_char *buf)
3177 return onenand_otp_walk(mtd, from, len, retlen, buf, do_otp_write, MTD_OTP_USER);
3181 * onenand_lock_user_prot_reg - [MTD Interface] Lock user OTP area
3182 * @param mtd MTD device structure
3183 * @param from The offset to lock
3184 * @param len number of bytes to unlock
3186 * Write lock mark on spare area in page 0 in OTP block
3188 static int onenand_lock_user_prot_reg(struct mtd_info *mtd, loff_t from,
3191 struct onenand_chip *this = mtd->priv;
3192 u_char *buf = FLEXONENAND(this) ? this->page_buf : this->oob_buf;
3195 unsigned int otp_lock_offset = ONENAND_OTP_LOCK_OFFSET;
3197 memset(buf, 0xff, FLEXONENAND(this) ? this->writesize
3200 * Write lock mark to 8th word of sector0 of page0 of the spare0.
3201 * We write 16 bytes spare area instead of 2 bytes.
3202 * For Flex-OneNAND, we write lock mark to 1st word of sector 4 of
3203 * main area of page 49.
3207 len = FLEXONENAND(this) ? mtd->writesize : 16;
3210 * Note: OTP lock operation
3211 * OTP block : 0xXXFC XX 1111 1100
3212 * 1st block : 0xXXF3 (If chip support) XX 1111 0011
3213 * Both : 0xXXF0 (If chip support) XX 1111 0000
3215 if (FLEXONENAND(this))
3216 otp_lock_offset = FLEXONENAND_OTP_LOCK_OFFSET;
3218 /* ONENAND_OTP_AREA | ONENAND_OTP_BLOCK0 | ONENAND_OTP_AREA_BLOCK0 */
3220 buf[otp_lock_offset] = 0xFC;
3222 buf[otp_lock_offset] = 0xF3;
3224 buf[otp_lock_offset] = 0xF0;
3226 printk(KERN_DEBUG "[OneNAND] Invalid option selected for OTP\n");
3228 ret = onenand_otp_walk(mtd, from, len, &retlen, buf, do_otp_lock, MTD_OTP_USER);
3230 return ret ? : retlen;
3233 #endif /* CONFIG_MTD_ONENAND_OTP */
3236 * onenand_check_features - Check and set OneNAND features
3237 * @param mtd MTD data structure
3239 * Check and set OneNAND features
3243 static void onenand_check_features(struct mtd_info *mtd)
3245 struct onenand_chip *this = mtd->priv;
3246 unsigned int density, process, numbufs;
3248 /* Lock scheme depends on density and process */
3249 density = onenand_get_density(this->device_id);
3250 process = this->version_id >> ONENAND_VERSION_PROCESS_SHIFT;
3251 numbufs = this->read_word(this->base + ONENAND_REG_NUM_BUFFERS) >> 8;
3255 case ONENAND_DEVICE_DENSITY_8Gb:
3256 this->options |= ONENAND_HAS_NOP_1;
3258 case ONENAND_DEVICE_DENSITY_4Gb:
3259 if (ONENAND_IS_DDP(this))
3260 this->options |= ONENAND_HAS_2PLANE;
3261 else if (numbufs == 1) {
3262 this->options |= ONENAND_HAS_4KB_PAGE;
3263 this->options |= ONENAND_HAS_CACHE_PROGRAM;
3265 * There are two different 4KiB pagesize chips
3266 * and no way to detect it by H/W config values.
3268 * To detect the correct NOP for each chips,
3269 * It should check the version ID as workaround.
3271 * Now it has as following
3272 * KFM4G16Q4M has NOP 4 with version ID 0x0131
3273 * KFM4G16Q5M has NOP 1 with versoin ID 0x013e
3275 if ((this->version_id & 0xf) == 0xe)
3276 this->options |= ONENAND_HAS_NOP_1;
3278 this->options |= ONENAND_HAS_UNLOCK_ALL;
3281 case ONENAND_DEVICE_DENSITY_2Gb:
3282 /* 2Gb DDP does not have 2 plane */
3283 if (!ONENAND_IS_DDP(this))
3284 this->options |= ONENAND_HAS_2PLANE;
3285 this->options |= ONENAND_HAS_UNLOCK_ALL;
3288 case ONENAND_DEVICE_DENSITY_1Gb:
3289 /* A-Die has all block unlock */
3291 this->options |= ONENAND_HAS_UNLOCK_ALL;
3295 /* Some OneNAND has continuous lock scheme */
3297 this->options |= ONENAND_HAS_CONT_LOCK;
3301 /* The MLC has 4KiB pagesize. */
3302 if (ONENAND_IS_MLC(this))
3303 this->options |= ONENAND_HAS_4KB_PAGE;
3305 if (ONENAND_IS_4KB_PAGE(this))
3306 this->options &= ~ONENAND_HAS_2PLANE;
3308 if (FLEXONENAND(this)) {
3309 this->options &= ~ONENAND_HAS_CONT_LOCK;
3310 this->options |= ONENAND_HAS_UNLOCK_ALL;
3313 if (this->options & ONENAND_HAS_CONT_LOCK)
3314 printk(KERN_DEBUG "Lock scheme is Continuous Lock\n");
3315 if (this->options & ONENAND_HAS_UNLOCK_ALL)
3316 printk(KERN_DEBUG "Chip support all block unlock\n");
3317 if (this->options & ONENAND_HAS_2PLANE)
3318 printk(KERN_DEBUG "Chip has 2 plane\n");
3319 if (this->options & ONENAND_HAS_4KB_PAGE)
3320 printk(KERN_DEBUG "Chip has 4KiB pagesize\n");
3321 if (this->options & ONENAND_HAS_CACHE_PROGRAM)
3322 printk(KERN_DEBUG "Chip has cache program feature\n");
3326 * onenand_print_device_info - Print device & version ID
3327 * @param device device ID
3328 * @param version version ID
3330 * Print device & version ID
3332 static void onenand_print_device_info(int device, int version)
3334 int vcc, demuxed, ddp, density, flexonenand;
3336 vcc = device & ONENAND_DEVICE_VCC_MASK;
3337 demuxed = device & ONENAND_DEVICE_IS_DEMUX;
3338 ddp = device & ONENAND_DEVICE_IS_DDP;
3339 density = onenand_get_density(device);
3340 flexonenand = device & DEVICE_IS_FLEXONENAND;
3341 printk(KERN_INFO "%s%sOneNAND%s %dMB %sV 16-bit (0x%02x)\n",
3342 demuxed ? "" : "Muxed ",
3343 flexonenand ? "Flex-" : "",
3346 vcc ? "2.65/3.3" : "1.8",
3348 printk(KERN_INFO "OneNAND version = 0x%04x\n", version);
3351 static const struct onenand_manufacturers onenand_manuf_ids[] = {
3352 {ONENAND_MFR_SAMSUNG, "Samsung"},
3353 {ONENAND_MFR_NUMONYX, "Numonyx"},
3357 * onenand_check_maf - Check manufacturer ID
3358 * @param manuf manufacturer ID
3360 * Check manufacturer ID
3362 static int onenand_check_maf(int manuf)
3364 int size = ARRAY_SIZE(onenand_manuf_ids);
3368 for (i = 0; i < size; i++)
3369 if (manuf == onenand_manuf_ids[i].id)
3373 name = onenand_manuf_ids[i].name;
3377 printk(KERN_DEBUG "OneNAND Manufacturer: %s (0x%0x)\n", name, manuf);
3383 * flexonenand_get_boundary - Reads the SLC boundary
3384 * @param onenand_info - onenand info structure
3386 static int flexonenand_get_boundary(struct mtd_info *mtd)
3388 struct onenand_chip *this = mtd->priv;
3393 syscfg = this->read_word(this->base + ONENAND_REG_SYS_CFG1);
3394 this->write_word((syscfg | 0x0100), this->base + ONENAND_REG_SYS_CFG1);
3396 for (die = 0; die < this->dies; die++) {
3397 this->command(mtd, FLEXONENAND_CMD_PI_ACCESS, die, 0);
3398 this->wait(mtd, FL_SYNCING);
3400 this->command(mtd, FLEXONENAND_CMD_READ_PI, die, 0);
3401 this->wait(mtd, FL_READING);
3403 bdry = this->read_word(this->base + ONENAND_DATARAM);
3404 if ((bdry >> FLEXONENAND_PI_UNLOCK_SHIFT) == 3)
3408 this->boundary[die] = bdry & FLEXONENAND_PI_MASK;
3410 this->command(mtd, ONENAND_CMD_RESET, 0, 0);
3411 this->wait(mtd, FL_RESETTING);
3413 printk(KERN_INFO "Die %d boundary: %d%s\n", die,
3414 this->boundary[die], locked ? "(Locked)" : "(Unlocked)");
3418 this->write_word(syscfg, this->base + ONENAND_REG_SYS_CFG1);
3423 * flexonenand_get_size - Fill up fields in onenand_chip and mtd_info
3424 * boundary[], diesize[], mtd->size, mtd->erasesize
3425 * @param mtd - MTD device structure
3427 static void flexonenand_get_size(struct mtd_info *mtd)
3429 struct onenand_chip *this = mtd->priv;
3430 int die, i, eraseshift, density;
3431 int blksperdie, maxbdry;
3434 density = onenand_get_density(this->device_id);
3435 blksperdie = ((loff_t)(16 << density) << 20) >> (this->erase_shift);
3436 blksperdie >>= ONENAND_IS_DDP(this) ? 1 : 0;
3437 maxbdry = blksperdie - 1;
3438 eraseshift = this->erase_shift - 1;
3440 mtd->numeraseregions = this->dies << 1;
3442 /* This fills up the device boundary */
3443 flexonenand_get_boundary(mtd);
3446 for (; die < this->dies; die++) {
3447 if (!die || this->boundary[die-1] != maxbdry) {
3449 mtd->eraseregions[i].offset = ofs;
3450 mtd->eraseregions[i].erasesize = 1 << eraseshift;
3451 mtd->eraseregions[i].numblocks =
3452 this->boundary[die] + 1;
3453 ofs += mtd->eraseregions[i].numblocks << eraseshift;
3456 mtd->numeraseregions -= 1;
3457 mtd->eraseregions[i].numblocks +=
3458 this->boundary[die] + 1;
3459 ofs += (this->boundary[die] + 1) << (eraseshift - 1);
3461 if (this->boundary[die] != maxbdry) {
3463 mtd->eraseregions[i].offset = ofs;
3464 mtd->eraseregions[i].erasesize = 1 << eraseshift;
3465 mtd->eraseregions[i].numblocks = maxbdry ^
3466 this->boundary[die];
3467 ofs += mtd->eraseregions[i].numblocks << eraseshift;
3470 mtd->numeraseregions -= 1;
3473 /* Expose MLC erase size except when all blocks are SLC */
3474 mtd->erasesize = 1 << this->erase_shift;
3475 if (mtd->numeraseregions == 1)
3476 mtd->erasesize >>= 1;
3478 printk(KERN_INFO "Device has %d eraseregions\n", mtd->numeraseregions);
3479 for (i = 0; i < mtd->numeraseregions; i++)
3480 printk(KERN_INFO "[offset: 0x%08x, erasesize: 0x%05x,"
3481 " numblocks: %04u]\n",
3482 (unsigned int) mtd->eraseregions[i].offset,
3483 mtd->eraseregions[i].erasesize,
3484 mtd->eraseregions[i].numblocks);
3486 for (die = 0, mtd->size = 0; die < this->dies; die++) {
3487 this->diesize[die] = (loff_t)blksperdie << this->erase_shift;
3488 this->diesize[die] -= (loff_t)(this->boundary[die] + 1)
3489 << (this->erase_shift - 1);
3490 mtd->size += this->diesize[die];
3495 * flexonenand_check_blocks_erased - Check if blocks are erased
3496 * @param mtd_info - mtd info structure
3497 * @param start - first erase block to check
3498 * @param end - last erase block to check
3500 * Converting an unerased block from MLC to SLC
3501 * causes byte values to change. Since both data and its ECC
3502 * have changed, reads on the block give uncorrectable error.
3503 * This might lead to the block being detected as bad.
3505 * Avoid this by ensuring that the block to be converted is
3508 static int flexonenand_check_blocks_erased(struct mtd_info *mtd, int start, int end)
3510 struct onenand_chip *this = mtd->priv;
3513 struct mtd_oob_ops ops = {
3514 .mode = MTD_OPS_PLACE_OOB,
3516 .ooblen = mtd->oobsize,
3518 .oobbuf = this->oob_buf,
3522 printk(KERN_DEBUG "Check blocks from %d to %d\n", start, end);
3524 for (block = start; block <= end; block++) {
3525 addr = flexonenand_addr(this, block);
3526 if (onenand_block_isbad_nolock(mtd, addr, 0))
3530 * Since main area write results in ECC write to spare,
3531 * it is sufficient to check only ECC bytes for change.
3533 ret = onenand_read_oob_nolock(mtd, addr, &ops);
3537 for (i = 0; i < mtd->oobsize; i++)
3538 if (this->oob_buf[i] != 0xff)
3541 if (i != mtd->oobsize) {
3542 printk(KERN_WARNING "%s: Block %d not erased.\n",
3552 * flexonenand_set_boundary - Writes the SLC boundary
3553 * @param mtd - mtd info structure
3555 static int flexonenand_set_boundary(struct mtd_info *mtd, int die,
3556 int boundary, int lock)
3558 struct onenand_chip *this = mtd->priv;
3559 int ret, density, blksperdie, old, new, thisboundary;
3562 /* Change only once for SDP Flex-OneNAND */
3563 if (die && (!ONENAND_IS_DDP(this)))
3566 /* boundary value of -1 indicates no required change */
3567 if (boundary < 0 || boundary == this->boundary[die])
3570 density = onenand_get_density(this->device_id);
3571 blksperdie = ((16 << density) << 20) >> this->erase_shift;
3572 blksperdie >>= ONENAND_IS_DDP(this) ? 1 : 0;
3574 if (boundary >= blksperdie) {
3575 printk(KERN_ERR "%s: Invalid boundary value. "
3576 "Boundary not changed.\n", __func__);
3580 /* Check if converting blocks are erased */
3581 old = this->boundary[die] + (die * this->density_mask);
3582 new = boundary + (die * this->density_mask);
3583 ret = flexonenand_check_blocks_erased(mtd, min(old, new) + 1, max(old, new));
3585 printk(KERN_ERR "%s: Please erase blocks "
3586 "before boundary change\n", __func__);
3590 this->command(mtd, FLEXONENAND_CMD_PI_ACCESS, die, 0);
3591 this->wait(mtd, FL_SYNCING);
3593 /* Check is boundary is locked */
3594 this->command(mtd, FLEXONENAND_CMD_READ_PI, die, 0);
3595 this->wait(mtd, FL_READING);
3597 thisboundary = this->read_word(this->base + ONENAND_DATARAM);
3598 if ((thisboundary >> FLEXONENAND_PI_UNLOCK_SHIFT) != 3) {
3599 printk(KERN_ERR "%s: boundary locked\n", __func__);
3604 printk(KERN_INFO "Changing die %d boundary: %d%s\n",
3605 die, boundary, lock ? "(Locked)" : "(Unlocked)");
3607 addr = die ? this->diesize[0] : 0;
3609 boundary &= FLEXONENAND_PI_MASK;
3610 boundary |= lock ? 0 : (3 << FLEXONENAND_PI_UNLOCK_SHIFT);
3612 this->command(mtd, ONENAND_CMD_ERASE, addr, 0);
3613 ret = this->wait(mtd, FL_ERASING);
3615 printk(KERN_ERR "%s: Failed PI erase for Die %d\n",
3620 this->write_word(boundary, this->base + ONENAND_DATARAM);
3621 this->command(mtd, ONENAND_CMD_PROG, addr, 0);
3622 ret = this->wait(mtd, FL_WRITING);
3624 printk(KERN_ERR "%s: Failed PI write for Die %d\n",
3629 this->command(mtd, FLEXONENAND_CMD_PI_UPDATE, die, 0);
3630 ret = this->wait(mtd, FL_WRITING);
3632 this->write_word(ONENAND_CMD_RESET, this->base + ONENAND_REG_COMMAND);
3633 this->wait(mtd, FL_RESETTING);
3635 /* Recalculate device size on boundary change*/
3636 flexonenand_get_size(mtd);
3642 * onenand_chip_probe - [OneNAND Interface] The generic chip probe
3643 * @param mtd MTD device structure
3645 * OneNAND detection method:
3646 * Compare the values from command with ones from register
3648 static int onenand_chip_probe(struct mtd_info *mtd)
3650 struct onenand_chip *this = mtd->priv;
3651 int bram_maf_id, bram_dev_id, maf_id, dev_id;
3654 /* Save system configuration 1 */
3655 syscfg = this->read_word(this->base + ONENAND_REG_SYS_CFG1);
3656 /* Clear Sync. Burst Read mode to read BootRAM */
3657 this->write_word((syscfg & ~ONENAND_SYS_CFG1_SYNC_READ & ~ONENAND_SYS_CFG1_SYNC_WRITE), this->base + ONENAND_REG_SYS_CFG1);
3659 /* Send the command for reading device ID from BootRAM */
3660 this->write_word(ONENAND_CMD_READID, this->base + ONENAND_BOOTRAM);
3662 /* Read manufacturer and device IDs from BootRAM */
3663 bram_maf_id = this->read_word(this->base + ONENAND_BOOTRAM + 0x0);
3664 bram_dev_id = this->read_word(this->base + ONENAND_BOOTRAM + 0x2);
3666 /* Reset OneNAND to read default register values */
3667 this->write_word(ONENAND_CMD_RESET, this->base + ONENAND_BOOTRAM);
3669 this->wait(mtd, FL_RESETTING);
3671 /* Restore system configuration 1 */
3672 this->write_word(syscfg, this->base + ONENAND_REG_SYS_CFG1);
3674 /* Check manufacturer ID */
3675 if (onenand_check_maf(bram_maf_id))
3678 /* Read manufacturer and device IDs from Register */
3679 maf_id = this->read_word(this->base + ONENAND_REG_MANUFACTURER_ID);
3680 dev_id = this->read_word(this->base + ONENAND_REG_DEVICE_ID);
3682 /* Check OneNAND device */
3683 if (maf_id != bram_maf_id || dev_id != bram_dev_id)
3690 * onenand_probe - [OneNAND Interface] Probe the OneNAND device
3691 * @param mtd MTD device structure
3693 static int onenand_probe(struct mtd_info *mtd)
3695 struct onenand_chip *this = mtd->priv;
3700 ret = this->chip_probe(mtd);
3704 /* Device and version IDs from Register */
3705 dev_id = this->read_word(this->base + ONENAND_REG_DEVICE_ID);
3706 ver_id = this->read_word(this->base + ONENAND_REG_VERSION_ID);
3707 this->technology = this->read_word(this->base + ONENAND_REG_TECHNOLOGY);
3709 /* Flash device information */
3710 onenand_print_device_info(dev_id, ver_id);
3711 this->device_id = dev_id;
3712 this->version_id = ver_id;
3714 /* Check OneNAND features */
3715 onenand_check_features(mtd);
3717 density = onenand_get_density(dev_id);
3718 if (FLEXONENAND(this)) {
3719 this->dies = ONENAND_IS_DDP(this) ? 2 : 1;
3720 /* Maximum possible erase regions */
3721 mtd->numeraseregions = this->dies << 1;
3723 kcalloc(this->dies << 1,
3724 sizeof(struct mtd_erase_region_info),
3726 if (!mtd->eraseregions)
3731 * For Flex-OneNAND, chipsize represents maximum possible device size.
3732 * mtd->size represents the actual device size.
3734 this->chipsize = (16 << density) << 20;
3736 /* OneNAND page size & block size */
3737 /* The data buffer size is equal to page size */
3738 mtd->writesize = this->read_word(this->base + ONENAND_REG_DATA_BUFFER_SIZE);
3739 /* We use the full BufferRAM */
3740 if (ONENAND_IS_4KB_PAGE(this))
3741 mtd->writesize <<= 1;
3743 mtd->oobsize = mtd->writesize >> 5;
3744 /* Pages per a block are always 64 in OneNAND */
3745 mtd->erasesize = mtd->writesize << 6;
3747 * Flex-OneNAND SLC area has 64 pages per block.
3748 * Flex-OneNAND MLC area has 128 pages per block.
3749 * Expose MLC erase size to find erase_shift and page_mask.
3751 if (FLEXONENAND(this))
3752 mtd->erasesize <<= 1;
3754 this->erase_shift = ffs(mtd->erasesize) - 1;
3755 this->page_shift = ffs(mtd->writesize) - 1;
3756 this->page_mask = (1 << (this->erase_shift - this->page_shift)) - 1;
3757 /* Set density mask. it is used for DDP */
3758 if (ONENAND_IS_DDP(this))
3759 this->density_mask = this->chipsize >> (this->erase_shift + 1);
3760 /* It's real page size */
3761 this->writesize = mtd->writesize;
3763 /* REVISIT: Multichip handling */
3765 if (FLEXONENAND(this))
3766 flexonenand_get_size(mtd);
3768 mtd->size = this->chipsize;
3771 * We emulate the 4KiB page and 256KiB erase block size
3772 * But oobsize is still 64 bytes.
3773 * It is only valid if you turn on 2X program support,
3774 * Otherwise it will be ignored by compiler.
3776 if (ONENAND_IS_2PLANE(this)) {
3777 mtd->writesize <<= 1;
3778 mtd->erasesize <<= 1;
3785 * onenand_suspend - [MTD Interface] Suspend the OneNAND flash
3786 * @param mtd MTD device structure
3788 static int onenand_suspend(struct mtd_info *mtd)
3790 return onenand_get_device(mtd, FL_PM_SUSPENDED);
3794 * onenand_resume - [MTD Interface] Resume the OneNAND flash
3795 * @param mtd MTD device structure
3797 static void onenand_resume(struct mtd_info *mtd)
3799 struct onenand_chip *this = mtd->priv;
3801 if (this->state == FL_PM_SUSPENDED)
3802 onenand_release_device(mtd);
3804 printk(KERN_ERR "%s: resume() called for the chip which is not "
3805 "in suspended state\n", __func__);
3809 * onenand_scan - [OneNAND Interface] Scan for the OneNAND device
3810 * @param mtd MTD device structure
3811 * @param maxchips Number of chips to scan for
3813 * This fills out all the not initialized function pointers
3814 * with the defaults.
3815 * The flash ID is read and the mtd/chip structures are
3816 * filled with the appropriate values.
3818 int onenand_scan(struct mtd_info *mtd, int maxchips)
3821 struct onenand_chip *this = mtd->priv;
3823 if (!this->read_word)
3824 this->read_word = onenand_readw;
3825 if (!this->write_word)
3826 this->write_word = onenand_writew;
3829 this->command = onenand_command;
3831 onenand_setup_wait(mtd);
3832 if (!this->bbt_wait)
3833 this->bbt_wait = onenand_bbt_wait;
3834 if (!this->unlock_all)
3835 this->unlock_all = onenand_unlock_all;
3837 if (!this->chip_probe)
3838 this->chip_probe = onenand_chip_probe;
3840 if (!this->read_bufferram)
3841 this->read_bufferram = onenand_read_bufferram;
3842 if (!this->write_bufferram)
3843 this->write_bufferram = onenand_write_bufferram;
3845 if (!this->block_markbad)
3846 this->block_markbad = onenand_default_block_markbad;
3847 if (!this->scan_bbt)
3848 this->scan_bbt = onenand_default_bbt;
3850 if (onenand_probe(mtd))
3853 /* Set Sync. Burst Read after probing */
3854 if (this->mmcontrol) {
3855 printk(KERN_INFO "OneNAND Sync. Burst Read support\n");
3856 this->read_bufferram = onenand_sync_read_bufferram;
3859 /* Allocate buffers, if necessary */
3860 if (!this->page_buf) {
3861 this->page_buf = kzalloc(mtd->writesize, GFP_KERNEL);
3862 if (!this->page_buf)
3864 #ifdef CONFIG_MTD_ONENAND_VERIFY_WRITE
3865 this->verify_buf = kzalloc(mtd->writesize, GFP_KERNEL);
3866 if (!this->verify_buf) {
3867 kfree(this->page_buf);
3871 this->options |= ONENAND_PAGEBUF_ALLOC;
3873 if (!this->oob_buf) {
3874 this->oob_buf = kzalloc(mtd->oobsize, GFP_KERNEL);
3875 if (!this->oob_buf) {
3876 if (this->options & ONENAND_PAGEBUF_ALLOC) {
3877 this->options &= ~ONENAND_PAGEBUF_ALLOC;
3878 #ifdef CONFIG_MTD_ONENAND_VERIFY_WRITE
3879 kfree(this->verify_buf);
3881 kfree(this->page_buf);
3885 this->options |= ONENAND_OOBBUF_ALLOC;
3888 this->state = FL_READY;
3889 init_waitqueue_head(&this->wq);
3890 spin_lock_init(&this->chip_lock);
3893 * Allow subpage writes up to oobsize.
3895 switch (mtd->oobsize) {
3897 if (FLEXONENAND(this)) {
3898 mtd_set_ooblayout(mtd, &flexonenand_ooblayout_ops);
3899 mtd->subpage_sft = 0;
3901 mtd_set_ooblayout(mtd, &onenand_oob_128_ooblayout_ops);
3902 mtd->subpage_sft = 2;
3904 if (ONENAND_IS_NOP_1(this))
3905 mtd->subpage_sft = 0;
3908 mtd_set_ooblayout(mtd, &onenand_oob_32_64_ooblayout_ops);
3909 mtd->subpage_sft = 2;
3913 mtd_set_ooblayout(mtd, &onenand_oob_32_64_ooblayout_ops);
3914 mtd->subpage_sft = 1;
3918 printk(KERN_WARNING "%s: No OOB scheme defined for oobsize %d\n",
3919 __func__, mtd->oobsize);
3920 mtd->subpage_sft = 0;
3921 /* To prevent kernel oops */
3922 mtd_set_ooblayout(mtd, &onenand_oob_32_64_ooblayout_ops);
3926 this->subpagesize = mtd->writesize >> mtd->subpage_sft;
3929 * The number of bytes available for a client to place data into
3930 * the out of band area
3932 ret = mtd_ooblayout_count_freebytes(mtd);
3936 mtd->oobavail = ret;
3938 mtd->ecc_strength = 1;
3940 /* Fill in remaining MTD driver data */
3941 mtd->type = ONENAND_IS_MLC(this) ? MTD_MLCNANDFLASH : MTD_NANDFLASH;
3942 mtd->flags = MTD_CAP_NANDFLASH;
3943 mtd->_erase = onenand_erase;
3945 mtd->_unpoint = NULL;
3946 mtd->_read_oob = onenand_read_oob;
3947 mtd->_write_oob = onenand_write_oob;
3948 mtd->_panic_write = onenand_panic_write;
3949 #ifdef CONFIG_MTD_ONENAND_OTP
3950 mtd->_get_fact_prot_info = onenand_get_fact_prot_info;
3951 mtd->_read_fact_prot_reg = onenand_read_fact_prot_reg;
3952 mtd->_get_user_prot_info = onenand_get_user_prot_info;
3953 mtd->_read_user_prot_reg = onenand_read_user_prot_reg;
3954 mtd->_write_user_prot_reg = onenand_write_user_prot_reg;
3955 mtd->_lock_user_prot_reg = onenand_lock_user_prot_reg;
3957 mtd->_sync = onenand_sync;
3958 mtd->_lock = onenand_lock;
3959 mtd->_unlock = onenand_unlock;
3960 mtd->_suspend = onenand_suspend;
3961 mtd->_resume = onenand_resume;
3962 mtd->_block_isbad = onenand_block_isbad;
3963 mtd->_block_markbad = onenand_block_markbad;
3964 mtd->owner = THIS_MODULE;
3965 mtd->writebufsize = mtd->writesize;
3967 /* Unlock whole block */
3968 if (!(this->options & ONENAND_SKIP_INITIAL_UNLOCKING))
3969 this->unlock_all(mtd);
3971 /* Set the bad block marker position */
3972 this->badblockpos = ONENAND_BADBLOCK_POS;
3974 ret = this->scan_bbt(mtd);
3975 if ((!FLEXONENAND(this)) || ret)
3978 /* Change Flex-OneNAND boundaries if required */
3979 for (i = 0; i < MAX_DIES; i++)
3980 flexonenand_set_boundary(mtd, i, flex_bdry[2 * i],
3981 flex_bdry[(2 * i) + 1]);
3987 * onenand_release - [OneNAND Interface] Free resources held by the OneNAND device
3988 * @param mtd MTD device structure
3990 void onenand_release(struct mtd_info *mtd)
3992 struct onenand_chip *this = mtd->priv;
3994 /* Deregister partitions */
3995 mtd_device_unregister(mtd);
3997 /* Free bad block table memory, if allocated */
3999 struct bbm_info *bbm = this->bbm;
4003 /* Buffers allocated by onenand_scan */
4004 if (this->options & ONENAND_PAGEBUF_ALLOC) {
4005 kfree(this->page_buf);
4006 #ifdef CONFIG_MTD_ONENAND_VERIFY_WRITE
4007 kfree(this->verify_buf);
4010 if (this->options & ONENAND_OOBBUF_ALLOC)
4011 kfree(this->oob_buf);
4012 kfree(mtd->eraseregions);
4015 EXPORT_SYMBOL_GPL(onenand_scan);
4016 EXPORT_SYMBOL_GPL(onenand_release);
4018 MODULE_LICENSE("GPL");
4019 MODULE_AUTHOR("Kyungmin Park <kyungmin.park@samsung.com>");
4020 MODULE_DESCRIPTION("Generic OneNAND flash driver code");