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->oobblock;
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_get_device - [GENERIC] Get chip for selected access
486 * @param mtd MTD device structure
487 * @param new_state the state which is requested
489 * Get the device and lock it for exclusive access
491 static void onenand_get_device(struct mtd_info *mtd, int new_state)
497 * onenand_release_device - [GENERIC] release chip
498 * @param mtd MTD device structure
500 * Deselect, release chip lock and wake up anyone waiting on the device
502 static void onenand_release_device(struct mtd_info *mtd)
508 * onenand_read_ecc - [MTD Interface] Read data with ECC
509 * @param mtd MTD device structure
510 * @param from offset to read from
511 * @param len number of bytes to read
512 * @param retlen pointer to variable to store the number of read bytes
513 * @param buf the databuffer to put data
514 * @param oob_buf filesystem supplied oob data buffer
515 * @param oobsel oob selection structure
517 * OneNAND read with ECC
519 static int onenand_read_ecc(struct mtd_info *mtd, loff_t from, size_t len,
520 size_t * retlen, u_char * buf,
521 u_char * oob_buf, struct nand_oobinfo *oobsel)
523 struct onenand_chip *this = mtd->priv;
524 int read = 0, column;
528 MTDDEBUG (MTD_DEBUG_LEVEL3, "onenand_read_ecc: "
529 "from = 0x%08x, len = %i\n",
530 (unsigned int)from, (int)len);
532 /* Do not allow reads past end of device */
533 if ((from + len) > mtd->size) {
534 MTDDEBUG (MTD_DEBUG_LEVEL0, "onenand_read_ecc: "
535 "Attempt read beyond end of device\n");
540 /* Grab the lock and see if the device is available */
541 onenand_get_device(mtd, FL_READING);
544 thislen = min_t(int, mtd->oobblock, len - read);
546 column = from & (mtd->oobblock - 1);
547 if (column + thislen > mtd->oobblock)
548 thislen = mtd->oobblock - column;
550 if (!onenand_check_bufferram(mtd, from)) {
551 this->command(mtd, ONENAND_CMD_READ, from,
553 ret = this->wait(mtd, FL_READING);
554 /* First copy data and check return value for ECC handling */
555 onenand_update_bufferram(mtd, from, 1);
558 this->read_bufferram(mtd, ONENAND_DATARAM, buf, column,
566 MTDDEBUG (MTD_DEBUG_LEVEL0,
567 "onenand_read_ecc: read failed = %d\n", ret);
575 /* Deselect and wake up anyone waiting on the device */
576 onenand_release_device(mtd);
579 * Return success, if no ECC failures, else -EBADMSG
580 * fs driver will take care of that, because
581 * retlen == desired len and result == -EBADMSG
588 * onenand_read - [MTD Interface] MTD compability function for onenand_read_ecc
589 * @param mtd MTD device structure
590 * @param from offset to read from
591 * @param len number of bytes to read
592 * @param retlen pointer to variable to store the number of read bytes
593 * @param buf the databuffer to put data
595 * This function simply calls onenand_read_ecc with oob buffer and oobsel = NULL
597 int onenand_read(struct mtd_info *mtd, loff_t from, size_t len,
598 size_t * retlen, u_char * buf)
600 return onenand_read_ecc(mtd, from, len, retlen, buf, NULL, NULL);
604 * onenand_read_oob - [MTD Interface] OneNAND read out-of-band
605 * @param mtd MTD device structure
606 * @param from offset to read from
607 * @param len number of bytes to read
608 * @param retlen pointer to variable to store the number of read bytes
609 * @param buf the databuffer to put data
611 * OneNAND read out-of-band data from the spare area
613 int onenand_read_oob(struct mtd_info *mtd, loff_t from, size_t len,
614 size_t * retlen, u_char * buf)
616 struct onenand_chip *this = mtd->priv;
617 int read = 0, thislen, column;
620 MTDDEBUG (MTD_DEBUG_LEVEL3, "onenand_read_oob: "
621 "from = 0x%08x, len = %i\n",
622 (unsigned int)from, (int)len);
624 /* Initialize return length value */
627 /* Do not allow reads past end of device */
628 if (unlikely((from + len) > mtd->size)) {
629 MTDDEBUG (MTD_DEBUG_LEVEL0, "onenand_read_oob: "
630 "Attempt read beyond end of device\n");
634 /* Grab the lock and see if the device is available */
635 onenand_get_device(mtd, FL_READING);
637 column = from & (mtd->oobsize - 1);
640 thislen = mtd->oobsize - column;
641 thislen = min_t(int, thislen, len);
643 this->command(mtd, ONENAND_CMD_READOOB, from, mtd->oobsize);
645 onenand_update_bufferram(mtd, from, 0);
647 ret = this->wait(mtd, FL_READING);
648 /* First copy data and check return value for ECC handling */
650 this->read_bufferram(mtd, ONENAND_SPARERAM, buf, column,
658 MTDDEBUG (MTD_DEBUG_LEVEL0,
659 "onenand_read_oob: read failed = %d\n", ret);
667 from += mtd->oobblock;
672 /* Deselect and wake up anyone waiting on the device */
673 onenand_release_device(mtd);
679 #ifdef CONFIG_MTD_ONENAND_VERIFY_WRITE
681 * onenand_verify_page - [GENERIC] verify the chip contents after a write
682 * @param mtd MTD device structure
683 * @param buf the databuffer to verify
685 * Check DataRAM area directly
687 static int onenand_verify_page(struct mtd_info *mtd, u_char * buf,
690 struct onenand_chip *this = mtd->priv;
691 void __iomem *dataram0, *dataram1;
694 this->command(mtd, ONENAND_CMD_READ, addr, mtd->oobblock);
696 ret = this->wait(mtd, FL_READING);
700 onenand_update_bufferram(mtd, addr, 1);
702 /* Check, if the two dataram areas are same */
703 dataram0 = this->base + ONENAND_DATARAM;
704 dataram1 = dataram0 + mtd->oobblock;
706 if (memcmp(dataram0, dataram1, mtd->oobblock))
712 #define onenand_verify_page(...) (0)
715 #define NOTALIGNED(x) ((x & (mtd->oobblock - 1)) != 0)
718 * onenand_write_ecc - [MTD Interface] OneNAND write with ECC
719 * @param mtd MTD device structure
720 * @param to offset to write to
721 * @param len number of bytes to write
722 * @param retlen pointer to variable to store the number of written bytes
723 * @param buf the data to write
724 * @param eccbuf filesystem supplied oob data buffer
725 * @param oobsel oob selection structure
727 * OneNAND write with ECC
729 static int onenand_write_ecc(struct mtd_info *mtd, loff_t to, size_t len,
730 size_t * retlen, const u_char * buf,
731 u_char * eccbuf, struct nand_oobinfo *oobsel)
733 struct onenand_chip *this = mtd->priv;
737 MTDDEBUG (MTD_DEBUG_LEVEL3, "onenand_write_ecc: "
738 "to = 0x%08x, len = %i\n",
739 (unsigned int)to, (int)len);
741 /* Initialize retlen, in case of early exit */
744 /* Do not allow writes past end of device */
745 if (unlikely((to + len) > mtd->size)) {
746 MTDDEBUG (MTD_DEBUG_LEVEL0, "onenand_write_ecc: "
747 "Attempt write to past end of device\n");
751 /* Reject writes, which are not page aligned */
752 if (unlikely(NOTALIGNED(to)) || unlikely(NOTALIGNED(len))) {
753 MTDDEBUG (MTD_DEBUG_LEVEL0, "onenand_write_ecc: "
754 "Attempt to write not page aligned data\n");
758 /* Grab the lock and see if the device is available */
759 onenand_get_device(mtd, FL_WRITING);
761 /* Loop until all data write */
762 while (written < len) {
763 int thislen = min_t(int, mtd->oobblock, len - written);
765 this->command(mtd, ONENAND_CMD_BUFFERRAM, to, mtd->oobblock);
767 this->write_bufferram(mtd, ONENAND_DATARAM, buf, 0, thislen);
768 this->write_bufferram(mtd, ONENAND_SPARERAM, ffchars, 0,
771 this->command(mtd, ONENAND_CMD_PROG, to, mtd->oobblock);
773 onenand_update_bufferram(mtd, to, 1);
775 ret = this->wait(mtd, FL_WRITING);
777 MTDDEBUG (MTD_DEBUG_LEVEL0,
778 "onenand_write_ecc: write filaed %d\n", ret);
784 /* Only check verify write turn on */
785 ret = onenand_verify_page(mtd, (u_char *) buf, to);
787 MTDDEBUG (MTD_DEBUG_LEVEL0,
788 "onenand_write_ecc: verify failed %d\n", ret);
799 /* Deselect and wake up anyone waiting on the device */
800 onenand_release_device(mtd);
808 * onenand_write - [MTD Interface] compability function for onenand_write_ecc
809 * @param mtd MTD device structure
810 * @param to offset to write to
811 * @param len number of bytes to write
812 * @param retlen pointer to variable to store the number of written bytes
813 * @param buf the data to write
815 * This function simply calls onenand_write_ecc
816 * with oob buffer and oobsel = NULL
818 int onenand_write(struct mtd_info *mtd, loff_t to, size_t len,
819 size_t * retlen, const u_char * buf)
821 return onenand_write_ecc(mtd, to, len, retlen, buf, NULL, NULL);
825 * onenand_write_oob - [MTD Interface] OneNAND write out-of-band
826 * @param mtd MTD device structure
827 * @param to offset to write to
828 * @param len number of bytes to write
829 * @param retlen pointer to variable to store the number of written bytes
830 * @param buf the data to write
832 * OneNAND write out-of-band
834 int onenand_write_oob(struct mtd_info *mtd, loff_t to, size_t len,
835 size_t * retlen, const u_char * buf)
837 struct onenand_chip *this = mtd->priv;
841 MTDDEBUG (MTD_DEBUG_LEVEL3, "onenand_write_oob: "
842 "to = 0x%08x, len = %i\n",
843 (unsigned int)to, (int)len);
845 /* Initialize retlen, in case of early exit */
848 /* Do not allow writes past end of device */
849 if (unlikely((to + len) > mtd->size)) {
850 MTDDEBUG (MTD_DEBUG_LEVEL0, "onenand_write_oob: "
851 "Attempt write to past end of device\n");
855 /* Grab the lock and see if the device is available */
856 onenand_get_device(mtd, FL_WRITING);
858 /* Loop until all data write */
859 while (written < len) {
860 int thislen = min_t(int, mtd->oobsize, len - written);
862 column = to & (mtd->oobsize - 1);
864 this->command(mtd, ONENAND_CMD_BUFFERRAM, to, mtd->oobsize);
866 this->write_bufferram(mtd, ONENAND_SPARERAM, ffchars, 0,
868 this->write_bufferram(mtd, ONENAND_SPARERAM, buf, column,
871 this->command(mtd, ONENAND_CMD_PROGOOB, to, mtd->oobsize);
873 onenand_update_bufferram(mtd, to, 0);
875 status = this->wait(mtd, FL_WRITING);
887 /* Deselect and wake up anyone waiting on the device */
888 onenand_release_device(mtd);
896 * onenand_erase - [MTD Interface] erase block(s)
897 * @param mtd MTD device structure
898 * @param instr erase instruction
900 * Erase one ore more blocks
902 int onenand_erase(struct mtd_info *mtd, struct erase_info *instr)
904 struct onenand_chip *this = mtd->priv;
905 unsigned int block_size;
910 MTDDEBUG (MTD_DEBUG_LEVEL3, "onenand_erase: start = 0x%08x, len = %i\n",
911 (unsigned int)instr->addr, (unsigned int)instr->len);
913 block_size = (1 << this->erase_shift);
915 /* Start address must align on block boundary */
916 if (unlikely(instr->addr & (block_size - 1))) {
917 MTDDEBUG (MTD_DEBUG_LEVEL0,
918 "onenand_erase: Unaligned address\n");
922 /* Length must align on block boundary */
923 if (unlikely(instr->len & (block_size - 1))) {
924 MTDDEBUG (MTD_DEBUG_LEVEL0,
925 "onenand_erase: Length not block aligned\n");
929 /* Do not allow erase past end of device */
930 if (unlikely((instr->len + instr->addr) > mtd->size)) {
931 MTDDEBUG (MTD_DEBUG_LEVEL0,
932 "onenand_erase: Erase past end of device\n");
936 instr->fail_addr = 0xffffffff;
938 /* Grab the lock and see if the device is available */
939 onenand_get_device(mtd, FL_ERASING);
941 /* Loop throught the pages */
945 instr->state = MTD_ERASING;
949 /* TODO Check badblock */
951 this->command(mtd, ONENAND_CMD_ERASE, addr, block_size);
953 ret = this->wait(mtd, FL_ERASING);
954 /* Check, if it is write protected */
957 MTDDEBUG (MTD_DEBUG_LEVEL0, "onenand_erase: "
958 "Device is write protected!!!\n");
960 MTDDEBUG (MTD_DEBUG_LEVEL0, "onenand_erase: "
961 "Failed erase, block %d\n",
962 (unsigned)(addr >> this->erase_shift));
963 instr->state = MTD_ERASE_FAILED;
964 instr->fail_addr = addr;
972 instr->state = MTD_ERASE_DONE;
976 ret = instr->state == MTD_ERASE_DONE ? 0 : -EIO;
977 /* Do call back function */
979 mtd_erase_callback(instr);
981 /* Deselect and wake up anyone waiting on the device */
982 onenand_release_device(mtd);
988 * onenand_sync - [MTD Interface] sync
989 * @param mtd MTD device structure
991 * Sync is actually a wait for chip ready function
993 void onenand_sync(struct mtd_info *mtd)
995 MTDDEBUG (MTD_DEBUG_LEVEL3, "onenand_sync: called\n");
997 /* Grab the lock and see if the device is available */
998 onenand_get_device(mtd, FL_SYNCING);
1000 /* Release it and go back */
1001 onenand_release_device(mtd);
1005 * onenand_block_isbad - [MTD Interface] Check whether the block at the given offset is bad
1006 * @param mtd MTD device structure
1007 * @param ofs offset relative to mtd start
1009 int onenand_block_isbad(struct mtd_info *mtd, loff_t ofs)
1013 * 1. Bad block table (BBT)
1014 * -> using NAND BBT to support JFFS2
1015 * 2. Bad block management (BBM)
1016 * -> bad block replace scheme
1018 * Currently we do nothing
1024 * onenand_block_markbad - [MTD Interface] Mark the block at the given offset as bad
1025 * @param mtd MTD device structure
1026 * @param ofs offset relative to mtd start
1028 int onenand_block_markbad(struct mtd_info *mtd, loff_t ofs)
1035 * onenand_unlock - [MTD Interface] Unlock block(s)
1036 * @param mtd MTD device structure
1037 * @param ofs offset relative to mtd start
1038 * @param len number of bytes to unlock
1040 * Unlock one or more blocks
1042 int onenand_unlock(struct mtd_info *mtd, loff_t ofs, size_t len)
1044 struct onenand_chip *this = mtd->priv;
1045 int start, end, block, value, status;
1047 start = ofs >> this->erase_shift;
1048 end = len >> this->erase_shift;
1050 /* Continuous lock scheme */
1051 if (this->options & ONENAND_CONT_LOCK) {
1052 /* Set start block address */
1053 this->write_word(start,
1054 this->base + ONENAND_REG_START_BLOCK_ADDRESS);
1055 /* Set end block address */
1056 this->write_word(end - 1,
1057 this->base + ONENAND_REG_END_BLOCK_ADDRESS);
1058 /* Write unlock command */
1059 this->command(mtd, ONENAND_CMD_UNLOCK, 0, 0);
1061 /* There's no return value */
1062 this->wait(mtd, FL_UNLOCKING);
1065 while (this->read_word(this->base + ONENAND_REG_CTRL_STATUS)
1066 & ONENAND_CTRL_ONGO)
1069 /* Check lock status */
1070 status = this->read_word(this->base + ONENAND_REG_WP_STATUS);
1071 if (!(status & ONENAND_WP_US))
1072 printk(KERN_ERR "wp status = 0x%x\n", status);
1077 /* Block lock scheme */
1078 for (block = start; block < end; block++) {
1079 /* Set start block address */
1080 this->write_word(block,
1081 this->base + ONENAND_REG_START_BLOCK_ADDRESS);
1082 /* Write unlock command */
1083 this->command(mtd, ONENAND_CMD_UNLOCK, 0, 0);
1085 /* There's no return value */
1086 this->wait(mtd, FL_UNLOCKING);
1089 while (this->read_word(this->base + ONENAND_REG_CTRL_STATUS)
1090 & ONENAND_CTRL_ONGO)
1093 /* Set block address for read block status */
1094 value = onenand_block_address(this->device_id, block);
1095 this->write_word(value,
1096 this->base + ONENAND_REG_START_ADDRESS1);
1098 /* Check lock status */
1099 status = this->read_word(this->base + ONENAND_REG_WP_STATUS);
1100 if (!(status & ONENAND_WP_US))
1101 printk(KERN_ERR "block = %d, wp status = 0x%x\n",
1109 * onenand_print_device_info - Print device ID
1110 * @param device device ID
1114 char * onenand_print_device_info(int device)
1116 int vcc, demuxed, ddp, density;
1117 char *dev_info = malloc(80);
1119 vcc = device & ONENAND_DEVICE_VCC_MASK;
1120 demuxed = device & ONENAND_DEVICE_IS_DEMUX;
1121 ddp = device & ONENAND_DEVICE_IS_DDP;
1122 density = device >> ONENAND_DEVICE_DENSITY_SHIFT;
1123 sprintf(dev_info, "%sOneNAND%s %dMB %sV 16-bit (0x%02x)",
1124 demuxed ? "" : "Muxed ",
1126 (16 << density), vcc ? "2.65/3.3" : "1.8", device);
1131 static const struct onenand_manufacturers onenand_manuf_ids[] = {
1132 {ONENAND_MFR_SAMSUNG, "Samsung"},
1133 {ONENAND_MFR_UNKNOWN, "Unknown"}
1137 * onenand_check_maf - Check manufacturer ID
1138 * @param manuf manufacturer ID
1140 * Check manufacturer ID
1142 static int onenand_check_maf(int manuf)
1146 for (i = 0; onenand_manuf_ids[i].id; i++) {
1147 if (manuf == onenand_manuf_ids[i].id)
1151 #ifdef ONENAND_DEBUG
1152 printk(KERN_DEBUG "OneNAND Manufacturer: %s (0x%0x)\n",
1153 onenand_manuf_ids[i].name, manuf);
1156 return (i != ONENAND_MFR_UNKNOWN);
1160 * onenand_probe - [OneNAND Interface] Probe the OneNAND device
1161 * @param mtd MTD device structure
1163 * OneNAND detection method:
1164 * Compare the the values from command with ones from register
1166 static int onenand_probe(struct mtd_info *mtd)
1168 struct onenand_chip *this = mtd->priv;
1169 int bram_maf_id, bram_dev_id, maf_id, dev_id;
1173 /* Send the command for reading device ID from BootRAM */
1174 this->write_word(ONENAND_CMD_READID, this->base + ONENAND_BOOTRAM);
1176 /* Read manufacturer and device IDs from BootRAM */
1177 bram_maf_id = this->read_word(this->base + ONENAND_BOOTRAM + 0x0);
1178 bram_dev_id = this->read_word(this->base + ONENAND_BOOTRAM + 0x2);
1180 /* Check manufacturer ID */
1181 if (onenand_check_maf(bram_maf_id))
1184 /* Reset OneNAND to read default register values */
1185 this->write_word(ONENAND_CMD_RESET, this->base + ONENAND_BOOTRAM);
1189 for (i = 0; i < 10000; i++) ;
1192 /* Read manufacturer and device IDs from Register */
1193 maf_id = this->read_word(this->base + ONENAND_REG_MANUFACTURER_ID);
1194 dev_id = this->read_word(this->base + ONENAND_REG_DEVICE_ID);
1196 /* Check OneNAND device */
1197 if (maf_id != bram_maf_id || dev_id != bram_dev_id)
1200 /* FIXME : Current OneNAND MTD doesn't support Flex-OneNAND */
1201 if (dev_id & (1 << 9)) {
1202 printk("Not yet support Flex-OneNAND\n");
1206 /* Flash device information */
1207 mtd->name = onenand_print_device_info(dev_id);
1208 this->device_id = dev_id;
1210 density = dev_id >> ONENAND_DEVICE_DENSITY_SHIFT;
1211 this->chipsize = (16 << density) << 20;
1213 /* OneNAND page size & block size */
1214 /* The data buffer size is equal to page size */
1216 this->read_word(this->base + ONENAND_REG_DATA_BUFFER_SIZE);
1217 mtd->oobsize = mtd->oobblock >> 5;
1218 /* Pagers per block is always 64 in OneNAND */
1219 mtd->erasesize = mtd->oobblock << 6;
1221 this->erase_shift = ffs(mtd->erasesize) - 1;
1222 this->page_shift = ffs(mtd->oobblock) - 1;
1223 this->ppb_shift = (this->erase_shift - this->page_shift);
1224 this->page_mask = (mtd->erasesize / mtd->oobblock) - 1;
1226 /* REVIST: Multichip handling */
1228 mtd->size = this->chipsize;
1231 version_id = this->read_word(this->base + ONENAND_REG_VERSION_ID);
1232 #ifdef ONENAND_DEBUG
1233 printk(KERN_DEBUG "OneNAND version = 0x%04x\n", version_id);
1237 if (density <= ONENAND_DEVICE_DENSITY_512Mb &&
1238 !(version_id >> ONENAND_VERSION_PROCESS_SHIFT)) {
1239 printk(KERN_INFO "Lock scheme is Continues Lock\n");
1240 this->options |= ONENAND_CONT_LOCK;
1243 mtd->erase = onenand_erase;
1244 mtd->read = onenand_read;
1245 mtd->write = onenand_write;
1246 mtd->read_ecc = onenand_read_ecc;
1247 mtd->write_ecc = onenand_write_ecc;
1248 mtd->read_oob = onenand_read_oob;
1249 mtd->write_oob = onenand_write_oob;
1250 mtd->sync = onenand_sync;
1251 mtd->block_isbad = onenand_block_isbad;
1252 mtd->block_markbad = onenand_block_markbad;
1258 * onenand_scan - [OneNAND Interface] Scan for the OneNAND device
1259 * @param mtd MTD device structure
1260 * @param maxchips Number of chips to scan for
1262 * This fills out all the not initialized function pointers
1263 * with the defaults.
1264 * The flash ID is read and the mtd/chip structures are
1265 * filled with the appropriate values.
1267 int onenand_scan(struct mtd_info *mtd, int maxchips)
1269 struct onenand_chip *this = mtd->priv;
1271 if (!this->read_word)
1272 this->read_word = onenand_readw;
1273 if (!this->write_word)
1274 this->write_word = onenand_writew;
1277 this->command = onenand_command;
1279 this->wait = onenand_wait;
1281 if (!this->read_bufferram)
1282 this->read_bufferram = onenand_read_bufferram;
1283 if (!this->write_bufferram)
1284 this->write_bufferram = onenand_write_bufferram;
1286 if (onenand_probe(mtd))
1289 /* Set Sync. Burst Read after probing */
1290 if (this->mmcontrol) {
1291 printk(KERN_INFO "OneNAND Sync. Burst Read support\n");
1292 this->read_bufferram = onenand_sync_read_bufferram;
1295 onenand_unlock(mtd, 0, mtd->size);
1297 return onenand_default_bbt(mtd);
1301 * onenand_release - [OneNAND Interface] Free resources held by the OneNAND device
1302 * @param mtd MTD device structure
1304 void onenand_release(struct mtd_info *mtd)
1308 #endif /* CONFIG_CMD_ONENAND */