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 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License version 2 as
14 * published by the Free Software Foundation.
18 #include <linux/mtd/compat.h>
19 #include <linux/mtd/mtd.h>
20 #include <linux/mtd/onenand.h>
23 #include <asm/errno.h>
26 /* It should access 16-bit instead of 8-bit */
27 static inline void *memcpy_16(void *dst, const void *src, unsigned int len)
40 * onenand_oob_64 - oob info for large (2KB) page
42 static struct nand_ecclayout onenand_oob_64 = {
51 {2, 3}, {14, 2}, {18, 3}, {30, 2},
52 {34, 3}, {46, 2}, {50, 3}, {62, 2}
57 * onenand_oob_32 - oob info for middle (1KB) page
59 static struct nand_ecclayout onenand_oob_32 = {
65 .oobfree = { {2, 3}, {14, 2}, {18, 3}, {30, 2} }
68 static const unsigned char ffchars[] = {
69 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
70 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 16 */
71 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
72 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 32 */
73 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
74 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 48 */
75 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
76 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 64 */
80 * onenand_readw - [OneNAND Interface] Read OneNAND register
81 * @param addr address to read
83 * Read OneNAND register
85 static unsigned short onenand_readw(void __iomem * addr)
91 * onenand_writew - [OneNAND Interface] Write OneNAND register with value
92 * @param value value to write
93 * @param addr address to write
95 * Write OneNAND register with value
97 static void onenand_writew(unsigned short value, void __iomem * addr)
103 * onenand_block_address - [DEFAULT] Get block address
104 * @param device the device id
105 * @param block the block
106 * @return translated block address if DDP, otherwise same
108 * Setup Start Address 1 Register (F100h)
110 static int onenand_block_address(struct onenand_chip *this, int block)
112 /* Device Flash Core select, NAND Flash Block Address */
113 if (block & this->density_mask)
114 return ONENAND_DDP_CHIP1 | (block ^ this->density_mask);
120 * onenand_bufferram_address - [DEFAULT] Get bufferram address
121 * @param device the device id
122 * @param block the block
123 * @return set DBS value if DDP, otherwise 0
125 * Setup Start Address 2 Register (F101h) for DDP
127 static int onenand_bufferram_address(struct onenand_chip *this, int block)
129 /* Device BufferRAM Select */
130 if (block & this->density_mask)
131 return ONENAND_DDP_CHIP1;
133 return ONENAND_DDP_CHIP0;
137 * onenand_page_address - [DEFAULT] Get page address
138 * @param page the page address
139 * @param sector the sector address
140 * @return combined page and sector address
142 * Setup Start Address 8 Register (F107h)
144 static int onenand_page_address(int page, int sector)
146 /* Flash Page Address, Flash Sector Address */
149 fpa = page & ONENAND_FPA_MASK;
150 fsa = sector & ONENAND_FSA_MASK;
152 return ((fpa << ONENAND_FPA_SHIFT) | fsa);
156 * onenand_buffer_address - [DEFAULT] Get buffer address
157 * @param dataram1 DataRAM index
158 * @param sectors the sector address
159 * @param count the number of sectors
160 * @return the start buffer value
162 * Setup Start Buffer Register (F200h)
164 static int onenand_buffer_address(int dataram1, int sectors, int count)
168 /* BufferRAM Sector Address */
169 bsa = sectors & ONENAND_BSA_MASK;
172 bsa |= ONENAND_BSA_DATARAM1; /* DataRAM1 */
174 bsa |= ONENAND_BSA_DATARAM0; /* DataRAM0 */
176 /* BufferRAM Sector Count */
177 bsc = count & ONENAND_BSC_MASK;
179 return ((bsa << ONENAND_BSA_SHIFT) | bsc);
183 * onenand_get_density - [DEFAULT] Get OneNAND density
184 * @param dev_id OneNAND device ID
186 * Get OneNAND density from device ID
188 static inline int onenand_get_density(int dev_id)
190 int density = dev_id >> ONENAND_DEVICE_DENSITY_SHIFT;
191 return (density & ONENAND_DEVICE_DENSITY_MASK);
195 * onenand_command - [DEFAULT] Send command to OneNAND device
196 * @param mtd MTD device structure
197 * @param cmd the command to be sent
198 * @param addr offset to read from or write to
199 * @param len number of bytes to read or write
201 * Send command to OneNAND device. This function is used for middle/large page
202 * devices (1KB/2KB Bytes per page)
204 static int onenand_command(struct mtd_info *mtd, int cmd, loff_t addr,
207 struct onenand_chip *this = mtd->priv;
208 int value, readcmd = 0;
210 /* Now we use page size operation */
211 int sectors = 4, count = 4;
213 /* Address translation */
215 case ONENAND_CMD_UNLOCK:
216 case ONENAND_CMD_LOCK:
217 case ONENAND_CMD_LOCK_TIGHT:
218 case ONENAND_CMD_UNLOCK_ALL:
223 case ONENAND_CMD_ERASE:
224 case ONENAND_CMD_BUFFERRAM:
225 block = (int)(addr >> this->erase_shift);
230 block = (int)(addr >> this->erase_shift);
231 page = (int)(addr >> this->page_shift);
232 page &= this->page_mask;
236 /* NOTE: The setting order of the registers is very important! */
237 if (cmd == ONENAND_CMD_BUFFERRAM) {
238 /* Select DataRAM for DDP */
239 value = onenand_bufferram_address(this, block);
240 this->write_word(value,
241 this->base + ONENAND_REG_START_ADDRESS2);
243 /* Switch to the next data buffer */
244 ONENAND_SET_NEXT_BUFFERRAM(this);
250 /* Write 'DFS, FBA' of Flash */
251 value = onenand_block_address(this, block);
252 this->write_word(value,
253 this->base + ONENAND_REG_START_ADDRESS1);
255 /* Write 'DFS, FBA' of Flash */
256 value = onenand_bufferram_address(this, block);
257 this->write_word(value,
258 this->base + ONENAND_REG_START_ADDRESS2);
265 case ONENAND_CMD_READ:
266 case ONENAND_CMD_READOOB:
267 dataram = ONENAND_SET_NEXT_BUFFERRAM(this);
272 dataram = ONENAND_CURRENT_BUFFERRAM(this);
276 /* Write 'FPA, FSA' of Flash */
277 value = onenand_page_address(page, sectors);
278 this->write_word(value,
279 this->base + ONENAND_REG_START_ADDRESS8);
281 /* Write 'BSA, BSC' of DataRAM */
282 value = onenand_buffer_address(dataram, sectors, count);
283 this->write_word(value, this->base + ONENAND_REG_START_BUFFER);
286 /* Interrupt clear */
287 this->write_word(ONENAND_INT_CLEAR, this->base + ONENAND_REG_INTERRUPT);
289 this->write_word(cmd, this->base + ONENAND_REG_COMMAND);
295 * onenand_wait - [DEFAULT] wait until the command is done
296 * @param mtd MTD device structure
297 * @param state state to select the max. timeout value
299 * Wait for command done. This applies to all OneNAND command
300 * Read can take up to 30us, erase up to 2ms and program up to 350us
301 * according to general OneNAND specs
303 static int onenand_wait(struct mtd_info *mtd, int state)
305 struct onenand_chip *this = mtd->priv;
306 unsigned int flags = ONENAND_INT_MASTER;
307 unsigned int interrupt = 0;
308 unsigned int ctrl, ecc;
311 interrupt = this->read_word(this->base + ONENAND_REG_INTERRUPT);
312 if (interrupt & flags)
316 ctrl = this->read_word(this->base + ONENAND_REG_CTRL_STATUS);
318 if (ctrl & ONENAND_CTRL_ERROR) {
319 printk("onenand_wait: controller error = 0x%04x\n", ctrl);
320 if (ctrl & ONENAND_CTRL_LOCK)
321 printk("onenand_wait: it's locked error = 0x%04x\n",
327 if (interrupt & ONENAND_INT_READ) {
328 ecc = this->read_word(this->base + ONENAND_REG_ECC_STATUS);
329 if (ecc & ONENAND_ECC_2BIT_ALL) {
330 MTDDEBUG (MTD_DEBUG_LEVEL0,
331 "onenand_wait: ECC error = 0x%04x\n", ecc);
340 * onenand_bufferram_offset - [DEFAULT] BufferRAM offset
341 * @param mtd MTD data structure
342 * @param area BufferRAM area
343 * @return offset given area
345 * Return BufferRAM offset given area
347 static inline int onenand_bufferram_offset(struct mtd_info *mtd, int area)
349 struct onenand_chip *this = mtd->priv;
351 if (ONENAND_CURRENT_BUFFERRAM(this)) {
352 if (area == ONENAND_DATARAM)
353 return mtd->writesize;
354 if (area == ONENAND_SPARERAM)
362 * onenand_read_bufferram - [OneNAND Interface] Read the bufferram area
363 * @param mtd MTD data structure
364 * @param area BufferRAM area
365 * @param buffer the databuffer to put/get data
366 * @param offset offset to read from or write to
367 * @param count number of bytes to read/write
369 * Read the BufferRAM area
371 static int onenand_read_bufferram(struct mtd_info *mtd, loff_t addr, int area,
372 unsigned char *buffer, int offset,
375 struct onenand_chip *this = mtd->priv;
376 void __iomem *bufferram;
378 bufferram = this->base + area;
379 bufferram += onenand_bufferram_offset(mtd, area);
381 memcpy_16(buffer, bufferram + offset, count);
387 * onenand_sync_read_bufferram - [OneNAND Interface] Read the bufferram area with Sync. Burst mode
388 * @param mtd MTD data structure
389 * @param area BufferRAM area
390 * @param buffer the databuffer to put/get data
391 * @param offset offset to read from or write to
392 * @param count number of bytes to read/write
394 * Read the BufferRAM area with Sync. Burst Mode
396 static int onenand_sync_read_bufferram(struct mtd_info *mtd, loff_t addr, int area,
397 unsigned char *buffer, int offset,
400 struct onenand_chip *this = mtd->priv;
401 void __iomem *bufferram;
403 bufferram = this->base + area;
404 bufferram += onenand_bufferram_offset(mtd, area);
406 this->mmcontrol(mtd, ONENAND_SYS_CFG1_SYNC_READ);
408 memcpy_16(buffer, bufferram + offset, count);
410 this->mmcontrol(mtd, 0);
416 * onenand_write_bufferram - [OneNAND Interface] Write the bufferram area
417 * @param mtd MTD data structure
418 * @param area BufferRAM area
419 * @param buffer the databuffer to put/get data
420 * @param offset offset to read from or write to
421 * @param count number of bytes to read/write
423 * Write the BufferRAM area
425 static int onenand_write_bufferram(struct mtd_info *mtd, loff_t addr, int area,
426 const unsigned char *buffer, int offset,
429 struct onenand_chip *this = mtd->priv;
430 void __iomem *bufferram;
432 bufferram = this->base + area;
433 bufferram += onenand_bufferram_offset(mtd, area);
435 memcpy_16(bufferram + offset, buffer, count);
441 * onenand_get_2x_blockpage - [GENERIC] Get blockpage at 2x program mode
442 * @param mtd MTD data structure
443 * @param addr address to check
444 * @return blockpage address
446 * Get blockpage address at 2x program mode
448 static int onenand_get_2x_blockpage(struct mtd_info *mtd, loff_t addr)
450 struct onenand_chip *this = mtd->priv;
451 int blockpage, block, page;
453 /* Calculate the even block number */
454 block = (int) (addr >> this->erase_shift) & ~1;
455 /* Is it the odd plane? */
456 if (addr & this->writesize)
458 page = (int) (addr >> (this->page_shift + 1)) & this->page_mask;
459 blockpage = (block << 7) | page;
465 * onenand_check_bufferram - [GENERIC] Check BufferRAM information
466 * @param mtd MTD data structure
467 * @param addr address to check
468 * @return 1 if there are valid data, otherwise 0
470 * Check bufferram if there is data we required
472 static int onenand_check_bufferram(struct mtd_info *mtd, loff_t addr)
474 struct onenand_chip *this = mtd->priv;
475 int blockpage, found = 0;
478 #ifdef CONFIG_S3C64XX
482 if (ONENAND_IS_2PLANE(this))
483 blockpage = onenand_get_2x_blockpage(mtd, addr);
485 blockpage = (int) (addr >> this->page_shift);
487 /* Is there valid data? */
488 i = ONENAND_CURRENT_BUFFERRAM(this);
489 if (this->bufferram[i].blockpage == blockpage)
492 /* Check another BufferRAM */
493 i = ONENAND_NEXT_BUFFERRAM(this);
494 if (this->bufferram[i].blockpage == blockpage) {
495 ONENAND_SET_NEXT_BUFFERRAM(this);
500 if (found && ONENAND_IS_DDP(this)) {
501 /* Select DataRAM for DDP */
502 int block = (int) (addr >> this->erase_shift);
503 int value = onenand_bufferram_address(this, block);
504 this->write_word(value, this->base + ONENAND_REG_START_ADDRESS2);
511 * onenand_update_bufferram - [GENERIC] Update BufferRAM information
512 * @param mtd MTD data structure
513 * @param addr address to update
514 * @param valid valid flag
516 * Update BufferRAM information
518 static int onenand_update_bufferram(struct mtd_info *mtd, loff_t addr,
521 struct onenand_chip *this = mtd->priv;
525 if (ONENAND_IS_2PLANE(this))
526 blockpage = onenand_get_2x_blockpage(mtd, addr);
528 blockpage = (int)(addr >> this->page_shift);
530 /* Invalidate another BufferRAM */
531 i = ONENAND_NEXT_BUFFERRAM(this);
532 if (this->bufferram[i].blockpage == blockpage)
533 this->bufferram[i].blockpage = -1;
535 /* Update BufferRAM */
536 i = ONENAND_CURRENT_BUFFERRAM(this);
538 this->bufferram[i].blockpage = blockpage;
540 this->bufferram[i].blockpage = -1;
546 * onenand_invalidate_bufferram - [GENERIC] Invalidate BufferRAM information
547 * @param mtd MTD data structure
548 * @param addr start address to invalidate
549 * @param len length to invalidate
551 * Invalidate BufferRAM information
553 static void onenand_invalidate_bufferram(struct mtd_info *mtd, loff_t addr,
556 struct onenand_chip *this = mtd->priv;
558 loff_t end_addr = addr + len;
560 /* Invalidate BufferRAM */
561 for (i = 0; i < MAX_BUFFERRAM; i++) {
562 loff_t buf_addr = this->bufferram[i].blockpage << this->page_shift;
564 if (buf_addr >= addr && buf_addr < end_addr)
565 this->bufferram[i].blockpage = -1;
570 * onenand_get_device - [GENERIC] Get chip for selected access
571 * @param mtd MTD device structure
572 * @param new_state the state which is requested
574 * Get the device and lock it for exclusive access
576 static void onenand_get_device(struct mtd_info *mtd, int new_state)
582 * onenand_release_device - [GENERIC] release chip
583 * @param mtd MTD device structure
585 * Deselect, release chip lock and wake up anyone waiting on the device
587 static void onenand_release_device(struct mtd_info *mtd)
593 * onenand_transfer_auto_oob - [Internal] oob auto-placement transfer
594 * @param mtd MTD device structure
595 * @param buf destination address
596 * @param column oob offset to read from
597 * @param thislen oob length to read
599 static int onenand_transfer_auto_oob(struct mtd_info *mtd, uint8_t *buf,
600 int column, int thislen)
602 struct onenand_chip *this = mtd->priv;
603 struct nand_oobfree *free;
604 int readcol = column;
605 int readend = column + thislen;
608 uint8_t *oob_buf = this->oob_buf;
610 free = this->ecclayout->oobfree;
611 for (i = 0; i < MTD_MAX_OOBFREE_ENTRIES && free->length; i++, free++) {
612 if (readcol >= lastgap)
613 readcol += free->offset - lastgap;
614 if (readend >= lastgap)
615 readend += free->offset - lastgap;
616 lastgap = free->offset + free->length;
618 this->read_bufferram(mtd, 0, ONENAND_SPARERAM, oob_buf, 0, mtd->oobsize);
619 free = this->ecclayout->oobfree;
620 for (i = 0; i < MTD_MAX_OOBFREE_ENTRIES && free->length; i++, free++) {
621 int free_end = free->offset + free->length;
622 if (free->offset < readend && free_end > readcol) {
623 int st = max_t(int,free->offset,readcol);
624 int ed = min_t(int,free_end,readend);
626 memcpy(buf, oob_buf + st, n);
628 } else if (column == 0)
635 * onenand_read_ops_nolock - [OneNAND Interface] OneNAND read main and/or out-of-band
636 * @param mtd MTD device structure
637 * @param from offset to read from
638 * @param ops oob operation description structure
640 * OneNAND read main and/or out-of-band data
642 static int onenand_read_ops_nolock(struct mtd_info *mtd, loff_t from,
643 struct mtd_oob_ops *ops)
645 struct onenand_chip *this = mtd->priv;
646 struct mtd_ecc_stats stats;
647 size_t len = ops->len;
648 size_t ooblen = ops->ooblen;
649 u_char *buf = ops->datbuf;
650 u_char *oobbuf = ops->oobbuf;
651 int read = 0, column, thislen;
652 int oobread = 0, oobcolumn, thisooblen, oobsize;
653 int ret = 0, boundary = 0;
654 int writesize = this->writesize;
656 MTDDEBUG(MTD_DEBUG_LEVEL3, "onenand_read_ops_nolock: from = 0x%08x, len = %i\n", (unsigned int) from, (int) len);
658 if (ops->mode == MTD_OOB_AUTO)
659 oobsize = this->ecclayout->oobavail;
661 oobsize = mtd->oobsize;
663 oobcolumn = from & (mtd->oobsize - 1);
665 /* Do not allow reads past end of device */
666 if ((from + len) > mtd->size) {
667 printk(KERN_ERR "onenand_read_ops_nolock: Attempt read beyond end of device\n");
673 stats = mtd->ecc_stats;
675 /* Read-while-load method */
677 /* Do first load to bufferRAM */
679 if (!onenand_check_bufferram(mtd, from)) {
680 this->main_buf = buf;
681 this->command(mtd, ONENAND_CMD_READ, from, writesize);
682 ret = this->wait(mtd, FL_READING);
683 onenand_update_bufferram(mtd, from, !ret);
689 thislen = min_t(int, writesize, len - read);
690 column = from & (writesize - 1);
691 if (column + thislen > writesize)
692 thislen = writesize - column;
695 /* If there is more to load then start next load */
697 if (read + thislen < len) {
698 this->main_buf = buf + thislen;
699 this->command(mtd, ONENAND_CMD_READ, from, writesize);
701 * Chip boundary handling in DDP
702 * Now we issued chip 1 read and pointed chip 1
703 * bufferam so we have to point chip 0 bufferam.
705 if (ONENAND_IS_DDP(this) &&
706 unlikely(from == (this->chipsize >> 1))) {
707 this->write_word(ONENAND_DDP_CHIP0, this->base + ONENAND_REG_START_ADDRESS2);
711 ONENAND_SET_PREV_BUFFERRAM(this);
714 /* While load is going, read from last bufferRAM */
715 this->read_bufferram(mtd, from - thislen, ONENAND_DATARAM, buf, column, thislen);
717 /* Read oob area if needed */
719 thisooblen = oobsize - oobcolumn;
720 thisooblen = min_t(int, thisooblen, ooblen - oobread);
722 if (ops->mode == MTD_OOB_AUTO)
723 onenand_transfer_auto_oob(mtd, oobbuf, oobcolumn, thisooblen);
725 this->read_bufferram(mtd, 0, ONENAND_SPARERAM, oobbuf, oobcolumn, thisooblen);
726 oobread += thisooblen;
727 oobbuf += thisooblen;
731 /* See if we are done */
735 /* Set up for next read from bufferRAM */
736 if (unlikely(boundary))
737 this->write_word(ONENAND_DDP_CHIP1, this->base + ONENAND_REG_START_ADDRESS2);
738 ONENAND_SET_NEXT_BUFFERRAM(this);
740 thislen = min_t(int, writesize, len - read);
743 /* Now wait for load */
744 ret = this->wait(mtd, FL_READING);
745 onenand_update_bufferram(mtd, from, !ret);
751 * Return success, if no ECC failures, else -EBADMSG
752 * fs driver will take care of that, because
753 * retlen == desired len and result == -EBADMSG
756 ops->oobretlen = oobread;
761 if (mtd->ecc_stats.failed - stats.failed)
764 return mtd->ecc_stats.corrected - stats.corrected ? -EUCLEAN : 0;
768 * onenand_read_oob_nolock - [MTD Interface] OneNAND read out-of-band
769 * @param mtd MTD device structure
770 * @param from offset to read from
771 * @param ops oob operation description structure
773 * OneNAND read out-of-band data from the spare area
775 static int onenand_read_oob_nolock(struct mtd_info *mtd, loff_t from,
776 struct mtd_oob_ops *ops)
778 struct onenand_chip *this = mtd->priv;
779 struct mtd_ecc_stats stats;
780 int read = 0, thislen, column, oobsize;
781 size_t len = ops->ooblen;
782 mtd_oob_mode_t mode = ops->mode;
783 u_char *buf = ops->oobbuf;
786 from += ops->ooboffs;
788 MTDDEBUG(MTD_DEBUG_LEVEL3, "onenand_read_oob_nolock: from = 0x%08x, len = %i\n", (unsigned int) from, (int) len);
790 /* Initialize return length value */
793 if (mode == MTD_OOB_AUTO)
794 oobsize = this->ecclayout->oobavail;
796 oobsize = mtd->oobsize;
798 column = from & (mtd->oobsize - 1);
800 if (unlikely(column >= oobsize)) {
801 printk(KERN_ERR "onenand_read_oob_nolock: Attempted to start read outside oob\n");
805 /* Do not allow reads past end of device */
806 if (unlikely(from >= mtd->size ||
807 column + len > ((mtd->size >> this->page_shift) -
808 (from >> this->page_shift)) * oobsize)) {
809 printk(KERN_ERR "onenand_read_oob_nolock: Attempted to read beyond end of device\n");
813 stats = mtd->ecc_stats;
816 thislen = oobsize - column;
817 thislen = min_t(int, thislen, len);
819 this->spare_buf = buf;
820 this->command(mtd, ONENAND_CMD_READOOB, from, mtd->oobsize);
822 onenand_update_bufferram(mtd, from, 0);
824 ret = this->wait(mtd, FL_READING);
825 if (ret && ret != -EBADMSG) {
826 printk(KERN_ERR "onenand_read_oob_nolock: read failed = 0x%x\n", ret);
830 if (mode == MTD_OOB_AUTO)
831 onenand_transfer_auto_oob(mtd, buf, column, thislen);
833 this->read_bufferram(mtd, 0, ONENAND_SPARERAM, buf, column, thislen);
845 from += mtd->writesize;
850 ops->oobretlen = read;
855 if (mtd->ecc_stats.failed - stats.failed)
862 * onenand_read - [MTD Interface] MTD compability function for onenand_read_ecc
863 * @param mtd MTD device structure
864 * @param from offset to read from
865 * @param len number of bytes to read
866 * @param retlen pointer to variable to store the number of read bytes
867 * @param buf the databuffer to put data
869 * This function simply calls onenand_read_ecc with oob buffer and oobsel = NULL
871 int onenand_read(struct mtd_info *mtd, loff_t from, size_t len,
872 size_t * retlen, u_char * buf)
874 struct mtd_oob_ops ops = {
882 onenand_get_device(mtd, FL_READING);
883 ret = onenand_read_ops_nolock(mtd, from, &ops);
884 onenand_release_device(mtd);
886 *retlen = ops.retlen;
891 * onenand_read_oob - [MTD Interface] OneNAND read out-of-band
892 * @param mtd MTD device structure
893 * @param from offset to read from
894 * @param ops oob operations description structure
896 * OneNAND main and/or out-of-band
898 int onenand_read_oob(struct mtd_info *mtd, loff_t from,
899 struct mtd_oob_ops *ops)
908 /* Not implemented yet */
913 onenand_get_device(mtd, FL_READING);
915 ret = onenand_read_ops_nolock(mtd, from, ops);
917 ret = onenand_read_oob_nolock(mtd, from, ops);
918 onenand_release_device(mtd);
924 * onenand_bbt_wait - [DEFAULT] wait until the command is done
925 * @param mtd MTD device structure
926 * @param state state to select the max. timeout value
928 * Wait for command done.
930 static int onenand_bbt_wait(struct mtd_info *mtd, int state)
932 struct onenand_chip *this = mtd->priv;
933 unsigned int flags = ONENAND_INT_MASTER;
934 unsigned int interrupt;
938 interrupt = this->read_word(this->base + ONENAND_REG_INTERRUPT);
939 if (interrupt & flags)
943 /* To get correct interrupt status in timeout case */
944 interrupt = this->read_word(this->base + ONENAND_REG_INTERRUPT);
945 ctrl = this->read_word(this->base + ONENAND_REG_CTRL_STATUS);
947 if (interrupt & ONENAND_INT_READ) {
948 int ecc = this->read_word(this->base + ONENAND_REG_ECC_STATUS);
949 if (ecc & ONENAND_ECC_2BIT_ALL)
950 return ONENAND_BBT_READ_ERROR;
952 printk(KERN_ERR "onenand_bbt_wait: read timeout!"
953 "ctrl=0x%04x intr=0x%04x\n", ctrl, interrupt);
954 return ONENAND_BBT_READ_FATAL_ERROR;
957 /* Initial bad block case: 0x2400 or 0x0400 */
958 if (ctrl & ONENAND_CTRL_ERROR) {
959 printk(KERN_DEBUG "onenand_bbt_wait: controller error = 0x%04x\n", ctrl);
960 return ONENAND_BBT_READ_ERROR;
967 * onenand_bbt_read_oob - [MTD Interface] OneNAND read out-of-band for bbt scan
968 * @param mtd MTD device structure
969 * @param from offset to read from
970 * @param ops oob operation description structure
972 * OneNAND read out-of-band data from the spare area for bbt scan
974 int onenand_bbt_read_oob(struct mtd_info *mtd, loff_t from,
975 struct mtd_oob_ops *ops)
977 struct onenand_chip *this = mtd->priv;
978 int read = 0, thislen, column;
980 size_t len = ops->ooblen;
981 u_char *buf = ops->oobbuf;
983 MTDDEBUG(MTD_DEBUG_LEVEL3, "onenand_bbt_read_oob: from = 0x%08x, len = %zi\n", (unsigned int) from, len);
985 /* Initialize return value */
988 /* Do not allow reads past end of device */
989 if (unlikely((from + len) > mtd->size)) {
990 printk(KERN_ERR "onenand_bbt_read_oob: Attempt read beyond end of device\n");
991 return ONENAND_BBT_READ_FATAL_ERROR;
994 /* Grab the lock and see if the device is available */
995 onenand_get_device(mtd, FL_READING);
997 column = from & (mtd->oobsize - 1);
1001 thislen = mtd->oobsize - column;
1002 thislen = min_t(int, thislen, len);
1004 this->spare_buf = buf;
1005 this->command(mtd, ONENAND_CMD_READOOB, from, mtd->oobsize);
1007 onenand_update_bufferram(mtd, from, 0);
1009 ret = this->bbt_wait(mtd, FL_READING);
1013 this->read_bufferram(mtd, 0, ONENAND_SPARERAM, buf, column, thislen);
1022 /* Update Page size */
1023 from += this->writesize;
1028 /* Deselect and wake up anyone waiting on the device */
1029 onenand_release_device(mtd);
1031 ops->oobretlen = read;
1036 #ifdef CONFIG_MTD_ONENAND_VERIFY_WRITE
1038 * onenand_verify_oob - [GENERIC] verify the oob contents after a write
1039 * @param mtd MTD device structure
1040 * @param buf the databuffer to verify
1041 * @param to offset to read from
1043 static int onenand_verify_oob(struct mtd_info *mtd, const u_char *buf, loff_t to)
1045 struct onenand_chip *this = mtd->priv;
1046 u_char *oob_buf = this->oob_buf;
1049 this->command(mtd, ONENAND_CMD_READOOB, to, mtd->oobsize);
1050 onenand_update_bufferram(mtd, to, 0);
1051 status = this->wait(mtd, FL_READING);
1055 this->read_bufferram(mtd, 0, ONENAND_SPARERAM, oob_buf, 0, mtd->oobsize);
1056 for (i = 0; i < mtd->oobsize; i++)
1057 if (buf[i] != 0xFF && buf[i] != oob_buf[i])
1064 * onenand_verify - [GENERIC] verify the chip contents after a write
1065 * @param mtd MTD device structure
1066 * @param buf the databuffer to verify
1067 * @param addr offset to read from
1068 * @param len number of bytes to read and compare
1070 static int onenand_verify(struct mtd_info *mtd, const u_char *buf, loff_t addr, size_t len)
1072 struct onenand_chip *this = mtd->priv;
1073 void __iomem *dataram;
1075 int thislen, column;
1078 thislen = min_t(int, this->writesize, len);
1079 column = addr & (this->writesize - 1);
1080 if (column + thislen > this->writesize)
1081 thislen = this->writesize - column;
1083 this->command(mtd, ONENAND_CMD_READ, addr, this->writesize);
1085 onenand_update_bufferram(mtd, addr, 0);
1087 ret = this->wait(mtd, FL_READING);
1091 onenand_update_bufferram(mtd, addr, 1);
1093 dataram = this->base + ONENAND_DATARAM;
1094 dataram += onenand_bufferram_offset(mtd, ONENAND_DATARAM);
1096 if (memcmp(buf, dataram + column, thislen))
1107 #define onenand_verify(...) (0)
1108 #define onenand_verify_oob(...) (0)
1111 #define NOTALIGNED(x) ((x & (this->subpagesize - 1)) != 0)
1114 * onenand_fill_auto_oob - [Internal] oob auto-placement transfer
1115 * @param mtd MTD device structure
1116 * @param oob_buf oob buffer
1117 * @param buf source address
1118 * @param column oob offset to write to
1119 * @param thislen oob length to write
1121 static int onenand_fill_auto_oob(struct mtd_info *mtd, u_char *oob_buf,
1122 const u_char *buf, int column, int thislen)
1124 struct onenand_chip *this = mtd->priv;
1125 struct nand_oobfree *free;
1126 int writecol = column;
1127 int writeend = column + thislen;
1131 free = this->ecclayout->oobfree;
1132 for (i = 0; i < MTD_MAX_OOBFREE_ENTRIES && free->length; i++, free++) {
1133 if (writecol >= lastgap)
1134 writecol += free->offset - lastgap;
1135 if (writeend >= lastgap)
1136 writeend += free->offset - lastgap;
1137 lastgap = free->offset + free->length;
1139 free = this->ecclayout->oobfree;
1140 for (i = 0; i < MTD_MAX_OOBFREE_ENTRIES && free->length; i++, free++) {
1141 int free_end = free->offset + free->length;
1142 if (free->offset < writeend && free_end > writecol) {
1143 int st = max_t(int,free->offset,writecol);
1144 int ed = min_t(int,free_end,writeend);
1146 memcpy(oob_buf + st, buf, n);
1148 } else if (column == 0)
1155 * onenand_write_ops_nolock - [OneNAND Interface] write main and/or out-of-band
1156 * @param mtd MTD device structure
1157 * @param to offset to write to
1158 * @param ops oob operation description structure
1160 * Write main and/or oob with ECC
1162 static int onenand_write_ops_nolock(struct mtd_info *mtd, loff_t to,
1163 struct mtd_oob_ops *ops)
1165 struct onenand_chip *this = mtd->priv;
1166 int written = 0, column, thislen, subpage;
1167 int oobwritten = 0, oobcolumn, thisooblen, oobsize;
1168 size_t len = ops->len;
1169 size_t ooblen = ops->ooblen;
1170 const u_char *buf = ops->datbuf;
1171 const u_char *oob = ops->oobbuf;
1175 MTDDEBUG(MTD_DEBUG_LEVEL3, "onenand_write_ops_nolock: to = 0x%08x, len = %i\n", (unsigned int) to, (int) len);
1177 /* Initialize retlen, in case of early exit */
1181 /* Do not allow writes past end of device */
1182 if (unlikely((to + len) > mtd->size)) {
1183 printk(KERN_ERR "onenand_write_ops_nolock: Attempt write to past end of device\n");
1187 /* Reject writes, which are not page aligned */
1188 if (unlikely(NOTALIGNED(to) || NOTALIGNED(len))) {
1189 printk(KERN_ERR "onenand_write_ops_nolock: Attempt to write not page aligned data\n");
1193 if (ops->mode == MTD_OOB_AUTO)
1194 oobsize = this->ecclayout->oobavail;
1196 oobsize = mtd->oobsize;
1198 oobcolumn = to & (mtd->oobsize - 1);
1200 column = to & (mtd->writesize - 1);
1202 /* Loop until all data write */
1203 while (written < len) {
1204 u_char *wbuf = (u_char *) buf;
1206 thislen = min_t(int, mtd->writesize - column, len - written);
1207 thisooblen = min_t(int, oobsize - oobcolumn, ooblen - oobwritten);
1209 this->command(mtd, ONENAND_CMD_BUFFERRAM, to, thislen);
1211 /* Partial page write */
1212 subpage = thislen < mtd->writesize;
1214 memset(this->page_buf, 0xff, mtd->writesize);
1215 memcpy(this->page_buf + column, buf, thislen);
1216 wbuf = this->page_buf;
1219 this->write_bufferram(mtd, to, ONENAND_DATARAM, wbuf, 0, mtd->writesize);
1222 oobbuf = this->oob_buf;
1224 /* We send data to spare ram with oobsize
1225 * * to prevent byte access */
1226 memset(oobbuf, 0xff, mtd->oobsize);
1227 if (ops->mode == MTD_OOB_AUTO)
1228 onenand_fill_auto_oob(mtd, oobbuf, oob, oobcolumn, thisooblen);
1230 memcpy(oobbuf + oobcolumn, oob, thisooblen);
1232 oobwritten += thisooblen;
1236 oobbuf = (u_char *) ffchars;
1238 this->write_bufferram(mtd, 0, ONENAND_SPARERAM, oobbuf, 0, mtd->oobsize);
1240 this->command(mtd, ONENAND_CMD_PROG, to, mtd->writesize);
1242 ret = this->wait(mtd, FL_WRITING);
1244 /* In partial page write we don't update bufferram */
1245 onenand_update_bufferram(mtd, to, !ret && !subpage);
1246 if (ONENAND_IS_2PLANE(this)) {
1247 ONENAND_SET_BUFFERRAM1(this);
1248 onenand_update_bufferram(mtd, to + this->writesize, !ret && !subpage);
1252 printk(KERN_ERR "onenand_write_ops_nolock: write filaed %d\n", ret);
1256 /* Only check verify write turn on */
1257 ret = onenand_verify(mtd, buf, to, thislen);
1259 printk(KERN_ERR "onenand_write_ops_nolock: verify failed %d\n", ret);
1273 ops->retlen = written;
1279 * onenand_write_oob_nolock - [Internal] OneNAND write out-of-band
1280 * @param mtd MTD device structure
1281 * @param to offset to write to
1282 * @param len number of bytes to write
1283 * @param retlen pointer to variable to store the number of written bytes
1284 * @param buf the data to write
1285 * @param mode operation mode
1287 * OneNAND write out-of-band
1289 static int onenand_write_oob_nolock(struct mtd_info *mtd, loff_t to,
1290 struct mtd_oob_ops *ops)
1292 struct onenand_chip *this = mtd->priv;
1293 int column, ret = 0, oobsize;
1296 size_t len = ops->ooblen;
1297 const u_char *buf = ops->oobbuf;
1298 mtd_oob_mode_t mode = ops->mode;
1302 MTDDEBUG(MTD_DEBUG_LEVEL3, "onenand_write_oob_nolock: to = 0x%08x, len = %i\n", (unsigned int) to, (int) len);
1304 /* Initialize retlen, in case of early exit */
1307 if (mode == MTD_OOB_AUTO)
1308 oobsize = this->ecclayout->oobavail;
1310 oobsize = mtd->oobsize;
1312 column = to & (mtd->oobsize - 1);
1314 if (unlikely(column >= oobsize)) {
1315 printk(KERN_ERR "onenand_write_oob_nolock: Attempted to start write outside oob\n");
1319 /* For compatibility with NAND: Do not allow write past end of page */
1320 if (unlikely(column + len > oobsize)) {
1321 printk(KERN_ERR "onenand_write_oob_nolock: "
1322 "Attempt to write past end of page\n");
1326 /* Do not allow reads past end of device */
1327 if (unlikely(to >= mtd->size ||
1328 column + len > ((mtd->size >> this->page_shift) -
1329 (to >> this->page_shift)) * oobsize)) {
1330 printk(KERN_ERR "onenand_write_oob_nolock: Attempted to write past end of device\n");
1334 oobbuf = this->oob_buf;
1336 /* Loop until all data write */
1337 while (written < len) {
1338 int thislen = min_t(int, oobsize, len - written);
1340 this->command(mtd, ONENAND_CMD_BUFFERRAM, to, mtd->oobsize);
1342 /* We send data to spare ram with oobsize
1343 * to prevent byte access */
1344 memset(oobbuf, 0xff, mtd->oobsize);
1345 if (mode == MTD_OOB_AUTO)
1346 onenand_fill_auto_oob(mtd, oobbuf, buf, column, thislen);
1348 memcpy(oobbuf + column, buf, thislen);
1349 this->write_bufferram(mtd, 0, ONENAND_SPARERAM, oobbuf, 0, mtd->oobsize);
1351 this->command(mtd, ONENAND_CMD_PROGOOB, to, mtd->oobsize);
1353 onenand_update_bufferram(mtd, to, 0);
1354 if (ONENAND_IS_2PLANE(this)) {
1355 ONENAND_SET_BUFFERRAM1(this);
1356 onenand_update_bufferram(mtd, to + this->writesize, 0);
1359 ret = this->wait(mtd, FL_WRITING);
1361 printk(KERN_ERR "onenand_write_oob_nolock: write failed %d\n", ret);
1365 ret = onenand_verify_oob(mtd, oobbuf, to);
1367 printk(KERN_ERR "onenand_write_oob_nolock: verify failed %d\n", ret);
1375 to += mtd->writesize;
1380 ops->oobretlen = written;
1386 * onenand_write - [MTD Interface] compability function for onenand_write_ecc
1387 * @param mtd MTD device structure
1388 * @param to offset to write to
1389 * @param len number of bytes to write
1390 * @param retlen pointer to variable to store the number of written bytes
1391 * @param buf the data to write
1395 int onenand_write(struct mtd_info *mtd, loff_t to, size_t len,
1396 size_t * retlen, const u_char * buf)
1398 struct mtd_oob_ops ops = {
1401 .datbuf = (u_char *) buf,
1406 onenand_get_device(mtd, FL_WRITING);
1407 ret = onenand_write_ops_nolock(mtd, to, &ops);
1408 onenand_release_device(mtd);
1410 *retlen = ops.retlen;
1415 * onenand_write_oob - [MTD Interface] OneNAND write out-of-band
1416 * @param mtd MTD device structure
1417 * @param to offset to write to
1418 * @param ops oob operation description structure
1420 * OneNAND write main and/or out-of-band
1422 int onenand_write_oob(struct mtd_info *mtd, loff_t to,
1423 struct mtd_oob_ops *ops)
1427 switch (ops->mode) {
1432 /* Not implemented yet */
1437 onenand_get_device(mtd, FL_WRITING);
1439 ret = onenand_write_ops_nolock(mtd, to, ops);
1441 ret = onenand_write_oob_nolock(mtd, to, ops);
1442 onenand_release_device(mtd);
1449 * onenand_block_isbad_nolock - [GENERIC] Check if a block is marked bad
1450 * @param mtd MTD device structure
1451 * @param ofs offset from device start
1452 * @param allowbbt 1, if its allowed to access the bbt area
1454 * Check, if the block is bad, Either by reading the bad block table or
1455 * calling of the scan function.
1457 static int onenand_block_isbad_nolock(struct mtd_info *mtd, loff_t ofs, int allowbbt)
1459 struct onenand_chip *this = mtd->priv;
1460 struct bbm_info *bbm = this->bbm;
1462 /* Return info from the table */
1463 return bbm->isbad_bbt(mtd, ofs, allowbbt);
1468 * onenand_erase - [MTD Interface] erase block(s)
1469 * @param mtd MTD device structure
1470 * @param instr erase instruction
1472 * Erase one ore more blocks
1474 int onenand_erase(struct mtd_info *mtd, struct erase_info *instr)
1476 struct onenand_chip *this = mtd->priv;
1477 unsigned int block_size;
1482 MTDDEBUG (MTD_DEBUG_LEVEL3,
1483 "onenand_erase: start = 0x%08x, len = %i\n",
1484 (unsigned int)instr->addr, (unsigned int)instr->len);
1486 block_size = (1 << this->erase_shift);
1488 /* Start address must align on block boundary */
1489 if (unlikely(instr->addr & (block_size - 1))) {
1490 MTDDEBUG (MTD_DEBUG_LEVEL0,
1491 "onenand_erase: Unaligned address\n");
1495 /* Length must align on block boundary */
1496 if (unlikely(instr->len & (block_size - 1))) {
1497 MTDDEBUG (MTD_DEBUG_LEVEL0,
1498 "onenand_erase: Length not block aligned\n");
1502 /* Do not allow erase past end of device */
1503 if (unlikely((instr->len + instr->addr) > mtd->size)) {
1504 MTDDEBUG (MTD_DEBUG_LEVEL0,
1505 "onenand_erase: Erase past end of device\n");
1509 instr->fail_addr = 0xffffffff;
1511 /* Grab the lock and see if the device is available */
1512 onenand_get_device(mtd, FL_ERASING);
1514 /* Loop throught the pages */
1518 instr->state = MTD_ERASING;
1522 /* Check if we have a bad block, we do not erase bad blocks */
1523 if (instr->priv == 0 && onenand_block_isbad_nolock(mtd, addr, 0)) {
1524 printk(KERN_WARNING "onenand_erase: attempt to erase"
1525 " a bad block at addr 0x%08x\n",
1526 (unsigned int) addr);
1527 instr->state = MTD_ERASE_FAILED;
1531 this->command(mtd, ONENAND_CMD_ERASE, addr, block_size);
1533 onenand_invalidate_bufferram(mtd, addr, block_size);
1535 ret = this->wait(mtd, FL_ERASING);
1536 /* Check, if it is write protected */
1539 MTDDEBUG (MTD_DEBUG_LEVEL0, "onenand_erase: "
1540 "Device is write protected!!!\n");
1542 MTDDEBUG (MTD_DEBUG_LEVEL0, "onenand_erase: "
1543 "Failed erase, block %d\n",
1544 (unsigned)(addr >> this->erase_shift));
1546 printk("onenand_erase: "
1547 "Device is write protected!!!\n");
1549 printk("onenand_erase: "
1550 "Failed erase, block %d\n",
1551 (unsigned)(addr >> this->erase_shift));
1552 instr->state = MTD_ERASE_FAILED;
1553 instr->fail_addr = addr;
1562 instr->state = MTD_ERASE_DONE;
1566 ret = instr->state == MTD_ERASE_DONE ? 0 : -EIO;
1567 /* Do call back function */
1569 mtd_erase_callback(instr);
1571 /* Deselect and wake up anyone waiting on the device */
1572 onenand_release_device(mtd);
1578 * onenand_sync - [MTD Interface] sync
1579 * @param mtd MTD device structure
1581 * Sync is actually a wait for chip ready function
1583 void onenand_sync(struct mtd_info *mtd)
1585 MTDDEBUG (MTD_DEBUG_LEVEL3, "onenand_sync: called\n");
1587 /* Grab the lock and see if the device is available */
1588 onenand_get_device(mtd, FL_SYNCING);
1590 /* Release it and go back */
1591 onenand_release_device(mtd);
1595 * onenand_block_isbad - [MTD Interface] Check whether the block at the given offset is bad
1596 * @param mtd MTD device structure
1597 * @param ofs offset relative to mtd start
1599 * Check whether the block is bad
1601 int onenand_block_isbad(struct mtd_info *mtd, loff_t ofs)
1605 /* Check for invalid offset */
1606 if (ofs > mtd->size)
1609 onenand_get_device(mtd, FL_READING);
1610 ret = onenand_block_isbad_nolock(mtd,ofs, 0);
1611 onenand_release_device(mtd);
1616 * onenand_default_block_markbad - [DEFAULT] mark a block bad
1617 * @param mtd MTD device structure
1618 * @param ofs offset from device start
1620 * This is the default implementation, which can be overridden by
1621 * a hardware specific driver.
1623 static int onenand_default_block_markbad(struct mtd_info *mtd, loff_t ofs)
1625 struct onenand_chip *this = mtd->priv;
1626 struct bbm_info *bbm = this->bbm;
1627 u_char buf[2] = {0, 0};
1628 struct mtd_oob_ops ops = {
1629 .mode = MTD_OOB_PLACE,
1636 /* Get block number */
1637 block = ((int) ofs) >> bbm->bbt_erase_shift;
1639 bbm->bbt[block >> 2] |= 0x01 << ((block & 0x03) << 1);
1641 /* We write two bytes, so we dont have to mess with 16 bit access */
1642 ofs += mtd->oobsize + (bbm->badblockpos & ~0x01);
1643 return onenand_write_oob_nolock(mtd, ofs, &ops);
1647 * onenand_block_markbad - [MTD Interface] Mark the block at the given offset as bad
1648 * @param mtd MTD device structure
1649 * @param ofs offset relative to mtd start
1651 * Mark the block as bad
1653 int onenand_block_markbad(struct mtd_info *mtd, loff_t ofs)
1655 struct onenand_chip *this = mtd->priv;
1658 ret = onenand_block_isbad(mtd, ofs);
1660 /* If it was bad already, return success and do nothing */
1666 ret = this->block_markbad(mtd, ofs);
1671 * onenand_do_lock_cmd - [OneNAND Interface] Lock or unlock block(s)
1672 * @param mtd MTD device structure
1673 * @param ofs offset relative to mtd start
1674 * @param len number of bytes to lock or unlock
1675 * @param cmd lock or unlock command
1677 * Lock or unlock one or more blocks
1679 static int onenand_do_lock_cmd(struct mtd_info *mtd, loff_t ofs, size_t len, int cmd)
1681 struct onenand_chip *this = mtd->priv;
1682 int start, end, block, value, status;
1685 start = ofs >> this->erase_shift;
1686 end = len >> this->erase_shift;
1688 if (cmd == ONENAND_CMD_LOCK)
1689 wp_status_mask = ONENAND_WP_LS;
1691 wp_status_mask = ONENAND_WP_US;
1693 /* Continuous lock scheme */
1694 if (this->options & ONENAND_HAS_CONT_LOCK) {
1695 /* Set start block address */
1696 this->write_word(start,
1697 this->base + ONENAND_REG_START_BLOCK_ADDRESS);
1698 /* Set end block address */
1699 this->write_word(end - 1,
1700 this->base + ONENAND_REG_END_BLOCK_ADDRESS);
1701 /* Write unlock command */
1702 this->command(mtd, cmd, 0, 0);
1704 /* There's no return value */
1705 this->wait(mtd, FL_UNLOCKING);
1708 while (this->read_word(this->base + ONENAND_REG_CTRL_STATUS)
1709 & ONENAND_CTRL_ONGO)
1712 /* Check lock status */
1713 status = this->read_word(this->base + ONENAND_REG_WP_STATUS);
1714 if (!(status & ONENAND_WP_US))
1715 printk(KERN_ERR "wp status = 0x%x\n", status);
1720 /* Block lock scheme */
1721 for (block = start; block < start + end; block++) {
1722 /* Set block address */
1723 value = onenand_block_address(this, block);
1724 this->write_word(value, this->base + ONENAND_REG_START_ADDRESS1);
1725 /* Select DataRAM for DDP */
1726 value = onenand_bufferram_address(this, block);
1727 this->write_word(value, this->base + ONENAND_REG_START_ADDRESS2);
1729 /* Set start block address */
1730 this->write_word(block,
1731 this->base + ONENAND_REG_START_BLOCK_ADDRESS);
1732 /* Write unlock command */
1733 this->command(mtd, ONENAND_CMD_UNLOCK, 0, 0);
1735 /* There's no return value */
1736 this->wait(mtd, FL_UNLOCKING);
1739 while (this->read_word(this->base + ONENAND_REG_CTRL_STATUS)
1740 & ONENAND_CTRL_ONGO)
1743 /* Check lock status */
1744 status = this->read_word(this->base + ONENAND_REG_WP_STATUS);
1745 if (!(status & ONENAND_WP_US))
1746 printk(KERN_ERR "block = %d, wp status = 0x%x\n",
1753 #ifdef ONENAND_LINUX
1755 * onenand_lock - [MTD Interface] Lock block(s)
1756 * @param mtd MTD device structure
1757 * @param ofs offset relative to mtd start
1758 * @param len number of bytes to unlock
1760 * Lock one or more blocks
1762 static int onenand_lock(struct mtd_info *mtd, loff_t ofs, size_t len)
1766 onenand_get_device(mtd, FL_LOCKING);
1767 ret = onenand_do_lock_cmd(mtd, ofs, len, ONENAND_CMD_LOCK);
1768 onenand_release_device(mtd);
1773 * onenand_unlock - [MTD Interface] Unlock block(s)
1774 * @param mtd MTD device structure
1775 * @param ofs offset relative to mtd start
1776 * @param len number of bytes to unlock
1778 * Unlock one or more blocks
1780 static int onenand_unlock(struct mtd_info *mtd, loff_t ofs, size_t len)
1784 onenand_get_device(mtd, FL_LOCKING);
1785 ret = onenand_do_lock_cmd(mtd, ofs, len, ONENAND_CMD_UNLOCK);
1786 onenand_release_device(mtd);
1792 * onenand_check_lock_status - [OneNAND Interface] Check lock status
1793 * @param this onenand chip data structure
1797 static int onenand_check_lock_status(struct onenand_chip *this)
1799 unsigned int value, block, status;
1802 end = this->chipsize >> this->erase_shift;
1803 for (block = 0; block < end; block++) {
1804 /* Set block address */
1805 value = onenand_block_address(this, block);
1806 this->write_word(value, this->base + ONENAND_REG_START_ADDRESS1);
1807 /* Select DataRAM for DDP */
1808 value = onenand_bufferram_address(this, block);
1809 this->write_word(value, this->base + ONENAND_REG_START_ADDRESS2);
1810 /* Set start block address */
1811 this->write_word(block, this->base + ONENAND_REG_START_BLOCK_ADDRESS);
1813 /* Check lock status */
1814 status = this->read_word(this->base + ONENAND_REG_WP_STATUS);
1815 if (!(status & ONENAND_WP_US)) {
1816 printk(KERN_ERR "block = %d, wp status = 0x%x\n", block, status);
1825 * onenand_unlock_all - [OneNAND Interface] unlock all blocks
1826 * @param mtd MTD device structure
1830 static void onenand_unlock_all(struct mtd_info *mtd)
1832 struct onenand_chip *this = mtd->priv;
1834 size_t len = this->chipsize;
1836 if (this->options & ONENAND_HAS_UNLOCK_ALL) {
1837 /* Set start block address */
1838 this->write_word(0, this->base + ONENAND_REG_START_BLOCK_ADDRESS);
1839 /* Write unlock command */
1840 this->command(mtd, ONENAND_CMD_UNLOCK_ALL, 0, 0);
1842 /* There's no return value */
1843 this->wait(mtd, FL_LOCKING);
1846 while (this->read_word(this->base + ONENAND_REG_CTRL_STATUS)
1847 & ONENAND_CTRL_ONGO)
1852 /* Check lock status */
1853 if (onenand_check_lock_status(this))
1856 /* Workaround for all block unlock in DDP */
1857 if (ONENAND_IS_DDP(this)) {
1858 /* All blocks on another chip */
1859 ofs = this->chipsize >> 1;
1860 len = this->chipsize >> 1;
1864 onenand_do_lock_cmd(mtd, ofs, len, ONENAND_CMD_UNLOCK);
1869 * onenand_check_features - Check and set OneNAND features
1870 * @param mtd MTD data structure
1872 * Check and set OneNAND features
1876 static void onenand_check_features(struct mtd_info *mtd)
1878 struct onenand_chip *this = mtd->priv;
1879 unsigned int density, process;
1881 /* Lock scheme depends on density and process */
1882 density = onenand_get_density(this->device_id);
1883 process = this->version_id >> ONENAND_VERSION_PROCESS_SHIFT;
1887 case ONENAND_DEVICE_DENSITY_4Gb:
1888 this->options |= ONENAND_HAS_2PLANE;
1890 case ONENAND_DEVICE_DENSITY_2Gb:
1891 /* 2Gb DDP don't have 2 plane */
1892 if (!ONENAND_IS_DDP(this))
1893 this->options |= ONENAND_HAS_2PLANE;
1894 this->options |= ONENAND_HAS_UNLOCK_ALL;
1896 case ONENAND_DEVICE_DENSITY_1Gb:
1897 /* A-Die has all block unlock */
1899 this->options |= ONENAND_HAS_UNLOCK_ALL;
1903 /* Some OneNAND has continuous lock scheme */
1905 this->options |= ONENAND_HAS_CONT_LOCK;
1909 if (this->options & ONENAND_HAS_CONT_LOCK)
1910 printk(KERN_DEBUG "Lock scheme is Continuous Lock\n");
1911 if (this->options & ONENAND_HAS_UNLOCK_ALL)
1912 printk(KERN_DEBUG "Chip support all block unlock\n");
1913 if (this->options & ONENAND_HAS_2PLANE)
1914 printk(KERN_DEBUG "Chip has 2 plane\n");
1918 * onenand_print_device_info - Print device ID
1919 * @param device device ID
1923 char *onenand_print_device_info(int device, int version)
1925 int vcc, demuxed, ddp, density;
1926 char *dev_info = malloc(80);
1929 vcc = device & ONENAND_DEVICE_VCC_MASK;
1930 demuxed = device & ONENAND_DEVICE_IS_DEMUX;
1931 ddp = device & ONENAND_DEVICE_IS_DDP;
1932 density = device >> ONENAND_DEVICE_DENSITY_SHIFT;
1933 p += sprintf(dev_info, "%sOneNAND%s %dMB %sV 16-bit (0x%02x)",
1934 demuxed ? "" : "Muxed ",
1936 (16 << density), vcc ? "2.65/3.3" : "1.8", device);
1938 sprintf(p, "\nOneNAND version = 0x%04x", version);
1939 printk("%s\n", dev_info);
1944 static const struct onenand_manufacturers onenand_manuf_ids[] = {
1945 {ONENAND_MFR_SAMSUNG, "Samsung"},
1949 * onenand_check_maf - Check manufacturer ID
1950 * @param manuf manufacturer ID
1952 * Check manufacturer ID
1954 static int onenand_check_maf(int manuf)
1956 int size = ARRAY_SIZE(onenand_manuf_ids);
1960 for (i = 0; size; i++)
1961 if (manuf == onenand_manuf_ids[i].id)
1965 name = onenand_manuf_ids[i].name;
1969 #ifdef ONENAND_DEBUG
1970 printk(KERN_DEBUG "OneNAND Manufacturer: %s (0x%0x)\n", name, manuf);
1977 * onenand_probe - [OneNAND Interface] Probe the OneNAND device
1978 * @param mtd MTD device structure
1980 * OneNAND detection method:
1981 * Compare the the values from command with ones from register
1983 static int onenand_probe(struct mtd_info *mtd)
1985 struct onenand_chip *this = mtd->priv;
1986 int bram_maf_id, bram_dev_id, maf_id, dev_id, ver_id;
1990 /* Save system configuration 1 */
1991 syscfg = this->read_word(this->base + ONENAND_REG_SYS_CFG1);
1992 /* Clear Sync. Burst Read mode to read BootRAM */
1993 this->write_word((syscfg & ~ONENAND_SYS_CFG1_SYNC_READ), this->base + ONENAND_REG_SYS_CFG1);
1995 /* Send the command for reading device ID from BootRAM */
1996 this->write_word(ONENAND_CMD_READID, this->base + ONENAND_BOOTRAM);
1998 /* Read manufacturer and device IDs from BootRAM */
1999 bram_maf_id = this->read_word(this->base + ONENAND_BOOTRAM + 0x0);
2000 bram_dev_id = this->read_word(this->base + ONENAND_BOOTRAM + 0x2);
2002 /* Reset OneNAND to read default register values */
2003 this->write_word(ONENAND_CMD_RESET, this->base + ONENAND_BOOTRAM);
2006 this->wait(mtd, FL_RESETING);
2008 /* Restore system configuration 1 */
2009 this->write_word(syscfg, this->base + ONENAND_REG_SYS_CFG1);
2011 /* Check manufacturer ID */
2012 if (onenand_check_maf(bram_maf_id))
2015 /* Read manufacturer and device IDs from Register */
2016 maf_id = this->read_word(this->base + ONENAND_REG_MANUFACTURER_ID);
2017 dev_id = this->read_word(this->base + ONENAND_REG_DEVICE_ID);
2018 ver_id = this->read_word(this->base + ONENAND_REG_VERSION_ID);
2020 /* Check OneNAND device */
2021 if (maf_id != bram_maf_id || dev_id != bram_dev_id)
2024 /* FIXME : Current OneNAND MTD doesn't support Flex-OneNAND */
2025 if (dev_id & (1 << 9)) {
2026 printk("Not yet support Flex-OneNAND\n");
2030 /* Flash device information */
2031 mtd->name = onenand_print_device_info(dev_id, ver_id);
2032 this->device_id = dev_id;
2033 this->version_id = ver_id;
2035 density = onenand_get_density(dev_id);
2036 this->chipsize = (16 << density) << 20;
2037 /* Set density mask. it is used for DDP */
2038 if (ONENAND_IS_DDP(this))
2039 this->density_mask = (1 << (density + 6));
2041 this->density_mask = 0;
2043 /* OneNAND page size & block size */
2044 /* The data buffer size is equal to page size */
2046 this->read_word(this->base + ONENAND_REG_DATA_BUFFER_SIZE);
2047 mtd->oobsize = mtd->writesize >> 5;
2048 /* Pagers per block is always 64 in OneNAND */
2049 mtd->erasesize = mtd->writesize << 6;
2051 this->erase_shift = ffs(mtd->erasesize) - 1;
2052 this->page_shift = ffs(mtd->writesize) - 1;
2053 this->ppb_shift = (this->erase_shift - this->page_shift);
2054 this->page_mask = (mtd->erasesize / mtd->writesize) - 1;
2055 /* It's real page size */
2056 this->writesize = mtd->writesize;
2058 /* REVIST: Multichip handling */
2060 mtd->size = this->chipsize;
2062 /* Check OneNAND features */
2063 onenand_check_features(mtd);
2065 mtd->flags = MTD_CAP_NANDFLASH;
2066 mtd->erase = onenand_erase;
2067 mtd->read = onenand_read;
2068 mtd->write = onenand_write;
2069 mtd->read_oob = onenand_read_oob;
2070 mtd->write_oob = onenand_write_oob;
2071 mtd->sync = onenand_sync;
2072 mtd->block_isbad = onenand_block_isbad;
2073 mtd->block_markbad = onenand_block_markbad;
2079 * onenand_scan - [OneNAND Interface] Scan for the OneNAND device
2080 * @param mtd MTD device structure
2081 * @param maxchips Number of chips to scan for
2083 * This fills out all the not initialized function pointers
2084 * with the defaults.
2085 * The flash ID is read and the mtd/chip structures are
2086 * filled with the appropriate values.
2088 int onenand_scan(struct mtd_info *mtd, int maxchips)
2091 struct onenand_chip *this = mtd->priv;
2093 if (!this->read_word)
2094 this->read_word = onenand_readw;
2095 if (!this->write_word)
2096 this->write_word = onenand_writew;
2099 this->command = onenand_command;
2101 this->wait = onenand_wait;
2102 if (!this->bbt_wait)
2103 this->bbt_wait = onenand_bbt_wait;
2105 if (!this->read_bufferram)
2106 this->read_bufferram = onenand_read_bufferram;
2107 if (!this->write_bufferram)
2108 this->write_bufferram = onenand_write_bufferram;
2110 if (!this->block_markbad)
2111 this->block_markbad = onenand_default_block_markbad;
2112 if (!this->scan_bbt)
2113 this->scan_bbt = onenand_default_bbt;
2115 if (onenand_probe(mtd))
2118 /* Set Sync. Burst Read after probing */
2119 if (this->mmcontrol) {
2120 printk(KERN_INFO "OneNAND Sync. Burst Read support\n");
2121 this->read_bufferram = onenand_sync_read_bufferram;
2124 /* Allocate buffers, if necessary */
2125 if (!this->page_buf) {
2126 this->page_buf = kzalloc(mtd->writesize, GFP_KERNEL);
2127 if (!this->page_buf) {
2128 printk(KERN_ERR "onenand_scan(): Can't allocate page_buf\n");
2131 this->options |= ONENAND_PAGEBUF_ALLOC;
2133 if (!this->oob_buf) {
2134 this->oob_buf = kzalloc(mtd->oobsize, GFP_KERNEL);
2135 if (!this->oob_buf) {
2136 printk(KERN_ERR "onenand_scan: Can't allocate oob_buf\n");
2137 if (this->options & ONENAND_PAGEBUF_ALLOC) {
2138 this->options &= ~ONENAND_PAGEBUF_ALLOC;
2139 kfree(this->page_buf);
2143 this->options |= ONENAND_OOBBUF_ALLOC;
2146 this->state = FL_READY;
2149 * Allow subpage writes up to oobsize.
2151 switch (mtd->oobsize) {
2153 this->ecclayout = &onenand_oob_64;
2154 mtd->subpage_sft = 2;
2158 this->ecclayout = &onenand_oob_32;
2159 mtd->subpage_sft = 1;
2163 printk(KERN_WARNING "No OOB scheme defined for oobsize %d\n",
2165 mtd->subpage_sft = 0;
2166 /* To prevent kernel oops */
2167 this->ecclayout = &onenand_oob_32;
2171 this->subpagesize = mtd->writesize >> mtd->subpage_sft;
2174 * The number of bytes available for a client to place data into
2175 * the out of band area
2177 this->ecclayout->oobavail = 0;
2178 for (i = 0; i < MTD_MAX_OOBFREE_ENTRIES &&
2179 this->ecclayout->oobfree[i].length; i++)
2180 this->ecclayout->oobavail +=
2181 this->ecclayout->oobfree[i].length;
2182 mtd->oobavail = this->ecclayout->oobavail;
2184 mtd->ecclayout = this->ecclayout;
2186 /* Unlock whole block */
2187 onenand_unlock_all(mtd);
2189 return this->scan_bbt(mtd);
2193 * onenand_release - [OneNAND Interface] Free resources held by the OneNAND device
2194 * @param mtd MTD device structure
2196 void onenand_release(struct mtd_info *mtd)