2 * linux/drivers/mtd/onenand/onenand_base.c
4 * Copyright (C) 2005 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/jiffies.h>
17 #include <linux/mtd/mtd.h>
18 #include <linux/mtd/onenand.h>
19 #include <linux/mtd/partitions.h>
24 * onenand_oob_64 - oob info for large (2KB) page
26 static struct nand_oobinfo onenand_oob_64 = {
27 .useecc = MTD_NANDECC_AUTOPLACE,
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_oobinfo onenand_oob_32 = {
45 .useecc = MTD_NANDECC_AUTOPLACE,
51 .oobfree = { {2, 3}, {14, 2}, {18, 3}, {30, 2} }
54 static const unsigned char ffchars[] = {
55 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
56 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 16 */
57 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
58 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 32 */
59 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
60 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 48 */
61 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
62 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 64 */
66 * onenand_readw - [OneNAND Interface] Read OneNAND register
67 * @param addr address to read
69 * Read OneNAND register
71 static unsigned short onenand_readw(void __iomem *addr)
77 * onenand_writew - [OneNAND Interface] Write OneNAND register with value
78 * @param value value to write
79 * @param addr address to write
81 * Write OneNAND register with value
83 static void onenand_writew(unsigned short value, void __iomem *addr)
89 * onenand_block_address - [DEFAULT] Get block address
90 * @param this onenand chip data structure
91 * @param block the block
92 * @return translated block address if DDP, otherwise same
94 * Setup Start Address 1 Register (F100h)
96 static int onenand_block_address(struct onenand_chip *this, int block)
98 if (this->device_id & ONENAND_DEVICE_IS_DDP) {
99 /* Device Flash Core select, NAND Flash Block Address */
102 if (block & this->density_mask)
105 return (dfs << ONENAND_DDP_SHIFT) |
106 (block & (this->density_mask - 1));
113 * onenand_bufferram_address - [DEFAULT] Get bufferram address
114 * @param this onenand chip data structure
115 * @param block the block
116 * @return set DBS value if DDP, otherwise 0
118 * Setup Start Address 2 Register (F101h) for DDP
120 static int onenand_bufferram_address(struct onenand_chip *this, int block)
122 if (this->device_id & ONENAND_DEVICE_IS_DDP) {
123 /* Device BufferRAM Select */
126 if (block & this->density_mask)
129 return (dbs << ONENAND_DDP_SHIFT);
136 * onenand_page_address - [DEFAULT] Get page address
137 * @param page the page address
138 * @param sector the sector address
139 * @return combined page and sector address
141 * Setup Start Address 8 Register (F107h)
143 static int onenand_page_address(int page, int sector)
145 /* Flash Page Address, Flash Sector Address */
148 fpa = page & ONENAND_FPA_MASK;
149 fsa = sector & ONENAND_FSA_MASK;
151 return ((fpa << ONENAND_FPA_SHIFT) | fsa);
155 * onenand_buffer_address - [DEFAULT] Get buffer address
156 * @param dataram1 DataRAM index
157 * @param sectors the sector address
158 * @param count the number of sectors
159 * @return the start buffer value
161 * Setup Start Buffer Register (F200h)
163 static int onenand_buffer_address(int dataram1, int sectors, int count)
167 /* BufferRAM Sector Address */
168 bsa = sectors & ONENAND_BSA_MASK;
171 bsa |= ONENAND_BSA_DATARAM1; /* DataRAM1 */
173 bsa |= ONENAND_BSA_DATARAM0; /* DataRAM0 */
175 /* BufferRAM Sector Count */
176 bsc = count & ONENAND_BSC_MASK;
178 return ((bsa << ONENAND_BSA_SHIFT) | bsc);
182 * onenand_command - [DEFAULT] Send command to OneNAND device
183 * @param mtd MTD device structure
184 * @param cmd the command to be sent
185 * @param addr offset to read from or write to
186 * @param len number of bytes to read or write
188 * Send command to OneNAND device. This function is used for middle/large page
189 * devices (1KB/2KB Bytes per page)
191 static int onenand_command(struct mtd_info *mtd, int cmd, loff_t addr, size_t len)
193 struct onenand_chip *this = mtd->priv;
194 int value, readcmd = 0;
196 /* Now we use page size operation */
197 int sectors = 4, count = 4;
199 /* Address translation */
201 case ONENAND_CMD_UNLOCK:
202 case ONENAND_CMD_LOCK:
203 case ONENAND_CMD_LOCK_TIGHT:
208 case ONENAND_CMD_ERASE:
209 case ONENAND_CMD_BUFFERRAM:
210 block = (int) (addr >> this->erase_shift);
215 block = (int) (addr >> this->erase_shift);
216 page = (int) (addr >> this->page_shift);
217 page &= this->page_mask;
221 /* NOTE: The setting order of the registers is very important! */
222 if (cmd == ONENAND_CMD_BUFFERRAM) {
223 /* Select DataRAM for DDP */
224 value = onenand_bufferram_address(this, block);
225 this->write_word(value, this->base + ONENAND_REG_START_ADDRESS2);
227 /* Switch to the next data buffer */
228 ONENAND_SET_NEXT_BUFFERRAM(this);
234 /* Write 'DFS, FBA' of Flash */
235 value = onenand_block_address(this, block);
236 this->write_word(value, this->base + ONENAND_REG_START_ADDRESS1);
243 case ONENAND_CMD_READ:
244 case ONENAND_CMD_READOOB:
245 dataram = ONENAND_SET_NEXT_BUFFERRAM(this);
250 dataram = ONENAND_CURRENT_BUFFERRAM(this);
254 /* Write 'FPA, FSA' of Flash */
255 value = onenand_page_address(page, sectors);
256 this->write_word(value, this->base + ONENAND_REG_START_ADDRESS8);
258 /* Write 'BSA, BSC' of DataRAM */
259 value = onenand_buffer_address(dataram, sectors, count);
260 this->write_word(value, this->base + ONENAND_REG_START_BUFFER);
263 /* Select DataRAM for DDP */
264 value = onenand_bufferram_address(this, block);
265 this->write_word(value, this->base + ONENAND_REG_START_ADDRESS2);
269 /* Interrupt clear */
270 this->write_word(ONENAND_INT_CLEAR, this->base + ONENAND_REG_INTERRUPT);
273 this->write_word(cmd, this->base + ONENAND_REG_COMMAND);
279 * onenand_wait - [DEFAULT] wait until the command is done
280 * @param mtd MTD device structure
281 * @param state state to select the max. timeout value
283 * Wait for command done. This applies to all OneNAND command
284 * Read can take up to 30us, erase up to 2ms and program up to 350us
285 * according to general OneNAND specs
287 static int onenand_wait(struct mtd_info *mtd, int state)
289 struct onenand_chip * this = mtd->priv;
290 unsigned long timeout;
291 unsigned int flags = ONENAND_INT_MASTER;
292 unsigned int interrupt = 0;
293 unsigned int ctrl, ecc;
295 /* The 20 msec is enough */
296 timeout = jiffies + msecs_to_jiffies(20);
297 while (time_before(jiffies, timeout)) {
298 interrupt = this->read_word(this->base + ONENAND_REG_INTERRUPT);
300 if (interrupt & flags)
303 if (state != FL_READING)
305 touch_softlockup_watchdog();
307 /* To get correct interrupt status in timeout case */
308 interrupt = this->read_word(this->base + ONENAND_REG_INTERRUPT);
310 ctrl = this->read_word(this->base + ONENAND_REG_CTRL_STATUS);
312 if (ctrl & ONENAND_CTRL_ERROR) {
313 /* It maybe occur at initial bad block */
314 DEBUG(MTD_DEBUG_LEVEL0, "onenand_wait: controller error = 0x%04x\n", ctrl);
315 /* Clear other interrupt bits for preventing ECC error */
316 interrupt &= ONENAND_INT_MASTER;
319 if (ctrl & ONENAND_CTRL_LOCK) {
320 DEBUG(MTD_DEBUG_LEVEL0, "onenand_wait: it's locked error = 0x%04x\n", ctrl);
324 if (interrupt & ONENAND_INT_READ) {
325 ecc = this->read_word(this->base + ONENAND_REG_ECC_STATUS);
326 if (ecc & ONENAND_ECC_2BIT_ALL) {
327 DEBUG(MTD_DEBUG_LEVEL0, "onenand_wait: ECC error = 0x%04x\n", ecc);
336 * onenand_bufferram_offset - [DEFAULT] BufferRAM offset
337 * @param mtd MTD data structure
338 * @param area BufferRAM area
339 * @return offset given area
341 * Return BufferRAM offset given area
343 static inline int onenand_bufferram_offset(struct mtd_info *mtd, int area)
345 struct onenand_chip *this = mtd->priv;
347 if (ONENAND_CURRENT_BUFFERRAM(this)) {
348 if (area == ONENAND_DATARAM)
349 return mtd->oobblock;
350 if (area == ONENAND_SPARERAM)
358 * onenand_read_bufferram - [OneNAND Interface] Read the bufferram area
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
367 static int onenand_read_bufferram(struct mtd_info *mtd, int area,
368 unsigned char *buffer, int offset, size_t count)
370 struct onenand_chip *this = mtd->priv;
371 void __iomem *bufferram;
373 bufferram = this->base + area;
375 bufferram += onenand_bufferram_offset(mtd, area);
377 if (ONENAND_CHECK_BYTE_ACCESS(count)) {
380 /* Align with word(16-bit) size */
383 /* Read word and save byte */
384 word = this->read_word(bufferram + offset + count);
385 buffer[count] = (word & 0xff);
388 memcpy(buffer, bufferram + offset, count);
394 * onenand_sync_read_bufferram - [OneNAND Interface] Read the bufferram area with Sync. Burst mode
395 * @param mtd MTD data structure
396 * @param area BufferRAM area
397 * @param buffer the databuffer to put/get data
398 * @param offset offset to read from or write to
399 * @param count number of bytes to read/write
401 * Read the BufferRAM area with Sync. Burst Mode
403 static int onenand_sync_read_bufferram(struct mtd_info *mtd, int area,
404 unsigned char *buffer, int offset, size_t count)
406 struct onenand_chip *this = mtd->priv;
407 void __iomem *bufferram;
409 bufferram = this->base + area;
411 bufferram += onenand_bufferram_offset(mtd, area);
413 this->mmcontrol(mtd, ONENAND_SYS_CFG1_SYNC_READ);
415 if (ONENAND_CHECK_BYTE_ACCESS(count)) {
418 /* Align with word(16-bit) size */
421 /* Read word and save byte */
422 word = this->read_word(bufferram + offset + count);
423 buffer[count] = (word & 0xff);
426 memcpy(buffer, bufferram + offset, count);
428 this->mmcontrol(mtd, 0);
434 * onenand_write_bufferram - [OneNAND Interface] Write the bufferram area
435 * @param mtd MTD data structure
436 * @param area BufferRAM area
437 * @param buffer the databuffer to put/get data
438 * @param offset offset to read from or write to
439 * @param count number of bytes to read/write
441 * Write the BufferRAM area
443 static int onenand_write_bufferram(struct mtd_info *mtd, int area,
444 const unsigned char *buffer, int offset, size_t count)
446 struct onenand_chip *this = mtd->priv;
447 void __iomem *bufferram;
449 bufferram = this->base + area;
451 bufferram += onenand_bufferram_offset(mtd, area);
453 if (ONENAND_CHECK_BYTE_ACCESS(count)) {
457 /* Align with word(16-bit) size */
460 /* Calculate byte access offset */
461 byte_offset = offset + count;
463 /* Read word and save byte */
464 word = this->read_word(bufferram + byte_offset);
465 word = (word & ~0xff) | buffer[count];
466 this->write_word(word, bufferram + byte_offset);
469 memcpy(bufferram + offset, buffer, count);
475 * onenand_check_bufferram - [GENERIC] Check BufferRAM information
476 * @param mtd MTD data structure
477 * @param addr address to check
478 * @return 1 if there are valid data, otherwise 0
480 * Check bufferram if there is data we required
482 static int onenand_check_bufferram(struct mtd_info *mtd, loff_t addr)
484 struct onenand_chip *this = mtd->priv;
488 block = (int) (addr >> this->erase_shift);
489 page = (int) (addr >> this->page_shift);
490 page &= this->page_mask;
492 i = ONENAND_CURRENT_BUFFERRAM(this);
494 /* Is there valid data? */
495 if (this->bufferram[i].block == block &&
496 this->bufferram[i].page == page &&
497 this->bufferram[i].valid)
504 * onenand_update_bufferram - [GENERIC] Update BufferRAM information
505 * @param mtd MTD data structure
506 * @param addr address to update
507 * @param valid valid flag
509 * Update BufferRAM information
511 static int onenand_update_bufferram(struct mtd_info *mtd, loff_t addr,
514 struct onenand_chip *this = mtd->priv;
518 block = (int) (addr >> this->erase_shift);
519 page = (int) (addr >> this->page_shift);
520 page &= this->page_mask;
522 /* Invalidate BufferRAM */
523 for (i = 0; i < MAX_BUFFERRAM; i++) {
524 if (this->bufferram[i].block == block &&
525 this->bufferram[i].page == page)
526 this->bufferram[i].valid = 0;
529 /* Update BufferRAM */
530 i = ONENAND_CURRENT_BUFFERRAM(this);
531 this->bufferram[i].block = block;
532 this->bufferram[i].page = page;
533 this->bufferram[i].valid = valid;
539 * onenand_get_device - [GENERIC] Get chip for selected access
540 * @param mtd MTD device structure
541 * @param new_state the state which is requested
543 * Get the device and lock it for exclusive access
545 static int onenand_get_device(struct mtd_info *mtd, int new_state)
547 struct onenand_chip *this = mtd->priv;
548 DECLARE_WAITQUEUE(wait, current);
551 * Grab the lock and see if the device is available
554 spin_lock(&this->chip_lock);
555 if (this->state == FL_READY) {
556 this->state = new_state;
557 spin_unlock(&this->chip_lock);
560 if (new_state == FL_PM_SUSPENDED) {
561 spin_unlock(&this->chip_lock);
562 return (this->state == FL_PM_SUSPENDED) ? 0 : -EAGAIN;
564 set_current_state(TASK_UNINTERRUPTIBLE);
565 add_wait_queue(&this->wq, &wait);
566 spin_unlock(&this->chip_lock);
568 remove_wait_queue(&this->wq, &wait);
575 * onenand_release_device - [GENERIC] release chip
576 * @param mtd MTD device structure
578 * Deselect, release chip lock and wake up anyone waiting on the device
580 static void onenand_release_device(struct mtd_info *mtd)
582 struct onenand_chip *this = mtd->priv;
584 /* Release the chip */
585 spin_lock(&this->chip_lock);
586 this->state = FL_READY;
588 spin_unlock(&this->chip_lock);
592 * onenand_read_ecc - [MTD Interface] Read data with ECC
593 * @param mtd MTD device structure
594 * @param from offset to read from
595 * @param len number of bytes to read
596 * @param retlen pointer to variable to store the number of read bytes
597 * @param buf the databuffer to put data
598 * @param oob_buf filesystem supplied oob data buffer
599 * @param oobsel oob selection structure
601 * OneNAND read with ECC
603 static int onenand_read_ecc(struct mtd_info *mtd, loff_t from, size_t len,
604 size_t *retlen, u_char *buf,
605 u_char *oob_buf, struct nand_oobinfo *oobsel)
607 struct onenand_chip *this = mtd->priv;
608 int read = 0, column;
612 DEBUG(MTD_DEBUG_LEVEL3, "onenand_read_ecc: from = 0x%08x, len = %i\n", (unsigned int) from, (int) len);
614 /* Do not allow reads past end of device */
615 if ((from + len) > mtd->size) {
616 DEBUG(MTD_DEBUG_LEVEL0, "onenand_read_ecc: Attempt read beyond end of device\n");
621 /* Grab the lock and see if the device is available */
622 onenand_get_device(mtd, FL_READING);
624 /* TODO handling oob */
627 thislen = min_t(int, mtd->oobblock, len - read);
629 column = from & (mtd->oobblock - 1);
630 if (column + thislen > mtd->oobblock)
631 thislen = mtd->oobblock - column;
633 if (!onenand_check_bufferram(mtd, from)) {
634 this->command(mtd, ONENAND_CMD_READ, from, mtd->oobblock);
636 ret = this->wait(mtd, FL_READING);
637 /* First copy data and check return value for ECC handling */
638 onenand_update_bufferram(mtd, from, 1);
641 this->read_bufferram(mtd, ONENAND_DATARAM, buf, column, thislen);
649 DEBUG(MTD_DEBUG_LEVEL0, "onenand_read_ecc: read failed = %d\n", ret);
658 /* Deselect and wake up anyone waiting on the device */
659 onenand_release_device(mtd);
662 * Return success, if no ECC failures, else -EBADMSG
663 * fs driver will take care of that, because
664 * retlen == desired len and result == -EBADMSG
671 * onenand_read - [MTD Interface] MTD compability function for onenand_read_ecc
672 * @param mtd MTD device structure
673 * @param from offset to read from
674 * @param len number of bytes to read
675 * @param retlen pointer to variable to store the number of read bytes
676 * @param buf the databuffer to put data
678 * This function simply calls onenand_read_ecc with oob buffer and oobsel = NULL
680 static int onenand_read(struct mtd_info *mtd, loff_t from, size_t len,
681 size_t *retlen, u_char *buf)
683 return onenand_read_ecc(mtd, from, len, retlen, buf, NULL, NULL);
687 * onenand_read_oob - [MTD Interface] OneNAND read out-of-band
688 * @param mtd MTD device structure
689 * @param from offset to read from
690 * @param len number of bytes to read
691 * @param retlen pointer to variable to store the number of read bytes
692 * @param buf the databuffer to put data
694 * OneNAND read out-of-band data from the spare area
696 static int onenand_read_oob(struct mtd_info *mtd, loff_t from, size_t len,
697 size_t *retlen, u_char *buf)
699 struct onenand_chip *this = mtd->priv;
700 int read = 0, thislen, column;
703 DEBUG(MTD_DEBUG_LEVEL3, "onenand_read_oob: from = 0x%08x, len = %i\n", (unsigned int) from, (int) len);
705 /* Initialize return length value */
708 /* Do not allow reads past end of device */
709 if (unlikely((from + len) > mtd->size)) {
710 DEBUG(MTD_DEBUG_LEVEL0, "onenand_read_oob: Attempt read beyond end of device\n");
714 /* Grab the lock and see if the device is available */
715 onenand_get_device(mtd, FL_READING);
717 column = from & (mtd->oobsize - 1);
720 thislen = mtd->oobsize - column;
721 thislen = min_t(int, thislen, len);
723 this->command(mtd, ONENAND_CMD_READOOB, from, mtd->oobsize);
725 onenand_update_bufferram(mtd, from, 0);
727 ret = this->wait(mtd, FL_READING);
728 /* First copy data and check return value for ECC handling */
730 this->read_bufferram(mtd, ONENAND_SPARERAM, buf, column, thislen);
738 DEBUG(MTD_DEBUG_LEVEL0, "onenand_read_oob: read failed = %d\n", ret);
747 from += mtd->oobblock;
753 /* Deselect and wake up anyone waiting on the device */
754 onenand_release_device(mtd);
760 #ifdef CONFIG_MTD_ONENAND_VERIFY_WRITE
762 * onenand_verify_page - [GENERIC] verify the chip contents after a write
763 * @param mtd MTD device structure
764 * @param buf the databuffer to verify
766 * Check DataRAM area directly
768 static int onenand_verify_page(struct mtd_info *mtd, u_char *buf, loff_t addr)
770 struct onenand_chip *this = mtd->priv;
771 void __iomem *dataram0, *dataram1;
774 this->command(mtd, ONENAND_CMD_READ, addr, mtd->oobblock);
776 ret = this->wait(mtd, FL_READING);
780 onenand_update_bufferram(mtd, addr, 1);
782 /* Check, if the two dataram areas are same */
783 dataram0 = this->base + ONENAND_DATARAM;
784 dataram1 = dataram0 + mtd->oobblock;
786 if (memcmp(dataram0, dataram1, mtd->oobblock))
792 #define onenand_verify_page(...) (0)
795 #define NOTALIGNED(x) ((x & (mtd->oobblock - 1)) != 0)
798 * onenand_write_ecc - [MTD Interface] OneNAND write with ECC
799 * @param mtd MTD device structure
800 * @param to offset to write to
801 * @param len number of bytes to write
802 * @param retlen pointer to variable to store the number of written bytes
803 * @param buf the data to write
804 * @param eccbuf filesystem supplied oob data buffer
805 * @param oobsel oob selection structure
807 * OneNAND write with ECC
809 static int onenand_write_ecc(struct mtd_info *mtd, loff_t to, size_t len,
810 size_t *retlen, const u_char *buf,
811 u_char *eccbuf, struct nand_oobinfo *oobsel)
813 struct onenand_chip *this = mtd->priv;
817 DEBUG(MTD_DEBUG_LEVEL3, "onenand_write_ecc: to = 0x%08x, len = %i\n", (unsigned int) to, (int) len);
819 /* Initialize retlen, in case of early exit */
822 /* Do not allow writes past end of device */
823 if (unlikely((to + len) > mtd->size)) {
824 DEBUG(MTD_DEBUG_LEVEL0, "onenand_write_ecc: Attempt write to past end of device\n");
828 /* Reject writes, which are not page aligned */
829 if (unlikely(NOTALIGNED(to)) || unlikely(NOTALIGNED(len))) {
830 DEBUG(MTD_DEBUG_LEVEL0, "onenand_write_ecc: Attempt to write not page aligned data\n");
834 /* Grab the lock and see if the device is available */
835 onenand_get_device(mtd, FL_WRITING);
837 /* Loop until all data write */
838 while (written < len) {
839 int thislen = min_t(int, mtd->oobblock, len - written);
841 this->command(mtd, ONENAND_CMD_BUFFERRAM, to, mtd->oobblock);
843 this->write_bufferram(mtd, ONENAND_DATARAM, buf, 0, thislen);
844 this->write_bufferram(mtd, ONENAND_SPARERAM, ffchars, 0, mtd->oobsize);
846 this->command(mtd, ONENAND_CMD_PROG, to, mtd->oobblock);
848 onenand_update_bufferram(mtd, to, 1);
850 ret = this->wait(mtd, FL_WRITING);
852 DEBUG(MTD_DEBUG_LEVEL0, "onenand_write_ecc: write filaed %d\n", ret);
858 /* Only check verify write turn on */
859 ret = onenand_verify_page(mtd, (u_char *) buf, to);
861 DEBUG(MTD_DEBUG_LEVEL0, "onenand_write_ecc: verify failed %d\n", ret);
873 /* Deselect and wake up anyone waiting on the device */
874 onenand_release_device(mtd);
882 * onenand_write - [MTD Interface] compability function for onenand_write_ecc
883 * @param mtd MTD device structure
884 * @param to offset to write to
885 * @param len number of bytes to write
886 * @param retlen pointer to variable to store the number of written bytes
887 * @param buf the data to write
889 * This function simply calls onenand_write_ecc
890 * with oob buffer and oobsel = NULL
892 static int onenand_write(struct mtd_info *mtd, loff_t to, size_t len,
893 size_t *retlen, const u_char *buf)
895 return onenand_write_ecc(mtd, to, len, retlen, buf, NULL, NULL);
899 * onenand_write_oob - [MTD Interface] OneNAND write out-of-band
900 * @param mtd MTD device structure
901 * @param to offset to write to
902 * @param len number of bytes to write
903 * @param retlen pointer to variable to store the number of written bytes
904 * @param buf the data to write
906 * OneNAND write out-of-band
908 static int onenand_write_oob(struct mtd_info *mtd, loff_t to, size_t len,
909 size_t *retlen, const u_char *buf)
911 struct onenand_chip *this = mtd->priv;
915 DEBUG(MTD_DEBUG_LEVEL3, "onenand_write_oob: to = 0x%08x, len = %i\n", (unsigned int) to, (int) len);
917 /* Initialize retlen, in case of early exit */
920 /* Do not allow writes past end of device */
921 if (unlikely((to + len) > mtd->size)) {
922 DEBUG(MTD_DEBUG_LEVEL0, "onenand_write_oob: Attempt write to past end of device\n");
926 /* Grab the lock and see if the device is available */
927 onenand_get_device(mtd, FL_WRITING);
929 /* Loop until all data write */
930 while (written < len) {
931 int thislen = min_t(int, mtd->oobsize, len - written);
933 column = to & (mtd->oobsize - 1);
935 this->command(mtd, ONENAND_CMD_BUFFERRAM, to, mtd->oobsize);
937 this->write_bufferram(mtd, ONENAND_SPARERAM, ffchars, 0, mtd->oobsize);
938 this->write_bufferram(mtd, ONENAND_SPARERAM, buf, column, thislen);
940 this->command(mtd, ONENAND_CMD_PROGOOB, to, mtd->oobsize);
942 onenand_update_bufferram(mtd, to, 0);
944 status = this->wait(mtd, FL_WRITING);
958 /* Deselect and wake up anyone waiting on the device */
959 onenand_release_device(mtd);
967 * onenand_writev_ecc - [MTD Interface] write with iovec with ecc
968 * @param mtd MTD device structure
969 * @param vecs the iovectors to write
970 * @param count number of vectors
971 * @param to offset to write to
972 * @param retlen pointer to variable to store the number of written bytes
973 * @param eccbuf filesystem supplied oob data buffer
974 * @param oobsel oob selection structure
976 * OneNAND write with iovec with ecc
978 static int onenand_writev_ecc(struct mtd_info *mtd, const struct kvec *vecs,
979 unsigned long count, loff_t to, size_t *retlen,
980 u_char *eccbuf, struct nand_oobinfo *oobsel)
982 struct onenand_chip *this = mtd->priv;
984 size_t total_len, len;
988 /* Preset written len for early exit */
991 /* Calculate total length of data */
993 for (i = 0; i < count; i++)
994 total_len += vecs[i].iov_len;
996 DEBUG(MTD_DEBUG_LEVEL3, "onenand_writev_ecc: to = 0x%08x, len = %i, count = %ld\n", (unsigned int) to, (unsigned int) total_len, count);
998 /* Do not allow write past end of the device */
999 if (unlikely((to + total_len) > mtd->size)) {
1000 DEBUG(MTD_DEBUG_LEVEL0, "onenand_writev_ecc: Attempted write past end of device\n");
1004 /* Reject writes, which are not page aligned */
1005 if (unlikely(NOTALIGNED(to)) || unlikely(NOTALIGNED(total_len))) {
1006 DEBUG(MTD_DEBUG_LEVEL0, "onenand_writev_ecc: Attempt to write not page aligned data\n");
1010 /* Grab the lock and see if the device is available */
1011 onenand_get_device(mtd, FL_WRITING);
1013 /* TODO handling oob */
1015 /* Loop until all keve's data has been written */
1018 pbuf = this->page_buf;
1020 * If the given tuple is >= pagesize then
1021 * write it out from the iov
1023 if ((vecs->iov_len - len) >= mtd->oobblock) {
1024 pbuf = vecs->iov_base + len;
1026 len += mtd->oobblock;
1028 /* Check, if we have to switch to the next tuple */
1029 if (len >= (int) vecs->iov_len) {
1035 int cnt = 0, thislen;
1036 while (cnt < mtd->oobblock) {
1037 thislen = min_t(int, mtd->oobblock - cnt, vecs->iov_len - len);
1038 memcpy(this->page_buf + cnt, vecs->iov_base + len, thislen);
1042 /* Check, if we have to switch to the next tuple */
1043 if (len >= (int) vecs->iov_len) {
1051 this->command(mtd, ONENAND_CMD_BUFFERRAM, to, mtd->oobblock);
1053 this->write_bufferram(mtd, ONENAND_DATARAM, pbuf, 0, mtd->oobblock);
1054 this->write_bufferram(mtd, ONENAND_SPARERAM, ffchars, 0, mtd->oobsize);
1056 this->command(mtd, ONENAND_CMD_PROG, to, mtd->oobblock);
1058 onenand_update_bufferram(mtd, to, 1);
1060 ret = this->wait(mtd, FL_WRITING);
1062 DEBUG(MTD_DEBUG_LEVEL0, "onenand_writev_ecc: write failed %d\n", ret);
1067 /* Only check verify write turn on */
1068 ret = onenand_verify_page(mtd, (u_char *) pbuf, to);
1070 DEBUG(MTD_DEBUG_LEVEL0, "onenand_writev_ecc: verify failed %d\n", ret);
1074 written += mtd->oobblock;
1076 to += mtd->oobblock;
1080 /* Deselect and wakt up anyone waiting on the device */
1081 onenand_release_device(mtd);
1089 * onenand_writev - [MTD Interface] compabilty function for onenand_writev_ecc
1090 * @param mtd MTD device structure
1091 * @param vecs the iovectors to write
1092 * @param count number of vectors
1093 * @param to offset to write to
1094 * @param retlen pointer to variable to store the number of written bytes
1096 * OneNAND write with kvec. This just calls the ecc function
1098 static int onenand_writev(struct mtd_info *mtd, const struct kvec *vecs,
1099 unsigned long count, loff_t to, size_t *retlen)
1101 return onenand_writev_ecc(mtd, vecs, count, to, retlen, NULL, NULL);
1105 * onenand_block_checkbad - [GENERIC] Check if a block is marked bad
1106 * @param mtd MTD device structure
1107 * @param ofs offset from device start
1108 * @param getchip 0, if the chip is already selected
1109 * @param allowbbt 1, if its allowed to access the bbt area
1111 * Check, if the block is bad. Either by reading the bad block table or
1112 * calling of the scan function.
1114 static int onenand_block_checkbad(struct mtd_info *mtd, loff_t ofs, int getchip, int allowbbt)
1116 struct onenand_chip *this = mtd->priv;
1117 struct bbm_info *bbm = this->bbm;
1119 /* Return info from the table */
1120 return bbm->isbad_bbt(mtd, ofs, allowbbt);
1124 * onenand_erase - [MTD Interface] erase block(s)
1125 * @param mtd MTD device structure
1126 * @param instr erase instruction
1128 * Erase one ore more blocks
1130 static int onenand_erase(struct mtd_info *mtd, struct erase_info *instr)
1132 struct onenand_chip *this = mtd->priv;
1133 unsigned int block_size;
1138 DEBUG(MTD_DEBUG_LEVEL3, "onenand_erase: start = 0x%08x, len = %i\n", (unsigned int) instr->addr, (unsigned int) instr->len);
1140 block_size = (1 << this->erase_shift);
1142 /* Start address must align on block boundary */
1143 if (unlikely(instr->addr & (block_size - 1))) {
1144 DEBUG(MTD_DEBUG_LEVEL0, "onenand_erase: Unaligned address\n");
1148 /* Length must align on block boundary */
1149 if (unlikely(instr->len & (block_size - 1))) {
1150 DEBUG(MTD_DEBUG_LEVEL0, "onenand_erase: Length not block aligned\n");
1154 /* Do not allow erase past end of device */
1155 if (unlikely((instr->len + instr->addr) > mtd->size)) {
1156 DEBUG(MTD_DEBUG_LEVEL0, "onenand_erase: Erase past end of device\n");
1160 instr->fail_addr = 0xffffffff;
1162 /* Grab the lock and see if the device is available */
1163 onenand_get_device(mtd, FL_ERASING);
1165 /* Loop throught the pages */
1169 instr->state = MTD_ERASING;
1173 /* Check if we have a bad block, we do not erase bad blocks */
1174 if (onenand_block_checkbad(mtd, addr, 0, 0)) {
1175 printk (KERN_WARNING "onenand_erase: attempt to erase a bad block at addr 0x%08x\n", (unsigned int) addr);
1176 instr->state = MTD_ERASE_FAILED;
1180 this->command(mtd, ONENAND_CMD_ERASE, addr, block_size);
1182 ret = this->wait(mtd, FL_ERASING);
1183 /* Check, if it is write protected */
1186 DEBUG(MTD_DEBUG_LEVEL0, "onenand_erase: Device is write protected!!!\n");
1188 DEBUG(MTD_DEBUG_LEVEL0, "onenand_erase: Failed erase, block %d\n", (unsigned) (addr >> this->erase_shift));
1189 instr->state = MTD_ERASE_FAILED;
1190 instr->fail_addr = addr;
1198 instr->state = MTD_ERASE_DONE;
1202 ret = instr->state == MTD_ERASE_DONE ? 0 : -EIO;
1203 /* Do call back function */
1205 mtd_erase_callback(instr);
1207 /* Deselect and wake up anyone waiting on the device */
1208 onenand_release_device(mtd);
1214 * onenand_sync - [MTD Interface] sync
1215 * @param mtd MTD device structure
1217 * Sync is actually a wait for chip ready function
1219 static void onenand_sync(struct mtd_info *mtd)
1221 DEBUG(MTD_DEBUG_LEVEL3, "onenand_sync: called\n");
1223 /* Grab the lock and see if the device is available */
1224 onenand_get_device(mtd, FL_SYNCING);
1226 /* Release it and go back */
1227 onenand_release_device(mtd);
1232 * onenand_block_isbad - [MTD Interface] Check whether the block at the given offset is bad
1233 * @param mtd MTD device structure
1234 * @param ofs offset relative to mtd start
1236 * Check whether the block is bad
1238 static int onenand_block_isbad(struct mtd_info *mtd, loff_t ofs)
1240 /* Check for invalid offset */
1241 if (ofs > mtd->size)
1244 return onenand_block_checkbad(mtd, ofs, 1, 0);
1248 * onenand_default_block_markbad - [DEFAULT] mark a block bad
1249 * @param mtd MTD device structure
1250 * @param ofs offset from device start
1252 * This is the default implementation, which can be overridden by
1253 * a hardware specific driver.
1255 static int onenand_default_block_markbad(struct mtd_info *mtd, loff_t ofs)
1257 struct onenand_chip *this = mtd->priv;
1258 struct bbm_info *bbm = this->bbm;
1259 u_char buf[2] = {0, 0};
1263 /* Get block number */
1264 block = ((int) ofs) >> bbm->bbt_erase_shift;
1266 bbm->bbt[block >> 2] |= 0x01 << ((block & 0x03) << 1);
1268 /* We write two bytes, so we dont have to mess with 16 bit access */
1269 ofs += mtd->oobsize + (bbm->badblockpos & ~0x01);
1270 return mtd->write_oob(mtd, ofs , 2, &retlen, buf);
1274 * onenand_block_markbad - [MTD Interface] Mark the block at the given offset as bad
1275 * @param mtd MTD device structure
1276 * @param ofs offset relative to mtd start
1278 * Mark the block as bad
1280 static int onenand_block_markbad(struct mtd_info *mtd, loff_t ofs)
1282 struct onenand_chip *this = mtd->priv;
1285 ret = onenand_block_isbad(mtd, ofs);
1287 /* If it was bad already, return success and do nothing */
1293 return this->block_markbad(mtd, ofs);
1297 * onenand_unlock - [MTD Interface] Unlock block(s)
1298 * @param mtd MTD device structure
1299 * @param ofs offset relative to mtd start
1300 * @param len number of bytes to unlock
1302 * Unlock one or more blocks
1304 static int onenand_unlock(struct mtd_info *mtd, loff_t ofs, size_t len)
1306 struct onenand_chip *this = mtd->priv;
1307 int start, end, block, value, status;
1309 start = ofs >> this->erase_shift;
1310 end = len >> this->erase_shift;
1312 /* Continuous lock scheme */
1313 if (this->options & ONENAND_CONT_LOCK) {
1314 /* Set start block address */
1315 this->write_word(start, this->base + ONENAND_REG_START_BLOCK_ADDRESS);
1316 /* Set end block address */
1317 this->write_word(end - 1, this->base + ONENAND_REG_END_BLOCK_ADDRESS);
1318 /* Write unlock command */
1319 this->command(mtd, ONENAND_CMD_UNLOCK, 0, 0);
1321 /* There's no return value */
1322 this->wait(mtd, FL_UNLOCKING);
1325 while (this->read_word(this->base + ONENAND_REG_CTRL_STATUS)
1326 & ONENAND_CTRL_ONGO)
1329 /* Check lock status */
1330 status = this->read_word(this->base + ONENAND_REG_WP_STATUS);
1331 if (!(status & ONENAND_WP_US))
1332 printk(KERN_ERR "wp status = 0x%x\n", status);
1337 /* Block lock scheme */
1338 for (block = start; block < end; block++) {
1339 /* Set block address */
1340 value = onenand_block_address(this, block);
1341 this->write_word(value, this->base + ONENAND_REG_START_ADDRESS1);
1342 /* Select DataRAM for DDP */
1343 value = onenand_bufferram_address(this, block);
1344 this->write_word(value, this->base + ONENAND_REG_START_ADDRESS2);
1345 /* Set start block address */
1346 this->write_word(block, this->base + ONENAND_REG_START_BLOCK_ADDRESS);
1347 /* Write unlock command */
1348 this->command(mtd, ONENAND_CMD_UNLOCK, 0, 0);
1350 /* There's no return value */
1351 this->wait(mtd, FL_UNLOCKING);
1354 while (this->read_word(this->base + ONENAND_REG_CTRL_STATUS)
1355 & ONENAND_CTRL_ONGO)
1358 /* Check lock status */
1359 status = this->read_word(this->base + ONENAND_REG_WP_STATUS);
1360 if (!(status & ONENAND_WP_US))
1361 printk(KERN_ERR "block = %d, wp status = 0x%x\n", block, status);
1368 * onenand_print_device_info - Print device ID
1369 * @param device device ID
1373 static void onenand_print_device_info(int device)
1375 int vcc, demuxed, ddp, density;
1377 vcc = device & ONENAND_DEVICE_VCC_MASK;
1378 demuxed = device & ONENAND_DEVICE_IS_DEMUX;
1379 ddp = device & ONENAND_DEVICE_IS_DDP;
1380 density = device >> ONENAND_DEVICE_DENSITY_SHIFT;
1381 printk(KERN_INFO "%sOneNAND%s %dMB %sV 16-bit (0x%02x)\n",
1382 demuxed ? "" : "Muxed ",
1385 vcc ? "2.65/3.3" : "1.8",
1389 static const struct onenand_manufacturers onenand_manuf_ids[] = {
1390 {ONENAND_MFR_SAMSUNG, "Samsung"},
1394 * onenand_check_maf - Check manufacturer ID
1395 * @param manuf manufacturer ID
1397 * Check manufacturer ID
1399 static int onenand_check_maf(int manuf)
1401 int size = ARRAY_SIZE(onenand_manuf_ids);
1405 for (i = 0; i < size; i++)
1406 if (manuf == onenand_manuf_ids[i].id)
1410 name = onenand_manuf_ids[i].name;
1414 printk(KERN_DEBUG "OneNAND Manufacturer: %s (0x%0x)\n", name, manuf);
1420 * onenand_probe - [OneNAND Interface] Probe the OneNAND device
1421 * @param mtd MTD device structure
1423 * OneNAND detection method:
1424 * Compare the the values from command with ones from register
1426 static int onenand_probe(struct mtd_info *mtd)
1428 struct onenand_chip *this = mtd->priv;
1429 int bram_maf_id, bram_dev_id, maf_id, dev_id;
1433 /* Send the command for reading device ID from BootRAM */
1434 this->write_word(ONENAND_CMD_READID, this->base + ONENAND_BOOTRAM);
1436 /* Read manufacturer and device IDs from BootRAM */
1437 bram_maf_id = this->read_word(this->base + ONENAND_BOOTRAM + 0x0);
1438 bram_dev_id = this->read_word(this->base + ONENAND_BOOTRAM + 0x2);
1440 /* Check manufacturer ID */
1441 if (onenand_check_maf(bram_maf_id))
1444 /* Reset OneNAND to read default register values */
1445 this->write_word(ONENAND_CMD_RESET, this->base + ONENAND_BOOTRAM);
1447 /* Read manufacturer and device IDs from Register */
1448 maf_id = this->read_word(this->base + ONENAND_REG_MANUFACTURER_ID);
1449 dev_id = this->read_word(this->base + ONENAND_REG_DEVICE_ID);
1451 /* Check OneNAND device */
1452 if (maf_id != bram_maf_id || dev_id != bram_dev_id)
1455 /* Flash device information */
1456 onenand_print_device_info(dev_id);
1457 this->device_id = dev_id;
1459 density = dev_id >> ONENAND_DEVICE_DENSITY_SHIFT;
1460 this->chipsize = (16 << density) << 20;
1461 /* Set density mask. it is used for DDP */
1462 this->density_mask = (1 << (density + 6));
1464 /* OneNAND page size & block size */
1465 /* The data buffer size is equal to page size */
1466 mtd->oobblock = this->read_word(this->base + ONENAND_REG_DATA_BUFFER_SIZE);
1467 mtd->oobsize = mtd->oobblock >> 5;
1468 /* Pagers per block is always 64 in OneNAND */
1469 mtd->erasesize = mtd->oobblock << 6;
1471 this->erase_shift = ffs(mtd->erasesize) - 1;
1472 this->page_shift = ffs(mtd->oobblock) - 1;
1473 this->ppb_shift = (this->erase_shift - this->page_shift);
1474 this->page_mask = (mtd->erasesize / mtd->oobblock) - 1;
1476 /* REVIST: Multichip handling */
1478 mtd->size = this->chipsize;
1481 version_id = this->read_word(this->base + ONENAND_REG_VERSION_ID);
1482 printk(KERN_DEBUG "OneNAND version = 0x%04x\n", version_id);
1485 if (density <= ONENAND_DEVICE_DENSITY_512Mb &&
1486 !(version_id >> ONENAND_VERSION_PROCESS_SHIFT)) {
1487 printk(KERN_INFO "Lock scheme is Continues Lock\n");
1488 this->options |= ONENAND_CONT_LOCK;
1495 * onenand_suspend - [MTD Interface] Suspend the OneNAND flash
1496 * @param mtd MTD device structure
1498 static int onenand_suspend(struct mtd_info *mtd)
1500 return onenand_get_device(mtd, FL_PM_SUSPENDED);
1504 * onenand_resume - [MTD Interface] Resume the OneNAND flash
1505 * @param mtd MTD device structure
1507 static void onenand_resume(struct mtd_info *mtd)
1509 struct onenand_chip *this = mtd->priv;
1511 if (this->state == FL_PM_SUSPENDED)
1512 onenand_release_device(mtd);
1514 printk(KERN_ERR "resume() called for the chip which is not"
1515 "in suspended state\n");
1520 * onenand_scan - [OneNAND Interface] Scan for the OneNAND device
1521 * @param mtd MTD device structure
1522 * @param maxchips Number of chips to scan for
1524 * This fills out all the not initialized function pointers
1525 * with the defaults.
1526 * The flash ID is read and the mtd/chip structures are
1527 * filled with the appropriate values.
1529 int onenand_scan(struct mtd_info *mtd, int maxchips)
1531 struct onenand_chip *this = mtd->priv;
1533 if (!this->read_word)
1534 this->read_word = onenand_readw;
1535 if (!this->write_word)
1536 this->write_word = onenand_writew;
1539 this->command = onenand_command;
1541 this->wait = onenand_wait;
1543 if (!this->read_bufferram)
1544 this->read_bufferram = onenand_read_bufferram;
1545 if (!this->write_bufferram)
1546 this->write_bufferram = onenand_write_bufferram;
1548 if (!this->block_markbad)
1549 this->block_markbad = onenand_default_block_markbad;
1550 if (!this->scan_bbt)
1551 this->scan_bbt = onenand_default_bbt;
1553 if (onenand_probe(mtd))
1556 /* Set Sync. Burst Read after probing */
1557 if (this->mmcontrol) {
1558 printk(KERN_INFO "OneNAND Sync. Burst Read support\n");
1559 this->read_bufferram = onenand_sync_read_bufferram;
1562 /* Allocate buffers, if necessary */
1563 if (!this->page_buf) {
1565 len = mtd->oobblock + mtd->oobsize;
1566 this->page_buf = kmalloc(len, GFP_KERNEL);
1567 if (!this->page_buf) {
1568 printk(KERN_ERR "onenand_scan(): Can't allocate page_buf\n");
1571 this->options |= ONENAND_PAGEBUF_ALLOC;
1574 this->state = FL_READY;
1575 init_waitqueue_head(&this->wq);
1576 spin_lock_init(&this->chip_lock);
1578 switch (mtd->oobsize) {
1580 this->autooob = &onenand_oob_64;
1584 this->autooob = &onenand_oob_32;
1588 printk(KERN_WARNING "No OOB scheme defined for oobsize %d\n",
1590 /* To prevent kernel oops */
1591 this->autooob = &onenand_oob_32;
1595 memcpy(&mtd->oobinfo, this->autooob, sizeof(mtd->oobinfo));
1597 /* Fill in remaining MTD driver data */
1598 mtd->type = MTD_NANDFLASH;
1599 mtd->flags = MTD_CAP_NANDFLASH | MTD_ECC;
1600 mtd->ecctype = MTD_ECC_SW;
1601 mtd->erase = onenand_erase;
1603 mtd->unpoint = NULL;
1604 mtd->read = onenand_read;
1605 mtd->write = onenand_write;
1606 mtd->read_ecc = onenand_read_ecc;
1607 mtd->write_ecc = onenand_write_ecc;
1608 mtd->read_oob = onenand_read_oob;
1609 mtd->write_oob = onenand_write_oob;
1611 mtd->readv_ecc = NULL;
1612 mtd->writev = onenand_writev;
1613 mtd->writev_ecc = onenand_writev_ecc;
1614 mtd->sync = onenand_sync;
1616 mtd->unlock = onenand_unlock;
1617 mtd->suspend = onenand_suspend;
1618 mtd->resume = onenand_resume;
1619 mtd->block_isbad = onenand_block_isbad;
1620 mtd->block_markbad = onenand_block_markbad;
1621 mtd->owner = THIS_MODULE;
1623 /* Unlock whole block */
1624 mtd->unlock(mtd, 0x0, this->chipsize);
1626 return this->scan_bbt(mtd);
1630 * onenand_release - [OneNAND Interface] Free resources held by the OneNAND device
1631 * @param mtd MTD device structure
1633 void onenand_release(struct mtd_info *mtd)
1635 struct onenand_chip *this = mtd->priv;
1637 #ifdef CONFIG_MTD_PARTITIONS
1638 /* Deregister partitions */
1639 del_mtd_partitions (mtd);
1641 /* Deregister the device */
1642 del_mtd_device (mtd);
1644 /* Free bad block table memory, if allocated */
1647 /* Buffer allocated by onenand_scan */
1648 if (this->options & ONENAND_PAGEBUF_ALLOC)
1649 kfree(this->page_buf);
1652 EXPORT_SYMBOL_GPL(onenand_scan);
1653 EXPORT_SYMBOL_GPL(onenand_release);
1655 MODULE_LICENSE("GPL");
1656 MODULE_AUTHOR("Kyungmin Park <kyungmin.park@samsung.com>");
1657 MODULE_DESCRIPTION("Generic OneNAND flash driver code");