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)
39 static const unsigned char ffchars[] = {
40 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
41 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 16 */
42 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
43 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 32 */
44 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
45 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 48 */
46 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
47 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 64 */
51 * onenand_readw - [OneNAND Interface] Read OneNAND register
52 * @param addr address to read
54 * Read OneNAND register
56 static unsigned short onenand_readw(void __iomem * addr)
62 * onenand_writew - [OneNAND Interface] Write OneNAND register with value
63 * @param value value to write
64 * @param addr address to write
66 * Write OneNAND register with value
68 static void onenand_writew(unsigned short value, void __iomem * addr)
74 * onenand_block_address - [DEFAULT] Get block address
75 * @param device the device id
76 * @param block the block
77 * @return translated block address if DDP, otherwise same
79 * Setup Start Address 1 Register (F100h)
81 static int onenand_block_address(struct onenand_chip *this, int block)
83 /* Device Flash Core select, NAND Flash Block Address */
84 if (block & this->density_mask)
85 return ONENAND_DDP_CHIP1 | (block ^ this->density_mask);
91 * onenand_bufferram_address - [DEFAULT] Get bufferram address
92 * @param device the device id
93 * @param block the block
94 * @return set DBS value if DDP, otherwise 0
96 * Setup Start Address 2 Register (F101h) for DDP
98 static int onenand_bufferram_address(struct onenand_chip *this, int block)
100 /* Device BufferRAM Select */
101 if (block & this->density_mask)
102 return ONENAND_DDP_CHIP1;
104 return ONENAND_DDP_CHIP0;
108 * onenand_page_address - [DEFAULT] Get page address
109 * @param page the page address
110 * @param sector the sector address
111 * @return combined page and sector address
113 * Setup Start Address 8 Register (F107h)
115 static int onenand_page_address(int page, int sector)
117 /* Flash Page Address, Flash Sector Address */
120 fpa = page & ONENAND_FPA_MASK;
121 fsa = sector & ONENAND_FSA_MASK;
123 return ((fpa << ONENAND_FPA_SHIFT) | fsa);
127 * onenand_buffer_address - [DEFAULT] Get buffer address
128 * @param dataram1 DataRAM index
129 * @param sectors the sector address
130 * @param count the number of sectors
131 * @return the start buffer value
133 * Setup Start Buffer Register (F200h)
135 static int onenand_buffer_address(int dataram1, int sectors, int count)
139 /* BufferRAM Sector Address */
140 bsa = sectors & ONENAND_BSA_MASK;
143 bsa |= ONENAND_BSA_DATARAM1; /* DataRAM1 */
145 bsa |= ONENAND_BSA_DATARAM0; /* DataRAM0 */
147 /* BufferRAM Sector Count */
148 bsc = count & ONENAND_BSC_MASK;
150 return ((bsa << ONENAND_BSA_SHIFT) | bsc);
154 * onenand_get_density - [DEFAULT] Get OneNAND density
155 * @param dev_id OneNAND device ID
157 * Get OneNAND density from device ID
159 static inline int onenand_get_density(int dev_id)
161 int density = dev_id >> ONENAND_DEVICE_DENSITY_SHIFT;
162 return (density & ONENAND_DEVICE_DENSITY_MASK);
166 * onenand_command - [DEFAULT] Send command to OneNAND device
167 * @param mtd MTD device structure
168 * @param cmd the command to be sent
169 * @param addr offset to read from or write to
170 * @param len number of bytes to read or write
172 * Send command to OneNAND device. This function is used for middle/large page
173 * devices (1KB/2KB Bytes per page)
175 static int onenand_command(struct mtd_info *mtd, int cmd, loff_t addr,
178 struct onenand_chip *this = mtd->priv;
179 int value, readcmd = 0;
181 /* Now we use page size operation */
182 int sectors = 4, count = 4;
184 /* Address translation */
186 case ONENAND_CMD_UNLOCK:
187 case ONENAND_CMD_LOCK:
188 case ONENAND_CMD_LOCK_TIGHT:
189 case ONENAND_CMD_UNLOCK_ALL:
194 case ONENAND_CMD_ERASE:
195 case ONENAND_CMD_BUFFERRAM:
196 block = (int)(addr >> this->erase_shift);
201 block = (int)(addr >> this->erase_shift);
202 page = (int)(addr >> this->page_shift);
203 page &= this->page_mask;
207 /* NOTE: The setting order of the registers is very important! */
208 if (cmd == ONENAND_CMD_BUFFERRAM) {
209 /* Select DataRAM for DDP */
210 value = onenand_bufferram_address(this, block);
211 this->write_word(value,
212 this->base + ONENAND_REG_START_ADDRESS2);
214 /* Switch to the next data buffer */
215 ONENAND_SET_NEXT_BUFFERRAM(this);
221 /* Write 'DFS, FBA' of Flash */
222 value = onenand_block_address(this, block);
223 this->write_word(value,
224 this->base + ONENAND_REG_START_ADDRESS1);
226 /* Write 'DFS, FBA' of Flash */
227 value = onenand_bufferram_address(this, block);
228 this->write_word(value,
229 this->base + ONENAND_REG_START_ADDRESS2);
236 case ONENAND_CMD_READ:
237 case ONENAND_CMD_READOOB:
238 dataram = ONENAND_SET_NEXT_BUFFERRAM(this);
243 dataram = ONENAND_CURRENT_BUFFERRAM(this);
247 /* Write 'FPA, FSA' of Flash */
248 value = onenand_page_address(page, sectors);
249 this->write_word(value,
250 this->base + ONENAND_REG_START_ADDRESS8);
252 /* Write 'BSA, BSC' of DataRAM */
253 value = onenand_buffer_address(dataram, sectors, count);
254 this->write_word(value, this->base + ONENAND_REG_START_BUFFER);
257 /* Interrupt clear */
258 this->write_word(ONENAND_INT_CLEAR, this->base + ONENAND_REG_INTERRUPT);
260 this->write_word(cmd, this->base + ONENAND_REG_COMMAND);
266 * onenand_wait - [DEFAULT] wait until the command is done
267 * @param mtd MTD device structure
268 * @param state state to select the max. timeout value
270 * Wait for command done. This applies to all OneNAND command
271 * Read can take up to 30us, erase up to 2ms and program up to 350us
272 * according to general OneNAND specs
274 static int onenand_wait(struct mtd_info *mtd, int state)
276 struct onenand_chip *this = mtd->priv;
277 unsigned int flags = ONENAND_INT_MASTER;
278 unsigned int interrupt = 0;
279 unsigned int ctrl, ecc;
282 interrupt = this->read_word(this->base + ONENAND_REG_INTERRUPT);
283 if (interrupt & flags)
287 ctrl = this->read_word(this->base + ONENAND_REG_CTRL_STATUS);
289 if (ctrl & ONENAND_CTRL_ERROR) {
290 printk("onenand_wait: controller error = 0x%04x\n", ctrl);
291 if (ctrl & ONENAND_CTRL_LOCK)
292 printk("onenand_wait: it's locked error = 0x%04x\n",
298 if (interrupt & ONENAND_INT_READ) {
299 ecc = this->read_word(this->base + ONENAND_REG_ECC_STATUS);
300 if (ecc & ONENAND_ECC_2BIT_ALL) {
301 MTDDEBUG (MTD_DEBUG_LEVEL0,
302 "onenand_wait: ECC error = 0x%04x\n", ecc);
311 * onenand_bufferram_offset - [DEFAULT] BufferRAM offset
312 * @param mtd MTD data structure
313 * @param area BufferRAM area
314 * @return offset given area
316 * Return BufferRAM offset given area
318 static inline int onenand_bufferram_offset(struct mtd_info *mtd, int area)
320 struct onenand_chip *this = mtd->priv;
322 if (ONENAND_CURRENT_BUFFERRAM(this)) {
323 if (area == ONENAND_DATARAM)
324 return mtd->writesize;
325 if (area == ONENAND_SPARERAM)
333 * onenand_read_bufferram - [OneNAND Interface] Read the bufferram area
334 * @param mtd MTD data structure
335 * @param area BufferRAM area
336 * @param buffer the databuffer to put/get data
337 * @param offset offset to read from or write to
338 * @param count number of bytes to read/write
340 * Read the BufferRAM area
342 static int onenand_read_bufferram(struct mtd_info *mtd, loff_t addr, int area,
343 unsigned char *buffer, int offset,
346 struct onenand_chip *this = mtd->priv;
347 void __iomem *bufferram;
349 bufferram = this->base + area;
350 bufferram += onenand_bufferram_offset(mtd, area);
352 memcpy_16(buffer, bufferram + offset, count);
358 * onenand_sync_read_bufferram - [OneNAND Interface] Read the bufferram area with Sync. Burst mode
359 * @param mtd MTD data structure
360 * @param area BufferRAM area
361 * @param buffer the databuffer to put/get data
362 * @param offset offset to read from or write to
363 * @param count number of bytes to read/write
365 * Read the BufferRAM area with Sync. Burst Mode
367 static int onenand_sync_read_bufferram(struct mtd_info *mtd, loff_t addr, int area,
368 unsigned char *buffer, int offset,
371 struct onenand_chip *this = mtd->priv;
372 void __iomem *bufferram;
374 bufferram = this->base + area;
375 bufferram += onenand_bufferram_offset(mtd, area);
377 this->mmcontrol(mtd, ONENAND_SYS_CFG1_SYNC_READ);
379 memcpy_16(buffer, bufferram + offset, count);
381 this->mmcontrol(mtd, 0);
387 * onenand_write_bufferram - [OneNAND Interface] Write the bufferram area
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 * Write the BufferRAM area
396 static int onenand_write_bufferram(struct mtd_info *mtd, loff_t addr, int area,
397 const 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 memcpy_16(bufferram + offset, buffer, count);
412 * onenand_get_2x_blockpage - [GENERIC] Get blockpage at 2x program mode
413 * @param mtd MTD data structure
414 * @param addr address to check
415 * @return blockpage address
417 * Get blockpage address at 2x program mode
419 static int onenand_get_2x_blockpage(struct mtd_info *mtd, loff_t addr)
421 struct onenand_chip *this = mtd->priv;
422 int blockpage, block, page;
424 /* Calculate the even block number */
425 block = (int) (addr >> this->erase_shift) & ~1;
426 /* Is it the odd plane? */
427 if (addr & this->writesize)
429 page = (int) (addr >> (this->page_shift + 1)) & this->page_mask;
430 blockpage = (block << 7) | page;
436 * onenand_check_bufferram - [GENERIC] Check BufferRAM information
437 * @param mtd MTD data structure
438 * @param addr address to check
439 * @return 1 if there are valid data, otherwise 0
441 * Check bufferram if there is data we required
443 static int onenand_check_bufferram(struct mtd_info *mtd, loff_t addr)
445 struct onenand_chip *this = mtd->priv;
446 int blockpage, found = 0;
449 #ifdef CONFIG_S3C64XX
453 if (ONENAND_IS_2PLANE(this))
454 blockpage = onenand_get_2x_blockpage(mtd, addr);
456 blockpage = (int) (addr >> this->page_shift);
458 /* Is there valid data? */
459 i = ONENAND_CURRENT_BUFFERRAM(this);
460 if (this->bufferram[i].blockpage == blockpage)
463 /* Check another BufferRAM */
464 i = ONENAND_NEXT_BUFFERRAM(this);
465 if (this->bufferram[i].blockpage == blockpage) {
466 ONENAND_SET_NEXT_BUFFERRAM(this);
471 if (found && ONENAND_IS_DDP(this)) {
472 /* Select DataRAM for DDP */
473 int block = (int) (addr >> this->erase_shift);
474 int value = onenand_bufferram_address(this, block);
475 this->write_word(value, this->base + ONENAND_REG_START_ADDRESS2);
482 * onenand_update_bufferram - [GENERIC] Update BufferRAM information
483 * @param mtd MTD data structure
484 * @param addr address to update
485 * @param valid valid flag
487 * Update BufferRAM information
489 static int onenand_update_bufferram(struct mtd_info *mtd, loff_t addr,
492 struct onenand_chip *this = mtd->priv;
496 if (ONENAND_IS_2PLANE(this))
497 blockpage = onenand_get_2x_blockpage(mtd, addr);
499 blockpage = (int)(addr >> this->page_shift);
501 /* Invalidate another BufferRAM */
502 i = ONENAND_NEXT_BUFFERRAM(this);
503 if (this->bufferram[i].blockpage == blockpage)
504 this->bufferram[i].blockpage = -1;
506 /* Update BufferRAM */
507 i = ONENAND_CURRENT_BUFFERRAM(this);
509 this->bufferram[i].blockpage = blockpage;
511 this->bufferram[i].blockpage = -1;
517 * onenand_invalidate_bufferram - [GENERIC] Invalidate BufferRAM information
518 * @param mtd MTD data structure
519 * @param addr start address to invalidate
520 * @param len length to invalidate
522 * Invalidate BufferRAM information
524 static void onenand_invalidate_bufferram(struct mtd_info *mtd, loff_t addr,
527 struct onenand_chip *this = mtd->priv;
529 loff_t end_addr = addr + len;
531 /* Invalidate BufferRAM */
532 for (i = 0; i < MAX_BUFFERRAM; i++) {
533 loff_t buf_addr = this->bufferram[i].blockpage << this->page_shift;
535 if (buf_addr >= addr && buf_addr < end_addr)
536 this->bufferram[i].blockpage = -1;
541 * onenand_get_device - [GENERIC] Get chip for selected access
542 * @param mtd MTD device structure
543 * @param new_state the state which is requested
545 * Get the device and lock it for exclusive access
547 static void onenand_get_device(struct mtd_info *mtd, int new_state)
553 * onenand_release_device - [GENERIC] release chip
554 * @param mtd MTD device structure
556 * Deselect, release chip lock and wake up anyone waiting on the device
558 static void onenand_release_device(struct mtd_info *mtd)
564 * onenand_transfer_auto_oob - [Internal] oob auto-placement transfer
565 * @param mtd MTD device structure
566 * @param buf destination address
567 * @param column oob offset to read from
568 * @param thislen oob length to read
570 static int onenand_transfer_auto_oob(struct mtd_info *mtd, uint8_t *buf,
571 int column, int thislen)
573 struct onenand_chip *this = mtd->priv;
574 struct nand_oobfree *free;
575 int readcol = column;
576 int readend = column + thislen;
579 uint8_t *oob_buf = this->oob_buf;
581 free = this->ecclayout->oobfree;
582 for (i = 0; i < MTD_MAX_OOBFREE_ENTRIES && free->length; i++, free++) {
583 if (readcol >= lastgap)
584 readcol += free->offset - lastgap;
585 if (readend >= lastgap)
586 readend += free->offset - lastgap;
587 lastgap = free->offset + free->length;
589 this->read_bufferram(mtd, 0, ONENAND_SPARERAM, oob_buf, 0, mtd->oobsize);
590 free = this->ecclayout->oobfree;
591 for (i = 0; i < MTD_MAX_OOBFREE_ENTRIES && free->length; i++, free++) {
592 int free_end = free->offset + free->length;
593 if (free->offset < readend && free_end > readcol) {
594 int st = max_t(int,free->offset,readcol);
595 int ed = min_t(int,free_end,readend);
597 memcpy(buf, oob_buf + st, n);
599 } else if (column == 0)
606 * onenand_read_ops_nolock - [OneNAND Interface] OneNAND read main and/or out-of-band
607 * @param mtd MTD device structure
608 * @param from offset to read from
609 * @param ops oob operation description structure
611 * OneNAND read main and/or out-of-band data
613 static int onenand_read_ops_nolock(struct mtd_info *mtd, loff_t from,
614 struct mtd_oob_ops *ops)
616 struct onenand_chip *this = mtd->priv;
617 struct mtd_ecc_stats stats;
618 size_t len = ops->len;
619 size_t ooblen = ops->ooblen;
620 u_char *buf = ops->datbuf;
621 u_char *oobbuf = ops->oobbuf;
622 int read = 0, column, thislen;
623 int oobread = 0, oobcolumn, thisooblen, oobsize;
624 int ret = 0, boundary = 0;
625 int writesize = this->writesize;
627 MTDDEBUG(MTD_DEBUG_LEVEL3, "onenand_read_ops_nolock: from = 0x%08x, len = %i\n", (unsigned int) from, (int) len);
629 if (ops->mode == MTD_OOB_AUTO)
630 oobsize = this->ecclayout->oobavail;
632 oobsize = mtd->oobsize;
634 oobcolumn = from & (mtd->oobsize - 1);
636 /* Do not allow reads past end of device */
637 if ((from + len) > mtd->size) {
638 printk(KERN_ERR "onenand_read_ops_nolock: Attempt read beyond end of device\n");
644 stats = mtd->ecc_stats;
646 /* Read-while-load method */
648 /* Do first load to bufferRAM */
650 if (!onenand_check_bufferram(mtd, from)) {
651 this->main_buf = buf;
652 this->command(mtd, ONENAND_CMD_READ, from, writesize);
653 ret = this->wait(mtd, FL_READING);
654 onenand_update_bufferram(mtd, from, !ret);
660 thislen = min_t(int, writesize, len - read);
661 column = from & (writesize - 1);
662 if (column + thislen > writesize)
663 thislen = writesize - column;
666 /* If there is more to load then start next load */
668 if (read + thislen < len) {
669 this->main_buf = buf + thislen;
670 this->command(mtd, ONENAND_CMD_READ, from, writesize);
672 * Chip boundary handling in DDP
673 * Now we issued chip 1 read and pointed chip 1
674 * bufferam so we have to point chip 0 bufferam.
676 if (ONENAND_IS_DDP(this) &&
677 unlikely(from == (this->chipsize >> 1))) {
678 this->write_word(ONENAND_DDP_CHIP0, this->base + ONENAND_REG_START_ADDRESS2);
682 ONENAND_SET_PREV_BUFFERRAM(this);
685 /* While load is going, read from last bufferRAM */
686 this->read_bufferram(mtd, from - thislen, ONENAND_DATARAM, buf, column, thislen);
688 /* Read oob area if needed */
690 thisooblen = oobsize - oobcolumn;
691 thisooblen = min_t(int, thisooblen, ooblen - oobread);
693 if (ops->mode == MTD_OOB_AUTO)
694 onenand_transfer_auto_oob(mtd, oobbuf, oobcolumn, thisooblen);
696 this->read_bufferram(mtd, 0, ONENAND_SPARERAM, oobbuf, oobcolumn, thisooblen);
697 oobread += thisooblen;
698 oobbuf += thisooblen;
702 /* See if we are done */
706 /* Set up for next read from bufferRAM */
707 if (unlikely(boundary))
708 this->write_word(ONENAND_DDP_CHIP1, this->base + ONENAND_REG_START_ADDRESS2);
709 ONENAND_SET_NEXT_BUFFERRAM(this);
711 thislen = min_t(int, writesize, len - read);
714 /* Now wait for load */
715 ret = this->wait(mtd, FL_READING);
716 onenand_update_bufferram(mtd, from, !ret);
722 * Return success, if no ECC failures, else -EBADMSG
723 * fs driver will take care of that, because
724 * retlen == desired len and result == -EBADMSG
727 ops->oobretlen = oobread;
732 if (mtd->ecc_stats.failed - stats.failed)
735 return mtd->ecc_stats.corrected - stats.corrected ? -EUCLEAN : 0;
739 * onenand_read_oob_nolock - [MTD Interface] OneNAND read out-of-band
740 * @param mtd MTD device structure
741 * @param from offset to read from
742 * @param ops oob operation description structure
744 * OneNAND read out-of-band data from the spare area
746 static int onenand_read_oob_nolock(struct mtd_info *mtd, loff_t from,
747 struct mtd_oob_ops *ops)
749 struct onenand_chip *this = mtd->priv;
750 struct mtd_ecc_stats stats;
751 int read = 0, thislen, column, oobsize;
752 size_t len = ops->ooblen;
753 mtd_oob_mode_t mode = ops->mode;
754 u_char *buf = ops->oobbuf;
757 from += ops->ooboffs;
759 MTDDEBUG(MTD_DEBUG_LEVEL3, "onenand_read_oob_nolock: from = 0x%08x, len = %i\n", (unsigned int) from, (int) len);
761 /* Initialize return length value */
764 if (mode == MTD_OOB_AUTO)
765 oobsize = this->ecclayout->oobavail;
767 oobsize = mtd->oobsize;
769 column = from & (mtd->oobsize - 1);
771 if (unlikely(column >= oobsize)) {
772 printk(KERN_ERR "onenand_read_oob_nolock: Attempted to start read outside oob\n");
776 /* Do not allow reads past end of device */
777 if (unlikely(from >= mtd->size ||
778 column + len > ((mtd->size >> this->page_shift) -
779 (from >> this->page_shift)) * oobsize)) {
780 printk(KERN_ERR "onenand_read_oob_nolock: Attempted to read beyond end of device\n");
784 stats = mtd->ecc_stats;
787 thislen = oobsize - column;
788 thislen = min_t(int, thislen, len);
790 this->spare_buf = buf;
791 this->command(mtd, ONENAND_CMD_READOOB, from, mtd->oobsize);
793 onenand_update_bufferram(mtd, from, 0);
795 ret = this->wait(mtd, FL_READING);
796 if (ret && ret != -EBADMSG) {
797 printk(KERN_ERR "onenand_read_oob_nolock: read failed = 0x%x\n", ret);
801 if (mode == MTD_OOB_AUTO)
802 onenand_transfer_auto_oob(mtd, buf, column, thislen);
804 this->read_bufferram(mtd, 0, ONENAND_SPARERAM, buf, column, thislen);
816 from += mtd->writesize;
821 ops->oobretlen = read;
826 if (mtd->ecc_stats.failed - stats.failed)
833 * onenand_read - [MTD Interface] MTD compability function for onenand_read_ecc
834 * @param mtd MTD device structure
835 * @param from offset to read from
836 * @param len number of bytes to read
837 * @param retlen pointer to variable to store the number of read bytes
838 * @param buf the databuffer to put data
840 * This function simply calls onenand_read_ecc with oob buffer and oobsel = NULL
842 int onenand_read(struct mtd_info *mtd, loff_t from, size_t len,
843 size_t * retlen, u_char * buf)
845 struct mtd_oob_ops ops = {
853 onenand_get_device(mtd, FL_READING);
854 ret = onenand_read_ops_nolock(mtd, from, &ops);
855 onenand_release_device(mtd);
857 *retlen = ops.retlen;
862 * onenand_read_oob - [MTD Interface] OneNAND read out-of-band
863 * @param mtd MTD device structure
864 * @param from offset to read from
865 * @param ops oob operations description structure
867 * OneNAND main and/or out-of-band
869 int onenand_read_oob(struct mtd_info *mtd, loff_t from,
870 struct mtd_oob_ops *ops)
879 /* Not implemented yet */
884 onenand_get_device(mtd, FL_READING);
886 ret = onenand_read_ops_nolock(mtd, from, ops);
888 ret = onenand_read_oob_nolock(mtd, from, ops);
889 onenand_release_device(mtd);
895 * onenand_bbt_wait - [DEFAULT] wait until the command is done
896 * @param mtd MTD device structure
897 * @param state state to select the max. timeout value
899 * Wait for command done.
901 static int onenand_bbt_wait(struct mtd_info *mtd, int state)
903 struct onenand_chip *this = mtd->priv;
904 unsigned int flags = ONENAND_INT_MASTER;
905 unsigned int interrupt;
909 interrupt = this->read_word(this->base + ONENAND_REG_INTERRUPT);
910 if (interrupt & flags)
914 /* To get correct interrupt status in timeout case */
915 interrupt = this->read_word(this->base + ONENAND_REG_INTERRUPT);
916 ctrl = this->read_word(this->base + ONENAND_REG_CTRL_STATUS);
918 if (interrupt & ONENAND_INT_READ) {
919 int ecc = this->read_word(this->base + ONENAND_REG_ECC_STATUS);
920 if (ecc & ONENAND_ECC_2BIT_ALL)
921 return ONENAND_BBT_READ_ERROR;
923 printk(KERN_ERR "onenand_bbt_wait: read timeout!"
924 "ctrl=0x%04x intr=0x%04x\n", ctrl, interrupt);
925 return ONENAND_BBT_READ_FATAL_ERROR;
928 /* Initial bad block case: 0x2400 or 0x0400 */
929 if (ctrl & ONENAND_CTRL_ERROR) {
930 printk(KERN_DEBUG "onenand_bbt_wait: controller error = 0x%04x\n", ctrl);
931 return ONENAND_BBT_READ_ERROR;
938 * onenand_bbt_read_oob - [MTD Interface] OneNAND read out-of-band for bbt scan
939 * @param mtd MTD device structure
940 * @param from offset to read from
941 * @param ops oob operation description structure
943 * OneNAND read out-of-band data from the spare area for bbt scan
945 int onenand_bbt_read_oob(struct mtd_info *mtd, loff_t from,
946 struct mtd_oob_ops *ops)
948 struct onenand_chip *this = mtd->priv;
949 int read = 0, thislen, column;
951 size_t len = ops->ooblen;
952 u_char *buf = ops->oobbuf;
954 MTDDEBUG(MTD_DEBUG_LEVEL3, "onenand_bbt_read_oob: from = 0x%08x, len = %zi\n", (unsigned int) from, len);
956 /* Initialize return value */
959 /* Do not allow reads past end of device */
960 if (unlikely((from + len) > mtd->size)) {
961 printk(KERN_ERR "onenand_bbt_read_oob: Attempt read beyond end of device\n");
962 return ONENAND_BBT_READ_FATAL_ERROR;
965 /* Grab the lock and see if the device is available */
966 onenand_get_device(mtd, FL_READING);
968 column = from & (mtd->oobsize - 1);
972 thislen = mtd->oobsize - column;
973 thislen = min_t(int, thislen, len);
975 this->spare_buf = buf;
976 this->command(mtd, ONENAND_CMD_READOOB, from, mtd->oobsize);
978 onenand_update_bufferram(mtd, from, 0);
980 ret = this->bbt_wait(mtd, FL_READING);
984 this->read_spareram(mtd, 0, ONENAND_SPARERAM, buf, column, thislen);
993 /* Update Page size */
994 from += this->writesize;
999 /* Deselect and wake up anyone waiting on the device */
1000 onenand_release_device(mtd);
1002 ops->oobretlen = read;
1007 #ifdef CONFIG_MTD_ONENAND_VERIFY_WRITE
1009 * onenand_verify_oob - [GENERIC] verify the oob contents after a write
1010 * @param mtd MTD device structure
1011 * @param buf the databuffer to verify
1012 * @param to offset to read from
1014 static int onenand_verify_oob(struct mtd_info *mtd, const u_char *buf, loff_t to)
1016 struct onenand_chip *this = mtd->priv;
1017 u_char *oob_buf = this->oob_buf;
1020 this->command(mtd, ONENAND_CMD_READOOB, to, mtd->oobsize);
1021 onenand_update_bufferram(mtd, to, 0);
1022 status = this->wait(mtd, FL_READING);
1026 this->read_bufferram(mtd, 0, ONENAND_SPARERAM, oob_buf, 0, mtd->oobsize);
1027 for (i = 0; i < mtd->oobsize; i++)
1028 if (buf[i] != 0xFF && buf[i] != oob_buf[i])
1035 * onenand_verify - [GENERIC] verify the chip contents after a write
1036 * @param mtd MTD device structure
1037 * @param buf the databuffer to verify
1038 * @param addr offset to read from
1039 * @param len number of bytes to read and compare
1041 static int onenand_verify(struct mtd_info *mtd, const u_char *buf, loff_t addr, size_t len)
1043 struct onenand_chip *this = mtd->priv;
1044 void __iomem *dataram;
1046 int thislen, column;
1049 thislen = min_t(int, this->writesize, len);
1050 column = addr & (this->writesize - 1);
1051 if (column + thislen > this->writesize)
1052 thislen = this->writesize - column;
1054 this->command(mtd, ONENAND_CMD_READ, addr, this->writesize);
1056 onenand_update_bufferram(mtd, addr, 0);
1058 ret = this->wait(mtd, FL_READING);
1062 onenand_update_bufferram(mtd, addr, 1);
1064 dataram = this->base + ONENAND_DATARAM;
1065 dataram += onenand_bufferram_offset(mtd, ONENAND_DATARAM);
1067 if (memcmp(buf, dataram + column, thislen))
1078 #define onenand_verify(...) (0)
1079 #define onenand_verify_oob(...) (0)
1082 #define NOTALIGNED(x) ((x & (mtd->writesize - 1)) != 0)
1085 * onenand_fill_auto_oob - [Internal] oob auto-placement transfer
1086 * @param mtd MTD device structure
1087 * @param oob_buf oob buffer
1088 * @param buf source address
1089 * @param column oob offset to write to
1090 * @param thislen oob length to write
1092 static int onenand_fill_auto_oob(struct mtd_info *mtd, u_char *oob_buf,
1093 const u_char *buf, int column, int thislen)
1095 struct onenand_chip *this = mtd->priv;
1096 struct nand_oobfree *free;
1097 int writecol = column;
1098 int writeend = column + thislen;
1102 free = this->ecclayout->oobfree;
1103 for (i = 0; i < MTD_MAX_OOBFREE_ENTRIES && free->length; i++, free++) {
1104 if (writecol >= lastgap)
1105 writecol += free->offset - lastgap;
1106 if (writeend >= lastgap)
1107 writeend += free->offset - lastgap;
1108 lastgap = free->offset + free->length;
1110 free = this->ecclayout->oobfree;
1111 for (i = 0; i < MTD_MAX_OOBFREE_ENTRIES && free->length; i++, free++) {
1112 int free_end = free->offset + free->length;
1113 if (free->offset < writeend && free_end > writecol) {
1114 int st = max_t(int,free->offset,writecol);
1115 int ed = min_t(int,free_end,writeend);
1117 memcpy(oob_buf + st, buf, n);
1119 } else if (column == 0)
1126 * onenand_write_ops_nolock - [OneNAND Interface] write main and/or out-of-band
1127 * @param mtd MTD device structure
1128 * @param to offset to write to
1129 * @param ops oob operation description structure
1131 * Write main and/or oob with ECC
1133 static int onenand_write_ops_nolock(struct mtd_info *mtd, loff_t to,
1134 struct mtd_oob_ops *ops)
1136 struct onenand_chip *this = mtd->priv;
1137 int written = 0, column, thislen, subpage;
1138 int oobwritten = 0, oobcolumn, thisooblen, oobsize;
1139 size_t len = ops->len;
1140 size_t ooblen = ops->ooblen;
1141 const u_char *buf = ops->datbuf;
1142 const u_char *oob = ops->oobbuf;
1146 MTDDEBUG(MTD_DEBUG_LEVEL3, "onenand_write_ops_nolock: to = 0x%08x, len = %i\n", (unsigned int) to, (int) len);
1148 /* Initialize retlen, in case of early exit */
1152 /* Do not allow writes past end of device */
1153 if (unlikely((to + len) > mtd->size)) {
1154 printk(KERN_ERR "onenand_write_ops_nolock: Attempt write to past end of device\n");
1158 /* Reject writes, which are not page aligned */
1159 if (unlikely(NOTALIGNED(to) || NOTALIGNED(len))) {
1160 printk(KERN_ERR "onenand_write_ops_nolock: Attempt to write not page aligned data\n");
1164 if (ops->mode == MTD_OOB_AUTO)
1165 oobsize = this->ecclayout->oobavail;
1167 oobsize = mtd->oobsize;
1169 oobcolumn = to & (mtd->oobsize - 1);
1171 column = to & (mtd->writesize - 1);
1173 /* Loop until all data write */
1174 while (written < len) {
1175 u_char *wbuf = (u_char *) buf;
1177 thislen = min_t(int, mtd->writesize - column, len - written);
1178 thisooblen = min_t(int, oobsize - oobcolumn, ooblen - oobwritten);
1180 this->command(mtd, ONENAND_CMD_BUFFERRAM, to, thislen);
1182 /* Partial page write */
1183 subpage = thislen < mtd->writesize;
1185 memset(this->page_buf, 0xff, mtd->writesize);
1186 memcpy(this->page_buf + column, buf, thislen);
1187 wbuf = this->page_buf;
1190 this->write_bufferram(mtd, to, ONENAND_DATARAM, wbuf, 0, mtd->writesize);
1193 oobbuf = this->oob_buf;
1195 /* We send data to spare ram with oobsize
1196 * * to prevent byte access */
1197 memset(oobbuf, 0xff, mtd->oobsize);
1198 if (ops->mode == MTD_OOB_AUTO)
1199 onenand_fill_auto_oob(mtd, oobbuf, oob, oobcolumn, thisooblen);
1201 memcpy(oobbuf + oobcolumn, oob, thisooblen);
1203 oobwritten += thisooblen;
1207 oobbuf = (u_char *) ffchars;
1209 this->write_bufferram(mtd, 0, ONENAND_SPARERAM, oobbuf, 0, mtd->oobsize);
1211 this->command(mtd, ONENAND_CMD_PROG, to, mtd->writesize);
1213 ret = this->wait(mtd, FL_WRITING);
1215 /* In partial page write we don't update bufferram */
1216 onenand_update_bufferram(mtd, to, !ret && !subpage);
1217 if (ONENAND_IS_2PLANE(this)) {
1218 ONENAND_SET_BUFFERRAM1(this);
1219 onenand_update_bufferram(mtd, to + this->writesize, !ret && !subpage);
1223 printk(KERN_ERR "onenand_write_ops_nolock: write filaed %d\n", ret);
1227 /* Only check verify write turn on */
1228 ret = onenand_verify(mtd, buf, to, thislen);
1230 printk(KERN_ERR "onenand_write_ops_nolock: verify failed %d\n", ret);
1244 ops->retlen = written;
1250 * onenand_write_oob_nolock - [Internal] OneNAND write out-of-band
1251 * @param mtd MTD device structure
1252 * @param to offset to write to
1253 * @param len number of bytes to write
1254 * @param retlen pointer to variable to store the number of written bytes
1255 * @param buf the data to write
1256 * @param mode operation mode
1258 * OneNAND write out-of-band
1260 static int onenand_write_oob_nolock(struct mtd_info *mtd, loff_t to,
1261 struct mtd_oob_ops *ops)
1263 struct onenand_chip *this = mtd->priv;
1264 int column, ret = 0, oobsize;
1267 size_t len = ops->ooblen;
1268 const u_char *buf = ops->oobbuf;
1269 mtd_oob_mode_t mode = ops->mode;
1273 MTDDEBUG(MTD_DEBUG_LEVEL3, "onenand_write_oob_nolock: to = 0x%08x, len = %i\n", (unsigned int) to, (int) len);
1275 /* Initialize retlen, in case of early exit */
1278 if (mode == MTD_OOB_AUTO)
1279 oobsize = this->ecclayout->oobavail;
1281 oobsize = mtd->oobsize;
1283 column = to & (mtd->oobsize - 1);
1285 if (unlikely(column >= oobsize)) {
1286 printk(KERN_ERR "onenand_write_oob_nolock: Attempted to start write outside oob\n");
1290 /* For compatibility with NAND: Do not allow write past end of page */
1291 if (unlikely(column + len > oobsize)) {
1292 printk(KERN_ERR "onenand_write_oob_nolock: "
1293 "Attempt to write past end of page\n");
1297 /* Do not allow reads past end of device */
1298 if (unlikely(to >= mtd->size ||
1299 column + len > ((mtd->size >> this->page_shift) -
1300 (to >> this->page_shift)) * oobsize)) {
1301 printk(KERN_ERR "onenand_write_oob_nolock: Attempted to write past end of device\n");
1305 oobbuf = this->oob_buf;
1307 /* Loop until all data write */
1308 while (written < len) {
1309 int thislen = min_t(int, oobsize, len - written);
1311 this->command(mtd, ONENAND_CMD_BUFFERRAM, to, mtd->oobsize);
1313 /* We send data to spare ram with oobsize
1314 * to prevent byte access */
1315 memset(oobbuf, 0xff, mtd->oobsize);
1316 if (mode == MTD_OOB_AUTO)
1317 onenand_fill_auto_oob(mtd, oobbuf, buf, column, thislen);
1319 memcpy(oobbuf + column, buf, thislen);
1320 this->write_bufferram(mtd, 0, ONENAND_SPARERAM, oobbuf, 0, mtd->oobsize);
1322 this->command(mtd, ONENAND_CMD_PROGOOB, to, mtd->oobsize);
1324 onenand_update_bufferram(mtd, to, 0);
1325 if (ONENAND_IS_2PLANE(this)) {
1326 ONENAND_SET_BUFFERRAM1(this);
1327 onenand_update_bufferram(mtd, to + this->writesize, 0);
1330 ret = this->wait(mtd, FL_WRITING);
1332 printk(KERN_ERR "onenand_write_oob_nolock: write failed %d\n", ret);
1336 ret = onenand_verify_oob(mtd, oobbuf, to);
1338 printk(KERN_ERR "onenand_write_oob_nolock: verify failed %d\n", ret);
1346 to += mtd->writesize;
1351 ops->oobretlen = written;
1357 * onenand_write - [MTD Interface] compability function for onenand_write_ecc
1358 * @param mtd MTD device structure
1359 * @param to offset to write to
1360 * @param len number of bytes to write
1361 * @param retlen pointer to variable to store the number of written bytes
1362 * @param buf the data to write
1366 int onenand_write(struct mtd_info *mtd, loff_t to, size_t len,
1367 size_t * retlen, const u_char * buf)
1369 struct mtd_oob_ops ops = {
1372 .datbuf = (u_char *) buf,
1377 onenand_get_device(mtd, FL_WRITING);
1378 ret = onenand_write_ops_nolock(mtd, to, &ops);
1379 onenand_release_device(mtd);
1381 *retlen = ops.retlen;
1386 * onenand_write_oob - [MTD Interface] OneNAND write out-of-band
1387 * @param mtd MTD device structure
1388 * @param to offset to write to
1389 * @param ops oob operation description structure
1391 * OneNAND write main and/or out-of-band
1393 int onenand_write_oob(struct mtd_info *mtd, loff_t to,
1394 struct mtd_oob_ops *ops)
1398 switch (ops->mode) {
1403 /* Not implemented yet */
1408 onenand_get_device(mtd, FL_WRITING);
1410 ret = onenand_write_ops_nolock(mtd, to, ops);
1412 ret = onenand_write_oob_nolock(mtd, to, ops);
1413 onenand_release_device(mtd);
1420 * onenand_block_isbad_nolock - [GENERIC] Check if a block is marked bad
1421 * @param mtd MTD device structure
1422 * @param ofs offset from device start
1423 * @param allowbbt 1, if its allowed to access the bbt area
1425 * Check, if the block is bad, Either by reading the bad block table or
1426 * calling of the scan function.
1428 static int onenand_block_isbad_nolock(struct mtd_info *mtd, loff_t ofs, int allowbbt)
1430 struct onenand_chip *this = mtd->priv;
1431 struct bbm_info *bbm = this->bbm;
1433 /* Return info from the table */
1434 return bbm->isbad_bbt(mtd, ofs, allowbbt);
1439 * onenand_erase - [MTD Interface] erase block(s)
1440 * @param mtd MTD device structure
1441 * @param instr erase instruction
1443 * Erase one ore more blocks
1445 int onenand_erase(struct mtd_info *mtd, struct erase_info *instr)
1447 struct onenand_chip *this = mtd->priv;
1448 unsigned int block_size;
1453 MTDDEBUG (MTD_DEBUG_LEVEL3,
1454 "onenand_erase: start = 0x%08x, len = %i\n",
1455 (unsigned int)instr->addr, (unsigned int)instr->len);
1457 block_size = (1 << this->erase_shift);
1459 /* Start address must align on block boundary */
1460 if (unlikely(instr->addr & (block_size - 1))) {
1461 MTDDEBUG (MTD_DEBUG_LEVEL0,
1462 "onenand_erase: Unaligned address\n");
1466 /* Length must align on block boundary */
1467 if (unlikely(instr->len & (block_size - 1))) {
1468 MTDDEBUG (MTD_DEBUG_LEVEL0,
1469 "onenand_erase: Length not block aligned\n");
1473 /* Do not allow erase past end of device */
1474 if (unlikely((instr->len + instr->addr) > mtd->size)) {
1475 MTDDEBUG (MTD_DEBUG_LEVEL0,
1476 "onenand_erase: Erase past end of device\n");
1480 instr->fail_addr = 0xffffffff;
1482 /* Grab the lock and see if the device is available */
1483 onenand_get_device(mtd, FL_ERASING);
1485 /* Loop throught the pages */
1489 instr->state = MTD_ERASING;
1493 /* Check if we have a bad block, we do not erase bad blocks */
1494 if (instr->priv == 0 && onenand_block_isbad_nolock(mtd, addr, 0)) {
1495 printk(KERN_WARNING "onenand_erase: attempt to erase"
1496 " a bad block at addr 0x%08x\n",
1497 (unsigned int) addr);
1498 instr->state = MTD_ERASE_FAILED;
1502 this->command(mtd, ONENAND_CMD_ERASE, addr, block_size);
1504 onenand_invalidate_bufferram(mtd, addr, block_size);
1506 ret = this->wait(mtd, FL_ERASING);
1507 /* Check, if it is write protected */
1510 MTDDEBUG (MTD_DEBUG_LEVEL0, "onenand_erase: "
1511 "Device is write protected!!!\n");
1513 MTDDEBUG (MTD_DEBUG_LEVEL0, "onenand_erase: "
1514 "Failed erase, block %d\n",
1515 (unsigned)(addr >> this->erase_shift));
1517 printk("onenand_erase: "
1518 "Device is write protected!!!\n");
1520 printk("onenand_erase: "
1521 "Failed erase, block %d\n",
1522 (unsigned)(addr >> this->erase_shift));
1523 instr->state = MTD_ERASE_FAILED;
1524 instr->fail_addr = addr;
1533 instr->state = MTD_ERASE_DONE;
1537 ret = instr->state == MTD_ERASE_DONE ? 0 : -EIO;
1538 /* Do call back function */
1540 mtd_erase_callback(instr);
1542 /* Deselect and wake up anyone waiting on the device */
1543 onenand_release_device(mtd);
1549 * onenand_sync - [MTD Interface] sync
1550 * @param mtd MTD device structure
1552 * Sync is actually a wait for chip ready function
1554 void onenand_sync(struct mtd_info *mtd)
1556 MTDDEBUG (MTD_DEBUG_LEVEL3, "onenand_sync: called\n");
1558 /* Grab the lock and see if the device is available */
1559 onenand_get_device(mtd, FL_SYNCING);
1561 /* Release it and go back */
1562 onenand_release_device(mtd);
1566 * onenand_block_isbad - [MTD Interface] Check whether the block at the given offset is bad
1567 * @param mtd MTD device structure
1568 * @param ofs offset relative to mtd start
1570 * Check whether the block is bad
1572 int onenand_block_isbad(struct mtd_info *mtd, loff_t ofs)
1576 /* Check for invalid offset */
1577 if (ofs > mtd->size)
1580 onenand_get_device(mtd, FL_READING);
1581 ret = onenand_block_isbad_nolock(mtd,ofs, 0);
1582 onenand_release_device(mtd);
1587 * onenand_block_markbad - [MTD Interface] Mark the block at the given offset as bad
1588 * @param mtd MTD device structure
1589 * @param ofs offset relative to mtd start
1591 * Mark the block as bad
1593 int onenand_block_markbad(struct mtd_info *mtd, loff_t ofs)
1595 struct onenand_chip *this = mtd->priv;
1598 ret = onenand_block_isbad(mtd, ofs);
1600 /* If it was bad already, return success and do nothing */
1606 ret = this->block_markbad(mtd, ofs);
1611 * onenand_do_lock_cmd - [OneNAND Interface] Lock or unlock block(s)
1612 * @param mtd MTD device structure
1613 * @param ofs offset relative to mtd start
1614 * @param len number of bytes to lock or unlock
1615 * @param cmd lock or unlock command
1617 * Lock or unlock one or more blocks
1619 static int onenand_do_lock_cmd(struct mtd_info *mtd, loff_t ofs, size_t len, int cmd)
1621 struct onenand_chip *this = mtd->priv;
1622 int start, end, block, value, status;
1625 start = ofs >> this->erase_shift;
1626 end = len >> this->erase_shift;
1628 if (cmd == ONENAND_CMD_LOCK)
1629 wp_status_mask = ONENAND_WP_LS;
1631 wp_status_mask = ONENAND_WP_US;
1633 /* Continuous lock scheme */
1634 if (this->options & ONENAND_HAS_CONT_LOCK) {
1635 /* Set start block address */
1636 this->write_word(start,
1637 this->base + ONENAND_REG_START_BLOCK_ADDRESS);
1638 /* Set end block address */
1639 this->write_word(end - 1,
1640 this->base + ONENAND_REG_END_BLOCK_ADDRESS);
1641 /* Write unlock command */
1642 this->command(mtd, cmd, 0, 0);
1644 /* There's no return value */
1645 this->wait(mtd, FL_UNLOCKING);
1648 while (this->read_word(this->base + ONENAND_REG_CTRL_STATUS)
1649 & ONENAND_CTRL_ONGO)
1652 /* Check lock status */
1653 status = this->read_word(this->base + ONENAND_REG_WP_STATUS);
1654 if (!(status & ONENAND_WP_US))
1655 printk(KERN_ERR "wp status = 0x%x\n", status);
1660 /* Block lock scheme */
1661 for (block = start; block < start + end; block++) {
1662 /* Set block address */
1663 value = onenand_block_address(this, block);
1664 this->write_word(value, this->base + ONENAND_REG_START_ADDRESS1);
1665 /* Select DataRAM for DDP */
1666 value = onenand_bufferram_address(this, block);
1667 this->write_word(value, this->base + ONENAND_REG_START_ADDRESS2);
1669 /* Set start block address */
1670 this->write_word(block,
1671 this->base + ONENAND_REG_START_BLOCK_ADDRESS);
1672 /* Write unlock command */
1673 this->command(mtd, ONENAND_CMD_UNLOCK, 0, 0);
1675 /* There's no return value */
1676 this->wait(mtd, FL_UNLOCKING);
1679 while (this->read_word(this->base + ONENAND_REG_CTRL_STATUS)
1680 & ONENAND_CTRL_ONGO)
1683 /* Check lock status */
1684 status = this->read_word(this->base + ONENAND_REG_WP_STATUS);
1685 if (!(status & ONENAND_WP_US))
1686 printk(KERN_ERR "block = %d, wp status = 0x%x\n",
1693 #ifdef ONENAND_LINUX
1695 * onenand_lock - [MTD Interface] Lock block(s)
1696 * @param mtd MTD device structure
1697 * @param ofs offset relative to mtd start
1698 * @param len number of bytes to unlock
1700 * Lock one or more blocks
1702 static int onenand_lock(struct mtd_info *mtd, loff_t ofs, size_t len)
1706 onenand_get_device(mtd, FL_LOCKING);
1707 ret = onenand_do_lock_cmd(mtd, ofs, len, ONENAND_CMD_LOCK);
1708 onenand_release_device(mtd);
1713 * onenand_unlock - [MTD Interface] Unlock block(s)
1714 * @param mtd MTD device structure
1715 * @param ofs offset relative to mtd start
1716 * @param len number of bytes to unlock
1718 * Unlock one or more blocks
1720 static int onenand_unlock(struct mtd_info *mtd, loff_t ofs, size_t len)
1724 onenand_get_device(mtd, FL_LOCKING);
1725 ret = onenand_do_lock_cmd(mtd, ofs, len, ONENAND_CMD_UNLOCK);
1726 onenand_release_device(mtd);
1732 * onenand_check_lock_status - [OneNAND Interface] Check lock status
1733 * @param this onenand chip data structure
1737 static int onenand_check_lock_status(struct onenand_chip *this)
1739 unsigned int value, block, status;
1742 end = this->chipsize >> this->erase_shift;
1743 for (block = 0; block < end; block++) {
1744 /* Set block address */
1745 value = onenand_block_address(this, block);
1746 this->write_word(value, this->base + ONENAND_REG_START_ADDRESS1);
1747 /* Select DataRAM for DDP */
1748 value = onenand_bufferram_address(this, block);
1749 this->write_word(value, this->base + ONENAND_REG_START_ADDRESS2);
1750 /* Set start block address */
1751 this->write_word(block, this->base + ONENAND_REG_START_BLOCK_ADDRESS);
1753 /* Check lock status */
1754 status = this->read_word(this->base + ONENAND_REG_WP_STATUS);
1755 if (!(status & ONENAND_WP_US)) {
1756 printk(KERN_ERR "block = %d, wp status = 0x%x\n", block, status);
1765 * onenand_unlock_all - [OneNAND Interface] unlock all blocks
1766 * @param mtd MTD device structure
1770 static void onenand_unlock_all(struct mtd_info *mtd)
1772 struct onenand_chip *this = mtd->priv;
1774 size_t len = this->chipsize;
1776 if (this->options & ONENAND_HAS_UNLOCK_ALL) {
1777 /* Set start block address */
1778 this->write_word(0, this->base + ONENAND_REG_START_BLOCK_ADDRESS);
1779 /* Write unlock command */
1780 this->command(mtd, ONENAND_CMD_UNLOCK_ALL, 0, 0);
1782 /* There's no return value */
1783 this->wait(mtd, FL_LOCKING);
1786 while (this->read_word(this->base + ONENAND_REG_CTRL_STATUS)
1787 & ONENAND_CTRL_ONGO)
1792 /* Check lock status */
1793 if (onenand_check_lock_status(this))
1796 /* Workaround for all block unlock in DDP */
1797 if (ONENAND_IS_DDP(this)) {
1798 /* All blocks on another chip */
1799 ofs = this->chipsize >> 1;
1800 len = this->chipsize >> 1;
1804 onenand_do_lock_cmd(mtd, ofs, len, ONENAND_CMD_UNLOCK);
1809 * onenand_check_features - Check and set OneNAND features
1810 * @param mtd MTD data structure
1812 * Check and set OneNAND features
1816 static void onenand_check_features(struct mtd_info *mtd)
1818 struct onenand_chip *this = mtd->priv;
1819 unsigned int density, process;
1821 /* Lock scheme depends on density and process */
1822 density = onenand_get_density(this->device_id);
1823 process = this->version_id >> ONENAND_VERSION_PROCESS_SHIFT;
1827 case ONENAND_DEVICE_DENSITY_4Gb:
1828 this->options |= ONENAND_HAS_2PLANE;
1830 case ONENAND_DEVICE_DENSITY_2Gb:
1831 /* 2Gb DDP don't have 2 plane */
1832 if (!ONENAND_IS_DDP(this))
1833 this->options |= ONENAND_HAS_2PLANE;
1834 this->options |= ONENAND_HAS_UNLOCK_ALL;
1836 case ONENAND_DEVICE_DENSITY_1Gb:
1837 /* A-Die has all block unlock */
1839 this->options |= ONENAND_HAS_UNLOCK_ALL;
1843 /* Some OneNAND has continuous lock scheme */
1845 this->options |= ONENAND_HAS_CONT_LOCK;
1849 if (this->options & ONENAND_HAS_CONT_LOCK)
1850 printk(KERN_DEBUG "Lock scheme is Continuous Lock\n");
1851 if (this->options & ONENAND_HAS_UNLOCK_ALL)
1852 printk(KERN_DEBUG "Chip support all block unlock\n");
1853 if (this->options & ONENAND_HAS_2PLANE)
1854 printk(KERN_DEBUG "Chip has 2 plane\n");
1858 * onenand_print_device_info - Print device ID
1859 * @param device device ID
1863 char *onenand_print_device_info(int device, int version)
1865 int vcc, demuxed, ddp, density;
1866 char *dev_info = malloc(80);
1869 vcc = device & ONENAND_DEVICE_VCC_MASK;
1870 demuxed = device & ONENAND_DEVICE_IS_DEMUX;
1871 ddp = device & ONENAND_DEVICE_IS_DDP;
1872 density = device >> ONENAND_DEVICE_DENSITY_SHIFT;
1873 p += sprintf(dev_info, "%sOneNAND%s %dMB %sV 16-bit (0x%02x)",
1874 demuxed ? "" : "Muxed ",
1876 (16 << density), vcc ? "2.65/3.3" : "1.8", device);
1878 sprintf(p, "\nOneNAND version = 0x%04x", version);
1879 printk("%s\n", dev_info);
1884 static const struct onenand_manufacturers onenand_manuf_ids[] = {
1885 {ONENAND_MFR_SAMSUNG, "Samsung"},
1889 * onenand_check_maf - Check manufacturer ID
1890 * @param manuf manufacturer ID
1892 * Check manufacturer ID
1894 static int onenand_check_maf(int manuf)
1896 int size = ARRAY_SIZE(onenand_manuf_ids);
1900 for (i = 0; size; i++)
1901 if (manuf == onenand_manuf_ids[i].id)
1905 name = onenand_manuf_ids[i].name;
1909 #ifdef ONENAND_DEBUG
1910 printk(KERN_DEBUG "OneNAND Manufacturer: %s (0x%0x)\n", name, manuf);
1917 * onenand_probe - [OneNAND Interface] Probe the OneNAND device
1918 * @param mtd MTD device structure
1920 * OneNAND detection method:
1921 * Compare the the values from command with ones from register
1923 static int onenand_probe(struct mtd_info *mtd)
1925 struct onenand_chip *this = mtd->priv;
1926 int bram_maf_id, bram_dev_id, maf_id, dev_id, ver_id;
1930 /* Save system configuration 1 */
1931 syscfg = this->read_word(this->base + ONENAND_REG_SYS_CFG1);
1932 /* Clear Sync. Burst Read mode to read BootRAM */
1933 this->write_word((syscfg & ~ONENAND_SYS_CFG1_SYNC_READ), this->base + ONENAND_REG_SYS_CFG1);
1935 /* Send the command for reading device ID from BootRAM */
1936 this->write_word(ONENAND_CMD_READID, this->base + ONENAND_BOOTRAM);
1938 /* Read manufacturer and device IDs from BootRAM */
1939 bram_maf_id = this->read_word(this->base + ONENAND_BOOTRAM + 0x0);
1940 bram_dev_id = this->read_word(this->base + ONENAND_BOOTRAM + 0x2);
1942 /* Reset OneNAND to read default register values */
1943 this->write_word(ONENAND_CMD_RESET, this->base + ONENAND_BOOTRAM);
1946 this->wait(mtd, FL_RESETING);
1948 /* Restore system configuration 1 */
1949 this->write_word(syscfg, this->base + ONENAND_REG_SYS_CFG1);
1951 /* Check manufacturer ID */
1952 if (onenand_check_maf(bram_maf_id))
1955 /* Read manufacturer and device IDs from Register */
1956 maf_id = this->read_word(this->base + ONENAND_REG_MANUFACTURER_ID);
1957 dev_id = this->read_word(this->base + ONENAND_REG_DEVICE_ID);
1958 ver_id = this->read_word(this->base + ONENAND_REG_VERSION_ID);
1960 /* Check OneNAND device */
1961 if (maf_id != bram_maf_id || dev_id != bram_dev_id)
1964 /* FIXME : Current OneNAND MTD doesn't support Flex-OneNAND */
1965 if (dev_id & (1 << 9)) {
1966 printk("Not yet support Flex-OneNAND\n");
1970 /* Flash device information */
1971 mtd->name = onenand_print_device_info(dev_id, ver_id);
1972 this->device_id = dev_id;
1973 this->version_id = ver_id;
1975 density = onenand_get_density(dev_id);
1976 this->chipsize = (16 << density) << 20;
1977 /* Set density mask. it is used for DDP */
1978 if (ONENAND_IS_DDP(this))
1979 this->density_mask = (1 << (density + 6));
1981 this->density_mask = 0;
1983 /* OneNAND page size & block size */
1984 /* The data buffer size is equal to page size */
1986 this->read_word(this->base + ONENAND_REG_DATA_BUFFER_SIZE);
1987 mtd->oobsize = mtd->writesize >> 5;
1988 /* Pagers per block is always 64 in OneNAND */
1989 mtd->erasesize = mtd->writesize << 6;
1991 this->erase_shift = ffs(mtd->erasesize) - 1;
1992 this->page_shift = ffs(mtd->writesize) - 1;
1993 this->ppb_shift = (this->erase_shift - this->page_shift);
1994 this->page_mask = (mtd->erasesize / mtd->writesize) - 1;
1995 /* It's real page size */
1996 this->writesize = mtd->writesize;
1998 /* REVIST: Multichip handling */
2000 mtd->size = this->chipsize;
2002 /* Check OneNAND features */
2003 onenand_check_features(mtd);
2005 mtd->flags = MTD_CAP_NANDFLASH;
2006 mtd->erase = onenand_erase;
2007 mtd->read = onenand_read;
2008 mtd->write = onenand_write;
2009 mtd->read_oob = onenand_read_oob;
2010 mtd->write_oob = onenand_write_oob;
2011 mtd->sync = onenand_sync;
2012 mtd->block_isbad = onenand_block_isbad;
2013 mtd->block_markbad = onenand_block_markbad;
2019 * onenand_scan - [OneNAND Interface] Scan for the OneNAND device
2020 * @param mtd MTD device structure
2021 * @param maxchips Number of chips to scan for
2023 * This fills out all the not initialized function pointers
2024 * with the defaults.
2025 * The flash ID is read and the mtd/chip structures are
2026 * filled with the appropriate values.
2028 int onenand_scan(struct mtd_info *mtd, int maxchips)
2030 struct onenand_chip *this = mtd->priv;
2032 if (!this->read_word)
2033 this->read_word = onenand_readw;
2034 if (!this->write_word)
2035 this->write_word = onenand_writew;
2038 this->command = onenand_command;
2040 this->wait = onenand_wait;
2041 if (!this->bbt_wait)
2042 this->bbt_wait = onenand_bbt_wait;
2044 if (!this->read_bufferram)
2045 this->read_bufferram = onenand_read_bufferram;
2046 if (!this->read_spareram)
2047 this->read_spareram = onenand_read_bufferram;
2048 if (!this->write_bufferram)
2049 this->write_bufferram = onenand_write_bufferram;
2051 if (!this->scan_bbt)
2052 this->scan_bbt = onenand_default_bbt;
2054 if (onenand_probe(mtd))
2057 /* Set Sync. Burst Read after probing */
2058 if (this->mmcontrol) {
2059 printk(KERN_INFO "OneNAND Sync. Burst Read support\n");
2060 this->read_bufferram = onenand_sync_read_bufferram;
2063 /* Allocate buffers, if necessary */
2064 if (!this->page_buf) {
2065 this->page_buf = kzalloc(mtd->writesize, GFP_KERNEL);
2066 if (!this->page_buf) {
2067 printk(KERN_ERR "onenand_scan(): Can't allocate page_buf\n");
2070 this->options |= ONENAND_PAGEBUF_ALLOC;
2072 if (!this->oob_buf) {
2073 this->oob_buf = kzalloc(mtd->oobsize, GFP_KERNEL);
2074 if (!this->oob_buf) {
2075 printk(KERN_ERR "onenand_scan: Can't allocate oob_buf\n");
2076 if (this->options & ONENAND_PAGEBUF_ALLOC) {
2077 this->options &= ~ONENAND_PAGEBUF_ALLOC;
2078 kfree(this->page_buf);
2082 this->options |= ONENAND_OOBBUF_ALLOC;
2085 /* Unlock whole block */
2086 onenand_unlock_all(mtd);
2088 return this->scan_bbt(mtd);
2092 * onenand_release - [OneNAND Interface] Free resources held by the OneNAND device
2093 * @param mtd MTD device structure
2095 void onenand_release(struct mtd_info *mtd)