2 * linux/drivers/mtd/onenand/onenand_base.c
4 * Copyright (C) 2005-2007 Samsung Electronics
5 * Kyungmin Park <kyungmin.park@samsung.com>
8 * Adrian Hunter <ext-adrian.hunter@nokia.com>:
9 * auto-placement support, read-while load support, various fixes
10 * Copyright (C) Nokia Corporation, 2007
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License version 2 as
14 * published by the Free Software Foundation.
18 #include <linux/mtd/compat.h>
19 #include <linux/mtd/mtd.h>
20 #include <linux/mtd/onenand.h>
23 #include <asm/errno.h>
26 /* It should access 16-bit instead of 8-bit */
27 static inline void *memcpy_16(void *dst
, const void *src
, unsigned int len
)
39 static const unsigned char ffchars
[] = {
40 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
41 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 16 */
42 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
43 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 32 */
44 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
45 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 48 */
46 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
47 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 64 */
51 * onenand_readw - [OneNAND Interface] Read OneNAND register
52 * @param addr address to read
54 * Read OneNAND register
56 static unsigned short onenand_readw(void __iomem
* addr
)
62 * onenand_writew - [OneNAND Interface] Write OneNAND register with value
63 * @param value value to write
64 * @param addr address to write
66 * Write OneNAND register with value
68 static void onenand_writew(unsigned short value
, void __iomem
* addr
)
74 * onenand_block_address - [DEFAULT] Get block address
75 * @param device the device id
76 * @param block the block
77 * @return translated block address if DDP, otherwise same
79 * Setup Start Address 1 Register (F100h)
81 static int onenand_block_address(struct onenand_chip
*this, int block
)
83 /* Device Flash Core select, NAND Flash Block Address */
84 if (block
& this->density_mask
)
85 return ONENAND_DDP_CHIP1
| (block
^ this->density_mask
);
91 * onenand_bufferram_address - [DEFAULT] Get bufferram address
92 * @param device the device id
93 * @param block the block
94 * @return set DBS value if DDP, otherwise 0
96 * Setup Start Address 2 Register (F101h) for DDP
98 static int onenand_bufferram_address(struct onenand_chip
*this, int block
)
100 /* Device BufferRAM Select */
101 if (block
& this->density_mask
)
102 return ONENAND_DDP_CHIP1
;
104 return ONENAND_DDP_CHIP0
;
108 * onenand_page_address - [DEFAULT] Get page address
109 * @param page the page address
110 * @param sector the sector address
111 * @return combined page and sector address
113 * Setup Start Address 8 Register (F107h)
115 static int onenand_page_address(int page
, int sector
)
117 /* Flash Page Address, Flash Sector Address */
120 fpa
= page
& ONENAND_FPA_MASK
;
121 fsa
= sector
& ONENAND_FSA_MASK
;
123 return ((fpa
<< ONENAND_FPA_SHIFT
) | fsa
);
127 * onenand_buffer_address - [DEFAULT] Get buffer address
128 * @param dataram1 DataRAM index
129 * @param sectors the sector address
130 * @param count the number of sectors
131 * @return the start buffer value
133 * Setup Start Buffer Register (F200h)
135 static int onenand_buffer_address(int dataram1
, int sectors
, int count
)
139 /* BufferRAM Sector Address */
140 bsa
= sectors
& ONENAND_BSA_MASK
;
143 bsa
|= ONENAND_BSA_DATARAM1
; /* DataRAM1 */
145 bsa
|= ONENAND_BSA_DATARAM0
; /* DataRAM0 */
147 /* BufferRAM Sector Count */
148 bsc
= count
& ONENAND_BSC_MASK
;
150 return ((bsa
<< ONENAND_BSA_SHIFT
) | bsc
);
154 * onenand_get_density - [DEFAULT] Get OneNAND density
155 * @param dev_id OneNAND device ID
157 * Get OneNAND density from device ID
159 static inline int onenand_get_density(int dev_id
)
161 int density
= dev_id
>> ONENAND_DEVICE_DENSITY_SHIFT
;
162 return (density
& ONENAND_DEVICE_DENSITY_MASK
);
166 * onenand_command - [DEFAULT] Send command to OneNAND device
167 * @param mtd MTD device structure
168 * @param cmd the command to be sent
169 * @param addr offset to read from or write to
170 * @param len number of bytes to read or write
172 * Send command to OneNAND device. This function is used for middle/large page
173 * devices (1KB/2KB Bytes per page)
175 static int onenand_command(struct mtd_info
*mtd
, int cmd
, loff_t addr
,
178 struct onenand_chip
*this = mtd
->priv
;
179 int value
, readcmd
= 0;
181 /* Now we use page size operation */
182 int sectors
= 4, count
= 4;
184 /* Address translation */
186 case ONENAND_CMD_UNLOCK
:
187 case ONENAND_CMD_LOCK
:
188 case ONENAND_CMD_LOCK_TIGHT
:
189 case ONENAND_CMD_UNLOCK_ALL
:
194 case ONENAND_CMD_ERASE
:
195 case ONENAND_CMD_BUFFERRAM
:
196 block
= (int)(addr
>> this->erase_shift
);
201 block
= (int)(addr
>> this->erase_shift
);
202 page
= (int)(addr
>> this->page_shift
);
203 page
&= this->page_mask
;
207 /* NOTE: The setting order of the registers is very important! */
208 if (cmd
== ONENAND_CMD_BUFFERRAM
) {
209 /* Select DataRAM for DDP */
210 value
= onenand_bufferram_address(this, block
);
211 this->write_word(value
,
212 this->base
+ ONENAND_REG_START_ADDRESS2
);
214 /* Switch to the next data buffer */
215 ONENAND_SET_NEXT_BUFFERRAM(this);
221 /* Write 'DFS, FBA' of Flash */
222 value
= onenand_block_address(this, block
);
223 this->write_word(value
,
224 this->base
+ ONENAND_REG_START_ADDRESS1
);
226 /* Write 'DFS, FBA' of Flash */
227 value
= onenand_bufferram_address(this, block
);
228 this->write_word(value
,
229 this->base
+ ONENAND_REG_START_ADDRESS2
);
236 case ONENAND_CMD_READ
:
237 case ONENAND_CMD_READOOB
:
238 dataram
= ONENAND_SET_NEXT_BUFFERRAM(this);
243 dataram
= ONENAND_CURRENT_BUFFERRAM(this);
247 /* Write 'FPA, FSA' of Flash */
248 value
= onenand_page_address(page
, sectors
);
249 this->write_word(value
,
250 this->base
+ ONENAND_REG_START_ADDRESS8
);
252 /* Write 'BSA, BSC' of DataRAM */
253 value
= onenand_buffer_address(dataram
, sectors
, count
);
254 this->write_word(value
, this->base
+ ONENAND_REG_START_BUFFER
);
257 /* Interrupt clear */
258 this->write_word(ONENAND_INT_CLEAR
, this->base
+ ONENAND_REG_INTERRUPT
);
260 this->write_word(cmd
, this->base
+ ONENAND_REG_COMMAND
);
266 * onenand_wait - [DEFAULT] wait until the command is done
267 * @param mtd MTD device structure
268 * @param state state to select the max. timeout value
270 * Wait for command done. This applies to all OneNAND command
271 * Read can take up to 30us, erase up to 2ms and program up to 350us
272 * according to general OneNAND specs
274 static int onenand_wait(struct mtd_info
*mtd
, int state
)
276 struct onenand_chip
*this = mtd
->priv
;
277 unsigned int flags
= ONENAND_INT_MASTER
;
278 unsigned int interrupt
= 0;
279 unsigned int ctrl
, ecc
;
282 interrupt
= this->read_word(this->base
+ ONENAND_REG_INTERRUPT
);
283 if (interrupt
& flags
)
287 ctrl
= this->read_word(this->base
+ ONENAND_REG_CTRL_STATUS
);
289 if (ctrl
& ONENAND_CTRL_ERROR
) {
290 printk("onenand_wait: controller error = 0x%04x\n", ctrl
);
291 if (ctrl
& ONENAND_CTRL_LOCK
)
292 printk("onenand_wait: it's locked error = 0x%04x\n",
298 if (interrupt
& ONENAND_INT_READ
) {
299 ecc
= this->read_word(this->base
+ ONENAND_REG_ECC_STATUS
);
300 if (ecc
& ONENAND_ECC_2BIT_ALL
) {
301 MTDDEBUG (MTD_DEBUG_LEVEL0
,
302 "onenand_wait: ECC error = 0x%04x\n", ecc
);
311 * onenand_bufferram_offset - [DEFAULT] BufferRAM offset
312 * @param mtd MTD data structure
313 * @param area BufferRAM area
314 * @return offset given area
316 * Return BufferRAM offset given area
318 static inline int onenand_bufferram_offset(struct mtd_info
*mtd
, int area
)
320 struct onenand_chip
*this = mtd
->priv
;
322 if (ONENAND_CURRENT_BUFFERRAM(this)) {
323 if (area
== ONENAND_DATARAM
)
324 return mtd
->writesize
;
325 if (area
== ONENAND_SPARERAM
)
333 * onenand_read_bufferram - [OneNAND Interface] Read the bufferram area
334 * @param mtd MTD data structure
335 * @param area BufferRAM area
336 * @param buffer the databuffer to put/get data
337 * @param offset offset to read from or write to
338 * @param count number of bytes to read/write
340 * Read the BufferRAM area
342 static int onenand_read_bufferram(struct mtd_info
*mtd
, loff_t addr
, int area
,
343 unsigned char *buffer
, int offset
,
346 struct onenand_chip
*this = mtd
->priv
;
347 void __iomem
*bufferram
;
349 bufferram
= this->base
+ area
;
350 bufferram
+= onenand_bufferram_offset(mtd
, area
);
352 memcpy_16(buffer
, bufferram
+ offset
, count
);
358 * onenand_sync_read_bufferram - [OneNAND Interface] Read the bufferram area with Sync. Burst mode
359 * @param mtd MTD data structure
360 * @param area BufferRAM area
361 * @param buffer the databuffer to put/get data
362 * @param offset offset to read from or write to
363 * @param count number of bytes to read/write
365 * Read the BufferRAM area with Sync. Burst Mode
367 static int onenand_sync_read_bufferram(struct mtd_info
*mtd
, loff_t addr
, int area
,
368 unsigned char *buffer
, int offset
,
371 struct onenand_chip
*this = mtd
->priv
;
372 void __iomem
*bufferram
;
374 bufferram
= this->base
+ area
;
375 bufferram
+= onenand_bufferram_offset(mtd
, area
);
377 this->mmcontrol(mtd
, ONENAND_SYS_CFG1_SYNC_READ
);
379 memcpy_16(buffer
, bufferram
+ offset
, count
);
381 this->mmcontrol(mtd
, 0);
387 * onenand_write_bufferram - [OneNAND Interface] Write the bufferram area
388 * @param mtd MTD data structure
389 * @param area BufferRAM area
390 * @param buffer the databuffer to put/get data
391 * @param offset offset to read from or write to
392 * @param count number of bytes to read/write
394 * Write the BufferRAM area
396 static int onenand_write_bufferram(struct mtd_info
*mtd
, loff_t addr
, int area
,
397 const unsigned char *buffer
, int offset
,
400 struct onenand_chip
*this = mtd
->priv
;
401 void __iomem
*bufferram
;
403 bufferram
= this->base
+ area
;
404 bufferram
+= onenand_bufferram_offset(mtd
, area
);
406 memcpy_16(bufferram
+ offset
, buffer
, count
);
412 * onenand_get_2x_blockpage - [GENERIC] Get blockpage at 2x program mode
413 * @param mtd MTD data structure
414 * @param addr address to check
415 * @return blockpage address
417 * Get blockpage address at 2x program mode
419 static int onenand_get_2x_blockpage(struct mtd_info
*mtd
, loff_t addr
)
421 struct onenand_chip
*this = mtd
->priv
;
422 int blockpage
, block
, page
;
424 /* Calculate the even block number */
425 block
= (int) (addr
>> this->erase_shift
) & ~1;
426 /* Is it the odd plane? */
427 if (addr
& this->writesize
)
429 page
= (int) (addr
>> (this->page_shift
+ 1)) & this->page_mask
;
430 blockpage
= (block
<< 7) | page
;
436 * onenand_check_bufferram - [GENERIC] Check BufferRAM information
437 * @param mtd MTD data structure
438 * @param addr address to check
439 * @return 1 if there are valid data, otherwise 0
441 * Check bufferram if there is data we required
443 static int onenand_check_bufferram(struct mtd_info
*mtd
, loff_t addr
)
445 struct onenand_chip
*this = mtd
->priv
;
446 int blockpage
, found
= 0;
449 #ifdef CONFIG_S3C64XX
453 if (ONENAND_IS_2PLANE(this))
454 blockpage
= onenand_get_2x_blockpage(mtd
, addr
);
456 blockpage
= (int) (addr
>> this->page_shift
);
458 /* Is there valid data? */
459 i
= ONENAND_CURRENT_BUFFERRAM(this);
460 if (this->bufferram
[i
].blockpage
== blockpage
)
463 /* Check another BufferRAM */
464 i
= ONENAND_NEXT_BUFFERRAM(this);
465 if (this->bufferram
[i
].blockpage
== blockpage
) {
466 ONENAND_SET_NEXT_BUFFERRAM(this);
471 if (found
&& ONENAND_IS_DDP(this)) {
472 /* Select DataRAM for DDP */
473 int block
= (int) (addr
>> this->erase_shift
);
474 int value
= onenand_bufferram_address(this, block
);
475 this->write_word(value
, this->base
+ ONENAND_REG_START_ADDRESS2
);
482 * onenand_update_bufferram - [GENERIC] Update BufferRAM information
483 * @param mtd MTD data structure
484 * @param addr address to update
485 * @param valid valid flag
487 * Update BufferRAM information
489 static int onenand_update_bufferram(struct mtd_info
*mtd
, loff_t addr
,
492 struct onenand_chip
*this = mtd
->priv
;
496 if (ONENAND_IS_2PLANE(this))
497 blockpage
= onenand_get_2x_blockpage(mtd
, addr
);
499 blockpage
= (int)(addr
>> this->page_shift
);
501 /* Invalidate another BufferRAM */
502 i
= ONENAND_NEXT_BUFFERRAM(this);
503 if (this->bufferram
[i
].blockpage
== blockpage
)
504 this->bufferram
[i
].blockpage
= -1;
506 /* Update BufferRAM */
507 i
= ONENAND_CURRENT_BUFFERRAM(this);
509 this->bufferram
[i
].blockpage
= blockpage
;
511 this->bufferram
[i
].blockpage
= -1;
517 * onenand_invalidate_bufferram - [GENERIC] Invalidate BufferRAM information
518 * @param mtd MTD data structure
519 * @param addr start address to invalidate
520 * @param len length to invalidate
522 * Invalidate BufferRAM information
524 static void onenand_invalidate_bufferram(struct mtd_info
*mtd
, loff_t addr
,
527 struct onenand_chip
*this = mtd
->priv
;
529 loff_t end_addr
= addr
+ len
;
531 /* Invalidate BufferRAM */
532 for (i
= 0; i
< MAX_BUFFERRAM
; i
++) {
533 loff_t buf_addr
= this->bufferram
[i
].blockpage
<< this->page_shift
;
535 if (buf_addr
>= addr
&& buf_addr
< end_addr
)
536 this->bufferram
[i
].blockpage
= -1;
541 * onenand_get_device - [GENERIC] Get chip for selected access
542 * @param mtd MTD device structure
543 * @param new_state the state which is requested
545 * Get the device and lock it for exclusive access
547 static void onenand_get_device(struct mtd_info
*mtd
, int new_state
)
553 * onenand_release_device - [GENERIC] release chip
554 * @param mtd MTD device structure
556 * Deselect, release chip lock and wake up anyone waiting on the device
558 static void onenand_release_device(struct mtd_info
*mtd
)
564 * onenand_transfer_auto_oob - [Internal] oob auto-placement transfer
565 * @param mtd MTD device structure
566 * @param buf destination address
567 * @param column oob offset to read from
568 * @param thislen oob length to read
570 static int onenand_transfer_auto_oob(struct mtd_info
*mtd
, uint8_t *buf
,
571 int column
, int thislen
)
573 struct onenand_chip
*this = mtd
->priv
;
574 struct nand_oobfree
*free
;
575 int readcol
= column
;
576 int readend
= column
+ thislen
;
579 uint8_t *oob_buf
= this->oob_buf
;
581 free
= this->ecclayout
->oobfree
;
582 for (i
= 0; i
< MTD_MAX_OOBFREE_ENTRIES
&& free
->length
; i
++, free
++) {
583 if (readcol
>= lastgap
)
584 readcol
+= free
->offset
- lastgap
;
585 if (readend
>= lastgap
)
586 readend
+= free
->offset
- lastgap
;
587 lastgap
= free
->offset
+ free
->length
;
589 this->read_bufferram(mtd
, 0, ONENAND_SPARERAM
, oob_buf
, 0, mtd
->oobsize
);
590 free
= this->ecclayout
->oobfree
;
591 for (i
= 0; i
< MTD_MAX_OOBFREE_ENTRIES
&& free
->length
; i
++, free
++) {
592 int free_end
= free
->offset
+ free
->length
;
593 if (free
->offset
< readend
&& free_end
> readcol
) {
594 int st
= max_t(int,free
->offset
,readcol
);
595 int ed
= min_t(int,free_end
,readend
);
597 memcpy(buf
, oob_buf
+ st
, n
);
599 } else if (column
== 0)
606 * onenand_read_ops_nolock - [OneNAND Interface] OneNAND read main and/or out-of-band
607 * @param mtd MTD device structure
608 * @param from offset to read from
609 * @param ops oob operation description structure
611 * OneNAND read main and/or out-of-band data
613 static int onenand_read_ops_nolock(struct mtd_info
*mtd
, loff_t from
,
614 struct mtd_oob_ops
*ops
)
616 struct onenand_chip
*this = mtd
->priv
;
617 struct mtd_ecc_stats stats
;
618 size_t len
= ops
->len
;
619 size_t ooblen
= ops
->ooblen
;
620 u_char
*buf
= ops
->datbuf
;
621 u_char
*oobbuf
= ops
->oobbuf
;
622 int read
= 0, column
, thislen
;
623 int oobread
= 0, oobcolumn
, thisooblen
, oobsize
;
624 int ret
= 0, boundary
= 0;
625 int writesize
= this->writesize
;
627 MTDDEBUG(MTD_DEBUG_LEVEL3
, "onenand_read_ops_nolock: from = 0x%08x, len = %i\n", (unsigned int) from
, (int) len
);
629 if (ops
->mode
== MTD_OOB_AUTO
)
630 oobsize
= this->ecclayout
->oobavail
;
632 oobsize
= mtd
->oobsize
;
634 oobcolumn
= from
& (mtd
->oobsize
- 1);
636 /* Do not allow reads past end of device */
637 if ((from
+ len
) > mtd
->size
) {
638 printk(KERN_ERR
"onenand_read_ops_nolock: Attempt read beyond end of device\n");
644 stats
= mtd
->ecc_stats
;
646 /* Read-while-load method */
648 /* Do first load to bufferRAM */
650 if (!onenand_check_bufferram(mtd
, from
)) {
651 this->main_buf
= buf
;
652 this->command(mtd
, ONENAND_CMD_READ
, from
, writesize
);
653 ret
= this->wait(mtd
, FL_READING
);
654 onenand_update_bufferram(mtd
, from
, !ret
);
660 thislen
= min_t(int, writesize
, len
- read
);
661 column
= from
& (writesize
- 1);
662 if (column
+ thislen
> writesize
)
663 thislen
= writesize
- column
;
666 /* If there is more to load then start next load */
668 if (read
+ thislen
< len
) {
669 this->main_buf
= buf
+ thislen
;
670 this->command(mtd
, ONENAND_CMD_READ
, from
, writesize
);
672 * Chip boundary handling in DDP
673 * Now we issued chip 1 read and pointed chip 1
674 * bufferam so we have to point chip 0 bufferam.
676 if (ONENAND_IS_DDP(this) &&
677 unlikely(from
== (this->chipsize
>> 1))) {
678 this->write_word(ONENAND_DDP_CHIP0
, this->base
+ ONENAND_REG_START_ADDRESS2
);
682 ONENAND_SET_PREV_BUFFERRAM(this);
685 /* While load is going, read from last bufferRAM */
686 this->read_bufferram(mtd
, from
- thislen
, ONENAND_DATARAM
, buf
, column
, thislen
);
688 /* Read oob area if needed */
690 thisooblen
= oobsize
- oobcolumn
;
691 thisooblen
= min_t(int, thisooblen
, ooblen
- oobread
);
693 if (ops
->mode
== MTD_OOB_AUTO
)
694 onenand_transfer_auto_oob(mtd
, oobbuf
, oobcolumn
, thisooblen
);
696 this->read_bufferram(mtd
, 0, ONENAND_SPARERAM
, oobbuf
, oobcolumn
, thisooblen
);
697 oobread
+= thisooblen
;
698 oobbuf
+= thisooblen
;
702 /* See if we are done */
706 /* Set up for next read from bufferRAM */
707 if (unlikely(boundary
))
708 this->write_word(ONENAND_DDP_CHIP1
, this->base
+ ONENAND_REG_START_ADDRESS2
);
709 ONENAND_SET_NEXT_BUFFERRAM(this);
711 thislen
= min_t(int, writesize
, len
- read
);
714 /* Now wait for load */
715 ret
= this->wait(mtd
, FL_READING
);
716 onenand_update_bufferram(mtd
, from
, !ret
);
722 * Return success, if no ECC failures, else -EBADMSG
723 * fs driver will take care of that, because
724 * retlen == desired len and result == -EBADMSG
727 ops
->oobretlen
= oobread
;
732 if (mtd
->ecc_stats
.failed
- stats
.failed
)
735 return mtd
->ecc_stats
.corrected
- stats
.corrected
? -EUCLEAN
: 0;
739 * onenand_read_oob_nolock - [MTD Interface] OneNAND read out-of-band
740 * @param mtd MTD device structure
741 * @param from offset to read from
742 * @param ops oob operation description structure
744 * OneNAND read out-of-band data from the spare area
746 static int onenand_read_oob_nolock(struct mtd_info
*mtd
, loff_t from
,
747 struct mtd_oob_ops
*ops
)
749 struct onenand_chip
*this = mtd
->priv
;
750 struct mtd_ecc_stats stats
;
751 int read
= 0, thislen
, column
, oobsize
;
752 size_t len
= ops
->ooblen
;
753 mtd_oob_mode_t mode
= ops
->mode
;
754 u_char
*buf
= ops
->oobbuf
;
757 from
+= ops
->ooboffs
;
759 MTDDEBUG(MTD_DEBUG_LEVEL3
, "onenand_read_oob_nolock: from = 0x%08x, len = %i\n", (unsigned int) from
, (int) len
);
761 /* Initialize return length value */
764 if (mode
== MTD_OOB_AUTO
)
765 oobsize
= this->ecclayout
->oobavail
;
767 oobsize
= mtd
->oobsize
;
769 column
= from
& (mtd
->oobsize
- 1);
771 if (unlikely(column
>= oobsize
)) {
772 printk(KERN_ERR
"onenand_read_oob_nolock: Attempted to start read outside oob\n");
776 /* Do not allow reads past end of device */
777 if (unlikely(from
>= mtd
->size
||
778 column
+ len
> ((mtd
->size
>> this->page_shift
) -
779 (from
>> this->page_shift
)) * oobsize
)) {
780 printk(KERN_ERR
"onenand_read_oob_nolock: Attempted to read beyond end of device\n");
784 stats
= mtd
->ecc_stats
;
787 thislen
= oobsize
- column
;
788 thislen
= min_t(int, thislen
, len
);
790 this->spare_buf
= buf
;
791 this->command(mtd
, ONENAND_CMD_READOOB
, from
, mtd
->oobsize
);
793 onenand_update_bufferram(mtd
, from
, 0);
795 ret
= this->wait(mtd
, FL_READING
);
796 if (ret
&& ret
!= -EBADMSG
) {
797 printk(KERN_ERR
"onenand_read_oob_nolock: read failed = 0x%x\n", ret
);
801 if (mode
== MTD_OOB_AUTO
)
802 onenand_transfer_auto_oob(mtd
, buf
, column
, thislen
);
804 this->read_bufferram(mtd
, 0, ONENAND_SPARERAM
, buf
, column
, thislen
);
816 from
+= mtd
->writesize
;
821 ops
->oobretlen
= read
;
826 if (mtd
->ecc_stats
.failed
- stats
.failed
)
833 * onenand_read - [MTD Interface] MTD compability function for onenand_read_ecc
834 * @param mtd MTD device structure
835 * @param from offset to read from
836 * @param len number of bytes to read
837 * @param retlen pointer to variable to store the number of read bytes
838 * @param buf the databuffer to put data
840 * This function simply calls onenand_read_ecc with oob buffer and oobsel = NULL
842 int onenand_read(struct mtd_info
*mtd
, loff_t from
, size_t len
,
843 size_t * retlen
, u_char
* buf
)
845 struct mtd_oob_ops ops
= {
853 onenand_get_device(mtd
, FL_READING
);
854 ret
= onenand_read_ops_nolock(mtd
, from
, &ops
);
855 onenand_release_device(mtd
);
857 *retlen
= ops
.retlen
;
862 * onenand_read_oob - [MTD Interface] OneNAND read out-of-band
863 * @param mtd MTD device structure
864 * @param from offset to read from
865 * @param ops oob operations description structure
867 * OneNAND main and/or out-of-band
869 int onenand_read_oob(struct mtd_info
*mtd
, loff_t from
,
870 struct mtd_oob_ops
*ops
)
879 /* Not implemented yet */
884 onenand_get_device(mtd
, FL_READING
);
886 ret
= onenand_read_ops_nolock(mtd
, from
, ops
);
888 ret
= onenand_read_oob_nolock(mtd
, from
, ops
);
889 onenand_release_device(mtd
);
895 * onenand_bbt_wait - [DEFAULT] wait until the command is done
896 * @param mtd MTD device structure
897 * @param state state to select the max. timeout value
899 * Wait for command done.
901 static int onenand_bbt_wait(struct mtd_info
*mtd
, int state
)
903 struct onenand_chip
*this = mtd
->priv
;
904 unsigned int flags
= ONENAND_INT_MASTER
;
905 unsigned int interrupt
;
909 interrupt
= this->read_word(this->base
+ ONENAND_REG_INTERRUPT
);
910 if (interrupt
& flags
)
914 /* To get correct interrupt status in timeout case */
915 interrupt
= this->read_word(this->base
+ ONENAND_REG_INTERRUPT
);
916 ctrl
= this->read_word(this->base
+ ONENAND_REG_CTRL_STATUS
);
918 if (interrupt
& ONENAND_INT_READ
) {
919 int ecc
= this->read_word(this->base
+ ONENAND_REG_ECC_STATUS
);
920 if (ecc
& ONENAND_ECC_2BIT_ALL
)
921 return ONENAND_BBT_READ_ERROR
;
923 printk(KERN_ERR
"onenand_bbt_wait: read timeout!"
924 "ctrl=0x%04x intr=0x%04x\n", ctrl
, interrupt
);
925 return ONENAND_BBT_READ_FATAL_ERROR
;
928 /* Initial bad block case: 0x2400 or 0x0400 */
929 if (ctrl
& ONENAND_CTRL_ERROR
) {
930 printk(KERN_DEBUG
"onenand_bbt_wait: controller error = 0x%04x\n", ctrl
);
931 return ONENAND_BBT_READ_ERROR
;
938 * onenand_bbt_read_oob - [MTD Interface] OneNAND read out-of-band for bbt scan
939 * @param mtd MTD device structure
940 * @param from offset to read from
941 * @param ops oob operation description structure
943 * OneNAND read out-of-band data from the spare area for bbt scan
945 int onenand_bbt_read_oob(struct mtd_info
*mtd
, loff_t from
,
946 struct mtd_oob_ops
*ops
)
948 struct onenand_chip
*this = mtd
->priv
;
949 int read
= 0, thislen
, column
;
951 size_t len
= ops
->ooblen
;
952 u_char
*buf
= ops
->oobbuf
;
954 MTDDEBUG(MTD_DEBUG_LEVEL3
, "onenand_bbt_read_oob: from = 0x%08x, len = %zi\n", (unsigned int) from
, len
);
956 /* Initialize return value */
959 /* Do not allow reads past end of device */
960 if (unlikely((from
+ len
) > mtd
->size
)) {
961 printk(KERN_ERR
"onenand_bbt_read_oob: Attempt read beyond end of device\n");
962 return ONENAND_BBT_READ_FATAL_ERROR
;
965 /* Grab the lock and see if the device is available */
966 onenand_get_device(mtd
, FL_READING
);
968 column
= from
& (mtd
->oobsize
- 1);
972 thislen
= mtd
->oobsize
- column
;
973 thislen
= min_t(int, thislen
, len
);
975 this->spare_buf
= buf
;
976 this->command(mtd
, ONENAND_CMD_READOOB
, from
, mtd
->oobsize
);
978 onenand_update_bufferram(mtd
, from
, 0);
980 ret
= this->bbt_wait(mtd
, FL_READING
);
984 this->read_spareram(mtd
, 0, ONENAND_SPARERAM
, buf
, column
, thislen
);
993 /* Update Page size */
994 from
+= this->writesize
;
999 /* Deselect and wake up anyone waiting on the device */
1000 onenand_release_device(mtd
);
1002 ops
->oobretlen
= read
;
1007 #ifdef CONFIG_MTD_ONENAND_VERIFY_WRITE
1009 * onenand_verify_oob - [GENERIC] verify the oob contents after a write
1010 * @param mtd MTD device structure
1011 * @param buf the databuffer to verify
1012 * @param to offset to read from
1014 static int onenand_verify_oob(struct mtd_info
*mtd
, const u_char
*buf
, loff_t to
)
1016 struct onenand_chip
*this = mtd
->priv
;
1017 u_char
*oob_buf
= this->oob_buf
;
1020 this->command(mtd
, ONENAND_CMD_READOOB
, to
, mtd
->oobsize
);
1021 onenand_update_bufferram(mtd
, to
, 0);
1022 status
= this->wait(mtd
, FL_READING
);
1026 this->read_bufferram(mtd
, 0, ONENAND_SPARERAM
, oob_buf
, 0, mtd
->oobsize
);
1027 for (i
= 0; i
< mtd
->oobsize
; i
++)
1028 if (buf
[i
] != 0xFF && buf
[i
] != oob_buf
[i
])
1035 * onenand_verify - [GENERIC] verify the chip contents after a write
1036 * @param mtd MTD device structure
1037 * @param buf the databuffer to verify
1038 * @param addr offset to read from
1039 * @param len number of bytes to read and compare
1041 static int onenand_verify(struct mtd_info
*mtd
, const u_char
*buf
, loff_t addr
, size_t len
)
1043 struct onenand_chip
*this = mtd
->priv
;
1044 void __iomem
*dataram
;
1046 int thislen
, column
;
1049 thislen
= min_t(int, this->writesize
, len
);
1050 column
= addr
& (this->writesize
- 1);
1051 if (column
+ thislen
> this->writesize
)
1052 thislen
= this->writesize
- column
;
1054 this->command(mtd
, ONENAND_CMD_READ
, addr
, this->writesize
);
1056 onenand_update_bufferram(mtd
, addr
, 0);
1058 ret
= this->wait(mtd
, FL_READING
);
1062 onenand_update_bufferram(mtd
, addr
, 1);
1064 dataram
= this->base
+ ONENAND_DATARAM
;
1065 dataram
+= onenand_bufferram_offset(mtd
, ONENAND_DATARAM
);
1067 if (memcmp(buf
, dataram
+ column
, thislen
))
1078 #define onenand_verify(...) (0)
1079 #define onenand_verify_oob(...) (0)
1082 #define NOTALIGNED(x) ((x & (mtd->writesize - 1)) != 0)
1085 * onenand_fill_auto_oob - [Internal] oob auto-placement transfer
1086 * @param mtd MTD device structure
1087 * @param oob_buf oob buffer
1088 * @param buf source address
1089 * @param column oob offset to write to
1090 * @param thislen oob length to write
1092 static int onenand_fill_auto_oob(struct mtd_info
*mtd
, u_char
*oob_buf
,
1093 const u_char
*buf
, int column
, int thislen
)
1095 struct onenand_chip
*this = mtd
->priv
;
1096 struct nand_oobfree
*free
;
1097 int writecol
= column
;
1098 int writeend
= column
+ thislen
;
1102 free
= this->ecclayout
->oobfree
;
1103 for (i
= 0; i
< MTD_MAX_OOBFREE_ENTRIES
&& free
->length
; i
++, free
++) {
1104 if (writecol
>= lastgap
)
1105 writecol
+= free
->offset
- lastgap
;
1106 if (writeend
>= lastgap
)
1107 writeend
+= free
->offset
- lastgap
;
1108 lastgap
= free
->offset
+ free
->length
;
1110 free
= this->ecclayout
->oobfree
;
1111 for (i
= 0; i
< MTD_MAX_OOBFREE_ENTRIES
&& free
->length
; i
++, free
++) {
1112 int free_end
= free
->offset
+ free
->length
;
1113 if (free
->offset
< writeend
&& free_end
> writecol
) {
1114 int st
= max_t(int,free
->offset
,writecol
);
1115 int ed
= min_t(int,free_end
,writeend
);
1117 memcpy(oob_buf
+ st
, buf
, n
);
1119 } else if (column
== 0)
1126 * onenand_write_ops_nolock - [OneNAND Interface] write main and/or out-of-band
1127 * @param mtd MTD device structure
1128 * @param to offset to write to
1129 * @param ops oob operation description structure
1131 * Write main and/or oob with ECC
1133 static int onenand_write_ops_nolock(struct mtd_info
*mtd
, loff_t to
,
1134 struct mtd_oob_ops
*ops
)
1136 struct onenand_chip
*this = mtd
->priv
;
1137 int written
= 0, column
, thislen
, subpage
;
1138 int oobwritten
= 0, oobcolumn
, thisooblen
, oobsize
;
1139 size_t len
= ops
->len
;
1140 size_t ooblen
= ops
->ooblen
;
1141 const u_char
*buf
= ops
->datbuf
;
1142 const u_char
*oob
= ops
->oobbuf
;
1146 MTDDEBUG(MTD_DEBUG_LEVEL3
, "onenand_write_ops_nolock: to = 0x%08x, len = %i\n", (unsigned int) to
, (int) len
);
1148 /* Initialize retlen, in case of early exit */
1152 /* Do not allow writes past end of device */
1153 if (unlikely((to
+ len
) > mtd
->size
)) {
1154 printk(KERN_ERR
"onenand_write_ops_nolock: Attempt write to past end of device\n");
1158 /* Reject writes, which are not page aligned */
1159 if (unlikely(NOTALIGNED(to
) || NOTALIGNED(len
))) {
1160 printk(KERN_ERR
"onenand_write_ops_nolock: Attempt to write not page aligned data\n");
1164 if (ops
->mode
== MTD_OOB_AUTO
)
1165 oobsize
= this->ecclayout
->oobavail
;
1167 oobsize
= mtd
->oobsize
;
1169 oobcolumn
= to
& (mtd
->oobsize
- 1);
1171 column
= to
& (mtd
->writesize
- 1);
1173 /* Loop until all data write */
1174 while (written
< len
) {
1175 u_char
*wbuf
= (u_char
*) buf
;
1177 thislen
= min_t(int, mtd
->writesize
- column
, len
- written
);
1178 thisooblen
= min_t(int, oobsize
- oobcolumn
, ooblen
- oobwritten
);
1180 this->command(mtd
, ONENAND_CMD_BUFFERRAM
, to
, thislen
);
1182 /* Partial page write */
1183 subpage
= thislen
< mtd
->writesize
;
1185 memset(this->page_buf
, 0xff, mtd
->writesize
);
1186 memcpy(this->page_buf
+ column
, buf
, thislen
);
1187 wbuf
= this->page_buf
;
1190 this->write_bufferram(mtd
, to
, ONENAND_DATARAM
, wbuf
, 0, mtd
->writesize
);
1193 oobbuf
= this->oob_buf
;
1195 /* We send data to spare ram with oobsize
1196 * * to prevent byte access */
1197 memset(oobbuf
, 0xff, mtd
->oobsize
);
1198 if (ops
->mode
== MTD_OOB_AUTO
)
1199 onenand_fill_auto_oob(mtd
, oobbuf
, oob
, oobcolumn
, thisooblen
);
1201 memcpy(oobbuf
+ oobcolumn
, oob
, thisooblen
);
1203 oobwritten
+= thisooblen
;
1207 oobbuf
= (u_char
*) ffchars
;
1209 this->write_bufferram(mtd
, 0, ONENAND_SPARERAM
, oobbuf
, 0, mtd
->oobsize
);
1211 this->command(mtd
, ONENAND_CMD_PROG
, to
, mtd
->writesize
);
1213 ret
= this->wait(mtd
, FL_WRITING
);
1215 /* In partial page write we don't update bufferram */
1216 onenand_update_bufferram(mtd
, to
, !ret
&& !subpage
);
1217 if (ONENAND_IS_2PLANE(this)) {
1218 ONENAND_SET_BUFFERRAM1(this);
1219 onenand_update_bufferram(mtd
, to
+ this->writesize
, !ret
&& !subpage
);
1223 printk(KERN_ERR
"onenand_write_ops_nolock: write filaed %d\n", ret
);
1227 /* Only check verify write turn on */
1228 ret
= onenand_verify(mtd
, buf
, to
, thislen
);
1230 printk(KERN_ERR
"onenand_write_ops_nolock: verify failed %d\n", ret
);
1244 ops
->retlen
= written
;
1250 * onenand_write_oob_nolock - [Internal] OneNAND write out-of-band
1251 * @param mtd MTD device structure
1252 * @param to offset to write to
1253 * @param len number of bytes to write
1254 * @param retlen pointer to variable to store the number of written bytes
1255 * @param buf the data to write
1256 * @param mode operation mode
1258 * OneNAND write out-of-band
1260 static int onenand_write_oob_nolock(struct mtd_info
*mtd
, loff_t to
,
1261 struct mtd_oob_ops
*ops
)
1263 struct onenand_chip
*this = mtd
->priv
;
1264 int column
, ret
= 0, oobsize
;
1267 size_t len
= ops
->ooblen
;
1268 const u_char
*buf
= ops
->oobbuf
;
1269 mtd_oob_mode_t mode
= ops
->mode
;
1273 MTDDEBUG(MTD_DEBUG_LEVEL3
, "onenand_write_oob_nolock: to = 0x%08x, len = %i\n", (unsigned int) to
, (int) len
);
1275 /* Initialize retlen, in case of early exit */
1278 if (mode
== MTD_OOB_AUTO
)
1279 oobsize
= this->ecclayout
->oobavail
;
1281 oobsize
= mtd
->oobsize
;
1283 column
= to
& (mtd
->oobsize
- 1);
1285 if (unlikely(column
>= oobsize
)) {
1286 printk(KERN_ERR
"onenand_write_oob_nolock: Attempted to start write outside oob\n");
1290 /* For compatibility with NAND: Do not allow write past end of page */
1291 if (unlikely(column
+ len
> oobsize
)) {
1292 printk(KERN_ERR
"onenand_write_oob_nolock: "
1293 "Attempt to write past end of page\n");
1297 /* Do not allow reads past end of device */
1298 if (unlikely(to
>= mtd
->size
||
1299 column
+ len
> ((mtd
->size
>> this->page_shift
) -
1300 (to
>> this->page_shift
)) * oobsize
)) {
1301 printk(KERN_ERR
"onenand_write_oob_nolock: Attempted to write past end of device\n");
1305 oobbuf
= this->oob_buf
;
1307 /* Loop until all data write */
1308 while (written
< len
) {
1309 int thislen
= min_t(int, oobsize
, len
- written
);
1311 this->command(mtd
, ONENAND_CMD_BUFFERRAM
, to
, mtd
->oobsize
);
1313 /* We send data to spare ram with oobsize
1314 * to prevent byte access */
1315 memset(oobbuf
, 0xff, mtd
->oobsize
);
1316 if (mode
== MTD_OOB_AUTO
)
1317 onenand_fill_auto_oob(mtd
, oobbuf
, buf
, column
, thislen
);
1319 memcpy(oobbuf
+ column
, buf
, thislen
);
1320 this->write_bufferram(mtd
, 0, ONENAND_SPARERAM
, oobbuf
, 0, mtd
->oobsize
);
1322 this->command(mtd
, ONENAND_CMD_PROGOOB
, to
, mtd
->oobsize
);
1324 onenand_update_bufferram(mtd
, to
, 0);
1325 if (ONENAND_IS_2PLANE(this)) {
1326 ONENAND_SET_BUFFERRAM1(this);
1327 onenand_update_bufferram(mtd
, to
+ this->writesize
, 0);
1330 ret
= this->wait(mtd
, FL_WRITING
);
1332 printk(KERN_ERR
"onenand_write_oob_nolock: write failed %d\n", ret
);
1336 ret
= onenand_verify_oob(mtd
, oobbuf
, to
);
1338 printk(KERN_ERR
"onenand_write_oob_nolock: verify failed %d\n", ret
);
1346 to
+= mtd
->writesize
;
1351 ops
->oobretlen
= written
;
1357 * onenand_write - [MTD Interface] compability function for onenand_write_ecc
1358 * @param mtd MTD device structure
1359 * @param to offset to write to
1360 * @param len number of bytes to write
1361 * @param retlen pointer to variable to store the number of written bytes
1362 * @param buf the data to write
1366 int onenand_write(struct mtd_info
*mtd
, loff_t to
, size_t len
,
1367 size_t * retlen
, const u_char
* buf
)
1369 struct mtd_oob_ops ops
= {
1372 .datbuf
= (u_char
*) buf
,
1377 onenand_get_device(mtd
, FL_WRITING
);
1378 ret
= onenand_write_ops_nolock(mtd
, to
, &ops
);
1379 onenand_release_device(mtd
);
1381 *retlen
= ops
.retlen
;
1386 * onenand_write_oob - [MTD Interface] OneNAND write out-of-band
1387 * @param mtd MTD device structure
1388 * @param to offset to write to
1389 * @param ops oob operation description structure
1391 * OneNAND write main and/or out-of-band
1393 int onenand_write_oob(struct mtd_info
*mtd
, loff_t to
,
1394 struct mtd_oob_ops
*ops
)
1398 switch (ops
->mode
) {
1403 /* Not implemented yet */
1408 onenand_get_device(mtd
, FL_WRITING
);
1410 ret
= onenand_write_ops_nolock(mtd
, to
, ops
);
1412 ret
= onenand_write_oob_nolock(mtd
, to
, ops
);
1413 onenand_release_device(mtd
);
1420 * onenand_block_isbad_nolock - [GENERIC] Check if a block is marked bad
1421 * @param mtd MTD device structure
1422 * @param ofs offset from device start
1423 * @param allowbbt 1, if its allowed to access the bbt area
1425 * Check, if the block is bad, Either by reading the bad block table or
1426 * calling of the scan function.
1428 static int onenand_block_isbad_nolock(struct mtd_info
*mtd
, loff_t ofs
, int allowbbt
)
1430 struct onenand_chip
*this = mtd
->priv
;
1431 struct bbm_info
*bbm
= this->bbm
;
1433 /* Return info from the table */
1434 return bbm
->isbad_bbt(mtd
, ofs
, allowbbt
);
1439 * onenand_erase - [MTD Interface] erase block(s)
1440 * @param mtd MTD device structure
1441 * @param instr erase instruction
1443 * Erase one ore more blocks
1445 int onenand_erase(struct mtd_info
*mtd
, struct erase_info
*instr
)
1447 struct onenand_chip
*this = mtd
->priv
;
1448 unsigned int block_size
;
1453 MTDDEBUG (MTD_DEBUG_LEVEL3
,
1454 "onenand_erase: start = 0x%08x, len = %i\n",
1455 (unsigned int)instr
->addr
, (unsigned int)instr
->len
);
1457 block_size
= (1 << this->erase_shift
);
1459 /* Start address must align on block boundary */
1460 if (unlikely(instr
->addr
& (block_size
- 1))) {
1461 MTDDEBUG (MTD_DEBUG_LEVEL0
,
1462 "onenand_erase: Unaligned address\n");
1466 /* Length must align on block boundary */
1467 if (unlikely(instr
->len
& (block_size
- 1))) {
1468 MTDDEBUG (MTD_DEBUG_LEVEL0
,
1469 "onenand_erase: Length not block aligned\n");
1473 /* Do not allow erase past end of device */
1474 if (unlikely((instr
->len
+ instr
->addr
) > mtd
->size
)) {
1475 MTDDEBUG (MTD_DEBUG_LEVEL0
,
1476 "onenand_erase: Erase past end of device\n");
1480 instr
->fail_addr
= 0xffffffff;
1482 /* Grab the lock and see if the device is available */
1483 onenand_get_device(mtd
, FL_ERASING
);
1485 /* Loop throught the pages */
1489 instr
->state
= MTD_ERASING
;
1493 /* Check if we have a bad block, we do not erase bad blocks */
1494 if (instr
->priv
== 0 && onenand_block_isbad_nolock(mtd
, addr
, 0)) {
1495 printk(KERN_WARNING
"onenand_erase: attempt to erase"
1496 " a bad block at addr 0x%08x\n",
1497 (unsigned int) addr
);
1498 instr
->state
= MTD_ERASE_FAILED
;
1502 this->command(mtd
, ONENAND_CMD_ERASE
, addr
, block_size
);
1504 onenand_invalidate_bufferram(mtd
, addr
, block_size
);
1506 ret
= this->wait(mtd
, FL_ERASING
);
1507 /* Check, if it is write protected */
1510 MTDDEBUG (MTD_DEBUG_LEVEL0
, "onenand_erase: "
1511 "Device is write protected!!!\n");
1513 MTDDEBUG (MTD_DEBUG_LEVEL0
, "onenand_erase: "
1514 "Failed erase, block %d\n",
1515 (unsigned)(addr
>> this->erase_shift
));
1517 printk("onenand_erase: "
1518 "Device is write protected!!!\n");
1520 printk("onenand_erase: "
1521 "Failed erase, block %d\n",
1522 (unsigned)(addr
>> this->erase_shift
));
1523 instr
->state
= MTD_ERASE_FAILED
;
1524 instr
->fail_addr
= addr
;
1533 instr
->state
= MTD_ERASE_DONE
;
1537 ret
= instr
->state
== MTD_ERASE_DONE
? 0 : -EIO
;
1538 /* Do call back function */
1540 mtd_erase_callback(instr
);
1542 /* Deselect and wake up anyone waiting on the device */
1543 onenand_release_device(mtd
);
1549 * onenand_sync - [MTD Interface] sync
1550 * @param mtd MTD device structure
1552 * Sync is actually a wait for chip ready function
1554 void onenand_sync(struct mtd_info
*mtd
)
1556 MTDDEBUG (MTD_DEBUG_LEVEL3
, "onenand_sync: called\n");
1558 /* Grab the lock and see if the device is available */
1559 onenand_get_device(mtd
, FL_SYNCING
);
1561 /* Release it and go back */
1562 onenand_release_device(mtd
);
1566 * onenand_block_isbad - [MTD Interface] Check whether the block at the given offset is bad
1567 * @param mtd MTD device structure
1568 * @param ofs offset relative to mtd start
1570 * Check whether the block is bad
1572 int onenand_block_isbad(struct mtd_info
*mtd
, loff_t ofs
)
1576 /* Check for invalid offset */
1577 if (ofs
> mtd
->size
)
1580 onenand_get_device(mtd
, FL_READING
);
1581 ret
= onenand_block_isbad_nolock(mtd
,ofs
, 0);
1582 onenand_release_device(mtd
);
1587 * onenand_default_block_markbad - [DEFAULT] mark a block bad
1588 * @param mtd MTD device structure
1589 * @param ofs offset from device start
1591 * This is the default implementation, which can be overridden by
1592 * a hardware specific driver.
1594 static int onenand_default_block_markbad(struct mtd_info
*mtd
, loff_t ofs
)
1596 struct onenand_chip
*this = mtd
->priv
;
1597 struct bbm_info
*bbm
= this->bbm
;
1598 u_char buf
[2] = {0, 0};
1599 struct mtd_oob_ops ops
= {
1600 .mode
= MTD_OOB_PLACE
,
1607 /* Get block number */
1608 block
= ((int) ofs
) >> bbm
->bbt_erase_shift
;
1610 bbm
->bbt
[block
>> 2] |= 0x01 << ((block
& 0x03) << 1);
1612 /* We write two bytes, so we dont have to mess with 16 bit access */
1613 ofs
+= mtd
->oobsize
+ (bbm
->badblockpos
& ~0x01);
1614 return onenand_write_oob_nolock(mtd
, ofs
, &ops
);
1618 * onenand_block_markbad - [MTD Interface] Mark the block at the given offset as bad
1619 * @param mtd MTD device structure
1620 * @param ofs offset relative to mtd start
1622 * Mark the block as bad
1624 int onenand_block_markbad(struct mtd_info
*mtd
, loff_t ofs
)
1626 struct onenand_chip
*this = mtd
->priv
;
1629 ret
= onenand_block_isbad(mtd
, ofs
);
1631 /* If it was bad already, return success and do nothing */
1637 ret
= this->block_markbad(mtd
, ofs
);
1642 * onenand_do_lock_cmd - [OneNAND Interface] Lock or unlock block(s)
1643 * @param mtd MTD device structure
1644 * @param ofs offset relative to mtd start
1645 * @param len number of bytes to lock or unlock
1646 * @param cmd lock or unlock command
1648 * Lock or unlock one or more blocks
1650 static int onenand_do_lock_cmd(struct mtd_info
*mtd
, loff_t ofs
, size_t len
, int cmd
)
1652 struct onenand_chip
*this = mtd
->priv
;
1653 int start
, end
, block
, value
, status
;
1656 start
= ofs
>> this->erase_shift
;
1657 end
= len
>> this->erase_shift
;
1659 if (cmd
== ONENAND_CMD_LOCK
)
1660 wp_status_mask
= ONENAND_WP_LS
;
1662 wp_status_mask
= ONENAND_WP_US
;
1664 /* Continuous lock scheme */
1665 if (this->options
& ONENAND_HAS_CONT_LOCK
) {
1666 /* Set start block address */
1667 this->write_word(start
,
1668 this->base
+ ONENAND_REG_START_BLOCK_ADDRESS
);
1669 /* Set end block address */
1670 this->write_word(end
- 1,
1671 this->base
+ ONENAND_REG_END_BLOCK_ADDRESS
);
1672 /* Write unlock command */
1673 this->command(mtd
, cmd
, 0, 0);
1675 /* There's no return value */
1676 this->wait(mtd
, FL_UNLOCKING
);
1679 while (this->read_word(this->base
+ ONENAND_REG_CTRL_STATUS
)
1680 & ONENAND_CTRL_ONGO
)
1683 /* Check lock status */
1684 status
= this->read_word(this->base
+ ONENAND_REG_WP_STATUS
);
1685 if (!(status
& ONENAND_WP_US
))
1686 printk(KERN_ERR
"wp status = 0x%x\n", status
);
1691 /* Block lock scheme */
1692 for (block
= start
; block
< start
+ end
; block
++) {
1693 /* Set block address */
1694 value
= onenand_block_address(this, block
);
1695 this->write_word(value
, this->base
+ ONENAND_REG_START_ADDRESS1
);
1696 /* Select DataRAM for DDP */
1697 value
= onenand_bufferram_address(this, block
);
1698 this->write_word(value
, this->base
+ ONENAND_REG_START_ADDRESS2
);
1700 /* Set start block address */
1701 this->write_word(block
,
1702 this->base
+ ONENAND_REG_START_BLOCK_ADDRESS
);
1703 /* Write unlock command */
1704 this->command(mtd
, ONENAND_CMD_UNLOCK
, 0, 0);
1706 /* There's no return value */
1707 this->wait(mtd
, FL_UNLOCKING
);
1710 while (this->read_word(this->base
+ ONENAND_REG_CTRL_STATUS
)
1711 & ONENAND_CTRL_ONGO
)
1714 /* Check lock status */
1715 status
= this->read_word(this->base
+ ONENAND_REG_WP_STATUS
);
1716 if (!(status
& ONENAND_WP_US
))
1717 printk(KERN_ERR
"block = %d, wp status = 0x%x\n",
1724 #ifdef ONENAND_LINUX
1726 * onenand_lock - [MTD Interface] Lock block(s)
1727 * @param mtd MTD device structure
1728 * @param ofs offset relative to mtd start
1729 * @param len number of bytes to unlock
1731 * Lock one or more blocks
1733 static int onenand_lock(struct mtd_info
*mtd
, loff_t ofs
, size_t len
)
1737 onenand_get_device(mtd
, FL_LOCKING
);
1738 ret
= onenand_do_lock_cmd(mtd
, ofs
, len
, ONENAND_CMD_LOCK
);
1739 onenand_release_device(mtd
);
1744 * onenand_unlock - [MTD Interface] Unlock block(s)
1745 * @param mtd MTD device structure
1746 * @param ofs offset relative to mtd start
1747 * @param len number of bytes to unlock
1749 * Unlock one or more blocks
1751 static int onenand_unlock(struct mtd_info
*mtd
, loff_t ofs
, size_t len
)
1755 onenand_get_device(mtd
, FL_LOCKING
);
1756 ret
= onenand_do_lock_cmd(mtd
, ofs
, len
, ONENAND_CMD_UNLOCK
);
1757 onenand_release_device(mtd
);
1763 * onenand_check_lock_status - [OneNAND Interface] Check lock status
1764 * @param this onenand chip data structure
1768 static int onenand_check_lock_status(struct onenand_chip
*this)
1770 unsigned int value
, block
, status
;
1773 end
= this->chipsize
>> this->erase_shift
;
1774 for (block
= 0; block
< end
; block
++) {
1775 /* Set block address */
1776 value
= onenand_block_address(this, block
);
1777 this->write_word(value
, this->base
+ ONENAND_REG_START_ADDRESS1
);
1778 /* Select DataRAM for DDP */
1779 value
= onenand_bufferram_address(this, block
);
1780 this->write_word(value
, this->base
+ ONENAND_REG_START_ADDRESS2
);
1781 /* Set start block address */
1782 this->write_word(block
, this->base
+ ONENAND_REG_START_BLOCK_ADDRESS
);
1784 /* Check lock status */
1785 status
= this->read_word(this->base
+ ONENAND_REG_WP_STATUS
);
1786 if (!(status
& ONENAND_WP_US
)) {
1787 printk(KERN_ERR
"block = %d, wp status = 0x%x\n", block
, status
);
1796 * onenand_unlock_all - [OneNAND Interface] unlock all blocks
1797 * @param mtd MTD device structure
1801 static void onenand_unlock_all(struct mtd_info
*mtd
)
1803 struct onenand_chip
*this = mtd
->priv
;
1805 size_t len
= this->chipsize
;
1807 if (this->options
& ONENAND_HAS_UNLOCK_ALL
) {
1808 /* Set start block address */
1809 this->write_word(0, this->base
+ ONENAND_REG_START_BLOCK_ADDRESS
);
1810 /* Write unlock command */
1811 this->command(mtd
, ONENAND_CMD_UNLOCK_ALL
, 0, 0);
1813 /* There's no return value */
1814 this->wait(mtd
, FL_LOCKING
);
1817 while (this->read_word(this->base
+ ONENAND_REG_CTRL_STATUS
)
1818 & ONENAND_CTRL_ONGO
)
1823 /* Check lock status */
1824 if (onenand_check_lock_status(this))
1827 /* Workaround for all block unlock in DDP */
1828 if (ONENAND_IS_DDP(this)) {
1829 /* All blocks on another chip */
1830 ofs
= this->chipsize
>> 1;
1831 len
= this->chipsize
>> 1;
1835 onenand_do_lock_cmd(mtd
, ofs
, len
, ONENAND_CMD_UNLOCK
);
1840 * onenand_check_features - Check and set OneNAND features
1841 * @param mtd MTD data structure
1843 * Check and set OneNAND features
1847 static void onenand_check_features(struct mtd_info
*mtd
)
1849 struct onenand_chip
*this = mtd
->priv
;
1850 unsigned int density
, process
;
1852 /* Lock scheme depends on density and process */
1853 density
= onenand_get_density(this->device_id
);
1854 process
= this->version_id
>> ONENAND_VERSION_PROCESS_SHIFT
;
1858 case ONENAND_DEVICE_DENSITY_4Gb
:
1859 this->options
|= ONENAND_HAS_2PLANE
;
1861 case ONENAND_DEVICE_DENSITY_2Gb
:
1862 /* 2Gb DDP don't have 2 plane */
1863 if (!ONENAND_IS_DDP(this))
1864 this->options
|= ONENAND_HAS_2PLANE
;
1865 this->options
|= ONENAND_HAS_UNLOCK_ALL
;
1867 case ONENAND_DEVICE_DENSITY_1Gb
:
1868 /* A-Die has all block unlock */
1870 this->options
|= ONENAND_HAS_UNLOCK_ALL
;
1874 /* Some OneNAND has continuous lock scheme */
1876 this->options
|= ONENAND_HAS_CONT_LOCK
;
1880 if (this->options
& ONENAND_HAS_CONT_LOCK
)
1881 printk(KERN_DEBUG
"Lock scheme is Continuous Lock\n");
1882 if (this->options
& ONENAND_HAS_UNLOCK_ALL
)
1883 printk(KERN_DEBUG
"Chip support all block unlock\n");
1884 if (this->options
& ONENAND_HAS_2PLANE
)
1885 printk(KERN_DEBUG
"Chip has 2 plane\n");
1889 * onenand_print_device_info - Print device ID
1890 * @param device device ID
1894 char *onenand_print_device_info(int device
, int version
)
1896 int vcc
, demuxed
, ddp
, density
;
1897 char *dev_info
= malloc(80);
1900 vcc
= device
& ONENAND_DEVICE_VCC_MASK
;
1901 demuxed
= device
& ONENAND_DEVICE_IS_DEMUX
;
1902 ddp
= device
& ONENAND_DEVICE_IS_DDP
;
1903 density
= device
>> ONENAND_DEVICE_DENSITY_SHIFT
;
1904 p
+= sprintf(dev_info
, "%sOneNAND%s %dMB %sV 16-bit (0x%02x)",
1905 demuxed
? "" : "Muxed ",
1907 (16 << density
), vcc
? "2.65/3.3" : "1.8", device
);
1909 sprintf(p
, "\nOneNAND version = 0x%04x", version
);
1910 printk("%s\n", dev_info
);
1915 static const struct onenand_manufacturers onenand_manuf_ids
[] = {
1916 {ONENAND_MFR_SAMSUNG
, "Samsung"},
1920 * onenand_check_maf - Check manufacturer ID
1921 * @param manuf manufacturer ID
1923 * Check manufacturer ID
1925 static int onenand_check_maf(int manuf
)
1927 int size
= ARRAY_SIZE(onenand_manuf_ids
);
1931 for (i
= 0; size
; i
++)
1932 if (manuf
== onenand_manuf_ids
[i
].id
)
1936 name
= onenand_manuf_ids
[i
].name
;
1940 #ifdef ONENAND_DEBUG
1941 printk(KERN_DEBUG
"OneNAND Manufacturer: %s (0x%0x)\n", name
, manuf
);
1948 * onenand_probe - [OneNAND Interface] Probe the OneNAND device
1949 * @param mtd MTD device structure
1951 * OneNAND detection method:
1952 * Compare the the values from command with ones from register
1954 static int onenand_probe(struct mtd_info
*mtd
)
1956 struct onenand_chip
*this = mtd
->priv
;
1957 int bram_maf_id
, bram_dev_id
, maf_id
, dev_id
, ver_id
;
1961 /* Save system configuration 1 */
1962 syscfg
= this->read_word(this->base
+ ONENAND_REG_SYS_CFG1
);
1963 /* Clear Sync. Burst Read mode to read BootRAM */
1964 this->write_word((syscfg
& ~ONENAND_SYS_CFG1_SYNC_READ
), this->base
+ ONENAND_REG_SYS_CFG1
);
1966 /* Send the command for reading device ID from BootRAM */
1967 this->write_word(ONENAND_CMD_READID
, this->base
+ ONENAND_BOOTRAM
);
1969 /* Read manufacturer and device IDs from BootRAM */
1970 bram_maf_id
= this->read_word(this->base
+ ONENAND_BOOTRAM
+ 0x0);
1971 bram_dev_id
= this->read_word(this->base
+ ONENAND_BOOTRAM
+ 0x2);
1973 /* Reset OneNAND to read default register values */
1974 this->write_word(ONENAND_CMD_RESET
, this->base
+ ONENAND_BOOTRAM
);
1977 this->wait(mtd
, FL_RESETING
);
1979 /* Restore system configuration 1 */
1980 this->write_word(syscfg
, this->base
+ ONENAND_REG_SYS_CFG1
);
1982 /* Check manufacturer ID */
1983 if (onenand_check_maf(bram_maf_id
))
1986 /* Read manufacturer and device IDs from Register */
1987 maf_id
= this->read_word(this->base
+ ONENAND_REG_MANUFACTURER_ID
);
1988 dev_id
= this->read_word(this->base
+ ONENAND_REG_DEVICE_ID
);
1989 ver_id
= this->read_word(this->base
+ ONENAND_REG_VERSION_ID
);
1991 /* Check OneNAND device */
1992 if (maf_id
!= bram_maf_id
|| dev_id
!= bram_dev_id
)
1995 /* FIXME : Current OneNAND MTD doesn't support Flex-OneNAND */
1996 if (dev_id
& (1 << 9)) {
1997 printk("Not yet support Flex-OneNAND\n");
2001 /* Flash device information */
2002 mtd
->name
= onenand_print_device_info(dev_id
, ver_id
);
2003 this->device_id
= dev_id
;
2004 this->version_id
= ver_id
;
2006 density
= onenand_get_density(dev_id
);
2007 this->chipsize
= (16 << density
) << 20;
2008 /* Set density mask. it is used for DDP */
2009 if (ONENAND_IS_DDP(this))
2010 this->density_mask
= (1 << (density
+ 6));
2012 this->density_mask
= 0;
2014 /* OneNAND page size & block size */
2015 /* The data buffer size is equal to page size */
2017 this->read_word(this->base
+ ONENAND_REG_DATA_BUFFER_SIZE
);
2018 mtd
->oobsize
= mtd
->writesize
>> 5;
2019 /* Pagers per block is always 64 in OneNAND */
2020 mtd
->erasesize
= mtd
->writesize
<< 6;
2022 this->erase_shift
= ffs(mtd
->erasesize
) - 1;
2023 this->page_shift
= ffs(mtd
->writesize
) - 1;
2024 this->ppb_shift
= (this->erase_shift
- this->page_shift
);
2025 this->page_mask
= (mtd
->erasesize
/ mtd
->writesize
) - 1;
2026 /* It's real page size */
2027 this->writesize
= mtd
->writesize
;
2029 /* REVIST: Multichip handling */
2031 mtd
->size
= this->chipsize
;
2033 /* Check OneNAND features */
2034 onenand_check_features(mtd
);
2036 mtd
->flags
= MTD_CAP_NANDFLASH
;
2037 mtd
->erase
= onenand_erase
;
2038 mtd
->read
= onenand_read
;
2039 mtd
->write
= onenand_write
;
2040 mtd
->read_oob
= onenand_read_oob
;
2041 mtd
->write_oob
= onenand_write_oob
;
2042 mtd
->sync
= onenand_sync
;
2043 mtd
->block_isbad
= onenand_block_isbad
;
2044 mtd
->block_markbad
= onenand_block_markbad
;
2050 * onenand_scan - [OneNAND Interface] Scan for the OneNAND device
2051 * @param mtd MTD device structure
2052 * @param maxchips Number of chips to scan for
2054 * This fills out all the not initialized function pointers
2055 * with the defaults.
2056 * The flash ID is read and the mtd/chip structures are
2057 * filled with the appropriate values.
2059 int onenand_scan(struct mtd_info
*mtd
, int maxchips
)
2061 struct onenand_chip
*this = mtd
->priv
;
2063 if (!this->read_word
)
2064 this->read_word
= onenand_readw
;
2065 if (!this->write_word
)
2066 this->write_word
= onenand_writew
;
2069 this->command
= onenand_command
;
2071 this->wait
= onenand_wait
;
2072 if (!this->bbt_wait
)
2073 this->bbt_wait
= onenand_bbt_wait
;
2075 if (!this->read_bufferram
)
2076 this->read_bufferram
= onenand_read_bufferram
;
2077 if (!this->read_spareram
)
2078 this->read_spareram
= onenand_read_bufferram
;
2079 if (!this->write_bufferram
)
2080 this->write_bufferram
= onenand_write_bufferram
;
2082 if (!this->block_markbad
)
2083 this->block_markbad
= onenand_default_block_markbad
;
2084 if (!this->scan_bbt
)
2085 this->scan_bbt
= onenand_default_bbt
;
2087 if (onenand_probe(mtd
))
2090 /* Set Sync. Burst Read after probing */
2091 if (this->mmcontrol
) {
2092 printk(KERN_INFO
"OneNAND Sync. Burst Read support\n");
2093 this->read_bufferram
= onenand_sync_read_bufferram
;
2096 /* Allocate buffers, if necessary */
2097 if (!this->page_buf
) {
2098 this->page_buf
= kzalloc(mtd
->writesize
, GFP_KERNEL
);
2099 if (!this->page_buf
) {
2100 printk(KERN_ERR
"onenand_scan(): Can't allocate page_buf\n");
2103 this->options
|= ONENAND_PAGEBUF_ALLOC
;
2105 if (!this->oob_buf
) {
2106 this->oob_buf
= kzalloc(mtd
->oobsize
, GFP_KERNEL
);
2107 if (!this->oob_buf
) {
2108 printk(KERN_ERR
"onenand_scan: Can't allocate oob_buf\n");
2109 if (this->options
& ONENAND_PAGEBUF_ALLOC
) {
2110 this->options
&= ~ONENAND_PAGEBUF_ALLOC
;
2111 kfree(this->page_buf
);
2115 this->options
|= ONENAND_OOBBUF_ALLOC
;
2118 /* Unlock whole block */
2119 onenand_unlock_all(mtd
);
2121 return this->scan_bbt(mtd
);
2125 * onenand_release - [OneNAND Interface] Free resources held by the OneNAND device
2126 * @param mtd MTD device structure
2128 void onenand_release(struct mtd_info
*mtd
)