2 * linux/drivers/mtd/onenand/onenand_base.c
4 * Copyright (C) 2005-2007 Samsung Electronics
5 * Kyungmin Park <kyungmin.park@samsung.com>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
12 #include <linux/kernel.h>
13 #include <linux/module.h>
14 #include <linux/init.h>
15 #include <linux/sched.h>
16 #include <linux/interrupt.h>
17 #include <linux/jiffies.h>
18 #include <linux/mtd/mtd.h>
19 #include <linux/mtd/onenand.h>
20 #include <linux/mtd/partitions.h>
25 * onenand_oob_64 - oob info for large (2KB) page
27 static struct nand_ecclayout onenand_oob_64 = {
36 {2, 3}, {14, 2}, {18, 3}, {30, 2},
37 {34, 3}, {46, 2}, {50, 3}, {62, 2}
42 * onenand_oob_32 - oob info for middle (1KB) page
44 static struct nand_ecclayout onenand_oob_32 = {
50 .oobfree = { {2, 3}, {14, 2}, {18, 3}, {30, 2} }
53 static const unsigned char ffchars[] = {
54 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
55 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 16 */
56 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
57 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 32 */
58 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
59 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 48 */
60 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
61 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 64 */
65 * onenand_readw - [OneNAND Interface] Read OneNAND register
66 * @param addr address to read
68 * Read OneNAND register
70 static unsigned short onenand_readw(void __iomem *addr)
76 * onenand_writew - [OneNAND Interface] Write OneNAND register with value
77 * @param value value to write
78 * @param addr address to write
80 * Write OneNAND register with value
82 static void onenand_writew(unsigned short value, void __iomem *addr)
88 * onenand_block_address - [DEFAULT] Get block address
89 * @param this onenand chip data structure
90 * @param block the block
91 * @return translated block address if DDP, otherwise same
93 * Setup Start Address 1 Register (F100h)
95 static int onenand_block_address(struct onenand_chip *this, int block)
97 /* Device Flash Core select, NAND Flash Block Address */
98 if (block & this->density_mask)
99 return ONENAND_DDP_CHIP1 | (block ^ this->density_mask);
105 * onenand_bufferram_address - [DEFAULT] Get bufferram address
106 * @param this onenand chip data structure
107 * @param block the block
108 * @return set DBS value if DDP, otherwise 0
110 * Setup Start Address 2 Register (F101h) for DDP
112 static int onenand_bufferram_address(struct onenand_chip *this, int block)
114 /* Device BufferRAM Select */
115 if (block & this->density_mask)
116 return ONENAND_DDP_CHIP1;
118 return ONENAND_DDP_CHIP0;
122 * onenand_page_address - [DEFAULT] Get page address
123 * @param page the page address
124 * @param sector the sector address
125 * @return combined page and sector address
127 * Setup Start Address 8 Register (F107h)
129 static int onenand_page_address(int page, int sector)
131 /* Flash Page Address, Flash Sector Address */
134 fpa = page & ONENAND_FPA_MASK;
135 fsa = sector & ONENAND_FSA_MASK;
137 return ((fpa << ONENAND_FPA_SHIFT) | fsa);
141 * onenand_buffer_address - [DEFAULT] Get buffer address
142 * @param dataram1 DataRAM index
143 * @param sectors the sector address
144 * @param count the number of sectors
145 * @return the start buffer value
147 * Setup Start Buffer Register (F200h)
149 static int onenand_buffer_address(int dataram1, int sectors, int count)
153 /* BufferRAM Sector Address */
154 bsa = sectors & ONENAND_BSA_MASK;
157 bsa |= ONENAND_BSA_DATARAM1; /* DataRAM1 */
159 bsa |= ONENAND_BSA_DATARAM0; /* DataRAM0 */
161 /* BufferRAM Sector Count */
162 bsc = count & ONENAND_BSC_MASK;
164 return ((bsa << ONENAND_BSA_SHIFT) | bsc);
168 * onenand_command - [DEFAULT] Send command to OneNAND device
169 * @param mtd MTD device structure
170 * @param cmd the command to be sent
171 * @param addr offset to read from or write to
172 * @param len number of bytes to read or write
174 * Send command to OneNAND device. This function is used for middle/large page
175 * devices (1KB/2KB Bytes per page)
177 static int onenand_command(struct mtd_info *mtd, int cmd, loff_t addr, size_t len)
179 struct onenand_chip *this = mtd->priv;
180 int value, readcmd = 0, block_cmd = 0;
183 /* Address translation */
185 case ONENAND_CMD_UNLOCK:
186 case ONENAND_CMD_LOCK:
187 case ONENAND_CMD_LOCK_TIGHT:
188 case ONENAND_CMD_UNLOCK_ALL:
193 case ONENAND_CMD_ERASE:
194 case ONENAND_CMD_BUFFERRAM:
195 case ONENAND_CMD_OTP_ACCESS:
197 block = (int) (addr >> this->erase_shift);
202 block = (int) (addr >> this->erase_shift);
203 page = (int) (addr >> this->page_shift);
204 page &= this->page_mask;
208 /* NOTE: The setting order of the registers is very important! */
209 if (cmd == ONENAND_CMD_BUFFERRAM) {
210 /* Select DataRAM for DDP */
211 value = onenand_bufferram_address(this, block);
212 this->write_word(value, 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, this->base + ONENAND_REG_START_ADDRESS1);
226 /* Select DataRAM for DDP */
227 value = onenand_bufferram_address(this, block);
228 this->write_word(value, this->base + ONENAND_REG_START_ADDRESS2);
233 /* Now we use page size operation */
234 int sectors = 4, count = 4;
238 case ONENAND_CMD_READ:
239 case ONENAND_CMD_READOOB:
240 dataram = ONENAND_SET_NEXT_BUFFERRAM(this);
245 dataram = ONENAND_CURRENT_BUFFERRAM(this);
249 /* Write 'FPA, FSA' of Flash */
250 value = onenand_page_address(page, sectors);
251 this->write_word(value, this->base + ONENAND_REG_START_ADDRESS8);
253 /* Write 'BSA, BSC' of DataRAM */
254 value = onenand_buffer_address(dataram, sectors, count);
255 this->write_word(value, this->base + ONENAND_REG_START_BUFFER);
258 /* Select DataRAM for DDP */
259 value = onenand_bufferram_address(this, block);
260 this->write_word(value, this->base + ONENAND_REG_START_ADDRESS2);
264 /* Interrupt clear */
265 this->write_word(ONENAND_INT_CLEAR, this->base + ONENAND_REG_INTERRUPT);
268 this->write_word(cmd, this->base + ONENAND_REG_COMMAND);
274 * onenand_wait - [DEFAULT] wait until the command is done
275 * @param mtd MTD device structure
276 * @param state state to select the max. timeout value
278 * Wait for command done. This applies to all OneNAND command
279 * Read can take up to 30us, erase up to 2ms and program up to 350us
280 * according to general OneNAND specs
282 static int onenand_wait(struct mtd_info *mtd, int state)
284 struct onenand_chip * this = mtd->priv;
285 unsigned long timeout;
286 unsigned int flags = ONENAND_INT_MASTER;
287 unsigned int interrupt = 0;
290 /* The 20 msec is enough */
291 timeout = jiffies + msecs_to_jiffies(20);
292 while (time_before(jiffies, timeout)) {
293 interrupt = this->read_word(this->base + ONENAND_REG_INTERRUPT);
295 if (interrupt & flags)
298 if (state != FL_READING)
301 /* To get correct interrupt status in timeout case */
302 interrupt = this->read_word(this->base + ONENAND_REG_INTERRUPT);
304 ctrl = this->read_word(this->base + ONENAND_REG_CTRL_STATUS);
306 if (ctrl & ONENAND_CTRL_ERROR) {
307 DEBUG(MTD_DEBUG_LEVEL0, "onenand_wait: controller error = 0x%04x\n", ctrl);
308 if (ctrl & ONENAND_CTRL_LOCK)
309 DEBUG(MTD_DEBUG_LEVEL0, "onenand_wait: it's locked error.\n");
313 if (interrupt & ONENAND_INT_READ) {
314 int ecc = this->read_word(this->base + ONENAND_REG_ECC_STATUS);
316 DEBUG(MTD_DEBUG_LEVEL0, "onenand_wait: ECC error = 0x%04x\n", ecc);
317 if (ecc & ONENAND_ECC_2BIT_ALL) {
318 mtd->ecc_stats.failed++;
320 } else if (ecc & ONENAND_ECC_1BIT_ALL)
321 mtd->ecc_stats.corrected++;
323 } else if (state == FL_READING) {
324 printk(KERN_ERR "onenand_wait: read timeout! ctrl=0x%04x intr=0x%04x\n", ctrl, interrupt);
332 * onenand_interrupt - [DEFAULT] onenand interrupt handler
333 * @param irq onenand interrupt number
334 * @param dev_id interrupt data
338 static irqreturn_t onenand_interrupt(int irq, void *data)
340 struct onenand_chip *this = (struct onenand_chip *) data;
342 /* To handle shared interrupt */
343 if (!this->complete.done)
344 complete(&this->complete);
350 * onenand_interrupt_wait - [DEFAULT] wait until the command is done
351 * @param mtd MTD device structure
352 * @param state state to select the max. timeout value
354 * Wait for command done.
356 static int onenand_interrupt_wait(struct mtd_info *mtd, int state)
358 struct onenand_chip *this = mtd->priv;
360 wait_for_completion(&this->complete);
362 return onenand_wait(mtd, state);
366 * onenand_try_interrupt_wait - [DEFAULT] try interrupt wait
367 * @param mtd MTD device structure
368 * @param state state to select the max. timeout value
370 * Try interrupt based wait (It is used one-time)
372 static int onenand_try_interrupt_wait(struct mtd_info *mtd, int state)
374 struct onenand_chip *this = mtd->priv;
375 unsigned long remain, timeout;
377 /* We use interrupt wait first */
378 this->wait = onenand_interrupt_wait;
380 timeout = msecs_to_jiffies(100);
381 remain = wait_for_completion_timeout(&this->complete, timeout);
383 printk(KERN_INFO "OneNAND: There's no interrupt. "
384 "We use the normal wait\n");
386 /* Release the irq */
387 free_irq(this->irq, this);
389 this->wait = onenand_wait;
392 return onenand_wait(mtd, state);
396 * onenand_setup_wait - [OneNAND Interface] setup onenand wait method
397 * @param mtd MTD device structure
399 * There's two method to wait onenand work
400 * 1. polling - read interrupt status register
401 * 2. interrupt - use the kernel interrupt method
403 static void onenand_setup_wait(struct mtd_info *mtd)
405 struct onenand_chip *this = mtd->priv;
408 init_completion(&this->complete);
410 if (this->irq <= 0) {
411 this->wait = onenand_wait;
415 if (request_irq(this->irq, &onenand_interrupt,
416 IRQF_SHARED, "onenand", this)) {
417 /* If we can't get irq, use the normal wait */
418 this->wait = onenand_wait;
422 /* Enable interrupt */
423 syscfg = this->read_word(this->base + ONENAND_REG_SYS_CFG1);
424 syscfg |= ONENAND_SYS_CFG1_IOBE;
425 this->write_word(syscfg, this->base + ONENAND_REG_SYS_CFG1);
427 this->wait = onenand_try_interrupt_wait;
431 * onenand_bufferram_offset - [DEFAULT] BufferRAM offset
432 * @param mtd MTD data structure
433 * @param area BufferRAM area
434 * @return offset given area
436 * Return BufferRAM offset given area
438 static inline int onenand_bufferram_offset(struct mtd_info *mtd, int area)
440 struct onenand_chip *this = mtd->priv;
442 if (ONENAND_CURRENT_BUFFERRAM(this)) {
443 if (area == ONENAND_DATARAM)
444 return mtd->writesize;
445 if (area == ONENAND_SPARERAM)
453 * onenand_read_bufferram - [OneNAND Interface] Read the bufferram area
454 * @param mtd MTD data structure
455 * @param area BufferRAM area
456 * @param buffer the databuffer to put/get data
457 * @param offset offset to read from or write to
458 * @param count number of bytes to read/write
460 * Read the BufferRAM area
462 static int onenand_read_bufferram(struct mtd_info *mtd, int area,
463 unsigned char *buffer, int offset, size_t count)
465 struct onenand_chip *this = mtd->priv;
466 void __iomem *bufferram;
468 bufferram = this->base + area;
470 bufferram += onenand_bufferram_offset(mtd, area);
472 if (ONENAND_CHECK_BYTE_ACCESS(count)) {
475 /* Align with word(16-bit) size */
478 /* Read word and save byte */
479 word = this->read_word(bufferram + offset + count);
480 buffer[count] = (word & 0xff);
483 memcpy(buffer, bufferram + offset, count);
489 * onenand_sync_read_bufferram - [OneNAND Interface] Read the bufferram area with Sync. Burst mode
490 * @param mtd MTD data structure
491 * @param area BufferRAM area
492 * @param buffer the databuffer to put/get data
493 * @param offset offset to read from or write to
494 * @param count number of bytes to read/write
496 * Read the BufferRAM area with Sync. Burst Mode
498 static int onenand_sync_read_bufferram(struct mtd_info *mtd, int area,
499 unsigned char *buffer, int offset, size_t count)
501 struct onenand_chip *this = mtd->priv;
502 void __iomem *bufferram;
504 bufferram = this->base + area;
506 bufferram += onenand_bufferram_offset(mtd, area);
508 this->mmcontrol(mtd, ONENAND_SYS_CFG1_SYNC_READ);
510 if (ONENAND_CHECK_BYTE_ACCESS(count)) {
513 /* Align with word(16-bit) size */
516 /* Read word and save byte */
517 word = this->read_word(bufferram + offset + count);
518 buffer[count] = (word & 0xff);
521 memcpy(buffer, bufferram + offset, count);
523 this->mmcontrol(mtd, 0);
529 * onenand_write_bufferram - [OneNAND Interface] Write the bufferram area
530 * @param mtd MTD data structure
531 * @param area BufferRAM area
532 * @param buffer the databuffer to put/get data
533 * @param offset offset to read from or write to
534 * @param count number of bytes to read/write
536 * Write the BufferRAM area
538 static int onenand_write_bufferram(struct mtd_info *mtd, int area,
539 const unsigned char *buffer, int offset, size_t count)
541 struct onenand_chip *this = mtd->priv;
542 void __iomem *bufferram;
544 bufferram = this->base + area;
546 bufferram += onenand_bufferram_offset(mtd, area);
548 if (ONENAND_CHECK_BYTE_ACCESS(count)) {
552 /* Align with word(16-bit) size */
555 /* Calculate byte access offset */
556 byte_offset = offset + count;
558 /* Read word and save byte */
559 word = this->read_word(bufferram + byte_offset);
560 word = (word & ~0xff) | buffer[count];
561 this->write_word(word, bufferram + byte_offset);
564 memcpy(bufferram + offset, buffer, count);
570 * onenand_check_bufferram - [GENERIC] Check BufferRAM information
571 * @param mtd MTD data structure
572 * @param addr address to check
573 * @return 1 if there are valid data, otherwise 0
575 * Check bufferram if there is data we required
577 static int onenand_check_bufferram(struct mtd_info *mtd, loff_t addr)
579 struct onenand_chip *this = mtd->priv;
583 blockpage = (int) (addr >> this->page_shift);
585 /* Is there valid data? */
586 i = ONENAND_CURRENT_BUFFERRAM(this);
587 if (this->bufferram[i].blockpage == blockpage)
590 /* Check another BufferRAM */
591 i = ONENAND_NEXT_BUFFERRAM(this);
592 if (this->bufferram[i].blockpage == blockpage) {
593 ONENAND_SET_NEXT_BUFFERRAM(this);
601 * onenand_update_bufferram - [GENERIC] Update BufferRAM information
602 * @param mtd MTD data structure
603 * @param addr address to update
604 * @param valid valid flag
606 * Update BufferRAM information
608 static void onenand_update_bufferram(struct mtd_info *mtd, loff_t addr,
611 struct onenand_chip *this = mtd->priv;
615 blockpage = (int) (addr >> this->page_shift);
617 /* Invalidate another BufferRAM */
618 i = ONENAND_NEXT_BUFFERRAM(this);
619 if (this->bufferram[i].blockpage == blockpage)
620 this->bufferram[i].blockpage = -1;
622 /* Update BufferRAM */
623 i = ONENAND_CURRENT_BUFFERRAM(this);
625 this->bufferram[i].blockpage = blockpage;
627 this->bufferram[i].blockpage = -1;
631 * onenand_get_device - [GENERIC] Get chip for selected access
632 * @param mtd MTD device structure
633 * @param new_state the state which is requested
635 * Get the device and lock it for exclusive access
637 static int onenand_get_device(struct mtd_info *mtd, int new_state)
639 struct onenand_chip *this = mtd->priv;
640 DECLARE_WAITQUEUE(wait, current);
643 * Grab the lock and see if the device is available
646 spin_lock(&this->chip_lock);
647 if (this->state == FL_READY) {
648 this->state = new_state;
649 spin_unlock(&this->chip_lock);
652 if (new_state == FL_PM_SUSPENDED) {
653 spin_unlock(&this->chip_lock);
654 return (this->state == FL_PM_SUSPENDED) ? 0 : -EAGAIN;
656 set_current_state(TASK_UNINTERRUPTIBLE);
657 add_wait_queue(&this->wq, &wait);
658 spin_unlock(&this->chip_lock);
660 remove_wait_queue(&this->wq, &wait);
667 * onenand_release_device - [GENERIC] release chip
668 * @param mtd MTD device structure
670 * Deselect, release chip lock and wake up anyone waiting on the device
672 static void onenand_release_device(struct mtd_info *mtd)
674 struct onenand_chip *this = mtd->priv;
676 /* Release the chip */
677 spin_lock(&this->chip_lock);
678 this->state = FL_READY;
680 spin_unlock(&this->chip_lock);
684 * onenand_read - [MTD Interface] Read data from flash
685 * @param mtd MTD device structure
686 * @param from offset to read from
687 * @param len number of bytes to read
688 * @param retlen pointer to variable to store the number of read bytes
689 * @param buf the databuffer to put data
693 static int onenand_read(struct mtd_info *mtd, loff_t from, size_t len,
694 size_t *retlen, u_char *buf)
696 struct onenand_chip *this = mtd->priv;
697 struct mtd_ecc_stats stats;
698 int read = 0, column;
700 int ret = 0, boundary = 0;
702 DEBUG(MTD_DEBUG_LEVEL3, "onenand_read: from = 0x%08x, len = %i\n", (unsigned int) from, (int) len);
704 /* Do not allow reads past end of device */
705 if ((from + len) > mtd->size) {
706 DEBUG(MTD_DEBUG_LEVEL0, "onenand_read: Attempt read beyond end of device\n");
711 /* Grab the lock and see if the device is available */
712 onenand_get_device(mtd, FL_READING);
714 stats = mtd->ecc_stats;
716 /* Read-while-load method */
718 /* Do first load to bufferRAM */
720 if (!onenand_check_bufferram(mtd, from)) {
721 this->command(mtd, ONENAND_CMD_READ, from, mtd->writesize);
722 ret = this->wait(mtd, FL_READING);
723 onenand_update_bufferram(mtd, from, !ret);
727 thislen = min_t(int, mtd->writesize, len - read);
728 column = from & (mtd->writesize - 1);
729 if (column + thislen > mtd->writesize)
730 thislen = mtd->writesize - column;
733 /* If there is more to load then start next load */
735 if (read + thislen < len) {
736 this->command(mtd, ONENAND_CMD_READ, from, mtd->writesize);
738 * Chip boundary handling in DDP
739 * Now we issued chip 1 read and pointed chip 1
740 * bufferam so we have to point chip 0 bufferam.
742 if (ONENAND_IS_DDP(this) &&
743 unlikely(from == (this->chipsize >> 1))) {
744 this->write_word(ONENAND_DDP_CHIP0, this->base + ONENAND_REG_START_ADDRESS2);
748 ONENAND_SET_PREV_BUFFERRAM(this);
750 /* While load is going, read from last bufferRAM */
751 this->read_bufferram(mtd, ONENAND_DATARAM, buf, column, thislen);
752 /* See if we are done */
756 /* Set up for next read from bufferRAM */
757 if (unlikely(boundary))
758 this->write_word(ONENAND_DDP_CHIP1, this->base + ONENAND_REG_START_ADDRESS2);
759 ONENAND_SET_NEXT_BUFFERRAM(this);
761 thislen = min_t(int, mtd->writesize, len - read);
764 /* Now wait for load */
765 ret = this->wait(mtd, FL_READING);
766 onenand_update_bufferram(mtd, from, !ret);
769 /* Deselect and wake up anyone waiting on the device */
770 onenand_release_device(mtd);
773 * Return success, if no ECC failures, else -EBADMSG
774 * fs driver will take care of that, because
775 * retlen == desired len and result == -EBADMSG
779 if (mtd->ecc_stats.failed - stats.failed)
785 return mtd->ecc_stats.corrected - stats.corrected ? -EUCLEAN : 0;
789 * onenand_transfer_auto_oob - [Internal] oob auto-placement transfer
790 * @param mtd MTD device structure
791 * @param buf destination address
792 * @param column oob offset to read from
793 * @param thislen oob length to read
795 static int onenand_transfer_auto_oob(struct mtd_info *mtd, uint8_t *buf, int column,
798 struct onenand_chip *this = mtd->priv;
799 struct nand_oobfree *free;
800 int readcol = column;
801 int readend = column + thislen;
803 uint8_t *oob_buf = this->page_buf + mtd->writesize;
805 for (free = this->ecclayout->oobfree; free->length; ++free) {
806 if (readcol >= lastgap)
807 readcol += free->offset - lastgap;
808 if (readend >= lastgap)
809 readend += free->offset - lastgap;
810 lastgap = free->offset + free->length;
812 this->read_bufferram(mtd, ONENAND_SPARERAM, oob_buf, 0, mtd->oobsize);
813 for (free = this->ecclayout->oobfree; free->length; ++free) {
814 int free_end = free->offset + free->length;
815 if (free->offset < readend && free_end > readcol) {
816 int st = max_t(int,free->offset,readcol);
817 int ed = min_t(int,free_end,readend);
819 memcpy(buf, oob_buf + st, n);
827 * onenand_do_read_oob - [MTD Interface] OneNAND read out-of-band
828 * @param mtd MTD device structure
829 * @param from offset to read from
830 * @param len number of bytes to read
831 * @param retlen pointer to variable to store the number of read bytes
832 * @param buf the databuffer to put data
833 * @param mode operation mode
835 * OneNAND read out-of-band data from the spare area
837 int onenand_do_read_oob(struct mtd_info *mtd, loff_t from, size_t len,
838 size_t *retlen, u_char *buf, mtd_oob_mode_t mode)
840 struct onenand_chip *this = mtd->priv;
841 int read = 0, thislen, column, oobsize;
844 DEBUG(MTD_DEBUG_LEVEL3, "onenand_read_oob: from = 0x%08x, len = %i\n", (unsigned int) from, (int) len);
846 /* Initialize return length value */
849 if (mode == MTD_OOB_AUTO)
850 oobsize = this->ecclayout->oobavail;
852 oobsize = mtd->oobsize;
854 column = from & (mtd->oobsize - 1);
856 if (unlikely(column >= oobsize)) {
857 DEBUG(MTD_DEBUG_LEVEL0, "onenand_read_oob: Attempted to start read outside oob\n");
861 /* Do not allow reads past end of device */
862 if (unlikely(from >= mtd->size ||
863 column + len > ((mtd->size >> this->page_shift) -
864 (from >> this->page_shift)) * oobsize)) {
865 DEBUG(MTD_DEBUG_LEVEL0, "onenand_read_oob: Attempted to read beyond end of device\n");
869 /* Grab the lock and see if the device is available */
870 onenand_get_device(mtd, FL_READING);
875 thislen = oobsize - column;
876 thislen = min_t(int, thislen, len);
878 this->command(mtd, ONENAND_CMD_READOOB, from, mtd->oobsize);
880 onenand_update_bufferram(mtd, from, 0);
882 ret = this->wait(mtd, FL_READING);
883 /* First copy data and check return value for ECC handling */
885 if (mode == MTD_OOB_AUTO)
886 onenand_transfer_auto_oob(mtd, buf, column, thislen);
888 this->read_bufferram(mtd, ONENAND_SPARERAM, buf, column, thislen);
891 DEBUG(MTD_DEBUG_LEVEL0, "onenand_read_oob: read failed = 0x%x\n", ret);
905 from += mtd->writesize;
910 /* Deselect and wake up anyone waiting on the device */
911 onenand_release_device(mtd);
918 * onenand_read_oob - [MTD Interface] NAND write data and/or out-of-band
919 * @mtd: MTD device structure
920 * @from: offset to read from
921 * @ops: oob operation description structure
923 static int onenand_read_oob(struct mtd_info *mtd, loff_t from,
924 struct mtd_oob_ops *ops)
931 /* Not implemented yet */
935 return onenand_do_read_oob(mtd, from + ops->ooboffs, ops->ooblen,
936 &ops->oobretlen, ops->oobbuf, ops->mode);
939 #ifdef CONFIG_MTD_ONENAND_VERIFY_WRITE
941 * onenand_verify_oob - [GENERIC] verify the oob contents after a write
942 * @param mtd MTD device structure
943 * @param buf the databuffer to verify
944 * @param to offset to read from
947 static int onenand_verify_oob(struct mtd_info *mtd, const u_char *buf, loff_t to)
949 struct onenand_chip *this = mtd->priv;
950 char *readp = this->page_buf + mtd->writesize;
953 this->command(mtd, ONENAND_CMD_READOOB, to, mtd->oobsize);
954 onenand_update_bufferram(mtd, to, 0);
955 status = this->wait(mtd, FL_READING);
959 this->read_bufferram(mtd, ONENAND_SPARERAM, readp, 0, mtd->oobsize);
960 for(i = 0; i < mtd->oobsize; i++)
961 if (buf[i] != 0xFF && buf[i] != readp[i])
968 * onenand_verify - [GENERIC] verify the chip contents after a write
969 * @param mtd MTD device structure
970 * @param buf the databuffer to verify
971 * @param addr offset to read from
972 * @param len number of bytes to read and compare
975 static int onenand_verify(struct mtd_info *mtd, const u_char *buf, loff_t addr, size_t len)
977 struct onenand_chip *this = mtd->priv;
978 void __iomem *dataram;
983 thislen = min_t(int, mtd->writesize, len);
984 column = addr & (mtd->writesize - 1);
985 if (column + thislen > mtd->writesize)
986 thislen = mtd->writesize - column;
988 this->command(mtd, ONENAND_CMD_READ, addr, mtd->writesize);
990 onenand_update_bufferram(mtd, addr, 0);
992 ret = this->wait(mtd, FL_READING);
996 onenand_update_bufferram(mtd, addr, 1);
998 dataram = this->base + ONENAND_DATARAM;
999 dataram += onenand_bufferram_offset(mtd, ONENAND_DATARAM);
1001 if (memcmp(buf, dataram + column, thislen))
1012 #define onenand_verify(...) (0)
1013 #define onenand_verify_oob(...) (0)
1016 #define NOTALIGNED(x) ((x & (this->subpagesize - 1)) != 0)
1019 * onenand_write - [MTD Interface] write buffer to FLASH
1020 * @param mtd MTD device structure
1021 * @param to offset to write to
1022 * @param len number of bytes to write
1023 * @param retlen pointer to variable to store the number of written bytes
1024 * @param buf the data to write
1028 static int onenand_write(struct mtd_info *mtd, loff_t to, size_t len,
1029 size_t *retlen, const u_char *buf)
1031 struct onenand_chip *this = mtd->priv;
1034 int column, subpage;
1036 DEBUG(MTD_DEBUG_LEVEL3, "onenand_write: to = 0x%08x, len = %i\n", (unsigned int) to, (int) len);
1038 /* Initialize retlen, in case of early exit */
1041 /* Do not allow writes past end of device */
1042 if (unlikely((to + len) > mtd->size)) {
1043 DEBUG(MTD_DEBUG_LEVEL0, "onenand_write: Attempt write to past end of device\n");
1047 /* Reject writes, which are not page aligned */
1048 if (unlikely(NOTALIGNED(to)) || unlikely(NOTALIGNED(len))) {
1049 DEBUG(MTD_DEBUG_LEVEL0, "onenand_write: Attempt to write not page aligned data\n");
1053 column = to & (mtd->writesize - 1);
1054 subpage = column || (len & (mtd->writesize - 1));
1056 /* Grab the lock and see if the device is available */
1057 onenand_get_device(mtd, FL_WRITING);
1059 /* Loop until all data write */
1060 while (written < len) {
1061 int bytes = mtd->writesize;
1062 int thislen = min_t(int, bytes, len - written);
1063 u_char *wbuf = (u_char *) buf;
1067 this->command(mtd, ONENAND_CMD_BUFFERRAM, to, bytes);
1069 /* Partial page write */
1071 bytes = min_t(int, bytes - column, (int) len);
1072 memset(this->page_buf, 0xff, mtd->writesize);
1073 memcpy(this->page_buf + column, buf, bytes);
1074 wbuf = this->page_buf;
1075 /* Even though partial write, we need page size */
1076 thislen = mtd->writesize;
1079 this->write_bufferram(mtd, ONENAND_DATARAM, wbuf, 0, thislen);
1080 this->write_bufferram(mtd, ONENAND_SPARERAM, ffchars, 0, mtd->oobsize);
1082 this->command(mtd, ONENAND_CMD_PROG, to, mtd->writesize);
1084 /* In partial page write we don't update bufferram */
1085 onenand_update_bufferram(mtd, to, !subpage);
1087 ret = this->wait(mtd, FL_WRITING);
1089 DEBUG(MTD_DEBUG_LEVEL0, "onenand_write: write filaed %d\n", ret);
1093 /* Only check verify write turn on */
1094 ret = onenand_verify(mtd, (u_char *) wbuf, to, thislen);
1096 DEBUG(MTD_DEBUG_LEVEL0, "onenand_write: verify failed %d\n", ret);
1109 /* Deselect and wake up anyone waiting on the device */
1110 onenand_release_device(mtd);
1118 * onenand_fill_auto_oob - [Internal] oob auto-placement transfer
1119 * @param mtd MTD device structure
1120 * @param oob_buf oob buffer
1121 * @param buf source address
1122 * @param column oob offset to write to
1123 * @param thislen oob length to write
1125 static int onenand_fill_auto_oob(struct mtd_info *mtd, u_char *oob_buf,
1126 const u_char *buf, int column, int thislen)
1128 struct onenand_chip *this = mtd->priv;
1129 struct nand_oobfree *free;
1130 int writecol = column;
1131 int writeend = column + thislen;
1134 for (free = this->ecclayout->oobfree; free->length; ++free) {
1135 if (writecol >= lastgap)
1136 writecol += free->offset - lastgap;
1137 if (writeend >= lastgap)
1138 writeend += free->offset - lastgap;
1139 lastgap = free->offset + free->length;
1141 for (free = this->ecclayout->oobfree; free->length; ++free) {
1142 int free_end = free->offset + free->length;
1143 if (free->offset < writeend && free_end > writecol) {
1144 int st = max_t(int,free->offset,writecol);
1145 int ed = min_t(int,free_end,writeend);
1147 memcpy(oob_buf + st, buf, n);
1155 * onenand_do_write_oob - [Internal] OneNAND write out-of-band
1156 * @param mtd MTD device structure
1157 * @param to offset to write to
1158 * @param len number of bytes to write
1159 * @param retlen pointer to variable to store the number of written bytes
1160 * @param buf the data to write
1161 * @param mode operation mode
1163 * OneNAND write out-of-band
1165 static int onenand_do_write_oob(struct mtd_info *mtd, loff_t to, size_t len,
1166 size_t *retlen, const u_char *buf, mtd_oob_mode_t mode)
1168 struct onenand_chip *this = mtd->priv;
1169 int column, ret = 0, oobsize;
1172 DEBUG(MTD_DEBUG_LEVEL3, "onenand_write_oob: to = 0x%08x, len = %i\n", (unsigned int) to, (int) len);
1174 /* Initialize retlen, in case of early exit */
1177 if (mode == MTD_OOB_AUTO)
1178 oobsize = this->ecclayout->oobavail;
1180 oobsize = mtd->oobsize;
1182 column = to & (mtd->oobsize - 1);
1184 if (unlikely(column >= oobsize)) {
1185 DEBUG(MTD_DEBUG_LEVEL0, "onenand_write_oob: Attempted to start write outside oob\n");
1189 /* Do not allow reads past end of device */
1190 if (unlikely(to >= mtd->size ||
1191 column + len > ((mtd->size >> this->page_shift) -
1192 (to >> this->page_shift)) * oobsize)) {
1193 DEBUG(MTD_DEBUG_LEVEL0, "onenand_write_oob: Attempted to write past end of device\n");
1197 /* Grab the lock and see if the device is available */
1198 onenand_get_device(mtd, FL_WRITING);
1200 /* Loop until all data write */
1201 while (written < len) {
1202 int thislen = min_t(int, oobsize, len - written);
1206 this->command(mtd, ONENAND_CMD_BUFFERRAM, to, mtd->oobsize);
1208 /* We send data to spare ram with oobsize
1209 * to prevent byte access */
1210 memset(this->page_buf, 0xff, mtd->oobsize);
1211 if (mode == MTD_OOB_AUTO)
1212 onenand_fill_auto_oob(mtd, this->page_buf, buf, column, thislen);
1214 memcpy(this->page_buf + column, buf, thislen);
1215 this->write_bufferram(mtd, ONENAND_SPARERAM, this->page_buf, 0, mtd->oobsize);
1217 this->command(mtd, ONENAND_CMD_PROGOOB, to, mtd->oobsize);
1219 onenand_update_bufferram(mtd, to, 0);
1221 ret = this->wait(mtd, FL_WRITING);
1223 DEBUG(MTD_DEBUG_LEVEL0, "onenand_write_oob: write failed %d\n", ret);
1227 ret = onenand_verify_oob(mtd, this->page_buf, to);
1229 DEBUG(MTD_DEBUG_LEVEL0, "onenand_write_oob: verify failed %d\n", ret);
1237 to += mtd->writesize;
1242 /* Deselect and wake up anyone waiting on the device */
1243 onenand_release_device(mtd);
1251 * onenand_write_oob - [MTD Interface] NAND write data and/or out-of-band
1252 * @mtd: MTD device structure
1253 * @from: offset to read from
1254 * @ops: oob operation description structure
1256 static int onenand_write_oob(struct mtd_info *mtd, loff_t to,
1257 struct mtd_oob_ops *ops)
1259 switch (ops->mode) {
1264 /* Not implemented yet */
1268 return onenand_do_write_oob(mtd, to + ops->ooboffs, ops->ooblen,
1269 &ops->oobretlen, ops->oobbuf, ops->mode);
1273 * onenand_block_checkbad - [GENERIC] Check if a block is marked bad
1274 * @param mtd MTD device structure
1275 * @param ofs offset from device start
1276 * @param getchip 0, if the chip is already selected
1277 * @param allowbbt 1, if its allowed to access the bbt area
1279 * Check, if the block is bad. Either by reading the bad block table or
1280 * calling of the scan function.
1282 static int onenand_block_checkbad(struct mtd_info *mtd, loff_t ofs, int getchip, int allowbbt)
1284 struct onenand_chip *this = mtd->priv;
1285 struct bbm_info *bbm = this->bbm;
1287 /* Return info from the table */
1288 return bbm->isbad_bbt(mtd, ofs, allowbbt);
1292 * onenand_erase - [MTD Interface] erase block(s)
1293 * @param mtd MTD device structure
1294 * @param instr erase instruction
1296 * Erase one ore more blocks
1298 static int onenand_erase(struct mtd_info *mtd, struct erase_info *instr)
1300 struct onenand_chip *this = mtd->priv;
1301 unsigned int block_size;
1306 DEBUG(MTD_DEBUG_LEVEL3, "onenand_erase: start = 0x%08x, len = %i\n", (unsigned int) instr->addr, (unsigned int) instr->len);
1308 block_size = (1 << this->erase_shift);
1310 /* Start address must align on block boundary */
1311 if (unlikely(instr->addr & (block_size - 1))) {
1312 DEBUG(MTD_DEBUG_LEVEL0, "onenand_erase: Unaligned address\n");
1316 /* Length must align on block boundary */
1317 if (unlikely(instr->len & (block_size - 1))) {
1318 DEBUG(MTD_DEBUG_LEVEL0, "onenand_erase: Length not block aligned\n");
1322 /* Do not allow erase past end of device */
1323 if (unlikely((instr->len + instr->addr) > mtd->size)) {
1324 DEBUG(MTD_DEBUG_LEVEL0, "onenand_erase: Erase past end of device\n");
1328 instr->fail_addr = 0xffffffff;
1330 /* Grab the lock and see if the device is available */
1331 onenand_get_device(mtd, FL_ERASING);
1333 /* Loop throught the pages */
1337 instr->state = MTD_ERASING;
1342 /* Check if we have a bad block, we do not erase bad blocks */
1343 if (onenand_block_checkbad(mtd, addr, 0, 0)) {
1344 printk (KERN_WARNING "onenand_erase: attempt to erase a bad block at addr 0x%08x\n", (unsigned int) addr);
1345 instr->state = MTD_ERASE_FAILED;
1349 this->command(mtd, ONENAND_CMD_ERASE, addr, block_size);
1351 ret = this->wait(mtd, FL_ERASING);
1352 /* Check, if it is write protected */
1354 DEBUG(MTD_DEBUG_LEVEL0, "onenand_erase: Failed erase, block %d\n", (unsigned) (addr >> this->erase_shift));
1355 instr->state = MTD_ERASE_FAILED;
1356 instr->fail_addr = addr;
1364 instr->state = MTD_ERASE_DONE;
1368 ret = instr->state == MTD_ERASE_DONE ? 0 : -EIO;
1369 /* Do call back function */
1371 mtd_erase_callback(instr);
1373 /* Deselect and wake up anyone waiting on the device */
1374 onenand_release_device(mtd);
1380 * onenand_sync - [MTD Interface] sync
1381 * @param mtd MTD device structure
1383 * Sync is actually a wait for chip ready function
1385 static void onenand_sync(struct mtd_info *mtd)
1387 DEBUG(MTD_DEBUG_LEVEL3, "onenand_sync: called\n");
1389 /* Grab the lock and see if the device is available */
1390 onenand_get_device(mtd, FL_SYNCING);
1392 /* Release it and go back */
1393 onenand_release_device(mtd);
1397 * onenand_block_isbad - [MTD Interface] Check whether the block at the given offset is bad
1398 * @param mtd MTD device structure
1399 * @param ofs offset relative to mtd start
1401 * Check whether the block is bad
1403 static int onenand_block_isbad(struct mtd_info *mtd, loff_t ofs)
1405 /* Check for invalid offset */
1406 if (ofs > mtd->size)
1409 return onenand_block_checkbad(mtd, ofs, 1, 0);
1413 * onenand_default_block_markbad - [DEFAULT] mark a block bad
1414 * @param mtd MTD device structure
1415 * @param ofs offset from device start
1417 * This is the default implementation, which can be overridden by
1418 * a hardware specific driver.
1420 static int onenand_default_block_markbad(struct mtd_info *mtd, loff_t ofs)
1422 struct onenand_chip *this = mtd->priv;
1423 struct bbm_info *bbm = this->bbm;
1424 u_char buf[2] = {0, 0};
1428 /* Get block number */
1429 block = ((int) ofs) >> bbm->bbt_erase_shift;
1431 bbm->bbt[block >> 2] |= 0x01 << ((block & 0x03) << 1);
1433 /* We write two bytes, so we dont have to mess with 16 bit access */
1434 ofs += mtd->oobsize + (bbm->badblockpos & ~0x01);
1435 return onenand_do_write_oob(mtd, ofs , 2, &retlen, buf, MTD_OOB_PLACE);
1439 * onenand_block_markbad - [MTD Interface] Mark the block at the given offset as bad
1440 * @param mtd MTD device structure
1441 * @param ofs offset relative to mtd start
1443 * Mark the block as bad
1445 static int onenand_block_markbad(struct mtd_info *mtd, loff_t ofs)
1447 struct onenand_chip *this = mtd->priv;
1450 ret = onenand_block_isbad(mtd, ofs);
1452 /* If it was bad already, return success and do nothing */
1458 return this->block_markbad(mtd, ofs);
1462 * onenand_do_lock_cmd - [OneNAND Interface] Lock or unlock block(s)
1463 * @param mtd MTD device structure
1464 * @param ofs offset relative to mtd start
1465 * @param len number of bytes to lock or unlock
1467 * Lock or unlock one or more blocks
1469 static int onenand_do_lock_cmd(struct mtd_info *mtd, loff_t ofs, size_t len, int cmd)
1471 struct onenand_chip *this = mtd->priv;
1472 int start, end, block, value, status;
1475 start = ofs >> this->erase_shift;
1476 end = len >> this->erase_shift;
1478 if (cmd == ONENAND_CMD_LOCK)
1479 wp_status_mask = ONENAND_WP_LS;
1481 wp_status_mask = ONENAND_WP_US;
1483 /* Continuous lock scheme */
1484 if (this->options & ONENAND_HAS_CONT_LOCK) {
1485 /* Set start block address */
1486 this->write_word(start, this->base + ONENAND_REG_START_BLOCK_ADDRESS);
1487 /* Set end block address */
1488 this->write_word(start + end - 1, this->base + ONENAND_REG_END_BLOCK_ADDRESS);
1489 /* Write lock command */
1490 this->command(mtd, cmd, 0, 0);
1492 /* There's no return value */
1493 this->wait(mtd, FL_LOCKING);
1496 while (this->read_word(this->base + ONENAND_REG_CTRL_STATUS)
1497 & ONENAND_CTRL_ONGO)
1500 /* Check lock status */
1501 status = this->read_word(this->base + ONENAND_REG_WP_STATUS);
1502 if (!(status & wp_status_mask))
1503 printk(KERN_ERR "wp status = 0x%x\n", status);
1508 /* Block lock scheme */
1509 for (block = start; block < start + end; block++) {
1510 /* Set block address */
1511 value = onenand_block_address(this, block);
1512 this->write_word(value, this->base + ONENAND_REG_START_ADDRESS1);
1513 /* Select DataRAM for DDP */
1514 value = onenand_bufferram_address(this, block);
1515 this->write_word(value, this->base + ONENAND_REG_START_ADDRESS2);
1516 /* Set start block address */
1517 this->write_word(block, this->base + ONENAND_REG_START_BLOCK_ADDRESS);
1518 /* Write lock command */
1519 this->command(mtd, cmd, 0, 0);
1521 /* There's no return value */
1522 this->wait(mtd, FL_LOCKING);
1525 while (this->read_word(this->base + ONENAND_REG_CTRL_STATUS)
1526 & ONENAND_CTRL_ONGO)
1529 /* Check lock status */
1530 status = this->read_word(this->base + ONENAND_REG_WP_STATUS);
1531 if (!(status & wp_status_mask))
1532 printk(KERN_ERR "block = %d, wp status = 0x%x\n", block, status);
1539 * onenand_lock - [MTD Interface] Lock block(s)
1540 * @param mtd MTD device structure
1541 * @param ofs offset relative to mtd start
1542 * @param len number of bytes to unlock
1544 * Lock one or more blocks
1546 static int onenand_lock(struct mtd_info *mtd, loff_t ofs, size_t len)
1548 return onenand_do_lock_cmd(mtd, ofs, len, ONENAND_CMD_LOCK);
1552 * onenand_unlock - [MTD Interface] Unlock block(s)
1553 * @param mtd MTD device structure
1554 * @param ofs offset relative to mtd start
1555 * @param len number of bytes to unlock
1557 * Unlock one or more blocks
1559 static int onenand_unlock(struct mtd_info *mtd, loff_t ofs, size_t len)
1561 return onenand_do_lock_cmd(mtd, ofs, len, ONENAND_CMD_UNLOCK);
1565 * onenand_check_lock_status - [OneNAND Interface] Check lock status
1566 * @param this onenand chip data structure
1570 static void onenand_check_lock_status(struct onenand_chip *this)
1572 unsigned int value, block, status;
1575 end = this->chipsize >> this->erase_shift;
1576 for (block = 0; block < end; block++) {
1577 /* Set block address */
1578 value = onenand_block_address(this, block);
1579 this->write_word(value, this->base + ONENAND_REG_START_ADDRESS1);
1580 /* Select DataRAM for DDP */
1581 value = onenand_bufferram_address(this, block);
1582 this->write_word(value, this->base + ONENAND_REG_START_ADDRESS2);
1583 /* Set start block address */
1584 this->write_word(block, this->base + ONENAND_REG_START_BLOCK_ADDRESS);
1586 /* Check lock status */
1587 status = this->read_word(this->base + ONENAND_REG_WP_STATUS);
1588 if (!(status & ONENAND_WP_US))
1589 printk(KERN_ERR "block = %d, wp status = 0x%x\n", block, status);
1594 * onenand_unlock_all - [OneNAND Interface] unlock all blocks
1595 * @param mtd MTD device structure
1599 static int onenand_unlock_all(struct mtd_info *mtd)
1601 struct onenand_chip *this = mtd->priv;
1603 if (this->options & ONENAND_HAS_UNLOCK_ALL) {
1604 /* Set start block address */
1605 this->write_word(0, this->base + ONENAND_REG_START_BLOCK_ADDRESS);
1606 /* Write unlock command */
1607 this->command(mtd, ONENAND_CMD_UNLOCK_ALL, 0, 0);
1609 /* There's no return value */
1610 this->wait(mtd, FL_LOCKING);
1613 while (this->read_word(this->base + ONENAND_REG_CTRL_STATUS)
1614 & ONENAND_CTRL_ONGO)
1617 /* Workaround for all block unlock in DDP */
1618 if (ONENAND_IS_DDP(this)) {
1619 /* 1st block on another chip */
1620 loff_t ofs = this->chipsize >> 1;
1621 size_t len = mtd->erasesize;
1623 onenand_unlock(mtd, ofs, len);
1626 onenand_check_lock_status(this);
1631 onenand_unlock(mtd, 0x0, this->chipsize);
1636 #ifdef CONFIG_MTD_ONENAND_OTP
1638 /* Interal OTP operation */
1639 typedef int (*otp_op_t)(struct mtd_info *mtd, loff_t form, size_t len,
1640 size_t *retlen, u_char *buf);
1643 * do_otp_read - [DEFAULT] Read OTP block area
1644 * @param mtd MTD device structure
1645 * @param from The offset to read
1646 * @param len number of bytes to read
1647 * @param retlen pointer to variable to store the number of readbytes
1648 * @param buf the databuffer to put/get data
1650 * Read OTP block area.
1652 static int do_otp_read(struct mtd_info *mtd, loff_t from, size_t len,
1653 size_t *retlen, u_char *buf)
1655 struct onenand_chip *this = mtd->priv;
1658 /* Enter OTP access mode */
1659 this->command(mtd, ONENAND_CMD_OTP_ACCESS, 0, 0);
1660 this->wait(mtd, FL_OTPING);
1662 ret = mtd->read(mtd, from, len, retlen, buf);
1664 /* Exit OTP access mode */
1665 this->command(mtd, ONENAND_CMD_RESET, 0, 0);
1666 this->wait(mtd, FL_RESETING);
1672 * do_otp_write - [DEFAULT] Write OTP block area
1673 * @param mtd MTD device structure
1674 * @param from The offset to write
1675 * @param len number of bytes to write
1676 * @param retlen pointer to variable to store the number of write bytes
1677 * @param buf the databuffer to put/get data
1679 * Write OTP block area.
1681 static int do_otp_write(struct mtd_info *mtd, loff_t from, size_t len,
1682 size_t *retlen, u_char *buf)
1684 struct onenand_chip *this = mtd->priv;
1685 unsigned char *pbuf = buf;
1688 /* Force buffer page aligned */
1689 if (len < mtd->writesize) {
1690 memcpy(this->page_buf, buf, len);
1691 memset(this->page_buf + len, 0xff, mtd->writesize - len);
1692 pbuf = this->page_buf;
1693 len = mtd->writesize;
1696 /* Enter OTP access mode */
1697 this->command(mtd, ONENAND_CMD_OTP_ACCESS, 0, 0);
1698 this->wait(mtd, FL_OTPING);
1700 ret = mtd->write(mtd, from, len, retlen, pbuf);
1702 /* Exit OTP access mode */
1703 this->command(mtd, ONENAND_CMD_RESET, 0, 0);
1704 this->wait(mtd, FL_RESETING);
1710 * do_otp_lock - [DEFAULT] Lock OTP block area
1711 * @param mtd MTD device structure
1712 * @param from The offset to lock
1713 * @param len number of bytes to lock
1714 * @param retlen pointer to variable to store the number of lock bytes
1715 * @param buf the databuffer to put/get data
1717 * Lock OTP block area.
1719 static int do_otp_lock(struct mtd_info *mtd, loff_t from, size_t len,
1720 size_t *retlen, u_char *buf)
1722 struct onenand_chip *this = mtd->priv;
1725 /* Enter OTP access mode */
1726 this->command(mtd, ONENAND_CMD_OTP_ACCESS, 0, 0);
1727 this->wait(mtd, FL_OTPING);
1729 ret = onenand_do_write_oob(mtd, from, len, retlen, buf, MTD_OOB_PLACE);
1731 /* Exit OTP access mode */
1732 this->command(mtd, ONENAND_CMD_RESET, 0, 0);
1733 this->wait(mtd, FL_RESETING);
1739 * onenand_otp_walk - [DEFAULT] Handle OTP operation
1740 * @param mtd MTD device structure
1741 * @param from The offset to read/write
1742 * @param len number of bytes to read/write
1743 * @param retlen pointer to variable to store the number of read bytes
1744 * @param buf the databuffer to put/get data
1745 * @param action do given action
1746 * @param mode specify user and factory
1748 * Handle OTP operation.
1750 static int onenand_otp_walk(struct mtd_info *mtd, loff_t from, size_t len,
1751 size_t *retlen, u_char *buf,
1752 otp_op_t action, int mode)
1754 struct onenand_chip *this = mtd->priv;
1761 density = this->device_id >> ONENAND_DEVICE_DENSITY_SHIFT;
1762 if (density < ONENAND_DEVICE_DENSITY_512Mb)
1767 if (mode == MTD_OTP_FACTORY) {
1768 from += mtd->writesize * otp_pages;
1769 otp_pages = 64 - otp_pages;
1772 /* Check User/Factory boundary */
1773 if (((mtd->writesize * otp_pages) - (from + len)) < 0)
1776 while (len > 0 && otp_pages > 0) {
1777 if (!action) { /* OTP Info functions */
1778 struct otp_info *otpinfo;
1780 len -= sizeof(struct otp_info);
1784 otpinfo = (struct otp_info *) buf;
1785 otpinfo->start = from;
1786 otpinfo->length = mtd->writesize;
1787 otpinfo->locked = 0;
1789 from += mtd->writesize;
1790 buf += sizeof(struct otp_info);
1791 *retlen += sizeof(struct otp_info);
1796 ret = action(mtd, from, len, &tmp_retlen, buf);
1812 * onenand_get_fact_prot_info - [MTD Interface] Read factory OTP info
1813 * @param mtd MTD device structure
1814 * @param buf the databuffer to put/get data
1815 * @param len number of bytes to read
1817 * Read factory OTP info.
1819 static int onenand_get_fact_prot_info(struct mtd_info *mtd,
1820 struct otp_info *buf, size_t len)
1825 ret = onenand_otp_walk(mtd, 0, len, &retlen, (u_char *) buf, NULL, MTD_OTP_FACTORY);
1827 return ret ? : retlen;
1831 * onenand_read_fact_prot_reg - [MTD Interface] Read factory OTP area
1832 * @param mtd MTD device structure
1833 * @param from The offset to read
1834 * @param len number of bytes to read
1835 * @param retlen pointer to variable to store the number of read bytes
1836 * @param buf the databuffer to put/get data
1838 * Read factory OTP area.
1840 static int onenand_read_fact_prot_reg(struct mtd_info *mtd, loff_t from,
1841 size_t len, size_t *retlen, u_char *buf)
1843 return onenand_otp_walk(mtd, from, len, retlen, buf, do_otp_read, MTD_OTP_FACTORY);
1847 * onenand_get_user_prot_info - [MTD Interface] Read user OTP info
1848 * @param mtd MTD device structure
1849 * @param buf the databuffer to put/get data
1850 * @param len number of bytes to read
1852 * Read user OTP info.
1854 static int onenand_get_user_prot_info(struct mtd_info *mtd,
1855 struct otp_info *buf, size_t len)
1860 ret = onenand_otp_walk(mtd, 0, len, &retlen, (u_char *) buf, NULL, MTD_OTP_USER);
1862 return ret ? : retlen;
1866 * onenand_read_user_prot_reg - [MTD Interface] Read user OTP area
1867 * @param mtd MTD device structure
1868 * @param from The offset to read
1869 * @param len number of bytes to read
1870 * @param retlen pointer to variable to store the number of read bytes
1871 * @param buf the databuffer to put/get data
1873 * Read user OTP area.
1875 static int onenand_read_user_prot_reg(struct mtd_info *mtd, loff_t from,
1876 size_t len, size_t *retlen, u_char *buf)
1878 return onenand_otp_walk(mtd, from, len, retlen, buf, do_otp_read, MTD_OTP_USER);
1882 * onenand_write_user_prot_reg - [MTD Interface] Write user OTP area
1883 * @param mtd MTD device structure
1884 * @param from The offset to write
1885 * @param len number of bytes to write
1886 * @param retlen pointer to variable to store the number of write bytes
1887 * @param buf the databuffer to put/get data
1889 * Write user OTP area.
1891 static int onenand_write_user_prot_reg(struct mtd_info *mtd, loff_t from,
1892 size_t len, size_t *retlen, u_char *buf)
1894 return onenand_otp_walk(mtd, from, len, retlen, buf, do_otp_write, MTD_OTP_USER);
1898 * onenand_lock_user_prot_reg - [MTD Interface] Lock user OTP area
1899 * @param mtd MTD device structure
1900 * @param from The offset to lock
1901 * @param len number of bytes to unlock
1903 * Write lock mark on spare area in page 0 in OTP block
1905 static int onenand_lock_user_prot_reg(struct mtd_info *mtd, loff_t from,
1908 unsigned char oob_buf[64];
1912 memset(oob_buf, 0xff, mtd->oobsize);
1914 * Note: OTP lock operation
1915 * OTP block : 0xXXFC
1916 * 1st block : 0xXXF3 (If chip support)
1917 * Both : 0xXXF0 (If chip support)
1919 oob_buf[ONENAND_OTP_LOCK_OFFSET] = 0xFC;
1922 * Write lock mark to 8th word of sector0 of page0 of the spare0.
1923 * We write 16 bytes spare area instead of 2 bytes.
1928 ret = onenand_otp_walk(mtd, from, len, &retlen, oob_buf, do_otp_lock, MTD_OTP_USER);
1930 return ret ? : retlen;
1932 #endif /* CONFIG_MTD_ONENAND_OTP */
1935 * onenand_check_features - Check and set OneNAND features
1936 * @param mtd MTD data structure
1938 * Check and set OneNAND features
1941 static void onenand_check_features(struct mtd_info *mtd)
1943 struct onenand_chip *this = mtd->priv;
1944 unsigned int density, process;
1946 /* Lock scheme depends on density and process */
1947 density = this->device_id >> ONENAND_DEVICE_DENSITY_SHIFT;
1948 process = this->version_id >> ONENAND_VERSION_PROCESS_SHIFT;
1951 if (density >= ONENAND_DEVICE_DENSITY_1Gb) {
1952 /* A-Die has all block unlock */
1954 printk(KERN_DEBUG "Chip support all block unlock\n");
1955 this->options |= ONENAND_HAS_UNLOCK_ALL;
1958 /* Some OneNAND has continues lock scheme */
1960 printk(KERN_DEBUG "Lock scheme is Continues Lock\n");
1961 this->options |= ONENAND_HAS_CONT_LOCK;
1967 * onenand_print_device_info - Print device ID
1968 * @param device device ID
1972 static void onenand_print_device_info(int device, int version)
1974 int vcc, demuxed, ddp, density;
1976 vcc = device & ONENAND_DEVICE_VCC_MASK;
1977 demuxed = device & ONENAND_DEVICE_IS_DEMUX;
1978 ddp = device & ONENAND_DEVICE_IS_DDP;
1979 density = device >> ONENAND_DEVICE_DENSITY_SHIFT;
1980 printk(KERN_INFO "%sOneNAND%s %dMB %sV 16-bit (0x%02x)\n",
1981 demuxed ? "" : "Muxed ",
1984 vcc ? "2.65/3.3" : "1.8",
1986 printk(KERN_DEBUG "OneNAND version = 0x%04x\n", version);
1989 static const struct onenand_manufacturers onenand_manuf_ids[] = {
1990 {ONENAND_MFR_SAMSUNG, "Samsung"},
1994 * onenand_check_maf - Check manufacturer ID
1995 * @param manuf manufacturer ID
1997 * Check manufacturer ID
1999 static int onenand_check_maf(int manuf)
2001 int size = ARRAY_SIZE(onenand_manuf_ids);
2005 for (i = 0; i < size; i++)
2006 if (manuf == onenand_manuf_ids[i].id)
2010 name = onenand_manuf_ids[i].name;
2014 printk(KERN_DEBUG "OneNAND Manufacturer: %s (0x%0x)\n", name, manuf);
2020 * onenand_probe - [OneNAND Interface] Probe the OneNAND device
2021 * @param mtd MTD device structure
2023 * OneNAND detection method:
2024 * Compare the the values from command with ones from register
2026 static int onenand_probe(struct mtd_info *mtd)
2028 struct onenand_chip *this = mtd->priv;
2029 int bram_maf_id, bram_dev_id, maf_id, dev_id, ver_id;
2033 /* Save system configuration 1 */
2034 syscfg = this->read_word(this->base + ONENAND_REG_SYS_CFG1);
2035 /* Clear Sync. Burst Read mode to read BootRAM */
2036 this->write_word((syscfg & ~ONENAND_SYS_CFG1_SYNC_READ), this->base + ONENAND_REG_SYS_CFG1);
2038 /* Send the command for reading device ID from BootRAM */
2039 this->write_word(ONENAND_CMD_READID, this->base + ONENAND_BOOTRAM);
2041 /* Read manufacturer and device IDs from BootRAM */
2042 bram_maf_id = this->read_word(this->base + ONENAND_BOOTRAM + 0x0);
2043 bram_dev_id = this->read_word(this->base + ONENAND_BOOTRAM + 0x2);
2045 /* Reset OneNAND to read default register values */
2046 this->write_word(ONENAND_CMD_RESET, this->base + ONENAND_BOOTRAM);
2048 this->wait(mtd, FL_RESETING);
2050 /* Restore system configuration 1 */
2051 this->write_word(syscfg, this->base + ONENAND_REG_SYS_CFG1);
2053 /* Check manufacturer ID */
2054 if (onenand_check_maf(bram_maf_id))
2057 /* Read manufacturer and device IDs from Register */
2058 maf_id = this->read_word(this->base + ONENAND_REG_MANUFACTURER_ID);
2059 dev_id = this->read_word(this->base + ONENAND_REG_DEVICE_ID);
2060 ver_id = this->read_word(this->base + ONENAND_REG_VERSION_ID);
2062 /* Check OneNAND device */
2063 if (maf_id != bram_maf_id || dev_id != bram_dev_id)
2066 /* Flash device information */
2067 onenand_print_device_info(dev_id, ver_id);
2068 this->device_id = dev_id;
2069 this->version_id = ver_id;
2071 density = dev_id >> ONENAND_DEVICE_DENSITY_SHIFT;
2072 this->chipsize = (16 << density) << 20;
2073 /* Set density mask. it is used for DDP */
2074 if (ONENAND_IS_DDP(this))
2075 this->density_mask = (1 << (density + 6));
2077 this->density_mask = 0;
2079 /* OneNAND page size & block size */
2080 /* The data buffer size is equal to page size */
2081 mtd->writesize = this->read_word(this->base + ONENAND_REG_DATA_BUFFER_SIZE);
2082 mtd->oobsize = mtd->writesize >> 5;
2083 /* Pages per a block are always 64 in OneNAND */
2084 mtd->erasesize = mtd->writesize << 6;
2086 this->erase_shift = ffs(mtd->erasesize) - 1;
2087 this->page_shift = ffs(mtd->writesize) - 1;
2088 this->page_mask = (1 << (this->erase_shift - this->page_shift)) - 1;
2090 /* REVIST: Multichip handling */
2092 mtd->size = this->chipsize;
2094 /* Check OneNAND features */
2095 onenand_check_features(mtd);
2101 * onenand_suspend - [MTD Interface] Suspend the OneNAND flash
2102 * @param mtd MTD device structure
2104 static int onenand_suspend(struct mtd_info *mtd)
2106 return onenand_get_device(mtd, FL_PM_SUSPENDED);
2110 * onenand_resume - [MTD Interface] Resume the OneNAND flash
2111 * @param mtd MTD device structure
2113 static void onenand_resume(struct mtd_info *mtd)
2115 struct onenand_chip *this = mtd->priv;
2117 if (this->state == FL_PM_SUSPENDED)
2118 onenand_release_device(mtd);
2120 printk(KERN_ERR "resume() called for the chip which is not"
2121 "in suspended state\n");
2125 * onenand_scan - [OneNAND Interface] Scan for the OneNAND device
2126 * @param mtd MTD device structure
2127 * @param maxchips Number of chips to scan for
2129 * This fills out all the not initialized function pointers
2130 * with the defaults.
2131 * The flash ID is read and the mtd/chip structures are
2132 * filled with the appropriate values.
2134 int onenand_scan(struct mtd_info *mtd, int maxchips)
2137 struct onenand_chip *this = mtd->priv;
2139 if (!this->read_word)
2140 this->read_word = onenand_readw;
2141 if (!this->write_word)
2142 this->write_word = onenand_writew;
2145 this->command = onenand_command;
2147 onenand_setup_wait(mtd);
2149 if (!this->read_bufferram)
2150 this->read_bufferram = onenand_read_bufferram;
2151 if (!this->write_bufferram)
2152 this->write_bufferram = onenand_write_bufferram;
2154 if (!this->block_markbad)
2155 this->block_markbad = onenand_default_block_markbad;
2156 if (!this->scan_bbt)
2157 this->scan_bbt = onenand_default_bbt;
2159 if (onenand_probe(mtd))
2162 /* Set Sync. Burst Read after probing */
2163 if (this->mmcontrol) {
2164 printk(KERN_INFO "OneNAND Sync. Burst Read support\n");
2165 this->read_bufferram = onenand_sync_read_bufferram;
2168 /* Allocate buffers, if necessary */
2169 if (!this->page_buf) {
2171 len = mtd->writesize + mtd->oobsize;
2172 this->page_buf = kmalloc(len, GFP_KERNEL);
2173 if (!this->page_buf) {
2174 printk(KERN_ERR "onenand_scan(): Can't allocate page_buf\n");
2177 this->options |= ONENAND_PAGEBUF_ALLOC;
2180 this->state = FL_READY;
2181 init_waitqueue_head(&this->wq);
2182 spin_lock_init(&this->chip_lock);
2185 * Allow subpage writes up to oobsize.
2187 switch (mtd->oobsize) {
2189 this->ecclayout = &onenand_oob_64;
2190 mtd->subpage_sft = 2;
2194 this->ecclayout = &onenand_oob_32;
2195 mtd->subpage_sft = 1;
2199 printk(KERN_WARNING "No OOB scheme defined for oobsize %d\n",
2201 mtd->subpage_sft = 0;
2202 /* To prevent kernel oops */
2203 this->ecclayout = &onenand_oob_32;
2207 this->subpagesize = mtd->writesize >> mtd->subpage_sft;
2210 * The number of bytes available for a client to place data into
2211 * the out of band area
2213 this->ecclayout->oobavail = 0;
2214 for (i = 0; this->ecclayout->oobfree[i].length; i++)
2215 this->ecclayout->oobavail +=
2216 this->ecclayout->oobfree[i].length;
2218 mtd->ecclayout = this->ecclayout;
2220 /* Fill in remaining MTD driver data */
2221 mtd->type = MTD_NANDFLASH;
2222 mtd->flags = MTD_CAP_NANDFLASH;
2223 mtd->ecctype = MTD_ECC_SW;
2224 mtd->erase = onenand_erase;
2226 mtd->unpoint = NULL;
2227 mtd->read = onenand_read;
2228 mtd->write = onenand_write;
2229 mtd->read_oob = onenand_read_oob;
2230 mtd->write_oob = onenand_write_oob;
2231 #ifdef CONFIG_MTD_ONENAND_OTP
2232 mtd->get_fact_prot_info = onenand_get_fact_prot_info;
2233 mtd->read_fact_prot_reg = onenand_read_fact_prot_reg;
2234 mtd->get_user_prot_info = onenand_get_user_prot_info;
2235 mtd->read_user_prot_reg = onenand_read_user_prot_reg;
2236 mtd->write_user_prot_reg = onenand_write_user_prot_reg;
2237 mtd->lock_user_prot_reg = onenand_lock_user_prot_reg;
2239 mtd->sync = onenand_sync;
2240 mtd->lock = onenand_lock;
2241 mtd->unlock = onenand_unlock;
2242 mtd->suspend = onenand_suspend;
2243 mtd->resume = onenand_resume;
2244 mtd->block_isbad = onenand_block_isbad;
2245 mtd->block_markbad = onenand_block_markbad;
2246 mtd->owner = THIS_MODULE;
2248 /* Unlock whole block */
2249 onenand_unlock_all(mtd);
2251 return this->scan_bbt(mtd);
2255 * onenand_release - [OneNAND Interface] Free resources held by the OneNAND device
2256 * @param mtd MTD device structure
2258 void onenand_release(struct mtd_info *mtd)
2260 struct onenand_chip *this = mtd->priv;
2262 #ifdef CONFIG_MTD_PARTITIONS
2263 /* Deregister partitions */
2264 del_mtd_partitions (mtd);
2266 /* Deregister the device */
2267 del_mtd_device (mtd);
2269 /* Free bad block table memory, if allocated */
2271 struct bbm_info *bbm = this->bbm;
2275 /* Buffer allocated by onenand_scan */
2276 if (this->options & ONENAND_PAGEBUF_ALLOC)
2277 kfree(this->page_buf);
2280 EXPORT_SYMBOL_GPL(onenand_scan);
2281 EXPORT_SYMBOL_GPL(onenand_release);
2283 MODULE_LICENSE("GPL");
2284 MODULE_AUTHOR("Kyungmin Park <kyungmin.park@samsung.com>");
2285 MODULE_DESCRIPTION("Generic OneNAND flash driver code");