2 * linux/drivers/mtd/onenand/onenand_base.c
4 * Copyright (C) 2005-2007 Samsung Electronics
5 * Kyungmin Park <kyungmin.park@samsung.com>
8 * Adrian Hunter <ext-adrian.hunter@nokia.com>:
9 * auto-placement support, read-while load support, various fixes
10 * Copyright (C) Nokia Corporation, 2007
12 * Rohit Hagargundgi <h.rohit at samsung.com>,
13 * Amul Kumar Saha <amul.saha@samsung.com>:
14 * Flex-OneNAND support
15 * Copyright (C) Samsung Electronics, 2009
17 * This program is free software; you can redistribute it and/or modify
18 * it under the terms of the GNU General Public License version 2 as
19 * published by the Free Software Foundation.
23 #include <linux/compat.h>
24 #include <linux/mtd/mtd.h>
25 #include <linux/mtd/onenand.h>
28 #include <asm/errno.h>
31 /* It should access 16-bit instead of 8-bit */
32 static void *memcpy_16(void *dst, const void *src, unsigned int len)
45 * onenand_oob_128 - oob info for Flex-Onenand with 4KB page
46 * For now, we expose only 64 out of 80 ecc bytes
48 static struct nand_ecclayout onenand_oob_128 = {
51 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
52 22, 23, 24, 25, 26, 27, 28, 29, 30, 31,
53 38, 39, 40, 41, 42, 43, 44, 45, 46, 47,
54 54, 55, 56, 57, 58, 59, 60, 61, 62, 63,
55 70, 71, 72, 73, 74, 75, 76, 77, 78, 79,
56 86, 87, 88, 89, 90, 91, 92, 93, 94, 95,
60 {2, 4}, {18, 4}, {34, 4}, {50, 4},
61 {66, 4}, {82, 4}, {98, 4}, {114, 4}
66 * onenand_oob_64 - oob info for large (2KB) page
68 static struct nand_ecclayout onenand_oob_64 = {
77 {2, 3}, {14, 2}, {18, 3}, {30, 2},
78 {34, 3}, {46, 2}, {50, 3}, {62, 2}
83 * onenand_oob_32 - oob info for middle (1KB) page
85 static struct nand_ecclayout onenand_oob_32 = {
91 .oobfree = { {2, 3}, {14, 2}, {18, 3}, {30, 2} }
95 * Warning! This array is used with the memcpy_16() function, thus
96 * it must be aligned to 2 bytes. GCC can make this array unaligned
97 * as the array is made of unsigned char, which memcpy16() doesn't
98 * like and will cause unaligned access.
100 static const unsigned char __aligned(2) ffchars[] = {
101 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
102 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 16 */
103 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
104 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 32 */
105 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
106 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 48 */
107 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
108 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 64 */
109 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
110 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 80 */
111 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
112 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 96 */
113 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
114 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 112 */
115 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
116 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 128 */
120 * onenand_readw - [OneNAND Interface] Read OneNAND register
121 * @param addr address to read
123 * Read OneNAND register
125 static unsigned short onenand_readw(void __iomem * addr)
131 * onenand_writew - [OneNAND Interface] Write OneNAND register with value
132 * @param value value to write
133 * @param addr address to write
135 * Write OneNAND register with value
137 static void onenand_writew(unsigned short value, void __iomem * addr)
143 * onenand_block_address - [DEFAULT] Get block address
144 * @param device the device id
145 * @param block the block
146 * @return translated block address if DDP, otherwise same
148 * Setup Start Address 1 Register (F100h)
150 static int onenand_block_address(struct onenand_chip *this, int block)
152 /* Device Flash Core select, NAND Flash Block Address */
153 if (block & this->density_mask)
154 return ONENAND_DDP_CHIP1 | (block ^ this->density_mask);
160 * onenand_bufferram_address - [DEFAULT] Get bufferram address
161 * @param device the device id
162 * @param block the block
163 * @return set DBS value if DDP, otherwise 0
165 * Setup Start Address 2 Register (F101h) for DDP
167 static int onenand_bufferram_address(struct onenand_chip *this, int block)
169 /* Device BufferRAM Select */
170 if (block & this->density_mask)
171 return ONENAND_DDP_CHIP1;
173 return ONENAND_DDP_CHIP0;
177 * onenand_page_address - [DEFAULT] Get page address
178 * @param page the page address
179 * @param sector the sector address
180 * @return combined page and sector address
182 * Setup Start Address 8 Register (F107h)
184 static int onenand_page_address(int page, int sector)
186 /* Flash Page Address, Flash Sector Address */
189 fpa = page & ONENAND_FPA_MASK;
190 fsa = sector & ONENAND_FSA_MASK;
192 return ((fpa << ONENAND_FPA_SHIFT) | fsa);
196 * onenand_buffer_address - [DEFAULT] Get buffer address
197 * @param dataram1 DataRAM index
198 * @param sectors the sector address
199 * @param count the number of sectors
200 * @return the start buffer value
202 * Setup Start Buffer Register (F200h)
204 static int onenand_buffer_address(int dataram1, int sectors, int count)
208 /* BufferRAM Sector Address */
209 bsa = sectors & ONENAND_BSA_MASK;
212 bsa |= ONENAND_BSA_DATARAM1; /* DataRAM1 */
214 bsa |= ONENAND_BSA_DATARAM0; /* DataRAM0 */
216 /* BufferRAM Sector Count */
217 bsc = count & ONENAND_BSC_MASK;
219 return ((bsa << ONENAND_BSA_SHIFT) | bsc);
223 * flexonenand_block - Return block number for flash address
224 * @param this - OneNAND device structure
225 * @param addr - Address for which block number is needed
227 static unsigned int flexonenand_block(struct onenand_chip *this, loff_t addr)
229 unsigned int boundary, blk, die = 0;
231 if (ONENAND_IS_DDP(this) && addr >= this->diesize[0]) {
233 addr -= this->diesize[0];
236 boundary = this->boundary[die];
238 blk = addr >> (this->erase_shift - 1);
240 blk = (blk + boundary + 1) >> 1;
242 blk += die ? this->density_mask : 0;
246 unsigned int onenand_block(struct onenand_chip *this, loff_t addr)
248 if (!FLEXONENAND(this))
249 return addr >> this->erase_shift;
250 return flexonenand_block(this, addr);
254 * flexonenand_addr - Return address of the block
255 * @this: OneNAND device structure
256 * @block: Block number on Flex-OneNAND
258 * Return address of the block
260 static loff_t flexonenand_addr(struct onenand_chip *this, int block)
263 int die = 0, boundary;
265 if (ONENAND_IS_DDP(this) && block >= this->density_mask) {
266 block -= this->density_mask;
268 ofs = this->diesize[0];
271 boundary = this->boundary[die];
272 ofs += (loff_t) block << (this->erase_shift - 1);
273 if (block > (boundary + 1))
274 ofs += (loff_t) (block - boundary - 1)
275 << (this->erase_shift - 1);
279 loff_t onenand_addr(struct onenand_chip *this, int block)
281 if (!FLEXONENAND(this))
282 return (loff_t) block << this->erase_shift;
283 return flexonenand_addr(this, block);
287 * flexonenand_region - [Flex-OneNAND] Return erase region of addr
288 * @param mtd MTD device structure
289 * @param addr address whose erase region needs to be identified
291 int flexonenand_region(struct mtd_info *mtd, loff_t addr)
295 for (i = 0; i < mtd->numeraseregions; i++)
296 if (addr < mtd->eraseregions[i].offset)
302 * onenand_get_density - [DEFAULT] Get OneNAND density
303 * @param dev_id OneNAND device ID
305 * Get OneNAND density from device ID
307 static inline int onenand_get_density(int dev_id)
309 int density = dev_id >> ONENAND_DEVICE_DENSITY_SHIFT;
310 return (density & ONENAND_DEVICE_DENSITY_MASK);
314 * onenand_command - [DEFAULT] Send command to OneNAND device
315 * @param mtd MTD device structure
316 * @param cmd the command to be sent
317 * @param addr offset to read from or write to
318 * @param len number of bytes to read or write
320 * Send command to OneNAND device. This function is used for middle/large page
321 * devices (1KB/2KB Bytes per page)
323 static int onenand_command(struct mtd_info *mtd, int cmd, loff_t addr,
326 struct onenand_chip *this = mtd->priv;
330 /* Now we use page size operation */
331 int sectors = 0, count = 0;
333 /* Address translation */
335 case ONENAND_CMD_UNLOCK:
336 case ONENAND_CMD_LOCK:
337 case ONENAND_CMD_LOCK_TIGHT:
338 case ONENAND_CMD_UNLOCK_ALL:
343 case FLEXONENAND_CMD_PI_ACCESS:
344 /* addr contains die index */
345 block = addr * this->density_mask;
349 case ONENAND_CMD_ERASE:
350 case ONENAND_CMD_BUFFERRAM:
351 block = onenand_block(this, addr);
355 case FLEXONENAND_CMD_READ_PI:
356 cmd = ONENAND_CMD_READ;
357 block = addr * this->density_mask;
362 block = onenand_block(this, addr);
364 - onenand_addr(this, block)) >> this->page_shift;
365 page &= this->page_mask;
369 /* NOTE: The setting order of the registers is very important! */
370 if (cmd == ONENAND_CMD_BUFFERRAM) {
371 /* Select DataRAM for DDP */
372 value = onenand_bufferram_address(this, block);
373 this->write_word(value,
374 this->base + ONENAND_REG_START_ADDRESS2);
376 if (ONENAND_IS_4KB_PAGE(this))
377 ONENAND_SET_BUFFERRAM0(this);
379 /* Switch to the next data buffer */
380 ONENAND_SET_NEXT_BUFFERRAM(this);
386 /* Write 'DFS, FBA' of Flash */
387 value = onenand_block_address(this, block);
388 this->write_word(value,
389 this->base + ONENAND_REG_START_ADDRESS1);
391 /* Select DataRAM for DDP */
392 value = onenand_bufferram_address(this, block);
393 this->write_word(value,
394 this->base + ONENAND_REG_START_ADDRESS2);
401 case FLEXONENAND_CMD_RECOVER_LSB:
402 case ONENAND_CMD_READ:
403 case ONENAND_CMD_READOOB:
404 if (ONENAND_IS_4KB_PAGE(this))
405 dataram = ONENAND_SET_BUFFERRAM0(this);
407 dataram = ONENAND_SET_NEXT_BUFFERRAM(this);
412 dataram = ONENAND_CURRENT_BUFFERRAM(this);
416 /* Write 'FPA, FSA' of Flash */
417 value = onenand_page_address(page, sectors);
418 this->write_word(value,
419 this->base + ONENAND_REG_START_ADDRESS8);
421 /* Write 'BSA, BSC' of DataRAM */
422 value = onenand_buffer_address(dataram, sectors, count);
423 this->write_word(value, this->base + ONENAND_REG_START_BUFFER);
426 /* Interrupt clear */
427 this->write_word(ONENAND_INT_CLEAR, this->base + ONENAND_REG_INTERRUPT);
429 this->write_word(cmd, this->base + ONENAND_REG_COMMAND);
435 * onenand_read_ecc - return ecc status
436 * @param this onenand chip structure
438 static int onenand_read_ecc(struct onenand_chip *this)
442 if (!FLEXONENAND(this))
443 return this->read_word(this->base + ONENAND_REG_ECC_STATUS);
445 for (i = 0; i < 4; i++) {
446 ecc = this->read_word(this->base
447 + ((ONENAND_REG_ECC_STATUS + i) << 1));
450 if (ecc & FLEXONENAND_UNCORRECTABLE_ERROR)
451 return ONENAND_ECC_2BIT_ALL;
458 * onenand_wait - [DEFAULT] wait until the command is done
459 * @param mtd MTD device structure
460 * @param state state to select the max. timeout value
462 * Wait for command done. This applies to all OneNAND command
463 * Read can take up to 30us, erase up to 2ms and program up to 350us
464 * according to general OneNAND specs
466 static int onenand_wait(struct mtd_info *mtd, int state)
468 struct onenand_chip *this = mtd->priv;
469 unsigned int flags = ONENAND_INT_MASTER;
470 unsigned int interrupt = 0;
474 interrupt = this->read_word(this->base + ONENAND_REG_INTERRUPT);
475 if (interrupt & flags)
479 ctrl = this->read_word(this->base + ONENAND_REG_CTRL_STATUS);
481 if (interrupt & ONENAND_INT_READ) {
482 int ecc = onenand_read_ecc(this);
483 if (ecc & ONENAND_ECC_2BIT_ALL) {
484 printk("onenand_wait: ECC error = 0x%04x\n", ecc);
489 if (ctrl & ONENAND_CTRL_ERROR) {
490 printk("onenand_wait: controller error = 0x%04x\n", ctrl);
491 if (ctrl & ONENAND_CTRL_LOCK)
492 printk("onenand_wait: it's locked error = 0x%04x\n",
503 * onenand_bufferram_offset - [DEFAULT] BufferRAM offset
504 * @param mtd MTD data structure
505 * @param area BufferRAM area
506 * @return offset given area
508 * Return BufferRAM offset given area
510 static inline int onenand_bufferram_offset(struct mtd_info *mtd, int area)
512 struct onenand_chip *this = mtd->priv;
514 if (ONENAND_CURRENT_BUFFERRAM(this)) {
515 if (area == ONENAND_DATARAM)
516 return mtd->writesize;
517 if (area == ONENAND_SPARERAM)
525 * onenand_read_bufferram - [OneNAND Interface] Read the bufferram area
526 * @param mtd MTD data structure
527 * @param area BufferRAM area
528 * @param buffer the databuffer to put/get data
529 * @param offset offset to read from or write to
530 * @param count number of bytes to read/write
532 * Read the BufferRAM area
534 static int onenand_read_bufferram(struct mtd_info *mtd, loff_t addr, int area,
535 unsigned char *buffer, int offset,
538 struct onenand_chip *this = mtd->priv;
539 void __iomem *bufferram;
541 bufferram = this->base + area;
542 bufferram += onenand_bufferram_offset(mtd, area);
544 memcpy_16(buffer, bufferram + offset, count);
550 * onenand_sync_read_bufferram - [OneNAND Interface] Read the bufferram area with Sync. Burst mode
551 * @param mtd MTD data structure
552 * @param area BufferRAM area
553 * @param buffer the databuffer to put/get data
554 * @param offset offset to read from or write to
555 * @param count number of bytes to read/write
557 * Read the BufferRAM area with Sync. Burst Mode
559 static int onenand_sync_read_bufferram(struct mtd_info *mtd, loff_t addr, int area,
560 unsigned char *buffer, int offset,
563 struct onenand_chip *this = mtd->priv;
564 void __iomem *bufferram;
566 bufferram = this->base + area;
567 bufferram += onenand_bufferram_offset(mtd, area);
569 this->mmcontrol(mtd, ONENAND_SYS_CFG1_SYNC_READ);
571 memcpy_16(buffer, bufferram + offset, count);
573 this->mmcontrol(mtd, 0);
579 * onenand_write_bufferram - [OneNAND Interface] Write the bufferram area
580 * @param mtd MTD data structure
581 * @param area BufferRAM area
582 * @param buffer the databuffer to put/get data
583 * @param offset offset to read from or write to
584 * @param count number of bytes to read/write
586 * Write the BufferRAM area
588 static int onenand_write_bufferram(struct mtd_info *mtd, loff_t addr, int area,
589 const unsigned char *buffer, int offset,
592 struct onenand_chip *this = mtd->priv;
593 void __iomem *bufferram;
595 bufferram = this->base + area;
596 bufferram += onenand_bufferram_offset(mtd, area);
598 memcpy_16(bufferram + offset, buffer, count);
604 * onenand_get_2x_blockpage - [GENERIC] Get blockpage at 2x program mode
605 * @param mtd MTD data structure
606 * @param addr address to check
607 * @return blockpage address
609 * Get blockpage address at 2x program mode
611 static int onenand_get_2x_blockpage(struct mtd_info *mtd, loff_t addr)
613 struct onenand_chip *this = mtd->priv;
614 int blockpage, block, page;
616 /* Calculate the even block number */
617 block = (int) (addr >> this->erase_shift) & ~1;
618 /* Is it the odd plane? */
619 if (addr & this->writesize)
621 page = (int) (addr >> (this->page_shift + 1)) & this->page_mask;
622 blockpage = (block << 7) | page;
628 * onenand_check_bufferram - [GENERIC] Check BufferRAM information
629 * @param mtd MTD data structure
630 * @param addr address to check
631 * @return 1 if there are valid data, otherwise 0
633 * Check bufferram if there is data we required
635 static int onenand_check_bufferram(struct mtd_info *mtd, loff_t addr)
637 struct onenand_chip *this = mtd->priv;
638 int blockpage, found = 0;
641 if (ONENAND_IS_2PLANE(this))
642 blockpage = onenand_get_2x_blockpage(mtd, addr);
644 blockpage = (int) (addr >> this->page_shift);
646 /* Is there valid data? */
647 i = ONENAND_CURRENT_BUFFERRAM(this);
648 if (this->bufferram[i].blockpage == blockpage)
651 /* Check another BufferRAM */
652 i = ONENAND_NEXT_BUFFERRAM(this);
653 if (this->bufferram[i].blockpage == blockpage) {
654 ONENAND_SET_NEXT_BUFFERRAM(this);
659 if (found && ONENAND_IS_DDP(this)) {
660 /* Select DataRAM for DDP */
661 int block = onenand_block(this, addr);
662 int value = onenand_bufferram_address(this, block);
663 this->write_word(value, this->base + ONENAND_REG_START_ADDRESS2);
670 * onenand_update_bufferram - [GENERIC] Update BufferRAM information
671 * @param mtd MTD data structure
672 * @param addr address to update
673 * @param valid valid flag
675 * Update BufferRAM information
677 static int onenand_update_bufferram(struct mtd_info *mtd, loff_t addr,
680 struct onenand_chip *this = mtd->priv;
684 if (ONENAND_IS_2PLANE(this))
685 blockpage = onenand_get_2x_blockpage(mtd, addr);
687 blockpage = (int)(addr >> this->page_shift);
689 /* Invalidate another BufferRAM */
690 i = ONENAND_NEXT_BUFFERRAM(this);
691 if (this->bufferram[i].blockpage == blockpage)
692 this->bufferram[i].blockpage = -1;
694 /* Update BufferRAM */
695 i = ONENAND_CURRENT_BUFFERRAM(this);
697 this->bufferram[i].blockpage = blockpage;
699 this->bufferram[i].blockpage = -1;
705 * onenand_invalidate_bufferram - [GENERIC] Invalidate BufferRAM information
706 * @param mtd MTD data structure
707 * @param addr start address to invalidate
708 * @param len length to invalidate
710 * Invalidate BufferRAM information
712 static void onenand_invalidate_bufferram(struct mtd_info *mtd, loff_t addr,
715 struct onenand_chip *this = mtd->priv;
717 loff_t end_addr = addr + len;
719 /* Invalidate BufferRAM */
720 for (i = 0; i < MAX_BUFFERRAM; i++) {
721 loff_t buf_addr = this->bufferram[i].blockpage << this->page_shift;
723 if (buf_addr >= addr && buf_addr < end_addr)
724 this->bufferram[i].blockpage = -1;
729 * onenand_get_device - [GENERIC] Get chip for selected access
730 * @param mtd MTD device structure
731 * @param new_state the state which is requested
733 * Get the device and lock it for exclusive access
735 static void onenand_get_device(struct mtd_info *mtd, int new_state)
741 * onenand_release_device - [GENERIC] release chip
742 * @param mtd MTD device structure
744 * Deselect, release chip lock and wake up anyone waiting on the device
746 static void onenand_release_device(struct mtd_info *mtd)
752 * onenand_transfer_auto_oob - [INTERN] oob auto-placement transfer
753 * @param mtd MTD device structure
754 * @param buf destination address
755 * @param column oob offset to read from
756 * @param thislen oob length to read
758 static int onenand_transfer_auto_oob(struct mtd_info *mtd, uint8_t *buf,
759 int column, int thislen)
761 struct onenand_chip *this = mtd->priv;
762 struct nand_oobfree *free;
763 int readcol = column;
764 int readend = column + thislen;
767 uint8_t *oob_buf = this->oob_buf;
769 free = this->ecclayout->oobfree;
770 for (i = 0; i < MTD_MAX_OOBFREE_ENTRIES_LARGE && free->length;
772 if (readcol >= lastgap)
773 readcol += free->offset - lastgap;
774 if (readend >= lastgap)
775 readend += free->offset - lastgap;
776 lastgap = free->offset + free->length;
778 this->read_bufferram(mtd, 0, ONENAND_SPARERAM, oob_buf, 0, mtd->oobsize);
779 free = this->ecclayout->oobfree;
780 for (i = 0; i < MTD_MAX_OOBFREE_ENTRIES_LARGE && free->length;
782 int free_end = free->offset + free->length;
783 if (free->offset < readend && free_end > readcol) {
784 int st = max_t(int,free->offset,readcol);
785 int ed = min_t(int,free_end,readend);
787 memcpy(buf, oob_buf + st, n);
789 } else if (column == 0)
796 * onenand_recover_lsb - [Flex-OneNAND] Recover LSB page data
797 * @param mtd MTD device structure
798 * @param addr address to recover
799 * @param status return value from onenand_wait
801 * MLC NAND Flash cell has paired pages - LSB page and MSB page. LSB page has
802 * lower page address and MSB page has higher page address in paired pages.
803 * If power off occurs during MSB page program, the paired LSB page data can
804 * become corrupt. LSB page recovery read is a way to read LSB page though page
805 * data are corrupted. When uncorrectable error occurs as a result of LSB page
806 * read after power up, issue LSB page recovery read.
808 static int onenand_recover_lsb(struct mtd_info *mtd, loff_t addr, int status)
810 struct onenand_chip *this = mtd->priv;
813 /* Recovery is only for Flex-OneNAND */
814 if (!FLEXONENAND(this))
817 /* check if we failed due to uncorrectable error */
818 if (!mtd_is_eccerr(status) && status != ONENAND_BBT_READ_ECC_ERROR)
821 /* check if address lies in MLC region */
822 i = flexonenand_region(mtd, addr);
823 if (mtd->eraseregions[i].erasesize < (1 << this->erase_shift))
826 printk("onenand_recover_lsb:"
827 "Attempting to recover from uncorrectable read\n");
829 /* Issue the LSB page recovery command */
830 this->command(mtd, FLEXONENAND_CMD_RECOVER_LSB, addr, this->writesize);
831 return this->wait(mtd, FL_READING);
835 * onenand_read_ops_nolock - [OneNAND Interface] OneNAND read main and/or out-of-band
836 * @param mtd MTD device structure
837 * @param from offset to read from
838 * @param ops oob operation description structure
840 * OneNAND read main and/or out-of-band data
842 static int onenand_read_ops_nolock(struct mtd_info *mtd, loff_t from,
843 struct mtd_oob_ops *ops)
845 struct onenand_chip *this = mtd->priv;
846 struct mtd_ecc_stats stats;
847 size_t len = ops->len;
848 size_t ooblen = ops->ooblen;
849 u_char *buf = ops->datbuf;
850 u_char *oobbuf = ops->oobbuf;
851 int read = 0, column, thislen;
852 int oobread = 0, oobcolumn, thisooblen, oobsize;
853 int ret = 0, boundary = 0;
854 int writesize = this->writesize;
856 MTDDEBUG(MTD_DEBUG_LEVEL3, "onenand_read_ops_nolock: from = 0x%08x, len = %i\n", (unsigned int) from, (int) len);
858 if (ops->mode == MTD_OPS_AUTO_OOB)
859 oobsize = this->ecclayout->oobavail;
861 oobsize = mtd->oobsize;
863 oobcolumn = from & (mtd->oobsize - 1);
865 /* Do not allow reads past end of device */
866 if ((from + len) > mtd->size) {
867 printk(KERN_ERR "onenand_read_ops_nolock: Attempt read beyond end of device\n");
873 stats = mtd->ecc_stats;
875 /* Read-while-load method */
876 /* Note: We can't use this feature in MLC */
878 /* Do first load to bufferRAM */
880 if (!onenand_check_bufferram(mtd, from)) {
881 this->main_buf = buf;
882 this->command(mtd, ONENAND_CMD_READ, from, writesize);
883 ret = this->wait(mtd, FL_READING);
885 ret = onenand_recover_lsb(mtd, from, ret);
886 onenand_update_bufferram(mtd, from, !ret);
892 thislen = min_t(int, writesize, len - read);
893 column = from & (writesize - 1);
894 if (column + thislen > writesize)
895 thislen = writesize - column;
898 /* If there is more to load then start next load */
900 if (!ONENAND_IS_4KB_PAGE(this) && read + thislen < len) {
901 this->main_buf = buf + thislen;
902 this->command(mtd, ONENAND_CMD_READ, from, writesize);
904 * Chip boundary handling in DDP
905 * Now we issued chip 1 read and pointed chip 1
906 * bufferam so we have to point chip 0 bufferam.
908 if (ONENAND_IS_DDP(this) &&
909 unlikely(from == (this->chipsize >> 1))) {
910 this->write_word(ONENAND_DDP_CHIP0, this->base + ONENAND_REG_START_ADDRESS2);
914 ONENAND_SET_PREV_BUFFERRAM(this);
917 /* While load is going, read from last bufferRAM */
918 this->read_bufferram(mtd, from - thislen, ONENAND_DATARAM, buf, column, thislen);
920 /* Read oob area if needed */
922 thisooblen = oobsize - oobcolumn;
923 thisooblen = min_t(int, thisooblen, ooblen - oobread);
925 if (ops->mode == MTD_OPS_AUTO_OOB)
926 onenand_transfer_auto_oob(mtd, oobbuf, oobcolumn, thisooblen);
928 this->read_bufferram(mtd, 0, ONENAND_SPARERAM, oobbuf, oobcolumn, thisooblen);
929 oobread += thisooblen;
930 oobbuf += thisooblen;
934 if (ONENAND_IS_4KB_PAGE(this) && (read + thislen < len)) {
935 this->command(mtd, ONENAND_CMD_READ, from, writesize);
936 ret = this->wait(mtd, FL_READING);
938 ret = onenand_recover_lsb(mtd, from, ret);
939 onenand_update_bufferram(mtd, from, !ret);
940 if (mtd_is_eccerr(ret))
944 /* See if we are done */
948 /* Set up for next read from bufferRAM */
949 if (unlikely(boundary))
950 this->write_word(ONENAND_DDP_CHIP1, this->base + ONENAND_REG_START_ADDRESS2);
951 if (!ONENAND_IS_4KB_PAGE(this))
952 ONENAND_SET_NEXT_BUFFERRAM(this);
954 thislen = min_t(int, writesize, len - read);
957 if (!ONENAND_IS_4KB_PAGE(this)) {
958 /* Now wait for load */
959 ret = this->wait(mtd, FL_READING);
960 onenand_update_bufferram(mtd, from, !ret);
961 if (mtd_is_eccerr(ret))
967 * Return success, if no ECC failures, else -EBADMSG
968 * fs driver will take care of that, because
969 * retlen == desired len and result == -EBADMSG
972 ops->oobretlen = oobread;
977 if (mtd->ecc_stats.failed - stats.failed)
980 /* return max bitflips per ecc step; ONENANDs correct 1 bit only */
981 return mtd->ecc_stats.corrected != stats.corrected ? 1 : 0;
985 * onenand_read_oob_nolock - [MTD Interface] OneNAND read out-of-band
986 * @param mtd MTD device structure
987 * @param from offset to read from
988 * @param ops oob operation description structure
990 * OneNAND read out-of-band data from the spare area
992 static int onenand_read_oob_nolock(struct mtd_info *mtd, loff_t from,
993 struct mtd_oob_ops *ops)
995 struct onenand_chip *this = mtd->priv;
996 struct mtd_ecc_stats stats;
997 int read = 0, thislen, column, oobsize;
998 size_t len = ops->ooblen;
999 unsigned int mode = ops->mode;
1000 u_char *buf = ops->oobbuf;
1001 int ret = 0, readcmd;
1003 from += ops->ooboffs;
1005 MTDDEBUG(MTD_DEBUG_LEVEL3, "onenand_read_oob_nolock: from = 0x%08x, len = %i\n", (unsigned int) from, (int) len);
1007 /* Initialize return length value */
1010 if (mode == MTD_OPS_AUTO_OOB)
1011 oobsize = this->ecclayout->oobavail;
1013 oobsize = mtd->oobsize;
1015 column = from & (mtd->oobsize - 1);
1017 if (unlikely(column >= oobsize)) {
1018 printk(KERN_ERR "onenand_read_oob_nolock: Attempted to start read outside oob\n");
1022 /* Do not allow reads past end of device */
1023 if (unlikely(from >= mtd->size ||
1024 column + len > ((mtd->size >> this->page_shift) -
1025 (from >> this->page_shift)) * oobsize)) {
1026 printk(KERN_ERR "onenand_read_oob_nolock: Attempted to read beyond end of device\n");
1030 stats = mtd->ecc_stats;
1032 readcmd = ONENAND_IS_4KB_PAGE(this) ?
1033 ONENAND_CMD_READ : ONENAND_CMD_READOOB;
1035 while (read < len) {
1036 thislen = oobsize - column;
1037 thislen = min_t(int, thislen, len);
1039 this->spare_buf = buf;
1040 this->command(mtd, readcmd, from, mtd->oobsize);
1042 onenand_update_bufferram(mtd, from, 0);
1044 ret = this->wait(mtd, FL_READING);
1046 ret = onenand_recover_lsb(mtd, from, ret);
1048 if (ret && ret != -EBADMSG) {
1049 printk(KERN_ERR "onenand_read_oob_nolock: read failed = 0x%x\n", ret);
1053 if (mode == MTD_OPS_AUTO_OOB)
1054 onenand_transfer_auto_oob(mtd, buf, column, thislen);
1056 this->read_bufferram(mtd, 0, ONENAND_SPARERAM, buf, column, thislen);
1068 from += mtd->writesize;
1073 ops->oobretlen = read;
1078 if (mtd->ecc_stats.failed - stats.failed)
1085 * onenand_read - [MTD Interface] MTD compability function for onenand_read_ecc
1086 * @param mtd MTD device structure
1087 * @param from offset to read from
1088 * @param len number of bytes to read
1089 * @param retlen pointer to variable to store the number of read bytes
1090 * @param buf the databuffer to put data
1092 * This function simply calls onenand_read_ecc with oob buffer and oobsel = NULL
1094 int onenand_read(struct mtd_info *mtd, loff_t from, size_t len,
1095 size_t * retlen, u_char * buf)
1097 struct mtd_oob_ops ops = {
1105 onenand_get_device(mtd, FL_READING);
1106 ret = onenand_read_ops_nolock(mtd, from, &ops);
1107 onenand_release_device(mtd);
1109 *retlen = ops.retlen;
1114 * onenand_read_oob - [MTD Interface] OneNAND read out-of-band
1115 * @param mtd MTD device structure
1116 * @param from offset to read from
1117 * @param ops oob operations description structure
1119 * OneNAND main and/or out-of-band
1121 int onenand_read_oob(struct mtd_info *mtd, loff_t from,
1122 struct mtd_oob_ops *ops)
1126 switch (ops->mode) {
1127 case MTD_OPS_PLACE_OOB:
1128 case MTD_OPS_AUTO_OOB:
1131 /* Not implemented yet */
1136 onenand_get_device(mtd, FL_READING);
1138 ret = onenand_read_ops_nolock(mtd, from, ops);
1140 ret = onenand_read_oob_nolock(mtd, from, ops);
1141 onenand_release_device(mtd);
1147 * onenand_bbt_wait - [DEFAULT] wait until the command is done
1148 * @param mtd MTD device structure
1149 * @param state state to select the max. timeout value
1151 * Wait for command done.
1153 static int onenand_bbt_wait(struct mtd_info *mtd, int state)
1155 struct onenand_chip *this = mtd->priv;
1156 unsigned int flags = ONENAND_INT_MASTER;
1157 unsigned int interrupt;
1161 interrupt = this->read_word(this->base + ONENAND_REG_INTERRUPT);
1162 if (interrupt & flags)
1166 /* To get correct interrupt status in timeout case */
1167 interrupt = this->read_word(this->base + ONENAND_REG_INTERRUPT);
1168 ctrl = this->read_word(this->base + ONENAND_REG_CTRL_STATUS);
1170 if (interrupt & ONENAND_INT_READ) {
1171 int ecc = onenand_read_ecc(this);
1172 if (ecc & ONENAND_ECC_2BIT_ALL) {
1173 printk(KERN_INFO "onenand_bbt_wait: ecc error = 0x%04x"
1174 ", controller = 0x%04x\n", ecc, ctrl);
1175 return ONENAND_BBT_READ_ERROR;
1178 printk(KERN_ERR "onenand_bbt_wait: read timeout!"
1179 "ctrl=0x%04x intr=0x%04x\n", ctrl, interrupt);
1180 return ONENAND_BBT_READ_FATAL_ERROR;
1183 /* Initial bad block case: 0x2400 or 0x0400 */
1184 if (ctrl & ONENAND_CTRL_ERROR) {
1185 printk(KERN_DEBUG "onenand_bbt_wait: controller error = 0x%04x\n", ctrl);
1186 return ONENAND_BBT_READ_ERROR;
1193 * onenand_bbt_read_oob - [MTD Interface] OneNAND read out-of-band for bbt scan
1194 * @param mtd MTD device structure
1195 * @param from offset to read from
1196 * @param ops oob operation description structure
1198 * OneNAND read out-of-band data from the spare area for bbt scan
1200 int onenand_bbt_read_oob(struct mtd_info *mtd, loff_t from,
1201 struct mtd_oob_ops *ops)
1203 struct onenand_chip *this = mtd->priv;
1204 int read = 0, thislen, column;
1205 int ret = 0, readcmd;
1206 size_t len = ops->ooblen;
1207 u_char *buf = ops->oobbuf;
1209 MTDDEBUG(MTD_DEBUG_LEVEL3, "onenand_bbt_read_oob: from = 0x%08x, len = %zi\n", (unsigned int) from, len);
1211 readcmd = ONENAND_IS_4KB_PAGE(this) ?
1212 ONENAND_CMD_READ : ONENAND_CMD_READOOB;
1214 /* Initialize return value */
1217 /* Do not allow reads past end of device */
1218 if (unlikely((from + len) > mtd->size)) {
1219 printk(KERN_ERR "onenand_bbt_read_oob: Attempt read beyond end of device\n");
1220 return ONENAND_BBT_READ_FATAL_ERROR;
1223 /* Grab the lock and see if the device is available */
1224 onenand_get_device(mtd, FL_READING);
1226 column = from & (mtd->oobsize - 1);
1228 while (read < len) {
1230 thislen = mtd->oobsize - column;
1231 thislen = min_t(int, thislen, len);
1233 this->spare_buf = buf;
1234 this->command(mtd, readcmd, from, mtd->oobsize);
1236 onenand_update_bufferram(mtd, from, 0);
1238 ret = this->bbt_wait(mtd, FL_READING);
1240 ret = onenand_recover_lsb(mtd, from, ret);
1245 this->read_bufferram(mtd, 0, ONENAND_SPARERAM, buf, column, thislen);
1254 /* Update Page size */
1255 from += this->writesize;
1260 /* Deselect and wake up anyone waiting on the device */
1261 onenand_release_device(mtd);
1263 ops->oobretlen = read;
1268 #ifdef CONFIG_MTD_ONENAND_VERIFY_WRITE
1270 * onenand_verify_oob - [GENERIC] verify the oob contents after a write
1271 * @param mtd MTD device structure
1272 * @param buf the databuffer to verify
1273 * @param to offset to read from
1275 static int onenand_verify_oob(struct mtd_info *mtd, const u_char *buf, loff_t to)
1277 struct onenand_chip *this = mtd->priv;
1278 u_char *oob_buf = this->oob_buf;
1279 int status, i, readcmd;
1281 readcmd = ONENAND_IS_4KB_PAGE(this) ?
1282 ONENAND_CMD_READ : ONENAND_CMD_READOOB;
1284 this->command(mtd, readcmd, to, mtd->oobsize);
1285 onenand_update_bufferram(mtd, to, 0);
1286 status = this->wait(mtd, FL_READING);
1290 this->read_bufferram(mtd, 0, ONENAND_SPARERAM, oob_buf, 0, mtd->oobsize);
1291 for (i = 0; i < mtd->oobsize; i++)
1292 if (buf[i] != 0xFF && buf[i] != oob_buf[i])
1299 * onenand_verify - [GENERIC] verify the chip contents after a write
1300 * @param mtd MTD device structure
1301 * @param buf the databuffer to verify
1302 * @param addr offset to read from
1303 * @param len number of bytes to read and compare
1305 static int onenand_verify(struct mtd_info *mtd, const u_char *buf, loff_t addr, size_t len)
1307 struct onenand_chip *this = mtd->priv;
1308 void __iomem *dataram;
1310 int thislen, column;
1313 thislen = min_t(int, this->writesize, len);
1314 column = addr & (this->writesize - 1);
1315 if (column + thislen > this->writesize)
1316 thislen = this->writesize - column;
1318 this->command(mtd, ONENAND_CMD_READ, addr, this->writesize);
1320 onenand_update_bufferram(mtd, addr, 0);
1322 ret = this->wait(mtd, FL_READING);
1326 onenand_update_bufferram(mtd, addr, 1);
1328 dataram = this->base + ONENAND_DATARAM;
1329 dataram += onenand_bufferram_offset(mtd, ONENAND_DATARAM);
1331 if (memcmp(buf, dataram + column, thislen))
1342 #define onenand_verify(...) (0)
1343 #define onenand_verify_oob(...) (0)
1346 #define NOTALIGNED(x) ((x & (this->subpagesize - 1)) != 0)
1349 * onenand_fill_auto_oob - [INTERN] oob auto-placement transfer
1350 * @param mtd MTD device structure
1351 * @param oob_buf oob buffer
1352 * @param buf source address
1353 * @param column oob offset to write to
1354 * @param thislen oob length to write
1356 static int onenand_fill_auto_oob(struct mtd_info *mtd, u_char *oob_buf,
1357 const u_char *buf, int column, int thislen)
1359 struct onenand_chip *this = mtd->priv;
1360 struct nand_oobfree *free;
1361 int writecol = column;
1362 int writeend = column + thislen;
1366 free = this->ecclayout->oobfree;
1367 for (i = 0; i < MTD_MAX_OOBFREE_ENTRIES_LARGE && free->length;
1369 if (writecol >= lastgap)
1370 writecol += free->offset - lastgap;
1371 if (writeend >= lastgap)
1372 writeend += free->offset - lastgap;
1373 lastgap = free->offset + free->length;
1375 free = this->ecclayout->oobfree;
1376 for (i = 0; i < MTD_MAX_OOBFREE_ENTRIES_LARGE && free->length;
1378 int free_end = free->offset + free->length;
1379 if (free->offset < writeend && free_end > writecol) {
1380 int st = max_t(int,free->offset,writecol);
1381 int ed = min_t(int,free_end,writeend);
1383 memcpy(oob_buf + st, buf, n);
1385 } else if (column == 0)
1392 * onenand_write_ops_nolock - [OneNAND Interface] write main and/or out-of-band
1393 * @param mtd MTD device structure
1394 * @param to offset to write to
1395 * @param ops oob operation description structure
1397 * Write main and/or oob with ECC
1399 static int onenand_write_ops_nolock(struct mtd_info *mtd, loff_t to,
1400 struct mtd_oob_ops *ops)
1402 struct onenand_chip *this = mtd->priv;
1403 int written = 0, column, thislen, subpage;
1404 int oobwritten = 0, oobcolumn, thisooblen, oobsize;
1405 size_t len = ops->len;
1406 size_t ooblen = ops->ooblen;
1407 const u_char *buf = ops->datbuf;
1408 const u_char *oob = ops->oobbuf;
1412 MTDDEBUG(MTD_DEBUG_LEVEL3, "onenand_write_ops_nolock: to = 0x%08x, len = %i\n", (unsigned int) to, (int) len);
1414 /* Initialize retlen, in case of early exit */
1418 /* Reject writes, which are not page aligned */
1419 if (unlikely(NOTALIGNED(to) || NOTALIGNED(len))) {
1420 printk(KERN_ERR "onenand_write_ops_nolock: Attempt to write not page aligned data\n");
1424 if (ops->mode == MTD_OPS_AUTO_OOB)
1425 oobsize = this->ecclayout->oobavail;
1427 oobsize = mtd->oobsize;
1429 oobcolumn = to & (mtd->oobsize - 1);
1431 column = to & (mtd->writesize - 1);
1433 /* Loop until all data write */
1434 while (written < len) {
1435 u_char *wbuf = (u_char *) buf;
1437 thislen = min_t(int, mtd->writesize - column, len - written);
1438 thisooblen = min_t(int, oobsize - oobcolumn, ooblen - oobwritten);
1440 this->command(mtd, ONENAND_CMD_BUFFERRAM, to, thislen);
1442 /* Partial page write */
1443 subpage = thislen < mtd->writesize;
1445 memset(this->page_buf, 0xff, mtd->writesize);
1446 memcpy(this->page_buf + column, buf, thislen);
1447 wbuf = this->page_buf;
1450 this->write_bufferram(mtd, to, ONENAND_DATARAM, wbuf, 0, mtd->writesize);
1453 oobbuf = this->oob_buf;
1455 /* We send data to spare ram with oobsize
1456 * * to prevent byte access */
1457 memset(oobbuf, 0xff, mtd->oobsize);
1458 if (ops->mode == MTD_OPS_AUTO_OOB)
1459 onenand_fill_auto_oob(mtd, oobbuf, oob, oobcolumn, thisooblen);
1461 memcpy(oobbuf + oobcolumn, oob, thisooblen);
1463 oobwritten += thisooblen;
1467 oobbuf = (u_char *) ffchars;
1469 this->write_bufferram(mtd, 0, ONENAND_SPARERAM, oobbuf, 0, mtd->oobsize);
1471 this->command(mtd, ONENAND_CMD_PROG, to, mtd->writesize);
1473 ret = this->wait(mtd, FL_WRITING);
1475 /* In partial page write we don't update bufferram */
1476 onenand_update_bufferram(mtd, to, !ret && !subpage);
1477 if (ONENAND_IS_2PLANE(this)) {
1478 ONENAND_SET_BUFFERRAM1(this);
1479 onenand_update_bufferram(mtd, to + this->writesize, !ret && !subpage);
1483 printk(KERN_ERR "onenand_write_ops_nolock: write filaed %d\n", ret);
1487 /* Only check verify write turn on */
1488 ret = onenand_verify(mtd, buf, to, thislen);
1490 printk(KERN_ERR "onenand_write_ops_nolock: verify failed %d\n", ret);
1504 ops->retlen = written;
1510 * onenand_write_oob_nolock - [INTERN] OneNAND write out-of-band
1511 * @param mtd MTD device structure
1512 * @param to offset to write to
1513 * @param len number of bytes to write
1514 * @param retlen pointer to variable to store the number of written bytes
1515 * @param buf the data to write
1516 * @param mode operation mode
1518 * OneNAND write out-of-band
1520 static int onenand_write_oob_nolock(struct mtd_info *mtd, loff_t to,
1521 struct mtd_oob_ops *ops)
1523 struct onenand_chip *this = mtd->priv;
1524 int column, ret = 0, oobsize;
1525 int written = 0, oobcmd;
1527 size_t len = ops->ooblen;
1528 const u_char *buf = ops->oobbuf;
1529 unsigned int mode = ops->mode;
1533 MTDDEBUG(MTD_DEBUG_LEVEL3, "onenand_write_oob_nolock: to = 0x%08x, len = %i\n", (unsigned int) to, (int) len);
1535 /* Initialize retlen, in case of early exit */
1538 if (mode == MTD_OPS_AUTO_OOB)
1539 oobsize = this->ecclayout->oobavail;
1541 oobsize = mtd->oobsize;
1543 column = to & (mtd->oobsize - 1);
1545 if (unlikely(column >= oobsize)) {
1546 printk(KERN_ERR "onenand_write_oob_nolock: Attempted to start write outside oob\n");
1550 /* For compatibility with NAND: Do not allow write past end of page */
1551 if (unlikely(column + len > oobsize)) {
1552 printk(KERN_ERR "onenand_write_oob_nolock: "
1553 "Attempt to write past end of page\n");
1557 /* Do not allow reads past end of device */
1558 if (unlikely(to >= mtd->size ||
1559 column + len > ((mtd->size >> this->page_shift) -
1560 (to >> this->page_shift)) * oobsize)) {
1561 printk(KERN_ERR "onenand_write_oob_nolock: Attempted to write past end of device\n");
1565 oobbuf = this->oob_buf;
1567 oobcmd = ONENAND_IS_4KB_PAGE(this) ?
1568 ONENAND_CMD_PROG : ONENAND_CMD_PROGOOB;
1570 /* Loop until all data write */
1571 while (written < len) {
1572 int thislen = min_t(int, oobsize, len - written);
1574 this->command(mtd, ONENAND_CMD_BUFFERRAM, to, mtd->oobsize);
1576 /* We send data to spare ram with oobsize
1577 * to prevent byte access */
1578 memset(oobbuf, 0xff, mtd->oobsize);
1579 if (mode == MTD_OPS_AUTO_OOB)
1580 onenand_fill_auto_oob(mtd, oobbuf, buf, column, thislen);
1582 memcpy(oobbuf + column, buf, thislen);
1583 this->write_bufferram(mtd, 0, ONENAND_SPARERAM, oobbuf, 0, mtd->oobsize);
1585 if (ONENAND_IS_4KB_PAGE(this)) {
1586 /* Set main area of DataRAM to 0xff*/
1587 memset(this->page_buf, 0xff, mtd->writesize);
1588 this->write_bufferram(mtd, 0, ONENAND_DATARAM,
1589 this->page_buf, 0, mtd->writesize);
1592 this->command(mtd, oobcmd, to, mtd->oobsize);
1594 onenand_update_bufferram(mtd, to, 0);
1595 if (ONENAND_IS_2PLANE(this)) {
1596 ONENAND_SET_BUFFERRAM1(this);
1597 onenand_update_bufferram(mtd, to + this->writesize, 0);
1600 ret = this->wait(mtd, FL_WRITING);
1602 printk(KERN_ERR "onenand_write_oob_nolock: write failed %d\n", ret);
1606 ret = onenand_verify_oob(mtd, oobbuf, to);
1608 printk(KERN_ERR "onenand_write_oob_nolock: verify failed %d\n", ret);
1616 to += mtd->writesize;
1621 ops->oobretlen = written;
1627 * onenand_write - [MTD Interface] compability function for onenand_write_ecc
1628 * @param mtd MTD device structure
1629 * @param to offset to write to
1630 * @param len number of bytes to write
1631 * @param retlen pointer to variable to store the number of written bytes
1632 * @param buf the data to write
1636 int onenand_write(struct mtd_info *mtd, loff_t to, size_t len,
1637 size_t * retlen, const u_char * buf)
1639 struct mtd_oob_ops ops = {
1642 .datbuf = (u_char *) buf,
1647 onenand_get_device(mtd, FL_WRITING);
1648 ret = onenand_write_ops_nolock(mtd, to, &ops);
1649 onenand_release_device(mtd);
1651 *retlen = ops.retlen;
1656 * onenand_write_oob - [MTD Interface] OneNAND write out-of-band
1657 * @param mtd MTD device structure
1658 * @param to offset to write to
1659 * @param ops oob operation description structure
1661 * OneNAND write main and/or out-of-band
1663 int onenand_write_oob(struct mtd_info *mtd, loff_t to,
1664 struct mtd_oob_ops *ops)
1668 switch (ops->mode) {
1669 case MTD_OPS_PLACE_OOB:
1670 case MTD_OPS_AUTO_OOB:
1673 /* Not implemented yet */
1678 onenand_get_device(mtd, FL_WRITING);
1680 ret = onenand_write_ops_nolock(mtd, to, ops);
1682 ret = onenand_write_oob_nolock(mtd, to, ops);
1683 onenand_release_device(mtd);
1690 * onenand_block_isbad_nolock - [GENERIC] Check if a block is marked bad
1691 * @param mtd MTD device structure
1692 * @param ofs offset from device start
1693 * @param allowbbt 1, if its allowed to access the bbt area
1695 * Check, if the block is bad, Either by reading the bad block table or
1696 * calling of the scan function.
1698 static int onenand_block_isbad_nolock(struct mtd_info *mtd, loff_t ofs, int allowbbt)
1700 struct onenand_chip *this = mtd->priv;
1701 struct bbm_info *bbm = this->bbm;
1703 /* Return info from the table */
1704 return bbm->isbad_bbt(mtd, ofs, allowbbt);
1709 * onenand_erase - [MTD Interface] erase block(s)
1710 * @param mtd MTD device structure
1711 * @param instr erase instruction
1713 * Erase one ore more blocks
1715 int onenand_erase(struct mtd_info *mtd, struct erase_info *instr)
1717 struct onenand_chip *this = mtd->priv;
1718 unsigned int block_size;
1719 loff_t addr = instr->addr;
1720 unsigned int len = instr->len;
1722 struct mtd_erase_region_info *region = NULL;
1723 unsigned int region_end = 0;
1725 MTDDEBUG(MTD_DEBUG_LEVEL3, "onenand_erase: start = 0x%08x, len = %i\n",
1726 (unsigned int) addr, len);
1728 if (FLEXONENAND(this)) {
1729 /* Find the eraseregion of this address */
1730 i = flexonenand_region(mtd, addr);
1731 region = &mtd->eraseregions[i];
1733 block_size = region->erasesize;
1734 region_end = region->offset
1735 + region->erasesize * region->numblocks;
1737 /* Start address within region must align on block boundary.
1738 * Erase region's start offset is always block start address.
1740 if (unlikely((addr - region->offset) & (block_size - 1))) {
1741 MTDDEBUG(MTD_DEBUG_LEVEL0, "onenand_erase:"
1742 " Unaligned address\n");
1746 block_size = 1 << this->erase_shift;
1748 /* Start address must align on block boundary */
1749 if (unlikely(addr & (block_size - 1))) {
1750 MTDDEBUG(MTD_DEBUG_LEVEL0, "onenand_erase:"
1751 "Unaligned address\n");
1756 /* Length must align on block boundary */
1757 if (unlikely(len & (block_size - 1))) {
1758 MTDDEBUG (MTD_DEBUG_LEVEL0,
1759 "onenand_erase: Length not block aligned\n");
1763 /* Grab the lock and see if the device is available */
1764 onenand_get_device(mtd, FL_ERASING);
1766 /* Loop throught the pages */
1767 instr->state = MTD_ERASING;
1771 /* Check if we have a bad block, we do not erase bad blocks */
1772 if (instr->priv == 0 && onenand_block_isbad_nolock(mtd, addr, 0)) {
1773 printk(KERN_WARNING "onenand_erase: attempt to erase"
1774 " a bad block at addr 0x%08x\n",
1775 (unsigned int) addr);
1776 instr->state = MTD_ERASE_FAILED;
1780 this->command(mtd, ONENAND_CMD_ERASE, addr, block_size);
1782 onenand_invalidate_bufferram(mtd, addr, block_size);
1784 ret = this->wait(mtd, FL_ERASING);
1785 /* Check, if it is write protected */
1788 MTDDEBUG (MTD_DEBUG_LEVEL0, "onenand_erase: "
1789 "Device is write protected!!!\n");
1791 MTDDEBUG (MTD_DEBUG_LEVEL0, "onenand_erase: "
1792 "Failed erase, block %d\n",
1793 onenand_block(this, addr));
1794 instr->state = MTD_ERASE_FAILED;
1795 instr->fail_addr = addr;
1803 if (addr == region_end) {
1808 block_size = region->erasesize;
1809 region_end = region->offset
1810 + region->erasesize * region->numblocks;
1812 if (len & (block_size - 1)) {
1813 /* This has been checked at MTD
1814 * partitioning level. */
1815 printk("onenand_erase: Unaligned address\n");
1821 instr->state = MTD_ERASE_DONE;
1825 ret = instr->state == MTD_ERASE_DONE ? 0 : -EIO;
1826 /* Do call back function */
1828 mtd_erase_callback(instr);
1830 /* Deselect and wake up anyone waiting on the device */
1831 onenand_release_device(mtd);
1837 * onenand_sync - [MTD Interface] sync
1838 * @param mtd MTD device structure
1840 * Sync is actually a wait for chip ready function
1842 void onenand_sync(struct mtd_info *mtd)
1844 MTDDEBUG (MTD_DEBUG_LEVEL3, "onenand_sync: called\n");
1846 /* Grab the lock and see if the device is available */
1847 onenand_get_device(mtd, FL_SYNCING);
1849 /* Release it and go back */
1850 onenand_release_device(mtd);
1854 * onenand_block_isbad - [MTD Interface] Check whether the block at the given offset is bad
1855 * @param mtd MTD device structure
1856 * @param ofs offset relative to mtd start
1858 * Check whether the block is bad
1860 int onenand_block_isbad(struct mtd_info *mtd, loff_t ofs)
1864 /* Check for invalid offset */
1865 if (ofs > mtd->size)
1868 onenand_get_device(mtd, FL_READING);
1869 ret = onenand_block_isbad_nolock(mtd,ofs, 0);
1870 onenand_release_device(mtd);
1875 * onenand_default_block_markbad - [DEFAULT] mark a block bad
1876 * @param mtd MTD device structure
1877 * @param ofs offset from device start
1879 * This is the default implementation, which can be overridden by
1880 * a hardware specific driver.
1882 static int onenand_default_block_markbad(struct mtd_info *mtd, loff_t ofs)
1884 struct onenand_chip *this = mtd->priv;
1885 struct bbm_info *bbm = this->bbm;
1886 u_char buf[2] = {0, 0};
1887 struct mtd_oob_ops ops = {
1888 .mode = MTD_OPS_PLACE_OOB,
1895 /* Get block number */
1896 block = onenand_block(this, ofs);
1898 bbm->bbt[block >> 2] |= 0x01 << ((block & 0x03) << 1);
1900 /* We write two bytes, so we dont have to mess with 16 bit access */
1901 ofs += mtd->oobsize + (bbm->badblockpos & ~0x01);
1902 return onenand_write_oob_nolock(mtd, ofs, &ops);
1906 * onenand_block_markbad - [MTD Interface] Mark the block at the given offset as bad
1907 * @param mtd MTD device structure
1908 * @param ofs offset relative to mtd start
1910 * Mark the block as bad
1912 int onenand_block_markbad(struct mtd_info *mtd, loff_t ofs)
1916 ret = onenand_block_isbad(mtd, ofs);
1918 /* If it was bad already, return success and do nothing */
1924 ret = mtd_block_markbad(mtd, ofs);
1929 * onenand_do_lock_cmd - [OneNAND Interface] Lock or unlock block(s)
1930 * @param mtd MTD device structure
1931 * @param ofs offset relative to mtd start
1932 * @param len number of bytes to lock or unlock
1933 * @param cmd lock or unlock command
1935 * Lock or unlock one or more blocks
1937 static int onenand_do_lock_cmd(struct mtd_info *mtd, loff_t ofs, size_t len, int cmd)
1939 struct onenand_chip *this = mtd->priv;
1940 int start, end, block, value, status;
1942 start = onenand_block(this, ofs);
1943 end = onenand_block(this, ofs + len);
1945 /* Continuous lock scheme */
1946 if (this->options & ONENAND_HAS_CONT_LOCK) {
1947 /* Set start block address */
1948 this->write_word(start,
1949 this->base + ONENAND_REG_START_BLOCK_ADDRESS);
1950 /* Set end block address */
1951 this->write_word(end - 1,
1952 this->base + ONENAND_REG_END_BLOCK_ADDRESS);
1953 /* Write unlock command */
1954 this->command(mtd, cmd, 0, 0);
1956 /* There's no return value */
1957 this->wait(mtd, FL_UNLOCKING);
1960 while (this->read_word(this->base + ONENAND_REG_CTRL_STATUS)
1961 & ONENAND_CTRL_ONGO)
1964 /* Check lock status */
1965 status = this->read_word(this->base + ONENAND_REG_WP_STATUS);
1966 if (!(status & ONENAND_WP_US))
1967 printk(KERN_ERR "wp status = 0x%x\n", status);
1972 /* Block lock scheme */
1973 for (block = start; block < end; block++) {
1974 /* Set block address */
1975 value = onenand_block_address(this, block);
1976 this->write_word(value, this->base + ONENAND_REG_START_ADDRESS1);
1977 /* Select DataRAM for DDP */
1978 value = onenand_bufferram_address(this, block);
1979 this->write_word(value, this->base + ONENAND_REG_START_ADDRESS2);
1981 /* Set start block address */
1982 this->write_word(block,
1983 this->base + ONENAND_REG_START_BLOCK_ADDRESS);
1984 /* Write unlock command */
1985 this->command(mtd, ONENAND_CMD_UNLOCK, 0, 0);
1987 /* There's no return value */
1988 this->wait(mtd, FL_UNLOCKING);
1991 while (this->read_word(this->base + ONENAND_REG_CTRL_STATUS)
1992 & ONENAND_CTRL_ONGO)
1995 /* Check lock status */
1996 status = this->read_word(this->base + ONENAND_REG_WP_STATUS);
1997 if (!(status & ONENAND_WP_US))
1998 printk(KERN_ERR "block = %d, wp status = 0x%x\n",
2005 #ifdef ONENAND_LINUX
2007 * onenand_lock - [MTD Interface] Lock block(s)
2008 * @param mtd MTD device structure
2009 * @param ofs offset relative to mtd start
2010 * @param len number of bytes to unlock
2012 * Lock one or more blocks
2014 static int onenand_lock(struct mtd_info *mtd, loff_t ofs, size_t len)
2018 onenand_get_device(mtd, FL_LOCKING);
2019 ret = onenand_do_lock_cmd(mtd, ofs, len, ONENAND_CMD_LOCK);
2020 onenand_release_device(mtd);
2025 * onenand_unlock - [MTD Interface] Unlock block(s)
2026 * @param mtd MTD device structure
2027 * @param ofs offset relative to mtd start
2028 * @param len number of bytes to unlock
2030 * Unlock one or more blocks
2032 static int onenand_unlock(struct mtd_info *mtd, loff_t ofs, size_t len)
2036 onenand_get_device(mtd, FL_LOCKING);
2037 ret = onenand_do_lock_cmd(mtd, ofs, len, ONENAND_CMD_UNLOCK);
2038 onenand_release_device(mtd);
2044 * onenand_check_lock_status - [OneNAND Interface] Check lock status
2045 * @param this onenand chip data structure
2049 static int onenand_check_lock_status(struct onenand_chip *this)
2051 unsigned int value, block, status;
2054 end = this->chipsize >> this->erase_shift;
2055 for (block = 0; block < end; block++) {
2056 /* Set block address */
2057 value = onenand_block_address(this, block);
2058 this->write_word(value, this->base + ONENAND_REG_START_ADDRESS1);
2059 /* Select DataRAM for DDP */
2060 value = onenand_bufferram_address(this, block);
2061 this->write_word(value, this->base + ONENAND_REG_START_ADDRESS2);
2062 /* Set start block address */
2063 this->write_word(block, this->base + ONENAND_REG_START_BLOCK_ADDRESS);
2065 /* Check lock status */
2066 status = this->read_word(this->base + ONENAND_REG_WP_STATUS);
2067 if (!(status & ONENAND_WP_US)) {
2068 printk(KERN_ERR "block = %d, wp status = 0x%x\n", block, status);
2077 * onenand_unlock_all - [OneNAND Interface] unlock all blocks
2078 * @param mtd MTD device structure
2082 static void onenand_unlock_all(struct mtd_info *mtd)
2084 struct onenand_chip *this = mtd->priv;
2086 size_t len = mtd->size;
2088 if (this->options & ONENAND_HAS_UNLOCK_ALL) {
2089 /* Set start block address */
2090 this->write_word(0, this->base + ONENAND_REG_START_BLOCK_ADDRESS);
2091 /* Write unlock command */
2092 this->command(mtd, ONENAND_CMD_UNLOCK_ALL, 0, 0);
2094 /* There's no return value */
2095 this->wait(mtd, FL_LOCKING);
2098 while (this->read_word(this->base + ONENAND_REG_CTRL_STATUS)
2099 & ONENAND_CTRL_ONGO)
2102 /* Check lock status */
2103 if (onenand_check_lock_status(this))
2106 /* Workaround for all block unlock in DDP */
2107 if (ONENAND_IS_DDP(this) && !FLEXONENAND(this)) {
2108 /* All blocks on another chip */
2109 ofs = this->chipsize >> 1;
2110 len = this->chipsize >> 1;
2114 onenand_do_lock_cmd(mtd, ofs, len, ONENAND_CMD_UNLOCK);
2119 * onenand_check_features - Check and set OneNAND features
2120 * @param mtd MTD data structure
2122 * Check and set OneNAND features
2126 static void onenand_check_features(struct mtd_info *mtd)
2128 struct onenand_chip *this = mtd->priv;
2129 unsigned int density, process;
2131 /* Lock scheme depends on density and process */
2132 density = onenand_get_density(this->device_id);
2133 process = this->version_id >> ONENAND_VERSION_PROCESS_SHIFT;
2137 case ONENAND_DEVICE_DENSITY_4Gb:
2138 if (ONENAND_IS_DDP(this))
2139 this->options |= ONENAND_HAS_2PLANE;
2141 this->options |= ONENAND_HAS_4KB_PAGE;
2143 case ONENAND_DEVICE_DENSITY_2Gb:
2144 /* 2Gb DDP don't have 2 plane */
2145 if (!ONENAND_IS_DDP(this))
2146 this->options |= ONENAND_HAS_2PLANE;
2147 this->options |= ONENAND_HAS_UNLOCK_ALL;
2149 case ONENAND_DEVICE_DENSITY_1Gb:
2150 /* A-Die has all block unlock */
2152 this->options |= ONENAND_HAS_UNLOCK_ALL;
2156 /* Some OneNAND has continuous lock scheme */
2158 this->options |= ONENAND_HAS_CONT_LOCK;
2162 if (ONENAND_IS_MLC(this))
2163 this->options |= ONENAND_HAS_4KB_PAGE;
2165 if (ONENAND_IS_4KB_PAGE(this))
2166 this->options &= ~ONENAND_HAS_2PLANE;
2168 if (FLEXONENAND(this)) {
2169 this->options &= ~ONENAND_HAS_CONT_LOCK;
2170 this->options |= ONENAND_HAS_UNLOCK_ALL;
2173 if (this->options & ONENAND_HAS_CONT_LOCK)
2174 printk(KERN_DEBUG "Lock scheme is Continuous Lock\n");
2175 if (this->options & ONENAND_HAS_UNLOCK_ALL)
2176 printk(KERN_DEBUG "Chip support all block unlock\n");
2177 if (this->options & ONENAND_HAS_2PLANE)
2178 printk(KERN_DEBUG "Chip has 2 plane\n");
2179 if (this->options & ONENAND_HAS_4KB_PAGE)
2180 printk(KERN_DEBUG "Chip has 4KiB pagesize\n");
2185 * onenand_print_device_info - Print device ID
2186 * @param device device ID
2190 char *onenand_print_device_info(int device, int version)
2192 int vcc, demuxed, ddp, density, flexonenand;
2193 char *dev_info = malloc(80);
2196 vcc = device & ONENAND_DEVICE_VCC_MASK;
2197 demuxed = device & ONENAND_DEVICE_IS_DEMUX;
2198 ddp = device & ONENAND_DEVICE_IS_DDP;
2199 density = onenand_get_density(device);
2200 flexonenand = device & DEVICE_IS_FLEXONENAND;
2201 p += sprintf(dev_info, "%s%sOneNAND%s %dMB %sV 16-bit (0x%02x)",
2202 demuxed ? "" : "Muxed ",
2203 flexonenand ? "Flex-" : "",
2205 (16 << density), vcc ? "2.65/3.3" : "1.8", device);
2207 sprintf(p, "\nOneNAND version = 0x%04x", version);
2208 printk("%s\n", dev_info);
2213 static const struct onenand_manufacturers onenand_manuf_ids[] = {
2214 {ONENAND_MFR_NUMONYX, "Numonyx"},
2215 {ONENAND_MFR_SAMSUNG, "Samsung"},
2219 * onenand_check_maf - Check manufacturer ID
2220 * @param manuf manufacturer ID
2222 * Check manufacturer ID
2224 static int onenand_check_maf(int manuf)
2226 int size = ARRAY_SIZE(onenand_manuf_ids);
2228 #ifdef ONENAND_DEBUG
2232 for (i = 0; i < size; i++)
2233 if (manuf == onenand_manuf_ids[i].id)
2236 #ifdef ONENAND_DEBUG
2238 name = onenand_manuf_ids[i].name;
2242 printk(KERN_DEBUG "OneNAND Manufacturer: %s (0x%0x)\n", name, manuf);
2249 * flexonenand_get_boundary - Reads the SLC boundary
2250 * @param onenand_info - onenand info structure
2252 * Fill up boundary[] field in onenand_chip
2254 static int flexonenand_get_boundary(struct mtd_info *mtd)
2256 struct onenand_chip *this = mtd->priv;
2257 unsigned int die, bdry;
2261 syscfg = this->read_word(this->base + ONENAND_REG_SYS_CFG1);
2262 this->write_word((syscfg | 0x0100), this->base + ONENAND_REG_SYS_CFG1);
2264 for (die = 0; die < this->dies; die++) {
2265 this->command(mtd, FLEXONENAND_CMD_PI_ACCESS, die, 0);
2266 this->wait(mtd, FL_SYNCING);
2268 this->command(mtd, FLEXONENAND_CMD_READ_PI, die, 0);
2269 this->wait(mtd, FL_READING);
2271 bdry = this->read_word(this->base + ONENAND_DATARAM);
2272 if ((bdry >> FLEXONENAND_PI_UNLOCK_SHIFT) == 3)
2276 this->boundary[die] = bdry & FLEXONENAND_PI_MASK;
2278 this->command(mtd, ONENAND_CMD_RESET, 0, 0);
2279 this->wait(mtd, FL_RESETING);
2281 printk(KERN_INFO "Die %d boundary: %d%s\n", die,
2282 this->boundary[die], locked ? "(Locked)" : "(Unlocked)");
2286 this->write_word(syscfg, this->base + ONENAND_REG_SYS_CFG1);
2291 * flexonenand_get_size - Fill up fields in onenand_chip and mtd_info
2292 * boundary[], diesize[], mtd->size, mtd->erasesize,
2294 * @param mtd - MTD device structure
2296 static void flexonenand_get_size(struct mtd_info *mtd)
2298 struct onenand_chip *this = mtd->priv;
2299 int die, i, eraseshift, density;
2300 int blksperdie, maxbdry;
2303 density = onenand_get_density(this->device_id);
2304 blksperdie = ((loff_t)(16 << density) << 20) >> (this->erase_shift);
2305 blksperdie >>= ONENAND_IS_DDP(this) ? 1 : 0;
2306 maxbdry = blksperdie - 1;
2307 eraseshift = this->erase_shift - 1;
2309 mtd->numeraseregions = this->dies << 1;
2311 /* This fills up the device boundary */
2312 flexonenand_get_boundary(mtd);
2316 for (; die < this->dies; die++) {
2317 if (!die || this->boundary[die-1] != maxbdry) {
2319 mtd->eraseregions[i].offset = ofs;
2320 mtd->eraseregions[i].erasesize = 1 << eraseshift;
2321 mtd->eraseregions[i].numblocks =
2322 this->boundary[die] + 1;
2323 ofs += mtd->eraseregions[i].numblocks << eraseshift;
2326 mtd->numeraseregions -= 1;
2327 mtd->eraseregions[i].numblocks +=
2328 this->boundary[die] + 1;
2329 ofs += (this->boundary[die] + 1) << (eraseshift - 1);
2331 if (this->boundary[die] != maxbdry) {
2333 mtd->eraseregions[i].offset = ofs;
2334 mtd->eraseregions[i].erasesize = 1 << eraseshift;
2335 mtd->eraseregions[i].numblocks = maxbdry ^
2336 this->boundary[die];
2337 ofs += mtd->eraseregions[i].numblocks << eraseshift;
2340 mtd->numeraseregions -= 1;
2343 /* Expose MLC erase size except when all blocks are SLC */
2344 mtd->erasesize = 1 << this->erase_shift;
2345 if (mtd->numeraseregions == 1)
2346 mtd->erasesize >>= 1;
2348 printk(KERN_INFO "Device has %d eraseregions\n", mtd->numeraseregions);
2349 for (i = 0; i < mtd->numeraseregions; i++)
2350 printk(KERN_INFO "[offset: 0x%08llx, erasesize: 0x%05x,"
2351 " numblocks: %04u]\n", mtd->eraseregions[i].offset,
2352 mtd->eraseregions[i].erasesize,
2353 mtd->eraseregions[i].numblocks);
2355 for (die = 0, mtd->size = 0; die < this->dies; die++) {
2356 this->diesize[die] = (loff_t) (blksperdie << this->erase_shift);
2357 this->diesize[die] -= (loff_t) (this->boundary[die] + 1)
2358 << (this->erase_shift - 1);
2359 mtd->size += this->diesize[die];
2364 * flexonenand_check_blocks_erased - Check if blocks are erased
2365 * @param mtd_info - mtd info structure
2366 * @param start - first erase block to check
2367 * @param end - last erase block to check
2369 * Converting an unerased block from MLC to SLC
2370 * causes byte values to change. Since both data and its ECC
2371 * have changed, reads on the block give uncorrectable error.
2372 * This might lead to the block being detected as bad.
2374 * Avoid this by ensuring that the block to be converted is
2377 static int flexonenand_check_blocks_erased(struct mtd_info *mtd,
2380 struct onenand_chip *this = mtd->priv;
2383 struct mtd_oob_ops ops = {
2384 .mode = MTD_OPS_PLACE_OOB,
2386 .ooblen = mtd->oobsize,
2388 .oobbuf = this->oob_buf,
2392 printk(KERN_DEBUG "Check blocks from %d to %d\n", start, end);
2394 for (block = start; block <= end; block++) {
2395 addr = flexonenand_addr(this, block);
2396 if (onenand_block_isbad_nolock(mtd, addr, 0))
2400 * Since main area write results in ECC write to spare,
2401 * it is sufficient to check only ECC bytes for change.
2403 ret = onenand_read_oob_nolock(mtd, addr, &ops);
2407 for (i = 0; i < mtd->oobsize; i++)
2408 if (this->oob_buf[i] != 0xff)
2411 if (i != mtd->oobsize) {
2412 printk(KERN_WARNING "Block %d not erased.\n", block);
2421 * flexonenand_set_boundary - Writes the SLC boundary
2422 * @param mtd - mtd info structure
2424 int flexonenand_set_boundary(struct mtd_info *mtd, int die,
2425 int boundary, int lock)
2427 struct onenand_chip *this = mtd->priv;
2428 int ret, density, blksperdie, old, new, thisboundary;
2431 if (die >= this->dies)
2434 if (boundary == this->boundary[die])
2437 density = onenand_get_density(this->device_id);
2438 blksperdie = ((16 << density) << 20) >> this->erase_shift;
2439 blksperdie >>= ONENAND_IS_DDP(this) ? 1 : 0;
2441 if (boundary >= blksperdie) {
2442 printk("flexonenand_set_boundary:"
2443 "Invalid boundary value. "
2444 "Boundary not changed.\n");
2448 /* Check if converting blocks are erased */
2449 old = this->boundary[die] + (die * this->density_mask);
2450 new = boundary + (die * this->density_mask);
2451 ret = flexonenand_check_blocks_erased(mtd, min(old, new)
2452 + 1, max(old, new));
2454 printk(KERN_ERR "flexonenand_set_boundary: Please erase blocks before boundary change\n");
2458 this->command(mtd, FLEXONENAND_CMD_PI_ACCESS, die, 0);
2459 this->wait(mtd, FL_SYNCING);
2461 /* Check is boundary is locked */
2462 this->command(mtd, FLEXONENAND_CMD_READ_PI, die, 0);
2463 ret = this->wait(mtd, FL_READING);
2465 thisboundary = this->read_word(this->base + ONENAND_DATARAM);
2466 if ((thisboundary >> FLEXONENAND_PI_UNLOCK_SHIFT) != 3) {
2467 printk(KERN_ERR "flexonenand_set_boundary: boundary locked\n");
2471 printk(KERN_INFO "flexonenand_set_boundary: Changing die %d boundary: %d%s\n",
2472 die, boundary, lock ? "(Locked)" : "(Unlocked)");
2474 boundary &= FLEXONENAND_PI_MASK;
2475 boundary |= lock ? 0 : (3 << FLEXONENAND_PI_UNLOCK_SHIFT);
2477 addr = die ? this->diesize[0] : 0;
2478 this->command(mtd, ONENAND_CMD_ERASE, addr, 0);
2479 ret = this->wait(mtd, FL_ERASING);
2481 printk("flexonenand_set_boundary:"
2482 "Failed PI erase for Die %d\n", die);
2486 this->write_word(boundary, this->base + ONENAND_DATARAM);
2487 this->command(mtd, ONENAND_CMD_PROG, addr, 0);
2488 ret = this->wait(mtd, FL_WRITING);
2490 printk("flexonenand_set_boundary:"
2491 "Failed PI write for Die %d\n", die);
2495 this->command(mtd, FLEXONENAND_CMD_PI_UPDATE, die, 0);
2496 ret = this->wait(mtd, FL_WRITING);
2498 this->write_word(ONENAND_CMD_RESET, this->base + ONENAND_REG_COMMAND);
2499 this->wait(mtd, FL_RESETING);
2501 /* Recalculate device size on boundary change*/
2502 flexonenand_get_size(mtd);
2508 * onenand_chip_probe - [OneNAND Interface] Probe the OneNAND chip
2509 * @param mtd MTD device structure
2511 * OneNAND detection method:
2512 * Compare the the values from command with ones from register
2514 static int onenand_chip_probe(struct mtd_info *mtd)
2516 struct onenand_chip *this = mtd->priv;
2517 int bram_maf_id, bram_dev_id, maf_id, dev_id;
2520 /* Save system configuration 1 */
2521 syscfg = this->read_word(this->base + ONENAND_REG_SYS_CFG1);
2523 /* Clear Sync. Burst Read mode to read BootRAM */
2524 this->write_word((syscfg & ~ONENAND_SYS_CFG1_SYNC_READ),
2525 this->base + ONENAND_REG_SYS_CFG1);
2527 /* Send the command for reading device ID from BootRAM */
2528 this->write_word(ONENAND_CMD_READID, this->base + ONENAND_BOOTRAM);
2530 /* Read manufacturer and device IDs from BootRAM */
2531 bram_maf_id = this->read_word(this->base + ONENAND_BOOTRAM + 0x0);
2532 bram_dev_id = this->read_word(this->base + ONENAND_BOOTRAM + 0x2);
2534 /* Reset OneNAND to read default register values */
2535 this->write_word(ONENAND_CMD_RESET, this->base + ONENAND_BOOTRAM);
2538 this->wait(mtd, FL_RESETING);
2540 /* Restore system configuration 1 */
2541 this->write_word(syscfg, this->base + ONENAND_REG_SYS_CFG1);
2543 /* Check manufacturer ID */
2544 if (onenand_check_maf(bram_maf_id))
2547 /* Read manufacturer and device IDs from Register */
2548 maf_id = this->read_word(this->base + ONENAND_REG_MANUFACTURER_ID);
2549 dev_id = this->read_word(this->base + ONENAND_REG_DEVICE_ID);
2551 /* Check OneNAND device */
2552 if (maf_id != bram_maf_id || dev_id != bram_dev_id)
2559 * onenand_probe - [OneNAND Interface] Probe the OneNAND device
2560 * @param mtd MTD device structure
2562 * OneNAND detection method:
2563 * Compare the the values from command with ones from register
2565 int onenand_probe(struct mtd_info *mtd)
2567 struct onenand_chip *this = mtd->priv;
2572 ret = this->chip_probe(mtd);
2576 /* Read device IDs from Register */
2577 dev_id = this->read_word(this->base + ONENAND_REG_DEVICE_ID);
2578 ver_id = this->read_word(this->base + ONENAND_REG_VERSION_ID);
2579 this->technology = this->read_word(this->base + ONENAND_REG_TECHNOLOGY);
2581 /* Flash device information */
2582 mtd->name = onenand_print_device_info(dev_id, ver_id);
2583 this->device_id = dev_id;
2584 this->version_id = ver_id;
2586 /* Check OneNAND features */
2587 onenand_check_features(mtd);
2589 density = onenand_get_density(dev_id);
2590 if (FLEXONENAND(this)) {
2591 this->dies = ONENAND_IS_DDP(this) ? 2 : 1;
2592 /* Maximum possible erase regions */
2593 mtd->numeraseregions = this->dies << 1;
2594 mtd->eraseregions = malloc(sizeof(struct mtd_erase_region_info)
2595 * (this->dies << 1));
2596 if (!mtd->eraseregions)
2601 * For Flex-OneNAND, chipsize represents maximum possible device size.
2602 * mtd->size represents the actual device size.
2604 this->chipsize = (16 << density) << 20;
2606 /* OneNAND page size & block size */
2607 /* The data buffer size is equal to page size */
2609 this->read_word(this->base + ONENAND_REG_DATA_BUFFER_SIZE);
2610 /* We use the full BufferRAM */
2611 if (ONENAND_IS_4KB_PAGE(this))
2612 mtd->writesize <<= 1;
2614 mtd->oobsize = mtd->writesize >> 5;
2615 /* Pagers per block is always 64 in OneNAND */
2616 mtd->erasesize = mtd->writesize << 6;
2618 * Flex-OneNAND SLC area has 64 pages per block.
2619 * Flex-OneNAND MLC area has 128 pages per block.
2620 * Expose MLC erase size to find erase_shift and page_mask.
2622 if (FLEXONENAND(this))
2623 mtd->erasesize <<= 1;
2625 this->erase_shift = ffs(mtd->erasesize) - 1;
2626 this->page_shift = ffs(mtd->writesize) - 1;
2627 this->ppb_shift = (this->erase_shift - this->page_shift);
2628 this->page_mask = (mtd->erasesize / mtd->writesize) - 1;
2629 /* Set density mask. it is used for DDP */
2630 if (ONENAND_IS_DDP(this))
2631 this->density_mask = this->chipsize >> (this->erase_shift + 1);
2632 /* It's real page size */
2633 this->writesize = mtd->writesize;
2635 /* REVIST: Multichip handling */
2637 if (FLEXONENAND(this))
2638 flexonenand_get_size(mtd);
2640 mtd->size = this->chipsize;
2642 mtd->flags = MTD_CAP_NANDFLASH;
2643 mtd->_erase = onenand_erase;
2644 mtd->_read = onenand_read;
2645 mtd->_write = onenand_write;
2646 mtd->_read_oob = onenand_read_oob;
2647 mtd->_write_oob = onenand_write_oob;
2648 mtd->_sync = onenand_sync;
2649 mtd->_block_isbad = onenand_block_isbad;
2650 mtd->_block_markbad = onenand_block_markbad;
2656 * onenand_scan - [OneNAND Interface] Scan for the OneNAND device
2657 * @param mtd MTD device structure
2658 * @param maxchips Number of chips to scan for
2660 * This fills out all the not initialized function pointers
2661 * with the defaults.
2662 * The flash ID is read and the mtd/chip structures are
2663 * filled with the appropriate values.
2665 int onenand_scan(struct mtd_info *mtd, int maxchips)
2668 struct onenand_chip *this = mtd->priv;
2670 if (!this->read_word)
2671 this->read_word = onenand_readw;
2672 if (!this->write_word)
2673 this->write_word = onenand_writew;
2676 this->command = onenand_command;
2678 this->wait = onenand_wait;
2679 if (!this->bbt_wait)
2680 this->bbt_wait = onenand_bbt_wait;
2682 if (!this->read_bufferram)
2683 this->read_bufferram = onenand_read_bufferram;
2684 if (!this->write_bufferram)
2685 this->write_bufferram = onenand_write_bufferram;
2687 if (!this->chip_probe)
2688 this->chip_probe = onenand_chip_probe;
2690 if (!this->block_markbad)
2691 this->block_markbad = onenand_default_block_markbad;
2692 if (!this->scan_bbt)
2693 this->scan_bbt = onenand_default_bbt;
2695 if (onenand_probe(mtd))
2698 /* Set Sync. Burst Read after probing */
2699 if (this->mmcontrol) {
2700 printk(KERN_INFO "OneNAND Sync. Burst Read support\n");
2701 this->read_bufferram = onenand_sync_read_bufferram;
2704 /* Allocate buffers, if necessary */
2705 if (!this->page_buf) {
2706 this->page_buf = kzalloc(mtd->writesize, GFP_KERNEL);
2707 if (!this->page_buf) {
2708 printk(KERN_ERR "onenand_scan(): Can't allocate page_buf\n");
2711 this->options |= ONENAND_PAGEBUF_ALLOC;
2713 if (!this->oob_buf) {
2714 this->oob_buf = kzalloc(mtd->oobsize, GFP_KERNEL);
2715 if (!this->oob_buf) {
2716 printk(KERN_ERR "onenand_scan: Can't allocate oob_buf\n");
2717 if (this->options & ONENAND_PAGEBUF_ALLOC) {
2718 this->options &= ~ONENAND_PAGEBUF_ALLOC;
2719 kfree(this->page_buf);
2723 this->options |= ONENAND_OOBBUF_ALLOC;
2726 this->state = FL_READY;
2729 * Allow subpage writes up to oobsize.
2731 switch (mtd->oobsize) {
2733 this->ecclayout = &onenand_oob_128;
2734 mtd->subpage_sft = 0;
2738 this->ecclayout = &onenand_oob_64;
2739 mtd->subpage_sft = 2;
2743 this->ecclayout = &onenand_oob_32;
2744 mtd->subpage_sft = 1;
2748 printk(KERN_WARNING "No OOB scheme defined for oobsize %d\n",
2750 mtd->subpage_sft = 0;
2751 /* To prevent kernel oops */
2752 this->ecclayout = &onenand_oob_32;
2756 this->subpagesize = mtd->writesize >> mtd->subpage_sft;
2759 * The number of bytes available for a client to place data into
2760 * the out of band area
2762 this->ecclayout->oobavail = 0;
2764 for (i = 0; i < MTD_MAX_OOBFREE_ENTRIES_LARGE &&
2765 this->ecclayout->oobfree[i].length; i++)
2766 this->ecclayout->oobavail +=
2767 this->ecclayout->oobfree[i].length;
2768 mtd->oobavail = this->ecclayout->oobavail;
2770 mtd->ecclayout = this->ecclayout;
2772 /* Unlock whole block */
2773 onenand_unlock_all(mtd);
2775 return this->scan_bbt(mtd);
2779 * onenand_release - [OneNAND Interface] Free resources held by the OneNAND device
2780 * @param mtd MTD device structure
2782 void onenand_release(struct mtd_info *mtd)