2 * linux/drivers/mtd/onenand/onenand_base.c
4 * Copyright (C) 2005 Samsung Electronics
5 * Kyungmin Park <kyungmin.park@samsung.com>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
12 #include <linux/kernel.h>
13 #include <linux/module.h>
14 #include <linux/init.h>
15 #include <linux/sched.h>
16 #include <linux/jiffies.h>
17 #include <linux/mtd/mtd.h>
18 #include <linux/mtd/onenand.h>
19 #include <linux/mtd/partitions.h>
24 * onenand_oob_64 - oob info for large (2KB) page
26 static struct nand_oobinfo onenand_oob_64
= {
27 .useecc
= MTD_NANDECC_AUTOPLACE
,
36 {2, 3}, {14, 2}, {18, 3}, {30, 2},
37 {34, 3}, {46, 2}, {50, 3}, {62, 2}
42 * onenand_oob_32 - oob info for middle (1KB) page
44 static struct nand_oobinfo onenand_oob_32
= {
45 .useecc
= MTD_NANDECC_AUTOPLACE
,
51 .oobfree
= { {2, 3}, {14, 2}, {18, 3}, {30, 2} }
54 static const unsigned char ffchars
[] = {
55 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
56 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 16 */
57 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
58 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 32 */
59 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
60 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 48 */
61 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
62 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 64 */
66 * onenand_readw - [OneNAND Interface] Read OneNAND register
67 * @param addr address to read
69 * Read OneNAND register
71 static unsigned short onenand_readw(void __iomem
*addr
)
77 * onenand_writew - [OneNAND Interface] Write OneNAND register with value
78 * @param value value to write
79 * @param addr address to write
81 * Write OneNAND register with value
83 static void onenand_writew(unsigned short value
, void __iomem
*addr
)
89 * onenand_block_address - [DEFAULT] Get block address
90 * @param this onenand chip data structure
91 * @param block the block
92 * @return translated block address if DDP, otherwise same
94 * Setup Start Address 1 Register (F100h)
96 static int onenand_block_address(struct onenand_chip
*this, int block
)
98 if (this->device_id
& ONENAND_DEVICE_IS_DDP
) {
99 /* Device Flash Core select, NAND Flash Block Address */
102 if (block
& this->density_mask
)
105 return (dfs
<< ONENAND_DDP_SHIFT
) |
106 (block
& (this->density_mask
- 1));
113 * onenand_bufferram_address - [DEFAULT] Get bufferram address
114 * @param this onenand chip data structure
115 * @param block the block
116 * @return set DBS value if DDP, otherwise 0
118 * Setup Start Address 2 Register (F101h) for DDP
120 static int onenand_bufferram_address(struct onenand_chip
*this, int block
)
122 if (this->device_id
& ONENAND_DEVICE_IS_DDP
) {
123 /* Device BufferRAM Select */
126 if (block
& this->density_mask
)
129 return (dbs
<< ONENAND_DDP_SHIFT
);
136 * onenand_page_address - [DEFAULT] Get page address
137 * @param page the page address
138 * @param sector the sector address
139 * @return combined page and sector address
141 * Setup Start Address 8 Register (F107h)
143 static int onenand_page_address(int page
, int sector
)
145 /* Flash Page Address, Flash Sector Address */
148 fpa
= page
& ONENAND_FPA_MASK
;
149 fsa
= sector
& ONENAND_FSA_MASK
;
151 return ((fpa
<< ONENAND_FPA_SHIFT
) | fsa
);
155 * onenand_buffer_address - [DEFAULT] Get buffer address
156 * @param dataram1 DataRAM index
157 * @param sectors the sector address
158 * @param count the number of sectors
159 * @return the start buffer value
161 * Setup Start Buffer Register (F200h)
163 static int onenand_buffer_address(int dataram1
, int sectors
, int count
)
167 /* BufferRAM Sector Address */
168 bsa
= sectors
& ONENAND_BSA_MASK
;
171 bsa
|= ONENAND_BSA_DATARAM1
; /* DataRAM1 */
173 bsa
|= ONENAND_BSA_DATARAM0
; /* DataRAM0 */
175 /* BufferRAM Sector Count */
176 bsc
= count
& ONENAND_BSC_MASK
;
178 return ((bsa
<< ONENAND_BSA_SHIFT
) | bsc
);
182 * onenand_command - [DEFAULT] Send command to OneNAND device
183 * @param mtd MTD device structure
184 * @param cmd the command to be sent
185 * @param addr offset to read from or write to
186 * @param len number of bytes to read or write
188 * Send command to OneNAND device. This function is used for middle/large page
189 * devices (1KB/2KB Bytes per page)
191 static int onenand_command(struct mtd_info
*mtd
, int cmd
, loff_t addr
, size_t len
)
193 struct onenand_chip
*this = mtd
->priv
;
194 int value
, readcmd
= 0, block_cmd
= 0;
196 /* Now we use page size operation */
197 int sectors
= 4, count
= 4;
199 /* Address translation */
201 case ONENAND_CMD_UNLOCK
:
202 case ONENAND_CMD_LOCK
:
203 case ONENAND_CMD_LOCK_TIGHT
:
208 case ONENAND_CMD_ERASE
:
209 case ONENAND_CMD_BUFFERRAM
:
210 case ONENAND_CMD_OTP_ACCESS
:
212 block
= (int) (addr
>> this->erase_shift
);
217 block
= (int) (addr
>> this->erase_shift
);
218 page
= (int) (addr
>> this->page_shift
);
219 page
&= this->page_mask
;
223 /* NOTE: The setting order of the registers is very important! */
224 if (cmd
== ONENAND_CMD_BUFFERRAM
) {
225 /* Select DataRAM for DDP */
226 value
= onenand_bufferram_address(this, block
);
227 this->write_word(value
, this->base
+ ONENAND_REG_START_ADDRESS2
);
229 /* Switch to the next data buffer */
230 ONENAND_SET_NEXT_BUFFERRAM(this);
236 /* Write 'DFS, FBA' of Flash */
237 value
= onenand_block_address(this, block
);
238 this->write_word(value
, this->base
+ ONENAND_REG_START_ADDRESS1
);
240 if (cmd
== block_cmd
) {
241 /* Select DataRAM for DDP */
242 value
= onenand_bufferram_address(this, block
);
243 this->write_word(value
, this->base
+ ONENAND_REG_START_ADDRESS2
);
251 case ONENAND_CMD_READ
:
252 case ONENAND_CMD_READOOB
:
253 dataram
= ONENAND_SET_NEXT_BUFFERRAM(this);
258 dataram
= ONENAND_CURRENT_BUFFERRAM(this);
262 /* Write 'FPA, FSA' of Flash */
263 value
= onenand_page_address(page
, sectors
);
264 this->write_word(value
, this->base
+ ONENAND_REG_START_ADDRESS8
);
266 /* Write 'BSA, BSC' of DataRAM */
267 value
= onenand_buffer_address(dataram
, sectors
, count
);
268 this->write_word(value
, this->base
+ ONENAND_REG_START_BUFFER
);
271 /* Select DataRAM for DDP */
272 value
= onenand_bufferram_address(this, block
);
273 this->write_word(value
, this->base
+ ONENAND_REG_START_ADDRESS2
);
277 /* Interrupt clear */
278 this->write_word(ONENAND_INT_CLEAR
, this->base
+ ONENAND_REG_INTERRUPT
);
281 this->write_word(cmd
, this->base
+ ONENAND_REG_COMMAND
);
287 * onenand_wait - [DEFAULT] wait until the command is done
288 * @param mtd MTD device structure
289 * @param state state to select the max. timeout value
291 * Wait for command done. This applies to all OneNAND command
292 * Read can take up to 30us, erase up to 2ms and program up to 350us
293 * according to general OneNAND specs
295 static int onenand_wait(struct mtd_info
*mtd
, int state
)
297 struct onenand_chip
* this = mtd
->priv
;
298 unsigned long timeout
;
299 unsigned int flags
= ONENAND_INT_MASTER
;
300 unsigned int interrupt
= 0;
301 unsigned int ctrl
, ecc
;
303 /* The 20 msec is enough */
304 timeout
= jiffies
+ msecs_to_jiffies(20);
305 while (time_before(jiffies
, timeout
)) {
306 interrupt
= this->read_word(this->base
+ ONENAND_REG_INTERRUPT
);
308 if (interrupt
& flags
)
311 if (state
!= FL_READING
)
313 touch_softlockup_watchdog();
315 /* To get correct interrupt status in timeout case */
316 interrupt
= this->read_word(this->base
+ ONENAND_REG_INTERRUPT
);
318 ctrl
= this->read_word(this->base
+ ONENAND_REG_CTRL_STATUS
);
320 if (ctrl
& ONENAND_CTRL_ERROR
) {
321 /* It maybe occur at initial bad block */
322 DEBUG(MTD_DEBUG_LEVEL0
, "onenand_wait: controller error = 0x%04x\n", ctrl
);
323 /* Clear other interrupt bits for preventing ECC error */
324 interrupt
&= ONENAND_INT_MASTER
;
327 if (ctrl
& ONENAND_CTRL_LOCK
) {
328 DEBUG(MTD_DEBUG_LEVEL0
, "onenand_wait: it's locked error = 0x%04x\n", ctrl
);
332 if (interrupt
& ONENAND_INT_READ
) {
333 ecc
= this->read_word(this->base
+ ONENAND_REG_ECC_STATUS
);
334 if (ecc
& ONENAND_ECC_2BIT_ALL
) {
335 DEBUG(MTD_DEBUG_LEVEL0
, "onenand_wait: ECC error = 0x%04x\n", ecc
);
344 * onenand_bufferram_offset - [DEFAULT] BufferRAM offset
345 * @param mtd MTD data structure
346 * @param area BufferRAM area
347 * @return offset given area
349 * Return BufferRAM offset given area
351 static inline int onenand_bufferram_offset(struct mtd_info
*mtd
, int area
)
353 struct onenand_chip
*this = mtd
->priv
;
355 if (ONENAND_CURRENT_BUFFERRAM(this)) {
356 if (area
== ONENAND_DATARAM
)
357 return mtd
->oobblock
;
358 if (area
== ONENAND_SPARERAM
)
366 * onenand_read_bufferram - [OneNAND Interface] Read the bufferram area
367 * @param mtd MTD data structure
368 * @param area BufferRAM area
369 * @param buffer the databuffer to put/get data
370 * @param offset offset to read from or write to
371 * @param count number of bytes to read/write
373 * Read the BufferRAM area
375 static int onenand_read_bufferram(struct mtd_info
*mtd
, int area
,
376 unsigned char *buffer
, int offset
, size_t count
)
378 struct onenand_chip
*this = mtd
->priv
;
379 void __iomem
*bufferram
;
381 bufferram
= this->base
+ area
;
383 bufferram
+= onenand_bufferram_offset(mtd
, area
);
385 if (ONENAND_CHECK_BYTE_ACCESS(count
)) {
388 /* Align with word(16-bit) size */
391 /* Read word and save byte */
392 word
= this->read_word(bufferram
+ offset
+ count
);
393 buffer
[count
] = (word
& 0xff);
396 memcpy(buffer
, bufferram
+ offset
, count
);
402 * onenand_sync_read_bufferram - [OneNAND Interface] Read the bufferram area with Sync. Burst mode
403 * @param mtd MTD data structure
404 * @param area BufferRAM area
405 * @param buffer the databuffer to put/get data
406 * @param offset offset to read from or write to
407 * @param count number of bytes to read/write
409 * Read the BufferRAM area with Sync. Burst Mode
411 static int onenand_sync_read_bufferram(struct mtd_info
*mtd
, int area
,
412 unsigned char *buffer
, int offset
, size_t count
)
414 struct onenand_chip
*this = mtd
->priv
;
415 void __iomem
*bufferram
;
417 bufferram
= this->base
+ area
;
419 bufferram
+= onenand_bufferram_offset(mtd
, area
);
421 this->mmcontrol(mtd
, ONENAND_SYS_CFG1_SYNC_READ
);
423 if (ONENAND_CHECK_BYTE_ACCESS(count
)) {
426 /* Align with word(16-bit) size */
429 /* Read word and save byte */
430 word
= this->read_word(bufferram
+ offset
+ count
);
431 buffer
[count
] = (word
& 0xff);
434 memcpy(buffer
, bufferram
+ offset
, count
);
436 this->mmcontrol(mtd
, 0);
442 * onenand_write_bufferram - [OneNAND Interface] Write the bufferram area
443 * @param mtd MTD data structure
444 * @param area BufferRAM area
445 * @param buffer the databuffer to put/get data
446 * @param offset offset to read from or write to
447 * @param count number of bytes to read/write
449 * Write the BufferRAM area
451 static int onenand_write_bufferram(struct mtd_info
*mtd
, int area
,
452 const unsigned char *buffer
, int offset
, size_t count
)
454 struct onenand_chip
*this = mtd
->priv
;
455 void __iomem
*bufferram
;
457 bufferram
= this->base
+ area
;
459 bufferram
+= onenand_bufferram_offset(mtd
, area
);
461 if (ONENAND_CHECK_BYTE_ACCESS(count
)) {
465 /* Align with word(16-bit) size */
468 /* Calculate byte access offset */
469 byte_offset
= offset
+ count
;
471 /* Read word and save byte */
472 word
= this->read_word(bufferram
+ byte_offset
);
473 word
= (word
& ~0xff) | buffer
[count
];
474 this->write_word(word
, bufferram
+ byte_offset
);
477 memcpy(bufferram
+ offset
, buffer
, count
);
483 * onenand_check_bufferram - [GENERIC] Check BufferRAM information
484 * @param mtd MTD data structure
485 * @param addr address to check
486 * @return 1 if there are valid data, otherwise 0
488 * Check bufferram if there is data we required
490 static int onenand_check_bufferram(struct mtd_info
*mtd
, loff_t addr
)
492 struct onenand_chip
*this = mtd
->priv
;
496 block
= (int) (addr
>> this->erase_shift
);
497 page
= (int) (addr
>> this->page_shift
);
498 page
&= this->page_mask
;
500 i
= ONENAND_CURRENT_BUFFERRAM(this);
502 /* Is there valid data? */
503 if (this->bufferram
[i
].block
== block
&&
504 this->bufferram
[i
].page
== page
&&
505 this->bufferram
[i
].valid
)
512 * onenand_update_bufferram - [GENERIC] Update BufferRAM information
513 * @param mtd MTD data structure
514 * @param addr address to update
515 * @param valid valid flag
517 * Update BufferRAM information
519 static int onenand_update_bufferram(struct mtd_info
*mtd
, loff_t addr
,
522 struct onenand_chip
*this = mtd
->priv
;
526 block
= (int) (addr
>> this->erase_shift
);
527 page
= (int) (addr
>> this->page_shift
);
528 page
&= this->page_mask
;
530 /* Invalidate BufferRAM */
531 for (i
= 0; i
< MAX_BUFFERRAM
; i
++) {
532 if (this->bufferram
[i
].block
== block
&&
533 this->bufferram
[i
].page
== page
)
534 this->bufferram
[i
].valid
= 0;
537 /* Update BufferRAM */
538 i
= ONENAND_CURRENT_BUFFERRAM(this);
539 this->bufferram
[i
].block
= block
;
540 this->bufferram
[i
].page
= page
;
541 this->bufferram
[i
].valid
= valid
;
547 * onenand_get_device - [GENERIC] Get chip for selected access
548 * @param mtd MTD device structure
549 * @param new_state the state which is requested
551 * Get the device and lock it for exclusive access
553 static int onenand_get_device(struct mtd_info
*mtd
, int new_state
)
555 struct onenand_chip
*this = mtd
->priv
;
556 DECLARE_WAITQUEUE(wait
, current
);
559 * Grab the lock and see if the device is available
562 spin_lock(&this->chip_lock
);
563 if (this->state
== FL_READY
) {
564 this->state
= new_state
;
565 spin_unlock(&this->chip_lock
);
568 if (new_state
== FL_PM_SUSPENDED
) {
569 spin_unlock(&this->chip_lock
);
570 return (this->state
== FL_PM_SUSPENDED
) ? 0 : -EAGAIN
;
572 set_current_state(TASK_UNINTERRUPTIBLE
);
573 add_wait_queue(&this->wq
, &wait
);
574 spin_unlock(&this->chip_lock
);
576 remove_wait_queue(&this->wq
, &wait
);
583 * onenand_release_device - [GENERIC] release chip
584 * @param mtd MTD device structure
586 * Deselect, release chip lock and wake up anyone waiting on the device
588 static void onenand_release_device(struct mtd_info
*mtd
)
590 struct onenand_chip
*this = mtd
->priv
;
592 /* Release the chip */
593 spin_lock(&this->chip_lock
);
594 this->state
= FL_READY
;
596 spin_unlock(&this->chip_lock
);
600 * onenand_read_ecc - [MTD Interface] Read data with ECC
601 * @param mtd MTD device structure
602 * @param from offset to read from
603 * @param len number of bytes to read
604 * @param retlen pointer to variable to store the number of read bytes
605 * @param buf the databuffer to put data
606 * @param oob_buf filesystem supplied oob data buffer
607 * @param oobsel oob selection structure
609 * OneNAND read with ECC
611 static int onenand_read_ecc(struct mtd_info
*mtd
, loff_t from
, size_t len
,
612 size_t *retlen
, u_char
*buf
,
613 u_char
*oob_buf
, struct nand_oobinfo
*oobsel
)
615 struct onenand_chip
*this = mtd
->priv
;
616 int read
= 0, column
;
620 DEBUG(MTD_DEBUG_LEVEL3
, "onenand_read_ecc: from = 0x%08x, len = %i\n", (unsigned int) from
, (int) len
);
622 /* Do not allow reads past end of device */
623 if ((from
+ len
) > mtd
->size
) {
624 DEBUG(MTD_DEBUG_LEVEL0
, "onenand_read_ecc: Attempt read beyond end of device\n");
629 /* Grab the lock and see if the device is available */
630 onenand_get_device(mtd
, FL_READING
);
632 /* TODO handling oob */
635 thislen
= min_t(int, mtd
->oobblock
, len
- read
);
637 column
= from
& (mtd
->oobblock
- 1);
638 if (column
+ thislen
> mtd
->oobblock
)
639 thislen
= mtd
->oobblock
- column
;
641 if (!onenand_check_bufferram(mtd
, from
)) {
642 this->command(mtd
, ONENAND_CMD_READ
, from
, mtd
->oobblock
);
644 ret
= this->wait(mtd
, FL_READING
);
645 /* First copy data and check return value for ECC handling */
646 onenand_update_bufferram(mtd
, from
, 1);
649 this->read_bufferram(mtd
, ONENAND_DATARAM
, buf
, column
, thislen
);
657 DEBUG(MTD_DEBUG_LEVEL0
, "onenand_read_ecc: read failed = %d\n", ret
);
666 /* Deselect and wake up anyone waiting on the device */
667 onenand_release_device(mtd
);
670 * Return success, if no ECC failures, else -EBADMSG
671 * fs driver will take care of that, because
672 * retlen == desired len and result == -EBADMSG
679 * onenand_read - [MTD Interface] MTD compability function for onenand_read_ecc
680 * @param mtd MTD device structure
681 * @param from offset to read from
682 * @param len number of bytes to read
683 * @param retlen pointer to variable to store the number of read bytes
684 * @param buf the databuffer to put data
686 * This function simply calls onenand_read_ecc with oob buffer and oobsel = NULL
688 static int onenand_read(struct mtd_info
*mtd
, loff_t from
, size_t len
,
689 size_t *retlen
, u_char
*buf
)
691 return onenand_read_ecc(mtd
, from
, len
, retlen
, buf
, NULL
, NULL
);
695 * onenand_read_oob - [MTD Interface] OneNAND read out-of-band
696 * @param mtd MTD device structure
697 * @param from offset to read from
698 * @param len number of bytes to read
699 * @param retlen pointer to variable to store the number of read bytes
700 * @param buf the databuffer to put data
702 * OneNAND read out-of-band data from the spare area
704 static int onenand_read_oob(struct mtd_info
*mtd
, loff_t from
, size_t len
,
705 size_t *retlen
, u_char
*buf
)
707 struct onenand_chip
*this = mtd
->priv
;
708 int read
= 0, thislen
, column
;
711 DEBUG(MTD_DEBUG_LEVEL3
, "onenand_read_oob: from = 0x%08x, len = %i\n", (unsigned int) from
, (int) len
);
713 /* Initialize return length value */
716 /* Do not allow reads past end of device */
717 if (unlikely((from
+ len
) > mtd
->size
)) {
718 DEBUG(MTD_DEBUG_LEVEL0
, "onenand_read_oob: Attempt read beyond end of device\n");
722 /* Grab the lock and see if the device is available */
723 onenand_get_device(mtd
, FL_READING
);
725 column
= from
& (mtd
->oobsize
- 1);
728 thislen
= mtd
->oobsize
- column
;
729 thislen
= min_t(int, thislen
, len
);
731 this->command(mtd
, ONENAND_CMD_READOOB
, from
, mtd
->oobsize
);
733 onenand_update_bufferram(mtd
, from
, 0);
735 ret
= this->wait(mtd
, FL_READING
);
736 /* First copy data and check return value for ECC handling */
738 this->read_bufferram(mtd
, ONENAND_SPARERAM
, buf
, column
, thislen
);
746 DEBUG(MTD_DEBUG_LEVEL0
, "onenand_read_oob: read failed = %d\n", ret
);
755 from
+= mtd
->oobblock
;
761 /* Deselect and wake up anyone waiting on the device */
762 onenand_release_device(mtd
);
768 #ifdef CONFIG_MTD_ONENAND_VERIFY_WRITE
770 * onenand_verify_oob - [GENERIC] verify the oob contents after a write
771 * @param mtd MTD device structure
772 * @param buf the databuffer to verify
773 * @param to offset to read from
774 * @param len number of bytes to read and compare
777 static int onenand_verify_oob(struct mtd_info
*mtd
, const u_char
*buf
, loff_t to
, int len
)
779 struct onenand_chip
*this = mtd
->priv
;
780 char *readp
= this->page_buf
;
781 int column
= to
& (mtd
->oobsize
- 1);
784 this->command(mtd
, ONENAND_CMD_READOOB
, to
, mtd
->oobsize
);
785 onenand_update_bufferram(mtd
, to
, 0);
786 status
= this->wait(mtd
, FL_READING
);
790 this->read_bufferram(mtd
, ONENAND_SPARERAM
, readp
, column
, len
);
792 for(i
= 0; i
< len
; i
++)
793 if (buf
[i
] != 0xFF && buf
[i
] != readp
[i
])
800 * onenand_verify_page - [GENERIC] verify the chip contents after a write
801 * @param mtd MTD device structure
802 * @param buf the databuffer to verify
804 * Check DataRAM area directly
806 static int onenand_verify_page(struct mtd_info
*mtd
, u_char
*buf
, loff_t addr
)
808 struct onenand_chip
*this = mtd
->priv
;
809 void __iomem
*dataram0
, *dataram1
;
812 this->command(mtd
, ONENAND_CMD_READ
, addr
, mtd
->oobblock
);
814 ret
= this->wait(mtd
, FL_READING
);
818 onenand_update_bufferram(mtd
, addr
, 1);
820 /* Check, if the two dataram areas are same */
821 dataram0
= this->base
+ ONENAND_DATARAM
;
822 dataram1
= dataram0
+ mtd
->oobblock
;
824 if (memcmp(dataram0
, dataram1
, mtd
->oobblock
))
830 #define onenand_verify_page(...) (0)
831 #define onenand_verify_oob(...) (0)
834 #define NOTALIGNED(x) ((x & (mtd->oobblock - 1)) != 0)
837 * onenand_write_ecc - [MTD Interface] OneNAND write with ECC
838 * @param mtd MTD device structure
839 * @param to offset to write to
840 * @param len number of bytes to write
841 * @param retlen pointer to variable to store the number of written bytes
842 * @param buf the data to write
843 * @param eccbuf filesystem supplied oob data buffer
844 * @param oobsel oob selection structure
846 * OneNAND write with ECC
848 static int onenand_write_ecc(struct mtd_info
*mtd
, loff_t to
, size_t len
,
849 size_t *retlen
, const u_char
*buf
,
850 u_char
*eccbuf
, struct nand_oobinfo
*oobsel
)
852 struct onenand_chip
*this = mtd
->priv
;
856 DEBUG(MTD_DEBUG_LEVEL3
, "onenand_write_ecc: to = 0x%08x, len = %i\n", (unsigned int) to
, (int) len
);
858 /* Initialize retlen, in case of early exit */
861 /* Do not allow writes past end of device */
862 if (unlikely((to
+ len
) > mtd
->size
)) {
863 DEBUG(MTD_DEBUG_LEVEL0
, "onenand_write_ecc: Attempt write to past end of device\n");
867 /* Reject writes, which are not page aligned */
868 if (unlikely(NOTALIGNED(to
)) || unlikely(NOTALIGNED(len
))) {
869 DEBUG(MTD_DEBUG_LEVEL0
, "onenand_write_ecc: Attempt to write not page aligned data\n");
873 /* Grab the lock and see if the device is available */
874 onenand_get_device(mtd
, FL_WRITING
);
876 /* Loop until all data write */
877 while (written
< len
) {
878 int thislen
= min_t(int, mtd
->oobblock
, len
- written
);
880 this->command(mtd
, ONENAND_CMD_BUFFERRAM
, to
, mtd
->oobblock
);
882 this->write_bufferram(mtd
, ONENAND_DATARAM
, buf
, 0, thislen
);
883 this->write_bufferram(mtd
, ONENAND_SPARERAM
, ffchars
, 0, mtd
->oobsize
);
885 this->command(mtd
, ONENAND_CMD_PROG
, to
, mtd
->oobblock
);
887 onenand_update_bufferram(mtd
, to
, 1);
889 ret
= this->wait(mtd
, FL_WRITING
);
891 DEBUG(MTD_DEBUG_LEVEL0
, "onenand_write_ecc: write filaed %d\n", ret
);
897 /* Only check verify write turn on */
898 ret
= onenand_verify_page(mtd
, (u_char
*) buf
, to
);
900 DEBUG(MTD_DEBUG_LEVEL0
, "onenand_write_ecc: verify failed %d\n", ret
);
912 /* Deselect and wake up anyone waiting on the device */
913 onenand_release_device(mtd
);
921 * onenand_write - [MTD Interface] compability function for onenand_write_ecc
922 * @param mtd MTD device structure
923 * @param to offset to write to
924 * @param len number of bytes to write
925 * @param retlen pointer to variable to store the number of written bytes
926 * @param buf the data to write
928 * This function simply calls onenand_write_ecc
929 * with oob buffer and oobsel = NULL
931 static int onenand_write(struct mtd_info
*mtd
, loff_t to
, size_t len
,
932 size_t *retlen
, const u_char
*buf
)
934 return onenand_write_ecc(mtd
, to
, len
, retlen
, buf
, NULL
, NULL
);
938 * onenand_write_oob - [MTD Interface] OneNAND write out-of-band
939 * @param mtd MTD device structure
940 * @param to offset to write to
941 * @param len number of bytes to write
942 * @param retlen pointer to variable to store the number of written bytes
943 * @param buf the data to write
945 * OneNAND write out-of-band
947 static int onenand_write_oob(struct mtd_info
*mtd
, loff_t to
, size_t len
,
948 size_t *retlen
, const u_char
*buf
)
950 struct onenand_chip
*this = mtd
->priv
;
954 DEBUG(MTD_DEBUG_LEVEL3
, "onenand_write_oob: to = 0x%08x, len = %i\n", (unsigned int) to
, (int) len
);
956 /* Initialize retlen, in case of early exit */
959 /* Do not allow writes past end of device */
960 if (unlikely((to
+ len
) > mtd
->size
)) {
961 DEBUG(MTD_DEBUG_LEVEL0
, "onenand_write_oob: Attempt write to past end of device\n");
965 /* Grab the lock and see if the device is available */
966 onenand_get_device(mtd
, FL_WRITING
);
968 /* Loop until all data write */
969 while (written
< len
) {
970 int thislen
= min_t(int, mtd
->oobsize
, len
- written
);
972 column
= to
& (mtd
->oobsize
- 1);
974 this->command(mtd
, ONENAND_CMD_BUFFERRAM
, to
, mtd
->oobsize
);
976 /* We send data to spare ram with oobsize
977 * to prevent byte access */
978 memset(this->page_buf
, 0xff, mtd
->oobsize
);
979 memcpy(this->page_buf
+ column
, buf
, thislen
);
980 this->write_bufferram(mtd
, ONENAND_SPARERAM
, this->page_buf
, 0, mtd
->oobsize
);
982 this->command(mtd
, ONENAND_CMD_PROGOOB
, to
, mtd
->oobsize
);
984 onenand_update_bufferram(mtd
, to
, 0);
986 ret
= this->wait(mtd
, FL_WRITING
);
988 DEBUG(MTD_DEBUG_LEVEL0
, "onenand_write_oob: write filaed %d\n", ret
);
992 ret
= onenand_verify_oob(mtd
, buf
, to
, thislen
);
994 DEBUG(MTD_DEBUG_LEVEL0
, "onenand_write_oob: verify failed %d\n", ret
);
1008 /* Deselect and wake up anyone waiting on the device */
1009 onenand_release_device(mtd
);
1017 * onenand_writev_ecc - [MTD Interface] write with iovec with ecc
1018 * @param mtd MTD device structure
1019 * @param vecs the iovectors to write
1020 * @param count number of vectors
1021 * @param to offset to write to
1022 * @param retlen pointer to variable to store the number of written bytes
1023 * @param eccbuf filesystem supplied oob data buffer
1024 * @param oobsel oob selection structure
1026 * OneNAND write with iovec with ecc
1028 static int onenand_writev_ecc(struct mtd_info
*mtd
, const struct kvec
*vecs
,
1029 unsigned long count
, loff_t to
, size_t *retlen
,
1030 u_char
*eccbuf
, struct nand_oobinfo
*oobsel
)
1032 struct onenand_chip
*this = mtd
->priv
;
1033 unsigned char *pbuf
;
1034 size_t total_len
, len
;
1038 /* Preset written len for early exit */
1041 /* Calculate total length of data */
1043 for (i
= 0; i
< count
; i
++)
1044 total_len
+= vecs
[i
].iov_len
;
1046 DEBUG(MTD_DEBUG_LEVEL3
, "onenand_writev_ecc: to = 0x%08x, len = %i, count = %ld\n", (unsigned int) to
, (unsigned int) total_len
, count
);
1048 /* Do not allow write past end of the device */
1049 if (unlikely((to
+ total_len
) > mtd
->size
)) {
1050 DEBUG(MTD_DEBUG_LEVEL0
, "onenand_writev_ecc: Attempted write past end of device\n");
1054 /* Reject writes, which are not page aligned */
1055 if (unlikely(NOTALIGNED(to
)) || unlikely(NOTALIGNED(total_len
))) {
1056 DEBUG(MTD_DEBUG_LEVEL0
, "onenand_writev_ecc: Attempt to write not page aligned data\n");
1060 /* Grab the lock and see if the device is available */
1061 onenand_get_device(mtd
, FL_WRITING
);
1063 /* TODO handling oob */
1065 /* Loop until all keve's data has been written */
1068 pbuf
= this->page_buf
;
1070 * If the given tuple is >= pagesize then
1071 * write it out from the iov
1073 if ((vecs
->iov_len
- len
) >= mtd
->oobblock
) {
1074 pbuf
= vecs
->iov_base
+ len
;
1076 len
+= mtd
->oobblock
;
1078 /* Check, if we have to switch to the next tuple */
1079 if (len
>= (int) vecs
->iov_len
) {
1085 int cnt
= 0, thislen
;
1086 while (cnt
< mtd
->oobblock
) {
1087 thislen
= min_t(int, mtd
->oobblock
- cnt
, vecs
->iov_len
- len
);
1088 memcpy(this->page_buf
+ cnt
, vecs
->iov_base
+ len
, thislen
);
1092 /* Check, if we have to switch to the next tuple */
1093 if (len
>= (int) vecs
->iov_len
) {
1101 this->command(mtd
, ONENAND_CMD_BUFFERRAM
, to
, mtd
->oobblock
);
1103 this->write_bufferram(mtd
, ONENAND_DATARAM
, pbuf
, 0, mtd
->oobblock
);
1104 this->write_bufferram(mtd
, ONENAND_SPARERAM
, ffchars
, 0, mtd
->oobsize
);
1106 this->command(mtd
, ONENAND_CMD_PROG
, to
, mtd
->oobblock
);
1108 onenand_update_bufferram(mtd
, to
, 1);
1110 ret
= this->wait(mtd
, FL_WRITING
);
1112 DEBUG(MTD_DEBUG_LEVEL0
, "onenand_writev_ecc: write failed %d\n", ret
);
1117 /* Only check verify write turn on */
1118 ret
= onenand_verify_page(mtd
, (u_char
*) pbuf
, to
);
1120 DEBUG(MTD_DEBUG_LEVEL0
, "onenand_writev_ecc: verify failed %d\n", ret
);
1124 written
+= mtd
->oobblock
;
1126 to
+= mtd
->oobblock
;
1130 /* Deselect and wakt up anyone waiting on the device */
1131 onenand_release_device(mtd
);
1139 * onenand_writev - [MTD Interface] compabilty function for onenand_writev_ecc
1140 * @param mtd MTD device structure
1141 * @param vecs the iovectors to write
1142 * @param count number of vectors
1143 * @param to offset to write to
1144 * @param retlen pointer to variable to store the number of written bytes
1146 * OneNAND write with kvec. This just calls the ecc function
1148 static int onenand_writev(struct mtd_info
*mtd
, const struct kvec
*vecs
,
1149 unsigned long count
, loff_t to
, size_t *retlen
)
1151 return onenand_writev_ecc(mtd
, vecs
, count
, to
, retlen
, NULL
, NULL
);
1155 * onenand_block_checkbad - [GENERIC] Check if a block is marked bad
1156 * @param mtd MTD device structure
1157 * @param ofs offset from device start
1158 * @param getchip 0, if the chip is already selected
1159 * @param allowbbt 1, if its allowed to access the bbt area
1161 * Check, if the block is bad. Either by reading the bad block table or
1162 * calling of the scan function.
1164 static int onenand_block_checkbad(struct mtd_info
*mtd
, loff_t ofs
, int getchip
, int allowbbt
)
1166 struct onenand_chip
*this = mtd
->priv
;
1167 struct bbm_info
*bbm
= this->bbm
;
1169 /* Return info from the table */
1170 return bbm
->isbad_bbt(mtd
, ofs
, allowbbt
);
1174 * onenand_erase - [MTD Interface] erase block(s)
1175 * @param mtd MTD device structure
1176 * @param instr erase instruction
1178 * Erase one ore more blocks
1180 static int onenand_erase(struct mtd_info
*mtd
, struct erase_info
*instr
)
1182 struct onenand_chip
*this = mtd
->priv
;
1183 unsigned int block_size
;
1188 DEBUG(MTD_DEBUG_LEVEL3
, "onenand_erase: start = 0x%08x, len = %i\n", (unsigned int) instr
->addr
, (unsigned int) instr
->len
);
1190 block_size
= (1 << this->erase_shift
);
1192 /* Start address must align on block boundary */
1193 if (unlikely(instr
->addr
& (block_size
- 1))) {
1194 DEBUG(MTD_DEBUG_LEVEL0
, "onenand_erase: Unaligned address\n");
1198 /* Length must align on block boundary */
1199 if (unlikely(instr
->len
& (block_size
- 1))) {
1200 DEBUG(MTD_DEBUG_LEVEL0
, "onenand_erase: Length not block aligned\n");
1204 /* Do not allow erase past end of device */
1205 if (unlikely((instr
->len
+ instr
->addr
) > mtd
->size
)) {
1206 DEBUG(MTD_DEBUG_LEVEL0
, "onenand_erase: Erase past end of device\n");
1210 instr
->fail_addr
= 0xffffffff;
1212 /* Grab the lock and see if the device is available */
1213 onenand_get_device(mtd
, FL_ERASING
);
1215 /* Loop throught the pages */
1219 instr
->state
= MTD_ERASING
;
1223 /* Check if we have a bad block, we do not erase bad blocks */
1224 if (onenand_block_checkbad(mtd
, addr
, 0, 0)) {
1225 printk (KERN_WARNING
"onenand_erase: attempt to erase a bad block at addr 0x%08x\n", (unsigned int) addr
);
1226 instr
->state
= MTD_ERASE_FAILED
;
1230 this->command(mtd
, ONENAND_CMD_ERASE
, addr
, block_size
);
1232 ret
= this->wait(mtd
, FL_ERASING
);
1233 /* Check, if it is write protected */
1236 DEBUG(MTD_DEBUG_LEVEL0
, "onenand_erase: Device is write protected!!!\n");
1238 DEBUG(MTD_DEBUG_LEVEL0
, "onenand_erase: Failed erase, block %d\n", (unsigned) (addr
>> this->erase_shift
));
1239 instr
->state
= MTD_ERASE_FAILED
;
1240 instr
->fail_addr
= addr
;
1248 instr
->state
= MTD_ERASE_DONE
;
1252 ret
= instr
->state
== MTD_ERASE_DONE
? 0 : -EIO
;
1253 /* Do call back function */
1255 mtd_erase_callback(instr
);
1257 /* Deselect and wake up anyone waiting on the device */
1258 onenand_release_device(mtd
);
1264 * onenand_sync - [MTD Interface] sync
1265 * @param mtd MTD device structure
1267 * Sync is actually a wait for chip ready function
1269 static void onenand_sync(struct mtd_info
*mtd
)
1271 DEBUG(MTD_DEBUG_LEVEL3
, "onenand_sync: called\n");
1273 /* Grab the lock and see if the device is available */
1274 onenand_get_device(mtd
, FL_SYNCING
);
1276 /* Release it and go back */
1277 onenand_release_device(mtd
);
1282 * onenand_block_isbad - [MTD Interface] Check whether the block at the given offset is bad
1283 * @param mtd MTD device structure
1284 * @param ofs offset relative to mtd start
1286 * Check whether the block is bad
1288 static int onenand_block_isbad(struct mtd_info
*mtd
, loff_t ofs
)
1290 /* Check for invalid offset */
1291 if (ofs
> mtd
->size
)
1294 return onenand_block_checkbad(mtd
, ofs
, 1, 0);
1298 * onenand_default_block_markbad - [DEFAULT] mark a block bad
1299 * @param mtd MTD device structure
1300 * @param ofs offset from device start
1302 * This is the default implementation, which can be overridden by
1303 * a hardware specific driver.
1305 static int onenand_default_block_markbad(struct mtd_info
*mtd
, loff_t ofs
)
1307 struct onenand_chip
*this = mtd
->priv
;
1308 struct bbm_info
*bbm
= this->bbm
;
1309 u_char buf
[2] = {0, 0};
1313 /* Get block number */
1314 block
= ((int) ofs
) >> bbm
->bbt_erase_shift
;
1316 bbm
->bbt
[block
>> 2] |= 0x01 << ((block
& 0x03) << 1);
1318 /* We write two bytes, so we dont have to mess with 16 bit access */
1319 ofs
+= mtd
->oobsize
+ (bbm
->badblockpos
& ~0x01);
1320 return mtd
->write_oob(mtd
, ofs
, 2, &retlen
, buf
);
1324 * onenand_block_markbad - [MTD Interface] Mark the block at the given offset as bad
1325 * @param mtd MTD device structure
1326 * @param ofs offset relative to mtd start
1328 * Mark the block as bad
1330 static int onenand_block_markbad(struct mtd_info
*mtd
, loff_t ofs
)
1332 struct onenand_chip
*this = mtd
->priv
;
1335 ret
= onenand_block_isbad(mtd
, ofs
);
1337 /* If it was bad already, return success and do nothing */
1343 return this->block_markbad(mtd
, ofs
);
1347 * onenand_unlock - [MTD Interface] Unlock block(s)
1348 * @param mtd MTD device structure
1349 * @param ofs offset relative to mtd start
1350 * @param len number of bytes to unlock
1352 * Unlock one or more blocks
1354 static int onenand_unlock(struct mtd_info
*mtd
, loff_t ofs
, size_t len
)
1356 struct onenand_chip
*this = mtd
->priv
;
1357 int start
, end
, block
, value
, status
;
1359 start
= ofs
>> this->erase_shift
;
1360 end
= len
>> this->erase_shift
;
1362 /* Continuous lock scheme */
1363 if (this->options
& ONENAND_CONT_LOCK
) {
1364 /* Set start block address */
1365 this->write_word(start
, this->base
+ ONENAND_REG_START_BLOCK_ADDRESS
);
1366 /* Set end block address */
1367 this->write_word(end
- 1, this->base
+ ONENAND_REG_END_BLOCK_ADDRESS
);
1368 /* Write unlock command */
1369 this->command(mtd
, ONENAND_CMD_UNLOCK
, 0, 0);
1371 /* There's no return value */
1372 this->wait(mtd
, FL_UNLOCKING
);
1375 while (this->read_word(this->base
+ ONENAND_REG_CTRL_STATUS
)
1376 & ONENAND_CTRL_ONGO
)
1379 /* Check lock status */
1380 status
= this->read_word(this->base
+ ONENAND_REG_WP_STATUS
);
1381 if (!(status
& ONENAND_WP_US
))
1382 printk(KERN_ERR
"wp status = 0x%x\n", status
);
1387 /* Block lock scheme */
1388 for (block
= start
; block
< end
; block
++) {
1389 /* Set block address */
1390 value
= onenand_block_address(this, block
);
1391 this->write_word(value
, this->base
+ ONENAND_REG_START_ADDRESS1
);
1392 /* Select DataRAM for DDP */
1393 value
= onenand_bufferram_address(this, block
);
1394 this->write_word(value
, this->base
+ ONENAND_REG_START_ADDRESS2
);
1395 /* Set start block address */
1396 this->write_word(block
, this->base
+ ONENAND_REG_START_BLOCK_ADDRESS
);
1397 /* Write unlock command */
1398 this->command(mtd
, ONENAND_CMD_UNLOCK
, 0, 0);
1400 /* There's no return value */
1401 this->wait(mtd
, FL_UNLOCKING
);
1404 while (this->read_word(this->base
+ ONENAND_REG_CTRL_STATUS
)
1405 & ONENAND_CTRL_ONGO
)
1408 /* Check lock status */
1409 status
= this->read_word(this->base
+ ONENAND_REG_WP_STATUS
);
1410 if (!(status
& ONENAND_WP_US
))
1411 printk(KERN_ERR
"block = %d, wp status = 0x%x\n", block
, status
);
1417 #ifdef CONFIG_MTD_ONENAND_OTP
1419 /* Interal OTP operation */
1420 typedef int (*otp_op_t
)(struct mtd_info
*mtd
, loff_t form
, size_t len
,
1421 size_t *retlen
, u_char
*buf
);
1424 * do_otp_read - [DEFAULT] Read OTP block area
1425 * @param mtd MTD device structure
1426 * @param from The offset to read
1427 * @param len number of bytes to read
1428 * @param retlen pointer to variable to store the number of readbytes
1429 * @param buf the databuffer to put/get data
1431 * Read OTP block area.
1433 static int do_otp_read(struct mtd_info
*mtd
, loff_t from
, size_t len
,
1434 size_t *retlen
, u_char
*buf
)
1436 struct onenand_chip
*this = mtd
->priv
;
1439 /* Enter OTP access mode */
1440 this->command(mtd
, ONENAND_CMD_OTP_ACCESS
, 0, 0);
1441 this->wait(mtd
, FL_OTPING
);
1443 ret
= mtd
->read(mtd
, from
, len
, retlen
, buf
);
1445 /* Exit OTP access mode */
1446 this->command(mtd
, ONENAND_CMD_RESET
, 0, 0);
1447 this->wait(mtd
, FL_RESETING
);
1453 * do_otp_write - [DEFAULT] Write OTP block area
1454 * @param mtd MTD device structure
1455 * @param from The offset to write
1456 * @param len number of bytes to write
1457 * @param retlen pointer to variable to store the number of write bytes
1458 * @param buf the databuffer to put/get data
1460 * Write OTP block area.
1462 static int do_otp_write(struct mtd_info
*mtd
, loff_t from
, size_t len
,
1463 size_t *retlen
, u_char
*buf
)
1465 struct onenand_chip
*this = mtd
->priv
;
1466 unsigned char *pbuf
= buf
;
1469 /* Force buffer page aligned */
1470 if (len
< mtd
->oobblock
) {
1471 memcpy(this->page_buf
, buf
, len
);
1472 memset(this->page_buf
+ len
, 0xff, mtd
->oobblock
- len
);
1473 pbuf
= this->page_buf
;
1474 len
= mtd
->oobblock
;
1477 /* Enter OTP access mode */
1478 this->command(mtd
, ONENAND_CMD_OTP_ACCESS
, 0, 0);
1479 this->wait(mtd
, FL_OTPING
);
1481 ret
= mtd
->write(mtd
, from
, len
, retlen
, pbuf
);
1483 /* Exit OTP access mode */
1484 this->command(mtd
, ONENAND_CMD_RESET
, 0, 0);
1485 this->wait(mtd
, FL_RESETING
);
1491 * do_otp_lock - [DEFAULT] Lock OTP block area
1492 * @param mtd MTD device structure
1493 * @param from The offset to lock
1494 * @param len number of bytes to lock
1495 * @param retlen pointer to variable to store the number of lock bytes
1496 * @param buf the databuffer to put/get data
1498 * Lock OTP block area.
1500 static int do_otp_lock(struct mtd_info
*mtd
, loff_t from
, size_t len
,
1501 size_t *retlen
, u_char
*buf
)
1503 struct onenand_chip
*this = mtd
->priv
;
1506 /* Enter OTP access mode */
1507 this->command(mtd
, ONENAND_CMD_OTP_ACCESS
, 0, 0);
1508 this->wait(mtd
, FL_OTPING
);
1510 ret
= mtd
->write_oob(mtd
, from
, len
, retlen
, buf
);
1512 /* Exit OTP access mode */
1513 this->command(mtd
, ONENAND_CMD_RESET
, 0, 0);
1514 this->wait(mtd
, FL_RESETING
);
1520 * onenand_otp_walk - [DEFAULT] Handle OTP operation
1521 * @param mtd MTD device structure
1522 * @param from The offset to read/write
1523 * @param len number of bytes to read/write
1524 * @param retlen pointer to variable to store the number of read bytes
1525 * @param buf the databuffer to put/get data
1526 * @param action do given action
1527 * @param mode specify user and factory
1529 * Handle OTP operation.
1531 static int onenand_otp_walk(struct mtd_info
*mtd
, loff_t from
, size_t len
,
1532 size_t *retlen
, u_char
*buf
,
1533 otp_op_t action
, int mode
)
1535 struct onenand_chip
*this = mtd
->priv
;
1542 density
= this->device_id
>> ONENAND_DEVICE_DENSITY_SHIFT
;
1543 if (density
< ONENAND_DEVICE_DENSITY_512Mb
)
1548 if (mode
== MTD_OTP_FACTORY
) {
1549 from
+= mtd
->oobblock
* otp_pages
;
1550 otp_pages
= 64 - otp_pages
;
1553 /* Check User/Factory boundary */
1554 if (((mtd
->oobblock
* otp_pages
) - (from
+ len
)) < 0)
1557 while (len
> 0 && otp_pages
> 0) {
1558 if (!action
) { /* OTP Info functions */
1559 struct otp_info
*otpinfo
;
1561 len
-= sizeof(struct otp_info
);
1565 otpinfo
= (struct otp_info
*) buf
;
1566 otpinfo
->start
= from
;
1567 otpinfo
->length
= mtd
->oobblock
;
1568 otpinfo
->locked
= 0;
1570 from
+= mtd
->oobblock
;
1571 buf
+= sizeof(struct otp_info
);
1572 *retlen
+= sizeof(struct otp_info
);
1577 ret
= action(mtd
, from
, len
, &tmp_retlen
, buf
);
1593 * onenand_get_fact_prot_info - [MTD Interface] Read factory OTP info
1594 * @param mtd MTD device structure
1595 * @param buf the databuffer to put/get data
1596 * @param len number of bytes to read
1598 * Read factory OTP info.
1600 static int onenand_get_fact_prot_info(struct mtd_info
*mtd
,
1601 struct otp_info
*buf
, size_t len
)
1606 ret
= onenand_otp_walk(mtd
, 0, len
, &retlen
, (u_char
*) buf
, NULL
, MTD_OTP_FACTORY
);
1608 return ret
? : retlen
;
1612 * onenand_read_fact_prot_reg - [MTD Interface] Read factory OTP area
1613 * @param mtd MTD device structure
1614 * @param from The offset to read
1615 * @param len number of bytes to read
1616 * @param retlen pointer to variable to store the number of read bytes
1617 * @param buf the databuffer to put/get data
1619 * Read factory OTP area.
1621 static int onenand_read_fact_prot_reg(struct mtd_info
*mtd
, loff_t from
,
1622 size_t len
, size_t *retlen
, u_char
*buf
)
1624 return onenand_otp_walk(mtd
, from
, len
, retlen
, buf
, do_otp_read
, MTD_OTP_FACTORY
);
1628 * onenand_get_user_prot_info - [MTD Interface] Read user OTP info
1629 * @param mtd MTD device structure
1630 * @param buf the databuffer to put/get data
1631 * @param len number of bytes to read
1633 * Read user OTP info.
1635 static int onenand_get_user_prot_info(struct mtd_info
*mtd
,
1636 struct otp_info
*buf
, size_t len
)
1641 ret
= onenand_otp_walk(mtd
, 0, len
, &retlen
, (u_char
*) buf
, NULL
, MTD_OTP_USER
);
1643 return ret
? : retlen
;
1647 * onenand_read_user_prot_reg - [MTD Interface] Read user OTP area
1648 * @param mtd MTD device structure
1649 * @param from The offset to read
1650 * @param len number of bytes to read
1651 * @param retlen pointer to variable to store the number of read bytes
1652 * @param buf the databuffer to put/get data
1654 * Read user OTP area.
1656 static int onenand_read_user_prot_reg(struct mtd_info
*mtd
, loff_t from
,
1657 size_t len
, size_t *retlen
, u_char
*buf
)
1659 return onenand_otp_walk(mtd
, from
, len
, retlen
, buf
, do_otp_read
, MTD_OTP_USER
);
1663 * onenand_write_user_prot_reg - [MTD Interface] Write user OTP area
1664 * @param mtd MTD device structure
1665 * @param from The offset to write
1666 * @param len number of bytes to write
1667 * @param retlen pointer to variable to store the number of write bytes
1668 * @param buf the databuffer to put/get data
1670 * Write user OTP area.
1672 static int onenand_write_user_prot_reg(struct mtd_info
*mtd
, loff_t from
,
1673 size_t len
, size_t *retlen
, u_char
*buf
)
1675 return onenand_otp_walk(mtd
, from
, len
, retlen
, buf
, do_otp_write
, MTD_OTP_USER
);
1679 * onenand_lock_user_prot_reg - [MTD Interface] Lock user OTP area
1680 * @param mtd MTD device structure
1681 * @param from The offset to lock
1682 * @param len number of bytes to unlock
1684 * Write lock mark on spare area in page 0 in OTP block
1686 static int onenand_lock_user_prot_reg(struct mtd_info
*mtd
, loff_t from
,
1689 unsigned char oob_buf
[64];
1693 memset(oob_buf
, 0xff, mtd
->oobsize
);
1695 * Note: OTP lock operation
1696 * OTP block : 0xXXFC
1697 * 1st block : 0xXXF3 (If chip support)
1698 * Both : 0xXXF0 (If chip support)
1700 oob_buf
[ONENAND_OTP_LOCK_OFFSET
] = 0xFC;
1703 * Write lock mark to 8th word of sector0 of page0 of the spare0.
1704 * We write 16 bytes spare area instead of 2 bytes.
1709 ret
= onenand_otp_walk(mtd
, from
, len
, &retlen
, oob_buf
, do_otp_lock
, MTD_OTP_USER
);
1711 return ret
? : retlen
;
1713 #endif /* CONFIG_MTD_ONENAND_OTP */
1716 * onenand_print_device_info - Print device ID
1717 * @param device device ID
1721 static void onenand_print_device_info(int device
)
1723 int vcc
, demuxed
, ddp
, density
;
1725 vcc
= device
& ONENAND_DEVICE_VCC_MASK
;
1726 demuxed
= device
& ONENAND_DEVICE_IS_DEMUX
;
1727 ddp
= device
& ONENAND_DEVICE_IS_DDP
;
1728 density
= device
>> ONENAND_DEVICE_DENSITY_SHIFT
;
1729 printk(KERN_INFO
"%sOneNAND%s %dMB %sV 16-bit (0x%02x)\n",
1730 demuxed
? "" : "Muxed ",
1733 vcc
? "2.65/3.3" : "1.8",
1737 static const struct onenand_manufacturers onenand_manuf_ids
[] = {
1738 {ONENAND_MFR_SAMSUNG
, "Samsung"},
1742 * onenand_check_maf - Check manufacturer ID
1743 * @param manuf manufacturer ID
1745 * Check manufacturer ID
1747 static int onenand_check_maf(int manuf
)
1749 int size
= ARRAY_SIZE(onenand_manuf_ids
);
1753 for (i
= 0; i
< size
; i
++)
1754 if (manuf
== onenand_manuf_ids
[i
].id
)
1758 name
= onenand_manuf_ids
[i
].name
;
1762 printk(KERN_DEBUG
"OneNAND Manufacturer: %s (0x%0x)\n", name
, manuf
);
1768 * onenand_probe - [OneNAND Interface] Probe the OneNAND device
1769 * @param mtd MTD device structure
1771 * OneNAND detection method:
1772 * Compare the the values from command with ones from register
1774 static int onenand_probe(struct mtd_info
*mtd
)
1776 struct onenand_chip
*this = mtd
->priv
;
1777 int bram_maf_id
, bram_dev_id
, maf_id
, dev_id
;
1781 /* Send the command for reading device ID from BootRAM */
1782 this->write_word(ONENAND_CMD_READID
, this->base
+ ONENAND_BOOTRAM
);
1784 /* Read manufacturer and device IDs from BootRAM */
1785 bram_maf_id
= this->read_word(this->base
+ ONENAND_BOOTRAM
+ 0x0);
1786 bram_dev_id
= this->read_word(this->base
+ ONENAND_BOOTRAM
+ 0x2);
1788 /* Check manufacturer ID */
1789 if (onenand_check_maf(bram_maf_id
))
1792 /* Reset OneNAND to read default register values */
1793 this->write_word(ONENAND_CMD_RESET
, this->base
+ ONENAND_BOOTRAM
);
1795 /* Read manufacturer and device IDs from Register */
1796 maf_id
= this->read_word(this->base
+ ONENAND_REG_MANUFACTURER_ID
);
1797 dev_id
= this->read_word(this->base
+ ONENAND_REG_DEVICE_ID
);
1799 /* Check OneNAND device */
1800 if (maf_id
!= bram_maf_id
|| dev_id
!= bram_dev_id
)
1803 /* Flash device information */
1804 onenand_print_device_info(dev_id
);
1805 this->device_id
= dev_id
;
1807 density
= dev_id
>> ONENAND_DEVICE_DENSITY_SHIFT
;
1808 this->chipsize
= (16 << density
) << 20;
1809 /* Set density mask. it is used for DDP */
1810 this->density_mask
= (1 << (density
+ 6));
1812 /* OneNAND page size & block size */
1813 /* The data buffer size is equal to page size */
1814 mtd
->oobblock
= this->read_word(this->base
+ ONENAND_REG_DATA_BUFFER_SIZE
);
1815 mtd
->oobsize
= mtd
->oobblock
>> 5;
1816 /* Pagers per block is always 64 in OneNAND */
1817 mtd
->erasesize
= mtd
->oobblock
<< 6;
1819 this->erase_shift
= ffs(mtd
->erasesize
) - 1;
1820 this->page_shift
= ffs(mtd
->oobblock
) - 1;
1821 this->ppb_shift
= (this->erase_shift
- this->page_shift
);
1822 this->page_mask
= (mtd
->erasesize
/ mtd
->oobblock
) - 1;
1824 /* REVIST: Multichip handling */
1826 mtd
->size
= this->chipsize
;
1829 version_id
= this->read_word(this->base
+ ONENAND_REG_VERSION_ID
);
1830 printk(KERN_DEBUG
"OneNAND version = 0x%04x\n", version_id
);
1833 if (density
<= ONENAND_DEVICE_DENSITY_512Mb
&&
1834 !(version_id
>> ONENAND_VERSION_PROCESS_SHIFT
)) {
1835 printk(KERN_INFO
"Lock scheme is Continues Lock\n");
1836 this->options
|= ONENAND_CONT_LOCK
;
1843 * onenand_suspend - [MTD Interface] Suspend the OneNAND flash
1844 * @param mtd MTD device structure
1846 static int onenand_suspend(struct mtd_info
*mtd
)
1848 return onenand_get_device(mtd
, FL_PM_SUSPENDED
);
1852 * onenand_resume - [MTD Interface] Resume the OneNAND flash
1853 * @param mtd MTD device structure
1855 static void onenand_resume(struct mtd_info
*mtd
)
1857 struct onenand_chip
*this = mtd
->priv
;
1859 if (this->state
== FL_PM_SUSPENDED
)
1860 onenand_release_device(mtd
);
1862 printk(KERN_ERR
"resume() called for the chip which is not"
1863 "in suspended state\n");
1867 * onenand_scan - [OneNAND Interface] Scan for the OneNAND device
1868 * @param mtd MTD device structure
1869 * @param maxchips Number of chips to scan for
1871 * This fills out all the not initialized function pointers
1872 * with the defaults.
1873 * The flash ID is read and the mtd/chip structures are
1874 * filled with the appropriate values.
1876 int onenand_scan(struct mtd_info
*mtd
, int maxchips
)
1878 struct onenand_chip
*this = mtd
->priv
;
1880 if (!this->read_word
)
1881 this->read_word
= onenand_readw
;
1882 if (!this->write_word
)
1883 this->write_word
= onenand_writew
;
1886 this->command
= onenand_command
;
1888 this->wait
= onenand_wait
;
1890 if (!this->read_bufferram
)
1891 this->read_bufferram
= onenand_read_bufferram
;
1892 if (!this->write_bufferram
)
1893 this->write_bufferram
= onenand_write_bufferram
;
1895 if (!this->block_markbad
)
1896 this->block_markbad
= onenand_default_block_markbad
;
1897 if (!this->scan_bbt
)
1898 this->scan_bbt
= onenand_default_bbt
;
1900 if (onenand_probe(mtd
))
1903 /* Set Sync. Burst Read after probing */
1904 if (this->mmcontrol
) {
1905 printk(KERN_INFO
"OneNAND Sync. Burst Read support\n");
1906 this->read_bufferram
= onenand_sync_read_bufferram
;
1909 /* Allocate buffers, if necessary */
1910 if (!this->page_buf
) {
1912 len
= mtd
->oobblock
+ mtd
->oobsize
;
1913 this->page_buf
= kmalloc(len
, GFP_KERNEL
);
1914 if (!this->page_buf
) {
1915 printk(KERN_ERR
"onenand_scan(): Can't allocate page_buf\n");
1918 this->options
|= ONENAND_PAGEBUF_ALLOC
;
1921 this->state
= FL_READY
;
1922 init_waitqueue_head(&this->wq
);
1923 spin_lock_init(&this->chip_lock
);
1925 switch (mtd
->oobsize
) {
1927 this->autooob
= &onenand_oob_64
;
1931 this->autooob
= &onenand_oob_32
;
1935 printk(KERN_WARNING
"No OOB scheme defined for oobsize %d\n",
1937 /* To prevent kernel oops */
1938 this->autooob
= &onenand_oob_32
;
1942 memcpy(&mtd
->oobinfo
, this->autooob
, sizeof(mtd
->oobinfo
));
1944 /* Fill in remaining MTD driver data */
1945 mtd
->type
= MTD_NANDFLASH
;
1946 mtd
->flags
= MTD_CAP_NANDFLASH
| MTD_ECC
;
1947 mtd
->ecctype
= MTD_ECC_SW
;
1948 mtd
->erase
= onenand_erase
;
1950 mtd
->unpoint
= NULL
;
1951 mtd
->read
= onenand_read
;
1952 mtd
->write
= onenand_write
;
1953 mtd
->read_ecc
= onenand_read_ecc
;
1954 mtd
->write_ecc
= onenand_write_ecc
;
1955 mtd
->read_oob
= onenand_read_oob
;
1956 mtd
->write_oob
= onenand_write_oob
;
1957 #ifdef CONFIG_MTD_ONENAND_OTP
1958 mtd
->get_fact_prot_info
= onenand_get_fact_prot_info
;
1959 mtd
->read_fact_prot_reg
= onenand_read_fact_prot_reg
;
1960 mtd
->get_user_prot_info
= onenand_get_user_prot_info
;
1961 mtd
->read_user_prot_reg
= onenand_read_user_prot_reg
;
1962 mtd
->write_user_prot_reg
= onenand_write_user_prot_reg
;
1963 mtd
->lock_user_prot_reg
= onenand_lock_user_prot_reg
;
1966 mtd
->readv_ecc
= NULL
;
1967 mtd
->writev
= onenand_writev
;
1968 mtd
->writev_ecc
= onenand_writev_ecc
;
1969 mtd
->sync
= onenand_sync
;
1971 mtd
->unlock
= onenand_unlock
;
1972 mtd
->suspend
= onenand_suspend
;
1973 mtd
->resume
= onenand_resume
;
1974 mtd
->block_isbad
= onenand_block_isbad
;
1975 mtd
->block_markbad
= onenand_block_markbad
;
1976 mtd
->owner
= THIS_MODULE
;
1978 /* Unlock whole block */
1979 mtd
->unlock(mtd
, 0x0, this->chipsize
);
1981 return this->scan_bbt(mtd
);
1985 * onenand_release - [OneNAND Interface] Free resources held by the OneNAND device
1986 * @param mtd MTD device structure
1988 void onenand_release(struct mtd_info
*mtd
)
1990 struct onenand_chip
*this = mtd
->priv
;
1992 #ifdef CONFIG_MTD_PARTITIONS
1993 /* Deregister partitions */
1994 del_mtd_partitions (mtd
);
1996 /* Deregister the device */
1997 del_mtd_device (mtd
);
1999 /* Free bad block table memory, if allocated */
2002 /* Buffer allocated by onenand_scan */
2003 if (this->options
& ONENAND_PAGEBUF_ALLOC
)
2004 kfree(this->page_buf
);
2007 EXPORT_SYMBOL_GPL(onenand_scan
);
2008 EXPORT_SYMBOL_GPL(onenand_release
);
2010 MODULE_LICENSE("GPL");
2011 MODULE_AUTHOR("Kyungmin Park <kyungmin.park@samsung.com>");
2012 MODULE_DESCRIPTION("Generic OneNAND flash driver code");