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.
14 #ifdef CONFIG_CMD_ONENAND
16 #include <linux/mtd/compat.h>
17 #include <linux/mtd/mtd.h>
18 #include <linux/mtd/onenand.h>
21 #include <asm/errno.h>
24 /* It should access 16-bit instead of 8-bit */
25 static inline void *memcpy_16(void *dst, const void *src, unsigned int len)
37 static const unsigned char ffchars[] = {
38 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
39 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 16 */
40 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
41 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 32 */
42 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
43 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 48 */
44 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
45 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 64 */
49 * onenand_readw - [OneNAND Interface] Read OneNAND register
50 * @param addr address to read
52 * Read OneNAND register
54 static unsigned short onenand_readw(void __iomem * addr)
60 * onenand_writew - [OneNAND Interface] Write OneNAND register with value
61 * @param value value to write
62 * @param addr address to write
64 * Write OneNAND register with value
66 static void onenand_writew(unsigned short value, void __iomem * addr)
72 * onenand_block_address - [DEFAULT] Get block address
73 * @param device the device id
74 * @param block the block
75 * @return translated block address if DDP, otherwise same
77 * Setup Start Address 1 Register (F100h)
79 static int onenand_block_address(int device, int block)
81 if (device & ONENAND_DEVICE_IS_DDP) {
82 /* Device Flash Core select, NAND Flash Block Address */
83 int dfs = 0, density, mask;
85 density = device >> ONENAND_DEVICE_DENSITY_SHIFT;
86 mask = (1 << (density + 6));
91 return (dfs << ONENAND_DDP_SHIFT) | (block & (mask - 1));
98 * onenand_bufferram_address - [DEFAULT] Get bufferram address
99 * @param device the device id
100 * @param block the block
101 * @return set DBS value if DDP, otherwise 0
103 * Setup Start Address 2 Register (F101h) for DDP
105 static int onenand_bufferram_address(int device, int block)
107 if (device & ONENAND_DEVICE_IS_DDP) {
108 /* Device BufferRAM Select */
109 int dbs = 0, density, mask;
111 density = device >> ONENAND_DEVICE_DENSITY_SHIFT;
112 mask = (1 << (density + 6));
117 return (dbs << ONENAND_DDP_SHIFT);
124 * onenand_page_address - [DEFAULT] Get page address
125 * @param page the page address
126 * @param sector the sector address
127 * @return combined page and sector address
129 * Setup Start Address 8 Register (F107h)
131 static int onenand_page_address(int page, int sector)
133 /* Flash Page Address, Flash Sector Address */
136 fpa = page & ONENAND_FPA_MASK;
137 fsa = sector & ONENAND_FSA_MASK;
139 return ((fpa << ONENAND_FPA_SHIFT) | fsa);
143 * onenand_buffer_address - [DEFAULT] Get buffer address
144 * @param dataram1 DataRAM index
145 * @param sectors the sector address
146 * @param count the number of sectors
147 * @return the start buffer value
149 * Setup Start Buffer Register (F200h)
151 static int onenand_buffer_address(int dataram1, int sectors, int count)
155 /* BufferRAM Sector Address */
156 bsa = sectors & ONENAND_BSA_MASK;
159 bsa |= ONENAND_BSA_DATARAM1; /* DataRAM1 */
161 bsa |= ONENAND_BSA_DATARAM0; /* DataRAM0 */
163 /* BufferRAM Sector Count */
164 bsc = count & ONENAND_BSC_MASK;
166 return ((bsa << ONENAND_BSA_SHIFT) | bsc);
170 * onenand_command - [DEFAULT] Send command to OneNAND device
171 * @param mtd MTD device structure
172 * @param cmd the command to be sent
173 * @param addr offset to read from or write to
174 * @param len number of bytes to read or write
176 * Send command to OneNAND device. This function is used for middle/large page
177 * devices (1KB/2KB Bytes per page)
179 static int onenand_command(struct mtd_info *mtd, int cmd, loff_t addr,
182 struct onenand_chip *this = mtd->priv;
183 int value, readcmd = 0;
185 /* Now we use page size operation */
186 int sectors = 4, count = 4;
188 /* Address translation */
190 case ONENAND_CMD_UNLOCK:
191 case ONENAND_CMD_LOCK:
192 case ONENAND_CMD_LOCK_TIGHT:
197 case ONENAND_CMD_ERASE:
198 case ONENAND_CMD_BUFFERRAM:
199 block = (int)(addr >> this->erase_shift);
204 block = (int)(addr >> this->erase_shift);
205 page = (int)(addr >> this->page_shift);
206 page &= this->page_mask;
210 /* NOTE: The setting order of the registers is very important! */
211 if (cmd == ONENAND_CMD_BUFFERRAM) {
212 /* Select DataRAM for DDP */
213 value = onenand_bufferram_address(this->device_id, block);
214 this->write_word(value,
215 this->base + ONENAND_REG_START_ADDRESS2);
217 /* Switch to the next data buffer */
218 ONENAND_SET_NEXT_BUFFERRAM(this);
224 /* Write 'DFS, FBA' of Flash */
225 value = onenand_block_address(this->device_id, block);
226 this->write_word(value,
227 this->base + ONENAND_REG_START_ADDRESS1);
234 case ONENAND_CMD_READ:
235 case ONENAND_CMD_READOOB:
236 dataram = ONENAND_SET_NEXT_BUFFERRAM(this);
241 dataram = ONENAND_CURRENT_BUFFERRAM(this);
245 /* Write 'FPA, FSA' of Flash */
246 value = onenand_page_address(page, sectors);
247 this->write_word(value,
248 this->base + ONENAND_REG_START_ADDRESS8);
250 /* Write 'BSA, BSC' of DataRAM */
251 value = onenand_buffer_address(dataram, sectors, count);
252 this->write_word(value, this->base + ONENAND_REG_START_BUFFER);
255 /* Select DataRAM for DDP */
257 onenand_bufferram_address(this->device_id, block);
258 this->write_word(value,
260 ONENAND_REG_START_ADDRESS2);
264 /* Interrupt clear */
265 this->write_word(ONENAND_INT_CLEAR, this->base + ONENAND_REG_INTERRUPT);
267 this->write_word(cmd, this->base + ONENAND_REG_COMMAND);
273 * onenand_wait - [DEFAULT] wait until the command is done
274 * @param mtd MTD device structure
275 * @param state state to select the max. timeout value
277 * Wait for command done. This applies to all OneNAND command
278 * Read can take up to 30us, erase up to 2ms and program up to 350us
279 * according to general OneNAND specs
281 static int onenand_wait(struct mtd_info *mtd, int state)
283 struct onenand_chip *this = mtd->priv;
284 unsigned int flags = ONENAND_INT_MASTER;
285 unsigned int interrupt = 0;
286 unsigned int ctrl, ecc;
289 interrupt = this->read_word(this->base + ONENAND_REG_INTERRUPT);
290 if (interrupt & flags)
294 ctrl = this->read_word(this->base + ONENAND_REG_CTRL_STATUS);
296 if (ctrl & ONENAND_CTRL_ERROR) {
297 MTDDEBUG (MTD_DEBUG_LEVEL0,
298 "onenand_wait: controller error = 0x%04x\n", ctrl);
302 if (ctrl & ONENAND_CTRL_LOCK) {
303 MTDDEBUG (MTD_DEBUG_LEVEL0,
304 "onenand_wait: it's locked error = 0x%04x\n", ctrl);
308 if (interrupt & ONENAND_INT_READ) {
309 ecc = this->read_word(this->base + ONENAND_REG_ECC_STATUS);
310 if (ecc & ONENAND_ECC_2BIT_ALL) {
311 MTDDEBUG (MTD_DEBUG_LEVEL0,
312 "onenand_wait: ECC error = 0x%04x\n", ecc);
321 * onenand_bufferram_offset - [DEFAULT] BufferRAM offset
322 * @param mtd MTD data structure
323 * @param area BufferRAM area
324 * @return offset given area
326 * Return BufferRAM offset given area
328 static inline int onenand_bufferram_offset(struct mtd_info *mtd, int area)
330 struct onenand_chip *this = mtd->priv;
332 if (ONENAND_CURRENT_BUFFERRAM(this)) {
333 if (area == ONENAND_DATARAM)
334 return mtd->writesize;
335 if (area == ONENAND_SPARERAM)
343 * onenand_read_bufferram - [OneNAND Interface] Read the bufferram area
344 * @param mtd MTD data structure
345 * @param area BufferRAM area
346 * @param buffer the databuffer to put/get data
347 * @param offset offset to read from or write to
348 * @param count number of bytes to read/write
350 * Read the BufferRAM area
352 static int onenand_read_bufferram(struct mtd_info *mtd, int area,
353 unsigned char *buffer, int offset,
356 struct onenand_chip *this = mtd->priv;
357 void __iomem *bufferram;
359 bufferram = this->base + area;
360 bufferram += onenand_bufferram_offset(mtd, area);
362 memcpy_16(buffer, bufferram + offset, count);
368 * onenand_sync_read_bufferram - [OneNAND Interface] Read the bufferram area with Sync. Burst mode
369 * @param mtd MTD data structure
370 * @param area BufferRAM area
371 * @param buffer the databuffer to put/get data
372 * @param offset offset to read from or write to
373 * @param count number of bytes to read/write
375 * Read the BufferRAM area with Sync. Burst Mode
377 static int onenand_sync_read_bufferram(struct mtd_info *mtd, int area,
378 unsigned char *buffer, int offset,
381 struct onenand_chip *this = mtd->priv;
382 void __iomem *bufferram;
384 bufferram = this->base + area;
385 bufferram += onenand_bufferram_offset(mtd, area);
387 this->mmcontrol(mtd, ONENAND_SYS_CFG1_SYNC_READ);
389 memcpy_16(buffer, bufferram + offset, count);
391 this->mmcontrol(mtd, 0);
397 * onenand_write_bufferram - [OneNAND Interface] Write the bufferram area
398 * @param mtd MTD data structure
399 * @param area BufferRAM area
400 * @param buffer the databuffer to put/get data
401 * @param offset offset to read from or write to
402 * @param count number of bytes to read/write
404 * Write the BufferRAM area
406 static int onenand_write_bufferram(struct mtd_info *mtd, int area,
407 const unsigned char *buffer, int offset,
410 struct onenand_chip *this = mtd->priv;
411 void __iomem *bufferram;
413 bufferram = this->base + area;
414 bufferram += onenand_bufferram_offset(mtd, area);
416 memcpy_16(bufferram + offset, buffer, count);
422 * onenand_check_bufferram - [GENERIC] Check BufferRAM information
423 * @param mtd MTD data structure
424 * @param addr address to check
425 * @return 1 if there are valid data, otherwise 0
427 * Check bufferram if there is data we required
429 static int onenand_check_bufferram(struct mtd_info *mtd, loff_t addr)
431 struct onenand_chip *this = mtd->priv;
435 block = (int)(addr >> this->erase_shift);
436 page = (int)(addr >> this->page_shift);
437 page &= this->page_mask;
439 i = ONENAND_CURRENT_BUFFERRAM(this);
441 /* Is there valid data? */
442 if (this->bufferram[i].block == block &&
443 this->bufferram[i].page == page && this->bufferram[i].valid)
450 * onenand_update_bufferram - [GENERIC] Update BufferRAM information
451 * @param mtd MTD data structure
452 * @param addr address to update
453 * @param valid valid flag
455 * Update BufferRAM information
457 static int onenand_update_bufferram(struct mtd_info *mtd, loff_t addr,
460 struct onenand_chip *this = mtd->priv;
464 block = (int)(addr >> this->erase_shift);
465 page = (int)(addr >> this->page_shift);
466 page &= this->page_mask;
468 /* Invalidate BufferRAM */
469 for (i = 0; i < MAX_BUFFERRAM; i++) {
470 if (this->bufferram[i].block == block &&
471 this->bufferram[i].page == page)
472 this->bufferram[i].valid = 0;
475 /* Update BufferRAM */
476 i = ONENAND_CURRENT_BUFFERRAM(this);
477 this->bufferram[i].block = block;
478 this->bufferram[i].page = page;
479 this->bufferram[i].valid = valid;
485 * onenand_invalidate_bufferram - [GENERIC] Invalidate BufferRAM information
486 * @param mtd MTD data structure
487 * @param addr start address to invalidate
488 * @param len length to invalidate
490 * Invalidate BufferRAM information
492 static void onenand_invalidate_bufferram(struct mtd_info *mtd, loff_t addr,
495 struct onenand_chip *this = mtd->priv;
497 loff_t end_addr = addr + len;
499 /* Invalidate BufferRAM */
500 for (i = 0; i < MAX_BUFFERRAM; i++) {
501 loff_t buf_addr = this->bufferram[i].block << this->erase_shift;
503 if (buf_addr >= addr && buf_addr < end_addr)
504 this->bufferram[i].valid = 0;
509 * onenand_get_device - [GENERIC] Get chip for selected access
510 * @param mtd MTD device structure
511 * @param new_state the state which is requested
513 * Get the device and lock it for exclusive access
515 static void onenand_get_device(struct mtd_info *mtd, int new_state)
521 * onenand_release_device - [GENERIC] release chip
522 * @param mtd MTD device structure
524 * Deselect, release chip lock and wake up anyone waiting on the device
526 static void onenand_release_device(struct mtd_info *mtd)
532 * onenand_read_ecc - [MTD Interface] Read data with ECC
533 * @param mtd MTD device structure
534 * @param from offset to read from
535 * @param len number of bytes to read
536 * @param retlen pointer to variable to store the number of read bytes
537 * @param buf the databuffer to put data
538 * @param oob_buf filesystem supplied oob data buffer
539 * @param oobsel oob selection structure
541 * OneNAND read with ECC
543 static int onenand_read_ecc(struct mtd_info *mtd, loff_t from, size_t len,
544 size_t * retlen, u_char * buf,
545 u_char * oob_buf, struct nand_oobinfo *oobsel)
547 struct onenand_chip *this = mtd->priv;
548 int read = 0, column;
552 MTDDEBUG (MTD_DEBUG_LEVEL3, "onenand_read_ecc: "
553 "from = 0x%08x, len = %i\n",
554 (unsigned int)from, (int)len);
556 /* Do not allow reads past end of device */
557 if ((from + len) > mtd->size) {
558 MTDDEBUG (MTD_DEBUG_LEVEL0, "onenand_read_ecc: "
559 "Attempt read beyond end of device\n");
564 /* Grab the lock and see if the device is available */
565 onenand_get_device(mtd, FL_READING);
568 thislen = min_t(int, mtd->writesize, len - read);
570 column = from & (mtd->writesize - 1);
571 if (column + thislen > mtd->writesize)
572 thislen = mtd->writesize - column;
574 if (!onenand_check_bufferram(mtd, from)) {
575 this->command(mtd, ONENAND_CMD_READ, from,
577 ret = this->wait(mtd, FL_READING);
578 /* First copy data and check return value for ECC handling */
579 onenand_update_bufferram(mtd, from, 1);
582 this->read_bufferram(mtd, ONENAND_DATARAM, buf, column,
590 MTDDEBUG (MTD_DEBUG_LEVEL0,
591 "onenand_read_ecc: read failed = %d\n", ret);
599 /* Deselect and wake up anyone waiting on the device */
600 onenand_release_device(mtd);
603 * Return success, if no ECC failures, else -EBADMSG
604 * fs driver will take care of that, because
605 * retlen == desired len and result == -EBADMSG
612 * onenand_read - [MTD Interface] MTD compability function for onenand_read_ecc
613 * @param mtd MTD device structure
614 * @param from offset to read from
615 * @param len number of bytes to read
616 * @param retlen pointer to variable to store the number of read bytes
617 * @param buf the databuffer to put data
619 * This function simply calls onenand_read_ecc with oob buffer and oobsel = NULL
621 int onenand_read(struct mtd_info *mtd, loff_t from, size_t len,
622 size_t * retlen, u_char * buf)
624 return onenand_read_ecc(mtd, from, len, retlen, buf, NULL, NULL);
628 * onenand_read_oob - [MTD Interface] OneNAND read out-of-band
629 * @param mtd MTD device structure
630 * @param from offset to read from
631 * @param len number of bytes to read
632 * @param retlen pointer to variable to store the number of read bytes
633 * @param buf the databuffer to put data
635 * OneNAND read out-of-band data from the spare area
637 int onenand_read_oob(struct mtd_info *mtd, loff_t from, size_t len,
638 size_t * retlen, u_char * buf)
640 struct onenand_chip *this = mtd->priv;
641 int read = 0, thislen, column;
644 MTDDEBUG (MTD_DEBUG_LEVEL3, "onenand_read_oob: "
645 "from = 0x%08x, len = %i\n",
646 (unsigned int)from, (int)len);
648 /* Initialize return length value */
651 /* Do not allow reads past end of device */
652 if (unlikely((from + len) > mtd->size)) {
653 MTDDEBUG (MTD_DEBUG_LEVEL0, "onenand_read_oob: "
654 "Attempt read beyond end of device\n");
658 /* Grab the lock and see if the device is available */
659 onenand_get_device(mtd, FL_READING);
661 column = from & (mtd->oobsize - 1);
664 thislen = mtd->oobsize - column;
665 thislen = min_t(int, thislen, len);
667 this->command(mtd, ONENAND_CMD_READOOB, from, mtd->oobsize);
669 onenand_update_bufferram(mtd, from, 0);
671 ret = this->wait(mtd, FL_READING);
672 /* First copy data and check return value for ECC handling */
674 this->read_bufferram(mtd, ONENAND_SPARERAM, buf, column,
682 MTDDEBUG (MTD_DEBUG_LEVEL0,
683 "onenand_read_oob: read failed = %d\n", ret);
691 from += mtd->writesize;
696 /* Deselect and wake up anyone waiting on the device */
697 onenand_release_device(mtd);
703 #ifdef CONFIG_MTD_ONENAND_VERIFY_WRITE
705 * onenand_verify_page - [GENERIC] verify the chip contents after a write
706 * @param mtd MTD device structure
707 * @param buf the databuffer to verify
709 * Check DataRAM area directly
711 static int onenand_verify_page(struct mtd_info *mtd, u_char * buf,
714 struct onenand_chip *this = mtd->priv;
715 void __iomem *dataram0, *dataram1;
718 this->command(mtd, ONENAND_CMD_READ, addr, mtd->writesize);
720 ret = this->wait(mtd, FL_READING);
724 onenand_update_bufferram(mtd, addr, 1);
726 /* Check, if the two dataram areas are same */
727 dataram0 = this->base + ONENAND_DATARAM;
728 dataram1 = dataram0 + mtd->writesize;
730 if (memcmp(dataram0, dataram1, mtd->writesize))
736 #define onenand_verify_page(...) (0)
739 #define NOTALIGNED(x) ((x & (mtd->writesize - 1)) != 0)
742 * onenand_write_ecc - [MTD Interface] OneNAND write with ECC
743 * @param mtd MTD device structure
744 * @param to offset to write to
745 * @param len number of bytes to write
746 * @param retlen pointer to variable to store the number of written bytes
747 * @param buf the data to write
748 * @param eccbuf filesystem supplied oob data buffer
749 * @param oobsel oob selection structure
751 * OneNAND write with ECC
753 static int onenand_write_ecc(struct mtd_info *mtd, loff_t to, size_t len,
754 size_t * retlen, const u_char * buf,
755 u_char * eccbuf, struct nand_oobinfo *oobsel)
757 struct onenand_chip *this = mtd->priv;
761 MTDDEBUG (MTD_DEBUG_LEVEL3, "onenand_write_ecc: "
762 "to = 0x%08x, len = %i\n",
763 (unsigned int)to, (int)len);
765 /* Initialize retlen, in case of early exit */
768 /* Do not allow writes past end of device */
769 if (unlikely((to + len) > mtd->size)) {
770 MTDDEBUG (MTD_DEBUG_LEVEL0, "onenand_write_ecc: "
771 "Attempt write to past end of device\n");
775 /* Reject writes, which are not page aligned */
776 if (unlikely(NOTALIGNED(to)) || unlikely(NOTALIGNED(len))) {
777 MTDDEBUG (MTD_DEBUG_LEVEL0, "onenand_write_ecc: "
778 "Attempt to write not page aligned data\n");
782 /* Grab the lock and see if the device is available */
783 onenand_get_device(mtd, FL_WRITING);
785 /* Loop until all data write */
786 while (written < len) {
787 int thislen = min_t(int, mtd->writesize, len - written);
789 this->command(mtd, ONENAND_CMD_BUFFERRAM, to, mtd->writesize);
791 this->write_bufferram(mtd, ONENAND_DATARAM, buf, 0, thislen);
792 this->write_bufferram(mtd, ONENAND_SPARERAM, ffchars, 0,
795 this->command(mtd, ONENAND_CMD_PROG, to, mtd->writesize);
797 onenand_update_bufferram(mtd, to, 1);
799 ret = this->wait(mtd, FL_WRITING);
801 MTDDEBUG (MTD_DEBUG_LEVEL0,
802 "onenand_write_ecc: write filaed %d\n", ret);
808 /* Only check verify write turn on */
809 ret = onenand_verify_page(mtd, (u_char *) buf, to);
811 MTDDEBUG (MTD_DEBUG_LEVEL0,
812 "onenand_write_ecc: verify failed %d\n", ret);
823 /* Deselect and wake up anyone waiting on the device */
824 onenand_release_device(mtd);
832 * onenand_write - [MTD Interface] compability function for onenand_write_ecc
833 * @param mtd MTD device structure
834 * @param to offset to write to
835 * @param len number of bytes to write
836 * @param retlen pointer to variable to store the number of written bytes
837 * @param buf the data to write
839 * This function simply calls onenand_write_ecc
840 * with oob buffer and oobsel = NULL
842 int onenand_write(struct mtd_info *mtd, loff_t to, size_t len,
843 size_t * retlen, const u_char * buf)
845 return onenand_write_ecc(mtd, to, len, retlen, buf, NULL, NULL);
849 * onenand_write_oob - [MTD Interface] OneNAND write out-of-band
850 * @param mtd MTD device structure
851 * @param to offset to write to
852 * @param len number of bytes to write
853 * @param retlen pointer to variable to store the number of written bytes
854 * @param buf the data to write
856 * OneNAND write out-of-band
858 int onenand_write_oob(struct mtd_info *mtd, loff_t to, size_t len,
859 size_t * retlen, const u_char * buf)
861 struct onenand_chip *this = mtd->priv;
865 MTDDEBUG (MTD_DEBUG_LEVEL3, "onenand_write_oob: "
866 "to = 0x%08x, len = %i\n",
867 (unsigned int)to, (int)len);
869 /* Initialize retlen, in case of early exit */
872 /* Do not allow writes past end of device */
873 if (unlikely((to + len) > mtd->size)) {
874 MTDDEBUG (MTD_DEBUG_LEVEL0, "onenand_write_oob: "
875 "Attempt write to past end of device\n");
879 /* Grab the lock and see if the device is available */
880 onenand_get_device(mtd, FL_WRITING);
882 /* Loop until all data write */
883 while (written < len) {
884 int thislen = min_t(int, mtd->oobsize, len - written);
886 column = to & (mtd->oobsize - 1);
888 this->command(mtd, ONENAND_CMD_BUFFERRAM, to, mtd->oobsize);
890 this->write_bufferram(mtd, ONENAND_SPARERAM, ffchars, 0,
892 this->write_bufferram(mtd, ONENAND_SPARERAM, buf, column,
895 this->command(mtd, ONENAND_CMD_PROGOOB, to, mtd->oobsize);
897 onenand_update_bufferram(mtd, to, 0);
899 status = this->wait(mtd, FL_WRITING);
911 /* Deselect and wake up anyone waiting on the device */
912 onenand_release_device(mtd);
920 * onenand_block_isbad_nolock - [GENERIC] Check if a block is marked bad
921 * @param mtd MTD device structure
922 * @param ofs offset from device start
923 * @param allowbbt 1, if its allowed to access the bbt area
925 * Check, if the block is bad, Either by reading the bad block table or
926 * calling of the scan function.
928 static int onenand_block_isbad_nolock(struct mtd_info *mtd, loff_t ofs, int allowbbt)
930 struct onenand_chip *this = mtd->priv;
931 struct bbm_info *bbm = this->bbm;
933 /* Return info from the table */
934 return bbm->isbad_bbt(mtd, ofs, allowbbt);
939 * onenand_erase - [MTD Interface] erase block(s)
940 * @param mtd MTD device structure
941 * @param instr erase instruction
943 * Erase one ore more blocks
945 int onenand_erase(struct mtd_info *mtd, struct erase_info *instr)
947 struct onenand_chip *this = mtd->priv;
948 unsigned int block_size;
953 MTDDEBUG (MTD_DEBUG_LEVEL3, "onenand_erase: start = 0x%08x, len = %i\n",
954 (unsigned int)instr->addr, (unsigned int)instr->len);
956 block_size = (1 << this->erase_shift);
958 /* Start address must align on block boundary */
959 if (unlikely(instr->addr & (block_size - 1))) {
960 MTDDEBUG (MTD_DEBUG_LEVEL0,
961 "onenand_erase: Unaligned address\n");
965 /* Length must align on block boundary */
966 if (unlikely(instr->len & (block_size - 1))) {
967 MTDDEBUG (MTD_DEBUG_LEVEL0,
968 "onenand_erase: Length not block aligned\n");
972 /* Do not allow erase past end of device */
973 if (unlikely((instr->len + instr->addr) > mtd->size)) {
974 MTDDEBUG (MTD_DEBUG_LEVEL0,
975 "onenand_erase: Erase past end of device\n");
979 instr->fail_addr = 0xffffffff;
981 /* Grab the lock and see if the device is available */
982 onenand_get_device(mtd, FL_ERASING);
984 /* Loop throught the pages */
988 instr->state = MTD_ERASING;
992 /* TODO Check badblock */
994 this->command(mtd, ONENAND_CMD_ERASE, addr, block_size);
996 onenand_invalidate_bufferram(mtd, addr, block_size);
998 ret = this->wait(mtd, FL_ERASING);
999 /* Check, if it is write protected */
1002 MTDDEBUG (MTD_DEBUG_LEVEL0, "onenand_erase: "
1003 "Device is write protected!!!\n");
1005 MTDDEBUG (MTD_DEBUG_LEVEL0, "onenand_erase: "
1006 "Failed erase, block %d\n",
1007 (unsigned)(addr >> this->erase_shift));
1008 instr->state = MTD_ERASE_FAILED;
1009 instr->fail_addr = addr;
1017 instr->state = MTD_ERASE_DONE;
1021 ret = instr->state == MTD_ERASE_DONE ? 0 : -EIO;
1022 /* Do call back function */
1024 mtd_erase_callback(instr);
1026 /* Deselect and wake up anyone waiting on the device */
1027 onenand_release_device(mtd);
1033 * onenand_sync - [MTD Interface] sync
1034 * @param mtd MTD device structure
1036 * Sync is actually a wait for chip ready function
1038 void onenand_sync(struct mtd_info *mtd)
1040 MTDDEBUG (MTD_DEBUG_LEVEL3, "onenand_sync: called\n");
1042 /* Grab the lock and see if the device is available */
1043 onenand_get_device(mtd, FL_SYNCING);
1045 /* Release it and go back */
1046 onenand_release_device(mtd);
1050 * onenand_block_isbad - [MTD Interface] Check whether the block at the given offset is bad
1051 * @param mtd MTD device structure
1052 * @param ofs offset relative to mtd start
1054 * Check whether the block is bad
1056 int onenand_block_isbad(struct mtd_info *mtd, loff_t ofs)
1060 /* Check for invalid offset */
1061 if (ofs > mtd->size)
1064 onenand_get_device(mtd, FL_READING);
1065 ret = onenand_block_isbad_nolock(mtd,ofs, 0);
1066 onenand_release_device(mtd);
1071 * onenand_block_markbad - [MTD Interface] Mark the block at the given offset as bad
1072 * @param mtd MTD device structure
1073 * @param ofs offset relative to mtd start
1075 * Mark the block as bad
1077 int onenand_block_markbad(struct mtd_info *mtd, loff_t ofs)
1079 struct onenand_chip *this = mtd->priv;
1082 ret = onenand_block_isbad(mtd, ofs);
1084 /* If it was bad already, return success and do nothing */
1090 ret = this->block_markbad(mtd, ofs);
1095 * onenand_unlock - [MTD Interface] Unlock block(s)
1096 * @param mtd MTD device structure
1097 * @param ofs offset relative to mtd start
1098 * @param len number of bytes to unlock
1100 * Unlock one or more blocks
1102 int onenand_unlock(struct mtd_info *mtd, loff_t ofs, size_t len)
1104 struct onenand_chip *this = mtd->priv;
1105 int start, end, block, value, status;
1107 start = ofs >> this->erase_shift;
1108 end = len >> this->erase_shift;
1110 /* Continuous lock scheme */
1111 if (this->options & ONENAND_CONT_LOCK) {
1112 /* Set start block address */
1113 this->write_word(start,
1114 this->base + ONENAND_REG_START_BLOCK_ADDRESS);
1115 /* Set end block address */
1116 this->write_word(end - 1,
1117 this->base + ONENAND_REG_END_BLOCK_ADDRESS);
1118 /* Write unlock command */
1119 this->command(mtd, ONENAND_CMD_UNLOCK, 0, 0);
1121 /* There's no return value */
1122 this->wait(mtd, FL_UNLOCKING);
1125 while (this->read_word(this->base + ONENAND_REG_CTRL_STATUS)
1126 & ONENAND_CTRL_ONGO)
1129 /* Check lock status */
1130 status = this->read_word(this->base + ONENAND_REG_WP_STATUS);
1131 if (!(status & ONENAND_WP_US))
1132 printk(KERN_ERR "wp status = 0x%x\n", status);
1137 /* Block lock scheme */
1138 for (block = start; block < end; block++) {
1139 /* Set start block address */
1140 this->write_word(block,
1141 this->base + ONENAND_REG_START_BLOCK_ADDRESS);
1142 /* Write unlock command */
1143 this->command(mtd, ONENAND_CMD_UNLOCK, 0, 0);
1145 /* There's no return value */
1146 this->wait(mtd, FL_UNLOCKING);
1149 while (this->read_word(this->base + ONENAND_REG_CTRL_STATUS)
1150 & ONENAND_CTRL_ONGO)
1153 /* Set block address for read block status */
1154 value = onenand_block_address(this->device_id, block);
1155 this->write_word(value,
1156 this->base + ONENAND_REG_START_ADDRESS1);
1158 /* Check lock status */
1159 status = this->read_word(this->base + ONENAND_REG_WP_STATUS);
1160 if (!(status & ONENAND_WP_US))
1161 printk(KERN_ERR "block = %d, wp status = 0x%x\n",
1169 * onenand_print_device_info - Print device ID
1170 * @param device device ID
1174 char * onenand_print_device_info(int device)
1176 int vcc, demuxed, ddp, density;
1177 char *dev_info = malloc(80);
1179 vcc = device & ONENAND_DEVICE_VCC_MASK;
1180 demuxed = device & ONENAND_DEVICE_IS_DEMUX;
1181 ddp = device & ONENAND_DEVICE_IS_DDP;
1182 density = device >> ONENAND_DEVICE_DENSITY_SHIFT;
1183 sprintf(dev_info, "%sOneNAND%s %dMB %sV 16-bit (0x%02x)",
1184 demuxed ? "" : "Muxed ",
1186 (16 << density), vcc ? "2.65/3.3" : "1.8", device);
1191 static const struct onenand_manufacturers onenand_manuf_ids[] = {
1192 {ONENAND_MFR_SAMSUNG, "Samsung"},
1193 {ONENAND_MFR_UNKNOWN, "Unknown"}
1197 * onenand_check_maf - Check manufacturer ID
1198 * @param manuf manufacturer ID
1200 * Check manufacturer ID
1202 static int onenand_check_maf(int manuf)
1206 for (i = 0; onenand_manuf_ids[i].id; i++) {
1207 if (manuf == onenand_manuf_ids[i].id)
1211 #ifdef ONENAND_DEBUG
1212 printk(KERN_DEBUG "OneNAND Manufacturer: %s (0x%0x)\n",
1213 onenand_manuf_ids[i].name, manuf);
1216 return (i != ONENAND_MFR_UNKNOWN);
1220 * onenand_probe - [OneNAND Interface] Probe the OneNAND device
1221 * @param mtd MTD device structure
1223 * OneNAND detection method:
1224 * Compare the the values from command with ones from register
1226 static int onenand_probe(struct mtd_info *mtd)
1228 struct onenand_chip *this = mtd->priv;
1229 int bram_maf_id, bram_dev_id, maf_id, dev_id;
1233 /* Send the command for reading device ID from BootRAM */
1234 this->write_word(ONENAND_CMD_READID, this->base + ONENAND_BOOTRAM);
1236 /* Read manufacturer and device IDs from BootRAM */
1237 bram_maf_id = this->read_word(this->base + ONENAND_BOOTRAM + 0x0);
1238 bram_dev_id = this->read_word(this->base + ONENAND_BOOTRAM + 0x2);
1240 /* Check manufacturer ID */
1241 if (onenand_check_maf(bram_maf_id))
1244 /* Reset OneNAND to read default register values */
1245 this->write_word(ONENAND_CMD_RESET, this->base + ONENAND_BOOTRAM);
1248 this->wait(mtd, FL_RESETING);
1250 /* Read manufacturer and device IDs from Register */
1251 maf_id = this->read_word(this->base + ONENAND_REG_MANUFACTURER_ID);
1252 dev_id = this->read_word(this->base + ONENAND_REG_DEVICE_ID);
1254 /* Check OneNAND device */
1255 if (maf_id != bram_maf_id || dev_id != bram_dev_id)
1258 /* FIXME : Current OneNAND MTD doesn't support Flex-OneNAND */
1259 if (dev_id & (1 << 9)) {
1260 printk("Not yet support Flex-OneNAND\n");
1264 /* Flash device information */
1265 mtd->name = onenand_print_device_info(dev_id);
1266 this->device_id = dev_id;
1268 density = dev_id >> ONENAND_DEVICE_DENSITY_SHIFT;
1269 this->chipsize = (16 << density) << 20;
1271 /* OneNAND page size & block size */
1272 /* The data buffer size is equal to page size */
1274 this->read_word(this->base + ONENAND_REG_DATA_BUFFER_SIZE);
1275 mtd->oobsize = mtd->writesize >> 5;
1276 /* Pagers per block is always 64 in OneNAND */
1277 mtd->erasesize = mtd->writesize << 6;
1279 this->erase_shift = ffs(mtd->erasesize) - 1;
1280 this->page_shift = ffs(mtd->writesize) - 1;
1281 this->ppb_shift = (this->erase_shift - this->page_shift);
1282 this->page_mask = (mtd->erasesize / mtd->writesize) - 1;
1284 /* REVIST: Multichip handling */
1286 mtd->size = this->chipsize;
1289 version_id = this->read_word(this->base + ONENAND_REG_VERSION_ID);
1290 #ifdef ONENAND_DEBUG
1291 printk(KERN_DEBUG "OneNAND version = 0x%04x\n", version_id);
1295 if (density <= ONENAND_DEVICE_DENSITY_512Mb &&
1296 !(version_id >> ONENAND_VERSION_PROCESS_SHIFT)) {
1297 printk(KERN_INFO "Lock scheme is Continues Lock\n");
1298 this->options |= ONENAND_CONT_LOCK;
1301 mtd->flags = MTD_CAP_NANDFLASH;
1302 mtd->erase = onenand_erase;
1303 mtd->read = onenand_read;
1304 mtd->write = onenand_write;
1305 mtd->read_oob = onenand_read_oob;
1306 mtd->write_oob = onenand_write_oob;
1307 mtd->sync = onenand_sync;
1308 mtd->block_isbad = onenand_block_isbad;
1309 mtd->block_markbad = onenand_block_markbad;
1315 * onenand_scan - [OneNAND Interface] Scan for the OneNAND device
1316 * @param mtd MTD device structure
1317 * @param maxchips Number of chips to scan for
1319 * This fills out all the not initialized function pointers
1320 * with the defaults.
1321 * The flash ID is read and the mtd/chip structures are
1322 * filled with the appropriate values.
1324 int onenand_scan(struct mtd_info *mtd, int maxchips)
1326 struct onenand_chip *this = mtd->priv;
1328 if (!this->read_word)
1329 this->read_word = onenand_readw;
1330 if (!this->write_word)
1331 this->write_word = onenand_writew;
1334 this->command = onenand_command;
1336 this->wait = onenand_wait;
1338 if (!this->read_bufferram)
1339 this->read_bufferram = onenand_read_bufferram;
1340 if (!this->write_bufferram)
1341 this->write_bufferram = onenand_write_bufferram;
1343 if (onenand_probe(mtd))
1346 /* Set Sync. Burst Read after probing */
1347 if (this->mmcontrol) {
1348 printk(KERN_INFO "OneNAND Sync. Burst Read support\n");
1349 this->read_bufferram = onenand_sync_read_bufferram;
1352 onenand_unlock(mtd, 0, mtd->size);
1354 return onenand_default_bbt(mtd);
1358 * onenand_release - [OneNAND Interface] Free resources held by the OneNAND device
1359 * @param mtd MTD device structure
1361 void onenand_release(struct mtd_info *mtd)
1365 #endif /* CONFIG_CMD_ONENAND */