2 * linux/drivers/mtd/onenand/onenand_base.c
4 * Copyright (C) 2005-2007 Samsung Electronics
5 * Kyungmin Park <kyungmin.park@samsung.com>
9 #include <linux/mtd/compat.h>
10 #include <linux/mtd/mtd.h>
11 #include <linux/mtd/onenand.h>
14 #include <asm/errno.h>
20 #define printk(...) do {} while (0)
21 #define puts(...) do {} while (0)
23 extern void *memcpy32(void *dst, const void *src, int len);
25 /* It should access 16-bit instead of 8-bit */
26 static void *memcpy_16(void *dst, const void *src, unsigned int len)
32 if (len >= 32 && (len & (32 - 1)) == 0)
33 return memcpy32(dst, src, len);
42 * onenand_oob_128 - oob info for Flex-Onenand with 4KB page
43 * For now, we expose only 64 out of 80 ecc bytes
45 static struct nand_ecclayout onenand_oob_128 = {
48 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
49 22, 23, 24, 25, 26, 27, 28, 29, 30, 31,
50 38, 39, 40, 41, 42, 43, 44, 45, 46, 47,
51 54, 55, 56, 57, 58, 59, 60, 61, 62, 63,
52 70, 71, 72, 73, 74, 75, 76, 77, 78, 79,
53 86, 87, 88, 89, 90, 91, 92, 93, 94, 95,
57 {2, 4}, {18, 4}, {34, 4}, {50, 4},
58 {66, 4}, {82, 4}, {98, 4}, {114, 4}
63 * onenand_oob_64 - oob info for large (2KB) page
65 static struct nand_ecclayout onenand_oob_64 = {
74 {2, 3}, {14, 2}, {18, 3}, {30, 2},
75 {34, 3}, {46, 2}, {50, 3}, {62, 2}
80 * onenand_oob_32 - oob info for middle (1KB) page
82 static struct nand_ecclayout onenand_oob_32 = {
88 .oobfree = { {2, 3}, {14, 2}, {18, 3}, {30, 2} }
91 static const unsigned char ffchars[] __attribute__((aligned(4))) = {
92 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
93 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 16 */
94 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
95 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 32 */
96 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
97 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 48 */
98 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
99 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 64 */
100 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
101 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 80 */
102 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
103 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 96 */
104 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
105 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 112 */
106 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
107 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 128 */
111 * from ../mtdpart.c to support callback
113 void mtd_erase_callback(struct erase_info *instr)
119 * onenand_readw - [OneNAND Interface] Read OneNAND register
120 * @param addr address to read
122 * Read OneNAND register
124 static unsigned short onenand_readw(void __iomem * addr)
130 * onenand_writew - [OneNAND Interface] Write OneNAND register with value
131 * @param value value to write
132 * @param addr address to write
134 * Write OneNAND register with value
136 static void onenand_writew(unsigned short value, void __iomem * addr)
142 * onenand_block_address - [DEFAULT] Get block address
143 * @param device the device id
144 * @param block the block
145 * @return translated block address if DDP, otherwise same
147 * Setup Start Address 1 Register (F100h)
149 static int onenand_block_address(struct onenand_chip *this, int block)
151 /* Device Flash Core select, NAND Flash Block Address */
152 if (block & this->density_mask)
153 return ONENAND_DDP_CHIP1 | (block ^ this->density_mask);
159 * onenand_bufferram_address - [DEFAULT] Get bufferram address
160 * @param device the device id
161 * @param block the block
162 * @return set DBS value if DDP, otherwise 0
164 * Setup Start Address 2 Register (F101h) for DDP
166 static int onenand_bufferram_address(struct onenand_chip *this, int block)
168 /* Device BufferRAM Select */
169 if (block & this->density_mask)
170 return ONENAND_DDP_CHIP1;
172 return ONENAND_DDP_CHIP0;
176 * onenand_page_address - [DEFAULT] Get page address
177 * @param page the page address
178 * @param sector the sector address
179 * @return combined page and sector address
181 * Setup Start Address 8 Register (F107h)
183 static int onenand_page_address(int page, int sector)
185 /* Flash Page Address, Flash Sector Address */
188 fpa = page & ONENAND_FPA_MASK;
189 fsa = sector & ONENAND_FSA_MASK;
191 return ((fpa << ONENAND_FPA_SHIFT) | fsa);
195 * onenand_buffer_address - [DEFAULT] Get buffer address
196 * @param dataram1 DataRAM index
197 * @param sectors the sector address
198 * @param count the number of sectors
199 * @return the start buffer value
201 * Setup Start Buffer Register (F200h)
203 static int onenand_buffer_address(int dataram1, int sectors, int count)
207 /* BufferRAM Sector Address */
208 bsa = sectors & ONENAND_BSA_MASK;
211 bsa |= ONENAND_BSA_DATARAM1; /* DataRAM1 */
213 bsa |= ONENAND_BSA_DATARAM0; /* DataRAM0 */
215 /* BufferRAM Sector Count */
216 bsc = count & ONENAND_BSC_MASK;
218 return ((bsa << ONENAND_BSA_SHIFT) | bsc);
222 * flexonenand_block - Return block number for flash address
223 * @param this - OneNAND device structure
224 * @param addr - Address for which block number is needed
226 static unsigned int flexonenand_block(struct onenand_chip *this, loff_t addr)
228 unsigned int boundary, blk, die = 0;
230 if (ONENAND_IS_DDP(this) && addr >= this->diesize[0]) {
232 addr -= this->diesize[0];
235 boundary = this->boundary[die];
237 blk = addr >> (this->erase_shift - 1);
239 blk = (blk + boundary + 1) >> 1;
241 blk += die ? this->density_mask : 0;
245 unsigned int onenand_block(struct onenand_chip *this, loff_t addr)
247 if (!FLEXONENAND(this))
248 return addr >> this->erase_shift;
249 return flexonenand_block(this, addr);
253 * flexonenand_addr - Return address of the block
254 * @this: OneNAND device structure
255 * @block: Block number on Flex-OneNAND
257 * Return address of the block
259 static loff_t flexonenand_addr(struct onenand_chip *this, int block)
262 int die = 0, boundary;
264 if (ONENAND_IS_DDP(this) && block >= this->density_mask) {
265 block -= this->density_mask;
267 ofs = this->diesize[0];
270 boundary = this->boundary[die];
271 ofs += (loff_t) block << (this->erase_shift - 1);
272 if (block > (boundary + 1))
273 ofs += (loff_t) (block - boundary - 1)
274 << (this->erase_shift - 1);
278 loff_t onenand_addr(struct onenand_chip *this, int block)
280 if (!FLEXONENAND(this))
281 return (loff_t) block << this->erase_shift;
282 return flexonenand_addr(this, block);
286 * flexonenand_region - [Flex-OneNAND] Return erase region of addr
287 * @param mtd MTD device structure
288 * @param addr address whose erase region needs to be identified
290 int flexonenand_region(struct mtd_info *mtd, loff_t addr)
294 for (i = 0; i < mtd->numeraseregions; i++)
295 if (addr < mtd->eraseregions[i].offset)
301 * onenand_get_density - [DEFAULT] Get OneNAND density
302 * @param dev_id OneNAND device ID
304 * Get OneNAND density from device ID
306 static inline int onenand_get_density(int dev_id)
308 int density = dev_id >> ONENAND_DEVICE_DENSITY_SHIFT;
309 return (density & ONENAND_DEVICE_DENSITY_MASK);
313 * onenand_command - [DEFAULT] Send command to OneNAND device
314 * @param mtd MTD device structure
315 * @param cmd the command to be sent
316 * @param addr offset to read from or write to
317 * @param len number of bytes to read or write
319 * Send command to OneNAND device. This function is used for middle/large page
320 * devices (1KB/2KB Bytes per page)
322 static int onenand_command(struct mtd_info *mtd, int cmd, loff_t addr,
325 struct onenand_chip *this = mtd->priv;
326 int value, block, page;
328 /* Address translation */
330 case ONENAND_CMD_UNLOCK:
331 case ONENAND_CMD_LOCK:
332 case ONENAND_CMD_LOCK_TIGHT:
333 case ONENAND_CMD_UNLOCK_ALL:
338 case FLEXONENAND_CMD_PI_ACCESS:
339 /* addr contains die index */
340 block = addr * this->density_mask;
344 case ONENAND_CMD_ERASE:
345 case ONENAND_CMD_BUFFERRAM:
346 block = onenand_block(this, addr);
350 case FLEXONENAND_CMD_READ_PI:
351 cmd = ONENAND_CMD_READ;
352 block = addr * this->density_mask;
357 block = onenand_block(this, addr);
359 - onenand_addr(this, block)) >> this->page_shift;
360 page &= this->page_mask;
364 /* NOTE: The setting order of the registers is very important! */
365 if (cmd == ONENAND_CMD_BUFFERRAM) {
366 /* Select DataRAM for DDP */
367 value = onenand_bufferram_address(this, block);
368 this->write_word(value,
369 this->base + ONENAND_REG_START_ADDRESS2);
371 if (ONENAND_IS_4KB_PAGE(this))
372 /* It is always BufferRAM0 */
373 ONENAND_SET_BUFFERRAM0(this);
375 /* Switch to the next data buffer */
376 ONENAND_SET_NEXT_BUFFERRAM(this);
382 /* Write 'DFS, FBA' of Flash */
383 value = onenand_block_address(this, block);
384 this->write_word(value,
385 this->base + ONENAND_REG_START_ADDRESS1);
387 /* Select DataRAM for DDP */
388 value = onenand_bufferram_address(this, block);
389 this->write_word(value,
390 this->base + ONENAND_REG_START_ADDRESS2);
394 /* Now we use page size operation */
395 int sectors = 0, count = 0;
399 case FLEXONENAND_CMD_RECOVER_LSB:
400 case ONENAND_CMD_READ:
401 case ONENAND_CMD_READOOB:
402 case ONENAND_CMD_SUPERLOAD:
403 if (ONENAND_IS_4KB_PAGE(this))
404 /* It is always BufferRAM0 */
405 dataram = ONENAND_SET_BUFFERRAM0(this);
407 dataram = ONENAND_SET_NEXT_BUFFERRAM(this);
411 dataram = ONENAND_CURRENT_BUFFERRAM(this);
415 /* Write 'FPA, FSA' of Flash */
416 value = onenand_page_address(page, sectors);
417 this->write_word(value,
418 this->base + ONENAND_REG_START_ADDRESS8);
420 /* Write 'BSA, BSC' of DataRAM */
421 value = onenand_buffer_address(dataram, sectors, count);
422 this->write_word(value, this->base + ONENAND_REG_START_BUFFER);
425 /* Interrupt clear */
426 this->write_word(ONENAND_INT_CLEAR, this->base + ONENAND_REG_INTERRUPT);
428 this->write_word(cmd, this->base + ONENAND_REG_COMMAND);
434 * onenand_read_ecc - return ecc status
435 * @param this onenand chip structure
437 static int onenand_read_ecc(struct onenand_chip *this)
441 if (!ONENAND_IS_4KB_PAGE(this))
442 return this->read_word(this->base + ONENAND_REG_ECC_STATUS);
444 for (i = 0; i < 4; i++) {
445 ecc = this->read_word(this->base
446 + ((ONENAND_REG_ECC_STATUS + i) << 1));
449 if (ecc & FLEXONENAND_UNCORRECTABLE_ERROR)
450 return ONENAND_ECC_2BIT_ALL;
457 * onenand_wait - [DEFAULT] wait until the command is done
458 * @param mtd MTD device structure
459 * @param state state to select the max. timeout value
461 * Wait for command done. This applies to all OneNAND command
462 * Read can take up to 30us, erase up to 2ms and program up to 350us
463 * according to general OneNAND specs
465 static int onenand_wait(struct mtd_info *mtd, int state)
467 struct onenand_chip *this = mtd->priv;
468 unsigned int flags = ONENAND_INT_MASTER;
469 unsigned int interrupt = 0;
473 interrupt = this->read_word(this->base + ONENAND_REG_INTERRUPT);
474 if (interrupt & flags)
478 ctrl = this->read_word(this->base + ONENAND_REG_CTRL_STATUS);
480 if (interrupt & ONENAND_INT_READ) {
481 int ecc = onenand_read_ecc(this);
482 if (ecc & ONENAND_ECC_2BIT_ALL) {
483 printk("onenand_wait: ECC error = 0x%04x\n", ecc);
488 if (ctrl & ONENAND_CTRL_ERROR) {
489 printk("onenand_wait: controller error = 0x%04x\n", ctrl);
490 if (ctrl & ONENAND_CTRL_LOCK)
491 printk("onenand_wait: it's locked error = 0x%04x\n",
501 * onenand_bufferram_offset - [DEFAULT] BufferRAM offset
502 * @param mtd MTD data structure
503 * @param area BufferRAM area
504 * @return offset given area
506 * Return BufferRAM offset given area
508 static inline int onenand_bufferram_offset(struct mtd_info *mtd, int area)
510 struct onenand_chip *this = mtd->priv;
512 if (ONENAND_CURRENT_BUFFERRAM(this)) {
513 if (area == ONENAND_DATARAM)
514 return mtd->writesize;
515 if (area == ONENAND_SPARERAM)
523 * onenand_read_bufferram - [OneNAND Interface] Read the bufferram area
524 * @param mtd MTD data structure
525 * @param area BufferRAM area
526 * @param buffer the databuffer to put/get data
527 * @param offset offset to read from or write to
528 * @param count number of bytes to read/write
530 * Read the BufferRAM area
532 static int onenand_read_bufferram(struct mtd_info *mtd, loff_t addr, int area,
533 unsigned char *buffer, int offset,
536 struct onenand_chip *this = mtd->priv;
537 void __iomem *bufferram;
539 bufferram = this->base + area;
540 bufferram += onenand_bufferram_offset(mtd, area);
542 memcpy_16(buffer, bufferram + offset, count);
548 * onenand_write_bufferram - [OneNAND Interface] Write the bufferram area
549 * @param mtd MTD data structure
550 * @param area BufferRAM area
551 * @param buffer the databuffer to put/get data
552 * @param offset offset to read from or write to
553 * @param count number of bytes to read/write
555 * Write the BufferRAM area
557 static int onenand_write_bufferram(struct mtd_info *mtd, loff_t addr, int area,
558 const unsigned char *buffer, int offset,
561 struct onenand_chip *this = mtd->priv;
562 void __iomem *bufferram;
564 bufferram = this->base + area;
565 bufferram += onenand_bufferram_offset(mtd, area);
567 memcpy_16(bufferram + offset, buffer, count);
573 * onenand_get_2x_blockpage - [GENERIC] Get blockpage at 2x program mode
574 * @param mtd MTD data structure
575 * @param addr address to check
576 * @return blockpage address
578 * Get blockpage address at 2x program mode
580 static int onenand_get_2x_blockpage(struct mtd_info *mtd, loff_t addr)
582 struct onenand_chip *this = mtd->priv;
583 int blockpage, block, page;
585 /* Calculate the even block number */
586 block = (int) (addr >> this->erase_shift) & ~1;
587 /* Is it the odd plane? */
588 if (addr & this->writesize)
590 page = (int) (addr >> (this->page_shift + 1)) & this->page_mask;
591 blockpage = (block << 7) | page;
597 * onenand_check_bufferram - [GENERIC] Check BufferRAM information
598 * @param mtd MTD data structure
599 * @param addr address to check
600 * @return 1 if there are valid data, otherwise 0
602 * Check bufferram if there is data we required
604 static int onenand_check_bufferram(struct mtd_info *mtd, loff_t addr)
606 struct onenand_chip *this = mtd->priv;
607 int blockpage, found = 0;
610 if (ONENAND_IS_2PLANE(this))
611 blockpage = onenand_get_2x_blockpage(mtd, addr);
613 blockpage = (int) (addr >> this->page_shift);
615 /* Is there valid data? */
616 i = ONENAND_CURRENT_BUFFERRAM(this);
617 if (this->bufferram[i].blockpage == blockpage)
620 /* Check another BufferRAM */
621 i = ONENAND_NEXT_BUFFERRAM(this);
622 if (this->bufferram[i].blockpage == blockpage) {
623 ONENAND_SET_NEXT_BUFFERRAM(this);
628 if (found && ONENAND_IS_DDP(this)) {
629 /* Select DataRAM for DDP */
630 int block = onenand_block(this, addr);
631 int value = onenand_bufferram_address(this, block);
632 this->write_word(value, this->base + ONENAND_REG_START_ADDRESS2);
639 * onenand_update_bufferram - [GENERIC] Update BufferRAM information
640 * @param mtd MTD data structure
641 * @param addr address to update
642 * @param valid valid flag
644 * Update BufferRAM information
646 static int onenand_update_bufferram(struct mtd_info *mtd, loff_t addr,
649 struct onenand_chip *this = mtd->priv;
653 if (ONENAND_IS_2PLANE(this))
654 blockpage = onenand_get_2x_blockpage(mtd, addr);
656 blockpage = (int)(addr >> this->page_shift);
658 /* Invalidate another BufferRAM */
659 i = ONENAND_NEXT_BUFFERRAM(this);
660 if (this->bufferram[i].blockpage == blockpage)
661 this->bufferram[i].blockpage = -1;
663 /* Update BufferRAM */
664 i = ONENAND_CURRENT_BUFFERRAM(this);
666 this->bufferram[i].blockpage = blockpage;
668 this->bufferram[i].blockpage = -1;
674 * onenand_invalidate_bufferram - [GENERIC] Invalidate BufferRAM information
675 * @param mtd MTD data structure
676 * @param addr start address to invalidate
677 * @param len length to invalidate
679 * Invalidate BufferRAM information
681 static void onenand_invalidate_bufferram(struct mtd_info *mtd, loff_t addr,
684 struct onenand_chip *this = mtd->priv;
686 loff_t end_addr = addr + len;
688 /* Invalidate BufferRAM */
689 for (i = 0; i < MAX_BUFFERRAM; i++) {
690 loff_t buf_addr = this->bufferram[i].blockpage << this->page_shift;
692 if (buf_addr >= addr && buf_addr < end_addr)
693 this->bufferram[i].blockpage = -1;
698 * onenand_get_device - [GENERIC] Get chip for selected access
699 * @param mtd MTD device structure
700 * @param new_state the state which is requested
702 * Get the device and lock it for exclusive access
704 static void onenand_get_device(struct mtd_info *mtd, int new_state)
710 * onenand_release_device - [GENERIC] release chip
711 * @param mtd MTD device structure
713 * Deselect, release chip lock and wake up anyone waiting on the device
715 static void onenand_release_device(struct mtd_info *mtd)
721 * onenand_transfer_auto_oob - [Internal] oob auto-placement transfer
722 * @param mtd MTD device structure
723 * @param buf destination address
724 * @param column oob offset to read from
725 * @param thislen oob length to read
727 static int onenand_transfer_auto_oob(struct mtd_info *mtd, uint8_t *buf,
728 int column, int thislen)
730 struct onenand_chip *this = mtd->priv;
731 struct nand_oobfree *free;
732 int readcol = column;
733 int readend = column + thislen;
736 uint8_t *oob_buf = this->oob_buf;
738 free = this->ecclayout->oobfree;
739 for (i = 0; i < MTD_MAX_OOBFREE_ENTRIES && free->length; i++, free++) {
740 if (readcol >= lastgap)
741 readcol += free->offset - lastgap;
742 if (readend >= lastgap)
743 readend += free->offset - lastgap;
744 lastgap = free->offset + free->length;
746 this->read_bufferram(mtd, 0, ONENAND_SPARERAM, oob_buf, 0, mtd->oobsize);
747 free = this->ecclayout->oobfree;
748 for (i = 0; i < MTD_MAX_OOBFREE_ENTRIES && free->length; i++, free++) {
749 int free_end = free->offset + free->length;
750 if (free->offset < readend && free_end > readcol) {
751 int st = max_t(int,free->offset,readcol);
752 int ed = min_t(int,free_end,readend);
754 memcpy(buf, oob_buf + st, n);
756 } else if (column == 0)
763 * onenand_recover_lsb - [Flex-OneNAND] Recover LSB page data
764 * @param mtd MTD device structure
765 * @param addr address to recover
766 * @param status return value from onenand_wait
768 * MLC NAND Flash cell has paired pages - LSB page and MSB page. LSB page has
769 * lower page address and MSB page has higher page address in paired pages.
770 * If power off occurs during MSB page program, the paired LSB page data can
771 * become corrupt. LSB page recovery read is a way to read LSB page though page
772 * data are corrupted. When uncorrectable error occurs as a result of LSB page
773 * read after power up, issue LSB page recovery read.
775 static int onenand_recover_lsb(struct mtd_info *mtd, loff_t addr, int status)
777 struct onenand_chip *this = mtd->priv;
780 /* Recovery is only for Flex-OneNAND */
781 if (!FLEXONENAND(this))
784 /* check if we failed due to uncorrectable error */
785 if (status != -EBADMSG && status != ONENAND_BBT_READ_ECC_ERROR)
788 /* check if address lies in MLC region */
789 i = flexonenand_region(mtd, addr);
790 if (mtd->eraseregions[i].erasesize < (1 << this->erase_shift))
793 printk("onenand_recover_lsb:"
794 "Attempting to recover from uncorrectable read\n");
796 /* Issue the LSB page recovery command */
797 this->command(mtd, FLEXONENAND_CMD_RECOVER_LSB, addr, this->writesize);
798 return this->wait(mtd, FL_READING);
802 * onenand_read_ops_nolock - [OneNAND Interface] OneNAND read main and/or out-of-band
803 * @param mtd MTD device structure
804 * @param from offset to read from
805 * @param ops oob operation description structure
807 * OneNAND read main and/or out-of-band data
809 static int onenand_normal_read_ops_nolock(struct mtd_info *mtd, loff_t from,
810 struct mtd_oob_ops *ops)
812 struct onenand_chip *this = mtd->priv;
813 struct mtd_ecc_stats stats;
814 size_t len = ops->len;
815 size_t ooblen = ops->ooblen;
816 u_char *buf = ops->datbuf;
817 u_char *oobbuf = ops->oobbuf;
818 int read = 0, column, thislen;
819 int oobread = 0, oobcolumn, thisooblen, oobsize;
820 int ret = 0, boundary = 0;
821 int writesize = this->writesize;
823 MTDDEBUG(MTD_DEBUG_LEVEL3, "onenand_read_ops_nolock: from = 0x%08x, len = %i\n", (unsigned int) from, (int) len);
825 if (ops->mode == MTD_OOB_AUTO)
826 oobsize = this->ecclayout->oobavail;
828 oobsize = mtd->oobsize;
830 oobcolumn = from & (mtd->oobsize - 1);
832 /* Do not allow reads past end of device */
833 if ((from + len) > mtd->size) {
834 printk(KERN_ERR "onenand_read_ops_nolock: Attempt read beyond end of device\n");
840 stats = mtd->ecc_stats;
842 /* Read-while-load method */
843 /* Note: We can't use this feature in MLC */
845 /* Do first load to bufferRAM */
847 if (!onenand_check_bufferram(mtd, from)) {
848 this->command(mtd, ONENAND_CMD_READ, from, writesize);
849 ret = this->wait(mtd, FL_READING);
851 ret = onenand_recover_lsb(mtd, from, ret);
852 onenand_update_bufferram(mtd, from, !ret);
858 thislen = min_t(int, writesize, len - read);
859 column = from & (writesize - 1);
860 if (column + thislen > writesize)
861 thislen = writesize - column;
864 /* If there is more to load then start next load */
867 if (ONENAND_IS_4KB_PAGE(this))
868 goto skip_read_while_load;
870 if (read + thislen < len) {
871 this->command(mtd, ONENAND_CMD_READ, from, writesize);
873 * Chip boundary handling in DDP
874 * Now we issued chip 1 read and pointed chip 1
875 * bufferam so we have to point chip 0 bufferam.
877 if (ONENAND_IS_DDP(this) &&
878 unlikely(from == (this->chipsize >> 1))) {
879 this->write_word(ONENAND_DDP_CHIP0, this->base + ONENAND_REG_START_ADDRESS2);
883 ONENAND_SET_PREV_BUFFERRAM(this);
885 skip_read_while_load:
886 /* While load is going, read from last bufferRAM */
887 this->read_bufferram(mtd, from - thislen, ONENAND_DATARAM, buf, column, thislen);
889 /* Read oob area if needed */
891 thisooblen = oobsize - oobcolumn;
892 thisooblen = min_t(int, thisooblen, ooblen - oobread);
894 if (ops->mode == MTD_OOB_AUTO)
895 onenand_transfer_auto_oob(mtd, oobbuf, oobcolumn, thisooblen);
897 this->read_bufferram(mtd, 0, ONENAND_SPARERAM, oobbuf, oobcolumn, thisooblen);
898 oobread += thisooblen;
899 oobbuf += thisooblen;
903 /* See if we are done */
907 /* Set up for next read from bufferRAM */
908 if (unlikely(boundary))
909 this->write_word(ONENAND_DDP_CHIP1, this->base + ONENAND_REG_START_ADDRESS2);
910 if (ONENAND_IS_4KB_PAGE(this))
911 this->command(mtd, ONENAND_CMD_READ, from, writesize);
913 ONENAND_SET_NEXT_BUFFERRAM(this);
915 thislen = min_t(int, writesize, len - read);
918 if (ONENAND_IS_4KB_PAGE(this)) {
919 /* Now wait for load */
920 ret = this->wait(mtd, FL_READING);
921 onenand_update_bufferram(mtd, from, !ret);
928 * Return success, if no ECC failures, else -EBADMSG
929 * fs driver will take care of that, because
930 * retlen == desired len and result == -EBADMSG
933 ops->oobretlen = oobread;
938 if (mtd->ecc_stats.failed - stats.failed)
941 return mtd->ecc_stats.corrected - stats.corrected ? -EUCLEAN : 0;
944 static int onenand_superload_read_ops_nolock(struct mtd_info *mtd, loff_t from,
945 struct mtd_oob_ops *ops)
947 struct onenand_chip *this = mtd->priv;
948 struct mtd_ecc_stats stats;
949 size_t len = ops->len;
950 size_t ooblen = ops->ooblen;
951 u_char *buf = ops->datbuf;
952 u_char *oobbuf = ops->oobbuf;
953 int read = 0, column, thislen;
954 int oobread = 0, oobcolumn, thisooblen, oobsize;
955 int ret = 0, boundary = 0;
956 int writesize = this->writesize;
958 MTDDEBUG(MTD_DEBUG_LEVEL3, "onenand_read_ops_nolock: from = 0x%08x, len = %i\n", (unsigned int) from, (int) len);
960 if (ops->mode == MTD_OOB_AUTO)
961 oobsize = this->ecclayout->oobavail;
963 oobsize = mtd->oobsize;
965 oobcolumn = from & (mtd->oobsize - 1);
967 /* Do not allow reads past end of device */
968 if ((from + len) > mtd->size) {
969 printk(KERN_ERR "onenand_read_ops_nolock: Attempt read beyond end of device\n");
975 stats = mtd->ecc_stats;
977 this->command(mtd, ONENAND_CMD_READ, from, writesize);
978 ret = this->wait(mtd, FL_READING);
980 ret = onenand_recover_lsb(mtd, from, ret);
981 onenand_update_bufferram(mtd, from, !ret);
985 thislen = min_t(int, writesize, len - read);
986 column = from & (writesize - 1);
987 if (column + thislen > writesize)
988 thislen = writesize - column;
991 /* If there is more to load then start next load */
994 if (read + thislen < len) {
995 this->command(mtd, ONENAND_CMD_SUPERLOAD, from, writesize);
997 * Chip boundary handling in DDP
998 * Now we issued chip 1 read and pointed chip 1
999 * bufferam so we have to point chip 0 bufferam.
1001 if (ONENAND_IS_DDP(this) &&
1002 unlikely(from == (this->chipsize >> 1))) {
1003 this->write_word(ONENAND_DDP_CHIP0, this->base + ONENAND_REG_START_ADDRESS2);
1008 /* While load is going, read from last bufferRAM */
1009 this->read_bufferram(mtd, from - thislen, ONENAND_DATARAM, buf, column, thislen);
1011 /* Read oob area if needed */
1013 thisooblen = oobsize - oobcolumn;
1014 thisooblen = min_t(int, thisooblen, ooblen - oobread);
1016 if (ops->mode == MTD_OOB_AUTO)
1017 onenand_transfer_auto_oob(mtd, oobbuf, oobcolumn, thisooblen);
1019 this->read_bufferram(mtd, 0, ONENAND_SPARERAM, oobbuf, oobcolumn, thisooblen);
1020 oobread += thisooblen;
1021 oobbuf += thisooblen;
1024 /* It should be read at least 4 bytes
1025 * to the end of the spare area
1027 this->read_bufferram(mtd, 0, ONENAND_SPARERAM, this->oob_buf, 0, mtd->oobsize);
1030 /* See if we are done */
1034 /* Set up for next read from bufferRAM */
1035 if (unlikely(boundary))
1036 this->write_word(ONENAND_DDP_CHIP1, this->base + ONENAND_REG_START_ADDRESS2);
1039 thislen = min_t(int, writesize, len - read);
1042 /* Now wait for load */
1043 ret = this->wait(mtd, FL_READING);
1044 onenand_update_bufferram(mtd, from, !ret);
1045 if (ret == -EBADMSG)
1050 * Return success, if no ECC failures, else -EBADMSG
1051 * fs driver will take care of that, because
1052 * retlen == desired len and result == -EBADMSG
1055 ops->oobretlen = oobread;
1060 if (mtd->ecc_stats.failed - stats.failed)
1063 return mtd->ecc_stats.corrected - stats.corrected ? -EUCLEAN : 0;
1067 * onenand_read_ops_nolock - [OneNAND Interface] OneNAND read main and/or out-of-band
1068 * @param mtd MTD device structure
1069 * @param from offset to read from
1070 * @param ops oob operation description structure
1072 * OneNAND read main and/or out-of-band data
1074 static int onenand_read_ops_nolock(struct mtd_info *mtd, loff_t from,
1075 struct mtd_oob_ops *ops)
1077 struct onenand_chip *this = mtd->priv;
1079 if (ONENAND_IS_4KB_PAGE(this) && ONENAND_IS_SYNC_MODE(this))
1080 return onenand_superload_read_ops_nolock(mtd, from, ops);
1082 return onenand_normal_read_ops_nolock(mtd, from, ops);
1087 * onenand_read_oob_nolock - [MTD Interface] OneNAND read out-of-band
1088 * @param mtd MTD device structure
1089 * @param from offset to read from
1090 * @param ops oob operation description structure
1092 * OneNAND read out-of-band data from the spare area
1094 static int onenand_read_oob_nolock(struct mtd_info *mtd, loff_t from,
1095 struct mtd_oob_ops *ops)
1097 struct onenand_chip *this = mtd->priv;
1098 struct mtd_ecc_stats stats;
1099 int read = 0, thislen, column, oobsize;
1100 size_t len = ops->ooblen;
1101 mtd_oob_mode_t mode = ops->mode;
1102 u_char *buf = ops->oobbuf;
1103 int ret = 0, readcmd;
1105 from += ops->ooboffs;
1107 MTDDEBUG(MTD_DEBUG_LEVEL3, "onenand_read_oob_nolock: from = 0x%08x, len = %i\n", (unsigned int) from, (int) len);
1109 /* Initialize return length value */
1112 if (mode == MTD_OOB_AUTO)
1113 oobsize = this->ecclayout->oobavail;
1115 oobsize = mtd->oobsize;
1117 column = from & (mtd->oobsize - 1);
1119 if (unlikely(column >= oobsize)) {
1120 printk(KERN_ERR "onenand_read_oob_nolock: Attempted to start read outside oob\n");
1124 /* Do not allow reads past end of device */
1125 if (unlikely(from >= mtd->size ||
1126 column + len > ((mtd->size >> this->page_shift) -
1127 (from >> this->page_shift)) * oobsize)) {
1128 printk(KERN_ERR "onenand_read_oob_nolock: Attempted to read beyond end of device\n");
1132 stats = mtd->ecc_stats;
1134 readcmd = ONENAND_IS_MLC(this) ? ONENAND_CMD_READ : ONENAND_CMD_READOOB;
1136 while (read < len) {
1137 thislen = oobsize - column;
1138 thislen = min_t(int, thislen, len);
1140 this->command(mtd, readcmd, from, mtd->oobsize);
1142 onenand_update_bufferram(mtd, from, 0);
1144 ret = this->wait(mtd, FL_READING);
1146 ret = onenand_recover_lsb(mtd, from, ret);
1148 if (ret && ret != -EBADMSG) {
1149 printk(KERN_ERR "onenand_read_oob_nolock: read failed = 0x%x\n", ret);
1153 if (mode == MTD_OOB_AUTO)
1154 onenand_transfer_auto_oob(mtd, buf, column, thislen);
1156 this->read_bufferram(mtd, 0, ONENAND_SPARERAM, buf, column, thislen);
1168 from += mtd->writesize;
1173 ops->oobretlen = read;
1178 if (mtd->ecc_stats.failed - stats.failed)
1185 * onenand_read - [MTD Interface] MTD compability function for onenand_read_ecc
1186 * @param mtd MTD device structure
1187 * @param from offset to read from
1188 * @param len number of bytes to read
1189 * @param retlen pointer to variable to store the number of read bytes
1190 * @param buf the databuffer to put data
1192 * This function simply calls onenand_read_ecc with oob buffer and oobsel = NULL
1194 static int onenand_read(struct mtd_info *mtd, loff_t from, size_t len,
1195 size_t * retlen, u_char * buf)
1197 struct mtd_oob_ops ops = {
1205 onenand_get_device(mtd, FL_READING);
1206 ret = onenand_read_ops_nolock(mtd, from, &ops);
1207 onenand_release_device(mtd);
1209 *retlen = ops.retlen;
1214 * onenand_read_oob - [MTD Interface] OneNAND read out-of-band
1215 * @param mtd MTD device structure
1216 * @param from offset to read from
1217 * @param ops oob operations description structure
1219 * OneNAND main and/or out-of-band
1221 static int onenand_read_oob(struct mtd_info *mtd, loff_t from,
1222 struct mtd_oob_ops *ops)
1226 switch (ops->mode) {
1231 /* Not implemented yet */
1236 onenand_get_device(mtd, FL_READING);
1238 ret = onenand_read_ops_nolock(mtd, from, ops);
1240 ret = onenand_read_oob_nolock(mtd, from, ops);
1241 onenand_release_device(mtd);
1247 * onenand_bbt_wait - [DEFAULT] wait until the command is done
1248 * @param mtd MTD device structure
1249 * @param state state to select the max. timeout value
1251 * Wait for command done.
1253 static int onenand_bbt_wait(struct mtd_info *mtd, int state)
1255 struct onenand_chip *this = mtd->priv;
1256 unsigned int flags = ONENAND_INT_MASTER;
1257 unsigned int interrupt;
1261 interrupt = this->read_word(this->base + ONENAND_REG_INTERRUPT);
1262 if (interrupt & flags)
1266 /* To get correct interrupt status in timeout case */
1267 interrupt = this->read_word(this->base + ONENAND_REG_INTERRUPT);
1268 ctrl = this->read_word(this->base + ONENAND_REG_CTRL_STATUS);
1270 if (interrupt & ONENAND_INT_READ) {
1271 int ecc = onenand_read_ecc(this);
1272 if (ecc & ONENAND_ECC_2BIT_ALL) {
1273 printk(KERN_INFO "onenand_bbt_wait: ecc error = 0x%04x"
1274 ", controller = 0x%04x\n", ecc, ctrl);
1275 return ONENAND_BBT_READ_ERROR;
1278 printk(KERN_ERR "onenand_bbt_wait: read timeout!"
1279 "ctrl=0x%04x intr=0x%04x\n", ctrl, interrupt);
1280 return ONENAND_BBT_READ_FATAL_ERROR;
1283 /* Initial bad block case: 0x2400 or 0x0400 */
1284 if (ctrl & ONENAND_CTRL_ERROR) {
1285 printk(KERN_DEBUG "onenand_bbt_wait: controller error = 0x%04x\n", ctrl);
1286 return ONENAND_BBT_READ_ERROR;
1293 * onenand_bbt_read_oob - [MTD Interface] OneNAND read out-of-band for bbt scan
1294 * @param mtd MTD device structure
1295 * @param from offset to read from
1296 * @param ops oob operation description structure
1298 * OneNAND read out-of-band data from the spare area for bbt scan
1300 int onenand_bbt_read_oob(struct mtd_info *mtd, loff_t from,
1301 struct mtd_oob_ops *ops)
1303 struct onenand_chip *this = mtd->priv;
1304 int read = 0, thislen, column;
1305 int ret = 0, readcmd;
1306 size_t len = ops->ooblen;
1307 u_char *buf = ops->oobbuf;
1309 MTDDEBUG(MTD_DEBUG_LEVEL3, "onenand_bbt_read_oob: from = 0x%08x, len = %zi\n", (unsigned int) from, len);
1311 readcmd = ONENAND_IS_MLC(this) ? ONENAND_CMD_READ : ONENAND_CMD_READOOB;
1313 /* Initialize return value */
1316 /* Do not allow reads past end of device */
1317 if (unlikely((from + len) > mtd->size)) {
1318 printk(KERN_ERR "onenand_bbt_read_oob: Attempt read beyond end of device\n");
1319 return ONENAND_BBT_READ_FATAL_ERROR;
1322 /* Grab the lock and see if the device is available */
1323 onenand_get_device(mtd, FL_READING);
1325 column = from & (mtd->oobsize - 1);
1327 while (read < len) {
1329 thislen = mtd->oobsize - column;
1330 thislen = min_t(int, thislen, len);
1332 this->command(mtd, readcmd, from, mtd->oobsize);
1334 onenand_update_bufferram(mtd, from, 0);
1336 ret = this->bbt_wait(mtd, FL_READING);
1338 ret = onenand_recover_lsb(mtd, from, ret);
1343 this->read_bufferram(mtd, 0, ONENAND_SPARERAM, buf, column, thislen);
1352 /* Update Page size */
1353 from += this->writesize;
1358 /* Deselect and wake up anyone waiting on the device */
1359 onenand_release_device(mtd);
1361 ops->oobretlen = read;
1365 #ifdef CONFIG_MTD_ONENAND_VERIFY_WRITE
1367 * onenand_verify_oob - [GENERIC] verify the oob contents after a write
1368 * @param mtd MTD device structure
1369 * @param buf the databuffer to verify
1370 * @param to offset to read from
1372 static int onenand_verify_oob(struct mtd_info *mtd, const u_char *buf, loff_t to)
1374 struct onenand_chip *this = mtd->priv;
1375 u_char *oob_buf = this->oob_buf;
1376 int status, i, readcmd;
1378 readcmd = ONENAND_IS_MLC(this) ? ONENAND_CMD_READ : ONENAND_CMD_READOOB;
1380 this->command(mtd, readcmd, to, mtd->oobsize);
1381 onenand_update_bufferram(mtd, to, 0);
1382 status = this->wait(mtd, FL_READING);
1386 this->read_bufferram(mtd, 0, ONENAND_SPARERAM, oob_buf, 0, mtd->oobsize);
1387 for (i = 0; i < mtd->oobsize; i++)
1388 if (buf[i] != 0xFF && buf[i] != oob_buf[i])
1395 * onenand_verify - [GENERIC] verify the chip contents after a write
1396 * @param mtd MTD device structure
1397 * @param buf the databuffer to verify
1398 * @param addr offset to read from
1399 * @param len number of bytes to read and compare
1401 static int onenand_verify(struct mtd_info *mtd, const u_char *buf, loff_t addr, size_t len)
1403 struct onenand_chip *this = mtd->priv;
1405 int thislen, column;
1408 thislen = min_t(int, this->writesize, len);
1409 column = addr & (this->writesize - 1);
1410 if (column + thislen > this->writesize)
1411 thislen = this->writesize - column;
1413 this->command(mtd, ONENAND_CMD_READ, addr, this->writesize);
1415 onenand_update_bufferram(mtd, addr, 0);
1417 ret = this->wait(mtd, FL_READING);
1421 onenand_update_bufferram(mtd, addr, 1);
1423 this->read_bufferram(mtd, 0, ONENAND_DATARAM, this->verify_buf, 0, mtd->writesize);
1425 if (memcmp(buf, this->verify_buf, thislen))
1436 #define onenand_verify(...) (0)
1437 #define onenand_verify_oob(...) (0)
1440 #define NOTALIGNED(x) ((x & (this->subpagesize - 1)) != 0)
1443 * onenand_fill_auto_oob - [Internal] oob auto-placement transfer
1444 * @param mtd MTD device structure
1445 * @param oob_buf oob buffer
1446 * @param buf source address
1447 * @param column oob offset to write to
1448 * @param thislen oob length to write
1450 static int onenand_fill_auto_oob(struct mtd_info *mtd, u_char *oob_buf,
1451 const u_char *buf, int column, int thislen)
1453 struct onenand_chip *this = mtd->priv;
1454 struct nand_oobfree *free;
1455 int writecol = column;
1456 int writeend = column + thislen;
1460 free = this->ecclayout->oobfree;
1461 for (i = 0; i < MTD_MAX_OOBFREE_ENTRIES && free->length; i++, free++) {
1462 if (writecol >= lastgap)
1463 writecol += free->offset - lastgap;
1464 if (writeend >= lastgap)
1465 writeend += free->offset - lastgap;
1466 lastgap = free->offset + free->length;
1468 free = this->ecclayout->oobfree;
1469 for (i = 0; i < MTD_MAX_OOBFREE_ENTRIES && free->length; i++, free++) {
1470 int free_end = free->offset + free->length;
1471 if (free->offset < writeend && free_end > writecol) {
1472 int st = max_t(int,free->offset,writecol);
1473 int ed = min_t(int,free_end,writeend);
1475 memcpy(oob_buf + st, buf, n);
1477 } else if (column == 0)
1484 * onenand_write_ops_nolock - [OneNAND Interface] write main and/or out-of-band
1485 * @param mtd MTD device structure
1486 * @param to offset to write to
1487 * @param ops oob operation description structure
1489 * Write main and/or oob with ECC
1491 static int onenand_write_ops_nolock(struct mtd_info *mtd, loff_t to,
1492 struct mtd_oob_ops *ops)
1494 struct onenand_chip *this = mtd->priv;
1495 int written = 0, column, thislen, subpage;
1496 int oobwritten = 0, oobcolumn, thisooblen, oobsize;
1497 size_t len = ops->len;
1498 size_t ooblen = ops->ooblen;
1499 const u_char *buf = ops->datbuf;
1500 const u_char *oob = ops->oobbuf;
1504 MTDDEBUG(MTD_DEBUG_LEVEL3, "onenand_write_ops_nolock: to = 0x%08x, len = %i\n", (unsigned int) to, (int) len);
1506 /* Initialize retlen, in case of early exit */
1510 /* Do not allow writes past end of device */
1511 if (unlikely((to + len) > mtd->size)) {
1512 printk(KERN_ERR "onenand_write_ops_nolock: Attempt write to past end of device\n");
1516 /* Reject writes, which are not page aligned */
1517 if (unlikely(NOTALIGNED(to) || NOTALIGNED(len))) {
1518 printk(KERN_ERR "onenand_write_ops_nolock: Attempt to write not page aligned data\n");
1522 if (ops->mode == MTD_OOB_AUTO)
1523 oobsize = this->ecclayout->oobavail;
1525 oobsize = mtd->oobsize;
1527 oobcolumn = to & (mtd->oobsize - 1);
1529 column = to & (mtd->writesize - 1);
1531 /* Loop until all data write */
1532 while (written < len) {
1533 u_char *wbuf = (u_char *) buf;
1535 thislen = min_t(int, mtd->writesize - column, len - written);
1536 thisooblen = min_t(int, oobsize - oobcolumn, ooblen - oobwritten);
1538 this->command(mtd, ONENAND_CMD_BUFFERRAM, to, thislen);
1540 /* Partial page write */
1541 subpage = thislen < mtd->writesize;
1543 memset(this->page_buf, 0xff, mtd->writesize);
1544 memcpy(this->page_buf + column, buf, thislen);
1545 wbuf = this->page_buf;
1548 this->write_bufferram(mtd, to, ONENAND_DATARAM, wbuf, 0, mtd->writesize);
1551 oobbuf = this->oob_buf;
1553 /* We send data to spare ram with oobsize
1554 * * to prevent byte access */
1555 memset(oobbuf, 0xff, mtd->oobsize);
1556 if (ops->mode == MTD_OOB_AUTO)
1557 onenand_fill_auto_oob(mtd, oobbuf, oob, oobcolumn, thisooblen);
1559 memcpy(oobbuf + oobcolumn, oob, thisooblen);
1561 oobwritten += thisooblen;
1565 oobbuf = (u_char *) ffchars;
1567 this->write_bufferram(mtd, to, ONENAND_SPARERAM, oobbuf, 0, mtd->oobsize);
1569 this->command(mtd, ONENAND_CMD_PROG, to, mtd->writesize);
1571 ret = this->wait(mtd, FL_WRITING);
1573 /* In partial page write we don't update bufferram */
1574 onenand_update_bufferram(mtd, to, !ret && !subpage);
1575 if (ONENAND_IS_2PLANE(this)) {
1576 ONENAND_SET_BUFFERRAM1(this);
1577 onenand_update_bufferram(mtd, to + this->writesize, !ret && !subpage);
1581 printk(KERN_ERR "onenand_write_ops_nolock: write filaed %d\n", ret);
1585 /* Only check verify write turn on */
1586 ret = onenand_verify(mtd, buf, to, thislen);
1588 printk(KERN_ERR "onenand_write_ops_nolock: verify failed %d\n", ret);
1602 ops->retlen = written;
1608 * onenand_write_oob_nolock - [Internal] OneNAND write out-of-band
1609 * @param mtd MTD device structure
1610 * @param to offset to write to
1611 * @param len number of bytes to write
1612 * @param retlen pointer to variable to store the number of written bytes
1613 * @param buf the data to write
1614 * @param mode operation mode
1616 * OneNAND write out-of-band
1618 static int onenand_write_oob_nolock(struct mtd_info *mtd, loff_t to,
1619 struct mtd_oob_ops *ops)
1621 struct onenand_chip *this = mtd->priv;
1622 int column, ret = 0, oobsize;
1623 int written = 0, oobcmd;
1625 size_t len = ops->ooblen;
1626 const u_char *buf = ops->oobbuf;
1627 mtd_oob_mode_t mode = ops->mode;
1631 MTDDEBUG(MTD_DEBUG_LEVEL3, "onenand_write_oob_nolock: to = 0x%08x, len = %i\n", (unsigned int) to, (int) len);
1633 /* Initialize retlen, in case of early exit */
1636 if (mode == MTD_OOB_AUTO)
1637 oobsize = this->ecclayout->oobavail;
1639 oobsize = mtd->oobsize;
1641 column = to & (mtd->oobsize - 1);
1643 if (unlikely(column >= oobsize)) {
1644 printk(KERN_ERR "onenand_write_oob_nolock: Attempted to start write outside oob\n");
1648 /* For compatibility with NAND: Do not allow write past end of page */
1649 if (unlikely(column + len > oobsize)) {
1650 printk(KERN_ERR "onenand_write_oob_nolock: "
1651 "Attempt to write past end of page\n");
1655 /* Do not allow reads past end of device */
1656 if (unlikely(to >= mtd->size ||
1657 column + len > ((mtd->size >> this->page_shift) -
1658 (to >> this->page_shift)) * oobsize)) {
1659 printk(KERN_ERR "onenand_write_oob_nolock: Attempted to write past end of device\n");
1663 oobbuf = this->oob_buf;
1665 oobcmd = ONENAND_IS_MLC(this) ? ONENAND_CMD_PROG : ONENAND_CMD_PROGOOB;
1667 /* Loop until all data write */
1668 while (written < len) {
1669 int thislen = min_t(int, oobsize, len - written);
1671 this->command(mtd, ONENAND_CMD_BUFFERRAM, to, mtd->oobsize);
1673 /* We send data to spare ram with oobsize
1674 * to prevent byte access */
1675 memset(oobbuf, 0xff, mtd->oobsize);
1676 if (mode == MTD_OOB_AUTO)
1677 onenand_fill_auto_oob(mtd, oobbuf, buf, column, thislen);
1679 memcpy(oobbuf + column, buf, thislen);
1680 this->write_bufferram(mtd, 0, ONENAND_SPARERAM, oobbuf, 0, mtd->oobsize);
1682 if (ONENAND_IS_MLC(this)) {
1683 /* Set main area of DataRAM to 0xff*/
1684 memset(this->page_buf, 0xff, mtd->writesize);
1685 this->write_bufferram(mtd, 0, ONENAND_DATARAM,
1686 this->page_buf, 0, mtd->writesize);
1689 this->command(mtd, oobcmd, to, mtd->oobsize);
1691 onenand_update_bufferram(mtd, to, 0);
1692 if (ONENAND_IS_2PLANE(this)) {
1693 ONENAND_SET_BUFFERRAM1(this);
1694 onenand_update_bufferram(mtd, to + this->writesize, 0);
1697 ret = this->wait(mtd, FL_WRITING);
1699 printk(KERN_ERR "onenand_write_oob_nolock: write failed %d\n", ret);
1703 ret = onenand_verify_oob(mtd, oobbuf, to);
1705 printk(KERN_ERR "onenand_write_oob_nolock: verify failed %d\n", ret);
1713 to += mtd->writesize;
1718 ops->oobretlen = written;
1724 * onenand_write - [MTD Interface] compability function for onenand_write_ecc
1725 * @param mtd MTD device structure
1726 * @param to offset to write to
1727 * @param len number of bytes to write
1728 * @param retlen pointer to variable to store the number of written bytes
1729 * @param buf the data to write
1733 static int onenand_write(struct mtd_info *mtd, loff_t to, size_t len,
1734 size_t * retlen, const u_char * buf)
1736 struct mtd_oob_ops ops = {
1739 .datbuf = (u_char *) buf,
1741 .mode = MTD_OOB_AUTO,
1745 onenand_get_device(mtd, FL_WRITING);
1746 ret = onenand_write_ops_nolock(mtd, to, &ops);
1747 onenand_release_device(mtd);
1749 *retlen = ops.retlen;
1754 * onenand_write_oob - [MTD Interface] OneNAND write out-of-band
1755 * @param mtd MTD device structure
1756 * @param to offset to write to
1757 * @param ops oob operation description structure
1759 * OneNAND write main and/or out-of-band
1761 int onenand_write_oob(struct mtd_info *mtd, loff_t to,
1762 struct mtd_oob_ops *ops)
1766 switch (ops->mode) {
1771 /* Not implemented yet */
1776 onenand_get_device(mtd, FL_WRITING);
1778 ret = onenand_write_ops_nolock(mtd, to, ops);
1780 ret = onenand_write_oob_nolock(mtd, to, ops);
1781 onenand_release_device(mtd);
1788 * onenand_block_isbad_nolock - [GENERIC] Check if a block is marked bad
1789 * @param mtd MTD device structure
1790 * @param ofs offset from device start
1791 * @param allowbbt 1, if its allowed to access the bbt area
1793 * Check, if the block is bad, Either by reading the bad block table or
1794 * calling of the scan function.
1796 static int onenand_block_isbad_nolock(struct mtd_info *mtd, loff_t ofs, int allowbbt)
1798 struct onenand_chip *this = mtd->priv;
1799 struct bbm_info *bbm = this->bbm;
1801 /* Return info from the table */
1802 return bbm->isbad_bbt(mtd, ofs, allowbbt);
1807 * onenand_erase - [MTD Interface] erase block(s)
1808 * @param mtd MTD device structure
1809 * @param instr erase instruction
1811 * Erase one ore more blocks
1813 static int onenand_erase(struct mtd_info *mtd, struct erase_info *instr)
1815 struct onenand_chip *this = mtd->priv;
1816 unsigned int block_size;
1817 loff_t addr = instr->addr;
1818 unsigned int len = instr->len;
1820 struct mtd_erase_region_info *region = NULL;
1821 unsigned int region_end = 0;
1823 MTDDEBUG(MTD_DEBUG_LEVEL3, "onenand_erase: start = 0x%08x, len = %i\n",
1824 (unsigned int) addr, len);
1826 /* Do not allow erase past end of device */
1827 if (unlikely((len + addr) > mtd->size)) {
1828 MTDDEBUG(MTD_DEBUG_LEVEL0, "onenand_erase:"
1829 "Erase past end of device\n");
1833 if (FLEXONENAND(this)) {
1834 /* Find the eraseregion of this address */
1835 i = flexonenand_region(mtd, addr);
1836 region = &mtd->eraseregions[i];
1838 block_size = region->erasesize;
1839 region_end = region->offset
1840 + region->erasesize * region->numblocks;
1842 /* Start address within region must align on block boundary.
1843 * Erase region's start offset is always block start address.
1845 if (unlikely((addr - region->offset) & (block_size - 1))) {
1846 MTDDEBUG(MTD_DEBUG_LEVEL0, "onenand_erase:"
1847 " Unaligned address\n");
1851 block_size = 1 << this->erase_shift;
1853 /* Start address must align on block boundary */
1854 if (unlikely(addr & (block_size - 1))) {
1855 MTDDEBUG(MTD_DEBUG_LEVEL0, "onenand_erase:"
1856 "Unaligned address\n");
1861 /* Length must align on block boundary */
1862 if (unlikely(len & (block_size - 1))) {
1863 MTDDEBUG (MTD_DEBUG_LEVEL0,
1864 "onenand_erase: Length not block aligned\n");
1868 instr->fail_addr = 0xffffffff;
1870 /* Grab the lock and see if the device is available */
1871 onenand_get_device(mtd, FL_ERASING);
1873 /* Loop throught the pages */
1874 instr->state = MTD_ERASING;
1878 /* Check if we have a bad block, we do not erase bad blocks */
1879 if (instr->priv == 0 && onenand_block_isbad_nolock(mtd, addr, 0)) {
1880 printk(KERN_WARNING "onenand_erase: attempt to erase"
1881 " a bad block at addr 0x%08x\n",
1882 (unsigned int) addr);
1883 instr->state = MTD_ERASE_FAILED;
1887 this->command(mtd, ONENAND_CMD_ERASE, addr, block_size);
1889 onenand_invalidate_bufferram(mtd, addr, block_size);
1891 ret = this->wait(mtd, FL_ERASING);
1892 /* Check, if it is write protected */
1895 MTDDEBUG (MTD_DEBUG_LEVEL0, "onenand_erase: "
1896 "Device is write protected!!!\n");
1898 MTDDEBUG (MTD_DEBUG_LEVEL0, "onenand_erase: "
1899 "Failed erase, block %d\n",
1900 onenand_block(this, addr));
1901 instr->state = MTD_ERASE_FAILED;
1902 instr->fail_addr = addr;
1908 if (addr == region_end) {
1913 block_size = region->erasesize;
1914 region_end = region->offset
1915 + region->erasesize * region->numblocks;
1917 if (len & (block_size - 1)) {
1918 /* This has been checked at MTD
1919 * partitioning level. */
1920 printk("onenand_erase: Unaligned address\n");
1926 instr->state = MTD_ERASE_DONE;
1930 ret = instr->state == MTD_ERASE_DONE ? 0 : -EIO;
1931 /* Do call back function */
1933 mtd_erase_callback(instr);
1935 /* Deselect and wake up anyone waiting on the device */
1936 onenand_release_device(mtd);
1942 * onenand_sync - [MTD Interface] sync
1943 * @param mtd MTD device structure
1945 * Sync is actually a wait for chip ready function
1947 static void onenand_sync(struct mtd_info *mtd)
1949 MTDDEBUG (MTD_DEBUG_LEVEL3, "onenand_sync: called\n");
1951 /* Grab the lock and see if the device is available */
1952 onenand_get_device(mtd, FL_SYNCING);
1954 /* Release it and go back */
1955 onenand_release_device(mtd);
1959 * onenand_block_isbad - [MTD Interface] Check whether the block at the given offset is bad
1960 * @param mtd MTD device structure
1961 * @param ofs offset relative to mtd start
1963 * Check whether the block is bad
1965 static int onenand_block_isbad(struct mtd_info *mtd, loff_t ofs)
1969 /* Check for invalid offset */
1970 if (ofs > mtd->size)
1973 onenand_get_device(mtd, FL_READING);
1974 ret = onenand_block_isbad_nolock(mtd, ofs, 0);
1975 onenand_release_device(mtd);
1980 * onenand_default_block_markbad - [DEFAULT] mark a block bad
1981 * @param mtd MTD device structure
1982 * @param ofs offset from device start
1984 * This is the default implementation, which can be overridden by
1985 * a hardware specific driver.
1987 static int onenand_default_block_markbad(struct mtd_info *mtd, loff_t ofs)
1989 struct onenand_chip *this = mtd->priv;
1990 struct bbm_info *bbm = this->bbm;
1991 u_char buf[2] = {0, 0};
1992 struct mtd_oob_ops ops = {
1993 .mode = MTD_OOB_PLACE,
2000 /* Get block number */
2001 block = onenand_block(this, ofs);
2003 bbm->bbt[block >> 2] |= 0x01 << ((block & 0x03) << 1);
2005 /* We write two bytes, so we dont have to mess with 16 bit access */
2006 ofs += mtd->oobsize + (bbm->badblockpos & ~0x01);
2007 return onenand_write_oob_nolock(mtd, ofs, &ops);
2011 * onenand_block_markbad - [MTD Interface] Mark the block at the given offset as bad
2012 * @param mtd MTD device structure
2013 * @param ofs offset relative to mtd start
2015 * Mark the block as bad
2017 static int onenand_block_markbad(struct mtd_info *mtd, loff_t ofs)
2019 struct onenand_chip *this = mtd->priv;
2022 ret = onenand_block_isbad(mtd, ofs);
2024 /* If it was bad already, return success and do nothing */
2030 ret = this->block_markbad(mtd, ofs);
2035 * onenand_do_lock_cmd - [OneNAND Interface] Lock or unlock block(s)
2036 * @param mtd MTD device structure
2037 * @param ofs offset relative to mtd start
2038 * @param len number of bytes to lock or unlock
2039 * @param cmd lock or unlock command
2041 * Lock or unlock one or more blocks
2043 static int onenand_do_lock_cmd(struct mtd_info *mtd, loff_t ofs, size_t len, int cmd)
2045 struct onenand_chip *this = mtd->priv;
2046 int start, end, block, value, status;
2049 start = onenand_block(this, ofs);
2050 end = onenand_block(this, ofs + len);
2052 if (cmd == ONENAND_CMD_LOCK)
2053 wp_status_mask = ONENAND_WP_LS;
2055 wp_status_mask = ONENAND_WP_US;
2057 /* Continuous lock scheme */
2058 if (this->options & ONENAND_HAS_CONT_LOCK) {
2059 /* Set start block address */
2060 this->write_word(start,
2061 this->base + ONENAND_REG_START_BLOCK_ADDRESS);
2062 /* Set end block address */
2063 this->write_word(end - 1,
2064 this->base + ONENAND_REG_END_BLOCK_ADDRESS);
2065 /* Write unlock command */
2066 this->command(mtd, cmd, 0, 0);
2068 /* There's no return value */
2069 this->wait(mtd, FL_UNLOCKING);
2072 while (this->read_word(this->base + ONENAND_REG_CTRL_STATUS)
2073 & ONENAND_CTRL_ONGO)
2076 /* Check lock status */
2077 status = this->read_word(this->base + ONENAND_REG_WP_STATUS);
2078 if (!(status & ONENAND_WP_US))
2079 printk(KERN_ERR "wp status = 0x%x\n", status);
2084 /* Block lock scheme */
2085 for (block = start; block < end; block++) {
2086 /* Set block address */
2087 value = onenand_block_address(this, block);
2088 this->write_word(value, this->base + ONENAND_REG_START_ADDRESS1);
2089 /* Select DataRAM for DDP */
2090 value = onenand_bufferram_address(this, block);
2091 this->write_word(value, this->base + ONENAND_REG_START_ADDRESS2);
2093 /* Set start block address */
2094 this->write_word(block,
2095 this->base + ONENAND_REG_START_BLOCK_ADDRESS);
2096 /* Write unlock command */
2097 this->command(mtd, ONENAND_CMD_UNLOCK, 0, 0);
2099 /* There's no return value */
2100 this->wait(mtd, FL_UNLOCKING);
2103 while (this->read_word(this->base + ONENAND_REG_CTRL_STATUS)
2104 & ONENAND_CTRL_ONGO)
2107 /* Check lock status */
2108 status = this->read_word(this->base + ONENAND_REG_WP_STATUS);
2109 if (!(status & ONENAND_WP_US))
2110 printk(KERN_ERR "block = %d, wp status = 0x%x\n",
2117 #ifdef ONENAND_LINUX
2119 * onenand_lock - [MTD Interface] Lock block(s)
2120 * @param mtd MTD device structure
2121 * @param ofs offset relative to mtd start
2122 * @param len number of bytes to unlock
2124 * Lock one or more blocks
2126 static int onenand_lock(struct mtd_info *mtd, loff_t ofs, size_t len)
2130 onenand_get_device(mtd, FL_LOCKING);
2131 ret = onenand_do_lock_cmd(mtd, ofs, len, ONENAND_CMD_LOCK);
2132 onenand_release_device(mtd);
2137 * onenand_unlock - [MTD Interface] Unlock block(s)
2138 * @param mtd MTD device structure
2139 * @param ofs offset relative to mtd start
2140 * @param len number of bytes to unlock
2142 * Unlock one or more blocks
2144 static int onenand_unlock(struct mtd_info *mtd, loff_t ofs, size_t len)
2148 onenand_get_device(mtd, FL_LOCKING);
2149 ret = onenand_do_lock_cmd(mtd, ofs, len, ONENAND_CMD_UNLOCK);
2150 onenand_release_device(mtd);
2156 * onenand_check_lock_status - [OneNAND Interface] Check lock status
2157 * @param this onenand chip data structure
2161 static int onenand_check_lock_status(struct onenand_chip *this)
2163 unsigned int value, block, status;
2166 end = this->chipsize >> this->erase_shift;
2167 for (block = 0; block < end; block++) {
2168 /* Set block address */
2169 value = onenand_block_address(this, block);
2170 this->write_word(value, this->base + ONENAND_REG_START_ADDRESS1);
2171 /* Select DataRAM for DDP */
2172 value = onenand_bufferram_address(this, block);
2173 this->write_word(value, this->base + ONENAND_REG_START_ADDRESS2);
2174 /* Set start block address */
2175 this->write_word(block, this->base + ONENAND_REG_START_BLOCK_ADDRESS);
2177 /* Check lock status */
2178 status = this->read_word(this->base + ONENAND_REG_WP_STATUS);
2179 if (!(status & ONENAND_WP_US)) {
2180 printk(KERN_ERR "block = %d, wp status = 0x%x\n", block, status);
2189 * onenand_unlock_all - [OneNAND Interface] unlock all blocks
2190 * @param mtd MTD device structure
2194 static void onenand_unlock_all(struct mtd_info *mtd)
2196 struct onenand_chip *this = mtd->priv;
2198 size_t len = mtd->size;
2200 if (this->options & ONENAND_HAS_UNLOCK_ALL) {
2201 /* Set start block address */
2202 this->write_word(0, this->base + ONENAND_REG_START_BLOCK_ADDRESS);
2203 /* Write unlock command */
2204 this->command(mtd, ONENAND_CMD_UNLOCK_ALL, 0, 0);
2206 /* There's no return value */
2207 this->wait(mtd, FL_LOCKING);
2210 while (this->read_word(this->base + ONENAND_REG_CTRL_STATUS)
2211 & ONENAND_CTRL_ONGO)
2214 /* Check lock status */
2215 if (onenand_check_lock_status(this))
2218 /* Workaround for all block unlock in DDP */
2219 if (ONENAND_IS_DDP(this) && !FLEXONENAND(this)) {
2220 /* All blocks on another chip */
2221 ofs = this->chipsize >> 1;
2222 len = this->chipsize >> 1;
2226 onenand_do_lock_cmd(mtd, ofs, len, ONENAND_CMD_UNLOCK);
2231 * onenand_check_features - Check and set OneNAND features
2232 * @param mtd MTD data structure
2234 * Check and set OneNAND features
2238 static void onenand_check_features(struct mtd_info *mtd)
2240 struct onenand_chip *this = mtd->priv;
2241 unsigned int density, process;
2242 unsigned int syscfg;
2244 /* Lock scheme depends on density and process */
2245 density = onenand_get_density(this->device_id);
2246 process = this->version_id >> ONENAND_VERSION_PROCESS_SHIFT;
2250 case ONENAND_DEVICE_DENSITY_4Gb:
2251 if (ONENAND_IS_DDP(this))
2252 this->options |= ONENAND_HAS_2PLANE;
2254 this->options |= ONENAND_HAS_4KB_PAGE;
2256 case ONENAND_DEVICE_DENSITY_2Gb:
2257 /* 2Gb DDP don't have 2 plane */
2258 if (!ONENAND_IS_DDP(this))
2259 this->options |= ONENAND_HAS_2PLANE;
2260 this->options |= ONENAND_HAS_UNLOCK_ALL;
2262 case ONENAND_DEVICE_DENSITY_1Gb:
2263 /* A-Die has all block unlock */
2265 this->options |= ONENAND_HAS_UNLOCK_ALL;
2269 /* Some OneNAND has continuous lock scheme */
2271 this->options |= ONENAND_HAS_CONT_LOCK;
2275 if (ONENAND_IS_4KB_PAGE(this))
2276 this->options &= ~ONENAND_HAS_2PLANE;
2278 if (FLEXONENAND(this)) {
2279 this->options &= ~ONENAND_HAS_CONT_LOCK;
2280 this->options |= ONENAND_HAS_UNLOCK_ALL;
2283 syscfg = this->read_word(this->base + ONENAND_REG_SYS_CFG1);
2284 if (syscfg & ONENAND_SYS_CFG1_SYNC_READ) {
2285 printk(KERN_DEBUG "Sync Burst Mode\n");
2286 this->options |= ONENAND_SYNC_MODE;
2289 if (this->options & ONENAND_HAS_CONT_LOCK)
2290 printk(KERN_DEBUG "Lock scheme is Continuous Lock\n");
2291 if (this->options & ONENAND_HAS_UNLOCK_ALL)
2292 printk(KERN_DEBUG "Chip support all block unlock\n");
2293 #ifdef ONENAND_LINUX
2294 if (this->options & ONENAND_HAS_2PLANE)
2295 printk(KERN_DEBUG "Chip has 2 plane\n");
2297 if (this->options & ONENAND_HAS_4KB_PAGE)
2298 printk(KERN_DEBUG "Chip has 4KiB pagesize\n");
2302 * onenand_print_device_info - Print device ID
2303 * @param device device ID
2307 char *onenand_print_device_info(int device, int version)
2309 int vcc, demuxed, ddp, density, flexonenand;
2310 char *dev_info = malloc(80);
2315 vcc = device & ONENAND_DEVICE_VCC_MASK;
2316 demuxed = device & ONENAND_DEVICE_IS_DEMUX;
2317 ddp = device & ONENAND_DEVICE_IS_DDP;
2318 density = onenand_get_density(device);
2319 flexonenand = device & DEVICE_IS_FLEXONENAND;
2321 p += sprintf(dev_info, "%s%sOneNAND%s %dMB %sV 16-bit (0x%02x)",
2322 demuxed ? "" : "Muxed ",
2323 flexonenand ? "Flex-" : "",
2325 (16 << density), vcc ? "2.65/3.3" : "1.8", device);
2327 sprintf(p, "\nOneNAND version = 0x%04x", version);
2328 printk("%s\n", dev_info);
2334 static const struct onenand_manufacturers onenand_manuf_ids[] = {
2335 {ONENAND_MFR_SAMSUNG, "Samsung"},
2339 * onenand_check_maf - Check manufacturer ID
2340 * @param manuf manufacturer ID
2342 * Check manufacturer ID
2344 static int onenand_check_maf(int manuf)
2346 int size = ARRAY_SIZE(onenand_manuf_ids);
2350 for (i = 0; i < size; i++)
2351 if (manuf == onenand_manuf_ids[i].id)
2355 name = onenand_manuf_ids[i].name;
2359 #ifdef ONENAND_DEBUG
2360 printk(KERN_DEBUG "OneNAND Manufacturer: %s (0x%0x)\n", name, manuf);
2367 * flexonenand_get_boundary - Reads the SLC boundary
2368 * @param onenand_info - onenand info structure
2370 * Fill up boundary[] field in onenand_chip
2372 static int flexonenand_get_boundary(struct mtd_info *mtd)
2374 struct onenand_chip *this = mtd->priv;
2375 unsigned int die, bdry;
2376 int ret, syscfg, locked;
2379 syscfg = this->read_word(this->base + ONENAND_REG_SYS_CFG1);
2380 this->write_word((syscfg | 0x0100), this->base + ONENAND_REG_SYS_CFG1);
2382 for (die = 0; die < this->dies; die++) {
2383 this->command(mtd, FLEXONENAND_CMD_PI_ACCESS, die, 0);
2384 this->wait(mtd, FL_SYNCING);
2386 this->command(mtd, FLEXONENAND_CMD_READ_PI, die, 0);
2387 ret = this->wait(mtd, FL_READING);
2389 bdry = this->read_word(this->base + ONENAND_DATARAM);
2390 if ((bdry >> FLEXONENAND_PI_UNLOCK_SHIFT) == 3)
2394 this->boundary[die] = bdry & FLEXONENAND_PI_MASK;
2396 this->command(mtd, ONENAND_CMD_RESET, 0, 0);
2397 ret = this->wait(mtd, FL_RESETING);
2399 printk(KERN_INFO "Die %d boundary: %d%s\n", die,
2400 this->boundary[die], locked ? "(Locked)" : "(Unlocked)");
2404 this->write_word(syscfg, this->base + ONENAND_REG_SYS_CFG1);
2409 * flexonenand_get_size - Fill up fields in onenand_chip and mtd_info
2410 * boundary[], diesize[], mtd->size, mtd->erasesize,
2412 * @param mtd - MTD device structure
2414 static void flexonenand_get_size(struct mtd_info *mtd)
2416 struct onenand_chip *this = mtd->priv;
2417 int die, i, eraseshift, density;
2418 int blksperdie, maxbdry;
2421 density = onenand_get_density(this->device_id);
2422 blksperdie = ((loff_t)(16 << density) << 20) >> (this->erase_shift);
2423 blksperdie >>= ONENAND_IS_DDP(this) ? 1 : 0;
2424 maxbdry = blksperdie - 1;
2425 eraseshift = this->erase_shift - 1;
2427 mtd->numeraseregions = this->dies << 1;
2429 /* This fills up the device boundary */
2430 flexonenand_get_boundary(mtd);
2434 for (; die < this->dies; die++) {
2435 if (!die || this->boundary[die-1] != maxbdry) {
2437 mtd->eraseregions[i].offset = ofs;
2438 mtd->eraseregions[i].erasesize = 1 << eraseshift;
2439 mtd->eraseregions[i].numblocks =
2440 this->boundary[die] + 1;
2441 ofs += mtd->eraseregions[i].numblocks << eraseshift;
2444 mtd->numeraseregions -= 1;
2445 mtd->eraseregions[i].numblocks +=
2446 this->boundary[die] + 1;
2447 ofs += (this->boundary[die] + 1) << (eraseshift - 1);
2449 if (this->boundary[die] != maxbdry) {
2451 mtd->eraseregions[i].offset = ofs;
2452 mtd->eraseregions[i].erasesize = 1 << eraseshift;
2453 mtd->eraseregions[i].numblocks = maxbdry ^
2454 this->boundary[die];
2455 ofs += mtd->eraseregions[i].numblocks << eraseshift;
2458 mtd->numeraseregions -= 1;
2461 /* Expose MLC erase size except when all blocks are SLC */
2462 mtd->erasesize = 1 << this->erase_shift;
2463 if (mtd->numeraseregions == 1)
2464 mtd->erasesize >>= 1;
2466 printk(KERN_INFO "Device has %d eraseregions\n", mtd->numeraseregions);
2467 for (i = 0; i < mtd->numeraseregions; i++)
2468 printk(KERN_INFO "[offset: 0x%08llx, erasesize: 0x%05x,"
2469 " numblocks: %04u]\n", mtd->eraseregions[i].offset,
2470 mtd->eraseregions[i].erasesize,
2471 mtd->eraseregions[i].numblocks);
2473 for (die = 0, mtd->size = 0; die < this->dies; die++) {
2474 this->diesize[die] = (loff_t) (blksperdie << this->erase_shift);
2475 this->diesize[die] -= (loff_t) (this->boundary[die] + 1)
2476 << (this->erase_shift - 1);
2477 mtd->size += this->diesize[die];
2482 * flexonenand_check_blocks_erased - Check if blocks are erased
2483 * @param mtd_info - mtd info structure
2484 * @param start - first erase block to check
2485 * @param end - last erase block to check
2487 * Converting an unerased block from MLC to SLC
2488 * causes byte values to change. Since both data and its ECC
2489 * have changed, reads on the block give uncorrectable error.
2490 * This might lead to the block being detected as bad.
2492 * Avoid this by ensuring that the block to be converted is
2495 static int flexonenand_check_blocks_erased(struct mtd_info *mtd,
2498 struct onenand_chip *this = mtd->priv;
2501 struct mtd_oob_ops ops = {
2502 .mode = MTD_OOB_PLACE,
2504 .ooblen = mtd->oobsize,
2506 .oobbuf = this->oob_buf,
2510 printk(KERN_DEBUG "Check blocks from %d to %d\n", start, end);
2512 for (block = start; block <= end; block++) {
2513 addr = flexonenand_addr(this, block);
2514 if (onenand_block_isbad_nolock(mtd, addr, 0))
2518 * Since main area write results in ECC write to spare,
2519 * it is sufficient to check only ECC bytes for change.
2521 ret = onenand_read_oob_nolock(mtd, addr, &ops);
2525 for (i = 0; i < mtd->oobsize; i++)
2526 if (this->oob_buf[i] != 0xff)
2529 if (i != mtd->oobsize) {
2530 printk(KERN_WARNING "Block %d not erased.\n", block);
2539 * flexonenand_set_boundary - Writes the SLC boundary
2540 * @param mtd - mtd info structure
2542 int flexonenand_set_boundary(struct mtd_info *mtd, int die,
2543 int boundary, int lock)
2545 struct onenand_chip *this = mtd->priv;
2546 int ret, density, blksperdie, old, new, thisboundary;
2549 if (die >= this->dies)
2552 if (boundary == this->boundary[die])
2555 density = onenand_get_density(this->device_id);
2556 blksperdie = ((16 << density) << 20) >> this->erase_shift;
2557 blksperdie >>= ONENAND_IS_DDP(this) ? 1 : 0;
2559 if (boundary >= blksperdie) {
2560 printk("flexonenand_set_boundary:"
2561 "Invalid boundary value. "
2562 "Boundary not changed.\n");
2566 /* Check if converting blocks are erased */
2567 old = this->boundary[die] + (die * this->density_mask);
2568 new = boundary + (die * this->density_mask);
2569 ret = flexonenand_check_blocks_erased(mtd, min(old, new)
2570 + 1, max(old, new));
2572 printk(KERN_ERR "flexonenand_set_boundary: Please erase blocks before boundary change\n");
2576 this->command(mtd, FLEXONENAND_CMD_PI_ACCESS, die, 0);
2577 this->wait(mtd, FL_SYNCING);
2579 /* Check is boundary is locked */
2580 this->command(mtd, FLEXONENAND_CMD_READ_PI, die, 0);
2581 ret = this->wait(mtd, FL_READING);
2583 thisboundary = this->read_word(this->base + ONENAND_DATARAM);
2584 if ((thisboundary >> FLEXONENAND_PI_UNLOCK_SHIFT) != 3) {
2585 printk(KERN_ERR "flexonenand_set_boundary: boundary locked\n");
2589 printk(KERN_INFO "flexonenand_set_boundary: Changing die %d boundary: %d%s\n",
2590 die, boundary, lock ? "(Locked)" : "(Unlocked)");
2592 boundary &= FLEXONENAND_PI_MASK;
2593 boundary |= lock ? 0 : (3 << FLEXONENAND_PI_UNLOCK_SHIFT);
2595 addr = die ? this->diesize[0] : 0;
2596 this->command(mtd, ONENAND_CMD_ERASE, addr, 0);
2597 ret = this->wait(mtd, FL_ERASING);
2599 printk("flexonenand_set_boundary:"
2600 "Failed PI erase for Die %d\n", die);
2604 this->write_word(boundary, this->base + ONENAND_DATARAM);
2605 this->command(mtd, ONENAND_CMD_PROG, addr, 0);
2606 ret = this->wait(mtd, FL_WRITING);
2608 printk("flexonenand_set_boundary:"
2609 "Failed PI write for Die %d\n", die);
2613 this->command(mtd, FLEXONENAND_CMD_PI_UPDATE, die, 0);
2614 ret = this->wait(mtd, FL_WRITING);
2616 this->write_word(ONENAND_CMD_RESET, this->base + ONENAND_REG_COMMAND);
2617 this->wait(mtd, FL_RESETING);
2619 /* Recalculate device size on boundary change*/
2620 flexonenand_get_size(mtd);
2626 * onenand_probe - [OneNAND Interface] Probe the OneNAND device
2627 * @param mtd MTD device structure
2629 * OneNAND detection method:
2630 * Compare the the values from command with ones from register
2632 static int onenand_probe(struct mtd_info *mtd)
2634 struct onenand_chip *this = mtd->priv;
2635 int bram_maf_id, bram_dev_id, maf_id, dev_id, ver_id;
2638 #if defined(CONFIG_S5PC110) || defined(CONFIG_S5P6442)
2639 int onenand_if_ctrl_cfg;
2642 /* Save system configuration 1 */
2643 syscfg = this->read_word(this->base + ONENAND_REG_SYS_CFG1);
2645 #if defined(CONFIG_S5PC110) || defined(CONFIG_S5P6442)
2646 if (syscfg & ONENAND_SYS_CFG1_WM) {
2647 this->write_word(syscfg & ~(ONENAND_SYS_CFG1_WM), this->base + ONENAND_REG_SYS_CFG1);
2648 onenand_if_ctrl_cfg = readl(0xB0600100);
2649 writel(onenand_if_ctrl_cfg & ~ONENAND_SYS_CFG1_WM, 0xB0600100);
2652 /* Clear Sync. Burst Read mode to read BootRAM */
2653 this->write_word((syscfg & ~ONENAND_SYS_CFG1_SYNC_READ), this->base + ONENAND_REG_SYS_CFG1);
2656 /* Send the command for reading device ID from BootRAM */
2657 this->write_word(ONENAND_CMD_READID, this->base + ONENAND_BOOTRAM);
2659 /* Read manufacturer and device IDs from BootRAM */
2660 bram_maf_id = this->read_word(this->base + ONENAND_BOOTRAM + 0x0);
2661 bram_dev_id = this->read_word(this->base + ONENAND_BOOTRAM + 0x2);
2663 /* Reset OneNAND to read default register values */
2664 this->write_word(ONENAND_CMD_RESET, this->base + ONENAND_BOOTRAM);
2667 this->wait(mtd, FL_RESETING);
2669 /* Restore system configuration 1 */
2670 this->write_word(syscfg, this->base + ONENAND_REG_SYS_CFG1);
2672 #if defined(CONFIG_S5PC110) || defined(CONFIG_S5P6442)
2673 if (syscfg & ONENAND_SYS_CFG1_WM)
2674 writel(onenand_if_ctrl_cfg, 0xB0600100);
2677 /* Check manufacturer ID */
2678 if (onenand_check_maf(bram_maf_id))
2681 /* Read manufacturer and device IDs from Register */
2682 maf_id = this->read_word(this->base + ONENAND_REG_MANUFACTURER_ID);
2683 dev_id = this->read_word(this->base + ONENAND_REG_DEVICE_ID);
2684 ver_id = this->read_word(this->base + ONENAND_REG_VERSION_ID);
2685 this->technology = this->read_word(this->base + ONENAND_REG_TECHNOLOGY);
2687 /* Check OneNAND device */
2688 if (maf_id != bram_maf_id || dev_id != bram_dev_id)
2691 /* Flash device information */
2692 mtd->name = onenand_print_device_info(dev_id, ver_id);
2693 this->device_id = dev_id;
2694 this->version_id = ver_id;
2696 /* Check OneNAND features */
2697 onenand_check_features(mtd);
2699 density = onenand_get_density(dev_id);
2700 if (FLEXONENAND(this)) {
2701 this->dies = ONENAND_IS_DDP(this) ? 2 : 1;
2702 /* Maximum possible erase regions */
2703 mtd->numeraseregions = this->dies << 1;
2704 mtd->eraseregions = malloc(sizeof(struct mtd_erase_region_info)
2705 * (this->dies << 1));
2706 if (!mtd->eraseregions)
2711 * For Flex-OneNAND, chipsize represents maximum possible device size.
2712 * mtd->size represents the actual device size.
2714 this->chipsize = (16 << density) << 20;
2716 /* OneNAND page size & block size */
2717 /* The data buffer size is equal to page size */
2719 this->read_word(this->base + ONENAND_REG_DATA_BUFFER_SIZE);
2720 /* We use the full BufferRAM */
2721 if (ONENAND_IS_4KB_PAGE(this))
2722 mtd->writesize <<= 1;
2724 mtd->oobsize = mtd->writesize >> 5;
2725 /* Pagers per block is always 64 in OneNAND */
2726 mtd->erasesize = mtd->writesize << 6;
2728 * Flex-OneNAND SLC area has 64 pages per block.
2729 * Flex-OneNAND MLC area has 128 pages per block.
2730 * Expose MLC erase size to find erase_shift and page_mask.
2732 if (FLEXONENAND(this))
2733 mtd->erasesize <<= 1;
2735 this->erase_shift = ffs(mtd->erasesize) - 1;
2736 this->page_shift = ffs(mtd->writesize) - 1;
2737 this->ppb_shift = (this->erase_shift - this->page_shift);
2738 this->page_mask = (mtd->erasesize / mtd->writesize) - 1;
2739 /* Set density mask. it is used for DDP */
2740 if (ONENAND_IS_DDP(this))
2741 this->density_mask = this->chipsize >> (this->erase_shift + 1);
2742 /* It's real page size */
2743 this->writesize = mtd->writesize;
2745 /* REVIST: Multichip handling */
2747 if (FLEXONENAND(this))
2748 flexonenand_get_size(mtd);
2750 mtd->size = this->chipsize;
2752 mtd->flags = MTD_CAP_NANDFLASH;
2753 mtd->erase = onenand_erase;
2754 mtd->read = onenand_read;
2755 mtd->write = onenand_write;
2756 mtd->read_oob = onenand_read_oob;
2757 mtd->write_oob = onenand_write_oob;
2758 mtd->sync = onenand_sync;
2759 mtd->block_isbad = onenand_block_isbad;
2760 mtd->block_markbad = onenand_block_markbad;
2766 * onenand_scan - [OneNAND Interface] Scan for the OneNAND device
2767 * @param mtd MTD device structure
2768 * @param maxchips Number of chips to scan for
2770 * This fills out all the not initialized function pointers
2771 * with the defaults.
2772 * The flash ID is read and the mtd/chip structures are
2773 * filled with the appropriate values.
2776 int onenand_scan(struct mtd_info *mtd, int maxchips)
2779 struct onenand_chip *this = mtd->priv;
2781 if (!this->read_word)
2782 this->read_word = onenand_readw;
2783 if (!this->write_word)
2784 this->write_word = onenand_writew;
2787 this->command = onenand_command;
2789 this->wait = onenand_wait;
2790 if (!this->bbt_wait)
2791 this->bbt_wait = onenand_bbt_wait;
2792 if (!this->unlock_all)
2793 this->unlock_all = onenand_unlock_all;
2795 if (!this->read_bufferram)
2796 this->read_bufferram = onenand_read_bufferram;
2797 if (!this->write_bufferram)
2798 this->write_bufferram = onenand_write_bufferram;
2800 if (!this->block_markbad)
2801 this->block_markbad = onenand_default_block_markbad;
2802 if (!this->scan_bbt)
2803 this->scan_bbt = onenand_default_bbt;
2805 if (onenand_probe(mtd))
2808 /* Allocate buffers, if necessary */
2809 if (!this->page_buf) {
2810 this->page_buf = kzalloc(mtd->writesize, GFP_KERNEL);
2811 if (!this->page_buf) {
2812 printk(KERN_ERR "onenand_scan(): Can't allocate page_buf\n");
2815 #ifdef CONFIG_MTD_ONENAND_VERIFY_WRITE
2816 this->verify_buf = kzalloc(mtd->writesize, GFP_KERNEL);
2817 if (!this->verify_buf) {
2818 kfree(this->page_buf);
2822 this->options |= ONENAND_PAGEBUF_ALLOC;
2824 if (!this->oob_buf) {
2825 this->oob_buf = kzalloc(mtd->oobsize, GFP_KERNEL);
2826 if (!this->oob_buf) {
2827 printk(KERN_ERR "onenand_scan: Can't allocate oob_buf\n");
2828 if (this->options & ONENAND_PAGEBUF_ALLOC) {
2829 this->options &= ~ONENAND_PAGEBUF_ALLOC;
2830 kfree(this->page_buf);
2834 this->options |= ONENAND_OOBBUF_ALLOC;
2837 this->state = FL_READY;
2840 * Allow subpage writes up to oobsize.
2842 switch (mtd->oobsize) {
2844 this->ecclayout = &onenand_oob_128;
2845 mtd->subpage_sft = 0;
2849 this->ecclayout = &onenand_oob_64;
2850 mtd->subpage_sft = 2;
2854 this->ecclayout = &onenand_oob_32;
2855 mtd->subpage_sft = 1;
2859 printk(KERN_WARNING "No OOB scheme defined for oobsize %d\n",
2861 mtd->subpage_sft = 0;
2862 /* To prevent kernel oops */
2863 this->ecclayout = &onenand_oob_32;
2867 this->subpagesize = mtd->writesize >> mtd->subpage_sft;
2870 * The number of bytes available for a client to place data into
2871 * the out of band area
2873 this->ecclayout->oobavail = 0;
2874 for (i = 0; i < MTD_MAX_OOBFREE_ENTRIES &&
2875 this->ecclayout->oobfree[i].length; i++)
2876 this->ecclayout->oobavail +=
2877 this->ecclayout->oobfree[i].length;
2878 mtd->oobavail = this->ecclayout->oobavail;
2880 mtd->ecclayout = this->ecclayout;
2882 /* Unlock whole block */
2883 this->unlock_all(mtd);
2885 return this->scan_bbt(mtd);
2889 * onenand_release - [OneNAND Interface] Free resources held by the OneNAND device
2890 * @param mtd MTD device structure
2892 void onenand_release(struct mtd_info *mtd)