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(int device
, int block
)
83 if (device
& ONENAND_DEVICE_IS_DDP
) {
84 /* Device Flash Core select, NAND Flash Block Address */
85 int dfs
= 0, density
, mask
;
87 density
= device
>> ONENAND_DEVICE_DENSITY_SHIFT
;
88 mask
= (1 << (density
+ 6));
93 return (dfs
<< ONENAND_DDP_SHIFT
) | (block
& (mask
- 1));
100 * onenand_bufferram_address - [DEFAULT] Get bufferram address
101 * @param device the device id
102 * @param block the block
103 * @return set DBS value if DDP, otherwise 0
105 * Setup Start Address 2 Register (F101h) for DDP
107 static int onenand_bufferram_address(int device
, int block
)
109 if (device
& ONENAND_DEVICE_IS_DDP
) {
110 /* Device BufferRAM Select */
111 int dbs
= 0, density
, mask
;
113 density
= device
>> ONENAND_DEVICE_DENSITY_SHIFT
;
114 mask
= (1 << (density
+ 6));
119 return (dbs
<< ONENAND_DDP_SHIFT
);
126 * onenand_page_address - [DEFAULT] Get page address
127 * @param page the page address
128 * @param sector the sector address
129 * @return combined page and sector address
131 * Setup Start Address 8 Register (F107h)
133 static int onenand_page_address(int page
, int sector
)
135 /* Flash Page Address, Flash Sector Address */
138 fpa
= page
& ONENAND_FPA_MASK
;
139 fsa
= sector
& ONENAND_FSA_MASK
;
141 return ((fpa
<< ONENAND_FPA_SHIFT
) | fsa
);
145 * onenand_buffer_address - [DEFAULT] Get buffer address
146 * @param dataram1 DataRAM index
147 * @param sectors the sector address
148 * @param count the number of sectors
149 * @return the start buffer value
151 * Setup Start Buffer Register (F200h)
153 static int onenand_buffer_address(int dataram1
, int sectors
, int count
)
157 /* BufferRAM Sector Address */
158 bsa
= sectors
& ONENAND_BSA_MASK
;
161 bsa
|= ONENAND_BSA_DATARAM1
; /* DataRAM1 */
163 bsa
|= ONENAND_BSA_DATARAM0
; /* DataRAM0 */
165 /* BufferRAM Sector Count */
166 bsc
= count
& ONENAND_BSC_MASK
;
168 return ((bsa
<< ONENAND_BSA_SHIFT
) | bsc
);
172 * onenand_command - [DEFAULT] Send command to OneNAND device
173 * @param mtd MTD device structure
174 * @param cmd the command to be sent
175 * @param addr offset to read from or write to
176 * @param len number of bytes to read or write
178 * Send command to OneNAND device. This function is used for middle/large page
179 * devices (1KB/2KB Bytes per page)
181 static int onenand_command(struct mtd_info
*mtd
, int cmd
, loff_t addr
,
184 struct onenand_chip
*this = mtd
->priv
;
185 int value
, readcmd
= 0;
187 /* Now we use page size operation */
188 int sectors
= 4, count
= 4;
190 /* Address translation */
192 case ONENAND_CMD_UNLOCK
:
193 case ONENAND_CMD_LOCK
:
194 case ONENAND_CMD_LOCK_TIGHT
:
199 case ONENAND_CMD_ERASE
:
200 case ONENAND_CMD_BUFFERRAM
:
201 block
= (int)(addr
>> this->erase_shift
);
206 block
= (int)(addr
>> this->erase_shift
);
207 page
= (int)(addr
>> this->page_shift
);
208 page
&= this->page_mask
;
212 /* NOTE: The setting order of the registers is very important! */
213 if (cmd
== ONENAND_CMD_BUFFERRAM
) {
214 /* Select DataRAM for DDP */
215 value
= onenand_bufferram_address(this->device_id
, block
);
216 this->write_word(value
,
217 this->base
+ ONENAND_REG_START_ADDRESS2
);
219 /* Switch to the next data buffer */
220 ONENAND_SET_NEXT_BUFFERRAM(this);
226 /* Write 'DFS, FBA' of Flash */
227 value
= onenand_block_address(this->device_id
, block
);
228 this->write_word(value
,
229 this->base
+ ONENAND_REG_START_ADDRESS1
);
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 /* Select DataRAM for DDP */
259 onenand_bufferram_address(this->device_id
, block
);
260 this->write_word(value
,
262 ONENAND_REG_START_ADDRESS2
);
266 /* Interrupt clear */
267 this->write_word(ONENAND_INT_CLEAR
, this->base
+ ONENAND_REG_INTERRUPT
);
269 this->write_word(cmd
, this->base
+ ONENAND_REG_COMMAND
);
275 * onenand_wait - [DEFAULT] wait until the command is done
276 * @param mtd MTD device structure
277 * @param state state to select the max. timeout value
279 * Wait for command done. This applies to all OneNAND command
280 * Read can take up to 30us, erase up to 2ms and program up to 350us
281 * according to general OneNAND specs
283 static int onenand_wait(struct mtd_info
*mtd
, int state
)
285 struct onenand_chip
*this = mtd
->priv
;
286 unsigned int flags
= ONENAND_INT_MASTER
;
287 unsigned int interrupt
= 0;
288 unsigned int ctrl
, ecc
;
291 interrupt
= this->read_word(this->base
+ ONENAND_REG_INTERRUPT
);
292 if (interrupt
& flags
)
296 ctrl
= this->read_word(this->base
+ ONENAND_REG_CTRL_STATUS
);
298 if (ctrl
& ONENAND_CTRL_ERROR
) {
299 MTDDEBUG (MTD_DEBUG_LEVEL0
,
300 "onenand_wait: controller error = 0x%04x\n", ctrl
);
304 if (ctrl
& ONENAND_CTRL_LOCK
) {
305 MTDDEBUG (MTD_DEBUG_LEVEL0
,
306 "onenand_wait: it's locked error = 0x%04x\n", ctrl
);
310 if (interrupt
& ONENAND_INT_READ
) {
311 ecc
= this->read_word(this->base
+ ONENAND_REG_ECC_STATUS
);
312 if (ecc
& ONENAND_ECC_2BIT_ALL
) {
313 MTDDEBUG (MTD_DEBUG_LEVEL0
,
314 "onenand_wait: ECC error = 0x%04x\n", ecc
);
323 * onenand_bufferram_offset - [DEFAULT] BufferRAM offset
324 * @param mtd MTD data structure
325 * @param area BufferRAM area
326 * @return offset given area
328 * Return BufferRAM offset given area
330 static inline int onenand_bufferram_offset(struct mtd_info
*mtd
, int area
)
332 struct onenand_chip
*this = mtd
->priv
;
334 if (ONENAND_CURRENT_BUFFERRAM(this)) {
335 if (area
== ONENAND_DATARAM
)
336 return mtd
->writesize
;
337 if (area
== ONENAND_SPARERAM
)
345 * onenand_read_bufferram - [OneNAND Interface] Read the bufferram area
346 * @param mtd MTD data structure
347 * @param area BufferRAM area
348 * @param buffer the databuffer to put/get data
349 * @param offset offset to read from or write to
350 * @param count number of bytes to read/write
352 * Read the BufferRAM area
354 static int onenand_read_bufferram(struct mtd_info
*mtd
, int area
,
355 unsigned char *buffer
, int offset
,
358 struct onenand_chip
*this = mtd
->priv
;
359 void __iomem
*bufferram
;
361 bufferram
= this->base
+ area
;
362 bufferram
+= onenand_bufferram_offset(mtd
, area
);
364 memcpy_16(buffer
, bufferram
+ offset
, count
);
370 * onenand_sync_read_bufferram - [OneNAND Interface] Read the bufferram area with Sync. Burst mode
371 * @param mtd MTD data structure
372 * @param area BufferRAM area
373 * @param buffer the databuffer to put/get data
374 * @param offset offset to read from or write to
375 * @param count number of bytes to read/write
377 * Read the BufferRAM area with Sync. Burst Mode
379 static int onenand_sync_read_bufferram(struct mtd_info
*mtd
, int area
,
380 unsigned char *buffer
, int offset
,
383 struct onenand_chip
*this = mtd
->priv
;
384 void __iomem
*bufferram
;
386 bufferram
= this->base
+ area
;
387 bufferram
+= onenand_bufferram_offset(mtd
, area
);
389 this->mmcontrol(mtd
, ONENAND_SYS_CFG1_SYNC_READ
);
391 memcpy_16(buffer
, bufferram
+ offset
, count
);
393 this->mmcontrol(mtd
, 0);
399 * onenand_write_bufferram - [OneNAND Interface] Write the bufferram area
400 * @param mtd MTD data structure
401 * @param area BufferRAM area
402 * @param buffer the databuffer to put/get data
403 * @param offset offset to read from or write to
404 * @param count number of bytes to read/write
406 * Write the BufferRAM area
408 static int onenand_write_bufferram(struct mtd_info
*mtd
, int area
,
409 const unsigned char *buffer
, int offset
,
412 struct onenand_chip
*this = mtd
->priv
;
413 void __iomem
*bufferram
;
415 bufferram
= this->base
+ area
;
416 bufferram
+= onenand_bufferram_offset(mtd
, area
);
418 memcpy_16(bufferram
+ offset
, buffer
, count
);
424 * onenand_check_bufferram - [GENERIC] Check BufferRAM information
425 * @param mtd MTD data structure
426 * @param addr address to check
427 * @return 1 if there are valid data, otherwise 0
429 * Check bufferram if there is data we required
431 static int onenand_check_bufferram(struct mtd_info
*mtd
, loff_t addr
)
433 struct onenand_chip
*this = mtd
->priv
;
437 block
= (int)(addr
>> this->erase_shift
);
438 page
= (int)(addr
>> this->page_shift
);
439 page
&= this->page_mask
;
441 i
= ONENAND_CURRENT_BUFFERRAM(this);
443 /* Is there valid data? */
444 if (this->bufferram
[i
].block
== block
&&
445 this->bufferram
[i
].page
== page
&& this->bufferram
[i
].valid
)
452 * onenand_update_bufferram - [GENERIC] Update BufferRAM information
453 * @param mtd MTD data structure
454 * @param addr address to update
455 * @param valid valid flag
457 * Update BufferRAM information
459 static int onenand_update_bufferram(struct mtd_info
*mtd
, loff_t addr
,
462 struct onenand_chip
*this = mtd
->priv
;
466 block
= (int)(addr
>> this->erase_shift
);
467 page
= (int)(addr
>> this->page_shift
);
468 page
&= this->page_mask
;
470 /* Invalidate BufferRAM */
471 for (i
= 0; i
< MAX_BUFFERRAM
; i
++) {
472 if (this->bufferram
[i
].block
== block
&&
473 this->bufferram
[i
].page
== page
)
474 this->bufferram
[i
].valid
= 0;
477 /* Update BufferRAM */
478 i
= ONENAND_CURRENT_BUFFERRAM(this);
479 this->bufferram
[i
].block
= block
;
480 this->bufferram
[i
].page
= page
;
481 this->bufferram
[i
].valid
= valid
;
487 * onenand_invalidate_bufferram - [GENERIC] Invalidate BufferRAM information
488 * @param mtd MTD data structure
489 * @param addr start address to invalidate
490 * @param len length to invalidate
492 * Invalidate BufferRAM information
494 static void onenand_invalidate_bufferram(struct mtd_info
*mtd
, loff_t addr
,
497 struct onenand_chip
*this = mtd
->priv
;
499 loff_t end_addr
= addr
+ len
;
501 /* Invalidate BufferRAM */
502 for (i
= 0; i
< MAX_BUFFERRAM
; i
++) {
503 loff_t buf_addr
= this->bufferram
[i
].block
<< this->erase_shift
;
505 if (buf_addr
>= addr
&& buf_addr
< end_addr
)
506 this->bufferram
[i
].valid
= 0;
511 * onenand_get_device - [GENERIC] Get chip for selected access
512 * @param mtd MTD device structure
513 * @param new_state the state which is requested
515 * Get the device and lock it for exclusive access
517 static void onenand_get_device(struct mtd_info
*mtd
, int new_state
)
523 * onenand_release_device - [GENERIC] release chip
524 * @param mtd MTD device structure
526 * Deselect, release chip lock and wake up anyone waiting on the device
528 static void onenand_release_device(struct mtd_info
*mtd
)
534 * onenand_transfer_auto_oob - [Internal] oob auto-placement transfer
535 * @param mtd MTD device structure
536 * @param buf destination address
537 * @param column oob offset to read from
538 * @param thislen oob length to read
540 static int onenand_transfer_auto_oob(struct mtd_info
*mtd
, uint8_t *buf
,
541 int column
, int thislen
)
543 struct onenand_chip
*this = mtd
->priv
;
544 struct nand_oobfree
*free
;
545 int readcol
= column
;
546 int readend
= column
+ thislen
;
549 uint8_t *oob_buf
= this->oob_buf
;
551 free
= this->ecclayout
->oobfree
;
552 for (i
= 0; i
< MTD_MAX_OOBFREE_ENTRIES
&& free
->length
; i
++, free
++) {
553 if (readcol
>= lastgap
)
554 readcol
+= free
->offset
- lastgap
;
555 if (readend
>= lastgap
)
556 readend
+= free
->offset
- lastgap
;
557 lastgap
= free
->offset
+ free
->length
;
559 this->read_bufferram(mtd
, ONENAND_SPARERAM
, oob_buf
, 0, mtd
->oobsize
);
560 free
= this->ecclayout
->oobfree
;
561 for (i
= 0; i
< MTD_MAX_OOBFREE_ENTRIES
&& free
->length
; i
++, free
++) {
562 int free_end
= free
->offset
+ free
->length
;
563 if (free
->offset
< readend
&& free_end
> readcol
) {
564 int st
= max_t(int,free
->offset
,readcol
);
565 int ed
= min_t(int,free_end
,readend
);
567 memcpy(buf
, oob_buf
+ st
, n
);
569 } else if (column
== 0)
576 * onenand_read_ops_nolock - [OneNAND Interface] OneNAND read main and/or out-of-band
577 * @param mtd MTD device structure
578 * @param from offset to read from
579 * @param ops oob operation description structure
581 * OneNAND read main and/or out-of-band data
583 static int onenand_read_ops_nolock(struct mtd_info
*mtd
, loff_t from
,
584 struct mtd_oob_ops
*ops
)
586 struct onenand_chip
*this = mtd
->priv
;
587 struct mtd_ecc_stats stats
;
588 size_t len
= ops
->len
;
589 size_t ooblen
= ops
->ooblen
;
590 u_char
*buf
= ops
->datbuf
;
591 u_char
*oobbuf
= ops
->oobbuf
;
592 int read
= 0, column
, thislen
;
593 int oobread
= 0, oobcolumn
, thisooblen
, oobsize
;
594 int ret
= 0, boundary
= 0;
595 int writesize
= this->writesize
;
597 MTDDEBUG(MTD_DEBUG_LEVEL3
,
598 "onenand_read_ops_nolock: from = 0x%08x, len = %i\n",
599 (unsigned int) from
, (int) len
);
601 if (ops
->mode
== MTD_OOB_AUTO
)
602 oobsize
= this->ecclayout
->oobavail
;
604 oobsize
= mtd
->oobsize
;
606 oobcolumn
= from
& (mtd
->oobsize
- 1);
608 /* Do not allow reads past end of device */
609 if ((from
+ len
) > mtd
->size
) {
610 printk(KERN_ERR
"onenand_read_ops_nolock: Attempt read beyond end of device\n");
616 stats
= mtd
->ecc_stats
;
618 /* Read-while-load method */
620 /* Do first load to bufferRAM */
622 if (!onenand_check_bufferram(mtd
, from
)) {
623 this->command(mtd
, ONENAND_CMD_READ
, from
, writesize
);
624 ret
= this->wait(mtd
, FL_READING
);
625 onenand_update_bufferram(mtd
, from
, !ret
);
631 thislen
= min_t(int, writesize
, len
- read
);
632 column
= from
& (writesize
- 1);
633 if (column
+ thislen
> writesize
)
634 thislen
= writesize
- column
;
637 /* If there is more to load then start next load */
639 if (read
+ thislen
< len
) {
640 this->command(mtd
, ONENAND_CMD_READ
, from
, writesize
);
642 * Chip boundary handling in DDP
643 * Now we issued chip 1 read and pointed chip 1
644 * bufferam so we have to point chip 0 bufferam.
646 if (ONENAND_IS_DDP(this) &&
647 unlikely(from
== (this->chipsize
>> 1))) {
648 this->write_word(ONENAND_DDP_CHIP0
, this->base
+ ONENAND_REG_START_ADDRESS2
);
652 ONENAND_SET_PREV_BUFFERRAM(this);
655 /* While load is going, read from last bufferRAM */
656 this->read_bufferram(mtd
, ONENAND_DATARAM
, buf
, column
, thislen
);
658 /* Read oob area if needed */
660 thisooblen
= oobsize
- oobcolumn
;
661 thisooblen
= min_t(int, thisooblen
, ooblen
- oobread
);
663 if (ops
->mode
== MTD_OOB_AUTO
)
664 onenand_transfer_auto_oob(mtd
, oobbuf
, oobcolumn
, thisooblen
);
666 this->read_bufferram(mtd
, ONENAND_SPARERAM
, oobbuf
, oobcolumn
, thisooblen
);
667 oobread
+= thisooblen
;
668 oobbuf
+= thisooblen
;
672 /* See if we are done */
676 /* Set up for next read from bufferRAM */
677 if (unlikely(boundary
))
678 this->write_word(ONENAND_DDP_CHIP1
, this->base
+ ONENAND_REG_START_ADDRESS2
);
679 ONENAND_SET_NEXT_BUFFERRAM(this);
681 thislen
= min_t(int, writesize
, len
- read
);
684 /* Now wait for load */
685 ret
= this->wait(mtd
, FL_READING
);
686 onenand_update_bufferram(mtd
, from
, !ret
);
692 * Return success, if no ECC failures, else -EBADMSG
693 * fs driver will take care of that, because
694 * retlen == desired len and result == -EBADMSG
697 ops
->oobretlen
= oobread
;
702 if (mtd
->ecc_stats
.failed
- stats
.failed
)
705 return mtd
->ecc_stats
.corrected
- stats
.corrected
? -EUCLEAN
: 0;
709 * onenand_read_oob_nolock - [MTD Interface] OneNAND read out-of-band
710 * @param mtd MTD device structure
711 * @param from offset to read from
712 * @param ops oob operation description structure
714 * OneNAND read out-of-band data from the spare area
716 static int onenand_read_oob_nolock(struct mtd_info
*mtd
, loff_t from
,
717 struct mtd_oob_ops
*ops
)
719 struct onenand_chip
*this = mtd
->priv
;
720 struct mtd_ecc_stats stats
;
721 int read
= 0, thislen
, column
, oobsize
;
722 size_t len
= ops
->ooblen
;
723 mtd_oob_mode_t mode
= ops
->mode
;
724 u_char
*buf
= ops
->oobbuf
;
727 from
+= ops
->ooboffs
;
729 MTDDEBUG(MTD_DEBUG_LEVEL3
,
730 "onenand_read_oob_nolock: from = 0x%08x, len = %i\n",
731 (unsigned int) from
, (int) len
);
733 /* Initialize return length value */
736 if (mode
== MTD_OOB_AUTO
)
737 oobsize
= this->ecclayout
->oobavail
;
739 oobsize
= mtd
->oobsize
;
741 column
= from
& (mtd
->oobsize
- 1);
743 if (unlikely(column
>= oobsize
)) {
744 printk(KERN_ERR
"onenand_read_oob_nolock: Attempted to start read outside oob\n");
748 /* Do not allow reads past end of device */
749 if (unlikely(from
>= mtd
->size
||
750 column
+ len
> ((mtd
->size
>> this->page_shift
) -
751 (from
>> this->page_shift
)) * oobsize
)) {
752 printk(KERN_ERR
"onenand_read_oob_nolock: Attempted to read beyond end of device\n");
756 stats
= mtd
->ecc_stats
;
759 thislen
= oobsize
- column
;
760 thislen
= min_t(int, thislen
, len
);
762 this->command(mtd
, ONENAND_CMD_READOOB
, from
, mtd
->oobsize
);
764 onenand_update_bufferram(mtd
, from
, 0);
766 ret
= this->wait(mtd
, FL_READING
);
767 if (ret
&& ret
!= -EBADMSG
) {
768 printk(KERN_ERR
"onenand_read_oob_nolock: read failed = 0x%x\n", ret
);
772 if (mode
== MTD_OOB_AUTO
)
773 onenand_transfer_auto_oob(mtd
, buf
, column
, thislen
);
775 this->read_bufferram(mtd
, ONENAND_SPARERAM
, buf
, column
, thislen
);
787 from
+= mtd
->writesize
;
792 ops
->oobretlen
= read
;
797 if (mtd
->ecc_stats
.failed
- stats
.failed
)
804 * onenand_read - [MTD Interface] MTD compability function for onenand_read_ecc
805 * @param mtd MTD device structure
806 * @param from offset to read from
807 * @param len number of bytes to read
808 * @param retlen pointer to variable to store the number of read bytes
809 * @param buf the databuffer to put data
811 * This function simply calls onenand_read_ecc with oob buffer and oobsel = NULL
813 int onenand_read(struct mtd_info
*mtd
, loff_t from
, size_t len
,
814 size_t * retlen
, u_char
* buf
)
816 struct mtd_oob_ops ops
= {
824 onenand_get_device(mtd
, FL_READING
);
825 ret
= onenand_read_ops_nolock(mtd
, from
, &ops
);
826 onenand_release_device(mtd
);
828 *retlen
= ops
.retlen
;
833 * onenand_read_oob - [MTD Interface] OneNAND read out-of-band
834 * @param mtd MTD device structure
835 * @param from offset to read from
836 * @param ops oob operations description structure
838 * OneNAND main and/or out-of-band
840 int onenand_read_oob(struct mtd_info
*mtd
, loff_t from
,
841 struct mtd_oob_ops
*ops
)
850 /* Not implemented yet */
855 onenand_get_device(mtd
, FL_READING
);
857 ret
= onenand_read_ops_nolock(mtd
, from
, ops
);
859 ret
= onenand_read_oob_nolock(mtd
, from
, ops
);
860 onenand_release_device(mtd
);
866 * onenand_bbt_wait - [DEFAULT] wait until the command is done
867 * @param mtd MTD device structure
868 * @param state state to select the max. timeout value
870 * Wait for command done.
872 static int onenand_bbt_wait(struct mtd_info
*mtd
, int state
)
874 struct onenand_chip
*this = mtd
->priv
;
875 unsigned int flags
= ONENAND_INT_MASTER
;
876 unsigned int interrupt
;
880 interrupt
= this->read_word(this->base
+ ONENAND_REG_INTERRUPT
);
881 if (interrupt
& flags
)
885 /* To get correct interrupt status in timeout case */
886 interrupt
= this->read_word(this->base
+ ONENAND_REG_INTERRUPT
);
887 ctrl
= this->read_word(this->base
+ ONENAND_REG_CTRL_STATUS
);
889 /* Initial bad block case: 0x2400 or 0x0400 */
890 if (ctrl
& ONENAND_CTRL_ERROR
) {
891 printk(KERN_DEBUG
"onenand_bbt_wait: controller error = 0x%04x\n", ctrl
);
892 return ONENAND_BBT_READ_ERROR
;
895 if (interrupt
& ONENAND_INT_READ
) {
896 int ecc
= this->read_word(this->base
+ ONENAND_REG_ECC_STATUS
);
897 if (ecc
& ONENAND_ECC_2BIT_ALL
)
898 return ONENAND_BBT_READ_ERROR
;
900 printk(KERN_ERR
"onenand_bbt_wait: read timeout!"
901 "ctrl=0x%04x intr=0x%04x\n", ctrl
, interrupt
);
902 return ONENAND_BBT_READ_FATAL_ERROR
;
909 * onenand_bbt_read_oob - [MTD Interface] OneNAND read out-of-band for bbt scan
910 * @param mtd MTD device structure
911 * @param from offset to read from
912 * @param ops oob operation description structure
914 * OneNAND read out-of-band data from the spare area for bbt scan
916 int onenand_bbt_read_oob(struct mtd_info
*mtd
, loff_t from
,
917 struct mtd_oob_ops
*ops
)
919 struct onenand_chip
*this = mtd
->priv
;
920 int read
= 0, thislen
, column
;
922 size_t len
= ops
->ooblen
;
923 u_char
*buf
= ops
->oobbuf
;
925 MTDDEBUG(MTD_DEBUG_LEVEL3
,
926 "onenand_bbt_read_oob: from = 0x%08x, len = %zi\n",
927 (unsigned int) from
, len
);
929 /* Initialize return value */
932 /* Do not allow reads past end of device */
933 if (unlikely((from
+ len
) > mtd
->size
)) {
934 printk(KERN_ERR
"onenand_bbt_read_oob: Attempt read beyond end of device\n");
935 return ONENAND_BBT_READ_FATAL_ERROR
;
938 /* Grab the lock and see if the device is available */
939 onenand_get_device(mtd
, FL_READING
);
941 column
= from
& (mtd
->oobsize
- 1);
945 thislen
= mtd
->oobsize
- column
;
946 thislen
= min_t(int, thislen
, len
);
948 this->command(mtd
, ONENAND_CMD_READOOB
, from
, mtd
->oobsize
);
950 onenand_update_bufferram(mtd
, from
, 0);
952 ret
= onenand_bbt_wait(mtd
, FL_READING
);
956 this->read_bufferram(mtd
, ONENAND_SPARERAM
, buf
, column
, thislen
);
965 /* Update Page size */
966 from
+= this->writesize
;
971 /* Deselect and wake up anyone waiting on the device */
972 onenand_release_device(mtd
);
974 ops
->oobretlen
= read
;
979 #ifdef CONFIG_MTD_ONENAND_VERIFY_WRITE
981 * onenand_verify_oob - [GENERIC] verify the oob contents after a write
982 * @param mtd MTD device structure
983 * @param buf the databuffer to verify
984 * @param to offset to read from
986 static int onenand_verify_oob(struct mtd_info
*mtd
, const u_char
*buf
, loff_t to
)
988 struct onenand_chip
*this = mtd
->priv
;
989 u_char
*oob_buf
= this->oob_buf
;
992 this->command(mtd
, ONENAND_CMD_READOOB
, to
, mtd
->oobsize
);
993 onenand_update_bufferram(mtd
, to
, 0);
994 status
= this->wait(mtd
, FL_READING
);
998 this->read_bufferram(mtd
, ONENAND_SPARERAM
, oob_buf
, 0, mtd
->oobsize
);
999 for (i
= 0; i
< mtd
->oobsize
; i
++)
1000 if (buf
[i
] != 0xFF && buf
[i
] != oob_buf
[i
])
1007 * onenand_verify - [GENERIC] verify the chip contents after a write
1008 * @param mtd MTD device structure
1009 * @param buf the databuffer to verify
1010 * @param addr offset to read from
1011 * @param len number of bytes to read and compare
1013 static int onenand_verify(struct mtd_info
*mtd
, const u_char
*buf
, loff_t addr
, size_t len
)
1015 struct onenand_chip
*this = mtd
->priv
;
1016 void __iomem
*dataram
;
1018 int thislen
, column
;
1021 thislen
= min_t(int, this->writesize
, len
);
1022 column
= addr
& (this->writesize
- 1);
1023 if (column
+ thislen
> this->writesize
)
1024 thislen
= this->writesize
- column
;
1026 this->command(mtd
, ONENAND_CMD_READ
, addr
, this->writesize
);
1028 onenand_update_bufferram(mtd
, addr
, 0);
1030 ret
= this->wait(mtd
, FL_READING
);
1034 onenand_update_bufferram(mtd
, addr
, 1);
1036 dataram
= this->base
+ ONENAND_DATARAM
;
1037 dataram
+= onenand_bufferram_offset(mtd
, ONENAND_DATARAM
);
1039 if (memcmp(buf
, dataram
+ column
, thislen
))
1050 #define onenand_verify(...) (0)
1051 #define onenand_verify_oob(...) (0)
1054 #define NOTALIGNED(x) ((x & (mtd->writesize - 1)) != 0)
1057 * onenand_fill_auto_oob - [Internal] oob auto-placement transfer
1058 * @param mtd MTD device structure
1059 * @param oob_buf oob buffer
1060 * @param buf source address
1061 * @param column oob offset to write to
1062 * @param thislen oob length to write
1064 static int onenand_fill_auto_oob(struct mtd_info
*mtd
, u_char
*oob_buf
,
1065 const u_char
*buf
, int column
, int thislen
)
1067 struct onenand_chip
*this = mtd
->priv
;
1068 struct nand_oobfree
*free
;
1069 int writecol
= column
;
1070 int writeend
= column
+ thislen
;
1074 free
= this->ecclayout
->oobfree
;
1075 for (i
= 0; i
< MTD_MAX_OOBFREE_ENTRIES
&& free
->length
; i
++, free
++) {
1076 if (writecol
>= lastgap
)
1077 writecol
+= free
->offset
- lastgap
;
1078 if (writeend
>= lastgap
)
1079 writeend
+= free
->offset
- lastgap
;
1080 lastgap
= free
->offset
+ free
->length
;
1082 free
= this->ecclayout
->oobfree
;
1083 for (i
= 0; i
< MTD_MAX_OOBFREE_ENTRIES
&& free
->length
; i
++, free
++) {
1084 int free_end
= free
->offset
+ free
->length
;
1085 if (free
->offset
< writeend
&& free_end
> writecol
) {
1086 int st
= max_t(int,free
->offset
,writecol
);
1087 int ed
= min_t(int,free_end
,writeend
);
1089 memcpy(oob_buf
+ st
, buf
, n
);
1091 } else if (column
== 0)
1098 * onenand_write_ops_nolock - [OneNAND Interface] write main and/or out-of-band
1099 * @param mtd MTD device structure
1100 * @param to offset to write to
1101 * @param ops oob operation description structure
1103 * Write main and/or oob with ECC
1105 static int onenand_write_ops_nolock(struct mtd_info
*mtd
, loff_t to
,
1106 struct mtd_oob_ops
*ops
)
1108 struct onenand_chip
*this = mtd
->priv
;
1109 int written
= 0, column
, thislen
, subpage
;
1110 int oobwritten
= 0, oobcolumn
, thisooblen
, oobsize
;
1111 size_t len
= ops
->len
;
1112 size_t ooblen
= ops
->ooblen
;
1113 const u_char
*buf
= ops
->datbuf
;
1114 const u_char
*oob
= ops
->oobbuf
;
1118 MTDDEBUG(MTD_DEBUG_LEVEL3
,
1119 "onenand_write_ops_nolock: to = 0x%08x, len = %i\n",
1120 (unsigned int) to
, (int) len
);
1122 /* Initialize retlen, in case of early exit */
1126 /* Do not allow writes past end of device */
1127 if (unlikely((to
+ len
) > mtd
->size
)) {
1128 printk(KERN_ERR
"onenand_write_ops_nolock: Attempt write to past end of device\n");
1132 /* Reject writes, which are not page aligned */
1133 if (unlikely(NOTALIGNED(to
) || NOTALIGNED(len
))) {
1134 printk(KERN_ERR
"onenand_write_ops_nolock: Attempt to write not page aligned data\n");
1138 if (ops
->mode
== MTD_OOB_AUTO
)
1139 oobsize
= this->ecclayout
->oobavail
;
1141 oobsize
= mtd
->oobsize
;
1143 oobcolumn
= to
& (mtd
->oobsize
- 1);
1145 column
= to
& (mtd
->writesize
- 1);
1147 /* Loop until all data write */
1148 while (written
< len
) {
1149 u_char
*wbuf
= (u_char
*) buf
;
1151 thislen
= min_t(int, mtd
->writesize
- column
, len
- written
);
1152 thisooblen
= min_t(int, oobsize
- oobcolumn
, ooblen
- oobwritten
);
1154 this->command(mtd
, ONENAND_CMD_BUFFERRAM
, to
, thislen
);
1156 /* Partial page write */
1157 subpage
= thislen
< mtd
->writesize
;
1159 memset(this->page_buf
, 0xff, mtd
->writesize
);
1160 memcpy(this->page_buf
+ column
, buf
, thislen
);
1161 wbuf
= this->page_buf
;
1164 this->write_bufferram(mtd
, ONENAND_DATARAM
, wbuf
, 0, mtd
->writesize
);
1167 oobbuf
= this->oob_buf
;
1169 /* We send data to spare ram with oobsize
1170 * * to prevent byte access */
1171 memset(oobbuf
, 0xff, mtd
->oobsize
);
1172 if (ops
->mode
== MTD_OOB_AUTO
)
1173 onenand_fill_auto_oob(mtd
, oobbuf
, oob
, oobcolumn
, thisooblen
);
1175 memcpy(oobbuf
+ oobcolumn
, oob
, thisooblen
);
1177 oobwritten
+= thisooblen
;
1181 oobbuf
= (u_char
*) ffchars
;
1183 this->write_bufferram(mtd
, ONENAND_SPARERAM
, oobbuf
, 0, mtd
->oobsize
);
1185 this->command(mtd
, ONENAND_CMD_PROG
, to
, mtd
->writesize
);
1187 ret
= this->wait(mtd
, FL_WRITING
);
1189 /* In partial page write we don't update bufferram */
1190 onenand_update_bufferram(mtd
, to
, !ret
&& !subpage
);
1191 if (ONENAND_IS_2PLANE(this)) {
1192 ONENAND_SET_BUFFERRAM1(this);
1193 onenand_update_bufferram(mtd
, to
+ this->writesize
, !ret
&& !subpage
);
1197 printk(KERN_ERR
"onenand_write_ops_nolock: write filaed %d\n", ret
);
1201 /* Only check verify write turn on */
1202 ret
= onenand_verify(mtd
, buf
, to
, thislen
);
1204 printk(KERN_ERR
"onenand_write_ops_nolock: verify failed %d\n", ret
);
1218 ops
->retlen
= written
;
1224 * onenand_write_oob_nolock - [Internal] OneNAND write out-of-band
1225 * @param mtd MTD device structure
1226 * @param to offset to write to
1227 * @param len number of bytes to write
1228 * @param retlen pointer to variable to store the number of written bytes
1229 * @param buf the data to write
1230 * @param mode operation mode
1232 * OneNAND write out-of-band
1234 static int onenand_write_oob_nolock(struct mtd_info
*mtd
, loff_t to
,
1235 struct mtd_oob_ops
*ops
)
1237 struct onenand_chip
*this = mtd
->priv
;
1238 int column
, ret
= 0, oobsize
;
1241 size_t len
= ops
->ooblen
;
1242 const u_char
*buf
= ops
->oobbuf
;
1243 mtd_oob_mode_t mode
= ops
->mode
;
1247 MTDDEBUG(MTD_DEBUG_LEVEL3
,
1248 "onenand_write_oob_nolock: to = 0x%08x, len = %i\n",
1249 (unsigned int) to
, (int) len
);
1251 /* Initialize retlen, in case of early exit */
1254 if (mode
== MTD_OOB_AUTO
)
1255 oobsize
= this->ecclayout
->oobavail
;
1257 oobsize
= mtd
->oobsize
;
1259 column
= to
& (mtd
->oobsize
- 1);
1261 if (unlikely(column
>= oobsize
)) {
1262 printk(KERN_ERR
"onenand_write_oob_nolock: Attempted to start write outside oob\n");
1266 /* For compatibility with NAND: Do not allow write past end of page */
1267 if (unlikely(column
+ len
> oobsize
)) {
1268 printk(KERN_ERR
"onenand_write_oob_nolock: "
1269 "Attempt to write past end of page\n");
1273 /* Do not allow reads past end of device */
1274 if (unlikely(to
>= mtd
->size
||
1275 column
+ len
> ((mtd
->size
>> this->page_shift
) -
1276 (to
>> this->page_shift
)) * oobsize
)) {
1277 printk(KERN_ERR
"onenand_write_oob_nolock: Attempted to write past end of device\n");
1281 oobbuf
= this->oob_buf
;
1283 /* Loop until all data write */
1284 while (written
< len
) {
1285 int thislen
= min_t(int, oobsize
, len
- written
);
1287 this->command(mtd
, ONENAND_CMD_BUFFERRAM
, to
, mtd
->oobsize
);
1289 /* We send data to spare ram with oobsize
1290 * to prevent byte access */
1291 memset(oobbuf
, 0xff, mtd
->oobsize
);
1292 if (mode
== MTD_OOB_AUTO
)
1293 onenand_fill_auto_oob(mtd
, oobbuf
, buf
, column
, thislen
);
1295 memcpy(oobbuf
+ column
, buf
, thislen
);
1296 this->write_bufferram(mtd
, ONENAND_SPARERAM
, oobbuf
, 0, mtd
->oobsize
);
1298 this->command(mtd
, ONENAND_CMD_PROGOOB
, to
, mtd
->oobsize
);
1300 onenand_update_bufferram(mtd
, to
, 0);
1301 if (ONENAND_IS_2PLANE(this)) {
1302 ONENAND_SET_BUFFERRAM1(this);
1303 onenand_update_bufferram(mtd
, to
+ this->writesize
, 0);
1306 ret
= this->wait(mtd
, FL_WRITING
);
1308 printk(KERN_ERR
"onenand_write_oob_nolock: write failed %d\n", ret
);
1312 ret
= onenand_verify_oob(mtd
, oobbuf
, to
);
1314 printk(KERN_ERR
"onenand_write_oob_nolock: verify failed %d\n", ret
);
1322 to
+= mtd
->writesize
;
1327 ops
->oobretlen
= written
;
1333 * onenand_write - [MTD Interface] compability function for onenand_write_ecc
1334 * @param mtd MTD device structure
1335 * @param to offset to write to
1336 * @param len number of bytes to write
1337 * @param retlen pointer to variable to store the number of written bytes
1338 * @param buf the data to write
1342 int onenand_write(struct mtd_info
*mtd
, loff_t to
, size_t len
,
1343 size_t * retlen
, const u_char
* buf
)
1345 struct mtd_oob_ops ops
= {
1348 .datbuf
= (u_char
*) buf
,
1353 onenand_get_device(mtd
, FL_WRITING
);
1354 ret
= onenand_write_ops_nolock(mtd
, to
, &ops
);
1355 onenand_release_device(mtd
);
1357 *retlen
= ops
.retlen
;
1362 * onenand_write_oob - [MTD Interface] OneNAND write out-of-band
1363 * @param mtd MTD device structure
1364 * @param to offset to write to
1365 * @param ops oob operation description structure
1367 * OneNAND write main and/or out-of-band
1369 int onenand_write_oob(struct mtd_info
*mtd
, loff_t to
,
1370 struct mtd_oob_ops
*ops
)
1374 switch (ops
->mode
) {
1379 /* Not implemented yet */
1384 onenand_get_device(mtd
, FL_WRITING
);
1386 ret
= onenand_write_ops_nolock(mtd
, to
, ops
);
1388 ret
= onenand_write_oob_nolock(mtd
, to
, ops
);
1389 onenand_release_device(mtd
);
1396 * onenand_block_isbad_nolock - [GENERIC] Check if a block is marked bad
1397 * @param mtd MTD device structure
1398 * @param ofs offset from device start
1399 * @param allowbbt 1, if its allowed to access the bbt area
1401 * Check, if the block is bad, Either by reading the bad block table or
1402 * calling of the scan function.
1404 static int onenand_block_isbad_nolock(struct mtd_info
*mtd
, loff_t ofs
, int allowbbt
)
1406 struct onenand_chip
*this = mtd
->priv
;
1407 struct bbm_info
*bbm
= this->bbm
;
1409 /* Return info from the table */
1410 return bbm
->isbad_bbt(mtd
, ofs
, allowbbt
);
1415 * onenand_erase - [MTD Interface] erase block(s)
1416 * @param mtd MTD device structure
1417 * @param instr erase instruction
1419 * Erase one ore more blocks
1421 int onenand_erase(struct mtd_info
*mtd
, struct erase_info
*instr
)
1423 struct onenand_chip
*this = mtd
->priv
;
1424 unsigned int block_size
;
1429 MTDDEBUG (MTD_DEBUG_LEVEL3
,
1430 "onenand_erase: start = 0x%08x, len = %i\n",
1431 (unsigned int)instr
->addr
, (unsigned int)instr
->len
);
1433 block_size
= (1 << this->erase_shift
);
1435 /* Start address must align on block boundary */
1436 if (unlikely(instr
->addr
& (block_size
- 1))) {
1437 MTDDEBUG (MTD_DEBUG_LEVEL0
,
1438 "onenand_erase: Unaligned address\n");
1442 /* Length must align on block boundary */
1443 if (unlikely(instr
->len
& (block_size
- 1))) {
1444 MTDDEBUG (MTD_DEBUG_LEVEL0
,
1445 "onenand_erase: Length not block aligned\n");
1449 /* Do not allow erase past end of device */
1450 if (unlikely((instr
->len
+ instr
->addr
) > mtd
->size
)) {
1451 MTDDEBUG (MTD_DEBUG_LEVEL0
,
1452 "onenand_erase: Erase past end of device\n");
1456 instr
->fail_addr
= 0xffffffff;
1458 /* Grab the lock and see if the device is available */
1459 onenand_get_device(mtd
, FL_ERASING
);
1461 /* Loop throught the pages */
1465 instr
->state
= MTD_ERASING
;
1469 /* TODO Check badblock */
1471 this->command(mtd
, ONENAND_CMD_ERASE
, addr
, block_size
);
1473 onenand_invalidate_bufferram(mtd
, addr
, block_size
);
1475 ret
= this->wait(mtd
, FL_ERASING
);
1476 /* Check, if it is write protected */
1479 MTDDEBUG (MTD_DEBUG_LEVEL0
, "onenand_erase: "
1480 "Device is write protected!!!\n");
1482 MTDDEBUG (MTD_DEBUG_LEVEL0
, "onenand_erase: "
1483 "Failed erase, block %d\n",
1484 (unsigned)(addr
>> this->erase_shift
));
1485 instr
->state
= MTD_ERASE_FAILED
;
1486 instr
->fail_addr
= addr
;
1494 instr
->state
= MTD_ERASE_DONE
;
1498 ret
= instr
->state
== MTD_ERASE_DONE
? 0 : -EIO
;
1499 /* Do call back function */
1501 mtd_erase_callback(instr
);
1503 /* Deselect and wake up anyone waiting on the device */
1504 onenand_release_device(mtd
);
1510 * onenand_sync - [MTD Interface] sync
1511 * @param mtd MTD device structure
1513 * Sync is actually a wait for chip ready function
1515 void onenand_sync(struct mtd_info
*mtd
)
1517 MTDDEBUG (MTD_DEBUG_LEVEL3
, "onenand_sync: called\n");
1519 /* Grab the lock and see if the device is available */
1520 onenand_get_device(mtd
, FL_SYNCING
);
1522 /* Release it and go back */
1523 onenand_release_device(mtd
);
1527 * onenand_block_isbad - [MTD Interface] Check whether the block at the given offset is bad
1528 * @param mtd MTD device structure
1529 * @param ofs offset relative to mtd start
1531 * Check whether the block is bad
1533 int onenand_block_isbad(struct mtd_info
*mtd
, loff_t ofs
)
1537 /* Check for invalid offset */
1538 if (ofs
> mtd
->size
)
1541 onenand_get_device(mtd
, FL_READING
);
1542 ret
= onenand_block_isbad_nolock(mtd
,ofs
, 0);
1543 onenand_release_device(mtd
);
1548 * onenand_block_markbad - [MTD Interface] Mark the block at the given offset as bad
1549 * @param mtd MTD device structure
1550 * @param ofs offset relative to mtd start
1552 * Mark the block as bad
1554 int onenand_block_markbad(struct mtd_info
*mtd
, loff_t ofs
)
1556 struct onenand_chip
*this = mtd
->priv
;
1559 ret
= onenand_block_isbad(mtd
, ofs
);
1561 /* If it was bad already, return success and do nothing */
1567 ret
= this->block_markbad(mtd
, ofs
);
1572 * onenand_unlock - [MTD Interface] Unlock block(s)
1573 * @param mtd MTD device structure
1574 * @param ofs offset relative to mtd start
1575 * @param len number of bytes to unlock
1577 * Unlock one or more blocks
1579 int onenand_unlock(struct mtd_info
*mtd
, loff_t ofs
, size_t len
)
1581 struct onenand_chip
*this = mtd
->priv
;
1582 int start
, end
, block
, value
, status
;
1584 start
= ofs
>> this->erase_shift
;
1585 end
= len
>> this->erase_shift
;
1587 /* Continuous lock scheme */
1588 if (this->options
& ONENAND_CONT_LOCK
) {
1589 /* Set start block address */
1590 this->write_word(start
,
1591 this->base
+ ONENAND_REG_START_BLOCK_ADDRESS
);
1592 /* Set end block address */
1593 this->write_word(end
- 1,
1594 this->base
+ ONENAND_REG_END_BLOCK_ADDRESS
);
1595 /* Write unlock command */
1596 this->command(mtd
, ONENAND_CMD_UNLOCK
, 0, 0);
1598 /* There's no return value */
1599 this->wait(mtd
, FL_UNLOCKING
);
1602 while (this->read_word(this->base
+ ONENAND_REG_CTRL_STATUS
)
1603 & ONENAND_CTRL_ONGO
)
1606 /* Check lock status */
1607 status
= this->read_word(this->base
+ ONENAND_REG_WP_STATUS
);
1608 if (!(status
& ONENAND_WP_US
))
1609 printk(KERN_ERR
"wp status = 0x%x\n", status
);
1614 /* Block lock scheme */
1615 for (block
= start
; block
< end
; block
++) {
1616 /* Set start block address */
1617 this->write_word(block
,
1618 this->base
+ ONENAND_REG_START_BLOCK_ADDRESS
);
1619 /* Write unlock command */
1620 this->command(mtd
, ONENAND_CMD_UNLOCK
, 0, 0);
1622 /* There's no return value */
1623 this->wait(mtd
, FL_UNLOCKING
);
1626 while (this->read_word(this->base
+ ONENAND_REG_CTRL_STATUS
)
1627 & ONENAND_CTRL_ONGO
)
1630 /* Set block address for read block status */
1631 value
= onenand_block_address(this->device_id
, block
);
1632 this->write_word(value
,
1633 this->base
+ ONENAND_REG_START_ADDRESS1
);
1635 /* Check lock status */
1636 status
= this->read_word(this->base
+ ONENAND_REG_WP_STATUS
);
1637 if (!(status
& ONENAND_WP_US
))
1638 printk(KERN_ERR
"block = %d, wp status = 0x%x\n",
1646 * onenand_print_device_info - Print device ID
1647 * @param device device ID
1651 char * onenand_print_device_info(int device
)
1653 int vcc
, demuxed
, ddp
, density
;
1654 char *dev_info
= malloc(80);
1656 vcc
= device
& ONENAND_DEVICE_VCC_MASK
;
1657 demuxed
= device
& ONENAND_DEVICE_IS_DEMUX
;
1658 ddp
= device
& ONENAND_DEVICE_IS_DDP
;
1659 density
= device
>> ONENAND_DEVICE_DENSITY_SHIFT
;
1660 sprintf(dev_info
, "%sOneNAND%s %dMB %sV 16-bit (0x%02x)",
1661 demuxed
? "" : "Muxed ",
1663 (16 << density
), vcc
? "2.65/3.3" : "1.8", device
);
1668 static const struct onenand_manufacturers onenand_manuf_ids
[] = {
1669 {ONENAND_MFR_SAMSUNG
, "Samsung"},
1670 {ONENAND_MFR_UNKNOWN
, "Unknown"}
1674 * onenand_check_maf - Check manufacturer ID
1675 * @param manuf manufacturer ID
1677 * Check manufacturer ID
1679 static int onenand_check_maf(int manuf
)
1683 for (i
= 0; onenand_manuf_ids
[i
].id
; i
++) {
1684 if (manuf
== onenand_manuf_ids
[i
].id
)
1688 #ifdef ONENAND_DEBUG
1689 printk(KERN_DEBUG
"OneNAND Manufacturer: %s (0x%0x)\n",
1690 onenand_manuf_ids
[i
].name
, manuf
);
1693 return (i
!= ONENAND_MFR_UNKNOWN
);
1697 * onenand_probe - [OneNAND Interface] Probe the OneNAND device
1698 * @param mtd MTD device structure
1700 * OneNAND detection method:
1701 * Compare the the values from command with ones from register
1703 static int onenand_probe(struct mtd_info
*mtd
)
1705 struct onenand_chip
*this = mtd
->priv
;
1706 int bram_maf_id
, bram_dev_id
, maf_id
, dev_id
;
1710 /* Send the command for reading device ID from BootRAM */
1711 this->write_word(ONENAND_CMD_READID
, this->base
+ ONENAND_BOOTRAM
);
1713 /* Read manufacturer and device IDs from BootRAM */
1714 bram_maf_id
= this->read_word(this->base
+ ONENAND_BOOTRAM
+ 0x0);
1715 bram_dev_id
= this->read_word(this->base
+ ONENAND_BOOTRAM
+ 0x2);
1717 /* Check manufacturer ID */
1718 if (onenand_check_maf(bram_maf_id
))
1721 /* Reset OneNAND to read default register values */
1722 this->write_word(ONENAND_CMD_RESET
, this->base
+ ONENAND_BOOTRAM
);
1725 this->wait(mtd
, FL_RESETING
);
1727 /* Read manufacturer and device IDs from Register */
1728 maf_id
= this->read_word(this->base
+ ONENAND_REG_MANUFACTURER_ID
);
1729 dev_id
= this->read_word(this->base
+ ONENAND_REG_DEVICE_ID
);
1731 /* Check OneNAND device */
1732 if (maf_id
!= bram_maf_id
|| dev_id
!= bram_dev_id
)
1735 /* FIXME : Current OneNAND MTD doesn't support Flex-OneNAND */
1736 if (dev_id
& (1 << 9)) {
1737 printk("Not yet support Flex-OneNAND\n");
1741 /* Flash device information */
1742 mtd
->name
= onenand_print_device_info(dev_id
);
1743 this->device_id
= dev_id
;
1745 density
= dev_id
>> ONENAND_DEVICE_DENSITY_SHIFT
;
1746 this->chipsize
= (16 << density
) << 20;
1748 /* OneNAND page size & block size */
1749 /* The data buffer size is equal to page size */
1751 this->read_word(this->base
+ ONENAND_REG_DATA_BUFFER_SIZE
);
1752 mtd
->oobsize
= mtd
->writesize
>> 5;
1753 /* Pagers per block is always 64 in OneNAND */
1754 mtd
->erasesize
= mtd
->writesize
<< 6;
1756 this->erase_shift
= ffs(mtd
->erasesize
) - 1;
1757 this->page_shift
= ffs(mtd
->writesize
) - 1;
1758 this->ppb_shift
= (this->erase_shift
- this->page_shift
);
1759 this->page_mask
= (mtd
->erasesize
/ mtd
->writesize
) - 1;
1760 /* It's real page size */
1761 this->writesize
= mtd
->writesize
;
1763 /* REVIST: Multichip handling */
1765 mtd
->size
= this->chipsize
;
1768 version_id
= this->read_word(this->base
+ ONENAND_REG_VERSION_ID
);
1769 #ifdef ONENAND_DEBUG
1770 printk(KERN_DEBUG
"OneNAND version = 0x%04x\n", version_id
);
1774 if (density
<= ONENAND_DEVICE_DENSITY_512Mb
&&
1775 !(version_id
>> ONENAND_VERSION_PROCESS_SHIFT
)) {
1776 printk(KERN_INFO
"Lock scheme is Continues Lock\n");
1777 this->options
|= ONENAND_CONT_LOCK
;
1780 mtd
->flags
= MTD_CAP_NANDFLASH
;
1781 mtd
->erase
= onenand_erase
;
1782 mtd
->read
= onenand_read
;
1783 mtd
->write
= onenand_write
;
1784 mtd
->read_oob
= onenand_read_oob
;
1785 mtd
->write_oob
= onenand_write_oob
;
1786 mtd
->sync
= onenand_sync
;
1787 mtd
->block_isbad
= onenand_block_isbad
;
1788 mtd
->block_markbad
= onenand_block_markbad
;
1794 * onenand_scan - [OneNAND Interface] Scan for the OneNAND device
1795 * @param mtd MTD device structure
1796 * @param maxchips Number of chips to scan for
1798 * This fills out all the not initialized function pointers
1799 * with the defaults.
1800 * The flash ID is read and the mtd/chip structures are
1801 * filled with the appropriate values.
1803 int onenand_scan(struct mtd_info
*mtd
, int maxchips
)
1805 struct onenand_chip
*this = mtd
->priv
;
1807 if (!this->read_word
)
1808 this->read_word
= onenand_readw
;
1809 if (!this->write_word
)
1810 this->write_word
= onenand_writew
;
1813 this->command
= onenand_command
;
1815 this->wait
= onenand_wait
;
1817 if (!this->read_bufferram
)
1818 this->read_bufferram
= onenand_read_bufferram
;
1819 if (!this->write_bufferram
)
1820 this->write_bufferram
= onenand_write_bufferram
;
1822 if (onenand_probe(mtd
))
1825 /* Set Sync. Burst Read after probing */
1826 if (this->mmcontrol
) {
1827 printk(KERN_INFO
"OneNAND Sync. Burst Read support\n");
1828 this->read_bufferram
= onenand_sync_read_bufferram
;
1831 /* Allocate buffers, if necessary */
1832 if (!this->page_buf
) {
1833 this->page_buf
= kzalloc(mtd
->writesize
, GFP_KERNEL
);
1834 if (!this->page_buf
) {
1835 printk(KERN_ERR
"onenand_scan(): Can't allocate page_buf\n");
1838 this->options
|= ONENAND_PAGEBUF_ALLOC
;
1840 if (!this->oob_buf
) {
1841 this->oob_buf
= kzalloc(mtd
->oobsize
, GFP_KERNEL
);
1842 if (!this->oob_buf
) {
1843 printk(KERN_ERR
"onenand_scan: Can't allocate oob_buf\n");
1844 if (this->options
& ONENAND_PAGEBUF_ALLOC
) {
1845 this->options
&= ~ONENAND_PAGEBUF_ALLOC
;
1846 kfree(this->page_buf
);
1850 this->options
|= ONENAND_OOBBUF_ALLOC
;
1853 onenand_unlock(mtd
, 0, mtd
->size
);
1855 return onenand_default_bbt(mtd
);
1859 * onenand_release - [OneNAND Interface] Free resources held by the OneNAND device
1860 * @param mtd MTD device structure
1862 void onenand_release(struct mtd_info
*mtd
)