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, block_cmd = 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 case ONENAND_CMD_OTP_ACCESS:
212 block = (int) (addr >> this->erase_shift);
217 block = (int) (addr >> this->erase_shift);
218 page = (int) (addr >> this->page_shift);
219 page &= this->page_mask;
223 /* NOTE: The setting order of the registers is very important! */
224 if (cmd == ONENAND_CMD_BUFFERRAM) {
225 /* Select DataRAM for DDP */
226 value = onenand_bufferram_address(this, block);
227 this->write_word(value, this->base + ONENAND_REG_START_ADDRESS2);
229 /* Switch to the next data buffer */
230 ONENAND_SET_NEXT_BUFFERRAM(this);
236 /* Write 'DFS, FBA' of Flash */
237 value = onenand_block_address(this, block);
238 this->write_word(value, this->base + ONENAND_REG_START_ADDRESS1);
241 /* Select DataRAM for DDP */
242 value = onenand_bufferram_address(this, block);
243 this->write_word(value, this->base + ONENAND_REG_START_ADDRESS2);
251 case ONENAND_CMD_READ:
252 case ONENAND_CMD_READOOB:
253 dataram = ONENAND_SET_NEXT_BUFFERRAM(this);
258 dataram = ONENAND_CURRENT_BUFFERRAM(this);
262 /* Write 'FPA, FSA' of Flash */
263 value = onenand_page_address(page, sectors);
264 this->write_word(value, this->base + ONENAND_REG_START_ADDRESS8);
266 /* Write 'BSA, BSC' of DataRAM */
267 value = onenand_buffer_address(dataram, sectors, count);
268 this->write_word(value, this->base + ONENAND_REG_START_BUFFER);
271 /* Select DataRAM for DDP */
272 value = onenand_bufferram_address(this, block);
273 this->write_word(value, this->base + ONENAND_REG_START_ADDRESS2);
277 /* Interrupt clear */
278 this->write_word(ONENAND_INT_CLEAR, this->base + ONENAND_REG_INTERRUPT);
281 this->write_word(cmd, this->base + ONENAND_REG_COMMAND);
287 * onenand_wait - [DEFAULT] wait until the command is done
288 * @param mtd MTD device structure
289 * @param state state to select the max. timeout value
291 * Wait for command done. This applies to all OneNAND command
292 * Read can take up to 30us, erase up to 2ms and program up to 350us
293 * according to general OneNAND specs
295 static int onenand_wait(struct mtd_info *mtd, int state)
297 struct onenand_chip * this = mtd->priv;
298 unsigned long timeout;
299 unsigned int flags = ONENAND_INT_MASTER;
300 unsigned int interrupt = 0;
301 unsigned int ctrl, ecc;
303 /* The 20 msec is enough */
304 timeout = jiffies + msecs_to_jiffies(20);
305 while (time_before(jiffies, timeout)) {
306 interrupt = this->read_word(this->base + ONENAND_REG_INTERRUPT);
308 if (interrupt & flags)
311 if (state != FL_READING)
313 touch_softlockup_watchdog();
315 /* To get correct interrupt status in timeout case */
316 interrupt = this->read_word(this->base + ONENAND_REG_INTERRUPT);
318 ctrl = this->read_word(this->base + ONENAND_REG_CTRL_STATUS);
320 if (ctrl & ONENAND_CTRL_ERROR) {
321 /* It maybe occur at initial bad block */
322 DEBUG(MTD_DEBUG_LEVEL0, "onenand_wait: controller error = 0x%04x\n", ctrl);
323 /* Clear other interrupt bits for preventing ECC error */
324 interrupt &= ONENAND_INT_MASTER;
327 if (ctrl & ONENAND_CTRL_LOCK) {
328 DEBUG(MTD_DEBUG_LEVEL0, "onenand_wait: it's locked error = 0x%04x\n", ctrl);
332 if (interrupt & ONENAND_INT_READ) {
333 ecc = this->read_word(this->base + ONENAND_REG_ECC_STATUS);
334 if (ecc & ONENAND_ECC_2BIT_ALL) {
335 DEBUG(MTD_DEBUG_LEVEL0, "onenand_wait: ECC error = 0x%04x\n", ecc);
344 * onenand_bufferram_offset - [DEFAULT] BufferRAM offset
345 * @param mtd MTD data structure
346 * @param area BufferRAM area
347 * @return offset given area
349 * Return BufferRAM offset given area
351 static inline int onenand_bufferram_offset(struct mtd_info *mtd, int area)
353 struct onenand_chip *this = mtd->priv;
355 if (ONENAND_CURRENT_BUFFERRAM(this)) {
356 if (area == ONENAND_DATARAM)
357 return mtd->writesize;
358 if (area == ONENAND_SPARERAM)
366 * onenand_read_bufferram - [OneNAND Interface] Read the bufferram area
367 * @param mtd MTD data structure
368 * @param area BufferRAM area
369 * @param buffer the databuffer to put/get data
370 * @param offset offset to read from or write to
371 * @param count number of bytes to read/write
373 * Read the BufferRAM area
375 static int onenand_read_bufferram(struct mtd_info *mtd, int area,
376 unsigned char *buffer, int offset, size_t count)
378 struct onenand_chip *this = mtd->priv;
379 void __iomem *bufferram;
381 bufferram = this->base + area;
383 bufferram += onenand_bufferram_offset(mtd, area);
385 if (ONENAND_CHECK_BYTE_ACCESS(count)) {
388 /* Align with word(16-bit) size */
391 /* Read word and save byte */
392 word = this->read_word(bufferram + offset + count);
393 buffer[count] = (word & 0xff);
396 memcpy(buffer, bufferram + offset, count);
402 * onenand_sync_read_bufferram - [OneNAND Interface] Read the bufferram area with Sync. Burst mode
403 * @param mtd MTD data structure
404 * @param area BufferRAM area
405 * @param buffer the databuffer to put/get data
406 * @param offset offset to read from or write to
407 * @param count number of bytes to read/write
409 * Read the BufferRAM area with Sync. Burst Mode
411 static int onenand_sync_read_bufferram(struct mtd_info *mtd, int area,
412 unsigned char *buffer, int offset, size_t count)
414 struct onenand_chip *this = mtd->priv;
415 void __iomem *bufferram;
417 bufferram = this->base + area;
419 bufferram += onenand_bufferram_offset(mtd, area);
421 this->mmcontrol(mtd, ONENAND_SYS_CFG1_SYNC_READ);
423 if (ONENAND_CHECK_BYTE_ACCESS(count)) {
426 /* Align with word(16-bit) size */
429 /* Read word and save byte */
430 word = this->read_word(bufferram + offset + count);
431 buffer[count] = (word & 0xff);
434 memcpy(buffer, bufferram + offset, count);
436 this->mmcontrol(mtd, 0);
442 * onenand_write_bufferram - [OneNAND Interface] Write the bufferram area
443 * @param mtd MTD data structure
444 * @param area BufferRAM area
445 * @param buffer the databuffer to put/get data
446 * @param offset offset to read from or write to
447 * @param count number of bytes to read/write
449 * Write the BufferRAM area
451 static int onenand_write_bufferram(struct mtd_info *mtd, int area,
452 const unsigned char *buffer, int offset, size_t count)
454 struct onenand_chip *this = mtd->priv;
455 void __iomem *bufferram;
457 bufferram = this->base + area;
459 bufferram += onenand_bufferram_offset(mtd, area);
461 if (ONENAND_CHECK_BYTE_ACCESS(count)) {
465 /* Align with word(16-bit) size */
468 /* Calculate byte access offset */
469 byte_offset = offset + count;
471 /* Read word and save byte */
472 word = this->read_word(bufferram + byte_offset);
473 word = (word & ~0xff) | buffer[count];
474 this->write_word(word, bufferram + byte_offset);
477 memcpy(bufferram + offset, buffer, count);
483 * onenand_check_bufferram - [GENERIC] Check BufferRAM information
484 * @param mtd MTD data structure
485 * @param addr address to check
486 * @return 1 if there are valid data, otherwise 0
488 * Check bufferram if there is data we required
490 static int onenand_check_bufferram(struct mtd_info *mtd, loff_t addr)
492 struct onenand_chip *this = mtd->priv;
496 block = (int) (addr >> this->erase_shift);
497 page = (int) (addr >> this->page_shift);
498 page &= this->page_mask;
500 i = ONENAND_CURRENT_BUFFERRAM(this);
502 /* Is there valid data? */
503 if (this->bufferram[i].block == block &&
504 this->bufferram[i].page == page &&
505 this->bufferram[i].valid)
512 * onenand_update_bufferram - [GENERIC] Update BufferRAM information
513 * @param mtd MTD data structure
514 * @param addr address to update
515 * @param valid valid flag
517 * Update BufferRAM information
519 static int onenand_update_bufferram(struct mtd_info *mtd, loff_t addr,
522 struct onenand_chip *this = mtd->priv;
526 block = (int) (addr >> this->erase_shift);
527 page = (int) (addr >> this->page_shift);
528 page &= this->page_mask;
530 /* Invalidate BufferRAM */
531 for (i = 0; i < MAX_BUFFERRAM; i++) {
532 if (this->bufferram[i].block == block &&
533 this->bufferram[i].page == page)
534 this->bufferram[i].valid = 0;
537 /* Update BufferRAM */
538 i = ONENAND_CURRENT_BUFFERRAM(this);
539 this->bufferram[i].block = block;
540 this->bufferram[i].page = page;
541 this->bufferram[i].valid = valid;
547 * onenand_get_device - [GENERIC] Get chip for selected access
548 * @param mtd MTD device structure
549 * @param new_state the state which is requested
551 * Get the device and lock it for exclusive access
553 static int onenand_get_device(struct mtd_info *mtd, int new_state)
555 struct onenand_chip *this = mtd->priv;
556 DECLARE_WAITQUEUE(wait, current);
559 * Grab the lock and see if the device is available
562 spin_lock(&this->chip_lock);
563 if (this->state == FL_READY) {
564 this->state = new_state;
565 spin_unlock(&this->chip_lock);
568 if (new_state == FL_PM_SUSPENDED) {
569 spin_unlock(&this->chip_lock);
570 return (this->state == FL_PM_SUSPENDED) ? 0 : -EAGAIN;
572 set_current_state(TASK_UNINTERRUPTIBLE);
573 add_wait_queue(&this->wq, &wait);
574 spin_unlock(&this->chip_lock);
576 remove_wait_queue(&this->wq, &wait);
583 * onenand_release_device - [GENERIC] release chip
584 * @param mtd MTD device structure
586 * Deselect, release chip lock and wake up anyone waiting on the device
588 static void onenand_release_device(struct mtd_info *mtd)
590 struct onenand_chip *this = mtd->priv;
592 /* Release the chip */
593 spin_lock(&this->chip_lock);
594 this->state = FL_READY;
596 spin_unlock(&this->chip_lock);
600 * onenand_read_ecc - [MTD Interface] Read data with ECC
601 * @param mtd MTD device structure
602 * @param from offset to read from
603 * @param len number of bytes to read
604 * @param retlen pointer to variable to store the number of read bytes
605 * @param buf the databuffer to put data
606 * @param oob_buf filesystem supplied oob data buffer
607 * @param oobsel oob selection structure
609 * OneNAND read with ECC
611 static int onenand_read_ecc(struct mtd_info *mtd, loff_t from, size_t len,
612 size_t *retlen, u_char *buf,
613 u_char *oob_buf, struct nand_oobinfo *oobsel)
615 struct onenand_chip *this = mtd->priv;
616 int read = 0, column;
620 DEBUG(MTD_DEBUG_LEVEL3, "onenand_read_ecc: from = 0x%08x, len = %i\n", (unsigned int) from, (int) len);
622 /* Do not allow reads past end of device */
623 if ((from + len) > mtd->size) {
624 DEBUG(MTD_DEBUG_LEVEL0, "onenand_read_ecc: Attempt read beyond end of device\n");
629 /* Grab the lock and see if the device is available */
630 onenand_get_device(mtd, FL_READING);
632 /* TODO handling oob */
635 thislen = min_t(int, mtd->writesize, len - read);
637 column = from & (mtd->writesize - 1);
638 if (column + thislen > mtd->writesize)
639 thislen = mtd->writesize - column;
641 if (!onenand_check_bufferram(mtd, from)) {
642 this->command(mtd, ONENAND_CMD_READ, from, mtd->writesize);
644 ret = this->wait(mtd, FL_READING);
645 /* First copy data and check return value for ECC handling */
646 onenand_update_bufferram(mtd, from, 1);
649 this->read_bufferram(mtd, ONENAND_DATARAM, buf, column, thislen);
657 DEBUG(MTD_DEBUG_LEVEL0, "onenand_read_ecc: read failed = %d\n", ret);
666 /* Deselect and wake up anyone waiting on the device */
667 onenand_release_device(mtd);
670 * Return success, if no ECC failures, else -EBADMSG
671 * fs driver will take care of that, because
672 * retlen == desired len and result == -EBADMSG
679 * onenand_read - [MTD Interface] MTD compability function for onenand_read_ecc
680 * @param mtd MTD device structure
681 * @param from offset to read from
682 * @param len number of bytes to read
683 * @param retlen pointer to variable to store the number of read bytes
684 * @param buf the databuffer to put data
686 * This function simply calls onenand_read_ecc with oob buffer and oobsel = NULL
688 static int onenand_read(struct mtd_info *mtd, loff_t from, size_t len,
689 size_t *retlen, u_char *buf)
691 return onenand_read_ecc(mtd, from, len, retlen, buf, NULL, NULL);
695 * onenand_read_oob - [MTD Interface] OneNAND read out-of-band
696 * @param mtd MTD device structure
697 * @param from offset to read from
698 * @param len number of bytes to read
699 * @param retlen pointer to variable to store the number of read bytes
700 * @param buf the databuffer to put data
702 * OneNAND read out-of-band data from the spare area
704 static int onenand_read_oob(struct mtd_info *mtd, loff_t from, size_t len,
705 size_t *retlen, u_char *buf)
707 struct onenand_chip *this = mtd->priv;
708 int read = 0, thislen, column;
711 DEBUG(MTD_DEBUG_LEVEL3, "onenand_read_oob: from = 0x%08x, len = %i\n", (unsigned int) from, (int) len);
713 /* Initialize return length value */
716 /* Do not allow reads past end of device */
717 if (unlikely((from + len) > mtd->size)) {
718 DEBUG(MTD_DEBUG_LEVEL0, "onenand_read_oob: Attempt read beyond end of device\n");
722 /* Grab the lock and see if the device is available */
723 onenand_get_device(mtd, FL_READING);
725 column = from & (mtd->oobsize - 1);
728 thislen = mtd->oobsize - column;
729 thislen = min_t(int, thislen, len);
731 this->command(mtd, ONENAND_CMD_READOOB, from, mtd->oobsize);
733 onenand_update_bufferram(mtd, from, 0);
735 ret = this->wait(mtd, FL_READING);
736 /* First copy data and check return value for ECC handling */
738 this->read_bufferram(mtd, ONENAND_SPARERAM, buf, column, thislen);
746 DEBUG(MTD_DEBUG_LEVEL0, "onenand_read_oob: read failed = %d\n", ret);
755 from += mtd->writesize;
761 /* Deselect and wake up anyone waiting on the device */
762 onenand_release_device(mtd);
768 #ifdef CONFIG_MTD_ONENAND_VERIFY_WRITE
770 * onenand_verify_oob - [GENERIC] verify the oob contents after a write
771 * @param mtd MTD device structure
772 * @param buf the databuffer to verify
773 * @param to offset to read from
774 * @param len number of bytes to read and compare
777 static int onenand_verify_oob(struct mtd_info *mtd, const u_char *buf, loff_t to, int len)
779 struct onenand_chip *this = mtd->priv;
780 char *readp = this->page_buf;
781 int column = to & (mtd->oobsize - 1);
784 this->command(mtd, ONENAND_CMD_READOOB, to, mtd->oobsize);
785 onenand_update_bufferram(mtd, to, 0);
786 status = this->wait(mtd, FL_READING);
790 this->read_bufferram(mtd, ONENAND_SPARERAM, readp, column, len);
792 for(i = 0; i < len; i++)
793 if (buf[i] != 0xFF && buf[i] != readp[i])
800 * onenand_verify_page - [GENERIC] verify the chip contents after a write
801 * @param mtd MTD device structure
802 * @param buf the databuffer to verify
804 * Check DataRAM area directly
806 static int onenand_verify_page(struct mtd_info *mtd, u_char *buf, loff_t addr)
808 struct onenand_chip *this = mtd->priv;
809 void __iomem *dataram0, *dataram1;
812 this->command(mtd, ONENAND_CMD_READ, addr, mtd->writesize);
814 ret = this->wait(mtd, FL_READING);
818 onenand_update_bufferram(mtd, addr, 1);
820 /* Check, if the two dataram areas are same */
821 dataram0 = this->base + ONENAND_DATARAM;
822 dataram1 = dataram0 + mtd->writesize;
824 if (memcmp(dataram0, dataram1, mtd->writesize))
830 #define onenand_verify_page(...) (0)
831 #define onenand_verify_oob(...) (0)
834 #define NOTALIGNED(x) ((x & (mtd->writesize - 1)) != 0)
837 * onenand_write_ecc - [MTD Interface] OneNAND write with ECC
838 * @param mtd MTD device structure
839 * @param to offset to write to
840 * @param len number of bytes to write
841 * @param retlen pointer to variable to store the number of written bytes
842 * @param buf the data to write
843 * @param eccbuf filesystem supplied oob data buffer
844 * @param oobsel oob selection structure
846 * OneNAND write with ECC
848 static int onenand_write_ecc(struct mtd_info *mtd, loff_t to, size_t len,
849 size_t *retlen, const u_char *buf,
850 u_char *eccbuf, struct nand_oobinfo *oobsel)
852 struct onenand_chip *this = mtd->priv;
856 DEBUG(MTD_DEBUG_LEVEL3, "onenand_write_ecc: to = 0x%08x, len = %i\n", (unsigned int) to, (int) len);
858 /* Initialize retlen, in case of early exit */
861 /* Do not allow writes past end of device */
862 if (unlikely((to + len) > mtd->size)) {
863 DEBUG(MTD_DEBUG_LEVEL0, "onenand_write_ecc: Attempt write to past end of device\n");
867 /* Reject writes, which are not page aligned */
868 if (unlikely(NOTALIGNED(to)) || unlikely(NOTALIGNED(len))) {
869 DEBUG(MTD_DEBUG_LEVEL0, "onenand_write_ecc: Attempt to write not page aligned data\n");
873 /* Grab the lock and see if the device is available */
874 onenand_get_device(mtd, FL_WRITING);
876 /* Loop until all data write */
877 while (written < len) {
878 int thislen = min_t(int, mtd->writesize, len - written);
880 this->command(mtd, ONENAND_CMD_BUFFERRAM, to, mtd->writesize);
882 this->write_bufferram(mtd, ONENAND_DATARAM, buf, 0, thislen);
883 this->write_bufferram(mtd, ONENAND_SPARERAM, ffchars, 0, mtd->oobsize);
885 this->command(mtd, ONENAND_CMD_PROG, to, mtd->writesize);
887 onenand_update_bufferram(mtd, to, 1);
889 ret = this->wait(mtd, FL_WRITING);
891 DEBUG(MTD_DEBUG_LEVEL0, "onenand_write_ecc: write filaed %d\n", ret);
897 /* Only check verify write turn on */
898 ret = onenand_verify_page(mtd, (u_char *) buf, to);
900 DEBUG(MTD_DEBUG_LEVEL0, "onenand_write_ecc: verify failed %d\n", ret);
912 /* Deselect and wake up anyone waiting on the device */
913 onenand_release_device(mtd);
921 * onenand_write - [MTD Interface] compability function for onenand_write_ecc
922 * @param mtd MTD device structure
923 * @param to offset to write to
924 * @param len number of bytes to write
925 * @param retlen pointer to variable to store the number of written bytes
926 * @param buf the data to write
928 * This function simply calls onenand_write_ecc
929 * with oob buffer and oobsel = NULL
931 static int onenand_write(struct mtd_info *mtd, loff_t to, size_t len,
932 size_t *retlen, const u_char *buf)
934 return onenand_write_ecc(mtd, to, len, retlen, buf, NULL, NULL);
938 * onenand_write_oob - [MTD Interface] OneNAND write out-of-band
939 * @param mtd MTD device structure
940 * @param to offset to write to
941 * @param len number of bytes to write
942 * @param retlen pointer to variable to store the number of written bytes
943 * @param buf the data to write
945 * OneNAND write out-of-band
947 static int onenand_write_oob(struct mtd_info *mtd, loff_t to, size_t len,
948 size_t *retlen, const u_char *buf)
950 struct onenand_chip *this = mtd->priv;
954 DEBUG(MTD_DEBUG_LEVEL3, "onenand_write_oob: to = 0x%08x, len = %i\n", (unsigned int) to, (int) len);
956 /* Initialize retlen, in case of early exit */
959 /* Do not allow writes past end of device */
960 if (unlikely((to + len) > mtd->size)) {
961 DEBUG(MTD_DEBUG_LEVEL0, "onenand_write_oob: Attempt write to past end of device\n");
965 /* Grab the lock and see if the device is available */
966 onenand_get_device(mtd, FL_WRITING);
968 /* Loop until all data write */
969 while (written < len) {
970 int thislen = min_t(int, mtd->oobsize, len - written);
972 column = to & (mtd->oobsize - 1);
974 this->command(mtd, ONENAND_CMD_BUFFERRAM, to, mtd->oobsize);
976 /* We send data to spare ram with oobsize
977 * to prevent byte access */
978 memset(this->page_buf, 0xff, mtd->oobsize);
979 memcpy(this->page_buf + column, buf, thislen);
980 this->write_bufferram(mtd, ONENAND_SPARERAM, this->page_buf, 0, mtd->oobsize);
982 this->command(mtd, ONENAND_CMD_PROGOOB, to, mtd->oobsize);
984 onenand_update_bufferram(mtd, to, 0);
986 ret = this->wait(mtd, FL_WRITING);
988 DEBUG(MTD_DEBUG_LEVEL0, "onenand_write_oob: write filaed %d\n", ret);
992 ret = onenand_verify_oob(mtd, buf, to, thislen);
994 DEBUG(MTD_DEBUG_LEVEL0, "onenand_write_oob: verify failed %d\n", ret);
1008 /* Deselect and wake up anyone waiting on the device */
1009 onenand_release_device(mtd);
1017 * onenand_block_checkbad - [GENERIC] Check if a block is marked bad
1018 * @param mtd MTD device structure
1019 * @param ofs offset from device start
1020 * @param getchip 0, if the chip is already selected
1021 * @param allowbbt 1, if its allowed to access the bbt area
1023 * Check, if the block is bad. Either by reading the bad block table or
1024 * calling of the scan function.
1026 static int onenand_block_checkbad(struct mtd_info *mtd, loff_t ofs, int getchip, int allowbbt)
1028 struct onenand_chip *this = mtd->priv;
1029 struct bbm_info *bbm = this->bbm;
1031 /* Return info from the table */
1032 return bbm->isbad_bbt(mtd, ofs, allowbbt);
1036 * onenand_erase - [MTD Interface] erase block(s)
1037 * @param mtd MTD device structure
1038 * @param instr erase instruction
1040 * Erase one ore more blocks
1042 static int onenand_erase(struct mtd_info *mtd, struct erase_info *instr)
1044 struct onenand_chip *this = mtd->priv;
1045 unsigned int block_size;
1050 DEBUG(MTD_DEBUG_LEVEL3, "onenand_erase: start = 0x%08x, len = %i\n", (unsigned int) instr->addr, (unsigned int) instr->len);
1052 block_size = (1 << this->erase_shift);
1054 /* Start address must align on block boundary */
1055 if (unlikely(instr->addr & (block_size - 1))) {
1056 DEBUG(MTD_DEBUG_LEVEL0, "onenand_erase: Unaligned address\n");
1060 /* Length must align on block boundary */
1061 if (unlikely(instr->len & (block_size - 1))) {
1062 DEBUG(MTD_DEBUG_LEVEL0, "onenand_erase: Length not block aligned\n");
1066 /* Do not allow erase past end of device */
1067 if (unlikely((instr->len + instr->addr) > mtd->size)) {
1068 DEBUG(MTD_DEBUG_LEVEL0, "onenand_erase: Erase past end of device\n");
1072 instr->fail_addr = 0xffffffff;
1074 /* Grab the lock and see if the device is available */
1075 onenand_get_device(mtd, FL_ERASING);
1077 /* Loop throught the pages */
1081 instr->state = MTD_ERASING;
1085 /* Check if we have a bad block, we do not erase bad blocks */
1086 if (onenand_block_checkbad(mtd, addr, 0, 0)) {
1087 printk (KERN_WARNING "onenand_erase: attempt to erase a bad block at addr 0x%08x\n", (unsigned int) addr);
1088 instr->state = MTD_ERASE_FAILED;
1092 this->command(mtd, ONENAND_CMD_ERASE, addr, block_size);
1094 ret = this->wait(mtd, FL_ERASING);
1095 /* Check, if it is write protected */
1098 DEBUG(MTD_DEBUG_LEVEL0, "onenand_erase: Device is write protected!!!\n");
1100 DEBUG(MTD_DEBUG_LEVEL0, "onenand_erase: Failed erase, block %d\n", (unsigned) (addr >> this->erase_shift));
1101 instr->state = MTD_ERASE_FAILED;
1102 instr->fail_addr = addr;
1110 instr->state = MTD_ERASE_DONE;
1114 ret = instr->state == MTD_ERASE_DONE ? 0 : -EIO;
1115 /* Do call back function */
1117 mtd_erase_callback(instr);
1119 /* Deselect and wake up anyone waiting on the device */
1120 onenand_release_device(mtd);
1126 * onenand_sync - [MTD Interface] sync
1127 * @param mtd MTD device structure
1129 * Sync is actually a wait for chip ready function
1131 static void onenand_sync(struct mtd_info *mtd)
1133 DEBUG(MTD_DEBUG_LEVEL3, "onenand_sync: called\n");
1135 /* Grab the lock and see if the device is available */
1136 onenand_get_device(mtd, FL_SYNCING);
1138 /* Release it and go back */
1139 onenand_release_device(mtd);
1144 * onenand_block_isbad - [MTD Interface] Check whether the block at the given offset is bad
1145 * @param mtd MTD device structure
1146 * @param ofs offset relative to mtd start
1148 * Check whether the block is bad
1150 static int onenand_block_isbad(struct mtd_info *mtd, loff_t ofs)
1152 /* Check for invalid offset */
1153 if (ofs > mtd->size)
1156 return onenand_block_checkbad(mtd, ofs, 1, 0);
1160 * onenand_default_block_markbad - [DEFAULT] mark a block bad
1161 * @param mtd MTD device structure
1162 * @param ofs offset from device start
1164 * This is the default implementation, which can be overridden by
1165 * a hardware specific driver.
1167 static int onenand_default_block_markbad(struct mtd_info *mtd, loff_t ofs)
1169 struct onenand_chip *this = mtd->priv;
1170 struct bbm_info *bbm = this->bbm;
1171 u_char buf[2] = {0, 0};
1175 /* Get block number */
1176 block = ((int) ofs) >> bbm->bbt_erase_shift;
1178 bbm->bbt[block >> 2] |= 0x01 << ((block & 0x03) << 1);
1180 /* We write two bytes, so we dont have to mess with 16 bit access */
1181 ofs += mtd->oobsize + (bbm->badblockpos & ~0x01);
1182 return mtd->write_oob(mtd, ofs , 2, &retlen, buf);
1186 * onenand_block_markbad - [MTD Interface] Mark the block at the given offset as bad
1187 * @param mtd MTD device structure
1188 * @param ofs offset relative to mtd start
1190 * Mark the block as bad
1192 static int onenand_block_markbad(struct mtd_info *mtd, loff_t ofs)
1194 struct onenand_chip *this = mtd->priv;
1197 ret = onenand_block_isbad(mtd, ofs);
1199 /* If it was bad already, return success and do nothing */
1205 return this->block_markbad(mtd, ofs);
1209 * onenand_unlock - [MTD Interface] Unlock block(s)
1210 * @param mtd MTD device structure
1211 * @param ofs offset relative to mtd start
1212 * @param len number of bytes to unlock
1214 * Unlock one or more blocks
1216 static int onenand_unlock(struct mtd_info *mtd, loff_t ofs, size_t len)
1218 struct onenand_chip *this = mtd->priv;
1219 int start, end, block, value, status;
1221 start = ofs >> this->erase_shift;
1222 end = len >> this->erase_shift;
1224 /* Continuous lock scheme */
1225 if (this->options & ONENAND_CONT_LOCK) {
1226 /* Set start block address */
1227 this->write_word(start, this->base + ONENAND_REG_START_BLOCK_ADDRESS);
1228 /* Set end block address */
1229 this->write_word(end - 1, this->base + ONENAND_REG_END_BLOCK_ADDRESS);
1230 /* Write unlock command */
1231 this->command(mtd, ONENAND_CMD_UNLOCK, 0, 0);
1233 /* There's no return value */
1234 this->wait(mtd, FL_UNLOCKING);
1237 while (this->read_word(this->base + ONENAND_REG_CTRL_STATUS)
1238 & ONENAND_CTRL_ONGO)
1241 /* Check lock status */
1242 status = this->read_word(this->base + ONENAND_REG_WP_STATUS);
1243 if (!(status & ONENAND_WP_US))
1244 printk(KERN_ERR "wp status = 0x%x\n", status);
1249 /* Block lock scheme */
1250 for (block = start; block < end; block++) {
1251 /* Set block address */
1252 value = onenand_block_address(this, block);
1253 this->write_word(value, this->base + ONENAND_REG_START_ADDRESS1);
1254 /* Select DataRAM for DDP */
1255 value = onenand_bufferram_address(this, block);
1256 this->write_word(value, this->base + ONENAND_REG_START_ADDRESS2);
1257 /* Set start block address */
1258 this->write_word(block, this->base + ONENAND_REG_START_BLOCK_ADDRESS);
1259 /* Write unlock command */
1260 this->command(mtd, ONENAND_CMD_UNLOCK, 0, 0);
1262 /* There's no return value */
1263 this->wait(mtd, FL_UNLOCKING);
1266 while (this->read_word(this->base + ONENAND_REG_CTRL_STATUS)
1267 & ONENAND_CTRL_ONGO)
1270 /* Check lock status */
1271 status = this->read_word(this->base + ONENAND_REG_WP_STATUS);
1272 if (!(status & ONENAND_WP_US))
1273 printk(KERN_ERR "block = %d, wp status = 0x%x\n", block, status);
1279 #ifdef CONFIG_MTD_ONENAND_OTP
1281 /* Interal OTP operation */
1282 typedef int (*otp_op_t)(struct mtd_info *mtd, loff_t form, size_t len,
1283 size_t *retlen, u_char *buf);
1286 * do_otp_read - [DEFAULT] Read OTP block area
1287 * @param mtd MTD device structure
1288 * @param from The offset to read
1289 * @param len number of bytes to read
1290 * @param retlen pointer to variable to store the number of readbytes
1291 * @param buf the databuffer to put/get data
1293 * Read OTP block area.
1295 static int do_otp_read(struct mtd_info *mtd, loff_t from, size_t len,
1296 size_t *retlen, u_char *buf)
1298 struct onenand_chip *this = mtd->priv;
1301 /* Enter OTP access mode */
1302 this->command(mtd, ONENAND_CMD_OTP_ACCESS, 0, 0);
1303 this->wait(mtd, FL_OTPING);
1305 ret = mtd->read(mtd, from, len, retlen, buf);
1307 /* Exit OTP access mode */
1308 this->command(mtd, ONENAND_CMD_RESET, 0, 0);
1309 this->wait(mtd, FL_RESETING);
1315 * do_otp_write - [DEFAULT] Write OTP block area
1316 * @param mtd MTD device structure
1317 * @param from The offset to write
1318 * @param len number of bytes to write
1319 * @param retlen pointer to variable to store the number of write bytes
1320 * @param buf the databuffer to put/get data
1322 * Write OTP block area.
1324 static int do_otp_write(struct mtd_info *mtd, loff_t from, size_t len,
1325 size_t *retlen, u_char *buf)
1327 struct onenand_chip *this = mtd->priv;
1328 unsigned char *pbuf = buf;
1331 /* Force buffer page aligned */
1332 if (len < mtd->writesize) {
1333 memcpy(this->page_buf, buf, len);
1334 memset(this->page_buf + len, 0xff, mtd->writesize - len);
1335 pbuf = this->page_buf;
1336 len = mtd->writesize;
1339 /* Enter OTP access mode */
1340 this->command(mtd, ONENAND_CMD_OTP_ACCESS, 0, 0);
1341 this->wait(mtd, FL_OTPING);
1343 ret = mtd->write(mtd, from, len, retlen, pbuf);
1345 /* Exit OTP access mode */
1346 this->command(mtd, ONENAND_CMD_RESET, 0, 0);
1347 this->wait(mtd, FL_RESETING);
1353 * do_otp_lock - [DEFAULT] Lock OTP block area
1354 * @param mtd MTD device structure
1355 * @param from The offset to lock
1356 * @param len number of bytes to lock
1357 * @param retlen pointer to variable to store the number of lock bytes
1358 * @param buf the databuffer to put/get data
1360 * Lock OTP block area.
1362 static int do_otp_lock(struct mtd_info *mtd, loff_t from, size_t len,
1363 size_t *retlen, u_char *buf)
1365 struct onenand_chip *this = mtd->priv;
1368 /* Enter OTP access mode */
1369 this->command(mtd, ONENAND_CMD_OTP_ACCESS, 0, 0);
1370 this->wait(mtd, FL_OTPING);
1372 ret = mtd->write_oob(mtd, from, len, retlen, buf);
1374 /* Exit OTP access mode */
1375 this->command(mtd, ONENAND_CMD_RESET, 0, 0);
1376 this->wait(mtd, FL_RESETING);
1382 * onenand_otp_walk - [DEFAULT] Handle OTP operation
1383 * @param mtd MTD device structure
1384 * @param from The offset to read/write
1385 * @param len number of bytes to read/write
1386 * @param retlen pointer to variable to store the number of read bytes
1387 * @param buf the databuffer to put/get data
1388 * @param action do given action
1389 * @param mode specify user and factory
1391 * Handle OTP operation.
1393 static int onenand_otp_walk(struct mtd_info *mtd, loff_t from, size_t len,
1394 size_t *retlen, u_char *buf,
1395 otp_op_t action, int mode)
1397 struct onenand_chip *this = mtd->priv;
1404 density = this->device_id >> ONENAND_DEVICE_DENSITY_SHIFT;
1405 if (density < ONENAND_DEVICE_DENSITY_512Mb)
1410 if (mode == MTD_OTP_FACTORY) {
1411 from += mtd->writesize * otp_pages;
1412 otp_pages = 64 - otp_pages;
1415 /* Check User/Factory boundary */
1416 if (((mtd->writesize * otp_pages) - (from + len)) < 0)
1419 while (len > 0 && otp_pages > 0) {
1420 if (!action) { /* OTP Info functions */
1421 struct otp_info *otpinfo;
1423 len -= sizeof(struct otp_info);
1427 otpinfo = (struct otp_info *) buf;
1428 otpinfo->start = from;
1429 otpinfo->length = mtd->writesize;
1430 otpinfo->locked = 0;
1432 from += mtd->writesize;
1433 buf += sizeof(struct otp_info);
1434 *retlen += sizeof(struct otp_info);
1439 ret = action(mtd, from, len, &tmp_retlen, buf);
1455 * onenand_get_fact_prot_info - [MTD Interface] Read factory OTP info
1456 * @param mtd MTD device structure
1457 * @param buf the databuffer to put/get data
1458 * @param len number of bytes to read
1460 * Read factory OTP info.
1462 static int onenand_get_fact_prot_info(struct mtd_info *mtd,
1463 struct otp_info *buf, size_t len)
1468 ret = onenand_otp_walk(mtd, 0, len, &retlen, (u_char *) buf, NULL, MTD_OTP_FACTORY);
1470 return ret ? : retlen;
1474 * onenand_read_fact_prot_reg - [MTD Interface] Read factory OTP area
1475 * @param mtd MTD device structure
1476 * @param from The offset to read
1477 * @param len number of bytes to read
1478 * @param retlen pointer to variable to store the number of read bytes
1479 * @param buf the databuffer to put/get data
1481 * Read factory OTP area.
1483 static int onenand_read_fact_prot_reg(struct mtd_info *mtd, loff_t from,
1484 size_t len, size_t *retlen, u_char *buf)
1486 return onenand_otp_walk(mtd, from, len, retlen, buf, do_otp_read, MTD_OTP_FACTORY);
1490 * onenand_get_user_prot_info - [MTD Interface] Read user OTP info
1491 * @param mtd MTD device structure
1492 * @param buf the databuffer to put/get data
1493 * @param len number of bytes to read
1495 * Read user OTP info.
1497 static int onenand_get_user_prot_info(struct mtd_info *mtd,
1498 struct otp_info *buf, size_t len)
1503 ret = onenand_otp_walk(mtd, 0, len, &retlen, (u_char *) buf, NULL, MTD_OTP_USER);
1505 return ret ? : retlen;
1509 * onenand_read_user_prot_reg - [MTD Interface] Read user OTP area
1510 * @param mtd MTD device structure
1511 * @param from The offset to read
1512 * @param len number of bytes to read
1513 * @param retlen pointer to variable to store the number of read bytes
1514 * @param buf the databuffer to put/get data
1516 * Read user OTP area.
1518 static int onenand_read_user_prot_reg(struct mtd_info *mtd, loff_t from,
1519 size_t len, size_t *retlen, u_char *buf)
1521 return onenand_otp_walk(mtd, from, len, retlen, buf, do_otp_read, MTD_OTP_USER);
1525 * onenand_write_user_prot_reg - [MTD Interface] Write user OTP area
1526 * @param mtd MTD device structure
1527 * @param from The offset to write
1528 * @param len number of bytes to write
1529 * @param retlen pointer to variable to store the number of write bytes
1530 * @param buf the databuffer to put/get data
1532 * Write user OTP area.
1534 static int onenand_write_user_prot_reg(struct mtd_info *mtd, loff_t from,
1535 size_t len, size_t *retlen, u_char *buf)
1537 return onenand_otp_walk(mtd, from, len, retlen, buf, do_otp_write, MTD_OTP_USER);
1541 * onenand_lock_user_prot_reg - [MTD Interface] Lock user OTP area
1542 * @param mtd MTD device structure
1543 * @param from The offset to lock
1544 * @param len number of bytes to unlock
1546 * Write lock mark on spare area in page 0 in OTP block
1548 static int onenand_lock_user_prot_reg(struct mtd_info *mtd, loff_t from,
1551 unsigned char oob_buf[64];
1555 memset(oob_buf, 0xff, mtd->oobsize);
1557 * Note: OTP lock operation
1558 * OTP block : 0xXXFC
1559 * 1st block : 0xXXF3 (If chip support)
1560 * Both : 0xXXF0 (If chip support)
1562 oob_buf[ONENAND_OTP_LOCK_OFFSET] = 0xFC;
1565 * Write lock mark to 8th word of sector0 of page0 of the spare0.
1566 * We write 16 bytes spare area instead of 2 bytes.
1571 ret = onenand_otp_walk(mtd, from, len, &retlen, oob_buf, do_otp_lock, MTD_OTP_USER);
1573 return ret ? : retlen;
1575 #endif /* CONFIG_MTD_ONENAND_OTP */
1578 * onenand_print_device_info - Print device ID
1579 * @param device device ID
1583 static void onenand_print_device_info(int device)
1585 int vcc, demuxed, ddp, density;
1587 vcc = device & ONENAND_DEVICE_VCC_MASK;
1588 demuxed = device & ONENAND_DEVICE_IS_DEMUX;
1589 ddp = device & ONENAND_DEVICE_IS_DDP;
1590 density = device >> ONENAND_DEVICE_DENSITY_SHIFT;
1591 printk(KERN_INFO "%sOneNAND%s %dMB %sV 16-bit (0x%02x)\n",
1592 demuxed ? "" : "Muxed ",
1595 vcc ? "2.65/3.3" : "1.8",
1599 static const struct onenand_manufacturers onenand_manuf_ids[] = {
1600 {ONENAND_MFR_SAMSUNG, "Samsung"},
1604 * onenand_check_maf - Check manufacturer ID
1605 * @param manuf manufacturer ID
1607 * Check manufacturer ID
1609 static int onenand_check_maf(int manuf)
1611 int size = ARRAY_SIZE(onenand_manuf_ids);
1615 for (i = 0; i < size; i++)
1616 if (manuf == onenand_manuf_ids[i].id)
1620 name = onenand_manuf_ids[i].name;
1624 printk(KERN_DEBUG "OneNAND Manufacturer: %s (0x%0x)\n", name, manuf);
1630 * onenand_probe - [OneNAND Interface] Probe the OneNAND device
1631 * @param mtd MTD device structure
1633 * OneNAND detection method:
1634 * Compare the the values from command with ones from register
1636 static int onenand_probe(struct mtd_info *mtd)
1638 struct onenand_chip *this = mtd->priv;
1639 int bram_maf_id, bram_dev_id, maf_id, dev_id;
1643 /* Send the command for reading device ID from BootRAM */
1644 this->write_word(ONENAND_CMD_READID, this->base + ONENAND_BOOTRAM);
1646 /* Read manufacturer and device IDs from BootRAM */
1647 bram_maf_id = this->read_word(this->base + ONENAND_BOOTRAM + 0x0);
1648 bram_dev_id = this->read_word(this->base + ONENAND_BOOTRAM + 0x2);
1650 /* Check manufacturer ID */
1651 if (onenand_check_maf(bram_maf_id))
1654 /* Reset OneNAND to read default register values */
1655 this->write_word(ONENAND_CMD_RESET, this->base + ONENAND_BOOTRAM);
1657 /* Read manufacturer and device IDs from Register */
1658 maf_id = this->read_word(this->base + ONENAND_REG_MANUFACTURER_ID);
1659 dev_id = this->read_word(this->base + ONENAND_REG_DEVICE_ID);
1661 /* Check OneNAND device */
1662 if (maf_id != bram_maf_id || dev_id != bram_dev_id)
1665 /* Flash device information */
1666 onenand_print_device_info(dev_id);
1667 this->device_id = dev_id;
1669 density = dev_id >> ONENAND_DEVICE_DENSITY_SHIFT;
1670 this->chipsize = (16 << density) << 20;
1671 /* Set density mask. it is used for DDP */
1672 this->density_mask = (1 << (density + 6));
1674 /* OneNAND page size & block size */
1675 /* The data buffer size is equal to page size */
1676 mtd->writesize = this->read_word(this->base + ONENAND_REG_DATA_BUFFER_SIZE);
1677 mtd->oobsize = mtd->writesize >> 5;
1678 /* Pagers per block is always 64 in OneNAND */
1679 mtd->erasesize = mtd->writesize << 6;
1681 this->erase_shift = ffs(mtd->erasesize) - 1;
1682 this->page_shift = ffs(mtd->writesize) - 1;
1683 this->ppb_shift = (this->erase_shift - this->page_shift);
1684 this->page_mask = (mtd->erasesize / mtd->writesize) - 1;
1686 /* REVIST: Multichip handling */
1688 mtd->size = this->chipsize;
1691 version_id = this->read_word(this->base + ONENAND_REG_VERSION_ID);
1692 printk(KERN_DEBUG "OneNAND version = 0x%04x\n", version_id);
1695 if (density <= ONENAND_DEVICE_DENSITY_512Mb &&
1696 !(version_id >> ONENAND_VERSION_PROCESS_SHIFT)) {
1697 printk(KERN_INFO "Lock scheme is Continues Lock\n");
1698 this->options |= ONENAND_CONT_LOCK;
1705 * onenand_suspend - [MTD Interface] Suspend the OneNAND flash
1706 * @param mtd MTD device structure
1708 static int onenand_suspend(struct mtd_info *mtd)
1710 return onenand_get_device(mtd, FL_PM_SUSPENDED);
1714 * onenand_resume - [MTD Interface] Resume the OneNAND flash
1715 * @param mtd MTD device structure
1717 static void onenand_resume(struct mtd_info *mtd)
1719 struct onenand_chip *this = mtd->priv;
1721 if (this->state == FL_PM_SUSPENDED)
1722 onenand_release_device(mtd);
1724 printk(KERN_ERR "resume() called for the chip which is not"
1725 "in suspended state\n");
1729 * onenand_scan - [OneNAND Interface] Scan for the OneNAND device
1730 * @param mtd MTD device structure
1731 * @param maxchips Number of chips to scan for
1733 * This fills out all the not initialized function pointers
1734 * with the defaults.
1735 * The flash ID is read and the mtd/chip structures are
1736 * filled with the appropriate values.
1738 int onenand_scan(struct mtd_info *mtd, int maxchips)
1740 struct onenand_chip *this = mtd->priv;
1742 if (!this->read_word)
1743 this->read_word = onenand_readw;
1744 if (!this->write_word)
1745 this->write_word = onenand_writew;
1748 this->command = onenand_command;
1750 this->wait = onenand_wait;
1752 if (!this->read_bufferram)
1753 this->read_bufferram = onenand_read_bufferram;
1754 if (!this->write_bufferram)
1755 this->write_bufferram = onenand_write_bufferram;
1757 if (!this->block_markbad)
1758 this->block_markbad = onenand_default_block_markbad;
1759 if (!this->scan_bbt)
1760 this->scan_bbt = onenand_default_bbt;
1762 if (onenand_probe(mtd))
1765 /* Set Sync. Burst Read after probing */
1766 if (this->mmcontrol) {
1767 printk(KERN_INFO "OneNAND Sync. Burst Read support\n");
1768 this->read_bufferram = onenand_sync_read_bufferram;
1771 /* Allocate buffers, if necessary */
1772 if (!this->page_buf) {
1774 len = mtd->writesize + mtd->oobsize;
1775 this->page_buf = kmalloc(len, GFP_KERNEL);
1776 if (!this->page_buf) {
1777 printk(KERN_ERR "onenand_scan(): Can't allocate page_buf\n");
1780 this->options |= ONENAND_PAGEBUF_ALLOC;
1783 this->state = FL_READY;
1784 init_waitqueue_head(&this->wq);
1785 spin_lock_init(&this->chip_lock);
1787 switch (mtd->oobsize) {
1789 this->autooob = &onenand_oob_64;
1793 this->autooob = &onenand_oob_32;
1797 printk(KERN_WARNING "No OOB scheme defined for oobsize %d\n",
1799 /* To prevent kernel oops */
1800 this->autooob = &onenand_oob_32;
1804 memcpy(&mtd->oobinfo, this->autooob, sizeof(mtd->oobinfo));
1806 /* Fill in remaining MTD driver data */
1807 mtd->type = MTD_NANDFLASH;
1808 mtd->flags = MTD_CAP_NANDFLASH;
1809 mtd->ecctype = MTD_ECC_SW;
1810 mtd->erase = onenand_erase;
1812 mtd->unpoint = NULL;
1813 mtd->read = onenand_read;
1814 mtd->write = onenand_write;
1815 mtd->read_ecc = onenand_read_ecc;
1816 mtd->write_ecc = onenand_write_ecc;
1817 mtd->read_oob = onenand_read_oob;
1818 mtd->write_oob = onenand_write_oob;
1819 #ifdef CONFIG_MTD_ONENAND_OTP
1820 mtd->get_fact_prot_info = onenand_get_fact_prot_info;
1821 mtd->read_fact_prot_reg = onenand_read_fact_prot_reg;
1822 mtd->get_user_prot_info = onenand_get_user_prot_info;
1823 mtd->read_user_prot_reg = onenand_read_user_prot_reg;
1824 mtd->write_user_prot_reg = onenand_write_user_prot_reg;
1825 mtd->lock_user_prot_reg = onenand_lock_user_prot_reg;
1827 mtd->sync = onenand_sync;
1829 mtd->unlock = onenand_unlock;
1830 mtd->suspend = onenand_suspend;
1831 mtd->resume = onenand_resume;
1832 mtd->block_isbad = onenand_block_isbad;
1833 mtd->block_markbad = onenand_block_markbad;
1834 mtd->owner = THIS_MODULE;
1836 /* Unlock whole block */
1837 mtd->unlock(mtd, 0x0, this->chipsize);
1839 return this->scan_bbt(mtd);
1843 * onenand_release - [OneNAND Interface] Free resources held by the OneNAND device
1844 * @param mtd MTD device structure
1846 void onenand_release(struct mtd_info *mtd)
1848 struct onenand_chip *this = mtd->priv;
1850 #ifdef CONFIG_MTD_PARTITIONS
1851 /* Deregister partitions */
1852 del_mtd_partitions (mtd);
1854 /* Deregister the device */
1855 del_mtd_device (mtd);
1857 /* Free bad block table memory, if allocated */
1860 /* Buffer allocated by onenand_scan */
1861 if (this->options & ONENAND_PAGEBUF_ALLOC)
1862 kfree(this->page_buf);
1865 EXPORT_SYMBOL_GPL(onenand_scan);
1866 EXPORT_SYMBOL_GPL(onenand_release);
1868 MODULE_LICENSE("GPL");
1869 MODULE_AUTHOR("Kyungmin Park <kyungmin.park@samsung.com>");
1870 MODULE_DESCRIPTION("Generic OneNAND flash driver code");